blob: a049bffdf7705e279429b37a58adff59920c58c4 [file] [log] [blame]
Tejun Heoece1d632006-04-02 18:51:53 +09001/*
2 * libata-eh.c - libata error handling
3 *
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
7 *
8 * Copyright 2006 Tejun Heo <htejun@gmail.com>
9 *
10 *
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.
15 *
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.
20 *
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.
25 *
26 *
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
29 *
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
32 *
33 */
34
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>
Tejun Heof8bbfc22006-05-19 21:07:05 +090042#include "scsi_transport_api.h"
Tejun Heoece1d632006-04-02 18:51:53 +090043
44#include <linux/libata.h>
45
46#include "libata.h"
47
Tejun Heoad9e2762006-05-15 20:58:12 +090048static void __ata_port_freeze(struct ata_port *ap);
49
Tejun Heo0c247c52006-05-15 20:58:19 +090050static void ata_ering_record(struct ata_ering *ering, int is_io,
51 unsigned int err_mask)
52{
53 struct ata_ering_entry *ent;
54
55 WARN_ON(!err_mask);
56
57 ering->cursor++;
58 ering->cursor %= ATA_ERING_SIZE;
59
60 ent = &ering->ring[ering->cursor];
61 ent->is_io = is_io;
62 ent->err_mask = err_mask;
63 ent->timestamp = get_jiffies_64();
64}
65
66static struct ata_ering_entry * ata_ering_top(struct ata_ering *ering)
67{
68 struct ata_ering_entry *ent = &ering->ring[ering->cursor];
69 if (!ent->err_mask)
70 return NULL;
71 return ent;
72}
73
74static int ata_ering_map(struct ata_ering *ering,
75 int (*map_fn)(struct ata_ering_entry *, void *),
76 void *arg)
77{
78 int idx, rc = 0;
79 struct ata_ering_entry *ent;
80
81 idx = ering->cursor;
82 do {
83 ent = &ering->ring[idx];
84 if (!ent->err_mask)
85 break;
86 rc = map_fn(ent, arg);
87 if (rc)
88 break;
89 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
90 } while (idx != ering->cursor);
91
92 return rc;
93}
94
Tejun Heoece1d632006-04-02 18:51:53 +090095/**
96 * ata_scsi_timed_out - SCSI layer time out callback
97 * @cmd: timed out SCSI command
98 *
99 * Handles SCSI layer timeout. We race with normal completion of
100 * the qc for @cmd. If the qc is already gone, we lose and let
101 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
102 * timed out and EH should be invoked. Prevent ata_qc_complete()
103 * from finishing it by setting EH_SCHEDULED and return
104 * EH_NOT_HANDLED.
105 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900106 * TODO: kill this function once old EH is gone.
107 *
Tejun Heoece1d632006-04-02 18:51:53 +0900108 * LOCKING:
109 * Called from timer context
110 *
111 * RETURNS:
112 * EH_HANDLED or EH_NOT_HANDLED
113 */
114enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
115{
116 struct Scsi_Host *host = cmd->device->host;
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400117 struct ata_port *ap = ata_shost_to_port(host);
Tejun Heoece1d632006-04-02 18:51:53 +0900118 unsigned long flags;
119 struct ata_queued_cmd *qc;
Tejun Heoad9e2762006-05-15 20:58:12 +0900120 enum scsi_eh_timer_return ret;
Tejun Heoece1d632006-04-02 18:51:53 +0900121
122 DPRINTK("ENTER\n");
123
Tejun Heoad9e2762006-05-15 20:58:12 +0900124 if (ap->ops->error_handler) {
125 ret = EH_NOT_HANDLED;
126 goto out;
127 }
128
129 ret = EH_HANDLED;
Tejun Heoece1d632006-04-02 18:51:53 +0900130 spin_lock_irqsave(&ap->host_set->lock, flags);
131 qc = ata_qc_from_tag(ap, ap->active_tag);
132 if (qc) {
133 WARN_ON(qc->scsicmd != cmd);
134 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
135 qc->err_mask |= AC_ERR_TIMEOUT;
136 ret = EH_NOT_HANDLED;
137 }
138 spin_unlock_irqrestore(&ap->host_set->lock, flags);
139
Tejun Heoad9e2762006-05-15 20:58:12 +0900140 out:
Tejun Heoece1d632006-04-02 18:51:53 +0900141 DPRINTK("EXIT, ret=%d\n", ret);
142 return ret;
143}
144
145/**
146 * ata_scsi_error - SCSI layer error handler callback
147 * @host: SCSI host on which error occurred
148 *
149 * Handles SCSI-layer-thrown error events.
150 *
151 * LOCKING:
152 * Inherited from SCSI layer (none, can sleep)
153 *
154 * RETURNS:
155 * Zero.
156 */
Jeff Garzik381544b2006-04-11 13:04:39 -0400157void ata_scsi_error(struct Scsi_Host *host)
Tejun Heoece1d632006-04-02 18:51:53 +0900158{
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400159 struct ata_port *ap = ata_shost_to_port(host);
Tejun Heoad9e2762006-05-15 20:58:12 +0900160 spinlock_t *hs_lock = &ap->host_set->lock;
161 int i, repeat_cnt = ATA_EH_MAX_REPEAT;
162 unsigned long flags;
Tejun Heoece1d632006-04-02 18:51:53 +0900163
164 DPRINTK("ENTER\n");
165
Tejun Heoad9e2762006-05-15 20:58:12 +0900166 /* synchronize with port task */
Tejun Heoece1d632006-04-02 18:51:53 +0900167 ata_port_flush_task(ap);
168
Tejun Heoad9e2762006-05-15 20:58:12 +0900169 /* synchronize with host_set lock and sort out timeouts */
Tejun Heoece1d632006-04-02 18:51:53 +0900170
Tejun Heoad9e2762006-05-15 20:58:12 +0900171 /* For new EH, all qcs are finished in one of three ways -
172 * normal completion, error completion, and SCSI timeout.
173 * Both cmpletions can race against SCSI timeout. When normal
174 * completion wins, the qc never reaches EH. When error
175 * completion wins, the qc has ATA_QCFLAG_FAILED set.
176 *
177 * When SCSI timeout wins, things are a bit more complex.
178 * Normal or error completion can occur after the timeout but
179 * before this point. In such cases, both types of
180 * completions are honored. A scmd is determined to have
181 * timed out iff its associated qc is active and not failed.
182 */
183 if (ap->ops->error_handler) {
184 struct scsi_cmnd *scmd, *tmp;
185 int nr_timedout = 0;
Tejun Heoece1d632006-04-02 18:51:53 +0900186
Tejun Heoad9e2762006-05-15 20:58:12 +0900187 spin_lock_irqsave(hs_lock, flags);
188
189 list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
190 struct ata_queued_cmd *qc;
191
192 for (i = 0; i < ATA_MAX_QUEUE; i++) {
193 qc = __ata_qc_from_tag(ap, i);
194 if (qc->flags & ATA_QCFLAG_ACTIVE &&
195 qc->scsicmd == scmd)
196 break;
197 }
198
199 if (i < ATA_MAX_QUEUE) {
200 /* the scmd has an associated qc */
201 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
202 /* which hasn't failed yet, timeout */
203 qc->err_mask |= AC_ERR_TIMEOUT;
204 qc->flags |= ATA_QCFLAG_FAILED;
205 nr_timedout++;
206 }
207 } else {
208 /* Normal completion occurred after
209 * SCSI timeout but before this point.
210 * Successfully complete it.
211 */
212 scmd->retries = scmd->allowed;
213 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
214 }
215 }
216
217 /* If we have timed out qcs. They belong to EH from
218 * this point but the state of the controller is
219 * unknown. Freeze the port to make sure the IRQ
220 * handler doesn't diddle with those qcs. This must
221 * be done atomically w.r.t. setting QCFLAG_FAILED.
222 */
223 if (nr_timedout)
224 __ata_port_freeze(ap);
225
226 spin_unlock_irqrestore(hs_lock, flags);
227 } else
228 spin_unlock_wait(hs_lock);
229
230 repeat:
231 /* invoke error handler */
232 if (ap->ops->error_handler) {
Tejun Heof3e81b12006-05-15 20:58:21 +0900233 /* fetch & clear EH info */
Tejun Heoad9e2762006-05-15 20:58:12 +0900234 spin_lock_irqsave(hs_lock, flags);
Tejun Heof3e81b12006-05-15 20:58:21 +0900235
236 memset(&ap->eh_context, 0, sizeof(ap->eh_context));
237 ap->eh_context.i = ap->eh_info;
238 memset(&ap->eh_info, 0, sizeof(ap->eh_info));
239
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900240 ap->flags |= ATA_FLAG_EH_IN_PROGRESS;
Tejun Heoad9e2762006-05-15 20:58:12 +0900241 ap->flags &= ~ATA_FLAG_EH_PENDING;
Tejun Heof3e81b12006-05-15 20:58:21 +0900242
Tejun Heoad9e2762006-05-15 20:58:12 +0900243 spin_unlock_irqrestore(hs_lock, flags);
244
245 /* invoke EH */
246 ap->ops->error_handler(ap);
247
248 /* Exception might have happend after ->error_handler
249 * recovered the port but before this point. Repeat
250 * EH in such case.
251 */
252 spin_lock_irqsave(hs_lock, flags);
253
254 if (ap->flags & ATA_FLAG_EH_PENDING) {
255 if (--repeat_cnt) {
256 ata_port_printk(ap, KERN_INFO,
257 "EH pending after completion, "
258 "repeating EH (cnt=%d)\n", repeat_cnt);
259 spin_unlock_irqrestore(hs_lock, flags);
260 goto repeat;
261 }
262 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
263 "tries, giving up\n", ATA_EH_MAX_REPEAT);
264 }
265
Tejun Heof3e81b12006-05-15 20:58:21 +0900266 /* this run is complete, make sure EH info is clear */
267 memset(&ap->eh_info, 0, sizeof(ap->eh_info));
268
Tejun Heoad9e2762006-05-15 20:58:12 +0900269 /* Clear host_eh_scheduled while holding hs_lock such
270 * that if exception occurs after this point but
271 * before EH completion, SCSI midlayer will
272 * re-initiate EH.
273 */
274 host->host_eh_scheduled = 0;
275
276 spin_unlock_irqrestore(hs_lock, flags);
277 } else {
278 WARN_ON(ata_qc_from_tag(ap, ap->active_tag) == NULL);
279 ap->ops->eng_timeout(ap);
280 }
281
282 /* finish or retry handled scmd's and clean up */
Tejun Heoece1d632006-04-02 18:51:53 +0900283 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
284
285 scsi_eh_flush_done_q(&ap->eh_done_q);
286
Tejun Heoad9e2762006-05-15 20:58:12 +0900287 /* clean up */
288 spin_lock_irqsave(hs_lock, flags);
289
290 if (ap->flags & ATA_FLAG_RECOVERED)
291 ata_port_printk(ap, KERN_INFO, "EH complete\n");
292 ap->flags &= ~ATA_FLAG_RECOVERED;
293
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900294 /* tell wait_eh that we're done */
295 ap->flags &= ~ATA_FLAG_EH_IN_PROGRESS;
296 wake_up_all(&ap->eh_wait_q);
297
Tejun Heoad9e2762006-05-15 20:58:12 +0900298 spin_unlock_irqrestore(hs_lock, flags);
299
Tejun Heoece1d632006-04-02 18:51:53 +0900300 DPRINTK("EXIT\n");
Tejun Heoece1d632006-04-02 18:51:53 +0900301}
302
303/**
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900304 * ata_port_wait_eh - Wait for the currently pending EH to complete
305 * @ap: Port to wait EH for
306 *
307 * Wait until the currently pending EH is complete.
308 *
309 * LOCKING:
310 * Kernel thread context (may sleep).
311 */
312void ata_port_wait_eh(struct ata_port *ap)
313{
314 unsigned long flags;
315 DEFINE_WAIT(wait);
316
317 retry:
318 spin_lock_irqsave(&ap->host_set->lock, flags);
319
320 while (ap->flags & (ATA_FLAG_EH_PENDING | ATA_FLAG_EH_IN_PROGRESS)) {
321 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
322 spin_unlock_irqrestore(&ap->host_set->lock, flags);
323 schedule();
324 spin_lock_irqsave(&ap->host_set->lock, flags);
325 }
326
327 spin_unlock_irqrestore(&ap->host_set->lock, flags);
328
329 /* make sure SCSI EH is complete */
330 if (scsi_host_in_recovery(ap->host)) {
331 msleep(10);
332 goto retry;
333 }
334}
335
336/**
Tejun Heoece1d632006-04-02 18:51:53 +0900337 * ata_qc_timeout - Handle timeout of queued command
338 * @qc: Command that timed out
339 *
340 * Some part of the kernel (currently, only the SCSI layer)
341 * has noticed that the active command on port @ap has not
342 * completed after a specified length of time. Handle this
343 * condition by disabling DMA (if necessary) and completing
344 * transactions, with error if necessary.
345 *
346 * This also handles the case of the "lost interrupt", where
347 * for some reason (possibly hardware bug, possibly driver bug)
348 * an interrupt was not delivered to the driver, even though the
349 * transaction completed successfully.
350 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900351 * TODO: kill this function once old EH is gone.
352 *
Tejun Heoece1d632006-04-02 18:51:53 +0900353 * LOCKING:
354 * Inherited from SCSI layer (none, can sleep)
355 */
356static void ata_qc_timeout(struct ata_queued_cmd *qc)
357{
358 struct ata_port *ap = qc->ap;
359 struct ata_host_set *host_set = ap->host_set;
360 u8 host_stat = 0, drv_stat;
361 unsigned long flags;
362
363 DPRINTK("ENTER\n");
364
365 ap->hsm_task_state = HSM_ST_IDLE;
366
367 spin_lock_irqsave(&host_set->lock, flags);
368
369 switch (qc->tf.protocol) {
370
371 case ATA_PROT_DMA:
372 case ATA_PROT_ATAPI_DMA:
373 host_stat = ap->ops->bmdma_status(ap);
374
375 /* before we do anything else, clear DMA-Start bit */
376 ap->ops->bmdma_stop(qc);
377
378 /* fall through */
379
380 default:
381 ata_altstatus(ap);
382 drv_stat = ata_chk_status(ap);
383
384 /* ack bmdma irq events */
385 ap->ops->irq_clear(ap);
386
Tejun Heof15a1da2006-05-15 20:57:56 +0900387 ata_dev_printk(qc->dev, KERN_ERR, "command 0x%x timeout, "
388 "stat 0x%x host_stat 0x%x\n",
389 qc->tf.command, drv_stat, host_stat);
Tejun Heoece1d632006-04-02 18:51:53 +0900390
391 /* complete taskfile transaction */
Jeff Garzikc13b56a2006-04-02 10:34:24 -0400392 qc->err_mask |= AC_ERR_TIMEOUT;
Tejun Heoece1d632006-04-02 18:51:53 +0900393 break;
394 }
395
396 spin_unlock_irqrestore(&host_set->lock, flags);
397
398 ata_eh_qc_complete(qc);
399
400 DPRINTK("EXIT\n");
401}
402
403/**
404 * ata_eng_timeout - Handle timeout of queued command
405 * @ap: Port on which timed-out command is active
406 *
407 * Some part of the kernel (currently, only the SCSI layer)
408 * has noticed that the active command on port @ap has not
409 * completed after a specified length of time. Handle this
410 * condition by disabling DMA (if necessary) and completing
411 * transactions, with error if necessary.
412 *
413 * This also handles the case of the "lost interrupt", where
414 * for some reason (possibly hardware bug, possibly driver bug)
415 * an interrupt was not delivered to the driver, even though the
416 * transaction completed successfully.
417 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900418 * TODO: kill this function once old EH is gone.
419 *
Tejun Heoece1d632006-04-02 18:51:53 +0900420 * LOCKING:
421 * Inherited from SCSI layer (none, can sleep)
422 */
423void ata_eng_timeout(struct ata_port *ap)
424{
425 DPRINTK("ENTER\n");
426
427 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
428
429 DPRINTK("EXIT\n");
430}
431
Tejun Heof686bcb2006-05-15 20:58:05 +0900432/**
433 * ata_qc_schedule_eh - schedule qc for error handling
434 * @qc: command to schedule error handling for
435 *
436 * Schedule error handling for @qc. EH will kick in as soon as
437 * other commands are drained.
438 *
439 * LOCKING:
440 * spin_lock_irqsave(host_set lock)
441 */
442void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
443{
444 struct ata_port *ap = qc->ap;
445
446 WARN_ON(!ap->ops->error_handler);
447
448 qc->flags |= ATA_QCFLAG_FAILED;
449 qc->ap->flags |= ATA_FLAG_EH_PENDING;
450
451 /* The following will fail if timeout has already expired.
452 * ata_scsi_error() takes care of such scmds on EH entry.
453 * Note that ATA_QCFLAG_FAILED is unconditionally set after
454 * this function completes.
455 */
456 scsi_req_abort_cmd(qc->scsicmd);
457}
458
Tejun Heo7b70fc02006-05-15 20:58:07 +0900459/**
460 * ata_port_schedule_eh - schedule error handling without a qc
461 * @ap: ATA port to schedule EH for
462 *
463 * Schedule error handling for @ap. EH will kick in as soon as
464 * all commands are drained.
465 *
466 * LOCKING:
467 * spin_lock_irqsave(host_set lock)
468 */
469void ata_port_schedule_eh(struct ata_port *ap)
470{
471 WARN_ON(!ap->ops->error_handler);
472
473 ap->flags |= ATA_FLAG_EH_PENDING;
Tejun Heof8bbfc22006-05-19 21:07:05 +0900474 scsi_schedule_eh(ap->host);
Tejun Heo7b70fc02006-05-15 20:58:07 +0900475
476 DPRINTK("port EH scheduled\n");
477}
478
479/**
480 * ata_port_abort - abort all qc's on the port
481 * @ap: ATA port to abort qc's for
482 *
483 * Abort all active qc's of @ap and schedule EH.
484 *
485 * LOCKING:
486 * spin_lock_irqsave(host_set lock)
487 *
488 * RETURNS:
489 * Number of aborted qc's.
490 */
491int ata_port_abort(struct ata_port *ap)
492{
493 int tag, nr_aborted = 0;
494
495 WARN_ON(!ap->ops->error_handler);
496
497 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
498 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
499
500 if (qc) {
501 qc->flags |= ATA_QCFLAG_FAILED;
502 ata_qc_complete(qc);
503 nr_aborted++;
504 }
505 }
506
507 if (!nr_aborted)
508 ata_port_schedule_eh(ap);
509
510 return nr_aborted;
511}
512
Tejun Heoe3180492006-05-15 20:58:09 +0900513/**
514 * __ata_port_freeze - freeze port
515 * @ap: ATA port to freeze
516 *
517 * This function is called when HSM violation or some other
518 * condition disrupts normal operation of the port. Frozen port
519 * is not allowed to perform any operation until the port is
520 * thawed, which usually follows a successful reset.
521 *
522 * ap->ops->freeze() callback can be used for freezing the port
523 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
524 * port cannot be frozen hardware-wise, the interrupt handler
525 * must ack and clear interrupts unconditionally while the port
526 * is frozen.
527 *
528 * LOCKING:
529 * spin_lock_irqsave(host_set lock)
530 */
531static void __ata_port_freeze(struct ata_port *ap)
532{
533 WARN_ON(!ap->ops->error_handler);
534
535 if (ap->ops->freeze)
536 ap->ops->freeze(ap);
537
538 ap->flags |= ATA_FLAG_FROZEN;
539
540 DPRINTK("ata%u port frozen\n", ap->id);
541}
542
543/**
544 * ata_port_freeze - abort & freeze port
545 * @ap: ATA port to freeze
546 *
547 * Abort and freeze @ap.
548 *
549 * LOCKING:
550 * spin_lock_irqsave(host_set lock)
551 *
552 * RETURNS:
553 * Number of aborted commands.
554 */
555int ata_port_freeze(struct ata_port *ap)
556{
557 int nr_aborted;
558
559 WARN_ON(!ap->ops->error_handler);
560
561 nr_aborted = ata_port_abort(ap);
562 __ata_port_freeze(ap);
563
564 return nr_aborted;
565}
566
567/**
568 * ata_eh_freeze_port - EH helper to freeze port
569 * @ap: ATA port to freeze
570 *
571 * Freeze @ap.
572 *
573 * LOCKING:
574 * None.
575 */
576void ata_eh_freeze_port(struct ata_port *ap)
577{
578 unsigned long flags;
579
580 if (!ap->ops->error_handler)
581 return;
582
583 spin_lock_irqsave(&ap->host_set->lock, flags);
584 __ata_port_freeze(ap);
585 spin_unlock_irqrestore(&ap->host_set->lock, flags);
586}
587
588/**
589 * ata_port_thaw_port - EH helper to thaw port
590 * @ap: ATA port to thaw
591 *
592 * Thaw frozen port @ap.
593 *
594 * LOCKING:
595 * None.
596 */
597void ata_eh_thaw_port(struct ata_port *ap)
598{
599 unsigned long flags;
600
601 if (!ap->ops->error_handler)
602 return;
603
604 spin_lock_irqsave(&ap->host_set->lock, flags);
605
606 ap->flags &= ~ATA_FLAG_FROZEN;
607
608 if (ap->ops->thaw)
609 ap->ops->thaw(ap);
610
611 spin_unlock_irqrestore(&ap->host_set->lock, flags);
612
613 DPRINTK("ata%u port thawed\n", ap->id);
614}
615
Tejun Heoece1d632006-04-02 18:51:53 +0900616static void ata_eh_scsidone(struct scsi_cmnd *scmd)
617{
618 /* nada */
619}
620
621static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
622{
623 struct ata_port *ap = qc->ap;
624 struct scsi_cmnd *scmd = qc->scsicmd;
625 unsigned long flags;
626
627 spin_lock_irqsave(&ap->host_set->lock, flags);
628 qc->scsidone = ata_eh_scsidone;
629 __ata_qc_complete(qc);
630 WARN_ON(ata_tag_valid(qc->tag));
631 spin_unlock_irqrestore(&ap->host_set->lock, flags);
632
633 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
634}
635
636/**
637 * ata_eh_qc_complete - Complete an active ATA command from EH
638 * @qc: Command to complete
639 *
640 * Indicate to the mid and upper layers that an ATA command has
641 * completed. To be used from EH.
642 */
643void ata_eh_qc_complete(struct ata_queued_cmd *qc)
644{
645 struct scsi_cmnd *scmd = qc->scsicmd;
646 scmd->retries = scmd->allowed;
647 __ata_eh_qc_complete(qc);
648}
649
650/**
651 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
652 * @qc: Command to retry
653 *
654 * Indicate to the mid and upper layers that an ATA command
655 * should be retried. To be used from EH.
656 *
657 * SCSI midlayer limits the number of retries to scmd->allowed.
658 * scmd->retries is decremented for commands which get retried
659 * due to unrelated failures (qc->err_mask is zero).
660 */
661void ata_eh_qc_retry(struct ata_queued_cmd *qc)
662{
663 struct scsi_cmnd *scmd = qc->scsicmd;
664 if (!qc->err_mask && scmd->retries)
665 scmd->retries--;
666 __ata_eh_qc_complete(qc);
667}
Tejun Heo022bdb02006-05-15 20:58:22 +0900668
669/**
Tejun Heo0ea035a2006-05-31 18:28:01 +0900670 * ata_eh_detach_dev - detach ATA device
671 * @dev: ATA device to detach
672 *
673 * Detach @dev.
674 *
675 * LOCKING:
676 * None.
677 */
678static void ata_eh_detach_dev(struct ata_device *dev)
679{
680 struct ata_port *ap = dev->ap;
681 unsigned long flags;
682
683 ata_dev_disable(dev);
684
685 spin_lock_irqsave(&ap->host_set->lock, flags);
686
687 dev->flags &= ~ATA_DFLAG_DETACH;
688
689 if (ata_scsi_offline_dev(dev)) {
690 dev->flags |= ATA_DFLAG_DETACHED;
691 ap->flags |= ATA_FLAG_SCSI_HOTPLUG;
692 }
693
694 spin_unlock_irqrestore(&ap->host_set->lock, flags);
695}
696
697/**
Tejun Heo022bdb02006-05-15 20:58:22 +0900698 * ata_eh_about_to_do - about to perform eh_action
699 * @ap: target ATA port
700 * @action: action about to be performed
701 *
702 * Called just before performing EH actions to clear related bits
703 * in @ap->eh_info such that eh actions are not unnecessarily
704 * repeated.
705 *
706 * LOCKING:
707 * None.
708 */
709static void ata_eh_about_to_do(struct ata_port *ap, unsigned int action)
710{
711 unsigned long flags;
712
713 spin_lock_irqsave(&ap->host_set->lock, flags);
714 ap->eh_info.action &= ~action;
715 ap->flags |= ATA_FLAG_RECOVERED;
716 spin_unlock_irqrestore(&ap->host_set->lock, flags);
717}
718
719/**
720 * ata_err_string - convert err_mask to descriptive string
721 * @err_mask: error mask to convert to string
722 *
723 * Convert @err_mask to descriptive string. Errors are
724 * prioritized according to severity and only the most severe
725 * error is reported.
726 *
727 * LOCKING:
728 * None.
729 *
730 * RETURNS:
731 * Descriptive string for @err_mask
732 */
733static const char * ata_err_string(unsigned int err_mask)
734{
735 if (err_mask & AC_ERR_HOST_BUS)
736 return "host bus error";
737 if (err_mask & AC_ERR_ATA_BUS)
738 return "ATA bus error";
739 if (err_mask & AC_ERR_TIMEOUT)
740 return "timeout";
741 if (err_mask & AC_ERR_HSM)
742 return "HSM violation";
743 if (err_mask & AC_ERR_SYSTEM)
744 return "internal error";
745 if (err_mask & AC_ERR_MEDIA)
746 return "media error";
747 if (err_mask & AC_ERR_INVALID)
748 return "invalid argument";
749 if (err_mask & AC_ERR_DEV)
750 return "device error";
751 return "unknown error";
752}
753
754/**
Tejun Heoe8ee8452006-05-15 21:03:46 +0900755 * ata_read_log_page - read a specific log page
756 * @dev: target device
757 * @page: page to read
758 * @buf: buffer to store read page
759 * @sectors: number of sectors to read
760 *
761 * Read log page using READ_LOG_EXT command.
762 *
763 * LOCKING:
764 * Kernel thread context (may sleep).
765 *
766 * RETURNS:
767 * 0 on success, AC_ERR_* mask otherwise.
768 */
769static unsigned int ata_read_log_page(struct ata_device *dev,
770 u8 page, void *buf, unsigned int sectors)
771{
772 struct ata_taskfile tf;
773 unsigned int err_mask;
774
775 DPRINTK("read log page - page %d\n", page);
776
777 ata_tf_init(dev, &tf);
778 tf.command = ATA_CMD_READ_LOG_EXT;
779 tf.lbal = page;
780 tf.nsect = sectors;
781 tf.hob_nsect = sectors >> 8;
782 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
783 tf.protocol = ATA_PROT_PIO;
784
785 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
786 buf, sectors * ATA_SECT_SIZE);
787
788 DPRINTK("EXIT, err_mask=%x\n", err_mask);
789 return err_mask;
790}
791
792/**
793 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
794 * @dev: Device to read log page 10h from
795 * @tag: Resulting tag of the failed command
796 * @tf: Resulting taskfile registers of the failed command
797 *
798 * Read log page 10h to obtain NCQ error details and clear error
799 * condition.
800 *
801 * LOCKING:
802 * Kernel thread context (may sleep).
803 *
804 * RETURNS:
805 * 0 on success, -errno otherwise.
806 */
807static int ata_eh_read_log_10h(struct ata_device *dev,
808 int *tag, struct ata_taskfile *tf)
809{
810 u8 *buf = dev->ap->sector_buf;
811 unsigned int err_mask;
812 u8 csum;
813 int i;
814
815 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
816 if (err_mask)
817 return -EIO;
818
819 csum = 0;
820 for (i = 0; i < ATA_SECT_SIZE; i++)
821 csum += buf[i];
822 if (csum)
823 ata_dev_printk(dev, KERN_WARNING,
824 "invalid checksum 0x%x on log page 10h\n", csum);
825
826 if (buf[0] & 0x80)
827 return -ENOENT;
828
829 *tag = buf[0] & 0x1f;
830
831 tf->command = buf[2];
832 tf->feature = buf[3];
833 tf->lbal = buf[4];
834 tf->lbam = buf[5];
835 tf->lbah = buf[6];
836 tf->device = buf[7];
837 tf->hob_lbal = buf[8];
838 tf->hob_lbam = buf[9];
839 tf->hob_lbah = buf[10];
840 tf->nsect = buf[12];
841 tf->hob_nsect = buf[13];
842
843 return 0;
844}
845
846/**
Tejun Heo022bdb02006-05-15 20:58:22 +0900847 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
848 * @dev: device to perform REQUEST_SENSE to
849 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
850 *
851 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
852 * SENSE. This function is EH helper.
853 *
854 * LOCKING:
855 * Kernel thread context (may sleep).
856 *
857 * RETURNS:
858 * 0 on success, AC_ERR_* mask on failure
859 */
860static unsigned int atapi_eh_request_sense(struct ata_device *dev,
861 unsigned char *sense_buf)
862{
863 struct ata_port *ap = dev->ap;
864 struct ata_taskfile tf;
865 u8 cdb[ATAPI_CDB_LEN];
866
867 DPRINTK("ATAPI request sense\n");
868
869 ata_tf_init(dev, &tf);
870
871 /* FIXME: is this needed? */
872 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
873
874 /* XXX: why tf_read here? */
875 ap->ops->tf_read(ap, &tf);
876
877 /* fill these in, for the case where they are -not- overwritten */
878 sense_buf[0] = 0x70;
879 sense_buf[2] = tf.feature >> 4;
880
881 memset(cdb, 0, ATAPI_CDB_LEN);
882 cdb[0] = REQUEST_SENSE;
883 cdb[4] = SCSI_SENSE_BUFFERSIZE;
884
885 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
886 tf.command = ATA_CMD_PACKET;
887
888 /* is it pointless to prefer PIO for "safety reasons"? */
889 if (ap->flags & ATA_FLAG_PIO_DMA) {
890 tf.protocol = ATA_PROT_ATAPI_DMA;
891 tf.feature |= ATAPI_PKT_DMA;
892 } else {
893 tf.protocol = ATA_PROT_ATAPI;
894 tf.lbam = (8 * 1024) & 0xff;
895 tf.lbah = (8 * 1024) >> 8;
896 }
897
898 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
899 sense_buf, SCSI_SENSE_BUFFERSIZE);
900}
901
902/**
903 * ata_eh_analyze_serror - analyze SError for a failed port
904 * @ap: ATA port to analyze SError for
905 *
906 * Analyze SError if available and further determine cause of
907 * failure.
908 *
909 * LOCKING:
910 * None.
911 */
912static void ata_eh_analyze_serror(struct ata_port *ap)
913{
914 struct ata_eh_context *ehc = &ap->eh_context;
915 u32 serror = ehc->i.serror;
916 unsigned int err_mask = 0, action = 0;
917
918 if (serror & SERR_PERSISTENT) {
919 err_mask |= AC_ERR_ATA_BUS;
920 action |= ATA_EH_HARDRESET;
921 }
922 if (serror &
923 (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
924 err_mask |= AC_ERR_ATA_BUS;
925 action |= ATA_EH_SOFTRESET;
926 }
927 if (serror & SERR_PROTOCOL) {
928 err_mask |= AC_ERR_HSM;
929 action |= ATA_EH_SOFTRESET;
930 }
931 if (serror & SERR_INTERNAL) {
932 err_mask |= AC_ERR_SYSTEM;
933 action |= ATA_EH_SOFTRESET;
934 }
935 if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG)) {
936 err_mask |= AC_ERR_ATA_BUS;
937 action |= ATA_EH_HARDRESET;
938 }
939
940 ehc->i.err_mask |= err_mask;
941 ehc->i.action |= action;
942}
943
944/**
Tejun Heoe8ee8452006-05-15 21:03:46 +0900945 * ata_eh_analyze_ncq_error - analyze NCQ error
946 * @ap: ATA port to analyze NCQ error for
947 *
948 * Read log page 10h, determine the offending qc and acquire
949 * error status TF. For NCQ device errors, all LLDDs have to do
950 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
951 * care of the rest.
952 *
953 * LOCKING:
954 * Kernel thread context (may sleep).
955 */
956static void ata_eh_analyze_ncq_error(struct ata_port *ap)
957{
958 struct ata_eh_context *ehc = &ap->eh_context;
959 struct ata_device *dev = ap->device;
960 struct ata_queued_cmd *qc;
961 struct ata_taskfile tf;
962 int tag, rc;
963
964 /* if frozen, we can't do much */
965 if (ap->flags & ATA_FLAG_FROZEN)
966 return;
967
968 /* is it NCQ device error? */
969 if (!ap->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
970 return;
971
972 /* has LLDD analyzed already? */
973 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
974 qc = __ata_qc_from_tag(ap, tag);
975
976 if (!(qc->flags & ATA_QCFLAG_FAILED))
977 continue;
978
979 if (qc->err_mask)
980 return;
981 }
982
983 /* okay, this error is ours */
984 rc = ata_eh_read_log_10h(dev, &tag, &tf);
985 if (rc) {
986 ata_port_printk(ap, KERN_ERR, "failed to read log page 10h "
987 "(errno=%d)\n", rc);
988 return;
989 }
990
991 if (!(ap->sactive & (1 << tag))) {
992 ata_port_printk(ap, KERN_ERR, "log page 10h reported "
993 "inactive tag %d\n", tag);
994 return;
995 }
996
997 /* we've got the perpetrator, condemn it */
998 qc = __ata_qc_from_tag(ap, tag);
999 memcpy(&qc->result_tf, &tf, sizeof(tf));
1000 qc->err_mask |= AC_ERR_DEV;
1001 ehc->i.err_mask &= ~AC_ERR_DEV;
1002}
1003
1004/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001005 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1006 * @qc: qc to analyze
1007 * @tf: Taskfile registers to analyze
1008 *
1009 * Analyze taskfile of @qc and further determine cause of
1010 * failure. This function also requests ATAPI sense data if
1011 * avaliable.
1012 *
1013 * LOCKING:
1014 * Kernel thread context (may sleep).
1015 *
1016 * RETURNS:
1017 * Determined recovery action
1018 */
1019static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1020 const struct ata_taskfile *tf)
1021{
1022 unsigned int tmp, action = 0;
1023 u8 stat = tf->command, err = tf->feature;
1024
1025 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1026 qc->err_mask |= AC_ERR_HSM;
1027 return ATA_EH_SOFTRESET;
1028 }
1029
1030 if (!(qc->err_mask & AC_ERR_DEV))
1031 return 0;
1032
1033 switch (qc->dev->class) {
1034 case ATA_DEV_ATA:
1035 if (err & ATA_ICRC)
1036 qc->err_mask |= AC_ERR_ATA_BUS;
1037 if (err & ATA_UNC)
1038 qc->err_mask |= AC_ERR_MEDIA;
1039 if (err & ATA_IDNF)
1040 qc->err_mask |= AC_ERR_INVALID;
1041 break;
1042
1043 case ATA_DEV_ATAPI:
1044 tmp = atapi_eh_request_sense(qc->dev,
1045 qc->scsicmd->sense_buffer);
1046 if (!tmp) {
1047 /* ATA_QCFLAG_SENSE_VALID is used to tell
1048 * atapi_qc_complete() that sense data is
1049 * already valid.
1050 *
1051 * TODO: interpret sense data and set
1052 * appropriate err_mask.
1053 */
1054 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1055 } else
1056 qc->err_mask |= tmp;
1057 }
1058
1059 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1060 action |= ATA_EH_SOFTRESET;
1061
1062 return action;
1063}
1064
1065static int ata_eh_categorize_ering_entry(struct ata_ering_entry *ent)
1066{
1067 if (ent->err_mask & (AC_ERR_ATA_BUS | AC_ERR_TIMEOUT))
1068 return 1;
1069
1070 if (ent->is_io) {
1071 if (ent->err_mask & AC_ERR_HSM)
1072 return 1;
1073 if ((ent->err_mask &
1074 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1075 return 2;
1076 }
1077
1078 return 0;
1079}
1080
1081struct speed_down_needed_arg {
1082 u64 since;
1083 int nr_errors[3];
1084};
1085
1086static int speed_down_needed_cb(struct ata_ering_entry *ent, void *void_arg)
1087{
1088 struct speed_down_needed_arg *arg = void_arg;
1089
1090 if (ent->timestamp < arg->since)
1091 return -1;
1092
1093 arg->nr_errors[ata_eh_categorize_ering_entry(ent)]++;
1094 return 0;
1095}
1096
1097/**
1098 * ata_eh_speed_down_needed - Determine wheter speed down is necessary
1099 * @dev: Device of interest
1100 *
1101 * This function examines error ring of @dev and determines
1102 * whether speed down is necessary. Speed down is necessary if
1103 * there have been more than 3 of Cat-1 errors or 10 of Cat-2
1104 * errors during last 15 minutes.
1105 *
1106 * Cat-1 errors are ATA_BUS, TIMEOUT for any command and HSM
1107 * violation for known supported commands.
1108 *
1109 * Cat-2 errors are unclassified DEV error for known supported
1110 * command.
1111 *
1112 * LOCKING:
1113 * Inherited from caller.
1114 *
1115 * RETURNS:
1116 * 1 if speed down is necessary, 0 otherwise
1117 */
1118static int ata_eh_speed_down_needed(struct ata_device *dev)
1119{
1120 const u64 interval = 15LLU * 60 * HZ;
1121 static const int err_limits[3] = { -1, 3, 10 };
1122 struct speed_down_needed_arg arg;
1123 struct ata_ering_entry *ent;
1124 int err_cat;
1125 u64 j64;
1126
1127 ent = ata_ering_top(&dev->ering);
1128 if (!ent)
1129 return 0;
1130
1131 err_cat = ata_eh_categorize_ering_entry(ent);
1132 if (err_cat == 0)
1133 return 0;
1134
1135 memset(&arg, 0, sizeof(arg));
1136
1137 j64 = get_jiffies_64();
1138 if (j64 >= interval)
1139 arg.since = j64 - interval;
1140 else
1141 arg.since = 0;
1142
1143 ata_ering_map(&dev->ering, speed_down_needed_cb, &arg);
1144
1145 return arg.nr_errors[err_cat] > err_limits[err_cat];
1146}
1147
1148/**
1149 * ata_eh_speed_down - record error and speed down if necessary
1150 * @dev: Failed device
1151 * @is_io: Did the device fail during normal IO?
1152 * @err_mask: err_mask of the error
1153 *
1154 * Record error and examine error history to determine whether
1155 * adjusting transmission speed is necessary. It also sets
1156 * transmission limits appropriately if such adjustment is
1157 * necessary.
1158 *
1159 * LOCKING:
1160 * Kernel thread context (may sleep).
1161 *
1162 * RETURNS:
1163 * 0 on success, -errno otherwise
1164 */
1165static int ata_eh_speed_down(struct ata_device *dev, int is_io,
1166 unsigned int err_mask)
1167{
1168 if (!err_mask)
1169 return 0;
1170
1171 /* record error and determine whether speed down is necessary */
1172 ata_ering_record(&dev->ering, is_io, err_mask);
1173
1174 if (!ata_eh_speed_down_needed(dev))
1175 return 0;
1176
1177 /* speed down SATA link speed if possible */
1178 if (sata_down_spd_limit(dev->ap) == 0)
1179 return ATA_EH_HARDRESET;
1180
1181 /* lower transfer mode */
1182 if (ata_down_xfermask_limit(dev, 0) == 0)
1183 return ATA_EH_SOFTRESET;
1184
1185 ata_dev_printk(dev, KERN_ERR,
1186 "speed down requested but no transfer mode left\n");
1187 return 0;
1188}
1189
1190/**
1191 * ata_eh_autopsy - analyze error and determine recovery action
1192 * @ap: ATA port to perform autopsy on
1193 *
1194 * Analyze why @ap failed and determine which recovery action is
1195 * needed. This function also sets more detailed AC_ERR_* values
1196 * and fills sense data for ATAPI CHECK SENSE.
1197 *
1198 * LOCKING:
1199 * Kernel thread context (may sleep).
1200 */
1201static void ata_eh_autopsy(struct ata_port *ap)
1202{
1203 struct ata_eh_context *ehc = &ap->eh_context;
1204 unsigned int action = ehc->i.action;
1205 struct ata_device *failed_dev = NULL;
1206 unsigned int all_err_mask = 0;
1207 int tag, is_io = 0;
1208 u32 serror;
1209 int rc;
1210
1211 DPRINTK("ENTER\n");
1212
1213 /* obtain and analyze SError */
1214 rc = sata_scr_read(ap, SCR_ERROR, &serror);
1215 if (rc == 0) {
1216 ehc->i.serror |= serror;
1217 ata_eh_analyze_serror(ap);
1218 } else if (rc != -EOPNOTSUPP)
1219 action |= ATA_EH_HARDRESET;
1220
Tejun Heoe8ee8452006-05-15 21:03:46 +09001221 /* analyze NCQ failure */
1222 ata_eh_analyze_ncq_error(ap);
1223
Tejun Heo022bdb02006-05-15 20:58:22 +09001224 /* any real error trumps AC_ERR_OTHER */
1225 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1226 ehc->i.err_mask &= ~AC_ERR_OTHER;
1227
1228 all_err_mask |= ehc->i.err_mask;
1229
1230 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1231 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1232
1233 if (!(qc->flags & ATA_QCFLAG_FAILED))
1234 continue;
1235
1236 /* inherit upper level err_mask */
1237 qc->err_mask |= ehc->i.err_mask;
1238
Tejun Heo022bdb02006-05-15 20:58:22 +09001239 /* analyze TF */
1240 action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1241
1242 /* DEV errors are probably spurious in case of ATA_BUS error */
1243 if (qc->err_mask & AC_ERR_ATA_BUS)
1244 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1245 AC_ERR_INVALID);
1246
1247 /* any real error trumps unknown error */
1248 if (qc->err_mask & ~AC_ERR_OTHER)
1249 qc->err_mask &= ~AC_ERR_OTHER;
1250
1251 /* SENSE_VALID trumps dev/unknown error and revalidation */
1252 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1253 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1254 action &= ~ATA_EH_REVALIDATE;
1255 }
1256
1257 /* accumulate error info */
1258 failed_dev = qc->dev;
1259 all_err_mask |= qc->err_mask;
1260 if (qc->flags & ATA_QCFLAG_IO)
1261 is_io = 1;
1262 }
1263
1264 /* speed down iff command was in progress */
1265 if (failed_dev)
1266 action |= ata_eh_speed_down(failed_dev, is_io, all_err_mask);
1267
Tejun Heoa20f33f2006-05-16 12:58:24 +09001268 /* enforce default EH actions */
1269 if (ap->flags & ATA_FLAG_FROZEN ||
1270 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
1271 action |= ATA_EH_SOFTRESET;
1272 else if (all_err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001273 action |= ATA_EH_REVALIDATE;
1274
Tejun Heoa20f33f2006-05-16 12:58:24 +09001275 /* record autopsy result */
Tejun Heo022bdb02006-05-15 20:58:22 +09001276 ehc->i.dev = failed_dev;
1277 ehc->i.action = action;
1278
1279 DPRINTK("EXIT\n");
1280}
1281
1282/**
1283 * ata_eh_report - report error handling to user
1284 * @ap: ATA port EH is going on
1285 *
1286 * Report EH to user.
1287 *
1288 * LOCKING:
1289 * None.
1290 */
1291static void ata_eh_report(struct ata_port *ap)
1292{
1293 struct ata_eh_context *ehc = &ap->eh_context;
1294 const char *frozen, *desc;
1295 int tag, nr_failed = 0;
1296
1297 desc = NULL;
1298 if (ehc->i.desc[0] != '\0')
1299 desc = ehc->i.desc;
1300
1301 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1302 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1303
1304 if (!(qc->flags & ATA_QCFLAG_FAILED))
1305 continue;
1306 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1307 continue;
1308
1309 nr_failed++;
1310 }
1311
1312 if (!nr_failed && !ehc->i.err_mask)
1313 return;
1314
1315 frozen = "";
1316 if (ap->flags & ATA_FLAG_FROZEN)
1317 frozen = " frozen";
1318
1319 if (ehc->i.dev) {
Tejun Heoe8ee8452006-05-15 21:03:46 +09001320 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1321 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1322 ehc->i.err_mask, ap->sactive, ehc->i.serror,
1323 ehc->i.action, frozen);
Tejun Heo022bdb02006-05-15 20:58:22 +09001324 if (desc)
1325 ata_dev_printk(ehc->i.dev, KERN_ERR, "(%s)\n", desc);
1326 } else {
Tejun Heoe8ee8452006-05-15 21:03:46 +09001327 ata_port_printk(ap, KERN_ERR, "exception Emask 0x%x "
1328 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1329 ehc->i.err_mask, ap->sactive, ehc->i.serror,
1330 ehc->i.action, frozen);
Tejun Heo022bdb02006-05-15 20:58:22 +09001331 if (desc)
1332 ata_port_printk(ap, KERN_ERR, "(%s)\n", desc);
1333 }
1334
1335 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1336 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1337
1338 if (!(qc->flags & ATA_QCFLAG_FAILED) || !qc->err_mask)
1339 continue;
1340
1341 ata_dev_printk(qc->dev, KERN_ERR, "tag %d cmd 0x%x "
1342 "Emask 0x%x stat 0x%x err 0x%x (%s)\n",
1343 qc->tag, qc->tf.command, qc->err_mask,
1344 qc->result_tf.command, qc->result_tf.feature,
1345 ata_err_string(qc->err_mask));
1346 }
1347}
1348
Tejun Heo664faf02006-05-31 18:27:50 +09001349static int ata_eh_followup_srst_needed(int rc, int classify,
1350 const unsigned int *classes)
1351{
1352 if (rc == -EAGAIN)
1353 return 1;
1354 if (rc != 0)
1355 return 0;
1356 if (classify && classes[0] == ATA_DEV_UNKNOWN)
1357 return 1;
1358 return 0;
1359}
1360
1361static int ata_eh_reset(struct ata_port *ap, int classify,
Tejun Heof5914a42006-05-31 18:27:48 +09001362 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
Tejun Heo022bdb02006-05-15 20:58:22 +09001363 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
1364{
1365 struct ata_eh_context *ehc = &ap->eh_context;
Tejun Heo664faf02006-05-31 18:27:50 +09001366 unsigned int *classes = ehc->classes;
Tejun Heo022bdb02006-05-15 20:58:22 +09001367 int tries = ATA_EH_RESET_TRIES;
Tejun Heof5914a42006-05-31 18:27:48 +09001368 unsigned int action;
Tejun Heo022bdb02006-05-15 20:58:22 +09001369 ata_reset_fn_t reset;
Tejun Heo664faf02006-05-31 18:27:50 +09001370 int i, did_followup_srst, rc;
Tejun Heo022bdb02006-05-15 20:58:22 +09001371
Tejun Heof5914a42006-05-31 18:27:48 +09001372 /* Determine which reset to use and record in ehc->i.action.
1373 * prereset() may examine and modify it.
1374 */
1375 action = ehc->i.action;
1376 ehc->i.action &= ~ATA_EH_RESET_MASK;
Tejun Heo022bdb02006-05-15 20:58:22 +09001377 if (softreset && (!hardreset || (!sata_set_spd_needed(ap) &&
Tejun Heof5914a42006-05-31 18:27:48 +09001378 !(action & ATA_EH_HARDRESET))))
1379 ehc->i.action |= ATA_EH_SOFTRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001380 else
Tejun Heof5914a42006-05-31 18:27:48 +09001381 ehc->i.action |= ATA_EH_HARDRESET;
1382
1383 if (prereset) {
1384 rc = prereset(ap);
1385 if (rc) {
1386 ata_port_printk(ap, KERN_ERR,
1387 "prereset failed (errno=%d)\n", rc);
1388 return rc;
1389 }
1390 }
1391
1392 /* prereset() might have modified ehc->i.action */
1393 if (ehc->i.action & ATA_EH_HARDRESET)
Tejun Heo022bdb02006-05-15 20:58:22 +09001394 reset = hardreset;
Tejun Heof5914a42006-05-31 18:27:48 +09001395 else if (ehc->i.action & ATA_EH_SOFTRESET)
1396 reset = softreset;
1397 else {
1398 /* prereset told us not to reset, bang classes and return */
1399 for (i = 0; i < ATA_MAX_DEVICES; i++)
1400 classes[i] = ATA_DEV_NONE;
1401 return 0;
1402 }
1403
1404 /* did prereset() screw up? if so, fix up to avoid oopsing */
1405 if (!reset) {
1406 ata_port_printk(ap, KERN_ERR, "BUG: prereset() requested "
1407 "invalid reset type\n");
1408 if (softreset)
1409 reset = softreset;
1410 else
1411 reset = hardreset;
1412 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001413
1414 retry:
1415 ata_port_printk(ap, KERN_INFO, "%s resetting port\n",
1416 reset == softreset ? "soft" : "hard");
1417
1418 /* reset */
1419 ata_eh_about_to_do(ap, ATA_EH_RESET_MASK);
1420 ehc->i.flags |= ATA_EHI_DID_RESET;
1421
1422 rc = ata_do_reset(ap, reset, classes);
1423
Tejun Heo664faf02006-05-31 18:27:50 +09001424 did_followup_srst = 0;
1425 if (reset == hardreset &&
1426 ata_eh_followup_srst_needed(rc, classify, classes)) {
1427 /* okay, let's do follow-up softreset */
1428 did_followup_srst = 1;
1429 reset = softreset;
1430
1431 if (!reset) {
1432 ata_port_printk(ap, KERN_ERR,
1433 "follow-up softreset required "
1434 "but no softreset avaliable\n");
1435 return -EINVAL;
1436 }
1437
1438 ata_eh_about_to_do(ap, ATA_EH_RESET_MASK);
1439 rc = ata_do_reset(ap, reset, classes);
1440
1441 if (rc == 0 && classify &&
1442 classes[0] == ATA_DEV_UNKNOWN) {
1443 ata_port_printk(ap, KERN_ERR,
1444 "classification failed\n");
1445 return -EINVAL;
1446 }
1447 }
1448
Tejun Heo022bdb02006-05-15 20:58:22 +09001449 if (rc && --tries) {
Tejun Heo664faf02006-05-31 18:27:50 +09001450 const char *type;
1451
1452 if (reset == softreset) {
1453 if (did_followup_srst)
1454 type = "follow-up soft";
1455 else
1456 type = "soft";
1457 } else
1458 type = "hard";
1459
Tejun Heo022bdb02006-05-15 20:58:22 +09001460 ata_port_printk(ap, KERN_WARNING,
Tejun Heo664faf02006-05-31 18:27:50 +09001461 "%sreset failed, retrying in 5 secs\n", type);
Tejun Heo022bdb02006-05-15 20:58:22 +09001462 ssleep(5);
1463
1464 if (reset == hardreset)
1465 sata_down_spd_limit(ap);
1466 if (hardreset)
1467 reset = hardreset;
1468 goto retry;
1469 }
1470
1471 if (rc == 0) {
Tejun Heo20952b62006-05-31 18:27:23 +09001472 /* After the reset, the device state is PIO 0 and the
1473 * controller state is undefined. Record the mode.
1474 */
1475 for (i = 0; i < ATA_MAX_DEVICES; i++)
1476 ap->device[i].pio_mode = XFER_PIO_0;
1477
Tejun Heo022bdb02006-05-15 20:58:22 +09001478 if (postreset)
1479 postreset(ap, classes);
1480
1481 /* reset successful, schedule revalidation */
1482 ehc->i.dev = NULL;
1483 ehc->i.action &= ~ATA_EH_RESET_MASK;
1484 ehc->i.action |= ATA_EH_REVALIDATE;
1485 }
1486
1487 return rc;
1488}
1489
1490static int ata_eh_revalidate(struct ata_port *ap,
1491 struct ata_device **r_failed_dev)
1492{
1493 struct ata_eh_context *ehc = &ap->eh_context;
1494 struct ata_device *dev;
1495 int i, rc = 0;
1496
1497 DPRINTK("ENTER\n");
1498
1499 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1500 dev = &ap->device[i];
1501
1502 if (ehc->i.action & ATA_EH_REVALIDATE && ata_dev_enabled(dev) &&
1503 (!ehc->i.dev || ehc->i.dev == dev)) {
1504 if (ata_port_offline(ap)) {
1505 rc = -EIO;
1506 break;
1507 }
1508
1509 ata_eh_about_to_do(ap, ATA_EH_REVALIDATE);
1510 rc = ata_dev_revalidate(dev,
1511 ehc->i.flags & ATA_EHI_DID_RESET);
1512 if (rc)
1513 break;
1514
1515 ehc->i.action &= ~ATA_EH_REVALIDATE;
1516 }
1517 }
1518
1519 if (rc)
1520 *r_failed_dev = dev;
1521
1522 DPRINTK("EXIT\n");
1523 return rc;
1524}
1525
1526static int ata_port_nr_enabled(struct ata_port *ap)
1527{
1528 int i, cnt = 0;
1529
1530 for (i = 0; i < ATA_MAX_DEVICES; i++)
1531 if (ata_dev_enabled(&ap->device[i]))
1532 cnt++;
1533 return cnt;
1534}
1535
1536/**
1537 * ata_eh_recover - recover host port after error
1538 * @ap: host port to recover
Tejun Heof5914a42006-05-31 18:27:48 +09001539 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09001540 * @softreset: softreset method (can be NULL)
1541 * @hardreset: hardreset method (can be NULL)
1542 * @postreset: postreset method (can be NULL)
1543 *
1544 * This is the alpha and omega, eum and yang, heart and soul of
1545 * libata exception handling. On entry, actions required to
1546 * recover each devices are recorded in eh_context. This
1547 * function executes all the operations with appropriate retrials
1548 * and fallbacks to resurrect failed devices.
1549 *
1550 * LOCKING:
1551 * Kernel thread context (may sleep).
1552 *
1553 * RETURNS:
1554 * 0 on success, -errno on failure.
1555 */
Tejun Heof5914a42006-05-31 18:27:48 +09001556static int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
1557 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
Tejun Heo022bdb02006-05-15 20:58:22 +09001558 ata_postreset_fn_t postreset)
1559{
1560 struct ata_eh_context *ehc = &ap->eh_context;
1561 struct ata_device *dev;
1562 int down_xfermask, i, rc;
1563
1564 DPRINTK("ENTER\n");
1565
1566 /* prep for recovery */
1567 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1568 dev = &ap->device[i];
1569
1570 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
1571 }
1572
1573 retry:
1574 down_xfermask = 0;
1575 rc = 0;
1576
1577 /* skip EH if possible. */
1578 if (!ata_port_nr_enabled(ap) && !(ap->flags & ATA_FLAG_FROZEN))
1579 ehc->i.action = 0;
1580
1581 /* reset */
1582 if (ehc->i.action & ATA_EH_RESET_MASK) {
1583 ata_eh_freeze_port(ap);
1584
Tejun Heo664faf02006-05-31 18:27:50 +09001585 rc = ata_eh_reset(ap, 0, prereset, softreset, hardreset,
Tejun Heof5914a42006-05-31 18:27:48 +09001586 postreset);
Tejun Heo022bdb02006-05-15 20:58:22 +09001587 if (rc) {
1588 ata_port_printk(ap, KERN_ERR,
1589 "reset failed, giving up\n");
1590 goto out;
1591 }
1592
1593 ata_eh_thaw_port(ap);
1594 }
1595
1596 /* revalidate existing devices */
1597 rc = ata_eh_revalidate(ap, &dev);
1598 if (rc)
1599 goto dev_fail;
1600
1601 /* configure transfer mode if the port has been reset */
1602 if (ehc->i.flags & ATA_EHI_DID_RESET) {
1603 rc = ata_set_mode(ap, &dev);
1604 if (rc) {
1605 down_xfermask = 1;
1606 goto dev_fail;
1607 }
1608 }
1609
1610 goto out;
1611
1612 dev_fail:
1613 switch (rc) {
1614 case -ENODEV:
1615 case -EINVAL:
1616 ehc->tries[dev->devno] = 0;
1617 break;
1618 case -EIO:
1619 sata_down_spd_limit(ap);
1620 default:
1621 ehc->tries[dev->devno]--;
1622 if (down_xfermask &&
1623 ata_down_xfermask_limit(dev, ehc->tries[dev->devno] == 1))
1624 ehc->tries[dev->devno] = 0;
1625 }
1626
1627 /* disable device if it has used up all its chances */
1628 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno])
1629 ata_dev_disable(dev);
1630
1631 /* soft didn't work? be haaaaard */
1632 if (ehc->i.flags & ATA_EHI_DID_RESET)
1633 ehc->i.action |= ATA_EH_HARDRESET;
1634 else
1635 ehc->i.action |= ATA_EH_SOFTRESET;
1636
1637 if (ata_port_nr_enabled(ap)) {
1638 ata_port_printk(ap, KERN_WARNING, "failed to recover some "
1639 "devices, retrying in 5 secs\n");
1640 ssleep(5);
1641 } else {
1642 /* no device left, repeat fast */
1643 msleep(500);
1644 }
1645
1646 goto retry;
1647
1648 out:
1649 if (rc) {
1650 for (i = 0; i < ATA_MAX_DEVICES; i++)
1651 ata_dev_disable(&ap->device[i]);
1652 }
1653
1654 DPRINTK("EXIT, rc=%d\n", rc);
1655 return rc;
1656}
1657
1658/**
1659 * ata_eh_finish - finish up EH
1660 * @ap: host port to finish EH for
1661 *
1662 * Recovery is complete. Clean up EH states and retry or finish
1663 * failed qcs.
1664 *
1665 * LOCKING:
1666 * None.
1667 */
1668static void ata_eh_finish(struct ata_port *ap)
1669{
1670 int tag;
1671
1672 /* retry or finish qcs */
1673 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1674 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1675
1676 if (!(qc->flags & ATA_QCFLAG_FAILED))
1677 continue;
1678
1679 if (qc->err_mask) {
1680 /* FIXME: Once EH migration is complete,
1681 * generate sense data in this function,
1682 * considering both err_mask and tf.
1683 */
1684 if (qc->err_mask & AC_ERR_INVALID)
1685 ata_eh_qc_complete(qc);
1686 else
1687 ata_eh_qc_retry(qc);
1688 } else {
1689 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1690 ata_eh_qc_complete(qc);
1691 } else {
1692 /* feed zero TF to sense generation */
1693 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
1694 ata_eh_qc_retry(qc);
1695 }
1696 }
1697 }
1698}
1699
1700/**
1701 * ata_do_eh - do standard error handling
1702 * @ap: host port to handle error for
Tejun Heof5914a42006-05-31 18:27:48 +09001703 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09001704 * @softreset: softreset method (can be NULL)
1705 * @hardreset: hardreset method (can be NULL)
1706 * @postreset: postreset method (can be NULL)
1707 *
1708 * Perform standard error handling sequence.
1709 *
1710 * LOCKING:
1711 * Kernel thread context (may sleep).
1712 */
Tejun Heof5914a42006-05-31 18:27:48 +09001713void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
1714 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
1715 ata_postreset_fn_t postreset)
Tejun Heo022bdb02006-05-15 20:58:22 +09001716{
1717 ata_eh_autopsy(ap);
1718 ata_eh_report(ap);
Tejun Heof5914a42006-05-31 18:27:48 +09001719 ata_eh_recover(ap, prereset, softreset, hardreset, postreset);
Tejun Heo022bdb02006-05-15 20:58:22 +09001720 ata_eh_finish(ap);
1721}