]>
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 | ||
9c747fa0 LM |
33 | static int devpath_to_ll(const char *dev_path, const char *entry, |
34 | unsigned long long *val); | |
35 | ||
fc13853f | 36 | static __u16 devpath_to_vendor(const char *dev_path); |
b390f610 | 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' */ | |
5dbb8c8d | 53 | char path[292]; |
b390f610 DW |
54 | char link[256]; |
55 | char *c; | |
56 | DIR *driver_dir; | |
57 | struct dirent *de; | |
58 | struct sys_dev *head = NULL; | |
59 | struct sys_dev *list = NULL; | |
a8e5382a | 60 | enum sys_dev_type type; |
9c747fa0 | 61 | unsigned long long dev_id; |
6b781d33 | 62 | unsigned long long class; |
a8e5382a LM |
63 | |
64 | if (strcmp(driver, "isci") == 0) | |
65 | type = SYS_DEV_SAS; | |
66 | else if (strcmp(driver, "ahci") == 0) | |
67 | type = SYS_DEV_SATA; | |
614902f6 PB |
68 | else if (strcmp(driver, "nvme") == 0) |
69 | type = SYS_DEV_NVME; | |
a8e5382a LM |
70 | else |
71 | type = SYS_DEV_UNKNOWN; | |
b390f610 DW |
72 | |
73 | sprintf(path, "/sys/bus/%s/drivers/%s", bus, driver); | |
74 | driver_dir = opendir(path); | |
75 | if (!driver_dir) | |
76 | return NULL; | |
77 | for (de = readdir(driver_dir); de; de = readdir(driver_dir)) { | |
5a1920f2 AW |
78 | int n; |
79 | ||
b390f610 DW |
80 | /* is 'de' a device? check that the 'subsystem' link exists and |
81 | * that its target matches 'bus' | |
82 | */ | |
83 | sprintf(path, "/sys/bus/%s/drivers/%s/%s/subsystem", | |
84 | bus, driver, de->d_name); | |
5a1920f2 | 85 | n = readlink(path, link, sizeof(link)); |
f21e18ca | 86 | if (n < 0 || n >= (int)sizeof(link)) |
b390f610 | 87 | continue; |
5a1920f2 | 88 | link[n] = '\0'; |
b390f610 DW |
89 | c = strrchr(link, '/'); |
90 | if (!c) | |
91 | continue; | |
92 | if (strncmp(bus, c+1, strlen(bus)) != 0) | |
93 | continue; | |
94 | ||
a8e5382a LM |
95 | sprintf(path, "/sys/bus/%s/drivers/%s/%s", |
96 | bus, driver, de->d_name); | |
97 | ||
98 | /* if it's not Intel device skip it. */ | |
99 | if (devpath_to_vendor(path) != 0x8086) | |
100 | continue; | |
101 | ||
9c747fa0 LM |
102 | if (devpath_to_ll(path, "device", &dev_id) != 0) |
103 | continue; | |
104 | ||
6b781d33 AP |
105 | if (devpath_to_ll(path, "class", &class) != 0) |
106 | continue; | |
107 | ||
b390f610 DW |
108 | /* start / add list entry */ |
109 | if (!head) { | |
503975b9 | 110 | head = xmalloc(sizeof(*head)); |
b390f610 DW |
111 | list = head; |
112 | } else { | |
503975b9 | 113 | list->next = xmalloc(sizeof(*head)); |
b390f610 DW |
114 | list = list->next; |
115 | } | |
116 | ||
117 | if (!list) { | |
118 | free_sys_dev(&head); | |
119 | break; | |
120 | } | |
121 | ||
9c747fa0 | 122 | list->dev_id = (__u16) dev_id; |
6b781d33 | 123 | list->class = (__u32) class; |
a8e5382a | 124 | list->type = type; |
e3635eee | 125 | list->path = realpath(path, NULL); |
b390f610 | 126 | list->next = NULL; |
a8e5382a LM |
127 | if ((list->pci_id = strrchr(list->path, '/')) != NULL) |
128 | list->pci_id++; | |
b390f610 | 129 | } |
2a17c77b | 130 | closedir(driver_dir); |
b390f610 DW |
131 | return head; |
132 | } | |
133 | ||
9c747fa0 | 134 | static struct sys_dev *intel_devices=NULL; |
9bc4ae77 | 135 | static time_t valid_time = 0; |
9c747fa0 | 136 | |
9c747fa0 LM |
137 | static int devpath_to_ll(const char *dev_path, const char *entry, unsigned long long *val) |
138 | { | |
139 | char path[strlen(dev_path) + strlen(entry) + 2]; | |
140 | int fd; | |
141 | int n; | |
142 | ||
143 | sprintf(path, "%s/%s", dev_path, entry); | |
144 | ||
145 | fd = open(path, O_RDONLY); | |
146 | if (fd < 0) | |
147 | return -1; | |
148 | n = sysfs_fd_get_ll(fd, val); | |
149 | close(fd); | |
150 | return n; | |
151 | } | |
152 | ||
fc13853f | 153 | static __u16 devpath_to_vendor(const char *dev_path) |
b390f610 DW |
154 | { |
155 | char path[strlen(dev_path) + strlen("/vendor") + 1]; | |
156 | char vendor[7]; | |
157 | int fd; | |
158 | __u16 id = 0xffff; | |
159 | int n; | |
160 | ||
161 | sprintf(path, "%s/vendor", dev_path); | |
162 | ||
163 | fd = open(path, O_RDONLY); | |
164 | if (fd < 0) | |
165 | return 0xffff; | |
166 | ||
167 | n = read(fd, vendor, sizeof(vendor)); | |
168 | if (n == sizeof(vendor)) { | |
169 | vendor[n - 1] = '\0'; | |
170 | id = strtoul(vendor, NULL, 16); | |
171 | } | |
172 | close(fd); | |
173 | ||
174 | return id; | |
175 | } | |
176 | ||
a8e5382a | 177 | struct sys_dev *find_intel_devices(void) |
b390f610 | 178 | { |
614902f6 | 179 | struct sys_dev *ahci, *isci, *nvme; |
a8e5382a | 180 | |
9bc4ae77 N |
181 | if (valid_time > time(0) - 10) |
182 | return intel_devices; | |
183 | ||
184 | if (intel_devices) | |
185 | free_sys_dev(&intel_devices); | |
186 | ||
a8e5382a LM |
187 | isci = find_driver_devices("pci", "isci"); |
188 | ahci = find_driver_devices("pci", "ahci"); | |
614902f6 | 189 | nvme = find_driver_devices("pci", "nvme"); |
a8e5382a | 190 | |
614902f6 PB |
191 | if (!isci && !ahci) { |
192 | ahci = nvme; | |
193 | } else if (!ahci) { | |
a8e5382a | 194 | ahci = isci; |
614902f6 PB |
195 | struct sys_dev *elem = ahci; |
196 | while (elem->next) | |
197 | elem = elem->next; | |
198 | elem->next = nvme; | |
a8e5382a LM |
199 | } else { |
200 | struct sys_dev *elem = ahci; | |
201 | while (elem->next) | |
202 | elem = elem->next; | |
203 | elem->next = isci; | |
614902f6 PB |
204 | while (elem->next) |
205 | elem = elem->next; | |
206 | elem->next = nvme; | |
a8e5382a | 207 | } |
9bc4ae77 N |
208 | intel_devices = ahci; |
209 | valid_time = time(0); | |
210 | return intel_devices; | |
b390f610 DW |
211 | } |
212 | ||
3c8bfb5d | 213 | /* |
fc13853f | 214 | * PCI Expansion ROM Data Structure Format */ |
3c8bfb5d LM |
215 | struct pciExpDataStructFormat { |
216 | __u8 ver[4]; | |
217 | __u16 vendorID; | |
218 | __u16 deviceID; | |
6b781d33 | 219 | __u16 devListOffset; |
3c8bfb5d LM |
220 | } __attribute__ ((packed)); |
221 | ||
6b781d33 AP |
222 | struct devid_list { |
223 | __u16 devid; | |
224 | struct devid_list *next; | |
225 | }; | |
226 | ||
227 | struct orom_entry { | |
228 | struct imsm_orom orom; | |
229 | struct devid_list *devid_list; | |
230 | }; | |
231 | ||
232 | static struct orom_entry oroms[SYS_DEV_MAX]; | |
233 | ||
234 | const struct imsm_orom *get_orom_by_device_id(__u16 dev_id) | |
235 | { | |
236 | int i; | |
237 | struct devid_list *list; | |
238 | ||
239 | for (i = 0; i < SYS_DEV_MAX; i++) { | |
240 | for (list = oroms[i].devid_list; list; list = list->next) { | |
241 | if (list->devid == dev_id) | |
242 | return &oroms[i].orom; | |
243 | } | |
244 | } | |
245 | return NULL; | |
246 | } | |
247 | ||
248 | static const struct imsm_orom *add_orom(const struct imsm_orom *orom) | |
249 | { | |
250 | int i; | |
251 | ||
252 | for (i = 0; i < SYS_DEV_MAX; i++) { | |
253 | if (&oroms[i].orom == orom) | |
254 | return orom; | |
255 | if (oroms[i].orom.signature[0] == 0) { | |
256 | oroms[i].orom = *orom; | |
257 | return &oroms[i].orom; | |
258 | } | |
259 | } | |
260 | return NULL; | |
261 | } | |
262 | ||
263 | static void add_orom_device_id(const struct imsm_orom *orom, __u16 dev_id) | |
264 | { | |
265 | int i; | |
266 | struct devid_list *list; | |
267 | struct devid_list *prev = NULL; | |
268 | ||
269 | for (i = 0; i < SYS_DEV_MAX; i++) { | |
270 | if (&oroms[i].orom == orom) { | |
271 | for (list = oroms[i].devid_list; list; prev = list, list = list->next) { | |
272 | if (list->devid == dev_id) | |
273 | return; | |
274 | } | |
275 | list = xmalloc(sizeof(struct devid_list)); | |
276 | list->devid = dev_id; | |
277 | list->next = NULL; | |
278 | ||
279 | if (prev == NULL) | |
280 | oroms[i].devid_list = list; | |
281 | else | |
282 | prev->next = list; | |
283 | return; | |
284 | } | |
285 | } | |
286 | } | |
fc13853f | 287 | |
3c8bfb5d | 288 | static int scan(const void *start, const void *end, const void *data) |
b390f610 DW |
289 | { |
290 | int offset; | |
6b781d33 | 291 | const struct imsm_orom *imsm_mem = NULL; |
b390f610 | 292 | int len = (end - start); |
3c8bfb5d LM |
293 | struct pciExpDataStructFormat *ptr= (struct pciExpDataStructFormat *)data; |
294 | ||
9c747fa0 LM |
295 | if (data + 0x18 > end) { |
296 | dprintf("cannot find pciExpDataStruct \n"); | |
297 | return 0; | |
298 | } | |
299 | ||
3c8bfb5d LM |
300 | dprintf("ptr->vendorID: %lx __le16_to_cpu(ptr->deviceID): %lx \n", |
301 | (ulong) __le16_to_cpu(ptr->vendorID), | |
302 | (ulong) __le16_to_cpu(ptr->deviceID)); | |
303 | ||
6b781d33 | 304 | if (__le16_to_cpu(ptr->vendorID) != 0x8086) |
3c8bfb5d | 305 | return 0; |
b390f610 DW |
306 | |
307 | for (offset = 0; offset < len; offset += 4) { | |
6b781d33 AP |
308 | const void *mem = start + offset; |
309 | ||
310 | if ((memcmp(mem, IMSM_OROM_SIGNATURE, 4) == 0)) { | |
311 | imsm_mem = mem; | |
312 | break; | |
b390f610 DW |
313 | } |
314 | } | |
6b781d33 AP |
315 | |
316 | if (!imsm_mem) | |
317 | return 0; | |
318 | ||
319 | const struct imsm_orom *orom = add_orom(imsm_mem); | |
320 | ||
321 | if (ptr->devListOffset) { | |
322 | const __u16 *dev_list = (void *)ptr + ptr->devListOffset; | |
323 | int i; | |
324 | ||
325 | for (i = 0; dev_list[i] != 0; i++) | |
326 | add_orom_device_id(orom, dev_list[i]); | |
327 | } else { | |
328 | add_orom_device_id(orom, __le16_to_cpu(ptr->deviceID)); | |
329 | } | |
330 | ||
b390f610 DW |
331 | return 0; |
332 | } | |
333 | ||
6b781d33 | 334 | const struct imsm_orom *imsm_platform_test(struct sys_dev *hba) |
fc13853f | 335 | { |
6b781d33 AP |
336 | struct imsm_orom orom = { |
337 | .signature = IMSM_OROM_SIGNATURE, | |
338 | .rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | | |
339 | IMSM_OROM_RLC_RAID10 | IMSM_OROM_RLC_RAID5, | |
340 | .sss = IMSM_OROM_SSS_4kB | IMSM_OROM_SSS_8kB | | |
341 | IMSM_OROM_SSS_16kB | IMSM_OROM_SSS_32kB | | |
342 | IMSM_OROM_SSS_64kB | IMSM_OROM_SSS_128kB | | |
343 | IMSM_OROM_SSS_256kB | IMSM_OROM_SSS_512kB | | |
344 | IMSM_OROM_SSS_1MB | IMSM_OROM_SSS_2MB, | |
345 | .dpa = IMSM_OROM_DISKS_PER_ARRAY, | |
346 | .tds = IMSM_OROM_TOTAL_DISKS, | |
347 | .vpa = IMSM_OROM_VOLUMES_PER_ARRAY, | |
348 | .vphba = IMSM_OROM_VOLUMES_PER_HBA | |
349 | }; | |
350 | orom.attr = orom.rlc | IMSM_OROM_ATTR_ChecksumVerify; | |
fc13853f LM |
351 | |
352 | if (check_env("IMSM_TEST_OROM_NORAID5")) { | |
6b781d33 | 353 | orom.rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | |
fc13853f LM |
354 | IMSM_OROM_RLC_RAID10; |
355 | } | |
6b781d33 AP |
356 | if (check_env("IMSM_TEST_AHCI_EFI_NORAID5") && (hba->type == SYS_DEV_SAS)) { |
357 | orom.rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | | |
fc13853f LM |
358 | IMSM_OROM_RLC_RAID10; |
359 | } | |
6b781d33 AP |
360 | if (check_env("IMSM_TEST_SCU_EFI_NORAID5") && (hba->type == SYS_DEV_SATA)) { |
361 | orom.rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | | |
fc13853f | 362 | IMSM_OROM_RLC_RAID10; |
cceebc67 DW |
363 | } |
364 | ||
6b781d33 AP |
365 | const struct imsm_orom *ret = add_orom(&orom); |
366 | ||
367 | add_orom_device_id(ret, hba->dev_id); | |
368 | ||
369 | return ret; | |
fc13853f LM |
370 | } |
371 | ||
6b781d33 | 372 | static const struct imsm_orom *find_imsm_hba_orom(struct sys_dev *hba) |
fc13853f LM |
373 | { |
374 | unsigned long align; | |
375 | ||
6b781d33 AP |
376 | if (check_env("IMSM_TEST_OROM")) |
377 | return imsm_platform_test(hba); | |
fc13853f | 378 | |
fc13853f | 379 | /* return empty OROM capabilities in EFI test mode */ |
6b781d33 | 380 | if (check_env("IMSM_TEST_AHCI_EFI") || check_env("IMSM_TEST_SCU_EFI")) |
fc13853f LM |
381 | return NULL; |
382 | ||
9bc4ae77 | 383 | find_intel_devices(); |
9c747fa0 LM |
384 | |
385 | if (intel_devices == NULL) | |
b390f610 DW |
386 | return NULL; |
387 | ||
388 | /* scan option-rom memory looking for an imsm signature */ | |
969c2555 DW |
389 | if (check_env("IMSM_SAFE_OROM_SCAN")) |
390 | align = 2048; | |
391 | else | |
392 | align = 512; | |
393 | if (probe_roms_init(align) != 0) | |
b390f610 DW |
394 | return NULL; |
395 | probe_roms(); | |
9c747fa0 | 396 | /* ignore return value - True is returned if both adapater roms are found */ |
fc13853f | 397 | scan_adapter_roms(scan); |
b390f610 DW |
398 | probe_roms_exit(); |
399 | ||
6b781d33 | 400 | return get_orom_by_device_id(hba->dev_id); |
fc13853f LM |
401 | } |
402 | ||
1a901471 LM |
403 | #define GUID_STR_MAX 37 /* according to GUID format: |
404 | * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" */ | |
405 | ||
406 | #define EFI_GUID(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \ | |
407 | ((struct efi_guid) \ | |
408 | {{ (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) & 0xff, \ | |
409 | (b) & 0xff, ((b) >> 8) & 0xff, \ | |
410 | (c) & 0xff, ((c) >> 8) & 0xff, \ | |
411 | (d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) }}) | |
fc13853f | 412 | |
5a6baf66 LM |
413 | #define SYS_EFI_VAR_PATH "/sys/firmware/efi/vars" |
414 | #define SCU_PROP "RstScuV" | |
415 | #define AHCI_PROP "RstSataV" | |
81188ef8 AP |
416 | #define AHCI_SSATA_PROP "RstsSatV" |
417 | #define AHCI_CSATA_PROP "RstCSatV" | |
5a6baf66 LM |
418 | |
419 | #define VENDOR_GUID \ | |
420 | EFI_GUID(0x193dfefa, 0xa445, 0x4302, 0x99, 0xd8, 0xef, 0x3a, 0xad, 0x1a, 0x04, 0xc6) | |
421 | ||
6b781d33 | 422 | #define PCI_CLASS_RAID_CNTRL 0x010400 |
5a6baf66 | 423 | |
aae4c111 | 424 | int read_efi_variable(void *buffer, ssize_t buf_size, char *variable_name, struct efi_guid guid) |
5a6baf66 | 425 | { |
5a6baf66 LM |
426 | char path[PATH_MAX]; |
427 | char buf[GUID_STR_MAX]; | |
aae4c111 HCP |
428 | int dfd; |
429 | ssize_t n, var_data_len; | |
430 | ||
431 | snprintf(path, PATH_MAX, "%s/%s-%s/size", SYS_EFI_VAR_PATH, variable_name, guid_str(buf, guid)); | |
432 | ||
433 | dprintf("EFI VAR: path=%s\n", path); | |
434 | /* get size of variable data */ | |
435 | dfd = open(path, O_RDONLY); | |
436 | if (dfd < 0) | |
437 | return 1; | |
438 | ||
439 | n = read(dfd, &buf, sizeof(buf)); | |
440 | close(dfd); | |
441 | if (n < 0) | |
442 | return 1; | |
443 | buf[n] = '\0'; | |
444 | ||
445 | errno = 0; | |
446 | var_data_len = strtoul(buf, NULL, 16); | |
447 | if ((errno == ERANGE && (var_data_len == LONG_MAX)) | |
5d500228 | 448 | || (errno != 0 && var_data_len == 0)) |
aae4c111 HCP |
449 | return 1; |
450 | ||
451 | /* get data */ | |
452 | snprintf(path, PATH_MAX, "%s/%s-%s/data", SYS_EFI_VAR_PATH, variable_name, guid_str(buf, guid)); | |
453 | ||
454 | dprintf("EFI VAR: path=%s\n", path); | |
455 | dfd = open(path, O_RDONLY); | |
456 | if (dfd < 0) | |
457 | return 1; | |
458 | ||
459 | n = read(dfd, buffer, buf_size); | |
460 | close(dfd); | |
461 | if (n != var_data_len || n < buf_size) { | |
462 | return 1; | |
463 | } | |
464 | ||
465 | return 0; | |
466 | } | |
5a6baf66 | 467 | |
6b781d33 | 468 | const struct imsm_orom *find_imsm_efi(struct sys_dev *hba) |
aae4c111 | 469 | { |
6b781d33 AP |
470 | struct imsm_orom orom; |
471 | const struct imsm_orom *ret; | |
81188ef8 | 472 | int err; |
5a6baf66 | 473 | |
6b781d33 AP |
474 | if (check_env("IMSM_TEST_AHCI_EFI") || check_env("IMSM_TEST_SCU_EFI")) |
475 | return imsm_platform_test(hba); | |
5a6baf66 | 476 | |
5a6baf66 | 477 | /* OROM test is set, return that there is no EFI capabilities */ |
aae4c111 | 478 | if (check_env("IMSM_TEST_OROM")) |
5a6baf66 | 479 | return NULL; |
5a6baf66 | 480 | |
6b781d33 | 481 | if (hba->type == SYS_DEV_SATA && hba->class != PCI_CLASS_RAID_CNTRL) |
5a6baf66 | 482 | return NULL; |
aae4c111 | 483 | |
81188ef8 AP |
484 | err = read_efi_variable(&orom, sizeof(orom), hba->type == SYS_DEV_SAS ? SCU_PROP : AHCI_PROP, VENDOR_GUID); |
485 | ||
486 | /* try to read variable for second AHCI controller */ | |
487 | if (err && hba->type == SYS_DEV_SATA) | |
488 | err = read_efi_variable(&orom, sizeof(orom), AHCI_SSATA_PROP, VENDOR_GUID); | |
489 | ||
490 | /* try to read variable for combined AHCI controllers */ | |
491 | if (err && hba->type == SYS_DEV_SATA) { | |
492 | static const struct imsm_orom *csata; | |
493 | ||
494 | err = read_efi_variable(&orom, sizeof(orom), AHCI_CSATA_PROP, VENDOR_GUID); | |
495 | if (!err) { | |
496 | if (!csata) | |
497 | csata = add_orom(&orom); | |
498 | add_orom_device_id(csata, hba->dev_id); | |
499 | return csata; | |
500 | } | |
501 | } | |
502 | ||
503 | if (err) | |
6b781d33 | 504 | return NULL; |
5a6baf66 | 505 | |
6b781d33 AP |
506 | ret = add_orom(&orom); |
507 | add_orom_device_id(ret, hba->dev_id); | |
508 | ||
509 | return ret; | |
fc13853f LM |
510 | } |
511 | ||
614902f6 PB |
512 | const struct imsm_orom *find_imsm_nvme(struct sys_dev *hba) |
513 | { | |
514 | static const struct imsm_orom *nvme_orom; | |
515 | ||
516 | if (hba->type != SYS_DEV_NVME) | |
517 | return NULL; | |
518 | ||
519 | if (!nvme_orom) { | |
520 | struct imsm_orom nvme_orom_compat = { | |
521 | .signature = IMSM_NVME_OROM_COMPAT_SIGNATURE, | |
522 | .rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | | |
523 | IMSM_OROM_RLC_RAID10 | IMSM_OROM_RLC_RAID5, | |
524 | .sss = IMSM_OROM_SSS_4kB | IMSM_OROM_SSS_8kB | | |
525 | IMSM_OROM_SSS_16kB | IMSM_OROM_SSS_32kB | | |
526 | IMSM_OROM_SSS_64kB | IMSM_OROM_SSS_128kB, | |
527 | .dpa = IMSM_OROM_DISKS_PER_ARRAY_NVME, | |
528 | .tds = IMSM_OROM_TOTAL_DISKS_NVME, | |
529 | .vpa = IMSM_OROM_VOLUMES_PER_ARRAY, | |
530 | .vphba = IMSM_OROM_TOTAL_DISKS_NVME / 2 * IMSM_OROM_VOLUMES_PER_ARRAY, | |
531 | .attr = IMSM_OROM_ATTR_2TB | IMSM_OROM_ATTR_2TB_DISK, | |
532 | }; | |
533 | nvme_orom = add_orom(&nvme_orom_compat); | |
534 | } | |
535 | add_orom_device_id(nvme_orom, hba->dev_id); | |
536 | return nvme_orom; | |
537 | } | |
538 | ||
6b781d33 | 539 | const struct imsm_orom *find_imsm_capability(struct sys_dev *hba) |
fc13853f | 540 | { |
6b781d33 AP |
541 | const struct imsm_orom *cap = get_orom_by_device_id(hba->dev_id); |
542 | ||
543 | if (cap) | |
544 | return cap; | |
fc13853f | 545 | |
614902f6 PB |
546 | if (hba->type == SYS_DEV_NVME) |
547 | return find_imsm_nvme(hba); | |
6b781d33 | 548 | if ((cap = find_imsm_efi(hba)) != NULL) |
5a6baf66 | 549 | return cap; |
6b781d33 | 550 | if ((cap = find_imsm_hba_orom(hba)) != NULL) |
fc13853f | 551 | return cap; |
614902f6 | 552 | |
b390f610 DW |
553 | return NULL; |
554 | } | |
25921536 DW |
555 | |
556 | char *devt_to_devpath(dev_t dev) | |
557 | { | |
37f0e1e0 | 558 | char device[46]; |
25921536 DW |
559 | |
560 | sprintf(device, "/sys/dev/block/%d:%d/device", major(dev), minor(dev)); | |
e3635eee | 561 | return realpath(device, NULL); |
25921536 DW |
562 | } |
563 | ||
a8e5382a | 564 | char *diskfd_to_devpath(int fd) |
25921536 DW |
565 | { |
566 | /* return the device path for a disk, return NULL on error or fd | |
567 | * refers to a partition | |
568 | */ | |
569 | struct stat st; | |
570 | ||
571 | if (fstat(fd, &st) != 0) | |
572 | return NULL; | |
573 | if (!S_ISBLK(st.st_mode)) | |
574 | return NULL; | |
575 | ||
576 | return devt_to_devpath(st.st_rdev); | |
577 | } | |
578 | ||
579 | int path_attached_to_hba(const char *disk_path, const char *hba_path) | |
580 | { | |
581 | int rc; | |
582 | ||
fc13853f LM |
583 | if (check_env("IMSM_TEST_AHCI_DEV") || |
584 | check_env("IMSM_TEST_SCU_DEV")) { | |
585 | return 1; | |
586 | } | |
587 | ||
25921536 DW |
588 | if (!disk_path || !hba_path) |
589 | return 0; | |
a8e5382a | 590 | dprintf("hba: %s - disk: %s\n", hba_path, disk_path); |
25921536 DW |
591 | if (strncmp(disk_path, hba_path, strlen(hba_path)) == 0) |
592 | rc = 1; | |
593 | else | |
594 | rc = 0; | |
595 | ||
596 | return rc; | |
597 | } | |
598 | ||
599 | int devt_attached_to_hba(dev_t dev, const char *hba_path) | |
600 | { | |
601 | char *disk_path = devt_to_devpath(dev); | |
602 | int rc = path_attached_to_hba(disk_path, hba_path); | |
603 | ||
604 | if (disk_path) | |
605 | free(disk_path); | |
606 | ||
607 | return rc; | |
608 | } | |
609 | ||
610 | int disk_attached_to_hba(int fd, const char *hba_path) | |
611 | { | |
612 | char *disk_path = diskfd_to_devpath(fd); | |
613 | int rc = path_attached_to_hba(disk_path, hba_path); | |
614 | ||
615 | if (disk_path) | |
616 | free(disk_path); | |
617 | ||
618 | return rc; | |
619 | } |