1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * libata-core.c - helper library for ATA 4 * 5 * Copyright 2003-2004 Red Hat, Inc. All rights reserved. 6 * Copyright 2003-2004 Jeff Garzik 7 * 8 * libata documentation is available via 'make {ps|pdf}docs', 9 * as Documentation/driver-api/libata.rst 10 * 11 * Hardware documentation available from http://www.t13.org/ and 12 * http://www.sata-io.org/ 13 * 14 * Standards documents from: 15 * http://www.t13.org (ATA standards, PCI DMA IDE spec) 16 * http://www.t10.org (SCSI MMC - for ATAPI MMC) 17 * http://www.sata-io.org (SATA) 18 * http://www.compactflash.org (CF) 19 * http://www.qic.org (QIC157 - Tape and DSC) 20 * http://www.ce-ata.org (CE-ATA: not supported) 21 * 22 * libata is essentially a library of internal helper functions for 23 * low-level ATA host controller drivers. As such, the API/ABI is 24 * likely to change as new drivers are added and updated. 25 * Do not depend on ABI/API stability. 26 */ 27 28 #include <linux/kernel.h> 29 #include <linux/module.h> 30 #include <linux/pci.h> 31 #include <linux/init.h> 32 #include <linux/list.h> 33 #include <linux/mm.h> 34 #include <linux/spinlock.h> 35 #include <linux/blkdev.h> 36 #include <linux/delay.h> 37 #include <linux/timer.h> 38 #include <linux/time.h> 39 #include <linux/interrupt.h> 40 #include <linux/completion.h> 41 #include <linux/suspend.h> 42 #include <linux/workqueue.h> 43 #include <linux/scatterlist.h> 44 #include <linux/io.h> 45 #include <linux/log2.h> 46 #include <linux/slab.h> 47 #include <linux/glob.h> 48 #include <scsi/scsi.h> 49 #include <scsi/scsi_cmnd.h> 50 #include <scsi/scsi_host.h> 51 #include <linux/libata.h> 52 #include <asm/byteorder.h> 53 #include <asm/unaligned.h> 54 #include <linux/cdrom.h> 55 #include <linux/ratelimit.h> 56 #include <linux/leds.h> 57 #include <linux/pm_runtime.h> 58 #include <linux/platform_device.h> 59 #include <asm/setup.h> 60 61 #define CREATE_TRACE_POINTS 62 #include <trace/events/libata.h> 63 64 #include "libata.h" 65 #include "libata-transport.h" 66 67 const struct ata_port_operations ata_base_port_ops = { 68 .prereset = ata_std_prereset, 69 .postreset = ata_std_postreset, 70 .error_handler = ata_std_error_handler, 71 .sched_eh = ata_std_sched_eh, 72 .end_eh = ata_std_end_eh, 73 }; 74 75 const struct ata_port_operations sata_port_ops = { 76 .inherits = &ata_base_port_ops, 77 78 .qc_defer = ata_std_qc_defer, 79 .hardreset = sata_std_hardreset, 80 }; 81 EXPORT_SYMBOL_GPL(sata_port_ops); 82 83 static unsigned int ata_dev_init_params(struct ata_device *dev, 84 u16 heads, u16 sectors); 85 static unsigned int ata_dev_set_xfermode(struct ata_device *dev); 86 static void ata_dev_xfermask(struct ata_device *dev); 87 static unsigned long ata_dev_blacklisted(const struct ata_device *dev); 88 89 atomic_t ata_print_id = ATOMIC_INIT(0); 90 91 #ifdef CONFIG_ATA_FORCE 92 struct ata_force_param { 93 const char *name; 94 u8 cbl; 95 u8 spd_limit; 96 unsigned long xfer_mask; 97 unsigned int horkage_on; 98 unsigned int horkage_off; 99 u16 lflags; 100 }; 101 102 struct ata_force_ent { 103 int port; 104 int device; 105 struct ata_force_param param; 106 }; 107 108 static struct ata_force_ent *ata_force_tbl; 109 static int ata_force_tbl_size; 110 111 static char ata_force_param_buf[COMMAND_LINE_SIZE] __initdata; 112 /* param_buf is thrown away after initialization, disallow read */ 113 module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0); 114 MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/admin-guide/kernel-parameters.rst for details)"); 115 #endif 116 117 static int atapi_enabled = 1; 118 module_param(atapi_enabled, int, 0444); 119 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on [default])"); 120 121 static int atapi_dmadir = 0; 122 module_param(atapi_dmadir, int, 0444); 123 MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off [default], 1=on)"); 124 125 int atapi_passthru16 = 1; 126 module_param(atapi_passthru16, int, 0444); 127 MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices (0=off, 1=on [default])"); 128 129 int libata_fua = 0; 130 module_param_named(fua, libata_fua, int, 0444); 131 MODULE_PARM_DESC(fua, "FUA support (0=off [default], 1=on)"); 132 133 static int ata_ignore_hpa; 134 module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644); 135 MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)"); 136 137 static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA; 138 module_param_named(dma, libata_dma_mask, int, 0444); 139 MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)"); 140 141 static int ata_probe_timeout; 142 module_param(ata_probe_timeout, int, 0444); 143 MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)"); 144 145 int libata_noacpi = 0; 146 module_param_named(noacpi, libata_noacpi, int, 0444); 147 MODULE_PARM_DESC(noacpi, "Disable the use of ACPI in probe/suspend/resume (0=off [default], 1=on)"); 148 149 int libata_allow_tpm = 0; 150 module_param_named(allow_tpm, libata_allow_tpm, int, 0444); 151 MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands (0=off [default], 1=on)"); 152 153 static int atapi_an; 154 module_param(atapi_an, int, 0444); 155 MODULE_PARM_DESC(atapi_an, "Enable ATAPI AN media presence notification (0=0ff [default], 1=on)"); 156 157 MODULE_AUTHOR("Jeff Garzik"); 158 MODULE_DESCRIPTION("Library module for ATA devices"); 159 MODULE_LICENSE("GPL"); 160 MODULE_VERSION(DRV_VERSION); 161 162 static inline bool ata_dev_print_info(struct ata_device *dev) 163 { 164 struct ata_eh_context *ehc = &dev->link->eh_context; 165 166 return ehc->i.flags & ATA_EHI_PRINTINFO; 167 } 168 169 static bool ata_sstatus_online(u32 sstatus) 170 { 171 return (sstatus & 0xf) == 0x3; 172 } 173 174 /** 175 * ata_link_next - link iteration helper 176 * @link: the previous link, NULL to start 177 * @ap: ATA port containing links to iterate 178 * @mode: iteration mode, one of ATA_LITER_* 179 * 180 * LOCKING: 181 * Host lock or EH context. 182 * 183 * RETURNS: 184 * Pointer to the next link. 185 */ 186 struct ata_link *ata_link_next(struct ata_link *link, struct ata_port *ap, 187 enum ata_link_iter_mode mode) 188 { 189 BUG_ON(mode != ATA_LITER_EDGE && 190 mode != ATA_LITER_PMP_FIRST && mode != ATA_LITER_HOST_FIRST); 191 192 /* NULL link indicates start of iteration */ 193 if (!link) 194 switch (mode) { 195 case ATA_LITER_EDGE: 196 case ATA_LITER_PMP_FIRST: 197 if (sata_pmp_attached(ap)) 198 return ap->pmp_link; 199 fallthrough; 200 case ATA_LITER_HOST_FIRST: 201 return &ap->link; 202 } 203 204 /* we just iterated over the host link, what's next? */ 205 if (link == &ap->link) 206 switch (mode) { 207 case ATA_LITER_HOST_FIRST: 208 if (sata_pmp_attached(ap)) 209 return ap->pmp_link; 210 fallthrough; 211 case ATA_LITER_PMP_FIRST: 212 if (unlikely(ap->slave_link)) 213 return ap->slave_link; 214 fallthrough; 215 case ATA_LITER_EDGE: 216 return NULL; 217 } 218 219 /* slave_link excludes PMP */ 220 if (unlikely(link == ap->slave_link)) 221 return NULL; 222 223 /* we were over a PMP link */ 224 if (++link < ap->pmp_link + ap->nr_pmp_links) 225 return link; 226 227 if (mode == ATA_LITER_PMP_FIRST) 228 return &ap->link; 229 230 return NULL; 231 } 232 EXPORT_SYMBOL_GPL(ata_link_next); 233 234 /** 235 * ata_dev_next - device iteration helper 236 * @dev: the previous device, NULL to start 237 * @link: ATA link containing devices to iterate 238 * @mode: iteration mode, one of ATA_DITER_* 239 * 240 * LOCKING: 241 * Host lock or EH context. 242 * 243 * RETURNS: 244 * Pointer to the next device. 245 */ 246 struct ata_device *ata_dev_next(struct ata_device *dev, struct ata_link *link, 247 enum ata_dev_iter_mode mode) 248 { 249 BUG_ON(mode != ATA_DITER_ENABLED && mode != ATA_DITER_ENABLED_REVERSE && 250 mode != ATA_DITER_ALL && mode != ATA_DITER_ALL_REVERSE); 251 252 /* NULL dev indicates start of iteration */ 253 if (!dev) 254 switch (mode) { 255 case ATA_DITER_ENABLED: 256 case ATA_DITER_ALL: 257 dev = link->device; 258 goto check; 259 case ATA_DITER_ENABLED_REVERSE: 260 case ATA_DITER_ALL_REVERSE: 261 dev = link->device + ata_link_max_devices(link) - 1; 262 goto check; 263 } 264 265 next: 266 /* move to the next one */ 267 switch (mode) { 268 case ATA_DITER_ENABLED: 269 case ATA_DITER_ALL: 270 if (++dev < link->device + ata_link_max_devices(link)) 271 goto check; 272 return NULL; 273 case ATA_DITER_ENABLED_REVERSE: 274 case ATA_DITER_ALL_REVERSE: 275 if (--dev >= link->device) 276 goto check; 277 return NULL; 278 } 279 280 check: 281 if ((mode == ATA_DITER_ENABLED || mode == ATA_DITER_ENABLED_REVERSE) && 282 !ata_dev_enabled(dev)) 283 goto next; 284 return dev; 285 } 286 EXPORT_SYMBOL_GPL(ata_dev_next); 287 288 /** 289 * ata_dev_phys_link - find physical link for a device 290 * @dev: ATA device to look up physical link for 291 * 292 * Look up physical link which @dev is attached to. Note that 293 * this is different from @dev->link only when @dev is on slave 294 * link. For all other cases, it's the same as @dev->link. 295 * 296 * LOCKING: 297 * Don't care. 298 * 299 * RETURNS: 300 * Pointer to the found physical link. 301 */ 302 struct ata_link *ata_dev_phys_link(struct ata_device *dev) 303 { 304 struct ata_port *ap = dev->link->ap; 305 306 if (!ap->slave_link) 307 return dev->link; 308 if (!dev->devno) 309 return &ap->link; 310 return ap->slave_link; 311 } 312 313 #ifdef CONFIG_ATA_FORCE 314 /** 315 * ata_force_cbl - force cable type according to libata.force 316 * @ap: ATA port of interest 317 * 318 * Force cable type according to libata.force and whine about it. 319 * The last entry which has matching port number is used, so it 320 * can be specified as part of device force parameters. For 321 * example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the 322 * same effect. 323 * 324 * LOCKING: 325 * EH context. 326 */ 327 void ata_force_cbl(struct ata_port *ap) 328 { 329 int i; 330 331 for (i = ata_force_tbl_size - 1; i >= 0; i--) { 332 const struct ata_force_ent *fe = &ata_force_tbl[i]; 333 334 if (fe->port != -1 && fe->port != ap->print_id) 335 continue; 336 337 if (fe->param.cbl == ATA_CBL_NONE) 338 continue; 339 340 ap->cbl = fe->param.cbl; 341 ata_port_notice(ap, "FORCE: cable set to %s\n", fe->param.name); 342 return; 343 } 344 } 345 346 /** 347 * ata_force_link_limits - force link limits according to libata.force 348 * @link: ATA link of interest 349 * 350 * Force link flags and SATA spd limit according to libata.force 351 * and whine about it. When only the port part is specified 352 * (e.g. 1:), the limit applies to all links connected to both 353 * the host link and all fan-out ports connected via PMP. If the 354 * device part is specified as 0 (e.g. 1.00:), it specifies the 355 * first fan-out link not the host link. Device number 15 always 356 * points to the host link whether PMP is attached or not. If the 357 * controller has slave link, device number 16 points to it. 358 * 359 * LOCKING: 360 * EH context. 361 */ 362 static void ata_force_link_limits(struct ata_link *link) 363 { 364 bool did_spd = false; 365 int linkno = link->pmp; 366 int i; 367 368 if (ata_is_host_link(link)) 369 linkno += 15; 370 371 for (i = ata_force_tbl_size - 1; i >= 0; i--) { 372 const struct ata_force_ent *fe = &ata_force_tbl[i]; 373 374 if (fe->port != -1 && fe->port != link->ap->print_id) 375 continue; 376 377 if (fe->device != -1 && fe->device != linkno) 378 continue; 379 380 /* only honor the first spd limit */ 381 if (!did_spd && fe->param.spd_limit) { 382 link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1; 383 ata_link_notice(link, "FORCE: PHY spd limit set to %s\n", 384 fe->param.name); 385 did_spd = true; 386 } 387 388 /* let lflags stack */ 389 if (fe->param.lflags) { 390 link->flags |= fe->param.lflags; 391 ata_link_notice(link, 392 "FORCE: link flag 0x%x forced -> 0x%x\n", 393 fe->param.lflags, link->flags); 394 } 395 } 396 } 397 398 /** 399 * ata_force_xfermask - force xfermask according to libata.force 400 * @dev: ATA device of interest 401 * 402 * Force xfer_mask according to libata.force and whine about it. 403 * For consistency with link selection, device number 15 selects 404 * the first device connected to the host link. 405 * 406 * LOCKING: 407 * EH context. 408 */ 409 static void ata_force_xfermask(struct ata_device *dev) 410 { 411 int devno = dev->link->pmp + dev->devno; 412 int alt_devno = devno; 413 int i; 414 415 /* allow n.15/16 for devices attached to host port */ 416 if (ata_is_host_link(dev->link)) 417 alt_devno += 15; 418 419 for (i = ata_force_tbl_size - 1; i >= 0; i--) { 420 const struct ata_force_ent *fe = &ata_force_tbl[i]; 421 unsigned long pio_mask, mwdma_mask, udma_mask; 422 423 if (fe->port != -1 && fe->port != dev->link->ap->print_id) 424 continue; 425 426 if (fe->device != -1 && fe->device != devno && 427 fe->device != alt_devno) 428 continue; 429 430 if (!fe->param.xfer_mask) 431 continue; 432 433 ata_unpack_xfermask(fe->param.xfer_mask, 434 &pio_mask, &mwdma_mask, &udma_mask); 435 if (udma_mask) 436 dev->udma_mask = udma_mask; 437 else if (mwdma_mask) { 438 dev->udma_mask = 0; 439 dev->mwdma_mask = mwdma_mask; 440 } else { 441 dev->udma_mask = 0; 442 dev->mwdma_mask = 0; 443 dev->pio_mask = pio_mask; 444 } 445 446 ata_dev_notice(dev, "FORCE: xfer_mask set to %s\n", 447 fe->param.name); 448 return; 449 } 450 } 451 452 /** 453 * ata_force_horkage - force horkage according to libata.force 454 * @dev: ATA device of interest 455 * 456 * Force horkage according to libata.force and whine about it. 457 * For consistency with link selection, device number 15 selects 458 * the first device connected to the host link. 459 * 460 * LOCKING: 461 * EH context. 462 */ 463 static void ata_force_horkage(struct ata_device *dev) 464 { 465 int devno = dev->link->pmp + dev->devno; 466 int alt_devno = devno; 467 int i; 468 469 /* allow n.15/16 for devices attached to host port */ 470 if (ata_is_host_link(dev->link)) 471 alt_devno += 15; 472 473 for (i = 0; i < ata_force_tbl_size; i++) { 474 const struct ata_force_ent *fe = &ata_force_tbl[i]; 475 476 if (fe->port != -1 && fe->port != dev->link->ap->print_id) 477 continue; 478 479 if (fe->device != -1 && fe->device != devno && 480 fe->device != alt_devno) 481 continue; 482 483 if (!(~dev->horkage & fe->param.horkage_on) && 484 !(dev->horkage & fe->param.horkage_off)) 485 continue; 486 487 dev->horkage |= fe->param.horkage_on; 488 dev->horkage &= ~fe->param.horkage_off; 489 490 ata_dev_notice(dev, "FORCE: horkage modified (%s)\n", 491 fe->param.name); 492 } 493 } 494 #else 495 static inline void ata_force_link_limits(struct ata_link *link) { } 496 static inline void ata_force_xfermask(struct ata_device *dev) { } 497 static inline void ata_force_horkage(struct ata_device *dev) { } 498 #endif 499 500 /** 501 * atapi_cmd_type - Determine ATAPI command type from SCSI opcode 502 * @opcode: SCSI opcode 503 * 504 * Determine ATAPI command type from @opcode. 505 * 506 * LOCKING: 507 * None. 508 * 509 * RETURNS: 510 * ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC} 511 */ 512 int atapi_cmd_type(u8 opcode) 513 { 514 switch (opcode) { 515 case GPCMD_READ_10: 516 case GPCMD_READ_12: 517 return ATAPI_READ; 518 519 case GPCMD_WRITE_10: 520 case GPCMD_WRITE_12: 521 case GPCMD_WRITE_AND_VERIFY_10: 522 return ATAPI_WRITE; 523 524 case GPCMD_READ_CD: 525 case GPCMD_READ_CD_MSF: 526 return ATAPI_READ_CD; 527 528 case ATA_16: 529 case ATA_12: 530 if (atapi_passthru16) 531 return ATAPI_PASS_THRU; 532 fallthrough; 533 default: 534 return ATAPI_MISC; 535 } 536 } 537 EXPORT_SYMBOL_GPL(atapi_cmd_type); 538 539 static const u8 ata_rw_cmds[] = { 540 /* pio multi */ 541 ATA_CMD_READ_MULTI, 542 ATA_CMD_WRITE_MULTI, 543 ATA_CMD_READ_MULTI_EXT, 544 ATA_CMD_WRITE_MULTI_EXT, 545 0, 546 0, 547 0, 548 ATA_CMD_WRITE_MULTI_FUA_EXT, 549 /* pio */ 550 ATA_CMD_PIO_READ, 551 ATA_CMD_PIO_WRITE, 552 ATA_CMD_PIO_READ_EXT, 553 ATA_CMD_PIO_WRITE_EXT, 554 0, 555 0, 556 0, 557 0, 558 /* dma */ 559 ATA_CMD_READ, 560 ATA_CMD_WRITE, 561 ATA_CMD_READ_EXT, 562 ATA_CMD_WRITE_EXT, 563 0, 564 0, 565 0, 566 ATA_CMD_WRITE_FUA_EXT 567 }; 568 569 /** 570 * ata_rwcmd_protocol - set taskfile r/w commands and protocol 571 * @tf: command to examine and configure 572 * @dev: device tf belongs to 573 * 574 * Examine the device configuration and tf->flags to calculate 575 * the proper read/write commands and protocol to use. 576 * 577 * LOCKING: 578 * caller. 579 */ 580 static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev) 581 { 582 u8 cmd; 583 584 int index, fua, lba48, write; 585 586 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0; 587 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0; 588 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0; 589 590 if (dev->flags & ATA_DFLAG_PIO) { 591 tf->protocol = ATA_PROT_PIO; 592 index = dev->multi_count ? 0 : 8; 593 } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) { 594 /* Unable to use DMA due to host limitation */ 595 tf->protocol = ATA_PROT_PIO; 596 index = dev->multi_count ? 0 : 8; 597 } else { 598 tf->protocol = ATA_PROT_DMA; 599 index = 16; 600 } 601 602 cmd = ata_rw_cmds[index + fua + lba48 + write]; 603 if (cmd) { 604 tf->command = cmd; 605 return 0; 606 } 607 return -1; 608 } 609 610 /** 611 * ata_tf_read_block - Read block address from ATA taskfile 612 * @tf: ATA taskfile of interest 613 * @dev: ATA device @tf belongs to 614 * 615 * LOCKING: 616 * None. 617 * 618 * Read block address from @tf. This function can handle all 619 * three address formats - LBA, LBA48 and CHS. tf->protocol and 620 * flags select the address format to use. 621 * 622 * RETURNS: 623 * Block address read from @tf. 624 */ 625 u64 ata_tf_read_block(const struct ata_taskfile *tf, struct ata_device *dev) 626 { 627 u64 block = 0; 628 629 if (tf->flags & ATA_TFLAG_LBA) { 630 if (tf->flags & ATA_TFLAG_LBA48) { 631 block |= (u64)tf->hob_lbah << 40; 632 block |= (u64)tf->hob_lbam << 32; 633 block |= (u64)tf->hob_lbal << 24; 634 } else 635 block |= (tf->device & 0xf) << 24; 636 637 block |= tf->lbah << 16; 638 block |= tf->lbam << 8; 639 block |= tf->lbal; 640 } else { 641 u32 cyl, head, sect; 642 643 cyl = tf->lbam | (tf->lbah << 8); 644 head = tf->device & 0xf; 645 sect = tf->lbal; 646 647 if (!sect) { 648 ata_dev_warn(dev, 649 "device reported invalid CHS sector 0\n"); 650 return U64_MAX; 651 } 652 653 block = (cyl * dev->heads + head) * dev->sectors + sect - 1; 654 } 655 656 return block; 657 } 658 659 /** 660 * ata_build_rw_tf - Build ATA taskfile for given read/write request 661 * @tf: Target ATA taskfile 662 * @dev: ATA device @tf belongs to 663 * @block: Block address 664 * @n_block: Number of blocks 665 * @tf_flags: RW/FUA etc... 666 * @tag: tag 667 * @class: IO priority class 668 * 669 * LOCKING: 670 * None. 671 * 672 * Build ATA taskfile @tf for read/write request described by 673 * @block, @n_block, @tf_flags and @tag on @dev. 674 * 675 * RETURNS: 676 * 677 * 0 on success, -ERANGE if the request is too large for @dev, 678 * -EINVAL if the request is invalid. 679 */ 680 int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev, 681 u64 block, u32 n_block, unsigned int tf_flags, 682 unsigned int tag, int class) 683 { 684 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 685 tf->flags |= tf_flags; 686 687 if (ata_ncq_enabled(dev) && !ata_tag_internal(tag)) { 688 /* yay, NCQ */ 689 if (!lba_48_ok(block, n_block)) 690 return -ERANGE; 691 692 tf->protocol = ATA_PROT_NCQ; 693 tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48; 694 695 if (tf->flags & ATA_TFLAG_WRITE) 696 tf->command = ATA_CMD_FPDMA_WRITE; 697 else 698 tf->command = ATA_CMD_FPDMA_READ; 699 700 tf->nsect = tag << 3; 701 tf->hob_feature = (n_block >> 8) & 0xff; 702 tf->feature = n_block & 0xff; 703 704 tf->hob_lbah = (block >> 40) & 0xff; 705 tf->hob_lbam = (block >> 32) & 0xff; 706 tf->hob_lbal = (block >> 24) & 0xff; 707 tf->lbah = (block >> 16) & 0xff; 708 tf->lbam = (block >> 8) & 0xff; 709 tf->lbal = block & 0xff; 710 711 tf->device = ATA_LBA; 712 if (tf->flags & ATA_TFLAG_FUA) 713 tf->device |= 1 << 7; 714 715 if (dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE && 716 class == IOPRIO_CLASS_RT) 717 tf->hob_nsect |= ATA_PRIO_HIGH << ATA_SHIFT_PRIO; 718 } else if (dev->flags & ATA_DFLAG_LBA) { 719 tf->flags |= ATA_TFLAG_LBA; 720 721 if (lba_28_ok(block, n_block)) { 722 /* use LBA28 */ 723 tf->device |= (block >> 24) & 0xf; 724 } else if (lba_48_ok(block, n_block)) { 725 if (!(dev->flags & ATA_DFLAG_LBA48)) 726 return -ERANGE; 727 728 /* use LBA48 */ 729 tf->flags |= ATA_TFLAG_LBA48; 730 731 tf->hob_nsect = (n_block >> 8) & 0xff; 732 733 tf->hob_lbah = (block >> 40) & 0xff; 734 tf->hob_lbam = (block >> 32) & 0xff; 735 tf->hob_lbal = (block >> 24) & 0xff; 736 } else 737 /* request too large even for LBA48 */ 738 return -ERANGE; 739 740 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0)) 741 return -EINVAL; 742 743 tf->nsect = n_block & 0xff; 744 745 tf->lbah = (block >> 16) & 0xff; 746 tf->lbam = (block >> 8) & 0xff; 747 tf->lbal = block & 0xff; 748 749 tf->device |= ATA_LBA; 750 } else { 751 /* CHS */ 752 u32 sect, head, cyl, track; 753 754 /* The request -may- be too large for CHS addressing. */ 755 if (!lba_28_ok(block, n_block)) 756 return -ERANGE; 757 758 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0)) 759 return -EINVAL; 760 761 /* Convert LBA to CHS */ 762 track = (u32)block / dev->sectors; 763 cyl = track / dev->heads; 764 head = track % dev->heads; 765 sect = (u32)block % dev->sectors + 1; 766 767 /* Check whether the converted CHS can fit. 768 Cylinder: 0-65535 769 Head: 0-15 770 Sector: 1-255*/ 771 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect)) 772 return -ERANGE; 773 774 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */ 775 tf->lbal = sect; 776 tf->lbam = cyl; 777 tf->lbah = cyl >> 8; 778 tf->device |= head; 779 } 780 781 return 0; 782 } 783 784 /** 785 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask 786 * @pio_mask: pio_mask 787 * @mwdma_mask: mwdma_mask 788 * @udma_mask: udma_mask 789 * 790 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single 791 * unsigned int xfer_mask. 792 * 793 * LOCKING: 794 * None. 795 * 796 * RETURNS: 797 * Packed xfer_mask. 798 */ 799 unsigned long ata_pack_xfermask(unsigned long pio_mask, 800 unsigned long mwdma_mask, 801 unsigned long udma_mask) 802 { 803 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) | 804 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) | 805 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA); 806 } 807 EXPORT_SYMBOL_GPL(ata_pack_xfermask); 808 809 /** 810 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks 811 * @xfer_mask: xfer_mask to unpack 812 * @pio_mask: resulting pio_mask 813 * @mwdma_mask: resulting mwdma_mask 814 * @udma_mask: resulting udma_mask 815 * 816 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask. 817 * Any NULL destination masks will be ignored. 818 */ 819 void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask, 820 unsigned long *mwdma_mask, unsigned long *udma_mask) 821 { 822 if (pio_mask) 823 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO; 824 if (mwdma_mask) 825 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA; 826 if (udma_mask) 827 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA; 828 } 829 830 static const struct ata_xfer_ent { 831 int shift, bits; 832 u8 base; 833 } ata_xfer_tbl[] = { 834 { ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 }, 835 { ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 }, 836 { ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 }, 837 { -1, }, 838 }; 839 840 /** 841 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask 842 * @xfer_mask: xfer_mask of interest 843 * 844 * Return matching XFER_* value for @xfer_mask. Only the highest 845 * bit of @xfer_mask is considered. 846 * 847 * LOCKING: 848 * None. 849 * 850 * RETURNS: 851 * Matching XFER_* value, 0xff if no match found. 852 */ 853 u8 ata_xfer_mask2mode(unsigned long xfer_mask) 854 { 855 int highbit = fls(xfer_mask) - 1; 856 const struct ata_xfer_ent *ent; 857 858 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++) 859 if (highbit >= ent->shift && highbit < ent->shift + ent->bits) 860 return ent->base + highbit - ent->shift; 861 return 0xff; 862 } 863 EXPORT_SYMBOL_GPL(ata_xfer_mask2mode); 864 865 /** 866 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_* 867 * @xfer_mode: XFER_* of interest 868 * 869 * Return matching xfer_mask for @xfer_mode. 870 * 871 * LOCKING: 872 * None. 873 * 874 * RETURNS: 875 * Matching xfer_mask, 0 if no match found. 876 */ 877 unsigned long ata_xfer_mode2mask(u8 xfer_mode) 878 { 879 const struct ata_xfer_ent *ent; 880 881 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++) 882 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits) 883 return ((2 << (ent->shift + xfer_mode - ent->base)) - 1) 884 & ~((1 << ent->shift) - 1); 885 return 0; 886 } 887 EXPORT_SYMBOL_GPL(ata_xfer_mode2mask); 888 889 /** 890 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_* 891 * @xfer_mode: XFER_* of interest 892 * 893 * Return matching xfer_shift for @xfer_mode. 894 * 895 * LOCKING: 896 * None. 897 * 898 * RETURNS: 899 * Matching xfer_shift, -1 if no match found. 900 */ 901 int ata_xfer_mode2shift(unsigned long xfer_mode) 902 { 903 const struct ata_xfer_ent *ent; 904 905 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++) 906 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits) 907 return ent->shift; 908 return -1; 909 } 910 EXPORT_SYMBOL_GPL(ata_xfer_mode2shift); 911 912 /** 913 * ata_mode_string - convert xfer_mask to string 914 * @xfer_mask: mask of bits supported; only highest bit counts. 915 * 916 * Determine string which represents the highest speed 917 * (highest bit in @modemask). 918 * 919 * LOCKING: 920 * None. 921 * 922 * RETURNS: 923 * Constant C string representing highest speed listed in 924 * @mode_mask, or the constant C string "<n/a>". 925 */ 926 const char *ata_mode_string(unsigned long xfer_mask) 927 { 928 static const char * const xfer_mode_str[] = { 929 "PIO0", 930 "PIO1", 931 "PIO2", 932 "PIO3", 933 "PIO4", 934 "PIO5", 935 "PIO6", 936 "MWDMA0", 937 "MWDMA1", 938 "MWDMA2", 939 "MWDMA3", 940 "MWDMA4", 941 "UDMA/16", 942 "UDMA/25", 943 "UDMA/33", 944 "UDMA/44", 945 "UDMA/66", 946 "UDMA/100", 947 "UDMA/133", 948 "UDMA7", 949 }; 950 int highbit; 951 952 highbit = fls(xfer_mask) - 1; 953 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str)) 954 return xfer_mode_str[highbit]; 955 return "<n/a>"; 956 } 957 EXPORT_SYMBOL_GPL(ata_mode_string); 958 959 const char *sata_spd_string(unsigned int spd) 960 { 961 static const char * const spd_str[] = { 962 "1.5 Gbps", 963 "3.0 Gbps", 964 "6.0 Gbps", 965 }; 966 967 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str)) 968 return "<unknown>"; 969 return spd_str[spd - 1]; 970 } 971 972 /** 973 * ata_dev_classify - determine device type based on ATA-spec signature 974 * @tf: ATA taskfile register set for device to be identified 975 * 976 * Determine from taskfile register contents whether a device is 977 * ATA or ATAPI, as per "Signature and persistence" section 978 * of ATA/PI spec (volume 1, sect 5.14). 979 * 980 * LOCKING: 981 * None. 982 * 983 * RETURNS: 984 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP, 985 * %ATA_DEV_ZAC, or %ATA_DEV_UNKNOWN the event of failure. 986 */ 987 unsigned int ata_dev_classify(const struct ata_taskfile *tf) 988 { 989 /* Apple's open source Darwin code hints that some devices only 990 * put a proper signature into the LBA mid/high registers, 991 * So, we only check those. It's sufficient for uniqueness. 992 * 993 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate 994 * signatures for ATA and ATAPI devices attached on SerialATA, 995 * 0x3c/0xc3 and 0x69/0x96 respectively. However, SerialATA 996 * spec has never mentioned about using different signatures 997 * for ATA/ATAPI devices. Then, Serial ATA II: Port 998 * Multiplier specification began to use 0x69/0x96 to identify 999 * port multpliers and 0x3c/0xc3 to identify SEMB device. 1000 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and 1001 * 0x69/0x96 shortly and described them as reserved for 1002 * SerialATA. 1003 * 1004 * We follow the current spec and consider that 0x69/0x96 1005 * identifies a port multiplier and 0x3c/0xc3 a SEMB device. 1006 * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports 1007 * SEMB signature. This is worked around in 1008 * ata_dev_read_id(). 1009 */ 1010 if (tf->lbam == 0 && tf->lbah == 0) 1011 return ATA_DEV_ATA; 1012 1013 if (tf->lbam == 0x14 && tf->lbah == 0xeb) 1014 return ATA_DEV_ATAPI; 1015 1016 if (tf->lbam == 0x69 && tf->lbah == 0x96) 1017 return ATA_DEV_PMP; 1018 1019 if (tf->lbam == 0x3c && tf->lbah == 0xc3) 1020 return ATA_DEV_SEMB; 1021 1022 if (tf->lbam == 0xcd && tf->lbah == 0xab) 1023 return ATA_DEV_ZAC; 1024 1025 return ATA_DEV_UNKNOWN; 1026 } 1027 EXPORT_SYMBOL_GPL(ata_dev_classify); 1028 1029 /** 1030 * ata_id_string - Convert IDENTIFY DEVICE page into string 1031 * @id: IDENTIFY DEVICE results we will examine 1032 * @s: string into which data is output 1033 * @ofs: offset into identify device page 1034 * @len: length of string to return. must be an even number. 1035 * 1036 * The strings in the IDENTIFY DEVICE page are broken up into 1037 * 16-bit chunks. Run through the string, and output each 1038 * 8-bit chunk linearly, regardless of platform. 1039 * 1040 * LOCKING: 1041 * caller. 1042 */ 1043 1044 void ata_id_string(const u16 *id, unsigned char *s, 1045 unsigned int ofs, unsigned int len) 1046 { 1047 unsigned int c; 1048 1049 BUG_ON(len & 1); 1050 1051 while (len > 0) { 1052 c = id[ofs] >> 8; 1053 *s = c; 1054 s++; 1055 1056 c = id[ofs] & 0xff; 1057 *s = c; 1058 s++; 1059 1060 ofs++; 1061 len -= 2; 1062 } 1063 } 1064 EXPORT_SYMBOL_GPL(ata_id_string); 1065 1066 /** 1067 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string 1068 * @id: IDENTIFY DEVICE results we will examine 1069 * @s: string into which data is output 1070 * @ofs: offset into identify device page 1071 * @len: length of string to return. must be an odd number. 1072 * 1073 * This function is identical to ata_id_string except that it 1074 * trims trailing spaces and terminates the resulting string with 1075 * null. @len must be actual maximum length (even number) + 1. 1076 * 1077 * LOCKING: 1078 * caller. 1079 */ 1080 void ata_id_c_string(const u16 *id, unsigned char *s, 1081 unsigned int ofs, unsigned int len) 1082 { 1083 unsigned char *p; 1084 1085 ata_id_string(id, s, ofs, len - 1); 1086 1087 p = s + strnlen(s, len - 1); 1088 while (p > s && p[-1] == ' ') 1089 p--; 1090 *p = '\0'; 1091 } 1092 EXPORT_SYMBOL_GPL(ata_id_c_string); 1093 1094 static u64 ata_id_n_sectors(const u16 *id) 1095 { 1096 if (ata_id_has_lba(id)) { 1097 if (ata_id_has_lba48(id)) 1098 return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2); 1099 else 1100 return ata_id_u32(id, ATA_ID_LBA_CAPACITY); 1101 } else { 1102 if (ata_id_current_chs_valid(id)) 1103 return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] * 1104 id[ATA_ID_CUR_SECTORS]; 1105 else 1106 return id[ATA_ID_CYLS] * id[ATA_ID_HEADS] * 1107 id[ATA_ID_SECTORS]; 1108 } 1109 } 1110 1111 u64 ata_tf_to_lba48(const struct ata_taskfile *tf) 1112 { 1113 u64 sectors = 0; 1114 1115 sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40; 1116 sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32; 1117 sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24; 1118 sectors |= (tf->lbah & 0xff) << 16; 1119 sectors |= (tf->lbam & 0xff) << 8; 1120 sectors |= (tf->lbal & 0xff); 1121 1122 return sectors; 1123 } 1124 1125 u64 ata_tf_to_lba(const struct ata_taskfile *tf) 1126 { 1127 u64 sectors = 0; 1128 1129 sectors |= (tf->device & 0x0f) << 24; 1130 sectors |= (tf->lbah & 0xff) << 16; 1131 sectors |= (tf->lbam & 0xff) << 8; 1132 sectors |= (tf->lbal & 0xff); 1133 1134 return sectors; 1135 } 1136 1137 /** 1138 * ata_read_native_max_address - Read native max address 1139 * @dev: target device 1140 * @max_sectors: out parameter for the result native max address 1141 * 1142 * Perform an LBA48 or LBA28 native size query upon the device in 1143 * question. 1144 * 1145 * RETURNS: 1146 * 0 on success, -EACCES if command is aborted by the drive. 1147 * -EIO on other errors. 1148 */ 1149 static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors) 1150 { 1151 unsigned int err_mask; 1152 struct ata_taskfile tf; 1153 int lba48 = ata_id_has_lba48(dev->id); 1154 1155 ata_tf_init(dev, &tf); 1156 1157 /* always clear all address registers */ 1158 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR; 1159 1160 if (lba48) { 1161 tf.command = ATA_CMD_READ_NATIVE_MAX_EXT; 1162 tf.flags |= ATA_TFLAG_LBA48; 1163 } else 1164 tf.command = ATA_CMD_READ_NATIVE_MAX; 1165 1166 tf.protocol = ATA_PROT_NODATA; 1167 tf.device |= ATA_LBA; 1168 1169 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0); 1170 if (err_mask) { 1171 ata_dev_warn(dev, 1172 "failed to read native max address (err_mask=0x%x)\n", 1173 err_mask); 1174 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED)) 1175 return -EACCES; 1176 return -EIO; 1177 } 1178 1179 if (lba48) 1180 *max_sectors = ata_tf_to_lba48(&tf) + 1; 1181 else 1182 *max_sectors = ata_tf_to_lba(&tf) + 1; 1183 if (dev->horkage & ATA_HORKAGE_HPA_SIZE) 1184 (*max_sectors)--; 1185 return 0; 1186 } 1187 1188 /** 1189 * ata_set_max_sectors - Set max sectors 1190 * @dev: target device 1191 * @new_sectors: new max sectors value to set for the device 1192 * 1193 * Set max sectors of @dev to @new_sectors. 1194 * 1195 * RETURNS: 1196 * 0 on success, -EACCES if command is aborted or denied (due to 1197 * previous non-volatile SET_MAX) by the drive. -EIO on other 1198 * errors. 1199 */ 1200 static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors) 1201 { 1202 unsigned int err_mask; 1203 struct ata_taskfile tf; 1204 int lba48 = ata_id_has_lba48(dev->id); 1205 1206 new_sectors--; 1207 1208 ata_tf_init(dev, &tf); 1209 1210 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR; 1211 1212 if (lba48) { 1213 tf.command = ATA_CMD_SET_MAX_EXT; 1214 tf.flags |= ATA_TFLAG_LBA48; 1215 1216 tf.hob_lbal = (new_sectors >> 24) & 0xff; 1217 tf.hob_lbam = (new_sectors >> 32) & 0xff; 1218 tf.hob_lbah = (new_sectors >> 40) & 0xff; 1219 } else { 1220 tf.command = ATA_CMD_SET_MAX; 1221 1222 tf.device |= (new_sectors >> 24) & 0xf; 1223 } 1224 1225 tf.protocol = ATA_PROT_NODATA; 1226 tf.device |= ATA_LBA; 1227 1228 tf.lbal = (new_sectors >> 0) & 0xff; 1229 tf.lbam = (new_sectors >> 8) & 0xff; 1230 tf.lbah = (new_sectors >> 16) & 0xff; 1231 1232 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0); 1233 if (err_mask) { 1234 ata_dev_warn(dev, 1235 "failed to set max address (err_mask=0x%x)\n", 1236 err_mask); 1237 if (err_mask == AC_ERR_DEV && 1238 (tf.feature & (ATA_ABORTED | ATA_IDNF))) 1239 return -EACCES; 1240 return -EIO; 1241 } 1242 1243 return 0; 1244 } 1245 1246 /** 1247 * ata_hpa_resize - Resize a device with an HPA set 1248 * @dev: Device to resize 1249 * 1250 * Read the size of an LBA28 or LBA48 disk with HPA features and resize 1251 * it if required to the full size of the media. The caller must check 1252 * the drive has the HPA feature set enabled. 1253 * 1254 * RETURNS: 1255 * 0 on success, -errno on failure. 1256 */ 1257 static int ata_hpa_resize(struct ata_device *dev) 1258 { 1259 bool print_info = ata_dev_print_info(dev); 1260 bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA; 1261 u64 sectors = ata_id_n_sectors(dev->id); 1262 u64 native_sectors; 1263 int rc; 1264 1265 /* do we need to do it? */ 1266 if ((dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ZAC) || 1267 !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) || 1268 (dev->horkage & ATA_HORKAGE_BROKEN_HPA)) 1269 return 0; 1270 1271 /* read native max address */ 1272 rc = ata_read_native_max_address(dev, &native_sectors); 1273 if (rc) { 1274 /* If device aborted the command or HPA isn't going to 1275 * be unlocked, skip HPA resizing. 1276 */ 1277 if (rc == -EACCES || !unlock_hpa) { 1278 ata_dev_warn(dev, 1279 "HPA support seems broken, skipping HPA handling\n"); 1280 dev->horkage |= ATA_HORKAGE_BROKEN_HPA; 1281 1282 /* we can continue if device aborted the command */ 1283 if (rc == -EACCES) 1284 rc = 0; 1285 } 1286 1287 return rc; 1288 } 1289 dev->n_native_sectors = native_sectors; 1290 1291 /* nothing to do? */ 1292 if (native_sectors <= sectors || !unlock_hpa) { 1293 if (!print_info || native_sectors == sectors) 1294 return 0; 1295 1296 if (native_sectors > sectors) 1297 ata_dev_info(dev, 1298 "HPA detected: current %llu, native %llu\n", 1299 (unsigned long long)sectors, 1300 (unsigned long long)native_sectors); 1301 else if (native_sectors < sectors) 1302 ata_dev_warn(dev, 1303 "native sectors (%llu) is smaller than sectors (%llu)\n", 1304 (unsigned long long)native_sectors, 1305 (unsigned long long)sectors); 1306 return 0; 1307 } 1308 1309 /* let's unlock HPA */ 1310 rc = ata_set_max_sectors(dev, native_sectors); 1311 if (rc == -EACCES) { 1312 /* if device aborted the command, skip HPA resizing */ 1313 ata_dev_warn(dev, 1314 "device aborted resize (%llu -> %llu), skipping HPA handling\n", 1315 (unsigned long long)sectors, 1316 (unsigned long long)native_sectors); 1317 dev->horkage |= ATA_HORKAGE_BROKEN_HPA; 1318 return 0; 1319 } else if (rc) 1320 return rc; 1321 1322 /* re-read IDENTIFY data */ 1323 rc = ata_dev_reread_id(dev, 0); 1324 if (rc) { 1325 ata_dev_err(dev, 1326 "failed to re-read IDENTIFY data after HPA resizing\n"); 1327 return rc; 1328 } 1329 1330 if (print_info) { 1331 u64 new_sectors = ata_id_n_sectors(dev->id); 1332 ata_dev_info(dev, 1333 "HPA unlocked: %llu -> %llu, native %llu\n", 1334 (unsigned long long)sectors, 1335 (unsigned long long)new_sectors, 1336 (unsigned long long)native_sectors); 1337 } 1338 1339 return 0; 1340 } 1341 1342 /** 1343 * ata_dump_id - IDENTIFY DEVICE info debugging output 1344 * @dev: device from which the information is fetched 1345 * @id: IDENTIFY DEVICE page to dump 1346 * 1347 * Dump selected 16-bit words from the given IDENTIFY DEVICE 1348 * page. 1349 * 1350 * LOCKING: 1351 * caller. 1352 */ 1353 1354 static inline void ata_dump_id(struct ata_device *dev, const u16 *id) 1355 { 1356 ata_dev_dbg(dev, 1357 "49==0x%04x 53==0x%04x 63==0x%04x 64==0x%04x 75==0x%04x\n" 1358 "80==0x%04x 81==0x%04x 82==0x%04x 83==0x%04x 84==0x%04x\n" 1359 "88==0x%04x 93==0x%04x\n", 1360 id[49], id[53], id[63], id[64], id[75], id[80], 1361 id[81], id[82], id[83], id[84], id[88], id[93]); 1362 } 1363 1364 /** 1365 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data 1366 * @id: IDENTIFY data to compute xfer mask from 1367 * 1368 * Compute the xfermask for this device. This is not as trivial 1369 * as it seems if we must consider early devices correctly. 1370 * 1371 * FIXME: pre IDE drive timing (do we care ?). 1372 * 1373 * LOCKING: 1374 * None. 1375 * 1376 * RETURNS: 1377 * Computed xfermask 1378 */ 1379 unsigned long ata_id_xfermask(const u16 *id) 1380 { 1381 unsigned long pio_mask, mwdma_mask, udma_mask; 1382 1383 /* Usual case. Word 53 indicates word 64 is valid */ 1384 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) { 1385 pio_mask = id[ATA_ID_PIO_MODES] & 0x03; 1386 pio_mask <<= 3; 1387 pio_mask |= 0x7; 1388 } else { 1389 /* If word 64 isn't valid then Word 51 high byte holds 1390 * the PIO timing number for the maximum. Turn it into 1391 * a mask. 1392 */ 1393 u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF; 1394 if (mode < 5) /* Valid PIO range */ 1395 pio_mask = (2 << mode) - 1; 1396 else 1397 pio_mask = 1; 1398 1399 /* But wait.. there's more. Design your standards by 1400 * committee and you too can get a free iordy field to 1401 * process. However its the speeds not the modes that 1402 * are supported... Note drivers using the timing API 1403 * will get this right anyway 1404 */ 1405 } 1406 1407 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07; 1408 1409 if (ata_id_is_cfa(id)) { 1410 /* 1411 * Process compact flash extended modes 1412 */ 1413 int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7; 1414 int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7; 1415 1416 if (pio) 1417 pio_mask |= (1 << 5); 1418 if (pio > 1) 1419 pio_mask |= (1 << 6); 1420 if (dma) 1421 mwdma_mask |= (1 << 3); 1422 if (dma > 1) 1423 mwdma_mask |= (1 << 4); 1424 } 1425 1426 udma_mask = 0; 1427 if (id[ATA_ID_FIELD_VALID] & (1 << 2)) 1428 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff; 1429 1430 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask); 1431 } 1432 EXPORT_SYMBOL_GPL(ata_id_xfermask); 1433 1434 static void ata_qc_complete_internal(struct ata_queued_cmd *qc) 1435 { 1436 struct completion *waiting = qc->private_data; 1437 1438 complete(waiting); 1439 } 1440 1441 /** 1442 * ata_exec_internal_sg - execute libata internal command 1443 * @dev: Device to which the command is sent 1444 * @tf: Taskfile registers for the command and the result 1445 * @cdb: CDB for packet command 1446 * @dma_dir: Data transfer direction of the command 1447 * @sgl: sg list for the data buffer of the command 1448 * @n_elem: Number of sg entries 1449 * @timeout: Timeout in msecs (0 for default) 1450 * 1451 * Executes libata internal command with timeout. @tf contains 1452 * command on entry and result on return. Timeout and error 1453 * conditions are reported via return value. No recovery action 1454 * is taken after a command times out. It's caller's duty to 1455 * clean up after timeout. 1456 * 1457 * LOCKING: 1458 * None. Should be called with kernel context, might sleep. 1459 * 1460 * RETURNS: 1461 * Zero on success, AC_ERR_* mask on failure 1462 */ 1463 unsigned ata_exec_internal_sg(struct ata_device *dev, 1464 struct ata_taskfile *tf, const u8 *cdb, 1465 int dma_dir, struct scatterlist *sgl, 1466 unsigned int n_elem, unsigned long timeout) 1467 { 1468 struct ata_link *link = dev->link; 1469 struct ata_port *ap = link->ap; 1470 u8 command = tf->command; 1471 int auto_timeout = 0; 1472 struct ata_queued_cmd *qc; 1473 unsigned int preempted_tag; 1474 u32 preempted_sactive; 1475 u64 preempted_qc_active; 1476 int preempted_nr_active_links; 1477 DECLARE_COMPLETION_ONSTACK(wait); 1478 unsigned long flags; 1479 unsigned int err_mask; 1480 int rc; 1481 1482 spin_lock_irqsave(ap->lock, flags); 1483 1484 /* no internal command while frozen */ 1485 if (ap->pflags & ATA_PFLAG_FROZEN) { 1486 spin_unlock_irqrestore(ap->lock, flags); 1487 return AC_ERR_SYSTEM; 1488 } 1489 1490 /* initialize internal qc */ 1491 qc = __ata_qc_from_tag(ap, ATA_TAG_INTERNAL); 1492 1493 qc->tag = ATA_TAG_INTERNAL; 1494 qc->hw_tag = 0; 1495 qc->scsicmd = NULL; 1496 qc->ap = ap; 1497 qc->dev = dev; 1498 ata_qc_reinit(qc); 1499 1500 preempted_tag = link->active_tag; 1501 preempted_sactive = link->sactive; 1502 preempted_qc_active = ap->qc_active; 1503 preempted_nr_active_links = ap->nr_active_links; 1504 link->active_tag = ATA_TAG_POISON; 1505 link->sactive = 0; 1506 ap->qc_active = 0; 1507 ap->nr_active_links = 0; 1508 1509 /* prepare & issue qc */ 1510 qc->tf = *tf; 1511 if (cdb) 1512 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN); 1513 1514 /* some SATA bridges need us to indicate data xfer direction */ 1515 if (tf->protocol == ATAPI_PROT_DMA && (dev->flags & ATA_DFLAG_DMADIR) && 1516 dma_dir == DMA_FROM_DEVICE) 1517 qc->tf.feature |= ATAPI_DMADIR; 1518 1519 qc->flags |= ATA_QCFLAG_RESULT_TF; 1520 qc->dma_dir = dma_dir; 1521 if (dma_dir != DMA_NONE) { 1522 unsigned int i, buflen = 0; 1523 struct scatterlist *sg; 1524 1525 for_each_sg(sgl, sg, n_elem, i) 1526 buflen += sg->length; 1527 1528 ata_sg_init(qc, sgl, n_elem); 1529 qc->nbytes = buflen; 1530 } 1531 1532 qc->private_data = &wait; 1533 qc->complete_fn = ata_qc_complete_internal; 1534 1535 ata_qc_issue(qc); 1536 1537 spin_unlock_irqrestore(ap->lock, flags); 1538 1539 if (!timeout) { 1540 if (ata_probe_timeout) 1541 timeout = ata_probe_timeout * 1000; 1542 else { 1543 timeout = ata_internal_cmd_timeout(dev, command); 1544 auto_timeout = 1; 1545 } 1546 } 1547 1548 if (ap->ops->error_handler) 1549 ata_eh_release(ap); 1550 1551 rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout)); 1552 1553 if (ap->ops->error_handler) 1554 ata_eh_acquire(ap); 1555 1556 ata_sff_flush_pio_task(ap); 1557 1558 if (!rc) { 1559 spin_lock_irqsave(ap->lock, flags); 1560 1561 /* We're racing with irq here. If we lose, the 1562 * following test prevents us from completing the qc 1563 * twice. If we win, the port is frozen and will be 1564 * cleaned up by ->post_internal_cmd(). 1565 */ 1566 if (qc->flags & ATA_QCFLAG_ACTIVE) { 1567 qc->err_mask |= AC_ERR_TIMEOUT; 1568 1569 if (ap->ops->error_handler) 1570 ata_port_freeze(ap); 1571 else 1572 ata_qc_complete(qc); 1573 1574 ata_dev_warn(dev, "qc timeout (cmd 0x%x)\n", 1575 command); 1576 } 1577 1578 spin_unlock_irqrestore(ap->lock, flags); 1579 } 1580 1581 /* do post_internal_cmd */ 1582 if (ap->ops->post_internal_cmd) 1583 ap->ops->post_internal_cmd(qc); 1584 1585 /* perform minimal error analysis */ 1586 if (qc->flags & ATA_QCFLAG_FAILED) { 1587 if (qc->result_tf.command & (ATA_ERR | ATA_DF)) 1588 qc->err_mask |= AC_ERR_DEV; 1589 1590 if (!qc->err_mask) 1591 qc->err_mask |= AC_ERR_OTHER; 1592 1593 if (qc->err_mask & ~AC_ERR_OTHER) 1594 qc->err_mask &= ~AC_ERR_OTHER; 1595 } else if (qc->tf.command == ATA_CMD_REQ_SENSE_DATA) { 1596 qc->result_tf.command |= ATA_SENSE; 1597 } 1598 1599 /* finish up */ 1600 spin_lock_irqsave(ap->lock, flags); 1601 1602 *tf = qc->result_tf; 1603 err_mask = qc->err_mask; 1604 1605 ata_qc_free(qc); 1606 link->active_tag = preempted_tag; 1607 link->sactive = preempted_sactive; 1608 ap->qc_active = preempted_qc_active; 1609 ap->nr_active_links = preempted_nr_active_links; 1610 1611 spin_unlock_irqrestore(ap->lock, flags); 1612 1613 if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout) 1614 ata_internal_cmd_timed_out(dev, command); 1615 1616 return err_mask; 1617 } 1618 1619 /** 1620 * ata_exec_internal - execute libata internal command 1621 * @dev: Device to which the command is sent 1622 * @tf: Taskfile registers for the command and the result 1623 * @cdb: CDB for packet command 1624 * @dma_dir: Data transfer direction of the command 1625 * @buf: Data buffer of the command 1626 * @buflen: Length of data buffer 1627 * @timeout: Timeout in msecs (0 for default) 1628 * 1629 * Wrapper around ata_exec_internal_sg() which takes simple 1630 * buffer instead of sg list. 1631 * 1632 * LOCKING: 1633 * None. Should be called with kernel context, might sleep. 1634 * 1635 * RETURNS: 1636 * Zero on success, AC_ERR_* mask on failure 1637 */ 1638 unsigned ata_exec_internal(struct ata_device *dev, 1639 struct ata_taskfile *tf, const u8 *cdb, 1640 int dma_dir, void *buf, unsigned int buflen, 1641 unsigned long timeout) 1642 { 1643 struct scatterlist *psg = NULL, sg; 1644 unsigned int n_elem = 0; 1645 1646 if (dma_dir != DMA_NONE) { 1647 WARN_ON(!buf); 1648 sg_init_one(&sg, buf, buflen); 1649 psg = &sg; 1650 n_elem++; 1651 } 1652 1653 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem, 1654 timeout); 1655 } 1656 1657 /** 1658 * ata_pio_need_iordy - check if iordy needed 1659 * @adev: ATA device 1660 * 1661 * Check if the current speed of the device requires IORDY. Used 1662 * by various controllers for chip configuration. 1663 */ 1664 unsigned int ata_pio_need_iordy(const struct ata_device *adev) 1665 { 1666 /* Don't set IORDY if we're preparing for reset. IORDY may 1667 * lead to controller lock up on certain controllers if the 1668 * port is not occupied. See bko#11703 for details. 1669 */ 1670 if (adev->link->ap->pflags & ATA_PFLAG_RESETTING) 1671 return 0; 1672 /* Controller doesn't support IORDY. Probably a pointless 1673 * check as the caller should know this. 1674 */ 1675 if (adev->link->ap->flags & ATA_FLAG_NO_IORDY) 1676 return 0; 1677 /* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6. */ 1678 if (ata_id_is_cfa(adev->id) 1679 && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6)) 1680 return 0; 1681 /* PIO3 and higher it is mandatory */ 1682 if (adev->pio_mode > XFER_PIO_2) 1683 return 1; 1684 /* We turn it on when possible */ 1685 if (ata_id_has_iordy(adev->id)) 1686 return 1; 1687 return 0; 1688 } 1689 EXPORT_SYMBOL_GPL(ata_pio_need_iordy); 1690 1691 /** 1692 * ata_pio_mask_no_iordy - Return the non IORDY mask 1693 * @adev: ATA device 1694 * 1695 * Compute the highest mode possible if we are not using iordy. Return 1696 * -1 if no iordy mode is available. 1697 */ 1698 static u32 ata_pio_mask_no_iordy(const struct ata_device *adev) 1699 { 1700 /* If we have no drive specific rule, then PIO 2 is non IORDY */ 1701 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */ 1702 u16 pio = adev->id[ATA_ID_EIDE_PIO]; 1703 /* Is the speed faster than the drive allows non IORDY ? */ 1704 if (pio) { 1705 /* This is cycle times not frequency - watch the logic! */ 1706 if (pio > 240) /* PIO2 is 240nS per cycle */ 1707 return 3 << ATA_SHIFT_PIO; 1708 return 7 << ATA_SHIFT_PIO; 1709 } 1710 } 1711 return 3 << ATA_SHIFT_PIO; 1712 } 1713 1714 /** 1715 * ata_do_dev_read_id - default ID read method 1716 * @dev: device 1717 * @tf: proposed taskfile 1718 * @id: data buffer 1719 * 1720 * Issue the identify taskfile and hand back the buffer containing 1721 * identify data. For some RAID controllers and for pre ATA devices 1722 * this function is wrapped or replaced by the driver 1723 */ 1724 unsigned int ata_do_dev_read_id(struct ata_device *dev, 1725 struct ata_taskfile *tf, __le16 *id) 1726 { 1727 return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE, 1728 id, sizeof(id[0]) * ATA_ID_WORDS, 0); 1729 } 1730 EXPORT_SYMBOL_GPL(ata_do_dev_read_id); 1731 1732 /** 1733 * ata_dev_read_id - Read ID data from the specified device 1734 * @dev: target device 1735 * @p_class: pointer to class of the target device (may be changed) 1736 * @flags: ATA_READID_* flags 1737 * @id: buffer to read IDENTIFY data into 1738 * 1739 * Read ID data from the specified device. ATA_CMD_ID_ATA is 1740 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI 1741 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS 1742 * for pre-ATA4 drives. 1743 * 1744 * FIXME: ATA_CMD_ID_ATA is optional for early drives and right 1745 * now we abort if we hit that case. 1746 * 1747 * LOCKING: 1748 * Kernel thread context (may sleep) 1749 * 1750 * RETURNS: 1751 * 0 on success, -errno otherwise. 1752 */ 1753 int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class, 1754 unsigned int flags, u16 *id) 1755 { 1756 struct ata_port *ap = dev->link->ap; 1757 unsigned int class = *p_class; 1758 struct ata_taskfile tf; 1759 unsigned int err_mask = 0; 1760 const char *reason; 1761 bool is_semb = class == ATA_DEV_SEMB; 1762 int may_fallback = 1, tried_spinup = 0; 1763 int rc; 1764 1765 retry: 1766 ata_tf_init(dev, &tf); 1767 1768 switch (class) { 1769 case ATA_DEV_SEMB: 1770 class = ATA_DEV_ATA; /* some hard drives report SEMB sig */ 1771 fallthrough; 1772 case ATA_DEV_ATA: 1773 case ATA_DEV_ZAC: 1774 tf.command = ATA_CMD_ID_ATA; 1775 break; 1776 case ATA_DEV_ATAPI: 1777 tf.command = ATA_CMD_ID_ATAPI; 1778 break; 1779 default: 1780 rc = -ENODEV; 1781 reason = "unsupported class"; 1782 goto err_out; 1783 } 1784 1785 tf.protocol = ATA_PROT_PIO; 1786 1787 /* Some devices choke if TF registers contain garbage. Make 1788 * sure those are properly initialized. 1789 */ 1790 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 1791 1792 /* Device presence detection is unreliable on some 1793 * controllers. Always poll IDENTIFY if available. 1794 */ 1795 tf.flags |= ATA_TFLAG_POLLING; 1796 1797 if (ap->ops->read_id) 1798 err_mask = ap->ops->read_id(dev, &tf, (__le16 *)id); 1799 else 1800 err_mask = ata_do_dev_read_id(dev, &tf, (__le16 *)id); 1801 1802 if (err_mask) { 1803 if (err_mask & AC_ERR_NODEV_HINT) { 1804 ata_dev_dbg(dev, "NODEV after polling detection\n"); 1805 return -ENOENT; 1806 } 1807 1808 if (is_semb) { 1809 ata_dev_info(dev, 1810 "IDENTIFY failed on device w/ SEMB sig, disabled\n"); 1811 /* SEMB is not supported yet */ 1812 *p_class = ATA_DEV_SEMB_UNSUP; 1813 return 0; 1814 } 1815 1816 if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) { 1817 /* Device or controller might have reported 1818 * the wrong device class. Give a shot at the 1819 * other IDENTIFY if the current one is 1820 * aborted by the device. 1821 */ 1822 if (may_fallback) { 1823 may_fallback = 0; 1824 1825 if (class == ATA_DEV_ATA) 1826 class = ATA_DEV_ATAPI; 1827 else 1828 class = ATA_DEV_ATA; 1829 goto retry; 1830 } 1831 1832 /* Control reaches here iff the device aborted 1833 * both flavors of IDENTIFYs which happens 1834 * sometimes with phantom devices. 1835 */ 1836 ata_dev_dbg(dev, 1837 "both IDENTIFYs aborted, assuming NODEV\n"); 1838 return -ENOENT; 1839 } 1840 1841 rc = -EIO; 1842 reason = "I/O error"; 1843 goto err_out; 1844 } 1845 1846 if (dev->horkage & ATA_HORKAGE_DUMP_ID) { 1847 ata_dev_info(dev, "dumping IDENTIFY data, " 1848 "class=%d may_fallback=%d tried_spinup=%d\n", 1849 class, may_fallback, tried_spinup); 1850 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 1851 16, 2, id, ATA_ID_WORDS * sizeof(*id), true); 1852 } 1853 1854 /* Falling back doesn't make sense if ID data was read 1855 * successfully at least once. 1856 */ 1857 may_fallback = 0; 1858 1859 swap_buf_le16(id, ATA_ID_WORDS); 1860 1861 /* sanity check */ 1862 rc = -EINVAL; 1863 reason = "device reports invalid type"; 1864 1865 if (class == ATA_DEV_ATA || class == ATA_DEV_ZAC) { 1866 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id)) 1867 goto err_out; 1868 if (ap->host->flags & ATA_HOST_IGNORE_ATA && 1869 ata_id_is_ata(id)) { 1870 ata_dev_dbg(dev, 1871 "host indicates ignore ATA devices, ignored\n"); 1872 return -ENOENT; 1873 } 1874 } else { 1875 if (ata_id_is_ata(id)) 1876 goto err_out; 1877 } 1878 1879 if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) { 1880 tried_spinup = 1; 1881 /* 1882 * Drive powered-up in standby mode, and requires a specific 1883 * SET_FEATURES spin-up subcommand before it will accept 1884 * anything other than the original IDENTIFY command. 1885 */ 1886 err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0); 1887 if (err_mask && id[2] != 0x738c) { 1888 rc = -EIO; 1889 reason = "SPINUP failed"; 1890 goto err_out; 1891 } 1892 /* 1893 * If the drive initially returned incomplete IDENTIFY info, 1894 * we now must reissue the IDENTIFY command. 1895 */ 1896 if (id[2] == 0x37c8) 1897 goto retry; 1898 } 1899 1900 if ((flags & ATA_READID_POSTRESET) && 1901 (class == ATA_DEV_ATA || class == ATA_DEV_ZAC)) { 1902 /* 1903 * The exact sequence expected by certain pre-ATA4 drives is: 1904 * SRST RESET 1905 * IDENTIFY (optional in early ATA) 1906 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA) 1907 * anything else.. 1908 * Some drives were very specific about that exact sequence. 1909 * 1910 * Note that ATA4 says lba is mandatory so the second check 1911 * should never trigger. 1912 */ 1913 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) { 1914 err_mask = ata_dev_init_params(dev, id[3], id[6]); 1915 if (err_mask) { 1916 rc = -EIO; 1917 reason = "INIT_DEV_PARAMS failed"; 1918 goto err_out; 1919 } 1920 1921 /* current CHS translation info (id[53-58]) might be 1922 * changed. reread the identify device info. 1923 */ 1924 flags &= ~ATA_READID_POSTRESET; 1925 goto retry; 1926 } 1927 } 1928 1929 *p_class = class; 1930 1931 return 0; 1932 1933 err_out: 1934 ata_dev_warn(dev, "failed to IDENTIFY (%s, err_mask=0x%x)\n", 1935 reason, err_mask); 1936 return rc; 1937 } 1938 1939 /** 1940 * ata_read_log_page - read a specific log page 1941 * @dev: target device 1942 * @log: log to read 1943 * @page: page to read 1944 * @buf: buffer to store read page 1945 * @sectors: number of sectors to read 1946 * 1947 * Read log page using READ_LOG_EXT command. 1948 * 1949 * LOCKING: 1950 * Kernel thread context (may sleep). 1951 * 1952 * RETURNS: 1953 * 0 on success, AC_ERR_* mask otherwise. 1954 */ 1955 unsigned int ata_read_log_page(struct ata_device *dev, u8 log, 1956 u8 page, void *buf, unsigned int sectors) 1957 { 1958 unsigned long ap_flags = dev->link->ap->flags; 1959 struct ata_taskfile tf; 1960 unsigned int err_mask; 1961 bool dma = false; 1962 1963 ata_dev_dbg(dev, "read log page - log 0x%x, page 0x%x\n", log, page); 1964 1965 /* 1966 * Return error without actually issuing the command on controllers 1967 * which e.g. lockup on a read log page. 1968 */ 1969 if (ap_flags & ATA_FLAG_NO_LOG_PAGE) 1970 return AC_ERR_DEV; 1971 1972 retry: 1973 ata_tf_init(dev, &tf); 1974 if (ata_dma_enabled(dev) && ata_id_has_read_log_dma_ext(dev->id) && 1975 !(dev->horkage & ATA_HORKAGE_NO_DMA_LOG)) { 1976 tf.command = ATA_CMD_READ_LOG_DMA_EXT; 1977 tf.protocol = ATA_PROT_DMA; 1978 dma = true; 1979 } else { 1980 tf.command = ATA_CMD_READ_LOG_EXT; 1981 tf.protocol = ATA_PROT_PIO; 1982 dma = false; 1983 } 1984 tf.lbal = log; 1985 tf.lbam = page; 1986 tf.nsect = sectors; 1987 tf.hob_nsect = sectors >> 8; 1988 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE; 1989 1990 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE, 1991 buf, sectors * ATA_SECT_SIZE, 0); 1992 1993 if (err_mask) { 1994 if (dma) { 1995 dev->horkage |= ATA_HORKAGE_NO_DMA_LOG; 1996 goto retry; 1997 } 1998 ata_dev_err(dev, 1999 "Read log 0x%02x page 0x%02x failed, Emask 0x%x\n", 2000 (unsigned int)log, (unsigned int)page, err_mask); 2001 } 2002 2003 return err_mask; 2004 } 2005 2006 static bool ata_log_supported(struct ata_device *dev, u8 log) 2007 { 2008 struct ata_port *ap = dev->link->ap; 2009 2010 if (ata_read_log_page(dev, ATA_LOG_DIRECTORY, 0, ap->sector_buf, 1)) 2011 return false; 2012 return get_unaligned_le16(&ap->sector_buf[log * 2]) ? true : false; 2013 } 2014 2015 static bool ata_identify_page_supported(struct ata_device *dev, u8 page) 2016 { 2017 struct ata_port *ap = dev->link->ap; 2018 unsigned int err, i; 2019 2020 if (dev->horkage & ATA_HORKAGE_NO_ID_DEV_LOG) 2021 return false; 2022 2023 if (!ata_log_supported(dev, ATA_LOG_IDENTIFY_DEVICE)) { 2024 /* 2025 * IDENTIFY DEVICE data log is defined as mandatory starting 2026 * with ACS-3 (ATA version 10). Warn about the missing log 2027 * for drives which implement this ATA level or above. 2028 */ 2029 if (ata_id_major_version(dev->id) >= 10) 2030 ata_dev_warn(dev, 2031 "ATA Identify Device Log not supported\n"); 2032 dev->horkage |= ATA_HORKAGE_NO_ID_DEV_LOG; 2033 return false; 2034 } 2035 2036 /* 2037 * Read IDENTIFY DEVICE data log, page 0, to figure out if the page is 2038 * supported. 2039 */ 2040 err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, 0, ap->sector_buf, 2041 1); 2042 if (err) 2043 return false; 2044 2045 for (i = 0; i < ap->sector_buf[8]; i++) { 2046 if (ap->sector_buf[9 + i] == page) 2047 return true; 2048 } 2049 2050 return false; 2051 } 2052 2053 static int ata_do_link_spd_horkage(struct ata_device *dev) 2054 { 2055 struct ata_link *plink = ata_dev_phys_link(dev); 2056 u32 target, target_limit; 2057 2058 if (!sata_scr_valid(plink)) 2059 return 0; 2060 2061 if (dev->horkage & ATA_HORKAGE_1_5_GBPS) 2062 target = 1; 2063 else 2064 return 0; 2065 2066 target_limit = (1 << target) - 1; 2067 2068 /* if already on stricter limit, no need to push further */ 2069 if (plink->sata_spd_limit <= target_limit) 2070 return 0; 2071 2072 plink->sata_spd_limit = target_limit; 2073 2074 /* Request another EH round by returning -EAGAIN if link is 2075 * going faster than the target speed. Forward progress is 2076 * guaranteed by setting sata_spd_limit to target_limit above. 2077 */ 2078 if (plink->sata_spd > target) { 2079 ata_dev_info(dev, "applying link speed limit horkage to %s\n", 2080 sata_spd_string(target)); 2081 return -EAGAIN; 2082 } 2083 return 0; 2084 } 2085 2086 static inline u8 ata_dev_knobble(struct ata_device *dev) 2087 { 2088 struct ata_port *ap = dev->link->ap; 2089 2090 if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK) 2091 return 0; 2092 2093 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id))); 2094 } 2095 2096 static void ata_dev_config_ncq_send_recv(struct ata_device *dev) 2097 { 2098 struct ata_port *ap = dev->link->ap; 2099 unsigned int err_mask; 2100 2101 if (!ata_log_supported(dev, ATA_LOG_NCQ_SEND_RECV)) { 2102 ata_dev_warn(dev, "NCQ Send/Recv Log not supported\n"); 2103 return; 2104 } 2105 err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_SEND_RECV, 2106 0, ap->sector_buf, 1); 2107 if (!err_mask) { 2108 u8 *cmds = dev->ncq_send_recv_cmds; 2109 2110 dev->flags |= ATA_DFLAG_NCQ_SEND_RECV; 2111 memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_SEND_RECV_SIZE); 2112 2113 if (dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM) { 2114 ata_dev_dbg(dev, "disabling queued TRIM support\n"); 2115 cmds[ATA_LOG_NCQ_SEND_RECV_DSM_OFFSET] &= 2116 ~ATA_LOG_NCQ_SEND_RECV_DSM_TRIM; 2117 } 2118 } 2119 } 2120 2121 static void ata_dev_config_ncq_non_data(struct ata_device *dev) 2122 { 2123 struct ata_port *ap = dev->link->ap; 2124 unsigned int err_mask; 2125 2126 if (!ata_log_supported(dev, ATA_LOG_NCQ_NON_DATA)) { 2127 ata_dev_warn(dev, 2128 "NCQ Send/Recv Log not supported\n"); 2129 return; 2130 } 2131 err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_NON_DATA, 2132 0, ap->sector_buf, 1); 2133 if (!err_mask) { 2134 u8 *cmds = dev->ncq_non_data_cmds; 2135 2136 memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_NON_DATA_SIZE); 2137 } 2138 } 2139 2140 static void ata_dev_config_ncq_prio(struct ata_device *dev) 2141 { 2142 struct ata_port *ap = dev->link->ap; 2143 unsigned int err_mask; 2144 2145 if (!ata_identify_page_supported(dev, ATA_LOG_SATA_SETTINGS)) 2146 return; 2147 2148 err_mask = ata_read_log_page(dev, 2149 ATA_LOG_IDENTIFY_DEVICE, 2150 ATA_LOG_SATA_SETTINGS, 2151 ap->sector_buf, 2152 1); 2153 if (err_mask) 2154 goto not_supported; 2155 2156 if (!(ap->sector_buf[ATA_LOG_NCQ_PRIO_OFFSET] & BIT(3))) 2157 goto not_supported; 2158 2159 dev->flags |= ATA_DFLAG_NCQ_PRIO; 2160 2161 return; 2162 2163 not_supported: 2164 dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE; 2165 dev->flags &= ~ATA_DFLAG_NCQ_PRIO; 2166 } 2167 2168 static bool ata_dev_check_adapter(struct ata_device *dev, 2169 unsigned short vendor_id) 2170 { 2171 struct pci_dev *pcidev = NULL; 2172 struct device *parent_dev = NULL; 2173 2174 for (parent_dev = dev->tdev.parent; parent_dev != NULL; 2175 parent_dev = parent_dev->parent) { 2176 if (dev_is_pci(parent_dev)) { 2177 pcidev = to_pci_dev(parent_dev); 2178 if (pcidev->vendor == vendor_id) 2179 return true; 2180 break; 2181 } 2182 } 2183 2184 return false; 2185 } 2186 2187 static int ata_dev_config_ncq(struct ata_device *dev, 2188 char *desc, size_t desc_sz) 2189 { 2190 struct ata_port *ap = dev->link->ap; 2191 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id); 2192 unsigned int err_mask; 2193 char *aa_desc = ""; 2194 2195 if (!ata_id_has_ncq(dev->id)) { 2196 desc[0] = '\0'; 2197 return 0; 2198 } 2199 if (!IS_ENABLED(CONFIG_SATA_HOST)) 2200 return 0; 2201 if (dev->horkage & ATA_HORKAGE_NONCQ) { 2202 snprintf(desc, desc_sz, "NCQ (not used)"); 2203 return 0; 2204 } 2205 2206 if (dev->horkage & ATA_HORKAGE_NO_NCQ_ON_ATI && 2207 ata_dev_check_adapter(dev, PCI_VENDOR_ID_ATI)) { 2208 snprintf(desc, desc_sz, "NCQ (not used)"); 2209 return 0; 2210 } 2211 2212 if (ap->flags & ATA_FLAG_NCQ) { 2213 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE); 2214 dev->flags |= ATA_DFLAG_NCQ; 2215 } 2216 2217 if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) && 2218 (ap->flags & ATA_FLAG_FPDMA_AA) && 2219 ata_id_has_fpdma_aa(dev->id)) { 2220 err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE, 2221 SATA_FPDMA_AA); 2222 if (err_mask) { 2223 ata_dev_err(dev, 2224 "failed to enable AA (error_mask=0x%x)\n", 2225 err_mask); 2226 if (err_mask != AC_ERR_DEV) { 2227 dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA; 2228 return -EIO; 2229 } 2230 } else 2231 aa_desc = ", AA"; 2232 } 2233 2234 if (hdepth >= ddepth) 2235 snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc); 2236 else 2237 snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth, 2238 ddepth, aa_desc); 2239 2240 if ((ap->flags & ATA_FLAG_FPDMA_AUX)) { 2241 if (ata_id_has_ncq_send_and_recv(dev->id)) 2242 ata_dev_config_ncq_send_recv(dev); 2243 if (ata_id_has_ncq_non_data(dev->id)) 2244 ata_dev_config_ncq_non_data(dev); 2245 if (ata_id_has_ncq_prio(dev->id)) 2246 ata_dev_config_ncq_prio(dev); 2247 } 2248 2249 return 0; 2250 } 2251 2252 static void ata_dev_config_sense_reporting(struct ata_device *dev) 2253 { 2254 unsigned int err_mask; 2255 2256 if (!ata_id_has_sense_reporting(dev->id)) 2257 return; 2258 2259 if (ata_id_sense_reporting_enabled(dev->id)) 2260 return; 2261 2262 err_mask = ata_dev_set_feature(dev, SETFEATURE_SENSE_DATA, 0x1); 2263 if (err_mask) { 2264 ata_dev_dbg(dev, 2265 "failed to enable Sense Data Reporting, Emask 0x%x\n", 2266 err_mask); 2267 } 2268 } 2269 2270 static void ata_dev_config_zac(struct ata_device *dev) 2271 { 2272 struct ata_port *ap = dev->link->ap; 2273 unsigned int err_mask; 2274 u8 *identify_buf = ap->sector_buf; 2275 2276 dev->zac_zones_optimal_open = U32_MAX; 2277 dev->zac_zones_optimal_nonseq = U32_MAX; 2278 dev->zac_zones_max_open = U32_MAX; 2279 2280 /* 2281 * Always set the 'ZAC' flag for Host-managed devices. 2282 */ 2283 if (dev->class == ATA_DEV_ZAC) 2284 dev->flags |= ATA_DFLAG_ZAC; 2285 else if (ata_id_zoned_cap(dev->id) == 0x01) 2286 /* 2287 * Check for host-aware devices. 2288 */ 2289 dev->flags |= ATA_DFLAG_ZAC; 2290 2291 if (!(dev->flags & ATA_DFLAG_ZAC)) 2292 return; 2293 2294 if (!ata_identify_page_supported(dev, ATA_LOG_ZONED_INFORMATION)) { 2295 ata_dev_warn(dev, 2296 "ATA Zoned Information Log not supported\n"); 2297 return; 2298 } 2299 2300 /* 2301 * Read IDENTIFY DEVICE data log, page 9 (Zoned-device information) 2302 */ 2303 err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, 2304 ATA_LOG_ZONED_INFORMATION, 2305 identify_buf, 1); 2306 if (!err_mask) { 2307 u64 zoned_cap, opt_open, opt_nonseq, max_open; 2308 2309 zoned_cap = get_unaligned_le64(&identify_buf[8]); 2310 if ((zoned_cap >> 63)) 2311 dev->zac_zoned_cap = (zoned_cap & 1); 2312 opt_open = get_unaligned_le64(&identify_buf[24]); 2313 if ((opt_open >> 63)) 2314 dev->zac_zones_optimal_open = (u32)opt_open; 2315 opt_nonseq = get_unaligned_le64(&identify_buf[32]); 2316 if ((opt_nonseq >> 63)) 2317 dev->zac_zones_optimal_nonseq = (u32)opt_nonseq; 2318 max_open = get_unaligned_le64(&identify_buf[40]); 2319 if ((max_open >> 63)) 2320 dev->zac_zones_max_open = (u32)max_open; 2321 } 2322 } 2323 2324 static void ata_dev_config_trusted(struct ata_device *dev) 2325 { 2326 struct ata_port *ap = dev->link->ap; 2327 u64 trusted_cap; 2328 unsigned int err; 2329 2330 if (!ata_id_has_trusted(dev->id)) 2331 return; 2332 2333 if (!ata_identify_page_supported(dev, ATA_LOG_SECURITY)) { 2334 ata_dev_warn(dev, 2335 "Security Log not supported\n"); 2336 return; 2337 } 2338 2339 err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, ATA_LOG_SECURITY, 2340 ap->sector_buf, 1); 2341 if (err) 2342 return; 2343 2344 trusted_cap = get_unaligned_le64(&ap->sector_buf[40]); 2345 if (!(trusted_cap & (1ULL << 63))) { 2346 ata_dev_dbg(dev, 2347 "Trusted Computing capability qword not valid!\n"); 2348 return; 2349 } 2350 2351 if (trusted_cap & (1 << 0)) 2352 dev->flags |= ATA_DFLAG_TRUSTED; 2353 } 2354 2355 static int ata_dev_config_lba(struct ata_device *dev) 2356 { 2357 const u16 *id = dev->id; 2358 const char *lba_desc; 2359 char ncq_desc[24]; 2360 int ret; 2361 2362 dev->flags |= ATA_DFLAG_LBA; 2363 2364 if (ata_id_has_lba48(id)) { 2365 lba_desc = "LBA48"; 2366 dev->flags |= ATA_DFLAG_LBA48; 2367 if (dev->n_sectors >= (1UL << 28) && 2368 ata_id_has_flush_ext(id)) 2369 dev->flags |= ATA_DFLAG_FLUSH_EXT; 2370 } else { 2371 lba_desc = "LBA"; 2372 } 2373 2374 /* config NCQ */ 2375 ret = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc)); 2376 2377 /* print device info to dmesg */ 2378 if (ata_dev_print_info(dev)) 2379 ata_dev_info(dev, 2380 "%llu sectors, multi %u: %s %s\n", 2381 (unsigned long long)dev->n_sectors, 2382 dev->multi_count, lba_desc, ncq_desc); 2383 2384 return ret; 2385 } 2386 2387 static void ata_dev_config_chs(struct ata_device *dev) 2388 { 2389 const u16 *id = dev->id; 2390 2391 if (ata_id_current_chs_valid(id)) { 2392 /* Current CHS translation is valid. */ 2393 dev->cylinders = id[54]; 2394 dev->heads = id[55]; 2395 dev->sectors = id[56]; 2396 } else { 2397 /* Default translation */ 2398 dev->cylinders = id[1]; 2399 dev->heads = id[3]; 2400 dev->sectors = id[6]; 2401 } 2402 2403 /* print device info to dmesg */ 2404 if (ata_dev_print_info(dev)) 2405 ata_dev_info(dev, 2406 "%llu sectors, multi %u, CHS %u/%u/%u\n", 2407 (unsigned long long)dev->n_sectors, 2408 dev->multi_count, dev->cylinders, 2409 dev->heads, dev->sectors); 2410 } 2411 2412 static void ata_dev_config_devslp(struct ata_device *dev) 2413 { 2414 u8 *sata_setting = dev->link->ap->sector_buf; 2415 unsigned int err_mask; 2416 int i, j; 2417 2418 /* 2419 * Check device sleep capability. Get DevSlp timing variables 2420 * from SATA Settings page of Identify Device Data Log. 2421 */ 2422 if (!ata_id_has_devslp(dev->id) || 2423 !ata_identify_page_supported(dev, ATA_LOG_SATA_SETTINGS)) 2424 return; 2425 2426 err_mask = ata_read_log_page(dev, 2427 ATA_LOG_IDENTIFY_DEVICE, 2428 ATA_LOG_SATA_SETTINGS, 2429 sata_setting, 1); 2430 if (err_mask) 2431 return; 2432 2433 dev->flags |= ATA_DFLAG_DEVSLP; 2434 for (i = 0; i < ATA_LOG_DEVSLP_SIZE; i++) { 2435 j = ATA_LOG_DEVSLP_OFFSET + i; 2436 dev->devslp_timing[i] = sata_setting[j]; 2437 } 2438 } 2439 2440 static void ata_dev_config_cpr(struct ata_device *dev) 2441 { 2442 unsigned int err_mask; 2443 size_t buf_len; 2444 int i, nr_cpr = 0; 2445 struct ata_cpr_log *cpr_log = NULL; 2446 u8 *desc, *buf = NULL; 2447 2448 if (!ata_identify_page_supported(dev, 2449 ATA_LOG_CONCURRENT_POSITIONING_RANGES)) 2450 goto out; 2451 2452 /* 2453 * Read IDENTIFY DEVICE data log, page 0x47 2454 * (concurrent positioning ranges). We can have at most 255 32B range 2455 * descriptors plus a 64B header. 2456 */ 2457 buf_len = (64 + 255 * 32 + 511) & ~511; 2458 buf = kzalloc(buf_len, GFP_KERNEL); 2459 if (!buf) 2460 goto out; 2461 2462 err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, 2463 ATA_LOG_CONCURRENT_POSITIONING_RANGES, 2464 buf, buf_len >> 9); 2465 if (err_mask) 2466 goto out; 2467 2468 nr_cpr = buf[0]; 2469 if (!nr_cpr) 2470 goto out; 2471 2472 cpr_log = kzalloc(struct_size(cpr_log, cpr, nr_cpr), GFP_KERNEL); 2473 if (!cpr_log) 2474 goto out; 2475 2476 cpr_log->nr_cpr = nr_cpr; 2477 desc = &buf[64]; 2478 for (i = 0; i < nr_cpr; i++, desc += 32) { 2479 cpr_log->cpr[i].num = desc[0]; 2480 cpr_log->cpr[i].num_storage_elements = desc[1]; 2481 cpr_log->cpr[i].start_lba = get_unaligned_le64(&desc[8]); 2482 cpr_log->cpr[i].num_lbas = get_unaligned_le64(&desc[16]); 2483 } 2484 2485 out: 2486 swap(dev->cpr_log, cpr_log); 2487 kfree(cpr_log); 2488 kfree(buf); 2489 } 2490 2491 static void ata_dev_print_features(struct ata_device *dev) 2492 { 2493 if (!(dev->flags & ATA_DFLAG_FEATURES_MASK)) 2494 return; 2495 2496 ata_dev_info(dev, 2497 "Features:%s%s%s%s%s%s\n", 2498 dev->flags & ATA_DFLAG_TRUSTED ? " Trust" : "", 2499 dev->flags & ATA_DFLAG_DA ? " Dev-Attention" : "", 2500 dev->flags & ATA_DFLAG_DEVSLP ? " Dev-Sleep" : "", 2501 dev->flags & ATA_DFLAG_NCQ_SEND_RECV ? " NCQ-sndrcv" : "", 2502 dev->flags & ATA_DFLAG_NCQ_PRIO ? " NCQ-prio" : "", 2503 dev->cpr_log ? " CPR" : ""); 2504 } 2505 2506 /** 2507 * ata_dev_configure - Configure the specified ATA/ATAPI device 2508 * @dev: Target device to configure 2509 * 2510 * Configure @dev according to @dev->id. Generic and low-level 2511 * driver specific fixups are also applied. 2512 * 2513 * LOCKING: 2514 * Kernel thread context (may sleep) 2515 * 2516 * RETURNS: 2517 * 0 on success, -errno otherwise 2518 */ 2519 int ata_dev_configure(struct ata_device *dev) 2520 { 2521 struct ata_port *ap = dev->link->ap; 2522 bool print_info = ata_dev_print_info(dev); 2523 const u16 *id = dev->id; 2524 unsigned long xfer_mask; 2525 unsigned int err_mask; 2526 char revbuf[7]; /* XYZ-99\0 */ 2527 char fwrevbuf[ATA_ID_FW_REV_LEN+1]; 2528 char modelbuf[ATA_ID_PROD_LEN+1]; 2529 int rc; 2530 2531 if (!ata_dev_enabled(dev)) { 2532 ata_dev_dbg(dev, "no device\n"); 2533 return 0; 2534 } 2535 2536 /* set horkage */ 2537 dev->horkage |= ata_dev_blacklisted(dev); 2538 ata_force_horkage(dev); 2539 2540 if (dev->horkage & ATA_HORKAGE_DISABLE) { 2541 ata_dev_info(dev, "unsupported device, disabling\n"); 2542 ata_dev_disable(dev); 2543 return 0; 2544 } 2545 2546 if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) && 2547 dev->class == ATA_DEV_ATAPI) { 2548 ata_dev_warn(dev, "WARNING: ATAPI is %s, device ignored\n", 2549 atapi_enabled ? "not supported with this driver" 2550 : "disabled"); 2551 ata_dev_disable(dev); 2552 return 0; 2553 } 2554 2555 rc = ata_do_link_spd_horkage(dev); 2556 if (rc) 2557 return rc; 2558 2559 /* some WD SATA-1 drives have issues with LPM, turn on NOLPM for them */ 2560 if ((dev->horkage & ATA_HORKAGE_WD_BROKEN_LPM) && 2561 (id[ATA_ID_SATA_CAPABILITY] & 0xe) == 0x2) 2562 dev->horkage |= ATA_HORKAGE_NOLPM; 2563 2564 if (ap->flags & ATA_FLAG_NO_LPM) 2565 dev->horkage |= ATA_HORKAGE_NOLPM; 2566 2567 if (dev->horkage & ATA_HORKAGE_NOLPM) { 2568 ata_dev_warn(dev, "LPM support broken, forcing max_power\n"); 2569 dev->link->ap->target_lpm_policy = ATA_LPM_MAX_POWER; 2570 } 2571 2572 /* let ACPI work its magic */ 2573 rc = ata_acpi_on_devcfg(dev); 2574 if (rc) 2575 return rc; 2576 2577 /* massage HPA, do it early as it might change IDENTIFY data */ 2578 rc = ata_hpa_resize(dev); 2579 if (rc) 2580 return rc; 2581 2582 /* print device capabilities */ 2583 ata_dev_dbg(dev, 2584 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x " 2585 "85:%04x 86:%04x 87:%04x 88:%04x\n", 2586 __func__, 2587 id[49], id[82], id[83], id[84], 2588 id[85], id[86], id[87], id[88]); 2589 2590 /* initialize to-be-configured parameters */ 2591 dev->flags &= ~ATA_DFLAG_CFG_MASK; 2592 dev->max_sectors = 0; 2593 dev->cdb_len = 0; 2594 dev->n_sectors = 0; 2595 dev->cylinders = 0; 2596 dev->heads = 0; 2597 dev->sectors = 0; 2598 dev->multi_count = 0; 2599 2600 /* 2601 * common ATA, ATAPI feature tests 2602 */ 2603 2604 /* find max transfer mode; for printk only */ 2605 xfer_mask = ata_id_xfermask(id); 2606 2607 ata_dump_id(dev, id); 2608 2609 /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */ 2610 ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV, 2611 sizeof(fwrevbuf)); 2612 2613 ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD, 2614 sizeof(modelbuf)); 2615 2616 /* ATA-specific feature tests */ 2617 if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) { 2618 if (ata_id_is_cfa(id)) { 2619 /* CPRM may make this media unusable */ 2620 if (id[ATA_ID_CFA_KEY_MGMT] & 1) 2621 ata_dev_warn(dev, 2622 "supports DRM functions and may not be fully accessible\n"); 2623 snprintf(revbuf, 7, "CFA"); 2624 } else { 2625 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id)); 2626 /* Warn the user if the device has TPM extensions */ 2627 if (ata_id_has_tpm(id)) 2628 ata_dev_warn(dev, 2629 "supports DRM functions and may not be fully accessible\n"); 2630 } 2631 2632 dev->n_sectors = ata_id_n_sectors(id); 2633 2634 /* get current R/W Multiple count setting */ 2635 if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) { 2636 unsigned int max = dev->id[47] & 0xff; 2637 unsigned int cnt = dev->id[59] & 0xff; 2638 /* only recognize/allow powers of two here */ 2639 if (is_power_of_2(max) && is_power_of_2(cnt)) 2640 if (cnt <= max) 2641 dev->multi_count = cnt; 2642 } 2643 2644 /* print device info to dmesg */ 2645 if (print_info) 2646 ata_dev_info(dev, "%s: %s, %s, max %s\n", 2647 revbuf, modelbuf, fwrevbuf, 2648 ata_mode_string(xfer_mask)); 2649 2650 if (ata_id_has_lba(id)) { 2651 rc = ata_dev_config_lba(dev); 2652 if (rc) 2653 return rc; 2654 } else { 2655 ata_dev_config_chs(dev); 2656 } 2657 2658 ata_dev_config_devslp(dev); 2659 ata_dev_config_sense_reporting(dev); 2660 ata_dev_config_zac(dev); 2661 ata_dev_config_trusted(dev); 2662 ata_dev_config_cpr(dev); 2663 dev->cdb_len = 32; 2664 2665 if (print_info) 2666 ata_dev_print_features(dev); 2667 } 2668 2669 /* ATAPI-specific feature tests */ 2670 else if (dev->class == ATA_DEV_ATAPI) { 2671 const char *cdb_intr_string = ""; 2672 const char *atapi_an_string = ""; 2673 const char *dma_dir_string = ""; 2674 u32 sntf; 2675 2676 rc = atapi_cdb_len(id); 2677 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) { 2678 ata_dev_warn(dev, "unsupported CDB len %d\n", rc); 2679 rc = -EINVAL; 2680 goto err_out_nosup; 2681 } 2682 dev->cdb_len = (unsigned int) rc; 2683 2684 /* Enable ATAPI AN if both the host and device have 2685 * the support. If PMP is attached, SNTF is required 2686 * to enable ATAPI AN to discern between PHY status 2687 * changed notifications and ATAPI ANs. 2688 */ 2689 if (atapi_an && 2690 (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) && 2691 (!sata_pmp_attached(ap) || 2692 sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) { 2693 /* issue SET feature command to turn this on */ 2694 err_mask = ata_dev_set_feature(dev, 2695 SETFEATURES_SATA_ENABLE, SATA_AN); 2696 if (err_mask) 2697 ata_dev_err(dev, 2698 "failed to enable ATAPI AN (err_mask=0x%x)\n", 2699 err_mask); 2700 else { 2701 dev->flags |= ATA_DFLAG_AN; 2702 atapi_an_string = ", ATAPI AN"; 2703 } 2704 } 2705 2706 if (ata_id_cdb_intr(dev->id)) { 2707 dev->flags |= ATA_DFLAG_CDB_INTR; 2708 cdb_intr_string = ", CDB intr"; 2709 } 2710 2711 if (atapi_dmadir || (dev->horkage & ATA_HORKAGE_ATAPI_DMADIR) || atapi_id_dmadir(dev->id)) { 2712 dev->flags |= ATA_DFLAG_DMADIR; 2713 dma_dir_string = ", DMADIR"; 2714 } 2715 2716 if (ata_id_has_da(dev->id)) { 2717 dev->flags |= ATA_DFLAG_DA; 2718 zpodd_init(dev); 2719 } 2720 2721 /* print device info to dmesg */ 2722 if (print_info) 2723 ata_dev_info(dev, 2724 "ATAPI: %s, %s, max %s%s%s%s\n", 2725 modelbuf, fwrevbuf, 2726 ata_mode_string(xfer_mask), 2727 cdb_intr_string, atapi_an_string, 2728 dma_dir_string); 2729 } 2730 2731 /* determine max_sectors */ 2732 dev->max_sectors = ATA_MAX_SECTORS; 2733 if (dev->flags & ATA_DFLAG_LBA48) 2734 dev->max_sectors = ATA_MAX_SECTORS_LBA48; 2735 2736 /* Limit PATA drive on SATA cable bridge transfers to udma5, 2737 200 sectors */ 2738 if (ata_dev_knobble(dev)) { 2739 if (print_info) 2740 ata_dev_info(dev, "applying bridge limits\n"); 2741 dev->udma_mask &= ATA_UDMA5; 2742 dev->max_sectors = ATA_MAX_SECTORS; 2743 } 2744 2745 if ((dev->class == ATA_DEV_ATAPI) && 2746 (atapi_command_packet_set(id) == TYPE_TAPE)) { 2747 dev->max_sectors = ATA_MAX_SECTORS_TAPE; 2748 dev->horkage |= ATA_HORKAGE_STUCK_ERR; 2749 } 2750 2751 if (dev->horkage & ATA_HORKAGE_MAX_SEC_128) 2752 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128, 2753 dev->max_sectors); 2754 2755 if (dev->horkage & ATA_HORKAGE_MAX_SEC_1024) 2756 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_1024, 2757 dev->max_sectors); 2758 2759 if (dev->horkage & ATA_HORKAGE_MAX_SEC_LBA48) 2760 dev->max_sectors = ATA_MAX_SECTORS_LBA48; 2761 2762 if (ap->ops->dev_config) 2763 ap->ops->dev_config(dev); 2764 2765 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) { 2766 /* Let the user know. We don't want to disallow opens for 2767 rescue purposes, or in case the vendor is just a blithering 2768 idiot. Do this after the dev_config call as some controllers 2769 with buggy firmware may want to avoid reporting false device 2770 bugs */ 2771 2772 if (print_info) { 2773 ata_dev_warn(dev, 2774 "Drive reports diagnostics failure. This may indicate a drive\n"); 2775 ata_dev_warn(dev, 2776 "fault or invalid emulation. Contact drive vendor for information.\n"); 2777 } 2778 } 2779 2780 if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) { 2781 ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n"); 2782 ata_dev_warn(dev, " contact the vendor or visit http://ata.wiki.kernel.org\n"); 2783 } 2784 2785 return 0; 2786 2787 err_out_nosup: 2788 return rc; 2789 } 2790 2791 /** 2792 * ata_cable_40wire - return 40 wire cable type 2793 * @ap: port 2794 * 2795 * Helper method for drivers which want to hardwire 40 wire cable 2796 * detection. 2797 */ 2798 2799 int ata_cable_40wire(struct ata_port *ap) 2800 { 2801 return ATA_CBL_PATA40; 2802 } 2803 EXPORT_SYMBOL_GPL(ata_cable_40wire); 2804 2805 /** 2806 * ata_cable_80wire - return 80 wire cable type 2807 * @ap: port 2808 * 2809 * Helper method for drivers which want to hardwire 80 wire cable 2810 * detection. 2811 */ 2812 2813 int ata_cable_80wire(struct ata_port *ap) 2814 { 2815 return ATA_CBL_PATA80; 2816 } 2817 EXPORT_SYMBOL_GPL(ata_cable_80wire); 2818 2819 /** 2820 * ata_cable_unknown - return unknown PATA cable. 2821 * @ap: port 2822 * 2823 * Helper method for drivers which have no PATA cable detection. 2824 */ 2825 2826 int ata_cable_unknown(struct ata_port *ap) 2827 { 2828 return ATA_CBL_PATA_UNK; 2829 } 2830 EXPORT_SYMBOL_GPL(ata_cable_unknown); 2831 2832 /** 2833 * ata_cable_ignore - return ignored PATA cable. 2834 * @ap: port 2835 * 2836 * Helper method for drivers which don't use cable type to limit 2837 * transfer mode. 2838 */ 2839 int ata_cable_ignore(struct ata_port *ap) 2840 { 2841 return ATA_CBL_PATA_IGN; 2842 } 2843 EXPORT_SYMBOL_GPL(ata_cable_ignore); 2844 2845 /** 2846 * ata_cable_sata - return SATA cable type 2847 * @ap: port 2848 * 2849 * Helper method for drivers which have SATA cables 2850 */ 2851 2852 int ata_cable_sata(struct ata_port *ap) 2853 { 2854 return ATA_CBL_SATA; 2855 } 2856 EXPORT_SYMBOL_GPL(ata_cable_sata); 2857 2858 /** 2859 * ata_bus_probe - Reset and probe ATA bus 2860 * @ap: Bus to probe 2861 * 2862 * Master ATA bus probing function. Initiates a hardware-dependent 2863 * bus reset, then attempts to identify any devices found on 2864 * the bus. 2865 * 2866 * LOCKING: 2867 * PCI/etc. bus probe sem. 2868 * 2869 * RETURNS: 2870 * Zero on success, negative errno otherwise. 2871 */ 2872 2873 int ata_bus_probe(struct ata_port *ap) 2874 { 2875 unsigned int classes[ATA_MAX_DEVICES]; 2876 int tries[ATA_MAX_DEVICES]; 2877 int rc; 2878 struct ata_device *dev; 2879 2880 ata_for_each_dev(dev, &ap->link, ALL) 2881 tries[dev->devno] = ATA_PROBE_MAX_TRIES; 2882 2883 retry: 2884 ata_for_each_dev(dev, &ap->link, ALL) { 2885 /* If we issue an SRST then an ATA drive (not ATAPI) 2886 * may change configuration and be in PIO0 timing. If 2887 * we do a hard reset (or are coming from power on) 2888 * this is true for ATA or ATAPI. Until we've set a 2889 * suitable controller mode we should not touch the 2890 * bus as we may be talking too fast. 2891 */ 2892 dev->pio_mode = XFER_PIO_0; 2893 dev->dma_mode = 0xff; 2894 2895 /* If the controller has a pio mode setup function 2896 * then use it to set the chipset to rights. Don't 2897 * touch the DMA setup as that will be dealt with when 2898 * configuring devices. 2899 */ 2900 if (ap->ops->set_piomode) 2901 ap->ops->set_piomode(ap, dev); 2902 } 2903 2904 /* reset and determine device classes */ 2905 ap->ops->phy_reset(ap); 2906 2907 ata_for_each_dev(dev, &ap->link, ALL) { 2908 if (dev->class != ATA_DEV_UNKNOWN) 2909 classes[dev->devno] = dev->class; 2910 else 2911 classes[dev->devno] = ATA_DEV_NONE; 2912 2913 dev->class = ATA_DEV_UNKNOWN; 2914 } 2915 2916 /* read IDENTIFY page and configure devices. We have to do the identify 2917 specific sequence bass-ackwards so that PDIAG- is released by 2918 the slave device */ 2919 2920 ata_for_each_dev(dev, &ap->link, ALL_REVERSE) { 2921 if (tries[dev->devno]) 2922 dev->class = classes[dev->devno]; 2923 2924 if (!ata_dev_enabled(dev)) 2925 continue; 2926 2927 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET, 2928 dev->id); 2929 if (rc) 2930 goto fail; 2931 } 2932 2933 /* Now ask for the cable type as PDIAG- should have been released */ 2934 if (ap->ops->cable_detect) 2935 ap->cbl = ap->ops->cable_detect(ap); 2936 2937 /* We may have SATA bridge glue hiding here irrespective of 2938 * the reported cable types and sensed types. When SATA 2939 * drives indicate we have a bridge, we don't know which end 2940 * of the link the bridge is which is a problem. 2941 */ 2942 ata_for_each_dev(dev, &ap->link, ENABLED) 2943 if (ata_id_is_sata(dev->id)) 2944 ap->cbl = ATA_CBL_SATA; 2945 2946 /* After the identify sequence we can now set up the devices. We do 2947 this in the normal order so that the user doesn't get confused */ 2948 2949 ata_for_each_dev(dev, &ap->link, ENABLED) { 2950 ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO; 2951 rc = ata_dev_configure(dev); 2952 ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO; 2953 if (rc) 2954 goto fail; 2955 } 2956 2957 /* configure transfer mode */ 2958 rc = ata_set_mode(&ap->link, &dev); 2959 if (rc) 2960 goto fail; 2961 2962 ata_for_each_dev(dev, &ap->link, ENABLED) 2963 return 0; 2964 2965 return -ENODEV; 2966 2967 fail: 2968 tries[dev->devno]--; 2969 2970 switch (rc) { 2971 case -EINVAL: 2972 /* eeek, something went very wrong, give up */ 2973 tries[dev->devno] = 0; 2974 break; 2975 2976 case -ENODEV: 2977 /* give it just one more chance */ 2978 tries[dev->devno] = min(tries[dev->devno], 1); 2979 fallthrough; 2980 case -EIO: 2981 if (tries[dev->devno] == 1) { 2982 /* This is the last chance, better to slow 2983 * down than lose it. 2984 */ 2985 sata_down_spd_limit(&ap->link, 0); 2986 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO); 2987 } 2988 } 2989 2990 if (!tries[dev->devno]) 2991 ata_dev_disable(dev); 2992 2993 goto retry; 2994 } 2995 2996 /** 2997 * sata_print_link_status - Print SATA link status 2998 * @link: SATA link to printk link status about 2999 * 3000 * This function prints link speed and status of a SATA link. 3001 * 3002 * LOCKING: 3003 * None. 3004 */ 3005 static void sata_print_link_status(struct ata_link *link) 3006 { 3007 u32 sstatus, scontrol, tmp; 3008 3009 if (sata_scr_read(link, SCR_STATUS, &sstatus)) 3010 return; 3011 sata_scr_read(link, SCR_CONTROL, &scontrol); 3012 3013 if (ata_phys_link_online(link)) { 3014 tmp = (sstatus >> 4) & 0xf; 3015 ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n", 3016 sata_spd_string(tmp), sstatus, scontrol); 3017 } else { 3018 ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n", 3019 sstatus, scontrol); 3020 } 3021 } 3022 3023 /** 3024 * ata_dev_pair - return other device on cable 3025 * @adev: device 3026 * 3027 * Obtain the other device on the same cable, or if none is 3028 * present NULL is returned 3029 */ 3030 3031 struct ata_device *ata_dev_pair(struct ata_device *adev) 3032 { 3033 struct ata_link *link = adev->link; 3034 struct ata_device *pair = &link->device[1 - adev->devno]; 3035 if (!ata_dev_enabled(pair)) 3036 return NULL; 3037 return pair; 3038 } 3039 EXPORT_SYMBOL_GPL(ata_dev_pair); 3040 3041 /** 3042 * sata_down_spd_limit - adjust SATA spd limit downward 3043 * @link: Link to adjust SATA spd limit for 3044 * @spd_limit: Additional limit 3045 * 3046 * Adjust SATA spd limit of @link downward. Note that this 3047 * function only adjusts the limit. The change must be applied 3048 * using sata_set_spd(). 3049 * 3050 * If @spd_limit is non-zero, the speed is limited to equal to or 3051 * lower than @spd_limit if such speed is supported. If 3052 * @spd_limit is slower than any supported speed, only the lowest 3053 * supported speed is allowed. 3054 * 3055 * LOCKING: 3056 * Inherited from caller. 3057 * 3058 * RETURNS: 3059 * 0 on success, negative errno on failure 3060 */ 3061 int sata_down_spd_limit(struct ata_link *link, u32 spd_limit) 3062 { 3063 u32 sstatus, spd, mask; 3064 int rc, bit; 3065 3066 if (!sata_scr_valid(link)) 3067 return -EOPNOTSUPP; 3068 3069 /* If SCR can be read, use it to determine the current SPD. 3070 * If not, use cached value in link->sata_spd. 3071 */ 3072 rc = sata_scr_read(link, SCR_STATUS, &sstatus); 3073 if (rc == 0 && ata_sstatus_online(sstatus)) 3074 spd = (sstatus >> 4) & 0xf; 3075 else 3076 spd = link->sata_spd; 3077 3078 mask = link->sata_spd_limit; 3079 if (mask <= 1) 3080 return -EINVAL; 3081 3082 /* unconditionally mask off the highest bit */ 3083 bit = fls(mask) - 1; 3084 mask &= ~(1 << bit); 3085 3086 /* 3087 * Mask off all speeds higher than or equal to the current one. At 3088 * this point, if current SPD is not available and we previously 3089 * recorded the link speed from SStatus, the driver has already 3090 * masked off the highest bit so mask should already be 1 or 0. 3091 * Otherwise, we should not force 1.5Gbps on a link where we have 3092 * not previously recorded speed from SStatus. Just return in this 3093 * case. 3094 */ 3095 if (spd > 1) 3096 mask &= (1 << (spd - 1)) - 1; 3097 else 3098 return -EINVAL; 3099 3100 /* were we already at the bottom? */ 3101 if (!mask) 3102 return -EINVAL; 3103 3104 if (spd_limit) { 3105 if (mask & ((1 << spd_limit) - 1)) 3106 mask &= (1 << spd_limit) - 1; 3107 else { 3108 bit = ffs(mask) - 1; 3109 mask = 1 << bit; 3110 } 3111 } 3112 3113 link->sata_spd_limit = mask; 3114 3115 ata_link_warn(link, "limiting SATA link speed to %s\n", 3116 sata_spd_string(fls(mask))); 3117 3118 return 0; 3119 } 3120 3121 #ifdef CONFIG_ATA_ACPI 3122 /** 3123 * ata_timing_cycle2mode - find xfer mode for the specified cycle duration 3124 * @xfer_shift: ATA_SHIFT_* value for transfer type to examine. 3125 * @cycle: cycle duration in ns 3126 * 3127 * Return matching xfer mode for @cycle. The returned mode is of 3128 * the transfer type specified by @xfer_shift. If @cycle is too 3129 * slow for @xfer_shift, 0xff is returned. If @cycle is faster 3130 * than the fastest known mode, the fasted mode is returned. 3131 * 3132 * LOCKING: 3133 * None. 3134 * 3135 * RETURNS: 3136 * Matching xfer_mode, 0xff if no match found. 3137 */ 3138 u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle) 3139 { 3140 u8 base_mode = 0xff, last_mode = 0xff; 3141 const struct ata_xfer_ent *ent; 3142 const struct ata_timing *t; 3143 3144 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++) 3145 if (ent->shift == xfer_shift) 3146 base_mode = ent->base; 3147 3148 for (t = ata_timing_find_mode(base_mode); 3149 t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) { 3150 unsigned short this_cycle; 3151 3152 switch (xfer_shift) { 3153 case ATA_SHIFT_PIO: 3154 case ATA_SHIFT_MWDMA: 3155 this_cycle = t->cycle; 3156 break; 3157 case ATA_SHIFT_UDMA: 3158 this_cycle = t->udma; 3159 break; 3160 default: 3161 return 0xff; 3162 } 3163 3164 if (cycle > this_cycle) 3165 break; 3166 3167 last_mode = t->mode; 3168 } 3169 3170 return last_mode; 3171 } 3172 #endif 3173 3174 /** 3175 * ata_down_xfermask_limit - adjust dev xfer masks downward 3176 * @dev: Device to adjust xfer masks 3177 * @sel: ATA_DNXFER_* selector 3178 * 3179 * Adjust xfer masks of @dev downward. Note that this function 3180 * does not apply the change. Invoking ata_set_mode() afterwards 3181 * will apply the limit. 3182 * 3183 * LOCKING: 3184 * Inherited from caller. 3185 * 3186 * RETURNS: 3187 * 0 on success, negative errno on failure 3188 */ 3189 int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel) 3190 { 3191 char buf[32]; 3192 unsigned long orig_mask, xfer_mask; 3193 unsigned long pio_mask, mwdma_mask, udma_mask; 3194 int quiet, highbit; 3195 3196 quiet = !!(sel & ATA_DNXFER_QUIET); 3197 sel &= ~ATA_DNXFER_QUIET; 3198 3199 xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask, 3200 dev->mwdma_mask, 3201 dev->udma_mask); 3202 ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask); 3203 3204 switch (sel) { 3205 case ATA_DNXFER_PIO: 3206 highbit = fls(pio_mask) - 1; 3207 pio_mask &= ~(1 << highbit); 3208 break; 3209 3210 case ATA_DNXFER_DMA: 3211 if (udma_mask) { 3212 highbit = fls(udma_mask) - 1; 3213 udma_mask &= ~(1 << highbit); 3214 if (!udma_mask) 3215 return -ENOENT; 3216 } else if (mwdma_mask) { 3217 highbit = fls(mwdma_mask) - 1; 3218 mwdma_mask &= ~(1 << highbit); 3219 if (!mwdma_mask) 3220 return -ENOENT; 3221 } 3222 break; 3223 3224 case ATA_DNXFER_40C: 3225 udma_mask &= ATA_UDMA_MASK_40C; 3226 break; 3227 3228 case ATA_DNXFER_FORCE_PIO0: 3229 pio_mask &= 1; 3230 fallthrough; 3231 case ATA_DNXFER_FORCE_PIO: 3232 mwdma_mask = 0; 3233 udma_mask = 0; 3234 break; 3235 3236 default: 3237 BUG(); 3238 } 3239 3240 xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask); 3241 3242 if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask) 3243 return -ENOENT; 3244 3245 if (!quiet) { 3246 if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA)) 3247 snprintf(buf, sizeof(buf), "%s:%s", 3248 ata_mode_string(xfer_mask), 3249 ata_mode_string(xfer_mask & ATA_MASK_PIO)); 3250 else 3251 snprintf(buf, sizeof(buf), "%s", 3252 ata_mode_string(xfer_mask)); 3253 3254 ata_dev_warn(dev, "limiting speed to %s\n", buf); 3255 } 3256 3257 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask, 3258 &dev->udma_mask); 3259 3260 return 0; 3261 } 3262 3263 static int ata_dev_set_mode(struct ata_device *dev) 3264 { 3265 struct ata_port *ap = dev->link->ap; 3266 struct ata_eh_context *ehc = &dev->link->eh_context; 3267 const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER; 3268 const char *dev_err_whine = ""; 3269 int ign_dev_err = 0; 3270 unsigned int err_mask = 0; 3271 int rc; 3272 3273 dev->flags &= ~ATA_DFLAG_PIO; 3274 if (dev->xfer_shift == ATA_SHIFT_PIO) 3275 dev->flags |= ATA_DFLAG_PIO; 3276 3277 if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id)) 3278 dev_err_whine = " (SET_XFERMODE skipped)"; 3279 else { 3280 if (nosetxfer) 3281 ata_dev_warn(dev, 3282 "NOSETXFER but PATA detected - can't " 3283 "skip SETXFER, might malfunction\n"); 3284 err_mask = ata_dev_set_xfermode(dev); 3285 } 3286 3287 if (err_mask & ~AC_ERR_DEV) 3288 goto fail; 3289 3290 /* revalidate */ 3291 ehc->i.flags |= ATA_EHI_POST_SETMODE; 3292 rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0); 3293 ehc->i.flags &= ~ATA_EHI_POST_SETMODE; 3294 if (rc) 3295 return rc; 3296 3297 if (dev->xfer_shift == ATA_SHIFT_PIO) { 3298 /* Old CFA may refuse this command, which is just fine */ 3299 if (ata_id_is_cfa(dev->id)) 3300 ign_dev_err = 1; 3301 /* Catch several broken garbage emulations plus some pre 3302 ATA devices */ 3303 if (ata_id_major_version(dev->id) == 0 && 3304 dev->pio_mode <= XFER_PIO_2) 3305 ign_dev_err = 1; 3306 /* Some very old devices and some bad newer ones fail 3307 any kind of SET_XFERMODE request but support PIO0-2 3308 timings and no IORDY */ 3309 if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2) 3310 ign_dev_err = 1; 3311 } 3312 /* Early MWDMA devices do DMA but don't allow DMA mode setting. 3313 Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */ 3314 if (dev->xfer_shift == ATA_SHIFT_MWDMA && 3315 dev->dma_mode == XFER_MW_DMA_0 && 3316 (dev->id[63] >> 8) & 1) 3317 ign_dev_err = 1; 3318 3319 /* if the device is actually configured correctly, ignore dev err */ 3320 if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id))) 3321 ign_dev_err = 1; 3322 3323 if (err_mask & AC_ERR_DEV) { 3324 if (!ign_dev_err) 3325 goto fail; 3326 else 3327 dev_err_whine = " (device error ignored)"; 3328 } 3329 3330 ata_dev_dbg(dev, "xfer_shift=%u, xfer_mode=0x%x\n", 3331 dev->xfer_shift, (int)dev->xfer_mode); 3332 3333 if (!(ehc->i.flags & ATA_EHI_QUIET) || 3334 ehc->i.flags & ATA_EHI_DID_HARDRESET) 3335 ata_dev_info(dev, "configured for %s%s\n", 3336 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)), 3337 dev_err_whine); 3338 3339 return 0; 3340 3341 fail: 3342 ata_dev_err(dev, "failed to set xfermode (err_mask=0x%x)\n", err_mask); 3343 return -EIO; 3344 } 3345 3346 /** 3347 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER 3348 * @link: link on which timings will be programmed 3349 * @r_failed_dev: out parameter for failed device 3350 * 3351 * Standard implementation of the function used to tune and set 3352 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If 3353 * ata_dev_set_mode() fails, pointer to the failing device is 3354 * returned in @r_failed_dev. 3355 * 3356 * LOCKING: 3357 * PCI/etc. bus probe sem. 3358 * 3359 * RETURNS: 3360 * 0 on success, negative errno otherwise 3361 */ 3362 3363 int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev) 3364 { 3365 struct ata_port *ap = link->ap; 3366 struct ata_device *dev; 3367 int rc = 0, used_dma = 0, found = 0; 3368 3369 /* step 1: calculate xfer_mask */ 3370 ata_for_each_dev(dev, link, ENABLED) { 3371 unsigned long pio_mask, dma_mask; 3372 unsigned int mode_mask; 3373 3374 mode_mask = ATA_DMA_MASK_ATA; 3375 if (dev->class == ATA_DEV_ATAPI) 3376 mode_mask = ATA_DMA_MASK_ATAPI; 3377 else if (ata_id_is_cfa(dev->id)) 3378 mode_mask = ATA_DMA_MASK_CFA; 3379 3380 ata_dev_xfermask(dev); 3381 ata_force_xfermask(dev); 3382 3383 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0); 3384 3385 if (libata_dma_mask & mode_mask) 3386 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, 3387 dev->udma_mask); 3388 else 3389 dma_mask = 0; 3390 3391 dev->pio_mode = ata_xfer_mask2mode(pio_mask); 3392 dev->dma_mode = ata_xfer_mask2mode(dma_mask); 3393 3394 found = 1; 3395 if (ata_dma_enabled(dev)) 3396 used_dma = 1; 3397 } 3398 if (!found) 3399 goto out; 3400 3401 /* step 2: always set host PIO timings */ 3402 ata_for_each_dev(dev, link, ENABLED) { 3403 if (dev->pio_mode == 0xff) { 3404 ata_dev_warn(dev, "no PIO support\n"); 3405 rc = -EINVAL; 3406 goto out; 3407 } 3408 3409 dev->xfer_mode = dev->pio_mode; 3410 dev->xfer_shift = ATA_SHIFT_PIO; 3411 if (ap->ops->set_piomode) 3412 ap->ops->set_piomode(ap, dev); 3413 } 3414 3415 /* step 3: set host DMA timings */ 3416 ata_for_each_dev(dev, link, ENABLED) { 3417 if (!ata_dma_enabled(dev)) 3418 continue; 3419 3420 dev->xfer_mode = dev->dma_mode; 3421 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode); 3422 if (ap->ops->set_dmamode) 3423 ap->ops->set_dmamode(ap, dev); 3424 } 3425 3426 /* step 4: update devices' xfer mode */ 3427 ata_for_each_dev(dev, link, ENABLED) { 3428 rc = ata_dev_set_mode(dev); 3429 if (rc) 3430 goto out; 3431 } 3432 3433 /* Record simplex status. If we selected DMA then the other 3434 * host channels are not permitted to do so. 3435 */ 3436 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX)) 3437 ap->host->simplex_claimed = ap; 3438 3439 out: 3440 if (rc) 3441 *r_failed_dev = dev; 3442 return rc; 3443 } 3444 EXPORT_SYMBOL_GPL(ata_do_set_mode); 3445 3446 /** 3447 * ata_wait_ready - wait for link to become ready 3448 * @link: link to be waited on 3449 * @deadline: deadline jiffies for the operation 3450 * @check_ready: callback to check link readiness 3451 * 3452 * Wait for @link to become ready. @check_ready should return 3453 * positive number if @link is ready, 0 if it isn't, -ENODEV if 3454 * link doesn't seem to be occupied, other errno for other error 3455 * conditions. 3456 * 3457 * Transient -ENODEV conditions are allowed for 3458 * ATA_TMOUT_FF_WAIT. 3459 * 3460 * LOCKING: 3461 * EH context. 3462 * 3463 * RETURNS: 3464 * 0 if @link is ready before @deadline; otherwise, -errno. 3465 */ 3466 int ata_wait_ready(struct ata_link *link, unsigned long deadline, 3467 int (*check_ready)(struct ata_link *link)) 3468 { 3469 unsigned long start = jiffies; 3470 unsigned long nodev_deadline; 3471 int warned = 0; 3472 3473 /* choose which 0xff timeout to use, read comment in libata.h */ 3474 if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN) 3475 nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG); 3476 else 3477 nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT); 3478 3479 /* Slave readiness can't be tested separately from master. On 3480 * M/S emulation configuration, this function should be called 3481 * only on the master and it will handle both master and slave. 3482 */ 3483 WARN_ON(link == link->ap->slave_link); 3484 3485 if (time_after(nodev_deadline, deadline)) 3486 nodev_deadline = deadline; 3487 3488 while (1) { 3489 unsigned long now = jiffies; 3490 int ready, tmp; 3491 3492 ready = tmp = check_ready(link); 3493 if (ready > 0) 3494 return 0; 3495 3496 /* 3497 * -ENODEV could be transient. Ignore -ENODEV if link 3498 * is online. Also, some SATA devices take a long 3499 * time to clear 0xff after reset. Wait for 3500 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't 3501 * offline. 3502 * 3503 * Note that some PATA controllers (pata_ali) explode 3504 * if status register is read more than once when 3505 * there's no device attached. 3506 */ 3507 if (ready == -ENODEV) { 3508 if (ata_link_online(link)) 3509 ready = 0; 3510 else if ((link->ap->flags & ATA_FLAG_SATA) && 3511 !ata_link_offline(link) && 3512 time_before(now, nodev_deadline)) 3513 ready = 0; 3514 } 3515 3516 if (ready) 3517 return ready; 3518 if (time_after(now, deadline)) 3519 return -EBUSY; 3520 3521 if (!warned && time_after(now, start + 5 * HZ) && 3522 (deadline - now > 3 * HZ)) { 3523 ata_link_warn(link, 3524 "link is slow to respond, please be patient " 3525 "(ready=%d)\n", tmp); 3526 warned = 1; 3527 } 3528 3529 ata_msleep(link->ap, 50); 3530 } 3531 } 3532 3533 /** 3534 * ata_wait_after_reset - wait for link to become ready after reset 3535 * @link: link to be waited on 3536 * @deadline: deadline jiffies for the operation 3537 * @check_ready: callback to check link readiness 3538 * 3539 * Wait for @link to become ready after reset. 3540 * 3541 * LOCKING: 3542 * EH context. 3543 * 3544 * RETURNS: 3545 * 0 if @link is ready before @deadline; otherwise, -errno. 3546 */ 3547 int ata_wait_after_reset(struct ata_link *link, unsigned long deadline, 3548 int (*check_ready)(struct ata_link *link)) 3549 { 3550 ata_msleep(link->ap, ATA_WAIT_AFTER_RESET); 3551 3552 return ata_wait_ready(link, deadline, check_ready); 3553 } 3554 EXPORT_SYMBOL_GPL(ata_wait_after_reset); 3555 3556 /** 3557 * ata_std_prereset - prepare for reset 3558 * @link: ATA link to be reset 3559 * @deadline: deadline jiffies for the operation 3560 * 3561 * @link is about to be reset. Initialize it. Failure from 3562 * prereset makes libata abort whole reset sequence and give up 3563 * that port, so prereset should be best-effort. It does its 3564 * best to prepare for reset sequence but if things go wrong, it 3565 * should just whine, not fail. 3566 * 3567 * LOCKING: 3568 * Kernel thread context (may sleep) 3569 * 3570 * RETURNS: 3571 * 0 on success, -errno otherwise. 3572 */ 3573 int ata_std_prereset(struct ata_link *link, unsigned long deadline) 3574 { 3575 struct ata_port *ap = link->ap; 3576 struct ata_eh_context *ehc = &link->eh_context; 3577 const unsigned long *timing = sata_ehc_deb_timing(ehc); 3578 int rc; 3579 3580 /* if we're about to do hardreset, nothing more to do */ 3581 if (ehc->i.action & ATA_EH_HARDRESET) 3582 return 0; 3583 3584 /* if SATA, resume link */ 3585 if (ap->flags & ATA_FLAG_SATA) { 3586 rc = sata_link_resume(link, timing, deadline); 3587 /* whine about phy resume failure but proceed */ 3588 if (rc && rc != -EOPNOTSUPP) 3589 ata_link_warn(link, 3590 "failed to resume link for reset (errno=%d)\n", 3591 rc); 3592 } 3593 3594 /* no point in trying softreset on offline link */ 3595 if (ata_phys_link_offline(link)) 3596 ehc->i.action &= ~ATA_EH_SOFTRESET; 3597 3598 return 0; 3599 } 3600 EXPORT_SYMBOL_GPL(ata_std_prereset); 3601 3602 /** 3603 * sata_std_hardreset - COMRESET w/o waiting or classification 3604 * @link: link to reset 3605 * @class: resulting class of attached device 3606 * @deadline: deadline jiffies for the operation 3607 * 3608 * Standard SATA COMRESET w/o waiting or classification. 3609 * 3610 * LOCKING: 3611 * Kernel thread context (may sleep) 3612 * 3613 * RETURNS: 3614 * 0 if link offline, -EAGAIN if link online, -errno on errors. 3615 */ 3616 int sata_std_hardreset(struct ata_link *link, unsigned int *class, 3617 unsigned long deadline) 3618 { 3619 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context); 3620 bool online; 3621 int rc; 3622 3623 /* do hardreset */ 3624 rc = sata_link_hardreset(link, timing, deadline, &online, NULL); 3625 return online ? -EAGAIN : rc; 3626 } 3627 EXPORT_SYMBOL_GPL(sata_std_hardreset); 3628 3629 /** 3630 * ata_std_postreset - standard postreset callback 3631 * @link: the target ata_link 3632 * @classes: classes of attached devices 3633 * 3634 * This function is invoked after a successful reset. Note that 3635 * the device might have been reset more than once using 3636 * different reset methods before postreset is invoked. 3637 * 3638 * LOCKING: 3639 * Kernel thread context (may sleep) 3640 */ 3641 void ata_std_postreset(struct ata_link *link, unsigned int *classes) 3642 { 3643 u32 serror; 3644 3645 /* reset complete, clear SError */ 3646 if (!sata_scr_read(link, SCR_ERROR, &serror)) 3647 sata_scr_write(link, SCR_ERROR, serror); 3648 3649 /* print link status */ 3650 sata_print_link_status(link); 3651 } 3652 EXPORT_SYMBOL_GPL(ata_std_postreset); 3653 3654 /** 3655 * ata_dev_same_device - Determine whether new ID matches configured device 3656 * @dev: device to compare against 3657 * @new_class: class of the new device 3658 * @new_id: IDENTIFY page of the new device 3659 * 3660 * Compare @new_class and @new_id against @dev and determine 3661 * whether @dev is the device indicated by @new_class and 3662 * @new_id. 3663 * 3664 * LOCKING: 3665 * None. 3666 * 3667 * RETURNS: 3668 * 1 if @dev matches @new_class and @new_id, 0 otherwise. 3669 */ 3670 static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class, 3671 const u16 *new_id) 3672 { 3673 const u16 *old_id = dev->id; 3674 unsigned char model[2][ATA_ID_PROD_LEN + 1]; 3675 unsigned char serial[2][ATA_ID_SERNO_LEN + 1]; 3676 3677 if (dev->class != new_class) { 3678 ata_dev_info(dev, "class mismatch %d != %d\n", 3679 dev->class, new_class); 3680 return 0; 3681 } 3682 3683 ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0])); 3684 ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1])); 3685 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0])); 3686 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1])); 3687 3688 if (strcmp(model[0], model[1])) { 3689 ata_dev_info(dev, "model number mismatch '%s' != '%s'\n", 3690 model[0], model[1]); 3691 return 0; 3692 } 3693 3694 if (strcmp(serial[0], serial[1])) { 3695 ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n", 3696 serial[0], serial[1]); 3697 return 0; 3698 } 3699 3700 return 1; 3701 } 3702 3703 /** 3704 * ata_dev_reread_id - Re-read IDENTIFY data 3705 * @dev: target ATA device 3706 * @readid_flags: read ID flags 3707 * 3708 * Re-read IDENTIFY page and make sure @dev is still attached to 3709 * the port. 3710 * 3711 * LOCKING: 3712 * Kernel thread context (may sleep) 3713 * 3714 * RETURNS: 3715 * 0 on success, negative errno otherwise 3716 */ 3717 int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags) 3718 { 3719 unsigned int class = dev->class; 3720 u16 *id = (void *)dev->link->ap->sector_buf; 3721 int rc; 3722 3723 /* read ID data */ 3724 rc = ata_dev_read_id(dev, &class, readid_flags, id); 3725 if (rc) 3726 return rc; 3727 3728 /* is the device still there? */ 3729 if (!ata_dev_same_device(dev, class, id)) 3730 return -ENODEV; 3731 3732 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS); 3733 return 0; 3734 } 3735 3736 /** 3737 * ata_dev_revalidate - Revalidate ATA device 3738 * @dev: device to revalidate 3739 * @new_class: new class code 3740 * @readid_flags: read ID flags 3741 * 3742 * Re-read IDENTIFY page, make sure @dev is still attached to the 3743 * port and reconfigure it according to the new IDENTIFY page. 3744 * 3745 * LOCKING: 3746 * Kernel thread context (may sleep) 3747 * 3748 * RETURNS: 3749 * 0 on success, negative errno otherwise 3750 */ 3751 int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class, 3752 unsigned int readid_flags) 3753 { 3754 u64 n_sectors = dev->n_sectors; 3755 u64 n_native_sectors = dev->n_native_sectors; 3756 int rc; 3757 3758 if (!ata_dev_enabled(dev)) 3759 return -ENODEV; 3760 3761 /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */ 3762 if (ata_class_enabled(new_class) && 3763 new_class != ATA_DEV_ATA && 3764 new_class != ATA_DEV_ATAPI && 3765 new_class != ATA_DEV_ZAC && 3766 new_class != ATA_DEV_SEMB) { 3767 ata_dev_info(dev, "class mismatch %u != %u\n", 3768 dev->class, new_class); 3769 rc = -ENODEV; 3770 goto fail; 3771 } 3772 3773 /* re-read ID */ 3774 rc = ata_dev_reread_id(dev, readid_flags); 3775 if (rc) 3776 goto fail; 3777 3778 /* configure device according to the new ID */ 3779 rc = ata_dev_configure(dev); 3780 if (rc) 3781 goto fail; 3782 3783 /* verify n_sectors hasn't changed */ 3784 if (dev->class != ATA_DEV_ATA || !n_sectors || 3785 dev->n_sectors == n_sectors) 3786 return 0; 3787 3788 /* n_sectors has changed */ 3789 ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n", 3790 (unsigned long long)n_sectors, 3791 (unsigned long long)dev->n_sectors); 3792 3793 /* 3794 * Something could have caused HPA to be unlocked 3795 * involuntarily. If n_native_sectors hasn't changed and the 3796 * new size matches it, keep the device. 3797 */ 3798 if (dev->n_native_sectors == n_native_sectors && 3799 dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) { 3800 ata_dev_warn(dev, 3801 "new n_sectors matches native, probably " 3802 "late HPA unlock, n_sectors updated\n"); 3803 /* use the larger n_sectors */ 3804 return 0; 3805 } 3806 3807 /* 3808 * Some BIOSes boot w/o HPA but resume w/ HPA locked. Try 3809 * unlocking HPA in those cases. 3810 * 3811 * https://bugzilla.kernel.org/show_bug.cgi?id=15396 3812 */ 3813 if (dev->n_native_sectors == n_native_sectors && 3814 dev->n_sectors < n_sectors && n_sectors == n_native_sectors && 3815 !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) { 3816 ata_dev_warn(dev, 3817 "old n_sectors matches native, probably " 3818 "late HPA lock, will try to unlock HPA\n"); 3819 /* try unlocking HPA */ 3820 dev->flags |= ATA_DFLAG_UNLOCK_HPA; 3821 rc = -EIO; 3822 } else 3823 rc = -ENODEV; 3824 3825 /* restore original n_[native_]sectors and fail */ 3826 dev->n_native_sectors = n_native_sectors; 3827 dev->n_sectors = n_sectors; 3828 fail: 3829 ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc); 3830 return rc; 3831 } 3832 3833 struct ata_blacklist_entry { 3834 const char *model_num; 3835 const char *model_rev; 3836 unsigned long horkage; 3837 }; 3838 3839 static const struct ata_blacklist_entry ata_device_blacklist [] = { 3840 /* Devices with DMA related problems under Linux */ 3841 { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA }, 3842 { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA }, 3843 { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA }, 3844 { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA }, 3845 { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA }, 3846 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA }, 3847 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA }, 3848 { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA }, 3849 { "CRD-8400B", NULL, ATA_HORKAGE_NODMA }, 3850 { "CRD-848[02]B", NULL, ATA_HORKAGE_NODMA }, 3851 { "CRD-84", NULL, ATA_HORKAGE_NODMA }, 3852 { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA }, 3853 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA }, 3854 { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA }, 3855 { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA }, 3856 { "HITACHI CDR-8[34]35",NULL, ATA_HORKAGE_NODMA }, 3857 { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA }, 3858 { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA }, 3859 { "CD-532E-A", NULL, ATA_HORKAGE_NODMA }, 3860 { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA }, 3861 { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA }, 3862 { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA }, 3863 { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA }, 3864 { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA }, 3865 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA }, 3866 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA }, 3867 { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA }, 3868 { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA }, 3869 { " 2GB ATA Flash Disk", "ADMA428M", ATA_HORKAGE_NODMA }, 3870 { "VRFDFC22048UCHC-TE*", NULL, ATA_HORKAGE_NODMA }, 3871 /* Odd clown on sil3726/4726 PMPs */ 3872 { "Config Disk", NULL, ATA_HORKAGE_DISABLE }, 3873 /* Similar story with ASMedia 1092 */ 3874 { "ASMT109x- Config", NULL, ATA_HORKAGE_DISABLE }, 3875 3876 /* Weird ATAPI devices */ 3877 { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 }, 3878 { "QUANTUM DAT DAT72-000", NULL, ATA_HORKAGE_ATAPI_MOD16_DMA }, 3879 { "Slimtype DVD A DS8A8SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 }, 3880 { "Slimtype DVD A DS8A9SH", NULL, ATA_HORKAGE_MAX_SEC_LBA48 }, 3881 3882 /* 3883 * Causes silent data corruption with higher max sects. 3884 * http://lkml.kernel.org/g/x49wpy40ysk.fsf@segfault.boston.devel.redhat.com 3885 */ 3886 { "ST380013AS", "3.20", ATA_HORKAGE_MAX_SEC_1024 }, 3887 3888 /* 3889 * These devices time out with higher max sects. 3890 * https://bugzilla.kernel.org/show_bug.cgi?id=121671 3891 */ 3892 { "LITEON CX1-JB*-HP", NULL, ATA_HORKAGE_MAX_SEC_1024 }, 3893 { "LITEON EP1-*", NULL, ATA_HORKAGE_MAX_SEC_1024 }, 3894 3895 /* Devices we expect to fail diagnostics */ 3896 3897 /* Devices where NCQ should be avoided */ 3898 /* NCQ is slow */ 3899 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ }, 3900 { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, }, 3901 /* http://thread.gmane.org/gmane.linux.ide/14907 */ 3902 { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ }, 3903 /* NCQ is broken */ 3904 { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ }, 3905 { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ }, 3906 { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ }, 3907 { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ }, 3908 { "OCZ CORE_SSD", "02.10104", ATA_HORKAGE_NONCQ }, 3909 3910 /* Seagate NCQ + FLUSH CACHE firmware bug */ 3911 { "ST31500341AS", "SD1[5-9]", ATA_HORKAGE_NONCQ | 3912 ATA_HORKAGE_FIRMWARE_WARN }, 3913 3914 { "ST31000333AS", "SD1[5-9]", ATA_HORKAGE_NONCQ | 3915 ATA_HORKAGE_FIRMWARE_WARN }, 3916 3917 { "ST3640[36]23AS", "SD1[5-9]", ATA_HORKAGE_NONCQ | 3918 ATA_HORKAGE_FIRMWARE_WARN }, 3919 3920 { "ST3320[68]13AS", "SD1[5-9]", ATA_HORKAGE_NONCQ | 3921 ATA_HORKAGE_FIRMWARE_WARN }, 3922 3923 /* drives which fail FPDMA_AA activation (some may freeze afterwards) 3924 the ST disks also have LPM issues */ 3925 { "ST1000LM024 HN-M101MBB", NULL, ATA_HORKAGE_BROKEN_FPDMA_AA | 3926 ATA_HORKAGE_NOLPM, }, 3927 { "VB0250EAVER", "HPG7", ATA_HORKAGE_BROKEN_FPDMA_AA }, 3928 3929 /* Blacklist entries taken from Silicon Image 3124/3132 3930 Windows driver .inf file - also several Linux problem reports */ 3931 { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, }, 3932 { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, }, 3933 { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, }, 3934 3935 /* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */ 3936 { "C300-CTFDDAC128MAG", "0001", ATA_HORKAGE_NONCQ, }, 3937 3938 /* Sandisk SD7/8/9s lock up hard on large trims */ 3939 { "SanDisk SD[789]*", NULL, ATA_HORKAGE_MAX_TRIM_128M, }, 3940 3941 /* devices which puke on READ_NATIVE_MAX */ 3942 { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, }, 3943 { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA }, 3944 { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA }, 3945 { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA }, 3946 3947 /* this one allows HPA unlocking but fails IOs on the area */ 3948 { "OCZ-VERTEX", "1.30", ATA_HORKAGE_BROKEN_HPA }, 3949 3950 /* Devices which report 1 sector over size HPA */ 3951 { "ST340823A", NULL, ATA_HORKAGE_HPA_SIZE, }, 3952 { "ST320413A", NULL, ATA_HORKAGE_HPA_SIZE, }, 3953 { "ST310211A", NULL, ATA_HORKAGE_HPA_SIZE, }, 3954 3955 /* Devices which get the IVB wrong */ 3956 { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, }, 3957 /* Maybe we should just blacklist TSSTcorp... */ 3958 { "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]", ATA_HORKAGE_IVB, }, 3959 3960 /* Devices that do not need bridging limits applied */ 3961 { "MTRON MSP-SATA*", NULL, ATA_HORKAGE_BRIDGE_OK, }, 3962 { "BUFFALO HD-QSU2/R5", NULL, ATA_HORKAGE_BRIDGE_OK, }, 3963 3964 /* Devices which aren't very happy with higher link speeds */ 3965 { "WD My Book", NULL, ATA_HORKAGE_1_5_GBPS, }, 3966 { "Seagate FreeAgent GoFlex", NULL, ATA_HORKAGE_1_5_GBPS, }, 3967 3968 /* 3969 * Devices which choke on SETXFER. Applies only if both the 3970 * device and controller are SATA. 3971 */ 3972 { "PIONEER DVD-RW DVRTD08", NULL, ATA_HORKAGE_NOSETXFER }, 3973 { "PIONEER DVD-RW DVRTD08A", NULL, ATA_HORKAGE_NOSETXFER }, 3974 { "PIONEER DVD-RW DVR-215", NULL, ATA_HORKAGE_NOSETXFER }, 3975 { "PIONEER DVD-RW DVR-212D", NULL, ATA_HORKAGE_NOSETXFER }, 3976 { "PIONEER DVD-RW DVR-216D", NULL, ATA_HORKAGE_NOSETXFER }, 3977 3978 /* Crucial BX100 SSD 500GB has broken LPM support */ 3979 { "CT500BX100SSD1", NULL, ATA_HORKAGE_NOLPM }, 3980 3981 /* 512GB MX100 with MU01 firmware has both queued TRIM and LPM issues */ 3982 { "Crucial_CT512MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM | 3983 ATA_HORKAGE_ZERO_AFTER_TRIM | 3984 ATA_HORKAGE_NOLPM, }, 3985 /* 512GB MX100 with newer firmware has only LPM issues */ 3986 { "Crucial_CT512MX100*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM | 3987 ATA_HORKAGE_NOLPM, }, 3988 3989 /* 480GB+ M500 SSDs have both queued TRIM and LPM issues */ 3990 { "Crucial_CT480M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | 3991 ATA_HORKAGE_ZERO_AFTER_TRIM | 3992 ATA_HORKAGE_NOLPM, }, 3993 { "Crucial_CT960M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | 3994 ATA_HORKAGE_ZERO_AFTER_TRIM | 3995 ATA_HORKAGE_NOLPM, }, 3996 3997 /* These specific Samsung models/firmware-revs do not handle LPM well */ 3998 { "SAMSUNG MZMPC128HBFU-000MV", "CXM14M1Q", ATA_HORKAGE_NOLPM, }, 3999 { "SAMSUNG SSD PM830 mSATA *", "CXM13D1Q", ATA_HORKAGE_NOLPM, }, 4000 { "SAMSUNG MZ7TD256HAFV-000L9", NULL, ATA_HORKAGE_NOLPM, }, 4001 { "SAMSUNG MZ7TE512HMHP-000L1", "EXT06L0Q", ATA_HORKAGE_NOLPM, }, 4002 4003 /* devices that don't properly handle queued TRIM commands */ 4004 { "Micron_M500IT_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM | 4005 ATA_HORKAGE_ZERO_AFTER_TRIM, }, 4006 { "Micron_M500_*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | 4007 ATA_HORKAGE_ZERO_AFTER_TRIM, }, 4008 { "Crucial_CT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | 4009 ATA_HORKAGE_ZERO_AFTER_TRIM, }, 4010 { "Micron_M5[15]0_*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM | 4011 ATA_HORKAGE_ZERO_AFTER_TRIM, }, 4012 { "Crucial_CT*M550*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM | 4013 ATA_HORKAGE_ZERO_AFTER_TRIM, }, 4014 { "Crucial_CT*MX100*", "MU01", ATA_HORKAGE_NO_NCQ_TRIM | 4015 ATA_HORKAGE_ZERO_AFTER_TRIM, }, 4016 { "Samsung SSD 840*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | 4017 ATA_HORKAGE_ZERO_AFTER_TRIM, }, 4018 { "Samsung SSD 850*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | 4019 ATA_HORKAGE_ZERO_AFTER_TRIM, }, 4020 { "Samsung SSD 860*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | 4021 ATA_HORKAGE_ZERO_AFTER_TRIM | 4022 ATA_HORKAGE_NO_NCQ_ON_ATI, }, 4023 { "Samsung SSD 870*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | 4024 ATA_HORKAGE_ZERO_AFTER_TRIM | 4025 ATA_HORKAGE_NO_NCQ_ON_ATI, }, 4026 { "FCCT*M500*", NULL, ATA_HORKAGE_NO_NCQ_TRIM | 4027 ATA_HORKAGE_ZERO_AFTER_TRIM, }, 4028 4029 /* devices that don't properly handle TRIM commands */ 4030 { "SuperSSpeed S238*", NULL, ATA_HORKAGE_NOTRIM, }, 4031 4032 /* 4033 * As defined, the DRAT (Deterministic Read After Trim) and RZAT 4034 * (Return Zero After Trim) flags in the ATA Command Set are 4035 * unreliable in the sense that they only define what happens if 4036 * the device successfully executed the DSM TRIM command. TRIM 4037 * is only advisory, however, and the device is free to silently 4038 * ignore all or parts of the request. 4039 * 4040 * Whitelist drives that are known to reliably return zeroes 4041 * after TRIM. 4042 */ 4043 4044 /* 4045 * The intel 510 drive has buggy DRAT/RZAT. Explicitly exclude 4046 * that model before whitelisting all other intel SSDs. 4047 */ 4048 { "INTEL*SSDSC2MH*", NULL, 0, }, 4049 4050 { "Micron*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, 4051 { "Crucial*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, 4052 { "INTEL*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, 4053 { "SSD*INTEL*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, 4054 { "Samsung*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, 4055 { "SAMSUNG*SSD*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, 4056 { "SAMSUNG*MZ7KM*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, 4057 { "ST[1248][0248]0[FH]*", NULL, ATA_HORKAGE_ZERO_AFTER_TRIM, }, 4058 4059 /* 4060 * Some WD SATA-I drives spin up and down erratically when the link 4061 * is put into the slumber mode. We don't have full list of the 4062 * affected devices. Disable LPM if the device matches one of the 4063 * known prefixes and is SATA-1. As a side effect LPM partial is 4064 * lost too. 4065 * 4066 * https://bugzilla.kernel.org/show_bug.cgi?id=57211 4067 */ 4068 { "WDC WD800JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM }, 4069 { "WDC WD1200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM }, 4070 { "WDC WD1600JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM }, 4071 { "WDC WD2000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM }, 4072 { "WDC WD2500JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM }, 4073 { "WDC WD3000JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM }, 4074 { "WDC WD3200JD-*", NULL, ATA_HORKAGE_WD_BROKEN_LPM }, 4075 4076 /* End Marker */ 4077 { } 4078 }; 4079 4080 static unsigned long ata_dev_blacklisted(const struct ata_device *dev) 4081 { 4082 unsigned char model_num[ATA_ID_PROD_LEN + 1]; 4083 unsigned char model_rev[ATA_ID_FW_REV_LEN + 1]; 4084 const struct ata_blacklist_entry *ad = ata_device_blacklist; 4085 4086 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num)); 4087 ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev)); 4088 4089 while (ad->model_num) { 4090 if (glob_match(ad->model_num, model_num)) { 4091 if (ad->model_rev == NULL) 4092 return ad->horkage; 4093 if (glob_match(ad->model_rev, model_rev)) 4094 return ad->horkage; 4095 } 4096 ad++; 4097 } 4098 return 0; 4099 } 4100 4101 static int ata_dma_blacklisted(const struct ata_device *dev) 4102 { 4103 /* We don't support polling DMA. 4104 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO) 4105 * if the LLDD handles only interrupts in the HSM_ST_LAST state. 4106 */ 4107 if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) && 4108 (dev->flags & ATA_DFLAG_CDB_INTR)) 4109 return 1; 4110 return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0; 4111 } 4112 4113 /** 4114 * ata_is_40wire - check drive side detection 4115 * @dev: device 4116 * 4117 * Perform drive side detection decoding, allowing for device vendors 4118 * who can't follow the documentation. 4119 */ 4120 4121 static int ata_is_40wire(struct ata_device *dev) 4122 { 4123 if (dev->horkage & ATA_HORKAGE_IVB) 4124 return ata_drive_40wire_relaxed(dev->id); 4125 return ata_drive_40wire(dev->id); 4126 } 4127 4128 /** 4129 * cable_is_40wire - 40/80/SATA decider 4130 * @ap: port to consider 4131 * 4132 * This function encapsulates the policy for speed management 4133 * in one place. At the moment we don't cache the result but 4134 * there is a good case for setting ap->cbl to the result when 4135 * we are called with unknown cables (and figuring out if it 4136 * impacts hotplug at all). 4137 * 4138 * Return 1 if the cable appears to be 40 wire. 4139 */ 4140 4141 static int cable_is_40wire(struct ata_port *ap) 4142 { 4143 struct ata_link *link; 4144 struct ata_device *dev; 4145 4146 /* If the controller thinks we are 40 wire, we are. */ 4147 if (ap->cbl == ATA_CBL_PATA40) 4148 return 1; 4149 4150 /* If the controller thinks we are 80 wire, we are. */ 4151 if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA) 4152 return 0; 4153 4154 /* If the system is known to be 40 wire short cable (eg 4155 * laptop), then we allow 80 wire modes even if the drive 4156 * isn't sure. 4157 */ 4158 if (ap->cbl == ATA_CBL_PATA40_SHORT) 4159 return 0; 4160 4161 /* If the controller doesn't know, we scan. 4162 * 4163 * Note: We look for all 40 wire detects at this point. Any 4164 * 80 wire detect is taken to be 80 wire cable because 4165 * - in many setups only the one drive (slave if present) will 4166 * give a valid detect 4167 * - if you have a non detect capable drive you don't want it 4168 * to colour the choice 4169 */ 4170 ata_for_each_link(link, ap, EDGE) { 4171 ata_for_each_dev(dev, link, ENABLED) { 4172 if (!ata_is_40wire(dev)) 4173 return 0; 4174 } 4175 } 4176 return 1; 4177 } 4178 4179 /** 4180 * ata_dev_xfermask - Compute supported xfermask of the given device 4181 * @dev: Device to compute xfermask for 4182 * 4183 * Compute supported xfermask of @dev and store it in 4184 * dev->*_mask. This function is responsible for applying all 4185 * known limits including host controller limits, device 4186 * blacklist, etc... 4187 * 4188 * LOCKING: 4189 * None. 4190 */ 4191 static void ata_dev_xfermask(struct ata_device *dev) 4192 { 4193 struct ata_link *link = dev->link; 4194 struct ata_port *ap = link->ap; 4195 struct ata_host *host = ap->host; 4196 unsigned long xfer_mask; 4197 4198 /* controller modes available */ 4199 xfer_mask = ata_pack_xfermask(ap->pio_mask, 4200 ap->mwdma_mask, ap->udma_mask); 4201 4202 /* drive modes available */ 4203 xfer_mask &= ata_pack_xfermask(dev->pio_mask, 4204 dev->mwdma_mask, dev->udma_mask); 4205 xfer_mask &= ata_id_xfermask(dev->id); 4206 4207 /* 4208 * CFA Advanced TrueIDE timings are not allowed on a shared 4209 * cable 4210 */ 4211 if (ata_dev_pair(dev)) { 4212 /* No PIO5 or PIO6 */ 4213 xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5)); 4214 /* No MWDMA3 or MWDMA 4 */ 4215 xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3)); 4216 } 4217 4218 if (ata_dma_blacklisted(dev)) { 4219 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA); 4220 ata_dev_warn(dev, 4221 "device is on DMA blacklist, disabling DMA\n"); 4222 } 4223 4224 if ((host->flags & ATA_HOST_SIMPLEX) && 4225 host->simplex_claimed && host->simplex_claimed != ap) { 4226 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA); 4227 ata_dev_warn(dev, 4228 "simplex DMA is claimed by other device, disabling DMA\n"); 4229 } 4230 4231 if (ap->flags & ATA_FLAG_NO_IORDY) 4232 xfer_mask &= ata_pio_mask_no_iordy(dev); 4233 4234 if (ap->ops->mode_filter) 4235 xfer_mask = ap->ops->mode_filter(dev, xfer_mask); 4236 4237 /* Apply cable rule here. Don't apply it early because when 4238 * we handle hot plug the cable type can itself change. 4239 * Check this last so that we know if the transfer rate was 4240 * solely limited by the cable. 4241 * Unknown or 80 wire cables reported host side are checked 4242 * drive side as well. Cases where we know a 40wire cable 4243 * is used safely for 80 are not checked here. 4244 */ 4245 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA)) 4246 /* UDMA/44 or higher would be available */ 4247 if (cable_is_40wire(ap)) { 4248 ata_dev_warn(dev, 4249 "limited to UDMA/33 due to 40-wire cable\n"); 4250 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA); 4251 } 4252 4253 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, 4254 &dev->mwdma_mask, &dev->udma_mask); 4255 } 4256 4257 /** 4258 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command 4259 * @dev: Device to which command will be sent 4260 * 4261 * Issue SET FEATURES - XFER MODE command to device @dev 4262 * on port @ap. 4263 * 4264 * LOCKING: 4265 * PCI/etc. bus probe sem. 4266 * 4267 * RETURNS: 4268 * 0 on success, AC_ERR_* mask otherwise. 4269 */ 4270 4271 static unsigned int ata_dev_set_xfermode(struct ata_device *dev) 4272 { 4273 struct ata_taskfile tf; 4274 unsigned int err_mask; 4275 4276 /* set up set-features taskfile */ 4277 ata_dev_dbg(dev, "set features - xfer mode\n"); 4278 4279 /* Some controllers and ATAPI devices show flaky interrupt 4280 * behavior after setting xfer mode. Use polling instead. 4281 */ 4282 ata_tf_init(dev, &tf); 4283 tf.command = ATA_CMD_SET_FEATURES; 4284 tf.feature = SETFEATURES_XFER; 4285 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING; 4286 tf.protocol = ATA_PROT_NODATA; 4287 /* If we are using IORDY we must send the mode setting command */ 4288 if (ata_pio_need_iordy(dev)) 4289 tf.nsect = dev->xfer_mode; 4290 /* If the device has IORDY and the controller does not - turn it off */ 4291 else if (ata_id_has_iordy(dev->id)) 4292 tf.nsect = 0x01; 4293 else /* In the ancient relic department - skip all of this */ 4294 return 0; 4295 4296 /* On some disks, this command causes spin-up, so we need longer timeout */ 4297 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 15000); 4298 4299 return err_mask; 4300 } 4301 4302 /** 4303 * ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES 4304 * @dev: Device to which command will be sent 4305 * @enable: Whether to enable or disable the feature 4306 * @feature: The sector count represents the feature to set 4307 * 4308 * Issue SET FEATURES - SATA FEATURES command to device @dev 4309 * on port @ap with sector count 4310 * 4311 * LOCKING: 4312 * PCI/etc. bus probe sem. 4313 * 4314 * RETURNS: 4315 * 0 on success, AC_ERR_* mask otherwise. 4316 */ 4317 unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable, u8 feature) 4318 { 4319 struct ata_taskfile tf; 4320 unsigned int err_mask; 4321 unsigned long timeout = 0; 4322 4323 /* set up set-features taskfile */ 4324 ata_dev_dbg(dev, "set features - SATA features\n"); 4325 4326 ata_tf_init(dev, &tf); 4327 tf.command = ATA_CMD_SET_FEATURES; 4328 tf.feature = enable; 4329 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 4330 tf.protocol = ATA_PROT_NODATA; 4331 tf.nsect = feature; 4332 4333 if (enable == SETFEATURES_SPINUP) 4334 timeout = ata_probe_timeout ? 4335 ata_probe_timeout * 1000 : SETFEATURES_SPINUP_TIMEOUT; 4336 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, timeout); 4337 4338 return err_mask; 4339 } 4340 EXPORT_SYMBOL_GPL(ata_dev_set_feature); 4341 4342 /** 4343 * ata_dev_init_params - Issue INIT DEV PARAMS command 4344 * @dev: Device to which command will be sent 4345 * @heads: Number of heads (taskfile parameter) 4346 * @sectors: Number of sectors (taskfile parameter) 4347 * 4348 * LOCKING: 4349 * Kernel thread context (may sleep) 4350 * 4351 * RETURNS: 4352 * 0 on success, AC_ERR_* mask otherwise. 4353 */ 4354 static unsigned int ata_dev_init_params(struct ata_device *dev, 4355 u16 heads, u16 sectors) 4356 { 4357 struct ata_taskfile tf; 4358 unsigned int err_mask; 4359 4360 /* Number of sectors per track 1-255. Number of heads 1-16 */ 4361 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16) 4362 return AC_ERR_INVALID; 4363 4364 /* set up init dev params taskfile */ 4365 ata_dev_dbg(dev, "init dev params \n"); 4366 4367 ata_tf_init(dev, &tf); 4368 tf.command = ATA_CMD_INIT_DEV_PARAMS; 4369 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 4370 tf.protocol = ATA_PROT_NODATA; 4371 tf.nsect = sectors; 4372 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */ 4373 4374 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0); 4375 /* A clean abort indicates an original or just out of spec drive 4376 and we should continue as we issue the setup based on the 4377 drive reported working geometry */ 4378 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED)) 4379 err_mask = 0; 4380 4381 return err_mask; 4382 } 4383 4384 /** 4385 * atapi_check_dma - Check whether ATAPI DMA can be supported 4386 * @qc: Metadata associated with taskfile to check 4387 * 4388 * Allow low-level driver to filter ATA PACKET commands, returning 4389 * a status indicating whether or not it is OK to use DMA for the 4390 * supplied PACKET command. 4391 * 4392 * LOCKING: 4393 * spin_lock_irqsave(host lock) 4394 * 4395 * RETURNS: 0 when ATAPI DMA can be used 4396 * nonzero otherwise 4397 */ 4398 int atapi_check_dma(struct ata_queued_cmd *qc) 4399 { 4400 struct ata_port *ap = qc->ap; 4401 4402 /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a 4403 * few ATAPI devices choke on such DMA requests. 4404 */ 4405 if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) && 4406 unlikely(qc->nbytes & 15)) 4407 return 1; 4408 4409 if (ap->ops->check_atapi_dma) 4410 return ap->ops->check_atapi_dma(qc); 4411 4412 return 0; 4413 } 4414 4415 /** 4416 * ata_std_qc_defer - Check whether a qc needs to be deferred 4417 * @qc: ATA command in question 4418 * 4419 * Non-NCQ commands cannot run with any other command, NCQ or 4420 * not. As upper layer only knows the queue depth, we are 4421 * responsible for maintaining exclusion. This function checks 4422 * whether a new command @qc can be issued. 4423 * 4424 * LOCKING: 4425 * spin_lock_irqsave(host lock) 4426 * 4427 * RETURNS: 4428 * ATA_DEFER_* if deferring is needed, 0 otherwise. 4429 */ 4430 int ata_std_qc_defer(struct ata_queued_cmd *qc) 4431 { 4432 struct ata_link *link = qc->dev->link; 4433 4434 if (ata_is_ncq(qc->tf.protocol)) { 4435 if (!ata_tag_valid(link->active_tag)) 4436 return 0; 4437 } else { 4438 if (!ata_tag_valid(link->active_tag) && !link->sactive) 4439 return 0; 4440 } 4441 4442 return ATA_DEFER_LINK; 4443 } 4444 EXPORT_SYMBOL_GPL(ata_std_qc_defer); 4445 4446 enum ata_completion_errors ata_noop_qc_prep(struct ata_queued_cmd *qc) 4447 { 4448 return AC_ERR_OK; 4449 } 4450 EXPORT_SYMBOL_GPL(ata_noop_qc_prep); 4451 4452 /** 4453 * ata_sg_init - Associate command with scatter-gather table. 4454 * @qc: Command to be associated 4455 * @sg: Scatter-gather table. 4456 * @n_elem: Number of elements in s/g table. 4457 * 4458 * Initialize the data-related elements of queued_cmd @qc 4459 * to point to a scatter-gather table @sg, containing @n_elem 4460 * elements. 4461 * 4462 * LOCKING: 4463 * spin_lock_irqsave(host lock) 4464 */ 4465 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg, 4466 unsigned int n_elem) 4467 { 4468 qc->sg = sg; 4469 qc->n_elem = n_elem; 4470 qc->cursg = qc->sg; 4471 } 4472 4473 #ifdef CONFIG_HAS_DMA 4474 4475 /** 4476 * ata_sg_clean - Unmap DMA memory associated with command 4477 * @qc: Command containing DMA memory to be released 4478 * 4479 * Unmap all mapped DMA memory associated with this command. 4480 * 4481 * LOCKING: 4482 * spin_lock_irqsave(host lock) 4483 */ 4484 static void ata_sg_clean(struct ata_queued_cmd *qc) 4485 { 4486 struct ata_port *ap = qc->ap; 4487 struct scatterlist *sg = qc->sg; 4488 int dir = qc->dma_dir; 4489 4490 WARN_ON_ONCE(sg == NULL); 4491 4492 if (qc->n_elem) 4493 dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir); 4494 4495 qc->flags &= ~ATA_QCFLAG_DMAMAP; 4496 qc->sg = NULL; 4497 } 4498 4499 /** 4500 * ata_sg_setup - DMA-map the scatter-gather table associated with a command. 4501 * @qc: Command with scatter-gather table to be mapped. 4502 * 4503 * DMA-map the scatter-gather table associated with queued_cmd @qc. 4504 * 4505 * LOCKING: 4506 * spin_lock_irqsave(host lock) 4507 * 4508 * RETURNS: 4509 * Zero on success, negative on error. 4510 * 4511 */ 4512 static int ata_sg_setup(struct ata_queued_cmd *qc) 4513 { 4514 struct ata_port *ap = qc->ap; 4515 unsigned int n_elem; 4516 4517 n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir); 4518 if (n_elem < 1) 4519 return -1; 4520 4521 qc->orig_n_elem = qc->n_elem; 4522 qc->n_elem = n_elem; 4523 qc->flags |= ATA_QCFLAG_DMAMAP; 4524 4525 return 0; 4526 } 4527 4528 #else /* !CONFIG_HAS_DMA */ 4529 4530 static inline void ata_sg_clean(struct ata_queued_cmd *qc) {} 4531 static inline int ata_sg_setup(struct ata_queued_cmd *qc) { return -1; } 4532 4533 #endif /* !CONFIG_HAS_DMA */ 4534 4535 /** 4536 * swap_buf_le16 - swap halves of 16-bit words in place 4537 * @buf: Buffer to swap 4538 * @buf_words: Number of 16-bit words in buffer. 4539 * 4540 * Swap halves of 16-bit words if needed to convert from 4541 * little-endian byte order to native cpu byte order, or 4542 * vice-versa. 4543 * 4544 * LOCKING: 4545 * Inherited from caller. 4546 */ 4547 void swap_buf_le16(u16 *buf, unsigned int buf_words) 4548 { 4549 #ifdef __BIG_ENDIAN 4550 unsigned int i; 4551 4552 for (i = 0; i < buf_words; i++) 4553 buf[i] = le16_to_cpu(buf[i]); 4554 #endif /* __BIG_ENDIAN */ 4555 } 4556 4557 /** 4558 * ata_qc_new_init - Request an available ATA command, and initialize it 4559 * @dev: Device from whom we request an available command structure 4560 * @tag: tag 4561 * 4562 * LOCKING: 4563 * None. 4564 */ 4565 4566 struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev, int tag) 4567 { 4568 struct ata_port *ap = dev->link->ap; 4569 struct ata_queued_cmd *qc; 4570 4571 /* no command while frozen */ 4572 if (unlikely(ap->pflags & ATA_PFLAG_FROZEN)) 4573 return NULL; 4574 4575 /* libsas case */ 4576 if (ap->flags & ATA_FLAG_SAS_HOST) { 4577 tag = ata_sas_allocate_tag(ap); 4578 if (tag < 0) 4579 return NULL; 4580 } 4581 4582 qc = __ata_qc_from_tag(ap, tag); 4583 qc->tag = qc->hw_tag = tag; 4584 qc->scsicmd = NULL; 4585 qc->ap = ap; 4586 qc->dev = dev; 4587 4588 ata_qc_reinit(qc); 4589 4590 return qc; 4591 } 4592 4593 /** 4594 * ata_qc_free - free unused ata_queued_cmd 4595 * @qc: Command to complete 4596 * 4597 * Designed to free unused ata_queued_cmd object 4598 * in case something prevents using it. 4599 * 4600 * LOCKING: 4601 * spin_lock_irqsave(host lock) 4602 */ 4603 void ata_qc_free(struct ata_queued_cmd *qc) 4604 { 4605 struct ata_port *ap; 4606 unsigned int tag; 4607 4608 WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */ 4609 ap = qc->ap; 4610 4611 qc->flags = 0; 4612 tag = qc->tag; 4613 if (ata_tag_valid(tag)) { 4614 qc->tag = ATA_TAG_POISON; 4615 if (ap->flags & ATA_FLAG_SAS_HOST) 4616 ata_sas_free_tag(tag, ap); 4617 } 4618 } 4619 4620 void __ata_qc_complete(struct ata_queued_cmd *qc) 4621 { 4622 struct ata_port *ap; 4623 struct ata_link *link; 4624 4625 WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */ 4626 WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE)); 4627 ap = qc->ap; 4628 link = qc->dev->link; 4629 4630 if (likely(qc->flags & ATA_QCFLAG_DMAMAP)) 4631 ata_sg_clean(qc); 4632 4633 /* command should be marked inactive atomically with qc completion */ 4634 if (ata_is_ncq(qc->tf.protocol)) { 4635 link->sactive &= ~(1 << qc->hw_tag); 4636 if (!link->sactive) 4637 ap->nr_active_links--; 4638 } else { 4639 link->active_tag = ATA_TAG_POISON; 4640 ap->nr_active_links--; 4641 } 4642 4643 /* clear exclusive status */ 4644 if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL && 4645 ap->excl_link == link)) 4646 ap->excl_link = NULL; 4647 4648 /* atapi: mark qc as inactive to prevent the interrupt handler 4649 * from completing the command twice later, before the error handler 4650 * is called. (when rc != 0 and atapi request sense is needed) 4651 */ 4652 qc->flags &= ~ATA_QCFLAG_ACTIVE; 4653 ap->qc_active &= ~(1ULL << qc->tag); 4654 4655 /* call completion callback */ 4656 qc->complete_fn(qc); 4657 } 4658 4659 static void fill_result_tf(struct ata_queued_cmd *qc) 4660 { 4661 struct ata_port *ap = qc->ap; 4662 4663 qc->result_tf.flags = qc->tf.flags; 4664 ap->ops->qc_fill_rtf(qc); 4665 } 4666 4667 static void ata_verify_xfer(struct ata_queued_cmd *qc) 4668 { 4669 struct ata_device *dev = qc->dev; 4670 4671 if (!ata_is_data(qc->tf.protocol)) 4672 return; 4673 4674 if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol)) 4675 return; 4676 4677 dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER; 4678 } 4679 4680 /** 4681 * ata_qc_complete - Complete an active ATA command 4682 * @qc: Command to complete 4683 * 4684 * Indicate to the mid and upper layers that an ATA command has 4685 * completed, with either an ok or not-ok status. 4686 * 4687 * Refrain from calling this function multiple times when 4688 * successfully completing multiple NCQ commands. 4689 * ata_qc_complete_multiple() should be used instead, which will 4690 * properly update IRQ expect state. 4691 * 4692 * LOCKING: 4693 * spin_lock_irqsave(host lock) 4694 */ 4695 void ata_qc_complete(struct ata_queued_cmd *qc) 4696 { 4697 struct ata_port *ap = qc->ap; 4698 4699 /* Trigger the LED (if available) */ 4700 ledtrig_disk_activity(!!(qc->tf.flags & ATA_TFLAG_WRITE)); 4701 4702 /* XXX: New EH and old EH use different mechanisms to 4703 * synchronize EH with regular execution path. 4704 * 4705 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED. 4706 * Normal execution path is responsible for not accessing a 4707 * failed qc. libata core enforces the rule by returning NULL 4708 * from ata_qc_from_tag() for failed qcs. 4709 * 4710 * Old EH depends on ata_qc_complete() nullifying completion 4711 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does 4712 * not synchronize with interrupt handler. Only PIO task is 4713 * taken care of. 4714 */ 4715 if (ap->ops->error_handler) { 4716 struct ata_device *dev = qc->dev; 4717 struct ata_eh_info *ehi = &dev->link->eh_info; 4718 4719 if (unlikely(qc->err_mask)) 4720 qc->flags |= ATA_QCFLAG_FAILED; 4721 4722 /* 4723 * Finish internal commands without any further processing 4724 * and always with the result TF filled. 4725 */ 4726 if (unlikely(ata_tag_internal(qc->tag))) { 4727 fill_result_tf(qc); 4728 trace_ata_qc_complete_internal(qc); 4729 __ata_qc_complete(qc); 4730 return; 4731 } 4732 4733 /* 4734 * Non-internal qc has failed. Fill the result TF and 4735 * summon EH. 4736 */ 4737 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) { 4738 fill_result_tf(qc); 4739 trace_ata_qc_complete_failed(qc); 4740 ata_qc_schedule_eh(qc); 4741 return; 4742 } 4743 4744 WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN); 4745 4746 /* read result TF if requested */ 4747 if (qc->flags & ATA_QCFLAG_RESULT_TF) 4748 fill_result_tf(qc); 4749 4750 trace_ata_qc_complete_done(qc); 4751 /* Some commands need post-processing after successful 4752 * completion. 4753 */ 4754 switch (qc->tf.command) { 4755 case ATA_CMD_SET_FEATURES: 4756 if (qc->tf.feature != SETFEATURES_WC_ON && 4757 qc->tf.feature != SETFEATURES_WC_OFF && 4758 qc->tf.feature != SETFEATURES_RA_ON && 4759 qc->tf.feature != SETFEATURES_RA_OFF) 4760 break; 4761 fallthrough; 4762 case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */ 4763 case ATA_CMD_SET_MULTI: /* multi_count changed */ 4764 /* revalidate device */ 4765 ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE; 4766 ata_port_schedule_eh(ap); 4767 break; 4768 4769 case ATA_CMD_SLEEP: 4770 dev->flags |= ATA_DFLAG_SLEEPING; 4771 break; 4772 } 4773 4774 if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) 4775 ata_verify_xfer(qc); 4776 4777 __ata_qc_complete(qc); 4778 } else { 4779 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED) 4780 return; 4781 4782 /* read result TF if failed or requested */ 4783 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF) 4784 fill_result_tf(qc); 4785 4786 __ata_qc_complete(qc); 4787 } 4788 } 4789 EXPORT_SYMBOL_GPL(ata_qc_complete); 4790 4791 /** 4792 * ata_qc_get_active - get bitmask of active qcs 4793 * @ap: port in question 4794 * 4795 * LOCKING: 4796 * spin_lock_irqsave(host lock) 4797 * 4798 * RETURNS: 4799 * Bitmask of active qcs 4800 */ 4801 u64 ata_qc_get_active(struct ata_port *ap) 4802 { 4803 u64 qc_active = ap->qc_active; 4804 4805 /* ATA_TAG_INTERNAL is sent to hw as tag 0 */ 4806 if (qc_active & (1ULL << ATA_TAG_INTERNAL)) { 4807 qc_active |= (1 << 0); 4808 qc_active &= ~(1ULL << ATA_TAG_INTERNAL); 4809 } 4810 4811 return qc_active; 4812 } 4813 EXPORT_SYMBOL_GPL(ata_qc_get_active); 4814 4815 /** 4816 * ata_qc_issue - issue taskfile to device 4817 * @qc: command to issue to device 4818 * 4819 * Prepare an ATA command to submission to device. 4820 * This includes mapping the data into a DMA-able 4821 * area, filling in the S/G table, and finally 4822 * writing the taskfile to hardware, starting the command. 4823 * 4824 * LOCKING: 4825 * spin_lock_irqsave(host lock) 4826 */ 4827 void ata_qc_issue(struct ata_queued_cmd *qc) 4828 { 4829 struct ata_port *ap = qc->ap; 4830 struct ata_link *link = qc->dev->link; 4831 u8 prot = qc->tf.protocol; 4832 4833 /* Make sure only one non-NCQ command is outstanding. The 4834 * check is skipped for old EH because it reuses active qc to 4835 * request ATAPI sense. 4836 */ 4837 WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag)); 4838 4839 if (ata_is_ncq(prot)) { 4840 WARN_ON_ONCE(link->sactive & (1 << qc->hw_tag)); 4841 4842 if (!link->sactive) 4843 ap->nr_active_links++; 4844 link->sactive |= 1 << qc->hw_tag; 4845 } else { 4846 WARN_ON_ONCE(link->sactive); 4847 4848 ap->nr_active_links++; 4849 link->active_tag = qc->tag; 4850 } 4851 4852 qc->flags |= ATA_QCFLAG_ACTIVE; 4853 ap->qc_active |= 1ULL << qc->tag; 4854 4855 /* 4856 * We guarantee to LLDs that they will have at least one 4857 * non-zero sg if the command is a data command. 4858 */ 4859 if (ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes)) 4860 goto sys_err; 4861 4862 if (ata_is_dma(prot) || (ata_is_pio(prot) && 4863 (ap->flags & ATA_FLAG_PIO_DMA))) 4864 if (ata_sg_setup(qc)) 4865 goto sys_err; 4866 4867 /* if device is sleeping, schedule reset and abort the link */ 4868 if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) { 4869 link->eh_info.action |= ATA_EH_RESET; 4870 ata_ehi_push_desc(&link->eh_info, "waking up from sleep"); 4871 ata_link_abort(link); 4872 return; 4873 } 4874 4875 trace_ata_qc_prep(qc); 4876 qc->err_mask |= ap->ops->qc_prep(qc); 4877 if (unlikely(qc->err_mask)) 4878 goto err; 4879 trace_ata_qc_issue(qc); 4880 qc->err_mask |= ap->ops->qc_issue(qc); 4881 if (unlikely(qc->err_mask)) 4882 goto err; 4883 return; 4884 4885 sys_err: 4886 qc->err_mask |= AC_ERR_SYSTEM; 4887 err: 4888 ata_qc_complete(qc); 4889 } 4890 4891 /** 4892 * ata_phys_link_online - test whether the given link is online 4893 * @link: ATA link to test 4894 * 4895 * Test whether @link is online. Note that this function returns 4896 * 0 if online status of @link cannot be obtained, so 4897 * ata_link_online(link) != !ata_link_offline(link). 4898 * 4899 * LOCKING: 4900 * None. 4901 * 4902 * RETURNS: 4903 * True if the port online status is available and online. 4904 */ 4905 bool ata_phys_link_online(struct ata_link *link) 4906 { 4907 u32 sstatus; 4908 4909 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 && 4910 ata_sstatus_online(sstatus)) 4911 return true; 4912 return false; 4913 } 4914 4915 /** 4916 * ata_phys_link_offline - test whether the given link is offline 4917 * @link: ATA link to test 4918 * 4919 * Test whether @link is offline. Note that this function 4920 * returns 0 if offline status of @link cannot be obtained, so 4921 * ata_link_online(link) != !ata_link_offline(link). 4922 * 4923 * LOCKING: 4924 * None. 4925 * 4926 * RETURNS: 4927 * True if the port offline status is available and offline. 4928 */ 4929 bool ata_phys_link_offline(struct ata_link *link) 4930 { 4931 u32 sstatus; 4932 4933 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 && 4934 !ata_sstatus_online(sstatus)) 4935 return true; 4936 return false; 4937 } 4938 4939 /** 4940 * ata_link_online - test whether the given link is online 4941 * @link: ATA link to test 4942 * 4943 * Test whether @link is online. This is identical to 4944 * ata_phys_link_online() when there's no slave link. When 4945 * there's a slave link, this function should only be called on 4946 * the master link and will return true if any of M/S links is 4947 * online. 4948 * 4949 * LOCKING: 4950 * None. 4951 * 4952 * RETURNS: 4953 * True if the port online status is available and online. 4954 */ 4955 bool ata_link_online(struct ata_link *link) 4956 { 4957 struct ata_link *slave = link->ap->slave_link; 4958 4959 WARN_ON(link == slave); /* shouldn't be called on slave link */ 4960 4961 return ata_phys_link_online(link) || 4962 (slave && ata_phys_link_online(slave)); 4963 } 4964 EXPORT_SYMBOL_GPL(ata_link_online); 4965 4966 /** 4967 * ata_link_offline - test whether the given link is offline 4968 * @link: ATA link to test 4969 * 4970 * Test whether @link is offline. This is identical to 4971 * ata_phys_link_offline() when there's no slave link. When 4972 * there's a slave link, this function should only be called on 4973 * the master link and will return true if both M/S links are 4974 * offline. 4975 * 4976 * LOCKING: 4977 * None. 4978 * 4979 * RETURNS: 4980 * True if the port offline status is available and offline. 4981 */ 4982 bool ata_link_offline(struct ata_link *link) 4983 { 4984 struct ata_link *slave = link->ap->slave_link; 4985 4986 WARN_ON(link == slave); /* shouldn't be called on slave link */ 4987 4988 return ata_phys_link_offline(link) && 4989 (!slave || ata_phys_link_offline(slave)); 4990 } 4991 EXPORT_SYMBOL_GPL(ata_link_offline); 4992 4993 #ifdef CONFIG_PM 4994 static void ata_port_request_pm(struct ata_port *ap, pm_message_t mesg, 4995 unsigned int action, unsigned int ehi_flags, 4996 bool async) 4997 { 4998 struct ata_link *link; 4999 unsigned long flags; 5000 5001 /* Previous resume operation might still be in 5002 * progress. Wait for PM_PENDING to clear. 5003 */ 5004 if (ap->pflags & ATA_PFLAG_PM_PENDING) { 5005 ata_port_wait_eh(ap); 5006 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING); 5007 } 5008 5009 /* request PM ops to EH */ 5010 spin_lock_irqsave(ap->lock, flags); 5011 5012 ap->pm_mesg = mesg; 5013 ap->pflags |= ATA_PFLAG_PM_PENDING; 5014 ata_for_each_link(link, ap, HOST_FIRST) { 5015 link->eh_info.action |= action; 5016 link->eh_info.flags |= ehi_flags; 5017 } 5018 5019 ata_port_schedule_eh(ap); 5020 5021 spin_unlock_irqrestore(ap->lock, flags); 5022 5023 if (!async) { 5024 ata_port_wait_eh(ap); 5025 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING); 5026 } 5027 } 5028 5029 /* 5030 * On some hardware, device fails to respond after spun down for suspend. As 5031 * the device won't be used before being resumed, we don't need to touch the 5032 * device. Ask EH to skip the usual stuff and proceed directly to suspend. 5033 * 5034 * http://thread.gmane.org/gmane.linux.ide/46764 5035 */ 5036 static const unsigned int ata_port_suspend_ehi = ATA_EHI_QUIET 5037 | ATA_EHI_NO_AUTOPSY 5038 | ATA_EHI_NO_RECOVERY; 5039 5040 static void ata_port_suspend(struct ata_port *ap, pm_message_t mesg) 5041 { 5042 ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, false); 5043 } 5044 5045 static void ata_port_suspend_async(struct ata_port *ap, pm_message_t mesg) 5046 { 5047 ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, true); 5048 } 5049 5050 static int ata_port_pm_suspend(struct device *dev) 5051 { 5052 struct ata_port *ap = to_ata_port(dev); 5053 5054 if (pm_runtime_suspended(dev)) 5055 return 0; 5056 5057 ata_port_suspend(ap, PMSG_SUSPEND); 5058 return 0; 5059 } 5060 5061 static int ata_port_pm_freeze(struct device *dev) 5062 { 5063 struct ata_port *ap = to_ata_port(dev); 5064 5065 if (pm_runtime_suspended(dev)) 5066 return 0; 5067 5068 ata_port_suspend(ap, PMSG_FREEZE); 5069 return 0; 5070 } 5071 5072 static int ata_port_pm_poweroff(struct device *dev) 5073 { 5074 ata_port_suspend(to_ata_port(dev), PMSG_HIBERNATE); 5075 return 0; 5076 } 5077 5078 static const unsigned int ata_port_resume_ehi = ATA_EHI_NO_AUTOPSY 5079 | ATA_EHI_QUIET; 5080 5081 static void ata_port_resume(struct ata_port *ap, pm_message_t mesg) 5082 { 5083 ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, false); 5084 } 5085 5086 static void ata_port_resume_async(struct ata_port *ap, pm_message_t mesg) 5087 { 5088 ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, true); 5089 } 5090 5091 static int ata_port_pm_resume(struct device *dev) 5092 { 5093 ata_port_resume_async(to_ata_port(dev), PMSG_RESUME); 5094 pm_runtime_disable(dev); 5095 pm_runtime_set_active(dev); 5096 pm_runtime_enable(dev); 5097 return 0; 5098 } 5099 5100 /* 5101 * For ODDs, the upper layer will poll for media change every few seconds, 5102 * which will make it enter and leave suspend state every few seconds. And 5103 * as each suspend will cause a hard/soft reset, the gain of runtime suspend 5104 * is very little and the ODD may malfunction after constantly being reset. 5105 * So the idle callback here will not proceed to suspend if a non-ZPODD capable 5106 * ODD is attached to the port. 5107 */ 5108 static int ata_port_runtime_idle(struct device *dev) 5109 { 5110 struct ata_port *ap = to_ata_port(dev); 5111 struct ata_link *link; 5112 struct ata_device *adev; 5113 5114 ata_for_each_link(link, ap, HOST_FIRST) { 5115 ata_for_each_dev(adev, link, ENABLED) 5116 if (adev->class == ATA_DEV_ATAPI && 5117 !zpodd_dev_enabled(adev)) 5118 return -EBUSY; 5119 } 5120 5121 return 0; 5122 } 5123 5124 static int ata_port_runtime_suspend(struct device *dev) 5125 { 5126 ata_port_suspend(to_ata_port(dev), PMSG_AUTO_SUSPEND); 5127 return 0; 5128 } 5129 5130 static int ata_port_runtime_resume(struct device *dev) 5131 { 5132 ata_port_resume(to_ata_port(dev), PMSG_AUTO_RESUME); 5133 return 0; 5134 } 5135 5136 static const struct dev_pm_ops ata_port_pm_ops = { 5137 .suspend = ata_port_pm_suspend, 5138 .resume = ata_port_pm_resume, 5139 .freeze = ata_port_pm_freeze, 5140 .thaw = ata_port_pm_resume, 5141 .poweroff = ata_port_pm_poweroff, 5142 .restore = ata_port_pm_resume, 5143 5144 .runtime_suspend = ata_port_runtime_suspend, 5145 .runtime_resume = ata_port_runtime_resume, 5146 .runtime_idle = ata_port_runtime_idle, 5147 }; 5148 5149 /* sas ports don't participate in pm runtime management of ata_ports, 5150 * and need to resume ata devices at the domain level, not the per-port 5151 * level. sas suspend/resume is async to allow parallel port recovery 5152 * since sas has multiple ata_port instances per Scsi_Host. 5153 */ 5154 void ata_sas_port_suspend(struct ata_port *ap) 5155 { 5156 ata_port_suspend_async(ap, PMSG_SUSPEND); 5157 } 5158 EXPORT_SYMBOL_GPL(ata_sas_port_suspend); 5159 5160 void ata_sas_port_resume(struct ata_port *ap) 5161 { 5162 ata_port_resume_async(ap, PMSG_RESUME); 5163 } 5164 EXPORT_SYMBOL_GPL(ata_sas_port_resume); 5165 5166 /** 5167 * ata_host_suspend - suspend host 5168 * @host: host to suspend 5169 * @mesg: PM message 5170 * 5171 * Suspend @host. Actual operation is performed by port suspend. 5172 */ 5173 int ata_host_suspend(struct ata_host *host, pm_message_t mesg) 5174 { 5175 host->dev->power.power_state = mesg; 5176 return 0; 5177 } 5178 EXPORT_SYMBOL_GPL(ata_host_suspend); 5179 5180 /** 5181 * ata_host_resume - resume host 5182 * @host: host to resume 5183 * 5184 * Resume @host. Actual operation is performed by port resume. 5185 */ 5186 void ata_host_resume(struct ata_host *host) 5187 { 5188 host->dev->power.power_state = PMSG_ON; 5189 } 5190 EXPORT_SYMBOL_GPL(ata_host_resume); 5191 #endif 5192 5193 const struct device_type ata_port_type = { 5194 .name = "ata_port", 5195 #ifdef CONFIG_PM 5196 .pm = &ata_port_pm_ops, 5197 #endif 5198 }; 5199 5200 /** 5201 * ata_dev_init - Initialize an ata_device structure 5202 * @dev: Device structure to initialize 5203 * 5204 * Initialize @dev in preparation for probing. 5205 * 5206 * LOCKING: 5207 * Inherited from caller. 5208 */ 5209 void ata_dev_init(struct ata_device *dev) 5210 { 5211 struct ata_link *link = ata_dev_phys_link(dev); 5212 struct ata_port *ap = link->ap; 5213 unsigned long flags; 5214 5215 /* SATA spd limit is bound to the attached device, reset together */ 5216 link->sata_spd_limit = link->hw_sata_spd_limit; 5217 link->sata_spd = 0; 5218 5219 /* High bits of dev->flags are used to record warm plug 5220 * requests which occur asynchronously. Synchronize using 5221 * host lock. 5222 */ 5223 spin_lock_irqsave(ap->lock, flags); 5224 dev->flags &= ~ATA_DFLAG_INIT_MASK; 5225 dev->horkage = 0; 5226 spin_unlock_irqrestore(ap->lock, flags); 5227 5228 memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0, 5229 ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN); 5230 dev->pio_mask = UINT_MAX; 5231 dev->mwdma_mask = UINT_MAX; 5232 dev->udma_mask = UINT_MAX; 5233 } 5234 5235 /** 5236 * ata_link_init - Initialize an ata_link structure 5237 * @ap: ATA port link is attached to 5238 * @link: Link structure to initialize 5239 * @pmp: Port multiplier port number 5240 * 5241 * Initialize @link. 5242 * 5243 * LOCKING: 5244 * Kernel thread context (may sleep) 5245 */ 5246 void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp) 5247 { 5248 int i; 5249 5250 /* clear everything except for devices */ 5251 memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0, 5252 ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN); 5253 5254 link->ap = ap; 5255 link->pmp = pmp; 5256 link->active_tag = ATA_TAG_POISON; 5257 link->hw_sata_spd_limit = UINT_MAX; 5258 5259 /* can't use iterator, ap isn't initialized yet */ 5260 for (i = 0; i < ATA_MAX_DEVICES; i++) { 5261 struct ata_device *dev = &link->device[i]; 5262 5263 dev->link = link; 5264 dev->devno = dev - link->device; 5265 #ifdef CONFIG_ATA_ACPI 5266 dev->gtf_filter = ata_acpi_gtf_filter; 5267 #endif 5268 ata_dev_init(dev); 5269 } 5270 } 5271 5272 /** 5273 * sata_link_init_spd - Initialize link->sata_spd_limit 5274 * @link: Link to configure sata_spd_limit for 5275 * 5276 * Initialize ``link->[hw_]sata_spd_limit`` to the currently 5277 * configured value. 5278 * 5279 * LOCKING: 5280 * Kernel thread context (may sleep). 5281 * 5282 * RETURNS: 5283 * 0 on success, -errno on failure. 5284 */ 5285 int sata_link_init_spd(struct ata_link *link) 5286 { 5287 u8 spd; 5288 int rc; 5289 5290 rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol); 5291 if (rc) 5292 return rc; 5293 5294 spd = (link->saved_scontrol >> 4) & 0xf; 5295 if (spd) 5296 link->hw_sata_spd_limit &= (1 << spd) - 1; 5297 5298 ata_force_link_limits(link); 5299 5300 link->sata_spd_limit = link->hw_sata_spd_limit; 5301 5302 return 0; 5303 } 5304 5305 /** 5306 * ata_port_alloc - allocate and initialize basic ATA port resources 5307 * @host: ATA host this allocated port belongs to 5308 * 5309 * Allocate and initialize basic ATA port resources. 5310 * 5311 * RETURNS: 5312 * Allocate ATA port on success, NULL on failure. 5313 * 5314 * LOCKING: 5315 * Inherited from calling layer (may sleep). 5316 */ 5317 struct ata_port *ata_port_alloc(struct ata_host *host) 5318 { 5319 struct ata_port *ap; 5320 5321 ap = kzalloc(sizeof(*ap), GFP_KERNEL); 5322 if (!ap) 5323 return NULL; 5324 5325 ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN; 5326 ap->lock = &host->lock; 5327 ap->print_id = -1; 5328 ap->local_port_no = -1; 5329 ap->host = host; 5330 ap->dev = host->dev; 5331 5332 mutex_init(&ap->scsi_scan_mutex); 5333 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug); 5334 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan); 5335 INIT_LIST_HEAD(&ap->eh_done_q); 5336 init_waitqueue_head(&ap->eh_wait_q); 5337 init_completion(&ap->park_req_pending); 5338 timer_setup(&ap->fastdrain_timer, ata_eh_fastdrain_timerfn, 5339 TIMER_DEFERRABLE); 5340 5341 ap->cbl = ATA_CBL_NONE; 5342 5343 ata_link_init(ap, &ap->link, 0); 5344 5345 #ifdef ATA_IRQ_TRAP 5346 ap->stats.unhandled_irq = 1; 5347 ap->stats.idle_irq = 1; 5348 #endif 5349 ata_sff_port_init(ap); 5350 5351 return ap; 5352 } 5353 5354 static void ata_devres_release(struct device *gendev, void *res) 5355 { 5356 struct ata_host *host = dev_get_drvdata(gendev); 5357 int i; 5358 5359 for (i = 0; i < host->n_ports; i++) { 5360 struct ata_port *ap = host->ports[i]; 5361 5362 if (!ap) 5363 continue; 5364 5365 if (ap->scsi_host) 5366 scsi_host_put(ap->scsi_host); 5367 5368 } 5369 5370 dev_set_drvdata(gendev, NULL); 5371 ata_host_put(host); 5372 } 5373 5374 static void ata_host_release(struct kref *kref) 5375 { 5376 struct ata_host *host = container_of(kref, struct ata_host, kref); 5377 int i; 5378 5379 for (i = 0; i < host->n_ports; i++) { 5380 struct ata_port *ap = host->ports[i]; 5381 5382 kfree(ap->pmp_link); 5383 kfree(ap->slave_link); 5384 kfree(ap); 5385 host->ports[i] = NULL; 5386 } 5387 kfree(host); 5388 } 5389 5390 void ata_host_get(struct ata_host *host) 5391 { 5392 kref_get(&host->kref); 5393 } 5394 5395 void ata_host_put(struct ata_host *host) 5396 { 5397 kref_put(&host->kref, ata_host_release); 5398 } 5399 EXPORT_SYMBOL_GPL(ata_host_put); 5400 5401 /** 5402 * ata_host_alloc - allocate and init basic ATA host resources 5403 * @dev: generic device this host is associated with 5404 * @max_ports: maximum number of ATA ports associated with this host 5405 * 5406 * Allocate and initialize basic ATA host resources. LLD calls 5407 * this function to allocate a host, initializes it fully and 5408 * attaches it using ata_host_register(). 5409 * 5410 * @max_ports ports are allocated and host->n_ports is 5411 * initialized to @max_ports. The caller is allowed to decrease 5412 * host->n_ports before calling ata_host_register(). The unused 5413 * ports will be automatically freed on registration. 5414 * 5415 * RETURNS: 5416 * Allocate ATA host on success, NULL on failure. 5417 * 5418 * LOCKING: 5419 * Inherited from calling layer (may sleep). 5420 */ 5421 struct ata_host *ata_host_alloc(struct device *dev, int max_ports) 5422 { 5423 struct ata_host *host; 5424 size_t sz; 5425 int i; 5426 void *dr; 5427 5428 /* alloc a container for our list of ATA ports (buses) */ 5429 sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *); 5430 host = kzalloc(sz, GFP_KERNEL); 5431 if (!host) 5432 return NULL; 5433 5434 if (!devres_open_group(dev, NULL, GFP_KERNEL)) 5435 goto err_free; 5436 5437 dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL); 5438 if (!dr) 5439 goto err_out; 5440 5441 devres_add(dev, dr); 5442 dev_set_drvdata(dev, host); 5443 5444 spin_lock_init(&host->lock); 5445 mutex_init(&host->eh_mutex); 5446 host->dev = dev; 5447 host->n_ports = max_ports; 5448 kref_init(&host->kref); 5449 5450 /* allocate ports bound to this host */ 5451 for (i = 0; i < max_ports; i++) { 5452 struct ata_port *ap; 5453 5454 ap = ata_port_alloc(host); 5455 if (!ap) 5456 goto err_out; 5457 5458 ap->port_no = i; 5459 host->ports[i] = ap; 5460 } 5461 5462 devres_remove_group(dev, NULL); 5463 return host; 5464 5465 err_out: 5466 devres_release_group(dev, NULL); 5467 err_free: 5468 kfree(host); 5469 return NULL; 5470 } 5471 EXPORT_SYMBOL_GPL(ata_host_alloc); 5472 5473 /** 5474 * ata_host_alloc_pinfo - alloc host and init with port_info array 5475 * @dev: generic device this host is associated with 5476 * @ppi: array of ATA port_info to initialize host with 5477 * @n_ports: number of ATA ports attached to this host 5478 * 5479 * Allocate ATA host and initialize with info from @ppi. If NULL 5480 * terminated, @ppi may contain fewer entries than @n_ports. The 5481 * last entry will be used for the remaining ports. 5482 * 5483 * RETURNS: 5484 * Allocate ATA host on success, NULL on failure. 5485 * 5486 * LOCKING: 5487 * Inherited from calling layer (may sleep). 5488 */ 5489 struct ata_host *ata_host_alloc_pinfo(struct device *dev, 5490 const struct ata_port_info * const * ppi, 5491 int n_ports) 5492 { 5493 const struct ata_port_info *pi; 5494 struct ata_host *host; 5495 int i, j; 5496 5497 host = ata_host_alloc(dev, n_ports); 5498 if (!host) 5499 return NULL; 5500 5501 for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) { 5502 struct ata_port *ap = host->ports[i]; 5503 5504 if (ppi[j]) 5505 pi = ppi[j++]; 5506 5507 ap->pio_mask = pi->pio_mask; 5508 ap->mwdma_mask = pi->mwdma_mask; 5509 ap->udma_mask = pi->udma_mask; 5510 ap->flags |= pi->flags; 5511 ap->link.flags |= pi->link_flags; 5512 ap->ops = pi->port_ops; 5513 5514 if (!host->ops && (pi->port_ops != &ata_dummy_port_ops)) 5515 host->ops = pi->port_ops; 5516 } 5517 5518 return host; 5519 } 5520 EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo); 5521 5522 static void ata_host_stop(struct device *gendev, void *res) 5523 { 5524 struct ata_host *host = dev_get_drvdata(gendev); 5525 int i; 5526 5527 WARN_ON(!(host->flags & ATA_HOST_STARTED)); 5528 5529 for (i = 0; i < host->n_ports; i++) { 5530 struct ata_port *ap = host->ports[i]; 5531 5532 if (ap->ops->port_stop) 5533 ap->ops->port_stop(ap); 5534 } 5535 5536 if (host->ops->host_stop) 5537 host->ops->host_stop(host); 5538 } 5539 5540 /** 5541 * ata_finalize_port_ops - finalize ata_port_operations 5542 * @ops: ata_port_operations to finalize 5543 * 5544 * An ata_port_operations can inherit from another ops and that 5545 * ops can again inherit from another. This can go on as many 5546 * times as necessary as long as there is no loop in the 5547 * inheritance chain. 5548 * 5549 * Ops tables are finalized when the host is started. NULL or 5550 * unspecified entries are inherited from the closet ancestor 5551 * which has the method and the entry is populated with it. 5552 * After finalization, the ops table directly points to all the 5553 * methods and ->inherits is no longer necessary and cleared. 5554 * 5555 * Using ATA_OP_NULL, inheriting ops can force a method to NULL. 5556 * 5557 * LOCKING: 5558 * None. 5559 */ 5560 static void ata_finalize_port_ops(struct ata_port_operations *ops) 5561 { 5562 static DEFINE_SPINLOCK(lock); 5563 const struct ata_port_operations *cur; 5564 void **begin = (void **)ops; 5565 void **end = (void **)&ops->inherits; 5566 void **pp; 5567 5568 if (!ops || !ops->inherits) 5569 return; 5570 5571 spin_lock(&lock); 5572 5573 for (cur = ops->inherits; cur; cur = cur->inherits) { 5574 void **inherit = (void **)cur; 5575 5576 for (pp = begin; pp < end; pp++, inherit++) 5577 if (!*pp) 5578 *pp = *inherit; 5579 } 5580 5581 for (pp = begin; pp < end; pp++) 5582 if (IS_ERR(*pp)) 5583 *pp = NULL; 5584 5585 ops->inherits = NULL; 5586 5587 spin_unlock(&lock); 5588 } 5589 5590 /** 5591 * ata_host_start - start and freeze ports of an ATA host 5592 * @host: ATA host to start ports for 5593 * 5594 * Start and then freeze ports of @host. Started status is 5595 * recorded in host->flags, so this function can be called 5596 * multiple times. Ports are guaranteed to get started only 5597 * once. If host->ops isn't initialized yet, its set to the 5598 * first non-dummy port ops. 5599 * 5600 * LOCKING: 5601 * Inherited from calling layer (may sleep). 5602 * 5603 * RETURNS: 5604 * 0 if all ports are started successfully, -errno otherwise. 5605 */ 5606 int ata_host_start(struct ata_host *host) 5607 { 5608 int have_stop = 0; 5609 void *start_dr = NULL; 5610 int i, rc; 5611 5612 if (host->flags & ATA_HOST_STARTED) 5613 return 0; 5614 5615 ata_finalize_port_ops(host->ops); 5616 5617 for (i = 0; i < host->n_ports; i++) { 5618 struct ata_port *ap = host->ports[i]; 5619 5620 ata_finalize_port_ops(ap->ops); 5621 5622 if (!host->ops && !ata_port_is_dummy(ap)) 5623 host->ops = ap->ops; 5624 5625 if (ap->ops->port_stop) 5626 have_stop = 1; 5627 } 5628 5629 if (host->ops && host->ops->host_stop) 5630 have_stop = 1; 5631 5632 if (have_stop) { 5633 start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL); 5634 if (!start_dr) 5635 return -ENOMEM; 5636 } 5637 5638 for (i = 0; i < host->n_ports; i++) { 5639 struct ata_port *ap = host->ports[i]; 5640 5641 if (ap->ops->port_start) { 5642 rc = ap->ops->port_start(ap); 5643 if (rc) { 5644 if (rc != -ENODEV) 5645 dev_err(host->dev, 5646 "failed to start port %d (errno=%d)\n", 5647 i, rc); 5648 goto err_out; 5649 } 5650 } 5651 ata_eh_freeze_port(ap); 5652 } 5653 5654 if (start_dr) 5655 devres_add(host->dev, start_dr); 5656 host->flags |= ATA_HOST_STARTED; 5657 return 0; 5658 5659 err_out: 5660 while (--i >= 0) { 5661 struct ata_port *ap = host->ports[i]; 5662 5663 if (ap->ops->port_stop) 5664 ap->ops->port_stop(ap); 5665 } 5666 devres_free(start_dr); 5667 return rc; 5668 } 5669 EXPORT_SYMBOL_GPL(ata_host_start); 5670 5671 /** 5672 * ata_host_init - Initialize a host struct for sas (ipr, libsas) 5673 * @host: host to initialize 5674 * @dev: device host is attached to 5675 * @ops: port_ops 5676 * 5677 */ 5678 void ata_host_init(struct ata_host *host, struct device *dev, 5679 struct ata_port_operations *ops) 5680 { 5681 spin_lock_init(&host->lock); 5682 mutex_init(&host->eh_mutex); 5683 host->n_tags = ATA_MAX_QUEUE; 5684 host->dev = dev; 5685 host->ops = ops; 5686 kref_init(&host->kref); 5687 } 5688 EXPORT_SYMBOL_GPL(ata_host_init); 5689 5690 void __ata_port_probe(struct ata_port *ap) 5691 { 5692 struct ata_eh_info *ehi = &ap->link.eh_info; 5693 unsigned long flags; 5694 5695 /* kick EH for boot probing */ 5696 spin_lock_irqsave(ap->lock, flags); 5697 5698 ehi->probe_mask |= ATA_ALL_DEVICES; 5699 ehi->action |= ATA_EH_RESET; 5700 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET; 5701 5702 ap->pflags &= ~ATA_PFLAG_INITIALIZING; 5703 ap->pflags |= ATA_PFLAG_LOADING; 5704 ata_port_schedule_eh(ap); 5705 5706 spin_unlock_irqrestore(ap->lock, flags); 5707 } 5708 5709 int ata_port_probe(struct ata_port *ap) 5710 { 5711 int rc = 0; 5712 5713 if (ap->ops->error_handler) { 5714 __ata_port_probe(ap); 5715 ata_port_wait_eh(ap); 5716 } else { 5717 rc = ata_bus_probe(ap); 5718 } 5719 return rc; 5720 } 5721 5722 5723 static void async_port_probe(void *data, async_cookie_t cookie) 5724 { 5725 struct ata_port *ap = data; 5726 5727 /* 5728 * If we're not allowed to scan this host in parallel, 5729 * we need to wait until all previous scans have completed 5730 * before going further. 5731 * Jeff Garzik says this is only within a controller, so we 5732 * don't need to wait for port 0, only for later ports. 5733 */ 5734 if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0) 5735 async_synchronize_cookie(cookie); 5736 5737 (void)ata_port_probe(ap); 5738 5739 /* in order to keep device order, we need to synchronize at this point */ 5740 async_synchronize_cookie(cookie); 5741 5742 ata_scsi_scan_host(ap, 1); 5743 } 5744 5745 /** 5746 * ata_host_register - register initialized ATA host 5747 * @host: ATA host to register 5748 * @sht: template for SCSI host 5749 * 5750 * Register initialized ATA host. @host is allocated using 5751 * ata_host_alloc() and fully initialized by LLD. This function 5752 * starts ports, registers @host with ATA and SCSI layers and 5753 * probe registered devices. 5754 * 5755 * LOCKING: 5756 * Inherited from calling layer (may sleep). 5757 * 5758 * RETURNS: 5759 * 0 on success, -errno otherwise. 5760 */ 5761 int ata_host_register(struct ata_host *host, struct scsi_host_template *sht) 5762 { 5763 int i, rc; 5764 5765 host->n_tags = clamp(sht->can_queue, 1, ATA_MAX_QUEUE); 5766 5767 /* host must have been started */ 5768 if (!(host->flags & ATA_HOST_STARTED)) { 5769 dev_err(host->dev, "BUG: trying to register unstarted host\n"); 5770 WARN_ON(1); 5771 return -EINVAL; 5772 } 5773 5774 /* Blow away unused ports. This happens when LLD can't 5775 * determine the exact number of ports to allocate at 5776 * allocation time. 5777 */ 5778 for (i = host->n_ports; host->ports[i]; i++) 5779 kfree(host->ports[i]); 5780 5781 /* give ports names and add SCSI hosts */ 5782 for (i = 0; i < host->n_ports; i++) { 5783 host->ports[i]->print_id = atomic_inc_return(&ata_print_id); 5784 host->ports[i]->local_port_no = i + 1; 5785 } 5786 5787 /* Create associated sysfs transport objects */ 5788 for (i = 0; i < host->n_ports; i++) { 5789 rc = ata_tport_add(host->dev,host->ports[i]); 5790 if (rc) { 5791 goto err_tadd; 5792 } 5793 } 5794 5795 rc = ata_scsi_add_hosts(host, sht); 5796 if (rc) 5797 goto err_tadd; 5798 5799 /* set cable, sata_spd_limit and report */ 5800 for (i = 0; i < host->n_ports; i++) { 5801 struct ata_port *ap = host->ports[i]; 5802 unsigned long xfer_mask; 5803 5804 /* set SATA cable type if still unset */ 5805 if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA)) 5806 ap->cbl = ATA_CBL_SATA; 5807 5808 /* init sata_spd_limit to the current value */ 5809 sata_link_init_spd(&ap->link); 5810 if (ap->slave_link) 5811 sata_link_init_spd(ap->slave_link); 5812 5813 /* print per-port info to dmesg */ 5814 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask, 5815 ap->udma_mask); 5816 5817 if (!ata_port_is_dummy(ap)) { 5818 ata_port_info(ap, "%cATA max %s %s\n", 5819 (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P', 5820 ata_mode_string(xfer_mask), 5821 ap->link.eh_info.desc); 5822 ata_ehi_clear_desc(&ap->link.eh_info); 5823 } else 5824 ata_port_info(ap, "DUMMY\n"); 5825 } 5826 5827 /* perform each probe asynchronously */ 5828 for (i = 0; i < host->n_ports; i++) { 5829 struct ata_port *ap = host->ports[i]; 5830 ap->cookie = async_schedule(async_port_probe, ap); 5831 } 5832 5833 return 0; 5834 5835 err_tadd: 5836 while (--i >= 0) { 5837 ata_tport_delete(host->ports[i]); 5838 } 5839 return rc; 5840 5841 } 5842 EXPORT_SYMBOL_GPL(ata_host_register); 5843 5844 /** 5845 * ata_host_activate - start host, request IRQ and register it 5846 * @host: target ATA host 5847 * @irq: IRQ to request 5848 * @irq_handler: irq_handler used when requesting IRQ 5849 * @irq_flags: irq_flags used when requesting IRQ 5850 * @sht: scsi_host_template to use when registering the host 5851 * 5852 * After allocating an ATA host and initializing it, most libata 5853 * LLDs perform three steps to activate the host - start host, 5854 * request IRQ and register it. This helper takes necessary 5855 * arguments and performs the three steps in one go. 5856 * 5857 * An invalid IRQ skips the IRQ registration and expects the host to 5858 * have set polling mode on the port. In this case, @irq_handler 5859 * should be NULL. 5860 * 5861 * LOCKING: 5862 * Inherited from calling layer (may sleep). 5863 * 5864 * RETURNS: 5865 * 0 on success, -errno otherwise. 5866 */ 5867 int ata_host_activate(struct ata_host *host, int irq, 5868 irq_handler_t irq_handler, unsigned long irq_flags, 5869 struct scsi_host_template *sht) 5870 { 5871 int i, rc; 5872 char *irq_desc; 5873 5874 rc = ata_host_start(host); 5875 if (rc) 5876 return rc; 5877 5878 /* Special case for polling mode */ 5879 if (!irq) { 5880 WARN_ON(irq_handler); 5881 return ata_host_register(host, sht); 5882 } 5883 5884 irq_desc = devm_kasprintf(host->dev, GFP_KERNEL, "%s[%s]", 5885 dev_driver_string(host->dev), 5886 dev_name(host->dev)); 5887 if (!irq_desc) 5888 return -ENOMEM; 5889 5890 rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags, 5891 irq_desc, host); 5892 if (rc) 5893 return rc; 5894 5895 for (i = 0; i < host->n_ports; i++) 5896 ata_port_desc(host->ports[i], "irq %d", irq); 5897 5898 rc = ata_host_register(host, sht); 5899 /* if failed, just free the IRQ and leave ports alone */ 5900 if (rc) 5901 devm_free_irq(host->dev, irq, host); 5902 5903 return rc; 5904 } 5905 EXPORT_SYMBOL_GPL(ata_host_activate); 5906 5907 /** 5908 * ata_port_detach - Detach ATA port in preparation of device removal 5909 * @ap: ATA port to be detached 5910 * 5911 * Detach all ATA devices and the associated SCSI devices of @ap; 5912 * then, remove the associated SCSI host. @ap is guaranteed to 5913 * be quiescent on return from this function. 5914 * 5915 * LOCKING: 5916 * Kernel thread context (may sleep). 5917 */ 5918 static void ata_port_detach(struct ata_port *ap) 5919 { 5920 unsigned long flags; 5921 struct ata_link *link; 5922 struct ata_device *dev; 5923 5924 if (!ap->ops->error_handler) 5925 goto skip_eh; 5926 5927 /* tell EH we're leaving & flush EH */ 5928 spin_lock_irqsave(ap->lock, flags); 5929 ap->pflags |= ATA_PFLAG_UNLOADING; 5930 ata_port_schedule_eh(ap); 5931 spin_unlock_irqrestore(ap->lock, flags); 5932 5933 /* wait till EH commits suicide */ 5934 ata_port_wait_eh(ap); 5935 5936 /* it better be dead now */ 5937 WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED)); 5938 5939 cancel_delayed_work_sync(&ap->hotplug_task); 5940 5941 skip_eh: 5942 /* clean up zpodd on port removal */ 5943 ata_for_each_link(link, ap, HOST_FIRST) { 5944 ata_for_each_dev(dev, link, ALL) { 5945 if (zpodd_dev_enabled(dev)) 5946 zpodd_exit(dev); 5947 } 5948 } 5949 if (ap->pmp_link) { 5950 int i; 5951 for (i = 0; i < SATA_PMP_MAX_PORTS; i++) 5952 ata_tlink_delete(&ap->pmp_link[i]); 5953 } 5954 /* remove the associated SCSI host */ 5955 scsi_remove_host(ap->scsi_host); 5956 ata_tport_delete(ap); 5957 } 5958 5959 /** 5960 * ata_host_detach - Detach all ports of an ATA host 5961 * @host: Host to detach 5962 * 5963 * Detach all ports of @host. 5964 * 5965 * LOCKING: 5966 * Kernel thread context (may sleep). 5967 */ 5968 void ata_host_detach(struct ata_host *host) 5969 { 5970 int i; 5971 5972 for (i = 0; i < host->n_ports; i++) { 5973 /* Ensure ata_port probe has completed */ 5974 async_synchronize_cookie(host->ports[i]->cookie + 1); 5975 ata_port_detach(host->ports[i]); 5976 } 5977 5978 /* the host is dead now, dissociate ACPI */ 5979 ata_acpi_dissociate(host); 5980 } 5981 EXPORT_SYMBOL_GPL(ata_host_detach); 5982 5983 #ifdef CONFIG_PCI 5984 5985 /** 5986 * ata_pci_remove_one - PCI layer callback for device removal 5987 * @pdev: PCI device that was removed 5988 * 5989 * PCI layer indicates to libata via this hook that hot-unplug or 5990 * module unload event has occurred. Detach all ports. Resource 5991 * release is handled via devres. 5992 * 5993 * LOCKING: 5994 * Inherited from PCI layer (may sleep). 5995 */ 5996 void ata_pci_remove_one(struct pci_dev *pdev) 5997 { 5998 struct ata_host *host = pci_get_drvdata(pdev); 5999 6000 ata_host_detach(host); 6001 } 6002 EXPORT_SYMBOL_GPL(ata_pci_remove_one); 6003 6004 void ata_pci_shutdown_one(struct pci_dev *pdev) 6005 { 6006 struct ata_host *host = pci_get_drvdata(pdev); 6007 int i; 6008 6009 for (i = 0; i < host->n_ports; i++) { 6010 struct ata_port *ap = host->ports[i]; 6011 6012 ap->pflags |= ATA_PFLAG_FROZEN; 6013 6014 /* Disable port interrupts */ 6015 if (ap->ops->freeze) 6016 ap->ops->freeze(ap); 6017 6018 /* Stop the port DMA engines */ 6019 if (ap->ops->port_stop) 6020 ap->ops->port_stop(ap); 6021 } 6022 } 6023 EXPORT_SYMBOL_GPL(ata_pci_shutdown_one); 6024 6025 /* move to PCI subsystem */ 6026 int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits) 6027 { 6028 unsigned long tmp = 0; 6029 6030 switch (bits->width) { 6031 case 1: { 6032 u8 tmp8 = 0; 6033 pci_read_config_byte(pdev, bits->reg, &tmp8); 6034 tmp = tmp8; 6035 break; 6036 } 6037 case 2: { 6038 u16 tmp16 = 0; 6039 pci_read_config_word(pdev, bits->reg, &tmp16); 6040 tmp = tmp16; 6041 break; 6042 } 6043 case 4: { 6044 u32 tmp32 = 0; 6045 pci_read_config_dword(pdev, bits->reg, &tmp32); 6046 tmp = tmp32; 6047 break; 6048 } 6049 6050 default: 6051 return -EINVAL; 6052 } 6053 6054 tmp &= bits->mask; 6055 6056 return (tmp == bits->val) ? 1 : 0; 6057 } 6058 EXPORT_SYMBOL_GPL(pci_test_config_bits); 6059 6060 #ifdef CONFIG_PM 6061 void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg) 6062 { 6063 pci_save_state(pdev); 6064 pci_disable_device(pdev); 6065 6066 if (mesg.event & PM_EVENT_SLEEP) 6067 pci_set_power_state(pdev, PCI_D3hot); 6068 } 6069 EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend); 6070 6071 int ata_pci_device_do_resume(struct pci_dev *pdev) 6072 { 6073 int rc; 6074 6075 pci_set_power_state(pdev, PCI_D0); 6076 pci_restore_state(pdev); 6077 6078 rc = pcim_enable_device(pdev); 6079 if (rc) { 6080 dev_err(&pdev->dev, 6081 "failed to enable device after resume (%d)\n", rc); 6082 return rc; 6083 } 6084 6085 pci_set_master(pdev); 6086 return 0; 6087 } 6088 EXPORT_SYMBOL_GPL(ata_pci_device_do_resume); 6089 6090 int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg) 6091 { 6092 struct ata_host *host = pci_get_drvdata(pdev); 6093 int rc = 0; 6094 6095 rc = ata_host_suspend(host, mesg); 6096 if (rc) 6097 return rc; 6098 6099 ata_pci_device_do_suspend(pdev, mesg); 6100 6101 return 0; 6102 } 6103 EXPORT_SYMBOL_GPL(ata_pci_device_suspend); 6104 6105 int ata_pci_device_resume(struct pci_dev *pdev) 6106 { 6107 struct ata_host *host = pci_get_drvdata(pdev); 6108 int rc; 6109 6110 rc = ata_pci_device_do_resume(pdev); 6111 if (rc == 0) 6112 ata_host_resume(host); 6113 return rc; 6114 } 6115 EXPORT_SYMBOL_GPL(ata_pci_device_resume); 6116 #endif /* CONFIG_PM */ 6117 #endif /* CONFIG_PCI */ 6118 6119 /** 6120 * ata_platform_remove_one - Platform layer callback for device removal 6121 * @pdev: Platform device that was removed 6122 * 6123 * Platform layer indicates to libata via this hook that hot-unplug or 6124 * module unload event has occurred. Detach all ports. Resource 6125 * release is handled via devres. 6126 * 6127 * LOCKING: 6128 * Inherited from platform layer (may sleep). 6129 */ 6130 int ata_platform_remove_one(struct platform_device *pdev) 6131 { 6132 struct ata_host *host = platform_get_drvdata(pdev); 6133 6134 ata_host_detach(host); 6135 6136 return 0; 6137 } 6138 EXPORT_SYMBOL_GPL(ata_platform_remove_one); 6139 6140 #ifdef CONFIG_ATA_FORCE 6141 static int __init ata_parse_force_one(char **cur, 6142 struct ata_force_ent *force_ent, 6143 const char **reason) 6144 { 6145 static const struct ata_force_param force_tbl[] __initconst = { 6146 { "40c", .cbl = ATA_CBL_PATA40 }, 6147 { "80c", .cbl = ATA_CBL_PATA80 }, 6148 { "short40c", .cbl = ATA_CBL_PATA40_SHORT }, 6149 { "unk", .cbl = ATA_CBL_PATA_UNK }, 6150 { "ign", .cbl = ATA_CBL_PATA_IGN }, 6151 { "sata", .cbl = ATA_CBL_SATA }, 6152 { "1.5Gbps", .spd_limit = 1 }, 6153 { "3.0Gbps", .spd_limit = 2 }, 6154 { "noncq", .horkage_on = ATA_HORKAGE_NONCQ }, 6155 { "ncq", .horkage_off = ATA_HORKAGE_NONCQ }, 6156 { "noncqtrim", .horkage_on = ATA_HORKAGE_NO_NCQ_TRIM }, 6157 { "ncqtrim", .horkage_off = ATA_HORKAGE_NO_NCQ_TRIM }, 6158 { "noncqati", .horkage_on = ATA_HORKAGE_NO_NCQ_ON_ATI }, 6159 { "ncqati", .horkage_off = ATA_HORKAGE_NO_NCQ_ON_ATI }, 6160 { "dump_id", .horkage_on = ATA_HORKAGE_DUMP_ID }, 6161 { "pio0", .xfer_mask = 1 << (ATA_SHIFT_PIO + 0) }, 6162 { "pio1", .xfer_mask = 1 << (ATA_SHIFT_PIO + 1) }, 6163 { "pio2", .xfer_mask = 1 << (ATA_SHIFT_PIO + 2) }, 6164 { "pio3", .xfer_mask = 1 << (ATA_SHIFT_PIO + 3) }, 6165 { "pio4", .xfer_mask = 1 << (ATA_SHIFT_PIO + 4) }, 6166 { "pio5", .xfer_mask = 1 << (ATA_SHIFT_PIO + 5) }, 6167 { "pio6", .xfer_mask = 1 << (ATA_SHIFT_PIO + 6) }, 6168 { "mwdma0", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 0) }, 6169 { "mwdma1", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 1) }, 6170 { "mwdma2", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 2) }, 6171 { "mwdma3", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 3) }, 6172 { "mwdma4", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 4) }, 6173 { "udma0", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) }, 6174 { "udma16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) }, 6175 { "udma/16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) }, 6176 { "udma1", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) }, 6177 { "udma25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) }, 6178 { "udma/25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) }, 6179 { "udma2", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) }, 6180 { "udma33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) }, 6181 { "udma/33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) }, 6182 { "udma3", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) }, 6183 { "udma44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) }, 6184 { "udma/44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) }, 6185 { "udma4", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) }, 6186 { "udma66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) }, 6187 { "udma/66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) }, 6188 { "udma5", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) }, 6189 { "udma100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) }, 6190 { "udma/100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) }, 6191 { "udma6", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) }, 6192 { "udma133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) }, 6193 { "udma/133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) }, 6194 { "udma7", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 7) }, 6195 { "nohrst", .lflags = ATA_LFLAG_NO_HRST }, 6196 { "nosrst", .lflags = ATA_LFLAG_NO_SRST }, 6197 { "norst", .lflags = ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST }, 6198 { "rstonce", .lflags = ATA_LFLAG_RST_ONCE }, 6199 { "atapi_dmadir", .horkage_on = ATA_HORKAGE_ATAPI_DMADIR }, 6200 { "disable", .horkage_on = ATA_HORKAGE_DISABLE }, 6201 }; 6202 char *start = *cur, *p = *cur; 6203 char *id, *val, *endp; 6204 const struct ata_force_param *match_fp = NULL; 6205 int nr_matches = 0, i; 6206 6207 /* find where this param ends and update *cur */ 6208 while (*p != '\0' && *p != ',') 6209 p++; 6210 6211 if (*p == '\0') 6212 *cur = p; 6213 else 6214 *cur = p + 1; 6215 6216 *p = '\0'; 6217 6218 /* parse */ 6219 p = strchr(start, ':'); 6220 if (!p) { 6221 val = strstrip(start); 6222 goto parse_val; 6223 } 6224 *p = '\0'; 6225 6226 id = strstrip(start); 6227 val = strstrip(p + 1); 6228 6229 /* parse id */ 6230 p = strchr(id, '.'); 6231 if (p) { 6232 *p++ = '\0'; 6233 force_ent->device = simple_strtoul(p, &endp, 10); 6234 if (p == endp || *endp != '\0') { 6235 *reason = "invalid device"; 6236 return -EINVAL; 6237 } 6238 } 6239 6240 force_ent->port = simple_strtoul(id, &endp, 10); 6241 if (id == endp || *endp != '\0') { 6242 *reason = "invalid port/link"; 6243 return -EINVAL; 6244 } 6245 6246 parse_val: 6247 /* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */ 6248 for (i = 0; i < ARRAY_SIZE(force_tbl); i++) { 6249 const struct ata_force_param *fp = &force_tbl[i]; 6250 6251 if (strncasecmp(val, fp->name, strlen(val))) 6252 continue; 6253 6254 nr_matches++; 6255 match_fp = fp; 6256 6257 if (strcasecmp(val, fp->name) == 0) { 6258 nr_matches = 1; 6259 break; 6260 } 6261 } 6262 6263 if (!nr_matches) { 6264 *reason = "unknown value"; 6265 return -EINVAL; 6266 } 6267 if (nr_matches > 1) { 6268 *reason = "ambiguous value"; 6269 return -EINVAL; 6270 } 6271 6272 force_ent->param = *match_fp; 6273 6274 return 0; 6275 } 6276 6277 static void __init ata_parse_force_param(void) 6278 { 6279 int idx = 0, size = 1; 6280 int last_port = -1, last_device = -1; 6281 char *p, *cur, *next; 6282 6283 /* calculate maximum number of params and allocate force_tbl */ 6284 for (p = ata_force_param_buf; *p; p++) 6285 if (*p == ',') 6286 size++; 6287 6288 ata_force_tbl = kcalloc(size, sizeof(ata_force_tbl[0]), GFP_KERNEL); 6289 if (!ata_force_tbl) { 6290 printk(KERN_WARNING "ata: failed to extend force table, " 6291 "libata.force ignored\n"); 6292 return; 6293 } 6294 6295 /* parse and populate the table */ 6296 for (cur = ata_force_param_buf; *cur != '\0'; cur = next) { 6297 const char *reason = ""; 6298 struct ata_force_ent te = { .port = -1, .device = -1 }; 6299 6300 next = cur; 6301 if (ata_parse_force_one(&next, &te, &reason)) { 6302 printk(KERN_WARNING "ata: failed to parse force " 6303 "parameter \"%s\" (%s)\n", 6304 cur, reason); 6305 continue; 6306 } 6307 6308 if (te.port == -1) { 6309 te.port = last_port; 6310 te.device = last_device; 6311 } 6312 6313 ata_force_tbl[idx++] = te; 6314 6315 last_port = te.port; 6316 last_device = te.device; 6317 } 6318 6319 ata_force_tbl_size = idx; 6320 } 6321 6322 static void ata_free_force_param(void) 6323 { 6324 kfree(ata_force_tbl); 6325 } 6326 #else 6327 static inline void ata_parse_force_param(void) { } 6328 static inline void ata_free_force_param(void) { } 6329 #endif 6330 6331 static int __init ata_init(void) 6332 { 6333 int rc; 6334 6335 ata_parse_force_param(); 6336 6337 rc = ata_sff_init(); 6338 if (rc) { 6339 ata_free_force_param(); 6340 return rc; 6341 } 6342 6343 libata_transport_init(); 6344 ata_scsi_transport_template = ata_attach_transport(); 6345 if (!ata_scsi_transport_template) { 6346 ata_sff_exit(); 6347 rc = -ENOMEM; 6348 goto err_out; 6349 } 6350 6351 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n"); 6352 return 0; 6353 6354 err_out: 6355 return rc; 6356 } 6357 6358 static void __exit ata_exit(void) 6359 { 6360 ata_release_transport(ata_scsi_transport_template); 6361 libata_transport_exit(); 6362 ata_sff_exit(); 6363 ata_free_force_param(); 6364 } 6365 6366 subsys_initcall(ata_init); 6367 module_exit(ata_exit); 6368 6369 static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1); 6370 6371 int ata_ratelimit(void) 6372 { 6373 return __ratelimit(&ratelimit); 6374 } 6375 EXPORT_SYMBOL_GPL(ata_ratelimit); 6376 6377 /** 6378 * ata_msleep - ATA EH owner aware msleep 6379 * @ap: ATA port to attribute the sleep to 6380 * @msecs: duration to sleep in milliseconds 6381 * 6382 * Sleeps @msecs. If the current task is owner of @ap's EH, the 6383 * ownership is released before going to sleep and reacquired 6384 * after the sleep is complete. IOW, other ports sharing the 6385 * @ap->host will be allowed to own the EH while this task is 6386 * sleeping. 6387 * 6388 * LOCKING: 6389 * Might sleep. 6390 */ 6391 void ata_msleep(struct ata_port *ap, unsigned int msecs) 6392 { 6393 bool owns_eh = ap && ap->host->eh_owner == current; 6394 6395 if (owns_eh) 6396 ata_eh_release(ap); 6397 6398 if (msecs < 20) { 6399 unsigned long usecs = msecs * USEC_PER_MSEC; 6400 usleep_range(usecs, usecs + 50); 6401 } else { 6402 msleep(msecs); 6403 } 6404 6405 if (owns_eh) 6406 ata_eh_acquire(ap); 6407 } 6408 EXPORT_SYMBOL_GPL(ata_msleep); 6409 6410 /** 6411 * ata_wait_register - wait until register value changes 6412 * @ap: ATA port to wait register for, can be NULL 6413 * @reg: IO-mapped register 6414 * @mask: Mask to apply to read register value 6415 * @val: Wait condition 6416 * @interval: polling interval in milliseconds 6417 * @timeout: timeout in milliseconds 6418 * 6419 * Waiting for some bits of register to change is a common 6420 * operation for ATA controllers. This function reads 32bit LE 6421 * IO-mapped register @reg and tests for the following condition. 6422 * 6423 * (*@reg & mask) != val 6424 * 6425 * If the condition is met, it returns; otherwise, the process is 6426 * repeated after @interval_msec until timeout. 6427 * 6428 * LOCKING: 6429 * Kernel thread context (may sleep) 6430 * 6431 * RETURNS: 6432 * The final register value. 6433 */ 6434 u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask, u32 val, 6435 unsigned long interval, unsigned long timeout) 6436 { 6437 unsigned long deadline; 6438 u32 tmp; 6439 6440 tmp = ioread32(reg); 6441 6442 /* Calculate timeout _after_ the first read to make sure 6443 * preceding writes reach the controller before starting to 6444 * eat away the timeout. 6445 */ 6446 deadline = ata_deadline(jiffies, timeout); 6447 6448 while ((tmp & mask) == val && time_before(jiffies, deadline)) { 6449 ata_msleep(ap, interval); 6450 tmp = ioread32(reg); 6451 } 6452 6453 return tmp; 6454 } 6455 EXPORT_SYMBOL_GPL(ata_wait_register); 6456 6457 /* 6458 * Dummy port_ops 6459 */ 6460 static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc) 6461 { 6462 return AC_ERR_SYSTEM; 6463 } 6464 6465 static void ata_dummy_error_handler(struct ata_port *ap) 6466 { 6467 /* truly dummy */ 6468 } 6469 6470 struct ata_port_operations ata_dummy_port_ops = { 6471 .qc_prep = ata_noop_qc_prep, 6472 .qc_issue = ata_dummy_qc_issue, 6473 .error_handler = ata_dummy_error_handler, 6474 .sched_eh = ata_std_sched_eh, 6475 .end_eh = ata_std_end_eh, 6476 }; 6477 EXPORT_SYMBOL_GPL(ata_dummy_port_ops); 6478 6479 const struct ata_port_info ata_dummy_port_info = { 6480 .port_ops = &ata_dummy_port_ops, 6481 }; 6482 EXPORT_SYMBOL_GPL(ata_dummy_port_info); 6483 6484 void ata_print_version(const struct device *dev, const char *version) 6485 { 6486 dev_printk(KERN_DEBUG, dev, "version %s\n", version); 6487 } 6488 EXPORT_SYMBOL(ata_print_version); 6489 6490 EXPORT_TRACEPOINT_SYMBOL_GPL(ata_tf_load); 6491 EXPORT_TRACEPOINT_SYMBOL_GPL(ata_exec_command); 6492 EXPORT_TRACEPOINT_SYMBOL_GPL(ata_bmdma_setup); 6493 EXPORT_TRACEPOINT_SYMBOL_GPL(ata_bmdma_start); 6494 EXPORT_TRACEPOINT_SYMBOL_GPL(ata_bmdma_status); 6495