]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - platform-intel.c
tests: add test that DDF marks missing devices as failed on assembly.
[thirdparty/mdadm.git] / platform-intel.c
index 08cf4edd30b177fe65c5e6cd24603e7510aafaef..f347382ac3f446d978e795f95d4712a2fc0e0778 100644 (file)
 #include <sys/stat.h>
 #include <limits.h>
 
+static int devpath_to_ll(const char *dev_path, const char *entry,
+                        unsigned long long *val);
 
 static __u16 devpath_to_vendor(const char *dev_path);
 
-void free_sys_dev(struct sys_dev **list)
+static void free_sys_dev(struct sys_dev **list)
 {
        while (*list) {
                struct sys_dev *next = (*list)->next;
@@ -56,6 +58,7 @@ struct sys_dev *find_driver_devices(const char *bus, const char *driver)
        struct sys_dev *head = NULL;
        struct sys_dev *list = NULL;
        enum sys_dev_type type;
+       unsigned long long dev_id;
 
        if (strcmp(driver, "isci") == 0)
                type = SYS_DEV_SAS;
@@ -93,12 +96,15 @@ struct sys_dev *find_driver_devices(const char *bus, const char *driver)
                if (devpath_to_vendor(path) != 0x8086)
                        continue;
 
+               if (devpath_to_ll(path, "device", &dev_id) != 0)
+                       continue;
+
                /* start / add list entry */
                if (!head) {
-                       head = malloc(sizeof(*head));
+                       head = xmalloc(sizeof(*head));
                        list = head;
                } else {
-                       list->next = malloc(sizeof(*head));
+                       list->next = xmalloc(sizeof(*head));
                        list = list->next;
                }
 
@@ -107,8 +113,9 @@ struct sys_dev *find_driver_devices(const char *bus, const char *driver)
                        break;
                }
 
+               list->dev_id = (__u16) dev_id;
                list->type = type;
-               list->path = canonicalize_file_name(path);
+               list->path = realpath(path, NULL);
                list->next = NULL;
                if ((list->pci_id = strrchr(list->path, '/')) != NULL)
                        list->pci_id++;
@@ -117,6 +124,34 @@ struct sys_dev *find_driver_devices(const char *bus, const char *driver)
        return head;
 }
 
