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