]> git.ipfire.org Git - thirdparty/mdadm.git/blob - platform-intel.c
platform-intel: limit guid length
[thirdparty/mdadm.git] / platform-intel.c
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 #define NVME_SUBSYS_PATH "/sys/devices/virtual/nvme-subsystem/"
34
35 static int devpath_to_ll(const char *dev_path, const char *entry,
36 unsigned long long *val);
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[PATH_MAX];
54 char link[PATH_MAX];
55 char *c, *p;
56 DIR *driver_dir;
57 struct dirent *de;
58 struct sys_dev *head = NULL;
59 struct sys_dev *list = NULL;
60 struct sys_dev *vmd = NULL;
61 enum sys_dev_type type;
62 unsigned long long dev_id;
63 unsigned long long class;
64
65 if (strcmp(driver, "isci") == 0)
66 type = SYS_DEV_SAS;
67 else if (strcmp(driver, "ahci") == 0) {
68 vmd = find_driver_devices("pci", "vmd");
69 type = SYS_DEV_SATA;
70 } else if (strcmp(driver, "nvme") == 0) {
71 /* if looking for nvme devs, first look for vmd */
72 vmd = find_driver_devices("pci", "vmd");
73 type = SYS_DEV_NVME;
74 } else if (strcmp(driver, "vmd") == 0)
75 type = SYS_DEV_VMD;
76 else
77 type = SYS_DEV_UNKNOWN;
78
79 sprintf(path, "/sys/bus/%s/drivers/%s", bus, driver);
80 driver_dir = opendir(path);
81 if (!driver_dir) {
82 if (vmd)
83 free_sys_dev(&vmd);
84 return NULL;
85 }
86 for (de = readdir(driver_dir); de; de = readdir(driver_dir)) {
87 int n;
88 int skip = 0;
89
90 /* is 'de' a device? check that the 'subsystem' link exists and
91 * that its target matches 'bus'
92 */
93 sprintf(path, "/sys/bus/%s/drivers/%s/%s/subsystem",
94 bus, driver, de->d_name);
95 n = readlink(path, link, sizeof(link));
96 if (n < 0 || n >= (int)sizeof(link))
97 continue;
98 link[n] = '\0';
99 c = strrchr(link, '/');
100 if (!c)
101 continue;
102 if (strncmp(bus, c+1, strlen(bus)) != 0)
103 continue;
104
105 sprintf(path, "/sys/bus/%s/drivers/%s/%s",
106 bus, driver, de->d_name);
107
108 /* if searching for nvme - skip vmd connected one */
109 if (type == SYS_DEV_NVME) {
110 struct sys_dev *dev;
111 char *rp = realpath(path, NULL);
112 for (dev = vmd; dev; dev = dev->next) {
113 if ((strncmp(dev->path, rp, strlen(dev->path)) == 0))
114 skip = 1;
115 }
116 free(rp);
117 }
118
119 /* change sata type if under a vmd controller */
120 if (type == SYS_DEV_SATA) {
121 struct sys_dev *dev;
122 char *rp = realpath(path, NULL);
123 for (dev = vmd; dev; dev = dev->next) {
124 if ((strncmp(dev->path, rp, strlen(dev->path)) == 0))
125 type = SYS_DEV_SATA_VMD;
126 }
127 free(rp);
128 }
129
130 /* if it's not Intel device or mark as VMD connected - skip it. */
131 if (devpath_to_vendor(path) != 0x8086 || skip == 1)
132 continue;
133
134 if (devpath_to_ll(path, "device", &dev_id) != 0)
135 continue;
136
137 if (devpath_to_ll(path, "class", &class) != 0)
138 continue;
139
140 /*
141 * Each VMD device (domain) adds separate PCI bus, it is better
142 * to store path as a path to that bus (easier further
143 * determination which NVMe dev is connected to this particular
144 * VMD domain).
145 */
146 if (type == SYS_DEV_VMD) {
147 sprintf(path, "/sys/bus/%s/drivers/%s/%s/domain/device",
148 bus, driver, de->d_name);
149 }
150 p = realpath(path, NULL);
151 if (p == NULL) {
152 pr_err("Unable to get real path for '%s'\n", path);
153 continue;
154 }
155
156 /* start / add list entry */
157 if (!head) {
158 head = xmalloc(sizeof(*head));
159 list = head;
160 } else {
161 list->next = xmalloc(sizeof(*head));
162 list = list->next;
163 }
164
165 if (!list) {
166 free_sys_dev(&head);
167 break;
168 }
169
170 list->dev_id = (__u16) dev_id;
171 list->class = (__u32) class;
172 list->type = type;
173 list->next = NULL;
174 list->path = p;
175
176 if ((list->pci_id = strrchr(list->path, '/')) != NULL)
177 list->pci_id++;
178 }
179 closedir(driver_dir);
180
181 /* nvme vmd needs a list separate from sata vmd */
182 if (vmd && type == SYS_DEV_NVME) {
183 if (list)
184 list->next = vmd;
185 else
186 head = vmd;
187 }
188
189 return head;
190 }
191
192 static struct sys_dev *intel_devices=NULL;
193 static time_t valid_time = 0;
194
195 struct sys_dev *device_by_id(__u16 device_id)
196 {
197 struct sys_dev *iter;
198
199 for (iter = intel_devices; iter != NULL; iter = iter->next)
200 if (iter->dev_id == device_id)
201 return iter;
202 return NULL;
203 }
204
205 struct sys_dev *device_by_id_and_path(__u16 device_id, const char *path)
206 {
207 struct sys_dev *iter;
208
209 for (iter = intel_devices; iter != NULL; iter = iter->next)
210 if ((iter->dev_id == device_id) && strstr(iter->path, path))
211 return iter;
212 return NULL;
213 }
214
215 static int devpath_to_ll(const char *dev_path, const char *entry, unsigned long long *val)
216 {
217 char path[strnlen(dev_path, PATH_MAX) + strnlen(entry, PATH_MAX) + 2];
218 int fd;
219 int n;
220
221 sprintf(path, "%s/%s", dev_path, entry);
222
223 fd = open(path, O_RDONLY);
224 if (fd < 0)
225 return -1;
226 n = sysfs_fd_get_ll(fd, val);
227 close(fd);
228 return n;
229 }
230
231 __u16 devpath_to_vendor(const char *dev_path)
232 {
233 char path[strlen(dev_path) + strlen("/vendor") + 1];
234 char vendor[7];
235 int fd;
236 __u16 id = 0xffff;
237 int n;
238
239 sprintf(path, "%s/vendor", dev_path);
240
241 fd = open(path, O_RDONLY);
242 if (fd < 0)
243 return 0xffff;
244
245 n = read(fd, vendor, sizeof(vendor));
246 if (n == sizeof(vendor)) {
247 vendor[n - 1] = '\0';
248 id = strtoul(vendor, NULL, 16);
249 }
250 close(fd);
251
252 return id;
253 }
254
255 /* Description: Read text value of dev_path/entry field
256 * Parameters:
257 * dev_path - sysfs path to the device
258 * entry - entry to be read
259 * buf - buffer for read value
260 * len - size of buf
261 * verbose - error logging level
262 */
263 int devpath_to_char(const char *dev_path, const char *entry, char *buf, int len,
264 int verbose)
265 {
266 char path[PATH_MAX];
267
268 snprintf(path, sizeof(path), "%s/%s", dev_path, entry);
269 if (load_sys(path, buf, len)) {
270 if (verbose)
271 pr_err("Cannot read %s, aborting\n", path);
272 return 1;
273 }
274
275 return 0;
276 }
277
278 struct sys_dev *find_intel_devices(void)
279 {
280 struct sys_dev *ahci, *isci, *nvme;
281
282 if (valid_time > time(0) - 10)
283 return intel_devices;
284
285 if (intel_devices)
286 free_sys_dev(&intel_devices);
287
288 isci = find_driver_devices("pci", "isci");
289 /* Searching for AHCI will return list of SATA and SATA VMD controllers */
290 ahci = find_driver_devices("pci", "ahci");
291 /* Searching for NVMe will return list of NVMe and VMD controllers */
292 nvme = find_driver_devices("pci", "nvme");
293
294 if (!isci && !ahci) {
295 ahci = nvme;
296 } else if (!ahci) {
297 ahci = isci;
298 struct sys_dev *elem = ahci;
299 while (elem->next)
300 elem = elem->next;
301 elem->next = nvme;
302 } else {
303 struct sys_dev *elem = ahci;
304 while (elem->next)
305 elem = elem->next;
306 elem->next = isci;
307 while (elem->next)
308 elem = elem->next;
309 elem->next = nvme;
310 }
311 intel_devices = ahci;
312 valid_time = time(0);
313 return intel_devices;
314 }
315
316 /*
317 * PCI Expansion ROM Data Structure Format */
318 struct pciExpDataStructFormat {
319 __u8 ver[4];
320 __u16 vendorID;
321 __u16 deviceID;
322 __u16 devListOffset;
323 __u16 pciDataStructLen;
324 __u8 pciDataStructRev;
325 } __attribute__ ((packed));
326
327 struct orom_entry *orom_entries;
328
329 const struct orom_entry *get_orom_entry_by_device_id(__u16 dev_id)
330 {
331 struct orom_entry *entry;
332 struct devid_list *devid;
333
334 for (entry = orom_entries; entry; entry = entry->next) {
335 for (devid = entry->devid_list; devid; devid = devid->next) {
336 if (devid->devid == dev_id)
337 return entry;
338 }
339 }
340
341 return NULL;
342 }
343
344 const struct imsm_orom *get_orom_by_device_id(__u16 dev_id)
345 {
346 const struct orom_entry *entry = get_orom_entry_by_device_id(dev_id);
347
348 if (entry)
349 return &entry->orom;
350
351 return NULL;
352 }
353
354 static struct orom_entry *add_orom(const struct imsm_orom *orom)
355 {
356 struct orom_entry *list;
357 struct orom_entry *prev = NULL;
358
359 for (list = orom_entries; list; prev = list, list = list->next)
360 ;
361
362 list = xmalloc(sizeof(struct orom_entry));
363 list->orom = *orom;
364 list->devid_list = NULL;
365 list->next = NULL;
366
367 if (prev == NULL)
368 orom_entries = list;
369 else
370 prev->next = list;
371
372 return list;
373 }
374
375 static void add_orom_device_id(struct orom_entry *entry, __u16 dev_id)
376 {
377 struct devid_list *list;
378 struct devid_list *prev = NULL;
379
380 for (list = entry->devid_list; list; prev = list, list = list->next) {
381 if (list->devid == dev_id)
382 return;
383 }
384 list = xmalloc(sizeof(struct devid_list));
385 list->devid = dev_id;
386 list->next = NULL;
387
388 if (prev == NULL)
389 entry->devid_list = list;
390 else
391 prev->next = list;
392 }
393
394 static int scan(const void *start, const void *end, const void *data)
395 {
396 int offset;
397 const struct imsm_orom *imsm_mem = NULL;
398 int len = (end - start);
399 struct pciExpDataStructFormat *ptr= (struct pciExpDataStructFormat *)data;
400
401 if (data + 0x18 > end) {
402 dprintf("cannot find pciExpDataStruct \n");
403 return 0;
404 }
405
406 dprintf("ptr->vendorID: %lx __le16_to_cpu(ptr->deviceID): %lx \n",
407 (ulong) __le16_to_cpu(ptr->vendorID),
408 (ulong) __le16_to_cpu(ptr->deviceID));
409
410 if (__le16_to_cpu(ptr->vendorID) != 0x8086)
411 return 0;
412
413 if (get_orom_by_device_id(ptr->deviceID))
414 return 0;
415
416 for (offset = 0; offset < len; offset += 4) {
417 const void *mem = start + offset;
418
419 if ((memcmp(mem, IMSM_OROM_SIGNATURE, 4) == 0)) {
420 imsm_mem = mem;
421 break;
422 }
423 }
424
425 if (!imsm_mem)
426 return 0;
427
428 struct orom_entry *orom = add_orom(imsm_mem);
429
430 /* only PciDataStructure with revision 3 and above supports devices list. */
431 if (ptr->pciDataStructRev >= 3 && ptr->devListOffset) {
432 const __u16 *dev_list = (void *)ptr + ptr->devListOffset;
433 int i;
434
435 for (i = 0; dev_list[i] != 0; i++)
436 add_orom_device_id(orom, dev_list[i]);
437 } else {
438 add_orom_device_id(orom, __le16_to_cpu(ptr->deviceID));
439 }
440
441 return 0;
442 }
443
444 const struct imsm_orom *imsm_platform_test(struct sys_dev *hba)
445 {
446 struct imsm_orom orom = {
447 .signature = IMSM_OROM_SIGNATURE,
448 .rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 |
449 IMSM_OROM_RLC_RAID10 | IMSM_OROM_RLC_RAID5,
450 .sss = IMSM_OROM_SSS_4kB | IMSM_OROM_SSS_8kB |
451 IMSM_OROM_SSS_16kB | IMSM_OROM_SSS_32kB |
452 IMSM_OROM_SSS_64kB | IMSM_OROM_SSS_128kB |
453 IMSM_OROM_SSS_256kB | IMSM_OROM_SSS_512kB |
454 IMSM_OROM_SSS_1MB | IMSM_OROM_SSS_2MB,
455 .dpa = IMSM_OROM_DISKS_PER_ARRAY,
456 .tds = IMSM_OROM_TOTAL_DISKS,
457 .vpa = IMSM_OROM_VOLUMES_PER_ARRAY,
458 .vphba = IMSM_OROM_VOLUMES_PER_HBA
459 };
460 orom.attr = orom.rlc | IMSM_OROM_ATTR_ChecksumVerify;
461
462 if (check_env("IMSM_TEST_OROM_NORAID5")) {
463 orom.rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 |
464 IMSM_OROM_RLC_RAID10;
465 }
466 if (check_env("IMSM_TEST_AHCI_EFI_NORAID5") && (hba->type == SYS_DEV_SAS)) {
467 orom.rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 |
468 IMSM_OROM_RLC_RAID10;
469 }
470 if (check_env("IMSM_TEST_SCU_EFI_NORAID5") && (hba->type == SYS_DEV_SATA)) {
471 orom.rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 |
472 IMSM_OROM_RLC_RAID10;
473 }
474
475 struct orom_entry *ret = add_orom(&orom);
476
477 add_orom_device_id(ret, hba->dev_id);
478
479 return &ret->orom;
480 }
481
482 static const struct imsm_orom *find_imsm_hba_orom(struct sys_dev *hba)
483 {
484 unsigned long align;
485
486 if (check_env("IMSM_TEST_OROM"))
487 return imsm_platform_test(hba);
488
489 /* return empty OROM capabilities in EFI test mode */
490 if (check_env("IMSM_TEST_AHCI_EFI") || check_env("IMSM_TEST_SCU_EFI"))
491 return NULL;
492
493 find_intel_devices();
494
495 if (intel_devices == NULL)
496 return NULL;
497
498 /* scan option-rom memory looking for an imsm signature */
499 if (check_env("IMSM_SAFE_OROM_SCAN"))
500 align = 2048;
501 else
502 align = 512;
503 if (probe_roms_init(align) != 0)
504 return NULL;
505 probe_roms();
506 /* ignore return value - True is returned if both adapater roms are found */
507 scan_adapter_roms(scan);
508 probe_roms_exit();
509
510 return get_orom_by_device_id(hba->dev_id);
511 }
512
513 #define EFI_GUID(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \
514 ((struct efi_guid) \
515 {{ (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) & 0xff, \
516 (b) & 0xff, ((b) >> 8) & 0xff, \
517 (c) & 0xff, ((c) >> 8) & 0xff, \
518 (d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) }})
519
520 #define SYS_EFI_VAR_PATH "/sys/firmware/efi/vars"
521 #define SYS_EFIVARS_PATH "/sys/firmware/efi/efivars"
522 #define SCU_PROP "RstScuV"
523 #define AHCI_PROP "RstSataV"
524 #define AHCI_SSATA_PROP "RstsSatV"
525 #define AHCI_TSATA_PROP "RsttSatV"
526 #define VROC_VMD_PROP "RstUefiV"
527 #define RST_VMD_PROP "RstVmdV"
528
529 #define VENDOR_GUID \
530 EFI_GUID(0x193dfefa, 0xa445, 0x4302, 0x99, 0xd8, 0xef, 0x3a, 0xad, 0x1a, 0x04, 0xc6)
531
532 #define PCI_CLASS_RAID_CNTRL 0x010400
533
534 static int read_efi_var(void *buffer, ssize_t buf_size,
535 const char *variable_name, struct efi_guid guid)
536 {
537 char path[PATH_MAX];
538 char buf[GUID_STR_MAX];
539 int fd;
540 ssize_t n;
541
542 snprintf(path, PATH_MAX, "%s/%s-%s", SYS_EFIVARS_PATH, variable_name, guid_str(buf, guid));
543
544 fd = open(path, O_RDONLY);
545 if (fd < 0)
546 return 1;
547
548 /* read the variable attributes and ignore it */
549 n = read(fd, buf, sizeof(__u32));
550 if (n < 0) {
551 close(fd);
552 return 1;
553 }
554
555 /* read the variable data */
556 n = read(fd, buffer, buf_size);
557 close(fd);
558 if (n < buf_size)
559 return 1;
560
561 return 0;
562 }
563
564 static int read_efi_variable(void *buffer, ssize_t buf_size,
565 const char *variable_name, struct efi_guid guid)
566 {
567 char path[PATH_MAX];
568 char buf[GUID_STR_MAX];
569 int dfd;
570 ssize_t n, var_data_len;
571
572 /* Try to read the variable using the new efivarfs interface first.
573 * If that fails, fall back to the old sysfs-efivars interface. */
574 if (!read_efi_var(buffer, buf_size, variable_name, guid))
575 return 0;
576
577 snprintf(path, PATH_MAX, "%s/%s-%s/size", SYS_EFI_VAR_PATH, variable_name, guid_str(buf, guid));
578
579 dprintf("EFI VAR: path=%s\n", path);
580 /* get size of variable data */
581 dfd = open(path, O_RDONLY);
582 if (dfd < 0)
583 return 1;
584
585 n = read(dfd, &buf, sizeof(buf));
586 close(dfd);
587 if (n < 0)
588 return 1;
589 buf[n] = '\0';
590
591 errno = 0;
592 var_data_len = strtoul(buf, NULL, 16);
593 if ((errno == ERANGE && (var_data_len == LONG_MAX)) ||
594 (errno != 0 && var_data_len == 0))
595 return 1;
596
597 /* get data */
598 snprintf(path, PATH_MAX, "%s/%s-%s/data", SYS_EFI_VAR_PATH, variable_name, guid_str(buf, guid));
599
600 dprintf("EFI VAR: path=%s\n", path);
601 dfd = open(path, O_RDONLY);
602 if (dfd < 0)
603 return 1;
604
605 n = read(dfd, buffer, buf_size);
606 close(dfd);
607 if (n != var_data_len || n < buf_size) {
608 return 1;
609 }
610
611 return 0;
612 }
613
614 const struct imsm_orom *find_imsm_efi(struct sys_dev *hba)
615 {
616 struct imsm_orom orom;
617 struct orom_entry *ret;
618 static const char * const sata_efivars[] = {AHCI_PROP, AHCI_SSATA_PROP,
619 AHCI_TSATA_PROP};
620 static const char * const vmd_efivars[] = {VROC_VMD_PROP, RST_VMD_PROP};
621 unsigned long i;
622
623 if (check_env("IMSM_TEST_AHCI_EFI") || check_env("IMSM_TEST_SCU_EFI"))
624 return imsm_platform_test(hba);
625
626 /* OROM test is set, return that there is no EFI capabilities */
627 if (check_env("IMSM_TEST_OROM"))
628 return NULL;
629
630 switch (hba->type) {
631 case SYS_DEV_SAS:
632 if (!read_efi_variable(&orom, sizeof(orom), SCU_PROP,
633 VENDOR_GUID))
634 break;
635
636 return NULL;
637 case SYS_DEV_SATA:
638 if (hba->class != PCI_CLASS_RAID_CNTRL)
639 return NULL;
640
641 for (i = 0; i < ARRAY_SIZE(sata_efivars); i++) {
642 if (!read_efi_variable(&orom, sizeof(orom),
643 sata_efivars[i], VENDOR_GUID))
644 break;
645
646 }
647 if (i == ARRAY_SIZE(sata_efivars))
648 return NULL;
649
650 break;
651 case SYS_DEV_VMD:
652 case SYS_DEV_SATA_VMD:
653 for (i = 0; i < ARRAY_SIZE(vmd_efivars); i++) {
654 if (!read_efi_variable(&orom, sizeof(orom),
655 vmd_efivars[i], VENDOR_GUID))
656 break;
657 }
658
659 if (i == ARRAY_SIZE(vmd_efivars))
660 return NULL;
661
662 break;
663 default:
664 return NULL;
665 }
666
667 ret = add_orom(&orom);
668 add_orom_device_id(ret, hba->dev_id);
669 ret->type = hba->type;
670
671 return &ret->orom;
672 }
673
674 const struct imsm_orom *find_imsm_nvme(struct sys_dev *hba)
675 {
676 static struct orom_entry *nvme_orom;
677
678 if (hba->type != SYS_DEV_NVME)
679 return NULL;
680
681 if (!nvme_orom) {
682 struct imsm_orom nvme_orom_compat = {
683 .signature = IMSM_NVME_OROM_COMPAT_SIGNATURE,
684 .rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 |
685 IMSM_OROM_RLC_RAID10 | IMSM_OROM_RLC_RAID5,
686 .sss = IMSM_OROM_SSS_4kB | IMSM_OROM_SSS_8kB |
687 IMSM_OROM_SSS_16kB | IMSM_OROM_SSS_32kB |
688 IMSM_OROM_SSS_64kB | IMSM_OROM_SSS_128kB,
689 .dpa = IMSM_OROM_DISKS_PER_ARRAY_NVME,
690 .tds = IMSM_OROM_TOTAL_DISKS_NVME,
691 .vpa = IMSM_OROM_VOLUMES_PER_ARRAY,
692 .vphba = IMSM_OROM_TOTAL_DISKS_NVME / 2 * IMSM_OROM_VOLUMES_PER_ARRAY,
693 .attr = IMSM_OROM_ATTR_2TB | IMSM_OROM_ATTR_2TB_DISK,
694 .driver_features = IMSM_OROM_CAPABILITIES_EnterpriseSystem
695 };
696 nvme_orom = add_orom(&nvme_orom_compat);
697 }
698 add_orom_device_id(nvme_orom, hba->dev_id);
699 nvme_orom->type = SYS_DEV_NVME;
700 return &nvme_orom->orom;
701 }
702
703 const struct imsm_orom *find_imsm_capability(struct sys_dev *hba)
704 {
705 const struct imsm_orom *cap = get_orom_by_device_id(hba->dev_id);
706
707 if (cap)
708 return cap;
709
710 if (hba->type == SYS_DEV_NVME)
711 return find_imsm_nvme(hba);
712 if ((cap = find_imsm_efi(hba)) != NULL)
713 return cap;
714 if ((cap = find_imsm_hba_orom(hba)) != NULL)
715 return cap;
716
717 return NULL;
718 }
719
720 /* Check whether the nvme device is represented by nvme subsytem,
721 * if yes virtual path should be changed to hardware device path,
722 * to allow IMSM capabilities detection.
723 * Returns:
724 * hardware path to device - if the device is represented via
725 * nvme virtual subsytem
726 * NULL - if the device is not represented via nvme virtual subsytem
727 */
728 char *get_nvme_multipath_dev_hw_path(const char *dev_path)
729 {
730 DIR *dir;
731 struct dirent *ent;
732 char *rp = NULL;
733
734 if (strncmp(dev_path, NVME_SUBSYS_PATH, strlen(NVME_SUBSYS_PATH)) != 0)
735 return NULL;
736
737 dir = opendir(dev_path);
738 if (!dir)
739 return NULL;
740
741 for (ent = readdir(dir); ent; ent = readdir(dir)) {
742 char buf[strlen(dev_path) + strlen(ent->d_name) + 1];
743
744 /* Check if dir is a controller, ignore namespaces*/
745 if (!(strncmp(ent->d_name, "nvme", 4) == 0) ||
746 (strrchr(ent->d_name, 'n') != &ent->d_name[0]))
747 continue;
748
749 sprintf(buf, "%s/%s", dev_path, ent->d_name);
750 rp = realpath(buf, NULL);
751 break;
752 }
753
754 closedir(dir);
755 return rp;
756 }
757
758 /* Description: Return part or whole realpath for the dev
759 * Parameters:
760 * dev - the device to be quered
761 * dev_level - level of "/device" entries. It allows to caller to access
762 * virtual or physical devices which are on "path" to quered
763 * one.
764 * buf - optional, must be PATH_MAX size. If set, then will be used.
765 */
766 char *devt_to_devpath(dev_t dev, int dev_level, char *buf)
767 {
768 char device[PATH_MAX];
769 char *hw_path;
770 int i;
771 unsigned long device_free_len = sizeof(device) - 1;
772 char dev_str[] = "/device";
773 unsigned long dev_str_len = strlen(dev_str);
774
775 snprintf(device, sizeof(device), "/sys/dev/block/%d:%d", major(dev),
776 minor(dev));
777
778 /* If caller wants block device, return path to it even if it is exposed
779 * via virtual layer.
780 */
781 if (dev_level == 0)
782 return realpath(device, buf);
783
784 device_free_len -= strlen(device);
785 for (i = 0; i < dev_level; i++) {
786 if (device_free_len < dev_str_len)
787 return NULL;
788
789 strncat(device, dev_str, device_free_len);
790
791 /* Resolve nvme-subsystem abstraction if needed
792 */
793 device_free_len -= dev_str_len;
794 if (i == 0) {
795 char rp[PATH_MAX];
796
797 if (!realpath(device, rp))
798 return NULL;
799 hw_path = get_nvme_multipath_dev_hw_path(rp);
800 if (hw_path) {
801 strcpy(device, hw_path);
802 device_free_len = sizeof(device) -
803 strlen(device) - 1;
804 free(hw_path);
805 }
806 }
807 }
808
809 return realpath(device, buf);
810 }
811
812 char *diskfd_to_devpath(int fd, int dev_level, char *buf)
813 {
814 /* return the device path for a disk, return NULL on error or fd
815 * refers to a partition
816 */
817 struct stat st;
818
819 if (fstat(fd, &st) != 0)
820 return NULL;
821 if (!S_ISBLK(st.st_mode))
822 return NULL;
823
824 return devt_to_devpath(st.st_rdev, dev_level, buf);
825 }
826
827 int path_attached_to_hba(const char *disk_path, const char *hba_path)
828 {
829 int rc;
830
831 if (check_env("IMSM_TEST_AHCI_DEV") ||
832 check_env("IMSM_TEST_SCU_DEV")) {
833 return 1;
834 }
835
836 if (!disk_path || !hba_path)
837 return 0;
838 dprintf("hba: %s - disk: %s\n", hba_path, disk_path);
839 if (strncmp(disk_path, hba_path, strlen(hba_path)) == 0)
840 rc = 1;
841 else
842 rc = 0;
843
844 return rc;
845 }
846
847 int devt_attached_to_hba(dev_t dev, const char *hba_path)
848 {
849 char *disk_path = devt_to_devpath(dev, 1, NULL);
850 int rc = path_attached_to_hba(disk_path, hba_path);
851
852 if (disk_path)
853 free(disk_path);
854
855 return rc;
856 }
857
858 int disk_attached_to_hba(int fd, const char *hba_path)
859 {
860 char *disk_path = diskfd_to_devpath(fd, 1, NULL);
861 int rc = path_attached_to_hba(disk_path, hba_path);
862
863 if (disk_path)
864 free(disk_path);
865
866 return rc;
867 }
868
869 char *vmd_domain_to_controller(struct sys_dev *hba, char *buf)
870 {
871 struct dirent *ent;
872 DIR *dir;
873 char path[PATH_MAX];
874
875 if (!hba)
876 return NULL;
877
878 if (hba->type != SYS_DEV_VMD)
879 return NULL;
880
881 dir = opendir("/sys/bus/pci/drivers/vmd");
882 if (!dir)
883 return NULL;
884
885 for (ent = readdir(dir); ent; ent = readdir(dir)) {
886 sprintf(path, "/sys/bus/pci/drivers/vmd/%s/domain/device",
887 ent->d_name);
888
889 if (!realpath(path, buf))
890 continue;
891
892 if (strncmp(buf, hba->path, strlen(buf)) == 0) {
893 sprintf(path, "/sys/bus/pci/drivers/vmd/%s", ent->d_name);
894 closedir(dir);
895 return realpath(path, buf);
896 }
897 }
898
899 closedir(dir);
900 return NULL;
901 }
902
903 /* Scan over all controller's namespaces and compare nsid value to verify if
904 * current one is supported. The routine doesn't check IMSM capabilities for
905 * namespace. Only one nvme namespace is supported by IMSM.
906 * Paramteres:
907 * fd - open descriptor to the nvme namespace
908 * verbose - error logging level
909 * Returns:
910 * 1 - if namespace is supported
911 * 0 - otherwise
912 */
913 int imsm_is_nvme_namespace_supported(int fd, int verbose)
914 {
915 DIR *dir = NULL;
916 struct dirent *ent;
917 char cntrl_path[PATH_MAX];
918 char ns_path[PATH_MAX];
919 unsigned long long lowest_nsid = ULLONG_MAX;
920 unsigned long long this_nsid;
921 int rv = 0;
922
923
924 if (!diskfd_to_devpath(fd, 1, cntrl_path) ||
925 !diskfd_to_devpath(fd, 0, ns_path)) {
926 if (verbose)
927 pr_err("Cannot get device paths\n");
928 goto abort;
929 }
930
931
932 if (devpath_to_ll(ns_path, "nsid", &this_nsid)) {
933 if (verbose)
934 pr_err("Cannot read nsid value for %s",
935 basename(ns_path));
936 goto abort;
937 }
938
939 dir = opendir(cntrl_path);
940 if (!dir)
941 goto abort;
942
943 /* The lowest nvme namespace is supported */
944 for (ent = readdir(dir); ent; ent = readdir(dir)) {
945 unsigned long long curr_nsid;
946 char curr_ns_path[PATH_MAX + 256];
947
948 if (!strstr(ent->d_name, "nvme"))
949 continue;
950
951 snprintf(curr_ns_path, sizeof(curr_ns_path), "%s/%s",
952 cntrl_path, ent->d_name);
953
954 if (devpath_to_ll(curr_ns_path, "nsid", &curr_nsid))
955 goto abort;
956
957 if (lowest_nsid > curr_nsid)
958 lowest_nsid = curr_nsid;
959 }
960
961 if (this_nsid == lowest_nsid)
962 rv = 1;
963 else if (verbose)
964 pr_err("IMSM is supported on the lowest NVMe namespace\n");
965
966 abort:
967 if (dir)
968 closedir(dir);
969
970 return rv;
971 }
972
973 /* Verify if multipath is supported by NVMe controller
974 * Returns:
975 * 0 - not supported
976 * 1 - supported
977 */
978 int is_multipath_nvme(int disk_fd)
979 {
980 char ns_path[PATH_MAX];
981
982 if (!diskfd_to_devpath(disk_fd, 0, ns_path))
983 return 0;
984
985 if (strncmp(ns_path, NVME_SUBSYS_PATH, strlen(NVME_SUBSYS_PATH)) == 0)
986 return 1;
987
988 return 0;
989 }