+static struct sys_dev *intel_devices=NULL;
+static time_t valid_time = 0;
+
+static enum sys_dev_type device_type_by_id(__u16 device_id)
+{
+       struct sys_dev *iter;
+
+       for(iter = intel_devices; iter != NULL; iter = iter->next)
+               if (iter->dev_id == device_id)
+                       return iter->type;
+       return SYS_DEV_UNKNOWN;
+}
+
+static int devpath_to_ll(const char *dev_path, const char *entry, unsigned long long *val)
+{
+       char path[strlen(dev_path) + strlen(entry) + 2];
+       int fd;
+       int n;
+
+       sprintf(path, "%s/%s", dev_path, entry);
+
+       fd = open(path, O_RDONLY);
+       if (fd < 0)
+               return -1;
+       n = sysfs_fd_get_ll(fd, val);
+       close(fd);
+       return n;
+}
 
 static __u16 devpath_to_vendor(const char *dev_path)
 {
@@ -146,6 +181,12 @@ struct sys_dev *find_intel_devices(void)
 {
        struct sys_dev *ahci, *isci;
 
+       if (valid_time > time(0) - 10)
+               return intel_devices;
+
+       if (intel_devices)
+               free_sys_dev(&intel_devices);
+
        isci = find_driver_devices("pci", "isci");
        ahci = find_driver_devices("pci", "ahci");
 
@@ -157,18 +198,9 @@ struct sys_dev *find_intel_devices(void)
                        elem = elem->next;
                elem->next = isci;
        }
-       return ahci;
-}
-
-static int platform_has_intel_devices(void)
-{
-       struct sys_dev *devices;
-       devices = find_intel_devices();
-       if (devices) {
-               free_sys_dev(&devices);
-               return 1;
-       }
-       return 0;
+       intel_devices = ahci;
+       valid_time = time(0);
+       return intel_devices;
 }
 
 /*
@@ -190,22 +222,27 @@ static int scan(const void *start, const void *end, const void *data)
        int len = (end - start);
        struct pciExpDataStructFormat *ptr= (struct pciExpDataStructFormat *)data;
 
+       if (data + 0x18 > end) {
+               dprintf("cannot find pciExpDataStruct \n");
+               return 0;
+       }
+
        dprintf("ptr->vendorID: %lx __le16_to_cpu(ptr->deviceID): %lx \n",
                (ulong) __le16_to_cpu(ptr->vendorID),
                (ulong) __le16_to_cpu(ptr->deviceID));
 
-       if ((__le16_to_cpu(ptr->vendorID) == 0x8086) &&
-           (__le16_to_cpu(ptr->deviceID) == 0x2822))
-               dev = SYS_DEV_SATA;
-       else if ((__le16_to_cpu(ptr->vendorID) == 0x8086) &&
-                (__le16_to_cpu(ptr->deviceID) == 0x1D60))
-               dev = SYS_DEV_SAS;
+       if (__le16_to_cpu(ptr->vendorID) == 0x8086) {
+               /* serach  attached intel devices by device id from OROM */
+               dev = device_type_by_id(__le16_to_cpu(ptr->deviceID));
+               if (dev == SYS_DEV_UNKNOWN)
+                       return 0;
+       }
        else
                return 0;
 
        for (offset = 0; offset < len; offset += 4) {
                imsm_mem = start + offset;
-               if (memcmp(imsm_mem->signature, "$VER", 4) == 0) {
+               if ((memcmp(imsm_mem->signature, "$VER", 4) == 0)) {
                        imsm_orom[dev] = *imsm_mem;
                        populated_orom[dev] = 1;
                        return populated_orom[SYS_DEV_SATA] && populated_orom[SYS_DEV_SAS];
@@ -214,7 +251,6 @@ static int scan(const void *start, const void *end, const void *data)
        return 0;
 }
 
-
 const struct imsm_orom *imsm_platform_test(enum sys_dev_type hba_id, int *populated,
                                           struct imsm_orom *imsm_orom)
 {
@@ -249,8 +285,6 @@ const struct imsm_orom *imsm_platform_test(enum sys_dev_type hba_id, int *popula
        return imsm_orom;
 }
 
-
-
 static const struct imsm_orom *find_imsm_hba_orom(enum sys_dev_type hba_id)
 {
        unsigned long align;
@@ -266,7 +300,7 @@ static const struct imsm_orom *find_imsm_hba_orom(enum sys_dev_type hba_id)
        }
        if (check_env("IMSM_TEST_OROM")) {
                dprintf("OROM CAP: %p,  pid: %d pop: %d\n",
-                     &imsm_orom[hba_id], (int) getpid(), populated_orom[hba_id]);
+                       &imsm_orom[hba_id], (int) getpid(), populated_orom[hba_id]);
                return imsm_platform_test(hba_id, &populated_orom[hba_id], &imsm_orom[hba_id]);
        }
        /* return empty OROM capabilities in EFI test mode */
@@ -274,7 +308,9 @@ static const struct imsm_orom *find_imsm_hba_orom(enum sys_dev_type hba_id)
            check_env("IMSM_TEST_SCU_EFI"))
                return NULL;
 
-       if (!platform_has_intel_devices())
+       find_intel_devices();
+
+       if (intel_devices == NULL)
                return NULL;
 
        /* scan option-rom memory looking for an imsm signature */
@@ -285,7 +321,7 @@ static const struct imsm_orom *find_imsm_hba_orom(enum sys_dev_type hba_id)
        if (probe_roms_init(align) != 0)
                return NULL;
        probe_roms();
-       /* ignore result - True is returned if both are found */
+       /* ignore return value - True is returned if both adapater roms are found */
        scan_adapter_roms(scan);
        probe_roms_exit();
 
@@ -294,6 +330,103 @@ static const struct imsm_orom *find_imsm_hba_orom(enum sys_dev_type hba_id)
        return NULL;
 }
 
