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