1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * libata-eh.c - libata error handling
5 * Copyright 2006 Tejun Heo <htejun@gmail.com>
7 * libata documentation is available via 'make {ps|pdf}docs',
8 * as Documentation/driver-api/libata.rst
10 * Hardware documentation available from http://www.t13.org/ and
11 * http://www.sata-io.org/
14 #include <linux/kernel.h>
15 #include <linux/blkdev.h>
16 #include <linux/export.h>
17 #include <linux/pci.h>
18 #include <scsi/scsi.h>
19 #include <scsi/scsi_host.h>
20 #include <scsi/scsi_eh.h>
21 #include <scsi/scsi_device.h>
22 #include <scsi/scsi_cmnd.h>
23 #include <scsi/scsi_dbg.h>
24 #include "../scsi/scsi_transport_api.h"
26 #include <linux/libata.h>
28 #include <trace/events/libata.h>
32 /* speed down verdicts */
33 ATA_EH_SPDN_NCQ_OFF = (1 << 0),
34 ATA_EH_SPDN_SPEED_DOWN = (1 << 1),
35 ATA_EH_SPDN_FALLBACK_TO_PIO = (1 << 2),
36 ATA_EH_SPDN_KEEP_ERRORS = (1 << 3),
39 ATA_EFLAG_IS_IO = (1 << 0),
40 ATA_EFLAG_DUBIOUS_XFER = (1 << 1),
41 ATA_EFLAG_OLD_ER = (1 << 31),
43 /* error categories */
46 ATA_ECAT_TOUT_HSM = 2,
48 ATA_ECAT_DUBIOUS_NONE = 4,
49 ATA_ECAT_DUBIOUS_ATA_BUS = 5,
50 ATA_ECAT_DUBIOUS_TOUT_HSM = 6,
51 ATA_ECAT_DUBIOUS_UNK_DEV = 7,
54 ATA_EH_CMD_DFL_TIMEOUT = 5000,
56 /* always put at least this amount of time between resets */
57 ATA_EH_RESET_COOL_DOWN = 5000,
59 /* Waiting in ->prereset can never be reliable. It's
60 * sometimes nice to wait there but it can't be depended upon;
61 * otherwise, we wouldn't be resetting. Just give it enough
62 * time for most drives to spin up.
64 ATA_EH_PRERESET_TIMEOUT = 10000,
65 ATA_EH_FASTDRAIN_INTERVAL = 3000,
69 /* probe speed down parameters, see ata_eh_schedule_probe() */
70 ATA_EH_PROBE_TRIAL_INTERVAL = 60000, /* 1 min */
71 ATA_EH_PROBE_TRIALS = 2,
74 /* The following table determines how we sequence resets. Each entry
75 * represents timeout for that try. The first try can be soft or
76 * hardreset. All others are hardreset if available. In most cases
77 * the first reset w/ 10sec timeout should succeed. Following entries
78 * are mostly for error handling, hotplug and those outlier devices that
79 * take an exceptionally long time to recover from reset.
81 static const unsigned long ata_eh_reset_timeouts[] = {
82 10000, /* most drives spin up by 10sec */
83 10000, /* > 99% working drives spin up before 20sec */
84 35000, /* give > 30 secs of idleness for outlier devices */
85 5000, /* and sweet one last chance */
86 ULONG_MAX, /* > 1 min has elapsed, give up */
89 static const unsigned long ata_eh_identify_timeouts[] = {
90 5000, /* covers > 99% of successes and not too boring on failures */
91 10000, /* combined time till here is enough even for media access */
92 30000, /* for true idiots */
96 static const unsigned long ata_eh_flush_timeouts[] = {
97 15000, /* be generous with flush */
99 30000, /* and even more generous */
103 static const unsigned long ata_eh_other_timeouts[] = {
104 5000, /* same rationale as identify timeout */
106 /* but no merciful 30sec for other commands, it just isn't worth it */
110 struct ata_eh_cmd_timeout_ent {
112 const unsigned long *timeouts;
115 /* The following table determines timeouts to use for EH internal
116 * commands. Each table entry is a command class and matches the
117 * commands the entry applies to and the timeout table to use.
119 * On the retry after a command timed out, the next timeout value from
120 * the table is used. If the table doesn't contain further entries,
121 * the last value is used.
123 * ehc->cmd_timeout_idx keeps track of which timeout to use per
124 * command class, so if SET_FEATURES times out on the first try, the
125 * next try will use the second timeout value only for that class.
127 #define CMDS(cmds...) (const u8 []){ cmds, 0 }
128 static const struct ata_eh_cmd_timeout_ent
129 ata_eh_cmd_timeout_table[ATA_EH_CMD_TIMEOUT_TABLE_SIZE] = {
130 { .commands = CMDS(ATA_CMD_ID_ATA, ATA_CMD_ID_ATAPI),
131 .timeouts = ata_eh_identify_timeouts, },
132 { .commands = CMDS(ATA_CMD_READ_NATIVE_MAX, ATA_CMD_READ_NATIVE_MAX_EXT),
133 .timeouts = ata_eh_other_timeouts, },
134 { .commands = CMDS(ATA_CMD_SET_MAX, ATA_CMD_SET_MAX_EXT),
135 .timeouts = ata_eh_other_timeouts, },
136 { .commands = CMDS(ATA_CMD_SET_FEATURES),
137 .timeouts = ata_eh_other_timeouts, },
138 { .commands = CMDS(ATA_CMD_INIT_DEV_PARAMS),
139 .timeouts = ata_eh_other_timeouts, },
140 { .commands = CMDS(ATA_CMD_FLUSH, ATA_CMD_FLUSH_EXT),
141 .timeouts = ata_eh_flush_timeouts },
145 static void __ata_port_freeze(struct ata_port *ap);
147 static void ata_eh_handle_port_suspend(struct ata_port *ap);
148 static void ata_eh_handle_port_resume(struct ata_port *ap);
149 #else /* CONFIG_PM */
150 static void ata_eh_handle_port_suspend(struct ata_port *ap)
153 static void ata_eh_handle_port_resume(struct ata_port *ap)
155 #endif /* CONFIG_PM */
157 static __printf(2, 0) void __ata_ehi_pushv_desc(struct ata_eh_info *ehi,
158 const char *fmt, va_list args)
160 ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
161 ATA_EH_DESC_LEN - ehi->desc_len,
166 * __ata_ehi_push_desc - push error description without adding separator
168 * @fmt: printf format string
170 * Format string according to @fmt and append it to @ehi->desc.
173 * spin_lock_irqsave(host lock)
175 void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
180 __ata_ehi_pushv_desc(ehi, fmt, args);
185 * ata_ehi_push_desc - push error description with separator
187 * @fmt: printf format string
189 * Format string according to @fmt and append it to @ehi->desc.
190 * If @ehi->desc is not empty, ", " is added in-between.
193 * spin_lock_irqsave(host lock)
195 void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
200 __ata_ehi_push_desc(ehi, ", ");
203 __ata_ehi_pushv_desc(ehi, fmt, args);
208 * ata_ehi_clear_desc - clean error description
214 * spin_lock_irqsave(host lock)
216 void ata_ehi_clear_desc(struct ata_eh_info *ehi)
223 * ata_port_desc - append port description
224 * @ap: target ATA port
225 * @fmt: printf format string
227 * Format string according to @fmt and append it to port
228 * description. If port description is not empty, " " is added
229 * in-between. This function is to be used while initializing
230 * ata_host. The description is printed on host registration.
235 void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
239 WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
241 if (ap->link.eh_info.desc_len)
242 __ata_ehi_push_desc(&ap->link.eh_info, " ");
245 __ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
252 * ata_port_pbar_desc - append PCI BAR description
253 * @ap: target ATA port
254 * @bar: target PCI BAR
255 * @offset: offset into PCI BAR
256 * @name: name of the area
258 * If @offset is negative, this function formats a string which
259 * contains the name, address, size and type of the BAR and
260 * appends it to the port description. If @offset is zero or
261 * positive, only name and offsetted address is appended.
266 void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
269 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
271 unsigned long long start, len;
273 if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
275 else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
278 start = (unsigned long long)pci_resource_start(pdev, bar);
279 len = (unsigned long long)pci_resource_len(pdev, bar);
282 ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
284 ata_port_desc(ap, "%s 0x%llx", name,
285 start + (unsigned long long)offset);
288 #endif /* CONFIG_PCI */
290 static int ata_lookup_timeout_table(u8 cmd)
294 for (i = 0; i < ATA_EH_CMD_TIMEOUT_TABLE_SIZE; i++) {
297 for (cur = ata_eh_cmd_timeout_table[i].commands; *cur; cur++)
306 * ata_internal_cmd_timeout - determine timeout for an internal command
307 * @dev: target device
308 * @cmd: internal command to be issued
310 * Determine timeout for internal command @cmd for @dev.
316 * Determined timeout.
318 unsigned long ata_internal_cmd_timeout(struct ata_device *dev, u8 cmd)
320 struct ata_eh_context *ehc = &dev->link->eh_context;
321 int ent = ata_lookup_timeout_table(cmd);
325 return ATA_EH_CMD_DFL_TIMEOUT;
327 idx = ehc->cmd_timeout_idx[dev->devno][ent];
328 return ata_eh_cmd_timeout_table[ent].timeouts[idx];
332 * ata_internal_cmd_timed_out - notification for internal command timeout
333 * @dev: target device
334 * @cmd: internal command which timed out
336 * Notify EH that internal command @cmd for @dev timed out. This
337 * function should be called only for commands whose timeouts are
338 * determined using ata_internal_cmd_timeout().
343 void ata_internal_cmd_timed_out(struct ata_device *dev, u8 cmd)
345 struct ata_eh_context *ehc = &dev->link->eh_context;
346 int ent = ata_lookup_timeout_table(cmd);
352 idx = ehc->cmd_timeout_idx[dev->devno][ent];
353 if (ata_eh_cmd_timeout_table[ent].timeouts[idx + 1] != ULONG_MAX)
354 ehc->cmd_timeout_idx[dev->devno][ent]++;
357 static void ata_ering_record(struct ata_ering *ering, unsigned int eflags,
358 unsigned int err_mask)
360 struct ata_ering_entry *ent;
365 ering->cursor %= ATA_ERING_SIZE;
367 ent = &ering->ring[ering->cursor];
368 ent->eflags = eflags;
369 ent->err_mask = err_mask;
370 ent->timestamp = get_jiffies_64();
373 static struct ata_ering_entry *ata_ering_top(struct ata_ering *ering)
375 struct ata_ering_entry *ent = &ering->ring[ering->cursor];
382 int ata_ering_map(struct ata_ering *ering,
383 int (*map_fn)(struct ata_ering_entry *, void *),
387 struct ata_ering_entry *ent;
391 ent = &ering->ring[idx];
394 rc = map_fn(ent, arg);
397 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
398 } while (idx != ering->cursor);
403 static int ata_ering_clear_cb(struct ata_ering_entry *ent, void *void_arg)
405 ent->eflags |= ATA_EFLAG_OLD_ER;
409 static void ata_ering_clear(struct ata_ering *ering)
411 ata_ering_map(ering, ata_ering_clear_cb, NULL);
414 static unsigned int ata_eh_dev_action(struct ata_device *dev)
416 struct ata_eh_context *ehc = &dev->link->eh_context;
418 return ehc->i.action | ehc->i.dev_action[dev->devno];
421 static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
422 struct ata_eh_info *ehi, unsigned int action)
424 struct ata_device *tdev;
427 ehi->action &= ~action;
428 ata_for_each_dev(tdev, link, ALL)
429 ehi->dev_action[tdev->devno] &= ~action;
431 /* doesn't make sense for port-wide EH actions */
432 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
434 /* break ehi->action into ehi->dev_action */
435 if (ehi->action & action) {
436 ata_for_each_dev(tdev, link, ALL)
437 ehi->dev_action[tdev->devno] |=
438 ehi->action & action;
439 ehi->action &= ~action;
442 /* turn off the specified per-dev action */
443 ehi->dev_action[dev->devno] &= ~action;
448 * ata_eh_acquire - acquire EH ownership
449 * @ap: ATA port to acquire EH ownership for
451 * Acquire EH ownership for @ap. This is the basic exclusion
452 * mechanism for ports sharing a host. Only one port hanging off
453 * the same host can claim the ownership of EH.
458 void ata_eh_acquire(struct ata_port *ap)
460 mutex_lock(&ap->host->eh_mutex);
461 WARN_ON_ONCE(ap->host->eh_owner);
462 ap->host->eh_owner = current;
466 * ata_eh_release - release EH ownership
467 * @ap: ATA port to release EH ownership for
469 * Release EH ownership for @ap if the caller. The caller must
470 * have acquired EH ownership using ata_eh_acquire() previously.
475 void ata_eh_release(struct ata_port *ap)
477 WARN_ON_ONCE(ap->host->eh_owner != current);
478 ap->host->eh_owner = NULL;
479 mutex_unlock(&ap->host->eh_mutex);
482 static void ata_eh_unload(struct ata_port *ap)
484 struct ata_link *link;
485 struct ata_device *dev;
488 /* Restore SControl IPM and SPD for the next driver and
489 * disable attached devices.
491 ata_for_each_link(link, ap, PMP_FIRST) {
492 sata_scr_write(link, SCR_CONTROL, link->saved_scontrol & 0xff0);
493 ata_for_each_dev(dev, link, ALL)
494 ata_dev_disable(dev);
497 /* freeze and set UNLOADED */
498 spin_lock_irqsave(ap->lock, flags);
500 ata_port_freeze(ap); /* won't be thawed */
501 ap->pflags &= ~ATA_PFLAG_EH_PENDING; /* clear pending from freeze */
502 ap->pflags |= ATA_PFLAG_UNLOADED;
504 spin_unlock_irqrestore(ap->lock, flags);
508 * ata_scsi_error - SCSI layer error handler callback
509 * @host: SCSI host on which error occurred
511 * Handles SCSI-layer-thrown error events.
514 * Inherited from SCSI layer (none, can sleep)
519 void ata_scsi_error(struct Scsi_Host *host)
521 struct ata_port *ap = ata_shost_to_port(host);
523 LIST_HEAD(eh_work_q);
527 spin_lock_irqsave(host->host_lock, flags);
528 list_splice_init(&host->eh_cmd_q, &eh_work_q);
529 spin_unlock_irqrestore(host->host_lock, flags);
531 ata_scsi_cmd_error_handler(host, ap, &eh_work_q);
533 /* If we timed raced normal completion and there is nothing to
534 recover nr_timedout == 0 why exactly are we doing error recovery ? */
535 ata_scsi_port_error_handler(host, ap);
537 /* finish or retry handled scmd's and clean up */
538 WARN_ON(!list_empty(&eh_work_q));
544 * ata_scsi_cmd_error_handler - error callback for a list of commands
545 * @host: scsi host containing the port
546 * @ap: ATA port within the host
547 * @eh_work_q: list of commands to process
549 * process the given list of commands and return those finished to the
550 * ap->eh_done_q. This function is the first part of the libata error
551 * handler which processes a given list of failed commands.
553 void ata_scsi_cmd_error_handler(struct Scsi_Host *host, struct ata_port *ap,
554 struct list_head *eh_work_q)
559 /* make sure sff pio task is not running */
560 ata_sff_flush_pio_task(ap);
562 /* synchronize with host lock and sort out timeouts */
564 /* For new EH, all qcs are finished in one of three ways -
565 * normal completion, error completion, and SCSI timeout.
566 * Both completions can race against SCSI timeout. When normal
567 * completion wins, the qc never reaches EH. When error
568 * completion wins, the qc has ATA_QCFLAG_FAILED set.
570 * When SCSI timeout wins, things are a bit more complex.
571 * Normal or error completion can occur after the timeout but
572 * before this point. In such cases, both types of
573 * completions are honored. A scmd is determined to have
574 * timed out iff its associated qc is active and not failed.
576 spin_lock_irqsave(ap->lock, flags);
577 if (ap->ops->error_handler) {
578 struct scsi_cmnd *scmd, *tmp;
581 /* This must occur under the ap->lock as we don't want
582 a polled recovery to race the real interrupt handler
584 The lost_interrupt handler checks for any completed but
585 non-notified command and completes much like an IRQ handler.
587 We then fall into the error recovery code which will treat
588 this as if normal completion won the race */
590 if (ap->ops->lost_interrupt)
591 ap->ops->lost_interrupt(ap);
593 list_for_each_entry_safe(scmd, tmp, eh_work_q, eh_entry) {
594 struct ata_queued_cmd *qc;
596 ata_qc_for_each_raw(ap, qc, i) {
597 if (qc->flags & ATA_QCFLAG_ACTIVE &&
602 if (i < ATA_MAX_QUEUE) {
603 /* the scmd has an associated qc */
604 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
605 /* which hasn't failed yet, timeout */
606 qc->err_mask |= AC_ERR_TIMEOUT;
607 qc->flags |= ATA_QCFLAG_FAILED;
611 /* Normal completion occurred after
612 * SCSI timeout but before this point.
613 * Successfully complete it.
615 scmd->retries = scmd->allowed;
616 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
620 /* If we have timed out qcs. They belong to EH from
621 * this point but the state of the controller is
622 * unknown. Freeze the port to make sure the IRQ
623 * handler doesn't diddle with those qcs. This must
624 * be done atomically w.r.t. setting QCFLAG_FAILED.
627 __ata_port_freeze(ap);
630 /* initialize eh_tries */
631 ap->eh_tries = ATA_EH_MAX_TRIES;
633 spin_unlock_irqrestore(ap->lock, flags);
636 EXPORT_SYMBOL(ata_scsi_cmd_error_handler);
639 * ata_scsi_port_error_handler - recover the port after the commands
640 * @host: SCSI host containing the port
643 * Handle the recovery of the port @ap after all the commands
644 * have been recovered.
646 void ata_scsi_port_error_handler(struct Scsi_Host *host, struct ata_port *ap)
650 /* invoke error handler */
651 if (ap->ops->error_handler) {
652 struct ata_link *link;
654 /* acquire EH ownership */
657 /* kill fast drain timer */
658 del_timer_sync(&ap->fastdrain_timer);
660 /* process port resume request */
661 ata_eh_handle_port_resume(ap);
663 /* fetch & clear EH info */
664 spin_lock_irqsave(ap->lock, flags);
666 ata_for_each_link(link, ap, HOST_FIRST) {
667 struct ata_eh_context *ehc = &link->eh_context;
668 struct ata_device *dev;
670 memset(&link->eh_context, 0, sizeof(link->eh_context));
671 link->eh_context.i = link->eh_info;
672 memset(&link->eh_info, 0, sizeof(link->eh_info));
674 ata_for_each_dev(dev, link, ENABLED) {
675 int devno = dev->devno;
677 ehc->saved_xfer_mode[devno] = dev->xfer_mode;
678 if (ata_ncq_enabled(dev))
679 ehc->saved_ncq_enabled |= 1 << devno;
683 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
684 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
685 ap->excl_link = NULL; /* don't maintain exclusion over EH */
687 spin_unlock_irqrestore(ap->lock, flags);
689 /* invoke EH, skip if unloading or suspended */
690 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
691 ap->ops->error_handler(ap);
693 /* if unloading, commence suicide */
694 if ((ap->pflags & ATA_PFLAG_UNLOADING) &&
695 !(ap->pflags & ATA_PFLAG_UNLOADED))
700 /* process port suspend request */
701 ata_eh_handle_port_suspend(ap);
703 /* Exception might have happened after ->error_handler
704 * recovered the port but before this point. Repeat
707 spin_lock_irqsave(ap->lock, flags);
709 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
710 if (--ap->eh_tries) {
711 spin_unlock_irqrestore(ap->lock, flags);
715 "EH pending after %d tries, giving up\n",
717 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
720 /* this run is complete, make sure EH info is clear */
721 ata_for_each_link(link, ap, HOST_FIRST)
722 memset(&link->eh_info, 0, sizeof(link->eh_info));
724 /* end eh (clear host_eh_scheduled) while holding
725 * ap->lock such that if exception occurs after this
726 * point but before EH completion, SCSI midlayer will
731 spin_unlock_irqrestore(ap->lock, flags);
734 WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
735 ap->ops->eng_timeout(ap);
738 scsi_eh_flush_done_q(&ap->eh_done_q);
741 spin_lock_irqsave(ap->lock, flags);
743 if (ap->pflags & ATA_PFLAG_LOADING)
744 ap->pflags &= ~ATA_PFLAG_LOADING;
745 else if ((ap->pflags & ATA_PFLAG_SCSI_HOTPLUG) &&
746 !(ap->flags & ATA_FLAG_SAS_HOST))
747 schedule_delayed_work(&ap->hotplug_task, 0);
749 if (ap->pflags & ATA_PFLAG_RECOVERED)
750 ata_port_info(ap, "EH complete\n");
752 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
754 /* tell wait_eh that we're done */
755 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
756 wake_up_all(&ap->eh_wait_q);
758 spin_unlock_irqrestore(ap->lock, flags);
760 EXPORT_SYMBOL_GPL(ata_scsi_port_error_handler);
763 * ata_port_wait_eh - Wait for the currently pending EH to complete
764 * @ap: Port to wait EH for
766 * Wait until the currently pending EH is complete.
769 * Kernel thread context (may sleep).
771 void ata_port_wait_eh(struct ata_port *ap)
777 spin_lock_irqsave(ap->lock, flags);
779 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
780 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
781 spin_unlock_irqrestore(ap->lock, flags);
783 spin_lock_irqsave(ap->lock, flags);
785 finish_wait(&ap->eh_wait_q, &wait);
787 spin_unlock_irqrestore(ap->lock, flags);
789 /* make sure SCSI EH is complete */
790 if (scsi_host_in_recovery(ap->scsi_host)) {
795 EXPORT_SYMBOL_GPL(ata_port_wait_eh);
797 static int ata_eh_nr_in_flight(struct ata_port *ap)
799 struct ata_queued_cmd *qc;
803 /* count only non-internal commands */
804 ata_qc_for_each(ap, qc, tag) {
812 void ata_eh_fastdrain_timerfn(struct timer_list *t)
814 struct ata_port *ap = from_timer(ap, t, fastdrain_timer);
818 spin_lock_irqsave(ap->lock, flags);
820 cnt = ata_eh_nr_in_flight(ap);
826 if (cnt == ap->fastdrain_cnt) {
827 struct ata_queued_cmd *qc;
830 /* No progress during the last interval, tag all
831 * in-flight qcs as timed out and freeze the port.
833 ata_qc_for_each(ap, qc, tag) {
835 qc->err_mask |= AC_ERR_TIMEOUT;
840 /* some qcs have finished, give it another chance */
841 ap->fastdrain_cnt = cnt;
842 ap->fastdrain_timer.expires =
843 ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
844 add_timer(&ap->fastdrain_timer);
848 spin_unlock_irqrestore(ap->lock, flags);
852 * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
853 * @ap: target ATA port
854 * @fastdrain: activate fast drain
856 * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
857 * is non-zero and EH wasn't pending before. Fast drain ensures
858 * that EH kicks in in timely manner.
861 * spin_lock_irqsave(host lock)
863 static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
867 /* already scheduled? */
868 if (ap->pflags & ATA_PFLAG_EH_PENDING)
871 ap->pflags |= ATA_PFLAG_EH_PENDING;
876 /* do we have in-flight qcs? */
877 cnt = ata_eh_nr_in_flight(ap);
881 /* activate fast drain */
882 ap->fastdrain_cnt = cnt;
883 ap->fastdrain_timer.expires =
884 ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
885 add_timer(&ap->fastdrain_timer);
889 * ata_qc_schedule_eh - schedule qc for error handling
890 * @qc: command to schedule error handling for
892 * Schedule error handling for @qc. EH will kick in as soon as
893 * other commands are drained.
896 * spin_lock_irqsave(host lock)
898 void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
900 struct ata_port *ap = qc->ap;
902 WARN_ON(!ap->ops->error_handler);
904 qc->flags |= ATA_QCFLAG_FAILED;
905 ata_eh_set_pending(ap, 1);
907 /* The following will fail if timeout has already expired.
908 * ata_scsi_error() takes care of such scmds on EH entry.
909 * Note that ATA_QCFLAG_FAILED is unconditionally set after
910 * this function completes.
912 blk_abort_request(qc->scsicmd->request);
916 * ata_std_sched_eh - non-libsas ata_ports issue eh with this common routine
917 * @ap: ATA port to schedule EH for
919 * LOCKING: inherited from ata_port_schedule_eh
920 * spin_lock_irqsave(host lock)
922 void ata_std_sched_eh(struct ata_port *ap)
924 WARN_ON(!ap->ops->error_handler);
926 if (ap->pflags & ATA_PFLAG_INITIALIZING)
929 ata_eh_set_pending(ap, 1);
930 scsi_schedule_eh(ap->scsi_host);
932 DPRINTK("port EH scheduled\n");
934 EXPORT_SYMBOL_GPL(ata_std_sched_eh);
937 * ata_std_end_eh - non-libsas ata_ports complete eh with this common routine
938 * @ap: ATA port to end EH for
940 * In the libata object model there is a 1:1 mapping of ata_port to
941 * shost, so host fields can be directly manipulated under ap->lock, in
942 * the libsas case we need to hold a lock at the ha->level to coordinate
946 * spin_lock_irqsave(host lock)
948 void ata_std_end_eh(struct ata_port *ap)
950 struct Scsi_Host *host = ap->scsi_host;
952 host->host_eh_scheduled = 0;
954 EXPORT_SYMBOL(ata_std_end_eh);
958 * ata_port_schedule_eh - schedule error handling without a qc
959 * @ap: ATA port to schedule EH for
961 * Schedule error handling for @ap. EH will kick in as soon as
962 * all commands are drained.
965 * spin_lock_irqsave(host lock)
967 void ata_port_schedule_eh(struct ata_port *ap)
969 /* see: ata_std_sched_eh, unless you know better */
970 ap->ops->sched_eh(ap);
973 static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
975 struct ata_queued_cmd *qc;
976 int tag, nr_aborted = 0;
978 WARN_ON(!ap->ops->error_handler);
980 /* we're gonna abort all commands, no need for fast drain */
981 ata_eh_set_pending(ap, 0);
983 /* include internal tag in iteration */
984 ata_qc_for_each_with_internal(ap, qc, tag) {
985 if (qc && (!link || qc->dev->link == link)) {
986 qc->flags |= ATA_QCFLAG_FAILED;
993 ata_port_schedule_eh(ap);
999 * ata_link_abort - abort all qc's on the link
1000 * @link: ATA link to abort qc's for
1002 * Abort all active qc's active on @link and schedule EH.
1005 * spin_lock_irqsave(host lock)
1008 * Number of aborted qc's.
1010 int ata_link_abort(struct ata_link *link)
1012 return ata_do_link_abort(link->ap, link);
1016 * ata_port_abort - abort all qc's on the port
1017 * @ap: ATA port to abort qc's for
1019 * Abort all active qc's of @ap and schedule EH.
1022 * spin_lock_irqsave(host_set lock)
1025 * Number of aborted qc's.
1027 int ata_port_abort(struct ata_port *ap)
1029 return ata_do_link_abort(ap, NULL);
1033 * __ata_port_freeze - freeze port
1034 * @ap: ATA port to freeze
1036 * This function is called when HSM violation or some other
1037 * condition disrupts normal operation of the port. Frozen port
1038 * is not allowed to perform any operation until the port is
1039 * thawed, which usually follows a successful reset.
1041 * ap->ops->freeze() callback can be used for freezing the port
1042 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
1043 * port cannot be frozen hardware-wise, the interrupt handler
1044 * must ack and clear interrupts unconditionally while the port
1048 * spin_lock_irqsave(host lock)
1050 static void __ata_port_freeze(struct ata_port *ap)
1052 WARN_ON(!ap->ops->error_handler);
1054 if (ap->ops->freeze)
1055 ap->ops->freeze(ap);
1057 ap->pflags |= ATA_PFLAG_FROZEN;
1059 DPRINTK("ata%u port frozen\n", ap->print_id);
1063 * ata_port_freeze - abort & freeze port
1064 * @ap: ATA port to freeze
1066 * Abort and freeze @ap. The freeze operation must be called
1067 * first, because some hardware requires special operations
1068 * before the taskfile registers are accessible.
1071 * spin_lock_irqsave(host lock)
1074 * Number of aborted commands.
1076 int ata_port_freeze(struct ata_port *ap)
1080 WARN_ON(!ap->ops->error_handler);
1082 __ata_port_freeze(ap);
1083 nr_aborted = ata_port_abort(ap);
1089 * sata_async_notification - SATA async notification handler
1090 * @ap: ATA port where async notification is received
1092 * Handler to be called when async notification via SDB FIS is
1093 * received. This function schedules EH if necessary.
1096 * spin_lock_irqsave(host lock)
1099 * 1 if EH is scheduled, 0 otherwise.
1101 int sata_async_notification(struct ata_port *ap)
1106 if (!(ap->flags & ATA_FLAG_AN))
1109 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
1111 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
1113 if (!sata_pmp_attached(ap) || rc) {
1114 /* PMP is not attached or SNTF is not available */
1115 if (!sata_pmp_attached(ap)) {
1116 /* PMP is not attached. Check whether ATAPI
1117 * AN is configured. If so, notify media
1120 struct ata_device *dev = ap->link.device;
1122 if ((dev->class == ATA_DEV_ATAPI) &&
1123 (dev->flags & ATA_DFLAG_AN))
1124 ata_scsi_media_change_notify(dev);
1127 /* PMP is attached but SNTF is not available.
1128 * ATAPI async media change notification is
1129 * not used. The PMP must be reporting PHY
1130 * status change, schedule EH.
1132 ata_port_schedule_eh(ap);
1136 /* PMP is attached and SNTF is available */
1137 struct ata_link *link;
1139 /* check and notify ATAPI AN */
1140 ata_for_each_link(link, ap, EDGE) {
1141 if (!(sntf & (1 << link->pmp)))
1144 if ((link->device->class == ATA_DEV_ATAPI) &&
1145 (link->device->flags & ATA_DFLAG_AN))
1146 ata_scsi_media_change_notify(link->device);
1149 /* If PMP is reporting that PHY status of some
1150 * downstream ports has changed, schedule EH.
1152 if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
1153 ata_port_schedule_eh(ap);
1162 * ata_eh_freeze_port - EH helper to freeze port
1163 * @ap: ATA port to freeze
1170 void ata_eh_freeze_port(struct ata_port *ap)
1172 unsigned long flags;
1174 if (!ap->ops->error_handler)
1177 spin_lock_irqsave(ap->lock, flags);
1178 __ata_port_freeze(ap);
1179 spin_unlock_irqrestore(ap->lock, flags);
1183 * ata_port_thaw_port - EH helper to thaw port
1184 * @ap: ATA port to thaw
1186 * Thaw frozen port @ap.
1191 void ata_eh_thaw_port(struct ata_port *ap)
1193 unsigned long flags;
1195 if (!ap->ops->error_handler)
1198 spin_lock_irqsave(ap->lock, flags);
1200 ap->pflags &= ~ATA_PFLAG_FROZEN;
1205 spin_unlock_irqrestore(ap->lock, flags);
1207 DPRINTK("ata%u port thawed\n", ap->print_id);
1210 static void ata_eh_scsidone(struct scsi_cmnd *scmd)
1215 static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
1217 struct ata_port *ap = qc->ap;
1218 struct scsi_cmnd *scmd = qc->scsicmd;
1219 unsigned long flags;
1221 spin_lock_irqsave(ap->lock, flags);
1222 qc->scsidone = ata_eh_scsidone;
1223 __ata_qc_complete(qc);
1224 WARN_ON(ata_tag_valid(qc->tag));
1225 spin_unlock_irqrestore(ap->lock, flags);
1227 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
1231 * ata_eh_qc_complete - Complete an active ATA command from EH
1232 * @qc: Command to complete
1234 * Indicate to the mid and upper layers that an ATA command has
1235 * completed. To be used from EH.
1237 void ata_eh_qc_complete(struct ata_queued_cmd *qc)
1239 struct scsi_cmnd *scmd = qc->scsicmd;
1240 scmd->retries = scmd->allowed;
1241 __ata_eh_qc_complete(qc);
1245 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
1246 * @qc: Command to retry
1248 * Indicate to the mid and upper layers that an ATA command
1249 * should be retried. To be used from EH.
1251 * SCSI midlayer limits the number of retries to scmd->allowed.
1252 * scmd->allowed is incremented for commands which get retried
1253 * due to unrelated failures (qc->err_mask is zero).
1255 void ata_eh_qc_retry(struct ata_queued_cmd *qc)
1257 struct scsi_cmnd *scmd = qc->scsicmd;
1260 __ata_eh_qc_complete(qc);
1264 * ata_dev_disable - disable ATA device
1265 * @dev: ATA device to disable
1272 void ata_dev_disable(struct ata_device *dev)
1274 if (!ata_dev_enabled(dev))
1277 if (ata_msg_drv(dev->link->ap))
1278 ata_dev_warn(dev, "disabled\n");
1279 ata_acpi_on_disable(dev);
1280 ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 | ATA_DNXFER_QUIET);
1283 /* From now till the next successful probe, ering is used to
1284 * track probe failures. Clear accumulated device error info.
1286 ata_ering_clear(&dev->ering);
1290 * ata_eh_detach_dev - detach ATA device
1291 * @dev: ATA device to detach
1298 void ata_eh_detach_dev(struct ata_device *dev)
1300 struct ata_link *link = dev->link;
1301 struct ata_port *ap = link->ap;
1302 struct ata_eh_context *ehc = &link->eh_context;
1303 unsigned long flags;
1305 ata_dev_disable(dev);
1307 spin_lock_irqsave(ap->lock, flags);
1309 dev->flags &= ~ATA_DFLAG_DETACH;
1311 if (ata_scsi_offline_dev(dev)) {
1312 dev->flags |= ATA_DFLAG_DETACHED;
1313 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
1316 /* clear per-dev EH info */
1317 ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
1318 ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
1319 ehc->saved_xfer_mode[dev->devno] = 0;
1320 ehc->saved_ncq_enabled &= ~(1 << dev->devno);
1322 spin_unlock_irqrestore(ap->lock, flags);
1326 * ata_eh_about_to_do - about to perform eh_action
1327 * @link: target ATA link
1328 * @dev: target ATA dev for per-dev action (can be NULL)
1329 * @action: action about to be performed
1331 * Called just before performing EH actions to clear related bits
1332 * in @link->eh_info such that eh actions are not unnecessarily
1338 void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
1339 unsigned int action)
1341 struct ata_port *ap = link->ap;
1342 struct ata_eh_info *ehi = &link->eh_info;
1343 struct ata_eh_context *ehc = &link->eh_context;
1344 unsigned long flags;
1346 spin_lock_irqsave(ap->lock, flags);
1348 ata_eh_clear_action(link, dev, ehi, action);
1350 /* About to take EH action, set RECOVERED. Ignore actions on
1351 * slave links as master will do them again.
1353 if (!(ehc->i.flags & ATA_EHI_QUIET) && link != ap->slave_link)
1354 ap->pflags |= ATA_PFLAG_RECOVERED;
1356 spin_unlock_irqrestore(ap->lock, flags);
1360 * ata_eh_done - EH action complete
1361 * @link: ATA link for which EH actions are complete
1362 * @dev: target ATA dev for per-dev action (can be NULL)
1363 * @action: action just completed
1365 * Called right after performing EH actions to clear related bits
1366 * in @link->eh_context.
1371 void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1372 unsigned int action)
1374 struct ata_eh_context *ehc = &link->eh_context;
1376 ata_eh_clear_action(link, dev, &ehc->i, action);
1380 * ata_err_string - convert err_mask to descriptive string
1381 * @err_mask: error mask to convert to string
1383 * Convert @err_mask to descriptive string. Errors are
1384 * prioritized according to severity and only the most severe
1385 * error is reported.
1391 * Descriptive string for @err_mask
1393 static const char *ata_err_string(unsigned int err_mask)
1395 if (err_mask & AC_ERR_HOST_BUS)
1396 return "host bus error";
1397 if (err_mask & AC_ERR_ATA_BUS)
1398 return "ATA bus error";
1399 if (err_mask & AC_ERR_TIMEOUT)
1401 if (err_mask & AC_ERR_HSM)
1402 return "HSM violation";
1403 if (err_mask & AC_ERR_SYSTEM)
1404 return "internal error";
1405 if (err_mask & AC_ERR_MEDIA)
1406 return "media error";
1407 if (err_mask & AC_ERR_INVALID)
1408 return "invalid argument";
1409 if (err_mask & AC_ERR_DEV)
1410 return "device error";
1411 if (err_mask & AC_ERR_NCQ)
1413 if (err_mask & AC_ERR_NODEV_HINT)
1414 return "Polling detection error";
1415 return "unknown error";
1419 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
1420 * @dev: Device to read log page 10h from
1421 * @tag: Resulting tag of the failed command
1422 * @tf: Resulting taskfile registers of the failed command
1424 * Read log page 10h to obtain NCQ error details and clear error
1428 * Kernel thread context (may sleep).
1431 * 0 on success, -errno otherwise.
1433 static int ata_eh_read_log_10h(struct ata_device *dev,
1434 int *tag, struct ata_taskfile *tf)
1436 u8 *buf = dev->link->ap->sector_buf;
1437 unsigned int err_mask;
1441 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, 0, buf, 1);
1446 for (i = 0; i < ATA_SECT_SIZE; i++)
1449 ata_dev_warn(dev, "invalid checksum 0x%x on log page 10h\n",
1455 *tag = buf[0] & 0x1f;
1457 tf->command = buf[2];
1458 tf->feature = buf[3];
1462 tf->device = buf[7];
1463 tf->hob_lbal = buf[8];
1464 tf->hob_lbam = buf[9];
1465 tf->hob_lbah = buf[10];
1466 tf->nsect = buf[12];
1467 tf->hob_nsect = buf[13];
1468 if (dev->class == ATA_DEV_ZAC && ata_id_has_ncq_autosense(dev->id))
1469 tf->auxiliary = buf[14] << 16 | buf[15] << 8 | buf[16];
1475 * atapi_eh_tur - perform ATAPI TEST_UNIT_READY
1476 * @dev: target ATAPI device
1477 * @r_sense_key: out parameter for sense_key
1479 * Perform ATAPI TEST_UNIT_READY.
1482 * EH context (may sleep).
1485 * 0 on success, AC_ERR_* mask on failure.
1487 unsigned int atapi_eh_tur(struct ata_device *dev, u8 *r_sense_key)
1489 u8 cdb[ATAPI_CDB_LEN] = { TEST_UNIT_READY, 0, 0, 0, 0, 0 };
1490 struct ata_taskfile tf;
1491 unsigned int err_mask;
1493 ata_tf_init(dev, &tf);
1495 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1496 tf.command = ATA_CMD_PACKET;
1497 tf.protocol = ATAPI_PROT_NODATA;
1499 err_mask = ata_exec_internal(dev, &tf, cdb, DMA_NONE, NULL, 0, 0);
1500 if (err_mask == AC_ERR_DEV)
1501 *r_sense_key = tf.feature >> 4;
1506 * ata_eh_request_sense - perform REQUEST_SENSE_DATA_EXT
1507 * @qc: qc to perform REQUEST_SENSE_SENSE_DATA_EXT to
1508 * @cmd: scsi command for which the sense code should be set
1510 * Perform REQUEST_SENSE_DATA_EXT after the device reported CHECK
1511 * SENSE. This function is an EH helper.
1514 * Kernel thread context (may sleep).
1516 static void ata_eh_request_sense(struct ata_queued_cmd *qc,
1517 struct scsi_cmnd *cmd)
1519 struct ata_device *dev = qc->dev;
1520 struct ata_taskfile tf;
1521 unsigned int err_mask;
1523 if (qc->ap->pflags & ATA_PFLAG_FROZEN) {
1524 ata_dev_warn(dev, "sense data available but port frozen\n");
1528 if (!cmd || qc->flags & ATA_QCFLAG_SENSE_VALID)
1531 if (!ata_id_sense_reporting_enabled(dev->id)) {
1532 ata_dev_warn(qc->dev, "sense data reporting disabled\n");
1536 DPRINTK("ATA request sense\n");
1538 ata_tf_init(dev, &tf);
1539 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1540 tf.flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
1541 tf.command = ATA_CMD_REQ_SENSE_DATA;
1542 tf.protocol = ATA_PROT_NODATA;
1544 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1545 /* Ignore err_mask; ATA_ERR might be set */
1546 if (tf.command & ATA_SENSE) {
1547 ata_scsi_set_sense(dev, cmd, tf.lbah, tf.lbam, tf.lbal);
1548 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1550 ata_dev_warn(dev, "request sense failed stat %02x emask %x\n",
1551 tf.command, err_mask);
1556 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1557 * @dev: device to perform REQUEST_SENSE to
1558 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
1559 * @dfl_sense_key: default sense key to use
1561 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
1562 * SENSE. This function is EH helper.
1565 * Kernel thread context (may sleep).
1568 * 0 on success, AC_ERR_* mask on failure
1570 unsigned int atapi_eh_request_sense(struct ata_device *dev,
1571 u8 *sense_buf, u8 dfl_sense_key)
1573 u8 cdb[ATAPI_CDB_LEN] =
1574 { REQUEST_SENSE, 0, 0, 0, SCSI_SENSE_BUFFERSIZE, 0 };
1575 struct ata_port *ap = dev->link->ap;
1576 struct ata_taskfile tf;
1578 DPRINTK("ATAPI request sense\n");
1580 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1582 /* initialize sense_buf with the error register,
1583 * for the case where they are -not- overwritten
1585 sense_buf[0] = 0x70;
1586 sense_buf[2] = dfl_sense_key;
1588 /* some devices time out if garbage left in tf */
1589 ata_tf_init(dev, &tf);
1591 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1592 tf.command = ATA_CMD_PACKET;
1594 /* is it pointless to prefer PIO for "safety reasons"? */
1595 if (ap->flags & ATA_FLAG_PIO_DMA) {
1596 tf.protocol = ATAPI_PROT_DMA;
1597 tf.feature |= ATAPI_PKT_DMA;
1599 tf.protocol = ATAPI_PROT_PIO;
1600 tf.lbam = SCSI_SENSE_BUFFERSIZE;
1604 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
1605 sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
1609 * ata_eh_analyze_serror - analyze SError for a failed port
1610 * @link: ATA link to analyze SError for
1612 * Analyze SError if available and further determine cause of
1618 static void ata_eh_analyze_serror(struct ata_link *link)
1620 struct ata_eh_context *ehc = &link->eh_context;
1621 u32 serror = ehc->i.serror;
1622 unsigned int err_mask = 0, action = 0;
1625 if (serror & (SERR_PERSISTENT | SERR_DATA)) {
1626 err_mask |= AC_ERR_ATA_BUS;
1627 action |= ATA_EH_RESET;
1629 if (serror & SERR_PROTOCOL) {
1630 err_mask |= AC_ERR_HSM;
1631 action |= ATA_EH_RESET;
1633 if (serror & SERR_INTERNAL) {
1634 err_mask |= AC_ERR_SYSTEM;
1635 action |= ATA_EH_RESET;
1638 /* Determine whether a hotplug event has occurred. Both
1639 * SError.N/X are considered hotplug events for enabled or
1640 * host links. For disabled PMP links, only N bit is
1641 * considered as X bit is left at 1 for link plugging.
1643 if (link->lpm_policy > ATA_LPM_MAX_POWER)
1644 hotplug_mask = 0; /* hotplug doesn't work w/ LPM */
1645 else if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
1646 hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
1648 hotplug_mask = SERR_PHYRDY_CHG;
1650 if (serror & hotplug_mask)
1651 ata_ehi_hotplugged(&ehc->i);
1653 ehc->i.err_mask |= err_mask;
1654 ehc->i.action |= action;
1658 * ata_eh_analyze_ncq_error - analyze NCQ error
1659 * @link: ATA link to analyze NCQ error for
1661 * Read log page 10h, determine the offending qc and acquire
1662 * error status TF. For NCQ device errors, all LLDDs have to do
1663 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1667 * Kernel thread context (may sleep).
1669 void ata_eh_analyze_ncq_error(struct ata_link *link)
1671 struct ata_port *ap = link->ap;
1672 struct ata_eh_context *ehc = &link->eh_context;
1673 struct ata_device *dev = link->device;
1674 struct ata_queued_cmd *qc;
1675 struct ata_taskfile tf;
1678 /* if frozen, we can't do much */
1679 if (ap->pflags & ATA_PFLAG_FROZEN)
1682 /* is it NCQ device error? */
1683 if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1686 /* has LLDD analyzed already? */
1687 ata_qc_for_each_raw(ap, qc, tag) {
1688 if (!(qc->flags & ATA_QCFLAG_FAILED))
1695 /* okay, this error is ours */
1696 memset(&tf, 0, sizeof(tf));
1697 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1699 ata_link_err(link, "failed to read log page 10h (errno=%d)\n",
1704 if (!(link->sactive & (1 << tag))) {
1705 ata_link_err(link, "log page 10h reported inactive tag %d\n",
1710 /* we've got the perpetrator, condemn it */
1711 qc = __ata_qc_from_tag(ap, tag);
1712 memcpy(&qc->result_tf, &tf, sizeof(tf));
1713 qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
1714 qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
1715 if (dev->class == ATA_DEV_ZAC &&
1716 ((qc->result_tf.command & ATA_SENSE) || qc->result_tf.auxiliary)) {
1717 char sense_key, asc, ascq;
1719 sense_key = (qc->result_tf.auxiliary >> 16) & 0xff;
1720 asc = (qc->result_tf.auxiliary >> 8) & 0xff;
1721 ascq = qc->result_tf.auxiliary & 0xff;
1722 ata_scsi_set_sense(dev, qc->scsicmd, sense_key, asc, ascq);
1723 ata_scsi_set_sense_information(dev, qc->scsicmd,
1725 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1728 ehc->i.err_mask &= ~AC_ERR_DEV;
1732 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1733 * @qc: qc to analyze
1734 * @tf: Taskfile registers to analyze
1736 * Analyze taskfile of @qc and further determine cause of
1737 * failure. This function also requests ATAPI sense data if
1741 * Kernel thread context (may sleep).
1744 * Determined recovery action
1746 static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1747 const struct ata_taskfile *tf)
1749 unsigned int tmp, action = 0;
1750 u8 stat = tf->command, err = tf->feature;
1752 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1753 qc->err_mask |= AC_ERR_HSM;
1754 return ATA_EH_RESET;
1757 if (stat & (ATA_ERR | ATA_DF)) {
1758 qc->err_mask |= AC_ERR_DEV;
1760 * Sense data reporting does not work if the
1761 * device fault bit is set.
1769 switch (qc->dev->class) {
1771 if (stat & ATA_SENSE)
1772 ata_eh_request_sense(qc, qc->scsicmd);
1776 qc->err_mask |= AC_ERR_ATA_BUS;
1777 if (err & (ATA_UNC | ATA_AMNF))
1778 qc->err_mask |= AC_ERR_MEDIA;
1780 qc->err_mask |= AC_ERR_INVALID;
1784 if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
1785 tmp = atapi_eh_request_sense(qc->dev,
1786 qc->scsicmd->sense_buffer,
1787 qc->result_tf.feature >> 4);
1789 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1791 qc->err_mask |= tmp;
1795 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1796 int ret = scsi_check_sense(qc->scsicmd);
1798 * SUCCESS here means that the sense code could be
1799 * evaluated and should be passed to the upper layers
1800 * for correct evaluation.
1801 * FAILED means the sense code could not be interpreted
1802 * and the device would need to be reset.
1803 * NEEDS_RETRY and ADD_TO_MLQUEUE means that the
1804 * command would need to be retried.
1806 if (ret == NEEDS_RETRY || ret == ADD_TO_MLQUEUE) {
1807 qc->flags |= ATA_QCFLAG_RETRY;
1808 qc->err_mask |= AC_ERR_OTHER;
1809 } else if (ret != SUCCESS) {
1810 qc->err_mask |= AC_ERR_HSM;
1813 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1814 action |= ATA_EH_RESET;
1819 static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask,
1824 if (!(eflags & ATA_EFLAG_DUBIOUS_XFER))
1828 base = ATA_ECAT_DUBIOUS_NONE;
1830 if (err_mask & AC_ERR_ATA_BUS)
1831 return base + ATA_ECAT_ATA_BUS;
1833 if (err_mask & AC_ERR_TIMEOUT)
1834 return base + ATA_ECAT_TOUT_HSM;
1836 if (eflags & ATA_EFLAG_IS_IO) {
1837 if (err_mask & AC_ERR_HSM)
1838 return base + ATA_ECAT_TOUT_HSM;
1840 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1841 return base + ATA_ECAT_UNK_DEV;
1847 struct speed_down_verdict_arg {
1850 int nr_errors[ATA_ECAT_NR];
1853 static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
1855 struct speed_down_verdict_arg *arg = void_arg;
1858 if ((ent->eflags & ATA_EFLAG_OLD_ER) || (ent->timestamp < arg->since))
1861 cat = ata_eh_categorize_error(ent->eflags, ent->err_mask,
1863 arg->nr_errors[cat]++;
1869 * ata_eh_speed_down_verdict - Determine speed down verdict
1870 * @dev: Device of interest
1872 * This function examines error ring of @dev and determines
1873 * whether NCQ needs to be turned off, transfer speed should be
1874 * stepped down, or falling back to PIO is necessary.
1876 * ECAT_ATA_BUS : ATA_BUS error for any command
1878 * ECAT_TOUT_HSM : TIMEOUT for any command or HSM violation for
1881 * ECAT_UNK_DEV : Unknown DEV error for IO commands
1883 * ECAT_DUBIOUS_* : Identical to above three but occurred while
1884 * data transfer hasn't been verified.
1888 * NCQ_OFF : Turn off NCQ.
1890 * SPEED_DOWN : Speed down transfer speed but don't fall back
1893 * FALLBACK_TO_PIO : Fall back to PIO.
1895 * Even if multiple verdicts are returned, only one action is
1896 * taken per error. An action triggered by non-DUBIOUS errors
1897 * clears ering, while one triggered by DUBIOUS_* errors doesn't.
1898 * This is to expedite speed down decisions right after device is
1899 * initially configured.
1901 * The following are speed down rules. #1 and #2 deal with
1904 * 1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors
1905 * occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO.
1907 * 2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors
1908 * occurred during last 5 mins, NCQ_OFF.
1910 * 3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors
1911 * occurred during last 5 mins, FALLBACK_TO_PIO
1913 * 4. If more than 3 TOUT_HSM or UNK_DEV errors occurred
1914 * during last 10 mins, NCQ_OFF.
1916 * 5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6
1917 * UNK_DEV errors occurred during last 10 mins, SPEED_DOWN.
1920 * Inherited from caller.
1923 * OR of ATA_EH_SPDN_* flags.
1925 static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
1927 const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
1928 u64 j64 = get_jiffies_64();
1929 struct speed_down_verdict_arg arg;
1930 unsigned int verdict = 0;
1932 /* scan past 5 mins of error history */
1933 memset(&arg, 0, sizeof(arg));
1934 arg.since = j64 - min(j64, j5mins);
1935 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1937 if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] +
1938 arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1)
1939 verdict |= ATA_EH_SPDN_SPEED_DOWN |
1940 ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS;
1942 if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] +
1943 arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1)
1944 verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS;
1946 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1947 arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1948 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
1949 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
1951 /* scan past 10 mins of error history */
1952 memset(&arg, 0, sizeof(arg));
1953 arg.since = j64 - min(j64, j10mins);
1954 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1956 if (arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1957 arg.nr_errors[ATA_ECAT_UNK_DEV] > 3)
1958 verdict |= ATA_EH_SPDN_NCQ_OFF;
1960 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1961 arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 ||
1962 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
1963 verdict |= ATA_EH_SPDN_SPEED_DOWN;
1969 * ata_eh_speed_down - record error and speed down if necessary
1970 * @dev: Failed device
1971 * @eflags: mask of ATA_EFLAG_* flags
1972 * @err_mask: err_mask of the error
1974 * Record error and examine error history to determine whether
1975 * adjusting transmission speed is necessary. It also sets
1976 * transmission limits appropriately if such adjustment is
1980 * Kernel thread context (may sleep).
1983 * Determined recovery action.
1985 static unsigned int ata_eh_speed_down(struct ata_device *dev,
1986 unsigned int eflags, unsigned int err_mask)
1988 struct ata_link *link = ata_dev_phys_link(dev);
1990 unsigned int verdict;
1991 unsigned int action = 0;
1993 /* don't bother if Cat-0 error */
1994 if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0)
1997 /* record error and determine whether speed down is necessary */
1998 ata_ering_record(&dev->ering, eflags, err_mask);
1999 verdict = ata_eh_speed_down_verdict(dev);
2002 if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
2003 (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
2004 ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
2005 dev->flags |= ATA_DFLAG_NCQ_OFF;
2006 ata_dev_warn(dev, "NCQ disabled due to excessive errors\n");
2011 if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
2012 /* speed down SATA link speed if possible */
2013 if (sata_down_spd_limit(link, 0) == 0) {
2014 action |= ATA_EH_RESET;
2018 /* lower transfer mode */
2019 if (dev->spdn_cnt < 2) {
2020 static const int dma_dnxfer_sel[] =
2021 { ATA_DNXFER_DMA, ATA_DNXFER_40C };
2022 static const int pio_dnxfer_sel[] =
2023 { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
2026 if (dev->xfer_shift != ATA_SHIFT_PIO)
2027 sel = dma_dnxfer_sel[dev->spdn_cnt];
2029 sel = pio_dnxfer_sel[dev->spdn_cnt];
2033 if (ata_down_xfermask_limit(dev, sel) == 0) {
2034 action |= ATA_EH_RESET;
2040 /* Fall back to PIO? Slowing down to PIO is meaningless for
2041 * SATA ATA devices. Consider it only for PATA and SATAPI.
2043 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
2044 (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) &&
2045 (dev->xfer_shift != ATA_SHIFT_PIO)) {
2046 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
2048 action |= ATA_EH_RESET;
2055 /* device has been slowed down, blow error history */
2056 if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS))
2057 ata_ering_clear(&dev->ering);
2062 * ata_eh_worth_retry - analyze error and decide whether to retry
2063 * @qc: qc to possibly retry
2065 * Look at the cause of the error and decide if a retry
2066 * might be useful or not. We don't want to retry media errors
2067 * because the drive itself has probably already taken 10-30 seconds
2068 * doing its own internal retries before reporting the failure.
2070 static inline int ata_eh_worth_retry(struct ata_queued_cmd *qc)
2072 if (qc->err_mask & AC_ERR_MEDIA)
2073 return 0; /* don't retry media errors */
2074 if (qc->flags & ATA_QCFLAG_IO)
2075 return 1; /* otherwise retry anything from fs stack */
2076 if (qc->err_mask & AC_ERR_INVALID)
2077 return 0; /* don't retry these */
2078 return qc->err_mask != AC_ERR_DEV; /* retry if not dev error */
2082 * ata_eh_quiet - check if we need to be quiet about a command error
2085 * Look at the qc flags anbd its scsi command request flags to determine
2086 * if we need to be quiet about the command failure.
2088 static inline bool ata_eh_quiet(struct ata_queued_cmd *qc)
2091 qc->scsicmd->request->rq_flags & RQF_QUIET)
2092 qc->flags |= ATA_QCFLAG_QUIET;
2093 return qc->flags & ATA_QCFLAG_QUIET;
2097 * ata_eh_link_autopsy - analyze error and determine recovery action
2098 * @link: host link to perform autopsy on
2100 * Analyze why @link failed and determine which recovery actions
2101 * are needed. This function also sets more detailed AC_ERR_*
2102 * values and fills sense data for ATAPI CHECK SENSE.
2105 * Kernel thread context (may sleep).
2107 static void ata_eh_link_autopsy(struct ata_link *link)
2109 struct ata_port *ap = link->ap;
2110 struct ata_eh_context *ehc = &link->eh_context;
2111 struct ata_queued_cmd *qc;
2112 struct ata_device *dev;
2113 unsigned int all_err_mask = 0, eflags = 0;
2114 int tag, nr_failed = 0, nr_quiet = 0;
2120 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
2123 /* obtain and analyze SError */
2124 rc = sata_scr_read(link, SCR_ERROR, &serror);
2126 ehc->i.serror |= serror;
2127 ata_eh_analyze_serror(link);
2128 } else if (rc != -EOPNOTSUPP) {
2129 /* SError read failed, force reset and probing */
2130 ehc->i.probe_mask |= ATA_ALL_DEVICES;
2131 ehc->i.action |= ATA_EH_RESET;
2132 ehc->i.err_mask |= AC_ERR_OTHER;
2135 /* analyze NCQ failure */
2136 ata_eh_analyze_ncq_error(link);
2138 /* any real error trumps AC_ERR_OTHER */
2139 if (ehc->i.err_mask & ~AC_ERR_OTHER)
2140 ehc->i.err_mask &= ~AC_ERR_OTHER;
2142 all_err_mask |= ehc->i.err_mask;
2144 ata_qc_for_each_raw(ap, qc, tag) {
2145 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
2146 ata_dev_phys_link(qc->dev) != link)
2149 /* inherit upper level err_mask */
2150 qc->err_mask |= ehc->i.err_mask;
2153 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
2155 /* DEV errors are probably spurious in case of ATA_BUS error */
2156 if (qc->err_mask & AC_ERR_ATA_BUS)
2157 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
2160 /* any real error trumps unknown error */
2161 if (qc->err_mask & ~AC_ERR_OTHER)
2162 qc->err_mask &= ~AC_ERR_OTHER;
2165 * SENSE_VALID trumps dev/unknown error and revalidation. Upper
2166 * layers will determine whether the command is worth retrying
2167 * based on the sense data and device class/type. Otherwise,
2168 * determine directly if the command is worth retrying using its
2169 * error mask and flags.
2171 if (qc->flags & ATA_QCFLAG_SENSE_VALID)
2172 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
2173 else if (ata_eh_worth_retry(qc))
2174 qc->flags |= ATA_QCFLAG_RETRY;
2176 /* accumulate error info */
2177 ehc->i.dev = qc->dev;
2178 all_err_mask |= qc->err_mask;
2179 if (qc->flags & ATA_QCFLAG_IO)
2180 eflags |= ATA_EFLAG_IS_IO;
2181 trace_ata_eh_link_autopsy_qc(qc);
2183 /* Count quiet errors */
2184 if (ata_eh_quiet(qc))
2189 /* If all failed commands requested silence, then be quiet */
2190 if (nr_quiet == nr_failed)
2191 ehc->i.flags |= ATA_EHI_QUIET;
2193 /* enforce default EH actions */
2194 if (ap->pflags & ATA_PFLAG_FROZEN ||
2195 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
2196 ehc->i.action |= ATA_EH_RESET;
2197 else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) ||
2198 (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV)))
2199 ehc->i.action |= ATA_EH_REVALIDATE;
2201 /* If we have offending qcs and the associated failed device,
2202 * perform per-dev EH action only on the offending device.
2205 ehc->i.dev_action[ehc->i.dev->devno] |=
2206 ehc->i.action & ATA_EH_PERDEV_MASK;
2207 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
2210 /* propagate timeout to host link */
2211 if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link))
2212 ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT;
2214 /* record error and consider speeding down */
2216 if (!dev && ((ata_link_max_devices(link) == 1 &&
2217 ata_dev_enabled(link->device))))
2221 if (dev->flags & ATA_DFLAG_DUBIOUS_XFER)
2222 eflags |= ATA_EFLAG_DUBIOUS_XFER;
2223 ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask);
2224 trace_ata_eh_link_autopsy(dev, ehc->i.action, all_err_mask);
2230 * ata_eh_autopsy - analyze error and determine recovery action
2231 * @ap: host port to perform autopsy on
2233 * Analyze all links of @ap and determine why they failed and
2234 * which recovery actions are needed.
2237 * Kernel thread context (may sleep).
2239 void ata_eh_autopsy(struct ata_port *ap)
2241 struct ata_link *link;
2243 ata_for_each_link(link, ap, EDGE)
2244 ata_eh_link_autopsy(link);
2246 /* Handle the frigging slave link. Autopsy is done similarly
2247 * but actions and flags are transferred over to the master
2248 * link and handled from there.
2250 if (ap->slave_link) {
2251 struct ata_eh_context *mehc = &ap->link.eh_context;
2252 struct ata_eh_context *sehc = &ap->slave_link->eh_context;
2254 /* transfer control flags from master to slave */
2255 sehc->i.flags |= mehc->i.flags & ATA_EHI_TO_SLAVE_MASK;
2257 /* perform autopsy on the slave link */
2258 ata_eh_link_autopsy(ap->slave_link);
2260 /* transfer actions from slave to master and clear slave */
2261 ata_eh_about_to_do(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
2262 mehc->i.action |= sehc->i.action;
2263 mehc->i.dev_action[1] |= sehc->i.dev_action[1];
2264 mehc->i.flags |= sehc->i.flags;
2265 ata_eh_done(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
2268 /* Autopsy of fanout ports can affect host link autopsy.
2269 * Perform host link autopsy last.
2271 if (sata_pmp_attached(ap))
2272 ata_eh_link_autopsy(&ap->link);
2276 * ata_get_cmd_descript - get description for ATA command
2277 * @command: ATA command code to get description for
2279 * Return a textual description of the given command, or NULL if the
2280 * command is not known.
2285 const char *ata_get_cmd_descript(u8 command)
2287 #ifdef CONFIG_ATA_VERBOSE_ERROR
2293 { ATA_CMD_DEV_RESET, "DEVICE RESET" },
2294 { ATA_CMD_CHK_POWER, "CHECK POWER MODE" },
2295 { ATA_CMD_STANDBY, "STANDBY" },
2296 { ATA_CMD_IDLE, "IDLE" },
2297 { ATA_CMD_EDD, "EXECUTE DEVICE DIAGNOSTIC" },
2298 { ATA_CMD_DOWNLOAD_MICRO, "DOWNLOAD MICROCODE" },
2299 { ATA_CMD_DOWNLOAD_MICRO_DMA, "DOWNLOAD MICROCODE DMA" },
2300 { ATA_CMD_NOP, "NOP" },
2301 { ATA_CMD_FLUSH, "FLUSH CACHE" },
2302 { ATA_CMD_FLUSH_EXT, "FLUSH CACHE EXT" },
2303 { ATA_CMD_ID_ATA, "IDENTIFY DEVICE" },
2304 { ATA_CMD_ID_ATAPI, "IDENTIFY PACKET DEVICE" },
2305 { ATA_CMD_SERVICE, "SERVICE" },
2306 { ATA_CMD_READ, "READ DMA" },
2307 { ATA_CMD_READ_EXT, "READ DMA EXT" },
2308 { ATA_CMD_READ_QUEUED, "READ DMA QUEUED" },
2309 { ATA_CMD_READ_STREAM_EXT, "READ STREAM EXT" },
2310 { ATA_CMD_READ_STREAM_DMA_EXT, "READ STREAM DMA EXT" },
2311 { ATA_CMD_WRITE, "WRITE DMA" },
2312 { ATA_CMD_WRITE_EXT, "WRITE DMA EXT" },
2313 { ATA_CMD_WRITE_QUEUED, "WRITE DMA QUEUED EXT" },
2314 { ATA_CMD_WRITE_STREAM_EXT, "WRITE STREAM EXT" },
2315 { ATA_CMD_WRITE_STREAM_DMA_EXT, "WRITE STREAM DMA EXT" },
2316 { ATA_CMD_WRITE_FUA_EXT, "WRITE DMA FUA EXT" },
2317 { ATA_CMD_WRITE_QUEUED_FUA_EXT, "WRITE DMA QUEUED FUA EXT" },
2318 { ATA_CMD_FPDMA_READ, "READ FPDMA QUEUED" },
2319 { ATA_CMD_FPDMA_WRITE, "WRITE FPDMA QUEUED" },
2320 { ATA_CMD_FPDMA_SEND, "SEND FPDMA QUEUED" },
2321 { ATA_CMD_FPDMA_RECV, "RECEIVE FPDMA QUEUED" },
2322 { ATA_CMD_PIO_READ, "READ SECTOR(S)" },
2323 { ATA_CMD_PIO_READ_EXT, "READ SECTOR(S) EXT" },
2324 { ATA_CMD_PIO_WRITE, "WRITE SECTOR(S)" },
2325 { ATA_CMD_PIO_WRITE_EXT, "WRITE SECTOR(S) EXT" },
2326 { ATA_CMD_READ_MULTI, "READ MULTIPLE" },
2327 { ATA_CMD_READ_MULTI_EXT, "READ MULTIPLE EXT" },
2328 { ATA_CMD_WRITE_MULTI, "WRITE MULTIPLE" },
2329 { ATA_CMD_WRITE_MULTI_EXT, "WRITE MULTIPLE EXT" },
2330 { ATA_CMD_WRITE_MULTI_FUA_EXT, "WRITE MULTIPLE FUA EXT" },
2331 { ATA_CMD_SET_FEATURES, "SET FEATURES" },
2332 { ATA_CMD_SET_MULTI, "SET MULTIPLE MODE" },
2333 { ATA_CMD_VERIFY, "READ VERIFY SECTOR(S)" },
2334 { ATA_CMD_VERIFY_EXT, "READ VERIFY SECTOR(S) EXT" },
2335 { ATA_CMD_WRITE_UNCORR_EXT, "WRITE UNCORRECTABLE EXT" },
2336 { ATA_CMD_STANDBYNOW1, "STANDBY IMMEDIATE" },
2337 { ATA_CMD_IDLEIMMEDIATE, "IDLE IMMEDIATE" },
2338 { ATA_CMD_SLEEP, "SLEEP" },
2339 { ATA_CMD_INIT_DEV_PARAMS, "INITIALIZE DEVICE PARAMETERS" },
2340 { ATA_CMD_READ_NATIVE_MAX, "READ NATIVE MAX ADDRESS" },
2341 { ATA_CMD_READ_NATIVE_MAX_EXT, "READ NATIVE MAX ADDRESS EXT" },
2342 { ATA_CMD_SET_MAX, "SET MAX ADDRESS" },
2343 { ATA_CMD_SET_MAX_EXT, "SET MAX ADDRESS EXT" },
2344 { ATA_CMD_READ_LOG_EXT, "READ LOG EXT" },
2345 { ATA_CMD_WRITE_LOG_EXT, "WRITE LOG EXT" },
2346 { ATA_CMD_READ_LOG_DMA_EXT, "READ LOG DMA EXT" },
2347 { ATA_CMD_WRITE_LOG_DMA_EXT, "WRITE LOG DMA EXT" },
2348 { ATA_CMD_TRUSTED_NONDATA, "TRUSTED NON-DATA" },
2349 { ATA_CMD_TRUSTED_RCV, "TRUSTED RECEIVE" },
2350 { ATA_CMD_TRUSTED_RCV_DMA, "TRUSTED RECEIVE DMA" },
2351 { ATA_CMD_TRUSTED_SND, "TRUSTED SEND" },
2352 { ATA_CMD_TRUSTED_SND_DMA, "TRUSTED SEND DMA" },
2353 { ATA_CMD_PMP_READ, "READ BUFFER" },
2354 { ATA_CMD_PMP_READ_DMA, "READ BUFFER DMA" },
2355 { ATA_CMD_PMP_WRITE, "WRITE BUFFER" },
2356 { ATA_CMD_PMP_WRITE_DMA, "WRITE BUFFER DMA" },
2357 { ATA_CMD_CONF_OVERLAY, "DEVICE CONFIGURATION OVERLAY" },
2358 { ATA_CMD_SEC_SET_PASS, "SECURITY SET PASSWORD" },
2359 { ATA_CMD_SEC_UNLOCK, "SECURITY UNLOCK" },
2360 { ATA_CMD_SEC_ERASE_PREP, "SECURITY ERASE PREPARE" },
2361 { ATA_CMD_SEC_ERASE_UNIT, "SECURITY ERASE UNIT" },
2362 { ATA_CMD_SEC_FREEZE_LOCK, "SECURITY FREEZE LOCK" },
2363 { ATA_CMD_SEC_DISABLE_PASS, "SECURITY DISABLE PASSWORD" },
2364 { ATA_CMD_CONFIG_STREAM, "CONFIGURE STREAM" },
2365 { ATA_CMD_SMART, "SMART" },
2366 { ATA_CMD_MEDIA_LOCK, "DOOR LOCK" },
2367 { ATA_CMD_MEDIA_UNLOCK, "DOOR UNLOCK" },
2368 { ATA_CMD_DSM, "DATA SET MANAGEMENT" },
2369 { ATA_CMD_CHK_MED_CRD_TYP, "CHECK MEDIA CARD TYPE" },
2370 { ATA_CMD_CFA_REQ_EXT_ERR, "CFA REQUEST EXTENDED ERROR" },
2371 { ATA_CMD_CFA_WRITE_NE, "CFA WRITE SECTORS WITHOUT ERASE" },
2372 { ATA_CMD_CFA_TRANS_SECT, "CFA TRANSLATE SECTOR" },
2373 { ATA_CMD_CFA_ERASE, "CFA ERASE SECTORS" },
2374 { ATA_CMD_CFA_WRITE_MULT_NE, "CFA WRITE MULTIPLE WITHOUT ERASE" },
2375 { ATA_CMD_REQ_SENSE_DATA, "REQUEST SENSE DATA EXT" },
2376 { ATA_CMD_SANITIZE_DEVICE, "SANITIZE DEVICE" },
2377 { ATA_CMD_ZAC_MGMT_IN, "ZAC MANAGEMENT IN" },
2378 { ATA_CMD_ZAC_MGMT_OUT, "ZAC MANAGEMENT OUT" },
2379 { ATA_CMD_READ_LONG, "READ LONG (with retries)" },
2380 { ATA_CMD_READ_LONG_ONCE, "READ LONG (without retries)" },
2381 { ATA_CMD_WRITE_LONG, "WRITE LONG (with retries)" },
2382 { ATA_CMD_WRITE_LONG_ONCE, "WRITE LONG (without retries)" },
2383 { ATA_CMD_RESTORE, "RECALIBRATE" },
2384 { 0, NULL } /* terminate list */
2388 for (i = 0; cmd_descr[i].text; i++)
2389 if (cmd_descr[i].command == command)
2390 return cmd_descr[i].text;
2395 EXPORT_SYMBOL_GPL(ata_get_cmd_descript);
2398 * ata_eh_link_report - report error handling to user
2399 * @link: ATA link EH is going on
2401 * Report EH to user.
2406 static void ata_eh_link_report(struct ata_link *link)
2408 struct ata_port *ap = link->ap;
2409 struct ata_eh_context *ehc = &link->eh_context;
2410 struct ata_queued_cmd *qc;
2411 const char *frozen, *desc;
2412 char tries_buf[6] = "";
2413 int tag, nr_failed = 0;
2415 if (ehc->i.flags & ATA_EHI_QUIET)
2419 if (ehc->i.desc[0] != '\0')
2422 ata_qc_for_each_raw(ap, qc, tag) {
2423 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
2424 ata_dev_phys_link(qc->dev) != link ||
2425 ((qc->flags & ATA_QCFLAG_QUIET) &&
2426 qc->err_mask == AC_ERR_DEV))
2428 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
2434 if (!nr_failed && !ehc->i.err_mask)
2438 if (ap->pflags & ATA_PFLAG_FROZEN)
2441 if (ap->eh_tries < ATA_EH_MAX_TRIES)
2442 snprintf(tries_buf, sizeof(tries_buf), " t%d",
2446 ata_dev_err(ehc->i.dev, "exception Emask 0x%x "
2447 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2448 ehc->i.err_mask, link->sactive, ehc->i.serror,
2449 ehc->i.action, frozen, tries_buf);
2451 ata_dev_err(ehc->i.dev, "%s\n", desc);
2453 ata_link_err(link, "exception Emask 0x%x "
2454 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2455 ehc->i.err_mask, link->sactive, ehc->i.serror,
2456 ehc->i.action, frozen, tries_buf);
2458 ata_link_err(link, "%s\n", desc);
2461 #ifdef CONFIG_ATA_VERBOSE_ERROR
2464 "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
2465 ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
2466 ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
2467 ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
2468 ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
2469 ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
2470 ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
2471 ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
2472 ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
2473 ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
2474 ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
2475 ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
2476 ehc->i.serror & SERR_CRC ? "BadCRC " : "",
2477 ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
2478 ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
2479 ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
2480 ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
2481 ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
2484 ata_qc_for_each_raw(ap, qc, tag) {
2485 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
2486 char data_buf[20] = "";
2487 char cdb_buf[70] = "";
2489 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
2490 ata_dev_phys_link(qc->dev) != link || !qc->err_mask)
2493 if (qc->dma_dir != DMA_NONE) {
2494 static const char *dma_str[] = {
2495 [DMA_BIDIRECTIONAL] = "bidi",
2496 [DMA_TO_DEVICE] = "out",
2497 [DMA_FROM_DEVICE] = "in",
2499 const char *prot_str = NULL;
2501 switch (qc->tf.protocol) {
2502 case ATA_PROT_UNKNOWN:
2503 prot_str = "unknown";
2505 case ATA_PROT_NODATA:
2506 prot_str = "nodata";
2515 prot_str = "ncq dma";
2517 case ATA_PROT_NCQ_NODATA:
2518 prot_str = "ncq nodata";
2520 case ATAPI_PROT_NODATA:
2521 prot_str = "nodata";
2523 case ATAPI_PROT_PIO:
2526 case ATAPI_PROT_DMA:
2530 snprintf(data_buf, sizeof(data_buf), " %s %u %s",
2531 prot_str, qc->nbytes, dma_str[qc->dma_dir]);
2534 if (ata_is_atapi(qc->tf.protocol)) {
2535 const u8 *cdb = qc->cdb;
2536 size_t cdb_len = qc->dev->cdb_len;
2539 cdb = qc->scsicmd->cmnd;
2540 cdb_len = qc->scsicmd->cmd_len;
2542 __scsi_format_command(cdb_buf, sizeof(cdb_buf),
2545 const char *descr = ata_get_cmd_descript(cmd->command);
2547 ata_dev_err(qc->dev, "failed command: %s\n",
2551 ata_dev_err(qc->dev,
2552 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
2554 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
2555 "Emask 0x%x (%s)%s\n",
2556 cmd->command, cmd->feature, cmd->nsect,
2557 cmd->lbal, cmd->lbam, cmd->lbah,
2558 cmd->hob_feature, cmd->hob_nsect,
2559 cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
2560 cmd->device, qc->tag, data_buf, cdb_buf,
2561 res->command, res->feature, res->nsect,
2562 res->lbal, res->lbam, res->lbah,
2563 res->hob_feature, res->hob_nsect,
2564 res->hob_lbal, res->hob_lbam, res->hob_lbah,
2565 res->device, qc->err_mask, ata_err_string(qc->err_mask),
2566 qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
2568 #ifdef CONFIG_ATA_VERBOSE_ERROR
2569 if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
2570 ATA_SENSE | ATA_ERR)) {
2571 if (res->command & ATA_BUSY)
2572 ata_dev_err(qc->dev, "status: { Busy }\n");
2574 ata_dev_err(qc->dev, "status: { %s%s%s%s%s}\n",
2575 res->command & ATA_DRDY ? "DRDY " : "",
2576 res->command & ATA_DF ? "DF " : "",
2577 res->command & ATA_DRQ ? "DRQ " : "",
2578 res->command & ATA_SENSE ? "SENSE " : "",
2579 res->command & ATA_ERR ? "ERR " : "");
2582 if (cmd->command != ATA_CMD_PACKET &&
2583 (res->feature & (ATA_ICRC | ATA_UNC | ATA_AMNF |
2584 ATA_IDNF | ATA_ABORTED)))
2585 ata_dev_err(qc->dev, "error: { %s%s%s%s%s}\n",
2586 res->feature & ATA_ICRC ? "ICRC " : "",
2587 res->feature & ATA_UNC ? "UNC " : "",
2588 res->feature & ATA_AMNF ? "AMNF " : "",
2589 res->feature & ATA_IDNF ? "IDNF " : "",
2590 res->feature & ATA_ABORTED ? "ABRT " : "");
2596 * ata_eh_report - report error handling to user
2597 * @ap: ATA port to report EH about
2599 * Report EH to user.
2604 void ata_eh_report(struct ata_port *ap)
2606 struct ata_link *link;
2608 ata_for_each_link(link, ap, HOST_FIRST)
2609 ata_eh_link_report(link);
2612 static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
2613 unsigned int *classes, unsigned long deadline,
2616 struct ata_device *dev;
2619 ata_for_each_dev(dev, link, ALL)
2620 classes[dev->devno] = ATA_DEV_UNKNOWN;
2622 return reset(link, classes, deadline);
2625 static int ata_eh_followup_srst_needed(struct ata_link *link, int rc)
2627 if ((link->flags & ATA_LFLAG_NO_SRST) || ata_link_offline(link))
2631 if (sata_pmp_supported(link->ap) && ata_is_host_link(link))
2636 int ata_eh_reset(struct ata_link *link, int classify,
2637 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
2638 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
2640 struct ata_port *ap = link->ap;
2641 struct ata_link *slave = ap->slave_link;
2642 struct ata_eh_context *ehc = &link->eh_context;
2643 struct ata_eh_context *sehc = slave ? &slave->eh_context : NULL;
2644 unsigned int *classes = ehc->classes;
2645 unsigned int lflags = link->flags;
2646 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
2647 int max_tries = 0, try = 0;
2648 struct ata_link *failed_link;
2649 struct ata_device *dev;
2650 unsigned long deadline, now;
2651 ata_reset_fn_t reset;
2652 unsigned long flags;
2659 while (ata_eh_reset_timeouts[max_tries] != ULONG_MAX)
2661 if (link->flags & ATA_LFLAG_RST_ONCE)
2663 if (link->flags & ATA_LFLAG_NO_HRST)
2665 if (link->flags & ATA_LFLAG_NO_SRST)
2668 /* make sure each reset attempt is at least COOL_DOWN apart */
2669 if (ehc->i.flags & ATA_EHI_DID_RESET) {
2671 WARN_ON(time_after(ehc->last_reset, now));
2672 deadline = ata_deadline(ehc->last_reset,
2673 ATA_EH_RESET_COOL_DOWN);
2674 if (time_before(now, deadline))
2675 schedule_timeout_uninterruptible(deadline - now);
2678 spin_lock_irqsave(ap->lock, flags);
2679 ap->pflags |= ATA_PFLAG_RESETTING;
2680 spin_unlock_irqrestore(ap->lock, flags);
2682 ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
2684 ata_for_each_dev(dev, link, ALL) {
2685 /* If we issue an SRST then an ATA drive (not ATAPI)
2686 * may change configuration and be in PIO0 timing. If
2687 * we do a hard reset (or are coming from power on)
2688 * this is true for ATA or ATAPI. Until we've set a
2689 * suitable controller mode we should not touch the
2690 * bus as we may be talking too fast.
2692 dev->pio_mode = XFER_PIO_0;
2693 dev->dma_mode = 0xff;
2695 /* If the controller has a pio mode setup function
2696 * then use it to set the chipset to rights. Don't
2697 * touch the DMA setup as that will be dealt with when
2698 * configuring devices.
2700 if (ap->ops->set_piomode)
2701 ap->ops->set_piomode(ap, dev);
2704 /* prefer hardreset */
2706 ehc->i.action &= ~ATA_EH_RESET;
2709 ehc->i.action |= ATA_EH_HARDRESET;
2710 } else if (softreset) {
2712 ehc->i.action |= ATA_EH_SOFTRESET;
2716 unsigned long deadline = ata_deadline(jiffies,
2717 ATA_EH_PRERESET_TIMEOUT);
2720 sehc->i.action &= ~ATA_EH_RESET;
2721 sehc->i.action |= ehc->i.action;
2724 rc = prereset(link, deadline);
2726 /* If present, do prereset on slave link too. Reset
2727 * is skipped iff both master and slave links report
2728 * -ENOENT or clear ATA_EH_RESET.
2730 if (slave && (rc == 0 || rc == -ENOENT)) {
2733 tmp = prereset(slave, deadline);
2737 ehc->i.action |= sehc->i.action;
2741 if (rc == -ENOENT) {
2742 ata_link_dbg(link, "port disabled--ignoring\n");
2743 ehc->i.action &= ~ATA_EH_RESET;
2745 ata_for_each_dev(dev, link, ALL)
2746 classes[dev->devno] = ATA_DEV_NONE;
2751 "prereset failed (errno=%d)\n",
2756 /* prereset() might have cleared ATA_EH_RESET. If so,
2757 * bang classes, thaw and return.
2759 if (reset && !(ehc->i.action & ATA_EH_RESET)) {
2760 ata_for_each_dev(dev, link, ALL)
2761 classes[dev->devno] = ATA_DEV_NONE;
2762 if ((ap->pflags & ATA_PFLAG_FROZEN) &&
2763 ata_is_host_link(link))
2764 ata_eh_thaw_port(ap);
2774 if (ata_is_host_link(link))
2775 ata_eh_freeze_port(ap);
2777 deadline = ata_deadline(jiffies, ata_eh_reset_timeouts[try++]);
2781 ata_link_info(link, "%s resetting link\n",
2782 reset == softreset ? "soft" : "hard");
2784 /* mark that this EH session started with reset */
2785 ehc->last_reset = jiffies;
2786 if (reset == hardreset)
2787 ehc->i.flags |= ATA_EHI_DID_HARDRESET;
2789 ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
2791 rc = ata_do_reset(link, reset, classes, deadline, true);
2792 if (rc && rc != -EAGAIN) {
2797 /* hardreset slave link if existent */
2798 if (slave && reset == hardreset) {
2802 ata_link_info(slave, "hard resetting link\n");
2804 ata_eh_about_to_do(slave, NULL, ATA_EH_RESET);
2805 tmp = ata_do_reset(slave, reset, classes, deadline,
2813 failed_link = slave;
2819 /* perform follow-up SRST if necessary */
2820 if (reset == hardreset &&
2821 ata_eh_followup_srst_needed(link, rc)) {
2826 "follow-up softreset required but no softreset available\n");
2832 ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
2833 rc = ata_do_reset(link, reset, classes, deadline, true);
2842 "no reset method available, skipping reset\n");
2843 if (!(lflags & ATA_LFLAG_ASSUME_CLASS))
2844 lflags |= ATA_LFLAG_ASSUME_ATA;
2848 * Post-reset processing
2850 ata_for_each_dev(dev, link, ALL) {
2851 /* After the reset, the device state is PIO 0 and the
2852 * controller state is undefined. Reset also wakes up
2853 * drives from sleeping mode.
2855 dev->pio_mode = XFER_PIO_0;
2856 dev->flags &= ~ATA_DFLAG_SLEEPING;
2858 if (ata_phys_link_offline(ata_dev_phys_link(dev)))
2861 /* apply class override */
2862 if (lflags & ATA_LFLAG_ASSUME_ATA)
2863 classes[dev->devno] = ATA_DEV_ATA;
2864 else if (lflags & ATA_LFLAG_ASSUME_SEMB)
2865 classes[dev->devno] = ATA_DEV_SEMB_UNSUP;
2868 /* record current link speed */
2869 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2870 link->sata_spd = (sstatus >> 4) & 0xf;
2871 if (slave && sata_scr_read(slave, SCR_STATUS, &sstatus) == 0)
2872 slave->sata_spd = (sstatus >> 4) & 0xf;
2875 if (ata_is_host_link(link))
2876 ata_eh_thaw_port(ap);
2878 /* postreset() should clear hardware SError. Although SError
2879 * is cleared during link resume, clearing SError here is
2880 * necessary as some PHYs raise hotplug events after SRST.
2881 * This introduces race condition where hotplug occurs between
2882 * reset and here. This race is mediated by cross checking
2883 * link onlineness and classification result later.
2886 postreset(link, classes);
2888 postreset(slave, classes);
2892 * Some controllers can't be frozen very well and may set spurious
2893 * error conditions during reset. Clear accumulated error
2894 * information and re-thaw the port if frozen. As reset is the
2895 * final recovery action and we cross check link onlineness against
2896 * device classification later, no hotplug event is lost by this.
2898 spin_lock_irqsave(link->ap->lock, flags);
2899 memset(&link->eh_info, 0, sizeof(link->eh_info));
2901 memset(&slave->eh_info, 0, sizeof(link->eh_info));
2902 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
2903 spin_unlock_irqrestore(link->ap->lock, flags);
2905 if (ap->pflags & ATA_PFLAG_FROZEN)
2906 ata_eh_thaw_port(ap);
2909 * Make sure onlineness and classification result correspond.
2910 * Hotplug could have happened during reset and some
2911 * controllers fail to wait while a drive is spinning up after
2912 * being hotplugged causing misdetection. By cross checking
2913 * link on/offlineness and classification result, those
2914 * conditions can be reliably detected and retried.
2917 ata_for_each_dev(dev, link, ALL) {
2918 if (ata_phys_link_online(ata_dev_phys_link(dev))) {
2919 if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
2920 ata_dev_dbg(dev, "link online but device misclassified\n");
2921 classes[dev->devno] = ATA_DEV_NONE;
2924 } else if (ata_phys_link_offline(ata_dev_phys_link(dev))) {
2925 if (ata_class_enabled(classes[dev->devno]))
2927 "link offline, clearing class %d to NONE\n",
2928 classes[dev->devno]);
2929 classes[dev->devno] = ATA_DEV_NONE;
2930 } else if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
2932 "link status unknown, clearing UNKNOWN to NONE\n");
2933 classes[dev->devno] = ATA_DEV_NONE;
2937 if (classify && nr_unknown) {
2938 if (try < max_tries) {
2940 "link online but %d devices misclassified, retrying\n",
2947 "link online but %d devices misclassified, "
2948 "device detection might fail\n", nr_unknown);
2951 /* reset successful, schedule revalidation */
2952 ata_eh_done(link, NULL, ATA_EH_RESET);
2954 ata_eh_done(slave, NULL, ATA_EH_RESET);
2955 ehc->last_reset = jiffies; /* update to completion time */
2956 ehc->i.action |= ATA_EH_REVALIDATE;
2957 link->lpm_policy = ATA_LPM_UNKNOWN; /* reset LPM state */
2961 /* clear hotplug flag */
2962 ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2964 sehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2966 spin_lock_irqsave(ap->lock, flags);
2967 ap->pflags &= ~ATA_PFLAG_RESETTING;
2968 spin_unlock_irqrestore(ap->lock, flags);
2973 /* if SCR isn't accessible on a fan-out port, PMP needs to be reset */
2974 if (!ata_is_host_link(link) &&
2975 sata_scr_read(link, SCR_STATUS, &sstatus))
2978 if (try >= max_tries) {
2980 * Thaw host port even if reset failed, so that the port
2981 * can be retried on the next phy event. This risks
2982 * repeated EH runs but seems to be a better tradeoff than
2983 * shutting down a port after a botched hotplug attempt.
2985 if (ata_is_host_link(link))
2986 ata_eh_thaw_port(ap);
2991 if (time_before(now, deadline)) {
2992 unsigned long delta = deadline - now;
2994 ata_link_warn(failed_link,
2995 "reset failed (errno=%d), retrying in %u secs\n",
2996 rc, DIV_ROUND_UP(jiffies_to_msecs(delta), 1000));
3000 delta = schedule_timeout_uninterruptible(delta);
3005 * While disks spinup behind PMP, some controllers fail sending SRST.
3006 * They need to be reset - as well as the PMP - before retrying.
3008 if (rc == -ERESTART) {
3009 if (ata_is_host_link(link))
3010 ata_eh_thaw_port(ap);
3014 if (try == max_tries - 1) {
3015 sata_down_spd_limit(link, 0);
3017 sata_down_spd_limit(slave, 0);
3018 } else if (rc == -EPIPE)
3019 sata_down_spd_limit(failed_link, 0);
3026 static inline void ata_eh_pull_park_action(struct ata_port *ap)
3028 struct ata_link *link;
3029 struct ata_device *dev;
3030 unsigned long flags;
3033 * This function can be thought of as an extended version of
3034 * ata_eh_about_to_do() specially crafted to accommodate the
3035 * requirements of ATA_EH_PARK handling. Since the EH thread
3036 * does not leave the do {} while () loop in ata_eh_recover as
3037 * long as the timeout for a park request to *one* device on
3038 * the port has not expired, and since we still want to pick
3039 * up park requests to other devices on the same port or
3040 * timeout updates for the same device, we have to pull
3041 * ATA_EH_PARK actions from eh_info into eh_context.i
3042 * ourselves at the beginning of each pass over the loop.
3044 * Additionally, all write accesses to &ap->park_req_pending
3045 * through reinit_completion() (see below) or complete_all()
3046 * (see ata_scsi_park_store()) are protected by the host lock.
3047 * As a result we have that park_req_pending.done is zero on
3048 * exit from this function, i.e. when ATA_EH_PARK actions for
3049 * *all* devices on port ap have been pulled into the
3050 * respective eh_context structs. If, and only if,
3051 * park_req_pending.done is non-zero by the time we reach
3052 * wait_for_completion_timeout(), another ATA_EH_PARK action
3053 * has been scheduled for at least one of the devices on port
3054 * ap and we have to cycle over the do {} while () loop in
3055 * ata_eh_recover() again.
3058 spin_lock_irqsave(ap->lock, flags);
3059 reinit_completion(&ap->park_req_pending);
3060 ata_for_each_link(link, ap, EDGE) {
3061 ata_for_each_dev(dev, link, ALL) {
3062 struct ata_eh_info *ehi = &link->eh_info;
3064 link->eh_context.i.dev_action[dev->devno] |=
3065 ehi->dev_action[dev->devno] & ATA_EH_PARK;
3066 ata_eh_clear_action(link, dev, ehi, ATA_EH_PARK);
3069 spin_unlock_irqrestore(ap->lock, flags);
3072 static void ata_eh_park_issue_cmd(struct ata_device *dev, int park)
3074 struct ata_eh_context *ehc = &dev->link->eh_context;
3075 struct ata_taskfile tf;
3076 unsigned int err_mask;
3078 ata_tf_init(dev, &tf);
3080 ehc->unloaded_mask |= 1 << dev->devno;
3081 tf.command = ATA_CMD_IDLEIMMEDIATE;
3087 ehc->unloaded_mask &= ~(1 << dev->devno);
3088 tf.command = ATA_CMD_CHK_POWER;
3091 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
3092 tf.protocol = ATA_PROT_NODATA;
3093 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
3094 if (park && (err_mask || tf.lbal != 0xc4)) {
3095 ata_dev_err(dev, "head unload failed!\n");
3096 ehc->unloaded_mask &= ~(1 << dev->devno);
3100 static int ata_eh_revalidate_and_attach(struct ata_link *link,
3101 struct ata_device **r_failed_dev)
3103 struct ata_port *ap = link->ap;
3104 struct ata_eh_context *ehc = &link->eh_context;
3105 struct ata_device *dev;
3106 unsigned int new_mask = 0;
3107 unsigned long flags;
3112 /* For PATA drive side cable detection to work, IDENTIFY must
3113 * be done backwards such that PDIAG- is released by the slave
3114 * device before the master device is identified.
3116 ata_for_each_dev(dev, link, ALL_REVERSE) {
3117 unsigned int action = ata_eh_dev_action(dev);
3118 unsigned int readid_flags = 0;
3120 if (ehc->i.flags & ATA_EHI_DID_RESET)
3121 readid_flags |= ATA_READID_POSTRESET;
3123 if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
3124 WARN_ON(dev->class == ATA_DEV_PMP);
3126 if (ata_phys_link_offline(ata_dev_phys_link(dev))) {
3131 ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
3132 rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
3137 ata_eh_done(link, dev, ATA_EH_REVALIDATE);
3139 /* Configuration may have changed, reconfigure
3142 ehc->i.flags |= ATA_EHI_SETMODE;
3144 /* schedule the scsi_rescan_device() here */
3145 schedule_work(&(ap->scsi_rescan_task));
3146 } else if (dev->class == ATA_DEV_UNKNOWN &&
3147 ehc->tries[dev->devno] &&
3148 ata_class_enabled(ehc->classes[dev->devno])) {
3149 /* Temporarily set dev->class, it will be
3150 * permanently set once all configurations are
3151 * complete. This is necessary because new
3152 * device configuration is done in two
3155 dev->class = ehc->classes[dev->devno];
3157 if (dev->class == ATA_DEV_PMP)
3158 rc = sata_pmp_attach(dev);
3160 rc = ata_dev_read_id(dev, &dev->class,
3161 readid_flags, dev->id);
3163 /* read_id might have changed class, store and reset */
3164 ehc->classes[dev->devno] = dev->class;
3165 dev->class = ATA_DEV_UNKNOWN;
3169 /* clear error info accumulated during probe */
3170 ata_ering_clear(&dev->ering);
3171 new_mask |= 1 << dev->devno;
3174 /* IDENTIFY was issued to non-existent
3175 * device. No need to reset. Just
3176 * thaw and ignore the device.
3178 ata_eh_thaw_port(ap);
3186 /* PDIAG- should have been released, ask cable type if post-reset */
3187 if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) {
3188 if (ap->ops->cable_detect)
3189 ap->cbl = ap->ops->cable_detect(ap);
3193 /* Configure new devices forward such that user doesn't see
3194 * device detection messages backwards.
3196 ata_for_each_dev(dev, link, ALL) {
3197 if (!(new_mask & (1 << dev->devno)))
3200 dev->class = ehc->classes[dev->devno];
3202 if (dev->class == ATA_DEV_PMP)
3205 ehc->i.flags |= ATA_EHI_PRINTINFO;
3206 rc = ata_dev_configure(dev);
3207 ehc->i.flags &= ~ATA_EHI_PRINTINFO;
3209 dev->class = ATA_DEV_UNKNOWN;
3213 spin_lock_irqsave(ap->lock, flags);
3214 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
3215 spin_unlock_irqrestore(ap->lock, flags);
3217 /* new device discovered, configure xfermode */
3218 ehc->i.flags |= ATA_EHI_SETMODE;
3224 *r_failed_dev = dev;
3225 DPRINTK("EXIT rc=%d\n", rc);
3230 * ata_set_mode - Program timings and issue SET FEATURES - XFER
3231 * @link: link on which timings will be programmed
3232 * @r_failed_dev: out parameter for failed device
3234 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3235 * ata_set_mode() fails, pointer to the failing device is
3236 * returned in @r_failed_dev.
3239 * PCI/etc. bus probe sem.
3242 * 0 on success, negative errno otherwise
3244 int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3246 struct ata_port *ap = link->ap;
3247 struct ata_device *dev;
3250 /* if data transfer is verified, clear DUBIOUS_XFER on ering top */
3251 ata_for_each_dev(dev, link, ENABLED) {
3252 if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
3253 struct ata_ering_entry *ent;
3255 ent = ata_ering_top(&dev->ering);
3257 ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER;
3261 /* has private set_mode? */
3262 if (ap->ops->set_mode)
3263 rc = ap->ops->set_mode(link, r_failed_dev);
3265 rc = ata_do_set_mode(link, r_failed_dev);
3267 /* if transfer mode has changed, set DUBIOUS_XFER on device */
3268 ata_for_each_dev(dev, link, ENABLED) {
3269 struct ata_eh_context *ehc = &link->eh_context;
3270 u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
3271 u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
3273 if (dev->xfer_mode != saved_xfer_mode ||
3274 ata_ncq_enabled(dev) != saved_ncq)
3275 dev->flags |= ATA_DFLAG_DUBIOUS_XFER;
3282 * atapi_eh_clear_ua - Clear ATAPI UNIT ATTENTION after reset
3283 * @dev: ATAPI device to clear UA for
3285 * Resets and other operations can make an ATAPI device raise
3286 * UNIT ATTENTION which causes the next operation to fail. This
3287 * function clears UA.
3290 * EH context (may sleep).
3293 * 0 on success, -errno on failure.
3295 static int atapi_eh_clear_ua(struct ata_device *dev)
3299 for (i = 0; i < ATA_EH_UA_TRIES; i++) {
3300 u8 *sense_buffer = dev->link->ap->sector_buf;
3302 unsigned int err_mask;
3304 err_mask = atapi_eh_tur(dev, &sense_key);
3305 if (err_mask != 0 && err_mask != AC_ERR_DEV) {
3307 "TEST_UNIT_READY failed (err_mask=0x%x)\n",
3312 if (!err_mask || sense_key != UNIT_ATTENTION)
3315 err_mask = atapi_eh_request_sense(dev, sense_buffer, sense_key);
3317 ata_dev_warn(dev, "failed to clear "
3318 "UNIT ATTENTION (err_mask=0x%x)\n", err_mask);
3323 ata_dev_warn(dev, "UNIT ATTENTION persists after %d tries\n",
3330 * ata_eh_maybe_retry_flush - Retry FLUSH if necessary
3331 * @dev: ATA device which may need FLUSH retry
3333 * If @dev failed FLUSH, it needs to be reported upper layer
3334 * immediately as it means that @dev failed to remap and already
3335 * lost at least a sector and further FLUSH retrials won't make
3336 * any difference to the lost sector. However, if FLUSH failed
3337 * for other reasons, for example transmission error, FLUSH needs
3340 * This function determines whether FLUSH failure retry is
3341 * necessary and performs it if so.
3344 * 0 if EH can continue, -errno if EH needs to be repeated.
3346 static int ata_eh_maybe_retry_flush(struct ata_device *dev)
3348 struct ata_link *link = dev->link;
3349 struct ata_port *ap = link->ap;
3350 struct ata_queued_cmd *qc;
3351 struct ata_taskfile tf;
3352 unsigned int err_mask;
3355 /* did flush fail for this device? */
3356 if (!ata_tag_valid(link->active_tag))
3359 qc = __ata_qc_from_tag(ap, link->active_tag);
3360 if (qc->dev != dev || (qc->tf.command != ATA_CMD_FLUSH_EXT &&
3361 qc->tf.command != ATA_CMD_FLUSH))
3364 /* if the device failed it, it should be reported to upper layers */
3365 if (qc->err_mask & AC_ERR_DEV)
3368 /* flush failed for some other reason, give it another shot */
3369 ata_tf_init(dev, &tf);
3371 tf.command = qc->tf.command;
3372 tf.flags |= ATA_TFLAG_DEVICE;
3373 tf.protocol = ATA_PROT_NODATA;
3375 ata_dev_warn(dev, "retrying FLUSH 0x%x Emask 0x%x\n",
3376 tf.command, qc->err_mask);
3378 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
3381 * FLUSH is complete but there's no way to
3382 * successfully complete a failed command from EH.
3383 * Making sure retry is allowed at least once and
3384 * retrying it should do the trick - whatever was in
3385 * the cache is already on the platter and this won't
3386 * cause infinite loop.
3388 qc->scsicmd->allowed = max(qc->scsicmd->allowed, 1);
3390 ata_dev_warn(dev, "FLUSH failed Emask 0x%x\n",
3394 /* if device failed it, report it to upper layers */
3395 if (err_mask & AC_ERR_DEV) {
3396 qc->err_mask |= AC_ERR_DEV;
3398 if (!(ap->pflags & ATA_PFLAG_FROZEN))
3406 * ata_eh_set_lpm - configure SATA interface power management
3407 * @link: link to configure power management
3408 * @policy: the link power management policy
3409 * @r_failed_dev: out parameter for failed device
3411 * Enable SATA Interface power management. This will enable
3412 * Device Interface Power Management (DIPM) for min_power and
3413 * medium_power_with_dipm policies, and then call driver specific
3414 * callbacks for enabling Host Initiated Power management.
3420 * 0 on success, -errno on failure.
3422 static int ata_eh_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
3423 struct ata_device **r_failed_dev)
3425 struct ata_port *ap = ata_is_host_link(link) ? link->ap : NULL;
3426 struct ata_eh_context *ehc = &link->eh_context;
3427 struct ata_device *dev, *link_dev = NULL, *lpm_dev = NULL;
3428 enum ata_lpm_policy old_policy = link->lpm_policy;
3429 bool no_dipm = link->ap->flags & ATA_FLAG_NO_DIPM;
3430 unsigned int hints = ATA_LPM_EMPTY | ATA_LPM_HIPM;
3431 unsigned int err_mask;
3434 /* if the link or host doesn't do LPM, noop */
3435 if ((link->flags & ATA_LFLAG_NO_LPM) || (ap && !ap->ops->set_lpm))
3439 * DIPM is enabled only for MIN_POWER as some devices
3440 * misbehave when the host NACKs transition to SLUMBER. Order
3441 * device and link configurations such that the host always
3442 * allows DIPM requests.
3444 ata_for_each_dev(dev, link, ENABLED) {
3445 bool hipm = ata_id_has_hipm(dev->id);
3446 bool dipm = ata_id_has_dipm(dev->id) && !no_dipm;
3448 /* find the first enabled and LPM enabled devices */
3452 if (!lpm_dev && (hipm || dipm))
3455 hints &= ~ATA_LPM_EMPTY;
3457 hints &= ~ATA_LPM_HIPM;
3459 /* disable DIPM before changing link config */
3460 if (policy < ATA_LPM_MED_POWER_WITH_DIPM && dipm) {
3461 err_mask = ata_dev_set_feature(dev,
3462 SETFEATURES_SATA_DISABLE, SATA_DIPM);
3463 if (err_mask && err_mask != AC_ERR_DEV) {
3465 "failed to disable DIPM, Emask 0x%x\n",
3474 rc = ap->ops->set_lpm(link, policy, hints);
3475 if (!rc && ap->slave_link)
3476 rc = ap->ops->set_lpm(ap->slave_link, policy, hints);
3478 rc = sata_pmp_set_lpm(link, policy, hints);
3481 * Attribute link config failure to the first (LPM) enabled
3482 * device on the link.
3485 if (rc == -EOPNOTSUPP) {
3486 link->flags |= ATA_LFLAG_NO_LPM;
3489 dev = lpm_dev ? lpm_dev : link_dev;
3494 * Low level driver acked the transition. Issue DIPM command
3495 * with the new policy set.
3497 link->lpm_policy = policy;
3498 if (ap && ap->slave_link)
3499 ap->slave_link->lpm_policy = policy;
3501 /* host config updated, enable DIPM if transitioning to MIN_POWER */
3502 ata_for_each_dev(dev, link, ENABLED) {
3503 if (policy >= ATA_LPM_MED_POWER_WITH_DIPM && !no_dipm &&
3504 ata_id_has_dipm(dev->id)) {
3505 err_mask = ata_dev_set_feature(dev,
3506 SETFEATURES_SATA_ENABLE, SATA_DIPM);
3507 if (err_mask && err_mask != AC_ERR_DEV) {
3509 "failed to enable DIPM, Emask 0x%x\n",
3517 link->last_lpm_change = jiffies;
3518 link->flags |= ATA_LFLAG_CHANGED;
3523 /* restore the old policy */
3524 link->lpm_policy = old_policy;
3525 if (ap && ap->slave_link)
3526 ap->slave_link->lpm_policy = old_policy;
3528 /* if no device or only one more chance is left, disable LPM */
3529 if (!dev || ehc->tries[dev->devno] <= 2) {
3530 ata_link_warn(link, "disabling LPM on the link\n");
3531 link->flags |= ATA_LFLAG_NO_LPM;
3534 *r_failed_dev = dev;
3538 int ata_link_nr_enabled(struct ata_link *link)
3540 struct ata_device *dev;
3543 ata_for_each_dev(dev, link, ENABLED)
3548 static int ata_link_nr_vacant(struct ata_link *link)
3550 struct ata_device *dev;
3553 ata_for_each_dev(dev, link, ALL)
3554 if (dev->class == ATA_DEV_UNKNOWN)
3559 static int ata_eh_skip_recovery(struct ata_link *link)
3561 struct ata_port *ap = link->ap;
3562 struct ata_eh_context *ehc = &link->eh_context;
3563 struct ata_device *dev;
3565 /* skip disabled links */
3566 if (link->flags & ATA_LFLAG_DISABLED)
3569 /* skip if explicitly requested */
3570 if (ehc->i.flags & ATA_EHI_NO_RECOVERY)
3573 /* thaw frozen port and recover failed devices */
3574 if ((ap->pflags & ATA_PFLAG_FROZEN) || ata_link_nr_enabled(link))
3577 /* reset at least once if reset is requested */
3578 if ((ehc->i.action & ATA_EH_RESET) &&
3579 !(ehc->i.flags & ATA_EHI_DID_RESET))
3582 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
3583 ata_for_each_dev(dev, link, ALL) {
3584 if (dev->class == ATA_DEV_UNKNOWN &&
3585 ehc->classes[dev->devno] != ATA_DEV_NONE)
3592 static int ata_count_probe_trials_cb(struct ata_ering_entry *ent, void *void_arg)
3594 u64 interval = msecs_to_jiffies(ATA_EH_PROBE_TRIAL_INTERVAL);
3595 u64 now = get_jiffies_64();
3596 int *trials = void_arg;
3598 if ((ent->eflags & ATA_EFLAG_OLD_ER) ||
3599 (ent->timestamp < now - min(now, interval)))
3606 static int ata_eh_schedule_probe(struct ata_device *dev)
3608 struct ata_eh_context *ehc = &dev->link->eh_context;
3609 struct ata_link *link = ata_dev_phys_link(dev);
3612 if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
3613 (ehc->did_probe_mask & (1 << dev->devno)))
3616 ata_eh_detach_dev(dev);
3618 ehc->did_probe_mask |= (1 << dev->devno);
3619 ehc->i.action |= ATA_EH_RESET;
3620 ehc->saved_xfer_mode[dev->devno] = 0;
3621 ehc->saved_ncq_enabled &= ~(1 << dev->devno);
3623 /* the link maybe in a deep sleep, wake it up */
3624 if (link->lpm_policy > ATA_LPM_MAX_POWER) {
3625 if (ata_is_host_link(link))
3626 link->ap->ops->set_lpm(link, ATA_LPM_MAX_POWER,
3629 sata_pmp_set_lpm(link, ATA_LPM_MAX_POWER,
3633 /* Record and count probe trials on the ering. The specific
3634 * error mask used is irrelevant. Because a successful device
3635 * detection clears the ering, this count accumulates only if
3636 * there are consecutive failed probes.
3638 * If the count is equal to or higher than ATA_EH_PROBE_TRIALS
3639 * in the last ATA_EH_PROBE_TRIAL_INTERVAL, link speed is
3640 * forced to 1.5Gbps.
3642 * This is to work around cases where failed link speed
3643 * negotiation results in device misdetection leading to
3644 * infinite DEVXCHG or PHRDY CHG events.
3646 ata_ering_record(&dev->ering, 0, AC_ERR_OTHER);
3647 ata_ering_map(&dev->ering, ata_count_probe_trials_cb, &trials);
3649 if (trials > ATA_EH_PROBE_TRIALS)
3650 sata_down_spd_limit(link, 1);
3655 static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
3657 struct ata_eh_context *ehc = &dev->link->eh_context;
3659 /* -EAGAIN from EH routine indicates retry without prejudice.
3660 * The requester is responsible for ensuring forward progress.
3663 ehc->tries[dev->devno]--;
3667 /* device missing or wrong IDENTIFY data, schedule probing */
3668 ehc->i.probe_mask |= (1 << dev->devno);
3671 /* give it just one more chance */
3672 ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
3675 if (ehc->tries[dev->devno] == 1) {
3676 /* This is the last chance, better to slow
3677 * down than lose it.
3679 sata_down_spd_limit(ata_dev_phys_link(dev), 0);
3680 if (dev->pio_mode > XFER_PIO_0)
3681 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
3685 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
3686 /* disable device if it has used up all its chances */
3687 ata_dev_disable(dev);
3689 /* detach if offline */
3690 if (ata_phys_link_offline(ata_dev_phys_link(dev)))
3691 ata_eh_detach_dev(dev);
3693 /* schedule probe if necessary */
3694 if (ata_eh_schedule_probe(dev)) {
3695 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
3696 memset(ehc->cmd_timeout_idx[dev->devno], 0,
3697 sizeof(ehc->cmd_timeout_idx[dev->devno]));
3702 ehc->i.action |= ATA_EH_RESET;
3708 * ata_eh_recover - recover host port after error
3709 * @ap: host port to recover
3710 * @prereset: prereset method (can be NULL)
3711 * @softreset: softreset method (can be NULL)
3712 * @hardreset: hardreset method (can be NULL)
3713 * @postreset: postreset method (can be NULL)
3714 * @r_failed_link: out parameter for failed link
3716 * This is the alpha and omega, eum and yang, heart and soul of
3717 * libata exception handling. On entry, actions required to
3718 * recover each link and hotplug requests are recorded in the
3719 * link's eh_context. This function executes all the operations
3720 * with appropriate retrials and fallbacks to resurrect failed
3721 * devices, detach goners and greet newcomers.
3724 * Kernel thread context (may sleep).
3727 * 0 on success, -errno on failure.
3729 int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
3730 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
3731 ata_postreset_fn_t postreset,
3732 struct ata_link **r_failed_link)
3734 struct ata_link *link;
3735 struct ata_device *dev;
3737 unsigned long flags, deadline;
3741 /* prep for recovery */
3742 ata_for_each_link(link, ap, EDGE) {
3743 struct ata_eh_context *ehc = &link->eh_context;
3745 /* re-enable link? */
3746 if (ehc->i.action & ATA_EH_ENABLE_LINK) {
3747 ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
3748 spin_lock_irqsave(ap->lock, flags);
3749 link->flags &= ~ATA_LFLAG_DISABLED;
3750 spin_unlock_irqrestore(ap->lock, flags);
3751 ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
3754 ata_for_each_dev(dev, link, ALL) {
3755 if (link->flags & ATA_LFLAG_NO_RETRY)
3756 ehc->tries[dev->devno] = 1;
3758 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
3760 /* collect port action mask recorded in dev actions */
3761 ehc->i.action |= ehc->i.dev_action[dev->devno] &
3762 ~ATA_EH_PERDEV_MASK;
3763 ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
3765 /* process hotplug request */
3766 if (dev->flags & ATA_DFLAG_DETACH)
3767 ata_eh_detach_dev(dev);
3769 /* schedule probe if necessary */
3770 if (!ata_dev_enabled(dev))
3771 ata_eh_schedule_probe(dev);
3778 /* if UNLOADING, finish immediately */
3779 if (ap->pflags & ATA_PFLAG_UNLOADING)
3783 ata_for_each_link(link, ap, EDGE) {
3784 struct ata_eh_context *ehc = &link->eh_context;
3786 /* skip EH if possible. */
3787 if (ata_eh_skip_recovery(link))
3790 ata_for_each_dev(dev, link, ALL)
3791 ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
3795 ata_for_each_link(link, ap, EDGE) {
3796 struct ata_eh_context *ehc = &link->eh_context;
3798 if (!(ehc->i.action & ATA_EH_RESET))
3801 rc = ata_eh_reset(link, ata_link_nr_vacant(link),
3802 prereset, softreset, hardreset, postreset);
3804 ata_link_err(link, "reset failed, giving up\n");
3813 * clears ATA_EH_PARK in eh_info and resets
3814 * ap->park_req_pending
3816 ata_eh_pull_park_action(ap);
3819 ata_for_each_link(link, ap, EDGE) {
3820 ata_for_each_dev(dev, link, ALL) {
3821 struct ata_eh_context *ehc = &link->eh_context;
3824 if (dev->class != ATA_DEV_ATA &&
3825 dev->class != ATA_DEV_ZAC)
3827 if (!(ehc->i.dev_action[dev->devno] &
3830 tmp = dev->unpark_deadline;
3831 if (time_before(deadline, tmp))
3833 else if (time_before_eq(tmp, jiffies))
3835 if (ehc->unloaded_mask & (1 << dev->devno))
3838 ata_eh_park_issue_cmd(dev, 1);
3843 if (time_before_eq(deadline, now))
3847 deadline = wait_for_completion_timeout(&ap->park_req_pending,
3851 ata_for_each_link(link, ap, EDGE) {
3852 ata_for_each_dev(dev, link, ALL) {
3853 if (!(link->eh_context.unloaded_mask &
3857 ata_eh_park_issue_cmd(dev, 0);
3858 ata_eh_done(link, dev, ATA_EH_PARK);
3864 ata_for_each_link(link, ap, PMP_FIRST) {
3865 struct ata_eh_context *ehc = &link->eh_context;
3867 if (sata_pmp_attached(ap) && ata_is_host_link(link))
3870 /* revalidate existing devices and attach new ones */
3871 rc = ata_eh_revalidate_and_attach(link, &dev);
3875 /* if PMP got attached, return, pmp EH will take care of it */
3876 if (link->device->class == ATA_DEV_PMP) {
3881 /* configure transfer mode if necessary */
3882 if (ehc->i.flags & ATA_EHI_SETMODE) {
3883 rc = ata_set_mode(link, &dev);
3886 ehc->i.flags &= ~ATA_EHI_SETMODE;
3889 /* If reset has been issued, clear UA to avoid
3890 * disrupting the current users of the device.
3892 if (ehc->i.flags & ATA_EHI_DID_RESET) {
3893 ata_for_each_dev(dev, link, ALL) {
3894 if (dev->class != ATA_DEV_ATAPI)
3896 rc = atapi_eh_clear_ua(dev);
3899 if (zpodd_dev_enabled(dev))
3900 zpodd_post_poweron(dev);
3904 /* retry flush if necessary */
3905 ata_for_each_dev(dev, link, ALL) {
3906 if (dev->class != ATA_DEV_ATA &&
3907 dev->class != ATA_DEV_ZAC)
3909 rc = ata_eh_maybe_retry_flush(dev);
3915 /* configure link power saving */
3916 if (link->lpm_policy != ap->target_lpm_policy) {
3917 rc = ata_eh_set_lpm(link, ap->target_lpm_policy, &dev);
3922 /* this link is okay now */
3929 ata_eh_handle_dev_fail(dev, rc);
3931 if (ap->pflags & ATA_PFLAG_FROZEN) {
3932 /* PMP reset requires working host port.
3933 * Can't retry if it's frozen.
3935 if (sata_pmp_attached(ap))
3945 if (rc && r_failed_link)
3946 *r_failed_link = link;
3948 DPRINTK("EXIT, rc=%d\n", rc);
3953 * ata_eh_finish - finish up EH
3954 * @ap: host port to finish EH for
3956 * Recovery is complete. Clean up EH states and retry or finish
3962 void ata_eh_finish(struct ata_port *ap)
3964 struct ata_queued_cmd *qc;
3967 /* retry or finish qcs */
3968 ata_qc_for_each_raw(ap, qc, tag) {
3969 if (!(qc->flags & ATA_QCFLAG_FAILED))
3973 /* FIXME: Once EH migration is complete,
3974 * generate sense data in this function,
3975 * considering both err_mask and tf.
3977 if (qc->flags & ATA_QCFLAG_RETRY)
3978 ata_eh_qc_retry(qc);
3980 ata_eh_qc_complete(qc);
3982 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
3983 ata_eh_qc_complete(qc);
3985 /* feed zero TF to sense generation */
3986 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
3987 ata_eh_qc_retry(qc);
3992 /* make sure nr_active_links is zero after EH */
3993 WARN_ON(ap->nr_active_links);
3994 ap->nr_active_links = 0;
3998 * ata_do_eh - do standard error handling
3999 * @ap: host port to handle error for
4001 * @prereset: prereset method (can be NULL)
4002 * @softreset: softreset method (can be NULL)
4003 * @hardreset: hardreset method (can be NULL)
4004 * @postreset: postreset method (can be NULL)
4006 * Perform standard error handling sequence.
4009 * Kernel thread context (may sleep).
4011 void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
4012 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
4013 ata_postreset_fn_t postreset)
4015 struct ata_device *dev;
4021 rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
4024 ata_for_each_dev(dev, &ap->link, ALL)
4025 ata_dev_disable(dev);
4032 * ata_std_error_handler - standard error handler
4033 * @ap: host port to handle error for
4035 * Standard error handler
4038 * Kernel thread context (may sleep).
4040 void ata_std_error_handler(struct ata_port *ap)
4042 struct ata_port_operations *ops = ap->ops;
4043 ata_reset_fn_t hardreset = ops->hardreset;
4045 /* ignore built-in hardreset if SCR access is not available */
4046 if (hardreset == sata_std_hardreset && !sata_scr_valid(&ap->link))
4049 ata_do_eh(ap, ops->prereset, ops->softreset, hardreset, ops->postreset);
4054 * ata_eh_handle_port_suspend - perform port suspend operation
4055 * @ap: port to suspend
4060 * Kernel thread context (may sleep).
4062 static void ata_eh_handle_port_suspend(struct ata_port *ap)
4064 unsigned long flags;
4066 struct ata_device *dev;
4068 /* are we suspending? */
4069 spin_lock_irqsave(ap->lock, flags);
4070 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
4071 ap->pm_mesg.event & PM_EVENT_RESUME) {
4072 spin_unlock_irqrestore(ap->lock, flags);
4075 spin_unlock_irqrestore(ap->lock, flags);
4077 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
4080 * If we have a ZPODD attached, check its zero
4081 * power ready status before the port is frozen.
4082 * Only needed for runtime suspend.
4084 if (PMSG_IS_AUTO(ap->pm_mesg)) {
4085 ata_for_each_dev(dev, &ap->link, ENABLED) {
4086 if (zpodd_dev_enabled(dev))
4087 zpodd_on_suspend(dev);
4091 /* tell ACPI we're suspending */
4092 rc = ata_acpi_on_suspend(ap);
4097 ata_eh_freeze_port(ap);
4099 if (ap->ops->port_suspend)
4100 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
4102 ata_acpi_set_state(ap, ap->pm_mesg);
4104 /* update the flags */
4105 spin_lock_irqsave(ap->lock, flags);
4107 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
4109 ap->pflags |= ATA_PFLAG_SUSPENDED;
4110 else if (ap->pflags & ATA_PFLAG_FROZEN)
4111 ata_port_schedule_eh(ap);
4113 spin_unlock_irqrestore(ap->lock, flags);
4119 * ata_eh_handle_port_resume - perform port resume operation
4120 * @ap: port to resume
4125 * Kernel thread context (may sleep).
4127 static void ata_eh_handle_port_resume(struct ata_port *ap)
4129 struct ata_link *link;
4130 struct ata_device *dev;
4131 unsigned long flags;
4133 /* are we resuming? */
4134 spin_lock_irqsave(ap->lock, flags);
4135 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
4136 !(ap->pm_mesg.event & PM_EVENT_RESUME)) {
4137 spin_unlock_irqrestore(ap->lock, flags);
4140 spin_unlock_irqrestore(ap->lock, flags);
4142 WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
4145 * Error timestamps are in jiffies which doesn't run while
4146 * suspended and PHY events during resume isn't too uncommon.
4147 * When the two are combined, it can lead to unnecessary speed
4148 * downs if the machine is suspended and resumed repeatedly.
4149 * Clear error history.
4151 ata_for_each_link(link, ap, HOST_FIRST)
4152 ata_for_each_dev(dev, link, ALL)
4153 ata_ering_clear(&dev->ering);
4155 ata_acpi_set_state(ap, ap->pm_mesg);
4157 if (ap->ops->port_resume)
4158 ap->ops->port_resume(ap);
4160 /* tell ACPI that we're resuming */
4161 ata_acpi_on_resume(ap);
4163 /* update the flags */
4164 spin_lock_irqsave(ap->lock, flags);
4165 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
4166 spin_unlock_irqrestore(ap->lock, flags);
4168 #endif /* CONFIG_PM */