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