2 * libata-eh.c - libata error handling
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
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,
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 <scsi/scsi.h>
37 #include <scsi/scsi_host.h>
38 #include <scsi/scsi_eh.h>
39 #include <scsi/scsi_device.h>
40 #include <scsi/scsi_cmnd.h>
41 #include "../scsi/scsi_transport_api.h"
43 #include <linux/libata.h>
48 ATA_EH_SPDN_NCQ_OFF = (1 << 0),
49 ATA_EH_SPDN_SPEED_DOWN = (1 << 1),
50 ATA_EH_SPDN_FALLBACK_TO_PIO = (1 << 2),
53 /* Waiting in ->prereset can never be reliable. It's sometimes nice
54 * to wait there but it can't be depended upon; otherwise, we wouldn't
55 * be resetting. Just give it enough time for most drives to spin up.
58 ATA_EH_PRERESET_TIMEOUT = 10 * HZ,
59 ATA_EH_FASTDRAIN_INTERVAL = 3 * HZ,
62 /* The following table determines how we sequence resets. Each entry
63 * represents timeout for that try. The first try can be soft or
64 * hardreset. All others are hardreset if available. In most cases
65 * the first reset w/ 10sec timeout should succeed. Following entries
66 * are mostly for error handling, hotplug and retarded devices.
68 static const unsigned long ata_eh_reset_timeouts[] = {
69 10 * HZ, /* most drives spin up by 10sec */
70 10 * HZ, /* > 99% working drives spin up before 20sec */
71 35 * HZ, /* give > 30 secs of idleness for retarded devices */
72 5 * HZ, /* and sweet one last chance */
73 /* > 1 min has elapsed, give up */
76 static void __ata_port_freeze(struct ata_port *ap);
78 static void ata_eh_handle_port_suspend(struct ata_port *ap);
79 static void ata_eh_handle_port_resume(struct ata_port *ap);
81 static void ata_eh_handle_port_suspend(struct ata_port *ap)
84 static void ata_eh_handle_port_resume(struct ata_port *ap)
86 #endif /* CONFIG_PM */
88 static void __ata_ehi_pushv_desc(struct ata_eh_info *ehi, const char *fmt,
91 ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
92 ATA_EH_DESC_LEN - ehi->desc_len,
97 * __ata_ehi_push_desc - push error description without adding separator
99 * @fmt: printf format string
101 * Format string according to @fmt and append it to @ehi->desc.
104 * spin_lock_irqsave(host lock)
106 void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
111 __ata_ehi_pushv_desc(ehi, fmt, args);
116 * ata_ehi_push_desc - push error description with separator
118 * @fmt: printf format string
120 * Format string according to @fmt and append it to @ehi->desc.
121 * If @ehi->desc is not empty, ", " is added in-between.
124 * spin_lock_irqsave(host lock)
126 void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
131 __ata_ehi_push_desc(ehi, ", ");
134 __ata_ehi_pushv_desc(ehi, fmt, args);
139 * ata_ehi_clear_desc - clean error description
145 * spin_lock_irqsave(host lock)
147 void ata_ehi_clear_desc(struct ata_eh_info *ehi)
154 * ata_port_desc - append port description
155 * @ap: target ATA port
156 * @fmt: printf format string
158 * Format string according to @fmt and append it to port
159 * description. If port description is not empty, " " is added
160 * in-between. This function is to be used while initializing
161 * ata_host. The description is printed on host registration.
166 void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
170 WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
172 if (ap->link.eh_info.desc_len)
173 __ata_ehi_push_desc(&ap->link.eh_info, " ");
176 __ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
183 * ata_port_pbar_desc - append PCI BAR description
184 * @ap: target ATA port
185 * @bar: target PCI BAR
186 * @offset: offset into PCI BAR
187 * @name: name of the area
189 * If @offset is negative, this function formats a string which
190 * contains the name, address, size and type of the BAR and
191 * appends it to the port description. If @offset is zero or
192 * positive, only name and offsetted address is appended.
197 void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
200 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
202 unsigned long long start, len;
204 if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
206 else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
209 start = (unsigned long long)pci_resource_start(pdev, bar);
210 len = (unsigned long long)pci_resource_len(pdev, bar);
213 ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
215 ata_port_desc(ap, "%s 0x%llx", name, start + offset);
218 #endif /* CONFIG_PCI */
220 static void ata_ering_record(struct ata_ering *ering, int is_io,
221 unsigned int err_mask)
223 struct ata_ering_entry *ent;
228 ering->cursor %= ATA_ERING_SIZE;
230 ent = &ering->ring[ering->cursor];
232 ent->err_mask = err_mask;
233 ent->timestamp = get_jiffies_64();
236 static void ata_ering_clear(struct ata_ering *ering)
238 memset(ering, 0, sizeof(*ering));
241 static int ata_ering_map(struct ata_ering *ering,
242 int (*map_fn)(struct ata_ering_entry *, void *),
246 struct ata_ering_entry *ent;
250 ent = &ering->ring[idx];
253 rc = map_fn(ent, arg);
256 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
257 } while (idx != ering->cursor);
262 static unsigned int ata_eh_dev_action(struct ata_device *dev)
264 struct ata_eh_context *ehc = &dev->link->eh_context;
266 return ehc->i.action | ehc->i.dev_action[dev->devno];
269 static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
270 struct ata_eh_info *ehi, unsigned int action)
272 struct ata_device *tdev;
275 ehi->action &= ~action;
276 ata_link_for_each_dev(tdev, link)
277 ehi->dev_action[tdev->devno] &= ~action;
279 /* doesn't make sense for port-wide EH actions */
280 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
282 /* break ehi->action into ehi->dev_action */
283 if (ehi->action & action) {
284 ata_link_for_each_dev(tdev, link)
285 ehi->dev_action[tdev->devno] |=
286 ehi->action & action;
287 ehi->action &= ~action;
290 /* turn off the specified per-dev action */
291 ehi->dev_action[dev->devno] &= ~action;
296 * ata_scsi_timed_out - SCSI layer time out callback
297 * @cmd: timed out SCSI command
299 * Handles SCSI layer timeout. We race with normal completion of
300 * the qc for @cmd. If the qc is already gone, we lose and let
301 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
302 * timed out and EH should be invoked. Prevent ata_qc_complete()
303 * from finishing it by setting EH_SCHEDULED and return
306 * TODO: kill this function once old EH is gone.
309 * Called from timer context
312 * EH_HANDLED or EH_NOT_HANDLED
314 enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
316 struct Scsi_Host *host = cmd->device->host;
317 struct ata_port *ap = ata_shost_to_port(host);
319 struct ata_queued_cmd *qc;
320 enum scsi_eh_timer_return ret;
324 if (ap->ops->error_handler) {
325 ret = EH_NOT_HANDLED;
330 spin_lock_irqsave(ap->lock, flags);
331 qc = ata_qc_from_tag(ap, ap->link.active_tag);
333 WARN_ON(qc->scsicmd != cmd);
334 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
335 qc->err_mask |= AC_ERR_TIMEOUT;
336 ret = EH_NOT_HANDLED;
338 spin_unlock_irqrestore(ap->lock, flags);
341 DPRINTK("EXIT, ret=%d\n", ret);
346 * ata_scsi_error - SCSI layer error handler callback
347 * @host: SCSI host on which error occurred
349 * Handles SCSI-layer-thrown error events.
352 * Inherited from SCSI layer (none, can sleep)
357 void ata_scsi_error(struct Scsi_Host *host)
359 struct ata_port *ap = ata_shost_to_port(host);
365 /* synchronize with port task */
366 ata_port_flush_task(ap);
368 /* synchronize with host lock and sort out timeouts */
370 /* For new EH, all qcs are finished in one of three ways -
371 * normal completion, error completion, and SCSI timeout.
372 * Both cmpletions can race against SCSI timeout. When normal
373 * completion wins, the qc never reaches EH. When error
374 * completion wins, the qc has ATA_QCFLAG_FAILED set.
376 * When SCSI timeout wins, things are a bit more complex.
377 * Normal or error completion can occur after the timeout but
378 * before this point. In such cases, both types of
379 * completions are honored. A scmd is determined to have
380 * timed out iff its associated qc is active and not failed.
382 if (ap->ops->error_handler) {
383 struct scsi_cmnd *scmd, *tmp;
386 spin_lock_irqsave(ap->lock, flags);
388 list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
389 struct ata_queued_cmd *qc;
391 for (i = 0; i < ATA_MAX_QUEUE; i++) {
392 qc = __ata_qc_from_tag(ap, i);
393 if (qc->flags & ATA_QCFLAG_ACTIVE &&
398 if (i < ATA_MAX_QUEUE) {
399 /* the scmd has an associated qc */
400 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
401 /* which hasn't failed yet, timeout */
402 qc->err_mask |= AC_ERR_TIMEOUT;
403 qc->flags |= ATA_QCFLAG_FAILED;
407 /* Normal completion occurred after
408 * SCSI timeout but before this point.
409 * Successfully complete it.
411 scmd->retries = scmd->allowed;
412 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
416 /* If we have timed out qcs. They belong to EH from
417 * this point but the state of the controller is
418 * unknown. Freeze the port to make sure the IRQ
419 * handler doesn't diddle with those qcs. This must
420 * be done atomically w.r.t. setting QCFLAG_FAILED.
423 __ata_port_freeze(ap);
425 spin_unlock_irqrestore(ap->lock, flags);
427 /* initialize eh_tries */
428 ap->eh_tries = ATA_EH_MAX_TRIES;
430 spin_unlock_wait(ap->lock);
433 /* invoke error handler */
434 if (ap->ops->error_handler) {
435 struct ata_link *link;
437 /* kill fast drain timer */
438 del_timer_sync(&ap->fastdrain_timer);
440 /* process port resume request */
441 ata_eh_handle_port_resume(ap);
443 /* fetch & clear EH info */
444 spin_lock_irqsave(ap->lock, flags);
446 __ata_port_for_each_link(link, ap) {
447 memset(&link->eh_context, 0, sizeof(link->eh_context));
448 link->eh_context.i = link->eh_info;
449 memset(&link->eh_info, 0, sizeof(link->eh_info));
452 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
453 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
455 spin_unlock_irqrestore(ap->lock, flags);
457 /* invoke EH, skip if unloading or suspended */
458 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
459 ap->ops->error_handler(ap);
463 /* process port suspend request */
464 ata_eh_handle_port_suspend(ap);
466 /* Exception might have happend after ->error_handler
467 * recovered the port but before this point. Repeat
470 spin_lock_irqsave(ap->lock, flags);
472 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
473 if (--ap->eh_tries) {
474 spin_unlock_irqrestore(ap->lock, flags);
477 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
478 "tries, giving up\n", ATA_EH_MAX_TRIES);
479 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
482 /* this run is complete, make sure EH info is clear */
483 __ata_port_for_each_link(link, ap)
484 memset(&link->eh_info, 0, sizeof(link->eh_info));
486 /* Clear host_eh_scheduled while holding ap->lock such
487 * that if exception occurs after this point but
488 * before EH completion, SCSI midlayer will
491 host->host_eh_scheduled = 0;
493 spin_unlock_irqrestore(ap->lock, flags);
495 WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
496 ap->ops->eng_timeout(ap);
499 /* finish or retry handled scmd's and clean up */
500 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
502 scsi_eh_flush_done_q(&ap->eh_done_q);
505 spin_lock_irqsave(ap->lock, flags);
507 if (ap->pflags & ATA_PFLAG_LOADING)
508 ap->pflags &= ~ATA_PFLAG_LOADING;
509 else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
510 queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0);
512 if (ap->pflags & ATA_PFLAG_RECOVERED)
513 ata_port_printk(ap, KERN_INFO, "EH complete\n");
515 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
517 /* tell wait_eh that we're done */
518 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
519 wake_up_all(&ap->eh_wait_q);
521 spin_unlock_irqrestore(ap->lock, flags);
527 * ata_port_wait_eh - Wait for the currently pending EH to complete
528 * @ap: Port to wait EH for
530 * Wait until the currently pending EH is complete.
533 * Kernel thread context (may sleep).
535 void ata_port_wait_eh(struct ata_port *ap)
541 spin_lock_irqsave(ap->lock, flags);
543 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
544 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
545 spin_unlock_irqrestore(ap->lock, flags);
547 spin_lock_irqsave(ap->lock, flags);
549 finish_wait(&ap->eh_wait_q, &wait);
551 spin_unlock_irqrestore(ap->lock, flags);
553 /* make sure SCSI EH is complete */
554 if (scsi_host_in_recovery(ap->scsi_host)) {
561 * ata_qc_timeout - Handle timeout of queued command
562 * @qc: Command that timed out
564 * Some part of the kernel (currently, only the SCSI layer)
565 * has noticed that the active command on port @ap has not
566 * completed after a specified length of time. Handle this
567 * condition by disabling DMA (if necessary) and completing
568 * transactions, with error if necessary.
570 * This also handles the case of the "lost interrupt", where
571 * for some reason (possibly hardware bug, possibly driver bug)
572 * an interrupt was not delivered to the driver, even though the
573 * transaction completed successfully.
575 * TODO: kill this function once old EH is gone.
578 * Inherited from SCSI layer (none, can sleep)
580 static void ata_qc_timeout(struct ata_queued_cmd *qc)
582 struct ata_port *ap = qc->ap;
583 u8 host_stat = 0, drv_stat;
588 ap->hsm_task_state = HSM_ST_IDLE;
590 spin_lock_irqsave(ap->lock, flags);
592 switch (qc->tf.protocol) {
595 case ATA_PROT_ATAPI_DMA:
596 host_stat = ap->ops->bmdma_status(ap);
598 /* before we do anything else, clear DMA-Start bit */
599 ap->ops->bmdma_stop(qc);
605 drv_stat = ata_chk_status(ap);
607 /* ack bmdma irq events */
608 ap->ops->irq_clear(ap);
610 ata_dev_printk(qc->dev, KERN_ERR, "command 0x%x timeout, "
611 "stat 0x%x host_stat 0x%x\n",
612 qc->tf.command, drv_stat, host_stat);
614 /* complete taskfile transaction */
615 qc->err_mask |= AC_ERR_TIMEOUT;
619 spin_unlock_irqrestore(ap->lock, flags);
621 ata_eh_qc_complete(qc);
627 * ata_eng_timeout - Handle timeout of queued command
628 * @ap: Port on which timed-out command is active
630 * Some part of the kernel (currently, only the SCSI layer)
631 * has noticed that the active command on port @ap has not
632 * completed after a specified length of time. Handle this
633 * condition by disabling DMA (if necessary) and completing
634 * transactions, with error if necessary.
636 * This also handles the case of the "lost interrupt", where
637 * for some reason (possibly hardware bug, possibly driver bug)
638 * an interrupt was not delivered to the driver, even though the
639 * transaction completed successfully.
641 * TODO: kill this function once old EH is gone.
644 * Inherited from SCSI layer (none, can sleep)
646 void ata_eng_timeout(struct ata_port *ap)
650 ata_qc_timeout(ata_qc_from_tag(ap, ap->link.active_tag));
655 static int ata_eh_nr_in_flight(struct ata_port *ap)
660 /* count only non-internal commands */
661 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++)
662 if (ata_qc_from_tag(ap, tag))
668 void ata_eh_fastdrain_timerfn(unsigned long arg)
670 struct ata_port *ap = (void *)arg;
674 spin_lock_irqsave(ap->lock, flags);
676 cnt = ata_eh_nr_in_flight(ap);
682 if (cnt == ap->fastdrain_cnt) {
685 /* No progress during the last interval, tag all
686 * in-flight qcs as timed out and freeze the port.
688 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) {
689 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
691 qc->err_mask |= AC_ERR_TIMEOUT;
696 /* some qcs have finished, give it another chance */
697 ap->fastdrain_cnt = cnt;
698 ap->fastdrain_timer.expires =
699 jiffies + ATA_EH_FASTDRAIN_INTERVAL;
700 add_timer(&ap->fastdrain_timer);
704 spin_unlock_irqrestore(ap->lock, flags);
708 * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
709 * @ap: target ATA port
710 * @fastdrain: activate fast drain
712 * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
713 * is non-zero and EH wasn't pending before. Fast drain ensures
714 * that EH kicks in in timely manner.
717 * spin_lock_irqsave(host lock)
719 static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
723 /* already scheduled? */
724 if (ap->pflags & ATA_PFLAG_EH_PENDING)
727 ap->pflags |= ATA_PFLAG_EH_PENDING;
732 /* do we have in-flight qcs? */
733 cnt = ata_eh_nr_in_flight(ap);
737 /* activate fast drain */
738 ap->fastdrain_cnt = cnt;
739 ap->fastdrain_timer.expires = jiffies + ATA_EH_FASTDRAIN_INTERVAL;
740 add_timer(&ap->fastdrain_timer);
744 * ata_qc_schedule_eh - schedule qc for error handling
745 * @qc: command to schedule error handling for
747 * Schedule error handling for @qc. EH will kick in as soon as
748 * other commands are drained.
751 * spin_lock_irqsave(host lock)
753 void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
755 struct ata_port *ap = qc->ap;
757 WARN_ON(!ap->ops->error_handler);
759 qc->flags |= ATA_QCFLAG_FAILED;
760 ata_eh_set_pending(ap, 1);
762 /* The following will fail if timeout has already expired.
763 * ata_scsi_error() takes care of such scmds on EH entry.
764 * Note that ATA_QCFLAG_FAILED is unconditionally set after
765 * this function completes.
767 scsi_req_abort_cmd(qc->scsicmd);
771 * ata_port_schedule_eh - schedule error handling without a qc
772 * @ap: ATA port to schedule EH for
774 * Schedule error handling for @ap. EH will kick in as soon as
775 * all commands are drained.
778 * spin_lock_irqsave(host lock)
780 void ata_port_schedule_eh(struct ata_port *ap)
782 WARN_ON(!ap->ops->error_handler);
784 if (ap->pflags & ATA_PFLAG_INITIALIZING)
787 ata_eh_set_pending(ap, 1);
788 scsi_schedule_eh(ap->scsi_host);
790 DPRINTK("port EH scheduled\n");
793 static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
795 int tag, nr_aborted = 0;
797 WARN_ON(!ap->ops->error_handler);
799 /* we're gonna abort all commands, no need for fast drain */
800 ata_eh_set_pending(ap, 0);
802 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
803 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
805 if (qc && (!link || qc->dev->link == link)) {
806 qc->flags |= ATA_QCFLAG_FAILED;
813 ata_port_schedule_eh(ap);
819 * ata_link_abort - abort all qc's on the link
820 * @link: ATA link to abort qc's for
822 * Abort all active qc's active on @link and schedule EH.
825 * spin_lock_irqsave(host lock)
828 * Number of aborted qc's.
830 int ata_link_abort(struct ata_link *link)
832 return ata_do_link_abort(link->ap, link);
836 * ata_port_abort - abort all qc's on the port
837 * @ap: ATA port to abort qc's for
839 * Abort all active qc's of @ap and schedule EH.
842 * spin_lock_irqsave(host_set lock)
845 * Number of aborted qc's.
847 int ata_port_abort(struct ata_port *ap)
849 return ata_do_link_abort(ap, NULL);
853 * __ata_port_freeze - freeze port
854 * @ap: ATA port to freeze
856 * This function is called when HSM violation or some other
857 * condition disrupts normal operation of the port. Frozen port
858 * is not allowed to perform any operation until the port is
859 * thawed, which usually follows a successful reset.
861 * ap->ops->freeze() callback can be used for freezing the port
862 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
863 * port cannot be frozen hardware-wise, the interrupt handler
864 * must ack and clear interrupts unconditionally while the port
868 * spin_lock_irqsave(host lock)
870 static void __ata_port_freeze(struct ata_port *ap)
872 WARN_ON(!ap->ops->error_handler);
877 ap->pflags |= ATA_PFLAG_FROZEN;
879 DPRINTK("ata%u port frozen\n", ap->print_id);
883 * ata_port_freeze - abort & freeze port
884 * @ap: ATA port to freeze
886 * Abort and freeze @ap.
889 * spin_lock_irqsave(host lock)
892 * Number of aborted commands.
894 int ata_port_freeze(struct ata_port *ap)
898 WARN_ON(!ap->ops->error_handler);
900 nr_aborted = ata_port_abort(ap);
901 __ata_port_freeze(ap);
907 * ata_eh_freeze_port - EH helper to freeze port
908 * @ap: ATA port to freeze
915 void ata_eh_freeze_port(struct ata_port *ap)
919 if (!ap->ops->error_handler)
922 spin_lock_irqsave(ap->lock, flags);
923 __ata_port_freeze(ap);
924 spin_unlock_irqrestore(ap->lock, flags);
928 * ata_port_thaw_port - EH helper to thaw port
929 * @ap: ATA port to thaw
931 * Thaw frozen port @ap.
936 void ata_eh_thaw_port(struct ata_port *ap)
940 if (!ap->ops->error_handler)
943 spin_lock_irqsave(ap->lock, flags);
945 ap->pflags &= ~ATA_PFLAG_FROZEN;
950 spin_unlock_irqrestore(ap->lock, flags);
952 DPRINTK("ata%u port thawed\n", ap->print_id);
955 static void ata_eh_scsidone(struct scsi_cmnd *scmd)
960 static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
962 struct ata_port *ap = qc->ap;
963 struct scsi_cmnd *scmd = qc->scsicmd;
966 spin_lock_irqsave(ap->lock, flags);
967 qc->scsidone = ata_eh_scsidone;
968 __ata_qc_complete(qc);
969 WARN_ON(ata_tag_valid(qc->tag));
970 spin_unlock_irqrestore(ap->lock, flags);
972 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
976 * ata_eh_qc_complete - Complete an active ATA command from EH
977 * @qc: Command to complete
979 * Indicate to the mid and upper layers that an ATA command has
980 * completed. To be used from EH.
982 void ata_eh_qc_complete(struct ata_queued_cmd *qc)
984 struct scsi_cmnd *scmd = qc->scsicmd;
985 scmd->retries = scmd->allowed;
986 __ata_eh_qc_complete(qc);
990 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
991 * @qc: Command to retry
993 * Indicate to the mid and upper layers that an ATA command
994 * should be retried. To be used from EH.
996 * SCSI midlayer limits the number of retries to scmd->allowed.
997 * scmd->retries is decremented for commands which get retried
998 * due to unrelated failures (qc->err_mask is zero).
1000 void ata_eh_qc_retry(struct ata_queued_cmd *qc)
1002 struct scsi_cmnd *scmd = qc->scsicmd;
1003 if (!qc->err_mask && scmd->retries)
1005 __ata_eh_qc_complete(qc);
1009 * ata_eh_detach_dev - detach ATA device
1010 * @dev: ATA device to detach
1017 void ata_eh_detach_dev(struct ata_device *dev)
1019 struct ata_link *link = dev->link;
1020 struct ata_port *ap = link->ap;
1021 unsigned long flags;
1023 ata_dev_disable(dev);
1025 spin_lock_irqsave(ap->lock, flags);
1027 dev->flags &= ~ATA_DFLAG_DETACH;
1029 if (ata_scsi_offline_dev(dev)) {
1030 dev->flags |= ATA_DFLAG_DETACHED;
1031 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
1034 /* clear per-dev EH actions */
1035 ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
1036 ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
1038 spin_unlock_irqrestore(ap->lock, flags);
1042 * ata_eh_about_to_do - about to perform eh_action
1043 * @link: target ATA link
1044 * @dev: target ATA dev for per-dev action (can be NULL)
1045 * @action: action about to be performed
1047 * Called just before performing EH actions to clear related bits
1048 * in @link->eh_info such that eh actions are not unnecessarily
1054 void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
1055 unsigned int action)
1057 struct ata_port *ap = link->ap;
1058 struct ata_eh_info *ehi = &link->eh_info;
1059 struct ata_eh_context *ehc = &link->eh_context;
1060 unsigned long flags;
1062 spin_lock_irqsave(ap->lock, flags);
1064 /* Reset is represented by combination of actions and EHI
1065 * flags. Suck in all related bits before clearing eh_info to
1066 * avoid losing requested action.
1068 if (action & ATA_EH_RESET_MASK) {
1069 ehc->i.action |= ehi->action & ATA_EH_RESET_MASK;
1070 ehc->i.flags |= ehi->flags & ATA_EHI_RESET_MODIFIER_MASK;
1072 /* make sure all reset actions are cleared & clear EHI flags */
1073 action |= ATA_EH_RESET_MASK;
1074 ehi->flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
1077 ata_eh_clear_action(link, dev, ehi, action);
1079 if (!(ehc->i.flags & ATA_EHI_QUIET))
1080 ap->pflags |= ATA_PFLAG_RECOVERED;
1082 spin_unlock_irqrestore(ap->lock, flags);
1086 * ata_eh_done - EH action complete
1087 * @ap: target ATA port
1088 * @dev: target ATA dev for per-dev action (can be NULL)
1089 * @action: action just completed
1091 * Called right after performing EH actions to clear related bits
1092 * in @link->eh_context.
1097 void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1098 unsigned int action)
1100 struct ata_eh_context *ehc = &link->eh_context;
1102 /* if reset is complete, clear all reset actions & reset modifier */
1103 if (action & ATA_EH_RESET_MASK) {
1104 action |= ATA_EH_RESET_MASK;
1105 ehc->i.flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
1108 ata_eh_clear_action(link, dev, &ehc->i, action);
1112 * ata_err_string - convert err_mask to descriptive string
1113 * @err_mask: error mask to convert to string
1115 * Convert @err_mask to descriptive string. Errors are
1116 * prioritized according to severity and only the most severe
1117 * error is reported.
1123 * Descriptive string for @err_mask
1125 static const char * ata_err_string(unsigned int err_mask)
1127 if (err_mask & AC_ERR_HOST_BUS)
1128 return "host bus error";
1129 if (err_mask & AC_ERR_ATA_BUS)
1130 return "ATA bus error";
1131 if (err_mask & AC_ERR_TIMEOUT)
1133 if (err_mask & AC_ERR_HSM)
1134 return "HSM violation";
1135 if (err_mask & AC_ERR_SYSTEM)
1136 return "internal error";
1137 if (err_mask & AC_ERR_MEDIA)
1138 return "media error";
1139 if (err_mask & AC_ERR_INVALID)
1140 return "invalid argument";
1141 if (err_mask & AC_ERR_DEV)
1142 return "device error";
1143 return "unknown error";
1147 * ata_read_log_page - read a specific log page
1148 * @dev: target device
1149 * @page: page to read
1150 * @buf: buffer to store read page
1151 * @sectors: number of sectors to read
1153 * Read log page using READ_LOG_EXT command.
1156 * Kernel thread context (may sleep).
1159 * 0 on success, AC_ERR_* mask otherwise.
1161 static unsigned int ata_read_log_page(struct ata_device *dev,
1162 u8 page, void *buf, unsigned int sectors)
1164 struct ata_taskfile tf;
1165 unsigned int err_mask;
1167 DPRINTK("read log page - page %d\n", page);
1169 ata_tf_init(dev, &tf);
1170 tf.command = ATA_CMD_READ_LOG_EXT;
1173 tf.hob_nsect = sectors >> 8;
1174 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1175 tf.protocol = ATA_PROT_PIO;
1177 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
1178 buf, sectors * ATA_SECT_SIZE);
1180 DPRINTK("EXIT, err_mask=%x\n", err_mask);
1185 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
1186 * @dev: Device to read log page 10h from
1187 * @tag: Resulting tag of the failed command
1188 * @tf: Resulting taskfile registers of the failed command
1190 * Read log page 10h to obtain NCQ error details and clear error
1194 * Kernel thread context (may sleep).
1197 * 0 on success, -errno otherwise.
1199 static int ata_eh_read_log_10h(struct ata_device *dev,
1200 int *tag, struct ata_taskfile *tf)
1202 u8 *buf = dev->link->ap->sector_buf;
1203 unsigned int err_mask;
1207 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
1212 for (i = 0; i < ATA_SECT_SIZE; i++)
1215 ata_dev_printk(dev, KERN_WARNING,
1216 "invalid checksum 0x%x on log page 10h\n", csum);
1221 *tag = buf[0] & 0x1f;
1223 tf->command = buf[2];
1224 tf->feature = buf[3];
1228 tf->device = buf[7];
1229 tf->hob_lbal = buf[8];
1230 tf->hob_lbam = buf[9];
1231 tf->hob_lbah = buf[10];
1232 tf->nsect = buf[12];
1233 tf->hob_nsect = buf[13];
1239 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1240 * @dev: device to perform REQUEST_SENSE to
1241 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
1243 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
1244 * SENSE. This function is EH helper.
1247 * Kernel thread context (may sleep).
1250 * 0 on success, AC_ERR_* mask on failure
1252 static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc)
1254 struct ata_device *dev = qc->dev;
1255 unsigned char *sense_buf = qc->scsicmd->sense_buffer;
1256 struct ata_port *ap = dev->link->ap;
1257 struct ata_taskfile tf;
1258 u8 cdb[ATAPI_CDB_LEN];
1260 DPRINTK("ATAPI request sense\n");
1262 /* FIXME: is this needed? */
1263 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1265 /* initialize sense_buf with the error register,
1266 * for the case where they are -not- overwritten
1268 sense_buf[0] = 0x70;
1269 sense_buf[2] = qc->result_tf.feature >> 4;
1271 /* some devices time out if garbage left in tf */
1272 ata_tf_init(dev, &tf);
1274 memset(cdb, 0, ATAPI_CDB_LEN);
1275 cdb[0] = REQUEST_SENSE;
1276 cdb[4] = SCSI_SENSE_BUFFERSIZE;
1278 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1279 tf.command = ATA_CMD_PACKET;
1281 /* is it pointless to prefer PIO for "safety reasons"? */
1282 if (ap->flags & ATA_FLAG_PIO_DMA) {
1283 tf.protocol = ATA_PROT_ATAPI_DMA;
1284 tf.feature |= ATAPI_PKT_DMA;
1286 tf.protocol = ATA_PROT_ATAPI;
1287 tf.lbam = (8 * 1024) & 0xff;
1288 tf.lbah = (8 * 1024) >> 8;
1291 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
1292 sense_buf, SCSI_SENSE_BUFFERSIZE);
1296 * ata_eh_analyze_serror - analyze SError for a failed port
1297 * @link: ATA link to analyze SError for
1299 * Analyze SError if available and further determine cause of
1305 static void ata_eh_analyze_serror(struct ata_link *link)
1307 struct ata_eh_context *ehc = &link->eh_context;
1308 u32 serror = ehc->i.serror;
1309 unsigned int err_mask = 0, action = 0;
1311 if (serror & SERR_PERSISTENT) {
1312 err_mask |= AC_ERR_ATA_BUS;
1313 action |= ATA_EH_HARDRESET;
1316 (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
1317 err_mask |= AC_ERR_ATA_BUS;
1318 action |= ATA_EH_SOFTRESET;
1320 if (serror & SERR_PROTOCOL) {
1321 err_mask |= AC_ERR_HSM;
1322 action |= ATA_EH_SOFTRESET;
1324 if (serror & SERR_INTERNAL) {
1325 err_mask |= AC_ERR_SYSTEM;
1326 action |= ATA_EH_HARDRESET;
1328 if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG))
1329 ata_ehi_hotplugged(&ehc->i);
1331 ehc->i.err_mask |= err_mask;
1332 ehc->i.action |= action;
1336 * ata_eh_analyze_ncq_error - analyze NCQ error
1337 * @link: ATA link to analyze NCQ error for
1339 * Read log page 10h, determine the offending qc and acquire
1340 * error status TF. For NCQ device errors, all LLDDs have to do
1341 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1345 * Kernel thread context (may sleep).
1347 static void ata_eh_analyze_ncq_error(struct ata_link *link)
1349 struct ata_port *ap = link->ap;
1350 struct ata_eh_context *ehc = &link->eh_context;
1351 struct ata_device *dev = link->device;
1352 struct ata_queued_cmd *qc;
1353 struct ata_taskfile tf;
1356 /* if frozen, we can't do much */
1357 if (ap->pflags & ATA_PFLAG_FROZEN)
1360 /* is it NCQ device error? */
1361 if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1364 /* has LLDD analyzed already? */
1365 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1366 qc = __ata_qc_from_tag(ap, tag);
1368 if (!(qc->flags & ATA_QCFLAG_FAILED))
1375 /* okay, this error is ours */
1376 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1378 ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
1379 "(errno=%d)\n", rc);
1383 if (!(link->sactive & (1 << tag))) {
1384 ata_link_printk(link, KERN_ERR, "log page 10h reported "
1385 "inactive tag %d\n", tag);
1389 /* we've got the perpetrator, condemn it */
1390 qc = __ata_qc_from_tag(ap, tag);
1391 memcpy(&qc->result_tf, &tf, sizeof(tf));
1392 qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
1393 ehc->i.err_mask &= ~AC_ERR_DEV;
1397 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1398 * @qc: qc to analyze
1399 * @tf: Taskfile registers to analyze
1401 * Analyze taskfile of @qc and further determine cause of
1402 * failure. This function also requests ATAPI sense data if
1406 * Kernel thread context (may sleep).
1409 * Determined recovery action
1411 static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1412 const struct ata_taskfile *tf)
1414 unsigned int tmp, action = 0;
1415 u8 stat = tf->command, err = tf->feature;
1417 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1418 qc->err_mask |= AC_ERR_HSM;
1419 return ATA_EH_SOFTRESET;
1422 if (stat & (ATA_ERR | ATA_DF))
1423 qc->err_mask |= AC_ERR_DEV;
1427 switch (qc->dev->class) {
1430 qc->err_mask |= AC_ERR_ATA_BUS;
1432 qc->err_mask |= AC_ERR_MEDIA;
1434 qc->err_mask |= AC_ERR_INVALID;
1438 if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
1439 tmp = atapi_eh_request_sense(qc);
1441 /* ATA_QCFLAG_SENSE_VALID is used to
1442 * tell atapi_qc_complete() that sense
1443 * data is already valid.
1445 * TODO: interpret sense data and set
1446 * appropriate err_mask.
1448 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1450 qc->err_mask |= tmp;
1454 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1455 action |= ATA_EH_SOFTRESET;
1460 static int ata_eh_categorize_error(int is_io, unsigned int err_mask)
1462 if (err_mask & AC_ERR_ATA_BUS)
1465 if (err_mask & AC_ERR_TIMEOUT)
1469 if (err_mask & AC_ERR_HSM)
1472 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1479 struct speed_down_verdict_arg {
1484 static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
1486 struct speed_down_verdict_arg *arg = void_arg;
1487 int cat = ata_eh_categorize_error(ent->is_io, ent->err_mask);
1489 if (ent->timestamp < arg->since)
1492 arg->nr_errors[cat]++;
1497 * ata_eh_speed_down_verdict - Determine speed down verdict
1498 * @dev: Device of interest
1500 * This function examines error ring of @dev and determines
1501 * whether NCQ needs to be turned off, transfer speed should be
1502 * stepped down, or falling back to PIO is necessary.
1504 * Cat-1 is ATA_BUS error for any command.
1506 * Cat-2 is TIMEOUT for any command or HSM violation for known
1507 * supported commands.
1509 * Cat-3 is is unclassified DEV error for known supported
1512 * NCQ needs to be turned off if there have been more than 3
1513 * Cat-2 + Cat-3 errors during last 10 minutes.
1515 * Speed down is necessary if there have been more than 3 Cat-1 +
1516 * Cat-2 errors or 10 Cat-3 errors during last 10 minutes.
1518 * Falling back to PIO mode is necessary if there have been more
1519 * than 10 Cat-1 + Cat-2 + Cat-3 errors during last 5 minutes.
1522 * Inherited from caller.
1525 * OR of ATA_EH_SPDN_* flags.
1527 static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
1529 const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
1530 u64 j64 = get_jiffies_64();
1531 struct speed_down_verdict_arg arg;
1532 unsigned int verdict = 0;
1534 /* scan past 10 mins of error history */
1535 memset(&arg, 0, sizeof(arg));
1536 arg.since = j64 - min(j64, j10mins);
1537 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1539 if (arg.nr_errors[2] + arg.nr_errors[3] > 3)
1540 verdict |= ATA_EH_SPDN_NCQ_OFF;
1541 if (arg.nr_errors[1] + arg.nr_errors[2] > 3 || arg.nr_errors[3] > 10)
1542 verdict |= ATA_EH_SPDN_SPEED_DOWN;
1544 /* scan past 3 mins of error history */
1545 memset(&arg, 0, sizeof(arg));
1546 arg.since = j64 - min(j64, j5mins);
1547 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1549 if (arg.nr_errors[1] + arg.nr_errors[2] + arg.nr_errors[3] > 10)
1550 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
1556 * ata_eh_speed_down - record error and speed down if necessary
1557 * @dev: Failed device
1558 * @is_io: Did the device fail during normal IO?
1559 * @err_mask: err_mask of the error
1561 * Record error and examine error history to determine whether
1562 * adjusting transmission speed is necessary. It also sets
1563 * transmission limits appropriately if such adjustment is
1567 * Kernel thread context (may sleep).
1570 * Determined recovery action.
1572 static unsigned int ata_eh_speed_down(struct ata_device *dev, int is_io,
1573 unsigned int err_mask)
1575 unsigned int verdict;
1576 unsigned int action = 0;
1578 /* don't bother if Cat-0 error */
1579 if (ata_eh_categorize_error(is_io, err_mask) == 0)
1582 /* record error and determine whether speed down is necessary */
1583 ata_ering_record(&dev->ering, is_io, err_mask);
1584 verdict = ata_eh_speed_down_verdict(dev);
1587 if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
1588 (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
1589 ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
1590 dev->flags |= ATA_DFLAG_NCQ_OFF;
1591 ata_dev_printk(dev, KERN_WARNING,
1592 "NCQ disabled due to excessive errors\n");
1597 if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1598 /* speed down SATA link speed if possible */
1599 if (sata_down_spd_limit(dev->link) == 0) {
1600 action |= ATA_EH_HARDRESET;
1604 /* lower transfer mode */
1605 if (dev->spdn_cnt < 2) {
1606 static const int dma_dnxfer_sel[] =
1607 { ATA_DNXFER_DMA, ATA_DNXFER_40C };
1608 static const int pio_dnxfer_sel[] =
1609 { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
1612 if (dev->xfer_shift != ATA_SHIFT_PIO)
1613 sel = dma_dnxfer_sel[dev->spdn_cnt];
1615 sel = pio_dnxfer_sel[dev->spdn_cnt];
1619 if (ata_down_xfermask_limit(dev, sel) == 0) {
1620 action |= ATA_EH_SOFTRESET;
1626 /* Fall back to PIO? Slowing down to PIO is meaningless for
1627 * SATA. Consider it only for PATA.
1629 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
1630 (dev->link->ap->cbl != ATA_CBL_SATA) &&
1631 (dev->xfer_shift != ATA_SHIFT_PIO)) {
1632 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
1634 action |= ATA_EH_SOFTRESET;
1641 /* device has been slowed down, blow error history */
1642 ata_ering_clear(&dev->ering);
1647 * ata_eh_link_autopsy - analyze error and determine recovery action
1648 * @link: host link to perform autopsy on
1650 * Analyze why @link failed and determine which recovery actions
1651 * are needed. This function also sets more detailed AC_ERR_*
1652 * values and fills sense data for ATAPI CHECK SENSE.
1655 * Kernel thread context (may sleep).
1657 static void ata_eh_link_autopsy(struct ata_link *link)
1659 struct ata_port *ap = link->ap;
1660 struct ata_eh_context *ehc = &link->eh_context;
1661 unsigned int all_err_mask = 0;
1668 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1671 /* obtain and analyze SError */
1672 rc = sata_scr_read(link, SCR_ERROR, &serror);
1674 ehc->i.serror |= serror;
1675 ata_eh_analyze_serror(link);
1676 } else if (rc != -EOPNOTSUPP) {
1677 /* SError read failed, force hardreset and probing */
1678 ata_ehi_schedule_probe(&ehc->i);
1679 ehc->i.action |= ATA_EH_HARDRESET;
1680 ehc->i.err_mask |= AC_ERR_OTHER;
1683 /* analyze NCQ failure */
1684 ata_eh_analyze_ncq_error(link);
1686 /* any real error trumps AC_ERR_OTHER */
1687 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1688 ehc->i.err_mask &= ~AC_ERR_OTHER;
1690 all_err_mask |= ehc->i.err_mask;
1692 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1693 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1695 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
1698 /* inherit upper level err_mask */
1699 qc->err_mask |= ehc->i.err_mask;
1702 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1704 /* DEV errors are probably spurious in case of ATA_BUS error */
1705 if (qc->err_mask & AC_ERR_ATA_BUS)
1706 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1709 /* any real error trumps unknown error */
1710 if (qc->err_mask & ~AC_ERR_OTHER)
1711 qc->err_mask &= ~AC_ERR_OTHER;
1713 /* SENSE_VALID trumps dev/unknown error and revalidation */
1714 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1715 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1716 ehc->i.action &= ~ATA_EH_REVALIDATE;
1719 /* accumulate error info */
1720 ehc->i.dev = qc->dev;
1721 all_err_mask |= qc->err_mask;
1722 if (qc->flags & ATA_QCFLAG_IO)
1726 /* enforce default EH actions */
1727 if (ap->pflags & ATA_PFLAG_FROZEN ||
1728 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
1729 ehc->i.action |= ATA_EH_SOFTRESET;
1730 else if (all_err_mask)
1731 ehc->i.action |= ATA_EH_REVALIDATE;
1733 /* if we have offending qcs and the associated failed device */
1736 ehc->i.action |= ata_eh_speed_down(ehc->i.dev, is_io,
1739 /* perform per-dev EH action only on the offending device */
1740 ehc->i.dev_action[ehc->i.dev->devno] |=
1741 ehc->i.action & ATA_EH_PERDEV_MASK;
1742 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
1749 * ata_eh_autopsy - analyze error and determine recovery action
1750 * @ap: host port to perform autopsy on
1752 * Analyze all links of @ap and determine why they failed and
1753 * which recovery actions are needed.
1756 * Kernel thread context (may sleep).
1758 void ata_eh_autopsy(struct ata_port *ap)
1760 struct ata_link *link;
1762 __ata_port_for_each_link(link, ap)
1763 ata_eh_link_autopsy(link);
1767 * ata_eh_link_report - report error handling to user
1768 * @link: ATA link EH is going on
1770 * Report EH to user.
1775 static void ata_eh_link_report(struct ata_link *link)
1777 struct ata_port *ap = link->ap;
1778 struct ata_eh_context *ehc = &link->eh_context;
1779 const char *frozen, *desc;
1781 int tag, nr_failed = 0;
1784 if (ehc->i.desc[0] != '\0')
1787 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1788 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1790 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
1792 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1798 if (!nr_failed && !ehc->i.err_mask)
1802 if (ap->pflags & ATA_PFLAG_FROZEN)
1805 memset(tries_buf, 0, sizeof(tries_buf));
1806 if (ap->eh_tries < ATA_EH_MAX_TRIES)
1807 snprintf(tries_buf, sizeof(tries_buf) - 1, " t%d",
1811 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1812 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1813 ehc->i.err_mask, link->sactive, ehc->i.serror,
1814 ehc->i.action, frozen, tries_buf);
1816 ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
1818 ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
1819 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1820 ehc->i.err_mask, link->sactive, ehc->i.serror,
1821 ehc->i.action, frozen, tries_buf);
1823 ata_link_printk(link, KERN_ERR, "%s\n", desc);
1826 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1827 static const char *dma_str[] = {
1828 [DMA_BIDIRECTIONAL] = "bidi",
1829 [DMA_TO_DEVICE] = "out",
1830 [DMA_FROM_DEVICE] = "in",
1833 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1834 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
1836 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
1837 qc->dev->link != link || !qc->err_mask)
1840 ata_dev_printk(qc->dev, KERN_ERR,
1841 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
1842 "tag %d cdb 0x%x data %u %s\n "
1843 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
1844 "Emask 0x%x (%s)%s\n",
1845 cmd->command, cmd->feature, cmd->nsect,
1846 cmd->lbal, cmd->lbam, cmd->lbah,
1847 cmd->hob_feature, cmd->hob_nsect,
1848 cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
1849 cmd->device, qc->tag, qc->cdb[0], qc->nbytes,
1850 dma_str[qc->dma_dir],
1851 res->command, res->feature, res->nsect,
1852 res->lbal, res->lbam, res->lbah,
1853 res->hob_feature, res->hob_nsect,
1854 res->hob_lbal, res->hob_lbam, res->hob_lbah,
1855 res->device, qc->err_mask, ata_err_string(qc->err_mask),
1856 qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
1861 * ata_eh_report - report error handling to user
1862 * @ap: ATA port to report EH about
1864 * Report EH to user.
1869 void ata_eh_report(struct ata_port *ap)
1871 struct ata_link *link;
1873 __ata_port_for_each_link(link, ap)
1874 ata_eh_link_report(link);
1877 static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
1878 unsigned int *classes, unsigned long deadline)
1880 struct ata_device *dev;
1883 ata_link_for_each_dev(dev, link)
1884 classes[dev->devno] = ATA_DEV_UNKNOWN;
1886 rc = reset(link, classes, deadline);
1890 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
1891 * is complete and convert all ATA_DEV_UNKNOWN to
1894 ata_link_for_each_dev(dev, link)
1895 if (classes[dev->devno] != ATA_DEV_UNKNOWN)
1899 ata_link_for_each_dev(dev, link) {
1900 if (classes[dev->devno] == ATA_DEV_UNKNOWN)
1901 classes[dev->devno] = ATA_DEV_NONE;
1908 static int ata_eh_followup_srst_needed(int rc, int classify,
1909 const unsigned int *classes)
1915 if (classify && classes[0] == ATA_DEV_UNKNOWN)
1920 int ata_eh_reset(struct ata_link *link, int classify,
1921 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
1922 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
1924 struct ata_eh_context *ehc = &link->eh_context;
1925 unsigned int *classes = ehc->classes;
1926 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
1928 struct ata_device *dev;
1929 unsigned long deadline;
1930 unsigned int action;
1931 ata_reset_fn_t reset;
1934 /* about to reset */
1935 ata_eh_about_to_do(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
1937 /* Determine which reset to use and record in ehc->i.action.
1938 * prereset() may examine and modify it.
1940 action = ehc->i.action;
1941 ehc->i.action &= ~ATA_EH_RESET_MASK;
1942 if (softreset && (!hardreset || (!sata_set_spd_needed(link) &&
1943 !(action & ATA_EH_HARDRESET))))
1944 ehc->i.action |= ATA_EH_SOFTRESET;
1946 ehc->i.action |= ATA_EH_HARDRESET;
1949 rc = prereset(link, jiffies + ATA_EH_PRERESET_TIMEOUT);
1951 if (rc == -ENOENT) {
1952 ata_link_printk(link, KERN_DEBUG,
1953 "port disabled. ignoring.\n");
1954 ehc->i.action &= ~ATA_EH_RESET_MASK;
1956 ata_link_for_each_dev(dev, link)
1957 classes[dev->devno] = ATA_DEV_NONE;
1961 ata_link_printk(link, KERN_ERR,
1962 "prereset failed (errno=%d)\n", rc);
1967 /* prereset() might have modified ehc->i.action */
1968 if (ehc->i.action & ATA_EH_HARDRESET)
1970 else if (ehc->i.action & ATA_EH_SOFTRESET)
1973 /* prereset told us not to reset, bang classes and return */
1974 ata_link_for_each_dev(dev, link)
1975 classes[dev->devno] = ATA_DEV_NONE;
1980 /* did prereset() screw up? if so, fix up to avoid oopsing */
1989 deadline = jiffies + ata_eh_reset_timeouts[try++];
1991 /* shut up during boot probing */
1993 ata_link_printk(link, KERN_INFO, "%s resetting link\n",
1994 reset == softreset ? "soft" : "hard");
1996 /* mark that this EH session started with reset */
1997 if (reset == hardreset)
1998 ehc->i.flags |= ATA_EHI_DID_HARDRESET;
2000 ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
2002 rc = ata_do_reset(link, reset, classes, deadline);
2004 if (reset == hardreset &&
2005 ata_eh_followup_srst_needed(rc, classify, classes)) {
2006 /* okay, let's do follow-up softreset */
2010 ata_link_printk(link, KERN_ERR,
2011 "follow-up softreset required "
2012 "but no softreset avaliable\n");
2017 ata_eh_about_to_do(link, NULL, ATA_EH_RESET_MASK);
2018 rc = ata_do_reset(link, reset, classes, deadline);
2020 if (rc == 0 && classify &&
2021 classes[0] == ATA_DEV_UNKNOWN) {
2022 ata_link_printk(link, KERN_ERR,
2023 "classification failed\n");
2029 if (rc && try < ARRAY_SIZE(ata_eh_reset_timeouts)) {
2030 unsigned long now = jiffies;
2032 if (time_before(now, deadline)) {
2033 unsigned long delta = deadline - jiffies;
2035 ata_link_printk(link, KERN_WARNING, "reset failed "
2036 "(errno=%d), retrying in %u secs\n",
2037 rc, (jiffies_to_msecs(delta) + 999) / 1000);
2039 schedule_timeout_uninterruptible(delta);
2043 try == ARRAY_SIZE(ata_eh_reset_timeouts) - 1)
2044 sata_down_spd_limit(link);
2053 /* After the reset, the device state is PIO 0 and the
2054 * controller state is undefined. Record the mode.
2056 ata_link_for_each_dev(dev, link)
2057 dev->pio_mode = XFER_PIO_0;
2059 /* record current link speed */
2060 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2061 link->sata_spd = (sstatus >> 4) & 0xf;
2064 postreset(link, classes);
2066 /* reset successful, schedule revalidation */
2067 ata_eh_done(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
2068 ehc->i.action |= ATA_EH_REVALIDATE;
2071 /* clear hotplug flag */
2072 ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2076 static int ata_eh_revalidate_and_attach(struct ata_link *link,
2077 struct ata_device **r_failed_dev)
2079 struct ata_port *ap = link->ap;
2080 struct ata_eh_context *ehc = &link->eh_context;
2081 struct ata_device *dev;
2082 unsigned int new_mask = 0;
2083 unsigned long flags;
2088 /* For PATA drive side cable detection to work, IDENTIFY must
2089 * be done backwards such that PDIAG- is released by the slave
2090 * device before the master device is identified.
2092 ata_link_for_each_dev_reverse(dev, link) {
2093 unsigned int action = ata_eh_dev_action(dev);
2094 unsigned int readid_flags = 0;
2096 if (ehc->i.flags & ATA_EHI_DID_RESET)
2097 readid_flags |= ATA_READID_POSTRESET;
2099 if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
2100 if (ata_link_offline(link)) {
2105 ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
2106 rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
2111 ata_eh_done(link, dev, ATA_EH_REVALIDATE);
2113 /* Configuration may have changed, reconfigure
2116 ehc->i.flags |= ATA_EHI_SETMODE;
2118 /* schedule the scsi_rescan_device() here */
2119 queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
2120 } else if (dev->class == ATA_DEV_UNKNOWN &&
2121 ehc->tries[dev->devno] &&
2122 ata_class_enabled(ehc->classes[dev->devno])) {
2123 dev->class = ehc->classes[dev->devno];
2125 rc = ata_dev_read_id(dev, &dev->class, readid_flags,
2129 new_mask |= 1 << dev->devno;
2132 /* IDENTIFY was issued to non-existent
2133 * device. No need to reset. Just
2134 * thaw and kill the device.
2136 ata_eh_thaw_port(ap);
2137 dev->class = ATA_DEV_UNKNOWN;
2140 dev->class = ATA_DEV_UNKNOWN;
2146 /* PDIAG- should have been released, ask cable type if post-reset */
2147 if (ata_is_host_link(link) && ap->ops->cable_detect &&
2148 (ehc->i.flags & ATA_EHI_DID_RESET))
2149 ap->cbl = ap->ops->cable_detect(ap);
2151 /* Configure new devices forward such that user doesn't see
2152 * device detection messages backwards.
2154 ata_link_for_each_dev(dev, link) {
2155 if (!(new_mask & (1 << dev->devno)))
2158 ehc->i.flags |= ATA_EHI_PRINTINFO;
2159 rc = ata_dev_configure(dev);
2160 ehc->i.flags &= ~ATA_EHI_PRINTINFO;
2164 spin_lock_irqsave(ap->lock, flags);
2165 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
2166 spin_unlock_irqrestore(ap->lock, flags);
2168 /* new device discovered, configure xfermode */
2169 ehc->i.flags |= ATA_EHI_SETMODE;
2175 *r_failed_dev = dev;
2176 DPRINTK("EXIT rc=%d\n", rc);
2180 static int ata_link_nr_enabled(struct ata_link *link)
2182 struct ata_device *dev;
2185 ata_link_for_each_dev(dev, link)
2186 if (ata_dev_enabled(dev))
2191 static int ata_link_nr_vacant(struct ata_link *link)
2193 struct ata_device *dev;
2196 ata_link_for_each_dev(dev, link)
2197 if (dev->class == ATA_DEV_UNKNOWN)
2202 static int ata_eh_skip_recovery(struct ata_link *link)
2204 struct ata_eh_context *ehc = &link->eh_context;
2205 struct ata_device *dev;
2207 /* thaw frozen port, resume link and recover failed devices */
2208 if ((link->ap->pflags & ATA_PFLAG_FROZEN) ||
2209 (ehc->i.flags & ATA_EHI_RESUME_LINK) || ata_link_nr_enabled(link))
2212 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
2213 ata_link_for_each_dev(dev, link) {
2214 if (dev->class == ATA_DEV_UNKNOWN &&
2215 ehc->classes[dev->devno] != ATA_DEV_NONE)
2222 static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
2224 struct ata_eh_context *ehc = &dev->link->eh_context;
2226 ehc->tries[dev->devno]--;
2230 /* device missing or wrong IDENTIFY data, schedule probing */
2231 ehc->i.probe_mask |= (1 << dev->devno);
2233 /* give it just one more chance */
2234 ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
2236 if (ehc->tries[dev->devno] == 1) {
2237 /* This is the last chance, better to slow
2238 * down than lose it.
2240 sata_down_spd_limit(dev->link);
2241 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2245 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2246 /* disable device if it has used up all its chances */
2247 ata_dev_disable(dev);
2249 /* detach if offline */
2250 if (ata_link_offline(dev->link))
2251 ata_eh_detach_dev(dev);
2253 /* probe if requested */
2254 if ((ehc->i.probe_mask & (1 << dev->devno)) &&
2255 !(ehc->did_probe_mask & (1 << dev->devno))) {
2256 ata_eh_detach_dev(dev);
2259 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2260 ehc->did_probe_mask |= (1 << dev->devno);
2261 ehc->i.action |= ATA_EH_SOFTRESET;
2266 /* soft didn't work? be haaaaard */
2267 if (ehc->i.flags & ATA_EHI_DID_RESET)
2268 ehc->i.action |= ATA_EH_HARDRESET;
2270 ehc->i.action |= ATA_EH_SOFTRESET;
2277 * ata_eh_recover - recover host port after error
2278 * @ap: host port to recover
2279 * @prereset: prereset method (can be NULL)
2280 * @softreset: softreset method (can be NULL)
2281 * @hardreset: hardreset method (can be NULL)
2282 * @postreset: postreset method (can be NULL)
2283 * @r_failed_link: out parameter for failed link
2285 * This is the alpha and omega, eum and yang, heart and soul of
2286 * libata exception handling. On entry, actions required to
2287 * recover each link and hotplug requests are recorded in the
2288 * link's eh_context. This function executes all the operations
2289 * with appropriate retrials and fallbacks to resurrect failed
2290 * devices, detach goners and greet newcomers.
2293 * Kernel thread context (may sleep).
2296 * 0 on success, -errno on failure.
2298 int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
2299 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2300 ata_postreset_fn_t postreset,
2301 struct ata_link **r_failed_link)
2303 struct ata_link *link;
2304 struct ata_device *dev;
2305 int nr_failed_devs, nr_disabled_devs;
2310 /* prep for recovery */
2311 ata_port_for_each_link(link, ap) {
2312 struct ata_eh_context *ehc = &link->eh_context;
2314 ata_link_for_each_dev(dev, link) {
2315 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2317 /* collect port action mask recorded in dev actions */
2318 ehc->i.action |= ehc->i.dev_action[dev->devno] &
2319 ~ATA_EH_PERDEV_MASK;
2320 ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
2322 /* process hotplug request */
2323 if (dev->flags & ATA_DFLAG_DETACH)
2324 ata_eh_detach_dev(dev);
2326 if (!ata_dev_enabled(dev) &&
2327 ((ehc->i.probe_mask & (1 << dev->devno)) &&
2328 !(ehc->did_probe_mask & (1 << dev->devno)))) {
2329 ata_eh_detach_dev(dev);
2331 ehc->did_probe_mask |= (1 << dev->devno);
2332 ehc->i.action |= ATA_EH_SOFTRESET;
2340 nr_disabled_devs = 0;
2343 /* if UNLOADING, finish immediately */
2344 if (ap->pflags & ATA_PFLAG_UNLOADING)
2348 ata_port_for_each_link(link, ap) {
2349 struct ata_eh_context *ehc = &link->eh_context;
2351 /* skip EH if possible. */
2352 if (ata_eh_skip_recovery(link))
2355 /* do we need to reset? */
2356 if (ehc->i.action & ATA_EH_RESET_MASK)
2359 ata_link_for_each_dev(dev, link)
2360 ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
2365 ata_eh_freeze_port(ap);
2367 ata_port_for_each_link(link, ap) {
2368 struct ata_eh_context *ehc = &link->eh_context;
2370 if (!(ehc->i.action & ATA_EH_RESET_MASK))
2373 rc = ata_eh_reset(link, ata_link_nr_vacant(link),
2374 prereset, softreset, hardreset,
2377 ata_link_printk(link, KERN_ERR,
2378 "reset failed, giving up\n");
2383 ata_eh_thaw_port(ap);
2387 ata_port_for_each_link(link, ap) {
2388 struct ata_eh_context *ehc = &link->eh_context;
2390 /* revalidate existing devices and attach new ones */
2391 rc = ata_eh_revalidate_and_attach(link, &dev);
2395 /* configure transfer mode if necessary */
2396 if (ehc->i.flags & ATA_EHI_SETMODE) {
2397 rc = ata_set_mode(link, &dev);
2400 ehc->i.flags &= ~ATA_EHI_SETMODE;
2403 /* this link is okay now */
2409 if (ata_eh_handle_dev_fail(dev, rc))
2412 if (ap->pflags & ATA_PFLAG_FROZEN)
2416 if (nr_failed_devs) {
2417 if (nr_failed_devs != nr_disabled_devs) {
2418 ata_port_printk(ap, KERN_WARNING, "failed to recover "
2419 "some devices, retrying in 5 secs\n");
2422 /* no device left to recover, repeat fast */
2430 if (rc && r_failed_link)
2431 *r_failed_link = link;
2433 DPRINTK("EXIT, rc=%d\n", rc);
2438 * ata_eh_finish - finish up EH
2439 * @ap: host port to finish EH for
2441 * Recovery is complete. Clean up EH states and retry or finish
2447 void ata_eh_finish(struct ata_port *ap)
2451 /* retry or finish qcs */
2452 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2453 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2455 if (!(qc->flags & ATA_QCFLAG_FAILED))
2459 /* FIXME: Once EH migration is complete,
2460 * generate sense data in this function,
2461 * considering both err_mask and tf.
2463 if (qc->err_mask & AC_ERR_INVALID)
2464 ata_eh_qc_complete(qc);
2466 ata_eh_qc_retry(qc);
2468 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2469 ata_eh_qc_complete(qc);
2471 /* feed zero TF to sense generation */
2472 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2473 ata_eh_qc_retry(qc);
2480 * ata_do_eh - do standard error handling
2481 * @ap: host port to handle error for
2482 * @prereset: prereset method (can be NULL)
2483 * @softreset: softreset method (can be NULL)
2484 * @hardreset: hardreset method (can be NULL)
2485 * @postreset: postreset method (can be NULL)
2487 * Perform standard error handling sequence.
2490 * Kernel thread context (may sleep).
2492 void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2493 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2494 ata_postreset_fn_t postreset)
2496 struct ata_device *dev;
2502 rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
2505 ata_link_for_each_dev(dev, &ap->link)
2506 ata_dev_disable(dev);
2514 * ata_eh_handle_port_suspend - perform port suspend operation
2515 * @ap: port to suspend
2520 * Kernel thread context (may sleep).
2522 static void ata_eh_handle_port_suspend(struct ata_port *ap)
2524 unsigned long flags;
2527 /* are we suspending? */
2528 spin_lock_irqsave(ap->lock, flags);
2529 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2530 ap->pm_mesg.event == PM_EVENT_ON) {
2531 spin_unlock_irqrestore(ap->lock, flags);
2534 spin_unlock_irqrestore(ap->lock, flags);
2536 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
2538 /* tell ACPI we're suspending */
2539 rc = ata_acpi_on_suspend(ap);
2544 ata_eh_freeze_port(ap);
2546 if (ap->ops->port_suspend)
2547 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
2551 spin_lock_irqsave(ap->lock, flags);
2553 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
2555 ap->pflags |= ATA_PFLAG_SUSPENDED;
2556 else if (ap->pflags & ATA_PFLAG_FROZEN)
2557 ata_port_schedule_eh(ap);
2559 if (ap->pm_result) {
2560 *ap->pm_result = rc;
2561 ap->pm_result = NULL;
2564 spin_unlock_irqrestore(ap->lock, flags);
2570 * ata_eh_handle_port_resume - perform port resume operation
2571 * @ap: port to resume
2576 * Kernel thread context (may sleep).
2578 static void ata_eh_handle_port_resume(struct ata_port *ap)
2580 unsigned long flags;
2583 /* are we resuming? */
2584 spin_lock_irqsave(ap->lock, flags);
2585 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2586 ap->pm_mesg.event != PM_EVENT_ON) {
2587 spin_unlock_irqrestore(ap->lock, flags);
2590 spin_unlock_irqrestore(ap->lock, flags);
2592 WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
2594 if (ap->ops->port_resume)
2595 rc = ap->ops->port_resume(ap);
2597 /* tell ACPI that we're resuming */
2598 ata_acpi_on_resume(ap);
2601 spin_lock_irqsave(ap->lock, flags);
2602 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
2603 if (ap->pm_result) {
2604 *ap->pm_result = rc;
2605 ap->pm_result = NULL;
2607 spin_unlock_irqrestore(ap->lock, flags);
2609 #endif /* CONFIG_PM */