]>
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 | ||
33 | ||
9c747fa0 LM |
34 | static int devpath_to_ll(const char *dev_path, const char *entry, |
35 | unsigned long long *val); | |
36 | ||
fc13853f | 37 | static __u16 devpath_to_vendor(const char *dev_path); |
b390f610 DW |
38 | |
39 | void free_sys_dev(struct sys_dev **list) | |
40 | { | |
41 | while (*list) { | |
42 | struct sys_dev *next = (*list)->next; | |
43 | ||
44 | if ((*list)->path) | |
45 | free((*list)->path); | |
46 | free(*list); | |
47 | *list = next; | |
48 | } | |
49 | } | |
50 | ||
51 | struct sys_dev *find_driver_devices(const char *bus, const char *driver) | |
52 | { | |
53 | /* search sysfs for devices driven by 'driver' */ | |
5dbb8c8d | 54 | char path[292]; |
b390f610 DW |
55 | char link[256]; |
56 | char *c; | |
57 | DIR *driver_dir; | |
58 | struct dirent *de; | |
59 | struct sys_dev *head = NULL; | |
60 | struct sys_dev *list = NULL; | |
a8e5382a | 61 | enum sys_dev_type type; |
9c747fa0 | 62 | unsigned long long dev_id; |
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; | |
68 | else | |
69 | type = SYS_DEV_UNKNOWN; | |
b390f610 DW |
70 | |
71 | sprintf(path, "/sys/bus/%s/drivers/%s", bus, driver); | |
72 | driver_dir = opendir(path); | |
73 | if (!driver_dir) | |
74 | return NULL; | |
75 | for (de = readdir(driver_dir); de; de = readdir(driver_dir)) { | |
5a1920f2 AW |
76 | int n; |
77 | ||
b390f610 DW |
78 | /* is 'de' a device? check that the 'subsystem' link exists and |
79 | * that its target matches 'bus' | |
80 | */ | |
81 | sprintf(path, "/sys/bus/%s/drivers/%s/%s/subsystem", | |
82 | bus, driver, de->d_name); | |
5a1920f2 | 83 | n = readlink(path, link, sizeof(link)); |
f21e18ca | 84 | if (n < 0 || n >= (int)sizeof(link)) |
b390f610 | 85 | continue; |
5a1920f2 | 86 | link[n] = '\0'; |
b390f610 DW |
87 | c = strrchr(link, '/'); |
88 | if (!c) | |
89 | continue; | |
90 | if (strncmp(bus, c+1, strlen(bus)) != 0) | |
91 | continue; | |
92 | ||
a8e5382a LM |
93 | sprintf(path, "/sys/bus/%s/drivers/%s/%s", |
94 | bus, driver, de->d_name); | |
95 | ||
96 | /* if it's not Intel device skip it. */ | |
97 | if (devpath_to_vendor(path) != 0x8086) | |
98 | continue; | |
99 | ||
9c747fa0 LM |
100 | if (devpath_to_ll(path, "device", &dev_id) != 0) |
101 | continue; | |
102 | ||
b390f610 DW |
103 | /* start / add list entry */ |
104 | if (!head) { | |
105 | head = malloc(sizeof(*head)); | |
106 | list = head; | |
107 | } else { | |
108 | list->next = malloc(sizeof(*head)); | |
109 | list = list->next; | |
110 | } | |
111 | ||
112 | if (!list) { | |
113 | free_sys_dev(&head); | |
114 | break; | |
115 | } | |
116 | ||
9c747fa0 | 117 | list->dev_id = (__u16) dev_id; |
a8e5382a | 118 | list->type = type; |
b390f610 DW |
119 | list->path = canonicalize_file_name(path); |
120 | list->next = NULL; | |
a8e5382a LM |
121 | if ((list->pci_id = strrchr(list->path, '/')) != NULL) |
122 | list->pci_id++; | |
b390f610 | 123 | } |
2a17c77b | 124 | closedir(driver_dir); |
b390f610 DW |
125 | return head; |
126 | } | |
127 | ||
fc13853f | 128 | |
9c747fa0 LM |
129 | static struct sys_dev *intel_devices=NULL; |
130 | ||
131 | static enum sys_dev_type device_type_by_id(__u16 device_id) | |
132 | { | |
133 | struct sys_dev *iter; | |
134 | ||
135 | for(iter = intel_devices; iter != NULL; iter = iter->next) | |
136 | if (iter->dev_id == device_id) | |
137 | return iter->type; | |
138 | return SYS_DEV_UNKNOWN; | |
139 | } | |
140 | ||
141 | static int devpath_to_ll(const char *dev_path, const char *entry, unsigned long long *val) | |
142 | { | |
143 | char path[strlen(dev_path) + strlen(entry) + 2]; | |
144 | int fd; | |
145 | int n; | |
146 | ||
147 | sprintf(path, "%s/%s", dev_path, entry); | |
148 | ||
149 | fd = open(path, O_RDONLY); | |
150 | if (fd < 0) | |
151 | return -1; | |
152 | n = sysfs_fd_get_ll(fd, val); | |
153 | close(fd); | |
154 | return n; | |
155 | } | |
156 | ||
157 | ||
fc13853f | 158 | static __u16 devpath_to_vendor(const char *dev_path) |
b390f610 DW |
159 | { |
160 | char path[strlen(dev_path) + strlen("/vendor") + 1]; | |
161 | char vendor[7]; | |
162 | int fd; | |
163 | __u16 id = 0xffff; | |
164 | int n; | |
165 | ||
166 | sprintf(path, "%s/vendor", dev_path); | |
167 | ||
168 | fd = open(path, O_RDONLY); | |
169 | if (fd < 0) | |
170 | return 0xffff; | |
171 | ||
172 | n = read(fd, vendor, sizeof(vendor)); | |
173 | if (n == sizeof(vendor)) { | |
174 | vendor[n - 1] = '\0'; | |
175 | id = strtoul(vendor, NULL, 16); | |
176 | } | |
177 | close(fd); | |
178 | ||
179 | return id; | |
180 | } | |
181 | ||
a8e5382a | 182 | struct sys_dev *find_intel_devices(void) |
b390f610 | 183 | { |
a8e5382a LM |
184 | struct sys_dev *ahci, *isci; |
185 | ||
186 | isci = find_driver_devices("pci", "isci"); | |
187 | ahci = find_driver_devices("pci", "ahci"); | |
188 | ||
189 | if (!ahci) { | |
190 | ahci = isci; | |
191 | } else { | |
192 | struct sys_dev *elem = ahci; | |
193 | while (elem->next) | |
194 | elem = elem->next; | |
195 | elem->next = isci; | |
196 | } | |
197 | return ahci; | |
b390f610 DW |
198 | } |
199 | ||
3c8bfb5d | 200 | /* |
fc13853f | 201 | * PCI Expansion ROM Data Structure Format */ |
3c8bfb5d LM |
202 | struct pciExpDataStructFormat { |
203 | __u8 ver[4]; | |
204 | __u16 vendorID; | |
205 | __u16 deviceID; | |
206 | } __attribute__ ((packed)); | |
207 | ||
fc13853f LM |
208 | static struct imsm_orom imsm_orom[SYS_DEV_MAX]; |
209 | static int populated_orom[SYS_DEV_MAX]; | |
210 | ||
3c8bfb5d | 211 | static int scan(const void *start, const void *end, const void *data) |
b390f610 DW |
212 | { |
213 | int offset; | |
214 | const struct imsm_orom *imsm_mem; | |
fc13853f | 215 | int dev; |
b390f610 | 216 | int len = (end - start); |
3c8bfb5d LM |
217 | struct pciExpDataStructFormat *ptr= (struct pciExpDataStructFormat *)data; |
218 | ||
9c747fa0 LM |
219 | if (data + 0x18 > end) { |
220 | dprintf("cannot find pciExpDataStruct \n"); | |
221 | return 0; | |
222 | } | |
223 | ||
3c8bfb5d LM |
224 | dprintf("ptr->vendorID: %lx __le16_to_cpu(ptr->deviceID): %lx \n", |
225 | (ulong) __le16_to_cpu(ptr->vendorID), | |
226 | (ulong) __le16_to_cpu(ptr->deviceID)); | |
227 | ||
9c747fa0 LM |
228 | if (__le16_to_cpu(ptr->vendorID) == 0x8086) { |
229 | /* serach attached intel devices by device id from OROM */ | |
230 | dev = device_type_by_id(__le16_to_cpu(ptr->deviceID)); | |
231 | if (dev == SYS_DEV_UNKNOWN) | |
232 | return 0; | |
233 | } | |
fc13853f | 234 | else |
3c8bfb5d | 235 | return 0; |
b390f610 DW |
236 | |
237 | for (offset = 0; offset < len; offset += 4) { | |
238 | imsm_mem = start + offset; | |
d0bc5190 LM |
239 | if ((memcmp(imsm_mem->signature, "$VER", 4) == 0) || |
240 | (memcmp(imsm_mem->signature, "$OEM", 4) == 0)) { | |
fc13853f LM |
241 | imsm_orom[dev] = *imsm_mem; |
242 | populated_orom[dev] = 1; | |
243 | return populated_orom[SYS_DEV_SATA] && populated_orom[SYS_DEV_SAS]; | |
b390f610 DW |
244 | } |
245 | } | |
b390f610 DW |
246 | return 0; |
247 | } | |
248 | ||
b390f610 | 249 | |
fc13853f LM |
250 | const struct imsm_orom *imsm_platform_test(enum sys_dev_type hba_id, int *populated, |
251 | struct imsm_orom *imsm_orom) | |
252 | { | |
253 | memset(imsm_orom, 0, sizeof(*imsm_orom)); | |
254 | imsm_orom->rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | | |
cceebc67 | 255 | IMSM_OROM_RLC_RAID10 | IMSM_OROM_RLC_RAID5; |
fc13853f | 256 | imsm_orom->sss = IMSM_OROM_SSS_4kB | IMSM_OROM_SSS_8kB | |
cceebc67 DW |
257 | IMSM_OROM_SSS_16kB | IMSM_OROM_SSS_32kB | |
258 | IMSM_OROM_SSS_64kB | IMSM_OROM_SSS_128kB | | |
259 | IMSM_OROM_SSS_256kB | IMSM_OROM_SSS_512kB | | |
260 | IMSM_OROM_SSS_1MB | IMSM_OROM_SSS_2MB; | |
fc13853f LM |
261 | imsm_orom->dpa = IMSM_OROM_DISKS_PER_ARRAY; |
262 | imsm_orom->tds = IMSM_OROM_TOTAL_DISKS; | |
263 | imsm_orom->vpa = IMSM_OROM_VOLUMES_PER_ARRAY; | |
264 | imsm_orom->vphba = IMSM_OROM_VOLUMES_PER_HBA; | |
265 | imsm_orom->attr = imsm_orom->rlc | IMSM_OROM_ATTR_ChecksumVerify; | |
266 | *populated = 1; | |
267 | ||
268 | if (check_env("IMSM_TEST_OROM_NORAID5")) { | |
269 | imsm_orom->rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | | |
270 | IMSM_OROM_RLC_RAID10; | |
271 | } | |
272 | if (check_env("IMSM_TEST_AHCI_EFI_NORAID5") && (hba_id == SYS_DEV_SAS)) { | |
273 | imsm_orom->rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | | |
274 | IMSM_OROM_RLC_RAID10; | |
275 | } | |
276 | if (check_env("IMSM_TEST_SCU_EFI_NORAID5") && (hba_id == SYS_DEV_SATA)) { | |
277 | imsm_orom->rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | | |
278 | IMSM_OROM_RLC_RAID10; | |
cceebc67 DW |
279 | } |
280 | ||
fc13853f LM |
281 | return imsm_orom; |
282 | } | |
283 | ||
284 | ||
285 | ||
286 | static const struct imsm_orom *find_imsm_hba_orom(enum sys_dev_type hba_id) | |
287 | { | |
288 | unsigned long align; | |
289 | ||
290 | if (hba_id >= SYS_DEV_MAX) | |
291 | return NULL; | |
292 | ||
293 | /* it's static data so we only need to read it once */ | |
294 | if (populated_orom[hba_id]) { | |
295 | dprintf("OROM CAP: %p, pid: %d pop: %d\n", | |
296 | &imsm_orom[hba_id], (int) getpid(), populated_orom[hba_id]); | |
297 | return &imsm_orom[hba_id]; | |
298 | } | |
299 | if (check_env("IMSM_TEST_OROM")) { | |
300 | dprintf("OROM CAP: %p, pid: %d pop: %d\n", | |
301 | &imsm_orom[hba_id], (int) getpid(), populated_orom[hba_id]); | |
302 | return imsm_platform_test(hba_id, &populated_orom[hba_id], &imsm_orom[hba_id]); | |
303 | } | |
304 | /* return empty OROM capabilities in EFI test mode */ | |
305 | if (check_env("IMSM_TEST_AHCI_EFI") || | |
306 | check_env("IMSM_TEST_SCU_EFI")) | |
307 | return NULL; | |
308 | ||
9c747fa0 LM |
309 | |
310 | if (intel_devices != NULL) | |
311 | free_sys_dev(&intel_devices); | |
312 | ||
313 | intel_devices = find_intel_devices(); | |
314 | ||
315 | if (intel_devices == NULL) | |
b390f610 DW |
316 | return NULL; |
317 | ||
318 | /* scan option-rom memory looking for an imsm signature */ | |
969c2555 DW |
319 | if (check_env("IMSM_SAFE_OROM_SCAN")) |
320 | align = 2048; | |
321 | else | |
322 | align = 512; | |
323 | if (probe_roms_init(align) != 0) | |
b390f610 DW |
324 | return NULL; |
325 | probe_roms(); | |
9c747fa0 | 326 | /* ignore return value - True is returned if both adapater roms are found */ |
fc13853f | 327 | scan_adapter_roms(scan); |
b390f610 DW |
328 | probe_roms_exit(); |
329 | ||
9c747fa0 LM |
330 | if (intel_devices != NULL) |
331 | free_sys_dev(&intel_devices); | |
332 | intel_devices = NULL; | |
333 | ||
fc13853f LM |
334 | if (populated_orom[hba_id]) |
335 | return &imsm_orom[hba_id]; | |
336 | return NULL; | |
337 | } | |
338 | ||
1a901471 LM |
339 | #define GUID_STR_MAX 37 /* according to GUID format: |
340 | * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" */ | |
341 | ||
342 | #define EFI_GUID(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \ | |
343 | ((struct efi_guid) \ | |
344 | {{ (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) & 0xff, \ | |
345 | (b) & 0xff, ((b) >> 8) & 0xff, \ | |
346 | (c) & 0xff, ((c) >> 8) & 0xff, \ | |
347 | (d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) }}) | |
fc13853f | 348 | |
5a6baf66 LM |
349 | |
350 | #define SYS_EFI_VAR_PATH "/sys/firmware/efi/vars" | |
351 | #define SCU_PROP "RstScuV" | |
8a0bf4f3 | 352 | #define SCU_PROP_OEM "RstScuO" |
5a6baf66 LM |
353 | #define AHCI_PROP "RstSataV" |
354 | ||
355 | #define VENDOR_GUID \ | |
356 | EFI_GUID(0x193dfefa, 0xa445, 0x4302, 0x99, 0xd8, 0xef, 0x3a, 0xad, 0x1a, 0x04, 0xc6) | |
357 | ||
358 | int populated_efi[SYS_DEV_MAX] = { 0, 0 }; | |
359 | ||
360 | static struct imsm_orom imsm_efi[SYS_DEV_MAX]; | |
361 | ||
362 | const struct imsm_orom *find_imsm_efi(enum sys_dev_type hba_id) | |
363 | { | |
364 | int dfd=-1; | |
365 | char path[PATH_MAX]; | |
366 | char buf[GUID_STR_MAX]; | |
367 | int n; | |
368 | ||
369 | if (hba_id >= SYS_DEV_MAX) | |
370 | return NULL; | |
371 | ||
372 | dprintf("EFI CAP: %p, pid: %d pop: %d\n", | |
373 | &imsm_efi[hba_id], (int) getpid(), populated_efi[hba_id]); | |
374 | ||
375 | /* it's static data so we only need to read it once */ | |
376 | if (populated_efi[hba_id]) { | |
377 | dprintf("EFI CAP: %p, pid: %d pop: %d\n", | |
378 | &imsm_efi[hba_id], (int) getpid(), populated_efi[hba_id]); | |
379 | return &imsm_efi[hba_id]; | |
380 | } | |
381 | if (check_env("IMSM_TEST_AHCI_EFI") || | |
382 | check_env("IMSM_TEST_SCU_EFI")) { | |
383 | dprintf("OROM CAP: %p, pid: %d pop: %d\n", | |
384 | &imsm_efi[hba_id], (int) getpid(), populated_efi[hba_id]); | |
385 | return imsm_platform_test(hba_id, &populated_efi[hba_id], &imsm_efi[hba_id]); | |
386 | } | |
387 | /* OROM test is set, return that there is no EFI capabilities */ | |
388 | if (check_env("IMSM_TEST_OROM")) { | |
389 | return NULL; | |
390 | } | |
391 | if (hba_id == SYS_DEV_SAS) | |
392 | snprintf(path, PATH_MAX, "%s/%s-%s", SYS_EFI_VAR_PATH, SCU_PROP, guid_str(buf, VENDOR_GUID)); | |
393 | else | |
394 | snprintf(path, PATH_MAX, "%s/%s-%s", SYS_EFI_VAR_PATH, AHCI_PROP, guid_str(buf, VENDOR_GUID)); | |
395 | ||
396 | dprintf("EFI VAR: path=%s\n", path); | |
8a0bf4f3 LM |
397 | dfd = open(path, O_RDONLY); |
398 | if ((dfd < 0) && (hba_id == SYS_DEV_SAS)) { | |
399 | /* check OEM parameters */ | |
400 | snprintf(path, PATH_MAX, "%s/%s-%s", SYS_EFI_VAR_PATH, SCU_PROP_OEM, guid_str(buf, VENDOR_GUID)); | |
401 | dfd = open(path, O_RDONLY); | |
402 | dprintf("EFI VAR: path=%s\n", path); | |
403 | } | |
404 | if (dfd < 0) { | |
5a6baf66 LM |
405 | populated_efi[hba_id] = 0; |
406 | return NULL; | |
407 | } | |
408 | n = read(dfd, &imsm_efi[hba_id], sizeof(imsm_efi[0])); | |
409 | close(dfd); | |
410 | if (n < (int) (sizeof(imsm_efi[0]))) { | |
411 | return NULL; | |
412 | } | |
413 | populated_efi[hba_id] = 1; | |
414 | return &imsm_efi[hba_id]; | |
415 | } | |
416 | ||
fc13853f LM |
417 | /* |
418 | * backward interface compatibility | |
419 | */ | |
420 | const struct imsm_orom *find_imsm_orom(void) | |
421 | { | |
422 | return find_imsm_hba_orom(SYS_DEV_SATA); | |
423 | } | |
424 | ||
425 | const struct imsm_orom *find_imsm_capability(enum sys_dev_type hba_id) | |
426 | { | |
427 | const struct imsm_orom *cap=NULL; | |
428 | ||
5a6baf66 LM |
429 | |
430 | if ((cap = find_imsm_efi(hba_id)) != NULL) | |
431 | return cap; | |
fc13853f LM |
432 | if ((cap = find_imsm_hba_orom(hba_id)) != NULL) |
433 | return cap; | |
b390f610 DW |
434 | return NULL; |
435 | } | |
25921536 DW |
436 | |
437 | char *devt_to_devpath(dev_t dev) | |
438 | { | |
37f0e1e0 | 439 | char device[46]; |
25921536 DW |
440 | |
441 | sprintf(device, "/sys/dev/block/%d:%d/device", major(dev), minor(dev)); | |
442 | return canonicalize_file_name(device); | |
443 | } | |
444 | ||
a8e5382a | 445 | char *diskfd_to_devpath(int fd) |
25921536 DW |
446 | { |
447 | /* return the device path for a disk, return NULL on error or fd | |
448 | * refers to a partition | |
449 | */ | |
450 | struct stat st; | |
451 | ||
452 | if (fstat(fd, &st) != 0) | |
453 | return NULL; | |
454 | if (!S_ISBLK(st.st_mode)) | |
455 | return NULL; | |
456 | ||
457 | return devt_to_devpath(st.st_rdev); | |
458 | } | |
459 | ||
460 | int path_attached_to_hba(const char *disk_path, const char *hba_path) | |
461 | { | |
462 | int rc; | |
463 | ||
fc13853f LM |
464 | if (check_env("IMSM_TEST_AHCI_DEV") || |
465 | check_env("IMSM_TEST_SCU_DEV")) { | |
466 | return 1; | |
467 | } | |
468 | ||
25921536 DW |
469 | if (!disk_path || !hba_path) |
470 | return 0; | |
a8e5382a | 471 | dprintf("hba: %s - disk: %s\n", hba_path, disk_path); |
25921536 DW |
472 | if (strncmp(disk_path, hba_path, strlen(hba_path)) == 0) |
473 | rc = 1; | |
474 | else | |
475 | rc = 0; | |
476 | ||
477 | return rc; | |
478 | } | |
479 | ||
480 | int devt_attached_to_hba(dev_t dev, const char *hba_path) | |
481 | { | |
482 | char *disk_path = devt_to_devpath(dev); | |
483 | int rc = path_attached_to_hba(disk_path, hba_path); | |
484 | ||
485 | if (disk_path) | |
486 | free(disk_path); | |
487 | ||
488 | return rc; | |
489 | } | |
490 | ||
491 | int disk_attached_to_hba(int fd, const char *hba_path) | |
492 | { | |
493 | char *disk_path = diskfd_to_devpath(fd); | |
494 | int rc = path_attached_to_hba(disk_path, hba_path); | |
495 | ||
496 | if (disk_path) | |
497 | free(disk_path); | |
498 | ||
499 | return rc; | |
500 | } |