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 <scsi/scsi.h>
37 #include <scsi/scsi_host.h>
38 #include <scsi/scsi_eh.h>
39 #include <scsi/scsi_device.h>
40 #include <scsi/scsi_cmnd.h>
41 #include "../scsi/scsi_transport_api.h"
43 #include <linux/libata.h>
48 ATA_EH_SPDN_NCQ_OFF = (1 << 0),
49 ATA_EH_SPDN_SPEED_DOWN = (1 << 1),
50 ATA_EH_SPDN_FALLBACK_TO_PIO = (1 << 2),
53 /* Waiting in ->prereset can never be reliable. It's sometimes nice
54 * to wait there but it can't be depended upon; otherwise, we wouldn't
55 * be resetting. Just give it enough time for most drives to spin up.
58 ATA_EH_PRERESET_TIMEOUT = 10 * HZ,
59 ATA_EH_FASTDRAIN_INTERVAL = 3 * HZ,
62 /* The following table determines how we sequence resets. Each entry
63 * represents timeout for that try. The first try can be soft or
64 * hardreset. All others are hardreset if available. In most cases
65 * the first reset w/ 10sec timeout should succeed. Following entries
66 * are mostly for error handling, hotplug and retarded devices.
68 static const unsigned long ata_eh_reset_timeouts[] = {
69 10 * HZ, /* most drives spin up by 10sec */
70 10 * HZ, /* > 99% working drives spin up before 20sec */
71 35 * HZ, /* give > 30 secs of idleness for retarded devices */
72 5 * HZ, /* and sweet one last chance */
73 /* > 1 min has elapsed, give up */
76 static void __ata_port_freeze(struct ata_port *ap);
78 static void ata_eh_handle_port_suspend(struct ata_port *ap);
79 static void ata_eh_handle_port_resume(struct ata_port *ap);
81 static void ata_eh_handle_port_suspend(struct ata_port *ap)
84 static void ata_eh_handle_port_resume(struct ata_port *ap)
86 #endif /* CONFIG_PM */
88 static void __ata_ehi_pushv_desc(struct ata_eh_info *ehi, const char *fmt,
91 ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
92 ATA_EH_DESC_LEN - ehi->desc_len,
97 * __ata_ehi_push_desc - push error description without adding separator
99 * @fmt: printf format string
101 * Format string according to @fmt and append it to @ehi->desc.
104 * spin_lock_irqsave(host lock)
106 void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
111 __ata_ehi_pushv_desc(ehi, fmt, args);
116 * ata_ehi_push_desc - push error description with separator
118 * @fmt: printf format string
120 * Format string according to @fmt and append it to @ehi->desc.
121 * If @ehi->desc is not empty, ", " is added in-between.
124 * spin_lock_irqsave(host lock)
126 void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
131 __ata_ehi_push_desc(ehi, ", ");
134 __ata_ehi_pushv_desc(ehi, fmt, args);
139 * ata_ehi_clear_desc - clean error description
145 * spin_lock_irqsave(host lock)
147 void ata_ehi_clear_desc(struct ata_eh_info *ehi)
154 * ata_port_desc - append port description
155 * @ap: target ATA port
156 * @fmt: printf format string
158 * Format string according to @fmt and append it to port
159 * description. If port description is not empty, " " is added
160 * in-between. This function is to be used while initializing
161 * ata_host. The description is printed on host registration.
166 void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
170 WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
172 if (ap->link.eh_info.desc_len)
173 __ata_ehi_push_desc(&ap->link.eh_info, " ");
176 __ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
183 * ata_port_pbar_desc - append PCI BAR description
184 * @ap: target ATA port
185 * @bar: target PCI BAR
186 * @offset: offset into PCI BAR
187 * @name: name of the area
189 * If @offset is negative, this function formats a string which
190 * contains the name, address, size and type of the BAR and
191 * appends it to the port description. If @offset is zero or
192 * positive, only name and offsetted address is appended.
197 void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
200 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
202 unsigned long long start, len;
204 if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
206 else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
209 start = (unsigned long long)pci_resource_start(pdev, bar);
210 len = (unsigned long long)pci_resource_len(pdev, bar);
213 ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
215 ata_port_desc(ap, "%s 0x%llx", name, start + offset);
218 #endif /* CONFIG_PCI */
220 static void ata_ering_record(struct ata_ering *ering, int is_io,
221 unsigned int err_mask)
223 struct ata_ering_entry *ent;
228 ering->cursor %= ATA_ERING_SIZE;
230 ent = &ering->ring[ering->cursor];
232 ent->err_mask = err_mask;
233 ent->timestamp = get_jiffies_64();
236 static void ata_ering_clear(struct ata_ering *ering)
238 memset(ering, 0, sizeof(*ering));
241 static int ata_ering_map(struct ata_ering *ering,
242 int (*map_fn)(struct ata_ering_entry *, void *),
246 struct ata_ering_entry *ent;
250 ent = &ering->ring[idx];
253 rc = map_fn(ent, arg);
256 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
257 } while (idx != ering->cursor);
262 static unsigned int ata_eh_dev_action(struct ata_device *dev)
264 struct ata_eh_context *ehc = &dev->link->eh_context;
266 return ehc->i.action | ehc->i.dev_action[dev->devno];
269 static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
270 struct ata_eh_info *ehi, unsigned int action)
272 struct ata_device *tdev;
275 ehi->action &= ~action;
276 ata_link_for_each_dev(tdev, link)
277 ehi->dev_action[tdev->devno] &= ~action;
279 /* doesn't make sense for port-wide EH actions */
280 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
282 /* break ehi->action into ehi->dev_action */
283 if (ehi->action & action) {
284 ata_link_for_each_dev(tdev, link)
285 ehi->dev_action[tdev->devno] |=
286 ehi->action & action;
287 ehi->action &= ~action;
290 /* turn off the specified per-dev action */
291 ehi->dev_action[dev->devno] &= ~action;
296 * ata_scsi_timed_out - SCSI layer time out callback
297 * @cmd: timed out SCSI command
299 * Handles SCSI layer timeout. We race with normal completion of
300 * the qc for @cmd. If the qc is already gone, we lose and let
301 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
302 * timed out and EH should be invoked. Prevent ata_qc_complete()
303 * from finishing it by setting EH_SCHEDULED and return
306 * TODO: kill this function once old EH is gone.
309 * Called from timer context
312 * EH_HANDLED or EH_NOT_HANDLED
314 enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
316 struct Scsi_Host *host = cmd->device->host;
317 struct ata_port *ap = ata_shost_to_port(host);
319 struct ata_queued_cmd *qc;
320 enum scsi_eh_timer_return ret;
324 if (ap->ops->error_handler) {
325 ret = EH_NOT_HANDLED;
330 spin_lock_irqsave(ap->lock, flags);
331 qc = ata_qc_from_tag(ap, ap->link.active_tag);
333 WARN_ON(qc->scsicmd != cmd);
334 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
335 qc->err_mask |= AC_ERR_TIMEOUT;
336 ret = EH_NOT_HANDLED;
338 spin_unlock_irqrestore(ap->lock, flags);
341 DPRINTK("EXIT, ret=%d\n", ret);
346 * ata_scsi_error - SCSI layer error handler callback
347 * @host: SCSI host on which error occurred
349 * Handles SCSI-layer-thrown error events.
352 * Inherited from SCSI layer (none, can sleep)
357 void ata_scsi_error(struct Scsi_Host *host)
359 struct ata_port *ap = ata_shost_to_port(host);
365 /* synchronize with port task */
366 ata_port_flush_task(ap);
368 /* synchronize with host lock and sort out timeouts */
370 /* For new EH, all qcs are finished in one of three ways -
371 * normal completion, error completion, and SCSI timeout.
372 * Both cmpletions can race against SCSI timeout. When normal
373 * completion wins, the qc never reaches EH. When error
374 * completion wins, the qc has ATA_QCFLAG_FAILED set.
376 * When SCSI timeout wins, things are a bit more complex.
377 * Normal or error completion can occur after the timeout but
378 * before this point. In such cases, both types of
379 * completions are honored. A scmd is determined to have
380 * timed out iff its associated qc is active and not failed.
382 if (ap->ops->error_handler) {
383 struct scsi_cmnd *scmd, *tmp;
386 spin_lock_irqsave(ap->lock, flags);
388 list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
389 struct ata_queued_cmd *qc;
391 for (i = 0; i < ATA_MAX_QUEUE; i++) {
392 qc = __ata_qc_from_tag(ap, i);
393 if (qc->flags & ATA_QCFLAG_ACTIVE &&
398 if (i < ATA_MAX_QUEUE) {
399 /* the scmd has an associated qc */
400 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
401 /* which hasn't failed yet, timeout */
402 qc->err_mask |= AC_ERR_TIMEOUT;
403 qc->flags |= ATA_QCFLAG_FAILED;
407 /* Normal completion occurred after
408 * SCSI timeout but before this point.
409 * Successfully complete it.
411 scmd->retries = scmd->allowed;
412 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
416 /* If we have timed out qcs. They belong to EH from
417 * this point but the state of the controller is
418 * unknown. Freeze the port to make sure the IRQ
419 * handler doesn't diddle with those qcs. This must
420 * be done atomically w.r.t. setting QCFLAG_FAILED.
423 __ata_port_freeze(ap);
425 spin_unlock_irqrestore(ap->lock, flags);
427 /* initialize eh_tries */
428 ap->eh_tries = ATA_EH_MAX_TRIES;
430 spin_unlock_wait(ap->lock);
433 /* invoke error handler */
434 if (ap->ops->error_handler) {
435 struct ata_link *link;
437 /* kill fast drain timer */
438 del_timer_sync(&ap->fastdrain_timer);
440 /* process port resume request */
441 ata_eh_handle_port_resume(ap);
443 /* fetch & clear EH info */
444 spin_lock_irqsave(ap->lock, flags);
446 __ata_port_for_each_link(link, ap) {
447 memset(&link->eh_context, 0, sizeof(link->eh_context));
448 link->eh_context.i = link->eh_info;
449 memset(&link->eh_info, 0, sizeof(link->eh_info));
452 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
453 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
454 ap->excl_link = NULL; /* don't maintain exclusion over EH */
456 spin_unlock_irqrestore(ap->lock, flags);
458 /* invoke EH, skip if unloading or suspended */
459 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
460 ap->ops->error_handler(ap);
464 /* process port suspend request */
465 ata_eh_handle_port_suspend(ap);
467 /* Exception might have happend after ->error_handler
468 * recovered the port but before this point. Repeat
471 spin_lock_irqsave(ap->lock, flags);
473 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
474 if (--ap->eh_tries) {
475 spin_unlock_irqrestore(ap->lock, flags);
478 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
479 "tries, giving up\n", ATA_EH_MAX_TRIES);
480 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
483 /* this run is complete, make sure EH info is clear */
484 __ata_port_for_each_link(link, ap)
485 memset(&link->eh_info, 0, sizeof(link->eh_info));
487 /* Clear host_eh_scheduled while holding ap->lock such
488 * that if exception occurs after this point but
489 * before EH completion, SCSI midlayer will
492 host->host_eh_scheduled = 0;
494 spin_unlock_irqrestore(ap->lock, flags);
496 WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
497 ap->ops->eng_timeout(ap);
500 /* finish or retry handled scmd's and clean up */
501 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
503 scsi_eh_flush_done_q(&ap->eh_done_q);
506 spin_lock_irqsave(ap->lock, flags);
508 if (ap->pflags & ATA_PFLAG_LOADING)
509 ap->pflags &= ~ATA_PFLAG_LOADING;
510 else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
511 queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0);
513 if (ap->pflags & ATA_PFLAG_RECOVERED)
514 ata_port_printk(ap, KERN_INFO, "EH complete\n");
516 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
518 /* tell wait_eh that we're done */
519 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
520 wake_up_all(&ap->eh_wait_q);
522 spin_unlock_irqrestore(ap->lock, flags);
528 * ata_port_wait_eh - Wait for the currently pending EH to complete
529 * @ap: Port to wait EH for
531 * Wait until the currently pending EH is complete.
534 * Kernel thread context (may sleep).
536 void ata_port_wait_eh(struct ata_port *ap)
542 spin_lock_irqsave(ap->lock, flags);
544 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
545 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
546 spin_unlock_irqrestore(ap->lock, flags);
548 spin_lock_irqsave(ap->lock, flags);
550 finish_wait(&ap->eh_wait_q, &wait);
552 spin_unlock_irqrestore(ap->lock, flags);
554 /* make sure SCSI EH is complete */
555 if (scsi_host_in_recovery(ap->scsi_host)) {
562 * ata_qc_timeout - Handle timeout of queued command
563 * @qc: Command that timed out
565 * Some part of the kernel (currently, only the SCSI layer)
566 * has noticed that the active command on port @ap has not
567 * completed after a specified length of time. Handle this
568 * condition by disabling DMA (if necessary) and completing
569 * transactions, with error if necessary.
571 * This also handles the case of the "lost interrupt", where
572 * for some reason (possibly hardware bug, possibly driver bug)
573 * an interrupt was not delivered to the driver, even though the
574 * transaction completed successfully.
576 * TODO: kill this function once old EH is gone.
579 * Inherited from SCSI layer (none, can sleep)
581 static void ata_qc_timeout(struct ata_queued_cmd *qc)
583 struct ata_port *ap = qc->ap;
584 u8 host_stat = 0, drv_stat;
589 ap->hsm_task_state = HSM_ST_IDLE;
591 spin_lock_irqsave(ap->lock, flags);
593 switch (qc->tf.protocol) {
596 case ATA_PROT_ATAPI_DMA:
597 host_stat = ap->ops->bmdma_status(ap);
599 /* before we do anything else, clear DMA-Start bit */
600 ap->ops->bmdma_stop(qc);
606 drv_stat = ata_chk_status(ap);
608 /* ack bmdma irq events */
609 ap->ops->irq_clear(ap);
611 ata_dev_printk(qc->dev, KERN_ERR, "command 0x%x timeout, "
612 "stat 0x%x host_stat 0x%x\n",
613 qc->tf.command, drv_stat, host_stat);
615 /* complete taskfile transaction */
616 qc->err_mask |= AC_ERR_TIMEOUT;
620 spin_unlock_irqrestore(ap->lock, flags);
622 ata_eh_qc_complete(qc);
628 * ata_eng_timeout - Handle timeout of queued command
629 * @ap: Port on which timed-out command is active
631 * Some part of the kernel (currently, only the SCSI layer)
632 * has noticed that the active command on port @ap has not
633 * completed after a specified length of time. Handle this
634 * condition by disabling DMA (if necessary) and completing
635 * transactions, with error if necessary.
637 * This also handles the case of the "lost interrupt", where
638 * for some reason (possibly hardware bug, possibly driver bug)
639 * an interrupt was not delivered to the driver, even though the
640 * transaction completed successfully.
642 * TODO: kill this function once old EH is gone.
645 * Inherited from SCSI layer (none, can sleep)
647 void ata_eng_timeout(struct ata_port *ap)
651 ata_qc_timeout(ata_qc_from_tag(ap, ap->link.active_tag));
656 static int ata_eh_nr_in_flight(struct ata_port *ap)
661 /* count only non-internal commands */
662 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++)
663 if (ata_qc_from_tag(ap, tag))
669 void ata_eh_fastdrain_timerfn(unsigned long arg)
671 struct ata_port *ap = (void *)arg;
675 spin_lock_irqsave(ap->lock, flags);
677 cnt = ata_eh_nr_in_flight(ap);
683 if (cnt == ap->fastdrain_cnt) {
686 /* No progress during the last interval, tag all
687 * in-flight qcs as timed out and freeze the port.
689 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) {
690 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
692 qc->err_mask |= AC_ERR_TIMEOUT;
697 /* some qcs have finished, give it another chance */
698 ap->fastdrain_cnt = cnt;
699 ap->fastdrain_timer.expires =
700 jiffies + ATA_EH_FASTDRAIN_INTERVAL;
701 add_timer(&ap->fastdrain_timer);
705 spin_unlock_irqrestore(ap->lock, flags);
709 * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
710 * @ap: target ATA port
711 * @fastdrain: activate fast drain
713 * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
714 * is non-zero and EH wasn't pending before. Fast drain ensures
715 * that EH kicks in in timely manner.
718 * spin_lock_irqsave(host lock)
720 static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
724 /* already scheduled? */
725 if (ap->pflags & ATA_PFLAG_EH_PENDING)
728 ap->pflags |= ATA_PFLAG_EH_PENDING;
733 /* do we have in-flight qcs? */
734 cnt = ata_eh_nr_in_flight(ap);
738 /* activate fast drain */
739 ap->fastdrain_cnt = cnt;
740 ap->fastdrain_timer.expires = jiffies + ATA_EH_FASTDRAIN_INTERVAL;
741 add_timer(&ap->fastdrain_timer);
745 * ata_qc_schedule_eh - schedule qc for error handling
746 * @qc: command to schedule error handling for
748 * Schedule error handling for @qc. EH will kick in as soon as
749 * other commands are drained.
752 * spin_lock_irqsave(host lock)
754 void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
756 struct ata_port *ap = qc->ap;
758 WARN_ON(!ap->ops->error_handler);
760 qc->flags |= ATA_QCFLAG_FAILED;
761 ata_eh_set_pending(ap, 1);
763 /* The following will fail if timeout has already expired.
764 * ata_scsi_error() takes care of such scmds on EH entry.
765 * Note that ATA_QCFLAG_FAILED is unconditionally set after
766 * this function completes.
768 scsi_req_abort_cmd(qc->scsicmd);
772 * ata_port_schedule_eh - schedule error handling without a qc
773 * @ap: ATA port to schedule EH for
775 * Schedule error handling for @ap. EH will kick in as soon as
776 * all commands are drained.
779 * spin_lock_irqsave(host lock)
781 void ata_port_schedule_eh(struct ata_port *ap)
783 WARN_ON(!ap->ops->error_handler);
785 if (ap->pflags & ATA_PFLAG_INITIALIZING)
788 ata_eh_set_pending(ap, 1);
789 scsi_schedule_eh(ap->scsi_host);
791 DPRINTK("port EH scheduled\n");
794 static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
796 int tag, nr_aborted = 0;
798 WARN_ON(!ap->ops->error_handler);
800 /* we're gonna abort all commands, no need for fast drain */
801 ata_eh_set_pending(ap, 0);
803 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
804 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
806 if (qc && (!link || qc->dev->link == link)) {
807 qc->flags |= ATA_QCFLAG_FAILED;
814 ata_port_schedule_eh(ap);
820 * ata_link_abort - abort all qc's on the link
821 * @link: ATA link to abort qc's for
823 * Abort all active qc's active on @link and schedule EH.
826 * spin_lock_irqsave(host lock)
829 * Number of aborted qc's.
831 int ata_link_abort(struct ata_link *link)
833 return ata_do_link_abort(link->ap, link);
837 * ata_port_abort - abort all qc's on the port
838 * @ap: ATA port to abort qc's for
840 * Abort all active qc's of @ap and schedule EH.
843 * spin_lock_irqsave(host_set lock)
846 * Number of aborted qc's.
848 int ata_port_abort(struct ata_port *ap)
850 return ata_do_link_abort(ap, NULL);
854 * __ata_port_freeze - freeze port
855 * @ap: ATA port to freeze
857 * This function is called when HSM violation or some other
858 * condition disrupts normal operation of the port. Frozen port
859 * is not allowed to perform any operation until the port is
860 * thawed, which usually follows a successful reset.
862 * ap->ops->freeze() callback can be used for freezing the port
863 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
864 * port cannot be frozen hardware-wise, the interrupt handler
865 * must ack and clear interrupts unconditionally while the port
869 * spin_lock_irqsave(host lock)
871 static void __ata_port_freeze(struct ata_port *ap)
873 WARN_ON(!ap->ops->error_handler);
878 ap->pflags |= ATA_PFLAG_FROZEN;
880 DPRINTK("ata%u port frozen\n", ap->print_id);
884 * ata_port_freeze - abort & freeze port
885 * @ap: ATA port to freeze
887 * Abort and freeze @ap.
890 * spin_lock_irqsave(host lock)
893 * Number of aborted commands.
895 int ata_port_freeze(struct ata_port *ap)
899 WARN_ON(!ap->ops->error_handler);
901 nr_aborted = ata_port_abort(ap);
902 __ata_port_freeze(ap);
908 * sata_async_notification - SATA async notification handler
909 * @ap: ATA port where async notification is received
911 * Handler to be called when async notification via SDB FIS is
912 * received. This function schedules EH if necessary.
915 * spin_lock_irqsave(host lock)
918 * 1 if EH is scheduled, 0 otherwise.
920 int sata_async_notification(struct ata_port *ap)
925 if (!(ap->flags & ATA_FLAG_AN))
928 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
930 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
932 if (!ap->nr_pmp_links || rc) {
933 /* PMP is not attached or SNTF is not available */
934 if (!ap->nr_pmp_links) {
935 /* PMP is not attached. Check whether ATAPI
936 * AN is configured. If so, notify media
939 struct ata_device *dev = ap->link.device;
941 if ((dev->class == ATA_DEV_ATAPI) &&
942 (dev->flags & ATA_DFLAG_AN))
943 ata_scsi_media_change_notify(dev);
946 /* PMP is attached but SNTF is not available.
947 * ATAPI async media change notification is
948 * not used. The PMP must be reporting PHY
949 * status change, schedule EH.
951 ata_port_schedule_eh(ap);
955 /* PMP is attached and SNTF is available */
956 struct ata_link *link;
958 /* check and notify ATAPI AN */
959 ata_port_for_each_link(link, ap) {
960 if (!(sntf & (1 << link->pmp)))
963 if ((link->device->class == ATA_DEV_ATAPI) &&
964 (link->device->flags & ATA_DFLAG_AN))
965 ata_scsi_media_change_notify(link->device);
968 /* If PMP is reporting that PHY status of some
969 * downstream ports has changed, schedule EH.
971 if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
972 ata_port_schedule_eh(ap);
981 * ata_eh_freeze_port - EH helper to freeze port
982 * @ap: ATA port to freeze
989 void ata_eh_freeze_port(struct ata_port *ap)
993 if (!ap->ops->error_handler)
996 spin_lock_irqsave(ap->lock, flags);
997 __ata_port_freeze(ap);
998 spin_unlock_irqrestore(ap->lock, flags);
1002 * ata_port_thaw_port - EH helper to thaw port
1003 * @ap: ATA port to thaw
1005 * Thaw frozen port @ap.
1010 void ata_eh_thaw_port(struct ata_port *ap)
1012 unsigned long flags;
1014 if (!ap->ops->error_handler)
1017 spin_lock_irqsave(ap->lock, flags);
1019 ap->pflags &= ~ATA_PFLAG_FROZEN;
1024 spin_unlock_irqrestore(ap->lock, flags);
1026 DPRINTK("ata%u port thawed\n", ap->print_id);
1029 static void ata_eh_scsidone(struct scsi_cmnd *scmd)
1034 static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
1036 struct ata_port *ap = qc->ap;
1037 struct scsi_cmnd *scmd = qc->scsicmd;
1038 unsigned long flags;
1040 spin_lock_irqsave(ap->lock, flags);
1041 qc->scsidone = ata_eh_scsidone;
1042 __ata_qc_complete(qc);
1043 WARN_ON(ata_tag_valid(qc->tag));
1044 spin_unlock_irqrestore(ap->lock, flags);
1046 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
1050 * ata_eh_qc_complete - Complete an active ATA command from EH
1051 * @qc: Command to complete
1053 * Indicate to the mid and upper layers that an ATA command has
1054 * completed. To be used from EH.
1056 void ata_eh_qc_complete(struct ata_queued_cmd *qc)
1058 struct scsi_cmnd *scmd = qc->scsicmd;
1059 scmd->retries = scmd->allowed;
1060 __ata_eh_qc_complete(qc);
1064 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
1065 * @qc: Command to retry
1067 * Indicate to the mid and upper layers that an ATA command
1068 * should be retried. To be used from EH.
1070 * SCSI midlayer limits the number of retries to scmd->allowed.
1071 * scmd->retries is decremented for commands which get retried
1072 * due to unrelated failures (qc->err_mask is zero).
1074 void ata_eh_qc_retry(struct ata_queued_cmd *qc)
1076 struct scsi_cmnd *scmd = qc->scsicmd;
1077 if (!qc->err_mask && scmd->retries)
1079 __ata_eh_qc_complete(qc);
1083 * ata_eh_detach_dev - detach ATA device
1084 * @dev: ATA device to detach
1091 void ata_eh_detach_dev(struct ata_device *dev)
1093 struct ata_link *link = dev->link;
1094 struct ata_port *ap = link->ap;
1095 unsigned long flags;
1097 ata_dev_disable(dev);
1099 spin_lock_irqsave(ap->lock, flags);
1101 dev->flags &= ~ATA_DFLAG_DETACH;
1103 if (ata_scsi_offline_dev(dev)) {
1104 dev->flags |= ATA_DFLAG_DETACHED;
1105 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
1108 /* clear per-dev EH actions */
1109 ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
1110 ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
1112 spin_unlock_irqrestore(ap->lock, flags);
1116 * ata_eh_about_to_do - about to perform eh_action
1117 * @link: target ATA link
1118 * @dev: target ATA dev for per-dev action (can be NULL)
1119 * @action: action about to be performed
1121 * Called just before performing EH actions to clear related bits
1122 * in @link->eh_info such that eh actions are not unnecessarily
1128 void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
1129 unsigned int action)
1131 struct ata_port *ap = link->ap;
1132 struct ata_eh_info *ehi = &link->eh_info;
1133 struct ata_eh_context *ehc = &link->eh_context;
1134 unsigned long flags;
1136 spin_lock_irqsave(ap->lock, flags);
1138 /* Reset is represented by combination of actions and EHI
1139 * flags. Suck in all related bits before clearing eh_info to
1140 * avoid losing requested action.
1142 if (action & ATA_EH_RESET_MASK) {
1143 ehc->i.action |= ehi->action & ATA_EH_RESET_MASK;
1144 ehc->i.flags |= ehi->flags & ATA_EHI_RESET_MODIFIER_MASK;
1146 /* make sure all reset actions are cleared & clear EHI flags */
1147 action |= ATA_EH_RESET_MASK;
1148 ehi->flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
1151 ata_eh_clear_action(link, dev, ehi, action);
1153 if (!(ehc->i.flags & ATA_EHI_QUIET))
1154 ap->pflags |= ATA_PFLAG_RECOVERED;
1156 spin_unlock_irqrestore(ap->lock, flags);
1160 * ata_eh_done - EH action complete
1161 * @ap: target ATA port
1162 * @dev: target ATA dev for per-dev action (can be NULL)
1163 * @action: action just completed
1165 * Called right after performing EH actions to clear related bits
1166 * in @link->eh_context.
1171 void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1172 unsigned int action)
1174 struct ata_eh_context *ehc = &link->eh_context;
1176 /* if reset is complete, clear all reset actions & reset modifier */
1177 if (action & ATA_EH_RESET_MASK) {
1178 action |= ATA_EH_RESET_MASK;
1179 ehc->i.flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
1182 ata_eh_clear_action(link, dev, &ehc->i, action);
1186 * ata_err_string - convert err_mask to descriptive string
1187 * @err_mask: error mask to convert to string
1189 * Convert @err_mask to descriptive string. Errors are
1190 * prioritized according to severity and only the most severe
1191 * error is reported.
1197 * Descriptive string for @err_mask
1199 static const char * ata_err_string(unsigned int err_mask)
1201 if (err_mask & AC_ERR_HOST_BUS)
1202 return "host bus error";
1203 if (err_mask & AC_ERR_ATA_BUS)
1204 return "ATA bus error";
1205 if (err_mask & AC_ERR_TIMEOUT)
1207 if (err_mask & AC_ERR_HSM)
1208 return "HSM violation";
1209 if (err_mask & AC_ERR_SYSTEM)
1210 return "internal error";
1211 if (err_mask & AC_ERR_MEDIA)
1212 return "media error";
1213 if (err_mask & AC_ERR_INVALID)
1214 return "invalid argument";
1215 if (err_mask & AC_ERR_DEV)
1216 return "device error";
1217 return "unknown error";
1221 * ata_read_log_page - read a specific log page
1222 * @dev: target device
1223 * @page: page to read
1224 * @buf: buffer to store read page
1225 * @sectors: number of sectors to read
1227 * Read log page using READ_LOG_EXT command.
1230 * Kernel thread context (may sleep).
1233 * 0 on success, AC_ERR_* mask otherwise.
1235 static unsigned int ata_read_log_page(struct ata_device *dev,
1236 u8 page, void *buf, unsigned int sectors)
1238 struct ata_taskfile tf;
1239 unsigned int err_mask;
1241 DPRINTK("read log page - page %d\n", page);
1243 ata_tf_init(dev, &tf);
1244 tf.command = ATA_CMD_READ_LOG_EXT;
1247 tf.hob_nsect = sectors >> 8;
1248 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1249 tf.protocol = ATA_PROT_PIO;
1251 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
1252 buf, sectors * ATA_SECT_SIZE);
1254 DPRINTK("EXIT, err_mask=%x\n", err_mask);
1259 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
1260 * @dev: Device to read log page 10h from
1261 * @tag: Resulting tag of the failed command
1262 * @tf: Resulting taskfile registers of the failed command
1264 * Read log page 10h to obtain NCQ error details and clear error
1268 * Kernel thread context (may sleep).
1271 * 0 on success, -errno otherwise.
1273 static int ata_eh_read_log_10h(struct ata_device *dev,
1274 int *tag, struct ata_taskfile *tf)
1276 u8 *buf = dev->link->ap->sector_buf;
1277 unsigned int err_mask;
1281 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
1286 for (i = 0; i < ATA_SECT_SIZE; i++)
1289 ata_dev_printk(dev, KERN_WARNING,
1290 "invalid checksum 0x%x on log page 10h\n", csum);
1295 *tag = buf[0] & 0x1f;
1297 tf->command = buf[2];
1298 tf->feature = buf[3];
1302 tf->device = buf[7];
1303 tf->hob_lbal = buf[8];
1304 tf->hob_lbam = buf[9];
1305 tf->hob_lbah = buf[10];
1306 tf->nsect = buf[12];
1307 tf->hob_nsect = buf[13];
1313 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1314 * @dev: device to perform REQUEST_SENSE to
1315 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
1317 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
1318 * SENSE. This function is EH helper.
1321 * Kernel thread context (may sleep).
1324 * 0 on success, AC_ERR_* mask on failure
1326 static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc)
1328 struct ata_device *dev = qc->dev;
1329 unsigned char *sense_buf = qc->scsicmd->sense_buffer;
1330 struct ata_port *ap = dev->link->ap;
1331 struct ata_taskfile tf;
1332 u8 cdb[ATAPI_CDB_LEN];
1334 DPRINTK("ATAPI request sense\n");
1336 /* FIXME: is this needed? */
1337 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1339 /* initialize sense_buf with the error register,
1340 * for the case where they are -not- overwritten
1342 sense_buf[0] = 0x70;
1343 sense_buf[2] = qc->result_tf.feature >> 4;
1345 /* some devices time out if garbage left in tf */
1346 ata_tf_init(dev, &tf);
1348 memset(cdb, 0, ATAPI_CDB_LEN);
1349 cdb[0] = REQUEST_SENSE;
1350 cdb[4] = SCSI_SENSE_BUFFERSIZE;
1352 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1353 tf.command = ATA_CMD_PACKET;
1355 /* is it pointless to prefer PIO for "safety reasons"? */
1356 if (ap->flags & ATA_FLAG_PIO_DMA) {
1357 tf.protocol = ATA_PROT_ATAPI_DMA;
1358 tf.feature |= ATAPI_PKT_DMA;
1360 tf.protocol = ATA_PROT_ATAPI;
1361 tf.lbam = (8 * 1024) & 0xff;
1362 tf.lbah = (8 * 1024) >> 8;
1365 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
1366 sense_buf, SCSI_SENSE_BUFFERSIZE);
1370 * ata_eh_analyze_serror - analyze SError for a failed port
1371 * @link: ATA link to analyze SError for
1373 * Analyze SError if available and further determine cause of
1379 static void ata_eh_analyze_serror(struct ata_link *link)
1381 struct ata_eh_context *ehc = &link->eh_context;
1382 u32 serror = ehc->i.serror;
1383 unsigned int err_mask = 0, action = 0;
1386 if (serror & SERR_PERSISTENT) {
1387 err_mask |= AC_ERR_ATA_BUS;
1388 action |= ATA_EH_HARDRESET;
1391 (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
1392 err_mask |= AC_ERR_ATA_BUS;
1393 action |= ATA_EH_SOFTRESET;
1395 if (serror & SERR_PROTOCOL) {
1396 err_mask |= AC_ERR_HSM;
1397 action |= ATA_EH_SOFTRESET;
1399 if (serror & SERR_INTERNAL) {
1400 err_mask |= AC_ERR_SYSTEM;
1401 action |= ATA_EH_HARDRESET;
1404 /* Determine whether a hotplug event has occurred. Both
1405 * SError.N/X are considered hotplug events for enabled or
1406 * host links. For disabled PMP links, only N bit is
1407 * considered as X bit is left at 1 for link plugging.
1411 if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
1412 hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
1414 hotplug_mask = SERR_PHYRDY_CHG;
1416 if (serror & hotplug_mask)
1417 ata_ehi_hotplugged(&ehc->i);
1419 ehc->i.err_mask |= err_mask;
1420 ehc->i.action |= action;
1424 * ata_eh_analyze_ncq_error - analyze NCQ error
1425 * @link: ATA link to analyze NCQ error for
1427 * Read log page 10h, determine the offending qc and acquire
1428 * error status TF. For NCQ device errors, all LLDDs have to do
1429 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1433 * Kernel thread context (may sleep).
1435 static void ata_eh_analyze_ncq_error(struct ata_link *link)
1437 struct ata_port *ap = link->ap;
1438 struct ata_eh_context *ehc = &link->eh_context;
1439 struct ata_device *dev = link->device;
1440 struct ata_queued_cmd *qc;
1441 struct ata_taskfile tf;
1444 /* if frozen, we can't do much */
1445 if (ap->pflags & ATA_PFLAG_FROZEN)
1448 /* is it NCQ device error? */
1449 if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1452 /* has LLDD analyzed already? */
1453 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1454 qc = __ata_qc_from_tag(ap, tag);
1456 if (!(qc->flags & ATA_QCFLAG_FAILED))
1463 /* okay, this error is ours */
1464 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1466 ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
1467 "(errno=%d)\n", rc);
1471 if (!(link->sactive & (1 << tag))) {
1472 ata_link_printk(link, KERN_ERR, "log page 10h reported "
1473 "inactive tag %d\n", tag);
1477 /* we've got the perpetrator, condemn it */
1478 qc = __ata_qc_from_tag(ap, tag);
1479 memcpy(&qc->result_tf, &tf, sizeof(tf));
1480 qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
1481 ehc->i.err_mask &= ~AC_ERR_DEV;
1485 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1486 * @qc: qc to analyze
1487 * @tf: Taskfile registers to analyze
1489 * Analyze taskfile of @qc and further determine cause of
1490 * failure. This function also requests ATAPI sense data if
1494 * Kernel thread context (may sleep).
1497 * Determined recovery action
1499 static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1500 const struct ata_taskfile *tf)
1502 unsigned int tmp, action = 0;
1503 u8 stat = tf->command, err = tf->feature;
1505 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1506 qc->err_mask |= AC_ERR_HSM;
1507 return ATA_EH_SOFTRESET;
1510 if (stat & (ATA_ERR | ATA_DF))
1511 qc->err_mask |= AC_ERR_DEV;
1515 switch (qc->dev->class) {
1518 qc->err_mask |= AC_ERR_ATA_BUS;
1520 qc->err_mask |= AC_ERR_MEDIA;
1522 qc->err_mask |= AC_ERR_INVALID;
1526 if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
1527 tmp = atapi_eh_request_sense(qc);
1529 /* ATA_QCFLAG_SENSE_VALID is used to
1530 * tell atapi_qc_complete() that sense
1531 * data is already valid.
1533 * TODO: interpret sense data and set
1534 * appropriate err_mask.
1536 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1538 qc->err_mask |= tmp;
1542 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1543 action |= ATA_EH_SOFTRESET;
1548 static int ata_eh_categorize_error(int is_io, unsigned int err_mask)
1550 if (err_mask & AC_ERR_ATA_BUS)
1553 if (err_mask & AC_ERR_TIMEOUT)
1557 if (err_mask & AC_ERR_HSM)
1560 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1567 struct speed_down_verdict_arg {
1572 static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
1574 struct speed_down_verdict_arg *arg = void_arg;
1575 int cat = ata_eh_categorize_error(ent->is_io, ent->err_mask);
1577 if (ent->timestamp < arg->since)
1580 arg->nr_errors[cat]++;
1585 * ata_eh_speed_down_verdict - Determine speed down verdict
1586 * @dev: Device of interest
1588 * This function examines error ring of @dev and determines
1589 * whether NCQ needs to be turned off, transfer speed should be
1590 * stepped down, or falling back to PIO is necessary.
1592 * Cat-1 is ATA_BUS error for any command.
1594 * Cat-2 is TIMEOUT for any command or HSM violation for known
1595 * supported commands.
1597 * Cat-3 is is unclassified DEV error for known supported
1600 * NCQ needs to be turned off if there have been more than 3
1601 * Cat-2 + Cat-3 errors during last 10 minutes.
1603 * Speed down is necessary if there have been more than 3 Cat-1 +
1604 * Cat-2 errors or 10 Cat-3 errors during last 10 minutes.
1606 * Falling back to PIO mode is necessary if there have been more
1607 * than 10 Cat-1 + Cat-2 + Cat-3 errors during last 5 minutes.
1610 * Inherited from caller.
1613 * OR of ATA_EH_SPDN_* flags.
1615 static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
1617 const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
1618 u64 j64 = get_jiffies_64();
1619 struct speed_down_verdict_arg arg;
1620 unsigned int verdict = 0;
1622 /* scan past 10 mins of error history */
1623 memset(&arg, 0, sizeof(arg));
1624 arg.since = j64 - min(j64, j10mins);
1625 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1627 if (arg.nr_errors[2] + arg.nr_errors[3] > 3)
1628 verdict |= ATA_EH_SPDN_NCQ_OFF;
1629 if (arg.nr_errors[1] + arg.nr_errors[2] > 3 || arg.nr_errors[3] > 10)
1630 verdict |= ATA_EH_SPDN_SPEED_DOWN;
1632 /* scan past 3 mins of error history */
1633 memset(&arg, 0, sizeof(arg));
1634 arg.since = j64 - min(j64, j5mins);
1635 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1637 if (arg.nr_errors[1] + arg.nr_errors[2] + arg.nr_errors[3] > 10)
1638 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
1644 * ata_eh_speed_down - record error and speed down if necessary
1645 * @dev: Failed device
1646 * @is_io: Did the device fail during normal IO?
1647 * @err_mask: err_mask of the error
1649 * Record error and examine error history to determine whether
1650 * adjusting transmission speed is necessary. It also sets
1651 * transmission limits appropriately if such adjustment is
1655 * Kernel thread context (may sleep).
1658 * Determined recovery action.
1660 static unsigned int ata_eh_speed_down(struct ata_device *dev, int is_io,
1661 unsigned int err_mask)
1663 unsigned int verdict;
1664 unsigned int action = 0;
1666 /* don't bother if Cat-0 error */
1667 if (ata_eh_categorize_error(is_io, err_mask) == 0)
1670 /* record error and determine whether speed down is necessary */
1671 ata_ering_record(&dev->ering, is_io, err_mask);
1672 verdict = ata_eh_speed_down_verdict(dev);
1675 if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
1676 (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
1677 ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
1678 dev->flags |= ATA_DFLAG_NCQ_OFF;
1679 ata_dev_printk(dev, KERN_WARNING,
1680 "NCQ disabled due to excessive errors\n");
1685 if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1686 /* speed down SATA link speed if possible */
1687 if (sata_down_spd_limit(dev->link) == 0) {
1688 action |= ATA_EH_HARDRESET;
1692 /* lower transfer mode */
1693 if (dev->spdn_cnt < 2) {
1694 static const int dma_dnxfer_sel[] =
1695 { ATA_DNXFER_DMA, ATA_DNXFER_40C };
1696 static const int pio_dnxfer_sel[] =
1697 { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
1700 if (dev->xfer_shift != ATA_SHIFT_PIO)
1701 sel = dma_dnxfer_sel[dev->spdn_cnt];
1703 sel = pio_dnxfer_sel[dev->spdn_cnt];
1707 if (ata_down_xfermask_limit(dev, sel) == 0) {
1708 action |= ATA_EH_SOFTRESET;
1714 /* Fall back to PIO? Slowing down to PIO is meaningless for
1715 * SATA. Consider it only for PATA.
1717 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
1718 (dev->link->ap->cbl != ATA_CBL_SATA) &&
1719 (dev->xfer_shift != ATA_SHIFT_PIO)) {
1720 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
1722 action |= ATA_EH_SOFTRESET;
1729 /* device has been slowed down, blow error history */
1730 ata_ering_clear(&dev->ering);
1735 * ata_eh_link_autopsy - analyze error and determine recovery action
1736 * @link: host link to perform autopsy on
1738 * Analyze why @link failed and determine which recovery actions
1739 * are needed. This function also sets more detailed AC_ERR_*
1740 * values and fills sense data for ATAPI CHECK SENSE.
1743 * Kernel thread context (may sleep).
1745 static void ata_eh_link_autopsy(struct ata_link *link)
1747 struct ata_port *ap = link->ap;
1748 struct ata_eh_context *ehc = &link->eh_context;
1749 unsigned int all_err_mask = 0;
1756 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1759 /* obtain and analyze SError */
1760 rc = sata_scr_read(link, SCR_ERROR, &serror);
1762 ehc->i.serror |= serror;
1763 ata_eh_analyze_serror(link);
1764 } else if (rc != -EOPNOTSUPP) {
1765 /* SError read failed, force hardreset and probing */
1766 ata_ehi_schedule_probe(&ehc->i);
1767 ehc->i.action |= ATA_EH_HARDRESET;
1768 ehc->i.err_mask |= AC_ERR_OTHER;
1771 /* analyze NCQ failure */
1772 ata_eh_analyze_ncq_error(link);
1774 /* any real error trumps AC_ERR_OTHER */
1775 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1776 ehc->i.err_mask &= ~AC_ERR_OTHER;
1778 all_err_mask |= ehc->i.err_mask;
1780 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1781 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1783 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
1786 /* inherit upper level err_mask */
1787 qc->err_mask |= ehc->i.err_mask;
1790 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1792 /* DEV errors are probably spurious in case of ATA_BUS error */
1793 if (qc->err_mask & AC_ERR_ATA_BUS)
1794 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1797 /* any real error trumps unknown error */
1798 if (qc->err_mask & ~AC_ERR_OTHER)
1799 qc->err_mask &= ~AC_ERR_OTHER;
1801 /* SENSE_VALID trumps dev/unknown error and revalidation */
1802 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1803 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1804 ehc->i.action &= ~ATA_EH_REVALIDATE;
1807 /* accumulate error info */
1808 ehc->i.dev = qc->dev;
1809 all_err_mask |= qc->err_mask;
1810 if (qc->flags & ATA_QCFLAG_IO)
1814 /* enforce default EH actions */
1815 if (ap->pflags & ATA_PFLAG_FROZEN ||
1816 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
1817 ehc->i.action |= ATA_EH_SOFTRESET;
1818 else if (all_err_mask)
1819 ehc->i.action |= ATA_EH_REVALIDATE;
1821 /* if we have offending qcs and the associated failed device */
1824 ehc->i.action |= ata_eh_speed_down(ehc->i.dev, is_io,
1827 /* perform per-dev EH action only on the offending device */
1828 ehc->i.dev_action[ehc->i.dev->devno] |=
1829 ehc->i.action & ATA_EH_PERDEV_MASK;
1830 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
1837 * ata_eh_autopsy - analyze error and determine recovery action
1838 * @ap: host port to perform autopsy on
1840 * Analyze all links of @ap and determine why they failed and
1841 * which recovery actions are needed.
1844 * Kernel thread context (may sleep).
1846 void ata_eh_autopsy(struct ata_port *ap)
1848 struct ata_link *link;
1850 __ata_port_for_each_link(link, ap)
1851 ata_eh_link_autopsy(link);
1855 * ata_eh_link_report - report error handling to user
1856 * @link: ATA link EH is going on
1858 * Report EH to user.
1863 static void ata_eh_link_report(struct ata_link *link)
1865 struct ata_port *ap = link->ap;
1866 struct ata_eh_context *ehc = &link->eh_context;
1867 const char *frozen, *desc;
1869 int tag, nr_failed = 0;
1872 if (ehc->i.desc[0] != '\0')
1875 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1876 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1878 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
1880 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1886 if (!nr_failed && !ehc->i.err_mask)
1890 if (ap->pflags & ATA_PFLAG_FROZEN)
1893 memset(tries_buf, 0, sizeof(tries_buf));
1894 if (ap->eh_tries < ATA_EH_MAX_TRIES)
1895 snprintf(tries_buf, sizeof(tries_buf) - 1, " t%d",
1899 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1900 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1901 ehc->i.err_mask, link->sactive, ehc->i.serror,
1902 ehc->i.action, frozen, tries_buf);
1904 ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
1906 ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
1907 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1908 ehc->i.err_mask, link->sactive, ehc->i.serror,
1909 ehc->i.action, frozen, tries_buf);
1911 ata_link_printk(link, KERN_ERR, "%s\n", desc);
1914 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1915 static const char *dma_str[] = {
1916 [DMA_BIDIRECTIONAL] = "bidi",
1917 [DMA_TO_DEVICE] = "out",
1918 [DMA_FROM_DEVICE] = "in",
1921 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1922 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
1924 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
1925 qc->dev->link != link || !qc->err_mask)
1928 ata_dev_printk(qc->dev, KERN_ERR,
1929 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
1930 "tag %d cdb 0x%x data %u %s\n "
1931 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
1932 "Emask 0x%x (%s)%s\n",
1933 cmd->command, cmd->feature, cmd->nsect,
1934 cmd->lbal, cmd->lbam, cmd->lbah,
1935 cmd->hob_feature, cmd->hob_nsect,
1936 cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
1937 cmd->device, qc->tag, qc->cdb[0], qc->nbytes,
1938 dma_str[qc->dma_dir],
1939 res->command, res->feature, res->nsect,
1940 res->lbal, res->lbam, res->lbah,
1941 res->hob_feature, res->hob_nsect,
1942 res->hob_lbal, res->hob_lbam, res->hob_lbah,
1943 res->device, qc->err_mask, ata_err_string(qc->err_mask),
1944 qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
1949 * ata_eh_report - report error handling to user
1950 * @ap: ATA port to report EH about
1952 * Report EH to user.
1957 void ata_eh_report(struct ata_port *ap)
1959 struct ata_link *link;
1961 __ata_port_for_each_link(link, ap)
1962 ata_eh_link_report(link);
1965 static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
1966 unsigned int *classes, unsigned long deadline)
1968 struct ata_device *dev;
1971 ata_link_for_each_dev(dev, link)
1972 classes[dev->devno] = ATA_DEV_UNKNOWN;
1974 rc = reset(link, classes, deadline);
1978 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
1979 * is complete and convert all ATA_DEV_UNKNOWN to
1982 ata_link_for_each_dev(dev, link)
1983 if (classes[dev->devno] != ATA_DEV_UNKNOWN)
1987 ata_link_for_each_dev(dev, link) {
1988 if (classes[dev->devno] == ATA_DEV_UNKNOWN)
1989 classes[dev->devno] = ATA_DEV_NONE;
1996 static int ata_eh_followup_srst_needed(struct ata_link *link,
1997 int rc, int classify,
1998 const unsigned int *classes)
2000 if (link->flags & ATA_LFLAG_NO_SRST)
2006 if ((link->ap->flags & ATA_FLAG_PMP) && ata_is_host_link(link))
2008 if (classify && !(link->flags & ATA_LFLAG_ASSUME_CLASS) &&
2009 classes[0] == ATA_DEV_UNKNOWN)
2014 int ata_eh_reset(struct ata_link *link, int classify,
2015 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
2016 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
2018 struct ata_eh_context *ehc = &link->eh_context;
2019 unsigned int *classes = ehc->classes;
2020 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
2022 struct ata_device *dev;
2023 unsigned long deadline;
2024 unsigned int action;
2025 ata_reset_fn_t reset;
2028 /* about to reset */
2029 ata_eh_about_to_do(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
2031 /* Determine which reset to use and record in ehc->i.action.
2032 * prereset() may examine and modify it.
2034 action = ehc->i.action;
2035 ehc->i.action &= ~ATA_EH_RESET_MASK;
2036 if (softreset && (!hardreset || (!(link->flags & ATA_LFLAG_NO_SRST) &&
2037 !sata_set_spd_needed(link) &&
2038 !(action & ATA_EH_HARDRESET))))
2039 ehc->i.action |= ATA_EH_SOFTRESET;
2041 ehc->i.action |= ATA_EH_HARDRESET;
2044 rc = prereset(link, jiffies + ATA_EH_PRERESET_TIMEOUT);
2046 if (rc == -ENOENT) {
2047 ata_link_printk(link, KERN_DEBUG,
2048 "port disabled. ignoring.\n");
2049 ehc->i.action &= ~ATA_EH_RESET_MASK;
2051 ata_link_for_each_dev(dev, link)
2052 classes[dev->devno] = ATA_DEV_NONE;
2056 ata_link_printk(link, KERN_ERR,
2057 "prereset failed (errno=%d)\n", rc);
2062 /* prereset() might have modified ehc->i.action */
2063 if (ehc->i.action & ATA_EH_HARDRESET)
2065 else if (ehc->i.action & ATA_EH_SOFTRESET)
2068 /* prereset told us not to reset, bang classes and return */
2069 ata_link_for_each_dev(dev, link)
2070 classes[dev->devno] = ATA_DEV_NONE;
2075 /* did prereset() screw up? if so, fix up to avoid oopsing */
2084 deadline = jiffies + ata_eh_reset_timeouts[try++];
2086 /* shut up during boot probing */
2088 ata_link_printk(link, KERN_INFO, "%s resetting link\n",
2089 reset == softreset ? "soft" : "hard");
2091 /* mark that this EH session started with reset */
2092 if (reset == hardreset)
2093 ehc->i.flags |= ATA_EHI_DID_HARDRESET;
2095 ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
2097 rc = ata_do_reset(link, reset, classes, deadline);
2099 if (reset == hardreset &&
2100 ata_eh_followup_srst_needed(link, rc, classify, classes)) {
2101 /* okay, let's do follow-up softreset */
2105 ata_link_printk(link, KERN_ERR,
2106 "follow-up softreset required "
2107 "but no softreset avaliable\n");
2112 ata_eh_about_to_do(link, NULL, ATA_EH_RESET_MASK);
2113 rc = ata_do_reset(link, reset, classes, deadline);
2115 if (rc == 0 && classify && classes[0] == ATA_DEV_UNKNOWN &&
2116 !(link->flags & ATA_LFLAG_ASSUME_CLASS)) {
2117 ata_link_printk(link, KERN_ERR,
2118 "classification failed\n");
2124 /* if we skipped follow-up srst, clear rc */
2128 if (rc && rc != -ERESTART && try < ARRAY_SIZE(ata_eh_reset_timeouts)) {
2129 unsigned long now = jiffies;
2131 if (time_before(now, deadline)) {
2132 unsigned long delta = deadline - jiffies;
2134 ata_link_printk(link, KERN_WARNING, "reset failed "
2135 "(errno=%d), retrying in %u secs\n",
2136 rc, (jiffies_to_msecs(delta) + 999) / 1000);
2138 schedule_timeout_uninterruptible(delta);
2142 try == ARRAY_SIZE(ata_eh_reset_timeouts) - 1)
2143 sata_down_spd_limit(link);
2152 ata_link_for_each_dev(dev, link) {
2153 /* After the reset, the device state is PIO 0
2154 * and the controller state is undefined.
2157 dev->pio_mode = XFER_PIO_0;
2159 if (ata_link_offline(link))
2162 /* apply class override and convert UNKNOWN to NONE */
2163 if (link->flags & ATA_LFLAG_ASSUME_ATA)
2164 classes[dev->devno] = ATA_DEV_ATA;
2165 else if (link->flags & ATA_LFLAG_ASSUME_SEMB)
2166 classes[dev->devno] = ATA_DEV_SEMB_UNSUP; /* not yet */
2167 else if (classes[dev->devno] == ATA_DEV_UNKNOWN)
2168 classes[dev->devno] = ATA_DEV_NONE;
2171 /* record current link speed */
2172 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2173 link->sata_spd = (sstatus >> 4) & 0xf;
2176 postreset(link, classes);
2178 /* reset successful, schedule revalidation */
2179 ata_eh_done(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
2180 ehc->i.action |= ATA_EH_REVALIDATE;
2183 /* clear hotplug flag */
2184 ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2188 static int ata_eh_revalidate_and_attach(struct ata_link *link,
2189 struct ata_device **r_failed_dev)
2191 struct ata_port *ap = link->ap;
2192 struct ata_eh_context *ehc = &link->eh_context;
2193 struct ata_device *dev;
2194 unsigned int new_mask = 0;
2195 unsigned long flags;
2200 /* For PATA drive side cable detection to work, IDENTIFY must
2201 * be done backwards such that PDIAG- is released by the slave
2202 * device before the master device is identified.
2204 ata_link_for_each_dev_reverse(dev, link) {
2205 unsigned int action = ata_eh_dev_action(dev);
2206 unsigned int readid_flags = 0;
2208 if (ehc->i.flags & ATA_EHI_DID_RESET)
2209 readid_flags |= ATA_READID_POSTRESET;
2211 if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
2212 WARN_ON(dev->class == ATA_DEV_PMP);
2214 if (ata_link_offline(link)) {
2219 ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
2220 rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
2225 ata_eh_done(link, dev, ATA_EH_REVALIDATE);
2227 /* Configuration may have changed, reconfigure
2230 ehc->i.flags |= ATA_EHI_SETMODE;
2232 /* schedule the scsi_rescan_device() here */
2233 queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
2234 } else if (dev->class == ATA_DEV_UNKNOWN &&
2235 ehc->tries[dev->devno] &&
2236 ata_class_enabled(ehc->classes[dev->devno])) {
2237 dev->class = ehc->classes[dev->devno];
2239 if (dev->class == ATA_DEV_PMP)
2240 rc = sata_pmp_attach(dev);
2242 rc = ata_dev_read_id(dev, &dev->class,
2243 readid_flags, dev->id);
2246 new_mask |= 1 << dev->devno;
2249 /* IDENTIFY was issued to non-existent
2250 * device. No need to reset. Just
2251 * thaw and kill the device.
2253 ata_eh_thaw_port(ap);
2254 dev->class = ATA_DEV_UNKNOWN;
2257 dev->class = ATA_DEV_UNKNOWN;
2263 /* PDIAG- should have been released, ask cable type if post-reset */
2264 if (ata_is_host_link(link) && ap->ops->cable_detect &&
2265 (ehc->i.flags & ATA_EHI_DID_RESET))
2266 ap->cbl = ap->ops->cable_detect(ap);
2268 /* Configure new devices forward such that user doesn't see
2269 * device detection messages backwards.
2271 ata_link_for_each_dev(dev, link) {
2272 if (!(new_mask & (1 << dev->devno)) ||
2273 dev->class == ATA_DEV_PMP)
2276 ehc->i.flags |= ATA_EHI_PRINTINFO;
2277 rc = ata_dev_configure(dev);
2278 ehc->i.flags &= ~ATA_EHI_PRINTINFO;
2282 spin_lock_irqsave(ap->lock, flags);
2283 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
2284 spin_unlock_irqrestore(ap->lock, flags);
2286 /* new device discovered, configure xfermode */
2287 ehc->i.flags |= ATA_EHI_SETMODE;
2293 *r_failed_dev = dev;
2294 DPRINTK("EXIT rc=%d\n", rc);
2298 static int ata_link_nr_enabled(struct ata_link *link)
2300 struct ata_device *dev;
2303 ata_link_for_each_dev(dev, link)
2304 if (ata_dev_enabled(dev))
2309 static int ata_link_nr_vacant(struct ata_link *link)
2311 struct ata_device *dev;
2314 ata_link_for_each_dev(dev, link)
2315 if (dev->class == ATA_DEV_UNKNOWN)
2320 static int ata_eh_skip_recovery(struct ata_link *link)
2322 struct ata_eh_context *ehc = &link->eh_context;
2323 struct ata_device *dev;
2325 /* skip disabled links */
2326 if (link->flags & ATA_LFLAG_DISABLED)
2329 /* thaw frozen port, resume link and recover failed devices */
2330 if ((link->ap->pflags & ATA_PFLAG_FROZEN) ||
2331 (ehc->i.flags & ATA_EHI_RESUME_LINK) || ata_link_nr_enabled(link))
2334 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
2335 ata_link_for_each_dev(dev, link) {
2336 if (dev->class == ATA_DEV_UNKNOWN &&
2337 ehc->classes[dev->devno] != ATA_DEV_NONE)
2344 static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
2346 struct ata_eh_context *ehc = &dev->link->eh_context;
2348 ehc->tries[dev->devno]--;
2352 /* device missing or wrong IDENTIFY data, schedule probing */
2353 ehc->i.probe_mask |= (1 << dev->devno);
2355 /* give it just one more chance */
2356 ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
2358 if (ehc->tries[dev->devno] == 1) {
2359 /* This is the last chance, better to slow
2360 * down than lose it.
2362 sata_down_spd_limit(dev->link);
2363 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2367 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2368 /* disable device if it has used up all its chances */
2369 ata_dev_disable(dev);
2371 /* detach if offline */
2372 if (ata_link_offline(dev->link))
2373 ata_eh_detach_dev(dev);
2375 /* probe if requested */
2376 if ((ehc->i.probe_mask & (1 << dev->devno)) &&
2377 !(ehc->did_probe_mask & (1 << dev->devno))) {
2378 ata_eh_detach_dev(dev);
2381 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2382 ehc->did_probe_mask |= (1 << dev->devno);
2383 ehc->i.action |= ATA_EH_SOFTRESET;
2388 /* soft didn't work? be haaaaard */
2389 if (ehc->i.flags & ATA_EHI_DID_RESET)
2390 ehc->i.action |= ATA_EH_HARDRESET;
2392 ehc->i.action |= ATA_EH_SOFTRESET;
2399 * ata_eh_recover - recover host port after error
2400 * @ap: host port to recover
2401 * @prereset: prereset method (can be NULL)
2402 * @softreset: softreset method (can be NULL)
2403 * @hardreset: hardreset method (can be NULL)
2404 * @postreset: postreset method (can be NULL)
2405 * @r_failed_link: out parameter for failed link
2407 * This is the alpha and omega, eum and yang, heart and soul of
2408 * libata exception handling. On entry, actions required to
2409 * recover each link and hotplug requests are recorded in the
2410 * link's eh_context. This function executes all the operations
2411 * with appropriate retrials and fallbacks to resurrect failed
2412 * devices, detach goners and greet newcomers.
2415 * Kernel thread context (may sleep).
2418 * 0 on success, -errno on failure.
2420 int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
2421 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2422 ata_postreset_fn_t postreset,
2423 struct ata_link **r_failed_link)
2425 struct ata_link *link;
2426 struct ata_device *dev;
2427 int nr_failed_devs, nr_disabled_devs;
2429 unsigned long flags;
2433 /* prep for recovery */
2434 ata_port_for_each_link(link, ap) {
2435 struct ata_eh_context *ehc = &link->eh_context;
2437 /* re-enable link? */
2438 if (ehc->i.action & ATA_EH_ENABLE_LINK) {
2439 ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
2440 spin_lock_irqsave(ap->lock, flags);
2441 link->flags &= ~ATA_LFLAG_DISABLED;
2442 spin_unlock_irqrestore(ap->lock, flags);
2443 ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
2446 ata_link_for_each_dev(dev, link) {
2447 if (link->flags & ATA_LFLAG_NO_RETRY)
2448 ehc->tries[dev->devno] = 1;
2450 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2452 /* collect port action mask recorded in dev actions */
2453 ehc->i.action |= ehc->i.dev_action[dev->devno] &
2454 ~ATA_EH_PERDEV_MASK;
2455 ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
2457 /* process hotplug request */
2458 if (dev->flags & ATA_DFLAG_DETACH)
2459 ata_eh_detach_dev(dev);
2461 if (!ata_dev_enabled(dev) &&
2462 ((ehc->i.probe_mask & (1 << dev->devno)) &&
2463 !(ehc->did_probe_mask & (1 << dev->devno)))) {
2464 ata_eh_detach_dev(dev);
2466 ehc->did_probe_mask |= (1 << dev->devno);
2467 ehc->i.action |= ATA_EH_SOFTRESET;
2475 nr_disabled_devs = 0;
2478 /* if UNLOADING, finish immediately */
2479 if (ap->pflags & ATA_PFLAG_UNLOADING)
2483 ata_port_for_each_link(link, ap) {
2484 struct ata_eh_context *ehc = &link->eh_context;
2486 /* skip EH if possible. */
2487 if (ata_eh_skip_recovery(link))
2490 /* do we need to reset? */
2491 if (ehc->i.action & ATA_EH_RESET_MASK)
2494 ata_link_for_each_dev(dev, link)
2495 ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
2500 ata_eh_freeze_port(ap);
2502 ata_port_for_each_link(link, ap) {
2503 struct ata_eh_context *ehc = &link->eh_context;
2505 if (!(ehc->i.action & ATA_EH_RESET_MASK))
2508 rc = ata_eh_reset(link, ata_link_nr_vacant(link),
2509 prereset, softreset, hardreset,
2512 ata_link_printk(link, KERN_ERR,
2513 "reset failed, giving up\n");
2518 ata_eh_thaw_port(ap);
2522 ata_port_for_each_link(link, ap) {
2523 struct ata_eh_context *ehc = &link->eh_context;
2525 /* revalidate existing devices and attach new ones */
2526 rc = ata_eh_revalidate_and_attach(link, &dev);
2530 /* if PMP got attached, return, pmp EH will take care of it */
2531 if (link->device->class == ATA_DEV_PMP) {
2536 /* configure transfer mode if necessary */
2537 if (ehc->i.flags & ATA_EHI_SETMODE) {
2538 rc = ata_set_mode(link, &dev);
2541 ehc->i.flags &= ~ATA_EHI_SETMODE;
2544 /* this link is okay now */
2550 if (ata_eh_handle_dev_fail(dev, rc))
2553 if (ap->pflags & ATA_PFLAG_FROZEN)
2557 if (nr_failed_devs) {
2558 if (nr_failed_devs != nr_disabled_devs) {
2559 ata_port_printk(ap, KERN_WARNING, "failed to recover "
2560 "some devices, retrying in 5 secs\n");
2563 /* no device left to recover, repeat fast */
2571 if (rc && r_failed_link)
2572 *r_failed_link = link;
2574 DPRINTK("EXIT, rc=%d\n", rc);
2579 * ata_eh_finish - finish up EH
2580 * @ap: host port to finish EH for
2582 * Recovery is complete. Clean up EH states and retry or finish
2588 void ata_eh_finish(struct ata_port *ap)
2592 /* retry or finish qcs */
2593 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2594 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2596 if (!(qc->flags & ATA_QCFLAG_FAILED))
2600 /* FIXME: Once EH migration is complete,
2601 * generate sense data in this function,
2602 * considering both err_mask and tf.
2604 if (qc->err_mask & AC_ERR_INVALID)
2605 ata_eh_qc_complete(qc);
2607 ata_eh_qc_retry(qc);
2609 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2610 ata_eh_qc_complete(qc);
2612 /* feed zero TF to sense generation */
2613 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2614 ata_eh_qc_retry(qc);
2619 /* make sure nr_active_links is zero after EH */
2620 WARN_ON(ap->nr_active_links);
2621 ap->nr_active_links = 0;
2625 * ata_do_eh - do standard error handling
2626 * @ap: host port to handle error for
2627 * @prereset: prereset method (can be NULL)
2628 * @softreset: softreset method (can be NULL)
2629 * @hardreset: hardreset method (can be NULL)
2630 * @postreset: postreset method (can be NULL)
2632 * Perform standard error handling sequence.
2635 * Kernel thread context (may sleep).
2637 void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2638 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2639 ata_postreset_fn_t postreset)
2641 struct ata_device *dev;
2647 rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
2650 ata_link_for_each_dev(dev, &ap->link)
2651 ata_dev_disable(dev);
2659 * ata_eh_handle_port_suspend - perform port suspend operation
2660 * @ap: port to suspend
2665 * Kernel thread context (may sleep).
2667 static void ata_eh_handle_port_suspend(struct ata_port *ap)
2669 unsigned long flags;
2672 /* are we suspending? */
2673 spin_lock_irqsave(ap->lock, flags);
2674 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2675 ap->pm_mesg.event == PM_EVENT_ON) {
2676 spin_unlock_irqrestore(ap->lock, flags);
2679 spin_unlock_irqrestore(ap->lock, flags);
2681 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
2683 /* tell ACPI we're suspending */
2684 rc = ata_acpi_on_suspend(ap);
2689 ata_eh_freeze_port(ap);
2691 if (ap->ops->port_suspend)
2692 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
2696 spin_lock_irqsave(ap->lock, flags);
2698 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
2700 ap->pflags |= ATA_PFLAG_SUSPENDED;
2701 else if (ap->pflags & ATA_PFLAG_FROZEN)
2702 ata_port_schedule_eh(ap);
2704 if (ap->pm_result) {
2705 *ap->pm_result = rc;
2706 ap->pm_result = NULL;
2709 spin_unlock_irqrestore(ap->lock, flags);
2715 * ata_eh_handle_port_resume - perform port resume operation
2716 * @ap: port to resume
2721 * Kernel thread context (may sleep).
2723 static void ata_eh_handle_port_resume(struct ata_port *ap)
2725 unsigned long flags;
2728 /* are we resuming? */
2729 spin_lock_irqsave(ap->lock, flags);
2730 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2731 ap->pm_mesg.event != PM_EVENT_ON) {
2732 spin_unlock_irqrestore(ap->lock, flags);
2735 spin_unlock_irqrestore(ap->lock, flags);
2737 WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
2739 if (ap->ops->port_resume)
2740 rc = ap->ops->port_resume(ap);
2742 /* tell ACPI that we're resuming */
2743 ata_acpi_on_resume(ap);
2746 spin_lock_irqsave(ap->lock, flags);
2747 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
2748 if (ap->pm_result) {
2749 *ap->pm_result = rc;
2750 ap->pm_result = NULL;
2752 spin_unlock_irqrestore(ap->lock, flags);
2754 #endif /* CONFIG_PM */