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