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