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