2 * Copyright 2008 ioogle, Inc. All rights reserved.
3 * Released under GPL v2.
5 * Libata transport class.
7 * The ATA transport class contains common code to deal with ATA HBAs,
8 * an approximated representation of ATA topologies in the driver model,
9 * and various sysfs attributes to expose these topologies and management
10 * interfaces to user-space.
12 * There are 3 objects defined in in this class:
16 * Each port has a link object. Each link can have up to two devices for PATA
17 * and generally one for SATA.
18 * If there is SATA port multiplier [PMP], 15 additional ata_link object are
21 * These objects are created when the ata host is initialized and when a PMP is
22 * found. They are removed only when the HBA is removed, cleaned before the
27 #include <linux/kernel.h>
28 #include <linux/blkdev.h>
29 #include <linux/spinlock.h>
30 #include <scsi/scsi_transport.h>
31 #include <linux/libata.h>
32 #include <linux/hdreg.h>
33 #include <linux/uaccess.h>
36 #include "libata-transport.h"
38 #define ATA_PORT_ATTRS 2
39 #define ATA_LINK_ATTRS 3
40 #define ATA_DEV_ATTRS 9
42 struct scsi_transport_template;
43 struct scsi_transport_template *ata_scsi_transport_template;
46 struct scsi_transport_template t;
48 struct device_attribute private_port_attrs[ATA_PORT_ATTRS];
49 struct device_attribute private_link_attrs[ATA_LINK_ATTRS];
50 struct device_attribute private_dev_attrs[ATA_DEV_ATTRS];
52 struct transport_container link_attr_cont;
53 struct transport_container dev_attr_cont;
56 * The array of null terminated pointers to attributes
57 * needed by scsi_sysfs.c
59 struct device_attribute *link_attrs[ATA_LINK_ATTRS + 1];
60 struct device_attribute *port_attrs[ATA_PORT_ATTRS + 1];
61 struct device_attribute *dev_attrs[ATA_DEV_ATTRS + 1];
63 #define to_ata_internal(tmpl) container_of(tmpl, struct ata_internal, t)
66 #define tdev_to_device(d) \
67 container_of((d), struct ata_device, tdev)
68 #define transport_class_to_dev(dev) \
69 tdev_to_device((dev)->parent)
71 #define tdev_to_link(d) \
72 container_of((d), struct ata_link, tdev)
73 #define transport_class_to_link(dev) \
74 tdev_to_link((dev)->parent)
76 #define tdev_to_port(d) \
77 container_of((d), struct ata_port, tdev)
78 #define transport_class_to_port(dev) \
79 tdev_to_port((dev)->parent)
82 /* Device objects are always created whit link objects */
83 static int ata_tdev_add(struct ata_device *dev);
84 static void ata_tdev_delete(struct ata_device *dev);
88 * Hack to allow attributes of the same name in different objects.
90 #define ATA_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
91 struct device_attribute device_attr_##_prefix##_##_name = \
92 __ATTR(_name,_mode,_show,_store)
94 #define ata_bitfield_name_match(title, table) \
96 get_ata_##title##_names(u32 table_key, char *buf) \
102 for (i = 0; i < ARRAY_SIZE(table); i++) { \
103 if (table[i].value & table_key) { \
104 len += sprintf(buf + len, "%s%s", \
105 prefix, table[i].name); \
109 len += sprintf(buf + len, "\n"); \
113 #define ata_bitfield_name_search(title, table) \
115 get_ata_##title##_names(u32 table_key, char *buf) \
120 for (i = 0; i < ARRAY_SIZE(table); i++) { \
121 if (table[i].value == table_key) { \
122 len += sprintf(buf + len, "%s", \
127 len += sprintf(buf + len, "\n"); \
134 } ata_class_names[] = {
135 { ATA_DEV_UNKNOWN, "unknown" },
136 { ATA_DEV_ATA, "ata" },
137 { ATA_DEV_ATA_UNSUP, "ata" },
138 { ATA_DEV_ATAPI, "atapi" },
139 { ATA_DEV_ATAPI_UNSUP, "atapi" },
140 { ATA_DEV_PMP, "pmp" },
141 { ATA_DEV_PMP_UNSUP, "pmp" },
142 { ATA_DEV_SEMB, "semb" },
143 { ATA_DEV_SEMB_UNSUP, "semb" },
144 { ATA_DEV_NONE, "none" }
146 ata_bitfield_name_search(class, ata_class_names)
152 } ata_err_names[] = {
153 { AC_ERR_DEV, "DeviceError" },
154 { AC_ERR_HSM, "HostStateMachineError" },
155 { AC_ERR_TIMEOUT, "Timeout" },
156 { AC_ERR_MEDIA, "MediaError" },
157 { AC_ERR_ATA_BUS, "BusError" },
158 { AC_ERR_HOST_BUS, "HostBusError" },
159 { AC_ERR_SYSTEM, "SystemError" },
160 { AC_ERR_INVALID, "InvalidArg" },
161 { AC_ERR_OTHER, "Unknown" },
162 { AC_ERR_NODEV_HINT, "NoDeviceHint" },
163 { AC_ERR_NCQ, "NCQError" }
165 ata_bitfield_name_match(err, ata_err_names)
170 } ata_xfer_names[] = {
171 { XFER_UDMA_7, "XFER_UDMA_7" },
172 { XFER_UDMA_6, "XFER_UDMA_6" },
173 { XFER_UDMA_5, "XFER_UDMA_5" },
174 { XFER_UDMA_4, "XFER_UDMA_4" },
175 { XFER_UDMA_3, "XFER_UDMA_3" },
176 { XFER_UDMA_2, "XFER_UDMA_2" },
177 { XFER_UDMA_1, "XFER_UDMA_1" },
178 { XFER_UDMA_0, "XFER_UDMA_0" },
179 { XFER_MW_DMA_4, "XFER_MW_DMA_4" },
180 { XFER_MW_DMA_3, "XFER_MW_DMA_3" },
181 { XFER_MW_DMA_2, "XFER_MW_DMA_2" },
182 { XFER_MW_DMA_1, "XFER_MW_DMA_1" },
183 { XFER_MW_DMA_0, "XFER_MW_DMA_0" },
184 { XFER_SW_DMA_2, "XFER_SW_DMA_2" },
185 { XFER_SW_DMA_1, "XFER_SW_DMA_1" },
186 { XFER_SW_DMA_0, "XFER_SW_DMA_0" },
187 { XFER_PIO_6, "XFER_PIO_6" },
188 { XFER_PIO_5, "XFER_PIO_5" },
189 { XFER_PIO_4, "XFER_PIO_4" },
190 { XFER_PIO_3, "XFER_PIO_3" },
191 { XFER_PIO_2, "XFER_PIO_2" },
192 { XFER_PIO_1, "XFER_PIO_1" },
193 { XFER_PIO_0, "XFER_PIO_0" },
194 { XFER_PIO_SLOW, "XFER_PIO_SLOW" }
196 ata_bitfield_name_match(xfer,ata_xfer_names)
199 * ATA Port attributes
201 #define ata_port_show_simple(field, name, format_string, cast) \
203 show_ata_port_##name(struct device *dev, \
204 struct device_attribute *attr, char *buf) \
206 struct ata_port *ap = transport_class_to_port(dev); \
208 return snprintf(buf, 20, format_string, cast ap->field); \
211 #define ata_port_simple_attr(field, name, format_string, type) \
212 ata_port_show_simple(field, name, format_string, (type)) \
213 static DEVICE_ATTR(name, S_IRUGO, show_ata_port_##name, NULL)
215 ata_port_simple_attr(nr_pmp_links, nr_pmp_links, "%d\n", int);
216 ata_port_simple_attr(stats.idle_irq, idle_irq, "%ld\n", unsigned long);
218 static DECLARE_TRANSPORT_CLASS(ata_port_class,
219 "ata_port", NULL, NULL, NULL);
221 static void ata_tport_release(struct device *dev)
223 put_device(dev->parent);
227 * ata_is_port -- check if a struct device represents a ATA port
228 * @dev: device to check
231 * %1 if the device represents a ATA Port, %0 else
233 int ata_is_port(const struct device *dev)
235 return dev->release == ata_tport_release;
238 static int ata_tport_match(struct attribute_container *cont,
241 if (!ata_is_port(dev))
243 return &ata_scsi_transport_template->host_attrs.ac == cont;
247 * ata_tport_delete -- remove ATA PORT
248 * @port: ATA PORT to remove
250 * Removes the specified ATA PORT. Remove the associated link as well.
252 void ata_tport_delete(struct ata_port *ap)
254 struct device *dev = &ap->tdev;
256 ata_tlink_delete(&ap->link);
258 transport_remove_device(dev);
260 transport_destroy_device(dev);
264 /** ata_tport_add - initialize a transport ATA port structure
266 * @parent: parent device
267 * @ap: existing ata_port structure
269 * Initialize a ATA port structure for sysfs. It will be added to the device
270 * tree below the device specified by @parent which could be a PCI device.
272 * Returns %0 on success
274 int ata_tport_add(struct device *parent,
278 struct device *dev = &ap->tdev;
280 device_initialize(dev);
282 dev->parent = get_device(parent);
283 dev->release = ata_tport_release;
284 dev_set_name(dev, "ata%d", ap->print_id);
285 transport_setup_device(dev);
286 error = device_add(dev);
291 transport_add_device(dev);
292 transport_configure_device(dev);
294 error = ata_tlink_add(&ap->link);
301 transport_remove_device(dev);
305 transport_destroy_device(dev);
312 * ATA link attributes
316 #define ata_link_show_linkspeed(field) \
318 show_ata_link_##field(struct device *dev, \
319 struct device_attribute *attr, char *buf) \
321 struct ata_link *link = transport_class_to_link(dev); \
323 return sprintf(buf,"%s\n", sata_spd_string(fls(link->field))); \
326 #define ata_link_linkspeed_attr(field) \
327 ata_link_show_linkspeed(field) \
328 static DEVICE_ATTR(field, S_IRUGO, show_ata_link_##field, NULL)
330 ata_link_linkspeed_attr(hw_sata_spd_limit);
331 ata_link_linkspeed_attr(sata_spd_limit);
332 ata_link_linkspeed_attr(sata_spd);
335 static DECLARE_TRANSPORT_CLASS(ata_link_class,
336 "ata_link", NULL, NULL, NULL);
338 static void ata_tlink_release(struct device *dev)
340 put_device(dev->parent);
344 * ata_is_link -- check if a struct device represents a ATA link
345 * @dev: device to check
348 * %1 if the device represents a ATA link, %0 else
350 int ata_is_link(const struct device *dev)
352 return dev->release == ata_tlink_release;
355 static int ata_tlink_match(struct attribute_container *cont,
358 struct ata_internal* i = to_ata_internal(ata_scsi_transport_template);
359 if (!ata_is_link(dev))
361 return &i->link_attr_cont.ac == cont;
365 * ata_tlink_delete -- remove ATA LINK
366 * @port: ATA LINK to remove
368 * Removes the specified ATA LINK. remove associated ATA device(s) as well.
370 void ata_tlink_delete(struct ata_link *link)
372 struct device *dev = &link->tdev;
373 struct ata_device *ata_dev;
375 ata_for_each_dev(ata_dev, link, ALL) {
376 ata_tdev_delete(ata_dev);
379 transport_remove_device(dev);
381 transport_destroy_device(dev);
386 * ata_tlink_add -- initialize a transport ATA link structure
387 * @link: allocated ata_link structure.
389 * Initialize an ATA LINK structure for sysfs. It will be added in the
390 * device tree below the ATA PORT it belongs to.
392 * Returns %0 on success
394 int ata_tlink_add(struct ata_link *link)
396 struct device *dev = &link->tdev;
397 struct ata_port *ap = link->ap;
398 struct ata_device *ata_dev;
401 device_initialize(dev);
402 dev->parent = get_device(&ap->tdev);
403 dev->release = ata_tlink_release;
404 if (ata_is_host_link(link))
405 dev_set_name(dev, "link%d", ap->print_id);
407 dev_set_name(dev, "link%d.%d", ap->print_id, link->pmp);
409 transport_setup_device(dev);
411 error = device_add(dev);
416 transport_add_device(dev);
417 transport_configure_device(dev);
419 ata_for_each_dev(ata_dev, link, ALL) {
420 error = ata_tdev_add(ata_dev);
427 while (--ata_dev >= link->device) {
428 ata_tdev_delete(ata_dev);
430 transport_remove_device(dev);
433 transport_destroy_device(dev);
439 * ATA device attributes
442 #define ata_dev_show_class(title, field) \
444 show_ata_dev_##field(struct device *dev, \
445 struct device_attribute *attr, char *buf) \
447 struct ata_device *ata_dev = transport_class_to_dev(dev); \
449 return get_ata_##title##_names(ata_dev->field, buf); \
452 #define ata_dev_attr(title, field) \
453 ata_dev_show_class(title, field) \
454 static DEVICE_ATTR(field, S_IRUGO, show_ata_dev_##field, NULL)
456 ata_dev_attr(class, class);
457 ata_dev_attr(xfer, pio_mode);
458 ata_dev_attr(xfer, dma_mode);
459 ata_dev_attr(xfer, xfer_mode);
462 #define ata_dev_show_simple(field, format_string, cast) \
464 show_ata_dev_##field(struct device *dev, \
465 struct device_attribute *attr, char *buf) \
467 struct ata_device *ata_dev = transport_class_to_dev(dev); \
469 return snprintf(buf, 20, format_string, cast ata_dev->field); \
472 #define ata_dev_simple_attr(field, format_string, type) \
473 ata_dev_show_simple(field, format_string, (type)) \
474 static DEVICE_ATTR(field, S_IRUGO, \
475 show_ata_dev_##field, NULL)
477 ata_dev_simple_attr(spdn_cnt, "%d\n", int);
479 struct ata_show_ering_arg {
484 static int ata_show_ering(struct ata_ering_entry *ent, void *void_arg)
486 struct ata_show_ering_arg* arg = void_arg;
487 struct timespec time;
489 jiffies_to_timespec(ent->timestamp,&time);
490 arg->written += sprintf(arg->buf + arg->written,
492 time.tv_sec, time.tv_nsec);
493 arg->written += get_ata_err_names(ent->err_mask,
494 arg->buf + arg->written);
499 show_ata_dev_ering(struct device *dev,
500 struct device_attribute *attr, char *buf)
502 struct ata_device *ata_dev = transport_class_to_dev(dev);
503 struct ata_show_ering_arg arg = { buf, 0 };
505 ata_ering_map(&ata_dev->ering, ata_show_ering, &arg);
510 static DEVICE_ATTR(ering, S_IRUGO, show_ata_dev_ering, NULL);
513 show_ata_dev_id(struct device *dev,
514 struct device_attribute *attr, char *buf)
516 struct ata_device *ata_dev = transport_class_to_dev(dev);
517 int written = 0, i = 0;
519 if (ata_dev->class == ATA_DEV_PMP)
521 for(i=0;i<ATA_ID_WORDS;i++) {
522 written += snprintf(buf+written, 20, "%04x%c",
524 ((i+1) & 7) ? ' ' : '\n');
529 static DEVICE_ATTR(id, S_IRUGO, show_ata_dev_id, NULL);
532 show_ata_dev_gscr(struct device *dev,
533 struct device_attribute *attr, char *buf)
535 struct ata_device *ata_dev = transport_class_to_dev(dev);
536 int written = 0, i = 0;
538 if (ata_dev->class != ATA_DEV_PMP)
540 for(i=0;i<SATA_PMP_GSCR_DWORDS;i++) {
541 written += snprintf(buf+written, 20, "%08x%c",
543 ((i+1) & 3) ? ' ' : '\n');
545 if (SATA_PMP_GSCR_DWORDS & 3)
546 buf[written-1] = '\n';
550 static DEVICE_ATTR(gscr, S_IRUGO, show_ata_dev_gscr, NULL);
552 static DECLARE_TRANSPORT_CLASS(ata_dev_class,
553 "ata_device", NULL, NULL, NULL);
555 static void ata_tdev_release(struct device *dev)
557 put_device(dev->parent);
561 * ata_is_ata_dev -- check if a struct device represents a ATA device
562 * @dev: device to check
565 * %1 if the device represents a ATA device, %0 else
567 int ata_is_ata_dev(const struct device *dev)
569 return dev->release == ata_tdev_release;
572 static int ata_tdev_match(struct attribute_container *cont,
575 struct ata_internal* i = to_ata_internal(ata_scsi_transport_template);
576 if (!ata_is_ata_dev(dev))
578 return &i->dev_attr_cont.ac == cont;
582 * ata_tdev_free -- free a ATA LINK
583 * @dev: ATA PHY to free
585 * Frees the specified ATA PHY.
588 * This function must only be called on a PHY that has not
589 * successfully been added using ata_tdev_add().
591 static void ata_tdev_free(struct ata_device *dev)
593 transport_destroy_device(&dev->tdev);
594 put_device(&dev->tdev);
598 * ata_tdev_delete -- remove ATA device
599 * @port: ATA PORT to remove
601 * Removes the specified ATA device.
603 static void ata_tdev_delete(struct ata_device *ata_dev)
605 struct device *dev = &ata_dev->tdev;
607 transport_remove_device(dev);
609 ata_tdev_free(ata_dev);
614 * ata_tdev_add -- initialize a transport ATA device structure.
615 * @ata_dev: ata_dev structure.
617 * Initialize an ATA device structure for sysfs. It will be added in the
618 * device tree below the ATA LINK device it belongs to.
620 * Returns %0 on success
622 static int ata_tdev_add(struct ata_device *ata_dev)
624 struct device *dev = &ata_dev->tdev;
625 struct ata_link *link = ata_dev->link;
626 struct ata_port *ap = link->ap;
629 device_initialize(dev);
630 dev->parent = get_device(&link->tdev);
631 dev->release = ata_tdev_release;
632 if (ata_is_host_link(link))
633 dev_set_name(dev, "dev%d.%d", ap->print_id,ata_dev->devno);
635 dev_set_name(dev, "dev%d.%d.0", ap->print_id, link->pmp);
637 transport_setup_device(dev);
638 error = device_add(dev);
640 ata_tdev_free(ata_dev);
644 transport_add_device(dev);
645 transport_configure_device(dev);
651 * Setup / Teardown code
654 #define SETUP_TEMPLATE(attrb, field, perm, test) \
655 i->private_##attrb[count] = dev_attr_##field; \
656 i->private_##attrb[count].attr.mode = perm; \
657 i->attrb[count] = &i->private_##attrb[count]; \
661 #define SETUP_LINK_ATTRIBUTE(field) \
662 SETUP_TEMPLATE(link_attrs, field, S_IRUGO, 1)
664 #define SETUP_PORT_ATTRIBUTE(field) \
665 SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
667 #define SETUP_DEV_ATTRIBUTE(field) \
668 SETUP_TEMPLATE(dev_attrs, field, S_IRUGO, 1)
671 * ata_attach_transport -- instantiate ATA transport template
673 struct scsi_transport_template *ata_attach_transport(void)
675 struct ata_internal *i;
678 i = kzalloc(sizeof(struct ata_internal), GFP_KERNEL);
682 i->t.eh_strategy_handler = ata_scsi_error;
683 i->t.eh_timed_out = ata_scsi_timed_out;
684 i->t.user_scan = ata_scsi_user_scan;
686 i->t.host_attrs.ac.attrs = &i->port_attrs[0];
687 i->t.host_attrs.ac.class = &ata_port_class.class;
688 i->t.host_attrs.ac.match = ata_tport_match;
689 transport_container_register(&i->t.host_attrs);
691 i->link_attr_cont.ac.class = &ata_link_class.class;
692 i->link_attr_cont.ac.attrs = &i->link_attrs[0];
693 i->link_attr_cont.ac.match = ata_tlink_match;
694 transport_container_register(&i->link_attr_cont);
696 i->dev_attr_cont.ac.class = &ata_dev_class.class;
697 i->dev_attr_cont.ac.attrs = &i->dev_attrs[0];
698 i->dev_attr_cont.ac.match = ata_tdev_match;
699 transport_container_register(&i->dev_attr_cont);
702 SETUP_PORT_ATTRIBUTE(nr_pmp_links);
703 SETUP_PORT_ATTRIBUTE(idle_irq);
704 BUG_ON(count > ATA_PORT_ATTRS);
705 i->port_attrs[count] = NULL;
708 SETUP_LINK_ATTRIBUTE(hw_sata_spd_limit);
709 SETUP_LINK_ATTRIBUTE(sata_spd_limit);
710 SETUP_LINK_ATTRIBUTE(sata_spd);
711 BUG_ON(count > ATA_LINK_ATTRS);
712 i->link_attrs[count] = NULL;
715 SETUP_DEV_ATTRIBUTE(class);
716 SETUP_DEV_ATTRIBUTE(pio_mode);
717 SETUP_DEV_ATTRIBUTE(dma_mode);
718 SETUP_DEV_ATTRIBUTE(xfer_mode);
719 SETUP_DEV_ATTRIBUTE(spdn_cnt);
720 SETUP_DEV_ATTRIBUTE(ering);
721 SETUP_DEV_ATTRIBUTE(id);
722 SETUP_DEV_ATTRIBUTE(gscr);
723 BUG_ON(count > ATA_DEV_ATTRS);
724 i->dev_attrs[count] = NULL;
730 * ata_release_transport -- release ATA transport template instance
731 * @t: transport template instance
733 void ata_release_transport(struct scsi_transport_template *t)
735 struct ata_internal *i = to_ata_internal(t);
737 transport_container_unregister(&i->t.host_attrs);
738 transport_container_unregister(&i->link_attr_cont);
739 transport_container_unregister(&i->dev_attr_cont);
744 __init int libata_transport_init(void)
748 error = transport_class_register(&ata_link_class);
750 goto out_unregister_transport;
751 error = transport_class_register(&ata_port_class);
753 goto out_unregister_link;
754 error = transport_class_register(&ata_dev_class);
756 goto out_unregister_port;
760 transport_class_unregister(&ata_port_class);
762 transport_class_unregister(&ata_link_class);
763 out_unregister_transport:
768 void __exit libata_transport_exit(void)
770 transport_class_unregister(&ata_link_class);
771 transport_class_unregister(&ata_port_class);
772 transport_class_unregister(&ata_dev_class);