]> git.ipfire.org Git - thirdparty/mdadm.git/blame - platform-intel.c
probe_roms: allow to probe expansion ROMs using vendor and device id.
[thirdparty/mdadm.git] / platform-intel.c
CommitLineData
b390f610
DW
1/*
2 * Intel(R) Matrix Storage Manager hardware and firmware support routines
3 *
4 * Copyright (C) 2008 Intel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19#include "mdadm.h"
20#include "platform-intel.h"
21#include "probe_roms.h"
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26#include <dirent.h>
27#include <fcntl.h>
28#include <sys/mman.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31
32void free_sys_dev(struct sys_dev **list)
33{
34 while (*list) {
35 struct sys_dev *next = (*list)->next;
36
37 if ((*list)->path)
38 free((*list)->path);
39 free(*list);
40 *list = next;
41 }
42}
43
44struct sys_dev *find_driver_devices(const char *bus, const char *driver)
45{
46 /* search sysfs for devices driven by 'driver' */
5dbb8c8d 47 char path[292];
b390f610
DW
48 char link[256];
49 char *c;
50 DIR *driver_dir;
51 struct dirent *de;
52 struct sys_dev *head = NULL;
53 struct sys_dev *list = NULL;
a8e5382a
LM
54 enum sys_dev_type type;
55
56 if (strcmp(driver, "isci") == 0)
57 type = SYS_DEV_SAS;
58 else if (strcmp(driver, "ahci") == 0)
59 type = SYS_DEV_SATA;
60 else
61 type = SYS_DEV_UNKNOWN;
b390f610
DW
62
63 sprintf(path, "/sys/bus/%s/drivers/%s", bus, driver);
64 driver_dir = opendir(path);
65 if (!driver_dir)
66 return NULL;
67 for (de = readdir(driver_dir); de; de = readdir(driver_dir)) {
5a1920f2
AW
68 int n;
69
b390f610
DW
70 /* is 'de' a device? check that the 'subsystem' link exists and
71 * that its target matches 'bus'
72 */
73 sprintf(path, "/sys/bus/%s/drivers/%s/%s/subsystem",
74 bus, driver, de->d_name);
5a1920f2 75 n = readlink(path, link, sizeof(link));
f21e18ca 76 if (n < 0 || n >= (int)sizeof(link))
b390f610 77 continue;
5a1920f2 78 link[n] = '\0';
b390f610
DW
79 c = strrchr(link, '/');
80 if (!c)
81 continue;
82 if (strncmp(bus, c+1, strlen(bus)) != 0)
83 continue;
84
a8e5382a
LM
85 sprintf(path, "/sys/bus/%s/drivers/%s/%s",
86 bus, driver, de->d_name);
87
88 /* if it's not Intel device skip it. */
89 if (devpath_to_vendor(path) != 0x8086)
90 continue;
91
b390f610
DW
92 /* start / add list entry */
93 if (!head) {
94 head = malloc(sizeof(*head));
95 list = head;
96 } else {
97 list->next = malloc(sizeof(*head));
98 list = list->next;
99 }
100
101 if (!list) {
102 free_sys_dev(&head);
103 break;
104 }
105
a8e5382a 106 list->type = type;
b390f610
DW
107 list->path = canonicalize_file_name(path);
108 list->next = NULL;
a8e5382a
LM
109 if ((list->pci_id = strrchr(list->path, '/')) != NULL)
110 list->pci_id++;
b390f610 111 }
2a17c77b 112 closedir(driver_dir);
b390f610
DW
113 return head;
114}
115
116__u16 devpath_to_vendor(const char *dev_path)
117{
118 char path[strlen(dev_path) + strlen("/vendor") + 1];
119 char vendor[7];
120 int fd;
121 __u16 id = 0xffff;
122 int n;
123
124 sprintf(path, "%s/vendor", dev_path);
125
126 fd = open(path, O_RDONLY);
127 if (fd < 0)
128 return 0xffff;
129
130 n = read(fd, vendor, sizeof(vendor));
131 if (n == sizeof(vendor)) {
132 vendor[n - 1] = '\0';
133 id = strtoul(vendor, NULL, 16);
134 }
135 close(fd);
136
137 return id;
138}
139
a8e5382a 140struct sys_dev *find_intel_devices(void)
b390f610 141{
a8e5382a
LM
142 struct sys_dev *ahci, *isci;
143
144 isci = find_driver_devices("pci", "isci");
145 ahci = find_driver_devices("pci", "ahci");
146
147 if (!ahci) {
148 ahci = isci;
149 } else {
150 struct sys_dev *elem = ahci;
151 while (elem->next)
152 elem = elem->next;
153 elem->next = isci;
154 }
155 return ahci;
b390f610
DW
156}
157
a8e5382a
LM
158static int platform_has_intel_devices(void)
159{
160 struct sys_dev *devices;
161 devices = find_intel_devices();
162 if (devices) {
163 free_sys_dev(&devices);
164 return 1;
165 }
166 return 0;
167}
b390f610 168
3c8bfb5d
LM
169/*
170 * PCI Expansion ROM Data Structure Format
171 */
172struct pciExpDataStructFormat {
173 __u8 ver[4];
174 __u16 vendorID;
175 __u16 deviceID;
176} __attribute__ ((packed));
177
b390f610 178static struct imsm_orom imsm_orom;
3c8bfb5d 179static int scan(const void *start, const void *end, const void *data)
b390f610
DW
180{
181 int offset;
182 const struct imsm_orom *imsm_mem;
183 int len = (end - start);
3c8bfb5d
LM
184 struct pciExpDataStructFormat *ptr= (struct pciExpDataStructFormat *)data;
185
186 dprintf("ptr->vendorID: %lx __le16_to_cpu(ptr->deviceID): %lx \n",
187 (ulong) __le16_to_cpu(ptr->vendorID),
188 (ulong) __le16_to_cpu(ptr->deviceID));
189
190 if (!((__le16_to_cpu(ptr->vendorID) == 0x8086) &&
191 (__le16_to_cpu(ptr->deviceID) == 0x2822)))
192 return 0;
b390f610
DW
193
194 for (offset = 0; offset < len; offset += 4) {
195 imsm_mem = start + offset;
196 if (memcmp(imsm_mem->signature, "$VER", 4) == 0) {
197 imsm_orom = *imsm_mem;
198 return 1;
199 }
200 }
b390f610
DW
201 return 0;
202}
203
204const struct imsm_orom *find_imsm_orom(void)
205{
206 static int populated = 0;
969c2555 207 unsigned long align;
b390f610
DW
208
209 /* it's static data so we only need to read it once */
210 if (populated)
211 return &imsm_orom;
212
cceebc67
DW
213 if (check_env("IMSM_TEST_OROM")) {
214 memset(&imsm_orom, 0, sizeof(imsm_orom));
215 imsm_orom.rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 |
216 IMSM_OROM_RLC_RAID10 | IMSM_OROM_RLC_RAID5;
217 imsm_orom.sss = IMSM_OROM_SSS_4kB | IMSM_OROM_SSS_8kB |
218 IMSM_OROM_SSS_16kB | IMSM_OROM_SSS_32kB |
219 IMSM_OROM_SSS_64kB | IMSM_OROM_SSS_128kB |
220 IMSM_OROM_SSS_256kB | IMSM_OROM_SSS_512kB |
221 IMSM_OROM_SSS_1MB | IMSM_OROM_SSS_2MB;
222 imsm_orom.dpa = 6;
223 imsm_orom.tds = 6;
224 imsm_orom.vpa = 2;
225 imsm_orom.vphba = 4;
226 imsm_orom.attr = imsm_orom.rlc | IMSM_OROM_ATTR_ChecksumVerify;
227 populated = 1;
228 return &imsm_orom;
229 }
230
a8e5382a 231 if (!platform_has_intel_devices())
b390f610
DW
232 return NULL;
233
234 /* scan option-rom memory looking for an imsm signature */
969c2555
DW
235 if (check_env("IMSM_SAFE_OROM_SCAN"))
236 align = 2048;
237 else
238 align = 512;
239 if (probe_roms_init(align) != 0)
b390f610
DW
240 return NULL;
241 probe_roms();
242 populated = scan_adapter_roms(scan);
243 probe_roms_exit();
244
245 if (populated)
246 return &imsm_orom;
247 return NULL;
248}
25921536
DW
249
250char *devt_to_devpath(dev_t dev)
251{
37f0e1e0 252 char device[46];
25921536
DW
253
254 sprintf(device, "/sys/dev/block/%d:%d/device", major(dev), minor(dev));
255 return canonicalize_file_name(device);
256}
257
a8e5382a 258char *diskfd_to_devpath(int fd)
25921536
DW
259{
260 /* return the device path for a disk, return NULL on error or fd
261 * refers to a partition
262 */
263 struct stat st;
264
265 if (fstat(fd, &st) != 0)
266 return NULL;
267 if (!S_ISBLK(st.st_mode))
268 return NULL;
269
270 return devt_to_devpath(st.st_rdev);
271}
272
273int path_attached_to_hba(const char *disk_path, const char *hba_path)
274{
275 int rc;
276
277 if (!disk_path || !hba_path)
278 return 0;
a8e5382a 279 dprintf("hba: %s - disk: %s\n", hba_path, disk_path);
25921536
DW
280 if (strncmp(disk_path, hba_path, strlen(hba_path)) == 0)
281 rc = 1;
282 else
283 rc = 0;
284
285 return rc;
286}
287
288int devt_attached_to_hba(dev_t dev, const char *hba_path)
289{
290 char *disk_path = devt_to_devpath(dev);
291 int rc = path_attached_to_hba(disk_path, hba_path);
292
293 if (disk_path)
294 free(disk_path);
295
296 return rc;
297}
298
299int disk_attached_to_hba(int fd, const char *hba_path)
300{
301 char *disk_path = diskfd_to_devpath(fd);
302 int rc = path_attached_to_hba(disk_path, hba_path);
303
304 if (disk_path)
305 free(disk_path);
306
307 return rc;
308}