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