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;
328 if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
329 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");
334 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
336 /* tell wait_eh that we're done */
337 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
338 wake_up_all(&ap->eh_wait_q);
340 spin_unlock_irqrestore(ap->lock, flags);
346 * ata_port_wait_eh - Wait for the currently pending EH to complete
347 * @ap: Port to wait EH for
349 * Wait until the currently pending EH is complete.
352 * Kernel thread context (may sleep).
354 void ata_port_wait_eh(struct ata_port *ap)
360 spin_lock_irqsave(ap->lock, flags);
362 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
363 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
364 spin_unlock_irqrestore(ap->lock, flags);
366 spin_lock_irqsave(ap->lock, flags);
368 finish_wait(&ap->eh_wait_q, &wait);
370 spin_unlock_irqrestore(ap->lock, flags);
372 /* make sure SCSI EH is complete */
373 if (scsi_host_in_recovery(ap->host)) {
380 * ata_qc_timeout - Handle timeout of queued command
381 * @qc: Command that timed out
383 * Some part of the kernel (currently, only the SCSI layer)
384 * has noticed that the active command on port @ap has not
385 * completed after a specified length of time. Handle this
386 * condition by disabling DMA (if necessary) and completing
387 * transactions, with error if necessary.
389 * This also handles the case of the "lost interrupt", where
390 * for some reason (possibly hardware bug, possibly driver bug)
391 * an interrupt was not delivered to the driver, even though the
392 * transaction completed successfully.
394 * TODO: kill this function once old EH is gone.
397 * Inherited from SCSI layer (none, can sleep)
399 static void ata_qc_timeout(struct ata_queued_cmd *qc)
401 struct ata_port *ap = qc->ap;
402 u8 host_stat = 0, drv_stat;
407 ap->hsm_task_state = HSM_ST_IDLE;
409 spin_lock_irqsave(ap->lock, flags);
411 switch (qc->tf.protocol) {
414 case ATA_PROT_ATAPI_DMA:
415 host_stat = ap->ops->bmdma_status(ap);
417 /* before we do anything else, clear DMA-Start bit */
418 ap->ops->bmdma_stop(qc);
424 drv_stat = ata_chk_status(ap);
426 /* ack bmdma irq events */
427 ap->ops->irq_clear(ap);
429 ata_dev_printk(qc->dev, KERN_ERR, "command 0x%x timeout, "
430 "stat 0x%x host_stat 0x%x\n",
431 qc->tf.command, drv_stat, host_stat);
433 /* complete taskfile transaction */
434 qc->err_mask |= AC_ERR_TIMEOUT;
438 spin_unlock_irqrestore(ap->lock, flags);
440 ata_eh_qc_complete(qc);
446 * ata_eng_timeout - Handle timeout of queued command
447 * @ap: Port on which timed-out command is active
449 * Some part of the kernel (currently, only the SCSI layer)
450 * has noticed that the active command on port @ap has not
451 * completed after a specified length of time. Handle this
452 * condition by disabling DMA (if necessary) and completing
453 * transactions, with error if necessary.
455 * This also handles the case of the "lost interrupt", where
456 * for some reason (possibly hardware bug, possibly driver bug)
457 * an interrupt was not delivered to the driver, even though the
458 * transaction completed successfully.
460 * TODO: kill this function once old EH is gone.
463 * Inherited from SCSI layer (none, can sleep)
465 void ata_eng_timeout(struct ata_port *ap)
469 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
475 * ata_qc_schedule_eh - schedule qc for error handling
476 * @qc: command to schedule error handling for
478 * Schedule error handling for @qc. EH will kick in as soon as
479 * other commands are drained.
482 * spin_lock_irqsave(host_set lock)
484 void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
486 struct ata_port *ap = qc->ap;
488 WARN_ON(!ap->ops->error_handler);
490 qc->flags |= ATA_QCFLAG_FAILED;
491 qc->ap->pflags |= ATA_PFLAG_EH_PENDING;
493 /* The following will fail if timeout has already expired.
494 * ata_scsi_error() takes care of such scmds on EH entry.
495 * Note that ATA_QCFLAG_FAILED is unconditionally set after
496 * this function completes.
498 scsi_req_abort_cmd(qc->scsicmd);
502 * ata_port_schedule_eh - schedule error handling without a qc
503 * @ap: ATA port to schedule EH for
505 * Schedule error handling for @ap. EH will kick in as soon as
506 * all commands are drained.
509 * spin_lock_irqsave(host_set lock)
511 void ata_port_schedule_eh(struct ata_port *ap)
513 WARN_ON(!ap->ops->error_handler);
515 ap->pflags |= ATA_PFLAG_EH_PENDING;
516 scsi_schedule_eh(ap->host);
518 DPRINTK("port EH scheduled\n");
522 * ata_port_abort - abort all qc's on the port
523 * @ap: ATA port to abort qc's for
525 * Abort all active qc's of @ap and schedule EH.
528 * spin_lock_irqsave(host_set lock)
531 * Number of aborted qc's.
533 int ata_port_abort(struct ata_port *ap)
535 int tag, nr_aborted = 0;
537 WARN_ON(!ap->ops->error_handler);
539 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
540 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
543 qc->flags |= ATA_QCFLAG_FAILED;
550 ata_port_schedule_eh(ap);
556 * __ata_port_freeze - freeze port
557 * @ap: ATA port to freeze
559 * This function is called when HSM violation or some other
560 * condition disrupts normal operation of the port. Frozen port
561 * is not allowed to perform any operation until the port is
562 * thawed, which usually follows a successful reset.
564 * ap->ops->freeze() callback can be used for freezing the port
565 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
566 * port cannot be frozen hardware-wise, the interrupt handler
567 * must ack and clear interrupts unconditionally while the port
571 * spin_lock_irqsave(host_set lock)
573 static void __ata_port_freeze(struct ata_port *ap)
575 WARN_ON(!ap->ops->error_handler);
580 ap->pflags |= ATA_PFLAG_FROZEN;
582 DPRINTK("ata%u port frozen\n", ap->id);
586 * ata_port_freeze - abort & freeze port
587 * @ap: ATA port to freeze
589 * Abort and freeze @ap.
592 * spin_lock_irqsave(host_set lock)
595 * Number of aborted commands.
597 int ata_port_freeze(struct ata_port *ap)
601 WARN_ON(!ap->ops->error_handler);
603 nr_aborted = ata_port_abort(ap);
604 __ata_port_freeze(ap);
610 * ata_eh_freeze_port - EH helper to freeze port
611 * @ap: ATA port to freeze
618 void ata_eh_freeze_port(struct ata_port *ap)
622 if (!ap->ops->error_handler)
625 spin_lock_irqsave(ap->lock, flags);
626 __ata_port_freeze(ap);
627 spin_unlock_irqrestore(ap->lock, flags);
631 * ata_port_thaw_port - EH helper to thaw port
632 * @ap: ATA port to thaw
634 * Thaw frozen port @ap.
639 void ata_eh_thaw_port(struct ata_port *ap)
643 if (!ap->ops->error_handler)
646 spin_lock_irqsave(ap->lock, flags);
648 ap->pflags &= ~ATA_PFLAG_FROZEN;
653 spin_unlock_irqrestore(ap->lock, flags);
655 DPRINTK("ata%u port thawed\n", ap->id);
658 static void ata_eh_scsidone(struct scsi_cmnd *scmd)
663 static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
665 struct ata_port *ap = qc->ap;
666 struct scsi_cmnd *scmd = qc->scsicmd;
669 spin_lock_irqsave(ap->lock, flags);
670 qc->scsidone = ata_eh_scsidone;
671 __ata_qc_complete(qc);
672 WARN_ON(ata_tag_valid(qc->tag));
673 spin_unlock_irqrestore(ap->lock, flags);
675 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
679 * ata_eh_qc_complete - Complete an active ATA command from EH
680 * @qc: Command to complete
682 * Indicate to the mid and upper layers that an ATA command has
683 * completed. To be used from EH.
685 void ata_eh_qc_complete(struct ata_queued_cmd *qc)
687 struct scsi_cmnd *scmd = qc->scsicmd;
688 scmd->retries = scmd->allowed;
689 __ata_eh_qc_complete(qc);
693 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
694 * @qc: Command to retry
696 * Indicate to the mid and upper layers that an ATA command
697 * should be retried. To be used from EH.
699 * SCSI midlayer limits the number of retries to scmd->allowed.
700 * scmd->retries is decremented for commands which get retried
701 * due to unrelated failures (qc->err_mask is zero).
703 void ata_eh_qc_retry(struct ata_queued_cmd *qc)
705 struct scsi_cmnd *scmd = qc->scsicmd;
706 if (!qc->err_mask && scmd->retries)
708 __ata_eh_qc_complete(qc);
712 * ata_eh_detach_dev - detach ATA device
713 * @dev: ATA device to detach
720 static void ata_eh_detach_dev(struct ata_device *dev)
722 struct ata_port *ap = dev->ap;
725 ata_dev_disable(dev);
727 spin_lock_irqsave(ap->lock, flags);
729 dev->flags &= ~ATA_DFLAG_DETACH;
731 if (ata_scsi_offline_dev(dev)) {
732 dev->flags |= ATA_DFLAG_DETACHED;
733 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
736 /* clear per-dev EH actions */
737 ata_eh_clear_action(dev, &ap->eh_info, ATA_EH_PERDEV_MASK);
738 ata_eh_clear_action(dev, &ap->eh_context.i, ATA_EH_PERDEV_MASK);
740 spin_unlock_irqrestore(ap->lock, flags);
744 * ata_eh_about_to_do - about to perform eh_action
745 * @ap: target ATA port
746 * @dev: target ATA dev for per-dev action (can be NULL)
747 * @action: action about to be performed
749 * Called just before performing EH actions to clear related bits
750 * in @ap->eh_info such that eh actions are not unnecessarily
756 static void ata_eh_about_to_do(struct ata_port *ap, struct ata_device *dev,
761 spin_lock_irqsave(ap->lock, flags);
762 ata_eh_clear_action(dev, &ap->eh_info, action);
763 ap->pflags |= ATA_PFLAG_RECOVERED;
764 spin_unlock_irqrestore(ap->lock, flags);
768 * ata_eh_done - EH action complete
769 * @ap: target ATA port
770 * @dev: target ATA dev for per-dev action (can be NULL)
771 * @action: action just completed
773 * Called right after performing EH actions to clear related bits
774 * in @ap->eh_context.
779 static void ata_eh_done(struct ata_port *ap, struct ata_device *dev,
782 ata_eh_clear_action(dev, &ap->eh_context.i, action);
786 * ata_err_string - convert err_mask to descriptive string
787 * @err_mask: error mask to convert to string
789 * Convert @err_mask to descriptive string. Errors are
790 * prioritized according to severity and only the most severe
797 * Descriptive string for @err_mask
799 static const char * ata_err_string(unsigned int err_mask)
801 if (err_mask & AC_ERR_HOST_BUS)
802 return "host bus error";
803 if (err_mask & AC_ERR_ATA_BUS)
804 return "ATA bus error";
805 if (err_mask & AC_ERR_TIMEOUT)
807 if (err_mask & AC_ERR_HSM)
808 return "HSM violation";
809 if (err_mask & AC_ERR_SYSTEM)
810 return "internal error";
811 if (err_mask & AC_ERR_MEDIA)
812 return "media error";
813 if (err_mask & AC_ERR_INVALID)
814 return "invalid argument";
815 if (err_mask & AC_ERR_DEV)
816 return "device error";
817 return "unknown error";
821 * ata_read_log_page - read a specific log page
822 * @dev: target device
823 * @page: page to read
824 * @buf: buffer to store read page
825 * @sectors: number of sectors to read
827 * Read log page using READ_LOG_EXT command.
830 * Kernel thread context (may sleep).
833 * 0 on success, AC_ERR_* mask otherwise.
835 static unsigned int ata_read_log_page(struct ata_device *dev,
836 u8 page, void *buf, unsigned int sectors)
838 struct ata_taskfile tf;
839 unsigned int err_mask;
841 DPRINTK("read log page - page %d\n", page);
843 ata_tf_init(dev, &tf);
844 tf.command = ATA_CMD_READ_LOG_EXT;
847 tf.hob_nsect = sectors >> 8;
848 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
849 tf.protocol = ATA_PROT_PIO;
851 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
852 buf, sectors * ATA_SECT_SIZE);
854 DPRINTK("EXIT, err_mask=%x\n", err_mask);
859 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
860 * @dev: Device to read log page 10h from
861 * @tag: Resulting tag of the failed command
862 * @tf: Resulting taskfile registers of the failed command
864 * Read log page 10h to obtain NCQ error details and clear error
868 * Kernel thread context (may sleep).
871 * 0 on success, -errno otherwise.
873 static int ata_eh_read_log_10h(struct ata_device *dev,
874 int *tag, struct ata_taskfile *tf)
876 u8 *buf = dev->ap->sector_buf;
877 unsigned int err_mask;
881 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
886 for (i = 0; i < ATA_SECT_SIZE; i++)
889 ata_dev_printk(dev, KERN_WARNING,
890 "invalid checksum 0x%x on log page 10h\n", csum);
895 *tag = buf[0] & 0x1f;
897 tf->command = buf[2];
898 tf->feature = buf[3];
903 tf->hob_lbal = buf[8];
904 tf->hob_lbam = buf[9];
905 tf->hob_lbah = buf[10];
907 tf->hob_nsect = buf[13];
913 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
914 * @dev: device to perform REQUEST_SENSE to
915 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
917 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
918 * SENSE. This function is EH helper.
921 * Kernel thread context (may sleep).
924 * 0 on success, AC_ERR_* mask on failure
926 static unsigned int atapi_eh_request_sense(struct ata_device *dev,
927 unsigned char *sense_buf)
929 struct ata_port *ap = dev->ap;
930 struct ata_taskfile tf;
931 u8 cdb[ATAPI_CDB_LEN];
933 DPRINTK("ATAPI request sense\n");
935 ata_tf_init(dev, &tf);
937 /* FIXME: is this needed? */
938 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
940 /* XXX: why tf_read here? */
941 ap->ops->tf_read(ap, &tf);
943 /* fill these in, for the case where they are -not- overwritten */
945 sense_buf[2] = tf.feature >> 4;
947 memset(cdb, 0, ATAPI_CDB_LEN);
948 cdb[0] = REQUEST_SENSE;
949 cdb[4] = SCSI_SENSE_BUFFERSIZE;
951 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
952 tf.command = ATA_CMD_PACKET;
954 /* is it pointless to prefer PIO for "safety reasons"? */
955 if (ap->flags & ATA_FLAG_PIO_DMA) {
956 tf.protocol = ATA_PROT_ATAPI_DMA;
957 tf.feature |= ATAPI_PKT_DMA;
959 tf.protocol = ATA_PROT_ATAPI;
960 tf.lbam = (8 * 1024) & 0xff;
961 tf.lbah = (8 * 1024) >> 8;
964 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
965 sense_buf, SCSI_SENSE_BUFFERSIZE);
969 * ata_eh_analyze_serror - analyze SError for a failed port
970 * @ap: ATA port to analyze SError for
972 * Analyze SError if available and further determine cause of
978 static void ata_eh_analyze_serror(struct ata_port *ap)
980 struct ata_eh_context *ehc = &ap->eh_context;
981 u32 serror = ehc->i.serror;
982 unsigned int err_mask = 0, action = 0;
984 if (serror & SERR_PERSISTENT) {
985 err_mask |= AC_ERR_ATA_BUS;
986 action |= ATA_EH_HARDRESET;
989 (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
990 err_mask |= AC_ERR_ATA_BUS;
991 action |= ATA_EH_SOFTRESET;
993 if (serror & SERR_PROTOCOL) {
994 err_mask |= AC_ERR_HSM;
995 action |= ATA_EH_SOFTRESET;
997 if (serror & SERR_INTERNAL) {
998 err_mask |= AC_ERR_SYSTEM;
999 action |= ATA_EH_SOFTRESET;
1001 if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG))
1002 ata_ehi_hotplugged(&ehc->i);
1004 ehc->i.err_mask |= err_mask;
1005 ehc->i.action |= action;
1009 * ata_eh_analyze_ncq_error - analyze NCQ error
1010 * @ap: ATA port to analyze NCQ error for
1012 * Read log page 10h, determine the offending qc and acquire
1013 * error status TF. For NCQ device errors, all LLDDs have to do
1014 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1018 * Kernel thread context (may sleep).
1020 static void ata_eh_analyze_ncq_error(struct ata_port *ap)
1022 struct ata_eh_context *ehc = &ap->eh_context;
1023 struct ata_device *dev = ap->device;
1024 struct ata_queued_cmd *qc;
1025 struct ata_taskfile tf;
1028 /* if frozen, we can't do much */
1029 if (ap->pflags & ATA_PFLAG_FROZEN)
1032 /* is it NCQ device error? */
1033 if (!ap->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1036 /* has LLDD analyzed already? */
1037 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1038 qc = __ata_qc_from_tag(ap, tag);
1040 if (!(qc->flags & ATA_QCFLAG_FAILED))
1047 /* okay, this error is ours */
1048 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1050 ata_port_printk(ap, KERN_ERR, "failed to read log page 10h "
1051 "(errno=%d)\n", rc);
1055 if (!(ap->sactive & (1 << tag))) {
1056 ata_port_printk(ap, KERN_ERR, "log page 10h reported "
1057 "inactive tag %d\n", tag);
1061 /* we've got the perpetrator, condemn it */
1062 qc = __ata_qc_from_tag(ap, tag);
1063 memcpy(&qc->result_tf, &tf, sizeof(tf));
1064 qc->err_mask |= AC_ERR_DEV;
1065 ehc->i.err_mask &= ~AC_ERR_DEV;
1069 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1070 * @qc: qc to analyze
1071 * @tf: Taskfile registers to analyze
1073 * Analyze taskfile of @qc and further determine cause of
1074 * failure. This function also requests ATAPI sense data if
1078 * Kernel thread context (may sleep).
1081 * Determined recovery action
1083 static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1084 const struct ata_taskfile *tf)
1086 unsigned int tmp, action = 0;
1087 u8 stat = tf->command, err = tf->feature;
1089 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1090 qc->err_mask |= AC_ERR_HSM;
1091 return ATA_EH_SOFTRESET;
1094 if (!(qc->err_mask & AC_ERR_DEV))
1097 switch (qc->dev->class) {
1100 qc->err_mask |= AC_ERR_ATA_BUS;
1102 qc->err_mask |= AC_ERR_MEDIA;
1104 qc->err_mask |= AC_ERR_INVALID;
1108 tmp = atapi_eh_request_sense(qc->dev,
1109 qc->scsicmd->sense_buffer);
1111 /* ATA_QCFLAG_SENSE_VALID is used to tell
1112 * atapi_qc_complete() that sense data is
1115 * TODO: interpret sense data and set
1116 * appropriate err_mask.
1118 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1120 qc->err_mask |= tmp;
1123 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1124 action |= ATA_EH_SOFTRESET;
1129 static int ata_eh_categorize_ering_entry(struct ata_ering_entry *ent)
1131 if (ent->err_mask & (AC_ERR_ATA_BUS | AC_ERR_TIMEOUT))
1135 if (ent->err_mask & AC_ERR_HSM)
1137 if ((ent->err_mask &
1138 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1145 struct speed_down_needed_arg {
1150 static int speed_down_needed_cb(struct ata_ering_entry *ent, void *void_arg)
1152 struct speed_down_needed_arg *arg = void_arg;
1154 if (ent->timestamp < arg->since)
1157 arg->nr_errors[ata_eh_categorize_ering_entry(ent)]++;
1162 * ata_eh_speed_down_needed - Determine wheter speed down is necessary
1163 * @dev: Device of interest
1165 * This function examines error ring of @dev and determines
1166 * whether speed down is necessary. Speed down is necessary if
1167 * there have been more than 3 of Cat-1 errors or 10 of Cat-2
1168 * errors during last 15 minutes.
1170 * Cat-1 errors are ATA_BUS, TIMEOUT for any command and HSM
1171 * violation for known supported commands.
1173 * Cat-2 errors are unclassified DEV error for known supported
1177 * Inherited from caller.
1180 * 1 if speed down is necessary, 0 otherwise
1182 static int ata_eh_speed_down_needed(struct ata_device *dev)
1184 const u64 interval = 15LLU * 60 * HZ;
1185 static const int err_limits[3] = { -1, 3, 10 };
1186 struct speed_down_needed_arg arg;
1187 struct ata_ering_entry *ent;
1191 ent = ata_ering_top(&dev->ering);
1195 err_cat = ata_eh_categorize_ering_entry(ent);
1199 memset(&arg, 0, sizeof(arg));
1201 j64 = get_jiffies_64();
1202 if (j64 >= interval)
1203 arg.since = j64 - interval;
1207 ata_ering_map(&dev->ering, speed_down_needed_cb, &arg);
1209 return arg.nr_errors[err_cat] > err_limits[err_cat];
1213 * ata_eh_speed_down - record error and speed down if necessary
1214 * @dev: Failed device
1215 * @is_io: Did the device fail during normal IO?
1216 * @err_mask: err_mask of the error
1218 * Record error and examine error history to determine whether
1219 * adjusting transmission speed is necessary. It also sets
1220 * transmission limits appropriately if such adjustment is
1224 * Kernel thread context (may sleep).
1227 * 0 on success, -errno otherwise
1229 static int ata_eh_speed_down(struct ata_device *dev, int is_io,
1230 unsigned int err_mask)
1235 /* record error and determine whether speed down is necessary */
1236 ata_ering_record(&dev->ering, is_io, err_mask);
1238 if (!ata_eh_speed_down_needed(dev))
1241 /* speed down SATA link speed if possible */
1242 if (sata_down_spd_limit(dev->ap) == 0)
1243 return ATA_EH_HARDRESET;
1245 /* lower transfer mode */
1246 if (ata_down_xfermask_limit(dev, 0) == 0)
1247 return ATA_EH_SOFTRESET;
1249 ata_dev_printk(dev, KERN_ERR,
1250 "speed down requested but no transfer mode left\n");
1255 * ata_eh_autopsy - analyze error and determine recovery action
1256 * @ap: ATA port to perform autopsy on
1258 * Analyze why @ap failed and determine which recovery action is
1259 * needed. This function also sets more detailed AC_ERR_* values
1260 * and fills sense data for ATAPI CHECK SENSE.
1263 * Kernel thread context (may sleep).
1265 static void ata_eh_autopsy(struct ata_port *ap)
1267 struct ata_eh_context *ehc = &ap->eh_context;
1268 unsigned int action = ehc->i.action;
1269 struct ata_device *failed_dev = NULL;
1270 unsigned int all_err_mask = 0;
1277 /* obtain and analyze SError */
1278 rc = sata_scr_read(ap, SCR_ERROR, &serror);
1280 ehc->i.serror |= serror;
1281 ata_eh_analyze_serror(ap);
1282 } else if (rc != -EOPNOTSUPP)
1283 action |= ATA_EH_HARDRESET;
1285 /* analyze NCQ failure */
1286 ata_eh_analyze_ncq_error(ap);
1288 /* any real error trumps AC_ERR_OTHER */
1289 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1290 ehc->i.err_mask &= ~AC_ERR_OTHER;
1292 all_err_mask |= ehc->i.err_mask;
1294 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1295 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1297 if (!(qc->flags & ATA_QCFLAG_FAILED))
1300 /* inherit upper level err_mask */
1301 qc->err_mask |= ehc->i.err_mask;
1304 action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1306 /* DEV errors are probably spurious in case of ATA_BUS error */
1307 if (qc->err_mask & AC_ERR_ATA_BUS)
1308 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1311 /* any real error trumps unknown error */
1312 if (qc->err_mask & ~AC_ERR_OTHER)
1313 qc->err_mask &= ~AC_ERR_OTHER;
1315 /* SENSE_VALID trumps dev/unknown error and revalidation */
1316 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1317 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1318 action &= ~ATA_EH_REVALIDATE;
1321 /* accumulate error info */
1322 failed_dev = qc->dev;
1323 all_err_mask |= qc->err_mask;
1324 if (qc->flags & ATA_QCFLAG_IO)
1328 /* enforce default EH actions */
1329 if (ap->pflags & ATA_PFLAG_FROZEN ||
1330 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
1331 action |= ATA_EH_SOFTRESET;
1332 else if (all_err_mask)
1333 action |= ATA_EH_REVALIDATE;
1335 /* if we have offending qcs and the associated failed device */
1338 action |= ata_eh_speed_down(failed_dev, is_io, all_err_mask);
1340 /* perform per-dev EH action only on the offending device */
1341 ehc->i.dev_action[failed_dev->devno] |=
1342 action & ATA_EH_PERDEV_MASK;
1343 action &= ~ATA_EH_PERDEV_MASK;
1346 /* record autopsy result */
1347 ehc->i.dev = failed_dev;
1348 ehc->i.action |= action;
1354 * ata_eh_report - report error handling to user
1355 * @ap: ATA port EH is going on
1357 * Report EH to user.
1362 static void ata_eh_report(struct ata_port *ap)
1364 struct ata_eh_context *ehc = &ap->eh_context;
1365 const char *frozen, *desc;
1366 int tag, nr_failed = 0;
1369 if (ehc->i.desc[0] != '\0')
1372 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1373 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1375 if (!(qc->flags & ATA_QCFLAG_FAILED))
1377 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1383 if (!nr_failed && !ehc->i.err_mask)
1387 if (ap->pflags & ATA_PFLAG_FROZEN)
1391 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1392 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1393 ehc->i.err_mask, ap->sactive, ehc->i.serror,
1394 ehc->i.action, frozen);
1396 ata_dev_printk(ehc->i.dev, KERN_ERR, "(%s)\n", desc);
1398 ata_port_printk(ap, KERN_ERR, "exception Emask 0x%x "
1399 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1400 ehc->i.err_mask, ap->sactive, ehc->i.serror,
1401 ehc->i.action, frozen);
1403 ata_port_printk(ap, KERN_ERR, "(%s)\n", desc);
1406 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1407 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1409 if (!(qc->flags & ATA_QCFLAG_FAILED) || !qc->err_mask)
1412 ata_dev_printk(qc->dev, KERN_ERR, "tag %d cmd 0x%x "
1413 "Emask 0x%x stat 0x%x err 0x%x (%s)\n",
1414 qc->tag, qc->tf.command, qc->err_mask,
1415 qc->result_tf.command, qc->result_tf.feature,
1416 ata_err_string(qc->err_mask));
1420 static int ata_do_reset(struct ata_port *ap, ata_reset_fn_t reset,
1421 unsigned int *classes)
1425 for (i = 0; i < ATA_MAX_DEVICES; i++)
1426 classes[i] = ATA_DEV_UNKNOWN;
1428 rc = reset(ap, classes);
1432 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
1433 * is complete and convert all ATA_DEV_UNKNOWN to
1436 for (i = 0; i < ATA_MAX_DEVICES; i++)
1437 if (classes[i] != ATA_DEV_UNKNOWN)
1440 if (i < ATA_MAX_DEVICES)
1441 for (i = 0; i < ATA_MAX_DEVICES; i++)
1442 if (classes[i] == ATA_DEV_UNKNOWN)
1443 classes[i] = ATA_DEV_NONE;
1448 static int ata_eh_followup_srst_needed(int rc, int classify,
1449 const unsigned int *classes)
1455 if (classify && classes[0] == ATA_DEV_UNKNOWN)
1460 static int ata_eh_reset(struct ata_port *ap, int classify,
1461 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
1462 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
1464 struct ata_eh_context *ehc = &ap->eh_context;
1465 unsigned int *classes = ehc->classes;
1466 int tries = ATA_EH_RESET_TRIES;
1467 int verbose = !(ap->pflags & ATA_PFLAG_LOADING);
1468 unsigned int action;
1469 ata_reset_fn_t reset;
1470 int i, did_followup_srst, rc;
1472 /* Determine which reset to use and record in ehc->i.action.
1473 * prereset() may examine and modify it.
1475 action = ehc->i.action;
1476 ehc->i.action &= ~ATA_EH_RESET_MASK;
1477 if (softreset && (!hardreset || (!sata_set_spd_needed(ap) &&
1478 !(action & ATA_EH_HARDRESET))))
1479 ehc->i.action |= ATA_EH_SOFTRESET;
1481 ehc->i.action |= ATA_EH_HARDRESET;
1486 ata_port_printk(ap, KERN_ERR,
1487 "prereset failed (errno=%d)\n", rc);
1492 /* prereset() might have modified ehc->i.action */
1493 if (ehc->i.action & ATA_EH_HARDRESET)
1495 else if (ehc->i.action & ATA_EH_SOFTRESET)
1498 /* prereset told us not to reset, bang classes and return */
1499 for (i = 0; i < ATA_MAX_DEVICES; i++)
1500 classes[i] = ATA_DEV_NONE;
1504 /* did prereset() screw up? if so, fix up to avoid oopsing */
1506 ata_port_printk(ap, KERN_ERR, "BUG: prereset() requested "
1507 "invalid reset type\n");
1515 /* shut up during boot probing */
1517 ata_port_printk(ap, KERN_INFO, "%s resetting port\n",
1518 reset == softreset ? "soft" : "hard");
1521 ata_eh_about_to_do(ap, NULL, ATA_EH_RESET_MASK);
1522 ehc->i.flags |= ATA_EHI_DID_RESET;
1524 rc = ata_do_reset(ap, reset, classes);
1526 did_followup_srst = 0;
1527 if (reset == hardreset &&
1528 ata_eh_followup_srst_needed(rc, classify, classes)) {
1529 /* okay, let's do follow-up softreset */
1530 did_followup_srst = 1;
1534 ata_port_printk(ap, KERN_ERR,
1535 "follow-up softreset required "
1536 "but no softreset avaliable\n");
1540 ata_eh_about_to_do(ap, NULL, ATA_EH_RESET_MASK);
1541 rc = ata_do_reset(ap, reset, classes);
1543 if (rc == 0 && classify &&
1544 classes[0] == ATA_DEV_UNKNOWN) {
1545 ata_port_printk(ap, KERN_ERR,
1546 "classification failed\n");
1551 if (rc && --tries) {
1554 if (reset == softreset) {
1555 if (did_followup_srst)
1556 type = "follow-up soft";
1562 ata_port_printk(ap, KERN_WARNING,
1563 "%sreset failed, retrying in 5 secs\n", type);
1566 if (reset == hardreset)
1567 sata_down_spd_limit(ap);
1574 /* After the reset, the device state is PIO 0 and the
1575 * controller state is undefined. Record the mode.
1577 for (i = 0; i < ATA_MAX_DEVICES; i++)
1578 ap->device[i].pio_mode = XFER_PIO_0;
1581 postreset(ap, classes);
1583 /* reset successful, schedule revalidation */
1584 ata_eh_done(ap, NULL, ATA_EH_RESET_MASK);
1585 ehc->i.action |= ATA_EH_REVALIDATE;
1591 static int ata_eh_revalidate_and_attach(struct ata_port *ap,
1592 struct ata_device **r_failed_dev)
1594 struct ata_eh_context *ehc = &ap->eh_context;
1595 struct ata_device *dev;
1596 unsigned long flags;
1601 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1602 unsigned int action;
1604 dev = &ap->device[i];
1605 action = ata_eh_dev_action(dev);
1607 if (action & ATA_EH_REVALIDATE && ata_dev_enabled(dev)) {
1608 if (ata_port_offline(ap)) {
1613 ata_eh_about_to_do(ap, dev, ATA_EH_REVALIDATE);
1614 rc = ata_dev_revalidate(dev,
1615 ehc->i.flags & ATA_EHI_DID_RESET);
1619 ata_eh_done(ap, dev, ATA_EH_REVALIDATE);
1621 /* schedule the scsi_rescan_device() here */
1622 queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
1623 } else if (dev->class == ATA_DEV_UNKNOWN &&
1624 ehc->tries[dev->devno] &&
1625 ata_class_enabled(ehc->classes[dev->devno])) {
1626 dev->class = ehc->classes[dev->devno];
1628 rc = ata_dev_read_id(dev, &dev->class, 1, dev->id);
1630 rc = ata_dev_configure(dev, 1);
1633 dev->class = ATA_DEV_UNKNOWN;
1637 spin_lock_irqsave(ap->lock, flags);
1638 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
1639 spin_unlock_irqrestore(ap->lock, flags);
1644 *r_failed_dev = dev;
1650 static int ata_port_nr_enabled(struct ata_port *ap)
1654 for (i = 0; i < ATA_MAX_DEVICES; i++)
1655 if (ata_dev_enabled(&ap->device[i]))
1660 static int ata_port_nr_vacant(struct ata_port *ap)
1664 for (i = 0; i < ATA_MAX_DEVICES; i++)
1665 if (ap->device[i].class == ATA_DEV_UNKNOWN)
1670 static int ata_eh_skip_recovery(struct ata_port *ap)
1672 struct ata_eh_context *ehc = &ap->eh_context;
1675 if (ap->pflags & ATA_PFLAG_FROZEN || ata_port_nr_enabled(ap))
1678 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
1679 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1680 struct ata_device *dev = &ap->device[i];
1682 if (dev->class == ATA_DEV_UNKNOWN &&
1683 ehc->classes[dev->devno] != ATA_DEV_NONE)
1691 * ata_eh_recover - recover host port after error
1692 * @ap: host port to recover
1693 * @prereset: prereset method (can be NULL)
1694 * @softreset: softreset method (can be NULL)
1695 * @hardreset: hardreset method (can be NULL)
1696 * @postreset: postreset method (can be NULL)
1698 * This is the alpha and omega, eum and yang, heart and soul of
1699 * libata exception handling. On entry, actions required to
1700 * recover the port and hotplug requests are recorded in
1701 * eh_context. This function executes all the operations with
1702 * appropriate retrials and fallbacks to resurrect failed
1703 * devices, detach goners and greet newcomers.
1706 * Kernel thread context (may sleep).
1709 * 0 on success, -errno on failure.
1711 static int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
1712 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
1713 ata_postreset_fn_t postreset)
1715 struct ata_eh_context *ehc = &ap->eh_context;
1716 struct ata_device *dev;
1717 int down_xfermask, i, rc;
1721 /* prep for recovery */
1722 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1723 dev = &ap->device[i];
1725 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
1727 /* process hotplug request */
1728 if (dev->flags & ATA_DFLAG_DETACH)
1729 ata_eh_detach_dev(dev);
1731 if (!ata_dev_enabled(dev) &&
1732 ((ehc->i.probe_mask & (1 << dev->devno)) &&
1733 !(ehc->did_probe_mask & (1 << dev->devno)))) {
1734 ata_eh_detach_dev(dev);
1736 ehc->did_probe_mask |= (1 << dev->devno);
1737 ehc->i.action |= ATA_EH_SOFTRESET;
1745 /* if UNLOADING, finish immediately */
1746 if (ap->pflags & ATA_PFLAG_UNLOADING)
1749 /* skip EH if possible. */
1750 if (ata_eh_skip_recovery(ap))
1753 for (i = 0; i < ATA_MAX_DEVICES; i++)
1754 ehc->classes[i] = ATA_DEV_UNKNOWN;
1757 if (ehc->i.action & ATA_EH_RESET_MASK) {
1758 ata_eh_freeze_port(ap);
1760 rc = ata_eh_reset(ap, ata_port_nr_vacant(ap), prereset,
1761 softreset, hardreset, postreset);
1763 ata_port_printk(ap, KERN_ERR,
1764 "reset failed, giving up\n");
1768 ata_eh_thaw_port(ap);
1771 /* revalidate existing devices and attach new ones */
1772 rc = ata_eh_revalidate_and_attach(ap, &dev);
1776 /* configure transfer mode if the port has been reset */
1777 if (ehc->i.flags & ATA_EHI_DID_RESET) {
1778 rc = ata_set_mode(ap, &dev);
1790 /* device missing, schedule probing */
1791 ehc->i.probe_mask |= (1 << dev->devno);
1793 ehc->tries[dev->devno] = 0;
1796 sata_down_spd_limit(ap);
1798 ehc->tries[dev->devno]--;
1799 if (down_xfermask &&
1800 ata_down_xfermask_limit(dev, ehc->tries[dev->devno] == 1))
1801 ehc->tries[dev->devno] = 0;
1804 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
1805 /* disable device if it has used up all its chances */
1806 ata_dev_disable(dev);
1808 /* detach if offline */
1809 if (ata_port_offline(ap))
1810 ata_eh_detach_dev(dev);
1812 /* probe if requested */
1813 if ((ehc->i.probe_mask & (1 << dev->devno)) &&
1814 !(ehc->did_probe_mask & (1 << dev->devno))) {
1815 ata_eh_detach_dev(dev);
1818 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
1819 ehc->did_probe_mask |= (1 << dev->devno);
1820 ehc->i.action |= ATA_EH_SOFTRESET;
1823 /* soft didn't work? be haaaaard */
1824 if (ehc->i.flags & ATA_EHI_DID_RESET)
1825 ehc->i.action |= ATA_EH_HARDRESET;
1827 ehc->i.action |= ATA_EH_SOFTRESET;
1830 if (ata_port_nr_enabled(ap)) {
1831 ata_port_printk(ap, KERN_WARNING, "failed to recover some "
1832 "devices, retrying in 5 secs\n");
1835 /* no device left, repeat fast */
1843 for (i = 0; i < ATA_MAX_DEVICES; i++)
1844 ata_dev_disable(&ap->device[i]);
1847 DPRINTK("EXIT, rc=%d\n", rc);
1852 * ata_eh_finish - finish up EH
1853 * @ap: host port to finish EH for
1855 * Recovery is complete. Clean up EH states and retry or finish
1861 static void ata_eh_finish(struct ata_port *ap)
1865 /* retry or finish qcs */
1866 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1867 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1869 if (!(qc->flags & ATA_QCFLAG_FAILED))
1873 /* FIXME: Once EH migration is complete,
1874 * generate sense data in this function,
1875 * considering both err_mask and tf.
1877 if (qc->err_mask & AC_ERR_INVALID)
1878 ata_eh_qc_complete(qc);
1880 ata_eh_qc_retry(qc);
1882 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1883 ata_eh_qc_complete(qc);
1885 /* feed zero TF to sense generation */
1886 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
1887 ata_eh_qc_retry(qc);
1894 * ata_do_eh - do standard error handling
1895 * @ap: host port to handle error for
1896 * @prereset: prereset method (can be NULL)
1897 * @softreset: softreset method (can be NULL)
1898 * @hardreset: hardreset method (can be NULL)
1899 * @postreset: postreset method (can be NULL)
1901 * Perform standard error handling sequence.
1904 * Kernel thread context (may sleep).
1906 void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
1907 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
1908 ata_postreset_fn_t postreset)
1910 if (!(ap->pflags & ATA_PFLAG_LOADING)) {
1915 ata_eh_recover(ap, prereset, softreset, hardreset, postreset);