1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * libata-eh.c - libata error handling
7 * libata documentation is available via 'make {ps|pdf}docs',
8 * as Documentation/driver-api/libata.rst
10 * Hardware documentation available from http://www.t13.org/ and
11 * http://www.sata-io.org/
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"
26 #include <linux/libata.h>
28 #include <trace/events/libata.h>
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),
39 ATA_EFLAG_IS_IO = (1 << 0),
40 ATA_EFLAG_DUBIOUS_XFER = (1 << 1),
41 ATA_EFLAG_OLD_ER = (1 << 31),
43 /* error categories */
46 ATA_ECAT_TOUT_HSM = 2,
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,
54 ATA_EH_CMD_DFL_TIMEOUT = 5000,
56 /* always put at least this amount of time between resets */
57 ATA_EH_RESET_COOL_DOWN = 5000,
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.
64 ATA_EH_PRERESET_TIMEOUT = 10000,
65 ATA_EH_FASTDRAIN_INTERVAL = 3000,
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,
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.
81 static const unsigned long 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 ULONG_MAX, /* > 1 min has elapsed, give up */
89 static const unsigned long 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 */
96 static const unsigned long 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 */
102 static const unsigned long ata_eh_flush_timeouts[] = {
103 15000, /* be generous with flush */
105 30000, /* and even more generous */
109 static const unsigned long ata_eh_other_timeouts[] = {
110 5000, /* same rationale as identify timeout */
112 /* but no merciful 30sec for other commands, it just isn't worth it */
116 struct ata_eh_cmd_timeout_ent {
118 const unsigned long *timeouts;
121 /* The following table determines timeouts to use for EH internal
122 * commands. Each table entry is a command class and matches the
123 * commands the entry applies to and the timeout table to use.
125 * On the retry after a command timed out, the next timeout value from
126 * the table is used. If the table doesn't contain further entries,
127 * the last value is used.
129 * ehc->cmd_timeout_idx keeps track of which timeout to use per
130 * command class, so if SET_FEATURES times out on the first try, the
131 * next try will use the second timeout value only for that class.
133 #define CMDS(cmds...) (const u8 []){ cmds, 0 }
134 static const struct ata_eh_cmd_timeout_ent
135 ata_eh_cmd_timeout_table[ATA_EH_CMD_TIMEOUT_TABLE_SIZE] = {
136 { .commands = CMDS(ATA_CMD_ID_ATA, ATA_CMD_ID_ATAPI),
137 .timeouts = ata_eh_identify_timeouts, },
138 { .commands = CMDS(ATA_CMD_READ_LOG_EXT, ATA_CMD_READ_LOG_DMA_EXT),
139 .timeouts = ata_eh_revalidate_timeouts, },
140 { .commands = CMDS(ATA_CMD_READ_NATIVE_MAX, ATA_CMD_READ_NATIVE_MAX_EXT),
141 .timeouts = ata_eh_other_timeouts, },
142 { .commands = CMDS(ATA_CMD_SET_MAX, ATA_CMD_SET_MAX_EXT),
143 .timeouts = ata_eh_other_timeouts, },
144 { .commands = CMDS(ATA_CMD_SET_FEATURES),
145 .timeouts = ata_eh_other_timeouts, },
146 { .commands = CMDS(ATA_CMD_INIT_DEV_PARAMS),
147 .timeouts = ata_eh_other_timeouts, },
148 { .commands = CMDS(ATA_CMD_FLUSH, ATA_CMD_FLUSH_EXT),
149 .timeouts = ata_eh_flush_timeouts },
153 static void __ata_port_freeze(struct ata_port *ap);
155 static void ata_eh_handle_port_suspend(struct ata_port *ap);
156 static void ata_eh_handle_port_resume(struct ata_port *ap);
157 #else /* CONFIG_PM */
158 static void ata_eh_handle_port_suspend(struct ata_port *ap)
161 static void ata_eh_handle_port_resume(struct ata_port *ap)
163 #endif /* CONFIG_PM */
165 static __printf(2, 0) void __ata_ehi_pushv_desc(struct ata_eh_info *ehi,
166 const char *fmt, va_list args)
168 ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
169 ATA_EH_DESC_LEN - ehi->desc_len,
174 * __ata_ehi_push_desc - push error description without adding separator
176 * @fmt: printf format string
178 * Format string according to @fmt and append it to @ehi->desc.
181 * spin_lock_irqsave(host lock)
183 void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
188 __ata_ehi_pushv_desc(ehi, fmt, args);
191 EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
194 * ata_ehi_push_desc - push error description with separator
196 * @fmt: printf format string
198 * Format string according to @fmt and append it to @ehi->desc.
199 * If @ehi->desc is not empty, ", " is added in-between.
202 * spin_lock_irqsave(host lock)
204 void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
209 __ata_ehi_push_desc(ehi, ", ");
212 __ata_ehi_pushv_desc(ehi, fmt, args);
215 EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
218 * ata_ehi_clear_desc - clean error description
224 * spin_lock_irqsave(host lock)
226 void ata_ehi_clear_desc(struct ata_eh_info *ehi)
231 EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
234 * ata_port_desc - append port description
235 * @ap: target ATA port
236 * @fmt: printf format string
238 * Format string according to @fmt and append it to port
239 * description. If port description is not empty, " " is added
240 * in-between. This function is to be used while initializing
241 * ata_host. The description is printed on host registration.
246 void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
250 WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
252 if (ap->link.eh_info.desc_len)
253 __ata_ehi_push_desc(&ap->link.eh_info, " ");
256 __ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
259 EXPORT_SYMBOL_GPL(ata_port_desc);
263 * ata_port_pbar_desc - append PCI BAR description
264 * @ap: target ATA port
265 * @bar: target PCI BAR
266 * @offset: offset into PCI BAR
267 * @name: name of the area
269 * If @offset is negative, this function formats a string which
270 * contains the name, address, size and type of the BAR and
271 * appends it to the port description. If @offset is zero or
272 * positive, only name and offsetted address is appended.
277 void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
280 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
282 unsigned long long start, len;
284 if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
286 else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
289 start = (unsigned long long)pci_resource_start(pdev, bar);
290 len = (unsigned long long)pci_resource_len(pdev, bar);
293 ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
295 ata_port_desc(ap, "%s 0x%llx", name,
296 start + (unsigned long long)offset);
298 EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
299 #endif /* CONFIG_PCI */
301 static int ata_lookup_timeout_table(u8 cmd)
305 for (i = 0; i < ATA_EH_CMD_TIMEOUT_TABLE_SIZE; i++) {
308 for (cur = ata_eh_cmd_timeout_table[i].commands; *cur; cur++)
317 * ata_internal_cmd_timeout - determine timeout for an internal command
318 * @dev: target device
319 * @cmd: internal command to be issued
321 * Determine timeout for internal command @cmd for @dev.
327 * Determined timeout.
329 unsigned long ata_internal_cmd_timeout(struct ata_device *dev, u8 cmd)
331 struct ata_eh_context *ehc = &dev->link->eh_context;
332 int ent = ata_lookup_timeout_table(cmd);
336 return ATA_EH_CMD_DFL_TIMEOUT;
338 idx = ehc->cmd_timeout_idx[dev->devno][ent];
339 return ata_eh_cmd_timeout_table[ent].timeouts[idx];
343 * ata_internal_cmd_timed_out - notification for internal command timeout
344 * @dev: target device
345 * @cmd: internal command which timed out
347 * Notify EH that internal command @cmd for @dev timed out. This
348 * function should be called only for commands whose timeouts are
349 * determined using ata_internal_cmd_timeout().
354 void ata_internal_cmd_timed_out(struct ata_device *dev, u8 cmd)
356 struct ata_eh_context *ehc = &dev->link->eh_context;
357 int ent = ata_lookup_timeout_table(cmd);
363 idx = ehc->cmd_timeout_idx[dev->devno][ent];
364 if (ata_eh_cmd_timeout_table[ent].timeouts[idx + 1] != ULONG_MAX)
365 ehc->cmd_timeout_idx[dev->devno][ent]++;
368 static void ata_ering_record(struct ata_ering *ering, unsigned int eflags,
369 unsigned int err_mask)
371 struct ata_ering_entry *ent;
376 ering->cursor %= ATA_ERING_SIZE;
378 ent = &ering->ring[ering->cursor];
379 ent->eflags = eflags;
380 ent->err_mask = err_mask;
381 ent->timestamp = get_jiffies_64();
384 static struct ata_ering_entry *ata_ering_top(struct ata_ering *ering)
386 struct ata_ering_entry *ent = &ering->ring[ering->cursor];
393 int ata_ering_map(struct ata_ering *ering,
394 int (*map_fn)(struct ata_ering_entry *, void *),
398 struct ata_ering_entry *ent;
402 ent = &ering->ring[idx];
405 rc = map_fn(ent, arg);
408 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
409 } while (idx != ering->cursor);
414 static int ata_ering_clear_cb(struct ata_ering_entry *ent, void *void_arg)
416 ent->eflags |= ATA_EFLAG_OLD_ER;
420 static void ata_ering_clear(struct ata_ering *ering)
422 ata_ering_map(ering, ata_ering_clear_cb, NULL);
425 static unsigned int ata_eh_dev_action(struct ata_device *dev)
427 struct ata_eh_context *ehc = &dev->link->eh_context;
429 return ehc->i.action | ehc->i.dev_action[dev->devno];
432 static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
433 struct ata_eh_info *ehi, unsigned int action)
435 struct ata_device *tdev;
438 ehi->action &= ~action;
439 ata_for_each_dev(tdev, link, ALL)
440 ehi->dev_action[tdev->devno] &= ~action;
442 /* doesn't make sense for port-wide EH actions */
443 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
445 /* break ehi->action into ehi->dev_action */
446 if (ehi->action & action) {
447 ata_for_each_dev(tdev, link, ALL)
448 ehi->dev_action[tdev->devno] |=
449 ehi->action & action;
450 ehi->action &= ~action;
453 /* turn off the specified per-dev action */
454 ehi->dev_action[dev->devno] &= ~action;
459 * ata_eh_acquire - acquire EH ownership
460 * @ap: ATA port to acquire EH ownership for
462 * Acquire EH ownership for @ap. This is the basic exclusion
463 * mechanism for ports sharing a host. Only one port hanging off
464 * the same host can claim the ownership of EH.
469 void ata_eh_acquire(struct ata_port *ap)
471 mutex_lock(&ap->host->eh_mutex);
472 WARN_ON_ONCE(ap->host->eh_owner);
473 ap->host->eh_owner = current;
477 * ata_eh_release - release EH ownership
478 * @ap: ATA port to release EH ownership for
480 * Release EH ownership for @ap if the caller. The caller must
481 * have acquired EH ownership using ata_eh_acquire() previously.
486 void ata_eh_release(struct ata_port *ap)
488 WARN_ON_ONCE(ap->host->eh_owner != current);
489 ap->host->eh_owner = NULL;
490 mutex_unlock(&ap->host->eh_mutex);
493 static void ata_eh_unload(struct ata_port *ap)
495 struct ata_link *link;
496 struct ata_device *dev;
499 /* Restore SControl IPM and SPD for the next driver and
500 * disable attached devices.
502 ata_for_each_link(link, ap, PMP_FIRST) {
503 sata_scr_write(link, SCR_CONTROL, link->saved_scontrol & 0xff0);
504 ata_for_each_dev(dev, link, ALL)
505 ata_dev_disable(dev);
508 /* freeze and set UNLOADED */
509 spin_lock_irqsave(ap->lock, flags);
511 ata_port_freeze(ap); /* won't be thawed */
512 ap->pflags &= ~ATA_PFLAG_EH_PENDING; /* clear pending from freeze */
513 ap->pflags |= ATA_PFLAG_UNLOADED;
515 spin_unlock_irqrestore(ap->lock, flags);
519 * ata_scsi_error - SCSI layer error handler callback
520 * @host: SCSI host on which error occurred
522 * Handles SCSI-layer-thrown error events.
525 * Inherited from SCSI layer (none, can sleep)
530 void ata_scsi_error(struct Scsi_Host *host)
532 struct ata_port *ap = ata_shost_to_port(host);
534 LIST_HEAD(eh_work_q);
538 spin_lock_irqsave(host->host_lock, flags);
539 list_splice_init(&host->eh_cmd_q, &eh_work_q);
540 spin_unlock_irqrestore(host->host_lock, flags);
542 ata_scsi_cmd_error_handler(host, ap, &eh_work_q);
544 /* If we timed raced normal completion and there is nothing to
545 recover nr_timedout == 0 why exactly are we doing error recovery ? */
546 ata_scsi_port_error_handler(host, ap);
548 /* finish or retry handled scmd's and clean up */
549 WARN_ON(!list_empty(&eh_work_q));
555 * ata_scsi_cmd_error_handler - error callback for a list of commands
556 * @host: scsi host containing the port
557 * @ap: ATA port within the host
558 * @eh_work_q: list of commands to process
560 * process the given list of commands and return those finished to the
561 * ap->eh_done_q. This function is the first part of the libata error
562 * handler which processes a given list of failed commands.
564 void ata_scsi_cmd_error_handler(struct Scsi_Host *host, struct ata_port *ap,
565 struct list_head *eh_work_q)
570 /* make sure sff pio task is not running */
571 ata_sff_flush_pio_task(ap);
573 /* synchronize with host lock and sort out timeouts */
575 /* For new EH, all qcs are finished in one of three ways -
576 * normal completion, error completion, and SCSI timeout.
577 * Both completions can race against SCSI timeout. When normal
578 * completion wins, the qc never reaches EH. When error
579 * completion wins, the qc has ATA_QCFLAG_FAILED set.
581 * When SCSI timeout wins, things are a bit more complex.
582 * Normal or error completion can occur after the timeout but
583 * before this point. In such cases, both types of
584 * completions are honored. A scmd is determined to have
585 * timed out iff its associated qc is active and not failed.
587 spin_lock_irqsave(ap->lock, flags);
588 if (ap->ops->error_handler) {
589 struct scsi_cmnd *scmd, *tmp;
592 /* This must occur under the ap->lock as we don't want
593 a polled recovery to race the real interrupt handler
595 The lost_interrupt handler checks for any completed but
596 non-notified command and completes much like an IRQ handler.
598 We then fall into the error recovery code which will treat
599 this as if normal completion won the race */
601 if (ap->ops->lost_interrupt)
602 ap->ops->lost_interrupt(ap);
604 list_for_each_entry_safe(scmd, tmp, eh_work_q, eh_entry) {
605 struct ata_queued_cmd *qc;
607 ata_qc_for_each_raw(ap, qc, i) {
608 if (qc->flags & ATA_QCFLAG_ACTIVE &&
613 if (i < ATA_MAX_QUEUE) {
614 /* the scmd has an associated qc */
615 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
616 /* which hasn't failed yet, timeout */
617 qc->err_mask |= AC_ERR_TIMEOUT;
618 qc->flags |= ATA_QCFLAG_FAILED;
622 /* Normal completion occurred after
623 * SCSI timeout but before this point.
624 * Successfully complete it.
626 scmd->retries = scmd->allowed;
627 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
631 /* If we have timed out qcs. They belong to EH from
632 * this point but the state of the controller is
633 * unknown. Freeze the port to make sure the IRQ
634 * handler doesn't diddle with those qcs. This must
635 * be done atomically w.r.t. setting QCFLAG_FAILED.
638 __ata_port_freeze(ap);
641 /* initialize eh_tries */
642 ap->eh_tries = ATA_EH_MAX_TRIES;
644 spin_unlock_irqrestore(ap->lock, flags);
647 EXPORT_SYMBOL(ata_scsi_cmd_error_handler);
650 * ata_scsi_port_error_handler - recover the port after the commands
651 * @host: SCSI host containing the port
654 * Handle the recovery of the port @ap after all the commands
655 * have been recovered.
657 void ata_scsi_port_error_handler(struct Scsi_Host *host, struct ata_port *ap)
661 /* invoke error handler */
662 if (ap->ops->error_handler) {
663 struct ata_link *link;
665 /* acquire EH ownership */
668 /* kill fast drain timer */
669 del_timer_sync(&ap->fastdrain_timer);
671 /* process port resume request */
672 ata_eh_handle_port_resume(ap);
674 /* fetch & clear EH info */
675 spin_lock_irqsave(ap->lock, flags);
677 ata_for_each_link(link, ap, HOST_FIRST) {
678 struct ata_eh_context *ehc = &link->eh_context;
679 struct ata_device *dev;
681 memset(&link->eh_context, 0, sizeof(link->eh_context));
682 link->eh_context.i = link->eh_info;
683 memset(&link->eh_info, 0, sizeof(link->eh_info));
685 ata_for_each_dev(dev, link, ENABLED) {
686 int devno = dev->devno;
688 ehc->saved_xfer_mode[devno] = dev->xfer_mode;
689 if (ata_ncq_enabled(dev))
690 ehc->saved_ncq_enabled |= 1 << devno;
694 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
695 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
696 ap->excl_link = NULL; /* don't maintain exclusion over EH */
698 spin_unlock_irqrestore(ap->lock, flags);
700 /* invoke EH, skip if unloading or suspended */
701 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
702 ap->ops->error_handler(ap);
704 /* if unloading, commence suicide */
705 if ((ap->pflags & ATA_PFLAG_UNLOADING) &&
706 !(ap->pflags & ATA_PFLAG_UNLOADED))
711 /* process port suspend request */
712 ata_eh_handle_port_suspend(ap);
714 /* Exception might have happened after ->error_handler
715 * recovered the port but before this point. Repeat
718 spin_lock_irqsave(ap->lock, flags);
720 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
721 if (--ap->eh_tries) {
722 spin_unlock_irqrestore(ap->lock, flags);
726 "EH pending after %d tries, giving up\n",
728 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
731 /* this run is complete, make sure EH info is clear */
732 ata_for_each_link(link, ap, HOST_FIRST)
733 memset(&link->eh_info, 0, sizeof(link->eh_info));
735 /* end eh (clear host_eh_scheduled) while holding
736 * ap->lock such that if exception occurs after this
737 * point but before EH completion, SCSI midlayer will
742 spin_unlock_irqrestore(ap->lock, flags);
745 WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
746 ap->ops->eng_timeout(ap);
749 scsi_eh_flush_done_q(&ap->eh_done_q);
752 spin_lock_irqsave(ap->lock, flags);
754 if (ap->pflags & ATA_PFLAG_LOADING)
755 ap->pflags &= ~ATA_PFLAG_LOADING;
756 else if ((ap->pflags & ATA_PFLAG_SCSI_HOTPLUG) &&
757 !(ap->flags & ATA_FLAG_SAS_HOST))
758 schedule_delayed_work(&ap->hotplug_task, 0);
760 if (ap->pflags & ATA_PFLAG_RECOVERED)
761 ata_port_info(ap, "EH complete\n");
763 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
765 /* tell wait_eh that we're done */
766 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
767 wake_up_all(&ap->eh_wait_q);
769 spin_unlock_irqrestore(ap->lock, flags);
771 EXPORT_SYMBOL_GPL(ata_scsi_port_error_handler);
774 * ata_port_wait_eh - Wait for the currently pending EH to complete
775 * @ap: Port to wait EH for
777 * Wait until the currently pending EH is complete.
780 * Kernel thread context (may sleep).
782 void ata_port_wait_eh(struct ata_port *ap)
788 spin_lock_irqsave(ap->lock, flags);
790 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
791 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
792 spin_unlock_irqrestore(ap->lock, flags);
794 spin_lock_irqsave(ap->lock, flags);
796 finish_wait(&ap->eh_wait_q, &wait);
798 spin_unlock_irqrestore(ap->lock, flags);
800 /* make sure SCSI EH is complete */
801 if (scsi_host_in_recovery(ap->scsi_host)) {
806 EXPORT_SYMBOL_GPL(ata_port_wait_eh);
808 static int ata_eh_nr_in_flight(struct ata_port *ap)
810 struct ata_queued_cmd *qc;
814 /* count only non-internal commands */
815 ata_qc_for_each(ap, qc, tag) {
823 void ata_eh_fastdrain_timerfn(struct timer_list *t)
825 struct ata_port *ap = from_timer(ap, t, fastdrain_timer);
829 spin_lock_irqsave(ap->lock, flags);
831 cnt = ata_eh_nr_in_flight(ap);
837 if (cnt == ap->fastdrain_cnt) {
838 struct ata_queued_cmd *qc;
841 /* No progress during the last interval, tag all
842 * in-flight qcs as timed out and freeze the port.
844 ata_qc_for_each(ap, qc, tag) {
846 qc->err_mask |= AC_ERR_TIMEOUT;
851 /* some qcs have finished, give it another chance */
852 ap->fastdrain_cnt = cnt;
853 ap->fastdrain_timer.expires =
854 ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
855 add_timer(&ap->fastdrain_timer);
859 spin_unlock_irqrestore(ap->lock, flags);
863 * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
864 * @ap: target ATA port
865 * @fastdrain: activate fast drain
867 * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
868 * is non-zero and EH wasn't pending before. Fast drain ensures
869 * that EH kicks in in timely manner.
872 * spin_lock_irqsave(host lock)
874 static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
878 /* already scheduled? */
879 if (ap->pflags & ATA_PFLAG_EH_PENDING)
882 ap->pflags |= ATA_PFLAG_EH_PENDING;
887 /* do we have in-flight qcs? */
888 cnt = ata_eh_nr_in_flight(ap);
892 /* activate fast drain */
893 ap->fastdrain_cnt = cnt;
894 ap->fastdrain_timer.expires =
895 ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
896 add_timer(&ap->fastdrain_timer);
900 * ata_qc_schedule_eh - schedule qc for error handling
901 * @qc: command to schedule error handling for
903 * Schedule error handling for @qc. EH will kick in as soon as
904 * other commands are drained.
907 * spin_lock_irqsave(host lock)
909 void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
911 struct ata_port *ap = qc->ap;
913 WARN_ON(!ap->ops->error_handler);
915 qc->flags |= ATA_QCFLAG_FAILED;
916 ata_eh_set_pending(ap, 1);
918 /* The following will fail if timeout has already expired.
919 * ata_scsi_error() takes care of such scmds on EH entry.
920 * Note that ATA_QCFLAG_FAILED is unconditionally set after
921 * this function completes.
923 blk_abort_request(scsi_cmd_to_rq(qc->scsicmd));
927 * ata_std_sched_eh - non-libsas ata_ports issue eh with this common routine
928 * @ap: ATA port to schedule EH for
930 * LOCKING: inherited from ata_port_schedule_eh
931 * spin_lock_irqsave(host lock)
933 void ata_std_sched_eh(struct ata_port *ap)
935 WARN_ON(!ap->ops->error_handler);
937 if (ap->pflags & ATA_PFLAG_INITIALIZING)
940 ata_eh_set_pending(ap, 1);
941 scsi_schedule_eh(ap->scsi_host);
943 DPRINTK("port EH scheduled\n");
945 EXPORT_SYMBOL_GPL(ata_std_sched_eh);
948 * ata_std_end_eh - non-libsas ata_ports complete eh with this common routine
949 * @ap: ATA port to end EH for
951 * In the libata object model there is a 1:1 mapping of ata_port to
952 * shost, so host fields can be directly manipulated under ap->lock, in
953 * the libsas case we need to hold a lock at the ha->level to coordinate
957 * spin_lock_irqsave(host lock)
959 void ata_std_end_eh(struct ata_port *ap)
961 struct Scsi_Host *host = ap->scsi_host;
963 host->host_eh_scheduled = 0;
965 EXPORT_SYMBOL(ata_std_end_eh);
969 * ata_port_schedule_eh - schedule error handling without a qc
970 * @ap: ATA port to schedule EH for
972 * Schedule error handling for @ap. EH will kick in as soon as
973 * all commands are drained.
976 * spin_lock_irqsave(host lock)
978 void ata_port_schedule_eh(struct ata_port *ap)
980 /* see: ata_std_sched_eh, unless you know better */
981 ap->ops->sched_eh(ap);
983 EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
985 static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
987 struct ata_queued_cmd *qc;
988 int tag, nr_aborted = 0;
990 WARN_ON(!ap->ops->error_handler);
992 /* we're gonna abort all commands, no need for fast drain */
993 ata_eh_set_pending(ap, 0);
995 /* include internal tag in iteration */
996 ata_qc_for_each_with_internal(ap, qc, tag) {
997 if (qc && (!link || qc->dev->link == link)) {
998 qc->flags |= ATA_QCFLAG_FAILED;
1005 ata_port_schedule_eh(ap);
1011 * ata_link_abort - abort all qc's on the link
1012 * @link: ATA link to abort qc's for
1014 * Abort all active qc's active on @link and schedule EH.
1017 * spin_lock_irqsave(host lock)
1020 * Number of aborted qc's.
1022 int ata_link_abort(struct ata_link *link)
1024 return ata_do_link_abort(link->ap, link);
1026 EXPORT_SYMBOL_GPL(ata_link_abort);
1029 * ata_port_abort - abort all qc's on the port
1030 * @ap: ATA port to abort qc's for
1032 * Abort all active qc's of @ap and schedule EH.
1035 * spin_lock_irqsave(host_set lock)
1038 * Number of aborted qc's.
1040 int ata_port_abort(struct ata_port *ap)
1042 return ata_do_link_abort(ap, NULL);
1044 EXPORT_SYMBOL_GPL(ata_port_abort);
1047 * __ata_port_freeze - freeze port
1048 * @ap: ATA port to freeze
1050 * This function is called when HSM violation or some other
1051 * condition disrupts normal operation of the port. Frozen port
1052 * is not allowed to perform any operation until the port is
1053 * thawed, which usually follows a successful reset.
1055 * ap->ops->freeze() callback can be used for freezing the port
1056 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
1057 * port cannot be frozen hardware-wise, the interrupt handler
1058 * must ack and clear interrupts unconditionally while the port
1062 * spin_lock_irqsave(host lock)
1064 static void __ata_port_freeze(struct ata_port *ap)
1066 WARN_ON(!ap->ops->error_handler);
1068 if (ap->ops->freeze)
1069 ap->ops->freeze(ap);
1071 ap->pflags |= ATA_PFLAG_FROZEN;
1073 DPRINTK("ata%u port frozen\n", ap->print_id);
1077 * ata_port_freeze - abort & freeze port
1078 * @ap: ATA port to freeze
1080 * Abort and freeze @ap. The freeze operation must be called
1081 * first, because some hardware requires special operations
1082 * before the taskfile registers are accessible.
1085 * spin_lock_irqsave(host lock)
1088 * Number of aborted commands.
1090 int ata_port_freeze(struct ata_port *ap)
1094 WARN_ON(!ap->ops->error_handler);
1096 __ata_port_freeze(ap);
1097 nr_aborted = ata_port_abort(ap);
1101 EXPORT_SYMBOL_GPL(ata_port_freeze);
1104 * ata_eh_freeze_port - EH helper to freeze port
1105 * @ap: ATA port to freeze
1112 void ata_eh_freeze_port(struct ata_port *ap)
1114 unsigned long flags;
1116 if (!ap->ops->error_handler)
1119 spin_lock_irqsave(ap->lock, flags);
1120 __ata_port_freeze(ap);
1121 spin_unlock_irqrestore(ap->lock, flags);
1123 EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
1126 * ata_eh_thaw_port - EH helper to thaw port
1127 * @ap: ATA port to thaw
1129 * Thaw frozen port @ap.
1134 void ata_eh_thaw_port(struct ata_port *ap)
1136 unsigned long flags;
1138 if (!ap->ops->error_handler)
1141 spin_lock_irqsave(ap->lock, flags);
1143 ap->pflags &= ~ATA_PFLAG_FROZEN;
1148 spin_unlock_irqrestore(ap->lock, flags);
1150 DPRINTK("ata%u port thawed\n", ap->print_id);
1153 static void ata_eh_scsidone(struct scsi_cmnd *scmd)
1158 static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
1160 struct ata_port *ap = qc->ap;
1161 struct scsi_cmnd *scmd = qc->scsicmd;
1162 unsigned long flags;
1164 spin_lock_irqsave(ap->lock, flags);
1165 qc->scsidone = ata_eh_scsidone;
1166 __ata_qc_complete(qc);
1167 WARN_ON(ata_tag_valid(qc->tag));
1168 spin_unlock_irqrestore(ap->lock, flags);
1170 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
1174 * ata_eh_qc_complete - Complete an active ATA command from EH
1175 * @qc: Command to complete
1177 * Indicate to the mid and upper layers that an ATA command has
1178 * completed. To be used from EH.
1180 void ata_eh_qc_complete(struct ata_queued_cmd *qc)
1182 struct scsi_cmnd *scmd = qc->scsicmd;
1183 scmd->retries = scmd->allowed;
1184 __ata_eh_qc_complete(qc);
1188 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
1189 * @qc: Command to retry
1191 * Indicate to the mid and upper layers that an ATA command
1192 * should be retried. To be used from EH.
1194 * SCSI midlayer limits the number of retries to scmd->allowed.
1195 * scmd->allowed is incremented for commands which get retried
1196 * due to unrelated failures (qc->err_mask is zero).
1198 void ata_eh_qc_retry(struct ata_queued_cmd *qc)
1200 struct scsi_cmnd *scmd = qc->scsicmd;
1203 __ata_eh_qc_complete(qc);
1207 * ata_dev_disable - disable ATA device
1208 * @dev: ATA device to disable
1215 void ata_dev_disable(struct ata_device *dev)
1217 if (!ata_dev_enabled(dev))
1220 if (ata_msg_drv(dev->link->ap))
1221 ata_dev_warn(dev, "disabled\n");
1222 ata_acpi_on_disable(dev);
1223 ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 | ATA_DNXFER_QUIET);
1226 /* From now till the next successful probe, ering is used to
1227 * track probe failures. Clear accumulated device error info.
1229 ata_ering_clear(&dev->ering);
1231 EXPORT_SYMBOL_GPL(ata_dev_disable);
1234 * ata_eh_detach_dev - detach ATA device
1235 * @dev: ATA device to detach
1242 void ata_eh_detach_dev(struct ata_device *dev)
1244 struct ata_link *link = dev->link;
1245 struct ata_port *ap = link->ap;
1246 struct ata_eh_context *ehc = &link->eh_context;
1247 unsigned long flags;
1249 ata_dev_disable(dev);
1251 spin_lock_irqsave(ap->lock, flags);
1253 dev->flags &= ~ATA_DFLAG_DETACH;
1255 if (ata_scsi_offline_dev(dev)) {
1256 dev->flags |= ATA_DFLAG_DETACHED;
1257 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
1260 /* clear per-dev EH info */
1261 ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
1262 ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
1263 ehc->saved_xfer_mode[dev->devno] = 0;
1264 ehc->saved_ncq_enabled &= ~(1 << dev->devno);
1266 spin_unlock_irqrestore(ap->lock, flags);
1270 * ata_eh_about_to_do - about to perform eh_action
1271 * @link: target ATA link
1272 * @dev: target ATA dev for per-dev action (can be NULL)
1273 * @action: action about to be performed
1275 * Called just before performing EH actions to clear related bits
1276 * in @link->eh_info such that eh actions are not unnecessarily
1282 void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
1283 unsigned int action)
1285 struct ata_port *ap = link->ap;
1286 struct ata_eh_info *ehi = &link->eh_info;
1287 struct ata_eh_context *ehc = &link->eh_context;
1288 unsigned long flags;
1290 spin_lock_irqsave(ap->lock, flags);
1292 ata_eh_clear_action(link, dev, ehi, action);
1294 /* About to take EH action, set RECOVERED. Ignore actions on
1295 * slave links as master will do them again.
1297 if (!(ehc->i.flags & ATA_EHI_QUIET) && link != ap->slave_link)
1298 ap->pflags |= ATA_PFLAG_RECOVERED;
1300 spin_unlock_irqrestore(ap->lock, flags);
1304 * ata_eh_done - EH action complete
1305 * @link: ATA link for which EH actions are complete
1306 * @dev: target ATA dev for per-dev action (can be NULL)
1307 * @action: action just completed
1309 * Called right after performing EH actions to clear related bits
1310 * in @link->eh_context.
1315 void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1316 unsigned int action)
1318 struct ata_eh_context *ehc = &link->eh_context;
1320 ata_eh_clear_action(link, dev, &ehc->i, action);
1324 * ata_err_string - convert err_mask to descriptive string
1325 * @err_mask: error mask to convert to string
1327 * Convert @err_mask to descriptive string. Errors are
1328 * prioritized according to severity and only the most severe
1329 * error is reported.
1335 * Descriptive string for @err_mask
1337 static const char *ata_err_string(unsigned int err_mask)
1339 if (err_mask & AC_ERR_HOST_BUS)
1340 return "host bus error";
1341 if (err_mask & AC_ERR_ATA_BUS)
1342 return "ATA bus error";
1343 if (err_mask & AC_ERR_TIMEOUT)
1345 if (err_mask & AC_ERR_HSM)
1346 return "HSM violation";
1347 if (err_mask & AC_ERR_SYSTEM)
1348 return "internal error";
1349 if (err_mask & AC_ERR_MEDIA)
1350 return "media error";
1351 if (err_mask & AC_ERR_INVALID)
1352 return "invalid argument";
1353 if (err_mask & AC_ERR_DEV)
1354 return "device error";
1355 if (err_mask & AC_ERR_NCQ)
1357 if (err_mask & AC_ERR_NODEV_HINT)
1358 return "Polling detection error";
1359 return "unknown error";
1363 * atapi_eh_tur - perform ATAPI TEST_UNIT_READY
1364 * @dev: target ATAPI device
1365 * @r_sense_key: out parameter for sense_key
1367 * Perform ATAPI TEST_UNIT_READY.
1370 * EH context (may sleep).
1373 * 0 on success, AC_ERR_* mask on failure.
1375 unsigned int atapi_eh_tur(struct ata_device *dev, u8 *r_sense_key)
1377 u8 cdb[ATAPI_CDB_LEN] = { TEST_UNIT_READY, 0, 0, 0, 0, 0 };
1378 struct ata_taskfile tf;
1379 unsigned int err_mask;
1381 ata_tf_init(dev, &tf);
1383 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1384 tf.command = ATA_CMD_PACKET;
1385 tf.protocol = ATAPI_PROT_NODATA;
1387 err_mask = ata_exec_internal(dev, &tf, cdb, DMA_NONE, NULL, 0, 0);
1388 if (err_mask == AC_ERR_DEV)
1389 *r_sense_key = tf.feature >> 4;
1394 * ata_eh_request_sense - perform REQUEST_SENSE_DATA_EXT
1395 * @qc: qc to perform REQUEST_SENSE_SENSE_DATA_EXT to
1396 * @cmd: scsi command for which the sense code should be set
1398 * Perform REQUEST_SENSE_DATA_EXT after the device reported CHECK
1399 * SENSE. This function is an EH helper.
1402 * Kernel thread context (may sleep).
1404 static void ata_eh_request_sense(struct ata_queued_cmd *qc,
1405 struct scsi_cmnd *cmd)
1407 struct ata_device *dev = qc->dev;
1408 struct ata_taskfile tf;
1409 unsigned int err_mask;
1411 if (qc->ap->pflags & ATA_PFLAG_FROZEN) {
1412 ata_dev_warn(dev, "sense data available but port frozen\n");
1416 if (!cmd || qc->flags & ATA_QCFLAG_SENSE_VALID)
1419 if (!ata_id_sense_reporting_enabled(dev->id)) {
1420 ata_dev_warn(qc->dev, "sense data reporting disabled\n");
1424 DPRINTK("ATA request sense\n");
1426 ata_tf_init(dev, &tf);
1427 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1428 tf.flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
1429 tf.command = ATA_CMD_REQ_SENSE_DATA;
1430 tf.protocol = ATA_PROT_NODATA;
1432 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1433 /* Ignore err_mask; ATA_ERR might be set */
1434 if (tf.command & ATA_SENSE) {
1435 ata_scsi_set_sense(dev, cmd, tf.lbah, tf.lbam, tf.lbal);
1436 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1438 ata_dev_warn(dev, "request sense failed stat %02x emask %x\n",
1439 tf.command, err_mask);
1444 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1445 * @dev: device to perform REQUEST_SENSE to
1446 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
1447 * @dfl_sense_key: default sense key to use
1449 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
1450 * SENSE. This function is EH helper.
1453 * Kernel thread context (may sleep).
1456 * 0 on success, AC_ERR_* mask on failure
1458 unsigned int atapi_eh_request_sense(struct ata_device *dev,
1459 u8 *sense_buf, u8 dfl_sense_key)
1461 u8 cdb[ATAPI_CDB_LEN] =
1462 { REQUEST_SENSE, 0, 0, 0, SCSI_SENSE_BUFFERSIZE, 0 };
1463 struct ata_port *ap = dev->link->ap;
1464 struct ata_taskfile tf;
1466 DPRINTK("ATAPI request sense\n");
1468 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1470 /* initialize sense_buf with the error register,
1471 * for the case where they are -not- overwritten
1473 sense_buf[0] = 0x70;
1474 sense_buf[2] = dfl_sense_key;
1476 /* some devices time out if garbage left in tf */
1477 ata_tf_init(dev, &tf);
1479 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1480 tf.command = ATA_CMD_PACKET;
1482 /* is it pointless to prefer PIO for "safety reasons"? */
1483 if (ap->flags & ATA_FLAG_PIO_DMA) {
1484 tf.protocol = ATAPI_PROT_DMA;
1485 tf.feature |= ATAPI_PKT_DMA;
1487 tf.protocol = ATAPI_PROT_PIO;
1488 tf.lbam = SCSI_SENSE_BUFFERSIZE;
1492 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
1493 sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
1497 * ata_eh_analyze_serror - analyze SError for a failed port
1498 * @link: ATA link to analyze SError for
1500 * Analyze SError if available and further determine cause of
1506 static void ata_eh_analyze_serror(struct ata_link *link)
1508 struct ata_eh_context *ehc = &link->eh_context;
1509 u32 serror = ehc->i.serror;
1510 unsigned int err_mask = 0, action = 0;
1513 if (serror & (SERR_PERSISTENT | SERR_DATA)) {
1514 err_mask |= AC_ERR_ATA_BUS;
1515 action |= ATA_EH_RESET;
1517 if (serror & SERR_PROTOCOL) {
1518 err_mask |= AC_ERR_HSM;
1519 action |= ATA_EH_RESET;
1521 if (serror & SERR_INTERNAL) {
1522 err_mask |= AC_ERR_SYSTEM;
1523 action |= ATA_EH_RESET;
1526 /* Determine whether a hotplug event has occurred. Both
1527 * SError.N/X are considered hotplug events for enabled or
1528 * host links. For disabled PMP links, only N bit is
1529 * considered as X bit is left at 1 for link plugging.
1531 if (link->lpm_policy > ATA_LPM_MAX_POWER)
1532 hotplug_mask = 0; /* hotplug doesn't work w/ LPM */
1533 else if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
1534 hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
1536 hotplug_mask = SERR_PHYRDY_CHG;
1538 if (serror & hotplug_mask)
1539 ata_ehi_hotplugged(&ehc->i);
1541 ehc->i.err_mask |= err_mask;
1542 ehc->i.action |= action;
1546 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1547 * @qc: qc to analyze
1548 * @tf: Taskfile registers to analyze
1550 * Analyze taskfile of @qc and further determine cause of
1551 * failure. This function also requests ATAPI sense data if
1555 * Kernel thread context (may sleep).
1558 * Determined recovery action
1560 static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1561 const struct ata_taskfile *tf)
1563 unsigned int tmp, action = 0;
1564 u8 stat = tf->command, err = tf->feature;
1566 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1567 qc->err_mask |= AC_ERR_HSM;
1568 return ATA_EH_RESET;
1571 if (stat & (ATA_ERR | ATA_DF)) {
1572 qc->err_mask |= AC_ERR_DEV;
1574 * Sense data reporting does not work if the
1575 * device fault bit is set.
1583 switch (qc->dev->class) {
1585 if (stat & ATA_SENSE)
1586 ata_eh_request_sense(qc, qc->scsicmd);
1590 qc->err_mask |= AC_ERR_ATA_BUS;
1591 if (err & (ATA_UNC | ATA_AMNF))
1592 qc->err_mask |= AC_ERR_MEDIA;
1594 qc->err_mask |= AC_ERR_INVALID;
1598 if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
1599 tmp = atapi_eh_request_sense(qc->dev,
1600 qc->scsicmd->sense_buffer,
1601 qc->result_tf.feature >> 4);
1603 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1605 qc->err_mask |= tmp;
1609 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1610 enum scsi_disposition ret = scsi_check_sense(qc->scsicmd);
1612 * SUCCESS here means that the sense code could be
1613 * evaluated and should be passed to the upper layers
1614 * for correct evaluation.
1615 * FAILED means the sense code could not be interpreted
1616 * and the device would need to be reset.
1617 * NEEDS_RETRY and ADD_TO_MLQUEUE means that the
1618 * command would need to be retried.
1620 if (ret == NEEDS_RETRY || ret == ADD_TO_MLQUEUE) {
1621 qc->flags |= ATA_QCFLAG_RETRY;
1622 qc->err_mask |= AC_ERR_OTHER;
1623 } else if (ret != SUCCESS) {
1624 qc->err_mask |= AC_ERR_HSM;
1627 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1628 action |= ATA_EH_RESET;
1633 static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask,
1638 if (!(eflags & ATA_EFLAG_DUBIOUS_XFER))
1642 base = ATA_ECAT_DUBIOUS_NONE;
1644 if (err_mask & AC_ERR_ATA_BUS)
1645 return base + ATA_ECAT_ATA_BUS;
1647 if (err_mask & AC_ERR_TIMEOUT)
1648 return base + ATA_ECAT_TOUT_HSM;
1650 if (eflags & ATA_EFLAG_IS_IO) {
1651 if (err_mask & AC_ERR_HSM)
1652 return base + ATA_ECAT_TOUT_HSM;
1654 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1655 return base + ATA_ECAT_UNK_DEV;
1661 struct speed_down_verdict_arg {
1664 int nr_errors[ATA_ECAT_NR];
1667 static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
1669 struct speed_down_verdict_arg *arg = void_arg;
1672 if ((ent->eflags & ATA_EFLAG_OLD_ER) || (ent->timestamp < arg->since))
1675 cat = ata_eh_categorize_error(ent->eflags, ent->err_mask,
1677 arg->nr_errors[cat]++;
1683 * ata_eh_speed_down_verdict - Determine speed down verdict
1684 * @dev: Device of interest
1686 * This function examines error ring of @dev and determines
1687 * whether NCQ needs to be turned off, transfer speed should be
1688 * stepped down, or falling back to PIO is necessary.
1690 * ECAT_ATA_BUS : ATA_BUS error for any command
1692 * ECAT_TOUT_HSM : TIMEOUT for any command or HSM violation for
1695 * ECAT_UNK_DEV : Unknown DEV error for IO commands
1697 * ECAT_DUBIOUS_* : Identical to above three but occurred while
1698 * data transfer hasn't been verified.
1702 * NCQ_OFF : Turn off NCQ.
1704 * SPEED_DOWN : Speed down transfer speed but don't fall back
1707 * FALLBACK_TO_PIO : Fall back to PIO.
1709 * Even if multiple verdicts are returned, only one action is
1710 * taken per error. An action triggered by non-DUBIOUS errors
1711 * clears ering, while one triggered by DUBIOUS_* errors doesn't.
1712 * This is to expedite speed down decisions right after device is
1713 * initially configured.
1715 * The following are speed down rules. #1 and #2 deal with
1718 * 1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors
1719 * occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO.
1721 * 2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors
1722 * occurred during last 5 mins, NCQ_OFF.
1724 * 3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors
1725 * occurred during last 5 mins, FALLBACK_TO_PIO
1727 * 4. If more than 3 TOUT_HSM or UNK_DEV errors occurred
1728 * during last 10 mins, NCQ_OFF.
1730 * 5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6
1731 * UNK_DEV errors occurred during last 10 mins, SPEED_DOWN.
1734 * Inherited from caller.
1737 * OR of ATA_EH_SPDN_* flags.
1739 static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
1741 const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
1742 u64 j64 = get_jiffies_64();
1743 struct speed_down_verdict_arg arg;
1744 unsigned int verdict = 0;
1746 /* scan past 5 mins of error history */
1747 memset(&arg, 0, sizeof(arg));
1748 arg.since = j64 - min(j64, j5mins);
1749 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1751 if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] +
1752 arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1)
1753 verdict |= ATA_EH_SPDN_SPEED_DOWN |
1754 ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS;
1756 if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] +
1757 arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1)
1758 verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS;
1760 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1761 arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1762 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
1763 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
1765 /* scan past 10 mins of error history */
1766 memset(&arg, 0, sizeof(arg));
1767 arg.since = j64 - min(j64, j10mins);
1768 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1770 if (arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1771 arg.nr_errors[ATA_ECAT_UNK_DEV] > 3)
1772 verdict |= ATA_EH_SPDN_NCQ_OFF;
1774 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1775 arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 ||
1776 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
1777 verdict |= ATA_EH_SPDN_SPEED_DOWN;
1783 * ata_eh_speed_down - record error and speed down if necessary
1784 * @dev: Failed device
1785 * @eflags: mask of ATA_EFLAG_* flags
1786 * @err_mask: err_mask of the error
1788 * Record error and examine error history to determine whether
1789 * adjusting transmission speed is necessary. It also sets
1790 * transmission limits appropriately if such adjustment is
1794 * Kernel thread context (may sleep).
1797 * Determined recovery action.
1799 static unsigned int ata_eh_speed_down(struct ata_device *dev,
1800 unsigned int eflags, unsigned int err_mask)
1802 struct ata_link *link = ata_dev_phys_link(dev);
1804 unsigned int verdict;
1805 unsigned int action = 0;
1807 /* don't bother if Cat-0 error */
1808 if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0)
1811 /* record error and determine whether speed down is necessary */
1812 ata_ering_record(&dev->ering, eflags, err_mask);
1813 verdict = ata_eh_speed_down_verdict(dev);
1816 if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
1817 (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
1818 ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
1819 dev->flags |= ATA_DFLAG_NCQ_OFF;
1820 ata_dev_warn(dev, "NCQ disabled due to excessive errors\n");
1825 if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1826 /* speed down SATA link speed if possible */
1827 if (sata_down_spd_limit(link, 0) == 0) {
1828 action |= ATA_EH_RESET;
1832 /* lower transfer mode */
1833 if (dev->spdn_cnt < 2) {
1834 static const int dma_dnxfer_sel[] =
1835 { ATA_DNXFER_DMA, ATA_DNXFER_40C };
1836 static const int pio_dnxfer_sel[] =
1837 { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
1840 if (dev->xfer_shift != ATA_SHIFT_PIO)
1841 sel = dma_dnxfer_sel[dev->spdn_cnt];
1843 sel = pio_dnxfer_sel[dev->spdn_cnt];
1847 if (ata_down_xfermask_limit(dev, sel) == 0) {
1848 action |= ATA_EH_RESET;
1854 /* Fall back to PIO? Slowing down to PIO is meaningless for
1855 * SATA ATA devices. Consider it only for PATA and SATAPI.
1857 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
1858 (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) &&
1859 (dev->xfer_shift != ATA_SHIFT_PIO)) {
1860 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
1862 action |= ATA_EH_RESET;
1869 /* device has been slowed down, blow error history */
1870 if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS))
1871 ata_ering_clear(&dev->ering);
1876 * ata_eh_worth_retry - analyze error and decide whether to retry
1877 * @qc: qc to possibly retry
1879 * Look at the cause of the error and decide if a retry
1880 * might be useful or not. We don't want to retry media errors
1881 * because the drive itself has probably already taken 10-30 seconds
1882 * doing its own internal retries before reporting the failure.
1884 static inline int ata_eh_worth_retry(struct ata_queued_cmd *qc)
1886 if (qc->err_mask & AC_ERR_MEDIA)
1887 return 0; /* don't retry media errors */
1888 if (qc->flags & ATA_QCFLAG_IO)
1889 return 1; /* otherwise retry anything from fs stack */
1890 if (qc->err_mask & AC_ERR_INVALID)
1891 return 0; /* don't retry these */
1892 return qc->err_mask != AC_ERR_DEV; /* retry if not dev error */
1896 * ata_eh_quiet - check if we need to be quiet about a command error
1899 * Look at the qc flags anbd its scsi command request flags to determine
1900 * if we need to be quiet about the command failure.
1902 static inline bool ata_eh_quiet(struct ata_queued_cmd *qc)
1904 if (qc->scsicmd && scsi_cmd_to_rq(qc->scsicmd)->rq_flags & RQF_QUIET)
1905 qc->flags |= ATA_QCFLAG_QUIET;
1906 return qc->flags & ATA_QCFLAG_QUIET;
1910 * ata_eh_link_autopsy - analyze error and determine recovery action
1911 * @link: host link to perform autopsy on
1913 * Analyze why @link failed and determine which recovery actions
1914 * are needed. This function also sets more detailed AC_ERR_*
1915 * values and fills sense data for ATAPI CHECK SENSE.
1918 * Kernel thread context (may sleep).
1920 static void ata_eh_link_autopsy(struct ata_link *link)
1922 struct ata_port *ap = link->ap;
1923 struct ata_eh_context *ehc = &link->eh_context;
1924 struct ata_queued_cmd *qc;
1925 struct ata_device *dev;
1926 unsigned int all_err_mask = 0, eflags = 0;
1927 int tag, nr_failed = 0, nr_quiet = 0;
1933 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1936 /* obtain and analyze SError */
1937 rc = sata_scr_read(link, SCR_ERROR, &serror);
1939 ehc->i.serror |= serror;
1940 ata_eh_analyze_serror(link);
1941 } else if (rc != -EOPNOTSUPP) {
1942 /* SError read failed, force reset and probing */
1943 ehc->i.probe_mask |= ATA_ALL_DEVICES;
1944 ehc->i.action |= ATA_EH_RESET;
1945 ehc->i.err_mask |= AC_ERR_OTHER;
1948 /* analyze NCQ failure */
1949 ata_eh_analyze_ncq_error(link);
1951 /* any real error trumps AC_ERR_OTHER */
1952 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1953 ehc->i.err_mask &= ~AC_ERR_OTHER;
1955 all_err_mask |= ehc->i.err_mask;
1957 ata_qc_for_each_raw(ap, qc, tag) {
1958 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
1959 ata_dev_phys_link(qc->dev) != link)
1962 /* inherit upper level err_mask */
1963 qc->err_mask |= ehc->i.err_mask;
1966 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1968 /* DEV errors are probably spurious in case of ATA_BUS error */
1969 if (qc->err_mask & AC_ERR_ATA_BUS)
1970 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1973 /* any real error trumps unknown error */
1974 if (qc->err_mask & ~AC_ERR_OTHER)
1975 qc->err_mask &= ~AC_ERR_OTHER;
1978 * SENSE_VALID trumps dev/unknown error and revalidation. Upper
1979 * layers will determine whether the command is worth retrying
1980 * based on the sense data and device class/type. Otherwise,
1981 * determine directly if the command is worth retrying using its
1982 * error mask and flags.
1984 if (qc->flags & ATA_QCFLAG_SENSE_VALID)
1985 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1986 else if (ata_eh_worth_retry(qc))
1987 qc->flags |= ATA_QCFLAG_RETRY;
1989 /* accumulate error info */
1990 ehc->i.dev = qc->dev;
1991 all_err_mask |= qc->err_mask;
1992 if (qc->flags & ATA_QCFLAG_IO)
1993 eflags |= ATA_EFLAG_IS_IO;
1994 trace_ata_eh_link_autopsy_qc(qc);
1996 /* Count quiet errors */
1997 if (ata_eh_quiet(qc))
2002 /* If all failed commands requested silence, then be quiet */
2003 if (nr_quiet == nr_failed)
2004 ehc->i.flags |= ATA_EHI_QUIET;
2006 /* enforce default EH actions */
2007 if (ap->pflags & ATA_PFLAG_FROZEN ||
2008 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
2009 ehc->i.action |= ATA_EH_RESET;
2010 else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) ||
2011 (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV)))
2012 ehc->i.action |= ATA_EH_REVALIDATE;
2014 /* If we have offending qcs and the associated failed device,
2015 * perform per-dev EH action only on the offending device.
2018 ehc->i.dev_action[ehc->i.dev->devno] |=
2019 ehc->i.action & ATA_EH_PERDEV_MASK;
2020 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
2023 /* propagate timeout to host link */
2024 if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link))
2025 ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT;
2027 /* record error and consider speeding down */
2029 if (!dev && ((ata_link_max_devices(link) == 1 &&
2030 ata_dev_enabled(link->device))))
2034 if (dev->flags & ATA_DFLAG_DUBIOUS_XFER)
2035 eflags |= ATA_EFLAG_DUBIOUS_XFER;
2036 ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask);
2037 trace_ata_eh_link_autopsy(dev, ehc->i.action, all_err_mask);
2043 * ata_eh_autopsy - analyze error and determine recovery action
2044 * @ap: host port to perform autopsy on
2046 * Analyze all links of @ap and determine why they failed and
2047 * which recovery actions are needed.
2050 * Kernel thread context (may sleep).
2052 void ata_eh_autopsy(struct ata_port *ap)
2054 struct ata_link *link;
2056 ata_for_each_link(link, ap, EDGE)
2057 ata_eh_link_autopsy(link);
2059 /* Handle the frigging slave link. Autopsy is done similarly
2060 * but actions and flags are transferred over to the master
2061 * link and handled from there.
2063 if (ap->slave_link) {
2064 struct ata_eh_context *mehc = &ap->link.eh_context;
2065 struct ata_eh_context *sehc = &ap->slave_link->eh_context;
2067 /* transfer control flags from master to slave */
2068 sehc->i.flags |= mehc->i.flags & ATA_EHI_TO_SLAVE_MASK;
2070 /* perform autopsy on the slave link */
2071 ata_eh_link_autopsy(ap->slave_link);
2073 /* transfer actions from slave to master and clear slave */
2074 ata_eh_about_to_do(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
2075 mehc->i.action |= sehc->i.action;
2076 mehc->i.dev_action[1] |= sehc->i.dev_action[1];
2077 mehc->i.flags |= sehc->i.flags;
2078 ata_eh_done(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
2081 /* Autopsy of fanout ports can affect host link autopsy.
2082 * Perform host link autopsy last.
2084 if (sata_pmp_attached(ap))
2085 ata_eh_link_autopsy(&ap->link);
2089 * ata_get_cmd_descript - get description for ATA command
2090 * @command: ATA command code to get description for
2092 * Return a textual description of the given command, or NULL if the
2093 * command is not known.
2098 const char *ata_get_cmd_descript(u8 command)
2100 #ifdef CONFIG_ATA_VERBOSE_ERROR
2106 { ATA_CMD_DEV_RESET, "DEVICE RESET" },
2107 { ATA_CMD_CHK_POWER, "CHECK POWER MODE" },
2108 { ATA_CMD_STANDBY, "STANDBY" },
2109 { ATA_CMD_IDLE, "IDLE" },
2110 { ATA_CMD_EDD, "EXECUTE DEVICE DIAGNOSTIC" },
2111 { ATA_CMD_DOWNLOAD_MICRO, "DOWNLOAD MICROCODE" },
2112 { ATA_CMD_DOWNLOAD_MICRO_DMA, "DOWNLOAD MICROCODE DMA" },
2113 { ATA_CMD_NOP, "NOP" },
2114 { ATA_CMD_FLUSH, "FLUSH CACHE" },
2115 { ATA_CMD_FLUSH_EXT, "FLUSH CACHE EXT" },
2116 { ATA_CMD_ID_ATA, "IDENTIFY DEVICE" },
2117 { ATA_CMD_ID_ATAPI, "IDENTIFY PACKET DEVICE" },
2118 { ATA_CMD_SERVICE, "SERVICE" },
2119 { ATA_CMD_READ, "READ DMA" },
2120 { ATA_CMD_READ_EXT, "READ DMA EXT" },
2121 { ATA_CMD_READ_QUEUED, "READ DMA QUEUED" },
2122 { ATA_CMD_READ_STREAM_EXT, "READ STREAM EXT" },
2123 { ATA_CMD_READ_STREAM_DMA_EXT, "READ STREAM DMA EXT" },
2124 { ATA_CMD_WRITE, "WRITE DMA" },
2125 { ATA_CMD_WRITE_EXT, "WRITE DMA EXT" },
2126 { ATA_CMD_WRITE_QUEUED, "WRITE DMA QUEUED EXT" },
2127 { ATA_CMD_WRITE_STREAM_EXT, "WRITE STREAM EXT" },
2128 { ATA_CMD_WRITE_STREAM_DMA_EXT, "WRITE STREAM DMA EXT" },
2129 { ATA_CMD_WRITE_FUA_EXT, "WRITE DMA FUA EXT" },
2130 { ATA_CMD_WRITE_QUEUED_FUA_EXT, "WRITE DMA QUEUED FUA EXT" },
2131 { ATA_CMD_FPDMA_READ, "READ FPDMA QUEUED" },
2132 { ATA_CMD_FPDMA_WRITE, "WRITE FPDMA QUEUED" },
2133 { ATA_CMD_FPDMA_SEND, "SEND FPDMA QUEUED" },
2134 { ATA_CMD_FPDMA_RECV, "RECEIVE FPDMA QUEUED" },
2135 { ATA_CMD_PIO_READ, "READ SECTOR(S)" },
2136 { ATA_CMD_PIO_READ_EXT, "READ SECTOR(S) EXT" },
2137 { ATA_CMD_PIO_WRITE, "WRITE SECTOR(S)" },
2138 { ATA_CMD_PIO_WRITE_EXT, "WRITE SECTOR(S) EXT" },
2139 { ATA_CMD_READ_MULTI, "READ MULTIPLE" },
2140 { ATA_CMD_READ_MULTI_EXT, "READ MULTIPLE EXT" },
2141 { ATA_CMD_WRITE_MULTI, "WRITE MULTIPLE" },
2142 { ATA_CMD_WRITE_MULTI_EXT, "WRITE MULTIPLE EXT" },
2143 { ATA_CMD_WRITE_MULTI_FUA_EXT, "WRITE MULTIPLE FUA EXT" },
2144 { ATA_CMD_SET_FEATURES, "SET FEATURES" },
2145 { ATA_CMD_SET_MULTI, "SET MULTIPLE MODE" },
2146 { ATA_CMD_VERIFY, "READ VERIFY SECTOR(S)" },
2147 { ATA_CMD_VERIFY_EXT, "READ VERIFY SECTOR(S) EXT" },
2148 { ATA_CMD_WRITE_UNCORR_EXT, "WRITE UNCORRECTABLE EXT" },
2149 { ATA_CMD_STANDBYNOW1, "STANDBY IMMEDIATE" },
2150 { ATA_CMD_IDLEIMMEDIATE, "IDLE IMMEDIATE" },
2151 { ATA_CMD_SLEEP, "SLEEP" },
2152 { ATA_CMD_INIT_DEV_PARAMS, "INITIALIZE DEVICE PARAMETERS" },
2153 { ATA_CMD_READ_NATIVE_MAX, "READ NATIVE MAX ADDRESS" },
2154 { ATA_CMD_READ_NATIVE_MAX_EXT, "READ NATIVE MAX ADDRESS EXT" },
2155 { ATA_CMD_SET_MAX, "SET MAX ADDRESS" },
2156 { ATA_CMD_SET_MAX_EXT, "SET MAX ADDRESS EXT" },
2157 { ATA_CMD_READ_LOG_EXT, "READ LOG EXT" },
2158 { ATA_CMD_WRITE_LOG_EXT, "WRITE LOG EXT" },
2159 { ATA_CMD_READ_LOG_DMA_EXT, "READ LOG DMA EXT" },
2160 { ATA_CMD_WRITE_LOG_DMA_EXT, "WRITE LOG DMA EXT" },
2161 { ATA_CMD_TRUSTED_NONDATA, "TRUSTED NON-DATA" },
2162 { ATA_CMD_TRUSTED_RCV, "TRUSTED RECEIVE" },
2163 { ATA_CMD_TRUSTED_RCV_DMA, "TRUSTED RECEIVE DMA" },
2164 { ATA_CMD_TRUSTED_SND, "TRUSTED SEND" },
2165 { ATA_CMD_TRUSTED_SND_DMA, "TRUSTED SEND DMA" },
2166 { ATA_CMD_PMP_READ, "READ BUFFER" },
2167 { ATA_CMD_PMP_READ_DMA, "READ BUFFER DMA" },
2168 { ATA_CMD_PMP_WRITE, "WRITE BUFFER" },
2169 { ATA_CMD_PMP_WRITE_DMA, "WRITE BUFFER DMA" },
2170 { ATA_CMD_CONF_OVERLAY, "DEVICE CONFIGURATION OVERLAY" },
2171 { ATA_CMD_SEC_SET_PASS, "SECURITY SET PASSWORD" },
2172 { ATA_CMD_SEC_UNLOCK, "SECURITY UNLOCK" },
2173 { ATA_CMD_SEC_ERASE_PREP, "SECURITY ERASE PREPARE" },
2174 { ATA_CMD_SEC_ERASE_UNIT, "SECURITY ERASE UNIT" },
2175 { ATA_CMD_SEC_FREEZE_LOCK, "SECURITY FREEZE LOCK" },
2176 { ATA_CMD_SEC_DISABLE_PASS, "SECURITY DISABLE PASSWORD" },
2177 { ATA_CMD_CONFIG_STREAM, "CONFIGURE STREAM" },
2178 { ATA_CMD_SMART, "SMART" },
2179 { ATA_CMD_MEDIA_LOCK, "DOOR LOCK" },
2180 { ATA_CMD_MEDIA_UNLOCK, "DOOR UNLOCK" },
2181 { ATA_CMD_DSM, "DATA SET MANAGEMENT" },
2182 { ATA_CMD_CHK_MED_CRD_TYP, "CHECK MEDIA CARD TYPE" },
2183 { ATA_CMD_CFA_REQ_EXT_ERR, "CFA REQUEST EXTENDED ERROR" },
2184 { ATA_CMD_CFA_WRITE_NE, "CFA WRITE SECTORS WITHOUT ERASE" },
2185 { ATA_CMD_CFA_TRANS_SECT, "CFA TRANSLATE SECTOR" },
2186 { ATA_CMD_CFA_ERASE, "CFA ERASE SECTORS" },
2187 { ATA_CMD_CFA_WRITE_MULT_NE, "CFA WRITE MULTIPLE WITHOUT ERASE" },
2188 { ATA_CMD_REQ_SENSE_DATA, "REQUEST SENSE DATA EXT" },
2189 { ATA_CMD_SANITIZE_DEVICE, "SANITIZE DEVICE" },
2190 { ATA_CMD_ZAC_MGMT_IN, "ZAC MANAGEMENT IN" },
2191 { ATA_CMD_ZAC_MGMT_OUT, "ZAC MANAGEMENT OUT" },
2192 { ATA_CMD_READ_LONG, "READ LONG (with retries)" },
2193 { ATA_CMD_READ_LONG_ONCE, "READ LONG (without retries)" },
2194 { ATA_CMD_WRITE_LONG, "WRITE LONG (with retries)" },
2195 { ATA_CMD_WRITE_LONG_ONCE, "WRITE LONG (without retries)" },
2196 { ATA_CMD_RESTORE, "RECALIBRATE" },
2197 { 0, NULL } /* terminate list */
2201 for (i = 0; cmd_descr[i].text; i++)
2202 if (cmd_descr[i].command == command)
2203 return cmd_descr[i].text;
2208 EXPORT_SYMBOL_GPL(ata_get_cmd_descript);
2211 * ata_eh_link_report - report error handling to user
2212 * @link: ATA link EH is going on
2214 * Report EH to user.
2219 static void ata_eh_link_report(struct ata_link *link)
2221 struct ata_port *ap = link->ap;
2222 struct ata_eh_context *ehc = &link->eh_context;
2223 struct ata_queued_cmd *qc;
2224 const char *frozen, *desc;
2225 char tries_buf[6] = "";
2226 int tag, nr_failed = 0;
2228 if (ehc->i.flags & ATA_EHI_QUIET)
2232 if (ehc->i.desc[0] != '\0')
2235 ata_qc_for_each_raw(ap, qc, tag) {
2236 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
2237 ata_dev_phys_link(qc->dev) != link ||
2238 ((qc->flags & ATA_QCFLAG_QUIET) &&
2239 qc->err_mask == AC_ERR_DEV))
2241 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
2247 if (!nr_failed && !ehc->i.err_mask)
2251 if (ap->pflags & ATA_PFLAG_FROZEN)
2254 if (ap->eh_tries < ATA_EH_MAX_TRIES)
2255 snprintf(tries_buf, sizeof(tries_buf), " t%d",
2259 ata_dev_err(ehc->i.dev, "exception Emask 0x%x "
2260 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2261 ehc->i.err_mask, link->sactive, ehc->i.serror,
2262 ehc->i.action, frozen, tries_buf);
2264 ata_dev_err(ehc->i.dev, "%s\n", desc);
2266 ata_link_err(link, "exception Emask 0x%x "
2267 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2268 ehc->i.err_mask, link->sactive, ehc->i.serror,
2269 ehc->i.action, frozen, tries_buf);
2271 ata_link_err(link, "%s\n", desc);
2274 #ifdef CONFIG_ATA_VERBOSE_ERROR
2277 "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
2278 ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
2279 ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
2280 ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
2281 ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
2282 ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
2283 ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
2284 ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
2285 ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
2286 ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
2287 ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
2288 ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
2289 ehc->i.serror & SERR_CRC ? "BadCRC " : "",
2290 ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
2291 ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
2292 ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
2293 ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
2294 ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
2297 ata_qc_for_each_raw(ap, qc, tag) {
2298 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
2299 char data_buf[20] = "";
2300 char cdb_buf[70] = "";
2302 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
2303 ata_dev_phys_link(qc->dev) != link || !qc->err_mask)
2306 if (qc->dma_dir != DMA_NONE) {
2307 static const char *dma_str[] = {
2308 [DMA_BIDIRECTIONAL] = "bidi",
2309 [DMA_TO_DEVICE] = "out",
2310 [DMA_FROM_DEVICE] = "in",
2312 const char *prot_str = NULL;
2314 switch (qc->tf.protocol) {
2315 case ATA_PROT_UNKNOWN:
2316 prot_str = "unknown";
2318 case ATA_PROT_NODATA:
2319 prot_str = "nodata";
2328 prot_str = "ncq dma";
2330 case ATA_PROT_NCQ_NODATA:
2331 prot_str = "ncq nodata";
2333 case ATAPI_PROT_NODATA:
2334 prot_str = "nodata";
2336 case ATAPI_PROT_PIO:
2339 case ATAPI_PROT_DMA:
2343 snprintf(data_buf, sizeof(data_buf), " %s %u %s",
2344 prot_str, qc->nbytes, dma_str[qc->dma_dir]);
2347 if (ata_is_atapi(qc->tf.protocol)) {
2348 const u8 *cdb = qc->cdb;
2349 size_t cdb_len = qc->dev->cdb_len;
2352 cdb = qc->scsicmd->cmnd;
2353 cdb_len = qc->scsicmd->cmd_len;
2355 __scsi_format_command(cdb_buf, sizeof(cdb_buf),
2358 const char *descr = ata_get_cmd_descript(cmd->command);
2360 ata_dev_err(qc->dev, "failed command: %s\n",
2364 ata_dev_err(qc->dev,
2365 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
2367 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
2368 "Emask 0x%x (%s)%s\n",
2369 cmd->command, cmd->feature, cmd->nsect,
2370 cmd->lbal, cmd->lbam, cmd->lbah,
2371 cmd->hob_feature, cmd->hob_nsect,
2372 cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
2373 cmd->device, qc->tag, data_buf, cdb_buf,
2374 res->command, res->feature, res->nsect,
2375 res->lbal, res->lbam, res->lbah,
2376 res->hob_feature, res->hob_nsect,
2377 res->hob_lbal, res->hob_lbam, res->hob_lbah,
2378 res->device, qc->err_mask, ata_err_string(qc->err_mask),
2379 qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
2381 #ifdef CONFIG_ATA_VERBOSE_ERROR
2382 if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
2383 ATA_SENSE | ATA_ERR)) {
2384 if (res->command & ATA_BUSY)
2385 ata_dev_err(qc->dev, "status: { Busy }\n");
2387 ata_dev_err(qc->dev, "status: { %s%s%s%s%s}\n",
2388 res->command & ATA_DRDY ? "DRDY " : "",
2389 res->command & ATA_DF ? "DF " : "",
2390 res->command & ATA_DRQ ? "DRQ " : "",
2391 res->command & ATA_SENSE ? "SENSE " : "",
2392 res->command & ATA_ERR ? "ERR " : "");
2395 if (cmd->command != ATA_CMD_PACKET &&
2396 (res->feature & (ATA_ICRC | ATA_UNC | ATA_AMNF |
2397 ATA_IDNF | ATA_ABORTED)))
2398 ata_dev_err(qc->dev, "error: { %s%s%s%s%s}\n",
2399 res->feature & ATA_ICRC ? "ICRC " : "",
2400 res->feature & ATA_UNC ? "UNC " : "",
2401 res->feature & ATA_AMNF ? "AMNF " : "",
2402 res->feature & ATA_IDNF ? "IDNF " : "",
2403 res->feature & ATA_ABORTED ? "ABRT " : "");
2409 * ata_eh_report - report error handling to user
2410 * @ap: ATA port to report EH about
2412 * Report EH to user.
2417 void ata_eh_report(struct ata_port *ap)
2419 struct ata_link *link;
2421 ata_for_each_link(link, ap, HOST_FIRST)
2422 ata_eh_link_report(link);
2425 static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
2426 unsigned int *classes, unsigned long deadline,
2429 struct ata_device *dev;
2432 ata_for_each_dev(dev, link, ALL)
2433 classes[dev->devno] = ATA_DEV_UNKNOWN;
2435 return reset(link, classes, deadline);
2438 static int ata_eh_followup_srst_needed(struct ata_link *link, int rc)
2440 if ((link->flags & ATA_LFLAG_NO_SRST) || ata_link_offline(link))
2444 if (sata_pmp_supported(link->ap) && ata_is_host_link(link))
2449 int ata_eh_reset(struct ata_link *link, int classify,
2450 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
2451 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
2453 struct ata_port *ap = link->ap;
2454 struct ata_link *slave = ap->slave_link;
2455 struct ata_eh_context *ehc = &link->eh_context;
2456 struct ata_eh_context *sehc = slave ? &slave->eh_context : NULL;
2457 unsigned int *classes = ehc->classes;
2458 unsigned int lflags = link->flags;
2459 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
2460 int max_tries = 0, try = 0;
2461 struct ata_link *failed_link;
2462 struct ata_device *dev;
2463 unsigned long deadline, now;
2464 ata_reset_fn_t reset;
2465 unsigned long flags;
2472 while (ata_eh_reset_timeouts[max_tries] != ULONG_MAX)
2474 if (link->flags & ATA_LFLAG_RST_ONCE)
2476 if (link->flags & ATA_LFLAG_NO_HRST)
2478 if (link->flags & ATA_LFLAG_NO_SRST)
2481 /* make sure each reset attempt is at least COOL_DOWN apart */
2482 if (ehc->i.flags & ATA_EHI_DID_RESET) {
2484 WARN_ON(time_after(ehc->last_reset, now));
2485 deadline = ata_deadline(ehc->last_reset,
2486 ATA_EH_RESET_COOL_DOWN);
2487 if (time_before(now, deadline))
2488 schedule_timeout_uninterruptible(deadline - now);
2491 spin_lock_irqsave(ap->lock, flags);
2492 ap->pflags |= ATA_PFLAG_RESETTING;
2493 spin_unlock_irqrestore(ap->lock, flags);
2495 ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
2497 ata_for_each_dev(dev, link, ALL) {
2498 /* If we issue an SRST then an ATA drive (not ATAPI)
2499 * may change configuration and be in PIO0 timing. If
2500 * we do a hard reset (or are coming from power on)
2501 * this is true for ATA or ATAPI. Until we've set a
2502 * suitable controller mode we should not touch the
2503 * bus as we may be talking too fast.
2505 dev->pio_mode = XFER_PIO_0;
2506 dev->dma_mode = 0xff;
2508 /* If the controller has a pio mode setup function
2509 * then use it to set the chipset to rights. Don't
2510 * touch the DMA setup as that will be dealt with when
2511 * configuring devices.
2513 if (ap->ops->set_piomode)
2514 ap->ops->set_piomode(ap, dev);
2517 /* prefer hardreset */
2519 ehc->i.action &= ~ATA_EH_RESET;
2522 ehc->i.action |= ATA_EH_HARDRESET;
2523 } else if (softreset) {
2525 ehc->i.action |= ATA_EH_SOFTRESET;
2529 unsigned long deadline = ata_deadline(jiffies,
2530 ATA_EH_PRERESET_TIMEOUT);
2533 sehc->i.action &= ~ATA_EH_RESET;
2534 sehc->i.action |= ehc->i.action;
2537 rc = prereset(link, deadline);
2539 /* If present, do prereset on slave link too. Reset
2540 * is skipped iff both master and slave links report
2541 * -ENOENT or clear ATA_EH_RESET.
2543 if (slave && (rc == 0 || rc == -ENOENT)) {
2546 tmp = prereset(slave, deadline);
2550 ehc->i.action |= sehc->i.action;
2554 if (rc == -ENOENT) {
2555 ata_link_dbg(link, "port disabled--ignoring\n");
2556 ehc->i.action &= ~ATA_EH_RESET;
2558 ata_for_each_dev(dev, link, ALL)
2559 classes[dev->devno] = ATA_DEV_NONE;
2564 "prereset failed (errno=%d)\n",
2569 /* prereset() might have cleared ATA_EH_RESET. If so,
2570 * bang classes, thaw and return.
2572 if (reset && !(ehc->i.action & ATA_EH_RESET)) {
2573 ata_for_each_dev(dev, link, ALL)
2574 classes[dev->devno] = ATA_DEV_NONE;
2575 if ((ap->pflags & ATA_PFLAG_FROZEN) &&
2576 ata_is_host_link(link))
2577 ata_eh_thaw_port(ap);
2587 if (ata_is_host_link(link))
2588 ata_eh_freeze_port(ap);
2590 deadline = ata_deadline(jiffies, ata_eh_reset_timeouts[try++]);
2594 ata_link_info(link, "%s resetting link\n",
2595 reset == softreset ? "soft" : "hard");
2597 /* mark that this EH session started with reset */
2598 ehc->last_reset = jiffies;
2599 if (reset == hardreset)
2600 ehc->i.flags |= ATA_EHI_DID_HARDRESET;
2602 ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
2604 rc = ata_do_reset(link, reset, classes, deadline, true);
2605 if (rc && rc != -EAGAIN) {
2610 /* hardreset slave link if existent */
2611 if (slave && reset == hardreset) {
2615 ata_link_info(slave, "hard resetting link\n");
2617 ata_eh_about_to_do(slave, NULL, ATA_EH_RESET);
2618 tmp = ata_do_reset(slave, reset, classes, deadline,
2627 failed_link = slave;
2633 /* perform follow-up SRST if necessary */
2634 if (reset == hardreset &&
2635 ata_eh_followup_srst_needed(link, rc)) {
2640 "follow-up softreset required but no softreset available\n");
2646 ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
2647 rc = ata_do_reset(link, reset, classes, deadline, true);
2656 "no reset method available, skipping reset\n");
2657 if (!(lflags & ATA_LFLAG_ASSUME_CLASS))
2658 lflags |= ATA_LFLAG_ASSUME_ATA;
2662 * Post-reset processing
2664 ata_for_each_dev(dev, link, ALL) {
2665 /* After the reset, the device state is PIO 0 and the
2666 * controller state is undefined. Reset also wakes up
2667 * drives from sleeping mode.
2669 dev->pio_mode = XFER_PIO_0;
2670 dev->flags &= ~ATA_DFLAG_SLEEPING;
2672 if (ata_phys_link_offline(ata_dev_phys_link(dev)))
2675 /* apply class override */
2676 if (lflags & ATA_LFLAG_ASSUME_ATA)
2677 classes[dev->devno] = ATA_DEV_ATA;
2678 else if (lflags & ATA_LFLAG_ASSUME_SEMB)
2679 classes[dev->devno] = ATA_DEV_SEMB_UNSUP;
2682 /* record current link speed */
2683 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2684 link->sata_spd = (sstatus >> 4) & 0xf;
2685 if (slave && sata_scr_read(slave, SCR_STATUS, &sstatus) == 0)
2686 slave->sata_spd = (sstatus >> 4) & 0xf;
2689 if (ata_is_host_link(link))
2690 ata_eh_thaw_port(ap);
2692 /* postreset() should clear hardware SError. Although SError
2693 * is cleared during link resume, clearing SError here is
2694 * necessary as some PHYs raise hotplug events after SRST.
2695 * This introduces race condition where hotplug occurs between
2696 * reset and here. This race is mediated by cross checking
2697 * link onlineness and classification result later.
2700 postreset(link, classes);
2702 postreset(slave, classes);
2706 * Some controllers can't be frozen very well and may set spurious
2707 * error conditions during reset. Clear accumulated error
2708 * information and re-thaw the port if frozen. As reset is the
2709 * final recovery action and we cross check link onlineness against
2710 * device classification later, no hotplug event is lost by this.
2712 spin_lock_irqsave(link->ap->lock, flags);
2713 memset(&link->eh_info, 0, sizeof(link->eh_info));
2715 memset(&slave->eh_info, 0, sizeof(link->eh_info));
2716 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
2717 spin_unlock_irqrestore(link->ap->lock, flags);
2719 if (ap->pflags & ATA_PFLAG_FROZEN)
2720 ata_eh_thaw_port(ap);
2723 * Make sure onlineness and classification result correspond.
2724 * Hotplug could have happened during reset and some
2725 * controllers fail to wait while a drive is spinning up after
2726 * being hotplugged causing misdetection. By cross checking
2727 * link on/offlineness and classification result, those
2728 * conditions can be reliably detected and retried.
2731 ata_for_each_dev(dev, link, ALL) {
2732 if (ata_phys_link_online(ata_dev_phys_link(dev))) {
2733 if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
2734 ata_dev_dbg(dev, "link online but device misclassified\n");
2735 classes[dev->devno] = ATA_DEV_NONE;
2738 } else if (ata_phys_link_offline(ata_dev_phys_link(dev))) {
2739 if (ata_class_enabled(classes[dev->devno]))
2741 "link offline, clearing class %d to NONE\n",
2742 classes[dev->devno]);
2743 classes[dev->devno] = ATA_DEV_NONE;
2744 } else if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
2746 "link status unknown, clearing UNKNOWN to NONE\n");
2747 classes[dev->devno] = ATA_DEV_NONE;
2751 if (classify && nr_unknown) {
2752 if (try < max_tries) {
2754 "link online but %d devices misclassified, retrying\n",
2761 "link online but %d devices misclassified, "
2762 "device detection might fail\n", nr_unknown);
2765 /* reset successful, schedule revalidation */
2766 ata_eh_done(link, NULL, ATA_EH_RESET);
2768 ata_eh_done(slave, NULL, ATA_EH_RESET);
2769 ehc->last_reset = jiffies; /* update to completion time */
2770 ehc->i.action |= ATA_EH_REVALIDATE;
2771 link->lpm_policy = ATA_LPM_UNKNOWN; /* reset LPM state */
2775 /* clear hotplug flag */
2776 ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2778 sehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2780 spin_lock_irqsave(ap->lock, flags);
2781 ap->pflags &= ~ATA_PFLAG_RESETTING;
2782 spin_unlock_irqrestore(ap->lock, flags);
2787 /* if SCR isn't accessible on a fan-out port, PMP needs to be reset */
2788 if (!ata_is_host_link(link) &&
2789 sata_scr_read(link, SCR_STATUS, &sstatus))
2792 if (try >= max_tries) {
2794 * Thaw host port even if reset failed, so that the port
2795 * can be retried on the next phy event. This risks
2796 * repeated EH runs but seems to be a better tradeoff than
2797 * shutting down a port after a botched hotplug attempt.
2799 if (ata_is_host_link(link))
2800 ata_eh_thaw_port(ap);
2805 if (time_before(now, deadline)) {
2806 unsigned long delta = deadline - now;
2808 ata_link_warn(failed_link,
2809 "reset failed (errno=%d), retrying in %u secs\n",
2810 rc, DIV_ROUND_UP(jiffies_to_msecs(delta), 1000));
2814 delta = schedule_timeout_uninterruptible(delta);
2819 * While disks spinup behind PMP, some controllers fail sending SRST.
2820 * They need to be reset - as well as the PMP - before retrying.
2822 if (rc == -ERESTART) {
2823 if (ata_is_host_link(link))
2824 ata_eh_thaw_port(ap);
2828 if (try == max_tries - 1) {
2829 sata_down_spd_limit(link, 0);
2831 sata_down_spd_limit(slave, 0);
2832 } else if (rc == -EPIPE)
2833 sata_down_spd_limit(failed_link, 0);
2840 static inline void ata_eh_pull_park_action(struct ata_port *ap)
2842 struct ata_link *link;
2843 struct ata_device *dev;
2844 unsigned long flags;
2847 * This function can be thought of as an extended version of
2848 * ata_eh_about_to_do() specially crafted to accommodate the
2849 * requirements of ATA_EH_PARK handling. Since the EH thread
2850 * does not leave the do {} while () loop in ata_eh_recover as
2851 * long as the timeout for a park request to *one* device on
2852 * the port has not expired, and since we still want to pick
2853 * up park requests to other devices on the same port or
2854 * timeout updates for the same device, we have to pull
2855 * ATA_EH_PARK actions from eh_info into eh_context.i
2856 * ourselves at the beginning of each pass over the loop.
2858 * Additionally, all write accesses to &ap->park_req_pending
2859 * through reinit_completion() (see below) or complete_all()
2860 * (see ata_scsi_park_store()) are protected by the host lock.
2861 * As a result we have that park_req_pending.done is zero on
2862 * exit from this function, i.e. when ATA_EH_PARK actions for
2863 * *all* devices on port ap have been pulled into the
2864 * respective eh_context structs. If, and only if,
2865 * park_req_pending.done is non-zero by the time we reach
2866 * wait_for_completion_timeout(), another ATA_EH_PARK action
2867 * has been scheduled for at least one of the devices on port
2868 * ap and we have to cycle over the do {} while () loop in
2869 * ata_eh_recover() again.
2872 spin_lock_irqsave(ap->lock, flags);
2873 reinit_completion(&ap->park_req_pending);
2874 ata_for_each_link(link, ap, EDGE) {
2875 ata_for_each_dev(dev, link, ALL) {
2876 struct ata_eh_info *ehi = &link->eh_info;
2878 link->eh_context.i.dev_action[dev->devno] |=
2879 ehi->dev_action[dev->devno] & ATA_EH_PARK;
2880 ata_eh_clear_action(link, dev, ehi, ATA_EH_PARK);
2883 spin_unlock_irqrestore(ap->lock, flags);
2886 static void ata_eh_park_issue_cmd(struct ata_device *dev, int park)
2888 struct ata_eh_context *ehc = &dev->link->eh_context;
2889 struct ata_taskfile tf;
2890 unsigned int err_mask;
2892 ata_tf_init(dev, &tf);
2894 ehc->unloaded_mask |= 1 << dev->devno;
2895 tf.command = ATA_CMD_IDLEIMMEDIATE;
2901 ehc->unloaded_mask &= ~(1 << dev->devno);
2902 tf.command = ATA_CMD_CHK_POWER;
2905 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
2906 tf.protocol = ATA_PROT_NODATA;
2907 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
2908 if (park && (err_mask || tf.lbal != 0xc4)) {
2909 ata_dev_err(dev, "head unload failed!\n");
2910 ehc->unloaded_mask &= ~(1 << dev->devno);
2914 static int ata_eh_revalidate_and_attach(struct ata_link *link,
2915 struct ata_device **r_failed_dev)
2917 struct ata_port *ap = link->ap;
2918 struct ata_eh_context *ehc = &link->eh_context;
2919 struct ata_device *dev;
2920 unsigned int new_mask = 0;
2921 unsigned long flags;
2926 /* For PATA drive side cable detection to work, IDENTIFY must
2927 * be done backwards such that PDIAG- is released by the slave
2928 * device before the master device is identified.
2930 ata_for_each_dev(dev, link, ALL_REVERSE) {
2931 unsigned int action = ata_eh_dev_action(dev);
2932 unsigned int readid_flags = 0;
2934 if (ehc->i.flags & ATA_EHI_DID_RESET)
2935 readid_flags |= ATA_READID_POSTRESET;
2937 if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
2938 WARN_ON(dev->class == ATA_DEV_PMP);
2940 if (ata_phys_link_offline(ata_dev_phys_link(dev))) {
2945 ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
2946 rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
2951 ata_eh_done(link, dev, ATA_EH_REVALIDATE);
2953 /* Configuration may have changed, reconfigure
2956 ehc->i.flags |= ATA_EHI_SETMODE;
2958 /* schedule the scsi_rescan_device() here */
2959 schedule_work(&(ap->scsi_rescan_task));
2960 } else if (dev->class == ATA_DEV_UNKNOWN &&
2961 ehc->tries[dev->devno] &&
2962 ata_class_enabled(ehc->classes[dev->devno])) {
2963 /* Temporarily set dev->class, it will be
2964 * permanently set once all configurations are
2965 * complete. This is necessary because new
2966 * device configuration is done in two
2969 dev->class = ehc->classes[dev->devno];
2971 if (dev->class == ATA_DEV_PMP)
2972 rc = sata_pmp_attach(dev);
2974 rc = ata_dev_read_id(dev, &dev->class,
2975 readid_flags, dev->id);
2977 /* read_id might have changed class, store and reset */
2978 ehc->classes[dev->devno] = dev->class;
2979 dev->class = ATA_DEV_UNKNOWN;
2983 /* clear error info accumulated during probe */
2984 ata_ering_clear(&dev->ering);
2985 new_mask |= 1 << dev->devno;
2988 /* IDENTIFY was issued to non-existent
2989 * device. No need to reset. Just
2990 * thaw and ignore the device.
2992 ata_eh_thaw_port(ap);
3000 /* PDIAG- should have been released, ask cable type if post-reset */
3001 if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) {
3002 if (ap->ops->cable_detect)
3003 ap->cbl = ap->ops->cable_detect(ap);
3007 /* Configure new devices forward such that user doesn't see
3008 * device detection messages backwards.
3010 ata_for_each_dev(dev, link, ALL) {
3011 if (!(new_mask & (1 << dev->devno)))
3014 dev->class = ehc->classes[dev->devno];
3016 if (dev->class == ATA_DEV_PMP)
3019 ehc->i.flags |= ATA_EHI_PRINTINFO;
3020 rc = ata_dev_configure(dev);
3021 ehc->i.flags &= ~ATA_EHI_PRINTINFO;
3023 dev->class = ATA_DEV_UNKNOWN;
3027 spin_lock_irqsave(ap->lock, flags);
3028 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
3029 spin_unlock_irqrestore(ap->lock, flags);
3031 /* new device discovered, configure xfermode */
3032 ehc->i.flags |= ATA_EHI_SETMODE;
3038 *r_failed_dev = dev;
3039 DPRINTK("EXIT rc=%d\n", rc);
3044 * ata_set_mode - Program timings and issue SET FEATURES - XFER
3045 * @link: link on which timings will be programmed
3046 * @r_failed_dev: out parameter for failed device
3048 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3049 * ata_set_mode() fails, pointer to the failing device is
3050 * returned in @r_failed_dev.
3053 * PCI/etc. bus probe sem.
3056 * 0 on success, negative errno otherwise
3058 int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3060 struct ata_port *ap = link->ap;
3061 struct ata_device *dev;
3064 /* if data transfer is verified, clear DUBIOUS_XFER on ering top */
3065 ata_for_each_dev(dev, link, ENABLED) {
3066 if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
3067 struct ata_ering_entry *ent;
3069 ent = ata_ering_top(&dev->ering);
3071 ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER;
3075 /* has private set_mode? */
3076 if (ap->ops->set_mode)
3077 rc = ap->ops->set_mode(link, r_failed_dev);
3079 rc = ata_do_set_mode(link, r_failed_dev);
3081 /* if transfer mode has changed, set DUBIOUS_XFER on device */
3082 ata_for_each_dev(dev, link, ENABLED) {
3083 struct ata_eh_context *ehc = &link->eh_context;
3084 u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
3085 u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
3087 if (dev->xfer_mode != saved_xfer_mode ||
3088 ata_ncq_enabled(dev) != saved_ncq)
3089 dev->flags |= ATA_DFLAG_DUBIOUS_XFER;
3096 * atapi_eh_clear_ua - Clear ATAPI UNIT ATTENTION after reset
3097 * @dev: ATAPI device to clear UA for
3099 * Resets and other operations can make an ATAPI device raise
3100 * UNIT ATTENTION which causes the next operation to fail. This
3101 * function clears UA.
3104 * EH context (may sleep).
3107 * 0 on success, -errno on failure.
3109 static int atapi_eh_clear_ua(struct ata_device *dev)
3113 for (i = 0; i < ATA_EH_UA_TRIES; i++) {
3114 u8 *sense_buffer = dev->link->ap->sector_buf;
3116 unsigned int err_mask;
3118 err_mask = atapi_eh_tur(dev, &sense_key);
3119 if (err_mask != 0 && err_mask != AC_ERR_DEV) {
3121 "TEST_UNIT_READY failed (err_mask=0x%x)\n",
3126 if (!err_mask || sense_key != UNIT_ATTENTION)
3129 err_mask = atapi_eh_request_sense(dev, sense_buffer, sense_key);
3131 ata_dev_warn(dev, "failed to clear "
3132 "UNIT ATTENTION (err_mask=0x%x)\n", err_mask);
3137 ata_dev_warn(dev, "UNIT ATTENTION persists after %d tries\n",
3144 * ata_eh_maybe_retry_flush - Retry FLUSH if necessary
3145 * @dev: ATA device which may need FLUSH retry
3147 * If @dev failed FLUSH, it needs to be reported upper layer
3148 * immediately as it means that @dev failed to remap and already
3149 * lost at least a sector and further FLUSH retrials won't make
3150 * any difference to the lost sector. However, if FLUSH failed
3151 * for other reasons, for example transmission error, FLUSH needs
3154 * This function determines whether FLUSH failure retry is
3155 * necessary and performs it if so.
3158 * 0 if EH can continue, -errno if EH needs to be repeated.
3160 static int ata_eh_maybe_retry_flush(struct ata_device *dev)
3162 struct ata_link *link = dev->link;
3163 struct ata_port *ap = link->ap;
3164 struct ata_queued_cmd *qc;
3165 struct ata_taskfile tf;
3166 unsigned int err_mask;
3169 /* did flush fail for this device? */
3170 if (!ata_tag_valid(link->active_tag))
3173 qc = __ata_qc_from_tag(ap, link->active_tag);
3174 if (qc->dev != dev || (qc->tf.command != ATA_CMD_FLUSH_EXT &&
3175 qc->tf.command != ATA_CMD_FLUSH))
3178 /* if the device failed it, it should be reported to upper layers */
3179 if (qc->err_mask & AC_ERR_DEV)
3182 /* flush failed for some other reason, give it another shot */
3183 ata_tf_init(dev, &tf);
3185 tf.command = qc->tf.command;
3186 tf.flags |= ATA_TFLAG_DEVICE;
3187 tf.protocol = ATA_PROT_NODATA;
3189 ata_dev_warn(dev, "retrying FLUSH 0x%x Emask 0x%x\n",
3190 tf.command, qc->err_mask);
3192 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
3195 * FLUSH is complete but there's no way to
3196 * successfully complete a failed command from EH.
3197 * Making sure retry is allowed at least once and
3198 * retrying it should do the trick - whatever was in
3199 * the cache is already on the platter and this won't
3200 * cause infinite loop.
3202 qc->scsicmd->allowed = max(qc->scsicmd->allowed, 1);
3204 ata_dev_warn(dev, "FLUSH failed Emask 0x%x\n",
3208 /* if device failed it, report it to upper layers */
3209 if (err_mask & AC_ERR_DEV) {
3210 qc->err_mask |= AC_ERR_DEV;
3212 if (!(ap->pflags & ATA_PFLAG_FROZEN))
3220 * ata_eh_set_lpm - configure SATA interface power management
3221 * @link: link to configure power management
3222 * @policy: the link power management policy
3223 * @r_failed_dev: out parameter for failed device
3225 * Enable SATA Interface power management. This will enable
3226 * Device Interface Power Management (DIPM) for min_power and
3227 * medium_power_with_dipm policies, and then call driver specific
3228 * callbacks for enabling Host Initiated Power management.
3234 * 0 on success, -errno on failure.
3236 static int ata_eh_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
3237 struct ata_device **r_failed_dev)
3239 struct ata_port *ap = ata_is_host_link(link) ? link->ap : NULL;
3240 struct ata_eh_context *ehc = &link->eh_context;
3241 struct ata_device *dev, *link_dev = NULL, *lpm_dev = NULL;
3242 enum ata_lpm_policy old_policy = link->lpm_policy;
3243 bool no_dipm = link->ap->flags & ATA_FLAG_NO_DIPM;
3244 unsigned int hints = ATA_LPM_EMPTY | ATA_LPM_HIPM;
3245 unsigned int err_mask;
3248 /* if the link or host doesn't do LPM, noop */
3249 if (!IS_ENABLED(CONFIG_SATA_HOST) ||
3250 (link->flags & ATA_LFLAG_NO_LPM) || (ap && !ap->ops->set_lpm))
3254 * DIPM is enabled only for MIN_POWER as some devices
3255 * misbehave when the host NACKs transition to SLUMBER. Order
3256 * device and link configurations such that the host always
3257 * allows DIPM requests.
3259 ata_for_each_dev(dev, link, ENABLED) {
3260 bool hipm = ata_id_has_hipm(dev->id);
3261 bool dipm = ata_id_has_dipm(dev->id) && !no_dipm;
3263 /* find the first enabled and LPM enabled devices */
3267 if (!lpm_dev && (hipm || dipm))
3270 hints &= ~ATA_LPM_EMPTY;
3272 hints &= ~ATA_LPM_HIPM;
3274 /* disable DIPM before changing link config */
3275 if (policy < ATA_LPM_MED_POWER_WITH_DIPM && dipm) {
3276 err_mask = ata_dev_set_feature(dev,
3277 SETFEATURES_SATA_DISABLE, SATA_DIPM);
3278 if (err_mask && err_mask != AC_ERR_DEV) {
3280 "failed to disable DIPM, Emask 0x%x\n",
3289 rc = ap->ops->set_lpm(link, policy, hints);
3290 if (!rc && ap->slave_link)
3291 rc = ap->ops->set_lpm(ap->slave_link, policy, hints);
3293 rc = sata_pmp_set_lpm(link, policy, hints);
3296 * Attribute link config failure to the first (LPM) enabled
3297 * device on the link.
3300 if (rc == -EOPNOTSUPP) {
3301 link->flags |= ATA_LFLAG_NO_LPM;
3304 dev = lpm_dev ? lpm_dev : link_dev;
3309 * Low level driver acked the transition. Issue DIPM command
3310 * with the new policy set.
3312 link->lpm_policy = policy;
3313 if (ap && ap->slave_link)
3314 ap->slave_link->lpm_policy = policy;
3316 /* host config updated, enable DIPM if transitioning to MIN_POWER */
3317 ata_for_each_dev(dev, link, ENABLED) {
3318 if (policy >= ATA_LPM_MED_POWER_WITH_DIPM && !no_dipm &&
3319 ata_id_has_dipm(dev->id)) {
3320 err_mask = ata_dev_set_feature(dev,
3321 SETFEATURES_SATA_ENABLE, SATA_DIPM);
3322 if (err_mask && err_mask != AC_ERR_DEV) {
3324 "failed to enable DIPM, Emask 0x%x\n",
3332 link->last_lpm_change = jiffies;
3333 link->flags |= ATA_LFLAG_CHANGED;
3338 /* restore the old policy */
3339 link->lpm_policy = old_policy;
3340 if (ap && ap->slave_link)
3341 ap->slave_link->lpm_policy = old_policy;
3343 /* if no device or only one more chance is left, disable LPM */
3344 if (!dev || ehc->tries[dev->devno] <= 2) {
3345 ata_link_warn(link, "disabling LPM on the link\n");
3346 link->flags |= ATA_LFLAG_NO_LPM;
3349 *r_failed_dev = dev;
3353 int ata_link_nr_enabled(struct ata_link *link)
3355 struct ata_device *dev;
3358 ata_for_each_dev(dev, link, ENABLED)
3363 static int ata_link_nr_vacant(struct ata_link *link)
3365 struct ata_device *dev;
3368 ata_for_each_dev(dev, link, ALL)
3369 if (dev->class == ATA_DEV_UNKNOWN)
3374 static int ata_eh_skip_recovery(struct ata_link *link)
3376 struct ata_port *ap = link->ap;
3377 struct ata_eh_context *ehc = &link->eh_context;
3378 struct ata_device *dev;
3380 /* skip disabled links */
3381 if (link->flags & ATA_LFLAG_DISABLED)
3384 /* skip if explicitly requested */
3385 if (ehc->i.flags & ATA_EHI_NO_RECOVERY)
3388 /* thaw frozen port and recover failed devices */
3389 if ((ap->pflags & ATA_PFLAG_FROZEN) || ata_link_nr_enabled(link))
3392 /* reset at least once if reset is requested */
3393 if ((ehc->i.action & ATA_EH_RESET) &&
3394 !(ehc->i.flags & ATA_EHI_DID_RESET))
3397 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
3398 ata_for_each_dev(dev, link, ALL) {
3399 if (dev->class == ATA_DEV_UNKNOWN &&
3400 ehc->classes[dev->devno] != ATA_DEV_NONE)
3407 static int ata_count_probe_trials_cb(struct ata_ering_entry *ent, void *void_arg)
3409 u64 interval = msecs_to_jiffies(ATA_EH_PROBE_TRIAL_INTERVAL);
3410 u64 now = get_jiffies_64();
3411 int *trials = void_arg;
3413 if ((ent->eflags & ATA_EFLAG_OLD_ER) ||
3414 (ent->timestamp < now - min(now, interval)))
3421 static int ata_eh_schedule_probe(struct ata_device *dev)
3423 struct ata_eh_context *ehc = &dev->link->eh_context;
3424 struct ata_link *link = ata_dev_phys_link(dev);
3427 if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
3428 (ehc->did_probe_mask & (1 << dev->devno)))
3431 ata_eh_detach_dev(dev);
3433 ehc->did_probe_mask |= (1 << dev->devno);
3434 ehc->i.action |= ATA_EH_RESET;
3435 ehc->saved_xfer_mode[dev->devno] = 0;
3436 ehc->saved_ncq_enabled &= ~(1 << dev->devno);
3438 /* the link maybe in a deep sleep, wake it up */
3439 if (link->lpm_policy > ATA_LPM_MAX_POWER) {
3440 if (ata_is_host_link(link))
3441 link->ap->ops->set_lpm(link, ATA_LPM_MAX_POWER,
3444 sata_pmp_set_lpm(link, ATA_LPM_MAX_POWER,
3448 /* Record and count probe trials on the ering. The specific
3449 * error mask used is irrelevant. Because a successful device
3450 * detection clears the ering, this count accumulates only if
3451 * there are consecutive failed probes.
3453 * If the count is equal to or higher than ATA_EH_PROBE_TRIALS
3454 * in the last ATA_EH_PROBE_TRIAL_INTERVAL, link speed is
3455 * forced to 1.5Gbps.
3457 * This is to work around cases where failed link speed
3458 * negotiation results in device misdetection leading to
3459 * infinite DEVXCHG or PHRDY CHG events.
3461 ata_ering_record(&dev->ering, 0, AC_ERR_OTHER);
3462 ata_ering_map(&dev->ering, ata_count_probe_trials_cb, &trials);
3464 if (trials > ATA_EH_PROBE_TRIALS)
3465 sata_down_spd_limit(link, 1);
3470 static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
3472 struct ata_eh_context *ehc = &dev->link->eh_context;
3474 /* -EAGAIN from EH routine indicates retry without prejudice.
3475 * The requester is responsible for ensuring forward progress.
3478 ehc->tries[dev->devno]--;
3482 /* device missing or wrong IDENTIFY data, schedule probing */
3483 ehc->i.probe_mask |= (1 << dev->devno);
3486 /* give it just one more chance */
3487 ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
3490 if (ehc->tries[dev->devno] == 1) {
3491 /* This is the last chance, better to slow
3492 * down than lose it.
3494 sata_down_spd_limit(ata_dev_phys_link(dev), 0);
3495 if (dev->pio_mode > XFER_PIO_0)
3496 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
3500 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
3501 /* disable device if it has used up all its chances */
3502 ata_dev_disable(dev);
3504 /* detach if offline */
3505 if (ata_phys_link_offline(ata_dev_phys_link(dev)))
3506 ata_eh_detach_dev(dev);
3508 /* schedule probe if necessary */
3509 if (ata_eh_schedule_probe(dev)) {
3510 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
3511 memset(ehc->cmd_timeout_idx[dev->devno], 0,
3512 sizeof(ehc->cmd_timeout_idx[dev->devno]));
3517 ehc->i.action |= ATA_EH_RESET;
3523 * ata_eh_recover - recover host port after error
3524 * @ap: host port to recover
3525 * @prereset: prereset method (can be NULL)
3526 * @softreset: softreset method (can be NULL)
3527 * @hardreset: hardreset method (can be NULL)
3528 * @postreset: postreset method (can be NULL)
3529 * @r_failed_link: out parameter for failed link
3531 * This is the alpha and omega, eum and yang, heart and soul of
3532 * libata exception handling. On entry, actions required to
3533 * recover each link and hotplug requests are recorded in the
3534 * link's eh_context. This function executes all the operations
3535 * with appropriate retrials and fallbacks to resurrect failed
3536 * devices, detach goners and greet newcomers.
3539 * Kernel thread context (may sleep).
3542 * 0 on success, -errno on failure.
3544 int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
3545 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
3546 ata_postreset_fn_t postreset,
3547 struct ata_link **r_failed_link)
3549 struct ata_link *link;
3550 struct ata_device *dev;
3552 unsigned long flags, deadline;
3556 /* prep for recovery */
3557 ata_for_each_link(link, ap, EDGE) {
3558 struct ata_eh_context *ehc = &link->eh_context;
3560 /* re-enable link? */
3561 if (ehc->i.action & ATA_EH_ENABLE_LINK) {
3562 ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
3563 spin_lock_irqsave(ap->lock, flags);
3564 link->flags &= ~ATA_LFLAG_DISABLED;
3565 spin_unlock_irqrestore(ap->lock, flags);
3566 ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
3569 ata_for_each_dev(dev, link, ALL) {
3570 if (link->flags & ATA_LFLAG_NO_RETRY)
3571 ehc->tries[dev->devno] = 1;
3573 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
3575 /* collect port action mask recorded in dev actions */
3576 ehc->i.action |= ehc->i.dev_action[dev->devno] &
3577 ~ATA_EH_PERDEV_MASK;
3578 ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
3580 /* process hotplug request */
3581 if (dev->flags & ATA_DFLAG_DETACH)
3582 ata_eh_detach_dev(dev);
3584 /* schedule probe if necessary */
3585 if (!ata_dev_enabled(dev))
3586 ata_eh_schedule_probe(dev);
3593 /* if UNLOADING, finish immediately */
3594 if (ap->pflags & ATA_PFLAG_UNLOADING)
3598 ata_for_each_link(link, ap, EDGE) {
3599 struct ata_eh_context *ehc = &link->eh_context;
3601 /* skip EH if possible. */
3602 if (ata_eh_skip_recovery(link))
3605 ata_for_each_dev(dev, link, ALL)
3606 ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
3610 ata_for_each_link(link, ap, EDGE) {
3611 struct ata_eh_context *ehc = &link->eh_context;
3613 if (!(ehc->i.action & ATA_EH_RESET))
3616 rc = ata_eh_reset(link, ata_link_nr_vacant(link),
3617 prereset, softreset, hardreset, postreset);
3619 ata_link_err(link, "reset failed, giving up\n");
3628 * clears ATA_EH_PARK in eh_info and resets
3629 * ap->park_req_pending
3631 ata_eh_pull_park_action(ap);
3634 ata_for_each_link(link, ap, EDGE) {
3635 ata_for_each_dev(dev, link, ALL) {
3636 struct ata_eh_context *ehc = &link->eh_context;
3639 if (dev->class != ATA_DEV_ATA &&
3640 dev->class != ATA_DEV_ZAC)
3642 if (!(ehc->i.dev_action[dev->devno] &
3645 tmp = dev->unpark_deadline;
3646 if (time_before(deadline, tmp))
3648 else if (time_before_eq(tmp, jiffies))
3650 if (ehc->unloaded_mask & (1 << dev->devno))
3653 ata_eh_park_issue_cmd(dev, 1);
3658 if (time_before_eq(deadline, now))
3662 deadline = wait_for_completion_timeout(&ap->park_req_pending,
3666 ata_for_each_link(link, ap, EDGE) {
3667 ata_for_each_dev(dev, link, ALL) {
3668 if (!(link->eh_context.unloaded_mask &
3672 ata_eh_park_issue_cmd(dev, 0);
3673 ata_eh_done(link, dev, ATA_EH_PARK);
3679 ata_for_each_link(link, ap, PMP_FIRST) {
3680 struct ata_eh_context *ehc = &link->eh_context;
3682 if (sata_pmp_attached(ap) && ata_is_host_link(link))
3685 /* revalidate existing devices and attach new ones */
3686 rc = ata_eh_revalidate_and_attach(link, &dev);
3690 /* if PMP got attached, return, pmp EH will take care of it */
3691 if (link->device->class == ATA_DEV_PMP) {
3696 /* configure transfer mode if necessary */
3697 if (ehc->i.flags & ATA_EHI_SETMODE) {
3698 rc = ata_set_mode(link, &dev);
3701 ehc->i.flags &= ~ATA_EHI_SETMODE;
3704 /* If reset has been issued, clear UA to avoid
3705 * disrupting the current users of the device.
3707 if (ehc->i.flags & ATA_EHI_DID_RESET) {
3708 ata_for_each_dev(dev, link, ALL) {
3709 if (dev->class != ATA_DEV_ATAPI)
3711 rc = atapi_eh_clear_ua(dev);
3714 if (zpodd_dev_enabled(dev))
3715 zpodd_post_poweron(dev);
3719 /* retry flush if necessary */
3720 ata_for_each_dev(dev, link, ALL) {
3721 if (dev->class != ATA_DEV_ATA &&
3722 dev->class != ATA_DEV_ZAC)
3724 rc = ata_eh_maybe_retry_flush(dev);
3730 /* configure link power saving */
3731 if (link->lpm_policy != ap->target_lpm_policy) {
3732 rc = ata_eh_set_lpm(link, ap->target_lpm_policy, &dev);
3737 /* this link is okay now */
3744 ata_eh_handle_dev_fail(dev, rc);
3746 if (ap->pflags & ATA_PFLAG_FROZEN) {
3747 /* PMP reset requires working host port.
3748 * Can't retry if it's frozen.
3750 if (sata_pmp_attached(ap))
3760 if (rc && r_failed_link)
3761 *r_failed_link = link;
3763 DPRINTK("EXIT, rc=%d\n", rc);
3768 * ata_eh_finish - finish up EH
3769 * @ap: host port to finish EH for
3771 * Recovery is complete. Clean up EH states and retry or finish
3777 void ata_eh_finish(struct ata_port *ap)
3779 struct ata_queued_cmd *qc;
3782 /* retry or finish qcs */
3783 ata_qc_for_each_raw(ap, qc, tag) {
3784 if (!(qc->flags & ATA_QCFLAG_FAILED))
3788 /* FIXME: Once EH migration is complete,
3789 * generate sense data in this function,
3790 * considering both err_mask and tf.
3792 if (qc->flags & ATA_QCFLAG_RETRY)
3793 ata_eh_qc_retry(qc);
3795 ata_eh_qc_complete(qc);
3797 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
3798 ata_eh_qc_complete(qc);
3800 /* feed zero TF to sense generation */
3801 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
3802 ata_eh_qc_retry(qc);
3807 /* make sure nr_active_links is zero after EH */
3808 WARN_ON(ap->nr_active_links);
3809 ap->nr_active_links = 0;
3813 * ata_do_eh - do standard error handling
3814 * @ap: host port to handle error for
3816 * @prereset: prereset method (can be NULL)
3817 * @softreset: softreset method (can be NULL)
3818 * @hardreset: hardreset method (can be NULL)
3819 * @postreset: postreset method (can be NULL)
3821 * Perform standard error handling sequence.
3824 * Kernel thread context (may sleep).
3826 void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
3827 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
3828 ata_postreset_fn_t postreset)
3830 struct ata_device *dev;
3836 rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
3839 ata_for_each_dev(dev, &ap->link, ALL)
3840 ata_dev_disable(dev);
3847 * ata_std_error_handler - standard error handler
3848 * @ap: host port to handle error for
3850 * Standard error handler
3853 * Kernel thread context (may sleep).
3855 void ata_std_error_handler(struct ata_port *ap)
3857 struct ata_port_operations *ops = ap->ops;
3858 ata_reset_fn_t hardreset = ops->hardreset;
3860 /* ignore built-in hardreset if SCR access is not available */
3861 if (hardreset == sata_std_hardreset && !sata_scr_valid(&ap->link))
3864 ata_do_eh(ap, ops->prereset, ops->softreset, hardreset, ops->postreset);
3866 EXPORT_SYMBOL_GPL(ata_std_error_handler);
3870 * ata_eh_handle_port_suspend - perform port suspend operation
3871 * @ap: port to suspend
3876 * Kernel thread context (may sleep).
3878 static void ata_eh_handle_port_suspend(struct ata_port *ap)
3880 unsigned long flags;
3882 struct ata_device *dev;
3884 /* are we suspending? */
3885 spin_lock_irqsave(ap->lock, flags);
3886 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
3887 ap->pm_mesg.event & PM_EVENT_RESUME) {
3888 spin_unlock_irqrestore(ap->lock, flags);
3891 spin_unlock_irqrestore(ap->lock, flags);
3893 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
3896 * If we have a ZPODD attached, check its zero
3897 * power ready status before the port is frozen.
3898 * Only needed for runtime suspend.
3900 if (PMSG_IS_AUTO(ap->pm_mesg)) {
3901 ata_for_each_dev(dev, &ap->link, ENABLED) {
3902 if (zpodd_dev_enabled(dev))
3903 zpodd_on_suspend(dev);
3907 /* tell ACPI we're suspending */
3908 rc = ata_acpi_on_suspend(ap);
3913 ata_eh_freeze_port(ap);
3915 if (ap->ops->port_suspend)
3916 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
3918 ata_acpi_set_state(ap, ap->pm_mesg);
3920 /* update the flags */
3921 spin_lock_irqsave(ap->lock, flags);
3923 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
3925 ap->pflags |= ATA_PFLAG_SUSPENDED;
3926 else if (ap->pflags & ATA_PFLAG_FROZEN)
3927 ata_port_schedule_eh(ap);
3929 spin_unlock_irqrestore(ap->lock, flags);
3935 * ata_eh_handle_port_resume - perform port resume operation
3936 * @ap: port to resume
3941 * Kernel thread context (may sleep).
3943 static void ata_eh_handle_port_resume(struct ata_port *ap)
3945 struct ata_link *link;
3946 struct ata_device *dev;
3947 unsigned long flags;
3949 /* are we resuming? */
3950 spin_lock_irqsave(ap->lock, flags);
3951 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
3952 !(ap->pm_mesg.event & PM_EVENT_RESUME)) {
3953 spin_unlock_irqrestore(ap->lock, flags);
3956 spin_unlock_irqrestore(ap->lock, flags);
3958 WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
3961 * Error timestamps are in jiffies which doesn't run while
3962 * suspended and PHY events during resume isn't too uncommon.
3963 * When the two are combined, it can lead to unnecessary speed
3964 * downs if the machine is suspended and resumed repeatedly.
3965 * Clear error history.
3967 ata_for_each_link(link, ap, HOST_FIRST)
3968 ata_for_each_dev(dev, link, ALL)
3969 ata_ering_clear(&dev->ering);
3971 ata_acpi_set_state(ap, ap->pm_mesg);
3973 if (ap->ops->port_resume)
3974 ap->ops->port_resume(ap);
3976 /* tell ACPI that we're resuming */
3977 ata_acpi_on_resume(ap);
3979 /* update the flags */
3980 spin_lock_irqsave(ap->lock, flags);
3981 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
3982 spin_unlock_irqrestore(ap->lock, flags);
3984 #endif /* CONFIG_PM */