]>
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; | |
208 | if (memcmp(imsm_mem->signature, "$VER", 4) == 0) { | |
fc13853f LM |
209 | imsm_orom[dev] = *imsm_mem; |
210 | populated_orom[dev] = 1; | |
211 | return populated_orom[SYS_DEV_SATA] && populated_orom[SYS_DEV_SAS]; | |
b390f610 DW |
212 | } |
213 | } | |
b390f610 DW |
214 | return 0; |
215 | } | |
216 | ||
b390f610 | 217 | |
fc13853f LM |
218 | const struct imsm_orom *imsm_platform_test(enum sys_dev_type hba_id, int *populated, |
219 | struct imsm_orom *imsm_orom) | |
220 | { | |
221 | memset(imsm_orom, 0, sizeof(*imsm_orom)); | |
222 | imsm_orom->rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | | |
cceebc67 | 223 | IMSM_OROM_RLC_RAID10 | IMSM_OROM_RLC_RAID5; |
fc13853f | 224 | imsm_orom->sss = IMSM_OROM_SSS_4kB | IMSM_OROM_SSS_8kB | |
cceebc67 DW |
225 | IMSM_OROM_SSS_16kB | IMSM_OROM_SSS_32kB | |
226 | IMSM_OROM_SSS_64kB | IMSM_OROM_SSS_128kB | | |
227 | IMSM_OROM_SSS_256kB | IMSM_OROM_SSS_512kB | | |
228 | IMSM_OROM_SSS_1MB | IMSM_OROM_SSS_2MB; | |
fc13853f LM |
229 | imsm_orom->dpa = IMSM_OROM_DISKS_PER_ARRAY; |
230 | imsm_orom->tds = IMSM_OROM_TOTAL_DISKS; | |
231 | imsm_orom->vpa = IMSM_OROM_VOLUMES_PER_ARRAY; | |
232 | imsm_orom->vphba = IMSM_OROM_VOLUMES_PER_HBA; | |
233 | imsm_orom->attr = imsm_orom->rlc | IMSM_OROM_ATTR_ChecksumVerify; | |
234 | *populated = 1; | |
235 | ||
236 | if (check_env("IMSM_TEST_OROM_NORAID5")) { | |
237 | imsm_orom->rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | | |
238 | IMSM_OROM_RLC_RAID10; | |
239 | } | |
240 | if (check_env("IMSM_TEST_AHCI_EFI_NORAID5") && (hba_id == SYS_DEV_SAS)) { | |
241 | imsm_orom->rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | | |
242 | IMSM_OROM_RLC_RAID10; | |
243 | } | |
244 | if (check_env("IMSM_TEST_SCU_EFI_NORAID5") && (hba_id == SYS_DEV_SATA)) { | |
245 | imsm_orom->rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 | | |
246 | IMSM_OROM_RLC_RAID10; | |
cceebc67 DW |
247 | } |
248 | ||
fc13853f LM |
249 | return imsm_orom; |
250 | } | |
251 | ||
252 | ||
253 | ||
254 | static const struct imsm_orom *find_imsm_hba_orom(enum sys_dev_type hba_id) | |
255 | { | |
256 | unsigned long align; | |
257 | ||
258 | if (hba_id >= SYS_DEV_MAX) | |
259 | return NULL; | |
260 | ||
261 | /* it's static data so we only need to read it once */ | |
262 | if (populated_orom[hba_id]) { | |
263 | dprintf("OROM CAP: %p, pid: %d pop: %d\n", | |
264 | &imsm_orom[hba_id], (int) getpid(), populated_orom[hba_id]); | |
265 | return &imsm_orom[hba_id]; | |
266 | } | |
267 | if (check_env("IMSM_TEST_OROM")) { | |
268 | dprintf("OROM CAP: %p, pid: %d pop: %d\n", | |
269 | &imsm_orom[hba_id], (int) getpid(), populated_orom[hba_id]); | |
270 | return imsm_platform_test(hba_id, &populated_orom[hba_id], &imsm_orom[hba_id]); | |
271 | } | |
272 | /* return empty OROM capabilities in EFI test mode */ | |
273 | if (check_env("IMSM_TEST_AHCI_EFI") || | |
274 | check_env("IMSM_TEST_SCU_EFI")) | |
275 | return NULL; | |
276 | ||
a8e5382a | 277 | if (!platform_has_intel_devices()) |
b390f610 DW |
278 | return NULL; |
279 | ||
280 | /* scan option-rom memory looking for an imsm signature */ | |
969c2555 DW |
281 | if (check_env("IMSM_SAFE_OROM_SCAN")) |
282 | align = 2048; | |
283 | else | |
284 | align = 512; | |
285 | if (probe_roms_init(align) != 0) | |
b390f610 DW |
286 | return NULL; |
287 | probe_roms(); | |
fc13853f LM |
288 | /* ignore result - True is returned if both are found */ |
289 | scan_adapter_roms(scan); | |
b390f610 DW |
290 | probe_roms_exit(); |
291 | ||
fc13853f LM |
292 | if (populated_orom[hba_id]) |
293 | return &imsm_orom[hba_id]; | |
294 | return NULL; | |
295 | } | |
296 | ||
1a901471 LM |
297 | #define GUID_STR_MAX 37 /* according to GUID format: |
298 | * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" */ | |
299 | ||
300 | #define EFI_GUID(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \ | |
301 | ((struct efi_guid) \ | |
302 | {{ (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) & 0xff, \ | |
303 | (b) & 0xff, ((b) >> 8) & 0xff, \ | |
304 | (c) & 0xff, ((c) >> 8) & 0xff, \ | |
305 | (d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) }}) | |
fc13853f LM |
306 | |
307 | /* | |
308 | * backward interface compatibility | |
309 | */ | |
310 | const struct imsm_orom *find_imsm_orom(void) | |
311 | { | |
312 | return find_imsm_hba_orom(SYS_DEV_SATA); | |
313 | } | |
314 | ||
315 | const struct imsm_orom *find_imsm_capability(enum sys_dev_type hba_id) | |
316 | { | |
317 | const struct imsm_orom *cap=NULL; | |
318 | ||
319 | if ((cap = find_imsm_hba_orom(hba_id)) != NULL) | |
320 | return cap; | |
b390f610 DW |
321 | return NULL; |
322 | } | |
25921536 DW |
323 | |
324 | char *devt_to_devpath(dev_t dev) | |
325 | { | |
37f0e1e0 | 326 | char device[46]; |
25921536 DW |
327 | |
328 | sprintf(device, "/sys/dev/block/%d:%d/device", major(dev), minor(dev)); | |
329 | return canonicalize_file_name(device); | |
330 | } | |
331 | ||
a8e5382a | 332 | char *diskfd_to_devpath(int fd) |
25921536 DW |
333 | { |
334 | /* return the device path for a disk, return NULL on error or fd | |
335 | * refers to a partition | |
336 | */ | |
337 | struct stat st; | |
338 | ||
339 | if (fstat(fd, &st) != 0) | |
340 | return NULL; | |
341 | if (!S_ISBLK(st.st_mode)) | |
342 | return NULL; | |
343 | ||
344 | return devt_to_devpath(st.st_rdev); | |
345 | } | |
346 | ||
347 | int path_attached_to_hba(const char *disk_path, const char *hba_path) | |
348 | { | |
349 | int rc; | |
350 | ||
fc13853f LM |
351 | if (check_env("IMSM_TEST_AHCI_DEV") || |
352 | check_env("IMSM_TEST_SCU_DEV")) { | |
353 | return 1; | |
354 | } | |
355 | ||
25921536 DW |
356 | if (!disk_path || !hba_path) |
357 | return 0; | |
a8e5382a | 358 | dprintf("hba: %s - disk: %s\n", hba_path, disk_path); |
25921536 DW |
359 | if (strncmp(disk_path, hba_path, strlen(hba_path)) == 0) |
360 | rc = 1; | |
361 | else | |
362 | rc = 0; | |
363 | ||
364 | return rc; | |
365 | } | |
366 | ||
367 | int devt_attached_to_hba(dev_t dev, const char *hba_path) | |
368 | { | |
369 | char *disk_path = devt_to_devpath(dev); | |
370 | int rc = path_attached_to_hba(disk_path, hba_path); | |
371 | ||
372 | if (disk_path) | |
373 | free(disk_path); | |
374 | ||
375 | return rc; | |
376 | } | |
377 | ||
378 | int disk_attached_to_hba(int fd, const char *hba_path) | |
379 | { | |
380 | char *disk_path = diskfd_to_devpath(fd); | |
381 | int rc = path_attached_to_hba(disk_path, hba_path); | |
382 | ||
383 | if (disk_path) | |
384 | free(disk_path); | |
385 | ||
386 | return rc; | |
387 | } |