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