[PATCH] libata-eh: implement ata_eh_info and ata_eh_context
[linux-2.6/btrfs-unstable.git] / drivers / scsi / libata-eh.c
blob1968f2d140f358247cc66ac877bab426f179307b
1 /*
2 * libata-eh.c - libata error handling
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
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,
24 * USA.
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>
45 #include "libata.h"
47 static void __ata_port_freeze(struct ata_port *ap);
49 static void ata_ering_record(struct ata_ering *ering, int is_io,
50 unsigned int err_mask)
52 struct ata_ering_entry *ent;
54 WARN_ON(!err_mask);
56 ering->cursor++;
57 ering->cursor %= ATA_ERING_SIZE;
59 ent = &ering->ring[ering->cursor];
60 ent->is_io = is_io;
61 ent->err_mask = err_mask;
62 ent->timestamp = get_jiffies_64();
65 static struct ata_ering_entry * ata_ering_top(struct ata_ering *ering)
67 struct ata_ering_entry *ent = &ering->ring[ering->cursor];
68 if (!ent->err_mask)
69 return NULL;
70 return ent;
73 static int ata_ering_map(struct ata_ering *ering,
74 int (*map_fn)(struct ata_ering_entry *, void *),
75 void *arg)
77 int idx, rc = 0;
78 struct ata_ering_entry *ent;
80 idx = ering->cursor;
81 do {
82 ent = &ering->ring[idx];
83 if (!ent->err_mask)
84 break;
85 rc = map_fn(ent, arg);
86 if (rc)
87 break;
88 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
89 } while (idx != ering->cursor);
91 return rc;
94 /**
95 * ata_scsi_timed_out - SCSI layer time out callback
96 * @cmd: timed out SCSI command
98 * Handles SCSI layer timeout. We race with normal completion of
99 * the qc for @cmd. If the qc is already gone, we lose and let
100 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
101 * timed out and EH should be invoked. Prevent ata_qc_complete()
102 * from finishing it by setting EH_SCHEDULED and return
103 * EH_NOT_HANDLED.
105 * TODO: kill this function once old EH is gone.
107 * LOCKING:
108 * Called from timer context
110 * RETURNS:
111 * EH_HANDLED or EH_NOT_HANDLED
113 enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
115 struct Scsi_Host *host = cmd->device->host;
116 struct ata_port *ap = ata_shost_to_port(host);
117 unsigned long flags;
118 struct ata_queued_cmd *qc;
119 enum scsi_eh_timer_return ret;
121 DPRINTK("ENTER\n");
123 if (ap->ops->error_handler) {
124 ret = EH_NOT_HANDLED;
125 goto out;
128 ret = EH_HANDLED;
129 spin_lock_irqsave(&ap->host_set->lock, flags);
130 qc = ata_qc_from_tag(ap, ap->active_tag);
131 if (qc) {
132 WARN_ON(qc->scsicmd != cmd);
133 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
134 qc->err_mask |= AC_ERR_TIMEOUT;
135 ret = EH_NOT_HANDLED;
137 spin_unlock_irqrestore(&ap->host_set->lock, flags);
139 out:
140 DPRINTK("EXIT, ret=%d\n", ret);
141 return ret;
145 * ata_scsi_error - SCSI layer error handler callback
146 * @host: SCSI host on which error occurred
148 * Handles SCSI-layer-thrown error events.
150 * LOCKING:
151 * Inherited from SCSI layer (none, can sleep)
153 * RETURNS:
154 * Zero.
156 void ata_scsi_error(struct Scsi_Host *host)
158 struct ata_port *ap = ata_shost_to_port(host);
159 spinlock_t *hs_lock = &ap->host_set->lock;
160 int i, repeat_cnt = ATA_EH_MAX_REPEAT;
161 unsigned long flags;
163 DPRINTK("ENTER\n");
165 /* synchronize with port task */
166 ata_port_flush_task(ap);
168 /* synchronize with host_set lock and sort out timeouts */
170 /* For new EH, all qcs are finished in one of three ways -
171 * normal completion, error completion, and SCSI timeout.
172 * Both cmpletions can race against SCSI timeout. When normal
173 * completion wins, the qc never reaches EH. When error
174 * completion wins, the qc has ATA_QCFLAG_FAILED set.
176 * When SCSI timeout wins, things are a bit more complex.
177 * Normal or error completion can occur after the timeout but
178 * before this point. In such cases, both types of
179 * completions are honored. A scmd is determined to have
180 * timed out iff its associated qc is active and not failed.
182 if (ap->ops->error_handler) {
183 struct scsi_cmnd *scmd, *tmp;
184 int nr_timedout = 0;
186 spin_lock_irqsave(hs_lock, flags);
188 list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
189 struct ata_queued_cmd *qc;
191 for (i = 0; i < ATA_MAX_QUEUE; i++) {
192 qc = __ata_qc_from_tag(ap, i);
193 if (qc->flags & ATA_QCFLAG_ACTIVE &&
194 qc->scsicmd == scmd)
195 break;
198 if (i < ATA_MAX_QUEUE) {
199 /* the scmd has an associated qc */
200 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
201 /* which hasn't failed yet, timeout */
202 qc->err_mask |= AC_ERR_TIMEOUT;
203 qc->flags |= ATA_QCFLAG_FAILED;
204 nr_timedout++;
206 } else {
207 /* Normal completion occurred after
208 * SCSI timeout but before this point.
209 * Successfully complete it.
211 scmd->retries = scmd->allowed;
212 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
216 /* If we have timed out qcs. They belong to EH from
217 * this point but the state of the controller is
218 * unknown. Freeze the port to make sure the IRQ
219 * handler doesn't diddle with those qcs. This must
220 * be done atomically w.r.t. setting QCFLAG_FAILED.
222 if (nr_timedout)
223 __ata_port_freeze(ap);
225 spin_unlock_irqrestore(hs_lock, flags);
226 } else
227 spin_unlock_wait(hs_lock);
229 repeat:
230 /* invoke error handler */
231 if (ap->ops->error_handler) {
232 /* fetch & clear EH info */
233 spin_lock_irqsave(hs_lock, flags);
235 memset(&ap->eh_context, 0, sizeof(ap->eh_context));
236 ap->eh_context.i = ap->eh_info;
237 memset(&ap->eh_info, 0, sizeof(ap->eh_info));
239 ap->flags &= ~ATA_FLAG_EH_PENDING;
241 spin_unlock_irqrestore(hs_lock, flags);
243 /* invoke EH */
244 ap->ops->error_handler(ap);
246 /* Exception might have happend after ->error_handler
247 * recovered the port but before this point. Repeat
248 * EH in such case.
250 spin_lock_irqsave(hs_lock, flags);
252 if (ap->flags & ATA_FLAG_EH_PENDING) {
253 if (--repeat_cnt) {
254 ata_port_printk(ap, KERN_INFO,
255 "EH pending after completion, "
256 "repeating EH (cnt=%d)\n", repeat_cnt);
257 spin_unlock_irqrestore(hs_lock, flags);
258 goto repeat;
260 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
261 "tries, giving up\n", ATA_EH_MAX_REPEAT);
264 /* this run is complete, make sure EH info is clear */
265 memset(&ap->eh_info, 0, sizeof(ap->eh_info));
267 /* Clear host_eh_scheduled while holding hs_lock such
268 * that if exception occurs after this point but
269 * before EH completion, SCSI midlayer will
270 * re-initiate EH.
272 host->host_eh_scheduled = 0;
274 spin_unlock_irqrestore(hs_lock, flags);
275 } else {
276 WARN_ON(ata_qc_from_tag(ap, ap->active_tag) == NULL);
277 ap->ops->eng_timeout(ap);
280 /* finish or retry handled scmd's and clean up */
281 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
283 scsi_eh_flush_done_q(&ap->eh_done_q);
285 /* clean up */
286 spin_lock_irqsave(hs_lock, flags);
288 if (ap->flags & ATA_FLAG_RECOVERED)
289 ata_port_printk(ap, KERN_INFO, "EH complete\n");
290 ap->flags &= ~ATA_FLAG_RECOVERED;
292 spin_unlock_irqrestore(hs_lock, flags);
294 DPRINTK("EXIT\n");
298 * ata_qc_timeout - Handle timeout of queued command
299 * @qc: Command that timed out
301 * Some part of the kernel (currently, only the SCSI layer)
302 * has noticed that the active command on port @ap has not
303 * completed after a specified length of time. Handle this
304 * condition by disabling DMA (if necessary) and completing
305 * transactions, with error if necessary.
307 * This also handles the case of the "lost interrupt", where
308 * for some reason (possibly hardware bug, possibly driver bug)
309 * an interrupt was not delivered to the driver, even though the
310 * transaction completed successfully.
312 * TODO: kill this function once old EH is gone.
314 * LOCKING:
315 * Inherited from SCSI layer (none, can sleep)
317 static void ata_qc_timeout(struct ata_queued_cmd *qc)
319 struct ata_port *ap = qc->ap;
320 struct ata_host_set *host_set = ap->host_set;
321 u8 host_stat = 0, drv_stat;
322 unsigned long flags;
324 DPRINTK("ENTER\n");
326 ap->hsm_task_state = HSM_ST_IDLE;
328 spin_lock_irqsave(&host_set->lock, flags);
330 switch (qc->tf.protocol) {
332 case ATA_PROT_DMA:
333 case ATA_PROT_ATAPI_DMA:
334 host_stat = ap->ops->bmdma_status(ap);
336 /* before we do anything else, clear DMA-Start bit */
337 ap->ops->bmdma_stop(qc);
339 /* fall through */
341 default:
342 ata_altstatus(ap);
343 drv_stat = ata_chk_status(ap);
345 /* ack bmdma irq events */
346 ap->ops->irq_clear(ap);
348 ata_dev_printk(qc->dev, KERN_ERR, "command 0x%x timeout, "
349 "stat 0x%x host_stat 0x%x\n",
350 qc->tf.command, drv_stat, host_stat);
352 /* complete taskfile transaction */
353 qc->err_mask |= ac_err_mask(drv_stat);
354 break;
357 spin_unlock_irqrestore(&host_set->lock, flags);
359 ata_eh_qc_complete(qc);
361 DPRINTK("EXIT\n");
365 * ata_eng_timeout - Handle timeout of queued command
366 * @ap: Port on which timed-out command is active
368 * Some part of the kernel (currently, only the SCSI layer)
369 * has noticed that the active command on port @ap has not
370 * completed after a specified length of time. Handle this
371 * condition by disabling DMA (if necessary) and completing
372 * transactions, with error if necessary.
374 * This also handles the case of the "lost interrupt", where
375 * for some reason (possibly hardware bug, possibly driver bug)
376 * an interrupt was not delivered to the driver, even though the
377 * transaction completed successfully.
379 * TODO: kill this function once old EH is gone.
381 * LOCKING:
382 * Inherited from SCSI layer (none, can sleep)
384 void ata_eng_timeout(struct ata_port *ap)
386 DPRINTK("ENTER\n");
388 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
390 DPRINTK("EXIT\n");
394 * ata_qc_schedule_eh - schedule qc for error handling
395 * @qc: command to schedule error handling for
397 * Schedule error handling for @qc. EH will kick in as soon as
398 * other commands are drained.
400 * LOCKING:
401 * spin_lock_irqsave(host_set lock)
403 void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
405 struct ata_port *ap = qc->ap;
407 WARN_ON(!ap->ops->error_handler);
409 qc->flags |= ATA_QCFLAG_FAILED;
410 qc->ap->flags |= ATA_FLAG_EH_PENDING;
412 /* The following will fail if timeout has already expired.
413 * ata_scsi_error() takes care of such scmds on EH entry.
414 * Note that ATA_QCFLAG_FAILED is unconditionally set after
415 * this function completes.
417 scsi_req_abort_cmd(qc->scsicmd);
421 * ata_port_schedule_eh - schedule error handling without a qc
422 * @ap: ATA port to schedule EH for
424 * Schedule error handling for @ap. EH will kick in as soon as
425 * all commands are drained.
427 * LOCKING:
428 * spin_lock_irqsave(host_set lock)
430 void ata_port_schedule_eh(struct ata_port *ap)
432 WARN_ON(!ap->ops->error_handler);
434 ap->flags |= ATA_FLAG_EH_PENDING;
435 ata_schedule_scsi_eh(ap->host);
437 DPRINTK("port EH scheduled\n");
441 * ata_port_abort - abort all qc's on the port
442 * @ap: ATA port to abort qc's for
444 * Abort all active qc's of @ap and schedule EH.
446 * LOCKING:
447 * spin_lock_irqsave(host_set lock)
449 * RETURNS:
450 * Number of aborted qc's.
452 int ata_port_abort(struct ata_port *ap)
454 int tag, nr_aborted = 0;
456 WARN_ON(!ap->ops->error_handler);
458 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
459 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
461 if (qc) {
462 qc->flags |= ATA_QCFLAG_FAILED;
463 ata_qc_complete(qc);
464 nr_aborted++;
468 if (!nr_aborted)
469 ata_port_schedule_eh(ap);
471 return nr_aborted;
475 * __ata_port_freeze - freeze port
476 * @ap: ATA port to freeze
478 * This function is called when HSM violation or some other
479 * condition disrupts normal operation of the port. Frozen port
480 * is not allowed to perform any operation until the port is
481 * thawed, which usually follows a successful reset.
483 * ap->ops->freeze() callback can be used for freezing the port
484 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
485 * port cannot be frozen hardware-wise, the interrupt handler
486 * must ack and clear interrupts unconditionally while the port
487 * is frozen.
489 * LOCKING:
490 * spin_lock_irqsave(host_set lock)
492 static void __ata_port_freeze(struct ata_port *ap)
494 WARN_ON(!ap->ops->error_handler);
496 if (ap->ops->freeze)
497 ap->ops->freeze(ap);
499 ap->flags |= ATA_FLAG_FROZEN;
501 DPRINTK("ata%u port frozen\n", ap->id);
505 * ata_port_freeze - abort & freeze port
506 * @ap: ATA port to freeze
508 * Abort and freeze @ap.
510 * LOCKING:
511 * spin_lock_irqsave(host_set lock)
513 * RETURNS:
514 * Number of aborted commands.
516 int ata_port_freeze(struct ata_port *ap)
518 int nr_aborted;
520 WARN_ON(!ap->ops->error_handler);
522 nr_aborted = ata_port_abort(ap);
523 __ata_port_freeze(ap);
525 return nr_aborted;
529 * ata_eh_freeze_port - EH helper to freeze port
530 * @ap: ATA port to freeze
532 * Freeze @ap.
534 * LOCKING:
535 * None.
537 void ata_eh_freeze_port(struct ata_port *ap)
539 unsigned long flags;
541 if (!ap->ops->error_handler)
542 return;
544 spin_lock_irqsave(&ap->host_set->lock, flags);
545 __ata_port_freeze(ap);
546 spin_unlock_irqrestore(&ap->host_set->lock, flags);
550 * ata_port_thaw_port - EH helper to thaw port
551 * @ap: ATA port to thaw
553 * Thaw frozen port @ap.
555 * LOCKING:
556 * None.
558 void ata_eh_thaw_port(struct ata_port *ap)
560 unsigned long flags;
562 if (!ap->ops->error_handler)
563 return;
565 spin_lock_irqsave(&ap->host_set->lock, flags);
567 ap->flags &= ~ATA_FLAG_FROZEN;
569 if (ap->ops->thaw)
570 ap->ops->thaw(ap);
572 spin_unlock_irqrestore(&ap->host_set->lock, flags);
574 DPRINTK("ata%u port thawed\n", ap->id);
577 static void ata_eh_scsidone(struct scsi_cmnd *scmd)
579 /* nada */
582 static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
584 struct ata_port *ap = qc->ap;
585 struct scsi_cmnd *scmd = qc->scsicmd;
586 unsigned long flags;
588 spin_lock_irqsave(&ap->host_set->lock, flags);
589 qc->scsidone = ata_eh_scsidone;
590 __ata_qc_complete(qc);
591 WARN_ON(ata_tag_valid(qc->tag));
592 spin_unlock_irqrestore(&ap->host_set->lock, flags);
594 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
598 * ata_eh_qc_complete - Complete an active ATA command from EH
599 * @qc: Command to complete
601 * Indicate to the mid and upper layers that an ATA command has
602 * completed. To be used from EH.
604 void ata_eh_qc_complete(struct ata_queued_cmd *qc)
606 struct scsi_cmnd *scmd = qc->scsicmd;
607 scmd->retries = scmd->allowed;
608 __ata_eh_qc_complete(qc);
612 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
613 * @qc: Command to retry
615 * Indicate to the mid and upper layers that an ATA command
616 * should be retried. To be used from EH.
618 * SCSI midlayer limits the number of retries to scmd->allowed.
619 * scmd->retries is decremented for commands which get retried
620 * due to unrelated failures (qc->err_mask is zero).
622 void ata_eh_qc_retry(struct ata_queued_cmd *qc)
624 struct scsi_cmnd *scmd = qc->scsicmd;
625 if (!qc->err_mask && scmd->retries)
626 scmd->retries--;
627 __ata_eh_qc_complete(qc);