2 * Intel(R) Matrix Storage Manager hardware and firmware support routines
4 * Copyright (C) 2008 Intel Corporation
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.
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
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.
20 #include "platform-intel.h"
21 #include "probe_roms.h"
29 #include <sys/types.h>
34 static int devpath_to_ll(const char *dev_path
, const char *entry
,
35 unsigned long long *val
);
37 static __u16
devpath_to_vendor(const char *dev_path
);
39 static void free_sys_dev(struct sys_dev
**list
)
42 struct sys_dev
*next
= (*list
)->next
;
51 struct sys_dev
*find_driver_devices(const char *bus
, const char *driver
)
53 /* search sysfs for devices driven by 'driver' */
59 struct sys_dev
*head
= NULL
;
60 struct sys_dev
*list
= NULL
;
61 enum sys_dev_type type
;
62 unsigned long long dev_id
;
64 if (strcmp(driver
, "isci") == 0)
66 else if (strcmp(driver
, "ahci") == 0)
69 type
= SYS_DEV_UNKNOWN
;
71 sprintf(path
, "/sys/bus/%s/drivers/%s", bus
, driver
);
72 driver_dir
= opendir(path
);
75 for (de
= readdir(driver_dir
); de
; de
= readdir(driver_dir
)) {
78 /* is 'de' a device? check that the 'subsystem' link exists and
79 * that its target matches 'bus'
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
))
87 c
= strrchr(link
, '/');
90 if (strncmp(bus
, c
+1, strlen(bus
)) != 0)
93 sprintf(path
, "/sys/bus/%s/drivers/%s/%s",
94 bus
, driver
, de
->d_name
);
96 /* if it's not Intel device skip it. */
97 if (devpath_to_vendor(path
) != 0x8086)
100 if (devpath_to_ll(path
, "device", &dev_id
) != 0)
103 /* start / add list entry */
105 head
= xmalloc(sizeof(*head
));
108 list
->next
= xmalloc(sizeof(*head
));
117 list
->dev_id
= (__u16
) dev_id
;
119 list
->path
= realpath(path
, NULL
);
121 if ((list
->pci_id
= strrchr(list
->path
, '/')) != NULL
)
124 closedir(driver_dir
);
129 static struct sys_dev
*intel_devices
=NULL
;
130 static time_t valid_time
= 0;
132 static enum sys_dev_type
device_type_by_id(__u16 device_id
)
134 struct sys_dev
*iter
;
136 for(iter
= intel_devices
; iter
!= NULL
; iter
= iter
->next
)
137 if (iter
->dev_id
== device_id
)
139 return SYS_DEV_UNKNOWN
;
142 static int devpath_to_ll(const char *dev_path
, const char *entry
, unsigned long long *val
)
144 char path
[strlen(dev_path
) + strlen(entry
) + 2];
148 sprintf(path
, "%s/%s", dev_path
, entry
);
150 fd
= open(path
, O_RDONLY
);
153 n
= sysfs_fd_get_ll(fd
, val
);
159 static __u16
devpath_to_vendor(const char *dev_path
)
161 char path
[strlen(dev_path
) + strlen("/vendor") + 1];
167 sprintf(path
, "%s/vendor", dev_path
);
169 fd
= open(path
, O_RDONLY
);
173 n
= read(fd
, vendor
, sizeof(vendor
));
174 if (n
== sizeof(vendor
)) {
175 vendor
[n
- 1] = '\0';
176 id
= strtoul(vendor
, NULL
, 16);
183 struct sys_dev
*find_intel_devices(void)
185 struct sys_dev
*ahci
, *isci
;
187 if (valid_time
> time(0) - 10)
188 return intel_devices
;
191 free_sys_dev(&intel_devices
);
193 isci
= find_driver_devices("pci", "isci");
194 ahci
= find_driver_devices("pci", "ahci");
199 struct sys_dev
*elem
= ahci
;
204 intel_devices
= ahci
;
205 valid_time
= time(0);
206 return intel_devices
;
210 * PCI Expansion ROM Data Structure Format */
211 struct pciExpDataStructFormat
{
215 } __attribute__ ((packed
));
217 static struct imsm_orom imsm_orom
[SYS_DEV_MAX
];
218 static int populated_orom
[SYS_DEV_MAX
];
220 static int scan(const void *start
, const void *end
, const void *data
)
223 const struct imsm_orom
*imsm_mem
;
225 int len
= (end
- start
);
226 struct pciExpDataStructFormat
*ptr
= (struct pciExpDataStructFormat
*)data
;
228 if (data
+ 0x18 > end
) {
229 dprintf("cannot find pciExpDataStruct \n");
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
));
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
)
246 for (offset
= 0; offset
< len
; offset
+= 4) {
247 imsm_mem
= start
+ offset
;
248 if ((memcmp(imsm_mem
->signature
, "$VER", 4) == 0)) {
249 imsm_orom
[dev
] = *imsm_mem
;
250 populated_orom
[dev
] = 1;
251 return populated_orom
[SYS_DEV_SATA
] && populated_orom
[SYS_DEV_SAS
];
258 const struct imsm_orom
*imsm_platform_test(enum sys_dev_type hba_id
, int *populated
,
259 struct imsm_orom
*imsm_orom
)
261 memset(imsm_orom
, 0, sizeof(*imsm_orom
));
262 imsm_orom
->rlc
= IMSM_OROM_RLC_RAID0
| IMSM_OROM_RLC_RAID1
|
263 IMSM_OROM_RLC_RAID10
| IMSM_OROM_RLC_RAID5
;
264 imsm_orom
->sss
= IMSM_OROM_SSS_4kB
| IMSM_OROM_SSS_8kB
|
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
;
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
;
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
;
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
;
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
;
294 static const struct imsm_orom
*find_imsm_hba_orom(enum sys_dev_type hba_id
)
298 if (hba_id
>= SYS_DEV_MAX
)
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
];
307 if (check_env("IMSM_TEST_OROM")) {
308 dprintf("OROM CAP: %p, pid: %d pop: %d\n",
309 &imsm_orom
[hba_id
], (int) getpid(), populated_orom
[hba_id
]);
310 return imsm_platform_test(hba_id
, &populated_orom
[hba_id
], &imsm_orom
[hba_id
]);
312 /* return empty OROM capabilities in EFI test mode */
313 if (check_env("IMSM_TEST_AHCI_EFI") ||
314 check_env("IMSM_TEST_SCU_EFI"))
317 find_intel_devices();
319 if (intel_devices
== NULL
)
322 /* scan option-rom memory looking for an imsm signature */
323 if (check_env("IMSM_SAFE_OROM_SCAN"))
327 if (probe_roms_init(align
) != 0)
330 /* ignore return value - True is returned if both adapater roms are found */
331 scan_adapter_roms(scan
);
334 if (populated_orom
[hba_id
])
335 return &imsm_orom
[hba_id
];
339 #define GUID_STR_MAX 37 /* according to GUID format:
340 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" */
342 #define EFI_GUID(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \
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) }})
350 #define SYS_EFI_VAR_PATH "/sys/firmware/efi/vars"
351 #define SCU_PROP "RstScuV"
352 #define AHCI_PROP "RstSataV"
354 #define VENDOR_GUID \
355 EFI_GUID(0x193dfefa, 0xa445, 0x4302, 0x99, 0xd8, 0xef, 0x3a, 0xad, 0x1a, 0x04, 0xc6)
357 int populated_efi
[SYS_DEV_MAX
] = { 0, 0 };
359 static struct imsm_orom imsm_efi
[SYS_DEV_MAX
];
361 int read_efi_variable(void *buffer
, ssize_t buf_size
, char *variable_name
, struct efi_guid guid
)
364 char buf
[GUID_STR_MAX
];
366 ssize_t n
, var_data_len
;
368 snprintf(path
, PATH_MAX
, "%s/%s-%s/size", SYS_EFI_VAR_PATH
, variable_name
, guid_str(buf
, guid
));
370 dprintf("EFI VAR: path=%s\n", path
);
371 /* get size of variable data */
372 dfd
= open(path
, O_RDONLY
);
376 n
= read(dfd
, &buf
, sizeof(buf
));
383 var_data_len
= strtoul(buf
, NULL
, 16);
384 if ((errno
== ERANGE
&& (var_data_len
== LONG_MAX
))
385 || (errno
!= 0 && var_data_len
== 0))
389 snprintf(path
, PATH_MAX
, "%s/%s-%s/data", SYS_EFI_VAR_PATH
, variable_name
, guid_str(buf
, guid
));
391 dprintf("EFI VAR: path=%s\n", path
);
392 dfd
= open(path
, O_RDONLY
);
396 n
= read(dfd
, buffer
, buf_size
);
398 if (n
!= var_data_len
|| n
< buf_size
) {
405 const struct imsm_orom
*find_imsm_efi(enum sys_dev_type hba_id
)
407 if (hba_id
>= SYS_DEV_MAX
)
410 dprintf("EFI CAP: %p, pid: %d pop: %d\n",
411 &imsm_efi
[hba_id
], (int) getpid(), populated_efi
[hba_id
]);
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
];
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
]);
425 /* OROM test is set, return that there is no EFI capabilities */
426 if (check_env("IMSM_TEST_OROM"))
429 if (read_efi_variable(&imsm_efi
[hba_id
], sizeof(imsm_efi
[0]), hba_id
== SYS_DEV_SAS
? SCU_PROP
: AHCI_PROP
, VENDOR_GUID
)) {
430 populated_efi
[hba_id
] = 0;
434 populated_efi
[hba_id
] = 1;
435 return &imsm_efi
[hba_id
];
439 * backward interface compatibility
441 const struct imsm_orom
*find_imsm_orom(void)
443 return find_imsm_hba_orom(SYS_DEV_SATA
);
446 const struct imsm_orom
*find_imsm_capability(enum sys_dev_type hba_id
)
448 const struct imsm_orom
*cap
=NULL
;
451 if ((cap
= find_imsm_efi(hba_id
)) != NULL
)
453 if ((cap
= find_imsm_hba_orom(hba_id
)) != NULL
)
458 char *devt_to_devpath(dev_t dev
)
462 sprintf(device
, "/sys/dev/block/%d:%d/device", major(dev
), minor(dev
));
463 return realpath(device
, NULL
);
466 char *diskfd_to_devpath(int fd
)
468 /* return the device path for a disk, return NULL on error or fd
469 * refers to a partition
473 if (fstat(fd
, &st
) != 0)
475 if (!S_ISBLK(st
.st_mode
))
478 return devt_to_devpath(st
.st_rdev
);
481 int path_attached_to_hba(const char *disk_path
, const char *hba_path
)
485 if (check_env("IMSM_TEST_AHCI_DEV") ||
486 check_env("IMSM_TEST_SCU_DEV")) {
490 if (!disk_path
|| !hba_path
)
492 dprintf("hba: %s - disk: %s\n", hba_path
, disk_path
);
493 if (strncmp(disk_path
, hba_path
, strlen(hba_path
)) == 0)
501 int devt_attached_to_hba(dev_t dev
, const char *hba_path
)
503 char *disk_path
= devt_to_devpath(dev
);
504 int rc
= path_attached_to_hba(disk_path
, hba_path
);
512 int disk_attached_to_hba(int fd
, const char *hba_path
)
514 char *disk_path
= diskfd_to_devpath(fd
);
515 int rc
= path_attached_to_hba(disk_path
, hba_path
);