Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 ioogle, Inc. All rights reserved. |
| 3 | * Released under GPL v2. |
| 4 | * |
| 5 | * Libata transport class. |
| 6 | * |
| 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. |
| 11 | * |
| 12 | * There are 3 objects defined in in this class: |
| 13 | * - ata_port |
| 14 | * - ata_link |
| 15 | * - ata_device |
| 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 |
| 19 | * created. |
| 20 | * |
| 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 |
| 23 | * error handler runs. |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | #include <linux/kernel.h> |
| 28 | #include <linux/blkdev.h> |
| 29 | #include <linux/spinlock.h> |
Jeff Garzik | 6a2148c | 2010-08-19 16:11:32 -0400 | [diff] [blame] | 30 | #include <linux/slab.h> |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 31 | #include <scsi/scsi_transport.h> |
| 32 | #include <linux/libata.h> |
| 33 | #include <linux/hdreg.h> |
| 34 | #include <linux/uaccess.h> |
Lin Ming | 9ee4f39 | 2011-12-05 09:20:28 +0800 | [diff] [blame] | 35 | #include <linux/pm_runtime.h> |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 36 | |
| 37 | #include "libata.h" |
| 38 | #include "libata-transport.h" |
| 39 | |
David Milburn | e628dc9 | 2013-05-14 13:48:40 -0500 | [diff] [blame] | 40 | #define ATA_PORT_ATTRS 3 |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 41 | #define ATA_LINK_ATTRS 3 |
| 42 | #define ATA_DEV_ATTRS 9 |
| 43 | |
| 44 | struct scsi_transport_template; |
| 45 | struct scsi_transport_template *ata_scsi_transport_template; |
| 46 | |
| 47 | struct ata_internal { |
| 48 | struct scsi_transport_template t; |
| 49 | |
| 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]; |
| 53 | |
| 54 | struct transport_container link_attr_cont; |
| 55 | struct transport_container dev_attr_cont; |
| 56 | |
| 57 | /* |
| 58 | * The array of null terminated pointers to attributes |
| 59 | * needed by scsi_sysfs.c |
| 60 | */ |
| 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]; |
| 64 | }; |
| 65 | #define to_ata_internal(tmpl) container_of(tmpl, struct ata_internal, t) |
| 66 | |
| 67 | |
| 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) |
| 72 | |
| 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) |
| 77 | |
| 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) |
| 82 | |
| 83 | |
| 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); |
| 87 | |
| 88 | |
| 89 | /* |
| 90 | * Hack to allow attributes of the same name in different objects. |
| 91 | */ |
| 92 | #define ATA_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \ |
| 93 | struct device_attribute device_attr_##_prefix##_##_name = \ |
| 94 | __ATTR(_name,_mode,_show,_store) |
| 95 | |
| 96 | #define ata_bitfield_name_match(title, table) \ |
| 97 | static ssize_t \ |
| 98 | get_ata_##title##_names(u32 table_key, char *buf) \ |
| 99 | { \ |
| 100 | char *prefix = ""; \ |
| 101 | ssize_t len = 0; \ |
| 102 | int i; \ |
| 103 | \ |
| 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); \ |
| 108 | prefix = ", "; \ |
| 109 | } \ |
| 110 | } \ |
| 111 | len += sprintf(buf + len, "\n"); \ |
| 112 | return len; \ |
| 113 | } |
| 114 | |
| 115 | #define ata_bitfield_name_search(title, table) \ |
| 116 | static ssize_t \ |
| 117 | get_ata_##title##_names(u32 table_key, char *buf) \ |
| 118 | { \ |
| 119 | ssize_t len = 0; \ |
| 120 | int i; \ |
| 121 | \ |
| 122 | for (i = 0; i < ARRAY_SIZE(table); i++) { \ |
| 123 | if (table[i].value == table_key) { \ |
| 124 | len += sprintf(buf + len, "%s", \ |
| 125 | table[i].name); \ |
| 126 | break; \ |
| 127 | } \ |
| 128 | } \ |
| 129 | len += sprintf(buf + len, "\n"); \ |
| 130 | return len; \ |
| 131 | } |
| 132 | |
| 133 | static struct { |
| 134 | u32 value; |
| 135 | char *name; |
| 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" }, |
Hannes Reinecke | 9162c65 | 2014-11-05 13:08:21 +0100 | [diff] [blame] | 146 | { ATA_DEV_ZAC, "zac" }, |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 147 | { ATA_DEV_NONE, "none" } |
| 148 | }; |
| 149 | ata_bitfield_name_search(class, ata_class_names) |
| 150 | |
| 151 | |
| 152 | static struct { |
| 153 | u32 value; |
| 154 | char *name; |
| 155 | } ata_err_names[] = { |
| 156 | { AC_ERR_DEV, "DeviceError" }, |
| 157 | { AC_ERR_HSM, "HostStateMachineError" }, |
| 158 | { AC_ERR_TIMEOUT, "Timeout" }, |
| 159 | { AC_ERR_MEDIA, "MediaError" }, |
| 160 | { AC_ERR_ATA_BUS, "BusError" }, |
| 161 | { AC_ERR_HOST_BUS, "HostBusError" }, |
| 162 | { AC_ERR_SYSTEM, "SystemError" }, |
| 163 | { AC_ERR_INVALID, "InvalidArg" }, |
| 164 | { AC_ERR_OTHER, "Unknown" }, |
| 165 | { AC_ERR_NODEV_HINT, "NoDeviceHint" }, |
| 166 | { AC_ERR_NCQ, "NCQError" } |
| 167 | }; |
| 168 | ata_bitfield_name_match(err, ata_err_names) |
| 169 | |
| 170 | static struct { |
| 171 | u32 value; |
| 172 | char *name; |
| 173 | } ata_xfer_names[] = { |
| 174 | { XFER_UDMA_7, "XFER_UDMA_7" }, |
| 175 | { XFER_UDMA_6, "XFER_UDMA_6" }, |
| 176 | { XFER_UDMA_5, "XFER_UDMA_5" }, |
| 177 | { XFER_UDMA_4, "XFER_UDMA_4" }, |
| 178 | { XFER_UDMA_3, "XFER_UDMA_3" }, |
| 179 | { XFER_UDMA_2, "XFER_UDMA_2" }, |
| 180 | { XFER_UDMA_1, "XFER_UDMA_1" }, |
| 181 | { XFER_UDMA_0, "XFER_UDMA_0" }, |
| 182 | { XFER_MW_DMA_4, "XFER_MW_DMA_4" }, |
| 183 | { XFER_MW_DMA_3, "XFER_MW_DMA_3" }, |
| 184 | { XFER_MW_DMA_2, "XFER_MW_DMA_2" }, |
| 185 | { XFER_MW_DMA_1, "XFER_MW_DMA_1" }, |
| 186 | { XFER_MW_DMA_0, "XFER_MW_DMA_0" }, |
| 187 | { XFER_SW_DMA_2, "XFER_SW_DMA_2" }, |
| 188 | { XFER_SW_DMA_1, "XFER_SW_DMA_1" }, |
| 189 | { XFER_SW_DMA_0, "XFER_SW_DMA_0" }, |
| 190 | { XFER_PIO_6, "XFER_PIO_6" }, |
| 191 | { XFER_PIO_5, "XFER_PIO_5" }, |
| 192 | { XFER_PIO_4, "XFER_PIO_4" }, |
| 193 | { XFER_PIO_3, "XFER_PIO_3" }, |
| 194 | { XFER_PIO_2, "XFER_PIO_2" }, |
| 195 | { XFER_PIO_1, "XFER_PIO_1" }, |
| 196 | { XFER_PIO_0, "XFER_PIO_0" }, |
| 197 | { XFER_PIO_SLOW, "XFER_PIO_SLOW" } |
| 198 | }; |
| 199 | ata_bitfield_name_match(xfer,ata_xfer_names) |
| 200 | |
| 201 | /* |
| 202 | * ATA Port attributes |
| 203 | */ |
| 204 | #define ata_port_show_simple(field, name, format_string, cast) \ |
| 205 | static ssize_t \ |
| 206 | show_ata_port_##name(struct device *dev, \ |
| 207 | struct device_attribute *attr, char *buf) \ |
| 208 | { \ |
| 209 | struct ata_port *ap = transport_class_to_port(dev); \ |
| 210 | \ |
| 211 | return snprintf(buf, 20, format_string, cast ap->field); \ |
| 212 | } |
| 213 | |
| 214 | #define ata_port_simple_attr(field, name, format_string, type) \ |
| 215 | ata_port_show_simple(field, name, format_string, (type)) \ |
| 216 | static DEVICE_ATTR(name, S_IRUGO, show_ata_port_##name, NULL) |
| 217 | |
| 218 | ata_port_simple_attr(nr_pmp_links, nr_pmp_links, "%d\n", int); |
| 219 | ata_port_simple_attr(stats.idle_irq, idle_irq, "%ld\n", unsigned long); |
David Milburn | e628dc9 | 2013-05-14 13:48:40 -0500 | [diff] [blame] | 220 | ata_port_simple_attr(local_port_no, port_no, "%u\n", unsigned int); |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 221 | |
| 222 | static DECLARE_TRANSPORT_CLASS(ata_port_class, |
| 223 | "ata_port", NULL, NULL, NULL); |
| 224 | |
| 225 | static void ata_tport_release(struct device *dev) |
| 226 | { |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /** |
| 230 | * ata_is_port -- check if a struct device represents a ATA port |
| 231 | * @dev: device to check |
| 232 | * |
| 233 | * Returns: |
| 234 | * %1 if the device represents a ATA Port, %0 else |
| 235 | */ |
H Hartley Sweeten | b0e73af | 2012-04-16 18:04:41 -0700 | [diff] [blame] | 236 | static int ata_is_port(const struct device *dev) |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 237 | { |
| 238 | return dev->release == ata_tport_release; |
| 239 | } |
| 240 | |
| 241 | static int ata_tport_match(struct attribute_container *cont, |
| 242 | struct device *dev) |
| 243 | { |
| 244 | if (!ata_is_port(dev)) |
| 245 | return 0; |
| 246 | return &ata_scsi_transport_template->host_attrs.ac == cont; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * ata_tport_delete -- remove ATA PORT |
| 251 | * @port: ATA PORT to remove |
| 252 | * |
| 253 | * Removes the specified ATA PORT. Remove the associated link as well. |
| 254 | */ |
| 255 | void ata_tport_delete(struct ata_port *ap) |
| 256 | { |
| 257 | struct device *dev = &ap->tdev; |
| 258 | |
| 259 | ata_tlink_delete(&ap->link); |
| 260 | |
| 261 | transport_remove_device(dev); |
| 262 | device_del(dev); |
| 263 | transport_destroy_device(dev); |
| 264 | put_device(dev); |
| 265 | } |
| 266 | |
| 267 | /** ata_tport_add - initialize a transport ATA port structure |
| 268 | * |
| 269 | * @parent: parent device |
| 270 | * @ap: existing ata_port structure |
| 271 | * |
| 272 | * Initialize a ATA port structure for sysfs. It will be added to the device |
| 273 | * tree below the device specified by @parent which could be a PCI device. |
| 274 | * |
| 275 | * Returns %0 on success |
| 276 | */ |
| 277 | int ata_tport_add(struct device *parent, |
| 278 | struct ata_port *ap) |
| 279 | { |
| 280 | int error; |
| 281 | struct device *dev = &ap->tdev; |
| 282 | |
| 283 | device_initialize(dev); |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 284 | dev->type = &ata_port_type; |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 285 | |
Gwendal Grignou | d85fc67 | 2017-03-03 09:00:09 -0800 | [diff] [blame] | 286 | dev->parent = parent; |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 287 | dev->release = ata_tport_release; |
| 288 | dev_set_name(dev, "ata%d", ap->print_id); |
| 289 | transport_setup_device(dev); |
Aaron Lu | f1bc1e4 | 2013-08-23 10:17:54 +0800 | [diff] [blame] | 290 | ata_acpi_bind_port(ap); |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 291 | error = device_add(dev); |
| 292 | if (error) { |
| 293 | goto tport_err; |
| 294 | } |
| 295 | |
Lin Ming | 966f121 | 2012-01-16 13:23:23 +0800 | [diff] [blame] | 296 | device_enable_async_suspend(dev); |
Lin Ming | 9ee4f39 | 2011-12-05 09:20:28 +0800 | [diff] [blame] | 297 | pm_runtime_set_active(dev); |
| 298 | pm_runtime_enable(dev); |
Lin Ming | 0c8d32c | 2012-04-18 09:29:47 +0800 | [diff] [blame] | 299 | pm_runtime_forbid(dev); |
Lin Ming | 9ee4f39 | 2011-12-05 09:20:28 +0800 | [diff] [blame] | 300 | |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 301 | transport_add_device(dev); |
| 302 | transport_configure_device(dev); |
| 303 | |
| 304 | error = ata_tlink_add(&ap->link); |
| 305 | if (error) { |
| 306 | goto tport_link_err; |
| 307 | } |
| 308 | return 0; |
| 309 | |
| 310 | tport_link_err: |
| 311 | transport_remove_device(dev); |
| 312 | device_del(dev); |
| 313 | |
| 314 | tport_err: |
| 315 | transport_destroy_device(dev); |
| 316 | put_device(dev); |
| 317 | return error; |
| 318 | } |
| 319 | |
| 320 | |
| 321 | /* |
| 322 | * ATA link attributes |
| 323 | */ |
Gwendal Grignou | 3e85c3e | 2013-10-25 16:28:57 -0700 | [diff] [blame] | 324 | static int noop(int x) { return x; } |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 325 | |
Gwendal Grignou | 3e85c3e | 2013-10-25 16:28:57 -0700 | [diff] [blame] | 326 | #define ata_link_show_linkspeed(field, format) \ |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 327 | static ssize_t \ |
| 328 | show_ata_link_##field(struct device *dev, \ |
| 329 | struct device_attribute *attr, char *buf) \ |
| 330 | { \ |
| 331 | struct ata_link *link = transport_class_to_link(dev); \ |
| 332 | \ |
Gwendal Grignou | 3e85c3e | 2013-10-25 16:28:57 -0700 | [diff] [blame] | 333 | return sprintf(buf, "%s\n", sata_spd_string(format(link->field))); \ |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Gwendal Grignou | 3e85c3e | 2013-10-25 16:28:57 -0700 | [diff] [blame] | 336 | #define ata_link_linkspeed_attr(field, format) \ |
| 337 | ata_link_show_linkspeed(field, format) \ |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 338 | static DEVICE_ATTR(field, S_IRUGO, show_ata_link_##field, NULL) |
| 339 | |
Gwendal Grignou | 3e85c3e | 2013-10-25 16:28:57 -0700 | [diff] [blame] | 340 | ata_link_linkspeed_attr(hw_sata_spd_limit, fls); |
| 341 | ata_link_linkspeed_attr(sata_spd_limit, fls); |
| 342 | ata_link_linkspeed_attr(sata_spd, noop); |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 343 | |
| 344 | |
| 345 | static DECLARE_TRANSPORT_CLASS(ata_link_class, |
| 346 | "ata_link", NULL, NULL, NULL); |
| 347 | |
| 348 | static void ata_tlink_release(struct device *dev) |
| 349 | { |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | /** |
| 353 | * ata_is_link -- check if a struct device represents a ATA link |
| 354 | * @dev: device to check |
| 355 | * |
| 356 | * Returns: |
| 357 | * %1 if the device represents a ATA link, %0 else |
| 358 | */ |
H Hartley Sweeten | b0e73af | 2012-04-16 18:04:41 -0700 | [diff] [blame] | 359 | static int ata_is_link(const struct device *dev) |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 360 | { |
| 361 | return dev->release == ata_tlink_release; |
| 362 | } |
| 363 | |
| 364 | static int ata_tlink_match(struct attribute_container *cont, |
| 365 | struct device *dev) |
| 366 | { |
| 367 | struct ata_internal* i = to_ata_internal(ata_scsi_transport_template); |
| 368 | if (!ata_is_link(dev)) |
| 369 | return 0; |
| 370 | return &i->link_attr_cont.ac == cont; |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * ata_tlink_delete -- remove ATA LINK |
| 375 | * @port: ATA LINK to remove |
| 376 | * |
| 377 | * Removes the specified ATA LINK. remove associated ATA device(s) as well. |
| 378 | */ |
| 379 | void ata_tlink_delete(struct ata_link *link) |
| 380 | { |
| 381 | struct device *dev = &link->tdev; |
| 382 | struct ata_device *ata_dev; |
| 383 | |
| 384 | ata_for_each_dev(ata_dev, link, ALL) { |
| 385 | ata_tdev_delete(ata_dev); |
| 386 | } |
| 387 | |
| 388 | transport_remove_device(dev); |
| 389 | device_del(dev); |
| 390 | transport_destroy_device(dev); |
| 391 | put_device(dev); |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * ata_tlink_add -- initialize a transport ATA link structure |
| 396 | * @link: allocated ata_link structure. |
| 397 | * |
| 398 | * Initialize an ATA LINK structure for sysfs. It will be added in the |
| 399 | * device tree below the ATA PORT it belongs to. |
| 400 | * |
| 401 | * Returns %0 on success |
| 402 | */ |
| 403 | int ata_tlink_add(struct ata_link *link) |
| 404 | { |
| 405 | struct device *dev = &link->tdev; |
| 406 | struct ata_port *ap = link->ap; |
| 407 | struct ata_device *ata_dev; |
| 408 | int error; |
| 409 | |
| 410 | device_initialize(dev); |
Gwendal Grignou | d85fc67 | 2017-03-03 09:00:09 -0800 | [diff] [blame] | 411 | dev->parent = &ap->tdev; |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 412 | dev->release = ata_tlink_release; |
| 413 | if (ata_is_host_link(link)) |
| 414 | dev_set_name(dev, "link%d", ap->print_id); |
| 415 | else |
| 416 | dev_set_name(dev, "link%d.%d", ap->print_id, link->pmp); |
| 417 | |
| 418 | transport_setup_device(dev); |
| 419 | |
| 420 | error = device_add(dev); |
| 421 | if (error) { |
| 422 | goto tlink_err; |
| 423 | } |
| 424 | |
| 425 | transport_add_device(dev); |
| 426 | transport_configure_device(dev); |
| 427 | |
| 428 | ata_for_each_dev(ata_dev, link, ALL) { |
| 429 | error = ata_tdev_add(ata_dev); |
| 430 | if (error) { |
| 431 | goto tlink_dev_err; |
| 432 | } |
| 433 | } |
| 434 | return 0; |
| 435 | tlink_dev_err: |
| 436 | while (--ata_dev >= link->device) { |
| 437 | ata_tdev_delete(ata_dev); |
| 438 | } |
| 439 | transport_remove_device(dev); |
| 440 | device_del(dev); |
| 441 | tlink_err: |
| 442 | transport_destroy_device(dev); |
| 443 | put_device(dev); |
| 444 | return error; |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | * ATA device attributes |
| 449 | */ |
| 450 | |
| 451 | #define ata_dev_show_class(title, field) \ |
| 452 | static ssize_t \ |
| 453 | show_ata_dev_##field(struct device *dev, \ |
| 454 | struct device_attribute *attr, char *buf) \ |
| 455 | { \ |
| 456 | struct ata_device *ata_dev = transport_class_to_dev(dev); \ |
| 457 | \ |
| 458 | return get_ata_##title##_names(ata_dev->field, buf); \ |
| 459 | } |
| 460 | |
| 461 | #define ata_dev_attr(title, field) \ |
| 462 | ata_dev_show_class(title, field) \ |
| 463 | static DEVICE_ATTR(field, S_IRUGO, show_ata_dev_##field, NULL) |
| 464 | |
| 465 | ata_dev_attr(class, class); |
| 466 | ata_dev_attr(xfer, pio_mode); |
| 467 | ata_dev_attr(xfer, dma_mode); |
| 468 | ata_dev_attr(xfer, xfer_mode); |
| 469 | |
| 470 | |
| 471 | #define ata_dev_show_simple(field, format_string, cast) \ |
| 472 | static ssize_t \ |
| 473 | show_ata_dev_##field(struct device *dev, \ |
| 474 | struct device_attribute *attr, char *buf) \ |
| 475 | { \ |
| 476 | struct ata_device *ata_dev = transport_class_to_dev(dev); \ |
| 477 | \ |
| 478 | return snprintf(buf, 20, format_string, cast ata_dev->field); \ |
| 479 | } |
| 480 | |
| 481 | #define ata_dev_simple_attr(field, format_string, type) \ |
| 482 | ata_dev_show_simple(field, format_string, (type)) \ |
| 483 | static DEVICE_ATTR(field, S_IRUGO, \ |
| 484 | show_ata_dev_##field, NULL) |
| 485 | |
| 486 | ata_dev_simple_attr(spdn_cnt, "%d\n", int); |
| 487 | |
| 488 | struct ata_show_ering_arg { |
| 489 | char* buf; |
| 490 | int written; |
| 491 | }; |
| 492 | |
| 493 | static int ata_show_ering(struct ata_ering_entry *ent, void *void_arg) |
| 494 | { |
| 495 | struct ata_show_ering_arg* arg = void_arg; |
Arnd Bergmann | f3f99d3 | 2016-06-17 17:37:12 +0200 | [diff] [blame] | 496 | u64 seconds; |
| 497 | u32 rem; |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 498 | |
Arnd Bergmann | f3f99d3 | 2016-06-17 17:37:12 +0200 | [diff] [blame] | 499 | seconds = div_u64_rem(ent->timestamp, HZ, &rem); |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 500 | arg->written += sprintf(arg->buf + arg->written, |
Arnd Bergmann | f3f99d3 | 2016-06-17 17:37:12 +0200 | [diff] [blame] | 501 | "[%5llu.%09lu]", seconds, |
| 502 | rem * NSEC_PER_SEC / HZ); |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 503 | arg->written += get_ata_err_names(ent->err_mask, |
| 504 | arg->buf + arg->written); |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | static ssize_t |
| 509 | show_ata_dev_ering(struct device *dev, |
| 510 | struct device_attribute *attr, char *buf) |
| 511 | { |
| 512 | struct ata_device *ata_dev = transport_class_to_dev(dev); |
| 513 | struct ata_show_ering_arg arg = { buf, 0 }; |
| 514 | |
| 515 | ata_ering_map(&ata_dev->ering, ata_show_ering, &arg); |
| 516 | return arg.written; |
| 517 | } |
| 518 | |
| 519 | |
| 520 | static DEVICE_ATTR(ering, S_IRUGO, show_ata_dev_ering, NULL); |
| 521 | |
| 522 | static ssize_t |
| 523 | show_ata_dev_id(struct device *dev, |
| 524 | struct device_attribute *attr, char *buf) |
| 525 | { |
| 526 | struct ata_device *ata_dev = transport_class_to_dev(dev); |
| 527 | int written = 0, i = 0; |
| 528 | |
| 529 | if (ata_dev->class == ATA_DEV_PMP) |
| 530 | return 0; |
| 531 | for(i=0;i<ATA_ID_WORDS;i++) { |
| 532 | written += snprintf(buf+written, 20, "%04x%c", |
| 533 | ata_dev->id[i], |
| 534 | ((i+1) & 7) ? ' ' : '\n'); |
| 535 | } |
| 536 | return written; |
| 537 | } |
| 538 | |
| 539 | static DEVICE_ATTR(id, S_IRUGO, show_ata_dev_id, NULL); |
| 540 | |
| 541 | static ssize_t |
| 542 | show_ata_dev_gscr(struct device *dev, |
| 543 | struct device_attribute *attr, char *buf) |
| 544 | { |
| 545 | struct ata_device *ata_dev = transport_class_to_dev(dev); |
| 546 | int written = 0, i = 0; |
| 547 | |
| 548 | if (ata_dev->class != ATA_DEV_PMP) |
| 549 | return 0; |
| 550 | for(i=0;i<SATA_PMP_GSCR_DWORDS;i++) { |
| 551 | written += snprintf(buf+written, 20, "%08x%c", |
| 552 | ata_dev->gscr[i], |
| 553 | ((i+1) & 3) ? ' ' : '\n'); |
| 554 | } |
| 555 | if (SATA_PMP_GSCR_DWORDS & 3) |
| 556 | buf[written-1] = '\n'; |
| 557 | return written; |
| 558 | } |
| 559 | |
| 560 | static DEVICE_ATTR(gscr, S_IRUGO, show_ata_dev_gscr, NULL); |
| 561 | |
Martin K. Petersen | f303074 | 2015-05-04 21:54:19 -0400 | [diff] [blame] | 562 | static ssize_t |
| 563 | show_ata_dev_trim(struct device *dev, |
| 564 | struct device_attribute *attr, char *buf) |
| 565 | { |
| 566 | struct ata_device *ata_dev = transport_class_to_dev(dev); |
| 567 | unsigned char *mode; |
| 568 | |
| 569 | if (!ata_id_has_trim(ata_dev->id)) |
| 570 | mode = "unsupported"; |
Arne Fitzenreiter | 71d126f | 2015-07-15 13:54:36 +0200 | [diff] [blame] | 571 | else if (ata_dev->horkage & ATA_HORKAGE_NOTRIM) |
| 572 | mode = "forced_unsupported"; |
Martin K. Petersen | f303074 | 2015-05-04 21:54:19 -0400 | [diff] [blame] | 573 | else if (ata_dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM) |
| 574 | mode = "forced_unqueued"; |
| 575 | else if (ata_fpdma_dsm_supported(ata_dev)) |
| 576 | mode = "queued"; |
| 577 | else |
| 578 | mode = "unqueued"; |
| 579 | |
| 580 | return snprintf(buf, 20, "%s\n", mode); |
| 581 | } |
| 582 | |
| 583 | static DEVICE_ATTR(trim, S_IRUGO, show_ata_dev_trim, NULL); |
| 584 | |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 585 | static DECLARE_TRANSPORT_CLASS(ata_dev_class, |
| 586 | "ata_device", NULL, NULL, NULL); |
| 587 | |
| 588 | static void ata_tdev_release(struct device *dev) |
| 589 | { |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | /** |
| 593 | * ata_is_ata_dev -- check if a struct device represents a ATA device |
| 594 | * @dev: device to check |
| 595 | * |
| 596 | * Returns: |
| 597 | * %1 if the device represents a ATA device, %0 else |
| 598 | */ |
H Hartley Sweeten | b0e73af | 2012-04-16 18:04:41 -0700 | [diff] [blame] | 599 | static int ata_is_ata_dev(const struct device *dev) |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 600 | { |
| 601 | return dev->release == ata_tdev_release; |
| 602 | } |
| 603 | |
| 604 | static int ata_tdev_match(struct attribute_container *cont, |
| 605 | struct device *dev) |
| 606 | { |
| 607 | struct ata_internal* i = to_ata_internal(ata_scsi_transport_template); |
| 608 | if (!ata_is_ata_dev(dev)) |
| 609 | return 0; |
| 610 | return &i->dev_attr_cont.ac == cont; |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * ata_tdev_free -- free a ATA LINK |
| 615 | * @dev: ATA PHY to free |
| 616 | * |
| 617 | * Frees the specified ATA PHY. |
| 618 | * |
| 619 | * Note: |
| 620 | * This function must only be called on a PHY that has not |
| 621 | * successfully been added using ata_tdev_add(). |
| 622 | */ |
| 623 | static void ata_tdev_free(struct ata_device *dev) |
| 624 | { |
| 625 | transport_destroy_device(&dev->tdev); |
| 626 | put_device(&dev->tdev); |
| 627 | } |
| 628 | |
| 629 | /** |
| 630 | * ata_tdev_delete -- remove ATA device |
| 631 | * @port: ATA PORT to remove |
| 632 | * |
| 633 | * Removes the specified ATA device. |
| 634 | */ |
| 635 | static void ata_tdev_delete(struct ata_device *ata_dev) |
| 636 | { |
| 637 | struct device *dev = &ata_dev->tdev; |
| 638 | |
| 639 | transport_remove_device(dev); |
| 640 | device_del(dev); |
| 641 | ata_tdev_free(ata_dev); |
| 642 | } |
| 643 | |
| 644 | |
| 645 | /** |
| 646 | * ata_tdev_add -- initialize a transport ATA device structure. |
| 647 | * @ata_dev: ata_dev structure. |
| 648 | * |
| 649 | * Initialize an ATA device structure for sysfs. It will be added in the |
| 650 | * device tree below the ATA LINK device it belongs to. |
| 651 | * |
| 652 | * Returns %0 on success |
| 653 | */ |
| 654 | static int ata_tdev_add(struct ata_device *ata_dev) |
| 655 | { |
| 656 | struct device *dev = &ata_dev->tdev; |
| 657 | struct ata_link *link = ata_dev->link; |
| 658 | struct ata_port *ap = link->ap; |
| 659 | int error; |
| 660 | |
| 661 | device_initialize(dev); |
Gwendal Grignou | d85fc67 | 2017-03-03 09:00:09 -0800 | [diff] [blame] | 662 | dev->parent = &link->tdev; |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 663 | dev->release = ata_tdev_release; |
| 664 | if (ata_is_host_link(link)) |
| 665 | dev_set_name(dev, "dev%d.%d", ap->print_id,ata_dev->devno); |
| 666 | else |
| 667 | dev_set_name(dev, "dev%d.%d.0", ap->print_id, link->pmp); |
| 668 | |
| 669 | transport_setup_device(dev); |
Aaron Lu | f1bc1e4 | 2013-08-23 10:17:54 +0800 | [diff] [blame] | 670 | ata_acpi_bind_dev(ata_dev); |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 671 | error = device_add(dev); |
| 672 | if (error) { |
| 673 | ata_tdev_free(ata_dev); |
| 674 | return error; |
| 675 | } |
| 676 | |
| 677 | transport_add_device(dev); |
| 678 | transport_configure_device(dev); |
| 679 | return 0; |
| 680 | } |
| 681 | |
| 682 | |
| 683 | /* |
| 684 | * Setup / Teardown code |
| 685 | */ |
| 686 | |
| 687 | #define SETUP_TEMPLATE(attrb, field, perm, test) \ |
| 688 | i->private_##attrb[count] = dev_attr_##field; \ |
| 689 | i->private_##attrb[count].attr.mode = perm; \ |
| 690 | i->attrb[count] = &i->private_##attrb[count]; \ |
| 691 | if (test) \ |
| 692 | count++ |
| 693 | |
| 694 | #define SETUP_LINK_ATTRIBUTE(field) \ |
| 695 | SETUP_TEMPLATE(link_attrs, field, S_IRUGO, 1) |
| 696 | |
| 697 | #define SETUP_PORT_ATTRIBUTE(field) \ |
| 698 | SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1) |
| 699 | |
| 700 | #define SETUP_DEV_ATTRIBUTE(field) \ |
| 701 | SETUP_TEMPLATE(dev_attrs, field, S_IRUGO, 1) |
| 702 | |
| 703 | /** |
| 704 | * ata_attach_transport -- instantiate ATA transport template |
| 705 | */ |
| 706 | struct scsi_transport_template *ata_attach_transport(void) |
| 707 | { |
| 708 | struct ata_internal *i; |
| 709 | int count; |
| 710 | |
| 711 | i = kzalloc(sizeof(struct ata_internal), GFP_KERNEL); |
| 712 | if (!i) |
| 713 | return NULL; |
| 714 | |
| 715 | i->t.eh_strategy_handler = ata_scsi_error; |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 716 | i->t.user_scan = ata_scsi_user_scan; |
| 717 | |
| 718 | i->t.host_attrs.ac.attrs = &i->port_attrs[0]; |
| 719 | i->t.host_attrs.ac.class = &ata_port_class.class; |
| 720 | i->t.host_attrs.ac.match = ata_tport_match; |
| 721 | transport_container_register(&i->t.host_attrs); |
| 722 | |
| 723 | i->link_attr_cont.ac.class = &ata_link_class.class; |
| 724 | i->link_attr_cont.ac.attrs = &i->link_attrs[0]; |
| 725 | i->link_attr_cont.ac.match = ata_tlink_match; |
| 726 | transport_container_register(&i->link_attr_cont); |
| 727 | |
| 728 | i->dev_attr_cont.ac.class = &ata_dev_class.class; |
| 729 | i->dev_attr_cont.ac.attrs = &i->dev_attrs[0]; |
| 730 | i->dev_attr_cont.ac.match = ata_tdev_match; |
| 731 | transport_container_register(&i->dev_attr_cont); |
| 732 | |
| 733 | count = 0; |
| 734 | SETUP_PORT_ATTRIBUTE(nr_pmp_links); |
| 735 | SETUP_PORT_ATTRIBUTE(idle_irq); |
David Milburn | e628dc9 | 2013-05-14 13:48:40 -0500 | [diff] [blame] | 736 | SETUP_PORT_ATTRIBUTE(port_no); |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 737 | BUG_ON(count > ATA_PORT_ATTRS); |
| 738 | i->port_attrs[count] = NULL; |
| 739 | |
| 740 | count = 0; |
| 741 | SETUP_LINK_ATTRIBUTE(hw_sata_spd_limit); |
| 742 | SETUP_LINK_ATTRIBUTE(sata_spd_limit); |
| 743 | SETUP_LINK_ATTRIBUTE(sata_spd); |
| 744 | BUG_ON(count > ATA_LINK_ATTRS); |
| 745 | i->link_attrs[count] = NULL; |
| 746 | |
| 747 | count = 0; |
| 748 | SETUP_DEV_ATTRIBUTE(class); |
| 749 | SETUP_DEV_ATTRIBUTE(pio_mode); |
| 750 | SETUP_DEV_ATTRIBUTE(dma_mode); |
| 751 | SETUP_DEV_ATTRIBUTE(xfer_mode); |
| 752 | SETUP_DEV_ATTRIBUTE(spdn_cnt); |
| 753 | SETUP_DEV_ATTRIBUTE(ering); |
| 754 | SETUP_DEV_ATTRIBUTE(id); |
| 755 | SETUP_DEV_ATTRIBUTE(gscr); |
Martin K. Petersen | f303074 | 2015-05-04 21:54:19 -0400 | [diff] [blame] | 756 | SETUP_DEV_ATTRIBUTE(trim); |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 757 | BUG_ON(count > ATA_DEV_ATTRS); |
| 758 | i->dev_attrs[count] = NULL; |
| 759 | |
| 760 | return &i->t; |
| 761 | } |
| 762 | |
| 763 | /** |
| 764 | * ata_release_transport -- release ATA transport template instance |
| 765 | * @t: transport template instance |
| 766 | */ |
| 767 | void ata_release_transport(struct scsi_transport_template *t) |
| 768 | { |
| 769 | struct ata_internal *i = to_ata_internal(t); |
| 770 | |
| 771 | transport_container_unregister(&i->t.host_attrs); |
| 772 | transport_container_unregister(&i->link_attr_cont); |
| 773 | transport_container_unregister(&i->dev_attr_cont); |
| 774 | |
| 775 | kfree(i); |
| 776 | } |
| 777 | |
| 778 | __init int libata_transport_init(void) |
| 779 | { |
| 780 | int error; |
| 781 | |
| 782 | error = transport_class_register(&ata_link_class); |
| 783 | if (error) |
| 784 | goto out_unregister_transport; |
| 785 | error = transport_class_register(&ata_port_class); |
| 786 | if (error) |
| 787 | goto out_unregister_link; |
| 788 | error = transport_class_register(&ata_dev_class); |
| 789 | if (error) |
| 790 | goto out_unregister_port; |
| 791 | return 0; |
| 792 | |
| 793 | out_unregister_port: |
| 794 | transport_class_unregister(&ata_port_class); |
| 795 | out_unregister_link: |
| 796 | transport_class_unregister(&ata_link_class); |
| 797 | out_unregister_transport: |
| 798 | return error; |
| 799 | |
| 800 | } |
| 801 | |
| 802 | void __exit libata_transport_exit(void) |
| 803 | { |
| 804 | transport_class_unregister(&ata_link_class); |
| 805 | transport_class_unregister(&ata_port_class); |
| 806 | transport_class_unregister(&ata_dev_class); |
| 807 | } |