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);
77 static void ata_eh_finish(struct ata_port *ap);
79 static void ata_eh_handle_port_suspend(struct ata_port *ap);
80 static void ata_eh_handle_port_resume(struct ata_port *ap);
82 static void ata_eh_handle_port_suspend(struct ata_port *ap)
85 static void ata_eh_handle_port_resume(struct ata_port *ap)
87 #endif /* CONFIG_PM */
89 static void __ata_ehi_pushv_desc(struct ata_eh_info *ehi, const char *fmt,
92 ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
93 ATA_EH_DESC_LEN - ehi->desc_len,
98 * __ata_ehi_push_desc - push error description without adding separator
100 * @fmt: printf format string
102 * Format string according to @fmt and append it to @ehi->desc.
105 * spin_lock_irqsave(host lock)
107 void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
112 __ata_ehi_pushv_desc(ehi, fmt, args);
117 * ata_ehi_push_desc - push error description with separator
119 * @fmt: printf format string
121 * Format string according to @fmt and append it to @ehi->desc.
122 * If @ehi->desc is not empty, ", " is added in-between.
125 * spin_lock_irqsave(host lock)
127 void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
132 __ata_ehi_push_desc(ehi, ", ");
135 __ata_ehi_pushv_desc(ehi, fmt, args);
140 * ata_ehi_clear_desc - clean error description
146 * spin_lock_irqsave(host lock)
148 void ata_ehi_clear_desc(struct ata_eh_info *ehi)
154 static void ata_ering_record(struct ata_ering *ering, int is_io,
155 unsigned int err_mask)
157 struct ata_ering_entry *ent;
162 ering->cursor %= ATA_ERING_SIZE;
164 ent = &ering->ring[ering->cursor];
166 ent->err_mask = err_mask;
167 ent->timestamp = get_jiffies_64();
170 static void ata_ering_clear(struct ata_ering *ering)
172 memset(ering, 0, sizeof(*ering));
175 static int ata_ering_map(struct ata_ering *ering,
176 int (*map_fn)(struct ata_ering_entry *, void *),
180 struct ata_ering_entry *ent;
184 ent = &ering->ring[idx];
187 rc = map_fn(ent, arg);
190 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
191 } while (idx != ering->cursor);
196 static unsigned int ata_eh_dev_action(struct ata_device *dev)
198 struct ata_eh_context *ehc = &dev->link->eh_context;
200 return ehc->i.action | ehc->i.dev_action[dev->devno];
203 static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
204 struct ata_eh_info *ehi, unsigned int action)
206 struct ata_device *tdev;
209 ehi->action &= ~action;
210 ata_link_for_each_dev(tdev, link)
211 ehi->dev_action[tdev->devno] &= ~action;
213 /* doesn't make sense for port-wide EH actions */
214 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
216 /* break ehi->action into ehi->dev_action */
217 if (ehi->action & action) {
218 ata_link_for_each_dev(tdev, link)
219 ehi->dev_action[tdev->devno] |=
220 ehi->action & action;
221 ehi->action &= ~action;
224 /* turn off the specified per-dev action */
225 ehi->dev_action[dev->devno] &= ~action;
230 * ata_scsi_timed_out - SCSI layer time out callback
231 * @cmd: timed out SCSI command
233 * Handles SCSI layer timeout. We race with normal completion of
234 * the qc for @cmd. If the qc is already gone, we lose and let
235 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
236 * timed out and EH should be invoked. Prevent ata_qc_complete()
237 * from finishing it by setting EH_SCHEDULED and return
240 * TODO: kill this function once old EH is gone.
243 * Called from timer context
246 * EH_HANDLED or EH_NOT_HANDLED
248 enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
250 struct Scsi_Host *host = cmd->device->host;
251 struct ata_port *ap = ata_shost_to_port(host);
253 struct ata_queued_cmd *qc;
254 enum scsi_eh_timer_return ret;
258 if (ap->ops->error_handler) {
259 ret = EH_NOT_HANDLED;
264 spin_lock_irqsave(ap->lock, flags);
265 qc = ata_qc_from_tag(ap, ap->link.active_tag);
267 WARN_ON(qc->scsicmd != cmd);
268 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
269 qc->err_mask |= AC_ERR_TIMEOUT;
270 ret = EH_NOT_HANDLED;
272 spin_unlock_irqrestore(ap->lock, flags);
275 DPRINTK("EXIT, ret=%d\n", ret);
280 * ata_scsi_error - SCSI layer error handler callback
281 * @host: SCSI host on which error occurred
283 * Handles SCSI-layer-thrown error events.
286 * Inherited from SCSI layer (none, can sleep)
291 void ata_scsi_error(struct Scsi_Host *host)
293 struct ata_port *ap = ata_shost_to_port(host);
294 int i, repeat_cnt = ATA_EH_MAX_REPEAT;
299 /* synchronize with port task */
300 ata_port_flush_task(ap);
302 /* synchronize with host lock and sort out timeouts */
304 /* For new EH, all qcs are finished in one of three ways -
305 * normal completion, error completion, and SCSI timeout.
306 * Both cmpletions can race against SCSI timeout. When normal
307 * completion wins, the qc never reaches EH. When error
308 * completion wins, the qc has ATA_QCFLAG_FAILED set.
310 * When SCSI timeout wins, things are a bit more complex.
311 * Normal or error completion can occur after the timeout but
312 * before this point. In such cases, both types of
313 * completions are honored. A scmd is determined to have
314 * timed out iff its associated qc is active and not failed.
316 if (ap->ops->error_handler) {
317 struct scsi_cmnd *scmd, *tmp;
320 spin_lock_irqsave(ap->lock, flags);
322 list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
323 struct ata_queued_cmd *qc;
325 for (i = 0; i < ATA_MAX_QUEUE; i++) {
326 qc = __ata_qc_from_tag(ap, i);
327 if (qc->flags & ATA_QCFLAG_ACTIVE &&
332 if (i < ATA_MAX_QUEUE) {
333 /* the scmd has an associated qc */
334 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
335 /* which hasn't failed yet, timeout */
336 qc->err_mask |= AC_ERR_TIMEOUT;
337 qc->flags |= ATA_QCFLAG_FAILED;
341 /* Normal completion occurred after
342 * SCSI timeout but before this point.
343 * Successfully complete it.
345 scmd->retries = scmd->allowed;
346 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
350 /* If we have timed out qcs. They belong to EH from
351 * this point but the state of the controller is
352 * unknown. Freeze the port to make sure the IRQ
353 * handler doesn't diddle with those qcs. This must
354 * be done atomically w.r.t. setting QCFLAG_FAILED.
357 __ata_port_freeze(ap);
359 spin_unlock_irqrestore(ap->lock, flags);
361 spin_unlock_wait(ap->lock);
364 /* invoke error handler */
365 if (ap->ops->error_handler) {
366 /* kill fast drain timer */
367 del_timer_sync(&ap->fastdrain_timer);
369 /* process port resume request */
370 ata_eh_handle_port_resume(ap);
372 /* fetch & clear EH info */
373 spin_lock_irqsave(ap->lock, flags);
375 memset(&ap->link.eh_context, 0, sizeof(ap->link.eh_context));
376 ap->link.eh_context.i = ap->link.eh_info;
377 memset(&ap->link.eh_info, 0, sizeof(ap->link.eh_info));
379 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
380 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
382 spin_unlock_irqrestore(ap->lock, flags);
384 /* invoke EH, skip if unloading or suspended */
385 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
386 ap->ops->error_handler(ap);
390 /* process port suspend request */
391 ata_eh_handle_port_suspend(ap);
393 /* Exception might have happend after ->error_handler
394 * recovered the port but before this point. Repeat
397 spin_lock_irqsave(ap->lock, flags);
399 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
401 ata_port_printk(ap, KERN_INFO,
402 "EH pending after completion, "
403 "repeating EH (cnt=%d)\n", repeat_cnt);
404 spin_unlock_irqrestore(ap->lock, flags);
407 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
408 "tries, giving up\n", ATA_EH_MAX_REPEAT);
409 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
412 /* this run is complete, make sure EH info is clear */
413 memset(&ap->link.eh_info, 0, sizeof(ap->link.eh_info));
415 /* Clear host_eh_scheduled while holding ap->lock such
416 * that if exception occurs after this point but
417 * before EH completion, SCSI midlayer will
420 host->host_eh_scheduled = 0;
422 spin_unlock_irqrestore(ap->lock, flags);
424 WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
425 ap->ops->eng_timeout(ap);
428 /* finish or retry handled scmd's and clean up */
429 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
431 scsi_eh_flush_done_q(&ap->eh_done_q);
434 spin_lock_irqsave(ap->lock, flags);
436 if (ap->pflags & ATA_PFLAG_LOADING)
437 ap->pflags &= ~ATA_PFLAG_LOADING;
438 else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
439 queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0);
441 if (ap->pflags & ATA_PFLAG_RECOVERED)
442 ata_port_printk(ap, KERN_INFO, "EH complete\n");
444 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
446 /* tell wait_eh that we're done */
447 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
448 wake_up_all(&ap->eh_wait_q);
450 spin_unlock_irqrestore(ap->lock, flags);
456 * ata_port_wait_eh - Wait for the currently pending EH to complete
457 * @ap: Port to wait EH for
459 * Wait until the currently pending EH is complete.
462 * Kernel thread context (may sleep).
464 void ata_port_wait_eh(struct ata_port *ap)
470 spin_lock_irqsave(ap->lock, flags);
472 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
473 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
474 spin_unlock_irqrestore(ap->lock, flags);
476 spin_lock_irqsave(ap->lock, flags);
478 finish_wait(&ap->eh_wait_q, &wait);
480 spin_unlock_irqrestore(ap->lock, flags);
482 /* make sure SCSI EH is complete */
483 if (scsi_host_in_recovery(ap->scsi_host)) {
490 * ata_qc_timeout - Handle timeout of queued command
491 * @qc: Command that timed out
493 * Some part of the kernel (currently, only the SCSI layer)
494 * has noticed that the active command on port @ap has not
495 * completed after a specified length of time. Handle this
496 * condition by disabling DMA (if necessary) and completing
497 * transactions, with error if necessary.
499 * This also handles the case of the "lost interrupt", where
500 * for some reason (possibly hardware bug, possibly driver bug)
501 * an interrupt was not delivered to the driver, even though the
502 * transaction completed successfully.
504 * TODO: kill this function once old EH is gone.
507 * Inherited from SCSI layer (none, can sleep)
509 static void ata_qc_timeout(struct ata_queued_cmd *qc)
511 struct ata_port *ap = qc->ap;
512 u8 host_stat = 0, drv_stat;
517 ap->hsm_task_state = HSM_ST_IDLE;
519 spin_lock_irqsave(ap->lock, flags);
521 switch (qc->tf.protocol) {
524 case ATA_PROT_ATAPI_DMA:
525 host_stat = ap->ops->bmdma_status(ap);
527 /* before we do anything else, clear DMA-Start bit */
528 ap->ops->bmdma_stop(qc);
534 drv_stat = ata_chk_status(ap);
536 /* ack bmdma irq events */
537 ap->ops->irq_clear(ap);
539 ata_dev_printk(qc->dev, KERN_ERR, "command 0x%x timeout, "
540 "stat 0x%x host_stat 0x%x\n",
541 qc->tf.command, drv_stat, host_stat);
543 /* complete taskfile transaction */
544 qc->err_mask |= AC_ERR_TIMEOUT;
548 spin_unlock_irqrestore(ap->lock, flags);
550 ata_eh_qc_complete(qc);
556 * ata_eng_timeout - Handle timeout of queued command
557 * @ap: Port on which timed-out command is active
559 * Some part of the kernel (currently, only the SCSI layer)
560 * has noticed that the active command on port @ap has not
561 * completed after a specified length of time. Handle this
562 * condition by disabling DMA (if necessary) and completing
563 * transactions, with error if necessary.
565 * This also handles the case of the "lost interrupt", where
566 * for some reason (possibly hardware bug, possibly driver bug)
567 * an interrupt was not delivered to the driver, even though the
568 * transaction completed successfully.
570 * TODO: kill this function once old EH is gone.
573 * Inherited from SCSI layer (none, can sleep)
575 void ata_eng_timeout(struct ata_port *ap)
579 ata_qc_timeout(ata_qc_from_tag(ap, ap->link.active_tag));
584 static int ata_eh_nr_in_flight(struct ata_port *ap)
589 /* count only non-internal commands */
590 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++)
591 if (ata_qc_from_tag(ap, tag))
597 void ata_eh_fastdrain_timerfn(unsigned long arg)
599 struct ata_port *ap = (void *)arg;
603 spin_lock_irqsave(ap->lock, flags);
605 cnt = ata_eh_nr_in_flight(ap);
611 if (cnt == ap->fastdrain_cnt) {
614 /* No progress during the last interval, tag all
615 * in-flight qcs as timed out and freeze the port.
617 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) {
618 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
620 qc->err_mask |= AC_ERR_TIMEOUT;
625 /* some qcs have finished, give it another chance */
626 ap->fastdrain_cnt = cnt;
627 ap->fastdrain_timer.expires =
628 jiffies + ATA_EH_FASTDRAIN_INTERVAL;
629 add_timer(&ap->fastdrain_timer);
633 spin_unlock_irqrestore(ap->lock, flags);
637 * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
638 * @ap: target ATA port
639 * @fastdrain: activate fast drain
641 * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
642 * is non-zero and EH wasn't pending before. Fast drain ensures
643 * that EH kicks in in timely manner.
646 * spin_lock_irqsave(host lock)
648 static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
652 /* already scheduled? */
653 if (ap->pflags & ATA_PFLAG_EH_PENDING)
656 ap->pflags |= ATA_PFLAG_EH_PENDING;
661 /* do we have in-flight qcs? */
662 cnt = ata_eh_nr_in_flight(ap);
666 /* activate fast drain */
667 ap->fastdrain_cnt = cnt;
668 ap->fastdrain_timer.expires = jiffies + ATA_EH_FASTDRAIN_INTERVAL;
669 add_timer(&ap->fastdrain_timer);
673 * ata_qc_schedule_eh - schedule qc for error handling
674 * @qc: command to schedule error handling for
676 * Schedule error handling for @qc. EH will kick in as soon as
677 * other commands are drained.
680 * spin_lock_irqsave(host lock)
682 void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
684 struct ata_port *ap = qc->ap;
686 WARN_ON(!ap->ops->error_handler);
688 qc->flags |= ATA_QCFLAG_FAILED;
689 ata_eh_set_pending(ap, 1);
691 /* The following will fail if timeout has already expired.
692 * ata_scsi_error() takes care of such scmds on EH entry.
693 * Note that ATA_QCFLAG_FAILED is unconditionally set after
694 * this function completes.
696 scsi_req_abort_cmd(qc->scsicmd);
700 * ata_port_schedule_eh - schedule error handling without a qc
701 * @ap: ATA port to schedule EH for
703 * Schedule error handling for @ap. EH will kick in as soon as
704 * all commands are drained.
707 * spin_lock_irqsave(host lock)
709 void ata_port_schedule_eh(struct ata_port *ap)
711 WARN_ON(!ap->ops->error_handler);
713 if (ap->pflags & ATA_PFLAG_INITIALIZING)
716 ata_eh_set_pending(ap, 1);
717 scsi_schedule_eh(ap->scsi_host);
719 DPRINTK("port EH scheduled\n");
723 * ata_port_abort - abort all qc's on the port
724 * @ap: ATA port to abort qc's for
726 * Abort all active qc's of @ap and schedule EH.
729 * spin_lock_irqsave(host lock)
732 * Number of aborted qc's.
734 int ata_port_abort(struct ata_port *ap)
736 int tag, nr_aborted = 0;
738 WARN_ON(!ap->ops->error_handler);
740 /* we're gonna abort all commands, no need for fast drain */
741 ata_eh_set_pending(ap, 0);
743 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
744 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
747 qc->flags |= ATA_QCFLAG_FAILED;
754 ata_port_schedule_eh(ap);
760 * __ata_port_freeze - freeze port
761 * @ap: ATA port to freeze
763 * This function is called when HSM violation or some other
764 * condition disrupts normal operation of the port. Frozen port
765 * is not allowed to perform any operation until the port is
766 * thawed, which usually follows a successful reset.
768 * ap->ops->freeze() callback can be used for freezing the port
769 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
770 * port cannot be frozen hardware-wise, the interrupt handler
771 * must ack and clear interrupts unconditionally while the port
775 * spin_lock_irqsave(host lock)
777 static void __ata_port_freeze(struct ata_port *ap)
779 WARN_ON(!ap->ops->error_handler);
784 ap->pflags |= ATA_PFLAG_FROZEN;
786 DPRINTK("ata%u port frozen\n", ap->print_id);
790 * ata_port_freeze - abort & freeze port
791 * @ap: ATA port to freeze
793 * Abort and freeze @ap.
796 * spin_lock_irqsave(host lock)
799 * Number of aborted commands.
801 int ata_port_freeze(struct ata_port *ap)
805 WARN_ON(!ap->ops->error_handler);
807 nr_aborted = ata_port_abort(ap);
808 __ata_port_freeze(ap);
814 * ata_eh_freeze_port - EH helper to freeze port
815 * @ap: ATA port to freeze
822 void ata_eh_freeze_port(struct ata_port *ap)
826 if (!ap->ops->error_handler)
829 spin_lock_irqsave(ap->lock, flags);
830 __ata_port_freeze(ap);
831 spin_unlock_irqrestore(ap->lock, flags);
835 * ata_port_thaw_port - EH helper to thaw port
836 * @ap: ATA port to thaw
838 * Thaw frozen port @ap.
843 void ata_eh_thaw_port(struct ata_port *ap)
847 if (!ap->ops->error_handler)
850 spin_lock_irqsave(ap->lock, flags);
852 ap->pflags &= ~ATA_PFLAG_FROZEN;
857 spin_unlock_irqrestore(ap->lock, flags);
859 DPRINTK("ata%u port thawed\n", ap->print_id);
862 static void ata_eh_scsidone(struct scsi_cmnd *scmd)
867 static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
869 struct ata_port *ap = qc->ap;
870 struct scsi_cmnd *scmd = qc->scsicmd;
873 spin_lock_irqsave(ap->lock, flags);
874 qc->scsidone = ata_eh_scsidone;
875 __ata_qc_complete(qc);
876 WARN_ON(ata_tag_valid(qc->tag));
877 spin_unlock_irqrestore(ap->lock, flags);
879 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
883 * ata_eh_qc_complete - Complete an active ATA command from EH
884 * @qc: Command to complete
886 * Indicate to the mid and upper layers that an ATA command has
887 * completed. To be used from EH.
889 void ata_eh_qc_complete(struct ata_queued_cmd *qc)
891 struct scsi_cmnd *scmd = qc->scsicmd;
892 scmd->retries = scmd->allowed;
893 __ata_eh_qc_complete(qc);
897 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
898 * @qc: Command to retry
900 * Indicate to the mid and upper layers that an ATA command
901 * should be retried. To be used from EH.
903 * SCSI midlayer limits the number of retries to scmd->allowed.
904 * scmd->retries is decremented for commands which get retried
905 * due to unrelated failures (qc->err_mask is zero).
907 void ata_eh_qc_retry(struct ata_queued_cmd *qc)
909 struct scsi_cmnd *scmd = qc->scsicmd;
910 if (!qc->err_mask && scmd->retries)
912 __ata_eh_qc_complete(qc);
916 * ata_eh_detach_dev - detach ATA device
917 * @dev: ATA device to detach
924 static void ata_eh_detach_dev(struct ata_device *dev)
926 struct ata_link *link = dev->link;
927 struct ata_port *ap = link->ap;
930 ata_dev_disable(dev);
932 spin_lock_irqsave(ap->lock, flags);
934 dev->flags &= ~ATA_DFLAG_DETACH;
936 if (ata_scsi_offline_dev(dev)) {
937 dev->flags |= ATA_DFLAG_DETACHED;
938 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
941 /* clear per-dev EH actions */
942 ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
943 ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
945 spin_unlock_irqrestore(ap->lock, flags);
949 * ata_eh_about_to_do - about to perform eh_action
950 * @link: target ATA link
951 * @dev: target ATA dev for per-dev action (can be NULL)
952 * @action: action about to be performed
954 * Called just before performing EH actions to clear related bits
955 * in @link->eh_info such that eh actions are not unnecessarily
961 static void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
964 struct ata_port *ap = link->ap;
965 struct ata_eh_info *ehi = &link->eh_info;
966 struct ata_eh_context *ehc = &link->eh_context;
969 spin_lock_irqsave(ap->lock, flags);
971 /* Reset is represented by combination of actions and EHI
972 * flags. Suck in all related bits before clearing eh_info to
973 * avoid losing requested action.
975 if (action & ATA_EH_RESET_MASK) {
976 ehc->i.action |= ehi->action & ATA_EH_RESET_MASK;
977 ehc->i.flags |= ehi->flags & ATA_EHI_RESET_MODIFIER_MASK;
979 /* make sure all reset actions are cleared & clear EHI flags */
980 action |= ATA_EH_RESET_MASK;
981 ehi->flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
984 ata_eh_clear_action(link, dev, ehi, action);
986 if (!(ehc->i.flags & ATA_EHI_QUIET))
987 ap->pflags |= ATA_PFLAG_RECOVERED;
989 spin_unlock_irqrestore(ap->lock, flags);
993 * ata_eh_done - EH action complete
994 * @ap: target ATA port
995 * @dev: target ATA dev for per-dev action (can be NULL)
996 * @action: action just completed
998 * Called right after performing EH actions to clear related bits
999 * in @link->eh_context.
1004 static void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1005 unsigned int action)
1007 struct ata_eh_context *ehc = &link->eh_context;
1009 /* if reset is complete, clear all reset actions & reset modifier */
1010 if (action & ATA_EH_RESET_MASK) {
1011 action |= ATA_EH_RESET_MASK;
1012 ehc->i.flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
1015 ata_eh_clear_action(link, dev, &ehc->i, action);
1019 * ata_err_string - convert err_mask to descriptive string
1020 * @err_mask: error mask to convert to string
1022 * Convert @err_mask to descriptive string. Errors are
1023 * prioritized according to severity and only the most severe
1024 * error is reported.
1030 * Descriptive string for @err_mask
1032 static const char * ata_err_string(unsigned int err_mask)
1034 if (err_mask & AC_ERR_HOST_BUS)
1035 return "host bus error";
1036 if (err_mask & AC_ERR_ATA_BUS)
1037 return "ATA bus error";
1038 if (err_mask & AC_ERR_TIMEOUT)
1040 if (err_mask & AC_ERR_HSM)
1041 return "HSM violation";
1042 if (err_mask & AC_ERR_SYSTEM)
1043 return "internal error";
1044 if (err_mask & AC_ERR_MEDIA)
1045 return "media error";
1046 if (err_mask & AC_ERR_INVALID)
1047 return "invalid argument";
1048 if (err_mask & AC_ERR_DEV)
1049 return "device error";
1050 return "unknown error";
1054 * ata_read_log_page - read a specific log page
1055 * @dev: target device
1056 * @page: page to read
1057 * @buf: buffer to store read page
1058 * @sectors: number of sectors to read
1060 * Read log page using READ_LOG_EXT command.
1063 * Kernel thread context (may sleep).
1066 * 0 on success, AC_ERR_* mask otherwise.
1068 static unsigned int ata_read_log_page(struct ata_device *dev,
1069 u8 page, void *buf, unsigned int sectors)
1071 struct ata_taskfile tf;
1072 unsigned int err_mask;
1074 DPRINTK("read log page - page %d\n", page);
1076 ata_tf_init(dev, &tf);
1077 tf.command = ATA_CMD_READ_LOG_EXT;
1080 tf.hob_nsect = sectors >> 8;
1081 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1082 tf.protocol = ATA_PROT_PIO;
1084 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
1085 buf, sectors * ATA_SECT_SIZE);
1087 DPRINTK("EXIT, err_mask=%x\n", err_mask);
1092 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
1093 * @dev: Device to read log page 10h from
1094 * @tag: Resulting tag of the failed command
1095 * @tf: Resulting taskfile registers of the failed command
1097 * Read log page 10h to obtain NCQ error details and clear error
1101 * Kernel thread context (may sleep).
1104 * 0 on success, -errno otherwise.
1106 static int ata_eh_read_log_10h(struct ata_device *dev,
1107 int *tag, struct ata_taskfile *tf)
1109 u8 *buf = dev->link->ap->sector_buf;
1110 unsigned int err_mask;
1114 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
1119 for (i = 0; i < ATA_SECT_SIZE; i++)
1122 ata_dev_printk(dev, KERN_WARNING,
1123 "invalid checksum 0x%x on log page 10h\n", csum);
1128 *tag = buf[0] & 0x1f;
1130 tf->command = buf[2];
1131 tf->feature = buf[3];
1135 tf->device = buf[7];
1136 tf->hob_lbal = buf[8];
1137 tf->hob_lbam = buf[9];
1138 tf->hob_lbah = buf[10];
1139 tf->nsect = buf[12];
1140 tf->hob_nsect = buf[13];
1146 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1147 * @dev: device to perform REQUEST_SENSE to
1148 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
1150 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
1151 * SENSE. This function is EH helper.
1154 * Kernel thread context (may sleep).
1157 * 0 on success, AC_ERR_* mask on failure
1159 static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc)
1161 struct ata_device *dev = qc->dev;
1162 unsigned char *sense_buf = qc->scsicmd->sense_buffer;
1163 struct ata_port *ap = dev->link->ap;
1164 struct ata_taskfile tf;
1165 u8 cdb[ATAPI_CDB_LEN];
1167 DPRINTK("ATAPI request sense\n");
1169 /* FIXME: is this needed? */
1170 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1172 /* initialize sense_buf with the error register,
1173 * for the case where they are -not- overwritten
1175 sense_buf[0] = 0x70;
1176 sense_buf[2] = qc->result_tf.feature >> 4;
1178 /* some devices time out if garbage left in tf */
1179 ata_tf_init(dev, &tf);
1181 memset(cdb, 0, ATAPI_CDB_LEN);
1182 cdb[0] = REQUEST_SENSE;
1183 cdb[4] = SCSI_SENSE_BUFFERSIZE;
1185 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1186 tf.command = ATA_CMD_PACKET;
1188 /* is it pointless to prefer PIO for "safety reasons"? */
1189 if (ap->flags & ATA_FLAG_PIO_DMA) {
1190 tf.protocol = ATA_PROT_ATAPI_DMA;
1191 tf.feature |= ATAPI_PKT_DMA;
1193 tf.protocol = ATA_PROT_ATAPI;
1194 tf.lbam = (8 * 1024) & 0xff;
1195 tf.lbah = (8 * 1024) >> 8;
1198 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
1199 sense_buf, SCSI_SENSE_BUFFERSIZE);
1203 * ata_eh_analyze_serror - analyze SError for a failed port
1204 * @link: ATA link to analyze SError for
1206 * Analyze SError if available and further determine cause of
1212 static void ata_eh_analyze_serror(struct ata_link *link)
1214 struct ata_eh_context *ehc = &link->eh_context;
1215 u32 serror = ehc->i.serror;
1216 unsigned int err_mask = 0, action = 0;
1218 if (serror & SERR_PERSISTENT) {
1219 err_mask |= AC_ERR_ATA_BUS;
1220 action |= ATA_EH_HARDRESET;
1223 (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
1224 err_mask |= AC_ERR_ATA_BUS;
1225 action |= ATA_EH_SOFTRESET;
1227 if (serror & SERR_PROTOCOL) {
1228 err_mask |= AC_ERR_HSM;
1229 action |= ATA_EH_SOFTRESET;
1231 if (serror & SERR_INTERNAL) {
1232 err_mask |= AC_ERR_SYSTEM;
1233 action |= ATA_EH_HARDRESET;
1235 if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG))
1236 ata_ehi_hotplugged(&ehc->i);
1238 ehc->i.err_mask |= err_mask;
1239 ehc->i.action |= action;
1243 * ata_eh_analyze_ncq_error - analyze NCQ error
1244 * @link: ATA link to analyze NCQ error for
1246 * Read log page 10h, determine the offending qc and acquire
1247 * error status TF. For NCQ device errors, all LLDDs have to do
1248 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1252 * Kernel thread context (may sleep).
1254 static void ata_eh_analyze_ncq_error(struct ata_link *link)
1256 struct ata_port *ap = link->ap;
1257 struct ata_eh_context *ehc = &link->eh_context;
1258 struct ata_device *dev = link->device;
1259 struct ata_queued_cmd *qc;
1260 struct ata_taskfile tf;
1263 /* if frozen, we can't do much */
1264 if (ap->pflags & ATA_PFLAG_FROZEN)
1267 /* is it NCQ device error? */
1268 if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1271 /* has LLDD analyzed already? */
1272 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1273 qc = __ata_qc_from_tag(ap, tag);
1275 if (!(qc->flags & ATA_QCFLAG_FAILED))
1282 /* okay, this error is ours */
1283 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1285 ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
1286 "(errno=%d)\n", rc);
1290 if (!(link->sactive & (1 << tag))) {
1291 ata_link_printk(link, KERN_ERR, "log page 10h reported "
1292 "inactive tag %d\n", tag);
1296 /* we've got the perpetrator, condemn it */
1297 qc = __ata_qc_from_tag(ap, tag);
1298 memcpy(&qc->result_tf, &tf, sizeof(tf));
1299 qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
1300 ehc->i.err_mask &= ~AC_ERR_DEV;
1304 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1305 * @qc: qc to analyze
1306 * @tf: Taskfile registers to analyze
1308 * Analyze taskfile of @qc and further determine cause of
1309 * failure. This function also requests ATAPI sense data if
1313 * Kernel thread context (may sleep).
1316 * Determined recovery action
1318 static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1319 const struct ata_taskfile *tf)
1321 unsigned int tmp, action = 0;
1322 u8 stat = tf->command, err = tf->feature;
1324 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1325 qc->err_mask |= AC_ERR_HSM;
1326 return ATA_EH_SOFTRESET;
1329 if (stat & (ATA_ERR | ATA_DF))
1330 qc->err_mask |= AC_ERR_DEV;
1334 switch (qc->dev->class) {
1337 qc->err_mask |= AC_ERR_ATA_BUS;
1339 qc->err_mask |= AC_ERR_MEDIA;
1341 qc->err_mask |= AC_ERR_INVALID;
1345 if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
1346 tmp = atapi_eh_request_sense(qc);
1348 /* ATA_QCFLAG_SENSE_VALID is used to
1349 * tell atapi_qc_complete() that sense
1350 * data is already valid.
1352 * TODO: interpret sense data and set
1353 * appropriate err_mask.
1355 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1357 qc->err_mask |= tmp;
1361 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1362 action |= ATA_EH_SOFTRESET;
1367 static int ata_eh_categorize_error(int is_io, unsigned int err_mask)
1369 if (err_mask & AC_ERR_ATA_BUS)
1372 if (err_mask & AC_ERR_TIMEOUT)
1376 if (err_mask & AC_ERR_HSM)
1379 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1386 struct speed_down_verdict_arg {
1391 static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
1393 struct speed_down_verdict_arg *arg = void_arg;
1394 int cat = ata_eh_categorize_error(ent->is_io, ent->err_mask);
1396 if (ent->timestamp < arg->since)
1399 arg->nr_errors[cat]++;
1404 * ata_eh_speed_down_verdict - Determine speed down verdict
1405 * @dev: Device of interest
1407 * This function examines error ring of @dev and determines
1408 * whether NCQ needs to be turned off, transfer speed should be
1409 * stepped down, or falling back to PIO is necessary.
1411 * Cat-1 is ATA_BUS error for any command.
1413 * Cat-2 is TIMEOUT for any command or HSM violation for known
1414 * supported commands.
1416 * Cat-3 is is unclassified DEV error for known supported
1419 * NCQ needs to be turned off if there have been more than 3
1420 * Cat-2 + Cat-3 errors during last 10 minutes.
1422 * Speed down is necessary if there have been more than 3 Cat-1 +
1423 * Cat-2 errors or 10 Cat-3 errors during last 10 minutes.
1425 * Falling back to PIO mode is necessary if there have been more
1426 * than 10 Cat-1 + Cat-2 + Cat-3 errors during last 5 minutes.
1429 * Inherited from caller.
1432 * OR of ATA_EH_SPDN_* flags.
1434 static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
1436 const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
1437 u64 j64 = get_jiffies_64();
1438 struct speed_down_verdict_arg arg;
1439 unsigned int verdict = 0;
1441 /* scan past 10 mins of error history */
1442 memset(&arg, 0, sizeof(arg));
1443 arg.since = j64 - min(j64, j10mins);
1444 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1446 if (arg.nr_errors[2] + arg.nr_errors[3] > 3)
1447 verdict |= ATA_EH_SPDN_NCQ_OFF;
1448 if (arg.nr_errors[1] + arg.nr_errors[2] > 3 || arg.nr_errors[3] > 10)
1449 verdict |= ATA_EH_SPDN_SPEED_DOWN;
1451 /* scan past 3 mins of error history */
1452 memset(&arg, 0, sizeof(arg));
1453 arg.since = j64 - min(j64, j5mins);
1454 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1456 if (arg.nr_errors[1] + arg.nr_errors[2] + arg.nr_errors[3] > 10)
1457 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
1463 * ata_eh_speed_down - record error and speed down if necessary
1464 * @dev: Failed device
1465 * @is_io: Did the device fail during normal IO?
1466 * @err_mask: err_mask of the error
1468 * Record error and examine error history to determine whether
1469 * adjusting transmission speed is necessary. It also sets
1470 * transmission limits appropriately if such adjustment is
1474 * Kernel thread context (may sleep).
1477 * Determined recovery action.
1479 static unsigned int ata_eh_speed_down(struct ata_device *dev, int is_io,
1480 unsigned int err_mask)
1482 unsigned int verdict;
1483 unsigned int action = 0;
1485 /* don't bother if Cat-0 error */
1486 if (ata_eh_categorize_error(is_io, err_mask) == 0)
1489 /* record error and determine whether speed down is necessary */
1490 ata_ering_record(&dev->ering, is_io, err_mask);
1491 verdict = ata_eh_speed_down_verdict(dev);
1494 if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
1495 (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
1496 ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
1497 dev->flags |= ATA_DFLAG_NCQ_OFF;
1498 ata_dev_printk(dev, KERN_WARNING,
1499 "NCQ disabled due to excessive errors\n");
1504 if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1505 /* speed down SATA link speed if possible */
1506 if (sata_down_spd_limit(dev->link) == 0) {
1507 action |= ATA_EH_HARDRESET;
1511 /* lower transfer mode */
1512 if (dev->spdn_cnt < 2) {
1513 static const int dma_dnxfer_sel[] =
1514 { ATA_DNXFER_DMA, ATA_DNXFER_40C };
1515 static const int pio_dnxfer_sel[] =
1516 { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
1519 if (dev->xfer_shift != ATA_SHIFT_PIO)
1520 sel = dma_dnxfer_sel[dev->spdn_cnt];
1522 sel = pio_dnxfer_sel[dev->spdn_cnt];
1526 if (ata_down_xfermask_limit(dev, sel) == 0) {
1527 action |= ATA_EH_SOFTRESET;
1533 /* Fall back to PIO? Slowing down to PIO is meaningless for
1534 * SATA. Consider it only for PATA.
1536 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
1537 (dev->link->ap->cbl != ATA_CBL_SATA) &&
1538 (dev->xfer_shift != ATA_SHIFT_PIO)) {
1539 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
1541 action |= ATA_EH_SOFTRESET;
1548 /* device has been slowed down, blow error history */
1549 ata_ering_clear(&dev->ering);
1554 * ata_eh_autopsy - analyze error and determine recovery action
1555 * @link: ATA link to perform autopsy on
1557 * Analyze why @link failed and determine which recovery actions
1558 * are needed. This function also sets more detailed AC_ERR_*
1559 * values and fills sense data for ATAPI CHECK SENSE.
1562 * Kernel thread context (may sleep).
1564 static void ata_eh_autopsy(struct ata_link *link)
1566 struct ata_port *ap = link->ap;
1567 struct ata_eh_context *ehc = &link->eh_context;
1568 unsigned int all_err_mask = 0;
1575 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1578 /* obtain and analyze SError */
1579 rc = sata_scr_read(link, SCR_ERROR, &serror);
1581 ehc->i.serror |= serror;
1582 ata_eh_analyze_serror(link);
1583 } else if (rc != -EOPNOTSUPP) {
1584 /* SError read failed, force hardreset and probing */
1585 ata_ehi_schedule_probe(&ehc->i);
1586 ehc->i.action |= ATA_EH_HARDRESET;
1587 ehc->i.err_mask |= AC_ERR_OTHER;
1590 /* analyze NCQ failure */
1591 ata_eh_analyze_ncq_error(link);
1593 /* any real error trumps AC_ERR_OTHER */
1594 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1595 ehc->i.err_mask &= ~AC_ERR_OTHER;
1597 all_err_mask |= ehc->i.err_mask;
1599 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1600 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1602 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
1605 /* inherit upper level err_mask */
1606 qc->err_mask |= ehc->i.err_mask;
1609 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1611 /* DEV errors are probably spurious in case of ATA_BUS error */
1612 if (qc->err_mask & AC_ERR_ATA_BUS)
1613 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1616 /* any real error trumps unknown error */
1617 if (qc->err_mask & ~AC_ERR_OTHER)
1618 qc->err_mask &= ~AC_ERR_OTHER;
1620 /* SENSE_VALID trumps dev/unknown error and revalidation */
1621 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1622 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1623 ehc->i.action &= ~ATA_EH_REVALIDATE;
1626 /* accumulate error info */
1627 ehc->i.dev = qc->dev;
1628 all_err_mask |= qc->err_mask;
1629 if (qc->flags & ATA_QCFLAG_IO)
1633 /* enforce default EH actions */
1634 if (ap->pflags & ATA_PFLAG_FROZEN ||
1635 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
1636 ehc->i.action |= ATA_EH_SOFTRESET;
1637 else if (all_err_mask)
1638 ehc->i.action |= ATA_EH_REVALIDATE;
1640 /* if we have offending qcs and the associated failed device */
1643 ehc->i.action |= ata_eh_speed_down(ehc->i.dev, is_io,
1646 /* perform per-dev EH action only on the offending device */
1647 ehc->i.dev_action[ehc->i.dev->devno] |=
1648 ehc->i.action & ATA_EH_PERDEV_MASK;
1649 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
1656 * ata_eh_report - report error handling to user
1657 * @link: ATA link EH is going on
1659 * Report EH to user.
1664 static void ata_eh_report(struct ata_link *link)
1666 struct ata_port *ap = link->ap;
1667 struct ata_eh_context *ehc = &link->eh_context;
1668 const char *frozen, *desc;
1669 int tag, nr_failed = 0;
1672 if (ehc->i.desc[0] != '\0')
1675 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1676 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1678 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
1680 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1686 if (!nr_failed && !ehc->i.err_mask)
1690 if (ap->pflags & ATA_PFLAG_FROZEN)
1694 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1695 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1696 ehc->i.err_mask, link->sactive,
1697 ehc->i.serror, ehc->i.action, frozen);
1699 ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
1701 ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
1702 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1703 ehc->i.err_mask, link->sactive,
1704 ehc->i.serror, ehc->i.action, frozen);
1706 ata_link_printk(link, KERN_ERR, "%s\n", desc);
1709 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1710 static const char *dma_str[] = {
1711 [DMA_BIDIRECTIONAL] = "bidi",
1712 [DMA_TO_DEVICE] = "out",
1713 [DMA_FROM_DEVICE] = "in",
1716 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1717 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
1719 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
1720 qc->dev->link != link || !qc->err_mask)
1723 ata_dev_printk(qc->dev, KERN_ERR,
1724 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
1725 "tag %d cdb 0x%x data %u %s\n "
1726 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
1727 "Emask 0x%x (%s)%s\n",
1728 cmd->command, cmd->feature, cmd->nsect,
1729 cmd->lbal, cmd->lbam, cmd->lbah,
1730 cmd->hob_feature, cmd->hob_nsect,
1731 cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
1732 cmd->device, qc->tag, qc->cdb[0], qc->nbytes,
1733 dma_str[qc->dma_dir],
1734 res->command, res->feature, res->nsect,
1735 res->lbal, res->lbam, res->lbah,
1736 res->hob_feature, res->hob_nsect,
1737 res->hob_lbal, res->hob_lbam, res->hob_lbah,
1738 res->device, qc->err_mask, ata_err_string(qc->err_mask),
1739 qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
1743 static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
1744 unsigned int *classes, unsigned long deadline)
1746 struct ata_device *dev;
1749 ata_link_for_each_dev(dev, link)
1750 classes[dev->devno] = ATA_DEV_UNKNOWN;
1752 rc = reset(link, classes, deadline);
1756 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
1757 * is complete and convert all ATA_DEV_UNKNOWN to
1760 ata_link_for_each_dev(dev, link)
1761 if (classes[dev->devno] != ATA_DEV_UNKNOWN)
1765 ata_link_for_each_dev(dev, link) {
1766 if (classes[dev->devno] == ATA_DEV_UNKNOWN)
1767 classes[dev->devno] = ATA_DEV_NONE;
1774 static int ata_eh_followup_srst_needed(int rc, int classify,
1775 const unsigned int *classes)
1781 if (classify && classes[0] == ATA_DEV_UNKNOWN)
1786 static int ata_eh_reset(struct ata_link *link, int classify,
1787 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
1788 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
1790 struct ata_eh_context *ehc = &link->eh_context;
1791 unsigned int *classes = ehc->classes;
1792 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
1794 struct ata_device *dev;
1795 unsigned long deadline;
1796 unsigned int action;
1797 ata_reset_fn_t reset;
1800 /* about to reset */
1801 ata_eh_about_to_do(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
1803 /* Determine which reset to use and record in ehc->i.action.
1804 * prereset() may examine and modify it.
1806 action = ehc->i.action;
1807 ehc->i.action &= ~ATA_EH_RESET_MASK;
1808 if (softreset && (!hardreset || (!sata_set_spd_needed(link) &&
1809 !(action & ATA_EH_HARDRESET))))
1810 ehc->i.action |= ATA_EH_SOFTRESET;
1812 ehc->i.action |= ATA_EH_HARDRESET;
1815 rc = prereset(link, jiffies + ATA_EH_PRERESET_TIMEOUT);
1817 if (rc == -ENOENT) {
1818 ata_link_printk(link, KERN_DEBUG,
1819 "port disabled. ignoring.\n");
1820 ehc->i.action &= ~ATA_EH_RESET_MASK;
1822 ata_link_for_each_dev(dev, link)
1823 classes[dev->devno] = ATA_DEV_NONE;
1827 ata_link_printk(link, KERN_ERR,
1828 "prereset failed (errno=%d)\n", rc);
1833 /* prereset() might have modified ehc->i.action */
1834 if (ehc->i.action & ATA_EH_HARDRESET)
1836 else if (ehc->i.action & ATA_EH_SOFTRESET)
1839 /* prereset told us not to reset, bang classes and return */
1840 ata_link_for_each_dev(dev, link)
1841 classes[dev->devno] = ATA_DEV_NONE;
1846 /* did prereset() screw up? if so, fix up to avoid oopsing */
1855 deadline = jiffies + ata_eh_reset_timeouts[try++];
1857 /* shut up during boot probing */
1859 ata_link_printk(link, KERN_INFO, "%s resetting link\n",
1860 reset == softreset ? "soft" : "hard");
1862 /* mark that this EH session started with reset */
1863 if (reset == hardreset)
1864 ehc->i.flags |= ATA_EHI_DID_HARDRESET;
1866 ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
1868 rc = ata_do_reset(link, reset, classes, deadline);
1870 if (reset == hardreset &&
1871 ata_eh_followup_srst_needed(rc, classify, classes)) {
1872 /* okay, let's do follow-up softreset */
1876 ata_link_printk(link, KERN_ERR,
1877 "follow-up softreset required "
1878 "but no softreset avaliable\n");
1883 ata_eh_about_to_do(link, NULL, ATA_EH_RESET_MASK);
1884 rc = ata_do_reset(link, reset, classes, deadline);
1886 if (rc == 0 && classify &&
1887 classes[0] == ATA_DEV_UNKNOWN) {
1888 ata_link_printk(link, KERN_ERR,
1889 "classification failed\n");
1895 if (rc && try < ARRAY_SIZE(ata_eh_reset_timeouts)) {
1896 unsigned long now = jiffies;
1898 if (time_before(now, deadline)) {
1899 unsigned long delta = deadline - jiffies;
1901 ata_link_printk(link, KERN_WARNING, "reset failed "
1902 "(errno=%d), retrying in %u secs\n",
1903 rc, (jiffies_to_msecs(delta) + 999) / 1000);
1905 schedule_timeout_uninterruptible(delta);
1909 try == ARRAY_SIZE(ata_eh_reset_timeouts) - 1)
1910 sata_down_spd_limit(link);
1919 /* After the reset, the device state is PIO 0 and the
1920 * controller state is undefined. Record the mode.
1922 ata_link_for_each_dev(dev, link)
1923 dev->pio_mode = XFER_PIO_0;
1925 /* record current link speed */
1926 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
1927 link->sata_spd = (sstatus >> 4) & 0xf;
1930 postreset(link, classes);
1932 /* reset successful, schedule revalidation */
1933 ata_eh_done(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
1934 ehc->i.action |= ATA_EH_REVALIDATE;
1937 /* clear hotplug flag */
1938 ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
1942 static int ata_eh_revalidate_and_attach(struct ata_link *link,
1943 struct ata_device **r_failed_dev)
1945 struct ata_port *ap = link->ap;
1946 struct ata_eh_context *ehc = &link->eh_context;
1947 struct ata_device *dev;
1948 unsigned int new_mask = 0;
1949 unsigned long flags;
1954 /* For PATA drive side cable detection to work, IDENTIFY must
1955 * be done backwards such that PDIAG- is released by the slave
1956 * device before the master device is identified.
1958 ata_link_for_each_dev_reverse(dev, link) {
1959 unsigned int action = ata_eh_dev_action(dev);
1960 unsigned int readid_flags = 0;
1962 if (ehc->i.flags & ATA_EHI_DID_RESET)
1963 readid_flags |= ATA_READID_POSTRESET;
1965 if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
1966 if (ata_link_offline(link)) {
1971 ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
1972 rc = ata_dev_revalidate(dev, readid_flags);
1976 ata_eh_done(link, dev, ATA_EH_REVALIDATE);
1978 /* Configuration may have changed, reconfigure
1981 ehc->i.flags |= ATA_EHI_SETMODE;
1983 /* schedule the scsi_rescan_device() here */
1984 queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
1985 } else if (dev->class == ATA_DEV_UNKNOWN &&
1986 ehc->tries[dev->devno] &&
1987 ata_class_enabled(ehc->classes[dev->devno])) {
1988 dev->class = ehc->classes[dev->devno];
1990 rc = ata_dev_read_id(dev, &dev->class, readid_flags,
1994 new_mask |= 1 << dev->devno;
1997 /* IDENTIFY was issued to non-existent
1998 * device. No need to reset. Just
1999 * thaw and kill the device.
2001 ata_eh_thaw_port(ap);
2002 dev->class = ATA_DEV_UNKNOWN;
2005 dev->class = ATA_DEV_UNKNOWN;
2011 /* PDIAG- should have been released, ask cable type if post-reset */
2012 if ((ehc->i.flags & ATA_EHI_DID_RESET) && ap->ops->cable_detect)
2013 ap->cbl = ap->ops->cable_detect(ap);
2015 /* Configure new devices forward such that user doesn't see
2016 * device detection messages backwards.
2018 ata_link_for_each_dev(dev, link) {
2019 if (!(new_mask & (1 << dev->devno)))
2022 ehc->i.flags |= ATA_EHI_PRINTINFO;
2023 rc = ata_dev_configure(dev);
2024 ehc->i.flags &= ~ATA_EHI_PRINTINFO;
2028 spin_lock_irqsave(ap->lock, flags);
2029 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
2030 spin_unlock_irqrestore(ap->lock, flags);
2032 /* new device discovered, configure xfermode */
2033 ehc->i.flags |= ATA_EHI_SETMODE;
2039 *r_failed_dev = dev;
2040 DPRINTK("EXIT rc=%d\n", rc);
2044 static int ata_link_nr_enabled(struct ata_link *link)
2046 struct ata_device *dev;
2049 ata_link_for_each_dev(dev, link)
2050 if (ata_dev_enabled(dev))
2055 static int ata_link_nr_vacant(struct ata_link *link)
2057 struct ata_device *dev;
2060 ata_link_for_each_dev(dev, link)
2061 if (dev->class == ATA_DEV_UNKNOWN)
2066 static int ata_eh_skip_recovery(struct ata_link *link)
2068 struct ata_eh_context *ehc = &link->eh_context;
2069 struct ata_device *dev;
2071 /* thaw frozen port, resume link and recover failed devices */
2072 if ((link->ap->pflags & ATA_PFLAG_FROZEN) ||
2073 (ehc->i.flags & ATA_EHI_RESUME_LINK) || ata_link_nr_enabled(link))
2076 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
2077 ata_link_for_each_dev(dev, link) {
2078 if (dev->class == ATA_DEV_UNKNOWN &&
2079 ehc->classes[dev->devno] != ATA_DEV_NONE)
2086 static void ata_eh_handle_dev_fail(struct ata_device *dev, int err)
2088 struct ata_eh_context *ehc = &dev->link->eh_context;
2090 ehc->tries[dev->devno]--;
2094 /* device missing or wrong IDENTIFY data, schedule probing */
2095 ehc->i.probe_mask |= (1 << dev->devno);
2097 /* give it just one more chance */
2098 ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
2100 if (ehc->tries[dev->devno] == 1) {
2101 /* This is the last chance, better to slow
2102 * down than lose it.
2104 sata_down_spd_limit(dev->link);
2105 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2109 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2110 /* disable device if it has used up all its chances */
2111 ata_dev_disable(dev);
2113 /* detach if offline */
2114 if (ata_link_offline(dev->link))
2115 ata_eh_detach_dev(dev);
2117 /* probe if requested */
2118 if ((ehc->i.probe_mask & (1 << dev->devno)) &&
2119 !(ehc->did_probe_mask & (1 << dev->devno))) {
2120 ata_eh_detach_dev(dev);
2123 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2124 ehc->did_probe_mask |= (1 << dev->devno);
2125 ehc->i.action |= ATA_EH_SOFTRESET;
2128 /* soft didn't work? be haaaaard */
2129 if (ehc->i.flags & ATA_EHI_DID_RESET)
2130 ehc->i.action |= ATA_EH_HARDRESET;
2132 ehc->i.action |= ATA_EH_SOFTRESET;
2137 * ata_eh_recover - recover host port after error
2138 * @ap: host port to recover
2139 * @prereset: prereset method (can be NULL)
2140 * @softreset: softreset method (can be NULL)
2141 * @hardreset: hardreset method (can be NULL)
2142 * @postreset: postreset method (can be NULL)
2144 * This is the alpha and omega, eum and yang, heart and soul of
2145 * libata exception handling. On entry, actions required to
2146 * recover the port and hotplug requests are recorded in
2147 * eh_context. This function executes all the operations with
2148 * appropriate retrials and fallbacks to resurrect failed
2149 * devices, detach goners and greet newcomers.
2152 * Kernel thread context (may sleep).
2155 * 0 on success, -errno on failure.
2157 static int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
2158 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2159 ata_postreset_fn_t postreset)
2161 struct ata_link *link = &ap->link;
2162 struct ata_eh_context *ehc = &link->eh_context;
2163 struct ata_device *dev;
2168 /* prep for recovery */
2169 ata_link_for_each_dev(dev, link) {
2170 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2172 /* collect port action mask recorded in dev actions */
2174 ehc->i.dev_action[dev->devno] & ~ATA_EH_PERDEV_MASK;
2175 ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
2177 /* process hotplug request */
2178 if (dev->flags & ATA_DFLAG_DETACH)
2179 ata_eh_detach_dev(dev);
2181 if (!ata_dev_enabled(dev) &&
2182 ((ehc->i.probe_mask & (1 << dev->devno)) &&
2183 !(ehc->did_probe_mask & (1 << dev->devno)))) {
2184 ata_eh_detach_dev(dev);
2186 ehc->did_probe_mask |= (1 << dev->devno);
2187 ehc->i.action |= ATA_EH_SOFTRESET;
2194 /* if UNLOADING, finish immediately */
2195 if (ap->pflags & ATA_PFLAG_UNLOADING)
2198 /* skip EH if possible. */
2199 if (ata_eh_skip_recovery(link))
2202 ata_link_for_each_dev(dev, link)
2203 ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
2206 if (ehc->i.action & ATA_EH_RESET_MASK) {
2207 ata_eh_freeze_port(ap);
2209 rc = ata_eh_reset(link, ata_link_nr_vacant(link), prereset,
2210 softreset, hardreset, postreset);
2212 ata_link_printk(link, KERN_ERR,
2213 "reset failed, giving up\n");
2217 ata_eh_thaw_port(ap);
2220 /* revalidate existing devices and attach new ones */
2221 rc = ata_eh_revalidate_and_attach(link, &dev);
2225 /* configure transfer mode if necessary */
2226 if (ehc->i.flags & ATA_EHI_SETMODE) {
2227 rc = ata_set_mode(link, &dev);
2230 ehc->i.flags &= ~ATA_EHI_SETMODE;
2236 ata_eh_handle_dev_fail(dev, rc);
2238 if (ata_link_nr_enabled(link)) {
2239 ata_link_printk(link, KERN_WARNING, "failed to recover some "
2240 "devices, retrying in 5 secs\n");
2243 /* no device left, repeat fast */
2251 ata_link_for_each_dev(dev, link);
2252 ata_dev_disable(dev);
2255 DPRINTK("EXIT, rc=%d\n", rc);
2260 * ata_eh_finish - finish up EH
2261 * @ap: host port to finish EH for
2263 * Recovery is complete. Clean up EH states and retry or finish
2269 static void ata_eh_finish(struct ata_port *ap)
2273 /* retry or finish qcs */
2274 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2275 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2277 if (!(qc->flags & ATA_QCFLAG_FAILED))
2281 /* FIXME: Once EH migration is complete,
2282 * generate sense data in this function,
2283 * considering both err_mask and tf.
2285 if (qc->err_mask & AC_ERR_INVALID)
2286 ata_eh_qc_complete(qc);
2288 ata_eh_qc_retry(qc);
2290 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2291 ata_eh_qc_complete(qc);
2293 /* feed zero TF to sense generation */
2294 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2295 ata_eh_qc_retry(qc);
2302 * ata_do_eh - do standard error handling
2303 * @ap: host port to handle error for
2304 * @prereset: prereset method (can be NULL)
2305 * @softreset: softreset method (can be NULL)
2306 * @hardreset: hardreset method (can be NULL)
2307 * @postreset: postreset method (can be NULL)
2309 * Perform standard error handling sequence.
2312 * Kernel thread context (may sleep).
2314 void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2315 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2316 ata_postreset_fn_t postreset)
2318 ata_eh_autopsy(&ap->link);
2319 ata_eh_report(&ap->link);
2320 ata_eh_recover(ap, prereset, softreset, hardreset, postreset);
2326 * ata_eh_handle_port_suspend - perform port suspend operation
2327 * @ap: port to suspend
2332 * Kernel thread context (may sleep).
2334 static void ata_eh_handle_port_suspend(struct ata_port *ap)
2336 unsigned long flags;
2339 /* are we suspending? */
2340 spin_lock_irqsave(ap->lock, flags);
2341 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2342 ap->pm_mesg.event == PM_EVENT_ON) {
2343 spin_unlock_irqrestore(ap->lock, flags);
2346 spin_unlock_irqrestore(ap->lock, flags);
2348 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
2350 /* tell ACPI we're suspending */
2351 rc = ata_acpi_on_suspend(ap);
2356 ata_eh_freeze_port(ap);
2358 if (ap->ops->port_suspend)
2359 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
2363 spin_lock_irqsave(ap->lock, flags);
2365 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
2367 ap->pflags |= ATA_PFLAG_SUSPENDED;
2368 else if (ap->pflags & ATA_PFLAG_FROZEN)
2369 ata_port_schedule_eh(ap);
2371 if (ap->pm_result) {
2372 *ap->pm_result = rc;
2373 ap->pm_result = NULL;
2376 spin_unlock_irqrestore(ap->lock, flags);
2382 * ata_eh_handle_port_resume - perform port resume operation
2383 * @ap: port to resume
2388 * Kernel thread context (may sleep).
2390 static void ata_eh_handle_port_resume(struct ata_port *ap)
2392 unsigned long flags;
2395 /* are we resuming? */
2396 spin_lock_irqsave(ap->lock, flags);
2397 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2398 ap->pm_mesg.event != PM_EVENT_ON) {
2399 spin_unlock_irqrestore(ap->lock, flags);
2402 spin_unlock_irqrestore(ap->lock, flags);
2404 WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
2406 if (ap->ops->port_resume)
2407 rc = ap->ops->port_resume(ap);
2409 /* tell ACPI that we're resuming */
2410 ata_acpi_on_resume(ap);
2413 spin_lock_irqsave(ap->lock, flags);
2414 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
2415 if (ap->pm_result) {
2416 *ap->pm_result = rc;
2417 ap->pm_result = NULL;
2419 spin_unlock_irqrestore(ap->lock, flags);
2421 #endif /* CONFIG_PM */