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 ATA_EH_SPDN_NCQ_OFF
= (1 << 0),
50 ATA_EH_SPDN_SPEED_DOWN
= (1 << 1),
51 ATA_EH_SPDN_FALLBACK_TO_PIO
= (1 << 2),
54 /* Waiting in ->prereset can never be reliable. It's sometimes nice
55 * to wait there but it can't be depended upon; otherwise, we wouldn't
56 * be resetting. Just give it enough time for most drives to spin up.
59 ATA_EH_PRERESET_TIMEOUT
= 10 * HZ
,
60 ATA_EH_FASTDRAIN_INTERVAL
= 3 * HZ
,
63 /* The following table determines how we sequence resets. Each entry
64 * represents timeout for that try. The first try can be soft or
65 * hardreset. All others are hardreset if available. In most cases
66 * the first reset w/ 10sec timeout should succeed. Following entries
67 * are mostly for error handling, hotplug and retarded devices.
69 static const unsigned long ata_eh_reset_timeouts
[] = {
70 10 * HZ
, /* most drives spin up by 10sec */
71 10 * HZ
, /* > 99% working drives spin up before 20sec */
72 35 * HZ
, /* give > 30 secs of idleness for retarded devices */
73 5 * HZ
, /* and sweet one last chance */
74 /* > 1 min has elapsed, give up */
77 static void __ata_port_freeze(struct ata_port
*ap
);
79 static void ata_eh_handle_port_suspend(struct ata_port
*ap
);
80 static void ata_eh_handle_port_resume(struct ata_port
*ap
);
82 static void ata_eh_handle_port_suspend(struct ata_port
*ap
)
85 static void ata_eh_handle_port_resume(struct ata_port
*ap
)
87 #endif /* CONFIG_PM */
89 static void __ata_ehi_pushv_desc(struct ata_eh_info
*ehi
, const char *fmt
,
92 ehi
->desc_len
+= vscnprintf(ehi
->desc
+ ehi
->desc_len
,
93 ATA_EH_DESC_LEN
- ehi
->desc_len
,
98 * __ata_ehi_push_desc - push error description without adding separator
100 * @fmt: printf format string
102 * Format string according to @fmt and append it to @ehi->desc.
105 * spin_lock_irqsave(host lock)
107 void __ata_ehi_push_desc(struct ata_eh_info
*ehi
, const char *fmt
, ...)
112 __ata_ehi_pushv_desc(ehi
, fmt
, args
);
117 * ata_ehi_push_desc - push error description with separator
119 * @fmt: printf format string
121 * Format string according to @fmt and append it to @ehi->desc.
122 * If @ehi->desc is not empty, ", " is added in-between.
125 * spin_lock_irqsave(host lock)
127 void ata_ehi_push_desc(struct ata_eh_info
*ehi
, const char *fmt
, ...)
132 __ata_ehi_push_desc(ehi
, ", ");
135 __ata_ehi_pushv_desc(ehi
, fmt
, args
);
140 * ata_ehi_clear_desc - clean error description
146 * spin_lock_irqsave(host lock)
148 void ata_ehi_clear_desc(struct ata_eh_info
*ehi
)
155 * ata_port_desc - append port description
156 * @ap: target ATA port
157 * @fmt: printf format string
159 * Format string according to @fmt and append it to port
160 * description. If port description is not empty, " " is added
161 * in-between. This function is to be used while initializing
162 * ata_host. The description is printed on host registration.
167 void ata_port_desc(struct ata_port
*ap
, const char *fmt
, ...)
171 WARN_ON(!(ap
->pflags
& ATA_PFLAG_INITIALIZING
));
173 if (ap
->link
.eh_info
.desc_len
)
174 __ata_ehi_push_desc(&ap
->link
.eh_info
, " ");
177 __ata_ehi_pushv_desc(&ap
->link
.eh_info
, fmt
, args
);
184 * ata_port_pbar_desc - append PCI BAR description
185 * @ap: target ATA port
186 * @bar: target PCI BAR
187 * @offset: offset into PCI BAR
188 * @name: name of the area
190 * If @offset is negative, this function formats a string which
191 * contains the name, address, size and type of the BAR and
192 * appends it to the port description. If @offset is zero or
193 * positive, only name and offsetted address is appended.
198 void ata_port_pbar_desc(struct ata_port
*ap
, int bar
, ssize_t offset
,
201 struct pci_dev
*pdev
= to_pci_dev(ap
->host
->dev
);
203 unsigned long long start
, len
;
205 if (pci_resource_flags(pdev
, bar
) & IORESOURCE_MEM
)
207 else if (pci_resource_flags(pdev
, bar
) & IORESOURCE_IO
)
210 start
= (unsigned long long)pci_resource_start(pdev
, bar
);
211 len
= (unsigned long long)pci_resource_len(pdev
, bar
);
214 ata_port_desc(ap
, "%s %s%llu@0x%llx", name
, type
, len
, start
);
216 ata_port_desc(ap
, "%s 0x%llx", name
, start
+ offset
);
219 #endif /* CONFIG_PCI */
221 static void ata_ering_record(struct ata_ering
*ering
, int is_io
,
222 unsigned int err_mask
)
224 struct ata_ering_entry
*ent
;
229 ering
->cursor
%= ATA_ERING_SIZE
;
231 ent
= &ering
->ring
[ering
->cursor
];
233 ent
->err_mask
= err_mask
;
234 ent
->timestamp
= get_jiffies_64();
237 static void ata_ering_clear(struct ata_ering
*ering
)
239 memset(ering
, 0, sizeof(*ering
));
242 static int ata_ering_map(struct ata_ering
*ering
,
243 int (*map_fn
)(struct ata_ering_entry
*, void *),
247 struct ata_ering_entry
*ent
;
251 ent
= &ering
->ring
[idx
];
254 rc
= map_fn(ent
, arg
);
257 idx
= (idx
- 1 + ATA_ERING_SIZE
) % ATA_ERING_SIZE
;
258 } while (idx
!= ering
->cursor
);
263 static unsigned int ata_eh_dev_action(struct ata_device
*dev
)
265 struct ata_eh_context
*ehc
= &dev
->link
->eh_context
;
267 return ehc
->i
.action
| ehc
->i
.dev_action
[dev
->devno
];
270 static void ata_eh_clear_action(struct ata_link
*link
, struct ata_device
*dev
,
271 struct ata_eh_info
*ehi
, unsigned int action
)
273 struct ata_device
*tdev
;
276 ehi
->action
&= ~action
;
277 ata_link_for_each_dev(tdev
, link
)
278 ehi
->dev_action
[tdev
->devno
] &= ~action
;
280 /* doesn't make sense for port-wide EH actions */
281 WARN_ON(!(action
& ATA_EH_PERDEV_MASK
));
283 /* break ehi->action into ehi->dev_action */
284 if (ehi
->action
& action
) {
285 ata_link_for_each_dev(tdev
, link
)
286 ehi
->dev_action
[tdev
->devno
] |=
287 ehi
->action
& action
;
288 ehi
->action
&= ~action
;
291 /* turn off the specified per-dev action */
292 ehi
->dev_action
[dev
->devno
] &= ~action
;
297 * ata_scsi_timed_out - SCSI layer time out callback
298 * @cmd: timed out SCSI command
300 * Handles SCSI layer timeout. We race with normal completion of
301 * the qc for @cmd. If the qc is already gone, we lose and let
302 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
303 * timed out and EH should be invoked. Prevent ata_qc_complete()
304 * from finishing it by setting EH_SCHEDULED and return
307 * TODO: kill this function once old EH is gone.
310 * Called from timer context
313 * EH_HANDLED or EH_NOT_HANDLED
315 enum scsi_eh_timer_return
ata_scsi_timed_out(struct scsi_cmnd
*cmd
)
317 struct Scsi_Host
*host
= cmd
->device
->host
;
318 struct ata_port
*ap
= ata_shost_to_port(host
);
320 struct ata_queued_cmd
*qc
;
321 enum scsi_eh_timer_return ret
;
325 if (ap
->ops
->error_handler
) {
326 ret
= EH_NOT_HANDLED
;
331 spin_lock_irqsave(ap
->lock
, flags
);
332 qc
= ata_qc_from_tag(ap
, ap
->link
.active_tag
);
334 WARN_ON(qc
->scsicmd
!= cmd
);
335 qc
->flags
|= ATA_QCFLAG_EH_SCHEDULED
;
336 qc
->err_mask
|= AC_ERR_TIMEOUT
;
337 ret
= EH_NOT_HANDLED
;
339 spin_unlock_irqrestore(ap
->lock
, flags
);
342 DPRINTK("EXIT, ret=%d\n", ret
);
347 * ata_scsi_error - SCSI layer error handler callback
348 * @host: SCSI host on which error occurred
350 * Handles SCSI-layer-thrown error events.
353 * Inherited from SCSI layer (none, can sleep)
358 void ata_scsi_error(struct Scsi_Host
*host
)
360 struct ata_port
*ap
= ata_shost_to_port(host
);
366 /* synchronize with port task */
367 ata_port_flush_task(ap
);
369 /* synchronize with host lock and sort out timeouts */
371 /* For new EH, all qcs are finished in one of three ways -
372 * normal completion, error completion, and SCSI timeout.
373 * Both cmpletions can race against SCSI timeout. When normal
374 * completion wins, the qc never reaches EH. When error
375 * completion wins, the qc has ATA_QCFLAG_FAILED set.
377 * When SCSI timeout wins, things are a bit more complex.
378 * Normal or error completion can occur after the timeout but
379 * before this point. In such cases, both types of
380 * completions are honored. A scmd is determined to have
381 * timed out iff its associated qc is active and not failed.
383 if (ap
->ops
->error_handler
) {
384 struct scsi_cmnd
*scmd
, *tmp
;
387 spin_lock_irqsave(ap
->lock
, flags
);
389 list_for_each_entry_safe(scmd
, tmp
, &host
->eh_cmd_q
, eh_entry
) {
390 struct ata_queued_cmd
*qc
;
392 for (i
= 0; i
< ATA_MAX_QUEUE
; i
++) {
393 qc
= __ata_qc_from_tag(ap
, i
);
394 if (qc
->flags
& ATA_QCFLAG_ACTIVE
&&
399 if (i
< ATA_MAX_QUEUE
) {
400 /* the scmd has an associated qc */
401 if (!(qc
->flags
& ATA_QCFLAG_FAILED
)) {
402 /* which hasn't failed yet, timeout */
403 qc
->err_mask
|= AC_ERR_TIMEOUT
;
404 qc
->flags
|= ATA_QCFLAG_FAILED
;
408 /* Normal completion occurred after
409 * SCSI timeout but before this point.
410 * Successfully complete it.
412 scmd
->retries
= scmd
->allowed
;
413 scsi_eh_finish_cmd(scmd
, &ap
->eh_done_q
);
417 /* If we have timed out qcs. They belong to EH from
418 * this point but the state of the controller is
419 * unknown. Freeze the port to make sure the IRQ
420 * handler doesn't diddle with those qcs. This must
421 * be done atomically w.r.t. setting QCFLAG_FAILED.
424 __ata_port_freeze(ap
);
426 spin_unlock_irqrestore(ap
->lock
, flags
);
428 /* initialize eh_tries */
429 ap
->eh_tries
= ATA_EH_MAX_TRIES
;
431 spin_unlock_wait(ap
->lock
);
434 /* invoke error handler */
435 if (ap
->ops
->error_handler
) {
436 struct ata_link
*link
;
438 /* kill fast drain timer */
439 del_timer_sync(&ap
->fastdrain_timer
);
441 /* process port resume request */
442 ata_eh_handle_port_resume(ap
);
444 /* fetch & clear EH info */
445 spin_lock_irqsave(ap
->lock
, flags
);
447 __ata_port_for_each_link(link
, ap
) {
448 memset(&link
->eh_context
, 0, sizeof(link
->eh_context
));
449 link
->eh_context
.i
= link
->eh_info
;
450 memset(&link
->eh_info
, 0, sizeof(link
->eh_info
));
453 ap
->pflags
|= ATA_PFLAG_EH_IN_PROGRESS
;
454 ap
->pflags
&= ~ATA_PFLAG_EH_PENDING
;
455 ap
->excl_link
= NULL
; /* don't maintain exclusion over EH */
457 spin_unlock_irqrestore(ap
->lock
, flags
);
459 /* invoke EH, skip if unloading or suspended */
460 if (!(ap
->pflags
& (ATA_PFLAG_UNLOADING
| ATA_PFLAG_SUSPENDED
)))
461 ap
->ops
->error_handler(ap
);
465 /* process port suspend request */
466 ata_eh_handle_port_suspend(ap
);
468 /* Exception might have happend after ->error_handler
469 * recovered the port but before this point. Repeat
472 spin_lock_irqsave(ap
->lock
, flags
);
474 if (ap
->pflags
& ATA_PFLAG_EH_PENDING
) {
475 if (--ap
->eh_tries
) {
476 spin_unlock_irqrestore(ap
->lock
, flags
);
479 ata_port_printk(ap
, KERN_ERR
, "EH pending after %d "
480 "tries, giving up\n", ATA_EH_MAX_TRIES
);
481 ap
->pflags
&= ~ATA_PFLAG_EH_PENDING
;
484 /* this run is complete, make sure EH info is clear */
485 __ata_port_for_each_link(link
, ap
)
486 memset(&link
->eh_info
, 0, sizeof(link
->eh_info
));
488 /* Clear host_eh_scheduled while holding ap->lock such
489 * that if exception occurs after this point but
490 * before EH completion, SCSI midlayer will
493 host
->host_eh_scheduled
= 0;
495 spin_unlock_irqrestore(ap
->lock
, flags
);
497 WARN_ON(ata_qc_from_tag(ap
, ap
->link
.active_tag
) == NULL
);
498 ap
->ops
->eng_timeout(ap
);
501 /* finish or retry handled scmd's and clean up */
502 WARN_ON(host
->host_failed
|| !list_empty(&host
->eh_cmd_q
));
504 scsi_eh_flush_done_q(&ap
->eh_done_q
);
507 spin_lock_irqsave(ap
->lock
, flags
);
509 if (ap
->pflags
& ATA_PFLAG_LOADING
)
510 ap
->pflags
&= ~ATA_PFLAG_LOADING
;
511 else if (ap
->pflags
& ATA_PFLAG_SCSI_HOTPLUG
)
512 queue_delayed_work(ata_aux_wq
, &ap
->hotplug_task
, 0);
514 if (ap
->pflags
& ATA_PFLAG_RECOVERED
)
515 ata_port_printk(ap
, KERN_INFO
, "EH complete\n");
517 ap
->pflags
&= ~(ATA_PFLAG_SCSI_HOTPLUG
| ATA_PFLAG_RECOVERED
);
519 /* tell wait_eh that we're done */
520 ap
->pflags
&= ~ATA_PFLAG_EH_IN_PROGRESS
;
521 wake_up_all(&ap
->eh_wait_q
);
523 spin_unlock_irqrestore(ap
->lock
, flags
);
529 * ata_port_wait_eh - Wait for the currently pending EH to complete
530 * @ap: Port to wait EH for
532 * Wait until the currently pending EH is complete.
535 * Kernel thread context (may sleep).
537 void ata_port_wait_eh(struct ata_port
*ap
)
543 spin_lock_irqsave(ap
->lock
, flags
);
545 while (ap
->pflags
& (ATA_PFLAG_EH_PENDING
| ATA_PFLAG_EH_IN_PROGRESS
)) {
546 prepare_to_wait(&ap
->eh_wait_q
, &wait
, TASK_UNINTERRUPTIBLE
);
547 spin_unlock_irqrestore(ap
->lock
, flags
);
549 spin_lock_irqsave(ap
->lock
, flags
);
551 finish_wait(&ap
->eh_wait_q
, &wait
);
553 spin_unlock_irqrestore(ap
->lock
, flags
);
555 /* make sure SCSI EH is complete */
556 if (scsi_host_in_recovery(ap
->scsi_host
)) {
562 static int ata_eh_nr_in_flight(struct ata_port
*ap
)
567 /* count only non-internal commands */
568 for (tag
= 0; tag
< ATA_MAX_QUEUE
- 1; tag
++)
569 if (ata_qc_from_tag(ap
, tag
))
575 void ata_eh_fastdrain_timerfn(unsigned long arg
)
577 struct ata_port
*ap
= (void *)arg
;
581 spin_lock_irqsave(ap
->lock
, flags
);
583 cnt
= ata_eh_nr_in_flight(ap
);
589 if (cnt
== ap
->fastdrain_cnt
) {
592 /* No progress during the last interval, tag all
593 * in-flight qcs as timed out and freeze the port.
595 for (tag
= 0; tag
< ATA_MAX_QUEUE
- 1; tag
++) {
596 struct ata_queued_cmd
*qc
= ata_qc_from_tag(ap
, tag
);
598 qc
->err_mask
|= AC_ERR_TIMEOUT
;
603 /* some qcs have finished, give it another chance */
604 ap
->fastdrain_cnt
= cnt
;
605 ap
->fastdrain_timer
.expires
=
606 jiffies
+ ATA_EH_FASTDRAIN_INTERVAL
;
607 add_timer(&ap
->fastdrain_timer
);
611 spin_unlock_irqrestore(ap
->lock
, flags
);
615 * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
616 * @ap: target ATA port
617 * @fastdrain: activate fast drain
619 * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
620 * is non-zero and EH wasn't pending before. Fast drain ensures
621 * that EH kicks in in timely manner.
624 * spin_lock_irqsave(host lock)
626 static void ata_eh_set_pending(struct ata_port
*ap
, int fastdrain
)
630 /* already scheduled? */
631 if (ap
->pflags
& ATA_PFLAG_EH_PENDING
)
634 ap
->pflags
|= ATA_PFLAG_EH_PENDING
;
639 /* do we have in-flight qcs? */
640 cnt
= ata_eh_nr_in_flight(ap
);
644 /* activate fast drain */
645 ap
->fastdrain_cnt
= cnt
;
646 ap
->fastdrain_timer
.expires
= jiffies
+ ATA_EH_FASTDRAIN_INTERVAL
;
647 add_timer(&ap
->fastdrain_timer
);
651 * ata_qc_schedule_eh - schedule qc for error handling
652 * @qc: command to schedule error handling for
654 * Schedule error handling for @qc. EH will kick in as soon as
655 * other commands are drained.
658 * spin_lock_irqsave(host lock)
660 void ata_qc_schedule_eh(struct ata_queued_cmd
*qc
)
662 struct ata_port
*ap
= qc
->ap
;
664 WARN_ON(!ap
->ops
->error_handler
);
666 qc
->flags
|= ATA_QCFLAG_FAILED
;
667 ata_eh_set_pending(ap
, 1);
669 /* The following will fail if timeout has already expired.
670 * ata_scsi_error() takes care of such scmds on EH entry.
671 * Note that ATA_QCFLAG_FAILED is unconditionally set after
672 * this function completes.
674 scsi_req_abort_cmd(qc
->scsicmd
);
678 * ata_port_schedule_eh - schedule error handling without a qc
679 * @ap: ATA port to schedule EH for
681 * Schedule error handling for @ap. EH will kick in as soon as
682 * all commands are drained.
685 * spin_lock_irqsave(host lock)
687 void ata_port_schedule_eh(struct ata_port
*ap
)
689 WARN_ON(!ap
->ops
->error_handler
);
691 if (ap
->pflags
& ATA_PFLAG_INITIALIZING
)
694 ata_eh_set_pending(ap
, 1);
695 scsi_schedule_eh(ap
->scsi_host
);
697 DPRINTK("port EH scheduled\n");
700 static int ata_do_link_abort(struct ata_port
*ap
, struct ata_link
*link
)
702 int tag
, nr_aborted
= 0;
704 WARN_ON(!ap
->ops
->error_handler
);
706 /* we're gonna abort all commands, no need for fast drain */
707 ata_eh_set_pending(ap
, 0);
709 for (tag
= 0; tag
< ATA_MAX_QUEUE
; tag
++) {
710 struct ata_queued_cmd
*qc
= ata_qc_from_tag(ap
, tag
);
712 if (qc
&& (!link
|| qc
->dev
->link
== link
)) {
713 qc
->flags
|= ATA_QCFLAG_FAILED
;
720 ata_port_schedule_eh(ap
);
726 * ata_link_abort - abort all qc's on the link
727 * @link: ATA link to abort qc's for
729 * Abort all active qc's active on @link and schedule EH.
732 * spin_lock_irqsave(host lock)
735 * Number of aborted qc's.
737 int ata_link_abort(struct ata_link
*link
)
739 return ata_do_link_abort(link
->ap
, link
);
743 * ata_port_abort - abort all qc's on the port
744 * @ap: ATA port to abort qc's for
746 * Abort all active qc's of @ap and schedule EH.
749 * spin_lock_irqsave(host_set lock)
752 * Number of aborted qc's.
754 int ata_port_abort(struct ata_port
*ap
)
756 return ata_do_link_abort(ap
, NULL
);
760 * __ata_port_freeze - freeze port
761 * @ap: ATA port to freeze
763 * This function is called when HSM violation or some other
764 * condition disrupts normal operation of the port. Frozen port
765 * is not allowed to perform any operation until the port is
766 * thawed, which usually follows a successful reset.
768 * ap->ops->freeze() callback can be used for freezing the port
769 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
770 * port cannot be frozen hardware-wise, the interrupt handler
771 * must ack and clear interrupts unconditionally while the port
775 * spin_lock_irqsave(host lock)
777 static void __ata_port_freeze(struct ata_port
*ap
)
779 WARN_ON(!ap
->ops
->error_handler
);
784 ap
->pflags
|= ATA_PFLAG_FROZEN
;
786 DPRINTK("ata%u port frozen\n", ap
->print_id
);
790 * ata_port_freeze - abort & freeze port
791 * @ap: ATA port to freeze
793 * Abort and freeze @ap.
796 * spin_lock_irqsave(host lock)
799 * Number of aborted commands.
801 int ata_port_freeze(struct ata_port
*ap
)
805 WARN_ON(!ap
->ops
->error_handler
);
807 nr_aborted
= ata_port_abort(ap
);
808 __ata_port_freeze(ap
);
814 * sata_async_notification - SATA async notification handler
815 * @ap: ATA port where async notification is received
817 * Handler to be called when async notification via SDB FIS is
818 * received. This function schedules EH if necessary.
821 * spin_lock_irqsave(host lock)
824 * 1 if EH is scheduled, 0 otherwise.
826 int sata_async_notification(struct ata_port
*ap
)
831 if (!(ap
->flags
& ATA_FLAG_AN
))
834 rc
= sata_scr_read(&ap
->link
, SCR_NOTIFICATION
, &sntf
);
836 sata_scr_write(&ap
->link
, SCR_NOTIFICATION
, sntf
);
838 if (!ap
->nr_pmp_links
|| rc
) {
839 /* PMP is not attached or SNTF is not available */
840 if (!ap
->nr_pmp_links
) {
841 /* PMP is not attached. Check whether ATAPI
842 * AN is configured. If so, notify media
845 struct ata_device
*dev
= ap
->link
.device
;
847 if ((dev
->class == ATA_DEV_ATAPI
) &&
848 (dev
->flags
& ATA_DFLAG_AN
))
849 ata_scsi_media_change_notify(dev
);
852 /* PMP is attached but SNTF is not available.
853 * ATAPI async media change notification is
854 * not used. The PMP must be reporting PHY
855 * status change, schedule EH.
857 ata_port_schedule_eh(ap
);
861 /* PMP is attached and SNTF is available */
862 struct ata_link
*link
;
864 /* check and notify ATAPI AN */
865 ata_port_for_each_link(link
, ap
) {
866 if (!(sntf
& (1 << link
->pmp
)))
869 if ((link
->device
->class == ATA_DEV_ATAPI
) &&
870 (link
->device
->flags
& ATA_DFLAG_AN
))
871 ata_scsi_media_change_notify(link
->device
);
874 /* If PMP is reporting that PHY status of some
875 * downstream ports has changed, schedule EH.
877 if (sntf
& (1 << SATA_PMP_CTRL_PORT
)) {
878 ata_port_schedule_eh(ap
);
887 * ata_eh_freeze_port - EH helper to freeze port
888 * @ap: ATA port to freeze
895 void ata_eh_freeze_port(struct ata_port
*ap
)
899 if (!ap
->ops
->error_handler
)
902 spin_lock_irqsave(ap
->lock
, flags
);
903 __ata_port_freeze(ap
);
904 spin_unlock_irqrestore(ap
->lock
, flags
);
908 * ata_port_thaw_port - EH helper to thaw port
909 * @ap: ATA port to thaw
911 * Thaw frozen port @ap.
916 void ata_eh_thaw_port(struct ata_port
*ap
)
920 if (!ap
->ops
->error_handler
)
923 spin_lock_irqsave(ap
->lock
, flags
);
925 ap
->pflags
&= ~ATA_PFLAG_FROZEN
;
930 spin_unlock_irqrestore(ap
->lock
, flags
);
932 DPRINTK("ata%u port thawed\n", ap
->print_id
);
935 static void ata_eh_scsidone(struct scsi_cmnd
*scmd
)
940 static void __ata_eh_qc_complete(struct ata_queued_cmd
*qc
)
942 struct ata_port
*ap
= qc
->ap
;
943 struct scsi_cmnd
*scmd
= qc
->scsicmd
;
946 spin_lock_irqsave(ap
->lock
, flags
);
947 qc
->scsidone
= ata_eh_scsidone
;
948 __ata_qc_complete(qc
);
949 WARN_ON(ata_tag_valid(qc
->tag
));
950 spin_unlock_irqrestore(ap
->lock
, flags
);
952 scsi_eh_finish_cmd(scmd
, &ap
->eh_done_q
);
956 * ata_eh_qc_complete - Complete an active ATA command from EH
957 * @qc: Command to complete
959 * Indicate to the mid and upper layers that an ATA command has
960 * completed. To be used from EH.
962 void ata_eh_qc_complete(struct ata_queued_cmd
*qc
)
964 struct scsi_cmnd
*scmd
= qc
->scsicmd
;
965 scmd
->retries
= scmd
->allowed
;
966 __ata_eh_qc_complete(qc
);
970 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
971 * @qc: Command to retry
973 * Indicate to the mid and upper layers that an ATA command
974 * should be retried. To be used from EH.
976 * SCSI midlayer limits the number of retries to scmd->allowed.
977 * scmd->retries is decremented for commands which get retried
978 * due to unrelated failures (qc->err_mask is zero).
980 void ata_eh_qc_retry(struct ata_queued_cmd
*qc
)
982 struct scsi_cmnd
*scmd
= qc
->scsicmd
;
983 if (!qc
->err_mask
&& scmd
->retries
)
985 __ata_eh_qc_complete(qc
);
989 * ata_eh_detach_dev - detach ATA device
990 * @dev: ATA device to detach
997 void ata_eh_detach_dev(struct ata_device
*dev
)
999 struct ata_link
*link
= dev
->link
;
1000 struct ata_port
*ap
= link
->ap
;
1001 unsigned long flags
;
1003 ata_dev_disable(dev
);
1005 spin_lock_irqsave(ap
->lock
, flags
);
1007 dev
->flags
&= ~ATA_DFLAG_DETACH
;
1009 if (ata_scsi_offline_dev(dev
)) {
1010 dev
->flags
|= ATA_DFLAG_DETACHED
;
1011 ap
->pflags
|= ATA_PFLAG_SCSI_HOTPLUG
;
1014 /* clear per-dev EH actions */
1015 ata_eh_clear_action(link
, dev
, &link
->eh_info
, ATA_EH_PERDEV_MASK
);
1016 ata_eh_clear_action(link
, dev
, &link
->eh_context
.i
, ATA_EH_PERDEV_MASK
);
1018 spin_unlock_irqrestore(ap
->lock
, flags
);
1022 * ata_eh_about_to_do - about to perform eh_action
1023 * @link: target ATA link
1024 * @dev: target ATA dev for per-dev action (can be NULL)
1025 * @action: action about to be performed
1027 * Called just before performing EH actions to clear related bits
1028 * in @link->eh_info such that eh actions are not unnecessarily
1034 void ata_eh_about_to_do(struct ata_link
*link
, struct ata_device
*dev
,
1035 unsigned int action
)
1037 struct ata_port
*ap
= link
->ap
;
1038 struct ata_eh_info
*ehi
= &link
->eh_info
;
1039 struct ata_eh_context
*ehc
= &link
->eh_context
;
1040 unsigned long flags
;
1042 spin_lock_irqsave(ap
->lock
, flags
);
1044 /* Reset is represented by combination of actions and EHI
1045 * flags. Suck in all related bits before clearing eh_info to
1046 * avoid losing requested action.
1048 if (action
& ATA_EH_RESET_MASK
) {
1049 ehc
->i
.action
|= ehi
->action
& ATA_EH_RESET_MASK
;
1050 ehc
->i
.flags
|= ehi
->flags
& ATA_EHI_RESET_MODIFIER_MASK
;
1052 /* make sure all reset actions are cleared & clear EHI flags */
1053 action
|= ATA_EH_RESET_MASK
;
1054 ehi
->flags
&= ~ATA_EHI_RESET_MODIFIER_MASK
;
1057 ata_eh_clear_action(link
, dev
, ehi
, action
);
1059 if (!(ehc
->i
.flags
& ATA_EHI_QUIET
))
1060 ap
->pflags
|= ATA_PFLAG_RECOVERED
;
1062 spin_unlock_irqrestore(ap
->lock
, flags
);
1066 * ata_eh_done - EH action complete
1067 * @ap: target ATA port
1068 * @dev: target ATA dev for per-dev action (can be NULL)
1069 * @action: action just completed
1071 * Called right after performing EH actions to clear related bits
1072 * in @link->eh_context.
1077 void ata_eh_done(struct ata_link
*link
, struct ata_device
*dev
,
1078 unsigned int action
)
1080 struct ata_eh_context
*ehc
= &link
->eh_context
;
1082 /* if reset is complete, clear all reset actions & reset modifier */
1083 if (action
& ATA_EH_RESET_MASK
) {
1084 action
|= ATA_EH_RESET_MASK
;
1085 ehc
->i
.flags
&= ~ATA_EHI_RESET_MODIFIER_MASK
;
1088 ata_eh_clear_action(link
, dev
, &ehc
->i
, action
);
1092 * ata_err_string - convert err_mask to descriptive string
1093 * @err_mask: error mask to convert to string
1095 * Convert @err_mask to descriptive string. Errors are
1096 * prioritized according to severity and only the most severe
1097 * error is reported.
1103 * Descriptive string for @err_mask
1105 static const char *ata_err_string(unsigned int err_mask
)
1107 if (err_mask
& AC_ERR_HOST_BUS
)
1108 return "host bus error";
1109 if (err_mask
& AC_ERR_ATA_BUS
)
1110 return "ATA bus error";
1111 if (err_mask
& AC_ERR_TIMEOUT
)
1113 if (err_mask
& AC_ERR_HSM
)
1114 return "HSM violation";
1115 if (err_mask
& AC_ERR_SYSTEM
)
1116 return "internal error";
1117 if (err_mask
& AC_ERR_MEDIA
)
1118 return "media error";
1119 if (err_mask
& AC_ERR_INVALID
)
1120 return "invalid argument";
1121 if (err_mask
& AC_ERR_DEV
)
1122 return "device error";
1123 return "unknown error";
1127 * ata_read_log_page - read a specific log page
1128 * @dev: target device
1129 * @page: page to read
1130 * @buf: buffer to store read page
1131 * @sectors: number of sectors to read
1133 * Read log page using READ_LOG_EXT command.
1136 * Kernel thread context (may sleep).
1139 * 0 on success, AC_ERR_* mask otherwise.
1141 static unsigned int ata_read_log_page(struct ata_device
*dev
,
1142 u8 page
, void *buf
, unsigned int sectors
)
1144 struct ata_taskfile tf
;
1145 unsigned int err_mask
;
1147 DPRINTK("read log page - page %d\n", page
);
1149 ata_tf_init(dev
, &tf
);
1150 tf
.command
= ATA_CMD_READ_LOG_EXT
;
1153 tf
.hob_nsect
= sectors
>> 8;
1154 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_LBA48
| ATA_TFLAG_DEVICE
;
1155 tf
.protocol
= ATA_PROT_PIO
;
1157 err_mask
= ata_exec_internal(dev
, &tf
, NULL
, DMA_FROM_DEVICE
,
1158 buf
, sectors
* ATA_SECT_SIZE
, 0);
1160 DPRINTK("EXIT, err_mask=%x\n", err_mask
);
1165 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
1166 * @dev: Device to read log page 10h from
1167 * @tag: Resulting tag of the failed command
1168 * @tf: Resulting taskfile registers of the failed command
1170 * Read log page 10h to obtain NCQ error details and clear error
1174 * Kernel thread context (may sleep).
1177 * 0 on success, -errno otherwise.
1179 static int ata_eh_read_log_10h(struct ata_device
*dev
,
1180 int *tag
, struct ata_taskfile
*tf
)
1182 u8
*buf
= dev
->link
->ap
->sector_buf
;
1183 unsigned int err_mask
;
1187 err_mask
= ata_read_log_page(dev
, ATA_LOG_SATA_NCQ
, buf
, 1);
1192 for (i
= 0; i
< ATA_SECT_SIZE
; i
++)
1195 ata_dev_printk(dev
, KERN_WARNING
,
1196 "invalid checksum 0x%x on log page 10h\n", csum
);
1201 *tag
= buf
[0] & 0x1f;
1203 tf
->command
= buf
[2];
1204 tf
->feature
= buf
[3];
1208 tf
->device
= buf
[7];
1209 tf
->hob_lbal
= buf
[8];
1210 tf
->hob_lbam
= buf
[9];
1211 tf
->hob_lbah
= buf
[10];
1212 tf
->nsect
= buf
[12];
1213 tf
->hob_nsect
= buf
[13];
1219 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1220 * @dev: device to perform REQUEST_SENSE to
1221 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
1223 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
1224 * SENSE. This function is EH helper.
1227 * Kernel thread context (may sleep).
1230 * 0 on success, AC_ERR_* mask on failure
1232 static unsigned int atapi_eh_request_sense(struct ata_queued_cmd
*qc
)
1234 struct ata_device
*dev
= qc
->dev
;
1235 unsigned char *sense_buf
= qc
->scsicmd
->sense_buffer
;
1236 struct ata_port
*ap
= dev
->link
->ap
;
1237 struct ata_taskfile tf
;
1238 u8 cdb
[ATAPI_CDB_LEN
];
1240 DPRINTK("ATAPI request sense\n");
1242 /* FIXME: is this needed? */
1243 memset(sense_buf
, 0, SCSI_SENSE_BUFFERSIZE
);
1245 /* initialize sense_buf with the error register,
1246 * for the case where they are -not- overwritten
1248 sense_buf
[0] = 0x70;
1249 sense_buf
[2] = qc
->result_tf
.feature
>> 4;
1251 /* some devices time out if garbage left in tf */
1252 ata_tf_init(dev
, &tf
);
1254 memset(cdb
, 0, ATAPI_CDB_LEN
);
1255 cdb
[0] = REQUEST_SENSE
;
1256 cdb
[4] = SCSI_SENSE_BUFFERSIZE
;
1258 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
1259 tf
.command
= ATA_CMD_PACKET
;
1261 /* is it pointless to prefer PIO for "safety reasons"? */
1262 if (ap
->flags
& ATA_FLAG_PIO_DMA
) {
1263 tf
.protocol
= ATA_PROT_ATAPI_DMA
;
1264 tf
.feature
|= ATAPI_PKT_DMA
;
1266 tf
.protocol
= ATA_PROT_ATAPI
;
1267 tf
.lbam
= SCSI_SENSE_BUFFERSIZE
;
1271 return ata_exec_internal(dev
, &tf
, cdb
, DMA_FROM_DEVICE
,
1272 sense_buf
, SCSI_SENSE_BUFFERSIZE
, 0);
1276 * ata_eh_analyze_serror - analyze SError for a failed port
1277 * @link: ATA link to analyze SError for
1279 * Analyze SError if available and further determine cause of
1285 static void ata_eh_analyze_serror(struct ata_link
*link
)
1287 struct ata_eh_context
*ehc
= &link
->eh_context
;
1288 u32 serror
= ehc
->i
.serror
;
1289 unsigned int err_mask
= 0, action
= 0;
1292 if (serror
& SERR_PERSISTENT
) {
1293 err_mask
|= AC_ERR_ATA_BUS
;
1294 action
|= ATA_EH_HARDRESET
;
1297 (SERR_DATA_RECOVERED
| SERR_COMM_RECOVERED
| SERR_DATA
)) {
1298 err_mask
|= AC_ERR_ATA_BUS
;
1299 action
|= ATA_EH_SOFTRESET
;
1301 if (serror
& SERR_PROTOCOL
) {
1302 err_mask
|= AC_ERR_HSM
;
1303 action
|= ATA_EH_SOFTRESET
;
1305 if (serror
& SERR_INTERNAL
) {
1306 err_mask
|= AC_ERR_SYSTEM
;
1307 action
|= ATA_EH_HARDRESET
;
1310 /* Determine whether a hotplug event has occurred. Both
1311 * SError.N/X are considered hotplug events for enabled or
1312 * host links. For disabled PMP links, only N bit is
1313 * considered as X bit is left at 1 for link plugging.
1317 if (!(link
->flags
& ATA_LFLAG_DISABLED
) || ata_is_host_link(link
))
1318 hotplug_mask
= SERR_PHYRDY_CHG
| SERR_DEV_XCHG
;
1320 hotplug_mask
= SERR_PHYRDY_CHG
;
1322 if (serror
& hotplug_mask
)
1323 ata_ehi_hotplugged(&ehc
->i
);
1325 ehc
->i
.err_mask
|= err_mask
;
1326 ehc
->i
.action
|= action
;
1330 * ata_eh_analyze_ncq_error - analyze NCQ error
1331 * @link: ATA link to analyze NCQ error for
1333 * Read log page 10h, determine the offending qc and acquire
1334 * error status TF. For NCQ device errors, all LLDDs have to do
1335 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1339 * Kernel thread context (may sleep).
1341 static void ata_eh_analyze_ncq_error(struct ata_link
*link
)
1343 struct ata_port
*ap
= link
->ap
;
1344 struct ata_eh_context
*ehc
= &link
->eh_context
;
1345 struct ata_device
*dev
= link
->device
;
1346 struct ata_queued_cmd
*qc
;
1347 struct ata_taskfile tf
;
1350 /* if frozen, we can't do much */
1351 if (ap
->pflags
& ATA_PFLAG_FROZEN
)
1354 /* is it NCQ device error? */
1355 if (!link
->sactive
|| !(ehc
->i
.err_mask
& AC_ERR_DEV
))
1358 /* has LLDD analyzed already? */
1359 for (tag
= 0; tag
< ATA_MAX_QUEUE
; tag
++) {
1360 qc
= __ata_qc_from_tag(ap
, tag
);
1362 if (!(qc
->flags
& ATA_QCFLAG_FAILED
))
1369 /* okay, this error is ours */
1370 rc
= ata_eh_read_log_10h(dev
, &tag
, &tf
);
1372 ata_link_printk(link
, KERN_ERR
, "failed to read log page 10h "
1373 "(errno=%d)\n", rc
);
1377 if (!(link
->sactive
& (1 << tag
))) {
1378 ata_link_printk(link
, KERN_ERR
, "log page 10h reported "
1379 "inactive tag %d\n", tag
);
1383 /* we've got the perpetrator, condemn it */
1384 qc
= __ata_qc_from_tag(ap
, tag
);
1385 memcpy(&qc
->result_tf
, &tf
, sizeof(tf
));
1386 qc
->err_mask
|= AC_ERR_DEV
| AC_ERR_NCQ
;
1387 ehc
->i
.err_mask
&= ~AC_ERR_DEV
;
1391 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1392 * @qc: qc to analyze
1393 * @tf: Taskfile registers to analyze
1395 * Analyze taskfile of @qc and further determine cause of
1396 * failure. This function also requests ATAPI sense data if
1400 * Kernel thread context (may sleep).
1403 * Determined recovery action
1405 static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd
*qc
,
1406 const struct ata_taskfile
*tf
)
1408 unsigned int tmp
, action
= 0;
1409 u8 stat
= tf
->command
, err
= tf
->feature
;
1411 if ((stat
& (ATA_BUSY
| ATA_DRQ
| ATA_DRDY
)) != ATA_DRDY
) {
1412 qc
->err_mask
|= AC_ERR_HSM
;
1413 return ATA_EH_SOFTRESET
;
1416 if (stat
& (ATA_ERR
| ATA_DF
))
1417 qc
->err_mask
|= AC_ERR_DEV
;
1421 switch (qc
->dev
->class) {
1424 qc
->err_mask
|= AC_ERR_ATA_BUS
;
1426 qc
->err_mask
|= AC_ERR_MEDIA
;
1428 qc
->err_mask
|= AC_ERR_INVALID
;
1432 if (!(qc
->ap
->pflags
& ATA_PFLAG_FROZEN
)) {
1433 tmp
= atapi_eh_request_sense(qc
);
1435 /* ATA_QCFLAG_SENSE_VALID is used to
1436 * tell atapi_qc_complete() that sense
1437 * data is already valid.
1439 * TODO: interpret sense data and set
1440 * appropriate err_mask.
1442 qc
->flags
|= ATA_QCFLAG_SENSE_VALID
;
1444 qc
->err_mask
|= tmp
;
1448 if (qc
->err_mask
& (AC_ERR_HSM
| AC_ERR_TIMEOUT
| AC_ERR_ATA_BUS
))
1449 action
|= ATA_EH_SOFTRESET
;
1454 static int ata_eh_categorize_error(int is_io
, unsigned int err_mask
)
1456 if (err_mask
& AC_ERR_ATA_BUS
)
1459 if (err_mask
& AC_ERR_TIMEOUT
)
1463 if (err_mask
& AC_ERR_HSM
)
1466 (AC_ERR_DEV
|AC_ERR_MEDIA
|AC_ERR_INVALID
)) == AC_ERR_DEV
)
1473 struct speed_down_verdict_arg
{
1478 static int speed_down_verdict_cb(struct ata_ering_entry
*ent
, void *void_arg
)
1480 struct speed_down_verdict_arg
*arg
= void_arg
;
1481 int cat
= ata_eh_categorize_error(ent
->is_io
, ent
->err_mask
);
1483 if (ent
->timestamp
< arg
->since
)
1486 arg
->nr_errors
[cat
]++;
1491 * ata_eh_speed_down_verdict - Determine speed down verdict
1492 * @dev: Device of interest
1494 * This function examines error ring of @dev and determines
1495 * whether NCQ needs to be turned off, transfer speed should be
1496 * stepped down, or falling back to PIO is necessary.
1498 * Cat-1 is ATA_BUS error for any command.
1500 * Cat-2 is TIMEOUT for any command or HSM violation for known
1501 * supported commands.
1503 * Cat-3 is is unclassified DEV error for known supported
1506 * NCQ needs to be turned off if there have been more than 3
1507 * Cat-2 + Cat-3 errors during last 10 minutes.
1509 * Speed down is necessary if there have been more than 3 Cat-1 +
1510 * Cat-2 errors or 10 Cat-3 errors during last 10 minutes.
1512 * Falling back to PIO mode is necessary if there have been more
1513 * than 10 Cat-1 + Cat-2 + Cat-3 errors during last 5 minutes.
1516 * Inherited from caller.
1519 * OR of ATA_EH_SPDN_* flags.
1521 static unsigned int ata_eh_speed_down_verdict(struct ata_device
*dev
)
1523 const u64 j5mins
= 5LLU * 60 * HZ
, j10mins
= 10LLU * 60 * HZ
;
1524 u64 j64
= get_jiffies_64();
1525 struct speed_down_verdict_arg arg
;
1526 unsigned int verdict
= 0;
1528 /* scan past 10 mins of error history */
1529 memset(&arg
, 0, sizeof(arg
));
1530 arg
.since
= j64
- min(j64
, j10mins
);
1531 ata_ering_map(&dev
->ering
, speed_down_verdict_cb
, &arg
);
1533 if (arg
.nr_errors
[2] + arg
.nr_errors
[3] > 3)
1534 verdict
|= ATA_EH_SPDN_NCQ_OFF
;
1535 if (arg
.nr_errors
[1] + arg
.nr_errors
[2] > 3 || arg
.nr_errors
[3] > 10)
1536 verdict
|= ATA_EH_SPDN_SPEED_DOWN
;
1538 /* scan past 3 mins of error history */
1539 memset(&arg
, 0, sizeof(arg
));
1540 arg
.since
= j64
- min(j64
, j5mins
);
1541 ata_ering_map(&dev
->ering
, speed_down_verdict_cb
, &arg
);
1543 if (arg
.nr_errors
[1] + arg
.nr_errors
[2] + arg
.nr_errors
[3] > 10)
1544 verdict
|= ATA_EH_SPDN_FALLBACK_TO_PIO
;
1550 * ata_eh_speed_down - record error and speed down if necessary
1551 * @dev: Failed device
1552 * @is_io: Did the device fail during normal IO?
1553 * @err_mask: err_mask of the error
1555 * Record error and examine error history to determine whether
1556 * adjusting transmission speed is necessary. It also sets
1557 * transmission limits appropriately if such adjustment is
1561 * Kernel thread context (may sleep).
1564 * Determined recovery action.
1566 static unsigned int ata_eh_speed_down(struct ata_device
*dev
, int is_io
,
1567 unsigned int err_mask
)
1569 unsigned int verdict
;
1570 unsigned int action
= 0;
1572 /* don't bother if Cat-0 error */
1573 if (ata_eh_categorize_error(is_io
, err_mask
) == 0)
1576 /* record error and determine whether speed down is necessary */
1577 ata_ering_record(&dev
->ering
, is_io
, err_mask
);
1578 verdict
= ata_eh_speed_down_verdict(dev
);
1581 if ((verdict
& ATA_EH_SPDN_NCQ_OFF
) &&
1582 (dev
->flags
& (ATA_DFLAG_PIO
| ATA_DFLAG_NCQ
|
1583 ATA_DFLAG_NCQ_OFF
)) == ATA_DFLAG_NCQ
) {
1584 dev
->flags
|= ATA_DFLAG_NCQ_OFF
;
1585 ata_dev_printk(dev
, KERN_WARNING
,
1586 "NCQ disabled due to excessive errors\n");
1591 if (verdict
& ATA_EH_SPDN_SPEED_DOWN
) {
1592 /* speed down SATA link speed if possible */
1593 if (sata_down_spd_limit(dev
->link
) == 0) {
1594 action
|= ATA_EH_HARDRESET
;
1598 /* lower transfer mode */
1599 if (dev
->spdn_cnt
< 2) {
1600 static const int dma_dnxfer_sel
[] =
1601 { ATA_DNXFER_DMA
, ATA_DNXFER_40C
};
1602 static const int pio_dnxfer_sel
[] =
1603 { ATA_DNXFER_PIO
, ATA_DNXFER_FORCE_PIO0
};
1606 if (dev
->xfer_shift
!= ATA_SHIFT_PIO
)
1607 sel
= dma_dnxfer_sel
[dev
->spdn_cnt
];
1609 sel
= pio_dnxfer_sel
[dev
->spdn_cnt
];
1613 if (ata_down_xfermask_limit(dev
, sel
) == 0) {
1614 action
|= ATA_EH_SOFTRESET
;
1620 /* Fall back to PIO? Slowing down to PIO is meaningless for
1621 * SATA. Consider it only for PATA.
1623 if ((verdict
& ATA_EH_SPDN_FALLBACK_TO_PIO
) && (dev
->spdn_cnt
>= 2) &&
1624 (dev
->link
->ap
->cbl
!= ATA_CBL_SATA
) &&
1625 (dev
->xfer_shift
!= ATA_SHIFT_PIO
)) {
1626 if (ata_down_xfermask_limit(dev
, ATA_DNXFER_FORCE_PIO
) == 0) {
1628 action
|= ATA_EH_SOFTRESET
;
1635 /* device has been slowed down, blow error history */
1636 ata_ering_clear(&dev
->ering
);
1641 * ata_eh_link_autopsy - analyze error and determine recovery action
1642 * @link: host link to perform autopsy on
1644 * Analyze why @link failed and determine which recovery actions
1645 * are needed. This function also sets more detailed AC_ERR_*
1646 * values and fills sense data for ATAPI CHECK SENSE.
1649 * Kernel thread context (may sleep).
1651 static void ata_eh_link_autopsy(struct ata_link
*link
)
1653 struct ata_port
*ap
= link
->ap
;
1654 struct ata_eh_context
*ehc
= &link
->eh_context
;
1655 struct ata_device
*dev
;
1656 unsigned int all_err_mask
= 0;
1663 if (ehc
->i
.flags
& ATA_EHI_NO_AUTOPSY
)
1666 /* obtain and analyze SError */
1667 rc
= sata_scr_read(link
, SCR_ERROR
, &serror
);
1669 ehc
->i
.serror
|= serror
;
1670 ata_eh_analyze_serror(link
);
1671 } else if (rc
!= -EOPNOTSUPP
) {
1672 /* SError read failed, force hardreset and probing */
1673 ata_ehi_schedule_probe(&ehc
->i
);
1674 ehc
->i
.action
|= ATA_EH_HARDRESET
;
1675 ehc
->i
.err_mask
|= AC_ERR_OTHER
;
1678 /* analyze NCQ failure */
1679 ata_eh_analyze_ncq_error(link
);
1681 /* any real error trumps AC_ERR_OTHER */
1682 if (ehc
->i
.err_mask
& ~AC_ERR_OTHER
)
1683 ehc
->i
.err_mask
&= ~AC_ERR_OTHER
;
1685 all_err_mask
|= ehc
->i
.err_mask
;
1687 for (tag
= 0; tag
< ATA_MAX_QUEUE
; tag
++) {
1688 struct ata_queued_cmd
*qc
= __ata_qc_from_tag(ap
, tag
);
1690 if (!(qc
->flags
& ATA_QCFLAG_FAILED
) || qc
->dev
->link
!= link
)
1693 /* inherit upper level err_mask */
1694 qc
->err_mask
|= ehc
->i
.err_mask
;
1697 ehc
->i
.action
|= ata_eh_analyze_tf(qc
, &qc
->result_tf
);
1699 /* DEV errors are probably spurious in case of ATA_BUS error */
1700 if (qc
->err_mask
& AC_ERR_ATA_BUS
)
1701 qc
->err_mask
&= ~(AC_ERR_DEV
| AC_ERR_MEDIA
|
1704 /* any real error trumps unknown error */
1705 if (qc
->err_mask
& ~AC_ERR_OTHER
)
1706 qc
->err_mask
&= ~AC_ERR_OTHER
;
1708 /* SENSE_VALID trumps dev/unknown error and revalidation */
1709 if (qc
->flags
& ATA_QCFLAG_SENSE_VALID
)
1710 qc
->err_mask
&= ~(AC_ERR_DEV
| AC_ERR_OTHER
);
1712 /* accumulate error info */
1713 ehc
->i
.dev
= qc
->dev
;
1714 all_err_mask
|= qc
->err_mask
;
1715 if (qc
->flags
& ATA_QCFLAG_IO
)
1719 /* enforce default EH actions */
1720 if (ap
->pflags
& ATA_PFLAG_FROZEN
||
1721 all_err_mask
& (AC_ERR_HSM
| AC_ERR_TIMEOUT
))
1722 ehc
->i
.action
|= ATA_EH_SOFTRESET
;
1723 else if ((is_io
&& all_err_mask
) ||
1724 (!is_io
&& (all_err_mask
& ~AC_ERR_DEV
)))
1725 ehc
->i
.action
|= ATA_EH_REVALIDATE
;
1727 /* If we have offending qcs and the associated failed device,
1728 * perform per-dev EH action only on the offending device.
1731 ehc
->i
.dev_action
[ehc
->i
.dev
->devno
] |=
1732 ehc
->i
.action
& ATA_EH_PERDEV_MASK
;
1733 ehc
->i
.action
&= ~ATA_EH_PERDEV_MASK
;
1736 /* propagate timeout to host link */
1737 if ((all_err_mask
& AC_ERR_TIMEOUT
) && !ata_is_host_link(link
))
1738 ap
->link
.eh_context
.i
.err_mask
|= AC_ERR_TIMEOUT
;
1740 /* record error and consider speeding down */
1742 if (!dev
&& ((ata_link_max_devices(link
) == 1 &&
1743 ata_dev_enabled(link
->device
))))
1747 ehc
->i
.action
|= ata_eh_speed_down(dev
, is_io
, all_err_mask
);
1753 * ata_eh_autopsy - analyze error and determine recovery action
1754 * @ap: host port to perform autopsy on
1756 * Analyze all links of @ap and determine why they failed and
1757 * which recovery actions are needed.
1760 * Kernel thread context (may sleep).
1762 void ata_eh_autopsy(struct ata_port
*ap
)
1764 struct ata_link
*link
;
1766 ata_port_for_each_link(link
, ap
)
1767 ata_eh_link_autopsy(link
);
1769 /* Autopsy of fanout ports can affect host link autopsy.
1770 * Perform host link autopsy last.
1772 if (ap
->nr_pmp_links
)
1773 ata_eh_link_autopsy(&ap
->link
);
1777 * ata_eh_link_report - report error handling to user
1778 * @link: ATA link EH is going on
1780 * Report EH to user.
1785 static void ata_eh_link_report(struct ata_link
*link
)
1787 struct ata_port
*ap
= link
->ap
;
1788 struct ata_eh_context
*ehc
= &link
->eh_context
;
1789 const char *frozen
, *desc
;
1791 int tag
, nr_failed
= 0;
1793 if (ehc
->i
.flags
& ATA_EHI_QUIET
)
1797 if (ehc
->i
.desc
[0] != '\0')
1800 for (tag
= 0; tag
< ATA_MAX_QUEUE
; tag
++) {
1801 struct ata_queued_cmd
*qc
= __ata_qc_from_tag(ap
, tag
);
1803 if (!(qc
->flags
& ATA_QCFLAG_FAILED
) || qc
->dev
->link
!= link
||
1804 ((qc
->flags
& ATA_QCFLAG_QUIET
) &&
1805 qc
->err_mask
== AC_ERR_DEV
))
1807 if (qc
->flags
& ATA_QCFLAG_SENSE_VALID
&& !qc
->err_mask
)
1813 if (!nr_failed
&& !ehc
->i
.err_mask
)
1817 if (ap
->pflags
& ATA_PFLAG_FROZEN
)
1820 memset(tries_buf
, 0, sizeof(tries_buf
));
1821 if (ap
->eh_tries
< ATA_EH_MAX_TRIES
)
1822 snprintf(tries_buf
, sizeof(tries_buf
) - 1, " t%d",
1826 ata_dev_printk(ehc
->i
.dev
, KERN_ERR
, "exception Emask 0x%x "
1827 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1828 ehc
->i
.err_mask
, link
->sactive
, ehc
->i
.serror
,
1829 ehc
->i
.action
, frozen
, tries_buf
);
1831 ata_dev_printk(ehc
->i
.dev
, KERN_ERR
, "%s\n", desc
);
1833 ata_link_printk(link
, KERN_ERR
, "exception Emask 0x%x "
1834 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1835 ehc
->i
.err_mask
, link
->sactive
, ehc
->i
.serror
,
1836 ehc
->i
.action
, frozen
, tries_buf
);
1838 ata_link_printk(link
, KERN_ERR
, "%s\n", desc
);
1842 ata_port_printk(ap
, KERN_ERR
,
1843 "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
1844 ehc
->i
.serror
& SERR_DATA_RECOVERED
? "RecovData " : "",
1845 ehc
->i
.serror
& SERR_COMM_RECOVERED
? "RecovComm " : "",
1846 ehc
->i
.serror
& SERR_DATA
? "UnrecovData " : "",
1847 ehc
->i
.serror
& SERR_PERSISTENT
? "Persist " : "",
1848 ehc
->i
.serror
& SERR_PROTOCOL
? "Proto " : "",
1849 ehc
->i
.serror
& SERR_INTERNAL
? "HostInt " : "",
1850 ehc
->i
.serror
& SERR_PHYRDY_CHG
? "PHYRdyChg " : "",
1851 ehc
->i
.serror
& SERR_PHY_INT_ERR
? "PHYInt " : "",
1852 ehc
->i
.serror
& SERR_COMM_WAKE
? "CommWake " : "",
1853 ehc
->i
.serror
& SERR_10B_8B_ERR
? "10B8B " : "",
1854 ehc
->i
.serror
& SERR_DISPARITY
? "Dispar " : "",
1855 ehc
->i
.serror
& SERR_CRC
? "BadCRC " : "",
1856 ehc
->i
.serror
& SERR_HANDSHAKE
? "Handshk " : "",
1857 ehc
->i
.serror
& SERR_LINK_SEQ_ERR
? "LinkSeq " : "",
1858 ehc
->i
.serror
& SERR_TRANS_ST_ERROR
? "TrStaTrns " : "",
1859 ehc
->i
.serror
& SERR_UNRECOG_FIS
? "UnrecFIS " : "",
1860 ehc
->i
.serror
& SERR_DEV_XCHG
? "DevExch " : "");
1862 for (tag
= 0; tag
< ATA_MAX_QUEUE
; tag
++) {
1863 struct ata_queued_cmd
*qc
= __ata_qc_from_tag(ap
, tag
);
1864 struct ata_taskfile
*cmd
= &qc
->tf
, *res
= &qc
->result_tf
;
1865 const u8
*cdb
= qc
->cdb
;
1866 char data_buf
[20] = "";
1867 char cdb_buf
[70] = "";
1869 if (!(qc
->flags
& ATA_QCFLAG_FAILED
) ||
1870 qc
->dev
->link
!= link
|| !qc
->err_mask
)
1873 if (qc
->dma_dir
!= DMA_NONE
) {
1874 static const char *dma_str
[] = {
1875 [DMA_BIDIRECTIONAL
] = "bidi",
1876 [DMA_TO_DEVICE
] = "out",
1877 [DMA_FROM_DEVICE
] = "in",
1879 static const char *prot_str
[] = {
1880 [ATA_PROT_PIO
] = "pio",
1881 [ATA_PROT_DMA
] = "dma",
1882 [ATA_PROT_NCQ
] = "ncq",
1883 [ATA_PROT_ATAPI
] = "pio",
1884 [ATA_PROT_ATAPI_DMA
] = "dma",
1887 snprintf(data_buf
, sizeof(data_buf
), " %s %u %s",
1888 prot_str
[qc
->tf
.protocol
], qc
->nbytes
,
1889 dma_str
[qc
->dma_dir
]);
1892 if (is_atapi_taskfile(&qc
->tf
))
1893 snprintf(cdb_buf
, sizeof(cdb_buf
),
1894 "cdb %02x %02x %02x %02x %02x %02x %02x %02x "
1895 "%02x %02x %02x %02x %02x %02x %02x %02x\n ",
1896 cdb
[0], cdb
[1], cdb
[2], cdb
[3],
1897 cdb
[4], cdb
[5], cdb
[6], cdb
[7],
1898 cdb
[8], cdb
[9], cdb
[10], cdb
[11],
1899 cdb
[12], cdb
[13], cdb
[14], cdb
[15]);
1901 ata_dev_printk(qc
->dev
, KERN_ERR
,
1902 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
1904 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
1905 "Emask 0x%x (%s)%s\n",
1906 cmd
->command
, cmd
->feature
, cmd
->nsect
,
1907 cmd
->lbal
, cmd
->lbam
, cmd
->lbah
,
1908 cmd
->hob_feature
, cmd
->hob_nsect
,
1909 cmd
->hob_lbal
, cmd
->hob_lbam
, cmd
->hob_lbah
,
1910 cmd
->device
, qc
->tag
, data_buf
, cdb_buf
,
1911 res
->command
, res
->feature
, res
->nsect
,
1912 res
->lbal
, res
->lbam
, res
->lbah
,
1913 res
->hob_feature
, res
->hob_nsect
,
1914 res
->hob_lbal
, res
->hob_lbam
, res
->hob_lbah
,
1915 res
->device
, qc
->err_mask
, ata_err_string(qc
->err_mask
),
1916 qc
->err_mask
& AC_ERR_NCQ
? " <F>" : "");
1918 if (res
->command
& (ATA_BUSY
| ATA_DRDY
| ATA_DF
| ATA_DRQ
|
1920 if (res
->command
& ATA_BUSY
)
1921 ata_dev_printk(qc
->dev
, KERN_ERR
,
1922 "status: { Busy }\n");
1924 ata_dev_printk(qc
->dev
, KERN_ERR
,
1925 "status: { %s%s%s%s}\n",
1926 res
->command
& ATA_DRDY
? "DRDY " : "",
1927 res
->command
& ATA_DF
? "DF " : "",
1928 res
->command
& ATA_DRQ
? "DRQ " : "",
1929 res
->command
& ATA_ERR
? "ERR " : "");
1932 if (cmd
->command
!= ATA_CMD_PACKET
&&
1933 (res
->feature
& (ATA_ICRC
| ATA_UNC
| ATA_IDNF
|
1935 ata_dev_printk(qc
->dev
, KERN_ERR
,
1936 "error: { %s%s%s%s}\n",
1937 res
->feature
& ATA_ICRC
? "ICRC " : "",
1938 res
->feature
& ATA_UNC
? "UNC " : "",
1939 res
->feature
& ATA_IDNF
? "IDNF " : "",
1940 res
->feature
& ATA_ABORTED
? "ABRT " : "");
1945 * ata_eh_report - report error handling to user
1946 * @ap: ATA port to report EH about
1948 * Report EH to user.
1953 void ata_eh_report(struct ata_port
*ap
)
1955 struct ata_link
*link
;
1957 __ata_port_for_each_link(link
, ap
)
1958 ata_eh_link_report(link
);
1961 static int ata_do_reset(struct ata_link
*link
, ata_reset_fn_t reset
,
1962 unsigned int *classes
, unsigned long deadline
)
1964 struct ata_device
*dev
;
1967 ata_link_for_each_dev(dev
, link
)
1968 classes
[dev
->devno
] = ATA_DEV_UNKNOWN
;
1970 rc
= reset(link
, classes
, deadline
);
1974 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
1975 * is complete and convert all ATA_DEV_UNKNOWN to
1978 ata_link_for_each_dev(dev
, link
)
1979 if (classes
[dev
->devno
] != ATA_DEV_UNKNOWN
)
1983 ata_link_for_each_dev(dev
, link
) {
1984 if (classes
[dev
->devno
] == ATA_DEV_UNKNOWN
)
1985 classes
[dev
->devno
] = ATA_DEV_NONE
;
1992 static int ata_eh_followup_srst_needed(struct ata_link
*link
,
1993 int rc
, int classify
,
1994 const unsigned int *classes
)
1996 if (link
->flags
& ATA_LFLAG_NO_SRST
)
2002 if ((link
->ap
->flags
& ATA_FLAG_PMP
) && ata_is_host_link(link
))
2004 if (classify
&& !(link
->flags
& ATA_LFLAG_ASSUME_CLASS
) &&
2005 classes
[0] == ATA_DEV_UNKNOWN
)
2010 int ata_eh_reset(struct ata_link
*link
, int classify
,
2011 ata_prereset_fn_t prereset
, ata_reset_fn_t softreset
,
2012 ata_reset_fn_t hardreset
, ata_postreset_fn_t postreset
)
2014 const int max_tries
= ARRAY_SIZE(ata_eh_reset_timeouts
);
2015 struct ata_port
*ap
= link
->ap
;
2016 struct ata_eh_context
*ehc
= &link
->eh_context
;
2017 unsigned int *classes
= ehc
->classes
;
2018 unsigned int lflags
= link
->flags
;
2019 int verbose
= !(ehc
->i
.flags
& ATA_EHI_QUIET
);
2021 struct ata_device
*dev
;
2022 unsigned long deadline
, now
;
2023 unsigned int tmp_action
;
2024 ata_reset_fn_t reset
;
2025 unsigned long flags
;
2029 /* about to reset */
2030 spin_lock_irqsave(ap
->lock
, flags
);
2031 ap
->pflags
|= ATA_PFLAG_RESETTING
;
2032 spin_unlock_irqrestore(ap
->lock
, flags
);
2034 ata_eh_about_to_do(link
, NULL
, ehc
->i
.action
& ATA_EH_RESET_MASK
);
2036 ata_link_for_each_dev(dev
, link
) {
2037 /* If we issue an SRST then an ATA drive (not ATAPI)
2038 * may change configuration and be in PIO0 timing. If
2039 * we do a hard reset (or are coming from power on)
2040 * this is true for ATA or ATAPI. Until we've set a
2041 * suitable controller mode we should not touch the
2042 * bus as we may be talking too fast.
2044 dev
->pio_mode
= XFER_PIO_0
;
2046 /* If the controller has a pio mode setup function
2047 * then use it to set the chipset to rights. Don't
2048 * touch the DMA setup as that will be dealt with when
2049 * configuring devices.
2051 if (ap
->ops
->set_piomode
)
2052 ap
->ops
->set_piomode(ap
, dev
);
2055 /* Determine which reset to use and record in ehc->i.action.
2056 * prereset() may examine and modify it.
2058 if (softreset
&& (!hardreset
|| (!(lflags
& ATA_LFLAG_NO_SRST
) &&
2059 !sata_set_spd_needed(link
) &&
2060 !(ehc
->i
.action
& ATA_EH_HARDRESET
))))
2061 tmp_action
= ATA_EH_SOFTRESET
;
2063 tmp_action
= ATA_EH_HARDRESET
;
2065 ehc
->i
.action
= (ehc
->i
.action
& ~ATA_EH_RESET_MASK
) | tmp_action
;
2068 rc
= prereset(link
, jiffies
+ ATA_EH_PRERESET_TIMEOUT
);
2070 if (rc
== -ENOENT
) {
2071 ata_link_printk(link
, KERN_DEBUG
,
2072 "port disabled. ignoring.\n");
2073 ehc
->i
.action
&= ~ATA_EH_RESET_MASK
;
2075 ata_link_for_each_dev(dev
, link
)
2076 classes
[dev
->devno
] = ATA_DEV_NONE
;
2080 ata_link_printk(link
, KERN_ERR
,
2081 "prereset failed (errno=%d)\n", rc
);
2086 /* prereset() might have modified ehc->i.action */
2087 if (ehc
->i
.action
& ATA_EH_HARDRESET
)
2089 else if (ehc
->i
.action
& ATA_EH_SOFTRESET
)
2092 /* prereset told us not to reset, bang classes and return */
2093 ata_link_for_each_dev(dev
, link
)
2094 classes
[dev
->devno
] = ATA_DEV_NONE
;
2099 /* did prereset() screw up? if so, fix up to avoid oopsing */
2108 deadline
= jiffies
+ ata_eh_reset_timeouts
[try++];
2110 /* shut up during boot probing */
2112 ata_link_printk(link
, KERN_INFO
, "%s resetting link\n",
2113 reset
== softreset
? "soft" : "hard");
2115 /* mark that this EH session started with reset */
2116 if (reset
== hardreset
)
2117 ehc
->i
.flags
|= ATA_EHI_DID_HARDRESET
;
2119 ehc
->i
.flags
|= ATA_EHI_DID_SOFTRESET
;
2121 rc
= ata_do_reset(link
, reset
, classes
, deadline
);
2123 if (reset
== hardreset
&&
2124 ata_eh_followup_srst_needed(link
, rc
, classify
, classes
)) {
2125 /* okay, let's do follow-up softreset */
2129 ata_link_printk(link
, KERN_ERR
,
2130 "follow-up softreset required "
2131 "but no softreset avaliable\n");
2136 ata_eh_about_to_do(link
, NULL
, ATA_EH_RESET_MASK
);
2137 rc
= ata_do_reset(link
, reset
, classes
, deadline
);
2140 /* -EAGAIN can happen if we skipped followup SRST */
2141 if (rc
&& rc
!= -EAGAIN
)
2144 /* was classification successful? */
2145 if (classify
&& classes
[0] == ATA_DEV_UNKNOWN
&&
2146 !(lflags
& ATA_LFLAG_ASSUME_CLASS
)) {
2147 if (try < max_tries
) {
2148 ata_link_printk(link
, KERN_WARNING
,
2149 "classification failed\n");
2154 ata_link_printk(link
, KERN_WARNING
,
2155 "classfication failed, assuming ATA\n");
2156 lflags
|= ATA_LFLAG_ASSUME_ATA
;
2159 ata_link_for_each_dev(dev
, link
) {
2160 /* After the reset, the device state is PIO 0 and the
2161 * controller state is undefined. Reset also wakes up
2162 * drives from sleeping mode.
2164 dev
->pio_mode
= XFER_PIO_0
;
2165 dev
->flags
&= ~ATA_DFLAG_SLEEPING
;
2167 if (ata_link_offline(link
))
2170 /* apply class override */
2171 if (lflags
& ATA_LFLAG_ASSUME_ATA
)
2172 classes
[dev
->devno
] = ATA_DEV_ATA
;
2173 else if (lflags
& ATA_LFLAG_ASSUME_SEMB
)
2174 classes
[dev
->devno
] = ATA_DEV_SEMB_UNSUP
; /* not yet */
2177 /* record current link speed */
2178 if (sata_scr_read(link
, SCR_STATUS
, &sstatus
) == 0)
2179 link
->sata_spd
= (sstatus
>> 4) & 0xf;
2182 postreset(link
, classes
);
2184 /* reset successful, schedule revalidation */
2185 ata_eh_done(link
, NULL
, ehc
->i
.action
& ATA_EH_RESET_MASK
);
2186 ehc
->i
.action
|= ATA_EH_REVALIDATE
;
2190 /* clear hotplug flag */
2191 ehc
->i
.flags
&= ~ATA_EHI_HOTPLUGGED
;
2193 spin_lock_irqsave(ap
->lock
, flags
);
2194 ap
->pflags
&= ~ATA_PFLAG_RESETTING
;
2195 spin_unlock_irqrestore(ap
->lock
, flags
);
2200 if (rc
== -ERESTART
|| try >= max_tries
)
2204 if (time_before(now
, deadline
)) {
2205 unsigned long delta
= deadline
- now
;
2207 ata_link_printk(link
, KERN_WARNING
, "reset failed "
2208 "(errno=%d), retrying in %u secs\n",
2209 rc
, (jiffies_to_msecs(delta
) + 999) / 1000);
2212 delta
= schedule_timeout_uninterruptible(delta
);
2215 if (rc
== -EPIPE
|| try == max_tries
- 1)
2216 sata_down_spd_limit(link
);
2222 static int ata_eh_revalidate_and_attach(struct ata_link
*link
,
2223 struct ata_device
**r_failed_dev
)
2225 struct ata_port
*ap
= link
->ap
;
2226 struct ata_eh_context
*ehc
= &link
->eh_context
;
2227 struct ata_device
*dev
;
2228 unsigned int new_mask
= 0;
2229 unsigned long flags
;
2234 /* For PATA drive side cable detection to work, IDENTIFY must
2235 * be done backwards such that PDIAG- is released by the slave
2236 * device before the master device is identified.
2238 ata_link_for_each_dev_reverse(dev
, link
) {
2239 unsigned int action
= ata_eh_dev_action(dev
);
2240 unsigned int readid_flags
= 0;
2242 if (ehc
->i
.flags
& ATA_EHI_DID_RESET
)
2243 readid_flags
|= ATA_READID_POSTRESET
;
2245 if ((action
& ATA_EH_REVALIDATE
) && ata_dev_enabled(dev
)) {
2246 WARN_ON(dev
->class == ATA_DEV_PMP
);
2248 if (ata_link_offline(link
)) {
2253 ata_eh_about_to_do(link
, dev
, ATA_EH_REVALIDATE
);
2254 rc
= ata_dev_revalidate(dev
, ehc
->classes
[dev
->devno
],
2259 ata_eh_done(link
, dev
, ATA_EH_REVALIDATE
);
2261 /* Configuration may have changed, reconfigure
2264 ehc
->i
.flags
|= ATA_EHI_SETMODE
;
2266 /* schedule the scsi_rescan_device() here */
2267 queue_work(ata_aux_wq
, &(ap
->scsi_rescan_task
));
2268 } else if (dev
->class == ATA_DEV_UNKNOWN
&&
2269 ehc
->tries
[dev
->devno
] &&
2270 ata_class_enabled(ehc
->classes
[dev
->devno
])) {
2271 dev
->class = ehc
->classes
[dev
->devno
];
2273 if (dev
->class == ATA_DEV_PMP
)
2274 rc
= sata_pmp_attach(dev
);
2276 rc
= ata_dev_read_id(dev
, &dev
->class,
2277 readid_flags
, dev
->id
);
2280 new_mask
|= 1 << dev
->devno
;
2283 /* IDENTIFY was issued to non-existent
2284 * device. No need to reset. Just
2285 * thaw and kill the device.
2287 ata_eh_thaw_port(ap
);
2288 dev
->class = ATA_DEV_UNKNOWN
;
2291 dev
->class = ATA_DEV_UNKNOWN
;
2297 /* PDIAG- should have been released, ask cable type if post-reset */
2298 if (ata_is_host_link(link
) && ap
->ops
->cable_detect
&&
2299 (ehc
->i
.flags
& ATA_EHI_DID_RESET
))
2300 ap
->cbl
= ap
->ops
->cable_detect(ap
);
2302 /* Configure new devices forward such that user doesn't see
2303 * device detection messages backwards.
2305 ata_link_for_each_dev(dev
, link
) {
2306 if (!(new_mask
& (1 << dev
->devno
)) ||
2307 dev
->class == ATA_DEV_PMP
)
2310 ehc
->i
.flags
|= ATA_EHI_PRINTINFO
;
2311 rc
= ata_dev_configure(dev
);
2312 ehc
->i
.flags
&= ~ATA_EHI_PRINTINFO
;
2316 spin_lock_irqsave(ap
->lock
, flags
);
2317 ap
->pflags
|= ATA_PFLAG_SCSI_HOTPLUG
;
2318 spin_unlock_irqrestore(ap
->lock
, flags
);
2320 /* new device discovered, configure xfermode */
2321 ehc
->i
.flags
|= ATA_EHI_SETMODE
;
2327 *r_failed_dev
= dev
;
2328 DPRINTK("EXIT rc=%d\n", rc
);
2332 static int ata_link_nr_enabled(struct ata_link
*link
)
2334 struct ata_device
*dev
;
2337 ata_link_for_each_dev(dev
, link
)
2338 if (ata_dev_enabled(dev
))
2343 static int ata_link_nr_vacant(struct ata_link
*link
)
2345 struct ata_device
*dev
;
2348 ata_link_for_each_dev(dev
, link
)
2349 if (dev
->class == ATA_DEV_UNKNOWN
)
2354 static int ata_eh_skip_recovery(struct ata_link
*link
)
2356 struct ata_eh_context
*ehc
= &link
->eh_context
;
2357 struct ata_device
*dev
;
2359 /* skip disabled links */
2360 if (link
->flags
& ATA_LFLAG_DISABLED
)
2363 /* thaw frozen port, resume link and recover failed devices */
2364 if ((link
->ap
->pflags
& ATA_PFLAG_FROZEN
) ||
2365 (ehc
->i
.flags
& ATA_EHI_RESUME_LINK
) || ata_link_nr_enabled(link
))
2368 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
2369 ata_link_for_each_dev(dev
, link
) {
2370 if (dev
->class == ATA_DEV_UNKNOWN
&&
2371 ehc
->classes
[dev
->devno
] != ATA_DEV_NONE
)
2378 static int ata_eh_handle_dev_fail(struct ata_device
*dev
, int err
)
2380 struct ata_eh_context
*ehc
= &dev
->link
->eh_context
;
2382 ehc
->tries
[dev
->devno
]--;
2386 /* device missing or wrong IDENTIFY data, schedule probing */
2387 ehc
->i
.probe_mask
|= (1 << dev
->devno
);
2389 /* give it just one more chance */
2390 ehc
->tries
[dev
->devno
] = min(ehc
->tries
[dev
->devno
], 1);
2392 if (ehc
->tries
[dev
->devno
] == 1 && dev
->pio_mode
> XFER_PIO_0
) {
2393 /* This is the last chance, better to slow
2394 * down than lose it.
2396 sata_down_spd_limit(dev
->link
);
2397 ata_down_xfermask_limit(dev
, ATA_DNXFER_PIO
);
2401 if (ata_dev_enabled(dev
) && !ehc
->tries
[dev
->devno
]) {
2402 /* disable device if it has used up all its chances */
2403 ata_dev_disable(dev
);
2405 /* detach if offline */
2406 if (ata_link_offline(dev
->link
))
2407 ata_eh_detach_dev(dev
);
2409 /* probe if requested */
2410 if ((ehc
->i
.probe_mask
& (1 << dev
->devno
)) &&
2411 !(ehc
->did_probe_mask
& (1 << dev
->devno
))) {
2412 ata_eh_detach_dev(dev
);
2415 ehc
->tries
[dev
->devno
] = ATA_EH_DEV_TRIES
;
2416 ehc
->did_probe_mask
|= (1 << dev
->devno
);
2417 ehc
->i
.action
|= ATA_EH_SOFTRESET
;
2422 /* soft didn't work? be haaaaard */
2423 if (ehc
->i
.flags
& ATA_EHI_DID_RESET
)
2424 ehc
->i
.action
|= ATA_EH_HARDRESET
;
2426 ehc
->i
.action
|= ATA_EH_SOFTRESET
;
2433 * ata_eh_recover - recover host port after error
2434 * @ap: host port to recover
2435 * @prereset: prereset method (can be NULL)
2436 * @softreset: softreset method (can be NULL)
2437 * @hardreset: hardreset method (can be NULL)
2438 * @postreset: postreset method (can be NULL)
2439 * @r_failed_link: out parameter for failed link
2441 * This is the alpha and omega, eum and yang, heart and soul of
2442 * libata exception handling. On entry, actions required to
2443 * recover each link and hotplug requests are recorded in the
2444 * link's eh_context. This function executes all the operations
2445 * with appropriate retrials and fallbacks to resurrect failed
2446 * devices, detach goners and greet newcomers.
2449 * Kernel thread context (may sleep).
2452 * 0 on success, -errno on failure.
2454 int ata_eh_recover(struct ata_port
*ap
, ata_prereset_fn_t prereset
,
2455 ata_reset_fn_t softreset
, ata_reset_fn_t hardreset
,
2456 ata_postreset_fn_t postreset
,
2457 struct ata_link
**r_failed_link
)
2459 struct ata_link
*link
;
2460 struct ata_device
*dev
;
2461 int nr_failed_devs
, nr_disabled_devs
;
2463 unsigned long flags
;
2467 /* prep for recovery */
2468 ata_port_for_each_link(link
, ap
) {
2469 struct ata_eh_context
*ehc
= &link
->eh_context
;
2471 /* re-enable link? */
2472 if (ehc
->i
.action
& ATA_EH_ENABLE_LINK
) {
2473 ata_eh_about_to_do(link
, NULL
, ATA_EH_ENABLE_LINK
);
2474 spin_lock_irqsave(ap
->lock
, flags
);
2475 link
->flags
&= ~ATA_LFLAG_DISABLED
;
2476 spin_unlock_irqrestore(ap
->lock
, flags
);
2477 ata_eh_done(link
, NULL
, ATA_EH_ENABLE_LINK
);
2480 ata_link_for_each_dev(dev
, link
) {
2481 if (link
->flags
& ATA_LFLAG_NO_RETRY
)
2482 ehc
->tries
[dev
->devno
] = 1;
2484 ehc
->tries
[dev
->devno
] = ATA_EH_DEV_TRIES
;
2486 /* collect port action mask recorded in dev actions */
2487 ehc
->i
.action
|= ehc
->i
.dev_action
[dev
->devno
] &
2488 ~ATA_EH_PERDEV_MASK
;
2489 ehc
->i
.dev_action
[dev
->devno
] &= ATA_EH_PERDEV_MASK
;
2491 /* process hotplug request */
2492 if (dev
->flags
& ATA_DFLAG_DETACH
)
2493 ata_eh_detach_dev(dev
);
2495 if (!ata_dev_enabled(dev
) &&
2496 ((ehc
->i
.probe_mask
& (1 << dev
->devno
)) &&
2497 !(ehc
->did_probe_mask
& (1 << dev
->devno
)))) {
2498 ata_eh_detach_dev(dev
);
2500 ehc
->did_probe_mask
|= (1 << dev
->devno
);
2501 ehc
->i
.action
|= ATA_EH_SOFTRESET
;
2509 nr_disabled_devs
= 0;
2512 /* if UNLOADING, finish immediately */
2513 if (ap
->pflags
& ATA_PFLAG_UNLOADING
)
2517 ata_port_for_each_link(link
, ap
) {
2518 struct ata_eh_context
*ehc
= &link
->eh_context
;
2520 /* skip EH if possible. */
2521 if (ata_eh_skip_recovery(link
))
2524 /* do we need to reset? */
2525 if (ehc
->i
.action
& ATA_EH_RESET_MASK
)
2528 ata_link_for_each_dev(dev
, link
)
2529 ehc
->classes
[dev
->devno
] = ATA_DEV_UNKNOWN
;
2534 /* if PMP is attached, this function only deals with
2535 * downstream links, port should stay thawed.
2537 if (!ap
->nr_pmp_links
)
2538 ata_eh_freeze_port(ap
);
2540 ata_port_for_each_link(link
, ap
) {
2541 struct ata_eh_context
*ehc
= &link
->eh_context
;
2543 if (!(ehc
->i
.action
& ATA_EH_RESET_MASK
))
2546 rc
= ata_eh_reset(link
, ata_link_nr_vacant(link
),
2547 prereset
, softreset
, hardreset
,
2550 ata_link_printk(link
, KERN_ERR
,
2551 "reset failed, giving up\n");
2556 if (!ap
->nr_pmp_links
)
2557 ata_eh_thaw_port(ap
);
2561 ata_port_for_each_link(link
, ap
) {
2562 struct ata_eh_context
*ehc
= &link
->eh_context
;
2564 /* revalidate existing devices and attach new ones */
2565 rc
= ata_eh_revalidate_and_attach(link
, &dev
);
2569 /* if PMP got attached, return, pmp EH will take care of it */
2570 if (link
->device
->class == ATA_DEV_PMP
) {
2575 /* configure transfer mode if necessary */
2576 if (ehc
->i
.flags
& ATA_EHI_SETMODE
) {
2577 rc
= ata_set_mode(link
, &dev
);
2580 ehc
->i
.flags
&= ~ATA_EHI_SETMODE
;
2583 if (ehc
->i
.action
& ATA_EHI_LPM
)
2584 ata_link_for_each_dev(dev
, link
)
2585 ata_dev_enable_pm(dev
, ap
->pm_policy
);
2587 /* this link is okay now */
2593 if (ata_eh_handle_dev_fail(dev
, rc
))
2596 if (ap
->pflags
& ATA_PFLAG_FROZEN
) {
2597 /* PMP reset requires working host port.
2598 * Can't retry if it's frozen.
2600 if (ap
->nr_pmp_links
)
2606 if (nr_failed_devs
) {
2607 if (nr_failed_devs
!= nr_disabled_devs
) {
2608 ata_port_printk(ap
, KERN_WARNING
, "failed to recover "
2609 "some devices, retrying in 5 secs\n");
2612 /* no device left to recover, repeat fast */
2620 if (rc
&& r_failed_link
)
2621 *r_failed_link
= link
;
2623 DPRINTK("EXIT, rc=%d\n", rc
);
2628 * ata_eh_finish - finish up EH
2629 * @ap: host port to finish EH for
2631 * Recovery is complete. Clean up EH states and retry or finish
2637 void ata_eh_finish(struct ata_port
*ap
)
2641 /* retry or finish qcs */
2642 for (tag
= 0; tag
< ATA_MAX_QUEUE
; tag
++) {
2643 struct ata_queued_cmd
*qc
= __ata_qc_from_tag(ap
, tag
);
2645 if (!(qc
->flags
& ATA_QCFLAG_FAILED
))
2649 /* FIXME: Once EH migration is complete,
2650 * generate sense data in this function,
2651 * considering both err_mask and tf.
2653 * There's no point in retrying invalid
2654 * (detected by libata) and non-IO device
2655 * errors (rejected by device). Finish them
2658 if ((qc
->err_mask
& AC_ERR_INVALID
) ||
2659 (!(qc
->flags
& ATA_QCFLAG_IO
) &&
2660 qc
->err_mask
== AC_ERR_DEV
))
2661 ata_eh_qc_complete(qc
);
2663 ata_eh_qc_retry(qc
);
2665 if (qc
->flags
& ATA_QCFLAG_SENSE_VALID
) {
2666 ata_eh_qc_complete(qc
);
2668 /* feed zero TF to sense generation */
2669 memset(&qc
->result_tf
, 0, sizeof(qc
->result_tf
));
2670 ata_eh_qc_retry(qc
);
2675 /* make sure nr_active_links is zero after EH */
2676 WARN_ON(ap
->nr_active_links
);
2677 ap
->nr_active_links
= 0;
2681 * ata_do_eh - do standard error handling
2682 * @ap: host port to handle error for
2683 * @prereset: prereset method (can be NULL)
2684 * @softreset: softreset method (can be NULL)
2685 * @hardreset: hardreset method (can be NULL)
2686 * @postreset: postreset method (can be NULL)
2688 * Perform standard error handling sequence.
2691 * Kernel thread context (may sleep).
2693 void ata_do_eh(struct ata_port
*ap
, ata_prereset_fn_t prereset
,
2694 ata_reset_fn_t softreset
, ata_reset_fn_t hardreset
,
2695 ata_postreset_fn_t postreset
)
2697 struct ata_device
*dev
;
2703 rc
= ata_eh_recover(ap
, prereset
, softreset
, hardreset
, postreset
,
2706 ata_link_for_each_dev(dev
, &ap
->link
)
2707 ata_dev_disable(dev
);
2715 * ata_eh_handle_port_suspend - perform port suspend operation
2716 * @ap: port to suspend
2721 * Kernel thread context (may sleep).
2723 static void ata_eh_handle_port_suspend(struct ata_port
*ap
)
2725 unsigned long flags
;
2728 /* are we suspending? */
2729 spin_lock_irqsave(ap
->lock
, flags
);
2730 if (!(ap
->pflags
& ATA_PFLAG_PM_PENDING
) ||
2731 ap
->pm_mesg
.event
== PM_EVENT_ON
) {
2732 spin_unlock_irqrestore(ap
->lock
, flags
);
2735 spin_unlock_irqrestore(ap
->lock
, flags
);
2737 WARN_ON(ap
->pflags
& ATA_PFLAG_SUSPENDED
);
2739 /* tell ACPI we're suspending */
2740 rc
= ata_acpi_on_suspend(ap
);
2745 ata_eh_freeze_port(ap
);
2747 if (ap
->ops
->port_suspend
)
2748 rc
= ap
->ops
->port_suspend(ap
, ap
->pm_mesg
);
2752 spin_lock_irqsave(ap
->lock
, flags
);
2754 ap
->pflags
&= ~ATA_PFLAG_PM_PENDING
;
2756 ap
->pflags
|= ATA_PFLAG_SUSPENDED
;
2757 else if (ap
->pflags
& ATA_PFLAG_FROZEN
)
2758 ata_port_schedule_eh(ap
);
2760 if (ap
->pm_result
) {
2761 *ap
->pm_result
= rc
;
2762 ap
->pm_result
= NULL
;
2765 spin_unlock_irqrestore(ap
->lock
, flags
);
2771 * ata_eh_handle_port_resume - perform port resume operation
2772 * @ap: port to resume
2777 * Kernel thread context (may sleep).
2779 static void ata_eh_handle_port_resume(struct ata_port
*ap
)
2781 unsigned long flags
;
2784 /* are we resuming? */
2785 spin_lock_irqsave(ap
->lock
, flags
);
2786 if (!(ap
->pflags
& ATA_PFLAG_PM_PENDING
) ||
2787 ap
->pm_mesg
.event
!= PM_EVENT_ON
) {
2788 spin_unlock_irqrestore(ap
->lock
, flags
);
2791 spin_unlock_irqrestore(ap
->lock
, flags
);
2793 WARN_ON(!(ap
->pflags
& ATA_PFLAG_SUSPENDED
));
2795 if (ap
->ops
->port_resume
)
2796 rc
= ap
->ops
->port_resume(ap
);
2798 /* tell ACPI that we're resuming */
2799 ata_acpi_on_resume(ap
);
2802 spin_lock_irqsave(ap
->lock
, flags
);
2803 ap
->pflags
&= ~(ATA_PFLAG_PM_PENDING
| ATA_PFLAG_SUSPENDED
);
2804 if (ap
->pm_result
) {
2805 *ap
->pm_result
= rc
;
2806 ap
->pm_result
= NULL
;
2808 spin_unlock_irqrestore(ap
->lock
, flags
);
2810 #endif /* CONFIG_PM */