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