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/config.h>
36 #include <linux/kernel.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_transport_api.h"
44 #include <linux/libata.h>
48 static void __ata_port_freeze(struct ata_port
*ap
);
49 static void ata_eh_finish(struct ata_port
*ap
);
51 static void ata_ering_record(struct ata_ering
*ering
, int is_io
,
52 unsigned int err_mask
)
54 struct ata_ering_entry
*ent
;
59 ering
->cursor
%= ATA_ERING_SIZE
;
61 ent
= &ering
->ring
[ering
->cursor
];
63 ent
->err_mask
= err_mask
;
64 ent
->timestamp
= get_jiffies_64();
67 static struct ata_ering_entry
* ata_ering_top(struct ata_ering
*ering
)
69 struct ata_ering_entry
*ent
= &ering
->ring
[ering
->cursor
];
75 static int ata_ering_map(struct ata_ering
*ering
,
76 int (*map_fn
)(struct ata_ering_entry
*, void *),
80 struct ata_ering_entry
*ent
;
84 ent
= &ering
->ring
[idx
];
87 rc
= map_fn(ent
, arg
);
90 idx
= (idx
- 1 + ATA_ERING_SIZE
) % ATA_ERING_SIZE
;
91 } while (idx
!= ering
->cursor
);
97 * ata_scsi_timed_out - SCSI layer time out callback
98 * @cmd: timed out SCSI command
100 * Handles SCSI layer timeout. We race with normal completion of
101 * the qc for @cmd. If the qc is already gone, we lose and let
102 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
103 * timed out and EH should be invoked. Prevent ata_qc_complete()
104 * from finishing it by setting EH_SCHEDULED and return
107 * TODO: kill this function once old EH is gone.
110 * Called from timer context
113 * EH_HANDLED or EH_NOT_HANDLED
115 enum scsi_eh_timer_return
ata_scsi_timed_out(struct scsi_cmnd
*cmd
)
117 struct Scsi_Host
*host
= cmd
->device
->host
;
118 struct ata_port
*ap
= ata_shost_to_port(host
);
120 struct ata_queued_cmd
*qc
;
121 enum scsi_eh_timer_return ret
;
125 if (ap
->ops
->error_handler
) {
126 ret
= EH_NOT_HANDLED
;
131 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
132 qc
= ata_qc_from_tag(ap
, ap
->active_tag
);
134 WARN_ON(qc
->scsicmd
!= cmd
);
135 qc
->flags
|= ATA_QCFLAG_EH_SCHEDULED
;
136 qc
->err_mask
|= AC_ERR_TIMEOUT
;
137 ret
= EH_NOT_HANDLED
;
139 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
142 DPRINTK("EXIT, ret=%d\n", ret
);
147 * ata_scsi_error - SCSI layer error handler callback
148 * @host: SCSI host on which error occurred
150 * Handles SCSI-layer-thrown error events.
153 * Inherited from SCSI layer (none, can sleep)
158 void ata_scsi_error(struct Scsi_Host
*host
)
160 struct ata_port
*ap
= ata_shost_to_port(host
);
161 spinlock_t
*hs_lock
= &ap
->host_set
->lock
;
162 int i
, repeat_cnt
= ATA_EH_MAX_REPEAT
;
167 /* synchronize with port task */
168 ata_port_flush_task(ap
);
170 /* synchronize with host_set lock and sort out timeouts */
172 /* For new EH, all qcs are finished in one of three ways -
173 * normal completion, error completion, and SCSI timeout.
174 * Both cmpletions can race against SCSI timeout. When normal
175 * completion wins, the qc never reaches EH. When error
176 * completion wins, the qc has ATA_QCFLAG_FAILED set.
178 * When SCSI timeout wins, things are a bit more complex.
179 * Normal or error completion can occur after the timeout but
180 * before this point. In such cases, both types of
181 * completions are honored. A scmd is determined to have
182 * timed out iff its associated qc is active and not failed.
184 if (ap
->ops
->error_handler
) {
185 struct scsi_cmnd
*scmd
, *tmp
;
188 spin_lock_irqsave(hs_lock
, flags
);
190 list_for_each_entry_safe(scmd
, tmp
, &host
->eh_cmd_q
, eh_entry
) {
191 struct ata_queued_cmd
*qc
;
193 for (i
= 0; i
< ATA_MAX_QUEUE
; i
++) {
194 qc
= __ata_qc_from_tag(ap
, i
);
195 if (qc
->flags
& ATA_QCFLAG_ACTIVE
&&
200 if (i
< ATA_MAX_QUEUE
) {
201 /* the scmd has an associated qc */
202 if (!(qc
->flags
& ATA_QCFLAG_FAILED
)) {
203 /* which hasn't failed yet, timeout */
204 qc
->err_mask
|= AC_ERR_TIMEOUT
;
205 qc
->flags
|= ATA_QCFLAG_FAILED
;
209 /* Normal completion occurred after
210 * SCSI timeout but before this point.
211 * Successfully complete it.
213 scmd
->retries
= scmd
->allowed
;
214 scsi_eh_finish_cmd(scmd
, &ap
->eh_done_q
);
218 /* If we have timed out qcs. They belong to EH from
219 * this point but the state of the controller is
220 * unknown. Freeze the port to make sure the IRQ
221 * handler doesn't diddle with those qcs. This must
222 * be done atomically w.r.t. setting QCFLAG_FAILED.
225 __ata_port_freeze(ap
);
227 spin_unlock_irqrestore(hs_lock
, flags
);
229 spin_unlock_wait(hs_lock
);
232 /* invoke error handler */
233 if (ap
->ops
->error_handler
) {
234 /* fetch & clear EH info */
235 spin_lock_irqsave(hs_lock
, flags
);
237 memset(&ap
->eh_context
, 0, sizeof(ap
->eh_context
));
238 ap
->eh_context
.i
= ap
->eh_info
;
239 memset(&ap
->eh_info
, 0, sizeof(ap
->eh_info
));
241 ap
->flags
|= ATA_FLAG_EH_IN_PROGRESS
;
242 ap
->flags
&= ~ATA_FLAG_EH_PENDING
;
244 spin_unlock_irqrestore(hs_lock
, flags
);
246 /* invoke EH. if unloading, just finish failed qcs */
247 if (!(ap
->flags
& ATA_FLAG_UNLOADING
))
248 ap
->ops
->error_handler(ap
);
252 /* Exception might have happend after ->error_handler
253 * recovered the port but before this point. Repeat
256 spin_lock_irqsave(hs_lock
, flags
);
258 if (ap
->flags
& ATA_FLAG_EH_PENDING
) {
260 ata_port_printk(ap
, KERN_INFO
,
261 "EH pending after completion, "
262 "repeating EH (cnt=%d)\n", repeat_cnt
);
263 spin_unlock_irqrestore(hs_lock
, flags
);
266 ata_port_printk(ap
, KERN_ERR
, "EH pending after %d "
267 "tries, giving up\n", ATA_EH_MAX_REPEAT
);
270 /* this run is complete, make sure EH info is clear */
271 memset(&ap
->eh_info
, 0, sizeof(ap
->eh_info
));
273 /* Clear host_eh_scheduled while holding hs_lock such
274 * that if exception occurs after this point but
275 * before EH completion, SCSI midlayer will
278 host
->host_eh_scheduled
= 0;
280 spin_unlock_irqrestore(hs_lock
, flags
);
282 WARN_ON(ata_qc_from_tag(ap
, ap
->active_tag
) == NULL
);
283 ap
->ops
->eng_timeout(ap
);
286 /* finish or retry handled scmd's and clean up */
287 WARN_ON(host
->host_failed
|| !list_empty(&host
->eh_cmd_q
));
289 scsi_eh_flush_done_q(&ap
->eh_done_q
);
292 spin_lock_irqsave(hs_lock
, flags
);
294 if (ap
->flags
& ATA_FLAG_LOADING
) {
295 ap
->flags
&= ~ATA_FLAG_LOADING
;
297 if (ap
->flags
& ATA_FLAG_SCSI_HOTPLUG
)
298 queue_work(ata_aux_wq
, &ap
->hotplug_task
);
299 if (ap
->flags
& ATA_FLAG_RECOVERED
)
300 ata_port_printk(ap
, KERN_INFO
, "EH complete\n");
303 ap
->flags
&= ~(ATA_FLAG_SCSI_HOTPLUG
| ATA_FLAG_RECOVERED
);
305 /* tell wait_eh that we're done */
306 ap
->flags
&= ~ATA_FLAG_EH_IN_PROGRESS
;
307 wake_up_all(&ap
->eh_wait_q
);
309 spin_unlock_irqrestore(hs_lock
, flags
);
315 * ata_port_wait_eh - Wait for the currently pending EH to complete
316 * @ap: Port to wait EH for
318 * Wait until the currently pending EH is complete.
321 * Kernel thread context (may sleep).
323 void ata_port_wait_eh(struct ata_port
*ap
)
329 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
331 while (ap
->flags
& (ATA_FLAG_EH_PENDING
| ATA_FLAG_EH_IN_PROGRESS
)) {
332 prepare_to_wait(&ap
->eh_wait_q
, &wait
, TASK_UNINTERRUPTIBLE
);
333 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
335 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
337 finish_wait(&ap
->eh_wait_q
, &wait
);
339 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
341 /* make sure SCSI EH is complete */
342 if (scsi_host_in_recovery(ap
->host
)) {
349 * ata_qc_timeout - Handle timeout of queued command
350 * @qc: Command that timed out
352 * Some part of the kernel (currently, only the SCSI layer)
353 * has noticed that the active command on port @ap has not
354 * completed after a specified length of time. Handle this
355 * condition by disabling DMA (if necessary) and completing
356 * transactions, with error if necessary.
358 * This also handles the case of the "lost interrupt", where
359 * for some reason (possibly hardware bug, possibly driver bug)
360 * an interrupt was not delivered to the driver, even though the
361 * transaction completed successfully.
363 * TODO: kill this function once old EH is gone.
366 * Inherited from SCSI layer (none, can sleep)
368 static void ata_qc_timeout(struct ata_queued_cmd
*qc
)
370 struct ata_port
*ap
= qc
->ap
;
371 struct ata_host_set
*host_set
= ap
->host_set
;
372 u8 host_stat
= 0, drv_stat
;
377 ap
->hsm_task_state
= HSM_ST_IDLE
;
379 spin_lock_irqsave(&host_set
->lock
, flags
);
381 switch (qc
->tf
.protocol
) {
384 case ATA_PROT_ATAPI_DMA
:
385 host_stat
= ap
->ops
->bmdma_status(ap
);
387 /* before we do anything else, clear DMA-Start bit */
388 ap
->ops
->bmdma_stop(qc
);
394 drv_stat
= ata_chk_status(ap
);
396 /* ack bmdma irq events */
397 ap
->ops
->irq_clear(ap
);
399 ata_dev_printk(qc
->dev
, KERN_ERR
, "command 0x%x timeout, "
400 "stat 0x%x host_stat 0x%x\n",
401 qc
->tf
.command
, drv_stat
, host_stat
);
403 /* complete taskfile transaction */
404 qc
->err_mask
|= AC_ERR_TIMEOUT
;
408 spin_unlock_irqrestore(&host_set
->lock
, flags
);
410 ata_eh_qc_complete(qc
);
416 * ata_eng_timeout - Handle timeout of queued command
417 * @ap: Port on which timed-out command is active
419 * Some part of the kernel (currently, only the SCSI layer)
420 * has noticed that the active command on port @ap has not
421 * completed after a specified length of time. Handle this
422 * condition by disabling DMA (if necessary) and completing
423 * transactions, with error if necessary.
425 * This also handles the case of the "lost interrupt", where
426 * for some reason (possibly hardware bug, possibly driver bug)
427 * an interrupt was not delivered to the driver, even though the
428 * transaction completed successfully.
430 * TODO: kill this function once old EH is gone.
433 * Inherited from SCSI layer (none, can sleep)
435 void ata_eng_timeout(struct ata_port
*ap
)
439 ata_qc_timeout(ata_qc_from_tag(ap
, ap
->active_tag
));
445 * ata_qc_schedule_eh - schedule qc for error handling
446 * @qc: command to schedule error handling for
448 * Schedule error handling for @qc. EH will kick in as soon as
449 * other commands are drained.
452 * spin_lock_irqsave(host_set lock)
454 void ata_qc_schedule_eh(struct ata_queued_cmd
*qc
)
456 struct ata_port
*ap
= qc
->ap
;
458 WARN_ON(!ap
->ops
->error_handler
);
460 qc
->flags
|= ATA_QCFLAG_FAILED
;
461 qc
->ap
->flags
|= ATA_FLAG_EH_PENDING
;
463 /* The following will fail if timeout has already expired.
464 * ata_scsi_error() takes care of such scmds on EH entry.
465 * Note that ATA_QCFLAG_FAILED is unconditionally set after
466 * this function completes.
468 scsi_req_abort_cmd(qc
->scsicmd
);
472 * ata_port_schedule_eh - schedule error handling without a qc
473 * @ap: ATA port to schedule EH for
475 * Schedule error handling for @ap. EH will kick in as soon as
476 * all commands are drained.
479 * spin_lock_irqsave(host_set lock)
481 void ata_port_schedule_eh(struct ata_port
*ap
)
483 WARN_ON(!ap
->ops
->error_handler
);
485 ap
->flags
|= ATA_FLAG_EH_PENDING
;
486 scsi_schedule_eh(ap
->host
);
488 DPRINTK("port EH scheduled\n");
492 * ata_port_abort - abort all qc's on the port
493 * @ap: ATA port to abort qc's for
495 * Abort all active qc's of @ap and schedule EH.
498 * spin_lock_irqsave(host_set lock)
501 * Number of aborted qc's.
503 int ata_port_abort(struct ata_port
*ap
)
505 int tag
, nr_aborted
= 0;
507 WARN_ON(!ap
->ops
->error_handler
);
509 for (tag
= 0; tag
< ATA_MAX_QUEUE
; tag
++) {
510 struct ata_queued_cmd
*qc
= ata_qc_from_tag(ap
, tag
);
513 qc
->flags
|= ATA_QCFLAG_FAILED
;
520 ata_port_schedule_eh(ap
);
526 * __ata_port_freeze - freeze port
527 * @ap: ATA port to freeze
529 * This function is called when HSM violation or some other
530 * condition disrupts normal operation of the port. Frozen port
531 * is not allowed to perform any operation until the port is
532 * thawed, which usually follows a successful reset.
534 * ap->ops->freeze() callback can be used for freezing the port
535 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
536 * port cannot be frozen hardware-wise, the interrupt handler
537 * must ack and clear interrupts unconditionally while the port
541 * spin_lock_irqsave(host_set lock)
543 static void __ata_port_freeze(struct ata_port
*ap
)
545 WARN_ON(!ap
->ops
->error_handler
);
550 ap
->flags
|= ATA_FLAG_FROZEN
;
552 DPRINTK("ata%u port frozen\n", ap
->id
);
556 * ata_port_freeze - abort & freeze port
557 * @ap: ATA port to freeze
559 * Abort and freeze @ap.
562 * spin_lock_irqsave(host_set lock)
565 * Number of aborted commands.
567 int ata_port_freeze(struct ata_port
*ap
)
571 WARN_ON(!ap
->ops
->error_handler
);
573 nr_aborted
= ata_port_abort(ap
);
574 __ata_port_freeze(ap
);
580 * ata_eh_freeze_port - EH helper to freeze port
581 * @ap: ATA port to freeze
588 void ata_eh_freeze_port(struct ata_port
*ap
)
592 if (!ap
->ops
->error_handler
)
595 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
596 __ata_port_freeze(ap
);
597 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
601 * ata_port_thaw_port - EH helper to thaw port
602 * @ap: ATA port to thaw
604 * Thaw frozen port @ap.
609 void ata_eh_thaw_port(struct ata_port
*ap
)
613 if (!ap
->ops
->error_handler
)
616 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
618 ap
->flags
&= ~ATA_FLAG_FROZEN
;
623 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
625 DPRINTK("ata%u port thawed\n", ap
->id
);
628 static void ata_eh_scsidone(struct scsi_cmnd
*scmd
)
633 static void __ata_eh_qc_complete(struct ata_queued_cmd
*qc
)
635 struct ata_port
*ap
= qc
->ap
;
636 struct scsi_cmnd
*scmd
= qc
->scsicmd
;
639 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
640 qc
->scsidone
= ata_eh_scsidone
;
641 __ata_qc_complete(qc
);
642 WARN_ON(ata_tag_valid(qc
->tag
));
643 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
645 scsi_eh_finish_cmd(scmd
, &ap
->eh_done_q
);
649 * ata_eh_qc_complete - Complete an active ATA command from EH
650 * @qc: Command to complete
652 * Indicate to the mid and upper layers that an ATA command has
653 * completed. To be used from EH.
655 void ata_eh_qc_complete(struct ata_queued_cmd
*qc
)
657 struct scsi_cmnd
*scmd
= qc
->scsicmd
;
658 scmd
->retries
= scmd
->allowed
;
659 __ata_eh_qc_complete(qc
);
663 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
664 * @qc: Command to retry
666 * Indicate to the mid and upper layers that an ATA command
667 * should be retried. To be used from EH.
669 * SCSI midlayer limits the number of retries to scmd->allowed.
670 * scmd->retries is decremented for commands which get retried
671 * due to unrelated failures (qc->err_mask is zero).
673 void ata_eh_qc_retry(struct ata_queued_cmd
*qc
)
675 struct scsi_cmnd
*scmd
= qc
->scsicmd
;
676 if (!qc
->err_mask
&& scmd
->retries
)
678 __ata_eh_qc_complete(qc
);
682 * ata_eh_detach_dev - detach ATA device
683 * @dev: ATA device to detach
690 static void ata_eh_detach_dev(struct ata_device
*dev
)
692 struct ata_port
*ap
= dev
->ap
;
695 ata_dev_disable(dev
);
697 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
699 dev
->flags
&= ~ATA_DFLAG_DETACH
;
701 if (ata_scsi_offline_dev(dev
)) {
702 dev
->flags
|= ATA_DFLAG_DETACHED
;
703 ap
->flags
|= ATA_FLAG_SCSI_HOTPLUG
;
706 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
709 static void ata_eh_clear_action(struct ata_device
*dev
,
710 struct ata_eh_info
*ehi
, unsigned int action
)
715 ehi
->action
&= ~action
;
716 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
717 ehi
->dev_action
[i
] &= ~action
;
719 /* doesn't make sense for port-wide EH actions */
720 WARN_ON(!(action
& ATA_EH_PERDEV_MASK
));
722 /* break ehi->action into ehi->dev_action */
723 if (ehi
->action
& action
) {
724 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
725 ehi
->dev_action
[i
] |= ehi
->action
& action
;
726 ehi
->action
&= ~action
;
729 /* turn off the specified per-dev action */
730 ehi
->dev_action
[dev
->devno
] &= ~action
;
735 * ata_eh_about_to_do - about to perform eh_action
736 * @ap: target ATA port
737 * @dev: target ATA dev for per-dev action (can be NULL)
738 * @action: action about to be performed
740 * Called just before performing EH actions to clear related bits
741 * in @ap->eh_info such that eh actions are not unnecessarily
747 static void ata_eh_about_to_do(struct ata_port
*ap
, struct ata_device
*dev
,
752 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
753 ata_eh_clear_action(dev
, &ap
->eh_info
, action
);
754 ap
->flags
|= ATA_FLAG_RECOVERED
;
755 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
759 * ata_eh_done - EH action complete
760 * @ap: target ATA port
761 * @dev: target ATA dev for per-dev action (can be NULL)
762 * @action: action just completed
764 * Called right after performing EH actions to clear related bits
765 * in @ap->eh_context.
770 static void ata_eh_done(struct ata_port
*ap
, struct ata_device
*dev
,
773 ata_eh_clear_action(dev
, &ap
->eh_context
.i
, action
);
777 * ata_err_string - convert err_mask to descriptive string
778 * @err_mask: error mask to convert to string
780 * Convert @err_mask to descriptive string. Errors are
781 * prioritized according to severity and only the most severe
788 * Descriptive string for @err_mask
790 static const char * ata_err_string(unsigned int err_mask
)
792 if (err_mask
& AC_ERR_HOST_BUS
)
793 return "host bus error";
794 if (err_mask
& AC_ERR_ATA_BUS
)
795 return "ATA bus error";
796 if (err_mask
& AC_ERR_TIMEOUT
)
798 if (err_mask
& AC_ERR_HSM
)
799 return "HSM violation";
800 if (err_mask
& AC_ERR_SYSTEM
)
801 return "internal error";
802 if (err_mask
& AC_ERR_MEDIA
)
803 return "media error";
804 if (err_mask
& AC_ERR_INVALID
)
805 return "invalid argument";
806 if (err_mask
& AC_ERR_DEV
)
807 return "device error";
808 return "unknown error";
812 * ata_read_log_page - read a specific log page
813 * @dev: target device
814 * @page: page to read
815 * @buf: buffer to store read page
816 * @sectors: number of sectors to read
818 * Read log page using READ_LOG_EXT command.
821 * Kernel thread context (may sleep).
824 * 0 on success, AC_ERR_* mask otherwise.
826 static unsigned int ata_read_log_page(struct ata_device
*dev
,
827 u8 page
, void *buf
, unsigned int sectors
)
829 struct ata_taskfile tf
;
830 unsigned int err_mask
;
832 DPRINTK("read log page - page %d\n", page
);
834 ata_tf_init(dev
, &tf
);
835 tf
.command
= ATA_CMD_READ_LOG_EXT
;
838 tf
.hob_nsect
= sectors
>> 8;
839 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_LBA48
| ATA_TFLAG_DEVICE
;
840 tf
.protocol
= ATA_PROT_PIO
;
842 err_mask
= ata_exec_internal(dev
, &tf
, NULL
, DMA_FROM_DEVICE
,
843 buf
, sectors
* ATA_SECT_SIZE
);
845 DPRINTK("EXIT, err_mask=%x\n", err_mask
);
850 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
851 * @dev: Device to read log page 10h from
852 * @tag: Resulting tag of the failed command
853 * @tf: Resulting taskfile registers of the failed command
855 * Read log page 10h to obtain NCQ error details and clear error
859 * Kernel thread context (may sleep).
862 * 0 on success, -errno otherwise.
864 static int ata_eh_read_log_10h(struct ata_device
*dev
,
865 int *tag
, struct ata_taskfile
*tf
)
867 u8
*buf
= dev
->ap
->sector_buf
;
868 unsigned int err_mask
;
872 err_mask
= ata_read_log_page(dev
, ATA_LOG_SATA_NCQ
, buf
, 1);
877 for (i
= 0; i
< ATA_SECT_SIZE
; i
++)
880 ata_dev_printk(dev
, KERN_WARNING
,
881 "invalid checksum 0x%x on log page 10h\n", csum
);
886 *tag
= buf
[0] & 0x1f;
888 tf
->command
= buf
[2];
889 tf
->feature
= buf
[3];
894 tf
->hob_lbal
= buf
[8];
895 tf
->hob_lbam
= buf
[9];
896 tf
->hob_lbah
= buf
[10];
898 tf
->hob_nsect
= buf
[13];
904 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
905 * @dev: device to perform REQUEST_SENSE to
906 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
908 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
909 * SENSE. This function is EH helper.
912 * Kernel thread context (may sleep).
915 * 0 on success, AC_ERR_* mask on failure
917 static unsigned int atapi_eh_request_sense(struct ata_device
*dev
,
918 unsigned char *sense_buf
)
920 struct ata_port
*ap
= dev
->ap
;
921 struct ata_taskfile tf
;
922 u8 cdb
[ATAPI_CDB_LEN
];
924 DPRINTK("ATAPI request sense\n");
926 ata_tf_init(dev
, &tf
);
928 /* FIXME: is this needed? */
929 memset(sense_buf
, 0, SCSI_SENSE_BUFFERSIZE
);
931 /* XXX: why tf_read here? */
932 ap
->ops
->tf_read(ap
, &tf
);
934 /* fill these in, for the case where they are -not- overwritten */
936 sense_buf
[2] = tf
.feature
>> 4;
938 memset(cdb
, 0, ATAPI_CDB_LEN
);
939 cdb
[0] = REQUEST_SENSE
;
940 cdb
[4] = SCSI_SENSE_BUFFERSIZE
;
942 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
943 tf
.command
= ATA_CMD_PACKET
;
945 /* is it pointless to prefer PIO for "safety reasons"? */
946 if (ap
->flags
& ATA_FLAG_PIO_DMA
) {
947 tf
.protocol
= ATA_PROT_ATAPI_DMA
;
948 tf
.feature
|= ATAPI_PKT_DMA
;
950 tf
.protocol
= ATA_PROT_ATAPI
;
951 tf
.lbam
= (8 * 1024) & 0xff;
952 tf
.lbah
= (8 * 1024) >> 8;
955 return ata_exec_internal(dev
, &tf
, cdb
, DMA_FROM_DEVICE
,
956 sense_buf
, SCSI_SENSE_BUFFERSIZE
);
960 * ata_eh_analyze_serror - analyze SError for a failed port
961 * @ap: ATA port to analyze SError for
963 * Analyze SError if available and further determine cause of
969 static void ata_eh_analyze_serror(struct ata_port
*ap
)
971 struct ata_eh_context
*ehc
= &ap
->eh_context
;
972 u32 serror
= ehc
->i
.serror
;
973 unsigned int err_mask
= 0, action
= 0;
975 if (serror
& SERR_PERSISTENT
) {
976 err_mask
|= AC_ERR_ATA_BUS
;
977 action
|= ATA_EH_HARDRESET
;
980 (SERR_DATA_RECOVERED
| SERR_COMM_RECOVERED
| SERR_DATA
)) {
981 err_mask
|= AC_ERR_ATA_BUS
;
982 action
|= ATA_EH_SOFTRESET
;
984 if (serror
& SERR_PROTOCOL
) {
985 err_mask
|= AC_ERR_HSM
;
986 action
|= ATA_EH_SOFTRESET
;
988 if (serror
& SERR_INTERNAL
) {
989 err_mask
|= AC_ERR_SYSTEM
;
990 action
|= ATA_EH_SOFTRESET
;
992 if (serror
& (SERR_PHYRDY_CHG
| SERR_DEV_XCHG
))
993 ata_ehi_hotplugged(&ehc
->i
);
995 ehc
->i
.err_mask
|= err_mask
;
996 ehc
->i
.action
|= action
;
1000 * ata_eh_analyze_ncq_error - analyze NCQ error
1001 * @ap: ATA port to analyze NCQ error for
1003 * Read log page 10h, determine the offending qc and acquire
1004 * error status TF. For NCQ device errors, all LLDDs have to do
1005 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1009 * Kernel thread context (may sleep).
1011 static void ata_eh_analyze_ncq_error(struct ata_port
*ap
)
1013 struct ata_eh_context
*ehc
= &ap
->eh_context
;
1014 struct ata_device
*dev
= ap
->device
;
1015 struct ata_queued_cmd
*qc
;
1016 struct ata_taskfile tf
;
1019 /* if frozen, we can't do much */
1020 if (ap
->flags
& ATA_FLAG_FROZEN
)
1023 /* is it NCQ device error? */
1024 if (!ap
->sactive
|| !(ehc
->i
.err_mask
& AC_ERR_DEV
))
1027 /* has LLDD analyzed already? */
1028 for (tag
= 0; tag
< ATA_MAX_QUEUE
; tag
++) {
1029 qc
= __ata_qc_from_tag(ap
, tag
);
1031 if (!(qc
->flags
& ATA_QCFLAG_FAILED
))
1038 /* okay, this error is ours */
1039 rc
= ata_eh_read_log_10h(dev
, &tag
, &tf
);
1041 ata_port_printk(ap
, KERN_ERR
, "failed to read log page 10h "
1042 "(errno=%d)\n", rc
);
1046 if (!(ap
->sactive
& (1 << tag
))) {
1047 ata_port_printk(ap
, KERN_ERR
, "log page 10h reported "
1048 "inactive tag %d\n", tag
);
1052 /* we've got the perpetrator, condemn it */
1053 qc
= __ata_qc_from_tag(ap
, tag
);
1054 memcpy(&qc
->result_tf
, &tf
, sizeof(tf
));
1055 qc
->err_mask
|= AC_ERR_DEV
;
1056 ehc
->i
.err_mask
&= ~AC_ERR_DEV
;
1060 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1061 * @qc: qc to analyze
1062 * @tf: Taskfile registers to analyze
1064 * Analyze taskfile of @qc and further determine cause of
1065 * failure. This function also requests ATAPI sense data if
1069 * Kernel thread context (may sleep).
1072 * Determined recovery action
1074 static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd
*qc
,
1075 const struct ata_taskfile
*tf
)
1077 unsigned int tmp
, action
= 0;
1078 u8 stat
= tf
->command
, err
= tf
->feature
;
1080 if ((stat
& (ATA_BUSY
| ATA_DRQ
| ATA_DRDY
)) != ATA_DRDY
) {
1081 qc
->err_mask
|= AC_ERR_HSM
;
1082 return ATA_EH_SOFTRESET
;
1085 if (!(qc
->err_mask
& AC_ERR_DEV
))
1088 switch (qc
->dev
->class) {
1091 qc
->err_mask
|= AC_ERR_ATA_BUS
;
1093 qc
->err_mask
|= AC_ERR_MEDIA
;
1095 qc
->err_mask
|= AC_ERR_INVALID
;
1099 tmp
= atapi_eh_request_sense(qc
->dev
,
1100 qc
->scsicmd
->sense_buffer
);
1102 /* ATA_QCFLAG_SENSE_VALID is used to tell
1103 * atapi_qc_complete() that sense data is
1106 * TODO: interpret sense data and set
1107 * appropriate err_mask.
1109 qc
->flags
|= ATA_QCFLAG_SENSE_VALID
;
1111 qc
->err_mask
|= tmp
;
1114 if (qc
->err_mask
& (AC_ERR_HSM
| AC_ERR_TIMEOUT
| AC_ERR_ATA_BUS
))
1115 action
|= ATA_EH_SOFTRESET
;
1120 static int ata_eh_categorize_ering_entry(struct ata_ering_entry
*ent
)
1122 if (ent
->err_mask
& (AC_ERR_ATA_BUS
| AC_ERR_TIMEOUT
))
1126 if (ent
->err_mask
& AC_ERR_HSM
)
1128 if ((ent
->err_mask
&
1129 (AC_ERR_DEV
|AC_ERR_MEDIA
|AC_ERR_INVALID
)) == AC_ERR_DEV
)
1136 struct speed_down_needed_arg
{
1141 static int speed_down_needed_cb(struct ata_ering_entry
*ent
, void *void_arg
)
1143 struct speed_down_needed_arg
*arg
= void_arg
;
1145 if (ent
->timestamp
< arg
->since
)
1148 arg
->nr_errors
[ata_eh_categorize_ering_entry(ent
)]++;
1153 * ata_eh_speed_down_needed - Determine wheter speed down is necessary
1154 * @dev: Device of interest
1156 * This function examines error ring of @dev and determines
1157 * whether speed down is necessary. Speed down is necessary if
1158 * there have been more than 3 of Cat-1 errors or 10 of Cat-2
1159 * errors during last 15 minutes.
1161 * Cat-1 errors are ATA_BUS, TIMEOUT for any command and HSM
1162 * violation for known supported commands.
1164 * Cat-2 errors are unclassified DEV error for known supported
1168 * Inherited from caller.
1171 * 1 if speed down is necessary, 0 otherwise
1173 static int ata_eh_speed_down_needed(struct ata_device
*dev
)
1175 const u64 interval
= 15LLU * 60 * HZ
;
1176 static const int err_limits
[3] = { -1, 3, 10 };
1177 struct speed_down_needed_arg arg
;
1178 struct ata_ering_entry
*ent
;
1182 ent
= ata_ering_top(&dev
->ering
);
1186 err_cat
= ata_eh_categorize_ering_entry(ent
);
1190 memset(&arg
, 0, sizeof(arg
));
1192 j64
= get_jiffies_64();
1193 if (j64
>= interval
)
1194 arg
.since
= j64
- interval
;
1198 ata_ering_map(&dev
->ering
, speed_down_needed_cb
, &arg
);
1200 return arg
.nr_errors
[err_cat
] > err_limits
[err_cat
];
1204 * ata_eh_speed_down - record error and speed down if necessary
1205 * @dev: Failed device
1206 * @is_io: Did the device fail during normal IO?
1207 * @err_mask: err_mask of the error
1209 * Record error and examine error history to determine whether
1210 * adjusting transmission speed is necessary. It also sets
1211 * transmission limits appropriately if such adjustment is
1215 * Kernel thread context (may sleep).
1218 * 0 on success, -errno otherwise
1220 static int ata_eh_speed_down(struct ata_device
*dev
, int is_io
,
1221 unsigned int err_mask
)
1226 /* record error and determine whether speed down is necessary */
1227 ata_ering_record(&dev
->ering
, is_io
, err_mask
);
1229 if (!ata_eh_speed_down_needed(dev
))
1232 /* speed down SATA link speed if possible */
1233 if (sata_down_spd_limit(dev
->ap
) == 0)
1234 return ATA_EH_HARDRESET
;
1236 /* lower transfer mode */
1237 if (ata_down_xfermask_limit(dev
, 0) == 0)
1238 return ATA_EH_SOFTRESET
;
1240 ata_dev_printk(dev
, KERN_ERR
,
1241 "speed down requested but no transfer mode left\n");
1246 * ata_eh_autopsy - analyze error and determine recovery action
1247 * @ap: ATA port to perform autopsy on
1249 * Analyze why @ap failed and determine which recovery action is
1250 * needed. This function also sets more detailed AC_ERR_* values
1251 * and fills sense data for ATAPI CHECK SENSE.
1254 * Kernel thread context (may sleep).
1256 static void ata_eh_autopsy(struct ata_port
*ap
)
1258 struct ata_eh_context
*ehc
= &ap
->eh_context
;
1259 unsigned int action
= ehc
->i
.action
;
1260 struct ata_device
*failed_dev
= NULL
;
1261 unsigned int all_err_mask
= 0;
1268 /* obtain and analyze SError */
1269 rc
= sata_scr_read(ap
, SCR_ERROR
, &serror
);
1271 ehc
->i
.serror
|= serror
;
1272 ata_eh_analyze_serror(ap
);
1273 } else if (rc
!= -EOPNOTSUPP
)
1274 action
|= ATA_EH_HARDRESET
;
1276 /* analyze NCQ failure */
1277 ata_eh_analyze_ncq_error(ap
);
1279 /* any real error trumps AC_ERR_OTHER */
1280 if (ehc
->i
.err_mask
& ~AC_ERR_OTHER
)
1281 ehc
->i
.err_mask
&= ~AC_ERR_OTHER
;
1283 all_err_mask
|= ehc
->i
.err_mask
;
1285 for (tag
= 0; tag
< ATA_MAX_QUEUE
; tag
++) {
1286 struct ata_queued_cmd
*qc
= __ata_qc_from_tag(ap
, tag
);
1288 if (!(qc
->flags
& ATA_QCFLAG_FAILED
))
1291 /* inherit upper level err_mask */
1292 qc
->err_mask
|= ehc
->i
.err_mask
;
1295 action
|= ata_eh_analyze_tf(qc
, &qc
->result_tf
);
1297 /* DEV errors are probably spurious in case of ATA_BUS error */
1298 if (qc
->err_mask
& AC_ERR_ATA_BUS
)
1299 qc
->err_mask
&= ~(AC_ERR_DEV
| AC_ERR_MEDIA
|
1302 /* any real error trumps unknown error */
1303 if (qc
->err_mask
& ~AC_ERR_OTHER
)
1304 qc
->err_mask
&= ~AC_ERR_OTHER
;
1306 /* SENSE_VALID trumps dev/unknown error and revalidation */
1307 if (qc
->flags
& ATA_QCFLAG_SENSE_VALID
) {
1308 qc
->err_mask
&= ~(AC_ERR_DEV
| AC_ERR_OTHER
);
1309 action
&= ~ATA_EH_REVALIDATE
;
1312 /* accumulate error info */
1313 failed_dev
= qc
->dev
;
1314 all_err_mask
|= qc
->err_mask
;
1315 if (qc
->flags
& ATA_QCFLAG_IO
)
1319 /* enforce default EH actions */
1320 if (ap
->flags
& ATA_FLAG_FROZEN
||
1321 all_err_mask
& (AC_ERR_HSM
| AC_ERR_TIMEOUT
))
1322 action
|= ATA_EH_SOFTRESET
;
1323 else if (all_err_mask
)
1324 action
|= ATA_EH_REVALIDATE
;
1326 /* if we have offending qcs and the associated failed device */
1329 action
|= ata_eh_speed_down(failed_dev
, is_io
, all_err_mask
);
1331 /* perform per-dev EH action only on the offending device */
1332 ehc
->i
.dev_action
[failed_dev
->devno
] |=
1333 action
& ATA_EH_PERDEV_MASK
;
1334 action
&= ~ATA_EH_PERDEV_MASK
;
1337 /* record autopsy result */
1338 ehc
->i
.dev
= failed_dev
;
1339 ehc
->i
.action
= action
;
1345 * ata_eh_report - report error handling to user
1346 * @ap: ATA port EH is going on
1348 * Report EH to user.
1353 static void ata_eh_report(struct ata_port
*ap
)
1355 struct ata_eh_context
*ehc
= &ap
->eh_context
;
1356 const char *frozen
, *desc
;
1357 int tag
, nr_failed
= 0;
1360 if (ehc
->i
.desc
[0] != '\0')
1363 for (tag
= 0; tag
< ATA_MAX_QUEUE
; tag
++) {
1364 struct ata_queued_cmd
*qc
= __ata_qc_from_tag(ap
, tag
);
1366 if (!(qc
->flags
& ATA_QCFLAG_FAILED
))
1368 if (qc
->flags
& ATA_QCFLAG_SENSE_VALID
&& !qc
->err_mask
)
1374 if (!nr_failed
&& !ehc
->i
.err_mask
)
1378 if (ap
->flags
& ATA_FLAG_FROZEN
)
1382 ata_dev_printk(ehc
->i
.dev
, KERN_ERR
, "exception Emask 0x%x "
1383 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1384 ehc
->i
.err_mask
, ap
->sactive
, ehc
->i
.serror
,
1385 ehc
->i
.action
, frozen
);
1387 ata_dev_printk(ehc
->i
.dev
, KERN_ERR
, "(%s)\n", desc
);
1389 ata_port_printk(ap
, KERN_ERR
, "exception Emask 0x%x "
1390 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1391 ehc
->i
.err_mask
, ap
->sactive
, ehc
->i
.serror
,
1392 ehc
->i
.action
, frozen
);
1394 ata_port_printk(ap
, KERN_ERR
, "(%s)\n", desc
);
1397 for (tag
= 0; tag
< ATA_MAX_QUEUE
; tag
++) {
1398 struct ata_queued_cmd
*qc
= __ata_qc_from_tag(ap
, tag
);
1400 if (!(qc
->flags
& ATA_QCFLAG_FAILED
) || !qc
->err_mask
)
1403 ata_dev_printk(qc
->dev
, KERN_ERR
, "tag %d cmd 0x%x "
1404 "Emask 0x%x stat 0x%x err 0x%x (%s)\n",
1405 qc
->tag
, qc
->tf
.command
, qc
->err_mask
,
1406 qc
->result_tf
.command
, qc
->result_tf
.feature
,
1407 ata_err_string(qc
->err_mask
));
1411 static int ata_do_reset(struct ata_port
*ap
, ata_reset_fn_t reset
,
1412 unsigned int *classes
)
1416 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
1417 classes
[i
] = ATA_DEV_UNKNOWN
;
1419 rc
= reset(ap
, classes
);
1423 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
1424 * is complete and convert all ATA_DEV_UNKNOWN to
1427 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
1428 if (classes
[i
] != ATA_DEV_UNKNOWN
)
1431 if (i
< ATA_MAX_DEVICES
)
1432 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
1433 if (classes
[i
] == ATA_DEV_UNKNOWN
)
1434 classes
[i
] = ATA_DEV_NONE
;
1439 static int ata_eh_followup_srst_needed(int rc
, int classify
,
1440 const unsigned int *classes
)
1446 if (classify
&& classes
[0] == ATA_DEV_UNKNOWN
)
1451 static int ata_eh_reset(struct ata_port
*ap
, int classify
,
1452 ata_prereset_fn_t prereset
, ata_reset_fn_t softreset
,
1453 ata_reset_fn_t hardreset
, ata_postreset_fn_t postreset
)
1455 struct ata_eh_context
*ehc
= &ap
->eh_context
;
1456 unsigned int *classes
= ehc
->classes
;
1457 int tries
= ATA_EH_RESET_TRIES
;
1458 int verbose
= !(ap
->flags
& ATA_FLAG_LOADING
);
1459 unsigned int action
;
1460 ata_reset_fn_t reset
;
1461 int i
, did_followup_srst
, rc
;
1463 /* Determine which reset to use and record in ehc->i.action.
1464 * prereset() may examine and modify it.
1466 action
= ehc
->i
.action
;
1467 ehc
->i
.action
&= ~ATA_EH_RESET_MASK
;
1468 if (softreset
&& (!hardreset
|| (!sata_set_spd_needed(ap
) &&
1469 !(action
& ATA_EH_HARDRESET
))))
1470 ehc
->i
.action
|= ATA_EH_SOFTRESET
;
1472 ehc
->i
.action
|= ATA_EH_HARDRESET
;
1477 ata_port_printk(ap
, KERN_ERR
,
1478 "prereset failed (errno=%d)\n", rc
);
1483 /* prereset() might have modified ehc->i.action */
1484 if (ehc
->i
.action
& ATA_EH_HARDRESET
)
1486 else if (ehc
->i
.action
& ATA_EH_SOFTRESET
)
1489 /* prereset told us not to reset, bang classes and return */
1490 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
1491 classes
[i
] = ATA_DEV_NONE
;
1495 /* did prereset() screw up? if so, fix up to avoid oopsing */
1497 ata_port_printk(ap
, KERN_ERR
, "BUG: prereset() requested "
1498 "invalid reset type\n");
1506 /* shut up during boot probing */
1508 ata_port_printk(ap
, KERN_INFO
, "%s resetting port\n",
1509 reset
== softreset
? "soft" : "hard");
1512 ata_eh_about_to_do(ap
, NULL
, ATA_EH_RESET_MASK
);
1513 ehc
->i
.flags
|= ATA_EHI_DID_RESET
;
1515 rc
= ata_do_reset(ap
, reset
, classes
);
1517 did_followup_srst
= 0;
1518 if (reset
== hardreset
&&
1519 ata_eh_followup_srst_needed(rc
, classify
, classes
)) {
1520 /* okay, let's do follow-up softreset */
1521 did_followup_srst
= 1;
1525 ata_port_printk(ap
, KERN_ERR
,
1526 "follow-up softreset required "
1527 "but no softreset avaliable\n");
1531 ata_eh_about_to_do(ap
, NULL
, ATA_EH_RESET_MASK
);
1532 rc
= ata_do_reset(ap
, reset
, classes
);
1534 if (rc
== 0 && classify
&&
1535 classes
[0] == ATA_DEV_UNKNOWN
) {
1536 ata_port_printk(ap
, KERN_ERR
,
1537 "classification failed\n");
1542 if (rc
&& --tries
) {
1545 if (reset
== softreset
) {
1546 if (did_followup_srst
)
1547 type
= "follow-up soft";
1553 ata_port_printk(ap
, KERN_WARNING
,
1554 "%sreset failed, retrying in 5 secs\n", type
);
1557 if (reset
== hardreset
)
1558 sata_down_spd_limit(ap
);
1565 /* After the reset, the device state is PIO 0 and the
1566 * controller state is undefined. Record the mode.
1568 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
1569 ap
->device
[i
].pio_mode
= XFER_PIO_0
;
1572 postreset(ap
, classes
);
1574 /* reset successful, schedule revalidation */
1575 ata_eh_done(ap
, NULL
, ATA_EH_RESET_MASK
);
1576 ehc
->i
.action
|= ATA_EH_REVALIDATE
;
1582 static int ata_eh_revalidate_and_attach(struct ata_port
*ap
,
1583 struct ata_device
**r_failed_dev
)
1585 struct ata_eh_context
*ehc
= &ap
->eh_context
;
1586 struct ata_device
*dev
;
1587 unsigned long flags
;
1592 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
1593 unsigned int action
;
1595 dev
= &ap
->device
[i
];
1596 action
= ehc
->i
.action
| ehc
->i
.dev_action
[dev
->devno
];
1598 if (action
& ATA_EH_REVALIDATE
&& ata_dev_enabled(dev
)) {
1599 if (ata_port_offline(ap
)) {
1604 ata_eh_about_to_do(ap
, dev
, ATA_EH_REVALIDATE
);
1605 rc
= ata_dev_revalidate(dev
,
1606 ehc
->i
.flags
& ATA_EHI_DID_RESET
);
1610 ata_eh_done(ap
, dev
, ATA_EH_REVALIDATE
);
1612 /* schedule the scsi_rescan_device() here */
1613 queue_work(ata_aux_wq
, &(ap
->scsi_rescan_task
));
1614 } else if (dev
->class == ATA_DEV_UNKNOWN
&&
1615 ehc
->tries
[dev
->devno
] &&
1616 ata_class_enabled(ehc
->classes
[dev
->devno
])) {
1617 dev
->class = ehc
->classes
[dev
->devno
];
1619 rc
= ata_dev_read_id(dev
, &dev
->class, 1, dev
->id
);
1621 rc
= ata_dev_configure(dev
, 1);
1624 dev
->class = ATA_DEV_UNKNOWN
;
1628 spin_lock_irqsave(&ap
->host_set
->lock
, flags
);
1629 ap
->flags
|= ATA_FLAG_SCSI_HOTPLUG
;
1630 spin_unlock_irqrestore(&ap
->host_set
->lock
, flags
);
1635 *r_failed_dev
= dev
;
1641 static int ata_port_nr_enabled(struct ata_port
*ap
)
1645 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
1646 if (ata_dev_enabled(&ap
->device
[i
]))
1651 static int ata_port_nr_vacant(struct ata_port
*ap
)
1655 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
1656 if (ap
->device
[i
].class == ATA_DEV_UNKNOWN
)
1661 static int ata_eh_skip_recovery(struct ata_port
*ap
)
1663 struct ata_eh_context
*ehc
= &ap
->eh_context
;
1666 if (ap
->flags
& ATA_FLAG_FROZEN
|| ata_port_nr_enabled(ap
))
1669 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
1670 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
1671 struct ata_device
*dev
= &ap
->device
[i
];
1673 if (dev
->class == ATA_DEV_UNKNOWN
&&
1674 ehc
->classes
[dev
->devno
] != ATA_DEV_NONE
)
1682 * ata_eh_recover - recover host port after error
1683 * @ap: host port to recover
1684 * @prereset: prereset method (can be NULL)
1685 * @softreset: softreset method (can be NULL)
1686 * @hardreset: hardreset method (can be NULL)
1687 * @postreset: postreset method (can be NULL)
1689 * This is the alpha and omega, eum and yang, heart and soul of
1690 * libata exception handling. On entry, actions required to
1691 * recover the port and hotplug requests are recorded in
1692 * eh_context. This function executes all the operations with
1693 * appropriate retrials and fallbacks to resurrect failed
1694 * devices, detach goners and greet newcomers.
1697 * Kernel thread context (may sleep).
1700 * 0 on success, -errno on failure.
1702 static int ata_eh_recover(struct ata_port
*ap
, ata_prereset_fn_t prereset
,
1703 ata_reset_fn_t softreset
, ata_reset_fn_t hardreset
,
1704 ata_postreset_fn_t postreset
)
1706 struct ata_eh_context
*ehc
= &ap
->eh_context
;
1707 struct ata_device
*dev
;
1708 int down_xfermask
, i
, rc
;
1712 /* prep for recovery */
1713 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
1714 dev
= &ap
->device
[i
];
1716 ehc
->tries
[dev
->devno
] = ATA_EH_DEV_TRIES
;
1718 /* process hotplug request */
1719 if (dev
->flags
& ATA_DFLAG_DETACH
)
1720 ata_eh_detach_dev(dev
);
1722 if (!ata_dev_enabled(dev
) &&
1723 ((ehc
->i
.probe_mask
& (1 << dev
->devno
)) &&
1724 !(ehc
->did_probe_mask
& (1 << dev
->devno
)))) {
1725 ata_eh_detach_dev(dev
);
1727 ehc
->did_probe_mask
|= (1 << dev
->devno
);
1728 ehc
->i
.action
|= ATA_EH_SOFTRESET
;
1736 /* if UNLOADING, finish immediately */
1737 if (ap
->flags
& ATA_FLAG_UNLOADING
)
1740 /* skip EH if possible. */
1741 if (ata_eh_skip_recovery(ap
))
1744 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
1745 ehc
->classes
[i
] = ATA_DEV_UNKNOWN
;
1748 if (ehc
->i
.action
& ATA_EH_RESET_MASK
) {
1749 ata_eh_freeze_port(ap
);
1751 rc
= ata_eh_reset(ap
, ata_port_nr_vacant(ap
), prereset
,
1752 softreset
, hardreset
, postreset
);
1754 ata_port_printk(ap
, KERN_ERR
,
1755 "reset failed, giving up\n");
1759 ata_eh_thaw_port(ap
);
1762 /* revalidate existing devices and attach new ones */
1763 rc
= ata_eh_revalidate_and_attach(ap
, &dev
);
1767 /* configure transfer mode if the port has been reset */
1768 if (ehc
->i
.flags
& ATA_EHI_DID_RESET
) {
1769 rc
= ata_set_mode(ap
, &dev
);
1781 /* device missing, schedule probing */
1782 ehc
->i
.probe_mask
|= (1 << dev
->devno
);
1784 ehc
->tries
[dev
->devno
] = 0;
1787 sata_down_spd_limit(ap
);
1789 ehc
->tries
[dev
->devno
]--;
1790 if (down_xfermask
&&
1791 ata_down_xfermask_limit(dev
, ehc
->tries
[dev
->devno
] == 1))
1792 ehc
->tries
[dev
->devno
] = 0;
1795 if (ata_dev_enabled(dev
) && !ehc
->tries
[dev
->devno
]) {
1796 /* disable device if it has used up all its chances */
1797 ata_dev_disable(dev
);
1799 /* detach if offline */
1800 if (ata_port_offline(ap
))
1801 ata_eh_detach_dev(dev
);
1803 /* probe if requested */
1804 if ((ehc
->i
.probe_mask
& (1 << dev
->devno
)) &&
1805 !(ehc
->did_probe_mask
& (1 << dev
->devno
))) {
1806 ata_eh_detach_dev(dev
);
1809 ehc
->tries
[dev
->devno
] = ATA_EH_DEV_TRIES
;
1810 ehc
->did_probe_mask
|= (1 << dev
->devno
);
1811 ehc
->i
.action
|= ATA_EH_SOFTRESET
;
1814 /* soft didn't work? be haaaaard */
1815 if (ehc
->i
.flags
& ATA_EHI_DID_RESET
)
1816 ehc
->i
.action
|= ATA_EH_HARDRESET
;
1818 ehc
->i
.action
|= ATA_EH_SOFTRESET
;
1821 if (ata_port_nr_enabled(ap
)) {
1822 ata_port_printk(ap
, KERN_WARNING
, "failed to recover some "
1823 "devices, retrying in 5 secs\n");
1826 /* no device left, repeat fast */
1834 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++)
1835 ata_dev_disable(&ap
->device
[i
]);
1838 DPRINTK("EXIT, rc=%d\n", rc
);
1843 * ata_eh_finish - finish up EH
1844 * @ap: host port to finish EH for
1846 * Recovery is complete. Clean up EH states and retry or finish
1852 static void ata_eh_finish(struct ata_port
*ap
)
1856 /* retry or finish qcs */
1857 for (tag
= 0; tag
< ATA_MAX_QUEUE
; tag
++) {
1858 struct ata_queued_cmd
*qc
= __ata_qc_from_tag(ap
, tag
);
1860 if (!(qc
->flags
& ATA_QCFLAG_FAILED
))
1864 /* FIXME: Once EH migration is complete,
1865 * generate sense data in this function,
1866 * considering both err_mask and tf.
1868 if (qc
->err_mask
& AC_ERR_INVALID
)
1869 ata_eh_qc_complete(qc
);
1871 ata_eh_qc_retry(qc
);
1873 if (qc
->flags
& ATA_QCFLAG_SENSE_VALID
) {
1874 ata_eh_qc_complete(qc
);
1876 /* feed zero TF to sense generation */
1877 memset(&qc
->result_tf
, 0, sizeof(qc
->result_tf
));
1878 ata_eh_qc_retry(qc
);
1885 * ata_do_eh - do standard error handling
1886 * @ap: host port to handle error for
1887 * @prereset: prereset method (can be NULL)
1888 * @softreset: softreset method (can be NULL)
1889 * @hardreset: hardreset method (can be NULL)
1890 * @postreset: postreset method (can be NULL)
1892 * Perform standard error handling sequence.
1895 * Kernel thread context (may sleep).
1897 void ata_do_eh(struct ata_port
*ap
, ata_prereset_fn_t prereset
,
1898 ata_reset_fn_t softreset
, ata_reset_fn_t hardreset
,
1899 ata_postreset_fn_t postreset
)
1901 if (!(ap
->flags
& ATA_FLAG_LOADING
)) {
1906 ata_eh_recover(ap
, prereset
, softreset
, hardreset
, postreset
);