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>
37 #include "libata-transport.h"
39 #define ATA_PORT_ATTRS 2
40 #define ATA_LINK_ATTRS 3
41 #define ATA_DEV_ATTRS 9
43 struct scsi_transport_template
;
44 struct scsi_transport_template
*ata_scsi_transport_template
;
47 struct scsi_transport_template t
;
49 struct device_attribute private_port_attrs
[ATA_PORT_ATTRS
];
50 struct device_attribute private_link_attrs
[ATA_LINK_ATTRS
];
51 struct device_attribute private_dev_attrs
[ATA_DEV_ATTRS
];
53 struct transport_container link_attr_cont
;
54 struct transport_container dev_attr_cont
;
57 * The array of null terminated pointers to attributes
58 * needed by scsi_sysfs.c
60 struct device_attribute
*link_attrs
[ATA_LINK_ATTRS
+ 1];
61 struct device_attribute
*port_attrs
[ATA_PORT_ATTRS
+ 1];
62 struct device_attribute
*dev_attrs
[ATA_DEV_ATTRS
+ 1];
64 #define to_ata_internal(tmpl) container_of(tmpl, struct ata_internal, t)
67 #define tdev_to_device(d) \
68 container_of((d), struct ata_device, tdev)
69 #define transport_class_to_dev(dev) \
70 tdev_to_device((dev)->parent)
72 #define tdev_to_link(d) \
73 container_of((d), struct ata_link, tdev)
74 #define transport_class_to_link(dev) \
75 tdev_to_link((dev)->parent)
77 #define tdev_to_port(d) \
78 container_of((d), struct ata_port, tdev)
79 #define transport_class_to_port(dev) \
80 tdev_to_port((dev)->parent)
83 /* Device objects are always created whit link objects */
84 static int ata_tdev_add(struct ata_device
*dev
);
85 static void ata_tdev_delete(struct ata_device
*dev
);
89 * Hack to allow attributes of the same name in different objects.
91 #define ATA_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
92 struct device_attribute device_attr_##_prefix##_##_name = \
93 __ATTR(_name,_mode,_show,_store)
95 #define ata_bitfield_name_match(title, table) \
97 get_ata_##title##_names(u32 table_key, char *buf) \
103 for (i = 0; i < ARRAY_SIZE(table); i++) { \
104 if (table[i].value & table_key) { \
105 len += sprintf(buf + len, "%s%s", \
106 prefix, table[i].name); \
110 len += sprintf(buf + len, "\n"); \
114 #define ata_bitfield_name_search(title, table) \
116 get_ata_##title##_names(u32 table_key, char *buf) \
121 for (i = 0; i < ARRAY_SIZE(table); i++) { \
122 if (table[i].value == table_key) { \
123 len += sprintf(buf + len, "%s", \
128 len += sprintf(buf + len, "\n"); \
135 } ata_class_names
[] = {
136 { ATA_DEV_UNKNOWN
, "unknown" },
137 { ATA_DEV_ATA
, "ata" },
138 { ATA_DEV_ATA_UNSUP
, "ata" },
139 { ATA_DEV_ATAPI
, "atapi" },
140 { ATA_DEV_ATAPI_UNSUP
, "atapi" },
141 { ATA_DEV_PMP
, "pmp" },
142 { ATA_DEV_PMP_UNSUP
, "pmp" },
143 { ATA_DEV_SEMB
, "semb" },
144 { ATA_DEV_SEMB_UNSUP
, "semb" },
145 { ATA_DEV_NONE
, "none" }
147 ata_bitfield_name_search(class, ata_class_names
)
153 } ata_err_names
[] = {
154 { AC_ERR_DEV
, "DeviceError" },
155 { AC_ERR_HSM
, "HostStateMachineError" },
156 { AC_ERR_TIMEOUT
, "Timeout" },
157 { AC_ERR_MEDIA
, "MediaError" },
158 { AC_ERR_ATA_BUS
, "BusError" },
159 { AC_ERR_HOST_BUS
, "HostBusError" },
160 { AC_ERR_SYSTEM
, "SystemError" },
161 { AC_ERR_INVALID
, "InvalidArg" },
162 { AC_ERR_OTHER
, "Unknown" },
163 { AC_ERR_NODEV_HINT
, "NoDeviceHint" },
164 { AC_ERR_NCQ
, "NCQError" }
166 ata_bitfield_name_match(err
, ata_err_names
)
171 } ata_xfer_names
[] = {
172 { XFER_UDMA_7
, "XFER_UDMA_7" },
173 { XFER_UDMA_6
, "XFER_UDMA_6" },
174 { XFER_UDMA_5
, "XFER_UDMA_5" },
175 { XFER_UDMA_4
, "XFER_UDMA_4" },
176 { XFER_UDMA_3
, "XFER_UDMA_3" },
177 { XFER_UDMA_2
, "XFER_UDMA_2" },
178 { XFER_UDMA_1
, "XFER_UDMA_1" },
179 { XFER_UDMA_0
, "XFER_UDMA_0" },
180 { XFER_MW_DMA_4
, "XFER_MW_DMA_4" },
181 { XFER_MW_DMA_3
, "XFER_MW_DMA_3" },
182 { XFER_MW_DMA_2
, "XFER_MW_DMA_2" },
183 { XFER_MW_DMA_1
, "XFER_MW_DMA_1" },
184 { XFER_MW_DMA_0
, "XFER_MW_DMA_0" },
185 { XFER_SW_DMA_2
, "XFER_SW_DMA_2" },
186 { XFER_SW_DMA_1
, "XFER_SW_DMA_1" },
187 { XFER_SW_DMA_0
, "XFER_SW_DMA_0" },
188 { XFER_PIO_6
, "XFER_PIO_6" },
189 { XFER_PIO_5
, "XFER_PIO_5" },
190 { XFER_PIO_4
, "XFER_PIO_4" },
191 { XFER_PIO_3
, "XFER_PIO_3" },
192 { XFER_PIO_2
, "XFER_PIO_2" },
193 { XFER_PIO_1
, "XFER_PIO_1" },
194 { XFER_PIO_0
, "XFER_PIO_0" },
195 { XFER_PIO_SLOW
, "XFER_PIO_SLOW" }
197 ata_bitfield_name_match(xfer
,ata_xfer_names
)
200 * ATA Port attributes
202 #define ata_port_show_simple(field, name, format_string, cast) \
204 show_ata_port_##name(struct device *dev, \
205 struct device_attribute *attr, char *buf) \
207 struct ata_port *ap = transport_class_to_port(dev); \
209 return snprintf(buf, 20, format_string, cast ap->field); \
212 #define ata_port_simple_attr(field, name, format_string, type) \
213 ata_port_show_simple(field, name, format_string, (type)) \
214 static DEVICE_ATTR(name, S_IRUGO, show_ata_port_##name, NULL)
216 ata_port_simple_attr(nr_pmp_links
, nr_pmp_links
, "%d\n", int);
217 ata_port_simple_attr(stats
.idle_irq
, idle_irq
, "%ld\n", unsigned long);
219 static DECLARE_TRANSPORT_CLASS(ata_port_class
,
220 "ata_port", NULL
, NULL
, NULL
);
222 static void ata_tport_release(struct device
*dev
)
224 put_device(dev
->parent
);
228 * ata_is_port -- check if a struct device represents a ATA port
229 * @dev: device to check
232 * %1 if the device represents a ATA Port, %0 else
234 int ata_is_port(const struct device
*dev
)
236 return dev
->release
== ata_tport_release
;
239 static int ata_tport_match(struct attribute_container
*cont
,
242 if (!ata_is_port(dev
))
244 return &ata_scsi_transport_template
->host_attrs
.ac
== cont
;
248 * ata_tport_delete -- remove ATA PORT
249 * @port: ATA PORT to remove
251 * Removes the specified ATA PORT. Remove the associated link as well.
253 void ata_tport_delete(struct ata_port
*ap
)
255 struct device
*dev
= &ap
->tdev
;
257 ata_tlink_delete(&ap
->link
);
259 transport_remove_device(dev
);
261 transport_destroy_device(dev
);
265 /** ata_tport_add - initialize a transport ATA port structure
267 * @parent: parent device
268 * @ap: existing ata_port structure
270 * Initialize a ATA port structure for sysfs. It will be added to the device
271 * tree below the device specified by @parent which could be a PCI device.
273 * Returns %0 on success
275 int ata_tport_add(struct device
*parent
,
279 struct device
*dev
= &ap
->tdev
;
281 device_initialize(dev
);
283 dev
->parent
= get_device(parent
);
284 dev
->release
= ata_tport_release
;
285 dev_set_name(dev
, "ata%d", ap
->print_id
);
286 transport_setup_device(dev
);
287 error
= device_add(dev
);
292 transport_add_device(dev
);
293 transport_configure_device(dev
);
295 error
= ata_tlink_add(&ap
->link
);
302 transport_remove_device(dev
);
306 transport_destroy_device(dev
);
313 * ATA link attributes
317 #define ata_link_show_linkspeed(field) \
319 show_ata_link_##field(struct device *dev, \
320 struct device_attribute *attr, char *buf) \
322 struct ata_link *link = transport_class_to_link(dev); \
324 return sprintf(buf,"%s\n", sata_spd_string(fls(link->field))); \
327 #define ata_link_linkspeed_attr(field) \
328 ata_link_show_linkspeed(field) \
329 static DEVICE_ATTR(field, S_IRUGO, show_ata_link_##field, NULL)
331 ata_link_linkspeed_attr(hw_sata_spd_limit
);
332 ata_link_linkspeed_attr(sata_spd_limit
);
333 ata_link_linkspeed_attr(sata_spd
);
336 static DECLARE_TRANSPORT_CLASS(ata_link_class
,
337 "ata_link", NULL
, NULL
, NULL
);
339 static void ata_tlink_release(struct device
*dev
)
341 put_device(dev
->parent
);
345 * ata_is_link -- check if a struct device represents a ATA link
346 * @dev: device to check
349 * %1 if the device represents a ATA link, %0 else
351 int ata_is_link(const struct device
*dev
)
353 return dev
->release
== ata_tlink_release
;
356 static int ata_tlink_match(struct attribute_container
*cont
,
359 struct ata_internal
* i
= to_ata_internal(ata_scsi_transport_template
);
360 if (!ata_is_link(dev
))
362 return &i
->link_attr_cont
.ac
== cont
;
366 * ata_tlink_delete -- remove ATA LINK
367 * @port: ATA LINK to remove
369 * Removes the specified ATA LINK. remove associated ATA device(s) as well.
371 void ata_tlink_delete(struct ata_link
*link
)
373 struct device
*dev
= &link
->tdev
;
374 struct ata_device
*ata_dev
;
376 ata_for_each_dev(ata_dev
, link
, ALL
) {
377 ata_tdev_delete(ata_dev
);
380 transport_remove_device(dev
);
382 transport_destroy_device(dev
);
387 * ata_tlink_add -- initialize a transport ATA link structure
388 * @link: allocated ata_link structure.
390 * Initialize an ATA LINK structure for sysfs. It will be added in the
391 * device tree below the ATA PORT it belongs to.
393 * Returns %0 on success
395 int ata_tlink_add(struct ata_link
*link
)
397 struct device
*dev
= &link
->tdev
;
398 struct ata_port
*ap
= link
->ap
;
399 struct ata_device
*ata_dev
;
402 device_initialize(dev
);
403 dev
->parent
= get_device(&ap
->tdev
);
404 dev
->release
= ata_tlink_release
;
405 if (ata_is_host_link(link
))
406 dev_set_name(dev
, "link%d", ap
->print_id
);
408 dev_set_name(dev
, "link%d.%d", ap
->print_id
, link
->pmp
);
410 transport_setup_device(dev
);
412 error
= device_add(dev
);
417 transport_add_device(dev
);
418 transport_configure_device(dev
);
420 ata_for_each_dev(ata_dev
, link
, ALL
) {
421 error
= ata_tdev_add(ata_dev
);
428 while (--ata_dev
>= link
->device
) {
429 ata_tdev_delete(ata_dev
);
431 transport_remove_device(dev
);
434 transport_destroy_device(dev
);
440 * ATA device attributes
443 #define ata_dev_show_class(title, field) \
445 show_ata_dev_##field(struct device *dev, \
446 struct device_attribute *attr, char *buf) \
448 struct ata_device *ata_dev = transport_class_to_dev(dev); \
450 return get_ata_##title##_names(ata_dev->field, buf); \
453 #define ata_dev_attr(title, field) \
454 ata_dev_show_class(title, field) \
455 static DEVICE_ATTR(field, S_IRUGO, show_ata_dev_##field, NULL)
457 ata_dev_attr(class, class);
458 ata_dev_attr(xfer
, pio_mode
);
459 ata_dev_attr(xfer
, dma_mode
);
460 ata_dev_attr(xfer
, xfer_mode
);
463 #define ata_dev_show_simple(field, format_string, cast) \
465 show_ata_dev_##field(struct device *dev, \
466 struct device_attribute *attr, char *buf) \
468 struct ata_device *ata_dev = transport_class_to_dev(dev); \
470 return snprintf(buf, 20, format_string, cast ata_dev->field); \
473 #define ata_dev_simple_attr(field, format_string, type) \
474 ata_dev_show_simple(field, format_string, (type)) \
475 static DEVICE_ATTR(field, S_IRUGO, \
476 show_ata_dev_##field, NULL)
478 ata_dev_simple_attr(spdn_cnt
, "%d\n", int);
480 struct ata_show_ering_arg
{
485 static int ata_show_ering(struct ata_ering_entry
*ent
, void *void_arg
)
487 struct ata_show_ering_arg
* arg
= void_arg
;
488 struct timespec time
;
490 jiffies_to_timespec(ent
->timestamp
,&time
);
491 arg
->written
+= sprintf(arg
->buf
+ arg
->written
,
493 time
.tv_sec
, time
.tv_nsec
);
494 arg
->written
+= get_ata_err_names(ent
->err_mask
,
495 arg
->buf
+ arg
->written
);
500 show_ata_dev_ering(struct device
*dev
,
501 struct device_attribute
*attr
, char *buf
)
503 struct ata_device
*ata_dev
= transport_class_to_dev(dev
);
504 struct ata_show_ering_arg arg
= { buf
, 0 };
506 ata_ering_map(&ata_dev
->ering
, ata_show_ering
, &arg
);
511 static DEVICE_ATTR(ering
, S_IRUGO
, show_ata_dev_ering
, NULL
);
514 show_ata_dev_id(struct device
*dev
,
515 struct device_attribute
*attr
, char *buf
)
517 struct ata_device
*ata_dev
= transport_class_to_dev(dev
);
518 int written
= 0, i
= 0;
520 if (ata_dev
->class == ATA_DEV_PMP
)
522 for(i
=0;i
<ATA_ID_WORDS
;i
++) {
523 written
+= snprintf(buf
+written
, 20, "%04x%c",
525 ((i
+1) & 7) ? ' ' : '\n');
530 static DEVICE_ATTR(id
, S_IRUGO
, show_ata_dev_id
, NULL
);
533 show_ata_dev_gscr(struct device
*dev
,
534 struct device_attribute
*attr
, char *buf
)
536 struct ata_device
*ata_dev
= transport_class_to_dev(dev
);
537 int written
= 0, i
= 0;
539 if (ata_dev
->class != ATA_DEV_PMP
)
541 for(i
=0;i
<SATA_PMP_GSCR_DWORDS
;i
++) {
542 written
+= snprintf(buf
+written
, 20, "%08x%c",
544 ((i
+1) & 3) ? ' ' : '\n');
546 if (SATA_PMP_GSCR_DWORDS
& 3)
547 buf
[written
-1] = '\n';
551 static DEVICE_ATTR(gscr
, S_IRUGO
, show_ata_dev_gscr
, NULL
);
553 static DECLARE_TRANSPORT_CLASS(ata_dev_class
,
554 "ata_device", NULL
, NULL
, NULL
);
556 static void ata_tdev_release(struct device
*dev
)
558 put_device(dev
->parent
);
562 * ata_is_ata_dev -- check if a struct device represents a ATA device
563 * @dev: device to check
566 * %1 if the device represents a ATA device, %0 else
568 int ata_is_ata_dev(const struct device
*dev
)
570 return dev
->release
== ata_tdev_release
;
573 static int ata_tdev_match(struct attribute_container
*cont
,
576 struct ata_internal
* i
= to_ata_internal(ata_scsi_transport_template
);
577 if (!ata_is_ata_dev(dev
))
579 return &i
->dev_attr_cont
.ac
== cont
;
583 * ata_tdev_free -- free a ATA LINK
584 * @dev: ATA PHY to free
586 * Frees the specified ATA PHY.
589 * This function must only be called on a PHY that has not
590 * successfully been added using ata_tdev_add().
592 static void ata_tdev_free(struct ata_device
*dev
)
594 transport_destroy_device(&dev
->tdev
);
595 put_device(&dev
->tdev
);
599 * ata_tdev_delete -- remove ATA device
600 * @port: ATA PORT to remove
602 * Removes the specified ATA device.
604 static void ata_tdev_delete(struct ata_device
*ata_dev
)
606 struct device
*dev
= &ata_dev
->tdev
;
608 transport_remove_device(dev
);
610 ata_tdev_free(ata_dev
);
615 * ata_tdev_add -- initialize a transport ATA device structure.
616 * @ata_dev: ata_dev structure.
618 * Initialize an ATA device structure for sysfs. It will be added in the
619 * device tree below the ATA LINK device it belongs to.
621 * Returns %0 on success
623 static int ata_tdev_add(struct ata_device
*ata_dev
)
625 struct device
*dev
= &ata_dev
->tdev
;
626 struct ata_link
*link
= ata_dev
->link
;
627 struct ata_port
*ap
= link
->ap
;
630 device_initialize(dev
);
631 dev
->parent
= get_device(&link
->tdev
);
632 dev
->release
= ata_tdev_release
;
633 if (ata_is_host_link(link
))
634 dev_set_name(dev
, "dev%d.%d", ap
->print_id
,ata_dev
->devno
);
636 dev_set_name(dev
, "dev%d.%d.0", ap
->print_id
, link
->pmp
);
638 transport_setup_device(dev
);
639 error
= device_add(dev
);
641 ata_tdev_free(ata_dev
);
645 transport_add_device(dev
);
646 transport_configure_device(dev
);
652 * Setup / Teardown code
655 #define SETUP_TEMPLATE(attrb, field, perm, test) \
656 i->private_##attrb[count] = dev_attr_##field; \
657 i->private_##attrb[count].attr.mode = perm; \
658 i->attrb[count] = &i->private_##attrb[count]; \
662 #define SETUP_LINK_ATTRIBUTE(field) \
663 SETUP_TEMPLATE(link_attrs, field, S_IRUGO, 1)
665 #define SETUP_PORT_ATTRIBUTE(field) \
666 SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
668 #define SETUP_DEV_ATTRIBUTE(field) \
669 SETUP_TEMPLATE(dev_attrs, field, S_IRUGO, 1)
672 * ata_attach_transport -- instantiate ATA transport template
674 struct scsi_transport_template
*ata_attach_transport(void)
676 struct ata_internal
*i
;
679 i
= kzalloc(sizeof(struct ata_internal
), GFP_KERNEL
);
683 i
->t
.eh_strategy_handler
= ata_scsi_error
;
684 i
->t
.eh_timed_out
= ata_scsi_timed_out
;
685 i
->t
.user_scan
= ata_scsi_user_scan
;
687 i
->t
.host_attrs
.ac
.attrs
= &i
->port_attrs
[0];
688 i
->t
.host_attrs
.ac
.class = &ata_port_class
.class;
689 i
->t
.host_attrs
.ac
.match
= ata_tport_match
;
690 transport_container_register(&i
->t
.host_attrs
);
692 i
->link_attr_cont
.ac
.class = &ata_link_class
.class;
693 i
->link_attr_cont
.ac
.attrs
= &i
->link_attrs
[0];
694 i
->link_attr_cont
.ac
.match
= ata_tlink_match
;
695 transport_container_register(&i
->link_attr_cont
);
697 i
->dev_attr_cont
.ac
.class = &ata_dev_class
.class;
698 i
->dev_attr_cont
.ac
.attrs
= &i
->dev_attrs
[0];
699 i
->dev_attr_cont
.ac
.match
= ata_tdev_match
;
700 transport_container_register(&i
->dev_attr_cont
);
703 SETUP_PORT_ATTRIBUTE(nr_pmp_links
);
704 SETUP_PORT_ATTRIBUTE(idle_irq
);
705 BUG_ON(count
> ATA_PORT_ATTRS
);
706 i
->port_attrs
[count
] = NULL
;
709 SETUP_LINK_ATTRIBUTE(hw_sata_spd_limit
);
710 SETUP_LINK_ATTRIBUTE(sata_spd_limit
);
711 SETUP_LINK_ATTRIBUTE(sata_spd
);
712 BUG_ON(count
> ATA_LINK_ATTRS
);
713 i
->link_attrs
[count
] = NULL
;
716 SETUP_DEV_ATTRIBUTE(class);
717 SETUP_DEV_ATTRIBUTE(pio_mode
);
718 SETUP_DEV_ATTRIBUTE(dma_mode
);
719 SETUP_DEV_ATTRIBUTE(xfer_mode
);
720 SETUP_DEV_ATTRIBUTE(spdn_cnt
);
721 SETUP_DEV_ATTRIBUTE(ering
);
722 SETUP_DEV_ATTRIBUTE(id
);
723 SETUP_DEV_ATTRIBUTE(gscr
);
724 BUG_ON(count
> ATA_DEV_ATTRS
);
725 i
->dev_attrs
[count
] = NULL
;
731 * ata_release_transport -- release ATA transport template instance
732 * @t: transport template instance
734 void ata_release_transport(struct scsi_transport_template
*t
)
736 struct ata_internal
*i
= to_ata_internal(t
);
738 transport_container_unregister(&i
->t
.host_attrs
);
739 transport_container_unregister(&i
->link_attr_cont
);
740 transport_container_unregister(&i
->dev_attr_cont
);
745 __init
int libata_transport_init(void)
749 error
= transport_class_register(&ata_link_class
);
751 goto out_unregister_transport
;
752 error
= transport_class_register(&ata_port_class
);
754 goto out_unregister_link
;
755 error
= transport_class_register(&ata_dev_class
);
757 goto out_unregister_port
;
761 transport_class_unregister(&ata_port_class
);
763 transport_class_unregister(&ata_link_class
);
764 out_unregister_transport
:
769 void __exit
libata_transport_exit(void)
771 transport_class_unregister(&ata_link_class
);
772 transport_class_unregister(&ata_port_class
);
773 transport_class_unregister(&ata_dev_class
);