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