libata-pmp-prep: implement ATA_LFLAG_DISABLED
[safe/jmp/linux-2.6] / drivers / ata / 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/kernel.h>
36 #include <scsi/scsi.h>
37 #include <scsi/scsi_host.h>
38 #include <scsi/scsi_eh.h>
39 #include <scsi/scsi_device.h>
40 #include <scsi/scsi_cmnd.h>
41 #include "../scsi/scsi_transport_api.h"
42
43 #include <linux/libata.h>
44
45 #include "libata.h"
46
47 enum {
48         ATA_EH_SPDN_NCQ_OFF             = (1 << 0),
49         ATA_EH_SPDN_SPEED_DOWN          = (1 << 1),
50         ATA_EH_SPDN_FALLBACK_TO_PIO     = (1 << 2),
51 };
52
53 /* Waiting in ->prereset can never be reliable.  It's sometimes nice
54  * to wait there but it can't be depended upon; otherwise, we wouldn't
55  * be resetting.  Just give it enough time for most drives to spin up.
56  */
57 enum {
58         ATA_EH_PRERESET_TIMEOUT         = 10 * HZ,
59         ATA_EH_FASTDRAIN_INTERVAL       = 3 * HZ,
60 };
61
62 /* The following table determines how we sequence resets.  Each entry
63  * represents timeout for that try.  The first try can be soft or
64  * hardreset.  All others are hardreset if available.  In most cases
65  * the first reset w/ 10sec timeout should succeed.  Following entries
66  * are mostly for error handling, hotplug and retarded devices.
67  */
68 static const unsigned long ata_eh_reset_timeouts[] = {
69         10 * HZ,        /* most drives spin up by 10sec */
70         10 * HZ,        /* > 99% working drives spin up before 20sec */
71         35 * HZ,        /* give > 30 secs of idleness for retarded devices */
72         5 * HZ,         /* and sweet one last chance */
73         /* > 1 min has elapsed, give up */
74 };
75
76 static void __ata_port_freeze(struct ata_port *ap);
77 #ifdef CONFIG_PM
78 static void ata_eh_handle_port_suspend(struct ata_port *ap);
79 static void ata_eh_handle_port_resume(struct ata_port *ap);
80 #else /* CONFIG_PM */
81 static void ata_eh_handle_port_suspend(struct ata_port *ap)
82 { }
83
84 static void ata_eh_handle_port_resume(struct ata_port *ap)
85 { }
86 #endif /* CONFIG_PM */
87
88 static void __ata_ehi_pushv_desc(struct ata_eh_info *ehi, const char *fmt,
89                                  va_list args)
90 {
91         ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
92                                      ATA_EH_DESC_LEN - ehi->desc_len,
93                                      fmt, args);
94 }
95
96 /**
97  *      __ata_ehi_push_desc - push error description without adding separator
98  *      @ehi: target EHI
99  *      @fmt: printf format string
100  *
101  *      Format string according to @fmt and append it to @ehi->desc.
102  *
103  *      LOCKING:
104  *      spin_lock_irqsave(host lock)
105  */
106 void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
107 {
108         va_list args;
109
110         va_start(args, fmt);
111         __ata_ehi_pushv_desc(ehi, fmt, args);
112         va_end(args);
113 }
114
115 /**
116  *      ata_ehi_push_desc - push error description with separator
117  *      @ehi: target EHI
118  *      @fmt: printf format string
119  *
120  *      Format string according to @fmt and append it to @ehi->desc.
121  *      If @ehi->desc is not empty, ", " is added in-between.
122  *
123  *      LOCKING:
124  *      spin_lock_irqsave(host lock)
125  */
126 void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
127 {
128         va_list args;
129
130         if (ehi->desc_len)
131                 __ata_ehi_push_desc(ehi, ", ");
132
133         va_start(args, fmt);
134         __ata_ehi_pushv_desc(ehi, fmt, args);
135         va_end(args);
136 }
137
138 /**
139  *      ata_ehi_clear_desc - clean error description
140  *      @ehi: target EHI
141  *
142  *      Clear @ehi->desc.
143  *
144  *      LOCKING:
145  *      spin_lock_irqsave(host lock)
146  */
147 void ata_ehi_clear_desc(struct ata_eh_info *ehi)
148 {
149         ehi->desc[0] = '\0';
150         ehi->desc_len = 0;
151 }
152
153 /**
154  *      ata_port_desc - append port description
155  *      @ap: target ATA port
156  *      @fmt: printf format string
157  *
158  *      Format string according to @fmt and append it to port
159  *      description.  If port description is not empty, " " is added
160  *      in-between.  This function is to be used while initializing
161  *      ata_host.  The description is printed on host registration.
162  *
163  *      LOCKING:
164  *      None.
165  */
166 void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
167 {
168         va_list args;
169
170         WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
171
172         if (ap->link.eh_info.desc_len)
173                 __ata_ehi_push_desc(&ap->link.eh_info, " ");
174
175         va_start(args, fmt);
176         __ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
177         va_end(args);
178 }
179
180 #ifdef CONFIG_PCI
181
182 /**
183  *      ata_port_pbar_desc - append PCI BAR description
184  *      @ap: target ATA port
185  *      @bar: target PCI BAR
186  *      @offset: offset into PCI BAR
187  *      @name: name of the area
188  *
189  *      If @offset is negative, this function formats a string which
190  *      contains the name, address, size and type of the BAR and
191  *      appends it to the port description.  If @offset is zero or
192  *      positive, only name and offsetted address is appended.
193  *
194  *      LOCKING:
195  *      None.
196  */
197 void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
198                         const char *name)
199 {
200         struct pci_dev *pdev = to_pci_dev(ap->host->dev);
201         char *type = "";
202         unsigned long long start, len;
203
204         if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
205                 type = "m";
206         else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
207                 type = "i";
208
209         start = (unsigned long long)pci_resource_start(pdev, bar);
210         len = (unsigned long long)pci_resource_len(pdev, bar);
211
212         if (offset < 0)
213                 ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
214         else
215                 ata_port_desc(ap, "%s 0x%llx", name, start + offset);
216 }
217
218 #endif /* CONFIG_PCI */
219
220 static void ata_ering_record(struct ata_ering *ering, int is_io,
221                              unsigned int err_mask)
222 {
223         struct ata_ering_entry *ent;
224
225         WARN_ON(!err_mask);
226
227         ering->cursor++;
228         ering->cursor %= ATA_ERING_SIZE;
229
230         ent = &ering->ring[ering->cursor];
231         ent->is_io = is_io;
232         ent->err_mask = err_mask;
233         ent->timestamp = get_jiffies_64();
234 }
235
236 static void ata_ering_clear(struct ata_ering *ering)
237 {
238         memset(ering, 0, sizeof(*ering));
239 }
240
241 static int ata_ering_map(struct ata_ering *ering,
242                          int (*map_fn)(struct ata_ering_entry *, void *),
243                          void *arg)
244 {
245         int idx, rc = 0;
246         struct ata_ering_entry *ent;
247
248         idx = ering->cursor;
249         do {
250                 ent = &ering->ring[idx];
251                 if (!ent->err_mask)
252                         break;
253                 rc = map_fn(ent, arg);
254                 if (rc)
255                         break;
256                 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
257         } while (idx != ering->cursor);
258
259         return rc;
260 }
261
262 static unsigned int ata_eh_dev_action(struct ata_device *dev)
263 {
264         struct ata_eh_context *ehc = &dev->link->eh_context;
265
266         return ehc->i.action | ehc->i.dev_action[dev->devno];
267 }
268
269 static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
270                                 struct ata_eh_info *ehi, unsigned int action)
271 {
272         struct ata_device *tdev;
273
274         if (!dev) {
275                 ehi->action &= ~action;
276                 ata_link_for_each_dev(tdev, link)
277                         ehi->dev_action[tdev->devno] &= ~action;
278         } else {
279                 /* doesn't make sense for port-wide EH actions */
280                 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
281
282                 /* break ehi->action into ehi->dev_action */
283                 if (ehi->action & action) {
284                         ata_link_for_each_dev(tdev, link)
285                                 ehi->dev_action[tdev->devno] |=
286                                         ehi->action & action;
287                         ehi->action &= ~action;
288                 }
289
290                 /* turn off the specified per-dev action */
291                 ehi->dev_action[dev->devno] &= ~action;
292         }
293 }
294
295 /**
296  *      ata_scsi_timed_out - SCSI layer time out callback
297  *      @cmd: timed out SCSI command
298  *
299  *      Handles SCSI layer timeout.  We race with normal completion of
300  *      the qc for @cmd.  If the qc is already gone, we lose and let
301  *      the scsi command finish (EH_HANDLED).  Otherwise, the qc has
302  *      timed out and EH should be invoked.  Prevent ata_qc_complete()
303  *      from finishing it by setting EH_SCHEDULED and return
304  *      EH_NOT_HANDLED.
305  *
306  *      TODO: kill this function once old EH is gone.
307  *
308  *      LOCKING:
309  *      Called from timer context
310  *
311  *      RETURNS:
312  *      EH_HANDLED or EH_NOT_HANDLED
313  */
314 enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
315 {
316         struct Scsi_Host *host = cmd->device->host;
317         struct ata_port *ap = ata_shost_to_port(host);
318         unsigned long flags;
319         struct ata_queued_cmd *qc;
320         enum scsi_eh_timer_return ret;
321
322         DPRINTK("ENTER\n");
323
324         if (ap->ops->error_handler) {
325                 ret = EH_NOT_HANDLED;
326                 goto out;
327         }
328
329         ret = EH_HANDLED;
330         spin_lock_irqsave(ap->lock, flags);
331         qc = ata_qc_from_tag(ap, ap->link.active_tag);
332         if (qc) {
333                 WARN_ON(qc->scsicmd != cmd);
334                 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
335                 qc->err_mask |= AC_ERR_TIMEOUT;
336                 ret = EH_NOT_HANDLED;
337         }
338         spin_unlock_irqrestore(ap->lock, flags);
339
340  out:
341         DPRINTK("EXIT, ret=%d\n", ret);
342         return ret;
343 }
344
345 /**
346  *      ata_scsi_error - SCSI layer error handler callback
347  *      @host: SCSI host on which error occurred
348  *
349  *      Handles SCSI-layer-thrown error events.
350  *
351  *      LOCKING:
352  *      Inherited from SCSI layer (none, can sleep)
353  *
354  *      RETURNS:
355  *      Zero.
356  */
357 void ata_scsi_error(struct Scsi_Host *host)
358 {
359         struct ata_port *ap = ata_shost_to_port(host);
360         int i;
361         unsigned long flags;
362
363         DPRINTK("ENTER\n");
364
365         /* synchronize with port task */
366         ata_port_flush_task(ap);
367
368         /* synchronize with host lock and sort out timeouts */
369
370         /* For new EH, all qcs are finished in one of three ways -
371          * normal completion, error completion, and SCSI timeout.
372          * Both cmpletions can race against SCSI timeout.  When normal
373          * completion wins, the qc never reaches EH.  When error
374          * completion wins, the qc has ATA_QCFLAG_FAILED set.
375          *
376          * When SCSI timeout wins, things are a bit more complex.
377          * Normal or error completion can occur after the timeout but
378          * before this point.  In such cases, both types of
379          * completions are honored.  A scmd is determined to have
380          * timed out iff its associated qc is active and not failed.
381          */
382         if (ap->ops->error_handler) {
383                 struct scsi_cmnd *scmd, *tmp;
384                 int nr_timedout = 0;
385
386                 spin_lock_irqsave(ap->lock, flags);
387
388                 list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
389                         struct ata_queued_cmd *qc;
390
391                         for (i = 0; i < ATA_MAX_QUEUE; i++) {
392                                 qc = __ata_qc_from_tag(ap, i);
393                                 if (qc->flags & ATA_QCFLAG_ACTIVE &&
394                                     qc->scsicmd == scmd)
395                                         break;
396                         }
397
398                         if (i < ATA_MAX_QUEUE) {
399                                 /* the scmd has an associated qc */
400                                 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
401                                         /* which hasn't failed yet, timeout */
402                                         qc->err_mask |= AC_ERR_TIMEOUT;
403                                         qc->flags |= ATA_QCFLAG_FAILED;
404                                         nr_timedout++;
405                                 }
406                         } else {
407                                 /* Normal completion occurred after
408                                  * SCSI timeout but before this point.
409                                  * Successfully complete it.
410                                  */
411                                 scmd->retries = scmd->allowed;
412                                 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
413                         }
414                 }
415
416                 /* If we have timed out qcs.  They belong to EH from
417                  * this point but the state of the controller is
418                  * unknown.  Freeze the port to make sure the IRQ
419                  * handler doesn't diddle with those qcs.  This must
420                  * be done atomically w.r.t. setting QCFLAG_FAILED.
421                  */
422                 if (nr_timedout)
423                         __ata_port_freeze(ap);
424
425                 spin_unlock_irqrestore(ap->lock, flags);
426
427                 /* initialize eh_tries */
428                 ap->eh_tries = ATA_EH_MAX_TRIES;
429         } else
430                 spin_unlock_wait(ap->lock);
431
432  repeat:
433         /* invoke error handler */
434         if (ap->ops->error_handler) {
435                 struct ata_link *link;
436
437                 /* kill fast drain timer */
438                 del_timer_sync(&ap->fastdrain_timer);
439
440                 /* process port resume request */
441                 ata_eh_handle_port_resume(ap);
442
443                 /* fetch & clear EH info */
444                 spin_lock_irqsave(ap->lock, flags);
445
446                 __ata_port_for_each_link(link, ap) {
447                         memset(&link->eh_context, 0, sizeof(link->eh_context));
448                         link->eh_context.i = link->eh_info;
449                         memset(&link->eh_info, 0, sizeof(link->eh_info));
450                 }
451
452                 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
453                 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
454                 ap->excl_link = NULL;   /* don't maintain exclusion over EH */
455
456                 spin_unlock_irqrestore(ap->lock, flags);
457
458                 /* invoke EH, skip if unloading or suspended */
459                 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
460                         ap->ops->error_handler(ap);
461                 else
462                         ata_eh_finish(ap);
463
464                 /* process port suspend request */
465                 ata_eh_handle_port_suspend(ap);
466
467                 /* Exception might have happend after ->error_handler
468                  * recovered the port but before this point.  Repeat
469                  * EH in such case.
470                  */
471                 spin_lock_irqsave(ap->lock, flags);
472
473                 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
474                         if (--ap->eh_tries) {
475                                 spin_unlock_irqrestore(ap->lock, flags);
476                                 goto repeat;
477                         }
478                         ata_port_printk(ap, KERN_ERR, "EH pending after %d "
479                                         "tries, giving up\n", ATA_EH_MAX_TRIES);
480                         ap->pflags &= ~ATA_PFLAG_EH_PENDING;
481                 }
482
483                 /* this run is complete, make sure EH info is clear */
484                 __ata_port_for_each_link(link, ap)
485                         memset(&link->eh_info, 0, sizeof(link->eh_info));
486
487                 /* Clear host_eh_scheduled while holding ap->lock such
488                  * that if exception occurs after this point but
489                  * before EH completion, SCSI midlayer will
490                  * re-initiate EH.
491                  */
492                 host->host_eh_scheduled = 0;
493
494                 spin_unlock_irqrestore(ap->lock, flags);
495         } else {
496                 WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
497                 ap->ops->eng_timeout(ap);
498         }
499
500         /* finish or retry handled scmd's and clean up */
501         WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
502
503         scsi_eh_flush_done_q(&ap->eh_done_q);
504
505         /* clean up */
506         spin_lock_irqsave(ap->lock, flags);
507
508         if (ap->pflags & ATA_PFLAG_LOADING)
509                 ap->pflags &= ~ATA_PFLAG_LOADING;
510         else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
511                 queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0);
512
513         if (ap->pflags & ATA_PFLAG_RECOVERED)
514                 ata_port_printk(ap, KERN_INFO, "EH complete\n");
515
516         ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
517
518         /* tell wait_eh that we're done */
519         ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
520         wake_up_all(&ap->eh_wait_q);
521
522         spin_unlock_irqrestore(ap->lock, flags);
523
524         DPRINTK("EXIT\n");
525 }
526
527 /**
528  *      ata_port_wait_eh - Wait for the currently pending EH to complete
529  *      @ap: Port to wait EH for
530  *
531  *      Wait until the currently pending EH is complete.
532  *
533  *      LOCKING:
534  *      Kernel thread context (may sleep).
535  */
536 void ata_port_wait_eh(struct ata_port *ap)
537 {
538         unsigned long flags;
539         DEFINE_WAIT(wait);
540
541  retry:
542         spin_lock_irqsave(ap->lock, flags);
543
544         while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
545                 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
546                 spin_unlock_irqrestore(ap->lock, flags);
547                 schedule();
548                 spin_lock_irqsave(ap->lock, flags);
549         }
550         finish_wait(&ap->eh_wait_q, &wait);
551
552         spin_unlock_irqrestore(ap->lock, flags);
553
554         /* make sure SCSI EH is complete */
555         if (scsi_host_in_recovery(ap->scsi_host)) {
556                 msleep(10);
557                 goto retry;
558         }
559 }
560
561 /**
562  *      ata_qc_timeout - Handle timeout of queued command
563  *      @qc: Command that timed out
564  *
565  *      Some part of the kernel (currently, only the SCSI layer)
566  *      has noticed that the active command on port @ap has not
567  *      completed after a specified length of time.  Handle this
568  *      condition by disabling DMA (if necessary) and completing
569  *      transactions, with error if necessary.
570  *
571  *      This also handles the case of the "lost interrupt", where
572  *      for some reason (possibly hardware bug, possibly driver bug)
573  *      an interrupt was not delivered to the driver, even though the
574  *      transaction completed successfully.
575  *
576  *      TODO: kill this function once old EH is gone.
577  *
578  *      LOCKING:
579  *      Inherited from SCSI layer (none, can sleep)
580  */
581 static void ata_qc_timeout(struct ata_queued_cmd *qc)
582 {
583         struct ata_port *ap = qc->ap;
584         u8 host_stat = 0, drv_stat;
585         unsigned long flags;
586
587         DPRINTK("ENTER\n");
588
589         ap->hsm_task_state = HSM_ST_IDLE;
590
591         spin_lock_irqsave(ap->lock, flags);
592
593         switch (qc->tf.protocol) {
594
595         case ATA_PROT_DMA:
596         case ATA_PROT_ATAPI_DMA:
597                 host_stat = ap->ops->bmdma_status(ap);
598
599                 /* before we do anything else, clear DMA-Start bit */
600                 ap->ops->bmdma_stop(qc);
601
602                 /* fall through */
603
604         default:
605                 ata_altstatus(ap);
606                 drv_stat = ata_chk_status(ap);
607
608                 /* ack bmdma irq events */
609                 ap->ops->irq_clear(ap);
610
611                 ata_dev_printk(qc->dev, KERN_ERR, "command 0x%x timeout, "
612                                "stat 0x%x host_stat 0x%x\n",
613                                qc->tf.command, drv_stat, host_stat);
614
615                 /* complete taskfile transaction */
616                 qc->err_mask |= AC_ERR_TIMEOUT;
617                 break;
618         }
619
620         spin_unlock_irqrestore(ap->lock, flags);
621
622         ata_eh_qc_complete(qc);
623
624         DPRINTK("EXIT\n");
625 }
626
627 /**
628  *      ata_eng_timeout - Handle timeout of queued command
629  *      @ap: Port on which timed-out command is active
630  *
631  *      Some part of the kernel (currently, only the SCSI layer)
632  *      has noticed that the active command on port @ap has not
633  *      completed after a specified length of time.  Handle this
634  *      condition by disabling DMA (if necessary) and completing
635  *      transactions, with error if necessary.
636  *
637  *      This also handles the case of the "lost interrupt", where
638  *      for some reason (possibly hardware bug, possibly driver bug)
639  *      an interrupt was not delivered to the driver, even though the
640  *      transaction completed successfully.
641  *
642  *      TODO: kill this function once old EH is gone.
643  *
644  *      LOCKING:
645  *      Inherited from SCSI layer (none, can sleep)
646  */
647 void ata_eng_timeout(struct ata_port *ap)
648 {
649         DPRINTK("ENTER\n");
650
651         ata_qc_timeout(ata_qc_from_tag(ap, ap->link.active_tag));
652
653         DPRINTK("EXIT\n");
654 }
655
656 static int ata_eh_nr_in_flight(struct ata_port *ap)
657 {
658         unsigned int tag;
659         int nr = 0;
660
661         /* count only non-internal commands */
662         for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++)
663                 if (ata_qc_from_tag(ap, tag))
664                         nr++;
665
666         return nr;
667 }
668
669 void ata_eh_fastdrain_timerfn(unsigned long arg)
670 {
671         struct ata_port *ap = (void *)arg;
672         unsigned long flags;
673         int cnt;
674
675         spin_lock_irqsave(ap->lock, flags);
676
677         cnt = ata_eh_nr_in_flight(ap);
678
679         /* are we done? */
680         if (!cnt)
681                 goto out_unlock;
682
683         if (cnt == ap->fastdrain_cnt) {
684                 unsigned int tag;
685
686                 /* No progress during the last interval, tag all
687                  * in-flight qcs as timed out and freeze the port.
688                  */
689                 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) {
690                         struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
691                         if (qc)
692                                 qc->err_mask |= AC_ERR_TIMEOUT;
693                 }
694
695                 ata_port_freeze(ap);
696         } else {
697                 /* some qcs have finished, give it another chance */
698                 ap->fastdrain_cnt = cnt;
699                 ap->fastdrain_timer.expires =
700                         jiffies + ATA_EH_FASTDRAIN_INTERVAL;
701                 add_timer(&ap->fastdrain_timer);
702         }
703
704  out_unlock:
705         spin_unlock_irqrestore(ap->lock, flags);
706 }
707
708 /**
709  *      ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
710  *      @ap: target ATA port
711  *      @fastdrain: activate fast drain
712  *
713  *      Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
714  *      is non-zero and EH wasn't pending before.  Fast drain ensures
715  *      that EH kicks in in timely manner.
716  *
717  *      LOCKING:
718  *      spin_lock_irqsave(host lock)
719  */
720 static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
721 {
722         int cnt;
723
724         /* already scheduled? */
725         if (ap->pflags & ATA_PFLAG_EH_PENDING)
726                 return;
727
728         ap->pflags |= ATA_PFLAG_EH_PENDING;
729
730         if (!fastdrain)
731                 return;
732
733         /* do we have in-flight qcs? */
734         cnt = ata_eh_nr_in_flight(ap);
735         if (!cnt)
736                 return;
737
738         /* activate fast drain */
739         ap->fastdrain_cnt = cnt;
740         ap->fastdrain_timer.expires = jiffies + ATA_EH_FASTDRAIN_INTERVAL;
741         add_timer(&ap->fastdrain_timer);
742 }
743
744 /**
745  *      ata_qc_schedule_eh - schedule qc for error handling
746  *      @qc: command to schedule error handling for
747  *
748  *      Schedule error handling for @qc.  EH will kick in as soon as
749  *      other commands are drained.
750  *
751  *      LOCKING:
752  *      spin_lock_irqsave(host lock)
753  */
754 void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
755 {
756         struct ata_port *ap = qc->ap;
757
758         WARN_ON(!ap->ops->error_handler);
759
760         qc->flags |= ATA_QCFLAG_FAILED;
761         ata_eh_set_pending(ap, 1);
762
763         /* The following will fail if timeout has already expired.
764          * ata_scsi_error() takes care of such scmds on EH entry.
765          * Note that ATA_QCFLAG_FAILED is unconditionally set after
766          * this function completes.
767          */
768         scsi_req_abort_cmd(qc->scsicmd);
769 }
770
771 /**
772  *      ata_port_schedule_eh - schedule error handling without a qc
773  *      @ap: ATA port to schedule EH for
774  *
775  *      Schedule error handling for @ap.  EH will kick in as soon as
776  *      all commands are drained.
777  *
778  *      LOCKING:
779  *      spin_lock_irqsave(host lock)
780  */
781 void ata_port_schedule_eh(struct ata_port *ap)
782 {
783         WARN_ON(!ap->ops->error_handler);
784
785         if (ap->pflags & ATA_PFLAG_INITIALIZING)
786                 return;
787
788         ata_eh_set_pending(ap, 1);
789         scsi_schedule_eh(ap->scsi_host);
790
791         DPRINTK("port EH scheduled\n");
792 }
793
794 static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
795 {
796         int tag, nr_aborted = 0;
797
798         WARN_ON(!ap->ops->error_handler);
799
800         /* we're gonna abort all commands, no need for fast drain */
801         ata_eh_set_pending(ap, 0);
802
803         for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
804                 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
805
806                 if (qc && (!link || qc->dev->link == link)) {
807                         qc->flags |= ATA_QCFLAG_FAILED;
808                         ata_qc_complete(qc);
809                         nr_aborted++;
810                 }
811         }
812
813         if (!nr_aborted)
814                 ata_port_schedule_eh(ap);
815
816         return nr_aborted;
817 }
818
819 /**
820  *      ata_link_abort - abort all qc's on the link
821  *      @link: ATA link to abort qc's for
822  *
823  *      Abort all active qc's active on @link and schedule EH.
824  *
825  *      LOCKING:
826  *      spin_lock_irqsave(host lock)
827  *
828  *      RETURNS:
829  *      Number of aborted qc's.
830  */
831 int ata_link_abort(struct ata_link *link)
832 {
833         return ata_do_link_abort(link->ap, link);
834 }
835
836 /**
837  *      ata_port_abort - abort all qc's on the port
838  *      @ap: ATA port to abort qc's for
839  *
840  *      Abort all active qc's of @ap and schedule EH.
841  *
842  *      LOCKING:
843  *      spin_lock_irqsave(host_set lock)
844  *
845  *      RETURNS:
846  *      Number of aborted qc's.
847  */
848 int ata_port_abort(struct ata_port *ap)
849 {
850         return ata_do_link_abort(ap, NULL);
851 }
852
853 /**
854  *      __ata_port_freeze - freeze port
855  *      @ap: ATA port to freeze
856  *
857  *      This function is called when HSM violation or some other
858  *      condition disrupts normal operation of the port.  Frozen port
859  *      is not allowed to perform any operation until the port is
860  *      thawed, which usually follows a successful reset.
861  *
862  *      ap->ops->freeze() callback can be used for freezing the port
863  *      hardware-wise (e.g. mask interrupt and stop DMA engine).  If a
864  *      port cannot be frozen hardware-wise, the interrupt handler
865  *      must ack and clear interrupts unconditionally while the port
866  *      is frozen.
867  *
868  *      LOCKING:
869  *      spin_lock_irqsave(host lock)
870  */
871 static void __ata_port_freeze(struct ata_port *ap)
872 {
873         WARN_ON(!ap->ops->error_handler);
874
875         if (ap->ops->freeze)
876                 ap->ops->freeze(ap);
877
878         ap->pflags |= ATA_PFLAG_FROZEN;
879
880         DPRINTK("ata%u port frozen\n", ap->print_id);
881 }
882
883 /**
884  *      ata_port_freeze - abort & freeze port
885  *      @ap: ATA port to freeze
886  *
887  *      Abort and freeze @ap.
888  *
889  *      LOCKING:
890  *      spin_lock_irqsave(host lock)
891  *
892  *      RETURNS:
893  *      Number of aborted commands.
894  */
895 int ata_port_freeze(struct ata_port *ap)
896 {
897         int nr_aborted;
898
899         WARN_ON(!ap->ops->error_handler);
900
901         nr_aborted = ata_port_abort(ap);
902         __ata_port_freeze(ap);
903
904         return nr_aborted;
905 }
906
907 /**
908  *      ata_eh_freeze_port - EH helper to freeze port
909  *      @ap: ATA port to freeze
910  *
911  *      Freeze @ap.
912  *
913  *      LOCKING:
914  *      None.
915  */
916 void ata_eh_freeze_port(struct ata_port *ap)
917 {
918         unsigned long flags;
919
920         if (!ap->ops->error_handler)
921                 return;
922
923         spin_lock_irqsave(ap->lock, flags);
924         __ata_port_freeze(ap);
925         spin_unlock_irqrestore(ap->lock, flags);
926 }
927
928 /**
929  *      ata_port_thaw_port - EH helper to thaw port
930  *      @ap: ATA port to thaw
931  *
932  *      Thaw frozen port @ap.
933  *
934  *      LOCKING:
935  *      None.
936  */
937 void ata_eh_thaw_port(struct ata_port *ap)
938 {
939         unsigned long flags;
940
941         if (!ap->ops->error_handler)
942                 return;
943
944         spin_lock_irqsave(ap->lock, flags);
945
946         ap->pflags &= ~ATA_PFLAG_FROZEN;
947
948         if (ap->ops->thaw)
949                 ap->ops->thaw(ap);
950
951         spin_unlock_irqrestore(ap->lock, flags);
952
953         DPRINTK("ata%u port thawed\n", ap->print_id);
954 }
955
956 static void ata_eh_scsidone(struct scsi_cmnd *scmd)
957 {
958         /* nada */
959 }
960
961 static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
962 {
963         struct ata_port *ap = qc->ap;
964         struct scsi_cmnd *scmd = qc->scsicmd;
965         unsigned long flags;
966
967         spin_lock_irqsave(ap->lock, flags);
968         qc->scsidone = ata_eh_scsidone;
969         __ata_qc_complete(qc);
970         WARN_ON(ata_tag_valid(qc->tag));
971         spin_unlock_irqrestore(ap->lock, flags);
972
973         scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
974 }
975
976 /**
977  *      ata_eh_qc_complete - Complete an active ATA command from EH
978  *      @qc: Command to complete
979  *
980  *      Indicate to the mid and upper layers that an ATA command has
981  *      completed.  To be used from EH.
982  */
983 void ata_eh_qc_complete(struct ata_queued_cmd *qc)
984 {
985         struct scsi_cmnd *scmd = qc->scsicmd;
986         scmd->retries = scmd->allowed;
987         __ata_eh_qc_complete(qc);
988 }
989
990 /**
991  *      ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
992  *      @qc: Command to retry
993  *
994  *      Indicate to the mid and upper layers that an ATA command
995  *      should be retried.  To be used from EH.
996  *
997  *      SCSI midlayer limits the number of retries to scmd->allowed.
998  *      scmd->retries is decremented for commands which get retried
999  *      due to unrelated failures (qc->err_mask is zero).
1000  */
1001 void ata_eh_qc_retry(struct ata_queued_cmd *qc)
1002 {
1003         struct scsi_cmnd *scmd = qc->scsicmd;
1004         if (!qc->err_mask && scmd->retries)
1005                 scmd->retries--;
1006         __ata_eh_qc_complete(qc);
1007 }
1008
1009 /**
1010  *      ata_eh_detach_dev - detach ATA device
1011  *      @dev: ATA device to detach
1012  *
1013  *      Detach @dev.
1014  *
1015  *      LOCKING:
1016  *      None.
1017  */
1018 void ata_eh_detach_dev(struct ata_device *dev)
1019 {
1020         struct ata_link *link = dev->link;
1021         struct ata_port *ap = link->ap;
1022         unsigned long flags;
1023
1024         ata_dev_disable(dev);
1025
1026         spin_lock_irqsave(ap->lock, flags);
1027
1028         dev->flags &= ~ATA_DFLAG_DETACH;
1029
1030         if (ata_scsi_offline_dev(dev)) {
1031                 dev->flags |= ATA_DFLAG_DETACHED;
1032                 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
1033         }
1034
1035         /* clear per-dev EH actions */
1036         ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
1037         ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
1038
1039         spin_unlock_irqrestore(ap->lock, flags);
1040 }
1041
1042 /**
1043  *      ata_eh_about_to_do - about to perform eh_action
1044  *      @link: target ATA link
1045  *      @dev: target ATA dev for per-dev action (can be NULL)
1046  *      @action: action about to be performed
1047  *
1048  *      Called just before performing EH actions to clear related bits
1049  *      in @link->eh_info such that eh actions are not unnecessarily
1050  *      repeated.
1051  *
1052  *      LOCKING:
1053  *      None.
1054  */
1055 void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
1056                         unsigned int action)
1057 {
1058         struct ata_port *ap = link->ap;
1059         struct ata_eh_info *ehi = &link->eh_info;
1060         struct ata_eh_context *ehc = &link->eh_context;
1061         unsigned long flags;
1062
1063         spin_lock_irqsave(ap->lock, flags);
1064
1065         /* Reset is represented by combination of actions and EHI
1066          * flags.  Suck in all related bits before clearing eh_info to
1067          * avoid losing requested action.
1068          */
1069         if (action & ATA_EH_RESET_MASK) {
1070                 ehc->i.action |= ehi->action & ATA_EH_RESET_MASK;
1071                 ehc->i.flags |= ehi->flags & ATA_EHI_RESET_MODIFIER_MASK;
1072
1073                 /* make sure all reset actions are cleared & clear EHI flags */
1074                 action |= ATA_EH_RESET_MASK;
1075                 ehi->flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
1076         }
1077
1078         ata_eh_clear_action(link, dev, ehi, action);
1079
1080         if (!(ehc->i.flags & ATA_EHI_QUIET))
1081                 ap->pflags |= ATA_PFLAG_RECOVERED;
1082
1083         spin_unlock_irqrestore(ap->lock, flags);
1084 }
1085
1086 /**
1087  *      ata_eh_done - EH action complete
1088 *       @ap: target ATA port
1089  *      @dev: target ATA dev for per-dev action (can be NULL)
1090  *      @action: action just completed
1091  *
1092  *      Called right after performing EH actions to clear related bits
1093  *      in @link->eh_context.
1094  *
1095  *      LOCKING:
1096  *      None.
1097  */
1098 void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1099                  unsigned int action)
1100 {
1101         struct ata_eh_context *ehc = &link->eh_context;
1102
1103         /* if reset is complete, clear all reset actions & reset modifier */
1104         if (action & ATA_EH_RESET_MASK) {
1105                 action |= ATA_EH_RESET_MASK;
1106                 ehc->i.flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
1107         }
1108
1109         ata_eh_clear_action(link, dev, &ehc->i, action);
1110 }
1111
1112 /**
1113  *      ata_err_string - convert err_mask to descriptive string
1114  *      @err_mask: error mask to convert to string
1115  *
1116  *      Convert @err_mask to descriptive string.  Errors are
1117  *      prioritized according to severity and only the most severe
1118  *      error is reported.
1119  *
1120  *      LOCKING:
1121  *      None.
1122  *
1123  *      RETURNS:
1124  *      Descriptive string for @err_mask
1125  */
1126 static const char * ata_err_string(unsigned int err_mask)
1127 {
1128         if (err_mask & AC_ERR_HOST_BUS)
1129                 return "host bus error";
1130         if (err_mask & AC_ERR_ATA_BUS)
1131                 return "ATA bus error";
1132         if (err_mask & AC_ERR_TIMEOUT)
1133                 return "timeout";
1134         if (err_mask & AC_ERR_HSM)
1135                 return "HSM violation";
1136         if (err_mask & AC_ERR_SYSTEM)
1137                 return "internal error";
1138         if (err_mask & AC_ERR_MEDIA)
1139                 return "media error";
1140         if (err_mask & AC_ERR_INVALID)
1141                 return "invalid argument";
1142         if (err_mask & AC_ERR_DEV)
1143                 return "device error";
1144         return "unknown error";
1145 }
1146
1147 /**
1148  *      ata_read_log_page - read a specific log page
1149  *      @dev: target device
1150  *      @page: page to read
1151  *      @buf: buffer to store read page
1152  *      @sectors: number of sectors to read
1153  *
1154  *      Read log page using READ_LOG_EXT command.
1155  *
1156  *      LOCKING:
1157  *      Kernel thread context (may sleep).
1158  *
1159  *      RETURNS:
1160  *      0 on success, AC_ERR_* mask otherwise.
1161  */
1162 static unsigned int ata_read_log_page(struct ata_device *dev,
1163                                       u8 page, void *buf, unsigned int sectors)
1164 {
1165         struct ata_taskfile tf;
1166         unsigned int err_mask;
1167
1168         DPRINTK("read log page - page %d\n", page);
1169
1170         ata_tf_init(dev, &tf);
1171         tf.command = ATA_CMD_READ_LOG_EXT;
1172         tf.lbal = page;
1173         tf.nsect = sectors;
1174         tf.hob_nsect = sectors >> 8;
1175         tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1176         tf.protocol = ATA_PROT_PIO;
1177
1178         err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
1179                                      buf, sectors * ATA_SECT_SIZE);
1180
1181         DPRINTK("EXIT, err_mask=%x\n", err_mask);
1182         return err_mask;
1183 }
1184
1185 /**
1186  *      ata_eh_read_log_10h - Read log page 10h for NCQ error details
1187  *      @dev: Device to read log page 10h from
1188  *      @tag: Resulting tag of the failed command
1189  *      @tf: Resulting taskfile registers of the failed command
1190  *
1191  *      Read log page 10h to obtain NCQ error details and clear error
1192  *      condition.
1193  *
1194  *      LOCKING:
1195  *      Kernel thread context (may sleep).
1196  *
1197  *      RETURNS:
1198  *      0 on success, -errno otherwise.
1199  */
1200 static int ata_eh_read_log_10h(struct ata_device *dev,
1201                                int *tag, struct ata_taskfile *tf)
1202 {
1203         u8 *buf = dev->link->ap->sector_buf;
1204         unsigned int err_mask;
1205         u8 csum;
1206         int i;
1207
1208         err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
1209         if (err_mask)
1210                 return -EIO;
1211
1212         csum = 0;
1213         for (i = 0; i < ATA_SECT_SIZE; i++)
1214                 csum += buf[i];
1215         if (csum)
1216                 ata_dev_printk(dev, KERN_WARNING,
1217                                "invalid checksum 0x%x on log page 10h\n", csum);
1218
1219         if (buf[0] & 0x80)
1220                 return -ENOENT;
1221
1222         *tag = buf[0] & 0x1f;
1223
1224         tf->command = buf[2];
1225         tf->feature = buf[3];
1226         tf->lbal = buf[4];
1227         tf->lbam = buf[5];
1228         tf->lbah = buf[6];
1229         tf->device = buf[7];
1230         tf->hob_lbal = buf[8];
1231         tf->hob_lbam = buf[9];
1232         tf->hob_lbah = buf[10];
1233         tf->nsect = buf[12];
1234         tf->hob_nsect = buf[13];
1235
1236         return 0;
1237 }
1238
1239 /**
1240  *      atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1241  *      @dev: device to perform REQUEST_SENSE to
1242  *      @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
1243  *
1244  *      Perform ATAPI REQUEST_SENSE after the device reported CHECK
1245  *      SENSE.  This function is EH helper.
1246  *
1247  *      LOCKING:
1248  *      Kernel thread context (may sleep).
1249  *
1250  *      RETURNS:
1251  *      0 on success, AC_ERR_* mask on failure
1252  */
1253 static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc)
1254 {
1255         struct ata_device *dev = qc->dev;
1256         unsigned char *sense_buf = qc->scsicmd->sense_buffer;
1257         struct ata_port *ap = dev->link->ap;
1258         struct ata_taskfile tf;
1259         u8 cdb[ATAPI_CDB_LEN];
1260
1261         DPRINTK("ATAPI request sense\n");
1262
1263         /* FIXME: is this needed? */
1264         memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1265
1266         /* initialize sense_buf with the error register,
1267          * for the case where they are -not- overwritten
1268          */
1269         sense_buf[0] = 0x70;
1270         sense_buf[2] = qc->result_tf.feature >> 4;
1271
1272         /* some devices time out if garbage left in tf */
1273         ata_tf_init(dev, &tf);
1274
1275         memset(cdb, 0, ATAPI_CDB_LEN);
1276         cdb[0] = REQUEST_SENSE;
1277         cdb[4] = SCSI_SENSE_BUFFERSIZE;
1278
1279         tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1280         tf.command = ATA_CMD_PACKET;
1281
1282         /* is it pointless to prefer PIO for "safety reasons"? */
1283         if (ap->flags & ATA_FLAG_PIO_DMA) {
1284                 tf.protocol = ATA_PROT_ATAPI_DMA;
1285                 tf.feature |= ATAPI_PKT_DMA;
1286         } else {
1287                 tf.protocol = ATA_PROT_ATAPI;
1288                 tf.lbam = (8 * 1024) & 0xff;
1289                 tf.lbah = (8 * 1024) >> 8;
1290         }
1291
1292         return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
1293                                  sense_buf, SCSI_SENSE_BUFFERSIZE);
1294 }
1295
1296 /**
1297  *      ata_eh_analyze_serror - analyze SError for a failed port
1298  *      @link: ATA link to analyze SError for
1299  *
1300  *      Analyze SError if available and further determine cause of
1301  *      failure.
1302  *
1303  *      LOCKING:
1304  *      None.
1305  */
1306 static void ata_eh_analyze_serror(struct ata_link *link)
1307 {
1308         struct ata_eh_context *ehc = &link->eh_context;
1309         u32 serror = ehc->i.serror;
1310         unsigned int err_mask = 0, action = 0;
1311         u32 hotplug_mask;
1312
1313         if (serror & SERR_PERSISTENT) {
1314                 err_mask |= AC_ERR_ATA_BUS;
1315                 action |= ATA_EH_HARDRESET;
1316         }
1317         if (serror &
1318             (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
1319                 err_mask |= AC_ERR_ATA_BUS;
1320                 action |= ATA_EH_SOFTRESET;
1321         }
1322         if (serror & SERR_PROTOCOL) {
1323                 err_mask |= AC_ERR_HSM;
1324                 action |= ATA_EH_SOFTRESET;
1325         }
1326         if (serror & SERR_INTERNAL) {
1327                 err_mask |= AC_ERR_SYSTEM;
1328                 action |= ATA_EH_HARDRESET;
1329         }
1330
1331         /* Determine whether a hotplug event has occurred.  Both
1332          * SError.N/X are considered hotplug events for enabled or
1333          * host links.  For disabled PMP links, only N bit is
1334          * considered as X bit is left at 1 for link plugging.
1335          */
1336         hotplug_mask = 0;
1337
1338         if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
1339                 hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
1340         else
1341                 hotplug_mask = SERR_PHYRDY_CHG;
1342
1343         if (serror & hotplug_mask)
1344                 ata_ehi_hotplugged(&ehc->i);
1345
1346         ehc->i.err_mask |= err_mask;
1347         ehc->i.action |= action;
1348 }
1349
1350 /**
1351  *      ata_eh_analyze_ncq_error - analyze NCQ error
1352  *      @link: ATA link to analyze NCQ error for
1353  *
1354  *      Read log page 10h, determine the offending qc and acquire
1355  *      error status TF.  For NCQ device errors, all LLDDs have to do
1356  *      is setting AC_ERR_DEV in ehi->err_mask.  This function takes
1357  *      care of the rest.
1358  *
1359  *      LOCKING:
1360  *      Kernel thread context (may sleep).
1361  */
1362 static void ata_eh_analyze_ncq_error(struct ata_link *link)
1363 {
1364         struct ata_port *ap = link->ap;
1365         struct ata_eh_context *ehc = &link->eh_context;
1366         struct ata_device *dev = link->device;
1367         struct ata_queued_cmd *qc;
1368         struct ata_taskfile tf;
1369         int tag, rc;
1370
1371         /* if frozen, we can't do much */
1372         if (ap->pflags & ATA_PFLAG_FROZEN)
1373                 return;
1374
1375         /* is it NCQ device error? */
1376         if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1377                 return;
1378
1379         /* has LLDD analyzed already? */
1380         for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1381                 qc = __ata_qc_from_tag(ap, tag);
1382
1383                 if (!(qc->flags & ATA_QCFLAG_FAILED))
1384                         continue;
1385
1386                 if (qc->err_mask)
1387                         return;
1388         }
1389
1390         /* okay, this error is ours */
1391         rc = ata_eh_read_log_10h(dev, &tag, &tf);
1392         if (rc) {
1393                 ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
1394                                 "(errno=%d)\n", rc);
1395                 return;
1396         }
1397
1398         if (!(link->sactive & (1 << tag))) {
1399                 ata_link_printk(link, KERN_ERR, "log page 10h reported "
1400                                 "inactive tag %d\n", tag);
1401                 return;
1402         }
1403
1404         /* we've got the perpetrator, condemn it */
1405         qc = __ata_qc_from_tag(ap, tag);
1406         memcpy(&qc->result_tf, &tf, sizeof(tf));
1407         qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
1408         ehc->i.err_mask &= ~AC_ERR_DEV;
1409 }
1410
1411 /**
1412  *      ata_eh_analyze_tf - analyze taskfile of a failed qc
1413  *      @qc: qc to analyze
1414  *      @tf: Taskfile registers to analyze
1415  *
1416  *      Analyze taskfile of @qc and further determine cause of
1417  *      failure.  This function also requests ATAPI sense data if
1418  *      avaliable.
1419  *
1420  *      LOCKING:
1421  *      Kernel thread context (may sleep).
1422  *
1423  *      RETURNS:
1424  *      Determined recovery action
1425  */
1426 static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1427                                       const struct ata_taskfile *tf)
1428 {
1429         unsigned int tmp, action = 0;
1430         u8 stat = tf->command, err = tf->feature;
1431
1432         if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1433                 qc->err_mask |= AC_ERR_HSM;
1434                 return ATA_EH_SOFTRESET;
1435         }
1436
1437         if (stat & (ATA_ERR | ATA_DF))
1438                 qc->err_mask |= AC_ERR_DEV;
1439         else
1440                 return 0;
1441
1442         switch (qc->dev->class) {
1443         case ATA_DEV_ATA:
1444                 if (err & ATA_ICRC)
1445                         qc->err_mask |= AC_ERR_ATA_BUS;
1446                 if (err & ATA_UNC)
1447                         qc->err_mask |= AC_ERR_MEDIA;
1448                 if (err & ATA_IDNF)
1449                         qc->err_mask |= AC_ERR_INVALID;
1450                 break;
1451
1452         case ATA_DEV_ATAPI:
1453                 if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
1454                         tmp = atapi_eh_request_sense(qc);
1455                         if (!tmp) {
1456                                 /* ATA_QCFLAG_SENSE_VALID is used to
1457                                  * tell atapi_qc_complete() that sense
1458                                  * data is already valid.
1459                                  *
1460                                  * TODO: interpret sense data and set
1461                                  * appropriate err_mask.
1462                                  */
1463                                 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1464                         } else
1465                                 qc->err_mask |= tmp;
1466                 }
1467         }
1468
1469         if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1470                 action |= ATA_EH_SOFTRESET;
1471
1472         return action;
1473 }
1474
1475 static int ata_eh_categorize_error(int is_io, unsigned int err_mask)
1476 {
1477         if (err_mask & AC_ERR_ATA_BUS)
1478                 return 1;
1479
1480         if (err_mask & AC_ERR_TIMEOUT)
1481                 return 2;
1482
1483         if (is_io) {
1484                 if (err_mask & AC_ERR_HSM)
1485                         return 2;
1486                 if ((err_mask &
1487                      (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1488                         return 3;
1489         }
1490
1491         return 0;
1492 }
1493
1494 struct speed_down_verdict_arg {
1495         u64 since;
1496         int nr_errors[4];
1497 };
1498
1499 static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
1500 {
1501         struct speed_down_verdict_arg *arg = void_arg;
1502         int cat = ata_eh_categorize_error(ent->is_io, ent->err_mask);
1503
1504         if (ent->timestamp < arg->since)
1505                 return -1;
1506
1507         arg->nr_errors[cat]++;
1508         return 0;
1509 }
1510
1511 /**
1512  *      ata_eh_speed_down_verdict - Determine speed down verdict
1513  *      @dev: Device of interest
1514  *
1515  *      This function examines error ring of @dev and determines
1516  *      whether NCQ needs to be turned off, transfer speed should be
1517  *      stepped down, or falling back to PIO is necessary.
1518  *
1519  *      Cat-1 is ATA_BUS error for any command.
1520  *
1521  *      Cat-2 is TIMEOUT for any command or HSM violation for known
1522  *      supported commands.
1523  *
1524  *      Cat-3 is is unclassified DEV error for known supported
1525  *      command.
1526  *
1527  *      NCQ needs to be turned off if there have been more than 3
1528  *      Cat-2 + Cat-3 errors during last 10 minutes.
1529  *
1530  *      Speed down is necessary if there have been more than 3 Cat-1 +
1531  *      Cat-2 errors or 10 Cat-3 errors during last 10 minutes.
1532  *
1533  *      Falling back to PIO mode is necessary if there have been more
1534  *      than 10 Cat-1 + Cat-2 + Cat-3 errors during last 5 minutes.
1535  *
1536  *      LOCKING:
1537  *      Inherited from caller.
1538  *
1539  *      RETURNS:
1540  *      OR of ATA_EH_SPDN_* flags.
1541  */
1542 static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
1543 {
1544         const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
1545         u64 j64 = get_jiffies_64();
1546         struct speed_down_verdict_arg arg;
1547         unsigned int verdict = 0;
1548
1549         /* scan past 10 mins of error history */
1550         memset(&arg, 0, sizeof(arg));
1551         arg.since = j64 - min(j64, j10mins);
1552         ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1553
1554         if (arg.nr_errors[2] + arg.nr_errors[3] > 3)
1555                 verdict |= ATA_EH_SPDN_NCQ_OFF;
1556         if (arg.nr_errors[1] + arg.nr_errors[2] > 3 || arg.nr_errors[3] > 10)
1557                 verdict |= ATA_EH_SPDN_SPEED_DOWN;
1558
1559         /* scan past 3 mins of error history */
1560         memset(&arg, 0, sizeof(arg));
1561         arg.since = j64 - min(j64, j5mins);
1562         ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1563
1564         if (arg.nr_errors[1] + arg.nr_errors[2] + arg.nr_errors[3] > 10)
1565                 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
1566
1567         return verdict;
1568 }
1569
1570 /**
1571  *      ata_eh_speed_down - record error and speed down if necessary
1572  *      @dev: Failed device
1573  *      @is_io: Did the device fail during normal IO?
1574  *      @err_mask: err_mask of the error
1575  *
1576  *      Record error and examine error history to determine whether
1577  *      adjusting transmission speed is necessary.  It also sets
1578  *      transmission limits appropriately if such adjustment is
1579  *      necessary.
1580  *
1581  *      LOCKING:
1582  *      Kernel thread context (may sleep).
1583  *
1584  *      RETURNS:
1585  *      Determined recovery action.
1586  */
1587 static unsigned int ata_eh_speed_down(struct ata_device *dev, int is_io,
1588                                       unsigned int err_mask)
1589 {
1590         unsigned int verdict;
1591         unsigned int action = 0;
1592
1593         /* don't bother if Cat-0 error */
1594         if (ata_eh_categorize_error(is_io, err_mask) == 0)
1595                 return 0;
1596
1597         /* record error and determine whether speed down is necessary */
1598         ata_ering_record(&dev->ering, is_io, err_mask);
1599         verdict = ata_eh_speed_down_verdict(dev);
1600
1601         /* turn off NCQ? */
1602         if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
1603             (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
1604                            ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
1605                 dev->flags |= ATA_DFLAG_NCQ_OFF;
1606                 ata_dev_printk(dev, KERN_WARNING,
1607                                "NCQ disabled due to excessive errors\n");
1608                 goto done;
1609         }
1610
1611         /* speed down? */
1612         if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1613                 /* speed down SATA link speed if possible */
1614                 if (sata_down_spd_limit(dev->link) == 0) {
1615                         action |= ATA_EH_HARDRESET;
1616                         goto done;
1617                 }
1618
1619                 /* lower transfer mode */
1620                 if (dev->spdn_cnt < 2) {
1621                         static const int dma_dnxfer_sel[] =
1622                                 { ATA_DNXFER_DMA, ATA_DNXFER_40C };
1623                         static const int pio_dnxfer_sel[] =
1624                                 { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
1625                         int sel;
1626
1627                         if (dev->xfer_shift != ATA_SHIFT_PIO)
1628                                 sel = dma_dnxfer_sel[dev->spdn_cnt];
1629                         else
1630                                 sel = pio_dnxfer_sel[dev->spdn_cnt];
1631
1632                         dev->spdn_cnt++;
1633
1634                         if (ata_down_xfermask_limit(dev, sel) == 0) {
1635                                 action |= ATA_EH_SOFTRESET;
1636                                 goto done;
1637                         }
1638                 }
1639         }
1640
1641         /* Fall back to PIO?  Slowing down to PIO is meaningless for
1642          * SATA.  Consider it only for PATA.
1643          */
1644         if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
1645             (dev->link->ap->cbl != ATA_CBL_SATA) &&
1646             (dev->xfer_shift != ATA_SHIFT_PIO)) {
1647                 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
1648                         dev->spdn_cnt = 0;
1649                         action |= ATA_EH_SOFTRESET;
1650                         goto done;
1651                 }
1652         }
1653
1654         return 0;
1655  done:
1656         /* device has been slowed down, blow error history */
1657         ata_ering_clear(&dev->ering);
1658         return action;
1659 }
1660
1661 /**
1662  *      ata_eh_link_autopsy - analyze error and determine recovery action
1663  *      @link: host link to perform autopsy on
1664  *
1665  *      Analyze why @link failed and determine which recovery actions
1666  *      are needed.  This function also sets more detailed AC_ERR_*
1667  *      values and fills sense data for ATAPI CHECK SENSE.
1668  *
1669  *      LOCKING:
1670  *      Kernel thread context (may sleep).
1671  */
1672 static void ata_eh_link_autopsy(struct ata_link *link)
1673 {
1674         struct ata_port *ap = link->ap;
1675         struct ata_eh_context *ehc = &link->eh_context;
1676         unsigned int all_err_mask = 0;
1677         int tag, is_io = 0;
1678         u32 serror;
1679         int rc;
1680
1681         DPRINTK("ENTER\n");
1682
1683         if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1684                 return;
1685
1686         /* obtain and analyze SError */
1687         rc = sata_scr_read(link, SCR_ERROR, &serror);
1688         if (rc == 0) {
1689                 ehc->i.serror |= serror;
1690                 ata_eh_analyze_serror(link);
1691         } else if (rc != -EOPNOTSUPP) {
1692                 /* SError read failed, force hardreset and probing */
1693                 ata_ehi_schedule_probe(&ehc->i);
1694                 ehc->i.action |= ATA_EH_HARDRESET;
1695                 ehc->i.err_mask |= AC_ERR_OTHER;
1696         }
1697
1698         /* analyze NCQ failure */
1699         ata_eh_analyze_ncq_error(link);
1700
1701         /* any real error trumps AC_ERR_OTHER */
1702         if (ehc->i.err_mask & ~AC_ERR_OTHER)
1703                 ehc->i.err_mask &= ~AC_ERR_OTHER;
1704
1705         all_err_mask |= ehc->i.err_mask;
1706
1707         for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1708                 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1709
1710                 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
1711                         continue;
1712
1713                 /* inherit upper level err_mask */
1714                 qc->err_mask |= ehc->i.err_mask;
1715
1716                 /* analyze TF */
1717                 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1718
1719                 /* DEV errors are probably spurious in case of ATA_BUS error */
1720                 if (qc->err_mask & AC_ERR_ATA_BUS)
1721                         qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1722                                           AC_ERR_INVALID);
1723
1724                 /* any real error trumps unknown error */
1725                 if (qc->err_mask & ~AC_ERR_OTHER)
1726                         qc->err_mask &= ~AC_ERR_OTHER;
1727
1728                 /* SENSE_VALID trumps dev/unknown error and revalidation */
1729                 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1730                         qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1731                         ehc->i.action &= ~ATA_EH_REVALIDATE;
1732                 }
1733
1734                 /* accumulate error info */
1735                 ehc->i.dev = qc->dev;
1736                 all_err_mask |= qc->err_mask;
1737                 if (qc->flags & ATA_QCFLAG_IO)
1738                         is_io = 1;
1739         }
1740
1741         /* enforce default EH actions */
1742         if (ap->pflags & ATA_PFLAG_FROZEN ||
1743             all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
1744                 ehc->i.action |= ATA_EH_SOFTRESET;
1745         else if (all_err_mask)
1746                 ehc->i.action |= ATA_EH_REVALIDATE;
1747
1748         /* if we have offending qcs and the associated failed device */
1749         if (ehc->i.dev) {
1750                 /* speed down */
1751                 ehc->i.action |= ata_eh_speed_down(ehc->i.dev, is_io,
1752                                                    all_err_mask);
1753
1754                 /* perform per-dev EH action only on the offending device */
1755                 ehc->i.dev_action[ehc->i.dev->devno] |=
1756                         ehc->i.action & ATA_EH_PERDEV_MASK;
1757                 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
1758         }
1759
1760         DPRINTK("EXIT\n");
1761 }
1762
1763 /**
1764  *      ata_eh_autopsy - analyze error and determine recovery action
1765  *      @ap: host port to perform autopsy on
1766  *
1767  *      Analyze all links of @ap and determine why they failed and
1768  *      which recovery actions are needed.
1769  *
1770  *      LOCKING:
1771  *      Kernel thread context (may sleep).
1772  */
1773 void ata_eh_autopsy(struct ata_port *ap)
1774 {
1775         struct ata_link *link;
1776
1777         __ata_port_for_each_link(link, ap)
1778                 ata_eh_link_autopsy(link);
1779 }
1780
1781 /**
1782  *      ata_eh_link_report - report error handling to user
1783  *      @link: ATA link EH is going on
1784  *
1785  *      Report EH to user.
1786  *
1787  *      LOCKING:
1788  *      None.
1789  */
1790 static void ata_eh_link_report(struct ata_link *link)
1791 {
1792         struct ata_port *ap = link->ap;
1793         struct ata_eh_context *ehc = &link->eh_context;
1794         const char *frozen, *desc;
1795         char tries_buf[6];
1796         int tag, nr_failed = 0;
1797
1798         desc = NULL;
1799         if (ehc->i.desc[0] != '\0')
1800                 desc = ehc->i.desc;
1801
1802         for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1803                 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1804
1805                 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
1806                         continue;
1807                 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1808                         continue;
1809
1810                 nr_failed++;
1811         }
1812
1813         if (!nr_failed && !ehc->i.err_mask)
1814                 return;
1815
1816         frozen = "";
1817         if (ap->pflags & ATA_PFLAG_FROZEN)
1818                 frozen = " frozen";
1819
1820         memset(tries_buf, 0, sizeof(tries_buf));
1821         if (ap->eh_tries < ATA_EH_MAX_TRIES)
1822                 snprintf(tries_buf, sizeof(tries_buf) - 1, " t%d",
1823                          ap->eh_tries);
1824
1825         if (ehc->i.dev) {
1826                 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1827                                "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1828                                ehc->i.err_mask, link->sactive, ehc->i.serror,
1829                                ehc->i.action, frozen, tries_buf);
1830                 if (desc)
1831                         ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
1832         } else {
1833                 ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
1834                                 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1835                                 ehc->i.err_mask, link->sactive, ehc->i.serror,
1836                                 ehc->i.action, frozen, tries_buf);
1837                 if (desc)
1838                         ata_link_printk(link, KERN_ERR, "%s\n", desc);
1839         }
1840
1841         for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1842                 static const char *dma_str[] = {
1843                         [DMA_BIDIRECTIONAL]     = "bidi",
1844                         [DMA_TO_DEVICE]         = "out",
1845                         [DMA_FROM_DEVICE]       = "in",
1846                         [DMA_NONE]              = "",
1847                 };
1848                 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1849                 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
1850
1851                 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
1852                     qc->dev->link != link || !qc->err_mask)
1853                         continue;
1854
1855                 ata_dev_printk(qc->dev, KERN_ERR,
1856                         "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
1857                         "tag %d cdb 0x%x data %u %s\n         "
1858                         "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
1859                         "Emask 0x%x (%s)%s\n",
1860                         cmd->command, cmd->feature, cmd->nsect,
1861                         cmd->lbal, cmd->lbam, cmd->lbah,
1862                         cmd->hob_feature, cmd->hob_nsect,
1863                         cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
1864                         cmd->device, qc->tag, qc->cdb[0], qc->nbytes,
1865                         dma_str[qc->dma_dir],
1866                         res->command, res->feature, res->nsect,
1867                         res->lbal, res->lbam, res->lbah,
1868                         res->hob_feature, res->hob_nsect,
1869                         res->hob_lbal, res->hob_lbam, res->hob_lbah,
1870                         res->device, qc->err_mask, ata_err_string(qc->err_mask),
1871                         qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
1872         }
1873 }
1874
1875 /**
1876  *      ata_eh_report - report error handling to user
1877  *      @ap: ATA port to report EH about
1878  *
1879  *      Report EH to user.
1880  *
1881  *      LOCKING:
1882  *      None.
1883  */
1884 void ata_eh_report(struct ata_port *ap)
1885 {
1886         struct ata_link *link;
1887
1888         __ata_port_for_each_link(link, ap)
1889                 ata_eh_link_report(link);
1890 }
1891
1892 static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
1893                         unsigned int *classes, unsigned long deadline)
1894 {
1895         struct ata_device *dev;
1896         int rc;
1897
1898         ata_link_for_each_dev(dev, link)
1899                 classes[dev->devno] = ATA_DEV_UNKNOWN;
1900
1901         rc = reset(link, classes, deadline);
1902         if (rc)
1903                 return rc;
1904
1905         /* If any class isn't ATA_DEV_UNKNOWN, consider classification
1906          * is complete and convert all ATA_DEV_UNKNOWN to
1907          * ATA_DEV_NONE.
1908          */
1909         ata_link_for_each_dev(dev, link)
1910                 if (classes[dev->devno] != ATA_DEV_UNKNOWN)
1911                         break;
1912
1913         if (dev) {
1914                 ata_link_for_each_dev(dev, link) {
1915                         if (classes[dev->devno] == ATA_DEV_UNKNOWN)
1916                                 classes[dev->devno] = ATA_DEV_NONE;
1917                 }
1918         }
1919
1920         return 0;
1921 }
1922
1923 static int ata_eh_followup_srst_needed(struct ata_link *link,
1924                                        int rc, int classify,
1925                                        const unsigned int *classes)
1926 {
1927         if (link->flags & ATA_LFLAG_NO_SRST)
1928                 return 0;
1929         if (rc == -EAGAIN)
1930                 return 1;
1931         if (rc != 0)
1932                 return 0;
1933         if (classify && !(link->flags & ATA_LFLAG_ASSUME_CLASS) &&
1934             classes[0] == ATA_DEV_UNKNOWN)
1935                 return 1;
1936         return 0;
1937 }
1938
1939 int ata_eh_reset(struct ata_link *link, int classify,
1940                  ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
1941                  ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
1942 {
1943         struct ata_eh_context *ehc = &link->eh_context;
1944         unsigned int *classes = ehc->classes;
1945         int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
1946         int try = 0;
1947         struct ata_device *dev;
1948         unsigned long deadline;
1949         unsigned int action;
1950         ata_reset_fn_t reset;
1951         int rc;
1952
1953         /* about to reset */
1954         ata_eh_about_to_do(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
1955
1956         /* Determine which reset to use and record in ehc->i.action.
1957          * prereset() may examine and modify it.
1958          */
1959         action = ehc->i.action;
1960         ehc->i.action &= ~ATA_EH_RESET_MASK;
1961         if (softreset && (!hardreset || (!(link->flags & ATA_LFLAG_NO_SRST) &&
1962                                          !sata_set_spd_needed(link) &&
1963                                          !(action & ATA_EH_HARDRESET))))
1964                 ehc->i.action |= ATA_EH_SOFTRESET;
1965         else
1966                 ehc->i.action |= ATA_EH_HARDRESET;
1967
1968         if (prereset) {
1969                 rc = prereset(link, jiffies + ATA_EH_PRERESET_TIMEOUT);
1970                 if (rc) {
1971                         if (rc == -ENOENT) {
1972                                 ata_link_printk(link, KERN_DEBUG,
1973                                                 "port disabled. ignoring.\n");
1974                                 ehc->i.action &= ~ATA_EH_RESET_MASK;
1975
1976                                 ata_link_for_each_dev(dev, link)
1977                                         classes[dev->devno] = ATA_DEV_NONE;
1978
1979                                 rc = 0;
1980                         } else
1981                                 ata_link_printk(link, KERN_ERR,
1982                                         "prereset failed (errno=%d)\n", rc);
1983                         goto out;
1984                 }
1985         }
1986
1987         /* prereset() might have modified ehc->i.action */
1988         if (ehc->i.action & ATA_EH_HARDRESET)
1989                 reset = hardreset;
1990         else if (ehc->i.action & ATA_EH_SOFTRESET)
1991                 reset = softreset;
1992         else {
1993                 /* prereset told us not to reset, bang classes and return */
1994                 ata_link_for_each_dev(dev, link)
1995                         classes[dev->devno] = ATA_DEV_NONE;
1996                 rc = 0;
1997                 goto out;
1998         }
1999
2000         /* did prereset() screw up?  if so, fix up to avoid oopsing */
2001         if (!reset) {
2002                 if (softreset)
2003                         reset = softreset;
2004                 else
2005                         reset = hardreset;
2006         }
2007
2008  retry:
2009         deadline = jiffies + ata_eh_reset_timeouts[try++];
2010
2011         /* shut up during boot probing */
2012         if (verbose)
2013                 ata_link_printk(link, KERN_INFO, "%s resetting link\n",
2014                                 reset == softreset ? "soft" : "hard");
2015
2016         /* mark that this EH session started with reset */
2017         if (reset == hardreset)
2018                 ehc->i.flags |= ATA_EHI_DID_HARDRESET;
2019         else
2020                 ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
2021
2022         rc = ata_do_reset(link, reset, classes, deadline);
2023
2024         if (reset == hardreset &&
2025             ata_eh_followup_srst_needed(link, rc, classify, classes)) {
2026                 /* okay, let's do follow-up softreset */
2027                 reset = softreset;
2028
2029                 if (!reset) {
2030                         ata_link_printk(link, KERN_ERR,
2031                                         "follow-up softreset required "
2032                                         "but no softreset avaliable\n");
2033                         rc = -EINVAL;
2034                         goto out;
2035                 }
2036
2037                 ata_eh_about_to_do(link, NULL, ATA_EH_RESET_MASK);
2038                 rc = ata_do_reset(link, reset, classes, deadline);
2039
2040                 if (rc == 0 && classify && classes[0] == ATA_DEV_UNKNOWN &&
2041                     !(link->flags & ATA_LFLAG_ASSUME_CLASS)) {
2042                         ata_link_printk(link, KERN_ERR,
2043                                         "classification failed\n");
2044                         rc = -EINVAL;
2045                         goto out;
2046                 }
2047         }
2048
2049         /* if we skipped follow-up srst, clear rc */
2050         if (rc == -EAGAIN)
2051                 rc = 0;
2052
2053         if (rc && try < ARRAY_SIZE(ata_eh_reset_timeouts)) {
2054                 unsigned long now = jiffies;
2055
2056                 if (time_before(now, deadline)) {
2057                         unsigned long delta = deadline - jiffies;
2058
2059                         ata_link_printk(link, KERN_WARNING, "reset failed "
2060                                 "(errno=%d), retrying in %u secs\n",
2061                                 rc, (jiffies_to_msecs(delta) + 999) / 1000);
2062
2063                         schedule_timeout_uninterruptible(delta);
2064                 }
2065
2066                 if (rc == -EPIPE ||
2067                     try == ARRAY_SIZE(ata_eh_reset_timeouts) - 1)
2068                         sata_down_spd_limit(link);
2069                 if (hardreset)
2070                         reset = hardreset;
2071                 goto retry;
2072         }
2073
2074         if (rc == 0) {
2075                 u32 sstatus;
2076
2077                 ata_link_for_each_dev(dev, link) {
2078                         /* After the reset, the device state is PIO 0
2079                          * and the controller state is undefined.
2080                          * Record the mode.
2081                          */
2082                         dev->pio_mode = XFER_PIO_0;
2083
2084                         if (ata_link_offline(link))
2085                                 continue;
2086
2087                         /* apply class override and convert UNKNOWN to NONE */
2088                         if (link->flags & ATA_LFLAG_ASSUME_ATA)
2089                                 classes[dev->devno] = ATA_DEV_ATA;
2090                         else if (link->flags & ATA_LFLAG_ASSUME_SEMB)
2091                                 classes[dev->devno] = ATA_DEV_SEMB_UNSUP; /* not yet */
2092                         else if (classes[dev->devno] == ATA_DEV_UNKNOWN)
2093                                 classes[dev->devno] = ATA_DEV_NONE;
2094                 }
2095
2096                 /* record current link speed */
2097                 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2098                         link->sata_spd = (sstatus >> 4) & 0xf;
2099
2100                 if (postreset)
2101                         postreset(link, classes);
2102
2103                 /* reset successful, schedule revalidation */
2104                 ata_eh_done(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
2105                 ehc->i.action |= ATA_EH_REVALIDATE;
2106         }
2107  out:
2108         /* clear hotplug flag */
2109         ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2110         return rc;
2111 }
2112
2113 static int ata_eh_revalidate_and_attach(struct ata_link *link,
2114                                         struct ata_device **r_failed_dev)
2115 {
2116         struct ata_port *ap = link->ap;
2117         struct ata_eh_context *ehc = &link->eh_context;
2118         struct ata_device *dev;
2119         unsigned int new_mask = 0;
2120         unsigned long flags;
2121         int rc = 0;
2122
2123         DPRINTK("ENTER\n");
2124
2125         /* For PATA drive side cable detection to work, IDENTIFY must
2126          * be done backwards such that PDIAG- is released by the slave
2127          * device before the master device is identified.
2128          */
2129         ata_link_for_each_dev_reverse(dev, link) {
2130                 unsigned int action = ata_eh_dev_action(dev);
2131                 unsigned int readid_flags = 0;
2132
2133                 if (ehc->i.flags & ATA_EHI_DID_RESET)
2134                         readid_flags |= ATA_READID_POSTRESET;
2135
2136                 if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
2137                         if (ata_link_offline(link)) {
2138                                 rc = -EIO;
2139                                 goto err;
2140                         }
2141
2142                         ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
2143                         rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
2144                                                 readid_flags);
2145                         if (rc)
2146                                 goto err;
2147
2148                         ata_eh_done(link, dev, ATA_EH_REVALIDATE);
2149
2150                         /* Configuration may have changed, reconfigure
2151                          * transfer mode.
2152                          */
2153                         ehc->i.flags |= ATA_EHI_SETMODE;
2154
2155                         /* schedule the scsi_rescan_device() here */
2156                         queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
2157                 } else if (dev->class == ATA_DEV_UNKNOWN &&
2158                            ehc->tries[dev->devno] &&
2159                            ata_class_enabled(ehc->classes[dev->devno])) {
2160                         dev->class = ehc->classes[dev->devno];
2161
2162                         rc = ata_dev_read_id(dev, &dev->class, readid_flags,
2163                                              dev->id);
2164                         switch (rc) {
2165                         case 0:
2166                                 new_mask |= 1 << dev->devno;
2167                                 break;
2168                         case -ENOENT:
2169                                 /* IDENTIFY was issued to non-existent
2170                                  * device.  No need to reset.  Just
2171                                  * thaw and kill the device.
2172                                  */
2173                                 ata_eh_thaw_port(ap);
2174                                 dev->class = ATA_DEV_UNKNOWN;
2175                                 break;
2176                         default:
2177                                 dev->class = ATA_DEV_UNKNOWN;
2178                                 goto err;
2179                         }
2180                 }
2181         }
2182
2183         /* PDIAG- should have been released, ask cable type if post-reset */
2184         if (ata_is_host_link(link) && ap->ops->cable_detect &&
2185             (ehc->i.flags & ATA_EHI_DID_RESET))
2186                 ap->cbl = ap->ops->cable_detect(ap);
2187
2188         /* Configure new devices forward such that user doesn't see
2189          * device detection messages backwards.
2190          */
2191         ata_link_for_each_dev(dev, link) {
2192                 if (!(new_mask & (1 << dev->devno)))
2193                         continue;
2194
2195                 ehc->i.flags |= ATA_EHI_PRINTINFO;
2196                 rc = ata_dev_configure(dev);
2197                 ehc->i.flags &= ~ATA_EHI_PRINTINFO;
2198                 if (rc)
2199                         goto err;
2200
2201                 spin_lock_irqsave(ap->lock, flags);
2202                 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
2203                 spin_unlock_irqrestore(ap->lock, flags);
2204
2205                 /* new device discovered, configure xfermode */
2206                 ehc->i.flags |= ATA_EHI_SETMODE;
2207         }
2208
2209         return 0;
2210
2211  err:
2212         *r_failed_dev = dev;
2213         DPRINTK("EXIT rc=%d\n", rc);
2214         return rc;
2215 }
2216
2217 static int ata_link_nr_enabled(struct ata_link *link)
2218 {
2219         struct ata_device *dev;
2220         int cnt = 0;
2221
2222         ata_link_for_each_dev(dev, link)
2223                 if (ata_dev_enabled(dev))
2224                         cnt++;
2225         return cnt;
2226 }
2227
2228 static int ata_link_nr_vacant(struct ata_link *link)
2229 {
2230         struct ata_device *dev;
2231         int cnt = 0;
2232
2233         ata_link_for_each_dev(dev, link)
2234                 if (dev->class == ATA_DEV_UNKNOWN)
2235                         cnt++;
2236         return cnt;
2237 }
2238
2239 static int ata_eh_skip_recovery(struct ata_link *link)
2240 {
2241         struct ata_eh_context *ehc = &link->eh_context;
2242         struct ata_device *dev;
2243
2244         /* skip disabled links */
2245         if (link->flags & ATA_LFLAG_DISABLED)
2246                 return 1;
2247
2248         /* thaw frozen port, resume link and recover failed devices */
2249         if ((link->ap->pflags & ATA_PFLAG_FROZEN) ||
2250             (ehc->i.flags & ATA_EHI_RESUME_LINK) || ata_link_nr_enabled(link))
2251                 return 0;
2252
2253         /* skip if class codes for all vacant slots are ATA_DEV_NONE */
2254         ata_link_for_each_dev(dev, link) {
2255                 if (dev->class == ATA_DEV_UNKNOWN &&
2256                     ehc->classes[dev->devno] != ATA_DEV_NONE)
2257                         return 0;
2258         }
2259
2260         return 1;
2261 }
2262
2263 static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
2264 {
2265         struct ata_eh_context *ehc = &dev->link->eh_context;
2266
2267         ehc->tries[dev->devno]--;
2268
2269         switch (err) {
2270         case -ENODEV:
2271                 /* device missing or wrong IDENTIFY data, schedule probing */
2272                 ehc->i.probe_mask |= (1 << dev->devno);
2273         case -EINVAL:
2274                 /* give it just one more chance */
2275                 ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
2276         case -EIO:
2277                 if (ehc->tries[dev->devno] == 1) {
2278                         /* This is the last chance, better to slow
2279                          * down than lose it.
2280                          */
2281                         sata_down_spd_limit(dev->link);
2282                         ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2283                 }
2284         }
2285
2286         if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2287                 /* disable device if it has used up all its chances */
2288                 ata_dev_disable(dev);
2289
2290                 /* detach if offline */
2291                 if (ata_link_offline(dev->link))
2292                         ata_eh_detach_dev(dev);
2293
2294                 /* probe if requested */
2295                 if ((ehc->i.probe_mask & (1 << dev->devno)) &&
2296                     !(ehc->did_probe_mask & (1 << dev->devno))) {
2297                         ata_eh_detach_dev(dev);
2298                         ata_dev_init(dev);
2299
2300                         ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2301                         ehc->did_probe_mask |= (1 << dev->devno);
2302                         ehc->i.action |= ATA_EH_SOFTRESET;
2303                 }
2304
2305                 return 1;
2306         } else {
2307                 /* soft didn't work?  be haaaaard */
2308                 if (ehc->i.flags & ATA_EHI_DID_RESET)
2309                         ehc->i.action |= ATA_EH_HARDRESET;
2310                 else
2311                         ehc->i.action |= ATA_EH_SOFTRESET;
2312
2313                 return 0;
2314         }
2315 }
2316
2317 /**
2318  *      ata_eh_recover - recover host port after error
2319  *      @ap: host port to recover
2320  *      @prereset: prereset method (can be NULL)
2321  *      @softreset: softreset method (can be NULL)
2322  *      @hardreset: hardreset method (can be NULL)
2323  *      @postreset: postreset method (can be NULL)
2324  *      @r_failed_link: out parameter for failed link
2325  *
2326  *      This is the alpha and omega, eum and yang, heart and soul of
2327  *      libata exception handling.  On entry, actions required to
2328  *      recover each link and hotplug requests are recorded in the
2329  *      link's eh_context.  This function executes all the operations
2330  *      with appropriate retrials and fallbacks to resurrect failed
2331  *      devices, detach goners and greet newcomers.
2332  *
2333  *      LOCKING:
2334  *      Kernel thread context (may sleep).
2335  *
2336  *      RETURNS:
2337  *      0 on success, -errno on failure.
2338  */
2339 int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
2340                    ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2341                    ata_postreset_fn_t postreset,
2342                    struct ata_link **r_failed_link)
2343 {
2344         struct ata_link *link;
2345         struct ata_device *dev;
2346         int nr_failed_devs, nr_disabled_devs;
2347         int reset, rc;
2348         unsigned long flags;
2349
2350         DPRINTK("ENTER\n");
2351
2352         /* prep for recovery */
2353         ata_port_for_each_link(link, ap) {
2354                 struct ata_eh_context *ehc = &link->eh_context;
2355
2356                 /* re-enable link? */
2357                 if (ehc->i.action & ATA_EH_ENABLE_LINK) {
2358                         ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
2359                         spin_lock_irqsave(ap->lock, flags);
2360                         link->flags &= ~ATA_LFLAG_DISABLED;
2361                         spin_unlock_irqrestore(ap->lock, flags);
2362                         ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
2363                 }
2364
2365                 ata_link_for_each_dev(dev, link) {
2366                         if (link->flags & ATA_LFLAG_NO_RETRY)
2367                                 ehc->tries[dev->devno] = 1;
2368                         else
2369                                 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2370
2371                         /* collect port action mask recorded in dev actions */
2372                         ehc->i.action |= ehc->i.dev_action[dev->devno] &
2373                                          ~ATA_EH_PERDEV_MASK;
2374                         ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
2375
2376                         /* process hotplug request */
2377                         if (dev->flags & ATA_DFLAG_DETACH)
2378                                 ata_eh_detach_dev(dev);
2379
2380                         if (!ata_dev_enabled(dev) &&
2381                             ((ehc->i.probe_mask & (1 << dev->devno)) &&
2382                              !(ehc->did_probe_mask & (1 << dev->devno)))) {
2383                                 ata_eh_detach_dev(dev);
2384                                 ata_dev_init(dev);
2385                                 ehc->did_probe_mask |= (1 << dev->devno);
2386                                 ehc->i.action |= ATA_EH_SOFTRESET;
2387                         }
2388                 }
2389         }
2390
2391  retry:
2392         rc = 0;
2393         nr_failed_devs = 0;
2394         nr_disabled_devs = 0;
2395         reset = 0;
2396
2397         /* if UNLOADING, finish immediately */
2398         if (ap->pflags & ATA_PFLAG_UNLOADING)
2399                 goto out;
2400
2401         /* prep for EH */
2402         ata_port_for_each_link(link, ap) {
2403                 struct ata_eh_context *ehc = &link->eh_context;
2404
2405                 /* skip EH if possible. */
2406                 if (ata_eh_skip_recovery(link))
2407                         ehc->i.action = 0;
2408
2409                 /* do we need to reset? */
2410                 if (ehc->i.action & ATA_EH_RESET_MASK)
2411                         reset = 1;
2412
2413                 ata_link_for_each_dev(dev, link)
2414                         ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
2415         }
2416
2417         /* reset */
2418         if (reset) {
2419                 ata_eh_freeze_port(ap);
2420
2421                 ata_port_for_each_link(link, ap) {
2422                         struct ata_eh_context *ehc = &link->eh_context;
2423
2424                         if (!(ehc->i.action & ATA_EH_RESET_MASK))
2425                                 continue;
2426
2427                         rc = ata_eh_reset(link, ata_link_nr_vacant(link),
2428                                           prereset, softreset, hardreset,
2429                                           postreset);
2430                         if (rc) {
2431                                 ata_link_printk(link, KERN_ERR,
2432                                                 "reset failed, giving up\n");
2433                                 goto out;
2434                         }
2435                 }
2436
2437                 ata_eh_thaw_port(ap);
2438         }
2439
2440         /* the rest */
2441         ata_port_for_each_link(link, ap) {
2442                 struct ata_eh_context *ehc = &link->eh_context;
2443
2444                 /* revalidate existing devices and attach new ones */
2445                 rc = ata_eh_revalidate_and_attach(link, &dev);
2446                 if (rc)
2447                         goto dev_fail;
2448
2449                 /* configure transfer mode if necessary */
2450                 if (ehc->i.flags & ATA_EHI_SETMODE) {
2451                         rc = ata_set_mode(link, &dev);
2452                         if (rc)
2453                                 goto dev_fail;
2454                         ehc->i.flags &= ~ATA_EHI_SETMODE;
2455                 }
2456
2457                 /* this link is okay now */
2458                 ehc->i.flags = 0;
2459                 continue;
2460
2461         dev_fail:
2462                 nr_failed_devs++;
2463                 if (ata_eh_handle_dev_fail(dev, rc))
2464                         nr_disabled_devs++;
2465
2466                 if (ap->pflags & ATA_PFLAG_FROZEN)
2467                         break;
2468         }
2469
2470         if (nr_failed_devs) {
2471                 if (nr_failed_devs != nr_disabled_devs) {
2472                         ata_port_printk(ap, KERN_WARNING, "failed to recover "
2473                                         "some devices, retrying in 5 secs\n");
2474                         ssleep(5);
2475                 } else {
2476                         /* no device left to recover, repeat fast */
2477                         msleep(500);
2478                 }
2479
2480                 goto retry;
2481         }
2482
2483  out:
2484         if (rc && r_failed_link)
2485                 *r_failed_link = link;
2486
2487         DPRINTK("EXIT, rc=%d\n", rc);
2488         return rc;
2489 }
2490
2491 /**
2492  *      ata_eh_finish - finish up EH
2493  *      @ap: host port to finish EH for
2494  *
2495  *      Recovery is complete.  Clean up EH states and retry or finish
2496  *      failed qcs.
2497  *
2498  *      LOCKING:
2499  *      None.
2500  */
2501 void ata_eh_finish(struct ata_port *ap)
2502 {
2503         int tag;
2504
2505         /* retry or finish qcs */
2506         for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2507                 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2508
2509                 if (!(qc->flags & ATA_QCFLAG_FAILED))
2510                         continue;
2511
2512                 if (qc->err_mask) {
2513                         /* FIXME: Once EH migration is complete,
2514                          * generate sense data in this function,
2515                          * considering both err_mask and tf.
2516                          */
2517                         if (qc->err_mask & AC_ERR_INVALID)
2518                                 ata_eh_qc_complete(qc);
2519                         else
2520                                 ata_eh_qc_retry(qc);
2521                 } else {
2522                         if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2523                                 ata_eh_qc_complete(qc);
2524                         } else {
2525                                 /* feed zero TF to sense generation */
2526                                 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2527                                 ata_eh_qc_retry(qc);
2528                         }
2529                 }
2530         }
2531
2532         /* make sure nr_active_links is zero after EH */
2533         WARN_ON(ap->nr_active_links);
2534         ap->nr_active_links = 0;
2535 }
2536
2537 /**
2538  *      ata_do_eh - do standard error handling
2539  *      @ap: host port to handle error for
2540  *      @prereset: prereset method (can be NULL)
2541  *      @softreset: softreset method (can be NULL)
2542  *      @hardreset: hardreset method (can be NULL)
2543  *      @postreset: postreset method (can be NULL)
2544  *
2545  *      Perform standard error handling sequence.
2546  *
2547  *      LOCKING:
2548  *      Kernel thread context (may sleep).
2549  */
2550 void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2551                ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2552                ata_postreset_fn_t postreset)
2553 {
2554         struct ata_device *dev;
2555         int rc;
2556
2557         ata_eh_autopsy(ap);
2558         ata_eh_report(ap);
2559
2560         rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
2561                             NULL);
2562         if (rc) {
2563                 ata_link_for_each_dev(dev, &ap->link)
2564                         ata_dev_disable(dev);
2565         }
2566
2567         ata_eh_finish(ap);
2568 }
2569
2570 #ifdef CONFIG_PM
2571 /**
2572  *      ata_eh_handle_port_suspend - perform port suspend operation
2573  *      @ap: port to suspend
2574  *
2575  *      Suspend @ap.
2576  *
2577  *      LOCKING:
2578  *      Kernel thread context (may sleep).
2579  */
2580 static void ata_eh_handle_port_suspend(struct ata_port *ap)
2581 {
2582         unsigned long flags;
2583         int rc = 0;
2584
2585         /* are we suspending? */
2586         spin_lock_irqsave(ap->lock, flags);
2587         if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2588             ap->pm_mesg.event == PM_EVENT_ON) {
2589                 spin_unlock_irqrestore(ap->lock, flags);
2590                 return;
2591         }
2592         spin_unlock_irqrestore(ap->lock, flags);
2593
2594         WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
2595
2596         /* tell ACPI we're suspending */
2597         rc = ata_acpi_on_suspend(ap);
2598         if (rc)
2599                 goto out;
2600
2601         /* suspend */
2602         ata_eh_freeze_port(ap);
2603
2604         if (ap->ops->port_suspend)
2605                 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
2606
2607  out:
2608         /* report result */
2609         spin_lock_irqsave(ap->lock, flags);
2610
2611         ap->pflags &= ~ATA_PFLAG_PM_PENDING;
2612         if (rc == 0)
2613                 ap->pflags |= ATA_PFLAG_SUSPENDED;
2614         else if (ap->pflags & ATA_PFLAG_FROZEN)
2615                 ata_port_schedule_eh(ap);
2616
2617         if (ap->pm_result) {
2618                 *ap->pm_result = rc;
2619                 ap->pm_result = NULL;
2620         }
2621
2622         spin_unlock_irqrestore(ap->lock, flags);
2623
2624         return;
2625 }
2626
2627 /**
2628  *      ata_eh_handle_port_resume - perform port resume operation
2629  *      @ap: port to resume
2630  *
2631  *      Resume @ap.
2632  *
2633  *      LOCKING:
2634  *      Kernel thread context (may sleep).
2635  */
2636 static void ata_eh_handle_port_resume(struct ata_port *ap)
2637 {
2638         unsigned long flags;
2639         int rc = 0;
2640
2641         /* are we resuming? */
2642         spin_lock_irqsave(ap->lock, flags);
2643         if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2644             ap->pm_mesg.event != PM_EVENT_ON) {
2645                 spin_unlock_irqrestore(ap->lock, flags);
2646                 return;
2647         }
2648         spin_unlock_irqrestore(ap->lock, flags);
2649
2650         WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
2651
2652         if (ap->ops->port_resume)
2653                 rc = ap->ops->port_resume(ap);
2654
2655         /* tell ACPI that we're resuming */
2656         ata_acpi_on_resume(ap);
2657
2658         /* report result */
2659         spin_lock_irqsave(ap->lock, flags);
2660         ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
2661         if (ap->pm_result) {
2662                 *ap->pm_result = rc;
2663                 ap->pm_result = NULL;
2664         }
2665         spin_unlock_irqrestore(ap->lock, flags);
2666 }
2667 #endif /* CONFIG_PM */