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