2 * Intel(R) Matrix Storage Manager hardware and firmware support routines
4 * Copyright (C) 2008 Intel Corporation
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.
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
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.
20 #include "platform-intel.h"
21 #include "probe_roms.h"
29 #include <sys/types.h>
33 #define NVME_SUBSYS_PATH "/sys/devices/virtual/nvme-subsystem/"
35 static int devpath_to_ll(const char *dev_path
, const char *entry
,
36 unsigned long long *val
);
38 static void free_sys_dev(struct sys_dev
**list
)
41 struct sys_dev
*next
= (*list
)->next
;
50 struct sys_dev
*find_driver_devices(const char *bus
, const char *driver
)
52 /* search sysfs for devices driven by 'driver' */
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;
65 if (strcmp(driver
, "isci") == 0)
67 else if (strcmp(driver
, "ahci") == 0) {
68 vmd
= find_driver_devices("pci", "vmd");
70 } else if (strcmp(driver
, "nvme") == 0) {
71 /* if looking for nvme devs, first look for vmd */
72 vmd
= find_driver_devices("pci", "vmd");
74 } else if (strcmp(driver
, "vmd") == 0)
77 type
= SYS_DEV_UNKNOWN
;
79 sprintf(path
, "/sys/bus/%s/drivers/%s", bus
, driver
);
80 driver_dir
= opendir(path
);
86 for (de
= readdir(driver_dir
); de
; de
= readdir(driver_dir
)) {
90 /* is 'de' a device? check that the 'subsystem' link exists and
91 * that its target matches 'bus'
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
))
99 c
= strrchr(link
, '/');
102 if (strncmp(bus
, c
+1, strlen(bus
)) != 0)
105 sprintf(path
, "/sys/bus/%s/drivers/%s/%s",
106 bus
, driver
, de
->d_name
);
108 /* if searching for nvme - skip vmd connected one */
109 if (type
== SYS_DEV_NVME
) {
111 char *rp
= realpath(path
, NULL
);
112 for (dev
= vmd
; dev
; dev
= dev
->next
) {
113 if ((strncmp(dev
->path
, rp
, strlen(dev
->path
)) == 0))
119 /* change sata type if under a vmd controller */
120 if (type
== SYS_DEV_SATA
) {
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
;
130 /* if it's not Intel device or mark as VMD connected - skip it. */
131 if (devpath_to_vendor(path
) != 0x8086 || skip
== 1)
134 if (devpath_to_ll(path
, "device", &dev_id
) != 0)
137 if (devpath_to_ll(path
, "class", &class) != 0)
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
146 if (type
== SYS_DEV_VMD
) {
147 sprintf(path
, "/sys/bus/%s/drivers/%s/%s/domain/device",
148 bus
, driver
, de
->d_name
);
150 p
= realpath(path
, NULL
);
152 pr_err("Unable to get real path for '%s'\n", path
);
156 /* start / add list entry */
158 head
= xmalloc(sizeof(*head
));
161 list
->next
= xmalloc(sizeof(*head
));
170 list
->dev_id
= (__u16
) dev_id
;
171 list
->class = (__u32
) class;
176 if ((list
->pci_id
= strrchr(list
->path
, '/')) != NULL
)
179 closedir(driver_dir
);
181 /* nvme vmd needs a list separate from sata vmd */
182 if (vmd
&& type
== SYS_DEV_NVME
) {
192 static struct sys_dev
*intel_devices
=NULL
;
193 static time_t valid_time
= 0;
195 struct sys_dev
*device_by_id(__u16 device_id
)
197 struct sys_dev
*iter
;
199 for (iter
= intel_devices
; iter
!= NULL
; iter
= iter
->next
)
200 if (iter
->dev_id
== device_id
)
205 struct sys_dev
*device_by_id_and_path(__u16 device_id
, const char *path
)
207 struct sys_dev
*iter
;
209 for (iter
= intel_devices
; iter
!= NULL
; iter
= iter
->next
)
210 if ((iter
->dev_id
== device_id
) && strstr(iter
->path
, path
))
215 static int devpath_to_ll(const char *dev_path
, const char *entry
, unsigned long long *val
)
217 char path
[strnlen(dev_path
, PATH_MAX
) + strnlen(entry
, PATH_MAX
) + 2];
221 sprintf(path
, "%s/%s", dev_path
, entry
);
223 fd
= open(path
, O_RDONLY
);
226 n
= sysfs_fd_get_ll(fd
, val
);
231 __u16
devpath_to_vendor(const char *dev_path
)
233 char path
[strlen(dev_path
) + strlen("/vendor") + 1];
239 sprintf(path
, "%s/vendor", dev_path
);
241 fd
= open(path
, O_RDONLY
);
245 n
= read(fd
, vendor
, sizeof(vendor
));
246 if (n
== sizeof(vendor
)) {
247 vendor
[n
- 1] = '\0';
248 id
= strtoul(vendor
, NULL
, 16);
255 /* Description: Read text value of dev_path/entry field
257 * dev_path - sysfs path to the device
258 * entry - entry to be read
259 * buf - buffer for read value
261 * verbose - error logging level
263 int devpath_to_char(const char *dev_path
, const char *entry
, char *buf
, int len
,
268 snprintf(path
, sizeof(path
), "%s/%s", dev_path
, entry
);
269 if (load_sys(path
, buf
, len
)) {
271 pr_err("Cannot read %s, aborting\n", path
);
278 struct sys_dev
*find_intel_devices(void)
280 struct sys_dev
*ahci
, *isci
, *nvme
;
282 if (valid_time
> time(0) - 10)
283 return intel_devices
;
286 free_sys_dev(&intel_devices
);
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");
294 if (!isci
&& !ahci
) {
298 struct sys_dev
*elem
= ahci
;
303 struct sys_dev
*elem
= ahci
;
311 intel_devices
= ahci
;
312 valid_time
= time(0);
313 return intel_devices
;
317 * PCI Expansion ROM Data Structure Format */
318 struct pciExpDataStructFormat
{
323 __u16 pciDataStructLen
;
324 __u8 pciDataStructRev
;
325 } __attribute__ ((packed
));
327 struct orom_entry
*orom_entries
;
329 const struct orom_entry
*get_orom_entry_by_device_id(__u16 dev_id
)
331 struct orom_entry
*entry
;
332 struct devid_list
*devid
;
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
)
344 const struct imsm_orom
*get_orom_by_device_id(__u16 dev_id
)
346 const struct orom_entry
*entry
= get_orom_entry_by_device_id(dev_id
);
354 static struct orom_entry
*add_orom(const struct imsm_orom
*orom
)
356 struct orom_entry
*list
;
357 struct orom_entry
*prev
= NULL
;
359 for (list
= orom_entries
; list
; prev
= list
, list
= list
->next
)
362 list
= xmalloc(sizeof(struct orom_entry
));
364 list
->devid_list
= NULL
;
375 static void add_orom_device_id(struct orom_entry
*entry
, __u16 dev_id
)
377 struct devid_list
*list
;
378 struct devid_list
*prev
= NULL
;
380 for (list
= entry
->devid_list
; list
; prev
= list
, list
= list
->next
) {
381 if (list
->devid
== dev_id
)
384 list
= xmalloc(sizeof(struct devid_list
));
385 list
->devid
= dev_id
;
389 entry
->devid_list
= list
;
394 static int scan(const void *start
, const void *end
, const void *data
)
397 const struct imsm_orom
*imsm_mem
= NULL
;
398 int len
= (end
- start
);
399 struct pciExpDataStructFormat
*ptr
= (struct pciExpDataStructFormat
*)data
;
401 if (data
+ 0x18 > end
) {
402 dprintf("cannot find pciExpDataStruct \n");
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
));
410 if (__le16_to_cpu(ptr
->vendorID
) != 0x8086)
413 if (get_orom_by_device_id(ptr
->deviceID
))
416 for (offset
= 0; offset
< len
; offset
+= 4) {
417 const void *mem
= start
+ offset
;
419 if ((memcmp(mem
, IMSM_OROM_SIGNATURE
, 4) == 0)) {
428 struct orom_entry
*orom
= add_orom(imsm_mem
);
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
;
435 for (i
= 0; dev_list
[i
] != 0; i
++)
436 add_orom_device_id(orom
, dev_list
[i
]);
438 add_orom_device_id(orom
, __le16_to_cpu(ptr
->deviceID
));
444 const struct imsm_orom
*imsm_platform_test(struct sys_dev
*hba
)
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
460 orom
.attr
= orom
.rlc
| IMSM_OROM_ATTR_ChecksumVerify
;
462 if (check_env("IMSM_TEST_OROM_NORAID5")) {
463 orom
.rlc
= IMSM_OROM_RLC_RAID0
| IMSM_OROM_RLC_RAID1
|
464 IMSM_OROM_RLC_RAID10
;
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
;
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
;
475 struct orom_entry
*ret
= add_orom(&orom
);
477 add_orom_device_id(ret
, hba
->dev_id
);
482 static const struct imsm_orom
*find_imsm_hba_orom(struct sys_dev
*hba
)
486 if (check_env("IMSM_TEST_OROM"))
487 return imsm_platform_test(hba
);
489 /* return empty OROM capabilities in EFI test mode */
490 if (check_env("IMSM_TEST_AHCI_EFI") || check_env("IMSM_TEST_SCU_EFI"))
493 find_intel_devices();
495 if (intel_devices
== NULL
)
498 /* scan option-rom memory looking for an imsm signature */
499 if (check_env("IMSM_SAFE_OROM_SCAN"))
503 if (probe_roms_init(align
) != 0)
506 /* ignore return value - True is returned if both adapater roms are found */
507 scan_adapter_roms(scan
);
510 return get_orom_by_device_id(hba
->dev_id
);
513 #define EFI_GUID(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \
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) }})
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"
529 #define VENDOR_GUID \
530 EFI_GUID(0x193dfefa, 0xa445, 0x4302, 0x99, 0xd8, 0xef, 0x3a, 0xad, 0x1a, 0x04, 0xc6)
532 #define PCI_CLASS_RAID_CNTRL 0x010400
534 static int read_efi_var(void *buffer
, ssize_t buf_size
,
535 const char *variable_name
, struct efi_guid guid
)
538 char buf
[GUID_STR_MAX
];
542 snprintf(path
, PATH_MAX
, "%s/%s-%s", SYS_EFIVARS_PATH
, variable_name
, guid_str(buf
, guid
));
544 fd
= open(path
, O_RDONLY
);
548 /* read the variable attributes and ignore it */
549 n
= read(fd
, buf
, sizeof(__u32
));
555 /* read the variable data */
556 n
= read(fd
, buffer
, buf_size
);
564 static int read_efi_variable(void *buffer
, ssize_t buf_size
,
565 const char *variable_name
, struct efi_guid guid
)
568 char buf
[GUID_STR_MAX
];
570 ssize_t n
, var_data_len
;
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
))
577 snprintf(path
, PATH_MAX
, "%s/%s-%s/size", SYS_EFI_VAR_PATH
, variable_name
, guid_str(buf
, guid
));
579 dprintf("EFI VAR: path=%s\n", path
);
580 /* get size of variable data */
581 dfd
= open(path
, O_RDONLY
);
585 n
= read(dfd
, &buf
, sizeof(buf
));
592 var_data_len
= strtoul(buf
, NULL
, 16);
593 if ((errno
== ERANGE
&& (var_data_len
== LONG_MAX
)) ||
594 (errno
!= 0 && var_data_len
== 0))
598 snprintf(path
, PATH_MAX
, "%s/%s-%s/data", SYS_EFI_VAR_PATH
, variable_name
, guid_str(buf
, guid
));
600 dprintf("EFI VAR: path=%s\n", path
);
601 dfd
= open(path
, O_RDONLY
);
605 n
= read(dfd
, buffer
, buf_size
);
607 if (n
!= var_data_len
|| n
< buf_size
) {
614 const struct imsm_orom
*find_imsm_efi(struct sys_dev
*hba
)
616 struct imsm_orom orom
;
617 struct orom_entry
*ret
;
618 static const char * const sata_efivars
[] = {AHCI_PROP
, AHCI_SSATA_PROP
,
620 static const char * const vmd_efivars
[] = {VROC_VMD_PROP
, RST_VMD_PROP
};
623 if (check_env("IMSM_TEST_AHCI_EFI") || check_env("IMSM_TEST_SCU_EFI"))
624 return imsm_platform_test(hba
);
626 /* OROM test is set, return that there is no EFI capabilities */
627 if (check_env("IMSM_TEST_OROM"))
632 if (!read_efi_variable(&orom
, sizeof(orom
), SCU_PROP
,
638 if (hba
->class != PCI_CLASS_RAID_CNTRL
)
641 for (i
= 0; i
< ARRAY_SIZE(sata_efivars
); i
++) {
642 if (!read_efi_variable(&orom
, sizeof(orom
),
643 sata_efivars
[i
], VENDOR_GUID
))
647 if (i
== ARRAY_SIZE(sata_efivars
))
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
))
659 if (i
== ARRAY_SIZE(vmd_efivars
))
667 ret
= add_orom(&orom
);
668 add_orom_device_id(ret
, hba
->dev_id
);
669 ret
->type
= hba
->type
;
674 const struct imsm_orom
*find_imsm_nvme(struct sys_dev
*hba
)
676 static struct orom_entry
*nvme_orom
;
678 if (hba
->type
!= SYS_DEV_NVME
)
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
696 nvme_orom
= add_orom(&nvme_orom_compat
);
698 add_orom_device_id(nvme_orom
, hba
->dev_id
);
699 nvme_orom
->type
= SYS_DEV_NVME
;
700 return &nvme_orom
->orom
;
703 #define VMD_REGISTER_OFFSET 0x3FC
704 #define VMD_REGISTER_SKU_SHIFT 1
705 #define VMD_REGISTER_SKU_MASK (0x00000007)
706 #define VMD_REGISTER_SKU_PREMIUM 2
707 #define MD_REGISTER_VER_MAJOR_SHIFT 4
708 #define MD_REGISTER_VER_MAJOR_MASK (0x0000000F)
709 #define MD_REGISTER_VER_MINOR_SHIFT 8
710 #define MD_REGISTER_VER_MINOR_MASK (0x0000000F)
713 * read_vmd_register() - Reads VMD register and writes contents to buff ptr
714 * @buff: buffer for vmd register data, should be the size of uint32_t
716 * Return: 0 on success, 1 on error
718 int read_vmd_register(uint32_t *buff
, struct sys_dev
*hba
)
721 char vmd_pci_config_path
[PATH_MAX
];
723 if (!vmd_domain_to_controller(hba
, vmd_pci_config_path
))
726 strncat(vmd_pci_config_path
, "/config", PATH_MAX
- strnlen(vmd_pci_config_path
, PATH_MAX
));
728 fd
= open(vmd_pci_config_path
, O_RDONLY
);
732 if (pread(fd
, buff
, sizeof(uint32_t), VMD_REGISTER_OFFSET
) != sizeof(uint32_t)) {
741 * add_vmd_orom() - Adds VMD orom cap to orom list, writes orom_entry ptr into vmd_orom
742 * @vmd_orom: pointer to orom entry pointer
744 * Return: 0 on success, 1 on error
746 int add_vmd_orom(struct orom_entry
**vmd_orom
, struct sys_dev
*hba
)
749 uint32_t vmd_register_data
;
750 struct imsm_orom vmd_orom_cap
= {
751 .signature
= IMSM_VMD_OROM_COMPAT_SIGNATURE
,
752 .sss
= IMSM_OROM_SSS_4kB
| IMSM_OROM_SSS_8kB
|
753 IMSM_OROM_SSS_16kB
| IMSM_OROM_SSS_32kB
|
754 IMSM_OROM_SSS_64kB
| IMSM_OROM_SSS_128kB
,
755 .dpa
= IMSM_OROM_DISKS_PER_ARRAY_NVME
,
756 .tds
= IMSM_OROM_TOTAL_DISKS_VMD
,
757 .vpa
= IMSM_OROM_VOLUMES_PER_ARRAY
,
758 .vphba
= IMSM_OROM_VOLUMES_PER_HBA_VMD
,
759 .attr
= IMSM_OROM_ATTR_2TB
| IMSM_OROM_ATTR_2TB_DISK
,
760 .driver_features
= IMSM_OROM_CAPABILITIES_EnterpriseSystem
|
761 IMSM_OROM_CAPABILITIES_TPV
764 if (read_vmd_register(&vmd_register_data
, hba
) != 0)
767 sku
= (uint8_t)((vmd_register_data
>> VMD_REGISTER_SKU_SHIFT
) &
768 VMD_REGISTER_SKU_MASK
);
770 if (sku
== VMD_REGISTER_SKU_PREMIUM
)
771 vmd_orom_cap
.rlc
= IMSM_OROM_RLC_RAID0
| IMSM_OROM_RLC_RAID1
|
772 IMSM_OROM_RLC_RAID10
| IMSM_OROM_RLC_RAID5
;
774 vmd_orom_cap
.rlc
= IMSM_OROM_RLC_RAID_CNG
;
776 vmd_orom_cap
.major_ver
= (uint8_t)
777 ((vmd_register_data
>> MD_REGISTER_VER_MAJOR_SHIFT
) &
778 MD_REGISTER_VER_MAJOR_MASK
);
779 vmd_orom_cap
.minor_ver
= (uint8_t)
780 ((vmd_register_data
>> MD_REGISTER_VER_MINOR_SHIFT
) &
781 MD_REGISTER_VER_MINOR_MASK
);
783 *vmd_orom
= add_orom(&vmd_orom_cap
);
788 const struct imsm_orom
*find_imsm_vmd(struct sys_dev
*hba
)
790 static struct orom_entry
*vmd_orom
;
792 if (hba
->type
!= SYS_DEV_VMD
)
795 if (!vmd_orom
&& add_vmd_orom(&vmd_orom
, hba
) != 0)
798 add_orom_device_id(vmd_orom
, hba
->dev_id
);
799 vmd_orom
->type
= SYS_DEV_VMD
;
800 return &vmd_orom
->orom
;
803 const struct imsm_orom
*find_imsm_capability(struct sys_dev
*hba
)
805 const struct imsm_orom
*cap
= get_orom_by_device_id(hba
->dev_id
);
810 if (hba
->type
== SYS_DEV_NVME
)
811 return find_imsm_nvme(hba
);
813 cap
= find_imsm_efi(hba
);
817 if (hba
->type
== SYS_DEV_VMD
) {
818 cap
= find_imsm_vmd(hba
);
823 cap
= find_imsm_hba_orom(hba
);
830 /* Check whether the nvme device is represented by nvme subsytem,
831 * if yes virtual path should be changed to hardware device path,
832 * to allow IMSM capabilities detection.
834 * hardware path to device - if the device is represented via
835 * nvme virtual subsytem
836 * NULL - if the device is not represented via nvme virtual subsytem
838 char *get_nvme_multipath_dev_hw_path(const char *dev_path
)
844 if (strncmp(dev_path
, NVME_SUBSYS_PATH
, strlen(NVME_SUBSYS_PATH
)) != 0)
847 dir
= opendir(dev_path
);
851 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
852 char buf
[strlen(dev_path
) + strlen(ent
->d_name
) + 1];
854 /* Check if dir is a controller, ignore namespaces*/
855 if (!(strncmp(ent
->d_name
, "nvme", 4) == 0) ||
856 (strrchr(ent
->d_name
, 'n') != &ent
->d_name
[0]))
859 sprintf(buf
, "%s/%s", dev_path
, ent
->d_name
);
860 rp
= realpath(buf
, NULL
);
868 /* Description: Return part or whole realpath for the dev
870 * dev - the device to be quered
871 * dev_level - level of "/device" entries. It allows to caller to access
872 * virtual or physical devices which are on "path" to quered
874 * buf - optional, must be PATH_MAX size. If set, then will be used.
876 char *devt_to_devpath(dev_t dev
, int dev_level
, char *buf
)
878 char device
[PATH_MAX
];
881 unsigned long device_free_len
= sizeof(device
) - 1;
882 char dev_str
[] = "/device";
883 unsigned long dev_str_len
= strlen(dev_str
);
885 snprintf(device
, sizeof(device
), "/sys/dev/block/%d:%d", major(dev
),
888 /* If caller wants block device, return path to it even if it is exposed
892 return realpath(device
, buf
);
894 device_free_len
-= strlen(device
);
895 for (i
= 0; i
< dev_level
; i
++) {
896 if (device_free_len
< dev_str_len
)
899 strncat(device
, dev_str
, device_free_len
);
901 /* Resolve nvme-subsystem abstraction if needed
903 device_free_len
-= dev_str_len
;
907 if (!realpath(device
, rp
))
909 hw_path
= get_nvme_multipath_dev_hw_path(rp
);
911 strcpy(device
, hw_path
);
912 device_free_len
= sizeof(device
) -
919 return realpath(device
, buf
);
922 char *diskfd_to_devpath(int fd
, int dev_level
, char *buf
)
924 /* return the device path for a disk, return NULL on error or fd
925 * refers to a partition
929 if (fstat(fd
, &st
) != 0)
931 if (!S_ISBLK(st
.st_mode
))
934 return devt_to_devpath(st
.st_rdev
, dev_level
, buf
);
937 int path_attached_to_hba(const char *disk_path
, const char *hba_path
)
941 if (check_env("IMSM_TEST_AHCI_DEV") ||
942 check_env("IMSM_TEST_SCU_DEV")) {
946 if (!disk_path
|| !hba_path
)
948 dprintf("hba: %s - disk: %s\n", hba_path
, disk_path
);
949 if (strncmp(disk_path
, hba_path
, strlen(hba_path
)) == 0)
957 int devt_attached_to_hba(dev_t dev
, const char *hba_path
)
959 char *disk_path
= devt_to_devpath(dev
, 1, NULL
);
960 int rc
= path_attached_to_hba(disk_path
, hba_path
);
968 int disk_attached_to_hba(int fd
, const char *hba_path
)
970 char *disk_path
= diskfd_to_devpath(fd
, 1, NULL
);
971 int rc
= path_attached_to_hba(disk_path
, hba_path
);
979 char *vmd_domain_to_controller(struct sys_dev
*hba
, char *buf
)
988 if (hba
->type
!= SYS_DEV_VMD
)
991 dir
= opendir("/sys/bus/pci/drivers/vmd");
995 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
996 sprintf(path
, "/sys/bus/pci/drivers/vmd/%s/domain/device",
999 if (!realpath(path
, buf
))
1002 if (strncmp(buf
, hba
->path
, strlen(buf
)) == 0) {
1003 sprintf(path
, "/sys/bus/pci/drivers/vmd/%s", ent
->d_name
);
1005 return realpath(path
, buf
);
1013 /* Scan over all controller's namespaces and compare nsid value to verify if
1014 * current one is supported. The routine doesn't check IMSM capabilities for
1015 * namespace. Only one nvme namespace is supported by IMSM.
1017 * fd - open descriptor to the nvme namespace
1018 * verbose - error logging level
1020 * 1 - if namespace is supported
1023 int imsm_is_nvme_namespace_supported(int fd
, int verbose
)
1027 char cntrl_path
[PATH_MAX
];
1028 char ns_path
[PATH_MAX
];
1029 unsigned long long lowest_nsid
= ULLONG_MAX
;
1030 unsigned long long this_nsid
;
1034 if (!diskfd_to_devpath(fd
, 1, cntrl_path
) ||
1035 !diskfd_to_devpath(fd
, 0, ns_path
)) {
1037 pr_err("Cannot get device paths\n");
1042 if (devpath_to_ll(ns_path
, "nsid", &this_nsid
)) {
1044 pr_err("Cannot read nsid value for %s",
1049 dir
= opendir(cntrl_path
);
1053 /* The lowest nvme namespace is supported */
1054 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
1055 unsigned long long curr_nsid
;
1056 char curr_ns_path
[PATH_MAX
+ 256];
1058 if (!strstr(ent
->d_name
, "nvme"))
1061 snprintf(curr_ns_path
, sizeof(curr_ns_path
), "%s/%s",
1062 cntrl_path
, ent
->d_name
);
1064 if (devpath_to_ll(curr_ns_path
, "nsid", &curr_nsid
))
1067 if (lowest_nsid
> curr_nsid
)
1068 lowest_nsid
= curr_nsid
;
1071 if (this_nsid
== lowest_nsid
)
1074 pr_err("IMSM is supported on the lowest NVMe namespace\n");
1083 /* Verify if multipath is supported by NVMe controller
1088 int is_multipath_nvme(int disk_fd
)
1090 char ns_path
[PATH_MAX
];
1092 if (!diskfd_to_devpath(disk_fd
, 0, ns_path
))
1095 if (strncmp(ns_path
, NVME_SUBSYS_PATH
, strlen(NVME_SUBSYS_PATH
)) == 0)