ata: remove stale maintainership information from core code
[linux-block.git] / drivers / ata / libata-eh.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  libata-eh.c - libata error handling
4  *
5  *  Copyright 2006 Tejun Heo <htejun@gmail.com>
6  *
7  *  libata documentation is available via 'make {ps|pdf}docs',
8  *  as Documentation/driver-api/libata.rst
9  *
10  *  Hardware documentation available from http://www.t13.org/ and
11  *  http://www.sata-io.org/
12  */
13
14 #include <linux/kernel.h>
15 #include <linux/blkdev.h>
16 #include <linux/export.h>
17 #include <linux/pci.h>
18 #include <scsi/scsi.h>
19 #include <scsi/scsi_host.h>
20 #include <scsi/scsi_eh.h>
21 #include <scsi/scsi_device.h>
22 #include <scsi/scsi_cmnd.h>
23 #include <scsi/scsi_dbg.h>
24 #include "../scsi/scsi_transport_api.h"
25
26 #include <linux/libata.h>
27
28 #include <trace/events/libata.h>
29 #include "libata.h"
30
31 enum {
32         /* speed down verdicts */
33         ATA_EH_SPDN_NCQ_OFF             = (1 << 0),
34         ATA_EH_SPDN_SPEED_DOWN          = (1 << 1),
35         ATA_EH_SPDN_FALLBACK_TO_PIO     = (1 << 2),
36         ATA_EH_SPDN_KEEP_ERRORS         = (1 << 3),
37
38         /* error flags */
39         ATA_EFLAG_IS_IO                 = (1 << 0),
40         ATA_EFLAG_DUBIOUS_XFER          = (1 << 1),
41         ATA_EFLAG_OLD_ER                = (1 << 31),
42
43         /* error categories */
44         ATA_ECAT_NONE                   = 0,
45         ATA_ECAT_ATA_BUS                = 1,
46         ATA_ECAT_TOUT_HSM               = 2,
47         ATA_ECAT_UNK_DEV                = 3,
48         ATA_ECAT_DUBIOUS_NONE           = 4,
49         ATA_ECAT_DUBIOUS_ATA_BUS        = 5,
50         ATA_ECAT_DUBIOUS_TOUT_HSM       = 6,
51         ATA_ECAT_DUBIOUS_UNK_DEV        = 7,
52         ATA_ECAT_NR                     = 8,
53
54         ATA_EH_CMD_DFL_TIMEOUT          =  5000,
55
56         /* always put at least this amount of time between resets */
57         ATA_EH_RESET_COOL_DOWN          =  5000,
58
59         /* Waiting in ->prereset can never be reliable.  It's
60          * sometimes nice to wait there but it can't be depended upon;
61          * otherwise, we wouldn't be resetting.  Just give it enough
62          * time for most drives to spin up.
63          */
64         ATA_EH_PRERESET_TIMEOUT         = 10000,
65         ATA_EH_FASTDRAIN_INTERVAL       =  3000,
66
67         ATA_EH_UA_TRIES                 = 5,
68
69         /* probe speed down parameters, see ata_eh_schedule_probe() */
70         ATA_EH_PROBE_TRIAL_INTERVAL     = 60000,        /* 1 min */
71         ATA_EH_PROBE_TRIALS             = 2,
72 };
73
74 /* The following table determines how we sequence resets.  Each entry
75  * represents timeout for that try.  The first try can be soft or
76  * hardreset.  All others are hardreset if available.  In most cases
77  * the first reset w/ 10sec timeout should succeed.  Following entries
78  * are mostly for error handling, hotplug and those outlier devices that
79  * take an exceptionally long time to recover from reset.
80  */
81 static const unsigned long ata_eh_reset_timeouts[] = {
82         10000,  /* most drives spin up by 10sec */
83         10000,  /* > 99% working drives spin up before 20sec */
84         35000,  /* give > 30 secs of idleness for outlier devices */
85          5000,  /* and sweet one last chance */
86         ULONG_MAX, /* > 1 min has elapsed, give up */
87 };
88
89 static const unsigned long ata_eh_identify_timeouts[] = {
90          5000,  /* covers > 99% of successes and not too boring on failures */
91         10000,  /* combined time till here is enough even for media access */
92         30000,  /* for true idiots */
93         ULONG_MAX,
94 };
95
96 static const unsigned long ata_eh_flush_timeouts[] = {
97         15000,  /* be generous with flush */
98         15000,  /* ditto */
99         30000,  /* and even more generous */
100         ULONG_MAX,
101 };
102
103 static const unsigned long ata_eh_other_timeouts[] = {
104          5000,  /* same rationale as identify timeout */
105         10000,  /* ditto */
106         /* but no merciful 30sec for other commands, it just isn't worth it */
107         ULONG_MAX,
108 };
109
110 struct ata_eh_cmd_timeout_ent {
111         const u8                *commands;
112         const unsigned long     *timeouts;
113 };
114
115 /* The following table determines timeouts to use for EH internal
116  * commands.  Each table entry is a command class and matches the
117  * commands the entry applies to and the timeout table to use.
118  *
119  * On the retry after a command timed out, the next timeout value from
120  * the table is used.  If the table doesn't contain further entries,
121  * the last value is used.
122  *
123  * ehc->cmd_timeout_idx keeps track of which timeout to use per
124  * command class, so if SET_FEATURES times out on the first try, the
125  * next try will use the second timeout value only for that class.
126  */
127 #define CMDS(cmds...)   (const u8 []){ cmds, 0 }
128 static const struct ata_eh_cmd_timeout_ent
129 ata_eh_cmd_timeout_table[ATA_EH_CMD_TIMEOUT_TABLE_SIZE] = {
130         { .commands = CMDS(ATA_CMD_ID_ATA, ATA_CMD_ID_ATAPI),
131           .timeouts = ata_eh_identify_timeouts, },
132         { .commands = CMDS(ATA_CMD_READ_NATIVE_MAX, ATA_CMD_READ_NATIVE_MAX_EXT),
133           .timeouts = ata_eh_other_timeouts, },
134         { .commands = CMDS(ATA_CMD_SET_MAX, ATA_CMD_SET_MAX_EXT),
135           .timeouts = ata_eh_other_timeouts, },
136         { .commands = CMDS(ATA_CMD_SET_FEATURES),
137           .timeouts = ata_eh_other_timeouts, },
138         { .commands = CMDS(ATA_CMD_INIT_DEV_PARAMS),
139           .timeouts = ata_eh_other_timeouts, },
140         { .commands = CMDS(ATA_CMD_FLUSH, ATA_CMD_FLUSH_EXT),
141           .timeouts = ata_eh_flush_timeouts },
142 };
143 #undef CMDS
144
145 static void __ata_port_freeze(struct ata_port *ap);
146 #ifdef CONFIG_PM
147 static void ata_eh_handle_port_suspend(struct ata_port *ap);
148 static void ata_eh_handle_port_resume(struct ata_port *ap);
149 #else /* CONFIG_PM */
150 static void ata_eh_handle_port_suspend(struct ata_port *ap)
151 { }
152
153 static void ata_eh_handle_port_resume(struct ata_port *ap)
154 { }
155 #endif /* CONFIG_PM */
156
157 static __printf(2, 0) void __ata_ehi_pushv_desc(struct ata_eh_info *ehi,
158                                  const char *fmt, va_list args)
159 {
160         ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
161                                      ATA_EH_DESC_LEN - ehi->desc_len,
162                                      fmt, args);
163 }
164
165 /**
166  *      __ata_ehi_push_desc - push error description without adding separator
167  *      @ehi: target EHI
168  *      @fmt: printf format string
169  *
170  *      Format string according to @fmt and append it to @ehi->desc.
171  *
172  *      LOCKING:
173  *      spin_lock_irqsave(host lock)
174  */
175 void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
176 {
177         va_list args;
178
179         va_start(args, fmt);
180         __ata_ehi_pushv_desc(ehi, fmt, args);
181         va_end(args);
182 }
183
184 /**
185  *      ata_ehi_push_desc - push error description with separator
186  *      @ehi: target EHI
187  *      @fmt: printf format string
188  *
189  *      Format string according to @fmt and append it to @ehi->desc.
190  *      If @ehi->desc is not empty, ", " is added in-between.
191  *
192  *      LOCKING:
193  *      spin_lock_irqsave(host lock)
194  */
195 void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
196 {
197         va_list args;
198
199         if (ehi->desc_len)
200                 __ata_ehi_push_desc(ehi, ", ");
201
202         va_start(args, fmt);
203         __ata_ehi_pushv_desc(ehi, fmt, args);
204         va_end(args);
205 }
206
207 /**
208  *      ata_ehi_clear_desc - clean error description
209  *      @ehi: target EHI
210  *
211  *      Clear @ehi->desc.
212  *
213  *      LOCKING:
214  *      spin_lock_irqsave(host lock)
215  */
216 void ata_ehi_clear_desc(struct ata_eh_info *ehi)
217 {
218         ehi->desc[0] = '\0';
219         ehi->desc_len = 0;
220 }
221
222 /**
223  *      ata_port_desc - append port description
224  *      @ap: target ATA port
225  *      @fmt: printf format string
226  *
227  *      Format string according to @fmt and append it to port
228  *      description.  If port description is not empty, " " is added
229  *      in-between.  This function is to be used while initializing
230  *      ata_host.  The description is printed on host registration.
231  *
232  *      LOCKING:
233  *      None.
234  */
235 void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
236 {
237         va_list args;
238
239         WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
240
241         if (ap->link.eh_info.desc_len)
242                 __ata_ehi_push_desc(&ap->link.eh_info, " ");
243
244         va_start(args, fmt);
245         __ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
246         va_end(args);
247 }
248
249 #ifdef CONFIG_PCI
250
251 /**
252  *      ata_port_pbar_desc - append PCI BAR description
253  *      @ap: target ATA port
254  *      @bar: target PCI BAR
255  *      @offset: offset into PCI BAR
256  *      @name: name of the area
257  *
258  *      If @offset is negative, this function formats a string which
259  *      contains the name, address, size and type of the BAR and
260  *      appends it to the port description.  If @offset is zero or
261  *      positive, only name and offsetted address is appended.
262  *
263  *      LOCKING:
264  *      None.
265  */
266 void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
267                         const char *name)
268 {
269         struct pci_dev *pdev = to_pci_dev(ap->host->dev);
270         char *type = "";
271         unsigned long long start, len;
272
273         if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
274                 type = "m";
275         else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
276                 type = "i";
277
278         start = (unsigned long long)pci_resource_start(pdev, bar);
279         len = (unsigned long long)pci_resource_len(pdev, bar);
280
281         if (offset < 0)
282                 ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
283         else
284                 ata_port_desc(ap, "%s 0x%llx", name,
285                                 start + (unsigned long long)offset);
286 }
287
288 #endif /* CONFIG_PCI */
289
290 static int ata_lookup_timeout_table(u8 cmd)
291 {
292         int i;
293
294         for (i = 0; i < ATA_EH_CMD_TIMEOUT_TABLE_SIZE; i++) {
295                 const u8 *cur;
296
297                 for (cur = ata_eh_cmd_timeout_table[i].commands; *cur; cur++)
298                         if (*cur == cmd)
299                                 return i;
300         }
301
302         return -1;
303 }
304
305 /**
306  *      ata_internal_cmd_timeout - determine timeout for an internal command
307  *      @dev: target device
308  *      @cmd: internal command to be issued
309  *
310  *      Determine timeout for internal command @cmd for @dev.
311  *
312  *      LOCKING:
313  *      EH context.
314  *
315  *      RETURNS:
316  *      Determined timeout.
317  */
318 unsigned long ata_internal_cmd_timeout(struct ata_device *dev, u8 cmd)
319 {
320         struct ata_eh_context *ehc = &dev->link->eh_context;
321         int ent = ata_lookup_timeout_table(cmd);
322         int idx;
323
324         if (ent < 0)
325                 return ATA_EH_CMD_DFL_TIMEOUT;
326
327         idx = ehc->cmd_timeout_idx[dev->devno][ent];
328         return ata_eh_cmd_timeout_table[ent].timeouts[idx];
329 }
330
331 /**
332  *      ata_internal_cmd_timed_out - notification for internal command timeout
333  *      @dev: target device
334  *      @cmd: internal command which timed out
335  *
336  *      Notify EH that internal command @cmd for @dev timed out.  This
337  *      function should be called only for commands whose timeouts are
338  *      determined using ata_internal_cmd_timeout().
339  *
340  *      LOCKING:
341  *      EH context.
342  */
343 void ata_internal_cmd_timed_out(struct ata_device *dev, u8 cmd)
344 {
345         struct ata_eh_context *ehc = &dev->link->eh_context;
346         int ent = ata_lookup_timeout_table(cmd);
347         int idx;
348
349         if (ent < 0)
350                 return;
351
352         idx = ehc->cmd_timeout_idx[dev->devno][ent];
353         if (ata_eh_cmd_timeout_table[ent].timeouts[idx + 1] != ULONG_MAX)
354                 ehc->cmd_timeout_idx[dev->devno][ent]++;
355 }
356
357 static void ata_ering_record(struct ata_ering *ering, unsigned int eflags,
358                              unsigned int err_mask)
359 {
360         struct ata_ering_entry *ent;
361
362         WARN_ON(!err_mask);
363
364         ering->cursor++;
365         ering->cursor %= ATA_ERING_SIZE;
366
367         ent = &ering->ring[ering->cursor];
368         ent->eflags = eflags;
369         ent->err_mask = err_mask;
370         ent->timestamp = get_jiffies_64();
371 }
372
373 static struct ata_ering_entry *ata_ering_top(struct ata_ering *ering)
374 {
375         struct ata_ering_entry *ent = &ering->ring[ering->cursor];
376
377         if (ent->err_mask)
378                 return ent;
379         return NULL;
380 }
381
382 int ata_ering_map(struct ata_ering *ering,
383                   int (*map_fn)(struct ata_ering_entry *, void *),
384                   void *arg)
385 {
386         int idx, rc = 0;
387         struct ata_ering_entry *ent;
388
389         idx = ering->cursor;
390         do {
391                 ent = &ering->ring[idx];
392                 if (!ent->err_mask)
393                         break;
394                 rc = map_fn(ent, arg);
395                 if (rc)
396                         break;
397                 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
398         } while (idx != ering->cursor);
399
400         return rc;
401 }
402
403 static int ata_ering_clear_cb(struct ata_ering_entry *ent, void *void_arg)
404 {
405         ent->eflags |= ATA_EFLAG_OLD_ER;
406         return 0;
407 }
408
409 static void ata_ering_clear(struct ata_ering *ering)
410 {
411         ata_ering_map(ering, ata_ering_clear_cb, NULL);
412 }
413
414 static unsigned int ata_eh_dev_action(struct ata_device *dev)
415 {
416         struct ata_eh_context *ehc = &dev->link->eh_context;
417
418         return ehc->i.action | ehc->i.dev_action[dev->devno];
419 }
420
421 static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
422                                 struct ata_eh_info *ehi, unsigned int action)
423 {
424         struct ata_device *tdev;
425
426         if (!dev) {
427                 ehi->action &= ~action;
428                 ata_for_each_dev(tdev, link, ALL)
429                         ehi->dev_action[tdev->devno] &= ~action;
430         } else {
431                 /* doesn't make sense for port-wide EH actions */
432                 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
433
434                 /* break ehi->action into ehi->dev_action */
435                 if (ehi->action & action) {
436                         ata_for_each_dev(tdev, link, ALL)
437                                 ehi->dev_action[tdev->devno] |=
438                                         ehi->action & action;
439                         ehi->action &= ~action;
440                 }
441
442                 /* turn off the specified per-dev action */
443                 ehi->dev_action[dev->devno] &= ~action;
444         }
445 }
446
447 /**
448  *      ata_eh_acquire - acquire EH ownership
449  *      @ap: ATA port to acquire EH ownership for
450  *
451  *      Acquire EH ownership for @ap.  This is the basic exclusion
452  *      mechanism for ports sharing a host.  Only one port hanging off
453  *      the same host can claim the ownership of EH.
454  *
455  *      LOCKING:
456  *      EH context.
457  */
458 void ata_eh_acquire(struct ata_port *ap)
459 {
460         mutex_lock(&ap->host->eh_mutex);
461         WARN_ON_ONCE(ap->host->eh_owner);
462         ap->host->eh_owner = current;
463 }
464
465 /**
466  *      ata_eh_release - release EH ownership
467  *      @ap: ATA port to release EH ownership for
468  *
469  *      Release EH ownership for @ap if the caller.  The caller must
470  *      have acquired EH ownership using ata_eh_acquire() previously.
471  *
472  *      LOCKING:
473  *      EH context.
474  */
475 void ata_eh_release(struct ata_port *ap)
476 {
477         WARN_ON_ONCE(ap->host->eh_owner != current);
478         ap->host->eh_owner = NULL;
479         mutex_unlock(&ap->host->eh_mutex);
480 }
481
482 static void ata_eh_unload(struct ata_port *ap)
483 {
484         struct ata_link *link;
485         struct ata_device *dev;
486         unsigned long flags;
487
488         /* Restore SControl IPM and SPD for the next driver and
489          * disable attached devices.
490          */
491         ata_for_each_link(link, ap, PMP_FIRST) {
492                 sata_scr_write(link, SCR_CONTROL, link->saved_scontrol & 0xff0);
493                 ata_for_each_dev(dev, link, ALL)
494                         ata_dev_disable(dev);
495         }
496
497         /* freeze and set UNLOADED */
498         spin_lock_irqsave(ap->lock, flags);
499
500         ata_port_freeze(ap);                    /* won't be thawed */
501         ap->pflags &= ~ATA_PFLAG_EH_PENDING;    /* clear pending from freeze */
502         ap->pflags |= ATA_PFLAG_UNLOADED;
503
504         spin_unlock_irqrestore(ap->lock, flags);
505 }
506
507 /**
508  *      ata_scsi_error - SCSI layer error handler callback
509  *      @host: SCSI host on which error occurred
510  *
511  *      Handles SCSI-layer-thrown error events.
512  *
513  *      LOCKING:
514  *      Inherited from SCSI layer (none, can sleep)
515  *
516  *      RETURNS:
517  *      Zero.
518  */
519 void ata_scsi_error(struct Scsi_Host *host)
520 {
521         struct ata_port *ap = ata_shost_to_port(host);
522         unsigned long flags;
523         LIST_HEAD(eh_work_q);
524
525         DPRINTK("ENTER\n");
526
527         spin_lock_irqsave(host->host_lock, flags);
528         list_splice_init(&host->eh_cmd_q, &eh_work_q);
529         spin_unlock_irqrestore(host->host_lock, flags);
530
531         ata_scsi_cmd_error_handler(host, ap, &eh_work_q);
532
533         /* If we timed raced normal completion and there is nothing to
534            recover nr_timedout == 0 why exactly are we doing error recovery ? */
535         ata_scsi_port_error_handler(host, ap);
536
537         /* finish or retry handled scmd's and clean up */
538         WARN_ON(!list_empty(&eh_work_q));
539
540         DPRINTK("EXIT\n");
541 }
542
543 /**
544  * ata_scsi_cmd_error_handler - error callback for a list of commands
545  * @host:       scsi host containing the port
546  * @ap:         ATA port within the host
547  * @eh_work_q:  list of commands to process
548  *
549  * process the given list of commands and return those finished to the
550  * ap->eh_done_q.  This function is the first part of the libata error
551  * handler which processes a given list of failed commands.
552  */
553 void ata_scsi_cmd_error_handler(struct Scsi_Host *host, struct ata_port *ap,
554                                 struct list_head *eh_work_q)
555 {
556         int i;
557         unsigned long flags;
558
559         /* make sure sff pio task is not running */
560         ata_sff_flush_pio_task(ap);
561
562         /* synchronize with host lock and sort out timeouts */
563
564         /* For new EH, all qcs are finished in one of three ways -
565          * normal completion, error completion, and SCSI timeout.
566          * Both completions can race against SCSI timeout.  When normal
567          * completion wins, the qc never reaches EH.  When error
568          * completion wins, the qc has ATA_QCFLAG_FAILED set.
569          *
570          * When SCSI timeout wins, things are a bit more complex.
571          * Normal or error completion can occur after the timeout but
572          * before this point.  In such cases, both types of
573          * completions are honored.  A scmd is determined to have
574          * timed out iff its associated qc is active and not failed.
575          */
576         spin_lock_irqsave(ap->lock, flags);
577         if (ap->ops->error_handler) {
578                 struct scsi_cmnd *scmd, *tmp;
579                 int nr_timedout = 0;
580
581                 /* This must occur under the ap->lock as we don't want
582                    a polled recovery to race the real interrupt handler
583
584                    The lost_interrupt handler checks for any completed but
585                    non-notified command and completes much like an IRQ handler.
586
587                    We then fall into the error recovery code which will treat
588                    this as if normal completion won the race */
589
590                 if (ap->ops->lost_interrupt)
591                         ap->ops->lost_interrupt(ap);
592
593                 list_for_each_entry_safe(scmd, tmp, eh_work_q, eh_entry) {
594                         struct ata_queued_cmd *qc;
595
596                         ata_qc_for_each_raw(ap, qc, i) {
597                                 if (qc->flags & ATA_QCFLAG_ACTIVE &&
598                                     qc->scsicmd == scmd)
599                                         break;
600                         }
601
602                         if (i < ATA_MAX_QUEUE) {
603                                 /* the scmd has an associated qc */
604                                 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
605                                         /* which hasn't failed yet, timeout */
606                                         qc->err_mask |= AC_ERR_TIMEOUT;
607                                         qc->flags |= ATA_QCFLAG_FAILED;
608                                         nr_timedout++;
609                                 }
610                         } else {
611                                 /* Normal completion occurred after
612                                  * SCSI timeout but before this point.
613                                  * Successfully complete it.
614                                  */
615                                 scmd->retries = scmd->allowed;
616                                 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
617                         }
618                 }
619
620                 /* If we have timed out qcs.  They belong to EH from
621                  * this point but the state of the controller is
622                  * unknown.  Freeze the port to make sure the IRQ
623                  * handler doesn't diddle with those qcs.  This must
624                  * be done atomically w.r.t. setting QCFLAG_FAILED.
625                  */
626                 if (nr_timedout)
627                         __ata_port_freeze(ap);
628
629
630                 /* initialize eh_tries */
631                 ap->eh_tries = ATA_EH_MAX_TRIES;
632         }
633         spin_unlock_irqrestore(ap->lock, flags);
634
635 }
636 EXPORT_SYMBOL(ata_scsi_cmd_error_handler);
637
638 /**
639  * ata_scsi_port_error_handler - recover the port after the commands
640  * @host:       SCSI host containing the port
641  * @ap:         the ATA port
642  *
643  * Handle the recovery of the port @ap after all the commands
644  * have been recovered.
645  */
646 void ata_scsi_port_error_handler(struct Scsi_Host *host, struct ata_port *ap)
647 {
648         unsigned long flags;
649
650         /* invoke error handler */
651         if (ap->ops->error_handler) {
652                 struct ata_link *link;
653
654                 /* acquire EH ownership */
655                 ata_eh_acquire(ap);
656  repeat:
657                 /* kill fast drain timer */
658                 del_timer_sync(&ap->fastdrain_timer);
659
660                 /* process port resume request */
661                 ata_eh_handle_port_resume(ap);
662
663                 /* fetch & clear EH info */
664                 spin_lock_irqsave(ap->lock, flags);
665
666                 ata_for_each_link(link, ap, HOST_FIRST) {
667                         struct ata_eh_context *ehc = &link->eh_context;
668                         struct ata_device *dev;
669
670                         memset(&link->eh_context, 0, sizeof(link->eh_context));
671                         link->eh_context.i = link->eh_info;
672                         memset(&link->eh_info, 0, sizeof(link->eh_info));
673
674                         ata_for_each_dev(dev, link, ENABLED) {
675                                 int devno = dev->devno;
676
677                                 ehc->saved_xfer_mode[devno] = dev->xfer_mode;
678                                 if (ata_ncq_enabled(dev))
679                                         ehc->saved_ncq_enabled |= 1 << devno;
680                         }
681                 }
682
683                 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
684                 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
685                 ap->excl_link = NULL;   /* don't maintain exclusion over EH */
686
687                 spin_unlock_irqrestore(ap->lock, flags);
688
689                 /* invoke EH, skip if unloading or suspended */
690                 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
691                         ap->ops->error_handler(ap);
692                 else {
693                         /* if unloading, commence suicide */
694                         if ((ap->pflags & ATA_PFLAG_UNLOADING) &&
695                             !(ap->pflags & ATA_PFLAG_UNLOADED))
696                                 ata_eh_unload(ap);
697                         ata_eh_finish(ap);
698                 }
699
700                 /* process port suspend request */
701                 ata_eh_handle_port_suspend(ap);
702
703                 /* Exception might have happened after ->error_handler
704                  * recovered the port but before this point.  Repeat
705                  * EH in such case.
706                  */
707                 spin_lock_irqsave(ap->lock, flags);
708
709                 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
710                         if (--ap->eh_tries) {
711                                 spin_unlock_irqrestore(ap->lock, flags);
712                                 goto repeat;
713                         }
714                         ata_port_err(ap,
715                                      "EH pending after %d tries, giving up\n",
716                                      ATA_EH_MAX_TRIES);
717                         ap->pflags &= ~ATA_PFLAG_EH_PENDING;
718                 }
719
720                 /* this run is complete, make sure EH info is clear */
721                 ata_for_each_link(link, ap, HOST_FIRST)
722                         memset(&link->eh_info, 0, sizeof(link->eh_info));
723
724                 /* end eh (clear host_eh_scheduled) while holding
725                  * ap->lock such that if exception occurs after this
726                  * point but before EH completion, SCSI midlayer will
727                  * re-initiate EH.
728                  */
729                 ap->ops->end_eh(ap);
730
731                 spin_unlock_irqrestore(ap->lock, flags);
732                 ata_eh_release(ap);
733         } else {
734                 WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
735                 ap->ops->eng_timeout(ap);
736         }
737
738         scsi_eh_flush_done_q(&ap->eh_done_q);
739
740         /* clean up */
741         spin_lock_irqsave(ap->lock, flags);
742
743         if (ap->pflags & ATA_PFLAG_LOADING)
744                 ap->pflags &= ~ATA_PFLAG_LOADING;
745         else if ((ap->pflags & ATA_PFLAG_SCSI_HOTPLUG) &&
746                 !(ap->flags & ATA_FLAG_SAS_HOST))
747                 schedule_delayed_work(&ap->hotplug_task, 0);
748
749         if (ap->pflags & ATA_PFLAG_RECOVERED)
750                 ata_port_info(ap, "EH complete\n");
751
752         ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
753
754         /* tell wait_eh that we're done */
755         ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
756         wake_up_all(&ap->eh_wait_q);
757
758         spin_unlock_irqrestore(ap->lock, flags);
759 }
760 EXPORT_SYMBOL_GPL(ata_scsi_port_error_handler);
761
762 /**
763  *      ata_port_wait_eh - Wait for the currently pending EH to complete
764  *      @ap: Port to wait EH for
765  *
766  *      Wait until the currently pending EH is complete.
767  *
768  *      LOCKING:
769  *      Kernel thread context (may sleep).
770  */
771 void ata_port_wait_eh(struct ata_port *ap)
772 {
773         unsigned long flags;
774         DEFINE_WAIT(wait);
775
776  retry:
777         spin_lock_irqsave(ap->lock, flags);
778
779         while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
780                 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
781                 spin_unlock_irqrestore(ap->lock, flags);
782                 schedule();
783                 spin_lock_irqsave(ap->lock, flags);
784         }
785         finish_wait(&ap->eh_wait_q, &wait);
786
787         spin_unlock_irqrestore(ap->lock, flags);
788
789         /* make sure SCSI EH is complete */
790         if (scsi_host_in_recovery(ap->scsi_host)) {
791                 ata_msleep(ap, 10);
792                 goto retry;
793         }
794 }
795 EXPORT_SYMBOL_GPL(ata_port_wait_eh);
796
797 static int ata_eh_nr_in_flight(struct ata_port *ap)
798 {
799         struct ata_queued_cmd *qc;
800         unsigned int tag;
801         int nr = 0;
802
803         /* count only non-internal commands */
804         ata_qc_for_each(ap, qc, tag) {
805                 if (qc)
806                         nr++;
807         }
808
809         return nr;
810 }
811
812 void ata_eh_fastdrain_timerfn(struct timer_list *t)
813 {
814         struct ata_port *ap = from_timer(ap, t, fastdrain_timer);
815         unsigned long flags;
816         int cnt;
817
818         spin_lock_irqsave(ap->lock, flags);
819
820         cnt = ata_eh_nr_in_flight(ap);
821
822         /* are we done? */
823         if (!cnt)
824                 goto out_unlock;
825
826         if (cnt == ap->fastdrain_cnt) {
827                 struct ata_queued_cmd *qc;
828                 unsigned int tag;
829
830                 /* No progress during the last interval, tag all
831                  * in-flight qcs as timed out and freeze the port.
832                  */
833                 ata_qc_for_each(ap, qc, tag) {
834                         if (qc)
835                                 qc->err_mask |= AC_ERR_TIMEOUT;
836                 }
837
838                 ata_port_freeze(ap);
839         } else {
840                 /* some qcs have finished, give it another chance */
841                 ap->fastdrain_cnt = cnt;
842                 ap->fastdrain_timer.expires =
843                         ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
844                 add_timer(&ap->fastdrain_timer);
845         }
846
847  out_unlock:
848         spin_unlock_irqrestore(ap->lock, flags);
849 }
850
851 /**
852  *      ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
853  *      @ap: target ATA port
854  *      @fastdrain: activate fast drain
855  *
856  *      Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
857  *      is non-zero and EH wasn't pending before.  Fast drain ensures
858  *      that EH kicks in in timely manner.
859  *
860  *      LOCKING:
861  *      spin_lock_irqsave(host lock)
862  */
863 static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
864 {
865         int cnt;
866
867         /* already scheduled? */
868         if (ap->pflags & ATA_PFLAG_EH_PENDING)
869                 return;
870
871         ap->pflags |= ATA_PFLAG_EH_PENDING;
872
873         if (!fastdrain)
874                 return;
875
876         /* do we have in-flight qcs? */
877         cnt = ata_eh_nr_in_flight(ap);
878         if (!cnt)
879                 return;
880
881         /* activate fast drain */
882         ap->fastdrain_cnt = cnt;
883         ap->fastdrain_timer.expires =
884                 ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
885         add_timer(&ap->fastdrain_timer);
886 }
887
888 /**
889  *      ata_qc_schedule_eh - schedule qc for error handling
890  *      @qc: command to schedule error handling for
891  *
892  *      Schedule error handling for @qc.  EH will kick in as soon as
893  *      other commands are drained.
894  *
895  *      LOCKING:
896  *      spin_lock_irqsave(host lock)
897  */
898 void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
899 {
900         struct ata_port *ap = qc->ap;
901
902         WARN_ON(!ap->ops->error_handler);
903
904         qc->flags |= ATA_QCFLAG_FAILED;
905         ata_eh_set_pending(ap, 1);
906
907         /* The following will fail if timeout has already expired.
908          * ata_scsi_error() takes care of such scmds on EH entry.
909          * Note that ATA_QCFLAG_FAILED is unconditionally set after
910          * this function completes.
911          */
912         blk_abort_request(qc->scsicmd->request);
913 }
914
915 /**
916  * ata_std_sched_eh - non-libsas ata_ports issue eh with this common routine
917  * @ap: ATA port to schedule EH for
918  *
919  *      LOCKING: inherited from ata_port_schedule_eh
920  *      spin_lock_irqsave(host lock)
921  */
922 void ata_std_sched_eh(struct ata_port *ap)
923 {
924         WARN_ON(!ap->ops->error_handler);
925
926         if (ap->pflags & ATA_PFLAG_INITIALIZING)
927                 return;
928
929         ata_eh_set_pending(ap, 1);
930         scsi_schedule_eh(ap->scsi_host);
931
932         DPRINTK("port EH scheduled\n");
933 }
934 EXPORT_SYMBOL_GPL(ata_std_sched_eh);
935
936 /**
937  * ata_std_end_eh - non-libsas ata_ports complete eh with this common routine
938  * @ap: ATA port to end EH for
939  *
940  * In the libata object model there is a 1:1 mapping of ata_port to
941  * shost, so host fields can be directly manipulated under ap->lock, in
942  * the libsas case we need to hold a lock at the ha->level to coordinate
943  * these events.
944  *
945  *      LOCKING:
946  *      spin_lock_irqsave(host lock)
947  */
948 void ata_std_end_eh(struct ata_port *ap)
949 {
950         struct Scsi_Host *host = ap->scsi_host;
951
952         host->host_eh_scheduled = 0;
953 }
954 EXPORT_SYMBOL(ata_std_end_eh);
955
956
957 /**
958  *      ata_port_schedule_eh - schedule error handling without a qc
959  *      @ap: ATA port to schedule EH for
960  *
961  *      Schedule error handling for @ap.  EH will kick in as soon as
962  *      all commands are drained.
963  *
964  *      LOCKING:
965  *      spin_lock_irqsave(host lock)
966  */
967 void ata_port_schedule_eh(struct ata_port *ap)
968 {
969         /* see: ata_std_sched_eh, unless you know better */
970         ap->ops->sched_eh(ap);
971 }
972
973 static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
974 {
975         struct ata_queued_cmd *qc;
976         int tag, nr_aborted = 0;
977
978         WARN_ON(!ap->ops->error_handler);
979
980         /* we're gonna abort all commands, no need for fast drain */
981         ata_eh_set_pending(ap, 0);
982
983         /* include internal tag in iteration */
984         ata_qc_for_each_with_internal(ap, qc, tag) {
985                 if (qc && (!link || qc->dev->link == link)) {
986                         qc->flags |= ATA_QCFLAG_FAILED;
987                         ata_qc_complete(qc);
988                         nr_aborted++;
989                 }
990         }
991
992         if (!nr_aborted)
993                 ata_port_schedule_eh(ap);
994
995         return nr_aborted;
996 }
997
998 /**
999  *      ata_link_abort - abort all qc's on the link
1000  *      @link: ATA link to abort qc's for
1001  *
1002  *      Abort all active qc's active on @link and schedule EH.
1003  *
1004  *      LOCKING:
1005  *      spin_lock_irqsave(host lock)
1006  *
1007  *      RETURNS:
1008  *      Number of aborted qc's.
1009  */
1010 int ata_link_abort(struct ata_link *link)
1011 {
1012         return ata_do_link_abort(link->ap, link);
1013 }
1014
1015 /**
1016  *      ata_port_abort - abort all qc's on the port
1017  *      @ap: ATA port to abort qc's for
1018  *
1019  *      Abort all active qc's of @ap and schedule EH.
1020  *
1021  *      LOCKING:
1022  *      spin_lock_irqsave(host_set lock)
1023  *
1024  *      RETURNS:
1025  *      Number of aborted qc's.
1026  */
1027 int ata_port_abort(struct ata_port *ap)
1028 {
1029         return ata_do_link_abort(ap, NULL);
1030 }
1031
1032 /**
1033  *      __ata_port_freeze - freeze port
1034  *      @ap: ATA port to freeze
1035  *
1036  *      This function is called when HSM violation or some other
1037  *      condition disrupts normal operation of the port.  Frozen port
1038  *      is not allowed to perform any operation until the port is
1039  *      thawed, which usually follows a successful reset.
1040  *
1041  *      ap->ops->freeze() callback can be used for freezing the port
1042  *      hardware-wise (e.g. mask interrupt and stop DMA engine).  If a
1043  *      port cannot be frozen hardware-wise, the interrupt handler
1044  *      must ack and clear interrupts unconditionally while the port
1045  *      is frozen.
1046  *
1047  *      LOCKING:
1048  *      spin_lock_irqsave(host lock)
1049  */
1050 static void __ata_port_freeze(struct ata_port *ap)
1051 {
1052         WARN_ON(!ap->ops->error_handler);
1053
1054         if (ap->ops->freeze)
1055                 ap->ops->freeze(ap);
1056
1057         ap->pflags |= ATA_PFLAG_FROZEN;
1058
1059         DPRINTK("ata%u port frozen\n", ap->print_id);
1060 }
1061
1062 /**
1063  *      ata_port_freeze - abort & freeze port
1064  *      @ap: ATA port to freeze
1065  *
1066  *      Abort and freeze @ap.  The freeze operation must be called
1067  *      first, because some hardware requires special operations
1068  *      before the taskfile registers are accessible.
1069  *
1070  *      LOCKING:
1071  *      spin_lock_irqsave(host lock)
1072  *
1073  *      RETURNS:
1074  *      Number of aborted commands.
1075  */
1076 int ata_port_freeze(struct ata_port *ap)
1077 {
1078         int nr_aborted;
1079
1080         WARN_ON(!ap->ops->error_handler);
1081
1082         __ata_port_freeze(ap);
1083         nr_aborted = ata_port_abort(ap);
1084
1085         return nr_aborted;
1086 }
1087
1088 /**
1089  *      sata_async_notification - SATA async notification handler
1090  *      @ap: ATA port where async notification is received
1091  *
1092  *      Handler to be called when async notification via SDB FIS is
1093  *      received.  This function schedules EH if necessary.
1094  *
1095  *      LOCKING:
1096  *      spin_lock_irqsave(host lock)
1097  *
1098  *      RETURNS:
1099  *      1 if EH is scheduled, 0 otherwise.
1100  */
1101 int sata_async_notification(struct ata_port *ap)
1102 {
1103         u32 sntf;
1104         int rc;
1105
1106         if (!(ap->flags & ATA_FLAG_AN))
1107                 return 0;
1108
1109         rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
1110         if (rc == 0)
1111                 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
1112
1113         if (!sata_pmp_attached(ap) || rc) {
1114                 /* PMP is not attached or SNTF is not available */
1115                 if (!sata_pmp_attached(ap)) {
1116                         /* PMP is not attached.  Check whether ATAPI
1117                          * AN is configured.  If so, notify media
1118                          * change.
1119                          */
1120                         struct ata_device *dev = ap->link.device;
1121
1122                         if ((dev->class == ATA_DEV_ATAPI) &&
1123                             (dev->flags & ATA_DFLAG_AN))
1124                                 ata_scsi_media_change_notify(dev);
1125                         return 0;
1126                 } else {
1127                         /* PMP is attached but SNTF is not available.
1128                          * ATAPI async media change notification is
1129                          * not used.  The PMP must be reporting PHY
1130                          * status change, schedule EH.
1131                          */
1132                         ata_port_schedule_eh(ap);
1133                         return 1;
1134                 }
1135         } else {
1136                 /* PMP is attached and SNTF is available */
1137                 struct ata_link *link;
1138
1139                 /* check and notify ATAPI AN */
1140                 ata_for_each_link(link, ap, EDGE) {
1141                         if (!(sntf & (1 << link->pmp)))
1142                                 continue;
1143
1144                         if ((link->device->class == ATA_DEV_ATAPI) &&
1145                             (link->device->flags & ATA_DFLAG_AN))
1146                                 ata_scsi_media_change_notify(link->device);
1147                 }
1148
1149                 /* If PMP is reporting that PHY status of some
1150                  * downstream ports has changed, schedule EH.
1151                  */
1152                 if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
1153                         ata_port_schedule_eh(ap);
1154                         return 1;
1155                 }
1156
1157                 return 0;
1158         }
1159 }
1160
1161 /**
1162  *      ata_eh_freeze_port - EH helper to freeze port
1163  *      @ap: ATA port to freeze
1164  *
1165  *      Freeze @ap.
1166  *
1167  *      LOCKING:
1168  *      None.
1169  */
1170 void ata_eh_freeze_port(struct ata_port *ap)
1171 {
1172         unsigned long flags;
1173
1174         if (!ap->ops->error_handler)
1175                 return;
1176
1177         spin_lock_irqsave(ap->lock, flags);
1178         __ata_port_freeze(ap);
1179         spin_unlock_irqrestore(ap->lock, flags);
1180 }
1181
1182 /**
1183  *      ata_port_thaw_port - EH helper to thaw port
1184  *      @ap: ATA port to thaw
1185  *
1186  *      Thaw frozen port @ap.
1187  *
1188  *      LOCKING:
1189  *      None.
1190  */
1191 void ata_eh_thaw_port(struct ata_port *ap)
1192 {
1193         unsigned long flags;
1194
1195         if (!ap->ops->error_handler)
1196                 return;
1197
1198         spin_lock_irqsave(ap->lock, flags);
1199
1200         ap->pflags &= ~ATA_PFLAG_FROZEN;
1201
1202         if (ap->ops->thaw)
1203                 ap->ops->thaw(ap);
1204
1205         spin_unlock_irqrestore(ap->lock, flags);
1206
1207         DPRINTK("ata%u port thawed\n", ap->print_id);
1208 }
1209
1210 static void ata_eh_scsidone(struct scsi_cmnd *scmd)
1211 {
1212         /* nada */
1213 }
1214
1215 static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
1216 {
1217         struct ata_port *ap = qc->ap;
1218         struct scsi_cmnd *scmd = qc->scsicmd;
1219         unsigned long flags;
1220
1221         spin_lock_irqsave(ap->lock, flags);
1222         qc->scsidone = ata_eh_scsidone;
1223         __ata_qc_complete(qc);
1224         WARN_ON(ata_tag_valid(qc->tag));
1225         spin_unlock_irqrestore(ap->lock, flags);
1226
1227         scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
1228 }
1229
1230 /**
1231  *      ata_eh_qc_complete - Complete an active ATA command from EH
1232  *      @qc: Command to complete
1233  *
1234  *      Indicate to the mid and upper layers that an ATA command has
1235  *      completed.  To be used from EH.
1236  */
1237 void ata_eh_qc_complete(struct ata_queued_cmd *qc)
1238 {
1239         struct scsi_cmnd *scmd = qc->scsicmd;
1240         scmd->retries = scmd->allowed;
1241         __ata_eh_qc_complete(qc);
1242 }
1243
1244 /**
1245  *      ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
1246  *      @qc: Command to retry
1247  *
1248  *      Indicate to the mid and upper layers that an ATA command
1249  *      should be retried.  To be used from EH.
1250  *
1251  *      SCSI midlayer limits the number of retries to scmd->allowed.
1252  *      scmd->allowed is incremented for commands which get retried
1253  *      due to unrelated failures (qc->err_mask is zero).
1254  */
1255 void ata_eh_qc_retry(struct ata_queued_cmd *qc)
1256 {
1257         struct scsi_cmnd *scmd = qc->scsicmd;
1258         if (!qc->err_mask)
1259                 scmd->allowed++;
1260         __ata_eh_qc_complete(qc);
1261 }
1262
1263 /**
1264  *      ata_dev_disable - disable ATA device
1265  *      @dev: ATA device to disable
1266  *
1267  *      Disable @dev.
1268  *
1269  *      Locking:
1270  *      EH context.
1271  */
1272 void ata_dev_disable(struct ata_device *dev)
1273 {
1274         if (!ata_dev_enabled(dev))
1275                 return;
1276
1277         if (ata_msg_drv(dev->link->ap))
1278                 ata_dev_warn(dev, "disabled\n");
1279         ata_acpi_on_disable(dev);
1280         ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 | ATA_DNXFER_QUIET);
1281         dev->class++;
1282
1283         /* From now till the next successful probe, ering is used to
1284          * track probe failures.  Clear accumulated device error info.
1285          */
1286         ata_ering_clear(&dev->ering);
1287 }
1288
1289 /**
1290  *      ata_eh_detach_dev - detach ATA device
1291  *      @dev: ATA device to detach
1292  *
1293  *      Detach @dev.
1294  *
1295  *      LOCKING:
1296  *      None.
1297  */
1298 void ata_eh_detach_dev(struct ata_device *dev)
1299 {
1300         struct ata_link *link = dev->link;
1301         struct ata_port *ap = link->ap;
1302         struct ata_eh_context *ehc = &link->eh_context;
1303         unsigned long flags;
1304
1305         ata_dev_disable(dev);
1306
1307         spin_lock_irqsave(ap->lock, flags);
1308
1309         dev->flags &= ~ATA_DFLAG_DETACH;
1310
1311         if (ata_scsi_offline_dev(dev)) {
1312                 dev->flags |= ATA_DFLAG_DETACHED;
1313                 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
1314         }
1315
1316         /* clear per-dev EH info */
1317         ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
1318         ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
1319         ehc->saved_xfer_mode[dev->devno] = 0;
1320         ehc->saved_ncq_enabled &= ~(1 << dev->devno);
1321
1322         spin_unlock_irqrestore(ap->lock, flags);
1323 }
1324
1325 /**
1326  *      ata_eh_about_to_do - about to perform eh_action
1327  *      @link: target ATA link
1328  *      @dev: target ATA dev for per-dev action (can be NULL)
1329  *      @action: action about to be performed
1330  *
1331  *      Called just before performing EH actions to clear related bits
1332  *      in @link->eh_info such that eh actions are not unnecessarily
1333  *      repeated.
1334  *
1335  *      LOCKING:
1336  *      None.
1337  */
1338 void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
1339                         unsigned int action)
1340 {
1341         struct ata_port *ap = link->ap;
1342         struct ata_eh_info *ehi = &link->eh_info;
1343         struct ata_eh_context *ehc = &link->eh_context;
1344         unsigned long flags;
1345
1346         spin_lock_irqsave(ap->lock, flags);
1347
1348         ata_eh_clear_action(link, dev, ehi, action);
1349
1350         /* About to take EH action, set RECOVERED.  Ignore actions on
1351          * slave links as master will do them again.
1352          */
1353         if (!(ehc->i.flags & ATA_EHI_QUIET) && link != ap->slave_link)
1354                 ap->pflags |= ATA_PFLAG_RECOVERED;
1355
1356         spin_unlock_irqrestore(ap->lock, flags);
1357 }
1358
1359 /**
1360  *      ata_eh_done - EH action complete
1361  *      @link: ATA link for which EH actions are complete
1362  *      @dev: target ATA dev for per-dev action (can be NULL)
1363  *      @action: action just completed
1364  *
1365  *      Called right after performing EH actions to clear related bits
1366  *      in @link->eh_context.
1367  *
1368  *      LOCKING:
1369  *      None.
1370  */
1371 void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1372                  unsigned int action)
1373 {
1374         struct ata_eh_context *ehc = &link->eh_context;
1375
1376         ata_eh_clear_action(link, dev, &ehc->i, action);
1377 }
1378
1379 /**
1380  *      ata_err_string - convert err_mask to descriptive string
1381  *      @err_mask: error mask to convert to string
1382  *
1383  *      Convert @err_mask to descriptive string.  Errors are
1384  *      prioritized according to severity and only the most severe
1385  *      error is reported.
1386  *
1387  *      LOCKING:
1388  *      None.
1389  *
1390  *      RETURNS:
1391  *      Descriptive string for @err_mask
1392  */
1393 static const char *ata_err_string(unsigned int err_mask)
1394 {
1395         if (err_mask & AC_ERR_HOST_BUS)
1396                 return "host bus error";
1397         if (err_mask & AC_ERR_ATA_BUS)
1398                 return "ATA bus error";
1399         if (err_mask & AC_ERR_TIMEOUT)
1400                 return "timeout";
1401         if (err_mask & AC_ERR_HSM)
1402                 return "HSM violation";
1403         if (err_mask & AC_ERR_SYSTEM)
1404                 return "internal error";
1405         if (err_mask & AC_ERR_MEDIA)
1406                 return "media error";
1407         if (err_mask & AC_ERR_INVALID)
1408                 return "invalid argument";
1409         if (err_mask & AC_ERR_DEV)
1410                 return "device error";
1411         if (err_mask & AC_ERR_NCQ)
1412                 return "NCQ error";
1413         if (err_mask & AC_ERR_NODEV_HINT)
1414                 return "Polling detection error";
1415         return "unknown error";
1416 }
1417
1418 /**
1419  *      ata_eh_read_log_10h - Read log page 10h for NCQ error details
1420  *      @dev: Device to read log page 10h from
1421  *      @tag: Resulting tag of the failed command
1422  *      @tf: Resulting taskfile registers of the failed command
1423  *
1424  *      Read log page 10h to obtain NCQ error details and clear error
1425  *      condition.
1426  *
1427  *      LOCKING:
1428  *      Kernel thread context (may sleep).
1429  *
1430  *      RETURNS:
1431  *      0 on success, -errno otherwise.
1432  */
1433 static int ata_eh_read_log_10h(struct ata_device *dev,
1434                                int *tag, struct ata_taskfile *tf)
1435 {
1436         u8 *buf = dev->link->ap->sector_buf;
1437         unsigned int err_mask;
1438         u8 csum;
1439         int i;
1440
1441         err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, 0, buf, 1);
1442         if (err_mask)
1443                 return -EIO;
1444
1445         csum = 0;
1446         for (i = 0; i < ATA_SECT_SIZE; i++)
1447                 csum += buf[i];
1448         if (csum)
1449                 ata_dev_warn(dev, "invalid checksum 0x%x on log page 10h\n",
1450                              csum);
1451
1452         if (buf[0] & 0x80)
1453                 return -ENOENT;
1454
1455         *tag = buf[0] & 0x1f;
1456
1457         tf->command = buf[2];
1458         tf->feature = buf[3];
1459         tf->lbal = buf[4];
1460         tf->lbam = buf[5];
1461         tf->lbah = buf[6];
1462         tf->device = buf[7];
1463         tf->hob_lbal = buf[8];
1464         tf->hob_lbam = buf[9];
1465         tf->hob_lbah = buf[10];
1466         tf->nsect = buf[12];
1467         tf->hob_nsect = buf[13];
1468         if (dev->class == ATA_DEV_ZAC && ata_id_has_ncq_autosense(dev->id))
1469                 tf->auxiliary = buf[14] << 16 | buf[15] << 8 | buf[16];
1470
1471         return 0;
1472 }
1473
1474 /**
1475  *      atapi_eh_tur - perform ATAPI TEST_UNIT_READY
1476  *      @dev: target ATAPI device
1477  *      @r_sense_key: out parameter for sense_key
1478  *
1479  *      Perform ATAPI TEST_UNIT_READY.
1480  *
1481  *      LOCKING:
1482  *      EH context (may sleep).
1483  *
1484  *      RETURNS:
1485  *      0 on success, AC_ERR_* mask on failure.
1486  */
1487 unsigned int atapi_eh_tur(struct ata_device *dev, u8 *r_sense_key)
1488 {
1489         u8 cdb[ATAPI_CDB_LEN] = { TEST_UNIT_READY, 0, 0, 0, 0, 0 };
1490         struct ata_taskfile tf;
1491         unsigned int err_mask;
1492
1493         ata_tf_init(dev, &tf);
1494
1495         tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1496         tf.command = ATA_CMD_PACKET;
1497         tf.protocol = ATAPI_PROT_NODATA;
1498
1499         err_mask = ata_exec_internal(dev, &tf, cdb, DMA_NONE, NULL, 0, 0);
1500         if (err_mask == AC_ERR_DEV)
1501                 *r_sense_key = tf.feature >> 4;
1502         return err_mask;
1503 }
1504
1505 /**
1506  *      ata_eh_request_sense - perform REQUEST_SENSE_DATA_EXT
1507  *      @qc: qc to perform REQUEST_SENSE_SENSE_DATA_EXT to
1508  *      @cmd: scsi command for which the sense code should be set
1509  *
1510  *      Perform REQUEST_SENSE_DATA_EXT after the device reported CHECK
1511  *      SENSE.  This function is an EH helper.
1512  *
1513  *      LOCKING:
1514  *      Kernel thread context (may sleep).
1515  */
1516 static void ata_eh_request_sense(struct ata_queued_cmd *qc,
1517                                  struct scsi_cmnd *cmd)
1518 {
1519         struct ata_device *dev = qc->dev;
1520         struct ata_taskfile tf;
1521         unsigned int err_mask;
1522
1523         if (qc->ap->pflags & ATA_PFLAG_FROZEN) {
1524                 ata_dev_warn(dev, "sense data available but port frozen\n");
1525                 return;
1526         }
1527
1528         if (!cmd || qc->flags & ATA_QCFLAG_SENSE_VALID)
1529                 return;
1530
1531         if (!ata_id_sense_reporting_enabled(dev->id)) {
1532                 ata_dev_warn(qc->dev, "sense data reporting disabled\n");
1533                 return;
1534         }
1535
1536         DPRINTK("ATA request sense\n");
1537
1538         ata_tf_init(dev, &tf);
1539         tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1540         tf.flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
1541         tf.command = ATA_CMD_REQ_SENSE_DATA;
1542         tf.protocol = ATA_PROT_NODATA;
1543
1544         err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1545         /* Ignore err_mask; ATA_ERR might be set */
1546         if (tf.command & ATA_SENSE) {
1547                 ata_scsi_set_sense(dev, cmd, tf.lbah, tf.lbam, tf.lbal);
1548                 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1549         } else {
1550                 ata_dev_warn(dev, "request sense failed stat %02x emask %x\n",
1551                              tf.command, err_mask);
1552         }
1553 }
1554
1555 /**
1556  *      atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1557  *      @dev: device to perform REQUEST_SENSE to
1558  *      @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
1559  *      @dfl_sense_key: default sense key to use
1560  *
1561  *      Perform ATAPI REQUEST_SENSE after the device reported CHECK
1562  *      SENSE.  This function is EH helper.
1563  *
1564  *      LOCKING:
1565  *      Kernel thread context (may sleep).
1566  *
1567  *      RETURNS:
1568  *      0 on success, AC_ERR_* mask on failure
1569  */
1570 unsigned int atapi_eh_request_sense(struct ata_device *dev,
1571                                            u8 *sense_buf, u8 dfl_sense_key)
1572 {
1573         u8 cdb[ATAPI_CDB_LEN] =
1574                 { REQUEST_SENSE, 0, 0, 0, SCSI_SENSE_BUFFERSIZE, 0 };
1575         struct ata_port *ap = dev->link->ap;
1576         struct ata_taskfile tf;
1577
1578         DPRINTK("ATAPI request sense\n");
1579
1580         memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1581
1582         /* initialize sense_buf with the error register,
1583          * for the case where they are -not- overwritten
1584          */
1585         sense_buf[0] = 0x70;
1586         sense_buf[2] = dfl_sense_key;
1587
1588         /* some devices time out if garbage left in tf */
1589         ata_tf_init(dev, &tf);
1590
1591         tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1592         tf.command = ATA_CMD_PACKET;
1593
1594         /* is it pointless to prefer PIO for "safety reasons"? */
1595         if (ap->flags & ATA_FLAG_PIO_DMA) {
1596                 tf.protocol = ATAPI_PROT_DMA;
1597                 tf.feature |= ATAPI_PKT_DMA;
1598         } else {
1599                 tf.protocol = ATAPI_PROT_PIO;
1600                 tf.lbam = SCSI_SENSE_BUFFERSIZE;
1601                 tf.lbah = 0;
1602         }
1603
1604         return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
1605                                  sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
1606 }
1607
1608 /**
1609  *      ata_eh_analyze_serror - analyze SError for a failed port
1610  *      @link: ATA link to analyze SError for
1611  *
1612  *      Analyze SError if available and further determine cause of
1613  *      failure.
1614  *
1615  *      LOCKING:
1616  *      None.
1617  */
1618 static void ata_eh_analyze_serror(struct ata_link *link)
1619 {
1620         struct ata_eh_context *ehc = &link->eh_context;
1621         u32 serror = ehc->i.serror;
1622         unsigned int err_mask = 0, action = 0;
1623         u32 hotplug_mask;
1624
1625         if (serror & (SERR_PERSISTENT | SERR_DATA)) {
1626                 err_mask |= AC_ERR_ATA_BUS;
1627                 action |= ATA_EH_RESET;
1628         }
1629         if (serror & SERR_PROTOCOL) {
1630                 err_mask |= AC_ERR_HSM;
1631                 action |= ATA_EH_RESET;
1632         }
1633         if (serror & SERR_INTERNAL) {
1634                 err_mask |= AC_ERR_SYSTEM;
1635                 action |= ATA_EH_RESET;
1636         }
1637
1638         /* Determine whether a hotplug event has occurred.  Both
1639          * SError.N/X are considered hotplug events for enabled or
1640          * host links.  For disabled PMP links, only N bit is
1641          * considered as X bit is left at 1 for link plugging.
1642          */
1643         if (link->lpm_policy > ATA_LPM_MAX_POWER)
1644                 hotplug_mask = 0;       /* hotplug doesn't work w/ LPM */
1645         else if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
1646                 hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
1647         else
1648                 hotplug_mask = SERR_PHYRDY_CHG;
1649
1650         if (serror & hotplug_mask)
1651                 ata_ehi_hotplugged(&ehc->i);
1652
1653         ehc->i.err_mask |= err_mask;
1654         ehc->i.action |= action;
1655 }
1656
1657 /**
1658  *      ata_eh_analyze_ncq_error - analyze NCQ error
1659  *      @link: ATA link to analyze NCQ error for
1660  *
1661  *      Read log page 10h, determine the offending qc and acquire
1662  *      error status TF.  For NCQ device errors, all LLDDs have to do
1663  *      is setting AC_ERR_DEV in ehi->err_mask.  This function takes
1664  *      care of the rest.
1665  *
1666  *      LOCKING:
1667  *      Kernel thread context (may sleep).
1668  */
1669 void ata_eh_analyze_ncq_error(struct ata_link *link)
1670 {
1671         struct ata_port *ap = link->ap;
1672         struct ata_eh_context *ehc = &link->eh_context;
1673         struct ata_device *dev = link->device;
1674         struct ata_queued_cmd *qc;
1675         struct ata_taskfile tf;
1676         int tag, rc;
1677
1678         /* if frozen, we can't do much */
1679         if (ap->pflags & ATA_PFLAG_FROZEN)
1680                 return;
1681
1682         /* is it NCQ device error? */
1683         if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1684                 return;
1685
1686         /* has LLDD analyzed already? */
1687         ata_qc_for_each_raw(ap, qc, tag) {
1688                 if (!(qc->flags & ATA_QCFLAG_FAILED))
1689                         continue;
1690
1691                 if (qc->err_mask)
1692                         return;
1693         }
1694
1695         /* okay, this error is ours */
1696         memset(&tf, 0, sizeof(tf));
1697         rc = ata_eh_read_log_10h(dev, &tag, &tf);
1698         if (rc) {
1699                 ata_link_err(link, "failed to read log page 10h (errno=%d)\n",
1700                              rc);
1701                 return;
1702         }
1703
1704         if (!(link->sactive & (1 << tag))) {
1705                 ata_link_err(link, "log page 10h reported inactive tag %d\n",
1706                              tag);
1707                 return;
1708         }
1709
1710         /* we've got the perpetrator, condemn it */
1711         qc = __ata_qc_from_tag(ap, tag);
1712         memcpy(&qc->result_tf, &tf, sizeof(tf));
1713         qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
1714         qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
1715         if (dev->class == ATA_DEV_ZAC &&
1716             ((qc->result_tf.command & ATA_SENSE) || qc->result_tf.auxiliary)) {
1717                 char sense_key, asc, ascq;
1718
1719                 sense_key = (qc->result_tf.auxiliary >> 16) & 0xff;
1720                 asc = (qc->result_tf.auxiliary >> 8) & 0xff;
1721                 ascq = qc->result_tf.auxiliary & 0xff;
1722                 ata_scsi_set_sense(dev, qc->scsicmd, sense_key, asc, ascq);
1723                 ata_scsi_set_sense_information(dev, qc->scsicmd,
1724                                                &qc->result_tf);
1725                 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1726         }
1727
1728         ehc->i.err_mask &= ~AC_ERR_DEV;
1729 }
1730
1731 /**
1732  *      ata_eh_analyze_tf - analyze taskfile of a failed qc
1733  *      @qc: qc to analyze
1734  *      @tf: Taskfile registers to analyze
1735  *
1736  *      Analyze taskfile of @qc and further determine cause of
1737  *      failure.  This function also requests ATAPI sense data if
1738  *      available.
1739  *
1740  *      LOCKING:
1741  *      Kernel thread context (may sleep).
1742  *
1743  *      RETURNS:
1744  *      Determined recovery action
1745  */
1746 static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1747                                       const struct ata_taskfile *tf)
1748 {
1749         unsigned int tmp, action = 0;
1750         u8 stat = tf->command, err = tf->feature;
1751
1752         if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1753                 qc->err_mask |= AC_ERR_HSM;
1754                 return ATA_EH_RESET;
1755         }
1756
1757         if (stat & (ATA_ERR | ATA_DF)) {
1758                 qc->err_mask |= AC_ERR_DEV;
1759                 /*
1760                  * Sense data reporting does not work if the
1761                  * device fault bit is set.
1762                  */
1763                 if (stat & ATA_DF)
1764                         stat &= ~ATA_SENSE;
1765         } else {
1766                 return 0;
1767         }
1768
1769         switch (qc->dev->class) {
1770         case ATA_DEV_ZAC:
1771                 if (stat & ATA_SENSE)
1772                         ata_eh_request_sense(qc, qc->scsicmd);
1773                 /* fall through */
1774         case ATA_DEV_ATA:
1775                 if (err & ATA_ICRC)
1776                         qc->err_mask |= AC_ERR_ATA_BUS;
1777                 if (err & (ATA_UNC | ATA_AMNF))
1778                         qc->err_mask |= AC_ERR_MEDIA;
1779                 if (err & ATA_IDNF)
1780                         qc->err_mask |= AC_ERR_INVALID;
1781                 break;
1782
1783         case ATA_DEV_ATAPI:
1784                 if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
1785                         tmp = atapi_eh_request_sense(qc->dev,
1786                                                 qc->scsicmd->sense_buffer,
1787                                                 qc->result_tf.feature >> 4);
1788                         if (!tmp)
1789                                 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1790                         else
1791                                 qc->err_mask |= tmp;
1792                 }
1793         }
1794
1795         if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1796                 int ret = scsi_check_sense(qc->scsicmd);
1797                 /*
1798                  * SUCCESS here means that the sense code could be
1799                  * evaluated and should be passed to the upper layers
1800                  * for correct evaluation.
1801                  * FAILED means the sense code could not be interpreted
1802                  * and the device would need to be reset.
1803                  * NEEDS_RETRY and ADD_TO_MLQUEUE means that the
1804                  * command would need to be retried.
1805                  */
1806                 if (ret == NEEDS_RETRY || ret == ADD_TO_MLQUEUE) {
1807                         qc->flags |= ATA_QCFLAG_RETRY;
1808                         qc->err_mask |= AC_ERR_OTHER;
1809                 } else if (ret != SUCCESS) {
1810                         qc->err_mask |= AC_ERR_HSM;
1811                 }
1812         }
1813         if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1814                 action |= ATA_EH_RESET;
1815
1816         return action;
1817 }
1818
1819 static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask,
1820                                    int *xfer_ok)
1821 {
1822         int base = 0;
1823
1824         if (!(eflags & ATA_EFLAG_DUBIOUS_XFER))
1825                 *xfer_ok = 1;
1826
1827         if (!*xfer_ok)
1828                 base = ATA_ECAT_DUBIOUS_NONE;
1829
1830         if (err_mask & AC_ERR_ATA_BUS)
1831                 return base + ATA_ECAT_ATA_BUS;
1832
1833         if (err_mask & AC_ERR_TIMEOUT)
1834                 return base + ATA_ECAT_TOUT_HSM;
1835
1836         if (eflags & ATA_EFLAG_IS_IO) {
1837                 if (err_mask & AC_ERR_HSM)
1838                         return base + ATA_ECAT_TOUT_HSM;
1839                 if ((err_mask &
1840                      (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1841                         return base + ATA_ECAT_UNK_DEV;
1842         }
1843
1844         return 0;
1845 }
1846
1847 struct speed_down_verdict_arg {
1848         u64 since;
1849         int xfer_ok;
1850         int nr_errors[ATA_ECAT_NR];
1851 };
1852
1853 static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
1854 {
1855         struct speed_down_verdict_arg *arg = void_arg;
1856         int cat;
1857
1858         if ((ent->eflags & ATA_EFLAG_OLD_ER) || (ent->timestamp < arg->since))
1859                 return -1;
1860
1861         cat = ata_eh_categorize_error(ent->eflags, ent->err_mask,
1862                                       &arg->xfer_ok);
1863         arg->nr_errors[cat]++;
1864
1865         return 0;
1866 }
1867
1868 /**
1869  *      ata_eh_speed_down_verdict - Determine speed down verdict
1870  *      @dev: Device of interest
1871  *
1872  *      This function examines error ring of @dev and determines
1873  *      whether NCQ needs to be turned off, transfer speed should be
1874  *      stepped down, or falling back to PIO is necessary.
1875  *
1876  *      ECAT_ATA_BUS    : ATA_BUS error for any command
1877  *
1878  *      ECAT_TOUT_HSM   : TIMEOUT for any command or HSM violation for
1879  *                        IO commands
1880  *
1881  *      ECAT_UNK_DEV    : Unknown DEV error for IO commands
1882  *
1883  *      ECAT_DUBIOUS_*  : Identical to above three but occurred while
1884  *                        data transfer hasn't been verified.
1885  *
1886  *      Verdicts are
1887  *
1888  *      NCQ_OFF         : Turn off NCQ.
1889  *
1890  *      SPEED_DOWN      : Speed down transfer speed but don't fall back
1891  *                        to PIO.
1892  *
1893  *      FALLBACK_TO_PIO : Fall back to PIO.
1894  *
1895  *      Even if multiple verdicts are returned, only one action is
1896  *      taken per error.  An action triggered by non-DUBIOUS errors
1897  *      clears ering, while one triggered by DUBIOUS_* errors doesn't.
1898  *      This is to expedite speed down decisions right after device is
1899  *      initially configured.
1900  *
1901  *      The following are speed down rules.  #1 and #2 deal with
1902  *      DUBIOUS errors.
1903  *
1904  *      1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors
1905  *         occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO.
1906  *
1907  *      2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors
1908  *         occurred during last 5 mins, NCQ_OFF.
1909  *
1910  *      3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors
1911  *         occurred during last 5 mins, FALLBACK_TO_PIO
1912  *
1913  *      4. If more than 3 TOUT_HSM or UNK_DEV errors occurred
1914  *         during last 10 mins, NCQ_OFF.
1915  *
1916  *      5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6
1917  *         UNK_DEV errors occurred during last 10 mins, SPEED_DOWN.
1918  *
1919  *      LOCKING:
1920  *      Inherited from caller.
1921  *
1922  *      RETURNS:
1923  *      OR of ATA_EH_SPDN_* flags.
1924  */
1925 static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
1926 {
1927         const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
1928         u64 j64 = get_jiffies_64();
1929         struct speed_down_verdict_arg arg;
1930         unsigned int verdict = 0;
1931
1932         /* scan past 5 mins of error history */
1933         memset(&arg, 0, sizeof(arg));
1934         arg.since = j64 - min(j64, j5mins);
1935         ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1936
1937         if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] +
1938             arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1)
1939                 verdict |= ATA_EH_SPDN_SPEED_DOWN |
1940                         ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS;
1941
1942         if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] +
1943             arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1)
1944                 verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS;
1945
1946         if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1947             arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1948             arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
1949                 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
1950
1951         /* scan past 10 mins of error history */
1952         memset(&arg, 0, sizeof(arg));
1953         arg.since = j64 - min(j64, j10mins);
1954         ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1955
1956         if (arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1957             arg.nr_errors[ATA_ECAT_UNK_DEV] > 3)
1958                 verdict |= ATA_EH_SPDN_NCQ_OFF;
1959
1960         if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1961             arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 ||
1962             arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
1963                 verdict |= ATA_EH_SPDN_SPEED_DOWN;
1964
1965         return verdict;
1966 }
1967
1968 /**
1969  *      ata_eh_speed_down - record error and speed down if necessary
1970  *      @dev: Failed device
1971  *      @eflags: mask of ATA_EFLAG_* flags
1972  *      @err_mask: err_mask of the error
1973  *
1974  *      Record error and examine error history to determine whether
1975  *      adjusting transmission speed is necessary.  It also sets
1976  *      transmission limits appropriately if such adjustment is
1977  *      necessary.
1978  *
1979  *      LOCKING:
1980  *      Kernel thread context (may sleep).
1981  *
1982  *      RETURNS:
1983  *      Determined recovery action.
1984  */
1985 static unsigned int ata_eh_speed_down(struct ata_device *dev,
1986                                 unsigned int eflags, unsigned int err_mask)
1987 {
1988         struct ata_link *link = ata_dev_phys_link(dev);
1989         int xfer_ok = 0;
1990         unsigned int verdict;
1991         unsigned int action = 0;
1992
1993         /* don't bother if Cat-0 error */
1994         if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0)
1995                 return 0;
1996
1997         /* record error and determine whether speed down is necessary */
1998         ata_ering_record(&dev->ering, eflags, err_mask);
1999         verdict = ata_eh_speed_down_verdict(dev);
2000
2001         /* turn off NCQ? */
2002         if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
2003             (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
2004                            ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
2005                 dev->flags |= ATA_DFLAG_NCQ_OFF;
2006                 ata_dev_warn(dev, "NCQ disabled due to excessive errors\n");
2007                 goto done;
2008         }
2009
2010         /* speed down? */
2011         if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
2012                 /* speed down SATA link speed if possible */
2013                 if (sata_down_spd_limit(link, 0) == 0) {
2014                         action |= ATA_EH_RESET;
2015                         goto done;
2016                 }
2017
2018                 /* lower transfer mode */
2019                 if (dev->spdn_cnt < 2) {
2020                         static const int dma_dnxfer_sel[] =
2021                                 { ATA_DNXFER_DMA, ATA_DNXFER_40C };
2022                         static const int pio_dnxfer_sel[] =
2023                                 { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
2024                         int sel;
2025
2026                         if (dev->xfer_shift != ATA_SHIFT_PIO)
2027                                 sel = dma_dnxfer_sel[dev->spdn_cnt];
2028                         else
2029                                 sel = pio_dnxfer_sel[dev->spdn_cnt];
2030
2031                         dev->spdn_cnt++;
2032
2033                         if (ata_down_xfermask_limit(dev, sel) == 0) {
2034                                 action |= ATA_EH_RESET;
2035                                 goto done;
2036                         }
2037                 }
2038         }
2039
2040         /* Fall back to PIO?  Slowing down to PIO is meaningless for
2041          * SATA ATA devices.  Consider it only for PATA and SATAPI.
2042          */
2043         if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
2044             (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) &&
2045             (dev->xfer_shift != ATA_SHIFT_PIO)) {
2046                 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
2047                         dev->spdn_cnt = 0;
2048                         action |= ATA_EH_RESET;
2049                         goto done;
2050                 }
2051         }
2052
2053         return 0;
2054  done:
2055         /* device has been slowed down, blow error history */
2056         if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS))
2057                 ata_ering_clear(&dev->ering);
2058         return action;
2059 }
2060
2061 /**
2062  *      ata_eh_worth_retry - analyze error and decide whether to retry
2063  *      @qc: qc to possibly retry
2064  *
2065  *      Look at the cause of the error and decide if a retry
2066  *      might be useful or not.  We don't want to retry media errors
2067  *      because the drive itself has probably already taken 10-30 seconds
2068  *      doing its own internal retries before reporting the failure.
2069  */
2070 static inline int ata_eh_worth_retry(struct ata_queued_cmd *qc)
2071 {
2072         if (qc->err_mask & AC_ERR_MEDIA)
2073                 return 0;       /* don't retry media errors */
2074         if (qc->flags & ATA_QCFLAG_IO)
2075                 return 1;       /* otherwise retry anything from fs stack */
2076         if (qc->err_mask & AC_ERR_INVALID)
2077                 return 0;       /* don't retry these */
2078         return qc->err_mask != AC_ERR_DEV;  /* retry if not dev error */
2079 }
2080
2081 /**
2082  *      ata_eh_quiet - check if we need to be quiet about a command error
2083  *      @qc: qc to check
2084  *
2085  *      Look at the qc flags anbd its scsi command request flags to determine
2086  *      if we need to be quiet about the command failure.
2087  */
2088 static inline bool ata_eh_quiet(struct ata_queued_cmd *qc)
2089 {
2090         if (qc->scsicmd &&
2091             qc->scsicmd->request->rq_flags & RQF_QUIET)
2092                 qc->flags |= ATA_QCFLAG_QUIET;
2093         return qc->flags & ATA_QCFLAG_QUIET;
2094 }
2095
2096 /**
2097  *      ata_eh_link_autopsy - analyze error and determine recovery action
2098  *      @link: host link to perform autopsy on
2099  *
2100  *      Analyze why @link failed and determine which recovery actions
2101  *      are needed.  This function also sets more detailed AC_ERR_*
2102  *      values and fills sense data for ATAPI CHECK SENSE.
2103  *
2104  *      LOCKING:
2105  *      Kernel thread context (may sleep).
2106  */
2107 static void ata_eh_link_autopsy(struct ata_link *link)
2108 {
2109         struct ata_port *ap = link->ap;
2110         struct ata_eh_context *ehc = &link->eh_context;
2111         struct ata_queued_cmd *qc;
2112         struct ata_device *dev;
2113         unsigned int all_err_mask = 0, eflags = 0;
2114         int tag, nr_failed = 0, nr_quiet = 0;
2115         u32 serror;
2116         int rc;
2117
2118         DPRINTK("ENTER\n");
2119
2120         if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
2121                 return;
2122
2123         /* obtain and analyze SError */
2124         rc = sata_scr_read(link, SCR_ERROR, &serror);
2125         if (rc == 0) {
2126                 ehc->i.serror |= serror;
2127                 ata_eh_analyze_serror(link);
2128         } else if (rc != -EOPNOTSUPP) {
2129                 /* SError read failed, force reset and probing */
2130                 ehc->i.probe_mask |= ATA_ALL_DEVICES;
2131                 ehc->i.action |= ATA_EH_RESET;
2132                 ehc->i.err_mask |= AC_ERR_OTHER;
2133         }
2134
2135         /* analyze NCQ failure */
2136         ata_eh_analyze_ncq_error(link);
2137
2138         /* any real error trumps AC_ERR_OTHER */
2139         if (ehc->i.err_mask & ~AC_ERR_OTHER)
2140                 ehc->i.err_mask &= ~AC_ERR_OTHER;
2141
2142         all_err_mask |= ehc->i.err_mask;
2143
2144         ata_qc_for_each_raw(ap, qc, tag) {
2145                 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
2146                     ata_dev_phys_link(qc->dev) != link)
2147                         continue;
2148
2149                 /* inherit upper level err_mask */
2150                 qc->err_mask |= ehc->i.err_mask;
2151
2152                 /* analyze TF */
2153                 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
2154
2155                 /* DEV errors are probably spurious in case of ATA_BUS error */
2156                 if (qc->err_mask & AC_ERR_ATA_BUS)
2157                         qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
2158                                           AC_ERR_INVALID);
2159
2160                 /* any real error trumps unknown error */
2161                 if (qc->err_mask & ~AC_ERR_OTHER)
2162                         qc->err_mask &= ~AC_ERR_OTHER;
2163
2164                 /*
2165                  * SENSE_VALID trumps dev/unknown error and revalidation. Upper
2166                  * layers will determine whether the command is worth retrying
2167                  * based on the sense data and device class/type. Otherwise,
2168                  * determine directly if the command is worth retrying using its
2169                  * error mask and flags.
2170                  */
2171                 if (qc->flags & ATA_QCFLAG_SENSE_VALID)
2172                         qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
2173                 else if (ata_eh_worth_retry(qc))
2174                         qc->flags |= ATA_QCFLAG_RETRY;
2175
2176                 /* accumulate error info */
2177                 ehc->i.dev = qc->dev;
2178                 all_err_mask |= qc->err_mask;
2179                 if (qc->flags & ATA_QCFLAG_IO)
2180                         eflags |= ATA_EFLAG_IS_IO;
2181                 trace_ata_eh_link_autopsy_qc(qc);
2182
2183                 /* Count quiet errors */
2184                 if (ata_eh_quiet(qc))
2185                         nr_quiet++;
2186                 nr_failed++;
2187         }
2188
2189         /* If all failed commands requested silence, then be quiet */
2190         if (nr_quiet == nr_failed)
2191                 ehc->i.flags |= ATA_EHI_QUIET;
2192
2193         /* enforce default EH actions */
2194         if (ap->pflags & ATA_PFLAG_FROZEN ||
2195             all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
2196                 ehc->i.action |= ATA_EH_RESET;
2197         else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) ||
2198                  (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV)))
2199                 ehc->i.action |= ATA_EH_REVALIDATE;
2200
2201         /* If we have offending qcs and the associated failed device,
2202          * perform per-dev EH action only on the offending device.
2203          */
2204         if (ehc->i.dev) {
2205                 ehc->i.dev_action[ehc->i.dev->devno] |=
2206                         ehc->i.action & ATA_EH_PERDEV_MASK;
2207                 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
2208         }
2209
2210         /* propagate timeout to host link */
2211         if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link))
2212                 ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT;
2213
2214         /* record error and consider speeding down */
2215         dev = ehc->i.dev;
2216         if (!dev && ((ata_link_max_devices(link) == 1 &&
2217                       ata_dev_enabled(link->device))))
2218             dev = link->device;
2219
2220         if (dev) {
2221                 if (dev->flags & ATA_DFLAG_DUBIOUS_XFER)
2222                         eflags |= ATA_EFLAG_DUBIOUS_XFER;
2223                 ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask);
2224                 trace_ata_eh_link_autopsy(dev, ehc->i.action, all_err_mask);
2225         }
2226         DPRINTK("EXIT\n");
2227 }
2228
2229 /**
2230  *      ata_eh_autopsy - analyze error and determine recovery action
2231  *      @ap: host port to perform autopsy on
2232  *
2233  *      Analyze all links of @ap and determine why they failed and
2234  *      which recovery actions are needed.
2235  *
2236  *      LOCKING:
2237  *      Kernel thread context (may sleep).
2238  */
2239 void ata_eh_autopsy(struct ata_port *ap)
2240 {
2241         struct ata_link *link;
2242
2243         ata_for_each_link(link, ap, EDGE)
2244                 ata_eh_link_autopsy(link);
2245
2246         /* Handle the frigging slave link.  Autopsy is done similarly
2247          * but actions and flags are transferred over to the master
2248          * link and handled from there.
2249          */
2250         if (ap->slave_link) {
2251                 struct ata_eh_context *mehc = &ap->link.eh_context;
2252                 struct ata_eh_context *sehc = &ap->slave_link->eh_context;
2253
2254                 /* transfer control flags from master to slave */
2255                 sehc->i.flags |= mehc->i.flags & ATA_EHI_TO_SLAVE_MASK;
2256
2257                 /* perform autopsy on the slave link */
2258                 ata_eh_link_autopsy(ap->slave_link);
2259
2260                 /* transfer actions from slave to master and clear slave */
2261                 ata_eh_about_to_do(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
2262                 mehc->i.action          |= sehc->i.action;
2263                 mehc->i.dev_action[1]   |= sehc->i.dev_action[1];
2264                 mehc->i.flags           |= sehc->i.flags;
2265                 ata_eh_done(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
2266         }
2267
2268         /* Autopsy of fanout ports can affect host link autopsy.
2269          * Perform host link autopsy last.
2270          */
2271         if (sata_pmp_attached(ap))
2272                 ata_eh_link_autopsy(&ap->link);
2273 }
2274
2275 /**
2276  *      ata_get_cmd_descript - get description for ATA command
2277  *      @command: ATA command code to get description for
2278  *
2279  *      Return a textual description of the given command, or NULL if the
2280  *      command is not known.
2281  *
2282  *      LOCKING:
2283  *      None
2284  */
2285 const char *ata_get_cmd_descript(u8 command)
2286 {
2287 #ifdef CONFIG_ATA_VERBOSE_ERROR
2288         static const struct
2289         {
2290                 u8 command;
2291                 const char *text;
2292         } cmd_descr[] = {
2293                 { ATA_CMD_DEV_RESET,            "DEVICE RESET" },
2294                 { ATA_CMD_CHK_POWER,            "CHECK POWER MODE" },
2295                 { ATA_CMD_STANDBY,              "STANDBY" },
2296                 { ATA_CMD_IDLE,                 "IDLE" },
2297                 { ATA_CMD_EDD,                  "EXECUTE DEVICE DIAGNOSTIC" },
2298                 { ATA_CMD_DOWNLOAD_MICRO,       "DOWNLOAD MICROCODE" },
2299                 { ATA_CMD_DOWNLOAD_MICRO_DMA,   "DOWNLOAD MICROCODE DMA" },
2300                 { ATA_CMD_NOP,                  "NOP" },
2301                 { ATA_CMD_FLUSH,                "FLUSH CACHE" },
2302                 { ATA_CMD_FLUSH_EXT,            "FLUSH CACHE EXT" },
2303                 { ATA_CMD_ID_ATA,               "IDENTIFY DEVICE" },
2304                 { ATA_CMD_ID_ATAPI,             "IDENTIFY PACKET DEVICE" },
2305                 { ATA_CMD_SERVICE,              "SERVICE" },
2306                 { ATA_CMD_READ,                 "READ DMA" },
2307                 { ATA_CMD_READ_EXT,             "READ DMA EXT" },
2308                 { ATA_CMD_READ_QUEUED,          "READ DMA QUEUED" },
2309                 { ATA_CMD_READ_STREAM_EXT,      "READ STREAM EXT" },
2310                 { ATA_CMD_READ_STREAM_DMA_EXT,  "READ STREAM DMA EXT" },
2311                 { ATA_CMD_WRITE,                "WRITE DMA" },
2312                 { ATA_CMD_WRITE_EXT,            "WRITE DMA EXT" },
2313                 { ATA_CMD_WRITE_QUEUED,         "WRITE DMA QUEUED EXT" },
2314                 { ATA_CMD_WRITE_STREAM_EXT,     "WRITE STREAM EXT" },
2315                 { ATA_CMD_WRITE_STREAM_DMA_EXT, "WRITE STREAM DMA EXT" },
2316                 { ATA_CMD_WRITE_FUA_EXT,        "WRITE DMA FUA EXT" },
2317                 { ATA_CMD_WRITE_QUEUED_FUA_EXT, "WRITE DMA QUEUED FUA EXT" },
2318                 { ATA_CMD_FPDMA_READ,           "READ FPDMA QUEUED" },
2319                 { ATA_CMD_FPDMA_WRITE,          "WRITE FPDMA QUEUED" },
2320                 { ATA_CMD_FPDMA_SEND,           "SEND FPDMA QUEUED" },
2321                 { ATA_CMD_FPDMA_RECV,           "RECEIVE FPDMA QUEUED" },
2322                 { ATA_CMD_PIO_READ,             "READ SECTOR(S)" },
2323                 { ATA_CMD_PIO_READ_EXT,         "READ SECTOR(S) EXT" },
2324                 { ATA_CMD_PIO_WRITE,            "WRITE SECTOR(S)" },
2325                 { ATA_CMD_PIO_WRITE_EXT,        "WRITE SECTOR(S) EXT" },
2326                 { ATA_CMD_READ_MULTI,           "READ MULTIPLE" },
2327                 { ATA_CMD_READ_MULTI_EXT,       "READ MULTIPLE EXT" },
2328                 { ATA_CMD_WRITE_MULTI,          "WRITE MULTIPLE" },
2329                 { ATA_CMD_WRITE_MULTI_EXT,      "WRITE MULTIPLE EXT" },
2330                 { ATA_CMD_WRITE_MULTI_FUA_EXT,  "WRITE MULTIPLE FUA EXT" },
2331                 { ATA_CMD_SET_FEATURES,         "SET FEATURES" },
2332                 { ATA_CMD_SET_MULTI,            "SET MULTIPLE MODE" },
2333                 { ATA_CMD_VERIFY,               "READ VERIFY SECTOR(S)" },
2334                 { ATA_CMD_VERIFY_EXT,           "READ VERIFY SECTOR(S) EXT" },
2335                 { ATA_CMD_WRITE_UNCORR_EXT,     "WRITE UNCORRECTABLE EXT" },
2336                 { ATA_CMD_STANDBYNOW1,          "STANDBY IMMEDIATE" },
2337                 { ATA_CMD_IDLEIMMEDIATE,        "IDLE IMMEDIATE" },
2338                 { ATA_CMD_SLEEP,                "SLEEP" },
2339                 { ATA_CMD_INIT_DEV_PARAMS,      "INITIALIZE DEVICE PARAMETERS" },
2340                 { ATA_CMD_READ_NATIVE_MAX,      "READ NATIVE MAX ADDRESS" },
2341                 { ATA_CMD_READ_NATIVE_MAX_EXT,  "READ NATIVE MAX ADDRESS EXT" },
2342                 { ATA_CMD_SET_MAX,              "SET MAX ADDRESS" },
2343                 { ATA_CMD_SET_MAX_EXT,          "SET MAX ADDRESS EXT" },
2344                 { ATA_CMD_READ_LOG_EXT,         "READ LOG EXT" },
2345                 { ATA_CMD_WRITE_LOG_EXT,        "WRITE LOG EXT" },
2346                 { ATA_CMD_READ_LOG_DMA_EXT,     "READ LOG DMA EXT" },
2347                 { ATA_CMD_WRITE_LOG_DMA_EXT,    "WRITE LOG DMA EXT" },
2348                 { ATA_CMD_TRUSTED_NONDATA,      "TRUSTED NON-DATA" },
2349                 { ATA_CMD_TRUSTED_RCV,          "TRUSTED RECEIVE" },
2350                 { ATA_CMD_TRUSTED_RCV_DMA,      "TRUSTED RECEIVE DMA" },
2351                 { ATA_CMD_TRUSTED_SND,          "TRUSTED SEND" },
2352                 { ATA_CMD_TRUSTED_SND_DMA,      "TRUSTED SEND DMA" },
2353                 { ATA_CMD_PMP_READ,             "READ BUFFER" },
2354                 { ATA_CMD_PMP_READ_DMA,         "READ BUFFER DMA" },
2355                 { ATA_CMD_PMP_WRITE,            "WRITE BUFFER" },
2356                 { ATA_CMD_PMP_WRITE_DMA,        "WRITE BUFFER DMA" },
2357                 { ATA_CMD_CONF_OVERLAY,         "DEVICE CONFIGURATION OVERLAY" },
2358                 { ATA_CMD_SEC_SET_PASS,         "SECURITY SET PASSWORD" },
2359                 { ATA_CMD_SEC_UNLOCK,           "SECURITY UNLOCK" },
2360                 { ATA_CMD_SEC_ERASE_PREP,       "SECURITY ERASE PREPARE" },
2361                 { ATA_CMD_SEC_ERASE_UNIT,       "SECURITY ERASE UNIT" },
2362                 { ATA_CMD_SEC_FREEZE_LOCK,      "SECURITY FREEZE LOCK" },
2363                 { ATA_CMD_SEC_DISABLE_PASS,     "SECURITY DISABLE PASSWORD" },
2364                 { ATA_CMD_CONFIG_STREAM,        "CONFIGURE STREAM" },
2365                 { ATA_CMD_SMART,                "SMART" },
2366                 { ATA_CMD_MEDIA_LOCK,           "DOOR LOCK" },
2367                 { ATA_CMD_MEDIA_UNLOCK,         "DOOR UNLOCK" },
2368                 { ATA_CMD_DSM,                  "DATA SET MANAGEMENT" },
2369                 { ATA_CMD_CHK_MED_CRD_TYP,      "CHECK MEDIA CARD TYPE" },
2370                 { ATA_CMD_CFA_REQ_EXT_ERR,      "CFA REQUEST EXTENDED ERROR" },
2371                 { ATA_CMD_CFA_WRITE_NE,         "CFA WRITE SECTORS WITHOUT ERASE" },
2372                 { ATA_CMD_CFA_TRANS_SECT,       "CFA TRANSLATE SECTOR" },
2373                 { ATA_CMD_CFA_ERASE,            "CFA ERASE SECTORS" },
2374                 { ATA_CMD_CFA_WRITE_MULT_NE,    "CFA WRITE MULTIPLE WITHOUT ERASE" },
2375                 { ATA_CMD_REQ_SENSE_DATA,       "REQUEST SENSE DATA EXT" },
2376                 { ATA_CMD_SANITIZE_DEVICE,      "SANITIZE DEVICE" },
2377                 { ATA_CMD_ZAC_MGMT_IN,          "ZAC MANAGEMENT IN" },
2378                 { ATA_CMD_ZAC_MGMT_OUT,         "ZAC MANAGEMENT OUT" },
2379                 { ATA_CMD_READ_LONG,            "READ LONG (with retries)" },
2380                 { ATA_CMD_READ_LONG_ONCE,       "READ LONG (without retries)" },
2381                 { ATA_CMD_WRITE_LONG,           "WRITE LONG (with retries)" },
2382                 { ATA_CMD_WRITE_LONG_ONCE,      "WRITE LONG (without retries)" },
2383                 { ATA_CMD_RESTORE,              "RECALIBRATE" },
2384                 { 0,                            NULL } /* terminate list */
2385         };
2386
2387         unsigned int i;
2388         for (i = 0; cmd_descr[i].text; i++)
2389                 if (cmd_descr[i].command == command)
2390                         return cmd_descr[i].text;
2391 #endif
2392
2393         return NULL;
2394 }
2395 EXPORT_SYMBOL_GPL(ata_get_cmd_descript);
2396
2397 /**
2398  *      ata_eh_link_report - report error handling to user
2399  *      @link: ATA link EH is going on
2400  *
2401  *      Report EH to user.
2402  *
2403  *      LOCKING:
2404  *      None.
2405  */
2406 static void ata_eh_link_report(struct ata_link *link)
2407 {
2408         struct ata_port *ap = link->ap;
2409         struct ata_eh_context *ehc = &link->eh_context;
2410         struct ata_queued_cmd *qc;
2411         const char *frozen, *desc;
2412         char tries_buf[6] = "";
2413         int tag, nr_failed = 0;
2414
2415         if (ehc->i.flags & ATA_EHI_QUIET)
2416                 return;
2417
2418         desc = NULL;
2419         if (ehc->i.desc[0] != '\0')
2420                 desc = ehc->i.desc;
2421
2422         ata_qc_for_each_raw(ap, qc, tag) {
2423                 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
2424                     ata_dev_phys_link(qc->dev) != link ||
2425                     ((qc->flags & ATA_QCFLAG_QUIET) &&
2426                      qc->err_mask == AC_ERR_DEV))
2427                         continue;
2428                 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
2429                         continue;
2430
2431                 nr_failed++;
2432         }
2433
2434         if (!nr_failed && !ehc->i.err_mask)
2435                 return;
2436
2437         frozen = "";
2438         if (ap->pflags & ATA_PFLAG_FROZEN)
2439                 frozen = " frozen";
2440
2441         if (ap->eh_tries < ATA_EH_MAX_TRIES)
2442                 snprintf(tries_buf, sizeof(tries_buf), " t%d",
2443                          ap->eh_tries);
2444
2445         if (ehc->i.dev) {
2446                 ata_dev_err(ehc->i.dev, "exception Emask 0x%x "
2447                             "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2448                             ehc->i.err_mask, link->sactive, ehc->i.serror,
2449                             ehc->i.action, frozen, tries_buf);
2450                 if (desc)
2451                         ata_dev_err(ehc->i.dev, "%s\n", desc);
2452         } else {
2453                 ata_link_err(link, "exception Emask 0x%x "
2454                              "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2455                              ehc->i.err_mask, link->sactive, ehc->i.serror,
2456                              ehc->i.action, frozen, tries_buf);
2457                 if (desc)
2458                         ata_link_err(link, "%s\n", desc);
2459         }
2460
2461 #ifdef CONFIG_ATA_VERBOSE_ERROR
2462         if (ehc->i.serror)
2463                 ata_link_err(link,
2464                   "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
2465                   ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
2466                   ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
2467                   ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
2468                   ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
2469                   ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
2470                   ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
2471                   ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
2472                   ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
2473                   ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
2474                   ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
2475                   ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
2476                   ehc->i.serror & SERR_CRC ? "BadCRC " : "",
2477                   ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
2478                   ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
2479                   ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
2480                   ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
2481                   ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
2482 #endif
2483
2484         ata_qc_for_each_raw(ap, qc, tag) {
2485                 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
2486                 char data_buf[20] = "";
2487                 char cdb_buf[70] = "";
2488
2489                 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
2490                     ata_dev_phys_link(qc->dev) != link || !qc->err_mask)
2491                         continue;
2492
2493                 if (qc->dma_dir != DMA_NONE) {
2494                         static const char *dma_str[] = {
2495                                 [DMA_BIDIRECTIONAL]     = "bidi",
2496                                 [DMA_TO_DEVICE]         = "out",
2497                                 [DMA_FROM_DEVICE]       = "in",
2498                         };
2499                         const char *prot_str = NULL;
2500
2501                         switch (qc->tf.protocol) {
2502                         case ATA_PROT_UNKNOWN:
2503                                 prot_str = "unknown";
2504                                 break;
2505                         case ATA_PROT_NODATA:
2506                                 prot_str = "nodata";
2507                                 break;
2508                         case ATA_PROT_PIO:
2509                                 prot_str = "pio";
2510                                 break;
2511                         case ATA_PROT_DMA:
2512                                 prot_str = "dma";
2513                                 break;
2514                         case ATA_PROT_NCQ:
2515                                 prot_str = "ncq dma";
2516                                 break;
2517                         case ATA_PROT_NCQ_NODATA:
2518                                 prot_str = "ncq nodata";
2519                                 break;
2520                         case ATAPI_PROT_NODATA:
2521                                 prot_str = "nodata";
2522                                 break;
2523                         case ATAPI_PROT_PIO:
2524                                 prot_str = "pio";
2525                                 break;
2526                         case ATAPI_PROT_DMA:
2527                                 prot_str = "dma";
2528                                 break;
2529                         }
2530                         snprintf(data_buf, sizeof(data_buf), " %s %u %s",
2531                                  prot_str, qc->nbytes, dma_str[qc->dma_dir]);
2532                 }
2533
2534                 if (ata_is_atapi(qc->tf.protocol)) {
2535                         const u8 *cdb = qc->cdb;
2536                         size_t cdb_len = qc->dev->cdb_len;
2537
2538                         if (qc->scsicmd) {
2539                                 cdb = qc->scsicmd->cmnd;
2540                                 cdb_len = qc->scsicmd->cmd_len;
2541                         }
2542                         __scsi_format_command(cdb_buf, sizeof(cdb_buf),
2543                                               cdb, cdb_len);
2544                 } else {
2545                         const char *descr = ata_get_cmd_descript(cmd->command);
2546                         if (descr)
2547                                 ata_dev_err(qc->dev, "failed command: %s\n",
2548                                             descr);
2549                 }
2550
2551                 ata_dev_err(qc->dev,
2552                         "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
2553                         "tag %d%s\n         %s"
2554                         "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
2555                         "Emask 0x%x (%s)%s\n",
2556                         cmd->command, cmd->feature, cmd->nsect,
2557                         cmd->lbal, cmd->lbam, cmd->lbah,
2558                         cmd->hob_feature, cmd->hob_nsect,
2559                         cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
2560                         cmd->device, qc->tag, data_buf, cdb_buf,
2561                         res->command, res->feature, res->nsect,
2562                         res->lbal, res->lbam, res->lbah,
2563                         res->hob_feature, res->hob_nsect,
2564                         res->hob_lbal, res->hob_lbam, res->hob_lbah,
2565                         res->device, qc->err_mask, ata_err_string(qc->err_mask),
2566                         qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
2567
2568 #ifdef CONFIG_ATA_VERBOSE_ERROR
2569                 if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
2570                                     ATA_SENSE | ATA_ERR)) {
2571                         if (res->command & ATA_BUSY)
2572                                 ata_dev_err(qc->dev, "status: { Busy }\n");
2573                         else
2574                                 ata_dev_err(qc->dev, "status: { %s%s%s%s%s}\n",
2575                                   res->command & ATA_DRDY ? "DRDY " : "",
2576                                   res->command & ATA_DF ? "DF " : "",
2577                                   res->command & ATA_DRQ ? "DRQ " : "",
2578                                   res->command & ATA_SENSE ? "SENSE " : "",
2579                                   res->command & ATA_ERR ? "ERR " : "");
2580                 }
2581
2582                 if (cmd->command != ATA_CMD_PACKET &&
2583                     (res->feature & (ATA_ICRC | ATA_UNC | ATA_AMNF |
2584                                      ATA_IDNF | ATA_ABORTED)))
2585                         ata_dev_err(qc->dev, "error: { %s%s%s%s%s}\n",
2586                           res->feature & ATA_ICRC ? "ICRC " : "",
2587                           res->feature & ATA_UNC ? "UNC " : "",
2588                           res->feature & ATA_AMNF ? "AMNF " : "",
2589                           res->feature & ATA_IDNF ? "IDNF " : "",
2590                           res->feature & ATA_ABORTED ? "ABRT " : "");
2591 #endif
2592         }
2593 }
2594
2595 /**
2596  *      ata_eh_report - report error handling to user
2597  *      @ap: ATA port to report EH about
2598  *
2599  *      Report EH to user.
2600  *
2601  *      LOCKING:
2602  *      None.
2603  */
2604 void ata_eh_report(struct ata_port *ap)
2605 {
2606         struct ata_link *link;
2607
2608         ata_for_each_link(link, ap, HOST_FIRST)
2609                 ata_eh_link_report(link);
2610 }
2611
2612 static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
2613                         unsigned int *classes, unsigned long deadline,
2614                         bool clear_classes)
2615 {
2616         struct ata_device *dev;
2617
2618         if (clear_classes)
2619                 ata_for_each_dev(dev, link, ALL)
2620                         classes[dev->devno] = ATA_DEV_UNKNOWN;
2621
2622         return reset(link, classes, deadline);
2623 }
2624
2625 static int ata_eh_followup_srst_needed(struct ata_link *link, int rc)
2626 {
2627         if ((link->flags & ATA_LFLAG_NO_SRST) || ata_link_offline(link))
2628                 return 0;
2629         if (rc == -EAGAIN)
2630                 return 1;
2631         if (sata_pmp_supported(link->ap) && ata_is_host_link(link))
2632                 return 1;
2633         return 0;
2634 }
2635
2636 int ata_eh_reset(struct ata_link *link, int classify,
2637                  ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
2638                  ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
2639 {
2640         struct ata_port *ap = link->ap;
2641         struct ata_link *slave = ap->slave_link;
2642         struct ata_eh_context *ehc = &link->eh_context;
2643         struct ata_eh_context *sehc = slave ? &slave->eh_context : NULL;
2644         unsigned int *classes = ehc->classes;
2645         unsigned int lflags = link->flags;
2646         int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
2647         int max_tries = 0, try = 0;
2648         struct ata_link *failed_link;
2649         struct ata_device *dev;
2650         unsigned long deadline, now;
2651         ata_reset_fn_t reset;
2652         unsigned long flags;
2653         u32 sstatus;
2654         int nr_unknown, rc;
2655
2656         /*
2657          * Prepare to reset
2658          */
2659         while (ata_eh_reset_timeouts[max_tries] != ULONG_MAX)
2660                 max_tries++;
2661         if (link->flags & ATA_LFLAG_RST_ONCE)
2662                 max_tries = 1;
2663         if (link->flags & ATA_LFLAG_NO_HRST)
2664                 hardreset = NULL;
2665         if (link->flags & ATA_LFLAG_NO_SRST)
2666                 softreset = NULL;
2667
2668         /* make sure each reset attempt is at least COOL_DOWN apart */
2669         if (ehc->i.flags & ATA_EHI_DID_RESET) {
2670                 now = jiffies;
2671                 WARN_ON(time_after(ehc->last_reset, now));
2672                 deadline = ata_deadline(ehc->last_reset,
2673                                         ATA_EH_RESET_COOL_DOWN);
2674                 if (time_before(now, deadline))
2675                         schedule_timeout_uninterruptible(deadline - now);
2676         }
2677
2678         spin_lock_irqsave(ap->lock, flags);
2679         ap->pflags |= ATA_PFLAG_RESETTING;
2680         spin_unlock_irqrestore(ap->lock, flags);
2681
2682         ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
2683
2684         ata_for_each_dev(dev, link, ALL) {
2685                 /* If we issue an SRST then an ATA drive (not ATAPI)
2686                  * may change configuration and be in PIO0 timing. If
2687                  * we do a hard reset (or are coming from power on)
2688                  * this is true for ATA or ATAPI. Until we've set a
2689                  * suitable controller mode we should not touch the
2690                  * bus as we may be talking too fast.
2691                  */
2692                 dev->pio_mode = XFER_PIO_0;
2693                 dev->dma_mode = 0xff;
2694
2695                 /* If the controller has a pio mode setup function
2696                  * then use it to set the chipset to rights. Don't
2697                  * touch the DMA setup as that will be dealt with when
2698                  * configuring devices.
2699                  */
2700                 if (ap->ops->set_piomode)
2701                         ap->ops->set_piomode(ap, dev);
2702         }
2703
2704         /* prefer hardreset */
2705         reset = NULL;
2706         ehc->i.action &= ~ATA_EH_RESET;
2707         if (hardreset) {
2708                 reset = hardreset;
2709                 ehc->i.action |= ATA_EH_HARDRESET;
2710         } else if (softreset) {
2711                 reset = softreset;
2712                 ehc->i.action |= ATA_EH_SOFTRESET;
2713         }
2714
2715         if (prereset) {
2716                 unsigned long deadline = ata_deadline(jiffies,
2717                                                       ATA_EH_PRERESET_TIMEOUT);
2718
2719                 if (slave) {
2720                         sehc->i.action &= ~ATA_EH_RESET;
2721                         sehc->i.action |= ehc->i.action;
2722                 }
2723
2724                 rc = prereset(link, deadline);
2725
2726                 /* If present, do prereset on slave link too.  Reset
2727                  * is skipped iff both master and slave links report
2728                  * -ENOENT or clear ATA_EH_RESET.
2729                  */
2730                 if (slave && (rc == 0 || rc == -ENOENT)) {
2731                         int tmp;
2732
2733                         tmp = prereset(slave, deadline);
2734                         if (tmp != -ENOENT)
2735                                 rc = tmp;
2736
2737                         ehc->i.action |= sehc->i.action;
2738                 }
2739
2740                 if (rc) {
2741                         if (rc == -ENOENT) {
2742                                 ata_link_dbg(link, "port disabled--ignoring\n");
2743                                 ehc->i.action &= ~ATA_EH_RESET;
2744
2745                                 ata_for_each_dev(dev, link, ALL)
2746                                         classes[dev->devno] = ATA_DEV_NONE;
2747
2748                                 rc = 0;
2749                         } else
2750                                 ata_link_err(link,
2751                                              "prereset failed (errno=%d)\n",
2752                                              rc);
2753                         goto out;
2754                 }
2755
2756                 /* prereset() might have cleared ATA_EH_RESET.  If so,
2757                  * bang classes, thaw and return.
2758                  */
2759                 if (reset && !(ehc->i.action & ATA_EH_RESET)) {
2760                         ata_for_each_dev(dev, link, ALL)
2761                                 classes[dev->devno] = ATA_DEV_NONE;
2762                         if ((ap->pflags & ATA_PFLAG_FROZEN) &&
2763                             ata_is_host_link(link))
2764                                 ata_eh_thaw_port(ap);
2765                         rc = 0;
2766                         goto out;
2767                 }
2768         }
2769
2770  retry:
2771         /*
2772          * Perform reset
2773          */
2774         if (ata_is_host_link(link))
2775                 ata_eh_freeze_port(ap);
2776
2777         deadline = ata_deadline(jiffies, ata_eh_reset_timeouts[try++]);
2778
2779         if (reset) {
2780                 if (verbose)
2781                         ata_link_info(link, "%s resetting link\n",
2782                                       reset == softreset ? "soft" : "hard");
2783
2784                 /* mark that this EH session started with reset */
2785                 ehc->last_reset = jiffies;
2786                 if (reset == hardreset)
2787                         ehc->i.flags |= ATA_EHI_DID_HARDRESET;
2788                 else
2789                         ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
2790
2791                 rc = ata_do_reset(link, reset, classes, deadline, true);
2792                 if (rc && rc != -EAGAIN) {
2793                         failed_link = link;
2794                         goto fail;
2795                 }
2796
2797                 /* hardreset slave link if existent */
2798                 if (slave && reset == hardreset) {
2799                         int tmp;
2800
2801                         if (verbose)
2802                                 ata_link_info(slave, "hard resetting link\n");
2803
2804                         ata_eh_about_to_do(slave, NULL, ATA_EH_RESET);
2805                         tmp = ata_do_reset(slave, reset, classes, deadline,
2806                                            false);
2807                         switch (tmp) {
2808                         case -EAGAIN:
2809                                 rc = -EAGAIN;
2810                         case 0:
2811                                 break;
2812                         default:
2813                                 failed_link = slave;
2814                                 rc = tmp;
2815                                 goto fail;
2816                         }
2817                 }
2818
2819                 /* perform follow-up SRST if necessary */
2820                 if (reset == hardreset &&
2821                     ata_eh_followup_srst_needed(link, rc)) {
2822                         reset = softreset;
2823
2824                         if (!reset) {
2825                                 ata_link_err(link,
2826              "follow-up softreset required but no softreset available\n");
2827                                 failed_link = link;
2828                                 rc = -EINVAL;
2829                                 goto fail;
2830                         }
2831
2832                         ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
2833                         rc = ata_do_reset(link, reset, classes, deadline, true);
2834                         if (rc) {
2835                                 failed_link = link;
2836                                 goto fail;
2837                         }
2838                 }
2839         } else {
2840                 if (verbose)
2841                         ata_link_info(link,
2842         "no reset method available, skipping reset\n");
2843                 if (!(lflags & ATA_LFLAG_ASSUME_CLASS))
2844                         lflags |= ATA_LFLAG_ASSUME_ATA;
2845         }
2846
2847         /*
2848          * Post-reset processing
2849          */
2850         ata_for_each_dev(dev, link, ALL) {
2851                 /* After the reset, the device state is PIO 0 and the
2852                  * controller state is undefined.  Reset also wakes up
2853                  * drives from sleeping mode.
2854                  */
2855                 dev->pio_mode = XFER_PIO_0;
2856                 dev->flags &= ~ATA_DFLAG_SLEEPING;
2857
2858                 if (ata_phys_link_offline(ata_dev_phys_link(dev)))
2859                         continue;
2860
2861                 /* apply class override */
2862                 if (lflags & ATA_LFLAG_ASSUME_ATA)
2863                         classes[dev->devno] = ATA_DEV_ATA;
2864                 else if (lflags & ATA_LFLAG_ASSUME_SEMB)
2865                         classes[dev->devno] = ATA_DEV_SEMB_UNSUP;
2866         }
2867
2868         /* record current link speed */
2869         if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2870                 link->sata_spd = (sstatus >> 4) & 0xf;
2871         if (slave && sata_scr_read(slave, SCR_STATUS, &sstatus) == 0)
2872                 slave->sata_spd = (sstatus >> 4) & 0xf;
2873
2874         /* thaw the port */
2875         if (ata_is_host_link(link))
2876                 ata_eh_thaw_port(ap);
2877
2878         /* postreset() should clear hardware SError.  Although SError
2879          * is cleared during link resume, clearing SError here is
2880          * necessary as some PHYs raise hotplug events after SRST.
2881          * This introduces race condition where hotplug occurs between
2882          * reset and here.  This race is mediated by cross checking
2883          * link onlineness and classification result later.
2884          */
2885         if (postreset) {
2886                 postreset(link, classes);
2887                 if (slave)
2888                         postreset(slave, classes);
2889         }
2890
2891         /*
2892          * Some controllers can't be frozen very well and may set spurious
2893          * error conditions during reset.  Clear accumulated error
2894          * information and re-thaw the port if frozen.  As reset is the
2895          * final recovery action and we cross check link onlineness against
2896          * device classification later, no hotplug event is lost by this.
2897          */
2898         spin_lock_irqsave(link->ap->lock, flags);
2899         memset(&link->eh_info, 0, sizeof(link->eh_info));
2900         if (slave)
2901                 memset(&slave->eh_info, 0, sizeof(link->eh_info));
2902         ap->pflags &= ~ATA_PFLAG_EH_PENDING;
2903         spin_unlock_irqrestore(link->ap->lock, flags);
2904
2905         if (ap->pflags & ATA_PFLAG_FROZEN)
2906                 ata_eh_thaw_port(ap);
2907
2908         /*
2909          * Make sure onlineness and classification result correspond.
2910          * Hotplug could have happened during reset and some
2911          * controllers fail to wait while a drive is spinning up after
2912          * being hotplugged causing misdetection.  By cross checking
2913          * link on/offlineness and classification result, those
2914          * conditions can be reliably detected and retried.
2915          */
2916         nr_unknown = 0;
2917         ata_for_each_dev(dev, link, ALL) {
2918                 if (ata_phys_link_online(ata_dev_phys_link(dev))) {
2919                         if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
2920                                 ata_dev_dbg(dev, "link online but device misclassified\n");
2921                                 classes[dev->devno] = ATA_DEV_NONE;
2922                                 nr_unknown++;
2923                         }
2924                 } else if (ata_phys_link_offline(ata_dev_phys_link(dev))) {
2925                         if (ata_class_enabled(classes[dev->devno]))
2926                                 ata_dev_dbg(dev,
2927                                             "link offline, clearing class %d to NONE\n",
2928                                             classes[dev->devno]);
2929                         classes[dev->devno] = ATA_DEV_NONE;
2930                 } else if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
2931                         ata_dev_dbg(dev,
2932                                     "link status unknown, clearing UNKNOWN to NONE\n");
2933                         classes[dev->devno] = ATA_DEV_NONE;
2934                 }
2935         }
2936
2937         if (classify && nr_unknown) {
2938                 if (try < max_tries) {
2939                         ata_link_warn(link,
2940                                       "link online but %d devices misclassified, retrying\n",
2941                                       nr_unknown);
2942                         failed_link = link;
2943                         rc = -EAGAIN;
2944                         goto fail;
2945                 }
2946                 ata_link_warn(link,
2947                               "link online but %d devices misclassified, "
2948                               "device detection might fail\n", nr_unknown);
2949         }
2950
2951         /* reset successful, schedule revalidation */
2952         ata_eh_done(link, NULL, ATA_EH_RESET);
2953         if (slave)
2954                 ata_eh_done(slave, NULL, ATA_EH_RESET);
2955         ehc->last_reset = jiffies;              /* update to completion time */
2956         ehc->i.action |= ATA_EH_REVALIDATE;
2957         link->lpm_policy = ATA_LPM_UNKNOWN;     /* reset LPM state */
2958
2959         rc = 0;
2960  out:
2961         /* clear hotplug flag */
2962         ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2963         if (slave)
2964                 sehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2965
2966         spin_lock_irqsave(ap->lock, flags);
2967         ap->pflags &= ~ATA_PFLAG_RESETTING;
2968         spin_unlock_irqrestore(ap->lock, flags);
2969
2970         return rc;
2971
2972  fail:
2973         /* if SCR isn't accessible on a fan-out port, PMP needs to be reset */
2974         if (!ata_is_host_link(link) &&
2975             sata_scr_read(link, SCR_STATUS, &sstatus))
2976                 rc = -ERESTART;
2977
2978         if (try >= max_tries) {
2979                 /*
2980                  * Thaw host port even if reset failed, so that the port
2981                  * can be retried on the next phy event.  This risks
2982                  * repeated EH runs but seems to be a better tradeoff than
2983                  * shutting down a port after a botched hotplug attempt.
2984                  */
2985                 if (ata_is_host_link(link))
2986                         ata_eh_thaw_port(ap);
2987                 goto out;
2988         }
2989
2990         now = jiffies;
2991         if (time_before(now, deadline)) {
2992                 unsigned long delta = deadline - now;
2993
2994                 ata_link_warn(failed_link,
2995                         "reset failed (errno=%d), retrying in %u secs\n",
2996                         rc, DIV_ROUND_UP(jiffies_to_msecs(delta), 1000));
2997
2998                 ata_eh_release(ap);
2999                 while (delta)
3000                         delta = schedule_timeout_uninterruptible(delta);
3001                 ata_eh_acquire(ap);
3002         }
3003
3004         /*
3005          * While disks spinup behind PMP, some controllers fail sending SRST.
3006          * They need to be reset - as well as the PMP - before retrying.
3007          */
3008         if (rc == -ERESTART) {
3009                 if (ata_is_host_link(link))
3010                         ata_eh_thaw_port(ap);
3011                 goto out;
3012         }
3013
3014         if (try == max_tries - 1) {
3015                 sata_down_spd_limit(link, 0);
3016                 if (slave)
3017                         sata_down_spd_limit(slave, 0);
3018         } else if (rc == -EPIPE)
3019                 sata_down_spd_limit(failed_link, 0);
3020
3021         if (hardreset)
3022                 reset = hardreset;
3023         goto retry;
3024 }
3025
3026 static inline void ata_eh_pull_park_action(struct ata_port *ap)
3027 {
3028         struct ata_link *link;
3029         struct ata_device *dev;
3030         unsigned long flags;
3031
3032         /*
3033          * This function can be thought of as an extended version of
3034          * ata_eh_about_to_do() specially crafted to accommodate the
3035          * requirements of ATA_EH_PARK handling. Since the EH thread
3036          * does not leave the do {} while () loop in ata_eh_recover as
3037          * long as the timeout for a park request to *one* device on
3038          * the port has not expired, and since we still want to pick
3039          * up park requests to other devices on the same port or
3040          * timeout updates for the same device, we have to pull
3041          * ATA_EH_PARK actions from eh_info into eh_context.i
3042          * ourselves at the beginning of each pass over the loop.
3043          *
3044          * Additionally, all write accesses to &ap->park_req_pending
3045          * through reinit_completion() (see below) or complete_all()
3046          * (see ata_scsi_park_store()) are protected by the host lock.
3047          * As a result we have that park_req_pending.done is zero on
3048          * exit from this function, i.e. when ATA_EH_PARK actions for
3049          * *all* devices on port ap have been pulled into the
3050          * respective eh_context structs. If, and only if,
3051          * park_req_pending.done is non-zero by the time we reach
3052          * wait_for_completion_timeout(), another ATA_EH_PARK action
3053          * has been scheduled for at least one of the devices on port
3054          * ap and we have to cycle over the do {} while () loop in
3055          * ata_eh_recover() again.
3056          */
3057
3058         spin_lock_irqsave(ap->lock, flags);
3059         reinit_completion(&ap->park_req_pending);
3060         ata_for_each_link(link, ap, EDGE) {
3061                 ata_for_each_dev(dev, link, ALL) {
3062                         struct ata_eh_info *ehi = &link->eh_info;
3063
3064                         link->eh_context.i.dev_action[dev->devno] |=
3065                                 ehi->dev_action[dev->devno] & ATA_EH_PARK;
3066                         ata_eh_clear_action(link, dev, ehi, ATA_EH_PARK);
3067                 }
3068         }
3069         spin_unlock_irqrestore(ap->lock, flags);
3070 }
3071
3072 static void ata_eh_park_issue_cmd(struct ata_device *dev, int park)
3073 {
3074         struct ata_eh_context *ehc = &dev->link->eh_context;
3075         struct ata_taskfile tf;
3076         unsigned int err_mask;
3077
3078         ata_tf_init(dev, &tf);
3079         if (park) {
3080                 ehc->unloaded_mask |= 1 << dev->devno;
3081                 tf.command = ATA_CMD_IDLEIMMEDIATE;
3082                 tf.feature = 0x44;
3083                 tf.lbal = 0x4c;
3084                 tf.lbam = 0x4e;
3085                 tf.lbah = 0x55;
3086         } else {
3087                 ehc->unloaded_mask &= ~(1 << dev->devno);
3088                 tf.command = ATA_CMD_CHK_POWER;
3089         }
3090
3091         tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
3092         tf.protocol = ATA_PROT_NODATA;
3093         err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
3094         if (park && (err_mask || tf.lbal != 0xc4)) {
3095                 ata_dev_err(dev, "head unload failed!\n");
3096                 ehc->unloaded_mask &= ~(1 << dev->devno);
3097         }
3098 }
3099
3100 static int ata_eh_revalidate_and_attach(struct ata_link *link,
3101                                         struct ata_device **r_failed_dev)
3102 {
3103         struct ata_port *ap = link->ap;
3104         struct ata_eh_context *ehc = &link->eh_context;
3105         struct ata_device *dev;
3106         unsigned int new_mask = 0;
3107         unsigned long flags;
3108         int rc = 0;
3109
3110         DPRINTK("ENTER\n");
3111
3112         /* For PATA drive side cable detection to work, IDENTIFY must
3113          * be done backwards such that PDIAG- is released by the slave
3114          * device before the master device is identified.
3115          */
3116         ata_for_each_dev(dev, link, ALL_REVERSE) {
3117                 unsigned int action = ata_eh_dev_action(dev);
3118                 unsigned int readid_flags = 0;
3119
3120                 if (ehc->i.flags & ATA_EHI_DID_RESET)
3121                         readid_flags |= ATA_READID_POSTRESET;
3122
3123                 if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
3124                         WARN_ON(dev->class == ATA_DEV_PMP);
3125
3126                         if (ata_phys_link_offline(ata_dev_phys_link(dev))) {
3127                                 rc = -EIO;
3128                                 goto err;
3129                         }
3130
3131                         ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
3132                         rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
3133                                                 readid_flags);
3134                         if (rc)
3135                                 goto err;
3136
3137                         ata_eh_done(link, dev, ATA_EH_REVALIDATE);
3138
3139                         /* Configuration may have changed, reconfigure
3140                          * transfer mode.
3141                          */
3142                         ehc->i.flags |= ATA_EHI_SETMODE;
3143
3144                         /* schedule the scsi_rescan_device() here */
3145                         schedule_work(&(ap->scsi_rescan_task));
3146                 } else if (dev->class == ATA_DEV_UNKNOWN &&
3147                            ehc->tries[dev->devno] &&
3148                            ata_class_enabled(ehc->classes[dev->devno])) {
3149                         /* Temporarily set dev->class, it will be
3150                          * permanently set once all configurations are
3151                          * complete.  This is necessary because new
3152                          * device configuration is done in two
3153                          * separate loops.
3154                          */
3155                         dev->class = ehc->classes[dev->devno];
3156
3157                         if (dev->class == ATA_DEV_PMP)
3158                                 rc = sata_pmp_attach(dev);
3159                         else
3160                                 rc = ata_dev_read_id(dev, &dev->class,
3161                                                      readid_flags, dev->id);
3162
3163                         /* read_id might have changed class, store and reset */
3164                         ehc->classes[dev->devno] = dev->class;
3165                         dev->class = ATA_DEV_UNKNOWN;
3166
3167                         switch (rc) {
3168                         case 0:
3169                                 /* clear error info accumulated during probe */
3170                                 ata_ering_clear(&dev->ering);
3171                                 new_mask |= 1 << dev->devno;
3172                                 break;
3173                         case -ENOENT:
3174                                 /* IDENTIFY was issued to non-existent
3175                                  * device.  No need to reset.  Just
3176                                  * thaw and ignore the device.
3177                                  */
3178                                 ata_eh_thaw_port(ap);
3179                                 break;
3180                         default:
3181                                 goto err;
3182                         }
3183                 }
3184         }
3185
3186         /* PDIAG- should have been released, ask cable type if post-reset */
3187         if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) {
3188                 if (ap->ops->cable_detect)
3189                         ap->cbl = ap->ops->cable_detect(ap);
3190                 ata_force_cbl(ap);
3191         }
3192
3193         /* Configure new devices forward such that user doesn't see
3194          * device detection messages backwards.
3195          */
3196         ata_for_each_dev(dev, link, ALL) {
3197                 if (!(new_mask & (1 << dev->devno)))
3198                         continue;
3199
3200                 dev->class = ehc->classes[dev->devno];
3201
3202                 if (dev->class == ATA_DEV_PMP)
3203                         continue;
3204
3205                 ehc->i.flags |= ATA_EHI_PRINTINFO;
3206                 rc = ata_dev_configure(dev);
3207                 ehc->i.flags &= ~ATA_EHI_PRINTINFO;
3208                 if (rc) {
3209                         dev->class = ATA_DEV_UNKNOWN;
3210                         goto err;
3211                 }
3212
3213                 spin_lock_irqsave(ap->lock, flags);
3214                 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
3215                 spin_unlock_irqrestore(ap->lock, flags);
3216
3217                 /* new device discovered, configure xfermode */
3218                 ehc->i.flags |= ATA_EHI_SETMODE;
3219         }
3220
3221         return 0;
3222
3223  err:
3224         *r_failed_dev = dev;
3225         DPRINTK("EXIT rc=%d\n", rc);
3226         return rc;
3227 }
3228
3229 /**
3230  *      ata_set_mode - Program timings and issue SET FEATURES - XFER
3231  *      @link: link on which timings will be programmed
3232  *      @r_failed_dev: out parameter for failed device
3233  *
3234  *      Set ATA device disk transfer mode (PIO3, UDMA6, etc.).  If
3235  *      ata_set_mode() fails, pointer to the failing device is
3236  *      returned in @r_failed_dev.
3237  *
3238  *      LOCKING:
3239  *      PCI/etc. bus probe sem.
3240  *
3241  *      RETURNS:
3242  *      0 on success, negative errno otherwise
3243  */
3244 int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3245 {
3246         struct ata_port *ap = link->ap;
3247         struct ata_device *dev;
3248         int rc;
3249
3250         /* if data transfer is verified, clear DUBIOUS_XFER on ering top */
3251         ata_for_each_dev(dev, link, ENABLED) {
3252                 if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
3253                         struct ata_ering_entry *ent;
3254
3255                         ent = ata_ering_top(&dev->ering);
3256                         if (ent)
3257                                 ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER;
3258                 }
3259         }
3260
3261         /* has private set_mode? */
3262         if (ap->ops->set_mode)
3263                 rc = ap->ops->set_mode(link, r_failed_dev);
3264         else
3265                 rc = ata_do_set_mode(link, r_failed_dev);
3266
3267         /* if transfer mode has changed, set DUBIOUS_XFER on device */
3268         ata_for_each_dev(dev, link, ENABLED) {
3269                 struct ata_eh_context *ehc = &link->eh_context;
3270                 u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
3271                 u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
3272
3273                 if (dev->xfer_mode != saved_xfer_mode ||
3274                     ata_ncq_enabled(dev) != saved_ncq)
3275                         dev->flags |= ATA_DFLAG_DUBIOUS_XFER;
3276         }
3277
3278         return rc;
3279 }
3280
3281 /**
3282  *      atapi_eh_clear_ua - Clear ATAPI UNIT ATTENTION after reset
3283  *      @dev: ATAPI device to clear UA for
3284  *
3285  *      Resets and other operations can make an ATAPI device raise
3286  *      UNIT ATTENTION which causes the next operation to fail.  This
3287  *      function clears UA.
3288  *
3289  *      LOCKING:
3290  *      EH context (may sleep).
3291  *
3292  *      RETURNS:
3293  *      0 on success, -errno on failure.
3294  */
3295 static int atapi_eh_clear_ua(struct ata_device *dev)
3296 {
3297         int i;
3298
3299         for (i = 0; i < ATA_EH_UA_TRIES; i++) {
3300                 u8 *sense_buffer = dev->link->ap->sector_buf;
3301                 u8 sense_key = 0;
3302                 unsigned int err_mask;
3303
3304                 err_mask = atapi_eh_tur(dev, &sense_key);
3305                 if (err_mask != 0 && err_mask != AC_ERR_DEV) {
3306                         ata_dev_warn(dev,
3307                                      "TEST_UNIT_READY failed (err_mask=0x%x)\n",
3308                                      err_mask);
3309                         return -EIO;
3310                 }
3311
3312                 if (!err_mask || sense_key != UNIT_ATTENTION)
3313                         return 0;
3314
3315                 err_mask = atapi_eh_request_sense(dev, sense_buffer, sense_key);
3316                 if (err_mask) {
3317                         ata_dev_warn(dev, "failed to clear "
3318                                 "UNIT ATTENTION (err_mask=0x%x)\n", err_mask);
3319                         return -EIO;
3320                 }
3321         }
3322
3323         ata_dev_warn(dev, "UNIT ATTENTION persists after %d tries\n",
3324                      ATA_EH_UA_TRIES);
3325
3326         return 0;
3327 }
3328
3329 /**
3330  *      ata_eh_maybe_retry_flush - Retry FLUSH if necessary
3331  *      @dev: ATA device which may need FLUSH retry
3332  *
3333  *      If @dev failed FLUSH, it needs to be reported upper layer
3334  *      immediately as it means that @dev failed to remap and already
3335  *      lost at least a sector and further FLUSH retrials won't make
3336  *      any difference to the lost sector.  However, if FLUSH failed
3337  *      for other reasons, for example transmission error, FLUSH needs
3338  *      to be retried.
3339  *
3340  *      This function determines whether FLUSH failure retry is
3341  *      necessary and performs it if so.
3342  *
3343  *      RETURNS:
3344  *      0 if EH can continue, -errno if EH needs to be repeated.
3345  */
3346 static int ata_eh_maybe_retry_flush(struct ata_device *dev)
3347 {
3348         struct ata_link *link = dev->link;
3349         struct ata_port *ap = link->ap;
3350         struct ata_queued_cmd *qc;
3351         struct ata_taskfile tf;
3352         unsigned int err_mask;
3353         int rc = 0;
3354
3355         /* did flush fail for this device? */
3356         if (!ata_tag_valid(link->active_tag))
3357                 return 0;
3358
3359         qc = __ata_qc_from_tag(ap, link->active_tag);
3360         if (qc->dev != dev || (qc->tf.command != ATA_CMD_FLUSH_EXT &&
3361                                qc->tf.command != ATA_CMD_FLUSH))
3362                 return 0;
3363
3364         /* if the device failed it, it should be reported to upper layers */
3365         if (qc->err_mask & AC_ERR_DEV)
3366                 return 0;
3367
3368         /* flush failed for some other reason, give it another shot */
3369         ata_tf_init(dev, &tf);
3370
3371         tf.command = qc->tf.command;
3372         tf.flags |= ATA_TFLAG_DEVICE;
3373         tf.protocol = ATA_PROT_NODATA;
3374
3375         ata_dev_warn(dev, "retrying FLUSH 0x%x Emask 0x%x\n",
3376                        tf.command, qc->err_mask);
3377
3378         err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
3379         if (!err_mask) {
3380                 /*
3381                  * FLUSH is complete but there's no way to
3382                  * successfully complete a failed command from EH.
3383                  * Making sure retry is allowed at least once and
3384                  * retrying it should do the trick - whatever was in
3385                  * the cache is already on the platter and this won't
3386                  * cause infinite loop.
3387                  */
3388                 qc->scsicmd->allowed = max(qc->scsicmd->allowed, 1);
3389         } else {
3390                 ata_dev_warn(dev, "FLUSH failed Emask 0x%x\n",
3391                                err_mask);
3392                 rc = -EIO;
3393
3394                 /* if device failed it, report it to upper layers */
3395                 if (err_mask & AC_ERR_DEV) {
3396                         qc->err_mask |= AC_ERR_DEV;
3397                         qc->result_tf = tf;
3398                         if (!(ap->pflags & ATA_PFLAG_FROZEN))
3399                                 rc = 0;
3400                 }
3401         }
3402         return rc;
3403 }
3404
3405 /**
3406  *      ata_eh_set_lpm - configure SATA interface power management
3407  *      @link: link to configure power management
3408  *      @policy: the link power management policy
3409  *      @r_failed_dev: out parameter for failed device
3410  *
3411  *      Enable SATA Interface power management.  This will enable
3412  *      Device Interface Power Management (DIPM) for min_power and
3413  *      medium_power_with_dipm policies, and then call driver specific
3414  *      callbacks for enabling Host Initiated Power management.
3415  *
3416  *      LOCKING:
3417  *      EH context.
3418  *
3419  *      RETURNS:
3420  *      0 on success, -errno on failure.
3421  */
3422 static int ata_eh_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
3423                           struct ata_device **r_failed_dev)
3424 {
3425         struct ata_port *ap = ata_is_host_link(link) ? link->ap : NULL;
3426         struct ata_eh_context *ehc = &link->eh_context;
3427         struct ata_device *dev, *link_dev = NULL, *lpm_dev = NULL;
3428         enum ata_lpm_policy old_policy = link->lpm_policy;
3429         bool no_dipm = link->ap->flags & ATA_FLAG_NO_DIPM;
3430         unsigned int hints = ATA_LPM_EMPTY | ATA_LPM_HIPM;
3431         unsigned int err_mask;
3432         int rc;
3433
3434         /* if the link or host doesn't do LPM, noop */
3435         if ((link->flags & ATA_LFLAG_NO_LPM) || (ap && !ap->ops->set_lpm))
3436                 return 0;
3437
3438         /*
3439          * DIPM is enabled only for MIN_POWER as some devices
3440          * misbehave when the host NACKs transition to SLUMBER.  Order
3441          * device and link configurations such that the host always
3442          * allows DIPM requests.
3443          */
3444         ata_for_each_dev(dev, link, ENABLED) {
3445                 bool hipm = ata_id_has_hipm(dev->id);
3446                 bool dipm = ata_id_has_dipm(dev->id) && !no_dipm;
3447
3448                 /* find the first enabled and LPM enabled devices */
3449                 if (!link_dev)
3450                         link_dev = dev;
3451
3452                 if (!lpm_dev && (hipm || dipm))
3453                         lpm_dev = dev;
3454
3455                 hints &= ~ATA_LPM_EMPTY;
3456                 if (!hipm)
3457                         hints &= ~ATA_LPM_HIPM;
3458
3459                 /* disable DIPM before changing link config */
3460                 if (policy < ATA_LPM_MED_POWER_WITH_DIPM && dipm) {
3461                         err_mask = ata_dev_set_feature(dev,
3462                                         SETFEATURES_SATA_DISABLE, SATA_DIPM);
3463                         if (err_mask && err_mask != AC_ERR_DEV) {
3464                                 ata_dev_warn(dev,
3465                                              "failed to disable DIPM, Emask 0x%x\n",
3466                                              err_mask);
3467                                 rc = -EIO;
3468                                 goto fail;
3469                         }
3470                 }
3471         }
3472
3473         if (ap) {
3474                 rc = ap->ops->set_lpm(link, policy, hints);
3475                 if (!rc && ap->slave_link)
3476                         rc = ap->ops->set_lpm(ap->slave_link, policy, hints);
3477         } else
3478                 rc = sata_pmp_set_lpm(link, policy, hints);
3479
3480         /*
3481          * Attribute link config failure to the first (LPM) enabled
3482          * device on the link.
3483          */
3484         if (rc) {
3485                 if (rc == -EOPNOTSUPP) {
3486                         link->flags |= ATA_LFLAG_NO_LPM;
3487                         return 0;
3488                 }
3489                 dev = lpm_dev ? lpm_dev : link_dev;
3490                 goto fail;
3491         }
3492
3493         /*
3494          * Low level driver acked the transition.  Issue DIPM command
3495          * with the new policy set.
3496          */
3497         link->lpm_policy = policy;
3498         if (ap && ap->slave_link)
3499                 ap->slave_link->lpm_policy = policy;
3500
3501         /* host config updated, enable DIPM if transitioning to MIN_POWER */
3502         ata_for_each_dev(dev, link, ENABLED) {
3503                 if (policy >= ATA_LPM_MED_POWER_WITH_DIPM && !no_dipm &&
3504                     ata_id_has_dipm(dev->id)) {
3505                         err_mask = ata_dev_set_feature(dev,
3506                                         SETFEATURES_SATA_ENABLE, SATA_DIPM);
3507                         if (err_mask && err_mask != AC_ERR_DEV) {
3508                                 ata_dev_warn(dev,
3509                                         "failed to enable DIPM, Emask 0x%x\n",
3510                                         err_mask);
3511                                 rc = -EIO;
3512                                 goto fail;
3513                         }
3514                 }
3515         }
3516
3517         link->last_lpm_change = jiffies;
3518         link->flags |= ATA_LFLAG_CHANGED;
3519
3520         return 0;
3521
3522 fail:
3523         /* restore the old policy */
3524         link->lpm_policy = old_policy;
3525         if (ap && ap->slave_link)
3526                 ap->slave_link->lpm_policy = old_policy;
3527
3528         /* if no device or only one more chance is left, disable LPM */
3529         if (!dev || ehc->tries[dev->devno] <= 2) {
3530                 ata_link_warn(link, "disabling LPM on the link\n");
3531                 link->flags |= ATA_LFLAG_NO_LPM;
3532         }
3533         if (r_failed_dev)
3534                 *r_failed_dev = dev;
3535         return rc;
3536 }
3537
3538 int ata_link_nr_enabled(struct ata_link *link)
3539 {
3540         struct ata_device *dev;
3541         int cnt = 0;
3542
3543         ata_for_each_dev(dev, link, ENABLED)
3544                 cnt++;
3545         return cnt;
3546 }
3547
3548 static int ata_link_nr_vacant(struct ata_link *link)
3549 {
3550         struct ata_device *dev;
3551         int cnt = 0;
3552
3553         ata_for_each_dev(dev, link, ALL)
3554                 if (dev->class == ATA_DEV_UNKNOWN)
3555                         cnt++;
3556         return cnt;
3557 }
3558
3559 static int ata_eh_skip_recovery(struct ata_link *link)
3560 {
3561         struct ata_port *ap = link->ap;
3562         struct ata_eh_context *ehc = &link->eh_context;
3563         struct ata_device *dev;
3564
3565         /* skip disabled links */
3566         if (link->flags & ATA_LFLAG_DISABLED)
3567                 return 1;
3568
3569         /* skip if explicitly requested */
3570         if (ehc->i.flags & ATA_EHI_NO_RECOVERY)
3571                 return 1;
3572
3573         /* thaw frozen port and recover failed devices */
3574         if ((ap->pflags & ATA_PFLAG_FROZEN) || ata_link_nr_enabled(link))
3575                 return 0;
3576
3577         /* reset at least once if reset is requested */
3578         if ((ehc->i.action & ATA_EH_RESET) &&
3579             !(ehc->i.flags & ATA_EHI_DID_RESET))
3580                 return 0;
3581
3582         /* skip if class codes for all vacant slots are ATA_DEV_NONE */
3583         ata_for_each_dev(dev, link, ALL) {
3584                 if (dev->class == ATA_DEV_UNKNOWN &&
3585                     ehc->classes[dev->devno] != ATA_DEV_NONE)
3586                         return 0;
3587         }
3588
3589         return 1;
3590 }
3591
3592 static int ata_count_probe_trials_cb(struct ata_ering_entry *ent, void *void_arg)
3593 {
3594         u64 interval = msecs_to_jiffies(ATA_EH_PROBE_TRIAL_INTERVAL);
3595         u64 now = get_jiffies_64();
3596         int *trials = void_arg;
3597
3598         if ((ent->eflags & ATA_EFLAG_OLD_ER) ||
3599             (ent->timestamp < now - min(now, interval)))
3600                 return -1;
3601
3602         (*trials)++;
3603         return 0;
3604 }
3605
3606 static int ata_eh_schedule_probe(struct ata_device *dev)
3607 {
3608         struct ata_eh_context *ehc = &dev->link->eh_context;
3609         struct ata_link *link = ata_dev_phys_link(dev);
3610         int trials = 0;
3611
3612         if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
3613             (ehc->did_probe_mask & (1 << dev->devno)))
3614                 return 0;
3615
3616         ata_eh_detach_dev(dev);
3617         ata_dev_init(dev);
3618         ehc->did_probe_mask |= (1 << dev->devno);
3619         ehc->i.action |= ATA_EH_RESET;
3620         ehc->saved_xfer_mode[dev->devno] = 0;
3621         ehc->saved_ncq_enabled &= ~(1 << dev->devno);
3622
3623         /* the link maybe in a deep sleep, wake it up */
3624         if (link->lpm_policy > ATA_LPM_MAX_POWER) {
3625                 if (ata_is_host_link(link))
3626                         link->ap->ops->set_lpm(link, ATA_LPM_MAX_POWER,
3627                                                ATA_LPM_EMPTY);
3628                 else
3629                         sata_pmp_set_lpm(link, ATA_LPM_MAX_POWER,
3630                                          ATA_LPM_EMPTY);
3631         }
3632
3633         /* Record and count probe trials on the ering.  The specific
3634          * error mask used is irrelevant.  Because a successful device
3635          * detection clears the ering, this count accumulates only if
3636          * there are consecutive failed probes.
3637          *
3638          * If the count is equal to or higher than ATA_EH_PROBE_TRIALS
3639          * in the last ATA_EH_PROBE_TRIAL_INTERVAL, link speed is
3640          * forced to 1.5Gbps.
3641          *
3642          * This is to work around cases where failed link speed
3643          * negotiation results in device misdetection leading to
3644          * infinite DEVXCHG or PHRDY CHG events.
3645          */
3646         ata_ering_record(&dev->ering, 0, AC_ERR_OTHER);
3647         ata_ering_map(&dev->ering, ata_count_probe_trials_cb, &trials);
3648
3649         if (trials > ATA_EH_PROBE_TRIALS)
3650                 sata_down_spd_limit(link, 1);
3651
3652         return 1;
3653 }
3654
3655 static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
3656 {
3657         struct ata_eh_context *ehc = &dev->link->eh_context;
3658
3659         /* -EAGAIN from EH routine indicates retry without prejudice.
3660          * The requester is responsible for ensuring forward progress.
3661          */
3662         if (err != -EAGAIN)
3663                 ehc->tries[dev->devno]--;
3664
3665         switch (err) {
3666         case -ENODEV:
3667                 /* device missing or wrong IDENTIFY data, schedule probing */
3668                 ehc->i.probe_mask |= (1 << dev->devno);
3669                 /* fall through */
3670         case -EINVAL:
3671                 /* give it just one more chance */
3672                 ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
3673                 /* fall through */
3674         case -EIO:
3675                 if (ehc->tries[dev->devno] == 1) {
3676                         /* This is the last chance, better to slow
3677                          * down than lose it.
3678                          */
3679                         sata_down_spd_limit(ata_dev_phys_link(dev), 0);
3680                         if (dev->pio_mode > XFER_PIO_0)
3681                                 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
3682                 }
3683         }
3684
3685         if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
3686                 /* disable device if it has used up all its chances */
3687                 ata_dev_disable(dev);
3688
3689                 /* detach if offline */
3690                 if (ata_phys_link_offline(ata_dev_phys_link(dev)))
3691                         ata_eh_detach_dev(dev);
3692
3693                 /* schedule probe if necessary */
3694                 if (ata_eh_schedule_probe(dev)) {
3695                         ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
3696                         memset(ehc->cmd_timeout_idx[dev->devno], 0,
3697                                sizeof(ehc->cmd_timeout_idx[dev->devno]));
3698                 }
3699
3700                 return 1;
3701         } else {
3702                 ehc->i.action |= ATA_EH_RESET;
3703                 return 0;
3704         }
3705 }
3706
3707 /**
3708  *      ata_eh_recover - recover host port after error
3709  *      @ap: host port to recover
3710  *      @prereset: prereset method (can be NULL)
3711  *      @softreset: softreset method (can be NULL)
3712  *      @hardreset: hardreset method (can be NULL)
3713  *      @postreset: postreset method (can be NULL)
3714  *      @r_failed_link: out parameter for failed link
3715  *
3716  *      This is the alpha and omega, eum and yang, heart and soul of
3717  *      libata exception handling.  On entry, actions required to
3718  *      recover each link and hotplug requests are recorded in the
3719  *      link's eh_context.  This function executes all the operations
3720  *      with appropriate retrials and fallbacks to resurrect failed
3721  *      devices, detach goners and greet newcomers.
3722  *
3723  *      LOCKING:
3724  *      Kernel thread context (may sleep).
3725  *
3726  *      RETURNS:
3727  *      0 on success, -errno on failure.
3728  */
3729 int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
3730                    ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
3731                    ata_postreset_fn_t postreset,
3732                    struct ata_link **r_failed_link)
3733 {
3734         struct ata_link *link;
3735         struct ata_device *dev;
3736         int rc, nr_fails;
3737         unsigned long flags, deadline;
3738
3739         DPRINTK("ENTER\n");
3740
3741         /* prep for recovery */
3742         ata_for_each_link(link, ap, EDGE) {
3743                 struct ata_eh_context *ehc = &link->eh_context;
3744
3745                 /* re-enable link? */
3746                 if (ehc->i.action & ATA_EH_ENABLE_LINK) {
3747                         ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
3748                         spin_lock_irqsave(ap->lock, flags);
3749                         link->flags &= ~ATA_LFLAG_DISABLED;
3750                         spin_unlock_irqrestore(ap->lock, flags);
3751                         ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
3752                 }
3753
3754                 ata_for_each_dev(dev, link, ALL) {
3755                         if (link->flags & ATA_LFLAG_NO_RETRY)
3756                                 ehc->tries[dev->devno] = 1;
3757                         else
3758                                 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
3759
3760                         /* collect port action mask recorded in dev actions */
3761                         ehc->i.action |= ehc->i.dev_action[dev->devno] &
3762                                          ~ATA_EH_PERDEV_MASK;
3763                         ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
3764
3765                         /* process hotplug request */
3766                         if (dev->flags & ATA_DFLAG_DETACH)
3767                                 ata_eh_detach_dev(dev);
3768
3769                         /* schedule probe if necessary */
3770                         if (!ata_dev_enabled(dev))
3771                                 ata_eh_schedule_probe(dev);
3772                 }
3773         }
3774
3775  retry:
3776         rc = 0;
3777
3778         /* if UNLOADING, finish immediately */
3779         if (ap->pflags & ATA_PFLAG_UNLOADING)
3780                 goto out;
3781
3782         /* prep for EH */
3783         ata_for_each_link(link, ap, EDGE) {
3784                 struct ata_eh_context *ehc = &link->eh_context;
3785
3786                 /* skip EH if possible. */
3787                 if (ata_eh_skip_recovery(link))
3788                         ehc->i.action = 0;
3789
3790                 ata_for_each_dev(dev, link, ALL)
3791                         ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
3792         }
3793
3794         /* reset */
3795         ata_for_each_link(link, ap, EDGE) {
3796                 struct ata_eh_context *ehc = &link->eh_context;
3797
3798                 if (!(ehc->i.action & ATA_EH_RESET))
3799                         continue;
3800
3801                 rc = ata_eh_reset(link, ata_link_nr_vacant(link),
3802                                   prereset, softreset, hardreset, postreset);
3803                 if (rc) {
3804                         ata_link_err(link, "reset failed, giving up\n");
3805                         goto out;
3806                 }
3807         }
3808
3809         do {
3810                 unsigned long now;
3811
3812                 /*
3813                  * clears ATA_EH_PARK in eh_info and resets
3814                  * ap->park_req_pending
3815                  */
3816                 ata_eh_pull_park_action(ap);
3817
3818                 deadline = jiffies;
3819                 ata_for_each_link(link, ap, EDGE) {
3820                         ata_for_each_dev(dev, link, ALL) {
3821                                 struct ata_eh_context *ehc = &link->eh_context;
3822                                 unsigned long tmp;
3823
3824                                 if (dev->class != ATA_DEV_ATA &&
3825                                     dev->class != ATA_DEV_ZAC)
3826                                         continue;
3827                                 if (!(ehc->i.dev_action[dev->devno] &
3828                                       ATA_EH_PARK))
3829                                         continue;
3830                                 tmp = dev->unpark_deadline;
3831                                 if (time_before(deadline, tmp))
3832                                         deadline = tmp;
3833                                 else if (time_before_eq(tmp, jiffies))
3834                                         continue;
3835                                 if (ehc->unloaded_mask & (1 << dev->devno))
3836                                         continue;
3837
3838                                 ata_eh_park_issue_cmd(dev, 1);
3839                         }
3840                 }
3841
3842                 now = jiffies;
3843                 if (time_before_eq(deadline, now))
3844                         break;
3845
3846                 ata_eh_release(ap);
3847                 deadline = wait_for_completion_timeout(&ap->park_req_pending,
3848                                                        deadline - now);
3849                 ata_eh_acquire(ap);
3850         } while (deadline);
3851         ata_for_each_link(link, ap, EDGE) {
3852                 ata_for_each_dev(dev, link, ALL) {
3853                         if (!(link->eh_context.unloaded_mask &
3854                               (1 << dev->devno)))
3855                                 continue;
3856
3857                         ata_eh_park_issue_cmd(dev, 0);
3858                         ata_eh_done(link, dev, ATA_EH_PARK);
3859                 }
3860         }
3861
3862         /* the rest */
3863         nr_fails = 0;
3864         ata_for_each_link(link, ap, PMP_FIRST) {
3865                 struct ata_eh_context *ehc = &link->eh_context;
3866
3867                 if (sata_pmp_attached(ap) && ata_is_host_link(link))
3868                         goto config_lpm;
3869
3870                 /* revalidate existing devices and attach new ones */
3871                 rc = ata_eh_revalidate_and_attach(link, &dev);
3872                 if (rc)
3873                         goto rest_fail;
3874
3875                 /* if PMP got attached, return, pmp EH will take care of it */
3876                 if (link->device->class == ATA_DEV_PMP) {
3877                         ehc->i.action = 0;
3878                         return 0;
3879                 }
3880
3881                 /* configure transfer mode if necessary */
3882                 if (ehc->i.flags & ATA_EHI_SETMODE) {
3883                         rc = ata_set_mode(link, &dev);
3884                         if (rc)
3885                                 goto rest_fail;
3886                         ehc->i.flags &= ~ATA_EHI_SETMODE;
3887                 }
3888
3889                 /* If reset has been issued, clear UA to avoid
3890                  * disrupting the current users of the device.
3891                  */
3892                 if (ehc->i.flags & ATA_EHI_DID_RESET) {
3893                         ata_for_each_dev(dev, link, ALL) {
3894                                 if (dev->class != ATA_DEV_ATAPI)
3895                                         continue;
3896                                 rc = atapi_eh_clear_ua(dev);
3897                                 if (rc)
3898                                         goto rest_fail;
3899                                 if (zpodd_dev_enabled(dev))
3900                                         zpodd_post_poweron(dev);
3901                         }
3902                 }
3903
3904                 /* retry flush if necessary */
3905                 ata_for_each_dev(dev, link, ALL) {
3906                         if (dev->class != ATA_DEV_ATA &&
3907                             dev->class != ATA_DEV_ZAC)
3908                                 continue;
3909                         rc = ata_eh_maybe_retry_flush(dev);
3910                         if (rc)
3911                                 goto rest_fail;
3912                 }
3913
3914         config_lpm:
3915                 /* configure link power saving */
3916                 if (link->lpm_policy != ap->target_lpm_policy) {
3917                         rc = ata_eh_set_lpm(link, ap->target_lpm_policy, &dev);
3918                         if (rc)
3919                                 goto rest_fail;
3920                 }
3921
3922                 /* this link is okay now */
3923                 ehc->i.flags = 0;
3924                 continue;
3925
3926         rest_fail:
3927                 nr_fails++;
3928                 if (dev)
3929                         ata_eh_handle_dev_fail(dev, rc);
3930
3931                 if (ap->pflags & ATA_PFLAG_FROZEN) {
3932                         /* PMP reset requires working host port.
3933                          * Can't retry if it's frozen.
3934                          */
3935                         if (sata_pmp_attached(ap))
3936                                 goto out;
3937                         break;
3938                 }
3939         }
3940
3941         if (nr_fails)
3942                 goto retry;
3943
3944  out:
3945         if (rc && r_failed_link)
3946                 *r_failed_link = link;
3947
3948         DPRINTK("EXIT, rc=%d\n", rc);
3949         return rc;
3950 }
3951
3952 /**
3953  *      ata_eh_finish - finish up EH
3954  *      @ap: host port to finish EH for
3955  *
3956  *      Recovery is complete.  Clean up EH states and retry or finish
3957  *      failed qcs.
3958  *
3959  *      LOCKING:
3960  *      None.
3961  */
3962 void ata_eh_finish(struct ata_port *ap)
3963 {
3964         struct ata_queued_cmd *qc;
3965         int tag;
3966
3967         /* retry or finish qcs */
3968         ata_qc_for_each_raw(ap, qc, tag) {
3969                 if (!(qc->flags & ATA_QCFLAG_FAILED))
3970                         continue;
3971
3972                 if (qc->err_mask) {
3973                         /* FIXME: Once EH migration is complete,
3974                          * generate sense data in this function,
3975                          * considering both err_mask and tf.
3976                          */
3977                         if (qc->flags & ATA_QCFLAG_RETRY)
3978                                 ata_eh_qc_retry(qc);
3979                         else
3980                                 ata_eh_qc_complete(qc);
3981                 } else {
3982                         if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
3983                                 ata_eh_qc_complete(qc);
3984                         } else {
3985                                 /* feed zero TF to sense generation */
3986                                 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
3987                                 ata_eh_qc_retry(qc);
3988                         }
3989                 }
3990         }
3991
3992         /* make sure nr_active_links is zero after EH */
3993         WARN_ON(ap->nr_active_links);
3994         ap->nr_active_links = 0;
3995 }
3996
3997 /**
3998  *      ata_do_eh - do standard error handling
3999  *      @ap: host port to handle error for
4000  *
4001  *      @prereset: prereset method (can be NULL)
4002  *      @softreset: softreset method (can be NULL)
4003  *      @hardreset: hardreset method (can be NULL)
4004  *      @postreset: postreset method (can be NULL)
4005  *
4006  *      Perform standard error handling sequence.
4007  *
4008  *      LOCKING:
4009  *      Kernel thread context (may sleep).
4010  */
4011 void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
4012                ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
4013                ata_postreset_fn_t postreset)
4014 {
4015         struct ata_device *dev;
4016         int rc;
4017
4018         ata_eh_autopsy(ap);
4019         ata_eh_report(ap);
4020
4021         rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
4022                             NULL);
4023         if (rc) {
4024                 ata_for_each_dev(dev, &ap->link, ALL)
4025                         ata_dev_disable(dev);
4026         }
4027
4028         ata_eh_finish(ap);
4029 }
4030
4031 /**
4032  *      ata_std_error_handler - standard error handler
4033  *      @ap: host port to handle error for
4034  *
4035  *      Standard error handler
4036  *
4037  *      LOCKING:
4038  *      Kernel thread context (may sleep).
4039  */
4040 void ata_std_error_handler(struct ata_port *ap)
4041 {
4042         struct ata_port_operations *ops = ap->ops;
4043         ata_reset_fn_t hardreset = ops->hardreset;
4044
4045         /* ignore built-in hardreset if SCR access is not available */
4046         if (hardreset == sata_std_hardreset && !sata_scr_valid(&ap->link))
4047                 hardreset = NULL;
4048
4049         ata_do_eh(ap, ops->prereset, ops->softreset, hardreset, ops->postreset);
4050 }
4051
4052 #ifdef CONFIG_PM
4053 /**
4054  *      ata_eh_handle_port_suspend - perform port suspend operation
4055  *      @ap: port to suspend
4056  *
4057  *      Suspend @ap.
4058  *
4059  *      LOCKING:
4060  *      Kernel thread context (may sleep).
4061  */
4062 static void ata_eh_handle_port_suspend(struct ata_port *ap)
4063 {
4064         unsigned long flags;
4065         int rc = 0;
4066         struct ata_device *dev;
4067
4068         /* are we suspending? */
4069         spin_lock_irqsave(ap->lock, flags);
4070         if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
4071             ap->pm_mesg.event & PM_EVENT_RESUME) {
4072                 spin_unlock_irqrestore(ap->lock, flags);
4073                 return;
4074         }
4075         spin_unlock_irqrestore(ap->lock, flags);
4076
4077         WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
4078
4079         /*
4080          * If we have a ZPODD attached, check its zero
4081          * power ready status before the port is frozen.
4082          * Only needed for runtime suspend.
4083          */
4084         if (PMSG_IS_AUTO(ap->pm_mesg)) {
4085                 ata_for_each_dev(dev, &ap->link, ENABLED) {
4086                         if (zpodd_dev_enabled(dev))
4087                                 zpodd_on_suspend(dev);
4088                 }
4089         }
4090
4091         /* tell ACPI we're suspending */
4092         rc = ata_acpi_on_suspend(ap);
4093         if (rc)
4094                 goto out;
4095
4096         /* suspend */
4097         ata_eh_freeze_port(ap);
4098
4099         if (ap->ops->port_suspend)
4100                 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
4101
4102         ata_acpi_set_state(ap, ap->pm_mesg);
4103  out:
4104         /* update the flags */
4105         spin_lock_irqsave(ap->lock, flags);
4106
4107         ap->pflags &= ~ATA_PFLAG_PM_PENDING;
4108         if (rc == 0)
4109                 ap->pflags |= ATA_PFLAG_SUSPENDED;
4110         else if (ap->pflags & ATA_PFLAG_FROZEN)
4111                 ata_port_schedule_eh(ap);
4112
4113         spin_unlock_irqrestore(ap->lock, flags);
4114
4115         return;
4116 }
4117
4118 /**
4119  *      ata_eh_handle_port_resume - perform port resume operation
4120  *      @ap: port to resume
4121  *
4122  *      Resume @ap.
4123  *
4124  *      LOCKING:
4125  *      Kernel thread context (may sleep).
4126  */
4127 static void ata_eh_handle_port_resume(struct ata_port *ap)
4128 {
4129         struct ata_link *link;
4130         struct ata_device *dev;
4131         unsigned long flags;
4132
4133         /* are we resuming? */
4134         spin_lock_irqsave(ap->lock, flags);
4135         if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
4136             !(ap->pm_mesg.event & PM_EVENT_RESUME)) {
4137                 spin_unlock_irqrestore(ap->lock, flags);
4138                 return;
4139         }
4140         spin_unlock_irqrestore(ap->lock, flags);
4141
4142         WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
4143
4144         /*
4145          * Error timestamps are in jiffies which doesn't run while
4146          * suspended and PHY events during resume isn't too uncommon.
4147          * When the two are combined, it can lead to unnecessary speed
4148          * downs if the machine is suspended and resumed repeatedly.
4149          * Clear error history.
4150          */
4151         ata_for_each_link(link, ap, HOST_FIRST)
4152                 ata_for_each_dev(dev, link, ALL)
4153                         ata_ering_clear(&dev->ering);
4154
4155         ata_acpi_set_state(ap, ap->pm_mesg);
4156
4157         if (ap->ops->port_resume)
4158                 ap->ops->port_resume(ap);
4159
4160         /* tell ACPI that we're resuming */
4161         ata_acpi_on_resume(ap);
4162
4163         /* update the flags */
4164         spin_lock_irqsave(ap->lock, flags);
4165         ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
4166         spin_unlock_irqrestore(ap->lock, flags);
4167 }
4168 #endif /* CONFIG_PM */