1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * libata-eh.c - libata error handling 4 * 5 * Copyright 2006 Tejun Heo <htejun@gmail.com> 6 * 7 * libata documentation is available via 'make {ps|pdf}docs', 8 * as Documentation/driver-api/libata.rst 9 * 10 * Hardware documentation available from http://www.t13.org/ and 11 * http://www.sata-io.org/ 12 */ 13 14 #include <linux/kernel.h> 15 #include <linux/blkdev.h> 16 #include <linux/export.h> 17 #include <linux/pci.h> 18 #include <scsi/scsi.h> 19 #include <scsi/scsi_host.h> 20 #include <scsi/scsi_eh.h> 21 #include <scsi/scsi_device.h> 22 #include <scsi/scsi_cmnd.h> 23 #include <scsi/scsi_dbg.h> 24 #include "../scsi/scsi_transport_api.h" 25 26 #include <linux/libata.h> 27 28 #include <trace/events/libata.h> 29 #include "libata.h" 30 31 enum { 32 /* speed down verdicts */ 33 ATA_EH_SPDN_NCQ_OFF = (1 << 0), 34 ATA_EH_SPDN_SPEED_DOWN = (1 << 1), 35 ATA_EH_SPDN_FALLBACK_TO_PIO = (1 << 2), 36 ATA_EH_SPDN_KEEP_ERRORS = (1 << 3), 37 38 /* error flags */ 39 ATA_EFLAG_IS_IO = (1 << 0), 40 ATA_EFLAG_DUBIOUS_XFER = (1 << 1), 41 ATA_EFLAG_OLD_ER = (1 << 31), 42 43 /* error categories */ 44 ATA_ECAT_NONE = 0, 45 ATA_ECAT_ATA_BUS = 1, 46 ATA_ECAT_TOUT_HSM = 2, 47 ATA_ECAT_UNK_DEV = 3, 48 ATA_ECAT_DUBIOUS_NONE = 4, 49 ATA_ECAT_DUBIOUS_ATA_BUS = 5, 50 ATA_ECAT_DUBIOUS_TOUT_HSM = 6, 51 ATA_ECAT_DUBIOUS_UNK_DEV = 7, 52 ATA_ECAT_NR = 8, 53 54 ATA_EH_CMD_DFL_TIMEOUT = 5000, 55 56 /* always put at least this amount of time between resets */ 57 ATA_EH_RESET_COOL_DOWN = 5000, 58 59 /* Waiting in ->prereset can never be reliable. It's 60 * sometimes nice to wait there but it can't be depended upon; 61 * otherwise, we wouldn't be resetting. Just give it enough 62 * time for most drives to spin up. 63 */ 64 ATA_EH_PRERESET_TIMEOUT = 10000, 65 ATA_EH_FASTDRAIN_INTERVAL = 3000, 66 67 ATA_EH_UA_TRIES = 5, 68 69 /* probe speed down parameters, see ata_eh_schedule_probe() */ 70 ATA_EH_PROBE_TRIAL_INTERVAL = 60000, /* 1 min */ 71 ATA_EH_PROBE_TRIALS = 2, 72 }; 73 74 /* The following table determines how we sequence resets. Each entry 75 * represents timeout for that try. The first try can be soft or 76 * hardreset. All others are hardreset if available. In most cases 77 * the first reset w/ 10sec timeout should succeed. Following entries 78 * are mostly for error handling, hotplug and those outlier devices that 79 * take an exceptionally long time to recover from reset. 80 */ 81 static const unsigned int ata_eh_reset_timeouts[] = { 82 10000, /* most drives spin up by 10sec */ 83 10000, /* > 99% working drives spin up before 20sec */ 84 35000, /* give > 30 secs of idleness for outlier devices */ 85 5000, /* and sweet one last chance */ 86 UINT_MAX, /* > 1 min has elapsed, give up */ 87 }; 88 89 static const unsigned int ata_eh_identify_timeouts[] = { 90 5000, /* covers > 99% of successes and not too boring on failures */ 91 10000, /* combined time till here is enough even for media access */ 92 30000, /* for true idiots */ 93 UINT_MAX, 94 }; 95 96 static const unsigned int ata_eh_revalidate_timeouts[] = { 97 15000, /* Some drives are slow to read log pages when waking-up */ 98 15000, /* combined time till here is enough even for media access */ 99 UINT_MAX, 100 }; 101 102 static const unsigned int ata_eh_flush_timeouts[] = { 103 15000, /* be generous with flush */ 104 15000, /* ditto */ 105 30000, /* and even more generous */ 106 UINT_MAX, 107 }; 108 109 static const unsigned int ata_eh_standby_timeouts[] = { 110 15000, /* Some drives may be slow to standby */ 111 /* but don't hold up a suspend too long waiting for them */ 112 UINT_MAX, 113 }; 114 115 static const unsigned int ata_eh_other_timeouts[] = { 116 5000, /* same rationale as identify timeout */ 117 10000, /* ditto */ 118 /* but no merciful 30sec for other commands, it just isn't worth it */ 119 UINT_MAX, 120 }; 121 122 struct ata_eh_cmd_timeout_ent { 123 const u8 *commands; 124 const unsigned int *timeouts; 125 }; 126 127 /* The following table determines timeouts to use for EH internal 128 * commands. Each table entry is a command class and matches the 129 * commands the entry applies to and the timeout table to use. 130 * 131 * On the retry after a command timed out, the next timeout value from 132 * the table is used. If the table doesn't contain further entries, 133 * the last value is used. 134 * 135 * ehc->cmd_timeout_idx keeps track of which timeout to use per 136 * command class, so if SET_FEATURES times out on the first try, the 137 * next try will use the second timeout value only for that class. 138 */ 139 #define CMDS(cmds...) (const u8 []){ cmds, 0 } 140 static const struct ata_eh_cmd_timeout_ent 141 ata_eh_cmd_timeout_table[ATA_EH_CMD_TIMEOUT_TABLE_SIZE] = { 142 { .commands = CMDS(ATA_CMD_ID_ATA, ATA_CMD_ID_ATAPI), 143 .timeouts = ata_eh_identify_timeouts, }, 144 { .commands = CMDS(ATA_CMD_READ_LOG_EXT, ATA_CMD_READ_LOG_DMA_EXT), 145 .timeouts = ata_eh_revalidate_timeouts, }, 146 { .commands = CMDS(ATA_CMD_READ_NATIVE_MAX, ATA_CMD_READ_NATIVE_MAX_EXT), 147 .timeouts = ata_eh_other_timeouts, }, 148 { .commands = CMDS(ATA_CMD_SET_MAX, ATA_CMD_SET_MAX_EXT), 149 .timeouts = ata_eh_other_timeouts, }, 150 { .commands = CMDS(ATA_CMD_SET_FEATURES), 151 .timeouts = ata_eh_other_timeouts, }, 152 { .commands = CMDS(ATA_CMD_INIT_DEV_PARAMS), 153 .timeouts = ata_eh_other_timeouts, }, 154 { .commands = CMDS(ATA_CMD_FLUSH, ATA_CMD_FLUSH_EXT), 155 .timeouts = ata_eh_flush_timeouts }, 156 { .commands = CMDS(ATA_CMD_STANDBYNOW1), 157 .timeouts = ata_eh_standby_timeouts }, 158 { .commands = CMDS(ATA_CMD_VERIFY), 159 .timeouts = ata_eh_reset_timeouts }, 160 }; 161 #undef CMDS 162 163 static void __ata_port_freeze(struct ata_port *ap); 164 #ifdef CONFIG_PM 165 static void ata_eh_handle_port_suspend(struct ata_port *ap); 166 static void ata_eh_handle_port_resume(struct ata_port *ap); 167 #else /* CONFIG_PM */ 168 static void ata_eh_handle_port_suspend(struct ata_port *ap) 169 { } 170 171 static void ata_eh_handle_port_resume(struct ata_port *ap) 172 { } 173 #endif /* CONFIG_PM */ 174 175 static __printf(2, 0) void __ata_ehi_pushv_desc(struct ata_eh_info *ehi, 176 const char *fmt, va_list args) 177 { 178 ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len, 179 ATA_EH_DESC_LEN - ehi->desc_len, 180 fmt, args); 181 } 182 183 /** 184 * __ata_ehi_push_desc - push error description without adding separator 185 * @ehi: target EHI 186 * @fmt: printf format string 187 * 188 * Format string according to @fmt and append it to @ehi->desc. 189 * 190 * LOCKING: 191 * spin_lock_irqsave(host lock) 192 */ 193 void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...) 194 { 195 va_list args; 196 197 va_start(args, fmt); 198 __ata_ehi_pushv_desc(ehi, fmt, args); 199 va_end(args); 200 } 201 EXPORT_SYMBOL_GPL(__ata_ehi_push_desc); 202 203 /** 204 * ata_ehi_push_desc - push error description with separator 205 * @ehi: target EHI 206 * @fmt: printf format string 207 * 208 * Format string according to @fmt and append it to @ehi->desc. 209 * If @ehi->desc is not empty, ", " is added in-between. 210 * 211 * LOCKING: 212 * spin_lock_irqsave(host lock) 213 */ 214 void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...) 215 { 216 va_list args; 217 218 if (ehi->desc_len) 219 __ata_ehi_push_desc(ehi, ", "); 220 221 va_start(args, fmt); 222 __ata_ehi_pushv_desc(ehi, fmt, args); 223 va_end(args); 224 } 225 EXPORT_SYMBOL_GPL(ata_ehi_push_desc); 226 227 /** 228 * ata_ehi_clear_desc - clean error description 229 * @ehi: target EHI 230 * 231 * Clear @ehi->desc. 232 * 233 * LOCKING: 234 * spin_lock_irqsave(host lock) 235 */ 236 void ata_ehi_clear_desc(struct ata_eh_info *ehi) 237 { 238 ehi->desc[0] = '\0'; 239 ehi->desc_len = 0; 240 } 241 EXPORT_SYMBOL_GPL(ata_ehi_clear_desc); 242 243 /** 244 * ata_port_desc - append port description 245 * @ap: target ATA port 246 * @fmt: printf format string 247 * 248 * Format string according to @fmt and append it to port 249 * description. If port description is not empty, " " is added 250 * in-between. This function is to be used while initializing 251 * ata_host. The description is printed on host registration. 252 * 253 * LOCKING: 254 * None. 255 */ 256 void ata_port_desc(struct ata_port *ap, const char *fmt, ...) 257 { 258 va_list args; 259 260 WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING)); 261 262 if (ap->link.eh_info.desc_len) 263 __ata_ehi_push_desc(&ap->link.eh_info, " "); 264 265 va_start(args, fmt); 266 __ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args); 267 va_end(args); 268 } 269 EXPORT_SYMBOL_GPL(ata_port_desc); 270 271 #ifdef CONFIG_PCI 272 /** 273 * ata_port_pbar_desc - append PCI BAR description 274 * @ap: target ATA port 275 * @bar: target PCI BAR 276 * @offset: offset into PCI BAR 277 * @name: name of the area 278 * 279 * If @offset is negative, this function formats a string which 280 * contains the name, address, size and type of the BAR and 281 * appends it to the port description. If @offset is zero or 282 * positive, only name and offsetted address is appended. 283 * 284 * LOCKING: 285 * None. 286 */ 287 void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset, 288 const char *name) 289 { 290 struct pci_dev *pdev = to_pci_dev(ap->host->dev); 291 char *type = ""; 292 unsigned long long start, len; 293 294 if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) 295 type = "m"; 296 else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO) 297 type = "i"; 298 299 start = (unsigned long long)pci_resource_start(pdev, bar); 300 len = (unsigned long long)pci_resource_len(pdev, bar); 301 302 if (offset < 0) 303 ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start); 304 else 305 ata_port_desc(ap, "%s 0x%llx", name, 306 start + (unsigned long long)offset); 307 } 308 EXPORT_SYMBOL_GPL(ata_port_pbar_desc); 309 #endif /* CONFIG_PCI */ 310 311 static int ata_lookup_timeout_table(u8 cmd) 312 { 313 int i; 314 315 for (i = 0; i < ATA_EH_CMD_TIMEOUT_TABLE_SIZE; i++) { 316 const u8 *cur; 317 318 for (cur = ata_eh_cmd_timeout_table[i].commands; *cur; cur++) 319 if (*cur == cmd) 320 return i; 321 } 322 323 return -1; 324 } 325 326 /** 327 * ata_internal_cmd_timeout - determine timeout for an internal command 328 * @dev: target device 329 * @cmd: internal command to be issued 330 * 331 * Determine timeout for internal command @cmd for @dev. 332 * 333 * LOCKING: 334 * EH context. 335 * 336 * RETURNS: 337 * Determined timeout. 338 */ 339 unsigned int ata_internal_cmd_timeout(struct ata_device *dev, u8 cmd) 340 { 341 struct ata_eh_context *ehc = &dev->link->eh_context; 342 int ent = ata_lookup_timeout_table(cmd); 343 int idx; 344 345 if (ent < 0) 346 return ATA_EH_CMD_DFL_TIMEOUT; 347 348 idx = ehc->cmd_timeout_idx[dev->devno][ent]; 349 return ata_eh_cmd_timeout_table[ent].timeouts[idx]; 350 } 351 352 /** 353 * ata_internal_cmd_timed_out - notification for internal command timeout 354 * @dev: target device 355 * @cmd: internal command which timed out 356 * 357 * Notify EH that internal command @cmd for @dev timed out. This 358 * function should be called only for commands whose timeouts are 359 * determined using ata_internal_cmd_timeout(). 360 * 361 * LOCKING: 362 * EH context. 363 */ 364 void ata_internal_cmd_timed_out(struct ata_device *dev, u8 cmd) 365 { 366 struct ata_eh_context *ehc = &dev->link->eh_context; 367 int ent = ata_lookup_timeout_table(cmd); 368 int idx; 369 370 if (ent < 0) 371 return; 372 373 idx = ehc->cmd_timeout_idx[dev->devno][ent]; 374 if (ata_eh_cmd_timeout_table[ent].timeouts[idx + 1] != UINT_MAX) 375 ehc->cmd_timeout_idx[dev->devno][ent]++; 376 } 377 378 static void ata_ering_record(struct ata_ering *ering, unsigned int eflags, 379 unsigned int err_mask) 380 { 381 struct ata_ering_entry *ent; 382 383 WARN_ON(!err_mask); 384 385 ering->cursor++; 386 ering->cursor %= ATA_ERING_SIZE; 387 388 ent = &ering->ring[ering->cursor]; 389 ent->eflags = eflags; 390 ent->err_mask = err_mask; 391 ent->timestamp = get_jiffies_64(); 392 } 393 394 static struct ata_ering_entry *ata_ering_top(struct ata_ering *ering) 395 { 396 struct ata_ering_entry *ent = &ering->ring[ering->cursor]; 397 398 if (ent->err_mask) 399 return ent; 400 return NULL; 401 } 402 403 int ata_ering_map(struct ata_ering *ering, 404 int (*map_fn)(struct ata_ering_entry *, void *), 405 void *arg) 406 { 407 int idx, rc = 0; 408 struct ata_ering_entry *ent; 409 410 idx = ering->cursor; 411 do { 412 ent = &ering->ring[idx]; 413 if (!ent->err_mask) 414 break; 415 rc = map_fn(ent, arg); 416 if (rc) 417 break; 418 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE; 419 } while (idx != ering->cursor); 420 421 return rc; 422 } 423 424 static int ata_ering_clear_cb(struct ata_ering_entry *ent, void *void_arg) 425 { 426 ent->eflags |= ATA_EFLAG_OLD_ER; 427 return 0; 428 } 429 430 static void ata_ering_clear(struct ata_ering *ering) 431 { 432 ata_ering_map(ering, ata_ering_clear_cb, NULL); 433 } 434 435 static unsigned int ata_eh_dev_action(struct ata_device *dev) 436 { 437 struct ata_eh_context *ehc = &dev->link->eh_context; 438 439 return ehc->i.action | ehc->i.dev_action[dev->devno]; 440 } 441 442 static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev, 443 struct ata_eh_info *ehi, unsigned int action) 444 { 445 struct ata_device *tdev; 446 447 if (!dev) { 448 ehi->action &= ~action; 449 ata_for_each_dev(tdev, link, ALL) 450 ehi->dev_action[tdev->devno] &= ~action; 451 } else { 452 /* doesn't make sense for port-wide EH actions */ 453 WARN_ON(!(action & ATA_EH_PERDEV_MASK)); 454 455 /* break ehi->action into ehi->dev_action */ 456 if (ehi->action & action) { 457 ata_for_each_dev(tdev, link, ALL) 458 ehi->dev_action[tdev->devno] |= 459 ehi->action & action; 460 ehi->action &= ~action; 461 } 462 463 /* turn off the specified per-dev action */ 464 ehi->dev_action[dev->devno] &= ~action; 465 } 466 } 467 468 /** 469 * ata_eh_acquire - acquire EH ownership 470 * @ap: ATA port to acquire EH ownership for 471 * 472 * Acquire EH ownership for @ap. This is the basic exclusion 473 * mechanism for ports sharing a host. Only one port hanging off 474 * the same host can claim the ownership of EH. 475 * 476 * LOCKING: 477 * EH context. 478 */ 479 void ata_eh_acquire(struct ata_port *ap) 480 __acquires(&ap->host->eh_mutex) 481 { 482 mutex_lock(&ap->host->eh_mutex); 483 WARN_ON_ONCE(ap->host->eh_owner); 484 ap->host->eh_owner = current; 485 } 486 487 /** 488 * ata_eh_release - release EH ownership 489 * @ap: ATA port to release EH ownership for 490 * 491 * Release EH ownership for @ap if the caller. The caller must 492 * have acquired EH ownership using ata_eh_acquire() previously. 493 * 494 * LOCKING: 495 * EH context. 496 */ 497 void ata_eh_release(struct ata_port *ap) 498 __releases(&ap->host->eh_mutex) 499 { 500 WARN_ON_ONCE(ap->host->eh_owner != current); 501 ap->host->eh_owner = NULL; 502 mutex_unlock(&ap->host->eh_mutex); 503 } 504 505 static void ata_eh_dev_disable(struct ata_device *dev) 506 { 507 ata_acpi_on_disable(dev); 508 ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 | ATA_DNXFER_QUIET); 509 dev->class++; 510 511 /* 512 * From now till the next successful probe, ering is used to 513 * track probe failures. Clear accumulated device error info. 514 */ 515 ata_ering_clear(&dev->ering); 516 517 ata_dev_free_resources(dev); 518 } 519 520 static void ata_eh_unload(struct ata_port *ap) 521 { 522 struct ata_link *link; 523 struct ata_device *dev; 524 unsigned long flags; 525 526 /* 527 * Unless we are restarting, transition all enabled devices to 528 * standby power mode. 529 */ 530 if (system_state != SYSTEM_RESTART) { 531 ata_for_each_link(link, ap, PMP_FIRST) { 532 ata_for_each_dev(dev, link, ENABLED) 533 ata_dev_power_set_standby(dev); 534 } 535 } 536 537 /* 538 * Restore SControl IPM and SPD for the next driver and 539 * disable attached devices. 540 */ 541 ata_for_each_link(link, ap, PMP_FIRST) { 542 sata_scr_write(link, SCR_CONTROL, link->saved_scontrol & 0xff0); 543 ata_for_each_dev(dev, link, ENABLED) 544 ata_eh_dev_disable(dev); 545 } 546 547 /* freeze and set UNLOADED */ 548 spin_lock_irqsave(ap->lock, flags); 549 550 ata_port_freeze(ap); /* won't be thawed */ 551 ap->pflags &= ~ATA_PFLAG_EH_PENDING; /* clear pending from freeze */ 552 ap->pflags |= ATA_PFLAG_UNLOADED; 553 554 spin_unlock_irqrestore(ap->lock, flags); 555 } 556 557 /** 558 * ata_scsi_error - SCSI layer error handler callback 559 * @host: SCSI host on which error occurred 560 * 561 * Handles SCSI-layer-thrown error events. 562 * 563 * LOCKING: 564 * Inherited from SCSI layer (none, can sleep) 565 * 566 * RETURNS: 567 * Zero. 568 */ 569 void ata_scsi_error(struct Scsi_Host *host) 570 { 571 struct ata_port *ap = ata_shost_to_port(host); 572 unsigned long flags; 573 int nr_timedout; 574 LIST_HEAD(eh_work_q); 575 576 spin_lock_irqsave(host->host_lock, flags); 577 list_splice_init(&host->eh_cmd_q, &eh_work_q); 578 spin_unlock_irqrestore(host->host_lock, flags); 579 580 /* 581 * First check what errors we got with ata_scsi_cmd_error_handler(). 582 * If we had no command timeouts and EH is not scheduled for this port, 583 * meaning that we do not have any failed command, then there is no 584 * need to go through the full port error handling. We only need to 585 * flush the completed commands we have. 586 */ 587 nr_timedout = ata_scsi_cmd_error_handler(host, ap, &eh_work_q); 588 if (nr_timedout || ata_port_eh_scheduled(ap)) 589 ata_scsi_port_error_handler(host, ap); 590 else 591 scsi_eh_flush_done_q(&ap->eh_done_q); 592 593 WARN_ON(!list_empty(&eh_work_q)); 594 } 595 596 /** 597 * ata_scsi_cmd_error_handler - error callback for a list of commands 598 * @host: scsi host containing the port 599 * @ap: ATA port within the host 600 * @eh_work_q: list of commands to process 601 * 602 * process the given list of commands and return those finished to the 603 * ap->eh_done_q. This function is the first part of the libata error 604 * handler which processes a given list of failed commands. 605 * 606 * Return the number of commands that timed out. 607 */ 608 int ata_scsi_cmd_error_handler(struct Scsi_Host *host, struct ata_port *ap, 609 struct list_head *eh_work_q) 610 { 611 int i; 612 unsigned long flags; 613 struct scsi_cmnd *scmd, *tmp; 614 int nr_timedout = 0; 615 616 /* make sure sff pio task is not running */ 617 ata_sff_flush_pio_task(ap); 618 619 /* synchronize with host lock and sort out timeouts */ 620 621 /* 622 * For EH, all qcs are finished in one of three ways - 623 * normal completion, error completion, and SCSI timeout. 624 * Both completions can race against SCSI timeout. When normal 625 * completion wins, the qc never reaches EH. When error 626 * completion wins, the qc has ATA_QCFLAG_EH set. 627 * 628 * When SCSI timeout wins, things are a bit more complex. 629 * Normal or error completion can occur after the timeout but 630 * before this point. In such cases, both types of 631 * completions are honored. A scmd is determined to have 632 * timed out iff its associated qc is active and not failed. 633 */ 634 spin_lock_irqsave(ap->lock, flags); 635 636 /* 637 * This must occur under the ap->lock as we don't want 638 * a polled recovery to race the real interrupt handler 639 * 640 * The lost_interrupt handler checks for any completed but 641 * non-notified command and completes much like an IRQ handler. 642 * 643 * We then fall into the error recovery code which will treat 644 * this as if normal completion won the race 645 */ 646 if (ap->ops->lost_interrupt) 647 ap->ops->lost_interrupt(ap); 648 649 list_for_each_entry_safe(scmd, tmp, eh_work_q, eh_entry) { 650 struct ata_queued_cmd *qc; 651 652 /* 653 * If the scmd was added to EH, via ata_qc_schedule_eh() -> 654 * scsi_timeout() -> scsi_eh_scmd_add(), scsi_timeout() will 655 * have set DID_TIME_OUT (since libata does not have an abort 656 * handler). Thus, to clear DID_TIME_OUT, clear the host byte. 657 */ 658 set_host_byte(scmd, DID_OK); 659 660 ata_qc_for_each_raw(ap, qc, i) { 661 if (qc->scsicmd == scmd && 662 qc->flags & ATA_QCFLAG_ACTIVE) 663 break; 664 } 665 666 if (i < ATA_MAX_QUEUE) { 667 /* the scmd has an associated qc */ 668 if (!(qc->flags & ATA_QCFLAG_EH)) { 669 /* which hasn't failed yet, timeout */ 670 set_host_byte(scmd, DID_TIME_OUT); 671 qc->err_mask |= AC_ERR_TIMEOUT; 672 qc->flags |= ATA_QCFLAG_EH; 673 nr_timedout++; 674 } 675 } else { 676 /* Normal completion occurred after 677 * SCSI timeout but before this point. 678 * Successfully complete it. 679 */ 680 scmd->retries = scmd->allowed; 681 scsi_eh_finish_cmd(scmd, &ap->eh_done_q); 682 } 683 } 684 685 /* 686 * If we have timed out qcs. They belong to EH from 687 * this point but the state of the controller is 688 * unknown. Freeze the port to make sure the IRQ 689 * handler doesn't diddle with those qcs. This must 690 * be done atomically w.r.t. setting ATA_QCFLAG_EH. 691 */ 692 if (nr_timedout) 693 __ata_port_freeze(ap); 694 695 /* initialize eh_tries */ 696 ap->eh_tries = ATA_EH_MAX_TRIES; 697 698 spin_unlock_irqrestore(ap->lock, flags); 699 700 return nr_timedout; 701 } 702 EXPORT_SYMBOL(ata_scsi_cmd_error_handler); 703 704 /** 705 * ata_scsi_port_error_handler - recover the port after the commands 706 * @host: SCSI host containing the port 707 * @ap: the ATA port 708 * 709 * Handle the recovery of the port @ap after all the commands 710 * have been recovered. 711 */ 712 void ata_scsi_port_error_handler(struct Scsi_Host *host, struct ata_port *ap) 713 { 714 unsigned long flags; 715 struct ata_link *link; 716 717 /* acquire EH ownership */ 718 ata_eh_acquire(ap); 719 repeat: 720 /* kill fast drain timer */ 721 timer_delete_sync(&ap->fastdrain_timer); 722 723 /* process port resume request */ 724 ata_eh_handle_port_resume(ap); 725 726 /* fetch & clear EH info */ 727 spin_lock_irqsave(ap->lock, flags); 728 729 ata_for_each_link(link, ap, HOST_FIRST) { 730 struct ata_eh_context *ehc = &link->eh_context; 731 struct ata_device *dev; 732 733 memset(&link->eh_context, 0, sizeof(link->eh_context)); 734 link->eh_context.i = link->eh_info; 735 memset(&link->eh_info, 0, sizeof(link->eh_info)); 736 737 ata_for_each_dev(dev, link, ENABLED) { 738 int devno = dev->devno; 739 740 ehc->saved_xfer_mode[devno] = dev->xfer_mode; 741 if (ata_ncq_enabled(dev)) 742 ehc->saved_ncq_enabled |= 1 << devno; 743 744 /* If we are resuming, wake up the device */ 745 if (ap->pflags & ATA_PFLAG_RESUMING) { 746 dev->flags |= ATA_DFLAG_RESUMING; 747 ehc->i.dev_action[devno] |= ATA_EH_SET_ACTIVE; 748 } 749 } 750 } 751 752 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS; 753 ap->pflags &= ~ATA_PFLAG_EH_PENDING; 754 ap->excl_link = NULL; /* don't maintain exclusion over EH */ 755 756 spin_unlock_irqrestore(ap->lock, flags); 757 758 /* invoke EH, skip if unloading or suspended */ 759 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)) && 760 ata_adapter_is_online(ap)) 761 ap->ops->error_handler(ap); 762 else { 763 /* if unloading, commence suicide */ 764 if ((ap->pflags & ATA_PFLAG_UNLOADING) && 765 !(ap->pflags & ATA_PFLAG_UNLOADED)) 766 ata_eh_unload(ap); 767 ata_eh_finish(ap); 768 } 769 770 /* process port suspend request */ 771 ata_eh_handle_port_suspend(ap); 772 773 /* 774 * Exception might have happened after ->error_handler recovered the 775 * port but before this point. Repeat EH in such case. 776 */ 777 spin_lock_irqsave(ap->lock, flags); 778 779 if (ap->pflags & ATA_PFLAG_EH_PENDING) { 780 if (--ap->eh_tries) { 781 spin_unlock_irqrestore(ap->lock, flags); 782 goto repeat; 783 } 784 ata_port_err(ap, 785 "EH pending after %d tries, giving up\n", 786 ATA_EH_MAX_TRIES); 787 ap->pflags &= ~ATA_PFLAG_EH_PENDING; 788 } 789 790 /* this run is complete, make sure EH info is clear */ 791 ata_for_each_link(link, ap, HOST_FIRST) 792 memset(&link->eh_info, 0, sizeof(link->eh_info)); 793 794 /* 795 * end eh (clear host_eh_scheduled) while holding ap->lock such that if 796 * exception occurs after this point but before EH completion, SCSI 797 * midlayer will re-initiate EH. 798 */ 799 ap->ops->end_eh(ap); 800 801 spin_unlock_irqrestore(ap->lock, flags); 802 ata_eh_release(ap); 803 804 scsi_eh_flush_done_q(&ap->eh_done_q); 805 806 /* clean up */ 807 spin_lock_irqsave(ap->lock, flags); 808 809 ap->pflags &= ~ATA_PFLAG_RESUMING; 810 811 if (ap->pflags & ATA_PFLAG_LOADING) 812 ap->pflags &= ~ATA_PFLAG_LOADING; 813 else if ((ap->pflags & ATA_PFLAG_SCSI_HOTPLUG) && 814 !(ap->flags & ATA_FLAG_SAS_HOST)) 815 queue_delayed_work(system_dfl_long_wq, &ap->hotplug_task, 0); 816 817 if (ap->pflags & ATA_PFLAG_RECOVERED) 818 ata_port_info(ap, "EH complete\n"); 819 820 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED); 821 822 /* tell wait_eh that we're done */ 823 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS; 824 wake_up_all(&ap->eh_wait_q); 825 826 spin_unlock_irqrestore(ap->lock, flags); 827 } 828 EXPORT_SYMBOL_GPL(ata_scsi_port_error_handler); 829 830 /** 831 * ata_port_wait_eh - Wait for the currently pending EH to complete 832 * @ap: Port to wait EH for 833 * 834 * Wait until the currently pending EH is complete. 835 * 836 * LOCKING: 837 * Kernel thread context (may sleep). 838 */ 839 void ata_port_wait_eh(struct ata_port *ap) 840 { 841 unsigned long flags; 842 DEFINE_WAIT(wait); 843 844 retry: 845 spin_lock_irqsave(ap->lock, flags); 846 847 while (ata_port_eh_scheduled(ap)) { 848 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE); 849 spin_unlock_irqrestore(ap->lock, flags); 850 schedule(); 851 spin_lock_irqsave(ap->lock, flags); 852 } 853 finish_wait(&ap->eh_wait_q, &wait); 854 855 spin_unlock_irqrestore(ap->lock, flags); 856 857 /* make sure SCSI EH is complete */ 858 if (scsi_host_in_recovery(ap->scsi_host)) { 859 ata_msleep(ap, 10); 860 goto retry; 861 } 862 } 863 EXPORT_SYMBOL_GPL(ata_port_wait_eh); 864 865 static unsigned int ata_eh_nr_in_flight(struct ata_port *ap) 866 { 867 struct ata_queued_cmd *qc; 868 unsigned int tag; 869 unsigned int nr = 0; 870 871 /* count only non-internal commands */ 872 ata_qc_for_each(ap, qc, tag) { 873 if (qc) 874 nr++; 875 } 876 877 return nr; 878 } 879 880 void ata_eh_fastdrain_timerfn(struct timer_list *t) 881 { 882 struct ata_port *ap = timer_container_of(ap, t, fastdrain_timer); 883 unsigned long flags; 884 unsigned int cnt; 885 886 spin_lock_irqsave(ap->lock, flags); 887 888 cnt = ata_eh_nr_in_flight(ap); 889 890 /* are we done? */ 891 if (!cnt) 892 goto out_unlock; 893 894 if (cnt == ap->fastdrain_cnt) { 895 struct ata_queued_cmd *qc; 896 unsigned int tag; 897 898 /* No progress during the last interval, tag all 899 * in-flight qcs as timed out and freeze the port. 900 */ 901 ata_qc_for_each(ap, qc, tag) { 902 if (qc) 903 qc->err_mask |= AC_ERR_TIMEOUT; 904 } 905 906 ata_port_freeze(ap); 907 } else { 908 /* some qcs have finished, give it another chance */ 909 ap->fastdrain_cnt = cnt; 910 ap->fastdrain_timer.expires = 911 ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL); 912 add_timer(&ap->fastdrain_timer); 913 } 914 915 out_unlock: 916 spin_unlock_irqrestore(ap->lock, flags); 917 } 918 919 /** 920 * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain 921 * @ap: target ATA port 922 * @fastdrain: activate fast drain 923 * 924 * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain 925 * is non-zero and EH wasn't pending before. Fast drain ensures 926 * that EH kicks in in timely manner. 927 * 928 * LOCKING: 929 * spin_lock_irqsave(host lock) 930 */ 931 static void ata_eh_set_pending(struct ata_port *ap, bool fastdrain) 932 { 933 unsigned int cnt; 934 935 /* already scheduled? */ 936 if (ap->pflags & ATA_PFLAG_EH_PENDING) 937 return; 938 939 ap->pflags |= ATA_PFLAG_EH_PENDING; 940 941 /* 942 * If we have deferred QCs, requeue them so that the SCSI EH task can 943 * run. 944 */ 945 ata_scsi_requeue_deferred_qc(ap, NULL); 946 947 if (!fastdrain) 948 return; 949 950 /* do we have in-flight qcs? */ 951 cnt = ata_eh_nr_in_flight(ap); 952 if (!cnt) 953 return; 954 955 /* activate fast drain */ 956 ap->fastdrain_cnt = cnt; 957 ap->fastdrain_timer.expires = 958 ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL); 959 add_timer(&ap->fastdrain_timer); 960 } 961 962 /** 963 * ata_qc_schedule_eh - schedule qc for error handling 964 * @qc: command to schedule error handling for 965 * 966 * Schedule error handling for @qc. EH will kick in as soon as 967 * other commands are drained. 968 * 969 * LOCKING: 970 * spin_lock_irqsave(host lock) 971 */ 972 void ata_qc_schedule_eh(struct ata_queued_cmd *qc) 973 { 974 struct ata_port *ap = qc->ap; 975 976 qc->flags |= ATA_QCFLAG_EH; 977 ata_eh_set_pending(ap, true); 978 979 /* The following will fail if timeout has already expired. 980 * ata_scsi_error() takes care of such scmds on EH entry. 981 * Note that ATA_QCFLAG_EH is unconditionally set after 982 * this function completes. 983 */ 984 blk_abort_request(scsi_cmd_to_rq(qc->scsicmd)); 985 } 986 987 /** 988 * ata_std_sched_eh - non-libsas ata_ports issue eh with this common routine 989 * @ap: ATA port to schedule EH for 990 * 991 * LOCKING: inherited from ata_port_schedule_eh 992 * spin_lock_irqsave(host lock) 993 */ 994 void ata_std_sched_eh(struct ata_port *ap) 995 { 996 if (ap->pflags & ATA_PFLAG_INITIALIZING) 997 return; 998 999 ata_eh_set_pending(ap, true); 1000 scsi_schedule_eh(ap->scsi_host); 1001 1002 trace_ata_std_sched_eh(ap); 1003 } 1004 EXPORT_SYMBOL_GPL(ata_std_sched_eh); 1005 1006 /** 1007 * ata_std_end_eh - non-libsas ata_ports complete eh with this common routine 1008 * @ap: ATA port to end EH for 1009 * 1010 * In the libata object model there is a 1:1 mapping of ata_port to 1011 * shost, so host fields can be directly manipulated under ap->lock, in 1012 * the libsas case we need to hold a lock at the ha->level to coordinate 1013 * these events. 1014 * 1015 * LOCKING: 1016 * spin_lock_irqsave(host lock) 1017 */ 1018 void ata_std_end_eh(struct ata_port *ap) 1019 { 1020 struct Scsi_Host *host = ap->scsi_host; 1021 1022 host->host_eh_scheduled = 0; 1023 } 1024 EXPORT_SYMBOL(ata_std_end_eh); 1025 1026 1027 /** 1028 * ata_port_schedule_eh - schedule error handling without a qc 1029 * @ap: ATA port to schedule EH for 1030 * 1031 * Schedule error handling for @ap. EH will kick in as soon as 1032 * all commands are drained. 1033 * 1034 * LOCKING: 1035 * spin_lock_irqsave(host lock) 1036 */ 1037 void ata_port_schedule_eh(struct ata_port *ap) 1038 { 1039 /* see: ata_std_sched_eh, unless you know better */ 1040 ap->ops->sched_eh(ap); 1041 } 1042 EXPORT_SYMBOL_GPL(ata_port_schedule_eh); 1043 1044 static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link) 1045 { 1046 struct ata_queued_cmd *qc; 1047 int tag, nr_aborted = 0; 1048 1049 /* we're gonna abort all commands, no need for fast drain */ 1050 ata_eh_set_pending(ap, false); 1051 1052 /* include internal tag in iteration */ 1053 ata_qc_for_each_with_internal(ap, qc, tag) { 1054 if (qc && (!link || qc->dev->link == link)) { 1055 qc->flags |= ATA_QCFLAG_EH; 1056 ata_qc_complete(qc); 1057 nr_aborted++; 1058 } 1059 } 1060 1061 if (!nr_aborted) 1062 ata_port_schedule_eh(ap); 1063 1064 return nr_aborted; 1065 } 1066 1067 /** 1068 * ata_link_abort - abort all qc's on the link 1069 * @link: ATA link to abort qc's for 1070 * 1071 * Abort all active qc's active on @link and schedule EH. 1072 * 1073 * LOCKING: 1074 * spin_lock_irqsave(host lock) 1075 * 1076 * RETURNS: 1077 * Number of aborted qc's. 1078 */ 1079 int ata_link_abort(struct ata_link *link) 1080 { 1081 return ata_do_link_abort(link->ap, link); 1082 } 1083 EXPORT_SYMBOL_GPL(ata_link_abort); 1084 1085 /** 1086 * ata_port_abort - abort all qc's on the port 1087 * @ap: ATA port to abort qc's for 1088 * 1089 * Abort all active qc's of @ap and schedule EH. 1090 * 1091 * LOCKING: 1092 * spin_lock_irqsave(host_set lock) 1093 * 1094 * RETURNS: 1095 * Number of aborted qc's. 1096 */ 1097 int ata_port_abort(struct ata_port *ap) 1098 { 1099 return ata_do_link_abort(ap, NULL); 1100 } 1101 EXPORT_SYMBOL_GPL(ata_port_abort); 1102 1103 /** 1104 * __ata_port_freeze - freeze port 1105 * @ap: ATA port to freeze 1106 * 1107 * This function is called when HSM violation or some other 1108 * condition disrupts normal operation of the port. Frozen port 1109 * is not allowed to perform any operation until the port is 1110 * thawed, which usually follows a successful reset. 1111 * 1112 * ap->ops->freeze() callback can be used for freezing the port 1113 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a 1114 * port cannot be frozen hardware-wise, the interrupt handler 1115 * must ack and clear interrupts unconditionally while the port 1116 * is frozen. 1117 * 1118 * LOCKING: 1119 * spin_lock_irqsave(host lock) 1120 */ 1121 static void __ata_port_freeze(struct ata_port *ap) 1122 { 1123 if (ap->ops->freeze) 1124 ap->ops->freeze(ap); 1125 1126 ap->pflags |= ATA_PFLAG_FROZEN; 1127 1128 trace_ata_port_freeze(ap); 1129 } 1130 1131 /** 1132 * ata_port_freeze - abort & freeze port 1133 * @ap: ATA port to freeze 1134 * 1135 * Abort and freeze @ap. The freeze operation must be called 1136 * first, because some hardware requires special operations 1137 * before the taskfile registers are accessible. 1138 * 1139 * LOCKING: 1140 * spin_lock_irqsave(host lock) 1141 * 1142 * RETURNS: 1143 * Number of aborted commands. 1144 */ 1145 int ata_port_freeze(struct ata_port *ap) 1146 { 1147 __ata_port_freeze(ap); 1148 1149 return ata_port_abort(ap); 1150 } 1151 EXPORT_SYMBOL_GPL(ata_port_freeze); 1152 1153 /** 1154 * ata_eh_freeze_port - EH helper to freeze port 1155 * @ap: ATA port to freeze 1156 * 1157 * Freeze @ap. 1158 * 1159 * LOCKING: 1160 * None. 1161 */ 1162 void ata_eh_freeze_port(struct ata_port *ap) 1163 { 1164 unsigned long flags; 1165 1166 spin_lock_irqsave(ap->lock, flags); 1167 __ata_port_freeze(ap); 1168 spin_unlock_irqrestore(ap->lock, flags); 1169 } 1170 EXPORT_SYMBOL_GPL(ata_eh_freeze_port); 1171 1172 /** 1173 * ata_eh_thaw_port - EH helper to thaw port 1174 * @ap: ATA port to thaw 1175 * 1176 * Thaw frozen port @ap. 1177 * 1178 * LOCKING: 1179 * None. 1180 */ 1181 void ata_eh_thaw_port(struct ata_port *ap) 1182 { 1183 unsigned long flags; 1184 1185 spin_lock_irqsave(ap->lock, flags); 1186 1187 ap->pflags &= ~ATA_PFLAG_FROZEN; 1188 1189 if (ap->ops->thaw) 1190 ap->ops->thaw(ap); 1191 1192 spin_unlock_irqrestore(ap->lock, flags); 1193 1194 trace_ata_port_thaw(ap); 1195 } 1196 1197 static void ata_eh_scsidone(struct scsi_cmnd *scmd) 1198 { 1199 /* nada */ 1200 } 1201 1202 static void __ata_eh_qc_complete(struct ata_queued_cmd *qc) 1203 { 1204 struct ata_port *ap = qc->ap; 1205 struct scsi_cmnd *scmd = qc->scsicmd; 1206 unsigned long flags; 1207 1208 spin_lock_irqsave(ap->lock, flags); 1209 qc->scsidone = ata_eh_scsidone; 1210 __ata_qc_complete(qc); 1211 WARN_ON(ata_tag_valid(qc->tag)); 1212 spin_unlock_irqrestore(ap->lock, flags); 1213 1214 scsi_eh_finish_cmd(scmd, &ap->eh_done_q); 1215 } 1216 1217 /** 1218 * ata_eh_qc_complete - Complete an active ATA command from EH 1219 * @qc: Command to complete 1220 * 1221 * Indicate to the mid and upper layers that an ATA command has 1222 * completed. To be used from EH. 1223 */ 1224 void ata_eh_qc_complete(struct ata_queued_cmd *qc) 1225 { 1226 struct scsi_cmnd *scmd = qc->scsicmd; 1227 scmd->retries = scmd->allowed; 1228 __ata_eh_qc_complete(qc); 1229 } 1230 1231 /** 1232 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH 1233 * @qc: Command to retry 1234 * 1235 * Indicate to the mid and upper layers that an ATA command 1236 * should be retried. To be used from EH. 1237 * 1238 * SCSI midlayer limits the number of retries to scmd->allowed. 1239 * scmd->allowed is incremented for commands which get retried 1240 * due to unrelated failures (qc->err_mask is zero). 1241 */ 1242 void ata_eh_qc_retry(struct ata_queued_cmd *qc) 1243 { 1244 struct scsi_cmnd *scmd = qc->scsicmd; 1245 if (!qc->err_mask) 1246 scmd->allowed++; 1247 __ata_eh_qc_complete(qc); 1248 } 1249 1250 /** 1251 * ata_dev_disable - disable ATA device 1252 * @dev: ATA device to disable 1253 * 1254 * Disable @dev. 1255 * 1256 * Locking: 1257 * EH context. 1258 */ 1259 void ata_dev_disable(struct ata_device *dev) 1260 { 1261 if (!ata_dev_enabled(dev)) 1262 return; 1263 1264 ata_dev_warn(dev, "disable device\n"); 1265 1266 ata_eh_dev_disable(dev); 1267 } 1268 EXPORT_SYMBOL_GPL(ata_dev_disable); 1269 1270 /** 1271 * ata_eh_detach_dev - detach ATA device 1272 * @dev: ATA device to detach 1273 * 1274 * Detach @dev. 1275 * 1276 * LOCKING: 1277 * None. 1278 */ 1279 void ata_eh_detach_dev(struct ata_device *dev) 1280 { 1281 struct ata_link *link = dev->link; 1282 struct ata_port *ap = link->ap; 1283 struct ata_eh_context *ehc = &link->eh_context; 1284 unsigned long flags; 1285 1286 /* 1287 * If the device is still enabled, transition it to standby power mode 1288 * (i.e. spin down HDDs) and disable it. 1289 */ 1290 if (ata_dev_enabled(dev)) { 1291 ata_dev_power_set_standby(dev); 1292 ata_eh_dev_disable(dev); 1293 } 1294 1295 spin_lock_irqsave(ap->lock, flags); 1296 1297 dev->flags &= ~ATA_DFLAG_DETACH; 1298 1299 if (ata_scsi_offline_dev(dev)) { 1300 dev->flags |= ATA_DFLAG_DETACHED; 1301 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG; 1302 } 1303 1304 /* clear per-dev EH info */ 1305 ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK); 1306 ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK); 1307 ehc->saved_xfer_mode[dev->devno] = 0; 1308 ehc->saved_ncq_enabled &= ~(1 << dev->devno); 1309 1310 spin_unlock_irqrestore(ap->lock, flags); 1311 } 1312 1313 /** 1314 * ata_eh_about_to_do - about to perform eh_action 1315 * @link: target ATA link 1316 * @dev: target ATA dev for per-dev action (can be NULL) 1317 * @action: action about to be performed 1318 * 1319 * Called just before performing EH actions to clear related bits 1320 * in @link->eh_info such that eh actions are not unnecessarily 1321 * repeated. 1322 * 1323 * LOCKING: 1324 * None. 1325 */ 1326 void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev, 1327 unsigned int action) 1328 { 1329 struct ata_port *ap = link->ap; 1330 struct ata_eh_info *ehi = &link->eh_info; 1331 struct ata_eh_context *ehc = &link->eh_context; 1332 unsigned long flags; 1333 1334 trace_ata_eh_about_to_do(link, dev ? dev->devno : 0, action); 1335 1336 spin_lock_irqsave(ap->lock, flags); 1337 1338 ata_eh_clear_action(link, dev, ehi, action); 1339 1340 /* About to take EH action, set RECOVERED. Ignore actions on 1341 * slave links as master will do them again. 1342 */ 1343 if (!(ehc->i.flags & ATA_EHI_QUIET) && link != ap->slave_link) 1344 ap->pflags |= ATA_PFLAG_RECOVERED; 1345 1346 spin_unlock_irqrestore(ap->lock, flags); 1347 } 1348 1349 /** 1350 * ata_eh_done - EH action complete 1351 * @link: ATA link for which EH actions are complete 1352 * @dev: target ATA dev for per-dev action (can be NULL) 1353 * @action: action just completed 1354 * 1355 * Called right after performing EH actions to clear related bits 1356 * in @link->eh_context. 1357 * 1358 * LOCKING: 1359 * None. 1360 */ 1361 void ata_eh_done(struct ata_link *link, struct ata_device *dev, 1362 unsigned int action) 1363 { 1364 struct ata_eh_context *ehc = &link->eh_context; 1365 1366 trace_ata_eh_done(link, dev ? dev->devno : 0, action); 1367 1368 ata_eh_clear_action(link, dev, &ehc->i, action); 1369 } 1370 1371 /** 1372 * ata_err_string - convert err_mask to descriptive string 1373 * @err_mask: error mask to convert to string 1374 * 1375 * Convert @err_mask to descriptive string. Errors are 1376 * prioritized according to severity and only the most severe 1377 * error is reported. 1378 * 1379 * LOCKING: 1380 * None. 1381 * 1382 * RETURNS: 1383 * Descriptive string for @err_mask 1384 */ 1385 static const char *ata_err_string(unsigned int err_mask) 1386 { 1387 if (err_mask & AC_ERR_HOST_BUS) 1388 return "host bus error"; 1389 if (err_mask & AC_ERR_ATA_BUS) 1390 return "ATA bus error"; 1391 if (err_mask & AC_ERR_TIMEOUT) 1392 return "timeout"; 1393 if (err_mask & AC_ERR_HSM) 1394 return "HSM violation"; 1395 if (err_mask & AC_ERR_SYSTEM) 1396 return "internal error"; 1397 if (err_mask & AC_ERR_MEDIA) 1398 return "media error"; 1399 if (err_mask & AC_ERR_INVALID) 1400 return "invalid argument"; 1401 if (err_mask & AC_ERR_DEV) 1402 return "device error"; 1403 if (err_mask & AC_ERR_NCQ) 1404 return "NCQ error"; 1405 if (err_mask & AC_ERR_NODEV_HINT) 1406 return "Polling detection error"; 1407 return "unknown error"; 1408 } 1409 1410 /** 1411 * atapi_eh_tur - perform ATAPI TEST_UNIT_READY 1412 * @dev: target ATAPI device 1413 * @r_sense_key: out parameter for sense_key 1414 * 1415 * Perform ATAPI TEST_UNIT_READY. 1416 * 1417 * LOCKING: 1418 * EH context (may sleep). 1419 * 1420 * RETURNS: 1421 * 0 on success, AC_ERR_* mask on failure. 1422 */ 1423 unsigned int atapi_eh_tur(struct ata_device *dev, u8 *r_sense_key) 1424 { 1425 u8 cdb[ATAPI_CDB_LEN] = { TEST_UNIT_READY, 0, 0, 0, 0, 0 }; 1426 struct ata_taskfile tf; 1427 unsigned int err_mask; 1428 1429 ata_tf_init(dev, &tf); 1430 1431 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 1432 tf.command = ATA_CMD_PACKET; 1433 tf.protocol = ATAPI_PROT_NODATA; 1434 1435 err_mask = ata_exec_internal(dev, &tf, cdb, DMA_NONE, NULL, 0, 0); 1436 if (err_mask == AC_ERR_DEV) 1437 *r_sense_key = tf.error >> 4; 1438 return err_mask; 1439 } 1440 1441 /** 1442 * ata_eh_decide_disposition - Disposition a qc based on sense data 1443 * @qc: qc to examine 1444 * 1445 * For a regular SCSI command, the SCSI completion callback (scsi_done()) 1446 * will call scsi_complete(), which will call scsi_decide_disposition(), 1447 * which will call scsi_check_sense(). scsi_complete() finally calls 1448 * scsi_finish_command(). This is fine for SCSI, since any eventual sense 1449 * data is usually returned in the completion itself (without invoking SCSI 1450 * EH). However, for a QC, we always need to fetch the sense data 1451 * explicitly using SCSI EH. 1452 * 1453 * A command that is completed via SCSI EH will instead be completed using 1454 * scsi_eh_flush_done_q(), which will call scsi_finish_command() directly 1455 * (without ever calling scsi_check_sense()). 1456 * 1457 * For a command that went through SCSI EH, it is the responsibility of the 1458 * SCSI EH strategy handler to call scsi_decide_disposition(), see e.g. how 1459 * scsi_eh_get_sense() calls scsi_decide_disposition() for SCSI LLDDs that 1460 * do not get the sense data as part of the completion. 1461 * 1462 * Thus, for QC commands that went via SCSI EH, we need to call 1463 * scsi_check_sense() ourselves, similar to how scsi_eh_get_sense() calls 1464 * scsi_decide_disposition(), which calls scsi_check_sense(), in order to 1465 * set the correct SCSI ML byte (if any). 1466 * 1467 * LOCKING: 1468 * EH context. 1469 * 1470 * RETURNS: 1471 * SUCCESS or FAILED or NEEDS_RETRY or ADD_TO_MLQUEUE 1472 */ 1473 enum scsi_disposition ata_eh_decide_disposition(struct ata_queued_cmd *qc) 1474 { 1475 return scsi_check_sense(qc->scsicmd); 1476 } 1477 1478 /** 1479 * ata_eh_request_sense - perform REQUEST_SENSE_DATA_EXT 1480 * @qc: qc to perform REQUEST_SENSE_SENSE_DATA_EXT to 1481 * 1482 * Perform REQUEST_SENSE_DATA_EXT after the device reported CHECK 1483 * SENSE. This function is an EH helper. 1484 * 1485 * LOCKING: 1486 * Kernel thread context (may sleep). 1487 * 1488 * RETURNS: 1489 * true if sense data could be fetched, false otherwise. 1490 */ 1491 static bool ata_eh_request_sense(struct ata_queued_cmd *qc) 1492 { 1493 struct scsi_cmnd *cmd = qc->scsicmd; 1494 struct ata_device *dev = qc->dev; 1495 struct ata_taskfile tf; 1496 unsigned int err_mask; 1497 1498 if (ata_port_is_frozen(qc->ap)) { 1499 ata_dev_warn(dev, "sense data available but port frozen\n"); 1500 return false; 1501 } 1502 1503 if (!ata_id_sense_reporting_enabled(dev->id)) { 1504 ata_dev_warn(qc->dev, "sense data reporting disabled\n"); 1505 return false; 1506 } 1507 1508 ata_tf_init(dev, &tf); 1509 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 1510 tf.flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48; 1511 tf.command = ATA_CMD_REQ_SENSE_DATA; 1512 tf.protocol = ATA_PROT_NODATA; 1513 1514 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0); 1515 /* Ignore err_mask; ATA_ERR might be set */ 1516 if (tf.status & ATA_SENSE) { 1517 if (ata_scsi_sense_is_valid(tf.lbah, tf.lbam, tf.lbal)) { 1518 /* Set sense without also setting scsicmd->result */ 1519 scsi_build_sense_buffer(dev->flags & ATA_DFLAG_D_SENSE, 1520 cmd->sense_buffer, tf.lbah, 1521 tf.lbam, tf.lbal); 1522 qc->flags |= ATA_QCFLAG_SENSE_VALID; 1523 return true; 1524 } 1525 } else { 1526 ata_dev_warn(dev, "request sense failed stat %02x emask %x\n", 1527 tf.status, err_mask); 1528 } 1529 1530 return false; 1531 } 1532 1533 /** 1534 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE 1535 * @dev: device to perform REQUEST_SENSE to 1536 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long) 1537 * @dfl_sense_key: default sense key to use 1538 * 1539 * Perform ATAPI REQUEST_SENSE after the device reported CHECK 1540 * SENSE. This function is EH helper. 1541 * 1542 * LOCKING: 1543 * Kernel thread context (may sleep). 1544 * 1545 * RETURNS: 1546 * 0 on success, AC_ERR_* mask on failure 1547 */ 1548 unsigned int atapi_eh_request_sense(struct ata_device *dev, 1549 u8 *sense_buf, u8 dfl_sense_key) 1550 { 1551 u8 cdb[ATAPI_CDB_LEN] = 1552 { REQUEST_SENSE, 0, 0, 0, SCSI_SENSE_BUFFERSIZE, 0 }; 1553 struct ata_port *ap = dev->link->ap; 1554 struct ata_taskfile tf; 1555 1556 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE); 1557 1558 /* initialize sense_buf with the error register, 1559 * for the case where they are -not- overwritten 1560 */ 1561 sense_buf[0] = 0x70; 1562 sense_buf[2] = dfl_sense_key; 1563 1564 /* some devices time out if garbage left in tf */ 1565 ata_tf_init(dev, &tf); 1566 1567 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 1568 tf.command = ATA_CMD_PACKET; 1569 1570 /* 1571 * Do not use DMA if the connected device only supports PIO, even if the 1572 * port prefers PIO commands via DMA. 1573 * 1574 * Ideally, we should call atapi_check_dma() to check if it is safe for 1575 * the LLD to use DMA for REQUEST_SENSE, but we don't have a qc. 1576 * Since we can't check the command, perhaps we should only use pio? 1577 */ 1578 if ((ap->flags & ATA_FLAG_PIO_DMA) && !(dev->flags & ATA_DFLAG_PIO)) { 1579 tf.protocol = ATAPI_PROT_DMA; 1580 tf.feature |= ATAPI_PKT_DMA; 1581 } else { 1582 tf.protocol = ATAPI_PROT_PIO; 1583 tf.lbam = SCSI_SENSE_BUFFERSIZE; 1584 tf.lbah = 0; 1585 } 1586 1587 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE, 1588 sense_buf, SCSI_SENSE_BUFFERSIZE, 0); 1589 } 1590 1591 /** 1592 * ata_eh_analyze_serror - analyze SError for a failed port 1593 * @link: ATA link to analyze SError for 1594 * 1595 * Analyze SError if available and further determine cause of 1596 * failure. 1597 * 1598 * LOCKING: 1599 * None. 1600 */ 1601 static void ata_eh_analyze_serror(struct ata_link *link) 1602 { 1603 struct ata_eh_context *ehc = &link->eh_context; 1604 u32 serror = ehc->i.serror; 1605 unsigned int err_mask = 0, action = 0; 1606 u32 hotplug_mask; 1607 1608 if (serror & (SERR_PERSISTENT | SERR_DATA)) { 1609 err_mask |= AC_ERR_ATA_BUS; 1610 action |= ATA_EH_RESET; 1611 } 1612 if (serror & SERR_PROTOCOL) { 1613 err_mask |= AC_ERR_HSM; 1614 action |= ATA_EH_RESET; 1615 } 1616 if (serror & SERR_INTERNAL) { 1617 err_mask |= AC_ERR_SYSTEM; 1618 action |= ATA_EH_RESET; 1619 } 1620 1621 /* Determine whether a hotplug event has occurred. Both 1622 * SError.N/X are considered hotplug events for enabled or 1623 * host links. For disabled PMP links, only N bit is 1624 * considered as X bit is left at 1 for link plugging. 1625 */ 1626 if (link->lpm_policy > ATA_LPM_MAX_POWER) 1627 hotplug_mask = 0; /* hotplug doesn't work w/ LPM */ 1628 else if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link)) 1629 hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG; 1630 else 1631 hotplug_mask = SERR_PHYRDY_CHG; 1632 1633 if (serror & hotplug_mask) 1634 ata_ehi_hotplugged(&ehc->i); 1635 1636 ehc->i.err_mask |= err_mask; 1637 ehc->i.action |= action; 1638 } 1639 1640 /** 1641 * ata_eh_analyze_tf - analyze taskfile of a failed qc 1642 * @qc: qc to analyze 1643 * 1644 * Analyze taskfile of @qc and further determine cause of 1645 * failure. This function also requests ATAPI sense data if 1646 * available. 1647 * 1648 * LOCKING: 1649 * Kernel thread context (may sleep). 1650 * 1651 * RETURNS: 1652 * Determined recovery action 1653 */ 1654 static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc) 1655 { 1656 const struct ata_taskfile *tf = &qc->result_tf; 1657 unsigned int tmp, action = 0; 1658 u8 stat = tf->status, err = tf->error; 1659 1660 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) { 1661 qc->err_mask |= AC_ERR_HSM; 1662 return ATA_EH_RESET; 1663 } 1664 1665 if (stat & (ATA_ERR | ATA_DF)) { 1666 qc->err_mask |= AC_ERR_DEV; 1667 /* 1668 * Sense data reporting does not work if the 1669 * device fault bit is set. 1670 */ 1671 if (stat & ATA_DF) 1672 stat &= ~ATA_SENSE; 1673 } else { 1674 return 0; 1675 } 1676 1677 switch (qc->dev->class) { 1678 case ATA_DEV_ATA: 1679 case ATA_DEV_ZAC: 1680 /* 1681 * Fetch the sense data explicitly if: 1682 * -It was a non-NCQ command that failed, or 1683 * -It was a NCQ command that failed, but the sense data 1684 * was not included in the NCQ command error log 1685 * (i.e. NCQ autosense is not supported by the device). 1686 */ 1687 if (!(qc->flags & ATA_QCFLAG_SENSE_VALID) && 1688 (stat & ATA_SENSE) && ata_eh_request_sense(qc)) 1689 set_status_byte(qc->scsicmd, SAM_STAT_CHECK_CONDITION); 1690 if (err & ATA_ICRC) 1691 qc->err_mask |= AC_ERR_ATA_BUS; 1692 if (err & (ATA_UNC | ATA_AMNF)) 1693 qc->err_mask |= AC_ERR_MEDIA; 1694 if (err & ATA_IDNF) 1695 qc->err_mask |= AC_ERR_INVALID; 1696 break; 1697 1698 case ATA_DEV_ATAPI: 1699 if (!ata_port_is_frozen(qc->ap)) { 1700 tmp = atapi_eh_request_sense(qc->dev, 1701 qc->scsicmd->sense_buffer, 1702 qc->result_tf.error >> 4); 1703 if (!tmp) 1704 qc->flags |= ATA_QCFLAG_SENSE_VALID; 1705 else 1706 qc->err_mask |= tmp; 1707 } 1708 } 1709 1710 if (qc->flags & ATA_QCFLAG_SENSE_VALID) { 1711 enum scsi_disposition ret = ata_eh_decide_disposition(qc); 1712 1713 /* 1714 * SUCCESS here means that the sense code could be 1715 * evaluated and should be passed to the upper layers 1716 * for correct evaluation. 1717 * FAILED means the sense code could not be interpreted 1718 * and the device would need to be reset. 1719 * NEEDS_RETRY and ADD_TO_MLQUEUE means that the 1720 * command would need to be retried. 1721 */ 1722 if (ret == NEEDS_RETRY || ret == ADD_TO_MLQUEUE) { 1723 qc->flags |= ATA_QCFLAG_RETRY; 1724 qc->err_mask |= AC_ERR_OTHER; 1725 } else if (ret != SUCCESS) { 1726 qc->err_mask |= AC_ERR_HSM; 1727 } 1728 } 1729 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS)) 1730 action |= ATA_EH_RESET; 1731 1732 return action; 1733 } 1734 1735 static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask, 1736 int *xfer_ok) 1737 { 1738 int base = 0; 1739 1740 if (!(eflags & ATA_EFLAG_DUBIOUS_XFER)) 1741 *xfer_ok = 1; 1742 1743 if (!*xfer_ok) 1744 base = ATA_ECAT_DUBIOUS_NONE; 1745 1746 if (err_mask & AC_ERR_ATA_BUS) 1747 return base + ATA_ECAT_ATA_BUS; 1748 1749 if (err_mask & AC_ERR_TIMEOUT) 1750 return base + ATA_ECAT_TOUT_HSM; 1751 1752 if (eflags & ATA_EFLAG_IS_IO) { 1753 if (err_mask & AC_ERR_HSM) 1754 return base + ATA_ECAT_TOUT_HSM; 1755 if ((err_mask & 1756 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV) 1757 return base + ATA_ECAT_UNK_DEV; 1758 } 1759 1760 return 0; 1761 } 1762 1763 struct speed_down_verdict_arg { 1764 u64 since; 1765 int xfer_ok; 1766 int nr_errors[ATA_ECAT_NR]; 1767 }; 1768 1769 static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg) 1770 { 1771 struct speed_down_verdict_arg *arg = void_arg; 1772 int cat; 1773 1774 if ((ent->eflags & ATA_EFLAG_OLD_ER) || (ent->timestamp < arg->since)) 1775 return -1; 1776 1777 cat = ata_eh_categorize_error(ent->eflags, ent->err_mask, 1778 &arg->xfer_ok); 1779 arg->nr_errors[cat]++; 1780 1781 return 0; 1782 } 1783 1784 /** 1785 * ata_eh_speed_down_verdict - Determine speed down verdict 1786 * @dev: Device of interest 1787 * 1788 * This function examines error ring of @dev and determines 1789 * whether NCQ needs to be turned off, transfer speed should be 1790 * stepped down, or falling back to PIO is necessary. 1791 * 1792 * ECAT_ATA_BUS : ATA_BUS error for any command 1793 * 1794 * ECAT_TOUT_HSM : TIMEOUT for any command or HSM violation for 1795 * IO commands 1796 * 1797 * ECAT_UNK_DEV : Unknown DEV error for IO commands 1798 * 1799 * ECAT_DUBIOUS_* : Identical to above three but occurred while 1800 * data transfer hasn't been verified. 1801 * 1802 * Verdicts are 1803 * 1804 * NCQ_OFF : Turn off NCQ. 1805 * 1806 * SPEED_DOWN : Speed down transfer speed but don't fall back 1807 * to PIO. 1808 * 1809 * FALLBACK_TO_PIO : Fall back to PIO. 1810 * 1811 * Even if multiple verdicts are returned, only one action is 1812 * taken per error. An action triggered by non-DUBIOUS errors 1813 * clears ering, while one triggered by DUBIOUS_* errors doesn't. 1814 * This is to expedite speed down decisions right after device is 1815 * initially configured. 1816 * 1817 * The following are speed down rules. #1 and #2 deal with 1818 * DUBIOUS errors. 1819 * 1820 * 1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors 1821 * occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO. 1822 * 1823 * 2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors 1824 * occurred during last 5 mins, NCQ_OFF. 1825 * 1826 * 3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors 1827 * occurred during last 5 mins, FALLBACK_TO_PIO 1828 * 1829 * 4. If more than 3 TOUT_HSM or UNK_DEV errors occurred 1830 * during last 10 mins, NCQ_OFF. 1831 * 1832 * 5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6 1833 * UNK_DEV errors occurred during last 10 mins, SPEED_DOWN. 1834 * 1835 * LOCKING: 1836 * Inherited from caller. 1837 * 1838 * RETURNS: 1839 * OR of ATA_EH_SPDN_* flags. 1840 */ 1841 static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev) 1842 { 1843 const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ; 1844 u64 j64 = get_jiffies_64(); 1845 struct speed_down_verdict_arg arg; 1846 unsigned int verdict = 0; 1847 1848 /* scan past 5 mins of error history */ 1849 memset(&arg, 0, sizeof(arg)); 1850 arg.since = j64 - min(j64, j5mins); 1851 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg); 1852 1853 if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] + 1854 arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1) 1855 verdict |= ATA_EH_SPDN_SPEED_DOWN | 1856 ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS; 1857 1858 if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] + 1859 arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1) 1860 verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS; 1861 1862 if (arg.nr_errors[ATA_ECAT_ATA_BUS] + 1863 arg.nr_errors[ATA_ECAT_TOUT_HSM] + 1864 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6) 1865 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO; 1866 1867 /* scan past 10 mins of error history */ 1868 memset(&arg, 0, sizeof(arg)); 1869 arg.since = j64 - min(j64, j10mins); 1870 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg); 1871 1872 if (arg.nr_errors[ATA_ECAT_TOUT_HSM] + 1873 arg.nr_errors[ATA_ECAT_UNK_DEV] > 3) 1874 verdict |= ATA_EH_SPDN_NCQ_OFF; 1875 1876 if (arg.nr_errors[ATA_ECAT_ATA_BUS] + 1877 arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 || 1878 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6) 1879 verdict |= ATA_EH_SPDN_SPEED_DOWN; 1880 1881 return verdict; 1882 } 1883 1884 /** 1885 * ata_eh_speed_down - record error and speed down if necessary 1886 * @dev: Failed device 1887 * @eflags: mask of ATA_EFLAG_* flags 1888 * @err_mask: err_mask of the error 1889 * 1890 * Record error and examine error history to determine whether 1891 * adjusting transmission speed is necessary. It also sets 1892 * transmission limits appropriately if such adjustment is 1893 * necessary. 1894 * 1895 * LOCKING: 1896 * Kernel thread context (may sleep). 1897 * 1898 * RETURNS: 1899 * Determined recovery action. 1900 */ 1901 static unsigned int ata_eh_speed_down(struct ata_device *dev, 1902 unsigned int eflags, unsigned int err_mask) 1903 { 1904 struct ata_link *link = ata_dev_phys_link(dev); 1905 int xfer_ok = 0; 1906 unsigned int verdict; 1907 unsigned int action = 0; 1908 1909 /* don't bother if Cat-0 error */ 1910 if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0) 1911 return 0; 1912 1913 /* record error and determine whether speed down is necessary */ 1914 ata_ering_record(&dev->ering, eflags, err_mask); 1915 verdict = ata_eh_speed_down_verdict(dev); 1916 1917 /* turn off NCQ? */ 1918 if ((verdict & ATA_EH_SPDN_NCQ_OFF) && ata_ncq_enabled(dev)) { 1919 dev->flags |= ATA_DFLAG_NCQ_OFF; 1920 ata_dev_warn(dev, "NCQ disabled due to excessive errors\n"); 1921 goto done; 1922 } 1923 1924 /* speed down? */ 1925 if (verdict & ATA_EH_SPDN_SPEED_DOWN) { 1926 /* speed down SATA link speed if possible */ 1927 if (sata_down_spd_limit(link, 0) == 0) { 1928 action |= ATA_EH_RESET; 1929 goto done; 1930 } 1931 1932 /* lower transfer mode */ 1933 if (dev->spdn_cnt < 2) { 1934 static const int dma_dnxfer_sel[] = 1935 { ATA_DNXFER_DMA, ATA_DNXFER_40C }; 1936 static const int pio_dnxfer_sel[] = 1937 { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 }; 1938 int sel; 1939 1940 if (dev->xfer_shift != ATA_SHIFT_PIO) 1941 sel = dma_dnxfer_sel[dev->spdn_cnt]; 1942 else 1943 sel = pio_dnxfer_sel[dev->spdn_cnt]; 1944 1945 dev->spdn_cnt++; 1946 1947 if (ata_down_xfermask_limit(dev, sel) == 0) { 1948 action |= ATA_EH_RESET; 1949 goto done; 1950 } 1951 } 1952 } 1953 1954 /* Fall back to PIO? Slowing down to PIO is meaningless for 1955 * SATA ATA devices. Consider it only for PATA and SATAPI. 1956 */ 1957 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) && 1958 (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) && 1959 (dev->xfer_shift != ATA_SHIFT_PIO)) { 1960 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) { 1961 dev->spdn_cnt = 0; 1962 action |= ATA_EH_RESET; 1963 goto done; 1964 } 1965 } 1966 1967 return 0; 1968 done: 1969 /* device has been slowed down, blow error history */ 1970 if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS)) 1971 ata_ering_clear(&dev->ering); 1972 return action; 1973 } 1974 1975 /** 1976 * ata_eh_worth_retry - analyze error and decide whether to retry 1977 * @qc: qc to possibly retry 1978 * 1979 * Look at the cause of the error and decide if a retry 1980 * might be useful or not. We don't want to retry media errors 1981 * because the drive itself has probably already taken 10-30 seconds 1982 * doing its own internal retries before reporting the failure. 1983 */ 1984 static inline int ata_eh_worth_retry(struct ata_queued_cmd *qc) 1985 { 1986 if (qc->err_mask & AC_ERR_MEDIA) 1987 return 0; /* don't retry media errors */ 1988 if (qc->flags & ATA_QCFLAG_IO) 1989 return 1; /* otherwise retry anything from fs stack */ 1990 if (qc->err_mask & AC_ERR_INVALID) 1991 return 0; /* don't retry these */ 1992 return qc->err_mask != AC_ERR_DEV; /* retry if not dev error */ 1993 } 1994 1995 /** 1996 * ata_eh_quiet - check if we need to be quiet about a command error 1997 * @qc: qc to check 1998 * 1999 * Look at the qc flags anbd its scsi command request flags to determine 2000 * if we need to be quiet about the command failure. 2001 */ 2002 static inline bool ata_eh_quiet(struct ata_queued_cmd *qc) 2003 { 2004 if (qc->scsicmd && scsi_cmd_to_rq(qc->scsicmd)->rq_flags & RQF_QUIET) 2005 qc->flags |= ATA_QCFLAG_QUIET; 2006 return qc->flags & ATA_QCFLAG_QUIET; 2007 } 2008 2009 static int ata_eh_get_non_ncq_success_sense(struct ata_link *link) 2010 { 2011 struct ata_port *ap = link->ap; 2012 struct ata_queued_cmd *qc; 2013 2014 qc = __ata_qc_from_tag(ap, link->active_tag); 2015 if (!qc) 2016 return -EIO; 2017 2018 if (!(qc->flags & ATA_QCFLAG_EH) || 2019 !(qc->flags & ATA_QCFLAG_EH_SUCCESS_CMD) || 2020 qc->err_mask) 2021 return -EIO; 2022 2023 if (!ata_eh_request_sense(qc)) 2024 return -EIO; 2025 2026 /* 2027 * No point in checking the return value, since the command has already 2028 * completed successfully. 2029 */ 2030 ata_eh_decide_disposition(qc); 2031 2032 return 0; 2033 } 2034 2035 static void ata_eh_get_success_sense(struct ata_link *link) 2036 { 2037 struct ata_eh_context *ehc = &link->eh_context; 2038 struct ata_device *dev = link->device; 2039 struct ata_port *ap = link->ap; 2040 struct ata_queued_cmd *qc; 2041 int tag, ret = 0; 2042 2043 if (!(ehc->i.dev_action[dev->devno] & ATA_EH_GET_SUCCESS_SENSE)) 2044 return; 2045 2046 /* if frozen, we can't do much */ 2047 if (ata_port_is_frozen(ap)) { 2048 ata_dev_warn(dev, 2049 "successful sense data available but port frozen\n"); 2050 goto out; 2051 } 2052 2053 /* 2054 * If the link has sactive set, then we have outstanding NCQ commands 2055 * and have to read the Successful NCQ Commands log to get the sense 2056 * data. Otherwise, we are dealing with a non-NCQ command and use 2057 * request sense ext command to retrieve the sense data. 2058 */ 2059 if (link->sactive) 2060 ret = ata_eh_get_ncq_success_sense(link); 2061 else 2062 ret = ata_eh_get_non_ncq_success_sense(link); 2063 if (ret) 2064 goto out; 2065 2066 ata_eh_done(link, dev, ATA_EH_GET_SUCCESS_SENSE); 2067 return; 2068 2069 out: 2070 /* 2071 * If we failed to get sense data for a successful command that ought to 2072 * have sense data, we cannot simply return BLK_STS_OK to user space. 2073 * This is because we can't know if the sense data that we couldn't get 2074 * was actually "DATA CURRENTLY UNAVAILABLE". Reporting such a command 2075 * as success to user space would result in a silent data corruption. 2076 * Thus, add a bogus ABORTED_COMMAND sense data to such commands, such 2077 * that SCSI will report these commands as BLK_STS_IOERR to user space. 2078 */ 2079 ata_qc_for_each_raw(ap, qc, tag) { 2080 if (!(qc->flags & ATA_QCFLAG_EH) || 2081 !(qc->flags & ATA_QCFLAG_EH_SUCCESS_CMD) || 2082 qc->err_mask || 2083 ata_dev_phys_link(qc->dev) != link) 2084 continue; 2085 2086 /* We managed to get sense for this success command, skip. */ 2087 if (qc->flags & ATA_QCFLAG_SENSE_VALID) 2088 continue; 2089 2090 /* This success command did not have any sense data, skip. */ 2091 if (!(qc->result_tf.status & ATA_SENSE)) 2092 continue; 2093 2094 /* This success command had sense data, but we failed to get. */ 2095 ata_scsi_set_sense(dev, qc->scsicmd, ABORTED_COMMAND, 0, 0); 2096 qc->flags |= ATA_QCFLAG_SENSE_VALID; 2097 } 2098 ata_eh_done(link, dev, ATA_EH_GET_SUCCESS_SENSE); 2099 } 2100 2101 /* 2102 * Check if a link is established. This is a relaxed version of 2103 * ata_phys_link_online() which accounts for the fact that this is potentially 2104 * called after changing the link power management policy, which may not be 2105 * reflected immediately in the SStatus register (e.g., we may still be seeing 2106 * the PHY in partial, slumber or devsleep Partial power management state. 2107 * So check that: 2108 * - A device is still present, that is, DET is 1h (Device presence detected 2109 * but Phy communication not established) or 3h (Device presence detected and 2110 * Phy communication established) 2111 * - Communication is established, that is, IPM is not 0h, indicating that PHY 2112 * is online or in a low power state. 2113 */ 2114 static bool ata_eh_link_established(struct ata_link *link) 2115 { 2116 u32 sstatus; 2117 u8 det, ipm; 2118 2119 /* 2120 * For old IDE/PATA adapters that do not have a valid scr_read method, 2121 * or if reading the SStatus register fails, assume that the device is 2122 * present. Device probe will determine if that is really the case. 2123 */ 2124 if (sata_scr_read(link, SCR_STATUS, &sstatus)) 2125 return true; 2126 2127 det = sstatus & 0x0f; 2128 ipm = (sstatus >> 8) & 0x0f; 2129 2130 return (det & 0x01) && ipm; 2131 } 2132 2133 /** 2134 * ata_eh_link_set_lpm - configure SATA interface power management 2135 * @link: link to configure 2136 * @policy: the link power management policy 2137 * @r_failed_dev: out parameter for failed device 2138 * 2139 * Enable SATA Interface power management. This will enable 2140 * Device Interface Power Management (DIPM) for min_power and 2141 * medium_power_with_dipm policies, and then call driver specific 2142 * callbacks for enabling Host Initiated Power management. 2143 * 2144 * LOCKING: 2145 * EH context. 2146 * 2147 * RETURNS: 2148 * 0 on success, -errno on failure. 2149 */ 2150 static int ata_eh_link_set_lpm(struct ata_link *link, 2151 enum ata_lpm_policy policy, 2152 struct ata_device **r_failed_dev) 2153 { 2154 struct ata_port *ap = ata_is_host_link(link) ? link->ap : NULL; 2155 struct ata_eh_context *ehc = &link->eh_context; 2156 struct ata_device *dev, *link_dev = NULL, *lpm_dev = NULL; 2157 enum ata_lpm_policy old_policy = link->lpm_policy; 2158 bool host_has_dipm = !(link->ap->flags & ATA_FLAG_NO_DIPM); 2159 unsigned int hints = ATA_LPM_EMPTY | ATA_LPM_HIPM; 2160 unsigned int err_mask; 2161 int rc; 2162 2163 /* if the link or host doesn't do LPM, noop */ 2164 if (!IS_ENABLED(CONFIG_SATA_HOST) || 2165 (link->flags & ATA_LFLAG_NO_LPM) || (ap && !ap->ops->set_lpm)) 2166 return 0; 2167 2168 /* 2169 * This function currently assumes that it will never be supplied policy 2170 * ATA_LPM_UNKNOWN. 2171 */ 2172 if (WARN_ON_ONCE(policy == ATA_LPM_UNKNOWN)) 2173 return 0; 2174 2175 ata_link_dbg(link, "Set LPM policy: %d -> %d\n", old_policy, policy); 2176 2177 /* 2178 * DIPM is enabled only for ATA_LPM_MIN_POWER, 2179 * ATA_LPM_MIN_POWER_WITH_PARTIAL, and ATA_LPM_MED_POWER_WITH_DIPM, as 2180 * some devices misbehave when the host NACKs transition to SLUMBER. 2181 */ 2182 ata_for_each_dev(dev, link, ENABLED) { 2183 bool dev_has_hipm = ata_id_has_hipm(dev->id); 2184 bool dev_has_dipm = ata_id_has_dipm(dev->id); 2185 2186 /* find the first enabled and LPM enabled devices */ 2187 if (!link_dev) 2188 link_dev = dev; 2189 2190 if (!lpm_dev && 2191 (dev_has_hipm || (dev_has_dipm && host_has_dipm))) 2192 lpm_dev = dev; 2193 2194 hints &= ~ATA_LPM_EMPTY; 2195 if (!dev_has_hipm) 2196 hints &= ~ATA_LPM_HIPM; 2197 2198 /* disable DIPM before changing link config */ 2199 if (dev_has_dipm) { 2200 err_mask = ata_dev_set_feature(dev, 2201 SETFEATURES_SATA_DISABLE, SATA_DIPM); 2202 if (err_mask && err_mask != AC_ERR_DEV) { 2203 ata_dev_warn(dev, 2204 "failed to disable DIPM, Emask 0x%x\n", 2205 err_mask); 2206 rc = -EIO; 2207 goto fail; 2208 } 2209 } 2210 } 2211 2212 if (ap) { 2213 rc = ap->ops->set_lpm(link, policy, hints); 2214 if (!rc && ap->slave_link) 2215 rc = ap->ops->set_lpm(ap->slave_link, policy, hints); 2216 } else 2217 rc = sata_pmp_set_lpm(link, policy, hints); 2218 2219 /* 2220 * Attribute link config failure to the first (LPM) enabled 2221 * device on the link. 2222 */ 2223 if (rc) { 2224 if (rc == -EOPNOTSUPP) { 2225 link->flags |= ATA_LFLAG_NO_LPM; 2226 return 0; 2227 } 2228 dev = lpm_dev ? lpm_dev : link_dev; 2229 goto fail; 2230 } 2231 2232 /* 2233 * Low level driver acked the transition. Issue DIPM command 2234 * with the new policy set. 2235 */ 2236 link->lpm_policy = policy; 2237 if (ap && ap->slave_link) 2238 ap->slave_link->lpm_policy = policy; 2239 2240 /* 2241 * Host config updated, enable DIPM if transitioning to 2242 * ATA_LPM_MIN_POWER, ATA_LPM_MIN_POWER_WITH_PARTIAL, or 2243 * ATA_LPM_MED_POWER_WITH_DIPM. 2244 */ 2245 ata_for_each_dev(dev, link, ENABLED) { 2246 bool dev_has_dipm = ata_id_has_dipm(dev->id); 2247 2248 if (policy >= ATA_LPM_MED_POWER_WITH_DIPM && host_has_dipm && 2249 dev_has_dipm) { 2250 err_mask = ata_dev_set_feature(dev, 2251 SETFEATURES_SATA_ENABLE, SATA_DIPM); 2252 if (err_mask && err_mask != AC_ERR_DEV) { 2253 ata_dev_warn(dev, 2254 "failed to enable DIPM, Emask 0x%x\n", 2255 err_mask); 2256 rc = -EIO; 2257 goto fail; 2258 } 2259 } 2260 } 2261 2262 link->last_lpm_change = jiffies; 2263 link->flags |= ATA_LFLAG_CHANGED; 2264 2265 return 0; 2266 2267 fail: 2268 /* restore the old policy */ 2269 link->lpm_policy = old_policy; 2270 if (ap && ap->slave_link) 2271 ap->slave_link->lpm_policy = old_policy; 2272 2273 /* if no device or only one more chance is left, disable LPM */ 2274 if (!dev || ehc->tries[dev->devno] <= 2) { 2275 ata_link_warn(link, "disabling LPM on the link\n"); 2276 link->flags |= ATA_LFLAG_NO_LPM; 2277 } 2278 if (r_failed_dev) 2279 *r_failed_dev = dev; 2280 return rc; 2281 } 2282 2283 /** 2284 * ata_eh_link_autopsy - analyze error and determine recovery action 2285 * @link: host link to perform autopsy on 2286 * 2287 * Analyze why @link failed and determine which recovery actions 2288 * are needed. This function also sets more detailed AC_ERR_* 2289 * values and fills sense data for ATAPI CHECK SENSE. 2290 * 2291 * LOCKING: 2292 * Kernel thread context (may sleep). 2293 */ 2294 static void ata_eh_link_autopsy(struct ata_link *link) 2295 { 2296 struct ata_port *ap = link->ap; 2297 struct ata_eh_context *ehc = &link->eh_context; 2298 struct ata_queued_cmd *qc; 2299 struct ata_device *dev; 2300 unsigned int all_err_mask = 0, eflags = 0; 2301 int tag, nr_failed = 0, nr_quiet = 0; 2302 u32 serror; 2303 int rc; 2304 2305 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY) 2306 return; 2307 2308 /* obtain and analyze SError */ 2309 rc = sata_scr_read(link, SCR_ERROR, &serror); 2310 if (rc == 0) { 2311 ehc->i.serror |= serror; 2312 ata_eh_analyze_serror(link); 2313 } else if (rc != -EOPNOTSUPP) { 2314 /* SError read failed, force reset and probing */ 2315 ehc->i.probe_mask |= ATA_ALL_DEVICES; 2316 ehc->i.action |= ATA_EH_RESET; 2317 ehc->i.err_mask |= AC_ERR_OTHER; 2318 } 2319 2320 /* analyze NCQ failure */ 2321 ata_eh_analyze_ncq_error(link); 2322 2323 /* 2324 * Check if this was a successful command that simply needs sense data. 2325 * Since the sense data is not part of the completion, we need to fetch 2326 * it using an additional command. Since this can't be done from irq 2327 * context, the sense data for successful commands are fetched by EH. 2328 */ 2329 ata_eh_get_success_sense(link); 2330 2331 /* any real error trumps AC_ERR_OTHER */ 2332 if (ehc->i.err_mask & ~AC_ERR_OTHER) 2333 ehc->i.err_mask &= ~AC_ERR_OTHER; 2334 2335 all_err_mask |= ehc->i.err_mask; 2336 2337 ata_qc_for_each_raw(ap, qc, tag) { 2338 if (!(qc->flags & ATA_QCFLAG_EH) || 2339 qc->flags & ATA_QCFLAG_RETRY || 2340 qc->flags & ATA_QCFLAG_EH_SUCCESS_CMD || 2341 ata_dev_phys_link(qc->dev) != link) 2342 continue; 2343 2344 /* inherit upper level err_mask */ 2345 qc->err_mask |= ehc->i.err_mask; 2346 2347 /* analyze TF */ 2348 ehc->i.action |= ata_eh_analyze_tf(qc); 2349 2350 /* DEV errors are probably spurious in case of ATA_BUS error */ 2351 if (qc->err_mask & AC_ERR_ATA_BUS) 2352 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA | 2353 AC_ERR_INVALID); 2354 2355 /* any real error trumps unknown error */ 2356 if (qc->err_mask & ~AC_ERR_OTHER) 2357 qc->err_mask &= ~AC_ERR_OTHER; 2358 2359 /* 2360 * SENSE_VALID trumps dev/unknown error and revalidation. Upper 2361 * layers will determine whether the command is worth retrying 2362 * based on the sense data and device class/type. Otherwise, 2363 * determine directly if the command is worth retrying using its 2364 * error mask and flags. 2365 */ 2366 if (qc->flags & ATA_QCFLAG_SENSE_VALID) 2367 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER); 2368 else if (ata_eh_worth_retry(qc)) 2369 qc->flags |= ATA_QCFLAG_RETRY; 2370 2371 /* accumulate error info */ 2372 ehc->i.dev = qc->dev; 2373 all_err_mask |= qc->err_mask; 2374 if (qc->flags & ATA_QCFLAG_IO) 2375 eflags |= ATA_EFLAG_IS_IO; 2376 trace_ata_eh_link_autopsy_qc(qc); 2377 2378 /* Count quiet errors */ 2379 if (ata_eh_quiet(qc)) 2380 nr_quiet++; 2381 nr_failed++; 2382 } 2383 2384 /* If all failed commands requested silence, then be quiet */ 2385 if (nr_quiet == nr_failed) 2386 ehc->i.flags |= ATA_EHI_QUIET; 2387 2388 /* enforce default EH actions */ 2389 if (ata_port_is_frozen(ap) || 2390 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT)) 2391 ehc->i.action |= ATA_EH_RESET; 2392 else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) || 2393 (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV))) 2394 ehc->i.action |= ATA_EH_REVALIDATE; 2395 2396 /* If we have offending qcs and the associated failed device, 2397 * perform per-dev EH action only on the offending device. 2398 */ 2399 if (ehc->i.dev) { 2400 ehc->i.dev_action[ehc->i.dev->devno] |= 2401 ehc->i.action & ATA_EH_PERDEV_MASK; 2402 ehc->i.action &= ~ATA_EH_PERDEV_MASK; 2403 } 2404 2405 /* propagate timeout to host link */ 2406 if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link)) 2407 ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT; 2408 2409 /* record error and consider speeding down */ 2410 dev = ehc->i.dev; 2411 if (!dev && ((ata_link_max_devices(link) == 1 && 2412 ata_dev_enabled(link->device)))) 2413 dev = link->device; 2414 2415 if (dev) { 2416 if (dev->flags & ATA_DFLAG_DUBIOUS_XFER) 2417 eflags |= ATA_EFLAG_DUBIOUS_XFER; 2418 ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask); 2419 trace_ata_eh_link_autopsy(dev, ehc->i.action, all_err_mask); 2420 } 2421 } 2422 2423 /** 2424 * ata_eh_autopsy - analyze error and determine recovery action 2425 * @ap: host port to perform autopsy on 2426 * 2427 * Analyze all links of @ap and determine why they failed and 2428 * which recovery actions are needed. 2429 * 2430 * LOCKING: 2431 * Kernel thread context (may sleep). 2432 */ 2433 void ata_eh_autopsy(struct ata_port *ap) 2434 { 2435 struct ata_link *link; 2436 2437 ata_for_each_link(link, ap, EDGE) 2438 ata_eh_link_autopsy(link); 2439 2440 /* Handle the frigging slave link. Autopsy is done similarly 2441 * but actions and flags are transferred over to the master 2442 * link and handled from there. 2443 */ 2444 if (ap->slave_link) { 2445 struct ata_eh_context *mehc = &ap->link.eh_context; 2446 struct ata_eh_context *sehc = &ap->slave_link->eh_context; 2447 2448 /* transfer control flags from master to slave */ 2449 sehc->i.flags |= mehc->i.flags & ATA_EHI_TO_SLAVE_MASK; 2450 2451 /* perform autopsy on the slave link */ 2452 ata_eh_link_autopsy(ap->slave_link); 2453 2454 /* transfer actions from slave to master and clear slave */ 2455 ata_eh_about_to_do(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS); 2456 mehc->i.action |= sehc->i.action; 2457 mehc->i.dev_action[1] |= sehc->i.dev_action[1]; 2458 mehc->i.flags |= sehc->i.flags; 2459 ata_eh_done(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS); 2460 } 2461 2462 /* Autopsy of fanout ports can affect host link autopsy. 2463 * Perform host link autopsy last. 2464 */ 2465 if (sata_pmp_attached(ap)) 2466 ata_eh_link_autopsy(&ap->link); 2467 } 2468 2469 /** 2470 * ata_get_cmd_name - get name for ATA command 2471 * @command: ATA command code to get name for 2472 * 2473 * Return a textual name of the given command or "unknown" 2474 * 2475 * LOCKING: 2476 * None 2477 */ 2478 const char *ata_get_cmd_name(u8 command) 2479 { 2480 #ifdef CONFIG_ATA_VERBOSE_ERROR 2481 static const struct 2482 { 2483 u8 command; 2484 const char *text; 2485 } cmd_descr[] = { 2486 { ATA_CMD_DEV_RESET, "DEVICE RESET" }, 2487 { ATA_CMD_CHK_POWER, "CHECK POWER MODE" }, 2488 { ATA_CMD_STANDBY, "STANDBY" }, 2489 { ATA_CMD_IDLE, "IDLE" }, 2490 { ATA_CMD_EDD, "EXECUTE DEVICE DIAGNOSTIC" }, 2491 { ATA_CMD_DOWNLOAD_MICRO, "DOWNLOAD MICROCODE" }, 2492 { ATA_CMD_DOWNLOAD_MICRO_DMA, "DOWNLOAD MICROCODE DMA" }, 2493 { ATA_CMD_NOP, "NOP" }, 2494 { ATA_CMD_FLUSH, "FLUSH CACHE" }, 2495 { ATA_CMD_FLUSH_EXT, "FLUSH CACHE EXT" }, 2496 { ATA_CMD_ID_ATA, "IDENTIFY DEVICE" }, 2497 { ATA_CMD_ID_ATAPI, "IDENTIFY PACKET DEVICE" }, 2498 { ATA_CMD_SERVICE, "SERVICE" }, 2499 { ATA_CMD_READ, "READ DMA" }, 2500 { ATA_CMD_READ_EXT, "READ DMA EXT" }, 2501 { ATA_CMD_READ_QUEUED, "READ DMA QUEUED" }, 2502 { ATA_CMD_READ_STREAM_EXT, "READ STREAM EXT" }, 2503 { ATA_CMD_READ_STREAM_DMA_EXT, "READ STREAM DMA EXT" }, 2504 { ATA_CMD_WRITE, "WRITE DMA" }, 2505 { ATA_CMD_WRITE_EXT, "WRITE DMA EXT" }, 2506 { ATA_CMD_WRITE_QUEUED, "WRITE DMA QUEUED EXT" }, 2507 { ATA_CMD_WRITE_STREAM_EXT, "WRITE STREAM EXT" }, 2508 { ATA_CMD_WRITE_STREAM_DMA_EXT, "WRITE STREAM DMA EXT" }, 2509 { ATA_CMD_WRITE_FUA_EXT, "WRITE DMA FUA EXT" }, 2510 { ATA_CMD_WRITE_QUEUED_FUA_EXT, "WRITE DMA QUEUED FUA EXT" }, 2511 { ATA_CMD_FPDMA_READ, "READ FPDMA QUEUED" }, 2512 { ATA_CMD_FPDMA_WRITE, "WRITE FPDMA QUEUED" }, 2513 { ATA_CMD_NCQ_NON_DATA, "NCQ NON-DATA" }, 2514 { ATA_CMD_FPDMA_SEND, "SEND FPDMA QUEUED" }, 2515 { ATA_CMD_FPDMA_RECV, "RECEIVE FPDMA QUEUED" }, 2516 { ATA_CMD_PIO_READ, "READ SECTOR(S)" }, 2517 { ATA_CMD_PIO_READ_EXT, "READ SECTOR(S) EXT" }, 2518 { ATA_CMD_PIO_WRITE, "WRITE SECTOR(S)" }, 2519 { ATA_CMD_PIO_WRITE_EXT, "WRITE SECTOR(S) EXT" }, 2520 { ATA_CMD_READ_MULTI, "READ MULTIPLE" }, 2521 { ATA_CMD_READ_MULTI_EXT, "READ MULTIPLE EXT" }, 2522 { ATA_CMD_WRITE_MULTI, "WRITE MULTIPLE" }, 2523 { ATA_CMD_WRITE_MULTI_EXT, "WRITE MULTIPLE EXT" }, 2524 { ATA_CMD_WRITE_MULTI_FUA_EXT, "WRITE MULTIPLE FUA EXT" }, 2525 { ATA_CMD_SET_FEATURES, "SET FEATURES" }, 2526 { ATA_CMD_SET_MULTI, "SET MULTIPLE MODE" }, 2527 { ATA_CMD_VERIFY, "READ VERIFY SECTOR(S)" }, 2528 { ATA_CMD_VERIFY_EXT, "READ VERIFY SECTOR(S) EXT" }, 2529 { ATA_CMD_WRITE_UNCORR_EXT, "WRITE UNCORRECTABLE EXT" }, 2530 { ATA_CMD_STANDBYNOW1, "STANDBY IMMEDIATE" }, 2531 { ATA_CMD_IDLEIMMEDIATE, "IDLE IMMEDIATE" }, 2532 { ATA_CMD_SLEEP, "SLEEP" }, 2533 { ATA_CMD_INIT_DEV_PARAMS, "INITIALIZE DEVICE PARAMETERS" }, 2534 { ATA_CMD_READ_NATIVE_MAX, "READ NATIVE MAX ADDRESS" }, 2535 { ATA_CMD_READ_NATIVE_MAX_EXT, "READ NATIVE MAX ADDRESS EXT" }, 2536 { ATA_CMD_SET_MAX, "SET MAX ADDRESS" }, 2537 { ATA_CMD_SET_MAX_EXT, "SET MAX ADDRESS EXT" }, 2538 { ATA_CMD_READ_LOG_EXT, "READ LOG EXT" }, 2539 { ATA_CMD_WRITE_LOG_EXT, "WRITE LOG EXT" }, 2540 { ATA_CMD_READ_LOG_DMA_EXT, "READ LOG DMA EXT" }, 2541 { ATA_CMD_WRITE_LOG_DMA_EXT, "WRITE LOG DMA EXT" }, 2542 { ATA_CMD_TRUSTED_NONDATA, "TRUSTED NON-DATA" }, 2543 { ATA_CMD_TRUSTED_RCV, "TRUSTED RECEIVE" }, 2544 { ATA_CMD_TRUSTED_RCV_DMA, "TRUSTED RECEIVE DMA" }, 2545 { ATA_CMD_TRUSTED_SND, "TRUSTED SEND" }, 2546 { ATA_CMD_TRUSTED_SND_DMA, "TRUSTED SEND DMA" }, 2547 { ATA_CMD_PMP_READ, "READ BUFFER" }, 2548 { ATA_CMD_PMP_READ_DMA, "READ BUFFER DMA" }, 2549 { ATA_CMD_PMP_WRITE, "WRITE BUFFER" }, 2550 { ATA_CMD_PMP_WRITE_DMA, "WRITE BUFFER DMA" }, 2551 { ATA_CMD_CONF_OVERLAY, "DEVICE CONFIGURATION OVERLAY" }, 2552 { ATA_CMD_SEC_SET_PASS, "SECURITY SET PASSWORD" }, 2553 { ATA_CMD_SEC_UNLOCK, "SECURITY UNLOCK" }, 2554 { ATA_CMD_SEC_ERASE_PREP, "SECURITY ERASE PREPARE" }, 2555 { ATA_CMD_SEC_ERASE_UNIT, "SECURITY ERASE UNIT" }, 2556 { ATA_CMD_SEC_FREEZE_LOCK, "SECURITY FREEZE LOCK" }, 2557 { ATA_CMD_SEC_DISABLE_PASS, "SECURITY DISABLE PASSWORD" }, 2558 { ATA_CMD_CONFIG_STREAM, "CONFIGURE STREAM" }, 2559 { ATA_CMD_SMART, "SMART" }, 2560 { ATA_CMD_MEDIA_LOCK, "DOOR LOCK" }, 2561 { ATA_CMD_MEDIA_UNLOCK, "DOOR UNLOCK" }, 2562 { ATA_CMD_DSM, "DATA SET MANAGEMENT" }, 2563 { ATA_CMD_CHK_MED_CRD_TYP, "CHECK MEDIA CARD TYPE" }, 2564 { ATA_CMD_CFA_REQ_EXT_ERR, "CFA REQUEST EXTENDED ERROR" }, 2565 { ATA_CMD_CFA_WRITE_NE, "CFA WRITE SECTORS WITHOUT ERASE" }, 2566 { ATA_CMD_CFA_TRANS_SECT, "CFA TRANSLATE SECTOR" }, 2567 { ATA_CMD_CFA_ERASE, "CFA ERASE SECTORS" }, 2568 { ATA_CMD_CFA_WRITE_MULT_NE, "CFA WRITE MULTIPLE WITHOUT ERASE" }, 2569 { ATA_CMD_REQ_SENSE_DATA, "REQUEST SENSE DATA EXT" }, 2570 { ATA_CMD_SANITIZE_DEVICE, "SANITIZE DEVICE" }, 2571 { ATA_CMD_ZAC_MGMT_IN, "ZAC MANAGEMENT IN" }, 2572 { ATA_CMD_ZAC_MGMT_OUT, "ZAC MANAGEMENT OUT" }, 2573 { ATA_CMD_READ_LONG, "READ LONG (with retries)" }, 2574 { ATA_CMD_READ_LONG_ONCE, "READ LONG (without retries)" }, 2575 { ATA_CMD_WRITE_LONG, "WRITE LONG (with retries)" }, 2576 { ATA_CMD_WRITE_LONG_ONCE, "WRITE LONG (without retries)" }, 2577 { ATA_CMD_RESTORE, "RECALIBRATE" }, 2578 { 0, NULL } /* terminate list */ 2579 }; 2580 2581 unsigned int i; 2582 for (i = 0; cmd_descr[i].text; i++) 2583 if (cmd_descr[i].command == command) 2584 return cmd_descr[i].text; 2585 #endif 2586 2587 return "unknown"; 2588 } 2589 EXPORT_SYMBOL_GPL(ata_get_cmd_name); 2590 2591 /** 2592 * ata_eh_link_report - report error handling to user 2593 * @link: ATA link EH is going on 2594 * 2595 * Report EH to user. 2596 * 2597 * LOCKING: 2598 * None. 2599 */ 2600 static void ata_eh_link_report(struct ata_link *link) 2601 { 2602 struct ata_port *ap = link->ap; 2603 struct ata_eh_context *ehc = &link->eh_context; 2604 struct ata_queued_cmd *qc; 2605 const char *frozen, *desc; 2606 char tries_buf[16] = ""; 2607 int tag, nr_failed = 0; 2608 2609 if (ehc->i.flags & ATA_EHI_QUIET) 2610 return; 2611 2612 desc = NULL; 2613 if (ehc->i.desc[0] != '\0') 2614 desc = ehc->i.desc; 2615 2616 ata_qc_for_each_raw(ap, qc, tag) { 2617 if (!(qc->flags & ATA_QCFLAG_EH) || 2618 ata_dev_phys_link(qc->dev) != link || 2619 ((qc->flags & ATA_QCFLAG_QUIET) && 2620 qc->err_mask == AC_ERR_DEV)) 2621 continue; 2622 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask) 2623 continue; 2624 2625 nr_failed++; 2626 } 2627 2628 if (!nr_failed && !ehc->i.err_mask) 2629 return; 2630 2631 frozen = ""; 2632 if (ata_port_is_frozen(ap)) 2633 frozen = " frozen"; 2634 2635 if (ap->eh_tries < ATA_EH_MAX_TRIES) 2636 snprintf(tries_buf, sizeof(tries_buf), " t%d", 2637 ap->eh_tries); 2638 2639 if (ehc->i.dev) { 2640 ata_dev_err(ehc->i.dev, "exception Emask 0x%x " 2641 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n", 2642 ehc->i.err_mask, link->sactive, ehc->i.serror, 2643 ehc->i.action, frozen, tries_buf); 2644 if (desc) 2645 ata_dev_err(ehc->i.dev, "%s\n", desc); 2646 } else { 2647 ata_link_err(link, "exception Emask 0x%x " 2648 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n", 2649 ehc->i.err_mask, link->sactive, ehc->i.serror, 2650 ehc->i.action, frozen, tries_buf); 2651 if (desc) 2652 ata_link_err(link, "%s\n", desc); 2653 } 2654 2655 #ifdef CONFIG_ATA_VERBOSE_ERROR 2656 if (ehc->i.serror) 2657 ata_link_err(link, 2658 "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n", 2659 ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "", 2660 ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "", 2661 ehc->i.serror & SERR_DATA ? "UnrecovData " : "", 2662 ehc->i.serror & SERR_PERSISTENT ? "Persist " : "", 2663 ehc->i.serror & SERR_PROTOCOL ? "Proto " : "", 2664 ehc->i.serror & SERR_INTERNAL ? "HostInt " : "", 2665 ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "", 2666 ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "", 2667 ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "", 2668 ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "", 2669 ehc->i.serror & SERR_DISPARITY ? "Dispar " : "", 2670 ehc->i.serror & SERR_CRC ? "BadCRC " : "", 2671 ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "", 2672 ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "", 2673 ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "", 2674 ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "", 2675 ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : ""); 2676 #endif 2677 2678 ata_qc_for_each_raw(ap, qc, tag) { 2679 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf; 2680 char data_buf[20] = ""; 2681 char cdb_buf[70] = ""; 2682 2683 if (!(qc->flags & ATA_QCFLAG_EH) || 2684 ata_dev_phys_link(qc->dev) != link || !qc->err_mask) 2685 continue; 2686 2687 if (qc->dma_dir != DMA_NONE) { 2688 static const char *dma_str[] = { 2689 [DMA_BIDIRECTIONAL] = "bidi", 2690 [DMA_TO_DEVICE] = "out", 2691 [DMA_FROM_DEVICE] = "in", 2692 }; 2693 const char *prot_str = NULL; 2694 2695 switch (qc->tf.protocol) { 2696 case ATA_PROT_UNKNOWN: 2697 prot_str = "unknown"; 2698 break; 2699 case ATA_PROT_NODATA: 2700 prot_str = "nodata"; 2701 break; 2702 case ATA_PROT_PIO: 2703 prot_str = "pio"; 2704 break; 2705 case ATA_PROT_DMA: 2706 prot_str = "dma"; 2707 break; 2708 case ATA_PROT_NCQ: 2709 prot_str = "ncq dma"; 2710 break; 2711 case ATA_PROT_NCQ_NODATA: 2712 prot_str = "ncq nodata"; 2713 break; 2714 case ATAPI_PROT_NODATA: 2715 prot_str = "nodata"; 2716 break; 2717 case ATAPI_PROT_PIO: 2718 prot_str = "pio"; 2719 break; 2720 case ATAPI_PROT_DMA: 2721 prot_str = "dma"; 2722 break; 2723 } 2724 snprintf(data_buf, sizeof(data_buf), " %s %u %s", 2725 prot_str, qc->nbytes, dma_str[qc->dma_dir]); 2726 } 2727 2728 if (ata_is_atapi(qc->tf.protocol)) { 2729 const u8 *cdb = qc->cdb; 2730 size_t cdb_len = qc->dev->cdb_len; 2731 2732 if (qc->scsicmd) { 2733 cdb = qc->scsicmd->cmnd; 2734 cdb_len = qc->scsicmd->cmd_len; 2735 } 2736 __scsi_format_command(cdb_buf, sizeof(cdb_buf), 2737 cdb, cdb_len); 2738 } else 2739 ata_dev_err(qc->dev, "failed command: %s\n", 2740 ata_get_cmd_name(cmd->command)); 2741 2742 ata_dev_err(qc->dev, 2743 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x " 2744 "tag %d%s\n %s" 2745 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x " 2746 "Emask 0x%x (%s)%s\n", 2747 cmd->command, cmd->feature, cmd->nsect, 2748 cmd->lbal, cmd->lbam, cmd->lbah, 2749 cmd->hob_feature, cmd->hob_nsect, 2750 cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah, 2751 cmd->device, qc->tag, data_buf, cdb_buf, 2752 res->status, res->error, res->nsect, 2753 res->lbal, res->lbam, res->lbah, 2754 res->hob_feature, res->hob_nsect, 2755 res->hob_lbal, res->hob_lbam, res->hob_lbah, 2756 res->device, qc->err_mask, ata_err_string(qc->err_mask), 2757 qc->err_mask & AC_ERR_NCQ ? " <F>" : ""); 2758 2759 #ifdef CONFIG_ATA_VERBOSE_ERROR 2760 if (res->status & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ | 2761 ATA_SENSE | ATA_ERR)) { 2762 if (res->status & ATA_BUSY) 2763 ata_dev_err(qc->dev, "status: { Busy }\n"); 2764 else 2765 ata_dev_err(qc->dev, "status: { %s%s%s%s%s}\n", 2766 res->status & ATA_DRDY ? "DRDY " : "", 2767 res->status & ATA_DF ? "DF " : "", 2768 res->status & ATA_DRQ ? "DRQ " : "", 2769 res->status & ATA_SENSE ? "SENSE " : "", 2770 res->status & ATA_ERR ? "ERR " : ""); 2771 } 2772 2773 if (cmd->command != ATA_CMD_PACKET && 2774 (res->error & (ATA_ICRC | ATA_UNC | ATA_AMNF | ATA_IDNF | 2775 ATA_ABORTED))) 2776 ata_dev_err(qc->dev, "error: { %s%s%s%s%s}\n", 2777 res->error & ATA_ICRC ? "ICRC " : "", 2778 res->error & ATA_UNC ? "UNC " : "", 2779 res->error & ATA_AMNF ? "AMNF " : "", 2780 res->error & ATA_IDNF ? "IDNF " : "", 2781 res->error & ATA_ABORTED ? "ABRT " : ""); 2782 #endif 2783 } 2784 } 2785 2786 /** 2787 * ata_eh_report - report error handling to user 2788 * @ap: ATA port to report EH about 2789 * 2790 * Report EH to user. 2791 * 2792 * LOCKING: 2793 * None. 2794 */ 2795 void ata_eh_report(struct ata_port *ap) 2796 { 2797 struct ata_link *link; 2798 2799 ata_for_each_link(link, ap, HOST_FIRST) 2800 ata_eh_link_report(link); 2801 } 2802 2803 static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset, 2804 unsigned int *classes, unsigned long deadline, 2805 bool clear_classes) 2806 { 2807 struct ata_device *dev; 2808 2809 if (clear_classes) 2810 ata_for_each_dev(dev, link, ALL) 2811 classes[dev->devno] = ATA_DEV_UNKNOWN; 2812 2813 return reset(link, classes, deadline); 2814 } 2815 2816 static bool ata_eh_followup_srst_needed(struct ata_link *link, int rc) 2817 { 2818 if ((link->flags & ATA_LFLAG_NO_SRST) || ata_link_offline(link)) 2819 return false; 2820 if (rc == -EAGAIN) 2821 return true; 2822 if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) 2823 return true; 2824 return false; 2825 } 2826 2827 int ata_eh_reset(struct ata_port *ap, struct ata_link *link, int classify, 2828 struct ata_reset_operations *reset_ops) 2829 __must_hold(&ap->host->eh_mutex) 2830 { 2831 struct ata_link *slave = ap->slave_link; 2832 struct ata_eh_context *ehc = &link->eh_context; 2833 struct ata_eh_context *sehc = slave ? &slave->eh_context : NULL; 2834 ata_reset_fn_t hardreset = reset_ops->hardreset; 2835 ata_reset_fn_t softreset = reset_ops->softreset; 2836 ata_prereset_fn_t prereset = reset_ops->prereset; 2837 ata_postreset_fn_t postreset = reset_ops->postreset; 2838 unsigned int *classes = ehc->classes; 2839 unsigned int lflags = link->flags; 2840 int verbose = !(ehc->i.flags & ATA_EHI_QUIET); 2841 int max_tries = 0, try = 0; 2842 struct ata_link *failed_link; 2843 struct ata_device *dev; 2844 unsigned long deadline, now; 2845 ata_reset_fn_t reset; 2846 unsigned long flags; 2847 u32 sstatus; 2848 int nr_unknown, rc; 2849 2850 /* 2851 * Prepare to reset 2852 */ 2853 while (ata_eh_reset_timeouts[max_tries] != UINT_MAX) 2854 max_tries++; 2855 if (link->flags & ATA_LFLAG_RST_ONCE) 2856 max_tries = 1; 2857 if (link->flags & ATA_LFLAG_NO_HRST) 2858 hardreset = NULL; 2859 if (link->flags & ATA_LFLAG_NO_SRST) 2860 softreset = NULL; 2861 2862 /* make sure each reset attempt is at least COOL_DOWN apart */ 2863 if (ehc->i.flags & ATA_EHI_DID_RESET) { 2864 now = jiffies; 2865 WARN_ON(time_after(ehc->last_reset, now)); 2866 deadline = ata_deadline(ehc->last_reset, 2867 ATA_EH_RESET_COOL_DOWN); 2868 if (time_before(now, deadline)) 2869 schedule_timeout_uninterruptible(deadline - now); 2870 } 2871 2872 spin_lock_irqsave(ap->lock, flags); 2873 ap->pflags |= ATA_PFLAG_RESETTING; 2874 spin_unlock_irqrestore(ap->lock, flags); 2875 2876 ata_eh_about_to_do(link, NULL, ATA_EH_RESET); 2877 2878 ata_for_each_dev(dev, link, ALL) { 2879 /* If we issue an SRST then an ATA drive (not ATAPI) 2880 * may change configuration and be in PIO0 timing. If 2881 * we do a hard reset (or are coming from power on) 2882 * this is true for ATA or ATAPI. Until we've set a 2883 * suitable controller mode we should not touch the 2884 * bus as we may be talking too fast. 2885 */ 2886 dev->pio_mode = XFER_PIO_0; 2887 dev->dma_mode = 0xff; 2888 2889 /* If the controller has a pio mode setup function 2890 * then use it to set the chipset to rights. Don't 2891 * touch the DMA setup as that will be dealt with when 2892 * configuring devices. 2893 */ 2894 if (ap->ops->set_piomode) 2895 ap->ops->set_piomode(ap, dev); 2896 } 2897 2898 /* prefer hardreset */ 2899 reset = NULL; 2900 ehc->i.action &= ~ATA_EH_RESET; 2901 if (hardreset) { 2902 reset = hardreset; 2903 ehc->i.action |= ATA_EH_HARDRESET; 2904 } else if (softreset) { 2905 reset = softreset; 2906 ehc->i.action |= ATA_EH_SOFTRESET; 2907 } 2908 2909 if (prereset) { 2910 unsigned long deadline = ata_deadline(jiffies, 2911 ATA_EH_PRERESET_TIMEOUT); 2912 2913 if (slave) { 2914 sehc->i.action &= ~ATA_EH_RESET; 2915 sehc->i.action |= ehc->i.action; 2916 } 2917 2918 rc = prereset(link, deadline); 2919 2920 /* If present, do prereset on slave link too. Reset 2921 * is skipped iff both master and slave links report 2922 * -ENOENT or clear ATA_EH_RESET. 2923 */ 2924 if (slave && (rc == 0 || rc == -ENOENT)) { 2925 int tmp; 2926 2927 tmp = prereset(slave, deadline); 2928 if (tmp != -ENOENT) 2929 rc = tmp; 2930 2931 ehc->i.action |= sehc->i.action; 2932 } 2933 2934 if (rc) { 2935 if (rc == -ENOENT) { 2936 ata_link_dbg(link, "port disabled--ignoring\n"); 2937 ehc->i.action &= ~ATA_EH_RESET; 2938 2939 ata_for_each_dev(dev, link, ALL) 2940 classes[dev->devno] = ATA_DEV_NONE; 2941 2942 rc = 0; 2943 } else 2944 ata_link_err(link, 2945 "prereset failed (errno=%d)\n", 2946 rc); 2947 goto out; 2948 } 2949 2950 /* prereset() might have cleared ATA_EH_RESET. If so, 2951 * bang classes, thaw and return. 2952 */ 2953 if (reset && !(ehc->i.action & ATA_EH_RESET)) { 2954 ata_for_each_dev(dev, link, ALL) 2955 classes[dev->devno] = ATA_DEV_NONE; 2956 if (ata_port_is_frozen(ap) && ata_is_host_link(link)) 2957 ata_eh_thaw_port(ap); 2958 rc = 0; 2959 goto out; 2960 } 2961 } 2962 2963 retry: 2964 /* 2965 * Perform reset 2966 */ 2967 if (ata_is_host_link(link)) 2968 ata_eh_freeze_port(ap); 2969 2970 deadline = ata_deadline(jiffies, ata_eh_reset_timeouts[try++]); 2971 2972 if (reset) { 2973 if (verbose) 2974 ata_link_info(link, "%s resetting link\n", 2975 reset == softreset ? "soft" : "hard"); 2976 2977 /* mark that this EH session started with reset */ 2978 ehc->last_reset = jiffies; 2979 if (reset == hardreset) { 2980 ehc->i.flags |= ATA_EHI_DID_HARDRESET; 2981 trace_ata_link_hardreset_begin(link, classes, deadline); 2982 } else { 2983 ehc->i.flags |= ATA_EHI_DID_SOFTRESET; 2984 trace_ata_link_softreset_begin(link, classes, deadline); 2985 } 2986 2987 rc = ata_do_reset(link, reset, classes, deadline, true); 2988 if (reset == hardreset) 2989 trace_ata_link_hardreset_end(link, classes, rc); 2990 else 2991 trace_ata_link_softreset_end(link, classes, rc); 2992 if (rc && rc != -EAGAIN) { 2993 failed_link = link; 2994 goto fail; 2995 } 2996 2997 /* hardreset slave link if existent */ 2998 if (slave && reset == hardreset) { 2999 int tmp; 3000 3001 if (verbose) 3002 ata_link_info(slave, "hard resetting link\n"); 3003 3004 ata_eh_about_to_do(slave, NULL, ATA_EH_RESET); 3005 trace_ata_slave_hardreset_begin(slave, classes, 3006 deadline); 3007 tmp = ata_do_reset(slave, reset, classes, deadline, 3008 false); 3009 trace_ata_slave_hardreset_end(slave, classes, tmp); 3010 switch (tmp) { 3011 case -EAGAIN: 3012 rc = -EAGAIN; 3013 break; 3014 case 0: 3015 break; 3016 default: 3017 failed_link = slave; 3018 rc = tmp; 3019 goto fail; 3020 } 3021 } 3022 3023 /* perform follow-up SRST if necessary */ 3024 if (reset == hardreset && 3025 ata_eh_followup_srst_needed(link, rc)) { 3026 reset = softreset; 3027 3028 if (!reset) { 3029 ata_link_err(link, 3030 "follow-up softreset required but no softreset available\n"); 3031 failed_link = link; 3032 rc = -EINVAL; 3033 goto fail; 3034 } 3035 3036 ata_eh_about_to_do(link, NULL, ATA_EH_RESET); 3037 trace_ata_link_softreset_begin(link, classes, deadline); 3038 rc = ata_do_reset(link, reset, classes, deadline, true); 3039 trace_ata_link_softreset_end(link, classes, rc); 3040 if (rc) { 3041 failed_link = link; 3042 goto fail; 3043 } 3044 } 3045 } else { 3046 if (verbose) 3047 ata_link_info(link, 3048 "no reset method available, skipping reset\n"); 3049 if (!(lflags & ATA_LFLAG_ASSUME_CLASS)) 3050 lflags |= ATA_LFLAG_ASSUME_ATA; 3051 } 3052 3053 /* 3054 * Post-reset processing 3055 */ 3056 ata_for_each_dev(dev, link, ALL) { 3057 /* After the reset, the device state is PIO 0 and the 3058 * controller state is undefined. Reset also wakes up 3059 * drives from sleeping mode. 3060 */ 3061 dev->pio_mode = XFER_PIO_0; 3062 dev->flags &= ~ATA_DFLAG_SLEEPING; 3063 3064 if (ata_phys_link_offline(ata_dev_phys_link(dev))) 3065 continue; 3066 3067 /* apply class override */ 3068 if (lflags & ATA_LFLAG_ASSUME_ATA) 3069 classes[dev->devno] = ATA_DEV_ATA; 3070 else if (lflags & ATA_LFLAG_ASSUME_SEMB) 3071 classes[dev->devno] = ATA_DEV_SEMB_UNSUP; 3072 } 3073 3074 /* record current link speed */ 3075 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0) 3076 link->sata_spd = (sstatus >> 4) & 0xf; 3077 if (slave && sata_scr_read(slave, SCR_STATUS, &sstatus) == 0) 3078 slave->sata_spd = (sstatus >> 4) & 0xf; 3079 3080 /* thaw the port */ 3081 if (ata_is_host_link(link)) 3082 ata_eh_thaw_port(ap); 3083 3084 /* postreset() should clear hardware SError. Although SError 3085 * is cleared during link resume, clearing SError here is 3086 * necessary as some PHYs raise hotplug events after SRST. 3087 * This introduces race condition where hotplug occurs between 3088 * reset and here. This race is mediated by cross checking 3089 * link onlineness and classification result later. 3090 */ 3091 if (postreset) { 3092 postreset(link, classes); 3093 trace_ata_link_postreset(link, classes, rc); 3094 if (slave) { 3095 postreset(slave, classes); 3096 trace_ata_slave_postreset(slave, classes, rc); 3097 } 3098 } 3099 3100 /* clear cached SError */ 3101 spin_lock_irqsave(link->ap->lock, flags); 3102 link->eh_info.serror = 0; 3103 if (slave) 3104 slave->eh_info.serror = 0; 3105 spin_unlock_irqrestore(link->ap->lock, flags); 3106 3107 /* 3108 * Make sure onlineness and classification result correspond. 3109 * Hotplug could have happened during reset and some 3110 * controllers fail to wait while a drive is spinning up after 3111 * being hotplugged causing misdetection. By cross checking 3112 * link on/offlineness and classification result, those 3113 * conditions can be reliably detected and retried. 3114 */ 3115 nr_unknown = 0; 3116 ata_for_each_dev(dev, link, ALL) { 3117 if (ata_phys_link_online(ata_dev_phys_link(dev))) { 3118 if (classes[dev->devno] == ATA_DEV_UNKNOWN) { 3119 ata_dev_dbg(dev, "link online but device misclassified\n"); 3120 classes[dev->devno] = ATA_DEV_NONE; 3121 nr_unknown++; 3122 } 3123 } else if (ata_phys_link_offline(ata_dev_phys_link(dev))) { 3124 if (ata_class_enabled(classes[dev->devno])) 3125 ata_dev_dbg(dev, 3126 "link offline, clearing class %d to NONE\n", 3127 classes[dev->devno]); 3128 classes[dev->devno] = ATA_DEV_NONE; 3129 } else if (classes[dev->devno] == ATA_DEV_UNKNOWN) { 3130 ata_dev_dbg(dev, 3131 "link status unknown, clearing UNKNOWN to NONE\n"); 3132 classes[dev->devno] = ATA_DEV_NONE; 3133 } 3134 } 3135 3136 if (classify && nr_unknown) { 3137 if (try < max_tries) { 3138 ata_link_warn(link, 3139 "link online but %d devices misclassified, retrying\n", 3140 nr_unknown); 3141 failed_link = link; 3142 rc = -EAGAIN; 3143 goto fail; 3144 } 3145 ata_link_warn(link, 3146 "link online but %d devices misclassified, " 3147 "device detection might fail\n", nr_unknown); 3148 } 3149 3150 /* reset successful, schedule revalidation */ 3151 ata_eh_done(link, NULL, ATA_EH_RESET); 3152 if (slave) 3153 ata_eh_done(slave, NULL, ATA_EH_RESET); 3154 ehc->last_reset = jiffies; /* update to completion time */ 3155 ehc->i.action |= ATA_EH_REVALIDATE; 3156 link->lpm_policy = ATA_LPM_UNKNOWN; /* reset LPM state */ 3157 3158 rc = 0; 3159 out: 3160 /* clear hotplug flag */ 3161 ehc->i.flags &= ~ATA_EHI_HOTPLUGGED; 3162 if (slave) 3163 sehc->i.flags &= ~ATA_EHI_HOTPLUGGED; 3164 3165 spin_lock_irqsave(ap->lock, flags); 3166 ap->pflags &= ~ATA_PFLAG_RESETTING; 3167 spin_unlock_irqrestore(ap->lock, flags); 3168 3169 return rc; 3170 3171 fail: 3172 /* if SCR isn't accessible on a fan-out port, PMP needs to be reset */ 3173 if (!ata_is_host_link(link) && 3174 sata_scr_read(link, SCR_STATUS, &sstatus)) 3175 rc = -ERESTART; 3176 3177 if (try >= max_tries || rc == -ENODEV) { 3178 /* 3179 * Thaw host port even if reset failed, so that the port 3180 * can be retried on the next phy event. This risks 3181 * repeated EH runs but seems to be a better tradeoff than 3182 * shutting down a port after a botched hotplug attempt. 3183 */ 3184 if (ata_is_host_link(link)) 3185 ata_eh_thaw_port(ap); 3186 ata_link_warn(link, "%s failed\n", 3187 reset == hardreset ? "hardreset" : "softreset"); 3188 goto out; 3189 } 3190 3191 now = jiffies; 3192 if (time_before(now, deadline)) { 3193 unsigned long delta = deadline - now; 3194 3195 ata_link_warn(failed_link, 3196 "reset failed (errno=%d), retrying in %u secs\n", 3197 rc, DIV_ROUND_UP(jiffies_to_msecs(delta), 1000)); 3198 3199 ata_eh_release(ap); 3200 while (delta) 3201 delta = schedule_timeout_uninterruptible(delta); 3202 ata_eh_acquire(ap); 3203 } 3204 3205 /* 3206 * While disks spinup behind PMP, some controllers fail sending SRST. 3207 * They need to be reset - as well as the PMP - before retrying. 3208 */ 3209 if (rc == -ERESTART) { 3210 if (ata_is_host_link(link)) 3211 ata_eh_thaw_port(ap); 3212 goto out; 3213 } 3214 3215 if (try == max_tries - 1) { 3216 sata_down_spd_limit(link, 0); 3217 if (slave) 3218 sata_down_spd_limit(slave, 0); 3219 } else if (rc == -EPIPE) 3220 sata_down_spd_limit(failed_link, 0); 3221 3222 if (hardreset) 3223 reset = hardreset; 3224 goto retry; 3225 } 3226 3227 static inline void ata_eh_pull_park_action(struct ata_port *ap) 3228 { 3229 struct ata_link *link; 3230 struct ata_device *dev; 3231 unsigned long flags; 3232 3233 /* 3234 * This function can be thought of as an extended version of 3235 * ata_eh_about_to_do() specially crafted to accommodate the 3236 * requirements of ATA_EH_PARK handling. Since the EH thread 3237 * does not leave the do {} while () loop in ata_eh_recover as 3238 * long as the timeout for a park request to *one* device on 3239 * the port has not expired, and since we still want to pick 3240 * up park requests to other devices on the same port or 3241 * timeout updates for the same device, we have to pull 3242 * ATA_EH_PARK actions from eh_info into eh_context.i 3243 * ourselves at the beginning of each pass over the loop. 3244 * 3245 * Additionally, all write accesses to &ap->park_req_pending 3246 * through reinit_completion() (see below) or complete_all() 3247 * (see ata_scsi_park_store()) are protected by the host lock. 3248 * As a result we have that park_req_pending.done is zero on 3249 * exit from this function, i.e. when ATA_EH_PARK actions for 3250 * *all* devices on port ap have been pulled into the 3251 * respective eh_context structs. If, and only if, 3252 * park_req_pending.done is non-zero by the time we reach 3253 * wait_for_completion_timeout(), another ATA_EH_PARK action 3254 * has been scheduled for at least one of the devices on port 3255 * ap and we have to cycle over the do {} while () loop in 3256 * ata_eh_recover() again. 3257 */ 3258 3259 spin_lock_irqsave(ap->lock, flags); 3260 reinit_completion(&ap->park_req_pending); 3261 ata_for_each_link(link, ap, EDGE) { 3262 ata_for_each_dev(dev, link, ALL) { 3263 struct ata_eh_info *ehi = &link->eh_info; 3264 3265 link->eh_context.i.dev_action[dev->devno] |= 3266 ehi->dev_action[dev->devno] & ATA_EH_PARK; 3267 ata_eh_clear_action(link, dev, ehi, ATA_EH_PARK); 3268 } 3269 } 3270 spin_unlock_irqrestore(ap->lock, flags); 3271 } 3272 3273 static void ata_eh_park_issue_cmd(struct ata_device *dev, int park) 3274 { 3275 struct ata_eh_context *ehc = &dev->link->eh_context; 3276 struct ata_taskfile tf; 3277 unsigned int err_mask; 3278 3279 ata_tf_init(dev, &tf); 3280 if (park) { 3281 ehc->unloaded_mask |= 1 << dev->devno; 3282 tf.command = ATA_CMD_IDLEIMMEDIATE; 3283 tf.feature = 0x44; 3284 tf.lbal = 0x4c; 3285 tf.lbam = 0x4e; 3286 tf.lbah = 0x55; 3287 } else { 3288 ehc->unloaded_mask &= ~(1 << dev->devno); 3289 tf.command = ATA_CMD_CHK_POWER; 3290 } 3291 3292 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR; 3293 tf.protocol = ATA_PROT_NODATA; 3294 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0); 3295 if (park && (err_mask || tf.lbal != 0xc4)) { 3296 ata_dev_err(dev, "head unload failed!\n"); 3297 ehc->unloaded_mask &= ~(1 << dev->devno); 3298 } 3299 } 3300 3301 static int ata_eh_revalidate_and_attach(struct ata_link *link, 3302 struct ata_device **r_failed_dev) 3303 { 3304 struct ata_port *ap = link->ap; 3305 struct ata_eh_context *ehc = &link->eh_context; 3306 struct ata_device *dev; 3307 unsigned int new_mask = 0; 3308 unsigned long flags; 3309 int rc = 0; 3310 3311 /* For PATA drive side cable detection to work, IDENTIFY must 3312 * be done backwards such that PDIAG- is released by the slave 3313 * device before the master device is identified. 3314 */ 3315 ata_for_each_dev(dev, link, ALL_REVERSE) { 3316 unsigned int action = ata_eh_dev_action(dev); 3317 unsigned int readid_flags = 0; 3318 3319 if (ehc->i.flags & ATA_EHI_DID_RESET) 3320 readid_flags |= ATA_READID_POSTRESET; 3321 3322 if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) { 3323 WARN_ON(dev->class == ATA_DEV_PMP); 3324 3325 /* 3326 * The link may be in a deep sleep, wake it up. 3327 * 3328 * If the link is in deep sleep, ata_phys_link_offline() 3329 * will return true, causing the revalidation to fail, 3330 * which leads to a (potentially) needless hard reset. 3331 * 3332 * ata_eh_recover() will later restore the link policy 3333 * to ap->target_lpm_policy after revalidation is done. 3334 */ 3335 if (link->lpm_policy > ATA_LPM_MAX_POWER) { 3336 rc = ata_eh_link_set_lpm(link, ATA_LPM_MAX_POWER, 3337 r_failed_dev); 3338 if (rc) 3339 goto err; 3340 } 3341 3342 if (!ata_eh_link_established(ata_dev_phys_link(dev))) { 3343 rc = -EIO; 3344 goto err; 3345 } 3346 3347 ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE); 3348 rc = ata_dev_revalidate(dev, ehc->classes[dev->devno], 3349 readid_flags); 3350 if (rc) 3351 goto err; 3352 3353 ata_eh_done(link, dev, ATA_EH_REVALIDATE); 3354 3355 /* Configuration may have changed, reconfigure 3356 * transfer mode. 3357 */ 3358 ehc->i.flags |= ATA_EHI_SETMODE; 3359 3360 /* schedule the scsi_rescan_device() here */ 3361 schedule_delayed_work(&ap->scsi_rescan_task, 0); 3362 } else if (dev->class == ATA_DEV_UNKNOWN && 3363 ehc->tries[dev->devno] && 3364 ata_class_enabled(ehc->classes[dev->devno])) { 3365 /* Temporarily set dev->class, it will be 3366 * permanently set once all configurations are 3367 * complete. This is necessary because new 3368 * device configuration is done in two 3369 * separate loops. 3370 */ 3371 dev->class = ehc->classes[dev->devno]; 3372 3373 if (dev->class == ATA_DEV_PMP) 3374 rc = sata_pmp_attach(dev); 3375 else 3376 rc = ata_dev_read_id(dev, &dev->class, 3377 readid_flags, dev->id); 3378 3379 /* read_id might have changed class, store and reset */ 3380 ehc->classes[dev->devno] = dev->class; 3381 dev->class = ATA_DEV_UNKNOWN; 3382 3383 switch (rc) { 3384 case 0: 3385 /* clear error info accumulated during probe */ 3386 ata_ering_clear(&dev->ering); 3387 new_mask |= 1 << dev->devno; 3388 break; 3389 case -ENOENT: 3390 /* IDENTIFY was issued to non-existent 3391 * device. No need to reset. Just 3392 * thaw and ignore the device. 3393 */ 3394 ata_eh_thaw_port(ap); 3395 break; 3396 default: 3397 goto err; 3398 } 3399 } 3400 } 3401 3402 /* PDIAG- should have been released, ask cable type if post-reset */ 3403 if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) { 3404 if (ap->ops->cable_detect) 3405 ap->cbl = ap->ops->cable_detect(ap); 3406 ata_force_cbl(ap); 3407 } 3408 3409 /* Configure new devices forward such that user doesn't see 3410 * device detection messages backwards. 3411 */ 3412 ata_for_each_dev(dev, link, ALL) { 3413 if (!(new_mask & (1 << dev->devno))) 3414 continue; 3415 3416 dev->class = ehc->classes[dev->devno]; 3417 3418 if (dev->class == ATA_DEV_PMP) 3419 continue; 3420 3421 ehc->i.flags |= ATA_EHI_PRINTINFO; 3422 rc = ata_dev_configure(dev); 3423 ehc->i.flags &= ~ATA_EHI_PRINTINFO; 3424 if (rc) { 3425 dev->class = ATA_DEV_UNKNOWN; 3426 goto err; 3427 } 3428 3429 spin_lock_irqsave(ap->lock, flags); 3430 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG; 3431 spin_unlock_irqrestore(ap->lock, flags); 3432 3433 /* new device discovered, configure xfermode */ 3434 ehc->i.flags |= ATA_EHI_SETMODE; 3435 } 3436 3437 return 0; 3438 3439 err: 3440 dev->flags &= ~ATA_DFLAG_RESUMING; 3441 *r_failed_dev = dev; 3442 return rc; 3443 } 3444 3445 /** 3446 * ata_eh_set_mode - Program timings and issue SET FEATURES - XFER 3447 * @link: link on which timings will be programmed 3448 * @r_failed_dev: out parameter for failed device 3449 * 3450 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If 3451 * ata_eh_set_mode() fails, pointer to the failing device is 3452 * returned in @r_failed_dev. 3453 * 3454 * LOCKING: 3455 * PCI/etc. bus probe sem. 3456 * 3457 * RETURNS: 3458 * 0 on success, negative errno otherwise 3459 */ 3460 static int ata_eh_set_mode(struct ata_link *link, 3461 struct ata_device **r_failed_dev) 3462 { 3463 struct ata_port *ap = link->ap; 3464 struct ata_device *dev; 3465 int rc; 3466 3467 /* if data transfer is verified, clear DUBIOUS_XFER on ering top */ 3468 ata_for_each_dev(dev, link, ENABLED) { 3469 if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) { 3470 struct ata_ering_entry *ent; 3471 3472 ent = ata_ering_top(&dev->ering); 3473 if (ent) 3474 ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER; 3475 } 3476 } 3477 3478 /* has private set_mode? */ 3479 if (ap->ops->set_mode) 3480 rc = ap->ops->set_mode(link, r_failed_dev); 3481 else 3482 rc = ata_set_mode(link, r_failed_dev); 3483 3484 /* if transfer mode has changed, set DUBIOUS_XFER on device */ 3485 ata_for_each_dev(dev, link, ENABLED) { 3486 struct ata_eh_context *ehc = &link->eh_context; 3487 u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno]; 3488 u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno)); 3489 3490 if (dev->xfer_mode != saved_xfer_mode || 3491 ata_ncq_enabled(dev) != saved_ncq) 3492 dev->flags |= ATA_DFLAG_DUBIOUS_XFER; 3493 } 3494 3495 return rc; 3496 } 3497 3498 /** 3499 * atapi_eh_clear_ua - Clear ATAPI UNIT ATTENTION after reset 3500 * @dev: ATAPI device to clear UA for 3501 * 3502 * Resets and other operations can make an ATAPI device raise 3503 * UNIT ATTENTION which causes the next operation to fail. This 3504 * function clears UA. 3505 * 3506 * LOCKING: 3507 * EH context (may sleep). 3508 * 3509 * RETURNS: 3510 * 0 on success, -errno on failure. 3511 */ 3512 static int atapi_eh_clear_ua(struct ata_device *dev) 3513 { 3514 int i; 3515 3516 for (i = 0; i < ATA_EH_UA_TRIES; i++) { 3517 u8 *sense_buffer = dev->sector_buf; 3518 u8 sense_key = 0; 3519 unsigned int err_mask; 3520 3521 err_mask = atapi_eh_tur(dev, &sense_key); 3522 if (err_mask != 0 && err_mask != AC_ERR_DEV) { 3523 ata_dev_warn(dev, 3524 "TEST_UNIT_READY failed (err_mask=0x%x)\n", 3525 err_mask); 3526 return -EIO; 3527 } 3528 3529 if (!err_mask || sense_key != UNIT_ATTENTION) 3530 return 0; 3531 3532 err_mask = atapi_eh_request_sense(dev, sense_buffer, sense_key); 3533 if (err_mask) { 3534 ata_dev_warn(dev, "failed to clear " 3535 "UNIT ATTENTION (err_mask=0x%x)\n", err_mask); 3536 return -EIO; 3537 } 3538 } 3539 3540 ata_dev_warn(dev, "UNIT ATTENTION persists after %d tries\n", 3541 ATA_EH_UA_TRIES); 3542 3543 return 0; 3544 } 3545 3546 /** 3547 * ata_eh_maybe_retry_flush - Retry FLUSH if necessary 3548 * @dev: ATA device which may need FLUSH retry 3549 * 3550 * If @dev failed FLUSH, it needs to be reported upper layer 3551 * immediately as it means that @dev failed to remap and already 3552 * lost at least a sector and further FLUSH retrials won't make 3553 * any difference to the lost sector. However, if FLUSH failed 3554 * for other reasons, for example transmission error, FLUSH needs 3555 * to be retried. 3556 * 3557 * This function determines whether FLUSH failure retry is 3558 * necessary and performs it if so. 3559 * 3560 * RETURNS: 3561 * 0 if EH can continue, -errno if EH needs to be repeated. 3562 */ 3563 static int ata_eh_maybe_retry_flush(struct ata_device *dev) 3564 { 3565 struct ata_link *link = dev->link; 3566 struct ata_port *ap = link->ap; 3567 struct ata_queued_cmd *qc; 3568 struct ata_taskfile tf; 3569 unsigned int err_mask; 3570 int rc = 0; 3571 3572 /* did flush fail for this device? */ 3573 if (!ata_tag_valid(link->active_tag)) 3574 return 0; 3575 3576 qc = __ata_qc_from_tag(ap, link->active_tag); 3577 if (qc->dev != dev || (qc->tf.command != ATA_CMD_FLUSH_EXT && 3578 qc->tf.command != ATA_CMD_FLUSH)) 3579 return 0; 3580 3581 /* if the device failed it, it should be reported to upper layers */ 3582 if (qc->err_mask & AC_ERR_DEV) 3583 return 0; 3584 3585 /* flush failed for some other reason, give it another shot */ 3586 ata_tf_init(dev, &tf); 3587 3588 tf.command = qc->tf.command; 3589 tf.flags |= ATA_TFLAG_DEVICE; 3590 tf.protocol = ATA_PROT_NODATA; 3591 3592 ata_dev_warn(dev, "retrying FLUSH 0x%x Emask 0x%x\n", 3593 tf.command, qc->err_mask); 3594 3595 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0); 3596 if (!err_mask) { 3597 /* 3598 * FLUSH is complete but there's no way to 3599 * successfully complete a failed command from EH. 3600 * Making sure retry is allowed at least once and 3601 * retrying it should do the trick - whatever was in 3602 * the cache is already on the platter and this won't 3603 * cause infinite loop. 3604 */ 3605 qc->scsicmd->allowed = max(qc->scsicmd->allowed, 1); 3606 } else { 3607 ata_dev_warn(dev, "FLUSH failed Emask 0x%x\n", 3608 err_mask); 3609 rc = -EIO; 3610 3611 /* if device failed it, report it to upper layers */ 3612 if (err_mask & AC_ERR_DEV) { 3613 qc->err_mask |= AC_ERR_DEV; 3614 qc->result_tf = tf; 3615 if (!ata_port_is_frozen(ap)) 3616 rc = 0; 3617 } 3618 } 3619 return rc; 3620 } 3621 3622 int ata_link_nr_enabled(struct ata_link *link) 3623 { 3624 struct ata_device *dev; 3625 int cnt = 0; 3626 3627 ata_for_each_dev(dev, link, ENABLED) 3628 cnt++; 3629 return cnt; 3630 } 3631 3632 static int ata_link_nr_vacant(struct ata_link *link) 3633 { 3634 struct ata_device *dev; 3635 int cnt = 0; 3636 3637 ata_for_each_dev(dev, link, ALL) 3638 if (dev->class == ATA_DEV_UNKNOWN) 3639 cnt++; 3640 return cnt; 3641 } 3642 3643 static int ata_eh_skip_recovery(struct ata_link *link) 3644 { 3645 struct ata_port *ap = link->ap; 3646 struct ata_eh_context *ehc = &link->eh_context; 3647 struct ata_device *dev; 3648 3649 /* skip disabled links */ 3650 if (link->flags & ATA_LFLAG_DISABLED) 3651 return 1; 3652 3653 /* skip if explicitly requested */ 3654 if (ehc->i.flags & ATA_EHI_NO_RECOVERY) 3655 return 1; 3656 3657 /* thaw frozen port and recover failed devices */ 3658 if (ata_port_is_frozen(ap) || ata_link_nr_enabled(link)) 3659 return 0; 3660 3661 /* reset at least once if reset is requested */ 3662 if ((ehc->i.action & ATA_EH_RESET) && 3663 !(ehc->i.flags & ATA_EHI_DID_RESET)) 3664 return 0; 3665 3666 /* skip if class codes for all vacant slots are ATA_DEV_NONE */ 3667 ata_for_each_dev(dev, link, ALL) { 3668 if (dev->class == ATA_DEV_UNKNOWN && 3669 ehc->classes[dev->devno] != ATA_DEV_NONE) 3670 return 0; 3671 } 3672 3673 return 1; 3674 } 3675 3676 static int ata_count_probe_trials_cb(struct ata_ering_entry *ent, void *void_arg) 3677 { 3678 u64 interval = msecs_to_jiffies(ATA_EH_PROBE_TRIAL_INTERVAL); 3679 u64 now = get_jiffies_64(); 3680 int *trials = void_arg; 3681 3682 if ((ent->eflags & ATA_EFLAG_OLD_ER) || 3683 (ent->timestamp < now - min(now, interval))) 3684 return -1; 3685 3686 (*trials)++; 3687 return 0; 3688 } 3689 3690 static int ata_eh_schedule_probe(struct ata_device *dev) 3691 { 3692 struct ata_eh_context *ehc = &dev->link->eh_context; 3693 struct ata_link *link = ata_dev_phys_link(dev); 3694 int trials = 0; 3695 3696 if (!(ehc->i.probe_mask & (1 << dev->devno)) || 3697 (ehc->did_probe_mask & (1 << dev->devno))) 3698 return 0; 3699 3700 ata_eh_detach_dev(dev); 3701 ata_dev_init(dev); 3702 ehc->did_probe_mask |= (1 << dev->devno); 3703 ehc->i.action |= ATA_EH_RESET; 3704 ehc->saved_xfer_mode[dev->devno] = 0; 3705 ehc->saved_ncq_enabled &= ~(1 << dev->devno); 3706 3707 /* the link maybe in a deep sleep, wake it up */ 3708 if (link->lpm_policy > ATA_LPM_MAX_POWER) { 3709 if (ata_is_host_link(link)) 3710 link->ap->ops->set_lpm(link, ATA_LPM_MAX_POWER, 3711 ATA_LPM_EMPTY); 3712 else 3713 sata_pmp_set_lpm(link, ATA_LPM_MAX_POWER, 3714 ATA_LPM_EMPTY); 3715 } 3716 3717 /* Record and count probe trials on the ering. The specific 3718 * error mask used is irrelevant. Because a successful device 3719 * detection clears the ering, this count accumulates only if 3720 * there are consecutive failed probes. 3721 * 3722 * If the count is equal to or higher than ATA_EH_PROBE_TRIALS 3723 * in the last ATA_EH_PROBE_TRIAL_INTERVAL, link speed is 3724 * forced to 1.5Gbps. 3725 * 3726 * This is to work around cases where failed link speed 3727 * negotiation results in device misdetection leading to 3728 * infinite DEVXCHG or PHRDY CHG events. 3729 */ 3730 ata_ering_record(&dev->ering, 0, AC_ERR_OTHER); 3731 ata_ering_map(&dev->ering, ata_count_probe_trials_cb, &trials); 3732 3733 if (trials > ATA_EH_PROBE_TRIALS) 3734 sata_down_spd_limit(link, 1); 3735 3736 return 1; 3737 } 3738 3739 static int ata_eh_handle_dev_fail(struct ata_device *dev, int err) 3740 { 3741 struct ata_eh_context *ehc = &dev->link->eh_context; 3742 3743 /* -EAGAIN from EH routine indicates retry without prejudice. 3744 * The requester is responsible for ensuring forward progress. 3745 */ 3746 if (err != -EAGAIN) 3747 ehc->tries[dev->devno]--; 3748 3749 switch (err) { 3750 case -ENODEV: 3751 /* device missing or wrong IDENTIFY data, schedule probing */ 3752 ehc->i.probe_mask |= (1 << dev->devno); 3753 fallthrough; 3754 case -EINVAL: 3755 /* give it just one more chance */ 3756 ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1); 3757 fallthrough; 3758 case -EIO: 3759 if (ehc->tries[dev->devno] == 1) { 3760 /* This is the last chance, better to slow 3761 * down than lose it. 3762 */ 3763 sata_down_spd_limit(ata_dev_phys_link(dev), 0); 3764 if (dev->pio_mode > XFER_PIO_0) 3765 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO); 3766 } 3767 } 3768 3769 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) { 3770 /* disable device if it has used up all its chances */ 3771 ata_dev_disable(dev); 3772 3773 /* detach if offline */ 3774 if (ata_phys_link_offline(ata_dev_phys_link(dev))) 3775 ata_eh_detach_dev(dev); 3776 3777 /* schedule probe if necessary */ 3778 if (ata_eh_schedule_probe(dev)) { 3779 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES; 3780 memset(ehc->cmd_timeout_idx[dev->devno], 0, 3781 sizeof(ehc->cmd_timeout_idx[dev->devno])); 3782 } 3783 3784 return 1; 3785 } else { 3786 ehc->i.action |= ATA_EH_RESET; 3787 return 0; 3788 } 3789 } 3790 3791 /** 3792 * ata_eh_recover - recover host port after error 3793 * @ap: host port to recover 3794 * @reset_ops: The set of reset operations to use 3795 * @r_failed_link: out parameter for failed link 3796 * 3797 * This is the alpha and omega, eum and yang, heart and soul of 3798 * libata exception handling. On entry, actions required to 3799 * recover each link and hotplug requests are recorded in the 3800 * link's eh_context. This function executes all the operations 3801 * with appropriate retrials and fallbacks to resurrect failed 3802 * devices, detach goners and greet newcomers. 3803 * 3804 * LOCKING: 3805 * Kernel thread context (may sleep). 3806 * 3807 * RETURNS: 3808 * 0 on success, -errno on failure. 3809 */ 3810 int ata_eh_recover(struct ata_port *ap, struct ata_reset_operations *reset_ops, 3811 struct ata_link **r_failed_link) 3812 __must_hold(&ap->host->eh_mutex) 3813 { 3814 struct ata_link *link; 3815 struct ata_device *dev; 3816 int rc, nr_fails; 3817 unsigned long flags, deadline; 3818 3819 /* prep for recovery */ 3820 ata_for_each_link(link, ap, EDGE) { 3821 struct ata_eh_context *ehc = &link->eh_context; 3822 3823 /* re-enable link? */ 3824 if (ehc->i.action & ATA_EH_ENABLE_LINK) { 3825 ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK); 3826 spin_lock_irqsave(ap->lock, flags); 3827 link->flags &= ~ATA_LFLAG_DISABLED; 3828 spin_unlock_irqrestore(ap->lock, flags); 3829 ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK); 3830 } 3831 3832 ata_for_each_dev(dev, link, ALL) { 3833 if (link->flags & ATA_LFLAG_NO_RETRY) 3834 ehc->tries[dev->devno] = 1; 3835 else 3836 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES; 3837 3838 /* collect port action mask recorded in dev actions */ 3839 ehc->i.action |= ehc->i.dev_action[dev->devno] & 3840 ~ATA_EH_PERDEV_MASK; 3841 ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK; 3842 3843 /* process hotplug request */ 3844 if (dev->flags & ATA_DFLAG_DETACH) 3845 ata_eh_detach_dev(dev); 3846 3847 /* schedule probe if necessary */ 3848 if (!ata_dev_enabled(dev)) 3849 ata_eh_schedule_probe(dev); 3850 } 3851 } 3852 3853 retry: 3854 rc = 0; 3855 3856 /* if UNLOADING, finish immediately */ 3857 if (ap->pflags & ATA_PFLAG_UNLOADING) 3858 goto out; 3859 3860 /* prep for EH */ 3861 ata_for_each_link(link, ap, EDGE) { 3862 struct ata_eh_context *ehc = &link->eh_context; 3863 3864 /* skip EH if possible. */ 3865 if (ata_eh_skip_recovery(link)) 3866 ehc->i.action = 0; 3867 3868 ata_for_each_dev(dev, link, ALL) 3869 ehc->classes[dev->devno] = ATA_DEV_UNKNOWN; 3870 } 3871 3872 /* reset */ 3873 ata_for_each_link(link, ap, EDGE) { 3874 struct ata_eh_context *ehc = &link->eh_context; 3875 3876 if (!(ehc->i.action & ATA_EH_RESET)) 3877 continue; 3878 3879 rc = ata_eh_reset(ap, link, ata_link_nr_vacant(link), 3880 reset_ops); 3881 if (rc) { 3882 ata_link_err(link, "reset failed, giving up\n"); 3883 goto out; 3884 } 3885 } 3886 3887 do { 3888 unsigned long now; 3889 3890 /* 3891 * clears ATA_EH_PARK in eh_info and resets 3892 * ap->park_req_pending 3893 */ 3894 ata_eh_pull_park_action(ap); 3895 3896 deadline = jiffies; 3897 ata_for_each_link(link, ap, EDGE) { 3898 ata_for_each_dev(dev, link, ALL) { 3899 struct ata_eh_context *ehc = &link->eh_context; 3900 unsigned long tmp; 3901 3902 if (dev->class != ATA_DEV_ATA && 3903 dev->class != ATA_DEV_ZAC) 3904 continue; 3905 if (!(ehc->i.dev_action[dev->devno] & 3906 ATA_EH_PARK)) 3907 continue; 3908 tmp = dev->unpark_deadline; 3909 if (time_before(deadline, tmp)) 3910 deadline = tmp; 3911 else if (time_before_eq(tmp, jiffies)) 3912 continue; 3913 if (ehc->unloaded_mask & (1 << dev->devno)) 3914 continue; 3915 3916 ata_eh_park_issue_cmd(dev, 1); 3917 } 3918 } 3919 3920 now = jiffies; 3921 if (time_before_eq(deadline, now)) 3922 break; 3923 3924 ata_eh_release(ap); 3925 deadline = wait_for_completion_timeout(&ap->park_req_pending, 3926 deadline - now); 3927 ata_eh_acquire(ap); 3928 } while (deadline); 3929 ata_for_each_link(link, ap, EDGE) { 3930 ata_for_each_dev(dev, link, ALL) { 3931 if (!(link->eh_context.unloaded_mask & 3932 (1 << dev->devno))) 3933 continue; 3934 3935 ata_eh_park_issue_cmd(dev, 0); 3936 ata_eh_done(link, dev, ATA_EH_PARK); 3937 } 3938 } 3939 3940 /* the rest */ 3941 nr_fails = 0; 3942 ata_for_each_link(link, ap, PMP_FIRST) { 3943 struct ata_eh_context *ehc = &link->eh_context; 3944 3945 if (sata_pmp_attached(ap) && ata_is_host_link(link)) 3946 goto config_lpm; 3947 3948 /* revalidate existing devices and attach new ones */ 3949 rc = ata_eh_revalidate_and_attach(link, &dev); 3950 if (rc) 3951 goto rest_fail; 3952 3953 /* if PMP got attached, return, pmp EH will take care of it */ 3954 if (link->device->class == ATA_DEV_PMP) { 3955 ehc->i.action = 0; 3956 return 0; 3957 } 3958 3959 /* configure transfer mode if necessary */ 3960 if (ehc->i.flags & ATA_EHI_SETMODE) { 3961 rc = ata_eh_set_mode(link, &dev); 3962 if (rc) 3963 goto rest_fail; 3964 ehc->i.flags &= ~ATA_EHI_SETMODE; 3965 } 3966 3967 /* If reset has been issued, clear UA to avoid 3968 * disrupting the current users of the device. 3969 */ 3970 if (ehc->i.flags & ATA_EHI_DID_RESET) { 3971 ata_for_each_dev(dev, link, ALL) { 3972 if (dev->class != ATA_DEV_ATAPI) 3973 continue; 3974 rc = atapi_eh_clear_ua(dev); 3975 if (rc) 3976 goto rest_fail; 3977 if (zpodd_dev_enabled(dev)) 3978 zpodd_post_poweron(dev); 3979 } 3980 } 3981 3982 /* 3983 * Make sure to transition devices to the active power mode 3984 * if needed (e.g. if we were scheduled on system resume). 3985 */ 3986 ata_for_each_dev(dev, link, ENABLED) { 3987 if (ehc->i.dev_action[dev->devno] & ATA_EH_SET_ACTIVE) { 3988 ata_dev_power_set_active(dev); 3989 ata_eh_done(link, dev, ATA_EH_SET_ACTIVE); 3990 } 3991 } 3992 3993 /* retry flush if necessary */ 3994 ata_for_each_dev(dev, link, ALL) { 3995 if (dev->class != ATA_DEV_ATA && 3996 dev->class != ATA_DEV_ZAC) 3997 continue; 3998 rc = ata_eh_maybe_retry_flush(dev); 3999 if (rc) 4000 goto rest_fail; 4001 } 4002 4003 config_lpm: 4004 /* configure link power saving */ 4005 if (link->lpm_policy != ap->target_lpm_policy) { 4006 rc = ata_eh_link_set_lpm(link, ap->target_lpm_policy, 4007 &dev); 4008 if (rc) 4009 goto rest_fail; 4010 } 4011 4012 /* this link is okay now */ 4013 ehc->i.flags = 0; 4014 continue; 4015 4016 rest_fail: 4017 nr_fails++; 4018 if (dev) 4019 ata_eh_handle_dev_fail(dev, rc); 4020 4021 if (ata_port_is_frozen(ap)) { 4022 /* PMP reset requires working host port. 4023 * Can't retry if it's frozen. 4024 */ 4025 if (sata_pmp_attached(ap)) 4026 goto out; 4027 break; 4028 } 4029 } 4030 4031 if (nr_fails) 4032 goto retry; 4033 4034 out: 4035 if (rc && r_failed_link) 4036 *r_failed_link = link; 4037 4038 return rc; 4039 } 4040 4041 /** 4042 * ata_eh_finish - finish up EH 4043 * @ap: host port to finish EH for 4044 * 4045 * Recovery is complete. Clean up EH states and retry or finish 4046 * failed qcs. 4047 * 4048 * LOCKING: 4049 * None. 4050 */ 4051 void ata_eh_finish(struct ata_port *ap) 4052 { 4053 struct ata_queued_cmd *qc; 4054 int tag; 4055 4056 /* retry or finish qcs */ 4057 ata_qc_for_each_raw(ap, qc, tag) { 4058 if (!(qc->flags & ATA_QCFLAG_EH)) 4059 continue; 4060 4061 if (qc->err_mask) { 4062 /* FIXME: Once EH migration is complete, 4063 * generate sense data in this function, 4064 * considering both err_mask and tf. 4065 */ 4066 if (qc->flags & ATA_QCFLAG_RETRY) { 4067 /* 4068 * Since qc->err_mask is set, ata_eh_qc_retry() 4069 * will not increment scmd->allowed, so upper 4070 * layer will only retry the command if it has 4071 * not already been retried too many times. 4072 */ 4073 ata_eh_qc_retry(qc); 4074 } else { 4075 ata_eh_qc_complete(qc); 4076 } 4077 } else { 4078 if (qc->flags & ATA_QCFLAG_SENSE_VALID || 4079 qc->flags & ATA_QCFLAG_EH_SUCCESS_CMD) { 4080 ata_eh_qc_complete(qc); 4081 } else { 4082 /* feed zero TF to sense generation */ 4083 memset(&qc->result_tf, 0, sizeof(qc->result_tf)); 4084 /* 4085 * Since qc->err_mask is not set, 4086 * ata_eh_qc_retry() will increment 4087 * scmd->allowed, so upper layer is guaranteed 4088 * to retry the command. 4089 */ 4090 ata_eh_qc_retry(qc); 4091 } 4092 } 4093 } 4094 4095 /* make sure nr_active_links is zero after EH */ 4096 WARN_ON(ap->nr_active_links); 4097 ap->nr_active_links = 0; 4098 } 4099 4100 /** 4101 * ata_std_error_handler - standard error handler 4102 * @ap: host port to handle error for 4103 * 4104 * Perform standard error handling sequence. 4105 * 4106 * LOCKING: 4107 * Kernel thread context (may sleep). 4108 */ 4109 void ata_std_error_handler(struct ata_port *ap) 4110 __must_hold(&ap->host->eh_mutex) 4111 { 4112 struct ata_reset_operations *reset_ops = &ap->ops->reset; 4113 struct ata_link *link = &ap->link; 4114 int rc; 4115 4116 /* Ignore built-in hardresets if SCR access is not available */ 4117 if ((reset_ops->hardreset == sata_std_hardreset || 4118 reset_ops->hardreset == sata_sff_hardreset) && 4119 !sata_scr_valid(link)) 4120 link->flags |= ATA_LFLAG_NO_HRST; 4121 4122 ata_eh_autopsy(ap); 4123 ata_eh_report(ap); 4124 4125 rc = ata_eh_recover(ap, reset_ops, NULL); 4126 if (rc) { 4127 struct ata_device *dev; 4128 4129 ata_for_each_dev(dev, link, ALL) 4130 ata_dev_disable(dev); 4131 } 4132 4133 ata_eh_finish(ap); 4134 } 4135 EXPORT_SYMBOL_GPL(ata_std_error_handler); 4136 4137 #ifdef CONFIG_PM 4138 /** 4139 * ata_eh_handle_port_suspend - perform port suspend operation 4140 * @ap: port to suspend 4141 * 4142 * Suspend @ap. 4143 * 4144 * LOCKING: 4145 * Kernel thread context (may sleep). 4146 */ 4147 static void ata_eh_handle_port_suspend(struct ata_port *ap) 4148 { 4149 unsigned long flags; 4150 int rc = 0; 4151 struct ata_device *dev; 4152 struct ata_link *link; 4153 4154 /* are we suspending? */ 4155 spin_lock_irqsave(ap->lock, flags); 4156 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) || 4157 ap->pm_mesg.event & PM_EVENT_RESUME) { 4158 spin_unlock_irqrestore(ap->lock, flags); 4159 return; 4160 } 4161 spin_unlock_irqrestore(ap->lock, flags); 4162 4163 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED); 4164 4165 /* 4166 * We will reach this point for all of the PM events: 4167 * PM_EVENT_SUSPEND (if runtime pm, PM_EVENT_AUTO will also be set) 4168 * PM_EVENT_FREEZE, and PM_EVENT_HIBERNATE. 4169 * 4170 * We do not want to perform disk spin down for PM_EVENT_FREEZE. 4171 * (Spin down will be performed by the subsequent PM_EVENT_HIBERNATE.) 4172 */ 4173 if (!(ap->pm_mesg.event & PM_EVENT_FREEZE)) { 4174 /* Set all devices attached to the port in standby mode */ 4175 ata_for_each_link(link, ap, HOST_FIRST) { 4176 ata_for_each_dev(dev, link, ENABLED) 4177 ata_dev_power_set_standby(dev); 4178 } 4179 } 4180 4181 /* 4182 * If we have a ZPODD attached, check its zero 4183 * power ready status before the port is frozen. 4184 * Only needed for runtime suspend. 4185 */ 4186 if (PMSG_IS_AUTO(ap->pm_mesg)) { 4187 ata_for_each_dev(dev, &ap->link, ENABLED) { 4188 if (zpodd_dev_enabled(dev)) 4189 zpodd_on_suspend(dev); 4190 } 4191 } 4192 4193 /* suspend */ 4194 ata_eh_freeze_port(ap); 4195 4196 if (ap->ops->port_suspend) 4197 rc = ap->ops->port_suspend(ap, ap->pm_mesg); 4198 4199 ata_acpi_set_state(ap, ap->pm_mesg); 4200 4201 /* update the flags */ 4202 spin_lock_irqsave(ap->lock, flags); 4203 4204 ap->pflags &= ~ATA_PFLAG_PM_PENDING; 4205 if (rc == 0) 4206 ap->pflags |= ATA_PFLAG_SUSPENDED; 4207 else if (ata_port_is_frozen(ap)) 4208 ata_port_schedule_eh(ap); 4209 4210 spin_unlock_irqrestore(ap->lock, flags); 4211 4212 return; 4213 } 4214 4215 /** 4216 * ata_eh_handle_port_resume - perform port resume operation 4217 * @ap: port to resume 4218 * 4219 * Resume @ap. 4220 * 4221 * LOCKING: 4222 * Kernel thread context (may sleep). 4223 */ 4224 static void ata_eh_handle_port_resume(struct ata_port *ap) 4225 { 4226 struct ata_link *link; 4227 struct ata_device *dev; 4228 unsigned long flags; 4229 4230 /* are we resuming? */ 4231 spin_lock_irqsave(ap->lock, flags); 4232 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) || 4233 !(ap->pm_mesg.event & PM_EVENT_RESUME)) { 4234 spin_unlock_irqrestore(ap->lock, flags); 4235 return; 4236 } 4237 spin_unlock_irqrestore(ap->lock, flags); 4238 4239 WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED)); 4240 4241 /* 4242 * Error timestamps are in jiffies which doesn't run while 4243 * suspended and PHY events during resume isn't too uncommon. 4244 * When the two are combined, it can lead to unnecessary speed 4245 * downs if the machine is suspended and resumed repeatedly. 4246 * Clear error history. 4247 */ 4248 ata_for_each_link(link, ap, HOST_FIRST) 4249 ata_for_each_dev(dev, link, ALL) 4250 ata_ering_clear(&dev->ering); 4251 4252 ata_acpi_set_state(ap, ap->pm_mesg); 4253 4254 if (ap->ops->port_resume) 4255 ap->ops->port_resume(ap); 4256 4257 /* tell ACPI that we're resuming */ 4258 ata_acpi_on_resume(ap); 4259 4260 /* update the flags */ 4261 spin_lock_irqsave(ap->lock, flags); 4262 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED); 4263 ap->pflags |= ATA_PFLAG_RESUMING; 4264 spin_unlock_irqrestore(ap->lock, flags); 4265 } 4266 #endif /* CONFIG_PM */ 4267