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