libata: implement ATA_QCFLAG_RETRY
[linux-2.6/verdex.git] / drivers / ata / libata-eh.c
blobcc8548e1572acd1f45841430225f8ac41f8f9948
1 /*
2 * libata-eh.c - libata error handling
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
8 * Copyright 2006 Tejun Heo <htejun@gmail.com>
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.
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.
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.
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
35 #include <linux/kernel.h>
36 #include <linux/pci.h>
37 #include <scsi/scsi.h>
38 #include <scsi/scsi_host.h>
39 #include <scsi/scsi_eh.h>
40 #include <scsi/scsi_device.h>
41 #include <scsi/scsi_cmnd.h>
42 #include "../scsi/scsi_transport_api.h"
44 #include <linux/libata.h>
46 #include "libata.h"
48 enum {
49 /* speed down verdicts */
50 ATA_EH_SPDN_NCQ_OFF = (1 << 0),
51 ATA_EH_SPDN_SPEED_DOWN = (1 << 1),
52 ATA_EH_SPDN_FALLBACK_TO_PIO = (1 << 2),
53 ATA_EH_SPDN_KEEP_ERRORS = (1 << 3),
55 /* error flags */
56 ATA_EFLAG_IS_IO = (1 << 0),
57 ATA_EFLAG_DUBIOUS_XFER = (1 << 1),
59 /* error categories */
60 ATA_ECAT_NONE = 0,
61 ATA_ECAT_ATA_BUS = 1,
62 ATA_ECAT_TOUT_HSM = 2,
63 ATA_ECAT_UNK_DEV = 3,
64 ATA_ECAT_DUBIOUS_NONE = 4,
65 ATA_ECAT_DUBIOUS_ATA_BUS = 5,
66 ATA_ECAT_DUBIOUS_TOUT_HSM = 6,
67 ATA_ECAT_DUBIOUS_UNK_DEV = 7,
68 ATA_ECAT_NR = 8,
71 /* Waiting in ->prereset can never be reliable. It's sometimes nice
72 * to wait there but it can't be depended upon; otherwise, we wouldn't
73 * be resetting. Just give it enough time for most drives to spin up.
75 enum {
76 ATA_EH_PRERESET_TIMEOUT = 10 * HZ,
77 ATA_EH_FASTDRAIN_INTERVAL = 3 * HZ,
80 /* The following table determines how we sequence resets. Each entry
81 * represents timeout for that try. The first try can be soft or
82 * hardreset. All others are hardreset if available. In most cases
83 * the first reset w/ 10sec timeout should succeed. Following entries
84 * are mostly for error handling, hotplug and retarded devices.
86 static const unsigned long ata_eh_reset_timeouts[] = {
87 10 * HZ, /* most drives spin up by 10sec */
88 10 * HZ, /* > 99% working drives spin up before 20sec */
89 35 * HZ, /* give > 30 secs of idleness for retarded devices */
90 5 * HZ, /* and sweet one last chance */
91 /* > 1 min has elapsed, give up */
94 static void __ata_port_freeze(struct ata_port *ap);
95 #ifdef CONFIG_PM
96 static void ata_eh_handle_port_suspend(struct ata_port *ap);
97 static void ata_eh_handle_port_resume(struct ata_port *ap);
98 #else /* CONFIG_PM */
99 static void ata_eh_handle_port_suspend(struct ata_port *ap)
102 static void ata_eh_handle_port_resume(struct ata_port *ap)
104 #endif /* CONFIG_PM */
106 static void __ata_ehi_pushv_desc(struct ata_eh_info *ehi, const char *fmt,
107 va_list args)
109 ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
110 ATA_EH_DESC_LEN - ehi->desc_len,
111 fmt, args);
115 * __ata_ehi_push_desc - push error description without adding separator
116 * @ehi: target EHI
117 * @fmt: printf format string
119 * Format string according to @fmt and append it to @ehi->desc.
121 * LOCKING:
122 * spin_lock_irqsave(host lock)
124 void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
126 va_list args;
128 va_start(args, fmt);
129 __ata_ehi_pushv_desc(ehi, fmt, args);
130 va_end(args);
134 * ata_ehi_push_desc - push error description with separator
135 * @ehi: target EHI
136 * @fmt: printf format string
138 * Format string according to @fmt and append it to @ehi->desc.
139 * If @ehi->desc is not empty, ", " is added in-between.
141 * LOCKING:
142 * spin_lock_irqsave(host lock)
144 void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
146 va_list args;
148 if (ehi->desc_len)
149 __ata_ehi_push_desc(ehi, ", ");
151 va_start(args, fmt);
152 __ata_ehi_pushv_desc(ehi, fmt, args);
153 va_end(args);
157 * ata_ehi_clear_desc - clean error description
158 * @ehi: target EHI
160 * Clear @ehi->desc.
162 * LOCKING:
163 * spin_lock_irqsave(host lock)
165 void ata_ehi_clear_desc(struct ata_eh_info *ehi)
167 ehi->desc[0] = '\0';
168 ehi->desc_len = 0;
172 * ata_port_desc - append port description
173 * @ap: target ATA port
174 * @fmt: printf format string
176 * Format string according to @fmt and append it to port
177 * description. If port description is not empty, " " is added
178 * in-between. This function is to be used while initializing
179 * ata_host. The description is printed on host registration.
181 * LOCKING:
182 * None.
184 void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
186 va_list args;
188 WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
190 if (ap->link.eh_info.desc_len)
191 __ata_ehi_push_desc(&ap->link.eh_info, " ");
193 va_start(args, fmt);
194 __ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
195 va_end(args);
198 #ifdef CONFIG_PCI
201 * ata_port_pbar_desc - append PCI BAR description
202 * @ap: target ATA port
203 * @bar: target PCI BAR
204 * @offset: offset into PCI BAR
205 * @name: name of the area
207 * If @offset is negative, this function formats a string which
208 * contains the name, address, size and type of the BAR and
209 * appends it to the port description. If @offset is zero or
210 * positive, only name and offsetted address is appended.
212 * LOCKING:
213 * None.
215 void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
216 const char *name)
218 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
219 char *type = "";
220 unsigned long long start, len;
222 if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
223 type = "m";
224 else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
225 type = "i";
227 start = (unsigned long long)pci_resource_start(pdev, bar);
228 len = (unsigned long long)pci_resource_len(pdev, bar);
230 if (offset < 0)
231 ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
232 else
233 ata_port_desc(ap, "%s 0x%llx", name,
234 start + (unsigned long long)offset);
237 #endif /* CONFIG_PCI */
239 static void ata_ering_record(struct ata_ering *ering, unsigned int eflags,
240 unsigned int err_mask)
242 struct ata_ering_entry *ent;
244 WARN_ON(!err_mask);
246 ering->cursor++;
247 ering->cursor %= ATA_ERING_SIZE;
249 ent = &ering->ring[ering->cursor];
250 ent->eflags = eflags;
251 ent->err_mask = err_mask;
252 ent->timestamp = get_jiffies_64();
255 static struct ata_ering_entry *ata_ering_top(struct ata_ering *ering)
257 struct ata_ering_entry *ent = &ering->ring[ering->cursor];
259 if (ent->err_mask)
260 return ent;
261 return NULL;
264 static void ata_ering_clear(struct ata_ering *ering)
266 memset(ering, 0, sizeof(*ering));
269 static int ata_ering_map(struct ata_ering *ering,
270 int (*map_fn)(struct ata_ering_entry *, void *),
271 void *arg)
273 int idx, rc = 0;
274 struct ata_ering_entry *ent;
276 idx = ering->cursor;
277 do {
278 ent = &ering->ring[idx];
279 if (!ent->err_mask)
280 break;
281 rc = map_fn(ent, arg);
282 if (rc)
283 break;
284 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
285 } while (idx != ering->cursor);
287 return rc;
290 static unsigned int ata_eh_dev_action(struct ata_device *dev)
292 struct ata_eh_context *ehc = &dev->link->eh_context;
294 return ehc->i.action | ehc->i.dev_action[dev->devno];
297 static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
298 struct ata_eh_info *ehi, unsigned int action)
300 struct ata_device *tdev;
302 if (!dev) {
303 ehi->action &= ~action;
304 ata_link_for_each_dev(tdev, link)
305 ehi->dev_action[tdev->devno] &= ~action;
306 } else {
307 /* doesn't make sense for port-wide EH actions */
308 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
310 /* break ehi->action into ehi->dev_action */
311 if (ehi->action & action) {
312 ata_link_for_each_dev(tdev, link)
313 ehi->dev_action[tdev->devno] |=
314 ehi->action & action;
315 ehi->action &= ~action;
318 /* turn off the specified per-dev action */
319 ehi->dev_action[dev->devno] &= ~action;
324 * ata_scsi_timed_out - SCSI layer time out callback
325 * @cmd: timed out SCSI command
327 * Handles SCSI layer timeout. We race with normal completion of
328 * the qc for @cmd. If the qc is already gone, we lose and let
329 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
330 * timed out and EH should be invoked. Prevent ata_qc_complete()
331 * from finishing it by setting EH_SCHEDULED and return
332 * EH_NOT_HANDLED.
334 * TODO: kill this function once old EH is gone.
336 * LOCKING:
337 * Called from timer context
339 * RETURNS:
340 * EH_HANDLED or EH_NOT_HANDLED
342 enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
344 struct Scsi_Host *host = cmd->device->host;
345 struct ata_port *ap = ata_shost_to_port(host);
346 unsigned long flags;
347 struct ata_queued_cmd *qc;
348 enum scsi_eh_timer_return ret;
350 DPRINTK("ENTER\n");
352 if (ap->ops->error_handler) {
353 ret = EH_NOT_HANDLED;
354 goto out;
357 ret = EH_HANDLED;
358 spin_lock_irqsave(ap->lock, flags);
359 qc = ata_qc_from_tag(ap, ap->link.active_tag);
360 if (qc) {
361 WARN_ON(qc->scsicmd != cmd);
362 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
363 qc->err_mask |= AC_ERR_TIMEOUT;
364 ret = EH_NOT_HANDLED;
366 spin_unlock_irqrestore(ap->lock, flags);
368 out:
369 DPRINTK("EXIT, ret=%d\n", ret);
370 return ret;
374 * ata_scsi_error - SCSI layer error handler callback
375 * @host: SCSI host on which error occurred
377 * Handles SCSI-layer-thrown error events.
379 * LOCKING:
380 * Inherited from SCSI layer (none, can sleep)
382 * RETURNS:
383 * Zero.
385 void ata_scsi_error(struct Scsi_Host *host)
387 struct ata_port *ap = ata_shost_to_port(host);
388 int i;
389 unsigned long flags;
391 DPRINTK("ENTER\n");
393 /* synchronize with port task */
394 ata_port_flush_task(ap);
396 /* synchronize with host lock and sort out timeouts */
398 /* For new EH, all qcs are finished in one of three ways -
399 * normal completion, error completion, and SCSI timeout.
400 * Both cmpletions can race against SCSI timeout. When normal
401 * completion wins, the qc never reaches EH. When error
402 * completion wins, the qc has ATA_QCFLAG_FAILED set.
404 * When SCSI timeout wins, things are a bit more complex.
405 * Normal or error completion can occur after the timeout but
406 * before this point. In such cases, both types of
407 * completions are honored. A scmd is determined to have
408 * timed out iff its associated qc is active and not failed.
410 if (ap->ops->error_handler) {
411 struct scsi_cmnd *scmd, *tmp;
412 int nr_timedout = 0;
414 spin_lock_irqsave(ap->lock, flags);
416 list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
417 struct ata_queued_cmd *qc;
419 for (i = 0; i < ATA_MAX_QUEUE; i++) {
420 qc = __ata_qc_from_tag(ap, i);
421 if (qc->flags & ATA_QCFLAG_ACTIVE &&
422 qc->scsicmd == scmd)
423 break;
426 if (i < ATA_MAX_QUEUE) {
427 /* the scmd has an associated qc */
428 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
429 /* which hasn't failed yet, timeout */
430 qc->err_mask |= AC_ERR_TIMEOUT;
431 qc->flags |= ATA_QCFLAG_FAILED;
432 nr_timedout++;
434 } else {
435 /* Normal completion occurred after
436 * SCSI timeout but before this point.
437 * Successfully complete it.
439 scmd->retries = scmd->allowed;
440 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
444 /* If we have timed out qcs. They belong to EH from
445 * this point but the state of the controller is
446 * unknown. Freeze the port to make sure the IRQ
447 * handler doesn't diddle with those qcs. This must
448 * be done atomically w.r.t. setting QCFLAG_FAILED.
450 if (nr_timedout)
451 __ata_port_freeze(ap);
453 spin_unlock_irqrestore(ap->lock, flags);
455 /* initialize eh_tries */
456 ap->eh_tries = ATA_EH_MAX_TRIES;
457 } else
458 spin_unlock_wait(ap->lock);
460 repeat:
461 /* invoke error handler */
462 if (ap->ops->error_handler) {
463 struct ata_link *link;
465 /* kill fast drain timer */
466 del_timer_sync(&ap->fastdrain_timer);
468 /* process port resume request */
469 ata_eh_handle_port_resume(ap);
471 /* fetch & clear EH info */
472 spin_lock_irqsave(ap->lock, flags);
474 __ata_port_for_each_link(link, ap) {
475 struct ata_eh_context *ehc = &link->eh_context;
476 struct ata_device *dev;
478 memset(&link->eh_context, 0, sizeof(link->eh_context));
479 link->eh_context.i = link->eh_info;
480 memset(&link->eh_info, 0, sizeof(link->eh_info));
482 ata_link_for_each_dev(dev, link) {
483 int devno = dev->devno;
485 ehc->saved_xfer_mode[devno] = dev->xfer_mode;
486 if (ata_ncq_enabled(dev))
487 ehc->saved_ncq_enabled |= 1 << devno;
491 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
492 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
493 ap->excl_link = NULL; /* don't maintain exclusion over EH */
495 spin_unlock_irqrestore(ap->lock, flags);
497 /* invoke EH, skip if unloading or suspended */
498 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
499 ap->ops->error_handler(ap);
500 else
501 ata_eh_finish(ap);
503 /* process port suspend request */
504 ata_eh_handle_port_suspend(ap);
506 /* Exception might have happend after ->error_handler
507 * recovered the port but before this point. Repeat
508 * EH in such case.
510 spin_lock_irqsave(ap->lock, flags);
512 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
513 if (--ap->eh_tries) {
514 spin_unlock_irqrestore(ap->lock, flags);
515 goto repeat;
517 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
518 "tries, giving up\n", ATA_EH_MAX_TRIES);
519 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
522 /* this run is complete, make sure EH info is clear */
523 __ata_port_for_each_link(link, ap)
524 memset(&link->eh_info, 0, sizeof(link->eh_info));
526 /* Clear host_eh_scheduled while holding ap->lock such
527 * that if exception occurs after this point but
528 * before EH completion, SCSI midlayer will
529 * re-initiate EH.
531 host->host_eh_scheduled = 0;
533 spin_unlock_irqrestore(ap->lock, flags);
534 } else {
535 WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
536 ap->ops->eng_timeout(ap);
539 /* finish or retry handled scmd's and clean up */
540 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
542 scsi_eh_flush_done_q(&ap->eh_done_q);
544 /* clean up */
545 spin_lock_irqsave(ap->lock, flags);
547 if (ap->pflags & ATA_PFLAG_LOADING)
548 ap->pflags &= ~ATA_PFLAG_LOADING;
549 else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
550 queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0);
552 if (ap->pflags & ATA_PFLAG_RECOVERED)
553 ata_port_printk(ap, KERN_INFO, "EH complete\n");
555 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
557 /* tell wait_eh that we're done */
558 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
559 wake_up_all(&ap->eh_wait_q);
561 spin_unlock_irqrestore(ap->lock, flags);
563 DPRINTK("EXIT\n");
567 * ata_port_wait_eh - Wait for the currently pending EH to complete
568 * @ap: Port to wait EH for
570 * Wait until the currently pending EH is complete.
572 * LOCKING:
573 * Kernel thread context (may sleep).
575 void ata_port_wait_eh(struct ata_port *ap)
577 unsigned long flags;
578 DEFINE_WAIT(wait);
580 retry:
581 spin_lock_irqsave(ap->lock, flags);
583 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
584 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
585 spin_unlock_irqrestore(ap->lock, flags);
586 schedule();
587 spin_lock_irqsave(ap->lock, flags);
589 finish_wait(&ap->eh_wait_q, &wait);
591 spin_unlock_irqrestore(ap->lock, flags);
593 /* make sure SCSI EH is complete */
594 if (scsi_host_in_recovery(ap->scsi_host)) {
595 msleep(10);
596 goto retry;
600 static int ata_eh_nr_in_flight(struct ata_port *ap)
602 unsigned int tag;
603 int nr = 0;
605 /* count only non-internal commands */
606 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++)
607 if (ata_qc_from_tag(ap, tag))
608 nr++;
610 return nr;
613 void ata_eh_fastdrain_timerfn(unsigned long arg)
615 struct ata_port *ap = (void *)arg;
616 unsigned long flags;
617 int cnt;
619 spin_lock_irqsave(ap->lock, flags);
621 cnt = ata_eh_nr_in_flight(ap);
623 /* are we done? */
624 if (!cnt)
625 goto out_unlock;
627 if (cnt == ap->fastdrain_cnt) {
628 unsigned int tag;
630 /* No progress during the last interval, tag all
631 * in-flight qcs as timed out and freeze the port.
633 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) {
634 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
635 if (qc)
636 qc->err_mask |= AC_ERR_TIMEOUT;
639 ata_port_freeze(ap);
640 } else {
641 /* some qcs have finished, give it another chance */
642 ap->fastdrain_cnt = cnt;
643 ap->fastdrain_timer.expires =
644 jiffies + ATA_EH_FASTDRAIN_INTERVAL;
645 add_timer(&ap->fastdrain_timer);
648 out_unlock:
649 spin_unlock_irqrestore(ap->lock, flags);
653 * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
654 * @ap: target ATA port
655 * @fastdrain: activate fast drain
657 * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
658 * is non-zero and EH wasn't pending before. Fast drain ensures
659 * that EH kicks in in timely manner.
661 * LOCKING:
662 * spin_lock_irqsave(host lock)
664 static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
666 int cnt;
668 /* already scheduled? */
669 if (ap->pflags & ATA_PFLAG_EH_PENDING)
670 return;
672 ap->pflags |= ATA_PFLAG_EH_PENDING;
674 if (!fastdrain)
675 return;
677 /* do we have in-flight qcs? */
678 cnt = ata_eh_nr_in_flight(ap);
679 if (!cnt)
680 return;
682 /* activate fast drain */
683 ap->fastdrain_cnt = cnt;
684 ap->fastdrain_timer.expires = jiffies + ATA_EH_FASTDRAIN_INTERVAL;
685 add_timer(&ap->fastdrain_timer);
689 * ata_qc_schedule_eh - schedule qc for error handling
690 * @qc: command to schedule error handling for
692 * Schedule error handling for @qc. EH will kick in as soon as
693 * other commands are drained.
695 * LOCKING:
696 * spin_lock_irqsave(host lock)
698 void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
700 struct ata_port *ap = qc->ap;
702 WARN_ON(!ap->ops->error_handler);
704 qc->flags |= ATA_QCFLAG_FAILED;
705 ata_eh_set_pending(ap, 1);
707 /* The following will fail if timeout has already expired.
708 * ata_scsi_error() takes care of such scmds on EH entry.
709 * Note that ATA_QCFLAG_FAILED is unconditionally set after
710 * this function completes.
712 scsi_req_abort_cmd(qc->scsicmd);
716 * ata_port_schedule_eh - schedule error handling without a qc
717 * @ap: ATA port to schedule EH for
719 * Schedule error handling for @ap. EH will kick in as soon as
720 * all commands are drained.
722 * LOCKING:
723 * spin_lock_irqsave(host lock)
725 void ata_port_schedule_eh(struct ata_port *ap)
727 WARN_ON(!ap->ops->error_handler);
729 if (ap->pflags & ATA_PFLAG_INITIALIZING)
730 return;
732 ata_eh_set_pending(ap, 1);
733 scsi_schedule_eh(ap->scsi_host);
735 DPRINTK("port EH scheduled\n");
738 static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
740 int tag, nr_aborted = 0;
742 WARN_ON(!ap->ops->error_handler);
744 /* we're gonna abort all commands, no need for fast drain */
745 ata_eh_set_pending(ap, 0);
747 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
748 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
750 if (qc && (!link || qc->dev->link == link)) {
751 qc->flags |= ATA_QCFLAG_FAILED;
752 ata_qc_complete(qc);
753 nr_aborted++;
757 if (!nr_aborted)
758 ata_port_schedule_eh(ap);
760 return nr_aborted;
764 * ata_link_abort - abort all qc's on the link
765 * @link: ATA link to abort qc's for
767 * Abort all active qc's active on @link and schedule EH.
769 * LOCKING:
770 * spin_lock_irqsave(host lock)
772 * RETURNS:
773 * Number of aborted qc's.
775 int ata_link_abort(struct ata_link *link)
777 return ata_do_link_abort(link->ap, link);
781 * ata_port_abort - abort all qc's on the port
782 * @ap: ATA port to abort qc's for
784 * Abort all active qc's of @ap and schedule EH.
786 * LOCKING:
787 * spin_lock_irqsave(host_set lock)
789 * RETURNS:
790 * Number of aborted qc's.
792 int ata_port_abort(struct ata_port *ap)
794 return ata_do_link_abort(ap, NULL);
798 * __ata_port_freeze - freeze port
799 * @ap: ATA port to freeze
801 * This function is called when HSM violation or some other
802 * condition disrupts normal operation of the port. Frozen port
803 * is not allowed to perform any operation until the port is
804 * thawed, which usually follows a successful reset.
806 * ap->ops->freeze() callback can be used for freezing the port
807 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
808 * port cannot be frozen hardware-wise, the interrupt handler
809 * must ack and clear interrupts unconditionally while the port
810 * is frozen.
812 * LOCKING:
813 * spin_lock_irqsave(host lock)
815 static void __ata_port_freeze(struct ata_port *ap)
817 WARN_ON(!ap->ops->error_handler);
819 if (ap->ops->freeze)
820 ap->ops->freeze(ap);
822 ap->pflags |= ATA_PFLAG_FROZEN;
824 DPRINTK("ata%u port frozen\n", ap->print_id);
828 * ata_port_freeze - abort & freeze port
829 * @ap: ATA port to freeze
831 * Abort and freeze @ap.
833 * LOCKING:
834 * spin_lock_irqsave(host lock)
836 * RETURNS:
837 * Number of aborted commands.
839 int ata_port_freeze(struct ata_port *ap)
841 int nr_aborted;
843 WARN_ON(!ap->ops->error_handler);
845 nr_aborted = ata_port_abort(ap);
846 __ata_port_freeze(ap);
848 return nr_aborted;
852 * sata_async_notification - SATA async notification handler
853 * @ap: ATA port where async notification is received
855 * Handler to be called when async notification via SDB FIS is
856 * received. This function schedules EH if necessary.
858 * LOCKING:
859 * spin_lock_irqsave(host lock)
861 * RETURNS:
862 * 1 if EH is scheduled, 0 otherwise.
864 int sata_async_notification(struct ata_port *ap)
866 u32 sntf;
867 int rc;
869 if (!(ap->flags & ATA_FLAG_AN))
870 return 0;
872 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
873 if (rc == 0)
874 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
876 if (!ap->nr_pmp_links || rc) {
877 /* PMP is not attached or SNTF is not available */
878 if (!ap->nr_pmp_links) {
879 /* PMP is not attached. Check whether ATAPI
880 * AN is configured. If so, notify media
881 * change.
883 struct ata_device *dev = ap->link.device;
885 if ((dev->class == ATA_DEV_ATAPI) &&
886 (dev->flags & ATA_DFLAG_AN))
887 ata_scsi_media_change_notify(dev);
888 return 0;
889 } else {
890 /* PMP is attached but SNTF is not available.
891 * ATAPI async media change notification is
892 * not used. The PMP must be reporting PHY
893 * status change, schedule EH.
895 ata_port_schedule_eh(ap);
896 return 1;
898 } else {
899 /* PMP is attached and SNTF is available */
900 struct ata_link *link;
902 /* check and notify ATAPI AN */
903 ata_port_for_each_link(link, ap) {
904 if (!(sntf & (1 << link->pmp)))
905 continue;
907 if ((link->device->class == ATA_DEV_ATAPI) &&
908 (link->device->flags & ATA_DFLAG_AN))
909 ata_scsi_media_change_notify(link->device);
912 /* If PMP is reporting that PHY status of some
913 * downstream ports has changed, schedule EH.
915 if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
916 ata_port_schedule_eh(ap);
917 return 1;
920 return 0;
925 * ata_eh_freeze_port - EH helper to freeze port
926 * @ap: ATA port to freeze
928 * Freeze @ap.
930 * LOCKING:
931 * None.
933 void ata_eh_freeze_port(struct ata_port *ap)
935 unsigned long flags;
937 if (!ap->ops->error_handler)
938 return;
940 spin_lock_irqsave(ap->lock, flags);
941 __ata_port_freeze(ap);
942 spin_unlock_irqrestore(ap->lock, flags);
946 * ata_port_thaw_port - EH helper to thaw port
947 * @ap: ATA port to thaw
949 * Thaw frozen port @ap.
951 * LOCKING:
952 * None.
954 void ata_eh_thaw_port(struct ata_port *ap)
956 unsigned long flags;
958 if (!ap->ops->error_handler)
959 return;
961 spin_lock_irqsave(ap->lock, flags);
963 ap->pflags &= ~ATA_PFLAG_FROZEN;
965 if (ap->ops->thaw)
966 ap->ops->thaw(ap);
968 spin_unlock_irqrestore(ap->lock, flags);
970 DPRINTK("ata%u port thawed\n", ap->print_id);
973 static void ata_eh_scsidone(struct scsi_cmnd *scmd)
975 /* nada */
978 static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
980 struct ata_port *ap = qc->ap;
981 struct scsi_cmnd *scmd = qc->scsicmd;
982 unsigned long flags;
984 spin_lock_irqsave(ap->lock, flags);
985 qc->scsidone = ata_eh_scsidone;
986 __ata_qc_complete(qc);
987 WARN_ON(ata_tag_valid(qc->tag));
988 spin_unlock_irqrestore(ap->lock, flags);
990 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
994 * ata_eh_qc_complete - Complete an active ATA command from EH
995 * @qc: Command to complete
997 * Indicate to the mid and upper layers that an ATA command has
998 * completed. To be used from EH.
1000 void ata_eh_qc_complete(struct ata_queued_cmd *qc)
1002 struct scsi_cmnd *scmd = qc->scsicmd;
1003 scmd->retries = scmd->allowed;
1004 __ata_eh_qc_complete(qc);
1008 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
1009 * @qc: Command to retry
1011 * Indicate to the mid and upper layers that an ATA command
1012 * should be retried. To be used from EH.
1014 * SCSI midlayer limits the number of retries to scmd->allowed.
1015 * scmd->retries is decremented for commands which get retried
1016 * due to unrelated failures (qc->err_mask is zero).
1018 void ata_eh_qc_retry(struct ata_queued_cmd *qc)
1020 struct scsi_cmnd *scmd = qc->scsicmd;
1021 if (!qc->err_mask && scmd->retries)
1022 scmd->retries--;
1023 __ata_eh_qc_complete(qc);
1027 * ata_eh_detach_dev - detach ATA device
1028 * @dev: ATA device to detach
1030 * Detach @dev.
1032 * LOCKING:
1033 * None.
1035 void ata_eh_detach_dev(struct ata_device *dev)
1037 struct ata_link *link = dev->link;
1038 struct ata_port *ap = link->ap;
1039 unsigned long flags;
1041 ata_dev_disable(dev);
1043 spin_lock_irqsave(ap->lock, flags);
1045 dev->flags &= ~ATA_DFLAG_DETACH;
1047 if (ata_scsi_offline_dev(dev)) {
1048 dev->flags |= ATA_DFLAG_DETACHED;
1049 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
1052 /* clear per-dev EH actions */
1053 ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
1054 ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
1056 spin_unlock_irqrestore(ap->lock, flags);
1060 * ata_eh_about_to_do - about to perform eh_action
1061 * @link: target ATA link
1062 * @dev: target ATA dev for per-dev action (can be NULL)
1063 * @action: action about to be performed
1065 * Called just before performing EH actions to clear related bits
1066 * in @link->eh_info such that eh actions are not unnecessarily
1067 * repeated.
1069 * LOCKING:
1070 * None.
1072 void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
1073 unsigned int action)
1075 struct ata_port *ap = link->ap;
1076 struct ata_eh_info *ehi = &link->eh_info;
1077 struct ata_eh_context *ehc = &link->eh_context;
1078 unsigned long flags;
1080 spin_lock_irqsave(ap->lock, flags);
1082 ata_eh_clear_action(link, dev, ehi, action);
1084 if (!(ehc->i.flags & ATA_EHI_QUIET))
1085 ap->pflags |= ATA_PFLAG_RECOVERED;
1087 spin_unlock_irqrestore(ap->lock, flags);
1091 * ata_eh_done - EH action complete
1092 * @ap: target ATA port
1093 * @dev: target ATA dev for per-dev action (can be NULL)
1094 * @action: action just completed
1096 * Called right after performing EH actions to clear related bits
1097 * in @link->eh_context.
1099 * LOCKING:
1100 * None.
1102 void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1103 unsigned int action)
1105 struct ata_eh_context *ehc = &link->eh_context;
1107 ata_eh_clear_action(link, dev, &ehc->i, action);
1111 * ata_err_string - convert err_mask to descriptive string
1112 * @err_mask: error mask to convert to string
1114 * Convert @err_mask to descriptive string. Errors are
1115 * prioritized according to severity and only the most severe
1116 * error is reported.
1118 * LOCKING:
1119 * None.
1121 * RETURNS:
1122 * Descriptive string for @err_mask
1124 static const char *ata_err_string(unsigned int err_mask)
1126 if (err_mask & AC_ERR_HOST_BUS)
1127 return "host bus error";
1128 if (err_mask & AC_ERR_ATA_BUS)
1129 return "ATA bus error";
1130 if (err_mask & AC_ERR_TIMEOUT)
1131 return "timeout";
1132 if (err_mask & AC_ERR_HSM)
1133 return "HSM violation";
1134 if (err_mask & AC_ERR_SYSTEM)
1135 return "internal error";
1136 if (err_mask & AC_ERR_MEDIA)
1137 return "media error";
1138 if (err_mask & AC_ERR_INVALID)
1139 return "invalid argument";
1140 if (err_mask & AC_ERR_DEV)
1141 return "device error";
1142 return "unknown error";
1146 * ata_read_log_page - read a specific log page
1147 * @dev: target device
1148 * @page: page to read
1149 * @buf: buffer to store read page
1150 * @sectors: number of sectors to read
1152 * Read log page using READ_LOG_EXT command.
1154 * LOCKING:
1155 * Kernel thread context (may sleep).
1157 * RETURNS:
1158 * 0 on success, AC_ERR_* mask otherwise.
1160 static unsigned int ata_read_log_page(struct ata_device *dev,
1161 u8 page, void *buf, unsigned int sectors)
1163 struct ata_taskfile tf;
1164 unsigned int err_mask;
1166 DPRINTK("read log page - page %d\n", page);
1168 ata_tf_init(dev, &tf);
1169 tf.command = ATA_CMD_READ_LOG_EXT;
1170 tf.lbal = page;
1171 tf.nsect = sectors;
1172 tf.hob_nsect = sectors >> 8;
1173 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1174 tf.protocol = ATA_PROT_PIO;
1176 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
1177 buf, sectors * ATA_SECT_SIZE, 0);
1179 DPRINTK("EXIT, err_mask=%x\n", err_mask);
1180 return err_mask;
1184 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
1185 * @dev: Device to read log page 10h from
1186 * @tag: Resulting tag of the failed command
1187 * @tf: Resulting taskfile registers of the failed command
1189 * Read log page 10h to obtain NCQ error details and clear error
1190 * condition.
1192 * LOCKING:
1193 * Kernel thread context (may sleep).
1195 * RETURNS:
1196 * 0 on success, -errno otherwise.
1198 static int ata_eh_read_log_10h(struct ata_device *dev,
1199 int *tag, struct ata_taskfile *tf)
1201 u8 *buf = dev->link->ap->sector_buf;
1202 unsigned int err_mask;
1203 u8 csum;
1204 int i;
1206 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
1207 if (err_mask)
1208 return -EIO;
1210 csum = 0;
1211 for (i = 0; i < ATA_SECT_SIZE; i++)
1212 csum += buf[i];
1213 if (csum)
1214 ata_dev_printk(dev, KERN_WARNING,
1215 "invalid checksum 0x%x on log page 10h\n", csum);
1217 if (buf[0] & 0x80)
1218 return -ENOENT;
1220 *tag = buf[0] & 0x1f;
1222 tf->command = buf[2];
1223 tf->feature = buf[3];
1224 tf->lbal = buf[4];
1225 tf->lbam = buf[5];
1226 tf->lbah = buf[6];
1227 tf->device = buf[7];
1228 tf->hob_lbal = buf[8];
1229 tf->hob_lbam = buf[9];
1230 tf->hob_lbah = buf[10];
1231 tf->nsect = buf[12];
1232 tf->hob_nsect = buf[13];
1234 return 0;
1238 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1239 * @dev: device to perform REQUEST_SENSE to
1240 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
1242 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
1243 * SENSE. This function is EH helper.
1245 * LOCKING:
1246 * Kernel thread context (may sleep).
1248 * RETURNS:
1249 * 0 on success, AC_ERR_* mask on failure
1251 static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc)
1253 struct ata_device *dev = qc->dev;
1254 unsigned char *sense_buf = qc->scsicmd->sense_buffer;
1255 struct ata_port *ap = dev->link->ap;
1256 struct ata_taskfile tf;
1257 u8 cdb[ATAPI_CDB_LEN];
1259 DPRINTK("ATAPI request sense\n");
1261 /* FIXME: is this needed? */
1262 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1264 /* initialize sense_buf with the error register,
1265 * for the case where they are -not- overwritten
1267 sense_buf[0] = 0x70;
1268 sense_buf[2] = qc->result_tf.feature >> 4;
1270 /* some devices time out if garbage left in tf */
1271 ata_tf_init(dev, &tf);
1273 memset(cdb, 0, ATAPI_CDB_LEN);
1274 cdb[0] = REQUEST_SENSE;
1275 cdb[4] = SCSI_SENSE_BUFFERSIZE;
1277 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1278 tf.command = ATA_CMD_PACKET;
1280 /* is it pointless to prefer PIO for "safety reasons"? */
1281 if (ap->flags & ATA_FLAG_PIO_DMA) {
1282 tf.protocol = ATAPI_PROT_DMA;
1283 tf.feature |= ATAPI_PKT_DMA;
1284 } else {
1285 tf.protocol = ATAPI_PROT_PIO;
1286 tf.lbam = SCSI_SENSE_BUFFERSIZE;
1287 tf.lbah = 0;
1290 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
1291 sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
1295 * ata_eh_analyze_serror - analyze SError for a failed port
1296 * @link: ATA link to analyze SError for
1298 * Analyze SError if available and further determine cause of
1299 * failure.
1301 * LOCKING:
1302 * None.
1304 static void ata_eh_analyze_serror(struct ata_link *link)
1306 struct ata_eh_context *ehc = &link->eh_context;
1307 u32 serror = ehc->i.serror;
1308 unsigned int err_mask = 0, action = 0;
1309 u32 hotplug_mask;
1311 if (serror & SERR_PERSISTENT) {
1312 err_mask |= AC_ERR_ATA_BUS;
1313 action |= ATA_EH_RESET;
1315 if (serror &
1316 (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
1317 err_mask |= AC_ERR_ATA_BUS;
1318 action |= ATA_EH_RESET;
1320 if (serror & SERR_PROTOCOL) {
1321 err_mask |= AC_ERR_HSM;
1322 action |= ATA_EH_RESET;
1324 if (serror & SERR_INTERNAL) {
1325 err_mask |= AC_ERR_SYSTEM;
1326 action |= ATA_EH_RESET;
1329 /* Determine whether a hotplug event has occurred. Both
1330 * SError.N/X are considered hotplug events for enabled or
1331 * host links. For disabled PMP links, only N bit is
1332 * considered as X bit is left at 1 for link plugging.
1334 hotplug_mask = 0;
1336 if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
1337 hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
1338 else
1339 hotplug_mask = SERR_PHYRDY_CHG;
1341 if (serror & hotplug_mask)
1342 ata_ehi_hotplugged(&ehc->i);
1344 ehc->i.err_mask |= err_mask;
1345 ehc->i.action |= action;
1349 * ata_eh_analyze_ncq_error - analyze NCQ error
1350 * @link: ATA link to analyze NCQ error for
1352 * Read log page 10h, determine the offending qc and acquire
1353 * error status TF. For NCQ device errors, all LLDDs have to do
1354 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1355 * care of the rest.
1357 * LOCKING:
1358 * Kernel thread context (may sleep).
1360 static void ata_eh_analyze_ncq_error(struct ata_link *link)
1362 struct ata_port *ap = link->ap;
1363 struct ata_eh_context *ehc = &link->eh_context;
1364 struct ata_device *dev = link->device;
1365 struct ata_queued_cmd *qc;
1366 struct ata_taskfile tf;
1367 int tag, rc;
1369 /* if frozen, we can't do much */
1370 if (ap->pflags & ATA_PFLAG_FROZEN)
1371 return;
1373 /* is it NCQ device error? */
1374 if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1375 return;
1377 /* has LLDD analyzed already? */
1378 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1379 qc = __ata_qc_from_tag(ap, tag);
1381 if (!(qc->flags & ATA_QCFLAG_FAILED))
1382 continue;
1384 if (qc->err_mask)
1385 return;
1388 /* okay, this error is ours */
1389 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1390 if (rc) {
1391 ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
1392 "(errno=%d)\n", rc);
1393 return;
1396 if (!(link->sactive & (1 << tag))) {
1397 ata_link_printk(link, KERN_ERR, "log page 10h reported "
1398 "inactive tag %d\n", tag);
1399 return;
1402 /* we've got the perpetrator, condemn it */
1403 qc = __ata_qc_from_tag(ap, tag);
1404 memcpy(&qc->result_tf, &tf, sizeof(tf));
1405 qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
1406 ehc->i.err_mask &= ~AC_ERR_DEV;
1410 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1411 * @qc: qc to analyze
1412 * @tf: Taskfile registers to analyze
1414 * Analyze taskfile of @qc and further determine cause of
1415 * failure. This function also requests ATAPI sense data if
1416 * avaliable.
1418 * LOCKING:
1419 * Kernel thread context (may sleep).
1421 * RETURNS:
1422 * Determined recovery action
1424 static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1425 const struct ata_taskfile *tf)
1427 unsigned int tmp, action = 0;
1428 u8 stat = tf->command, err = tf->feature;
1430 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1431 qc->err_mask |= AC_ERR_HSM;
1432 return ATA_EH_RESET;
1435 if (stat & (ATA_ERR | ATA_DF))
1436 qc->err_mask |= AC_ERR_DEV;
1437 else
1438 return 0;
1440 switch (qc->dev->class) {
1441 case ATA_DEV_ATA:
1442 if (err & ATA_ICRC)
1443 qc->err_mask |= AC_ERR_ATA_BUS;
1444 if (err & ATA_UNC)
1445 qc->err_mask |= AC_ERR_MEDIA;
1446 if (err & ATA_IDNF)
1447 qc->err_mask |= AC_ERR_INVALID;
1448 break;
1450 case ATA_DEV_ATAPI:
1451 if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
1452 tmp = atapi_eh_request_sense(qc);
1453 if (!tmp) {
1454 /* ATA_QCFLAG_SENSE_VALID is used to
1455 * tell atapi_qc_complete() that sense
1456 * data is already valid.
1458 * TODO: interpret sense data and set
1459 * appropriate err_mask.
1461 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1462 } else
1463 qc->err_mask |= tmp;
1467 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1468 action |= ATA_EH_RESET;
1470 return action;
1473 static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask,
1474 int *xfer_ok)
1476 int base = 0;
1478 if (!(eflags & ATA_EFLAG_DUBIOUS_XFER))
1479 *xfer_ok = 1;
1481 if (!*xfer_ok)
1482 base = ATA_ECAT_DUBIOUS_NONE;
1484 if (err_mask & AC_ERR_ATA_BUS)
1485 return base + ATA_ECAT_ATA_BUS;
1487 if (err_mask & AC_ERR_TIMEOUT)
1488 return base + ATA_ECAT_TOUT_HSM;
1490 if (eflags & ATA_EFLAG_IS_IO) {
1491 if (err_mask & AC_ERR_HSM)
1492 return base + ATA_ECAT_TOUT_HSM;
1493 if ((err_mask &
1494 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1495 return base + ATA_ECAT_UNK_DEV;
1498 return 0;
1501 struct speed_down_verdict_arg {
1502 u64 since;
1503 int xfer_ok;
1504 int nr_errors[ATA_ECAT_NR];
1507 static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
1509 struct speed_down_verdict_arg *arg = void_arg;
1510 int cat;
1512 if (ent->timestamp < arg->since)
1513 return -1;
1515 cat = ata_eh_categorize_error(ent->eflags, ent->err_mask,
1516 &arg->xfer_ok);
1517 arg->nr_errors[cat]++;
1519 return 0;
1523 * ata_eh_speed_down_verdict - Determine speed down verdict
1524 * @dev: Device of interest
1526 * This function examines error ring of @dev and determines
1527 * whether NCQ needs to be turned off, transfer speed should be
1528 * stepped down, or falling back to PIO is necessary.
1530 * ECAT_ATA_BUS : ATA_BUS error for any command
1532 * ECAT_TOUT_HSM : TIMEOUT for any command or HSM violation for
1533 * IO commands
1535 * ECAT_UNK_DEV : Unknown DEV error for IO commands
1537 * ECAT_DUBIOUS_* : Identical to above three but occurred while
1538 * data transfer hasn't been verified.
1540 * Verdicts are
1542 * NCQ_OFF : Turn off NCQ.
1544 * SPEED_DOWN : Speed down transfer speed but don't fall back
1545 * to PIO.
1547 * FALLBACK_TO_PIO : Fall back to PIO.
1549 * Even if multiple verdicts are returned, only one action is
1550 * taken per error. An action triggered by non-DUBIOUS errors
1551 * clears ering, while one triggered by DUBIOUS_* errors doesn't.
1552 * This is to expedite speed down decisions right after device is
1553 * initially configured.
1555 * The followings are speed down rules. #1 and #2 deal with
1556 * DUBIOUS errors.
1558 * 1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors
1559 * occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO.
1561 * 2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors
1562 * occurred during last 5 mins, NCQ_OFF.
1564 * 3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors
1565 * ocurred during last 5 mins, FALLBACK_TO_PIO
1567 * 4. If more than 3 TOUT_HSM or UNK_DEV errors occurred
1568 * during last 10 mins, NCQ_OFF.
1570 * 5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6
1571 * UNK_DEV errors occurred during last 10 mins, SPEED_DOWN.
1573 * LOCKING:
1574 * Inherited from caller.
1576 * RETURNS:
1577 * OR of ATA_EH_SPDN_* flags.
1579 static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
1581 const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
1582 u64 j64 = get_jiffies_64();
1583 struct speed_down_verdict_arg arg;
1584 unsigned int verdict = 0;
1586 /* scan past 5 mins of error history */
1587 memset(&arg, 0, sizeof(arg));
1588 arg.since = j64 - min(j64, j5mins);
1589 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1591 if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] +
1592 arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1)
1593 verdict |= ATA_EH_SPDN_SPEED_DOWN |
1594 ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS;
1596 if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] +
1597 arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1)
1598 verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS;
1600 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1601 arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1602 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
1603 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
1605 /* scan past 10 mins of error history */
1606 memset(&arg, 0, sizeof(arg));
1607 arg.since = j64 - min(j64, j10mins);
1608 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1610 if (arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1611 arg.nr_errors[ATA_ECAT_UNK_DEV] > 3)
1612 verdict |= ATA_EH_SPDN_NCQ_OFF;
1614 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1615 arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 ||
1616 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
1617 verdict |= ATA_EH_SPDN_SPEED_DOWN;
1619 return verdict;
1623 * ata_eh_speed_down - record error and speed down if necessary
1624 * @dev: Failed device
1625 * @eflags: mask of ATA_EFLAG_* flags
1626 * @err_mask: err_mask of the error
1628 * Record error and examine error history to determine whether
1629 * adjusting transmission speed is necessary. It also sets
1630 * transmission limits appropriately if such adjustment is
1631 * necessary.
1633 * LOCKING:
1634 * Kernel thread context (may sleep).
1636 * RETURNS:
1637 * Determined recovery action.
1639 static unsigned int ata_eh_speed_down(struct ata_device *dev,
1640 unsigned int eflags, unsigned int err_mask)
1642 struct ata_link *link = dev->link;
1643 int xfer_ok = 0;
1644 unsigned int verdict;
1645 unsigned int action = 0;
1647 /* don't bother if Cat-0 error */
1648 if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0)
1649 return 0;
1651 /* record error and determine whether speed down is necessary */
1652 ata_ering_record(&dev->ering, eflags, err_mask);
1653 verdict = ata_eh_speed_down_verdict(dev);
1655 /* turn off NCQ? */
1656 if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
1657 (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
1658 ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
1659 dev->flags |= ATA_DFLAG_NCQ_OFF;
1660 ata_dev_printk(dev, KERN_WARNING,
1661 "NCQ disabled due to excessive errors\n");
1662 goto done;
1665 /* speed down? */
1666 if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1667 /* speed down SATA link speed if possible */
1668 if (sata_down_spd_limit(link) == 0) {
1669 action |= ATA_EH_RESET;
1670 goto done;
1673 /* lower transfer mode */
1674 if (dev->spdn_cnt < 2) {
1675 static const int dma_dnxfer_sel[] =
1676 { ATA_DNXFER_DMA, ATA_DNXFER_40C };
1677 static const int pio_dnxfer_sel[] =
1678 { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
1679 int sel;
1681 if (dev->xfer_shift != ATA_SHIFT_PIO)
1682 sel = dma_dnxfer_sel[dev->spdn_cnt];
1683 else
1684 sel = pio_dnxfer_sel[dev->spdn_cnt];
1686 dev->spdn_cnt++;
1688 if (ata_down_xfermask_limit(dev, sel) == 0) {
1689 action |= ATA_EH_RESET;
1690 goto done;
1695 /* Fall back to PIO? Slowing down to PIO is meaningless for
1696 * SATA ATA devices. Consider it only for PATA and SATAPI.
1698 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
1699 (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) &&
1700 (dev->xfer_shift != ATA_SHIFT_PIO)) {
1701 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
1702 dev->spdn_cnt = 0;
1703 action |= ATA_EH_RESET;
1704 goto done;
1708 return 0;
1709 done:
1710 /* device has been slowed down, blow error history */
1711 if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS))
1712 ata_ering_clear(&dev->ering);
1713 return action;
1717 * ata_eh_link_autopsy - analyze error and determine recovery action
1718 * @link: host link to perform autopsy on
1720 * Analyze why @link failed and determine which recovery actions
1721 * are needed. This function also sets more detailed AC_ERR_*
1722 * values and fills sense data for ATAPI CHECK SENSE.
1724 * LOCKING:
1725 * Kernel thread context (may sleep).
1727 static void ata_eh_link_autopsy(struct ata_link *link)
1729 struct ata_port *ap = link->ap;
1730 struct ata_eh_context *ehc = &link->eh_context;
1731 struct ata_device *dev;
1732 unsigned int all_err_mask = 0, eflags = 0;
1733 int tag;
1734 u32 serror;
1735 int rc;
1737 DPRINTK("ENTER\n");
1739 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1740 return;
1742 /* obtain and analyze SError */
1743 rc = sata_scr_read(link, SCR_ERROR, &serror);
1744 if (rc == 0) {
1745 ehc->i.serror |= serror;
1746 ata_eh_analyze_serror(link);
1747 } else if (rc != -EOPNOTSUPP) {
1748 /* SError read failed, force reset and probing */
1749 ehc->i.probe_mask |= ATA_ALL_DEVICES;
1750 ehc->i.action |= ATA_EH_RESET;
1751 ehc->i.err_mask |= AC_ERR_OTHER;
1754 /* analyze NCQ failure */
1755 ata_eh_analyze_ncq_error(link);
1757 /* any real error trumps AC_ERR_OTHER */
1758 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1759 ehc->i.err_mask &= ~AC_ERR_OTHER;
1761 all_err_mask |= ehc->i.err_mask;
1763 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1764 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1766 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
1767 continue;
1769 /* inherit upper level err_mask */
1770 qc->err_mask |= ehc->i.err_mask;
1772 /* analyze TF */
1773 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1775 /* DEV errors are probably spurious in case of ATA_BUS error */
1776 if (qc->err_mask & AC_ERR_ATA_BUS)
1777 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1778 AC_ERR_INVALID);
1780 /* any real error trumps unknown error */
1781 if (qc->err_mask & ~AC_ERR_OTHER)
1782 qc->err_mask &= ~AC_ERR_OTHER;
1784 /* SENSE_VALID trumps dev/unknown error and revalidation */
1785 if (qc->flags & ATA_QCFLAG_SENSE_VALID)
1786 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1788 /* determine whether the command is worth retrying */
1789 if (!(qc->err_mask & AC_ERR_INVALID) &&
1790 ((qc->flags & ATA_QCFLAG_IO) || qc->err_mask != AC_ERR_DEV))
1791 qc->flags |= ATA_QCFLAG_RETRY;
1793 /* accumulate error info */
1794 ehc->i.dev = qc->dev;
1795 all_err_mask |= qc->err_mask;
1796 if (qc->flags & ATA_QCFLAG_IO)
1797 eflags |= ATA_EFLAG_IS_IO;
1800 /* enforce default EH actions */
1801 if (ap->pflags & ATA_PFLAG_FROZEN ||
1802 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
1803 ehc->i.action |= ATA_EH_RESET;
1804 else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) ||
1805 (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV)))
1806 ehc->i.action |= ATA_EH_REVALIDATE;
1808 /* If we have offending qcs and the associated failed device,
1809 * perform per-dev EH action only on the offending device.
1811 if (ehc->i.dev) {
1812 ehc->i.dev_action[ehc->i.dev->devno] |=
1813 ehc->i.action & ATA_EH_PERDEV_MASK;
1814 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
1817 /* propagate timeout to host link */
1818 if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link))
1819 ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT;
1821 /* record error and consider speeding down */
1822 dev = ehc->i.dev;
1823 if (!dev && ((ata_link_max_devices(link) == 1 &&
1824 ata_dev_enabled(link->device))))
1825 dev = link->device;
1827 if (dev) {
1828 if (dev->flags & ATA_DFLAG_DUBIOUS_XFER)
1829 eflags |= ATA_EFLAG_DUBIOUS_XFER;
1830 ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask);
1833 DPRINTK("EXIT\n");
1837 * ata_eh_autopsy - analyze error and determine recovery action
1838 * @ap: host port to perform autopsy on
1840 * Analyze all links of @ap and determine why they failed and
1841 * which recovery actions are needed.
1843 * LOCKING:
1844 * Kernel thread context (may sleep).
1846 void ata_eh_autopsy(struct ata_port *ap)
1848 struct ata_link *link;
1850 ata_port_for_each_link(link, ap)
1851 ata_eh_link_autopsy(link);
1853 /* Autopsy of fanout ports can affect host link autopsy.
1854 * Perform host link autopsy last.
1856 if (ap->nr_pmp_links)
1857 ata_eh_link_autopsy(&ap->link);
1861 * ata_eh_link_report - report error handling to user
1862 * @link: ATA link EH is going on
1864 * Report EH to user.
1866 * LOCKING:
1867 * None.
1869 static void ata_eh_link_report(struct ata_link *link)
1871 struct ata_port *ap = link->ap;
1872 struct ata_eh_context *ehc = &link->eh_context;
1873 const char *frozen, *desc;
1874 char tries_buf[6];
1875 int tag, nr_failed = 0;
1877 if (ehc->i.flags & ATA_EHI_QUIET)
1878 return;
1880 desc = NULL;
1881 if (ehc->i.desc[0] != '\0')
1882 desc = ehc->i.desc;
1884 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1885 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1887 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link ||
1888 ((qc->flags & ATA_QCFLAG_QUIET) &&
1889 qc->err_mask == AC_ERR_DEV))
1890 continue;
1891 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1892 continue;
1894 nr_failed++;
1897 if (!nr_failed && !ehc->i.err_mask)
1898 return;
1900 frozen = "";
1901 if (ap->pflags & ATA_PFLAG_FROZEN)
1902 frozen = " frozen";
1904 memset(tries_buf, 0, sizeof(tries_buf));
1905 if (ap->eh_tries < ATA_EH_MAX_TRIES)
1906 snprintf(tries_buf, sizeof(tries_buf) - 1, " t%d",
1907 ap->eh_tries);
1909 if (ehc->i.dev) {
1910 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1911 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1912 ehc->i.err_mask, link->sactive, ehc->i.serror,
1913 ehc->i.action, frozen, tries_buf);
1914 if (desc)
1915 ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
1916 } else {
1917 ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
1918 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1919 ehc->i.err_mask, link->sactive, ehc->i.serror,
1920 ehc->i.action, frozen, tries_buf);
1921 if (desc)
1922 ata_link_printk(link, KERN_ERR, "%s\n", desc);
1925 if (ehc->i.serror)
1926 ata_port_printk(ap, KERN_ERR,
1927 "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
1928 ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
1929 ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
1930 ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
1931 ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
1932 ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
1933 ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
1934 ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
1935 ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
1936 ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
1937 ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
1938 ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
1939 ehc->i.serror & SERR_CRC ? "BadCRC " : "",
1940 ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
1941 ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
1942 ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
1943 ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
1944 ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
1946 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1947 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1948 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
1949 const u8 *cdb = qc->cdb;
1950 char data_buf[20] = "";
1951 char cdb_buf[70] = "";
1953 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
1954 qc->dev->link != link || !qc->err_mask)
1955 continue;
1957 if (qc->dma_dir != DMA_NONE) {
1958 static const char *dma_str[] = {
1959 [DMA_BIDIRECTIONAL] = "bidi",
1960 [DMA_TO_DEVICE] = "out",
1961 [DMA_FROM_DEVICE] = "in",
1963 static const char *prot_str[] = {
1964 [ATA_PROT_PIO] = "pio",
1965 [ATA_PROT_DMA] = "dma",
1966 [ATA_PROT_NCQ] = "ncq",
1967 [ATAPI_PROT_PIO] = "pio",
1968 [ATAPI_PROT_DMA] = "dma",
1971 snprintf(data_buf, sizeof(data_buf), " %s %u %s",
1972 prot_str[qc->tf.protocol], qc->nbytes,
1973 dma_str[qc->dma_dir]);
1976 if (ata_is_atapi(qc->tf.protocol))
1977 snprintf(cdb_buf, sizeof(cdb_buf),
1978 "cdb %02x %02x %02x %02x %02x %02x %02x %02x "
1979 "%02x %02x %02x %02x %02x %02x %02x %02x\n ",
1980 cdb[0], cdb[1], cdb[2], cdb[3],
1981 cdb[4], cdb[5], cdb[6], cdb[7],
1982 cdb[8], cdb[9], cdb[10], cdb[11],
1983 cdb[12], cdb[13], cdb[14], cdb[15]);
1985 ata_dev_printk(qc->dev, KERN_ERR,
1986 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
1987 "tag %d%s\n %s"
1988 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
1989 "Emask 0x%x (%s)%s\n",
1990 cmd->command, cmd->feature, cmd->nsect,
1991 cmd->lbal, cmd->lbam, cmd->lbah,
1992 cmd->hob_feature, cmd->hob_nsect,
1993 cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
1994 cmd->device, qc->tag, data_buf, cdb_buf,
1995 res->command, res->feature, res->nsect,
1996 res->lbal, res->lbam, res->lbah,
1997 res->hob_feature, res->hob_nsect,
1998 res->hob_lbal, res->hob_lbam, res->hob_lbah,
1999 res->device, qc->err_mask, ata_err_string(qc->err_mask),
2000 qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
2002 if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
2003 ATA_ERR)) {
2004 if (res->command & ATA_BUSY)
2005 ata_dev_printk(qc->dev, KERN_ERR,
2006 "status: { Busy }\n");
2007 else
2008 ata_dev_printk(qc->dev, KERN_ERR,
2009 "status: { %s%s%s%s}\n",
2010 res->command & ATA_DRDY ? "DRDY " : "",
2011 res->command & ATA_DF ? "DF " : "",
2012 res->command & ATA_DRQ ? "DRQ " : "",
2013 res->command & ATA_ERR ? "ERR " : "");
2016 if (cmd->command != ATA_CMD_PACKET &&
2017 (res->feature & (ATA_ICRC | ATA_UNC | ATA_IDNF |
2018 ATA_ABORTED)))
2019 ata_dev_printk(qc->dev, KERN_ERR,
2020 "error: { %s%s%s%s}\n",
2021 res->feature & ATA_ICRC ? "ICRC " : "",
2022 res->feature & ATA_UNC ? "UNC " : "",
2023 res->feature & ATA_IDNF ? "IDNF " : "",
2024 res->feature & ATA_ABORTED ? "ABRT " : "");
2029 * ata_eh_report - report error handling to user
2030 * @ap: ATA port to report EH about
2032 * Report EH to user.
2034 * LOCKING:
2035 * None.
2037 void ata_eh_report(struct ata_port *ap)
2039 struct ata_link *link;
2041 __ata_port_for_each_link(link, ap)
2042 ata_eh_link_report(link);
2045 static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
2046 unsigned int *classes, unsigned long deadline)
2048 struct ata_device *dev;
2049 int rc;
2051 ata_link_for_each_dev(dev, link)
2052 classes[dev->devno] = ATA_DEV_UNKNOWN;
2054 rc = reset(link, classes, deadline);
2055 if (rc)
2056 return rc;
2058 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2059 * is complete and convert all ATA_DEV_UNKNOWN to
2060 * ATA_DEV_NONE.
2062 ata_link_for_each_dev(dev, link)
2063 if (classes[dev->devno] != ATA_DEV_UNKNOWN)
2064 break;
2066 if (dev) {
2067 ata_link_for_each_dev(dev, link) {
2068 if (classes[dev->devno] == ATA_DEV_UNKNOWN)
2069 classes[dev->devno] = ATA_DEV_NONE;
2073 return 0;
2076 static int ata_eh_followup_srst_needed(struct ata_link *link,
2077 int rc, int classify,
2078 const unsigned int *classes)
2080 if (link->flags & ATA_LFLAG_NO_SRST)
2081 return 0;
2082 if (rc == -EAGAIN)
2083 return 1;
2084 if (rc != 0)
2085 return 0;
2086 if ((link->ap->flags & ATA_FLAG_PMP) && ata_is_host_link(link))
2087 return 1;
2088 if (classify && !(link->flags & ATA_LFLAG_ASSUME_CLASS) &&
2089 classes[0] == ATA_DEV_UNKNOWN)
2090 return 1;
2091 return 0;
2094 int ata_eh_reset(struct ata_link *link, int classify,
2095 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
2096 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
2098 const int max_tries = ARRAY_SIZE(ata_eh_reset_timeouts);
2099 struct ata_port *ap = link->ap;
2100 struct ata_eh_context *ehc = &link->eh_context;
2101 unsigned int *classes = ehc->classes;
2102 unsigned int lflags = link->flags;
2103 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
2104 int try = 0;
2105 struct ata_device *dev;
2106 unsigned long deadline, now;
2107 ata_reset_fn_t reset;
2108 unsigned long flags;
2109 u32 sstatus;
2110 int rc;
2112 /* about to reset */
2113 spin_lock_irqsave(ap->lock, flags);
2114 ap->pflags |= ATA_PFLAG_RESETTING;
2115 spin_unlock_irqrestore(ap->lock, flags);
2117 ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
2119 ata_link_for_each_dev(dev, link) {
2120 /* If we issue an SRST then an ATA drive (not ATAPI)
2121 * may change configuration and be in PIO0 timing. If
2122 * we do a hard reset (or are coming from power on)
2123 * this is true for ATA or ATAPI. Until we've set a
2124 * suitable controller mode we should not touch the
2125 * bus as we may be talking too fast.
2127 dev->pio_mode = XFER_PIO_0;
2129 /* If the controller has a pio mode setup function
2130 * then use it to set the chipset to rights. Don't
2131 * touch the DMA setup as that will be dealt with when
2132 * configuring devices.
2134 if (ap->ops->set_piomode)
2135 ap->ops->set_piomode(ap, dev);
2138 if (!softreset && !hardreset) {
2139 if (verbose)
2140 ata_link_printk(link, KERN_INFO, "no reset method "
2141 "available, skipping reset\n");
2142 if (!(lflags & ATA_LFLAG_ASSUME_CLASS))
2143 lflags |= ATA_LFLAG_ASSUME_ATA;
2144 goto done;
2147 /* prefer hardreset */
2148 ehc->i.action &= ~ATA_EH_RESET;
2149 if (hardreset) {
2150 reset = hardreset;
2151 ehc->i.action = ATA_EH_HARDRESET;
2152 } else {
2153 reset = softreset;
2154 ehc->i.action = ATA_EH_SOFTRESET;
2157 if (prereset) {
2158 rc = prereset(link, jiffies + ATA_EH_PRERESET_TIMEOUT);
2159 if (rc) {
2160 if (rc == -ENOENT) {
2161 ata_link_printk(link, KERN_DEBUG,
2162 "port disabled. ignoring.\n");
2163 ehc->i.action &= ~ATA_EH_RESET;
2165 ata_link_for_each_dev(dev, link)
2166 classes[dev->devno] = ATA_DEV_NONE;
2168 rc = 0;
2169 } else
2170 ata_link_printk(link, KERN_ERR,
2171 "prereset failed (errno=%d)\n", rc);
2172 goto out;
2176 /* prereset() might have cleared ATA_EH_RESET */
2177 if (!(ehc->i.action & ATA_EH_RESET)) {
2178 /* prereset told us not to reset, bang classes and return */
2179 ata_link_for_each_dev(dev, link)
2180 classes[dev->devno] = ATA_DEV_NONE;
2181 rc = 0;
2182 goto out;
2185 retry:
2186 deadline = jiffies + ata_eh_reset_timeouts[try++];
2188 /* shut up during boot probing */
2189 if (verbose)
2190 ata_link_printk(link, KERN_INFO, "%s resetting link\n",
2191 reset == softreset ? "soft" : "hard");
2193 /* mark that this EH session started with reset */
2194 if (reset == hardreset)
2195 ehc->i.flags |= ATA_EHI_DID_HARDRESET;
2196 else
2197 ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
2199 rc = ata_do_reset(link, reset, classes, deadline);
2201 if (reset == hardreset &&
2202 ata_eh_followup_srst_needed(link, rc, classify, classes)) {
2203 /* okay, let's do follow-up softreset */
2204 reset = softreset;
2206 if (!reset) {
2207 ata_link_printk(link, KERN_ERR,
2208 "follow-up softreset required "
2209 "but no softreset avaliable\n");
2210 rc = -EINVAL;
2211 goto fail;
2214 ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
2215 rc = ata_do_reset(link, reset, classes, deadline);
2218 /* -EAGAIN can happen if we skipped followup SRST */
2219 if (rc && rc != -EAGAIN)
2220 goto fail;
2222 /* was classification successful? */
2223 if (classify && classes[0] == ATA_DEV_UNKNOWN &&
2224 !(lflags & ATA_LFLAG_ASSUME_CLASS)) {
2225 if (try < max_tries) {
2226 ata_link_printk(link, KERN_WARNING,
2227 "classification failed\n");
2228 rc = -EINVAL;
2229 goto fail;
2232 ata_link_printk(link, KERN_WARNING,
2233 "classfication failed, assuming ATA\n");
2234 lflags |= ATA_LFLAG_ASSUME_ATA;
2237 done:
2238 ata_link_for_each_dev(dev, link) {
2239 /* After the reset, the device state is PIO 0 and the
2240 * controller state is undefined. Reset also wakes up
2241 * drives from sleeping mode.
2243 dev->pio_mode = XFER_PIO_0;
2244 dev->flags &= ~ATA_DFLAG_SLEEPING;
2246 if (ata_link_offline(link))
2247 continue;
2249 /* apply class override */
2250 if (lflags & ATA_LFLAG_ASSUME_ATA)
2251 classes[dev->devno] = ATA_DEV_ATA;
2252 else if (lflags & ATA_LFLAG_ASSUME_SEMB)
2253 classes[dev->devno] = ATA_DEV_SEMB_UNSUP; /* not yet */
2256 /* record current link speed */
2257 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2258 link->sata_spd = (sstatus >> 4) & 0xf;
2260 if (postreset)
2261 postreset(link, classes);
2263 /* reset successful, schedule revalidation */
2264 ata_eh_done(link, NULL, ATA_EH_RESET);
2265 ehc->i.action |= ATA_EH_REVALIDATE;
2267 rc = 0;
2268 out:
2269 /* clear hotplug flag */
2270 ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2272 spin_lock_irqsave(ap->lock, flags);
2273 ap->pflags &= ~ATA_PFLAG_RESETTING;
2274 spin_unlock_irqrestore(ap->lock, flags);
2276 return rc;
2278 fail:
2279 if (rc == -ERESTART || try >= max_tries)
2280 goto out;
2282 now = jiffies;
2283 if (time_before(now, deadline)) {
2284 unsigned long delta = deadline - now;
2286 ata_link_printk(link, KERN_WARNING, "reset failed "
2287 "(errno=%d), retrying in %u secs\n",
2288 rc, (jiffies_to_msecs(delta) + 999) / 1000);
2290 while (delta)
2291 delta = schedule_timeout_uninterruptible(delta);
2294 if (rc == -EPIPE || try == max_tries - 1)
2295 sata_down_spd_limit(link);
2296 if (hardreset)
2297 reset = hardreset;
2298 goto retry;
2301 static int ata_eh_revalidate_and_attach(struct ata_link *link,
2302 struct ata_device **r_failed_dev)
2304 struct ata_port *ap = link->ap;
2305 struct ata_eh_context *ehc = &link->eh_context;
2306 struct ata_device *dev;
2307 unsigned int new_mask = 0;
2308 unsigned long flags;
2309 int rc = 0;
2311 DPRINTK("ENTER\n");
2313 /* For PATA drive side cable detection to work, IDENTIFY must
2314 * be done backwards such that PDIAG- is released by the slave
2315 * device before the master device is identified.
2317 ata_link_for_each_dev_reverse(dev, link) {
2318 unsigned int action = ata_eh_dev_action(dev);
2319 unsigned int readid_flags = 0;
2321 if (ehc->i.flags & ATA_EHI_DID_RESET)
2322 readid_flags |= ATA_READID_POSTRESET;
2324 if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
2325 WARN_ON(dev->class == ATA_DEV_PMP);
2327 if (ata_link_offline(link)) {
2328 rc = -EIO;
2329 goto err;
2332 ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
2333 rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
2334 readid_flags);
2335 if (rc)
2336 goto err;
2338 ata_eh_done(link, dev, ATA_EH_REVALIDATE);
2340 /* Configuration may have changed, reconfigure
2341 * transfer mode.
2343 ehc->i.flags |= ATA_EHI_SETMODE;
2345 /* schedule the scsi_rescan_device() here */
2346 queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
2347 } else if (dev->class == ATA_DEV_UNKNOWN &&
2348 ehc->tries[dev->devno] &&
2349 ata_class_enabled(ehc->classes[dev->devno])) {
2350 dev->class = ehc->classes[dev->devno];
2352 if (dev->class == ATA_DEV_PMP)
2353 rc = sata_pmp_attach(dev);
2354 else
2355 rc = ata_dev_read_id(dev, &dev->class,
2356 readid_flags, dev->id);
2357 switch (rc) {
2358 case 0:
2359 new_mask |= 1 << dev->devno;
2360 break;
2361 case -ENOENT:
2362 /* IDENTIFY was issued to non-existent
2363 * device. No need to reset. Just
2364 * thaw and kill the device.
2366 ata_eh_thaw_port(ap);
2367 dev->class = ATA_DEV_UNKNOWN;
2368 break;
2369 default:
2370 dev->class = ATA_DEV_UNKNOWN;
2371 goto err;
2376 /* PDIAG- should have been released, ask cable type if post-reset */
2377 if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) {
2378 if (ap->ops->cable_detect)
2379 ap->cbl = ap->ops->cable_detect(ap);
2380 ata_force_cbl(ap);
2383 /* Configure new devices forward such that user doesn't see
2384 * device detection messages backwards.
2386 ata_link_for_each_dev(dev, link) {
2387 if (!(new_mask & (1 << dev->devno)) ||
2388 dev->class == ATA_DEV_PMP)
2389 continue;
2391 ehc->i.flags |= ATA_EHI_PRINTINFO;
2392 rc = ata_dev_configure(dev);
2393 ehc->i.flags &= ~ATA_EHI_PRINTINFO;
2394 if (rc)
2395 goto err;
2397 spin_lock_irqsave(ap->lock, flags);
2398 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
2399 spin_unlock_irqrestore(ap->lock, flags);
2401 /* new device discovered, configure xfermode */
2402 ehc->i.flags |= ATA_EHI_SETMODE;
2405 return 0;
2407 err:
2408 *r_failed_dev = dev;
2409 DPRINTK("EXIT rc=%d\n", rc);
2410 return rc;
2414 * ata_set_mode - Program timings and issue SET FEATURES - XFER
2415 * @link: link on which timings will be programmed
2416 * @r_failed_dev: out paramter for failed device
2418 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
2419 * ata_set_mode() fails, pointer to the failing device is
2420 * returned in @r_failed_dev.
2422 * LOCKING:
2423 * PCI/etc. bus probe sem.
2425 * RETURNS:
2426 * 0 on success, negative errno otherwise
2428 int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
2430 struct ata_port *ap = link->ap;
2431 struct ata_device *dev;
2432 int rc;
2434 /* if data transfer is verified, clear DUBIOUS_XFER on ering top */
2435 ata_link_for_each_dev(dev, link) {
2436 if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
2437 struct ata_ering_entry *ent;
2439 ent = ata_ering_top(&dev->ering);
2440 if (ent)
2441 ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER;
2445 /* has private set_mode? */
2446 if (ap->ops->set_mode)
2447 rc = ap->ops->set_mode(link, r_failed_dev);
2448 else
2449 rc = ata_do_set_mode(link, r_failed_dev);
2451 /* if transfer mode has changed, set DUBIOUS_XFER on device */
2452 ata_link_for_each_dev(dev, link) {
2453 struct ata_eh_context *ehc = &link->eh_context;
2454 u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
2455 u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
2457 if (dev->xfer_mode != saved_xfer_mode ||
2458 ata_ncq_enabled(dev) != saved_ncq)
2459 dev->flags |= ATA_DFLAG_DUBIOUS_XFER;
2462 return rc;
2465 static int ata_link_nr_enabled(struct ata_link *link)
2467 struct ata_device *dev;
2468 int cnt = 0;
2470 ata_link_for_each_dev(dev, link)
2471 if (ata_dev_enabled(dev))
2472 cnt++;
2473 return cnt;
2476 static int ata_link_nr_vacant(struct ata_link *link)
2478 struct ata_device *dev;
2479 int cnt = 0;
2481 ata_link_for_each_dev(dev, link)
2482 if (dev->class == ATA_DEV_UNKNOWN)
2483 cnt++;
2484 return cnt;
2487 static int ata_eh_skip_recovery(struct ata_link *link)
2489 struct ata_port *ap = link->ap;
2490 struct ata_eh_context *ehc = &link->eh_context;
2491 struct ata_device *dev;
2493 /* skip disabled links */
2494 if (link->flags & ATA_LFLAG_DISABLED)
2495 return 1;
2497 /* thaw frozen port and recover failed devices */
2498 if ((ap->pflags & ATA_PFLAG_FROZEN) || ata_link_nr_enabled(link))
2499 return 0;
2501 /* reset at least once if reset is requested */
2502 if ((ehc->i.action & ATA_EH_RESET) &&
2503 !(ehc->i.flags & ATA_EHI_DID_RESET))
2504 return 0;
2506 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
2507 ata_link_for_each_dev(dev, link) {
2508 if (dev->class == ATA_DEV_UNKNOWN &&
2509 ehc->classes[dev->devno] != ATA_DEV_NONE)
2510 return 0;
2513 return 1;
2516 static int ata_eh_schedule_probe(struct ata_device *dev)
2518 struct ata_eh_context *ehc = &dev->link->eh_context;
2520 if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
2521 (ehc->did_probe_mask & (1 << dev->devno)))
2522 return 0;
2524 ata_eh_detach_dev(dev);
2525 ata_dev_init(dev);
2526 ehc->did_probe_mask |= (1 << dev->devno);
2527 ehc->i.action |= ATA_EH_RESET;
2528 ehc->saved_xfer_mode[dev->devno] = 0;
2529 ehc->saved_ncq_enabled &= ~(1 << dev->devno);
2531 return 1;
2534 static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
2536 struct ata_eh_context *ehc = &dev->link->eh_context;
2538 ehc->tries[dev->devno]--;
2540 switch (err) {
2541 case -ENODEV:
2542 /* device missing or wrong IDENTIFY data, schedule probing */
2543 ehc->i.probe_mask |= (1 << dev->devno);
2544 case -EINVAL:
2545 /* give it just one more chance */
2546 ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
2547 case -EIO:
2548 if (ehc->tries[dev->devno] == 1 && dev->pio_mode > XFER_PIO_0) {
2549 /* This is the last chance, better to slow
2550 * down than lose it.
2552 sata_down_spd_limit(dev->link);
2553 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2557 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2558 /* disable device if it has used up all its chances */
2559 ata_dev_disable(dev);
2561 /* detach if offline */
2562 if (ata_link_offline(dev->link))
2563 ata_eh_detach_dev(dev);
2565 /* schedule probe if necessary */
2566 if (ata_eh_schedule_probe(dev))
2567 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2569 return 1;
2570 } else {
2571 ehc->i.action |= ATA_EH_RESET;
2572 return 0;
2577 * ata_eh_recover - recover host port after error
2578 * @ap: host port to recover
2579 * @prereset: prereset method (can be NULL)
2580 * @softreset: softreset method (can be NULL)
2581 * @hardreset: hardreset method (can be NULL)
2582 * @postreset: postreset method (can be NULL)
2583 * @r_failed_link: out parameter for failed link
2585 * This is the alpha and omega, eum and yang, heart and soul of
2586 * libata exception handling. On entry, actions required to
2587 * recover each link and hotplug requests are recorded in the
2588 * link's eh_context. This function executes all the operations
2589 * with appropriate retrials and fallbacks to resurrect failed
2590 * devices, detach goners and greet newcomers.
2592 * LOCKING:
2593 * Kernel thread context (may sleep).
2595 * RETURNS:
2596 * 0 on success, -errno on failure.
2598 int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
2599 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2600 ata_postreset_fn_t postreset,
2601 struct ata_link **r_failed_link)
2603 struct ata_link *link;
2604 struct ata_device *dev;
2605 int nr_failed_devs, nr_disabled_devs;
2606 int reset, rc;
2607 unsigned long flags;
2609 DPRINTK("ENTER\n");
2611 /* prep for recovery */
2612 ata_port_for_each_link(link, ap) {
2613 struct ata_eh_context *ehc = &link->eh_context;
2615 /* re-enable link? */
2616 if (ehc->i.action & ATA_EH_ENABLE_LINK) {
2617 ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
2618 spin_lock_irqsave(ap->lock, flags);
2619 link->flags &= ~ATA_LFLAG_DISABLED;
2620 spin_unlock_irqrestore(ap->lock, flags);
2621 ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
2624 ata_link_for_each_dev(dev, link) {
2625 if (link->flags & ATA_LFLAG_NO_RETRY)
2626 ehc->tries[dev->devno] = 1;
2627 else
2628 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2630 /* collect port action mask recorded in dev actions */
2631 ehc->i.action |= ehc->i.dev_action[dev->devno] &
2632 ~ATA_EH_PERDEV_MASK;
2633 ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
2635 /* process hotplug request */
2636 if (dev->flags & ATA_DFLAG_DETACH)
2637 ata_eh_detach_dev(dev);
2639 /* schedule probe if necessary */
2640 if (!ata_dev_enabled(dev))
2641 ata_eh_schedule_probe(dev);
2645 retry:
2646 rc = 0;
2647 nr_failed_devs = 0;
2648 nr_disabled_devs = 0;
2649 reset = 0;
2651 /* if UNLOADING, finish immediately */
2652 if (ap->pflags & ATA_PFLAG_UNLOADING)
2653 goto out;
2655 /* prep for EH */
2656 ata_port_for_each_link(link, ap) {
2657 struct ata_eh_context *ehc = &link->eh_context;
2659 /* skip EH if possible. */
2660 if (ata_eh_skip_recovery(link))
2661 ehc->i.action = 0;
2663 /* do we need to reset? */
2664 if (ehc->i.action & ATA_EH_RESET)
2665 reset = 1;
2667 ata_link_for_each_dev(dev, link)
2668 ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
2671 /* reset */
2672 if (reset) {
2673 /* if PMP is attached, this function only deals with
2674 * downstream links, port should stay thawed.
2676 if (!ap->nr_pmp_links)
2677 ata_eh_freeze_port(ap);
2679 ata_port_for_each_link(link, ap) {
2680 struct ata_eh_context *ehc = &link->eh_context;
2682 if (!(ehc->i.action & ATA_EH_RESET))
2683 continue;
2685 rc = ata_eh_reset(link, ata_link_nr_vacant(link),
2686 prereset, softreset, hardreset,
2687 postreset);
2688 if (rc) {
2689 ata_link_printk(link, KERN_ERR,
2690 "reset failed, giving up\n");
2691 goto out;
2695 if (!ap->nr_pmp_links)
2696 ata_eh_thaw_port(ap);
2699 /* the rest */
2700 ata_port_for_each_link(link, ap) {
2701 struct ata_eh_context *ehc = &link->eh_context;
2703 /* revalidate existing devices and attach new ones */
2704 rc = ata_eh_revalidate_and_attach(link, &dev);
2705 if (rc)
2706 goto dev_fail;
2708 /* if PMP got attached, return, pmp EH will take care of it */
2709 if (link->device->class == ATA_DEV_PMP) {
2710 ehc->i.action = 0;
2711 return 0;
2714 /* configure transfer mode if necessary */
2715 if (ehc->i.flags & ATA_EHI_SETMODE) {
2716 rc = ata_set_mode(link, &dev);
2717 if (rc)
2718 goto dev_fail;
2719 ehc->i.flags &= ~ATA_EHI_SETMODE;
2722 if (ehc->i.action & ATA_EH_LPM)
2723 ata_link_for_each_dev(dev, link)
2724 ata_dev_enable_pm(dev, ap->pm_policy);
2726 /* this link is okay now */
2727 ehc->i.flags = 0;
2728 continue;
2730 dev_fail:
2731 nr_failed_devs++;
2732 if (ata_eh_handle_dev_fail(dev, rc))
2733 nr_disabled_devs++;
2735 if (ap->pflags & ATA_PFLAG_FROZEN) {
2736 /* PMP reset requires working host port.
2737 * Can't retry if it's frozen.
2739 if (ap->nr_pmp_links)
2740 goto out;
2741 break;
2745 if (nr_failed_devs) {
2746 if (nr_failed_devs != nr_disabled_devs) {
2747 ata_port_printk(ap, KERN_WARNING, "failed to recover "
2748 "some devices, retrying in 5 secs\n");
2749 ssleep(5);
2750 } else {
2751 /* no device left to recover, repeat fast */
2752 msleep(500);
2755 goto retry;
2758 out:
2759 if (rc && r_failed_link)
2760 *r_failed_link = link;
2762 DPRINTK("EXIT, rc=%d\n", rc);
2763 return rc;
2767 * ata_eh_finish - finish up EH
2768 * @ap: host port to finish EH for
2770 * Recovery is complete. Clean up EH states and retry or finish
2771 * failed qcs.
2773 * LOCKING:
2774 * None.
2776 void ata_eh_finish(struct ata_port *ap)
2778 int tag;
2780 /* retry or finish qcs */
2781 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2782 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2784 if (!(qc->flags & ATA_QCFLAG_FAILED))
2785 continue;
2787 if (qc->err_mask) {
2788 /* FIXME: Once EH migration is complete,
2789 * generate sense data in this function,
2790 * considering both err_mask and tf.
2792 if (qc->flags & ATA_QCFLAG_RETRY)
2793 ata_eh_qc_retry(qc);
2794 else
2795 ata_eh_qc_complete(qc);
2796 } else {
2797 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2798 ata_eh_qc_complete(qc);
2799 } else {
2800 /* feed zero TF to sense generation */
2801 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2802 ata_eh_qc_retry(qc);
2807 /* make sure nr_active_links is zero after EH */
2808 WARN_ON(ap->nr_active_links);
2809 ap->nr_active_links = 0;
2813 * ata_do_eh - do standard error handling
2814 * @ap: host port to handle error for
2816 * @prereset: prereset method (can be NULL)
2817 * @softreset: softreset method (can be NULL)
2818 * @hardreset: hardreset method (can be NULL)
2819 * @postreset: postreset method (can be NULL)
2821 * Perform standard error handling sequence.
2823 * LOCKING:
2824 * Kernel thread context (may sleep).
2826 void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2827 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2828 ata_postreset_fn_t postreset)
2830 struct ata_device *dev;
2831 int rc;
2833 ata_eh_autopsy(ap);
2834 ata_eh_report(ap);
2836 rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
2837 NULL);
2838 if (rc) {
2839 ata_link_for_each_dev(dev, &ap->link)
2840 ata_dev_disable(dev);
2843 ata_eh_finish(ap);
2847 * ata_std_error_handler - standard error handler
2848 * @ap: host port to handle error for
2850 * Standard error handler
2852 * LOCKING:
2853 * Kernel thread context (may sleep).
2855 void ata_std_error_handler(struct ata_port *ap)
2857 struct ata_port_operations *ops = ap->ops;
2858 ata_reset_fn_t hardreset = ops->hardreset;
2860 /* sata_std_hardreset is inherited to all drivers from
2861 * ata_base_port_ops. Ignore it if SCR access is not
2862 * available.
2864 if (hardreset == sata_std_hardreset && !sata_scr_valid(&ap->link))
2865 hardreset = NULL;
2867 ata_do_eh(ap, ops->prereset, ops->softreset, hardreset, ops->postreset);
2870 #ifdef CONFIG_PM
2872 * ata_eh_handle_port_suspend - perform port suspend operation
2873 * @ap: port to suspend
2875 * Suspend @ap.
2877 * LOCKING:
2878 * Kernel thread context (may sleep).
2880 static void ata_eh_handle_port_suspend(struct ata_port *ap)
2882 unsigned long flags;
2883 int rc = 0;
2885 /* are we suspending? */
2886 spin_lock_irqsave(ap->lock, flags);
2887 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2888 ap->pm_mesg.event == PM_EVENT_ON) {
2889 spin_unlock_irqrestore(ap->lock, flags);
2890 return;
2892 spin_unlock_irqrestore(ap->lock, flags);
2894 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
2896 /* tell ACPI we're suspending */
2897 rc = ata_acpi_on_suspend(ap);
2898 if (rc)
2899 goto out;
2901 /* suspend */
2902 ata_eh_freeze_port(ap);
2904 if (ap->ops->port_suspend)
2905 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
2907 ata_acpi_set_state(ap, PMSG_SUSPEND);
2908 out:
2909 /* report result */
2910 spin_lock_irqsave(ap->lock, flags);
2912 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
2913 if (rc == 0)
2914 ap->pflags |= ATA_PFLAG_SUSPENDED;
2915 else if (ap->pflags & ATA_PFLAG_FROZEN)
2916 ata_port_schedule_eh(ap);
2918 if (ap->pm_result) {
2919 *ap->pm_result = rc;
2920 ap->pm_result = NULL;
2923 spin_unlock_irqrestore(ap->lock, flags);
2925 return;
2929 * ata_eh_handle_port_resume - perform port resume operation
2930 * @ap: port to resume
2932 * Resume @ap.
2934 * LOCKING:
2935 * Kernel thread context (may sleep).
2937 static void ata_eh_handle_port_resume(struct ata_port *ap)
2939 unsigned long flags;
2940 int rc = 0;
2942 /* are we resuming? */
2943 spin_lock_irqsave(ap->lock, flags);
2944 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2945 ap->pm_mesg.event != PM_EVENT_ON) {
2946 spin_unlock_irqrestore(ap->lock, flags);
2947 return;
2949 spin_unlock_irqrestore(ap->lock, flags);
2951 WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
2953 ata_acpi_set_state(ap, PMSG_ON);
2955 if (ap->ops->port_resume)
2956 rc = ap->ops->port_resume(ap);
2958 /* tell ACPI that we're resuming */
2959 ata_acpi_on_resume(ap);
2961 /* report result */
2962 spin_lock_irqsave(ap->lock, flags);
2963 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
2964 if (ap->pm_result) {
2965 *ap->pm_result = rc;
2966 ap->pm_result = NULL;
2968 spin_unlock_irqrestore(ap->lock, flags);
2970 #endif /* CONFIG_PM */