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
)) {
563 * ata_qc_timeout - Handle timeout of queued command
564 * @qc: Command that timed out
566 * Some part of the kernel (currently, only the SCSI layer)
567 * has noticed that the active command on port @ap has not
568 * completed after a specified length of time. Handle this
569 * condition by disabling DMA (if necessary) and completing
570 * transactions, with error if necessary.
572 * This also handles the case of the "lost interrupt", where
573 * for some reason (possibly hardware bug, possibly driver bug)
574 * an interrupt was not delivered to the driver, even though the
575 * transaction completed successfully.
577 * TODO: kill this function once old EH is gone.
580 * Inherited from SCSI layer (none, can sleep)
582 static void ata_qc_timeout(struct ata_queued_cmd
*qc
)
584 struct ata_port
*ap
= qc
->ap
;
585 u8 host_stat
= 0, drv_stat
;
590 ap
->hsm_task_state
= HSM_ST_IDLE
;
592 spin_lock_irqsave(ap
->lock
, flags
);
594 switch (qc
->tf
.protocol
) {
597 case ATA_PROT_ATAPI_DMA
:
598 host_stat
= ap
->ops
->bmdma_status(ap
);
600 /* before we do anything else, clear DMA-Start bit */
601 ap
->ops
->bmdma_stop(qc
);
607 drv_stat
= ata_chk_status(ap
);
609 /* ack bmdma irq events */
610 ap
->ops
->irq_clear(ap
);
612 ata_dev_printk(qc
->dev
, KERN_ERR
, "command 0x%x timeout, "
613 "stat 0x%x host_stat 0x%x\n",
614 qc
->tf
.command
, drv_stat
, host_stat
);
616 /* complete taskfile transaction */
617 qc
->err_mask
|= AC_ERR_TIMEOUT
;
621 spin_unlock_irqrestore(ap
->lock
, flags
);
623 ata_eh_qc_complete(qc
);
629 * ata_eng_timeout - Handle timeout of queued command
630 * @ap: Port on which timed-out command is active
632 * Some part of the kernel (currently, only the SCSI layer)
633 * has noticed that the active command on port @ap has not
634 * completed after a specified length of time. Handle this
635 * condition by disabling DMA (if necessary) and completing
636 * transactions, with error if necessary.
638 * This also handles the case of the "lost interrupt", where
639 * for some reason (possibly hardware bug, possibly driver bug)
640 * an interrupt was not delivered to the driver, even though the
641 * transaction completed successfully.
643 * TODO: kill this function once old EH is gone.
646 * Inherited from SCSI layer (none, can sleep)
648 void ata_eng_timeout(struct ata_port
*ap
)
652 ata_qc_timeout(ata_qc_from_tag(ap
, ap
->link
.active_tag
));
657 static int ata_eh_nr_in_flight(struct ata_port
*ap
)
662 /* count only non-internal commands */
663 for (tag
= 0; tag
< ATA_MAX_QUEUE
- 1; tag
++)
664 if (ata_qc_from_tag(ap
, tag
))
670 void ata_eh_fastdrain_timerfn(unsigned long arg
)
672 struct ata_port
*ap
= (void *)arg
;
676 spin_lock_irqsave(ap
->lock
, flags
);
678 cnt
= ata_eh_nr_in_flight(ap
);
684 if (cnt
== ap
->fastdrain_cnt
) {
687 /* No progress during the last interval, tag all
688 * in-flight qcs as timed out and freeze the port.
690 for (tag
= 0; tag
< ATA_MAX_QUEUE
- 1; tag
++) {
691 struct ata_queued_cmd
*qc
= ata_qc_from_tag(ap
, tag
);
693 qc
->err_mask
|= AC_ERR_TIMEOUT
;
698 /* some qcs have finished, give it another chance */
699 ap
->fastdrain_cnt
= cnt
;
700 ap
->fastdrain_timer
.expires
=
701 jiffies
+ ATA_EH_FASTDRAIN_INTERVAL
;
702 add_timer(&ap
->fastdrain_timer
);
706 spin_unlock_irqrestore(ap
->lock
, flags
);
710 * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
711 * @ap: target ATA port
712 * @fastdrain: activate fast drain
714 * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
715 * is non-zero and EH wasn't pending before. Fast drain ensures
716 * that EH kicks in in timely manner.
719 * spin_lock_irqsave(host lock)
721 static void ata_eh_set_pending(struct ata_port
*ap
, int fastdrain
)
725 /* already scheduled? */
726 if (ap
->pflags
& ATA_PFLAG_EH_PENDING
)
729 ap
->pflags
|= ATA_PFLAG_EH_PENDING
;
734 /* do we have in-flight qcs? */
735 cnt
= ata_eh_nr_in_flight(ap
);
739 /* activate fast drain */
740 ap
->fastdrain_cnt
= cnt
;
741 ap
->fastdrain_timer
.expires
= jiffies
+ ATA_EH_FASTDRAIN_INTERVAL
;
742 add_timer(&ap
->fastdrain_timer
);
746 * ata_qc_schedule_eh - schedule qc for error handling
747 * @qc: command to schedule error handling for
749 * Schedule error handling for @qc. EH will kick in as soon as
750 * other commands are drained.
753 * spin_lock_irqsave(host lock)
755 void ata_qc_schedule_eh(struct ata_queued_cmd
*qc
)
757 struct ata_port
*ap
= qc
->ap
;
759 WARN_ON(!ap
->ops
->error_handler
);
761 qc
->flags
|= ATA_QCFLAG_FAILED
;
762 ata_eh_set_pending(ap
, 1);
764 /* The following will fail if timeout has already expired.
765 * ata_scsi_error() takes care of such scmds on EH entry.
766 * Note that ATA_QCFLAG_FAILED is unconditionally set after
767 * this function completes.
769 scsi_req_abort_cmd(qc
->scsicmd
);
773 * ata_port_schedule_eh - schedule error handling without a qc
774 * @ap: ATA port to schedule EH for
776 * Schedule error handling for @ap. EH will kick in as soon as
777 * all commands are drained.
780 * spin_lock_irqsave(host lock)
782 void ata_port_schedule_eh(struct ata_port
*ap
)
784 WARN_ON(!ap
->ops
->error_handler
);
786 if (ap
->pflags
& ATA_PFLAG_INITIALIZING
)
789 ata_eh_set_pending(ap
, 1);
790 scsi_schedule_eh(ap
->scsi_host
);
792 DPRINTK("port EH scheduled\n");
795 static int ata_do_link_abort(struct ata_port
*ap
, struct ata_link
*link
)
797 int tag
, nr_aborted
= 0;
799 WARN_ON(!ap
->ops
->error_handler
);
801 /* we're gonna abort all commands, no need for fast drain */
802 ata_eh_set_pending(ap
, 0);
804 for (tag
= 0; tag
< ATA_MAX_QUEUE
; tag
++) {
805 struct ata_queued_cmd
*qc
= ata_qc_from_tag(ap
, tag
);
807 if (qc
&& (!link
|| qc
->dev
->link
== link
)) {
808 qc
->flags
|= ATA_QCFLAG_FAILED
;
815 ata_port_schedule_eh(ap
);
821 * ata_link_abort - abort all qc's on the link
822 * @link: ATA link to abort qc's for
824 * Abort all active qc's active on @link and schedule EH.
827 * spin_lock_irqsave(host lock)
830 * Number of aborted qc's.
832 int ata_link_abort(struct ata_link
*link
)
834 return ata_do_link_abort(link
->ap
, link
);
838 * ata_port_abort - abort all qc's on the port
839 * @ap: ATA port to abort qc's for
841 * Abort all active qc's of @ap and schedule EH.
844 * spin_lock_irqsave(host_set lock)
847 * Number of aborted qc's.
849 int ata_port_abort(struct ata_port
*ap
)
851 return ata_do_link_abort(ap
, NULL
);
855 * __ata_port_freeze - freeze port
856 * @ap: ATA port to freeze
858 * This function is called when HSM violation or some other
859 * condition disrupts normal operation of the port. Frozen port
860 * is not allowed to perform any operation until the port is
861 * thawed, which usually follows a successful reset.
863 * ap->ops->freeze() callback can be used for freezing the port
864 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
865 * port cannot be frozen hardware-wise, the interrupt handler
866 * must ack and clear interrupts unconditionally while the port
870 * spin_lock_irqsave(host lock)
872 static void __ata_port_freeze(struct ata_port
*ap
)
874 WARN_ON(!ap
->ops
->error_handler
);
879 ap
->pflags
|= ATA_PFLAG_FROZEN
;
881 DPRINTK("ata%u port frozen\n", ap
->print_id
);
885 * ata_port_freeze - abort & freeze port
886 * @ap: ATA port to freeze
888 * Abort and freeze @ap.
891 * spin_lock_irqsave(host lock)
894 * Number of aborted commands.
896 int ata_port_freeze(struct ata_port
*ap
)
900 WARN_ON(!ap
->ops
->error_handler
);
902 nr_aborted
= ata_port_abort(ap
);
903 __ata_port_freeze(ap
);
909 * sata_async_notification - SATA async notification handler
910 * @ap: ATA port where async notification is received
912 * Handler to be called when async notification via SDB FIS is
913 * received. This function schedules EH if necessary.
916 * spin_lock_irqsave(host lock)
919 * 1 if EH is scheduled, 0 otherwise.
921 int sata_async_notification(struct ata_port
*ap
)
926 if (!(ap
->flags
& ATA_FLAG_AN
))
929 rc
= sata_scr_read(&ap
->link
, SCR_NOTIFICATION
, &sntf
);
931 sata_scr_write(&ap
->link
, SCR_NOTIFICATION
, sntf
);
933 if (!ap
->nr_pmp_links
|| rc
) {
934 /* PMP is not attached or SNTF is not available */
935 if (!ap
->nr_pmp_links
) {
936 /* PMP is not attached. Check whether ATAPI
937 * AN is configured. If so, notify media
940 struct ata_device
*dev
= ap
->link
.device
;
942 if ((dev
->class == ATA_DEV_ATAPI
) &&
943 (dev
->flags
& ATA_DFLAG_AN
))
944 ata_scsi_media_change_notify(dev
);
947 /* PMP is attached but SNTF is not available.
948 * ATAPI async media change notification is
949 * not used. The PMP must be reporting PHY
950 * status change, schedule EH.
952 ata_port_schedule_eh(ap
);
956 /* PMP is attached and SNTF is available */
957 struct ata_link
*link
;
959 /* check and notify ATAPI AN */
960 ata_port_for_each_link(link
, ap
) {
961 if (!(sntf
& (1 << link
->pmp
)))
964 if ((link
->device
->class == ATA_DEV_ATAPI
) &&
965 (link
->device
->flags
& ATA_DFLAG_AN
))
966 ata_scsi_media_change_notify(link
->device
);
969 /* If PMP is reporting that PHY status of some
970 * downstream ports has changed, schedule EH.
972 if (sntf
& (1 << SATA_PMP_CTRL_PORT
)) {
973 ata_port_schedule_eh(ap
);
982 * ata_eh_freeze_port - EH helper to freeze port
983 * @ap: ATA port to freeze
990 void ata_eh_freeze_port(struct ata_port
*ap
)
994 if (!ap
->ops
->error_handler
)
997 spin_lock_irqsave(ap
->lock
, flags
);
998 __ata_port_freeze(ap
);
999 spin_unlock_irqrestore(ap
->lock
, flags
);
1003 * ata_port_thaw_port - EH helper to thaw port
1004 * @ap: ATA port to thaw
1006 * Thaw frozen port @ap.
1011 void ata_eh_thaw_port(struct ata_port
*ap
)
1013 unsigned long flags
;
1015 if (!ap
->ops
->error_handler
)
1018 spin_lock_irqsave(ap
->lock
, flags
);
1020 ap
->pflags
&= ~ATA_PFLAG_FROZEN
;
1025 spin_unlock_irqrestore(ap
->lock
, flags
);
1027 DPRINTK("ata%u port thawed\n", ap
->print_id
);
1030 static void ata_eh_scsidone(struct scsi_cmnd
*scmd
)
1035 static void __ata_eh_qc_complete(struct ata_queued_cmd
*qc
)
1037 struct ata_port
*ap
= qc
->ap
;
1038 struct scsi_cmnd
*scmd
= qc
->scsicmd
;
1039 unsigned long flags
;
1041 spin_lock_irqsave(ap
->lock
, flags
);
1042 qc
->scsidone
= ata_eh_scsidone
;
1043 __ata_qc_complete(qc
);
1044 WARN_ON(ata_tag_valid(qc
->tag
));
1045 spin_unlock_irqrestore(ap
->lock
, flags
);
1047 scsi_eh_finish_cmd(scmd
, &ap
->eh_done_q
);
1051 * ata_eh_qc_complete - Complete an active ATA command from EH
1052 * @qc: Command to complete
1054 * Indicate to the mid and upper layers that an ATA command has
1055 * completed. To be used from EH.
1057 void ata_eh_qc_complete(struct ata_queued_cmd
*qc
)
1059 struct scsi_cmnd
*scmd
= qc
->scsicmd
;
1060 scmd
->retries
= scmd
->allowed
;
1061 __ata_eh_qc_complete(qc
);
1065 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
1066 * @qc: Command to retry
1068 * Indicate to the mid and upper layers that an ATA command
1069 * should be retried. To be used from EH.
1071 * SCSI midlayer limits the number of retries to scmd->allowed.
1072 * scmd->retries is decremented for commands which get retried
1073 * due to unrelated failures (qc->err_mask is zero).
1075 void ata_eh_qc_retry(struct ata_queued_cmd
*qc
)
1077 struct scsi_cmnd
*scmd
= qc
->scsicmd
;
1078 if (!qc
->err_mask
&& scmd
->retries
)
1080 __ata_eh_qc_complete(qc
);
1084 * ata_eh_detach_dev - detach ATA device
1085 * @dev: ATA device to detach
1092 void ata_eh_detach_dev(struct ata_device
*dev
)
1094 struct ata_link
*link
= dev
->link
;
1095 struct ata_port
*ap
= link
->ap
;
1096 unsigned long flags
;
1098 ata_dev_disable(dev
);
1100 spin_lock_irqsave(ap
->lock
, flags
);
1102 dev
->flags
&= ~ATA_DFLAG_DETACH
;
1104 if (ata_scsi_offline_dev(dev
)) {
1105 dev
->flags
|= ATA_DFLAG_DETACHED
;
1106 ap
->pflags
|= ATA_PFLAG_SCSI_HOTPLUG
;
1109 /* clear per-dev EH actions */
1110 ata_eh_clear_action(link
, dev
, &link
->eh_info
, ATA_EH_PERDEV_MASK
);
1111 ata_eh_clear_action(link
, dev
, &link
->eh_context
.i
, ATA_EH_PERDEV_MASK
);
1113 spin_unlock_irqrestore(ap
->lock
, flags
);
1117 * ata_eh_about_to_do - about to perform eh_action
1118 * @link: target ATA link
1119 * @dev: target ATA dev for per-dev action (can be NULL)
1120 * @action: action about to be performed
1122 * Called just before performing EH actions to clear related bits
1123 * in @link->eh_info such that eh actions are not unnecessarily
1129 void ata_eh_about_to_do(struct ata_link
*link
, struct ata_device
*dev
,
1130 unsigned int action
)
1132 struct ata_port
*ap
= link
->ap
;
1133 struct ata_eh_info
*ehi
= &link
->eh_info
;
1134 struct ata_eh_context
*ehc
= &link
->eh_context
;
1135 unsigned long flags
;
1137 spin_lock_irqsave(ap
->lock
, flags
);
1139 /* Reset is represented by combination of actions and EHI
1140 * flags. Suck in all related bits before clearing eh_info to
1141 * avoid losing requested action.
1143 if (action
& ATA_EH_RESET_MASK
) {
1144 ehc
->i
.action
|= ehi
->action
& ATA_EH_RESET_MASK
;
1145 ehc
->i
.flags
|= ehi
->flags
& ATA_EHI_RESET_MODIFIER_MASK
;
1147 /* make sure all reset actions are cleared & clear EHI flags */
1148 action
|= ATA_EH_RESET_MASK
;
1149 ehi
->flags
&= ~ATA_EHI_RESET_MODIFIER_MASK
;
1152 ata_eh_clear_action(link
, dev
, ehi
, action
);
1154 if (!(ehc
->i
.flags
& ATA_EHI_QUIET
))
1155 ap
->pflags
|= ATA_PFLAG_RECOVERED
;
1157 spin_unlock_irqrestore(ap
->lock
, flags
);
1161 * ata_eh_done - EH action complete
1162 * @ap: target ATA port
1163 * @dev: target ATA dev for per-dev action (can be NULL)
1164 * @action: action just completed
1166 * Called right after performing EH actions to clear related bits
1167 * in @link->eh_context.
1172 void ata_eh_done(struct ata_link
*link
, struct ata_device
*dev
,
1173 unsigned int action
)
1175 struct ata_eh_context
*ehc
= &link
->eh_context
;
1177 /* if reset is complete, clear all reset actions & reset modifier */
1178 if (action
& ATA_EH_RESET_MASK
) {
1179 action
|= ATA_EH_RESET_MASK
;
1180 ehc
->i
.flags
&= ~ATA_EHI_RESET_MODIFIER_MASK
;
1183 ata_eh_clear_action(link
, dev
, &ehc
->i
, action
);
1187 * ata_err_string - convert err_mask to descriptive string
1188 * @err_mask: error mask to convert to string
1190 * Convert @err_mask to descriptive string. Errors are
1191 * prioritized according to severity and only the most severe
1192 * error is reported.
1198 * Descriptive string for @err_mask
1200 static const char * ata_err_string(unsigned int err_mask
)
1202 if (err_mask
& AC_ERR_HOST_BUS
)
1203 return "host bus error";
1204 if (err_mask
& AC_ERR_ATA_BUS
)
1205 return "ATA bus error";
1206 if (err_mask
& AC_ERR_TIMEOUT
)
1208 if (err_mask
& AC_ERR_HSM
)
1209 return "HSM violation";
1210 if (err_mask
& AC_ERR_SYSTEM
)
1211 return "internal error";
1212 if (err_mask
& AC_ERR_MEDIA
)
1213 return "media error";
1214 if (err_mask
& AC_ERR_INVALID
)
1215 return "invalid argument";
1216 if (err_mask
& AC_ERR_DEV
)
1217 return "device error";
1218 return "unknown error";
1222 * ata_read_log_page - read a specific log page
1223 * @dev: target device
1224 * @page: page to read
1225 * @buf: buffer to store read page
1226 * @sectors: number of sectors to read
1228 * Read log page using READ_LOG_EXT command.
1231 * Kernel thread context (may sleep).
1234 * 0 on success, AC_ERR_* mask otherwise.
1236 static unsigned int ata_read_log_page(struct ata_device
*dev
,
1237 u8 page
, void *buf
, unsigned int sectors
)
1239 struct ata_taskfile tf
;
1240 unsigned int err_mask
;
1242 DPRINTK("read log page - page %d\n", page
);
1244 ata_tf_init(dev
, &tf
);
1245 tf
.command
= ATA_CMD_READ_LOG_EXT
;
1248 tf
.hob_nsect
= sectors
>> 8;
1249 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_LBA48
| ATA_TFLAG_DEVICE
;
1250 tf
.protocol
= ATA_PROT_PIO
;
1252 err_mask
= ata_exec_internal(dev
, &tf
, NULL
, DMA_FROM_DEVICE
,
1253 buf
, sectors
* ATA_SECT_SIZE
, 0);
1255 DPRINTK("EXIT, err_mask=%x\n", err_mask
);
1260 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
1261 * @dev: Device to read log page 10h from
1262 * @tag: Resulting tag of the failed command
1263 * @tf: Resulting taskfile registers of the failed command
1265 * Read log page 10h to obtain NCQ error details and clear error
1269 * Kernel thread context (may sleep).
1272 * 0 on success, -errno otherwise.
1274 static int ata_eh_read_log_10h(struct ata_device
*dev
,
1275 int *tag
, struct ata_taskfile
*tf
)
1277 u8
*buf
= dev
->link
->ap
->sector_buf
;
1278 unsigned int err_mask
;
1282 err_mask
= ata_read_log_page(dev
, ATA_LOG_SATA_NCQ
, buf
, 1);
1287 for (i
= 0; i
< ATA_SECT_SIZE
; i
++)
1290 ata_dev_printk(dev
, KERN_WARNING
,
1291 "invalid checksum 0x%x on log page 10h\n", csum
);
1296 *tag
= buf
[0] & 0x1f;
1298 tf
->command
= buf
[2];
1299 tf
->feature
= buf
[3];
1303 tf
->device
= buf
[7];
1304 tf
->hob_lbal
= buf
[8];
1305 tf
->hob_lbam
= buf
[9];
1306 tf
->hob_lbah
= buf
[10];
1307 tf
->nsect
= buf
[12];
1308 tf
->hob_nsect
= buf
[13];
1314 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1315 * @dev: device to perform REQUEST_SENSE to
1316 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
1318 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
1319 * SENSE. This function is EH helper.
1322 * Kernel thread context (may sleep).
1325 * 0 on success, AC_ERR_* mask on failure
1327 static unsigned int atapi_eh_request_sense(struct ata_queued_cmd
*qc
)
1329 struct ata_device
*dev
= qc
->dev
;
1330 unsigned char *sense_buf
= qc
->scsicmd
->sense_buffer
;
1331 struct ata_port
*ap
= dev
->link
->ap
;
1332 struct ata_taskfile tf
;
1333 u8 cdb
[ATAPI_CDB_LEN
];
1335 DPRINTK("ATAPI request sense\n");
1337 /* FIXME: is this needed? */
1338 memset(sense_buf
, 0, SCSI_SENSE_BUFFERSIZE
);
1340 /* initialize sense_buf with the error register,
1341 * for the case where they are -not- overwritten
1343 sense_buf
[0] = 0x70;
1344 sense_buf
[2] = qc
->result_tf
.feature
>> 4;
1346 /* some devices time out if garbage left in tf */
1347 ata_tf_init(dev
, &tf
);
1349 memset(cdb
, 0, ATAPI_CDB_LEN
);
1350 cdb
[0] = REQUEST_SENSE
;
1351 cdb
[4] = SCSI_SENSE_BUFFERSIZE
;
1353 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
1354 tf
.command
= ATA_CMD_PACKET
;
1356 /* is it pointless to prefer PIO for "safety reasons"? */
1357 if (ap
->flags
& ATA_FLAG_PIO_DMA
) {
1358 tf
.protocol
= ATA_PROT_ATAPI_DMA
;
1359 tf
.feature
|= ATAPI_PKT_DMA
;
1361 tf
.protocol
= ATA_PROT_ATAPI
;
1362 tf
.lbam
= (8 * 1024) & 0xff;
1363 tf
.lbah
= (8 * 1024) >> 8;
1366 return ata_exec_internal(dev
, &tf
, cdb
, DMA_FROM_DEVICE
,
1367 sense_buf
, SCSI_SENSE_BUFFERSIZE
, 0);
1371 * ata_eh_analyze_serror - analyze SError for a failed port
1372 * @link: ATA link to analyze SError for
1374 * Analyze SError if available and further determine cause of
1380 static void ata_eh_analyze_serror(struct ata_link
*link
)
1382 struct ata_eh_context
*ehc
= &link
->eh_context
;
1383 u32 serror
= ehc
->i
.serror
;
1384 unsigned int err_mask
= 0, action
= 0;
1387 if (serror
& SERR_PERSISTENT
) {
1388 err_mask
|= AC_ERR_ATA_BUS
;
1389 action
|= ATA_EH_HARDRESET
;
1392 (SERR_DATA_RECOVERED
| SERR_COMM_RECOVERED
| SERR_DATA
)) {
1393 err_mask
|= AC_ERR_ATA_BUS
;
1394 action
|= ATA_EH_SOFTRESET
;
1396 if (serror
& SERR_PROTOCOL
) {
1397 err_mask
|= AC_ERR_HSM
;
1398 action
|= ATA_EH_SOFTRESET
;
1400 if (serror
& SERR_INTERNAL
) {
1401 err_mask
|= AC_ERR_SYSTEM
;
1402 action
|= ATA_EH_HARDRESET
;
1405 /* Determine whether a hotplug event has occurred. Both
1406 * SError.N/X are considered hotplug events for enabled or
1407 * host links. For disabled PMP links, only N bit is
1408 * considered as X bit is left at 1 for link plugging.
1412 if (!(link
->flags
& ATA_LFLAG_DISABLED
) || ata_is_host_link(link
))
1413 hotplug_mask
= SERR_PHYRDY_CHG
| SERR_DEV_XCHG
;
1415 hotplug_mask
= SERR_PHYRDY_CHG
;
1417 if (serror
& hotplug_mask
)
1418 ata_ehi_hotplugged(&ehc
->i
);
1420 ehc
->i
.err_mask
|= err_mask
;
1421 ehc
->i
.action
|= action
;
1425 * ata_eh_analyze_ncq_error - analyze NCQ error
1426 * @link: ATA link to analyze NCQ error for
1428 * Read log page 10h, determine the offending qc and acquire
1429 * error status TF. For NCQ device errors, all LLDDs have to do
1430 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1434 * Kernel thread context (may sleep).
1436 static void ata_eh_analyze_ncq_error(struct ata_link
*link
)
1438 struct ata_port
*ap
= link
->ap
;
1439 struct ata_eh_context
*ehc
= &link
->eh_context
;
1440 struct ata_device
*dev
= link
->device
;
1441 struct ata_queued_cmd
*qc
;
1442 struct ata_taskfile tf
;
1445 /* if frozen, we can't do much */
1446 if (ap
->pflags
& ATA_PFLAG_FROZEN
)
1449 /* is it NCQ device error? */
1450 if (!link
->sactive
|| !(ehc
->i
.err_mask
& AC_ERR_DEV
))
1453 /* has LLDD analyzed already? */
1454 for (tag
= 0; tag
< ATA_MAX_QUEUE
; tag
++) {
1455 qc
= __ata_qc_from_tag(ap
, tag
);
1457 if (!(qc
->flags
& ATA_QCFLAG_FAILED
))
1464 /* okay, this error is ours */
1465 rc
= ata_eh_read_log_10h(dev
, &tag
, &tf
);
1467 ata_link_printk(link
, KERN_ERR
, "failed to read log page 10h "
1468 "(errno=%d)\n", rc
);
1472 if (!(link
->sactive
& (1 << tag
))) {
1473 ata_link_printk(link
, KERN_ERR
, "log page 10h reported "
1474 "inactive tag %d\n", tag
);
1478 /* we've got the perpetrator, condemn it */
1479 qc
= __ata_qc_from_tag(ap
, tag
);
1480 memcpy(&qc
->result_tf
, &tf
, sizeof(tf
));
1481 qc
->err_mask
|= AC_ERR_DEV
| AC_ERR_NCQ
;
1482 ehc
->i
.err_mask
&= ~AC_ERR_DEV
;
1486 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1487 * @qc: qc to analyze
1488 * @tf: Taskfile registers to analyze
1490 * Analyze taskfile of @qc and further determine cause of
1491 * failure. This function also requests ATAPI sense data if
1495 * Kernel thread context (may sleep).
1498 * Determined recovery action
1500 static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd
*qc
,
1501 const struct ata_taskfile
*tf
)
1503 unsigned int tmp
, action
= 0;
1504 u8 stat
= tf
->command
, err
= tf
->feature
;
1506 if ((stat
& (ATA_BUSY
| ATA_DRQ
| ATA_DRDY
)) != ATA_DRDY
) {
1507 qc
->err_mask
|= AC_ERR_HSM
;
1508 return ATA_EH_SOFTRESET
;
1511 if (stat
& (ATA_ERR
| ATA_DF
))
1512 qc
->err_mask
|= AC_ERR_DEV
;
1516 switch (qc
->dev
->class) {
1519 qc
->err_mask
|= AC_ERR_ATA_BUS
;
1521 qc
->err_mask
|= AC_ERR_MEDIA
;
1523 qc
->err_mask
|= AC_ERR_INVALID
;
1527 if (!(qc
->ap
->pflags
& ATA_PFLAG_FROZEN
)) {
1528 tmp
= atapi_eh_request_sense(qc
);
1530 /* ATA_QCFLAG_SENSE_VALID is used to
1531 * tell atapi_qc_complete() that sense
1532 * data is already valid.
1534 * TODO: interpret sense data and set
1535 * appropriate err_mask.
1537 qc
->flags
|= ATA_QCFLAG_SENSE_VALID
;
1539 qc
->err_mask
|= tmp
;
1543 if (qc
->err_mask
& (AC_ERR_HSM
| AC_ERR_TIMEOUT
| AC_ERR_ATA_BUS
))
1544 action
|= ATA_EH_SOFTRESET
;
1549 static int ata_eh_categorize_error(int is_io
, unsigned int err_mask
)
1551 if (err_mask
& AC_ERR_ATA_BUS
)
1554 if (err_mask
& AC_ERR_TIMEOUT
)
1558 if (err_mask
& AC_ERR_HSM
)
1561 (AC_ERR_DEV
|AC_ERR_MEDIA
|AC_ERR_INVALID
)) == AC_ERR_DEV
)
1568 struct speed_down_verdict_arg
{
1573 static int speed_down_verdict_cb(struct ata_ering_entry
*ent
, void *void_arg
)
1575 struct speed_down_verdict_arg
*arg
= void_arg
;
1576 int cat
= ata_eh_categorize_error(ent
->is_io
, ent
->err_mask
);
1578 if (ent
->timestamp
< arg
->since
)
1581 arg
->nr_errors
[cat
]++;
1586 * ata_eh_speed_down_verdict - Determine speed down verdict
1587 * @dev: Device of interest
1589 * This function examines error ring of @dev and determines
1590 * whether NCQ needs to be turned off, transfer speed should be
1591 * stepped down, or falling back to PIO is necessary.
1593 * Cat-1 is ATA_BUS error for any command.
1595 * Cat-2 is TIMEOUT for any command or HSM violation for known
1596 * supported commands.
1598 * Cat-3 is is unclassified DEV error for known supported
1601 * NCQ needs to be turned off if there have been more than 3
1602 * Cat-2 + Cat-3 errors during last 10 minutes.
1604 * Speed down is necessary if there have been more than 3 Cat-1 +
1605 * Cat-2 errors or 10 Cat-3 errors during last 10 minutes.
1607 * Falling back to PIO mode is necessary if there have been more
1608 * than 10 Cat-1 + Cat-2 + Cat-3 errors during last 5 minutes.
1611 * Inherited from caller.
1614 * OR of ATA_EH_SPDN_* flags.
1616 static unsigned int ata_eh_speed_down_verdict(struct ata_device
*dev
)
1618 const u64 j5mins
= 5LLU * 60 * HZ
, j10mins
= 10LLU * 60 * HZ
;
1619 u64 j64
= get_jiffies_64();
1620 struct speed_down_verdict_arg arg
;
1621 unsigned int verdict
= 0;
1623 /* scan past 10 mins of error history */
1624 memset(&arg
, 0, sizeof(arg
));
1625 arg
.since
= j64
- min(j64
, j10mins
);
1626 ata_ering_map(&dev
->ering
, speed_down_verdict_cb
, &arg
);
1628 if (arg
.nr_errors
[2] + arg
.nr_errors
[3] > 3)
1629 verdict
|= ATA_EH_SPDN_NCQ_OFF
;
1630 if (arg
.nr_errors
[1] + arg
.nr_errors
[2] > 3 || arg
.nr_errors
[3] > 10)
1631 verdict
|= ATA_EH_SPDN_SPEED_DOWN
;
1633 /* scan past 3 mins of error history */
1634 memset(&arg
, 0, sizeof(arg
));
1635 arg
.since
= j64
- min(j64
, j5mins
);
1636 ata_ering_map(&dev
->ering
, speed_down_verdict_cb
, &arg
);
1638 if (arg
.nr_errors
[1] + arg
.nr_errors
[2] + arg
.nr_errors
[3] > 10)
1639 verdict
|= ATA_EH_SPDN_FALLBACK_TO_PIO
;
1645 * ata_eh_speed_down - record error and speed down if necessary
1646 * @dev: Failed device
1647 * @is_io: Did the device fail during normal IO?
1648 * @err_mask: err_mask of the error
1650 * Record error and examine error history to determine whether
1651 * adjusting transmission speed is necessary. It also sets
1652 * transmission limits appropriately if such adjustment is
1656 * Kernel thread context (may sleep).
1659 * Determined recovery action.
1661 static unsigned int ata_eh_speed_down(struct ata_device
*dev
, int is_io
,
1662 unsigned int err_mask
)
1664 unsigned int verdict
;
1665 unsigned int action
= 0;
1667 /* don't bother if Cat-0 error */
1668 if (ata_eh_categorize_error(is_io
, err_mask
) == 0)
1671 /* record error and determine whether speed down is necessary */
1672 ata_ering_record(&dev
->ering
, is_io
, err_mask
);
1673 verdict
= ata_eh_speed_down_verdict(dev
);
1676 if ((verdict
& ATA_EH_SPDN_NCQ_OFF
) &&
1677 (dev
->flags
& (ATA_DFLAG_PIO
| ATA_DFLAG_NCQ
|
1678 ATA_DFLAG_NCQ_OFF
)) == ATA_DFLAG_NCQ
) {
1679 dev
->flags
|= ATA_DFLAG_NCQ_OFF
;
1680 ata_dev_printk(dev
, KERN_WARNING
,
1681 "NCQ disabled due to excessive errors\n");
1686 if (verdict
& ATA_EH_SPDN_SPEED_DOWN
) {
1687 /* speed down SATA link speed if possible */
1688 if (sata_down_spd_limit(dev
->link
) == 0) {
1689 action
|= ATA_EH_HARDRESET
;
1693 /* lower transfer mode */
1694 if (dev
->spdn_cnt
< 2) {
1695 static const int dma_dnxfer_sel
[] =
1696 { ATA_DNXFER_DMA
, ATA_DNXFER_40C
};
1697 static const int pio_dnxfer_sel
[] =
1698 { ATA_DNXFER_PIO
, ATA_DNXFER_FORCE_PIO0
};
1701 if (dev
->xfer_shift
!= ATA_SHIFT_PIO
)
1702 sel
= dma_dnxfer_sel
[dev
->spdn_cnt
];
1704 sel
= pio_dnxfer_sel
[dev
->spdn_cnt
];
1708 if (ata_down_xfermask_limit(dev
, sel
) == 0) {
1709 action
|= ATA_EH_SOFTRESET
;
1715 /* Fall back to PIO? Slowing down to PIO is meaningless for
1716 * SATA. Consider it only for PATA.
1718 if ((verdict
& ATA_EH_SPDN_FALLBACK_TO_PIO
) && (dev
->spdn_cnt
>= 2) &&
1719 (dev
->link
->ap
->cbl
!= ATA_CBL_SATA
) &&
1720 (dev
->xfer_shift
!= ATA_SHIFT_PIO
)) {
1721 if (ata_down_xfermask_limit(dev
, ATA_DNXFER_FORCE_PIO
) == 0) {
1723 action
|= ATA_EH_SOFTRESET
;
1730 /* device has been slowed down, blow error history */
1731 ata_ering_clear(&dev
->ering
);
1736 * ata_eh_link_autopsy - analyze error and determine recovery action
1737 * @link: host link to perform autopsy on
1739 * Analyze why @link failed and determine which recovery actions
1740 * are needed. This function also sets more detailed AC_ERR_*
1741 * values and fills sense data for ATAPI CHECK SENSE.
1744 * Kernel thread context (may sleep).
1746 static void ata_eh_link_autopsy(struct ata_link
*link
)
1748 struct ata_port
*ap
= link
->ap
;
1749 struct ata_eh_context
*ehc
= &link
->eh_context
;
1750 unsigned int all_err_mask
= 0;
1757 if (ehc
->i
.flags
& ATA_EHI_NO_AUTOPSY
)
1760 /* obtain and analyze SError */
1761 rc
= sata_scr_read(link
, SCR_ERROR
, &serror
);
1763 ehc
->i
.serror
|= serror
;
1764 ata_eh_analyze_serror(link
);
1765 } else if (rc
!= -EOPNOTSUPP
) {
1766 /* SError read failed, force hardreset and probing */
1767 ata_ehi_schedule_probe(&ehc
->i
);
1768 ehc
->i
.action
|= ATA_EH_HARDRESET
;
1769 ehc
->i
.err_mask
|= AC_ERR_OTHER
;
1772 /* analyze NCQ failure */
1773 ata_eh_analyze_ncq_error(link
);
1775 /* any real error trumps AC_ERR_OTHER */
1776 if (ehc
->i
.err_mask
& ~AC_ERR_OTHER
)
1777 ehc
->i
.err_mask
&= ~AC_ERR_OTHER
;
1779 all_err_mask
|= ehc
->i
.err_mask
;
1781 for (tag
= 0; tag
< ATA_MAX_QUEUE
; tag
++) {
1782 struct ata_queued_cmd
*qc
= __ata_qc_from_tag(ap
, tag
);
1784 if (!(qc
->flags
& ATA_QCFLAG_FAILED
) || qc
->dev
->link
!= link
)
1787 /* inherit upper level err_mask */
1788 qc
->err_mask
|= ehc
->i
.err_mask
;
1791 ehc
->i
.action
|= ata_eh_analyze_tf(qc
, &qc
->result_tf
);
1793 /* DEV errors are probably spurious in case of ATA_BUS error */
1794 if (qc
->err_mask
& AC_ERR_ATA_BUS
)
1795 qc
->err_mask
&= ~(AC_ERR_DEV
| AC_ERR_MEDIA
|
1798 /* any real error trumps unknown error */
1799 if (qc
->err_mask
& ~AC_ERR_OTHER
)
1800 qc
->err_mask
&= ~AC_ERR_OTHER
;
1802 /* SENSE_VALID trumps dev/unknown error and revalidation */
1803 if (qc
->flags
& ATA_QCFLAG_SENSE_VALID
) {
1804 qc
->err_mask
&= ~(AC_ERR_DEV
| AC_ERR_OTHER
);
1805 ehc
->i
.action
&= ~ATA_EH_REVALIDATE
;
1808 /* accumulate error info */
1809 ehc
->i
.dev
= qc
->dev
;
1810 all_err_mask
|= qc
->err_mask
;
1811 if (qc
->flags
& ATA_QCFLAG_IO
)
1815 /* enforce default EH actions */
1816 if (ap
->pflags
& ATA_PFLAG_FROZEN
||
1817 all_err_mask
& (AC_ERR_HSM
| AC_ERR_TIMEOUT
))
1818 ehc
->i
.action
|= ATA_EH_SOFTRESET
;
1819 else if (all_err_mask
)
1820 ehc
->i
.action
|= ATA_EH_REVALIDATE
;
1822 /* if we have offending qcs and the associated failed device */
1825 ehc
->i
.action
|= ata_eh_speed_down(ehc
->i
.dev
, is_io
,
1828 /* perform per-dev EH action only on the offending device */
1829 ehc
->i
.dev_action
[ehc
->i
.dev
->devno
] |=
1830 ehc
->i
.action
& ATA_EH_PERDEV_MASK
;
1831 ehc
->i
.action
&= ~ATA_EH_PERDEV_MASK
;
1838 * ata_eh_autopsy - analyze error and determine recovery action
1839 * @ap: host port to perform autopsy on
1841 * Analyze all links of @ap and determine why they failed and
1842 * which recovery actions are needed.
1845 * Kernel thread context (may sleep).
1847 void ata_eh_autopsy(struct ata_port
*ap
)
1849 struct ata_link
*link
;
1851 __ata_port_for_each_link(link
, ap
)
1852 ata_eh_link_autopsy(link
);
1856 * ata_eh_link_report - report error handling to user
1857 * @link: ATA link EH is going on
1859 * Report EH to user.
1864 static void ata_eh_link_report(struct ata_link
*link
)
1866 struct ata_port
*ap
= link
->ap
;
1867 struct ata_eh_context
*ehc
= &link
->eh_context
;
1868 const char *frozen
, *desc
;
1870 int tag
, nr_failed
= 0;
1872 if (ehc
->i
.flags
& ATA_EHI_QUIET
)
1876 if (ehc
->i
.desc
[0] != '\0')
1879 for (tag
= 0; tag
< ATA_MAX_QUEUE
; tag
++) {
1880 struct ata_queued_cmd
*qc
= __ata_qc_from_tag(ap
, tag
);
1882 if (!(qc
->flags
& ATA_QCFLAG_FAILED
) || qc
->dev
->link
!= link
)
1884 if (qc
->flags
& ATA_QCFLAG_SENSE_VALID
&& !qc
->err_mask
)
1890 if (!nr_failed
&& !ehc
->i
.err_mask
)
1894 if (ap
->pflags
& ATA_PFLAG_FROZEN
)
1897 memset(tries_buf
, 0, sizeof(tries_buf
));
1898 if (ap
->eh_tries
< ATA_EH_MAX_TRIES
)
1899 snprintf(tries_buf
, sizeof(tries_buf
) - 1, " t%d",
1903 ata_dev_printk(ehc
->i
.dev
, KERN_ERR
, "exception Emask 0x%x "
1904 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1905 ehc
->i
.err_mask
, link
->sactive
, ehc
->i
.serror
,
1906 ehc
->i
.action
, frozen
, tries_buf
);
1908 ata_dev_printk(ehc
->i
.dev
, KERN_ERR
, "%s\n", desc
);
1910 ata_link_printk(link
, KERN_ERR
, "exception Emask 0x%x "
1911 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1912 ehc
->i
.err_mask
, link
->sactive
, ehc
->i
.serror
,
1913 ehc
->i
.action
, frozen
, tries_buf
);
1915 ata_link_printk(link
, KERN_ERR
, "%s\n", desc
);
1919 ata_port_printk(ap
, KERN_ERR
,
1920 "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
1921 ehc
->i
.serror
& SERR_DATA_RECOVERED
? "RecovData " : "",
1922 ehc
->i
.serror
& SERR_COMM_RECOVERED
? "RecovComm " : "",
1923 ehc
->i
.serror
& SERR_DATA
? "UnrecovData " : "",
1924 ehc
->i
.serror
& SERR_PERSISTENT
? "Persist " : "",
1925 ehc
->i
.serror
& SERR_PROTOCOL
? "Proto " : "",
1926 ehc
->i
.serror
& SERR_INTERNAL
? "HostInt " : "",
1927 ehc
->i
.serror
& SERR_PHYRDY_CHG
? "PHYRdyChg " : "",
1928 ehc
->i
.serror
& SERR_PHY_INT_ERR
? "PHYInt " : "",
1929 ehc
->i
.serror
& SERR_COMM_WAKE
? "CommWake " : "",
1930 ehc
->i
.serror
& SERR_10B_8B_ERR
? "10B8B " : "",
1931 ehc
->i
.serror
& SERR_DISPARITY
? "Dispar " : "",
1932 ehc
->i
.serror
& SERR_CRC
? "BadCRC " : "",
1933 ehc
->i
.serror
& SERR_HANDSHAKE
? "Handshk " : "",
1934 ehc
->i
.serror
& SERR_LINK_SEQ_ERR
? "LinkSeq " : "",
1935 ehc
->i
.serror
& SERR_TRANS_ST_ERROR
? "TrStaTrns " : "",
1936 ehc
->i
.serror
& SERR_UNRECOG_FIS
? "UnrecFIS " : "",
1937 ehc
->i
.serror
& SERR_DEV_XCHG
? "DevExch " : "" );
1939 for (tag
= 0; tag
< ATA_MAX_QUEUE
; tag
++) {
1940 static const char *dma_str
[] = {
1941 [DMA_BIDIRECTIONAL
] = "bidi",
1942 [DMA_TO_DEVICE
] = "out",
1943 [DMA_FROM_DEVICE
] = "in",
1946 struct ata_queued_cmd
*qc
= __ata_qc_from_tag(ap
, tag
);
1947 struct ata_taskfile
*cmd
= &qc
->tf
, *res
= &qc
->result_tf
;
1949 if (!(qc
->flags
& ATA_QCFLAG_FAILED
) ||
1950 qc
->dev
->link
!= link
|| !qc
->err_mask
)
1953 ata_dev_printk(qc
->dev
, KERN_ERR
,
1954 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
1955 "tag %d cdb 0x%x data %u %s\n "
1956 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
1957 "Emask 0x%x (%s)%s\n",
1958 cmd
->command
, cmd
->feature
, cmd
->nsect
,
1959 cmd
->lbal
, cmd
->lbam
, cmd
->lbah
,
1960 cmd
->hob_feature
, cmd
->hob_nsect
,
1961 cmd
->hob_lbal
, cmd
->hob_lbam
, cmd
->hob_lbah
,
1962 cmd
->device
, qc
->tag
, qc
->cdb
[0], qc
->nbytes
,
1963 dma_str
[qc
->dma_dir
],
1964 res
->command
, res
->feature
, res
->nsect
,
1965 res
->lbal
, res
->lbam
, res
->lbah
,
1966 res
->hob_feature
, res
->hob_nsect
,
1967 res
->hob_lbal
, res
->hob_lbam
, res
->hob_lbah
,
1968 res
->device
, qc
->err_mask
, ata_err_string(qc
->err_mask
),
1969 qc
->err_mask
& AC_ERR_NCQ
? " <F>" : "");
1971 if (res
->command
& (ATA_BUSY
| ATA_DRDY
| ATA_DF
| ATA_DRQ
|
1973 if (res
->command
& ATA_BUSY
)
1974 ata_dev_printk(qc
->dev
, KERN_ERR
,
1975 "status: { Busy }\n" );
1977 ata_dev_printk(qc
->dev
, KERN_ERR
,
1978 "status: { %s%s%s%s}\n",
1979 res
->command
& ATA_DRDY
? "DRDY " : "",
1980 res
->command
& ATA_DF
? "DF " : "",
1981 res
->command
& ATA_DRQ
? "DRQ " : "",
1982 res
->command
& ATA_ERR
? "ERR " : "" );
1985 if (cmd
->command
!= ATA_CMD_PACKET
&&
1986 (res
->feature
& (ATA_ICRC
| ATA_UNC
| ATA_IDNF
|
1988 ata_dev_printk(qc
->dev
, KERN_ERR
,
1989 "error: { %s%s%s%s}\n",
1990 res
->feature
& ATA_ICRC
? "ICRC " : "",
1991 res
->feature
& ATA_UNC
? "UNC " : "",
1992 res
->feature
& ATA_IDNF
? "IDNF " : "",
1993 res
->feature
& ATA_ABORTED
? "ABRT " : "" );
1998 * ata_eh_report - report error handling to user
1999 * @ap: ATA port to report EH about
2001 * Report EH to user.
2006 void ata_eh_report(struct ata_port
*ap
)
2008 struct ata_link
*link
;
2010 __ata_port_for_each_link(link
, ap
)
2011 ata_eh_link_report(link
);
2014 static int ata_do_reset(struct ata_link
*link
, ata_reset_fn_t reset
,
2015 unsigned int *classes
, unsigned long deadline
)
2017 struct ata_device
*dev
;
2020 ata_link_for_each_dev(dev
, link
)
2021 classes
[dev
->devno
] = ATA_DEV_UNKNOWN
;
2023 rc
= reset(link
, classes
, deadline
);
2027 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2028 * is complete and convert all ATA_DEV_UNKNOWN to
2031 ata_link_for_each_dev(dev
, link
)
2032 if (classes
[dev
->devno
] != ATA_DEV_UNKNOWN
)
2036 ata_link_for_each_dev(dev
, link
) {
2037 if (classes
[dev
->devno
] == ATA_DEV_UNKNOWN
)
2038 classes
[dev
->devno
] = ATA_DEV_NONE
;
2045 static int ata_eh_followup_srst_needed(struct ata_link
*link
,
2046 int rc
, int classify
,
2047 const unsigned int *classes
)
2049 if (link
->flags
& ATA_LFLAG_NO_SRST
)
2055 if ((link
->ap
->flags
& ATA_FLAG_PMP
) && ata_is_host_link(link
))
2057 if (classify
&& !(link
->flags
& ATA_LFLAG_ASSUME_CLASS
) &&
2058 classes
[0] == ATA_DEV_UNKNOWN
)
2063 int ata_eh_reset(struct ata_link
*link
, int classify
,
2064 ata_prereset_fn_t prereset
, ata_reset_fn_t softreset
,
2065 ata_reset_fn_t hardreset
, ata_postreset_fn_t postreset
)
2067 struct ata_port
*ap
= link
->ap
;
2068 struct ata_eh_context
*ehc
= &link
->eh_context
;
2069 unsigned int *classes
= ehc
->classes
;
2070 int verbose
= !(ehc
->i
.flags
& ATA_EHI_QUIET
);
2072 struct ata_device
*dev
;
2073 unsigned long deadline
;
2074 unsigned int action
;
2075 ata_reset_fn_t reset
;
2076 unsigned long flags
;
2079 /* about to reset */
2080 spin_lock_irqsave(ap
->lock
, flags
);
2081 ap
->pflags
|= ATA_PFLAG_RESETTING
;
2082 spin_unlock_irqrestore(ap
->lock
, flags
);
2084 ata_eh_about_to_do(link
, NULL
, ehc
->i
.action
& ATA_EH_RESET_MASK
);
2086 /* Determine which reset to use and record in ehc->i.action.
2087 * prereset() may examine and modify it.
2089 action
= ehc
->i
.action
;
2090 ehc
->i
.action
&= ~ATA_EH_RESET_MASK
;
2091 if (softreset
&& (!hardreset
|| (!(link
->flags
& ATA_LFLAG_NO_SRST
) &&
2092 !sata_set_spd_needed(link
) &&
2093 !(action
& ATA_EH_HARDRESET
))))
2094 ehc
->i
.action
|= ATA_EH_SOFTRESET
;
2096 ehc
->i
.action
|= ATA_EH_HARDRESET
;
2099 rc
= prereset(link
, jiffies
+ ATA_EH_PRERESET_TIMEOUT
);
2101 if (rc
== -ENOENT
) {
2102 ata_link_printk(link
, KERN_DEBUG
,
2103 "port disabled. ignoring.\n");
2104 ehc
->i
.action
&= ~ATA_EH_RESET_MASK
;
2106 ata_link_for_each_dev(dev
, link
)
2107 classes
[dev
->devno
] = ATA_DEV_NONE
;
2111 ata_link_printk(link
, KERN_ERR
,
2112 "prereset failed (errno=%d)\n", rc
);
2117 /* prereset() might have modified ehc->i.action */
2118 if (ehc
->i
.action
& ATA_EH_HARDRESET
)
2120 else if (ehc
->i
.action
& ATA_EH_SOFTRESET
)
2123 /* prereset told us not to reset, bang classes and return */
2124 ata_link_for_each_dev(dev
, link
)
2125 classes
[dev
->devno
] = ATA_DEV_NONE
;
2130 /* did prereset() screw up? if so, fix up to avoid oopsing */
2139 deadline
= jiffies
+ ata_eh_reset_timeouts
[try++];
2141 /* shut up during boot probing */
2143 ata_link_printk(link
, KERN_INFO
, "%s resetting link\n",
2144 reset
== softreset
? "soft" : "hard");
2146 /* mark that this EH session started with reset */
2147 if (reset
== hardreset
)
2148 ehc
->i
.flags
|= ATA_EHI_DID_HARDRESET
;
2150 ehc
->i
.flags
|= ATA_EHI_DID_SOFTRESET
;
2152 rc
= ata_do_reset(link
, reset
, classes
, deadline
);
2154 if (reset
== hardreset
&&
2155 ata_eh_followup_srst_needed(link
, rc
, classify
, classes
)) {
2156 /* okay, let's do follow-up softreset */
2160 ata_link_printk(link
, KERN_ERR
,
2161 "follow-up softreset required "
2162 "but no softreset avaliable\n");
2167 ata_eh_about_to_do(link
, NULL
, ATA_EH_RESET_MASK
);
2168 rc
= ata_do_reset(link
, reset
, classes
, deadline
);
2170 if (rc
== 0 && classify
&& classes
[0] == ATA_DEV_UNKNOWN
&&
2171 !(link
->flags
& ATA_LFLAG_ASSUME_CLASS
)) {
2172 ata_link_printk(link
, KERN_ERR
,
2173 "classification failed\n");
2179 /* if we skipped follow-up srst, clear rc */
2183 if (rc
&& rc
!= -ERESTART
&& try < ARRAY_SIZE(ata_eh_reset_timeouts
)) {
2184 unsigned long now
= jiffies
;
2186 if (time_before(now
, deadline
)) {
2187 unsigned long delta
= deadline
- jiffies
;
2189 ata_link_printk(link
, KERN_WARNING
, "reset failed "
2190 "(errno=%d), retrying in %u secs\n",
2191 rc
, (jiffies_to_msecs(delta
) + 999) / 1000);
2194 delta
= schedule_timeout_uninterruptible(delta
);
2198 try == ARRAY_SIZE(ata_eh_reset_timeouts
) - 1)
2199 sata_down_spd_limit(link
);
2208 ata_link_for_each_dev(dev
, link
) {
2209 /* After the reset, the device state is PIO 0
2210 * and the controller state is undefined.
2213 dev
->pio_mode
= XFER_PIO_0
;
2215 if (ata_link_offline(link
))
2218 /* apply class override and convert UNKNOWN to NONE */
2219 if (link
->flags
& ATA_LFLAG_ASSUME_ATA
)
2220 classes
[dev
->devno
] = ATA_DEV_ATA
;
2221 else if (link
->flags
& ATA_LFLAG_ASSUME_SEMB
)
2222 classes
[dev
->devno
] = ATA_DEV_SEMB_UNSUP
; /* not yet */
2223 else if (classes
[dev
->devno
] == ATA_DEV_UNKNOWN
)
2224 classes
[dev
->devno
] = ATA_DEV_NONE
;
2227 /* record current link speed */
2228 if (sata_scr_read(link
, SCR_STATUS
, &sstatus
) == 0)
2229 link
->sata_spd
= (sstatus
>> 4) & 0xf;
2232 postreset(link
, classes
);
2234 /* reset successful, schedule revalidation */
2235 ata_eh_done(link
, NULL
, ehc
->i
.action
& ATA_EH_RESET_MASK
);
2236 ehc
->i
.action
|= ATA_EH_REVALIDATE
;
2239 /* clear hotplug flag */
2240 ehc
->i
.flags
&= ~ATA_EHI_HOTPLUGGED
;
2242 spin_lock_irqsave(ap
->lock
, flags
);
2243 ap
->pflags
&= ~ATA_PFLAG_RESETTING
;
2244 spin_unlock_irqrestore(ap
->lock
, flags
);
2249 static int ata_eh_revalidate_and_attach(struct ata_link
*link
,
2250 struct ata_device
**r_failed_dev
)
2252 struct ata_port
*ap
= link
->ap
;
2253 struct ata_eh_context
*ehc
= &link
->eh_context
;
2254 struct ata_device
*dev
;
2255 unsigned int new_mask
= 0;
2256 unsigned long flags
;
2261 /* For PATA drive side cable detection to work, IDENTIFY must
2262 * be done backwards such that PDIAG- is released by the slave
2263 * device before the master device is identified.
2265 ata_link_for_each_dev_reverse(dev
, link
) {
2266 unsigned int action
= ata_eh_dev_action(dev
);
2267 unsigned int readid_flags
= 0;
2269 if (ehc
->i
.flags
& ATA_EHI_DID_RESET
)
2270 readid_flags
|= ATA_READID_POSTRESET
;
2272 if ((action
& ATA_EH_REVALIDATE
) && ata_dev_enabled(dev
)) {
2273 WARN_ON(dev
->class == ATA_DEV_PMP
);
2275 if (ata_link_offline(link
)) {
2280 ata_eh_about_to_do(link
, dev
, ATA_EH_REVALIDATE
);
2281 rc
= ata_dev_revalidate(dev
, ehc
->classes
[dev
->devno
],
2286 ata_eh_done(link
, dev
, ATA_EH_REVALIDATE
);
2288 /* Configuration may have changed, reconfigure
2291 ehc
->i
.flags
|= ATA_EHI_SETMODE
;
2293 /* schedule the scsi_rescan_device() here */
2294 queue_work(ata_aux_wq
, &(ap
->scsi_rescan_task
));
2295 } else if (dev
->class == ATA_DEV_UNKNOWN
&&
2296 ehc
->tries
[dev
->devno
] &&
2297 ata_class_enabled(ehc
->classes
[dev
->devno
])) {
2298 dev
->class = ehc
->classes
[dev
->devno
];
2300 if (dev
->class == ATA_DEV_PMP
)
2301 rc
= sata_pmp_attach(dev
);
2303 rc
= ata_dev_read_id(dev
, &dev
->class,
2304 readid_flags
, dev
->id
);
2307 new_mask
|= 1 << dev
->devno
;
2310 /* IDENTIFY was issued to non-existent
2311 * device. No need to reset. Just
2312 * thaw and kill the device.
2314 ata_eh_thaw_port(ap
);
2315 dev
->class = ATA_DEV_UNKNOWN
;
2318 dev
->class = ATA_DEV_UNKNOWN
;
2324 /* PDIAG- should have been released, ask cable type if post-reset */
2325 if (ata_is_host_link(link
) && ap
->ops
->cable_detect
&&
2326 (ehc
->i
.flags
& ATA_EHI_DID_RESET
))
2327 ap
->cbl
= ap
->ops
->cable_detect(ap
);
2329 /* Configure new devices forward such that user doesn't see
2330 * device detection messages backwards.
2332 ata_link_for_each_dev(dev
, link
) {
2333 if (!(new_mask
& (1 << dev
->devno
)) ||
2334 dev
->class == ATA_DEV_PMP
)
2337 ehc
->i
.flags
|= ATA_EHI_PRINTINFO
;
2338 rc
= ata_dev_configure(dev
);
2339 ehc
->i
.flags
&= ~ATA_EHI_PRINTINFO
;
2343 spin_lock_irqsave(ap
->lock
, flags
);
2344 ap
->pflags
|= ATA_PFLAG_SCSI_HOTPLUG
;
2345 spin_unlock_irqrestore(ap
->lock
, flags
);
2347 /* new device discovered, configure xfermode */
2348 ehc
->i
.flags
|= ATA_EHI_SETMODE
;
2354 *r_failed_dev
= dev
;
2355 DPRINTK("EXIT rc=%d\n", rc
);
2359 static int ata_link_nr_enabled(struct ata_link
*link
)
2361 struct ata_device
*dev
;
2364 ata_link_for_each_dev(dev
, link
)
2365 if (ata_dev_enabled(dev
))
2370 static int ata_link_nr_vacant(struct ata_link
*link
)
2372 struct ata_device
*dev
;
2375 ata_link_for_each_dev(dev
, link
)
2376 if (dev
->class == ATA_DEV_UNKNOWN
)
2381 static int ata_eh_skip_recovery(struct ata_link
*link
)
2383 struct ata_eh_context
*ehc
= &link
->eh_context
;
2384 struct ata_device
*dev
;
2386 /* skip disabled links */
2387 if (link
->flags
& ATA_LFLAG_DISABLED
)
2390 /* thaw frozen port, resume link and recover failed devices */
2391 if ((link
->ap
->pflags
& ATA_PFLAG_FROZEN
) ||
2392 (ehc
->i
.flags
& ATA_EHI_RESUME_LINK
) || ata_link_nr_enabled(link
))
2395 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
2396 ata_link_for_each_dev(dev
, link
) {
2397 if (dev
->class == ATA_DEV_UNKNOWN
&&
2398 ehc
->classes
[dev
->devno
] != ATA_DEV_NONE
)
2405 static int ata_eh_handle_dev_fail(struct ata_device
*dev
, int err
)
2407 struct ata_eh_context
*ehc
= &dev
->link
->eh_context
;
2409 ehc
->tries
[dev
->devno
]--;
2413 /* device missing or wrong IDENTIFY data, schedule probing */
2414 ehc
->i
.probe_mask
|= (1 << dev
->devno
);
2416 /* give it just one more chance */
2417 ehc
->tries
[dev
->devno
] = min(ehc
->tries
[dev
->devno
], 1);
2419 if (ehc
->tries
[dev
->devno
] == 1) {
2420 /* This is the last chance, better to slow
2421 * down than lose it.
2423 sata_down_spd_limit(dev
->link
);
2424 ata_down_xfermask_limit(dev
, ATA_DNXFER_PIO
);
2428 if (ata_dev_enabled(dev
) && !ehc
->tries
[dev
->devno
]) {
2429 /* disable device if it has used up all its chances */
2430 ata_dev_disable(dev
);
2432 /* detach if offline */
2433 if (ata_link_offline(dev
->link
))
2434 ata_eh_detach_dev(dev
);
2436 /* probe if requested */
2437 if ((ehc
->i
.probe_mask
& (1 << dev
->devno
)) &&
2438 !(ehc
->did_probe_mask
& (1 << dev
->devno
))) {
2439 ata_eh_detach_dev(dev
);
2442 ehc
->tries
[dev
->devno
] = ATA_EH_DEV_TRIES
;
2443 ehc
->did_probe_mask
|= (1 << dev
->devno
);
2444 ehc
->i
.action
|= ATA_EH_SOFTRESET
;
2449 /* soft didn't work? be haaaaard */
2450 if (ehc
->i
.flags
& ATA_EHI_DID_RESET
)
2451 ehc
->i
.action
|= ATA_EH_HARDRESET
;
2453 ehc
->i
.action
|= ATA_EH_SOFTRESET
;
2460 * ata_eh_recover - recover host port after error
2461 * @ap: host port to recover
2462 * @prereset: prereset method (can be NULL)
2463 * @softreset: softreset method (can be NULL)
2464 * @hardreset: hardreset method (can be NULL)
2465 * @postreset: postreset method (can be NULL)
2466 * @r_failed_link: out parameter for failed link
2468 * This is the alpha and omega, eum and yang, heart and soul of
2469 * libata exception handling. On entry, actions required to
2470 * recover each link and hotplug requests are recorded in the
2471 * link's eh_context. This function executes all the operations
2472 * with appropriate retrials and fallbacks to resurrect failed
2473 * devices, detach goners and greet newcomers.
2476 * Kernel thread context (may sleep).
2479 * 0 on success, -errno on failure.
2481 int ata_eh_recover(struct ata_port
*ap
, ata_prereset_fn_t prereset
,
2482 ata_reset_fn_t softreset
, ata_reset_fn_t hardreset
,
2483 ata_postreset_fn_t postreset
,
2484 struct ata_link
**r_failed_link
)
2486 struct ata_link
*link
;
2487 struct ata_device
*dev
;
2488 int nr_failed_devs
, nr_disabled_devs
;
2490 unsigned long flags
;
2494 /* prep for recovery */
2495 ata_port_for_each_link(link
, ap
) {
2496 struct ata_eh_context
*ehc
= &link
->eh_context
;
2498 /* re-enable link? */
2499 if (ehc
->i
.action
& ATA_EH_ENABLE_LINK
) {
2500 ata_eh_about_to_do(link
, NULL
, ATA_EH_ENABLE_LINK
);
2501 spin_lock_irqsave(ap
->lock
, flags
);
2502 link
->flags
&= ~ATA_LFLAG_DISABLED
;
2503 spin_unlock_irqrestore(ap
->lock
, flags
);
2504 ata_eh_done(link
, NULL
, ATA_EH_ENABLE_LINK
);
2507 ata_link_for_each_dev(dev
, link
) {
2508 if (link
->flags
& ATA_LFLAG_NO_RETRY
)
2509 ehc
->tries
[dev
->devno
] = 1;
2511 ehc
->tries
[dev
->devno
] = ATA_EH_DEV_TRIES
;
2513 /* collect port action mask recorded in dev actions */
2514 ehc
->i
.action
|= ehc
->i
.dev_action
[dev
->devno
] &
2515 ~ATA_EH_PERDEV_MASK
;
2516 ehc
->i
.dev_action
[dev
->devno
] &= ATA_EH_PERDEV_MASK
;
2518 /* process hotplug request */
2519 if (dev
->flags
& ATA_DFLAG_DETACH
)
2520 ata_eh_detach_dev(dev
);
2522 if (!ata_dev_enabled(dev
) &&
2523 ((ehc
->i
.probe_mask
& (1 << dev
->devno
)) &&
2524 !(ehc
->did_probe_mask
& (1 << dev
->devno
)))) {
2525 ata_eh_detach_dev(dev
);
2527 ehc
->did_probe_mask
|= (1 << dev
->devno
);
2528 ehc
->i
.action
|= ATA_EH_SOFTRESET
;
2536 nr_disabled_devs
= 0;
2539 /* if UNLOADING, finish immediately */
2540 if (ap
->pflags
& ATA_PFLAG_UNLOADING
)
2544 ata_port_for_each_link(link
, ap
) {
2545 struct ata_eh_context
*ehc
= &link
->eh_context
;
2547 /* skip EH if possible. */
2548 if (ata_eh_skip_recovery(link
))
2551 /* do we need to reset? */
2552 if (ehc
->i
.action
& ATA_EH_RESET_MASK
)
2555 ata_link_for_each_dev(dev
, link
)
2556 ehc
->classes
[dev
->devno
] = ATA_DEV_UNKNOWN
;
2561 /* if PMP is attached, this function only deals with
2562 * downstream links, port should stay thawed.
2564 if (!ap
->nr_pmp_links
)
2565 ata_eh_freeze_port(ap
);
2567 ata_port_for_each_link(link
, ap
) {
2568 struct ata_eh_context
*ehc
= &link
->eh_context
;
2570 if (!(ehc
->i
.action
& ATA_EH_RESET_MASK
))
2573 rc
= ata_eh_reset(link
, ata_link_nr_vacant(link
),
2574 prereset
, softreset
, hardreset
,
2577 ata_link_printk(link
, KERN_ERR
,
2578 "reset failed, giving up\n");
2583 if (!ap
->nr_pmp_links
)
2584 ata_eh_thaw_port(ap
);
2588 ata_port_for_each_link(link
, ap
) {
2589 struct ata_eh_context
*ehc
= &link
->eh_context
;
2591 /* revalidate existing devices and attach new ones */
2592 rc
= ata_eh_revalidate_and_attach(link
, &dev
);
2596 /* if PMP got attached, return, pmp EH will take care of it */
2597 if (link
->device
->class == ATA_DEV_PMP
) {
2602 /* configure transfer mode if necessary */
2603 if (ehc
->i
.flags
& ATA_EHI_SETMODE
) {
2604 rc
= ata_set_mode(link
, &dev
);
2607 ehc
->i
.flags
&= ~ATA_EHI_SETMODE
;
2610 /* this link is okay now */
2616 if (ata_eh_handle_dev_fail(dev
, rc
))
2619 if (ap
->pflags
& ATA_PFLAG_FROZEN
) {
2620 /* PMP reset requires working host port.
2621 * Can't retry if it's frozen.
2623 if (ap
->nr_pmp_links
)
2629 if (nr_failed_devs
) {
2630 if (nr_failed_devs
!= nr_disabled_devs
) {
2631 ata_port_printk(ap
, KERN_WARNING
, "failed to recover "
2632 "some devices, retrying in 5 secs\n");
2635 /* no device left to recover, repeat fast */
2643 if (rc
&& r_failed_link
)
2644 *r_failed_link
= link
;
2646 DPRINTK("EXIT, rc=%d\n", rc
);
2651 * ata_eh_finish - finish up EH
2652 * @ap: host port to finish EH for
2654 * Recovery is complete. Clean up EH states and retry or finish
2660 void ata_eh_finish(struct ata_port
*ap
)
2664 /* retry or finish qcs */
2665 for (tag
= 0; tag
< ATA_MAX_QUEUE
; tag
++) {
2666 struct ata_queued_cmd
*qc
= __ata_qc_from_tag(ap
, tag
);
2668 if (!(qc
->flags
& ATA_QCFLAG_FAILED
))
2672 /* FIXME: Once EH migration is complete,
2673 * generate sense data in this function,
2674 * considering both err_mask and tf.
2676 if (qc
->err_mask
& AC_ERR_INVALID
)
2677 ata_eh_qc_complete(qc
);
2679 ata_eh_qc_retry(qc
);
2681 if (qc
->flags
& ATA_QCFLAG_SENSE_VALID
) {
2682 ata_eh_qc_complete(qc
);
2684 /* feed zero TF to sense generation */
2685 memset(&qc
->result_tf
, 0, sizeof(qc
->result_tf
));
2686 ata_eh_qc_retry(qc
);
2691 /* make sure nr_active_links is zero after EH */
2692 WARN_ON(ap
->nr_active_links
);
2693 ap
->nr_active_links
= 0;
2697 * ata_do_eh - do standard error handling
2698 * @ap: host port to handle error for
2699 * @prereset: prereset method (can be NULL)
2700 * @softreset: softreset method (can be NULL)
2701 * @hardreset: hardreset method (can be NULL)
2702 * @postreset: postreset method (can be NULL)
2704 * Perform standard error handling sequence.
2707 * Kernel thread context (may sleep).
2709 void ata_do_eh(struct ata_port
*ap
, ata_prereset_fn_t prereset
,
2710 ata_reset_fn_t softreset
, ata_reset_fn_t hardreset
,
2711 ata_postreset_fn_t postreset
)
2713 struct ata_device
*dev
;
2719 rc
= ata_eh_recover(ap
, prereset
, softreset
, hardreset
, postreset
,
2722 ata_link_for_each_dev(dev
, &ap
->link
)
2723 ata_dev_disable(dev
);
2731 * ata_eh_handle_port_suspend - perform port suspend operation
2732 * @ap: port to suspend
2737 * Kernel thread context (may sleep).
2739 static void ata_eh_handle_port_suspend(struct ata_port
*ap
)
2741 unsigned long flags
;
2744 /* are we suspending? */
2745 spin_lock_irqsave(ap
->lock
, flags
);
2746 if (!(ap
->pflags
& ATA_PFLAG_PM_PENDING
) ||
2747 ap
->pm_mesg
.event
== PM_EVENT_ON
) {
2748 spin_unlock_irqrestore(ap
->lock
, flags
);
2751 spin_unlock_irqrestore(ap
->lock
, flags
);
2753 WARN_ON(ap
->pflags
& ATA_PFLAG_SUSPENDED
);
2755 /* tell ACPI we're suspending */
2756 rc
= ata_acpi_on_suspend(ap
);
2761 ata_eh_freeze_port(ap
);
2763 if (ap
->ops
->port_suspend
)
2764 rc
= ap
->ops
->port_suspend(ap
, ap
->pm_mesg
);
2768 spin_lock_irqsave(ap
->lock
, flags
);
2770 ap
->pflags
&= ~ATA_PFLAG_PM_PENDING
;
2772 ap
->pflags
|= ATA_PFLAG_SUSPENDED
;
2773 else if (ap
->pflags
& ATA_PFLAG_FROZEN
)
2774 ata_port_schedule_eh(ap
);
2776 if (ap
->pm_result
) {
2777 *ap
->pm_result
= rc
;
2778 ap
->pm_result
= NULL
;
2781 spin_unlock_irqrestore(ap
->lock
, flags
);
2787 * ata_eh_handle_port_resume - perform port resume operation
2788 * @ap: port to resume
2793 * Kernel thread context (may sleep).
2795 static void ata_eh_handle_port_resume(struct ata_port
*ap
)
2797 unsigned long flags
;
2800 /* are we resuming? */
2801 spin_lock_irqsave(ap
->lock
, flags
);
2802 if (!(ap
->pflags
& ATA_PFLAG_PM_PENDING
) ||
2803 ap
->pm_mesg
.event
!= PM_EVENT_ON
) {
2804 spin_unlock_irqrestore(ap
->lock
, flags
);
2807 spin_unlock_irqrestore(ap
->lock
, flags
);
2809 WARN_ON(!(ap
->pflags
& ATA_PFLAG_SUSPENDED
));
2811 if (ap
->ops
->port_resume
)
2812 rc
= ap
->ops
->port_resume(ap
);
2814 /* tell ACPI that we're resuming */
2815 ata_acpi_on_resume(ap
);
2818 spin_lock_irqsave(ap
->lock
, flags
);
2819 ap
->pflags
&= ~(ATA_PFLAG_PM_PENDING
| ATA_PFLAG_SUSPENDED
);
2820 if (ap
->pm_result
) {
2821 *ap
->pm_result
= rc
;
2822 ap
->pm_result
= NULL
;
2824 spin_unlock_irqrestore(ap
->lock
, flags
);
2826 #endif /* CONFIG_PM */