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 static int devpath_to_ll(const char *dev_path
, const char *entry
,
34 unsigned long long *val
);
36 static void free_sys_dev(struct sys_dev
**list
)
39 struct sys_dev
*next
= (*list
)->next
;
48 struct sys_dev
*find_driver_devices(const char *bus
, const char *driver
)
50 /* search sysfs for devices driven by 'driver' */
56 struct sys_dev
*head
= NULL
;
57 struct sys_dev
*list
= NULL
;
58 struct sys_dev
*vmd
= NULL
;
59 enum sys_dev_type type
;
60 unsigned long long dev_id
;
61 unsigned long long class;
63 if (strcmp(driver
, "isci") == 0)
65 else if (strcmp(driver
, "ahci") == 0)
67 else if (strcmp(driver
, "nvme") == 0) {
68 /* if looking for nvme devs, first look for vmd */
69 vmd
= find_driver_devices("pci", "vmd");
71 } else if (strcmp(driver
, "vmd") == 0)
74 type
= SYS_DEV_UNKNOWN
;
76 sprintf(path
, "/sys/bus/%s/drivers/%s", bus
, driver
);
77 driver_dir
= opendir(path
);
83 for (de
= readdir(driver_dir
); de
; de
= readdir(driver_dir
)) {
87 /* is 'de' a device? check that the 'subsystem' link exists and
88 * that its target matches 'bus'
90 sprintf(path
, "/sys/bus/%s/drivers/%s/%s/subsystem",
91 bus
, driver
, de
->d_name
);
92 n
= readlink(path
, link
, sizeof(link
));
93 if (n
< 0 || n
>= (int)sizeof(link
))
96 c
= strrchr(link
, '/');
99 if (strncmp(bus
, c
+1, strlen(bus
)) != 0)
102 sprintf(path
, "/sys/bus/%s/drivers/%s/%s",
103 bus
, driver
, de
->d_name
);
105 /* if searching for nvme - skip vmd connected one */
106 if (type
== SYS_DEV_NVME
) {
108 char *rp
= realpath(path
, NULL
);
109 for (dev
= vmd
; dev
; dev
= dev
->next
) {
110 if ((strncmp(dev
->path
, rp
, strlen(dev
->path
)) == 0))
116 /* if it's not Intel device or mark as VMD connected - skip it. */
117 if (devpath_to_vendor(path
) != 0x8086 || skip
== 1)
120 if (devpath_to_ll(path
, "device", &dev_id
) != 0)
123 if (devpath_to_ll(path
, "class", &class) != 0)
126 /* start / add list entry */
128 head
= xmalloc(sizeof(*head
));
131 list
->next
= xmalloc(sizeof(*head
));
140 list
->dev_id
= (__u16
) dev_id
;
141 list
->class = (__u32
) class;
143 /* Each VMD device (domain) adds separate PCI bus, it is better to
144 * store path as a path to that bus (easier further determination which
145 * NVMe dev is connected to this particular VMD domain).
147 if (type
== SYS_DEV_VMD
) {
148 sprintf(path
, "/sys/bus/%s/drivers/%s/%s/domain/device",
149 bus
, driver
, de
->d_name
);
151 list
->path
= realpath(path
, NULL
);
153 if ((list
->pci_id
= strrchr(list
->path
, '/')) != NULL
)
156 closedir(driver_dir
);
168 static struct sys_dev
*intel_devices
=NULL
;
169 static time_t valid_time
= 0;
171 struct sys_dev
*device_by_id(__u16 device_id
)
173 struct sys_dev
*iter
;
175 for (iter
= intel_devices
; iter
!= NULL
; iter
= iter
->next
)
176 if (iter
->dev_id
== device_id
)
181 static int devpath_to_ll(const char *dev_path
, const char *entry
, unsigned long long *val
)
183 char path
[strlen(dev_path
) + strlen(entry
) + 2];
187 sprintf(path
, "%s/%s", dev_path
, entry
);
189 fd
= open(path
, O_RDONLY
);
192 n
= sysfs_fd_get_ll(fd
, val
);
197 __u16
devpath_to_vendor(const char *dev_path
)
199 char path
[strlen(dev_path
) + strlen("/vendor") + 1];
205 sprintf(path
, "%s/vendor", dev_path
);
207 fd
= open(path
, O_RDONLY
);
211 n
= read(fd
, vendor
, sizeof(vendor
));
212 if (n
== sizeof(vendor
)) {
213 vendor
[n
- 1] = '\0';
214 id
= strtoul(vendor
, NULL
, 16);
221 struct sys_dev
*find_intel_devices(void)
223 struct sys_dev
*ahci
, *isci
, *nvme
;
225 if (valid_time
> time(0) - 10)
226 return intel_devices
;
229 free_sys_dev(&intel_devices
);
231 isci
= find_driver_devices("pci", "isci");
232 ahci
= find_driver_devices("pci", "ahci");
233 /* Searching for NVMe will return list of NVMe and VMD controllers */
234 nvme
= find_driver_devices("pci", "nvme");
236 if (!isci
&& !ahci
) {
240 struct sys_dev
*elem
= ahci
;
245 struct sys_dev
*elem
= ahci
;
253 intel_devices
= ahci
;
254 valid_time
= time(0);
255 return intel_devices
;
259 * PCI Expansion ROM Data Structure Format */
260 struct pciExpDataStructFormat
{
265 __u16 pciDataStructLen
;
266 __u8 pciDataStructRev
;
267 } __attribute__ ((packed
));
269 struct orom_entry
*orom_entries
;
271 const struct orom_entry
*get_orom_entry_by_device_id(__u16 dev_id
)
273 struct orom_entry
*entry
;
274 struct devid_list
*devid
;
276 for (entry
= orom_entries
; entry
; entry
= entry
->next
) {
277 for (devid
= entry
->devid_list
; devid
; devid
= devid
->next
) {
278 if (devid
->devid
== dev_id
)
286 const struct imsm_orom
*get_orom_by_device_id(__u16 dev_id
)
288 const struct orom_entry
*entry
= get_orom_entry_by_device_id(dev_id
);
296 static struct orom_entry
*add_orom(const struct imsm_orom
*orom
)
298 struct orom_entry
*list
;
299 struct orom_entry
*prev
= NULL
;
301 for (list
= orom_entries
; list
; prev
= list
, list
= list
->next
)
304 list
= xmalloc(sizeof(struct orom_entry
));
306 list
->devid_list
= NULL
;
317 static void add_orom_device_id(struct orom_entry
*entry
, __u16 dev_id
)
319 struct devid_list
*list
;
320 struct devid_list
*prev
= NULL
;
322 for (list
= entry
->devid_list
; list
; prev
= list
, list
= list
->next
) {
323 if (list
->devid
== dev_id
)
326 list
= xmalloc(sizeof(struct devid_list
));
327 list
->devid
= dev_id
;
331 entry
->devid_list
= list
;
336 static int scan(const void *start
, const void *end
, const void *data
)
339 const struct imsm_orom
*imsm_mem
= NULL
;
340 int len
= (end
- start
);
341 struct pciExpDataStructFormat
*ptr
= (struct pciExpDataStructFormat
*)data
;
343 if (data
+ 0x18 > end
) {
344 dprintf("cannot find pciExpDataStruct \n");
348 dprintf("ptr->vendorID: %lx __le16_to_cpu(ptr->deviceID): %lx \n",
349 (ulong
) __le16_to_cpu(ptr
->vendorID
),
350 (ulong
) __le16_to_cpu(ptr
->deviceID
));
352 if (__le16_to_cpu(ptr
->vendorID
) != 0x8086)
355 for (offset
= 0; offset
< len
; offset
+= 4) {
356 const void *mem
= start
+ offset
;
358 if ((memcmp(mem
, IMSM_OROM_SIGNATURE
, 4) == 0)) {
367 struct orom_entry
*orom
= add_orom(imsm_mem
);
369 /* only PciDataStructure with revision 3 and above supports devices list. */
370 if (ptr
->pciDataStructRev
>= 3 && ptr
->devListOffset
) {
371 const __u16
*dev_list
= (void *)ptr
+ ptr
->devListOffset
;
374 for (i
= 0; dev_list
[i
] != 0; i
++)
375 add_orom_device_id(orom
, dev_list
[i
]);
377 add_orom_device_id(orom
, __le16_to_cpu(ptr
->deviceID
));
383 const struct imsm_orom
*imsm_platform_test(struct sys_dev
*hba
)
385 struct imsm_orom orom
= {
386 .signature
= IMSM_OROM_SIGNATURE
,
387 .rlc
= IMSM_OROM_RLC_RAID0
| IMSM_OROM_RLC_RAID1
|
388 IMSM_OROM_RLC_RAID10
| IMSM_OROM_RLC_RAID5
,
389 .sss
= IMSM_OROM_SSS_4kB
| IMSM_OROM_SSS_8kB
|
390 IMSM_OROM_SSS_16kB
| IMSM_OROM_SSS_32kB
|
391 IMSM_OROM_SSS_64kB
| IMSM_OROM_SSS_128kB
|
392 IMSM_OROM_SSS_256kB
| IMSM_OROM_SSS_512kB
|
393 IMSM_OROM_SSS_1MB
| IMSM_OROM_SSS_2MB
,
394 .dpa
= IMSM_OROM_DISKS_PER_ARRAY
,
395 .tds
= IMSM_OROM_TOTAL_DISKS
,
396 .vpa
= IMSM_OROM_VOLUMES_PER_ARRAY
,
397 .vphba
= IMSM_OROM_VOLUMES_PER_HBA
399 orom
.attr
= orom
.rlc
| IMSM_OROM_ATTR_ChecksumVerify
;
401 if (check_env("IMSM_TEST_OROM_NORAID5")) {
402 orom
.rlc
= IMSM_OROM_RLC_RAID0
| IMSM_OROM_RLC_RAID1
|
403 IMSM_OROM_RLC_RAID10
;
405 if (check_env("IMSM_TEST_AHCI_EFI_NORAID5") && (hba
->type
== SYS_DEV_SAS
)) {
406 orom
.rlc
= IMSM_OROM_RLC_RAID0
| IMSM_OROM_RLC_RAID1
|
407 IMSM_OROM_RLC_RAID10
;
409 if (check_env("IMSM_TEST_SCU_EFI_NORAID5") && (hba
->type
== SYS_DEV_SATA
)) {
410 orom
.rlc
= IMSM_OROM_RLC_RAID0
| IMSM_OROM_RLC_RAID1
|
411 IMSM_OROM_RLC_RAID10
;
414 struct orom_entry
*ret
= add_orom(&orom
);
416 add_orom_device_id(ret
, hba
->dev_id
);
421 static const struct imsm_orom
*find_imsm_hba_orom(struct sys_dev
*hba
)
425 if (check_env("IMSM_TEST_OROM"))
426 return imsm_platform_test(hba
);
428 /* return empty OROM capabilities in EFI test mode */
429 if (check_env("IMSM_TEST_AHCI_EFI") || check_env("IMSM_TEST_SCU_EFI"))
432 find_intel_devices();
434 if (intel_devices
== NULL
)
437 /* scan option-rom memory looking for an imsm signature */
438 if (check_env("IMSM_SAFE_OROM_SCAN"))
442 if (probe_roms_init(align
) != 0)
445 /* ignore return value - True is returned if both adapater roms are found */
446 scan_adapter_roms(scan
);
449 return get_orom_by_device_id(hba
->dev_id
);
452 #define GUID_STR_MAX 37 /* according to GUID format:
453 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" */
455 #define EFI_GUID(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \
457 {{ (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) & 0xff, \
458 (b) & 0xff, ((b) >> 8) & 0xff, \
459 (c) & 0xff, ((c) >> 8) & 0xff, \
460 (d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) }})
462 #define SYS_EFI_VAR_PATH "/sys/firmware/efi/vars"
463 #define SYS_EFIVARS_PATH "/sys/firmware/efi/efivars"
464 #define SCU_PROP "RstScuV"
465 #define AHCI_PROP "RstSataV"
466 #define AHCI_SSATA_PROP "RstsSatV"
467 #define AHCI_CSATA_PROP "RstCSatV"
468 #define VMD_PROP "RstUefiV"
470 #define VENDOR_GUID \
471 EFI_GUID(0x193dfefa, 0xa445, 0x4302, 0x99, 0xd8, 0xef, 0x3a, 0xad, 0x1a, 0x04, 0xc6)
473 #define PCI_CLASS_RAID_CNTRL 0x010400
475 static int read_efi_var(void *buffer
, ssize_t buf_size
, char *variable_name
, struct efi_guid guid
)
478 char buf
[GUID_STR_MAX
];
482 snprintf(path
, PATH_MAX
, "%s/%s-%s", SYS_EFIVARS_PATH
, variable_name
, guid_str(buf
, guid
));
484 fd
= open(path
, O_RDONLY
);
488 /* read the variable attributes and ignore it */
489 n
= read(fd
, buf
, sizeof(__u32
));
495 /* read the variable data */
496 n
= read(fd
, buffer
, buf_size
);
504 static int read_efi_variable(void *buffer
, ssize_t buf_size
, char *variable_name
, struct efi_guid guid
)
507 char buf
[GUID_STR_MAX
];
509 ssize_t n
, var_data_len
;
511 /* Try to read the variable using the new efivarfs interface first.
512 * If that fails, fall back to the old sysfs-efivars interface. */
513 if (!read_efi_var(buffer
, buf_size
, variable_name
, guid
))
516 snprintf(path
, PATH_MAX
, "%s/%s-%s/size", SYS_EFI_VAR_PATH
, variable_name
, guid_str(buf
, guid
));
518 dprintf("EFI VAR: path=%s\n", path
);
519 /* get size of variable data */
520 dfd
= open(path
, O_RDONLY
);
524 n
= read(dfd
, &buf
, sizeof(buf
));
531 var_data_len
= strtoul(buf
, NULL
, 16);
532 if ((errno
== ERANGE
&& (var_data_len
== LONG_MAX
))
533 || (errno
!= 0 && var_data_len
== 0))
537 snprintf(path
, PATH_MAX
, "%s/%s-%s/data", SYS_EFI_VAR_PATH
, variable_name
, guid_str(buf
, guid
));
539 dprintf("EFI VAR: path=%s\n", path
);
540 dfd
= open(path
, O_RDONLY
);
544 n
= read(dfd
, buffer
, buf_size
);
546 if (n
!= var_data_len
|| n
< buf_size
) {
553 const struct imsm_orom
*find_imsm_efi(struct sys_dev
*hba
)
555 struct imsm_orom orom
;
556 struct orom_entry
*ret
;
559 if (check_env("IMSM_TEST_AHCI_EFI") || check_env("IMSM_TEST_SCU_EFI"))
560 return imsm_platform_test(hba
);
562 /* OROM test is set, return that there is no EFI capabilities */
563 if (check_env("IMSM_TEST_OROM"))
566 if (hba
->type
== SYS_DEV_SATA
&& hba
->class != PCI_CLASS_RAID_CNTRL
)
569 err
= read_efi_variable(&orom
, sizeof(orom
), hba
->type
== SYS_DEV_SAS
? SCU_PROP
: AHCI_PROP
, VENDOR_GUID
);
571 /* try to read variable for second AHCI controller */
572 if (err
&& hba
->type
== SYS_DEV_SATA
)
573 err
= read_efi_variable(&orom
, sizeof(orom
), AHCI_SSATA_PROP
, VENDOR_GUID
);
575 /* try to read variable for combined AHCI controllers */
576 if (err
&& hba
->type
== SYS_DEV_SATA
) {
577 static struct orom_entry
*csata
;
579 err
= read_efi_variable(&orom
, sizeof(orom
), AHCI_CSATA_PROP
, VENDOR_GUID
);
582 csata
= add_orom(&orom
);
583 add_orom_device_id(csata
, hba
->dev_id
);
584 csata
->type
= hba
->type
;
589 if (hba
->type
== SYS_DEV_VMD
) {
590 err
= read_efi_variable(&orom
, sizeof(orom
), VMD_PROP
, VENDOR_GUID
);
596 ret
= add_orom(&orom
);
597 add_orom_device_id(ret
, hba
->dev_id
);
598 ret
->type
= hba
->type
;
603 const struct imsm_orom
*find_imsm_nvme(struct sys_dev
*hba
)
605 static struct orom_entry
*nvme_orom
;
607 if (hba
->type
!= SYS_DEV_NVME
)
611 struct imsm_orom nvme_orom_compat
= {
612 .signature
= IMSM_NVME_OROM_COMPAT_SIGNATURE
,
613 .rlc
= IMSM_OROM_RLC_RAID0
| IMSM_OROM_RLC_RAID1
|
614 IMSM_OROM_RLC_RAID10
| IMSM_OROM_RLC_RAID5
,
615 .sss
= IMSM_OROM_SSS_4kB
| IMSM_OROM_SSS_8kB
|
616 IMSM_OROM_SSS_16kB
| IMSM_OROM_SSS_32kB
|
617 IMSM_OROM_SSS_64kB
| IMSM_OROM_SSS_128kB
,
618 .dpa
= IMSM_OROM_DISKS_PER_ARRAY_NVME
,
619 .tds
= IMSM_OROM_TOTAL_DISKS_NVME
,
620 .vpa
= IMSM_OROM_VOLUMES_PER_ARRAY
,
621 .vphba
= IMSM_OROM_TOTAL_DISKS_NVME
/ 2 * IMSM_OROM_VOLUMES_PER_ARRAY
,
622 .attr
= IMSM_OROM_ATTR_2TB
| IMSM_OROM_ATTR_2TB_DISK
,
623 .driver_features
= IMSM_OROM_CAPABILITIES_EnterpriseSystem
625 nvme_orom
= add_orom(&nvme_orom_compat
);
627 add_orom_device_id(nvme_orom
, hba
->dev_id
);
628 nvme_orom
->type
= SYS_DEV_NVME
;
629 return &nvme_orom
->orom
;
632 const struct imsm_orom
*find_imsm_capability(struct sys_dev
*hba
)
634 const struct imsm_orom
*cap
= get_orom_by_device_id(hba
->dev_id
);
639 if (hba
->type
== SYS_DEV_NVME
)
640 return find_imsm_nvme(hba
);
641 if ((cap
= find_imsm_efi(hba
)) != NULL
)
643 if ((cap
= find_imsm_hba_orom(hba
)) != NULL
)
649 char *devt_to_devpath(dev_t dev
)
653 sprintf(device
, "/sys/dev/block/%d:%d/device", major(dev
), minor(dev
));
654 return realpath(device
, NULL
);
657 char *diskfd_to_devpath(int fd
)
659 /* return the device path for a disk, return NULL on error or fd
660 * refers to a partition
664 if (fstat(fd
, &st
) != 0)
666 if (!S_ISBLK(st
.st_mode
))
669 return devt_to_devpath(st
.st_rdev
);
672 int path_attached_to_hba(const char *disk_path
, const char *hba_path
)
676 if (check_env("IMSM_TEST_AHCI_DEV") ||
677 check_env("IMSM_TEST_SCU_DEV")) {
681 if (!disk_path
|| !hba_path
)
683 dprintf("hba: %s - disk: %s\n", hba_path
, disk_path
);
684 if (strncmp(disk_path
, hba_path
, strlen(hba_path
)) == 0)
692 int devt_attached_to_hba(dev_t dev
, const char *hba_path
)
694 char *disk_path
= devt_to_devpath(dev
);
695 int rc
= path_attached_to_hba(disk_path
, hba_path
);
703 int disk_attached_to_hba(int fd
, const char *hba_path
)
705 char *disk_path
= diskfd_to_devpath(fd
);
706 int rc
= path_attached_to_hba(disk_path
, hba_path
);
714 char *vmd_domain_to_controller(struct sys_dev
*hba
, char *buf
)
723 if (hba
->type
!= SYS_DEV_VMD
)
726 dir
= opendir("/sys/bus/pci/drivers/vmd");
730 for (ent
= readdir(dir
); ent
; ent
= readdir(dir
)) {
731 sprintf(path
, "/sys/bus/pci/drivers/vmd/%s/domain/device",
734 if (!realpath(path
, buf
))
737 if (strncmp(buf
, hba
->path
, strlen(buf
)) == 0) {
738 sprintf(path
, "/sys/bus/pci/drivers/vmd/%s", ent
->d_name
);
740 return realpath(path
, buf
);