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 <linux/slab.h>
31 #include <scsi/scsi_transport.h>
32 #include <linux/libata.h>
33 #include <linux/hdreg.h>
34 #include <linux/uaccess.h>
35 #include <linux/pm_runtime.h>
38 #include "libata-transport.h"
40 #define ATA_PORT_ATTRS 2
41 #define ATA_LINK_ATTRS 3
42 #define ATA_DEV_ATTRS 9
44 struct scsi_transport_template;
45 struct scsi_transport_template *ata_scsi_transport_template;
48 struct scsi_transport_template t;
50 struct device_attribute private_port_attrs[ATA_PORT_ATTRS];
51 struct device_attribute private_link_attrs[ATA_LINK_ATTRS];
52 struct device_attribute private_dev_attrs[ATA_DEV_ATTRS];
54 struct transport_container link_attr_cont;
55 struct transport_container dev_attr_cont;
58 * The array of null terminated pointers to attributes
59 * needed by scsi_sysfs.c
61 struct device_attribute *link_attrs[ATA_LINK_ATTRS + 1];
62 struct device_attribute *port_attrs[ATA_PORT_ATTRS + 1];
63 struct device_attribute *dev_attrs[ATA_DEV_ATTRS + 1];
65 #define to_ata_internal(tmpl) container_of(tmpl, struct ata_internal, t)
68 #define tdev_to_device(d) \
69 container_of((d), struct ata_device, tdev)
70 #define transport_class_to_dev(dev) \
71 tdev_to_device((dev)->parent)
73 #define tdev_to_link(d) \
74 container_of((d), struct ata_link, tdev)
75 #define transport_class_to_link(dev) \
76 tdev_to_link((dev)->parent)
78 #define tdev_to_port(d) \
79 container_of((d), struct ata_port, tdev)
80 #define transport_class_to_port(dev) \
81 tdev_to_port((dev)->parent)
84 /* Device objects are always created whit link objects */
85 static int ata_tdev_add(struct ata_device *dev);
86 static void ata_tdev_delete(struct ata_device *dev);
90 * Hack to allow attributes of the same name in different objects.
92 #define ATA_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
93 struct device_attribute device_attr_##_prefix##_##_name = \
94 __ATTR(_name,_mode,_show,_store)
96 #define ata_bitfield_name_match(title, table) \
98 get_ata_##title##_names(u32 table_key, char *buf) \
104 for (i = 0; i < ARRAY_SIZE(table); i++) { \
105 if (table[i].value & table_key) { \
106 len += sprintf(buf + len, "%s%s", \
107 prefix, table[i].name); \
111 len += sprintf(buf + len, "\n"); \
115 #define ata_bitfield_name_search(title, table) \
117 get_ata_##title##_names(u32 table_key, char *buf) \
122 for (i = 0; i < ARRAY_SIZE(table); i++) { \
123 if (table[i].value == table_key) { \
124 len += sprintf(buf + len, "%s", \
129 len += sprintf(buf + len, "\n"); \
136 } ata_class_names[] = {
137 { ATA_DEV_UNKNOWN, "unknown" },
138 { ATA_DEV_ATA, "ata" },
139 { ATA_DEV_ATA_UNSUP, "ata" },
140 { ATA_DEV_ATAPI, "atapi" },
141 { ATA_DEV_ATAPI_UNSUP, "atapi" },
142 { ATA_DEV_PMP, "pmp" },
143 { ATA_DEV_PMP_UNSUP, "pmp" },
144 { ATA_DEV_SEMB, "semb" },
145 { ATA_DEV_SEMB_UNSUP, "semb" },
146 { ATA_DEV_NONE, "none" }
148 ata_bitfield_name_search(class, ata_class_names)
154 } ata_err_names[] = {
155 { AC_ERR_DEV, "DeviceError" },
156 { AC_ERR_HSM, "HostStateMachineError" },
157 { AC_ERR_TIMEOUT, "Timeout" },
158 { AC_ERR_MEDIA, "MediaError" },
159 { AC_ERR_ATA_BUS, "BusError" },
160 { AC_ERR_HOST_BUS, "HostBusError" },
161 { AC_ERR_SYSTEM, "SystemError" },
162 { AC_ERR_INVALID, "InvalidArg" },
163 { AC_ERR_OTHER, "Unknown" },
164 { AC_ERR_NODEV_HINT, "NoDeviceHint" },
165 { AC_ERR_NCQ, "NCQError" }
167 ata_bitfield_name_match(err, ata_err_names)
172 } ata_xfer_names[] = {
173 { XFER_UDMA_7, "XFER_UDMA_7" },
174 { XFER_UDMA_6, "XFER_UDMA_6" },
175 { XFER_UDMA_5, "XFER_UDMA_5" },
176 { XFER_UDMA_4, "XFER_UDMA_4" },
177 { XFER_UDMA_3, "XFER_UDMA_3" },
178 { XFER_UDMA_2, "XFER_UDMA_2" },
179 { XFER_UDMA_1, "XFER_UDMA_1" },
180 { XFER_UDMA_0, "XFER_UDMA_0" },
181 { XFER_MW_DMA_4, "XFER_MW_DMA_4" },
182 { XFER_MW_DMA_3, "XFER_MW_DMA_3" },
183 { XFER_MW_DMA_2, "XFER_MW_DMA_2" },
184 { XFER_MW_DMA_1, "XFER_MW_DMA_1" },
185 { XFER_MW_DMA_0, "XFER_MW_DMA_0" },
186 { XFER_SW_DMA_2, "XFER_SW_DMA_2" },
187 { XFER_SW_DMA_1, "XFER_SW_DMA_1" },
188 { XFER_SW_DMA_0, "XFER_SW_DMA_0" },
189 { XFER_PIO_6, "XFER_PIO_6" },
190 { XFER_PIO_5, "XFER_PIO_5" },
191 { XFER_PIO_4, "XFER_PIO_4" },
192 { XFER_PIO_3, "XFER_PIO_3" },
193 { XFER_PIO_2, "XFER_PIO_2" },
194 { XFER_PIO_1, "XFER_PIO_1" },
195 { XFER_PIO_0, "XFER_PIO_0" },
196 { XFER_PIO_SLOW, "XFER_PIO_SLOW" }
198 ata_bitfield_name_match(xfer,ata_xfer_names)
201 * ATA Port attributes
203 #define ata_port_show_simple(field, name, format_string, cast) \
205 show_ata_port_##name(struct device *dev, \
206 struct device_attribute *attr, char *buf) \
208 struct ata_port *ap = transport_class_to_port(dev); \
210 return snprintf(buf, 20, format_string, cast ap->field); \
213 #define ata_port_simple_attr(field, name, format_string, type) \
214 ata_port_show_simple(field, name, format_string, (type)) \
215 static DEVICE_ATTR(name, S_IRUGO, show_ata_port_##name, NULL)
217 ata_port_simple_attr(nr_pmp_links, nr_pmp_links, "%d\n", int);
218 ata_port_simple_attr(stats.idle_irq, idle_irq, "%ld\n", unsigned long);
220 static DECLARE_TRANSPORT_CLASS(ata_port_class,
221 "ata_port", NULL, NULL, NULL);
223 static void ata_tport_release(struct device *dev)
225 put_device(dev->parent);
229 * ata_is_port -- check if a struct device represents a ATA port
230 * @dev: device to check
233 * %1 if the device represents a ATA Port, %0 else
235 static int ata_is_port(const struct device *dev)
237 return dev->release == ata_tport_release;
240 static int ata_tport_match(struct attribute_container *cont,
243 if (!ata_is_port(dev))
245 return &ata_scsi_transport_template->host_attrs.ac == cont;
249 * ata_tport_delete -- remove ATA PORT
250 * @port: ATA PORT to remove
252 * Removes the specified ATA PORT. Remove the associated link as well.
254 void ata_tport_delete(struct ata_port *ap)
256 struct device *dev = &ap->tdev;
258 ata_tlink_delete(&ap->link);
260 transport_remove_device(dev);
262 transport_destroy_device(dev);
266 /** ata_tport_add - initialize a transport ATA port structure
268 * @parent: parent device
269 * @ap: existing ata_port structure
271 * Initialize a ATA port structure for sysfs. It will be added to the device
272 * tree below the device specified by @parent which could be a PCI device.
274 * Returns %0 on success
276 int ata_tport_add(struct device *parent,
280 struct device *dev = &ap->tdev;
282 device_initialize(dev);
283 dev->type = &ata_port_type;
285 dev->parent = get_device(parent);
286 dev->release = ata_tport_release;
287 dev_set_name(dev, "ata%d", ap->print_id);
288 transport_setup_device(dev);
289 error = device_add(dev);
294 device_enable_async_suspend(dev);
295 pm_runtime_set_active(dev);
296 pm_runtime_enable(dev);
297 pm_runtime_forbid(dev);
299 transport_add_device(dev);
300 transport_configure_device(dev);
302 error = ata_tlink_add(&ap->link);
309 transport_remove_device(dev);
313 transport_destroy_device(dev);
320 * ATA link attributes
324 #define ata_link_show_linkspeed(field) \
326 show_ata_link_##field(struct device *dev, \
327 struct device_attribute *attr, char *buf) \
329 struct ata_link *link = transport_class_to_link(dev); \
331 return sprintf(buf,"%s\n", sata_spd_string(fls(link->field))); \
334 #define ata_link_linkspeed_attr(field) \
335 ata_link_show_linkspeed(field) \
336 static DEVICE_ATTR(field, S_IRUGO, show_ata_link_##field, NULL)
338 ata_link_linkspeed_attr(hw_sata_spd_limit);
339 ata_link_linkspeed_attr(sata_spd_limit);
340 ata_link_linkspeed_attr(sata_spd);
343 static DECLARE_TRANSPORT_CLASS(ata_link_class,
344 "ata_link", NULL, NULL, NULL);
346 static void ata_tlink_release(struct device *dev)
348 put_device(dev->parent);
352 * ata_is_link -- check if a struct device represents a ATA link
353 * @dev: device to check
356 * %1 if the device represents a ATA link, %0 else
358 static int ata_is_link(const struct device *dev)
360 return dev->release == ata_tlink_release;
363 static int ata_tlink_match(struct attribute_container *cont,
366 struct ata_internal* i = to_ata_internal(ata_scsi_transport_template);
367 if (!ata_is_link(dev))
369 return &i->link_attr_cont.ac == cont;
373 * ata_tlink_delete -- remove ATA LINK
374 * @port: ATA LINK to remove
376 * Removes the specified ATA LINK. remove associated ATA device(s) as well.
378 void ata_tlink_delete(struct ata_link *link)
380 struct device *dev = &link->tdev;
381 struct ata_device *ata_dev;
383 ata_for_each_dev(ata_dev, link, ALL) {
384 ata_tdev_delete(ata_dev);
387 transport_remove_device(dev);
389 transport_destroy_device(dev);
394 * ata_tlink_add -- initialize a transport ATA link structure
395 * @link: allocated ata_link structure.
397 * Initialize an ATA LINK structure for sysfs. It will be added in the
398 * device tree below the ATA PORT it belongs to.
400 * Returns %0 on success
402 int ata_tlink_add(struct ata_link *link)
404 struct device *dev = &link->tdev;
405 struct ata_port *ap = link->ap;
406 struct ata_device *ata_dev;
409 device_initialize(dev);
410 dev->parent = get_device(&ap->tdev);
411 dev->release = ata_tlink_release;
412 if (ata_is_host_link(link))
413 dev_set_name(dev, "link%d", ap->print_id);
415 dev_set_name(dev, "link%d.%d", ap->print_id, link->pmp);
417 transport_setup_device(dev);
419 error = device_add(dev);
424 transport_add_device(dev);
425 transport_configure_device(dev);
427 ata_for_each_dev(ata_dev, link, ALL) {
428 error = ata_tdev_add(ata_dev);
435 while (--ata_dev >= link->device) {
436 ata_tdev_delete(ata_dev);
438 transport_remove_device(dev);
441 transport_destroy_device(dev);
447 * ATA device attributes
450 #define ata_dev_show_class(title, field) \
452 show_ata_dev_##field(struct device *dev, \
453 struct device_attribute *attr, char *buf) \
455 struct ata_device *ata_dev = transport_class_to_dev(dev); \
457 return get_ata_##title##_names(ata_dev->field, buf); \
460 #define ata_dev_attr(title, field) \
461 ata_dev_show_class(title, field) \
462 static DEVICE_ATTR(field, S_IRUGO, show_ata_dev_##field, NULL)
464 ata_dev_attr(class, class);
465 ata_dev_attr(xfer, pio_mode);
466 ata_dev_attr(xfer, dma_mode);
467 ata_dev_attr(xfer, xfer_mode);
470 #define ata_dev_show_simple(field, format_string, cast) \
472 show_ata_dev_##field(struct device *dev, \
473 struct device_attribute *attr, char *buf) \
475 struct ata_device *ata_dev = transport_class_to_dev(dev); \
477 return snprintf(buf, 20, format_string, cast ata_dev->field); \
480 #define ata_dev_simple_attr(field, format_string, type) \
481 ata_dev_show_simple(field, format_string, (type)) \
482 static DEVICE_ATTR(field, S_IRUGO, \
483 show_ata_dev_##field, NULL)
485 ata_dev_simple_attr(spdn_cnt, "%d\n", int);
487 struct ata_show_ering_arg {
492 static int ata_show_ering(struct ata_ering_entry *ent, void *void_arg)
494 struct ata_show_ering_arg* arg = void_arg;
495 struct timespec time;
497 jiffies_to_timespec(ent->timestamp,&time);
498 arg->written += sprintf(arg->buf + arg->written,
500 time.tv_sec, time.tv_nsec);
501 arg->written += get_ata_err_names(ent->err_mask,
502 arg->buf + arg->written);
507 show_ata_dev_ering(struct device *dev,
508 struct device_attribute *attr, char *buf)
510 struct ata_device *ata_dev = transport_class_to_dev(dev);
511 struct ata_show_ering_arg arg = { buf, 0 };
513 ata_ering_map(&ata_dev->ering, ata_show_ering, &arg);
518 static DEVICE_ATTR(ering, S_IRUGO, show_ata_dev_ering, NULL);
521 show_ata_dev_id(struct device *dev,
522 struct device_attribute *attr, char *buf)
524 struct ata_device *ata_dev = transport_class_to_dev(dev);
525 int written = 0, i = 0;
527 if (ata_dev->class == ATA_DEV_PMP)
529 for(i=0;i<ATA_ID_WORDS;i++) {
530 written += snprintf(buf+written, 20, "%04x%c",
532 ((i+1) & 7) ? ' ' : '\n');
537 static DEVICE_ATTR(id, S_IRUGO, show_ata_dev_id, NULL);
540 show_ata_dev_gscr(struct device *dev,
541 struct device_attribute *attr, char *buf)
543 struct ata_device *ata_dev = transport_class_to_dev(dev);
544 int written = 0, i = 0;
546 if (ata_dev->class != ATA_DEV_PMP)
548 for(i=0;i<SATA_PMP_GSCR_DWORDS;i++) {
549 written += snprintf(buf+written, 20, "%08x%c",
551 ((i+1) & 3) ? ' ' : '\n');
553 if (SATA_PMP_GSCR_DWORDS & 3)
554 buf[written-1] = '\n';
558 static DEVICE_ATTR(gscr, S_IRUGO, show_ata_dev_gscr, NULL);
560 static DECLARE_TRANSPORT_CLASS(ata_dev_class,
561 "ata_device", NULL, NULL, NULL);
563 static void ata_tdev_release(struct device *dev)
565 put_device(dev->parent);
569 * ata_is_ata_dev -- check if a struct device represents a ATA device
570 * @dev: device to check
573 * %1 if the device represents a ATA device, %0 else
575 static int ata_is_ata_dev(const struct device *dev)
577 return dev->release == ata_tdev_release;
580 static int ata_tdev_match(struct attribute_container *cont,
583 struct ata_internal* i = to_ata_internal(ata_scsi_transport_template);
584 if (!ata_is_ata_dev(dev))
586 return &i->dev_attr_cont.ac == cont;
590 * ata_tdev_free -- free a ATA LINK
591 * @dev: ATA PHY to free
593 * Frees the specified ATA PHY.
596 * This function must only be called on a PHY that has not
597 * successfully been added using ata_tdev_add().
599 static void ata_tdev_free(struct ata_device *dev)
601 transport_destroy_device(&dev->tdev);
602 put_device(&dev->tdev);
606 * ata_tdev_delete -- remove ATA device
607 * @port: ATA PORT to remove
609 * Removes the specified ATA device.
611 static void ata_tdev_delete(struct ata_device *ata_dev)
613 struct device *dev = &ata_dev->tdev;
615 transport_remove_device(dev);
617 ata_tdev_free(ata_dev);
622 * ata_tdev_add -- initialize a transport ATA device structure.
623 * @ata_dev: ata_dev structure.
625 * Initialize an ATA device structure for sysfs. It will be added in the
626 * device tree below the ATA LINK device it belongs to.
628 * Returns %0 on success
630 static int ata_tdev_add(struct ata_device *ata_dev)
632 struct device *dev = &ata_dev->tdev;
633 struct ata_link *link = ata_dev->link;
634 struct ata_port *ap = link->ap;
637 device_initialize(dev);
638 dev->parent = get_device(&link->tdev);
639 dev->release = ata_tdev_release;
640 if (ata_is_host_link(link))
641 dev_set_name(dev, "dev%d.%d", ap->print_id,ata_dev->devno);
643 dev_set_name(dev, "dev%d.%d.0", ap->print_id, link->pmp);
645 transport_setup_device(dev);
646 error = device_add(dev);
648 ata_tdev_free(ata_dev);
652 transport_add_device(dev);
653 transport_configure_device(dev);
659 * Setup / Teardown code
662 #define SETUP_TEMPLATE(attrb, field, perm, test) \
663 i->private_##attrb[count] = dev_attr_##field; \
664 i->private_##attrb[count].attr.mode = perm; \
665 i->attrb[count] = &i->private_##attrb[count]; \
669 #define SETUP_LINK_ATTRIBUTE(field) \
670 SETUP_TEMPLATE(link_attrs, field, S_IRUGO, 1)
672 #define SETUP_PORT_ATTRIBUTE(field) \
673 SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
675 #define SETUP_DEV_ATTRIBUTE(field) \
676 SETUP_TEMPLATE(dev_attrs, field, S_IRUGO, 1)
679 * ata_attach_transport -- instantiate ATA transport template
681 struct scsi_transport_template *ata_attach_transport(void)
683 struct ata_internal *i;
686 i = kzalloc(sizeof(struct ata_internal), GFP_KERNEL);
690 i->t.eh_strategy_handler = ata_scsi_error;
691 i->t.eh_timed_out = ata_scsi_timed_out;
692 i->t.user_scan = ata_scsi_user_scan;
694 i->t.host_attrs.ac.attrs = &i->port_attrs[0];
695 i->t.host_attrs.ac.class = &ata_port_class.class;
696 i->t.host_attrs.ac.match = ata_tport_match;
697 transport_container_register(&i->t.host_attrs);
699 i->link_attr_cont.ac.class = &ata_link_class.class;
700 i->link_attr_cont.ac.attrs = &i->link_attrs[0];
701 i->link_attr_cont.ac.match = ata_tlink_match;
702 transport_container_register(&i->link_attr_cont);
704 i->dev_attr_cont.ac.class = &ata_dev_class.class;
705 i->dev_attr_cont.ac.attrs = &i->dev_attrs[0];
706 i->dev_attr_cont.ac.match = ata_tdev_match;
707 transport_container_register(&i->dev_attr_cont);
710 SETUP_PORT_ATTRIBUTE(nr_pmp_links);
711 SETUP_PORT_ATTRIBUTE(idle_irq);
712 BUG_ON(count > ATA_PORT_ATTRS);
713 i->port_attrs[count] = NULL;
716 SETUP_LINK_ATTRIBUTE(hw_sata_spd_limit);
717 SETUP_LINK_ATTRIBUTE(sata_spd_limit);
718 SETUP_LINK_ATTRIBUTE(sata_spd);
719 BUG_ON(count > ATA_LINK_ATTRS);
720 i->link_attrs[count] = NULL;
723 SETUP_DEV_ATTRIBUTE(class);
724 SETUP_DEV_ATTRIBUTE(pio_mode);
725 SETUP_DEV_ATTRIBUTE(dma_mode);
726 SETUP_DEV_ATTRIBUTE(xfer_mode);
727 SETUP_DEV_ATTRIBUTE(spdn_cnt);
728 SETUP_DEV_ATTRIBUTE(ering);
729 SETUP_DEV_ATTRIBUTE(id);
730 SETUP_DEV_ATTRIBUTE(gscr);
731 BUG_ON(count > ATA_DEV_ATTRS);
732 i->dev_attrs[count] = NULL;
738 * ata_release_transport -- release ATA transport template instance
739 * @t: transport template instance
741 void ata_release_transport(struct scsi_transport_template *t)
743 struct ata_internal *i = to_ata_internal(t);
745 transport_container_unregister(&i->t.host_attrs);
746 transport_container_unregister(&i->link_attr_cont);
747 transport_container_unregister(&i->dev_attr_cont);
752 __init int libata_transport_init(void)
756 error = transport_class_register(&ata_link_class);
758 goto out_unregister_transport;
759 error = transport_class_register(&ata_port_class);
761 goto out_unregister_link;
762 error = transport_class_register(&ata_dev_class);
764 goto out_unregister_port;
768 transport_class_unregister(&ata_port_class);
770 transport_class_unregister(&ata_link_class);
771 out_unregister_transport:
776 void __exit libata_transport_exit(void)
778 transport_class_unregister(&ata_link_class);
779 transport_class_unregister(&ata_port_class);
780 transport_class_unregister(&ata_dev_class);