]>
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 void free_sys_dev(struct sys_dev **list) | |
37 | { | |
38 | while (*list) { | |
39 | struct sys_dev *next = (*list)->next; | |
40 | ||
41 | if ((*list)->path) | |
42 | free((*list)->path); | |
43 | free(*list); | |
44 | *list = next; | |
45 | } | |
46 | } | |
47 | ||
48 | struct sys_dev *find_driver_devices(const char *bus, const char *driver) | |
49 | { | |
50 | /* search sysfs for devices driven by 'driver' */ | |
51 | char path[PATH_MAX]; | |
52 | char link[PATH_MAX]; | |
53 | char *c, *p; | |
54 | DIR *driver_dir; | |
55 | struct dirent *de; | |
56 | struct sys_dev *head = NULL; | |
57 | struct sys_dev *list = NULL; | |
58 | struct sys_dev *vmd = NULL; | |
59 | enum sys_dev_type type; | |
60 | unsigned long long dev_id; | |
61 | unsigned long long class; | |
62 | ||
63 | if (strcmp(driver, "isci") == 0) | |
64 | type = SYS_DEV_SAS; | |
65 | else if (strcmp(driver, "ahci") == 0) | |
66 | type = SYS_DEV_SATA; | |
67 | else if (strcmp(driver, "nvme") == 0) { | |
68 | /* if looking for nvme devs, first look for vmd */ | |
69 | vmd = find_driver_devices("pci", "vmd"); | |
70 | type = SYS_DEV_NVME; | |
71 | } else if (strcmp(driver, "vmd") == 0) | |
72 | type = SYS_DEV_VMD; | |
73 | else | |
74 | type = SYS_DEV_UNKNOWN; | |
75 | ||
76 | sprintf(path, "/sys/bus/%s/drivers/%s", bus, driver); | |
77 | driver_dir = opendir(path); | |
78 | if (!driver_dir) { | |
79 | if (vmd) | |
80 | free_sys_dev(&vmd); | |
81 | return NULL; | |
82 | } | |
83 | for (de = readdir(driver_dir); de; de = readdir(driver_dir)) { | |
84 | int n; | |
85 | int skip = 0; | |
86 | ||
87 | /* is 'de' a device? check that the 'subsystem' link exists and | |
88 | * that its target matches 'bus' | |
89 | */ | |
90 | sprintf(path, "/sys/bus/%s/drivers/%s/%s/subsystem", | |
91 | bus, driver, de->d_name); | |
92 | n = readlink(path, link, sizeof(link)); | |
93 | if (n < 0 || n >= (int)sizeof(link)) | |
94 | continue; | |
95 | link[n] = '\0'; | |
96 | c = strrchr(link, '/'); | |
97 | if (!c) | |
98 | continue; | |
99 | if (strncmp(bus, c+1, strlen(bus)) != 0) | |
100 | continue; | |
101 | ||
102 | sprintf(path, "/sys/bus/%s/drivers/%s/%s", | |
103 | bus, driver, de->d_name); | |
104 | ||
105 | /* if searching for nvme - skip vmd connected one */ | |
106 | if (type == SYS_DEV_NVME) { | |
107 | struct sys_dev *dev; | |
108 | char *rp = realpath(path, NULL); | |
109 | for (dev = vmd; dev; dev = dev->next) { | |
110 | if ((strncmp(dev->path, rp, strlen(dev->path)) == 0)) | |
111 | skip = 1; | |
112 | } | |
113 | free(rp); | |
114 | } | |
115 | ||
116 | /* if it's not Intel device or mark as VMD connected - skip it. */ | |
117 | if (devpath_to_vendor(path) != 0x8086 || skip == 1) | |
118 | continue; | |
119 | ||
120 | if (devpath_to_ll(path, "device", &dev_id) != 0) | |
121 | continue; | |
122 | ||
123 | if (devpath_to_ll(path, "class", &class) != 0) | |
124 | continue; | |
125 | ||
126 | /* | |
127 | * Each VMD device (domain) adds separate PCI bus, it is better | |
128 | * to store path as a path to that bus (easier further | |
129 | * determination which NVMe dev is connected to this particular | |
130 | * VMD domain). | |
131 | */ | |
132 | if (type == SYS_DEV_VMD) { | |
133 | sprintf(path, "/sys/bus/%s/drivers/%s/%s/domain/device", | |
134 | bus, driver, de->d_name); | |
135 | } | |
136 | p = realpath(path, NULL); | |
137 | if (p == NULL) { | |
138 | pr_err("Unable to get real path for '%s'\n", path); | |
139 | continue; | |
140 | } | |
141 | ||
142 | /* start / add list entry */ | |
143 | if (!head) { | |
144 | head = xmalloc(sizeof(*head)); | |
145 | list = head; | |
146 | } else { | |
147 | list->next = xmalloc(sizeof(*head)); | |
148 | list = list->next; | |
149 | } | |
150 | ||
151 | if (!list) { | |
152 | free_sys_dev(&head); | |
153 | break; | |
154 | } | |
155 | ||
156 | list->dev_id = (__u16) dev_id; | |
157 | list->class = (__u32) class; | |
158 | list->type = type; | |
159 | list->next = NULL; | |
160 | list->path = p; | |
161 | ||
162 | if ((list->pci_id = strrchr(list->path, '/')) != NULL) | |
163 | list->pci_id++; | |
164 | } | |
165 | closedir(driver_dir); | |
166 | ||
167 | if (vmd) { | |
168 | if (list) | |
169 | list->next = vmd; | |
170 | else | |
171 | head = vmd; | |
172 | } | |
173 | ||
174 | return head; | |
175 | } | |
176 | ||
177 | static struct sys_dev *intel_devices=NULL; | |
178 | static time_t valid_time = 0; | |
179 | ||
180 | struct sys_dev *device_by_id(__u16 device_id) | |
181 | { | |
182 | struct sys_dev *iter; | |
183 | ||
184 | for (iter = intel_devices; iter != NULL; iter = iter->next) | |
185 | if (iter->dev_id == device_id) | |
186 | return iter; | |
187 | return NULL; | |
188 | } | |
189 | ||
190 | struct sys_dev *device_by_id_and_path(__u16 device_id, const char *path) | |
191 | { | |
192 | struct sys_dev *iter; | |
193 | ||
194 | for (iter = intel_devices; iter != NULL; iter = iter->next) | |
195 | if ((iter->dev_id == device_id) && strstr(iter->path, path)) | |
196 | return iter; | |
197 | return NULL; | |
198 | } | |
199 | ||
200 | static int devpath_to_ll(const char *dev_path, const char *entry, unsigned long long *val) | |
201 | { | |
202 | char path[strlen(dev_path) + strlen(entry) + 2]; | |
203 | int fd; | |
204 | int n; | |
205 | ||
206 | sprintf(path, "%s/%s", dev_path, entry); | |
207 | ||
208 | fd = open(path, O_RDONLY); | |
209 | if (fd < 0) | |
210 | return -1; | |
211 | n = sysfs_fd_get_ll(fd, val); | |
212 | close(fd); | |
213 | return n; | |
214 | } | |
215 | ||
216 | __u16 devpath_to_vendor(const char *dev_path) | |
217 | { | |
218 | char path[strlen(dev_path) + strlen("/vendor") + 1]; | |
219 | char vendor[7]; | |
220 | int fd; | |
221 | __u16 id = 0xffff; | |
222 | int n; | |
223 | ||
224 | sprintf(path, "%s/vendor", dev_path); | |
225 | ||
226 | fd = open(path, O_RDONLY); | |
227 | if (fd < 0) | |
228 | return 0xffff; | |
229 | ||
230 | n = read(fd, vendor, sizeof(vendor)); | |
231 | if (n == sizeof(vendor)) { | |
232 | vendor[n - 1] = '\0'; | |
233 | id = strtoul(vendor, NULL, 16); | |
234 | } | |
235 | close(fd); | |
236 | ||
237 | return id; | |
238 | } | |
239 | ||
240 | struct sys_dev *find_intel_devices(void) | |
241 | { | |
242 | struct sys_dev *ahci, *isci, *nvme; | |
243 | ||
244 | if (valid_time > time(0) - 10) | |
245 | return intel_devices; | |
246 | ||
247 | if (intel_devices) | |
248 | free_sys_dev(&intel_devices); | |
249 | ||
250 | isci = find_driver_devices("pci", "isci"); | |
251 | ahci = find_driver_devices("pci", "ahci"); | |
252 | /* Searching for NVMe will return list of NVMe and VMD controllers */ | |
253 | nvme = find_driver_devices("pci", "nvme"); | |
254 | ||
255 | if (!isci && !ahci) { | |
256 | ahci = nvme; | |
257 | } else if (!ahci) { | |
258 | ahci = isci; | |
259 | struct sys_dev *elem = ahci; | |
260 | while (elem->next) | |
261 | elem = elem->next; | |
262 | elem->next = nvme; | |
263 | } else { | |
264 | struct sys_dev *elem = ahci; | |
265 | while (elem->next) | |
266 | elem = elem->next; | |
267 | elem->next = isci; | |
268 | while (elem->next) | |
269 | elem = elem->next; | |
270 | elem->next = nvme; | |
271 | } | |
272 | intel_devices = ahci; | |
273 | valid_time = time(0); | |
274 | return intel_devices; | |
275 | } | |
276 | ||
277 | /* | |
278 | * PCI Expansion ROM Data Structure Format */ | |
279 | struct pciExpDataStructFormat { | |
280 | __u8 ver[4]; | |
281 | __u16 vendorID; | |
282 | __u16 deviceID; | |
283 | __u16 devListOffset; | |
284 | __u16 pciDataStructLen; | |
285 | __u8 pciDataStructRev; | |
286 | } __attribute__ ((packed)); | |
287 | ||
288 | struct orom_entry *orom_entries; | |
289 | ||
290 | const struct orom_entry *get_orom_entry_by_device_id(__u16 dev_id) | |
291 | { | |
292 | struct orom_entry *entry; | |
293 | struct devid_list *devid; | |
294 | ||
295 | for (entry = orom_entries; entry; entry = entry->next) { | |
296 | for (devid = entry->devid_list; devid; devid = devid->next) { | |
297 | if (devid->devid == dev_id) | |
298 | return entry; | |
299 | } | |
300 | } | |
301 | ||
302 | return NULL; | |
303 | } | |
304 | ||
305 | const struct imsm_orom *get_orom_by_device_id(__u16 dev_id) | |
306 | { | |
307 | const struct orom_entry *entry = get_orom_entry_by_device_id(dev_id); | |
308 | ||
309 | if (entry) | |
310 | return &entry->orom; | |
311 | ||
312 | return NULL; | |
313 | } | |
314 | ||
315 | static struct orom_entry *add_orom(const struct imsm_orom *orom) | |
316 | { | |
317 | struct orom_entry *list; | |
318 | struct orom_entry *prev = NULL; | |
319 | ||
320 | for (list = orom_entries; list; prev = list, list = list->next) | |
321 | ; | |
322 | ||
323 | list = xmalloc(sizeof(struct orom_entry)); | |
324 | list->orom = *orom; | |
325 | list->devid_list = NULL; | |
326 | list->next = NULL; | |
327 | ||
328 | if (prev == NULL) | |
329 | orom_entries = list; | |
330 | else | |
331 | prev->next = list; | |
332 | ||
333 | return list; | |
334 | } | |
335 | ||
336 | static void add_orom_device_id(struct orom_entry *entry, __u16 dev_id) | |
337 | { | |
338 | struct devid_list *list; | |
339 | struct devid_list *prev = NULL; | |
340 | ||
341 | for (list = entry->devid_list; list; prev = list, list = list->next) { | |
342 | if (list->devid == dev_id) | |
343 | return; | |
344 | } | |
345 | list = xmalloc(sizeof(struct devid_list)); | |
346 | list->devid = dev_id; | |
347 | list->next = NULL; | |
348 | ||
349 | if (prev == NULL) | |
350 | entry->devid_list = list; | |
351 | else | |
352 | prev->next = list; | |
353 | } | |
354 | ||
355 | static int scan(const void *start, const void *end, const void *data) | |
356 | { | |
357 | int offset; | |
358 | const struct imsm_orom *imsm_mem = NULL; | |
359 | int len = (end - start); | |
360 | struct pciExpDataStructFormat *ptr= (struct pciExpDataStructFormat *)data; | |
361 | ||
362 | if (data + 0x18 > end) { | |
363 | dprintf("cannot find pciExpDataStruct \n"); | |
364 | return 0; | |
365 | } | |
366 | ||
367 | dprintf("ptr->vendorID: %lx __le16_to_cpu(ptr->deviceID): %lx \n", | |
368 | (ulong) __le16_to_cpu(ptr->vendorID), | |
369 | (ulong) __le16_to_cpu(ptr->deviceID)); | |
370 | ||
371 | if (__le16_to_cpu(ptr->vendorID) != 0x8086) | |
372 | return 0; | |
373 | ||
374 | if (get_orom_by_device_id(ptr->deviceID)) | |
375 | return 0; | |
376 | ||
377 | for (offset = 0; offset < len; offset += 4) { | |
378 | const void *mem = start + offset; | |
379 | ||
380 | if ((memcmp(mem, IMSM_OROM_SIGNATURE, 4) == 0)) { | |
381 | imsm_mem = mem; | |
382 | break; | |
383 | } | |
384 | } | |
385 | ||
386 | if (!imsm_mem) | |
387 | return 0; | |
388 | ||
389 | struct orom_entry *orom = add_orom(imsm_mem); | |
390 | ||
391 | /* only PciDataStructure with revision 3 and above supports devices list. */ | |
392 | if (ptr->pciDataStructRev >= 3 && ptr->devListOffset) { | |
393 | const __u16 *dev_list = (void *)ptr + ptr->devListOffset; | |
394 | int i; | |
395 | ||
396 | for (i = 0; dev_list[i] != 0; i++) | |
397 | add_orom_device_id(orom, dev_list[i]); | |
398 | } else { | |
399 | add_orom_device_id(orom, __le16_to_cpu(ptr->deviceID)); | |
400 | } | |
401 | ||
402 | return 0; | |
403 | } | |
404 | ||
405 | const struct imsm_orom *imsm_platform_test(struct sys_dev *hba) | |
406 | { | |
407 | struct imsm_orom orom = { | |
408 | .signature = IMSM_OROM_SIGNATURE, | |
409 | .rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | | |
410 | IMSM_OROM_RLC_RAID10 | IMSM_OROM_RLC_RAID5, | |
411 | .sss = IMSM_OROM_SSS_4kB | IMSM_OROM_SSS_8kB | | |
412 | IMSM_OROM_SSS_16kB | IMSM_OROM_SSS_32kB | | |
413 | IMSM_OROM_SSS_64kB | IMSM_OROM_SSS_128kB | | |
414 | IMSM_OROM_SSS_256kB | IMSM_OROM_SSS_512kB | | |
415 | IMSM_OROM_SSS_1MB | IMSM_OROM_SSS_2MB, | |
416 | .dpa = IMSM_OROM_DISKS_PER_ARRAY, | |
417 | .tds = IMSM_OROM_TOTAL_DISKS, | |
418 | .vpa = IMSM_OROM_VOLUMES_PER_ARRAY, | |
419 | .vphba = IMSM_OROM_VOLUMES_PER_HBA | |
420 | }; | |
421 | orom.attr = orom.rlc | IMSM_OROM_ATTR_ChecksumVerify; | |
422 | ||
423 | if (check_env("IMSM_TEST_OROM_NORAID5")) { | |
424 | orom.rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | | |
425 | IMSM_OROM_RLC_RAID10; | |
426 | } | |
427 | if (check_env("IMSM_TEST_AHCI_EFI_NORAID5") && (hba->type == SYS_DEV_SAS)) { | |
428 | orom.rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | | |
429 | IMSM_OROM_RLC_RAID10; | |
430 | } | |
431 | if (check_env("IMSM_TEST_SCU_EFI_NORAID5") && (hba->type == SYS_DEV_SATA)) { | |
432 | orom.rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | | |
433 | IMSM_OROM_RLC_RAID10; | |
434 | } | |
435 | ||
436 | struct orom_entry *ret = add_orom(&orom); | |
437 | ||
438 | add_orom_device_id(ret, hba->dev_id); | |
439 | ||
440 | return &ret->orom; | |
441 | } | |
442 | ||
443 | static const struct imsm_orom *find_imsm_hba_orom(struct sys_dev *hba) | |
444 | { | |
445 | unsigned long align; | |
446 | ||
447 | if (check_env("IMSM_TEST_OROM")) | |
448 | return imsm_platform_test(hba); | |
449 | ||
450 | /* return empty OROM capabilities in EFI test mode */ | |
451 | if (check_env("IMSM_TEST_AHCI_EFI") || check_env("IMSM_TEST_SCU_EFI")) | |
452 | return NULL; | |
453 | ||
454 | find_intel_devices(); | |
455 | ||
456 | if (intel_devices == NULL) | |
457 | return NULL; | |
458 | ||
459 | /* scan option-rom memory looking for an imsm signature */ | |
460 | if (check_env("IMSM_SAFE_OROM_SCAN")) | |
461 | align = 2048; | |
462 | else | |
463 | align = 512; | |
464 | if (probe_roms_init(align) != 0) | |
465 | return NULL; | |
466 | probe_roms(); | |
467 | /* ignore return value - True is returned if both adapater roms are found */ | |
468 | scan_adapter_roms(scan); | |
469 | probe_roms_exit(); | |
470 | ||
471 | return get_orom_by_device_id(hba->dev_id); | |
472 | } | |
473 | ||
474 | #define GUID_STR_MAX 37 /* according to GUID format: | |
475 | * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" */ | |
476 | ||
477 | #define EFI_GUID(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \ | |
478 | ((struct efi_guid) \ | |
479 | {{ (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) & 0xff, \ | |
480 | (b) & 0xff, ((b) >> 8) & 0xff, \ | |
481 | (c) & 0xff, ((c) >> 8) & 0xff, \ | |
482 | (d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) }}) | |
483 | ||
484 | #define SYS_EFI_VAR_PATH "/sys/firmware/efi/vars" | |
485 | #define SYS_EFIVARS_PATH "/sys/firmware/efi/efivars" | |
486 | #define SCU_PROP "RstScuV" | |
487 | #define AHCI_PROP "RstSataV" | |
488 | #define AHCI_SSATA_PROP "RstsSatV" | |
489 | #define AHCI_CSATA_PROP "RstCSatV" | |
490 | #define VMD_PROP "RstUefiV" | |
491 | ||
492 | #define VENDOR_GUID \ | |
493 | EFI_GUID(0x193dfefa, 0xa445, 0x4302, 0x99, 0xd8, 0xef, 0x3a, 0xad, 0x1a, 0x04, 0xc6) | |
494 | ||
495 | #define PCI_CLASS_RAID_CNTRL 0x010400 | |
496 | ||
497 | static int read_efi_var(void *buffer, ssize_t buf_size, char *variable_name, struct efi_guid guid) | |
498 | { | |
499 | char path[PATH_MAX]; | |
500 | char buf[GUID_STR_MAX]; | |
501 | int fd; | |
502 | ssize_t n; | |
503 | ||
504 | snprintf(path, PATH_MAX, "%s/%s-%s", SYS_EFIVARS_PATH, variable_name, guid_str(buf, guid)); | |
505 | ||
506 | fd = open(path, O_RDONLY); | |
507 | if (fd < 0) | |
508 | return 1; | |
509 | ||
510 | /* read the variable attributes and ignore it */ | |
511 | n = read(fd, buf, sizeof(__u32)); | |
512 | if (n < 0) { | |
513 | close(fd); | |
514 | return 1; | |
515 | } | |
516 | ||
517 | /* read the variable data */ | |
518 | n = read(fd, buffer, buf_size); | |
519 | close(fd); | |
520 | if (n < buf_size) | |
521 | return 1; | |
522 | ||
523 | return 0; | |
524 | } | |
525 | ||
526 | static int read_efi_variable(void *buffer, ssize_t buf_size, char *variable_name, struct efi_guid guid) | |
527 | { | |
528 | char path[PATH_MAX]; | |
529 | char buf[GUID_STR_MAX]; | |
530 | int dfd; | |
531 | ssize_t n, var_data_len; | |
532 | ||
533 | /* Try to read the variable using the new efivarfs interface first. | |
534 | * If that fails, fall back to the old sysfs-efivars interface. */ | |
535 | if (!read_efi_var(buffer, buf_size, variable_name, guid)) | |
536 | return 0; | |
537 | ||
538 | snprintf(path, PATH_MAX, "%s/%s-%s/size", SYS_EFI_VAR_PATH, variable_name, guid_str(buf, guid)); | |
539 | ||
540 | dprintf("EFI VAR: path=%s\n", path); | |
541 | /* get size of variable data */ | |
542 | dfd = open(path, O_RDONLY); | |
543 | if (dfd < 0) | |
544 | return 1; | |
545 | ||
546 | n = read(dfd, &buf, sizeof(buf)); | |
547 | close(dfd); | |
548 | if (n < 0) | |
549 | return 1; | |
550 | buf[n] = '\0'; | |
551 | ||
552 | errno = 0; | |
553 | var_data_len = strtoul(buf, NULL, 16); | |
554 | if ((errno == ERANGE && (var_data_len == LONG_MAX)) || | |
555 | (errno != 0 && var_data_len == 0)) | |
556 | return 1; | |
557 | ||
558 | /* get data */ | |
559 | snprintf(path, PATH_MAX, "%s/%s-%s/data", SYS_EFI_VAR_PATH, variable_name, guid_str(buf, guid)); | |
560 | ||
561 | dprintf("EFI VAR: path=%s\n", path); | |
562 | dfd = open(path, O_RDONLY); | |
563 | if (dfd < 0) | |
564 | return 1; | |
565 | ||
566 | n = read(dfd, buffer, buf_size); | |
567 | close(dfd); | |
568 | if (n != var_data_len || n < buf_size) { | |
569 | return 1; | |
570 | } | |
571 | ||
572 | return 0; | |
573 | } | |
574 | ||
575 | const struct imsm_orom *find_imsm_efi(struct sys_dev *hba) | |
576 | { | |
577 | struct imsm_orom orom; | |
578 | struct orom_entry *ret; | |
579 | int err; | |
580 | ||
581 | if (check_env("IMSM_TEST_AHCI_EFI") || check_env("IMSM_TEST_SCU_EFI")) | |
582 | return imsm_platform_test(hba); | |
583 | ||
584 | /* OROM test is set, return that there is no EFI capabilities */ | |
585 | if (check_env("IMSM_TEST_OROM")) | |
586 | return NULL; | |
587 | ||
588 | if (hba->type == SYS_DEV_SATA && hba->class != PCI_CLASS_RAID_CNTRL) | |
589 | return NULL; | |
590 | ||
591 | err = read_efi_variable(&orom, sizeof(orom), hba->type == SYS_DEV_SAS ? SCU_PROP : AHCI_PROP, VENDOR_GUID); | |
592 | ||
593 | /* try to read variable for second AHCI controller */ | |
594 | if (err && hba->type == SYS_DEV_SATA) | |
595 | err = read_efi_variable(&orom, sizeof(orom), AHCI_SSATA_PROP, VENDOR_GUID); | |
596 | ||
597 | /* try to read variable for combined AHCI controllers */ | |
598 | if (err && hba->type == SYS_DEV_SATA) { | |
599 | static struct orom_entry *csata; | |
600 | ||
601 | err = read_efi_variable(&orom, sizeof(orom), AHCI_CSATA_PROP, VENDOR_GUID); | |
602 | if (!err) { | |
603 | if (!csata) | |
604 | csata = add_orom(&orom); | |
605 | add_orom_device_id(csata, hba->dev_id); | |
606 | csata->type = hba->type; | |
607 | return &csata->orom; | |
608 | } | |
609 | } | |
610 | ||
611 | if (hba->type == SYS_DEV_VMD) { | |
612 | err = read_efi_variable(&orom, sizeof(orom), VMD_PROP, VENDOR_GUID); | |
613 | } | |
614 | ||
615 | if (err) | |
616 | return NULL; | |
617 | ||
618 | ret = add_orom(&orom); | |
619 | add_orom_device_id(ret, hba->dev_id); | |
620 | ret->type = hba->type; | |
621 | ||
622 | return &ret->orom; | |
623 | } | |
624 | ||
625 | const struct imsm_orom *find_imsm_nvme(struct sys_dev *hba) | |
626 | { | |
627 | static struct orom_entry *nvme_orom; | |
628 | ||
629 | if (hba->type != SYS_DEV_NVME) | |
630 | return NULL; | |
631 | ||
632 | if (!nvme_orom) { | |
633 | struct imsm_orom nvme_orom_compat = { | |
634 | .signature = IMSM_NVME_OROM_COMPAT_SIGNATURE, | |
635 | .rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | | |
636 | IMSM_OROM_RLC_RAID10 | IMSM_OROM_RLC_RAID5, | |
637 | .sss = IMSM_OROM_SSS_4kB | IMSM_OROM_SSS_8kB | | |
638 | IMSM_OROM_SSS_16kB | IMSM_OROM_SSS_32kB | | |
639 | IMSM_OROM_SSS_64kB | IMSM_OROM_SSS_128kB, | |
640 | .dpa = IMSM_OROM_DISKS_PER_ARRAY_NVME, | |
641 | .tds = IMSM_OROM_TOTAL_DISKS_NVME, | |
642 | .vpa = IMSM_OROM_VOLUMES_PER_ARRAY, | |
643 | .vphba = IMSM_OROM_TOTAL_DISKS_NVME / 2 * IMSM_OROM_VOLUMES_PER_ARRAY, | |
644 | .attr = IMSM_OROM_ATTR_2TB | IMSM_OROM_ATTR_2TB_DISK, | |
645 | .driver_features = IMSM_OROM_CAPABILITIES_EnterpriseSystem | |
646 | }; | |
647 | nvme_orom = add_orom(&nvme_orom_compat); | |
648 | } | |
649 | add_orom_device_id(nvme_orom, hba->dev_id); | |
650 | nvme_orom->type = SYS_DEV_NVME; | |
651 | return &nvme_orom->orom; | |
652 | } | |
653 | ||
654 | const struct imsm_orom *find_imsm_capability(struct sys_dev *hba) | |
655 | { | |
656 | const struct imsm_orom *cap = get_orom_by_device_id(hba->dev_id); | |
657 | ||
658 | if (cap) | |
659 | return cap; | |
660 | ||
661 | if (hba->type == SYS_DEV_NVME) | |
662 | return find_imsm_nvme(hba); | |
663 | if ((cap = find_imsm_efi(hba)) != NULL) | |
664 | return cap; | |
665 | if ((cap = find_imsm_hba_orom(hba)) != NULL) | |
666 | return cap; | |
667 | ||
668 | return NULL; | |
669 | } | |
670 | ||
671 | char *devt_to_devpath(dev_t dev) | |
672 | { | |
673 | char device[46]; | |
674 | ||
675 | sprintf(device, "/sys/dev/block/%d:%d/device", major(dev), minor(dev)); | |
676 | return realpath(device, NULL); | |
677 | } | |
678 | ||
679 | char *diskfd_to_devpath(int fd) | |
680 | { | |
681 | /* return the device path for a disk, return NULL on error or fd | |
682 | * refers to a partition | |
683 | */ | |
684 | struct stat st; | |
685 | ||
686 | if (fstat(fd, &st) != 0) | |
687 | return NULL; | |
688 | if (!S_ISBLK(st.st_mode)) | |
689 | return NULL; | |
690 | ||
691 | return devt_to_devpath(st.st_rdev); | |
692 | } | |
693 | ||
694 | int path_attached_to_hba(const char *disk_path, const char *hba_path) | |
695 | { | |
696 | int rc; | |
697 | ||
698 | if (check_env("IMSM_TEST_AHCI_DEV") || | |
699 | check_env("IMSM_TEST_SCU_DEV")) { | |
700 | return 1; | |
701 | } | |
702 | ||
703 | if (!disk_path || !hba_path) | |
704 | return 0; | |
705 | dprintf("hba: %s - disk: %s\n", hba_path, disk_path); | |
706 | if (strncmp(disk_path, hba_path, strlen(hba_path)) == 0) | |
707 | rc = 1; | |
708 | else | |
709 | rc = 0; | |
710 | ||
711 | return rc; | |
712 | } | |
713 | ||
714 | int devt_attached_to_hba(dev_t dev, const char *hba_path) | |
715 | { | |
716 | char *disk_path = devt_to_devpath(dev); | |
717 | int rc = path_attached_to_hba(disk_path, hba_path); | |
718 | ||
719 | if (disk_path) | |
720 | free(disk_path); | |
721 | ||
722 | return rc; | |
723 | } | |
724 | ||
725 | int disk_attached_to_hba(int fd, const char *hba_path) | |
726 | { | |
727 | char *disk_path = diskfd_to_devpath(fd); | |
728 | int rc = path_attached_to_hba(disk_path, hba_path); | |
729 | ||
730 | if (disk_path) | |
731 | free(disk_path); | |
732 | ||
733 | return rc; | |
734 | } | |
735 | ||
736 | char *vmd_domain_to_controller(struct sys_dev *hba, char *buf) | |
737 | { | |
738 | struct dirent *ent; | |
739 | DIR *dir; | |
740 | char path[PATH_MAX]; | |
741 | ||
742 | if (!hba) | |
743 | return NULL; | |
744 | ||
745 | if (hba->type != SYS_DEV_VMD) | |
746 | return NULL; | |
747 | ||
748 | dir = opendir("/sys/bus/pci/drivers/vmd"); | |
749 | if (!dir) | |
750 | return NULL; | |
751 | ||
752 | for (ent = readdir(dir); ent; ent = readdir(dir)) { | |
753 | sprintf(path, "/sys/bus/pci/drivers/vmd/%s/domain/device", | |
754 | ent->d_name); | |
755 | ||
756 | if (!realpath(path, buf)) | |
757 | continue; | |
758 | ||
759 | if (strncmp(buf, hba->path, strlen(buf)) == 0) { | |
760 | sprintf(path, "/sys/bus/pci/drivers/vmd/%s", ent->d_name); | |
761 | closedir(dir); | |
762 | return realpath(path, buf); | |
763 | } | |
764 | } | |
765 | ||
766 | closedir(dir); | |
767 | return NULL; | |
768 | } | |
769 | /* Verify that NVMe drive is supported by IMSM | |
770 | * Returns: | |
771 | * 0 - not supported | |
772 | * 1 - supported | |
773 | */ | |
774 | int imsm_is_nvme_supported(int disk_fd, int verbose) | |
775 | { | |
776 | char nsid_path[PATH_MAX]; | |
777 | char buf[PATH_MAX]; | |
778 | struct stat stb; | |
779 | ||
780 | if (disk_fd < 0) | |
781 | return 0; | |
782 | ||
783 | if (fstat(disk_fd, &stb)) | |
784 | return 0; | |
785 | ||
786 | snprintf(nsid_path, PATH_MAX-1, "/sys/dev/block/%d:%d/nsid", | |
787 | major(stb.st_rdev), minor(stb.st_rdev)); | |
788 | ||
789 | if (load_sys(nsid_path, buf, sizeof(buf))) { | |
790 | pr_err("Cannot read %s, rejecting drive\n", nsid_path); | |
791 | return 0; | |
792 | } | |
793 | if (strtoll(buf, NULL, 10) != 1) { | |
794 | if (verbose) | |
795 | pr_err("Only first namespace is supported by IMSM, aborting\n"); | |
796 | return 0; | |
797 | } | |
798 | return 1; | |
799 | } |