+#define GUID_STR_MAX   37  /* according to GUID format:
+                            * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" */
+
+#define EFI_GUID(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \
+((struct efi_guid) \
+{{ (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) & 0xff, \
+  (b) & 0xff, ((b) >> 8) & 0xff, \
+  (c) & 0xff, ((c) >> 8) & 0xff, \
+  (d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) }})
+
+#define SYS_EFI_VAR_PATH "/sys/firmware/efi/vars"
+#define SCU_PROP "RstScuV"
+#define AHCI_PROP "RstSataV"
+
+#define VENDOR_GUID \
+       EFI_GUID(0x193dfefa, 0xa445, 0x4302, 0x99, 0xd8, 0xef, 0x3a, 0xad, 0x1a, 0x04, 0xc6)
+
+int populated_efi[SYS_DEV_MAX] = { 0, 0 };
+
+static struct imsm_orom imsm_efi[SYS_DEV_MAX];
+
+int read_efi_variable(void *buffer, ssize_t buf_size, char *variable_name, struct efi_guid guid)
+{
+       char path[PATH_MAX];
+       char buf[GUID_STR_MAX];
+       int dfd;
+       ssize_t n, var_data_len;
+
+       snprintf(path, PATH_MAX, "%s/%s-%s/size", SYS_EFI_VAR_PATH, variable_name, guid_str(buf, guid));
+
+       dprintf("EFI VAR: path=%s\n", path);
+       /* get size of variable data */
+       dfd = open(path, O_RDONLY);
+       if (dfd < 0)
+               return 1;
+
+       n = read(dfd, &buf, sizeof(buf));
+       close(dfd);
+       if (n < 0)
+               return 1;
+       buf[n] = '\0';
+
+       errno = 0;
+       var_data_len = strtoul(buf, NULL, 16);
+       if ((errno == ERANGE && (var_data_len == LONG_MAX))
+           || (errno != 0 && var_data_len == 0))
+               return 1;
+
+       /* get data */
+       snprintf(path, PATH_MAX, "%s/%s-%s/data", SYS_EFI_VAR_PATH, variable_name, guid_str(buf, guid));
+
+       dprintf("EFI VAR: path=%s\n", path);
+       dfd = open(path, O_RDONLY);
+       if (dfd < 0)
+               return 1;
+
+       n = read(dfd, buffer, buf_size);
+       close(dfd);
+       if (n != var_data_len || n < buf_size) {
+               return 1;
+       }
+
+       return 0;
+}
+
+const struct imsm_orom *find_imsm_efi(enum sys_dev_type hba_id)
+{
+       if (hba_id >= SYS_DEV_MAX)
+               return NULL;
+
+       dprintf("EFI CAP: %p,  pid: %d pop: %d\n",
+               &imsm_efi[hba_id], (int) getpid(), populated_efi[hba_id]);
+
+       /* it's static data so we only need to read it once */
+       if (populated_efi[hba_id]) {
+               dprintf("EFI CAP: %p, pid: %d pop: %d\n",
+                       &imsm_efi[hba_id], (int) getpid(), populated_efi[hba_id]);
+               return &imsm_efi[hba_id];
+       }
+       if (check_env("IMSM_TEST_AHCI_EFI") ||
+           check_env("IMSM_TEST_SCU_EFI")) {
+               dprintf("OROM CAP: %p,  pid: %d pop: %d\n",
+                       &imsm_efi[hba_id], (int) getpid(), populated_efi[hba_id]);
+               return imsm_platform_test(hba_id, &populated_efi[hba_id], &imsm_efi[hba_id]);
+       }
+       /* OROM test is set, return that there is no EFI capabilities */
+       if (check_env("IMSM_TEST_OROM"))
+               return NULL;
+
+       if (read_efi_variable(&imsm_efi[hba_id], sizeof(imsm_efi[0]), hba_id == SYS_DEV_SAS ? SCU_PROP : AHCI_PROP, VENDOR_GUID)) {
+               populated_efi[hba_id] = 0;
+               return NULL;
+       }
+
+       populated_efi[hba_id] = 1;
+       return &imsm_efi[hba_id];
+}
 
 /*
  * backward interface compatibility
@@ -307,6 +440,8 @@ const struct imsm_orom *find_imsm_capability(enum sys_dev_type hba_id)
 {
        const struct imsm_orom *cap=NULL;
 
+       if ((cap = find_imsm_efi(hba_id)) != NULL)
+               return cap;
        if ((cap = find_imsm_hba_orom(hba_id)) != NULL)
                return cap;
        return NULL;
@@ -317,7 +452,7 @@ char *devt_to_devpath(dev_t dev)
        char device[46];
 
        sprintf(device, "/sys/dev/block/%d:%d/device", major(dev), minor(dev));
-       return canonicalize_file_name(device);
+       return realpath(device, NULL);
 }
 
 char *diskfd_to_devpath(int fd)