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);
55 static void ata_eh_handle_port_suspend(struct ata_port *ap);
56 static void ata_eh_handle_port_resume(struct ata_port *ap);
58 static void ata_ering_record(struct ata_ering *ering, int is_io,
59 unsigned int err_mask)
61 struct ata_ering_entry *ent;
66 ering->cursor %= ATA_ERING_SIZE;
68 ent = &ering->ring[ering->cursor];
70 ent->err_mask = err_mask;
71 ent->timestamp = get_jiffies_64();
74 static void ata_ering_clear(struct ata_ering *ering)
76 memset(ering, 0, sizeof(*ering));
79 static int ata_ering_map(struct ata_ering *ering,
80 int (*map_fn)(struct ata_ering_entry *, void *),
84 struct ata_ering_entry *ent;
88 ent = &ering->ring[idx];
91 rc = map_fn(ent, arg);
94 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
95 } while (idx != ering->cursor);
100 static unsigned int ata_eh_dev_action(struct ata_device *dev)
102 struct ata_eh_context *ehc = &dev->ap->eh_context;
104 return ehc->i.action | ehc->i.dev_action[dev->devno];
107 static void ata_eh_clear_action(struct ata_device *dev,
108 struct ata_eh_info *ehi, unsigned int action)
113 ehi->action &= ~action;
114 for (i = 0; i < ATA_MAX_DEVICES; i++)
115 ehi->dev_action[i] &= ~action;
117 /* doesn't make sense for port-wide EH actions */
118 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
120 /* break ehi->action into ehi->dev_action */
121 if (ehi->action & action) {
122 for (i = 0; i < ATA_MAX_DEVICES; i++)
123 ehi->dev_action[i] |= ehi->action & action;
124 ehi->action &= ~action;
127 /* turn off the specified per-dev action */
128 ehi->dev_action[dev->devno] &= ~action;
133 * ata_scsi_timed_out - SCSI layer time out callback
134 * @cmd: timed out SCSI command
136 * Handles SCSI layer timeout. We race with normal completion of
137 * the qc for @cmd. If the qc is already gone, we lose and let
138 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
139 * timed out and EH should be invoked. Prevent ata_qc_complete()
140 * from finishing it by setting EH_SCHEDULED and return
143 * TODO: kill this function once old EH is gone.
146 * Called from timer context
149 * EH_HANDLED or EH_NOT_HANDLED
151 enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
153 struct Scsi_Host *host = cmd->device->host;
154 struct ata_port *ap = ata_shost_to_port(host);
156 struct ata_queued_cmd *qc;
157 enum scsi_eh_timer_return ret;
161 if (ap->ops->error_handler) {
162 ret = EH_NOT_HANDLED;
167 spin_lock_irqsave(ap->lock, flags);
168 qc = ata_qc_from_tag(ap, ap->active_tag);
170 WARN_ON(qc->scsicmd != cmd);
171 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
172 qc->err_mask |= AC_ERR_TIMEOUT;
173 ret = EH_NOT_HANDLED;
175 spin_unlock_irqrestore(ap->lock, flags);
178 DPRINTK("EXIT, ret=%d\n", ret);
183 * ata_scsi_error - SCSI layer error handler callback
184 * @host: SCSI host on which error occurred
186 * Handles SCSI-layer-thrown error events.
189 * Inherited from SCSI layer (none, can sleep)
194 void ata_scsi_error(struct Scsi_Host *host)
196 struct ata_port *ap = ata_shost_to_port(host);
197 int i, repeat_cnt = ATA_EH_MAX_REPEAT;
202 /* synchronize with port task */
203 ata_port_flush_task(ap);
205 /* synchronize with host lock and sort out timeouts */
207 /* For new EH, all qcs are finished in one of three ways -
208 * normal completion, error completion, and SCSI timeout.
209 * Both cmpletions can race against SCSI timeout. When normal
210 * completion wins, the qc never reaches EH. When error
211 * completion wins, the qc has ATA_QCFLAG_FAILED set.
213 * When SCSI timeout wins, things are a bit more complex.
214 * Normal or error completion can occur after the timeout but
215 * before this point. In such cases, both types of
216 * completions are honored. A scmd is determined to have
217 * timed out iff its associated qc is active and not failed.
219 if (ap->ops->error_handler) {
220 struct scsi_cmnd *scmd, *tmp;
223 spin_lock_irqsave(ap->lock, flags);
225 list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
226 struct ata_queued_cmd *qc;
228 for (i = 0; i < ATA_MAX_QUEUE; i++) {
229 qc = __ata_qc_from_tag(ap, i);
230 if (qc->flags & ATA_QCFLAG_ACTIVE &&
235 if (i < ATA_MAX_QUEUE) {
236 /* the scmd has an associated qc */
237 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
238 /* which hasn't failed yet, timeout */
239 qc->err_mask |= AC_ERR_TIMEOUT;
240 qc->flags |= ATA_QCFLAG_FAILED;
244 /* Normal completion occurred after
245 * SCSI timeout but before this point.
246 * Successfully complete it.
248 scmd->retries = scmd->allowed;
249 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
253 /* If we have timed out qcs. They belong to EH from
254 * this point but the state of the controller is
255 * unknown. Freeze the port to make sure the IRQ
256 * handler doesn't diddle with those qcs. This must
257 * be done atomically w.r.t. setting QCFLAG_FAILED.
260 __ata_port_freeze(ap);
262 spin_unlock_irqrestore(ap->lock, flags);
264 spin_unlock_wait(ap->lock);
267 /* invoke error handler */
268 if (ap->ops->error_handler) {
269 /* process port resume request */
270 ata_eh_handle_port_resume(ap);
272 /* fetch & clear EH info */
273 spin_lock_irqsave(ap->lock, flags);
275 memset(&ap->eh_context, 0, sizeof(ap->eh_context));
276 ap->eh_context.i = ap->eh_info;
277 memset(&ap->eh_info, 0, sizeof(ap->eh_info));
279 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
280 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
282 spin_unlock_irqrestore(ap->lock, flags);
284 /* invoke EH, skip if unloading or suspended */
285 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
286 ap->ops->error_handler(ap);
290 /* process port suspend request */
291 ata_eh_handle_port_suspend(ap);
293 /* Exception might have happend after ->error_handler
294 * recovered the port but before this point. Repeat
297 spin_lock_irqsave(ap->lock, flags);
299 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
301 ata_port_printk(ap, KERN_INFO,
302 "EH pending after completion, "
303 "repeating EH (cnt=%d)\n", repeat_cnt);
304 spin_unlock_irqrestore(ap->lock, flags);
307 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
308 "tries, giving up\n", ATA_EH_MAX_REPEAT);
311 /* this run is complete, make sure EH info is clear */
312 memset(&ap->eh_info, 0, sizeof(ap->eh_info));
314 /* Clear host_eh_scheduled while holding ap->lock such
315 * that if exception occurs after this point but
316 * before EH completion, SCSI midlayer will
319 host->host_eh_scheduled = 0;
321 spin_unlock_irqrestore(ap->lock, flags);
323 WARN_ON(ata_qc_from_tag(ap, ap->active_tag) == NULL);
324 ap->ops->eng_timeout(ap);
327 /* finish or retry handled scmd's and clean up */
328 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
330 scsi_eh_flush_done_q(&ap->eh_done_q);
333 spin_lock_irqsave(ap->lock, flags);
335 if (ap->pflags & ATA_PFLAG_LOADING)
336 ap->pflags &= ~ATA_PFLAG_LOADING;
337 else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
338 queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0);
340 if (ap->pflags & ATA_PFLAG_RECOVERED)
341 ata_port_printk(ap, KERN_INFO, "EH complete\n");
343 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
345 /* tell wait_eh that we're done */
346 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
347 wake_up_all(&ap->eh_wait_q);
349 spin_unlock_irqrestore(ap->lock, flags);
355 * ata_port_wait_eh - Wait for the currently pending EH to complete
356 * @ap: Port to wait EH for
358 * Wait until the currently pending EH is complete.
361 * Kernel thread context (may sleep).
363 void ata_port_wait_eh(struct ata_port *ap)
369 spin_lock_irqsave(ap->lock, flags);
371 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
372 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
373 spin_unlock_irqrestore(ap->lock, flags);
375 spin_lock_irqsave(ap->lock, flags);
377 finish_wait(&ap->eh_wait_q, &wait);
379 spin_unlock_irqrestore(ap->lock, flags);
381 /* make sure SCSI EH is complete */
382 if (scsi_host_in_recovery(ap->scsi_host)) {
389 * ata_qc_timeout - Handle timeout of queued command
390 * @qc: Command that timed out
392 * Some part of the kernel (currently, only the SCSI layer)
393 * has noticed that the active command on port @ap has not
394 * completed after a specified length of time. Handle this
395 * condition by disabling DMA (if necessary) and completing
396 * transactions, with error if necessary.
398 * This also handles the case of the "lost interrupt", where
399 * for some reason (possibly hardware bug, possibly driver bug)
400 * an interrupt was not delivered to the driver, even though the
401 * transaction completed successfully.
403 * TODO: kill this function once old EH is gone.
406 * Inherited from SCSI layer (none, can sleep)
408 static void ata_qc_timeout(struct ata_queued_cmd *qc)
410 struct ata_port *ap = qc->ap;
411 u8 host_stat = 0, drv_stat;
416 ap->hsm_task_state = HSM_ST_IDLE;
418 spin_lock_irqsave(ap->lock, flags);
420 switch (qc->tf.protocol) {
423 case ATA_PROT_ATAPI_DMA:
424 host_stat = ap->ops->bmdma_status(ap);
426 /* before we do anything else, clear DMA-Start bit */
427 ap->ops->bmdma_stop(qc);
433 drv_stat = ata_chk_status(ap);
435 /* ack bmdma irq events */
436 ap->ops->irq_clear(ap);
438 ata_dev_printk(qc->dev, KERN_ERR, "command 0x%x timeout, "
439 "stat 0x%x host_stat 0x%x\n",
440 qc->tf.command, drv_stat, host_stat);
442 /* complete taskfile transaction */
443 qc->err_mask |= AC_ERR_TIMEOUT;
447 spin_unlock_irqrestore(ap->lock, flags);
449 ata_eh_qc_complete(qc);
455 * ata_eng_timeout - Handle timeout of queued command
456 * @ap: Port on which timed-out command is active
458 * Some part of the kernel (currently, only the SCSI layer)
459 * has noticed that the active command on port @ap has not
460 * completed after a specified length of time. Handle this
461 * condition by disabling DMA (if necessary) and completing
462 * transactions, with error if necessary.
464 * This also handles the case of the "lost interrupt", where
465 * for some reason (possibly hardware bug, possibly driver bug)
466 * an interrupt was not delivered to the driver, even though the
467 * transaction completed successfully.
469 * TODO: kill this function once old EH is gone.
472 * Inherited from SCSI layer (none, can sleep)
474 void ata_eng_timeout(struct ata_port *ap)
478 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
484 * ata_qc_schedule_eh - schedule qc for error handling
485 * @qc: command to schedule error handling for
487 * Schedule error handling for @qc. EH will kick in as soon as
488 * other commands are drained.
491 * spin_lock_irqsave(host lock)
493 void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
495 struct ata_port *ap = qc->ap;
497 WARN_ON(!ap->ops->error_handler);
499 qc->flags |= ATA_QCFLAG_FAILED;
500 qc->ap->pflags |= ATA_PFLAG_EH_PENDING;
502 /* The following will fail if timeout has already expired.
503 * ata_scsi_error() takes care of such scmds on EH entry.
504 * Note that ATA_QCFLAG_FAILED is unconditionally set after
505 * this function completes.
507 scsi_req_abort_cmd(qc->scsicmd);
511 * ata_port_schedule_eh - schedule error handling without a qc
512 * @ap: ATA port to schedule EH for
514 * Schedule error handling for @ap. EH will kick in as soon as
515 * all commands are drained.
518 * spin_lock_irqsave(host lock)
520 void ata_port_schedule_eh(struct ata_port *ap)
522 WARN_ON(!ap->ops->error_handler);
524 ap->pflags |= ATA_PFLAG_EH_PENDING;
525 scsi_schedule_eh(ap->scsi_host);
527 DPRINTK("port EH scheduled\n");
531 * ata_port_abort - abort all qc's on the port
532 * @ap: ATA port to abort qc's for
534 * Abort all active qc's of @ap and schedule EH.
537 * spin_lock_irqsave(host lock)
540 * Number of aborted qc's.
542 int ata_port_abort(struct ata_port *ap)
544 int tag, nr_aborted = 0;
546 WARN_ON(!ap->ops->error_handler);
548 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
549 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
552 qc->flags |= ATA_QCFLAG_FAILED;
559 ata_port_schedule_eh(ap);
565 * __ata_port_freeze - freeze port
566 * @ap: ATA port to freeze
568 * This function is called when HSM violation or some other
569 * condition disrupts normal operation of the port. Frozen port
570 * is not allowed to perform any operation until the port is
571 * thawed, which usually follows a successful reset.
573 * ap->ops->freeze() callback can be used for freezing the port
574 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
575 * port cannot be frozen hardware-wise, the interrupt handler
576 * must ack and clear interrupts unconditionally while the port
580 * spin_lock_irqsave(host lock)
582 static void __ata_port_freeze(struct ata_port *ap)
584 WARN_ON(!ap->ops->error_handler);
589 ap->pflags |= ATA_PFLAG_FROZEN;
591 DPRINTK("ata%u port frozen\n", ap->print_id);
595 * ata_port_freeze - abort & freeze port
596 * @ap: ATA port to freeze
598 * Abort and freeze @ap.
601 * spin_lock_irqsave(host lock)
604 * Number of aborted commands.
606 int ata_port_freeze(struct ata_port *ap)
610 WARN_ON(!ap->ops->error_handler);
612 nr_aborted = ata_port_abort(ap);
613 __ata_port_freeze(ap);
619 * ata_eh_freeze_port - EH helper to freeze port
620 * @ap: ATA port to freeze
627 void ata_eh_freeze_port(struct ata_port *ap)
631 if (!ap->ops->error_handler)
634 spin_lock_irqsave(ap->lock, flags);
635 __ata_port_freeze(ap);
636 spin_unlock_irqrestore(ap->lock, flags);
640 * ata_port_thaw_port - EH helper to thaw port
641 * @ap: ATA port to thaw
643 * Thaw frozen port @ap.
648 void ata_eh_thaw_port(struct ata_port *ap)
652 if (!ap->ops->error_handler)
655 spin_lock_irqsave(ap->lock, flags);
657 ap->pflags &= ~ATA_PFLAG_FROZEN;
662 spin_unlock_irqrestore(ap->lock, flags);
664 DPRINTK("ata%u port thawed\n", ap->print_id);
667 static void ata_eh_scsidone(struct scsi_cmnd *scmd)
672 static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
674 struct ata_port *ap = qc->ap;
675 struct scsi_cmnd *scmd = qc->scsicmd;
678 spin_lock_irqsave(ap->lock, flags);
679 qc->scsidone = ata_eh_scsidone;
680 __ata_qc_complete(qc);
681 WARN_ON(ata_tag_valid(qc->tag));
682 spin_unlock_irqrestore(ap->lock, flags);
684 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
688 * ata_eh_qc_complete - Complete an active ATA command from EH
689 * @qc: Command to complete
691 * Indicate to the mid and upper layers that an ATA command has
692 * completed. To be used from EH.
694 void ata_eh_qc_complete(struct ata_queued_cmd *qc)
696 struct scsi_cmnd *scmd = qc->scsicmd;
697 scmd->retries = scmd->allowed;
698 __ata_eh_qc_complete(qc);
702 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
703 * @qc: Command to retry
705 * Indicate to the mid and upper layers that an ATA command
706 * should be retried. To be used from EH.
708 * SCSI midlayer limits the number of retries to scmd->allowed.
709 * scmd->retries is decremented for commands which get retried
710 * due to unrelated failures (qc->err_mask is zero).
712 void ata_eh_qc_retry(struct ata_queued_cmd *qc)
714 struct scsi_cmnd *scmd = qc->scsicmd;
715 if (!qc->err_mask && scmd->retries)
717 __ata_eh_qc_complete(qc);
721 * ata_eh_detach_dev - detach ATA device
722 * @dev: ATA device to detach
729 static void ata_eh_detach_dev(struct ata_device *dev)
731 struct ata_port *ap = dev->ap;
734 ata_dev_disable(dev);
736 spin_lock_irqsave(ap->lock, flags);
738 dev->flags &= ~ATA_DFLAG_DETACH;
740 if (ata_scsi_offline_dev(dev)) {
741 dev->flags |= ATA_DFLAG_DETACHED;
742 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
745 /* clear per-dev EH actions */
746 ata_eh_clear_action(dev, &ap->eh_info, ATA_EH_PERDEV_MASK);
747 ata_eh_clear_action(dev, &ap->eh_context.i, ATA_EH_PERDEV_MASK);
749 spin_unlock_irqrestore(ap->lock, flags);
753 * ata_eh_about_to_do - about to perform eh_action
754 * @ap: target ATA port
755 * @dev: target ATA dev for per-dev action (can be NULL)
756 * @action: action about to be performed
758 * Called just before performing EH actions to clear related bits
759 * in @ap->eh_info such that eh actions are not unnecessarily
765 static void ata_eh_about_to_do(struct ata_port *ap, struct ata_device *dev,
769 struct ata_eh_info *ehi = &ap->eh_info;
770 struct ata_eh_context *ehc = &ap->eh_context;
772 spin_lock_irqsave(ap->lock, flags);
774 /* Reset is represented by combination of actions and EHI
775 * flags. Suck in all related bits before clearing eh_info to
776 * avoid losing requested action.
778 if (action & ATA_EH_RESET_MASK) {
779 ehc->i.action |= ehi->action & ATA_EH_RESET_MASK;
780 ehc->i.flags |= ehi->flags & ATA_EHI_RESET_MODIFIER_MASK;
782 /* make sure all reset actions are cleared & clear EHI flags */
783 action |= ATA_EH_RESET_MASK;
784 ehi->flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
787 ata_eh_clear_action(dev, ehi, action);
789 if (!(ehc->i.flags & ATA_EHI_QUIET))
790 ap->pflags |= ATA_PFLAG_RECOVERED;
792 spin_unlock_irqrestore(ap->lock, flags);
796 * ata_eh_done - EH action complete
797 * @ap: target ATA port
798 * @dev: target ATA dev for per-dev action (can be NULL)
799 * @action: action just completed
801 * Called right after performing EH actions to clear related bits
802 * in @ap->eh_context.
807 static void ata_eh_done(struct ata_port *ap, struct ata_device *dev,
810 /* if reset is complete, clear all reset actions & reset modifier */
811 if (action & ATA_EH_RESET_MASK) {
812 action |= ATA_EH_RESET_MASK;
813 ap->eh_context.i.flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
816 ata_eh_clear_action(dev, &ap->eh_context.i, action);
820 * ata_err_string - convert err_mask to descriptive string
821 * @err_mask: error mask to convert to string
823 * Convert @err_mask to descriptive string. Errors are
824 * prioritized according to severity and only the most severe
831 * Descriptive string for @err_mask
833 static const char * ata_err_string(unsigned int err_mask)
835 if (err_mask & AC_ERR_HOST_BUS)
836 return "host bus error";
837 if (err_mask & AC_ERR_ATA_BUS)
838 return "ATA bus error";
839 if (err_mask & AC_ERR_TIMEOUT)
841 if (err_mask & AC_ERR_HSM)
842 return "HSM violation";
843 if (err_mask & AC_ERR_SYSTEM)
844 return "internal error";
845 if (err_mask & AC_ERR_MEDIA)
846 return "media error";
847 if (err_mask & AC_ERR_INVALID)
848 return "invalid argument";
849 if (err_mask & AC_ERR_DEV)
850 return "device error";
851 return "unknown error";
855 * ata_read_log_page - read a specific log page
856 * @dev: target device
857 * @page: page to read
858 * @buf: buffer to store read page
859 * @sectors: number of sectors to read
861 * Read log page using READ_LOG_EXT command.
864 * Kernel thread context (may sleep).
867 * 0 on success, AC_ERR_* mask otherwise.
869 static unsigned int ata_read_log_page(struct ata_device *dev,
870 u8 page, void *buf, unsigned int sectors)
872 struct ata_taskfile tf;
873 unsigned int err_mask;
875 DPRINTK("read log page - page %d\n", page);
877 ata_tf_init(dev, &tf);
878 tf.command = ATA_CMD_READ_LOG_EXT;
881 tf.hob_nsect = sectors >> 8;
882 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
883 tf.protocol = ATA_PROT_PIO;
885 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
886 buf, sectors * ATA_SECT_SIZE);
888 DPRINTK("EXIT, err_mask=%x\n", err_mask);
893 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
894 * @dev: Device to read log page 10h from
895 * @tag: Resulting tag of the failed command
896 * @tf: Resulting taskfile registers of the failed command
898 * Read log page 10h to obtain NCQ error details and clear error
902 * Kernel thread context (may sleep).
905 * 0 on success, -errno otherwise.
907 static int ata_eh_read_log_10h(struct ata_device *dev,
908 int *tag, struct ata_taskfile *tf)
910 u8 *buf = dev->ap->sector_buf;
911 unsigned int err_mask;
915 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
920 for (i = 0; i < ATA_SECT_SIZE; i++)
923 ata_dev_printk(dev, KERN_WARNING,
924 "invalid checksum 0x%x on log page 10h\n", csum);
929 *tag = buf[0] & 0x1f;
931 tf->command = buf[2];
932 tf->feature = buf[3];
937 tf->hob_lbal = buf[8];
938 tf->hob_lbam = buf[9];
939 tf->hob_lbah = buf[10];
941 tf->hob_nsect = buf[13];
947 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
948 * @dev: device to perform REQUEST_SENSE to
949 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
951 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
952 * SENSE. This function is EH helper.
955 * Kernel thread context (may sleep).
958 * 0 on success, AC_ERR_* mask on failure
960 static unsigned int atapi_eh_request_sense(struct ata_device *dev,
961 unsigned char *sense_buf)
963 struct ata_port *ap = dev->ap;
964 struct ata_taskfile tf;
965 u8 cdb[ATAPI_CDB_LEN];
967 DPRINTK("ATAPI request sense\n");
969 ata_tf_init(dev, &tf);
971 /* FIXME: is this needed? */
972 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
974 /* XXX: why tf_read here? */
975 ap->ops->tf_read(ap, &tf);
977 /* fill these in, for the case where they are -not- overwritten */
979 sense_buf[2] = tf.feature >> 4;
981 memset(cdb, 0, ATAPI_CDB_LEN);
982 cdb[0] = REQUEST_SENSE;
983 cdb[4] = SCSI_SENSE_BUFFERSIZE;
985 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
986 tf.command = ATA_CMD_PACKET;
988 /* is it pointless to prefer PIO for "safety reasons"? */
989 if (ap->flags & ATA_FLAG_PIO_DMA) {
990 tf.protocol = ATA_PROT_ATAPI_DMA;
991 tf.feature |= ATAPI_PKT_DMA;
993 tf.protocol = ATA_PROT_ATAPI;
994 tf.lbam = (8 * 1024) & 0xff;
995 tf.lbah = (8 * 1024) >> 8;
998 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
999 sense_buf, SCSI_SENSE_BUFFERSIZE);
1003 * ata_eh_analyze_serror - analyze SError for a failed port
1004 * @ap: ATA port to analyze SError for
1006 * Analyze SError if available and further determine cause of
1012 static void ata_eh_analyze_serror(struct ata_port *ap)
1014 struct ata_eh_context *ehc = &ap->eh_context;
1015 u32 serror = ehc->i.serror;
1016 unsigned int err_mask = 0, action = 0;
1018 if (serror & SERR_PERSISTENT) {
1019 err_mask |= AC_ERR_ATA_BUS;
1020 action |= ATA_EH_HARDRESET;
1023 (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
1024 err_mask |= AC_ERR_ATA_BUS;
1025 action |= ATA_EH_SOFTRESET;
1027 if (serror & SERR_PROTOCOL) {
1028 err_mask |= AC_ERR_HSM;
1029 action |= ATA_EH_SOFTRESET;
1031 if (serror & SERR_INTERNAL) {
1032 err_mask |= AC_ERR_SYSTEM;
1033 action |= ATA_EH_SOFTRESET;
1035 if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG))
1036 ata_ehi_hotplugged(&ehc->i);
1038 ehc->i.err_mask |= err_mask;
1039 ehc->i.action |= action;
1043 * ata_eh_analyze_ncq_error - analyze NCQ error
1044 * @ap: ATA port to analyze NCQ error for
1046 * Read log page 10h, determine the offending qc and acquire
1047 * error status TF. For NCQ device errors, all LLDDs have to do
1048 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1052 * Kernel thread context (may sleep).
1054 static void ata_eh_analyze_ncq_error(struct ata_port *ap)
1056 struct ata_eh_context *ehc = &ap->eh_context;
1057 struct ata_device *dev = ap->device;
1058 struct ata_queued_cmd *qc;
1059 struct ata_taskfile tf;
1062 /* if frozen, we can't do much */
1063 if (ap->pflags & ATA_PFLAG_FROZEN)
1066 /* is it NCQ device error? */
1067 if (!ap->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1070 /* has LLDD analyzed already? */
1071 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1072 qc = __ata_qc_from_tag(ap, tag);
1074 if (!(qc->flags & ATA_QCFLAG_FAILED))
1081 /* okay, this error is ours */
1082 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1084 ata_port_printk(ap, KERN_ERR, "failed to read log page 10h "
1085 "(errno=%d)\n", rc);
1089 if (!(ap->sactive & (1 << tag))) {
1090 ata_port_printk(ap, KERN_ERR, "log page 10h reported "
1091 "inactive tag %d\n", tag);
1095 /* we've got the perpetrator, condemn it */
1096 qc = __ata_qc_from_tag(ap, tag);
1097 memcpy(&qc->result_tf, &tf, sizeof(tf));
1098 qc->err_mask |= AC_ERR_DEV;
1099 ehc->i.err_mask &= ~AC_ERR_DEV;
1103 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1104 * @qc: qc to analyze
1105 * @tf: Taskfile registers to analyze
1107 * Analyze taskfile of @qc and further determine cause of
1108 * failure. This function also requests ATAPI sense data if
1112 * Kernel thread context (may sleep).
1115 * Determined recovery action
1117 static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1118 const struct ata_taskfile *tf)
1120 unsigned int tmp, action = 0;
1121 u8 stat = tf->command, err = tf->feature;
1123 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1124 qc->err_mask |= AC_ERR_HSM;
1125 return ATA_EH_SOFTRESET;
1128 if (!(qc->err_mask & AC_ERR_DEV))
1131 switch (qc->dev->class) {
1134 qc->err_mask |= AC_ERR_ATA_BUS;
1136 qc->err_mask |= AC_ERR_MEDIA;
1138 qc->err_mask |= AC_ERR_INVALID;
1142 if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
1143 tmp = atapi_eh_request_sense(qc->dev,
1144 qc->scsicmd->sense_buffer);
1146 /* ATA_QCFLAG_SENSE_VALID is used to
1147 * tell atapi_qc_complete() that sense
1148 * data is already valid.
1150 * TODO: interpret sense data and set
1151 * appropriate err_mask.
1153 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1155 qc->err_mask |= tmp;
1159 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1160 action |= ATA_EH_SOFTRESET;
1165 static int ata_eh_categorize_error(int is_io, unsigned int err_mask)
1167 if (err_mask & AC_ERR_ATA_BUS)
1170 if (err_mask & AC_ERR_TIMEOUT)
1174 if (err_mask & AC_ERR_HSM)
1177 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1184 struct speed_down_verdict_arg {
1189 static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
1191 struct speed_down_verdict_arg *arg = void_arg;
1192 int cat = ata_eh_categorize_error(ent->is_io, ent->err_mask);
1194 if (ent->timestamp < arg->since)
1197 arg->nr_errors[cat]++;
1202 * ata_eh_speed_down_verdict - Determine speed down verdict
1203 * @dev: Device of interest
1205 * This function examines error ring of @dev and determines
1206 * whether NCQ needs to be turned off, transfer speed should be
1207 * stepped down, or falling back to PIO is necessary.
1209 * Cat-1 is ATA_BUS error for any command.
1211 * Cat-2 is TIMEOUT for any command or HSM violation for known
1212 * supported commands.
1214 * Cat-3 is is unclassified DEV error for known supported
1217 * NCQ needs to be turned off if there have been more than 3
1218 * Cat-2 + Cat-3 errors during last 10 minutes.
1220 * Speed down is necessary if there have been more than 3 Cat-1 +
1221 * Cat-2 errors or 10 Cat-3 errors during last 10 minutes.
1223 * Falling back to PIO mode is necessary if there have been more
1224 * than 10 Cat-1 + Cat-2 + Cat-3 errors during last 5 minutes.
1227 * Inherited from caller.
1230 * OR of ATA_EH_SPDN_* flags.
1232 static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
1234 const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
1235 u64 j64 = get_jiffies_64();
1236 struct speed_down_verdict_arg arg;
1237 unsigned int verdict = 0;
1239 /* scan past 10 mins of error history */
1240 memset(&arg, 0, sizeof(arg));
1241 arg.since = j64 - min(j64, j10mins);
1242 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1244 if (arg.nr_errors[2] + arg.nr_errors[3] > 3)
1245 verdict |= ATA_EH_SPDN_NCQ_OFF;
1246 if (arg.nr_errors[1] + arg.nr_errors[2] > 3 || arg.nr_errors[3] > 10)
1247 verdict |= ATA_EH_SPDN_SPEED_DOWN;
1249 /* scan past 3 mins of error history */
1250 memset(&arg, 0, sizeof(arg));
1251 arg.since = j64 - min(j64, j5mins);
1252 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1254 if (arg.nr_errors[1] + arg.nr_errors[2] + arg.nr_errors[3] > 10)
1255 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
1261 * ata_eh_speed_down - record error and speed down if necessary
1262 * @dev: Failed device
1263 * @is_io: Did the device fail during normal IO?
1264 * @err_mask: err_mask of the error
1266 * Record error and examine error history to determine whether
1267 * adjusting transmission speed is necessary. It also sets
1268 * transmission limits appropriately if such adjustment is
1272 * Kernel thread context (may sleep).
1275 * Determined recovery action.
1277 static unsigned int ata_eh_speed_down(struct ata_device *dev, int is_io,
1278 unsigned int err_mask)
1280 unsigned int verdict;
1281 unsigned int action = 0;
1283 /* don't bother if Cat-0 error */
1284 if (ata_eh_categorize_error(is_io, err_mask) == 0)
1287 /* record error and determine whether speed down is necessary */
1288 ata_ering_record(&dev->ering, is_io, err_mask);
1289 verdict = ata_eh_speed_down_verdict(dev);
1292 if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
1293 (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
1294 ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
1295 dev->flags |= ATA_DFLAG_NCQ_OFF;
1296 ata_dev_printk(dev, KERN_WARNING,
1297 "NCQ disabled due to excessive errors\n");
1302 if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1303 /* speed down SATA link speed if possible */
1304 if (sata_down_spd_limit(dev->ap) == 0) {
1305 action |= ATA_EH_HARDRESET;
1309 /* lower transfer mode */
1310 if (dev->spdn_cnt < 2) {
1311 static const int dma_dnxfer_sel[] =
1312 { ATA_DNXFER_DMA, ATA_DNXFER_40C };
1313 static const int pio_dnxfer_sel[] =
1314 { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
1317 if (dev->xfer_shift != ATA_SHIFT_PIO)
1318 sel = dma_dnxfer_sel[dev->spdn_cnt];
1320 sel = pio_dnxfer_sel[dev->spdn_cnt];
1324 if (ata_down_xfermask_limit(dev, sel) == 0) {
1325 action |= ATA_EH_SOFTRESET;
1331 /* Fall back to PIO? Slowing down to PIO is meaningless for
1332 * SATA. Consider it only for PATA.
1334 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
1335 (dev->ap->cbl != ATA_CBL_SATA) &&
1336 (dev->xfer_shift != ATA_SHIFT_PIO)) {
1337 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
1339 action |= ATA_EH_SOFTRESET;
1346 /* device has been slowed down, blow error history */
1347 ata_ering_clear(&dev->ering);
1352 * ata_eh_autopsy - analyze error and determine recovery action
1353 * @ap: ATA port to perform autopsy on
1355 * Analyze why @ap failed and determine which recovery action is
1356 * needed. This function also sets more detailed AC_ERR_* values
1357 * and fills sense data for ATAPI CHECK SENSE.
1360 * Kernel thread context (may sleep).
1362 static void ata_eh_autopsy(struct ata_port *ap)
1364 struct ata_eh_context *ehc = &ap->eh_context;
1365 unsigned int all_err_mask = 0;
1372 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1375 /* obtain and analyze SError */
1376 rc = sata_scr_read(ap, SCR_ERROR, &serror);
1378 ehc->i.serror |= serror;
1379 ata_eh_analyze_serror(ap);
1380 } else if (rc != -EOPNOTSUPP)
1381 ehc->i.action |= ATA_EH_HARDRESET;
1383 /* analyze NCQ failure */
1384 ata_eh_analyze_ncq_error(ap);
1386 /* any real error trumps AC_ERR_OTHER */
1387 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1388 ehc->i.err_mask &= ~AC_ERR_OTHER;
1390 all_err_mask |= ehc->i.err_mask;
1392 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1393 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1395 if (!(qc->flags & ATA_QCFLAG_FAILED))
1398 /* inherit upper level err_mask */
1399 qc->err_mask |= ehc->i.err_mask;
1402 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1404 /* DEV errors are probably spurious in case of ATA_BUS error */
1405 if (qc->err_mask & AC_ERR_ATA_BUS)
1406 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1409 /* any real error trumps unknown error */
1410 if (qc->err_mask & ~AC_ERR_OTHER)
1411 qc->err_mask &= ~AC_ERR_OTHER;
1413 /* SENSE_VALID trumps dev/unknown error and revalidation */
1414 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1415 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1416 ehc->i.action &= ~ATA_EH_REVALIDATE;
1419 /* accumulate error info */
1420 ehc->i.dev = qc->dev;
1421 all_err_mask |= qc->err_mask;
1422 if (qc->flags & ATA_QCFLAG_IO)
1426 /* enforce default EH actions */
1427 if (ap->pflags & ATA_PFLAG_FROZEN ||
1428 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
1429 ehc->i.action |= ATA_EH_SOFTRESET;
1430 else if (all_err_mask)
1431 ehc->i.action |= ATA_EH_REVALIDATE;
1433 /* if we have offending qcs and the associated failed device */
1436 ehc->i.action |= ata_eh_speed_down(ehc->i.dev, is_io,
1439 /* perform per-dev EH action only on the offending device */
1440 ehc->i.dev_action[ehc->i.dev->devno] |=
1441 ehc->i.action & ATA_EH_PERDEV_MASK;
1442 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
1449 * ata_eh_report - report error handling to user
1450 * @ap: ATA port EH is going on
1452 * Report EH to user.
1457 static void ata_eh_report(struct ata_port *ap)
1459 struct ata_eh_context *ehc = &ap->eh_context;
1460 const char *frozen, *desc;
1461 int tag, nr_failed = 0;
1464 if (ehc->i.desc[0] != '\0')
1467 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1468 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1470 if (!(qc->flags & ATA_QCFLAG_FAILED))
1472 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1478 if (!nr_failed && !ehc->i.err_mask)
1482 if (ap->pflags & ATA_PFLAG_FROZEN)
1486 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1487 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1488 ehc->i.err_mask, ap->sactive, ehc->i.serror,
1489 ehc->i.action, frozen);
1491 ata_dev_printk(ehc->i.dev, KERN_ERR, "(%s)\n", desc);
1493 ata_port_printk(ap, KERN_ERR, "exception Emask 0x%x "
1494 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1495 ehc->i.err_mask, ap->sactive, ehc->i.serror,
1496 ehc->i.action, frozen);
1498 ata_port_printk(ap, KERN_ERR, "(%s)\n", desc);
1501 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1502 static const char *dma_str[] = {
1503 [DMA_BIDIRECTIONAL] = "bidi",
1504 [DMA_TO_DEVICE] = "out",
1505 [DMA_FROM_DEVICE] = "in",
1508 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1509 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
1511 if (!(qc->flags & ATA_QCFLAG_FAILED) || !qc->err_mask)
1514 ata_dev_printk(qc->dev, KERN_ERR,
1515 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
1516 "tag %d cdb 0x%x data %u %s\n "
1517 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
1518 "Emask 0x%x (%s)\n",
1519 cmd->command, cmd->feature, cmd->nsect,
1520 cmd->lbal, cmd->lbam, cmd->lbah,
1521 cmd->hob_feature, cmd->hob_nsect,
1522 cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
1523 cmd->device, qc->tag, qc->cdb[0], qc->nbytes,
1524 dma_str[qc->dma_dir],
1525 res->command, res->feature, res->nsect,
1526 res->lbal, res->lbam, res->lbah,
1527 res->hob_feature, res->hob_nsect,
1528 res->hob_lbal, res->hob_lbam, res->hob_lbah,
1529 res->device, qc->err_mask, ata_err_string(qc->err_mask));
1533 static int ata_do_reset(struct ata_port *ap, ata_reset_fn_t reset,
1534 unsigned int *classes)
1538 for (i = 0; i < ATA_MAX_DEVICES; i++)
1539 classes[i] = ATA_DEV_UNKNOWN;
1541 rc = reset(ap, classes);
1545 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
1546 * is complete and convert all ATA_DEV_UNKNOWN to
1549 for (i = 0; i < ATA_MAX_DEVICES; i++)
1550 if (classes[i] != ATA_DEV_UNKNOWN)
1553 if (i < ATA_MAX_DEVICES)
1554 for (i = 0; i < ATA_MAX_DEVICES; i++)
1555 if (classes[i] == ATA_DEV_UNKNOWN)
1556 classes[i] = ATA_DEV_NONE;
1561 static int ata_eh_followup_srst_needed(int rc, int classify,
1562 const unsigned int *classes)
1568 if (classify && classes[0] == ATA_DEV_UNKNOWN)
1573 static int ata_eh_reset(struct ata_port *ap, int classify,
1574 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
1575 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
1577 struct ata_eh_context *ehc = &ap->eh_context;
1578 unsigned int *classes = ehc->classes;
1579 int tries = ATA_EH_RESET_TRIES;
1580 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
1581 unsigned int action;
1582 ata_reset_fn_t reset;
1583 int i, did_followup_srst, rc;
1585 /* about to reset */
1586 ata_eh_about_to_do(ap, NULL, ehc->i.action & ATA_EH_RESET_MASK);
1588 /* Determine which reset to use and record in ehc->i.action.
1589 * prereset() may examine and modify it.
1591 action = ehc->i.action;
1592 ehc->i.action &= ~ATA_EH_RESET_MASK;
1593 if (softreset && (!hardreset || (!sata_set_spd_needed(ap) &&
1594 !(action & ATA_EH_HARDRESET))))
1595 ehc->i.action |= ATA_EH_SOFTRESET;
1597 ehc->i.action |= ATA_EH_HARDRESET;
1602 if (rc == -ENOENT) {
1603 ata_port_printk(ap, KERN_DEBUG, "port disabled. ignoring.\n");
1604 ap->eh_context.i.action &= ~ATA_EH_RESET_MASK;
1606 ata_port_printk(ap, KERN_ERR,
1607 "prereset failed (errno=%d)\n", rc);
1612 /* prereset() might have modified ehc->i.action */
1613 if (ehc->i.action & ATA_EH_HARDRESET)
1615 else if (ehc->i.action & ATA_EH_SOFTRESET)
1618 /* prereset told us not to reset, bang classes and return */
1619 for (i = 0; i < ATA_MAX_DEVICES; i++)
1620 classes[i] = ATA_DEV_NONE;
1624 /* did prereset() screw up? if so, fix up to avoid oopsing */
1626 ata_port_printk(ap, KERN_ERR, "BUG: prereset() requested "
1627 "invalid reset type\n");
1635 /* shut up during boot probing */
1637 ata_port_printk(ap, KERN_INFO, "%s resetting port\n",
1638 reset == softreset ? "soft" : "hard");
1640 /* mark that this EH session started with reset */
1641 ehc->i.flags |= ATA_EHI_DID_RESET;
1643 rc = ata_do_reset(ap, reset, classes);
1645 did_followup_srst = 0;
1646 if (reset == hardreset &&
1647 ata_eh_followup_srst_needed(rc, classify, classes)) {
1648 /* okay, let's do follow-up softreset */
1649 did_followup_srst = 1;
1653 ata_port_printk(ap, KERN_ERR,
1654 "follow-up softreset required "
1655 "but no softreset avaliable\n");
1659 ata_eh_about_to_do(ap, NULL, ATA_EH_RESET_MASK);
1660 rc = ata_do_reset(ap, reset, classes);
1662 if (rc == 0 && classify &&
1663 classes[0] == ATA_DEV_UNKNOWN) {
1664 ata_port_printk(ap, KERN_ERR,
1665 "classification failed\n");
1670 if (rc && --tries) {
1673 if (reset == softreset) {
1674 if (did_followup_srst)
1675 type = "follow-up soft";
1681 ata_port_printk(ap, KERN_WARNING,
1682 "%sreset failed, retrying in 5 secs\n", type);
1685 if (reset == hardreset)
1686 sata_down_spd_limit(ap);
1693 /* After the reset, the device state is PIO 0 and the
1694 * controller state is undefined. Record the mode.
1696 for (i = 0; i < ATA_MAX_DEVICES; i++)
1697 ap->device[i].pio_mode = XFER_PIO_0;
1700 postreset(ap, classes);
1702 /* reset successful, schedule revalidation */
1703 ata_eh_done(ap, NULL, ehc->i.action & ATA_EH_RESET_MASK);
1704 ehc->i.action |= ATA_EH_REVALIDATE;
1710 static int ata_eh_revalidate_and_attach(struct ata_port *ap,
1711 struct ata_device **r_failed_dev)
1713 struct ata_eh_context *ehc = &ap->eh_context;
1714 struct ata_device *dev;
1715 unsigned long flags;
1720 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1721 unsigned int action, readid_flags = 0;
1723 dev = &ap->device[i];
1724 action = ata_eh_dev_action(dev);
1726 if (ehc->i.flags & ATA_EHI_DID_RESET)
1727 readid_flags |= ATA_READID_POSTRESET;
1729 if (action & ATA_EH_REVALIDATE && ata_dev_ready(dev)) {
1730 if (ata_port_offline(ap)) {
1735 ata_eh_about_to_do(ap, dev, ATA_EH_REVALIDATE);
1736 rc = ata_dev_revalidate(dev, readid_flags);
1740 ata_eh_done(ap, dev, ATA_EH_REVALIDATE);
1742 /* Configuration may have changed, reconfigure
1745 ehc->i.flags |= ATA_EHI_SETMODE;
1747 /* schedule the scsi_rescan_device() here */
1748 queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
1749 } else if (dev->class == ATA_DEV_UNKNOWN &&
1750 ehc->tries[dev->devno] &&
1751 ata_class_enabled(ehc->classes[dev->devno])) {
1752 dev->class = ehc->classes[dev->devno];
1754 rc = ata_dev_read_id(dev, &dev->class, readid_flags,
1757 ehc->i.flags |= ATA_EHI_PRINTINFO;
1758 rc = ata_dev_configure(dev);
1759 ehc->i.flags &= ~ATA_EHI_PRINTINFO;
1760 } else if (rc == -ENOENT) {
1761 /* IDENTIFY was issued to non-existent
1762 * device. No need to reset. Just
1763 * thaw and kill the device.
1765 ata_eh_thaw_port(ap);
1766 dev->class = ATA_DEV_UNKNOWN;
1771 dev->class = ATA_DEV_UNKNOWN;
1775 if (ata_dev_enabled(dev)) {
1776 spin_lock_irqsave(ap->lock, flags);
1777 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
1778 spin_unlock_irqrestore(ap->lock, flags);
1780 /* new device discovered, configure xfermode */
1781 ehc->i.flags |= ATA_EHI_SETMODE;
1787 *r_failed_dev = dev;
1794 * ata_eh_suspend - handle suspend EH action
1795 * @ap: target host port
1796 * @r_failed_dev: result parameter to indicate failing device
1798 * Handle suspend EH action. Disk devices are spinned down and
1799 * other types of devices are just marked suspended. Once
1800 * suspended, no EH action to the device is allowed until it is
1804 * Kernel thread context (may sleep).
1807 * 0 on success, -errno otherwise
1809 static int ata_eh_suspend(struct ata_port *ap, struct ata_device **r_failed_dev)
1811 struct ata_device *dev;
1816 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1817 unsigned long flags;
1818 unsigned int action, err_mask;
1820 dev = &ap->device[i];
1821 action = ata_eh_dev_action(dev);
1823 if (!ata_dev_enabled(dev) || !(action & ATA_EH_SUSPEND))
1826 WARN_ON(dev->flags & ATA_DFLAG_SUSPENDED);
1828 ata_eh_about_to_do(ap, dev, ATA_EH_SUSPEND);
1830 if (dev->class == ATA_DEV_ATA && !(action & ATA_EH_PM_FREEZE)) {
1832 rc = ata_flush_cache(dev);
1837 err_mask = ata_do_simple_cmd(dev, ATA_CMD_STANDBYNOW1);
1839 ata_dev_printk(dev, KERN_ERR, "failed to "
1840 "spin down (err_mask=0x%x)\n",
1847 spin_lock_irqsave(ap->lock, flags);
1848 dev->flags |= ATA_DFLAG_SUSPENDED;
1849 spin_unlock_irqrestore(ap->lock, flags);
1851 ata_eh_done(ap, dev, ATA_EH_SUSPEND);
1855 *r_failed_dev = dev;
1862 * ata_eh_prep_resume - prep for resume EH action
1863 * @ap: target host port
1865 * Clear SUSPENDED in preparation for scheduled resume actions.
1866 * This allows other parts of EH to access the devices being
1870 * Kernel thread context (may sleep).
1872 static void ata_eh_prep_resume(struct ata_port *ap)
1874 struct ata_device *dev;
1875 unsigned long flags;
1880 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1881 unsigned int action;
1883 dev = &ap->device[i];
1884 action = ata_eh_dev_action(dev);
1886 if (!ata_dev_enabled(dev) || !(action & ATA_EH_RESUME))
1889 spin_lock_irqsave(ap->lock, flags);
1890 dev->flags &= ~ATA_DFLAG_SUSPENDED;
1891 spin_unlock_irqrestore(ap->lock, flags);
1898 * ata_eh_resume - handle resume EH action
1899 * @ap: target host port
1900 * @r_failed_dev: result parameter to indicate failing device
1902 * Handle resume EH action. Target devices are already reset and
1903 * revalidated. Spinning up is the only operation left.
1906 * Kernel thread context (may sleep).
1909 * 0 on success, -errno otherwise
1911 static int ata_eh_resume(struct ata_port *ap, struct ata_device **r_failed_dev)
1913 struct ata_device *dev;
1918 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1919 unsigned int action, err_mask;
1921 dev = &ap->device[i];
1922 action = ata_eh_dev_action(dev);
1924 if (!ata_dev_enabled(dev) || !(action & ATA_EH_RESUME))
1927 ata_eh_about_to_do(ap, dev, ATA_EH_RESUME);
1929 if (dev->class == ATA_DEV_ATA && !(action & ATA_EH_PM_FREEZE)) {
1930 err_mask = ata_do_simple_cmd(dev,
1931 ATA_CMD_IDLEIMMEDIATE);
1933 ata_dev_printk(dev, KERN_ERR, "failed to "
1934 "spin up (err_mask=0x%x)\n",
1941 ata_eh_done(ap, dev, ATA_EH_RESUME);
1945 *r_failed_dev = dev;
1951 static int ata_port_nr_enabled(struct ata_port *ap)
1955 for (i = 0; i < ATA_MAX_DEVICES; i++)
1956 if (ata_dev_enabled(&ap->device[i]))
1961 static int ata_port_nr_vacant(struct ata_port *ap)
1965 for (i = 0; i < ATA_MAX_DEVICES; i++)
1966 if (ap->device[i].class == ATA_DEV_UNKNOWN)
1971 static int ata_eh_skip_recovery(struct ata_port *ap)
1973 struct ata_eh_context *ehc = &ap->eh_context;
1976 /* skip if all possible devices are suspended */
1977 for (i = 0; i < ata_port_max_devices(ap); i++) {
1978 struct ata_device *dev = &ap->device[i];
1980 if (!(dev->flags & ATA_DFLAG_SUSPENDED))
1984 if (i == ata_port_max_devices(ap))
1987 /* thaw frozen port, resume link and recover failed devices */
1988 if ((ap->pflags & ATA_PFLAG_FROZEN) ||
1989 (ehc->i.flags & ATA_EHI_RESUME_LINK) || ata_port_nr_enabled(ap))
1992 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
1993 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1994 struct ata_device *dev = &ap->device[i];
1996 if (dev->class == ATA_DEV_UNKNOWN &&
1997 ehc->classes[dev->devno] != ATA_DEV_NONE)
2005 * ata_eh_recover - recover host port after error
2006 * @ap: host port to recover
2007 * @prereset: prereset method (can be NULL)
2008 * @softreset: softreset method (can be NULL)
2009 * @hardreset: hardreset method (can be NULL)
2010 * @postreset: postreset method (can be NULL)
2012 * This is the alpha and omega, eum and yang, heart and soul of
2013 * libata exception handling. On entry, actions required to
2014 * recover the port and hotplug requests are recorded in
2015 * eh_context. This function executes all the operations with
2016 * appropriate retrials and fallbacks to resurrect failed
2017 * devices, detach goners and greet newcomers.
2020 * Kernel thread context (may sleep).
2023 * 0 on success, -errno on failure.
2025 static int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
2026 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2027 ata_postreset_fn_t postreset)
2029 struct ata_eh_context *ehc = &ap->eh_context;
2030 struct ata_device *dev;
2035 /* prep for recovery */
2036 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2037 dev = &ap->device[i];
2039 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2041 /* collect port action mask recorded in dev actions */
2042 ehc->i.action |= ehc->i.dev_action[i] & ~ATA_EH_PERDEV_MASK;
2043 ehc->i.dev_action[i] &= ATA_EH_PERDEV_MASK;
2045 /* process hotplug request */
2046 if (dev->flags & ATA_DFLAG_DETACH)
2047 ata_eh_detach_dev(dev);
2049 if (!ata_dev_enabled(dev) &&
2050 ((ehc->i.probe_mask & (1 << dev->devno)) &&
2051 !(ehc->did_probe_mask & (1 << dev->devno)))) {
2052 ata_eh_detach_dev(dev);
2054 ehc->did_probe_mask |= (1 << dev->devno);
2055 ehc->i.action |= ATA_EH_SOFTRESET;
2062 /* if UNLOADING, finish immediately */
2063 if (ap->pflags & ATA_PFLAG_UNLOADING)
2066 /* prep for resume */
2067 ata_eh_prep_resume(ap);
2069 /* skip EH if possible. */
2070 if (ata_eh_skip_recovery(ap))
2073 for (i = 0; i < ATA_MAX_DEVICES; i++)
2074 ehc->classes[i] = ATA_DEV_UNKNOWN;
2077 if (ehc->i.action & ATA_EH_RESET_MASK) {
2078 ata_eh_freeze_port(ap);
2080 rc = ata_eh_reset(ap, ata_port_nr_vacant(ap), prereset,
2081 softreset, hardreset, postreset);
2083 ata_port_printk(ap, KERN_ERR,
2084 "reset failed, giving up\n");
2088 ata_eh_thaw_port(ap);
2091 /* revalidate existing devices and attach new ones */
2092 rc = ata_eh_revalidate_and_attach(ap, &dev);
2096 /* resume devices */
2097 rc = ata_eh_resume(ap, &dev);
2101 /* configure transfer mode if necessary */
2102 if (ehc->i.flags & ATA_EHI_SETMODE) {
2103 rc = ata_set_mode(ap, &dev);
2106 ehc->i.flags &= ~ATA_EHI_SETMODE;
2109 /* suspend devices */
2110 rc = ata_eh_suspend(ap, &dev);
2117 ehc->tries[dev->devno]--;
2121 /* eeek, something went very wrong, give up */
2122 ehc->tries[dev->devno] = 0;
2126 /* device missing or wrong IDENTIFY data, schedule probing */
2127 ehc->i.probe_mask |= (1 << dev->devno);
2128 /* give it just one more chance */
2129 ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
2131 if (ehc->tries[dev->devno] == 1) {
2132 /* This is the last chance, better to slow
2133 * down than lose it.
2135 sata_down_spd_limit(ap);
2136 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2140 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2141 /* disable device if it has used up all its chances */
2142 ata_dev_disable(dev);
2144 /* detach if offline */
2145 if (ata_port_offline(ap))
2146 ata_eh_detach_dev(dev);
2148 /* probe if requested */
2149 if ((ehc->i.probe_mask & (1 << dev->devno)) &&
2150 !(ehc->did_probe_mask & (1 << dev->devno))) {
2151 ata_eh_detach_dev(dev);
2154 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2155 ehc->did_probe_mask |= (1 << dev->devno);
2156 ehc->i.action |= ATA_EH_SOFTRESET;
2159 /* soft didn't work? be haaaaard */
2160 if (ehc->i.flags & ATA_EHI_DID_RESET)
2161 ehc->i.action |= ATA_EH_HARDRESET;
2163 ehc->i.action |= ATA_EH_SOFTRESET;
2166 if (ata_port_nr_enabled(ap)) {
2167 ata_port_printk(ap, KERN_WARNING, "failed to recover some "
2168 "devices, retrying in 5 secs\n");
2171 /* no device left, repeat fast */
2179 for (i = 0; i < ATA_MAX_DEVICES; i++)
2180 ata_dev_disable(&ap->device[i]);
2183 DPRINTK("EXIT, rc=%d\n", rc);
2188 * ata_eh_finish - finish up EH
2189 * @ap: host port to finish EH for
2191 * Recovery is complete. Clean up EH states and retry or finish
2197 static void ata_eh_finish(struct ata_port *ap)
2201 /* retry or finish qcs */
2202 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2203 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2205 if (!(qc->flags & ATA_QCFLAG_FAILED))
2209 /* FIXME: Once EH migration is complete,
2210 * generate sense data in this function,
2211 * considering both err_mask and tf.
2213 if (qc->err_mask & AC_ERR_INVALID)
2214 ata_eh_qc_complete(qc);
2216 ata_eh_qc_retry(qc);
2218 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2219 ata_eh_qc_complete(qc);
2221 /* feed zero TF to sense generation */
2222 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2223 ata_eh_qc_retry(qc);
2230 * ata_do_eh - do standard error handling
2231 * @ap: host port to handle error for
2232 * @prereset: prereset method (can be NULL)
2233 * @softreset: softreset method (can be NULL)
2234 * @hardreset: hardreset method (can be NULL)
2235 * @postreset: postreset method (can be NULL)
2237 * Perform standard error handling sequence.
2240 * Kernel thread context (may sleep).
2242 void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2243 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2244 ata_postreset_fn_t postreset)
2248 ata_eh_recover(ap, prereset, softreset, hardreset, postreset);
2253 * ata_eh_handle_port_suspend - perform port suspend operation
2254 * @ap: port to suspend
2259 * Kernel thread context (may sleep).
2261 static void ata_eh_handle_port_suspend(struct ata_port *ap)
2263 unsigned long flags;
2266 /* are we suspending? */
2267 spin_lock_irqsave(ap->lock, flags);
2268 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2269 ap->pm_mesg.event == PM_EVENT_ON) {
2270 spin_unlock_irqrestore(ap->lock, flags);
2273 spin_unlock_irqrestore(ap->lock, flags);
2275 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
2278 ata_eh_freeze_port(ap);
2280 if (ap->ops->port_suspend)
2281 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
2284 spin_lock_irqsave(ap->lock, flags);
2286 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
2288 ap->pflags |= ATA_PFLAG_SUSPENDED;
2290 ata_port_schedule_eh(ap);
2292 if (ap->pm_result) {
2293 *ap->pm_result = rc;
2294 ap->pm_result = NULL;
2297 spin_unlock_irqrestore(ap->lock, flags);
2303 * ata_eh_handle_port_resume - perform port resume operation
2304 * @ap: port to resume
2308 * This function also waits upto one second until all devices
2309 * hanging off this port requests resume EH action. This is to
2310 * prevent invoking EH and thus reset multiple times on resume.
2312 * On DPM resume, where some of devices might not be resumed
2313 * together, this may delay port resume upto one second, but such
2314 * DPM resumes are rare and 1 sec delay isn't too bad.
2317 * Kernel thread context (may sleep).
2319 static void ata_eh_handle_port_resume(struct ata_port *ap)
2321 unsigned long timeout;
2322 unsigned long flags;
2325 /* are we resuming? */
2326 spin_lock_irqsave(ap->lock, flags);
2327 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2328 ap->pm_mesg.event != PM_EVENT_ON) {
2329 spin_unlock_irqrestore(ap->lock, flags);
2332 spin_unlock_irqrestore(ap->lock, flags);
2335 if (!(ap->pflags & ATA_PFLAG_SUSPENDED))
2338 if (ap->ops->port_resume)
2339 rc = ap->ops->port_resume(ap);
2341 /* give devices time to request EH */
2342 timeout = jiffies + HZ; /* 1s max */
2344 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2345 struct ata_device *dev = &ap->device[i];
2346 unsigned int action = ata_eh_dev_action(dev);
2348 if ((dev->flags & ATA_DFLAG_SUSPENDED) &&
2349 !(action & ATA_EH_RESUME))
2353 if (i == ATA_MAX_DEVICES || time_after(jiffies, timeout))
2359 spin_lock_irqsave(ap->lock, flags);
2360 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
2361 if (ap->pm_result) {
2362 *ap->pm_result = rc;
2363 ap->pm_result = NULL;
2365 spin_unlock_irqrestore(ap->lock, flags);