]>
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 | ||
fcebeb77 MT |
242 | /* Description: Read text value of dev_path/entry field |
243 | * Parameters: | |
244 | * dev_path - sysfs path to the device | |
245 | * entry - entry to be read | |
246 | * buf - buffer for read value | |
247 | * len - size of buf | |
248 | * verbose - error logging level | |
249 | */ | |
250 | int devpath_to_char(const char *dev_path, const char *entry, char *buf, int len, | |
251 | int verbose) | |
252 | { | |
253 | char path[PATH_MAX]; | |
254 | ||
255 | snprintf(path, sizeof(path), "%s/%s", dev_path, entry); | |
256 | if (load_sys(path, buf, len)) { | |
257 | if (verbose) | |
258 | pr_err("Cannot read %s, aborting\n", path); | |
259 | return 1; | |
260 | } | |
261 | ||
262 | return 0; | |
263 | } | |
264 | ||
a8e5382a | 265 | struct sys_dev *find_intel_devices(void) |
b390f610 | 266 | { |
614902f6 | 267 | struct sys_dev *ahci, *isci, *nvme; |
a8e5382a | 268 | |
9bc4ae77 N |
269 | if (valid_time > time(0) - 10) |
270 | return intel_devices; | |
271 | ||
272 | if (intel_devices) | |
273 | free_sys_dev(&intel_devices); | |
274 | ||
a8e5382a LM |
275 | isci = find_driver_devices("pci", "isci"); |
276 | ahci = find_driver_devices("pci", "ahci"); | |
60f0f54d | 277 | /* Searching for NVMe will return list of NVMe and VMD controllers */ |
614902f6 | 278 | nvme = find_driver_devices("pci", "nvme"); |
a8e5382a | 279 | |
614902f6 PB |
280 | if (!isci && !ahci) { |
281 | ahci = nvme; | |
282 | } else if (!ahci) { | |
a8e5382a | 283 | ahci = isci; |
614902f6 PB |
284 | struct sys_dev *elem = ahci; |
285 | while (elem->next) | |
286 | elem = elem->next; | |
287 | elem->next = nvme; | |
a8e5382a LM |
288 | } else { |
289 | struct sys_dev *elem = ahci; | |
290 | while (elem->next) | |
291 | elem = elem->next; | |
292 | elem->next = isci; | |
614902f6 PB |
293 | while (elem->next) |
294 | elem = elem->next; | |
295 | elem->next = nvme; | |
a8e5382a | 296 | } |
9bc4ae77 N |
297 | intel_devices = ahci; |
298 | valid_time = time(0); | |
299 | return intel_devices; | |
b390f610 DW |
300 | } |
301 | ||
3c8bfb5d | 302 | /* |
fc13853f | 303 | * PCI Expansion ROM Data Structure Format */ |
3c8bfb5d LM |
304 | struct pciExpDataStructFormat { |
305 | __u8 ver[4]; | |
306 | __u16 vendorID; | |
307 | __u16 deviceID; | |
6b781d33 | 308 | __u16 devListOffset; |
f666bcc6 PB |
309 | __u16 pciDataStructLen; |
310 | __u8 pciDataStructRev; | |
3c8bfb5d LM |
311 | } __attribute__ ((packed)); |
312 | ||
5e1d6128 | 313 | struct orom_entry *orom_entries; |
0858eccf | 314 | |
72a45777 | 315 | const struct orom_entry *get_orom_entry_by_device_id(__u16 dev_id) |
6b781d33 | 316 | { |
5e1d6128 AP |
317 | struct orom_entry *entry; |
318 | struct devid_list *devid; | |
6b781d33 | 319 | |
5e1d6128 AP |
320 | for (entry = orom_entries; entry; entry = entry->next) { |
321 | for (devid = entry->devid_list; devid; devid = devid->next) { | |
322 | if (devid->devid == dev_id) | |
72a45777 | 323 | return entry; |
6b781d33 AP |
324 | } |
325 | } | |
5e1d6128 | 326 | |
6b781d33 AP |
327 | return NULL; |
328 | } | |
329 | ||
72a45777 PB |
330 | const struct imsm_orom *get_orom_by_device_id(__u16 dev_id) |
331 | { | |
332 | const struct orom_entry *entry = get_orom_entry_by_device_id(dev_id); | |
333 | ||
334 | if (entry) | |
335 | return &entry->orom; | |
336 | ||
337 | return NULL; | |
338 | } | |
339 | ||
5e1d6128 | 340 | static struct orom_entry *add_orom(const struct imsm_orom *orom) |
6b781d33 | 341 | { |
5e1d6128 AP |
342 | struct orom_entry *list; |
343 | struct orom_entry *prev = NULL; | |
344 | ||
345 | for (list = orom_entries; list; prev = list, list = list->next) | |
346 | ; | |
347 | ||
348 | list = xmalloc(sizeof(struct orom_entry)); | |
349 | list->orom = *orom; | |
350 | list->devid_list = NULL; | |
351 | list->next = NULL; | |
352 | ||
353 | if (prev == NULL) | |
354 | orom_entries = list; | |
355 | else | |
356 | prev->next = list; | |
357 | ||
358 | return list; | |
6b781d33 AP |
359 | } |
360 | ||
5e1d6128 | 361 | static void add_orom_device_id(struct orom_entry *entry, __u16 dev_id) |
6b781d33 | 362 | { |
6b781d33 AP |
363 | struct devid_list *list; |
364 | struct devid_list *prev = NULL; | |
365 | ||
5e1d6128 AP |
366 | for (list = entry->devid_list; list; prev = list, list = list->next) { |
367 | if (list->devid == dev_id) | |
6b781d33 | 368 | return; |
6b781d33 | 369 | } |
5e1d6128 AP |
370 | list = xmalloc(sizeof(struct devid_list)); |
371 | list->devid = dev_id; | |
372 | list->next = NULL; | |
373 | ||
374 | if (prev == NULL) | |
375 | entry->devid_list = list; | |
376 | else | |
377 | prev->next = list; | |
6b781d33 | 378 | } |
fc13853f | 379 | |
3c8bfb5d | 380 | static int scan(const void *start, const void *end, const void *data) |
b390f610 DW |
381 | { |
382 | int offset; | |
6b781d33 | 383 | const struct imsm_orom *imsm_mem = NULL; |
b390f610 | 384 | int len = (end - start); |
3c8bfb5d LM |
385 | struct pciExpDataStructFormat *ptr= (struct pciExpDataStructFormat *)data; |
386 | ||
9c747fa0 LM |
387 | if (data + 0x18 > end) { |
388 | dprintf("cannot find pciExpDataStruct \n"); | |
389 | return 0; | |
390 | } | |
391 | ||
3c8bfb5d LM |
392 | dprintf("ptr->vendorID: %lx __le16_to_cpu(ptr->deviceID): %lx \n", |
393 | (ulong) __le16_to_cpu(ptr->vendorID), | |
394 | (ulong) __le16_to_cpu(ptr->deviceID)); | |
395 | ||
6b781d33 | 396 | if (__le16_to_cpu(ptr->vendorID) != 0x8086) |
3c8bfb5d | 397 | return 0; |
b390f610 | 398 | |
5c4cc0c8 RS |
399 | if (get_orom_by_device_id(ptr->deviceID)) |
400 | return 0; | |
401 | ||
b390f610 | 402 | for (offset = 0; offset < len; offset += 4) { |
6b781d33 AP |
403 | const void *mem = start + offset; |
404 | ||
405 | if ((memcmp(mem, IMSM_OROM_SIGNATURE, 4) == 0)) { | |
406 | imsm_mem = mem; | |
407 | break; | |
b390f610 DW |
408 | } |
409 | } | |
6b781d33 AP |
410 | |
411 | if (!imsm_mem) | |
412 | return 0; | |
413 | ||
5e1d6128 | 414 | struct orom_entry *orom = add_orom(imsm_mem); |
6b781d33 | 415 | |
f666bcc6 PB |
416 | /* only PciDataStructure with revision 3 and above supports devices list. */ |
417 | if (ptr->pciDataStructRev >= 3 && ptr->devListOffset) { | |
6b781d33 AP |
418 | const __u16 *dev_list = (void *)ptr + ptr->devListOffset; |
419 | int i; | |
420 | ||
421 | for (i = 0; dev_list[i] != 0; i++) | |
422 | add_orom_device_id(orom, dev_list[i]); | |
423 | } else { | |
424 | add_orom_device_id(orom, __le16_to_cpu(ptr->deviceID)); | |
425 | } | |
426 | ||
b390f610 DW |
427 | return 0; |
428 | } | |
429 | ||
6b781d33 | 430 | const struct imsm_orom *imsm_platform_test(struct sys_dev *hba) |
fc13853f | 431 | { |
6b781d33 AP |
432 | struct imsm_orom orom = { |
433 | .signature = IMSM_OROM_SIGNATURE, | |
434 | .rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | | |
435 | IMSM_OROM_RLC_RAID10 | IMSM_OROM_RLC_RAID5, | |
436 | .sss = IMSM_OROM_SSS_4kB | IMSM_OROM_SSS_8kB | | |
437 | IMSM_OROM_SSS_16kB | IMSM_OROM_SSS_32kB | | |
438 | IMSM_OROM_SSS_64kB | IMSM_OROM_SSS_128kB | | |
439 | IMSM_OROM_SSS_256kB | IMSM_OROM_SSS_512kB | | |
440 | IMSM_OROM_SSS_1MB | IMSM_OROM_SSS_2MB, | |
441 | .dpa = IMSM_OROM_DISKS_PER_ARRAY, | |
442 | .tds = IMSM_OROM_TOTAL_DISKS, | |
443 | .vpa = IMSM_OROM_VOLUMES_PER_ARRAY, | |
444 | .vphba = IMSM_OROM_VOLUMES_PER_HBA | |
445 | }; | |
446 | orom.attr = orom.rlc | IMSM_OROM_ATTR_ChecksumVerify; | |
fc13853f LM |
447 | |
448 | if (check_env("IMSM_TEST_OROM_NORAID5")) { | |
6b781d33 | 449 | orom.rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | |
fc13853f LM |
450 | IMSM_OROM_RLC_RAID10; |
451 | } | |
6b781d33 AP |
452 | if (check_env("IMSM_TEST_AHCI_EFI_NORAID5") && (hba->type == SYS_DEV_SAS)) { |
453 | orom.rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | | |
fc13853f LM |
454 | IMSM_OROM_RLC_RAID10; |
455 | } | |
6b781d33 AP |
456 | if (check_env("IMSM_TEST_SCU_EFI_NORAID5") && (hba->type == SYS_DEV_SATA)) { |
457 | orom.rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | | |
fc13853f | 458 | IMSM_OROM_RLC_RAID10; |
cceebc67 DW |
459 | } |
460 | ||
5e1d6128 | 461 | struct orom_entry *ret = add_orom(&orom); |
6b781d33 AP |
462 | |
463 | add_orom_device_id(ret, hba->dev_id); | |
464 | ||
5e1d6128 | 465 | return &ret->orom; |
fc13853f LM |
466 | } |
467 | ||
6b781d33 | 468 | static const struct imsm_orom *find_imsm_hba_orom(struct sys_dev *hba) |
fc13853f LM |
469 | { |
470 | unsigned long align; | |
471 | ||
6b781d33 AP |
472 | if (check_env("IMSM_TEST_OROM")) |
473 | return imsm_platform_test(hba); | |
fc13853f | 474 | |
fc13853f | 475 | /* return empty OROM capabilities in EFI test mode */ |
6b781d33 | 476 | if (check_env("IMSM_TEST_AHCI_EFI") || check_env("IMSM_TEST_SCU_EFI")) |
fc13853f LM |
477 | return NULL; |
478 | ||
9bc4ae77 | 479 | find_intel_devices(); |
9c747fa0 LM |
480 | |
481 | if (intel_devices == NULL) | |
b390f610 DW |
482 | return NULL; |
483 | ||
484 | /* scan option-rom memory looking for an imsm signature */ | |
969c2555 DW |
485 | if (check_env("IMSM_SAFE_OROM_SCAN")) |
486 | align = 2048; | |
487 | else | |
488 | align = 512; | |
489 | if (probe_roms_init(align) != 0) | |
b390f610 DW |
490 | return NULL; |
491 | probe_roms(); | |
9c747fa0 | 492 | /* ignore return value - True is returned if both adapater roms are found */ |
fc13853f | 493 | scan_adapter_roms(scan); |
b390f610 DW |
494 | probe_roms_exit(); |
495 | ||
6b781d33 | 496 | return get_orom_by_device_id(hba->dev_id); |
fc13853f LM |
497 | } |
498 | ||
1a901471 LM |
499 | #define GUID_STR_MAX 37 /* according to GUID format: |
500 | * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" */ | |
501 | ||
502 | #define EFI_GUID(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \ | |
503 | ((struct efi_guid) \ | |
504 | {{ (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) & 0xff, \ | |
505 | (b) & 0xff, ((b) >> 8) & 0xff, \ | |
506 | (c) & 0xff, ((c) >> 8) & 0xff, \ | |
507 | (d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) }}) | |
fc13853f | 508 | |
5a6baf66 | 509 | #define SYS_EFI_VAR_PATH "/sys/firmware/efi/vars" |
88605db9 | 510 | #define SYS_EFIVARS_PATH "/sys/firmware/efi/efivars" |
5a6baf66 LM |
511 | #define SCU_PROP "RstScuV" |
512 | #define AHCI_PROP "RstSataV" | |
81188ef8 | 513 | #define AHCI_SSATA_PROP "RstsSatV" |
f94df5cf | 514 | #define AHCI_TSATA_PROP "RsttSatV" |
60f0f54d | 515 | #define VMD_PROP "RstUefiV" |
5a6baf66 LM |
516 | |
517 | #define VENDOR_GUID \ | |
518 | EFI_GUID(0x193dfefa, 0xa445, 0x4302, 0x99, 0xd8, 0xef, 0x3a, 0xad, 0x1a, 0x04, 0xc6) | |
519 | ||
6b781d33 | 520 | #define PCI_CLASS_RAID_CNTRL 0x010400 |
5a6baf66 | 521 | |
f94df5cf MT |
522 | static int read_efi_var(void *buffer, ssize_t buf_size, |
523 | const char *variable_name, struct efi_guid guid) | |
88605db9 AP |
524 | { |
525 | char path[PATH_MAX]; | |
526 | char buf[GUID_STR_MAX]; | |
527 | int fd; | |
528 | ssize_t n; | |
529 | ||
530 | snprintf(path, PATH_MAX, "%s/%s-%s", SYS_EFIVARS_PATH, variable_name, guid_str(buf, guid)); | |
531 | ||
532 | fd = open(path, O_RDONLY); | |
533 | if (fd < 0) | |
534 | return 1; | |
535 | ||
536 | /* read the variable attributes and ignore it */ | |
537 | n = read(fd, buf, sizeof(__u32)); | |
538 | if (n < 0) { | |
539 | close(fd); | |
540 | return 1; | |
541 | } | |
542 | ||
543 | /* read the variable data */ | |
544 | n = read(fd, buffer, buf_size); | |
545 | close(fd); | |
546 | if (n < buf_size) | |
547 | return 1; | |
548 | ||
549 | return 0; | |
550 | } | |
551 | ||
f94df5cf MT |
552 | static int read_efi_variable(void *buffer, ssize_t buf_size, |
553 | const char *variable_name, struct efi_guid guid) | |
5a6baf66 | 554 | { |
5a6baf66 LM |
555 | char path[PATH_MAX]; |
556 | char buf[GUID_STR_MAX]; | |
aae4c111 HCP |
557 | int dfd; |
558 | ssize_t n, var_data_len; | |
559 | ||
88605db9 AP |
560 | /* Try to read the variable using the new efivarfs interface first. |
561 | * If that fails, fall back to the old sysfs-efivars interface. */ | |
562 | if (!read_efi_var(buffer, buf_size, variable_name, guid)) | |
563 | return 0; | |
564 | ||
aae4c111 HCP |
565 | snprintf(path, PATH_MAX, "%s/%s-%s/size", SYS_EFI_VAR_PATH, variable_name, guid_str(buf, guid)); |
566 | ||
567 | dprintf("EFI VAR: path=%s\n", path); | |
568 | /* get size of variable data */ | |
569 | dfd = open(path, O_RDONLY); | |
570 | if (dfd < 0) | |
571 | return 1; | |
572 | ||
573 | n = read(dfd, &buf, sizeof(buf)); | |
574 | close(dfd); | |
575 | if (n < 0) | |
576 | return 1; | |
577 | buf[n] = '\0'; | |
578 | ||
579 | errno = 0; | |
580 | var_data_len = strtoul(buf, NULL, 16); | |
d7be7d87 JS |
581 | if ((errno == ERANGE && (var_data_len == LONG_MAX)) || |
582 | (errno != 0 && var_data_len == 0)) | |
aae4c111 HCP |
583 | return 1; |
584 | ||
585 | /* get data */ | |
586 | snprintf(path, PATH_MAX, "%s/%s-%s/data", SYS_EFI_VAR_PATH, variable_name, guid_str(buf, guid)); | |
587 | ||
588 | dprintf("EFI VAR: path=%s\n", path); | |
589 | dfd = open(path, O_RDONLY); | |
590 | if (dfd < 0) | |
591 | return 1; | |
592 | ||
593 | n = read(dfd, buffer, buf_size); | |
594 | close(dfd); | |
595 | if (n != var_data_len || n < buf_size) { | |
596 | return 1; | |
597 | } | |
598 | ||
599 | return 0; | |
600 | } | |
5a6baf66 | 601 | |
6b781d33 | 602 | const struct imsm_orom *find_imsm_efi(struct sys_dev *hba) |
aae4c111 | 603 | { |
6b781d33 | 604 | struct imsm_orom orom; |
5e1d6128 | 605 | struct orom_entry *ret; |
f94df5cf MT |
606 | static const char * const sata_efivars[] = {AHCI_PROP, AHCI_SSATA_PROP, |
607 | AHCI_TSATA_PROP}; | |
608 | unsigned long i; | |
5a6baf66 | 609 | |
6b781d33 AP |
610 | if (check_env("IMSM_TEST_AHCI_EFI") || check_env("IMSM_TEST_SCU_EFI")) |
611 | return imsm_platform_test(hba); | |
5a6baf66 | 612 | |
5a6baf66 | 613 | /* OROM test is set, return that there is no EFI capabilities */ |
aae4c111 | 614 | if (check_env("IMSM_TEST_OROM")) |
5a6baf66 | 615 | return NULL; |
5a6baf66 | 616 | |
f94df5cf MT |
617 | switch (hba->type) { |
618 | case SYS_DEV_SAS: | |
619 | if (!read_efi_variable(&orom, sizeof(orom), SCU_PROP, | |
620 | VENDOR_GUID)) | |
621 | break; | |
81188ef8 | 622 | |
f94df5cf MT |
623 | return NULL; |
624 | case SYS_DEV_SATA: | |
625 | if (hba->class != PCI_CLASS_RAID_CNTRL) | |
626 | return NULL; | |
81188ef8 | 627 | |
f94df5cf MT |
628 | for (i = 0; i < ARRAY_SIZE(sata_efivars); i++) { |
629 | if (!read_efi_variable(&orom, sizeof(orom), | |
630 | sata_efivars[i], VENDOR_GUID)) | |
631 | break; | |
81188ef8 | 632 | |
81188ef8 | 633 | } |
f94df5cf MT |
634 | if (i == ARRAY_SIZE(sata_efivars)) |
635 | return NULL; | |
81188ef8 | 636 | |
f94df5cf MT |
637 | break; |
638 | case SYS_DEV_VMD: | |
639 | if (!read_efi_variable(&orom, sizeof(orom), VMD_PROP, | |
640 | VENDOR_GUID)) | |
641 | break; | |
6b781d33 | 642 | return NULL; |
f94df5cf MT |
643 | default: |
644 | return NULL; | |
645 | } | |
5a6baf66 | 646 | |
6b781d33 AP |
647 | ret = add_orom(&orom); |
648 | add_orom_device_id(ret, hba->dev_id); | |
60f0f54d | 649 | ret->type = hba->type; |
6b781d33 | 650 | |
5e1d6128 | 651 | return &ret->orom; |
fc13853f LM |
652 | } |
653 | ||
614902f6 PB |
654 | const struct imsm_orom *find_imsm_nvme(struct sys_dev *hba) |
655 | { | |
5e1d6128 | 656 | static struct orom_entry *nvme_orom; |
614902f6 PB |
657 | |
658 | if (hba->type != SYS_DEV_NVME) | |
659 | return NULL; | |
660 | ||
661 | if (!nvme_orom) { | |
662 | struct imsm_orom nvme_orom_compat = { | |
663 | .signature = IMSM_NVME_OROM_COMPAT_SIGNATURE, | |
664 | .rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | | |
665 | IMSM_OROM_RLC_RAID10 | IMSM_OROM_RLC_RAID5, | |
666 | .sss = IMSM_OROM_SSS_4kB | IMSM_OROM_SSS_8kB | | |
667 | IMSM_OROM_SSS_16kB | IMSM_OROM_SSS_32kB | | |
668 | IMSM_OROM_SSS_64kB | IMSM_OROM_SSS_128kB, | |
669 | .dpa = IMSM_OROM_DISKS_PER_ARRAY_NVME, | |
670 | .tds = IMSM_OROM_TOTAL_DISKS_NVME, | |
671 | .vpa = IMSM_OROM_VOLUMES_PER_ARRAY, | |
672 | .vphba = IMSM_OROM_TOTAL_DISKS_NVME / 2 * IMSM_OROM_VOLUMES_PER_ARRAY, | |
673 | .attr = IMSM_OROM_ATTR_2TB | IMSM_OROM_ATTR_2TB_DISK, | |
0858eccf | 674 | .driver_features = IMSM_OROM_CAPABILITIES_EnterpriseSystem |
614902f6 PB |
675 | }; |
676 | nvme_orom = add_orom(&nvme_orom_compat); | |
677 | } | |
678 | add_orom_device_id(nvme_orom, hba->dev_id); | |
60f0f54d | 679 | nvme_orom->type = SYS_DEV_NVME; |
5e1d6128 | 680 | return &nvme_orom->orom; |
614902f6 PB |
681 | } |
682 | ||
6b781d33 | 683 | const struct imsm_orom *find_imsm_capability(struct sys_dev *hba) |
fc13853f | 684 | { |
6b781d33 AP |
685 | const struct imsm_orom *cap = get_orom_by_device_id(hba->dev_id); |
686 | ||
687 | if (cap) | |
688 | return cap; | |
fc13853f | 689 | |
614902f6 PB |
690 | if (hba->type == SYS_DEV_NVME) |
691 | return find_imsm_nvme(hba); | |
6b781d33 | 692 | if ((cap = find_imsm_efi(hba)) != NULL) |
5a6baf66 | 693 | return cap; |
6b781d33 | 694 | if ((cap = find_imsm_hba_orom(hba)) != NULL) |
fc13853f | 695 | return cap; |
614902f6 | 696 | |
b390f610 DW |
697 | return NULL; |
698 | } | |
25921536 | 699 | |
d835518b BK |
700 | /* Check whether the nvme device is represented by nvme subsytem, |
701 | * if yes virtual path should be changed to hardware device path, | |
702 | * to allow IMSM capabilities detection. | |
703 | * Returns: | |
704 | * hardware path to device - if the device is represented via | |
705 | * nvme virtual subsytem | |
706 | * NULL - if the device is not represented via nvme virtual subsytem | |
707 | */ | |
708 | char *get_nvme_multipath_dev_hw_path(const char *dev_path) | |
709 | { | |
710 | DIR *dir; | |
711 | struct dirent *ent; | |
712 | char *rp = NULL; | |
713 | ||
714 | if (strncmp(dev_path, NVME_SUBSYS_PATH, strlen(NVME_SUBSYS_PATH)) != 0) | |
715 | return NULL; | |
716 | ||
717 | dir = opendir(dev_path); | |
718 | if (!dir) | |
719 | return NULL; | |
720 | ||
721 | for (ent = readdir(dir); ent; ent = readdir(dir)) { | |
722 | char buf[strlen(dev_path) + strlen(ent->d_name) + 1]; | |
723 | ||
724 | /* Check if dir is a controller, ignore namespaces*/ | |
725 | if (!(strncmp(ent->d_name, "nvme", 4) == 0) || | |
726 | (strrchr(ent->d_name, 'n') != &ent->d_name[0])) | |
727 | continue; | |
728 | ||
729 | sprintf(buf, "%s/%s", dev_path, ent->d_name); | |
730 | rp = realpath(buf, NULL); | |
731 | break; | |
732 | } | |
733 | ||
734 | closedir(dir); | |
735 | return rp; | |
736 | } | |
737 | ||
7c798f87 MT |
738 | /* Description: Return part or whole realpath for the dev |
739 | * Parameters: | |
740 | * dev - the device to be quered | |
741 | * dev_level - level of "/device" entries. It allows to caller to access | |
742 | * virtual or physical devices which are on "path" to quered | |
743 | * one. | |
744 | * buf - optional, must be PATH_MAX size. If set, then will be used. | |
745 | */ | |
746 | char *devt_to_devpath(dev_t dev, int dev_level, char *buf) | |
25921536 | 747 | { |
7c798f87 MT |
748 | char device[PATH_MAX]; |
749 | char *hw_path; | |
750 | int i; | |
751 | unsigned long device_free_len = sizeof(device) - 1; | |
752 | char dev_str[] = "/device"; | |
753 | unsigned long dev_str_len = strlen(dev_str); | |
754 | ||
755 | snprintf(device, sizeof(device), "/sys/dev/block/%d:%d", major(dev), | |
756 | minor(dev)); | |
757 | ||
758 | /* If caller wants block device, return path to it even if it is exposed | |
759 | * via virtual layer. | |
760 | */ | |
761 | if (dev_level == 0) | |
762 | return realpath(device, buf); | |
25921536 | 763 | |
7c798f87 MT |
764 | device_free_len -= strlen(device); |
765 | for (i = 0; i < dev_level; i++) { | |
766 | if (device_free_len < dev_str_len) | |
767 | return NULL; | |
d835518b | 768 | |
7c798f87 | 769 | strncat(device, dev_str, device_free_len); |
d835518b | 770 | |
7c798f87 MT |
771 | /* Resolve nvme-subsystem abstraction if needed |
772 | */ | |
773 | device_free_len -= dev_str_len; | |
774 | if (i == 0) { | |
775 | char rp[PATH_MAX]; | |
776 | ||
777 | if (!realpath(device, rp)) | |
778 | return NULL; | |
779 | hw_path = get_nvme_multipath_dev_hw_path(rp); | |
780 | if (hw_path) { | |
781 | strcpy(device, hw_path); | |
782 | device_free_len = sizeof(device) - | |
783 | strlen(device) - 1; | |
784 | free(hw_path); | |
785 | } | |
786 | } | |
d835518b BK |
787 | } |
788 | ||
7c798f87 | 789 | return realpath(device, buf); |
25921536 DW |
790 | } |
791 | ||
7c798f87 | 792 | char *diskfd_to_devpath(int fd, int dev_level, char *buf) |
25921536 DW |
793 | { |
794 | /* return the device path for a disk, return NULL on error or fd | |
795 | * refers to a partition | |
796 | */ | |
797 | struct stat st; | |
798 | ||
799 | if (fstat(fd, &st) != 0) | |
800 | return NULL; | |
801 | if (!S_ISBLK(st.st_mode)) | |
802 | return NULL; | |
803 | ||
7c798f87 | 804 | return devt_to_devpath(st.st_rdev, dev_level, buf); |
25921536 DW |
805 | } |
806 | ||
807 | int path_attached_to_hba(const char *disk_path, const char *hba_path) | |
808 | { | |
809 | int rc; | |
810 | ||
fc13853f LM |
811 | if (check_env("IMSM_TEST_AHCI_DEV") || |
812 | check_env("IMSM_TEST_SCU_DEV")) { | |
813 | return 1; | |
814 | } | |
815 | ||
25921536 DW |
816 | if (!disk_path || !hba_path) |
817 | return 0; | |
a8e5382a | 818 | dprintf("hba: %s - disk: %s\n", hba_path, disk_path); |
25921536 DW |
819 | if (strncmp(disk_path, hba_path, strlen(hba_path)) == 0) |
820 | rc = 1; | |
821 | else | |
822 | rc = 0; | |
823 | ||
824 | return rc; | |
825 | } | |
826 | ||
827 | int devt_attached_to_hba(dev_t dev, const char *hba_path) | |
828 | { | |
7c798f87 | 829 | char *disk_path = devt_to_devpath(dev, 1, NULL); |
25921536 DW |
830 | int rc = path_attached_to_hba(disk_path, hba_path); |
831 | ||
832 | if (disk_path) | |
833 | free(disk_path); | |
834 | ||
835 | return rc; | |
836 | } | |
837 | ||
838 | int disk_attached_to_hba(int fd, const char *hba_path) | |
839 | { | |
7c798f87 | 840 | char *disk_path = diskfd_to_devpath(fd, 1, NULL); |
25921536 DW |
841 | int rc = path_attached_to_hba(disk_path, hba_path); |
842 | ||
843 | if (disk_path) | |
844 | free(disk_path); | |
845 | ||
846 | return rc; | |
847 | } | |
60f0f54d PB |
848 | |
849 | char *vmd_domain_to_controller(struct sys_dev *hba, char *buf) | |
850 | { | |
851 | struct dirent *ent; | |
852 | DIR *dir; | |
853 | char path[PATH_MAX]; | |
854 | ||
855 | if (!hba) | |
856 | return NULL; | |
857 | ||
858 | if (hba->type != SYS_DEV_VMD) | |
859 | return NULL; | |
860 | ||
861 | dir = opendir("/sys/bus/pci/drivers/vmd"); | |
b9135011 JS |
862 | if (!dir) |
863 | return NULL; | |
60f0f54d | 864 | |
b9135011 | 865 | for (ent = readdir(dir); ent; ent = readdir(dir)) { |
60f0f54d PB |
866 | sprintf(path, "/sys/bus/pci/drivers/vmd/%s/domain/device", |
867 | ent->d_name); | |
868 | ||
869 | if (!realpath(path, buf)) | |
870 | continue; | |
871 | ||
872 | if (strncmp(buf, hba->path, strlen(buf)) == 0) { | |
873 | sprintf(path, "/sys/bus/pci/drivers/vmd/%s", ent->d_name); | |
b9135011 | 874 | closedir(dir); |
7071320a | 875 | return realpath(path, buf); |
60f0f54d PB |
876 | } |
877 | } | |
b9135011 JS |
878 | |
879 | closedir(dir); | |
60f0f54d PB |
880 | return NULL; |
881 | } | |
a8f3cfd5 MT |
882 | /* Verify that NVMe drive is supported by IMSM |
883 | * Returns: | |
884 | * 0 - not supported | |
885 | * 1 - supported | |
886 | */ | |
887 | int imsm_is_nvme_supported(int disk_fd, int verbose) | |
888 | { | |
889 | char nsid_path[PATH_MAX]; | |
890 | char buf[PATH_MAX]; | |
891 | struct stat stb; | |
892 | ||
893 | if (disk_fd < 0) | |
894 | return 0; | |
895 | ||
896 | if (fstat(disk_fd, &stb)) | |
897 | return 0; | |
898 | ||
899 | snprintf(nsid_path, PATH_MAX-1, "/sys/dev/block/%d:%d/nsid", | |
900 | major(stb.st_rdev), minor(stb.st_rdev)); | |
901 | ||
902 | if (load_sys(nsid_path, buf, sizeof(buf))) { | |
903 | pr_err("Cannot read %s, rejecting drive\n", nsid_path); | |
904 | return 0; | |
905 | } | |
906 | if (strtoll(buf, NULL, 10) != 1) { | |
907 | if (verbose) | |
908 | pr_err("Only first namespace is supported by IMSM, aborting\n"); | |
909 | return 0; | |
910 | } | |
911 | return 1; | |
912 | } | |
d835518b BK |
913 | |
914 | /* Verify if multipath is supported by NVMe controller | |
915 | * Returns: | |
916 | * 0 - not supported | |
917 | * 1 - supported | |
918 | */ | |
919 | int is_multipath_nvme(int disk_fd) | |
920 | { | |
d835518b | 921 | char ns_path[PATH_MAX]; |
d835518b | 922 | |
7c798f87 | 923 | if (!diskfd_to_devpath(disk_fd, 0, ns_path)) |
d835518b BK |
924 | return 0; |
925 | ||
926 | if (strncmp(ns_path, NVME_SUBSYS_PATH, strlen(NVME_SUBSYS_PATH)) == 0) | |
927 | return 1; | |
928 | ||
929 | return 0; | |
930 | } |