2 * linux/arch/i386/kernel/edd.c
3 * Copyright (C) 2002, 2003 Dell Inc.
4 * by Matt Domsch <Matt_Domsch@dell.com>
5 * disk80 signature by Matt Domsch, Andrew Wilks, and Sandeep K. Shandilya
7 * BIOS Enhanced Disk Drive Services (EDD)
8 * conformant to T13 Committee www.t13.org
9 * projects 1572D, 1484D, 1386D, 1226DT
11 * This code takes information provided by BIOS EDD calls
12 * fn41 - Check Extensions Present and
13 * fn48 - Get Device Parametes with EDD extensions
14 * made in setup.S, copied to safe structures in setup.c,
15 * and presents it in sysfs.
17 * Please see http://domsch.com/linux/edd30/results.html for
18 * the list of BIOSs which have been reported to implement EDD.
19 * If you don't see yours listed, please send a report as described there.
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License v2.0 as published by
23 * the Free Software Foundation
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
34 * - refcounting of struct device objects could be improved.
37 * - Add IDE and USB disk device support
38 * - move edd.[ch] to better locations if/when one is decided
41 #include <linux/module.h>
42 #include <linux/string.h>
43 #include <linux/types.h>
44 #include <linux/init.h>
45 #include <linux/stat.h>
46 #include <linux/err.h>
47 #include <linux/ctype.h>
48 #include <linux/slab.h>
49 #include <linux/limits.h>
50 #include <linux/device.h>
51 #include <linux/pci.h>
52 #include <linux/device.h>
53 #include <linux/blkdev.h>
55 /* FIXME - this really belongs in include/scsi/scsi.h */
56 #include <../drivers/scsi/scsi.h>
57 #include <../drivers/scsi/hosts.h>
59 MODULE_AUTHOR("Matt Domsch <Matt_Domsch@Dell.com>");
60 MODULE_DESCRIPTION("sysfs interface to BIOS EDD information");
61 MODULE_LICENSE("GPL");
63 #define EDD_VERSION "0.12 2004-Jan-26"
64 #define EDD_DEVICE_NAME_SIZE 16
65 #define REPORT_URL "http://linux.dell.com/edd/results.html"
67 #define left (PAGE_SIZE - (p - buf) - 1)
70 struct edd_info *info;
74 struct edd_attribute {
75 struct attribute attr;
76 ssize_t(*show) (struct edd_device * edev, char *buf);
77 int (*test) (struct edd_device * edev);
80 /* forward declarations */
81 static int edd_dev_is_type(struct edd_device *edev, const char *type);
82 static struct pci_dev *edd_get_pci_dev(struct edd_device *edev);
84 static struct edd_device *edd_devices[EDDMAXNR];
86 #define EDD_DEVICE_ATTR(_name,_mode,_show,_test) \
87 struct edd_attribute edd_attr_##_name = { \
88 .attr = {.name = __stringify(_name), .mode = _mode }, \
93 static inline struct edd_info *
94 edd_dev_get_info(struct edd_device *edev)
100 edd_dev_set_info(struct edd_device *edev, struct edd_info *info)
105 #define to_edd_attr(_attr) container_of(_attr,struct edd_attribute,attr)
106 #define to_edd_device(obj) container_of(obj,struct edd_device,kobj)
109 edd_attr_show(struct kobject * kobj, struct attribute *attr, char *buf)
111 struct edd_device *dev = to_edd_device(kobj);
112 struct edd_attribute *edd_attr = to_edd_attr(attr);
116 ret = edd_attr->show(dev, buf);
120 static struct sysfs_ops edd_attr_ops = {
121 .show = edd_attr_show,
125 edd_show_host_bus(struct edd_device *edev, char *buf)
127 struct edd_info *info = edd_dev_get_info(edev);
131 if (!edev || !info || !buf) {
135 for (i = 0; i < 4; i++) {
136 if (isprint(info->params.host_bus_type[i])) {
137 p += snprintf(p, left, "%c", info->params.host_bus_type[i]);
139 p += snprintf(p, left, " ");
143 if (!strncmp(info->params.host_bus_type, "ISA", 3)) {
144 p += snprintf(p, left, "\tbase_address: %x\n",
145 info->params.interface_path.isa.base_address);
146 } else if (!strncmp(info->params.host_bus_type, "PCIX", 4) ||
147 !strncmp(info->params.host_bus_type, "PCI", 3)) {
148 p += snprintf(p, left,
149 "\t%02x:%02x.%d channel: %u\n",
150 info->params.interface_path.pci.bus,
151 info->params.interface_path.pci.slot,
152 info->params.interface_path.pci.function,
153 info->params.interface_path.pci.channel);
154 } else if (!strncmp(info->params.host_bus_type, "IBND", 4) ||
155 !strncmp(info->params.host_bus_type, "XPRS", 4) ||
156 !strncmp(info->params.host_bus_type, "HTPT", 4)) {
157 p += snprintf(p, left,
159 info->params.interface_path.ibnd.reserved);
162 p += snprintf(p, left, "\tunknown: %llx\n",
163 info->params.interface_path.unknown.reserved);
169 edd_show_interface(struct edd_device *edev, char *buf)
171 struct edd_info *info = edd_dev_get_info(edev);
175 if (!edev || !info || !buf) {
179 for (i = 0; i < 8; i++) {
180 if (isprint(info->params.interface_type[i])) {
181 p += snprintf(p, left, "%c", info->params.interface_type[i]);
183 p += snprintf(p, left, " ");
186 if (!strncmp(info->params.interface_type, "ATAPI", 5)) {
187 p += snprintf(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 += snprintf(p, left, "\tdevice: %u\n",
192 info->params.device_path.ata.device);
193 } else if (!strncmp(info->params.interface_type, "SCSI", 4)) {
194 p += snprintf(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 += snprintf(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 += snprintf(p, left, "\teui: %llx\n",
202 info->params.device_path.i1394.eui);
203 } else if (!strncmp(info->params.interface_type, "FIBRE", 5)) {
204 p += snprintf(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 += snprintf(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 += snprintf(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 += snprintf(p, left, "\tdevice: %u\n",
215 info->params.device_path.sata.device);
217 p += snprintf(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
228 * Returns: number of bytes written, or -EINVAL on failure
231 edd_show_raw_data(struct edd_device *edev, char *buf)
233 struct edd_info *info = edd_dev_get_info(edev);
234 ssize_t len = sizeof (*info) - 4;
235 if (!edev || !info || !buf) {
239 if (!(info->params.key == 0xBEDD || info->params.key == 0xDDBE))
240 len = info->params.length;
242 /* In case of buggy BIOSs */
243 if (len > (sizeof(*info) - 4))
244 len = sizeof(*info) - 4;
246 memcpy(buf, ((char *)info) + 4, len);
251 edd_show_version(struct edd_device *edev, char *buf)
253 struct edd_info *info = edd_dev_get_info(edev);
255 if (!edev || !info || !buf) {
259 p += snprintf(p, left, "0x%02x\n", info->version);
264 edd_show_disk80_sig(struct edd_device *edev, char *buf)
267 p += snprintf(p, left, "0x%08x\n", edd_disk80_sig);
272 edd_show_extensions(struct edd_device *edev, char *buf)
274 struct edd_info *info = edd_dev_get_info(edev);
276 if (!edev || !info || !buf) {
280 if (info->interface_support & EDD_EXT_FIXED_DISK_ACCESS) {
281 p += snprintf(p, left, "Fixed disk access\n");
283 if (info->interface_support & EDD_EXT_DEVICE_LOCKING_AND_EJECTING) {
284 p += snprintf(p, left, "Device locking and ejecting\n");
286 if (info->interface_support & EDD_EXT_ENHANCED_DISK_DRIVE_SUPPORT) {
287 p += snprintf(p, left, "Enhanced Disk Drive support\n");
289 if (info->interface_support & EDD_EXT_64BIT_EXTENSIONS) {
290 p += snprintf(p, left, "64-bit extensions\n");
296 edd_show_info_flags(struct edd_device *edev, char *buf)
298 struct edd_info *info = edd_dev_get_info(edev);
300 if (!edev || !info || !buf) {
304 if (info->params.info_flags & EDD_INFO_DMA_BOUNDARY_ERROR_TRANSPARENT)
305 p += snprintf(p, left, "DMA boundary error transparent\n");
306 if (info->params.info_flags & EDD_INFO_GEOMETRY_VALID)
307 p += snprintf(p, left, "geometry valid\n");
308 if (info->params.info_flags & EDD_INFO_REMOVABLE)
309 p += snprintf(p, left, "removable\n");
310 if (info->params.info_flags & EDD_INFO_WRITE_VERIFY)
311 p += snprintf(p, left, "write verify\n");
312 if (info->params.info_flags & EDD_INFO_MEDIA_CHANGE_NOTIFICATION)
313 p += snprintf(p, left, "media change notification\n");
314 if (info->params.info_flags & EDD_INFO_LOCKABLE)
315 p += snprintf(p, left, "lockable\n");
316 if (info->params.info_flags & EDD_INFO_NO_MEDIA_PRESENT)
317 p += snprintf(p, left, "no media present\n");
318 if (info->params.info_flags & EDD_INFO_USE_INT13_FN50)
319 p += snprintf(p, left, "use int13 fn50\n");
324 edd_show_default_cylinders(struct edd_device *edev, char *buf)
326 struct edd_info *info = edd_dev_get_info(edev);
328 if (!edev || !info || !buf) {
332 p += snprintf(p, left, "0x%x\n", info->params.num_default_cylinders);
337 edd_show_default_heads(struct edd_device *edev, char *buf)
339 struct edd_info *info = edd_dev_get_info(edev);
341 if (!edev || !info || !buf) {
345 p += snprintf(p, left, "0x%x\n", info->params.num_default_heads);
350 edd_show_default_sectors_per_track(struct edd_device *edev, char *buf)
352 struct edd_info *info = edd_dev_get_info(edev);
354 if (!edev || !info || !buf) {
358 p += snprintf(p, left, "0x%x\n", info->params.sectors_per_track);
363 edd_show_sectors(struct edd_device *edev, char *buf)
365 struct edd_info *info = edd_dev_get_info(edev);
367 if (!edev || !info || !buf) {
371 p += snprintf(p, left, "0x%llx\n", info->params.number_of_sectors);
377 * Some device instances may not have all the above attributes,
378 * or the attribute values may be meaningless (i.e. if
379 * the device is < EDD 3.0, it won't have host_bus and interface
380 * information), so don't bother making files for them. Likewise
381 * if the default_{cylinders,heads,sectors_per_track} values
382 * are zero, the BIOS doesn't provide sane values, don't bother
383 * creating files for them either.
387 edd_has_default_cylinders(struct edd_device *edev)
389 struct edd_info *info = edd_dev_get_info(edev);
392 return info->params.num_default_cylinders > 0;
396 edd_has_default_heads(struct edd_device *edev)
398 struct edd_info *info = edd_dev_get_info(edev);
401 return info->params.num_default_heads > 0;
405 edd_has_default_sectors_per_track(struct edd_device *edev)
407 struct edd_info *info = edd_dev_get_info(edev);
410 return info->params.sectors_per_track > 0;
414 edd_has_edd30(struct edd_device *edev)
416 struct edd_info *info = edd_dev_get_info(edev);
417 int i, nonzero_path = 0;
423 if (!(info->params.key == 0xBEDD || info->params.key == 0xDDBE)) {
427 for (i = 30; i <= 73; i++) {
428 c = *(((uint8_t *) info) + i + 4);
442 edd_has_disk80_sig(struct edd_device *edev)
444 struct edd_info *info = edd_dev_get_info(edev);
447 return info->device == 0x80;
450 static EDD_DEVICE_ATTR(raw_data, 0444, edd_show_raw_data, NULL);
451 static EDD_DEVICE_ATTR(version, 0444, edd_show_version, NULL);
452 static EDD_DEVICE_ATTR(extensions, 0444, edd_show_extensions, NULL);
453 static EDD_DEVICE_ATTR(info_flags, 0444, edd_show_info_flags, NULL);
454 static EDD_DEVICE_ATTR(sectors, 0444, edd_show_sectors, NULL);
455 static EDD_DEVICE_ATTR(default_cylinders, 0444, edd_show_default_cylinders,
456 edd_has_default_cylinders);
457 static EDD_DEVICE_ATTR(default_heads, 0444, edd_show_default_heads,
458 edd_has_default_heads);
459 static EDD_DEVICE_ATTR(default_sectors_per_track, 0444,
460 edd_show_default_sectors_per_track,
461 edd_has_default_sectors_per_track);
462 static EDD_DEVICE_ATTR(interface, 0444, edd_show_interface, edd_has_edd30);
463 static EDD_DEVICE_ATTR(host_bus, 0444, edd_show_host_bus, edd_has_edd30);
464 static EDD_DEVICE_ATTR(mbr_signature, 0444, edd_show_disk80_sig, edd_has_disk80_sig);
467 /* These are default attributes that are added for every edd
470 static struct attribute * def_attrs[] = {
471 &edd_attr_raw_data.attr,
472 &edd_attr_version.attr,
473 &edd_attr_extensions.attr,
474 &edd_attr_info_flags.attr,
475 &edd_attr_sectors.attr,
479 /* These attributes are conditional and only added for some devices. */
480 static struct edd_attribute * edd_attrs[] = {
481 &edd_attr_default_cylinders,
482 &edd_attr_default_heads,
483 &edd_attr_default_sectors_per_track,
486 &edd_attr_mbr_signature,
491 * edd_release - free edd structure
492 * @kobj: kobject of edd structure
494 * This is called when the refcount of the edd structure
495 * reaches 0. This should happen right after we unregister,
496 * but just in case, we use the release callback anyway.
499 static void edd_release(struct kobject * kobj)
501 struct edd_device * dev = to_edd_device(kobj);
505 static struct kobj_type ktype_edd = {
506 .release = edd_release,
507 .sysfs_ops = &edd_attr_ops,
508 .default_attrs = def_attrs,
511 static decl_subsys(edd,&ktype_edd,NULL);
515 * edd_dev_is_type() - is this EDD device a 'type' device?
517 * @type - a host bus or interface identifier string per the EDD spec
519 * Returns 1 (TRUE) if it is a 'type' device, 0 otherwise.
522 edd_dev_is_type(struct edd_device *edev, const char *type)
524 struct edd_info *info = edd_dev_get_info(edev);
526 if (edev && type && info) {
527 if (!strncmp(info->params.host_bus_type, type, strlen(type)) ||
528 !strncmp(info->params.interface_type, type, strlen(type)))
535 * edd_get_pci_dev() - finds pci_dev that matches edev
538 * Returns pci_dev if found, or NULL
540 static struct pci_dev *
541 edd_get_pci_dev(struct edd_device *edev)
543 struct edd_info *info = edd_dev_get_info(edev);
545 if (edd_dev_is_type(edev, "PCI")) {
546 return pci_find_slot(info->params.interface_path.pci.bus,
547 PCI_DEVFN(info->params.interface_path.pci.slot,
548 info->params.interface_path.pci.
555 edd_create_symlink_to_pcidev(struct edd_device *edev)
558 struct pci_dev *pci_dev = edd_get_pci_dev(edev);
561 return sysfs_create_link(&edev->kobj,&pci_dev->dev.kobj,"pci_dev");
565 * FIXME - as of 15-Jan-2003, there are some non-"scsi_device"s on the
566 * scsi_bus list. The following functions could possibly mis-access
567 * memory in that case. This is actually a problem with the SCSI
568 * layer, which is being addressed there. Until then, don't use the
573 #undef CONFIG_SCSI_MODULE
574 #if defined(CONFIG_SCSI) || defined(CONFIG_SCSI_MODULE)
576 struct edd_match_data {
577 struct edd_device * edev;
578 struct scsi_device * sd;
582 * edd_match_scsidev()
583 * @edev - EDD device is a known SCSI device
584 * @sd - scsi_device with host who's parent is a PCI controller
586 * returns 1 if a match is found, 0 if not.
588 static int edd_match_scsidev(struct device * dev, void * d)
590 struct edd_match_data * data = (struct edd_match_data *)d;
591 struct edd_info *info = edd_dev_get_info(data->edev);
592 struct scsi_device * sd = to_scsi_device(dev);
595 if ((sd->channel == info->params.interface_path.pci.channel) &&
596 (sd->id == info->params.device_path.scsi.id) &&
597 (sd->lun == info->params.device_path.scsi.lun)) {
606 * edd_find_matching_device()
607 * @edev - edd_device to match
609 * Search the SCSI devices for a drive that matches the EDD
610 * device descriptor we have. If we find a match, return it,
611 * otherwise, return NULL.
614 static struct scsi_device *
615 edd_find_matching_scsi_device(struct edd_device *edev)
617 struct edd_match_data data;
618 struct bus_type * scsi_bus = find_bus("scsi");
626 if (edd_dev_is_type(edev, "SCSI")) {
627 if (bus_for_each_dev(scsi_bus,NULL,&data,edd_match_scsidev))
634 edd_create_symlink_to_scsidev(struct edd_device *edev)
636 struct pci_dev *pci_dev;
639 pci_dev = edd_get_pci_dev(edev);
641 struct scsi_device * sdev = edd_find_matching_scsi_device(edev);
642 if (sdev && get_device(&sdev->sdev_driverfs_dev)) {
643 rc = sysfs_create_link(&edev->kobj,
644 &sdev->sdev_driverfs_dev.kobj,
646 put_device(&sdev->sdev_driverfs_dev);
654 edd_create_symlink_to_scsidev(struct edd_device *edev)
662 edd_device_unregister(struct edd_device *edev)
664 kobject_unregister(&edev->kobj);
667 static void edd_populate_dir(struct edd_device * edev)
669 struct edd_attribute * attr;
673 for (i = 0; (attr = edd_attrs[i]) && !error; i++) {
675 (attr->test && attr->test(edev)))
676 error = sysfs_create_file(&edev->kobj,&attr->attr);
680 edd_create_symlink_to_pcidev(edev);
681 edd_create_symlink_to_scsidev(edev);
686 edd_device_register(struct edd_device *edev, int i)
692 memset(edev, 0, sizeof (*edev));
693 edd_dev_set_info(edev, &edd[i]);
694 snprintf(edev->kobj.name, EDD_DEVICE_NAME_SIZE, "int13_dev%02x",
696 kobj_set_kset_s(edev,edd_subsys);
697 error = kobject_register(&edev->kobj);
699 edd_populate_dir(edev);
704 * edd_init() - creates sysfs tree of EDD data
706 * This assumes that eddnr and edd were
707 * assigned in setup.c already.
714 struct edd_device *edev;
716 printk(KERN_INFO "BIOS EDD facility v%s, %d devices found\n",
718 printk(KERN_INFO "Please report your BIOS at %s\n", REPORT_URL);
721 printk(KERN_INFO "EDD information not available.\n");
725 rc = firmware_register(&edd_subsys);
729 for (i = 0; i < eddnr && i < EDDMAXNR && !rc; i++) {
730 edev = kmalloc(sizeof (*edev), GFP_KERNEL);
734 rc = edd_device_register(edev, i);
739 edd_devices[i] = edev;
743 firmware_unregister(&edd_subsys);
751 struct edd_device *edev;
753 for (i = 0; i < eddnr && i < EDDMAXNR; i++) {
754 if ((edev = edd_devices[i]))
755 edd_device_unregister(edev);
757 firmware_unregister(&edd_subsys);
760 late_initcall(edd_init);
761 module_exit(edd_exit);