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 | |
| 40 | #define ATA_PORT_ATTRS 2 |
| 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" }, |
| 146 | { ATA_DEV_NONE, "none" } |
| 147 | }; |
| 148 | ata_bitfield_name_search(class, ata_class_names) |
| 149 | |
| 150 | |
| 151 | static struct { |
| 152 | u32 value; |
| 153 | char *name; |
| 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" } |
| 166 | }; |
| 167 | ata_bitfield_name_match(err, ata_err_names) |
| 168 | |
| 169 | static struct { |
| 170 | u32 value; |
| 171 | char *name; |
| 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" } |
| 197 | }; |
| 198 | ata_bitfield_name_match(xfer,ata_xfer_names) |
| 199 | |
| 200 | /* |
| 201 | * ATA Port attributes |
| 202 | */ |
| 203 | #define ata_port_show_simple(field, name, format_string, cast) \ |
| 204 | static ssize_t \ |
| 205 | show_ata_port_##name(struct device *dev, \ |
| 206 | struct device_attribute *attr, char *buf) \ |
| 207 | { \ |
| 208 | struct ata_port *ap = transport_class_to_port(dev); \ |
| 209 | \ |
| 210 | return snprintf(buf, 20, format_string, cast ap->field); \ |
| 211 | } |
| 212 | |
| 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) |
| 216 | |
| 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); |
| 219 | |
| 220 | static DECLARE_TRANSPORT_CLASS(ata_port_class, |
| 221 | "ata_port", NULL, NULL, NULL); |
| 222 | |
| 223 | static void ata_tport_release(struct device *dev) |
| 224 | { |
| 225 | put_device(dev->parent); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * ata_is_port -- check if a struct device represents a ATA port |
| 230 | * @dev: device to check |
| 231 | * |
| 232 | * Returns: |
| 233 | * %1 if the device represents a ATA Port, %0 else |
| 234 | */ |
| 235 | int ata_is_port(const struct device *dev) |
| 236 | { |
| 237 | return dev->release == ata_tport_release; |
| 238 | } |
| 239 | |
| 240 | static int ata_tport_match(struct attribute_container *cont, |
| 241 | struct device *dev) |
| 242 | { |
| 243 | if (!ata_is_port(dev)) |
| 244 | return 0; |
| 245 | return &ata_scsi_transport_template->host_attrs.ac == cont; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * ata_tport_delete -- remove ATA PORT |
| 250 | * @port: ATA PORT to remove |
| 251 | * |
| 252 | * Removes the specified ATA PORT. Remove the associated link as well. |
| 253 | */ |
| 254 | void ata_tport_delete(struct ata_port *ap) |
| 255 | { |
| 256 | struct device *dev = &ap->tdev; |
| 257 | |
| 258 | ata_tlink_delete(&ap->link); |
| 259 | |
| 260 | transport_remove_device(dev); |
| 261 | device_del(dev); |
| 262 | transport_destroy_device(dev); |
| 263 | put_device(dev); |
| 264 | } |
| 265 | |
| 266 | /** ata_tport_add - initialize a transport ATA port structure |
| 267 | * |
| 268 | * @parent: parent device |
| 269 | * @ap: existing ata_port structure |
| 270 | * |
| 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. |
| 273 | * |
| 274 | * Returns %0 on success |
| 275 | */ |
| 276 | int ata_tport_add(struct device *parent, |
| 277 | struct ata_port *ap) |
| 278 | { |
| 279 | int error; |
| 280 | struct device *dev = &ap->tdev; |
| 281 | |
| 282 | device_initialize(dev); |
Lin Ming | 5ef4108 | 2011-12-05 09:20:27 +0800 | [diff] [blame] | 283 | dev->type = &ata_port_type; |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 284 | |
| 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); |
| 290 | if (error) { |
| 291 | goto tport_err; |
| 292 | } |
| 293 | |
Lin Ming | 966f121 | 2012-01-16 13:23:23 +0800 | [diff] [blame] | 294 | device_enable_async_suspend(dev); |
Lin Ming | 9ee4f39 | 2011-12-05 09:20:28 +0800 | [diff] [blame] | 295 | pm_runtime_set_active(dev); |
| 296 | pm_runtime_enable(dev); |
| 297 | |
Gwendal Grignou | d902747 | 2010-05-25 12:31:38 -0700 | [diff] [blame] | 298 | transport_add_device(dev); |
| 299 | transport_configure_device(dev); |
| 300 | |
| 301 | error = ata_tlink_add(&ap->link); |
| 302 | if (error) { |
| 303 | goto tport_link_err; |
| 304 | } |
| 305 | return 0; |
| 306 | |
| 307 | tport_link_err: |
| 308 | transport_remove_device(dev); |
| 309 | device_del(dev); |
| 310 | |
| 311 | tport_err: |
| 312 | transport_destroy_device(dev); |
| 313 | put_device(dev); |
| 314 | return error; |
| 315 | } |
| 316 | |
| 317 | |
| 318 | /* |
| 319 | * ATA link attributes |
| 320 | */ |
| 321 | |
| 322 | |
| 323 | #define ata_link_show_linkspeed(field) \ |
| 324 | static ssize_t \ |
| 325 | show_ata_link_##field(struct device *dev, \ |
| 326 | struct device_attribute *attr, char *buf) \ |
| 327 | { \ |
| 328 | struct ata_link *link = transport_class_to_link(dev); \ |
| 329 | \ |
| 330 | return sprintf(buf,"%s\n", sata_spd_string(fls(link->field))); \ |
| 331 | } |
| 332 | |
| 333 | #define ata_link_linkspeed_attr(field) \ |
| 334 | ata_link_show_linkspeed(field) \ |
| 335 | static DEVICE_ATTR(field, S_IRUGO, show_ata_link_##field, NULL) |
| 336 | |
| 337 | ata_link_linkspeed_attr(hw_sata_spd_limit); |
| 338 | ata_link_linkspeed_attr(sata_spd_limit); |
| 339 | ata_link_linkspeed_attr(sata_spd); |
| 340 | |
| 341 | |
| 342 | static DECLARE_TRANSPORT_CLASS(ata_link_class, |
| 343 | "ata_link", NULL, NULL, NULL); |
| 344 | |
| 345 | static void ata_tlink_release(struct device *dev) |
| 346 | { |
| 347 | put_device(dev->parent); |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * ata_is_link -- check if a struct device represents a ATA link |
| 352 | * @dev: device to check |
| 353 | * |
| 354 | * Returns: |
| 355 | * %1 if the device represents a ATA link, %0 else |
| 356 | */ |
| 357 | int ata_is_link(const struct device *dev) |
| 358 | { |
| 359 | return dev->release == ata_tlink_release; |
| 360 | } |
| 361 | |
| 362 | static int ata_tlink_match(struct attribute_container *cont, |
| 363 | struct device *dev) |
| 364 | { |
| 365 | struct ata_internal* i = to_ata_internal(ata_scsi_transport_template); |
| 366 | if (!ata_is_link(dev)) |
| 367 | return 0; |
| 368 | return &i->link_attr_cont.ac == cont; |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * ata_tlink_delete -- remove ATA LINK |
| 373 | * @port: ATA LINK to remove |
| 374 | * |
| 375 | * Removes the specified ATA LINK. remove associated ATA device(s) as well. |
| 376 | */ |
| 377 | void ata_tlink_delete(struct ata_link *link) |
| 378 | { |
| 379 | struct device *dev = &link->tdev; |
| 380 | struct ata_device *ata_dev; |
| 381 | |
| 382 | ata_for_each_dev(ata_dev, link, ALL) { |
| 383 | ata_tdev_delete(ata_dev); |
| 384 | } |
| 385 | |
| 386 | transport_remove_device(dev); |
| 387 | device_del(dev); |
| 388 | transport_destroy_device(dev); |
| 389 | put_device(dev); |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * ata_tlink_add -- initialize a transport ATA link structure |
| 394 | * @link: allocated ata_link structure. |
| 395 | * |
| 396 | * Initialize an ATA LINK structure for sysfs. It will be added in the |
| 397 | * device tree below the ATA PORT it belongs to. |
| 398 | * |
| 399 | * Returns %0 on success |
| 400 | */ |
| 401 | int ata_tlink_add(struct ata_link *link) |
| 402 | { |
| 403 | struct device *dev = &link->tdev; |
| 404 | struct ata_port *ap = link->ap; |
| 405 | struct ata_device *ata_dev; |
| 406 | int error; |
| 407 | |
| 408 | device_initialize(dev); |
| 409 | dev->parent = get_device(&ap->tdev); |
| 410 | dev->release = ata_tlink_release; |
| 411 | if (ata_is_host_link(link)) |
| 412 | dev_set_name(dev, "link%d", ap->print_id); |
| 413 | else |
| 414 | dev_set_name(dev, "link%d.%d", ap->print_id, link->pmp); |
| 415 | |
| 416 | transport_setup_device(dev); |
| 417 | |
| 418 | error = device_add(dev); |
| 419 | if (error) { |
| 420 | goto tlink_err; |
| 421 | } |
| 422 | |
| 423 | transport_add_device(dev); |
| 424 | transport_configure_device(dev); |
| 425 | |
| 426 | ata_for_each_dev(ata_dev, link, ALL) { |
| 427 | error = ata_tdev_add(ata_dev); |
| 428 | if (error) { |
| 429 | goto tlink_dev_err; |
| 430 | } |
| 431 | } |
| 432 | return 0; |
| 433 | tlink_dev_err: |
| 434 | while (--ata_dev >= link->device) { |
| 435 | ata_tdev_delete(ata_dev); |
| 436 | } |
| 437 | transport_remove_device(dev); |
| 438 | device_del(dev); |
| 439 | tlink_err: |
| 440 | transport_destroy_device(dev); |
| 441 | put_device(dev); |
| 442 | return error; |
| 443 | } |
| 444 | |
| 445 | /* |
| 446 | * ATA device attributes |
| 447 | */ |
| 448 | |
| 449 | #define ata_dev_show_class(title, field) \ |
| 450 | static ssize_t \ |
| 451 | show_ata_dev_##field(struct device *dev, \ |
| 452 | struct device_attribute *attr, char *buf) \ |
| 453 | { \ |
| 454 | struct ata_device *ata_dev = transport_class_to_dev(dev); \ |
| 455 | \ |
| 456 | return get_ata_##title##_names(ata_dev->field, buf); \ |
| 457 | } |
| 458 | |
| 459 | #define ata_dev_attr(title, field) \ |
| 460 | ata_dev_show_class(title, field) \ |
| 461 | static DEVICE_ATTR(field, S_IRUGO, show_ata_dev_##field, NULL) |
| 462 | |
| 463 | ata_dev_attr(class, class); |
| 464 | ata_dev_attr(xfer, pio_mode); |
| 465 | ata_dev_attr(xfer, dma_mode); |
| 466 | ata_dev_attr(xfer, xfer_mode); |
| 467 | |
| 468 | |
| 469 | #define ata_dev_show_simple(field, format_string, cast) \ |
| 470 | static ssize_t \ |
| 471 | show_ata_dev_##field(struct device *dev, \ |
| 472 | struct device_attribute *attr, char *buf) \ |
| 473 | { \ |
| 474 | struct ata_device *ata_dev = transport_class_to_dev(dev); \ |
| 475 | \ |
| 476 | return snprintf(buf, 20, format_string, cast ata_dev->field); \ |
| 477 | } |
| 478 | |
| 479 | #define ata_dev_simple_attr(field, format_string, type) \ |
| 480 | ata_dev_show_simple(field, format_string, (type)) \ |
| 481 | static DEVICE_ATTR(field, S_IRUGO, \ |
| 482 | show_ata_dev_##field, NULL) |
| 483 | |
| 484 | ata_dev_simple_attr(spdn_cnt, "%d\n", int); |
| 485 | |
| 486 | struct ata_show_ering_arg { |
| 487 | char* buf; |
| 488 | int written; |
| 489 | }; |
| 490 | |
| 491 | static int ata_show_ering(struct ata_ering_entry *ent, void *void_arg) |
| 492 | { |
| 493 | struct ata_show_ering_arg* arg = void_arg; |
| 494 | struct timespec time; |
| 495 | |
| 496 | jiffies_to_timespec(ent->timestamp,&time); |
| 497 | arg->written += sprintf(arg->buf + arg->written, |
| 498 | "[%5lu.%06lu]", |
| 499 | time.tv_sec, time.tv_nsec); |
| 500 | arg->written += get_ata_err_names(ent->err_mask, |
| 501 | arg->buf + arg->written); |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | static ssize_t |
| 506 | show_ata_dev_ering(struct device *dev, |
| 507 | struct device_attribute *attr, char *buf) |
| 508 | { |
| 509 | struct ata_device *ata_dev = transport_class_to_dev(dev); |
| 510 | struct ata_show_ering_arg arg = { buf, 0 }; |
| 511 | |
| 512 | ata_ering_map(&ata_dev->ering, ata_show_ering, &arg); |
| 513 | return arg.written; |
| 514 | } |
| 515 | |
| 516 | |
| 517 | static DEVICE_ATTR(ering, S_IRUGO, show_ata_dev_ering, NULL); |
| 518 | |
| 519 | static ssize_t |
| 520 | show_ata_dev_id(struct device *dev, |
| 521 | struct device_attribute *attr, char *buf) |
| 522 | { |
| 523 | struct ata_device *ata_dev = transport_class_to_dev(dev); |
| 524 | int written = 0, i = 0; |
| 525 | |
| 526 | if (ata_dev->class == ATA_DEV_PMP) |
| 527 | return 0; |
| 528 | for(i=0;i<ATA_ID_WORDS;i++) { |
| 529 | written += snprintf(buf+written, 20, "%04x%c", |
| 530 | ata_dev->id[i], |
| 531 | ((i+1) & 7) ? ' ' : '\n'); |
| 532 | } |
| 533 | return written; |
| 534 | } |
| 535 | |
| 536 | static DEVICE_ATTR(id, S_IRUGO, show_ata_dev_id, NULL); |
| 537 | |
| 538 | static ssize_t |
| 539 | show_ata_dev_gscr(struct device *dev, |
| 540 | struct device_attribute *attr, char *buf) |
| 541 | { |
| 542 | struct ata_device *ata_dev = transport_class_to_dev(dev); |
| 543 | int written = 0, i = 0; |
| 544 | |
| 545 | if (ata_dev->class != ATA_DEV_PMP) |
| 546 | return 0; |
| 547 | for(i=0;i<SATA_PMP_GSCR_DWORDS;i++) { |
| 548 | written += snprintf(buf+written, 20, "%08x%c", |
| 549 | ata_dev->gscr[i], |
| 550 | ((i+1) & 3) ? ' ' : '\n'); |
| 551 | } |
| 552 | if (SATA_PMP_GSCR_DWORDS & 3) |
| 553 | buf[written-1] = '\n'; |
| 554 | return written; |
| 555 | } |
| 556 | |
| 557 | static DEVICE_ATTR(gscr, S_IRUGO, show_ata_dev_gscr, NULL); |
| 558 | |
| 559 | static DECLARE_TRANSPORT_CLASS(ata_dev_class, |
| 560 | "ata_device", NULL, NULL, NULL); |
| 561 | |
| 562 | static void ata_tdev_release(struct device *dev) |
| 563 | { |
| 564 | put_device(dev->parent); |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * ata_is_ata_dev -- check if a struct device represents a ATA device |
| 569 | * @dev: device to check |
| 570 | * |
| 571 | * Returns: |
| 572 | * %1 if the device represents a ATA device, %0 else |
| 573 | */ |
| 574 | int ata_is_ata_dev(const struct device *dev) |
| 575 | { |
| 576 | return dev->release == ata_tdev_release; |
| 577 | } |
| 578 | |
| 579 | static int ata_tdev_match(struct attribute_container *cont, |
| 580 | struct device *dev) |
| 581 | { |
| 582 | struct ata_internal* i = to_ata_internal(ata_scsi_transport_template); |
| 583 | if (!ata_is_ata_dev(dev)) |
| 584 | return 0; |
| 585 | return &i->dev_attr_cont.ac == cont; |
| 586 | } |
| 587 | |
| 588 | /** |
| 589 | * ata_tdev_free -- free a ATA LINK |
| 590 | * @dev: ATA PHY to free |
| 591 | * |
| 592 | * Frees the specified ATA PHY. |
| 593 | * |
| 594 | * Note: |
| 595 | * This function must only be called on a PHY that has not |
| 596 | * successfully been added using ata_tdev_add(). |
| 597 | */ |
| 598 | static void ata_tdev_free(struct ata_device *dev) |
| 599 | { |
| 600 | transport_destroy_device(&dev->tdev); |
| 601 | put_device(&dev->tdev); |
| 602 | } |
| 603 | |
| 604 | /** |
| 605 | * ata_tdev_delete -- remove ATA device |
| 606 | * @port: ATA PORT to remove |
| 607 | * |
| 608 | * Removes the specified ATA device. |
| 609 | */ |
| 610 | static void ata_tdev_delete(struct ata_device *ata_dev) |
| 611 | { |
| 612 | struct device *dev = &ata_dev->tdev; |
| 613 | |
| 614 | transport_remove_device(dev); |
| 615 | device_del(dev); |
| 616 | ata_tdev_free(ata_dev); |
| 617 | } |
| 618 | |
| 619 | |
| 620 | /** |
| 621 | * ata_tdev_add -- initialize a transport ATA device structure. |
| 622 | * @ata_dev: ata_dev structure. |
| 623 | * |
| 624 | * Initialize an ATA device structure for sysfs. It will be added in the |
| 625 | * device tree below the ATA LINK device it belongs to. |
| 626 | * |
| 627 | * Returns %0 on success |
| 628 | */ |
| 629 | static int ata_tdev_add(struct ata_device *ata_dev) |
| 630 | { |
| 631 | struct device *dev = &ata_dev->tdev; |
| 632 | struct ata_link *link = ata_dev->link; |
| 633 | struct ata_port *ap = link->ap; |
| 634 | int error; |
| 635 | |
| 636 | device_initialize(dev); |
| 637 | dev->parent = get_device(&link->tdev); |
| 638 | dev->release = ata_tdev_release; |
| 639 | if (ata_is_host_link(link)) |
| 640 | dev_set_name(dev, "dev%d.%d", ap->print_id,ata_dev->devno); |
| 641 | else |
| 642 | dev_set_name(dev, "dev%d.%d.0", ap->print_id, link->pmp); |
| 643 | |
| 644 | transport_setup_device(dev); |
| 645 | error = device_add(dev); |
| 646 | if (error) { |
| 647 | ata_tdev_free(ata_dev); |
| 648 | return error; |
| 649 | } |
| 650 | |
| 651 | transport_add_device(dev); |
| 652 | transport_configure_device(dev); |
| 653 | return 0; |
| 654 | } |
| 655 | |
| 656 | |
| 657 | /* |
| 658 | * Setup / Teardown code |
| 659 | */ |
| 660 | |
| 661 | #define SETUP_TEMPLATE(attrb, field, perm, test) \ |
| 662 | i->private_##attrb[count] = dev_attr_##field; \ |
| 663 | i->private_##attrb[count].attr.mode = perm; \ |
| 664 | i->attrb[count] = &i->private_##attrb[count]; \ |
| 665 | if (test) \ |
| 666 | count++ |
| 667 | |
| 668 | #define SETUP_LINK_ATTRIBUTE(field) \ |
| 669 | SETUP_TEMPLATE(link_attrs, field, S_IRUGO, 1) |
| 670 | |
| 671 | #define SETUP_PORT_ATTRIBUTE(field) \ |
| 672 | SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1) |
| 673 | |
| 674 | #define SETUP_DEV_ATTRIBUTE(field) \ |
| 675 | SETUP_TEMPLATE(dev_attrs, field, S_IRUGO, 1) |
| 676 | |
| 677 | /** |
| 678 | * ata_attach_transport -- instantiate ATA transport template |
| 679 | */ |
| 680 | struct scsi_transport_template *ata_attach_transport(void) |
| 681 | { |
| 682 | struct ata_internal *i; |
| 683 | int count; |
| 684 | |
| 685 | i = kzalloc(sizeof(struct ata_internal), GFP_KERNEL); |
| 686 | if (!i) |
| 687 | return NULL; |
| 688 | |
| 689 | i->t.eh_strategy_handler = ata_scsi_error; |
| 690 | i->t.eh_timed_out = ata_scsi_timed_out; |
| 691 | i->t.user_scan = ata_scsi_user_scan; |
| 692 | |
| 693 | i->t.host_attrs.ac.attrs = &i->port_attrs[0]; |
| 694 | i->t.host_attrs.ac.class = &ata_port_class.class; |
| 695 | i->t.host_attrs.ac.match = ata_tport_match; |
| 696 | transport_container_register(&i->t.host_attrs); |
| 697 | |
| 698 | i->link_attr_cont.ac.class = &ata_link_class.class; |
| 699 | i->link_attr_cont.ac.attrs = &i->link_attrs[0]; |
| 700 | i->link_attr_cont.ac.match = ata_tlink_match; |
| 701 | transport_container_register(&i->link_attr_cont); |
| 702 | |
| 703 | i->dev_attr_cont.ac.class = &ata_dev_class.class; |
| 704 | i->dev_attr_cont.ac.attrs = &i->dev_attrs[0]; |
| 705 | i->dev_attr_cont.ac.match = ata_tdev_match; |
| 706 | transport_container_register(&i->dev_attr_cont); |
| 707 | |
| 708 | count = 0; |
| 709 | SETUP_PORT_ATTRIBUTE(nr_pmp_links); |
| 710 | SETUP_PORT_ATTRIBUTE(idle_irq); |
| 711 | BUG_ON(count > ATA_PORT_ATTRS); |
| 712 | i->port_attrs[count] = NULL; |
| 713 | |
| 714 | count = 0; |
| 715 | SETUP_LINK_ATTRIBUTE(hw_sata_spd_limit); |
| 716 | SETUP_LINK_ATTRIBUTE(sata_spd_limit); |
| 717 | SETUP_LINK_ATTRIBUTE(sata_spd); |
| 718 | BUG_ON(count > ATA_LINK_ATTRS); |
| 719 | i->link_attrs[count] = NULL; |
| 720 | |
| 721 | count = 0; |
| 722 | SETUP_DEV_ATTRIBUTE(class); |
| 723 | SETUP_DEV_ATTRIBUTE(pio_mode); |
| 724 | SETUP_DEV_ATTRIBUTE(dma_mode); |
| 725 | SETUP_DEV_ATTRIBUTE(xfer_mode); |
| 726 | SETUP_DEV_ATTRIBUTE(spdn_cnt); |
| 727 | SETUP_DEV_ATTRIBUTE(ering); |
| 728 | SETUP_DEV_ATTRIBUTE(id); |
| 729 | SETUP_DEV_ATTRIBUTE(gscr); |
| 730 | BUG_ON(count > ATA_DEV_ATTRS); |
| 731 | i->dev_attrs[count] = NULL; |
| 732 | |
| 733 | return &i->t; |
| 734 | } |
| 735 | |
| 736 | /** |
| 737 | * ata_release_transport -- release ATA transport template instance |
| 738 | * @t: transport template instance |
| 739 | */ |
| 740 | void ata_release_transport(struct scsi_transport_template *t) |
| 741 | { |
| 742 | struct ata_internal *i = to_ata_internal(t); |
| 743 | |
| 744 | transport_container_unregister(&i->t.host_attrs); |
| 745 | transport_container_unregister(&i->link_attr_cont); |
| 746 | transport_container_unregister(&i->dev_attr_cont); |
| 747 | |
| 748 | kfree(i); |
| 749 | } |
| 750 | |
| 751 | __init int libata_transport_init(void) |
| 752 | { |
| 753 | int error; |
| 754 | |
| 755 | error = transport_class_register(&ata_link_class); |
| 756 | if (error) |
| 757 | goto out_unregister_transport; |
| 758 | error = transport_class_register(&ata_port_class); |
| 759 | if (error) |
| 760 | goto out_unregister_link; |
| 761 | error = transport_class_register(&ata_dev_class); |
| 762 | if (error) |
| 763 | goto out_unregister_port; |
| 764 | return 0; |
| 765 | |
| 766 | out_unregister_port: |
| 767 | transport_class_unregister(&ata_port_class); |
| 768 | out_unregister_link: |
| 769 | transport_class_unregister(&ata_link_class); |
| 770 | out_unregister_transport: |
| 771 | return error; |
| 772 | |
| 773 | } |
| 774 | |
| 775 | void __exit libata_transport_exit(void) |
| 776 | { |
| 777 | transport_class_unregister(&ata_link_class); |
| 778 | transport_class_unregister(&ata_port_class); |
| 779 | transport_class_unregister(&ata_dev_class); |
| 780 | } |