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);
50 static void ata_eh_handle_port_suspend(struct ata_port *ap);
51 static void ata_eh_handle_port_resume(struct ata_port *ap);
53 static void ata_ering_record(struct ata_ering *ering, int is_io,
54 unsigned int err_mask)
56 struct ata_ering_entry *ent;
61 ering->cursor %= ATA_ERING_SIZE;
63 ent = &ering->ring[ering->cursor];
65 ent->err_mask = err_mask;
66 ent->timestamp = get_jiffies_64();
69 static struct ata_ering_entry * ata_ering_top(struct ata_ering *ering)
71 struct ata_ering_entry *ent = &ering->ring[ering->cursor];
77 static int ata_ering_map(struct ata_ering *ering,
78 int (*map_fn)(struct ata_ering_entry *, void *),
82 struct ata_ering_entry *ent;
86 ent = &ering->ring[idx];
89 rc = map_fn(ent, arg);
92 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
93 } while (idx != ering->cursor);
98 static unsigned int ata_eh_dev_action(struct ata_device *dev)
100 struct ata_eh_context *ehc = &dev->ap->eh_context;
102 return ehc->i.action | ehc->i.dev_action[dev->devno];
105 static void ata_eh_clear_action(struct ata_device *dev,
106 struct ata_eh_info *ehi, unsigned int action)
111 ehi->action &= ~action;
112 for (i = 0; i < ATA_MAX_DEVICES; i++)
113 ehi->dev_action[i] &= ~action;
115 /* doesn't make sense for port-wide EH actions */
116 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
118 /* break ehi->action into ehi->dev_action */
119 if (ehi->action & action) {
120 for (i = 0; i < ATA_MAX_DEVICES; i++)
121 ehi->dev_action[i] |= ehi->action & action;
122 ehi->action &= ~action;
125 /* turn off the specified per-dev action */
126 ehi->dev_action[dev->devno] &= ~action;
131 * ata_scsi_timed_out - SCSI layer time out callback
132 * @cmd: timed out SCSI command
134 * Handles SCSI layer timeout. We race with normal completion of
135 * the qc for @cmd. If the qc is already gone, we lose and let
136 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
137 * timed out and EH should be invoked. Prevent ata_qc_complete()
138 * from finishing it by setting EH_SCHEDULED and return
141 * TODO: kill this function once old EH is gone.
144 * Called from timer context
147 * EH_HANDLED or EH_NOT_HANDLED
149 enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
151 struct Scsi_Host *host = cmd->device->host;
152 struct ata_port *ap = ata_shost_to_port(host);
154 struct ata_queued_cmd *qc;
155 enum scsi_eh_timer_return ret;
159 if (ap->ops->error_handler) {
160 ret = EH_NOT_HANDLED;
165 spin_lock_irqsave(ap->lock, flags);
166 qc = ata_qc_from_tag(ap, ap->active_tag);
168 WARN_ON(qc->scsicmd != cmd);
169 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
170 qc->err_mask |= AC_ERR_TIMEOUT;
171 ret = EH_NOT_HANDLED;
173 spin_unlock_irqrestore(ap->lock, flags);
176 DPRINTK("EXIT, ret=%d\n", ret);
181 * ata_scsi_error - SCSI layer error handler callback
182 * @host: SCSI host on which error occurred
184 * Handles SCSI-layer-thrown error events.
187 * Inherited from SCSI layer (none, can sleep)
192 void ata_scsi_error(struct Scsi_Host *host)
194 struct ata_port *ap = ata_shost_to_port(host);
195 int i, repeat_cnt = ATA_EH_MAX_REPEAT;
200 /* synchronize with port task */
201 ata_port_flush_task(ap);
203 /* synchronize with host_set lock and sort out timeouts */
205 /* For new EH, all qcs are finished in one of three ways -
206 * normal completion, error completion, and SCSI timeout.
207 * Both cmpletions can race against SCSI timeout. When normal
208 * completion wins, the qc never reaches EH. When error
209 * completion wins, the qc has ATA_QCFLAG_FAILED set.
211 * When SCSI timeout wins, things are a bit more complex.
212 * Normal or error completion can occur after the timeout but
213 * before this point. In such cases, both types of
214 * completions are honored. A scmd is determined to have
215 * timed out iff its associated qc is active and not failed.
217 if (ap->ops->error_handler) {
218 struct scsi_cmnd *scmd, *tmp;
221 spin_lock_irqsave(ap->lock, flags);
223 list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
224 struct ata_queued_cmd *qc;
226 for (i = 0; i < ATA_MAX_QUEUE; i++) {
227 qc = __ata_qc_from_tag(ap, i);
228 if (qc->flags & ATA_QCFLAG_ACTIVE &&
233 if (i < ATA_MAX_QUEUE) {
234 /* the scmd has an associated qc */
235 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
236 /* which hasn't failed yet, timeout */
237 qc->err_mask |= AC_ERR_TIMEOUT;
238 qc->flags |= ATA_QCFLAG_FAILED;
242 /* Normal completion occurred after
243 * SCSI timeout but before this point.
244 * Successfully complete it.
246 scmd->retries = scmd->allowed;
247 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
251 /* If we have timed out qcs. They belong to EH from
252 * this point but the state of the controller is
253 * unknown. Freeze the port to make sure the IRQ
254 * handler doesn't diddle with those qcs. This must
255 * be done atomically w.r.t. setting QCFLAG_FAILED.
258 __ata_port_freeze(ap);
260 spin_unlock_irqrestore(ap->lock, flags);
262 spin_unlock_wait(ap->lock);
265 /* invoke error handler */
266 if (ap->ops->error_handler) {
267 /* process port resume request */
268 ata_eh_handle_port_resume(ap);
270 /* fetch & clear EH info */
271 spin_lock_irqsave(ap->lock, flags);
273 memset(&ap->eh_context, 0, sizeof(ap->eh_context));
274 ap->eh_context.i = ap->eh_info;
275 memset(&ap->eh_info, 0, sizeof(ap->eh_info));
277 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
278 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
280 spin_unlock_irqrestore(ap->lock, flags);
282 /* invoke EH, skip if unloading or suspended */
283 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
284 ap->ops->error_handler(ap);
288 /* process port suspend request */
289 ata_eh_handle_port_suspend(ap);
291 /* Exception might have happend after ->error_handler
292 * recovered the port but before this point. Repeat
295 spin_lock_irqsave(ap->lock, flags);
297 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
299 ata_port_printk(ap, KERN_INFO,
300 "EH pending after completion, "
301 "repeating EH (cnt=%d)\n", repeat_cnt);
302 spin_unlock_irqrestore(ap->lock, flags);
305 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
306 "tries, giving up\n", ATA_EH_MAX_REPEAT);
309 /* this run is complete, make sure EH info is clear */
310 memset(&ap->eh_info, 0, sizeof(ap->eh_info));
312 /* Clear host_eh_scheduled while holding ap->lock such
313 * that if exception occurs after this point but
314 * before EH completion, SCSI midlayer will
317 host->host_eh_scheduled = 0;
319 spin_unlock_irqrestore(ap->lock, flags);
321 WARN_ON(ata_qc_from_tag(ap, ap->active_tag) == NULL);
322 ap->ops->eng_timeout(ap);
325 /* finish or retry handled scmd's and clean up */
326 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
328 scsi_eh_flush_done_q(&ap->eh_done_q);
331 spin_lock_irqsave(ap->lock, flags);
333 if (ap->pflags & ATA_PFLAG_LOADING)
334 ap->pflags &= ~ATA_PFLAG_LOADING;
335 else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
336 queue_work(ata_aux_wq, &ap->hotplug_task);
338 if (ap->pflags & ATA_PFLAG_RECOVERED)
339 ata_port_printk(ap, KERN_INFO, "EH complete\n");
341 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
343 /* tell wait_eh that we're done */
344 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
345 wake_up_all(&ap->eh_wait_q);
347 spin_unlock_irqrestore(ap->lock, flags);
353 * ata_port_wait_eh - Wait for the currently pending EH to complete
354 * @ap: Port to wait EH for
356 * Wait until the currently pending EH is complete.
359 * Kernel thread context (may sleep).
361 void ata_port_wait_eh(struct ata_port *ap)
367 spin_lock_irqsave(ap->lock, flags);
369 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
370 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
371 spin_unlock_irqrestore(ap->lock, flags);
373 spin_lock_irqsave(ap->lock, flags);
375 finish_wait(&ap->eh_wait_q, &wait);
377 spin_unlock_irqrestore(ap->lock, flags);
379 /* make sure SCSI EH is complete */
380 if (scsi_host_in_recovery(ap->host)) {
387 * ata_qc_timeout - Handle timeout of queued command
388 * @qc: Command that timed out
390 * Some part of the kernel (currently, only the SCSI layer)
391 * has noticed that the active command on port @ap has not
392 * completed after a specified length of time. Handle this
393 * condition by disabling DMA (if necessary) and completing
394 * transactions, with error if necessary.
396 * This also handles the case of the "lost interrupt", where
397 * for some reason (possibly hardware bug, possibly driver bug)
398 * an interrupt was not delivered to the driver, even though the
399 * transaction completed successfully.
401 * TODO: kill this function once old EH is gone.
404 * Inherited from SCSI layer (none, can sleep)
406 static void ata_qc_timeout(struct ata_queued_cmd *qc)
408 struct ata_port *ap = qc->ap;
409 u8 host_stat = 0, drv_stat;
414 ap->hsm_task_state = HSM_ST_IDLE;
416 spin_lock_irqsave(ap->lock, flags);
418 switch (qc->tf.protocol) {
421 case ATA_PROT_ATAPI_DMA:
422 host_stat = ap->ops->bmdma_status(ap);
424 /* before we do anything else, clear DMA-Start bit */
425 ap->ops->bmdma_stop(qc);
431 drv_stat = ata_chk_status(ap);
433 /* ack bmdma irq events */
434 ap->ops->irq_clear(ap);
436 ata_dev_printk(qc->dev, KERN_ERR, "command 0x%x timeout, "
437 "stat 0x%x host_stat 0x%x\n",
438 qc->tf.command, drv_stat, host_stat);
440 /* complete taskfile transaction */
441 qc->err_mask |= AC_ERR_TIMEOUT;
445 spin_unlock_irqrestore(ap->lock, flags);
447 ata_eh_qc_complete(qc);
453 * ata_eng_timeout - Handle timeout of queued command
454 * @ap: Port on which timed-out command is active
456 * Some part of the kernel (currently, only the SCSI layer)
457 * has noticed that the active command on port @ap has not
458 * completed after a specified length of time. Handle this
459 * condition by disabling DMA (if necessary) and completing
460 * transactions, with error if necessary.
462 * This also handles the case of the "lost interrupt", where
463 * for some reason (possibly hardware bug, possibly driver bug)
464 * an interrupt was not delivered to the driver, even though the
465 * transaction completed successfully.
467 * TODO: kill this function once old EH is gone.
470 * Inherited from SCSI layer (none, can sleep)
472 void ata_eng_timeout(struct ata_port *ap)
476 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
482 * ata_qc_schedule_eh - schedule qc for error handling
483 * @qc: command to schedule error handling for
485 * Schedule error handling for @qc. EH will kick in as soon as
486 * other commands are drained.
489 * spin_lock_irqsave(host_set lock)
491 void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
493 struct ata_port *ap = qc->ap;
495 WARN_ON(!ap->ops->error_handler);
497 qc->flags |= ATA_QCFLAG_FAILED;
498 qc->ap->pflags |= ATA_PFLAG_EH_PENDING;
500 /* The following will fail if timeout has already expired.
501 * ata_scsi_error() takes care of such scmds on EH entry.
502 * Note that ATA_QCFLAG_FAILED is unconditionally set after
503 * this function completes.
505 scsi_req_abort_cmd(qc->scsicmd);
509 * ata_port_schedule_eh - schedule error handling without a qc
510 * @ap: ATA port to schedule EH for
512 * Schedule error handling for @ap. EH will kick in as soon as
513 * all commands are drained.
516 * spin_lock_irqsave(host_set lock)
518 void ata_port_schedule_eh(struct ata_port *ap)
520 WARN_ON(!ap->ops->error_handler);
522 ap->pflags |= ATA_PFLAG_EH_PENDING;
523 scsi_schedule_eh(ap->host);
525 DPRINTK("port EH scheduled\n");
529 * ata_port_abort - abort all qc's on the port
530 * @ap: ATA port to abort qc's for
532 * Abort all active qc's of @ap and schedule EH.
535 * spin_lock_irqsave(host_set lock)
538 * Number of aborted qc's.
540 int ata_port_abort(struct ata_port *ap)
542 int tag, nr_aborted = 0;
544 WARN_ON(!ap->ops->error_handler);
546 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
547 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
550 qc->flags |= ATA_QCFLAG_FAILED;
557 ata_port_schedule_eh(ap);
563 * __ata_port_freeze - freeze port
564 * @ap: ATA port to freeze
566 * This function is called when HSM violation or some other
567 * condition disrupts normal operation of the port. Frozen port
568 * is not allowed to perform any operation until the port is
569 * thawed, which usually follows a successful reset.
571 * ap->ops->freeze() callback can be used for freezing the port
572 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
573 * port cannot be frozen hardware-wise, the interrupt handler
574 * must ack and clear interrupts unconditionally while the port
578 * spin_lock_irqsave(host_set lock)
580 static void __ata_port_freeze(struct ata_port *ap)
582 WARN_ON(!ap->ops->error_handler);
587 ap->pflags |= ATA_PFLAG_FROZEN;
589 DPRINTK("ata%u port frozen\n", ap->id);
593 * ata_port_freeze - abort & freeze port
594 * @ap: ATA port to freeze
596 * Abort and freeze @ap.
599 * spin_lock_irqsave(host_set lock)
602 * Number of aborted commands.
604 int ata_port_freeze(struct ata_port *ap)
608 WARN_ON(!ap->ops->error_handler);
610 nr_aborted = ata_port_abort(ap);
611 __ata_port_freeze(ap);
617 * ata_eh_freeze_port - EH helper to freeze port
618 * @ap: ATA port to freeze
625 void ata_eh_freeze_port(struct ata_port *ap)
629 if (!ap->ops->error_handler)
632 spin_lock_irqsave(ap->lock, flags);
633 __ata_port_freeze(ap);
634 spin_unlock_irqrestore(ap->lock, flags);
638 * ata_port_thaw_port - EH helper to thaw port
639 * @ap: ATA port to thaw
641 * Thaw frozen port @ap.
646 void ata_eh_thaw_port(struct ata_port *ap)
650 if (!ap->ops->error_handler)
653 spin_lock_irqsave(ap->lock, flags);
655 ap->pflags &= ~ATA_PFLAG_FROZEN;
660 spin_unlock_irqrestore(ap->lock, flags);
662 DPRINTK("ata%u port thawed\n", ap->id);
665 static void ata_eh_scsidone(struct scsi_cmnd *scmd)
670 static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
672 struct ata_port *ap = qc->ap;
673 struct scsi_cmnd *scmd = qc->scsicmd;
676 spin_lock_irqsave(ap->lock, flags);
677 qc->scsidone = ata_eh_scsidone;
678 __ata_qc_complete(qc);
679 WARN_ON(ata_tag_valid(qc->tag));
680 spin_unlock_irqrestore(ap->lock, flags);
682 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
686 * ata_eh_qc_complete - Complete an active ATA command from EH
687 * @qc: Command to complete
689 * Indicate to the mid and upper layers that an ATA command has
690 * completed. To be used from EH.
692 void ata_eh_qc_complete(struct ata_queued_cmd *qc)
694 struct scsi_cmnd *scmd = qc->scsicmd;
695 scmd->retries = scmd->allowed;
696 __ata_eh_qc_complete(qc);
700 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
701 * @qc: Command to retry
703 * Indicate to the mid and upper layers that an ATA command
704 * should be retried. To be used from EH.
706 * SCSI midlayer limits the number of retries to scmd->allowed.
707 * scmd->retries is decremented for commands which get retried
708 * due to unrelated failures (qc->err_mask is zero).
710 void ata_eh_qc_retry(struct ata_queued_cmd *qc)
712 struct scsi_cmnd *scmd = qc->scsicmd;
713 if (!qc->err_mask && scmd->retries)
715 __ata_eh_qc_complete(qc);
719 * ata_eh_detach_dev - detach ATA device
720 * @dev: ATA device to detach
727 static void ata_eh_detach_dev(struct ata_device *dev)
729 struct ata_port *ap = dev->ap;
732 ata_dev_disable(dev);
734 spin_lock_irqsave(ap->lock, flags);
736 dev->flags &= ~ATA_DFLAG_DETACH;
738 if (ata_scsi_offline_dev(dev)) {
739 dev->flags |= ATA_DFLAG_DETACHED;
740 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
743 /* clear per-dev EH actions */
744 ata_eh_clear_action(dev, &ap->eh_info, ATA_EH_PERDEV_MASK);
745 ata_eh_clear_action(dev, &ap->eh_context.i, ATA_EH_PERDEV_MASK);
747 spin_unlock_irqrestore(ap->lock, flags);
751 * ata_eh_about_to_do - about to perform eh_action
752 * @ap: target ATA port
753 * @dev: target ATA dev for per-dev action (can be NULL)
754 * @action: action about to be performed
756 * Called just before performing EH actions to clear related bits
757 * in @ap->eh_info such that eh actions are not unnecessarily
763 static void ata_eh_about_to_do(struct ata_port *ap, struct ata_device *dev,
768 spin_lock_irqsave(ap->lock, flags);
770 ata_eh_clear_action(dev, &ap->eh_info, action);
772 if (!(ap->eh_context.i.flags & ATA_EHI_QUIET))
773 ap->pflags |= ATA_PFLAG_RECOVERED;
775 spin_unlock_irqrestore(ap->lock, flags);
779 * ata_eh_done - EH action complete
780 * @ap: target ATA port
781 * @dev: target ATA dev for per-dev action (can be NULL)
782 * @action: action just completed
784 * Called right after performing EH actions to clear related bits
785 * in @ap->eh_context.
790 static void ata_eh_done(struct ata_port *ap, struct ata_device *dev,
793 ata_eh_clear_action(dev, &ap->eh_context.i, action);
797 * ata_err_string - convert err_mask to descriptive string
798 * @err_mask: error mask to convert to string
800 * Convert @err_mask to descriptive string. Errors are
801 * prioritized according to severity and only the most severe
808 * Descriptive string for @err_mask
810 static const char * ata_err_string(unsigned int err_mask)
812 if (err_mask & AC_ERR_HOST_BUS)
813 return "host bus error";
814 if (err_mask & AC_ERR_ATA_BUS)
815 return "ATA bus error";
816 if (err_mask & AC_ERR_TIMEOUT)
818 if (err_mask & AC_ERR_HSM)
819 return "HSM violation";
820 if (err_mask & AC_ERR_SYSTEM)
821 return "internal error";
822 if (err_mask & AC_ERR_MEDIA)
823 return "media error";
824 if (err_mask & AC_ERR_INVALID)
825 return "invalid argument";
826 if (err_mask & AC_ERR_DEV)
827 return "device error";
828 return "unknown error";
832 * ata_read_log_page - read a specific log page
833 * @dev: target device
834 * @page: page to read
835 * @buf: buffer to store read page
836 * @sectors: number of sectors to read
838 * Read log page using READ_LOG_EXT command.
841 * Kernel thread context (may sleep).
844 * 0 on success, AC_ERR_* mask otherwise.
846 static unsigned int ata_read_log_page(struct ata_device *dev,
847 u8 page, void *buf, unsigned int sectors)
849 struct ata_taskfile tf;
850 unsigned int err_mask;
852 DPRINTK("read log page - page %d\n", page);
854 ata_tf_init(dev, &tf);
855 tf.command = ATA_CMD_READ_LOG_EXT;
858 tf.hob_nsect = sectors >> 8;
859 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
860 tf.protocol = ATA_PROT_PIO;
862 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
863 buf, sectors * ATA_SECT_SIZE);
865 DPRINTK("EXIT, err_mask=%x\n", err_mask);
870 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
871 * @dev: Device to read log page 10h from
872 * @tag: Resulting tag of the failed command
873 * @tf: Resulting taskfile registers of the failed command
875 * Read log page 10h to obtain NCQ error details and clear error
879 * Kernel thread context (may sleep).
882 * 0 on success, -errno otherwise.
884 static int ata_eh_read_log_10h(struct ata_device *dev,
885 int *tag, struct ata_taskfile *tf)
887 u8 *buf = dev->ap->sector_buf;
888 unsigned int err_mask;
892 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
897 for (i = 0; i < ATA_SECT_SIZE; i++)
900 ata_dev_printk(dev, KERN_WARNING,
901 "invalid checksum 0x%x on log page 10h\n", csum);
906 *tag = buf[0] & 0x1f;
908 tf->command = buf[2];
909 tf->feature = buf[3];
914 tf->hob_lbal = buf[8];
915 tf->hob_lbam = buf[9];
916 tf->hob_lbah = buf[10];
918 tf->hob_nsect = buf[13];
924 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
925 * @dev: device to perform REQUEST_SENSE to
926 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
928 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
929 * SENSE. This function is EH helper.
932 * Kernel thread context (may sleep).
935 * 0 on success, AC_ERR_* mask on failure
937 static unsigned int atapi_eh_request_sense(struct ata_device *dev,
938 unsigned char *sense_buf)
940 struct ata_port *ap = dev->ap;
941 struct ata_taskfile tf;
942 u8 cdb[ATAPI_CDB_LEN];
944 DPRINTK("ATAPI request sense\n");
946 ata_tf_init(dev, &tf);
948 /* FIXME: is this needed? */
949 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
951 /* XXX: why tf_read here? */
952 ap->ops->tf_read(ap, &tf);
954 /* fill these in, for the case where they are -not- overwritten */
956 sense_buf[2] = tf.feature >> 4;
958 memset(cdb, 0, ATAPI_CDB_LEN);
959 cdb[0] = REQUEST_SENSE;
960 cdb[4] = SCSI_SENSE_BUFFERSIZE;
962 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
963 tf.command = ATA_CMD_PACKET;
965 /* is it pointless to prefer PIO for "safety reasons"? */
966 if (ap->flags & ATA_FLAG_PIO_DMA) {
967 tf.protocol = ATA_PROT_ATAPI_DMA;
968 tf.feature |= ATAPI_PKT_DMA;
970 tf.protocol = ATA_PROT_ATAPI;
971 tf.lbam = (8 * 1024) & 0xff;
972 tf.lbah = (8 * 1024) >> 8;
975 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
976 sense_buf, SCSI_SENSE_BUFFERSIZE);
980 * ata_eh_analyze_serror - analyze SError for a failed port
981 * @ap: ATA port to analyze SError for
983 * Analyze SError if available and further determine cause of
989 static void ata_eh_analyze_serror(struct ata_port *ap)
991 struct ata_eh_context *ehc = &ap->eh_context;
992 u32 serror = ehc->i.serror;
993 unsigned int err_mask = 0, action = 0;
995 if (serror & SERR_PERSISTENT) {
996 err_mask |= AC_ERR_ATA_BUS;
997 action |= ATA_EH_HARDRESET;
1000 (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
1001 err_mask |= AC_ERR_ATA_BUS;
1002 action |= ATA_EH_SOFTRESET;
1004 if (serror & SERR_PROTOCOL) {
1005 err_mask |= AC_ERR_HSM;
1006 action |= ATA_EH_SOFTRESET;
1008 if (serror & SERR_INTERNAL) {
1009 err_mask |= AC_ERR_SYSTEM;
1010 action |= ATA_EH_SOFTRESET;
1012 if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG))
1013 ata_ehi_hotplugged(&ehc->i);
1015 ehc->i.err_mask |= err_mask;
1016 ehc->i.action |= action;
1020 * ata_eh_analyze_ncq_error - analyze NCQ error
1021 * @ap: ATA port to analyze NCQ error for
1023 * Read log page 10h, determine the offending qc and acquire
1024 * error status TF. For NCQ device errors, all LLDDs have to do
1025 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1029 * Kernel thread context (may sleep).
1031 static void ata_eh_analyze_ncq_error(struct ata_port *ap)
1033 struct ata_eh_context *ehc = &ap->eh_context;
1034 struct ata_device *dev = ap->device;
1035 struct ata_queued_cmd *qc;
1036 struct ata_taskfile tf;
1039 /* if frozen, we can't do much */
1040 if (ap->pflags & ATA_PFLAG_FROZEN)
1043 /* is it NCQ device error? */
1044 if (!ap->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1047 /* has LLDD analyzed already? */
1048 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1049 qc = __ata_qc_from_tag(ap, tag);
1051 if (!(qc->flags & ATA_QCFLAG_FAILED))
1058 /* okay, this error is ours */
1059 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1061 ata_port_printk(ap, KERN_ERR, "failed to read log page 10h "
1062 "(errno=%d)\n", rc);
1066 if (!(ap->sactive & (1 << tag))) {
1067 ata_port_printk(ap, KERN_ERR, "log page 10h reported "
1068 "inactive tag %d\n", tag);
1072 /* we've got the perpetrator, condemn it */
1073 qc = __ata_qc_from_tag(ap, tag);
1074 memcpy(&qc->result_tf, &tf, sizeof(tf));
1075 qc->err_mask |= AC_ERR_DEV;
1076 ehc->i.err_mask &= ~AC_ERR_DEV;
1080 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1081 * @qc: qc to analyze
1082 * @tf: Taskfile registers to analyze
1084 * Analyze taskfile of @qc and further determine cause of
1085 * failure. This function also requests ATAPI sense data if
1089 * Kernel thread context (may sleep).
1092 * Determined recovery action
1094 static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1095 const struct ata_taskfile *tf)
1097 unsigned int tmp, action = 0;
1098 u8 stat = tf->command, err = tf->feature;
1100 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1101 qc->err_mask |= AC_ERR_HSM;
1102 return ATA_EH_SOFTRESET;
1105 if (!(qc->err_mask & AC_ERR_DEV))
1108 switch (qc->dev->class) {
1111 qc->err_mask |= AC_ERR_ATA_BUS;
1113 qc->err_mask |= AC_ERR_MEDIA;
1115 qc->err_mask |= AC_ERR_INVALID;
1119 tmp = atapi_eh_request_sense(qc->dev,
1120 qc->scsicmd->sense_buffer);
1122 /* ATA_QCFLAG_SENSE_VALID is used to tell
1123 * atapi_qc_complete() that sense data is
1126 * TODO: interpret sense data and set
1127 * appropriate err_mask.
1129 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1131 qc->err_mask |= tmp;
1134 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1135 action |= ATA_EH_SOFTRESET;
1140 static int ata_eh_categorize_ering_entry(struct ata_ering_entry *ent)
1142 if (ent->err_mask & (AC_ERR_ATA_BUS | AC_ERR_TIMEOUT))
1146 if (ent->err_mask & AC_ERR_HSM)
1148 if ((ent->err_mask &
1149 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1156 struct speed_down_needed_arg {
1161 static int speed_down_needed_cb(struct ata_ering_entry *ent, void *void_arg)
1163 struct speed_down_needed_arg *arg = void_arg;
1165 if (ent->timestamp < arg->since)
1168 arg->nr_errors[ata_eh_categorize_ering_entry(ent)]++;
1173 * ata_eh_speed_down_needed - Determine wheter speed down is necessary
1174 * @dev: Device of interest
1176 * This function examines error ring of @dev and determines
1177 * whether speed down is necessary. Speed down is necessary if
1178 * there have been more than 3 of Cat-1 errors or 10 of Cat-2
1179 * errors during last 15 minutes.
1181 * Cat-1 errors are ATA_BUS, TIMEOUT for any command and HSM
1182 * violation for known supported commands.
1184 * Cat-2 errors are unclassified DEV error for known supported
1188 * Inherited from caller.
1191 * 1 if speed down is necessary, 0 otherwise
1193 static int ata_eh_speed_down_needed(struct ata_device *dev)
1195 const u64 interval = 15LLU * 60 * HZ;
1196 static const int err_limits[3] = { -1, 3, 10 };
1197 struct speed_down_needed_arg arg;
1198 struct ata_ering_entry *ent;
1202 ent = ata_ering_top(&dev->ering);
1206 err_cat = ata_eh_categorize_ering_entry(ent);
1210 memset(&arg, 0, sizeof(arg));
1212 j64 = get_jiffies_64();
1213 if (j64 >= interval)
1214 arg.since = j64 - interval;
1218 ata_ering_map(&dev->ering, speed_down_needed_cb, &arg);
1220 return arg.nr_errors[err_cat] > err_limits[err_cat];
1224 * ata_eh_speed_down - record error and speed down if necessary
1225 * @dev: Failed device
1226 * @is_io: Did the device fail during normal IO?
1227 * @err_mask: err_mask of the error
1229 * Record error and examine error history to determine whether
1230 * adjusting transmission speed is necessary. It also sets
1231 * transmission limits appropriately if such adjustment is
1235 * Kernel thread context (may sleep).
1238 * 0 on success, -errno otherwise
1240 static int ata_eh_speed_down(struct ata_device *dev, int is_io,
1241 unsigned int err_mask)
1246 /* record error and determine whether speed down is necessary */
1247 ata_ering_record(&dev->ering, is_io, err_mask);
1249 if (!ata_eh_speed_down_needed(dev))
1252 /* speed down SATA link speed if possible */
1253 if (sata_down_spd_limit(dev->ap) == 0)
1254 return ATA_EH_HARDRESET;
1256 /* lower transfer mode */
1257 if (ata_down_xfermask_limit(dev, 0) == 0)
1258 return ATA_EH_SOFTRESET;
1260 ata_dev_printk(dev, KERN_ERR,
1261 "speed down requested but no transfer mode left\n");
1266 * ata_eh_autopsy - analyze error and determine recovery action
1267 * @ap: ATA port to perform autopsy on
1269 * Analyze why @ap failed and determine which recovery action is
1270 * needed. This function also sets more detailed AC_ERR_* values
1271 * and fills sense data for ATAPI CHECK SENSE.
1274 * Kernel thread context (may sleep).
1276 static void ata_eh_autopsy(struct ata_port *ap)
1278 struct ata_eh_context *ehc = &ap->eh_context;
1279 unsigned int action = ehc->i.action;
1280 struct ata_device *failed_dev = NULL;
1281 unsigned int all_err_mask = 0;
1288 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1291 /* obtain and analyze SError */
1292 rc = sata_scr_read(ap, SCR_ERROR, &serror);
1294 ehc->i.serror |= serror;
1295 ata_eh_analyze_serror(ap);
1296 } else if (rc != -EOPNOTSUPP)
1297 action |= ATA_EH_HARDRESET;
1299 /* analyze NCQ failure */
1300 ata_eh_analyze_ncq_error(ap);
1302 /* any real error trumps AC_ERR_OTHER */
1303 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1304 ehc->i.err_mask &= ~AC_ERR_OTHER;
1306 all_err_mask |= ehc->i.err_mask;
1308 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1309 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1311 if (!(qc->flags & ATA_QCFLAG_FAILED))
1314 /* inherit upper level err_mask */
1315 qc->err_mask |= ehc->i.err_mask;
1318 action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1320 /* DEV errors are probably spurious in case of ATA_BUS error */
1321 if (qc->err_mask & AC_ERR_ATA_BUS)
1322 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1325 /* any real error trumps unknown error */
1326 if (qc->err_mask & ~AC_ERR_OTHER)
1327 qc->err_mask &= ~AC_ERR_OTHER;
1329 /* SENSE_VALID trumps dev/unknown error and revalidation */
1330 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1331 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1332 action &= ~ATA_EH_REVALIDATE;
1335 /* accumulate error info */
1336 failed_dev = qc->dev;
1337 all_err_mask |= qc->err_mask;
1338 if (qc->flags & ATA_QCFLAG_IO)
1342 /* enforce default EH actions */
1343 if (ap->pflags & ATA_PFLAG_FROZEN ||
1344 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
1345 action |= ATA_EH_SOFTRESET;
1346 else if (all_err_mask)
1347 action |= ATA_EH_REVALIDATE;
1349 /* if we have offending qcs and the associated failed device */
1352 action |= ata_eh_speed_down(failed_dev, is_io, all_err_mask);
1354 /* perform per-dev EH action only on the offending device */
1355 ehc->i.dev_action[failed_dev->devno] |=
1356 action & ATA_EH_PERDEV_MASK;
1357 action &= ~ATA_EH_PERDEV_MASK;
1360 /* record autopsy result */
1361 ehc->i.dev = failed_dev;
1362 ehc->i.action |= action;
1368 * ata_eh_report - report error handling to user
1369 * @ap: ATA port EH is going on
1371 * Report EH to user.
1376 static void ata_eh_report(struct ata_port *ap)
1378 struct ata_eh_context *ehc = &ap->eh_context;
1379 const char *frozen, *desc;
1380 int tag, nr_failed = 0;
1383 if (ehc->i.desc[0] != '\0')
1386 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1387 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1389 if (!(qc->flags & ATA_QCFLAG_FAILED))
1391 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1397 if (!nr_failed && !ehc->i.err_mask)
1401 if (ap->pflags & ATA_PFLAG_FROZEN)
1405 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1406 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1407 ehc->i.err_mask, ap->sactive, ehc->i.serror,
1408 ehc->i.action, frozen);
1410 ata_dev_printk(ehc->i.dev, KERN_ERR, "(%s)\n", desc);
1412 ata_port_printk(ap, KERN_ERR, "exception Emask 0x%x "
1413 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1414 ehc->i.err_mask, ap->sactive, ehc->i.serror,
1415 ehc->i.action, frozen);
1417 ata_port_printk(ap, KERN_ERR, "(%s)\n", desc);
1420 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1421 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1423 if (!(qc->flags & ATA_QCFLAG_FAILED) || !qc->err_mask)
1426 ata_dev_printk(qc->dev, KERN_ERR, "tag %d cmd 0x%x "
1427 "Emask 0x%x stat 0x%x err 0x%x (%s)\n",
1428 qc->tag, qc->tf.command, qc->err_mask,
1429 qc->result_tf.command, qc->result_tf.feature,
1430 ata_err_string(qc->err_mask));
1434 static int ata_do_reset(struct ata_port *ap, ata_reset_fn_t reset,
1435 unsigned int *classes)
1439 for (i = 0; i < ATA_MAX_DEVICES; i++)
1440 classes[i] = ATA_DEV_UNKNOWN;
1442 rc = reset(ap, classes);
1446 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
1447 * is complete and convert all ATA_DEV_UNKNOWN to
1450 for (i = 0; i < ATA_MAX_DEVICES; i++)
1451 if (classes[i] != ATA_DEV_UNKNOWN)
1454 if (i < ATA_MAX_DEVICES)
1455 for (i = 0; i < ATA_MAX_DEVICES; i++)
1456 if (classes[i] == ATA_DEV_UNKNOWN)
1457 classes[i] = ATA_DEV_NONE;
1462 static int ata_eh_followup_srst_needed(int rc, int classify,
1463 const unsigned int *classes)
1469 if (classify && classes[0] == ATA_DEV_UNKNOWN)
1474 static int ata_eh_reset(struct ata_port *ap, int classify,
1475 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
1476 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
1478 struct ata_eh_context *ehc = &ap->eh_context;
1479 unsigned int *classes = ehc->classes;
1480 int tries = ATA_EH_RESET_TRIES;
1481 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
1482 unsigned int action;
1483 ata_reset_fn_t reset;
1484 int i, did_followup_srst, rc;
1486 /* Determine which reset to use and record in ehc->i.action.
1487 * prereset() may examine and modify it.
1489 action = ehc->i.action;
1490 ehc->i.action &= ~ATA_EH_RESET_MASK;
1491 if (softreset && (!hardreset || (!sata_set_spd_needed(ap) &&
1492 !(action & ATA_EH_HARDRESET))))
1493 ehc->i.action |= ATA_EH_SOFTRESET;
1495 ehc->i.action |= ATA_EH_HARDRESET;
1500 ata_port_printk(ap, KERN_ERR,
1501 "prereset failed (errno=%d)\n", rc);
1506 /* prereset() might have modified ehc->i.action */
1507 if (ehc->i.action & ATA_EH_HARDRESET)
1509 else if (ehc->i.action & ATA_EH_SOFTRESET)
1512 /* prereset told us not to reset, bang classes and return */
1513 for (i = 0; i < ATA_MAX_DEVICES; i++)
1514 classes[i] = ATA_DEV_NONE;
1518 /* did prereset() screw up? if so, fix up to avoid oopsing */
1520 ata_port_printk(ap, KERN_ERR, "BUG: prereset() requested "
1521 "invalid reset type\n");
1529 /* shut up during boot probing */
1531 ata_port_printk(ap, KERN_INFO, "%s resetting port\n",
1532 reset == softreset ? "soft" : "hard");
1535 ata_eh_about_to_do(ap, NULL, ATA_EH_RESET_MASK);
1536 ehc->i.flags |= ATA_EHI_DID_RESET;
1538 rc = ata_do_reset(ap, reset, classes);
1540 did_followup_srst = 0;
1541 if (reset == hardreset &&
1542 ata_eh_followup_srst_needed(rc, classify, classes)) {
1543 /* okay, let's do follow-up softreset */
1544 did_followup_srst = 1;
1548 ata_port_printk(ap, KERN_ERR,
1549 "follow-up softreset required "
1550 "but no softreset avaliable\n");
1554 ata_eh_about_to_do(ap, NULL, ATA_EH_RESET_MASK);
1555 rc = ata_do_reset(ap, reset, classes);
1557 if (rc == 0 && classify &&
1558 classes[0] == ATA_DEV_UNKNOWN) {
1559 ata_port_printk(ap, KERN_ERR,
1560 "classification failed\n");
1565 if (rc && --tries) {
1568 if (reset == softreset) {
1569 if (did_followup_srst)
1570 type = "follow-up soft";
1576 ata_port_printk(ap, KERN_WARNING,
1577 "%sreset failed, retrying in 5 secs\n", type);
1580 if (reset == hardreset)
1581 sata_down_spd_limit(ap);
1588 /* After the reset, the device state is PIO 0 and the
1589 * controller state is undefined. Record the mode.
1591 for (i = 0; i < ATA_MAX_DEVICES; i++)
1592 ap->device[i].pio_mode = XFER_PIO_0;
1595 postreset(ap, classes);
1597 /* reset successful, schedule revalidation */
1598 ata_eh_done(ap, NULL, ATA_EH_RESET_MASK);
1599 ehc->i.action |= ATA_EH_REVALIDATE;
1605 static int ata_eh_revalidate_and_attach(struct ata_port *ap,
1606 struct ata_device **r_failed_dev)
1608 struct ata_eh_context *ehc = &ap->eh_context;
1609 struct ata_device *dev;
1610 unsigned long flags;
1615 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1616 unsigned int action;
1618 dev = &ap->device[i];
1619 action = ata_eh_dev_action(dev);
1621 if (action & ATA_EH_REVALIDATE && ata_dev_ready(dev)) {
1622 if (ata_port_offline(ap)) {
1627 ata_eh_about_to_do(ap, dev, ATA_EH_REVALIDATE);
1628 rc = ata_dev_revalidate(dev,
1629 ehc->i.flags & ATA_EHI_DID_RESET);
1633 ata_eh_done(ap, dev, ATA_EH_REVALIDATE);
1635 /* schedule the scsi_rescan_device() here */
1636 queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
1637 } else if (dev->class == ATA_DEV_UNKNOWN &&
1638 ehc->tries[dev->devno] &&
1639 ata_class_enabled(ehc->classes[dev->devno])) {
1640 dev->class = ehc->classes[dev->devno];
1642 rc = ata_dev_read_id(dev, &dev->class, 1, dev->id);
1644 rc = ata_dev_configure(dev, 1);
1647 dev->class = ATA_DEV_UNKNOWN;
1651 spin_lock_irqsave(ap->lock, flags);
1652 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
1653 spin_unlock_irqrestore(ap->lock, flags);
1658 *r_failed_dev = dev;
1665 * ata_eh_suspend - handle suspend EH action
1666 * @ap: target host port
1667 * @r_failed_dev: result parameter to indicate failing device
1669 * Handle suspend EH action. Disk devices are spinned down and
1670 * other types of devices are just marked suspended. Once
1671 * suspended, no EH action to the device is allowed until it is
1675 * Kernel thread context (may sleep).
1678 * 0 on success, -errno otherwise
1680 static int ata_eh_suspend(struct ata_port *ap, struct ata_device **r_failed_dev)
1682 struct ata_device *dev;
1687 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1688 unsigned long flags;
1689 unsigned int action, err_mask;
1691 dev = &ap->device[i];
1692 action = ata_eh_dev_action(dev);
1694 if (!ata_dev_enabled(dev) || !(action & ATA_EH_SUSPEND))
1697 WARN_ON(dev->flags & ATA_DFLAG_SUSPENDED);
1699 ata_eh_about_to_do(ap, dev, ATA_EH_SUSPEND);
1701 if (dev->class == ATA_DEV_ATA && !(action & ATA_EH_PM_FREEZE)) {
1703 rc = ata_flush_cache(dev);
1708 err_mask = ata_do_simple_cmd(dev, ATA_CMD_STANDBYNOW1);
1710 ata_dev_printk(dev, KERN_ERR, "failed to "
1711 "spin down (err_mask=0x%x)\n",
1718 spin_lock_irqsave(ap->lock, flags);
1719 dev->flags |= ATA_DFLAG_SUSPENDED;
1720 spin_unlock_irqrestore(ap->lock, flags);
1722 ata_eh_done(ap, dev, ATA_EH_SUSPEND);
1726 *r_failed_dev = dev;
1733 * ata_eh_prep_resume - prep for resume EH action
1734 * @ap: target host port
1736 * Clear SUSPENDED in preparation for scheduled resume actions.
1737 * This allows other parts of EH to access the devices being
1741 * Kernel thread context (may sleep).
1743 static void ata_eh_prep_resume(struct ata_port *ap)
1745 struct ata_device *dev;
1746 unsigned long flags;
1751 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1752 unsigned int action;
1754 dev = &ap->device[i];
1755 action = ata_eh_dev_action(dev);
1757 if (!ata_dev_enabled(dev) || !(action & ATA_EH_RESUME))
1760 spin_lock_irqsave(ap->lock, flags);
1761 dev->flags &= ~ATA_DFLAG_SUSPENDED;
1762 spin_unlock_irqrestore(ap->lock, flags);
1769 * ata_eh_resume - handle resume EH action
1770 * @ap: target host port
1771 * @r_failed_dev: result parameter to indicate failing device
1773 * Handle resume EH action. Target devices are already reset and
1774 * revalidated. Spinning up is the only operation left.
1777 * Kernel thread context (may sleep).
1780 * 0 on success, -errno otherwise
1782 static int ata_eh_resume(struct ata_port *ap, struct ata_device **r_failed_dev)
1784 struct ata_device *dev;
1789 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1790 unsigned int action, err_mask;
1792 dev = &ap->device[i];
1793 action = ata_eh_dev_action(dev);
1795 if (!ata_dev_enabled(dev) || !(action & ATA_EH_RESUME))
1798 ata_eh_about_to_do(ap, dev, ATA_EH_RESUME);
1800 if (dev->class == ATA_DEV_ATA && !(action & ATA_EH_PM_FREEZE)) {
1801 err_mask = ata_do_simple_cmd(dev,
1802 ATA_CMD_IDLEIMMEDIATE);
1804 ata_dev_printk(dev, KERN_ERR, "failed to "
1805 "spin up (err_mask=0x%x)\n",
1812 ata_eh_done(ap, dev, ATA_EH_RESUME);
1816 *r_failed_dev = dev;
1822 static int ata_port_nr_enabled(struct ata_port *ap)
1826 for (i = 0; i < ATA_MAX_DEVICES; i++)
1827 if (ata_dev_enabled(&ap->device[i]))
1832 static int ata_port_nr_vacant(struct ata_port *ap)
1836 for (i = 0; i < ATA_MAX_DEVICES; i++)
1837 if (ap->device[i].class == ATA_DEV_UNKNOWN)
1842 static int ata_eh_skip_recovery(struct ata_port *ap)
1844 struct ata_eh_context *ehc = &ap->eh_context;
1847 /* skip if all possible devices are suspended */
1848 for (i = 0; i < ata_port_max_devices(ap); i++) {
1849 struct ata_device *dev = &ap->device[i];
1851 if (ata_dev_absent(dev) || ata_dev_ready(dev))
1855 if (i == ata_port_max_devices(ap))
1858 /* always thaw frozen port and recover failed devices */
1859 if (ap->pflags & ATA_PFLAG_FROZEN || ata_port_nr_enabled(ap))
1862 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
1863 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1864 struct ata_device *dev = &ap->device[i];
1866 if (dev->class == ATA_DEV_UNKNOWN &&
1867 ehc->classes[dev->devno] != ATA_DEV_NONE)
1875 * ata_eh_recover - recover host port after error
1876 * @ap: host port to recover
1877 * @prereset: prereset method (can be NULL)
1878 * @softreset: softreset method (can be NULL)
1879 * @hardreset: hardreset method (can be NULL)
1880 * @postreset: postreset method (can be NULL)
1882 * This is the alpha and omega, eum and yang, heart and soul of
1883 * libata exception handling. On entry, actions required to
1884 * recover the port and hotplug requests are recorded in
1885 * eh_context. This function executes all the operations with
1886 * appropriate retrials and fallbacks to resurrect failed
1887 * devices, detach goners and greet newcomers.
1890 * Kernel thread context (may sleep).
1893 * 0 on success, -errno on failure.
1895 static int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
1896 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
1897 ata_postreset_fn_t postreset)
1899 struct ata_eh_context *ehc = &ap->eh_context;
1900 struct ata_device *dev;
1901 int down_xfermask, i, rc;
1905 /* prep for recovery */
1906 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1907 dev = &ap->device[i];
1909 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
1911 /* process hotplug request */
1912 if (dev->flags & ATA_DFLAG_DETACH)
1913 ata_eh_detach_dev(dev);
1915 if (!ata_dev_enabled(dev) &&
1916 ((ehc->i.probe_mask & (1 << dev->devno)) &&
1917 !(ehc->did_probe_mask & (1 << dev->devno)))) {
1918 ata_eh_detach_dev(dev);
1920 ehc->did_probe_mask |= (1 << dev->devno);
1921 ehc->i.action |= ATA_EH_SOFTRESET;
1929 /* if UNLOADING, finish immediately */
1930 if (ap->pflags & ATA_PFLAG_UNLOADING)
1933 /* prep for resume */
1934 ata_eh_prep_resume(ap);
1936 /* skip EH if possible. */
1937 if (ata_eh_skip_recovery(ap))
1940 for (i = 0; i < ATA_MAX_DEVICES; i++)
1941 ehc->classes[i] = ATA_DEV_UNKNOWN;
1944 if (ehc->i.action & ATA_EH_RESET_MASK) {
1945 ata_eh_freeze_port(ap);
1947 rc = ata_eh_reset(ap, ata_port_nr_vacant(ap), prereset,
1948 softreset, hardreset, postreset);
1950 ata_port_printk(ap, KERN_ERR,
1951 "reset failed, giving up\n");
1955 ata_eh_thaw_port(ap);
1958 /* revalidate existing devices and attach new ones */
1959 rc = ata_eh_revalidate_and_attach(ap, &dev);
1963 /* resume devices */
1964 rc = ata_eh_resume(ap, &dev);
1968 /* configure transfer mode if the port has been reset */
1969 if (ehc->i.flags & ATA_EHI_DID_RESET) {
1970 rc = ata_set_mode(ap, &dev);
1977 /* suspend devices */
1978 rc = ata_eh_suspend(ap, &dev);
1987 /* device missing, schedule probing */
1988 ehc->i.probe_mask |= (1 << dev->devno);
1990 ehc->tries[dev->devno] = 0;
1993 sata_down_spd_limit(ap);
1995 ehc->tries[dev->devno]--;
1996 if (down_xfermask &&
1997 ata_down_xfermask_limit(dev, ehc->tries[dev->devno] == 1))
1998 ehc->tries[dev->devno] = 0;
2001 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2002 /* disable device if it has used up all its chances */
2003 ata_dev_disable(dev);
2005 /* detach if offline */
2006 if (ata_port_offline(ap))
2007 ata_eh_detach_dev(dev);
2009 /* probe if requested */
2010 if ((ehc->i.probe_mask & (1 << dev->devno)) &&
2011 !(ehc->did_probe_mask & (1 << dev->devno))) {
2012 ata_eh_detach_dev(dev);
2015 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2016 ehc->did_probe_mask |= (1 << dev->devno);
2017 ehc->i.action |= ATA_EH_SOFTRESET;
2020 /* soft didn't work? be haaaaard */
2021 if (ehc->i.flags & ATA_EHI_DID_RESET)
2022 ehc->i.action |= ATA_EH_HARDRESET;
2024 ehc->i.action |= ATA_EH_SOFTRESET;
2027 if (ata_port_nr_enabled(ap)) {
2028 ata_port_printk(ap, KERN_WARNING, "failed to recover some "
2029 "devices, retrying in 5 secs\n");
2032 /* no device left, repeat fast */
2040 for (i = 0; i < ATA_MAX_DEVICES; i++)
2041 ata_dev_disable(&ap->device[i]);
2044 DPRINTK("EXIT, rc=%d\n", rc);
2049 * ata_eh_finish - finish up EH
2050 * @ap: host port to finish EH for
2052 * Recovery is complete. Clean up EH states and retry or finish
2058 static void ata_eh_finish(struct ata_port *ap)
2062 /* retry or finish qcs */
2063 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2064 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2066 if (!(qc->flags & ATA_QCFLAG_FAILED))
2070 /* FIXME: Once EH migration is complete,
2071 * generate sense data in this function,
2072 * considering both err_mask and tf.
2074 if (qc->err_mask & AC_ERR_INVALID)
2075 ata_eh_qc_complete(qc);
2077 ata_eh_qc_retry(qc);
2079 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2080 ata_eh_qc_complete(qc);
2082 /* feed zero TF to sense generation */
2083 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2084 ata_eh_qc_retry(qc);
2091 * ata_do_eh - do standard error handling
2092 * @ap: host port to handle error for
2093 * @prereset: prereset method (can be NULL)
2094 * @softreset: softreset method (can be NULL)
2095 * @hardreset: hardreset method (can be NULL)
2096 * @postreset: postreset method (can be NULL)
2098 * Perform standard error handling sequence.
2101 * Kernel thread context (may sleep).
2103 void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2104 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2105 ata_postreset_fn_t postreset)
2109 ata_eh_recover(ap, prereset, softreset, hardreset, postreset);
2114 * ata_eh_handle_port_suspend - perform port suspend operation
2115 * @ap: port to suspend
2120 * Kernel thread context (may sleep).
2122 static void ata_eh_handle_port_suspend(struct ata_port *ap)
2124 unsigned long flags;
2127 /* are we suspending? */
2128 spin_lock_irqsave(ap->lock, flags);
2129 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2130 ap->pm_mesg.event == PM_EVENT_ON) {
2131 spin_unlock_irqrestore(ap->lock, flags);
2134 spin_unlock_irqrestore(ap->lock, flags);
2136 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
2139 ata_eh_freeze_port(ap);
2141 if (ap->ops->port_suspend)
2142 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
2145 spin_lock_irqsave(ap->lock, flags);
2147 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
2149 ap->pflags |= ATA_PFLAG_SUSPENDED;
2151 ata_port_schedule_eh(ap);
2153 if (ap->pm_result) {
2154 *ap->pm_result = rc;
2155 ap->pm_result = NULL;
2158 spin_unlock_irqrestore(ap->lock, flags);
2164 * ata_eh_handle_port_resume - perform port resume operation
2165 * @ap: port to resume
2169 * This function also waits upto one second until all devices
2170 * hanging off this port requests resume EH action. This is to
2171 * prevent invoking EH and thus reset multiple times on resume.
2173 * On DPM resume, where some of devices might not be resumed
2174 * together, this may delay port resume upto one second, but such
2175 * DPM resumes are rare and 1 sec delay isn't too bad.
2178 * Kernel thread context (may sleep).
2180 static void ata_eh_handle_port_resume(struct ata_port *ap)
2182 unsigned long timeout;
2183 unsigned long flags;
2186 /* are we resuming? */
2187 spin_lock_irqsave(ap->lock, flags);
2188 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2189 ap->pm_mesg.event != PM_EVENT_ON) {
2190 spin_unlock_irqrestore(ap->lock, flags);
2193 spin_unlock_irqrestore(ap->lock, flags);
2196 if (!(ap->pflags & ATA_PFLAG_SUSPENDED))
2199 if (ap->ops->port_resume)
2200 rc = ap->ops->port_resume(ap);
2202 /* give devices time to request EH */
2203 timeout = jiffies + HZ; /* 1s max */
2205 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2206 struct ata_device *dev = &ap->device[i];
2207 unsigned int action = ata_eh_dev_action(dev);
2209 if ((dev->flags & ATA_DFLAG_SUSPENDED) &&
2210 !(action & ATA_EH_RESUME))
2214 if (i == ATA_MAX_DEVICES || time_after(jiffies, timeout))
2220 spin_lock_irqsave(ap->lock, flags);
2221 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
2222 if (ap->pm_result) {
2223 *ap->pm_result = rc;
2224 ap->pm_result = NULL;
2226 spin_unlock_irqrestore(ap->lock, flags);