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_ATA_BUS = 4,
65 ATA_ECAT_DUBIOUS_TOUT_HSM = 5,
66 ATA_ECAT_DUBIOUS_UNK_DEV = 6,
69 ATA_ECAT_DUBIOUS_BASE = ATA_ECAT_DUBIOUS_ATA_BUS,
72 /* Waiting in ->prereset can never be reliable. It's sometimes nice
73 * to wait there but it can't be depended upon; otherwise, we wouldn't
74 * be resetting. Just give it enough time for most drives to spin up.
77 ATA_EH_PRERESET_TIMEOUT = 10 * HZ,
78 ATA_EH_FASTDRAIN_INTERVAL = 3 * HZ,
81 /* The following table determines how we sequence resets. Each entry
82 * represents timeout for that try. The first try can be soft or
83 * hardreset. All others are hardreset if available. In most cases
84 * the first reset w/ 10sec timeout should succeed. Following entries
85 * are mostly for error handling, hotplug and retarded devices.
87 static const unsigned long ata_eh_reset_timeouts[] = {
88 10 * HZ, /* most drives spin up by 10sec */
89 10 * HZ, /* > 99% working drives spin up before 20sec */
90 35 * HZ, /* give > 30 secs of idleness for retarded devices */
91 5 * HZ, /* and sweet one last chance */
92 /* > 1 min has elapsed, give up */
95 static void __ata_port_freeze(struct ata_port *ap);
97 static void ata_eh_handle_port_suspend(struct ata_port *ap);
98 static void ata_eh_handle_port_resume(struct ata_port *ap);
100 static void ata_eh_handle_port_suspend(struct ata_port *ap)
103 static void ata_eh_handle_port_resume(struct ata_port *ap)
105 #endif /* CONFIG_PM */
107 static void __ata_ehi_pushv_desc(struct ata_eh_info *ehi, const char *fmt,
110 ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
111 ATA_EH_DESC_LEN - ehi->desc_len,
116 * __ata_ehi_push_desc - push error description without adding separator
118 * @fmt: printf format string
120 * Format string according to @fmt and append it to @ehi->desc.
123 * spin_lock_irqsave(host lock)
125 void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
130 __ata_ehi_pushv_desc(ehi, fmt, args);
135 * ata_ehi_push_desc - push error description with separator
137 * @fmt: printf format string
139 * Format string according to @fmt and append it to @ehi->desc.
140 * If @ehi->desc is not empty, ", " is added in-between.
143 * spin_lock_irqsave(host lock)
145 void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
150 __ata_ehi_push_desc(ehi, ", ");
153 __ata_ehi_pushv_desc(ehi, fmt, args);
158 * ata_ehi_clear_desc - clean error description
164 * spin_lock_irqsave(host lock)
166 void ata_ehi_clear_desc(struct ata_eh_info *ehi)
173 * ata_port_desc - append port description
174 * @ap: target ATA port
175 * @fmt: printf format string
177 * Format string according to @fmt and append it to port
178 * description. If port description is not empty, " " is added
179 * in-between. This function is to be used while initializing
180 * ata_host. The description is printed on host registration.
185 void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
189 WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
191 if (ap->link.eh_info.desc_len)
192 __ata_ehi_push_desc(&ap->link.eh_info, " ");
195 __ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
202 * ata_port_pbar_desc - append PCI BAR description
203 * @ap: target ATA port
204 * @bar: target PCI BAR
205 * @offset: offset into PCI BAR
206 * @name: name of the area
208 * If @offset is negative, this function formats a string which
209 * contains the name, address, size and type of the BAR and
210 * appends it to the port description. If @offset is zero or
211 * positive, only name and offsetted address is appended.
216 void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
219 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
221 unsigned long long start, len;
223 if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
225 else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
228 start = (unsigned long long)pci_resource_start(pdev, bar);
229 len = (unsigned long long)pci_resource_len(pdev, bar);
232 ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
234 ata_port_desc(ap, "%s 0x%llx", name,
235 start + (unsigned long long)offset);
238 #endif /* CONFIG_PCI */
240 static void ata_ering_record(struct ata_ering *ering, unsigned int eflags,
241 unsigned int err_mask)
243 struct ata_ering_entry *ent;
248 ering->cursor %= ATA_ERING_SIZE;
250 ent = &ering->ring[ering->cursor];
251 ent->eflags = eflags;
252 ent->err_mask = err_mask;
253 ent->timestamp = get_jiffies_64();
256 static struct ata_ering_entry *ata_ering_top(struct ata_ering *ering)
258 struct ata_ering_entry *ent = &ering->ring[ering->cursor];
265 static void ata_ering_clear(struct ata_ering *ering)
267 memset(ering, 0, sizeof(*ering));
270 static int ata_ering_map(struct ata_ering *ering,
271 int (*map_fn)(struct ata_ering_entry *, void *),
275 struct ata_ering_entry *ent;
279 ent = &ering->ring[idx];
282 rc = map_fn(ent, arg);
285 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
286 } while (idx != ering->cursor);
291 static unsigned int ata_eh_dev_action(struct ata_device *dev)
293 struct ata_eh_context *ehc = &dev->link->eh_context;
295 return ehc->i.action | ehc->i.dev_action[dev->devno];
298 static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
299 struct ata_eh_info *ehi, unsigned int action)
301 struct ata_device *tdev;
304 ehi->action &= ~action;
305 ata_link_for_each_dev(tdev, link)
306 ehi->dev_action[tdev->devno] &= ~action;
308 /* doesn't make sense for port-wide EH actions */
309 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
311 /* break ehi->action into ehi->dev_action */
312 if (ehi->action & action) {
313 ata_link_for_each_dev(tdev, link)
314 ehi->dev_action[tdev->devno] |=
315 ehi->action & action;
316 ehi->action &= ~action;
319 /* turn off the specified per-dev action */
320 ehi->dev_action[dev->devno] &= ~action;
325 * ata_scsi_timed_out - SCSI layer time out callback
326 * @cmd: timed out SCSI command
328 * Handles SCSI layer timeout. We race with normal completion of
329 * the qc for @cmd. If the qc is already gone, we lose and let
330 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
331 * timed out and EH should be invoked. Prevent ata_qc_complete()
332 * from finishing it by setting EH_SCHEDULED and return
335 * TODO: kill this function once old EH is gone.
338 * Called from timer context
341 * EH_HANDLED or EH_NOT_HANDLED
343 enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
345 struct Scsi_Host *host = cmd->device->host;
346 struct ata_port *ap = ata_shost_to_port(host);
348 struct ata_queued_cmd *qc;
349 enum scsi_eh_timer_return ret;
353 if (ap->ops->error_handler) {
354 ret = EH_NOT_HANDLED;
359 spin_lock_irqsave(ap->lock, flags);
360 qc = ata_qc_from_tag(ap, ap->link.active_tag);
362 WARN_ON(qc->scsicmd != cmd);
363 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
364 qc->err_mask |= AC_ERR_TIMEOUT;
365 ret = EH_NOT_HANDLED;
367 spin_unlock_irqrestore(ap->lock, flags);
370 DPRINTK("EXIT, ret=%d\n", ret);
375 * ata_scsi_error - SCSI layer error handler callback
376 * @host: SCSI host on which error occurred
378 * Handles SCSI-layer-thrown error events.
381 * Inherited from SCSI layer (none, can sleep)
386 void ata_scsi_error(struct Scsi_Host *host)
388 struct ata_port *ap = ata_shost_to_port(host);
394 /* synchronize with port task */
395 ata_port_flush_task(ap);
397 /* synchronize with host lock and sort out timeouts */
399 /* For new EH, all qcs are finished in one of three ways -
400 * normal completion, error completion, and SCSI timeout.
401 * Both cmpletions can race against SCSI timeout. When normal
402 * completion wins, the qc never reaches EH. When error
403 * completion wins, the qc has ATA_QCFLAG_FAILED set.
405 * When SCSI timeout wins, things are a bit more complex.
406 * Normal or error completion can occur after the timeout but
407 * before this point. In such cases, both types of
408 * completions are honored. A scmd is determined to have
409 * timed out iff its associated qc is active and not failed.
411 if (ap->ops->error_handler) {
412 struct scsi_cmnd *scmd, *tmp;
415 spin_lock_irqsave(ap->lock, flags);
417 list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
418 struct ata_queued_cmd *qc;
420 for (i = 0; i < ATA_MAX_QUEUE; i++) {
421 qc = __ata_qc_from_tag(ap, i);
422 if (qc->flags & ATA_QCFLAG_ACTIVE &&
427 if (i < ATA_MAX_QUEUE) {
428 /* the scmd has an associated qc */
429 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
430 /* which hasn't failed yet, timeout */
431 qc->err_mask |= AC_ERR_TIMEOUT;
432 qc->flags |= ATA_QCFLAG_FAILED;
436 /* Normal completion occurred after
437 * SCSI timeout but before this point.
438 * Successfully complete it.
440 scmd->retries = scmd->allowed;
441 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
445 /* If we have timed out qcs. They belong to EH from
446 * this point but the state of the controller is
447 * unknown. Freeze the port to make sure the IRQ
448 * handler doesn't diddle with those qcs. This must
449 * be done atomically w.r.t. setting QCFLAG_FAILED.
452 __ata_port_freeze(ap);
454 spin_unlock_irqrestore(ap->lock, flags);
456 /* initialize eh_tries */
457 ap->eh_tries = ATA_EH_MAX_TRIES;
459 spin_unlock_wait(ap->lock);
462 /* invoke error handler */
463 if (ap->ops->error_handler) {
464 struct ata_link *link;
466 /* kill fast drain timer */
467 del_timer_sync(&ap->fastdrain_timer);
469 /* process port resume request */
470 ata_eh_handle_port_resume(ap);
472 /* fetch & clear EH info */
473 spin_lock_irqsave(ap->lock, flags);
475 __ata_port_for_each_link(link, ap) {
476 struct ata_eh_context *ehc = &link->eh_context;
477 struct ata_device *dev;
479 memset(&link->eh_context, 0, sizeof(link->eh_context));
480 link->eh_context.i = link->eh_info;
481 memset(&link->eh_info, 0, sizeof(link->eh_info));
483 ata_link_for_each_dev(dev, link) {
484 int devno = dev->devno;
486 ehc->saved_xfer_mode[devno] = dev->xfer_mode;
487 if (ata_ncq_enabled(dev))
488 ehc->saved_ncq_enabled |= 1 << devno;
492 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
493 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
494 ap->excl_link = NULL; /* don't maintain exclusion over EH */
496 spin_unlock_irqrestore(ap->lock, flags);
498 /* invoke EH, skip if unloading or suspended */
499 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
500 ap->ops->error_handler(ap);
504 /* process port suspend request */
505 ata_eh_handle_port_suspend(ap);
507 /* Exception might have happend after ->error_handler
508 * recovered the port but before this point. Repeat
511 spin_lock_irqsave(ap->lock, flags);
513 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
514 if (--ap->eh_tries) {
515 spin_unlock_irqrestore(ap->lock, flags);
518 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
519 "tries, giving up\n", ATA_EH_MAX_TRIES);
520 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
523 /* this run is complete, make sure EH info is clear */
524 __ata_port_for_each_link(link, ap)
525 memset(&link->eh_info, 0, sizeof(link->eh_info));
527 /* Clear host_eh_scheduled while holding ap->lock such
528 * that if exception occurs after this point but
529 * before EH completion, SCSI midlayer will
532 host->host_eh_scheduled = 0;
534 spin_unlock_irqrestore(ap->lock, flags);
536 WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
537 ap->ops->eng_timeout(ap);
540 /* finish or retry handled scmd's and clean up */
541 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
543 scsi_eh_flush_done_q(&ap->eh_done_q);
546 spin_lock_irqsave(ap->lock, flags);
548 if (ap->pflags & ATA_PFLAG_LOADING)
549 ap->pflags &= ~ATA_PFLAG_LOADING;
550 else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
551 queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0);
553 if (ap->pflags & ATA_PFLAG_RECOVERED)
554 ata_port_printk(ap, KERN_INFO, "EH complete\n");
556 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
558 /* tell wait_eh that we're done */
559 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
560 wake_up_all(&ap->eh_wait_q);
562 spin_unlock_irqrestore(ap->lock, flags);
568 * ata_port_wait_eh - Wait for the currently pending EH to complete
569 * @ap: Port to wait EH for
571 * Wait until the currently pending EH is complete.
574 * Kernel thread context (may sleep).
576 void ata_port_wait_eh(struct ata_port *ap)
582 spin_lock_irqsave(ap->lock, flags);
584 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
585 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
586 spin_unlock_irqrestore(ap->lock, flags);
588 spin_lock_irqsave(ap->lock, flags);
590 finish_wait(&ap->eh_wait_q, &wait);
592 spin_unlock_irqrestore(ap->lock, flags);
594 /* make sure SCSI EH is complete */
595 if (scsi_host_in_recovery(ap->scsi_host)) {
601 static int ata_eh_nr_in_flight(struct ata_port *ap)
606 /* count only non-internal commands */
607 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++)
608 if (ata_qc_from_tag(ap, tag))
614 void ata_eh_fastdrain_timerfn(unsigned long arg)
616 struct ata_port *ap = (void *)arg;
620 spin_lock_irqsave(ap->lock, flags);
622 cnt = ata_eh_nr_in_flight(ap);
628 if (cnt == ap->fastdrain_cnt) {
631 /* No progress during the last interval, tag all
632 * in-flight qcs as timed out and freeze the port.
634 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) {
635 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
637 qc->err_mask |= AC_ERR_TIMEOUT;
642 /* some qcs have finished, give it another chance */
643 ap->fastdrain_cnt = cnt;
644 ap->fastdrain_timer.expires =
645 jiffies + ATA_EH_FASTDRAIN_INTERVAL;
646 add_timer(&ap->fastdrain_timer);
650 spin_unlock_irqrestore(ap->lock, flags);
654 * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
655 * @ap: target ATA port
656 * @fastdrain: activate fast drain
658 * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
659 * is non-zero and EH wasn't pending before. Fast drain ensures
660 * that EH kicks in in timely manner.
663 * spin_lock_irqsave(host lock)
665 static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
669 /* already scheduled? */
670 if (ap->pflags & ATA_PFLAG_EH_PENDING)
673 ap->pflags |= ATA_PFLAG_EH_PENDING;
678 /* do we have in-flight qcs? */
679 cnt = ata_eh_nr_in_flight(ap);
683 /* activate fast drain */
684 ap->fastdrain_cnt = cnt;
685 ap->fastdrain_timer.expires = jiffies + ATA_EH_FASTDRAIN_INTERVAL;
686 add_timer(&ap->fastdrain_timer);
690 * ata_qc_schedule_eh - schedule qc for error handling
691 * @qc: command to schedule error handling for
693 * Schedule error handling for @qc. EH will kick in as soon as
694 * other commands are drained.
697 * spin_lock_irqsave(host lock)
699 void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
701 struct ata_port *ap = qc->ap;
703 WARN_ON(!ap->ops->error_handler);
705 qc->flags |= ATA_QCFLAG_FAILED;
706 ata_eh_set_pending(ap, 1);
708 /* The following will fail if timeout has already expired.
709 * ata_scsi_error() takes care of such scmds on EH entry.
710 * Note that ATA_QCFLAG_FAILED is unconditionally set after
711 * this function completes.
713 scsi_req_abort_cmd(qc->scsicmd);
717 * ata_port_schedule_eh - schedule error handling without a qc
718 * @ap: ATA port to schedule EH for
720 * Schedule error handling for @ap. EH will kick in as soon as
721 * all commands are drained.
724 * spin_lock_irqsave(host lock)
726 void ata_port_schedule_eh(struct ata_port *ap)
728 WARN_ON(!ap->ops->error_handler);
730 if (ap->pflags & ATA_PFLAG_INITIALIZING)
733 ata_eh_set_pending(ap, 1);
734 scsi_schedule_eh(ap->scsi_host);
736 DPRINTK("port EH scheduled\n");
739 static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
741 int tag, nr_aborted = 0;
743 WARN_ON(!ap->ops->error_handler);
745 /* we're gonna abort all commands, no need for fast drain */
746 ata_eh_set_pending(ap, 0);
748 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
749 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
751 if (qc && (!link || qc->dev->link == link)) {
752 qc->flags |= ATA_QCFLAG_FAILED;
759 ata_port_schedule_eh(ap);
765 * ata_link_abort - abort all qc's on the link
766 * @link: ATA link to abort qc's for
768 * Abort all active qc's active on @link and schedule EH.
771 * spin_lock_irqsave(host lock)
774 * Number of aborted qc's.
776 int ata_link_abort(struct ata_link *link)
778 return ata_do_link_abort(link->ap, link);
782 * ata_port_abort - abort all qc's on the port
783 * @ap: ATA port to abort qc's for
785 * Abort all active qc's of @ap and schedule EH.
788 * spin_lock_irqsave(host_set lock)
791 * Number of aborted qc's.
793 int ata_port_abort(struct ata_port *ap)
795 return ata_do_link_abort(ap, NULL);
799 * __ata_port_freeze - freeze port
800 * @ap: ATA port to freeze
802 * This function is called when HSM violation or some other
803 * condition disrupts normal operation of the port. Frozen port
804 * is not allowed to perform any operation until the port is
805 * thawed, which usually follows a successful reset.
807 * ap->ops->freeze() callback can be used for freezing the port
808 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
809 * port cannot be frozen hardware-wise, the interrupt handler
810 * must ack and clear interrupts unconditionally while the port
814 * spin_lock_irqsave(host lock)
816 static void __ata_port_freeze(struct ata_port *ap)
818 WARN_ON(!ap->ops->error_handler);
823 ap->pflags |= ATA_PFLAG_FROZEN;
825 DPRINTK("ata%u port frozen\n", ap->print_id);
829 * ata_port_freeze - abort & freeze port
830 * @ap: ATA port to freeze
832 * Abort and freeze @ap.
835 * spin_lock_irqsave(host lock)
838 * Number of aborted commands.
840 int ata_port_freeze(struct ata_port *ap)
844 WARN_ON(!ap->ops->error_handler);
846 nr_aborted = ata_port_abort(ap);
847 __ata_port_freeze(ap);
853 * sata_async_notification - SATA async notification handler
854 * @ap: ATA port where async notification is received
856 * Handler to be called when async notification via SDB FIS is
857 * received. This function schedules EH if necessary.
860 * spin_lock_irqsave(host lock)
863 * 1 if EH is scheduled, 0 otherwise.
865 int sata_async_notification(struct ata_port *ap)
870 if (!(ap->flags & ATA_FLAG_AN))
873 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
875 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
877 if (!ap->nr_pmp_links || rc) {
878 /* PMP is not attached or SNTF is not available */
879 if (!ap->nr_pmp_links) {
880 /* PMP is not attached. Check whether ATAPI
881 * AN is configured. If so, notify media
884 struct ata_device *dev = ap->link.device;
886 if ((dev->class == ATA_DEV_ATAPI) &&
887 (dev->flags & ATA_DFLAG_AN))
888 ata_scsi_media_change_notify(dev);
891 /* PMP is attached but SNTF is not available.
892 * ATAPI async media change notification is
893 * not used. The PMP must be reporting PHY
894 * status change, schedule EH.
896 ata_port_schedule_eh(ap);
900 /* PMP is attached and SNTF is available */
901 struct ata_link *link;
903 /* check and notify ATAPI AN */
904 ata_port_for_each_link(link, ap) {
905 if (!(sntf & (1 << link->pmp)))
908 if ((link->device->class == ATA_DEV_ATAPI) &&
909 (link->device->flags & ATA_DFLAG_AN))
910 ata_scsi_media_change_notify(link->device);
913 /* If PMP is reporting that PHY status of some
914 * downstream ports has changed, schedule EH.
916 if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
917 ata_port_schedule_eh(ap);
926 * ata_eh_freeze_port - EH helper to freeze port
927 * @ap: ATA port to freeze
934 void ata_eh_freeze_port(struct ata_port *ap)
938 if (!ap->ops->error_handler)
941 spin_lock_irqsave(ap->lock, flags);
942 __ata_port_freeze(ap);
943 spin_unlock_irqrestore(ap->lock, flags);
947 * ata_port_thaw_port - EH helper to thaw port
948 * @ap: ATA port to thaw
950 * Thaw frozen port @ap.
955 void ata_eh_thaw_port(struct ata_port *ap)
959 if (!ap->ops->error_handler)
962 spin_lock_irqsave(ap->lock, flags);
964 ap->pflags &= ~ATA_PFLAG_FROZEN;
969 spin_unlock_irqrestore(ap->lock, flags);
971 DPRINTK("ata%u port thawed\n", ap->print_id);
974 static void ata_eh_scsidone(struct scsi_cmnd *scmd)
979 static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
981 struct ata_port *ap = qc->ap;
982 struct scsi_cmnd *scmd = qc->scsicmd;
985 spin_lock_irqsave(ap->lock, flags);
986 qc->scsidone = ata_eh_scsidone;
987 __ata_qc_complete(qc);
988 WARN_ON(ata_tag_valid(qc->tag));
989 spin_unlock_irqrestore(ap->lock, flags);
991 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
995 * ata_eh_qc_complete - Complete an active ATA command from EH
996 * @qc: Command to complete
998 * Indicate to the mid and upper layers that an ATA command has
999 * completed. To be used from EH.
1001 void ata_eh_qc_complete(struct ata_queued_cmd *qc)
1003 struct scsi_cmnd *scmd = qc->scsicmd;
1004 scmd->retries = scmd->allowed;
1005 __ata_eh_qc_complete(qc);
1009 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
1010 * @qc: Command to retry
1012 * Indicate to the mid and upper layers that an ATA command
1013 * should be retried. To be used from EH.
1015 * SCSI midlayer limits the number of retries to scmd->allowed.
1016 * scmd->retries is decremented for commands which get retried
1017 * due to unrelated failures (qc->err_mask is zero).
1019 void ata_eh_qc_retry(struct ata_queued_cmd *qc)
1021 struct scsi_cmnd *scmd = qc->scsicmd;
1022 if (!qc->err_mask && scmd->retries)
1024 __ata_eh_qc_complete(qc);
1028 * ata_eh_detach_dev - detach ATA device
1029 * @dev: ATA device to detach
1036 void ata_eh_detach_dev(struct ata_device *dev)
1038 struct ata_link *link = dev->link;
1039 struct ata_port *ap = link->ap;
1040 unsigned long flags;
1042 ata_dev_disable(dev);
1044 spin_lock_irqsave(ap->lock, flags);
1046 dev->flags &= ~ATA_DFLAG_DETACH;
1048 if (ata_scsi_offline_dev(dev)) {
1049 dev->flags |= ATA_DFLAG_DETACHED;
1050 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
1053 /* clear per-dev EH actions */
1054 ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
1055 ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
1057 spin_unlock_irqrestore(ap->lock, flags);
1061 * ata_eh_about_to_do - about to perform eh_action
1062 * @link: target ATA link
1063 * @dev: target ATA dev for per-dev action (can be NULL)
1064 * @action: action about to be performed
1066 * Called just before performing EH actions to clear related bits
1067 * in @link->eh_info such that eh actions are not unnecessarily
1073 void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
1074 unsigned int action)
1076 struct ata_port *ap = link->ap;
1077 struct ata_eh_info *ehi = &link->eh_info;
1078 struct ata_eh_context *ehc = &link->eh_context;
1079 unsigned long flags;
1081 spin_lock_irqsave(ap->lock, flags);
1083 /* Reset is represented by combination of actions and EHI
1084 * flags. Suck in all related bits before clearing eh_info to
1085 * avoid losing requested action.
1087 if (action & ATA_EH_RESET_MASK) {
1088 ehc->i.action |= ehi->action & ATA_EH_RESET_MASK;
1089 ehc->i.flags |= ehi->flags & ATA_EHI_RESET_MODIFIER_MASK;
1091 /* make sure all reset actions are cleared & clear EHI flags */
1092 action |= ATA_EH_RESET_MASK;
1093 ehi->flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
1096 ata_eh_clear_action(link, dev, ehi, action);
1098 if (!(ehc->i.flags & ATA_EHI_QUIET))
1099 ap->pflags |= ATA_PFLAG_RECOVERED;
1101 spin_unlock_irqrestore(ap->lock, flags);
1105 * ata_eh_done - EH action complete
1106 * @ap: target ATA port
1107 * @dev: target ATA dev for per-dev action (can be NULL)
1108 * @action: action just completed
1110 * Called right after performing EH actions to clear related bits
1111 * in @link->eh_context.
1116 void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1117 unsigned int action)
1119 struct ata_eh_context *ehc = &link->eh_context;
1121 /* if reset is complete, clear all reset actions & reset modifier */
1122 if (action & ATA_EH_RESET_MASK) {
1123 action |= ATA_EH_RESET_MASK;
1124 ehc->i.flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
1127 ata_eh_clear_action(link, dev, &ehc->i, action);
1131 * ata_err_string - convert err_mask to descriptive string
1132 * @err_mask: error mask to convert to string
1134 * Convert @err_mask to descriptive string. Errors are
1135 * prioritized according to severity and only the most severe
1136 * error is reported.
1142 * Descriptive string for @err_mask
1144 static const char *ata_err_string(unsigned int err_mask)
1146 if (err_mask & AC_ERR_HOST_BUS)
1147 return "host bus error";
1148 if (err_mask & AC_ERR_ATA_BUS)
1149 return "ATA bus error";
1150 if (err_mask & AC_ERR_TIMEOUT)
1152 if (err_mask & AC_ERR_HSM)
1153 return "HSM violation";
1154 if (err_mask & AC_ERR_SYSTEM)
1155 return "internal error";
1156 if (err_mask & AC_ERR_MEDIA)
1157 return "media error";
1158 if (err_mask & AC_ERR_INVALID)
1159 return "invalid argument";
1160 if (err_mask & AC_ERR_DEV)
1161 return "device error";
1162 return "unknown error";
1166 * ata_read_log_page - read a specific log page
1167 * @dev: target device
1168 * @page: page to read
1169 * @buf: buffer to store read page
1170 * @sectors: number of sectors to read
1172 * Read log page using READ_LOG_EXT command.
1175 * Kernel thread context (may sleep).
1178 * 0 on success, AC_ERR_* mask otherwise.
1180 static unsigned int ata_read_log_page(struct ata_device *dev,
1181 u8 page, void *buf, unsigned int sectors)
1183 struct ata_taskfile tf;
1184 unsigned int err_mask;
1186 DPRINTK("read log page - page %d\n", page);
1188 ata_tf_init(dev, &tf);
1189 tf.command = ATA_CMD_READ_LOG_EXT;
1192 tf.hob_nsect = sectors >> 8;
1193 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1194 tf.protocol = ATA_PROT_PIO;
1196 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
1197 buf, sectors * ATA_SECT_SIZE, 0);
1199 DPRINTK("EXIT, err_mask=%x\n", err_mask);
1204 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
1205 * @dev: Device to read log page 10h from
1206 * @tag: Resulting tag of the failed command
1207 * @tf: Resulting taskfile registers of the failed command
1209 * Read log page 10h to obtain NCQ error details and clear error
1213 * Kernel thread context (may sleep).
1216 * 0 on success, -errno otherwise.
1218 static int ata_eh_read_log_10h(struct ata_device *dev,
1219 int *tag, struct ata_taskfile *tf)
1221 u8 *buf = dev->link->ap->sector_buf;
1222 unsigned int err_mask;
1226 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
1231 for (i = 0; i < ATA_SECT_SIZE; i++)
1234 ata_dev_printk(dev, KERN_WARNING,
1235 "invalid checksum 0x%x on log page 10h\n", csum);
1240 *tag = buf[0] & 0x1f;
1242 tf->command = buf[2];
1243 tf->feature = buf[3];
1247 tf->device = buf[7];
1248 tf->hob_lbal = buf[8];
1249 tf->hob_lbam = buf[9];
1250 tf->hob_lbah = buf[10];
1251 tf->nsect = buf[12];
1252 tf->hob_nsect = buf[13];
1258 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1259 * @dev: device to perform REQUEST_SENSE to
1260 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
1262 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
1263 * SENSE. This function is EH helper.
1266 * Kernel thread context (may sleep).
1269 * 0 on success, AC_ERR_* mask on failure
1271 static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc)
1273 struct ata_device *dev = qc->dev;
1274 unsigned char *sense_buf = qc->scsicmd->sense_buffer;
1275 struct ata_port *ap = dev->link->ap;
1276 struct ata_taskfile tf;
1277 u8 cdb[ATAPI_CDB_LEN];
1279 DPRINTK("ATAPI request sense\n");
1281 /* FIXME: is this needed? */
1282 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1284 /* initialize sense_buf with the error register,
1285 * for the case where they are -not- overwritten
1287 sense_buf[0] = 0x70;
1288 sense_buf[2] = qc->result_tf.feature >> 4;
1290 /* some devices time out if garbage left in tf */
1291 ata_tf_init(dev, &tf);
1293 memset(cdb, 0, ATAPI_CDB_LEN);
1294 cdb[0] = REQUEST_SENSE;
1295 cdb[4] = SCSI_SENSE_BUFFERSIZE;
1297 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1298 tf.command = ATA_CMD_PACKET;
1300 /* is it pointless to prefer PIO for "safety reasons"? */
1301 if (ap->flags & ATA_FLAG_PIO_DMA) {
1302 tf.protocol = ATA_PROT_ATAPI_DMA;
1303 tf.feature |= ATAPI_PKT_DMA;
1305 tf.protocol = ATA_PROT_ATAPI;
1306 tf.lbam = SCSI_SENSE_BUFFERSIZE;
1310 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
1311 sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
1315 * ata_eh_analyze_serror - analyze SError for a failed port
1316 * @link: ATA link to analyze SError for
1318 * Analyze SError if available and further determine cause of
1324 static void ata_eh_analyze_serror(struct ata_link *link)
1326 struct ata_eh_context *ehc = &link->eh_context;
1327 u32 serror = ehc->i.serror;
1328 unsigned int err_mask = 0, action = 0;
1331 if (serror & SERR_PERSISTENT) {
1332 err_mask |= AC_ERR_ATA_BUS;
1333 action |= ATA_EH_HARDRESET;
1336 (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
1337 err_mask |= AC_ERR_ATA_BUS;
1338 action |= ATA_EH_SOFTRESET;
1340 if (serror & SERR_PROTOCOL) {
1341 err_mask |= AC_ERR_HSM;
1342 action |= ATA_EH_SOFTRESET;
1344 if (serror & SERR_INTERNAL) {
1345 err_mask |= AC_ERR_SYSTEM;
1346 action |= ATA_EH_HARDRESET;
1349 /* Determine whether a hotplug event has occurred. Both
1350 * SError.N/X are considered hotplug events for enabled or
1351 * host links. For disabled PMP links, only N bit is
1352 * considered as X bit is left at 1 for link plugging.
1356 if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
1357 hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
1359 hotplug_mask = SERR_PHYRDY_CHG;
1361 if (serror & hotplug_mask)
1362 ata_ehi_hotplugged(&ehc->i);
1364 ehc->i.err_mask |= err_mask;
1365 ehc->i.action |= action;
1369 * ata_eh_analyze_ncq_error - analyze NCQ error
1370 * @link: ATA link to analyze NCQ error for
1372 * Read log page 10h, determine the offending qc and acquire
1373 * error status TF. For NCQ device errors, all LLDDs have to do
1374 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1378 * Kernel thread context (may sleep).
1380 static void ata_eh_analyze_ncq_error(struct ata_link *link)
1382 struct ata_port *ap = link->ap;
1383 struct ata_eh_context *ehc = &link->eh_context;
1384 struct ata_device *dev = link->device;
1385 struct ata_queued_cmd *qc;
1386 struct ata_taskfile tf;
1389 /* if frozen, we can't do much */
1390 if (ap->pflags & ATA_PFLAG_FROZEN)
1393 /* is it NCQ device error? */
1394 if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1397 /* has LLDD analyzed already? */
1398 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1399 qc = __ata_qc_from_tag(ap, tag);
1401 if (!(qc->flags & ATA_QCFLAG_FAILED))
1408 /* okay, this error is ours */
1409 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1411 ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
1412 "(errno=%d)\n", rc);
1416 if (!(link->sactive & (1 << tag))) {
1417 ata_link_printk(link, KERN_ERR, "log page 10h reported "
1418 "inactive tag %d\n", tag);
1422 /* we've got the perpetrator, condemn it */
1423 qc = __ata_qc_from_tag(ap, tag);
1424 memcpy(&qc->result_tf, &tf, sizeof(tf));
1425 qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
1426 ehc->i.err_mask &= ~AC_ERR_DEV;
1430 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1431 * @qc: qc to analyze
1432 * @tf: Taskfile registers to analyze
1434 * Analyze taskfile of @qc and further determine cause of
1435 * failure. This function also requests ATAPI sense data if
1439 * Kernel thread context (may sleep).
1442 * Determined recovery action
1444 static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1445 const struct ata_taskfile *tf)
1447 unsigned int tmp, action = 0;
1448 u8 stat = tf->command, err = tf->feature;
1450 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1451 qc->err_mask |= AC_ERR_HSM;
1452 return ATA_EH_SOFTRESET;
1455 if (stat & (ATA_ERR | ATA_DF))
1456 qc->err_mask |= AC_ERR_DEV;
1460 switch (qc->dev->class) {
1463 qc->err_mask |= AC_ERR_ATA_BUS;
1465 qc->err_mask |= AC_ERR_MEDIA;
1467 qc->err_mask |= AC_ERR_INVALID;
1471 if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
1472 tmp = atapi_eh_request_sense(qc);
1474 /* ATA_QCFLAG_SENSE_VALID is used to
1475 * tell atapi_qc_complete() that sense
1476 * data is already valid.
1478 * TODO: interpret sense data and set
1479 * appropriate err_mask.
1481 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1483 qc->err_mask |= tmp;
1487 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1488 action |= ATA_EH_SOFTRESET;
1493 static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask,
1498 if (!(eflags & ATA_EFLAG_DUBIOUS_XFER))
1502 base = ATA_ECAT_DUBIOUS_BASE;
1504 if (err_mask & AC_ERR_ATA_BUS)
1505 return base + ATA_ECAT_ATA_BUS;
1507 if (err_mask & AC_ERR_TIMEOUT)
1508 return base + ATA_ECAT_TOUT_HSM;
1510 if (eflags & ATA_EFLAG_IS_IO) {
1511 if (err_mask & AC_ERR_HSM)
1512 return base + ATA_ECAT_TOUT_HSM;
1514 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1515 return base + ATA_ECAT_UNK_DEV;
1521 struct speed_down_verdict_arg {
1524 int nr_errors[ATA_ECAT_NR];
1527 static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
1529 struct speed_down_verdict_arg *arg = void_arg;
1532 if (ent->timestamp < arg->since)
1535 cat = ata_eh_categorize_error(ent->eflags, ent->err_mask,
1537 arg->nr_errors[cat]++;
1543 * ata_eh_speed_down_verdict - Determine speed down verdict
1544 * @dev: Device of interest
1546 * This function examines error ring of @dev and determines
1547 * whether NCQ needs to be turned off, transfer speed should be
1548 * stepped down, or falling back to PIO is necessary.
1550 * ECAT_ATA_BUS : ATA_BUS error for any command
1552 * ECAT_TOUT_HSM : TIMEOUT for any command or HSM violation for
1555 * ECAT_UNK_DEV : Unknown DEV error for IO commands
1557 * ECAT_DUBIOUS_* : Identical to above three but occurred while
1558 * data transfer hasn't been verified.
1562 * NCQ_OFF : Turn off NCQ.
1564 * SPEED_DOWN : Speed down transfer speed but don't fall back
1567 * FALLBACK_TO_PIO : Fall back to PIO.
1569 * Even if multiple verdicts are returned, only one action is
1570 * taken per error. An action triggered by non-DUBIOUS errors
1571 * clears ering, while one triggered by DUBIOUS_* errors doesn't.
1572 * This is to expedite speed down decisions right after device is
1573 * initially configured.
1575 * The followings are speed down rules. #1 and #2 deal with
1578 * 1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors
1579 * occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO.
1581 * 2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors
1582 * occurred during last 5 mins, NCQ_OFF.
1584 * 3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors
1585 * ocurred during last 5 mins, FALLBACK_TO_PIO
1587 * 4. If more than 3 TOUT_HSM or UNK_DEV errors occurred
1588 * during last 10 mins, NCQ_OFF.
1590 * 5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6
1591 * UNK_DEV errors occurred during last 10 mins, SPEED_DOWN.
1594 * Inherited from caller.
1597 * OR of ATA_EH_SPDN_* flags.
1599 static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
1601 const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
1602 u64 j64 = get_jiffies_64();
1603 struct speed_down_verdict_arg arg;
1604 unsigned int verdict = 0;
1606 /* scan past 5 mins of error history */
1607 memset(&arg, 0, sizeof(arg));
1608 arg.since = j64 - min(j64, j5mins);
1609 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1611 if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] +
1612 arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1)
1613 verdict |= ATA_EH_SPDN_SPEED_DOWN |
1614 ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS;
1616 if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] +
1617 arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1)
1618 verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS;
1620 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1621 arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1622 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
1623 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
1625 /* scan past 10 mins of error history */
1626 memset(&arg, 0, sizeof(arg));
1627 arg.since = j64 - min(j64, j10mins);
1628 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1630 if (arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1631 arg.nr_errors[ATA_ECAT_UNK_DEV] > 3)
1632 verdict |= ATA_EH_SPDN_NCQ_OFF;
1634 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1635 arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 ||
1636 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
1637 verdict |= ATA_EH_SPDN_SPEED_DOWN;
1643 * ata_eh_speed_down - record error and speed down if necessary
1644 * @dev: Failed device
1645 * @eflags: mask of ATA_EFLAG_* flags
1646 * @err_mask: err_mask of the error
1648 * Record error and examine error history to determine whether
1649 * adjusting transmission speed is necessary. It also sets
1650 * transmission limits appropriately if such adjustment is
1654 * Kernel thread context (may sleep).
1657 * Determined recovery action.
1659 static unsigned int ata_eh_speed_down(struct ata_device *dev,
1660 unsigned int eflags, unsigned int err_mask)
1662 struct ata_link *link = dev->link;
1664 unsigned int verdict;
1665 unsigned int action = 0;
1667 /* don't bother if Cat-0 error */
1668 if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0)
1671 /* record error and determine whether speed down is necessary */
1672 ata_ering_record(&dev->ering, eflags, err_mask);
1673 verdict = ata_eh_speed_down_verdict(dev);
1676 if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
1677 (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
1678 ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
1679 dev->flags |= ATA_DFLAG_NCQ_OFF;
1680 ata_dev_printk(dev, KERN_WARNING,
1681 "NCQ disabled due to excessive errors\n");
1686 if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1687 /* speed down SATA link speed if possible */
1688 if (sata_down_spd_limit(link) == 0) {
1689 action |= ATA_EH_HARDRESET;
1693 /* lower transfer mode */
1694 if (dev->spdn_cnt < 2) {
1695 static const int dma_dnxfer_sel[] =
1696 { ATA_DNXFER_DMA, ATA_DNXFER_40C };
1697 static const int pio_dnxfer_sel[] =
1698 { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
1701 if (dev->xfer_shift != ATA_SHIFT_PIO)
1702 sel = dma_dnxfer_sel[dev->spdn_cnt];
1704 sel = pio_dnxfer_sel[dev->spdn_cnt];
1708 if (ata_down_xfermask_limit(dev, sel) == 0) {
1709 action |= ATA_EH_SOFTRESET;
1715 /* Fall back to PIO? Slowing down to PIO is meaningless for
1716 * SATA ATA devices. Consider it only for PATA and SATAPI.
1718 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
1719 (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) &&
1720 (dev->xfer_shift != ATA_SHIFT_PIO)) {
1721 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
1723 action |= ATA_EH_SOFTRESET;
1730 /* device has been slowed down, blow error history */
1731 if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS))
1732 ata_ering_clear(&dev->ering);
1737 * ata_eh_link_autopsy - analyze error and determine recovery action
1738 * @link: host link to perform autopsy on
1740 * Analyze why @link failed and determine which recovery actions
1741 * are needed. This function also sets more detailed AC_ERR_*
1742 * values and fills sense data for ATAPI CHECK SENSE.
1745 * Kernel thread context (may sleep).
1747 static void ata_eh_link_autopsy(struct ata_link *link)
1749 struct ata_port *ap = link->ap;
1750 struct ata_eh_context *ehc = &link->eh_context;
1751 struct ata_device *dev;
1752 unsigned int all_err_mask = 0, eflags = 0;
1759 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1762 /* obtain and analyze SError */
1763 rc = sata_scr_read(link, SCR_ERROR, &serror);
1765 ehc->i.serror |= serror;
1766 ata_eh_analyze_serror(link);
1767 } else if (rc != -EOPNOTSUPP) {
1768 /* SError read failed, force hardreset and probing */
1769 ata_ehi_schedule_probe(&ehc->i);
1770 ehc->i.action |= ATA_EH_HARDRESET;
1771 ehc->i.err_mask |= AC_ERR_OTHER;
1774 /* analyze NCQ failure */
1775 ata_eh_analyze_ncq_error(link);
1777 /* any real error trumps AC_ERR_OTHER */
1778 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1779 ehc->i.err_mask &= ~AC_ERR_OTHER;
1781 all_err_mask |= ehc->i.err_mask;
1783 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1784 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1786 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
1789 /* inherit upper level err_mask */
1790 qc->err_mask |= ehc->i.err_mask;
1793 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1795 /* DEV errors are probably spurious in case of ATA_BUS error */
1796 if (qc->err_mask & AC_ERR_ATA_BUS)
1797 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1800 /* any real error trumps unknown error */
1801 if (qc->err_mask & ~AC_ERR_OTHER)
1802 qc->err_mask &= ~AC_ERR_OTHER;
1804 /* SENSE_VALID trumps dev/unknown error and revalidation */
1805 if (qc->flags & ATA_QCFLAG_SENSE_VALID)
1806 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1808 /* accumulate error info */
1809 ehc->i.dev = qc->dev;
1810 all_err_mask |= qc->err_mask;
1811 if (qc->flags & ATA_QCFLAG_IO)
1812 eflags |= ATA_EFLAG_IS_IO;
1815 /* enforce default EH actions */
1816 if (ap->pflags & ATA_PFLAG_FROZEN ||
1817 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
1818 ehc->i.action |= ATA_EH_SOFTRESET;
1819 else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) ||
1820 (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV)))
1821 ehc->i.action |= ATA_EH_REVALIDATE;
1823 /* If we have offending qcs and the associated failed device,
1824 * perform per-dev EH action only on the offending device.
1827 ehc->i.dev_action[ehc->i.dev->devno] |=
1828 ehc->i.action & ATA_EH_PERDEV_MASK;
1829 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
1832 /* propagate timeout to host link */
1833 if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link))
1834 ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT;
1836 /* record error and consider speeding down */
1838 if (!dev && ((ata_link_max_devices(link) == 1 &&
1839 ata_dev_enabled(link->device))))
1843 if (dev->flags & ATA_DFLAG_DUBIOUS_XFER)
1844 eflags |= ATA_EFLAG_DUBIOUS_XFER;
1845 ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask);
1852 * ata_eh_autopsy - analyze error and determine recovery action
1853 * @ap: host port to perform autopsy on
1855 * Analyze all links of @ap and determine why they failed and
1856 * which recovery actions are needed.
1859 * Kernel thread context (may sleep).
1861 void ata_eh_autopsy(struct ata_port *ap)
1863 struct ata_link *link;
1865 ata_port_for_each_link(link, ap)
1866 ata_eh_link_autopsy(link);
1868 /* Autopsy of fanout ports can affect host link autopsy.
1869 * Perform host link autopsy last.
1871 if (ap->nr_pmp_links)
1872 ata_eh_link_autopsy(&ap->link);
1876 * ata_eh_link_report - report error handling to user
1877 * @link: ATA link EH is going on
1879 * Report EH to user.
1884 static void ata_eh_link_report(struct ata_link *link)
1886 struct ata_port *ap = link->ap;
1887 struct ata_eh_context *ehc = &link->eh_context;
1888 const char *frozen, *desc;
1890 int tag, nr_failed = 0;
1892 if (ehc->i.flags & ATA_EHI_QUIET)
1896 if (ehc->i.desc[0] != '\0')
1899 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1900 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1902 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link ||
1903 ((qc->flags & ATA_QCFLAG_QUIET) &&
1904 qc->err_mask == AC_ERR_DEV))
1906 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1912 if (!nr_failed && !ehc->i.err_mask)
1916 if (ap->pflags & ATA_PFLAG_FROZEN)
1919 memset(tries_buf, 0, sizeof(tries_buf));
1920 if (ap->eh_tries < ATA_EH_MAX_TRIES)
1921 snprintf(tries_buf, sizeof(tries_buf) - 1, " t%d",
1925 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1926 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1927 ehc->i.err_mask, link->sactive, ehc->i.serror,
1928 ehc->i.action, frozen, tries_buf);
1930 ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
1932 ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
1933 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1934 ehc->i.err_mask, link->sactive, ehc->i.serror,
1935 ehc->i.action, frozen, tries_buf);
1937 ata_link_printk(link, KERN_ERR, "%s\n", desc);
1941 ata_port_printk(ap, KERN_ERR,
1942 "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
1943 ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
1944 ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
1945 ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
1946 ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
1947 ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
1948 ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
1949 ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
1950 ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
1951 ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
1952 ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
1953 ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
1954 ehc->i.serror & SERR_CRC ? "BadCRC " : "",
1955 ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
1956 ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
1957 ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
1958 ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
1959 ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
1961 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1962 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1963 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
1964 const u8 *cdb = qc->cdb;
1965 char data_buf[20] = "";
1966 char cdb_buf[70] = "";
1968 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
1969 qc->dev->link != link || !qc->err_mask)
1972 if (qc->dma_dir != DMA_NONE) {
1973 static const char *dma_str[] = {
1974 [DMA_BIDIRECTIONAL] = "bidi",
1975 [DMA_TO_DEVICE] = "out",
1976 [DMA_FROM_DEVICE] = "in",
1978 static const char *prot_str[] = {
1979 [ATA_PROT_PIO] = "pio",
1980 [ATA_PROT_DMA] = "dma",
1981 [ATA_PROT_NCQ] = "ncq",
1982 [ATA_PROT_ATAPI] = "pio",
1983 [ATA_PROT_ATAPI_DMA] = "dma",
1986 snprintf(data_buf, sizeof(data_buf), " %s %u %s",
1987 prot_str[qc->tf.protocol], qc->nbytes,
1988 dma_str[qc->dma_dir]);
1991 if (ata_is_atapi(qc->tf.protocol))
1992 snprintf(cdb_buf, sizeof(cdb_buf),
1993 "cdb %02x %02x %02x %02x %02x %02x %02x %02x "
1994 "%02x %02x %02x %02x %02x %02x %02x %02x\n ",
1995 cdb[0], cdb[1], cdb[2], cdb[3],
1996 cdb[4], cdb[5], cdb[6], cdb[7],
1997 cdb[8], cdb[9], cdb[10], cdb[11],
1998 cdb[12], cdb[13], cdb[14], cdb[15]);
2000 ata_dev_printk(qc->dev, KERN_ERR,
2001 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
2003 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
2004 "Emask 0x%x (%s)%s\n",
2005 cmd->command, cmd->feature, cmd->nsect,
2006 cmd->lbal, cmd->lbam, cmd->lbah,
2007 cmd->hob_feature, cmd->hob_nsect,
2008 cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
2009 cmd->device, qc->tag, data_buf, cdb_buf,
2010 res->command, res->feature, res->nsect,
2011 res->lbal, res->lbam, res->lbah,
2012 res->hob_feature, res->hob_nsect,
2013 res->hob_lbal, res->hob_lbam, res->hob_lbah,
2014 res->device, qc->err_mask, ata_err_string(qc->err_mask),
2015 qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
2017 if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
2019 if (res->command & ATA_BUSY)
2020 ata_dev_printk(qc->dev, KERN_ERR,
2021 "status: { Busy }\n");
2023 ata_dev_printk(qc->dev, KERN_ERR,
2024 "status: { %s%s%s%s}\n",
2025 res->command & ATA_DRDY ? "DRDY " : "",
2026 res->command & ATA_DF ? "DF " : "",
2027 res->command & ATA_DRQ ? "DRQ " : "",
2028 res->command & ATA_ERR ? "ERR " : "");
2031 if (cmd->command != ATA_CMD_PACKET &&
2032 (res->feature & (ATA_ICRC | ATA_UNC | ATA_IDNF |
2034 ata_dev_printk(qc->dev, KERN_ERR,
2035 "error: { %s%s%s%s}\n",
2036 res->feature & ATA_ICRC ? "ICRC " : "",
2037 res->feature & ATA_UNC ? "UNC " : "",
2038 res->feature & ATA_IDNF ? "IDNF " : "",
2039 res->feature & ATA_ABORTED ? "ABRT " : "");
2044 * ata_eh_report - report error handling to user
2045 * @ap: ATA port to report EH about
2047 * Report EH to user.
2052 void ata_eh_report(struct ata_port *ap)
2054 struct ata_link *link;
2056 __ata_port_for_each_link(link, ap)
2057 ata_eh_link_report(link);
2060 static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
2061 unsigned int *classes, unsigned long deadline)
2063 struct ata_device *dev;
2066 ata_link_for_each_dev(dev, link)
2067 classes[dev->devno] = ATA_DEV_UNKNOWN;
2069 rc = reset(link, classes, deadline);
2073 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2074 * is complete and convert all ATA_DEV_UNKNOWN to
2077 ata_link_for_each_dev(dev, link)
2078 if (classes[dev->devno] != ATA_DEV_UNKNOWN)
2082 ata_link_for_each_dev(dev, link) {
2083 if (classes[dev->devno] == ATA_DEV_UNKNOWN)
2084 classes[dev->devno] = ATA_DEV_NONE;
2091 static int ata_eh_followup_srst_needed(struct ata_link *link,
2092 int rc, int classify,
2093 const unsigned int *classes)
2095 if (link->flags & ATA_LFLAG_NO_SRST)
2101 if ((link->ap->flags & ATA_FLAG_PMP) && ata_is_host_link(link))
2103 if (classify && !(link->flags & ATA_LFLAG_ASSUME_CLASS) &&
2104 classes[0] == ATA_DEV_UNKNOWN)
2109 int ata_eh_reset(struct ata_link *link, int classify,
2110 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
2111 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
2113 const int max_tries = ARRAY_SIZE(ata_eh_reset_timeouts);
2114 struct ata_port *ap = link->ap;
2115 struct ata_eh_context *ehc = &link->eh_context;
2116 unsigned int *classes = ehc->classes;
2117 unsigned int lflags = link->flags;
2118 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
2120 struct ata_device *dev;
2121 unsigned long deadline, now;
2122 unsigned int tmp_action;
2123 ata_reset_fn_t reset;
2124 unsigned long flags;
2128 /* about to reset */
2129 spin_lock_irqsave(ap->lock, flags);
2130 ap->pflags |= ATA_PFLAG_RESETTING;
2131 spin_unlock_irqrestore(ap->lock, flags);
2133 ata_eh_about_to_do(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
2135 ata_link_for_each_dev(dev, link) {
2136 /* If we issue an SRST then an ATA drive (not ATAPI)
2137 * may change configuration and be in PIO0 timing. If
2138 * we do a hard reset (or are coming from power on)
2139 * this is true for ATA or ATAPI. Until we've set a
2140 * suitable controller mode we should not touch the
2141 * bus as we may be talking too fast.
2143 dev->pio_mode = XFER_PIO_0;
2145 /* If the controller has a pio mode setup function
2146 * then use it to set the chipset to rights. Don't
2147 * touch the DMA setup as that will be dealt with when
2148 * configuring devices.
2150 if (ap->ops->set_piomode)
2151 ap->ops->set_piomode(ap, dev);
2154 /* Determine which reset to use and record in ehc->i.action.
2155 * prereset() may examine and modify it.
2157 if (softreset && (!hardreset || (!(lflags & ATA_LFLAG_NO_SRST) &&
2158 !sata_set_spd_needed(link) &&
2159 !(ehc->i.action & ATA_EH_HARDRESET))))
2160 tmp_action = ATA_EH_SOFTRESET;
2162 tmp_action = ATA_EH_HARDRESET;
2164 ehc->i.action = (ehc->i.action & ~ATA_EH_RESET_MASK) | tmp_action;
2167 rc = prereset(link, jiffies + ATA_EH_PRERESET_TIMEOUT);
2169 if (rc == -ENOENT) {
2170 ata_link_printk(link, KERN_DEBUG,
2171 "port disabled. ignoring.\n");
2172 ehc->i.action &= ~ATA_EH_RESET_MASK;
2174 ata_link_for_each_dev(dev, link)
2175 classes[dev->devno] = ATA_DEV_NONE;
2179 ata_link_printk(link, KERN_ERR,
2180 "prereset failed (errno=%d)\n", rc);
2185 /* prereset() might have modified ehc->i.action */
2186 if (ehc->i.action & ATA_EH_HARDRESET)
2188 else if (ehc->i.action & ATA_EH_SOFTRESET)
2191 /* prereset told us not to reset, bang classes and return */
2192 ata_link_for_each_dev(dev, link)
2193 classes[dev->devno] = ATA_DEV_NONE;
2198 /* did prereset() screw up? if so, fix up to avoid oopsing */
2207 deadline = jiffies + ata_eh_reset_timeouts[try++];
2209 /* shut up during boot probing */
2211 ata_link_printk(link, KERN_INFO, "%s resetting link\n",
2212 reset == softreset ? "soft" : "hard");
2214 /* mark that this EH session started with reset */
2215 if (reset == hardreset)
2216 ehc->i.flags |= ATA_EHI_DID_HARDRESET;
2218 ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
2220 rc = ata_do_reset(link, reset, classes, deadline);
2222 if (reset == hardreset &&
2223 ata_eh_followup_srst_needed(link, rc, classify, classes)) {
2224 /* okay, let's do follow-up softreset */
2228 ata_link_printk(link, KERN_ERR,
2229 "follow-up softreset required "
2230 "but no softreset avaliable\n");
2235 ata_eh_about_to_do(link, NULL, ATA_EH_RESET_MASK);
2236 rc = ata_do_reset(link, reset, classes, deadline);
2239 /* -EAGAIN can happen if we skipped followup SRST */
2240 if (rc && rc != -EAGAIN)
2243 /* was classification successful? */
2244 if (classify && classes[0] == ATA_DEV_UNKNOWN &&
2245 !(lflags & ATA_LFLAG_ASSUME_CLASS)) {
2246 if (try < max_tries) {
2247 ata_link_printk(link, KERN_WARNING,
2248 "classification failed\n");
2253 ata_link_printk(link, KERN_WARNING,
2254 "classfication failed, assuming ATA\n");
2255 lflags |= ATA_LFLAG_ASSUME_ATA;
2258 ata_link_for_each_dev(dev, link) {
2259 /* After the reset, the device state is PIO 0 and the
2260 * controller state is undefined. Reset also wakes up
2261 * drives from sleeping mode.
2263 dev->pio_mode = XFER_PIO_0;
2264 dev->flags &= ~ATA_DFLAG_SLEEPING;
2266 if (ata_link_offline(link))
2269 /* apply class override */
2270 if (lflags & ATA_LFLAG_ASSUME_ATA)
2271 classes[dev->devno] = ATA_DEV_ATA;
2272 else if (lflags & ATA_LFLAG_ASSUME_SEMB)
2273 classes[dev->devno] = ATA_DEV_SEMB_UNSUP; /* not yet */
2276 /* record current link speed */
2277 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2278 link->sata_spd = (sstatus >> 4) & 0xf;
2281 postreset(link, classes);
2283 /* reset successful, schedule revalidation */
2284 ata_eh_done(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
2285 ehc->i.action |= ATA_EH_REVALIDATE;
2289 /* clear hotplug flag */
2290 ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2292 spin_lock_irqsave(ap->lock, flags);
2293 ap->pflags &= ~ATA_PFLAG_RESETTING;
2294 spin_unlock_irqrestore(ap->lock, flags);
2299 if (rc == -ERESTART || try >= max_tries)
2303 if (time_before(now, deadline)) {
2304 unsigned long delta = deadline - now;
2306 ata_link_printk(link, KERN_WARNING, "reset failed "
2307 "(errno=%d), retrying in %u secs\n",
2308 rc, (jiffies_to_msecs(delta) + 999) / 1000);
2311 delta = schedule_timeout_uninterruptible(delta);
2314 if (rc == -EPIPE || try == max_tries - 1)
2315 sata_down_spd_limit(link);
2321 static int ata_eh_revalidate_and_attach(struct ata_link *link,
2322 struct ata_device **r_failed_dev)
2324 struct ata_port *ap = link->ap;
2325 struct ata_eh_context *ehc = &link->eh_context;
2326 struct ata_device *dev;
2327 unsigned int new_mask = 0;
2328 unsigned long flags;
2333 /* For PATA drive side cable detection to work, IDENTIFY must
2334 * be done backwards such that PDIAG- is released by the slave
2335 * device before the master device is identified.
2337 ata_link_for_each_dev_reverse(dev, link) {
2338 unsigned int action = ata_eh_dev_action(dev);
2339 unsigned int readid_flags = 0;
2341 if (ehc->i.flags & ATA_EHI_DID_RESET)
2342 readid_flags |= ATA_READID_POSTRESET;
2344 if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
2345 WARN_ON(dev->class == ATA_DEV_PMP);
2347 if (ata_link_offline(link)) {
2352 ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
2353 rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
2358 ata_eh_done(link, dev, ATA_EH_REVALIDATE);
2360 /* Configuration may have changed, reconfigure
2363 ehc->i.flags |= ATA_EHI_SETMODE;
2365 /* schedule the scsi_rescan_device() here */
2366 queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
2367 } else if (dev->class == ATA_DEV_UNKNOWN &&
2368 ehc->tries[dev->devno] &&
2369 ata_class_enabled(ehc->classes[dev->devno])) {
2370 dev->class = ehc->classes[dev->devno];
2372 if (dev->class == ATA_DEV_PMP)
2373 rc = sata_pmp_attach(dev);
2375 rc = ata_dev_read_id(dev, &dev->class,
2376 readid_flags, dev->id);
2379 new_mask |= 1 << dev->devno;
2382 /* IDENTIFY was issued to non-existent
2383 * device. No need to reset. Just
2384 * thaw and kill the device.
2386 ata_eh_thaw_port(ap);
2387 dev->class = ATA_DEV_UNKNOWN;
2390 dev->class = ATA_DEV_UNKNOWN;
2396 /* PDIAG- should have been released, ask cable type if post-reset */
2397 if (ata_is_host_link(link) && ap->ops->cable_detect &&
2398 (ehc->i.flags & ATA_EHI_DID_RESET))
2399 ap->cbl = ap->ops->cable_detect(ap);
2401 /* Configure new devices forward such that user doesn't see
2402 * device detection messages backwards.
2404 ata_link_for_each_dev(dev, link) {
2405 if (!(new_mask & (1 << dev->devno)) ||
2406 dev->class == ATA_DEV_PMP)
2409 ehc->i.flags |= ATA_EHI_PRINTINFO;
2410 rc = ata_dev_configure(dev);
2411 ehc->i.flags &= ~ATA_EHI_PRINTINFO;
2415 spin_lock_irqsave(ap->lock, flags);
2416 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
2417 spin_unlock_irqrestore(ap->lock, flags);
2419 /* new device discovered, configure xfermode */
2420 ehc->i.flags |= ATA_EHI_SETMODE;
2426 *r_failed_dev = dev;
2427 DPRINTK("EXIT rc=%d\n", rc);
2432 * ata_set_mode - Program timings and issue SET FEATURES - XFER
2433 * @link: link on which timings will be programmed
2434 * @r_failed_dev: out paramter for failed device
2436 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
2437 * ata_set_mode() fails, pointer to the failing device is
2438 * returned in @r_failed_dev.
2441 * PCI/etc. bus probe sem.
2444 * 0 on success, negative errno otherwise
2446 int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
2448 struct ata_port *ap = link->ap;
2449 struct ata_device *dev;
2452 /* if data transfer is verified, clear DUBIOUS_XFER on ering top */
2453 ata_link_for_each_dev(dev, link) {
2454 if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
2455 struct ata_ering_entry *ent;
2457 ent = ata_ering_top(&dev->ering);
2459 ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER;
2463 /* has private set_mode? */
2464 if (ap->ops->set_mode)
2465 rc = ap->ops->set_mode(link, r_failed_dev);
2467 rc = ata_do_set_mode(link, r_failed_dev);
2469 /* if transfer mode has changed, set DUBIOUS_XFER on device */
2470 ata_link_for_each_dev(dev, link) {
2471 struct ata_eh_context *ehc = &link->eh_context;
2472 u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
2473 u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
2475 if (dev->xfer_mode != saved_xfer_mode ||
2476 ata_ncq_enabled(dev) != saved_ncq)
2477 dev->flags |= ATA_DFLAG_DUBIOUS_XFER;
2483 static int ata_link_nr_enabled(struct ata_link *link)
2485 struct ata_device *dev;
2488 ata_link_for_each_dev(dev, link)
2489 if (ata_dev_enabled(dev))
2494 static int ata_link_nr_vacant(struct ata_link *link)
2496 struct ata_device *dev;
2499 ata_link_for_each_dev(dev, link)
2500 if (dev->class == ATA_DEV_UNKNOWN)
2505 static int ata_eh_skip_recovery(struct ata_link *link)
2507 struct ata_eh_context *ehc = &link->eh_context;
2508 struct ata_device *dev;
2510 /* skip disabled links */
2511 if (link->flags & ATA_LFLAG_DISABLED)
2514 /* thaw frozen port, resume link and recover failed devices */
2515 if ((link->ap->pflags & ATA_PFLAG_FROZEN) ||
2516 (ehc->i.flags & ATA_EHI_RESUME_LINK) || ata_link_nr_enabled(link))
2519 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
2520 ata_link_for_each_dev(dev, link) {
2521 if (dev->class == ATA_DEV_UNKNOWN &&
2522 ehc->classes[dev->devno] != ATA_DEV_NONE)
2529 static int ata_eh_schedule_probe(struct ata_device *dev)
2531 struct ata_eh_context *ehc = &dev->link->eh_context;
2533 if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
2534 (ehc->did_probe_mask & (1 << dev->devno)))
2537 ata_eh_detach_dev(dev);
2539 ehc->did_probe_mask |= (1 << dev->devno);
2540 ehc->i.action |= ATA_EH_SOFTRESET;
2541 ehc->saved_xfer_mode[dev->devno] = 0;
2542 ehc->saved_ncq_enabled &= ~(1 << dev->devno);
2547 static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
2549 struct ata_eh_context *ehc = &dev->link->eh_context;
2551 ehc->tries[dev->devno]--;
2555 /* device missing or wrong IDENTIFY data, schedule probing */
2556 ehc->i.probe_mask |= (1 << dev->devno);
2558 /* give it just one more chance */
2559 ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
2561 if (ehc->tries[dev->devno] == 1 && dev->pio_mode > XFER_PIO_0) {
2562 /* This is the last chance, better to slow
2563 * down than lose it.
2565 sata_down_spd_limit(dev->link);
2566 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2570 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2571 /* disable device if it has used up all its chances */
2572 ata_dev_disable(dev);
2574 /* detach if offline */
2575 if (ata_link_offline(dev->link))
2576 ata_eh_detach_dev(dev);
2578 /* schedule probe if necessary */
2579 if (ata_eh_schedule_probe(dev))
2580 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2584 /* soft didn't work? be haaaaard */
2585 if (ehc->i.flags & ATA_EHI_DID_RESET)
2586 ehc->i.action |= ATA_EH_HARDRESET;
2588 ehc->i.action |= ATA_EH_SOFTRESET;
2595 * ata_eh_recover - recover host port after error
2596 * @ap: host port to recover
2597 * @prereset: prereset method (can be NULL)
2598 * @softreset: softreset method (can be NULL)
2599 * @hardreset: hardreset method (can be NULL)
2600 * @postreset: postreset method (can be NULL)
2601 * @r_failed_link: out parameter for failed link
2603 * This is the alpha and omega, eum and yang, heart and soul of
2604 * libata exception handling. On entry, actions required to
2605 * recover each link and hotplug requests are recorded in the
2606 * link's eh_context. This function executes all the operations
2607 * with appropriate retrials and fallbacks to resurrect failed
2608 * devices, detach goners and greet newcomers.
2611 * Kernel thread context (may sleep).
2614 * 0 on success, -errno on failure.
2616 int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
2617 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2618 ata_postreset_fn_t postreset,
2619 struct ata_link **r_failed_link)
2621 struct ata_link *link;
2622 struct ata_device *dev;
2623 int nr_failed_devs, nr_disabled_devs;
2625 unsigned long flags;
2629 /* prep for recovery */
2630 ata_port_for_each_link(link, ap) {
2631 struct ata_eh_context *ehc = &link->eh_context;
2633 /* re-enable link? */
2634 if (ehc->i.action & ATA_EH_ENABLE_LINK) {
2635 ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
2636 spin_lock_irqsave(ap->lock, flags);
2637 link->flags &= ~ATA_LFLAG_DISABLED;
2638 spin_unlock_irqrestore(ap->lock, flags);
2639 ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
2642 ata_link_for_each_dev(dev, link) {
2643 if (link->flags & ATA_LFLAG_NO_RETRY)
2644 ehc->tries[dev->devno] = 1;
2646 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2648 /* collect port action mask recorded in dev actions */
2649 ehc->i.action |= ehc->i.dev_action[dev->devno] &
2650 ~ATA_EH_PERDEV_MASK;
2651 ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
2653 /* process hotplug request */
2654 if (dev->flags & ATA_DFLAG_DETACH)
2655 ata_eh_detach_dev(dev);
2657 /* schedule probe if necessary */
2658 if (!ata_dev_enabled(dev))
2659 ata_eh_schedule_probe(dev);
2666 nr_disabled_devs = 0;
2669 /* if UNLOADING, finish immediately */
2670 if (ap->pflags & ATA_PFLAG_UNLOADING)
2674 ata_port_for_each_link(link, ap) {
2675 struct ata_eh_context *ehc = &link->eh_context;
2677 /* skip EH if possible. */
2678 if (ata_eh_skip_recovery(link))
2681 /* do we need to reset? */
2682 if (ehc->i.action & ATA_EH_RESET_MASK)
2685 ata_link_for_each_dev(dev, link)
2686 ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
2691 /* if PMP is attached, this function only deals with
2692 * downstream links, port should stay thawed.
2694 if (!ap->nr_pmp_links)
2695 ata_eh_freeze_port(ap);
2697 ata_port_for_each_link(link, ap) {
2698 struct ata_eh_context *ehc = &link->eh_context;
2700 if (!(ehc->i.action & ATA_EH_RESET_MASK))
2703 rc = ata_eh_reset(link, ata_link_nr_vacant(link),
2704 prereset, softreset, hardreset,
2707 ata_link_printk(link, KERN_ERR,
2708 "reset failed, giving up\n");
2713 if (!ap->nr_pmp_links)
2714 ata_eh_thaw_port(ap);
2718 ata_port_for_each_link(link, ap) {
2719 struct ata_eh_context *ehc = &link->eh_context;
2721 /* revalidate existing devices and attach new ones */
2722 rc = ata_eh_revalidate_and_attach(link, &dev);
2726 /* if PMP got attached, return, pmp EH will take care of it */
2727 if (link->device->class == ATA_DEV_PMP) {
2732 /* configure transfer mode if necessary */
2733 if (ehc->i.flags & ATA_EHI_SETMODE) {
2734 rc = ata_set_mode(link, &dev);
2737 ehc->i.flags &= ~ATA_EHI_SETMODE;
2740 if (ehc->i.action & ATA_EHI_LPM)
2741 ata_link_for_each_dev(dev, link)
2742 ata_dev_enable_pm(dev, ap->pm_policy);
2744 /* this link is okay now */
2750 if (ata_eh_handle_dev_fail(dev, rc))
2753 if (ap->pflags & ATA_PFLAG_FROZEN) {
2754 /* PMP reset requires working host port.
2755 * Can't retry if it's frozen.
2757 if (ap->nr_pmp_links)
2763 if (nr_failed_devs) {
2764 if (nr_failed_devs != nr_disabled_devs) {
2765 ata_port_printk(ap, KERN_WARNING, "failed to recover "
2766 "some devices, retrying in 5 secs\n");
2769 /* no device left to recover, repeat fast */
2777 if (rc && r_failed_link)
2778 *r_failed_link = link;
2780 DPRINTK("EXIT, rc=%d\n", rc);
2785 * ata_eh_finish - finish up EH
2786 * @ap: host port to finish EH for
2788 * Recovery is complete. Clean up EH states and retry or finish
2794 void ata_eh_finish(struct ata_port *ap)
2798 /* retry or finish qcs */
2799 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2800 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2802 if (!(qc->flags & ATA_QCFLAG_FAILED))
2806 /* FIXME: Once EH migration is complete,
2807 * generate sense data in this function,
2808 * considering both err_mask and tf.
2810 * There's no point in retrying invalid
2811 * (detected by libata) and non-IO device
2812 * errors (rejected by device). Finish them
2815 if ((qc->err_mask & AC_ERR_INVALID) ||
2816 (!(qc->flags & ATA_QCFLAG_IO) &&
2817 qc->err_mask == AC_ERR_DEV))
2818 ata_eh_qc_complete(qc);
2820 ata_eh_qc_retry(qc);
2822 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2823 ata_eh_qc_complete(qc);
2825 /* feed zero TF to sense generation */
2826 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2827 ata_eh_qc_retry(qc);
2832 /* make sure nr_active_links is zero after EH */
2833 WARN_ON(ap->nr_active_links);
2834 ap->nr_active_links = 0;
2838 * ata_do_eh - do standard error handling
2839 * @ap: host port to handle error for
2840 * @prereset: prereset method (can be NULL)
2841 * @softreset: softreset method (can be NULL)
2842 * @hardreset: hardreset method (can be NULL)
2843 * @postreset: postreset method (can be NULL)
2845 * Perform standard error handling sequence.
2848 * Kernel thread context (may sleep).
2850 void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2851 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2852 ata_postreset_fn_t postreset)
2854 struct ata_device *dev;
2860 rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
2863 ata_link_for_each_dev(dev, &ap->link)
2864 ata_dev_disable(dev);
2872 * ata_eh_handle_port_suspend - perform port suspend operation
2873 * @ap: port to suspend
2878 * Kernel thread context (may sleep).
2880 static void ata_eh_handle_port_suspend(struct ata_port *ap)
2882 unsigned long flags;
2885 /* are we suspending? */
2886 spin_lock_irqsave(ap->lock, flags);
2887 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2888 ap->pm_mesg.event == PM_EVENT_ON) {
2889 spin_unlock_irqrestore(ap->lock, flags);
2892 spin_unlock_irqrestore(ap->lock, flags);
2894 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
2896 /* tell ACPI we're suspending */
2897 rc = ata_acpi_on_suspend(ap);
2902 ata_eh_freeze_port(ap);
2904 if (ap->ops->port_suspend)
2905 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
2907 ata_acpi_set_state(ap, PMSG_SUSPEND);
2910 spin_lock_irqsave(ap->lock, flags);
2912 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
2914 ap->pflags |= ATA_PFLAG_SUSPENDED;
2915 else if (ap->pflags & ATA_PFLAG_FROZEN)
2916 ata_port_schedule_eh(ap);
2918 if (ap->pm_result) {
2919 *ap->pm_result = rc;
2920 ap->pm_result = NULL;
2923 spin_unlock_irqrestore(ap->lock, flags);
2929 * ata_eh_handle_port_resume - perform port resume operation
2930 * @ap: port to resume
2935 * Kernel thread context (may sleep).
2937 static void ata_eh_handle_port_resume(struct ata_port *ap)
2939 unsigned long flags;
2942 /* are we resuming? */
2943 spin_lock_irqsave(ap->lock, flags);
2944 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2945 ap->pm_mesg.event != PM_EVENT_ON) {
2946 spin_unlock_irqrestore(ap->lock, flags);
2949 spin_unlock_irqrestore(ap->lock, flags);
2951 WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
2953 ata_acpi_set_state(ap, PMSG_ON);
2955 if (ap->ops->port_resume)
2956 rc = ap->ops->port_resume(ap);
2958 /* tell ACPI that we're resuming */
2959 ata_acpi_on_resume(ap);
2962 spin_lock_irqsave(ap->lock, flags);
2963 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
2964 if (ap->pm_result) {
2965 *ap->pm_result = rc;
2966 ap->pm_result = NULL;
2968 spin_unlock_irqrestore(ap->lock, flags);
2970 #endif /* CONFIG_PM */