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