1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright 2008 ioogle, Inc. All rights reserved. 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 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 <linux/slab.h> 31 #include <scsi/scsi_transport.h> 32 #include <linux/libata.h> 33 #include <linux/hdreg.h> 34 #include <linux/uaccess.h> 35 #include <linux/pm_runtime.h> 36 37 #include "libata.h" 38 #include "libata-transport.h" 39 40 #define ATA_PORT_ATTRS 3 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_ZAC, "zac" }, 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_search(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 scnprintf(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); 220 /* We want the port_no sysfs attibute to start at 1 (ap->port_no starts at 0) */ 221 ata_port_simple_attr(port_no + 1, port_no, "%u\n", unsigned int); 222 223 static DECLARE_TRANSPORT_CLASS(ata_port_class, 224 "ata_port", NULL, NULL, NULL); 225 226 static void ata_tport_release(struct device *dev) 227 { 228 struct ata_port *ap = tdev_to_port(dev); 229 ata_host_put(ap->host); 230 } 231 232 /** 233 * ata_is_port -- check if a struct device represents a ATA port 234 * @dev: device to check 235 * 236 * Returns: 237 * %1 if the device represents a ATA Port, %0 else 238 */ 239 static int ata_is_port(const struct device *dev) 240 { 241 return dev->release == ata_tport_release; 242 } 243 244 static int ata_tport_match(struct attribute_container *cont, 245 struct device *dev) 246 { 247 if (!ata_is_port(dev)) 248 return 0; 249 return &ata_scsi_transport_template->host_attrs.ac == cont; 250 } 251 252 /** 253 * ata_tport_delete -- remove ATA PORT 254 * @ap: ATA PORT to remove 255 * 256 * Removes the specified ATA PORT. Remove the associated link as well. 257 */ 258 void ata_tport_delete(struct ata_port *ap) 259 { 260 struct device *dev = &ap->tdev; 261 262 ata_tlink_delete(&ap->link); 263 264 transport_remove_device(dev); 265 device_del(dev); 266 transport_destroy_device(dev); 267 put_device(dev); 268 } 269 EXPORT_SYMBOL_GPL(ata_tport_delete); 270 271 static const struct device_type ata_port_sas_type = { 272 .name = ATA_PORT_TYPE_NAME, 273 }; 274 275 /** ata_tport_add - initialize a transport ATA port structure 276 * 277 * @parent: parent device 278 * @ap: existing ata_port structure 279 * 280 * Initialize a ATA port structure for sysfs. It will be added to the device 281 * tree below the device specified by @parent which could be a PCI device. 282 * 283 * Returns %0 on success 284 */ 285 int ata_tport_add(struct device *parent, 286 struct ata_port *ap) 287 { 288 int error; 289 struct device *dev = &ap->tdev; 290 291 device_initialize(dev); 292 if (ap->flags & ATA_FLAG_SAS_HOST) 293 dev->type = &ata_port_sas_type; 294 else 295 dev->type = &ata_port_type; 296 297 dev->parent = parent; 298 ata_host_get(ap->host); 299 dev->release = ata_tport_release; 300 dev_set_name(dev, "ata%d", ap->print_id); 301 transport_setup_device(dev); 302 ata_acpi_bind_port(ap); 303 error = device_add(dev); 304 if (error) { 305 goto tport_err; 306 } 307 308 device_enable_async_suspend(dev); 309 pm_runtime_set_active(dev); 310 pm_runtime_enable(dev); 311 pm_runtime_forbid(dev); 312 313 error = transport_add_device(dev); 314 if (error) 315 goto tport_transport_add_err; 316 transport_configure_device(dev); 317 318 error = ata_tlink_add(&ap->link); 319 if (error) { 320 goto tport_link_err; 321 } 322 return 0; 323 324 tport_link_err: 325 transport_remove_device(dev); 326 tport_transport_add_err: 327 device_del(dev); 328 329 tport_err: 330 transport_destroy_device(dev); 331 put_device(dev); 332 return error; 333 } 334 EXPORT_SYMBOL_GPL(ata_tport_add); 335 336 /** 337 * ata_port_classify - determine device type based on ATA-spec signature 338 * @ap: ATA port device on which the classification should be run 339 * @tf: ATA taskfile register set for device to be identified 340 * 341 * A wrapper around ata_dev_classify() to provide additional logging 342 * 343 * RETURNS: 344 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP, 345 * %ATA_DEV_ZAC, or %ATA_DEV_UNKNOWN the event of failure. 346 */ 347 unsigned int ata_port_classify(struct ata_port *ap, 348 const struct ata_taskfile *tf) 349 { 350 int i; 351 unsigned int class = ata_dev_classify(tf); 352 353 /* Start with index '1' to skip the 'unknown' entry */ 354 for (i = 1; i < ARRAY_SIZE(ata_class_names); i++) { 355 if (ata_class_names[i].value == class) { 356 ata_port_dbg(ap, "found %s device by sig\n", 357 ata_class_names[i].name); 358 return class; 359 } 360 } 361 362 ata_port_info(ap, "found unknown device (class %u)\n", class); 363 return class; 364 } 365 EXPORT_SYMBOL_GPL(ata_port_classify); 366 367 /* 368 * ATA link attributes 369 */ 370 static int noop(int x) { return x; } 371 372 #define ata_link_show_linkspeed(field, format) \ 373 static ssize_t \ 374 show_ata_link_##field(struct device *dev, \ 375 struct device_attribute *attr, char *buf) \ 376 { \ 377 struct ata_link *link = transport_class_to_link(dev); \ 378 \ 379 return sprintf(buf, "%s\n", sata_spd_string(format(link->field))); \ 380 } 381 382 #define ata_link_linkspeed_attr(field, format) \ 383 ata_link_show_linkspeed(field, format) \ 384 static DEVICE_ATTR(field, S_IRUGO, show_ata_link_##field, NULL) 385 386 ata_link_linkspeed_attr(hw_sata_spd_limit, fls); 387 ata_link_linkspeed_attr(sata_spd_limit, fls); 388 ata_link_linkspeed_attr(sata_spd, noop); 389 390 391 static DECLARE_TRANSPORT_CLASS(ata_link_class, 392 "ata_link", NULL, NULL, NULL); 393 394 static void ata_tlink_release(struct device *dev) 395 { 396 } 397 398 /** 399 * ata_is_link -- check if a struct device represents a ATA link 400 * @dev: device to check 401 * 402 * Returns: 403 * %1 if the device represents a ATA link, %0 else 404 */ 405 static int ata_is_link(const struct device *dev) 406 { 407 return dev->release == ata_tlink_release; 408 } 409 410 static int ata_tlink_match(struct attribute_container *cont, 411 struct device *dev) 412 { 413 struct ata_internal* i = to_ata_internal(ata_scsi_transport_template); 414 if (!ata_is_link(dev)) 415 return 0; 416 return &i->link_attr_cont.ac == cont; 417 } 418 419 /** 420 * ata_tlink_delete -- remove ATA LINK 421 * @link: ATA LINK to remove 422 * 423 * Removes the specified ATA LINK. remove associated ATA device(s) as well. 424 */ 425 void ata_tlink_delete(struct ata_link *link) 426 { 427 struct device *dev = &link->tdev; 428 struct ata_device *ata_dev; 429 430 ata_for_each_dev(ata_dev, link, ALL) { 431 ata_tdev_delete(ata_dev); 432 } 433 434 transport_remove_device(dev); 435 device_del(dev); 436 transport_destroy_device(dev); 437 put_device(dev); 438 } 439 440 /** 441 * ata_tlink_add -- initialize a transport ATA link structure 442 * @link: allocated ata_link structure. 443 * 444 * Initialize an ATA LINK structure for sysfs. It will be added in the 445 * device tree below the ATA PORT it belongs to. 446 * 447 * Returns %0 on success 448 */ 449 int ata_tlink_add(struct ata_link *link) 450 { 451 struct device *dev = &link->tdev; 452 struct ata_port *ap = link->ap; 453 struct ata_device *ata_dev; 454 int error; 455 456 device_initialize(dev); 457 dev->parent = &ap->tdev; 458 dev->release = ata_tlink_release; 459 if (ata_is_host_link(link)) 460 dev_set_name(dev, "link%d", ap->print_id); 461 else 462 dev_set_name(dev, "link%d.%d", ap->print_id, link->pmp); 463 464 transport_setup_device(dev); 465 466 error = device_add(dev); 467 if (error) { 468 goto tlink_err; 469 } 470 471 error = transport_add_device(dev); 472 if (error) 473 goto tlink_transport_err; 474 transport_configure_device(dev); 475 476 ata_for_each_dev(ata_dev, link, ALL) { 477 error = ata_tdev_add(ata_dev); 478 if (error) { 479 goto tlink_dev_err; 480 } 481 } 482 return 0; 483 tlink_dev_err: 484 while (--ata_dev >= link->device) { 485 ata_tdev_delete(ata_dev); 486 } 487 transport_remove_device(dev); 488 tlink_transport_err: 489 device_del(dev); 490 tlink_err: 491 transport_destroy_device(dev); 492 put_device(dev); 493 return error; 494 } 495 496 /* 497 * ATA device attributes 498 */ 499 500 #define ata_dev_show_class(title, field) \ 501 static ssize_t \ 502 show_ata_dev_##field(struct device *dev, \ 503 struct device_attribute *attr, char *buf) \ 504 { \ 505 struct ata_device *ata_dev = transport_class_to_dev(dev); \ 506 \ 507 return get_ata_##title##_names(ata_dev->field, buf); \ 508 } 509 510 #define ata_dev_attr(title, field) \ 511 ata_dev_show_class(title, field) \ 512 static DEVICE_ATTR(field, S_IRUGO, show_ata_dev_##field, NULL) 513 514 ata_dev_attr(class, class); 515 ata_dev_attr(xfer, pio_mode); 516 ata_dev_attr(xfer, dma_mode); 517 ata_dev_attr(xfer, xfer_mode); 518 519 520 #define ata_dev_show_simple(field, format_string, cast) \ 521 static ssize_t \ 522 show_ata_dev_##field(struct device *dev, \ 523 struct device_attribute *attr, char *buf) \ 524 { \ 525 struct ata_device *ata_dev = transport_class_to_dev(dev); \ 526 \ 527 return scnprintf(buf, 20, format_string, cast ata_dev->field); \ 528 } 529 530 #define ata_dev_simple_attr(field, format_string, type) \ 531 ata_dev_show_simple(field, format_string, (type)) \ 532 static DEVICE_ATTR(field, S_IRUGO, \ 533 show_ata_dev_##field, NULL) 534 535 ata_dev_simple_attr(spdn_cnt, "%d\n", int); 536 537 struct ata_show_ering_arg { 538 char* buf; 539 int written; 540 }; 541 542 static int ata_show_ering(struct ata_ering_entry *ent, void *void_arg) 543 { 544 struct ata_show_ering_arg* arg = void_arg; 545 u64 seconds; 546 u32 rem; 547 548 seconds = div_u64_rem(ent->timestamp, HZ, &rem); 549 arg->written += sprintf(arg->buf + arg->written, 550 "[%5llu.%09lu]", seconds, 551 rem * NSEC_PER_SEC / HZ); 552 arg->written += get_ata_err_names(ent->err_mask, 553 arg->buf + arg->written); 554 return 0; 555 } 556 557 static ssize_t 558 show_ata_dev_ering(struct device *dev, 559 struct device_attribute *attr, char *buf) 560 { 561 struct ata_device *ata_dev = transport_class_to_dev(dev); 562 struct ata_show_ering_arg arg = { buf, 0 }; 563 564 ata_ering_map(&ata_dev->ering, ata_show_ering, &arg); 565 return arg.written; 566 } 567 568 569 static DEVICE_ATTR(ering, S_IRUGO, show_ata_dev_ering, NULL); 570 571 static ssize_t 572 show_ata_dev_id(struct device *dev, 573 struct device_attribute *attr, char *buf) 574 { 575 struct ata_device *ata_dev = transport_class_to_dev(dev); 576 int written = 0, i = 0; 577 578 if (ata_dev->class == ATA_DEV_PMP) 579 return 0; 580 for(i=0;i<ATA_ID_WORDS;i++) { 581 written += scnprintf(buf+written, 20, "%04x%c", 582 ata_dev->id[i], 583 ((i+1) & 7) ? ' ' : '\n'); 584 } 585 return written; 586 } 587 588 static DEVICE_ATTR(id, S_IRUGO, show_ata_dev_id, NULL); 589 590 static ssize_t 591 show_ata_dev_gscr(struct device *dev, 592 struct device_attribute *attr, char *buf) 593 { 594 struct ata_device *ata_dev = transport_class_to_dev(dev); 595 int written = 0, i = 0; 596 597 if (ata_dev->class != ATA_DEV_PMP) 598 return 0; 599 for(i=0;i<SATA_PMP_GSCR_DWORDS;i++) { 600 written += scnprintf(buf+written, 20, "%08x%c", 601 ata_dev->gscr[i], 602 ((i+1) & 3) ? ' ' : '\n'); 603 } 604 if (SATA_PMP_GSCR_DWORDS & 3) 605 buf[written-1] = '\n'; 606 return written; 607 } 608 609 static DEVICE_ATTR(gscr, S_IRUGO, show_ata_dev_gscr, NULL); 610 611 static ssize_t 612 show_ata_dev_trim(struct device *dev, 613 struct device_attribute *attr, char *buf) 614 { 615 struct ata_device *ata_dev = transport_class_to_dev(dev); 616 unsigned char *mode; 617 618 if (!ata_id_has_trim(ata_dev->id)) 619 mode = "unsupported"; 620 else if (ata_dev->horkage & ATA_HORKAGE_NOTRIM) 621 mode = "forced_unsupported"; 622 else if (ata_dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM) 623 mode = "forced_unqueued"; 624 else if (ata_fpdma_dsm_supported(ata_dev)) 625 mode = "queued"; 626 else 627 mode = "unqueued"; 628 629 return scnprintf(buf, 20, "%s\n", mode); 630 } 631 632 static DEVICE_ATTR(trim, S_IRUGO, show_ata_dev_trim, NULL); 633 634 static DECLARE_TRANSPORT_CLASS(ata_dev_class, 635 "ata_device", NULL, NULL, NULL); 636 637 static void ata_tdev_release(struct device *dev) 638 { 639 } 640 641 /** 642 * ata_is_ata_dev -- check if a struct device represents a ATA device 643 * @dev: device to check 644 * 645 * Returns: 646 * %1 if the device represents a ATA device, %0 else 647 */ 648 static int ata_is_ata_dev(const struct device *dev) 649 { 650 return dev->release == ata_tdev_release; 651 } 652 653 static int ata_tdev_match(struct attribute_container *cont, 654 struct device *dev) 655 { 656 struct ata_internal* i = to_ata_internal(ata_scsi_transport_template); 657 if (!ata_is_ata_dev(dev)) 658 return 0; 659 return &i->dev_attr_cont.ac == cont; 660 } 661 662 /** 663 * ata_tdev_free -- free a ATA LINK 664 * @dev: ATA PHY to free 665 * 666 * Frees the specified ATA PHY. 667 * 668 * Note: 669 * This function must only be called on a PHY that has not 670 * successfully been added using ata_tdev_add(). 671 */ 672 static void ata_tdev_free(struct ata_device *dev) 673 { 674 transport_destroy_device(&dev->tdev); 675 put_device(&dev->tdev); 676 } 677 678 /** 679 * ata_tdev_delete -- remove ATA device 680 * @ata_dev: ATA device to remove 681 * 682 * Removes the specified ATA device. 683 */ 684 static void ata_tdev_delete(struct ata_device *ata_dev) 685 { 686 struct device *dev = &ata_dev->tdev; 687 688 transport_remove_device(dev); 689 device_del(dev); 690 ata_tdev_free(ata_dev); 691 } 692 693 694 /** 695 * ata_tdev_add -- initialize a transport ATA device structure. 696 * @ata_dev: ata_dev structure. 697 * 698 * Initialize an ATA device structure for sysfs. It will be added in the 699 * device tree below the ATA LINK device it belongs to. 700 * 701 * Returns %0 on success 702 */ 703 static int ata_tdev_add(struct ata_device *ata_dev) 704 { 705 struct device *dev = &ata_dev->tdev; 706 struct ata_link *link = ata_dev->link; 707 struct ata_port *ap = link->ap; 708 int error; 709 710 device_initialize(dev); 711 dev->parent = &link->tdev; 712 dev->release = ata_tdev_release; 713 if (ata_is_host_link(link)) 714 dev_set_name(dev, "dev%d.%d", ap->print_id,ata_dev->devno); 715 else 716 dev_set_name(dev, "dev%d.%d.0", ap->print_id, link->pmp); 717 718 transport_setup_device(dev); 719 ata_acpi_bind_dev(ata_dev); 720 error = device_add(dev); 721 if (error) { 722 ata_tdev_free(ata_dev); 723 return error; 724 } 725 726 error = transport_add_device(dev); 727 if (error) { 728 device_del(dev); 729 ata_tdev_free(ata_dev); 730 return error; 731 } 732 733 transport_configure_device(dev); 734 return 0; 735 } 736 737 738 /* 739 * Setup / Teardown code 740 */ 741 742 #define SETUP_TEMPLATE(attrb, field, perm, test) \ 743 i->private_##attrb[count] = dev_attr_##field; \ 744 i->private_##attrb[count].attr.mode = perm; \ 745 i->attrb[count] = &i->private_##attrb[count]; \ 746 if (test) \ 747 count++ 748 749 #define SETUP_LINK_ATTRIBUTE(field) \ 750 SETUP_TEMPLATE(link_attrs, field, S_IRUGO, 1) 751 752 #define SETUP_PORT_ATTRIBUTE(field) \ 753 SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1) 754 755 #define SETUP_DEV_ATTRIBUTE(field) \ 756 SETUP_TEMPLATE(dev_attrs, field, S_IRUGO, 1) 757 758 /** 759 * ata_attach_transport -- instantiate ATA transport template 760 */ 761 struct scsi_transport_template *ata_attach_transport(void) 762 { 763 struct ata_internal *i; 764 int count; 765 766 i = kzalloc(sizeof(struct ata_internal), GFP_KERNEL); 767 if (!i) 768 return NULL; 769 770 i->t.eh_strategy_handler = ata_scsi_error; 771 i->t.user_scan = ata_scsi_user_scan; 772 773 i->t.host_attrs.ac.attrs = &i->port_attrs[0]; 774 i->t.host_attrs.ac.class = &ata_port_class.class; 775 i->t.host_attrs.ac.match = ata_tport_match; 776 transport_container_register(&i->t.host_attrs); 777 778 i->link_attr_cont.ac.class = &ata_link_class.class; 779 i->link_attr_cont.ac.attrs = &i->link_attrs[0]; 780 i->link_attr_cont.ac.match = ata_tlink_match; 781 transport_container_register(&i->link_attr_cont); 782 783 i->dev_attr_cont.ac.class = &ata_dev_class.class; 784 i->dev_attr_cont.ac.attrs = &i->dev_attrs[0]; 785 i->dev_attr_cont.ac.match = ata_tdev_match; 786 transport_container_register(&i->dev_attr_cont); 787 788 count = 0; 789 SETUP_PORT_ATTRIBUTE(nr_pmp_links); 790 SETUP_PORT_ATTRIBUTE(idle_irq); 791 SETUP_PORT_ATTRIBUTE(port_no); 792 BUG_ON(count > ATA_PORT_ATTRS); 793 i->port_attrs[count] = NULL; 794 795 count = 0; 796 SETUP_LINK_ATTRIBUTE(hw_sata_spd_limit); 797 SETUP_LINK_ATTRIBUTE(sata_spd_limit); 798 SETUP_LINK_ATTRIBUTE(sata_spd); 799 BUG_ON(count > ATA_LINK_ATTRS); 800 i->link_attrs[count] = NULL; 801 802 count = 0; 803 SETUP_DEV_ATTRIBUTE(class); 804 SETUP_DEV_ATTRIBUTE(pio_mode); 805 SETUP_DEV_ATTRIBUTE(dma_mode); 806 SETUP_DEV_ATTRIBUTE(xfer_mode); 807 SETUP_DEV_ATTRIBUTE(spdn_cnt); 808 SETUP_DEV_ATTRIBUTE(ering); 809 SETUP_DEV_ATTRIBUTE(id); 810 SETUP_DEV_ATTRIBUTE(gscr); 811 SETUP_DEV_ATTRIBUTE(trim); 812 BUG_ON(count > ATA_DEV_ATTRS); 813 i->dev_attrs[count] = NULL; 814 815 return &i->t; 816 } 817 818 /** 819 * ata_release_transport -- release ATA transport template instance 820 * @t: transport template instance 821 */ 822 void ata_release_transport(struct scsi_transport_template *t) 823 { 824 struct ata_internal *i = to_ata_internal(t); 825 826 transport_container_unregister(&i->t.host_attrs); 827 transport_container_unregister(&i->link_attr_cont); 828 transport_container_unregister(&i->dev_attr_cont); 829 830 kfree(i); 831 } 832 833 __init int libata_transport_init(void) 834 { 835 int error; 836 837 error = transport_class_register(&ata_link_class); 838 if (error) 839 goto out_unregister_transport; 840 error = transport_class_register(&ata_port_class); 841 if (error) 842 goto out_unregister_link; 843 error = transport_class_register(&ata_dev_class); 844 if (error) 845 goto out_unregister_port; 846 return 0; 847 848 out_unregister_port: 849 transport_class_unregister(&ata_port_class); 850 out_unregister_link: 851 transport_class_unregister(&ata_link_class); 852 out_unregister_transport: 853 return error; 854 855 } 856 857 void __exit libata_transport_exit(void) 858 { 859 transport_class_unregister(&ata_link_class); 860 transport_class_unregister(&ata_port_class); 861 transport_class_unregister(&ata_dev_class); 862 } 863