]> git.ipfire.org Git - thirdparty/mdadm.git/blame - platform-intel.c
make --update=homehost work again
[thirdparty/mdadm.git] / platform-intel.c
CommitLineData
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
34static int devpath_to_ll(const char *dev_path, const char *entry,
35 unsigned long long *val);
36
fc13853f 37static __u16 devpath_to_vendor(const char *dev_path);
b390f610 38
9bc4ae77 39static void free_sys_dev(struct sys_dev **list)
b390f610
DW
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
51struct 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) {
503975b9 105 head = xmalloc(sizeof(*head));
b390f610
DW
106 list = head;
107 } else {
503975b9 108 list->next = xmalloc(sizeof(*head));
b390f610
DW
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 129static struct sys_dev *intel_devices=NULL;
9bc4ae77 130static time_t valid_time = 0;
9c747fa0
LM
131
132static enum sys_dev_type device_type_by_id(__u16 device_id)
133{
134 struct sys_dev *iter;
135
136 for(iter = intel_devices; iter != NULL; iter = iter->next)
137 if (iter->dev_id == device_id)
138 return iter->type;
139 return SYS_DEV_UNKNOWN;
140}
141
142static int devpath_to_ll(const char *dev_path, const char *entry, unsigned long long *val)
143{
144 char path[strlen(dev_path) + strlen(entry) + 2];
145 int fd;
146 int n;
147
148 sprintf(path, "%s/%s", dev_path, entry);
149
150 fd = open(path, O_RDONLY);
151 if (fd < 0)
152 return -1;
153 n = sysfs_fd_get_ll(fd, val);
154 close(fd);
155 return n;
156}
157
158
fc13853f 159static __u16 devpath_to_vendor(const char *dev_path)
b390f610
DW
160{
161 char path[strlen(dev_path) + strlen("/vendor") + 1];
162 char vendor[7];
163 int fd;
164 __u16 id = 0xffff;
165 int n;
166
167 sprintf(path, "%s/vendor", dev_path);
168
169 fd = open(path, O_RDONLY);
170 if (fd < 0)
171 return 0xffff;
172
173 n = read(fd, vendor, sizeof(vendor));
174 if (n == sizeof(vendor)) {
175 vendor[n - 1] = '\0';
176 id = strtoul(vendor, NULL, 16);
177 }
178 close(fd);
179
180 return id;
181}
182
a8e5382a 183struct sys_dev *find_intel_devices(void)
b390f610 184{
a8e5382a
LM
185 struct sys_dev *ahci, *isci;
186
9bc4ae77
N
187 if (valid_time > time(0) - 10)
188 return intel_devices;
189
190 if (intel_devices)
191 free_sys_dev(&intel_devices);
192
a8e5382a
LM
193 isci = find_driver_devices("pci", "isci");
194 ahci = find_driver_devices("pci", "ahci");
195
196 if (!ahci) {
197 ahci = isci;
198 } else {
199 struct sys_dev *elem = ahci;
200 while (elem->next)
201 elem = elem->next;
202 elem->next = isci;
203 }
9bc4ae77
N
204 intel_devices = ahci;
205 valid_time = time(0);
206 return intel_devices;
b390f610
DW
207}
208
3c8bfb5d 209/*
fc13853f 210 * PCI Expansion ROM Data Structure Format */
3c8bfb5d
LM
211struct pciExpDataStructFormat {
212 __u8 ver[4];
213 __u16 vendorID;
214 __u16 deviceID;
215} __attribute__ ((packed));
216
fc13853f
LM
217static struct imsm_orom imsm_orom[SYS_DEV_MAX];
218static int populated_orom[SYS_DEV_MAX];
219
3c8bfb5d 220static int scan(const void *start, const void *end, const void *data)
b390f610
DW
221{
222 int offset;
223 const struct imsm_orom *imsm_mem;
fc13853f 224 int dev;
b390f610 225 int len = (end - start);
3c8bfb5d
LM
226 struct pciExpDataStructFormat *ptr= (struct pciExpDataStructFormat *)data;
227
9c747fa0
LM
228 if (data + 0x18 > end) {
229 dprintf("cannot find pciExpDataStruct \n");
230 return 0;
231 }
232
3c8bfb5d
LM
233 dprintf("ptr->vendorID: %lx __le16_to_cpu(ptr->deviceID): %lx \n",
234 (ulong) __le16_to_cpu(ptr->vendorID),
235 (ulong) __le16_to_cpu(ptr->deviceID));
236
9c747fa0
LM
237 if (__le16_to_cpu(ptr->vendorID) == 0x8086) {
238 /* serach attached intel devices by device id from OROM */
239 dev = device_type_by_id(__le16_to_cpu(ptr->deviceID));
240 if (dev == SYS_DEV_UNKNOWN)
241 return 0;
242 }
fc13853f 243 else
3c8bfb5d 244 return 0;
b390f610
DW
245
246 for (offset = 0; offset < len; offset += 4) {
247 imsm_mem = start + offset;
96687b79 248 if ((memcmp(imsm_mem->signature, "$VER", 4) == 0)) {
fc13853f
LM
249 imsm_orom[dev] = *imsm_mem;
250 populated_orom[dev] = 1;
251 return populated_orom[SYS_DEV_SATA] && populated_orom[SYS_DEV_SAS];
b390f610
DW
252 }
253 }
b390f610
DW
254 return 0;
255}
256
b390f610 257
fc13853f
LM
258const struct imsm_orom *imsm_platform_test(enum sys_dev_type hba_id, int *populated,
259 struct imsm_orom *imsm_orom)
260{
261 memset(imsm_orom, 0, sizeof(*imsm_orom));
262 imsm_orom->rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 |
cceebc67 263 IMSM_OROM_RLC_RAID10 | IMSM_OROM_RLC_RAID5;
fc13853f 264 imsm_orom->sss = IMSM_OROM_SSS_4kB | IMSM_OROM_SSS_8kB |
cceebc67
DW
265 IMSM_OROM_SSS_16kB | IMSM_OROM_SSS_32kB |
266 IMSM_OROM_SSS_64kB | IMSM_OROM_SSS_128kB |
267 IMSM_OROM_SSS_256kB | IMSM_OROM_SSS_512kB |
268 IMSM_OROM_SSS_1MB | IMSM_OROM_SSS_2MB;
fc13853f
LM
269 imsm_orom->dpa = IMSM_OROM_DISKS_PER_ARRAY;
270 imsm_orom->tds = IMSM_OROM_TOTAL_DISKS;
271 imsm_orom->vpa = IMSM_OROM_VOLUMES_PER_ARRAY;
272 imsm_orom->vphba = IMSM_OROM_VOLUMES_PER_HBA;
273 imsm_orom->attr = imsm_orom->rlc | IMSM_OROM_ATTR_ChecksumVerify;
274 *populated = 1;
275
276 if (check_env("IMSM_TEST_OROM_NORAID5")) {
277 imsm_orom->rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 |
278 IMSM_OROM_RLC_RAID10;
279 }
280 if (check_env("IMSM_TEST_AHCI_EFI_NORAID5") && (hba_id == SYS_DEV_SAS)) {
281 imsm_orom->rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 |
282 IMSM_OROM_RLC_RAID10;
283 }
284 if (check_env("IMSM_TEST_SCU_EFI_NORAID5") && (hba_id == SYS_DEV_SATA)) {
285 imsm_orom->rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 |
286 IMSM_OROM_RLC_RAID10;
cceebc67
DW
287 }
288
fc13853f
LM
289 return imsm_orom;
290}
291
292
293
294static const struct imsm_orom *find_imsm_hba_orom(enum sys_dev_type hba_id)
295{
296 unsigned long align;
297
298 if (hba_id >= SYS_DEV_MAX)
299 return NULL;
300
301 /* it's static data so we only need to read it once */
302 if (populated_orom[hba_id]) {
303 dprintf("OROM CAP: %p, pid: %d pop: %d\n",
304 &imsm_orom[hba_id], (int) getpid(), populated_orom[hba_id]);
305 return &imsm_orom[hba_id];
306 }
307 if (check_env("IMSM_TEST_OROM")) {
308 dprintf("OROM CAP: %p, pid: %d pop: %d\n",
5d500228 309 &imsm_orom[hba_id], (int) getpid(), populated_orom[hba_id]);
fc13853f
LM
310 return imsm_platform_test(hba_id, &populated_orom[hba_id], &imsm_orom[hba_id]);
311 }
312 /* return empty OROM capabilities in EFI test mode */
313 if (check_env("IMSM_TEST_AHCI_EFI") ||
314 check_env("IMSM_TEST_SCU_EFI"))
315 return NULL;
316
9bc4ae77 317 find_intel_devices();
9c747fa0
LM
318
319 if (intel_devices == NULL)
b390f610
DW
320 return NULL;
321
322 /* scan option-rom memory looking for an imsm signature */
969c2555
DW
323 if (check_env("IMSM_SAFE_OROM_SCAN"))
324 align = 2048;
325 else
326 align = 512;
327 if (probe_roms_init(align) != 0)
b390f610
DW
328 return NULL;
329 probe_roms();
9c747fa0 330 /* ignore return value - True is returned if both adapater roms are found */
fc13853f 331 scan_adapter_roms(scan);
b390f610
DW
332 probe_roms_exit();
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"
352#define AHCI_PROP "RstSataV"
353
354#define VENDOR_GUID \
355 EFI_GUID(0x193dfefa, 0xa445, 0x4302, 0x99, 0xd8, 0xef, 0x3a, 0xad, 0x1a, 0x04, 0xc6)
356
357int populated_efi[SYS_DEV_MAX] = { 0, 0 };
358
359static struct imsm_orom imsm_efi[SYS_DEV_MAX];
360
aae4c111 361int read_efi_variable(void *buffer, ssize_t buf_size, char *variable_name, struct efi_guid guid)
5a6baf66 362{
5a6baf66
LM
363 char path[PATH_MAX];
364 char buf[GUID_STR_MAX];
aae4c111
HCP
365 int dfd;
366 ssize_t n, var_data_len;
367
368 snprintf(path, PATH_MAX, "%s/%s-%s/size", SYS_EFI_VAR_PATH, variable_name, guid_str(buf, guid));
369
370 dprintf("EFI VAR: path=%s\n", path);
371 /* get size of variable data */
372 dfd = open(path, O_RDONLY);
373 if (dfd < 0)
374 return 1;
375
376 n = read(dfd, &buf, sizeof(buf));
377 close(dfd);
378 if (n < 0)
379 return 1;
380 buf[n] = '\0';
381
382 errno = 0;
383 var_data_len = strtoul(buf, NULL, 16);
384 if ((errno == ERANGE && (var_data_len == LONG_MAX))
5d500228 385 || (errno != 0 && var_data_len == 0))
aae4c111
HCP
386 return 1;
387
388 /* get data */
389 snprintf(path, PATH_MAX, "%s/%s-%s/data", SYS_EFI_VAR_PATH, variable_name, guid_str(buf, guid));
390
391 dprintf("EFI VAR: path=%s\n", path);
392 dfd = open(path, O_RDONLY);
393 if (dfd < 0)
394 return 1;
395
396 n = read(dfd, buffer, buf_size);
397 close(dfd);
398 if (n != var_data_len || n < buf_size) {
399 return 1;
400 }
401
402 return 0;
403}
5a6baf66 404
aae4c111
HCP
405const struct imsm_orom *find_imsm_efi(enum sys_dev_type hba_id)
406{
5a6baf66
LM
407 if (hba_id >= SYS_DEV_MAX)
408 return NULL;
409
410 dprintf("EFI CAP: %p, pid: %d pop: %d\n",
411 &imsm_efi[hba_id], (int) getpid(), populated_efi[hba_id]);
412
413 /* it's static data so we only need to read it once */
414 if (populated_efi[hba_id]) {
415 dprintf("EFI CAP: %p, pid: %d pop: %d\n",
416 &imsm_efi[hba_id], (int) getpid(), populated_efi[hba_id]);
417 return &imsm_efi[hba_id];
418 }
419 if (check_env("IMSM_TEST_AHCI_EFI") ||
420 check_env("IMSM_TEST_SCU_EFI")) {
421 dprintf("OROM CAP: %p, pid: %d pop: %d\n",
422 &imsm_efi[hba_id], (int) getpid(), populated_efi[hba_id]);
423 return imsm_platform_test(hba_id, &populated_efi[hba_id], &imsm_efi[hba_id]);
424 }
425 /* OROM test is set, return that there is no EFI capabilities */
aae4c111 426 if (check_env("IMSM_TEST_OROM"))
5a6baf66 427 return NULL;
5a6baf66 428
aae4c111 429 if (read_efi_variable(&imsm_efi[hba_id], sizeof(imsm_efi[0]), hba_id == SYS_DEV_SAS ? SCU_PROP : AHCI_PROP, VENDOR_GUID)) {
5a6baf66
LM
430 populated_efi[hba_id] = 0;
431 return NULL;
432 }
aae4c111 433
5a6baf66
LM
434 populated_efi[hba_id] = 1;
435 return &imsm_efi[hba_id];
436}
437
fc13853f
LM
438/*
439 * backward interface compatibility
440 */
441const struct imsm_orom *find_imsm_orom(void)
442{
443 return find_imsm_hba_orom(SYS_DEV_SATA);
444}
445
446const struct imsm_orom *find_imsm_capability(enum sys_dev_type hba_id)
447{
448 const struct imsm_orom *cap=NULL;
449
5a6baf66
LM
450
451 if ((cap = find_imsm_efi(hba_id)) != NULL)
452 return cap;
fc13853f
LM
453 if ((cap = find_imsm_hba_orom(hba_id)) != NULL)
454 return cap;
b390f610
DW
455 return NULL;
456}
25921536
DW
457
458char *devt_to_devpath(dev_t dev)
459{
37f0e1e0 460 char device[46];
25921536
DW
461
462 sprintf(device, "/sys/dev/block/%d:%d/device", major(dev), minor(dev));
463 return canonicalize_file_name(device);
464}
465
a8e5382a 466char *diskfd_to_devpath(int fd)
25921536
DW
467{
468 /* return the device path for a disk, return NULL on error or fd
469 * refers to a partition
470 */
471 struct stat st;
472
473 if (fstat(fd, &st) != 0)
474 return NULL;
475 if (!S_ISBLK(st.st_mode))
476 return NULL;
477
478 return devt_to_devpath(st.st_rdev);
479}
480
481int path_attached_to_hba(const char *disk_path, const char *hba_path)
482{
483 int rc;
484
fc13853f
LM
485 if (check_env("IMSM_TEST_AHCI_DEV") ||
486 check_env("IMSM_TEST_SCU_DEV")) {
487 return 1;
488 }
489
25921536
DW
490 if (!disk_path || !hba_path)
491 return 0;
a8e5382a 492 dprintf("hba: %s - disk: %s\n", hba_path, disk_path);
25921536
DW
493 if (strncmp(disk_path, hba_path, strlen(hba_path)) == 0)
494 rc = 1;
495 else
496 rc = 0;
497
498 return rc;
499}
500
501int devt_attached_to_hba(dev_t dev, const char *hba_path)
502{
503 char *disk_path = devt_to_devpath(dev);
504 int rc = path_attached_to_hba(disk_path, hba_path);
505
506 if (disk_path)
507 free(disk_path);
508
509 return rc;
510}
511
512int disk_attached_to_hba(int fd, const char *hba_path)
513{
514 char *disk_path = diskfd_to_devpath(fd);
515 int rc = path_attached_to_hba(disk_path, hba_path);
516
517 if (disk_path)
518 free(disk_path);
519
520 return rc;
521}