]>
Commit | Line | Data |
---|---|---|
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 | #include <limits.h> | |
32 | ||
33 | static int devpath_to_ll(const char *dev_path, const char *entry, | |
34 | unsigned long long *val); | |
35 | ||
36 | static __u16 devpath_to_vendor(const char *dev_path); | |
37 | ||
38 | static void free_sys_dev(struct sys_dev **list) | |
39 | { | |
40 | while (*list) { | |
41 | struct sys_dev *next = (*list)->next; | |
42 | ||
43 | if ((*list)->path) | |
44 | free((*list)->path); | |
45 | free(*list); | |
46 | *list = next; | |
47 | } | |
48 | } | |
49 | ||
50 | struct sys_dev *find_driver_devices(const char *bus, const char *driver) | |
51 | { | |
52 | /* search sysfs for devices driven by 'driver' */ | |
53 | char path[292]; | |
54 | char link[256]; | |
55 | char *c; | |
56 | DIR *driver_dir; | |
57 | struct dirent *de; | |
58 | struct sys_dev *head = NULL; | |
59 | struct sys_dev *list = NULL; | |
60 | enum sys_dev_type type; | |
61 | unsigned long long dev_id; | |
62 | unsigned long long class; | |
63 | ||
64 | if (strcmp(driver, "isci") == 0) | |
65 | type = SYS_DEV_SAS; | |
66 | else if (strcmp(driver, "ahci") == 0) | |
67 | type = SYS_DEV_SATA; | |
68 | else | |
69 | type = SYS_DEV_UNKNOWN; | |
70 | ||
71 | sprintf(path, "/sys/bus/%s/drivers/%s", bus, driver); | |
72 | driver_dir = opendir(path); | |
73 | if (!driver_dir) | |
74 | return NULL; | |
75 | for (de = readdir(driver_dir); de; de = readdir(driver_dir)) { | |
76 | int n; | |
77 | ||
78 | /* is 'de' a device? check that the 'subsystem' link exists and | |
79 | * that its target matches 'bus' | |
80 | */ | |
81 | sprintf(path, "/sys/bus/%s/drivers/%s/%s/subsystem", | |
82 | bus, driver, de->d_name); | |
83 | n = readlink(path, link, sizeof(link)); | |
84 | if (n < 0 || n >= (int)sizeof(link)) | |
85 | continue; | |
86 | link[n] = '\0'; | |
87 | c = strrchr(link, '/'); | |
88 | if (!c) | |
89 | continue; | |
90 | if (strncmp(bus, c+1, strlen(bus)) != 0) | |
91 | continue; | |
92 | ||
93 | sprintf(path, "/sys/bus/%s/drivers/%s/%s", | |
94 | bus, driver, de->d_name); | |
95 | ||
96 | /* if it's not Intel device skip it. */ | |
97 | if (devpath_to_vendor(path) != 0x8086) | |
98 | continue; | |
99 | ||
100 | if (devpath_to_ll(path, "device", &dev_id) != 0) | |
101 | continue; | |
102 | ||
103 | if (devpath_to_ll(path, "class", &class) != 0) | |
104 | continue; | |
105 | ||
106 | /* start / add list entry */ | |
107 | if (!head) { | |
108 | head = xmalloc(sizeof(*head)); | |
109 | list = head; | |
110 | } else { | |
111 | list->next = xmalloc(sizeof(*head)); | |
112 | list = list->next; | |
113 | } | |
114 | ||
115 | if (!list) { | |
116 | free_sys_dev(&head); | |
117 | break; | |
118 | } | |
119 | ||
120 | list->dev_id = (__u16) dev_id; | |
121 | list->class = (__u32) class; | |
122 | list->type = type; | |
123 | list->path = realpath(path, NULL); | |
124 | list->next = NULL; | |
125 | if ((list->pci_id = strrchr(list->path, '/')) != NULL) | |
126 | list->pci_id++; | |
127 | } | |
128 | closedir(driver_dir); | |
129 | return head; | |
130 | } | |
131 | ||
132 | static struct sys_dev *intel_devices=NULL; | |
133 | static time_t valid_time = 0; | |
134 | ||
135 | static int devpath_to_ll(const char *dev_path, const char *entry, unsigned long long *val) | |
136 | { | |
137 | char path[strlen(dev_path) + strlen(entry) + 2]; | |
138 | int fd; | |
139 | int n; | |
140 | ||
141 | sprintf(path, "%s/%s", dev_path, entry); | |
142 | ||
143 | fd = open(path, O_RDONLY); | |
144 | if (fd < 0) | |
145 | return -1; | |
146 | n = sysfs_fd_get_ll(fd, val); | |
147 | close(fd); | |
148 | return n; | |
149 | } | |
150 | ||
151 | static __u16 devpath_to_vendor(const char *dev_path) | |
152 | { | |
153 | char path[strlen(dev_path) + strlen("/vendor") + 1]; | |
154 | char vendor[7]; | |
155 | int fd; | |
156 | __u16 id = 0xffff; | |
157 | int n; | |
158 | ||
159 | sprintf(path, "%s/vendor", dev_path); | |
160 | ||
161 | fd = open(path, O_RDONLY); | |
162 | if (fd < 0) | |
163 | return 0xffff; | |
164 | ||
165 | n = read(fd, vendor, sizeof(vendor)); | |
166 | if (n == sizeof(vendor)) { | |
167 | vendor[n - 1] = '\0'; | |
168 | id = strtoul(vendor, NULL, 16); | |
169 | } | |
170 | close(fd); | |
171 | ||
172 | return id; | |
173 | } | |
174 | ||
175 | struct sys_dev *find_intel_devices(void) | |
176 | { | |
177 | struct sys_dev *ahci, *isci; | |
178 | ||
179 | if (valid_time > time(0) - 10) | |
180 | return intel_devices; | |
181 | ||
182 | if (intel_devices) | |
183 | free_sys_dev(&intel_devices); | |
184 | ||
185 | isci = find_driver_devices("pci", "isci"); | |
186 | ahci = find_driver_devices("pci", "ahci"); | |
187 | ||
188 | if (!ahci) { | |
189 | ahci = isci; | |
190 | } else { | |
191 | struct sys_dev *elem = ahci; | |
192 | while (elem->next) | |
193 | elem = elem->next; | |
194 | elem->next = isci; | |
195 | } | |
196 | intel_devices = ahci; | |
197 | valid_time = time(0); | |
198 | return intel_devices; | |
199 | } | |
200 | ||
201 | /* | |
202 | * PCI Expansion ROM Data Structure Format */ | |
203 | struct pciExpDataStructFormat { | |
204 | __u8 ver[4]; | |
205 | __u16 vendorID; | |
206 | __u16 deviceID; | |
207 | __u16 devListOffset; | |
208 | } __attribute__ ((packed)); | |
209 | ||
210 | struct devid_list { | |
211 | __u16 devid; | |
212 | struct devid_list *next; | |
213 | }; | |
214 | ||
215 | struct orom_entry { | |
216 | struct imsm_orom orom; | |
217 | struct devid_list *devid_list; | |
218 | }; | |
219 | ||
220 | static struct orom_entry oroms[SYS_DEV_MAX]; | |
221 | ||
222 | const struct imsm_orom *get_orom_by_device_id(__u16 dev_id) | |
223 | { | |
224 | int i; | |
225 | struct devid_list *list; | |
226 | ||
227 | for (i = 0; i < SYS_DEV_MAX; i++) { | |
228 | for (list = oroms[i].devid_list; list; list = list->next) { | |
229 | if (list->devid == dev_id) | |
230 | return &oroms[i].orom; | |
231 | } | |
232 | } | |
233 | return NULL; | |
234 | } | |
235 | ||
236 | static const struct imsm_orom *add_orom(const struct imsm_orom *orom) | |
237 | { | |
238 | int i; | |
239 | ||
240 | for (i = 0; i < SYS_DEV_MAX; i++) { | |
241 | if (&oroms[i].orom == orom) | |
242 | return orom; | |
243 | if (oroms[i].orom.signature[0] == 0) { | |
244 | oroms[i].orom = *orom; | |
245 | return &oroms[i].orom; | |
246 | } | |
247 | } | |
248 | return NULL; | |
249 | } | |
250 | ||
251 | static void add_orom_device_id(const struct imsm_orom *orom, __u16 dev_id) | |
252 | { | |
253 | int i; | |
254 | struct devid_list *list; | |
255 | struct devid_list *prev = NULL; | |
256 | ||
257 | for (i = 0; i < SYS_DEV_MAX; i++) { | |
258 | if (&oroms[i].orom == orom) { | |
259 | for (list = oroms[i].devid_list; list; prev = list, list = list->next) { | |
260 | if (list->devid == dev_id) | |
261 | return; | |
262 | } | |
263 | list = xmalloc(sizeof(struct devid_list)); | |
264 | list->devid = dev_id; | |
265 | list->next = NULL; | |
266 | ||
267 | if (prev == NULL) | |
268 | oroms[i].devid_list = list; | |
269 | else | |
270 | prev->next = list; | |
271 | return; | |
272 | } | |
273 | } | |
274 | } | |
275 | ||
276 | static int scan(const void *start, const void *end, const void *data) | |
277 | { | |
278 | int offset; | |
279 | const struct imsm_orom *imsm_mem = NULL; | |
280 | int len = (end - start); | |
281 | struct pciExpDataStructFormat *ptr= (struct pciExpDataStructFormat *)data; | |
282 | ||
283 | if (data + 0x18 > end) { | |
284 | dprintf("cannot find pciExpDataStruct \n"); | |
285 | return 0; | |
286 | } | |
287 | ||
288 | dprintf("ptr->vendorID: %lx __le16_to_cpu(ptr->deviceID): %lx \n", | |
289 | (ulong) __le16_to_cpu(ptr->vendorID), | |
290 | (ulong) __le16_to_cpu(ptr->deviceID)); | |
291 | ||
292 | if (__le16_to_cpu(ptr->vendorID) != 0x8086) | |
293 | return 0; | |
294 | ||
295 | for (offset = 0; offset < len; offset += 4) { | |
296 | const void *mem = start + offset; | |
297 | ||
298 | if ((memcmp(mem, IMSM_OROM_SIGNATURE, 4) == 0)) { | |
299 | imsm_mem = mem; | |
300 | break; | |
301 | } | |
302 | } | |
303 | ||
304 | if (!imsm_mem) | |
305 | return 0; | |
306 | ||
307 | const struct imsm_orom *orom = add_orom(imsm_mem); | |
308 | ||
309 | if (ptr->devListOffset) { | |
310 | const __u16 *dev_list = (void *)ptr + ptr->devListOffset; | |
311 | int i; | |
312 | ||
313 | for (i = 0; dev_list[i] != 0; i++) | |
314 | add_orom_device_id(orom, dev_list[i]); | |
315 | } else { | |
316 | add_orom_device_id(orom, __le16_to_cpu(ptr->deviceID)); | |
317 | } | |
318 | ||
319 | return 0; | |
320 | } | |
321 | ||
322 | const struct imsm_orom *imsm_platform_test(struct sys_dev *hba) | |
323 | { | |
324 | struct imsm_orom orom = { | |
325 | .signature = IMSM_OROM_SIGNATURE, | |
326 | .rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | | |
327 | IMSM_OROM_RLC_RAID10 | IMSM_OROM_RLC_RAID5, | |
328 | .sss = IMSM_OROM_SSS_4kB | IMSM_OROM_SSS_8kB | | |
329 | IMSM_OROM_SSS_16kB | IMSM_OROM_SSS_32kB | | |
330 | IMSM_OROM_SSS_64kB | IMSM_OROM_SSS_128kB | | |
331 | IMSM_OROM_SSS_256kB | IMSM_OROM_SSS_512kB | | |
332 | IMSM_OROM_SSS_1MB | IMSM_OROM_SSS_2MB, | |
333 | .dpa = IMSM_OROM_DISKS_PER_ARRAY, | |
334 | .tds = IMSM_OROM_TOTAL_DISKS, | |
335 | .vpa = IMSM_OROM_VOLUMES_PER_ARRAY, | |
336 | .vphba = IMSM_OROM_VOLUMES_PER_HBA | |
337 | }; | |
338 | orom.attr = orom.rlc | IMSM_OROM_ATTR_ChecksumVerify; | |
339 | ||
340 | if (check_env("IMSM_TEST_OROM_NORAID5")) { | |
341 | orom.rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | | |
342 | IMSM_OROM_RLC_RAID10; | |
343 | } | |
344 | if (check_env("IMSM_TEST_AHCI_EFI_NORAID5") && (hba->type == SYS_DEV_SAS)) { | |
345 | orom.rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | | |
346 | IMSM_OROM_RLC_RAID10; | |
347 | } | |
348 | if (check_env("IMSM_TEST_SCU_EFI_NORAID5") && (hba->type == SYS_DEV_SATA)) { | |
349 | orom.rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | | |
350 | IMSM_OROM_RLC_RAID10; | |
351 | } | |
352 | ||
353 | const struct imsm_orom *ret = add_orom(&orom); | |
354 | ||
355 | add_orom_device_id(ret, hba->dev_id); | |
356 | ||
357 | return ret; | |
358 | } | |
359 | ||
360 | static const struct imsm_orom *find_imsm_hba_orom(struct sys_dev *hba) | |
361 | { | |
362 | unsigned long align; | |
363 | ||
364 | if (check_env("IMSM_TEST_OROM")) | |
365 | return imsm_platform_test(hba); | |
366 | ||
367 | /* return empty OROM capabilities in EFI test mode */ | |
368 | if (check_env("IMSM_TEST_AHCI_EFI") || check_env("IMSM_TEST_SCU_EFI")) | |
369 | return NULL; | |
370 | ||
371 | find_intel_devices(); | |
372 | ||
373 | if (intel_devices == NULL) | |
374 | return NULL; | |
375 | ||
376 | /* scan option-rom memory looking for an imsm signature */ | |
377 | if (check_env("IMSM_SAFE_OROM_SCAN")) | |
378 | align = 2048; | |
379 | else | |
380 | align = 512; | |
381 | if (probe_roms_init(align) != 0) | |
382 | return NULL; | |
383 | probe_roms(); | |
384 | /* ignore return value - True is returned if both adapater roms are found */ | |
385 | scan_adapter_roms(scan); | |
386 | probe_roms_exit(); | |
387 | ||
388 | return get_orom_by_device_id(hba->dev_id); | |
389 | } | |
390 | ||
391 | #define GUID_STR_MAX 37 /* according to GUID format: | |
392 | * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" */ | |
393 | ||
394 | #define EFI_GUID(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \ | |
395 | ((struct efi_guid) \ | |
396 | {{ (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) & 0xff, \ | |
397 | (b) & 0xff, ((b) >> 8) & 0xff, \ | |
398 | (c) & 0xff, ((c) >> 8) & 0xff, \ | |
399 | (d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) }}) | |
400 | ||
401 | #define SYS_EFI_VAR_PATH "/sys/firmware/efi/vars" | |
402 | #define SCU_PROP "RstScuV" | |
403 | #define AHCI_PROP "RstSataV" | |
404 | ||
405 | #define VENDOR_GUID \ | |
406 | EFI_GUID(0x193dfefa, 0xa445, 0x4302, 0x99, 0xd8, 0xef, 0x3a, 0xad, 0x1a, 0x04, 0xc6) | |
407 | ||
408 | #define PCI_CLASS_RAID_CNTRL 0x010400 | |
409 | ||
410 | int read_efi_variable(void *buffer, ssize_t buf_size, char *variable_name, struct efi_guid guid) | |
411 | { | |
412 | char path[PATH_MAX]; | |
413 | char buf[GUID_STR_MAX]; | |
414 | int dfd; | |
415 | ssize_t n, var_data_len; | |
416 | ||
417 | snprintf(path, PATH_MAX, "%s/%s-%s/size", SYS_EFI_VAR_PATH, variable_name, guid_str(buf, guid)); | |
418 | ||
419 | dprintf("EFI VAR: path=%s\n", path); | |
420 | /* get size of variable data */ | |
421 | dfd = open(path, O_RDONLY); | |
422 | if (dfd < 0) | |
423 | return 1; | |
424 | ||
425 | n = read(dfd, &buf, sizeof(buf)); | |
426 | close(dfd); | |
427 | if (n < 0) | |
428 | return 1; | |
429 | buf[n] = '\0'; | |
430 | ||
431 | errno = 0; | |
432 | var_data_len = strtoul(buf, NULL, 16); | |
433 | if ((errno == ERANGE && (var_data_len == LONG_MAX)) | |
434 | || (errno != 0 && var_data_len == 0)) | |
435 | return 1; | |
436 | ||
437 | /* get data */ | |
438 | snprintf(path, PATH_MAX, "%s/%s-%s/data", SYS_EFI_VAR_PATH, variable_name, guid_str(buf, guid)); | |
439 | ||
440 | dprintf("EFI VAR: path=%s\n", path); | |
441 | dfd = open(path, O_RDONLY); | |
442 | if (dfd < 0) | |
443 | return 1; | |
444 | ||
445 | n = read(dfd, buffer, buf_size); | |
446 | close(dfd); | |
447 | if (n != var_data_len || n < buf_size) { | |
448 | return 1; | |
449 | } | |
450 | ||
451 | return 0; | |
452 | } | |
453 | ||
454 | const struct imsm_orom *find_imsm_efi(struct sys_dev *hba) | |
455 | { | |
456 | struct imsm_orom orom; | |
457 | const struct imsm_orom *ret; | |
458 | ||
459 | if (check_env("IMSM_TEST_AHCI_EFI") || check_env("IMSM_TEST_SCU_EFI")) | |
460 | return imsm_platform_test(hba); | |
461 | ||
462 | /* OROM test is set, return that there is no EFI capabilities */ | |
463 | if (check_env("IMSM_TEST_OROM")) | |
464 | return NULL; | |
465 | ||
466 | if (hba->type == SYS_DEV_SATA && hba->class != PCI_CLASS_RAID_CNTRL) | |
467 | return NULL; | |
468 | ||
469 | if (read_efi_variable(&orom, sizeof(orom), hba->type == SYS_DEV_SAS ? SCU_PROP : AHCI_PROP, VENDOR_GUID)) | |
470 | return NULL; | |
471 | ||
472 | ret = add_orom(&orom); | |
473 | add_orom_device_id(ret, hba->dev_id); | |
474 | ||
475 | return ret; | |
476 | } | |
477 | ||
478 | const struct imsm_orom *find_imsm_capability(struct sys_dev *hba) | |
479 | { | |
480 | const struct imsm_orom *cap = get_orom_by_device_id(hba->dev_id); | |
481 | ||
482 | if (cap) | |
483 | return cap; | |
484 | ||
485 | if ((cap = find_imsm_efi(hba)) != NULL) | |
486 | return cap; | |
487 | if ((cap = find_imsm_hba_orom(hba)) != NULL) | |
488 | return cap; | |
489 | return NULL; | |
490 | } | |
491 | ||
492 | char *devt_to_devpath(dev_t dev) | |
493 | { | |
494 | char device[46]; | |
495 | ||
496 | sprintf(device, "/sys/dev/block/%d:%d/device", major(dev), minor(dev)); | |
497 | return realpath(device, NULL); | |
498 | } | |
499 | ||
500 | char *diskfd_to_devpath(int fd) | |
501 | { | |
502 | /* return the device path for a disk, return NULL on error or fd | |
503 | * refers to a partition | |
504 | */ | |
505 | struct stat st; | |
506 | ||
507 | if (fstat(fd, &st) != 0) | |
508 | return NULL; | |
509 | if (!S_ISBLK(st.st_mode)) | |
510 | return NULL; | |
511 | ||
512 | return devt_to_devpath(st.st_rdev); | |
513 | } | |
514 | ||
515 | int path_attached_to_hba(const char *disk_path, const char *hba_path) | |
516 | { | |
517 | int rc; | |
518 | ||
519 | if (check_env("IMSM_TEST_AHCI_DEV") || | |
520 | check_env("IMSM_TEST_SCU_DEV")) { | |
521 | return 1; | |
522 | } | |
523 | ||
524 | if (!disk_path || !hba_path) | |
525 | return 0; | |
526 | dprintf("hba: %s - disk: %s\n", hba_path, disk_path); | |
527 | if (strncmp(disk_path, hba_path, strlen(hba_path)) == 0) | |
528 | rc = 1; | |
529 | else | |
530 | rc = 0; | |
531 | ||
532 | return rc; | |
533 | } | |
534 | ||
535 | int devt_attached_to_hba(dev_t dev, const char *hba_path) | |
536 | { | |
537 | char *disk_path = devt_to_devpath(dev); | |
538 | int rc = path_attached_to_hba(disk_path, hba_path); | |
539 | ||
540 | if (disk_path) | |
541 | free(disk_path); | |
542 | ||
543 | return rc; | |
544 | } | |
545 | ||
546 | int disk_attached_to_hba(int fd, const char *hba_path) | |
547 | { | |
548 | char *disk_path = diskfd_to_devpath(fd); | |
549 | int rc = path_attached_to_hba(disk_path, hba_path); | |
550 | ||
551 | if (disk_path) | |
552 | free(disk_path); | |
553 | ||
554 | return rc; | |
555 | } |