2 * libata-eh.c - libata error handling
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
8 * Copyright 2006 Tejun Heo <htejun@gmail.com>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; see the file COPYING. If not, write to
23 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
35 #include <linux/kernel.h>
36 #include <scsi/scsi.h>
37 #include <scsi/scsi_host.h>
38 #include <scsi/scsi_eh.h>
39 #include <scsi/scsi_device.h>
40 #include <scsi/scsi_cmnd.h>
41 #include "../scsi/scsi_transport_api.h"
43 #include <linux/libata.h>
48 ATA_EH_SPDN_NCQ_OFF
= (1 << 0),
49 ATA_EH_SPDN_SPEED_DOWN
= (1 << 1),
50 ATA_EH_SPDN_FALLBACK_TO_PIO
= (1 << 2),
53 /* Waiting in ->prereset can never be reliable. It's sometimes nice
54 * to wait there but it can't be depended upon; otherwise, we wouldn't
55 * be resetting. Just give it enough time for most drives to spin up.
58 ATA_EH_PRERESET_TIMEOUT
= 10 * HZ
,
61 /* The following table determines how we sequence resets. Each entry
62 * represents timeout for that try. The first try can be soft or
63 * hardreset. All others are hardreset if available. In most cases
64 * the first reset w/ 10sec timeout should succeed. Following entries
65 * are mostly for error handling, hotplug and retarded devices.
67 static const unsigned long ata_eh_reset_timeouts
[] = {
68 10 * HZ
, /* most drives spin up by 10sec */
69 10 * HZ
, /* > 99% working drives spin up before 20sec */
70 35 * HZ
, /* give > 30 secs of idleness for retarded devices */
71 5 * HZ
, /* and sweet one last chance */
72 /* > 1 min has elapsed, give up */
75 static void __ata_port_freeze(struct ata_port
*ap
);
76 static void ata_eh_finish(struct ata_port
*ap
);
78 static void ata_eh_handle_port_suspend(struct ata_port
*ap
);
79 static void ata_eh_handle_port_resume(struct ata_port
*ap
);
81 static void ata_eh_handle_port_suspend(struct ata_port
*ap
)
84 static void ata_eh_handle_port_resume(struct ata_port
*ap
)
86 #endif /* CONFIG_PM */
88 static void __ata_ehi_pushv_desc(struct ata_eh_info
*ehi
, const char *fmt
,
91 ehi
->desc_len
+= vscnprintf(ehi
->desc
+ ehi
->desc_len
,
92 ATA_EH_DESC_LEN
- ehi
->desc_len
,
97 * __ata_ehi_push_desc - push error description without adding separator
99 * @fmt: printf format string
101 * Format string according to @fmt and append it to @ehi->desc.
104 * spin_lock_irqsave(host lock)
106 void __ata_ehi_push_desc(struct ata_eh_info
*ehi
, const char *fmt
, ...)
111 __ata_ehi_pushv_desc(ehi
, fmt
, args
);
116 * ata_ehi_push_desc - push error description with separator
118 * @fmt: printf format string
120 * Format string according to @fmt and append it to @ehi->desc.
121 * If @ehi->desc is not empty, ", " is added in-between.
124 * spin_lock_irqsave(host lock)
126 void ata_ehi_push_desc(struct ata_eh_info
*ehi
, const char *fmt
, ...)
131 __ata_ehi_push_desc(ehi
, ", ");
134 __ata_ehi_pushv_desc(ehi
, fmt
, args
);
139 * ata_ehi_clear_desc - clean error description
145 * spin_lock_irqsave(host lock)
147 void ata_ehi_clear_desc(struct ata_eh_info
*ehi
)
153 static void ata_ering_record(struct ata_ering
*ering
, int is_io
,
154 unsigned int err_mask
)
156 struct ata_ering_entry
*ent
;
161 ering
->cursor
%= ATA_ERING_SIZE
;
163 ent
= &ering
->ring
[ering
->cursor
];
165 ent
->err_mask
= err_mask
;
166 ent
->timestamp
= get_jiffies_64();
169 static void ata_ering_clear(struct ata_ering
*ering
)
171 memset(ering
, 0, sizeof(*ering
));
174 static int ata_ering_map(struct ata_ering
*ering
,
175 int (*map_fn
)(struct ata_ering_entry
*, void *),
179 struct ata_ering_entry
*ent
;
183 ent
= &ering
->ring
[idx
];
186 rc
= map_fn(ent
, arg
);
189 idx
= (idx
- 1 + ATA_ERING_SIZE
) % ATA_ERING_SIZE
;
190 } while (idx
!= ering
->cursor
);
195 static unsigned int ata_eh_dev_action(struct ata_device
*dev
)
197 struct ata_eh_context
*ehc
= &dev
->ap
->eh_context
;
199 return ehc
->i
.action
| ehc
->i
.dev_action
[dev
->devno
];
202 static void ata_eh_clear_action(struct ata_device
*dev
,
203 struct ata_eh_info
*ehi
, unsigned int action
)
208 ehi
->action
&= ~action
;
209 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
210 ehi
->dev_action
[i
] &= ~action
;
212 /* doesn't make sense for port-wide EH actions */
213 WARN_ON(!(action
& ATA_EH_PERDEV_MASK
));
215 /* break ehi->action into ehi->dev_action */
216 if (ehi
->action
& action
) {
217 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
218 ehi
->dev_action
[i
] |= ehi
->action
& action
;
219 ehi
->action
&= ~action
;
222 /* turn off the specified per-dev action */
223 ehi
->dev_action
[dev
->devno
] &= ~action
;
228 * ata_scsi_timed_out - SCSI layer time out callback
229 * @cmd: timed out SCSI command
231 * Handles SCSI layer timeout. We race with normal completion of
232 * the qc for @cmd. If the qc is already gone, we lose and let
233 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
234 * timed out and EH should be invoked. Prevent ata_qc_complete()
235 * from finishing it by setting EH_SCHEDULED and return
238 * TODO: kill this function once old EH is gone.
241 * Called from timer context
244 * EH_HANDLED or EH_NOT_HANDLED
246 enum scsi_eh_timer_return
ata_scsi_timed_out(struct scsi_cmnd
*cmd
)
248 struct Scsi_Host
*host
= cmd
->device
->host
;
249 struct ata_port
*ap
= ata_shost_to_port(host
);
251 struct ata_queued_cmd
*qc
;
252 enum scsi_eh_timer_return ret
;
256 if (ap
->ops
->error_handler
) {
257 ret
= EH_NOT_HANDLED
;
262 spin_lock_irqsave(ap
->lock
, flags
);
263 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
265 WARN_ON(qc
->scsicmd
!= cmd
);
266 qc
->flags
|= ATA_QCFLAG_EH_SCHEDULED
;
267 qc
->err_mask
|= AC_ERR_TIMEOUT
;
268 ret
= EH_NOT_HANDLED
;
270 spin_unlock_irqrestore(ap
->lock
, flags
);
273 DPRINTK("EXIT, ret=%d\n", ret
);
278 * ata_scsi_error - SCSI layer error handler callback
279 * @host: SCSI host on which error occurred
281 * Handles SCSI-layer-thrown error events.
284 * Inherited from SCSI layer (none, can sleep)
289 void ata_scsi_error(struct Scsi_Host
*host
)
291 struct ata_port
*ap
= ata_shost_to_port(host
);
292 int i
, repeat_cnt
= ATA_EH_MAX_REPEAT
;
297 /* synchronize with port task */
298 ata_port_flush_task(ap
);
300 /* synchronize with host lock and sort out timeouts */
302 /* For new EH, all qcs are finished in one of three ways -
303 * normal completion, error completion, and SCSI timeout.
304 * Both cmpletions can race against SCSI timeout. When normal
305 * completion wins, the qc never reaches EH. When error
306 * completion wins, the qc has ATA_QCFLAG_FAILED set.
308 * When SCSI timeout wins, things are a bit more complex.
309 * Normal or error completion can occur after the timeout but
310 * before this point. In such cases, both types of
311 * completions are honored. A scmd is determined to have
312 * timed out iff its associated qc is active and not failed.
314 if (ap
->ops
->error_handler
) {
315 struct scsi_cmnd
*scmd
, *tmp
;
318 spin_lock_irqsave(ap
->lock
, flags
);
320 list_for_each_entry_safe(scmd
, tmp
, &host
->eh_cmd_q
, eh_entry
) {
321 struct ata_queued_cmd
*qc
;
323 for (i
= 0; i
< ATA_MAX_QUEUE
; i
++) {
324 qc
= __ata_qc_from_tag(ap
, i
);
325 if (qc
->flags
& ATA_QCFLAG_ACTIVE
&&
330 if (i
< ATA_MAX_QUEUE
) {
331 /* the scmd has an associated qc */
332 if (!(qc
->flags
& ATA_QCFLAG_FAILED
)) {
333 /* which hasn't failed yet, timeout */
334 qc
->err_mask
|= AC_ERR_TIMEOUT
;
335 qc
->flags
|= ATA_QCFLAG_FAILED
;
339 /* Normal completion occurred after
340 * SCSI timeout but before this point.
341 * Successfully complete it.
343 scmd
->retries
= scmd
->allowed
;
344 scsi_eh_finish_cmd(scmd
, &ap
->eh_done_q
);
348 /* If we have timed out qcs. They belong to EH from
349 * this point but the state of the controller is
350 * unknown. Freeze the port to make sure the IRQ
351 * handler doesn't diddle with those qcs. This must
352 * be done atomically w.r.t. setting QCFLAG_FAILED.
355 __ata_port_freeze(ap
);
357 spin_unlock_irqrestore(ap
->lock
, flags
);
359 spin_unlock_wait(ap
->lock
);
362 /* invoke error handler */
363 if (ap
->ops
->error_handler
) {
364 /* process port resume request */
365 ata_eh_handle_port_resume(ap
);
367 /* fetch & clear EH info */
368 spin_lock_irqsave(ap
->lock
, flags
);
370 memset(&ap
->eh_context
, 0, sizeof(ap
->eh_context
));
371 ap
->eh_context
.i
= ap
->eh_info
;
372 memset(&ap
->eh_info
, 0, sizeof(ap
->eh_info
));
374 ap
->pflags
|= ATA_PFLAG_EH_IN_PROGRESS
;
375 ap
->pflags
&= ~ATA_PFLAG_EH_PENDING
;
377 spin_unlock_irqrestore(ap
->lock
, flags
);
379 /* invoke EH, skip if unloading or suspended */
380 if (!(ap
->pflags
& (ATA_PFLAG_UNLOADING
| ATA_PFLAG_SUSPENDED
)))
381 ap
->ops
->error_handler(ap
);
385 /* process port suspend request */
386 ata_eh_handle_port_suspend(ap
);
388 /* Exception might have happend after ->error_handler
389 * recovered the port but before this point. Repeat
392 spin_lock_irqsave(ap
->lock
, flags
);
394 if (ap
->pflags
& ATA_PFLAG_EH_PENDING
) {
396 ata_port_printk(ap
, KERN_INFO
,
397 "EH pending after completion, "
398 "repeating EH (cnt=%d)\n", repeat_cnt
);
399 spin_unlock_irqrestore(ap
->lock
, flags
);
402 ata_port_printk(ap
, KERN_ERR
, "EH pending after %d "
403 "tries, giving up\n", ATA_EH_MAX_REPEAT
);
404 ap
->pflags
&= ~ATA_PFLAG_EH_PENDING
;
407 /* this run is complete, make sure EH info is clear */
408 memset(&ap
->eh_info
, 0, sizeof(ap
->eh_info
));
410 /* Clear host_eh_scheduled while holding ap->lock such
411 * that if exception occurs after this point but
412 * before EH completion, SCSI midlayer will
415 host
->host_eh_scheduled
= 0;
417 spin_unlock_irqrestore(ap
->lock
, flags
);
419 WARN_ON(ata_qc_from_tag(ap
, ap
->active_tag
) == NULL
);
420 ap
->ops
->eng_timeout(ap
);
423 /* finish or retry handled scmd's and clean up */
424 WARN_ON(host
->host_failed
|| !list_empty(&host
->eh_cmd_q
));
426 scsi_eh_flush_done_q(&ap
->eh_done_q
);
429 spin_lock_irqsave(ap
->lock
, flags
);
431 if (ap
->pflags
& ATA_PFLAG_LOADING
)
432 ap
->pflags
&= ~ATA_PFLAG_LOADING
;
433 else if (ap
->pflags
& ATA_PFLAG_SCSI_HOTPLUG
)
434 queue_delayed_work(ata_aux_wq
, &ap
->hotplug_task
, 0);
436 if (ap
->pflags
& ATA_PFLAG_RECOVERED
)
437 ata_port_printk(ap
, KERN_INFO
, "EH complete\n");
439 ap
->pflags
&= ~(ATA_PFLAG_SCSI_HOTPLUG
| ATA_PFLAG_RECOVERED
);
441 /* tell wait_eh that we're done */
442 ap
->pflags
&= ~ATA_PFLAG_EH_IN_PROGRESS
;
443 wake_up_all(&ap
->eh_wait_q
);
445 spin_unlock_irqrestore(ap
->lock
, flags
);
451 * ata_port_wait_eh - Wait for the currently pending EH to complete
452 * @ap: Port to wait EH for
454 * Wait until the currently pending EH is complete.
457 * Kernel thread context (may sleep).
459 void ata_port_wait_eh(struct ata_port
*ap
)
465 spin_lock_irqsave(ap
->lock
, flags
);
467 while (ap
->pflags
& (ATA_PFLAG_EH_PENDING
| ATA_PFLAG_EH_IN_PROGRESS
)) {
468 prepare_to_wait(&ap
->eh_wait_q
, &wait
, TASK_UNINTERRUPTIBLE
);
469 spin_unlock_irqrestore(ap
->lock
, flags
);
471 spin_lock_irqsave(ap
->lock
, flags
);
473 finish_wait(&ap
->eh_wait_q
, &wait
);
475 spin_unlock_irqrestore(ap
->lock
, flags
);
477 /* make sure SCSI EH is complete */
478 if (scsi_host_in_recovery(ap
->scsi_host
)) {
485 * ata_qc_timeout - Handle timeout of queued command
486 * @qc: Command that timed out
488 * Some part of the kernel (currently, only the SCSI layer)
489 * has noticed that the active command on port @ap has not
490 * completed after a specified length of time. Handle this
491 * condition by disabling DMA (if necessary) and completing
492 * transactions, with error if necessary.
494 * This also handles the case of the "lost interrupt", where
495 * for some reason (possibly hardware bug, possibly driver bug)
496 * an interrupt was not delivered to the driver, even though the
497 * transaction completed successfully.
499 * TODO: kill this function once old EH is gone.
502 * Inherited from SCSI layer (none, can sleep)
504 static void ata_qc_timeout(struct ata_queued_cmd
*qc
)
506 struct ata_port
*ap
= qc
->ap
;
507 u8 host_stat
= 0, drv_stat
;
512 ap
->hsm_task_state
= HSM_ST_IDLE
;
514 spin_lock_irqsave(ap
->lock
, flags
);
516 switch (qc
->tf
.protocol
) {
519 case ATA_PROT_ATAPI_DMA
:
520 host_stat
= ap
->ops
->bmdma_status(ap
);
522 /* before we do anything else, clear DMA-Start bit */
523 ap
->ops
->bmdma_stop(qc
);
529 drv_stat
= ata_chk_status(ap
);
531 /* ack bmdma irq events */
532 ap
->ops
->irq_clear(ap
);
534 ata_dev_printk(qc
->dev
, KERN_ERR
, "command 0x%x timeout, "
535 "stat 0x%x host_stat 0x%x\n",
536 qc
->tf
.command
, drv_stat
, host_stat
);
538 /* complete taskfile transaction */
539 qc
->err_mask
|= AC_ERR_TIMEOUT
;
543 spin_unlock_irqrestore(ap
->lock
, flags
);
545 ata_eh_qc_complete(qc
);
551 * ata_eng_timeout - Handle timeout of queued command
552 * @ap: Port on which timed-out command is active
554 * Some part of the kernel (currently, only the SCSI layer)
555 * has noticed that the active command on port @ap has not
556 * completed after a specified length of time. Handle this
557 * condition by disabling DMA (if necessary) and completing
558 * transactions, with error if necessary.
560 * This also handles the case of the "lost interrupt", where
561 * for some reason (possibly hardware bug, possibly driver bug)
562 * an interrupt was not delivered to the driver, even though the
563 * transaction completed successfully.
565 * TODO: kill this function once old EH is gone.
568 * Inherited from SCSI layer (none, can sleep)
570 void ata_eng_timeout(struct ata_port
*ap
)
574 ata_qc_timeout(ata_qc_from_tag(ap
, ap
->active_tag
));
580 * ata_qc_schedule_eh - schedule qc for error handling
581 * @qc: command to schedule error handling for
583 * Schedule error handling for @qc. EH will kick in as soon as
584 * other commands are drained.
587 * spin_lock_irqsave(host lock)
589 void ata_qc_schedule_eh(struct ata_queued_cmd
*qc
)
591 struct ata_port
*ap
= qc
->ap
;
593 WARN_ON(!ap
->ops
->error_handler
);
595 qc
->flags
|= ATA_QCFLAG_FAILED
;
596 qc
->ap
->pflags
|= ATA_PFLAG_EH_PENDING
;
598 /* The following will fail if timeout has already expired.
599 * ata_scsi_error() takes care of such scmds on EH entry.
600 * Note that ATA_QCFLAG_FAILED is unconditionally set after
601 * this function completes.
603 scsi_req_abort_cmd(qc
->scsicmd
);
607 * ata_port_schedule_eh - schedule error handling without a qc
608 * @ap: ATA port to schedule EH for
610 * Schedule error handling for @ap. EH will kick in as soon as
611 * all commands are drained.
614 * spin_lock_irqsave(host lock)
616 void ata_port_schedule_eh(struct ata_port
*ap
)
618 WARN_ON(!ap
->ops
->error_handler
);
620 if (ap
->pflags
& ATA_PFLAG_INITIALIZING
)
623 ap
->pflags
|= ATA_PFLAG_EH_PENDING
;
624 scsi_schedule_eh(ap
->scsi_host
);
626 DPRINTK("port EH scheduled\n");
630 * ata_port_abort - abort all qc's on the port
631 * @ap: ATA port to abort qc's for
633 * Abort all active qc's of @ap and schedule EH.
636 * spin_lock_irqsave(host lock)
639 * Number of aborted qc's.
641 int ata_port_abort(struct ata_port
*ap
)
643 int tag
, nr_aborted
= 0;
645 WARN_ON(!ap
->ops
->error_handler
);
647 for (tag
= 0; tag
< ATA_MAX_QUEUE
; tag
++) {
648 struct ata_queued_cmd
*qc
= ata_qc_from_tag(ap
, tag
);
651 qc
->flags
|= ATA_QCFLAG_FAILED
;
658 ata_port_schedule_eh(ap
);
664 * __ata_port_freeze - freeze port
665 * @ap: ATA port to freeze
667 * This function is called when HSM violation or some other
668 * condition disrupts normal operation of the port. Frozen port
669 * is not allowed to perform any operation until the port is
670 * thawed, which usually follows a successful reset.
672 * ap->ops->freeze() callback can be used for freezing the port
673 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
674 * port cannot be frozen hardware-wise, the interrupt handler
675 * must ack and clear interrupts unconditionally while the port
679 * spin_lock_irqsave(host lock)
681 static void __ata_port_freeze(struct ata_port
*ap
)
683 WARN_ON(!ap
->ops
->error_handler
);
688 ap
->pflags
|= ATA_PFLAG_FROZEN
;
690 DPRINTK("ata%u port frozen\n", ap
->print_id
);
694 * ata_port_freeze - abort & freeze port
695 * @ap: ATA port to freeze
697 * Abort and freeze @ap.
700 * spin_lock_irqsave(host lock)
703 * Number of aborted commands.
705 int ata_port_freeze(struct ata_port
*ap
)
709 WARN_ON(!ap
->ops
->error_handler
);
711 nr_aborted
= ata_port_abort(ap
);
712 __ata_port_freeze(ap
);
718 * ata_eh_freeze_port - EH helper to freeze port
719 * @ap: ATA port to freeze
726 void ata_eh_freeze_port(struct ata_port
*ap
)
730 if (!ap
->ops
->error_handler
)
733 spin_lock_irqsave(ap
->lock
, flags
);
734 __ata_port_freeze(ap
);
735 spin_unlock_irqrestore(ap
->lock
, flags
);
739 * ata_port_thaw_port - EH helper to thaw port
740 * @ap: ATA port to thaw
742 * Thaw frozen port @ap.
747 void ata_eh_thaw_port(struct ata_port
*ap
)
751 if (!ap
->ops
->error_handler
)
754 spin_lock_irqsave(ap
->lock
, flags
);
756 ap
->pflags
&= ~ATA_PFLAG_FROZEN
;
761 spin_unlock_irqrestore(ap
->lock
, flags
);
763 DPRINTK("ata%u port thawed\n", ap
->print_id
);
766 static void ata_eh_scsidone(struct scsi_cmnd
*scmd
)
771 static void __ata_eh_qc_complete(struct ata_queued_cmd
*qc
)
773 struct ata_port
*ap
= qc
->ap
;
774 struct scsi_cmnd
*scmd
= qc
->scsicmd
;
777 spin_lock_irqsave(ap
->lock
, flags
);
778 qc
->scsidone
= ata_eh_scsidone
;
779 __ata_qc_complete(qc
);
780 WARN_ON(ata_tag_valid(qc
->tag
));
781 spin_unlock_irqrestore(ap
->lock
, flags
);
783 scsi_eh_finish_cmd(scmd
, &ap
->eh_done_q
);
787 * ata_eh_qc_complete - Complete an active ATA command from EH
788 * @qc: Command to complete
790 * Indicate to the mid and upper layers that an ATA command has
791 * completed. To be used from EH.
793 void ata_eh_qc_complete(struct ata_queued_cmd
*qc
)
795 struct scsi_cmnd
*scmd
= qc
->scsicmd
;
796 scmd
->retries
= scmd
->allowed
;
797 __ata_eh_qc_complete(qc
);
801 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
802 * @qc: Command to retry
804 * Indicate to the mid and upper layers that an ATA command
805 * should be retried. To be used from EH.
807 * SCSI midlayer limits the number of retries to scmd->allowed.
808 * scmd->retries is decremented for commands which get retried
809 * due to unrelated failures (qc->err_mask is zero).
811 void ata_eh_qc_retry(struct ata_queued_cmd
*qc
)
813 struct scsi_cmnd
*scmd
= qc
->scsicmd
;
814 if (!qc
->err_mask
&& scmd
->retries
)
816 __ata_eh_qc_complete(qc
);
820 * ata_eh_detach_dev - detach ATA device
821 * @dev: ATA device to detach
828 static void ata_eh_detach_dev(struct ata_device
*dev
)
830 struct ata_port
*ap
= dev
->ap
;
833 ata_dev_disable(dev
);
835 spin_lock_irqsave(ap
->lock
, flags
);
837 dev
->flags
&= ~ATA_DFLAG_DETACH
;
839 if (ata_scsi_offline_dev(dev
)) {
840 dev
->flags
|= ATA_DFLAG_DETACHED
;
841 ap
->pflags
|= ATA_PFLAG_SCSI_HOTPLUG
;
844 /* clear per-dev EH actions */
845 ata_eh_clear_action(dev
, &ap
->eh_info
, ATA_EH_PERDEV_MASK
);
846 ata_eh_clear_action(dev
, &ap
->eh_context
.i
, ATA_EH_PERDEV_MASK
);
848 spin_unlock_irqrestore(ap
->lock
, flags
);
852 * ata_eh_about_to_do - about to perform eh_action
853 * @ap: target ATA port
854 * @dev: target ATA dev for per-dev action (can be NULL)
855 * @action: action about to be performed
857 * Called just before performing EH actions to clear related bits
858 * in @ap->eh_info such that eh actions are not unnecessarily
864 static void ata_eh_about_to_do(struct ata_port
*ap
, struct ata_device
*dev
,
868 struct ata_eh_info
*ehi
= &ap
->eh_info
;
869 struct ata_eh_context
*ehc
= &ap
->eh_context
;
871 spin_lock_irqsave(ap
->lock
, flags
);
873 /* Reset is represented by combination of actions and EHI
874 * flags. Suck in all related bits before clearing eh_info to
875 * avoid losing requested action.
877 if (action
& ATA_EH_RESET_MASK
) {
878 ehc
->i
.action
|= ehi
->action
& ATA_EH_RESET_MASK
;
879 ehc
->i
.flags
|= ehi
->flags
& ATA_EHI_RESET_MODIFIER_MASK
;
881 /* make sure all reset actions are cleared & clear EHI flags */
882 action
|= ATA_EH_RESET_MASK
;
883 ehi
->flags
&= ~ATA_EHI_RESET_MODIFIER_MASK
;
886 ata_eh_clear_action(dev
, ehi
, action
);
888 if (!(ehc
->i
.flags
& ATA_EHI_QUIET
))
889 ap
->pflags
|= ATA_PFLAG_RECOVERED
;
891 spin_unlock_irqrestore(ap
->lock
, flags
);
895 * ata_eh_done - EH action complete
896 * @ap: target ATA port
897 * @dev: target ATA dev for per-dev action (can be NULL)
898 * @action: action just completed
900 * Called right after performing EH actions to clear related bits
901 * in @ap->eh_context.
906 static void ata_eh_done(struct ata_port
*ap
, struct ata_device
*dev
,
909 /* if reset is complete, clear all reset actions & reset modifier */
910 if (action
& ATA_EH_RESET_MASK
) {
911 action
|= ATA_EH_RESET_MASK
;
912 ap
->eh_context
.i
.flags
&= ~ATA_EHI_RESET_MODIFIER_MASK
;
915 ata_eh_clear_action(dev
, &ap
->eh_context
.i
, action
);
919 * ata_err_string - convert err_mask to descriptive string
920 * @err_mask: error mask to convert to string
922 * Convert @err_mask to descriptive string. Errors are
923 * prioritized according to severity and only the most severe
930 * Descriptive string for @err_mask
932 static const char * ata_err_string(unsigned int err_mask
)
934 if (err_mask
& AC_ERR_HOST_BUS
)
935 return "host bus error";
936 if (err_mask
& AC_ERR_ATA_BUS
)
937 return "ATA bus error";
938 if (err_mask
& AC_ERR_TIMEOUT
)
940 if (err_mask
& AC_ERR_HSM
)
941 return "HSM violation";
942 if (err_mask
& AC_ERR_SYSTEM
)
943 return "internal error";
944 if (err_mask
& AC_ERR_MEDIA
)
945 return "media error";
946 if (err_mask
& AC_ERR_INVALID
)
947 return "invalid argument";
948 if (err_mask
& AC_ERR_DEV
)
949 return "device error";
950 return "unknown error";
954 * ata_read_log_page - read a specific log page
955 * @dev: target device
956 * @page: page to read
957 * @buf: buffer to store read page
958 * @sectors: number of sectors to read
960 * Read log page using READ_LOG_EXT command.
963 * Kernel thread context (may sleep).
966 * 0 on success, AC_ERR_* mask otherwise.
968 static unsigned int ata_read_log_page(struct ata_device
*dev
,
969 u8 page
, void *buf
, unsigned int sectors
)
971 struct ata_taskfile tf
;
972 unsigned int err_mask
;
974 DPRINTK("read log page - page %d\n", page
);
976 ata_tf_init(dev
, &tf
);
977 tf
.command
= ATA_CMD_READ_LOG_EXT
;
980 tf
.hob_nsect
= sectors
>> 8;
981 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_LBA48
| ATA_TFLAG_DEVICE
;
982 tf
.protocol
= ATA_PROT_PIO
;
984 err_mask
= ata_exec_internal(dev
, &tf
, NULL
, DMA_FROM_DEVICE
,
985 buf
, sectors
* ATA_SECT_SIZE
);
987 DPRINTK("EXIT, err_mask=%x\n", err_mask
);
992 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
993 * @dev: Device to read log page 10h from
994 * @tag: Resulting tag of the failed command
995 * @tf: Resulting taskfile registers of the failed command
997 * Read log page 10h to obtain NCQ error details and clear error
1001 * Kernel thread context (may sleep).
1004 * 0 on success, -errno otherwise.
1006 static int ata_eh_read_log_10h(struct ata_device
*dev
,
1007 int *tag
, struct ata_taskfile
*tf
)
1009 u8
*buf
= dev
->ap
->sector_buf
;
1010 unsigned int err_mask
;
1014 err_mask
= ata_read_log_page(dev
, ATA_LOG_SATA_NCQ
, buf
, 1);
1019 for (i
= 0; i
< ATA_SECT_SIZE
; i
++)
1022 ata_dev_printk(dev
, KERN_WARNING
,
1023 "invalid checksum 0x%x on log page 10h\n", csum
);
1028 *tag
= buf
[0] & 0x1f;
1030 tf
->command
= buf
[2];
1031 tf
->feature
= buf
[3];
1035 tf
->device
= buf
[7];
1036 tf
->hob_lbal
= buf
[8];
1037 tf
->hob_lbam
= buf
[9];
1038 tf
->hob_lbah
= buf
[10];
1039 tf
->nsect
= buf
[12];
1040 tf
->hob_nsect
= buf
[13];
1046 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1047 * @dev: device to perform REQUEST_SENSE to
1048 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
1050 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
1051 * SENSE. This function is EH helper.
1054 * Kernel thread context (may sleep).
1057 * 0 on success, AC_ERR_* mask on failure
1059 static unsigned int atapi_eh_request_sense(struct ata_queued_cmd
*qc
)
1061 struct ata_device
*dev
= qc
->dev
;
1062 unsigned char *sense_buf
= qc
->scsicmd
->sense_buffer
;
1063 struct ata_port
*ap
= dev
->ap
;
1064 struct ata_taskfile tf
;
1065 u8 cdb
[ATAPI_CDB_LEN
];
1067 DPRINTK("ATAPI request sense\n");
1069 /* FIXME: is this needed? */
1070 memset(sense_buf
, 0, SCSI_SENSE_BUFFERSIZE
);
1072 /* initialize sense_buf with the error register,
1073 * for the case where they are -not- overwritten
1075 sense_buf
[0] = 0x70;
1076 sense_buf
[2] = qc
->result_tf
.feature
>> 4;
1078 /* some devices time out if garbage left in tf */
1079 ata_tf_init(dev
, &tf
);
1081 memset(cdb
, 0, ATAPI_CDB_LEN
);
1082 cdb
[0] = REQUEST_SENSE
;
1083 cdb
[4] = SCSI_SENSE_BUFFERSIZE
;
1085 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
1086 tf
.command
= ATA_CMD_PACKET
;
1088 /* is it pointless to prefer PIO for "safety reasons"? */
1089 if (ap
->flags
& ATA_FLAG_PIO_DMA
) {
1090 tf
.protocol
= ATA_PROT_ATAPI_DMA
;
1091 tf
.feature
|= ATAPI_PKT_DMA
;
1093 tf
.protocol
= ATA_PROT_ATAPI
;
1094 tf
.lbam
= (8 * 1024) & 0xff;
1095 tf
.lbah
= (8 * 1024) >> 8;
1098 return ata_exec_internal(dev
, &tf
, cdb
, DMA_FROM_DEVICE
,
1099 sense_buf
, SCSI_SENSE_BUFFERSIZE
);
1103 * ata_eh_analyze_serror - analyze SError for a failed port
1104 * @ap: ATA port to analyze SError for
1106 * Analyze SError if available and further determine cause of
1112 static void ata_eh_analyze_serror(struct ata_port
*ap
)
1114 struct ata_eh_context
*ehc
= &ap
->eh_context
;
1115 u32 serror
= ehc
->i
.serror
;
1116 unsigned int err_mask
= 0, action
= 0;
1118 if (serror
& SERR_PERSISTENT
) {
1119 err_mask
|= AC_ERR_ATA_BUS
;
1120 action
|= ATA_EH_HARDRESET
;
1123 (SERR_DATA_RECOVERED
| SERR_COMM_RECOVERED
| SERR_DATA
)) {
1124 err_mask
|= AC_ERR_ATA_BUS
;
1125 action
|= ATA_EH_SOFTRESET
;
1127 if (serror
& SERR_PROTOCOL
) {
1128 err_mask
|= AC_ERR_HSM
;
1129 action
|= ATA_EH_SOFTRESET
;
1131 if (serror
& SERR_INTERNAL
) {
1132 err_mask
|= AC_ERR_SYSTEM
;
1133 action
|= ATA_EH_HARDRESET
;
1135 if (serror
& (SERR_PHYRDY_CHG
| SERR_DEV_XCHG
))
1136 ata_ehi_hotplugged(&ehc
->i
);
1138 ehc
->i
.err_mask
|= err_mask
;
1139 ehc
->i
.action
|= action
;
1143 * ata_eh_analyze_ncq_error - analyze NCQ error
1144 * @ap: ATA port to analyze NCQ error for
1146 * Read log page 10h, determine the offending qc and acquire
1147 * error status TF. For NCQ device errors, all LLDDs have to do
1148 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1152 * Kernel thread context (may sleep).
1154 static void ata_eh_analyze_ncq_error(struct ata_port
*ap
)
1156 struct ata_eh_context
*ehc
= &ap
->eh_context
;
1157 struct ata_device
*dev
= ap
->device
;
1158 struct ata_queued_cmd
*qc
;
1159 struct ata_taskfile tf
;
1162 /* if frozen, we can't do much */
1163 if (ap
->pflags
& ATA_PFLAG_FROZEN
)
1166 /* is it NCQ device error? */
1167 if (!ap
->sactive
|| !(ehc
->i
.err_mask
& AC_ERR_DEV
))
1170 /* has LLDD analyzed already? */
1171 for (tag
= 0; tag
< ATA_MAX_QUEUE
; tag
++) {
1172 qc
= __ata_qc_from_tag(ap
, tag
);
1174 if (!(qc
->flags
& ATA_QCFLAG_FAILED
))
1181 /* okay, this error is ours */
1182 rc
= ata_eh_read_log_10h(dev
, &tag
, &tf
);
1184 ata_port_printk(ap
, KERN_ERR
, "failed to read log page 10h "
1185 "(errno=%d)\n", rc
);
1189 if (!(ap
->sactive
& (1 << tag
))) {
1190 ata_port_printk(ap
, KERN_ERR
, "log page 10h reported "
1191 "inactive tag %d\n", tag
);
1195 /* we've got the perpetrator, condemn it */
1196 qc
= __ata_qc_from_tag(ap
, tag
);
1197 memcpy(&qc
->result_tf
, &tf
, sizeof(tf
));
1198 qc
->err_mask
|= AC_ERR_DEV
| AC_ERR_NCQ
;
1199 ehc
->i
.err_mask
&= ~AC_ERR_DEV
;
1203 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1204 * @qc: qc to analyze
1205 * @tf: Taskfile registers to analyze
1207 * Analyze taskfile of @qc and further determine cause of
1208 * failure. This function also requests ATAPI sense data if
1212 * Kernel thread context (may sleep).
1215 * Determined recovery action
1217 static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd
*qc
,
1218 const struct ata_taskfile
*tf
)
1220 unsigned int tmp
, action
= 0;
1221 u8 stat
= tf
->command
, err
= tf
->feature
;
1223 if ((stat
& (ATA_BUSY
| ATA_DRQ
| ATA_DRDY
)) != ATA_DRDY
) {
1224 qc
->err_mask
|= AC_ERR_HSM
;
1225 return ATA_EH_SOFTRESET
;
1228 if (stat
& (ATA_ERR
| ATA_DF
))
1229 qc
->err_mask
|= AC_ERR_DEV
;
1233 switch (qc
->dev
->class) {
1236 qc
->err_mask
|= AC_ERR_ATA_BUS
;
1238 qc
->err_mask
|= AC_ERR_MEDIA
;
1240 qc
->err_mask
|= AC_ERR_INVALID
;
1244 if (!(qc
->ap
->pflags
& ATA_PFLAG_FROZEN
)) {
1245 tmp
= atapi_eh_request_sense(qc
);
1247 /* ATA_QCFLAG_SENSE_VALID is used to
1248 * tell atapi_qc_complete() that sense
1249 * data is already valid.
1251 * TODO: interpret sense data and set
1252 * appropriate err_mask.
1254 qc
->flags
|= ATA_QCFLAG_SENSE_VALID
;
1256 qc
->err_mask
|= tmp
;
1260 if (qc
->err_mask
& (AC_ERR_HSM
| AC_ERR_TIMEOUT
| AC_ERR_ATA_BUS
))
1261 action
|= ATA_EH_SOFTRESET
;
1266 static int ata_eh_categorize_error(int is_io
, unsigned int err_mask
)
1268 if (err_mask
& AC_ERR_ATA_BUS
)
1271 if (err_mask
& AC_ERR_TIMEOUT
)
1275 if (err_mask
& AC_ERR_HSM
)
1278 (AC_ERR_DEV
|AC_ERR_MEDIA
|AC_ERR_INVALID
)) == AC_ERR_DEV
)
1285 struct speed_down_verdict_arg
{
1290 static int speed_down_verdict_cb(struct ata_ering_entry
*ent
, void *void_arg
)
1292 struct speed_down_verdict_arg
*arg
= void_arg
;
1293 int cat
= ata_eh_categorize_error(ent
->is_io
, ent
->err_mask
);
1295 if (ent
->timestamp
< arg
->since
)
1298 arg
->nr_errors
[cat
]++;
1303 * ata_eh_speed_down_verdict - Determine speed down verdict
1304 * @dev: Device of interest
1306 * This function examines error ring of @dev and determines
1307 * whether NCQ needs to be turned off, transfer speed should be
1308 * stepped down, or falling back to PIO is necessary.
1310 * Cat-1 is ATA_BUS error for any command.
1312 * Cat-2 is TIMEOUT for any command or HSM violation for known
1313 * supported commands.
1315 * Cat-3 is is unclassified DEV error for known supported
1318 * NCQ needs to be turned off if there have been more than 3
1319 * Cat-2 + Cat-3 errors during last 10 minutes.
1321 * Speed down is necessary if there have been more than 3 Cat-1 +
1322 * Cat-2 errors or 10 Cat-3 errors during last 10 minutes.
1324 * Falling back to PIO mode is necessary if there have been more
1325 * than 10 Cat-1 + Cat-2 + Cat-3 errors during last 5 minutes.
1328 * Inherited from caller.
1331 * OR of ATA_EH_SPDN_* flags.
1333 static unsigned int ata_eh_speed_down_verdict(struct ata_device
*dev
)
1335 const u64 j5mins
= 5LLU * 60 * HZ
, j10mins
= 10LLU * 60 * HZ
;
1336 u64 j64
= get_jiffies_64();
1337 struct speed_down_verdict_arg arg
;
1338 unsigned int verdict
= 0;
1340 /* scan past 10 mins of error history */
1341 memset(&arg
, 0, sizeof(arg
));
1342 arg
.since
= j64
- min(j64
, j10mins
);
1343 ata_ering_map(&dev
->ering
, speed_down_verdict_cb
, &arg
);
1345 if (arg
.nr_errors
[2] + arg
.nr_errors
[3] > 3)
1346 verdict
|= ATA_EH_SPDN_NCQ_OFF
;
1347 if (arg
.nr_errors
[1] + arg
.nr_errors
[2] > 3 || arg
.nr_errors
[3] > 10)
1348 verdict
|= ATA_EH_SPDN_SPEED_DOWN
;
1350 /* scan past 3 mins of error history */
1351 memset(&arg
, 0, sizeof(arg
));
1352 arg
.since
= j64
- min(j64
, j5mins
);
1353 ata_ering_map(&dev
->ering
, speed_down_verdict_cb
, &arg
);
1355 if (arg
.nr_errors
[1] + arg
.nr_errors
[2] + arg
.nr_errors
[3] > 10)
1356 verdict
|= ATA_EH_SPDN_FALLBACK_TO_PIO
;
1362 * ata_eh_speed_down - record error and speed down if necessary
1363 * @dev: Failed device
1364 * @is_io: Did the device fail during normal IO?
1365 * @err_mask: err_mask of the error
1367 * Record error and examine error history to determine whether
1368 * adjusting transmission speed is necessary. It also sets
1369 * transmission limits appropriately if such adjustment is
1373 * Kernel thread context (may sleep).
1376 * Determined recovery action.
1378 static unsigned int ata_eh_speed_down(struct ata_device
*dev
, int is_io
,
1379 unsigned int err_mask
)
1381 unsigned int verdict
;
1382 unsigned int action
= 0;
1384 /* don't bother if Cat-0 error */
1385 if (ata_eh_categorize_error(is_io
, err_mask
) == 0)
1388 /* record error and determine whether speed down is necessary */
1389 ata_ering_record(&dev
->ering
, is_io
, err_mask
);
1390 verdict
= ata_eh_speed_down_verdict(dev
);
1393 if ((verdict
& ATA_EH_SPDN_NCQ_OFF
) &&
1394 (dev
->flags
& (ATA_DFLAG_PIO
| ATA_DFLAG_NCQ
|
1395 ATA_DFLAG_NCQ_OFF
)) == ATA_DFLAG_NCQ
) {
1396 dev
->flags
|= ATA_DFLAG_NCQ_OFF
;
1397 ata_dev_printk(dev
, KERN_WARNING
,
1398 "NCQ disabled due to excessive errors\n");
1403 if (verdict
& ATA_EH_SPDN_SPEED_DOWN
) {
1404 /* speed down SATA link speed if possible */
1405 if (sata_down_spd_limit(dev
->ap
) == 0) {
1406 action
|= ATA_EH_HARDRESET
;
1410 /* lower transfer mode */
1411 if (dev
->spdn_cnt
< 2) {
1412 static const int dma_dnxfer_sel
[] =
1413 { ATA_DNXFER_DMA
, ATA_DNXFER_40C
};
1414 static const int pio_dnxfer_sel
[] =
1415 { ATA_DNXFER_PIO
, ATA_DNXFER_FORCE_PIO0
};
1418 if (dev
->xfer_shift
!= ATA_SHIFT_PIO
)
1419 sel
= dma_dnxfer_sel
[dev
->spdn_cnt
];
1421 sel
= pio_dnxfer_sel
[dev
->spdn_cnt
];
1425 if (ata_down_xfermask_limit(dev
, sel
) == 0) {
1426 action
|= ATA_EH_SOFTRESET
;
1432 /* Fall back to PIO? Slowing down to PIO is meaningless for
1433 * SATA. Consider it only for PATA.
1435 if ((verdict
& ATA_EH_SPDN_FALLBACK_TO_PIO
) && (dev
->spdn_cnt
>= 2) &&
1436 (dev
->ap
->cbl
!= ATA_CBL_SATA
) &&
1437 (dev
->xfer_shift
!= ATA_SHIFT_PIO
)) {
1438 if (ata_down_xfermask_limit(dev
, ATA_DNXFER_FORCE_PIO
) == 0) {
1440 action
|= ATA_EH_SOFTRESET
;
1447 /* device has been slowed down, blow error history */
1448 ata_ering_clear(&dev
->ering
);
1453 * ata_eh_autopsy - analyze error and determine recovery action
1454 * @ap: ATA port to perform autopsy on
1456 * Analyze why @ap failed and determine which recovery action is
1457 * needed. This function also sets more detailed AC_ERR_* values
1458 * and fills sense data for ATAPI CHECK SENSE.
1461 * Kernel thread context (may sleep).
1463 static void ata_eh_autopsy(struct ata_port
*ap
)
1465 struct ata_eh_context
*ehc
= &ap
->eh_context
;
1466 unsigned int all_err_mask
= 0;
1473 if (ehc
->i
.flags
& ATA_EHI_NO_AUTOPSY
)
1476 /* obtain and analyze SError */
1477 rc
= sata_scr_read(ap
, SCR_ERROR
, &serror
);
1479 ehc
->i
.serror
|= serror
;
1480 ata_eh_analyze_serror(ap
);
1481 } else if (rc
!= -EOPNOTSUPP
)
1482 ehc
->i
.action
|= ATA_EH_HARDRESET
;
1484 /* analyze NCQ failure */
1485 ata_eh_analyze_ncq_error(ap
);
1487 /* any real error trumps AC_ERR_OTHER */
1488 if (ehc
->i
.err_mask
& ~AC_ERR_OTHER
)
1489 ehc
->i
.err_mask
&= ~AC_ERR_OTHER
;
1491 all_err_mask
|= ehc
->i
.err_mask
;
1493 for (tag
= 0; tag
< ATA_MAX_QUEUE
; tag
++) {
1494 struct ata_queued_cmd
*qc
= __ata_qc_from_tag(ap
, tag
);
1496 if (!(qc
->flags
& ATA_QCFLAG_FAILED
))
1499 /* inherit upper level err_mask */
1500 qc
->err_mask
|= ehc
->i
.err_mask
;
1503 ehc
->i
.action
|= ata_eh_analyze_tf(qc
, &qc
->result_tf
);
1505 /* DEV errors are probably spurious in case of ATA_BUS error */
1506 if (qc
->err_mask
& AC_ERR_ATA_BUS
)
1507 qc
->err_mask
&= ~(AC_ERR_DEV
| AC_ERR_MEDIA
|
1510 /* any real error trumps unknown error */
1511 if (qc
->err_mask
& ~AC_ERR_OTHER
)
1512 qc
->err_mask
&= ~AC_ERR_OTHER
;
1514 /* SENSE_VALID trumps dev/unknown error and revalidation */
1515 if (qc
->flags
& ATA_QCFLAG_SENSE_VALID
) {
1516 qc
->err_mask
&= ~(AC_ERR_DEV
| AC_ERR_OTHER
);
1517 ehc
->i
.action
&= ~ATA_EH_REVALIDATE
;
1520 /* accumulate error info */
1521 ehc
->i
.dev
= qc
->dev
;
1522 all_err_mask
|= qc
->err_mask
;
1523 if (qc
->flags
& ATA_QCFLAG_IO
)
1527 /* enforce default EH actions */
1528 if (ap
->pflags
& ATA_PFLAG_FROZEN
||
1529 all_err_mask
& (AC_ERR_HSM
| AC_ERR_TIMEOUT
))
1530 ehc
->i
.action
|= ATA_EH_SOFTRESET
;
1531 else if (all_err_mask
)
1532 ehc
->i
.action
|= ATA_EH_REVALIDATE
;
1534 /* if we have offending qcs and the associated failed device */
1537 ehc
->i
.action
|= ata_eh_speed_down(ehc
->i
.dev
, is_io
,
1540 /* perform per-dev EH action only on the offending device */
1541 ehc
->i
.dev_action
[ehc
->i
.dev
->devno
] |=
1542 ehc
->i
.action
& ATA_EH_PERDEV_MASK
;
1543 ehc
->i
.action
&= ~ATA_EH_PERDEV_MASK
;
1550 * ata_eh_report - report error handling to user
1551 * @ap: ATA port EH is going on
1553 * Report EH to user.
1558 static void ata_eh_report(struct ata_port
*ap
)
1560 struct ata_eh_context
*ehc
= &ap
->eh_context
;
1561 const char *frozen
, *desc
;
1562 int tag
, nr_failed
= 0;
1565 if (ehc
->i
.desc
[0] != '\0')
1568 for (tag
= 0; tag
< ATA_MAX_QUEUE
; tag
++) {
1569 struct ata_queued_cmd
*qc
= __ata_qc_from_tag(ap
, tag
);
1571 if (!(qc
->flags
& ATA_QCFLAG_FAILED
))
1573 if (qc
->flags
& ATA_QCFLAG_SENSE_VALID
&& !qc
->err_mask
)
1579 if (!nr_failed
&& !ehc
->i
.err_mask
)
1583 if (ap
->pflags
& ATA_PFLAG_FROZEN
)
1587 ata_dev_printk(ehc
->i
.dev
, KERN_ERR
, "exception Emask 0x%x "
1588 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1589 ehc
->i
.err_mask
, ap
->sactive
, ehc
->i
.serror
,
1590 ehc
->i
.action
, frozen
);
1592 ata_dev_printk(ehc
->i
.dev
, KERN_ERR
, "%s\n", desc
);
1594 ata_port_printk(ap
, KERN_ERR
, "exception Emask 0x%x "
1595 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1596 ehc
->i
.err_mask
, ap
->sactive
, ehc
->i
.serror
,
1597 ehc
->i
.action
, frozen
);
1599 ata_port_printk(ap
, KERN_ERR
, "%s\n", desc
);
1602 for (tag
= 0; tag
< ATA_MAX_QUEUE
; tag
++) {
1603 static const char *dma_str
[] = {
1604 [DMA_BIDIRECTIONAL
] = "bidi",
1605 [DMA_TO_DEVICE
] = "out",
1606 [DMA_FROM_DEVICE
] = "in",
1609 struct ata_queued_cmd
*qc
= __ata_qc_from_tag(ap
, tag
);
1610 struct ata_taskfile
*cmd
= &qc
->tf
, *res
= &qc
->result_tf
;
1612 if (!(qc
->flags
& ATA_QCFLAG_FAILED
) || !qc
->err_mask
)
1615 ata_dev_printk(qc
->dev
, KERN_ERR
,
1616 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
1617 "tag %d cdb 0x%x data %u %s\n "
1618 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
1619 "Emask 0x%x (%s)%s\n",
1620 cmd
->command
, cmd
->feature
, cmd
->nsect
,
1621 cmd
->lbal
, cmd
->lbam
, cmd
->lbah
,
1622 cmd
->hob_feature
, cmd
->hob_nsect
,
1623 cmd
->hob_lbal
, cmd
->hob_lbam
, cmd
->hob_lbah
,
1624 cmd
->device
, qc
->tag
, qc
->cdb
[0], qc
->nbytes
,
1625 dma_str
[qc
->dma_dir
],
1626 res
->command
, res
->feature
, res
->nsect
,
1627 res
->lbal
, res
->lbam
, res
->lbah
,
1628 res
->hob_feature
, res
->hob_nsect
,
1629 res
->hob_lbal
, res
->hob_lbam
, res
->hob_lbah
,
1630 res
->device
, qc
->err_mask
, ata_err_string(qc
->err_mask
),
1631 qc
->err_mask
& AC_ERR_NCQ
? " <F>" : "");
1635 static int ata_do_reset(struct ata_port
*ap
, ata_reset_fn_t reset
,
1636 unsigned int *classes
, unsigned long deadline
)
1640 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
1641 classes
[i
] = ATA_DEV_UNKNOWN
;
1643 rc
= reset(ap
, classes
, deadline
);
1647 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
1648 * is complete and convert all ATA_DEV_UNKNOWN to
1651 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
1652 if (classes
[i
] != ATA_DEV_UNKNOWN
)
1655 if (i
< ATA_MAX_DEVICES
)
1656 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
1657 if (classes
[i
] == ATA_DEV_UNKNOWN
)
1658 classes
[i
] = ATA_DEV_NONE
;
1663 static int ata_eh_followup_srst_needed(int rc
, int classify
,
1664 const unsigned int *classes
)
1670 if (classify
&& classes
[0] == ATA_DEV_UNKNOWN
)
1675 static int ata_eh_reset(struct ata_port
*ap
, int classify
,
1676 ata_prereset_fn_t prereset
, ata_reset_fn_t softreset
,
1677 ata_reset_fn_t hardreset
, ata_postreset_fn_t postreset
)
1679 struct ata_eh_context
*ehc
= &ap
->eh_context
;
1680 unsigned int *classes
= ehc
->classes
;
1681 int verbose
= !(ehc
->i
.flags
& ATA_EHI_QUIET
);
1683 unsigned long deadline
;
1684 unsigned int action
;
1685 ata_reset_fn_t reset
;
1688 /* about to reset */
1689 ata_eh_about_to_do(ap
, NULL
, ehc
->i
.action
& ATA_EH_RESET_MASK
);
1691 /* Determine which reset to use and record in ehc->i.action.
1692 * prereset() may examine and modify it.
1694 action
= ehc
->i
.action
;
1695 ehc
->i
.action
&= ~ATA_EH_RESET_MASK
;
1696 if (softreset
&& (!hardreset
|| (!sata_set_spd_needed(ap
) &&
1697 !(action
& ATA_EH_HARDRESET
))))
1698 ehc
->i
.action
|= ATA_EH_SOFTRESET
;
1700 ehc
->i
.action
|= ATA_EH_HARDRESET
;
1703 rc
= prereset(ap
, jiffies
+ ATA_EH_PRERESET_TIMEOUT
);
1705 if (rc
== -ENOENT
) {
1706 ata_port_printk(ap
, KERN_DEBUG
,
1707 "port disabled. ignoring.\n");
1708 ap
->eh_context
.i
.action
&= ~ATA_EH_RESET_MASK
;
1710 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
1711 classes
[i
] = ATA_DEV_NONE
;
1715 ata_port_printk(ap
, KERN_ERR
,
1716 "prereset failed (errno=%d)\n", rc
);
1721 /* prereset() might have modified ehc->i.action */
1722 if (ehc
->i
.action
& ATA_EH_HARDRESET
)
1724 else if (ehc
->i
.action
& ATA_EH_SOFTRESET
)
1727 /* prereset told us not to reset, bang classes and return */
1728 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
1729 classes
[i
] = ATA_DEV_NONE
;
1733 /* did prereset() screw up? if so, fix up to avoid oopsing */
1742 deadline
= jiffies
+ ata_eh_reset_timeouts
[try++];
1744 /* shut up during boot probing */
1746 ata_port_printk(ap
, KERN_INFO
, "%s resetting port\n",
1747 reset
== softreset
? "soft" : "hard");
1749 /* mark that this EH session started with reset */
1750 if (reset
== hardreset
)
1751 ehc
->i
.flags
|= ATA_EHI_DID_HARDRESET
;
1753 ehc
->i
.flags
|= ATA_EHI_DID_SOFTRESET
;
1755 rc
= ata_do_reset(ap
, reset
, classes
, deadline
);
1757 if (reset
== hardreset
&&
1758 ata_eh_followup_srst_needed(rc
, classify
, classes
)) {
1759 /* okay, let's do follow-up softreset */
1763 ata_port_printk(ap
, KERN_ERR
,
1764 "follow-up softreset required "
1765 "but no softreset avaliable\n");
1769 ata_eh_about_to_do(ap
, NULL
, ATA_EH_RESET_MASK
);
1770 rc
= ata_do_reset(ap
, reset
, classes
, deadline
);
1772 if (rc
== 0 && classify
&&
1773 classes
[0] == ATA_DEV_UNKNOWN
) {
1774 ata_port_printk(ap
, KERN_ERR
,
1775 "classification failed\n");
1780 if (rc
&& try < ARRAY_SIZE(ata_eh_reset_timeouts
)) {
1781 unsigned long now
= jiffies
;
1783 if (time_before(now
, deadline
)) {
1784 unsigned long delta
= deadline
- jiffies
;
1786 ata_port_printk(ap
, KERN_WARNING
, "reset failed "
1787 "(errno=%d), retrying in %u secs\n",
1788 rc
, (jiffies_to_msecs(delta
) + 999) / 1000);
1790 schedule_timeout_uninterruptible(delta
);
1794 try == ARRAY_SIZE(ata_eh_reset_timeouts
) - 1)
1795 sata_down_spd_limit(ap
);
1804 /* After the reset, the device state is PIO 0 and the
1805 * controller state is undefined. Record the mode.
1807 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
1808 ap
->device
[i
].pio_mode
= XFER_PIO_0
;
1810 /* record current link speed */
1811 if (sata_scr_read(ap
, SCR_STATUS
, &sstatus
) == 0)
1812 ap
->sata_spd
= (sstatus
>> 4) & 0xf;
1815 postreset(ap
, classes
);
1817 /* reset successful, schedule revalidation */
1818 ata_eh_done(ap
, NULL
, ehc
->i
.action
& ATA_EH_RESET_MASK
);
1819 ehc
->i
.action
|= ATA_EH_REVALIDATE
;
1825 static int ata_eh_revalidate_and_attach(struct ata_port
*ap
,
1826 struct ata_device
**r_failed_dev
)
1828 struct ata_eh_context
*ehc
= &ap
->eh_context
;
1829 struct ata_device
*dev
;
1830 unsigned int new_mask
= 0;
1831 unsigned long flags
;
1836 /* For PATA drive side cable detection to work, IDENTIFY must
1837 * be done backwards such that PDIAG- is released by the slave
1838 * device before the master device is identified.
1840 for (i
= ATA_MAX_DEVICES
- 1; i
>= 0; i
--) {
1841 unsigned int action
, readid_flags
= 0;
1843 dev
= &ap
->device
[i
];
1844 action
= ata_eh_dev_action(dev
);
1846 if (ehc
->i
.flags
& ATA_EHI_DID_RESET
)
1847 readid_flags
|= ATA_READID_POSTRESET
;
1849 if ((action
& ATA_EH_REVALIDATE
) && ata_dev_enabled(dev
)) {
1850 if (ata_port_offline(ap
)) {
1855 ata_eh_about_to_do(ap
, dev
, ATA_EH_REVALIDATE
);
1856 rc
= ata_dev_revalidate(dev
, readid_flags
);
1860 ata_eh_done(ap
, dev
, ATA_EH_REVALIDATE
);
1862 /* Configuration may have changed, reconfigure
1865 ehc
->i
.flags
|= ATA_EHI_SETMODE
;
1867 /* schedule the scsi_rescan_device() here */
1868 queue_work(ata_aux_wq
, &(ap
->scsi_rescan_task
));
1869 } else if (dev
->class == ATA_DEV_UNKNOWN
&&
1870 ehc
->tries
[dev
->devno
] &&
1871 ata_class_enabled(ehc
->classes
[dev
->devno
])) {
1872 dev
->class = ehc
->classes
[dev
->devno
];
1874 rc
= ata_dev_read_id(dev
, &dev
->class, readid_flags
,
1881 /* IDENTIFY was issued to non-existent
1882 * device. No need to reset. Just
1883 * thaw and kill the device.
1885 ata_eh_thaw_port(ap
);
1886 dev
->class = ATA_DEV_UNKNOWN
;
1889 dev
->class = ATA_DEV_UNKNOWN
;
1895 /* PDIAG- should have been released, ask cable type if post-reset */
1896 if ((ehc
->i
.flags
& ATA_EHI_DID_RESET
) && ap
->ops
->cable_detect
)
1897 ap
->cbl
= ap
->ops
->cable_detect(ap
);
1899 /* Configure new devices forward such that user doesn't see
1900 * device detection messages backwards.
1902 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
1903 dev
= &ap
->device
[i
];
1905 if (!(new_mask
& (1 << i
)))
1908 ehc
->i
.flags
|= ATA_EHI_PRINTINFO
;
1909 rc
= ata_dev_configure(dev
);
1910 ehc
->i
.flags
&= ~ATA_EHI_PRINTINFO
;
1914 spin_lock_irqsave(ap
->lock
, flags
);
1915 ap
->pflags
|= ATA_PFLAG_SCSI_HOTPLUG
;
1916 spin_unlock_irqrestore(ap
->lock
, flags
);
1918 /* new device discovered, configure xfermode */
1919 ehc
->i
.flags
|= ATA_EHI_SETMODE
;
1925 *r_failed_dev
= dev
;
1926 DPRINTK("EXIT rc=%d\n", rc
);
1930 static int ata_port_nr_enabled(struct ata_port
*ap
)
1934 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
1935 if (ata_dev_enabled(&ap
->device
[i
]))
1940 static int ata_port_nr_vacant(struct ata_port
*ap
)
1944 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
1945 if (ap
->device
[i
].class == ATA_DEV_UNKNOWN
)
1950 static int ata_eh_skip_recovery(struct ata_port
*ap
)
1952 struct ata_eh_context
*ehc
= &ap
->eh_context
;
1955 /* thaw frozen port, resume link and recover failed devices */
1956 if ((ap
->pflags
& ATA_PFLAG_FROZEN
) ||
1957 (ehc
->i
.flags
& ATA_EHI_RESUME_LINK
) || ata_port_nr_enabled(ap
))
1960 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
1961 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
1962 struct ata_device
*dev
= &ap
->device
[i
];
1964 if (dev
->class == ATA_DEV_UNKNOWN
&&
1965 ehc
->classes
[dev
->devno
] != ATA_DEV_NONE
)
1972 static void ata_eh_handle_dev_fail(struct ata_device
*dev
, int err
)
1974 struct ata_port
*ap
= dev
->ap
;
1975 struct ata_eh_context
*ehc
= &ap
->eh_context
;
1977 ehc
->tries
[dev
->devno
]--;
1981 /* device missing or wrong IDENTIFY data, schedule probing */
1982 ehc
->i
.probe_mask
|= (1 << dev
->devno
);
1984 /* give it just one more chance */
1985 ehc
->tries
[dev
->devno
] = min(ehc
->tries
[dev
->devno
], 1);
1987 if (ehc
->tries
[dev
->devno
] == 1) {
1988 /* This is the last chance, better to slow
1989 * down than lose it.
1991 sata_down_spd_limit(ap
);
1992 ata_down_xfermask_limit(dev
, ATA_DNXFER_PIO
);
1996 if (ata_dev_enabled(dev
) && !ehc
->tries
[dev
->devno
]) {
1997 /* disable device if it has used up all its chances */
1998 ata_dev_disable(dev
);
2000 /* detach if offline */
2001 if (ata_port_offline(ap
))
2002 ata_eh_detach_dev(dev
);
2004 /* probe if requested */
2005 if ((ehc
->i
.probe_mask
& (1 << dev
->devno
)) &&
2006 !(ehc
->did_probe_mask
& (1 << dev
->devno
))) {
2007 ata_eh_detach_dev(dev
);
2010 ehc
->tries
[dev
->devno
] = ATA_EH_DEV_TRIES
;
2011 ehc
->did_probe_mask
|= (1 << dev
->devno
);
2012 ehc
->i
.action
|= ATA_EH_SOFTRESET
;
2015 /* soft didn't work? be haaaaard */
2016 if (ehc
->i
.flags
& ATA_EHI_DID_RESET
)
2017 ehc
->i
.action
|= ATA_EH_HARDRESET
;
2019 ehc
->i
.action
|= ATA_EH_SOFTRESET
;
2024 * ata_eh_recover - recover host port after error
2025 * @ap: host port to recover
2026 * @prereset: prereset method (can be NULL)
2027 * @softreset: softreset method (can be NULL)
2028 * @hardreset: hardreset method (can be NULL)
2029 * @postreset: postreset method (can be NULL)
2031 * This is the alpha and omega, eum and yang, heart and soul of
2032 * libata exception handling. On entry, actions required to
2033 * recover the port and hotplug requests are recorded in
2034 * eh_context. This function executes all the operations with
2035 * appropriate retrials and fallbacks to resurrect failed
2036 * devices, detach goners and greet newcomers.
2039 * Kernel thread context (may sleep).
2042 * 0 on success, -errno on failure.
2044 static int ata_eh_recover(struct ata_port
*ap
, ata_prereset_fn_t prereset
,
2045 ata_reset_fn_t softreset
, ata_reset_fn_t hardreset
,
2046 ata_postreset_fn_t postreset
)
2048 struct ata_eh_context
*ehc
= &ap
->eh_context
;
2049 struct ata_device
*dev
;
2054 /* prep for recovery */
2055 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
2056 dev
= &ap
->device
[i
];
2058 ehc
->tries
[dev
->devno
] = ATA_EH_DEV_TRIES
;
2060 /* collect port action mask recorded in dev actions */
2061 ehc
->i
.action
|= ehc
->i
.dev_action
[i
] & ~ATA_EH_PERDEV_MASK
;
2062 ehc
->i
.dev_action
[i
] &= ATA_EH_PERDEV_MASK
;
2064 /* process hotplug request */
2065 if (dev
->flags
& ATA_DFLAG_DETACH
)
2066 ata_eh_detach_dev(dev
);
2068 if (!ata_dev_enabled(dev
) &&
2069 ((ehc
->i
.probe_mask
& (1 << dev
->devno
)) &&
2070 !(ehc
->did_probe_mask
& (1 << dev
->devno
)))) {
2071 ata_eh_detach_dev(dev
);
2073 ehc
->did_probe_mask
|= (1 << dev
->devno
);
2074 ehc
->i
.action
|= ATA_EH_SOFTRESET
;
2081 /* if UNLOADING, finish immediately */
2082 if (ap
->pflags
& ATA_PFLAG_UNLOADING
)
2085 /* skip EH if possible. */
2086 if (ata_eh_skip_recovery(ap
))
2089 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
2090 ehc
->classes
[i
] = ATA_DEV_UNKNOWN
;
2093 if (ehc
->i
.action
& ATA_EH_RESET_MASK
) {
2094 ata_eh_freeze_port(ap
);
2096 rc
= ata_eh_reset(ap
, ata_port_nr_vacant(ap
), prereset
,
2097 softreset
, hardreset
, postreset
);
2099 ata_port_printk(ap
, KERN_ERR
,
2100 "reset failed, giving up\n");
2104 ata_eh_thaw_port(ap
);
2107 /* revalidate existing devices and attach new ones */
2108 rc
= ata_eh_revalidate_and_attach(ap
, &dev
);
2112 /* configure transfer mode if necessary */
2113 if (ehc
->i
.flags
& ATA_EHI_SETMODE
) {
2114 rc
= ata_set_mode(ap
, &dev
);
2117 ehc
->i
.flags
&= ~ATA_EHI_SETMODE
;
2123 ata_eh_handle_dev_fail(dev
, rc
);
2125 if (ata_port_nr_enabled(ap
)) {
2126 ata_port_printk(ap
, KERN_WARNING
, "failed to recover some "
2127 "devices, retrying in 5 secs\n");
2130 /* no device left, repeat fast */
2138 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
2139 ata_dev_disable(&ap
->device
[i
]);
2142 DPRINTK("EXIT, rc=%d\n", rc
);
2147 * ata_eh_finish - finish up EH
2148 * @ap: host port to finish EH for
2150 * Recovery is complete. Clean up EH states and retry or finish
2156 static void ata_eh_finish(struct ata_port
*ap
)
2160 /* retry or finish qcs */
2161 for (tag
= 0; tag
< ATA_MAX_QUEUE
; tag
++) {
2162 struct ata_queued_cmd
*qc
= __ata_qc_from_tag(ap
, tag
);
2164 if (!(qc
->flags
& ATA_QCFLAG_FAILED
))
2168 /* FIXME: Once EH migration is complete,
2169 * generate sense data in this function,
2170 * considering both err_mask and tf.
2172 if (qc
->err_mask
& AC_ERR_INVALID
)
2173 ata_eh_qc_complete(qc
);
2175 ata_eh_qc_retry(qc
);
2177 if (qc
->flags
& ATA_QCFLAG_SENSE_VALID
) {
2178 ata_eh_qc_complete(qc
);
2180 /* feed zero TF to sense generation */
2181 memset(&qc
->result_tf
, 0, sizeof(qc
->result_tf
));
2182 ata_eh_qc_retry(qc
);
2189 * ata_do_eh - do standard error handling
2190 * @ap: host port to handle error for
2191 * @prereset: prereset method (can be NULL)
2192 * @softreset: softreset method (can be NULL)
2193 * @hardreset: hardreset method (can be NULL)
2194 * @postreset: postreset method (can be NULL)
2196 * Perform standard error handling sequence.
2199 * Kernel thread context (may sleep).
2201 void ata_do_eh(struct ata_port
*ap
, ata_prereset_fn_t prereset
,
2202 ata_reset_fn_t softreset
, ata_reset_fn_t hardreset
,
2203 ata_postreset_fn_t postreset
)
2207 ata_eh_recover(ap
, prereset
, softreset
, hardreset
, postreset
);
2213 * ata_eh_handle_port_suspend - perform port suspend operation
2214 * @ap: port to suspend
2219 * Kernel thread context (may sleep).
2221 static void ata_eh_handle_port_suspend(struct ata_port
*ap
)
2223 unsigned long flags
;
2226 /* are we suspending? */
2227 spin_lock_irqsave(ap
->lock
, flags
);
2228 if (!(ap
->pflags
& ATA_PFLAG_PM_PENDING
) ||
2229 ap
->pm_mesg
.event
== PM_EVENT_ON
) {
2230 spin_unlock_irqrestore(ap
->lock
, flags
);
2233 spin_unlock_irqrestore(ap
->lock
, flags
);
2235 WARN_ON(ap
->pflags
& ATA_PFLAG_SUSPENDED
);
2237 /* tell ACPI we're suspending */
2238 rc
= ata_acpi_on_suspend(ap
);
2243 ata_eh_freeze_port(ap
);
2245 if (ap
->ops
->port_suspend
)
2246 rc
= ap
->ops
->port_suspend(ap
, ap
->pm_mesg
);
2250 spin_lock_irqsave(ap
->lock
, flags
);
2252 ap
->pflags
&= ~ATA_PFLAG_PM_PENDING
;
2254 ap
->pflags
|= ATA_PFLAG_SUSPENDED
;
2255 else if (ap
->pflags
& ATA_PFLAG_FROZEN
)
2256 ata_port_schedule_eh(ap
);
2258 if (ap
->pm_result
) {
2259 *ap
->pm_result
= rc
;
2260 ap
->pm_result
= NULL
;
2263 spin_unlock_irqrestore(ap
->lock
, flags
);
2269 * ata_eh_handle_port_resume - perform port resume operation
2270 * @ap: port to resume
2275 * Kernel thread context (may sleep).
2277 static void ata_eh_handle_port_resume(struct ata_port
*ap
)
2279 unsigned long flags
;
2282 /* are we resuming? */
2283 spin_lock_irqsave(ap
->lock
, flags
);
2284 if (!(ap
->pflags
& ATA_PFLAG_PM_PENDING
) ||
2285 ap
->pm_mesg
.event
!= PM_EVENT_ON
) {
2286 spin_unlock_irqrestore(ap
->lock
, flags
);
2289 spin_unlock_irqrestore(ap
->lock
, flags
);
2291 WARN_ON(!(ap
->pflags
& ATA_PFLAG_SUSPENDED
));
2293 if (ap
->ops
->port_resume
)
2294 rc
= ap
->ops
->port_resume(ap
);
2296 /* tell ACPI that we're resuming */
2297 ata_acpi_on_resume(ap
);
2300 spin_lock_irqsave(ap
->lock
, flags
);
2301 ap
->pflags
&= ~(ATA_PFLAG_PM_PENDING
| ATA_PFLAG_SUSPENDED
);
2302 if (ap
->pm_result
) {
2303 *ap
->pm_result
= rc
;
2304 ap
->pm_result
= NULL
;
2306 spin_unlock_irqrestore(ap
->lock
, flags
);
2308 #endif /* CONFIG_PM */