Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  Copyright 2008 ioogle, Inc.  All rights reserved.
0004  *
0005  * Libata transport class.
0006  *
0007  * The ATA transport class contains common code to deal with ATA HBAs,
0008  * an approximated representation of ATA topologies in the driver model,
0009  * and various sysfs attributes to expose these topologies and management
0010  * interfaces to user-space.
0011  *
0012  * There are 3 objects defined in this class:
0013  * - ata_port
0014  * - ata_link
0015  * - ata_device
0016  * Each port has a link object. Each link can have up to two devices for PATA
0017  * and generally one for SATA.
0018  * If there is SATA port multiplier [PMP], 15 additional ata_link object are
0019  * created.
0020  *
0021  * These objects are created when the ata host is initialized and when a PMP is
0022  * found. They are removed only when the HBA is removed, cleaned before the
0023  * error handler runs.
0024  */
0025 
0026 
0027 #include <linux/kernel.h>
0028 #include <linux/blkdev.h>
0029 #include <linux/spinlock.h>
0030 #include <linux/slab.h>
0031 #include <scsi/scsi_transport.h>
0032 #include <linux/libata.h>
0033 #include <linux/hdreg.h>
0034 #include <linux/uaccess.h>
0035 #include <linux/pm_runtime.h>
0036 
0037 #include "libata.h"
0038 #include "libata-transport.h"
0039 
0040 #define ATA_PORT_ATTRS      3
0041 #define ATA_LINK_ATTRS      3
0042 #define ATA_DEV_ATTRS       9
0043 
0044 struct scsi_transport_template;
0045 struct scsi_transport_template *ata_scsi_transport_template;
0046 
0047 struct ata_internal {
0048     struct scsi_transport_template t;
0049 
0050     struct device_attribute private_port_attrs[ATA_PORT_ATTRS];
0051     struct device_attribute private_link_attrs[ATA_LINK_ATTRS];
0052     struct device_attribute private_dev_attrs[ATA_DEV_ATTRS];
0053 
0054     struct transport_container link_attr_cont;
0055     struct transport_container dev_attr_cont;
0056 
0057     /*
0058      * The array of null terminated pointers to attributes
0059      * needed by scsi_sysfs.c
0060      */
0061     struct device_attribute *link_attrs[ATA_LINK_ATTRS + 1];
0062     struct device_attribute *port_attrs[ATA_PORT_ATTRS + 1];
0063     struct device_attribute *dev_attrs[ATA_DEV_ATTRS + 1];
0064 };
0065 #define to_ata_internal(tmpl)   container_of(tmpl, struct ata_internal, t)
0066 
0067 
0068 #define tdev_to_device(d)                   \
0069     container_of((d), struct ata_device, tdev)
0070 #define transport_class_to_dev(dev)             \
0071     tdev_to_device((dev)->parent)
0072 
0073 #define tdev_to_link(d)                     \
0074     container_of((d), struct ata_link, tdev)
0075 #define transport_class_to_link(dev)                \
0076     tdev_to_link((dev)->parent)
0077 
0078 #define tdev_to_port(d)                     \
0079     container_of((d), struct ata_port, tdev)
0080 #define transport_class_to_port(dev)                \
0081     tdev_to_port((dev)->parent)
0082 
0083 
0084 /* Device objects are always created whit link objects */
0085 static int ata_tdev_add(struct ata_device *dev);
0086 static void ata_tdev_delete(struct ata_device *dev);
0087 
0088 
0089 /*
0090  * Hack to allow attributes of the same name in different objects.
0091  */
0092 #define ATA_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
0093     struct device_attribute device_attr_##_prefix##_##_name = \
0094     __ATTR(_name,_mode,_show,_store)
0095 
0096 #define ata_bitfield_name_match(title, table)           \
0097 static ssize_t                          \
0098 get_ata_##title##_names(u32 table_key, char *buf)       \
0099 {                               \
0100     char *prefix = "";                  \
0101     ssize_t len = 0;                    \
0102     int i;                          \
0103                                 \
0104     for (i = 0; i < ARRAY_SIZE(table); i++) {       \
0105         if (table[i].value & table_key) {       \
0106             len += sprintf(buf + len, "%s%s",   \
0107                 prefix, table[i].name);     \
0108             prefix = ", ";              \
0109         }                       \
0110     }                           \
0111     len += sprintf(buf + len, "\n");            \
0112     return len;                     \
0113 }
0114 
0115 #define ata_bitfield_name_search(title, table)          \
0116 static ssize_t                          \
0117 get_ata_##title##_names(u32 table_key, char *buf)       \
0118 {                               \
0119     ssize_t len = 0;                    \
0120     int i;                          \
0121                                 \
0122     for (i = 0; i < ARRAY_SIZE(table); i++) {       \
0123         if (table[i].value == table_key) {      \
0124             len += sprintf(buf + len, "%s",     \
0125                 table[i].name);         \
0126             break;                  \
0127         }                       \
0128     }                           \
0129     len += sprintf(buf + len, "\n");            \
0130     return len;                     \
0131 }
0132 
0133 static struct {
0134     u32     value;
0135     char        *name;
0136 } ata_class_names[] = {
0137     { ATA_DEV_UNKNOWN,      "unknown" },
0138     { ATA_DEV_ATA,          "ata" },
0139     { ATA_DEV_ATA_UNSUP,        "ata" },
0140     { ATA_DEV_ATAPI,        "atapi" },
0141     { ATA_DEV_ATAPI_UNSUP,      "atapi" },
0142     { ATA_DEV_PMP,          "pmp" },
0143     { ATA_DEV_PMP_UNSUP,        "pmp" },
0144     { ATA_DEV_SEMB,         "semb" },
0145     { ATA_DEV_SEMB_UNSUP,       "semb" },
0146     { ATA_DEV_ZAC,          "zac" },
0147     { ATA_DEV_NONE,         "none" }
0148 };
0149 ata_bitfield_name_search(class, ata_class_names)
0150 
0151 
0152 static struct {
0153     u32     value;
0154     char        *name;
0155 } ata_err_names[] = {
0156     { AC_ERR_DEV,           "DeviceError" },
0157     { AC_ERR_HSM,           "HostStateMachineError" },
0158     { AC_ERR_TIMEOUT,       "Timeout" },
0159     { AC_ERR_MEDIA,         "MediaError" },
0160     { AC_ERR_ATA_BUS,       "BusError" },
0161     { AC_ERR_HOST_BUS,      "HostBusError" },
0162     { AC_ERR_SYSTEM,        "SystemError" },
0163     { AC_ERR_INVALID,       "InvalidArg" },
0164     { AC_ERR_OTHER,         "Unknown" },
0165     { AC_ERR_NODEV_HINT,        "NoDeviceHint" },
0166     { AC_ERR_NCQ,           "NCQError" }
0167 };
0168 ata_bitfield_name_match(err, ata_err_names)
0169 
0170 static struct {
0171     u32     value;
0172     char        *name;
0173 } ata_xfer_names[] = {
0174     { XFER_UDMA_7,          "XFER_UDMA_7" },
0175     { XFER_UDMA_6,          "XFER_UDMA_6" },
0176     { XFER_UDMA_5,          "XFER_UDMA_5" },
0177     { XFER_UDMA_4,          "XFER_UDMA_4" },
0178     { XFER_UDMA_3,          "XFER_UDMA_3" },
0179     { XFER_UDMA_2,          "XFER_UDMA_2" },
0180     { XFER_UDMA_1,          "XFER_UDMA_1" },
0181     { XFER_UDMA_0,          "XFER_UDMA_0" },
0182     { XFER_MW_DMA_4,        "XFER_MW_DMA_4" },
0183     { XFER_MW_DMA_3,        "XFER_MW_DMA_3" },
0184     { XFER_MW_DMA_2,        "XFER_MW_DMA_2" },
0185     { XFER_MW_DMA_1,        "XFER_MW_DMA_1" },
0186     { XFER_MW_DMA_0,        "XFER_MW_DMA_0" },
0187     { XFER_SW_DMA_2,        "XFER_SW_DMA_2" },
0188     { XFER_SW_DMA_1,        "XFER_SW_DMA_1" },
0189     { XFER_SW_DMA_0,        "XFER_SW_DMA_0" },
0190     { XFER_PIO_6,           "XFER_PIO_6" },
0191     { XFER_PIO_5,           "XFER_PIO_5" },
0192     { XFER_PIO_4,           "XFER_PIO_4" },
0193     { XFER_PIO_3,           "XFER_PIO_3" },
0194     { XFER_PIO_2,           "XFER_PIO_2" },
0195     { XFER_PIO_1,           "XFER_PIO_1" },
0196     { XFER_PIO_0,           "XFER_PIO_0" },
0197     { XFER_PIO_SLOW,        "XFER_PIO_SLOW" }
0198 };
0199 ata_bitfield_name_search(xfer, ata_xfer_names)
0200 
0201 /*
0202  * ATA Port attributes
0203  */
0204 #define ata_port_show_simple(field, name, format_string, cast)      \
0205 static ssize_t                              \
0206 show_ata_port_##name(struct device *dev,                \
0207              struct device_attribute *attr, char *buf)      \
0208 {                                   \
0209     struct ata_port *ap = transport_class_to_port(dev);     \
0210                                     \
0211     return scnprintf(buf, 20, format_string, cast ap->field);   \
0212 }
0213 
0214 #define ata_port_simple_attr(field, name, format_string, type)      \
0215     ata_port_show_simple(field, name, format_string, (type))    \
0216 static DEVICE_ATTR(name, S_IRUGO, show_ata_port_##name, NULL)
0217 
0218 ata_port_simple_attr(nr_pmp_links, nr_pmp_links, "%d\n", int);
0219 ata_port_simple_attr(stats.idle_irq, idle_irq, "%ld\n", unsigned long);
0220 ata_port_simple_attr(local_port_no, port_no, "%u\n", unsigned int);
0221 
0222 static DECLARE_TRANSPORT_CLASS(ata_port_class,
0223                    "ata_port", NULL, NULL, NULL);
0224 
0225 static void ata_tport_release(struct device *dev)
0226 {
0227     struct ata_port *ap = tdev_to_port(dev);
0228     ata_host_put(ap->host);
0229 }
0230 
0231 /**
0232  * ata_is_port --  check if a struct device represents a ATA port
0233  * @dev:    device to check
0234  *
0235  * Returns:
0236  *  %1 if the device represents a ATA Port, %0 else
0237  */
0238 static int ata_is_port(const struct device *dev)
0239 {
0240     return dev->release == ata_tport_release;
0241 }
0242 
0243 static int ata_tport_match(struct attribute_container *cont,
0244                struct device *dev)
0245 {
0246     if (!ata_is_port(dev))
0247         return 0;
0248     return &ata_scsi_transport_template->host_attrs.ac == cont;
0249 }
0250 
0251 /**
0252  * ata_tport_delete  --  remove ATA PORT
0253  * @ap: ATA PORT to remove
0254  *
0255  * Removes the specified ATA PORT.  Remove the associated link as well.
0256  */
0257 void ata_tport_delete(struct ata_port *ap)
0258 {
0259     struct device *dev = &ap->tdev;
0260 
0261     ata_tlink_delete(&ap->link);
0262 
0263     transport_remove_device(dev);
0264     device_del(dev);
0265     transport_destroy_device(dev);
0266     put_device(dev);
0267 }
0268 
0269 /** ata_tport_add - initialize a transport ATA port structure
0270  *
0271  * @parent: parent device
0272  * @ap:     existing ata_port structure
0273  *
0274  * Initialize a ATA port structure for sysfs.  It will be added to the device
0275  * tree below the device specified by @parent which could be a PCI device.
0276  *
0277  * Returns %0 on success
0278  */
0279 int ata_tport_add(struct device *parent,
0280           struct ata_port *ap)
0281 {
0282     int error;
0283     struct device *dev = &ap->tdev;
0284 
0285     device_initialize(dev);
0286     dev->type = &ata_port_type;
0287 
0288     dev->parent = parent;
0289     ata_host_get(ap->host);
0290     dev->release = ata_tport_release;
0291     dev_set_name(dev, "ata%d", ap->print_id);
0292     transport_setup_device(dev);
0293     ata_acpi_bind_port(ap);
0294     error = device_add(dev);
0295     if (error) {
0296         goto tport_err;
0297     }
0298 
0299     device_enable_async_suspend(dev);
0300     pm_runtime_set_active(dev);
0301     pm_runtime_enable(dev);
0302     pm_runtime_forbid(dev);
0303 
0304     transport_add_device(dev);
0305     transport_configure_device(dev);
0306 
0307     error = ata_tlink_add(&ap->link);
0308     if (error) {
0309         goto tport_link_err;
0310     }
0311     return 0;
0312 
0313  tport_link_err:
0314     transport_remove_device(dev);
0315     device_del(dev);
0316 
0317  tport_err:
0318     transport_destroy_device(dev);
0319     put_device(dev);
0320     ata_host_put(ap->host);
0321     return error;
0322 }
0323 
0324 /**
0325  *     ata_port_classify - determine device type based on ATA-spec signature
0326  *     @ap: ATA port device on which the classification should be run
0327  *     @tf: ATA taskfile register set for device to be identified
0328  *
0329  *     A wrapper around ata_dev_classify() to provide additional logging
0330  *
0331  *     RETURNS:
0332  *     Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP,
0333  *     %ATA_DEV_ZAC, or %ATA_DEV_UNKNOWN the event of failure.
0334  */
0335 unsigned int ata_port_classify(struct ata_port *ap,
0336                    const struct ata_taskfile *tf)
0337 {
0338     int i;
0339     unsigned int class = ata_dev_classify(tf);
0340 
0341     /* Start with index '1' to skip the 'unknown' entry */
0342     for (i = 1; i < ARRAY_SIZE(ata_class_names); i++) {
0343         if (ata_class_names[i].value == class) {
0344             ata_port_dbg(ap, "found %s device by sig\n",
0345                      ata_class_names[i].name);
0346             return class;
0347         }
0348     }
0349 
0350     ata_port_info(ap, "found unknown device (class %u)\n", class);
0351     return class;
0352 }
0353 EXPORT_SYMBOL_GPL(ata_port_classify);
0354 
0355 /*
0356  * ATA link attributes
0357  */
0358 static int noop(int x) { return x; }
0359 
0360 #define ata_link_show_linkspeed(field, format)              \
0361 static ssize_t                              \
0362 show_ata_link_##field(struct device *dev,               \
0363               struct device_attribute *attr, char *buf)     \
0364 {                                   \
0365     struct ata_link *link = transport_class_to_link(dev);       \
0366                                     \
0367     return sprintf(buf, "%s\n", sata_spd_string(format(link->field))); \
0368 }
0369 
0370 #define ata_link_linkspeed_attr(field, format)              \
0371     ata_link_show_linkspeed(field, format)              \
0372 static DEVICE_ATTR(field, S_IRUGO, show_ata_link_##field, NULL)
0373 
0374 ata_link_linkspeed_attr(hw_sata_spd_limit, fls);
0375 ata_link_linkspeed_attr(sata_spd_limit, fls);
0376 ata_link_linkspeed_attr(sata_spd, noop);
0377 
0378 
0379 static DECLARE_TRANSPORT_CLASS(ata_link_class,
0380         "ata_link", NULL, NULL, NULL);
0381 
0382 static void ata_tlink_release(struct device *dev)
0383 {
0384 }
0385 
0386 /**
0387  * ata_is_link --  check if a struct device represents a ATA link
0388  * @dev:    device to check
0389  *
0390  * Returns:
0391  *  %1 if the device represents a ATA link, %0 else
0392  */
0393 static int ata_is_link(const struct device *dev)
0394 {
0395     return dev->release == ata_tlink_release;
0396 }
0397 
0398 static int ata_tlink_match(struct attribute_container *cont,
0399                struct device *dev)
0400 {
0401     struct ata_internal* i = to_ata_internal(ata_scsi_transport_template);
0402     if (!ata_is_link(dev))
0403         return 0;
0404     return &i->link_attr_cont.ac == cont;
0405 }
0406 
0407 /**
0408  * ata_tlink_delete  --  remove ATA LINK
0409  * @link:   ATA LINK to remove
0410  *
0411  * Removes the specified ATA LINK.  remove associated ATA device(s) as well.
0412  */
0413 void ata_tlink_delete(struct ata_link *link)
0414 {
0415     struct device *dev = &link->tdev;
0416     struct ata_device *ata_dev;
0417 
0418     ata_for_each_dev(ata_dev, link, ALL) {
0419         ata_tdev_delete(ata_dev);
0420     }
0421 
0422     transport_remove_device(dev);
0423     device_del(dev);
0424     transport_destroy_device(dev);
0425     put_device(dev);
0426 }
0427 
0428 /**
0429  * ata_tlink_add  --  initialize a transport ATA link structure
0430  * @link:   allocated ata_link structure.
0431  *
0432  * Initialize an ATA LINK structure for sysfs.  It will be added in the
0433  * device tree below the ATA PORT it belongs to.
0434  *
0435  * Returns %0 on success
0436  */
0437 int ata_tlink_add(struct ata_link *link)
0438 {
0439     struct device *dev = &link->tdev;
0440     struct ata_port *ap = link->ap;
0441     struct ata_device *ata_dev;
0442     int error;
0443 
0444     device_initialize(dev);
0445     dev->parent = &ap->tdev;
0446     dev->release = ata_tlink_release;
0447     if (ata_is_host_link(link))
0448         dev_set_name(dev, "link%d", ap->print_id);
0449     else
0450         dev_set_name(dev, "link%d.%d", ap->print_id, link->pmp);
0451 
0452     transport_setup_device(dev);
0453 
0454     error = device_add(dev);
0455     if (error) {
0456         goto tlink_err;
0457     }
0458 
0459     transport_add_device(dev);
0460     transport_configure_device(dev);
0461 
0462     ata_for_each_dev(ata_dev, link, ALL) {
0463         error = ata_tdev_add(ata_dev);
0464         if (error) {
0465             goto tlink_dev_err;
0466         }
0467     }
0468     return 0;
0469   tlink_dev_err:
0470     while (--ata_dev >= link->device) {
0471         ata_tdev_delete(ata_dev);
0472     }
0473     transport_remove_device(dev);
0474     device_del(dev);
0475   tlink_err:
0476     transport_destroy_device(dev);
0477     put_device(dev);
0478     return error;
0479 }
0480 
0481 /*
0482  * ATA device attributes
0483  */
0484 
0485 #define ata_dev_show_class(title, field)                \
0486 static ssize_t                              \
0487 show_ata_dev_##field(struct device *dev,                \
0488              struct device_attribute *attr, char *buf)      \
0489 {                                   \
0490     struct ata_device *ata_dev = transport_class_to_dev(dev);   \
0491                                     \
0492     return get_ata_##title##_names(ata_dev->field, buf);        \
0493 }
0494 
0495 #define ata_dev_attr(title, field)                  \
0496     ata_dev_show_class(title, field)                \
0497 static DEVICE_ATTR(field, S_IRUGO, show_ata_dev_##field, NULL)
0498 
0499 ata_dev_attr(class, class);
0500 ata_dev_attr(xfer, pio_mode);
0501 ata_dev_attr(xfer, dma_mode);
0502 ata_dev_attr(xfer, xfer_mode);
0503 
0504 
0505 #define ata_dev_show_simple(field, format_string, cast)         \
0506 static ssize_t                              \
0507 show_ata_dev_##field(struct device *dev,                \
0508              struct device_attribute *attr, char *buf)      \
0509 {                                   \
0510     struct ata_device *ata_dev = transport_class_to_dev(dev);   \
0511                                     \
0512     return scnprintf(buf, 20, format_string, cast ata_dev->field);  \
0513 }
0514 
0515 #define ata_dev_simple_attr(field, format_string, type)     \
0516     ata_dev_show_simple(field, format_string, (type))   \
0517     static DEVICE_ATTR(field, S_IRUGO,          \
0518            show_ata_dev_##field, NULL)
0519 
0520 ata_dev_simple_attr(spdn_cnt, "%d\n", int);
0521 
0522 struct ata_show_ering_arg {
0523     char* buf;
0524     int written;
0525 };
0526 
0527 static int ata_show_ering(struct ata_ering_entry *ent, void *void_arg)
0528 {
0529     struct ata_show_ering_arg* arg = void_arg;
0530     u64 seconds;
0531     u32 rem;
0532 
0533     seconds = div_u64_rem(ent->timestamp, HZ, &rem);
0534     arg->written += sprintf(arg->buf + arg->written,
0535                 "[%5llu.%09lu]", seconds,
0536                 rem * NSEC_PER_SEC / HZ);
0537     arg->written += get_ata_err_names(ent->err_mask,
0538                       arg->buf + arg->written);
0539     return 0;
0540 }
0541 
0542 static ssize_t
0543 show_ata_dev_ering(struct device *dev,
0544            struct device_attribute *attr, char *buf)
0545 {
0546     struct ata_device *ata_dev = transport_class_to_dev(dev);
0547     struct ata_show_ering_arg arg = { buf, 0 };
0548 
0549     ata_ering_map(&ata_dev->ering, ata_show_ering, &arg);
0550     return arg.written;
0551 }
0552 
0553 
0554 static DEVICE_ATTR(ering, S_IRUGO, show_ata_dev_ering, NULL);
0555 
0556 static ssize_t
0557 show_ata_dev_id(struct device *dev,
0558         struct device_attribute *attr, char *buf)
0559 {
0560     struct ata_device *ata_dev = transport_class_to_dev(dev);
0561     int written = 0, i = 0;
0562 
0563     if (ata_dev->class == ATA_DEV_PMP)
0564         return 0;
0565     for(i=0;i<ATA_ID_WORDS;i++)  {
0566         written += scnprintf(buf+written, 20, "%04x%c",
0567                     ata_dev->id[i],
0568                     ((i+1) & 7) ? ' ' : '\n');
0569     }
0570     return written;
0571 }
0572 
0573 static DEVICE_ATTR(id, S_IRUGO, show_ata_dev_id, NULL);
0574 
0575 static ssize_t
0576 show_ata_dev_gscr(struct device *dev,
0577           struct device_attribute *attr, char *buf)
0578 {
0579     struct ata_device *ata_dev = transport_class_to_dev(dev);
0580     int written = 0, i = 0;
0581 
0582     if (ata_dev->class != ATA_DEV_PMP)
0583         return 0;
0584     for(i=0;i<SATA_PMP_GSCR_DWORDS;i++)  {
0585         written += scnprintf(buf+written, 20, "%08x%c",
0586                     ata_dev->gscr[i],
0587                     ((i+1) & 3) ? ' ' : '\n');
0588     }
0589     if (SATA_PMP_GSCR_DWORDS & 3)
0590         buf[written-1] = '\n';
0591     return written;
0592 }
0593 
0594 static DEVICE_ATTR(gscr, S_IRUGO, show_ata_dev_gscr, NULL);
0595 
0596 static ssize_t
0597 show_ata_dev_trim(struct device *dev,
0598           struct device_attribute *attr, char *buf)
0599 {
0600     struct ata_device *ata_dev = transport_class_to_dev(dev);
0601     unsigned char *mode;
0602 
0603     if (!ata_id_has_trim(ata_dev->id))
0604         mode = "unsupported";
0605     else if (ata_dev->horkage & ATA_HORKAGE_NOTRIM)
0606         mode = "forced_unsupported";
0607     else if (ata_dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM)
0608             mode = "forced_unqueued";
0609     else if (ata_fpdma_dsm_supported(ata_dev))
0610         mode = "queued";
0611     else
0612         mode = "unqueued";
0613 
0614     return scnprintf(buf, 20, "%s\n", mode);
0615 }
0616 
0617 static DEVICE_ATTR(trim, S_IRUGO, show_ata_dev_trim, NULL);
0618 
0619 static DECLARE_TRANSPORT_CLASS(ata_dev_class,
0620                    "ata_device", NULL, NULL, NULL);
0621 
0622 static void ata_tdev_release(struct device *dev)
0623 {
0624 }
0625 
0626 /**
0627  * ata_is_ata_dev  --  check if a struct device represents a ATA device
0628  * @dev:    device to check
0629  *
0630  * Returns:
0631  *  %1 if the device represents a ATA device, %0 else
0632  */
0633 static int ata_is_ata_dev(const struct device *dev)
0634 {
0635     return dev->release == ata_tdev_release;
0636 }
0637 
0638 static int ata_tdev_match(struct attribute_container *cont,
0639               struct device *dev)
0640 {
0641     struct ata_internal* i = to_ata_internal(ata_scsi_transport_template);
0642     if (!ata_is_ata_dev(dev))
0643         return 0;
0644     return &i->dev_attr_cont.ac == cont;
0645 }
0646 
0647 /**
0648  * ata_tdev_free  --  free a ATA LINK
0649  * @dev:    ATA PHY to free
0650  *
0651  * Frees the specified ATA PHY.
0652  *
0653  * Note:
0654  *   This function must only be called on a PHY that has not
0655  *   successfully been added using ata_tdev_add().
0656  */
0657 static void ata_tdev_free(struct ata_device *dev)
0658 {
0659     transport_destroy_device(&dev->tdev);
0660     put_device(&dev->tdev);
0661 }
0662 
0663 /**
0664  * ata_tdev_delete  --  remove ATA device
0665  * @ata_dev:    ATA device to remove
0666  *
0667  * Removes the specified ATA device.
0668  */
0669 static void ata_tdev_delete(struct ata_device *ata_dev)
0670 {
0671     struct device *dev = &ata_dev->tdev;
0672 
0673     transport_remove_device(dev);
0674     device_del(dev);
0675     ata_tdev_free(ata_dev);
0676 }
0677 
0678 
0679 /**
0680  * ata_tdev_add  --  initialize a transport ATA device structure.
0681  * @ata_dev:    ata_dev structure.
0682  *
0683  * Initialize an ATA device structure for sysfs.  It will be added in the
0684  * device tree below the ATA LINK device it belongs to.
0685  *
0686  * Returns %0 on success
0687  */
0688 static int ata_tdev_add(struct ata_device *ata_dev)
0689 {
0690     struct device *dev = &ata_dev->tdev;
0691     struct ata_link *link = ata_dev->link;
0692     struct ata_port *ap = link->ap;
0693     int error;
0694 
0695     device_initialize(dev);
0696     dev->parent = &link->tdev;
0697     dev->release = ata_tdev_release;
0698     if (ata_is_host_link(link))
0699         dev_set_name(dev, "dev%d.%d", ap->print_id,ata_dev->devno);
0700     else
0701         dev_set_name(dev, "dev%d.%d.0", ap->print_id, link->pmp);
0702 
0703     transport_setup_device(dev);
0704     ata_acpi_bind_dev(ata_dev);
0705     error = device_add(dev);
0706     if (error) {
0707         ata_tdev_free(ata_dev);
0708         return error;
0709     }
0710 
0711     transport_add_device(dev);
0712     transport_configure_device(dev);
0713     return 0;
0714 }
0715 
0716 
0717 /*
0718  * Setup / Teardown code
0719  */
0720 
0721 #define SETUP_TEMPLATE(attrb, field, perm, test)            \
0722     i->private_##attrb[count] = dev_attr_##field;           \
0723     i->private_##attrb[count].attr.mode = perm;         \
0724     i->attrb[count] = &i->private_##attrb[count];           \
0725     if (test)                           \
0726         count++
0727 
0728 #define SETUP_LINK_ATTRIBUTE(field)                 \
0729     SETUP_TEMPLATE(link_attrs, field, S_IRUGO, 1)
0730 
0731 #define SETUP_PORT_ATTRIBUTE(field)                 \
0732     SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
0733 
0734 #define SETUP_DEV_ATTRIBUTE(field)                  \
0735     SETUP_TEMPLATE(dev_attrs, field, S_IRUGO, 1)
0736 
0737 /**
0738  * ata_attach_transport  --  instantiate ATA transport template
0739  */
0740 struct scsi_transport_template *ata_attach_transport(void)
0741 {
0742     struct ata_internal *i;
0743     int count;
0744 
0745     i = kzalloc(sizeof(struct ata_internal), GFP_KERNEL);
0746     if (!i)
0747         return NULL;
0748 
0749     i->t.eh_strategy_handler    = ata_scsi_error;
0750     i->t.user_scan          = ata_scsi_user_scan;
0751 
0752     i->t.host_attrs.ac.attrs = &i->port_attrs[0];
0753     i->t.host_attrs.ac.class = &ata_port_class.class;
0754     i->t.host_attrs.ac.match = ata_tport_match;
0755     transport_container_register(&i->t.host_attrs);
0756 
0757     i->link_attr_cont.ac.class = &ata_link_class.class;
0758     i->link_attr_cont.ac.attrs = &i->link_attrs[0];
0759     i->link_attr_cont.ac.match = ata_tlink_match;
0760     transport_container_register(&i->link_attr_cont);
0761 
0762     i->dev_attr_cont.ac.class = &ata_dev_class.class;
0763     i->dev_attr_cont.ac.attrs = &i->dev_attrs[0];
0764     i->dev_attr_cont.ac.match = ata_tdev_match;
0765     transport_container_register(&i->dev_attr_cont);
0766 
0767     count = 0;
0768     SETUP_PORT_ATTRIBUTE(nr_pmp_links);
0769     SETUP_PORT_ATTRIBUTE(idle_irq);
0770     SETUP_PORT_ATTRIBUTE(port_no);
0771     BUG_ON(count > ATA_PORT_ATTRS);
0772     i->port_attrs[count] = NULL;
0773 
0774     count = 0;
0775     SETUP_LINK_ATTRIBUTE(hw_sata_spd_limit);
0776     SETUP_LINK_ATTRIBUTE(sata_spd_limit);
0777     SETUP_LINK_ATTRIBUTE(sata_spd);
0778     BUG_ON(count > ATA_LINK_ATTRS);
0779     i->link_attrs[count] = NULL;
0780 
0781     count = 0;
0782     SETUP_DEV_ATTRIBUTE(class);
0783     SETUP_DEV_ATTRIBUTE(pio_mode);
0784     SETUP_DEV_ATTRIBUTE(dma_mode);
0785     SETUP_DEV_ATTRIBUTE(xfer_mode);
0786     SETUP_DEV_ATTRIBUTE(spdn_cnt);
0787     SETUP_DEV_ATTRIBUTE(ering);
0788     SETUP_DEV_ATTRIBUTE(id);
0789     SETUP_DEV_ATTRIBUTE(gscr);
0790     SETUP_DEV_ATTRIBUTE(trim);
0791     BUG_ON(count > ATA_DEV_ATTRS);
0792     i->dev_attrs[count] = NULL;
0793 
0794     return &i->t;
0795 }
0796 
0797 /**
0798  * ata_release_transport  --  release ATA transport template instance
0799  * @t:      transport template instance
0800  */
0801 void ata_release_transport(struct scsi_transport_template *t)
0802 {
0803     struct ata_internal *i = to_ata_internal(t);
0804 
0805     transport_container_unregister(&i->t.host_attrs);
0806     transport_container_unregister(&i->link_attr_cont);
0807     transport_container_unregister(&i->dev_attr_cont);
0808 
0809     kfree(i);
0810 }
0811 
0812 __init int libata_transport_init(void)
0813 {
0814     int error;
0815 
0816     error = transport_class_register(&ata_link_class);
0817     if (error)
0818         goto out_unregister_transport;
0819     error = transport_class_register(&ata_port_class);
0820     if (error)
0821         goto out_unregister_link;
0822     error = transport_class_register(&ata_dev_class);
0823     if (error)
0824         goto out_unregister_port;
0825     return 0;
0826 
0827  out_unregister_port:
0828     transport_class_unregister(&ata_port_class);
0829  out_unregister_link:
0830     transport_class_unregister(&ata_link_class);
0831  out_unregister_transport:
0832     return error;
0833 
0834 }
0835 
0836 void __exit libata_transport_exit(void)
0837 {
0838     transport_class_unregister(&ata_link_class);
0839     transport_class_unregister(&ata_port_class);
0840     transport_class_unregister(&ata_dev_class);
0841 }