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/config.h>
36 #include <linux/kernel.h>
37 #include <scsi/scsi.h>
38 #include <scsi/scsi_host.h>
39 #include <scsi/scsi_eh.h>
40 #include <scsi/scsi_device.h>
41 #include <scsi/scsi_cmnd.h>
43 #include <linux/libata.h>
48 * ata_scsi_timed_out - SCSI layer time out callback
49 * @cmd: timed out SCSI command
51 * Handles SCSI layer timeout. We race with normal completion of
52 * the qc for @cmd. If the qc is already gone, we lose and let
53 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
54 * timed out and EH should be invoked. Prevent ata_qc_complete()
55 * from finishing it by setting EH_SCHEDULED and return
59 * Called from timer context
62 * EH_HANDLED or EH_NOT_HANDLED
64 enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
66 struct Scsi_Host *host = cmd->device->host;
67 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
69 struct ata_queued_cmd *qc;
70 enum scsi_eh_timer_return ret = EH_HANDLED;
74 spin_lock_irqsave(&ap->host_set->lock, flags);
75 qc = ata_qc_from_tag(ap, ap->active_tag);
77 WARN_ON(qc->scsicmd != cmd);
78 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
79 qc->err_mask |= AC_ERR_TIMEOUT;
82 spin_unlock_irqrestore(&ap->host_set->lock, flags);
84 DPRINTK("EXIT, ret=%d\n", ret);
89 * ata_scsi_error - SCSI layer error handler callback
90 * @host: SCSI host on which error occurred
92 * Handles SCSI-layer-thrown error events.
95 * Inherited from SCSI layer (none, can sleep)
100 int ata_scsi_error(struct Scsi_Host *host)
102 struct ata_port *ap = (struct ata_port *)&host->hostdata[0];
106 /* synchronize with IRQ handler and port task */
107 spin_unlock_wait(&ap->host_set->lock);
108 ata_port_flush_task(ap);
110 WARN_ON(ata_qc_from_tag(ap, ap->active_tag) == NULL);
112 ap->ops->eng_timeout(ap);
114 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
116 scsi_eh_flush_done_q(&ap->eh_done_q);
123 * ata_qc_timeout - Handle timeout of queued command
124 * @qc: Command that timed out
126 * Some part of the kernel (currently, only the SCSI layer)
127 * has noticed that the active command on port @ap has not
128 * completed after a specified length of time. Handle this
129 * condition by disabling DMA (if necessary) and completing
130 * transactions, with error if necessary.
132 * This also handles the case of the "lost interrupt", where
133 * for some reason (possibly hardware bug, possibly driver bug)
134 * an interrupt was not delivered to the driver, even though the
135 * transaction completed successfully.
138 * Inherited from SCSI layer (none, can sleep)
141 static void ata_qc_timeout(struct ata_queued_cmd *qc)
143 struct ata_port *ap = qc->ap;
144 struct ata_host_set *host_set = ap->host_set;
145 u8 host_stat = 0, drv_stat;
150 ap->hsm_task_state = HSM_ST_IDLE;
152 spin_lock_irqsave(&host_set->lock, flags);
154 switch (qc->tf.protocol) {
157 case ATA_PROT_ATAPI_DMA:
158 host_stat = ap->ops->bmdma_status(ap);
160 /* before we do anything else, clear DMA-Start bit */
161 ap->ops->bmdma_stop(qc);
167 drv_stat = ata_chk_status(ap);
169 /* ack bmdma irq events */
170 ap->ops->irq_clear(ap);
172 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
173 ap->id, qc->tf.command, drv_stat, host_stat);
175 ap->hsm_task_state = HSM_ST_IDLE;
177 /* complete taskfile transaction */
178 qc->err_mask |= AC_ERR_TIMEOUT;
182 spin_unlock_irqrestore(&host_set->lock, flags);
184 ata_eh_qc_complete(qc);
190 * ata_eng_timeout - Handle timeout of queued command
191 * @ap: Port on which timed-out command is active
193 * Some part of the kernel (currently, only the SCSI layer)
194 * has noticed that the active command on port @ap has not
195 * completed after a specified length of time. Handle this
196 * condition by disabling DMA (if necessary) and completing
197 * transactions, with error if necessary.
199 * This also handles the case of the "lost interrupt", where
200 * for some reason (possibly hardware bug, possibly driver bug)
201 * an interrupt was not delivered to the driver, even though the
202 * transaction completed successfully.
205 * Inherited from SCSI layer (none, can sleep)
207 void ata_eng_timeout(struct ata_port *ap)
211 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
216 static void ata_eh_scsidone(struct scsi_cmnd *scmd)
221 static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
223 struct ata_port *ap = qc->ap;
224 struct scsi_cmnd *scmd = qc->scsicmd;
227 spin_lock_irqsave(&ap->host_set->lock, flags);
228 qc->scsidone = ata_eh_scsidone;
229 __ata_qc_complete(qc);
230 WARN_ON(ata_tag_valid(qc->tag));
231 spin_unlock_irqrestore(&ap->host_set->lock, flags);
233 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
237 * ata_eh_qc_complete - Complete an active ATA command from EH
238 * @qc: Command to complete
240 * Indicate to the mid and upper layers that an ATA command has
241 * completed. To be used from EH.
243 void ata_eh_qc_complete(struct ata_queued_cmd *qc)
245 struct scsi_cmnd *scmd = qc->scsicmd;
246 scmd->retries = scmd->allowed;
247 __ata_eh_qc_complete(qc);
251 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
252 * @qc: Command to retry
254 * Indicate to the mid and upper layers that an ATA command
255 * should be retried. To be used from EH.
257 * SCSI midlayer limits the number of retries to scmd->allowed.
258 * scmd->retries is decremented for commands which get retried
259 * due to unrelated failures (qc->err_mask is zero).
261 void ata_eh_qc_retry(struct ata_queued_cmd *qc)
263 struct scsi_cmnd *scmd = qc->scsicmd;
264 if (!qc->err_mask && scmd->retries)
266 __ata_eh_qc_complete(qc);