1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * libata-eh.c - libata error handling
5 * Maintained by: Tejun Heo <tj@kernel.org>
6 * Please ALWAYS copy linux-ide@vger.kernel.org
9 * Copyright 2006 Tejun Heo <htejun@gmail.com>
11 * libata documentation is available via 'make {ps|pdf}docs',
12 * as Documentation/driver-api/libata.rst
14 * Hardware documentation available from http://www.t13.org/ and
15 * http://www.sata-io.org/
18 #include <linux/kernel.h>
19 #include <linux/blkdev.h>
20 #include <linux/export.h>
21 #include <linux/pci.h>
22 #include <scsi/scsi.h>
23 #include <scsi/scsi_host.h>
24 #include <scsi/scsi_eh.h>
25 #include <scsi/scsi_device.h>
26 #include <scsi/scsi_cmnd.h>
27 #include <scsi/scsi_dbg.h>
28 #include "../scsi/scsi_transport_api.h"
30 #include <linux/libata.h>
32 #include <trace/events/libata.h>
36 /* speed down verdicts */
37 ATA_EH_SPDN_NCQ_OFF = (1 << 0),
38 ATA_EH_SPDN_SPEED_DOWN = (1 << 1),
39 ATA_EH_SPDN_FALLBACK_TO_PIO = (1 << 2),
40 ATA_EH_SPDN_KEEP_ERRORS = (1 << 3),
43 ATA_EFLAG_IS_IO = (1 << 0),
44 ATA_EFLAG_DUBIOUS_XFER = (1 << 1),
45 ATA_EFLAG_OLD_ER = (1 << 31),
47 /* error categories */
50 ATA_ECAT_TOUT_HSM = 2,
52 ATA_ECAT_DUBIOUS_NONE = 4,
53 ATA_ECAT_DUBIOUS_ATA_BUS = 5,
54 ATA_ECAT_DUBIOUS_TOUT_HSM = 6,
55 ATA_ECAT_DUBIOUS_UNK_DEV = 7,
58 ATA_EH_CMD_DFL_TIMEOUT = 5000,
60 /* always put at least this amount of time between resets */
61 ATA_EH_RESET_COOL_DOWN = 5000,
63 /* Waiting in ->prereset can never be reliable. It's
64 * sometimes nice to wait there but it can't be depended upon;
65 * otherwise, we wouldn't be resetting. Just give it enough
66 * time for most drives to spin up.
68 ATA_EH_PRERESET_TIMEOUT = 10000,
69 ATA_EH_FASTDRAIN_INTERVAL = 3000,
73 /* probe speed down parameters, see ata_eh_schedule_probe() */
74 ATA_EH_PROBE_TRIAL_INTERVAL = 60000, /* 1 min */
75 ATA_EH_PROBE_TRIALS = 2,
78 /* The following table determines how we sequence resets. Each entry
79 * represents timeout for that try. The first try can be soft or
80 * hardreset. All others are hardreset if available. In most cases
81 * the first reset w/ 10sec timeout should succeed. Following entries
82 * are mostly for error handling, hotplug and those outlier devices that
83 * take an exceptionally long time to recover from reset.
85 static const unsigned long ata_eh_reset_timeouts[] = {
86 10000, /* most drives spin up by 10sec */
87 10000, /* > 99% working drives spin up before 20sec */
88 35000, /* give > 30 secs of idleness for outlier devices */
89 5000, /* and sweet one last chance */
90 ULONG_MAX, /* > 1 min has elapsed, give up */
93 static const unsigned long ata_eh_identify_timeouts[] = {
94 5000, /* covers > 99% of successes and not too boring on failures */
95 10000, /* combined time till here is enough even for media access */
96 30000, /* for true idiots */
100 static const unsigned long ata_eh_revalidate_timeouts[] = {
101 15000, /* Some drives are slow to read log pages when waking-up */
102 15000, /* combined time till here is enough even for media access */
106 static const unsigned long ata_eh_flush_timeouts[] = {
107 15000, /* be generous with flush */
109 30000, /* and even more generous */
113 static const unsigned long ata_eh_other_timeouts[] = {
114 5000, /* same rationale as identify timeout */
116 /* but no merciful 30sec for other commands, it just isn't worth it */
120 struct ata_eh_cmd_timeout_ent {
122 const unsigned long *timeouts;
125 /* The following table determines timeouts to use for EH internal
126 * commands. Each table entry is a command class and matches the
127 * commands the entry applies to and the timeout table to use.
129 * On the retry after a command timed out, the next timeout value from
130 * the table is used. If the table doesn't contain further entries,
131 * the last value is used.
133 * ehc->cmd_timeout_idx keeps track of which timeout to use per
134 * command class, so if SET_FEATURES times out on the first try, the
135 * next try will use the second timeout value only for that class.
137 #define CMDS(cmds...) (const u8 []){ cmds, 0 }
138 static const struct ata_eh_cmd_timeout_ent
139 ata_eh_cmd_timeout_table[ATA_EH_CMD_TIMEOUT_TABLE_SIZE] = {
140 { .commands = CMDS(ATA_CMD_ID_ATA, ATA_CMD_ID_ATAPI),
141 .timeouts = ata_eh_identify_timeouts, },
142 { .commands = CMDS(ATA_CMD_READ_LOG_EXT, ATA_CMD_READ_LOG_DMA_EXT),
143 .timeouts = ata_eh_revalidate_timeouts, },
144 { .commands = CMDS(ATA_CMD_READ_NATIVE_MAX, ATA_CMD_READ_NATIVE_MAX_EXT),
145 .timeouts = ata_eh_other_timeouts, },
146 { .commands = CMDS(ATA_CMD_SET_MAX, ATA_CMD_SET_MAX_EXT),
147 .timeouts = ata_eh_other_timeouts, },
148 { .commands = CMDS(ATA_CMD_SET_FEATURES),
149 .timeouts = ata_eh_other_timeouts, },
150 { .commands = CMDS(ATA_CMD_INIT_DEV_PARAMS),
151 .timeouts = ata_eh_other_timeouts, },
152 { .commands = CMDS(ATA_CMD_FLUSH, ATA_CMD_FLUSH_EXT),
153 .timeouts = ata_eh_flush_timeouts },
157 static void __ata_port_freeze(struct ata_port *ap);
159 static void ata_eh_handle_port_suspend(struct ata_port *ap);
160 static void ata_eh_handle_port_resume(struct ata_port *ap);
161 #else /* CONFIG_PM */
162 static void ata_eh_handle_port_suspend(struct ata_port *ap)
165 static void ata_eh_handle_port_resume(struct ata_port *ap)
167 #endif /* CONFIG_PM */
169 static __printf(2, 0) void __ata_ehi_pushv_desc(struct ata_eh_info *ehi,
170 const char *fmt, va_list args)
172 ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
173 ATA_EH_DESC_LEN - ehi->desc_len,
178 * __ata_ehi_push_desc - push error description without adding separator
180 * @fmt: printf format string
182 * Format string according to @fmt and append it to @ehi->desc.
185 * spin_lock_irqsave(host lock)
187 void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
192 __ata_ehi_pushv_desc(ehi, fmt, args);
197 * ata_ehi_push_desc - push error description with separator
199 * @fmt: printf format string
201 * Format string according to @fmt and append it to @ehi->desc.
202 * If @ehi->desc is not empty, ", " is added in-between.
205 * spin_lock_irqsave(host lock)
207 void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
212 __ata_ehi_push_desc(ehi, ", ");
215 __ata_ehi_pushv_desc(ehi, fmt, args);
220 * ata_ehi_clear_desc - clean error description
226 * spin_lock_irqsave(host lock)
228 void ata_ehi_clear_desc(struct ata_eh_info *ehi)
235 * ata_port_desc - append port description
236 * @ap: target ATA port
237 * @fmt: printf format string
239 * Format string according to @fmt and append it to port
240 * description. If port description is not empty, " " is added
241 * in-between. This function is to be used while initializing
242 * ata_host. The description is printed on host registration.
247 void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
251 WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
253 if (ap->link.eh_info.desc_len)
254 __ata_ehi_push_desc(&ap->link.eh_info, " ");
257 __ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
264 * ata_port_pbar_desc - append PCI BAR description
265 * @ap: target ATA port
266 * @bar: target PCI BAR
267 * @offset: offset into PCI BAR
268 * @name: name of the area
270 * If @offset is negative, this function formats a string which
271 * contains the name, address, size and type of the BAR and
272 * appends it to the port description. If @offset is zero or
273 * positive, only name and offsetted address is appended.
278 void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
281 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
283 unsigned long long start, len;
285 if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
287 else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
290 start = (unsigned long long)pci_resource_start(pdev, bar);
291 len = (unsigned long long)pci_resource_len(pdev, bar);
294 ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
296 ata_port_desc(ap, "%s 0x%llx", name,
297 start + (unsigned long long)offset);
300 #endif /* CONFIG_PCI */
302 static int ata_lookup_timeout_table(u8 cmd)
306 for (i = 0; i < ATA_EH_CMD_TIMEOUT_TABLE_SIZE; i++) {
309 for (cur = ata_eh_cmd_timeout_table[i].commands; *cur; cur++)
318 * ata_internal_cmd_timeout - determine timeout for an internal command
319 * @dev: target device
320 * @cmd: internal command to be issued
322 * Determine timeout for internal command @cmd for @dev.
328 * Determined timeout.
330 unsigned long ata_internal_cmd_timeout(struct ata_device *dev, u8 cmd)
332 struct ata_eh_context *ehc = &dev->link->eh_context;
333 int ent = ata_lookup_timeout_table(cmd);
337 return ATA_EH_CMD_DFL_TIMEOUT;
339 idx = ehc->cmd_timeout_idx[dev->devno][ent];
340 return ata_eh_cmd_timeout_table[ent].timeouts[idx];
344 * ata_internal_cmd_timed_out - notification for internal command timeout
345 * @dev: target device
346 * @cmd: internal command which timed out
348 * Notify EH that internal command @cmd for @dev timed out. This
349 * function should be called only for commands whose timeouts are
350 * determined using ata_internal_cmd_timeout().
355 void ata_internal_cmd_timed_out(struct ata_device *dev, u8 cmd)
357 struct ata_eh_context *ehc = &dev->link->eh_context;
358 int ent = ata_lookup_timeout_table(cmd);
364 idx = ehc->cmd_timeout_idx[dev->devno][ent];
365 if (ata_eh_cmd_timeout_table[ent].timeouts[idx + 1] != ULONG_MAX)
366 ehc->cmd_timeout_idx[dev->devno][ent]++;
369 static void ata_ering_record(struct ata_ering *ering, unsigned int eflags,
370 unsigned int err_mask)
372 struct ata_ering_entry *ent;
377 ering->cursor %= ATA_ERING_SIZE;
379 ent = &ering->ring[ering->cursor];
380 ent->eflags = eflags;
381 ent->err_mask = err_mask;
382 ent->timestamp = get_jiffies_64();
385 static struct ata_ering_entry *ata_ering_top(struct ata_ering *ering)
387 struct ata_ering_entry *ent = &ering->ring[ering->cursor];
394 int ata_ering_map(struct ata_ering *ering,
395 int (*map_fn)(struct ata_ering_entry *, void *),
399 struct ata_ering_entry *ent;
403 ent = &ering->ring[idx];
406 rc = map_fn(ent, arg);
409 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
410 } while (idx != ering->cursor);
415 static int ata_ering_clear_cb(struct ata_ering_entry *ent, void *void_arg)
417 ent->eflags |= ATA_EFLAG_OLD_ER;
421 static void ata_ering_clear(struct ata_ering *ering)
423 ata_ering_map(ering, ata_ering_clear_cb, NULL);
426 static unsigned int ata_eh_dev_action(struct ata_device *dev)
428 struct ata_eh_context *ehc = &dev->link->eh_context;
430 return ehc->i.action | ehc->i.dev_action[dev->devno];
433 static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
434 struct ata_eh_info *ehi, unsigned int action)
436 struct ata_device *tdev;
439 ehi->action &= ~action;
440 ata_for_each_dev(tdev, link, ALL)
441 ehi->dev_action[tdev->devno] &= ~action;
443 /* doesn't make sense for port-wide EH actions */
444 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
446 /* break ehi->action into ehi->dev_action */
447 if (ehi->action & action) {
448 ata_for_each_dev(tdev, link, ALL)
449 ehi->dev_action[tdev->devno] |=
450 ehi->action & action;
451 ehi->action &= ~action;
454 /* turn off the specified per-dev action */
455 ehi->dev_action[dev->devno] &= ~action;
460 * ata_eh_acquire - acquire EH ownership
461 * @ap: ATA port to acquire EH ownership for
463 * Acquire EH ownership for @ap. This is the basic exclusion
464 * mechanism for ports sharing a host. Only one port hanging off
465 * the same host can claim the ownership of EH.
470 void ata_eh_acquire(struct ata_port *ap)
472 mutex_lock(&ap->host->eh_mutex);
473 WARN_ON_ONCE(ap->host->eh_owner);
474 ap->host->eh_owner = current;
478 * ata_eh_release - release EH ownership
479 * @ap: ATA port to release EH ownership for
481 * Release EH ownership for @ap if the caller. The caller must
482 * have acquired EH ownership using ata_eh_acquire() previously.
487 void ata_eh_release(struct ata_port *ap)
489 WARN_ON_ONCE(ap->host->eh_owner != current);
490 ap->host->eh_owner = NULL;
491 mutex_unlock(&ap->host->eh_mutex);
494 static void ata_eh_unload(struct ata_port *ap)
496 struct ata_link *link;
497 struct ata_device *dev;
500 /* Restore SControl IPM and SPD for the next driver and
501 * disable attached devices.
503 ata_for_each_link(link, ap, PMP_FIRST) {
504 sata_scr_write(link, SCR_CONTROL, link->saved_scontrol & 0xff0);
505 ata_for_each_dev(dev, link, ALL)
506 ata_dev_disable(dev);
509 /* freeze and set UNLOADED */
510 spin_lock_irqsave(ap->lock, flags);
512 ata_port_freeze(ap); /* won't be thawed */
513 ap->pflags &= ~ATA_PFLAG_EH_PENDING; /* clear pending from freeze */
514 ap->pflags |= ATA_PFLAG_UNLOADED;
516 spin_unlock_irqrestore(ap->lock, flags);
520 * ata_scsi_error - SCSI layer error handler callback
521 * @host: SCSI host on which error occurred
523 * Handles SCSI-layer-thrown error events.
526 * Inherited from SCSI layer (none, can sleep)
531 void ata_scsi_error(struct Scsi_Host *host)
533 struct ata_port *ap = ata_shost_to_port(host);
535 LIST_HEAD(eh_work_q);
539 spin_lock_irqsave(host->host_lock, flags);
540 list_splice_init(&host->eh_cmd_q, &eh_work_q);
541 spin_unlock_irqrestore(host->host_lock, flags);
543 ata_scsi_cmd_error_handler(host, ap, &eh_work_q);
545 /* If we timed raced normal completion and there is nothing to
546 recover nr_timedout == 0 why exactly are we doing error recovery ? */
547 ata_scsi_port_error_handler(host, ap);
549 /* finish or retry handled scmd's and clean up */
550 WARN_ON(!list_empty(&eh_work_q));
556 * ata_scsi_cmd_error_handler - error callback for a list of commands
557 * @host: scsi host containing the port
558 * @ap: ATA port within the host
559 * @eh_work_q: list of commands to process
561 * process the given list of commands and return those finished to the
562 * ap->eh_done_q. This function is the first part of the libata error
563 * handler which processes a given list of failed commands.
565 void ata_scsi_cmd_error_handler(struct Scsi_Host *host, struct ata_port *ap,
566 struct list_head *eh_work_q)
571 /* make sure sff pio task is not running */
572 ata_sff_flush_pio_task(ap);
574 /* synchronize with host lock and sort out timeouts */
576 /* For new EH, all qcs are finished in one of three ways -
577 * normal completion, error completion, and SCSI timeout.
578 * Both completions can race against SCSI timeout. When normal
579 * completion wins, the qc never reaches EH. When error
580 * completion wins, the qc has ATA_QCFLAG_FAILED set.
582 * When SCSI timeout wins, things are a bit more complex.
583 * Normal or error completion can occur after the timeout but
584 * before this point. In such cases, both types of
585 * completions are honored. A scmd is determined to have
586 * timed out iff its associated qc is active and not failed.
588 spin_lock_irqsave(ap->lock, flags);
589 if (ap->ops->error_handler) {
590 struct scsi_cmnd *scmd, *tmp;
593 /* This must occur under the ap->lock as we don't want
594 a polled recovery to race the real interrupt handler
596 The lost_interrupt handler checks for any completed but
597 non-notified command and completes much like an IRQ handler.
599 We then fall into the error recovery code which will treat
600 this as if normal completion won the race */
602 if (ap->ops->lost_interrupt)
603 ap->ops->lost_interrupt(ap);
605 list_for_each_entry_safe(scmd, tmp, eh_work_q, eh_entry) {
606 struct ata_queued_cmd *qc;
608 ata_qc_for_each_raw(ap, qc, i) {
609 if (qc->flags & ATA_QCFLAG_ACTIVE &&
614 if (i < ATA_MAX_QUEUE) {
615 /* the scmd has an associated qc */
616 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
617 /* which hasn't failed yet, timeout */
618 qc->err_mask |= AC_ERR_TIMEOUT;
619 qc->flags |= ATA_QCFLAG_FAILED;
623 /* Normal completion occurred after
624 * SCSI timeout but before this point.
625 * Successfully complete it.
627 scmd->retries = scmd->allowed;
628 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
632 /* If we have timed out qcs. They belong to EH from
633 * this point but the state of the controller is
634 * unknown. Freeze the port to make sure the IRQ
635 * handler doesn't diddle with those qcs. This must
636 * be done atomically w.r.t. setting QCFLAG_FAILED.
639 __ata_port_freeze(ap);
642 /* initialize eh_tries */
643 ap->eh_tries = ATA_EH_MAX_TRIES;
645 spin_unlock_irqrestore(ap->lock, flags);
648 EXPORT_SYMBOL(ata_scsi_cmd_error_handler);
651 * ata_scsi_port_error_handler - recover the port after the commands
652 * @host: SCSI host containing the port
655 * Handle the recovery of the port @ap after all the commands
656 * have been recovered.
658 void ata_scsi_port_error_handler(struct Scsi_Host *host, struct ata_port *ap)
662 /* invoke error handler */
663 if (ap->ops->error_handler) {
664 struct ata_link *link;
666 /* acquire EH ownership */
669 /* kill fast drain timer */
670 del_timer_sync(&ap->fastdrain_timer);
672 /* process port resume request */
673 ata_eh_handle_port_resume(ap);
675 /* fetch & clear EH info */
676 spin_lock_irqsave(ap->lock, flags);
678 ata_for_each_link(link, ap, HOST_FIRST) {
679 struct ata_eh_context *ehc = &link->eh_context;
680 struct ata_device *dev;
682 memset(&link->eh_context, 0, sizeof(link->eh_context));
683 link->eh_context.i = link->eh_info;
684 memset(&link->eh_info, 0, sizeof(link->eh_info));
686 ata_for_each_dev(dev, link, ENABLED) {
687 int devno = dev->devno;
689 ehc->saved_xfer_mode[devno] = dev->xfer_mode;
690 if (ata_ncq_enabled(dev))
691 ehc->saved_ncq_enabled |= 1 << devno;
695 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
696 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
697 ap->excl_link = NULL; /* don't maintain exclusion over EH */
699 spin_unlock_irqrestore(ap->lock, flags);
701 /* invoke EH, skip if unloading or suspended */
702 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
703 ap->ops->error_handler(ap);
705 /* if unloading, commence suicide */
706 if ((ap->pflags & ATA_PFLAG_UNLOADING) &&
707 !(ap->pflags & ATA_PFLAG_UNLOADED))
712 /* process port suspend request */
713 ata_eh_handle_port_suspend(ap);
715 /* Exception might have happened after ->error_handler
716 * recovered the port but before this point. Repeat
719 spin_lock_irqsave(ap->lock, flags);
721 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
722 if (--ap->eh_tries) {
723 spin_unlock_irqrestore(ap->lock, flags);
727 "EH pending after %d tries, giving up\n",
729 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
732 /* this run is complete, make sure EH info is clear */
733 ata_for_each_link(link, ap, HOST_FIRST)
734 memset(&link->eh_info, 0, sizeof(link->eh_info));
736 /* end eh (clear host_eh_scheduled) while holding
737 * ap->lock such that if exception occurs after this
738 * point but before EH completion, SCSI midlayer will
743 spin_unlock_irqrestore(ap->lock, flags);
746 WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
747 ap->ops->eng_timeout(ap);
750 scsi_eh_flush_done_q(&ap->eh_done_q);
753 spin_lock_irqsave(ap->lock, flags);
755 if (ap->pflags & ATA_PFLAG_LOADING)
756 ap->pflags &= ~ATA_PFLAG_LOADING;
757 else if ((ap->pflags & ATA_PFLAG_SCSI_HOTPLUG) &&
758 !(ap->flags & ATA_FLAG_SAS_HOST))
759 schedule_delayed_work(&ap->hotplug_task, 0);
761 if (ap->pflags & ATA_PFLAG_RECOVERED)
762 ata_port_info(ap, "EH complete\n");
764 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
766 /* tell wait_eh that we're done */
767 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
768 wake_up_all(&ap->eh_wait_q);
770 spin_unlock_irqrestore(ap->lock, flags);
772 EXPORT_SYMBOL_GPL(ata_scsi_port_error_handler);
775 * ata_port_wait_eh - Wait for the currently pending EH to complete
776 * @ap: Port to wait EH for
778 * Wait until the currently pending EH is complete.
781 * Kernel thread context (may sleep).
783 void ata_port_wait_eh(struct ata_port *ap)
789 spin_lock_irqsave(ap->lock, flags);
791 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
792 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
793 spin_unlock_irqrestore(ap->lock, flags);
795 spin_lock_irqsave(ap->lock, flags);
797 finish_wait(&ap->eh_wait_q, &wait);
799 spin_unlock_irqrestore(ap->lock, flags);
801 /* make sure SCSI EH is complete */
802 if (scsi_host_in_recovery(ap->scsi_host)) {
807 EXPORT_SYMBOL_GPL(ata_port_wait_eh);
809 static int ata_eh_nr_in_flight(struct ata_port *ap)
811 struct ata_queued_cmd *qc;
815 /* count only non-internal commands */
816 ata_qc_for_each(ap, qc, tag) {
824 void ata_eh_fastdrain_timerfn(struct timer_list *t)
826 struct ata_port *ap = from_timer(ap, t, fastdrain_timer);
830 spin_lock_irqsave(ap->lock, flags);
832 cnt = ata_eh_nr_in_flight(ap);
838 if (cnt == ap->fastdrain_cnt) {
839 struct ata_queued_cmd *qc;
842 /* No progress during the last interval, tag all
843 * in-flight qcs as timed out and freeze the port.
845 ata_qc_for_each(ap, qc, tag) {
847 qc->err_mask |= AC_ERR_TIMEOUT;
852 /* some qcs have finished, give it another chance */
853 ap->fastdrain_cnt = cnt;
854 ap->fastdrain_timer.expires =
855 ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
856 add_timer(&ap->fastdrain_timer);
860 spin_unlock_irqrestore(ap->lock, flags);
864 * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
865 * @ap: target ATA port
866 * @fastdrain: activate fast drain
868 * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
869 * is non-zero and EH wasn't pending before. Fast drain ensures
870 * that EH kicks in in timely manner.
873 * spin_lock_irqsave(host lock)
875 static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
879 /* already scheduled? */
880 if (ap->pflags & ATA_PFLAG_EH_PENDING)
883 ap->pflags |= ATA_PFLAG_EH_PENDING;
888 /* do we have in-flight qcs? */
889 cnt = ata_eh_nr_in_flight(ap);
893 /* activate fast drain */
894 ap->fastdrain_cnt = cnt;
895 ap->fastdrain_timer.expires =
896 ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
897 add_timer(&ap->fastdrain_timer);
901 * ata_qc_schedule_eh - schedule qc for error handling
902 * @qc: command to schedule error handling for
904 * Schedule error handling for @qc. EH will kick in as soon as
905 * other commands are drained.
908 * spin_lock_irqsave(host lock)
910 void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
912 struct ata_port *ap = qc->ap;
914 WARN_ON(!ap->ops->error_handler);
916 qc->flags |= ATA_QCFLAG_FAILED;
917 ata_eh_set_pending(ap, 1);
919 /* The following will fail if timeout has already expired.
920 * ata_scsi_error() takes care of such scmds on EH entry.
921 * Note that ATA_QCFLAG_FAILED is unconditionally set after
922 * this function completes.
924 blk_abort_request(qc->scsicmd->request);
928 * ata_std_sched_eh - non-libsas ata_ports issue eh with this common routine
929 * @ap: ATA port to schedule EH for
931 * LOCKING: inherited from ata_port_schedule_eh
932 * spin_lock_irqsave(host lock)
934 void ata_std_sched_eh(struct ata_port *ap)
936 WARN_ON(!ap->ops->error_handler);
938 if (ap->pflags & ATA_PFLAG_INITIALIZING)
941 ata_eh_set_pending(ap, 1);
942 scsi_schedule_eh(ap->scsi_host);
944 DPRINTK("port EH scheduled\n");
946 EXPORT_SYMBOL_GPL(ata_std_sched_eh);
949 * ata_std_end_eh - non-libsas ata_ports complete eh with this common routine
950 * @ap: ATA port to end EH for
952 * In the libata object model there is a 1:1 mapping of ata_port to
953 * shost, so host fields can be directly manipulated under ap->lock, in
954 * the libsas case we need to hold a lock at the ha->level to coordinate
958 * spin_lock_irqsave(host lock)
960 void ata_std_end_eh(struct ata_port *ap)
962 struct Scsi_Host *host = ap->scsi_host;
964 host->host_eh_scheduled = 0;
966 EXPORT_SYMBOL(ata_std_end_eh);
970 * ata_port_schedule_eh - schedule error handling without a qc
971 * @ap: ATA port to schedule EH for
973 * Schedule error handling for @ap. EH will kick in as soon as
974 * all commands are drained.
977 * spin_lock_irqsave(host lock)
979 void ata_port_schedule_eh(struct ata_port *ap)
981 /* see: ata_std_sched_eh, unless you know better */
982 ap->ops->sched_eh(ap);
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);
1028 * ata_port_abort - abort all qc's on the port
1029 * @ap: ATA port to abort qc's for
1031 * Abort all active qc's of @ap and schedule EH.
1034 * spin_lock_irqsave(host_set lock)
1037 * Number of aborted qc's.
1039 int ata_port_abort(struct ata_port *ap)
1041 return ata_do_link_abort(ap, NULL);
1045 * __ata_port_freeze - freeze port
1046 * @ap: ATA port to freeze
1048 * This function is called when HSM violation or some other
1049 * condition disrupts normal operation of the port. Frozen port
1050 * is not allowed to perform any operation until the port is
1051 * thawed, which usually follows a successful reset.
1053 * ap->ops->freeze() callback can be used for freezing the port
1054 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
1055 * port cannot be frozen hardware-wise, the interrupt handler
1056 * must ack and clear interrupts unconditionally while the port
1060 * spin_lock_irqsave(host lock)
1062 static void __ata_port_freeze(struct ata_port *ap)
1064 WARN_ON(!ap->ops->error_handler);
1066 if (ap->ops->freeze)
1067 ap->ops->freeze(ap);
1069 ap->pflags |= ATA_PFLAG_FROZEN;
1071 DPRINTK("ata%u port frozen\n", ap->print_id);
1075 * ata_port_freeze - abort & freeze port
1076 * @ap: ATA port to freeze
1078 * Abort and freeze @ap. The freeze operation must be called
1079 * first, because some hardware requires special operations
1080 * before the taskfile registers are accessible.
1083 * spin_lock_irqsave(host lock)
1086 * Number of aborted commands.
1088 int ata_port_freeze(struct ata_port *ap)
1092 WARN_ON(!ap->ops->error_handler);
1094 __ata_port_freeze(ap);
1095 nr_aborted = ata_port_abort(ap);
1101 * sata_async_notification - SATA async notification handler
1102 * @ap: ATA port where async notification is received
1104 * Handler to be called when async notification via SDB FIS is
1105 * received. This function schedules EH if necessary.
1108 * spin_lock_irqsave(host lock)
1111 * 1 if EH is scheduled, 0 otherwise.
1113 int sata_async_notification(struct ata_port *ap)
1118 if (!(ap->flags & ATA_FLAG_AN))
1121 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
1123 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
1125 if (!sata_pmp_attached(ap) || rc) {
1126 /* PMP is not attached or SNTF is not available */
1127 if (!sata_pmp_attached(ap)) {
1128 /* PMP is not attached. Check whether ATAPI
1129 * AN is configured. If so, notify media
1132 struct ata_device *dev = ap->link.device;
1134 if ((dev->class == ATA_DEV_ATAPI) &&
1135 (dev->flags & ATA_DFLAG_AN))
1136 ata_scsi_media_change_notify(dev);
1139 /* PMP is attached but SNTF is not available.
1140 * ATAPI async media change notification is
1141 * not used. The PMP must be reporting PHY
1142 * status change, schedule EH.
1144 ata_port_schedule_eh(ap);
1148 /* PMP is attached and SNTF is available */
1149 struct ata_link *link;
1151 /* check and notify ATAPI AN */
1152 ata_for_each_link(link, ap, EDGE) {
1153 if (!(sntf & (1 << link->pmp)))
1156 if ((link->device->class == ATA_DEV_ATAPI) &&
1157 (link->device->flags & ATA_DFLAG_AN))
1158 ata_scsi_media_change_notify(link->device);
1161 /* If PMP is reporting that PHY status of some
1162 * downstream ports has changed, schedule EH.
1164 if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
1165 ata_port_schedule_eh(ap);
1174 * ata_eh_freeze_port - EH helper to freeze port
1175 * @ap: ATA port to freeze
1182 void ata_eh_freeze_port(struct ata_port *ap)
1184 unsigned long flags;
1186 if (!ap->ops->error_handler)
1189 spin_lock_irqsave(ap->lock, flags);
1190 __ata_port_freeze(ap);
1191 spin_unlock_irqrestore(ap->lock, flags);
1195 * ata_port_thaw_port - EH helper to thaw port
1196 * @ap: ATA port to thaw
1198 * Thaw frozen port @ap.
1203 void ata_eh_thaw_port(struct ata_port *ap)
1205 unsigned long flags;
1207 if (!ap->ops->error_handler)
1210 spin_lock_irqsave(ap->lock, flags);
1212 ap->pflags &= ~ATA_PFLAG_FROZEN;
1217 spin_unlock_irqrestore(ap->lock, flags);
1219 DPRINTK("ata%u port thawed\n", ap->print_id);
1222 static void ata_eh_scsidone(struct scsi_cmnd *scmd)
1227 static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
1229 struct ata_port *ap = qc->ap;
1230 struct scsi_cmnd *scmd = qc->scsicmd;
1231 unsigned long flags;
1233 spin_lock_irqsave(ap->lock, flags);
1234 qc->scsidone = ata_eh_scsidone;
1235 __ata_qc_complete(qc);
1236 WARN_ON(ata_tag_valid(qc->tag));
1237 spin_unlock_irqrestore(ap->lock, flags);
1239 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
1243 * ata_eh_qc_complete - Complete an active ATA command from EH
1244 * @qc: Command to complete
1246 * Indicate to the mid and upper layers that an ATA command has
1247 * completed. To be used from EH.
1249 void ata_eh_qc_complete(struct ata_queued_cmd *qc)
1251 struct scsi_cmnd *scmd = qc->scsicmd;
1252 scmd->retries = scmd->allowed;
1253 __ata_eh_qc_complete(qc);
1257 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
1258 * @qc: Command to retry
1260 * Indicate to the mid and upper layers that an ATA command
1261 * should be retried. To be used from EH.
1263 * SCSI midlayer limits the number of retries to scmd->allowed.
1264 * scmd->allowed is incremented for commands which get retried
1265 * due to unrelated failures (qc->err_mask is zero).
1267 void ata_eh_qc_retry(struct ata_queued_cmd *qc)
1269 struct scsi_cmnd *scmd = qc->scsicmd;
1272 __ata_eh_qc_complete(qc);
1276 * ata_dev_disable - disable ATA device
1277 * @dev: ATA device to disable
1284 void ata_dev_disable(struct ata_device *dev)
1286 if (!ata_dev_enabled(dev))
1289 if (ata_msg_drv(dev->link->ap))
1290 ata_dev_warn(dev, "disabled\n");
1291 ata_acpi_on_disable(dev);
1292 ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 | ATA_DNXFER_QUIET);
1295 /* From now till the next successful probe, ering is used to
1296 * track probe failures. Clear accumulated device error info.
1298 ata_ering_clear(&dev->ering);
1302 * ata_eh_detach_dev - detach ATA device
1303 * @dev: ATA device to detach
1310 void ata_eh_detach_dev(struct ata_device *dev)
1312 struct ata_link *link = dev->link;
1313 struct ata_port *ap = link->ap;
1314 struct ata_eh_context *ehc = &link->eh_context;
1315 unsigned long flags;
1317 ata_dev_disable(dev);
1319 spin_lock_irqsave(ap->lock, flags);
1321 dev->flags &= ~ATA_DFLAG_DETACH;
1323 if (ata_scsi_offline_dev(dev)) {
1324 dev->flags |= ATA_DFLAG_DETACHED;
1325 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
1328 /* clear per-dev EH info */
1329 ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
1330 ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
1331 ehc->saved_xfer_mode[dev->devno] = 0;
1332 ehc->saved_ncq_enabled &= ~(1 << dev->devno);
1334 spin_unlock_irqrestore(ap->lock, flags);
1338 * ata_eh_about_to_do - about to perform eh_action
1339 * @link: target ATA link
1340 * @dev: target ATA dev for per-dev action (can be NULL)
1341 * @action: action about to be performed
1343 * Called just before performing EH actions to clear related bits
1344 * in @link->eh_info such that eh actions are not unnecessarily
1350 void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
1351 unsigned int action)
1353 struct ata_port *ap = link->ap;
1354 struct ata_eh_info *ehi = &link->eh_info;
1355 struct ata_eh_context *ehc = &link->eh_context;
1356 unsigned long flags;
1358 spin_lock_irqsave(ap->lock, flags);
1360 ata_eh_clear_action(link, dev, ehi, action);
1362 /* About to take EH action, set RECOVERED. Ignore actions on
1363 * slave links as master will do them again.
1365 if (!(ehc->i.flags & ATA_EHI_QUIET) && link != ap->slave_link)
1366 ap->pflags |= ATA_PFLAG_RECOVERED;
1368 spin_unlock_irqrestore(ap->lock, flags);
1372 * ata_eh_done - EH action complete
1373 * @link: ATA link for which EH actions are complete
1374 * @dev: target ATA dev for per-dev action (can be NULL)
1375 * @action: action just completed
1377 * Called right after performing EH actions to clear related bits
1378 * in @link->eh_context.
1383 void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1384 unsigned int action)
1386 struct ata_eh_context *ehc = &link->eh_context;
1388 ata_eh_clear_action(link, dev, &ehc->i, action);
1392 * ata_err_string - convert err_mask to descriptive string
1393 * @err_mask: error mask to convert to string
1395 * Convert @err_mask to descriptive string. Errors are
1396 * prioritized according to severity and only the most severe
1397 * error is reported.
1403 * Descriptive string for @err_mask
1405 static const char *ata_err_string(unsigned int err_mask)
1407 if (err_mask & AC_ERR_HOST_BUS)
1408 return "host bus error";
1409 if (err_mask & AC_ERR_ATA_BUS)
1410 return "ATA bus error";
1411 if (err_mask & AC_ERR_TIMEOUT)
1413 if (err_mask & AC_ERR_HSM)
1414 return "HSM violation";
1415 if (err_mask & AC_ERR_SYSTEM)
1416 return "internal error";
1417 if (err_mask & AC_ERR_MEDIA)
1418 return "media error";
1419 if (err_mask & AC_ERR_INVALID)
1420 return "invalid argument";
1421 if (err_mask & AC_ERR_DEV)
1422 return "device error";
1423 if (err_mask & AC_ERR_NCQ)
1425 if (err_mask & AC_ERR_NODEV_HINT)
1426 return "Polling detection error";
1427 return "unknown error";
1431 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
1432 * @dev: Device to read log page 10h from
1433 * @tag: Resulting tag of the failed command
1434 * @tf: Resulting taskfile registers of the failed command
1436 * Read log page 10h to obtain NCQ error details and clear error
1440 * Kernel thread context (may sleep).
1443 * 0 on success, -errno otherwise.
1445 static int ata_eh_read_log_10h(struct ata_device *dev,
1446 int *tag, struct ata_taskfile *tf)
1448 u8 *buf = dev->link->ap->sector_buf;
1449 unsigned int err_mask;
1453 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, 0, buf, 1);
1458 for (i = 0; i < ATA_SECT_SIZE; i++)
1461 ata_dev_warn(dev, "invalid checksum 0x%x on log page 10h\n",
1467 *tag = buf[0] & 0x1f;
1469 tf->command = buf[2];
1470 tf->feature = buf[3];
1474 tf->device = buf[7];
1475 tf->hob_lbal = buf[8];
1476 tf->hob_lbam = buf[9];
1477 tf->hob_lbah = buf[10];
1478 tf->nsect = buf[12];
1479 tf->hob_nsect = buf[13];
1480 if (dev->class == ATA_DEV_ZAC && ata_id_has_ncq_autosense(dev->id))
1481 tf->auxiliary = buf[14] << 16 | buf[15] << 8 | buf[16];
1487 * atapi_eh_tur - perform ATAPI TEST_UNIT_READY
1488 * @dev: target ATAPI device
1489 * @r_sense_key: out parameter for sense_key
1491 * Perform ATAPI TEST_UNIT_READY.
1494 * EH context (may sleep).
1497 * 0 on success, AC_ERR_* mask on failure.
1499 unsigned int atapi_eh_tur(struct ata_device *dev, u8 *r_sense_key)
1501 u8 cdb[ATAPI_CDB_LEN] = { TEST_UNIT_READY, 0, 0, 0, 0, 0 };
1502 struct ata_taskfile tf;
1503 unsigned int err_mask;
1505 ata_tf_init(dev, &tf);
1507 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1508 tf.command = ATA_CMD_PACKET;
1509 tf.protocol = ATAPI_PROT_NODATA;
1511 err_mask = ata_exec_internal(dev, &tf, cdb, DMA_NONE, NULL, 0, 0);
1512 if (err_mask == AC_ERR_DEV)
1513 *r_sense_key = tf.feature >> 4;
1518 * ata_eh_request_sense - perform REQUEST_SENSE_DATA_EXT
1519 * @qc: qc to perform REQUEST_SENSE_SENSE_DATA_EXT to
1520 * @cmd: scsi command for which the sense code should be set
1522 * Perform REQUEST_SENSE_DATA_EXT after the device reported CHECK
1523 * SENSE. This function is an EH helper.
1526 * Kernel thread context (may sleep).
1528 static void ata_eh_request_sense(struct ata_queued_cmd *qc,
1529 struct scsi_cmnd *cmd)
1531 struct ata_device *dev = qc->dev;
1532 struct ata_taskfile tf;
1533 unsigned int err_mask;
1535 if (qc->ap->pflags & ATA_PFLAG_FROZEN) {
1536 ata_dev_warn(dev, "sense data available but port frozen\n");
1540 if (!cmd || qc->flags & ATA_QCFLAG_SENSE_VALID)
1543 if (!ata_id_sense_reporting_enabled(dev->id)) {
1544 ata_dev_warn(qc->dev, "sense data reporting disabled\n");
1548 DPRINTK("ATA request sense\n");
1550 ata_tf_init(dev, &tf);
1551 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1552 tf.flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
1553 tf.command = ATA_CMD_REQ_SENSE_DATA;
1554 tf.protocol = ATA_PROT_NODATA;
1556 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1557 /* Ignore err_mask; ATA_ERR might be set */
1558 if (tf.command & ATA_SENSE) {
1559 ata_scsi_set_sense(dev, cmd, tf.lbah, tf.lbam, tf.lbal);
1560 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1562 ata_dev_warn(dev, "request sense failed stat %02x emask %x\n",
1563 tf.command, err_mask);
1568 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1569 * @dev: device to perform REQUEST_SENSE to
1570 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
1571 * @dfl_sense_key: default sense key to use
1573 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
1574 * SENSE. This function is EH helper.
1577 * Kernel thread context (may sleep).
1580 * 0 on success, AC_ERR_* mask on failure
1582 unsigned int atapi_eh_request_sense(struct ata_device *dev,
1583 u8 *sense_buf, u8 dfl_sense_key)
1585 u8 cdb[ATAPI_CDB_LEN] =
1586 { REQUEST_SENSE, 0, 0, 0, SCSI_SENSE_BUFFERSIZE, 0 };
1587 struct ata_port *ap = dev->link->ap;
1588 struct ata_taskfile tf;
1590 DPRINTK("ATAPI request sense\n");
1592 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1594 /* initialize sense_buf with the error register,
1595 * for the case where they are -not- overwritten
1597 sense_buf[0] = 0x70;
1598 sense_buf[2] = dfl_sense_key;
1600 /* some devices time out if garbage left in tf */
1601 ata_tf_init(dev, &tf);
1603 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1604 tf.command = ATA_CMD_PACKET;
1606 /* is it pointless to prefer PIO for "safety reasons"? */
1607 if (ap->flags & ATA_FLAG_PIO_DMA) {
1608 tf.protocol = ATAPI_PROT_DMA;
1609 tf.feature |= ATAPI_PKT_DMA;
1611 tf.protocol = ATAPI_PROT_PIO;
1612 tf.lbam = SCSI_SENSE_BUFFERSIZE;
1616 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
1617 sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
1621 * ata_eh_analyze_serror - analyze SError for a failed port
1622 * @link: ATA link to analyze SError for
1624 * Analyze SError if available and further determine cause of
1630 static void ata_eh_analyze_serror(struct ata_link *link)
1632 struct ata_eh_context *ehc = &link->eh_context;
1633 u32 serror = ehc->i.serror;
1634 unsigned int err_mask = 0, action = 0;
1637 if (serror & (SERR_PERSISTENT | SERR_DATA)) {
1638 err_mask |= AC_ERR_ATA_BUS;
1639 action |= ATA_EH_RESET;
1641 if (serror & SERR_PROTOCOL) {
1642 err_mask |= AC_ERR_HSM;
1643 action |= ATA_EH_RESET;
1645 if (serror & SERR_INTERNAL) {
1646 err_mask |= AC_ERR_SYSTEM;
1647 action |= ATA_EH_RESET;
1650 /* Determine whether a hotplug event has occurred. Both
1651 * SError.N/X are considered hotplug events for enabled or
1652 * host links. For disabled PMP links, only N bit is
1653 * considered as X bit is left at 1 for link plugging.
1655 if (link->lpm_policy > ATA_LPM_MAX_POWER)
1656 hotplug_mask = 0; /* hotplug doesn't work w/ LPM */
1657 else if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
1658 hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
1660 hotplug_mask = SERR_PHYRDY_CHG;
1662 if (serror & hotplug_mask)
1663 ata_ehi_hotplugged(&ehc->i);
1665 ehc->i.err_mask |= err_mask;
1666 ehc->i.action |= action;
1670 * ata_eh_analyze_ncq_error - analyze NCQ error
1671 * @link: ATA link to analyze NCQ error for
1673 * Read log page 10h, determine the offending qc and acquire
1674 * error status TF. For NCQ device errors, all LLDDs have to do
1675 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1679 * Kernel thread context (may sleep).
1681 void ata_eh_analyze_ncq_error(struct ata_link *link)
1683 struct ata_port *ap = link->ap;
1684 struct ata_eh_context *ehc = &link->eh_context;
1685 struct ata_device *dev = link->device;
1686 struct ata_queued_cmd *qc;
1687 struct ata_taskfile tf;
1690 /* if frozen, we can't do much */
1691 if (ap->pflags & ATA_PFLAG_FROZEN)
1694 /* is it NCQ device error? */
1695 if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1698 /* has LLDD analyzed already? */
1699 ata_qc_for_each_raw(ap, qc, tag) {
1700 if (!(qc->flags & ATA_QCFLAG_FAILED))
1707 /* okay, this error is ours */
1708 memset(&tf, 0, sizeof(tf));
1709 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1711 ata_link_err(link, "failed to read log page 10h (errno=%d)\n",
1716 if (!(link->sactive & (1 << tag))) {
1717 ata_link_err(link, "log page 10h reported inactive tag %d\n",
1722 /* we've got the perpetrator, condemn it */
1723 qc = __ata_qc_from_tag(ap, tag);
1724 memcpy(&qc->result_tf, &tf, sizeof(tf));
1725 qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
1726 qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
1727 if (dev->class == ATA_DEV_ZAC &&
1728 ((qc->result_tf.command & ATA_SENSE) || qc->result_tf.auxiliary)) {
1729 char sense_key, asc, ascq;
1731 sense_key = (qc->result_tf.auxiliary >> 16) & 0xff;
1732 asc = (qc->result_tf.auxiliary >> 8) & 0xff;
1733 ascq = qc->result_tf.auxiliary & 0xff;
1734 ata_scsi_set_sense(dev, qc->scsicmd, sense_key, asc, ascq);
1735 ata_scsi_set_sense_information(dev, qc->scsicmd,
1737 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1740 ehc->i.err_mask &= ~AC_ERR_DEV;
1744 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1745 * @qc: qc to analyze
1746 * @tf: Taskfile registers to analyze
1748 * Analyze taskfile of @qc and further determine cause of
1749 * failure. This function also requests ATAPI sense data if
1753 * Kernel thread context (may sleep).
1756 * Determined recovery action
1758 static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1759 const struct ata_taskfile *tf)
1761 unsigned int tmp, action = 0;
1762 u8 stat = tf->command, err = tf->feature;
1764 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1765 qc->err_mask |= AC_ERR_HSM;
1766 return ATA_EH_RESET;
1769 if (stat & (ATA_ERR | ATA_DF)) {
1770 qc->err_mask |= AC_ERR_DEV;
1772 * Sense data reporting does not work if the
1773 * device fault bit is set.
1781 switch (qc->dev->class) {
1783 if (stat & ATA_SENSE)
1784 ata_eh_request_sense(qc, qc->scsicmd);
1788 qc->err_mask |= AC_ERR_ATA_BUS;
1789 if (err & (ATA_UNC | ATA_AMNF))
1790 qc->err_mask |= AC_ERR_MEDIA;
1792 qc->err_mask |= AC_ERR_INVALID;
1796 if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
1797 tmp = atapi_eh_request_sense(qc->dev,
1798 qc->scsicmd->sense_buffer,
1799 qc->result_tf.feature >> 4);
1801 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1803 qc->err_mask |= tmp;
1807 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1808 int ret = scsi_check_sense(qc->scsicmd);
1810 * SUCCESS here means that the sense code could be
1811 * evaluated and should be passed to the upper layers
1812 * for correct evaluation.
1813 * FAILED means the sense code could not be interpreted
1814 * and the device would need to be reset.
1815 * NEEDS_RETRY and ADD_TO_MLQUEUE means that the
1816 * command would need to be retried.
1818 if (ret == NEEDS_RETRY || ret == ADD_TO_MLQUEUE) {
1819 qc->flags |= ATA_QCFLAG_RETRY;
1820 qc->err_mask |= AC_ERR_OTHER;
1821 } else if (ret != SUCCESS) {
1822 qc->err_mask |= AC_ERR_HSM;
1825 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1826 action |= ATA_EH_RESET;
1831 static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask,
1836 if (!(eflags & ATA_EFLAG_DUBIOUS_XFER))
1840 base = ATA_ECAT_DUBIOUS_NONE;
1842 if (err_mask & AC_ERR_ATA_BUS)
1843 return base + ATA_ECAT_ATA_BUS;
1845 if (err_mask & AC_ERR_TIMEOUT)
1846 return base + ATA_ECAT_TOUT_HSM;
1848 if (eflags & ATA_EFLAG_IS_IO) {
1849 if (err_mask & AC_ERR_HSM)
1850 return base + ATA_ECAT_TOUT_HSM;
1852 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1853 return base + ATA_ECAT_UNK_DEV;
1859 struct speed_down_verdict_arg {
1862 int nr_errors[ATA_ECAT_NR];
1865 static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
1867 struct speed_down_verdict_arg *arg = void_arg;
1870 if ((ent->eflags & ATA_EFLAG_OLD_ER) || (ent->timestamp < arg->since))
1873 cat = ata_eh_categorize_error(ent->eflags, ent->err_mask,
1875 arg->nr_errors[cat]++;
1881 * ata_eh_speed_down_verdict - Determine speed down verdict
1882 * @dev: Device of interest
1884 * This function examines error ring of @dev and determines
1885 * whether NCQ needs to be turned off, transfer speed should be
1886 * stepped down, or falling back to PIO is necessary.
1888 * ECAT_ATA_BUS : ATA_BUS error for any command
1890 * ECAT_TOUT_HSM : TIMEOUT for any command or HSM violation for
1893 * ECAT_UNK_DEV : Unknown DEV error for IO commands
1895 * ECAT_DUBIOUS_* : Identical to above three but occurred while
1896 * data transfer hasn't been verified.
1900 * NCQ_OFF : Turn off NCQ.
1902 * SPEED_DOWN : Speed down transfer speed but don't fall back
1905 * FALLBACK_TO_PIO : Fall back to PIO.
1907 * Even if multiple verdicts are returned, only one action is
1908 * taken per error. An action triggered by non-DUBIOUS errors
1909 * clears ering, while one triggered by DUBIOUS_* errors doesn't.
1910 * This is to expedite speed down decisions right after device is
1911 * initially configured.
1913 * The following are speed down rules. #1 and #2 deal with
1916 * 1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors
1917 * occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO.
1919 * 2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors
1920 * occurred during last 5 mins, NCQ_OFF.
1922 * 3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors
1923 * occurred during last 5 mins, FALLBACK_TO_PIO
1925 * 4. If more than 3 TOUT_HSM or UNK_DEV errors occurred
1926 * during last 10 mins, NCQ_OFF.
1928 * 5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6
1929 * UNK_DEV errors occurred during last 10 mins, SPEED_DOWN.
1932 * Inherited from caller.
1935 * OR of ATA_EH_SPDN_* flags.
1937 static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
1939 const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
1940 u64 j64 = get_jiffies_64();
1941 struct speed_down_verdict_arg arg;
1942 unsigned int verdict = 0;
1944 /* scan past 5 mins of error history */
1945 memset(&arg, 0, sizeof(arg));
1946 arg.since = j64 - min(j64, j5mins);
1947 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1949 if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] +
1950 arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1)
1951 verdict |= ATA_EH_SPDN_SPEED_DOWN |
1952 ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS;
1954 if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] +
1955 arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1)
1956 verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS;
1958 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1959 arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1960 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
1961 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
1963 /* scan past 10 mins of error history */
1964 memset(&arg, 0, sizeof(arg));
1965 arg.since = j64 - min(j64, j10mins);
1966 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1968 if (arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1969 arg.nr_errors[ATA_ECAT_UNK_DEV] > 3)
1970 verdict |= ATA_EH_SPDN_NCQ_OFF;
1972 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1973 arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 ||
1974 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
1975 verdict |= ATA_EH_SPDN_SPEED_DOWN;
1981 * ata_eh_speed_down - record error and speed down if necessary
1982 * @dev: Failed device
1983 * @eflags: mask of ATA_EFLAG_* flags
1984 * @err_mask: err_mask of the error
1986 * Record error and examine error history to determine whether
1987 * adjusting transmission speed is necessary. It also sets
1988 * transmission limits appropriately if such adjustment is
1992 * Kernel thread context (may sleep).
1995 * Determined recovery action.
1997 static unsigned int ata_eh_speed_down(struct ata_device *dev,
1998 unsigned int eflags, unsigned int err_mask)
2000 struct ata_link *link = ata_dev_phys_link(dev);
2002 unsigned int verdict;
2003 unsigned int action = 0;
2005 /* don't bother if Cat-0 error */
2006 if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0)
2009 /* record error and determine whether speed down is necessary */
2010 ata_ering_record(&dev->ering, eflags, err_mask);
2011 verdict = ata_eh_speed_down_verdict(dev);
2014 if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
2015 (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
2016 ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
2017 dev->flags |= ATA_DFLAG_NCQ_OFF;
2018 ata_dev_warn(dev, "NCQ disabled due to excessive errors\n");
2023 if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
2024 /* speed down SATA link speed if possible */
2025 if (sata_down_spd_limit(link, 0) == 0) {
2026 action |= ATA_EH_RESET;
2030 /* lower transfer mode */
2031 if (dev->spdn_cnt < 2) {
2032 static const int dma_dnxfer_sel[] =
2033 { ATA_DNXFER_DMA, ATA_DNXFER_40C };
2034 static const int pio_dnxfer_sel[] =
2035 { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
2038 if (dev->xfer_shift != ATA_SHIFT_PIO)
2039 sel = dma_dnxfer_sel[dev->spdn_cnt];
2041 sel = pio_dnxfer_sel[dev->spdn_cnt];
2045 if (ata_down_xfermask_limit(dev, sel) == 0) {
2046 action |= ATA_EH_RESET;
2052 /* Fall back to PIO? Slowing down to PIO is meaningless for
2053 * SATA ATA devices. Consider it only for PATA and SATAPI.
2055 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
2056 (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) &&
2057 (dev->xfer_shift != ATA_SHIFT_PIO)) {
2058 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
2060 action |= ATA_EH_RESET;
2067 /* device has been slowed down, blow error history */
2068 if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS))
2069 ata_ering_clear(&dev->ering);
2074 * ata_eh_worth_retry - analyze error and decide whether to retry
2075 * @qc: qc to possibly retry
2077 * Look at the cause of the error and decide if a retry
2078 * might be useful or not. We don't want to retry media errors
2079 * because the drive itself has probably already taken 10-30 seconds
2080 * doing its own internal retries before reporting the failure.
2082 static inline int ata_eh_worth_retry(struct ata_queued_cmd *qc)
2084 if (qc->err_mask & AC_ERR_MEDIA)
2085 return 0; /* don't retry media errors */
2086 if (qc->flags & ATA_QCFLAG_IO)
2087 return 1; /* otherwise retry anything from fs stack */
2088 if (qc->err_mask & AC_ERR_INVALID)
2089 return 0; /* don't retry these */
2090 return qc->err_mask != AC_ERR_DEV; /* retry if not dev error */
2094 * ata_eh_quiet - check if we need to be quiet about a command error
2097 * Look at the qc flags anbd its scsi command request flags to determine
2098 * if we need to be quiet about the command failure.
2100 static inline bool ata_eh_quiet(struct ata_queued_cmd *qc)
2103 qc->scsicmd->request->rq_flags & RQF_QUIET)
2104 qc->flags |= ATA_QCFLAG_QUIET;
2105 return qc->flags & ATA_QCFLAG_QUIET;
2109 * ata_eh_link_autopsy - analyze error and determine recovery action
2110 * @link: host link to perform autopsy on
2112 * Analyze why @link failed and determine which recovery actions
2113 * are needed. This function also sets more detailed AC_ERR_*
2114 * values and fills sense data for ATAPI CHECK SENSE.
2117 * Kernel thread context (may sleep).
2119 static void ata_eh_link_autopsy(struct ata_link *link)
2121 struct ata_port *ap = link->ap;
2122 struct ata_eh_context *ehc = &link->eh_context;
2123 struct ata_queued_cmd *qc;
2124 struct ata_device *dev;
2125 unsigned int all_err_mask = 0, eflags = 0;
2126 int tag, nr_failed = 0, nr_quiet = 0;
2132 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
2135 /* obtain and analyze SError */
2136 rc = sata_scr_read(link, SCR_ERROR, &serror);
2138 ehc->i.serror |= serror;
2139 ata_eh_analyze_serror(link);
2140 } else if (rc != -EOPNOTSUPP) {
2141 /* SError read failed, force reset and probing */
2142 ehc->i.probe_mask |= ATA_ALL_DEVICES;
2143 ehc->i.action |= ATA_EH_RESET;
2144 ehc->i.err_mask |= AC_ERR_OTHER;
2147 /* analyze NCQ failure */
2148 ata_eh_analyze_ncq_error(link);
2150 /* any real error trumps AC_ERR_OTHER */
2151 if (ehc->i.err_mask & ~AC_ERR_OTHER)
2152 ehc->i.err_mask &= ~AC_ERR_OTHER;
2154 all_err_mask |= ehc->i.err_mask;
2156 ata_qc_for_each_raw(ap, qc, tag) {
2157 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
2158 ata_dev_phys_link(qc->dev) != link)
2161 /* inherit upper level err_mask */
2162 qc->err_mask |= ehc->i.err_mask;
2165 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
2167 /* DEV errors are probably spurious in case of ATA_BUS error */
2168 if (qc->err_mask & AC_ERR_ATA_BUS)
2169 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
2172 /* any real error trumps unknown error */
2173 if (qc->err_mask & ~AC_ERR_OTHER)
2174 qc->err_mask &= ~AC_ERR_OTHER;
2177 * SENSE_VALID trumps dev/unknown error and revalidation. Upper
2178 * layers will determine whether the command is worth retrying
2179 * based on the sense data and device class/type. Otherwise,
2180 * determine directly if the command is worth retrying using its
2181 * error mask and flags.
2183 if (qc->flags & ATA_QCFLAG_SENSE_VALID)
2184 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
2185 else if (ata_eh_worth_retry(qc))
2186 qc->flags |= ATA_QCFLAG_RETRY;
2188 /* accumulate error info */
2189 ehc->i.dev = qc->dev;
2190 all_err_mask |= qc->err_mask;
2191 if (qc->flags & ATA_QCFLAG_IO)
2192 eflags |= ATA_EFLAG_IS_IO;
2193 trace_ata_eh_link_autopsy_qc(qc);
2195 /* Count quiet errors */
2196 if (ata_eh_quiet(qc))
2201 /* If all failed commands requested silence, then be quiet */
2202 if (nr_quiet == nr_failed)
2203 ehc->i.flags |= ATA_EHI_QUIET;
2205 /* enforce default EH actions */
2206 if (ap->pflags & ATA_PFLAG_FROZEN ||
2207 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
2208 ehc->i.action |= ATA_EH_RESET;
2209 else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) ||
2210 (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV)))
2211 ehc->i.action |= ATA_EH_REVALIDATE;
2213 /* If we have offending qcs and the associated failed device,
2214 * perform per-dev EH action only on the offending device.
2217 ehc->i.dev_action[ehc->i.dev->devno] |=
2218 ehc->i.action & ATA_EH_PERDEV_MASK;
2219 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
2222 /* propagate timeout to host link */
2223 if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link))
2224 ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT;
2226 /* record error and consider speeding down */
2228 if (!dev && ((ata_link_max_devices(link) == 1 &&
2229 ata_dev_enabled(link->device))))
2233 if (dev->flags & ATA_DFLAG_DUBIOUS_XFER)
2234 eflags |= ATA_EFLAG_DUBIOUS_XFER;
2235 ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask);
2236 trace_ata_eh_link_autopsy(dev, ehc->i.action, all_err_mask);
2242 * ata_eh_autopsy - analyze error and determine recovery action
2243 * @ap: host port to perform autopsy on
2245 * Analyze all links of @ap and determine why they failed and
2246 * which recovery actions are needed.
2249 * Kernel thread context (may sleep).
2251 void ata_eh_autopsy(struct ata_port *ap)
2253 struct ata_link *link;
2255 ata_for_each_link(link, ap, EDGE)
2256 ata_eh_link_autopsy(link);
2258 /* Handle the frigging slave link. Autopsy is done similarly
2259 * but actions and flags are transferred over to the master
2260 * link and handled from there.
2262 if (ap->slave_link) {
2263 struct ata_eh_context *mehc = &ap->link.eh_context;
2264 struct ata_eh_context *sehc = &ap->slave_link->eh_context;
2266 /* transfer control flags from master to slave */
2267 sehc->i.flags |= mehc->i.flags & ATA_EHI_TO_SLAVE_MASK;
2269 /* perform autopsy on the slave link */
2270 ata_eh_link_autopsy(ap->slave_link);
2272 /* transfer actions from slave to master and clear slave */
2273 ata_eh_about_to_do(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
2274 mehc->i.action |= sehc->i.action;
2275 mehc->i.dev_action[1] |= sehc->i.dev_action[1];
2276 mehc->i.flags |= sehc->i.flags;
2277 ata_eh_done(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
2280 /* Autopsy of fanout ports can affect host link autopsy.
2281 * Perform host link autopsy last.
2283 if (sata_pmp_attached(ap))
2284 ata_eh_link_autopsy(&ap->link);
2288 * ata_get_cmd_descript - get description for ATA command
2289 * @command: ATA command code to get description for
2291 * Return a textual description of the given command, or NULL if the
2292 * command is not known.
2297 const char *ata_get_cmd_descript(u8 command)
2299 #ifdef CONFIG_ATA_VERBOSE_ERROR
2305 { ATA_CMD_DEV_RESET, "DEVICE RESET" },
2306 { ATA_CMD_CHK_POWER, "CHECK POWER MODE" },
2307 { ATA_CMD_STANDBY, "STANDBY" },
2308 { ATA_CMD_IDLE, "IDLE" },
2309 { ATA_CMD_EDD, "EXECUTE DEVICE DIAGNOSTIC" },
2310 { ATA_CMD_DOWNLOAD_MICRO, "DOWNLOAD MICROCODE" },
2311 { ATA_CMD_DOWNLOAD_MICRO_DMA, "DOWNLOAD MICROCODE DMA" },
2312 { ATA_CMD_NOP, "NOP" },
2313 { ATA_CMD_FLUSH, "FLUSH CACHE" },
2314 { ATA_CMD_FLUSH_EXT, "FLUSH CACHE EXT" },
2315 { ATA_CMD_ID_ATA, "IDENTIFY DEVICE" },
2316 { ATA_CMD_ID_ATAPI, "IDENTIFY PACKET DEVICE" },
2317 { ATA_CMD_SERVICE, "SERVICE" },
2318 { ATA_CMD_READ, "READ DMA" },
2319 { ATA_CMD_READ_EXT, "READ DMA EXT" },
2320 { ATA_CMD_READ_QUEUED, "READ DMA QUEUED" },
2321 { ATA_CMD_READ_STREAM_EXT, "READ STREAM EXT" },
2322 { ATA_CMD_READ_STREAM_DMA_EXT, "READ STREAM DMA EXT" },
2323 { ATA_CMD_WRITE, "WRITE DMA" },
2324 { ATA_CMD_WRITE_EXT, "WRITE DMA EXT" },
2325 { ATA_CMD_WRITE_QUEUED, "WRITE DMA QUEUED EXT" },
2326 { ATA_CMD_WRITE_STREAM_EXT, "WRITE STREAM EXT" },
2327 { ATA_CMD_WRITE_STREAM_DMA_EXT, "WRITE STREAM DMA EXT" },
2328 { ATA_CMD_WRITE_FUA_EXT, "WRITE DMA FUA EXT" },
2329 { ATA_CMD_WRITE_QUEUED_FUA_EXT, "WRITE DMA QUEUED FUA EXT" },
2330 { ATA_CMD_FPDMA_READ, "READ FPDMA QUEUED" },
2331 { ATA_CMD_FPDMA_WRITE, "WRITE FPDMA QUEUED" },
2332 { ATA_CMD_NCQ_NON_DATA, "NCQ NON-DATA" },
2333 { ATA_CMD_FPDMA_SEND, "SEND FPDMA QUEUED" },
2334 { ATA_CMD_FPDMA_RECV, "RECEIVE FPDMA QUEUED" },
2335 { ATA_CMD_PIO_READ, "READ SECTOR(S)" },
2336 { ATA_CMD_PIO_READ_EXT, "READ SECTOR(S) EXT" },
2337 { ATA_CMD_PIO_WRITE, "WRITE SECTOR(S)" },
2338 { ATA_CMD_PIO_WRITE_EXT, "WRITE SECTOR(S) EXT" },
2339 { ATA_CMD_READ_MULTI, "READ MULTIPLE" },
2340 { ATA_CMD_READ_MULTI_EXT, "READ MULTIPLE EXT" },
2341 { ATA_CMD_WRITE_MULTI, "WRITE MULTIPLE" },
2342 { ATA_CMD_WRITE_MULTI_EXT, "WRITE MULTIPLE EXT" },
2343 { ATA_CMD_WRITE_MULTI_FUA_EXT, "WRITE MULTIPLE FUA EXT" },
2344 { ATA_CMD_SET_FEATURES, "SET FEATURES" },
2345 { ATA_CMD_SET_MULTI, "SET MULTIPLE MODE" },
2346 { ATA_CMD_VERIFY, "READ VERIFY SECTOR(S)" },
2347 { ATA_CMD_VERIFY_EXT, "READ VERIFY SECTOR(S) EXT" },
2348 { ATA_CMD_WRITE_UNCORR_EXT, "WRITE UNCORRECTABLE EXT" },
2349 { ATA_CMD_STANDBYNOW1, "STANDBY IMMEDIATE" },
2350 { ATA_CMD_IDLEIMMEDIATE, "IDLE IMMEDIATE" },
2351 { ATA_CMD_SLEEP, "SLEEP" },
2352 { ATA_CMD_INIT_DEV_PARAMS, "INITIALIZE DEVICE PARAMETERS" },
2353 { ATA_CMD_READ_NATIVE_MAX, "READ NATIVE MAX ADDRESS" },
2354 { ATA_CMD_READ_NATIVE_MAX_EXT, "READ NATIVE MAX ADDRESS EXT" },
2355 { ATA_CMD_SET_MAX, "SET MAX ADDRESS" },
2356 { ATA_CMD_SET_MAX_EXT, "SET MAX ADDRESS EXT" },
2357 { ATA_CMD_READ_LOG_EXT, "READ LOG EXT" },
2358 { ATA_CMD_WRITE_LOG_EXT, "WRITE LOG EXT" },
2359 { ATA_CMD_READ_LOG_DMA_EXT, "READ LOG DMA EXT" },
2360 { ATA_CMD_WRITE_LOG_DMA_EXT, "WRITE LOG DMA EXT" },
2361 { ATA_CMD_TRUSTED_NONDATA, "TRUSTED NON-DATA" },
2362 { ATA_CMD_TRUSTED_RCV, "TRUSTED RECEIVE" },
2363 { ATA_CMD_TRUSTED_RCV_DMA, "TRUSTED RECEIVE DMA" },
2364 { ATA_CMD_TRUSTED_SND, "TRUSTED SEND" },
2365 { ATA_CMD_TRUSTED_SND_DMA, "TRUSTED SEND DMA" },
2366 { ATA_CMD_PMP_READ, "READ BUFFER" },
2367 { ATA_CMD_PMP_READ_DMA, "READ BUFFER DMA" },
2368 { ATA_CMD_PMP_WRITE, "WRITE BUFFER" },
2369 { ATA_CMD_PMP_WRITE_DMA, "WRITE BUFFER DMA" },
2370 { ATA_CMD_CONF_OVERLAY, "DEVICE CONFIGURATION OVERLAY" },
2371 { ATA_CMD_SEC_SET_PASS, "SECURITY SET PASSWORD" },
2372 { ATA_CMD_SEC_UNLOCK, "SECURITY UNLOCK" },
2373 { ATA_CMD_SEC_ERASE_PREP, "SECURITY ERASE PREPARE" },
2374 { ATA_CMD_SEC_ERASE_UNIT, "SECURITY ERASE UNIT" },
2375 { ATA_CMD_SEC_FREEZE_LOCK, "SECURITY FREEZE LOCK" },
2376 { ATA_CMD_SEC_DISABLE_PASS, "SECURITY DISABLE PASSWORD" },
2377 { ATA_CMD_CONFIG_STREAM, "CONFIGURE STREAM" },
2378 { ATA_CMD_SMART, "SMART" },
2379 { ATA_CMD_MEDIA_LOCK, "DOOR LOCK" },
2380 { ATA_CMD_MEDIA_UNLOCK, "DOOR UNLOCK" },
2381 { ATA_CMD_DSM, "DATA SET MANAGEMENT" },
2382 { ATA_CMD_CHK_MED_CRD_TYP, "CHECK MEDIA CARD TYPE" },
2383 { ATA_CMD_CFA_REQ_EXT_ERR, "CFA REQUEST EXTENDED ERROR" },
2384 { ATA_CMD_CFA_WRITE_NE, "CFA WRITE SECTORS WITHOUT ERASE" },
2385 { ATA_CMD_CFA_TRANS_SECT, "CFA TRANSLATE SECTOR" },
2386 { ATA_CMD_CFA_ERASE, "CFA ERASE SECTORS" },
2387 { ATA_CMD_CFA_WRITE_MULT_NE, "CFA WRITE MULTIPLE WITHOUT ERASE" },
2388 { ATA_CMD_REQ_SENSE_DATA, "REQUEST SENSE DATA EXT" },
2389 { ATA_CMD_SANITIZE_DEVICE, "SANITIZE DEVICE" },
2390 { ATA_CMD_ZAC_MGMT_IN, "ZAC MANAGEMENT IN" },
2391 { ATA_CMD_ZAC_MGMT_OUT, "ZAC MANAGEMENT OUT" },
2392 { ATA_CMD_READ_LONG, "READ LONG (with retries)" },
2393 { ATA_CMD_READ_LONG_ONCE, "READ LONG (without retries)" },
2394 { ATA_CMD_WRITE_LONG, "WRITE LONG (with retries)" },
2395 { ATA_CMD_WRITE_LONG_ONCE, "WRITE LONG (without retries)" },
2396 { ATA_CMD_RESTORE, "RECALIBRATE" },
2397 { 0, NULL } /* terminate list */
2401 for (i = 0; cmd_descr[i].text; i++)
2402 if (cmd_descr[i].command == command)
2403 return cmd_descr[i].text;
2408 EXPORT_SYMBOL_GPL(ata_get_cmd_descript);
2411 * ata_eh_link_report - report error handling to user
2412 * @link: ATA link EH is going on
2414 * Report EH to user.
2419 static void ata_eh_link_report(struct ata_link *link)
2421 struct ata_port *ap = link->ap;
2422 struct ata_eh_context *ehc = &link->eh_context;
2423 struct ata_queued_cmd *qc;
2424 const char *frozen, *desc;
2425 char tries_buf[6] = "";
2426 int tag, nr_failed = 0;
2428 if (ehc->i.flags & ATA_EHI_QUIET)
2432 if (ehc->i.desc[0] != '\0')
2435 ata_qc_for_each_raw(ap, qc, tag) {
2436 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
2437 ata_dev_phys_link(qc->dev) != link ||
2438 ((qc->flags & ATA_QCFLAG_QUIET) &&
2439 qc->err_mask == AC_ERR_DEV))
2441 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
2447 if (!nr_failed && !ehc->i.err_mask)
2451 if (ap->pflags & ATA_PFLAG_FROZEN)
2454 if (ap->eh_tries < ATA_EH_MAX_TRIES)
2455 snprintf(tries_buf, sizeof(tries_buf), " t%d",
2459 ata_dev_err(ehc->i.dev, "exception Emask 0x%x "
2460 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2461 ehc->i.err_mask, link->sactive, ehc->i.serror,
2462 ehc->i.action, frozen, tries_buf);
2464 ata_dev_err(ehc->i.dev, "%s\n", desc);
2466 ata_link_err(link, "exception Emask 0x%x "
2467 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2468 ehc->i.err_mask, link->sactive, ehc->i.serror,
2469 ehc->i.action, frozen, tries_buf);
2471 ata_link_err(link, "%s\n", desc);
2474 #ifdef CONFIG_ATA_VERBOSE_ERROR
2477 "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
2478 ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
2479 ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
2480 ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
2481 ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
2482 ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
2483 ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
2484 ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
2485 ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
2486 ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
2487 ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
2488 ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
2489 ehc->i.serror & SERR_CRC ? "BadCRC " : "",
2490 ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
2491 ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
2492 ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
2493 ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
2494 ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
2497 ata_qc_for_each_raw(ap, qc, tag) {
2498 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
2499 char data_buf[20] = "";
2500 char cdb_buf[70] = "";
2502 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
2503 ata_dev_phys_link(qc->dev) != link || !qc->err_mask)
2506 if (qc->dma_dir != DMA_NONE) {
2507 static const char *dma_str[] = {
2508 [DMA_BIDIRECTIONAL] = "bidi",
2509 [DMA_TO_DEVICE] = "out",
2510 [DMA_FROM_DEVICE] = "in",
2512 const char *prot_str = NULL;
2514 switch (qc->tf.protocol) {
2515 case ATA_PROT_UNKNOWN:
2516 prot_str = "unknown";
2518 case ATA_PROT_NODATA:
2519 prot_str = "nodata";
2528 prot_str = "ncq dma";
2530 case ATA_PROT_NCQ_NODATA:
2531 prot_str = "ncq nodata";
2533 case ATAPI_PROT_NODATA:
2534 prot_str = "nodata";
2536 case ATAPI_PROT_PIO:
2539 case ATAPI_PROT_DMA:
2543 snprintf(data_buf, sizeof(data_buf), " %s %u %s",
2544 prot_str, qc->nbytes, dma_str[qc->dma_dir]);
2547 if (ata_is_atapi(qc->tf.protocol)) {
2548 const u8 *cdb = qc->cdb;
2549 size_t cdb_len = qc->dev->cdb_len;
2552 cdb = qc->scsicmd->cmnd;
2553 cdb_len = qc->scsicmd->cmd_len;
2555 __scsi_format_command(cdb_buf, sizeof(cdb_buf),
2558 const char *descr = ata_get_cmd_descript(cmd->command);
2560 ata_dev_err(qc->dev, "failed command: %s\n",
2564 ata_dev_err(qc->dev,
2565 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
2567 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
2568 "Emask 0x%x (%s)%s\n",
2569 cmd->command, cmd->feature, cmd->nsect,
2570 cmd->lbal, cmd->lbam, cmd->lbah,
2571 cmd->hob_feature, cmd->hob_nsect,
2572 cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
2573 cmd->device, qc->tag, data_buf, cdb_buf,
2574 res->command, res->feature, res->nsect,
2575 res->lbal, res->lbam, res->lbah,
2576 res->hob_feature, res->hob_nsect,
2577 res->hob_lbal, res->hob_lbam, res->hob_lbah,
2578 res->device, qc->err_mask, ata_err_string(qc->err_mask),
2579 qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
2581 #ifdef CONFIG_ATA_VERBOSE_ERROR
2582 if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
2583 ATA_SENSE | ATA_ERR)) {
2584 if (res->command & ATA_BUSY)
2585 ata_dev_err(qc->dev, "status: { Busy }\n");
2587 ata_dev_err(qc->dev, "status: { %s%s%s%s%s}\n",
2588 res->command & ATA_DRDY ? "DRDY " : "",
2589 res->command & ATA_DF ? "DF " : "",
2590 res->command & ATA_DRQ ? "DRQ " : "",
2591 res->command & ATA_SENSE ? "SENSE " : "",
2592 res->command & ATA_ERR ? "ERR " : "");
2595 if (cmd->command != ATA_CMD_PACKET &&
2596 (res->feature & (ATA_ICRC | ATA_UNC | ATA_AMNF |
2597 ATA_IDNF | ATA_ABORTED)))
2598 ata_dev_err(qc->dev, "error: { %s%s%s%s%s}\n",
2599 res->feature & ATA_ICRC ? "ICRC " : "",
2600 res->feature & ATA_UNC ? "UNC " : "",
2601 res->feature & ATA_AMNF ? "AMNF " : "",
2602 res->feature & ATA_IDNF ? "IDNF " : "",
2603 res->feature & ATA_ABORTED ? "ABRT " : "");
2609 * ata_eh_report - report error handling to user
2610 * @ap: ATA port to report EH about
2612 * Report EH to user.
2617 void ata_eh_report(struct ata_port *ap)
2619 struct ata_link *link;
2621 ata_for_each_link(link, ap, HOST_FIRST)
2622 ata_eh_link_report(link);
2625 static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
2626 unsigned int *classes, unsigned long deadline,
2629 struct ata_device *dev;
2632 ata_for_each_dev(dev, link, ALL)
2633 classes[dev->devno] = ATA_DEV_UNKNOWN;
2635 return reset(link, classes, deadline);
2638 static int ata_eh_followup_srst_needed(struct ata_link *link, int rc)
2640 if ((link->flags & ATA_LFLAG_NO_SRST) || ata_link_offline(link))
2644 if (sata_pmp_supported(link->ap) && ata_is_host_link(link))
2649 int ata_eh_reset(struct ata_link *link, int classify,
2650 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
2651 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
2653 struct ata_port *ap = link->ap;
2654 struct ata_link *slave = ap->slave_link;
2655 struct ata_eh_context *ehc = &link->eh_context;
2656 struct ata_eh_context *sehc = slave ? &slave->eh_context : NULL;
2657 unsigned int *classes = ehc->classes;
2658 unsigned int lflags = link->flags;
2659 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
2660 int max_tries = 0, try = 0;
2661 struct ata_link *failed_link;
2662 struct ata_device *dev;
2663 unsigned long deadline, now;
2664 ata_reset_fn_t reset;
2665 unsigned long flags;
2672 while (ata_eh_reset_timeouts[max_tries] != ULONG_MAX)
2674 if (link->flags & ATA_LFLAG_RST_ONCE)
2676 if (link->flags & ATA_LFLAG_NO_HRST)
2678 if (link->flags & ATA_LFLAG_NO_SRST)
2681 /* make sure each reset attempt is at least COOL_DOWN apart */
2682 if (ehc->i.flags & ATA_EHI_DID_RESET) {
2684 WARN_ON(time_after(ehc->last_reset, now));
2685 deadline = ata_deadline(ehc->last_reset,
2686 ATA_EH_RESET_COOL_DOWN);
2687 if (time_before(now, deadline))
2688 schedule_timeout_uninterruptible(deadline - now);
2691 spin_lock_irqsave(ap->lock, flags);
2692 ap->pflags |= ATA_PFLAG_RESETTING;
2693 spin_unlock_irqrestore(ap->lock, flags);
2695 ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
2697 ata_for_each_dev(dev, link, ALL) {
2698 /* If we issue an SRST then an ATA drive (not ATAPI)
2699 * may change configuration and be in PIO0 timing. If
2700 * we do a hard reset (or are coming from power on)
2701 * this is true for ATA or ATAPI. Until we've set a
2702 * suitable controller mode we should not touch the
2703 * bus as we may be talking too fast.
2705 dev->pio_mode = XFER_PIO_0;
2706 dev->dma_mode = 0xff;
2708 /* If the controller has a pio mode setup function
2709 * then use it to set the chipset to rights. Don't
2710 * touch the DMA setup as that will be dealt with when
2711 * configuring devices.
2713 if (ap->ops->set_piomode)
2714 ap->ops->set_piomode(ap, dev);
2717 /* prefer hardreset */
2719 ehc->i.action &= ~ATA_EH_RESET;
2722 ehc->i.action |= ATA_EH_HARDRESET;
2723 } else if (softreset) {
2725 ehc->i.action |= ATA_EH_SOFTRESET;
2729 unsigned long deadline = ata_deadline(jiffies,
2730 ATA_EH_PRERESET_TIMEOUT);
2733 sehc->i.action &= ~ATA_EH_RESET;
2734 sehc->i.action |= ehc->i.action;
2737 rc = prereset(link, deadline);
2739 /* If present, do prereset on slave link too. Reset
2740 * is skipped iff both master and slave links report
2741 * -ENOENT or clear ATA_EH_RESET.
2743 if (slave && (rc == 0 || rc == -ENOENT)) {
2746 tmp = prereset(slave, deadline);
2750 ehc->i.action |= sehc->i.action;
2754 if (rc == -ENOENT) {
2755 ata_link_dbg(link, "port disabled--ignoring\n");
2756 ehc->i.action &= ~ATA_EH_RESET;
2758 ata_for_each_dev(dev, link, ALL)
2759 classes[dev->devno] = ATA_DEV_NONE;
2764 "prereset failed (errno=%d)\n",
2769 /* prereset() might have cleared ATA_EH_RESET. If so,
2770 * bang classes, thaw and return.
2772 if (reset && !(ehc->i.action & ATA_EH_RESET)) {
2773 ata_for_each_dev(dev, link, ALL)
2774 classes[dev->devno] = ATA_DEV_NONE;
2775 if ((ap->pflags & ATA_PFLAG_FROZEN) &&
2776 ata_is_host_link(link))
2777 ata_eh_thaw_port(ap);
2787 if (ata_is_host_link(link))
2788 ata_eh_freeze_port(ap);
2790 deadline = ata_deadline(jiffies, ata_eh_reset_timeouts[try++]);
2794 ata_link_info(link, "%s resetting link\n",
2795 reset == softreset ? "soft" : "hard");
2797 /* mark that this EH session started with reset */
2798 ehc->last_reset = jiffies;
2799 if (reset == hardreset)
2800 ehc->i.flags |= ATA_EHI_DID_HARDRESET;
2802 ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
2804 rc = ata_do_reset(link, reset, classes, deadline, true);
2805 if (rc && rc != -EAGAIN) {
2810 /* hardreset slave link if existent */
2811 if (slave && reset == hardreset) {
2815 ata_link_info(slave, "hard resetting link\n");
2817 ata_eh_about_to_do(slave, NULL, ATA_EH_RESET);
2818 tmp = ata_do_reset(slave, reset, classes, deadline,
2826 failed_link = slave;
2832 /* perform follow-up SRST if necessary */
2833 if (reset == hardreset &&
2834 ata_eh_followup_srst_needed(link, rc)) {
2839 "follow-up softreset required but no softreset available\n");
2845 ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
2846 rc = ata_do_reset(link, reset, classes, deadline, true);
2855 "no reset method available, skipping reset\n");
2856 if (!(lflags & ATA_LFLAG_ASSUME_CLASS))
2857 lflags |= ATA_LFLAG_ASSUME_ATA;
2861 * Post-reset processing
2863 ata_for_each_dev(dev, link, ALL) {
2864 /* After the reset, the device state is PIO 0 and the
2865 * controller state is undefined. Reset also wakes up
2866 * drives from sleeping mode.
2868 dev->pio_mode = XFER_PIO_0;
2869 dev->flags &= ~ATA_DFLAG_SLEEPING;
2871 if (ata_phys_link_offline(ata_dev_phys_link(dev)))
2874 /* apply class override */
2875 if (lflags & ATA_LFLAG_ASSUME_ATA)
2876 classes[dev->devno] = ATA_DEV_ATA;
2877 else if (lflags & ATA_LFLAG_ASSUME_SEMB)
2878 classes[dev->devno] = ATA_DEV_SEMB_UNSUP;
2881 /* record current link speed */
2882 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2883 link->sata_spd = (sstatus >> 4) & 0xf;
2884 if (slave && sata_scr_read(slave, SCR_STATUS, &sstatus) == 0)
2885 slave->sata_spd = (sstatus >> 4) & 0xf;
2888 if (ata_is_host_link(link))
2889 ata_eh_thaw_port(ap);
2891 /* postreset() should clear hardware SError. Although SError
2892 * is cleared during link resume, clearing SError here is
2893 * necessary as some PHYs raise hotplug events after SRST.
2894 * This introduces race condition where hotplug occurs between
2895 * reset and here. This race is mediated by cross checking
2896 * link onlineness and classification result later.
2899 postreset(link, classes);
2901 postreset(slave, classes);
2905 * Some controllers can't be frozen very well and may set spurious
2906 * error conditions during reset. Clear accumulated error
2907 * information and re-thaw the port if frozen. As reset is the
2908 * final recovery action and we cross check link onlineness against
2909 * device classification later, no hotplug event is lost by this.
2911 spin_lock_irqsave(link->ap->lock, flags);
2912 memset(&link->eh_info, 0, sizeof(link->eh_info));
2914 memset(&slave->eh_info, 0, sizeof(link->eh_info));
2915 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
2916 spin_unlock_irqrestore(link->ap->lock, flags);
2918 if (ap->pflags & ATA_PFLAG_FROZEN)
2919 ata_eh_thaw_port(ap);
2922 * Make sure onlineness and classification result correspond.
2923 * Hotplug could have happened during reset and some
2924 * controllers fail to wait while a drive is spinning up after
2925 * being hotplugged causing misdetection. By cross checking
2926 * link on/offlineness and classification result, those
2927 * conditions can be reliably detected and retried.
2930 ata_for_each_dev(dev, link, ALL) {
2931 if (ata_phys_link_online(ata_dev_phys_link(dev))) {
2932 if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
2933 ata_dev_dbg(dev, "link online but device misclassified\n");
2934 classes[dev->devno] = ATA_DEV_NONE;
2937 } else if (ata_phys_link_offline(ata_dev_phys_link(dev))) {
2938 if (ata_class_enabled(classes[dev->devno]))
2940 "link offline, clearing class %d to NONE\n",
2941 classes[dev->devno]);
2942 classes[dev->devno] = ATA_DEV_NONE;
2943 } else if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
2945 "link status unknown, clearing UNKNOWN to NONE\n");
2946 classes[dev->devno] = ATA_DEV_NONE;
2950 if (classify && nr_unknown) {
2951 if (try < max_tries) {
2953 "link online but %d devices misclassified, retrying\n",
2960 "link online but %d devices misclassified, "
2961 "device detection might fail\n", nr_unknown);
2964 /* reset successful, schedule revalidation */
2965 ata_eh_done(link, NULL, ATA_EH_RESET);
2967 ata_eh_done(slave, NULL, ATA_EH_RESET);
2968 ehc->last_reset = jiffies; /* update to completion time */
2969 ehc->i.action |= ATA_EH_REVALIDATE;
2970 link->lpm_policy = ATA_LPM_UNKNOWN; /* reset LPM state */
2974 /* clear hotplug flag */
2975 ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2977 sehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2979 spin_lock_irqsave(ap->lock, flags);
2980 ap->pflags &= ~ATA_PFLAG_RESETTING;
2981 spin_unlock_irqrestore(ap->lock, flags);
2986 /* if SCR isn't accessible on a fan-out port, PMP needs to be reset */
2987 if (!ata_is_host_link(link) &&
2988 sata_scr_read(link, SCR_STATUS, &sstatus))
2991 if (try >= max_tries) {
2993 * Thaw host port even if reset failed, so that the port
2994 * can be retried on the next phy event. This risks
2995 * repeated EH runs but seems to be a better tradeoff than
2996 * shutting down a port after a botched hotplug attempt.
2998 if (ata_is_host_link(link))
2999 ata_eh_thaw_port(ap);
3004 if (time_before(now, deadline)) {
3005 unsigned long delta = deadline - now;
3007 ata_link_warn(failed_link,
3008 "reset failed (errno=%d), retrying in %u secs\n",
3009 rc, DIV_ROUND_UP(jiffies_to_msecs(delta), 1000));
3013 delta = schedule_timeout_uninterruptible(delta);
3018 * While disks spinup behind PMP, some controllers fail sending SRST.
3019 * They need to be reset - as well as the PMP - before retrying.
3021 if (rc == -ERESTART) {
3022 if (ata_is_host_link(link))
3023 ata_eh_thaw_port(ap);
3027 if (try == max_tries - 1) {
3028 sata_down_spd_limit(link, 0);
3030 sata_down_spd_limit(slave, 0);
3031 } else if (rc == -EPIPE)
3032 sata_down_spd_limit(failed_link, 0);
3039 static inline void ata_eh_pull_park_action(struct ata_port *ap)
3041 struct ata_link *link;
3042 struct ata_device *dev;
3043 unsigned long flags;
3046 * This function can be thought of as an extended version of
3047 * ata_eh_about_to_do() specially crafted to accommodate the
3048 * requirements of ATA_EH_PARK handling. Since the EH thread
3049 * does not leave the do {} while () loop in ata_eh_recover as
3050 * long as the timeout for a park request to *one* device on
3051 * the port has not expired, and since we still want to pick
3052 * up park requests to other devices on the same port or
3053 * timeout updates for the same device, we have to pull
3054 * ATA_EH_PARK actions from eh_info into eh_context.i
3055 * ourselves at the beginning of each pass over the loop.
3057 * Additionally, all write accesses to &ap->park_req_pending
3058 * through reinit_completion() (see below) or complete_all()
3059 * (see ata_scsi_park_store()) are protected by the host lock.
3060 * As a result we have that park_req_pending.done is zero on
3061 * exit from this function, i.e. when ATA_EH_PARK actions for
3062 * *all* devices on port ap have been pulled into the
3063 * respective eh_context structs. If, and only if,
3064 * park_req_pending.done is non-zero by the time we reach
3065 * wait_for_completion_timeout(), another ATA_EH_PARK action
3066 * has been scheduled for at least one of the devices on port
3067 * ap and we have to cycle over the do {} while () loop in
3068 * ata_eh_recover() again.
3071 spin_lock_irqsave(ap->lock, flags);
3072 reinit_completion(&ap->park_req_pending);
3073 ata_for_each_link(link, ap, EDGE) {
3074 ata_for_each_dev(dev, link, ALL) {
3075 struct ata_eh_info *ehi = &link->eh_info;
3077 link->eh_context.i.dev_action[dev->devno] |=
3078 ehi->dev_action[dev->devno] & ATA_EH_PARK;
3079 ata_eh_clear_action(link, dev, ehi, ATA_EH_PARK);
3082 spin_unlock_irqrestore(ap->lock, flags);
3085 static void ata_eh_park_issue_cmd(struct ata_device *dev, int park)
3087 struct ata_eh_context *ehc = &dev->link->eh_context;
3088 struct ata_taskfile tf;
3089 unsigned int err_mask;
3091 ata_tf_init(dev, &tf);
3093 ehc->unloaded_mask |= 1 << dev->devno;
3094 tf.command = ATA_CMD_IDLEIMMEDIATE;
3100 ehc->unloaded_mask &= ~(1 << dev->devno);
3101 tf.command = ATA_CMD_CHK_POWER;
3104 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
3105 tf.protocol = ATA_PROT_NODATA;
3106 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
3107 if (park && (err_mask || tf.lbal != 0xc4)) {
3108 ata_dev_err(dev, "head unload failed!\n");
3109 ehc->unloaded_mask &= ~(1 << dev->devno);
3113 static int ata_eh_revalidate_and_attach(struct ata_link *link,
3114 struct ata_device **r_failed_dev)
3116 struct ata_port *ap = link->ap;
3117 struct ata_eh_context *ehc = &link->eh_context;
3118 struct ata_device *dev;
3119 unsigned int new_mask = 0;
3120 unsigned long flags;
3125 /* For PATA drive side cable detection to work, IDENTIFY must
3126 * be done backwards such that PDIAG- is released by the slave
3127 * device before the master device is identified.
3129 ata_for_each_dev(dev, link, ALL_REVERSE) {
3130 unsigned int action = ata_eh_dev_action(dev);
3131 unsigned int readid_flags = 0;
3133 if (ehc->i.flags & ATA_EHI_DID_RESET)
3134 readid_flags |= ATA_READID_POSTRESET;
3136 if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
3137 WARN_ON(dev->class == ATA_DEV_PMP);
3139 if (ata_phys_link_offline(ata_dev_phys_link(dev))) {
3144 ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
3145 rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
3150 ata_eh_done(link, dev, ATA_EH_REVALIDATE);
3152 /* Configuration may have changed, reconfigure
3155 ehc->i.flags |= ATA_EHI_SETMODE;
3157 /* schedule the scsi_rescan_device() here */
3158 schedule_work(&(ap->scsi_rescan_task));
3159 } else if (dev->class == ATA_DEV_UNKNOWN &&
3160 ehc->tries[dev->devno] &&
3161 ata_class_enabled(ehc->classes[dev->devno])) {
3162 /* Temporarily set dev->class, it will be
3163 * permanently set once all configurations are
3164 * complete. This is necessary because new
3165 * device configuration is done in two
3168 dev->class = ehc->classes[dev->devno];
3170 if (dev->class == ATA_DEV_PMP)
3171 rc = sata_pmp_attach(dev);
3173 rc = ata_dev_read_id(dev, &dev->class,
3174 readid_flags, dev->id);
3176 /* read_id might have changed class, store and reset */
3177 ehc->classes[dev->devno] = dev->class;
3178 dev->class = ATA_DEV_UNKNOWN;
3182 /* clear error info accumulated during probe */
3183 ata_ering_clear(&dev->ering);
3184 new_mask |= 1 << dev->devno;
3187 /* IDENTIFY was issued to non-existent
3188 * device. No need to reset. Just
3189 * thaw and ignore the device.
3191 ata_eh_thaw_port(ap);
3199 /* PDIAG- should have been released, ask cable type if post-reset */
3200 if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) {
3201 if (ap->ops->cable_detect)
3202 ap->cbl = ap->ops->cable_detect(ap);
3206 /* Configure new devices forward such that user doesn't see
3207 * device detection messages backwards.
3209 ata_for_each_dev(dev, link, ALL) {
3210 if (!(new_mask & (1 << dev->devno)))
3213 dev->class = ehc->classes[dev->devno];
3215 if (dev->class == ATA_DEV_PMP)
3218 ehc->i.flags |= ATA_EHI_PRINTINFO;
3219 rc = ata_dev_configure(dev);
3220 ehc->i.flags &= ~ATA_EHI_PRINTINFO;
3222 dev->class = ATA_DEV_UNKNOWN;
3226 spin_lock_irqsave(ap->lock, flags);
3227 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
3228 spin_unlock_irqrestore(ap->lock, flags);
3230 /* new device discovered, configure xfermode */
3231 ehc->i.flags |= ATA_EHI_SETMODE;
3237 *r_failed_dev = dev;
3238 DPRINTK("EXIT rc=%d\n", rc);
3243 * ata_set_mode - Program timings and issue SET FEATURES - XFER
3244 * @link: link on which timings will be programmed
3245 * @r_failed_dev: out parameter for failed device
3247 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3248 * ata_set_mode() fails, pointer to the failing device is
3249 * returned in @r_failed_dev.
3252 * PCI/etc. bus probe sem.
3255 * 0 on success, negative errno otherwise
3257 int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3259 struct ata_port *ap = link->ap;
3260 struct ata_device *dev;
3263 /* if data transfer is verified, clear DUBIOUS_XFER on ering top */
3264 ata_for_each_dev(dev, link, ENABLED) {
3265 if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
3266 struct ata_ering_entry *ent;
3268 ent = ata_ering_top(&dev->ering);
3270 ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER;
3274 /* has private set_mode? */
3275 if (ap->ops->set_mode)
3276 rc = ap->ops->set_mode(link, r_failed_dev);
3278 rc = ata_do_set_mode(link, r_failed_dev);
3280 /* if transfer mode has changed, set DUBIOUS_XFER on device */
3281 ata_for_each_dev(dev, link, ENABLED) {
3282 struct ata_eh_context *ehc = &link->eh_context;
3283 u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
3284 u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
3286 if (dev->xfer_mode != saved_xfer_mode ||
3287 ata_ncq_enabled(dev) != saved_ncq)
3288 dev->flags |= ATA_DFLAG_DUBIOUS_XFER;
3295 * atapi_eh_clear_ua - Clear ATAPI UNIT ATTENTION after reset
3296 * @dev: ATAPI device to clear UA for
3298 * Resets and other operations can make an ATAPI device raise
3299 * UNIT ATTENTION which causes the next operation to fail. This
3300 * function clears UA.
3303 * EH context (may sleep).
3306 * 0 on success, -errno on failure.
3308 static int atapi_eh_clear_ua(struct ata_device *dev)
3312 for (i = 0; i < ATA_EH_UA_TRIES; i++) {
3313 u8 *sense_buffer = dev->link->ap->sector_buf;
3315 unsigned int err_mask;
3317 err_mask = atapi_eh_tur(dev, &sense_key);
3318 if (err_mask != 0 && err_mask != AC_ERR_DEV) {
3320 "TEST_UNIT_READY failed (err_mask=0x%x)\n",
3325 if (!err_mask || sense_key != UNIT_ATTENTION)
3328 err_mask = atapi_eh_request_sense(dev, sense_buffer, sense_key);
3330 ata_dev_warn(dev, "failed to clear "
3331 "UNIT ATTENTION (err_mask=0x%x)\n", err_mask);
3336 ata_dev_warn(dev, "UNIT ATTENTION persists after %d tries\n",
3343 * ata_eh_maybe_retry_flush - Retry FLUSH if necessary
3344 * @dev: ATA device which may need FLUSH retry
3346 * If @dev failed FLUSH, it needs to be reported upper layer
3347 * immediately as it means that @dev failed to remap and already
3348 * lost at least a sector and further FLUSH retrials won't make
3349 * any difference to the lost sector. However, if FLUSH failed
3350 * for other reasons, for example transmission error, FLUSH needs
3353 * This function determines whether FLUSH failure retry is
3354 * necessary and performs it if so.
3357 * 0 if EH can continue, -errno if EH needs to be repeated.
3359 static int ata_eh_maybe_retry_flush(struct ata_device *dev)
3361 struct ata_link *link = dev->link;
3362 struct ata_port *ap = link->ap;
3363 struct ata_queued_cmd *qc;
3364 struct ata_taskfile tf;
3365 unsigned int err_mask;
3368 /* did flush fail for this device? */
3369 if (!ata_tag_valid(link->active_tag))
3372 qc = __ata_qc_from_tag(ap, link->active_tag);
3373 if (qc->dev != dev || (qc->tf.command != ATA_CMD_FLUSH_EXT &&
3374 qc->tf.command != ATA_CMD_FLUSH))
3377 /* if the device failed it, it should be reported to upper layers */
3378 if (qc->err_mask & AC_ERR_DEV)
3381 /* flush failed for some other reason, give it another shot */
3382 ata_tf_init(dev, &tf);
3384 tf.command = qc->tf.command;
3385 tf.flags |= ATA_TFLAG_DEVICE;
3386 tf.protocol = ATA_PROT_NODATA;
3388 ata_dev_warn(dev, "retrying FLUSH 0x%x Emask 0x%x\n",
3389 tf.command, qc->err_mask);
3391 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
3394 * FLUSH is complete but there's no way to
3395 * successfully complete a failed command from EH.
3396 * Making sure retry is allowed at least once and
3397 * retrying it should do the trick - whatever was in
3398 * the cache is already on the platter and this won't
3399 * cause infinite loop.
3401 qc->scsicmd->allowed = max(qc->scsicmd->allowed, 1);
3403 ata_dev_warn(dev, "FLUSH failed Emask 0x%x\n",
3407 /* if device failed it, report it to upper layers */
3408 if (err_mask & AC_ERR_DEV) {
3409 qc->err_mask |= AC_ERR_DEV;
3411 if (!(ap->pflags & ATA_PFLAG_FROZEN))
3419 * ata_eh_set_lpm - configure SATA interface power management
3420 * @link: link to configure power management
3421 * @policy: the link power management policy
3422 * @r_failed_dev: out parameter for failed device
3424 * Enable SATA Interface power management. This will enable
3425 * Device Interface Power Management (DIPM) for min_power and
3426 * medium_power_with_dipm policies, and then call driver specific
3427 * callbacks for enabling Host Initiated Power management.
3433 * 0 on success, -errno on failure.
3435 static int ata_eh_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
3436 struct ata_device **r_failed_dev)
3438 struct ata_port *ap = ata_is_host_link(link) ? link->ap : NULL;
3439 struct ata_eh_context *ehc = &link->eh_context;
3440 struct ata_device *dev, *link_dev = NULL, *lpm_dev = NULL;
3441 enum ata_lpm_policy old_policy = link->lpm_policy;
3442 bool no_dipm = link->ap->flags & ATA_FLAG_NO_DIPM;
3443 unsigned int hints = ATA_LPM_EMPTY | ATA_LPM_HIPM;
3444 unsigned int err_mask;
3447 /* if the link or host doesn't do LPM, noop */
3448 if ((link->flags & ATA_LFLAG_NO_LPM) || (ap && !ap->ops->set_lpm))
3452 * DIPM is enabled only for MIN_POWER as some devices
3453 * misbehave when the host NACKs transition to SLUMBER. Order
3454 * device and link configurations such that the host always
3455 * allows DIPM requests.
3457 ata_for_each_dev(dev, link, ENABLED) {
3458 bool hipm = ata_id_has_hipm(dev->id);
3459 bool dipm = ata_id_has_dipm(dev->id) && !no_dipm;
3461 /* find the first enabled and LPM enabled devices */
3465 if (!lpm_dev && (hipm || dipm))
3468 hints &= ~ATA_LPM_EMPTY;
3470 hints &= ~ATA_LPM_HIPM;
3472 /* disable DIPM before changing link config */
3473 if (policy < ATA_LPM_MED_POWER_WITH_DIPM && dipm) {
3474 err_mask = ata_dev_set_feature(dev,
3475 SETFEATURES_SATA_DISABLE, SATA_DIPM);
3476 if (err_mask && err_mask != AC_ERR_DEV) {
3478 "failed to disable DIPM, Emask 0x%x\n",
3487 rc = ap->ops->set_lpm(link, policy, hints);
3488 if (!rc && ap->slave_link)
3489 rc = ap->ops->set_lpm(ap->slave_link, policy, hints);
3491 rc = sata_pmp_set_lpm(link, policy, hints);
3494 * Attribute link config failure to the first (LPM) enabled
3495 * device on the link.
3498 if (rc == -EOPNOTSUPP) {
3499 link->flags |= ATA_LFLAG_NO_LPM;
3502 dev = lpm_dev ? lpm_dev : link_dev;
3507 * Low level driver acked the transition. Issue DIPM command
3508 * with the new policy set.
3510 link->lpm_policy = policy;
3511 if (ap && ap->slave_link)
3512 ap->slave_link->lpm_policy = policy;
3514 /* host config updated, enable DIPM if transitioning to MIN_POWER */
3515 ata_for_each_dev(dev, link, ENABLED) {
3516 if (policy >= ATA_LPM_MED_POWER_WITH_DIPM && !no_dipm &&
3517 ata_id_has_dipm(dev->id)) {
3518 err_mask = ata_dev_set_feature(dev,
3519 SETFEATURES_SATA_ENABLE, SATA_DIPM);
3520 if (err_mask && err_mask != AC_ERR_DEV) {
3522 "failed to enable DIPM, Emask 0x%x\n",
3530 link->last_lpm_change = jiffies;
3531 link->flags |= ATA_LFLAG_CHANGED;
3536 /* restore the old policy */
3537 link->lpm_policy = old_policy;
3538 if (ap && ap->slave_link)
3539 ap->slave_link->lpm_policy = old_policy;
3541 /* if no device or only one more chance is left, disable LPM */
3542 if (!dev || ehc->tries[dev->devno] <= 2) {
3543 ata_link_warn(link, "disabling LPM on the link\n");
3544 link->flags |= ATA_LFLAG_NO_LPM;
3547 *r_failed_dev = dev;
3551 int ata_link_nr_enabled(struct ata_link *link)
3553 struct ata_device *dev;
3556 ata_for_each_dev(dev, link, ENABLED)
3561 static int ata_link_nr_vacant(struct ata_link *link)
3563 struct ata_device *dev;
3566 ata_for_each_dev(dev, link, ALL)
3567 if (dev->class == ATA_DEV_UNKNOWN)
3572 static int ata_eh_skip_recovery(struct ata_link *link)
3574 struct ata_port *ap = link->ap;
3575 struct ata_eh_context *ehc = &link->eh_context;
3576 struct ata_device *dev;
3578 /* skip disabled links */
3579 if (link->flags & ATA_LFLAG_DISABLED)
3582 /* skip if explicitly requested */
3583 if (ehc->i.flags & ATA_EHI_NO_RECOVERY)
3586 /* thaw frozen port and recover failed devices */
3587 if ((ap->pflags & ATA_PFLAG_FROZEN) || ata_link_nr_enabled(link))
3590 /* reset at least once if reset is requested */
3591 if ((ehc->i.action & ATA_EH_RESET) &&
3592 !(ehc->i.flags & ATA_EHI_DID_RESET))
3595 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
3596 ata_for_each_dev(dev, link, ALL) {
3597 if (dev->class == ATA_DEV_UNKNOWN &&
3598 ehc->classes[dev->devno] != ATA_DEV_NONE)
3605 static int ata_count_probe_trials_cb(struct ata_ering_entry *ent, void *void_arg)
3607 u64 interval = msecs_to_jiffies(ATA_EH_PROBE_TRIAL_INTERVAL);
3608 u64 now = get_jiffies_64();
3609 int *trials = void_arg;
3611 if ((ent->eflags & ATA_EFLAG_OLD_ER) ||
3612 (ent->timestamp < now - min(now, interval)))
3619 static int ata_eh_schedule_probe(struct ata_device *dev)
3621 struct ata_eh_context *ehc = &dev->link->eh_context;
3622 struct ata_link *link = ata_dev_phys_link(dev);
3625 if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
3626 (ehc->did_probe_mask & (1 << dev->devno)))
3629 ata_eh_detach_dev(dev);
3631 ehc->did_probe_mask |= (1 << dev->devno);
3632 ehc->i.action |= ATA_EH_RESET;
3633 ehc->saved_xfer_mode[dev->devno] = 0;
3634 ehc->saved_ncq_enabled &= ~(1 << dev->devno);
3636 /* the link maybe in a deep sleep, wake it up */
3637 if (link->lpm_policy > ATA_LPM_MAX_POWER) {
3638 if (ata_is_host_link(link))
3639 link->ap->ops->set_lpm(link, ATA_LPM_MAX_POWER,
3642 sata_pmp_set_lpm(link, ATA_LPM_MAX_POWER,
3646 /* Record and count probe trials on the ering. The specific
3647 * error mask used is irrelevant. Because a successful device
3648 * detection clears the ering, this count accumulates only if
3649 * there are consecutive failed probes.
3651 * If the count is equal to or higher than ATA_EH_PROBE_TRIALS
3652 * in the last ATA_EH_PROBE_TRIAL_INTERVAL, link speed is
3653 * forced to 1.5Gbps.
3655 * This is to work around cases where failed link speed
3656 * negotiation results in device misdetection leading to
3657 * infinite DEVXCHG or PHRDY CHG events.
3659 ata_ering_record(&dev->ering, 0, AC_ERR_OTHER);
3660 ata_ering_map(&dev->ering, ata_count_probe_trials_cb, &trials);
3662 if (trials > ATA_EH_PROBE_TRIALS)
3663 sata_down_spd_limit(link, 1);
3668 static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
3670 struct ata_eh_context *ehc = &dev->link->eh_context;
3672 /* -EAGAIN from EH routine indicates retry without prejudice.
3673 * The requester is responsible for ensuring forward progress.
3676 ehc->tries[dev->devno]--;
3680 /* device missing or wrong IDENTIFY data, schedule probing */
3681 ehc->i.probe_mask |= (1 << dev->devno);
3684 /* give it just one more chance */
3685 ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
3688 if (ehc->tries[dev->devno] == 1) {
3689 /* This is the last chance, better to slow
3690 * down than lose it.
3692 sata_down_spd_limit(ata_dev_phys_link(dev), 0);
3693 if (dev->pio_mode > XFER_PIO_0)
3694 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
3698 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
3699 /* disable device if it has used up all its chances */
3700 ata_dev_disable(dev);
3702 /* detach if offline */
3703 if (ata_phys_link_offline(ata_dev_phys_link(dev)))
3704 ata_eh_detach_dev(dev);
3706 /* schedule probe if necessary */
3707 if (ata_eh_schedule_probe(dev)) {
3708 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
3709 memset(ehc->cmd_timeout_idx[dev->devno], 0,
3710 sizeof(ehc->cmd_timeout_idx[dev->devno]));
3715 ehc->i.action |= ATA_EH_RESET;
3721 * ata_eh_recover - recover host port after error
3722 * @ap: host port to recover
3723 * @prereset: prereset method (can be NULL)
3724 * @softreset: softreset method (can be NULL)
3725 * @hardreset: hardreset method (can be NULL)
3726 * @postreset: postreset method (can be NULL)
3727 * @r_failed_link: out parameter for failed link
3729 * This is the alpha and omega, eum and yang, heart and soul of
3730 * libata exception handling. On entry, actions required to
3731 * recover each link and hotplug requests are recorded in the
3732 * link's eh_context. This function executes all the operations
3733 * with appropriate retrials and fallbacks to resurrect failed
3734 * devices, detach goners and greet newcomers.
3737 * Kernel thread context (may sleep).
3740 * 0 on success, -errno on failure.
3742 int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
3743 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
3744 ata_postreset_fn_t postreset,
3745 struct ata_link **r_failed_link)
3747 struct ata_link *link;
3748 struct ata_device *dev;
3750 unsigned long flags, deadline;
3754 /* prep for recovery */
3755 ata_for_each_link(link, ap, EDGE) {
3756 struct ata_eh_context *ehc = &link->eh_context;
3758 /* re-enable link? */
3759 if (ehc->i.action & ATA_EH_ENABLE_LINK) {
3760 ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
3761 spin_lock_irqsave(ap->lock, flags);
3762 link->flags &= ~ATA_LFLAG_DISABLED;
3763 spin_unlock_irqrestore(ap->lock, flags);
3764 ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
3767 ata_for_each_dev(dev, link, ALL) {
3768 if (link->flags & ATA_LFLAG_NO_RETRY)
3769 ehc->tries[dev->devno] = 1;
3771 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
3773 /* collect port action mask recorded in dev actions */
3774 ehc->i.action |= ehc->i.dev_action[dev->devno] &
3775 ~ATA_EH_PERDEV_MASK;
3776 ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
3778 /* process hotplug request */
3779 if (dev->flags & ATA_DFLAG_DETACH)
3780 ata_eh_detach_dev(dev);
3782 /* schedule probe if necessary */
3783 if (!ata_dev_enabled(dev))
3784 ata_eh_schedule_probe(dev);
3791 /* if UNLOADING, finish immediately */
3792 if (ap->pflags & ATA_PFLAG_UNLOADING)
3796 ata_for_each_link(link, ap, EDGE) {
3797 struct ata_eh_context *ehc = &link->eh_context;
3799 /* skip EH if possible. */
3800 if (ata_eh_skip_recovery(link))
3803 ata_for_each_dev(dev, link, ALL)
3804 ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
3808 ata_for_each_link(link, ap, EDGE) {
3809 struct ata_eh_context *ehc = &link->eh_context;
3811 if (!(ehc->i.action & ATA_EH_RESET))
3814 rc = ata_eh_reset(link, ata_link_nr_vacant(link),
3815 prereset, softreset, hardreset, postreset);
3817 ata_link_err(link, "reset failed, giving up\n");
3826 * clears ATA_EH_PARK in eh_info and resets
3827 * ap->park_req_pending
3829 ata_eh_pull_park_action(ap);
3832 ata_for_each_link(link, ap, EDGE) {
3833 ata_for_each_dev(dev, link, ALL) {
3834 struct ata_eh_context *ehc = &link->eh_context;
3837 if (dev->class != ATA_DEV_ATA &&
3838 dev->class != ATA_DEV_ZAC)
3840 if (!(ehc->i.dev_action[dev->devno] &
3843 tmp = dev->unpark_deadline;
3844 if (time_before(deadline, tmp))
3846 else if (time_before_eq(tmp, jiffies))
3848 if (ehc->unloaded_mask & (1 << dev->devno))
3851 ata_eh_park_issue_cmd(dev, 1);
3856 if (time_before_eq(deadline, now))
3860 deadline = wait_for_completion_timeout(&ap->park_req_pending,
3864 ata_for_each_link(link, ap, EDGE) {
3865 ata_for_each_dev(dev, link, ALL) {
3866 if (!(link->eh_context.unloaded_mask &
3870 ata_eh_park_issue_cmd(dev, 0);
3871 ata_eh_done(link, dev, ATA_EH_PARK);
3877 ata_for_each_link(link, ap, PMP_FIRST) {
3878 struct ata_eh_context *ehc = &link->eh_context;
3880 if (sata_pmp_attached(ap) && ata_is_host_link(link))
3883 /* revalidate existing devices and attach new ones */
3884 rc = ata_eh_revalidate_and_attach(link, &dev);
3888 /* if PMP got attached, return, pmp EH will take care of it */
3889 if (link->device->class == ATA_DEV_PMP) {
3894 /* configure transfer mode if necessary */
3895 if (ehc->i.flags & ATA_EHI_SETMODE) {
3896 rc = ata_set_mode(link, &dev);
3899 ehc->i.flags &= ~ATA_EHI_SETMODE;
3902 /* If reset has been issued, clear UA to avoid
3903 * disrupting the current users of the device.
3905 if (ehc->i.flags & ATA_EHI_DID_RESET) {
3906 ata_for_each_dev(dev, link, ALL) {
3907 if (dev->class != ATA_DEV_ATAPI)
3909 rc = atapi_eh_clear_ua(dev);
3912 if (zpodd_dev_enabled(dev))
3913 zpodd_post_poweron(dev);
3917 /* retry flush if necessary */
3918 ata_for_each_dev(dev, link, ALL) {
3919 if (dev->class != ATA_DEV_ATA &&
3920 dev->class != ATA_DEV_ZAC)
3922 rc = ata_eh_maybe_retry_flush(dev);
3928 /* configure link power saving */
3929 if (link->lpm_policy != ap->target_lpm_policy) {
3930 rc = ata_eh_set_lpm(link, ap->target_lpm_policy, &dev);
3935 /* this link is okay now */
3942 ata_eh_handle_dev_fail(dev, rc);
3944 if (ap->pflags & ATA_PFLAG_FROZEN) {
3945 /* PMP reset requires working host port.
3946 * Can't retry if it's frozen.
3948 if (sata_pmp_attached(ap))
3958 if (rc && r_failed_link)
3959 *r_failed_link = link;
3961 DPRINTK("EXIT, rc=%d\n", rc);
3966 * ata_eh_finish - finish up EH
3967 * @ap: host port to finish EH for
3969 * Recovery is complete. Clean up EH states and retry or finish
3975 void ata_eh_finish(struct ata_port *ap)
3977 struct ata_queued_cmd *qc;
3980 /* retry or finish qcs */
3981 ata_qc_for_each_raw(ap, qc, tag) {
3982 if (!(qc->flags & ATA_QCFLAG_FAILED))
3986 /* FIXME: Once EH migration is complete,
3987 * generate sense data in this function,
3988 * considering both err_mask and tf.
3990 if (qc->flags & ATA_QCFLAG_RETRY)
3991 ata_eh_qc_retry(qc);
3993 ata_eh_qc_complete(qc);
3995 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
3996 ata_eh_qc_complete(qc);
3998 /* feed zero TF to sense generation */
3999 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
4000 ata_eh_qc_retry(qc);
4005 /* make sure nr_active_links is zero after EH */
4006 WARN_ON(ap->nr_active_links);
4007 ap->nr_active_links = 0;
4011 * ata_do_eh - do standard error handling
4012 * @ap: host port to handle error for
4014 * @prereset: prereset method (can be NULL)
4015 * @softreset: softreset method (can be NULL)
4016 * @hardreset: hardreset method (can be NULL)
4017 * @postreset: postreset method (can be NULL)
4019 * Perform standard error handling sequence.
4022 * Kernel thread context (may sleep).
4024 void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
4025 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
4026 ata_postreset_fn_t postreset)
4028 struct ata_device *dev;
4034 rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
4037 ata_for_each_dev(dev, &ap->link, ALL)
4038 ata_dev_disable(dev);
4045 * ata_std_error_handler - standard error handler
4046 * @ap: host port to handle error for
4048 * Standard error handler
4051 * Kernel thread context (may sleep).
4053 void ata_std_error_handler(struct ata_port *ap)
4055 struct ata_port_operations *ops = ap->ops;
4056 ata_reset_fn_t hardreset = ops->hardreset;
4058 /* ignore built-in hardreset if SCR access is not available */
4059 if (hardreset == sata_std_hardreset && !sata_scr_valid(&ap->link))
4062 ata_do_eh(ap, ops->prereset, ops->softreset, hardreset, ops->postreset);
4067 * ata_eh_handle_port_suspend - perform port suspend operation
4068 * @ap: port to suspend
4073 * Kernel thread context (may sleep).
4075 static void ata_eh_handle_port_suspend(struct ata_port *ap)
4077 unsigned long flags;
4079 struct ata_device *dev;
4081 /* are we suspending? */
4082 spin_lock_irqsave(ap->lock, flags);
4083 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
4084 ap->pm_mesg.event & PM_EVENT_RESUME) {
4085 spin_unlock_irqrestore(ap->lock, flags);
4088 spin_unlock_irqrestore(ap->lock, flags);
4090 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
4093 * If we have a ZPODD attached, check its zero
4094 * power ready status before the port is frozen.
4095 * Only needed for runtime suspend.
4097 if (PMSG_IS_AUTO(ap->pm_mesg)) {
4098 ata_for_each_dev(dev, &ap->link, ENABLED) {
4099 if (zpodd_dev_enabled(dev))
4100 zpodd_on_suspend(dev);
4104 /* tell ACPI we're suspending */
4105 rc = ata_acpi_on_suspend(ap);
4110 ata_eh_freeze_port(ap);
4112 if (ap->ops->port_suspend)
4113 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
4115 ata_acpi_set_state(ap, ap->pm_mesg);
4117 /* update the flags */
4118 spin_lock_irqsave(ap->lock, flags);
4120 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
4122 ap->pflags |= ATA_PFLAG_SUSPENDED;
4123 else if (ap->pflags & ATA_PFLAG_FROZEN)
4124 ata_port_schedule_eh(ap);
4126 spin_unlock_irqrestore(ap->lock, flags);
4132 * ata_eh_handle_port_resume - perform port resume operation
4133 * @ap: port to resume
4138 * Kernel thread context (may sleep).
4140 static void ata_eh_handle_port_resume(struct ata_port *ap)
4142 struct ata_link *link;
4143 struct ata_device *dev;
4144 unsigned long flags;
4146 /* are we resuming? */
4147 spin_lock_irqsave(ap->lock, flags);
4148 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
4149 !(ap->pm_mesg.event & PM_EVENT_RESUME)) {
4150 spin_unlock_irqrestore(ap->lock, flags);
4153 spin_unlock_irqrestore(ap->lock, flags);
4155 WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
4158 * Error timestamps are in jiffies which doesn't run while
4159 * suspended and PHY events during resume isn't too uncommon.
4160 * When the two are combined, it can lead to unnecessary speed
4161 * downs if the machine is suspended and resumed repeatedly.
4162 * Clear error history.
4164 ata_for_each_link(link, ap, HOST_FIRST)
4165 ata_for_each_dev(dev, link, ALL)
4166 ata_ering_clear(&dev->ering);
4168 ata_acpi_set_state(ap, ap->pm_mesg);
4170 if (ap->ops->port_resume)
4171 ap->ops->port_resume(ap);
4173 /* tell ACPI that we're resuming */
4174 ata_acpi_on_resume(ap);
4176 /* update the flags */
4177 spin_lock_irqsave(ap->lock, flags);
4178 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
4179 spin_unlock_irqrestore(ap->lock, flags);
4181 #endif /* CONFIG_PM */