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 <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>
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),
56 ATA_EFLAG_IS_IO
= (1 << 0),
57 ATA_EFLAG_DUBIOUS_XFER
= (1 << 1),
59 /* error categories */
62 ATA_ECAT_TOUT_HSM
= 2,
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,
70 ATA_EH_CMD_DFL_TIMEOUT
= 5000,
72 /* always put at least this amount of time between resets */
73 ATA_EH_RESET_COOL_DOWN
= 5000,
75 /* Waiting in ->prereset can never be reliable. It's
76 * sometimes nice to wait there but it can't be depended upon;
77 * otherwise, we wouldn't be resetting. Just give it enough
78 * time for most drives to spin up.
80 ATA_EH_PRERESET_TIMEOUT
= 10000,
81 ATA_EH_FASTDRAIN_INTERVAL
= 3000,
84 /* The following table determines how we sequence resets. Each entry
85 * represents timeout for that try. The first try can be soft or
86 * hardreset. All others are hardreset if available. In most cases
87 * the first reset w/ 10sec timeout should succeed. Following entries
88 * are mostly for error handling, hotplug and retarded devices.
90 static const unsigned long ata_eh_reset_timeouts
[] = {
91 10000, /* most drives spin up by 10sec */
92 10000, /* > 99% working drives spin up before 20sec */
93 35000, /* give > 30 secs of idleness for retarded devices */
94 5000, /* and sweet one last chance */
95 ULONG_MAX
, /* > 1 min has elapsed, give up */
98 static const unsigned long ata_eh_identify_timeouts
[] = {
99 5000, /* covers > 99% of successes and not too boring on failures */
100 10000, /* combined time till here is enough even for media access */
101 30000, /* for true idiots */
105 static const unsigned long ata_eh_other_timeouts
[] = {
106 5000, /* same rationale as identify timeout */
108 /* but no merciful 30sec for other commands, it just isn't worth it */
112 struct ata_eh_cmd_timeout_ent
{
114 const unsigned long *timeouts
;
117 /* The following table determines timeouts to use for EH internal
118 * commands. Each table entry is a command class and matches the
119 * commands the entry applies to and the timeout table to use.
121 * On the retry after a command timed out, the next timeout value from
122 * the table is used. If the table doesn't contain further entries,
123 * the last value is used.
125 * ehc->cmd_timeout_idx keeps track of which timeout to use per
126 * command class, so if SET_FEATURES times out on the first try, the
127 * next try will use the second timeout value only for that class.
129 #define CMDS(cmds...) (const u8 []){ cmds, 0 }
130 static const struct ata_eh_cmd_timeout_ent
131 ata_eh_cmd_timeout_table
[ATA_EH_CMD_TIMEOUT_TABLE_SIZE
] = {
132 { .commands
= CMDS(ATA_CMD_ID_ATA
, ATA_CMD_ID_ATAPI
),
133 .timeouts
= ata_eh_identify_timeouts
, },
134 { .commands
= CMDS(ATA_CMD_READ_NATIVE_MAX
, ATA_CMD_READ_NATIVE_MAX_EXT
),
135 .timeouts
= ata_eh_other_timeouts
, },
136 { .commands
= CMDS(ATA_CMD_SET_MAX
, ATA_CMD_SET_MAX_EXT
),
137 .timeouts
= ata_eh_other_timeouts
, },
138 { .commands
= CMDS(ATA_CMD_SET_FEATURES
),
139 .timeouts
= ata_eh_other_timeouts
, },
140 { .commands
= CMDS(ATA_CMD_INIT_DEV_PARAMS
),
141 .timeouts
= ata_eh_other_timeouts
, },
145 static void __ata_port_freeze(struct ata_port
*ap
);
147 static void ata_eh_handle_port_suspend(struct ata_port
*ap
);
148 static void ata_eh_handle_port_resume(struct ata_port
*ap
);
149 #else /* CONFIG_PM */
150 static void ata_eh_handle_port_suspend(struct ata_port
*ap
)
153 static void ata_eh_handle_port_resume(struct ata_port
*ap
)
155 #endif /* CONFIG_PM */
157 static void __ata_ehi_pushv_desc(struct ata_eh_info
*ehi
, const char *fmt
,
160 ehi
->desc_len
+= vscnprintf(ehi
->desc
+ ehi
->desc_len
,
161 ATA_EH_DESC_LEN
- ehi
->desc_len
,
166 * __ata_ehi_push_desc - push error description without adding separator
168 * @fmt: printf format string
170 * Format string according to @fmt and append it to @ehi->desc.
173 * spin_lock_irqsave(host lock)
175 void __ata_ehi_push_desc(struct ata_eh_info
*ehi
, const char *fmt
, ...)
180 __ata_ehi_pushv_desc(ehi
, fmt
, args
);
185 * ata_ehi_push_desc - push error description with separator
187 * @fmt: printf format string
189 * Format string according to @fmt and append it to @ehi->desc.
190 * If @ehi->desc is not empty, ", " is added in-between.
193 * spin_lock_irqsave(host lock)
195 void ata_ehi_push_desc(struct ata_eh_info
*ehi
, const char *fmt
, ...)
200 __ata_ehi_push_desc(ehi
, ", ");
203 __ata_ehi_pushv_desc(ehi
, fmt
, args
);
208 * ata_ehi_clear_desc - clean error description
214 * spin_lock_irqsave(host lock)
216 void ata_ehi_clear_desc(struct ata_eh_info
*ehi
)
223 * ata_port_desc - append port description
224 * @ap: target ATA port
225 * @fmt: printf format string
227 * Format string according to @fmt and append it to port
228 * description. If port description is not empty, " " is added
229 * in-between. This function is to be used while initializing
230 * ata_host. The description is printed on host registration.
235 void ata_port_desc(struct ata_port
*ap
, const char *fmt
, ...)
239 WARN_ON(!(ap
->pflags
& ATA_PFLAG_INITIALIZING
));
241 if (ap
->link
.eh_info
.desc_len
)
242 __ata_ehi_push_desc(&ap
->link
.eh_info
, " ");
245 __ata_ehi_pushv_desc(&ap
->link
.eh_info
, fmt
, args
);
252 * ata_port_pbar_desc - append PCI BAR description
253 * @ap: target ATA port
254 * @bar: target PCI BAR
255 * @offset: offset into PCI BAR
256 * @name: name of the area
258 * If @offset is negative, this function formats a string which
259 * contains the name, address, size and type of the BAR and
260 * appends it to the port description. If @offset is zero or
261 * positive, only name and offsetted address is appended.
266 void ata_port_pbar_desc(struct ata_port
*ap
, int bar
, ssize_t offset
,
269 struct pci_dev
*pdev
= to_pci_dev(ap
->host
->dev
);
271 unsigned long long start
, len
;
273 if (pci_resource_flags(pdev
, bar
) & IORESOURCE_MEM
)
275 else if (pci_resource_flags(pdev
, bar
) & IORESOURCE_IO
)
278 start
= (unsigned long long)pci_resource_start(pdev
, bar
);
279 len
= (unsigned long long)pci_resource_len(pdev
, bar
);
282 ata_port_desc(ap
, "%s %s%llu@0x%llx", name
, type
, len
, start
);
284 ata_port_desc(ap
, "%s 0x%llx", name
,
285 start
+ (unsigned long long)offset
);
288 #endif /* CONFIG_PCI */
290 static int ata_lookup_timeout_table(u8 cmd
)
294 for (i
= 0; i
< ATA_EH_CMD_TIMEOUT_TABLE_SIZE
; i
++) {
297 for (cur
= ata_eh_cmd_timeout_table
[i
].commands
; *cur
; cur
++)
306 * ata_internal_cmd_timeout - determine timeout for an internal command
307 * @dev: target device
308 * @cmd: internal command to be issued
310 * Determine timeout for internal command @cmd for @dev.
316 * Determined timeout.
318 unsigned long ata_internal_cmd_timeout(struct ata_device
*dev
, u8 cmd
)
320 struct ata_eh_context
*ehc
= &dev
->link
->eh_context
;
321 int ent
= ata_lookup_timeout_table(cmd
);
325 return ATA_EH_CMD_DFL_TIMEOUT
;
327 idx
= ehc
->cmd_timeout_idx
[dev
->devno
][ent
];
328 return ata_eh_cmd_timeout_table
[ent
].timeouts
[idx
];
332 * ata_internal_cmd_timed_out - notification for internal command timeout
333 * @dev: target device
334 * @cmd: internal command which timed out
336 * Notify EH that internal command @cmd for @dev timed out. This
337 * function should be called only for commands whose timeouts are
338 * determined using ata_internal_cmd_timeout().
343 void ata_internal_cmd_timed_out(struct ata_device
*dev
, u8 cmd
)
345 struct ata_eh_context
*ehc
= &dev
->link
->eh_context
;
346 int ent
= ata_lookup_timeout_table(cmd
);
352 idx
= ehc
->cmd_timeout_idx
[dev
->devno
][ent
];
353 if (ata_eh_cmd_timeout_table
[ent
].timeouts
[idx
+ 1] != ULONG_MAX
)
354 ehc
->cmd_timeout_idx
[dev
->devno
][ent
]++;
357 static void ata_ering_record(struct ata_ering
*ering
, unsigned int eflags
,
358 unsigned int err_mask
)
360 struct ata_ering_entry
*ent
;
365 ering
->cursor
%= ATA_ERING_SIZE
;
367 ent
= &ering
->ring
[ering
->cursor
];
368 ent
->eflags
= eflags
;
369 ent
->err_mask
= err_mask
;
370 ent
->timestamp
= get_jiffies_64();
373 static struct ata_ering_entry
*ata_ering_top(struct ata_ering
*ering
)
375 struct ata_ering_entry
*ent
= &ering
->ring
[ering
->cursor
];
382 static void ata_ering_clear(struct ata_ering
*ering
)
384 memset(ering
, 0, sizeof(*ering
));
387 static int ata_ering_map(struct ata_ering
*ering
,
388 int (*map_fn
)(struct ata_ering_entry
*, void *),
392 struct ata_ering_entry
*ent
;
396 ent
= &ering
->ring
[idx
];
399 rc
= map_fn(ent
, arg
);
402 idx
= (idx
- 1 + ATA_ERING_SIZE
) % ATA_ERING_SIZE
;
403 } while (idx
!= ering
->cursor
);
408 static unsigned int ata_eh_dev_action(struct ata_device
*dev
)
410 struct ata_eh_context
*ehc
= &dev
->link
->eh_context
;
412 return ehc
->i
.action
| ehc
->i
.dev_action
[dev
->devno
];
415 static void ata_eh_clear_action(struct ata_link
*link
, struct ata_device
*dev
,
416 struct ata_eh_info
*ehi
, unsigned int action
)
418 struct ata_device
*tdev
;
421 ehi
->action
&= ~action
;
422 ata_link_for_each_dev(tdev
, link
)
423 ehi
->dev_action
[tdev
->devno
] &= ~action
;
425 /* doesn't make sense for port-wide EH actions */
426 WARN_ON(!(action
& ATA_EH_PERDEV_MASK
));
428 /* break ehi->action into ehi->dev_action */
429 if (ehi
->action
& action
) {
430 ata_link_for_each_dev(tdev
, link
)
431 ehi
->dev_action
[tdev
->devno
] |=
432 ehi
->action
& action
;
433 ehi
->action
&= ~action
;
436 /* turn off the specified per-dev action */
437 ehi
->dev_action
[dev
->devno
] &= ~action
;
442 * ata_scsi_timed_out - SCSI layer time out callback
443 * @cmd: timed out SCSI command
445 * Handles SCSI layer timeout. We race with normal completion of
446 * the qc for @cmd. If the qc is already gone, we lose and let
447 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
448 * timed out and EH should be invoked. Prevent ata_qc_complete()
449 * from finishing it by setting EH_SCHEDULED and return
452 * TODO: kill this function once old EH is gone.
455 * Called from timer context
458 * EH_HANDLED or EH_NOT_HANDLED
460 enum scsi_eh_timer_return
ata_scsi_timed_out(struct scsi_cmnd
*cmd
)
462 struct Scsi_Host
*host
= cmd
->device
->host
;
463 struct ata_port
*ap
= ata_shost_to_port(host
);
465 struct ata_queued_cmd
*qc
;
466 enum scsi_eh_timer_return ret
;
470 if (ap
->ops
->error_handler
) {
471 ret
= EH_NOT_HANDLED
;
476 spin_lock_irqsave(ap
->lock
, flags
);
477 qc
= ata_qc_from_tag(ap
, ap
->link
.active_tag
);
479 WARN_ON(qc
->scsicmd
!= cmd
);
480 qc
->flags
|= ATA_QCFLAG_EH_SCHEDULED
;
481 qc
->err_mask
|= AC_ERR_TIMEOUT
;
482 ret
= EH_NOT_HANDLED
;
484 spin_unlock_irqrestore(ap
->lock
, flags
);
487 DPRINTK("EXIT, ret=%d\n", ret
);
492 * ata_scsi_error - SCSI layer error handler callback
493 * @host: SCSI host on which error occurred
495 * Handles SCSI-layer-thrown error events.
498 * Inherited from SCSI layer (none, can sleep)
503 void ata_scsi_error(struct Scsi_Host
*host
)
505 struct ata_port
*ap
= ata_shost_to_port(host
);
511 /* synchronize with port task */
512 ata_port_flush_task(ap
);
514 /* synchronize with host lock and sort out timeouts */
516 /* For new EH, all qcs are finished in one of three ways -
517 * normal completion, error completion, and SCSI timeout.
518 * Both cmpletions can race against SCSI timeout. When normal
519 * completion wins, the qc never reaches EH. When error
520 * completion wins, the qc has ATA_QCFLAG_FAILED set.
522 * When SCSI timeout wins, things are a bit more complex.
523 * Normal or error completion can occur after the timeout but
524 * before this point. In such cases, both types of
525 * completions are honored. A scmd is determined to have
526 * timed out iff its associated qc is active and not failed.
528 if (ap
->ops
->error_handler
) {
529 struct scsi_cmnd
*scmd
, *tmp
;
532 spin_lock_irqsave(ap
->lock
, flags
);
534 list_for_each_entry_safe(scmd
, tmp
, &host
->eh_cmd_q
, eh_entry
) {
535 struct ata_queued_cmd
*qc
;
537 for (i
= 0; i
< ATA_MAX_QUEUE
; i
++) {
538 qc
= __ata_qc_from_tag(ap
, i
);
539 if (qc
->flags
& ATA_QCFLAG_ACTIVE
&&
544 if (i
< ATA_MAX_QUEUE
) {
545 /* the scmd has an associated qc */
546 if (!(qc
->flags
& ATA_QCFLAG_FAILED
)) {
547 /* which hasn't failed yet, timeout */
548 qc
->err_mask
|= AC_ERR_TIMEOUT
;
549 qc
->flags
|= ATA_QCFLAG_FAILED
;
553 /* Normal completion occurred after
554 * SCSI timeout but before this point.
555 * Successfully complete it.
557 scmd
->retries
= scmd
->allowed
;
558 scsi_eh_finish_cmd(scmd
, &ap
->eh_done_q
);
562 /* If we have timed out qcs. They belong to EH from
563 * this point but the state of the controller is
564 * unknown. Freeze the port to make sure the IRQ
565 * handler doesn't diddle with those qcs. This must
566 * be done atomically w.r.t. setting QCFLAG_FAILED.
569 __ata_port_freeze(ap
);
571 spin_unlock_irqrestore(ap
->lock
, flags
);
573 /* initialize eh_tries */
574 ap
->eh_tries
= ATA_EH_MAX_TRIES
;
576 spin_unlock_wait(ap
->lock
);
579 /* invoke error handler */
580 if (ap
->ops
->error_handler
) {
581 struct ata_link
*link
;
583 /* kill fast drain timer */
584 del_timer_sync(&ap
->fastdrain_timer
);
586 /* process port resume request */
587 ata_eh_handle_port_resume(ap
);
589 /* fetch & clear EH info */
590 spin_lock_irqsave(ap
->lock
, flags
);
592 __ata_port_for_each_link(link
, ap
) {
593 struct ata_eh_context
*ehc
= &link
->eh_context
;
594 struct ata_device
*dev
;
596 memset(&link
->eh_context
, 0, sizeof(link
->eh_context
));
597 link
->eh_context
.i
= link
->eh_info
;
598 memset(&link
->eh_info
, 0, sizeof(link
->eh_info
));
600 ata_link_for_each_dev(dev
, link
) {
601 int devno
= dev
->devno
;
603 ehc
->saved_xfer_mode
[devno
] = dev
->xfer_mode
;
604 if (ata_ncq_enabled(dev
))
605 ehc
->saved_ncq_enabled
|= 1 << devno
;
609 ap
->pflags
|= ATA_PFLAG_EH_IN_PROGRESS
;
610 ap
->pflags
&= ~ATA_PFLAG_EH_PENDING
;
611 ap
->excl_link
= NULL
; /* don't maintain exclusion over EH */
613 spin_unlock_irqrestore(ap
->lock
, flags
);
615 /* invoke EH, skip if unloading or suspended */
616 if (!(ap
->pflags
& (ATA_PFLAG_UNLOADING
| ATA_PFLAG_SUSPENDED
)))
617 ap
->ops
->error_handler(ap
);
621 /* process port suspend request */
622 ata_eh_handle_port_suspend(ap
);
624 /* Exception might have happend after ->error_handler
625 * recovered the port but before this point. Repeat
628 spin_lock_irqsave(ap
->lock
, flags
);
630 if (ap
->pflags
& ATA_PFLAG_EH_PENDING
) {
631 if (--ap
->eh_tries
) {
632 spin_unlock_irqrestore(ap
->lock
, flags
);
635 ata_port_printk(ap
, KERN_ERR
, "EH pending after %d "
636 "tries, giving up\n", ATA_EH_MAX_TRIES
);
637 ap
->pflags
&= ~ATA_PFLAG_EH_PENDING
;
640 /* this run is complete, make sure EH info is clear */
641 __ata_port_for_each_link(link
, ap
)
642 memset(&link
->eh_info
, 0, sizeof(link
->eh_info
));
644 /* Clear host_eh_scheduled while holding ap->lock such
645 * that if exception occurs after this point but
646 * before EH completion, SCSI midlayer will
649 host
->host_eh_scheduled
= 0;
651 spin_unlock_irqrestore(ap
->lock
, flags
);
653 WARN_ON(ata_qc_from_tag(ap
, ap
->link
.active_tag
) == NULL
);
654 ap
->ops
->eng_timeout(ap
);
657 /* finish or retry handled scmd's and clean up */
658 WARN_ON(host
->host_failed
|| !list_empty(&host
->eh_cmd_q
));
660 scsi_eh_flush_done_q(&ap
->eh_done_q
);
663 spin_lock_irqsave(ap
->lock
, flags
);
665 if (ap
->pflags
& ATA_PFLAG_LOADING
)
666 ap
->pflags
&= ~ATA_PFLAG_LOADING
;
667 else if (ap
->pflags
& ATA_PFLAG_SCSI_HOTPLUG
)
668 queue_delayed_work(ata_aux_wq
, &ap
->hotplug_task
, 0);
670 if (ap
->pflags
& ATA_PFLAG_RECOVERED
)
671 ata_port_printk(ap
, KERN_INFO
, "EH complete\n");
673 ap
->pflags
&= ~(ATA_PFLAG_SCSI_HOTPLUG
| ATA_PFLAG_RECOVERED
);
675 /* tell wait_eh that we're done */
676 ap
->pflags
&= ~ATA_PFLAG_EH_IN_PROGRESS
;
677 wake_up_all(&ap
->eh_wait_q
);
679 spin_unlock_irqrestore(ap
->lock
, flags
);
685 * ata_port_wait_eh - Wait for the currently pending EH to complete
686 * @ap: Port to wait EH for
688 * Wait until the currently pending EH is complete.
691 * Kernel thread context (may sleep).
693 void ata_port_wait_eh(struct ata_port
*ap
)
699 spin_lock_irqsave(ap
->lock
, flags
);
701 while (ap
->pflags
& (ATA_PFLAG_EH_PENDING
| ATA_PFLAG_EH_IN_PROGRESS
)) {
702 prepare_to_wait(&ap
->eh_wait_q
, &wait
, TASK_UNINTERRUPTIBLE
);
703 spin_unlock_irqrestore(ap
->lock
, flags
);
705 spin_lock_irqsave(ap
->lock
, flags
);
707 finish_wait(&ap
->eh_wait_q
, &wait
);
709 spin_unlock_irqrestore(ap
->lock
, flags
);
711 /* make sure SCSI EH is complete */
712 if (scsi_host_in_recovery(ap
->scsi_host
)) {
718 static int ata_eh_nr_in_flight(struct ata_port
*ap
)
723 /* count only non-internal commands */
724 for (tag
= 0; tag
< ATA_MAX_QUEUE
- 1; tag
++)
725 if (ata_qc_from_tag(ap
, tag
))
731 void ata_eh_fastdrain_timerfn(unsigned long arg
)
733 struct ata_port
*ap
= (void *)arg
;
737 spin_lock_irqsave(ap
->lock
, flags
);
739 cnt
= ata_eh_nr_in_flight(ap
);
745 if (cnt
== ap
->fastdrain_cnt
) {
748 /* No progress during the last interval, tag all
749 * in-flight qcs as timed out and freeze the port.
751 for (tag
= 0; tag
< ATA_MAX_QUEUE
- 1; tag
++) {
752 struct ata_queued_cmd
*qc
= ata_qc_from_tag(ap
, tag
);
754 qc
->err_mask
|= AC_ERR_TIMEOUT
;
759 /* some qcs have finished, give it another chance */
760 ap
->fastdrain_cnt
= cnt
;
761 ap
->fastdrain_timer
.expires
=
762 ata_deadline(jiffies
, ATA_EH_FASTDRAIN_INTERVAL
);
763 add_timer(&ap
->fastdrain_timer
);
767 spin_unlock_irqrestore(ap
->lock
, flags
);
771 * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
772 * @ap: target ATA port
773 * @fastdrain: activate fast drain
775 * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
776 * is non-zero and EH wasn't pending before. Fast drain ensures
777 * that EH kicks in in timely manner.
780 * spin_lock_irqsave(host lock)
782 static void ata_eh_set_pending(struct ata_port
*ap
, int fastdrain
)
786 /* already scheduled? */
787 if (ap
->pflags
& ATA_PFLAG_EH_PENDING
)
790 ap
->pflags
|= ATA_PFLAG_EH_PENDING
;
795 /* do we have in-flight qcs? */
796 cnt
= ata_eh_nr_in_flight(ap
);
800 /* activate fast drain */
801 ap
->fastdrain_cnt
= cnt
;
802 ap
->fastdrain_timer
.expires
=
803 ata_deadline(jiffies
, ATA_EH_FASTDRAIN_INTERVAL
);
804 add_timer(&ap
->fastdrain_timer
);
808 * ata_qc_schedule_eh - schedule qc for error handling
809 * @qc: command to schedule error handling for
811 * Schedule error handling for @qc. EH will kick in as soon as
812 * other commands are drained.
815 * spin_lock_irqsave(host lock)
817 void ata_qc_schedule_eh(struct ata_queued_cmd
*qc
)
819 struct ata_port
*ap
= qc
->ap
;
821 WARN_ON(!ap
->ops
->error_handler
);
823 qc
->flags
|= ATA_QCFLAG_FAILED
;
824 ata_eh_set_pending(ap
, 1);
826 /* The following will fail if timeout has already expired.
827 * ata_scsi_error() takes care of such scmds on EH entry.
828 * Note that ATA_QCFLAG_FAILED is unconditionally set after
829 * this function completes.
831 scsi_req_abort_cmd(qc
->scsicmd
);
835 * ata_port_schedule_eh - schedule error handling without a qc
836 * @ap: ATA port to schedule EH for
838 * Schedule error handling for @ap. EH will kick in as soon as
839 * all commands are drained.
842 * spin_lock_irqsave(host lock)
844 void ata_port_schedule_eh(struct ata_port
*ap
)
846 WARN_ON(!ap
->ops
->error_handler
);
848 if (ap
->pflags
& ATA_PFLAG_INITIALIZING
)
851 ata_eh_set_pending(ap
, 1);
852 scsi_schedule_eh(ap
->scsi_host
);
854 DPRINTK("port EH scheduled\n");
857 static int ata_do_link_abort(struct ata_port
*ap
, struct ata_link
*link
)
859 int tag
, nr_aborted
= 0;
861 WARN_ON(!ap
->ops
->error_handler
);
863 /* we're gonna abort all commands, no need for fast drain */
864 ata_eh_set_pending(ap
, 0);
866 for (tag
= 0; tag
< ATA_MAX_QUEUE
; tag
++) {
867 struct ata_queued_cmd
*qc
= ata_qc_from_tag(ap
, tag
);
869 if (qc
&& (!link
|| qc
->dev
->link
== link
)) {
870 qc
->flags
|= ATA_QCFLAG_FAILED
;
877 ata_port_schedule_eh(ap
);
883 * ata_link_abort - abort all qc's on the link
884 * @link: ATA link to abort qc's for
886 * Abort all active qc's active on @link and schedule EH.
889 * spin_lock_irqsave(host lock)
892 * Number of aborted qc's.
894 int ata_link_abort(struct ata_link
*link
)
896 return ata_do_link_abort(link
->ap
, link
);
900 * ata_port_abort - abort all qc's on the port
901 * @ap: ATA port to abort qc's for
903 * Abort all active qc's of @ap and schedule EH.
906 * spin_lock_irqsave(host_set lock)
909 * Number of aborted qc's.
911 int ata_port_abort(struct ata_port
*ap
)
913 return ata_do_link_abort(ap
, NULL
);
917 * __ata_port_freeze - freeze port
918 * @ap: ATA port to freeze
920 * This function is called when HSM violation or some other
921 * condition disrupts normal operation of the port. Frozen port
922 * is not allowed to perform any operation until the port is
923 * thawed, which usually follows a successful reset.
925 * ap->ops->freeze() callback can be used for freezing the port
926 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
927 * port cannot be frozen hardware-wise, the interrupt handler
928 * must ack and clear interrupts unconditionally while the port
932 * spin_lock_irqsave(host lock)
934 static void __ata_port_freeze(struct ata_port
*ap
)
936 WARN_ON(!ap
->ops
->error_handler
);
941 ap
->pflags
|= ATA_PFLAG_FROZEN
;
943 DPRINTK("ata%u port frozen\n", ap
->print_id
);
947 * ata_port_freeze - abort & freeze port
948 * @ap: ATA port to freeze
950 * Abort and freeze @ap.
953 * spin_lock_irqsave(host lock)
956 * Number of aborted commands.
958 int ata_port_freeze(struct ata_port
*ap
)
962 WARN_ON(!ap
->ops
->error_handler
);
964 nr_aborted
= ata_port_abort(ap
);
965 __ata_port_freeze(ap
);
971 * sata_async_notification - SATA async notification handler
972 * @ap: ATA port where async notification is received
974 * Handler to be called when async notification via SDB FIS is
975 * received. This function schedules EH if necessary.
978 * spin_lock_irqsave(host lock)
981 * 1 if EH is scheduled, 0 otherwise.
983 int sata_async_notification(struct ata_port
*ap
)
988 if (!(ap
->flags
& ATA_FLAG_AN
))
991 rc
= sata_scr_read(&ap
->link
, SCR_NOTIFICATION
, &sntf
);
993 sata_scr_write(&ap
->link
, SCR_NOTIFICATION
, sntf
);
995 if (!sata_pmp_attached(ap
) || rc
) {
996 /* PMP is not attached or SNTF is not available */
997 if (!sata_pmp_attached(ap
)) {
998 /* PMP is not attached. Check whether ATAPI
999 * AN is configured. If so, notify media
1002 struct ata_device
*dev
= ap
->link
.device
;
1004 if ((dev
->class == ATA_DEV_ATAPI
) &&
1005 (dev
->flags
& ATA_DFLAG_AN
))
1006 ata_scsi_media_change_notify(dev
);
1009 /* PMP is attached but SNTF is not available.
1010 * ATAPI async media change notification is
1011 * not used. The PMP must be reporting PHY
1012 * status change, schedule EH.
1014 ata_port_schedule_eh(ap
);
1018 /* PMP is attached and SNTF is available */
1019 struct ata_link
*link
;
1021 /* check and notify ATAPI AN */
1022 ata_port_for_each_link(link
, ap
) {
1023 if (!(sntf
& (1 << link
->pmp
)))
1026 if ((link
->device
->class == ATA_DEV_ATAPI
) &&
1027 (link
->device
->flags
& ATA_DFLAG_AN
))
1028 ata_scsi_media_change_notify(link
->device
);
1031 /* If PMP is reporting that PHY status of some
1032 * downstream ports has changed, schedule EH.
1034 if (sntf
& (1 << SATA_PMP_CTRL_PORT
)) {
1035 ata_port_schedule_eh(ap
);
1044 * ata_eh_freeze_port - EH helper to freeze port
1045 * @ap: ATA port to freeze
1052 void ata_eh_freeze_port(struct ata_port
*ap
)
1054 unsigned long flags
;
1056 if (!ap
->ops
->error_handler
)
1059 spin_lock_irqsave(ap
->lock
, flags
);
1060 __ata_port_freeze(ap
);
1061 spin_unlock_irqrestore(ap
->lock
, flags
);
1065 * ata_port_thaw_port - EH helper to thaw port
1066 * @ap: ATA port to thaw
1068 * Thaw frozen port @ap.
1073 void ata_eh_thaw_port(struct ata_port
*ap
)
1075 unsigned long flags
;
1077 if (!ap
->ops
->error_handler
)
1080 spin_lock_irqsave(ap
->lock
, flags
);
1082 ap
->pflags
&= ~ATA_PFLAG_FROZEN
;
1087 spin_unlock_irqrestore(ap
->lock
, flags
);
1089 DPRINTK("ata%u port thawed\n", ap
->print_id
);
1092 static void ata_eh_scsidone(struct scsi_cmnd
*scmd
)
1097 static void __ata_eh_qc_complete(struct ata_queued_cmd
*qc
)
1099 struct ata_port
*ap
= qc
->ap
;
1100 struct scsi_cmnd
*scmd
= qc
->scsicmd
;
1101 unsigned long flags
;
1103 spin_lock_irqsave(ap
->lock
, flags
);
1104 qc
->scsidone
= ata_eh_scsidone
;
1105 __ata_qc_complete(qc
);
1106 WARN_ON(ata_tag_valid(qc
->tag
));
1107 spin_unlock_irqrestore(ap
->lock
, flags
);
1109 scsi_eh_finish_cmd(scmd
, &ap
->eh_done_q
);
1113 * ata_eh_qc_complete - Complete an active ATA command from EH
1114 * @qc: Command to complete
1116 * Indicate to the mid and upper layers that an ATA command has
1117 * completed. To be used from EH.
1119 void ata_eh_qc_complete(struct ata_queued_cmd
*qc
)
1121 struct scsi_cmnd
*scmd
= qc
->scsicmd
;
1122 scmd
->retries
= scmd
->allowed
;
1123 __ata_eh_qc_complete(qc
);
1127 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
1128 * @qc: Command to retry
1130 * Indicate to the mid and upper layers that an ATA command
1131 * should be retried. To be used from EH.
1133 * SCSI midlayer limits the number of retries to scmd->allowed.
1134 * scmd->retries is decremented for commands which get retried
1135 * due to unrelated failures (qc->err_mask is zero).
1137 void ata_eh_qc_retry(struct ata_queued_cmd
*qc
)
1139 struct scsi_cmnd
*scmd
= qc
->scsicmd
;
1140 if (!qc
->err_mask
&& scmd
->retries
)
1142 __ata_eh_qc_complete(qc
);
1146 * ata_eh_detach_dev - detach ATA device
1147 * @dev: ATA device to detach
1154 void ata_eh_detach_dev(struct ata_device
*dev
)
1156 struct ata_link
*link
= dev
->link
;
1157 struct ata_port
*ap
= link
->ap
;
1158 unsigned long flags
;
1160 ata_dev_disable(dev
);
1162 spin_lock_irqsave(ap
->lock
, flags
);
1164 dev
->flags
&= ~ATA_DFLAG_DETACH
;
1166 if (ata_scsi_offline_dev(dev
)) {
1167 dev
->flags
|= ATA_DFLAG_DETACHED
;
1168 ap
->pflags
|= ATA_PFLAG_SCSI_HOTPLUG
;
1171 /* clear per-dev EH actions */
1172 ata_eh_clear_action(link
, dev
, &link
->eh_info
, ATA_EH_PERDEV_MASK
);
1173 ata_eh_clear_action(link
, dev
, &link
->eh_context
.i
, ATA_EH_PERDEV_MASK
);
1175 spin_unlock_irqrestore(ap
->lock
, flags
);
1179 * ata_eh_about_to_do - about to perform eh_action
1180 * @link: target ATA link
1181 * @dev: target ATA dev for per-dev action (can be NULL)
1182 * @action: action about to be performed
1184 * Called just before performing EH actions to clear related bits
1185 * in @link->eh_info such that eh actions are not unnecessarily
1191 void ata_eh_about_to_do(struct ata_link
*link
, struct ata_device
*dev
,
1192 unsigned int action
)
1194 struct ata_port
*ap
= link
->ap
;
1195 struct ata_eh_info
*ehi
= &link
->eh_info
;
1196 struct ata_eh_context
*ehc
= &link
->eh_context
;
1197 unsigned long flags
;
1199 spin_lock_irqsave(ap
->lock
, flags
);
1201 ata_eh_clear_action(link
, dev
, ehi
, action
);
1203 if (!(ehc
->i
.flags
& ATA_EHI_QUIET
))
1204 ap
->pflags
|= ATA_PFLAG_RECOVERED
;
1206 spin_unlock_irqrestore(ap
->lock
, flags
);
1210 * ata_eh_done - EH action complete
1211 * @ap: target ATA port
1212 * @dev: target ATA dev for per-dev action (can be NULL)
1213 * @action: action just completed
1215 * Called right after performing EH actions to clear related bits
1216 * in @link->eh_context.
1221 void ata_eh_done(struct ata_link
*link
, struct ata_device
*dev
,
1222 unsigned int action
)
1224 struct ata_eh_context
*ehc
= &link
->eh_context
;
1226 ata_eh_clear_action(link
, dev
, &ehc
->i
, action
);
1230 * ata_err_string - convert err_mask to descriptive string
1231 * @err_mask: error mask to convert to string
1233 * Convert @err_mask to descriptive string. Errors are
1234 * prioritized according to severity and only the most severe
1235 * error is reported.
1241 * Descriptive string for @err_mask
1243 static const char *ata_err_string(unsigned int err_mask
)
1245 if (err_mask
& AC_ERR_HOST_BUS
)
1246 return "host bus error";
1247 if (err_mask
& AC_ERR_ATA_BUS
)
1248 return "ATA bus error";
1249 if (err_mask
& AC_ERR_TIMEOUT
)
1251 if (err_mask
& AC_ERR_HSM
)
1252 return "HSM violation";
1253 if (err_mask
& AC_ERR_SYSTEM
)
1254 return "internal error";
1255 if (err_mask
& AC_ERR_MEDIA
)
1256 return "media error";
1257 if (err_mask
& AC_ERR_INVALID
)
1258 return "invalid argument";
1259 if (err_mask
& AC_ERR_DEV
)
1260 return "device error";
1261 return "unknown error";
1265 * ata_read_log_page - read a specific log page
1266 * @dev: target device
1267 * @page: page to read
1268 * @buf: buffer to store read page
1269 * @sectors: number of sectors to read
1271 * Read log page using READ_LOG_EXT command.
1274 * Kernel thread context (may sleep).
1277 * 0 on success, AC_ERR_* mask otherwise.
1279 static unsigned int ata_read_log_page(struct ata_device
*dev
,
1280 u8 page
, void *buf
, unsigned int sectors
)
1282 struct ata_taskfile tf
;
1283 unsigned int err_mask
;
1285 DPRINTK("read log page - page %d\n", page
);
1287 ata_tf_init(dev
, &tf
);
1288 tf
.command
= ATA_CMD_READ_LOG_EXT
;
1291 tf
.hob_nsect
= sectors
>> 8;
1292 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_LBA48
| ATA_TFLAG_DEVICE
;
1293 tf
.protocol
= ATA_PROT_PIO
;
1295 err_mask
= ata_exec_internal(dev
, &tf
, NULL
, DMA_FROM_DEVICE
,
1296 buf
, sectors
* ATA_SECT_SIZE
, 0);
1298 DPRINTK("EXIT, err_mask=%x\n", err_mask
);
1303 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
1304 * @dev: Device to read log page 10h from
1305 * @tag: Resulting tag of the failed command
1306 * @tf: Resulting taskfile registers of the failed command
1308 * Read log page 10h to obtain NCQ error details and clear error
1312 * Kernel thread context (may sleep).
1315 * 0 on success, -errno otherwise.
1317 static int ata_eh_read_log_10h(struct ata_device
*dev
,
1318 int *tag
, struct ata_taskfile
*tf
)
1320 u8
*buf
= dev
->link
->ap
->sector_buf
;
1321 unsigned int err_mask
;
1325 err_mask
= ata_read_log_page(dev
, ATA_LOG_SATA_NCQ
, buf
, 1);
1330 for (i
= 0; i
< ATA_SECT_SIZE
; i
++)
1333 ata_dev_printk(dev
, KERN_WARNING
,
1334 "invalid checksum 0x%x on log page 10h\n", csum
);
1339 *tag
= buf
[0] & 0x1f;
1341 tf
->command
= buf
[2];
1342 tf
->feature
= buf
[3];
1346 tf
->device
= buf
[7];
1347 tf
->hob_lbal
= buf
[8];
1348 tf
->hob_lbam
= buf
[9];
1349 tf
->hob_lbah
= buf
[10];
1350 tf
->nsect
= buf
[12];
1351 tf
->hob_nsect
= buf
[13];
1357 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1358 * @dev: device to perform REQUEST_SENSE to
1359 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
1360 * @dfl_sense_key: default sense key to use
1362 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
1363 * SENSE. This function is EH helper.
1366 * Kernel thread context (may sleep).
1369 * 0 on success, AC_ERR_* mask on failure
1371 static unsigned int atapi_eh_request_sense(struct ata_device
*dev
,
1372 u8
*sense_buf
, u8 dfl_sense_key
)
1374 u8 cdb
[ATAPI_CDB_LEN
] =
1375 { REQUEST_SENSE
, 0, 0, 0, SCSI_SENSE_BUFFERSIZE
, 0 };
1376 struct ata_port
*ap
= dev
->link
->ap
;
1377 struct ata_taskfile tf
;
1379 DPRINTK("ATAPI request sense\n");
1381 /* FIXME: is this needed? */
1382 memset(sense_buf
, 0, SCSI_SENSE_BUFFERSIZE
);
1384 /* initialize sense_buf with the error register,
1385 * for the case where they are -not- overwritten
1387 sense_buf
[0] = 0x70;
1388 sense_buf
[2] = dfl_sense_key
;
1390 /* some devices time out if garbage left in tf */
1391 ata_tf_init(dev
, &tf
);
1393 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
1394 tf
.command
= ATA_CMD_PACKET
;
1396 /* is it pointless to prefer PIO for "safety reasons"? */
1397 if (ap
->flags
& ATA_FLAG_PIO_DMA
) {
1398 tf
.protocol
= ATAPI_PROT_DMA
;
1399 tf
.feature
|= ATAPI_PKT_DMA
;
1401 tf
.protocol
= ATAPI_PROT_PIO
;
1402 tf
.lbam
= SCSI_SENSE_BUFFERSIZE
;
1406 return ata_exec_internal(dev
, &tf
, cdb
, DMA_FROM_DEVICE
,
1407 sense_buf
, SCSI_SENSE_BUFFERSIZE
, 0);
1411 * ata_eh_analyze_serror - analyze SError for a failed port
1412 * @link: ATA link to analyze SError for
1414 * Analyze SError if available and further determine cause of
1420 static void ata_eh_analyze_serror(struct ata_link
*link
)
1422 struct ata_eh_context
*ehc
= &link
->eh_context
;
1423 u32 serror
= ehc
->i
.serror
;
1424 unsigned int err_mask
= 0, action
= 0;
1427 if (serror
& (SERR_PERSISTENT
| SERR_DATA
)) {
1428 err_mask
|= AC_ERR_ATA_BUS
;
1429 action
|= ATA_EH_RESET
;
1431 if (serror
& SERR_PROTOCOL
) {
1432 err_mask
|= AC_ERR_HSM
;
1433 action
|= ATA_EH_RESET
;
1435 if (serror
& SERR_INTERNAL
) {
1436 err_mask
|= AC_ERR_SYSTEM
;
1437 action
|= ATA_EH_RESET
;
1440 /* Determine whether a hotplug event has occurred. Both
1441 * SError.N/X are considered hotplug events for enabled or
1442 * host links. For disabled PMP links, only N bit is
1443 * considered as X bit is left at 1 for link plugging.
1447 if (!(link
->flags
& ATA_LFLAG_DISABLED
) || ata_is_host_link(link
))
1448 hotplug_mask
= SERR_PHYRDY_CHG
| SERR_DEV_XCHG
;
1450 hotplug_mask
= SERR_PHYRDY_CHG
;
1452 if (serror
& hotplug_mask
)
1453 ata_ehi_hotplugged(&ehc
->i
);
1455 ehc
->i
.err_mask
|= err_mask
;
1456 ehc
->i
.action
|= action
;
1460 * ata_eh_analyze_ncq_error - analyze NCQ error
1461 * @link: ATA link to analyze NCQ error for
1463 * Read log page 10h, determine the offending qc and acquire
1464 * error status TF. For NCQ device errors, all LLDDs have to do
1465 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1469 * Kernel thread context (may sleep).
1471 void ata_eh_analyze_ncq_error(struct ata_link
*link
)
1473 struct ata_port
*ap
= link
->ap
;
1474 struct ata_eh_context
*ehc
= &link
->eh_context
;
1475 struct ata_device
*dev
= link
->device
;
1476 struct ata_queued_cmd
*qc
;
1477 struct ata_taskfile tf
;
1480 /* if frozen, we can't do much */
1481 if (ap
->pflags
& ATA_PFLAG_FROZEN
)
1484 /* is it NCQ device error? */
1485 if (!link
->sactive
|| !(ehc
->i
.err_mask
& AC_ERR_DEV
))
1488 /* has LLDD analyzed already? */
1489 for (tag
= 0; tag
< ATA_MAX_QUEUE
; tag
++) {
1490 qc
= __ata_qc_from_tag(ap
, tag
);
1492 if (!(qc
->flags
& ATA_QCFLAG_FAILED
))
1499 /* okay, this error is ours */
1500 rc
= ata_eh_read_log_10h(dev
, &tag
, &tf
);
1502 ata_link_printk(link
, KERN_ERR
, "failed to read log page 10h "
1503 "(errno=%d)\n", rc
);
1507 if (!(link
->sactive
& (1 << tag
))) {
1508 ata_link_printk(link
, KERN_ERR
, "log page 10h reported "
1509 "inactive tag %d\n", tag
);
1513 /* we've got the perpetrator, condemn it */
1514 qc
= __ata_qc_from_tag(ap
, tag
);
1515 memcpy(&qc
->result_tf
, &tf
, sizeof(tf
));
1516 qc
->result_tf
.flags
= ATA_TFLAG_ISADDR
| ATA_TFLAG_LBA
| ATA_TFLAG_LBA48
;
1517 qc
->err_mask
|= AC_ERR_DEV
| AC_ERR_NCQ
;
1518 ehc
->i
.err_mask
&= ~AC_ERR_DEV
;
1522 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1523 * @qc: qc to analyze
1524 * @tf: Taskfile registers to analyze
1526 * Analyze taskfile of @qc and further determine cause of
1527 * failure. This function also requests ATAPI sense data if
1531 * Kernel thread context (may sleep).
1534 * Determined recovery action
1536 static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd
*qc
,
1537 const struct ata_taskfile
*tf
)
1539 unsigned int tmp
, action
= 0;
1540 u8 stat
= tf
->command
, err
= tf
->feature
;
1542 if ((stat
& (ATA_BUSY
| ATA_DRQ
| ATA_DRDY
)) != ATA_DRDY
) {
1543 qc
->err_mask
|= AC_ERR_HSM
;
1544 return ATA_EH_RESET
;
1547 if (stat
& (ATA_ERR
| ATA_DF
))
1548 qc
->err_mask
|= AC_ERR_DEV
;
1552 switch (qc
->dev
->class) {
1555 qc
->err_mask
|= AC_ERR_ATA_BUS
;
1557 qc
->err_mask
|= AC_ERR_MEDIA
;
1559 qc
->err_mask
|= AC_ERR_INVALID
;
1563 if (!(qc
->ap
->pflags
& ATA_PFLAG_FROZEN
)) {
1564 tmp
= atapi_eh_request_sense(qc
->dev
,
1565 qc
->scsicmd
->sense_buffer
,
1566 qc
->result_tf
.feature
>> 4);
1568 /* ATA_QCFLAG_SENSE_VALID is used to
1569 * tell atapi_qc_complete() that sense
1570 * data is already valid.
1572 * TODO: interpret sense data and set
1573 * appropriate err_mask.
1575 qc
->flags
|= ATA_QCFLAG_SENSE_VALID
;
1577 qc
->err_mask
|= tmp
;
1581 if (qc
->err_mask
& (AC_ERR_HSM
| AC_ERR_TIMEOUT
| AC_ERR_ATA_BUS
))
1582 action
|= ATA_EH_RESET
;
1587 static int ata_eh_categorize_error(unsigned int eflags
, unsigned int err_mask
,
1592 if (!(eflags
& ATA_EFLAG_DUBIOUS_XFER
))
1596 base
= ATA_ECAT_DUBIOUS_NONE
;
1598 if (err_mask
& AC_ERR_ATA_BUS
)
1599 return base
+ ATA_ECAT_ATA_BUS
;
1601 if (err_mask
& AC_ERR_TIMEOUT
)
1602 return base
+ ATA_ECAT_TOUT_HSM
;
1604 if (eflags
& ATA_EFLAG_IS_IO
) {
1605 if (err_mask
& AC_ERR_HSM
)
1606 return base
+ ATA_ECAT_TOUT_HSM
;
1608 (AC_ERR_DEV
|AC_ERR_MEDIA
|AC_ERR_INVALID
)) == AC_ERR_DEV
)
1609 return base
+ ATA_ECAT_UNK_DEV
;
1615 struct speed_down_verdict_arg
{
1618 int nr_errors
[ATA_ECAT_NR
];
1621 static int speed_down_verdict_cb(struct ata_ering_entry
*ent
, void *void_arg
)
1623 struct speed_down_verdict_arg
*arg
= void_arg
;
1626 if (ent
->timestamp
< arg
->since
)
1629 cat
= ata_eh_categorize_error(ent
->eflags
, ent
->err_mask
,
1631 arg
->nr_errors
[cat
]++;
1637 * ata_eh_speed_down_verdict - Determine speed down verdict
1638 * @dev: Device of interest
1640 * This function examines error ring of @dev and determines
1641 * whether NCQ needs to be turned off, transfer speed should be
1642 * stepped down, or falling back to PIO is necessary.
1644 * ECAT_ATA_BUS : ATA_BUS error for any command
1646 * ECAT_TOUT_HSM : TIMEOUT for any command or HSM violation for
1649 * ECAT_UNK_DEV : Unknown DEV error for IO commands
1651 * ECAT_DUBIOUS_* : Identical to above three but occurred while
1652 * data transfer hasn't been verified.
1656 * NCQ_OFF : Turn off NCQ.
1658 * SPEED_DOWN : Speed down transfer speed but don't fall back
1661 * FALLBACK_TO_PIO : Fall back to PIO.
1663 * Even if multiple verdicts are returned, only one action is
1664 * taken per error. An action triggered by non-DUBIOUS errors
1665 * clears ering, while one triggered by DUBIOUS_* errors doesn't.
1666 * This is to expedite speed down decisions right after device is
1667 * initially configured.
1669 * The followings are speed down rules. #1 and #2 deal with
1672 * 1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors
1673 * occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO.
1675 * 2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors
1676 * occurred during last 5 mins, NCQ_OFF.
1678 * 3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors
1679 * ocurred during last 5 mins, FALLBACK_TO_PIO
1681 * 4. If more than 3 TOUT_HSM or UNK_DEV errors occurred
1682 * during last 10 mins, NCQ_OFF.
1684 * 5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6
1685 * UNK_DEV errors occurred during last 10 mins, SPEED_DOWN.
1688 * Inherited from caller.
1691 * OR of ATA_EH_SPDN_* flags.
1693 static unsigned int ata_eh_speed_down_verdict(struct ata_device
*dev
)
1695 const u64 j5mins
= 5LLU * 60 * HZ
, j10mins
= 10LLU * 60 * HZ
;
1696 u64 j64
= get_jiffies_64();
1697 struct speed_down_verdict_arg arg
;
1698 unsigned int verdict
= 0;
1700 /* scan past 5 mins of error history */
1701 memset(&arg
, 0, sizeof(arg
));
1702 arg
.since
= j64
- min(j64
, j5mins
);
1703 ata_ering_map(&dev
->ering
, speed_down_verdict_cb
, &arg
);
1705 if (arg
.nr_errors
[ATA_ECAT_DUBIOUS_ATA_BUS
] +
1706 arg
.nr_errors
[ATA_ECAT_DUBIOUS_TOUT_HSM
] > 1)
1707 verdict
|= ATA_EH_SPDN_SPEED_DOWN
|
1708 ATA_EH_SPDN_FALLBACK_TO_PIO
| ATA_EH_SPDN_KEEP_ERRORS
;
1710 if (arg
.nr_errors
[ATA_ECAT_DUBIOUS_TOUT_HSM
] +
1711 arg
.nr_errors
[ATA_ECAT_DUBIOUS_UNK_DEV
] > 1)
1712 verdict
|= ATA_EH_SPDN_NCQ_OFF
| ATA_EH_SPDN_KEEP_ERRORS
;
1714 if (arg
.nr_errors
[ATA_ECAT_ATA_BUS
] +
1715 arg
.nr_errors
[ATA_ECAT_TOUT_HSM
] +
1716 arg
.nr_errors
[ATA_ECAT_UNK_DEV
] > 6)
1717 verdict
|= ATA_EH_SPDN_FALLBACK_TO_PIO
;
1719 /* scan past 10 mins of error history */
1720 memset(&arg
, 0, sizeof(arg
));
1721 arg
.since
= j64
- min(j64
, j10mins
);
1722 ata_ering_map(&dev
->ering
, speed_down_verdict_cb
, &arg
);
1724 if (arg
.nr_errors
[ATA_ECAT_TOUT_HSM
] +
1725 arg
.nr_errors
[ATA_ECAT_UNK_DEV
] > 3)
1726 verdict
|= ATA_EH_SPDN_NCQ_OFF
;
1728 if (arg
.nr_errors
[ATA_ECAT_ATA_BUS
] +
1729 arg
.nr_errors
[ATA_ECAT_TOUT_HSM
] > 3 ||
1730 arg
.nr_errors
[ATA_ECAT_UNK_DEV
] > 6)
1731 verdict
|= ATA_EH_SPDN_SPEED_DOWN
;
1737 * ata_eh_speed_down - record error and speed down if necessary
1738 * @dev: Failed device
1739 * @eflags: mask of ATA_EFLAG_* flags
1740 * @err_mask: err_mask of the error
1742 * Record error and examine error history to determine whether
1743 * adjusting transmission speed is necessary. It also sets
1744 * transmission limits appropriately if such adjustment is
1748 * Kernel thread context (may sleep).
1751 * Determined recovery action.
1753 static unsigned int ata_eh_speed_down(struct ata_device
*dev
,
1754 unsigned int eflags
, unsigned int err_mask
)
1756 struct ata_link
*link
= dev
->link
;
1758 unsigned int verdict
;
1759 unsigned int action
= 0;
1761 /* don't bother if Cat-0 error */
1762 if (ata_eh_categorize_error(eflags
, err_mask
, &xfer_ok
) == 0)
1765 /* record error and determine whether speed down is necessary */
1766 ata_ering_record(&dev
->ering
, eflags
, err_mask
);
1767 verdict
= ata_eh_speed_down_verdict(dev
);
1770 if ((verdict
& ATA_EH_SPDN_NCQ_OFF
) &&
1771 (dev
->flags
& (ATA_DFLAG_PIO
| ATA_DFLAG_NCQ
|
1772 ATA_DFLAG_NCQ_OFF
)) == ATA_DFLAG_NCQ
) {
1773 dev
->flags
|= ATA_DFLAG_NCQ_OFF
;
1774 ata_dev_printk(dev
, KERN_WARNING
,
1775 "NCQ disabled due to excessive errors\n");
1780 if (verdict
& ATA_EH_SPDN_SPEED_DOWN
) {
1781 /* speed down SATA link speed if possible */
1782 if (sata_down_spd_limit(link
) == 0) {
1783 action
|= ATA_EH_RESET
;
1787 /* lower transfer mode */
1788 if (dev
->spdn_cnt
< 2) {
1789 static const int dma_dnxfer_sel
[] =
1790 { ATA_DNXFER_DMA
, ATA_DNXFER_40C
};
1791 static const int pio_dnxfer_sel
[] =
1792 { ATA_DNXFER_PIO
, ATA_DNXFER_FORCE_PIO0
};
1795 if (dev
->xfer_shift
!= ATA_SHIFT_PIO
)
1796 sel
= dma_dnxfer_sel
[dev
->spdn_cnt
];
1798 sel
= pio_dnxfer_sel
[dev
->spdn_cnt
];
1802 if (ata_down_xfermask_limit(dev
, sel
) == 0) {
1803 action
|= ATA_EH_RESET
;
1809 /* Fall back to PIO? Slowing down to PIO is meaningless for
1810 * SATA ATA devices. Consider it only for PATA and SATAPI.
1812 if ((verdict
& ATA_EH_SPDN_FALLBACK_TO_PIO
) && (dev
->spdn_cnt
>= 2) &&
1813 (link
->ap
->cbl
!= ATA_CBL_SATA
|| dev
->class == ATA_DEV_ATAPI
) &&
1814 (dev
->xfer_shift
!= ATA_SHIFT_PIO
)) {
1815 if (ata_down_xfermask_limit(dev
, ATA_DNXFER_FORCE_PIO
) == 0) {
1817 action
|= ATA_EH_RESET
;
1824 /* device has been slowed down, blow error history */
1825 if (!(verdict
& ATA_EH_SPDN_KEEP_ERRORS
))
1826 ata_ering_clear(&dev
->ering
);
1831 * ata_eh_link_autopsy - analyze error and determine recovery action
1832 * @link: host link to perform autopsy on
1834 * Analyze why @link failed and determine which recovery actions
1835 * are needed. This function also sets more detailed AC_ERR_*
1836 * values and fills sense data for ATAPI CHECK SENSE.
1839 * Kernel thread context (may sleep).
1841 static void ata_eh_link_autopsy(struct ata_link
*link
)
1843 struct ata_port
*ap
= link
->ap
;
1844 struct ata_eh_context
*ehc
= &link
->eh_context
;
1845 struct ata_device
*dev
;
1846 unsigned int all_err_mask
= 0, eflags
= 0;
1853 if (ehc
->i
.flags
& ATA_EHI_NO_AUTOPSY
)
1856 /* obtain and analyze SError */
1857 rc
= sata_scr_read(link
, SCR_ERROR
, &serror
);
1859 ehc
->i
.serror
|= serror
;
1860 ata_eh_analyze_serror(link
);
1861 } else if (rc
!= -EOPNOTSUPP
) {
1862 /* SError read failed, force reset and probing */
1863 ehc
->i
.probe_mask
|= ATA_ALL_DEVICES
;
1864 ehc
->i
.action
|= ATA_EH_RESET
;
1865 ehc
->i
.err_mask
|= AC_ERR_OTHER
;
1868 /* analyze NCQ failure */
1869 ata_eh_analyze_ncq_error(link
);
1871 /* any real error trumps AC_ERR_OTHER */
1872 if (ehc
->i
.err_mask
& ~AC_ERR_OTHER
)
1873 ehc
->i
.err_mask
&= ~AC_ERR_OTHER
;
1875 all_err_mask
|= ehc
->i
.err_mask
;
1877 for (tag
= 0; tag
< ATA_MAX_QUEUE
; tag
++) {
1878 struct ata_queued_cmd
*qc
= __ata_qc_from_tag(ap
, tag
);
1880 if (!(qc
->flags
& ATA_QCFLAG_FAILED
) || qc
->dev
->link
!= link
)
1883 /* inherit upper level err_mask */
1884 qc
->err_mask
|= ehc
->i
.err_mask
;
1887 ehc
->i
.action
|= ata_eh_analyze_tf(qc
, &qc
->result_tf
);
1889 /* DEV errors are probably spurious in case of ATA_BUS error */
1890 if (qc
->err_mask
& AC_ERR_ATA_BUS
)
1891 qc
->err_mask
&= ~(AC_ERR_DEV
| AC_ERR_MEDIA
|
1894 /* any real error trumps unknown error */
1895 if (qc
->err_mask
& ~AC_ERR_OTHER
)
1896 qc
->err_mask
&= ~AC_ERR_OTHER
;
1898 /* SENSE_VALID trumps dev/unknown error and revalidation */
1899 if (qc
->flags
& ATA_QCFLAG_SENSE_VALID
)
1900 qc
->err_mask
&= ~(AC_ERR_DEV
| AC_ERR_OTHER
);
1902 /* determine whether the command is worth retrying */
1903 if (!(qc
->err_mask
& AC_ERR_INVALID
) &&
1904 ((qc
->flags
& ATA_QCFLAG_IO
) || qc
->err_mask
!= AC_ERR_DEV
))
1905 qc
->flags
|= ATA_QCFLAG_RETRY
;
1907 /* accumulate error info */
1908 ehc
->i
.dev
= qc
->dev
;
1909 all_err_mask
|= qc
->err_mask
;
1910 if (qc
->flags
& ATA_QCFLAG_IO
)
1911 eflags
|= ATA_EFLAG_IS_IO
;
1914 /* enforce default EH actions */
1915 if (ap
->pflags
& ATA_PFLAG_FROZEN
||
1916 all_err_mask
& (AC_ERR_HSM
| AC_ERR_TIMEOUT
))
1917 ehc
->i
.action
|= ATA_EH_RESET
;
1918 else if (((eflags
& ATA_EFLAG_IS_IO
) && all_err_mask
) ||
1919 (!(eflags
& ATA_EFLAG_IS_IO
) && (all_err_mask
& ~AC_ERR_DEV
)))
1920 ehc
->i
.action
|= ATA_EH_REVALIDATE
;
1922 /* If we have offending qcs and the associated failed device,
1923 * perform per-dev EH action only on the offending device.
1926 ehc
->i
.dev_action
[ehc
->i
.dev
->devno
] |=
1927 ehc
->i
.action
& ATA_EH_PERDEV_MASK
;
1928 ehc
->i
.action
&= ~ATA_EH_PERDEV_MASK
;
1931 /* propagate timeout to host link */
1932 if ((all_err_mask
& AC_ERR_TIMEOUT
) && !ata_is_host_link(link
))
1933 ap
->link
.eh_context
.i
.err_mask
|= AC_ERR_TIMEOUT
;
1935 /* record error and consider speeding down */
1937 if (!dev
&& ((ata_link_max_devices(link
) == 1 &&
1938 ata_dev_enabled(link
->device
))))
1942 if (dev
->flags
& ATA_DFLAG_DUBIOUS_XFER
)
1943 eflags
|= ATA_EFLAG_DUBIOUS_XFER
;
1944 ehc
->i
.action
|= ata_eh_speed_down(dev
, eflags
, all_err_mask
);
1951 * ata_eh_autopsy - analyze error and determine recovery action
1952 * @ap: host port to perform autopsy on
1954 * Analyze all links of @ap and determine why they failed and
1955 * which recovery actions are needed.
1958 * Kernel thread context (may sleep).
1960 void ata_eh_autopsy(struct ata_port
*ap
)
1962 struct ata_link
*link
;
1964 ata_port_for_each_link(link
, ap
)
1965 ata_eh_link_autopsy(link
);
1967 /* Autopsy of fanout ports can affect host link autopsy.
1968 * Perform host link autopsy last.
1970 if (sata_pmp_attached(ap
))
1971 ata_eh_link_autopsy(&ap
->link
);
1975 * ata_eh_link_report - report error handling to user
1976 * @link: ATA link EH is going on
1978 * Report EH to user.
1983 static void ata_eh_link_report(struct ata_link
*link
)
1985 struct ata_port
*ap
= link
->ap
;
1986 struct ata_eh_context
*ehc
= &link
->eh_context
;
1987 const char *frozen
, *desc
;
1989 int tag
, nr_failed
= 0;
1991 if (ehc
->i
.flags
& ATA_EHI_QUIET
)
1995 if (ehc
->i
.desc
[0] != '\0')
1998 for (tag
= 0; tag
< ATA_MAX_QUEUE
; tag
++) {
1999 struct ata_queued_cmd
*qc
= __ata_qc_from_tag(ap
, tag
);
2001 if (!(qc
->flags
& ATA_QCFLAG_FAILED
) || qc
->dev
->link
!= link
||
2002 ((qc
->flags
& ATA_QCFLAG_QUIET
) &&
2003 qc
->err_mask
== AC_ERR_DEV
))
2005 if (qc
->flags
& ATA_QCFLAG_SENSE_VALID
&& !qc
->err_mask
)
2011 if (!nr_failed
&& !ehc
->i
.err_mask
)
2015 if (ap
->pflags
& ATA_PFLAG_FROZEN
)
2018 memset(tries_buf
, 0, sizeof(tries_buf
));
2019 if (ap
->eh_tries
< ATA_EH_MAX_TRIES
)
2020 snprintf(tries_buf
, sizeof(tries_buf
) - 1, " t%d",
2024 ata_dev_printk(ehc
->i
.dev
, KERN_ERR
, "exception Emask 0x%x "
2025 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2026 ehc
->i
.err_mask
, link
->sactive
, ehc
->i
.serror
,
2027 ehc
->i
.action
, frozen
, tries_buf
);
2029 ata_dev_printk(ehc
->i
.dev
, KERN_ERR
, "%s\n", desc
);
2031 ata_link_printk(link
, KERN_ERR
, "exception Emask 0x%x "
2032 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2033 ehc
->i
.err_mask
, link
->sactive
, ehc
->i
.serror
,
2034 ehc
->i
.action
, frozen
, tries_buf
);
2036 ata_link_printk(link
, KERN_ERR
, "%s\n", desc
);
2040 ata_link_printk(link
, KERN_ERR
,
2041 "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
2042 ehc
->i
.serror
& SERR_DATA_RECOVERED
? "RecovData " : "",
2043 ehc
->i
.serror
& SERR_COMM_RECOVERED
? "RecovComm " : "",
2044 ehc
->i
.serror
& SERR_DATA
? "UnrecovData " : "",
2045 ehc
->i
.serror
& SERR_PERSISTENT
? "Persist " : "",
2046 ehc
->i
.serror
& SERR_PROTOCOL
? "Proto " : "",
2047 ehc
->i
.serror
& SERR_INTERNAL
? "HostInt " : "",
2048 ehc
->i
.serror
& SERR_PHYRDY_CHG
? "PHYRdyChg " : "",
2049 ehc
->i
.serror
& SERR_PHY_INT_ERR
? "PHYInt " : "",
2050 ehc
->i
.serror
& SERR_COMM_WAKE
? "CommWake " : "",
2051 ehc
->i
.serror
& SERR_10B_8B_ERR
? "10B8B " : "",
2052 ehc
->i
.serror
& SERR_DISPARITY
? "Dispar " : "",
2053 ehc
->i
.serror
& SERR_CRC
? "BadCRC " : "",
2054 ehc
->i
.serror
& SERR_HANDSHAKE
? "Handshk " : "",
2055 ehc
->i
.serror
& SERR_LINK_SEQ_ERR
? "LinkSeq " : "",
2056 ehc
->i
.serror
& SERR_TRANS_ST_ERROR
? "TrStaTrns " : "",
2057 ehc
->i
.serror
& SERR_UNRECOG_FIS
? "UnrecFIS " : "",
2058 ehc
->i
.serror
& SERR_DEV_XCHG
? "DevExch " : "");
2060 for (tag
= 0; tag
< ATA_MAX_QUEUE
; tag
++) {
2061 struct ata_queued_cmd
*qc
= __ata_qc_from_tag(ap
, tag
);
2062 struct ata_taskfile
*cmd
= &qc
->tf
, *res
= &qc
->result_tf
;
2063 const u8
*cdb
= qc
->cdb
;
2064 char data_buf
[20] = "";
2065 char cdb_buf
[70] = "";
2067 if (!(qc
->flags
& ATA_QCFLAG_FAILED
) ||
2068 qc
->dev
->link
!= link
|| !qc
->err_mask
)
2071 if (qc
->dma_dir
!= DMA_NONE
) {
2072 static const char *dma_str
[] = {
2073 [DMA_BIDIRECTIONAL
] = "bidi",
2074 [DMA_TO_DEVICE
] = "out",
2075 [DMA_FROM_DEVICE
] = "in",
2077 static const char *prot_str
[] = {
2078 [ATA_PROT_PIO
] = "pio",
2079 [ATA_PROT_DMA
] = "dma",
2080 [ATA_PROT_NCQ
] = "ncq",
2081 [ATAPI_PROT_PIO
] = "pio",
2082 [ATAPI_PROT_DMA
] = "dma",
2085 snprintf(data_buf
, sizeof(data_buf
), " %s %u %s",
2086 prot_str
[qc
->tf
.protocol
], qc
->nbytes
,
2087 dma_str
[qc
->dma_dir
]);
2090 if (ata_is_atapi(qc
->tf
.protocol
))
2091 snprintf(cdb_buf
, sizeof(cdb_buf
),
2092 "cdb %02x %02x %02x %02x %02x %02x %02x %02x "
2093 "%02x %02x %02x %02x %02x %02x %02x %02x\n ",
2094 cdb
[0], cdb
[1], cdb
[2], cdb
[3],
2095 cdb
[4], cdb
[5], cdb
[6], cdb
[7],
2096 cdb
[8], cdb
[9], cdb
[10], cdb
[11],
2097 cdb
[12], cdb
[13], cdb
[14], cdb
[15]);
2099 ata_dev_printk(qc
->dev
, KERN_ERR
,
2100 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
2102 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
2103 "Emask 0x%x (%s)%s\n",
2104 cmd
->command
, cmd
->feature
, cmd
->nsect
,
2105 cmd
->lbal
, cmd
->lbam
, cmd
->lbah
,
2106 cmd
->hob_feature
, cmd
->hob_nsect
,
2107 cmd
->hob_lbal
, cmd
->hob_lbam
, cmd
->hob_lbah
,
2108 cmd
->device
, qc
->tag
, data_buf
, cdb_buf
,
2109 res
->command
, res
->feature
, res
->nsect
,
2110 res
->lbal
, res
->lbam
, res
->lbah
,
2111 res
->hob_feature
, res
->hob_nsect
,
2112 res
->hob_lbal
, res
->hob_lbam
, res
->hob_lbah
,
2113 res
->device
, qc
->err_mask
, ata_err_string(qc
->err_mask
),
2114 qc
->err_mask
& AC_ERR_NCQ
? " <F>" : "");
2116 if (res
->command
& (ATA_BUSY
| ATA_DRDY
| ATA_DF
| ATA_DRQ
|
2118 if (res
->command
& ATA_BUSY
)
2119 ata_dev_printk(qc
->dev
, KERN_ERR
,
2120 "status: { Busy }\n");
2122 ata_dev_printk(qc
->dev
, KERN_ERR
,
2123 "status: { %s%s%s%s}\n",
2124 res
->command
& ATA_DRDY
? "DRDY " : "",
2125 res
->command
& ATA_DF
? "DF " : "",
2126 res
->command
& ATA_DRQ
? "DRQ " : "",
2127 res
->command
& ATA_ERR
? "ERR " : "");
2130 if (cmd
->command
!= ATA_CMD_PACKET
&&
2131 (res
->feature
& (ATA_ICRC
| ATA_UNC
| ATA_IDNF
|
2133 ata_dev_printk(qc
->dev
, KERN_ERR
,
2134 "error: { %s%s%s%s}\n",
2135 res
->feature
& ATA_ICRC
? "ICRC " : "",
2136 res
->feature
& ATA_UNC
? "UNC " : "",
2137 res
->feature
& ATA_IDNF
? "IDNF " : "",
2138 res
->feature
& ATA_ABORTED
? "ABRT " : "");
2143 * ata_eh_report - report error handling to user
2144 * @ap: ATA port to report EH about
2146 * Report EH to user.
2151 void ata_eh_report(struct ata_port
*ap
)
2153 struct ata_link
*link
;
2155 __ata_port_for_each_link(link
, ap
)
2156 ata_eh_link_report(link
);
2159 static int ata_do_reset(struct ata_link
*link
, ata_reset_fn_t reset
,
2160 unsigned int *classes
, unsigned long deadline
)
2162 struct ata_device
*dev
;
2164 ata_link_for_each_dev(dev
, link
)
2165 classes
[dev
->devno
] = ATA_DEV_UNKNOWN
;
2167 return reset(link
, classes
, deadline
);
2170 static int ata_eh_followup_srst_needed(struct ata_link
*link
,
2171 int rc
, const unsigned int *classes
)
2173 if ((link
->flags
& ATA_LFLAG_NO_SRST
) || ata_link_offline(link
))
2177 if (sata_pmp_supported(link
->ap
) && ata_is_host_link(link
))
2182 int ata_eh_reset(struct ata_link
*link
, int classify
,
2183 ata_prereset_fn_t prereset
, ata_reset_fn_t softreset
,
2184 ata_reset_fn_t hardreset
, ata_postreset_fn_t postreset
)
2186 struct ata_port
*ap
= link
->ap
;
2187 struct ata_eh_context
*ehc
= &link
->eh_context
;
2188 unsigned int *classes
= ehc
->classes
;
2189 unsigned int lflags
= link
->flags
;
2190 int verbose
= !(ehc
->i
.flags
& ATA_EHI_QUIET
);
2191 int max_tries
= 0, try = 0;
2192 struct ata_device
*dev
;
2193 unsigned long deadline
, now
;
2194 ata_reset_fn_t reset
;
2195 unsigned long flags
;
2202 while (ata_eh_reset_timeouts
[max_tries
] != ULONG_MAX
)
2204 if (link
->flags
& ATA_LFLAG_NO_HRST
)
2206 if (link
->flags
& ATA_LFLAG_NO_SRST
)
2209 /* make sure each reset attemp is at least COOL_DOWN apart */
2210 if (ehc
->i
.flags
& ATA_EHI_DID_RESET
) {
2212 WARN_ON(time_after(ehc
->last_reset
, now
));
2213 deadline
= ata_deadline(ehc
->last_reset
,
2214 ATA_EH_RESET_COOL_DOWN
);
2215 if (time_before(now
, deadline
))
2216 schedule_timeout_uninterruptible(deadline
- now
);
2219 spin_lock_irqsave(ap
->lock
, flags
);
2220 ap
->pflags
|= ATA_PFLAG_RESETTING
;
2221 spin_unlock_irqrestore(ap
->lock
, flags
);
2223 ata_eh_about_to_do(link
, NULL
, ATA_EH_RESET
);
2225 ata_link_for_each_dev(dev
, link
) {
2226 /* If we issue an SRST then an ATA drive (not ATAPI)
2227 * may change configuration and be in PIO0 timing. If
2228 * we do a hard reset (or are coming from power on)
2229 * this is true for ATA or ATAPI. Until we've set a
2230 * suitable controller mode we should not touch the
2231 * bus as we may be talking too fast.
2233 dev
->pio_mode
= XFER_PIO_0
;
2235 /* If the controller has a pio mode setup function
2236 * then use it to set the chipset to rights. Don't
2237 * touch the DMA setup as that will be dealt with when
2238 * configuring devices.
2240 if (ap
->ops
->set_piomode
)
2241 ap
->ops
->set_piomode(ap
, dev
);
2244 /* prefer hardreset */
2246 ehc
->i
.action
&= ~ATA_EH_RESET
;
2249 ehc
->i
.action
|= ATA_EH_HARDRESET
;
2250 } else if (softreset
) {
2252 ehc
->i
.action
|= ATA_EH_SOFTRESET
;
2257 ata_deadline(jiffies
, ATA_EH_PRERESET_TIMEOUT
));
2259 if (rc
== -ENOENT
) {
2260 ata_link_printk(link
, KERN_DEBUG
,
2261 "port disabled. ignoring.\n");
2262 ehc
->i
.action
&= ~ATA_EH_RESET
;
2264 ata_link_for_each_dev(dev
, link
)
2265 classes
[dev
->devno
] = ATA_DEV_NONE
;
2269 ata_link_printk(link
, KERN_ERR
,
2270 "prereset failed (errno=%d)\n", rc
);
2274 /* prereset() might have cleared ATA_EH_RESET. If so,
2275 * bang classes and return.
2277 if (reset
&& !(ehc
->i
.action
& ATA_EH_RESET
)) {
2278 ata_link_for_each_dev(dev
, link
)
2279 classes
[dev
->devno
] = ATA_DEV_NONE
;
2289 if (ata_is_host_link(link
))
2290 ata_eh_freeze_port(ap
);
2292 deadline
= ata_deadline(jiffies
, ata_eh_reset_timeouts
[try++]);
2296 ata_link_printk(link
, KERN_INFO
, "%s resetting link\n",
2297 reset
== softreset
? "soft" : "hard");
2299 /* mark that this EH session started with reset */
2300 ehc
->last_reset
= jiffies
;
2301 if (reset
== hardreset
)
2302 ehc
->i
.flags
|= ATA_EHI_DID_HARDRESET
;
2304 ehc
->i
.flags
|= ATA_EHI_DID_SOFTRESET
;
2306 rc
= ata_do_reset(link
, reset
, classes
, deadline
);
2307 if (rc
&& rc
!= -EAGAIN
)
2310 if (reset
== hardreset
&&
2311 ata_eh_followup_srst_needed(link
, rc
, classes
)) {
2312 /* okay, let's do follow-up softreset */
2316 ata_link_printk(link
, KERN_ERR
,
2317 "follow-up softreset required "
2318 "but no softreset avaliable\n");
2323 ata_eh_about_to_do(link
, NULL
, ATA_EH_RESET
);
2324 rc
= ata_do_reset(link
, reset
, classes
, deadline
);
2328 ata_link_printk(link
, KERN_INFO
, "no reset method "
2329 "available, skipping reset\n");
2330 if (!(lflags
& ATA_LFLAG_ASSUME_CLASS
))
2331 lflags
|= ATA_LFLAG_ASSUME_ATA
;
2335 * Post-reset processing
2337 ata_link_for_each_dev(dev
, link
) {
2338 /* After the reset, the device state is PIO 0 and the
2339 * controller state is undefined. Reset also wakes up
2340 * drives from sleeping mode.
2342 dev
->pio_mode
= XFER_PIO_0
;
2343 dev
->flags
&= ~ATA_DFLAG_SLEEPING
;
2345 if (ata_link_offline(link
))
2348 /* apply class override */
2349 if (lflags
& ATA_LFLAG_ASSUME_ATA
)
2350 classes
[dev
->devno
] = ATA_DEV_ATA
;
2351 else if (lflags
& ATA_LFLAG_ASSUME_SEMB
)
2352 classes
[dev
->devno
] = ATA_DEV_SEMB_UNSUP
; /* not yet */
2355 /* record current link speed */
2356 if (sata_scr_read(link
, SCR_STATUS
, &sstatus
) == 0)
2357 link
->sata_spd
= (sstatus
>> 4) & 0xf;
2360 if (ata_is_host_link(link
))
2361 ata_eh_thaw_port(ap
);
2363 /* postreset() should clear hardware SError. Although SError
2364 * is cleared during link resume, clearing SError here is
2365 * necessary as some PHYs raise hotplug events after SRST.
2366 * This introduces race condition where hotplug occurs between
2367 * reset and here. This race is mediated by cross checking
2368 * link onlineness and classification result later.
2371 postreset(link
, classes
);
2373 /* clear cached SError */
2374 spin_lock_irqsave(link
->ap
->lock
, flags
);
2375 link
->eh_info
.serror
= 0;
2376 spin_unlock_irqrestore(link
->ap
->lock
, flags
);
2378 /* Make sure onlineness and classification result correspond.
2379 * Hotplug could have happened during reset and some
2380 * controllers fail to wait while a drive is spinning up after
2381 * being hotplugged causing misdetection. By cross checking
2382 * link onlineness and classification result, those conditions
2383 * can be reliably detected and retried.
2386 ata_link_for_each_dev(dev
, link
) {
2387 /* convert all ATA_DEV_UNKNOWN to ATA_DEV_NONE */
2388 if (classes
[dev
->devno
] == ATA_DEV_UNKNOWN
)
2389 classes
[dev
->devno
] = ATA_DEV_NONE
;
2394 if (classify
&& !nr_known
&& ata_link_online(link
)) {
2395 if (try < max_tries
) {
2396 ata_link_printk(link
, KERN_WARNING
, "link online but "
2397 "device misclassified, retrying\n");
2401 ata_link_printk(link
, KERN_WARNING
,
2402 "link online but device misclassified, "
2403 "device detection might fail\n");
2406 /* reset successful, schedule revalidation */
2407 ata_eh_done(link
, NULL
, ATA_EH_RESET
);
2408 ehc
->last_reset
= jiffies
; /* update to completion time */
2409 ehc
->i
.action
|= ATA_EH_REVALIDATE
;
2413 /* clear hotplug flag */
2414 ehc
->i
.flags
&= ~ATA_EHI_HOTPLUGGED
;
2416 spin_lock_irqsave(ap
->lock
, flags
);
2417 ap
->pflags
&= ~ATA_PFLAG_RESETTING
;
2418 spin_unlock_irqrestore(ap
->lock
, flags
);
2423 /* if SCR isn't accessible on a fan-out port, PMP needs to be reset */
2424 if (!ata_is_host_link(link
) &&
2425 sata_scr_read(link
, SCR_STATUS
, &sstatus
))
2428 if (rc
== -ERESTART
|| try >= max_tries
)
2432 if (time_before(now
, deadline
)) {
2433 unsigned long delta
= deadline
- now
;
2435 ata_link_printk(link
, KERN_WARNING
,
2436 "reset failed (errno=%d), retrying in %u secs\n",
2437 rc
, DIV_ROUND_UP(jiffies_to_msecs(delta
), 1000));
2440 delta
= schedule_timeout_uninterruptible(delta
);
2443 if (rc
== -EPIPE
|| try == max_tries
- 1)
2444 sata_down_spd_limit(link
);
2450 static int ata_eh_revalidate_and_attach(struct ata_link
*link
,
2451 struct ata_device
**r_failed_dev
)
2453 struct ata_port
*ap
= link
->ap
;
2454 struct ata_eh_context
*ehc
= &link
->eh_context
;
2455 struct ata_device
*dev
;
2456 unsigned int new_mask
= 0;
2457 unsigned long flags
;
2462 /* For PATA drive side cable detection to work, IDENTIFY must
2463 * be done backwards such that PDIAG- is released by the slave
2464 * device before the master device is identified.
2466 ata_link_for_each_dev_reverse(dev
, link
) {
2467 unsigned int action
= ata_eh_dev_action(dev
);
2468 unsigned int readid_flags
= 0;
2470 if (ehc
->i
.flags
& ATA_EHI_DID_RESET
)
2471 readid_flags
|= ATA_READID_POSTRESET
;
2473 if ((action
& ATA_EH_REVALIDATE
) && ata_dev_enabled(dev
)) {
2474 WARN_ON(dev
->class == ATA_DEV_PMP
);
2476 if (ata_link_offline(link
)) {
2481 ata_eh_about_to_do(link
, dev
, ATA_EH_REVALIDATE
);
2482 rc
= ata_dev_revalidate(dev
, ehc
->classes
[dev
->devno
],
2487 ata_eh_done(link
, dev
, ATA_EH_REVALIDATE
);
2489 /* Configuration may have changed, reconfigure
2492 ehc
->i
.flags
|= ATA_EHI_SETMODE
;
2494 /* schedule the scsi_rescan_device() here */
2495 queue_work(ata_aux_wq
, &(ap
->scsi_rescan_task
));
2496 } else if (dev
->class == ATA_DEV_UNKNOWN
&&
2497 ehc
->tries
[dev
->devno
] &&
2498 ata_class_enabled(ehc
->classes
[dev
->devno
])) {
2499 dev
->class = ehc
->classes
[dev
->devno
];
2501 if (dev
->class == ATA_DEV_PMP
)
2502 rc
= sata_pmp_attach(dev
);
2504 rc
= ata_dev_read_id(dev
, &dev
->class,
2505 readid_flags
, dev
->id
);
2508 new_mask
|= 1 << dev
->devno
;
2511 /* IDENTIFY was issued to non-existent
2512 * device. No need to reset. Just
2513 * thaw and kill the device.
2515 ata_eh_thaw_port(ap
);
2516 dev
->class = ATA_DEV_UNKNOWN
;
2519 dev
->class = ATA_DEV_UNKNOWN
;
2525 /* PDIAG- should have been released, ask cable type if post-reset */
2526 if ((ehc
->i
.flags
& ATA_EHI_DID_RESET
) && ata_is_host_link(link
)) {
2527 if (ap
->ops
->cable_detect
)
2528 ap
->cbl
= ap
->ops
->cable_detect(ap
);
2532 /* Configure new devices forward such that user doesn't see
2533 * device detection messages backwards.
2535 ata_link_for_each_dev(dev
, link
) {
2536 if (!(new_mask
& (1 << dev
->devno
)) ||
2537 dev
->class == ATA_DEV_PMP
)
2540 ehc
->i
.flags
|= ATA_EHI_PRINTINFO
;
2541 rc
= ata_dev_configure(dev
);
2542 ehc
->i
.flags
&= ~ATA_EHI_PRINTINFO
;
2546 spin_lock_irqsave(ap
->lock
, flags
);
2547 ap
->pflags
|= ATA_PFLAG_SCSI_HOTPLUG
;
2548 spin_unlock_irqrestore(ap
->lock
, flags
);
2550 /* new device discovered, configure xfermode */
2551 ehc
->i
.flags
|= ATA_EHI_SETMODE
;
2557 *r_failed_dev
= dev
;
2558 DPRINTK("EXIT rc=%d\n", rc
);
2563 * ata_set_mode - Program timings and issue SET FEATURES - XFER
2564 * @link: link on which timings will be programmed
2565 * @r_failed_dev: out paramter for failed device
2567 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
2568 * ata_set_mode() fails, pointer to the failing device is
2569 * returned in @r_failed_dev.
2572 * PCI/etc. bus probe sem.
2575 * 0 on success, negative errno otherwise
2577 int ata_set_mode(struct ata_link
*link
, struct ata_device
**r_failed_dev
)
2579 struct ata_port
*ap
= link
->ap
;
2580 struct ata_device
*dev
;
2583 /* if data transfer is verified, clear DUBIOUS_XFER on ering top */
2584 ata_link_for_each_dev(dev
, link
) {
2585 if (!(dev
->flags
& ATA_DFLAG_DUBIOUS_XFER
)) {
2586 struct ata_ering_entry
*ent
;
2588 ent
= ata_ering_top(&dev
->ering
);
2590 ent
->eflags
&= ~ATA_EFLAG_DUBIOUS_XFER
;
2594 /* has private set_mode? */
2595 if (ap
->ops
->set_mode
)
2596 rc
= ap
->ops
->set_mode(link
, r_failed_dev
);
2598 rc
= ata_do_set_mode(link
, r_failed_dev
);
2600 /* if transfer mode has changed, set DUBIOUS_XFER on device */
2601 ata_link_for_each_dev(dev
, link
) {
2602 struct ata_eh_context
*ehc
= &link
->eh_context
;
2603 u8 saved_xfer_mode
= ehc
->saved_xfer_mode
[dev
->devno
];
2604 u8 saved_ncq
= !!(ehc
->saved_ncq_enabled
& (1 << dev
->devno
));
2606 if (dev
->xfer_mode
!= saved_xfer_mode
||
2607 ata_ncq_enabled(dev
) != saved_ncq
)
2608 dev
->flags
|= ATA_DFLAG_DUBIOUS_XFER
;
2614 static int ata_link_nr_enabled(struct ata_link
*link
)
2616 struct ata_device
*dev
;
2619 ata_link_for_each_dev(dev
, link
)
2620 if (ata_dev_enabled(dev
))
2625 static int ata_link_nr_vacant(struct ata_link
*link
)
2627 struct ata_device
*dev
;
2630 ata_link_for_each_dev(dev
, link
)
2631 if (dev
->class == ATA_DEV_UNKNOWN
)
2636 static int ata_eh_skip_recovery(struct ata_link
*link
)
2638 struct ata_port
*ap
= link
->ap
;
2639 struct ata_eh_context
*ehc
= &link
->eh_context
;
2640 struct ata_device
*dev
;
2642 /* skip disabled links */
2643 if (link
->flags
& ATA_LFLAG_DISABLED
)
2646 /* thaw frozen port and recover failed devices */
2647 if ((ap
->pflags
& ATA_PFLAG_FROZEN
) || ata_link_nr_enabled(link
))
2650 /* reset at least once if reset is requested */
2651 if ((ehc
->i
.action
& ATA_EH_RESET
) &&
2652 !(ehc
->i
.flags
& ATA_EHI_DID_RESET
))
2655 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
2656 ata_link_for_each_dev(dev
, link
) {
2657 if (dev
->class == ATA_DEV_UNKNOWN
&&
2658 ehc
->classes
[dev
->devno
] != ATA_DEV_NONE
)
2665 static int ata_eh_schedule_probe(struct ata_device
*dev
)
2667 struct ata_eh_context
*ehc
= &dev
->link
->eh_context
;
2669 if (!(ehc
->i
.probe_mask
& (1 << dev
->devno
)) ||
2670 (ehc
->did_probe_mask
& (1 << dev
->devno
)))
2673 ata_eh_detach_dev(dev
);
2675 ehc
->did_probe_mask
|= (1 << dev
->devno
);
2676 ehc
->i
.action
|= ATA_EH_RESET
;
2677 ehc
->saved_xfer_mode
[dev
->devno
] = 0;
2678 ehc
->saved_ncq_enabled
&= ~(1 << dev
->devno
);
2683 static int ata_eh_handle_dev_fail(struct ata_device
*dev
, int err
)
2685 struct ata_eh_context
*ehc
= &dev
->link
->eh_context
;
2687 ehc
->tries
[dev
->devno
]--;
2691 /* device missing or wrong IDENTIFY data, schedule probing */
2692 ehc
->i
.probe_mask
|= (1 << dev
->devno
);
2694 /* give it just one more chance */
2695 ehc
->tries
[dev
->devno
] = min(ehc
->tries
[dev
->devno
], 1);
2697 if (ehc
->tries
[dev
->devno
] == 1 && dev
->pio_mode
> XFER_PIO_0
) {
2698 /* This is the last chance, better to slow
2699 * down than lose it.
2701 sata_down_spd_limit(dev
->link
);
2702 ata_down_xfermask_limit(dev
, ATA_DNXFER_PIO
);
2706 if (ata_dev_enabled(dev
) && !ehc
->tries
[dev
->devno
]) {
2707 /* disable device if it has used up all its chances */
2708 ata_dev_disable(dev
);
2710 /* detach if offline */
2711 if (ata_link_offline(dev
->link
))
2712 ata_eh_detach_dev(dev
);
2714 /* schedule probe if necessary */
2715 if (ata_eh_schedule_probe(dev
)) {
2716 ehc
->tries
[dev
->devno
] = ATA_EH_DEV_TRIES
;
2717 memset(ehc
->cmd_timeout_idx
[dev
->devno
], 0,
2718 sizeof(ehc
->cmd_timeout_idx
[dev
->devno
]));
2723 ehc
->i
.action
|= ATA_EH_RESET
;
2729 * ata_eh_recover - recover host port after error
2730 * @ap: host port to recover
2731 * @prereset: prereset method (can be NULL)
2732 * @softreset: softreset method (can be NULL)
2733 * @hardreset: hardreset method (can be NULL)
2734 * @postreset: postreset method (can be NULL)
2735 * @r_failed_link: out parameter for failed link
2737 * This is the alpha and omega, eum and yang, heart and soul of
2738 * libata exception handling. On entry, actions required to
2739 * recover each link and hotplug requests are recorded in the
2740 * link's eh_context. This function executes all the operations
2741 * with appropriate retrials and fallbacks to resurrect failed
2742 * devices, detach goners and greet newcomers.
2745 * Kernel thread context (may sleep).
2748 * 0 on success, -errno on failure.
2750 int ata_eh_recover(struct ata_port
*ap
, ata_prereset_fn_t prereset
,
2751 ata_reset_fn_t softreset
, ata_reset_fn_t hardreset
,
2752 ata_postreset_fn_t postreset
,
2753 struct ata_link
**r_failed_link
)
2755 struct ata_link
*link
;
2756 struct ata_device
*dev
;
2759 unsigned long flags
;
2763 /* prep for recovery */
2764 ata_port_for_each_link(link
, ap
) {
2765 struct ata_eh_context
*ehc
= &link
->eh_context
;
2767 /* re-enable link? */
2768 if (ehc
->i
.action
& ATA_EH_ENABLE_LINK
) {
2769 ata_eh_about_to_do(link
, NULL
, ATA_EH_ENABLE_LINK
);
2770 spin_lock_irqsave(ap
->lock
, flags
);
2771 link
->flags
&= ~ATA_LFLAG_DISABLED
;
2772 spin_unlock_irqrestore(ap
->lock
, flags
);
2773 ata_eh_done(link
, NULL
, ATA_EH_ENABLE_LINK
);
2776 ata_link_for_each_dev(dev
, link
) {
2777 if (link
->flags
& ATA_LFLAG_NO_RETRY
)
2778 ehc
->tries
[dev
->devno
] = 1;
2780 ehc
->tries
[dev
->devno
] = ATA_EH_DEV_TRIES
;
2782 /* collect port action mask recorded in dev actions */
2783 ehc
->i
.action
|= ehc
->i
.dev_action
[dev
->devno
] &
2784 ~ATA_EH_PERDEV_MASK
;
2785 ehc
->i
.dev_action
[dev
->devno
] &= ATA_EH_PERDEV_MASK
;
2787 /* process hotplug request */
2788 if (dev
->flags
& ATA_DFLAG_DETACH
)
2789 ata_eh_detach_dev(dev
);
2791 /* schedule probe if necessary */
2792 if (!ata_dev_enabled(dev
))
2793 ata_eh_schedule_probe(dev
);
2801 /* if UNLOADING, finish immediately */
2802 if (ap
->pflags
& ATA_PFLAG_UNLOADING
)
2806 ata_port_for_each_link(link
, ap
) {
2807 struct ata_eh_context
*ehc
= &link
->eh_context
;
2809 /* skip EH if possible. */
2810 if (ata_eh_skip_recovery(link
))
2813 ata_link_for_each_dev(dev
, link
)
2814 ehc
->classes
[dev
->devno
] = ATA_DEV_UNKNOWN
;
2818 ata_port_for_each_link(link
, ap
) {
2819 struct ata_eh_context
*ehc
= &link
->eh_context
;
2821 if (!(ehc
->i
.action
& ATA_EH_RESET
))
2824 rc
= ata_eh_reset(link
, ata_link_nr_vacant(link
),
2825 prereset
, softreset
, hardreset
, postreset
);
2827 ata_link_printk(link
, KERN_ERR
,
2828 "reset failed, giving up\n");
2834 ata_port_for_each_link(link
, ap
) {
2835 struct ata_eh_context
*ehc
= &link
->eh_context
;
2837 /* revalidate existing devices and attach new ones */
2838 rc
= ata_eh_revalidate_and_attach(link
, &dev
);
2842 /* if PMP got attached, return, pmp EH will take care of it */
2843 if (link
->device
->class == ATA_DEV_PMP
) {
2848 /* configure transfer mode if necessary */
2849 if (ehc
->i
.flags
& ATA_EHI_SETMODE
) {
2850 rc
= ata_set_mode(link
, &dev
);
2853 ehc
->i
.flags
&= ~ATA_EHI_SETMODE
;
2856 if (ehc
->i
.action
& ATA_EH_LPM
)
2857 ata_link_for_each_dev(dev
, link
)
2858 ata_dev_enable_pm(dev
, ap
->pm_policy
);
2860 /* this link is okay now */
2866 ata_eh_handle_dev_fail(dev
, rc
);
2868 if (ap
->pflags
& ATA_PFLAG_FROZEN
) {
2869 /* PMP reset requires working host port.
2870 * Can't retry if it's frozen.
2872 if (sata_pmp_attached(ap
))
2882 if (rc
&& r_failed_link
)
2883 *r_failed_link
= link
;
2885 DPRINTK("EXIT, rc=%d\n", rc
);
2890 * ata_eh_finish - finish up EH
2891 * @ap: host port to finish EH for
2893 * Recovery is complete. Clean up EH states and retry or finish
2899 void ata_eh_finish(struct ata_port
*ap
)
2903 /* retry or finish qcs */
2904 for (tag
= 0; tag
< ATA_MAX_QUEUE
; tag
++) {
2905 struct ata_queued_cmd
*qc
= __ata_qc_from_tag(ap
, tag
);
2907 if (!(qc
->flags
& ATA_QCFLAG_FAILED
))
2911 /* FIXME: Once EH migration is complete,
2912 * generate sense data in this function,
2913 * considering both err_mask and tf.
2915 if (qc
->flags
& ATA_QCFLAG_RETRY
)
2916 ata_eh_qc_retry(qc
);
2918 ata_eh_qc_complete(qc
);
2920 if (qc
->flags
& ATA_QCFLAG_SENSE_VALID
) {
2921 ata_eh_qc_complete(qc
);
2923 /* feed zero TF to sense generation */
2924 memset(&qc
->result_tf
, 0, sizeof(qc
->result_tf
));
2925 ata_eh_qc_retry(qc
);
2930 /* make sure nr_active_links is zero after EH */
2931 WARN_ON(ap
->nr_active_links
);
2932 ap
->nr_active_links
= 0;
2936 * ata_do_eh - do standard error handling
2937 * @ap: host port to handle error for
2939 * @prereset: prereset method (can be NULL)
2940 * @softreset: softreset method (can be NULL)
2941 * @hardreset: hardreset method (can be NULL)
2942 * @postreset: postreset method (can be NULL)
2944 * Perform standard error handling sequence.
2947 * Kernel thread context (may sleep).
2949 void ata_do_eh(struct ata_port
*ap
, ata_prereset_fn_t prereset
,
2950 ata_reset_fn_t softreset
, ata_reset_fn_t hardreset
,
2951 ata_postreset_fn_t postreset
)
2953 struct ata_device
*dev
;
2959 rc
= ata_eh_recover(ap
, prereset
, softreset
, hardreset
, postreset
,
2962 ata_link_for_each_dev(dev
, &ap
->link
)
2963 ata_dev_disable(dev
);
2970 * ata_std_error_handler - standard error handler
2971 * @ap: host port to handle error for
2973 * Standard error handler
2976 * Kernel thread context (may sleep).
2978 void ata_std_error_handler(struct ata_port
*ap
)
2980 struct ata_port_operations
*ops
= ap
->ops
;
2981 ata_reset_fn_t hardreset
= ops
->hardreset
;
2983 /* ignore built-in hardreset if SCR access is not available */
2984 if (ata_is_builtin_hardreset(hardreset
) && !sata_scr_valid(&ap
->link
))
2987 ata_do_eh(ap
, ops
->prereset
, ops
->softreset
, hardreset
, ops
->postreset
);
2992 * ata_eh_handle_port_suspend - perform port suspend operation
2993 * @ap: port to suspend
2998 * Kernel thread context (may sleep).
3000 static void ata_eh_handle_port_suspend(struct ata_port
*ap
)
3002 unsigned long flags
;
3005 /* are we suspending? */
3006 spin_lock_irqsave(ap
->lock
, flags
);
3007 if (!(ap
->pflags
& ATA_PFLAG_PM_PENDING
) ||
3008 ap
->pm_mesg
.event
== PM_EVENT_ON
) {
3009 spin_unlock_irqrestore(ap
->lock
, flags
);
3012 spin_unlock_irqrestore(ap
->lock
, flags
);
3014 WARN_ON(ap
->pflags
& ATA_PFLAG_SUSPENDED
);
3016 /* tell ACPI we're suspending */
3017 rc
= ata_acpi_on_suspend(ap
);
3022 ata_eh_freeze_port(ap
);
3024 if (ap
->ops
->port_suspend
)
3025 rc
= ap
->ops
->port_suspend(ap
, ap
->pm_mesg
);
3027 ata_acpi_set_state(ap
, PMSG_SUSPEND
);
3030 spin_lock_irqsave(ap
->lock
, flags
);
3032 ap
->pflags
&= ~ATA_PFLAG_PM_PENDING
;
3034 ap
->pflags
|= ATA_PFLAG_SUSPENDED
;
3035 else if (ap
->pflags
& ATA_PFLAG_FROZEN
)
3036 ata_port_schedule_eh(ap
);
3038 if (ap
->pm_result
) {
3039 *ap
->pm_result
= rc
;
3040 ap
->pm_result
= NULL
;
3043 spin_unlock_irqrestore(ap
->lock
, flags
);
3049 * ata_eh_handle_port_resume - perform port resume operation
3050 * @ap: port to resume
3055 * Kernel thread context (may sleep).
3057 static void ata_eh_handle_port_resume(struct ata_port
*ap
)
3059 unsigned long flags
;
3062 /* are we resuming? */
3063 spin_lock_irqsave(ap
->lock
, flags
);
3064 if (!(ap
->pflags
& ATA_PFLAG_PM_PENDING
) ||
3065 ap
->pm_mesg
.event
!= PM_EVENT_ON
) {
3066 spin_unlock_irqrestore(ap
->lock
, flags
);
3069 spin_unlock_irqrestore(ap
->lock
, flags
);
3071 WARN_ON(!(ap
->pflags
& ATA_PFLAG_SUSPENDED
));
3073 ata_acpi_set_state(ap
, PMSG_ON
);
3075 if (ap
->ops
->port_resume
)
3076 rc
= ap
->ops
->port_resume(ap
);
3078 /* tell ACPI that we're resuming */
3079 ata_acpi_on_resume(ap
);
3082 spin_lock_irqsave(ap
->lock
, flags
);
3083 ap
->pflags
&= ~(ATA_PFLAG_PM_PENDING
| ATA_PFLAG_SUSPENDED
);
3084 if (ap
->pm_result
) {
3085 *ap
->pm_result
= rc
;
3086 ap
->pm_result
= NULL
;
3088 spin_unlock_irqrestore(ap
->lock
, flags
);
3090 #endif /* CONFIG_PM */