]> git.ipfire.org Git - thirdparty/mdadm.git/blob - platform-intel.c
Fix unsafe string functions
[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 GUID_STR_MAX 37 /* according to GUID format:
514 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" */
515
516 #define EFI_GUID(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \
517 ((struct efi_guid) \
518 {{ (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) & 0xff, \
519 (b) & 0xff, ((b) >> 8) & 0xff, \
520 (c) & 0xff, ((c) >> 8) & 0xff, \
521 (d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) }})
522
523 #define SYS_EFI_VAR_PATH "/sys/firmware/efi/vars"
524 #define SYS_EFIVARS_PATH "/sys/firmware/efi/efivars"
525 #define SCU_PROP "RstScuV"
526 #define AHCI_PROP "RstSataV"
527 #define AHCI_SSATA_PROP "RstsSatV"
528 #define AHCI_TSATA_PROP "RsttSatV"
529 #define VROC_VMD_PROP "RstUefiV"
530 #define RST_VMD_PROP "RstVmdV"
531
532 #define VENDOR_GUID \
533 EFI_GUID(0x193dfefa, 0xa445, 0x4302, 0x99, 0xd8, 0xef, 0x3a, 0xad, 0x1a, 0x04, 0xc6)
534
535 #define PCI_CLASS_RAID_CNTRL 0x010400
536
537 static int read_efi_var(void *buffer, ssize_t buf_size,
538 const char *variable_name, struct efi_guid guid)
539 {
540 char path[PATH_MAX];
541 char buf[GUID_STR_MAX];
542 int fd;
543 ssize_t n;
544
545 snprintf(path, PATH_MAX, "%s/%s-%s", SYS_EFIVARS_PATH, variable_name, guid_str(buf, guid));
546
547 fd = open(path, O_RDONLY);
548 if (fd < 0)
549 return 1;
550
551 /* read the variable attributes and ignore it */
552 n = read(fd, buf, sizeof(__u32));
553 if (n < 0) {
554 close(fd);
555 return 1;
556 }
557
558 /* read the variable data */
559 n = read(fd, buffer, buf_size);
560 close(fd);
561 if (n < buf_size)
562 return 1;
563
564 return 0;
565 }
566
567 static int read_efi_variable(void *buffer, ssize_t buf_size,
568 const char *variable_name, struct efi_guid guid)
569 {
570 char path[PATH_MAX];
571 char buf[GUID_STR_MAX];
572 int dfd;
573 ssize_t n, var_data_len;
574
575 /* Try to read the variable using the new efivarfs interface first.
576 * If that fails, fall back to the old sysfs-efivars interface. */
577 if (!read_efi_var(buffer, buf_size, variable_name, guid))
578 return 0;
579
580 snprintf(path, PATH_MAX, "%s/%s-%s/size", SYS_EFI_VAR_PATH, variable_name, guid_str(buf, guid));
581
582 dprintf("EFI VAR: path=%s\n", path);
583 /* get size of variable data */
584 dfd = open(path, O_RDONLY);
585 if (dfd < 0)
586 return 1;
587
588 n = read(dfd, &buf, sizeof(buf));
589 close(dfd);
590 if (n < 0)
591 return 1;
592 buf[n] = '\0';
593
594 errno = 0;
595 var_data_len = strtoul(buf, NULL, 16);
596 if ((errno == ERANGE && (var_data_len == LONG_MAX)) ||
597 (errno != 0 && var_data_len == 0))
598 return 1;
599
600 /* get data */
601 snprintf(path, PATH_MAX, "%s/%s-%s/data", SYS_EFI_VAR_PATH, variable_name, guid_str(buf, guid));
602
603 dprintf("EFI VAR: path=%s\n", path);
604 dfd = open(path, O_RDONLY);
605 if (dfd < 0)
606 return 1;
607
608 n = read(dfd, buffer, buf_size);
609 close(dfd);
610 if (n != var_data_len || n < buf_size) {
611 return 1;
612 }
613
614 return 0;
615 }
616
617 const struct imsm_orom *find_imsm_efi(struct sys_dev *hba)
618 {
619 struct imsm_orom orom;
620 struct orom_entry *ret;
621 static const char * const sata_efivars[] = {AHCI_PROP, AHCI_SSATA_PROP,
622 AHCI_TSATA_PROP};
623 static const char * const vmd_efivars[] = {VROC_VMD_PROP, RST_VMD_PROP};
624 unsigned long i;
625
626 if (check_env("IMSM_TEST_AHCI_EFI") || check_env("IMSM_TEST_SCU_EFI"))
627 return imsm_platform_test(hba);
628
629 /* OROM test is set, return that there is no EFI capabilities */
630 if (check_env("IMSM_TEST_OROM"))
631 return NULL;
632
633 switch (hba->type) {
634 case SYS_DEV_SAS:
635 if (!read_efi_variable(&orom, sizeof(orom), SCU_PROP,
636 VENDOR_GUID))
637 break;
638
639 return NULL;
640 case SYS_DEV_SATA:
641 if (hba->class != PCI_CLASS_RAID_CNTRL)
642 return NULL;
643
644 for (i = 0; i < ARRAY_SIZE(sata_efivars); i++) {
645 if (!read_efi_variable(&orom, sizeof(orom),
646 sata_efivars[i], VENDOR_GUID))
647 break;
648
649 }
650 if (i == ARRAY_SIZE(sata_efivars))
651 return NULL;
652
653 break;
654 case SYS_DEV_VMD:
655 case SYS_DEV_SATA_VMD:
656 for (i = 0; i < ARRAY_SIZE(vmd_efivars); i++) {
657 if (!read_efi_variable(&orom, sizeof(orom),
658 vmd_efivars[i], VENDOR_GUID))
659 break;
660 }
661
662 if (i == ARRAY_SIZE(vmd_efivars))
663 return NULL;
664
665 break;
666 default:
667 return NULL;
668 }
669
670 ret = add_orom(&orom);
671 add_orom_device_id(ret, hba->dev_id);
672 ret->type = hba->type;
673
674 return &ret->orom;
675 }
676
677 const struct imsm_orom *find_imsm_nvme(struct sys_dev *hba)
678 {
679 static struct orom_entry *nvme_orom;
680
681 if (hba->type != SYS_DEV_NVME)
682 return NULL;
683
684 if (!nvme_orom) {
685 struct imsm_orom nvme_orom_compat = {
686 .signature = IMSM_NVME_OROM_COMPAT_SIGNATURE,
687 .rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 |
688 IMSM_OROM_RLC_RAID10 | IMSM_OROM_RLC_RAID5,
689 .sss = IMSM_OROM_SSS_4kB | IMSM_OROM_SSS_8kB |
690 IMSM_OROM_SSS_16kB | IMSM_OROM_SSS_32kB |
691 IMSM_OROM_SSS_64kB | IMSM_OROM_SSS_128kB,
692 .dpa = IMSM_OROM_DISKS_PER_ARRAY_NVME,
693 .tds = IMSM_OROM_TOTAL_DISKS_NVME,
694 .vpa = IMSM_OROM_VOLUMES_PER_ARRAY,
695 .vphba = IMSM_OROM_TOTAL_DISKS_NVME / 2 * IMSM_OROM_VOLUMES_PER_ARRAY,
696 .attr = IMSM_OROM_ATTR_2TB | IMSM_OROM_ATTR_2TB_DISK,
697 .driver_features = IMSM_OROM_CAPABILITIES_EnterpriseSystem
698 };
699 nvme_orom = add_orom(&nvme_orom_compat);
700 }
701 add_orom_device_id(nvme_orom, hba->dev_id);
702 nvme_orom->type = SYS_DEV_NVME;
703 return &nvme_orom->orom;
704 }
705
706 const struct imsm_orom *find_imsm_capability(struct sys_dev *hba)
707 {
708 const struct imsm_orom *cap = get_orom_by_device_id(hba->dev_id);
709
710 if (cap)
711 return cap;
712
713 if (hba->type == SYS_DEV_NVME)
714 return find_imsm_nvme(hba);
715 if ((cap = find_imsm_efi(hba)) != NULL)
716 return cap;
717 if ((cap = find_imsm_hba_orom(hba)) != NULL)
718 return cap;
719
720 return NULL;
721 }
722
723 /* Check whether the nvme device is represented by nvme subsytem,
724 * if yes virtual path should be changed to hardware device path,
725 * to allow IMSM capabilities detection.
726 * Returns:
727 * hardware path to device - if the device is represented via
728 * nvme virtual subsytem
729 * NULL - if the device is not represented via nvme virtual subsytem
730 */
731 char *get_nvme_multipath_dev_hw_path(const char *dev_path)
732 {
733 DIR *dir;
734 struct dirent *ent;
735 char *rp = NULL;
736
737 if (strncmp(dev_path, NVME_SUBSYS_PATH, strlen(NVME_SUBSYS_PATH)) != 0)
738 return NULL;
739
740 dir = opendir(dev_path);
741 if (!dir)
742 return NULL;
743
744 for (ent = readdir(dir); ent; ent = readdir(dir)) {
745 char buf[strlen(dev_path) + strlen(ent->d_name) + 1];
746
747 /* Check if dir is a controller, ignore namespaces*/
748 if (!(strncmp(ent->d_name, "nvme", 4) == 0) ||
749 (strrchr(ent->d_name, 'n') != &ent->d_name[0]))
750 continue;
751
752 sprintf(buf, "%s/%s", dev_path, ent->d_name);
753 rp = realpath(buf, NULL);
754 break;
755 }
756
757 closedir(dir);
758 return rp;
759 }
760
761 /* Description: Return part or whole realpath for the dev
762 * Parameters:
763 * dev - the device to be quered
764 * dev_level - level of "/device" entries. It allows to caller to access
765 * virtual or physical devices which are on "path" to quered
766 * one.
767 * buf - optional, must be PATH_MAX size. If set, then will be used.
768 */
769 char *devt_to_devpath(dev_t dev, int dev_level, char *buf)
770 {
771 char device[PATH_MAX];
772 char *hw_path;
773 int i;
774 unsigned long device_free_len = sizeof(device) - 1;
775 char dev_str[] = "/device";
776 unsigned long dev_str_len = strlen(dev_str);
777
778 snprintf(device, sizeof(device), "/sys/dev/block/%d:%d", major(dev),
779 minor(dev));
780
781 /* If caller wants block device, return path to it even if it is exposed
782 * via virtual layer.
783 */
784 if (dev_level == 0)
785 return realpath(device, buf);
786
787 device_free_len -= strlen(device);
788 for (i = 0; i < dev_level; i++) {
789 if (device_free_len < dev_str_len)
790 return NULL;
791
792 strncat(device, dev_str, device_free_len);
793
794 /* Resolve nvme-subsystem abstraction if needed
795 */
796 device_free_len -= dev_str_len;
797 if (i == 0) {
798 char rp[PATH_MAX];
799
800 if (!realpath(device, rp))
801 return NULL;
802 hw_path = get_nvme_multipath_dev_hw_path(rp);
803 if (hw_path) {
804 strcpy(device, hw_path);
805 device_free_len = sizeof(device) -
806 strlen(device) - 1;
807 free(hw_path);
808 }
809 }
810 }
811
812 return realpath(device, buf);
813 }
814
815 char *diskfd_to_devpath(int fd, int dev_level, char *buf)
816 {
817 /* return the device path for a disk, return NULL on error or fd
818 * refers to a partition
819 */
820 struct stat st;
821
822 if (fstat(fd, &st) != 0)
823 return NULL;
824 if (!S_ISBLK(st.st_mode))
825 return NULL;
826
827 return devt_to_devpath(st.st_rdev, dev_level, buf);
828 }
829
830 int path_attached_to_hba(const char *disk_path, const char *hba_path)
831 {
832 int rc;
833
834 if (check_env("IMSM_TEST_AHCI_DEV") ||
835 check_env("IMSM_TEST_SCU_DEV")) {
836 return 1;
837 }
838
839 if (!disk_path || !hba_path)
840 return 0;
841 dprintf("hba: %s - disk: %s\n", hba_path, disk_path);
842 if (strncmp(disk_path, hba_path, strlen(hba_path)) == 0)
843 rc = 1;
844 else
845 rc = 0;
846
847 return rc;
848 }
849
850 int devt_attached_to_hba(dev_t dev, const char *hba_path)
851 {
852 char *disk_path = devt_to_devpath(dev, 1, NULL);
853 int rc = path_attached_to_hba(disk_path, hba_path);
854
855 if (disk_path)
856 free(disk_path);
857
858 return rc;
859 }
860
861 int disk_attached_to_hba(int fd, const char *hba_path)
862 {
863 char *disk_path = diskfd_to_devpath(fd, 1, NULL);
864 int rc = path_attached_to_hba(disk_path, hba_path);
865
866 if (disk_path)
867 free(disk_path);
868
869 return rc;
870 }
871
872 char *vmd_domain_to_controller(struct sys_dev *hba, char *buf)
873 {
874 struct dirent *ent;
875 DIR *dir;
876 char path[PATH_MAX];
877
878 if (!hba)
879 return NULL;
880
881 if (hba->type != SYS_DEV_VMD)
882 return NULL;
883
884 dir = opendir("/sys/bus/pci/drivers/vmd");
885 if (!dir)
886 return NULL;
887
888 for (ent = readdir(dir); ent; ent = readdir(dir)) {
889 sprintf(path, "/sys/bus/pci/drivers/vmd/%s/domain/device",
890 ent->d_name);
891
892 if (!realpath(path, buf))
893 continue;
894
895 if (strncmp(buf, hba->path, strlen(buf)) == 0) {
896 sprintf(path, "/sys/bus/pci/drivers/vmd/%s", ent->d_name);
897 closedir(dir);
898 return realpath(path, buf);
899 }
900 }
901
902 closedir(dir);
903 return NULL;
904 }
905
906 /* Scan over all controller's namespaces and compare nsid value to verify if
907 * current one is supported. The routine doesn't check IMSM capabilities for
908 * namespace. Only one nvme namespace is supported by IMSM.
909 * Paramteres:
910 * fd - open descriptor to the nvme namespace
911 * verbose - error logging level
912 * Returns:
913 * 1 - if namespace is supported
914 * 0 - otherwise
915 */
916 int imsm_is_nvme_namespace_supported(int fd, int verbose)
917 {
918 DIR *dir = NULL;
919 struct dirent *ent;
920 char cntrl_path[PATH_MAX];
921 char ns_path[PATH_MAX];
922 unsigned long long lowest_nsid = ULLONG_MAX;
923 unsigned long long this_nsid;
924 int rv = 0;
925
926
927 if (!diskfd_to_devpath(fd, 1, cntrl_path) ||
928 !diskfd_to_devpath(fd, 0, ns_path)) {
929 if (verbose)
930 pr_err("Cannot get device paths\n");
931 goto abort;
932 }
933
934
935 if (devpath_to_ll(ns_path, "nsid", &this_nsid)) {
936 if (verbose)
937 pr_err("Cannot read nsid value for %s",
938 basename(ns_path));
939 goto abort;
940 }
941
942 dir = opendir(cntrl_path);
943 if (!dir)
944 goto abort;
945
946 /* The lowest nvme namespace is supported */
947 for (ent = readdir(dir); ent; ent = readdir(dir)) {
948 unsigned long long curr_nsid;
949 char curr_ns_path[PATH_MAX + 256];
950
951 if (!strstr(ent->d_name, "nvme"))
952 continue;
953
954 snprintf(curr_ns_path, sizeof(curr_ns_path), "%s/%s",
955 cntrl_path, ent->d_name);
956
957 if (devpath_to_ll(curr_ns_path, "nsid", &curr_nsid))
958 goto abort;
959
960 if (lowest_nsid > curr_nsid)
961 lowest_nsid = curr_nsid;
962 }
963
964 if (this_nsid == lowest_nsid)
965 rv = 1;
966 else if (verbose)
967 pr_err("IMSM is supported on the lowest NVMe namespace\n");
968
969 abort:
970 if (dir)
971 closedir(dir);
972
973 return rv;
974 }
975
976 /* Verify if multipath is supported by NVMe controller
977 * Returns:
978 * 0 - not supported
979 * 1 - supported
980 */
981 int is_multipath_nvme(int disk_fd)
982 {
983 char ns_path[PATH_MAX];
984
985 if (!diskfd_to_devpath(disk_fd, 0, ns_path))
986 return 0;
987
988 if (strncmp(ns_path, NVME_SUBSYS_PATH, strlen(NVME_SUBSYS_PATH)) == 0)
989 return 1;
990
991 return 0;
992 }