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