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>
43 #include <linux/libata.h>
47 static void __ata_port_freeze(struct ata_port *ap);
49 static void ata_ering_record(struct ata_ering *ering, int is_io,
50 unsigned int err_mask)
52 struct ata_ering_entry *ent;
57 ering->cursor %= ATA_ERING_SIZE;
59 ent = &ering->ring[ering->cursor];
61 ent->err_mask = err_mask;
62 ent->timestamp = get_jiffies_64();
65 static struct ata_ering_entry * ata_ering_top(struct ata_ering *ering)
67 struct ata_ering_entry *ent = &ering->ring[ering->cursor];
73 static int ata_ering_map(struct ata_ering *ering,
74 int (*map_fn)(struct ata_ering_entry *, void *),
78 struct ata_ering_entry *ent;
82 ent = &ering->ring[idx];
85 rc = map_fn(ent, arg);
88 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
89 } while (idx != ering->cursor);
95 * ata_scsi_timed_out - SCSI layer time out callback
96 * @cmd: timed out SCSI command
98 * Handles SCSI layer timeout. We race with normal completion of
99 * the qc for @cmd. If the qc is already gone, we lose and let
100 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
101 * timed out and EH should be invoked. Prevent ata_qc_complete()
102 * from finishing it by setting EH_SCHEDULED and return
105 * TODO: kill this function once old EH is gone.
108 * Called from timer context
111 * EH_HANDLED or EH_NOT_HANDLED
113 enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
115 struct Scsi_Host *host = cmd->device->host;
116 struct ata_port *ap = ata_shost_to_port(host);
118 struct ata_queued_cmd *qc;
119 enum scsi_eh_timer_return ret;
123 if (ap->ops->error_handler) {
124 ret = EH_NOT_HANDLED;
129 spin_lock_irqsave(&ap->host_set->lock, flags);
130 qc = ata_qc_from_tag(ap, ap->active_tag);
132 WARN_ON(qc->scsicmd != cmd);
133 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
134 qc->err_mask |= AC_ERR_TIMEOUT;
135 ret = EH_NOT_HANDLED;
137 spin_unlock_irqrestore(&ap->host_set->lock, flags);
140 DPRINTK("EXIT, ret=%d\n", ret);
145 * ata_scsi_error - SCSI layer error handler callback
146 * @host: SCSI host on which error occurred
148 * Handles SCSI-layer-thrown error events.
151 * Inherited from SCSI layer (none, can sleep)
156 void ata_scsi_error(struct Scsi_Host *host)
158 struct ata_port *ap = ata_shost_to_port(host);
159 spinlock_t *hs_lock = &ap->host_set->lock;
160 int i, repeat_cnt = ATA_EH_MAX_REPEAT;
165 /* synchronize with port task */
166 ata_port_flush_task(ap);
168 /* synchronize with host_set lock and sort out timeouts */
170 /* For new EH, all qcs are finished in one of three ways -
171 * normal completion, error completion, and SCSI timeout.
172 * Both cmpletions can race against SCSI timeout. When normal
173 * completion wins, the qc never reaches EH. When error
174 * completion wins, the qc has ATA_QCFLAG_FAILED set.
176 * When SCSI timeout wins, things are a bit more complex.
177 * Normal or error completion can occur after the timeout but
178 * before this point. In such cases, both types of
179 * completions are honored. A scmd is determined to have
180 * timed out iff its associated qc is active and not failed.
182 if (ap->ops->error_handler) {
183 struct scsi_cmnd *scmd, *tmp;
186 spin_lock_irqsave(hs_lock, flags);
188 list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
189 struct ata_queued_cmd *qc;
191 for (i = 0; i < ATA_MAX_QUEUE; i++) {
192 qc = __ata_qc_from_tag(ap, i);
193 if (qc->flags & ATA_QCFLAG_ACTIVE &&
198 if (i < ATA_MAX_QUEUE) {
199 /* the scmd has an associated qc */
200 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
201 /* which hasn't failed yet, timeout */
202 qc->err_mask |= AC_ERR_TIMEOUT;
203 qc->flags |= ATA_QCFLAG_FAILED;
207 /* Normal completion occurred after
208 * SCSI timeout but before this point.
209 * Successfully complete it.
211 scmd->retries = scmd->allowed;
212 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
216 /* If we have timed out qcs. They belong to EH from
217 * this point but the state of the controller is
218 * unknown. Freeze the port to make sure the IRQ
219 * handler doesn't diddle with those qcs. This must
220 * be done atomically w.r.t. setting QCFLAG_FAILED.
223 __ata_port_freeze(ap);
225 spin_unlock_irqrestore(hs_lock, flags);
227 spin_unlock_wait(hs_lock);
230 /* invoke error handler */
231 if (ap->ops->error_handler) {
232 /* fetch & clear EH info */
233 spin_lock_irqsave(hs_lock, flags);
235 memset(&ap->eh_context, 0, sizeof(ap->eh_context));
236 ap->eh_context.i = ap->eh_info;
237 memset(&ap->eh_info, 0, sizeof(ap->eh_info));
239 ap->flags &= ~ATA_FLAG_EH_PENDING;
241 spin_unlock_irqrestore(hs_lock, flags);
244 ap->ops->error_handler(ap);
246 /* Exception might have happend after ->error_handler
247 * recovered the port but before this point. Repeat
250 spin_lock_irqsave(hs_lock, flags);
252 if (ap->flags & ATA_FLAG_EH_PENDING) {
254 ata_port_printk(ap, KERN_INFO,
255 "EH pending after completion, "
256 "repeating EH (cnt=%d)\n", repeat_cnt);
257 spin_unlock_irqrestore(hs_lock, flags);
260 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
261 "tries, giving up\n", ATA_EH_MAX_REPEAT);
264 /* this run is complete, make sure EH info is clear */
265 memset(&ap->eh_info, 0, sizeof(ap->eh_info));
267 /* Clear host_eh_scheduled while holding hs_lock such
268 * that if exception occurs after this point but
269 * before EH completion, SCSI midlayer will
272 host->host_eh_scheduled = 0;
274 spin_unlock_irqrestore(hs_lock, flags);
276 WARN_ON(ata_qc_from_tag(ap, ap->active_tag) == NULL);
277 ap->ops->eng_timeout(ap);
280 /* finish or retry handled scmd's and clean up */
281 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
283 scsi_eh_flush_done_q(&ap->eh_done_q);
286 spin_lock_irqsave(hs_lock, flags);
288 if (ap->flags & ATA_FLAG_RECOVERED)
289 ata_port_printk(ap, KERN_INFO, "EH complete\n");
290 ap->flags &= ~ATA_FLAG_RECOVERED;
292 spin_unlock_irqrestore(hs_lock, flags);
298 * ata_qc_timeout - Handle timeout of queued command
299 * @qc: Command that timed out
301 * Some part of the kernel (currently, only the SCSI layer)
302 * has noticed that the active command on port @ap has not
303 * completed after a specified length of time. Handle this
304 * condition by disabling DMA (if necessary) and completing
305 * transactions, with error if necessary.
307 * This also handles the case of the "lost interrupt", where
308 * for some reason (possibly hardware bug, possibly driver bug)
309 * an interrupt was not delivered to the driver, even though the
310 * transaction completed successfully.
312 * TODO: kill this function once old EH is gone.
315 * Inherited from SCSI layer (none, can sleep)
317 static void ata_qc_timeout(struct ata_queued_cmd *qc)
319 struct ata_port *ap = qc->ap;
320 struct ata_host_set *host_set = ap->host_set;
321 u8 host_stat = 0, drv_stat;
326 ap->hsm_task_state = HSM_ST_IDLE;
328 spin_lock_irqsave(&host_set->lock, flags);
330 switch (qc->tf.protocol) {
333 case ATA_PROT_ATAPI_DMA:
334 host_stat = ap->ops->bmdma_status(ap);
336 /* before we do anything else, clear DMA-Start bit */
337 ap->ops->bmdma_stop(qc);
343 drv_stat = ata_chk_status(ap);
345 /* ack bmdma irq events */
346 ap->ops->irq_clear(ap);
348 ata_dev_printk(qc->dev, KERN_ERR, "command 0x%x timeout, "
349 "stat 0x%x host_stat 0x%x\n",
350 qc->tf.command, drv_stat, host_stat);
352 /* complete taskfile transaction */
353 qc->err_mask |= ac_err_mask(drv_stat);
357 spin_unlock_irqrestore(&host_set->lock, flags);
359 ata_eh_qc_complete(qc);
365 * ata_eng_timeout - Handle timeout of queued command
366 * @ap: Port on which timed-out command is active
368 * Some part of the kernel (currently, only the SCSI layer)
369 * has noticed that the active command on port @ap has not
370 * completed after a specified length of time. Handle this
371 * condition by disabling DMA (if necessary) and completing
372 * transactions, with error if necessary.
374 * This also handles the case of the "lost interrupt", where
375 * for some reason (possibly hardware bug, possibly driver bug)
376 * an interrupt was not delivered to the driver, even though the
377 * transaction completed successfully.
379 * TODO: kill this function once old EH is gone.
382 * Inherited from SCSI layer (none, can sleep)
384 void ata_eng_timeout(struct ata_port *ap)
388 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
394 * ata_qc_schedule_eh - schedule qc for error handling
395 * @qc: command to schedule error handling for
397 * Schedule error handling for @qc. EH will kick in as soon as
398 * other commands are drained.
401 * spin_lock_irqsave(host_set lock)
403 void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
405 struct ata_port *ap = qc->ap;
407 WARN_ON(!ap->ops->error_handler);
409 qc->flags |= ATA_QCFLAG_FAILED;
410 qc->ap->flags |= ATA_FLAG_EH_PENDING;
412 /* The following will fail if timeout has already expired.
413 * ata_scsi_error() takes care of such scmds on EH entry.
414 * Note that ATA_QCFLAG_FAILED is unconditionally set after
415 * this function completes.
417 scsi_req_abort_cmd(qc->scsicmd);
421 * ata_port_schedule_eh - schedule error handling without a qc
422 * @ap: ATA port to schedule EH for
424 * Schedule error handling for @ap. EH will kick in as soon as
425 * all commands are drained.
428 * spin_lock_irqsave(host_set lock)
430 void ata_port_schedule_eh(struct ata_port *ap)
432 WARN_ON(!ap->ops->error_handler);
434 ap->flags |= ATA_FLAG_EH_PENDING;
435 ata_schedule_scsi_eh(ap->host);
437 DPRINTK("port EH scheduled\n");
441 * ata_port_abort - abort all qc's on the port
442 * @ap: ATA port to abort qc's for
444 * Abort all active qc's of @ap and schedule EH.
447 * spin_lock_irqsave(host_set lock)
450 * Number of aborted qc's.
452 int ata_port_abort(struct ata_port *ap)
454 int tag, nr_aborted = 0;
456 WARN_ON(!ap->ops->error_handler);
458 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
459 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
462 qc->flags |= ATA_QCFLAG_FAILED;
469 ata_port_schedule_eh(ap);
475 * __ata_port_freeze - freeze port
476 * @ap: ATA port to freeze
478 * This function is called when HSM violation or some other
479 * condition disrupts normal operation of the port. Frozen port
480 * is not allowed to perform any operation until the port is
481 * thawed, which usually follows a successful reset.
483 * ap->ops->freeze() callback can be used for freezing the port
484 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
485 * port cannot be frozen hardware-wise, the interrupt handler
486 * must ack and clear interrupts unconditionally while the port
490 * spin_lock_irqsave(host_set lock)
492 static void __ata_port_freeze(struct ata_port *ap)
494 WARN_ON(!ap->ops->error_handler);
499 ap->flags |= ATA_FLAG_FROZEN;
501 DPRINTK("ata%u port frozen\n", ap->id);
505 * ata_port_freeze - abort & freeze port
506 * @ap: ATA port to freeze
508 * Abort and freeze @ap.
511 * spin_lock_irqsave(host_set lock)
514 * Number of aborted commands.
516 int ata_port_freeze(struct ata_port *ap)
520 WARN_ON(!ap->ops->error_handler);
522 nr_aborted = ata_port_abort(ap);
523 __ata_port_freeze(ap);
529 * ata_eh_freeze_port - EH helper to freeze port
530 * @ap: ATA port to freeze
537 void ata_eh_freeze_port(struct ata_port *ap)
541 if (!ap->ops->error_handler)
544 spin_lock_irqsave(&ap->host_set->lock, flags);
545 __ata_port_freeze(ap);
546 spin_unlock_irqrestore(&ap->host_set->lock, flags);
550 * ata_port_thaw_port - EH helper to thaw port
551 * @ap: ATA port to thaw
553 * Thaw frozen port @ap.
558 void ata_eh_thaw_port(struct ata_port *ap)
562 if (!ap->ops->error_handler)
565 spin_lock_irqsave(&ap->host_set->lock, flags);
567 ap->flags &= ~ATA_FLAG_FROZEN;
572 spin_unlock_irqrestore(&ap->host_set->lock, flags);
574 DPRINTK("ata%u port thawed\n", ap->id);
577 static void ata_eh_scsidone(struct scsi_cmnd *scmd)
582 static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
584 struct ata_port *ap = qc->ap;
585 struct scsi_cmnd *scmd = qc->scsicmd;
588 spin_lock_irqsave(&ap->host_set->lock, flags);
589 qc->scsidone = ata_eh_scsidone;
590 __ata_qc_complete(qc);
591 WARN_ON(ata_tag_valid(qc->tag));
592 spin_unlock_irqrestore(&ap->host_set->lock, flags);
594 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
598 * ata_eh_qc_complete - Complete an active ATA command from EH
599 * @qc: Command to complete
601 * Indicate to the mid and upper layers that an ATA command has
602 * completed. To be used from EH.
604 void ata_eh_qc_complete(struct ata_queued_cmd *qc)
606 struct scsi_cmnd *scmd = qc->scsicmd;
607 scmd->retries = scmd->allowed;
608 __ata_eh_qc_complete(qc);
612 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
613 * @qc: Command to retry
615 * Indicate to the mid and upper layers that an ATA command
616 * should be retried. To be used from EH.
618 * SCSI midlayer limits the number of retries to scmd->allowed.
619 * scmd->retries is decremented for commands which get retried
620 * due to unrelated failures (qc->err_mask is zero).
622 void ata_eh_qc_retry(struct ata_queued_cmd *qc)
624 struct scsi_cmnd *scmd = qc->scsicmd;
625 if (!qc->err_mask && scmd->retries)
627 __ata_eh_qc_complete(qc);
631 * ata_eh_about_to_do - about to perform eh_action
632 * @ap: target ATA port
633 * @action: action about to be performed
635 * Called just before performing EH actions to clear related bits
636 * in @ap->eh_info such that eh actions are not unnecessarily
642 static void ata_eh_about_to_do(struct ata_port *ap, unsigned int action)
646 spin_lock_irqsave(&ap->host_set->lock, flags);
647 ap->eh_info.action &= ~action;
648 ap->flags |= ATA_FLAG_RECOVERED;
649 spin_unlock_irqrestore(&ap->host_set->lock, flags);
653 * ata_err_string - convert err_mask to descriptive string
654 * @err_mask: error mask to convert to string
656 * Convert @err_mask to descriptive string. Errors are
657 * prioritized according to severity and only the most severe
664 * Descriptive string for @err_mask
666 static const char * ata_err_string(unsigned int err_mask)
668 if (err_mask & AC_ERR_HOST_BUS)
669 return "host bus error";
670 if (err_mask & AC_ERR_ATA_BUS)
671 return "ATA bus error";
672 if (err_mask & AC_ERR_TIMEOUT)
674 if (err_mask & AC_ERR_HSM)
675 return "HSM violation";
676 if (err_mask & AC_ERR_SYSTEM)
677 return "internal error";
678 if (err_mask & AC_ERR_MEDIA)
679 return "media error";
680 if (err_mask & AC_ERR_INVALID)
681 return "invalid argument";
682 if (err_mask & AC_ERR_DEV)
683 return "device error";
684 return "unknown error";
688 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
689 * @dev: device to perform REQUEST_SENSE to
690 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
692 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
693 * SENSE. This function is EH helper.
696 * Kernel thread context (may sleep).
699 * 0 on success, AC_ERR_* mask on failure
701 static unsigned int atapi_eh_request_sense(struct ata_device *dev,
702 unsigned char *sense_buf)
704 struct ata_port *ap = dev->ap;
705 struct ata_taskfile tf;
706 u8 cdb[ATAPI_CDB_LEN];
708 DPRINTK("ATAPI request sense\n");
710 ata_tf_init(dev, &tf);
712 /* FIXME: is this needed? */
713 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
715 /* XXX: why tf_read here? */
716 ap->ops->tf_read(ap, &tf);
718 /* fill these in, for the case where they are -not- overwritten */
720 sense_buf[2] = tf.feature >> 4;
722 memset(cdb, 0, ATAPI_CDB_LEN);
723 cdb[0] = REQUEST_SENSE;
724 cdb[4] = SCSI_SENSE_BUFFERSIZE;
726 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
727 tf.command = ATA_CMD_PACKET;
729 /* is it pointless to prefer PIO for "safety reasons"? */
730 if (ap->flags & ATA_FLAG_PIO_DMA) {
731 tf.protocol = ATA_PROT_ATAPI_DMA;
732 tf.feature |= ATAPI_PKT_DMA;
734 tf.protocol = ATA_PROT_ATAPI;
735 tf.lbam = (8 * 1024) & 0xff;
736 tf.lbah = (8 * 1024) >> 8;
739 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
740 sense_buf, SCSI_SENSE_BUFFERSIZE);
744 * ata_eh_analyze_serror - analyze SError for a failed port
745 * @ap: ATA port to analyze SError for
747 * Analyze SError if available and further determine cause of
753 static void ata_eh_analyze_serror(struct ata_port *ap)
755 struct ata_eh_context *ehc = &ap->eh_context;
756 u32 serror = ehc->i.serror;
757 unsigned int err_mask = 0, action = 0;
759 if (serror & SERR_PERSISTENT) {
760 err_mask |= AC_ERR_ATA_BUS;
761 action |= ATA_EH_HARDRESET;
764 (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
765 err_mask |= AC_ERR_ATA_BUS;
766 action |= ATA_EH_SOFTRESET;
768 if (serror & SERR_PROTOCOL) {
769 err_mask |= AC_ERR_HSM;
770 action |= ATA_EH_SOFTRESET;
772 if (serror & SERR_INTERNAL) {
773 err_mask |= AC_ERR_SYSTEM;
774 action |= ATA_EH_SOFTRESET;
776 if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG)) {
777 err_mask |= AC_ERR_ATA_BUS;
778 action |= ATA_EH_HARDRESET;
781 ehc->i.err_mask |= err_mask;
782 ehc->i.action |= action;
786 * ata_eh_analyze_tf - analyze taskfile of a failed qc
788 * @tf: Taskfile registers to analyze
790 * Analyze taskfile of @qc and further determine cause of
791 * failure. This function also requests ATAPI sense data if
795 * Kernel thread context (may sleep).
798 * Determined recovery action
800 static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
801 const struct ata_taskfile *tf)
803 unsigned int tmp, action = 0;
804 u8 stat = tf->command, err = tf->feature;
806 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
807 qc->err_mask |= AC_ERR_HSM;
808 return ATA_EH_SOFTRESET;
811 if (!(qc->err_mask & AC_ERR_DEV))
814 switch (qc->dev->class) {
817 qc->err_mask |= AC_ERR_ATA_BUS;
819 qc->err_mask |= AC_ERR_MEDIA;
821 qc->err_mask |= AC_ERR_INVALID;
825 tmp = atapi_eh_request_sense(qc->dev,
826 qc->scsicmd->sense_buffer);
828 /* ATA_QCFLAG_SENSE_VALID is used to tell
829 * atapi_qc_complete() that sense data is
832 * TODO: interpret sense data and set
833 * appropriate err_mask.
835 qc->flags |= ATA_QCFLAG_SENSE_VALID;
840 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
841 action |= ATA_EH_SOFTRESET;
846 static int ata_eh_categorize_ering_entry(struct ata_ering_entry *ent)
848 if (ent->err_mask & (AC_ERR_ATA_BUS | AC_ERR_TIMEOUT))
852 if (ent->err_mask & AC_ERR_HSM)
855 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
862 struct speed_down_needed_arg {
867 static int speed_down_needed_cb(struct ata_ering_entry *ent, void *void_arg)
869 struct speed_down_needed_arg *arg = void_arg;
871 if (ent->timestamp < arg->since)
874 arg->nr_errors[ata_eh_categorize_ering_entry(ent)]++;
879 * ata_eh_speed_down_needed - Determine wheter speed down is necessary
880 * @dev: Device of interest
882 * This function examines error ring of @dev and determines
883 * whether speed down is necessary. Speed down is necessary if
884 * there have been more than 3 of Cat-1 errors or 10 of Cat-2
885 * errors during last 15 minutes.
887 * Cat-1 errors are ATA_BUS, TIMEOUT for any command and HSM
888 * violation for known supported commands.
890 * Cat-2 errors are unclassified DEV error for known supported
894 * Inherited from caller.
897 * 1 if speed down is necessary, 0 otherwise
899 static int ata_eh_speed_down_needed(struct ata_device *dev)
901 const u64 interval = 15LLU * 60 * HZ;
902 static const int err_limits[3] = { -1, 3, 10 };
903 struct speed_down_needed_arg arg;
904 struct ata_ering_entry *ent;
908 ent = ata_ering_top(&dev->ering);
912 err_cat = ata_eh_categorize_ering_entry(ent);
916 memset(&arg, 0, sizeof(arg));
918 j64 = get_jiffies_64();
920 arg.since = j64 - interval;
924 ata_ering_map(&dev->ering, speed_down_needed_cb, &arg);
926 return arg.nr_errors[err_cat] > err_limits[err_cat];
930 * ata_eh_speed_down - record error and speed down if necessary
931 * @dev: Failed device
932 * @is_io: Did the device fail during normal IO?
933 * @err_mask: err_mask of the error
935 * Record error and examine error history to determine whether
936 * adjusting transmission speed is necessary. It also sets
937 * transmission limits appropriately if such adjustment is
941 * Kernel thread context (may sleep).
944 * 0 on success, -errno otherwise
946 static int ata_eh_speed_down(struct ata_device *dev, int is_io,
947 unsigned int err_mask)
952 /* record error and determine whether speed down is necessary */
953 ata_ering_record(&dev->ering, is_io, err_mask);
955 if (!ata_eh_speed_down_needed(dev))
958 /* speed down SATA link speed if possible */
959 if (sata_down_spd_limit(dev->ap) == 0)
960 return ATA_EH_HARDRESET;
962 /* lower transfer mode */
963 if (ata_down_xfermask_limit(dev, 0) == 0)
964 return ATA_EH_SOFTRESET;
966 ata_dev_printk(dev, KERN_ERR,
967 "speed down requested but no transfer mode left\n");
972 * ata_eh_autopsy - analyze error and determine recovery action
973 * @ap: ATA port to perform autopsy on
975 * Analyze why @ap failed and determine which recovery action is
976 * needed. This function also sets more detailed AC_ERR_* values
977 * and fills sense data for ATAPI CHECK SENSE.
980 * Kernel thread context (may sleep).
982 static void ata_eh_autopsy(struct ata_port *ap)
984 struct ata_eh_context *ehc = &ap->eh_context;
985 unsigned int action = ehc->i.action;
986 struct ata_device *failed_dev = NULL;
987 unsigned int all_err_mask = 0;
994 /* obtain and analyze SError */
995 rc = sata_scr_read(ap, SCR_ERROR, &serror);
997 ehc->i.serror |= serror;
998 ata_eh_analyze_serror(ap);
999 } else if (rc != -EOPNOTSUPP)
1000 action |= ATA_EH_HARDRESET;
1002 /* any real error trumps AC_ERR_OTHER */
1003 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1004 ehc->i.err_mask &= ~AC_ERR_OTHER;
1006 all_err_mask |= ehc->i.err_mask;
1008 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1009 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1011 if (!(qc->flags & ATA_QCFLAG_FAILED))
1014 /* inherit upper level err_mask */
1015 qc->err_mask |= ehc->i.err_mask;
1017 if (qc->err_mask & AC_ERR_TIMEOUT)
1018 action |= ATA_EH_SOFTRESET;
1021 action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1023 /* DEV errors are probably spurious in case of ATA_BUS error */
1024 if (qc->err_mask & AC_ERR_ATA_BUS)
1025 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1028 /* any real error trumps unknown error */
1029 if (qc->err_mask & ~AC_ERR_OTHER)
1030 qc->err_mask &= ~AC_ERR_OTHER;
1032 /* SENSE_VALID trumps dev/unknown error and revalidation */
1033 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1034 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1035 action &= ~ATA_EH_REVALIDATE;
1038 /* accumulate error info */
1039 failed_dev = qc->dev;
1040 all_err_mask |= qc->err_mask;
1041 if (qc->flags & ATA_QCFLAG_IO)
1045 /* speed down iff command was in progress */
1047 action |= ata_eh_speed_down(failed_dev, is_io, all_err_mask);
1050 action |= ATA_EH_REVALIDATE;
1052 ehc->i.dev = failed_dev;
1053 ehc->i.action = action;
1059 * ata_eh_report - report error handling to user
1060 * @ap: ATA port EH is going on
1062 * Report EH to user.
1067 static void ata_eh_report(struct ata_port *ap)
1069 struct ata_eh_context *ehc = &ap->eh_context;
1070 const char *frozen, *desc;
1071 int tag, nr_failed = 0;
1074 if (ehc->i.desc[0] != '\0')
1077 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1078 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1080 if (!(qc->flags & ATA_QCFLAG_FAILED))
1082 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1088 if (!nr_failed && !ehc->i.err_mask)
1092 if (ap->flags & ATA_FLAG_FROZEN)
1096 ata_dev_printk(ehc->i.dev, KERN_ERR,
1097 "exception Emask 0x%x SErr 0x%x action 0x%x%s\n",
1098 ehc->i.err_mask, ehc->i.serror, ehc->i.action,
1101 ata_dev_printk(ehc->i.dev, KERN_ERR, "(%s)\n", desc);
1103 ata_port_printk(ap, KERN_ERR,
1104 "exception Emask 0x%x SErr 0x%x action 0x%x%s\n",
1105 ehc->i.err_mask, ehc->i.serror, ehc->i.action,
1108 ata_port_printk(ap, KERN_ERR, "(%s)\n", desc);
1111 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1112 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1114 if (!(qc->flags & ATA_QCFLAG_FAILED) || !qc->err_mask)
1117 ata_dev_printk(qc->dev, KERN_ERR, "tag %d cmd 0x%x "
1118 "Emask 0x%x stat 0x%x err 0x%x (%s)\n",
1119 qc->tag, qc->tf.command, qc->err_mask,
1120 qc->result_tf.command, qc->result_tf.feature,
1121 ata_err_string(qc->err_mask));
1125 static int ata_eh_reset(struct ata_port *ap, ata_reset_fn_t softreset,
1126 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
1128 struct ata_eh_context *ehc = &ap->eh_context;
1129 unsigned int classes[ATA_MAX_DEVICES];
1130 int tries = ATA_EH_RESET_TRIES;
1131 ata_reset_fn_t reset;
1134 if (softreset && (!hardreset || (!sata_set_spd_needed(ap) &&
1135 !(ehc->i.action & ATA_EH_HARDRESET))))
1141 ata_port_printk(ap, KERN_INFO, "%s resetting port\n",
1142 reset == softreset ? "soft" : "hard");
1145 ata_eh_about_to_do(ap, ATA_EH_RESET_MASK);
1146 ehc->i.flags |= ATA_EHI_DID_RESET;
1148 rc = ata_do_reset(ap, reset, classes);
1150 if (rc && --tries) {
1151 ata_port_printk(ap, KERN_WARNING,
1152 "%sreset failed, retrying in 5 secs\n",
1153 reset == softreset ? "soft" : "hard");
1156 if (reset == hardreset)
1157 sata_down_spd_limit(ap);
1165 postreset(ap, classes);
1167 /* reset successful, schedule revalidation */
1169 ehc->i.action &= ~ATA_EH_RESET_MASK;
1170 ehc->i.action |= ATA_EH_REVALIDATE;
1176 static int ata_eh_revalidate(struct ata_port *ap,
1177 struct ata_device **r_failed_dev)
1179 struct ata_eh_context *ehc = &ap->eh_context;
1180 struct ata_device *dev;
1185 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1186 dev = &ap->device[i];
1188 if (ehc->i.action & ATA_EH_REVALIDATE && ata_dev_enabled(dev) &&
1189 (!ehc->i.dev || ehc->i.dev == dev)) {
1190 if (ata_port_offline(ap)) {
1195 ata_eh_about_to_do(ap, ATA_EH_REVALIDATE);
1196 rc = ata_dev_revalidate(dev,
1197 ehc->i.flags & ATA_EHI_DID_RESET);
1201 ehc->i.action &= ~ATA_EH_REVALIDATE;
1206 *r_failed_dev = dev;
1212 static int ata_port_nr_enabled(struct ata_port *ap)
1216 for (i = 0; i < ATA_MAX_DEVICES; i++)
1217 if (ata_dev_enabled(&ap->device[i]))
1223 * ata_eh_recover - recover host port after error
1224 * @ap: host port to recover
1225 * @softreset: softreset method (can be NULL)
1226 * @hardreset: hardreset method (can be NULL)
1227 * @postreset: postreset method (can be NULL)
1229 * This is the alpha and omega, eum and yang, heart and soul of
1230 * libata exception handling. On entry, actions required to
1231 * recover each devices are recorded in eh_context. This
1232 * function executes all the operations with appropriate retrials
1233 * and fallbacks to resurrect failed devices.
1236 * Kernel thread context (may sleep).
1239 * 0 on success, -errno on failure.
1241 static int ata_eh_recover(struct ata_port *ap, ata_reset_fn_t softreset,
1242 ata_reset_fn_t hardreset,
1243 ata_postreset_fn_t postreset)
1245 struct ata_eh_context *ehc = &ap->eh_context;
1246 struct ata_device *dev;
1247 int down_xfermask, i, rc;
1251 /* prep for recovery */
1252 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1253 dev = &ap->device[i];
1255 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
1262 /* skip EH if possible. */
1263 if (!ata_port_nr_enabled(ap) && !(ap->flags & ATA_FLAG_FROZEN))
1267 if (ehc->i.action & ATA_EH_RESET_MASK) {
1268 ata_eh_freeze_port(ap);
1270 rc = ata_eh_reset(ap, softreset, hardreset, postreset);
1272 ata_port_printk(ap, KERN_ERR,
1273 "reset failed, giving up\n");
1277 ata_eh_thaw_port(ap);
1280 /* revalidate existing devices */
1281 rc = ata_eh_revalidate(ap, &dev);
1285 /* configure transfer mode if the port has been reset */
1286 if (ehc->i.flags & ATA_EHI_DID_RESET) {
1287 rc = ata_set_mode(ap, &dev);
1300 ehc->tries[dev->devno] = 0;
1303 sata_down_spd_limit(ap);
1305 ehc->tries[dev->devno]--;
1306 if (down_xfermask &&
1307 ata_down_xfermask_limit(dev, ehc->tries[dev->devno] == 1))
1308 ehc->tries[dev->devno] = 0;
1311 /* disable device if it has used up all its chances */
1312 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno])
1313 ata_dev_disable(dev);
1315 /* soft didn't work? be haaaaard */
1316 if (ehc->i.flags & ATA_EHI_DID_RESET)
1317 ehc->i.action |= ATA_EH_HARDRESET;
1319 ehc->i.action |= ATA_EH_SOFTRESET;
1321 if (ata_port_nr_enabled(ap)) {
1322 ata_port_printk(ap, KERN_WARNING, "failed to recover some "
1323 "devices, retrying in 5 secs\n");
1326 /* no device left, repeat fast */
1334 for (i = 0; i < ATA_MAX_DEVICES; i++)
1335 ata_dev_disable(&ap->device[i]);
1338 DPRINTK("EXIT, rc=%d\n", rc);
1343 * ata_eh_finish - finish up EH
1344 * @ap: host port to finish EH for
1346 * Recovery is complete. Clean up EH states and retry or finish
1352 static void ata_eh_finish(struct ata_port *ap)
1356 /* retry or finish qcs */
1357 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1358 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1360 if (!(qc->flags & ATA_QCFLAG_FAILED))
1364 /* FIXME: Once EH migration is complete,
1365 * generate sense data in this function,
1366 * considering both err_mask and tf.
1368 if (qc->err_mask & AC_ERR_INVALID)
1369 ata_eh_qc_complete(qc);
1371 ata_eh_qc_retry(qc);
1373 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1374 ata_eh_qc_complete(qc);
1376 /* feed zero TF to sense generation */
1377 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
1378 ata_eh_qc_retry(qc);
1385 * ata_do_eh - do standard error handling
1386 * @ap: host port to handle error for
1387 * @softreset: softreset method (can be NULL)
1388 * @hardreset: hardreset method (can be NULL)
1389 * @postreset: postreset method (can be NULL)
1391 * Perform standard error handling sequence.
1394 * Kernel thread context (may sleep).
1396 void ata_do_eh(struct ata_port *ap, ata_reset_fn_t softreset,
1397 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
1401 ata_eh_recover(ap, softreset, hardreset, postreset);