2 * libata-eh.c - libata error handling
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
8 * Copyright 2006 Tejun Heo <htejun@gmail.com>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; see the file COPYING. If not, write to
23 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
35 #include <linux/kernel.h>
36 #include <linux/blkdev.h>
37 #include <linux/pci.h>
38 #include <scsi/scsi.h>
39 #include <scsi/scsi_host.h>
40 #include <scsi/scsi_eh.h>
41 #include <scsi/scsi_device.h>
42 #include <scsi/scsi_cmnd.h>
43 #include <scsi/scsi_dbg.h>
44 #include "../scsi/scsi_transport_api.h"
46 #include <linux/libata.h>
51 /* speed down verdicts */
52 ATA_EH_SPDN_NCQ_OFF = (1 << 0),
53 ATA_EH_SPDN_SPEED_DOWN = (1 << 1),
54 ATA_EH_SPDN_FALLBACK_TO_PIO = (1 << 2),
55 ATA_EH_SPDN_KEEP_ERRORS = (1 << 3),
58 ATA_EFLAG_IS_IO = (1 << 0),
59 ATA_EFLAG_DUBIOUS_XFER = (1 << 1),
60 ATA_EFLAG_OLD_ER = (1 << 31),
62 /* error categories */
65 ATA_ECAT_TOUT_HSM = 2,
67 ATA_ECAT_DUBIOUS_NONE = 4,
68 ATA_ECAT_DUBIOUS_ATA_BUS = 5,
69 ATA_ECAT_DUBIOUS_TOUT_HSM = 6,
70 ATA_ECAT_DUBIOUS_UNK_DEV = 7,
73 ATA_EH_CMD_DFL_TIMEOUT = 5000,
75 /* always put at least this amount of time between resets */
76 ATA_EH_RESET_COOL_DOWN = 5000,
78 /* Waiting in ->prereset can never be reliable. It's
79 * sometimes nice to wait there but it can't be depended upon;
80 * otherwise, we wouldn't be resetting. Just give it enough
81 * time for most drives to spin up.
83 ATA_EH_PRERESET_TIMEOUT = 10000,
84 ATA_EH_FASTDRAIN_INTERVAL = 3000,
88 /* probe speed down parameters, see ata_eh_schedule_probe() */
89 ATA_EH_PROBE_TRIAL_INTERVAL = 60000, /* 1 min */
90 ATA_EH_PROBE_TRIALS = 2,
93 /* The following table determines how we sequence resets. Each entry
94 * represents timeout for that try. The first try can be soft or
95 * hardreset. All others are hardreset if available. In most cases
96 * the first reset w/ 10sec timeout should succeed. Following entries
97 * are mostly for error handling, hotplug and retarded devices.
99 static const unsigned long ata_eh_reset_timeouts[] = {
100 10000, /* most drives spin up by 10sec */
101 10000, /* > 99% working drives spin up before 20sec */
102 35000, /* give > 30 secs of idleness for retarded devices */
103 5000, /* and sweet one last chance */
104 ULONG_MAX, /* > 1 min has elapsed, give up */
107 static const unsigned long ata_eh_identify_timeouts[] = {
108 5000, /* covers > 99% of successes and not too boring on failures */
109 10000, /* combined time till here is enough even for media access */
110 30000, /* for true idiots */
114 static const unsigned long ata_eh_flush_timeouts[] = {
115 15000, /* be generous with flush */
117 30000, /* and even more generous */
121 static const unsigned long ata_eh_other_timeouts[] = {
122 5000, /* same rationale as identify timeout */
124 /* but no merciful 30sec for other commands, it just isn't worth it */
128 struct ata_eh_cmd_timeout_ent {
130 const unsigned long *timeouts;
133 /* The following table determines timeouts to use for EH internal
134 * commands. Each table entry is a command class and matches the
135 * commands the entry applies to and the timeout table to use.
137 * On the retry after a command timed out, the next timeout value from
138 * the table is used. If the table doesn't contain further entries,
139 * the last value is used.
141 * ehc->cmd_timeout_idx keeps track of which timeout to use per
142 * command class, so if SET_FEATURES times out on the first try, the
143 * next try will use the second timeout value only for that class.
145 #define CMDS(cmds...) (const u8 []){ cmds, 0 }
146 static const struct ata_eh_cmd_timeout_ent
147 ata_eh_cmd_timeout_table[ATA_EH_CMD_TIMEOUT_TABLE_SIZE] = {
148 { .commands = CMDS(ATA_CMD_ID_ATA, ATA_CMD_ID_ATAPI),
149 .timeouts = ata_eh_identify_timeouts, },
150 { .commands = CMDS(ATA_CMD_READ_NATIVE_MAX, ATA_CMD_READ_NATIVE_MAX_EXT),
151 .timeouts = ata_eh_other_timeouts, },
152 { .commands = CMDS(ATA_CMD_SET_MAX, ATA_CMD_SET_MAX_EXT),
153 .timeouts = ata_eh_other_timeouts, },
154 { .commands = CMDS(ATA_CMD_SET_FEATURES),
155 .timeouts = ata_eh_other_timeouts, },
156 { .commands = CMDS(ATA_CMD_INIT_DEV_PARAMS),
157 .timeouts = ata_eh_other_timeouts, },
158 { .commands = CMDS(ATA_CMD_FLUSH, ATA_CMD_FLUSH_EXT),
159 .timeouts = ata_eh_flush_timeouts },
163 static void __ata_port_freeze(struct ata_port *ap);
165 static void ata_eh_handle_port_suspend(struct ata_port *ap);
166 static void ata_eh_handle_port_resume(struct ata_port *ap);
167 #else /* CONFIG_PM */
168 static void ata_eh_handle_port_suspend(struct ata_port *ap)
171 static void ata_eh_handle_port_resume(struct ata_port *ap)
173 #endif /* CONFIG_PM */
175 static void __ata_ehi_pushv_desc(struct ata_eh_info *ehi, const char *fmt,
178 ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
179 ATA_EH_DESC_LEN - ehi->desc_len,
184 * __ata_ehi_push_desc - push error description without adding separator
186 * @fmt: printf format string
188 * Format string according to @fmt and append it to @ehi->desc.
191 * spin_lock_irqsave(host lock)
193 void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
198 __ata_ehi_pushv_desc(ehi, fmt, args);
203 * ata_ehi_push_desc - push error description with separator
205 * @fmt: printf format string
207 * Format string according to @fmt and append it to @ehi->desc.
208 * If @ehi->desc is not empty, ", " is added in-between.
211 * spin_lock_irqsave(host lock)
213 void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
218 __ata_ehi_push_desc(ehi, ", ");
221 __ata_ehi_pushv_desc(ehi, fmt, args);
226 * ata_ehi_clear_desc - clean error description
232 * spin_lock_irqsave(host lock)
234 void ata_ehi_clear_desc(struct ata_eh_info *ehi)
241 * ata_port_desc - append port description
242 * @ap: target ATA port
243 * @fmt: printf format string
245 * Format string according to @fmt and append it to port
246 * description. If port description is not empty, " " is added
247 * in-between. This function is to be used while initializing
248 * ata_host. The description is printed on host registration.
253 void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
257 WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
259 if (ap->link.eh_info.desc_len)
260 __ata_ehi_push_desc(&ap->link.eh_info, " ");
263 __ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
270 * ata_port_pbar_desc - append PCI BAR description
271 * @ap: target ATA port
272 * @bar: target PCI BAR
273 * @offset: offset into PCI BAR
274 * @name: name of the area
276 * If @offset is negative, this function formats a string which
277 * contains the name, address, size and type of the BAR and
278 * appends it to the port description. If @offset is zero or
279 * positive, only name and offsetted address is appended.
284 void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
287 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
289 unsigned long long start, len;
291 if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
293 else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
296 start = (unsigned long long)pci_resource_start(pdev, bar);
297 len = (unsigned long long)pci_resource_len(pdev, bar);
300 ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
302 ata_port_desc(ap, "%s 0x%llx", name,
303 start + (unsigned long long)offset);
306 #endif /* CONFIG_PCI */
308 static int ata_lookup_timeout_table(u8 cmd)
312 for (i = 0; i < ATA_EH_CMD_TIMEOUT_TABLE_SIZE; i++) {
315 for (cur = ata_eh_cmd_timeout_table[i].commands; *cur; cur++)
324 * ata_internal_cmd_timeout - determine timeout for an internal command
325 * @dev: target device
326 * @cmd: internal command to be issued
328 * Determine timeout for internal command @cmd for @dev.
334 * Determined timeout.
336 unsigned long ata_internal_cmd_timeout(struct ata_device *dev, u8 cmd)
338 struct ata_eh_context *ehc = &dev->link->eh_context;
339 int ent = ata_lookup_timeout_table(cmd);
343 return ATA_EH_CMD_DFL_TIMEOUT;
345 idx = ehc->cmd_timeout_idx[dev->devno][ent];
346 return ata_eh_cmd_timeout_table[ent].timeouts[idx];
350 * ata_internal_cmd_timed_out - notification for internal command timeout
351 * @dev: target device
352 * @cmd: internal command which timed out
354 * Notify EH that internal command @cmd for @dev timed out. This
355 * function should be called only for commands whose timeouts are
356 * determined using ata_internal_cmd_timeout().
361 void ata_internal_cmd_timed_out(struct ata_device *dev, u8 cmd)
363 struct ata_eh_context *ehc = &dev->link->eh_context;
364 int ent = ata_lookup_timeout_table(cmd);
370 idx = ehc->cmd_timeout_idx[dev->devno][ent];
371 if (ata_eh_cmd_timeout_table[ent].timeouts[idx + 1] != ULONG_MAX)
372 ehc->cmd_timeout_idx[dev->devno][ent]++;
375 static void ata_ering_record(struct ata_ering *ering, unsigned int eflags,
376 unsigned int err_mask)
378 struct ata_ering_entry *ent;
383 ering->cursor %= ATA_ERING_SIZE;
385 ent = &ering->ring[ering->cursor];
386 ent->eflags = eflags;
387 ent->err_mask = err_mask;
388 ent->timestamp = get_jiffies_64();
391 static struct ata_ering_entry *ata_ering_top(struct ata_ering *ering)
393 struct ata_ering_entry *ent = &ering->ring[ering->cursor];
400 int ata_ering_map(struct ata_ering *ering,
401 int (*map_fn)(struct ata_ering_entry *, void *),
405 struct ata_ering_entry *ent;
409 ent = &ering->ring[idx];
412 rc = map_fn(ent, arg);
415 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
416 } while (idx != ering->cursor);
421 int ata_ering_clear_cb(struct ata_ering_entry *ent, void *void_arg)
423 ent->eflags |= ATA_EFLAG_OLD_ER;
427 static void ata_ering_clear(struct ata_ering *ering)
429 ata_ering_map(ering, ata_ering_clear_cb, NULL);
432 static unsigned int ata_eh_dev_action(struct ata_device *dev)
434 struct ata_eh_context *ehc = &dev->link->eh_context;
436 return ehc->i.action | ehc->i.dev_action[dev->devno];
439 static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
440 struct ata_eh_info *ehi, unsigned int action)
442 struct ata_device *tdev;
445 ehi->action &= ~action;
446 ata_for_each_dev(tdev, link, ALL)
447 ehi->dev_action[tdev->devno] &= ~action;
449 /* doesn't make sense for port-wide EH actions */
450 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
452 /* break ehi->action into ehi->dev_action */
453 if (ehi->action & action) {
454 ata_for_each_dev(tdev, link, ALL)
455 ehi->dev_action[tdev->devno] |=
456 ehi->action & action;
457 ehi->action &= ~action;
460 /* turn off the specified per-dev action */
461 ehi->dev_action[dev->devno] &= ~action;
466 * ata_eh_acquire - acquire EH ownership
467 * @ap: ATA port to acquire EH ownership for
469 * Acquire EH ownership for @ap. This is the basic exclusion
470 * mechanism for ports sharing a host. Only one port hanging off
471 * the same host can claim the ownership of EH.
476 void ata_eh_acquire(struct ata_port *ap)
478 mutex_lock(&ap->host->eh_mutex);
479 WARN_ON_ONCE(ap->host->eh_owner);
480 ap->host->eh_owner = current;
484 * ata_eh_release - release EH ownership
485 * @ap: ATA port to release EH ownership for
487 * Release EH ownership for @ap if the caller. The caller must
488 * have acquired EH ownership using ata_eh_acquire() previously.
493 void ata_eh_release(struct ata_port *ap)
495 WARN_ON_ONCE(ap->host->eh_owner != current);
496 ap->host->eh_owner = NULL;
497 mutex_unlock(&ap->host->eh_mutex);
501 * ata_scsi_timed_out - SCSI layer time out callback
502 * @cmd: timed out SCSI command
504 * Handles SCSI layer timeout. We race with normal completion of
505 * the qc for @cmd. If the qc is already gone, we lose and let
506 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
507 * timed out and EH should be invoked. Prevent ata_qc_complete()
508 * from finishing it by setting EH_SCHEDULED and return
511 * TODO: kill this function once old EH is gone.
514 * Called from timer context
517 * EH_HANDLED or EH_NOT_HANDLED
519 enum blk_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
521 struct Scsi_Host *host = cmd->device->host;
522 struct ata_port *ap = ata_shost_to_port(host);
524 struct ata_queued_cmd *qc;
525 enum blk_eh_timer_return ret;
529 if (ap->ops->error_handler) {
530 ret = BLK_EH_NOT_HANDLED;
534 ret = BLK_EH_HANDLED;
535 spin_lock_irqsave(ap->lock, flags);
536 qc = ata_qc_from_tag(ap, ap->link.active_tag);
538 WARN_ON(qc->scsicmd != cmd);
539 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
540 qc->err_mask |= AC_ERR_TIMEOUT;
541 ret = BLK_EH_NOT_HANDLED;
543 spin_unlock_irqrestore(ap->lock, flags);
546 DPRINTK("EXIT, ret=%d\n", ret);
550 static void ata_eh_unload(struct ata_port *ap)
552 struct ata_link *link;
553 struct ata_device *dev;
556 /* Restore SControl IPM and SPD for the next driver and
557 * disable attached devices.
559 ata_for_each_link(link, ap, PMP_FIRST) {
560 sata_scr_write(link, SCR_CONTROL, link->saved_scontrol & 0xff0);
561 ata_for_each_dev(dev, link, ALL)
562 ata_dev_disable(dev);
565 /* freeze and set UNLOADED */
566 spin_lock_irqsave(ap->lock, flags);
568 ata_port_freeze(ap); /* won't be thawed */
569 ap->pflags &= ~ATA_PFLAG_EH_PENDING; /* clear pending from freeze */
570 ap->pflags |= ATA_PFLAG_UNLOADED;
572 spin_unlock_irqrestore(ap->lock, flags);
576 * ata_scsi_error - SCSI layer error handler callback
577 * @host: SCSI host on which error occurred
579 * Handles SCSI-layer-thrown error events.
582 * Inherited from SCSI layer (none, can sleep)
587 void ata_scsi_error(struct Scsi_Host *host)
589 struct ata_port *ap = ata_shost_to_port(host);
592 LIST_HEAD(eh_work_q);
596 spin_lock_irqsave(host->host_lock, flags);
597 list_splice_init(&host->eh_cmd_q, &eh_work_q);
598 spin_unlock_irqrestore(host->host_lock, flags);
600 /* make sure sff pio task is not running */
601 ata_sff_flush_pio_task(ap);
603 /* synchronize with host lock and sort out timeouts */
605 /* For new EH, all qcs are finished in one of three ways -
606 * normal completion, error completion, and SCSI timeout.
607 * Both completions can race against SCSI timeout. When normal
608 * completion wins, the qc never reaches EH. When error
609 * completion wins, the qc has ATA_QCFLAG_FAILED set.
611 * When SCSI timeout wins, things are a bit more complex.
612 * Normal or error completion can occur after the timeout but
613 * before this point. In such cases, both types of
614 * completions are honored. A scmd is determined to have
615 * timed out iff its associated qc is active and not failed.
617 if (ap->ops->error_handler) {
618 struct scsi_cmnd *scmd, *tmp;
621 spin_lock_irqsave(ap->lock, flags);
623 /* This must occur under the ap->lock as we don't want
624 a polled recovery to race the real interrupt handler
626 The lost_interrupt handler checks for any completed but
627 non-notified command and completes much like an IRQ handler.
629 We then fall into the error recovery code which will treat
630 this as if normal completion won the race */
632 if (ap->ops->lost_interrupt)
633 ap->ops->lost_interrupt(ap);
635 list_for_each_entry_safe(scmd, tmp, &eh_work_q, eh_entry) {
636 struct ata_queued_cmd *qc;
638 for (i = 0; i < ATA_MAX_QUEUE; i++) {
639 qc = __ata_qc_from_tag(ap, i);
640 if (qc->flags & ATA_QCFLAG_ACTIVE &&
645 if (i < ATA_MAX_QUEUE) {
646 /* the scmd has an associated qc */
647 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
648 /* which hasn't failed yet, timeout */
649 qc->err_mask |= AC_ERR_TIMEOUT;
650 qc->flags |= ATA_QCFLAG_FAILED;
654 /* Normal completion occurred after
655 * SCSI timeout but before this point.
656 * Successfully complete it.
658 scmd->retries = scmd->allowed;
659 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
663 /* If we have timed out qcs. They belong to EH from
664 * this point but the state of the controller is
665 * unknown. Freeze the port to make sure the IRQ
666 * handler doesn't diddle with those qcs. This must
667 * be done atomically w.r.t. setting QCFLAG_FAILED.
670 __ata_port_freeze(ap);
672 spin_unlock_irqrestore(ap->lock, flags);
674 /* initialize eh_tries */
675 ap->eh_tries = ATA_EH_MAX_TRIES;
677 spin_unlock_wait(ap->lock);
679 /* If we timed raced normal completion and there is nothing to
680 recover nr_timedout == 0 why exactly are we doing error recovery ? */
682 /* invoke error handler */
683 if (ap->ops->error_handler) {
684 struct ata_link *link;
686 /* acquire EH ownership */
689 /* kill fast drain timer */
690 del_timer_sync(&ap->fastdrain_timer);
692 /* process port resume request */
693 ata_eh_handle_port_resume(ap);
695 /* fetch & clear EH info */
696 spin_lock_irqsave(ap->lock, flags);
698 ata_for_each_link(link, ap, HOST_FIRST) {
699 struct ata_eh_context *ehc = &link->eh_context;
700 struct ata_device *dev;
702 memset(&link->eh_context, 0, sizeof(link->eh_context));
703 link->eh_context.i = link->eh_info;
704 memset(&link->eh_info, 0, sizeof(link->eh_info));
706 ata_for_each_dev(dev, link, ENABLED) {
707 int devno = dev->devno;
709 ehc->saved_xfer_mode[devno] = dev->xfer_mode;
710 if (ata_ncq_enabled(dev))
711 ehc->saved_ncq_enabled |= 1 << devno;
715 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
716 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
717 ap->excl_link = NULL; /* don't maintain exclusion over EH */
719 spin_unlock_irqrestore(ap->lock, flags);
721 /* invoke EH, skip if unloading or suspended */
722 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
723 ap->ops->error_handler(ap);
725 /* if unloading, commence suicide */
726 if ((ap->pflags & ATA_PFLAG_UNLOADING) &&
727 !(ap->pflags & ATA_PFLAG_UNLOADED))
732 /* process port suspend request */
733 ata_eh_handle_port_suspend(ap);
735 /* Exception might have happend after ->error_handler
736 * recovered the port but before this point. Repeat
739 spin_lock_irqsave(ap->lock, flags);
741 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
742 if (--ap->eh_tries) {
743 spin_unlock_irqrestore(ap->lock, flags);
746 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
747 "tries, giving up\n", ATA_EH_MAX_TRIES);
748 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
751 /* this run is complete, make sure EH info is clear */
752 ata_for_each_link(link, ap, HOST_FIRST)
753 memset(&link->eh_info, 0, sizeof(link->eh_info));
755 /* Clear host_eh_scheduled while holding ap->lock such
756 * that if exception occurs after this point but
757 * before EH completion, SCSI midlayer will
760 host->host_eh_scheduled = 0;
762 spin_unlock_irqrestore(ap->lock, flags);
765 WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
766 ap->ops->eng_timeout(ap);
769 /* finish or retry handled scmd's and clean up */
770 WARN_ON(host->host_failed || !list_empty(&eh_work_q));
772 scsi_eh_flush_done_q(&ap->eh_done_q);
775 spin_lock_irqsave(ap->lock, flags);
777 if (ap->pflags & ATA_PFLAG_LOADING)
778 ap->pflags &= ~ATA_PFLAG_LOADING;
779 else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
780 schedule_delayed_work(&ap->hotplug_task, 0);
782 if (ap->pflags & ATA_PFLAG_RECOVERED)
783 ata_port_printk(ap, KERN_INFO, "EH complete\n");
785 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
787 /* tell wait_eh that we're done */
788 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
789 wake_up_all(&ap->eh_wait_q);
791 spin_unlock_irqrestore(ap->lock, flags);
797 * ata_port_wait_eh - Wait for the currently pending EH to complete
798 * @ap: Port to wait EH for
800 * Wait until the currently pending EH is complete.
803 * Kernel thread context (may sleep).
805 void ata_port_wait_eh(struct ata_port *ap)
811 spin_lock_irqsave(ap->lock, flags);
813 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
814 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
815 spin_unlock_irqrestore(ap->lock, flags);
817 spin_lock_irqsave(ap->lock, flags);
819 finish_wait(&ap->eh_wait_q, &wait);
821 spin_unlock_irqrestore(ap->lock, flags);
823 /* make sure SCSI EH is complete */
824 if (scsi_host_in_recovery(ap->scsi_host)) {
830 static int ata_eh_nr_in_flight(struct ata_port *ap)
835 /* count only non-internal commands */
836 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++)
837 if (ata_qc_from_tag(ap, tag))
843 void ata_eh_fastdrain_timerfn(unsigned long arg)
845 struct ata_port *ap = (void *)arg;
849 spin_lock_irqsave(ap->lock, flags);
851 cnt = ata_eh_nr_in_flight(ap);
857 if (cnt == ap->fastdrain_cnt) {
860 /* No progress during the last interval, tag all
861 * in-flight qcs as timed out and freeze the port.
863 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) {
864 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
866 qc->err_mask |= AC_ERR_TIMEOUT;
871 /* some qcs have finished, give it another chance */
872 ap->fastdrain_cnt = cnt;
873 ap->fastdrain_timer.expires =
874 ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
875 add_timer(&ap->fastdrain_timer);
879 spin_unlock_irqrestore(ap->lock, flags);
883 * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
884 * @ap: target ATA port
885 * @fastdrain: activate fast drain
887 * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
888 * is non-zero and EH wasn't pending before. Fast drain ensures
889 * that EH kicks in in timely manner.
892 * spin_lock_irqsave(host lock)
894 static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
898 /* already scheduled? */
899 if (ap->pflags & ATA_PFLAG_EH_PENDING)
902 ap->pflags |= ATA_PFLAG_EH_PENDING;
907 /* do we have in-flight qcs? */
908 cnt = ata_eh_nr_in_flight(ap);
912 /* activate fast drain */
913 ap->fastdrain_cnt = cnt;
914 ap->fastdrain_timer.expires =
915 ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
916 add_timer(&ap->fastdrain_timer);
920 * ata_qc_schedule_eh - schedule qc for error handling
921 * @qc: command to schedule error handling for
923 * Schedule error handling for @qc. EH will kick in as soon as
924 * other commands are drained.
927 * spin_lock_irqsave(host lock)
929 void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
931 struct ata_port *ap = qc->ap;
932 struct request_queue *q = qc->scsicmd->device->request_queue;
935 WARN_ON(!ap->ops->error_handler);
937 qc->flags |= ATA_QCFLAG_FAILED;
938 ata_eh_set_pending(ap, 1);
940 /* The following will fail if timeout has already expired.
941 * ata_scsi_error() takes care of such scmds on EH entry.
942 * Note that ATA_QCFLAG_FAILED is unconditionally set after
943 * this function completes.
945 spin_lock_irqsave(q->queue_lock, flags);
946 blk_abort_request(qc->scsicmd->request);
947 spin_unlock_irqrestore(q->queue_lock, flags);
951 * ata_port_schedule_eh - schedule error handling without a qc
952 * @ap: ATA port to schedule EH for
954 * Schedule error handling for @ap. EH will kick in as soon as
955 * all commands are drained.
958 * spin_lock_irqsave(host lock)
960 void ata_port_schedule_eh(struct ata_port *ap)
962 WARN_ON(!ap->ops->error_handler);
964 if (ap->pflags & ATA_PFLAG_INITIALIZING)
967 ata_eh_set_pending(ap, 1);
968 scsi_schedule_eh(ap->scsi_host);
970 DPRINTK("port EH scheduled\n");
973 static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
975 int tag, nr_aborted = 0;
977 WARN_ON(!ap->ops->error_handler);
979 /* we're gonna abort all commands, no need for fast drain */
980 ata_eh_set_pending(ap, 0);
982 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
983 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, 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->retries is decremented 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;
1258 if (!qc->err_mask && scmd->retries)
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_printk(dev, KERN_WARNING, "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 * @ap: target ATA port
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 return "unknown error";
1415 * ata_read_log_page - read a specific log page
1416 * @dev: target device
1417 * @page: page to read
1418 * @buf: buffer to store read page
1419 * @sectors: number of sectors to read
1421 * Read log page using READ_LOG_EXT command.
1424 * Kernel thread context (may sleep).
1427 * 0 on success, AC_ERR_* mask otherwise.
1429 static unsigned int ata_read_log_page(struct ata_device *dev,
1430 u8 page, void *buf, unsigned int sectors)
1432 struct ata_taskfile tf;
1433 unsigned int err_mask;
1435 DPRINTK("read log page - page %d\n", page);
1437 ata_tf_init(dev, &tf);
1438 tf.command = ATA_CMD_READ_LOG_EXT;
1441 tf.hob_nsect = sectors >> 8;
1442 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1443 tf.protocol = ATA_PROT_PIO;
1445 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
1446 buf, sectors * ATA_SECT_SIZE, 0);
1448 DPRINTK("EXIT, err_mask=%x\n", err_mask);
1453 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
1454 * @dev: Device to read log page 10h from
1455 * @tag: Resulting tag of the failed command
1456 * @tf: Resulting taskfile registers of the failed command
1458 * Read log page 10h to obtain NCQ error details and clear error
1462 * Kernel thread context (may sleep).
1465 * 0 on success, -errno otherwise.
1467 static int ata_eh_read_log_10h(struct ata_device *dev,
1468 int *tag, struct ata_taskfile *tf)
1470 u8 *buf = dev->link->ap->sector_buf;
1471 unsigned int err_mask;
1475 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
1480 for (i = 0; i < ATA_SECT_SIZE; i++)
1483 ata_dev_printk(dev, KERN_WARNING,
1484 "invalid checksum 0x%x on log page 10h\n", csum);
1489 *tag = buf[0] & 0x1f;
1491 tf->command = buf[2];
1492 tf->feature = buf[3];
1496 tf->device = buf[7];
1497 tf->hob_lbal = buf[8];
1498 tf->hob_lbam = buf[9];
1499 tf->hob_lbah = buf[10];
1500 tf->nsect = buf[12];
1501 tf->hob_nsect = buf[13];
1507 * atapi_eh_tur - perform ATAPI TEST_UNIT_READY
1508 * @dev: target ATAPI device
1509 * @r_sense_key: out parameter for sense_key
1511 * Perform ATAPI TEST_UNIT_READY.
1514 * EH context (may sleep).
1517 * 0 on success, AC_ERR_* mask on failure.
1519 static unsigned int atapi_eh_tur(struct ata_device *dev, u8 *r_sense_key)
1521 u8 cdb[ATAPI_CDB_LEN] = { TEST_UNIT_READY, 0, 0, 0, 0, 0 };
1522 struct ata_taskfile tf;
1523 unsigned int err_mask;
1525 ata_tf_init(dev, &tf);
1527 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1528 tf.command = ATA_CMD_PACKET;
1529 tf.protocol = ATAPI_PROT_NODATA;
1531 err_mask = ata_exec_internal(dev, &tf, cdb, DMA_NONE, NULL, 0, 0);
1532 if (err_mask == AC_ERR_DEV)
1533 *r_sense_key = tf.feature >> 4;
1538 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1539 * @dev: device to perform REQUEST_SENSE to
1540 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
1541 * @dfl_sense_key: default sense key to use
1543 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
1544 * SENSE. This function is EH helper.
1547 * Kernel thread context (may sleep).
1550 * 0 on success, AC_ERR_* mask on failure
1552 static unsigned int atapi_eh_request_sense(struct ata_device *dev,
1553 u8 *sense_buf, u8 dfl_sense_key)
1555 u8 cdb[ATAPI_CDB_LEN] =
1556 { REQUEST_SENSE, 0, 0, 0, SCSI_SENSE_BUFFERSIZE, 0 };
1557 struct ata_port *ap = dev->link->ap;
1558 struct ata_taskfile tf;
1560 DPRINTK("ATAPI request sense\n");
1562 /* FIXME: is this needed? */
1563 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1565 /* initialize sense_buf with the error register,
1566 * for the case where they are -not- overwritten
1568 sense_buf[0] = 0x70;
1569 sense_buf[2] = dfl_sense_key;
1571 /* some devices time out if garbage left in tf */
1572 ata_tf_init(dev, &tf);
1574 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1575 tf.command = ATA_CMD_PACKET;
1577 /* is it pointless to prefer PIO for "safety reasons"? */
1578 if (ap->flags & ATA_FLAG_PIO_DMA) {
1579 tf.protocol = ATAPI_PROT_DMA;
1580 tf.feature |= ATAPI_PKT_DMA;
1582 tf.protocol = ATAPI_PROT_PIO;
1583 tf.lbam = SCSI_SENSE_BUFFERSIZE;
1587 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
1588 sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
1592 * ata_eh_analyze_serror - analyze SError for a failed port
1593 * @link: ATA link to analyze SError for
1595 * Analyze SError if available and further determine cause of
1601 static void ata_eh_analyze_serror(struct ata_link *link)
1603 struct ata_eh_context *ehc = &link->eh_context;
1604 u32 serror = ehc->i.serror;
1605 unsigned int err_mask = 0, action = 0;
1608 if (serror & (SERR_PERSISTENT | SERR_DATA)) {
1609 err_mask |= AC_ERR_ATA_BUS;
1610 action |= ATA_EH_RESET;
1612 if (serror & SERR_PROTOCOL) {
1613 err_mask |= AC_ERR_HSM;
1614 action |= ATA_EH_RESET;
1616 if (serror & SERR_INTERNAL) {
1617 err_mask |= AC_ERR_SYSTEM;
1618 action |= ATA_EH_RESET;
1621 /* Determine whether a hotplug event has occurred. Both
1622 * SError.N/X are considered hotplug events for enabled or
1623 * host links. For disabled PMP links, only N bit is
1624 * considered as X bit is left at 1 for link plugging.
1626 if (link->lpm_policy != ATA_LPM_MAX_POWER)
1627 hotplug_mask = 0; /* hotplug doesn't work w/ LPM */
1628 else if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
1629 hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
1631 hotplug_mask = SERR_PHYRDY_CHG;
1633 if (serror & hotplug_mask)
1634 ata_ehi_hotplugged(&ehc->i);
1636 ehc->i.err_mask |= err_mask;
1637 ehc->i.action |= action;
1641 * ata_eh_analyze_ncq_error - analyze NCQ error
1642 * @link: ATA link to analyze NCQ error for
1644 * Read log page 10h, determine the offending qc and acquire
1645 * error status TF. For NCQ device errors, all LLDDs have to do
1646 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1650 * Kernel thread context (may sleep).
1652 void ata_eh_analyze_ncq_error(struct ata_link *link)
1654 struct ata_port *ap = link->ap;
1655 struct ata_eh_context *ehc = &link->eh_context;
1656 struct ata_device *dev = link->device;
1657 struct ata_queued_cmd *qc;
1658 struct ata_taskfile tf;
1661 /* if frozen, we can't do much */
1662 if (ap->pflags & ATA_PFLAG_FROZEN)
1665 /* is it NCQ device error? */
1666 if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1669 /* has LLDD analyzed already? */
1670 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1671 qc = __ata_qc_from_tag(ap, tag);
1673 if (!(qc->flags & ATA_QCFLAG_FAILED))
1680 /* okay, this error is ours */
1681 memset(&tf, 0, sizeof(tf));
1682 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1684 ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
1685 "(errno=%d)\n", rc);
1689 if (!(link->sactive & (1 << tag))) {
1690 ata_link_printk(link, KERN_ERR, "log page 10h reported "
1691 "inactive tag %d\n", tag);
1695 /* we've got the perpetrator, condemn it */
1696 qc = __ata_qc_from_tag(ap, tag);
1697 memcpy(&qc->result_tf, &tf, sizeof(tf));
1698 qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
1699 qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
1700 ehc->i.err_mask &= ~AC_ERR_DEV;
1704 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1705 * @qc: qc to analyze
1706 * @tf: Taskfile registers to analyze
1708 * Analyze taskfile of @qc and further determine cause of
1709 * failure. This function also requests ATAPI sense data if
1713 * Kernel thread context (may sleep).
1716 * Determined recovery action
1718 static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1719 const struct ata_taskfile *tf)
1721 unsigned int tmp, action = 0;
1722 u8 stat = tf->command, err = tf->feature;
1724 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1725 qc->err_mask |= AC_ERR_HSM;
1726 return ATA_EH_RESET;
1729 if (stat & (ATA_ERR | ATA_DF))
1730 qc->err_mask |= AC_ERR_DEV;
1734 switch (qc->dev->class) {
1737 qc->err_mask |= AC_ERR_ATA_BUS;
1739 qc->err_mask |= AC_ERR_MEDIA;
1741 qc->err_mask |= AC_ERR_INVALID;
1745 if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
1746 tmp = atapi_eh_request_sense(qc->dev,
1747 qc->scsicmd->sense_buffer,
1748 qc->result_tf.feature >> 4);
1750 /* ATA_QCFLAG_SENSE_VALID is used to
1751 * tell atapi_qc_complete() that sense
1752 * data is already valid.
1754 * TODO: interpret sense data and set
1755 * appropriate err_mask.
1757 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1759 qc->err_mask |= tmp;
1763 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1764 action |= ATA_EH_RESET;
1769 static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask,
1774 if (!(eflags & ATA_EFLAG_DUBIOUS_XFER))
1778 base = ATA_ECAT_DUBIOUS_NONE;
1780 if (err_mask & AC_ERR_ATA_BUS)
1781 return base + ATA_ECAT_ATA_BUS;
1783 if (err_mask & AC_ERR_TIMEOUT)
1784 return base + ATA_ECAT_TOUT_HSM;
1786 if (eflags & ATA_EFLAG_IS_IO) {
1787 if (err_mask & AC_ERR_HSM)
1788 return base + ATA_ECAT_TOUT_HSM;
1790 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1791 return base + ATA_ECAT_UNK_DEV;
1797 struct speed_down_verdict_arg {
1800 int nr_errors[ATA_ECAT_NR];
1803 static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
1805 struct speed_down_verdict_arg *arg = void_arg;
1808 if ((ent->eflags & ATA_EFLAG_OLD_ER) || (ent->timestamp < arg->since))
1811 cat = ata_eh_categorize_error(ent->eflags, ent->err_mask,
1813 arg->nr_errors[cat]++;
1819 * ata_eh_speed_down_verdict - Determine speed down verdict
1820 * @dev: Device of interest
1822 * This function examines error ring of @dev and determines
1823 * whether NCQ needs to be turned off, transfer speed should be
1824 * stepped down, or falling back to PIO is necessary.
1826 * ECAT_ATA_BUS : ATA_BUS error for any command
1828 * ECAT_TOUT_HSM : TIMEOUT for any command or HSM violation for
1831 * ECAT_UNK_DEV : Unknown DEV error for IO commands
1833 * ECAT_DUBIOUS_* : Identical to above three but occurred while
1834 * data transfer hasn't been verified.
1838 * NCQ_OFF : Turn off NCQ.
1840 * SPEED_DOWN : Speed down transfer speed but don't fall back
1843 * FALLBACK_TO_PIO : Fall back to PIO.
1845 * Even if multiple verdicts are returned, only one action is
1846 * taken per error. An action triggered by non-DUBIOUS errors
1847 * clears ering, while one triggered by DUBIOUS_* errors doesn't.
1848 * This is to expedite speed down decisions right after device is
1849 * initially configured.
1851 * The followings are speed down rules. #1 and #2 deal with
1854 * 1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors
1855 * occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO.
1857 * 2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors
1858 * occurred during last 5 mins, NCQ_OFF.
1860 * 3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors
1861 * ocurred during last 5 mins, FALLBACK_TO_PIO
1863 * 4. If more than 3 TOUT_HSM or UNK_DEV errors occurred
1864 * during last 10 mins, NCQ_OFF.
1866 * 5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6
1867 * UNK_DEV errors occurred during last 10 mins, SPEED_DOWN.
1870 * Inherited from caller.
1873 * OR of ATA_EH_SPDN_* flags.
1875 static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
1877 const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
1878 u64 j64 = get_jiffies_64();
1879 struct speed_down_verdict_arg arg;
1880 unsigned int verdict = 0;
1882 /* scan past 5 mins of error history */
1883 memset(&arg, 0, sizeof(arg));
1884 arg.since = j64 - min(j64, j5mins);
1885 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1887 if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] +
1888 arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1)
1889 verdict |= ATA_EH_SPDN_SPEED_DOWN |
1890 ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS;
1892 if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] +
1893 arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1)
1894 verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS;
1896 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1897 arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1898 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
1899 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
1901 /* scan past 10 mins of error history */
1902 memset(&arg, 0, sizeof(arg));
1903 arg.since = j64 - min(j64, j10mins);
1904 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1906 if (arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1907 arg.nr_errors[ATA_ECAT_UNK_DEV] > 3)
1908 verdict |= ATA_EH_SPDN_NCQ_OFF;
1910 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1911 arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 ||
1912 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
1913 verdict |= ATA_EH_SPDN_SPEED_DOWN;
1919 * ata_eh_speed_down - record error and speed down if necessary
1920 * @dev: Failed device
1921 * @eflags: mask of ATA_EFLAG_* flags
1922 * @err_mask: err_mask of the error
1924 * Record error and examine error history to determine whether
1925 * adjusting transmission speed is necessary. It also sets
1926 * transmission limits appropriately if such adjustment is
1930 * Kernel thread context (may sleep).
1933 * Determined recovery action.
1935 static unsigned int ata_eh_speed_down(struct ata_device *dev,
1936 unsigned int eflags, unsigned int err_mask)
1938 struct ata_link *link = ata_dev_phys_link(dev);
1940 unsigned int verdict;
1941 unsigned int action = 0;
1943 /* don't bother if Cat-0 error */
1944 if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0)
1947 /* record error and determine whether speed down is necessary */
1948 ata_ering_record(&dev->ering, eflags, err_mask);
1949 verdict = ata_eh_speed_down_verdict(dev);
1952 if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
1953 (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
1954 ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
1955 dev->flags |= ATA_DFLAG_NCQ_OFF;
1956 ata_dev_printk(dev, KERN_WARNING,
1957 "NCQ disabled due to excessive errors\n");
1962 if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1963 /* speed down SATA link speed if possible */
1964 if (sata_down_spd_limit(link, 0) == 0) {
1965 action |= ATA_EH_RESET;
1969 /* lower transfer mode */
1970 if (dev->spdn_cnt < 2) {
1971 static const int dma_dnxfer_sel[] =
1972 { ATA_DNXFER_DMA, ATA_DNXFER_40C };
1973 static const int pio_dnxfer_sel[] =
1974 { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
1977 if (dev->xfer_shift != ATA_SHIFT_PIO)
1978 sel = dma_dnxfer_sel[dev->spdn_cnt];
1980 sel = pio_dnxfer_sel[dev->spdn_cnt];
1984 if (ata_down_xfermask_limit(dev, sel) == 0) {
1985 action |= ATA_EH_RESET;
1991 /* Fall back to PIO? Slowing down to PIO is meaningless for
1992 * SATA ATA devices. Consider it only for PATA and SATAPI.
1994 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
1995 (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) &&
1996 (dev->xfer_shift != ATA_SHIFT_PIO)) {
1997 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
1999 action |= ATA_EH_RESET;
2006 /* device has been slowed down, blow error history */
2007 if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS))
2008 ata_ering_clear(&dev->ering);
2013 * ata_eh_link_autopsy - analyze error and determine recovery action
2014 * @link: host link to perform autopsy on
2016 * Analyze why @link failed and determine which recovery actions
2017 * are needed. This function also sets more detailed AC_ERR_*
2018 * values and fills sense data for ATAPI CHECK SENSE.
2021 * Kernel thread context (may sleep).
2023 static void ata_eh_link_autopsy(struct ata_link *link)
2025 struct ata_port *ap = link->ap;
2026 struct ata_eh_context *ehc = &link->eh_context;
2027 struct ata_device *dev;
2028 unsigned int all_err_mask = 0, eflags = 0;
2035 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
2038 /* obtain and analyze SError */
2039 rc = sata_scr_read(link, SCR_ERROR, &serror);
2041 ehc->i.serror |= serror;
2042 ata_eh_analyze_serror(link);
2043 } else if (rc != -EOPNOTSUPP) {
2044 /* SError read failed, force reset and probing */
2045 ehc->i.probe_mask |= ATA_ALL_DEVICES;
2046 ehc->i.action |= ATA_EH_RESET;
2047 ehc->i.err_mask |= AC_ERR_OTHER;
2050 /* analyze NCQ failure */
2051 ata_eh_analyze_ncq_error(link);
2053 /* any real error trumps AC_ERR_OTHER */
2054 if (ehc->i.err_mask & ~AC_ERR_OTHER)
2055 ehc->i.err_mask &= ~AC_ERR_OTHER;
2057 all_err_mask |= ehc->i.err_mask;
2059 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2060 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2062 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
2063 ata_dev_phys_link(qc->dev) != link)
2066 /* inherit upper level err_mask */
2067 qc->err_mask |= ehc->i.err_mask;
2070 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
2072 /* DEV errors are probably spurious in case of ATA_BUS error */
2073 if (qc->err_mask & AC_ERR_ATA_BUS)
2074 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
2077 /* any real error trumps unknown error */
2078 if (qc->err_mask & ~AC_ERR_OTHER)
2079 qc->err_mask &= ~AC_ERR_OTHER;
2081 /* SENSE_VALID trumps dev/unknown error and revalidation */
2082 if (qc->flags & ATA_QCFLAG_SENSE_VALID)
2083 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
2085 /* determine whether the command is worth retrying */
2086 if (qc->flags & ATA_QCFLAG_IO ||
2087 (!(qc->err_mask & AC_ERR_INVALID) &&
2088 qc->err_mask != AC_ERR_DEV))
2089 qc->flags |= ATA_QCFLAG_RETRY;
2091 /* accumulate error info */
2092 ehc->i.dev = qc->dev;
2093 all_err_mask |= qc->err_mask;
2094 if (qc->flags & ATA_QCFLAG_IO)
2095 eflags |= ATA_EFLAG_IS_IO;
2098 /* enforce default EH actions */
2099 if (ap->pflags & ATA_PFLAG_FROZEN ||
2100 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
2101 ehc->i.action |= ATA_EH_RESET;
2102 else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) ||
2103 (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV)))
2104 ehc->i.action |= ATA_EH_REVALIDATE;
2106 /* If we have offending qcs and the associated failed device,
2107 * perform per-dev EH action only on the offending device.
2110 ehc->i.dev_action[ehc->i.dev->devno] |=
2111 ehc->i.action & ATA_EH_PERDEV_MASK;
2112 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
2115 /* propagate timeout to host link */
2116 if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link))
2117 ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT;
2119 /* record error and consider speeding down */
2121 if (!dev && ((ata_link_max_devices(link) == 1 &&
2122 ata_dev_enabled(link->device))))
2126 if (dev->flags & ATA_DFLAG_DUBIOUS_XFER)
2127 eflags |= ATA_EFLAG_DUBIOUS_XFER;
2128 ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask);
2135 * ata_eh_autopsy - analyze error and determine recovery action
2136 * @ap: host port to perform autopsy on
2138 * Analyze all links of @ap and determine why they failed and
2139 * which recovery actions are needed.
2142 * Kernel thread context (may sleep).
2144 void ata_eh_autopsy(struct ata_port *ap)
2146 struct ata_link *link;
2148 ata_for_each_link(link, ap, EDGE)
2149 ata_eh_link_autopsy(link);
2151 /* Handle the frigging slave link. Autopsy is done similarly
2152 * but actions and flags are transferred over to the master
2153 * link and handled from there.
2155 if (ap->slave_link) {
2156 struct ata_eh_context *mehc = &ap->link.eh_context;
2157 struct ata_eh_context *sehc = &ap->slave_link->eh_context;
2159 /* transfer control flags from master to slave */
2160 sehc->i.flags |= mehc->i.flags & ATA_EHI_TO_SLAVE_MASK;
2162 /* perform autopsy on the slave link */
2163 ata_eh_link_autopsy(ap->slave_link);
2165 /* transfer actions from slave to master and clear slave */
2166 ata_eh_about_to_do(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
2167 mehc->i.action |= sehc->i.action;
2168 mehc->i.dev_action[1] |= sehc->i.dev_action[1];
2169 mehc->i.flags |= sehc->i.flags;
2170 ata_eh_done(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
2173 /* Autopsy of fanout ports can affect host link autopsy.
2174 * Perform host link autopsy last.
2176 if (sata_pmp_attached(ap))
2177 ata_eh_link_autopsy(&ap->link);
2181 * ata_get_cmd_descript - get description for ATA command
2182 * @command: ATA command code to get description for
2184 * Return a textual description of the given command, or NULL if the
2185 * command is not known.
2190 const char *ata_get_cmd_descript(u8 command)
2192 #ifdef CONFIG_ATA_VERBOSE_ERROR
2198 { ATA_CMD_DEV_RESET, "DEVICE RESET" },
2199 { ATA_CMD_CHK_POWER, "CHECK POWER MODE" },
2200 { ATA_CMD_STANDBY, "STANDBY" },
2201 { ATA_CMD_IDLE, "IDLE" },
2202 { ATA_CMD_EDD, "EXECUTE DEVICE DIAGNOSTIC" },
2203 { ATA_CMD_DOWNLOAD_MICRO, "DOWNLOAD MICROCODE" },
2204 { ATA_CMD_NOP, "NOP" },
2205 { ATA_CMD_FLUSH, "FLUSH CACHE" },
2206 { ATA_CMD_FLUSH_EXT, "FLUSH CACHE EXT" },
2207 { ATA_CMD_ID_ATA, "IDENTIFY DEVICE" },
2208 { ATA_CMD_ID_ATAPI, "IDENTIFY PACKET DEVICE" },
2209 { ATA_CMD_SERVICE, "SERVICE" },
2210 { ATA_CMD_READ, "READ DMA" },
2211 { ATA_CMD_READ_EXT, "READ DMA EXT" },
2212 { ATA_CMD_READ_QUEUED, "READ DMA QUEUED" },
2213 { ATA_CMD_READ_STREAM_EXT, "READ STREAM EXT" },
2214 { ATA_CMD_READ_STREAM_DMA_EXT, "READ STREAM DMA EXT" },
2215 { ATA_CMD_WRITE, "WRITE DMA" },
2216 { ATA_CMD_WRITE_EXT, "WRITE DMA EXT" },
2217 { ATA_CMD_WRITE_QUEUED, "WRITE DMA QUEUED EXT" },
2218 { ATA_CMD_WRITE_STREAM_EXT, "WRITE STREAM EXT" },
2219 { ATA_CMD_WRITE_STREAM_DMA_EXT, "WRITE STREAM DMA EXT" },
2220 { ATA_CMD_WRITE_FUA_EXT, "WRITE DMA FUA EXT" },
2221 { ATA_CMD_WRITE_QUEUED_FUA_EXT, "WRITE DMA QUEUED FUA EXT" },
2222 { ATA_CMD_FPDMA_READ, "READ FPDMA QUEUED" },
2223 { ATA_CMD_FPDMA_WRITE, "WRITE FPDMA QUEUED" },
2224 { ATA_CMD_PIO_READ, "READ SECTOR(S)" },
2225 { ATA_CMD_PIO_READ_EXT, "READ SECTOR(S) EXT" },
2226 { ATA_CMD_PIO_WRITE, "WRITE SECTOR(S)" },
2227 { ATA_CMD_PIO_WRITE_EXT, "WRITE SECTOR(S) EXT" },
2228 { ATA_CMD_READ_MULTI, "READ MULTIPLE" },
2229 { ATA_CMD_READ_MULTI_EXT, "READ MULTIPLE EXT" },
2230 { ATA_CMD_WRITE_MULTI, "WRITE MULTIPLE" },
2231 { ATA_CMD_WRITE_MULTI_EXT, "WRITE MULTIPLE EXT" },
2232 { ATA_CMD_WRITE_MULTI_FUA_EXT, "WRITE MULTIPLE FUA EXT" },
2233 { ATA_CMD_SET_FEATURES, "SET FEATURES" },
2234 { ATA_CMD_SET_MULTI, "SET MULTIPLE MODE" },
2235 { ATA_CMD_VERIFY, "READ VERIFY SECTOR(S)" },
2236 { ATA_CMD_VERIFY_EXT, "READ VERIFY SECTOR(S) EXT" },
2237 { ATA_CMD_WRITE_UNCORR_EXT, "WRITE UNCORRECTABLE EXT" },
2238 { ATA_CMD_STANDBYNOW1, "STANDBY IMMEDIATE" },
2239 { ATA_CMD_IDLEIMMEDIATE, "IDLE IMMEDIATE" },
2240 { ATA_CMD_SLEEP, "SLEEP" },
2241 { ATA_CMD_INIT_DEV_PARAMS, "INITIALIZE DEVICE PARAMETERS" },
2242 { ATA_CMD_READ_NATIVE_MAX, "READ NATIVE MAX ADDRESS" },
2243 { ATA_CMD_READ_NATIVE_MAX_EXT, "READ NATIVE MAX ADDRESS EXT" },
2244 { ATA_CMD_SET_MAX, "SET MAX ADDRESS" },
2245 { ATA_CMD_SET_MAX_EXT, "SET MAX ADDRESS EXT" },
2246 { ATA_CMD_READ_LOG_EXT, "READ LOG EXT" },
2247 { ATA_CMD_WRITE_LOG_EXT, "WRITE LOG EXT" },
2248 { ATA_CMD_READ_LOG_DMA_EXT, "READ LOG DMA EXT" },
2249 { ATA_CMD_WRITE_LOG_DMA_EXT, "WRITE LOG DMA EXT" },
2250 { ATA_CMD_TRUSTED_RCV, "TRUSTED RECEIVE" },
2251 { ATA_CMD_TRUSTED_RCV_DMA, "TRUSTED RECEIVE DMA" },
2252 { ATA_CMD_TRUSTED_SND, "TRUSTED SEND" },
2253 { ATA_CMD_TRUSTED_SND_DMA, "TRUSTED SEND DMA" },
2254 { ATA_CMD_PMP_READ, "READ BUFFER" },
2255 { ATA_CMD_PMP_WRITE, "WRITE BUFFER" },
2256 { ATA_CMD_CONF_OVERLAY, "DEVICE CONFIGURATION OVERLAY" },
2257 { ATA_CMD_SEC_SET_PASS, "SECURITY SET PASSWORD" },
2258 { ATA_CMD_SEC_UNLOCK, "SECURITY UNLOCK" },
2259 { ATA_CMD_SEC_ERASE_PREP, "SECURITY ERASE PREPARE" },
2260 { ATA_CMD_SEC_ERASE_UNIT, "SECURITY ERASE UNIT" },
2261 { ATA_CMD_SEC_FREEZE_LOCK, "SECURITY FREEZE LOCK" },
2262 { ATA_CMD_SEC_DISABLE_PASS, "SECURITY DISABLE PASSWORD" },
2263 { ATA_CMD_CONFIG_STREAM, "CONFIGURE STREAM" },
2264 { ATA_CMD_SMART, "SMART" },
2265 { ATA_CMD_MEDIA_LOCK, "DOOR LOCK" },
2266 { ATA_CMD_MEDIA_UNLOCK, "DOOR UNLOCK" },
2267 { ATA_CMD_DSM, "DATA SET MANAGEMENT" },
2268 { ATA_CMD_CHK_MED_CRD_TYP, "CHECK MEDIA CARD TYPE" },
2269 { ATA_CMD_CFA_REQ_EXT_ERR, "CFA REQUEST EXTENDED ERROR" },
2270 { ATA_CMD_CFA_WRITE_NE, "CFA WRITE SECTORS WITHOUT ERASE" },
2271 { ATA_CMD_CFA_TRANS_SECT, "CFA TRANSLATE SECTOR" },
2272 { ATA_CMD_CFA_ERASE, "CFA ERASE SECTORS" },
2273 { ATA_CMD_CFA_WRITE_MULT_NE, "CFA WRITE MULTIPLE WITHOUT ERASE" },
2274 { ATA_CMD_READ_LONG, "READ LONG (with retries)" },
2275 { ATA_CMD_READ_LONG_ONCE, "READ LONG (without retries)" },
2276 { ATA_CMD_WRITE_LONG, "WRITE LONG (with retries)" },
2277 { ATA_CMD_WRITE_LONG_ONCE, "WRITE LONG (without retries)" },
2278 { ATA_CMD_RESTORE, "RECALIBRATE" },
2279 { 0, NULL } /* terminate list */
2283 for (i = 0; cmd_descr[i].text; i++)
2284 if (cmd_descr[i].command == command)
2285 return cmd_descr[i].text;
2292 * ata_eh_link_report - report error handling to user
2293 * @link: ATA link EH is going on
2295 * Report EH to user.
2300 static void ata_eh_link_report(struct ata_link *link)
2302 struct ata_port *ap = link->ap;
2303 struct ata_eh_context *ehc = &link->eh_context;
2304 const char *frozen, *desc;
2306 int tag, nr_failed = 0;
2308 if (ehc->i.flags & ATA_EHI_QUIET)
2312 if (ehc->i.desc[0] != '\0')
2315 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2316 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2318 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
2319 ata_dev_phys_link(qc->dev) != link ||
2320 ((qc->flags & ATA_QCFLAG_QUIET) &&
2321 qc->err_mask == AC_ERR_DEV))
2323 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
2329 if (!nr_failed && !ehc->i.err_mask)
2333 if (ap->pflags & ATA_PFLAG_FROZEN)
2336 memset(tries_buf, 0, sizeof(tries_buf));
2337 if (ap->eh_tries < ATA_EH_MAX_TRIES)
2338 snprintf(tries_buf, sizeof(tries_buf) - 1, " t%d",
2342 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
2343 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2344 ehc->i.err_mask, link->sactive, ehc->i.serror,
2345 ehc->i.action, frozen, tries_buf);
2347 ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
2349 ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
2350 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2351 ehc->i.err_mask, link->sactive, ehc->i.serror,
2352 ehc->i.action, frozen, tries_buf);
2354 ata_link_printk(link, KERN_ERR, "%s\n", desc);
2357 #ifdef CONFIG_ATA_VERBOSE_ERROR
2359 ata_link_printk(link, KERN_ERR,
2360 "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
2361 ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
2362 ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
2363 ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
2364 ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
2365 ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
2366 ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
2367 ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
2368 ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
2369 ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
2370 ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
2371 ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
2372 ehc->i.serror & SERR_CRC ? "BadCRC " : "",
2373 ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
2374 ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
2375 ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
2376 ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
2377 ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
2380 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2381 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2382 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
2383 const u8 *cdb = qc->cdb;
2384 char data_buf[20] = "";
2385 char cdb_buf[70] = "";
2387 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
2388 ata_dev_phys_link(qc->dev) != link || !qc->err_mask)
2391 if (qc->dma_dir != DMA_NONE) {
2392 static const char *dma_str[] = {
2393 [DMA_BIDIRECTIONAL] = "bidi",
2394 [DMA_TO_DEVICE] = "out",
2395 [DMA_FROM_DEVICE] = "in",
2397 static const char *prot_str[] = {
2398 [ATA_PROT_PIO] = "pio",
2399 [ATA_PROT_DMA] = "dma",
2400 [ATA_PROT_NCQ] = "ncq",
2401 [ATAPI_PROT_PIO] = "pio",
2402 [ATAPI_PROT_DMA] = "dma",
2405 snprintf(data_buf, sizeof(data_buf), " %s %u %s",
2406 prot_str[qc->tf.protocol], qc->nbytes,
2407 dma_str[qc->dma_dir]);
2410 if (ata_is_atapi(qc->tf.protocol)) {
2412 scsi_print_command(qc->scsicmd);
2414 snprintf(cdb_buf, sizeof(cdb_buf),
2415 "cdb %02x %02x %02x %02x %02x %02x %02x %02x "
2416 "%02x %02x %02x %02x %02x %02x %02x %02x\n ",
2417 cdb[0], cdb[1], cdb[2], cdb[3],
2418 cdb[4], cdb[5], cdb[6], cdb[7],
2419 cdb[8], cdb[9], cdb[10], cdb[11],
2420 cdb[12], cdb[13], cdb[14], cdb[15]);
2422 const char *descr = ata_get_cmd_descript(cmd->command);
2424 ata_dev_printk(qc->dev, KERN_ERR,
2425 "failed command: %s\n", descr);
2428 ata_dev_printk(qc->dev, KERN_ERR,
2429 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
2431 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
2432 "Emask 0x%x (%s)%s\n",
2433 cmd->command, cmd->feature, cmd->nsect,
2434 cmd->lbal, cmd->lbam, cmd->lbah,
2435 cmd->hob_feature, cmd->hob_nsect,
2436 cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
2437 cmd->device, qc->tag, data_buf, cdb_buf,
2438 res->command, res->feature, res->nsect,
2439 res->lbal, res->lbam, res->lbah,
2440 res->hob_feature, res->hob_nsect,
2441 res->hob_lbal, res->hob_lbam, res->hob_lbah,
2442 res->device, qc->err_mask, ata_err_string(qc->err_mask),
2443 qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
2445 #ifdef CONFIG_ATA_VERBOSE_ERROR
2446 if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
2448 if (res->command & ATA_BUSY)
2449 ata_dev_printk(qc->dev, KERN_ERR,
2450 "status: { Busy }\n");
2452 ata_dev_printk(qc->dev, KERN_ERR,
2453 "status: { %s%s%s%s}\n",
2454 res->command & ATA_DRDY ? "DRDY " : "",
2455 res->command & ATA_DF ? "DF " : "",
2456 res->command & ATA_DRQ ? "DRQ " : "",
2457 res->command & ATA_ERR ? "ERR " : "");
2460 if (cmd->command != ATA_CMD_PACKET &&
2461 (res->feature & (ATA_ICRC | ATA_UNC | ATA_IDNF |
2463 ata_dev_printk(qc->dev, KERN_ERR,
2464 "error: { %s%s%s%s}\n",
2465 res->feature & ATA_ICRC ? "ICRC " : "",
2466 res->feature & ATA_UNC ? "UNC " : "",
2467 res->feature & ATA_IDNF ? "IDNF " : "",
2468 res->feature & ATA_ABORTED ? "ABRT " : "");
2474 * ata_eh_report - report error handling to user
2475 * @ap: ATA port to report EH about
2477 * Report EH to user.
2482 void ata_eh_report(struct ata_port *ap)
2484 struct ata_link *link;
2486 ata_for_each_link(link, ap, HOST_FIRST)
2487 ata_eh_link_report(link);
2490 static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
2491 unsigned int *classes, unsigned long deadline,
2494 struct ata_device *dev;
2497 ata_for_each_dev(dev, link, ALL)
2498 classes[dev->devno] = ATA_DEV_UNKNOWN;
2500 return reset(link, classes, deadline);
2503 static int ata_eh_followup_srst_needed(struct ata_link *link,
2504 int rc, const unsigned int *classes)
2506 if ((link->flags & ATA_LFLAG_NO_SRST) || ata_link_offline(link))
2510 if (sata_pmp_supported(link->ap) && ata_is_host_link(link))
2515 int ata_eh_reset(struct ata_link *link, int classify,
2516 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
2517 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
2519 struct ata_port *ap = link->ap;
2520 struct ata_link *slave = ap->slave_link;
2521 struct ata_eh_context *ehc = &link->eh_context;
2522 struct ata_eh_context *sehc = slave ? &slave->eh_context : NULL;
2523 unsigned int *classes = ehc->classes;
2524 unsigned int lflags = link->flags;
2525 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
2526 int max_tries = 0, try = 0;
2527 struct ata_link *failed_link;
2528 struct ata_device *dev;
2529 unsigned long deadline, now;
2530 ata_reset_fn_t reset;
2531 unsigned long flags;
2538 while (ata_eh_reset_timeouts[max_tries] != ULONG_MAX)
2540 if (link->flags & ATA_LFLAG_NO_HRST)
2542 if (link->flags & ATA_LFLAG_NO_SRST)
2545 /* make sure each reset attemp is at least COOL_DOWN apart */
2546 if (ehc->i.flags & ATA_EHI_DID_RESET) {
2548 WARN_ON(time_after(ehc->last_reset, now));
2549 deadline = ata_deadline(ehc->last_reset,
2550 ATA_EH_RESET_COOL_DOWN);
2551 if (time_before(now, deadline))
2552 schedule_timeout_uninterruptible(deadline - now);
2555 spin_lock_irqsave(ap->lock, flags);
2556 ap->pflags |= ATA_PFLAG_RESETTING;
2557 spin_unlock_irqrestore(ap->lock, flags);
2559 ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
2561 ata_for_each_dev(dev, link, ALL) {
2562 /* If we issue an SRST then an ATA drive (not ATAPI)
2563 * may change configuration and be in PIO0 timing. If
2564 * we do a hard reset (or are coming from power on)
2565 * this is true for ATA or ATAPI. Until we've set a
2566 * suitable controller mode we should not touch the
2567 * bus as we may be talking too fast.
2569 dev->pio_mode = XFER_PIO_0;
2571 /* If the controller has a pio mode setup function
2572 * then use it to set the chipset to rights. Don't
2573 * touch the DMA setup as that will be dealt with when
2574 * configuring devices.
2576 if (ap->ops->set_piomode)
2577 ap->ops->set_piomode(ap, dev);
2580 /* prefer hardreset */
2582 ehc->i.action &= ~ATA_EH_RESET;
2585 ehc->i.action |= ATA_EH_HARDRESET;
2586 } else if (softreset) {
2588 ehc->i.action |= ATA_EH_SOFTRESET;
2592 unsigned long deadline = ata_deadline(jiffies,
2593 ATA_EH_PRERESET_TIMEOUT);
2596 sehc->i.action &= ~ATA_EH_RESET;
2597 sehc->i.action |= ehc->i.action;
2600 rc = prereset(link, deadline);
2602 /* If present, do prereset on slave link too. Reset
2603 * is skipped iff both master and slave links report
2604 * -ENOENT or clear ATA_EH_RESET.
2606 if (slave && (rc == 0 || rc == -ENOENT)) {
2609 tmp = prereset(slave, deadline);
2613 ehc->i.action |= sehc->i.action;
2617 if (rc == -ENOENT) {
2618 ata_link_printk(link, KERN_DEBUG,
2619 "port disabled. ignoring.\n");
2620 ehc->i.action &= ~ATA_EH_RESET;
2622 ata_for_each_dev(dev, link, ALL)
2623 classes[dev->devno] = ATA_DEV_NONE;
2627 ata_link_printk(link, KERN_ERR,
2628 "prereset failed (errno=%d)\n", rc);
2632 /* prereset() might have cleared ATA_EH_RESET. If so,
2633 * bang classes, thaw and return.
2635 if (reset && !(ehc->i.action & ATA_EH_RESET)) {
2636 ata_for_each_dev(dev, link, ALL)
2637 classes[dev->devno] = ATA_DEV_NONE;
2638 if ((ap->pflags & ATA_PFLAG_FROZEN) &&
2639 ata_is_host_link(link))
2640 ata_eh_thaw_port(ap);
2650 if (ata_is_host_link(link))
2651 ata_eh_freeze_port(ap);
2653 deadline = ata_deadline(jiffies, ata_eh_reset_timeouts[try++]);
2657 ata_link_printk(link, KERN_INFO, "%s resetting link\n",
2658 reset == softreset ? "soft" : "hard");
2660 /* mark that this EH session started with reset */
2661 ehc->last_reset = jiffies;
2662 if (reset == hardreset)
2663 ehc->i.flags |= ATA_EHI_DID_HARDRESET;
2665 ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
2667 rc = ata_do_reset(link, reset, classes, deadline, true);
2668 if (rc && rc != -EAGAIN) {
2673 /* hardreset slave link if existent */
2674 if (slave && reset == hardreset) {
2678 ata_link_printk(slave, KERN_INFO,
2679 "hard resetting link\n");
2681 ata_eh_about_to_do(slave, NULL, ATA_EH_RESET);
2682 tmp = ata_do_reset(slave, reset, classes, deadline,
2690 failed_link = slave;
2696 /* perform follow-up SRST if necessary */
2697 if (reset == hardreset &&
2698 ata_eh_followup_srst_needed(link, rc, classes)) {
2702 ata_link_printk(link, KERN_ERR,
2703 "follow-up softreset required "
2704 "but no softreset avaliable\n");
2710 ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
2711 rc = ata_do_reset(link, reset, classes, deadline, true);
2719 ata_link_printk(link, KERN_INFO, "no reset method "
2720 "available, skipping reset\n");
2721 if (!(lflags & ATA_LFLAG_ASSUME_CLASS))
2722 lflags |= ATA_LFLAG_ASSUME_ATA;
2726 * Post-reset processing
2728 ata_for_each_dev(dev, link, ALL) {
2729 /* After the reset, the device state is PIO 0 and the
2730 * controller state is undefined. Reset also wakes up
2731 * drives from sleeping mode.
2733 dev->pio_mode = XFER_PIO_0;
2734 dev->flags &= ~ATA_DFLAG_SLEEPING;
2736 if (ata_phys_link_offline(ata_dev_phys_link(dev)))
2739 /* apply class override */
2740 if (lflags & ATA_LFLAG_ASSUME_ATA)
2741 classes[dev->devno] = ATA_DEV_ATA;
2742 else if (lflags & ATA_LFLAG_ASSUME_SEMB)
2743 classes[dev->devno] = ATA_DEV_SEMB_UNSUP;
2746 /* record current link speed */
2747 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2748 link->sata_spd = (sstatus >> 4) & 0xf;
2749 if (slave && sata_scr_read(slave, SCR_STATUS, &sstatus) == 0)
2750 slave->sata_spd = (sstatus >> 4) & 0xf;
2753 if (ata_is_host_link(link))
2754 ata_eh_thaw_port(ap);
2756 /* postreset() should clear hardware SError. Although SError
2757 * is cleared during link resume, clearing SError here is
2758 * necessary as some PHYs raise hotplug events after SRST.
2759 * This introduces race condition where hotplug occurs between
2760 * reset and here. This race is mediated by cross checking
2761 * link onlineness and classification result later.
2764 postreset(link, classes);
2766 postreset(slave, classes);
2770 * Some controllers can't be frozen very well and may set
2771 * spuruious error conditions during reset. Clear accumulated
2772 * error information. As reset is the final recovery action,
2773 * nothing is lost by doing this.
2775 spin_lock_irqsave(link->ap->lock, flags);
2776 memset(&link->eh_info, 0, sizeof(link->eh_info));
2778 memset(&slave->eh_info, 0, sizeof(link->eh_info));
2779 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
2780 spin_unlock_irqrestore(link->ap->lock, flags);
2783 * Make sure onlineness and classification result correspond.
2784 * Hotplug could have happened during reset and some
2785 * controllers fail to wait while a drive is spinning up after
2786 * being hotplugged causing misdetection. By cross checking
2787 * link on/offlineness and classification result, those
2788 * conditions can be reliably detected and retried.
2791 ata_for_each_dev(dev, link, ALL) {
2792 if (ata_phys_link_online(ata_dev_phys_link(dev))) {
2793 if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
2794 ata_dev_printk(dev, KERN_DEBUG, "link online "
2795 "but device misclassifed\n");
2796 classes[dev->devno] = ATA_DEV_NONE;
2799 } else if (ata_phys_link_offline(ata_dev_phys_link(dev))) {
2800 if (ata_class_enabled(classes[dev->devno]))
2801 ata_dev_printk(dev, KERN_DEBUG, "link offline, "
2802 "clearing class %d to NONE\n",
2803 classes[dev->devno]);
2804 classes[dev->devno] = ATA_DEV_NONE;
2805 } else if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
2806 ata_dev_printk(dev, KERN_DEBUG, "link status unknown, "
2807 "clearing UNKNOWN to NONE\n");
2808 classes[dev->devno] = ATA_DEV_NONE;
2812 if (classify && nr_unknown) {
2813 if (try < max_tries) {
2814 ata_link_printk(link, KERN_WARNING, "link online but "
2815 "%d devices misclassified, retrying\n",
2821 ata_link_printk(link, KERN_WARNING,
2822 "link online but %d devices misclassified, "
2823 "device detection might fail\n", nr_unknown);
2826 /* reset successful, schedule revalidation */
2827 ata_eh_done(link, NULL, ATA_EH_RESET);
2829 ata_eh_done(slave, NULL, ATA_EH_RESET);
2830 ehc->last_reset = jiffies; /* update to completion time */
2831 ehc->i.action |= ATA_EH_REVALIDATE;
2832 link->lpm_policy = ATA_LPM_UNKNOWN; /* reset LPM state */
2836 /* clear hotplug flag */
2837 ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2839 sehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2841 spin_lock_irqsave(ap->lock, flags);
2842 ap->pflags &= ~ATA_PFLAG_RESETTING;
2843 spin_unlock_irqrestore(ap->lock, flags);
2848 /* if SCR isn't accessible on a fan-out port, PMP needs to be reset */
2849 if (!ata_is_host_link(link) &&
2850 sata_scr_read(link, SCR_STATUS, &sstatus))
2853 if (rc == -ERESTART || try >= max_tries)
2857 if (time_before(now, deadline)) {
2858 unsigned long delta = deadline - now;
2860 ata_link_printk(failed_link, KERN_WARNING,
2861 "reset failed (errno=%d), retrying in %u secs\n",
2862 rc, DIV_ROUND_UP(jiffies_to_msecs(delta), 1000));
2866 delta = schedule_timeout_uninterruptible(delta);
2870 if (try == max_tries - 1) {
2871 sata_down_spd_limit(link, 0);
2873 sata_down_spd_limit(slave, 0);
2874 } else if (rc == -EPIPE)
2875 sata_down_spd_limit(failed_link, 0);
2882 static inline void ata_eh_pull_park_action(struct ata_port *ap)
2884 struct ata_link *link;
2885 struct ata_device *dev;
2886 unsigned long flags;
2889 * This function can be thought of as an extended version of
2890 * ata_eh_about_to_do() specially crafted to accommodate the
2891 * requirements of ATA_EH_PARK handling. Since the EH thread
2892 * does not leave the do {} while () loop in ata_eh_recover as
2893 * long as the timeout for a park request to *one* device on
2894 * the port has not expired, and since we still want to pick
2895 * up park requests to other devices on the same port or
2896 * timeout updates for the same device, we have to pull
2897 * ATA_EH_PARK actions from eh_info into eh_context.i
2898 * ourselves at the beginning of each pass over the loop.
2900 * Additionally, all write accesses to &ap->park_req_pending
2901 * through INIT_COMPLETION() (see below) or complete_all()
2902 * (see ata_scsi_park_store()) are protected by the host lock.
2903 * As a result we have that park_req_pending.done is zero on
2904 * exit from this function, i.e. when ATA_EH_PARK actions for
2905 * *all* devices on port ap have been pulled into the
2906 * respective eh_context structs. If, and only if,
2907 * park_req_pending.done is non-zero by the time we reach
2908 * wait_for_completion_timeout(), another ATA_EH_PARK action
2909 * has been scheduled for at least one of the devices on port
2910 * ap and we have to cycle over the do {} while () loop in
2911 * ata_eh_recover() again.
2914 spin_lock_irqsave(ap->lock, flags);
2915 INIT_COMPLETION(ap->park_req_pending);
2916 ata_for_each_link(link, ap, EDGE) {
2917 ata_for_each_dev(dev, link, ALL) {
2918 struct ata_eh_info *ehi = &link->eh_info;
2920 link->eh_context.i.dev_action[dev->devno] |=
2921 ehi->dev_action[dev->devno] & ATA_EH_PARK;
2922 ata_eh_clear_action(link, dev, ehi, ATA_EH_PARK);
2925 spin_unlock_irqrestore(ap->lock, flags);
2928 static void ata_eh_park_issue_cmd(struct ata_device *dev, int park)
2930 struct ata_eh_context *ehc = &dev->link->eh_context;
2931 struct ata_taskfile tf;
2932 unsigned int err_mask;
2934 ata_tf_init(dev, &tf);
2936 ehc->unloaded_mask |= 1 << dev->devno;
2937 tf.command = ATA_CMD_IDLEIMMEDIATE;
2943 ehc->unloaded_mask &= ~(1 << dev->devno);
2944 tf.command = ATA_CMD_CHK_POWER;
2947 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
2948 tf.protocol |= ATA_PROT_NODATA;
2949 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
2950 if (park && (err_mask || tf.lbal != 0xc4)) {
2951 ata_dev_printk(dev, KERN_ERR, "head unload failed!\n");
2952 ehc->unloaded_mask &= ~(1 << dev->devno);
2956 static int ata_eh_revalidate_and_attach(struct ata_link *link,
2957 struct ata_device **r_failed_dev)
2959 struct ata_port *ap = link->ap;
2960 struct ata_eh_context *ehc = &link->eh_context;
2961 struct ata_device *dev;
2962 unsigned int new_mask = 0;
2963 unsigned long flags;
2968 /* For PATA drive side cable detection to work, IDENTIFY must
2969 * be done backwards such that PDIAG- is released by the slave
2970 * device before the master device is identified.
2972 ata_for_each_dev(dev, link, ALL_REVERSE) {
2973 unsigned int action = ata_eh_dev_action(dev);
2974 unsigned int readid_flags = 0;
2976 if (ehc->i.flags & ATA_EHI_DID_RESET)
2977 readid_flags |= ATA_READID_POSTRESET;
2979 if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
2980 WARN_ON(dev->class == ATA_DEV_PMP);
2982 if (ata_phys_link_offline(ata_dev_phys_link(dev))) {
2987 ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
2988 rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
2993 ata_eh_done(link, dev, ATA_EH_REVALIDATE);
2995 /* Configuration may have changed, reconfigure
2998 ehc->i.flags |= ATA_EHI_SETMODE;
3000 /* schedule the scsi_rescan_device() here */
3001 schedule_work(&(ap->scsi_rescan_task));
3002 } else if (dev->class == ATA_DEV_UNKNOWN &&
3003 ehc->tries[dev->devno] &&
3004 ata_class_enabled(ehc->classes[dev->devno])) {
3005 /* Temporarily set dev->class, it will be
3006 * permanently set once all configurations are
3007 * complete. This is necessary because new
3008 * device configuration is done in two
3011 dev->class = ehc->classes[dev->devno];
3013 if (dev->class == ATA_DEV_PMP)
3014 rc = sata_pmp_attach(dev);
3016 rc = ata_dev_read_id(dev, &dev->class,
3017 readid_flags, dev->id);
3019 /* read_id might have changed class, store and reset */
3020 ehc->classes[dev->devno] = dev->class;
3021 dev->class = ATA_DEV_UNKNOWN;
3025 /* clear error info accumulated during probe */
3026 ata_ering_clear(&dev->ering);
3027 new_mask |= 1 << dev->devno;
3030 /* IDENTIFY was issued to non-existent
3031 * device. No need to reset. Just
3032 * thaw and ignore the device.
3034 ata_eh_thaw_port(ap);
3042 /* PDIAG- should have been released, ask cable type if post-reset */
3043 if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) {
3044 if (ap->ops->cable_detect)
3045 ap->cbl = ap->ops->cable_detect(ap);
3049 /* Configure new devices forward such that user doesn't see
3050 * device detection messages backwards.
3052 ata_for_each_dev(dev, link, ALL) {
3053 if (!(new_mask & (1 << dev->devno)))
3056 dev->class = ehc->classes[dev->devno];
3058 if (dev->class == ATA_DEV_PMP)
3061 ehc->i.flags |= ATA_EHI_PRINTINFO;
3062 rc = ata_dev_configure(dev);
3063 ehc->i.flags &= ~ATA_EHI_PRINTINFO;
3065 dev->class = ATA_DEV_UNKNOWN;
3069 spin_lock_irqsave(ap->lock, flags);
3070 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
3071 spin_unlock_irqrestore(ap->lock, flags);
3073 /* new device discovered, configure xfermode */
3074 ehc->i.flags |= ATA_EHI_SETMODE;
3080 *r_failed_dev = dev;
3081 DPRINTK("EXIT rc=%d\n", rc);
3086 * ata_set_mode - Program timings and issue SET FEATURES - XFER
3087 * @link: link on which timings will be programmed
3088 * @r_failed_dev: out parameter for failed device
3090 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3091 * ata_set_mode() fails, pointer to the failing device is
3092 * returned in @r_failed_dev.
3095 * PCI/etc. bus probe sem.
3098 * 0 on success, negative errno otherwise
3100 int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3102 struct ata_port *ap = link->ap;
3103 struct ata_device *dev;
3106 /* if data transfer is verified, clear DUBIOUS_XFER on ering top */
3107 ata_for_each_dev(dev, link, ENABLED) {
3108 if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
3109 struct ata_ering_entry *ent;
3111 ent = ata_ering_top(&dev->ering);
3113 ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER;
3117 /* has private set_mode? */
3118 if (ap->ops->set_mode)
3119 rc = ap->ops->set_mode(link, r_failed_dev);
3121 rc = ata_do_set_mode(link, r_failed_dev);
3123 /* if transfer mode has changed, set DUBIOUS_XFER on device */
3124 ata_for_each_dev(dev, link, ENABLED) {
3125 struct ata_eh_context *ehc = &link->eh_context;
3126 u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
3127 u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
3129 if (dev->xfer_mode != saved_xfer_mode ||
3130 ata_ncq_enabled(dev) != saved_ncq)
3131 dev->flags |= ATA_DFLAG_DUBIOUS_XFER;
3138 * atapi_eh_clear_ua - Clear ATAPI UNIT ATTENTION after reset
3139 * @dev: ATAPI device to clear UA for
3141 * Resets and other operations can make an ATAPI device raise
3142 * UNIT ATTENTION which causes the next operation to fail. This
3143 * function clears UA.
3146 * EH context (may sleep).
3149 * 0 on success, -errno on failure.
3151 static int atapi_eh_clear_ua(struct ata_device *dev)
3155 for (i = 0; i < ATA_EH_UA_TRIES; i++) {
3156 u8 *sense_buffer = dev->link->ap->sector_buf;
3158 unsigned int err_mask;
3160 err_mask = atapi_eh_tur(dev, &sense_key);
3161 if (err_mask != 0 && err_mask != AC_ERR_DEV) {
3162 ata_dev_printk(dev, KERN_WARNING, "TEST_UNIT_READY "
3163 "failed (err_mask=0x%x)\n", err_mask);
3167 if (!err_mask || sense_key != UNIT_ATTENTION)
3170 err_mask = atapi_eh_request_sense(dev, sense_buffer, sense_key);
3172 ata_dev_printk(dev, KERN_WARNING, "failed to clear "
3173 "UNIT ATTENTION (err_mask=0x%x)\n", err_mask);
3178 ata_dev_printk(dev, KERN_WARNING,
3179 "UNIT ATTENTION persists after %d tries\n", ATA_EH_UA_TRIES);
3185 * ata_eh_maybe_retry_flush - Retry FLUSH if necessary
3186 * @dev: ATA device which may need FLUSH retry
3188 * If @dev failed FLUSH, it needs to be reported upper layer
3189 * immediately as it means that @dev failed to remap and already
3190 * lost at least a sector and further FLUSH retrials won't make
3191 * any difference to the lost sector. However, if FLUSH failed
3192 * for other reasons, for example transmission error, FLUSH needs
3195 * This function determines whether FLUSH failure retry is
3196 * necessary and performs it if so.
3199 * 0 if EH can continue, -errno if EH needs to be repeated.
3201 static int ata_eh_maybe_retry_flush(struct ata_device *dev)
3203 struct ata_link *link = dev->link;
3204 struct ata_port *ap = link->ap;
3205 struct ata_queued_cmd *qc;
3206 struct ata_taskfile tf;
3207 unsigned int err_mask;
3210 /* did flush fail for this device? */
3211 if (!ata_tag_valid(link->active_tag))
3214 qc = __ata_qc_from_tag(ap, link->active_tag);
3215 if (qc->dev != dev || (qc->tf.command != ATA_CMD_FLUSH_EXT &&
3216 qc->tf.command != ATA_CMD_FLUSH))
3219 /* if the device failed it, it should be reported to upper layers */
3220 if (qc->err_mask & AC_ERR_DEV)
3223 /* flush failed for some other reason, give it another shot */
3224 ata_tf_init(dev, &tf);
3226 tf.command = qc->tf.command;
3227 tf.flags |= ATA_TFLAG_DEVICE;
3228 tf.protocol = ATA_PROT_NODATA;
3230 ata_dev_printk(dev, KERN_WARNING, "retrying FLUSH 0x%x Emask 0x%x\n",
3231 tf.command, qc->err_mask);
3233 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
3236 * FLUSH is complete but there's no way to
3237 * successfully complete a failed command from EH.
3238 * Making sure retry is allowed at least once and
3239 * retrying it should do the trick - whatever was in
3240 * the cache is already on the platter and this won't
3241 * cause infinite loop.
3243 qc->scsicmd->allowed = max(qc->scsicmd->allowed, 1);
3245 ata_dev_printk(dev, KERN_WARNING, "FLUSH failed Emask 0x%x\n",
3249 /* if device failed it, report it to upper layers */
3250 if (err_mask & AC_ERR_DEV) {
3251 qc->err_mask |= AC_ERR_DEV;
3253 if (!(ap->pflags & ATA_PFLAG_FROZEN))
3261 * ata_eh_set_lpm - configure SATA interface power management
3262 * @link: link to configure power management
3263 * @policy: the link power management policy
3264 * @r_failed_dev: out parameter for failed device
3266 * Enable SATA Interface power management. This will enable
3267 * Device Interface Power Management (DIPM) for min_power
3268 * policy, and then call driver specific callbacks for
3269 * enabling Host Initiated Power management.
3275 * 0 on success, -errno on failure.
3277 static int ata_eh_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
3278 struct ata_device **r_failed_dev)
3280 struct ata_port *ap = ata_is_host_link(link) ? link->ap : NULL;
3281 struct ata_eh_context *ehc = &link->eh_context;
3282 struct ata_device *dev, *link_dev = NULL, *lpm_dev = NULL;
3283 enum ata_lpm_policy old_policy = link->lpm_policy;
3284 unsigned int hints = ATA_LPM_EMPTY | ATA_LPM_HIPM;
3285 unsigned int err_mask;
3288 /* if the link or host doesn't do LPM, noop */
3289 if ((link->flags & ATA_LFLAG_NO_LPM) || (ap && !ap->ops->set_lpm))
3293 * DIPM is enabled only for MIN_POWER as some devices
3294 * misbehave when the host NACKs transition to SLUMBER. Order
3295 * device and link configurations such that the host always
3296 * allows DIPM requests.
3298 ata_for_each_dev(dev, link, ENABLED) {
3299 bool hipm = ata_id_has_hipm(dev->id);
3300 bool dipm = ata_id_has_dipm(dev->id);
3302 /* find the first enabled and LPM enabled devices */
3306 if (!lpm_dev && (hipm || dipm))
3309 hints &= ~ATA_LPM_EMPTY;
3311 hints &= ~ATA_LPM_HIPM;
3313 /* disable DIPM before changing link config */
3314 if (policy != ATA_LPM_MIN_POWER && dipm) {
3315 err_mask = ata_dev_set_feature(dev,
3316 SETFEATURES_SATA_DISABLE, SATA_DIPM);
3317 if (err_mask && err_mask != AC_ERR_DEV) {
3318 ata_dev_printk(dev, KERN_WARNING,
3319 "failed to disable DIPM, Emask 0x%x\n",
3328 rc = ap->ops->set_lpm(link, policy, hints);
3329 if (!rc && ap->slave_link)
3330 rc = ap->ops->set_lpm(ap->slave_link, policy, hints);
3332 rc = sata_pmp_set_lpm(link, policy, hints);
3335 * Attribute link config failure to the first (LPM) enabled
3336 * device on the link.
3339 if (rc == -EOPNOTSUPP) {
3340 link->flags |= ATA_LFLAG_NO_LPM;
3343 dev = lpm_dev ? lpm_dev : link_dev;
3348 * Low level driver acked the transition. Issue DIPM command
3349 * with the new policy set.
3351 link->lpm_policy = policy;
3352 if (ap && ap->slave_link)
3353 ap->slave_link->lpm_policy = policy;
3355 /* host config updated, enable DIPM if transitioning to MIN_POWER */
3356 ata_for_each_dev(dev, link, ENABLED) {
3357 if (policy == ATA_LPM_MIN_POWER && ata_id_has_dipm(dev->id)) {
3358 err_mask = ata_dev_set_feature(dev,
3359 SETFEATURES_SATA_ENABLE, SATA_DIPM);
3360 if (err_mask && err_mask != AC_ERR_DEV) {
3361 ata_dev_printk(dev, KERN_WARNING,
3362 "failed to enable DIPM, Emask 0x%x\n",
3373 /* restore the old policy */
3374 link->lpm_policy = old_policy;
3375 if (ap && ap->slave_link)
3376 ap->slave_link->lpm_policy = old_policy;
3378 /* if no device or only one more chance is left, disable LPM */
3379 if (!dev || ehc->tries[dev->devno] <= 2) {
3380 ata_link_printk(link, KERN_WARNING,
3381 "disabling LPM on the link\n");
3382 link->flags |= ATA_LFLAG_NO_LPM;
3385 *r_failed_dev = dev;
3389 static int ata_link_nr_enabled(struct ata_link *link)
3391 struct ata_device *dev;
3394 ata_for_each_dev(dev, link, ENABLED)
3399 static int ata_link_nr_vacant(struct ata_link *link)
3401 struct ata_device *dev;
3404 ata_for_each_dev(dev, link, ALL)
3405 if (dev->class == ATA_DEV_UNKNOWN)
3410 static int ata_eh_skip_recovery(struct ata_link *link)
3412 struct ata_port *ap = link->ap;
3413 struct ata_eh_context *ehc = &link->eh_context;
3414 struct ata_device *dev;
3416 /* skip disabled links */
3417 if (link->flags & ATA_LFLAG_DISABLED)
3420 /* skip if explicitly requested */
3421 if (ehc->i.flags & ATA_EHI_NO_RECOVERY)
3424 /* thaw frozen port and recover failed devices */
3425 if ((ap->pflags & ATA_PFLAG_FROZEN) || ata_link_nr_enabled(link))
3428 /* reset at least once if reset is requested */
3429 if ((ehc->i.action & ATA_EH_RESET) &&
3430 !(ehc->i.flags & ATA_EHI_DID_RESET))
3433 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
3434 ata_for_each_dev(dev, link, ALL) {
3435 if (dev->class == ATA_DEV_UNKNOWN &&
3436 ehc->classes[dev->devno] != ATA_DEV_NONE)
3443 static int ata_count_probe_trials_cb(struct ata_ering_entry *ent, void *void_arg)
3445 u64 interval = msecs_to_jiffies(ATA_EH_PROBE_TRIAL_INTERVAL);
3446 u64 now = get_jiffies_64();
3447 int *trials = void_arg;
3449 if (ent->timestamp < now - min(now, interval))
3456 static int ata_eh_schedule_probe(struct ata_device *dev)
3458 struct ata_eh_context *ehc = &dev->link->eh_context;
3459 struct ata_link *link = ata_dev_phys_link(dev);
3462 if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
3463 (ehc->did_probe_mask & (1 << dev->devno)))
3466 ata_eh_detach_dev(dev);
3468 ehc->did_probe_mask |= (1 << dev->devno);
3469 ehc->i.action |= ATA_EH_RESET;
3470 ehc->saved_xfer_mode[dev->devno] = 0;
3471 ehc->saved_ncq_enabled &= ~(1 << dev->devno);
3473 /* the link maybe in a deep sleep, wake it up */
3474 if (link->lpm_policy > ATA_LPM_MAX_POWER) {
3475 if (ata_is_host_link(link))
3476 link->ap->ops->set_lpm(link, ATA_LPM_MAX_POWER,
3479 sata_pmp_set_lpm(link, ATA_LPM_MAX_POWER,
3483 /* Record and count probe trials on the ering. The specific
3484 * error mask used is irrelevant. Because a successful device
3485 * detection clears the ering, this count accumulates only if
3486 * there are consecutive failed probes.
3488 * If the count is equal to or higher than ATA_EH_PROBE_TRIALS
3489 * in the last ATA_EH_PROBE_TRIAL_INTERVAL, link speed is
3490 * forced to 1.5Gbps.
3492 * This is to work around cases where failed link speed
3493 * negotiation results in device misdetection leading to
3494 * infinite DEVXCHG or PHRDY CHG events.
3496 ata_ering_record(&dev->ering, 0, AC_ERR_OTHER);
3497 ata_ering_map(&dev->ering, ata_count_probe_trials_cb, &trials);
3499 if (trials > ATA_EH_PROBE_TRIALS)
3500 sata_down_spd_limit(link, 1);
3505 static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
3507 struct ata_eh_context *ehc = &dev->link->eh_context;
3509 /* -EAGAIN from EH routine indicates retry without prejudice.
3510 * The requester is responsible for ensuring forward progress.
3513 ehc->tries[dev->devno]--;
3517 /* device missing or wrong IDENTIFY data, schedule probing */
3518 ehc->i.probe_mask |= (1 << dev->devno);
3520 /* give it just one more chance */
3521 ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
3523 if (ehc->tries[dev->devno] == 1) {
3524 /* This is the last chance, better to slow
3525 * down than lose it.
3527 sata_down_spd_limit(ata_dev_phys_link(dev), 0);
3528 if (dev->pio_mode > XFER_PIO_0)
3529 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
3533 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
3534 /* disable device if it has used up all its chances */
3535 ata_dev_disable(dev);
3537 /* detach if offline */
3538 if (ata_phys_link_offline(ata_dev_phys_link(dev)))
3539 ata_eh_detach_dev(dev);
3541 /* schedule probe if necessary */
3542 if (ata_eh_schedule_probe(dev)) {
3543 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
3544 memset(ehc->cmd_timeout_idx[dev->devno], 0,
3545 sizeof(ehc->cmd_timeout_idx[dev->devno]));
3550 ehc->i.action |= ATA_EH_RESET;
3556 * ata_eh_recover - recover host port after error
3557 * @ap: host port to recover
3558 * @prereset: prereset method (can be NULL)
3559 * @softreset: softreset method (can be NULL)
3560 * @hardreset: hardreset method (can be NULL)
3561 * @postreset: postreset method (can be NULL)
3562 * @r_failed_link: out parameter for failed link
3564 * This is the alpha and omega, eum and yang, heart and soul of
3565 * libata exception handling. On entry, actions required to
3566 * recover each link and hotplug requests are recorded in the
3567 * link's eh_context. This function executes all the operations
3568 * with appropriate retrials and fallbacks to resurrect failed
3569 * devices, detach goners and greet newcomers.
3572 * Kernel thread context (may sleep).
3575 * 0 on success, -errno on failure.
3577 int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
3578 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
3579 ata_postreset_fn_t postreset,
3580 struct ata_link **r_failed_link)
3582 struct ata_link *link;
3583 struct ata_device *dev;
3585 unsigned long flags, deadline;
3589 /* prep for recovery */
3590 ata_for_each_link(link, ap, EDGE) {
3591 struct ata_eh_context *ehc = &link->eh_context;
3593 /* re-enable link? */
3594 if (ehc->i.action & ATA_EH_ENABLE_LINK) {
3595 ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
3596 spin_lock_irqsave(ap->lock, flags);
3597 link->flags &= ~ATA_LFLAG_DISABLED;
3598 spin_unlock_irqrestore(ap->lock, flags);
3599 ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
3602 ata_for_each_dev(dev, link, ALL) {
3603 if (link->flags & ATA_LFLAG_NO_RETRY)
3604 ehc->tries[dev->devno] = 1;
3606 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
3608 /* collect port action mask recorded in dev actions */
3609 ehc->i.action |= ehc->i.dev_action[dev->devno] &
3610 ~ATA_EH_PERDEV_MASK;
3611 ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
3613 /* process hotplug request */
3614 if (dev->flags & ATA_DFLAG_DETACH)
3615 ata_eh_detach_dev(dev);
3617 /* schedule probe if necessary */
3618 if (!ata_dev_enabled(dev))
3619 ata_eh_schedule_probe(dev);
3626 /* if UNLOADING, finish immediately */
3627 if (ap->pflags & ATA_PFLAG_UNLOADING)
3631 ata_for_each_link(link, ap, EDGE) {
3632 struct ata_eh_context *ehc = &link->eh_context;
3634 /* skip EH if possible. */
3635 if (ata_eh_skip_recovery(link))
3638 ata_for_each_dev(dev, link, ALL)
3639 ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
3643 ata_for_each_link(link, ap, EDGE) {
3644 struct ata_eh_context *ehc = &link->eh_context;
3646 if (!(ehc->i.action & ATA_EH_RESET))
3649 rc = ata_eh_reset(link, ata_link_nr_vacant(link),
3650 prereset, softreset, hardreset, postreset);
3652 ata_link_printk(link, KERN_ERR,
3653 "reset failed, giving up\n");
3662 * clears ATA_EH_PARK in eh_info and resets
3663 * ap->park_req_pending
3665 ata_eh_pull_park_action(ap);
3668 ata_for_each_link(link, ap, EDGE) {
3669 ata_for_each_dev(dev, link, ALL) {
3670 struct ata_eh_context *ehc = &link->eh_context;
3673 if (dev->class != ATA_DEV_ATA)
3675 if (!(ehc->i.dev_action[dev->devno] &
3678 tmp = dev->unpark_deadline;
3679 if (time_before(deadline, tmp))
3681 else if (time_before_eq(tmp, jiffies))
3683 if (ehc->unloaded_mask & (1 << dev->devno))
3686 ata_eh_park_issue_cmd(dev, 1);
3691 if (time_before_eq(deadline, now))
3695 deadline = wait_for_completion_timeout(&ap->park_req_pending,
3699 ata_for_each_link(link, ap, EDGE) {
3700 ata_for_each_dev(dev, link, ALL) {
3701 if (!(link->eh_context.unloaded_mask &
3705 ata_eh_park_issue_cmd(dev, 0);
3706 ata_eh_done(link, dev, ATA_EH_PARK);
3712 ata_for_each_link(link, ap, PMP_FIRST) {
3713 struct ata_eh_context *ehc = &link->eh_context;
3715 if (sata_pmp_attached(ap) && ata_is_host_link(link))
3718 /* revalidate existing devices and attach new ones */
3719 rc = ata_eh_revalidate_and_attach(link, &dev);
3723 /* if PMP got attached, return, pmp EH will take care of it */
3724 if (link->device->class == ATA_DEV_PMP) {
3729 /* configure transfer mode if necessary */
3730 if (ehc->i.flags & ATA_EHI_SETMODE) {
3731 rc = ata_set_mode(link, &dev);
3734 ehc->i.flags &= ~ATA_EHI_SETMODE;
3737 /* If reset has been issued, clear UA to avoid
3738 * disrupting the current users of the device.
3740 if (ehc->i.flags & ATA_EHI_DID_RESET) {
3741 ata_for_each_dev(dev, link, ALL) {
3742 if (dev->class != ATA_DEV_ATAPI)
3744 rc = atapi_eh_clear_ua(dev);
3750 /* retry flush if necessary */
3751 ata_for_each_dev(dev, link, ALL) {
3752 if (dev->class != ATA_DEV_ATA)
3754 rc = ata_eh_maybe_retry_flush(dev);
3760 /* configure link power saving */
3761 if (link->lpm_policy != ap->target_lpm_policy) {
3762 rc = ata_eh_set_lpm(link, ap->target_lpm_policy, &dev);
3767 /* this link is okay now */
3774 ata_eh_handle_dev_fail(dev, rc);
3776 if (ap->pflags & ATA_PFLAG_FROZEN) {
3777 /* PMP reset requires working host port.
3778 * Can't retry if it's frozen.
3780 if (sata_pmp_attached(ap))
3790 if (rc && r_failed_link)
3791 *r_failed_link = link;
3793 DPRINTK("EXIT, rc=%d\n", rc);
3798 * ata_eh_finish - finish up EH
3799 * @ap: host port to finish EH for
3801 * Recovery is complete. Clean up EH states and retry or finish
3807 void ata_eh_finish(struct ata_port *ap)
3811 /* retry or finish qcs */
3812 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
3813 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
3815 if (!(qc->flags & ATA_QCFLAG_FAILED))
3819 /* FIXME: Once EH migration is complete,
3820 * generate sense data in this function,
3821 * considering both err_mask and tf.
3823 if (qc->flags & ATA_QCFLAG_RETRY)
3824 ata_eh_qc_retry(qc);
3826 ata_eh_qc_complete(qc);
3828 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
3829 ata_eh_qc_complete(qc);
3831 /* feed zero TF to sense generation */
3832 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
3833 ata_eh_qc_retry(qc);
3838 /* make sure nr_active_links is zero after EH */
3839 WARN_ON(ap->nr_active_links);
3840 ap->nr_active_links = 0;
3844 * ata_do_eh - do standard error handling
3845 * @ap: host port to handle error for
3847 * @prereset: prereset method (can be NULL)
3848 * @softreset: softreset method (can be NULL)
3849 * @hardreset: hardreset method (can be NULL)
3850 * @postreset: postreset method (can be NULL)
3852 * Perform standard error handling sequence.
3855 * Kernel thread context (may sleep).
3857 void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
3858 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
3859 ata_postreset_fn_t postreset)
3861 struct ata_device *dev;
3867 rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
3870 ata_for_each_dev(dev, &ap->link, ALL)
3871 ata_dev_disable(dev);
3878 * ata_std_error_handler - standard error handler
3879 * @ap: host port to handle error for
3881 * Standard error handler
3884 * Kernel thread context (may sleep).
3886 void ata_std_error_handler(struct ata_port *ap)
3888 struct ata_port_operations *ops = ap->ops;
3889 ata_reset_fn_t hardreset = ops->hardreset;
3891 /* ignore built-in hardreset if SCR access is not available */
3892 if (hardreset == sata_std_hardreset && !sata_scr_valid(&ap->link))
3895 ata_do_eh(ap, ops->prereset, ops->softreset, hardreset, ops->postreset);
3900 * ata_eh_handle_port_suspend - perform port suspend operation
3901 * @ap: port to suspend
3906 * Kernel thread context (may sleep).
3908 static void ata_eh_handle_port_suspend(struct ata_port *ap)
3910 unsigned long flags;
3913 /* are we suspending? */
3914 spin_lock_irqsave(ap->lock, flags);
3915 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
3916 ap->pm_mesg.event == PM_EVENT_ON) {
3917 spin_unlock_irqrestore(ap->lock, flags);
3920 spin_unlock_irqrestore(ap->lock, flags);
3922 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
3924 /* tell ACPI we're suspending */
3925 rc = ata_acpi_on_suspend(ap);
3930 ata_eh_freeze_port(ap);
3932 if (ap->ops->port_suspend)
3933 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
3935 ata_acpi_set_state(ap, PMSG_SUSPEND);
3938 spin_lock_irqsave(ap->lock, flags);
3940 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
3942 ap->pflags |= ATA_PFLAG_SUSPENDED;
3943 else if (ap->pflags & ATA_PFLAG_FROZEN)
3944 ata_port_schedule_eh(ap);
3946 if (ap->pm_result) {
3947 *ap->pm_result = rc;
3948 ap->pm_result = NULL;
3951 spin_unlock_irqrestore(ap->lock, flags);
3957 * ata_eh_handle_port_resume - perform port resume operation
3958 * @ap: port to resume
3963 * Kernel thread context (may sleep).
3965 static void ata_eh_handle_port_resume(struct ata_port *ap)
3967 struct ata_link *link;
3968 struct ata_device *dev;
3969 unsigned long flags;
3972 /* are we resuming? */
3973 spin_lock_irqsave(ap->lock, flags);
3974 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
3975 ap->pm_mesg.event != PM_EVENT_ON) {
3976 spin_unlock_irqrestore(ap->lock, flags);
3979 spin_unlock_irqrestore(ap->lock, flags);
3981 WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
3984 * Error timestamps are in jiffies which doesn't run while
3985 * suspended and PHY events during resume isn't too uncommon.
3986 * When the two are combined, it can lead to unnecessary speed
3987 * downs if the machine is suspended and resumed repeatedly.
3988 * Clear error history.
3990 ata_for_each_link(link, ap, HOST_FIRST)
3991 ata_for_each_dev(dev, link, ALL)
3992 ata_ering_clear(&dev->ering);
3994 ata_acpi_set_state(ap, PMSG_ON);
3996 if (ap->ops->port_resume)
3997 rc = ap->ops->port_resume(ap);
3999 /* tell ACPI that we're resuming */
4000 ata_acpi_on_resume(ap);
4003 spin_lock_irqsave(ap->lock, flags);
4004 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
4005 if (ap->pm_result) {
4006 *ap->pm_result = rc;
4007 ap->pm_result = NULL;
4009 spin_unlock_irqrestore(ap->lock, flags);
4011 #endif /* CONFIG_PM */