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