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