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 static void __ata_port_freeze(struct ata_port *ap);
54 static void ata_eh_finish(struct ata_port *ap);
56 static void ata_eh_handle_port_suspend(struct ata_port *ap);
57 static void ata_eh_handle_port_resume(struct ata_port *ap);
58 static int ata_eh_suspend(struct ata_port *ap,
59 struct ata_device **r_failed_dev);
60 static void ata_eh_prep_resume(struct ata_port *ap);
61 static int ata_eh_resume(struct ata_port *ap, struct ata_device **r_failed_dev);
63 static void ata_eh_handle_port_suspend(struct ata_port *ap)
66 static void ata_eh_handle_port_resume(struct ata_port *ap)
69 static int ata_eh_suspend(struct ata_port *ap, struct ata_device **r_failed_dev)
74 static void ata_eh_prep_resume(struct ata_port *ap)
77 static int ata_eh_resume(struct ata_port *ap, struct ata_device **r_failed_dev)
81 #endif /* CONFIG_PM */
83 static void ata_ering_record(struct ata_ering *ering, int is_io,
84 unsigned int err_mask)
86 struct ata_ering_entry *ent;
91 ering->cursor %= ATA_ERING_SIZE;
93 ent = &ering->ring[ering->cursor];
95 ent->err_mask = err_mask;
96 ent->timestamp = get_jiffies_64();
99 static void ata_ering_clear(struct ata_ering *ering)
101 memset(ering, 0, sizeof(*ering));
104 static int ata_ering_map(struct ata_ering *ering,
105 int (*map_fn)(struct ata_ering_entry *, void *),
109 struct ata_ering_entry *ent;
113 ent = &ering->ring[idx];
116 rc = map_fn(ent, arg);
119 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
120 } while (idx != ering->cursor);
125 static unsigned int ata_eh_dev_action(struct ata_device *dev)
127 struct ata_eh_context *ehc = &dev->ap->eh_context;
129 return ehc->i.action | ehc->i.dev_action[dev->devno];
132 static void ata_eh_clear_action(struct ata_device *dev,
133 struct ata_eh_info *ehi, unsigned int action)
138 ehi->action &= ~action;
139 for (i = 0; i < ATA_MAX_DEVICES; i++)
140 ehi->dev_action[i] &= ~action;
142 /* doesn't make sense for port-wide EH actions */
143 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
145 /* break ehi->action into ehi->dev_action */
146 if (ehi->action & action) {
147 for (i = 0; i < ATA_MAX_DEVICES; i++)
148 ehi->dev_action[i] |= ehi->action & action;
149 ehi->action &= ~action;
152 /* turn off the specified per-dev action */
153 ehi->dev_action[dev->devno] &= ~action;
158 * ata_scsi_timed_out - SCSI layer time out callback
159 * @cmd: timed out SCSI command
161 * Handles SCSI layer timeout. We race with normal completion of
162 * the qc for @cmd. If the qc is already gone, we lose and let
163 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
164 * timed out and EH should be invoked. Prevent ata_qc_complete()
165 * from finishing it by setting EH_SCHEDULED and return
168 * TODO: kill this function once old EH is gone.
171 * Called from timer context
174 * EH_HANDLED or EH_NOT_HANDLED
176 enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
178 struct Scsi_Host *host = cmd->device->host;
179 struct ata_port *ap = ata_shost_to_port(host);
181 struct ata_queued_cmd *qc;
182 enum scsi_eh_timer_return ret;
186 if (ap->ops->error_handler) {
187 ret = EH_NOT_HANDLED;
192 spin_lock_irqsave(ap->lock, flags);
193 qc = ata_qc_from_tag(ap, ap->active_tag);
195 WARN_ON(qc->scsicmd != cmd);
196 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
197 qc->err_mask |= AC_ERR_TIMEOUT;
198 ret = EH_NOT_HANDLED;
200 spin_unlock_irqrestore(ap->lock, flags);
203 DPRINTK("EXIT, ret=%d\n", ret);
208 * ata_scsi_error - SCSI layer error handler callback
209 * @host: SCSI host on which error occurred
211 * Handles SCSI-layer-thrown error events.
214 * Inherited from SCSI layer (none, can sleep)
219 void ata_scsi_error(struct Scsi_Host *host)
221 struct ata_port *ap = ata_shost_to_port(host);
222 int i, repeat_cnt = ATA_EH_MAX_REPEAT;
227 /* synchronize with port task */
228 ata_port_flush_task(ap);
230 /* synchronize with host lock and sort out timeouts */
232 /* For new EH, all qcs are finished in one of three ways -
233 * normal completion, error completion, and SCSI timeout.
234 * Both cmpletions can race against SCSI timeout. When normal
235 * completion wins, the qc never reaches EH. When error
236 * completion wins, the qc has ATA_QCFLAG_FAILED set.
238 * When SCSI timeout wins, things are a bit more complex.
239 * Normal or error completion can occur after the timeout but
240 * before this point. In such cases, both types of
241 * completions are honored. A scmd is determined to have
242 * timed out iff its associated qc is active and not failed.
244 if (ap->ops->error_handler) {
245 struct scsi_cmnd *scmd, *tmp;
248 spin_lock_irqsave(ap->lock, flags);
250 list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
251 struct ata_queued_cmd *qc;
253 for (i = 0; i < ATA_MAX_QUEUE; i++) {
254 qc = __ata_qc_from_tag(ap, i);
255 if (qc->flags & ATA_QCFLAG_ACTIVE &&
260 if (i < ATA_MAX_QUEUE) {
261 /* the scmd has an associated qc */
262 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
263 /* which hasn't failed yet, timeout */
264 qc->err_mask |= AC_ERR_TIMEOUT;
265 qc->flags |= ATA_QCFLAG_FAILED;
269 /* Normal completion occurred after
270 * SCSI timeout but before this point.
271 * Successfully complete it.
273 scmd->retries = scmd->allowed;
274 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
278 /* If we have timed out qcs. They belong to EH from
279 * this point but the state of the controller is
280 * unknown. Freeze the port to make sure the IRQ
281 * handler doesn't diddle with those qcs. This must
282 * be done atomically w.r.t. setting QCFLAG_FAILED.
285 __ata_port_freeze(ap);
287 spin_unlock_irqrestore(ap->lock, flags);
289 spin_unlock_wait(ap->lock);
292 /* invoke error handler */
293 if (ap->ops->error_handler) {
294 /* process port resume request */
295 ata_eh_handle_port_resume(ap);
297 /* fetch & clear EH info */
298 spin_lock_irqsave(ap->lock, flags);
300 memset(&ap->eh_context, 0, sizeof(ap->eh_context));
301 ap->eh_context.i = ap->eh_info;
302 memset(&ap->eh_info, 0, sizeof(ap->eh_info));
304 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
305 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
307 spin_unlock_irqrestore(ap->lock, flags);
309 /* invoke EH, skip if unloading or suspended */
310 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
311 ap->ops->error_handler(ap);
315 /* process port suspend request */
316 ata_eh_handle_port_suspend(ap);
318 /* Exception might have happend after ->error_handler
319 * recovered the port but before this point. Repeat
322 spin_lock_irqsave(ap->lock, flags);
324 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
326 ata_port_printk(ap, KERN_INFO,
327 "EH pending after completion, "
328 "repeating EH (cnt=%d)\n", repeat_cnt);
329 spin_unlock_irqrestore(ap->lock, flags);
332 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
333 "tries, giving up\n", ATA_EH_MAX_REPEAT);
336 /* this run is complete, make sure EH info is clear */
337 memset(&ap->eh_info, 0, sizeof(ap->eh_info));
339 /* Clear host_eh_scheduled while holding ap->lock such
340 * that if exception occurs after this point but
341 * before EH completion, SCSI midlayer will
344 host->host_eh_scheduled = 0;
346 spin_unlock_irqrestore(ap->lock, flags);
348 WARN_ON(ata_qc_from_tag(ap, ap->active_tag) == NULL);
349 ap->ops->eng_timeout(ap);
352 /* finish or retry handled scmd's and clean up */
353 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
355 scsi_eh_flush_done_q(&ap->eh_done_q);
358 spin_lock_irqsave(ap->lock, flags);
360 if (ap->pflags & ATA_PFLAG_LOADING)
361 ap->pflags &= ~ATA_PFLAG_LOADING;
362 else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
363 queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0);
365 if (ap->pflags & ATA_PFLAG_RECOVERED)
366 ata_port_printk(ap, KERN_INFO, "EH complete\n");
368 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
370 /* tell wait_eh that we're done */
371 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
372 wake_up_all(&ap->eh_wait_q);
374 spin_unlock_irqrestore(ap->lock, flags);
380 * ata_port_wait_eh - Wait for the currently pending EH to complete
381 * @ap: Port to wait EH for
383 * Wait until the currently pending EH is complete.
386 * Kernel thread context (may sleep).
388 void ata_port_wait_eh(struct ata_port *ap)
394 spin_lock_irqsave(ap->lock, flags);
396 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
397 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
398 spin_unlock_irqrestore(ap->lock, flags);
400 spin_lock_irqsave(ap->lock, flags);
402 finish_wait(&ap->eh_wait_q, &wait);
404 spin_unlock_irqrestore(ap->lock, flags);
406 /* make sure SCSI EH is complete */
407 if (scsi_host_in_recovery(ap->scsi_host)) {
414 * ata_qc_timeout - Handle timeout of queued command
415 * @qc: Command that timed out
417 * Some part of the kernel (currently, only the SCSI layer)
418 * has noticed that the active command on port @ap has not
419 * completed after a specified length of time. Handle this
420 * condition by disabling DMA (if necessary) and completing
421 * transactions, with error if necessary.
423 * This also handles the case of the "lost interrupt", where
424 * for some reason (possibly hardware bug, possibly driver bug)
425 * an interrupt was not delivered to the driver, even though the
426 * transaction completed successfully.
428 * TODO: kill this function once old EH is gone.
431 * Inherited from SCSI layer (none, can sleep)
433 static void ata_qc_timeout(struct ata_queued_cmd *qc)
435 struct ata_port *ap = qc->ap;
436 u8 host_stat = 0, drv_stat;
441 ap->hsm_task_state = HSM_ST_IDLE;
443 spin_lock_irqsave(ap->lock, flags);
445 switch (qc->tf.protocol) {
448 case ATA_PROT_ATAPI_DMA:
449 host_stat = ap->ops->bmdma_status(ap);
451 /* before we do anything else, clear DMA-Start bit */
452 ap->ops->bmdma_stop(qc);
458 drv_stat = ata_chk_status(ap);
460 /* ack bmdma irq events */
461 ap->ops->irq_clear(ap);
463 ata_dev_printk(qc->dev, KERN_ERR, "command 0x%x timeout, "
464 "stat 0x%x host_stat 0x%x\n",
465 qc->tf.command, drv_stat, host_stat);
467 /* complete taskfile transaction */
468 qc->err_mask |= AC_ERR_TIMEOUT;
472 spin_unlock_irqrestore(ap->lock, flags);
474 ata_eh_qc_complete(qc);
480 * ata_eng_timeout - Handle timeout of queued command
481 * @ap: Port on which timed-out command is active
483 * Some part of the kernel (currently, only the SCSI layer)
484 * has noticed that the active command on port @ap has not
485 * completed after a specified length of time. Handle this
486 * condition by disabling DMA (if necessary) and completing
487 * transactions, with error if necessary.
489 * This also handles the case of the "lost interrupt", where
490 * for some reason (possibly hardware bug, possibly driver bug)
491 * an interrupt was not delivered to the driver, even though the
492 * transaction completed successfully.
494 * TODO: kill this function once old EH is gone.
497 * Inherited from SCSI layer (none, can sleep)
499 void ata_eng_timeout(struct ata_port *ap)
503 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
509 * ata_qc_schedule_eh - schedule qc for error handling
510 * @qc: command to schedule error handling for
512 * Schedule error handling for @qc. EH will kick in as soon as
513 * other commands are drained.
516 * spin_lock_irqsave(host lock)
518 void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
520 struct ata_port *ap = qc->ap;
522 WARN_ON(!ap->ops->error_handler);
524 qc->flags |= ATA_QCFLAG_FAILED;
525 qc->ap->pflags |= ATA_PFLAG_EH_PENDING;
527 /* The following will fail if timeout has already expired.
528 * ata_scsi_error() takes care of such scmds on EH entry.
529 * Note that ATA_QCFLAG_FAILED is unconditionally set after
530 * this function completes.
532 scsi_req_abort_cmd(qc->scsicmd);
536 * ata_port_schedule_eh - schedule error handling without a qc
537 * @ap: ATA port to schedule EH for
539 * Schedule error handling for @ap. EH will kick in as soon as
540 * all commands are drained.
543 * spin_lock_irqsave(host lock)
545 void ata_port_schedule_eh(struct ata_port *ap)
547 WARN_ON(!ap->ops->error_handler);
549 ap->pflags |= ATA_PFLAG_EH_PENDING;
550 scsi_schedule_eh(ap->scsi_host);
552 DPRINTK("port EH scheduled\n");
556 * ata_port_abort - abort all qc's on the port
557 * @ap: ATA port to abort qc's for
559 * Abort all active qc's of @ap and schedule EH.
562 * spin_lock_irqsave(host lock)
565 * Number of aborted qc's.
567 int ata_port_abort(struct ata_port *ap)
569 int tag, nr_aborted = 0;
571 WARN_ON(!ap->ops->error_handler);
573 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
574 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
577 qc->flags |= ATA_QCFLAG_FAILED;
584 ata_port_schedule_eh(ap);
590 * __ata_port_freeze - freeze port
591 * @ap: ATA port to freeze
593 * This function is called when HSM violation or some other
594 * condition disrupts normal operation of the port. Frozen port
595 * is not allowed to perform any operation until the port is
596 * thawed, which usually follows a successful reset.
598 * ap->ops->freeze() callback can be used for freezing the port
599 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
600 * port cannot be frozen hardware-wise, the interrupt handler
601 * must ack and clear interrupts unconditionally while the port
605 * spin_lock_irqsave(host lock)
607 static void __ata_port_freeze(struct ata_port *ap)
609 WARN_ON(!ap->ops->error_handler);
614 ap->pflags |= ATA_PFLAG_FROZEN;
616 DPRINTK("ata%u port frozen\n", ap->print_id);
620 * ata_port_freeze - abort & freeze port
621 * @ap: ATA port to freeze
623 * Abort and freeze @ap.
626 * spin_lock_irqsave(host lock)
629 * Number of aborted commands.
631 int ata_port_freeze(struct ata_port *ap)
635 WARN_ON(!ap->ops->error_handler);
637 nr_aborted = ata_port_abort(ap);
638 __ata_port_freeze(ap);
644 * ata_eh_freeze_port - EH helper to freeze port
645 * @ap: ATA port to freeze
652 void ata_eh_freeze_port(struct ata_port *ap)
656 if (!ap->ops->error_handler)
659 spin_lock_irqsave(ap->lock, flags);
660 __ata_port_freeze(ap);
661 spin_unlock_irqrestore(ap->lock, flags);
665 * ata_port_thaw_port - EH helper to thaw port
666 * @ap: ATA port to thaw
668 * Thaw frozen port @ap.
673 void ata_eh_thaw_port(struct ata_port *ap)
677 if (!ap->ops->error_handler)
680 spin_lock_irqsave(ap->lock, flags);
682 ap->pflags &= ~ATA_PFLAG_FROZEN;
687 spin_unlock_irqrestore(ap->lock, flags);
689 DPRINTK("ata%u port thawed\n", ap->print_id);
692 static void ata_eh_scsidone(struct scsi_cmnd *scmd)
697 static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
699 struct ata_port *ap = qc->ap;
700 struct scsi_cmnd *scmd = qc->scsicmd;
703 spin_lock_irqsave(ap->lock, flags);
704 qc->scsidone = ata_eh_scsidone;
705 __ata_qc_complete(qc);
706 WARN_ON(ata_tag_valid(qc->tag));
707 spin_unlock_irqrestore(ap->lock, flags);
709 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
713 * ata_eh_qc_complete - Complete an active ATA command from EH
714 * @qc: Command to complete
716 * Indicate to the mid and upper layers that an ATA command has
717 * completed. To be used from EH.
719 void ata_eh_qc_complete(struct ata_queued_cmd *qc)
721 struct scsi_cmnd *scmd = qc->scsicmd;
722 scmd->retries = scmd->allowed;
723 __ata_eh_qc_complete(qc);
727 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
728 * @qc: Command to retry
730 * Indicate to the mid and upper layers that an ATA command
731 * should be retried. To be used from EH.
733 * SCSI midlayer limits the number of retries to scmd->allowed.
734 * scmd->retries is decremented for commands which get retried
735 * due to unrelated failures (qc->err_mask is zero).
737 void ata_eh_qc_retry(struct ata_queued_cmd *qc)
739 struct scsi_cmnd *scmd = qc->scsicmd;
740 if (!qc->err_mask && scmd->retries)
742 __ata_eh_qc_complete(qc);
746 * ata_eh_detach_dev - detach ATA device
747 * @dev: ATA device to detach
754 static void ata_eh_detach_dev(struct ata_device *dev)
756 struct ata_port *ap = dev->ap;
759 ata_dev_disable(dev);
761 spin_lock_irqsave(ap->lock, flags);
763 dev->flags &= ~ATA_DFLAG_DETACH;
765 if (ata_scsi_offline_dev(dev)) {
766 dev->flags |= ATA_DFLAG_DETACHED;
767 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
770 /* clear per-dev EH actions */
771 ata_eh_clear_action(dev, &ap->eh_info, ATA_EH_PERDEV_MASK);
772 ata_eh_clear_action(dev, &ap->eh_context.i, ATA_EH_PERDEV_MASK);
774 spin_unlock_irqrestore(ap->lock, flags);
778 * ata_eh_about_to_do - about to perform eh_action
779 * @ap: target ATA port
780 * @dev: target ATA dev for per-dev action (can be NULL)
781 * @action: action about to be performed
783 * Called just before performing EH actions to clear related bits
784 * in @ap->eh_info such that eh actions are not unnecessarily
790 static void ata_eh_about_to_do(struct ata_port *ap, struct ata_device *dev,
794 struct ata_eh_info *ehi = &ap->eh_info;
795 struct ata_eh_context *ehc = &ap->eh_context;
797 spin_lock_irqsave(ap->lock, flags);
799 /* Reset is represented by combination of actions and EHI
800 * flags. Suck in all related bits before clearing eh_info to
801 * avoid losing requested action.
803 if (action & ATA_EH_RESET_MASK) {
804 ehc->i.action |= ehi->action & ATA_EH_RESET_MASK;
805 ehc->i.flags |= ehi->flags & ATA_EHI_RESET_MODIFIER_MASK;
807 /* make sure all reset actions are cleared & clear EHI flags */
808 action |= ATA_EH_RESET_MASK;
809 ehi->flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
812 ata_eh_clear_action(dev, ehi, action);
814 if (!(ehc->i.flags & ATA_EHI_QUIET))
815 ap->pflags |= ATA_PFLAG_RECOVERED;
817 spin_unlock_irqrestore(ap->lock, flags);
821 * ata_eh_done - EH action complete
822 * @ap: target ATA port
823 * @dev: target ATA dev for per-dev action (can be NULL)
824 * @action: action just completed
826 * Called right after performing EH actions to clear related bits
827 * in @ap->eh_context.
832 static void ata_eh_done(struct ata_port *ap, struct ata_device *dev,
835 /* if reset is complete, clear all reset actions & reset modifier */
836 if (action & ATA_EH_RESET_MASK) {
837 action |= ATA_EH_RESET_MASK;
838 ap->eh_context.i.flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
841 ata_eh_clear_action(dev, &ap->eh_context.i, action);
845 * ata_err_string - convert err_mask to descriptive string
846 * @err_mask: error mask to convert to string
848 * Convert @err_mask to descriptive string. Errors are
849 * prioritized according to severity and only the most severe
856 * Descriptive string for @err_mask
858 static const char * ata_err_string(unsigned int err_mask)
860 if (err_mask & AC_ERR_HOST_BUS)
861 return "host bus error";
862 if (err_mask & AC_ERR_ATA_BUS)
863 return "ATA bus error";
864 if (err_mask & AC_ERR_TIMEOUT)
866 if (err_mask & AC_ERR_HSM)
867 return "HSM violation";
868 if (err_mask & AC_ERR_SYSTEM)
869 return "internal error";
870 if (err_mask & AC_ERR_MEDIA)
871 return "media error";
872 if (err_mask & AC_ERR_INVALID)
873 return "invalid argument";
874 if (err_mask & AC_ERR_DEV)
875 return "device error";
876 return "unknown error";
880 * ata_read_log_page - read a specific log page
881 * @dev: target device
882 * @page: page to read
883 * @buf: buffer to store read page
884 * @sectors: number of sectors to read
886 * Read log page using READ_LOG_EXT command.
889 * Kernel thread context (may sleep).
892 * 0 on success, AC_ERR_* mask otherwise.
894 static unsigned int ata_read_log_page(struct ata_device *dev,
895 u8 page, void *buf, unsigned int sectors)
897 struct ata_taskfile tf;
898 unsigned int err_mask;
900 DPRINTK("read log page - page %d\n", page);
902 ata_tf_init(dev, &tf);
903 tf.command = ATA_CMD_READ_LOG_EXT;
906 tf.hob_nsect = sectors >> 8;
907 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
908 tf.protocol = ATA_PROT_PIO;
910 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
911 buf, sectors * ATA_SECT_SIZE);
913 DPRINTK("EXIT, err_mask=%x\n", err_mask);
918 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
919 * @dev: Device to read log page 10h from
920 * @tag: Resulting tag of the failed command
921 * @tf: Resulting taskfile registers of the failed command
923 * Read log page 10h to obtain NCQ error details and clear error
927 * Kernel thread context (may sleep).
930 * 0 on success, -errno otherwise.
932 static int ata_eh_read_log_10h(struct ata_device *dev,
933 int *tag, struct ata_taskfile *tf)
935 u8 *buf = dev->ap->sector_buf;
936 unsigned int err_mask;
940 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
945 for (i = 0; i < ATA_SECT_SIZE; i++)
948 ata_dev_printk(dev, KERN_WARNING,
949 "invalid checksum 0x%x on log page 10h\n", csum);
954 *tag = buf[0] & 0x1f;
956 tf->command = buf[2];
957 tf->feature = buf[3];
962 tf->hob_lbal = buf[8];
963 tf->hob_lbam = buf[9];
964 tf->hob_lbah = buf[10];
966 tf->hob_nsect = buf[13];
972 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
973 * @dev: device to perform REQUEST_SENSE to
974 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
976 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
977 * SENSE. This function is EH helper.
980 * Kernel thread context (may sleep).
983 * 0 on success, AC_ERR_* mask on failure
985 static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc)
987 struct ata_device *dev = qc->dev;
988 unsigned char *sense_buf = qc->scsicmd->sense_buffer;
989 struct ata_port *ap = dev->ap;
990 struct ata_taskfile tf;
991 u8 cdb[ATAPI_CDB_LEN];
993 DPRINTK("ATAPI request sense\n");
995 /* FIXME: is this needed? */
996 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
998 /* initialize sense_buf with the error register,
999 * for the case where they are -not- overwritten
1001 sense_buf[0] = 0x70;
1002 sense_buf[2] = qc->result_tf.feature >> 4;
1004 /* some devices time out if garbage left in tf */
1005 ata_tf_init(dev, &tf);
1007 memset(cdb, 0, ATAPI_CDB_LEN);
1008 cdb[0] = REQUEST_SENSE;
1009 cdb[4] = SCSI_SENSE_BUFFERSIZE;
1011 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1012 tf.command = ATA_CMD_PACKET;
1014 /* is it pointless to prefer PIO for "safety reasons"? */
1015 if (ap->flags & ATA_FLAG_PIO_DMA) {
1016 tf.protocol = ATA_PROT_ATAPI_DMA;
1017 tf.feature |= ATAPI_PKT_DMA;
1019 tf.protocol = ATA_PROT_ATAPI;
1020 tf.lbam = (8 * 1024) & 0xff;
1021 tf.lbah = (8 * 1024) >> 8;
1024 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
1025 sense_buf, SCSI_SENSE_BUFFERSIZE);
1029 * ata_eh_analyze_serror - analyze SError for a failed port
1030 * @ap: ATA port to analyze SError for
1032 * Analyze SError if available and further determine cause of
1038 static void ata_eh_analyze_serror(struct ata_port *ap)
1040 struct ata_eh_context *ehc = &ap->eh_context;
1041 u32 serror = ehc->i.serror;
1042 unsigned int err_mask = 0, action = 0;
1044 if (serror & SERR_PERSISTENT) {
1045 err_mask |= AC_ERR_ATA_BUS;
1046 action |= ATA_EH_HARDRESET;
1049 (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
1050 err_mask |= AC_ERR_ATA_BUS;
1051 action |= ATA_EH_SOFTRESET;
1053 if (serror & SERR_PROTOCOL) {
1054 err_mask |= AC_ERR_HSM;
1055 action |= ATA_EH_SOFTRESET;
1057 if (serror & SERR_INTERNAL) {
1058 err_mask |= AC_ERR_SYSTEM;
1059 action |= ATA_EH_HARDRESET;
1061 if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG))
1062 ata_ehi_hotplugged(&ehc->i);
1064 ehc->i.err_mask |= err_mask;
1065 ehc->i.action |= action;
1069 * ata_eh_analyze_ncq_error - analyze NCQ error
1070 * @ap: ATA port to analyze NCQ error for
1072 * Read log page 10h, determine the offending qc and acquire
1073 * error status TF. For NCQ device errors, all LLDDs have to do
1074 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1078 * Kernel thread context (may sleep).
1080 static void ata_eh_analyze_ncq_error(struct ata_port *ap)
1082 struct ata_eh_context *ehc = &ap->eh_context;
1083 struct ata_device *dev = ap->device;
1084 struct ata_queued_cmd *qc;
1085 struct ata_taskfile tf;
1088 /* if frozen, we can't do much */
1089 if (ap->pflags & ATA_PFLAG_FROZEN)
1092 /* is it NCQ device error? */
1093 if (!ap->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1096 /* has LLDD analyzed already? */
1097 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1098 qc = __ata_qc_from_tag(ap, tag);
1100 if (!(qc->flags & ATA_QCFLAG_FAILED))
1107 /* okay, this error is ours */
1108 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1110 ata_port_printk(ap, KERN_ERR, "failed to read log page 10h "
1111 "(errno=%d)\n", rc);
1115 if (!(ap->sactive & (1 << tag))) {
1116 ata_port_printk(ap, KERN_ERR, "log page 10h reported "
1117 "inactive tag %d\n", tag);
1121 /* we've got the perpetrator, condemn it */
1122 qc = __ata_qc_from_tag(ap, tag);
1123 memcpy(&qc->result_tf, &tf, sizeof(tf));
1124 qc->err_mask |= AC_ERR_DEV;
1125 ehc->i.err_mask &= ~AC_ERR_DEV;
1129 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1130 * @qc: qc to analyze
1131 * @tf: Taskfile registers to analyze
1133 * Analyze taskfile of @qc and further determine cause of
1134 * failure. This function also requests ATAPI sense data if
1138 * Kernel thread context (may sleep).
1141 * Determined recovery action
1143 static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1144 const struct ata_taskfile *tf)
1146 unsigned int tmp, action = 0;
1147 u8 stat = tf->command, err = tf->feature;
1149 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1150 qc->err_mask |= AC_ERR_HSM;
1151 return ATA_EH_SOFTRESET;
1154 if (stat & (ATA_ERR | ATA_DF))
1155 qc->err_mask |= AC_ERR_DEV;
1159 switch (qc->dev->class) {
1162 qc->err_mask |= AC_ERR_ATA_BUS;
1164 qc->err_mask |= AC_ERR_MEDIA;
1166 qc->err_mask |= AC_ERR_INVALID;
1170 if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
1171 tmp = atapi_eh_request_sense(qc);
1173 /* ATA_QCFLAG_SENSE_VALID is used to
1174 * tell atapi_qc_complete() that sense
1175 * data is already valid.
1177 * TODO: interpret sense data and set
1178 * appropriate err_mask.
1180 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1182 qc->err_mask |= tmp;
1186 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1187 action |= ATA_EH_SOFTRESET;
1192 static int ata_eh_categorize_error(int is_io, unsigned int err_mask)
1194 if (err_mask & AC_ERR_ATA_BUS)
1197 if (err_mask & AC_ERR_TIMEOUT)
1201 if (err_mask & AC_ERR_HSM)
1204 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1211 struct speed_down_verdict_arg {
1216 static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
1218 struct speed_down_verdict_arg *arg = void_arg;
1219 int cat = ata_eh_categorize_error(ent->is_io, ent->err_mask);
1221 if (ent->timestamp < arg->since)
1224 arg->nr_errors[cat]++;
1229 * ata_eh_speed_down_verdict - Determine speed down verdict
1230 * @dev: Device of interest
1232 * This function examines error ring of @dev and determines
1233 * whether NCQ needs to be turned off, transfer speed should be
1234 * stepped down, or falling back to PIO is necessary.
1236 * Cat-1 is ATA_BUS error for any command.
1238 * Cat-2 is TIMEOUT for any command or HSM violation for known
1239 * supported commands.
1241 * Cat-3 is is unclassified DEV error for known supported
1244 * NCQ needs to be turned off if there have been more than 3
1245 * Cat-2 + Cat-3 errors during last 10 minutes.
1247 * Speed down is necessary if there have been more than 3 Cat-1 +
1248 * Cat-2 errors or 10 Cat-3 errors during last 10 minutes.
1250 * Falling back to PIO mode is necessary if there have been more
1251 * than 10 Cat-1 + Cat-2 + Cat-3 errors during last 5 minutes.
1254 * Inherited from caller.
1257 * OR of ATA_EH_SPDN_* flags.
1259 static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
1261 const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
1262 u64 j64 = get_jiffies_64();
1263 struct speed_down_verdict_arg arg;
1264 unsigned int verdict = 0;
1266 /* scan past 10 mins of error history */
1267 memset(&arg, 0, sizeof(arg));
1268 arg.since = j64 - min(j64, j10mins);
1269 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1271 if (arg.nr_errors[2] + arg.nr_errors[3] > 3)
1272 verdict |= ATA_EH_SPDN_NCQ_OFF;
1273 if (arg.nr_errors[1] + arg.nr_errors[2] > 3 || arg.nr_errors[3] > 10)
1274 verdict |= ATA_EH_SPDN_SPEED_DOWN;
1276 /* scan past 3 mins of error history */
1277 memset(&arg, 0, sizeof(arg));
1278 arg.since = j64 - min(j64, j5mins);
1279 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1281 if (arg.nr_errors[1] + arg.nr_errors[2] + arg.nr_errors[3] > 10)
1282 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
1288 * ata_eh_speed_down - record error and speed down if necessary
1289 * @dev: Failed device
1290 * @is_io: Did the device fail during normal IO?
1291 * @err_mask: err_mask of the error
1293 * Record error and examine error history to determine whether
1294 * adjusting transmission speed is necessary. It also sets
1295 * transmission limits appropriately if such adjustment is
1299 * Kernel thread context (may sleep).
1302 * Determined recovery action.
1304 static unsigned int ata_eh_speed_down(struct ata_device *dev, int is_io,
1305 unsigned int err_mask)
1307 unsigned int verdict;
1308 unsigned int action = 0;
1310 /* don't bother if Cat-0 error */
1311 if (ata_eh_categorize_error(is_io, err_mask) == 0)
1314 /* record error and determine whether speed down is necessary */
1315 ata_ering_record(&dev->ering, is_io, err_mask);
1316 verdict = ata_eh_speed_down_verdict(dev);
1319 if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
1320 (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
1321 ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
1322 dev->flags |= ATA_DFLAG_NCQ_OFF;
1323 ata_dev_printk(dev, KERN_WARNING,
1324 "NCQ disabled due to excessive errors\n");
1329 if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1330 /* speed down SATA link speed if possible */
1331 if (sata_down_spd_limit(dev->ap) == 0) {
1332 action |= ATA_EH_HARDRESET;
1336 /* lower transfer mode */
1337 if (dev->spdn_cnt < 2) {
1338 static const int dma_dnxfer_sel[] =
1339 { ATA_DNXFER_DMA, ATA_DNXFER_40C };
1340 static const int pio_dnxfer_sel[] =
1341 { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
1344 if (dev->xfer_shift != ATA_SHIFT_PIO)
1345 sel = dma_dnxfer_sel[dev->spdn_cnt];
1347 sel = pio_dnxfer_sel[dev->spdn_cnt];
1351 if (ata_down_xfermask_limit(dev, sel) == 0) {
1352 action |= ATA_EH_SOFTRESET;
1358 /* Fall back to PIO? Slowing down to PIO is meaningless for
1359 * SATA. Consider it only for PATA.
1361 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
1362 (dev->ap->cbl != ATA_CBL_SATA) &&
1363 (dev->xfer_shift != ATA_SHIFT_PIO)) {
1364 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
1366 action |= ATA_EH_SOFTRESET;
1373 /* device has been slowed down, blow error history */
1374 ata_ering_clear(&dev->ering);
1379 * ata_eh_autopsy - analyze error and determine recovery action
1380 * @ap: ATA port to perform autopsy on
1382 * Analyze why @ap failed and determine which recovery action is
1383 * needed. This function also sets more detailed AC_ERR_* values
1384 * and fills sense data for ATAPI CHECK SENSE.
1387 * Kernel thread context (may sleep).
1389 static void ata_eh_autopsy(struct ata_port *ap)
1391 struct ata_eh_context *ehc = &ap->eh_context;
1392 unsigned int all_err_mask = 0;
1399 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1402 /* obtain and analyze SError */
1403 rc = sata_scr_read(ap, SCR_ERROR, &serror);
1405 ehc->i.serror |= serror;
1406 ata_eh_analyze_serror(ap);
1407 } else if (rc != -EOPNOTSUPP)
1408 ehc->i.action |= ATA_EH_HARDRESET;
1410 /* analyze NCQ failure */
1411 ata_eh_analyze_ncq_error(ap);
1413 /* any real error trumps AC_ERR_OTHER */
1414 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1415 ehc->i.err_mask &= ~AC_ERR_OTHER;
1417 all_err_mask |= ehc->i.err_mask;
1419 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1420 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1422 if (!(qc->flags & ATA_QCFLAG_FAILED))
1425 /* inherit upper level err_mask */
1426 qc->err_mask |= ehc->i.err_mask;
1429 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1431 /* DEV errors are probably spurious in case of ATA_BUS error */
1432 if (qc->err_mask & AC_ERR_ATA_BUS)
1433 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1436 /* any real error trumps unknown error */
1437 if (qc->err_mask & ~AC_ERR_OTHER)
1438 qc->err_mask &= ~AC_ERR_OTHER;
1440 /* SENSE_VALID trumps dev/unknown error and revalidation */
1441 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1442 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1443 ehc->i.action &= ~ATA_EH_REVALIDATE;
1446 /* accumulate error info */
1447 ehc->i.dev = qc->dev;
1448 all_err_mask |= qc->err_mask;
1449 if (qc->flags & ATA_QCFLAG_IO)
1453 /* enforce default EH actions */
1454 if (ap->pflags & ATA_PFLAG_FROZEN ||
1455 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
1456 ehc->i.action |= ATA_EH_SOFTRESET;
1457 else if (all_err_mask)
1458 ehc->i.action |= ATA_EH_REVALIDATE;
1460 /* if we have offending qcs and the associated failed device */
1463 ehc->i.action |= ata_eh_speed_down(ehc->i.dev, is_io,
1466 /* perform per-dev EH action only on the offending device */
1467 ehc->i.dev_action[ehc->i.dev->devno] |=
1468 ehc->i.action & ATA_EH_PERDEV_MASK;
1469 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
1476 * ata_eh_report - report error handling to user
1477 * @ap: ATA port EH is going on
1479 * Report EH to user.
1484 static void ata_eh_report(struct ata_port *ap)
1486 struct ata_eh_context *ehc = &ap->eh_context;
1487 const char *frozen, *desc;
1488 int tag, nr_failed = 0;
1491 if (ehc->i.desc[0] != '\0')
1494 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1495 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1497 if (!(qc->flags & ATA_QCFLAG_FAILED))
1499 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1505 if (!nr_failed && !ehc->i.err_mask)
1509 if (ap->pflags & ATA_PFLAG_FROZEN)
1513 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1514 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1515 ehc->i.err_mask, ap->sactive, ehc->i.serror,
1516 ehc->i.action, frozen);
1518 ata_dev_printk(ehc->i.dev, KERN_ERR, "(%s)\n", desc);
1520 ata_port_printk(ap, KERN_ERR, "exception Emask 0x%x "
1521 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1522 ehc->i.err_mask, ap->sactive, ehc->i.serror,
1523 ehc->i.action, frozen);
1525 ata_port_printk(ap, KERN_ERR, "(%s)\n", desc);
1528 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1529 static const char *dma_str[] = {
1530 [DMA_BIDIRECTIONAL] = "bidi",
1531 [DMA_TO_DEVICE] = "out",
1532 [DMA_FROM_DEVICE] = "in",
1535 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1536 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
1538 if (!(qc->flags & ATA_QCFLAG_FAILED) || !qc->err_mask)
1541 ata_dev_printk(qc->dev, KERN_ERR,
1542 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
1543 "tag %d cdb 0x%x data %u %s\n "
1544 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
1545 "Emask 0x%x (%s)\n",
1546 cmd->command, cmd->feature, cmd->nsect,
1547 cmd->lbal, cmd->lbam, cmd->lbah,
1548 cmd->hob_feature, cmd->hob_nsect,
1549 cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
1550 cmd->device, qc->tag, qc->cdb[0], qc->nbytes,
1551 dma_str[qc->dma_dir],
1552 res->command, res->feature, res->nsect,
1553 res->lbal, res->lbam, res->lbah,
1554 res->hob_feature, res->hob_nsect,
1555 res->hob_lbal, res->hob_lbam, res->hob_lbah,
1556 res->device, qc->err_mask, ata_err_string(qc->err_mask));
1560 static int ata_do_reset(struct ata_port *ap, ata_reset_fn_t reset,
1561 unsigned int *classes)
1565 for (i = 0; i < ATA_MAX_DEVICES; i++)
1566 classes[i] = ATA_DEV_UNKNOWN;
1568 rc = reset(ap, classes);
1572 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
1573 * is complete and convert all ATA_DEV_UNKNOWN to
1576 for (i = 0; i < ATA_MAX_DEVICES; i++)
1577 if (classes[i] != ATA_DEV_UNKNOWN)
1580 if (i < ATA_MAX_DEVICES)
1581 for (i = 0; i < ATA_MAX_DEVICES; i++)
1582 if (classes[i] == ATA_DEV_UNKNOWN)
1583 classes[i] = ATA_DEV_NONE;
1588 static int ata_eh_followup_srst_needed(int rc, int classify,
1589 const unsigned int *classes)
1595 if (classify && classes[0] == ATA_DEV_UNKNOWN)
1600 static int ata_eh_reset(struct ata_port *ap, int classify,
1601 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
1602 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
1604 struct ata_eh_context *ehc = &ap->eh_context;
1605 unsigned int *classes = ehc->classes;
1606 int tries = ATA_EH_RESET_TRIES;
1607 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
1608 unsigned int action;
1609 ata_reset_fn_t reset;
1610 int i, did_followup_srst, rc;
1612 /* about to reset */
1613 ata_eh_about_to_do(ap, NULL, ehc->i.action & ATA_EH_RESET_MASK);
1615 /* Determine which reset to use and record in ehc->i.action.
1616 * prereset() may examine and modify it.
1618 action = ehc->i.action;
1619 ehc->i.action &= ~ATA_EH_RESET_MASK;
1620 if (softreset && (!hardreset || (!sata_set_spd_needed(ap) &&
1621 !(action & ATA_EH_HARDRESET))))
1622 ehc->i.action |= ATA_EH_SOFTRESET;
1624 ehc->i.action |= ATA_EH_HARDRESET;
1629 if (rc == -ENOENT) {
1630 ata_port_printk(ap, KERN_DEBUG,
1631 "port disabled. ignoring.\n");
1632 ap->eh_context.i.action &= ~ATA_EH_RESET_MASK;
1634 for (i = 0; i < ATA_MAX_DEVICES; i++)
1635 classes[i] = ATA_DEV_NONE;
1639 ata_port_printk(ap, KERN_ERR,
1640 "prereset failed (errno=%d)\n", rc);
1645 /* prereset() might have modified ehc->i.action */
1646 if (ehc->i.action & ATA_EH_HARDRESET)
1648 else if (ehc->i.action & ATA_EH_SOFTRESET)
1651 /* prereset told us not to reset, bang classes and return */
1652 for (i = 0; i < ATA_MAX_DEVICES; i++)
1653 classes[i] = ATA_DEV_NONE;
1657 /* did prereset() screw up? if so, fix up to avoid oopsing */
1659 ata_port_printk(ap, KERN_ERR, "BUG: prereset() requested "
1660 "invalid reset type\n");
1668 /* shut up during boot probing */
1670 ata_port_printk(ap, KERN_INFO, "%s resetting port\n",
1671 reset == softreset ? "soft" : "hard");
1673 /* mark that this EH session started with reset */
1674 ehc->i.flags |= ATA_EHI_DID_RESET;
1676 rc = ata_do_reset(ap, reset, classes);
1678 did_followup_srst = 0;
1679 if (reset == hardreset &&
1680 ata_eh_followup_srst_needed(rc, classify, classes)) {
1681 /* okay, let's do follow-up softreset */
1682 did_followup_srst = 1;
1686 ata_port_printk(ap, KERN_ERR,
1687 "follow-up softreset required "
1688 "but no softreset avaliable\n");
1692 ata_eh_about_to_do(ap, NULL, ATA_EH_RESET_MASK);
1693 rc = ata_do_reset(ap, reset, classes);
1695 if (rc == 0 && classify &&
1696 classes[0] == ATA_DEV_UNKNOWN) {
1697 ata_port_printk(ap, KERN_ERR,
1698 "classification failed\n");
1703 if (rc && --tries) {
1706 if (reset == softreset) {
1707 if (did_followup_srst)
1708 type = "follow-up soft";
1714 ata_port_printk(ap, KERN_WARNING,
1715 "%sreset failed, retrying in 5 secs\n", type);
1718 if (reset == hardreset)
1719 sata_down_spd_limit(ap);
1726 /* After the reset, the device state is PIO 0 and the
1727 * controller state is undefined. Record the mode.
1729 for (i = 0; i < ATA_MAX_DEVICES; i++)
1730 ap->device[i].pio_mode = XFER_PIO_0;
1733 postreset(ap, classes);
1735 /* reset successful, schedule revalidation */
1736 ata_eh_done(ap, NULL, ehc->i.action & ATA_EH_RESET_MASK);
1737 ehc->i.action |= ATA_EH_REVALIDATE;
1743 static int ata_eh_revalidate_and_attach(struct ata_port *ap,
1744 struct ata_device **r_failed_dev)
1746 struct ata_eh_context *ehc = &ap->eh_context;
1747 struct ata_device *dev;
1748 unsigned int new_mask = 0;
1749 unsigned long flags;
1754 /* For PATA drive side cable detection to work, IDENTIFY must
1755 * be done backwards such that PDIAG- is released by the slave
1756 * device before the master device is identified.
1758 for (i = ATA_MAX_DEVICES - 1; i >= 0; i--) {
1759 unsigned int action, readid_flags = 0;
1761 dev = &ap->device[i];
1762 action = ata_eh_dev_action(dev);
1764 if (ehc->i.flags & ATA_EHI_DID_RESET)
1765 readid_flags |= ATA_READID_POSTRESET;
1767 if (action & ATA_EH_REVALIDATE && ata_dev_ready(dev)) {
1768 if (ata_port_offline(ap)) {
1773 ata_eh_about_to_do(ap, dev, ATA_EH_REVALIDATE);
1774 rc = ata_dev_revalidate(dev, readid_flags);
1778 ata_eh_done(ap, dev, ATA_EH_REVALIDATE);
1780 /* Configuration may have changed, reconfigure
1783 ehc->i.flags |= ATA_EHI_SETMODE;
1785 /* schedule the scsi_rescan_device() here */
1786 queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
1787 } else if (dev->class == ATA_DEV_UNKNOWN &&
1788 ehc->tries[dev->devno] &&
1789 ata_class_enabled(ehc->classes[dev->devno])) {
1790 dev->class = ehc->classes[dev->devno];
1792 rc = ata_dev_read_id(dev, &dev->class, readid_flags,
1799 /* IDENTIFY was issued to non-existent
1800 * device. No need to reset. Just
1801 * thaw and kill the device.
1803 ata_eh_thaw_port(ap);
1804 dev->class = ATA_DEV_UNKNOWN;
1807 dev->class = ATA_DEV_UNKNOWN;
1813 /* Configure new devices forward such that user doesn't see
1814 * device detection messages backwards.
1816 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1817 dev = &ap->device[i];
1819 if (!(new_mask & (1 << i)))
1822 ehc->i.flags |= ATA_EHI_PRINTINFO;
1823 rc = ata_dev_configure(dev);
1824 ehc->i.flags &= ~ATA_EHI_PRINTINFO;
1828 spin_lock_irqsave(ap->lock, flags);
1829 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
1830 spin_unlock_irqrestore(ap->lock, flags);
1832 /* new device discovered, configure xfermode */
1833 ehc->i.flags |= ATA_EHI_SETMODE;
1839 *r_failed_dev = dev;
1840 DPRINTK("EXIT rc=%d\n", rc);
1846 * ata_eh_suspend - handle suspend EH action
1847 * @ap: target host port
1848 * @r_failed_dev: result parameter to indicate failing device
1850 * Handle suspend EH action. Disk devices are spinned down and
1851 * other types of devices are just marked suspended. Once
1852 * suspended, no EH action to the device is allowed until it is
1856 * Kernel thread context (may sleep).
1859 * 0 on success, -errno otherwise
1861 static int ata_eh_suspend(struct ata_port *ap, struct ata_device **r_failed_dev)
1863 struct ata_device *dev;
1868 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1869 unsigned long flags;
1870 unsigned int action, err_mask;
1872 dev = &ap->device[i];
1873 action = ata_eh_dev_action(dev);
1875 if (!ata_dev_enabled(dev) || !(action & ATA_EH_SUSPEND))
1878 WARN_ON(dev->flags & ATA_DFLAG_SUSPENDED);
1880 ata_eh_about_to_do(ap, dev, ATA_EH_SUSPEND);
1882 if (dev->class == ATA_DEV_ATA && !(action & ATA_EH_PM_FREEZE)) {
1884 rc = ata_flush_cache(dev);
1889 err_mask = ata_do_simple_cmd(dev, ATA_CMD_STANDBYNOW1);
1891 ata_dev_printk(dev, KERN_ERR, "failed to "
1892 "spin down (err_mask=0x%x)\n",
1899 spin_lock_irqsave(ap->lock, flags);
1900 dev->flags |= ATA_DFLAG_SUSPENDED;
1901 spin_unlock_irqrestore(ap->lock, flags);
1903 ata_eh_done(ap, dev, ATA_EH_SUSPEND);
1907 *r_failed_dev = dev;
1914 * ata_eh_prep_resume - prep for resume EH action
1915 * @ap: target host port
1917 * Clear SUSPENDED in preparation for scheduled resume actions.
1918 * This allows other parts of EH to access the devices being
1922 * Kernel thread context (may sleep).
1924 static void ata_eh_prep_resume(struct ata_port *ap)
1926 struct ata_device *dev;
1927 unsigned long flags;
1932 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1933 unsigned int action;
1935 dev = &ap->device[i];
1936 action = ata_eh_dev_action(dev);
1938 if (!ata_dev_enabled(dev) || !(action & ATA_EH_RESUME))
1941 spin_lock_irqsave(ap->lock, flags);
1942 dev->flags &= ~ATA_DFLAG_SUSPENDED;
1943 spin_unlock_irqrestore(ap->lock, flags);
1950 * ata_eh_resume - handle resume EH action
1951 * @ap: target host port
1952 * @r_failed_dev: result parameter to indicate failing device
1954 * Handle resume EH action. Target devices are already reset and
1955 * revalidated. Spinning up is the only operation left.
1958 * Kernel thread context (may sleep).
1961 * 0 on success, -errno otherwise
1963 static int ata_eh_resume(struct ata_port *ap, struct ata_device **r_failed_dev)
1965 struct ata_device *dev;
1970 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1971 unsigned int action, err_mask;
1973 dev = &ap->device[i];
1974 action = ata_eh_dev_action(dev);
1976 if (!ata_dev_enabled(dev) || !(action & ATA_EH_RESUME))
1979 ata_eh_about_to_do(ap, dev, ATA_EH_RESUME);
1981 if (dev->class == ATA_DEV_ATA && !(action & ATA_EH_PM_FREEZE)) {
1982 err_mask = ata_do_simple_cmd(dev,
1983 ATA_CMD_IDLEIMMEDIATE);
1985 ata_dev_printk(dev, KERN_ERR, "failed to "
1986 "spin up (err_mask=0x%x)\n",
1993 ata_eh_done(ap, dev, ATA_EH_RESUME);
1997 *r_failed_dev = dev;
2002 #endif /* CONFIG_PM */
2004 static int ata_port_nr_enabled(struct ata_port *ap)
2008 for (i = 0; i < ATA_MAX_DEVICES; i++)
2009 if (ata_dev_enabled(&ap->device[i]))
2014 static int ata_port_nr_vacant(struct ata_port *ap)
2018 for (i = 0; i < ATA_MAX_DEVICES; i++)
2019 if (ap->device[i].class == ATA_DEV_UNKNOWN)
2024 static int ata_eh_skip_recovery(struct ata_port *ap)
2026 struct ata_eh_context *ehc = &ap->eh_context;
2029 /* skip if all possible devices are suspended */
2030 for (i = 0; i < ata_port_max_devices(ap); i++) {
2031 struct ata_device *dev = &ap->device[i];
2033 if (!(dev->flags & ATA_DFLAG_SUSPENDED))
2037 if (i == ata_port_max_devices(ap))
2040 /* thaw frozen port, resume link and recover failed devices */
2041 if ((ap->pflags & ATA_PFLAG_FROZEN) ||
2042 (ehc->i.flags & ATA_EHI_RESUME_LINK) || ata_port_nr_enabled(ap))
2045 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
2046 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2047 struct ata_device *dev = &ap->device[i];
2049 if (dev->class == ATA_DEV_UNKNOWN &&
2050 ehc->classes[dev->devno] != ATA_DEV_NONE)
2058 * ata_eh_recover - recover host port after error
2059 * @ap: host port to recover
2060 * @prereset: prereset method (can be NULL)
2061 * @softreset: softreset method (can be NULL)
2062 * @hardreset: hardreset method (can be NULL)
2063 * @postreset: postreset method (can be NULL)
2065 * This is the alpha and omega, eum and yang, heart and soul of
2066 * libata exception handling. On entry, actions required to
2067 * recover the port and hotplug requests are recorded in
2068 * eh_context. This function executes all the operations with
2069 * appropriate retrials and fallbacks to resurrect failed
2070 * devices, detach goners and greet newcomers.
2073 * Kernel thread context (may sleep).
2076 * 0 on success, -errno on failure.
2078 static int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
2079 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2080 ata_postreset_fn_t postreset)
2082 struct ata_eh_context *ehc = &ap->eh_context;
2083 struct ata_device *dev;
2088 /* prep for recovery */
2089 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2090 dev = &ap->device[i];
2092 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2094 /* collect port action mask recorded in dev actions */
2095 ehc->i.action |= ehc->i.dev_action[i] & ~ATA_EH_PERDEV_MASK;
2096 ehc->i.dev_action[i] &= ATA_EH_PERDEV_MASK;
2098 /* process hotplug request */
2099 if (dev->flags & ATA_DFLAG_DETACH)
2100 ata_eh_detach_dev(dev);
2102 if (!ata_dev_enabled(dev) &&
2103 ((ehc->i.probe_mask & (1 << dev->devno)) &&
2104 !(ehc->did_probe_mask & (1 << dev->devno)))) {
2105 ata_eh_detach_dev(dev);
2107 ehc->did_probe_mask |= (1 << dev->devno);
2108 ehc->i.action |= ATA_EH_SOFTRESET;
2115 /* if UNLOADING, finish immediately */
2116 if (ap->pflags & ATA_PFLAG_UNLOADING)
2119 /* prep for resume */
2120 ata_eh_prep_resume(ap);
2122 /* skip EH if possible. */
2123 if (ata_eh_skip_recovery(ap))
2126 for (i = 0; i < ATA_MAX_DEVICES; i++)
2127 ehc->classes[i] = ATA_DEV_UNKNOWN;
2130 if (ehc->i.action & ATA_EH_RESET_MASK) {
2131 ata_eh_freeze_port(ap);
2133 rc = ata_eh_reset(ap, ata_port_nr_vacant(ap), prereset,
2134 softreset, hardreset, postreset);
2136 ata_port_printk(ap, KERN_ERR,
2137 "reset failed, giving up\n");
2141 ata_eh_thaw_port(ap);
2144 /* revalidate existing devices and attach new ones */
2145 rc = ata_eh_revalidate_and_attach(ap, &dev);
2149 /* resume devices */
2150 rc = ata_eh_resume(ap, &dev);
2154 /* configure transfer mode if necessary */
2155 if (ehc->i.flags & ATA_EHI_SETMODE) {
2156 rc = ata_set_mode(ap, &dev);
2159 ehc->i.flags &= ~ATA_EHI_SETMODE;
2162 /* suspend devices */
2163 rc = ata_eh_suspend(ap, &dev);
2170 ehc->tries[dev->devno]--;
2174 /* eeek, something went very wrong, give up */
2175 ehc->tries[dev->devno] = 0;
2179 /* device missing or wrong IDENTIFY data, schedule probing */
2180 ehc->i.probe_mask |= (1 << dev->devno);
2181 /* give it just one more chance */
2182 ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
2184 if (ehc->tries[dev->devno] == 1) {
2185 /* This is the last chance, better to slow
2186 * down than lose it.
2188 sata_down_spd_limit(ap);
2189 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2193 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2194 /* disable device if it has used up all its chances */
2195 ata_dev_disable(dev);
2197 /* detach if offline */
2198 if (ata_port_offline(ap))
2199 ata_eh_detach_dev(dev);
2201 /* probe if requested */
2202 if ((ehc->i.probe_mask & (1 << dev->devno)) &&
2203 !(ehc->did_probe_mask & (1 << dev->devno))) {
2204 ata_eh_detach_dev(dev);
2207 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2208 ehc->did_probe_mask |= (1 << dev->devno);
2209 ehc->i.action |= ATA_EH_SOFTRESET;
2212 /* soft didn't work? be haaaaard */
2213 if (ehc->i.flags & ATA_EHI_DID_RESET)
2214 ehc->i.action |= ATA_EH_HARDRESET;
2216 ehc->i.action |= ATA_EH_SOFTRESET;
2219 if (ata_port_nr_enabled(ap)) {
2220 ata_port_printk(ap, KERN_WARNING, "failed to recover some "
2221 "devices, retrying in 5 secs\n");
2224 /* no device left, repeat fast */
2232 for (i = 0; i < ATA_MAX_DEVICES; i++)
2233 ata_dev_disable(&ap->device[i]);
2236 DPRINTK("EXIT, rc=%d\n", rc);
2241 * ata_eh_finish - finish up EH
2242 * @ap: host port to finish EH for
2244 * Recovery is complete. Clean up EH states and retry or finish
2250 static void ata_eh_finish(struct ata_port *ap)
2254 /* retry or finish qcs */
2255 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2256 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2258 if (!(qc->flags & ATA_QCFLAG_FAILED))
2262 /* FIXME: Once EH migration is complete,
2263 * generate sense data in this function,
2264 * considering both err_mask and tf.
2266 if (qc->err_mask & AC_ERR_INVALID)
2267 ata_eh_qc_complete(qc);
2269 ata_eh_qc_retry(qc);
2271 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2272 ata_eh_qc_complete(qc);
2274 /* feed zero TF to sense generation */
2275 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2276 ata_eh_qc_retry(qc);
2283 * ata_do_eh - do standard error handling
2284 * @ap: host port to handle error for
2285 * @prereset: prereset method (can be NULL)
2286 * @softreset: softreset method (can be NULL)
2287 * @hardreset: hardreset method (can be NULL)
2288 * @postreset: postreset method (can be NULL)
2290 * Perform standard error handling sequence.
2293 * Kernel thread context (may sleep).
2295 void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2296 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2297 ata_postreset_fn_t postreset)
2301 ata_eh_recover(ap, prereset, softreset, hardreset, postreset);
2307 * ata_eh_handle_port_suspend - perform port suspend operation
2308 * @ap: port to suspend
2313 * Kernel thread context (may sleep).
2315 static void ata_eh_handle_port_suspend(struct ata_port *ap)
2317 unsigned long flags;
2320 /* are we suspending? */
2321 spin_lock_irqsave(ap->lock, flags);
2322 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2323 ap->pm_mesg.event == PM_EVENT_ON) {
2324 spin_unlock_irqrestore(ap->lock, flags);
2327 spin_unlock_irqrestore(ap->lock, flags);
2329 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
2332 ata_eh_freeze_port(ap);
2334 if (ap->ops->port_suspend)
2335 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
2338 spin_lock_irqsave(ap->lock, flags);
2340 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
2342 ap->pflags |= ATA_PFLAG_SUSPENDED;
2344 ata_port_schedule_eh(ap);
2346 if (ap->pm_result) {
2347 *ap->pm_result = rc;
2348 ap->pm_result = NULL;
2351 spin_unlock_irqrestore(ap->lock, flags);
2357 * ata_eh_handle_port_resume - perform port resume operation
2358 * @ap: port to resume
2362 * This function also waits upto one second until all devices
2363 * hanging off this port requests resume EH action. This is to
2364 * prevent invoking EH and thus reset multiple times on resume.
2366 * On DPM resume, where some of devices might not be resumed
2367 * together, this may delay port resume upto one second, but such
2368 * DPM resumes are rare and 1 sec delay isn't too bad.
2371 * Kernel thread context (may sleep).
2373 static void ata_eh_handle_port_resume(struct ata_port *ap)
2375 unsigned long timeout;
2376 unsigned long flags;
2379 /* are we resuming? */
2380 spin_lock_irqsave(ap->lock, flags);
2381 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2382 ap->pm_mesg.event != PM_EVENT_ON) {
2383 spin_unlock_irqrestore(ap->lock, flags);
2386 spin_unlock_irqrestore(ap->lock, flags);
2389 if (!(ap->pflags & ATA_PFLAG_SUSPENDED))
2392 if (ap->ops->port_resume)
2393 rc = ap->ops->port_resume(ap);
2395 /* give devices time to request EH */
2396 timeout = jiffies + HZ; /* 1s max */
2398 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2399 struct ata_device *dev = &ap->device[i];
2400 unsigned int action = ata_eh_dev_action(dev);
2402 if ((dev->flags & ATA_DFLAG_SUSPENDED) &&
2403 !(action & ATA_EH_RESUME))
2407 if (i == ATA_MAX_DEVICES || time_after(jiffies, timeout))
2413 spin_lock_irqsave(ap->lock, flags);
2414 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
2415 if (ap->pm_result) {
2416 *ap->pm_result = rc;
2417 ap->pm_result = NULL;
2419 spin_unlock_irqrestore(ap->lock, flags);
2421 #endif /* CONFIG_PM */