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