1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/firmware/edd.c
4 * Copyright (C) 2002, 2003, 2004 Dell Inc.
5 * by Matt Domsch <Matt_Domsch@dell.com>
6 * disk signature by Matt Domsch, Andrew Wilks, and Sandeep K. Shandilya
7 * legacy CHS by Patrick J. LoPresti <patl@users.sourceforge.net>
9 * BIOS Enhanced Disk Drive Services (EDD)
10 * conformant to T13 Committee www.t13.org
11 * projects 1572D, 1484D, 1386D, 1226DT
13 * This code takes information provided by BIOS EDD calls
14 * fn41 - Check Extensions Present and
15 * fn48 - Get Device Parameters with EDD extensions
16 * made in setup.S, copied to safe structures in setup.c,
17 * and presents it in sysfs.
19 * Please see http://linux.dell.com/edd/results.html for
20 * the list of BIOSs which have been reported to implement EDD.
23 #include <linux/module.h>
24 #include <linux/string.h>
25 #include <linux/types.h>
26 #include <linux/init.h>
27 #include <linux/stat.h>
28 #include <linux/err.h>
29 #include <linux/ctype.h>
30 #include <linux/slab.h>
31 #include <linux/limits.h>
32 #include <linux/device.h>
33 #include <linux/pci.h>
34 #include <linux/blkdev.h>
35 #include <linux/edd.h>
37 #define EDD_VERSION "0.16"
38 #define EDD_DATE "2004-Jun-25"
40 MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
41 MODULE_DESCRIPTION("sysfs interface to BIOS EDD information");
42 MODULE_LICENSE("GPL");
43 MODULE_VERSION(EDD_VERSION);
45 #define left (PAGE_SIZE - (p - buf) - 1)
49 unsigned int mbr_signature;
50 struct edd_info *info;
54 struct edd_attribute {
55 struct attribute attr;
56 ssize_t(*show) (struct edd_device * edev, char *buf);
57 int (*test) (struct edd_device * edev);
60 /* forward declarations */
61 static int edd_dev_is_type(struct edd_device *edev, const char *type);
62 static struct pci_dev *edd_get_pci_dev(struct edd_device *edev);
64 static struct edd_device *edd_devices[EDD_MBR_SIG_MAX];
66 #define EDD_DEVICE_ATTR(_name,_mode,_show,_test) \
67 struct edd_attribute edd_attr_##_name = { \
68 .attr = {.name = __stringify(_name), .mode = _mode }, \
74 edd_has_mbr_signature(struct edd_device *edev)
76 return edev->index < min_t(unsigned char, edd.mbr_signature_nr, EDD_MBR_SIG_MAX);
80 edd_has_edd_info(struct edd_device *edev)
82 return edev->index < min_t(unsigned char, edd.edd_info_nr, EDDMAXNR);
85 static inline struct edd_info *
86 edd_dev_get_info(struct edd_device *edev)
92 edd_dev_set_info(struct edd_device *edev, int i)
95 if (edd_has_mbr_signature(edev))
96 edev->mbr_signature = edd.mbr_signature[i];
97 if (edd_has_edd_info(edev))
98 edev->info = &edd.edd_info[i];
101 #define to_edd_attr(_attr) container_of(_attr,struct edd_attribute,attr)
102 #define to_edd_device(obj) container_of(obj,struct edd_device,kobj)
105 edd_attr_show(struct kobject * kobj, struct attribute *attr, char *buf)
107 struct edd_device *dev = to_edd_device(kobj);
108 struct edd_attribute *edd_attr = to_edd_attr(attr);
112 ret = edd_attr->show(dev, buf);
116 static const struct sysfs_ops edd_attr_ops = {
117 .show = edd_attr_show,
121 edd_show_host_bus(struct edd_device *edev, char *buf)
123 struct edd_info *info;
129 info = edd_dev_get_info(edev);
133 for (i = 0; i < 4; i++) {
134 if (isprint(info->params.host_bus_type[i])) {
135 p += scnprintf(p, left, "%c", info->params.host_bus_type[i]);
137 p += scnprintf(p, left, " ");
141 if (!strncmp(info->params.host_bus_type, "ISA", 3)) {
142 p += scnprintf(p, left, "\tbase_address: %x\n",
143 info->params.interface_path.isa.base_address);
144 } else if (!strncmp(info->params.host_bus_type, "PCIX", 4) ||
145 !strncmp(info->params.host_bus_type, "PCI", 3) ||
146 !strncmp(info->params.host_bus_type, "XPRS", 4)) {
147 p += scnprintf(p, left,
148 "\t%02x:%02x.%d channel: %u\n",
149 info->params.interface_path.pci.bus,
150 info->params.interface_path.pci.slot,
151 info->params.interface_path.pci.function,
152 info->params.interface_path.pci.channel);
153 } else if (!strncmp(info->params.host_bus_type, "IBND", 4) ||
154 !strncmp(info->params.host_bus_type, "HTPT", 4)) {
155 p += scnprintf(p, left,
157 info->params.interface_path.ibnd.reserved);
160 p += scnprintf(p, left, "\tunknown: %llx\n",
161 info->params.interface_path.unknown.reserved);
167 edd_show_interface(struct edd_device *edev, char *buf)
169 struct edd_info *info;
175 info = edd_dev_get_info(edev);
179 for (i = 0; i < 8; i++) {
180 if (isprint(info->params.interface_type[i])) {
181 p += scnprintf(p, left, "%c", info->params.interface_type[i]);
183 p += scnprintf(p, left, " ");
186 if (!strncmp(info->params.interface_type, "ATAPI", 5)) {
187 p += scnprintf(p, left, "\tdevice: %u lun: %u\n",
188 info->params.device_path.atapi.device,
189 info->params.device_path.atapi.lun);
190 } else if (!strncmp(info->params.interface_type, "ATA", 3)) {
191 p += scnprintf(p, left, "\tdevice: %u\n",
192 info->params.device_path.ata.device);
193 } else if (!strncmp(info->params.interface_type, "SCSI", 4)) {
194 p += scnprintf(p, left, "\tid: %u lun: %llu\n",
195 info->params.device_path.scsi.id,
196 info->params.device_path.scsi.lun);
197 } else if (!strncmp(info->params.interface_type, "USB", 3)) {
198 p += scnprintf(p, left, "\tserial_number: %llx\n",
199 info->params.device_path.usb.serial_number);
200 } else if (!strncmp(info->params.interface_type, "1394", 4)) {
201 p += scnprintf(p, left, "\teui: %llx\n",
202 info->params.device_path.i1394.eui);
203 } else if (!strncmp(info->params.interface_type, "FIBRE", 5)) {
204 p += scnprintf(p, left, "\twwid: %llx lun: %llx\n",
205 info->params.device_path.fibre.wwid,
206 info->params.device_path.fibre.lun);
207 } else if (!strncmp(info->params.interface_type, "I2O", 3)) {
208 p += scnprintf(p, left, "\tidentity_tag: %llx\n",
209 info->params.device_path.i2o.identity_tag);
210 } else if (!strncmp(info->params.interface_type, "RAID", 4)) {
211 p += scnprintf(p, left, "\tidentity_tag: %x\n",
212 info->params.device_path.raid.array_number);
213 } else if (!strncmp(info->params.interface_type, "SATA", 4)) {
214 p += scnprintf(p, left, "\tdevice: %u\n",
215 info->params.device_path.sata.device);
217 p += scnprintf(p, left, "\tunknown: %llx %llx\n",
218 info->params.device_path.unknown.reserved1,
219 info->params.device_path.unknown.reserved2);
226 * edd_show_raw_data() - copies raw data to buffer for userspace to parse
227 * @edev: target edd_device
228 * @buf: output buffer
230 * Returns: number of bytes written, or -EINVAL on failure
233 edd_show_raw_data(struct edd_device *edev, char *buf)
235 struct edd_info *info;
236 ssize_t len = sizeof (info->params);
239 info = edd_dev_get_info(edev);
243 if (!(info->params.key == 0xBEDD || info->params.key == 0xDDBE))
244 len = info->params.length;
246 /* In case of buggy BIOSs */
247 if (len > (sizeof(info->params)))
248 len = sizeof(info->params);
250 memcpy(buf, &info->params, len);
255 edd_show_version(struct edd_device *edev, char *buf)
257 struct edd_info *info;
261 info = edd_dev_get_info(edev);
265 p += scnprintf(p, left, "0x%02x\n", info->version);
270 edd_show_mbr_signature(struct edd_device *edev, char *buf)
273 p += scnprintf(p, left, "0x%08x\n", edev->mbr_signature);
278 edd_show_extensions(struct edd_device *edev, char *buf)
280 struct edd_info *info;
284 info = edd_dev_get_info(edev);
288 if (info->interface_support & EDD_EXT_FIXED_DISK_ACCESS) {
289 p += scnprintf(p, left, "Fixed disk access\n");
291 if (info->interface_support & EDD_EXT_DEVICE_LOCKING_AND_EJECTING) {
292 p += scnprintf(p, left, "Device locking and ejecting\n");
294 if (info->interface_support & EDD_EXT_ENHANCED_DISK_DRIVE_SUPPORT) {
295 p += scnprintf(p, left, "Enhanced Disk Drive support\n");
297 if (info->interface_support & EDD_EXT_64BIT_EXTENSIONS) {
298 p += scnprintf(p, left, "64-bit extensions\n");
304 edd_show_info_flags(struct edd_device *edev, char *buf)
306 struct edd_info *info;
310 info = edd_dev_get_info(edev);
314 if (info->params.info_flags & EDD_INFO_DMA_BOUNDARY_ERROR_TRANSPARENT)
315 p += scnprintf(p, left, "DMA boundary error transparent\n");
316 if (info->params.info_flags & EDD_INFO_GEOMETRY_VALID)
317 p += scnprintf(p, left, "geometry valid\n");
318 if (info->params.info_flags & EDD_INFO_REMOVABLE)
319 p += scnprintf(p, left, "removable\n");
320 if (info->params.info_flags & EDD_INFO_WRITE_VERIFY)
321 p += scnprintf(p, left, "write verify\n");
322 if (info->params.info_flags & EDD_INFO_MEDIA_CHANGE_NOTIFICATION)
323 p += scnprintf(p, left, "media change notification\n");
324 if (info->params.info_flags & EDD_INFO_LOCKABLE)
325 p += scnprintf(p, left, "lockable\n");
326 if (info->params.info_flags & EDD_INFO_NO_MEDIA_PRESENT)
327 p += scnprintf(p, left, "no media present\n");
328 if (info->params.info_flags & EDD_INFO_USE_INT13_FN50)
329 p += scnprintf(p, left, "use int13 fn50\n");
334 edd_show_legacy_max_cylinder(struct edd_device *edev, char *buf)
336 struct edd_info *info;
340 info = edd_dev_get_info(edev);
344 p += scnprintf(p, left, "%u\n", info->legacy_max_cylinder);
349 edd_show_legacy_max_head(struct edd_device *edev, char *buf)
351 struct edd_info *info;
355 info = edd_dev_get_info(edev);
359 p += scnprintf(p, left, "%u\n", info->legacy_max_head);
364 edd_show_legacy_sectors_per_track(struct edd_device *edev, char *buf)
366 struct edd_info *info;
370 info = edd_dev_get_info(edev);
374 p += scnprintf(p, left, "%u\n", info->legacy_sectors_per_track);
379 edd_show_default_cylinders(struct edd_device *edev, char *buf)
381 struct edd_info *info;
385 info = edd_dev_get_info(edev);
389 p += scnprintf(p, left, "%u\n", info->params.num_default_cylinders);
394 edd_show_default_heads(struct edd_device *edev, char *buf)
396 struct edd_info *info;
400 info = edd_dev_get_info(edev);
404 p += scnprintf(p, left, "%u\n", info->params.num_default_heads);
409 edd_show_default_sectors_per_track(struct edd_device *edev, char *buf)
411 struct edd_info *info;
415 info = edd_dev_get_info(edev);
419 p += scnprintf(p, left, "%u\n", info->params.sectors_per_track);
424 edd_show_sectors(struct edd_device *edev, char *buf)
426 struct edd_info *info;
430 info = edd_dev_get_info(edev);
434 p += scnprintf(p, left, "%llu\n", info->params.number_of_sectors);
440 * Some device instances may not have all the above attributes,
441 * or the attribute values may be meaningless (i.e. if
442 * the device is < EDD 3.0, it won't have host_bus and interface
443 * information), so don't bother making files for them. Likewise
444 * if the default_{cylinders,heads,sectors_per_track} values
445 * are zero, the BIOS doesn't provide sane values, don't bother
446 * creating files for them either.
450 edd_has_legacy_max_cylinder(struct edd_device *edev)
452 struct edd_info *info;
455 info = edd_dev_get_info(edev);
458 return info->legacy_max_cylinder > 0;
462 edd_has_legacy_max_head(struct edd_device *edev)
464 struct edd_info *info;
467 info = edd_dev_get_info(edev);
470 return info->legacy_max_head > 0;
474 edd_has_legacy_sectors_per_track(struct edd_device *edev)
476 struct edd_info *info;
479 info = edd_dev_get_info(edev);
482 return info->legacy_sectors_per_track > 0;
486 edd_has_default_cylinders(struct edd_device *edev)
488 struct edd_info *info;
491 info = edd_dev_get_info(edev);
494 return info->params.num_default_cylinders > 0;
498 edd_has_default_heads(struct edd_device *edev)
500 struct edd_info *info;
503 info = edd_dev_get_info(edev);
506 return info->params.num_default_heads > 0;
510 edd_has_default_sectors_per_track(struct edd_device *edev)
512 struct edd_info *info;
515 info = edd_dev_get_info(edev);
518 return info->params.sectors_per_track > 0;
522 edd_has_edd30(struct edd_device *edev)
524 struct edd_info *info;
530 info = edd_dev_get_info(edev);
534 if (!(info->params.key == 0xBEDD || info->params.key == 0xDDBE)) {
539 /* We support only T13 spec */
540 if (info->params.device_path_info_length != 44)
543 for (i = 30; i < info->params.device_path_info_length + 30; i++)
544 csum += *(((u8 *)&info->params) + i);
553 static EDD_DEVICE_ATTR(raw_data, 0444, edd_show_raw_data, edd_has_edd_info);
554 static EDD_DEVICE_ATTR(version, 0444, edd_show_version, edd_has_edd_info);
555 static EDD_DEVICE_ATTR(extensions, 0444, edd_show_extensions, edd_has_edd_info);
556 static EDD_DEVICE_ATTR(info_flags, 0444, edd_show_info_flags, edd_has_edd_info);
557 static EDD_DEVICE_ATTR(sectors, 0444, edd_show_sectors, edd_has_edd_info);
558 static EDD_DEVICE_ATTR(legacy_max_cylinder, 0444,
559 edd_show_legacy_max_cylinder,
560 edd_has_legacy_max_cylinder);
561 static EDD_DEVICE_ATTR(legacy_max_head, 0444, edd_show_legacy_max_head,
562 edd_has_legacy_max_head);
563 static EDD_DEVICE_ATTR(legacy_sectors_per_track, 0444,
564 edd_show_legacy_sectors_per_track,
565 edd_has_legacy_sectors_per_track);
566 static EDD_DEVICE_ATTR(default_cylinders, 0444, edd_show_default_cylinders,
567 edd_has_default_cylinders);
568 static EDD_DEVICE_ATTR(default_heads, 0444, edd_show_default_heads,
569 edd_has_default_heads);
570 static EDD_DEVICE_ATTR(default_sectors_per_track, 0444,
571 edd_show_default_sectors_per_track,
572 edd_has_default_sectors_per_track);
573 static EDD_DEVICE_ATTR(interface, 0444, edd_show_interface, edd_has_edd30);
574 static EDD_DEVICE_ATTR(host_bus, 0444, edd_show_host_bus, edd_has_edd30);
575 static EDD_DEVICE_ATTR(mbr_signature, 0444, edd_show_mbr_signature, edd_has_mbr_signature);
578 /* These are default attributes that are added for every edd
579 * device discovered. There are none.
581 static struct attribute * def_attrs[] = {
585 /* These attributes are conditional and only added for some devices. */
586 static struct edd_attribute * edd_attrs[] = {
589 &edd_attr_extensions,
590 &edd_attr_info_flags,
592 &edd_attr_legacy_max_cylinder,
593 &edd_attr_legacy_max_head,
594 &edd_attr_legacy_sectors_per_track,
595 &edd_attr_default_cylinders,
596 &edd_attr_default_heads,
597 &edd_attr_default_sectors_per_track,
600 &edd_attr_mbr_signature,
605 * edd_release - free edd structure
606 * @kobj: kobject of edd structure
608 * This is called when the refcount of the edd structure
609 * reaches 0. This should happen right after we unregister,
610 * but just in case, we use the release callback anyway.
613 static void edd_release(struct kobject * kobj)
615 struct edd_device * dev = to_edd_device(kobj);
619 static struct kobj_type edd_ktype = {
620 .release = edd_release,
621 .sysfs_ops = &edd_attr_ops,
622 .default_attrs = def_attrs,
625 static struct kset *edd_kset;
629 * edd_dev_is_type() - is this EDD device a 'type' device?
630 * @edev: target edd_device
631 * @type: a host bus or interface identifier string per the EDD spec
633 * Returns 1 (TRUE) if it is a 'type' device, 0 otherwise.
636 edd_dev_is_type(struct edd_device *edev, const char *type)
638 struct edd_info *info;
641 info = edd_dev_get_info(edev);
644 if (!strncmp(info->params.host_bus_type, type, strlen(type)) ||
645 !strncmp(info->params.interface_type, type, strlen(type)))
652 * edd_get_pci_dev() - finds pci_dev that matches edev
655 * Returns pci_dev if found, or NULL
657 static struct pci_dev *
658 edd_get_pci_dev(struct edd_device *edev)
660 struct edd_info *info = edd_dev_get_info(edev);
662 if (edd_dev_is_type(edev, "PCI") || edd_dev_is_type(edev, "XPRS")) {
663 return pci_get_domain_bus_and_slot(0,
664 info->params.interface_path.pci.bus,
665 PCI_DEVFN(info->params.interface_path.pci.slot,
666 info->params.interface_path.pci.function));
672 edd_create_symlink_to_pcidev(struct edd_device *edev)
675 struct pci_dev *pci_dev = edd_get_pci_dev(edev);
679 ret = sysfs_create_link(&edev->kobj,&pci_dev->dev.kobj,"pci_dev");
680 pci_dev_put(pci_dev);
685 edd_device_unregister(struct edd_device *edev)
687 kobject_put(&edev->kobj);
690 static void edd_populate_dir(struct edd_device * edev)
692 struct edd_attribute * attr;
696 for (i = 0; (attr = edd_attrs[i]) && !error; i++) {
698 (attr->test && attr->test(edev)))
699 error = sysfs_create_file(&edev->kobj,&attr->attr);
703 edd_create_symlink_to_pcidev(edev);
708 edd_device_register(struct edd_device *edev, int i)
714 edd_dev_set_info(edev, i);
715 edev->kobj.kset = edd_kset;
716 error = kobject_init_and_add(&edev->kobj, &edd_ktype, NULL,
717 "int13_dev%02x", 0x80 + i);
719 edd_populate_dir(edev);
720 kobject_uevent(&edev->kobj, KOBJ_ADD);
725 static inline int edd_num_devices(void)
727 return max_t(unsigned char,
728 min_t(unsigned char, EDD_MBR_SIG_MAX, edd.mbr_signature_nr),
729 min_t(unsigned char, EDDMAXNR, edd.edd_info_nr));
733 * edd_init() - creates sysfs tree of EDD data
740 struct edd_device *edev;
742 if (!edd_num_devices())
745 printk(KERN_INFO "BIOS EDD facility v%s %s, %d devices found\n",
746 EDD_VERSION, EDD_DATE, edd_num_devices());
748 edd_kset = kset_create_and_add("edd", NULL, firmware_kobj);
752 for (i = 0; i < edd_num_devices(); i++) {
753 edev = kzalloc(sizeof (*edev), GFP_KERNEL);
759 rc = edd_device_register(edev, i);
764 edd_devices[i] = edev;
771 edd_device_unregister(edd_devices[i]);
772 kset_unregister(edd_kset);
780 struct edd_device *edev;
782 for (i = 0; i < edd_num_devices(); i++) {
783 if ((edev = edd_devices[i]))
784 edd_device_unregister(edev);
786 kset_unregister(edd_kset);
789 late_initcall(edd_init);
790 module_exit(edd_exit);