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/pci.h>
37 #include <scsi/scsi.h>
38 #include <scsi/scsi_host.h>
39 #include <scsi/scsi_eh.h>
40 #include <scsi/scsi_device.h>
41 #include <scsi/scsi_cmnd.h>
42 #include "../scsi/scsi_transport_api.h"
44 #include <linux/libata.h>
49 /* speed down verdicts */
50 ATA_EH_SPDN_NCQ_OFF = (1 << 0),
51 ATA_EH_SPDN_SPEED_DOWN = (1 << 1),
52 ATA_EH_SPDN_FALLBACK_TO_PIO = (1 << 2),
53 ATA_EH_SPDN_KEEP_ERRORS = (1 << 3),
56 ATA_EFLAG_IS_IO = (1 << 0),
57 ATA_EFLAG_DUBIOUS_XFER = (1 << 1),
59 /* error categories */
62 ATA_ECAT_TOUT_HSM = 2,
64 ATA_ECAT_DUBIOUS_NONE = 4,
65 ATA_ECAT_DUBIOUS_ATA_BUS = 5,
66 ATA_ECAT_DUBIOUS_TOUT_HSM = 6,
67 ATA_ECAT_DUBIOUS_UNK_DEV = 7,
70 /* always put at least this amount of time between resets */
71 ATA_EH_RESET_COOL_DOWN = 5000,
73 /* Waiting in ->prereset can never be reliable. It's
74 * sometimes nice to wait there but it can't be depended upon;
75 * otherwise, we wouldn't be resetting. Just give it enough
76 * time for most drives to spin up.
78 ATA_EH_PRERESET_TIMEOUT = 10000,
79 ATA_EH_FASTDRAIN_INTERVAL = 3000,
82 /* The following table determines how we sequence resets. Each entry
83 * represents timeout for that try. The first try can be soft or
84 * hardreset. All others are hardreset if available. In most cases
85 * the first reset w/ 10sec timeout should succeed. Following entries
86 * are mostly for error handling, hotplug and retarded devices.
88 static const unsigned long ata_eh_reset_timeouts[] = {
89 10000, /* most drives spin up by 10sec */
90 10000, /* > 99% working drives spin up before 20sec */
91 35000, /* give > 30 secs of idleness for retarded devices */
92 5000, /* and sweet one last chance */
93 /* > 1 min has elapsed, give up */
96 static void __ata_port_freeze(struct ata_port *ap);
98 static void ata_eh_handle_port_suspend(struct ata_port *ap);
99 static void ata_eh_handle_port_resume(struct ata_port *ap);
100 #else /* CONFIG_PM */
101 static void ata_eh_handle_port_suspend(struct ata_port *ap)
104 static void ata_eh_handle_port_resume(struct ata_port *ap)
106 #endif /* CONFIG_PM */
108 static void __ata_ehi_pushv_desc(struct ata_eh_info *ehi, const char *fmt,
111 ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
112 ATA_EH_DESC_LEN - ehi->desc_len,
117 * __ata_ehi_push_desc - push error description without adding separator
119 * @fmt: printf format string
121 * Format string according to @fmt and append it to @ehi->desc.
124 * spin_lock_irqsave(host lock)
126 void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
131 __ata_ehi_pushv_desc(ehi, fmt, args);
136 * ata_ehi_push_desc - push error description with separator
138 * @fmt: printf format string
140 * Format string according to @fmt and append it to @ehi->desc.
141 * If @ehi->desc is not empty, ", " is added in-between.
144 * spin_lock_irqsave(host lock)
146 void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
151 __ata_ehi_push_desc(ehi, ", ");
154 __ata_ehi_pushv_desc(ehi, fmt, args);
159 * ata_ehi_clear_desc - clean error description
165 * spin_lock_irqsave(host lock)
167 void ata_ehi_clear_desc(struct ata_eh_info *ehi)
174 * ata_port_desc - append port description
175 * @ap: target ATA port
176 * @fmt: printf format string
178 * Format string according to @fmt and append it to port
179 * description. If port description is not empty, " " is added
180 * in-between. This function is to be used while initializing
181 * ata_host. The description is printed on host registration.
186 void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
190 WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
192 if (ap->link.eh_info.desc_len)
193 __ata_ehi_push_desc(&ap->link.eh_info, " ");
196 __ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
203 * ata_port_pbar_desc - append PCI BAR description
204 * @ap: target ATA port
205 * @bar: target PCI BAR
206 * @offset: offset into PCI BAR
207 * @name: name of the area
209 * If @offset is negative, this function formats a string which
210 * contains the name, address, size and type of the BAR and
211 * appends it to the port description. If @offset is zero or
212 * positive, only name and offsetted address is appended.
217 void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
220 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
222 unsigned long long start, len;
224 if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
226 else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
229 start = (unsigned long long)pci_resource_start(pdev, bar);
230 len = (unsigned long long)pci_resource_len(pdev, bar);
233 ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
235 ata_port_desc(ap, "%s 0x%llx", name,
236 start + (unsigned long long)offset);
239 #endif /* CONFIG_PCI */
241 static void ata_ering_record(struct ata_ering *ering, unsigned int eflags,
242 unsigned int err_mask)
244 struct ata_ering_entry *ent;
249 ering->cursor %= ATA_ERING_SIZE;
251 ent = &ering->ring[ering->cursor];
252 ent->eflags = eflags;
253 ent->err_mask = err_mask;
254 ent->timestamp = get_jiffies_64();
257 static struct ata_ering_entry *ata_ering_top(struct ata_ering *ering)
259 struct ata_ering_entry *ent = &ering->ring[ering->cursor];
266 static void ata_ering_clear(struct ata_ering *ering)
268 memset(ering, 0, sizeof(*ering));
271 static int ata_ering_map(struct ata_ering *ering,
272 int (*map_fn)(struct ata_ering_entry *, void *),
276 struct ata_ering_entry *ent;
280 ent = &ering->ring[idx];
283 rc = map_fn(ent, arg);
286 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
287 } while (idx != ering->cursor);
292 static unsigned int ata_eh_dev_action(struct ata_device *dev)
294 struct ata_eh_context *ehc = &dev->link->eh_context;
296 return ehc->i.action | ehc->i.dev_action[dev->devno];
299 static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
300 struct ata_eh_info *ehi, unsigned int action)
302 struct ata_device *tdev;
305 ehi->action &= ~action;
306 ata_link_for_each_dev(tdev, link)
307 ehi->dev_action[tdev->devno] &= ~action;
309 /* doesn't make sense for port-wide EH actions */
310 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
312 /* break ehi->action into ehi->dev_action */
313 if (ehi->action & action) {
314 ata_link_for_each_dev(tdev, link)
315 ehi->dev_action[tdev->devno] |=
316 ehi->action & action;
317 ehi->action &= ~action;
320 /* turn off the specified per-dev action */
321 ehi->dev_action[dev->devno] &= ~action;
326 * ata_scsi_timed_out - SCSI layer time out callback
327 * @cmd: timed out SCSI command
329 * Handles SCSI layer timeout. We race with normal completion of
330 * the qc for @cmd. If the qc is already gone, we lose and let
331 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
332 * timed out and EH should be invoked. Prevent ata_qc_complete()
333 * from finishing it by setting EH_SCHEDULED and return
336 * TODO: kill this function once old EH is gone.
339 * Called from timer context
342 * EH_HANDLED or EH_NOT_HANDLED
344 enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
346 struct Scsi_Host *host = cmd->device->host;
347 struct ata_port *ap = ata_shost_to_port(host);
349 struct ata_queued_cmd *qc;
350 enum scsi_eh_timer_return ret;
354 if (ap->ops->error_handler) {
355 ret = EH_NOT_HANDLED;
360 spin_lock_irqsave(ap->lock, flags);
361 qc = ata_qc_from_tag(ap, ap->link.active_tag);
363 WARN_ON(qc->scsicmd != cmd);
364 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
365 qc->err_mask |= AC_ERR_TIMEOUT;
366 ret = EH_NOT_HANDLED;
368 spin_unlock_irqrestore(ap->lock, flags);
371 DPRINTK("EXIT, ret=%d\n", ret);
376 * ata_scsi_error - SCSI layer error handler callback
377 * @host: SCSI host on which error occurred
379 * Handles SCSI-layer-thrown error events.
382 * Inherited from SCSI layer (none, can sleep)
387 void ata_scsi_error(struct Scsi_Host *host)
389 struct ata_port *ap = ata_shost_to_port(host);
395 /* synchronize with port task */
396 ata_port_flush_task(ap);
398 /* synchronize with host lock and sort out timeouts */
400 /* For new EH, all qcs are finished in one of three ways -
401 * normal completion, error completion, and SCSI timeout.
402 * Both cmpletions can race against SCSI timeout. When normal
403 * completion wins, the qc never reaches EH. When error
404 * completion wins, the qc has ATA_QCFLAG_FAILED set.
406 * When SCSI timeout wins, things are a bit more complex.
407 * Normal or error completion can occur after the timeout but
408 * before this point. In such cases, both types of
409 * completions are honored. A scmd is determined to have
410 * timed out iff its associated qc is active and not failed.
412 if (ap->ops->error_handler) {
413 struct scsi_cmnd *scmd, *tmp;
416 spin_lock_irqsave(ap->lock, flags);
418 list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
419 struct ata_queued_cmd *qc;
421 for (i = 0; i < ATA_MAX_QUEUE; i++) {
422 qc = __ata_qc_from_tag(ap, i);
423 if (qc->flags & ATA_QCFLAG_ACTIVE &&
428 if (i < ATA_MAX_QUEUE) {
429 /* the scmd has an associated qc */
430 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
431 /* which hasn't failed yet, timeout */
432 qc->err_mask |= AC_ERR_TIMEOUT;
433 qc->flags |= ATA_QCFLAG_FAILED;
437 /* Normal completion occurred after
438 * SCSI timeout but before this point.
439 * Successfully complete it.
441 scmd->retries = scmd->allowed;
442 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
446 /* If we have timed out qcs. They belong to EH from
447 * this point but the state of the controller is
448 * unknown. Freeze the port to make sure the IRQ
449 * handler doesn't diddle with those qcs. This must
450 * be done atomically w.r.t. setting QCFLAG_FAILED.
453 __ata_port_freeze(ap);
455 spin_unlock_irqrestore(ap->lock, flags);
457 /* initialize eh_tries */
458 ap->eh_tries = ATA_EH_MAX_TRIES;
460 spin_unlock_wait(ap->lock);
463 /* invoke error handler */
464 if (ap->ops->error_handler) {
465 struct ata_link *link;
467 /* kill fast drain timer */
468 del_timer_sync(&ap->fastdrain_timer);
470 /* process port resume request */
471 ata_eh_handle_port_resume(ap);
473 /* fetch & clear EH info */
474 spin_lock_irqsave(ap->lock, flags);
476 __ata_port_for_each_link(link, ap) {
477 struct ata_eh_context *ehc = &link->eh_context;
478 struct ata_device *dev;
480 memset(&link->eh_context, 0, sizeof(link->eh_context));
481 link->eh_context.i = link->eh_info;
482 memset(&link->eh_info, 0, sizeof(link->eh_info));
484 ata_link_for_each_dev(dev, link) {
485 int devno = dev->devno;
487 ehc->saved_xfer_mode[devno] = dev->xfer_mode;
488 if (ata_ncq_enabled(dev))
489 ehc->saved_ncq_enabled |= 1 << devno;
492 /* set last reset timestamp to some time in the past */
493 ehc->last_reset = jiffies - 60 * HZ;
496 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
497 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
498 ap->excl_link = NULL; /* don't maintain exclusion over EH */
500 spin_unlock_irqrestore(ap->lock, flags);
502 /* invoke EH, skip if unloading or suspended */
503 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
504 ap->ops->error_handler(ap);
508 /* process port suspend request */
509 ata_eh_handle_port_suspend(ap);
511 /* Exception might have happend after ->error_handler
512 * recovered the port but before this point. Repeat
515 spin_lock_irqsave(ap->lock, flags);
517 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
518 if (--ap->eh_tries) {
519 spin_unlock_irqrestore(ap->lock, flags);
522 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
523 "tries, giving up\n", ATA_EH_MAX_TRIES);
524 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
527 /* this run is complete, make sure EH info is clear */
528 __ata_port_for_each_link(link, ap)
529 memset(&link->eh_info, 0, sizeof(link->eh_info));
531 /* Clear host_eh_scheduled while holding ap->lock such
532 * that if exception occurs after this point but
533 * before EH completion, SCSI midlayer will
536 host->host_eh_scheduled = 0;
538 spin_unlock_irqrestore(ap->lock, flags);
540 WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
541 ap->ops->eng_timeout(ap);
544 /* finish or retry handled scmd's and clean up */
545 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
547 scsi_eh_flush_done_q(&ap->eh_done_q);
550 spin_lock_irqsave(ap->lock, flags);
552 if (ap->pflags & ATA_PFLAG_LOADING)
553 ap->pflags &= ~ATA_PFLAG_LOADING;
554 else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
555 queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0);
557 if (ap->pflags & ATA_PFLAG_RECOVERED)
558 ata_port_printk(ap, KERN_INFO, "EH complete\n");
560 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
562 /* tell wait_eh that we're done */
563 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
564 wake_up_all(&ap->eh_wait_q);
566 spin_unlock_irqrestore(ap->lock, flags);
572 * ata_port_wait_eh - Wait for the currently pending EH to complete
573 * @ap: Port to wait EH for
575 * Wait until the currently pending EH is complete.
578 * Kernel thread context (may sleep).
580 void ata_port_wait_eh(struct ata_port *ap)
586 spin_lock_irqsave(ap->lock, flags);
588 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
589 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
590 spin_unlock_irqrestore(ap->lock, flags);
592 spin_lock_irqsave(ap->lock, flags);
594 finish_wait(&ap->eh_wait_q, &wait);
596 spin_unlock_irqrestore(ap->lock, flags);
598 /* make sure SCSI EH is complete */
599 if (scsi_host_in_recovery(ap->scsi_host)) {
605 static int ata_eh_nr_in_flight(struct ata_port *ap)
610 /* count only non-internal commands */
611 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++)
612 if (ata_qc_from_tag(ap, tag))
618 void ata_eh_fastdrain_timerfn(unsigned long arg)
620 struct ata_port *ap = (void *)arg;
624 spin_lock_irqsave(ap->lock, flags);
626 cnt = ata_eh_nr_in_flight(ap);
632 if (cnt == ap->fastdrain_cnt) {
635 /* No progress during the last interval, tag all
636 * in-flight qcs as timed out and freeze the port.
638 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) {
639 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
641 qc->err_mask |= AC_ERR_TIMEOUT;
646 /* some qcs have finished, give it another chance */
647 ap->fastdrain_cnt = cnt;
648 ap->fastdrain_timer.expires =
649 ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
650 add_timer(&ap->fastdrain_timer);
654 spin_unlock_irqrestore(ap->lock, flags);
658 * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
659 * @ap: target ATA port
660 * @fastdrain: activate fast drain
662 * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
663 * is non-zero and EH wasn't pending before. Fast drain ensures
664 * that EH kicks in in timely manner.
667 * spin_lock_irqsave(host lock)
669 static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
673 /* already scheduled? */
674 if (ap->pflags & ATA_PFLAG_EH_PENDING)
677 ap->pflags |= ATA_PFLAG_EH_PENDING;
682 /* do we have in-flight qcs? */
683 cnt = ata_eh_nr_in_flight(ap);
687 /* activate fast drain */
688 ap->fastdrain_cnt = cnt;
689 ap->fastdrain_timer.expires =
690 ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
691 add_timer(&ap->fastdrain_timer);
695 * ata_qc_schedule_eh - schedule qc for error handling
696 * @qc: command to schedule error handling for
698 * Schedule error handling for @qc. EH will kick in as soon as
699 * other commands are drained.
702 * spin_lock_irqsave(host lock)
704 void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
706 struct ata_port *ap = qc->ap;
708 WARN_ON(!ap->ops->error_handler);
710 qc->flags |= ATA_QCFLAG_FAILED;
711 ata_eh_set_pending(ap, 1);
713 /* The following will fail if timeout has already expired.
714 * ata_scsi_error() takes care of such scmds on EH entry.
715 * Note that ATA_QCFLAG_FAILED is unconditionally set after
716 * this function completes.
718 scsi_req_abort_cmd(qc->scsicmd);
722 * ata_port_schedule_eh - schedule error handling without a qc
723 * @ap: ATA port to schedule EH for
725 * Schedule error handling for @ap. EH will kick in as soon as
726 * all commands are drained.
729 * spin_lock_irqsave(host lock)
731 void ata_port_schedule_eh(struct ata_port *ap)
733 WARN_ON(!ap->ops->error_handler);
735 if (ap->pflags & ATA_PFLAG_INITIALIZING)
738 ata_eh_set_pending(ap, 1);
739 scsi_schedule_eh(ap->scsi_host);
741 DPRINTK("port EH scheduled\n");
744 static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
746 int tag, nr_aborted = 0;
748 WARN_ON(!ap->ops->error_handler);
750 /* we're gonna abort all commands, no need for fast drain */
751 ata_eh_set_pending(ap, 0);
753 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
754 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
756 if (qc && (!link || qc->dev->link == link)) {
757 qc->flags |= ATA_QCFLAG_FAILED;
764 ata_port_schedule_eh(ap);
770 * ata_link_abort - abort all qc's on the link
771 * @link: ATA link to abort qc's for
773 * Abort all active qc's active on @link and schedule EH.
776 * spin_lock_irqsave(host lock)
779 * Number of aborted qc's.
781 int ata_link_abort(struct ata_link *link)
783 return ata_do_link_abort(link->ap, link);
787 * ata_port_abort - abort all qc's on the port
788 * @ap: ATA port to abort qc's for
790 * Abort all active qc's of @ap and schedule EH.
793 * spin_lock_irqsave(host_set lock)
796 * Number of aborted qc's.
798 int ata_port_abort(struct ata_port *ap)
800 return ata_do_link_abort(ap, NULL);
804 * __ata_port_freeze - freeze port
805 * @ap: ATA port to freeze
807 * This function is called when HSM violation or some other
808 * condition disrupts normal operation of the port. Frozen port
809 * is not allowed to perform any operation until the port is
810 * thawed, which usually follows a successful reset.
812 * ap->ops->freeze() callback can be used for freezing the port
813 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
814 * port cannot be frozen hardware-wise, the interrupt handler
815 * must ack and clear interrupts unconditionally while the port
819 * spin_lock_irqsave(host lock)
821 static void __ata_port_freeze(struct ata_port *ap)
823 WARN_ON(!ap->ops->error_handler);
828 ap->pflags |= ATA_PFLAG_FROZEN;
830 DPRINTK("ata%u port frozen\n", ap->print_id);
834 * ata_port_freeze - abort & freeze port
835 * @ap: ATA port to freeze
837 * Abort and freeze @ap.
840 * spin_lock_irqsave(host lock)
843 * Number of aborted commands.
845 int ata_port_freeze(struct ata_port *ap)
849 WARN_ON(!ap->ops->error_handler);
851 nr_aborted = ata_port_abort(ap);
852 __ata_port_freeze(ap);
858 * sata_async_notification - SATA async notification handler
859 * @ap: ATA port where async notification is received
861 * Handler to be called when async notification via SDB FIS is
862 * received. This function schedules EH if necessary.
865 * spin_lock_irqsave(host lock)
868 * 1 if EH is scheduled, 0 otherwise.
870 int sata_async_notification(struct ata_port *ap)
875 if (!(ap->flags & ATA_FLAG_AN))
878 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
880 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
882 if (!sata_pmp_attached(ap) || rc) {
883 /* PMP is not attached or SNTF is not available */
884 if (!sata_pmp_attached(ap)) {
885 /* PMP is not attached. Check whether ATAPI
886 * AN is configured. If so, notify media
889 struct ata_device *dev = ap->link.device;
891 if ((dev->class == ATA_DEV_ATAPI) &&
892 (dev->flags & ATA_DFLAG_AN))
893 ata_scsi_media_change_notify(dev);
896 /* PMP is attached but SNTF is not available.
897 * ATAPI async media change notification is
898 * not used. The PMP must be reporting PHY
899 * status change, schedule EH.
901 ata_port_schedule_eh(ap);
905 /* PMP is attached and SNTF is available */
906 struct ata_link *link;
908 /* check and notify ATAPI AN */
909 ata_port_for_each_link(link, ap) {
910 if (!(sntf & (1 << link->pmp)))
913 if ((link->device->class == ATA_DEV_ATAPI) &&
914 (link->device->flags & ATA_DFLAG_AN))
915 ata_scsi_media_change_notify(link->device);
918 /* If PMP is reporting that PHY status of some
919 * downstream ports has changed, schedule EH.
921 if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
922 ata_port_schedule_eh(ap);
931 * ata_eh_freeze_port - EH helper to freeze port
932 * @ap: ATA port to freeze
939 void ata_eh_freeze_port(struct ata_port *ap)
943 if (!ap->ops->error_handler)
946 spin_lock_irqsave(ap->lock, flags);
947 __ata_port_freeze(ap);
948 spin_unlock_irqrestore(ap->lock, flags);
952 * ata_port_thaw_port - EH helper to thaw port
953 * @ap: ATA port to thaw
955 * Thaw frozen port @ap.
960 void ata_eh_thaw_port(struct ata_port *ap)
964 if (!ap->ops->error_handler)
967 spin_lock_irqsave(ap->lock, flags);
969 ap->pflags &= ~ATA_PFLAG_FROZEN;
974 spin_unlock_irqrestore(ap->lock, flags);
976 DPRINTK("ata%u port thawed\n", ap->print_id);
979 static void ata_eh_scsidone(struct scsi_cmnd *scmd)
984 static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
986 struct ata_port *ap = qc->ap;
987 struct scsi_cmnd *scmd = qc->scsicmd;
990 spin_lock_irqsave(ap->lock, flags);
991 qc->scsidone = ata_eh_scsidone;
992 __ata_qc_complete(qc);
993 WARN_ON(ata_tag_valid(qc->tag));
994 spin_unlock_irqrestore(ap->lock, flags);
996 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
1000 * ata_eh_qc_complete - Complete an active ATA command from EH
1001 * @qc: Command to complete
1003 * Indicate to the mid and upper layers that an ATA command has
1004 * completed. To be used from EH.
1006 void ata_eh_qc_complete(struct ata_queued_cmd *qc)
1008 struct scsi_cmnd *scmd = qc->scsicmd;
1009 scmd->retries = scmd->allowed;
1010 __ata_eh_qc_complete(qc);
1014 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
1015 * @qc: Command to retry
1017 * Indicate to the mid and upper layers that an ATA command
1018 * should be retried. To be used from EH.
1020 * SCSI midlayer limits the number of retries to scmd->allowed.
1021 * scmd->retries is decremented for commands which get retried
1022 * due to unrelated failures (qc->err_mask is zero).
1024 void ata_eh_qc_retry(struct ata_queued_cmd *qc)
1026 struct scsi_cmnd *scmd = qc->scsicmd;
1027 if (!qc->err_mask && scmd->retries)
1029 __ata_eh_qc_complete(qc);
1033 * ata_eh_detach_dev - detach ATA device
1034 * @dev: ATA device to detach
1041 void ata_eh_detach_dev(struct ata_device *dev)
1043 struct ata_link *link = dev->link;
1044 struct ata_port *ap = link->ap;
1045 unsigned long flags;
1047 ata_dev_disable(dev);
1049 spin_lock_irqsave(ap->lock, flags);
1051 dev->flags &= ~ATA_DFLAG_DETACH;
1053 if (ata_scsi_offline_dev(dev)) {
1054 dev->flags |= ATA_DFLAG_DETACHED;
1055 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
1058 /* clear per-dev EH actions */
1059 ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
1060 ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
1062 spin_unlock_irqrestore(ap->lock, flags);
1066 * ata_eh_about_to_do - about to perform eh_action
1067 * @link: target ATA link
1068 * @dev: target ATA dev for per-dev action (can be NULL)
1069 * @action: action about to be performed
1071 * Called just before performing EH actions to clear related bits
1072 * in @link->eh_info such that eh actions are not unnecessarily
1078 void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
1079 unsigned int action)
1081 struct ata_port *ap = link->ap;
1082 struct ata_eh_info *ehi = &link->eh_info;
1083 struct ata_eh_context *ehc = &link->eh_context;
1084 unsigned long flags;
1086 spin_lock_irqsave(ap->lock, flags);
1088 ata_eh_clear_action(link, dev, ehi, action);
1090 if (!(ehc->i.flags & ATA_EHI_QUIET))
1091 ap->pflags |= ATA_PFLAG_RECOVERED;
1093 spin_unlock_irqrestore(ap->lock, flags);
1097 * ata_eh_done - EH action complete
1098 * @ap: target ATA port
1099 * @dev: target ATA dev for per-dev action (can be NULL)
1100 * @action: action just completed
1102 * Called right after performing EH actions to clear related bits
1103 * in @link->eh_context.
1108 void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1109 unsigned int action)
1111 struct ata_eh_context *ehc = &link->eh_context;
1113 ata_eh_clear_action(link, dev, &ehc->i, action);
1117 * ata_err_string - convert err_mask to descriptive string
1118 * @err_mask: error mask to convert to string
1120 * Convert @err_mask to descriptive string. Errors are
1121 * prioritized according to severity and only the most severe
1122 * error is reported.
1128 * Descriptive string for @err_mask
1130 static const char *ata_err_string(unsigned int err_mask)
1132 if (err_mask & AC_ERR_HOST_BUS)
1133 return "host bus error";
1134 if (err_mask & AC_ERR_ATA_BUS)
1135 return "ATA bus error";
1136 if (err_mask & AC_ERR_TIMEOUT)
1138 if (err_mask & AC_ERR_HSM)
1139 return "HSM violation";
1140 if (err_mask & AC_ERR_SYSTEM)
1141 return "internal error";
1142 if (err_mask & AC_ERR_MEDIA)
1143 return "media error";
1144 if (err_mask & AC_ERR_INVALID)
1145 return "invalid argument";
1146 if (err_mask & AC_ERR_DEV)
1147 return "device error";
1148 return "unknown error";
1152 * ata_read_log_page - read a specific log page
1153 * @dev: target device
1154 * @page: page to read
1155 * @buf: buffer to store read page
1156 * @sectors: number of sectors to read
1158 * Read log page using READ_LOG_EXT command.
1161 * Kernel thread context (may sleep).
1164 * 0 on success, AC_ERR_* mask otherwise.
1166 static unsigned int ata_read_log_page(struct ata_device *dev,
1167 u8 page, void *buf, unsigned int sectors)
1169 struct ata_taskfile tf;
1170 unsigned int err_mask;
1172 DPRINTK("read log page - page %d\n", page);
1174 ata_tf_init(dev, &tf);
1175 tf.command = ATA_CMD_READ_LOG_EXT;
1178 tf.hob_nsect = sectors >> 8;
1179 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1180 tf.protocol = ATA_PROT_PIO;
1182 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
1183 buf, sectors * ATA_SECT_SIZE, 0);
1185 DPRINTK("EXIT, err_mask=%x\n", err_mask);
1190 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
1191 * @dev: Device to read log page 10h from
1192 * @tag: Resulting tag of the failed command
1193 * @tf: Resulting taskfile registers of the failed command
1195 * Read log page 10h to obtain NCQ error details and clear error
1199 * Kernel thread context (may sleep).
1202 * 0 on success, -errno otherwise.
1204 static int ata_eh_read_log_10h(struct ata_device *dev,
1205 int *tag, struct ata_taskfile *tf)
1207 u8 *buf = dev->link->ap->sector_buf;
1208 unsigned int err_mask;
1212 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
1217 for (i = 0; i < ATA_SECT_SIZE; i++)
1220 ata_dev_printk(dev, KERN_WARNING,
1221 "invalid checksum 0x%x on log page 10h\n", csum);
1226 *tag = buf[0] & 0x1f;
1228 tf->command = buf[2];
1229 tf->feature = buf[3];
1233 tf->device = buf[7];
1234 tf->hob_lbal = buf[8];
1235 tf->hob_lbam = buf[9];
1236 tf->hob_lbah = buf[10];
1237 tf->nsect = buf[12];
1238 tf->hob_nsect = buf[13];
1244 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1245 * @dev: device to perform REQUEST_SENSE to
1246 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
1248 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
1249 * SENSE. This function is EH helper.
1252 * Kernel thread context (may sleep).
1255 * 0 on success, AC_ERR_* mask on failure
1257 static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc)
1259 struct ata_device *dev = qc->dev;
1260 unsigned char *sense_buf = qc->scsicmd->sense_buffer;
1261 struct ata_port *ap = dev->link->ap;
1262 struct ata_taskfile tf;
1263 u8 cdb[ATAPI_CDB_LEN];
1265 DPRINTK("ATAPI request sense\n");
1267 /* FIXME: is this needed? */
1268 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1270 /* initialize sense_buf with the error register,
1271 * for the case where they are -not- overwritten
1273 sense_buf[0] = 0x70;
1274 sense_buf[2] = qc->result_tf.feature >> 4;
1276 /* some devices time out if garbage left in tf */
1277 ata_tf_init(dev, &tf);
1279 memset(cdb, 0, ATAPI_CDB_LEN);
1280 cdb[0] = REQUEST_SENSE;
1281 cdb[4] = SCSI_SENSE_BUFFERSIZE;
1283 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1284 tf.command = ATA_CMD_PACKET;
1286 /* is it pointless to prefer PIO for "safety reasons"? */
1287 if (ap->flags & ATA_FLAG_PIO_DMA) {
1288 tf.protocol = ATAPI_PROT_DMA;
1289 tf.feature |= ATAPI_PKT_DMA;
1291 tf.protocol = ATAPI_PROT_PIO;
1292 tf.lbam = SCSI_SENSE_BUFFERSIZE;
1296 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
1297 sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
1301 * ata_eh_analyze_serror - analyze SError for a failed port
1302 * @link: ATA link to analyze SError for
1304 * Analyze SError if available and further determine cause of
1310 static void ata_eh_analyze_serror(struct ata_link *link)
1312 struct ata_eh_context *ehc = &link->eh_context;
1313 u32 serror = ehc->i.serror;
1314 unsigned int err_mask = 0, action = 0;
1317 if (serror & (SERR_PERSISTENT | SERR_DATA)) {
1318 err_mask |= AC_ERR_ATA_BUS;
1319 action |= ATA_EH_RESET;
1321 if (serror & SERR_PROTOCOL) {
1322 err_mask |= AC_ERR_HSM;
1323 action |= ATA_EH_RESET;
1325 if (serror & SERR_INTERNAL) {
1326 err_mask |= AC_ERR_SYSTEM;
1327 action |= ATA_EH_RESET;
1330 /* Determine whether a hotplug event has occurred. Both
1331 * SError.N/X are considered hotplug events for enabled or
1332 * host links. For disabled PMP links, only N bit is
1333 * considered as X bit is left at 1 for link plugging.
1337 if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
1338 hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
1340 hotplug_mask = SERR_PHYRDY_CHG;
1342 if (serror & hotplug_mask)
1343 ata_ehi_hotplugged(&ehc->i);
1345 ehc->i.err_mask |= err_mask;
1346 ehc->i.action |= action;
1350 * ata_eh_analyze_ncq_error - analyze NCQ error
1351 * @link: ATA link to analyze NCQ error for
1353 * Read log page 10h, determine the offending qc and acquire
1354 * error status TF. For NCQ device errors, all LLDDs have to do
1355 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1359 * Kernel thread context (may sleep).
1361 void ata_eh_analyze_ncq_error(struct ata_link *link)
1363 struct ata_port *ap = link->ap;
1364 struct ata_eh_context *ehc = &link->eh_context;
1365 struct ata_device *dev = link->device;
1366 struct ata_queued_cmd *qc;
1367 struct ata_taskfile tf;
1370 /* if frozen, we can't do much */
1371 if (ap->pflags & ATA_PFLAG_FROZEN)
1374 /* is it NCQ device error? */
1375 if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1378 /* has LLDD analyzed already? */
1379 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1380 qc = __ata_qc_from_tag(ap, tag);
1382 if (!(qc->flags & ATA_QCFLAG_FAILED))
1389 /* okay, this error is ours */
1390 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1392 ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
1393 "(errno=%d)\n", rc);
1397 if (!(link->sactive & (1 << tag))) {
1398 ata_link_printk(link, KERN_ERR, "log page 10h reported "
1399 "inactive tag %d\n", tag);
1403 /* we've got the perpetrator, condemn it */
1404 qc = __ata_qc_from_tag(ap, tag);
1405 memcpy(&qc->result_tf, &tf, sizeof(tf));
1406 qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
1407 qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
1408 ehc->i.err_mask &= ~AC_ERR_DEV;
1412 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1413 * @qc: qc to analyze
1414 * @tf: Taskfile registers to analyze
1416 * Analyze taskfile of @qc and further determine cause of
1417 * failure. This function also requests ATAPI sense data if
1421 * Kernel thread context (may sleep).
1424 * Determined recovery action
1426 static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1427 const struct ata_taskfile *tf)
1429 unsigned int tmp, action = 0;
1430 u8 stat = tf->command, err = tf->feature;
1432 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1433 qc->err_mask |= AC_ERR_HSM;
1434 return ATA_EH_RESET;
1437 if (stat & (ATA_ERR | ATA_DF))
1438 qc->err_mask |= AC_ERR_DEV;
1442 switch (qc->dev->class) {
1445 qc->err_mask |= AC_ERR_ATA_BUS;
1447 qc->err_mask |= AC_ERR_MEDIA;
1449 qc->err_mask |= AC_ERR_INVALID;
1453 if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
1454 tmp = atapi_eh_request_sense(qc);
1456 /* ATA_QCFLAG_SENSE_VALID is used to
1457 * tell atapi_qc_complete() that sense
1458 * data is already valid.
1460 * TODO: interpret sense data and set
1461 * appropriate err_mask.
1463 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1465 qc->err_mask |= tmp;
1469 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1470 action |= ATA_EH_RESET;
1475 static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask,
1480 if (!(eflags & ATA_EFLAG_DUBIOUS_XFER))
1484 base = ATA_ECAT_DUBIOUS_NONE;
1486 if (err_mask & AC_ERR_ATA_BUS)
1487 return base + ATA_ECAT_ATA_BUS;
1489 if (err_mask & AC_ERR_TIMEOUT)
1490 return base + ATA_ECAT_TOUT_HSM;
1492 if (eflags & ATA_EFLAG_IS_IO) {
1493 if (err_mask & AC_ERR_HSM)
1494 return base + ATA_ECAT_TOUT_HSM;
1496 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1497 return base + ATA_ECAT_UNK_DEV;
1503 struct speed_down_verdict_arg {
1506 int nr_errors[ATA_ECAT_NR];
1509 static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
1511 struct speed_down_verdict_arg *arg = void_arg;
1514 if (ent->timestamp < arg->since)
1517 cat = ata_eh_categorize_error(ent->eflags, ent->err_mask,
1519 arg->nr_errors[cat]++;
1525 * ata_eh_speed_down_verdict - Determine speed down verdict
1526 * @dev: Device of interest
1528 * This function examines error ring of @dev and determines
1529 * whether NCQ needs to be turned off, transfer speed should be
1530 * stepped down, or falling back to PIO is necessary.
1532 * ECAT_ATA_BUS : ATA_BUS error for any command
1534 * ECAT_TOUT_HSM : TIMEOUT for any command or HSM violation for
1537 * ECAT_UNK_DEV : Unknown DEV error for IO commands
1539 * ECAT_DUBIOUS_* : Identical to above three but occurred while
1540 * data transfer hasn't been verified.
1544 * NCQ_OFF : Turn off NCQ.
1546 * SPEED_DOWN : Speed down transfer speed but don't fall back
1549 * FALLBACK_TO_PIO : Fall back to PIO.
1551 * Even if multiple verdicts are returned, only one action is
1552 * taken per error. An action triggered by non-DUBIOUS errors
1553 * clears ering, while one triggered by DUBIOUS_* errors doesn't.
1554 * This is to expedite speed down decisions right after device is
1555 * initially configured.
1557 * The followings are speed down rules. #1 and #2 deal with
1560 * 1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors
1561 * occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO.
1563 * 2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors
1564 * occurred during last 5 mins, NCQ_OFF.
1566 * 3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors
1567 * ocurred during last 5 mins, FALLBACK_TO_PIO
1569 * 4. If more than 3 TOUT_HSM or UNK_DEV errors occurred
1570 * during last 10 mins, NCQ_OFF.
1572 * 5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6
1573 * UNK_DEV errors occurred during last 10 mins, SPEED_DOWN.
1576 * Inherited from caller.
1579 * OR of ATA_EH_SPDN_* flags.
1581 static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
1583 const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
1584 u64 j64 = get_jiffies_64();
1585 struct speed_down_verdict_arg arg;
1586 unsigned int verdict = 0;
1588 /* scan past 5 mins of error history */
1589 memset(&arg, 0, sizeof(arg));
1590 arg.since = j64 - min(j64, j5mins);
1591 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1593 if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] +
1594 arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1)
1595 verdict |= ATA_EH_SPDN_SPEED_DOWN |
1596 ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS;
1598 if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] +
1599 arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1)
1600 verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS;
1602 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1603 arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1604 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
1605 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
1607 /* scan past 10 mins of error history */
1608 memset(&arg, 0, sizeof(arg));
1609 arg.since = j64 - min(j64, j10mins);
1610 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1612 if (arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1613 arg.nr_errors[ATA_ECAT_UNK_DEV] > 3)
1614 verdict |= ATA_EH_SPDN_NCQ_OFF;
1616 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1617 arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 ||
1618 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
1619 verdict |= ATA_EH_SPDN_SPEED_DOWN;
1625 * ata_eh_speed_down - record error and speed down if necessary
1626 * @dev: Failed device
1627 * @eflags: mask of ATA_EFLAG_* flags
1628 * @err_mask: err_mask of the error
1630 * Record error and examine error history to determine whether
1631 * adjusting transmission speed is necessary. It also sets
1632 * transmission limits appropriately if such adjustment is
1636 * Kernel thread context (may sleep).
1639 * Determined recovery action.
1641 static unsigned int ata_eh_speed_down(struct ata_device *dev,
1642 unsigned int eflags, unsigned int err_mask)
1644 struct ata_link *link = dev->link;
1646 unsigned int verdict;
1647 unsigned int action = 0;
1649 /* don't bother if Cat-0 error */
1650 if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0)
1653 /* record error and determine whether speed down is necessary */
1654 ata_ering_record(&dev->ering, eflags, err_mask);
1655 verdict = ata_eh_speed_down_verdict(dev);
1658 if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
1659 (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
1660 ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
1661 dev->flags |= ATA_DFLAG_NCQ_OFF;
1662 ata_dev_printk(dev, KERN_WARNING,
1663 "NCQ disabled due to excessive errors\n");
1668 if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1669 /* speed down SATA link speed if possible */
1670 if (sata_down_spd_limit(link) == 0) {
1671 action |= ATA_EH_RESET;
1675 /* lower transfer mode */
1676 if (dev->spdn_cnt < 2) {
1677 static const int dma_dnxfer_sel[] =
1678 { ATA_DNXFER_DMA, ATA_DNXFER_40C };
1679 static const int pio_dnxfer_sel[] =
1680 { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
1683 if (dev->xfer_shift != ATA_SHIFT_PIO)
1684 sel = dma_dnxfer_sel[dev->spdn_cnt];
1686 sel = pio_dnxfer_sel[dev->spdn_cnt];
1690 if (ata_down_xfermask_limit(dev, sel) == 0) {
1691 action |= ATA_EH_RESET;
1697 /* Fall back to PIO? Slowing down to PIO is meaningless for
1698 * SATA ATA devices. Consider it only for PATA and SATAPI.
1700 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
1701 (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) &&
1702 (dev->xfer_shift != ATA_SHIFT_PIO)) {
1703 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
1705 action |= ATA_EH_RESET;
1712 /* device has been slowed down, blow error history */
1713 if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS))
1714 ata_ering_clear(&dev->ering);
1719 * ata_eh_link_autopsy - analyze error and determine recovery action
1720 * @link: host link to perform autopsy on
1722 * Analyze why @link failed and determine which recovery actions
1723 * are needed. This function also sets more detailed AC_ERR_*
1724 * values and fills sense data for ATAPI CHECK SENSE.
1727 * Kernel thread context (may sleep).
1729 static void ata_eh_link_autopsy(struct ata_link *link)
1731 struct ata_port *ap = link->ap;
1732 struct ata_eh_context *ehc = &link->eh_context;
1733 struct ata_device *dev;
1734 unsigned int all_err_mask = 0, eflags = 0;
1741 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1744 /* obtain and analyze SError */
1745 rc = sata_scr_read(link, SCR_ERROR, &serror);
1747 ehc->i.serror |= serror;
1748 ata_eh_analyze_serror(link);
1749 } else if (rc != -EOPNOTSUPP) {
1750 /* SError read failed, force reset and probing */
1751 ehc->i.probe_mask |= ATA_ALL_DEVICES;
1752 ehc->i.action |= ATA_EH_RESET;
1753 ehc->i.err_mask |= AC_ERR_OTHER;
1756 /* analyze NCQ failure */
1757 ata_eh_analyze_ncq_error(link);
1759 /* any real error trumps AC_ERR_OTHER */
1760 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1761 ehc->i.err_mask &= ~AC_ERR_OTHER;
1763 all_err_mask |= ehc->i.err_mask;
1765 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1766 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1768 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
1771 /* inherit upper level err_mask */
1772 qc->err_mask |= ehc->i.err_mask;
1775 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1777 /* DEV errors are probably spurious in case of ATA_BUS error */
1778 if (qc->err_mask & AC_ERR_ATA_BUS)
1779 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1782 /* any real error trumps unknown error */
1783 if (qc->err_mask & ~AC_ERR_OTHER)
1784 qc->err_mask &= ~AC_ERR_OTHER;
1786 /* SENSE_VALID trumps dev/unknown error and revalidation */
1787 if (qc->flags & ATA_QCFLAG_SENSE_VALID)
1788 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1790 /* determine whether the command is worth retrying */
1791 if (!(qc->err_mask & AC_ERR_INVALID) &&
1792 ((qc->flags & ATA_QCFLAG_IO) || qc->err_mask != AC_ERR_DEV))
1793 qc->flags |= ATA_QCFLAG_RETRY;
1795 /* accumulate error info */
1796 ehc->i.dev = qc->dev;
1797 all_err_mask |= qc->err_mask;
1798 if (qc->flags & ATA_QCFLAG_IO)
1799 eflags |= ATA_EFLAG_IS_IO;
1802 /* enforce default EH actions */
1803 if (ap->pflags & ATA_PFLAG_FROZEN ||
1804 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
1805 ehc->i.action |= ATA_EH_RESET;
1806 else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) ||
1807 (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV)))
1808 ehc->i.action |= ATA_EH_REVALIDATE;
1810 /* If we have offending qcs and the associated failed device,
1811 * perform per-dev EH action only on the offending device.
1814 ehc->i.dev_action[ehc->i.dev->devno] |=
1815 ehc->i.action & ATA_EH_PERDEV_MASK;
1816 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
1819 /* propagate timeout to host link */
1820 if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link))
1821 ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT;
1823 /* record error and consider speeding down */
1825 if (!dev && ((ata_link_max_devices(link) == 1 &&
1826 ata_dev_enabled(link->device))))
1830 if (dev->flags & ATA_DFLAG_DUBIOUS_XFER)
1831 eflags |= ATA_EFLAG_DUBIOUS_XFER;
1832 ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask);
1839 * ata_eh_autopsy - analyze error and determine recovery action
1840 * @ap: host port to perform autopsy on
1842 * Analyze all links of @ap and determine why they failed and
1843 * which recovery actions are needed.
1846 * Kernel thread context (may sleep).
1848 void ata_eh_autopsy(struct ata_port *ap)
1850 struct ata_link *link;
1852 ata_port_for_each_link(link, ap)
1853 ata_eh_link_autopsy(link);
1855 /* Autopsy of fanout ports can affect host link autopsy.
1856 * Perform host link autopsy last.
1858 if (sata_pmp_attached(ap))
1859 ata_eh_link_autopsy(&ap->link);
1863 * ata_eh_link_report - report error handling to user
1864 * @link: ATA link EH is going on
1866 * Report EH to user.
1871 static void ata_eh_link_report(struct ata_link *link)
1873 struct ata_port *ap = link->ap;
1874 struct ata_eh_context *ehc = &link->eh_context;
1875 const char *frozen, *desc;
1877 int tag, nr_failed = 0;
1879 if (ehc->i.flags & ATA_EHI_QUIET)
1883 if (ehc->i.desc[0] != '\0')
1886 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1887 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1889 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link ||
1890 ((qc->flags & ATA_QCFLAG_QUIET) &&
1891 qc->err_mask == AC_ERR_DEV))
1893 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1899 if (!nr_failed && !ehc->i.err_mask)
1903 if (ap->pflags & ATA_PFLAG_FROZEN)
1906 memset(tries_buf, 0, sizeof(tries_buf));
1907 if (ap->eh_tries < ATA_EH_MAX_TRIES)
1908 snprintf(tries_buf, sizeof(tries_buf) - 1, " t%d",
1912 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1913 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1914 ehc->i.err_mask, link->sactive, ehc->i.serror,
1915 ehc->i.action, frozen, tries_buf);
1917 ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
1919 ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
1920 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1921 ehc->i.err_mask, link->sactive, ehc->i.serror,
1922 ehc->i.action, frozen, tries_buf);
1924 ata_link_printk(link, KERN_ERR, "%s\n", desc);
1928 ata_port_printk(ap, KERN_ERR,
1929 "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
1930 ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
1931 ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
1932 ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
1933 ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
1934 ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
1935 ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
1936 ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
1937 ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
1938 ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
1939 ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
1940 ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
1941 ehc->i.serror & SERR_CRC ? "BadCRC " : "",
1942 ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
1943 ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
1944 ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
1945 ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
1946 ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
1948 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1949 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1950 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
1951 const u8 *cdb = qc->cdb;
1952 char data_buf[20] = "";
1953 char cdb_buf[70] = "";
1955 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
1956 qc->dev->link != link || !qc->err_mask)
1959 if (qc->dma_dir != DMA_NONE) {
1960 static const char *dma_str[] = {
1961 [DMA_BIDIRECTIONAL] = "bidi",
1962 [DMA_TO_DEVICE] = "out",
1963 [DMA_FROM_DEVICE] = "in",
1965 static const char *prot_str[] = {
1966 [ATA_PROT_PIO] = "pio",
1967 [ATA_PROT_DMA] = "dma",
1968 [ATA_PROT_NCQ] = "ncq",
1969 [ATAPI_PROT_PIO] = "pio",
1970 [ATAPI_PROT_DMA] = "dma",
1973 snprintf(data_buf, sizeof(data_buf), " %s %u %s",
1974 prot_str[qc->tf.protocol], qc->nbytes,
1975 dma_str[qc->dma_dir]);
1978 if (ata_is_atapi(qc->tf.protocol))
1979 snprintf(cdb_buf, sizeof(cdb_buf),
1980 "cdb %02x %02x %02x %02x %02x %02x %02x %02x "
1981 "%02x %02x %02x %02x %02x %02x %02x %02x\n ",
1982 cdb[0], cdb[1], cdb[2], cdb[3],
1983 cdb[4], cdb[5], cdb[6], cdb[7],
1984 cdb[8], cdb[9], cdb[10], cdb[11],
1985 cdb[12], cdb[13], cdb[14], cdb[15]);
1987 ata_dev_printk(qc->dev, KERN_ERR,
1988 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
1990 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
1991 "Emask 0x%x (%s)%s\n",
1992 cmd->command, cmd->feature, cmd->nsect,
1993 cmd->lbal, cmd->lbam, cmd->lbah,
1994 cmd->hob_feature, cmd->hob_nsect,
1995 cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
1996 cmd->device, qc->tag, data_buf, cdb_buf,
1997 res->command, res->feature, res->nsect,
1998 res->lbal, res->lbam, res->lbah,
1999 res->hob_feature, res->hob_nsect,
2000 res->hob_lbal, res->hob_lbam, res->hob_lbah,
2001 res->device, qc->err_mask, ata_err_string(qc->err_mask),
2002 qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
2004 if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
2006 if (res->command & ATA_BUSY)
2007 ata_dev_printk(qc->dev, KERN_ERR,
2008 "status: { Busy }\n");
2010 ata_dev_printk(qc->dev, KERN_ERR,
2011 "status: { %s%s%s%s}\n",
2012 res->command & ATA_DRDY ? "DRDY " : "",
2013 res->command & ATA_DF ? "DF " : "",
2014 res->command & ATA_DRQ ? "DRQ " : "",
2015 res->command & ATA_ERR ? "ERR " : "");
2018 if (cmd->command != ATA_CMD_PACKET &&
2019 (res->feature & (ATA_ICRC | ATA_UNC | ATA_IDNF |
2021 ata_dev_printk(qc->dev, KERN_ERR,
2022 "error: { %s%s%s%s}\n",
2023 res->feature & ATA_ICRC ? "ICRC " : "",
2024 res->feature & ATA_UNC ? "UNC " : "",
2025 res->feature & ATA_IDNF ? "IDNF " : "",
2026 res->feature & ATA_ABORTED ? "ABRT " : "");
2031 * ata_eh_report - report error handling to user
2032 * @ap: ATA port to report EH about
2034 * Report EH to user.
2039 void ata_eh_report(struct ata_port *ap)
2041 struct ata_link *link;
2043 __ata_port_for_each_link(link, ap)
2044 ata_eh_link_report(link);
2047 static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
2048 unsigned int *classes, unsigned long deadline)
2050 struct ata_device *dev;
2052 ata_link_for_each_dev(dev, link)
2053 classes[dev->devno] = ATA_DEV_UNKNOWN;
2055 return reset(link, classes, deadline);
2058 static int ata_eh_followup_srst_needed(struct ata_link *link,
2059 int rc, int classify,
2060 const unsigned int *classes)
2062 if ((link->flags & ATA_LFLAG_NO_SRST) || ata_link_offline(link))
2064 if (rc == -EAGAIN) {
2071 if (sata_pmp_supported(link->ap) && ata_is_host_link(link))
2076 int ata_eh_reset(struct ata_link *link, int classify,
2077 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
2078 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
2080 const int max_tries = ARRAY_SIZE(ata_eh_reset_timeouts);
2081 struct ata_port *ap = link->ap;
2082 struct ata_eh_context *ehc = &link->eh_context;
2083 unsigned int *classes = ehc->classes;
2084 unsigned int lflags = link->flags;
2085 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
2087 struct ata_device *dev;
2088 unsigned long deadline, now;
2089 ata_reset_fn_t reset;
2090 unsigned long flags;
2098 deadline = ata_deadline(ehc->last_reset, ATA_EH_RESET_COOL_DOWN);
2099 if (time_before(now, deadline))
2100 schedule_timeout_uninterruptible(deadline - now);
2102 spin_lock_irqsave(ap->lock, flags);
2103 ap->pflags |= ATA_PFLAG_RESETTING;
2104 spin_unlock_irqrestore(ap->lock, flags);
2106 ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
2107 ehc->last_reset = jiffies;
2109 ata_link_for_each_dev(dev, link) {
2110 /* If we issue an SRST then an ATA drive (not ATAPI)
2111 * may change configuration and be in PIO0 timing. If
2112 * we do a hard reset (or are coming from power on)
2113 * this is true for ATA or ATAPI. Until we've set a
2114 * suitable controller mode we should not touch the
2115 * bus as we may be talking too fast.
2117 dev->pio_mode = XFER_PIO_0;
2119 /* If the controller has a pio mode setup function
2120 * then use it to set the chipset to rights. Don't
2121 * touch the DMA setup as that will be dealt with when
2122 * configuring devices.
2124 if (ap->ops->set_piomode)
2125 ap->ops->set_piomode(ap, dev);
2128 /* prefer hardreset */
2130 ehc->i.action &= ~ATA_EH_RESET;
2133 ehc->i.action = ATA_EH_HARDRESET;
2134 } else if (softreset) {
2136 ehc->i.action = ATA_EH_SOFTRESET;
2141 ata_deadline(jiffies, ATA_EH_PRERESET_TIMEOUT));
2143 if (rc == -ENOENT) {
2144 ata_link_printk(link, KERN_DEBUG,
2145 "port disabled. ignoring.\n");
2146 ehc->i.action &= ~ATA_EH_RESET;
2148 ata_link_for_each_dev(dev, link)
2149 classes[dev->devno] = ATA_DEV_NONE;
2153 ata_link_printk(link, KERN_ERR,
2154 "prereset failed (errno=%d)\n", rc);
2158 /* prereset() might have cleared ATA_EH_RESET. If so,
2159 * bang classes and return.
2161 if (reset && !(ehc->i.action & ATA_EH_RESET)) {
2162 ata_link_for_each_dev(dev, link)
2163 classes[dev->devno] = ATA_DEV_NONE;
2173 ehc->last_reset = jiffies;
2174 if (ata_is_host_link(link))
2175 ata_eh_freeze_port(ap);
2177 deadline = ata_deadline(jiffies, ata_eh_reset_timeouts[try++]);
2181 ata_link_printk(link, KERN_INFO, "%s resetting link\n",
2182 reset == softreset ? "soft" : "hard");
2184 /* mark that this EH session started with reset */
2185 if (reset == hardreset)
2186 ehc->i.flags |= ATA_EHI_DID_HARDRESET;
2188 ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
2190 rc = ata_do_reset(link, reset, classes, deadline);
2192 if (reset == hardreset &&
2193 ata_eh_followup_srst_needed(link, rc, classify, classes)) {
2194 /* okay, let's do follow-up softreset */
2198 ata_link_printk(link, KERN_ERR,
2199 "follow-up softreset required "
2200 "but no softreset avaliable\n");
2205 ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
2206 rc = ata_do_reset(link, reset, classes, deadline);
2209 /* -EAGAIN can happen if we skipped followup SRST */
2210 if (rc && rc != -EAGAIN)
2214 ata_link_printk(link, KERN_INFO, "no reset method "
2215 "available, skipping reset\n");
2216 if (!(lflags & ATA_LFLAG_ASSUME_CLASS))
2217 lflags |= ATA_LFLAG_ASSUME_ATA;
2221 * Post-reset processing
2223 ata_link_for_each_dev(dev, link) {
2224 /* After the reset, the device state is PIO 0 and the
2225 * controller state is undefined. Reset also wakes up
2226 * drives from sleeping mode.
2228 dev->pio_mode = XFER_PIO_0;
2229 dev->flags &= ~ATA_DFLAG_SLEEPING;
2231 if (ata_link_offline(link))
2234 /* apply class override */
2235 if (lflags & ATA_LFLAG_ASSUME_ATA)
2236 classes[dev->devno] = ATA_DEV_ATA;
2237 else if (lflags & ATA_LFLAG_ASSUME_SEMB)
2238 classes[dev->devno] = ATA_DEV_SEMB_UNSUP; /* not yet */
2241 /* record current link speed */
2242 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2243 link->sata_spd = (sstatus >> 4) & 0xf;
2246 if (ata_is_host_link(link))
2247 ata_eh_thaw_port(ap);
2249 /* postreset() should clear hardware SError. Although SError
2250 * is cleared during link resume, clearing SError here is
2251 * necessary as some PHYs raise hotplug events after SRST.
2252 * This introduces race condition where hotplug occurs between
2253 * reset and here. This race is mediated by cross checking
2254 * link onlineness and classification result later.
2257 postreset(link, classes);
2259 /* clear cached SError */
2260 spin_lock_irqsave(link->ap->lock, flags);
2261 link->eh_info.serror = 0;
2262 spin_unlock_irqrestore(link->ap->lock, flags);
2264 /* Make sure onlineness and classification result correspond.
2265 * Hotplug could have happened during reset and some
2266 * controllers fail to wait while a drive is spinning up after
2267 * being hotplugged causing misdetection. By cross checking
2268 * link onlineness and classification result, those conditions
2269 * can be reliably detected and retried.
2272 ata_link_for_each_dev(dev, link) {
2273 /* convert all ATA_DEV_UNKNOWN to ATA_DEV_NONE */
2274 if (classes[dev->devno] == ATA_DEV_UNKNOWN)
2275 classes[dev->devno] = ATA_DEV_NONE;
2280 if (classify && !nr_known && ata_link_online(link)) {
2281 if (try < max_tries) {
2282 ata_link_printk(link, KERN_WARNING, "link online but "
2283 "device misclassified, retrying\n");
2287 ata_link_printk(link, KERN_WARNING,
2288 "link online but device misclassified, "
2289 "device detection might fail\n");
2292 /* reset successful, schedule revalidation */
2293 ata_eh_done(link, NULL, ATA_EH_RESET);
2294 ehc->last_reset = jiffies;
2295 ehc->i.action |= ATA_EH_REVALIDATE;
2299 /* clear hotplug flag */
2300 ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2302 spin_lock_irqsave(ap->lock, flags);
2303 ap->pflags &= ~ATA_PFLAG_RESETTING;
2304 spin_unlock_irqrestore(ap->lock, flags);
2309 /* if SCR isn't accessible on a fan-out port, PMP needs to be reset */
2310 if (!ata_is_host_link(link) &&
2311 sata_scr_read(link, SCR_STATUS, &sstatus))
2314 if (rc == -ERESTART || try >= max_tries)
2318 if (time_before(now, deadline)) {
2319 unsigned long delta = deadline - now;
2321 ata_link_printk(link, KERN_WARNING,
2322 "reset failed (errno=%d), retrying in %u secs\n",
2323 rc, DIV_ROUND_UP(jiffies_to_msecs(delta), 1000));
2326 delta = schedule_timeout_uninterruptible(delta);
2329 if (rc == -EPIPE || try == max_tries - 1)
2330 sata_down_spd_limit(link);
2336 static int ata_eh_revalidate_and_attach(struct ata_link *link,
2337 struct ata_device **r_failed_dev)
2339 struct ata_port *ap = link->ap;
2340 struct ata_eh_context *ehc = &link->eh_context;
2341 struct ata_device *dev;
2342 unsigned int new_mask = 0;
2343 unsigned long flags;
2348 /* For PATA drive side cable detection to work, IDENTIFY must
2349 * be done backwards such that PDIAG- is released by the slave
2350 * device before the master device is identified.
2352 ata_link_for_each_dev_reverse(dev, link) {
2353 unsigned int action = ata_eh_dev_action(dev);
2354 unsigned int readid_flags = 0;
2356 if (ehc->i.flags & ATA_EHI_DID_RESET)
2357 readid_flags |= ATA_READID_POSTRESET;
2359 if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
2360 WARN_ON(dev->class == ATA_DEV_PMP);
2362 if (ata_link_offline(link)) {
2367 ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
2368 rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
2373 ata_eh_done(link, dev, ATA_EH_REVALIDATE);
2375 /* Configuration may have changed, reconfigure
2378 ehc->i.flags |= ATA_EHI_SETMODE;
2380 /* schedule the scsi_rescan_device() here */
2381 queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
2382 } else if (dev->class == ATA_DEV_UNKNOWN &&
2383 ehc->tries[dev->devno] &&
2384 ata_class_enabled(ehc->classes[dev->devno])) {
2385 dev->class = ehc->classes[dev->devno];
2387 if (dev->class == ATA_DEV_PMP)
2388 rc = sata_pmp_attach(dev);
2390 rc = ata_dev_read_id(dev, &dev->class,
2391 readid_flags, dev->id);
2394 new_mask |= 1 << dev->devno;
2397 /* IDENTIFY was issued to non-existent
2398 * device. No need to reset. Just
2399 * thaw and kill the device.
2401 ata_eh_thaw_port(ap);
2402 dev->class = ATA_DEV_UNKNOWN;
2405 dev->class = ATA_DEV_UNKNOWN;
2411 /* PDIAG- should have been released, ask cable type if post-reset */
2412 if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) {
2413 if (ap->ops->cable_detect)
2414 ap->cbl = ap->ops->cable_detect(ap);
2418 /* Configure new devices forward such that user doesn't see
2419 * device detection messages backwards.
2421 ata_link_for_each_dev(dev, link) {
2422 if (!(new_mask & (1 << dev->devno)) ||
2423 dev->class == ATA_DEV_PMP)
2426 ehc->i.flags |= ATA_EHI_PRINTINFO;
2427 rc = ata_dev_configure(dev);
2428 ehc->i.flags &= ~ATA_EHI_PRINTINFO;
2432 spin_lock_irqsave(ap->lock, flags);
2433 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
2434 spin_unlock_irqrestore(ap->lock, flags);
2436 /* new device discovered, configure xfermode */
2437 ehc->i.flags |= ATA_EHI_SETMODE;
2443 *r_failed_dev = dev;
2444 DPRINTK("EXIT rc=%d\n", rc);
2449 * ata_set_mode - Program timings and issue SET FEATURES - XFER
2450 * @link: link on which timings will be programmed
2451 * @r_failed_dev: out paramter for failed device
2453 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
2454 * ata_set_mode() fails, pointer to the failing device is
2455 * returned in @r_failed_dev.
2458 * PCI/etc. bus probe sem.
2461 * 0 on success, negative errno otherwise
2463 int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
2465 struct ata_port *ap = link->ap;
2466 struct ata_device *dev;
2469 /* if data transfer is verified, clear DUBIOUS_XFER on ering top */
2470 ata_link_for_each_dev(dev, link) {
2471 if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
2472 struct ata_ering_entry *ent;
2474 ent = ata_ering_top(&dev->ering);
2476 ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER;
2480 /* has private set_mode? */
2481 if (ap->ops->set_mode)
2482 rc = ap->ops->set_mode(link, r_failed_dev);
2484 rc = ata_do_set_mode(link, r_failed_dev);
2486 /* if transfer mode has changed, set DUBIOUS_XFER on device */
2487 ata_link_for_each_dev(dev, link) {
2488 struct ata_eh_context *ehc = &link->eh_context;
2489 u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
2490 u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
2492 if (dev->xfer_mode != saved_xfer_mode ||
2493 ata_ncq_enabled(dev) != saved_ncq)
2494 dev->flags |= ATA_DFLAG_DUBIOUS_XFER;
2500 static int ata_link_nr_enabled(struct ata_link *link)
2502 struct ata_device *dev;
2505 ata_link_for_each_dev(dev, link)
2506 if (ata_dev_enabled(dev))
2511 static int ata_link_nr_vacant(struct ata_link *link)
2513 struct ata_device *dev;
2516 ata_link_for_each_dev(dev, link)
2517 if (dev->class == ATA_DEV_UNKNOWN)
2522 static int ata_eh_skip_recovery(struct ata_link *link)
2524 struct ata_port *ap = link->ap;
2525 struct ata_eh_context *ehc = &link->eh_context;
2526 struct ata_device *dev;
2528 /* skip disabled links */
2529 if (link->flags & ATA_LFLAG_DISABLED)
2532 /* thaw frozen port and recover failed devices */
2533 if ((ap->pflags & ATA_PFLAG_FROZEN) || ata_link_nr_enabled(link))
2536 /* reset at least once if reset is requested */
2537 if ((ehc->i.action & ATA_EH_RESET) &&
2538 !(ehc->i.flags & ATA_EHI_DID_RESET))
2541 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
2542 ata_link_for_each_dev(dev, link) {
2543 if (dev->class == ATA_DEV_UNKNOWN &&
2544 ehc->classes[dev->devno] != ATA_DEV_NONE)
2551 static int ata_eh_schedule_probe(struct ata_device *dev)
2553 struct ata_eh_context *ehc = &dev->link->eh_context;
2555 if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
2556 (ehc->did_probe_mask & (1 << dev->devno)))
2559 ata_eh_detach_dev(dev);
2561 ehc->did_probe_mask |= (1 << dev->devno);
2562 ehc->i.action |= ATA_EH_RESET;
2563 ehc->saved_xfer_mode[dev->devno] = 0;
2564 ehc->saved_ncq_enabled &= ~(1 << dev->devno);
2569 static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
2571 struct ata_eh_context *ehc = &dev->link->eh_context;
2573 ehc->tries[dev->devno]--;
2577 /* device missing or wrong IDENTIFY data, schedule probing */
2578 ehc->i.probe_mask |= (1 << dev->devno);
2580 /* give it just one more chance */
2581 ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
2583 if (ehc->tries[dev->devno] == 1 && dev->pio_mode > XFER_PIO_0) {
2584 /* This is the last chance, better to slow
2585 * down than lose it.
2587 sata_down_spd_limit(dev->link);
2588 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2592 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2593 /* disable device if it has used up all its chances */
2594 ata_dev_disable(dev);
2596 /* detach if offline */
2597 if (ata_link_offline(dev->link))
2598 ata_eh_detach_dev(dev);
2600 /* schedule probe if necessary */
2601 if (ata_eh_schedule_probe(dev))
2602 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2606 ehc->i.action |= ATA_EH_RESET;
2612 * ata_eh_recover - recover host port after error
2613 * @ap: host port to recover
2614 * @prereset: prereset method (can be NULL)
2615 * @softreset: softreset method (can be NULL)
2616 * @hardreset: hardreset method (can be NULL)
2617 * @postreset: postreset method (can be NULL)
2618 * @r_failed_link: out parameter for failed link
2620 * This is the alpha and omega, eum and yang, heart and soul of
2621 * libata exception handling. On entry, actions required to
2622 * recover each link and hotplug requests are recorded in the
2623 * link's eh_context. This function executes all the operations
2624 * with appropriate retrials and fallbacks to resurrect failed
2625 * devices, detach goners and greet newcomers.
2628 * Kernel thread context (may sleep).
2631 * 0 on success, -errno on failure.
2633 int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
2634 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2635 ata_postreset_fn_t postreset,
2636 struct ata_link **r_failed_link)
2638 struct ata_link *link;
2639 struct ata_device *dev;
2642 unsigned long flags;
2646 /* prep for recovery */
2647 ata_port_for_each_link(link, ap) {
2648 struct ata_eh_context *ehc = &link->eh_context;
2650 /* re-enable link? */
2651 if (ehc->i.action & ATA_EH_ENABLE_LINK) {
2652 ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
2653 spin_lock_irqsave(ap->lock, flags);
2654 link->flags &= ~ATA_LFLAG_DISABLED;
2655 spin_unlock_irqrestore(ap->lock, flags);
2656 ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
2659 ata_link_for_each_dev(dev, link) {
2660 if (link->flags & ATA_LFLAG_NO_RETRY)
2661 ehc->tries[dev->devno] = 1;
2663 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2665 /* collect port action mask recorded in dev actions */
2666 ehc->i.action |= ehc->i.dev_action[dev->devno] &
2667 ~ATA_EH_PERDEV_MASK;
2668 ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
2670 /* process hotplug request */
2671 if (dev->flags & ATA_DFLAG_DETACH)
2672 ata_eh_detach_dev(dev);
2674 /* schedule probe if necessary */
2675 if (!ata_dev_enabled(dev))
2676 ata_eh_schedule_probe(dev);
2684 /* if UNLOADING, finish immediately */
2685 if (ap->pflags & ATA_PFLAG_UNLOADING)
2689 ata_port_for_each_link(link, ap) {
2690 struct ata_eh_context *ehc = &link->eh_context;
2692 /* skip EH if possible. */
2693 if (ata_eh_skip_recovery(link))
2696 ata_link_for_each_dev(dev, link)
2697 ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
2701 ata_port_for_each_link(link, ap) {
2702 struct ata_eh_context *ehc = &link->eh_context;
2704 if (!(ehc->i.action & ATA_EH_RESET))
2707 rc = ata_eh_reset(link, ata_link_nr_vacant(link),
2708 prereset, softreset, hardreset, postreset);
2710 ata_link_printk(link, KERN_ERR,
2711 "reset failed, giving up\n");
2717 ata_port_for_each_link(link, ap) {
2718 struct ata_eh_context *ehc = &link->eh_context;
2720 /* revalidate existing devices and attach new ones */
2721 rc = ata_eh_revalidate_and_attach(link, &dev);
2725 /* if PMP got attached, return, pmp EH will take care of it */
2726 if (link->device->class == ATA_DEV_PMP) {
2731 /* configure transfer mode if necessary */
2732 if (ehc->i.flags & ATA_EHI_SETMODE) {
2733 rc = ata_set_mode(link, &dev);
2736 ehc->i.flags &= ~ATA_EHI_SETMODE;
2739 if (ehc->i.action & ATA_EH_LPM)
2740 ata_link_for_each_dev(dev, link)
2741 ata_dev_enable_pm(dev, ap->pm_policy);
2743 /* this link is okay now */
2749 ata_eh_handle_dev_fail(dev, rc);
2751 if (ap->pflags & ATA_PFLAG_FROZEN) {
2752 /* PMP reset requires working host port.
2753 * Can't retry if it's frozen.
2755 if (sata_pmp_attached(ap))
2765 if (rc && r_failed_link)
2766 *r_failed_link = link;
2768 DPRINTK("EXIT, rc=%d\n", rc);
2773 * ata_eh_finish - finish up EH
2774 * @ap: host port to finish EH for
2776 * Recovery is complete. Clean up EH states and retry or finish
2782 void ata_eh_finish(struct ata_port *ap)
2786 /* retry or finish qcs */
2787 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2788 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2790 if (!(qc->flags & ATA_QCFLAG_FAILED))
2794 /* FIXME: Once EH migration is complete,
2795 * generate sense data in this function,
2796 * considering both err_mask and tf.
2798 if (qc->flags & ATA_QCFLAG_RETRY)
2799 ata_eh_qc_retry(qc);
2801 ata_eh_qc_complete(qc);
2803 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2804 ata_eh_qc_complete(qc);
2806 /* feed zero TF to sense generation */
2807 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2808 ata_eh_qc_retry(qc);
2813 /* make sure nr_active_links is zero after EH */
2814 WARN_ON(ap->nr_active_links);
2815 ap->nr_active_links = 0;
2819 * ata_do_eh - do standard error handling
2820 * @ap: host port to handle error for
2822 * @prereset: prereset method (can be NULL)
2823 * @softreset: softreset method (can be NULL)
2824 * @hardreset: hardreset method (can be NULL)
2825 * @postreset: postreset method (can be NULL)
2827 * Perform standard error handling sequence.
2830 * Kernel thread context (may sleep).
2832 void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2833 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2834 ata_postreset_fn_t postreset)
2836 struct ata_device *dev;
2842 rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
2845 ata_link_for_each_dev(dev, &ap->link)
2846 ata_dev_disable(dev);
2853 * ata_std_error_handler - standard error handler
2854 * @ap: host port to handle error for
2856 * Standard error handler
2859 * Kernel thread context (may sleep).
2861 void ata_std_error_handler(struct ata_port *ap)
2863 struct ata_port_operations *ops = ap->ops;
2864 ata_reset_fn_t hardreset = ops->hardreset;
2866 /* ignore built-in hardreset if SCR access is not available */
2867 if (ata_is_builtin_hardreset(hardreset) && !sata_scr_valid(&ap->link))
2870 ata_do_eh(ap, ops->prereset, ops->softreset, hardreset, ops->postreset);
2875 * ata_eh_handle_port_suspend - perform port suspend operation
2876 * @ap: port to suspend
2881 * Kernel thread context (may sleep).
2883 static void ata_eh_handle_port_suspend(struct ata_port *ap)
2885 unsigned long flags;
2888 /* are we suspending? */
2889 spin_lock_irqsave(ap->lock, flags);
2890 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2891 ap->pm_mesg.event == PM_EVENT_ON) {
2892 spin_unlock_irqrestore(ap->lock, flags);
2895 spin_unlock_irqrestore(ap->lock, flags);
2897 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
2899 /* tell ACPI we're suspending */
2900 rc = ata_acpi_on_suspend(ap);
2905 ata_eh_freeze_port(ap);
2907 if (ap->ops->port_suspend)
2908 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
2910 ata_acpi_set_state(ap, PMSG_SUSPEND);
2913 spin_lock_irqsave(ap->lock, flags);
2915 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
2917 ap->pflags |= ATA_PFLAG_SUSPENDED;
2918 else if (ap->pflags & ATA_PFLAG_FROZEN)
2919 ata_port_schedule_eh(ap);
2921 if (ap->pm_result) {
2922 *ap->pm_result = rc;
2923 ap->pm_result = NULL;
2926 spin_unlock_irqrestore(ap->lock, flags);
2932 * ata_eh_handle_port_resume - perform port resume operation
2933 * @ap: port to resume
2938 * Kernel thread context (may sleep).
2940 static void ata_eh_handle_port_resume(struct ata_port *ap)
2942 unsigned long flags;
2945 /* are we resuming? */
2946 spin_lock_irqsave(ap->lock, flags);
2947 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2948 ap->pm_mesg.event != PM_EVENT_ON) {
2949 spin_unlock_irqrestore(ap->lock, flags);
2952 spin_unlock_irqrestore(ap->lock, flags);
2954 WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
2956 ata_acpi_set_state(ap, PMSG_ON);
2958 if (ap->ops->port_resume)
2959 rc = ap->ops->port_resume(ap);
2961 /* tell ACPI that we're resuming */
2962 ata_acpi_on_resume(ap);
2965 spin_lock_irqsave(ap->lock, flags);
2966 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
2967 if (ap->pm_result) {
2968 *ap->pm_result = rc;
2969 ap->pm_result = NULL;
2971 spin_unlock_irqrestore(ap->lock, flags);
2973 #endif /* CONFIG_PM */