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);
-
static void free_sys_dev(struct sys_dev **list)
{
while (*list) {
struct sys_dev *find_driver_devices(const char *bus, const char *driver)
{
/* search sysfs for devices driven by 'driver' */
- char path[292];
- char link[256];
- char *c;
+ char path[PATH_MAX];
+ char link[PATH_MAX];
+ char *c, *p;
DIR *driver_dir;
struct dirent *de;
struct sys_dev *head = NULL;
struct sys_dev *list = NULL;
+ struct sys_dev *vmd = NULL;
enum sys_dev_type type;
unsigned long long dev_id;
unsigned long long class;
type = SYS_DEV_SAS;
else if (strcmp(driver, "ahci") == 0)
type = SYS_DEV_SATA;
- else if (strcmp(driver, "nvme") == 0)
+ else if (strcmp(driver, "nvme") == 0) {
+ /* if looking for nvme devs, first look for vmd */
+ vmd = find_driver_devices("pci", "vmd");
type = SYS_DEV_NVME;
+ } else if (strcmp(driver, "vmd") == 0)
+ type = SYS_DEV_VMD;
else
type = SYS_DEV_UNKNOWN;
sprintf(path, "/sys/bus/%s/drivers/%s", bus, driver);
driver_dir = opendir(path);
- if (!driver_dir)
+ if (!driver_dir) {
+ if (vmd)
+ free_sys_dev(&vmd);
return NULL;
+ }
for (de = readdir(driver_dir); de; de = readdir(driver_dir)) {
int n;
+ int skip = 0;
/* is 'de' a device? check that the 'subsystem' link exists and
* that its target matches 'bus'
sprintf(path, "/sys/bus/%s/drivers/%s/%s",
bus, driver, de->d_name);
- /* if it's not Intel device skip it. */
- if (devpath_to_vendor(path) != 0x8086)
+ /* if searching for nvme - skip vmd connected one */
+ if (type == SYS_DEV_NVME) {
+ struct sys_dev *dev;
+ char *rp = realpath(path, NULL);
+ for (dev = vmd; dev; dev = dev->next) {
+ if ((strncmp(dev->path, rp, strlen(dev->path)) == 0))
+ skip = 1;
+ }
+ free(rp);
+ }
+
+ /* if it's not Intel device or mark as VMD connected - skip it. */
+ if (devpath_to_vendor(path) != 0x8086 || skip == 1)
continue;
if (devpath_to_ll(path, "device", &dev_id) != 0)
if (devpath_to_ll(path, "class", &class) != 0)
continue;
+ /*
+ * Each VMD device (domain) adds separate PCI bus, it is better
+ * to store path as a path to that bus (easier further
+ * determination which NVMe dev is connected to this particular
+ * VMD domain).
+ */
+ if (type == SYS_DEV_VMD) {
+ sprintf(path, "/sys/bus/%s/drivers/%s/%s/domain/device",
+ bus, driver, de->d_name);
+ }
+ p = realpath(path, NULL);
+ if (p == NULL) {
+ pr_err("Unable to get real path for '%s'\n", path);
+ continue;
+ }
+
/* start / add list entry */
if (!head) {
head = xmalloc(sizeof(*head));
list->dev_id = (__u16) dev_id;
list->class = (__u32) class;
list->type = type;
- list->path = realpath(path, NULL);
list->next = NULL;
+ list->path = p;
+
if ((list->pci_id = strrchr(list->path, '/')) != NULL)
list->pci_id++;
}
closedir(driver_dir);
+
+ if (vmd) {
+ if (list)
+ list->next = vmd;
+ else
+ head = vmd;
+ }
+
return head;
}
static struct sys_dev *intel_devices=NULL;
static time_t valid_time = 0;
+struct sys_dev *device_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;
+ return NULL;
+}
+
+struct sys_dev *device_by_id_and_path(__u16 device_id, const char *path)
+{
+ struct sys_dev *iter;
+
+ for (iter = intel_devices; iter != NULL; iter = iter->next)
+ if ((iter->dev_id == device_id) && strstr(iter->path, path))
+ return iter;
+ return NULL;
+}
+
static int devpath_to_ll(const char *dev_path, const char *entry, unsigned long long *val)
{
char path[strlen(dev_path) + strlen(entry) + 2];
return n;
}
-static __u16 devpath_to_vendor(const char *dev_path)
+__u16 devpath_to_vendor(const char *dev_path)
{
char path[strlen(dev_path) + strlen("/vendor") + 1];
char vendor[7];
isci = find_driver_devices("pci", "isci");
ahci = find_driver_devices("pci", "ahci");
+ /* Searching for NVMe will return list of NVMe and VMD controllers */
nvme = find_driver_devices("pci", "nvme");
if (!isci && !ahci) {
__u16 vendorID;
__u16 deviceID;
__u16 devListOffset;
+ __u16 pciDataStructLen;
+ __u8 pciDataStructRev;
} __attribute__ ((packed));
-struct devid_list {
- __u16 devid;
- struct devid_list *next;
-};
-
-struct orom_entry {
- struct imsm_orom orom;
- struct devid_list *devid_list;
-};
-
-static struct orom_entry oroms[SYS_DEV_MAX];
+struct orom_entry *orom_entries;
-const struct imsm_orom *get_orom_by_device_id(__u16 dev_id)
+const struct orom_entry *get_orom_entry_by_device_id(__u16 dev_id)
{
- int i;
- struct devid_list *list;
+ struct orom_entry *entry;
+ struct devid_list *devid;
- for (i = 0; i < SYS_DEV_MAX; i++) {
- for (list = oroms[i].devid_list; list; list = list->next) {
- if (list->devid == dev_id)
- return &oroms[i].orom;
+ for (entry = orom_entries; entry; entry = entry->next) {
+ for (devid = entry->devid_list; devid; devid = devid->next) {
+ if (devid->devid == dev_id)
+ return entry;
}
}
+
return NULL;
}
-static const struct imsm_orom *add_orom(const struct imsm_orom *orom)
+const struct imsm_orom *get_orom_by_device_id(__u16 dev_id)
{
- int i;
-
- for (i = 0; i < SYS_DEV_MAX; i++) {
- if (&oroms[i].orom == orom)
- return orom;
- if (oroms[i].orom.signature[0] == 0) {
- oroms[i].orom = *orom;
- return &oroms[i].orom;
- }
- }
+ const struct orom_entry *entry = get_orom_entry_by_device_id(dev_id);
+
+ if (entry)
+ return &entry->orom;
+
return NULL;
}
-static void add_orom_device_id(const struct imsm_orom *orom, __u16 dev_id)
+static struct orom_entry *add_orom(const struct imsm_orom *orom)
+{
+ struct orom_entry *list;
+ struct orom_entry *prev = NULL;
+
+ for (list = orom_entries; list; prev = list, list = list->next)
+ ;
+
+ list = xmalloc(sizeof(struct orom_entry));
+ list->orom = *orom;
+ list->devid_list = NULL;
+ list->next = NULL;
+
+ if (prev == NULL)
+ orom_entries = list;
+ else
+ prev->next = list;
+
+ return list;
+}
+
+static void add_orom_device_id(struct orom_entry *entry, __u16 dev_id)
{
- int i;
struct devid_list *list;
struct devid_list *prev = NULL;
- for (i = 0; i < SYS_DEV_MAX; i++) {
- if (&oroms[i].orom == orom) {
- for (list = oroms[i].devid_list; list; prev = list, list = list->next) {
- if (list->devid == dev_id)
- return;
- }
- list = xmalloc(sizeof(struct devid_list));
- list->devid = dev_id;
- list->next = NULL;
-
- if (prev == NULL)
- oroms[i].devid_list = list;
- else
- prev->next = list;
+ for (list = entry->devid_list; list; prev = list, list = list->next) {
+ if (list->devid == dev_id)
return;
- }
}
+ list = xmalloc(sizeof(struct devid_list));
+ list->devid = dev_id;
+ list->next = NULL;
+
+ if (prev == NULL)
+ entry->devid_list = list;
+ else
+ prev->next = list;
}
static int scan(const void *start, const void *end, const void *data)
if (__le16_to_cpu(ptr->vendorID) != 0x8086)
return 0;
+ if (get_orom_by_device_id(ptr->deviceID))
+ return 0;
+
for (offset = 0; offset < len; offset += 4) {
const void *mem = start + offset;
if (!imsm_mem)
return 0;
- const struct imsm_orom *orom = add_orom(imsm_mem);
+ struct orom_entry *orom = add_orom(imsm_mem);
- if (ptr->devListOffset) {
+ /* only PciDataStructure with revision 3 and above supports devices list. */
+ if (ptr->pciDataStructRev >= 3 && ptr->devListOffset) {
const __u16 *dev_list = (void *)ptr + ptr->devListOffset;
int i;
IMSM_OROM_RLC_RAID10;
}
- const struct imsm_orom *ret = add_orom(&orom);
+ struct orom_entry *ret = add_orom(&orom);
add_orom_device_id(ret, hba->dev_id);
- return ret;
+ return &ret->orom;
}
static const struct imsm_orom *find_imsm_hba_orom(struct sys_dev *hba)
(d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) }})
#define SYS_EFI_VAR_PATH "/sys/firmware/efi/vars"
+#define SYS_EFIVARS_PATH "/sys/firmware/efi/efivars"
#define SCU_PROP "RstScuV"
#define AHCI_PROP "RstSataV"
#define AHCI_SSATA_PROP "RstsSatV"
#define AHCI_CSATA_PROP "RstCSatV"
+#define VMD_PROP "RstUefiV"
#define VENDOR_GUID \
EFI_GUID(0x193dfefa, 0xa445, 0x4302, 0x99, 0xd8, 0xef, 0x3a, 0xad, 0x1a, 0x04, 0xc6)
#define PCI_CLASS_RAID_CNTRL 0x010400
-int read_efi_variable(void *buffer, ssize_t buf_size, char *variable_name, struct efi_guid guid)
+static int read_efi_var(void *buffer, ssize_t buf_size, char *variable_name, struct efi_guid guid)
+{
+ char path[PATH_MAX];
+ char buf[GUID_STR_MAX];
+ int fd;
+ ssize_t n;
+
+ snprintf(path, PATH_MAX, "%s/%s-%s", SYS_EFIVARS_PATH, variable_name, guid_str(buf, guid));
+
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ return 1;
+
+ /* read the variable attributes and ignore it */
+ n = read(fd, buf, sizeof(__u32));
+ if (n < 0) {
+ close(fd);
+ return 1;
+ }
+
+ /* read the variable data */
+ n = read(fd, buffer, buf_size);
+ close(fd);
+ if (n < buf_size)
+ return 1;
+
+ return 0;
+}
+
+static 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;
+ /* Try to read the variable using the new efivarfs interface first.
+ * If that fails, fall back to the old sysfs-efivars interface. */
+ if (!read_efi_var(buffer, buf_size, variable_name, guid))
+ return 0;
+
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);
errno = 0;
var_data_len = strtoul(buf, NULL, 16);
- if ((errno == ERANGE && (var_data_len == LONG_MAX))
- || (errno != 0 && var_data_len == 0))
+ if ((errno == ERANGE && (var_data_len == LONG_MAX)) ||
+ (errno != 0 && var_data_len == 0))
return 1;
/* get data */
const struct imsm_orom *find_imsm_efi(struct sys_dev *hba)
{
struct imsm_orom orom;
- const struct imsm_orom *ret;
+ struct orom_entry *ret;
int err;
if (check_env("IMSM_TEST_AHCI_EFI") || check_env("IMSM_TEST_SCU_EFI"))
/* try to read variable for combined AHCI controllers */
if (err && hba->type == SYS_DEV_SATA) {
- static const struct imsm_orom *csata;
+ static struct orom_entry *csata;
err = read_efi_variable(&orom, sizeof(orom), AHCI_CSATA_PROP, VENDOR_GUID);
if (!err) {
if (!csata)
csata = add_orom(&orom);
add_orom_device_id(csata, hba->dev_id);
- return csata;
+ csata->type = hba->type;
+ return &csata->orom;
}
}
+ if (hba->type == SYS_DEV_VMD) {
+ err = read_efi_variable(&orom, sizeof(orom), VMD_PROP, VENDOR_GUID);
+ }
+
if (err)
return NULL;
ret = add_orom(&orom);
add_orom_device_id(ret, hba->dev_id);
+ ret->type = hba->type;
- return ret;
+ return &ret->orom;
}
const struct imsm_orom *find_imsm_nvme(struct sys_dev *hba)
{
- static const struct imsm_orom *nvme_orom;
+ static struct orom_entry *nvme_orom;
if (hba->type != SYS_DEV_NVME)
return NULL;
.vpa = IMSM_OROM_VOLUMES_PER_ARRAY,
.vphba = IMSM_OROM_TOTAL_DISKS_NVME / 2 * IMSM_OROM_VOLUMES_PER_ARRAY,
.attr = IMSM_OROM_ATTR_2TB | IMSM_OROM_ATTR_2TB_DISK,
+ .driver_features = IMSM_OROM_CAPABILITIES_EnterpriseSystem
};
nvme_orom = add_orom(&nvme_orom_compat);
}
add_orom_device_id(nvme_orom, hba->dev_id);
- return nvme_orom;
+ nvme_orom->type = SYS_DEV_NVME;
+ return &nvme_orom->orom;
}
const struct imsm_orom *find_imsm_capability(struct sys_dev *hba)
return rc;
}
+
+char *vmd_domain_to_controller(struct sys_dev *hba, char *buf)
+{
+ struct dirent *ent;
+ DIR *dir;
+ char path[PATH_MAX];
+
+ if (!hba)
+ return NULL;
+
+ if (hba->type != SYS_DEV_VMD)
+ return NULL;
+
+ dir = opendir("/sys/bus/pci/drivers/vmd");
+ if (!dir)
+ return NULL;
+
+ for (ent = readdir(dir); ent; ent = readdir(dir)) {
+ sprintf(path, "/sys/bus/pci/drivers/vmd/%s/domain/device",
+ ent->d_name);
+
+ if (!realpath(path, buf))
+ continue;
+
+ if (strncmp(buf, hba->path, strlen(buf)) == 0) {
+ sprintf(path, "/sys/bus/pci/drivers/vmd/%s", ent->d_name);
+ closedir(dir);
+ return realpath(path, buf);
+ }
+ }
+
+ closedir(dir);
+ return NULL;
+}