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