2 * libata-eh.c - libata error handling
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
8 * Copyright 2006 Tejun Heo <htejun@gmail.com>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; see the file COPYING. If not, write to
23 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
35 #include <linux/config.h>
36 #include <linux/kernel.h>
37 #include <scsi/scsi.h>
38 #include <scsi/scsi_host.h>
39 #include <scsi/scsi_eh.h>
40 #include <scsi/scsi_device.h>
41 #include <scsi/scsi_cmnd.h>
42 #include "scsi_transport_api.h"
44 #include <linux/libata.h>
48 static void __ata_port_freeze(struct ata_port *ap);
49 static void ata_eh_finish(struct ata_port *ap);
51 static void ata_ering_record(struct ata_ering *ering, int is_io,
52 unsigned int err_mask)
54 struct ata_ering_entry *ent;
59 ering->cursor %= ATA_ERING_SIZE;
61 ent = &ering->ring[ering->cursor];
63 ent->err_mask = err_mask;
64 ent->timestamp = get_jiffies_64();
67 static struct ata_ering_entry * ata_ering_top(struct ata_ering *ering)
69 struct ata_ering_entry *ent = &ering->ring[ering->cursor];
75 static int ata_ering_map(struct ata_ering *ering,
76 int (*map_fn)(struct ata_ering_entry *, void *),
80 struct ata_ering_entry *ent;
84 ent = &ering->ring[idx];
87 rc = map_fn(ent, arg);
90 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
91 } while (idx != ering->cursor);
96 static unsigned int ata_eh_dev_action(struct ata_device *dev)
98 struct ata_eh_context *ehc = &dev->ap->eh_context;
100 return ehc->i.action | ehc->i.dev_action[dev->devno];
103 static void ata_eh_clear_action(struct ata_device *dev,
104 struct ata_eh_info *ehi, unsigned int action)
109 ehi->action &= ~action;
110 for (i = 0; i < ATA_MAX_DEVICES; i++)
111 ehi->dev_action[i] &= ~action;
113 /* doesn't make sense for port-wide EH actions */
114 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
116 /* break ehi->action into ehi->dev_action */
117 if (ehi->action & action) {
118 for (i = 0; i < ATA_MAX_DEVICES; i++)
119 ehi->dev_action[i] |= ehi->action & action;
120 ehi->action &= ~action;
123 /* turn off the specified per-dev action */
124 ehi->dev_action[dev->devno] &= ~action;
129 * ata_scsi_timed_out - SCSI layer time out callback
130 * @cmd: timed out SCSI command
132 * Handles SCSI layer timeout. We race with normal completion of
133 * the qc for @cmd. If the qc is already gone, we lose and let
134 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
135 * timed out and EH should be invoked. Prevent ata_qc_complete()
136 * from finishing it by setting EH_SCHEDULED and return
139 * TODO: kill this function once old EH is gone.
142 * Called from timer context
145 * EH_HANDLED or EH_NOT_HANDLED
147 enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
149 struct Scsi_Host *host = cmd->device->host;
150 struct ata_port *ap = ata_shost_to_port(host);
152 struct ata_queued_cmd *qc;
153 enum scsi_eh_timer_return ret;
157 if (ap->ops->error_handler) {
158 ret = EH_NOT_HANDLED;
163 spin_lock_irqsave(ap->lock, flags);
164 qc = ata_qc_from_tag(ap, ap->active_tag);
166 WARN_ON(qc->scsicmd != cmd);
167 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
168 qc->err_mask |= AC_ERR_TIMEOUT;
169 ret = EH_NOT_HANDLED;
171 spin_unlock_irqrestore(ap->lock, flags);
174 DPRINTK("EXIT, ret=%d\n", ret);
179 * ata_scsi_error - SCSI layer error handler callback
180 * @host: SCSI host on which error occurred
182 * Handles SCSI-layer-thrown error events.
185 * Inherited from SCSI layer (none, can sleep)
190 void ata_scsi_error(struct Scsi_Host *host)
192 struct ata_port *ap = ata_shost_to_port(host);
193 int i, repeat_cnt = ATA_EH_MAX_REPEAT;
198 /* synchronize with port task */
199 ata_port_flush_task(ap);
201 /* synchronize with host_set lock and sort out timeouts */
203 /* For new EH, all qcs are finished in one of three ways -
204 * normal completion, error completion, and SCSI timeout.
205 * Both cmpletions can race against SCSI timeout. When normal
206 * completion wins, the qc never reaches EH. When error
207 * completion wins, the qc has ATA_QCFLAG_FAILED set.
209 * When SCSI timeout wins, things are a bit more complex.
210 * Normal or error completion can occur after the timeout but
211 * before this point. In such cases, both types of
212 * completions are honored. A scmd is determined to have
213 * timed out iff its associated qc is active and not failed.
215 if (ap->ops->error_handler) {
216 struct scsi_cmnd *scmd, *tmp;
219 spin_lock_irqsave(ap->lock, flags);
221 list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
222 struct ata_queued_cmd *qc;
224 for (i = 0; i < ATA_MAX_QUEUE; i++) {
225 qc = __ata_qc_from_tag(ap, i);
226 if (qc->flags & ATA_QCFLAG_ACTIVE &&
231 if (i < ATA_MAX_QUEUE) {
232 /* the scmd has an associated qc */
233 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
234 /* which hasn't failed yet, timeout */
235 qc->err_mask |= AC_ERR_TIMEOUT;
236 qc->flags |= ATA_QCFLAG_FAILED;
240 /* Normal completion occurred after
241 * SCSI timeout but before this point.
242 * Successfully complete it.
244 scmd->retries = scmd->allowed;
245 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
249 /* If we have timed out qcs. They belong to EH from
250 * this point but the state of the controller is
251 * unknown. Freeze the port to make sure the IRQ
252 * handler doesn't diddle with those qcs. This must
253 * be done atomically w.r.t. setting QCFLAG_FAILED.
256 __ata_port_freeze(ap);
258 spin_unlock_irqrestore(ap->lock, flags);
260 spin_unlock_wait(ap->lock);
263 /* invoke error handler */
264 if (ap->ops->error_handler) {
265 /* fetch & clear EH info */
266 spin_lock_irqsave(ap->lock, flags);
268 memset(&ap->eh_context, 0, sizeof(ap->eh_context));
269 ap->eh_context.i = ap->eh_info;
270 memset(&ap->eh_info, 0, sizeof(ap->eh_info));
272 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
273 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
275 spin_unlock_irqrestore(ap->lock, flags);
277 /* invoke EH. if unloading, just finish failed qcs */
278 if (!(ap->pflags & ATA_PFLAG_UNLOADING))
279 ap->ops->error_handler(ap);
283 /* Exception might have happend after ->error_handler
284 * recovered the port but before this point. Repeat
287 spin_lock_irqsave(ap->lock, flags);
289 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
291 ata_port_printk(ap, KERN_INFO,
292 "EH pending after completion, "
293 "repeating EH (cnt=%d)\n", repeat_cnt);
294 spin_unlock_irqrestore(ap->lock, flags);
297 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
298 "tries, giving up\n", ATA_EH_MAX_REPEAT);
301 /* this run is complete, make sure EH info is clear */
302 memset(&ap->eh_info, 0, sizeof(ap->eh_info));
304 /* Clear host_eh_scheduled while holding ap->lock such
305 * that if exception occurs after this point but
306 * before EH completion, SCSI midlayer will
309 host->host_eh_scheduled = 0;
311 spin_unlock_irqrestore(ap->lock, flags);
313 WARN_ON(ata_qc_from_tag(ap, ap->active_tag) == NULL);
314 ap->ops->eng_timeout(ap);
317 /* finish or retry handled scmd's and clean up */
318 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
320 scsi_eh_flush_done_q(&ap->eh_done_q);
323 spin_lock_irqsave(ap->lock, flags);
325 if (ap->pflags & ATA_PFLAG_LOADING)
326 ap->pflags &= ~ATA_PFLAG_LOADING;
327 else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
328 queue_work(ata_aux_wq, &ap->hotplug_task);
330 if (ap->pflags & ATA_PFLAG_RECOVERED)
331 ata_port_printk(ap, KERN_INFO, "EH complete\n");
333 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
335 /* tell wait_eh that we're done */
336 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
337 wake_up_all(&ap->eh_wait_q);
339 spin_unlock_irqrestore(ap->lock, flags);
345 * ata_port_wait_eh - Wait for the currently pending EH to complete
346 * @ap: Port to wait EH for
348 * Wait until the currently pending EH is complete.
351 * Kernel thread context (may sleep).
353 void ata_port_wait_eh(struct ata_port *ap)
359 spin_lock_irqsave(ap->lock, flags);
361 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
362 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
363 spin_unlock_irqrestore(ap->lock, flags);
365 spin_lock_irqsave(ap->lock, flags);
367 finish_wait(&ap->eh_wait_q, &wait);
369 spin_unlock_irqrestore(ap->lock, flags);
371 /* make sure SCSI EH is complete */
372 if (scsi_host_in_recovery(ap->host)) {
379 * ata_qc_timeout - Handle timeout of queued command
380 * @qc: Command that timed out
382 * Some part of the kernel (currently, only the SCSI layer)
383 * has noticed that the active command on port @ap has not
384 * completed after a specified length of time. Handle this
385 * condition by disabling DMA (if necessary) and completing
386 * transactions, with error if necessary.
388 * This also handles the case of the "lost interrupt", where
389 * for some reason (possibly hardware bug, possibly driver bug)
390 * an interrupt was not delivered to the driver, even though the
391 * transaction completed successfully.
393 * TODO: kill this function once old EH is gone.
396 * Inherited from SCSI layer (none, can sleep)
398 static void ata_qc_timeout(struct ata_queued_cmd *qc)
400 struct ata_port *ap = qc->ap;
401 u8 host_stat = 0, drv_stat;
406 ap->hsm_task_state = HSM_ST_IDLE;
408 spin_lock_irqsave(ap->lock, flags);
410 switch (qc->tf.protocol) {
413 case ATA_PROT_ATAPI_DMA:
414 host_stat = ap->ops->bmdma_status(ap);
416 /* before we do anything else, clear DMA-Start bit */
417 ap->ops->bmdma_stop(qc);
423 drv_stat = ata_chk_status(ap);
425 /* ack bmdma irq events */
426 ap->ops->irq_clear(ap);
428 ata_dev_printk(qc->dev, KERN_ERR, "command 0x%x timeout, "
429 "stat 0x%x host_stat 0x%x\n",
430 qc->tf.command, drv_stat, host_stat);
432 /* complete taskfile transaction */
433 qc->err_mask |= AC_ERR_TIMEOUT;
437 spin_unlock_irqrestore(ap->lock, flags);
439 ata_eh_qc_complete(qc);
445 * ata_eng_timeout - Handle timeout of queued command
446 * @ap: Port on which timed-out command is active
448 * Some part of the kernel (currently, only the SCSI layer)
449 * has noticed that the active command on port @ap has not
450 * completed after a specified length of time. Handle this
451 * condition by disabling DMA (if necessary) and completing
452 * transactions, with error if necessary.
454 * This also handles the case of the "lost interrupt", where
455 * for some reason (possibly hardware bug, possibly driver bug)
456 * an interrupt was not delivered to the driver, even though the
457 * transaction completed successfully.
459 * TODO: kill this function once old EH is gone.
462 * Inherited from SCSI layer (none, can sleep)
464 void ata_eng_timeout(struct ata_port *ap)
468 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
474 * ata_qc_schedule_eh - schedule qc for error handling
475 * @qc: command to schedule error handling for
477 * Schedule error handling for @qc. EH will kick in as soon as
478 * other commands are drained.
481 * spin_lock_irqsave(host_set lock)
483 void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
485 struct ata_port *ap = qc->ap;
487 WARN_ON(!ap->ops->error_handler);
489 qc->flags |= ATA_QCFLAG_FAILED;
490 qc->ap->pflags |= ATA_PFLAG_EH_PENDING;
492 /* The following will fail if timeout has already expired.
493 * ata_scsi_error() takes care of such scmds on EH entry.
494 * Note that ATA_QCFLAG_FAILED is unconditionally set after
495 * this function completes.
497 scsi_req_abort_cmd(qc->scsicmd);
501 * ata_port_schedule_eh - schedule error handling without a qc
502 * @ap: ATA port to schedule EH for
504 * Schedule error handling for @ap. EH will kick in as soon as
505 * all commands are drained.
508 * spin_lock_irqsave(host_set lock)
510 void ata_port_schedule_eh(struct ata_port *ap)
512 WARN_ON(!ap->ops->error_handler);
514 ap->pflags |= ATA_PFLAG_EH_PENDING;
515 scsi_schedule_eh(ap->host);
517 DPRINTK("port EH scheduled\n");
521 * ata_port_abort - abort all qc's on the port
522 * @ap: ATA port to abort qc's for
524 * Abort all active qc's of @ap and schedule EH.
527 * spin_lock_irqsave(host_set lock)
530 * Number of aborted qc's.
532 int ata_port_abort(struct ata_port *ap)
534 int tag, nr_aborted = 0;
536 WARN_ON(!ap->ops->error_handler);
538 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
539 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
542 qc->flags |= ATA_QCFLAG_FAILED;
549 ata_port_schedule_eh(ap);
555 * __ata_port_freeze - freeze port
556 * @ap: ATA port to freeze
558 * This function is called when HSM violation or some other
559 * condition disrupts normal operation of the port. Frozen port
560 * is not allowed to perform any operation until the port is
561 * thawed, which usually follows a successful reset.
563 * ap->ops->freeze() callback can be used for freezing the port
564 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
565 * port cannot be frozen hardware-wise, the interrupt handler
566 * must ack and clear interrupts unconditionally while the port
570 * spin_lock_irqsave(host_set lock)
572 static void __ata_port_freeze(struct ata_port *ap)
574 WARN_ON(!ap->ops->error_handler);
579 ap->pflags |= ATA_PFLAG_FROZEN;
581 DPRINTK("ata%u port frozen\n", ap->id);
585 * ata_port_freeze - abort & freeze port
586 * @ap: ATA port to freeze
588 * Abort and freeze @ap.
591 * spin_lock_irqsave(host_set lock)
594 * Number of aborted commands.
596 int ata_port_freeze(struct ata_port *ap)
600 WARN_ON(!ap->ops->error_handler);
602 nr_aborted = ata_port_abort(ap);
603 __ata_port_freeze(ap);
609 * ata_eh_freeze_port - EH helper to freeze port
610 * @ap: ATA port to freeze
617 void ata_eh_freeze_port(struct ata_port *ap)
621 if (!ap->ops->error_handler)
624 spin_lock_irqsave(ap->lock, flags);
625 __ata_port_freeze(ap);
626 spin_unlock_irqrestore(ap->lock, flags);
630 * ata_port_thaw_port - EH helper to thaw port
631 * @ap: ATA port to thaw
633 * Thaw frozen port @ap.
638 void ata_eh_thaw_port(struct ata_port *ap)
642 if (!ap->ops->error_handler)
645 spin_lock_irqsave(ap->lock, flags);
647 ap->pflags &= ~ATA_PFLAG_FROZEN;
652 spin_unlock_irqrestore(ap->lock, flags);
654 DPRINTK("ata%u port thawed\n", ap->id);
657 static void ata_eh_scsidone(struct scsi_cmnd *scmd)
662 static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
664 struct ata_port *ap = qc->ap;
665 struct scsi_cmnd *scmd = qc->scsicmd;
668 spin_lock_irqsave(ap->lock, flags);
669 qc->scsidone = ata_eh_scsidone;
670 __ata_qc_complete(qc);
671 WARN_ON(ata_tag_valid(qc->tag));
672 spin_unlock_irqrestore(ap->lock, flags);
674 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
678 * ata_eh_qc_complete - Complete an active ATA command from EH
679 * @qc: Command to complete
681 * Indicate to the mid and upper layers that an ATA command has
682 * completed. To be used from EH.
684 void ata_eh_qc_complete(struct ata_queued_cmd *qc)
686 struct scsi_cmnd *scmd = qc->scsicmd;
687 scmd->retries = scmd->allowed;
688 __ata_eh_qc_complete(qc);
692 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
693 * @qc: Command to retry
695 * Indicate to the mid and upper layers that an ATA command
696 * should be retried. To be used from EH.
698 * SCSI midlayer limits the number of retries to scmd->allowed.
699 * scmd->retries is decremented for commands which get retried
700 * due to unrelated failures (qc->err_mask is zero).
702 void ata_eh_qc_retry(struct ata_queued_cmd *qc)
704 struct scsi_cmnd *scmd = qc->scsicmd;
705 if (!qc->err_mask && scmd->retries)
707 __ata_eh_qc_complete(qc);
711 * ata_eh_detach_dev - detach ATA device
712 * @dev: ATA device to detach
719 static void ata_eh_detach_dev(struct ata_device *dev)
721 struct ata_port *ap = dev->ap;
724 ata_dev_disable(dev);
726 spin_lock_irqsave(ap->lock, flags);
728 dev->flags &= ~ATA_DFLAG_DETACH;
730 if (ata_scsi_offline_dev(dev)) {
731 dev->flags |= ATA_DFLAG_DETACHED;
732 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
735 /* clear per-dev EH actions */
736 ata_eh_clear_action(dev, &ap->eh_info, ATA_EH_PERDEV_MASK);
737 ata_eh_clear_action(dev, &ap->eh_context.i, ATA_EH_PERDEV_MASK);
739 spin_unlock_irqrestore(ap->lock, flags);
743 * ata_eh_about_to_do - about to perform eh_action
744 * @ap: target ATA port
745 * @dev: target ATA dev for per-dev action (can be NULL)
746 * @action: action about to be performed
748 * Called just before performing EH actions to clear related bits
749 * in @ap->eh_info such that eh actions are not unnecessarily
755 static void ata_eh_about_to_do(struct ata_port *ap, struct ata_device *dev,
760 spin_lock_irqsave(ap->lock, flags);
762 ata_eh_clear_action(dev, &ap->eh_info, action);
764 if (!(ap->eh_context.i.flags & ATA_EHI_QUIET))
765 ap->pflags |= ATA_PFLAG_RECOVERED;
767 spin_unlock_irqrestore(ap->lock, flags);
771 * ata_eh_done - EH action complete
772 * @ap: target ATA port
773 * @dev: target ATA dev for per-dev action (can be NULL)
774 * @action: action just completed
776 * Called right after performing EH actions to clear related bits
777 * in @ap->eh_context.
782 static void ata_eh_done(struct ata_port *ap, struct ata_device *dev,
785 ata_eh_clear_action(dev, &ap->eh_context.i, action);
789 * ata_err_string - convert err_mask to descriptive string
790 * @err_mask: error mask to convert to string
792 * Convert @err_mask to descriptive string. Errors are
793 * prioritized according to severity and only the most severe
800 * Descriptive string for @err_mask
802 static const char * ata_err_string(unsigned int err_mask)
804 if (err_mask & AC_ERR_HOST_BUS)
805 return "host bus error";
806 if (err_mask & AC_ERR_ATA_BUS)
807 return "ATA bus error";
808 if (err_mask & AC_ERR_TIMEOUT)
810 if (err_mask & AC_ERR_HSM)
811 return "HSM violation";
812 if (err_mask & AC_ERR_SYSTEM)
813 return "internal error";
814 if (err_mask & AC_ERR_MEDIA)
815 return "media error";
816 if (err_mask & AC_ERR_INVALID)
817 return "invalid argument";
818 if (err_mask & AC_ERR_DEV)
819 return "device error";
820 return "unknown error";
824 * ata_read_log_page - read a specific log page
825 * @dev: target device
826 * @page: page to read
827 * @buf: buffer to store read page
828 * @sectors: number of sectors to read
830 * Read log page using READ_LOG_EXT command.
833 * Kernel thread context (may sleep).
836 * 0 on success, AC_ERR_* mask otherwise.
838 static unsigned int ata_read_log_page(struct ata_device *dev,
839 u8 page, void *buf, unsigned int sectors)
841 struct ata_taskfile tf;
842 unsigned int err_mask;
844 DPRINTK("read log page - page %d\n", page);
846 ata_tf_init(dev, &tf);
847 tf.command = ATA_CMD_READ_LOG_EXT;
850 tf.hob_nsect = sectors >> 8;
851 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
852 tf.protocol = ATA_PROT_PIO;
854 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
855 buf, sectors * ATA_SECT_SIZE);
857 DPRINTK("EXIT, err_mask=%x\n", err_mask);
862 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
863 * @dev: Device to read log page 10h from
864 * @tag: Resulting tag of the failed command
865 * @tf: Resulting taskfile registers of the failed command
867 * Read log page 10h to obtain NCQ error details and clear error
871 * Kernel thread context (may sleep).
874 * 0 on success, -errno otherwise.
876 static int ata_eh_read_log_10h(struct ata_device *dev,
877 int *tag, struct ata_taskfile *tf)
879 u8 *buf = dev->ap->sector_buf;
880 unsigned int err_mask;
884 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
889 for (i = 0; i < ATA_SECT_SIZE; i++)
892 ata_dev_printk(dev, KERN_WARNING,
893 "invalid checksum 0x%x on log page 10h\n", csum);
898 *tag = buf[0] & 0x1f;
900 tf->command = buf[2];
901 tf->feature = buf[3];
906 tf->hob_lbal = buf[8];
907 tf->hob_lbam = buf[9];
908 tf->hob_lbah = buf[10];
910 tf->hob_nsect = buf[13];
916 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
917 * @dev: device to perform REQUEST_SENSE to
918 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
920 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
921 * SENSE. This function is EH helper.
924 * Kernel thread context (may sleep).
927 * 0 on success, AC_ERR_* mask on failure
929 static unsigned int atapi_eh_request_sense(struct ata_device *dev,
930 unsigned char *sense_buf)
932 struct ata_port *ap = dev->ap;
933 struct ata_taskfile tf;
934 u8 cdb[ATAPI_CDB_LEN];
936 DPRINTK("ATAPI request sense\n");
938 ata_tf_init(dev, &tf);
940 /* FIXME: is this needed? */
941 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
943 /* XXX: why tf_read here? */
944 ap->ops->tf_read(ap, &tf);
946 /* fill these in, for the case where they are -not- overwritten */
948 sense_buf[2] = tf.feature >> 4;
950 memset(cdb, 0, ATAPI_CDB_LEN);
951 cdb[0] = REQUEST_SENSE;
952 cdb[4] = SCSI_SENSE_BUFFERSIZE;
954 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
955 tf.command = ATA_CMD_PACKET;
957 /* is it pointless to prefer PIO for "safety reasons"? */
958 if (ap->flags & ATA_FLAG_PIO_DMA) {
959 tf.protocol = ATA_PROT_ATAPI_DMA;
960 tf.feature |= ATAPI_PKT_DMA;
962 tf.protocol = ATA_PROT_ATAPI;
963 tf.lbam = (8 * 1024) & 0xff;
964 tf.lbah = (8 * 1024) >> 8;
967 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
968 sense_buf, SCSI_SENSE_BUFFERSIZE);
972 * ata_eh_analyze_serror - analyze SError for a failed port
973 * @ap: ATA port to analyze SError for
975 * Analyze SError if available and further determine cause of
981 static void ata_eh_analyze_serror(struct ata_port *ap)
983 struct ata_eh_context *ehc = &ap->eh_context;
984 u32 serror = ehc->i.serror;
985 unsigned int err_mask = 0, action = 0;
987 if (serror & SERR_PERSISTENT) {
988 err_mask |= AC_ERR_ATA_BUS;
989 action |= ATA_EH_HARDRESET;
992 (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
993 err_mask |= AC_ERR_ATA_BUS;
994 action |= ATA_EH_SOFTRESET;
996 if (serror & SERR_PROTOCOL) {
997 err_mask |= AC_ERR_HSM;
998 action |= ATA_EH_SOFTRESET;
1000 if (serror & SERR_INTERNAL) {
1001 err_mask |= AC_ERR_SYSTEM;
1002 action |= ATA_EH_SOFTRESET;
1004 if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG))
1005 ata_ehi_hotplugged(&ehc->i);
1007 ehc->i.err_mask |= err_mask;
1008 ehc->i.action |= action;
1012 * ata_eh_analyze_ncq_error - analyze NCQ error
1013 * @ap: ATA port to analyze NCQ error for
1015 * Read log page 10h, determine the offending qc and acquire
1016 * error status TF. For NCQ device errors, all LLDDs have to do
1017 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1021 * Kernel thread context (may sleep).
1023 static void ata_eh_analyze_ncq_error(struct ata_port *ap)
1025 struct ata_eh_context *ehc = &ap->eh_context;
1026 struct ata_device *dev = ap->device;
1027 struct ata_queued_cmd *qc;
1028 struct ata_taskfile tf;
1031 /* if frozen, we can't do much */
1032 if (ap->pflags & ATA_PFLAG_FROZEN)
1035 /* is it NCQ device error? */
1036 if (!ap->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1039 /* has LLDD analyzed already? */
1040 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1041 qc = __ata_qc_from_tag(ap, tag);
1043 if (!(qc->flags & ATA_QCFLAG_FAILED))
1050 /* okay, this error is ours */
1051 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1053 ata_port_printk(ap, KERN_ERR, "failed to read log page 10h "
1054 "(errno=%d)\n", rc);
1058 if (!(ap->sactive & (1 << tag))) {
1059 ata_port_printk(ap, KERN_ERR, "log page 10h reported "
1060 "inactive tag %d\n", tag);
1064 /* we've got the perpetrator, condemn it */
1065 qc = __ata_qc_from_tag(ap, tag);
1066 memcpy(&qc->result_tf, &tf, sizeof(tf));
1067 qc->err_mask |= AC_ERR_DEV;
1068 ehc->i.err_mask &= ~AC_ERR_DEV;
1072 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1073 * @qc: qc to analyze
1074 * @tf: Taskfile registers to analyze
1076 * Analyze taskfile of @qc and further determine cause of
1077 * failure. This function also requests ATAPI sense data if
1081 * Kernel thread context (may sleep).
1084 * Determined recovery action
1086 static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1087 const struct ata_taskfile *tf)
1089 unsigned int tmp, action = 0;
1090 u8 stat = tf->command, err = tf->feature;
1092 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1093 qc->err_mask |= AC_ERR_HSM;
1094 return ATA_EH_SOFTRESET;
1097 if (!(qc->err_mask & AC_ERR_DEV))
1100 switch (qc->dev->class) {
1103 qc->err_mask |= AC_ERR_ATA_BUS;
1105 qc->err_mask |= AC_ERR_MEDIA;
1107 qc->err_mask |= AC_ERR_INVALID;
1111 tmp = atapi_eh_request_sense(qc->dev,
1112 qc->scsicmd->sense_buffer);
1114 /* ATA_QCFLAG_SENSE_VALID is used to tell
1115 * atapi_qc_complete() that sense data is
1118 * TODO: interpret sense data and set
1119 * appropriate err_mask.
1121 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1123 qc->err_mask |= tmp;
1126 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1127 action |= ATA_EH_SOFTRESET;
1132 static int ata_eh_categorize_ering_entry(struct ata_ering_entry *ent)
1134 if (ent->err_mask & (AC_ERR_ATA_BUS | AC_ERR_TIMEOUT))
1138 if (ent->err_mask & AC_ERR_HSM)
1140 if ((ent->err_mask &
1141 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1148 struct speed_down_needed_arg {
1153 static int speed_down_needed_cb(struct ata_ering_entry *ent, void *void_arg)
1155 struct speed_down_needed_arg *arg = void_arg;
1157 if (ent->timestamp < arg->since)
1160 arg->nr_errors[ata_eh_categorize_ering_entry(ent)]++;
1165 * ata_eh_speed_down_needed - Determine wheter speed down is necessary
1166 * @dev: Device of interest
1168 * This function examines error ring of @dev and determines
1169 * whether speed down is necessary. Speed down is necessary if
1170 * there have been more than 3 of Cat-1 errors or 10 of Cat-2
1171 * errors during last 15 minutes.
1173 * Cat-1 errors are ATA_BUS, TIMEOUT for any command and HSM
1174 * violation for known supported commands.
1176 * Cat-2 errors are unclassified DEV error for known supported
1180 * Inherited from caller.
1183 * 1 if speed down is necessary, 0 otherwise
1185 static int ata_eh_speed_down_needed(struct ata_device *dev)
1187 const u64 interval = 15LLU * 60 * HZ;
1188 static const int err_limits[3] = { -1, 3, 10 };
1189 struct speed_down_needed_arg arg;
1190 struct ata_ering_entry *ent;
1194 ent = ata_ering_top(&dev->ering);
1198 err_cat = ata_eh_categorize_ering_entry(ent);
1202 memset(&arg, 0, sizeof(arg));
1204 j64 = get_jiffies_64();
1205 if (j64 >= interval)
1206 arg.since = j64 - interval;
1210 ata_ering_map(&dev->ering, speed_down_needed_cb, &arg);
1212 return arg.nr_errors[err_cat] > err_limits[err_cat];
1216 * ata_eh_speed_down - record error and speed down if necessary
1217 * @dev: Failed device
1218 * @is_io: Did the device fail during normal IO?
1219 * @err_mask: err_mask of the error
1221 * Record error and examine error history to determine whether
1222 * adjusting transmission speed is necessary. It also sets
1223 * transmission limits appropriately if such adjustment is
1227 * Kernel thread context (may sleep).
1230 * 0 on success, -errno otherwise
1232 static int ata_eh_speed_down(struct ata_device *dev, int is_io,
1233 unsigned int err_mask)
1238 /* record error and determine whether speed down is necessary */
1239 ata_ering_record(&dev->ering, is_io, err_mask);
1241 if (!ata_eh_speed_down_needed(dev))
1244 /* speed down SATA link speed if possible */
1245 if (sata_down_spd_limit(dev->ap) == 0)
1246 return ATA_EH_HARDRESET;
1248 /* lower transfer mode */
1249 if (ata_down_xfermask_limit(dev, 0) == 0)
1250 return ATA_EH_SOFTRESET;
1252 ata_dev_printk(dev, KERN_ERR,
1253 "speed down requested but no transfer mode left\n");
1258 * ata_eh_autopsy - analyze error and determine recovery action
1259 * @ap: ATA port to perform autopsy on
1261 * Analyze why @ap failed and determine which recovery action is
1262 * needed. This function also sets more detailed AC_ERR_* values
1263 * and fills sense data for ATAPI CHECK SENSE.
1266 * Kernel thread context (may sleep).
1268 static void ata_eh_autopsy(struct ata_port *ap)
1270 struct ata_eh_context *ehc = &ap->eh_context;
1271 unsigned int action = ehc->i.action;
1272 struct ata_device *failed_dev = NULL;
1273 unsigned int all_err_mask = 0;
1280 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1283 /* obtain and analyze SError */
1284 rc = sata_scr_read(ap, SCR_ERROR, &serror);
1286 ehc->i.serror |= serror;
1287 ata_eh_analyze_serror(ap);
1288 } else if (rc != -EOPNOTSUPP)
1289 action |= ATA_EH_HARDRESET;
1291 /* analyze NCQ failure */
1292 ata_eh_analyze_ncq_error(ap);
1294 /* any real error trumps AC_ERR_OTHER */
1295 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1296 ehc->i.err_mask &= ~AC_ERR_OTHER;
1298 all_err_mask |= ehc->i.err_mask;
1300 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1301 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1303 if (!(qc->flags & ATA_QCFLAG_FAILED))
1306 /* inherit upper level err_mask */
1307 qc->err_mask |= ehc->i.err_mask;
1310 action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1312 /* DEV errors are probably spurious in case of ATA_BUS error */
1313 if (qc->err_mask & AC_ERR_ATA_BUS)
1314 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1317 /* any real error trumps unknown error */
1318 if (qc->err_mask & ~AC_ERR_OTHER)
1319 qc->err_mask &= ~AC_ERR_OTHER;
1321 /* SENSE_VALID trumps dev/unknown error and revalidation */
1322 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1323 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1324 action &= ~ATA_EH_REVALIDATE;
1327 /* accumulate error info */
1328 failed_dev = qc->dev;
1329 all_err_mask |= qc->err_mask;
1330 if (qc->flags & ATA_QCFLAG_IO)
1334 /* enforce default EH actions */
1335 if (ap->pflags & ATA_PFLAG_FROZEN ||
1336 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
1337 action |= ATA_EH_SOFTRESET;
1338 else if (all_err_mask)
1339 action |= ATA_EH_REVALIDATE;
1341 /* if we have offending qcs and the associated failed device */
1344 action |= ata_eh_speed_down(failed_dev, is_io, all_err_mask);
1346 /* perform per-dev EH action only on the offending device */
1347 ehc->i.dev_action[failed_dev->devno] |=
1348 action & ATA_EH_PERDEV_MASK;
1349 action &= ~ATA_EH_PERDEV_MASK;
1352 /* record autopsy result */
1353 ehc->i.dev = failed_dev;
1354 ehc->i.action |= action;
1360 * ata_eh_report - report error handling to user
1361 * @ap: ATA port EH is going on
1363 * Report EH to user.
1368 static void ata_eh_report(struct ata_port *ap)
1370 struct ata_eh_context *ehc = &ap->eh_context;
1371 const char *frozen, *desc;
1372 int tag, nr_failed = 0;
1375 if (ehc->i.desc[0] != '\0')
1378 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1379 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1381 if (!(qc->flags & ATA_QCFLAG_FAILED))
1383 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1389 if (!nr_failed && !ehc->i.err_mask)
1393 if (ap->pflags & ATA_PFLAG_FROZEN)
1397 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1398 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1399 ehc->i.err_mask, ap->sactive, ehc->i.serror,
1400 ehc->i.action, frozen);
1402 ata_dev_printk(ehc->i.dev, KERN_ERR, "(%s)\n", desc);
1404 ata_port_printk(ap, KERN_ERR, "exception Emask 0x%x "
1405 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1406 ehc->i.err_mask, ap->sactive, ehc->i.serror,
1407 ehc->i.action, frozen);
1409 ata_port_printk(ap, KERN_ERR, "(%s)\n", desc);
1412 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1413 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1415 if (!(qc->flags & ATA_QCFLAG_FAILED) || !qc->err_mask)
1418 ata_dev_printk(qc->dev, KERN_ERR, "tag %d cmd 0x%x "
1419 "Emask 0x%x stat 0x%x err 0x%x (%s)\n",
1420 qc->tag, qc->tf.command, qc->err_mask,
1421 qc->result_tf.command, qc->result_tf.feature,
1422 ata_err_string(qc->err_mask));
1426 static int ata_do_reset(struct ata_port *ap, ata_reset_fn_t reset,
1427 unsigned int *classes)
1431 for (i = 0; i < ATA_MAX_DEVICES; i++)
1432 classes[i] = ATA_DEV_UNKNOWN;
1434 rc = reset(ap, classes);
1438 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
1439 * is complete and convert all ATA_DEV_UNKNOWN to
1442 for (i = 0; i < ATA_MAX_DEVICES; i++)
1443 if (classes[i] != ATA_DEV_UNKNOWN)
1446 if (i < ATA_MAX_DEVICES)
1447 for (i = 0; i < ATA_MAX_DEVICES; i++)
1448 if (classes[i] == ATA_DEV_UNKNOWN)
1449 classes[i] = ATA_DEV_NONE;
1454 static int ata_eh_followup_srst_needed(int rc, int classify,
1455 const unsigned int *classes)
1461 if (classify && classes[0] == ATA_DEV_UNKNOWN)
1466 static int ata_eh_reset(struct ata_port *ap, int classify,
1467 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
1468 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
1470 struct ata_eh_context *ehc = &ap->eh_context;
1471 unsigned int *classes = ehc->classes;
1472 int tries = ATA_EH_RESET_TRIES;
1473 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
1474 unsigned int action;
1475 ata_reset_fn_t reset;
1476 int i, did_followup_srst, rc;
1478 /* Determine which reset to use and record in ehc->i.action.
1479 * prereset() may examine and modify it.
1481 action = ehc->i.action;
1482 ehc->i.action &= ~ATA_EH_RESET_MASK;
1483 if (softreset && (!hardreset || (!sata_set_spd_needed(ap) &&
1484 !(action & ATA_EH_HARDRESET))))
1485 ehc->i.action |= ATA_EH_SOFTRESET;
1487 ehc->i.action |= ATA_EH_HARDRESET;
1492 ata_port_printk(ap, KERN_ERR,
1493 "prereset failed (errno=%d)\n", rc);
1498 /* prereset() might have modified ehc->i.action */
1499 if (ehc->i.action & ATA_EH_HARDRESET)
1501 else if (ehc->i.action & ATA_EH_SOFTRESET)
1504 /* prereset told us not to reset, bang classes and return */
1505 for (i = 0; i < ATA_MAX_DEVICES; i++)
1506 classes[i] = ATA_DEV_NONE;
1510 /* did prereset() screw up? if so, fix up to avoid oopsing */
1512 ata_port_printk(ap, KERN_ERR, "BUG: prereset() requested "
1513 "invalid reset type\n");
1521 /* shut up during boot probing */
1523 ata_port_printk(ap, KERN_INFO, "%s resetting port\n",
1524 reset == softreset ? "soft" : "hard");
1527 ata_eh_about_to_do(ap, NULL, ATA_EH_RESET_MASK);
1528 ehc->i.flags |= ATA_EHI_DID_RESET;
1530 rc = ata_do_reset(ap, reset, classes);
1532 did_followup_srst = 0;
1533 if (reset == hardreset &&
1534 ata_eh_followup_srst_needed(rc, classify, classes)) {
1535 /* okay, let's do follow-up softreset */
1536 did_followup_srst = 1;
1540 ata_port_printk(ap, KERN_ERR,
1541 "follow-up softreset required "
1542 "but no softreset avaliable\n");
1546 ata_eh_about_to_do(ap, NULL, ATA_EH_RESET_MASK);
1547 rc = ata_do_reset(ap, reset, classes);
1549 if (rc == 0 && classify &&
1550 classes[0] == ATA_DEV_UNKNOWN) {
1551 ata_port_printk(ap, KERN_ERR,
1552 "classification failed\n");
1557 if (rc && --tries) {
1560 if (reset == softreset) {
1561 if (did_followup_srst)
1562 type = "follow-up soft";
1568 ata_port_printk(ap, KERN_WARNING,
1569 "%sreset failed, retrying in 5 secs\n", type);
1572 if (reset == hardreset)
1573 sata_down_spd_limit(ap);
1580 /* After the reset, the device state is PIO 0 and the
1581 * controller state is undefined. Record the mode.
1583 for (i = 0; i < ATA_MAX_DEVICES; i++)
1584 ap->device[i].pio_mode = XFER_PIO_0;
1587 postreset(ap, classes);
1589 /* reset successful, schedule revalidation */
1590 ata_eh_done(ap, NULL, ATA_EH_RESET_MASK);
1591 ehc->i.action |= ATA_EH_REVALIDATE;
1597 static int ata_eh_revalidate_and_attach(struct ata_port *ap,
1598 struct ata_device **r_failed_dev)
1600 struct ata_eh_context *ehc = &ap->eh_context;
1601 struct ata_device *dev;
1602 unsigned long flags;
1607 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1608 unsigned int action;
1610 dev = &ap->device[i];
1611 action = ata_eh_dev_action(dev);
1613 if (action & ATA_EH_REVALIDATE && ata_dev_ready(dev)) {
1614 if (ata_port_offline(ap)) {
1619 ata_eh_about_to_do(ap, dev, ATA_EH_REVALIDATE);
1620 rc = ata_dev_revalidate(dev,
1621 ehc->i.flags & ATA_EHI_DID_RESET);
1625 ata_eh_done(ap, dev, ATA_EH_REVALIDATE);
1627 /* schedule the scsi_rescan_device() here */
1628 queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
1629 } else if (dev->class == ATA_DEV_UNKNOWN &&
1630 ehc->tries[dev->devno] &&
1631 ata_class_enabled(ehc->classes[dev->devno])) {
1632 dev->class = ehc->classes[dev->devno];
1634 rc = ata_dev_read_id(dev, &dev->class, 1, dev->id);
1636 rc = ata_dev_configure(dev, 1);
1639 dev->class = ATA_DEV_UNKNOWN;
1643 spin_lock_irqsave(ap->lock, flags);
1644 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
1645 spin_unlock_irqrestore(ap->lock, flags);
1650 *r_failed_dev = dev;
1657 * ata_eh_suspend - handle suspend EH action
1658 * @ap: target host port
1659 * @r_failed_dev: result parameter to indicate failing device
1661 * Handle suspend EH action. Disk devices are spinned down and
1662 * other types of devices are just marked suspended. Once
1663 * suspended, no EH action to the device is allowed until it is
1667 * Kernel thread context (may sleep).
1670 * 0 on success, -errno otherwise
1672 static int ata_eh_suspend(struct ata_port *ap, struct ata_device **r_failed_dev)
1674 struct ata_device *dev;
1679 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1680 unsigned long flags;
1681 unsigned int action, err_mask;
1683 dev = &ap->device[i];
1684 action = ata_eh_dev_action(dev);
1686 if (!ata_dev_enabled(dev) || !(action & ATA_EH_SUSPEND))
1689 WARN_ON(dev->flags & ATA_DFLAG_SUSPENDED);
1691 ata_eh_about_to_do(ap, dev, ATA_EH_SUSPEND);
1693 if (dev->class == ATA_DEV_ATA && !(action & ATA_EH_PM_FREEZE)) {
1695 rc = ata_flush_cache(dev);
1700 err_mask = ata_do_simple_cmd(dev, ATA_CMD_STANDBYNOW1);
1702 ata_dev_printk(dev, KERN_ERR, "failed to "
1703 "spin down (err_mask=0x%x)\n",
1710 spin_lock_irqsave(ap->lock, flags);
1711 dev->flags |= ATA_DFLAG_SUSPENDED;
1712 spin_unlock_irqrestore(ap->lock, flags);
1714 ata_eh_done(ap, dev, ATA_EH_SUSPEND);
1718 *r_failed_dev = dev;
1725 * ata_eh_prep_resume - prep for resume EH action
1726 * @ap: target host port
1728 * Clear SUSPENDED in preparation for scheduled resume actions.
1729 * This allows other parts of EH to access the devices being
1733 * Kernel thread context (may sleep).
1735 static void ata_eh_prep_resume(struct ata_port *ap)
1737 struct ata_device *dev;
1738 unsigned long flags;
1743 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1744 unsigned int action;
1746 dev = &ap->device[i];
1747 action = ata_eh_dev_action(dev);
1749 if (!ata_dev_enabled(dev) || !(action & ATA_EH_RESUME))
1752 spin_lock_irqsave(ap->lock, flags);
1753 dev->flags &= ~ATA_DFLAG_SUSPENDED;
1754 spin_unlock_irqrestore(ap->lock, flags);
1761 * ata_eh_resume - handle resume EH action
1762 * @ap: target host port
1763 * @r_failed_dev: result parameter to indicate failing device
1765 * Handle resume EH action. Target devices are already reset and
1766 * revalidated. Spinning up is the only operation left.
1769 * Kernel thread context (may sleep).
1772 * 0 on success, -errno otherwise
1774 static int ata_eh_resume(struct ata_port *ap, struct ata_device **r_failed_dev)
1776 struct ata_device *dev;
1781 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1782 unsigned int action, err_mask;
1784 dev = &ap->device[i];
1785 action = ata_eh_dev_action(dev);
1787 if (!ata_dev_enabled(dev) || !(action & ATA_EH_RESUME))
1790 ata_eh_about_to_do(ap, dev, ATA_EH_RESUME);
1792 if (dev->class == ATA_DEV_ATA && !(action & ATA_EH_PM_FREEZE)) {
1793 err_mask = ata_do_simple_cmd(dev,
1794 ATA_CMD_IDLEIMMEDIATE);
1796 ata_dev_printk(dev, KERN_ERR, "failed to "
1797 "spin up (err_mask=0x%x)\n",
1804 ata_eh_done(ap, dev, ATA_EH_RESUME);
1808 *r_failed_dev = dev;
1814 static int ata_port_nr_enabled(struct ata_port *ap)
1818 for (i = 0; i < ATA_MAX_DEVICES; i++)
1819 if (ata_dev_enabled(&ap->device[i]))
1824 static int ata_port_nr_vacant(struct ata_port *ap)
1828 for (i = 0; i < ATA_MAX_DEVICES; i++)
1829 if (ap->device[i].class == ATA_DEV_UNKNOWN)
1834 static int ata_eh_skip_recovery(struct ata_port *ap)
1836 struct ata_eh_context *ehc = &ap->eh_context;
1839 /* skip if all possible devices are suspended */
1840 for (i = 0; i < ata_port_max_devices(ap); i++) {
1841 struct ata_device *dev = &ap->device[i];
1843 if (ata_dev_absent(dev) || ata_dev_ready(dev))
1847 if (i == ata_port_max_devices(ap))
1850 /* always thaw frozen port and recover failed devices */
1851 if (ap->pflags & ATA_PFLAG_FROZEN || ata_port_nr_enabled(ap))
1854 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
1855 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1856 struct ata_device *dev = &ap->device[i];
1858 if (dev->class == ATA_DEV_UNKNOWN &&
1859 ehc->classes[dev->devno] != ATA_DEV_NONE)
1867 * ata_eh_recover - recover host port after error
1868 * @ap: host port to recover
1869 * @prereset: prereset method (can be NULL)
1870 * @softreset: softreset method (can be NULL)
1871 * @hardreset: hardreset method (can be NULL)
1872 * @postreset: postreset method (can be NULL)
1874 * This is the alpha and omega, eum and yang, heart and soul of
1875 * libata exception handling. On entry, actions required to
1876 * recover the port and hotplug requests are recorded in
1877 * eh_context. This function executes all the operations with
1878 * appropriate retrials and fallbacks to resurrect failed
1879 * devices, detach goners and greet newcomers.
1882 * Kernel thread context (may sleep).
1885 * 0 on success, -errno on failure.
1887 static int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
1888 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
1889 ata_postreset_fn_t postreset)
1891 struct ata_eh_context *ehc = &ap->eh_context;
1892 struct ata_device *dev;
1893 int down_xfermask, i, rc;
1897 /* prep for recovery */
1898 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1899 dev = &ap->device[i];
1901 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
1903 /* process hotplug request */
1904 if (dev->flags & ATA_DFLAG_DETACH)
1905 ata_eh_detach_dev(dev);
1907 if (!ata_dev_enabled(dev) &&
1908 ((ehc->i.probe_mask & (1 << dev->devno)) &&
1909 !(ehc->did_probe_mask & (1 << dev->devno)))) {
1910 ata_eh_detach_dev(dev);
1912 ehc->did_probe_mask |= (1 << dev->devno);
1913 ehc->i.action |= ATA_EH_SOFTRESET;
1921 /* if UNLOADING, finish immediately */
1922 if (ap->pflags & ATA_PFLAG_UNLOADING)
1925 /* prep for resume */
1926 ata_eh_prep_resume(ap);
1928 /* skip EH if possible. */
1929 if (ata_eh_skip_recovery(ap))
1932 for (i = 0; i < ATA_MAX_DEVICES; i++)
1933 ehc->classes[i] = ATA_DEV_UNKNOWN;
1936 if (ehc->i.action & ATA_EH_RESET_MASK) {
1937 ata_eh_freeze_port(ap);
1939 rc = ata_eh_reset(ap, ata_port_nr_vacant(ap), prereset,
1940 softreset, hardreset, postreset);
1942 ata_port_printk(ap, KERN_ERR,
1943 "reset failed, giving up\n");
1947 ata_eh_thaw_port(ap);
1950 /* revalidate existing devices and attach new ones */
1951 rc = ata_eh_revalidate_and_attach(ap, &dev);
1955 /* resume devices */
1956 rc = ata_eh_resume(ap, &dev);
1960 /* configure transfer mode if the port has been reset */
1961 if (ehc->i.flags & ATA_EHI_DID_RESET) {
1962 rc = ata_set_mode(ap, &dev);
1969 /* suspend devices */
1970 rc = ata_eh_suspend(ap, &dev);
1979 /* device missing, schedule probing */
1980 ehc->i.probe_mask |= (1 << dev->devno);
1982 ehc->tries[dev->devno] = 0;
1985 sata_down_spd_limit(ap);
1987 ehc->tries[dev->devno]--;
1988 if (down_xfermask &&
1989 ata_down_xfermask_limit(dev, ehc->tries[dev->devno] == 1))
1990 ehc->tries[dev->devno] = 0;
1993 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
1994 /* disable device if it has used up all its chances */
1995 ata_dev_disable(dev);
1997 /* detach if offline */
1998 if (ata_port_offline(ap))
1999 ata_eh_detach_dev(dev);
2001 /* probe if requested */
2002 if ((ehc->i.probe_mask & (1 << dev->devno)) &&
2003 !(ehc->did_probe_mask & (1 << dev->devno))) {
2004 ata_eh_detach_dev(dev);
2007 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2008 ehc->did_probe_mask |= (1 << dev->devno);
2009 ehc->i.action |= ATA_EH_SOFTRESET;
2012 /* soft didn't work? be haaaaard */
2013 if (ehc->i.flags & ATA_EHI_DID_RESET)
2014 ehc->i.action |= ATA_EH_HARDRESET;
2016 ehc->i.action |= ATA_EH_SOFTRESET;
2019 if (ata_port_nr_enabled(ap)) {
2020 ata_port_printk(ap, KERN_WARNING, "failed to recover some "
2021 "devices, retrying in 5 secs\n");
2024 /* no device left, repeat fast */
2032 for (i = 0; i < ATA_MAX_DEVICES; i++)
2033 ata_dev_disable(&ap->device[i]);
2036 DPRINTK("EXIT, rc=%d\n", rc);
2041 * ata_eh_finish - finish up EH
2042 * @ap: host port to finish EH for
2044 * Recovery is complete. Clean up EH states and retry or finish
2050 static void ata_eh_finish(struct ata_port *ap)
2054 /* retry or finish qcs */
2055 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2056 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2058 if (!(qc->flags & ATA_QCFLAG_FAILED))
2062 /* FIXME: Once EH migration is complete,
2063 * generate sense data in this function,
2064 * considering both err_mask and tf.
2066 if (qc->err_mask & AC_ERR_INVALID)
2067 ata_eh_qc_complete(qc);
2069 ata_eh_qc_retry(qc);
2071 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2072 ata_eh_qc_complete(qc);
2074 /* feed zero TF to sense generation */
2075 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2076 ata_eh_qc_retry(qc);
2083 * ata_do_eh - do standard error handling
2084 * @ap: host port to handle error for
2085 * @prereset: prereset method (can be NULL)
2086 * @softreset: softreset method (can be NULL)
2087 * @hardreset: hardreset method (can be NULL)
2088 * @postreset: postreset method (can be NULL)
2090 * Perform standard error handling sequence.
2093 * Kernel thread context (may sleep).
2095 void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2096 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2097 ata_postreset_fn_t postreset)
2101 ata_eh_recover(ap, prereset, softreset, hardreset, postreset);