blob: b53e2e7db498a5a2155302babf40ed7bf40f33a0 [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 }
Tejun Heo084fe632006-05-31 18:28:03 +0900935 if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG))
936 ata_ehi_hotplugged(&ehc->i);
Tejun Heo022bdb02006-05-15 20:58:22 +0900937
938 ehc->i.err_mask |= err_mask;
939 ehc->i.action |= action;
940}
941
942/**
Tejun Heoe8ee8452006-05-15 21:03:46 +0900943 * ata_eh_analyze_ncq_error - analyze NCQ error
944 * @ap: ATA port to analyze NCQ error for
945 *
946 * Read log page 10h, determine the offending qc and acquire
947 * error status TF. For NCQ device errors, all LLDDs have to do
948 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
949 * care of the rest.
950 *
951 * LOCKING:
952 * Kernel thread context (may sleep).
953 */
954static void ata_eh_analyze_ncq_error(struct ata_port *ap)
955{
956 struct ata_eh_context *ehc = &ap->eh_context;
957 struct ata_device *dev = ap->device;
958 struct ata_queued_cmd *qc;
959 struct ata_taskfile tf;
960 int tag, rc;
961
962 /* if frozen, we can't do much */
963 if (ap->flags & ATA_FLAG_FROZEN)
964 return;
965
966 /* is it NCQ device error? */
967 if (!ap->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
968 return;
969
970 /* has LLDD analyzed already? */
971 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
972 qc = __ata_qc_from_tag(ap, tag);
973
974 if (!(qc->flags & ATA_QCFLAG_FAILED))
975 continue;
976
977 if (qc->err_mask)
978 return;
979 }
980
981 /* okay, this error is ours */
982 rc = ata_eh_read_log_10h(dev, &tag, &tf);
983 if (rc) {
984 ata_port_printk(ap, KERN_ERR, "failed to read log page 10h "
985 "(errno=%d)\n", rc);
986 return;
987 }
988
989 if (!(ap->sactive & (1 << tag))) {
990 ata_port_printk(ap, KERN_ERR, "log page 10h reported "
991 "inactive tag %d\n", tag);
992 return;
993 }
994
995 /* we've got the perpetrator, condemn it */
996 qc = __ata_qc_from_tag(ap, tag);
997 memcpy(&qc->result_tf, &tf, sizeof(tf));
998 qc->err_mask |= AC_ERR_DEV;
999 ehc->i.err_mask &= ~AC_ERR_DEV;
1000}
1001
1002/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001003 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1004 * @qc: qc to analyze
1005 * @tf: Taskfile registers to analyze
1006 *
1007 * Analyze taskfile of @qc and further determine cause of
1008 * failure. This function also requests ATAPI sense data if
1009 * avaliable.
1010 *
1011 * LOCKING:
1012 * Kernel thread context (may sleep).
1013 *
1014 * RETURNS:
1015 * Determined recovery action
1016 */
1017static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1018 const struct ata_taskfile *tf)
1019{
1020 unsigned int tmp, action = 0;
1021 u8 stat = tf->command, err = tf->feature;
1022
1023 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1024 qc->err_mask |= AC_ERR_HSM;
1025 return ATA_EH_SOFTRESET;
1026 }
1027
1028 if (!(qc->err_mask & AC_ERR_DEV))
1029 return 0;
1030
1031 switch (qc->dev->class) {
1032 case ATA_DEV_ATA:
1033 if (err & ATA_ICRC)
1034 qc->err_mask |= AC_ERR_ATA_BUS;
1035 if (err & ATA_UNC)
1036 qc->err_mask |= AC_ERR_MEDIA;
1037 if (err & ATA_IDNF)
1038 qc->err_mask |= AC_ERR_INVALID;
1039 break;
1040
1041 case ATA_DEV_ATAPI:
1042 tmp = atapi_eh_request_sense(qc->dev,
1043 qc->scsicmd->sense_buffer);
1044 if (!tmp) {
1045 /* ATA_QCFLAG_SENSE_VALID is used to tell
1046 * atapi_qc_complete() that sense data is
1047 * already valid.
1048 *
1049 * TODO: interpret sense data and set
1050 * appropriate err_mask.
1051 */
1052 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1053 } else
1054 qc->err_mask |= tmp;
1055 }
1056
1057 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1058 action |= ATA_EH_SOFTRESET;
1059
1060 return action;
1061}
1062
1063static int ata_eh_categorize_ering_entry(struct ata_ering_entry *ent)
1064{
1065 if (ent->err_mask & (AC_ERR_ATA_BUS | AC_ERR_TIMEOUT))
1066 return 1;
1067
1068 if (ent->is_io) {
1069 if (ent->err_mask & AC_ERR_HSM)
1070 return 1;
1071 if ((ent->err_mask &
1072 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1073 return 2;
1074 }
1075
1076 return 0;
1077}
1078
1079struct speed_down_needed_arg {
1080 u64 since;
1081 int nr_errors[3];
1082};
1083
1084static int speed_down_needed_cb(struct ata_ering_entry *ent, void *void_arg)
1085{
1086 struct speed_down_needed_arg *arg = void_arg;
1087
1088 if (ent->timestamp < arg->since)
1089 return -1;
1090
1091 arg->nr_errors[ata_eh_categorize_ering_entry(ent)]++;
1092 return 0;
1093}
1094
1095/**
1096 * ata_eh_speed_down_needed - Determine wheter speed down is necessary
1097 * @dev: Device of interest
1098 *
1099 * This function examines error ring of @dev and determines
1100 * whether speed down is necessary. Speed down is necessary if
1101 * there have been more than 3 of Cat-1 errors or 10 of Cat-2
1102 * errors during last 15 minutes.
1103 *
1104 * Cat-1 errors are ATA_BUS, TIMEOUT for any command and HSM
1105 * violation for known supported commands.
1106 *
1107 * Cat-2 errors are unclassified DEV error for known supported
1108 * command.
1109 *
1110 * LOCKING:
1111 * Inherited from caller.
1112 *
1113 * RETURNS:
1114 * 1 if speed down is necessary, 0 otherwise
1115 */
1116static int ata_eh_speed_down_needed(struct ata_device *dev)
1117{
1118 const u64 interval = 15LLU * 60 * HZ;
1119 static const int err_limits[3] = { -1, 3, 10 };
1120 struct speed_down_needed_arg arg;
1121 struct ata_ering_entry *ent;
1122 int err_cat;
1123 u64 j64;
1124
1125 ent = ata_ering_top(&dev->ering);
1126 if (!ent)
1127 return 0;
1128
1129 err_cat = ata_eh_categorize_ering_entry(ent);
1130 if (err_cat == 0)
1131 return 0;
1132
1133 memset(&arg, 0, sizeof(arg));
1134
1135 j64 = get_jiffies_64();
1136 if (j64 >= interval)
1137 arg.since = j64 - interval;
1138 else
1139 arg.since = 0;
1140
1141 ata_ering_map(&dev->ering, speed_down_needed_cb, &arg);
1142
1143 return arg.nr_errors[err_cat] > err_limits[err_cat];
1144}
1145
1146/**
1147 * ata_eh_speed_down - record error and speed down if necessary
1148 * @dev: Failed device
1149 * @is_io: Did the device fail during normal IO?
1150 * @err_mask: err_mask of the error
1151 *
1152 * Record error and examine error history to determine whether
1153 * adjusting transmission speed is necessary. It also sets
1154 * transmission limits appropriately if such adjustment is
1155 * necessary.
1156 *
1157 * LOCKING:
1158 * Kernel thread context (may sleep).
1159 *
1160 * RETURNS:
1161 * 0 on success, -errno otherwise
1162 */
1163static int ata_eh_speed_down(struct ata_device *dev, int is_io,
1164 unsigned int err_mask)
1165{
1166 if (!err_mask)
1167 return 0;
1168
1169 /* record error and determine whether speed down is necessary */
1170 ata_ering_record(&dev->ering, is_io, err_mask);
1171
1172 if (!ata_eh_speed_down_needed(dev))
1173 return 0;
1174
1175 /* speed down SATA link speed if possible */
1176 if (sata_down_spd_limit(dev->ap) == 0)
1177 return ATA_EH_HARDRESET;
1178
1179 /* lower transfer mode */
1180 if (ata_down_xfermask_limit(dev, 0) == 0)
1181 return ATA_EH_SOFTRESET;
1182
1183 ata_dev_printk(dev, KERN_ERR,
1184 "speed down requested but no transfer mode left\n");
1185 return 0;
1186}
1187
1188/**
1189 * ata_eh_autopsy - analyze error and determine recovery action
1190 * @ap: ATA port to perform autopsy on
1191 *
1192 * Analyze why @ap failed and determine which recovery action is
1193 * needed. This function also sets more detailed AC_ERR_* values
1194 * and fills sense data for ATAPI CHECK SENSE.
1195 *
1196 * LOCKING:
1197 * Kernel thread context (may sleep).
1198 */
1199static void ata_eh_autopsy(struct ata_port *ap)
1200{
1201 struct ata_eh_context *ehc = &ap->eh_context;
1202 unsigned int action = ehc->i.action;
1203 struct ata_device *failed_dev = NULL;
1204 unsigned int all_err_mask = 0;
1205 int tag, is_io = 0;
1206 u32 serror;
1207 int rc;
1208
1209 DPRINTK("ENTER\n");
1210
1211 /* obtain and analyze SError */
1212 rc = sata_scr_read(ap, SCR_ERROR, &serror);
1213 if (rc == 0) {
1214 ehc->i.serror |= serror;
1215 ata_eh_analyze_serror(ap);
1216 } else if (rc != -EOPNOTSUPP)
1217 action |= ATA_EH_HARDRESET;
1218
Tejun Heoe8ee8452006-05-15 21:03:46 +09001219 /* analyze NCQ failure */
1220 ata_eh_analyze_ncq_error(ap);
1221
Tejun Heo022bdb02006-05-15 20:58:22 +09001222 /* any real error trumps AC_ERR_OTHER */
1223 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1224 ehc->i.err_mask &= ~AC_ERR_OTHER;
1225
1226 all_err_mask |= ehc->i.err_mask;
1227
1228 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1229 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1230
1231 if (!(qc->flags & ATA_QCFLAG_FAILED))
1232 continue;
1233
1234 /* inherit upper level err_mask */
1235 qc->err_mask |= ehc->i.err_mask;
1236
Tejun Heo022bdb02006-05-15 20:58:22 +09001237 /* analyze TF */
1238 action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1239
1240 /* DEV errors are probably spurious in case of ATA_BUS error */
1241 if (qc->err_mask & AC_ERR_ATA_BUS)
1242 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1243 AC_ERR_INVALID);
1244
1245 /* any real error trumps unknown error */
1246 if (qc->err_mask & ~AC_ERR_OTHER)
1247 qc->err_mask &= ~AC_ERR_OTHER;
1248
1249 /* SENSE_VALID trumps dev/unknown error and revalidation */
1250 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1251 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1252 action &= ~ATA_EH_REVALIDATE;
1253 }
1254
1255 /* accumulate error info */
1256 failed_dev = qc->dev;
1257 all_err_mask |= qc->err_mask;
1258 if (qc->flags & ATA_QCFLAG_IO)
1259 is_io = 1;
1260 }
1261
1262 /* speed down iff command was in progress */
1263 if (failed_dev)
1264 action |= ata_eh_speed_down(failed_dev, is_io, all_err_mask);
1265
Tejun Heoa20f33f2006-05-16 12:58:24 +09001266 /* enforce default EH actions */
1267 if (ap->flags & ATA_FLAG_FROZEN ||
1268 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
1269 action |= ATA_EH_SOFTRESET;
1270 else if (all_err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001271 action |= ATA_EH_REVALIDATE;
1272
Tejun Heoa20f33f2006-05-16 12:58:24 +09001273 /* record autopsy result */
Tejun Heo022bdb02006-05-15 20:58:22 +09001274 ehc->i.dev = failed_dev;
1275 ehc->i.action = action;
1276
1277 DPRINTK("EXIT\n");
1278}
1279
1280/**
1281 * ata_eh_report - report error handling to user
1282 * @ap: ATA port EH is going on
1283 *
1284 * Report EH to user.
1285 *
1286 * LOCKING:
1287 * None.
1288 */
1289static void ata_eh_report(struct ata_port *ap)
1290{
1291 struct ata_eh_context *ehc = &ap->eh_context;
1292 const char *frozen, *desc;
1293 int tag, nr_failed = 0;
1294
1295 desc = NULL;
1296 if (ehc->i.desc[0] != '\0')
1297 desc = ehc->i.desc;
1298
1299 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1300 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1301
1302 if (!(qc->flags & ATA_QCFLAG_FAILED))
1303 continue;
1304 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1305 continue;
1306
1307 nr_failed++;
1308 }
1309
1310 if (!nr_failed && !ehc->i.err_mask)
1311 return;
1312
1313 frozen = "";
1314 if (ap->flags & ATA_FLAG_FROZEN)
1315 frozen = " frozen";
1316
1317 if (ehc->i.dev) {
Tejun Heoe8ee8452006-05-15 21:03:46 +09001318 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1319 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1320 ehc->i.err_mask, ap->sactive, ehc->i.serror,
1321 ehc->i.action, frozen);
Tejun Heo022bdb02006-05-15 20:58:22 +09001322 if (desc)
1323 ata_dev_printk(ehc->i.dev, KERN_ERR, "(%s)\n", desc);
1324 } else {
Tejun Heoe8ee8452006-05-15 21:03:46 +09001325 ata_port_printk(ap, KERN_ERR, "exception Emask 0x%x "
1326 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1327 ehc->i.err_mask, ap->sactive, ehc->i.serror,
1328 ehc->i.action, frozen);
Tejun Heo022bdb02006-05-15 20:58:22 +09001329 if (desc)
1330 ata_port_printk(ap, KERN_ERR, "(%s)\n", desc);
1331 }
1332
1333 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1334 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1335
1336 if (!(qc->flags & ATA_QCFLAG_FAILED) || !qc->err_mask)
1337 continue;
1338
1339 ata_dev_printk(qc->dev, KERN_ERR, "tag %d cmd 0x%x "
1340 "Emask 0x%x stat 0x%x err 0x%x (%s)\n",
1341 qc->tag, qc->tf.command, qc->err_mask,
1342 qc->result_tf.command, qc->result_tf.feature,
1343 ata_err_string(qc->err_mask));
1344 }
1345}
1346
Tejun Heo664faf02006-05-31 18:27:50 +09001347static int ata_eh_followup_srst_needed(int rc, int classify,
1348 const unsigned int *classes)
1349{
1350 if (rc == -EAGAIN)
1351 return 1;
1352 if (rc != 0)
1353 return 0;
1354 if (classify && classes[0] == ATA_DEV_UNKNOWN)
1355 return 1;
1356 return 0;
1357}
1358
1359static int ata_eh_reset(struct ata_port *ap, int classify,
Tejun Heof5914a42006-05-31 18:27:48 +09001360 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
Tejun Heo022bdb02006-05-15 20:58:22 +09001361 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
1362{
1363 struct ata_eh_context *ehc = &ap->eh_context;
Tejun Heo664faf02006-05-31 18:27:50 +09001364 unsigned int *classes = ehc->classes;
Tejun Heo022bdb02006-05-15 20:58:22 +09001365 int tries = ATA_EH_RESET_TRIES;
Tejun Heof5914a42006-05-31 18:27:48 +09001366 unsigned int action;
Tejun Heo022bdb02006-05-15 20:58:22 +09001367 ata_reset_fn_t reset;
Tejun Heo664faf02006-05-31 18:27:50 +09001368 int i, did_followup_srst, rc;
Tejun Heo022bdb02006-05-15 20:58:22 +09001369
Tejun Heof5914a42006-05-31 18:27:48 +09001370 /* Determine which reset to use and record in ehc->i.action.
1371 * prereset() may examine and modify it.
1372 */
1373 action = ehc->i.action;
1374 ehc->i.action &= ~ATA_EH_RESET_MASK;
Tejun Heo022bdb02006-05-15 20:58:22 +09001375 if (softreset && (!hardreset || (!sata_set_spd_needed(ap) &&
Tejun Heof5914a42006-05-31 18:27:48 +09001376 !(action & ATA_EH_HARDRESET))))
1377 ehc->i.action |= ATA_EH_SOFTRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001378 else
Tejun Heof5914a42006-05-31 18:27:48 +09001379 ehc->i.action |= ATA_EH_HARDRESET;
1380
1381 if (prereset) {
1382 rc = prereset(ap);
1383 if (rc) {
1384 ata_port_printk(ap, KERN_ERR,
1385 "prereset failed (errno=%d)\n", rc);
1386 return rc;
1387 }
1388 }
1389
1390 /* prereset() might have modified ehc->i.action */
1391 if (ehc->i.action & ATA_EH_HARDRESET)
Tejun Heo022bdb02006-05-15 20:58:22 +09001392 reset = hardreset;
Tejun Heof5914a42006-05-31 18:27:48 +09001393 else if (ehc->i.action & ATA_EH_SOFTRESET)
1394 reset = softreset;
1395 else {
1396 /* prereset told us not to reset, bang classes and return */
1397 for (i = 0; i < ATA_MAX_DEVICES; i++)
1398 classes[i] = ATA_DEV_NONE;
1399 return 0;
1400 }
1401
1402 /* did prereset() screw up? if so, fix up to avoid oopsing */
1403 if (!reset) {
1404 ata_port_printk(ap, KERN_ERR, "BUG: prereset() requested "
1405 "invalid reset type\n");
1406 if (softreset)
1407 reset = softreset;
1408 else
1409 reset = hardreset;
1410 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001411
1412 retry:
1413 ata_port_printk(ap, KERN_INFO, "%s resetting port\n",
1414 reset == softreset ? "soft" : "hard");
1415
1416 /* reset */
1417 ata_eh_about_to_do(ap, ATA_EH_RESET_MASK);
1418 ehc->i.flags |= ATA_EHI_DID_RESET;
1419
1420 rc = ata_do_reset(ap, reset, classes);
1421
Tejun Heo664faf02006-05-31 18:27:50 +09001422 did_followup_srst = 0;
1423 if (reset == hardreset &&
1424 ata_eh_followup_srst_needed(rc, classify, classes)) {
1425 /* okay, let's do follow-up softreset */
1426 did_followup_srst = 1;
1427 reset = softreset;
1428
1429 if (!reset) {
1430 ata_port_printk(ap, KERN_ERR,
1431 "follow-up softreset required "
1432 "but no softreset avaliable\n");
1433 return -EINVAL;
1434 }
1435
1436 ata_eh_about_to_do(ap, ATA_EH_RESET_MASK);
1437 rc = ata_do_reset(ap, reset, classes);
1438
1439 if (rc == 0 && classify &&
1440 classes[0] == ATA_DEV_UNKNOWN) {
1441 ata_port_printk(ap, KERN_ERR,
1442 "classification failed\n");
1443 return -EINVAL;
1444 }
1445 }
1446
Tejun Heo022bdb02006-05-15 20:58:22 +09001447 if (rc && --tries) {
Tejun Heo664faf02006-05-31 18:27:50 +09001448 const char *type;
1449
1450 if (reset == softreset) {
1451 if (did_followup_srst)
1452 type = "follow-up soft";
1453 else
1454 type = "soft";
1455 } else
1456 type = "hard";
1457
Tejun Heo022bdb02006-05-15 20:58:22 +09001458 ata_port_printk(ap, KERN_WARNING,
Tejun Heo664faf02006-05-31 18:27:50 +09001459 "%sreset failed, retrying in 5 secs\n", type);
Tejun Heo022bdb02006-05-15 20:58:22 +09001460 ssleep(5);
1461
1462 if (reset == hardreset)
1463 sata_down_spd_limit(ap);
1464 if (hardreset)
1465 reset = hardreset;
1466 goto retry;
1467 }
1468
1469 if (rc == 0) {
Tejun Heo20952b62006-05-31 18:27:23 +09001470 /* After the reset, the device state is PIO 0 and the
1471 * controller state is undefined. Record the mode.
1472 */
1473 for (i = 0; i < ATA_MAX_DEVICES; i++)
1474 ap->device[i].pio_mode = XFER_PIO_0;
1475
Tejun Heo022bdb02006-05-15 20:58:22 +09001476 if (postreset)
1477 postreset(ap, classes);
1478
1479 /* reset successful, schedule revalidation */
1480 ehc->i.dev = NULL;
1481 ehc->i.action &= ~ATA_EH_RESET_MASK;
1482 ehc->i.action |= ATA_EH_REVALIDATE;
1483 }
1484
1485 return rc;
1486}
1487
Tejun Heo084fe632006-05-31 18:28:03 +09001488static int ata_eh_revalidate_and_attach(struct ata_port *ap,
1489 struct ata_device **r_failed_dev)
Tejun Heo022bdb02006-05-15 20:58:22 +09001490{
1491 struct ata_eh_context *ehc = &ap->eh_context;
1492 struct ata_device *dev;
Tejun Heo084fe632006-05-31 18:28:03 +09001493 unsigned long flags;
Tejun Heo022bdb02006-05-15 20:58:22 +09001494 int i, rc = 0;
1495
1496 DPRINTK("ENTER\n");
1497
1498 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1499 dev = &ap->device[i];
1500
1501 if (ehc->i.action & ATA_EH_REVALIDATE && ata_dev_enabled(dev) &&
1502 (!ehc->i.dev || ehc->i.dev == dev)) {
1503 if (ata_port_offline(ap)) {
1504 rc = -EIO;
1505 break;
1506 }
1507
1508 ata_eh_about_to_do(ap, ATA_EH_REVALIDATE);
1509 rc = ata_dev_revalidate(dev,
1510 ehc->i.flags & ATA_EHI_DID_RESET);
1511 if (rc)
1512 break;
1513
1514 ehc->i.action &= ~ATA_EH_REVALIDATE;
Tejun Heo084fe632006-05-31 18:28:03 +09001515 } else if (dev->class == ATA_DEV_UNKNOWN &&
1516 ehc->tries[dev->devno] &&
1517 ata_class_enabled(ehc->classes[dev->devno])) {
1518 dev->class = ehc->classes[dev->devno];
1519
1520 rc = ata_dev_read_id(dev, &dev->class, 1, dev->id);
1521 if (rc == 0)
1522 rc = ata_dev_configure(dev, 1);
1523
1524 if (rc) {
1525 dev->class = ATA_DEV_UNKNOWN;
1526 break;
1527 }
1528
1529 spin_lock_irqsave(&ap->host_set->lock, flags);
1530 ap->flags |= ATA_FLAG_SCSI_HOTPLUG;
1531 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo022bdb02006-05-15 20:58:22 +09001532 }
1533 }
1534
1535 if (rc)
1536 *r_failed_dev = dev;
1537
1538 DPRINTK("EXIT\n");
1539 return rc;
1540}
1541
1542static int ata_port_nr_enabled(struct ata_port *ap)
1543{
1544 int i, cnt = 0;
1545
1546 for (i = 0; i < ATA_MAX_DEVICES; i++)
1547 if (ata_dev_enabled(&ap->device[i]))
1548 cnt++;
1549 return cnt;
1550}
1551
Tejun Heo084fe632006-05-31 18:28:03 +09001552static int ata_port_nr_vacant(struct ata_port *ap)
1553{
1554 int i, cnt = 0;
1555
1556 for (i = 0; i < ATA_MAX_DEVICES; i++)
1557 if (ap->device[i].class == ATA_DEV_UNKNOWN)
1558 cnt++;
1559 return cnt;
1560}
1561
1562static int ata_eh_skip_recovery(struct ata_port *ap)
1563{
1564 struct ata_eh_context *ehc = &ap->eh_context;
1565 int i;
1566
1567 if (ap->flags & ATA_FLAG_FROZEN || ata_port_nr_enabled(ap))
1568 return 0;
1569
1570 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
1571 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1572 struct ata_device *dev = &ap->device[i];
1573
1574 if (dev->class == ATA_DEV_UNKNOWN &&
1575 ehc->classes[dev->devno] != ATA_DEV_NONE)
1576 return 0;
1577 }
1578
1579 return 1;
1580}
1581
Tejun Heo022bdb02006-05-15 20:58:22 +09001582/**
1583 * ata_eh_recover - recover host port after error
1584 * @ap: host port to recover
Tejun Heof5914a42006-05-31 18:27:48 +09001585 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09001586 * @softreset: softreset method (can be NULL)
1587 * @hardreset: hardreset method (can be NULL)
1588 * @postreset: postreset method (can be NULL)
1589 *
1590 * This is the alpha and omega, eum and yang, heart and soul of
1591 * libata exception handling. On entry, actions required to
Tejun Heo084fe632006-05-31 18:28:03 +09001592 * recover the port and hotplug requests are recorded in
1593 * eh_context. This function executes all the operations with
1594 * appropriate retrials and fallbacks to resurrect failed
1595 * devices, detach goners and greet newcomers.
Tejun Heo022bdb02006-05-15 20:58:22 +09001596 *
1597 * LOCKING:
1598 * Kernel thread context (may sleep).
1599 *
1600 * RETURNS:
1601 * 0 on success, -errno on failure.
1602 */
Tejun Heof5914a42006-05-31 18:27:48 +09001603static int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
1604 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
Tejun Heo022bdb02006-05-15 20:58:22 +09001605 ata_postreset_fn_t postreset)
1606{
1607 struct ata_eh_context *ehc = &ap->eh_context;
1608 struct ata_device *dev;
1609 int down_xfermask, i, rc;
1610
1611 DPRINTK("ENTER\n");
1612
1613 /* prep for recovery */
1614 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1615 dev = &ap->device[i];
1616
1617 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
Tejun Heo084fe632006-05-31 18:28:03 +09001618
1619 /* process hotplug request */
1620 if (dev->flags & ATA_DFLAG_DETACH)
1621 ata_eh_detach_dev(dev);
1622
1623 if (!ata_dev_enabled(dev) &&
1624 ((ehc->i.probe_mask & (1 << dev->devno)) &&
1625 !(ehc->did_probe_mask & (1 << dev->devno)))) {
1626 ata_eh_detach_dev(dev);
1627 ata_dev_init(dev);
1628 ehc->did_probe_mask |= (1 << dev->devno);
1629 ehc->i.action |= ATA_EH_SOFTRESET;
1630 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001631 }
1632
1633 retry:
1634 down_xfermask = 0;
1635 rc = 0;
1636
1637 /* skip EH if possible. */
Tejun Heo084fe632006-05-31 18:28:03 +09001638 if (ata_eh_skip_recovery(ap))
Tejun Heo022bdb02006-05-15 20:58:22 +09001639 ehc->i.action = 0;
1640
Tejun Heo084fe632006-05-31 18:28:03 +09001641 for (i = 0; i < ATA_MAX_DEVICES; i++)
1642 ehc->classes[i] = ATA_DEV_UNKNOWN;
1643
Tejun Heo022bdb02006-05-15 20:58:22 +09001644 /* reset */
1645 if (ehc->i.action & ATA_EH_RESET_MASK) {
1646 ata_eh_freeze_port(ap);
1647
Tejun Heo084fe632006-05-31 18:28:03 +09001648 rc = ata_eh_reset(ap, ata_port_nr_vacant(ap), prereset,
1649 softreset, hardreset, postreset);
Tejun Heo022bdb02006-05-15 20:58:22 +09001650 if (rc) {
1651 ata_port_printk(ap, KERN_ERR,
1652 "reset failed, giving up\n");
1653 goto out;
1654 }
1655
1656 ata_eh_thaw_port(ap);
1657 }
1658
Tejun Heo084fe632006-05-31 18:28:03 +09001659 /* revalidate existing devices and attach new ones */
1660 rc = ata_eh_revalidate_and_attach(ap, &dev);
Tejun Heo022bdb02006-05-15 20:58:22 +09001661 if (rc)
1662 goto dev_fail;
1663
1664 /* configure transfer mode if the port has been reset */
1665 if (ehc->i.flags & ATA_EHI_DID_RESET) {
1666 rc = ata_set_mode(ap, &dev);
1667 if (rc) {
1668 down_xfermask = 1;
1669 goto dev_fail;
1670 }
1671 }
1672
1673 goto out;
1674
1675 dev_fail:
1676 switch (rc) {
1677 case -ENODEV:
Tejun Heo084fe632006-05-31 18:28:03 +09001678 /* device missing, schedule probing */
1679 ehc->i.probe_mask |= (1 << dev->devno);
Tejun Heo022bdb02006-05-15 20:58:22 +09001680 case -EINVAL:
1681 ehc->tries[dev->devno] = 0;
1682 break;
1683 case -EIO:
1684 sata_down_spd_limit(ap);
1685 default:
1686 ehc->tries[dev->devno]--;
1687 if (down_xfermask &&
1688 ata_down_xfermask_limit(dev, ehc->tries[dev->devno] == 1))
1689 ehc->tries[dev->devno] = 0;
1690 }
1691
Tejun Heo084fe632006-05-31 18:28:03 +09001692 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
1693 /* disable device if it has used up all its chances */
Tejun Heo022bdb02006-05-15 20:58:22 +09001694 ata_dev_disable(dev);
1695
Tejun Heo084fe632006-05-31 18:28:03 +09001696 /* detach if offline */
1697 if (ata_port_offline(ap))
1698 ata_eh_detach_dev(dev);
1699
1700 /* probe if requested */
1701 if ((ehc->i.probe_mask & (1 << dev->devno)) &&
1702 !(ehc->did_probe_mask & (1 << dev->devno))) {
1703 ata_eh_detach_dev(dev);
1704 ata_dev_init(dev);
1705
1706 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
1707 ehc->did_probe_mask |= (1 << dev->devno);
1708 ehc->i.action |= ATA_EH_SOFTRESET;
1709 }
1710 } else {
1711 /* soft didn't work? be haaaaard */
1712 if (ehc->i.flags & ATA_EHI_DID_RESET)
1713 ehc->i.action |= ATA_EH_HARDRESET;
1714 else
1715 ehc->i.action |= ATA_EH_SOFTRESET;
1716 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001717
1718 if (ata_port_nr_enabled(ap)) {
1719 ata_port_printk(ap, KERN_WARNING, "failed to recover some "
1720 "devices, retrying in 5 secs\n");
1721 ssleep(5);
1722 } else {
1723 /* no device left, repeat fast */
1724 msleep(500);
1725 }
1726
1727 goto retry;
1728
1729 out:
1730 if (rc) {
1731 for (i = 0; i < ATA_MAX_DEVICES; i++)
1732 ata_dev_disable(&ap->device[i]);
1733 }
1734
1735 DPRINTK("EXIT, rc=%d\n", rc);
1736 return rc;
1737}
1738
1739/**
1740 * ata_eh_finish - finish up EH
1741 * @ap: host port to finish EH for
1742 *
1743 * Recovery is complete. Clean up EH states and retry or finish
1744 * failed qcs.
1745 *
1746 * LOCKING:
1747 * None.
1748 */
1749static void ata_eh_finish(struct ata_port *ap)
1750{
1751 int tag;
1752
1753 /* retry or finish qcs */
1754 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1755 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1756
1757 if (!(qc->flags & ATA_QCFLAG_FAILED))
1758 continue;
1759
1760 if (qc->err_mask) {
1761 /* FIXME: Once EH migration is complete,
1762 * generate sense data in this function,
1763 * considering both err_mask and tf.
1764 */
1765 if (qc->err_mask & AC_ERR_INVALID)
1766 ata_eh_qc_complete(qc);
1767 else
1768 ata_eh_qc_retry(qc);
1769 } else {
1770 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1771 ata_eh_qc_complete(qc);
1772 } else {
1773 /* feed zero TF to sense generation */
1774 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
1775 ata_eh_qc_retry(qc);
1776 }
1777 }
1778 }
1779}
1780
1781/**
1782 * ata_do_eh - do standard error handling
1783 * @ap: host port to handle error for
Tejun Heof5914a42006-05-31 18:27:48 +09001784 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09001785 * @softreset: softreset method (can be NULL)
1786 * @hardreset: hardreset method (can be NULL)
1787 * @postreset: postreset method (can be NULL)
1788 *
1789 * Perform standard error handling sequence.
1790 *
1791 * LOCKING:
1792 * Kernel thread context (may sleep).
1793 */
Tejun Heof5914a42006-05-31 18:27:48 +09001794void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
1795 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
1796 ata_postreset_fn_t postreset)
Tejun Heo022bdb02006-05-15 20:58:22 +09001797{
1798 ata_eh_autopsy(ap);
1799 ata_eh_report(ap);
Tejun Heof5914a42006-05-31 18:27:48 +09001800 ata_eh_recover(ap, prereset, softreset, hardreset, postreset);
Tejun Heo022bdb02006-05-15 20:58:22 +09001801 ata_eh_finish(ap);
1802}