]>
Commit | Line | Data |
---|---|---|
b390f610 DW |
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> | |
fc13853f LM |
31 | #include <limits.h> |
32 | ||
d835518b BK |
33 | #define NVME_SUBSYS_PATH "/sys/devices/virtual/nvme-subsystem/" |
34 | ||
9c747fa0 LM |
35 | static int devpath_to_ll(const char *dev_path, const char *entry, |
36 | unsigned long long *val); | |
37 | ||
9bc4ae77 | 38 | static void free_sys_dev(struct sys_dev **list) |
b390f610 DW |
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' */ | |
30730cb8 AO |
53 | char path[PATH_MAX]; |
54 | char link[PATH_MAX]; | |
55 | char *c, *p; | |
b390f610 DW |
56 | DIR *driver_dir; |
57 | struct dirent *de; | |
58 | struct sys_dev *head = NULL; | |
59 | struct sys_dev *list = NULL; | |
60f0f54d | 60 | struct sys_dev *vmd = NULL; |
a8e5382a | 61 | enum sys_dev_type type; |
9c747fa0 | 62 | unsigned long long dev_id; |
6b781d33 | 63 | unsigned long long class; |
a8e5382a LM |
64 | |
65 | if (strcmp(driver, "isci") == 0) | |
66 | type = SYS_DEV_SAS; | |
67 | else if (strcmp(driver, "ahci") == 0) | |
68 | type = SYS_DEV_SATA; | |
60f0f54d PB |
69 | else if (strcmp(driver, "nvme") == 0) { |
70 | /* if looking for nvme devs, first look for vmd */ | |
71 | vmd = find_driver_devices("pci", "vmd"); | |
614902f6 | 72 | type = SYS_DEV_NVME; |
60f0f54d PB |
73 | } else if (strcmp(driver, "vmd") == 0) |
74 | type = SYS_DEV_VMD; | |
a8e5382a LM |
75 | else |
76 | type = SYS_DEV_UNKNOWN; | |
b390f610 DW |
77 | |
78 | sprintf(path, "/sys/bus/%s/drivers/%s", bus, driver); | |
79 | driver_dir = opendir(path); | |
60f0f54d PB |
80 | if (!driver_dir) { |
81 | if (vmd) | |
82 | free_sys_dev(&vmd); | |
b390f610 | 83 | return NULL; |
60f0f54d | 84 | } |
b390f610 | 85 | for (de = readdir(driver_dir); de; de = readdir(driver_dir)) { |
5a1920f2 | 86 | int n; |
60f0f54d | 87 | int skip = 0; |
5a1920f2 | 88 | |
b390f610 DW |
89 | /* is 'de' a device? check that the 'subsystem' link exists and |
90 | * that its target matches 'bus' | |
91 | */ | |
92 | sprintf(path, "/sys/bus/%s/drivers/%s/%s/subsystem", | |
93 | bus, driver, de->d_name); | |
5a1920f2 | 94 | n = readlink(path, link, sizeof(link)); |
f21e18ca | 95 | if (n < 0 || n >= (int)sizeof(link)) |
b390f610 | 96 | continue; |
5a1920f2 | 97 | link[n] = '\0'; |
b390f610 DW |
98 | c = strrchr(link, '/'); |
99 | if (!c) | |
100 | continue; | |
101 | if (strncmp(bus, c+1, strlen(bus)) != 0) | |
102 | continue; | |
103 | ||
a8e5382a LM |
104 | sprintf(path, "/sys/bus/%s/drivers/%s/%s", |
105 | bus, driver, de->d_name); | |
106 | ||
60f0f54d PB |
107 | /* if searching for nvme - skip vmd connected one */ |
108 | if (type == SYS_DEV_NVME) { | |
109 | struct sys_dev *dev; | |
110 | char *rp = realpath(path, NULL); | |
111 | for (dev = vmd; dev; dev = dev->next) { | |
112 | if ((strncmp(dev->path, rp, strlen(dev->path)) == 0)) | |
113 | skip = 1; | |
114 | } | |
115 | free(rp); | |
116 | } | |
117 | ||
118 | /* if it's not Intel device or mark as VMD connected - skip it. */ | |
119 | if (devpath_to_vendor(path) != 0x8086 || skip == 1) | |
a8e5382a LM |
120 | continue; |
121 | ||
9c747fa0 LM |
122 | if (devpath_to_ll(path, "device", &dev_id) != 0) |
123 | continue; | |
124 | ||
6b781d33 AP |
125 | if (devpath_to_ll(path, "class", &class) != 0) |
126 | continue; | |
127 | ||
30730cb8 AO |
128 | /* |
129 | * Each VMD device (domain) adds separate PCI bus, it is better | |
130 | * to store path as a path to that bus (easier further | |
131 | * determination which NVMe dev is connected to this particular | |
132 | * VMD domain). | |
133 | */ | |
134 | if (type == SYS_DEV_VMD) { | |
135 | sprintf(path, "/sys/bus/%s/drivers/%s/%s/domain/device", | |
136 | bus, driver, de->d_name); | |
137 | } | |
138 | p = realpath(path, NULL); | |
139 | if (p == NULL) { | |
140 | pr_err("Unable to get real path for '%s'\n", path); | |
141 | continue; | |
142 | } | |
143 | ||
b390f610 DW |
144 | /* start / add list entry */ |
145 | if (!head) { | |
503975b9 | 146 | head = xmalloc(sizeof(*head)); |
b390f610 DW |
147 | list = head; |
148 | } else { | |
503975b9 | 149 | list->next = xmalloc(sizeof(*head)); |
b390f610 DW |
150 | list = list->next; |
151 | } | |
152 | ||
153 | if (!list) { | |
154 | free_sys_dev(&head); | |
155 | break; | |
156 | } | |
157 | ||
9c747fa0 | 158 | list->dev_id = (__u16) dev_id; |
6b781d33 | 159 | list->class = (__u32) class; |
a8e5382a | 160 | list->type = type; |
b390f610 | 161 | list->next = NULL; |
30730cb8 AO |
162 | list->path = p; |
163 | ||
a8e5382a LM |
164 | if ((list->pci_id = strrchr(list->path, '/')) != NULL) |
165 | list->pci_id++; | |
b390f610 | 166 | } |
2a17c77b | 167 | closedir(driver_dir); |
60f0f54d PB |
168 | |
169 | if (vmd) { | |
170 | if (list) | |
171 | list->next = vmd; | |
172 | else | |
173 | head = vmd; | |
174 | } | |
175 | ||
b390f610 DW |
176 | return head; |
177 | } | |
178 | ||
9c747fa0 | 179 | static struct sys_dev *intel_devices=NULL; |
9bc4ae77 | 180 | static time_t valid_time = 0; |
9c747fa0 | 181 | |
0858eccf AP |
182 | struct sys_dev *device_by_id(__u16 device_id) |
183 | { | |
184 | struct sys_dev *iter; | |
185 | ||
186 | for (iter = intel_devices; iter != NULL; iter = iter->next) | |
187 | if (iter->dev_id == device_id) | |
188 | return iter; | |
189 | return NULL; | |
190 | } | |
191 | ||
d3c11416 AO |
192 | struct sys_dev *device_by_id_and_path(__u16 device_id, const char *path) |
193 | { | |
194 | struct sys_dev *iter; | |
195 | ||
196 | for (iter = intel_devices; iter != NULL; iter = iter->next) | |
197 | if ((iter->dev_id == device_id) && strstr(iter->path, path)) | |
198 | return iter; | |
199 | return NULL; | |
200 | } | |
201 | ||
9c747fa0 LM |
202 | static int devpath_to_ll(const char *dev_path, const char *entry, unsigned long long *val) |
203 | { | |
204 | char path[strlen(dev_path) + strlen(entry) + 2]; | |
205 | int fd; | |
206 | int n; | |
207 | ||
208 | sprintf(path, "%s/%s", dev_path, entry); | |
209 | ||
210 | fd = open(path, O_RDONLY); | |
211 | if (fd < 0) | |
212 | return -1; | |
213 | n = sysfs_fd_get_ll(fd, val); | |
214 | close(fd); | |
215 | return n; | |
216 | } | |
217 | ||
60f0f54d | 218 | __u16 devpath_to_vendor(const char *dev_path) |
b390f610 DW |
219 | { |
220 | char path[strlen(dev_path) + strlen("/vendor") + 1]; | |
221 | char vendor[7]; | |
222 | int fd; | |
223 | __u16 id = 0xffff; | |
224 | int n; | |
225 | ||
226 | sprintf(path, "%s/vendor", dev_path); | |
227 | ||
228 | fd = open(path, O_RDONLY); | |
229 | if (fd < 0) | |
230 | return 0xffff; | |
231 | ||
232 | n = read(fd, vendor, sizeof(vendor)); | |
233 | if (n == sizeof(vendor)) { | |
234 | vendor[n - 1] = '\0'; | |
235 | id = strtoul(vendor, NULL, 16); | |
236 | } | |
237 | close(fd); | |
238 | ||
239 | return id; | |
240 | } | |
241 | ||
a8e5382a | 242 | struct sys_dev *find_intel_devices(void) |
b390f610 | 243 | { |
614902f6 | 244 | struct sys_dev *ahci, *isci, *nvme; |
a8e5382a | 245 | |
9bc4ae77 N |
246 | if (valid_time > time(0) - 10) |
247 | return intel_devices; | |
248 | ||
249 | if (intel_devices) | |
250 | free_sys_dev(&intel_devices); | |
251 | ||
a8e5382a LM |
252 | isci = find_driver_devices("pci", "isci"); |
253 | ahci = find_driver_devices("pci", "ahci"); | |
60f0f54d | 254 | /* Searching for NVMe will return list of NVMe and VMD controllers */ |
614902f6 | 255 | nvme = find_driver_devices("pci", "nvme"); |
a8e5382a | 256 | |
614902f6 PB |
257 | if (!isci && !ahci) { |
258 | ahci = nvme; | |
259 | } else if (!ahci) { | |
a8e5382a | 260 | ahci = isci; |
614902f6 PB |
261 | struct sys_dev *elem = ahci; |
262 | while (elem->next) | |
263 | elem = elem->next; | |
264 | elem->next = nvme; | |
a8e5382a LM |
265 | } else { |
266 | struct sys_dev *elem = ahci; | |
267 | while (elem->next) | |
268 | elem = elem->next; | |
269 | elem->next = isci; | |
614902f6 PB |
270 | while (elem->next) |
271 | elem = elem->next; | |
272 | elem->next = nvme; | |
a8e5382a | 273 | } |
9bc4ae77 N |
274 | intel_devices = ahci; |
275 | valid_time = time(0); | |
276 | return intel_devices; | |
b390f610 DW |
277 | } |
278 | ||
3c8bfb5d | 279 | /* |
fc13853f | 280 | * PCI Expansion ROM Data Structure Format */ |
3c8bfb5d LM |
281 | struct pciExpDataStructFormat { |
282 | __u8 ver[4]; | |
283 | __u16 vendorID; | |
284 | __u16 deviceID; | |
6b781d33 | 285 | __u16 devListOffset; |
f666bcc6 PB |
286 | __u16 pciDataStructLen; |
287 | __u8 pciDataStructRev; | |
3c8bfb5d LM |
288 | } __attribute__ ((packed)); |
289 | ||
5e1d6128 | 290 | struct orom_entry *orom_entries; |
0858eccf | 291 | |
72a45777 | 292 | const struct orom_entry *get_orom_entry_by_device_id(__u16 dev_id) |
6b781d33 | 293 | { |
5e1d6128 AP |
294 | struct orom_entry *entry; |
295 | struct devid_list *devid; | |
6b781d33 | 296 | |
5e1d6128 AP |
297 | for (entry = orom_entries; entry; entry = entry->next) { |
298 | for (devid = entry->devid_list; devid; devid = devid->next) { | |
299 | if (devid->devid == dev_id) | |
72a45777 | 300 | return entry; |
6b781d33 AP |
301 | } |
302 | } | |
5e1d6128 | 303 | |
6b781d33 AP |
304 | return NULL; |
305 | } | |
306 | ||
72a45777 PB |
307 | const struct imsm_orom *get_orom_by_device_id(__u16 dev_id) |
308 | { | |
309 | const struct orom_entry *entry = get_orom_entry_by_device_id(dev_id); | |
310 | ||
311 | if (entry) | |
312 | return &entry->orom; | |
313 | ||
314 | return NULL; | |
315 | } | |
316 | ||
5e1d6128 | 317 | static struct orom_entry *add_orom(const struct imsm_orom *orom) |
6b781d33 | 318 | { |
5e1d6128 AP |
319 | struct orom_entry *list; |
320 | struct orom_entry *prev = NULL; | |
321 | ||
322 | for (list = orom_entries; list; prev = list, list = list->next) | |
323 | ; | |
324 | ||
325 | list = xmalloc(sizeof(struct orom_entry)); | |
326 | list->orom = *orom; | |
327 | list->devid_list = NULL; | |
328 | list->next = NULL; | |
329 | ||
330 | if (prev == NULL) | |
331 | orom_entries = list; | |
332 | else | |
333 | prev->next = list; | |
334 | ||
335 | return list; | |
6b781d33 AP |
336 | } |
337 | ||
5e1d6128 | 338 | static void add_orom_device_id(struct orom_entry *entry, __u16 dev_id) |
6b781d33 | 339 | { |
6b781d33 AP |
340 | struct devid_list *list; |
341 | struct devid_list *prev = NULL; | |
342 | ||
5e1d6128 AP |
343 | for (list = entry->devid_list; list; prev = list, list = list->next) { |
344 | if (list->devid == dev_id) | |
6b781d33 | 345 | return; |
6b781d33 | 346 | } |
5e1d6128 AP |
347 | list = xmalloc(sizeof(struct devid_list)); |
348 | list->devid = dev_id; | |
349 | list->next = NULL; | |
350 | ||
351 | if (prev == NULL) | |
352 | entry->devid_list = list; | |
353 | else | |
354 | prev->next = list; | |
6b781d33 | 355 | } |
fc13853f | 356 | |
3c8bfb5d | 357 | static int scan(const void *start, const void *end, const void *data) |
b390f610 DW |
358 | { |
359 | int offset; | |
6b781d33 | 360 | const struct imsm_orom *imsm_mem = NULL; |
b390f610 | 361 | int len = (end - start); |
3c8bfb5d LM |
362 | struct pciExpDataStructFormat *ptr= (struct pciExpDataStructFormat *)data; |
363 | ||
9c747fa0 LM |
364 | if (data + 0x18 > end) { |
365 | dprintf("cannot find pciExpDataStruct \n"); | |
366 | return 0; | |
367 | } | |
368 | ||
3c8bfb5d LM |
369 | dprintf("ptr->vendorID: %lx __le16_to_cpu(ptr->deviceID): %lx \n", |
370 | (ulong) __le16_to_cpu(ptr->vendorID), | |
371 | (ulong) __le16_to_cpu(ptr->deviceID)); | |
372 | ||
6b781d33 | 373 | if (__le16_to_cpu(ptr->vendorID) != 0x8086) |
3c8bfb5d | 374 | return 0; |
b390f610 | 375 | |
5c4cc0c8 RS |
376 | if (get_orom_by_device_id(ptr->deviceID)) |
377 | return 0; | |
378 | ||
b390f610 | 379 | for (offset = 0; offset < len; offset += 4) { |
6b781d33 AP |
380 | const void *mem = start + offset; |
381 | ||
382 | if ((memcmp(mem, IMSM_OROM_SIGNATURE, 4) == 0)) { | |
383 | imsm_mem = mem; | |
384 | break; | |
b390f610 DW |
385 | } |
386 | } | |
6b781d33 AP |
387 | |
388 | if (!imsm_mem) | |
389 | return 0; | |
390 | ||
5e1d6128 | 391 | struct orom_entry *orom = add_orom(imsm_mem); |
6b781d33 | 392 | |
f666bcc6 PB |
393 | /* only PciDataStructure with revision 3 and above supports devices list. */ |
394 | if (ptr->pciDataStructRev >= 3 && ptr->devListOffset) { | |
6b781d33 AP |
395 | const __u16 *dev_list = (void *)ptr + ptr->devListOffset; |
396 | int i; | |
397 | ||
398 | for (i = 0; dev_list[i] != 0; i++) | |
399 | add_orom_device_id(orom, dev_list[i]); | |
400 | } else { | |
401 | add_orom_device_id(orom, __le16_to_cpu(ptr->deviceID)); | |
402 | } | |
403 | ||
b390f610 DW |
404 | return 0; |
405 | } | |
406 | ||
6b781d33 | 407 | const struct imsm_orom *imsm_platform_test(struct sys_dev *hba) |
fc13853f | 408 | { |
6b781d33 AP |
409 | struct imsm_orom orom = { |
410 | .signature = IMSM_OROM_SIGNATURE, | |
411 | .rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | | |
412 | IMSM_OROM_RLC_RAID10 | IMSM_OROM_RLC_RAID5, | |
413 | .sss = IMSM_OROM_SSS_4kB | IMSM_OROM_SSS_8kB | | |
414 | IMSM_OROM_SSS_16kB | IMSM_OROM_SSS_32kB | | |
415 | IMSM_OROM_SSS_64kB | IMSM_OROM_SSS_128kB | | |
416 | IMSM_OROM_SSS_256kB | IMSM_OROM_SSS_512kB | | |
417 | IMSM_OROM_SSS_1MB | IMSM_OROM_SSS_2MB, | |
418 | .dpa = IMSM_OROM_DISKS_PER_ARRAY, | |
419 | .tds = IMSM_OROM_TOTAL_DISKS, | |
420 | .vpa = IMSM_OROM_VOLUMES_PER_ARRAY, | |
421 | .vphba = IMSM_OROM_VOLUMES_PER_HBA | |
422 | }; | |
423 | orom.attr = orom.rlc | IMSM_OROM_ATTR_ChecksumVerify; | |
fc13853f LM |
424 | |
425 | if (check_env("IMSM_TEST_OROM_NORAID5")) { | |
6b781d33 | 426 | orom.rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | |
fc13853f LM |
427 | IMSM_OROM_RLC_RAID10; |
428 | } | |
6b781d33 AP |
429 | if (check_env("IMSM_TEST_AHCI_EFI_NORAID5") && (hba->type == SYS_DEV_SAS)) { |
430 | orom.rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | | |
fc13853f LM |
431 | IMSM_OROM_RLC_RAID10; |
432 | } | |
6b781d33 AP |
433 | if (check_env("IMSM_TEST_SCU_EFI_NORAID5") && (hba->type == SYS_DEV_SATA)) { |
434 | orom.rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | | |
fc13853f | 435 | IMSM_OROM_RLC_RAID10; |
cceebc67 DW |
436 | } |
437 | ||
5e1d6128 | 438 | struct orom_entry *ret = add_orom(&orom); |
6b781d33 AP |
439 | |
440 | add_orom_device_id(ret, hba->dev_id); | |
441 | ||
5e1d6128 | 442 | return &ret->orom; |
fc13853f LM |
443 | } |
444 | ||
6b781d33 | 445 | static const struct imsm_orom *find_imsm_hba_orom(struct sys_dev *hba) |
fc13853f LM |
446 | { |
447 | unsigned long align; | |
448 | ||
6b781d33 AP |
449 | if (check_env("IMSM_TEST_OROM")) |
450 | return imsm_platform_test(hba); | |
fc13853f | 451 | |
fc13853f | 452 | /* return empty OROM capabilities in EFI test mode */ |
6b781d33 | 453 | if (check_env("IMSM_TEST_AHCI_EFI") || check_env("IMSM_TEST_SCU_EFI")) |
fc13853f LM |
454 | return NULL; |
455 | ||
9bc4ae77 | 456 | find_intel_devices(); |
9c747fa0 LM |
457 | |
458 | if (intel_devices == NULL) | |
b390f610 DW |
459 | return NULL; |
460 | ||
461 | /* scan option-rom memory looking for an imsm signature */ | |
969c2555 DW |
462 | if (check_env("IMSM_SAFE_OROM_SCAN")) |
463 | align = 2048; | |
464 | else | |
465 | align = 512; | |
466 | if (probe_roms_init(align) != 0) | |
b390f610 DW |
467 | return NULL; |
468 | probe_roms(); | |
9c747fa0 | 469 | /* ignore return value - True is returned if both adapater roms are found */ |
fc13853f | 470 | scan_adapter_roms(scan); |
b390f610 DW |
471 | probe_roms_exit(); |
472 | ||
6b781d33 | 473 | return get_orom_by_device_id(hba->dev_id); |
fc13853f LM |
474 | } |
475 | ||
1a901471 LM |
476 | #define GUID_STR_MAX 37 /* according to GUID format: |
477 | * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" */ | |
478 | ||
479 | #define EFI_GUID(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \ | |
480 | ((struct efi_guid) \ | |
481 | {{ (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) & 0xff, \ | |
482 | (b) & 0xff, ((b) >> 8) & 0xff, \ | |
483 | (c) & 0xff, ((c) >> 8) & 0xff, \ | |
484 | (d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) }}) | |
fc13853f | 485 | |
5a6baf66 | 486 | #define SYS_EFI_VAR_PATH "/sys/firmware/efi/vars" |
88605db9 | 487 | #define SYS_EFIVARS_PATH "/sys/firmware/efi/efivars" |
5a6baf66 LM |
488 | #define SCU_PROP "RstScuV" |
489 | #define AHCI_PROP "RstSataV" | |
81188ef8 AP |
490 | #define AHCI_SSATA_PROP "RstsSatV" |
491 | #define AHCI_CSATA_PROP "RstCSatV" | |
60f0f54d | 492 | #define VMD_PROP "RstUefiV" |
5a6baf66 LM |
493 | |
494 | #define VENDOR_GUID \ | |
495 | EFI_GUID(0x193dfefa, 0xa445, 0x4302, 0x99, 0xd8, 0xef, 0x3a, 0xad, 0x1a, 0x04, 0xc6) | |
496 | ||
6b781d33 | 497 | #define PCI_CLASS_RAID_CNTRL 0x010400 |
5a6baf66 | 498 | |
88605db9 AP |
499 | static int read_efi_var(void *buffer, ssize_t buf_size, char *variable_name, struct efi_guid guid) |
500 | { | |
501 | char path[PATH_MAX]; | |
502 | char buf[GUID_STR_MAX]; | |
503 | int fd; | |
504 | ssize_t n; | |
505 | ||
506 | snprintf(path, PATH_MAX, "%s/%s-%s", SYS_EFIVARS_PATH, variable_name, guid_str(buf, guid)); | |
507 | ||
508 | fd = open(path, O_RDONLY); | |
509 | if (fd < 0) | |
510 | return 1; | |
511 | ||
512 | /* read the variable attributes and ignore it */ | |
513 | n = read(fd, buf, sizeof(__u32)); | |
514 | if (n < 0) { | |
515 | close(fd); | |
516 | return 1; | |
517 | } | |
518 | ||
519 | /* read the variable data */ | |
520 | n = read(fd, buffer, buf_size); | |
521 | close(fd); | |
522 | if (n < buf_size) | |
523 | return 1; | |
524 | ||
525 | return 0; | |
526 | } | |
527 | ||
528 | static int read_efi_variable(void *buffer, ssize_t buf_size, char *variable_name, struct efi_guid guid) | |
5a6baf66 | 529 | { |
5a6baf66 LM |
530 | char path[PATH_MAX]; |
531 | char buf[GUID_STR_MAX]; | |
aae4c111 HCP |
532 | int dfd; |
533 | ssize_t n, var_data_len; | |
534 | ||
88605db9 AP |
535 | /* Try to read the variable using the new efivarfs interface first. |
536 | * If that fails, fall back to the old sysfs-efivars interface. */ | |
537 | if (!read_efi_var(buffer, buf_size, variable_name, guid)) | |
538 | return 0; | |
539 | ||
aae4c111 HCP |
540 | snprintf(path, PATH_MAX, "%s/%s-%s/size", SYS_EFI_VAR_PATH, variable_name, guid_str(buf, guid)); |
541 | ||
542 | dprintf("EFI VAR: path=%s\n", path); | |
543 | /* get size of variable data */ | |
544 | dfd = open(path, O_RDONLY); | |
545 | if (dfd < 0) | |
546 | return 1; | |
547 | ||
548 | n = read(dfd, &buf, sizeof(buf)); | |
549 | close(dfd); | |
550 | if (n < 0) | |
551 | return 1; | |
552 | buf[n] = '\0'; | |
553 | ||
554 | errno = 0; | |
555 | var_data_len = strtoul(buf, NULL, 16); | |
d7be7d87 JS |
556 | if ((errno == ERANGE && (var_data_len == LONG_MAX)) || |
557 | (errno != 0 && var_data_len == 0)) | |
aae4c111 HCP |
558 | return 1; |
559 | ||
560 | /* get data */ | |
561 | snprintf(path, PATH_MAX, "%s/%s-%s/data", SYS_EFI_VAR_PATH, variable_name, guid_str(buf, guid)); | |
562 | ||
563 | dprintf("EFI VAR: path=%s\n", path); | |
564 | dfd = open(path, O_RDONLY); | |
565 | if (dfd < 0) | |
566 | return 1; | |
567 | ||
568 | n = read(dfd, buffer, buf_size); | |
569 | close(dfd); | |
570 | if (n != var_data_len || n < buf_size) { | |
571 | return 1; | |
572 | } | |
573 | ||
574 | return 0; | |
575 | } | |
5a6baf66 | 576 | |
6b781d33 | 577 | const struct imsm_orom *find_imsm_efi(struct sys_dev *hba) |
aae4c111 | 578 | { |
6b781d33 | 579 | struct imsm_orom orom; |
5e1d6128 | 580 | struct orom_entry *ret; |
81188ef8 | 581 | int err; |
5a6baf66 | 582 | |
6b781d33 AP |
583 | if (check_env("IMSM_TEST_AHCI_EFI") || check_env("IMSM_TEST_SCU_EFI")) |
584 | return imsm_platform_test(hba); | |
5a6baf66 | 585 | |
5a6baf66 | 586 | /* OROM test is set, return that there is no EFI capabilities */ |
aae4c111 | 587 | if (check_env("IMSM_TEST_OROM")) |
5a6baf66 | 588 | return NULL; |
5a6baf66 | 589 | |
6b781d33 | 590 | if (hba->type == SYS_DEV_SATA && hba->class != PCI_CLASS_RAID_CNTRL) |
5a6baf66 | 591 | return NULL; |
aae4c111 | 592 | |
81188ef8 AP |
593 | err = read_efi_variable(&orom, sizeof(orom), hba->type == SYS_DEV_SAS ? SCU_PROP : AHCI_PROP, VENDOR_GUID); |
594 | ||
595 | /* try to read variable for second AHCI controller */ | |
596 | if (err && hba->type == SYS_DEV_SATA) | |
597 | err = read_efi_variable(&orom, sizeof(orom), AHCI_SSATA_PROP, VENDOR_GUID); | |
598 | ||
599 | /* try to read variable for combined AHCI controllers */ | |
600 | if (err && hba->type == SYS_DEV_SATA) { | |
5e1d6128 | 601 | static struct orom_entry *csata; |
81188ef8 AP |
602 | |
603 | err = read_efi_variable(&orom, sizeof(orom), AHCI_CSATA_PROP, VENDOR_GUID); | |
604 | if (!err) { | |
605 | if (!csata) | |
606 | csata = add_orom(&orom); | |
607 | add_orom_device_id(csata, hba->dev_id); | |
60f0f54d | 608 | csata->type = hba->type; |
5e1d6128 | 609 | return &csata->orom; |
81188ef8 AP |
610 | } |
611 | } | |
612 | ||
60f0f54d PB |
613 | if (hba->type == SYS_DEV_VMD) { |
614 | err = read_efi_variable(&orom, sizeof(orom), VMD_PROP, VENDOR_GUID); | |
615 | } | |
616 | ||
81188ef8 | 617 | if (err) |
6b781d33 | 618 | return NULL; |
5a6baf66 | 619 | |
6b781d33 AP |
620 | ret = add_orom(&orom); |
621 | add_orom_device_id(ret, hba->dev_id); | |
60f0f54d | 622 | ret->type = hba->type; |
6b781d33 | 623 | |
5e1d6128 | 624 | return &ret->orom; |
fc13853f LM |
625 | } |
626 | ||
614902f6 PB |
627 | const struct imsm_orom *find_imsm_nvme(struct sys_dev *hba) |
628 | { | |
5e1d6128 | 629 | static struct orom_entry *nvme_orom; |
614902f6 PB |
630 | |
631 | if (hba->type != SYS_DEV_NVME) | |
632 | return NULL; | |
633 | ||
634 | if (!nvme_orom) { | |
635 | struct imsm_orom nvme_orom_compat = { | |
636 | .signature = IMSM_NVME_OROM_COMPAT_SIGNATURE, | |
637 | .rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | | |
638 | IMSM_OROM_RLC_RAID10 | IMSM_OROM_RLC_RAID5, | |
639 | .sss = IMSM_OROM_SSS_4kB | IMSM_OROM_SSS_8kB | | |
640 | IMSM_OROM_SSS_16kB | IMSM_OROM_SSS_32kB | | |
641 | IMSM_OROM_SSS_64kB | IMSM_OROM_SSS_128kB, | |
642 | .dpa = IMSM_OROM_DISKS_PER_ARRAY_NVME, | |
643 | .tds = IMSM_OROM_TOTAL_DISKS_NVME, | |
644 | .vpa = IMSM_OROM_VOLUMES_PER_ARRAY, | |
645 | .vphba = IMSM_OROM_TOTAL_DISKS_NVME / 2 * IMSM_OROM_VOLUMES_PER_ARRAY, | |
646 | .attr = IMSM_OROM_ATTR_2TB | IMSM_OROM_ATTR_2TB_DISK, | |
0858eccf | 647 | .driver_features = IMSM_OROM_CAPABILITIES_EnterpriseSystem |
614902f6 PB |
648 | }; |
649 | nvme_orom = add_orom(&nvme_orom_compat); | |
650 | } | |
651 | add_orom_device_id(nvme_orom, hba->dev_id); | |
60f0f54d | 652 | nvme_orom->type = SYS_DEV_NVME; |
5e1d6128 | 653 | return &nvme_orom->orom; |
614902f6 PB |
654 | } |
655 | ||
6b781d33 | 656 | const struct imsm_orom *find_imsm_capability(struct sys_dev *hba) |
fc13853f | 657 | { |
6b781d33 AP |
658 | const struct imsm_orom *cap = get_orom_by_device_id(hba->dev_id); |
659 | ||
660 | if (cap) | |
661 | return cap; | |
fc13853f | 662 | |
614902f6 PB |
663 | if (hba->type == SYS_DEV_NVME) |
664 | return find_imsm_nvme(hba); | |
6b781d33 | 665 | if ((cap = find_imsm_efi(hba)) != NULL) |
5a6baf66 | 666 | return cap; |
6b781d33 | 667 | if ((cap = find_imsm_hba_orom(hba)) != NULL) |
fc13853f | 668 | return cap; |
614902f6 | 669 | |
b390f610 DW |
670 | return NULL; |
671 | } | |
25921536 | 672 | |
d835518b BK |
673 | /* Check whether the nvme device is represented by nvme subsytem, |
674 | * if yes virtual path should be changed to hardware device path, | |
675 | * to allow IMSM capabilities detection. | |
676 | * Returns: | |
677 | * hardware path to device - if the device is represented via | |
678 | * nvme virtual subsytem | |
679 | * NULL - if the device is not represented via nvme virtual subsytem | |
680 | */ | |
681 | char *get_nvme_multipath_dev_hw_path(const char *dev_path) | |
682 | { | |
683 | DIR *dir; | |
684 | struct dirent *ent; | |
685 | char *rp = NULL; | |
686 | ||
687 | if (strncmp(dev_path, NVME_SUBSYS_PATH, strlen(NVME_SUBSYS_PATH)) != 0) | |
688 | return NULL; | |
689 | ||
690 | dir = opendir(dev_path); | |
691 | if (!dir) | |
692 | return NULL; | |
693 | ||
694 | for (ent = readdir(dir); ent; ent = readdir(dir)) { | |
695 | char buf[strlen(dev_path) + strlen(ent->d_name) + 1]; | |
696 | ||
697 | /* Check if dir is a controller, ignore namespaces*/ | |
698 | if (!(strncmp(ent->d_name, "nvme", 4) == 0) || | |
699 | (strrchr(ent->d_name, 'n') != &ent->d_name[0])) | |
700 | continue; | |
701 | ||
702 | sprintf(buf, "%s/%s", dev_path, ent->d_name); | |
703 | rp = realpath(buf, NULL); | |
704 | break; | |
705 | } | |
706 | ||
707 | closedir(dir); | |
708 | return rp; | |
709 | } | |
710 | ||
25921536 DW |
711 | char *devt_to_devpath(dev_t dev) |
712 | { | |
37f0e1e0 | 713 | char device[46]; |
d835518b BK |
714 | char *rp; |
715 | char *buf; | |
25921536 DW |
716 | |
717 | sprintf(device, "/sys/dev/block/%d:%d/device", major(dev), minor(dev)); | |
d835518b BK |
718 | |
719 | rp = realpath(device, NULL); | |
720 | if (!rp) | |
721 | return NULL; | |
722 | ||
723 | buf = get_nvme_multipath_dev_hw_path(rp); | |
724 | if (buf) { | |
725 | free(rp); | |
726 | return buf; | |
727 | } | |
728 | ||
729 | return rp; | |
25921536 DW |
730 | } |
731 | ||
a8e5382a | 732 | char *diskfd_to_devpath(int fd) |
25921536 DW |
733 | { |
734 | /* return the device path for a disk, return NULL on error or fd | |
735 | * refers to a partition | |
736 | */ | |
737 | struct stat st; | |
738 | ||
739 | if (fstat(fd, &st) != 0) | |
740 | return NULL; | |
741 | if (!S_ISBLK(st.st_mode)) | |
742 | return NULL; | |
743 | ||
744 | return devt_to_devpath(st.st_rdev); | |
745 | } | |
746 | ||
747 | int path_attached_to_hba(const char *disk_path, const char *hba_path) | |
748 | { | |
749 | int rc; | |
750 | ||
fc13853f LM |
751 | if (check_env("IMSM_TEST_AHCI_DEV") || |
752 | check_env("IMSM_TEST_SCU_DEV")) { | |
753 | return 1; | |
754 | } | |
755 | ||
25921536 DW |
756 | if (!disk_path || !hba_path) |
757 | return 0; | |
a8e5382a | 758 | dprintf("hba: %s - disk: %s\n", hba_path, disk_path); |
25921536 DW |
759 | if (strncmp(disk_path, hba_path, strlen(hba_path)) == 0) |
760 | rc = 1; | |
761 | else | |
762 | rc = 0; | |
763 | ||
764 | return rc; | |
765 | } | |
766 | ||
767 | int devt_attached_to_hba(dev_t dev, const char *hba_path) | |
768 | { | |
769 | char *disk_path = devt_to_devpath(dev); | |
770 | int rc = path_attached_to_hba(disk_path, hba_path); | |
771 | ||
772 | if (disk_path) | |
773 | free(disk_path); | |
774 | ||
775 | return rc; | |
776 | } | |
777 | ||
778 | int disk_attached_to_hba(int fd, const char *hba_path) | |
779 | { | |
780 | char *disk_path = diskfd_to_devpath(fd); | |
781 | int rc = path_attached_to_hba(disk_path, hba_path); | |
782 | ||
783 | if (disk_path) | |
784 | free(disk_path); | |
785 | ||
786 | return rc; | |
787 | } | |
60f0f54d PB |
788 | |
789 | char *vmd_domain_to_controller(struct sys_dev *hba, char *buf) | |
790 | { | |
791 | struct dirent *ent; | |
792 | DIR *dir; | |
793 | char path[PATH_MAX]; | |
794 | ||
795 | if (!hba) | |
796 | return NULL; | |
797 | ||
798 | if (hba->type != SYS_DEV_VMD) | |
799 | return NULL; | |
800 | ||
801 | dir = opendir("/sys/bus/pci/drivers/vmd"); | |
b9135011 JS |
802 | if (!dir) |
803 | return NULL; | |
60f0f54d | 804 | |
b9135011 | 805 | for (ent = readdir(dir); ent; ent = readdir(dir)) { |
60f0f54d PB |
806 | sprintf(path, "/sys/bus/pci/drivers/vmd/%s/domain/device", |
807 | ent->d_name); | |
808 | ||
809 | if (!realpath(path, buf)) | |
810 | continue; | |
811 | ||
812 | if (strncmp(buf, hba->path, strlen(buf)) == 0) { | |
813 | sprintf(path, "/sys/bus/pci/drivers/vmd/%s", ent->d_name); | |
b9135011 | 814 | closedir(dir); |
7071320a | 815 | return realpath(path, buf); |
60f0f54d PB |
816 | } |
817 | } | |
b9135011 JS |
818 | |
819 | closedir(dir); | |
60f0f54d PB |
820 | return NULL; |
821 | } | |
a8f3cfd5 MT |
822 | /* Verify that NVMe drive is supported by IMSM |
823 | * Returns: | |
824 | * 0 - not supported | |
825 | * 1 - supported | |
826 | */ | |
827 | int imsm_is_nvme_supported(int disk_fd, int verbose) | |
828 | { | |
829 | char nsid_path[PATH_MAX]; | |
830 | char buf[PATH_MAX]; | |
831 | struct stat stb; | |
832 | ||
833 | if (disk_fd < 0) | |
834 | return 0; | |
835 | ||
836 | if (fstat(disk_fd, &stb)) | |
837 | return 0; | |
838 | ||
839 | snprintf(nsid_path, PATH_MAX-1, "/sys/dev/block/%d:%d/nsid", | |
840 | major(stb.st_rdev), minor(stb.st_rdev)); | |
841 | ||
842 | if (load_sys(nsid_path, buf, sizeof(buf))) { | |
843 | pr_err("Cannot read %s, rejecting drive\n", nsid_path); | |
844 | return 0; | |
845 | } | |
846 | if (strtoll(buf, NULL, 10) != 1) { | |
847 | if (verbose) | |
848 | pr_err("Only first namespace is supported by IMSM, aborting\n"); | |
849 | return 0; | |
850 | } | |
851 | return 1; | |
852 | } | |
d835518b BK |
853 | |
854 | /* Verify if multipath is supported by NVMe controller | |
855 | * Returns: | |
856 | * 0 - not supported | |
857 | * 1 - supported | |
858 | */ | |
859 | int is_multipath_nvme(int disk_fd) | |
860 | { | |
861 | char path_buf[PATH_MAX]; | |
862 | char ns_path[PATH_MAX]; | |
863 | char *kname = fd2kname(disk_fd); | |
864 | ||
865 | if (!kname) | |
866 | return 0; | |
867 | sprintf(path_buf, "/sys/block/%s", kname); | |
868 | ||
869 | if (!realpath(path_buf, ns_path)) | |
870 | return 0; | |
871 | ||
872 | if (strncmp(ns_path, NVME_SUBSYS_PATH, strlen(NVME_SUBSYS_PATH)) == 0) | |
873 | return 1; | |
874 | ||
875 | return 0; | |
876 | } |