]> git.ipfire.org Git - thirdparty/mdadm.git/blob - platform-intel.c
Create: support --readonly flag.
[thirdparty/mdadm.git] / platform-intel.c
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>
31 #include <limits.h>
32
33
34 static int devpath_to_ll(const char *dev_path, const char *entry,
35 unsigned long long *val);
36
37 static __u16 devpath_to_vendor(const char *dev_path);
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' */
54 char path[292];
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;
61 enum sys_dev_type type;
62 unsigned long long dev_id;
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;
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)) {
76 int n;
77
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);
83 n = readlink(path, link, sizeof(link));
84 if (n < 0 || n >= (int)sizeof(link))
85 continue;
86 link[n] = '\0';
87 c = strrchr(link, '/');
88 if (!c)
89 continue;
90 if (strncmp(bus, c+1, strlen(bus)) != 0)
91 continue;
92
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
100 if (devpath_to_ll(path, "device", &dev_id) != 0)
101 continue;
102
103 /* start / add list entry */
104 if (!head) {
105 head = xmalloc(sizeof(*head));
106 list = head;
107 } else {
108 list->next = xmalloc(sizeof(*head));
109 list = list->next;
110 }
111
112 if (!list) {
113 free_sys_dev(&head);
114 break;
115 }
116
117 list->dev_id = (__u16) dev_id;
118 list->type = type;
119 list->path = canonicalize_file_name(path);
120 list->next = NULL;
121 if ((list->pci_id = strrchr(list->path, '/')) != NULL)
122 list->pci_id++;
123 }
124 closedir(driver_dir);
125 return head;
126 }
127
128
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
158 static __u16 devpath_to_vendor(const char *dev_path)
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
182 struct sys_dev *find_intel_devices(void)
183 {
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;
198 }
199
200 /*
201 * PCI Expansion ROM Data Structure Format */
202 struct pciExpDataStructFormat {
203 __u8 ver[4];
204 __u16 vendorID;
205 __u16 deviceID;
206 } __attribute__ ((packed));
207
208 static struct imsm_orom imsm_orom[SYS_DEV_MAX];
209 static int populated_orom[SYS_DEV_MAX];
210
211 static int scan(const void *start, const void *end, const void *data)
212 {
213 int offset;
214 const struct imsm_orom *imsm_mem;
215 int dev;
216 int len = (end - start);
217 struct pciExpDataStructFormat *ptr= (struct pciExpDataStructFormat *)data;
218
219 if (data + 0x18 > end) {
220 dprintf("cannot find pciExpDataStruct \n");
221 return 0;
222 }
223
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
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 }
234 else
235 return 0;
236
237 for (offset = 0; offset < len; offset += 4) {
238 imsm_mem = start + offset;
239 if ((memcmp(imsm_mem->signature, "$VER", 4) == 0)) {
240 imsm_orom[dev] = *imsm_mem;
241 populated_orom[dev] = 1;
242 return populated_orom[SYS_DEV_SATA] && populated_orom[SYS_DEV_SAS];
243 }
244 }
245 return 0;
246 }
247
248
249 const struct imsm_orom *imsm_platform_test(enum sys_dev_type hba_id, int *populated,
250 struct imsm_orom *imsm_orom)
251 {
252 memset(imsm_orom, 0, sizeof(*imsm_orom));
253 imsm_orom->rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 |
254 IMSM_OROM_RLC_RAID10 | IMSM_OROM_RLC_RAID5;
255 imsm_orom->sss = IMSM_OROM_SSS_4kB | IMSM_OROM_SSS_8kB |
256 IMSM_OROM_SSS_16kB | IMSM_OROM_SSS_32kB |
257 IMSM_OROM_SSS_64kB | IMSM_OROM_SSS_128kB |
258 IMSM_OROM_SSS_256kB | IMSM_OROM_SSS_512kB |
259 IMSM_OROM_SSS_1MB | IMSM_OROM_SSS_2MB;
260 imsm_orom->dpa = IMSM_OROM_DISKS_PER_ARRAY;
261 imsm_orom->tds = IMSM_OROM_TOTAL_DISKS;
262 imsm_orom->vpa = IMSM_OROM_VOLUMES_PER_ARRAY;
263 imsm_orom->vphba = IMSM_OROM_VOLUMES_PER_HBA;
264 imsm_orom->attr = imsm_orom->rlc | IMSM_OROM_ATTR_ChecksumVerify;
265 *populated = 1;
266
267 if (check_env("IMSM_TEST_OROM_NORAID5")) {
268 imsm_orom->rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 |
269 IMSM_OROM_RLC_RAID10;
270 }
271 if (check_env("IMSM_TEST_AHCI_EFI_NORAID5") && (hba_id == SYS_DEV_SAS)) {
272 imsm_orom->rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 |
273 IMSM_OROM_RLC_RAID10;
274 }
275 if (check_env("IMSM_TEST_SCU_EFI_NORAID5") && (hba_id == SYS_DEV_SATA)) {
276 imsm_orom->rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 |
277 IMSM_OROM_RLC_RAID10;
278 }
279
280 return imsm_orom;
281 }
282
283
284
285 static const struct imsm_orom *find_imsm_hba_orom(enum sys_dev_type hba_id)
286 {
287 unsigned long align;
288
289 if (hba_id >= SYS_DEV_MAX)
290 return NULL;
291
292 /* it's static data so we only need to read it once */
293 if (populated_orom[hba_id]) {
294 dprintf("OROM CAP: %p, pid: %d pop: %d\n",
295 &imsm_orom[hba_id], (int) getpid(), populated_orom[hba_id]);
296 return &imsm_orom[hba_id];
297 }
298 if (check_env("IMSM_TEST_OROM")) {
299 dprintf("OROM CAP: %p, pid: %d pop: %d\n",
300 &imsm_orom[hba_id], (int) getpid(), populated_orom[hba_id]);
301 return imsm_platform_test(hba_id, &populated_orom[hba_id], &imsm_orom[hba_id]);
302 }
303 /* return empty OROM capabilities in EFI test mode */
304 if (check_env("IMSM_TEST_AHCI_EFI") ||
305 check_env("IMSM_TEST_SCU_EFI"))
306 return NULL;
307
308
309 if (intel_devices != NULL)
310 free_sys_dev(&intel_devices);
311
312 intel_devices = find_intel_devices();
313
314 if (intel_devices == NULL)
315 return NULL;
316
317 /* scan option-rom memory looking for an imsm signature */
318 if (check_env("IMSM_SAFE_OROM_SCAN"))
319 align = 2048;
320 else
321 align = 512;
322 if (probe_roms_init(align) != 0)
323 return NULL;
324 probe_roms();
325 /* ignore return value - True is returned if both adapater roms are found */
326 scan_adapter_roms(scan);
327 probe_roms_exit();
328
329 if (intel_devices != NULL)
330 free_sys_dev(&intel_devices);
331 intel_devices = NULL;
332
333 if (populated_orom[hba_id])
334 return &imsm_orom[hba_id];
335 return NULL;
336 }
337
338 #define GUID_STR_MAX 37 /* according to GUID format:
339 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" */
340
341 #define EFI_GUID(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \
342 ((struct efi_guid) \
343 {{ (a) & 0xff, ((a) >> 8) & 0xff, ((a) >> 16) & 0xff, ((a) >> 24) & 0xff, \
344 (b) & 0xff, ((b) >> 8) & 0xff, \
345 (c) & 0xff, ((c) >> 8) & 0xff, \
346 (d0), (d1), (d2), (d3), (d4), (d5), (d6), (d7) }})
347
348
349 #define SYS_EFI_VAR_PATH "/sys/firmware/efi/vars"
350 #define SCU_PROP "RstScuV"
351 #define AHCI_PROP "RstSataV"
352
353 #define VENDOR_GUID \
354 EFI_GUID(0x193dfefa, 0xa445, 0x4302, 0x99, 0xd8, 0xef, 0x3a, 0xad, 0x1a, 0x04, 0xc6)
355
356 int populated_efi[SYS_DEV_MAX] = { 0, 0 };
357
358 static struct imsm_orom imsm_efi[SYS_DEV_MAX];
359
360 int read_efi_variable(void *buffer, ssize_t buf_size, char *variable_name, struct efi_guid guid)
361 {
362 char path[PATH_MAX];
363 char buf[GUID_STR_MAX];
364 int dfd;
365 ssize_t n, var_data_len;
366
367 snprintf(path, PATH_MAX, "%s/%s-%s/size", SYS_EFI_VAR_PATH, variable_name, guid_str(buf, guid));
368
369 dprintf("EFI VAR: path=%s\n", path);
370 /* get size of variable data */
371 dfd = open(path, O_RDONLY);
372 if (dfd < 0)
373 return 1;
374
375 n = read(dfd, &buf, sizeof(buf));
376 close(dfd);
377 if (n < 0)
378 return 1;
379 buf[n] = '\0';
380
381 errno = 0;
382 var_data_len = strtoul(buf, NULL, 16);
383 if ((errno == ERANGE && (var_data_len == LONG_MAX))
384 || (errno != 0 && var_data_len == 0))
385 return 1;
386
387 /* get data */
388 snprintf(path, PATH_MAX, "%s/%s-%s/data", SYS_EFI_VAR_PATH, variable_name, guid_str(buf, guid));
389
390 dprintf("EFI VAR: path=%s\n", path);
391 dfd = open(path, O_RDONLY);
392 if (dfd < 0)
393 return 1;
394
395 n = read(dfd, buffer, buf_size);
396 close(dfd);
397 if (n != var_data_len || n < buf_size) {
398 return 1;
399 }
400
401 return 0;
402 }
403
404 const struct imsm_orom *find_imsm_efi(enum sys_dev_type hba_id)
405 {
406 if (hba_id >= SYS_DEV_MAX)
407 return NULL;
408
409 dprintf("EFI CAP: %p, pid: %d pop: %d\n",
410 &imsm_efi[hba_id], (int) getpid(), populated_efi[hba_id]);
411
412 /* it's static data so we only need to read it once */
413 if (populated_efi[hba_id]) {
414 dprintf("EFI CAP: %p, pid: %d pop: %d\n",
415 &imsm_efi[hba_id], (int) getpid(), populated_efi[hba_id]);
416 return &imsm_efi[hba_id];
417 }
418 if (check_env("IMSM_TEST_AHCI_EFI") ||
419 check_env("IMSM_TEST_SCU_EFI")) {
420 dprintf("OROM CAP: %p, pid: %d pop: %d\n",
421 &imsm_efi[hba_id], (int) getpid(), populated_efi[hba_id]);
422 return imsm_platform_test(hba_id, &populated_efi[hba_id], &imsm_efi[hba_id]);
423 }
424 /* OROM test is set, return that there is no EFI capabilities */
425 if (check_env("IMSM_TEST_OROM"))
426 return NULL;
427
428 if (read_efi_variable(&imsm_efi[hba_id], sizeof(imsm_efi[0]), hba_id == SYS_DEV_SAS ? SCU_PROP : AHCI_PROP, VENDOR_GUID)) {
429 populated_efi[hba_id] = 0;
430 return NULL;
431 }
432
433 populated_efi[hba_id] = 1;
434 return &imsm_efi[hba_id];
435 }
436
437 /*
438 * backward interface compatibility
439 */
440 const struct imsm_orom *find_imsm_orom(void)
441 {
442 return find_imsm_hba_orom(SYS_DEV_SATA);
443 }
444
445 const struct imsm_orom *find_imsm_capability(enum sys_dev_type hba_id)
446 {
447 const struct imsm_orom *cap=NULL;
448
449
450 if ((cap = find_imsm_efi(hba_id)) != NULL)
451 return cap;
452 if ((cap = find_imsm_hba_orom(hba_id)) != NULL)
453 return cap;
454 return NULL;
455 }
456
457 char *devt_to_devpath(dev_t dev)
458 {
459 char device[46];
460
461 sprintf(device, "/sys/dev/block/%d:%d/device", major(dev), minor(dev));
462 return canonicalize_file_name(device);
463 }
464
465 char *diskfd_to_devpath(int fd)
466 {
467 /* return the device path for a disk, return NULL on error or fd
468 * refers to a partition
469 */
470 struct stat st;
471
472 if (fstat(fd, &st) != 0)
473 return NULL;
474 if (!S_ISBLK(st.st_mode))
475 return NULL;
476
477 return devt_to_devpath(st.st_rdev);
478 }
479
480 int path_attached_to_hba(const char *disk_path, const char *hba_path)
481 {
482 int rc;
483
484 if (check_env("IMSM_TEST_AHCI_DEV") ||
485 check_env("IMSM_TEST_SCU_DEV")) {
486 return 1;
487 }
488
489 if (!disk_path || !hba_path)
490 return 0;
491 dprintf("hba: %s - disk: %s\n", hba_path, disk_path);
492 if (strncmp(disk_path, hba_path, strlen(hba_path)) == 0)
493 rc = 1;
494 else
495 rc = 0;
496
497 return rc;
498 }
499
500 int devt_attached_to_hba(dev_t dev, const char *hba_path)
501 {
502 char *disk_path = devt_to_devpath(dev);
503 int rc = path_attached_to_hba(disk_path, hba_path);
504
505 if (disk_path)
506 free(disk_path);
507
508 return rc;
509 }
510
511 int disk_attached_to_hba(int fd, const char *hba_path)
512 {
513 char *disk_path = diskfd_to_devpath(fd);
514 int rc = path_attached_to_hba(disk_path, hba_path);
515
516 if (disk_path)
517 free(disk_path);
518
519 return rc;
520 }