xref: /openbmc/linux/drivers/ata/libata-eh.c (revision cca3974e48607c3775dc73b544a5700b2e37c21a)
1c6fd2807SJeff Garzik /*
2c6fd2807SJeff Garzik  *  libata-eh.c - libata error handling
3c6fd2807SJeff Garzik  *
4c6fd2807SJeff Garzik  *  Maintained by:  Jeff Garzik <jgarzik@pobox.com>
5c6fd2807SJeff Garzik  *    		    Please ALWAYS copy linux-ide@vger.kernel.org
6c6fd2807SJeff Garzik  *		    on emails.
7c6fd2807SJeff Garzik  *
8c6fd2807SJeff Garzik  *  Copyright 2006 Tejun Heo <htejun@gmail.com>
9c6fd2807SJeff Garzik  *
10c6fd2807SJeff Garzik  *
11c6fd2807SJeff Garzik  *  This program is free software; you can redistribute it and/or
12c6fd2807SJeff Garzik  *  modify it under the terms of the GNU General Public License as
13c6fd2807SJeff Garzik  *  published by the Free Software Foundation; either version 2, or
14c6fd2807SJeff Garzik  *  (at your option) any later version.
15c6fd2807SJeff Garzik  *
16c6fd2807SJeff Garzik  *  This program is distributed in the hope that it will be useful,
17c6fd2807SJeff Garzik  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18c6fd2807SJeff Garzik  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19c6fd2807SJeff Garzik  *  General Public License for more details.
20c6fd2807SJeff Garzik  *
21c6fd2807SJeff Garzik  *  You should have received a copy of the GNU General Public License
22c6fd2807SJeff Garzik  *  along with this program; see the file COPYING.  If not, write to
23c6fd2807SJeff Garzik  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
24c6fd2807SJeff Garzik  *  USA.
25c6fd2807SJeff Garzik  *
26c6fd2807SJeff Garzik  *
27c6fd2807SJeff Garzik  *  libata documentation is available via 'make {ps|pdf}docs',
28c6fd2807SJeff Garzik  *  as Documentation/DocBook/libata.*
29c6fd2807SJeff Garzik  *
30c6fd2807SJeff Garzik  *  Hardware documentation available from http://www.t13.org/ and
31c6fd2807SJeff Garzik  *  http://www.sata-io.org/
32c6fd2807SJeff Garzik  *
33c6fd2807SJeff Garzik  */
34c6fd2807SJeff Garzik 
35c6fd2807SJeff Garzik #include <linux/config.h>
36c6fd2807SJeff Garzik #include <linux/kernel.h>
37c6fd2807SJeff Garzik #include <scsi/scsi.h>
38c6fd2807SJeff Garzik #include <scsi/scsi_host.h>
39c6fd2807SJeff Garzik #include <scsi/scsi_eh.h>
40c6fd2807SJeff Garzik #include <scsi/scsi_device.h>
41c6fd2807SJeff Garzik #include <scsi/scsi_cmnd.h>
42c6fd2807SJeff Garzik #include "../scsi/scsi_transport_api.h"
43c6fd2807SJeff Garzik 
44c6fd2807SJeff Garzik #include <linux/libata.h>
45c6fd2807SJeff Garzik 
46c6fd2807SJeff Garzik #include "libata.h"
47c6fd2807SJeff Garzik 
48c6fd2807SJeff Garzik static void __ata_port_freeze(struct ata_port *ap);
49c6fd2807SJeff Garzik static void ata_eh_finish(struct ata_port *ap);
50c6fd2807SJeff Garzik static void ata_eh_handle_port_suspend(struct ata_port *ap);
51c6fd2807SJeff Garzik static void ata_eh_handle_port_resume(struct ata_port *ap);
52c6fd2807SJeff Garzik 
53c6fd2807SJeff Garzik static void ata_ering_record(struct ata_ering *ering, int is_io,
54c6fd2807SJeff Garzik 			     unsigned int err_mask)
55c6fd2807SJeff Garzik {
56c6fd2807SJeff Garzik 	struct ata_ering_entry *ent;
57c6fd2807SJeff Garzik 
58c6fd2807SJeff Garzik 	WARN_ON(!err_mask);
59c6fd2807SJeff Garzik 
60c6fd2807SJeff Garzik 	ering->cursor++;
61c6fd2807SJeff Garzik 	ering->cursor %= ATA_ERING_SIZE;
62c6fd2807SJeff Garzik 
63c6fd2807SJeff Garzik 	ent = &ering->ring[ering->cursor];
64c6fd2807SJeff Garzik 	ent->is_io = is_io;
65c6fd2807SJeff Garzik 	ent->err_mask = err_mask;
66c6fd2807SJeff Garzik 	ent->timestamp = get_jiffies_64();
67c6fd2807SJeff Garzik }
68c6fd2807SJeff Garzik 
69c6fd2807SJeff Garzik static struct ata_ering_entry * ata_ering_top(struct ata_ering *ering)
70c6fd2807SJeff Garzik {
71c6fd2807SJeff Garzik 	struct ata_ering_entry *ent = &ering->ring[ering->cursor];
72c6fd2807SJeff Garzik 	if (!ent->err_mask)
73c6fd2807SJeff Garzik 		return NULL;
74c6fd2807SJeff Garzik 	return ent;
75c6fd2807SJeff Garzik }
76c6fd2807SJeff Garzik 
77c6fd2807SJeff Garzik static int ata_ering_map(struct ata_ering *ering,
78c6fd2807SJeff Garzik 			 int (*map_fn)(struct ata_ering_entry *, void *),
79c6fd2807SJeff Garzik 			 void *arg)
80c6fd2807SJeff Garzik {
81c6fd2807SJeff Garzik 	int idx, rc = 0;
82c6fd2807SJeff Garzik 	struct ata_ering_entry *ent;
83c6fd2807SJeff Garzik 
84c6fd2807SJeff Garzik 	idx = ering->cursor;
85c6fd2807SJeff Garzik 	do {
86c6fd2807SJeff Garzik 		ent = &ering->ring[idx];
87c6fd2807SJeff Garzik 		if (!ent->err_mask)
88c6fd2807SJeff Garzik 			break;
89c6fd2807SJeff Garzik 		rc = map_fn(ent, arg);
90c6fd2807SJeff Garzik 		if (rc)
91c6fd2807SJeff Garzik 			break;
92c6fd2807SJeff Garzik 		idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
93c6fd2807SJeff Garzik 	} while (idx != ering->cursor);
94c6fd2807SJeff Garzik 
95c6fd2807SJeff Garzik 	return rc;
96c6fd2807SJeff Garzik }
97c6fd2807SJeff Garzik 
98c6fd2807SJeff Garzik static unsigned int ata_eh_dev_action(struct ata_device *dev)
99c6fd2807SJeff Garzik {
100c6fd2807SJeff Garzik 	struct ata_eh_context *ehc = &dev->ap->eh_context;
101c6fd2807SJeff Garzik 
102c6fd2807SJeff Garzik 	return ehc->i.action | ehc->i.dev_action[dev->devno];
103c6fd2807SJeff Garzik }
104c6fd2807SJeff Garzik 
105c6fd2807SJeff Garzik static void ata_eh_clear_action(struct ata_device *dev,
106c6fd2807SJeff Garzik 				struct ata_eh_info *ehi, unsigned int action)
107c6fd2807SJeff Garzik {
108c6fd2807SJeff Garzik 	int i;
109c6fd2807SJeff Garzik 
110c6fd2807SJeff Garzik 	if (!dev) {
111c6fd2807SJeff Garzik 		ehi->action &= ~action;
112c6fd2807SJeff Garzik 		for (i = 0; i < ATA_MAX_DEVICES; i++)
113c6fd2807SJeff Garzik 			ehi->dev_action[i] &= ~action;
114c6fd2807SJeff Garzik 	} else {
115c6fd2807SJeff Garzik 		/* doesn't make sense for port-wide EH actions */
116c6fd2807SJeff Garzik 		WARN_ON(!(action & ATA_EH_PERDEV_MASK));
117c6fd2807SJeff Garzik 
118c6fd2807SJeff Garzik 		/* break ehi->action into ehi->dev_action */
119c6fd2807SJeff Garzik 		if (ehi->action & action) {
120c6fd2807SJeff Garzik 			for (i = 0; i < ATA_MAX_DEVICES; i++)
121c6fd2807SJeff Garzik 				ehi->dev_action[i] |= ehi->action & action;
122c6fd2807SJeff Garzik 			ehi->action &= ~action;
123c6fd2807SJeff Garzik 		}
124c6fd2807SJeff Garzik 
125c6fd2807SJeff Garzik 		/* turn off the specified per-dev action */
126c6fd2807SJeff Garzik 		ehi->dev_action[dev->devno] &= ~action;
127c6fd2807SJeff Garzik 	}
128c6fd2807SJeff Garzik }
129c6fd2807SJeff Garzik 
130c6fd2807SJeff Garzik /**
131c6fd2807SJeff Garzik  *	ata_scsi_timed_out - SCSI layer time out callback
132c6fd2807SJeff Garzik  *	@cmd: timed out SCSI command
133c6fd2807SJeff Garzik  *
134c6fd2807SJeff Garzik  *	Handles SCSI layer timeout.  We race with normal completion of
135c6fd2807SJeff Garzik  *	the qc for @cmd.  If the qc is already gone, we lose and let
136c6fd2807SJeff Garzik  *	the scsi command finish (EH_HANDLED).  Otherwise, the qc has
137c6fd2807SJeff Garzik  *	timed out and EH should be invoked.  Prevent ata_qc_complete()
138c6fd2807SJeff Garzik  *	from finishing it by setting EH_SCHEDULED and return
139c6fd2807SJeff Garzik  *	EH_NOT_HANDLED.
140c6fd2807SJeff Garzik  *
141c6fd2807SJeff Garzik  *	TODO: kill this function once old EH is gone.
142c6fd2807SJeff Garzik  *
143c6fd2807SJeff Garzik  *	LOCKING:
144c6fd2807SJeff Garzik  *	Called from timer context
145c6fd2807SJeff Garzik  *
146c6fd2807SJeff Garzik  *	RETURNS:
147c6fd2807SJeff Garzik  *	EH_HANDLED or EH_NOT_HANDLED
148c6fd2807SJeff Garzik  */
149c6fd2807SJeff Garzik enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
150c6fd2807SJeff Garzik {
151c6fd2807SJeff Garzik 	struct Scsi_Host *host = cmd->device->host;
152c6fd2807SJeff Garzik 	struct ata_port *ap = ata_shost_to_port(host);
153c6fd2807SJeff Garzik 	unsigned long flags;
154c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
155c6fd2807SJeff Garzik 	enum scsi_eh_timer_return ret;
156c6fd2807SJeff Garzik 
157c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
158c6fd2807SJeff Garzik 
159c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
160c6fd2807SJeff Garzik 		ret = EH_NOT_HANDLED;
161c6fd2807SJeff Garzik 		goto out;
162c6fd2807SJeff Garzik 	}
163c6fd2807SJeff Garzik 
164c6fd2807SJeff Garzik 	ret = EH_HANDLED;
165c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
166c6fd2807SJeff Garzik 	qc = ata_qc_from_tag(ap, ap->active_tag);
167c6fd2807SJeff Garzik 	if (qc) {
168c6fd2807SJeff Garzik 		WARN_ON(qc->scsicmd != cmd);
169c6fd2807SJeff Garzik 		qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
170c6fd2807SJeff Garzik 		qc->err_mask |= AC_ERR_TIMEOUT;
171c6fd2807SJeff Garzik 		ret = EH_NOT_HANDLED;
172c6fd2807SJeff Garzik 	}
173c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
174c6fd2807SJeff Garzik 
175c6fd2807SJeff Garzik  out:
176c6fd2807SJeff Garzik 	DPRINTK("EXIT, ret=%d\n", ret);
177c6fd2807SJeff Garzik 	return ret;
178c6fd2807SJeff Garzik }
179c6fd2807SJeff Garzik 
180c6fd2807SJeff Garzik /**
181c6fd2807SJeff Garzik  *	ata_scsi_error - SCSI layer error handler callback
182c6fd2807SJeff Garzik  *	@host: SCSI host on which error occurred
183c6fd2807SJeff Garzik  *
184c6fd2807SJeff Garzik  *	Handles SCSI-layer-thrown error events.
185c6fd2807SJeff Garzik  *
186c6fd2807SJeff Garzik  *	LOCKING:
187c6fd2807SJeff Garzik  *	Inherited from SCSI layer (none, can sleep)
188c6fd2807SJeff Garzik  *
189c6fd2807SJeff Garzik  *	RETURNS:
190c6fd2807SJeff Garzik  *	Zero.
191c6fd2807SJeff Garzik  */
192c6fd2807SJeff Garzik void ata_scsi_error(struct Scsi_Host *host)
193c6fd2807SJeff Garzik {
194c6fd2807SJeff Garzik 	struct ata_port *ap = ata_shost_to_port(host);
195c6fd2807SJeff Garzik 	int i, repeat_cnt = ATA_EH_MAX_REPEAT;
196c6fd2807SJeff Garzik 	unsigned long flags;
197c6fd2807SJeff Garzik 
198c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
199c6fd2807SJeff Garzik 
200c6fd2807SJeff Garzik 	/* synchronize with port task */
201c6fd2807SJeff Garzik 	ata_port_flush_task(ap);
202c6fd2807SJeff Garzik 
203*cca3974eSJeff Garzik 	/* synchronize with host lock and sort out timeouts */
204c6fd2807SJeff Garzik 
205c6fd2807SJeff Garzik 	/* For new EH, all qcs are finished in one of three ways -
206c6fd2807SJeff Garzik 	 * normal completion, error completion, and SCSI timeout.
207c6fd2807SJeff Garzik 	 * Both cmpletions can race against SCSI timeout.  When normal
208c6fd2807SJeff Garzik 	 * completion wins, the qc never reaches EH.  When error
209c6fd2807SJeff Garzik 	 * completion wins, the qc has ATA_QCFLAG_FAILED set.
210c6fd2807SJeff Garzik 	 *
211c6fd2807SJeff Garzik 	 * When SCSI timeout wins, things are a bit more complex.
212c6fd2807SJeff Garzik 	 * Normal or error completion can occur after the timeout but
213c6fd2807SJeff Garzik 	 * before this point.  In such cases, both types of
214c6fd2807SJeff Garzik 	 * completions are honored.  A scmd is determined to have
215c6fd2807SJeff Garzik 	 * timed out iff its associated qc is active and not failed.
216c6fd2807SJeff Garzik 	 */
217c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
218c6fd2807SJeff Garzik 		struct scsi_cmnd *scmd, *tmp;
219c6fd2807SJeff Garzik 		int nr_timedout = 0;
220c6fd2807SJeff Garzik 
221c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
222c6fd2807SJeff Garzik 
223c6fd2807SJeff Garzik 		list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
224c6fd2807SJeff Garzik 			struct ata_queued_cmd *qc;
225c6fd2807SJeff Garzik 
226c6fd2807SJeff Garzik 			for (i = 0; i < ATA_MAX_QUEUE; i++) {
227c6fd2807SJeff Garzik 				qc = __ata_qc_from_tag(ap, i);
228c6fd2807SJeff Garzik 				if (qc->flags & ATA_QCFLAG_ACTIVE &&
229c6fd2807SJeff Garzik 				    qc->scsicmd == scmd)
230c6fd2807SJeff Garzik 					break;
231c6fd2807SJeff Garzik 			}
232c6fd2807SJeff Garzik 
233c6fd2807SJeff Garzik 			if (i < ATA_MAX_QUEUE) {
234c6fd2807SJeff Garzik 				/* the scmd has an associated qc */
235c6fd2807SJeff Garzik 				if (!(qc->flags & ATA_QCFLAG_FAILED)) {
236c6fd2807SJeff Garzik 					/* which hasn't failed yet, timeout */
237c6fd2807SJeff Garzik 					qc->err_mask |= AC_ERR_TIMEOUT;
238c6fd2807SJeff Garzik 					qc->flags |= ATA_QCFLAG_FAILED;
239c6fd2807SJeff Garzik 					nr_timedout++;
240c6fd2807SJeff Garzik 				}
241c6fd2807SJeff Garzik 			} else {
242c6fd2807SJeff Garzik 				/* Normal completion occurred after
243c6fd2807SJeff Garzik 				 * SCSI timeout but before this point.
244c6fd2807SJeff Garzik 				 * Successfully complete it.
245c6fd2807SJeff Garzik 				 */
246c6fd2807SJeff Garzik 				scmd->retries = scmd->allowed;
247c6fd2807SJeff Garzik 				scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
248c6fd2807SJeff Garzik 			}
249c6fd2807SJeff Garzik 		}
250c6fd2807SJeff Garzik 
251c6fd2807SJeff Garzik 		/* If we have timed out qcs.  They belong to EH from
252c6fd2807SJeff Garzik 		 * this point but the state of the controller is
253c6fd2807SJeff Garzik 		 * unknown.  Freeze the port to make sure the IRQ
254c6fd2807SJeff Garzik 		 * handler doesn't diddle with those qcs.  This must
255c6fd2807SJeff Garzik 		 * be done atomically w.r.t. setting QCFLAG_FAILED.
256c6fd2807SJeff Garzik 		 */
257c6fd2807SJeff Garzik 		if (nr_timedout)
258c6fd2807SJeff Garzik 			__ata_port_freeze(ap);
259c6fd2807SJeff Garzik 
260c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
261c6fd2807SJeff Garzik 	} else
262c6fd2807SJeff Garzik 		spin_unlock_wait(ap->lock);
263c6fd2807SJeff Garzik 
264c6fd2807SJeff Garzik  repeat:
265c6fd2807SJeff Garzik 	/* invoke error handler */
266c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
267c6fd2807SJeff Garzik 		/* process port resume request */
268c6fd2807SJeff Garzik 		ata_eh_handle_port_resume(ap);
269c6fd2807SJeff Garzik 
270c6fd2807SJeff Garzik 		/* fetch & clear EH info */
271c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
272c6fd2807SJeff Garzik 
273c6fd2807SJeff Garzik 		memset(&ap->eh_context, 0, sizeof(ap->eh_context));
274c6fd2807SJeff Garzik 		ap->eh_context.i = ap->eh_info;
275c6fd2807SJeff Garzik 		memset(&ap->eh_info, 0, sizeof(ap->eh_info));
276c6fd2807SJeff Garzik 
277c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
278c6fd2807SJeff Garzik 		ap->pflags &= ~ATA_PFLAG_EH_PENDING;
279c6fd2807SJeff Garzik 
280c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
281c6fd2807SJeff Garzik 
282c6fd2807SJeff Garzik 		/* invoke EH, skip if unloading or suspended */
283c6fd2807SJeff Garzik 		if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
284c6fd2807SJeff Garzik 			ap->ops->error_handler(ap);
285c6fd2807SJeff Garzik 		else
286c6fd2807SJeff Garzik 			ata_eh_finish(ap);
287c6fd2807SJeff Garzik 
288c6fd2807SJeff Garzik 		/* process port suspend request */
289c6fd2807SJeff Garzik 		ata_eh_handle_port_suspend(ap);
290c6fd2807SJeff Garzik 
291c6fd2807SJeff Garzik 		/* Exception might have happend after ->error_handler
292c6fd2807SJeff Garzik 		 * recovered the port but before this point.  Repeat
293c6fd2807SJeff Garzik 		 * EH in such case.
294c6fd2807SJeff Garzik 		 */
295c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
296c6fd2807SJeff Garzik 
297c6fd2807SJeff Garzik 		if (ap->pflags & ATA_PFLAG_EH_PENDING) {
298c6fd2807SJeff Garzik 			if (--repeat_cnt) {
299c6fd2807SJeff Garzik 				ata_port_printk(ap, KERN_INFO,
300c6fd2807SJeff Garzik 					"EH pending after completion, "
301c6fd2807SJeff Garzik 					"repeating EH (cnt=%d)\n", repeat_cnt);
302c6fd2807SJeff Garzik 				spin_unlock_irqrestore(ap->lock, flags);
303c6fd2807SJeff Garzik 				goto repeat;
304c6fd2807SJeff Garzik 			}
305c6fd2807SJeff Garzik 			ata_port_printk(ap, KERN_ERR, "EH pending after %d "
306c6fd2807SJeff Garzik 					"tries, giving up\n", ATA_EH_MAX_REPEAT);
307c6fd2807SJeff Garzik 		}
308c6fd2807SJeff Garzik 
309c6fd2807SJeff Garzik 		/* this run is complete, make sure EH info is clear */
310c6fd2807SJeff Garzik 		memset(&ap->eh_info, 0, sizeof(ap->eh_info));
311c6fd2807SJeff Garzik 
312c6fd2807SJeff Garzik 		/* Clear host_eh_scheduled while holding ap->lock such
313c6fd2807SJeff Garzik 		 * that if exception occurs after this point but
314c6fd2807SJeff Garzik 		 * before EH completion, SCSI midlayer will
315c6fd2807SJeff Garzik 		 * re-initiate EH.
316c6fd2807SJeff Garzik 		 */
317c6fd2807SJeff Garzik 		host->host_eh_scheduled = 0;
318c6fd2807SJeff Garzik 
319c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
320c6fd2807SJeff Garzik 	} else {
321c6fd2807SJeff Garzik 		WARN_ON(ata_qc_from_tag(ap, ap->active_tag) == NULL);
322c6fd2807SJeff Garzik 		ap->ops->eng_timeout(ap);
323c6fd2807SJeff Garzik 	}
324c6fd2807SJeff Garzik 
325c6fd2807SJeff Garzik 	/* finish or retry handled scmd's and clean up */
326c6fd2807SJeff Garzik 	WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
327c6fd2807SJeff Garzik 
328c6fd2807SJeff Garzik 	scsi_eh_flush_done_q(&ap->eh_done_q);
329c6fd2807SJeff Garzik 
330c6fd2807SJeff Garzik 	/* clean up */
331c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
332c6fd2807SJeff Garzik 
333c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_LOADING)
334c6fd2807SJeff Garzik 		ap->pflags &= ~ATA_PFLAG_LOADING;
335c6fd2807SJeff Garzik 	else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
336c6fd2807SJeff Garzik 		queue_work(ata_aux_wq, &ap->hotplug_task);
337c6fd2807SJeff Garzik 
338c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_RECOVERED)
339c6fd2807SJeff Garzik 		ata_port_printk(ap, KERN_INFO, "EH complete\n");
340c6fd2807SJeff Garzik 
341c6fd2807SJeff Garzik 	ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
342c6fd2807SJeff Garzik 
343c6fd2807SJeff Garzik 	/* tell wait_eh that we're done */
344c6fd2807SJeff Garzik 	ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
345c6fd2807SJeff Garzik 	wake_up_all(&ap->eh_wait_q);
346c6fd2807SJeff Garzik 
347c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
348c6fd2807SJeff Garzik 
349c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
350c6fd2807SJeff Garzik }
351c6fd2807SJeff Garzik 
352c6fd2807SJeff Garzik /**
353c6fd2807SJeff Garzik  *	ata_port_wait_eh - Wait for the currently pending EH to complete
354c6fd2807SJeff Garzik  *	@ap: Port to wait EH for
355c6fd2807SJeff Garzik  *
356c6fd2807SJeff Garzik  *	Wait until the currently pending EH is complete.
357c6fd2807SJeff Garzik  *
358c6fd2807SJeff Garzik  *	LOCKING:
359c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
360c6fd2807SJeff Garzik  */
361c6fd2807SJeff Garzik void ata_port_wait_eh(struct ata_port *ap)
362c6fd2807SJeff Garzik {
363c6fd2807SJeff Garzik 	unsigned long flags;
364c6fd2807SJeff Garzik 	DEFINE_WAIT(wait);
365c6fd2807SJeff Garzik 
366c6fd2807SJeff Garzik  retry:
367c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
368c6fd2807SJeff Garzik 
369c6fd2807SJeff Garzik 	while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
370c6fd2807SJeff Garzik 		prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
371c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
372c6fd2807SJeff Garzik 		schedule();
373c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
374c6fd2807SJeff Garzik 	}
375c6fd2807SJeff Garzik 	finish_wait(&ap->eh_wait_q, &wait);
376c6fd2807SJeff Garzik 
377c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
378c6fd2807SJeff Garzik 
379c6fd2807SJeff Garzik 	/* make sure SCSI EH is complete */
380*cca3974eSJeff Garzik 	if (scsi_host_in_recovery(ap->scsi_host)) {
381c6fd2807SJeff Garzik 		msleep(10);
382c6fd2807SJeff Garzik 		goto retry;
383c6fd2807SJeff Garzik 	}
384c6fd2807SJeff Garzik }
385c6fd2807SJeff Garzik 
386c6fd2807SJeff Garzik /**
387c6fd2807SJeff Garzik  *	ata_qc_timeout - Handle timeout of queued command
388c6fd2807SJeff Garzik  *	@qc: Command that timed out
389c6fd2807SJeff Garzik  *
390c6fd2807SJeff Garzik  *	Some part of the kernel (currently, only the SCSI layer)
391c6fd2807SJeff Garzik  *	has noticed that the active command on port @ap has not
392c6fd2807SJeff Garzik  *	completed after a specified length of time.  Handle this
393c6fd2807SJeff Garzik  *	condition by disabling DMA (if necessary) and completing
394c6fd2807SJeff Garzik  *	transactions, with error if necessary.
395c6fd2807SJeff Garzik  *
396c6fd2807SJeff Garzik  *	This also handles the case of the "lost interrupt", where
397c6fd2807SJeff Garzik  *	for some reason (possibly hardware bug, possibly driver bug)
398c6fd2807SJeff Garzik  *	an interrupt was not delivered to the driver, even though the
399c6fd2807SJeff Garzik  *	transaction completed successfully.
400c6fd2807SJeff Garzik  *
401c6fd2807SJeff Garzik  *	TODO: kill this function once old EH is gone.
402c6fd2807SJeff Garzik  *
403c6fd2807SJeff Garzik  *	LOCKING:
404c6fd2807SJeff Garzik  *	Inherited from SCSI layer (none, can sleep)
405c6fd2807SJeff Garzik  */
406c6fd2807SJeff Garzik static void ata_qc_timeout(struct ata_queued_cmd *qc)
407c6fd2807SJeff Garzik {
408c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
409c6fd2807SJeff Garzik 	u8 host_stat = 0, drv_stat;
410c6fd2807SJeff Garzik 	unsigned long flags;
411c6fd2807SJeff Garzik 
412c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
413c6fd2807SJeff Garzik 
414c6fd2807SJeff Garzik 	ap->hsm_task_state = HSM_ST_IDLE;
415c6fd2807SJeff Garzik 
416c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
417c6fd2807SJeff Garzik 
418c6fd2807SJeff Garzik 	switch (qc->tf.protocol) {
419c6fd2807SJeff Garzik 
420c6fd2807SJeff Garzik 	case ATA_PROT_DMA:
421c6fd2807SJeff Garzik 	case ATA_PROT_ATAPI_DMA:
422c6fd2807SJeff Garzik 		host_stat = ap->ops->bmdma_status(ap);
423c6fd2807SJeff Garzik 
424c6fd2807SJeff Garzik 		/* before we do anything else, clear DMA-Start bit */
425c6fd2807SJeff Garzik 		ap->ops->bmdma_stop(qc);
426c6fd2807SJeff Garzik 
427c6fd2807SJeff Garzik 		/* fall through */
428c6fd2807SJeff Garzik 
429c6fd2807SJeff Garzik 	default:
430c6fd2807SJeff Garzik 		ata_altstatus(ap);
431c6fd2807SJeff Garzik 		drv_stat = ata_chk_status(ap);
432c6fd2807SJeff Garzik 
433c6fd2807SJeff Garzik 		/* ack bmdma irq events */
434c6fd2807SJeff Garzik 		ap->ops->irq_clear(ap);
435c6fd2807SJeff Garzik 
436c6fd2807SJeff Garzik 		ata_dev_printk(qc->dev, KERN_ERR, "command 0x%x timeout, "
437c6fd2807SJeff Garzik 			       "stat 0x%x host_stat 0x%x\n",
438c6fd2807SJeff Garzik 			       qc->tf.command, drv_stat, host_stat);
439c6fd2807SJeff Garzik 
440c6fd2807SJeff Garzik 		/* complete taskfile transaction */
441c6fd2807SJeff Garzik 		qc->err_mask |= AC_ERR_TIMEOUT;
442c6fd2807SJeff Garzik 		break;
443c6fd2807SJeff Garzik 	}
444c6fd2807SJeff Garzik 
445c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
446c6fd2807SJeff Garzik 
447c6fd2807SJeff Garzik 	ata_eh_qc_complete(qc);
448c6fd2807SJeff Garzik 
449c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
450c6fd2807SJeff Garzik }
451c6fd2807SJeff Garzik 
452c6fd2807SJeff Garzik /**
453c6fd2807SJeff Garzik  *	ata_eng_timeout - Handle timeout of queued command
454c6fd2807SJeff Garzik  *	@ap: Port on which timed-out command is active
455c6fd2807SJeff Garzik  *
456c6fd2807SJeff Garzik  *	Some part of the kernel (currently, only the SCSI layer)
457c6fd2807SJeff Garzik  *	has noticed that the active command on port @ap has not
458c6fd2807SJeff Garzik  *	completed after a specified length of time.  Handle this
459c6fd2807SJeff Garzik  *	condition by disabling DMA (if necessary) and completing
460c6fd2807SJeff Garzik  *	transactions, with error if necessary.
461c6fd2807SJeff Garzik  *
462c6fd2807SJeff Garzik  *	This also handles the case of the "lost interrupt", where
463c6fd2807SJeff Garzik  *	for some reason (possibly hardware bug, possibly driver bug)
464c6fd2807SJeff Garzik  *	an interrupt was not delivered to the driver, even though the
465c6fd2807SJeff Garzik  *	transaction completed successfully.
466c6fd2807SJeff Garzik  *
467c6fd2807SJeff Garzik  *	TODO: kill this function once old EH is gone.
468c6fd2807SJeff Garzik  *
469c6fd2807SJeff Garzik  *	LOCKING:
470c6fd2807SJeff Garzik  *	Inherited from SCSI layer (none, can sleep)
471c6fd2807SJeff Garzik  */
472c6fd2807SJeff Garzik void ata_eng_timeout(struct ata_port *ap)
473c6fd2807SJeff Garzik {
474c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
475c6fd2807SJeff Garzik 
476c6fd2807SJeff Garzik 	ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
477c6fd2807SJeff Garzik 
478c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
479c6fd2807SJeff Garzik }
480c6fd2807SJeff Garzik 
481c6fd2807SJeff Garzik /**
482c6fd2807SJeff Garzik  *	ata_qc_schedule_eh - schedule qc for error handling
483c6fd2807SJeff Garzik  *	@qc: command to schedule error handling for
484c6fd2807SJeff Garzik  *
485c6fd2807SJeff Garzik  *	Schedule error handling for @qc.  EH will kick in as soon as
486c6fd2807SJeff Garzik  *	other commands are drained.
487c6fd2807SJeff Garzik  *
488c6fd2807SJeff Garzik  *	LOCKING:
489*cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
490c6fd2807SJeff Garzik  */
491c6fd2807SJeff Garzik void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
492c6fd2807SJeff Garzik {
493c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
494c6fd2807SJeff Garzik 
495c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
496c6fd2807SJeff Garzik 
497c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_FAILED;
498c6fd2807SJeff Garzik 	qc->ap->pflags |= ATA_PFLAG_EH_PENDING;
499c6fd2807SJeff Garzik 
500c6fd2807SJeff Garzik 	/* The following will fail if timeout has already expired.
501c6fd2807SJeff Garzik 	 * ata_scsi_error() takes care of such scmds on EH entry.
502c6fd2807SJeff Garzik 	 * Note that ATA_QCFLAG_FAILED is unconditionally set after
503c6fd2807SJeff Garzik 	 * this function completes.
504c6fd2807SJeff Garzik 	 */
505c6fd2807SJeff Garzik 	scsi_req_abort_cmd(qc->scsicmd);
506c6fd2807SJeff Garzik }
507c6fd2807SJeff Garzik 
508c6fd2807SJeff Garzik /**
509c6fd2807SJeff Garzik  *	ata_port_schedule_eh - schedule error handling without a qc
510c6fd2807SJeff Garzik  *	@ap: ATA port to schedule EH for
511c6fd2807SJeff Garzik  *
512c6fd2807SJeff Garzik  *	Schedule error handling for @ap.  EH will kick in as soon as
513c6fd2807SJeff Garzik  *	all commands are drained.
514c6fd2807SJeff Garzik  *
515c6fd2807SJeff Garzik  *	LOCKING:
516*cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
517c6fd2807SJeff Garzik  */
518c6fd2807SJeff Garzik void ata_port_schedule_eh(struct ata_port *ap)
519c6fd2807SJeff Garzik {
520c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
521c6fd2807SJeff Garzik 
522c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_EH_PENDING;
523*cca3974eSJeff Garzik 	scsi_schedule_eh(ap->scsi_host);
524c6fd2807SJeff Garzik 
525c6fd2807SJeff Garzik 	DPRINTK("port EH scheduled\n");
526c6fd2807SJeff Garzik }
527c6fd2807SJeff Garzik 
528c6fd2807SJeff Garzik /**
529c6fd2807SJeff Garzik  *	ata_port_abort - abort all qc's on the port
530c6fd2807SJeff Garzik  *	@ap: ATA port to abort qc's for
531c6fd2807SJeff Garzik  *
532c6fd2807SJeff Garzik  *	Abort all active qc's of @ap and schedule EH.
533c6fd2807SJeff Garzik  *
534c6fd2807SJeff Garzik  *	LOCKING:
535*cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
536c6fd2807SJeff Garzik  *
537c6fd2807SJeff Garzik  *	RETURNS:
538c6fd2807SJeff Garzik  *	Number of aborted qc's.
539c6fd2807SJeff Garzik  */
540c6fd2807SJeff Garzik int ata_port_abort(struct ata_port *ap)
541c6fd2807SJeff Garzik {
542c6fd2807SJeff Garzik 	int tag, nr_aborted = 0;
543c6fd2807SJeff Garzik 
544c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
545c6fd2807SJeff Garzik 
546c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
547c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
548c6fd2807SJeff Garzik 
549c6fd2807SJeff Garzik 		if (qc) {
550c6fd2807SJeff Garzik 			qc->flags |= ATA_QCFLAG_FAILED;
551c6fd2807SJeff Garzik 			ata_qc_complete(qc);
552c6fd2807SJeff Garzik 			nr_aborted++;
553c6fd2807SJeff Garzik 		}
554c6fd2807SJeff Garzik 	}
555c6fd2807SJeff Garzik 
556c6fd2807SJeff Garzik 	if (!nr_aborted)
557c6fd2807SJeff Garzik 		ata_port_schedule_eh(ap);
558c6fd2807SJeff Garzik 
559c6fd2807SJeff Garzik 	return nr_aborted;
560c6fd2807SJeff Garzik }
561c6fd2807SJeff Garzik 
562c6fd2807SJeff Garzik /**
563c6fd2807SJeff Garzik  *	__ata_port_freeze - freeze port
564c6fd2807SJeff Garzik  *	@ap: ATA port to freeze
565c6fd2807SJeff Garzik  *
566c6fd2807SJeff Garzik  *	This function is called when HSM violation or some other
567c6fd2807SJeff Garzik  *	condition disrupts normal operation of the port.  Frozen port
568c6fd2807SJeff Garzik  *	is not allowed to perform any operation until the port is
569c6fd2807SJeff Garzik  *	thawed, which usually follows a successful reset.
570c6fd2807SJeff Garzik  *
571c6fd2807SJeff Garzik  *	ap->ops->freeze() callback can be used for freezing the port
572c6fd2807SJeff Garzik  *	hardware-wise (e.g. mask interrupt and stop DMA engine).  If a
573c6fd2807SJeff Garzik  *	port cannot be frozen hardware-wise, the interrupt handler
574c6fd2807SJeff Garzik  *	must ack and clear interrupts unconditionally while the port
575c6fd2807SJeff Garzik  *	is frozen.
576c6fd2807SJeff Garzik  *
577c6fd2807SJeff Garzik  *	LOCKING:
578*cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
579c6fd2807SJeff Garzik  */
580c6fd2807SJeff Garzik static void __ata_port_freeze(struct ata_port *ap)
581c6fd2807SJeff Garzik {
582c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
583c6fd2807SJeff Garzik 
584c6fd2807SJeff Garzik 	if (ap->ops->freeze)
585c6fd2807SJeff Garzik 		ap->ops->freeze(ap);
586c6fd2807SJeff Garzik 
587c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_FROZEN;
588c6fd2807SJeff Garzik 
589c6fd2807SJeff Garzik 	DPRINTK("ata%u port frozen\n", ap->id);
590c6fd2807SJeff Garzik }
591c6fd2807SJeff Garzik 
592c6fd2807SJeff Garzik /**
593c6fd2807SJeff Garzik  *	ata_port_freeze - abort & freeze port
594c6fd2807SJeff Garzik  *	@ap: ATA port to freeze
595c6fd2807SJeff Garzik  *
596c6fd2807SJeff Garzik  *	Abort and freeze @ap.
597c6fd2807SJeff Garzik  *
598c6fd2807SJeff Garzik  *	LOCKING:
599*cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
600c6fd2807SJeff Garzik  *
601c6fd2807SJeff Garzik  *	RETURNS:
602c6fd2807SJeff Garzik  *	Number of aborted commands.
603c6fd2807SJeff Garzik  */
604c6fd2807SJeff Garzik int ata_port_freeze(struct ata_port *ap)
605c6fd2807SJeff Garzik {
606c6fd2807SJeff Garzik 	int nr_aborted;
607c6fd2807SJeff Garzik 
608c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
609c6fd2807SJeff Garzik 
610c6fd2807SJeff Garzik 	nr_aborted = ata_port_abort(ap);
611c6fd2807SJeff Garzik 	__ata_port_freeze(ap);
612c6fd2807SJeff Garzik 
613c6fd2807SJeff Garzik 	return nr_aborted;
614c6fd2807SJeff Garzik }
615c6fd2807SJeff Garzik 
616c6fd2807SJeff Garzik /**
617c6fd2807SJeff Garzik  *	ata_eh_freeze_port - EH helper to freeze port
618c6fd2807SJeff Garzik  *	@ap: ATA port to freeze
619c6fd2807SJeff Garzik  *
620c6fd2807SJeff Garzik  *	Freeze @ap.
621c6fd2807SJeff Garzik  *
622c6fd2807SJeff Garzik  *	LOCKING:
623c6fd2807SJeff Garzik  *	None.
624c6fd2807SJeff Garzik  */
625c6fd2807SJeff Garzik void ata_eh_freeze_port(struct ata_port *ap)
626c6fd2807SJeff Garzik {
627c6fd2807SJeff Garzik 	unsigned long flags;
628c6fd2807SJeff Garzik 
629c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
630c6fd2807SJeff Garzik 		return;
631c6fd2807SJeff Garzik 
632c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
633c6fd2807SJeff Garzik 	__ata_port_freeze(ap);
634c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
635c6fd2807SJeff Garzik }
636c6fd2807SJeff Garzik 
637c6fd2807SJeff Garzik /**
638c6fd2807SJeff Garzik  *	ata_port_thaw_port - EH helper to thaw port
639c6fd2807SJeff Garzik  *	@ap: ATA port to thaw
640c6fd2807SJeff Garzik  *
641c6fd2807SJeff Garzik  *	Thaw frozen port @ap.
642c6fd2807SJeff Garzik  *
643c6fd2807SJeff Garzik  *	LOCKING:
644c6fd2807SJeff Garzik  *	None.
645c6fd2807SJeff Garzik  */
646c6fd2807SJeff Garzik void ata_eh_thaw_port(struct ata_port *ap)
647c6fd2807SJeff Garzik {
648c6fd2807SJeff Garzik 	unsigned long flags;
649c6fd2807SJeff Garzik 
650c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
651c6fd2807SJeff Garzik 		return;
652c6fd2807SJeff Garzik 
653c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
654c6fd2807SJeff Garzik 
655c6fd2807SJeff Garzik 	ap->pflags &= ~ATA_PFLAG_FROZEN;
656c6fd2807SJeff Garzik 
657c6fd2807SJeff Garzik 	if (ap->ops->thaw)
658c6fd2807SJeff Garzik 		ap->ops->thaw(ap);
659c6fd2807SJeff Garzik 
660c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
661c6fd2807SJeff Garzik 
662c6fd2807SJeff Garzik 	DPRINTK("ata%u port thawed\n", ap->id);
663c6fd2807SJeff Garzik }
664c6fd2807SJeff Garzik 
665c6fd2807SJeff Garzik static void ata_eh_scsidone(struct scsi_cmnd *scmd)
666c6fd2807SJeff Garzik {
667c6fd2807SJeff Garzik 	/* nada */
668c6fd2807SJeff Garzik }
669c6fd2807SJeff Garzik 
670c6fd2807SJeff Garzik static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
671c6fd2807SJeff Garzik {
672c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
673c6fd2807SJeff Garzik 	struct scsi_cmnd *scmd = qc->scsicmd;
674c6fd2807SJeff Garzik 	unsigned long flags;
675c6fd2807SJeff Garzik 
676c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
677c6fd2807SJeff Garzik 	qc->scsidone = ata_eh_scsidone;
678c6fd2807SJeff Garzik 	__ata_qc_complete(qc);
679c6fd2807SJeff Garzik 	WARN_ON(ata_tag_valid(qc->tag));
680c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
681c6fd2807SJeff Garzik 
682c6fd2807SJeff Garzik 	scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
683c6fd2807SJeff Garzik }
684c6fd2807SJeff Garzik 
685c6fd2807SJeff Garzik /**
686c6fd2807SJeff Garzik  *	ata_eh_qc_complete - Complete an active ATA command from EH
687c6fd2807SJeff Garzik  *	@qc: Command to complete
688c6fd2807SJeff Garzik  *
689c6fd2807SJeff Garzik  *	Indicate to the mid and upper layers that an ATA command has
690c6fd2807SJeff Garzik  *	completed.  To be used from EH.
691c6fd2807SJeff Garzik  */
692c6fd2807SJeff Garzik void ata_eh_qc_complete(struct ata_queued_cmd *qc)
693c6fd2807SJeff Garzik {
694c6fd2807SJeff Garzik 	struct scsi_cmnd *scmd = qc->scsicmd;
695c6fd2807SJeff Garzik 	scmd->retries = scmd->allowed;
696c6fd2807SJeff Garzik 	__ata_eh_qc_complete(qc);
697c6fd2807SJeff Garzik }
698c6fd2807SJeff Garzik 
699c6fd2807SJeff Garzik /**
700c6fd2807SJeff Garzik  *	ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
701c6fd2807SJeff Garzik  *	@qc: Command to retry
702c6fd2807SJeff Garzik  *
703c6fd2807SJeff Garzik  *	Indicate to the mid and upper layers that an ATA command
704c6fd2807SJeff Garzik  *	should be retried.  To be used from EH.
705c6fd2807SJeff Garzik  *
706c6fd2807SJeff Garzik  *	SCSI midlayer limits the number of retries to scmd->allowed.
707c6fd2807SJeff Garzik  *	scmd->retries is decremented for commands which get retried
708c6fd2807SJeff Garzik  *	due to unrelated failures (qc->err_mask is zero).
709c6fd2807SJeff Garzik  */
710c6fd2807SJeff Garzik void ata_eh_qc_retry(struct ata_queued_cmd *qc)
711c6fd2807SJeff Garzik {
712c6fd2807SJeff Garzik 	struct scsi_cmnd *scmd = qc->scsicmd;
713c6fd2807SJeff Garzik 	if (!qc->err_mask && scmd->retries)
714c6fd2807SJeff Garzik 		scmd->retries--;
715c6fd2807SJeff Garzik 	__ata_eh_qc_complete(qc);
716c6fd2807SJeff Garzik }
717c6fd2807SJeff Garzik 
718c6fd2807SJeff Garzik /**
719c6fd2807SJeff Garzik  *	ata_eh_detach_dev - detach ATA device
720c6fd2807SJeff Garzik  *	@dev: ATA device to detach
721c6fd2807SJeff Garzik  *
722c6fd2807SJeff Garzik  *	Detach @dev.
723c6fd2807SJeff Garzik  *
724c6fd2807SJeff Garzik  *	LOCKING:
725c6fd2807SJeff Garzik  *	None.
726c6fd2807SJeff Garzik  */
727c6fd2807SJeff Garzik static void ata_eh_detach_dev(struct ata_device *dev)
728c6fd2807SJeff Garzik {
729c6fd2807SJeff Garzik 	struct ata_port *ap = dev->ap;
730c6fd2807SJeff Garzik 	unsigned long flags;
731c6fd2807SJeff Garzik 
732c6fd2807SJeff Garzik 	ata_dev_disable(dev);
733c6fd2807SJeff Garzik 
734c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
735c6fd2807SJeff Garzik 
736c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_DETACH;
737c6fd2807SJeff Garzik 
738c6fd2807SJeff Garzik 	if (ata_scsi_offline_dev(dev)) {
739c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_DETACHED;
740c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
741c6fd2807SJeff Garzik 	}
742c6fd2807SJeff Garzik 
743c6fd2807SJeff Garzik 	/* clear per-dev EH actions */
744c6fd2807SJeff Garzik 	ata_eh_clear_action(dev, &ap->eh_info, ATA_EH_PERDEV_MASK);
745c6fd2807SJeff Garzik 	ata_eh_clear_action(dev, &ap->eh_context.i, ATA_EH_PERDEV_MASK);
746c6fd2807SJeff Garzik 
747c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
748c6fd2807SJeff Garzik }
749c6fd2807SJeff Garzik 
750c6fd2807SJeff Garzik /**
751c6fd2807SJeff Garzik  *	ata_eh_about_to_do - about to perform eh_action
752c6fd2807SJeff Garzik  *	@ap: target ATA port
753c6fd2807SJeff Garzik  *	@dev: target ATA dev for per-dev action (can be NULL)
754c6fd2807SJeff Garzik  *	@action: action about to be performed
755c6fd2807SJeff Garzik  *
756c6fd2807SJeff Garzik  *	Called just before performing EH actions to clear related bits
757c6fd2807SJeff Garzik  *	in @ap->eh_info such that eh actions are not unnecessarily
758c6fd2807SJeff Garzik  *	repeated.
759c6fd2807SJeff Garzik  *
760c6fd2807SJeff Garzik  *	LOCKING:
761c6fd2807SJeff Garzik  *	None.
762c6fd2807SJeff Garzik  */
763c6fd2807SJeff Garzik static void ata_eh_about_to_do(struct ata_port *ap, struct ata_device *dev,
764c6fd2807SJeff Garzik 			       unsigned int action)
765c6fd2807SJeff Garzik {
766c6fd2807SJeff Garzik 	unsigned long flags;
767c6fd2807SJeff Garzik 	struct ata_eh_info *ehi = &ap->eh_info;
768c6fd2807SJeff Garzik 	struct ata_eh_context *ehc = &ap->eh_context;
769c6fd2807SJeff Garzik 
770c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
771c6fd2807SJeff Garzik 
772c6fd2807SJeff Garzik 	/* Reset is represented by combination of actions and EHI
773c6fd2807SJeff Garzik 	 * flags.  Suck in all related bits before clearing eh_info to
774c6fd2807SJeff Garzik 	 * avoid losing requested action.
775c6fd2807SJeff Garzik 	 */
776c6fd2807SJeff Garzik 	if (action & ATA_EH_RESET_MASK) {
777c6fd2807SJeff Garzik 		ehc->i.action |= ehi->action & ATA_EH_RESET_MASK;
778c6fd2807SJeff Garzik 		ehc->i.flags |= ehi->flags & ATA_EHI_RESET_MODIFIER_MASK;
779c6fd2807SJeff Garzik 
780c6fd2807SJeff Garzik 		/* make sure all reset actions are cleared & clear EHI flags */
781c6fd2807SJeff Garzik 		action |= ATA_EH_RESET_MASK;
782c6fd2807SJeff Garzik 		ehi->flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
783c6fd2807SJeff Garzik 	}
784c6fd2807SJeff Garzik 
785c6fd2807SJeff Garzik 	ata_eh_clear_action(dev, ehi, action);
786c6fd2807SJeff Garzik 
787c6fd2807SJeff Garzik 	if (!(ehc->i.flags & ATA_EHI_QUIET))
788c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_RECOVERED;
789c6fd2807SJeff Garzik 
790c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
791c6fd2807SJeff Garzik }
792c6fd2807SJeff Garzik 
793c6fd2807SJeff Garzik /**
794c6fd2807SJeff Garzik  *	ata_eh_done - EH action complete
795c6fd2807SJeff Garzik  *	@ap: target ATA port
796c6fd2807SJeff Garzik  *	@dev: target ATA dev for per-dev action (can be NULL)
797c6fd2807SJeff Garzik  *	@action: action just completed
798c6fd2807SJeff Garzik  *
799c6fd2807SJeff Garzik  *	Called right after performing EH actions to clear related bits
800c6fd2807SJeff Garzik  *	in @ap->eh_context.
801c6fd2807SJeff Garzik  *
802c6fd2807SJeff Garzik  *	LOCKING:
803c6fd2807SJeff Garzik  *	None.
804c6fd2807SJeff Garzik  */
805c6fd2807SJeff Garzik static void ata_eh_done(struct ata_port *ap, struct ata_device *dev,
806c6fd2807SJeff Garzik 			unsigned int action)
807c6fd2807SJeff Garzik {
808c6fd2807SJeff Garzik 	/* if reset is complete, clear all reset actions & reset modifier */
809c6fd2807SJeff Garzik 	if (action & ATA_EH_RESET_MASK) {
810c6fd2807SJeff Garzik 		action |= ATA_EH_RESET_MASK;
811c6fd2807SJeff Garzik 		ap->eh_context.i.flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
812c6fd2807SJeff Garzik 	}
813c6fd2807SJeff Garzik 
814c6fd2807SJeff Garzik 	ata_eh_clear_action(dev, &ap->eh_context.i, action);
815c6fd2807SJeff Garzik }
816c6fd2807SJeff Garzik 
817c6fd2807SJeff Garzik /**
818c6fd2807SJeff Garzik  *	ata_err_string - convert err_mask to descriptive string
819c6fd2807SJeff Garzik  *	@err_mask: error mask to convert to string
820c6fd2807SJeff Garzik  *
821c6fd2807SJeff Garzik  *	Convert @err_mask to descriptive string.  Errors are
822c6fd2807SJeff Garzik  *	prioritized according to severity and only the most severe
823c6fd2807SJeff Garzik  *	error is reported.
824c6fd2807SJeff Garzik  *
825c6fd2807SJeff Garzik  *	LOCKING:
826c6fd2807SJeff Garzik  *	None.
827c6fd2807SJeff Garzik  *
828c6fd2807SJeff Garzik  *	RETURNS:
829c6fd2807SJeff Garzik  *	Descriptive string for @err_mask
830c6fd2807SJeff Garzik  */
831c6fd2807SJeff Garzik static const char * ata_err_string(unsigned int err_mask)
832c6fd2807SJeff Garzik {
833c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_HOST_BUS)
834c6fd2807SJeff Garzik 		return "host bus error";
835c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_ATA_BUS)
836c6fd2807SJeff Garzik 		return "ATA bus error";
837c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_TIMEOUT)
838c6fd2807SJeff Garzik 		return "timeout";
839c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_HSM)
840c6fd2807SJeff Garzik 		return "HSM violation";
841c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_SYSTEM)
842c6fd2807SJeff Garzik 		return "internal error";
843c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_MEDIA)
844c6fd2807SJeff Garzik 		return "media error";
845c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_INVALID)
846c6fd2807SJeff Garzik 		return "invalid argument";
847c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_DEV)
848c6fd2807SJeff Garzik 		return "device error";
849c6fd2807SJeff Garzik 	return "unknown error";
850c6fd2807SJeff Garzik }
851c6fd2807SJeff Garzik 
852c6fd2807SJeff Garzik /**
853c6fd2807SJeff Garzik  *	ata_read_log_page - read a specific log page
854c6fd2807SJeff Garzik  *	@dev: target device
855c6fd2807SJeff Garzik  *	@page: page to read
856c6fd2807SJeff Garzik  *	@buf: buffer to store read page
857c6fd2807SJeff Garzik  *	@sectors: number of sectors to read
858c6fd2807SJeff Garzik  *
859c6fd2807SJeff Garzik  *	Read log page using READ_LOG_EXT command.
860c6fd2807SJeff Garzik  *
861c6fd2807SJeff Garzik  *	LOCKING:
862c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
863c6fd2807SJeff Garzik  *
864c6fd2807SJeff Garzik  *	RETURNS:
865c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
866c6fd2807SJeff Garzik  */
867c6fd2807SJeff Garzik static unsigned int ata_read_log_page(struct ata_device *dev,
868c6fd2807SJeff Garzik 				      u8 page, void *buf, unsigned int sectors)
869c6fd2807SJeff Garzik {
870c6fd2807SJeff Garzik 	struct ata_taskfile tf;
871c6fd2807SJeff Garzik 	unsigned int err_mask;
872c6fd2807SJeff Garzik 
873c6fd2807SJeff Garzik 	DPRINTK("read log page - page %d\n", page);
874c6fd2807SJeff Garzik 
875c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
876c6fd2807SJeff Garzik 	tf.command = ATA_CMD_READ_LOG_EXT;
877c6fd2807SJeff Garzik 	tf.lbal = page;
878c6fd2807SJeff Garzik 	tf.nsect = sectors;
879c6fd2807SJeff Garzik 	tf.hob_nsect = sectors >> 8;
880c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
881c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_PIO;
882c6fd2807SJeff Garzik 
883c6fd2807SJeff Garzik 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
884c6fd2807SJeff Garzik 				     buf, sectors * ATA_SECT_SIZE);
885c6fd2807SJeff Garzik 
886c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
887c6fd2807SJeff Garzik 	return err_mask;
888c6fd2807SJeff Garzik }
889c6fd2807SJeff Garzik 
890c6fd2807SJeff Garzik /**
891c6fd2807SJeff Garzik  *	ata_eh_read_log_10h - Read log page 10h for NCQ error details
892c6fd2807SJeff Garzik  *	@dev: Device to read log page 10h from
893c6fd2807SJeff Garzik  *	@tag: Resulting tag of the failed command
894c6fd2807SJeff Garzik  *	@tf: Resulting taskfile registers of the failed command
895c6fd2807SJeff Garzik  *
896c6fd2807SJeff Garzik  *	Read log page 10h to obtain NCQ error details and clear error
897c6fd2807SJeff Garzik  *	condition.
898c6fd2807SJeff Garzik  *
899c6fd2807SJeff Garzik  *	LOCKING:
900c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
901c6fd2807SJeff Garzik  *
902c6fd2807SJeff Garzik  *	RETURNS:
903c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
904c6fd2807SJeff Garzik  */
905c6fd2807SJeff Garzik static int ata_eh_read_log_10h(struct ata_device *dev,
906c6fd2807SJeff Garzik 			       int *tag, struct ata_taskfile *tf)
907c6fd2807SJeff Garzik {
908c6fd2807SJeff Garzik 	u8 *buf = dev->ap->sector_buf;
909c6fd2807SJeff Garzik 	unsigned int err_mask;
910c6fd2807SJeff Garzik 	u8 csum;
911c6fd2807SJeff Garzik 	int i;
912c6fd2807SJeff Garzik 
913c6fd2807SJeff Garzik 	err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
914c6fd2807SJeff Garzik 	if (err_mask)
915c6fd2807SJeff Garzik 		return -EIO;
916c6fd2807SJeff Garzik 
917c6fd2807SJeff Garzik 	csum = 0;
918c6fd2807SJeff Garzik 	for (i = 0; i < ATA_SECT_SIZE; i++)
919c6fd2807SJeff Garzik 		csum += buf[i];
920c6fd2807SJeff Garzik 	if (csum)
921c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_WARNING,
922c6fd2807SJeff Garzik 			       "invalid checksum 0x%x on log page 10h\n", csum);
923c6fd2807SJeff Garzik 
924c6fd2807SJeff Garzik 	if (buf[0] & 0x80)
925c6fd2807SJeff Garzik 		return -ENOENT;
926c6fd2807SJeff Garzik 
927c6fd2807SJeff Garzik 	*tag = buf[0] & 0x1f;
928c6fd2807SJeff Garzik 
929c6fd2807SJeff Garzik 	tf->command = buf[2];
930c6fd2807SJeff Garzik 	tf->feature = buf[3];
931c6fd2807SJeff Garzik 	tf->lbal = buf[4];
932c6fd2807SJeff Garzik 	tf->lbam = buf[5];
933c6fd2807SJeff Garzik 	tf->lbah = buf[6];
934c6fd2807SJeff Garzik 	tf->device = buf[7];
935c6fd2807SJeff Garzik 	tf->hob_lbal = buf[8];
936c6fd2807SJeff Garzik 	tf->hob_lbam = buf[9];
937c6fd2807SJeff Garzik 	tf->hob_lbah = buf[10];
938c6fd2807SJeff Garzik 	tf->nsect = buf[12];
939c6fd2807SJeff Garzik 	tf->hob_nsect = buf[13];
940c6fd2807SJeff Garzik 
941c6fd2807SJeff Garzik 	return 0;
942c6fd2807SJeff Garzik }
943c6fd2807SJeff Garzik 
944c6fd2807SJeff Garzik /**
945c6fd2807SJeff Garzik  *	atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
946c6fd2807SJeff Garzik  *	@dev: device to perform REQUEST_SENSE to
947c6fd2807SJeff Garzik  *	@sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
948c6fd2807SJeff Garzik  *
949c6fd2807SJeff Garzik  *	Perform ATAPI REQUEST_SENSE after the device reported CHECK
950c6fd2807SJeff Garzik  *	SENSE.  This function is EH helper.
951c6fd2807SJeff Garzik  *
952c6fd2807SJeff Garzik  *	LOCKING:
953c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
954c6fd2807SJeff Garzik  *
955c6fd2807SJeff Garzik  *	RETURNS:
956c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask on failure
957c6fd2807SJeff Garzik  */
958c6fd2807SJeff Garzik static unsigned int atapi_eh_request_sense(struct ata_device *dev,
959c6fd2807SJeff Garzik 					   unsigned char *sense_buf)
960c6fd2807SJeff Garzik {
961c6fd2807SJeff Garzik 	struct ata_port *ap = dev->ap;
962c6fd2807SJeff Garzik 	struct ata_taskfile tf;
963c6fd2807SJeff Garzik 	u8 cdb[ATAPI_CDB_LEN];
964c6fd2807SJeff Garzik 
965c6fd2807SJeff Garzik 	DPRINTK("ATAPI request sense\n");
966c6fd2807SJeff Garzik 
967c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
968c6fd2807SJeff Garzik 
969c6fd2807SJeff Garzik 	/* FIXME: is this needed? */
970c6fd2807SJeff Garzik 	memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
971c6fd2807SJeff Garzik 
972c6fd2807SJeff Garzik 	/* XXX: why tf_read here? */
973c6fd2807SJeff Garzik 	ap->ops->tf_read(ap, &tf);
974c6fd2807SJeff Garzik 
975c6fd2807SJeff Garzik 	/* fill these in, for the case where they are -not- overwritten */
976c6fd2807SJeff Garzik 	sense_buf[0] = 0x70;
977c6fd2807SJeff Garzik 	sense_buf[2] = tf.feature >> 4;
978c6fd2807SJeff Garzik 
979c6fd2807SJeff Garzik 	memset(cdb, 0, ATAPI_CDB_LEN);
980c6fd2807SJeff Garzik 	cdb[0] = REQUEST_SENSE;
981c6fd2807SJeff Garzik 	cdb[4] = SCSI_SENSE_BUFFERSIZE;
982c6fd2807SJeff Garzik 
983c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
984c6fd2807SJeff Garzik 	tf.command = ATA_CMD_PACKET;
985c6fd2807SJeff Garzik 
986c6fd2807SJeff Garzik 	/* is it pointless to prefer PIO for "safety reasons"? */
987c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_PIO_DMA) {
988c6fd2807SJeff Garzik 		tf.protocol = ATA_PROT_ATAPI_DMA;
989c6fd2807SJeff Garzik 		tf.feature |= ATAPI_PKT_DMA;
990c6fd2807SJeff Garzik 	} else {
991c6fd2807SJeff Garzik 		tf.protocol = ATA_PROT_ATAPI;
992c6fd2807SJeff Garzik 		tf.lbam = (8 * 1024) & 0xff;
993c6fd2807SJeff Garzik 		tf.lbah = (8 * 1024) >> 8;
994c6fd2807SJeff Garzik 	}
995c6fd2807SJeff Garzik 
996c6fd2807SJeff Garzik 	return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
997c6fd2807SJeff Garzik 				 sense_buf, SCSI_SENSE_BUFFERSIZE);
998c6fd2807SJeff Garzik }
999c6fd2807SJeff Garzik 
1000c6fd2807SJeff Garzik /**
1001c6fd2807SJeff Garzik  *	ata_eh_analyze_serror - analyze SError for a failed port
1002c6fd2807SJeff Garzik  *	@ap: ATA port to analyze SError for
1003c6fd2807SJeff Garzik  *
1004c6fd2807SJeff Garzik  *	Analyze SError if available and further determine cause of
1005c6fd2807SJeff Garzik  *	failure.
1006c6fd2807SJeff Garzik  *
1007c6fd2807SJeff Garzik  *	LOCKING:
1008c6fd2807SJeff Garzik  *	None.
1009c6fd2807SJeff Garzik  */
1010c6fd2807SJeff Garzik static void ata_eh_analyze_serror(struct ata_port *ap)
1011c6fd2807SJeff Garzik {
1012c6fd2807SJeff Garzik 	struct ata_eh_context *ehc = &ap->eh_context;
1013c6fd2807SJeff Garzik 	u32 serror = ehc->i.serror;
1014c6fd2807SJeff Garzik 	unsigned int err_mask = 0, action = 0;
1015c6fd2807SJeff Garzik 
1016c6fd2807SJeff Garzik 	if (serror & SERR_PERSISTENT) {
1017c6fd2807SJeff Garzik 		err_mask |= AC_ERR_ATA_BUS;
1018c6fd2807SJeff Garzik 		action |= ATA_EH_HARDRESET;
1019c6fd2807SJeff Garzik 	}
1020c6fd2807SJeff Garzik 	if (serror &
1021c6fd2807SJeff Garzik 	    (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
1022c6fd2807SJeff Garzik 		err_mask |= AC_ERR_ATA_BUS;
1023c6fd2807SJeff Garzik 		action |= ATA_EH_SOFTRESET;
1024c6fd2807SJeff Garzik 	}
1025c6fd2807SJeff Garzik 	if (serror & SERR_PROTOCOL) {
1026c6fd2807SJeff Garzik 		err_mask |= AC_ERR_HSM;
1027c6fd2807SJeff Garzik 		action |= ATA_EH_SOFTRESET;
1028c6fd2807SJeff Garzik 	}
1029c6fd2807SJeff Garzik 	if (serror & SERR_INTERNAL) {
1030c6fd2807SJeff Garzik 		err_mask |= AC_ERR_SYSTEM;
1031c6fd2807SJeff Garzik 		action |= ATA_EH_SOFTRESET;
1032c6fd2807SJeff Garzik 	}
1033c6fd2807SJeff Garzik 	if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG))
1034c6fd2807SJeff Garzik 		ata_ehi_hotplugged(&ehc->i);
1035c6fd2807SJeff Garzik 
1036c6fd2807SJeff Garzik 	ehc->i.err_mask |= err_mask;
1037c6fd2807SJeff Garzik 	ehc->i.action |= action;
1038c6fd2807SJeff Garzik }
1039c6fd2807SJeff Garzik 
1040c6fd2807SJeff Garzik /**
1041c6fd2807SJeff Garzik  *	ata_eh_analyze_ncq_error - analyze NCQ error
1042c6fd2807SJeff Garzik  *	@ap: ATA port to analyze NCQ error for
1043c6fd2807SJeff Garzik  *
1044c6fd2807SJeff Garzik  *	Read log page 10h, determine the offending qc and acquire
1045c6fd2807SJeff Garzik  *	error status TF.  For NCQ device errors, all LLDDs have to do
1046c6fd2807SJeff Garzik  *	is setting AC_ERR_DEV in ehi->err_mask.  This function takes
1047c6fd2807SJeff Garzik  *	care of the rest.
1048c6fd2807SJeff Garzik  *
1049c6fd2807SJeff Garzik  *	LOCKING:
1050c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1051c6fd2807SJeff Garzik  */
1052c6fd2807SJeff Garzik static void ata_eh_analyze_ncq_error(struct ata_port *ap)
1053c6fd2807SJeff Garzik {
1054c6fd2807SJeff Garzik 	struct ata_eh_context *ehc = &ap->eh_context;
1055c6fd2807SJeff Garzik 	struct ata_device *dev = ap->device;
1056c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
1057c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1058c6fd2807SJeff Garzik 	int tag, rc;
1059c6fd2807SJeff Garzik 
1060c6fd2807SJeff Garzik 	/* if frozen, we can't do much */
1061c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN)
1062c6fd2807SJeff Garzik 		return;
1063c6fd2807SJeff Garzik 
1064c6fd2807SJeff Garzik 	/* is it NCQ device error? */
1065c6fd2807SJeff Garzik 	if (!ap->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1066c6fd2807SJeff Garzik 		return;
1067c6fd2807SJeff Garzik 
1068c6fd2807SJeff Garzik 	/* has LLDD analyzed already? */
1069c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1070c6fd2807SJeff Garzik 		qc = __ata_qc_from_tag(ap, tag);
1071c6fd2807SJeff Garzik 
1072c6fd2807SJeff Garzik 		if (!(qc->flags & ATA_QCFLAG_FAILED))
1073c6fd2807SJeff Garzik 			continue;
1074c6fd2807SJeff Garzik 
1075c6fd2807SJeff Garzik 		if (qc->err_mask)
1076c6fd2807SJeff Garzik 			return;
1077c6fd2807SJeff Garzik 	}
1078c6fd2807SJeff Garzik 
1079c6fd2807SJeff Garzik 	/* okay, this error is ours */
1080c6fd2807SJeff Garzik 	rc = ata_eh_read_log_10h(dev, &tag, &tf);
1081c6fd2807SJeff Garzik 	if (rc) {
1082c6fd2807SJeff Garzik 		ata_port_printk(ap, KERN_ERR, "failed to read log page 10h "
1083c6fd2807SJeff Garzik 				"(errno=%d)\n", rc);
1084c6fd2807SJeff Garzik 		return;
1085c6fd2807SJeff Garzik 	}
1086c6fd2807SJeff Garzik 
1087c6fd2807SJeff Garzik 	if (!(ap->sactive & (1 << tag))) {
1088c6fd2807SJeff Garzik 		ata_port_printk(ap, KERN_ERR, "log page 10h reported "
1089c6fd2807SJeff Garzik 				"inactive tag %d\n", tag);
1090c6fd2807SJeff Garzik 		return;
1091c6fd2807SJeff Garzik 	}
1092c6fd2807SJeff Garzik 
1093c6fd2807SJeff Garzik 	/* we've got the perpetrator, condemn it */
1094c6fd2807SJeff Garzik 	qc = __ata_qc_from_tag(ap, tag);
1095c6fd2807SJeff Garzik 	memcpy(&qc->result_tf, &tf, sizeof(tf));
1096c6fd2807SJeff Garzik 	qc->err_mask |= AC_ERR_DEV;
1097c6fd2807SJeff Garzik 	ehc->i.err_mask &= ~AC_ERR_DEV;
1098c6fd2807SJeff Garzik }
1099c6fd2807SJeff Garzik 
1100c6fd2807SJeff Garzik /**
1101c6fd2807SJeff Garzik  *	ata_eh_analyze_tf - analyze taskfile of a failed qc
1102c6fd2807SJeff Garzik  *	@qc: qc to analyze
1103c6fd2807SJeff Garzik  *	@tf: Taskfile registers to analyze
1104c6fd2807SJeff Garzik  *
1105c6fd2807SJeff Garzik  *	Analyze taskfile of @qc and further determine cause of
1106c6fd2807SJeff Garzik  *	failure.  This function also requests ATAPI sense data if
1107c6fd2807SJeff Garzik  *	avaliable.
1108c6fd2807SJeff Garzik  *
1109c6fd2807SJeff Garzik  *	LOCKING:
1110c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1111c6fd2807SJeff Garzik  *
1112c6fd2807SJeff Garzik  *	RETURNS:
1113c6fd2807SJeff Garzik  *	Determined recovery action
1114c6fd2807SJeff Garzik  */
1115c6fd2807SJeff Garzik static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1116c6fd2807SJeff Garzik 				      const struct ata_taskfile *tf)
1117c6fd2807SJeff Garzik {
1118c6fd2807SJeff Garzik 	unsigned int tmp, action = 0;
1119c6fd2807SJeff Garzik 	u8 stat = tf->command, err = tf->feature;
1120c6fd2807SJeff Garzik 
1121c6fd2807SJeff Garzik 	if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1122c6fd2807SJeff Garzik 		qc->err_mask |= AC_ERR_HSM;
1123c6fd2807SJeff Garzik 		return ATA_EH_SOFTRESET;
1124c6fd2807SJeff Garzik 	}
1125c6fd2807SJeff Garzik 
1126c6fd2807SJeff Garzik 	if (!(qc->err_mask & AC_ERR_DEV))
1127c6fd2807SJeff Garzik 		return 0;
1128c6fd2807SJeff Garzik 
1129c6fd2807SJeff Garzik 	switch (qc->dev->class) {
1130c6fd2807SJeff Garzik 	case ATA_DEV_ATA:
1131c6fd2807SJeff Garzik 		if (err & ATA_ICRC)
1132c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_ATA_BUS;
1133c6fd2807SJeff Garzik 		if (err & ATA_UNC)
1134c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_MEDIA;
1135c6fd2807SJeff Garzik 		if (err & ATA_IDNF)
1136c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_INVALID;
1137c6fd2807SJeff Garzik 		break;
1138c6fd2807SJeff Garzik 
1139c6fd2807SJeff Garzik 	case ATA_DEV_ATAPI:
1140c6fd2807SJeff Garzik 		tmp = atapi_eh_request_sense(qc->dev,
1141c6fd2807SJeff Garzik 					     qc->scsicmd->sense_buffer);
1142c6fd2807SJeff Garzik 		if (!tmp) {
1143c6fd2807SJeff Garzik 			/* ATA_QCFLAG_SENSE_VALID is used to tell
1144c6fd2807SJeff Garzik 			 * atapi_qc_complete() that sense data is
1145c6fd2807SJeff Garzik 			 * already valid.
1146c6fd2807SJeff Garzik 			 *
1147c6fd2807SJeff Garzik 			 * TODO: interpret sense data and set
1148c6fd2807SJeff Garzik 			 * appropriate err_mask.
1149c6fd2807SJeff Garzik 			 */
1150c6fd2807SJeff Garzik 			qc->flags |= ATA_QCFLAG_SENSE_VALID;
1151c6fd2807SJeff Garzik 		} else
1152c6fd2807SJeff Garzik 			qc->err_mask |= tmp;
1153c6fd2807SJeff Garzik 	}
1154c6fd2807SJeff Garzik 
1155c6fd2807SJeff Garzik 	if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1156c6fd2807SJeff Garzik 		action |= ATA_EH_SOFTRESET;
1157c6fd2807SJeff Garzik 
1158c6fd2807SJeff Garzik 	return action;
1159c6fd2807SJeff Garzik }
1160c6fd2807SJeff Garzik 
1161c6fd2807SJeff Garzik static int ata_eh_categorize_ering_entry(struct ata_ering_entry *ent)
1162c6fd2807SJeff Garzik {
1163c6fd2807SJeff Garzik 	if (ent->err_mask & (AC_ERR_ATA_BUS | AC_ERR_TIMEOUT))
1164c6fd2807SJeff Garzik 		return 1;
1165c6fd2807SJeff Garzik 
1166c6fd2807SJeff Garzik 	if (ent->is_io) {
1167c6fd2807SJeff Garzik 		if (ent->err_mask & AC_ERR_HSM)
1168c6fd2807SJeff Garzik 			return 1;
1169c6fd2807SJeff Garzik 		if ((ent->err_mask &
1170c6fd2807SJeff Garzik 		     (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1171c6fd2807SJeff Garzik 			return 2;
1172c6fd2807SJeff Garzik 	}
1173c6fd2807SJeff Garzik 
1174c6fd2807SJeff Garzik 	return 0;
1175c6fd2807SJeff Garzik }
1176c6fd2807SJeff Garzik 
1177c6fd2807SJeff Garzik struct speed_down_needed_arg {
1178c6fd2807SJeff Garzik 	u64 since;
1179c6fd2807SJeff Garzik 	int nr_errors[3];
1180c6fd2807SJeff Garzik };
1181c6fd2807SJeff Garzik 
1182c6fd2807SJeff Garzik static int speed_down_needed_cb(struct ata_ering_entry *ent, void *void_arg)
1183c6fd2807SJeff Garzik {
1184c6fd2807SJeff Garzik 	struct speed_down_needed_arg *arg = void_arg;
1185c6fd2807SJeff Garzik 
1186c6fd2807SJeff Garzik 	if (ent->timestamp < arg->since)
1187c6fd2807SJeff Garzik 		return -1;
1188c6fd2807SJeff Garzik 
1189c6fd2807SJeff Garzik 	arg->nr_errors[ata_eh_categorize_ering_entry(ent)]++;
1190c6fd2807SJeff Garzik 	return 0;
1191c6fd2807SJeff Garzik }
1192c6fd2807SJeff Garzik 
1193c6fd2807SJeff Garzik /**
1194c6fd2807SJeff Garzik  *	ata_eh_speed_down_needed - Determine wheter speed down is necessary
1195c6fd2807SJeff Garzik  *	@dev: Device of interest
1196c6fd2807SJeff Garzik  *
1197c6fd2807SJeff Garzik  *	This function examines error ring of @dev and determines
1198c6fd2807SJeff Garzik  *	whether speed down is necessary.  Speed down is necessary if
1199c6fd2807SJeff Garzik  *	there have been more than 3 of Cat-1 errors or 10 of Cat-2
1200c6fd2807SJeff Garzik  *	errors during last 15 minutes.
1201c6fd2807SJeff Garzik  *
1202c6fd2807SJeff Garzik  *	Cat-1 errors are ATA_BUS, TIMEOUT for any command and HSM
1203c6fd2807SJeff Garzik  *	violation for known supported commands.
1204c6fd2807SJeff Garzik  *
1205c6fd2807SJeff Garzik  *	Cat-2 errors are unclassified DEV error for known supported
1206c6fd2807SJeff Garzik  *	command.
1207c6fd2807SJeff Garzik  *
1208c6fd2807SJeff Garzik  *	LOCKING:
1209c6fd2807SJeff Garzik  *	Inherited from caller.
1210c6fd2807SJeff Garzik  *
1211c6fd2807SJeff Garzik  *	RETURNS:
1212c6fd2807SJeff Garzik  *	1 if speed down is necessary, 0 otherwise
1213c6fd2807SJeff Garzik  */
1214c6fd2807SJeff Garzik static int ata_eh_speed_down_needed(struct ata_device *dev)
1215c6fd2807SJeff Garzik {
1216c6fd2807SJeff Garzik 	const u64 interval = 15LLU * 60 * HZ;
1217c6fd2807SJeff Garzik 	static const int err_limits[3] = { -1, 3, 10 };
1218c6fd2807SJeff Garzik 	struct speed_down_needed_arg arg;
1219c6fd2807SJeff Garzik 	struct ata_ering_entry *ent;
1220c6fd2807SJeff Garzik 	int err_cat;
1221c6fd2807SJeff Garzik 	u64 j64;
1222c6fd2807SJeff Garzik 
1223c6fd2807SJeff Garzik 	ent = ata_ering_top(&dev->ering);
1224c6fd2807SJeff Garzik 	if (!ent)
1225c6fd2807SJeff Garzik 		return 0;
1226c6fd2807SJeff Garzik 
1227c6fd2807SJeff Garzik 	err_cat = ata_eh_categorize_ering_entry(ent);
1228c6fd2807SJeff Garzik 	if (err_cat == 0)
1229c6fd2807SJeff Garzik 		return 0;
1230c6fd2807SJeff Garzik 
1231c6fd2807SJeff Garzik 	memset(&arg, 0, sizeof(arg));
1232c6fd2807SJeff Garzik 
1233c6fd2807SJeff Garzik 	j64 = get_jiffies_64();
1234c6fd2807SJeff Garzik 	if (j64 >= interval)
1235c6fd2807SJeff Garzik 		arg.since = j64 - interval;
1236c6fd2807SJeff Garzik 	else
1237c6fd2807SJeff Garzik 		arg.since = 0;
1238c6fd2807SJeff Garzik 
1239c6fd2807SJeff Garzik 	ata_ering_map(&dev->ering, speed_down_needed_cb, &arg);
1240c6fd2807SJeff Garzik 
1241c6fd2807SJeff Garzik 	return arg.nr_errors[err_cat] > err_limits[err_cat];
1242c6fd2807SJeff Garzik }
1243c6fd2807SJeff Garzik 
1244c6fd2807SJeff Garzik /**
1245c6fd2807SJeff Garzik  *	ata_eh_speed_down - record error and speed down if necessary
1246c6fd2807SJeff Garzik  *	@dev: Failed device
1247c6fd2807SJeff Garzik  *	@is_io: Did the device fail during normal IO?
1248c6fd2807SJeff Garzik  *	@err_mask: err_mask of the error
1249c6fd2807SJeff Garzik  *
1250c6fd2807SJeff Garzik  *	Record error and examine error history to determine whether
1251c6fd2807SJeff Garzik  *	adjusting transmission speed is necessary.  It also sets
1252c6fd2807SJeff Garzik  *	transmission limits appropriately if such adjustment is
1253c6fd2807SJeff Garzik  *	necessary.
1254c6fd2807SJeff Garzik  *
1255c6fd2807SJeff Garzik  *	LOCKING:
1256c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1257c6fd2807SJeff Garzik  *
1258c6fd2807SJeff Garzik  *	RETURNS:
1259c6fd2807SJeff Garzik  *	0 on success, -errno otherwise
1260c6fd2807SJeff Garzik  */
1261c6fd2807SJeff Garzik static int ata_eh_speed_down(struct ata_device *dev, int is_io,
1262c6fd2807SJeff Garzik 			     unsigned int err_mask)
1263c6fd2807SJeff Garzik {
1264c6fd2807SJeff Garzik 	if (!err_mask)
1265c6fd2807SJeff Garzik 		return 0;
1266c6fd2807SJeff Garzik 
1267c6fd2807SJeff Garzik 	/* record error and determine whether speed down is necessary */
1268c6fd2807SJeff Garzik 	ata_ering_record(&dev->ering, is_io, err_mask);
1269c6fd2807SJeff Garzik 
1270c6fd2807SJeff Garzik 	if (!ata_eh_speed_down_needed(dev))
1271c6fd2807SJeff Garzik 		return 0;
1272c6fd2807SJeff Garzik 
1273c6fd2807SJeff Garzik 	/* speed down SATA link speed if possible */
1274c6fd2807SJeff Garzik 	if (sata_down_spd_limit(dev->ap) == 0)
1275c6fd2807SJeff Garzik 		return ATA_EH_HARDRESET;
1276c6fd2807SJeff Garzik 
1277c6fd2807SJeff Garzik 	/* lower transfer mode */
1278c6fd2807SJeff Garzik 	if (ata_down_xfermask_limit(dev, 0) == 0)
1279c6fd2807SJeff Garzik 		return ATA_EH_SOFTRESET;
1280c6fd2807SJeff Garzik 
1281c6fd2807SJeff Garzik 	ata_dev_printk(dev, KERN_ERR,
1282c6fd2807SJeff Garzik 		       "speed down requested but no transfer mode left\n");
1283c6fd2807SJeff Garzik 	return 0;
1284c6fd2807SJeff Garzik }
1285c6fd2807SJeff Garzik 
1286c6fd2807SJeff Garzik /**
1287c6fd2807SJeff Garzik  *	ata_eh_autopsy - analyze error and determine recovery action
1288c6fd2807SJeff Garzik  *	@ap: ATA port to perform autopsy on
1289c6fd2807SJeff Garzik  *
1290c6fd2807SJeff Garzik  *	Analyze why @ap failed and determine which recovery action is
1291c6fd2807SJeff Garzik  *	needed.  This function also sets more detailed AC_ERR_* values
1292c6fd2807SJeff Garzik  *	and fills sense data for ATAPI CHECK SENSE.
1293c6fd2807SJeff Garzik  *
1294c6fd2807SJeff Garzik  *	LOCKING:
1295c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1296c6fd2807SJeff Garzik  */
1297c6fd2807SJeff Garzik static void ata_eh_autopsy(struct ata_port *ap)
1298c6fd2807SJeff Garzik {
1299c6fd2807SJeff Garzik 	struct ata_eh_context *ehc = &ap->eh_context;
1300c6fd2807SJeff Garzik 	unsigned int all_err_mask = 0;
1301c6fd2807SJeff Garzik 	int tag, is_io = 0;
1302c6fd2807SJeff Garzik 	u32 serror;
1303c6fd2807SJeff Garzik 	int rc;
1304c6fd2807SJeff Garzik 
1305c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
1306c6fd2807SJeff Garzik 
1307c6fd2807SJeff Garzik 	if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1308c6fd2807SJeff Garzik 		return;
1309c6fd2807SJeff Garzik 
1310c6fd2807SJeff Garzik 	/* obtain and analyze SError */
1311c6fd2807SJeff Garzik 	rc = sata_scr_read(ap, SCR_ERROR, &serror);
1312c6fd2807SJeff Garzik 	if (rc == 0) {
1313c6fd2807SJeff Garzik 		ehc->i.serror |= serror;
1314c6fd2807SJeff Garzik 		ata_eh_analyze_serror(ap);
1315c6fd2807SJeff Garzik 	} else if (rc != -EOPNOTSUPP)
1316c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_HARDRESET;
1317c6fd2807SJeff Garzik 
1318c6fd2807SJeff Garzik 	/* analyze NCQ failure */
1319c6fd2807SJeff Garzik 	ata_eh_analyze_ncq_error(ap);
1320c6fd2807SJeff Garzik 
1321c6fd2807SJeff Garzik 	/* any real error trumps AC_ERR_OTHER */
1322c6fd2807SJeff Garzik 	if (ehc->i.err_mask & ~AC_ERR_OTHER)
1323c6fd2807SJeff Garzik 		ehc->i.err_mask &= ~AC_ERR_OTHER;
1324c6fd2807SJeff Garzik 
1325c6fd2807SJeff Garzik 	all_err_mask |= ehc->i.err_mask;
1326c6fd2807SJeff Garzik 
1327c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1328c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1329c6fd2807SJeff Garzik 
1330c6fd2807SJeff Garzik 		if (!(qc->flags & ATA_QCFLAG_FAILED))
1331c6fd2807SJeff Garzik 			continue;
1332c6fd2807SJeff Garzik 
1333c6fd2807SJeff Garzik 		/* inherit upper level err_mask */
1334c6fd2807SJeff Garzik 		qc->err_mask |= ehc->i.err_mask;
1335c6fd2807SJeff Garzik 
1336c6fd2807SJeff Garzik 		/* analyze TF */
1337c6fd2807SJeff Garzik 		ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1338c6fd2807SJeff Garzik 
1339c6fd2807SJeff Garzik 		/* DEV errors are probably spurious in case of ATA_BUS error */
1340c6fd2807SJeff Garzik 		if (qc->err_mask & AC_ERR_ATA_BUS)
1341c6fd2807SJeff Garzik 			qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1342c6fd2807SJeff Garzik 					  AC_ERR_INVALID);
1343c6fd2807SJeff Garzik 
1344c6fd2807SJeff Garzik 		/* any real error trumps unknown error */
1345c6fd2807SJeff Garzik 		if (qc->err_mask & ~AC_ERR_OTHER)
1346c6fd2807SJeff Garzik 			qc->err_mask &= ~AC_ERR_OTHER;
1347c6fd2807SJeff Garzik 
1348c6fd2807SJeff Garzik 		/* SENSE_VALID trumps dev/unknown error and revalidation */
1349c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1350c6fd2807SJeff Garzik 			qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1351c6fd2807SJeff Garzik 			ehc->i.action &= ~ATA_EH_REVALIDATE;
1352c6fd2807SJeff Garzik 		}
1353c6fd2807SJeff Garzik 
1354c6fd2807SJeff Garzik 		/* accumulate error info */
1355c6fd2807SJeff Garzik 		ehc->i.dev = qc->dev;
1356c6fd2807SJeff Garzik 		all_err_mask |= qc->err_mask;
1357c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_IO)
1358c6fd2807SJeff Garzik 			is_io = 1;
1359c6fd2807SJeff Garzik 	}
1360c6fd2807SJeff Garzik 
1361c6fd2807SJeff Garzik 	/* enforce default EH actions */
1362c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN ||
1363c6fd2807SJeff Garzik 	    all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
1364c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_SOFTRESET;
1365c6fd2807SJeff Garzik 	else if (all_err_mask)
1366c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_REVALIDATE;
1367c6fd2807SJeff Garzik 
1368c6fd2807SJeff Garzik 	/* if we have offending qcs and the associated failed device */
1369c6fd2807SJeff Garzik 	if (ehc->i.dev) {
1370c6fd2807SJeff Garzik 		/* speed down */
1371c6fd2807SJeff Garzik 		ehc->i.action |= ata_eh_speed_down(ehc->i.dev, is_io,
1372c6fd2807SJeff Garzik 						   all_err_mask);
1373c6fd2807SJeff Garzik 
1374c6fd2807SJeff Garzik 		/* perform per-dev EH action only on the offending device */
1375c6fd2807SJeff Garzik 		ehc->i.dev_action[ehc->i.dev->devno] |=
1376c6fd2807SJeff Garzik 			ehc->i.action & ATA_EH_PERDEV_MASK;
1377c6fd2807SJeff Garzik 		ehc->i.action &= ~ATA_EH_PERDEV_MASK;
1378c6fd2807SJeff Garzik 	}
1379c6fd2807SJeff Garzik 
1380c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
1381c6fd2807SJeff Garzik }
1382c6fd2807SJeff Garzik 
1383c6fd2807SJeff Garzik /**
1384c6fd2807SJeff Garzik  *	ata_eh_report - report error handling to user
1385c6fd2807SJeff Garzik  *	@ap: ATA port EH is going on
1386c6fd2807SJeff Garzik  *
1387c6fd2807SJeff Garzik  *	Report EH to user.
1388c6fd2807SJeff Garzik  *
1389c6fd2807SJeff Garzik  *	LOCKING:
1390c6fd2807SJeff Garzik  *	None.
1391c6fd2807SJeff Garzik  */
1392c6fd2807SJeff Garzik static void ata_eh_report(struct ata_port *ap)
1393c6fd2807SJeff Garzik {
1394c6fd2807SJeff Garzik 	struct ata_eh_context *ehc = &ap->eh_context;
1395c6fd2807SJeff Garzik 	const char *frozen, *desc;
1396c6fd2807SJeff Garzik 	int tag, nr_failed = 0;
1397c6fd2807SJeff Garzik 
1398c6fd2807SJeff Garzik 	desc = NULL;
1399c6fd2807SJeff Garzik 	if (ehc->i.desc[0] != '\0')
1400c6fd2807SJeff Garzik 		desc = ehc->i.desc;
1401c6fd2807SJeff Garzik 
1402c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1403c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1404c6fd2807SJeff Garzik 
1405c6fd2807SJeff Garzik 		if (!(qc->flags & ATA_QCFLAG_FAILED))
1406c6fd2807SJeff Garzik 			continue;
1407c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1408c6fd2807SJeff Garzik 			continue;
1409c6fd2807SJeff Garzik 
1410c6fd2807SJeff Garzik 		nr_failed++;
1411c6fd2807SJeff Garzik 	}
1412c6fd2807SJeff Garzik 
1413c6fd2807SJeff Garzik 	if (!nr_failed && !ehc->i.err_mask)
1414c6fd2807SJeff Garzik 		return;
1415c6fd2807SJeff Garzik 
1416c6fd2807SJeff Garzik 	frozen = "";
1417c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN)
1418c6fd2807SJeff Garzik 		frozen = " frozen";
1419c6fd2807SJeff Garzik 
1420c6fd2807SJeff Garzik 	if (ehc->i.dev) {
1421c6fd2807SJeff Garzik 		ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1422c6fd2807SJeff Garzik 			       "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1423c6fd2807SJeff Garzik 			       ehc->i.err_mask, ap->sactive, ehc->i.serror,
1424c6fd2807SJeff Garzik 			       ehc->i.action, frozen);
1425c6fd2807SJeff Garzik 		if (desc)
1426c6fd2807SJeff Garzik 			ata_dev_printk(ehc->i.dev, KERN_ERR, "(%s)\n", desc);
1427c6fd2807SJeff Garzik 	} else {
1428c6fd2807SJeff Garzik 		ata_port_printk(ap, KERN_ERR, "exception Emask 0x%x "
1429c6fd2807SJeff Garzik 				"SAct 0x%x SErr 0x%x action 0x%x%s\n",
1430c6fd2807SJeff Garzik 				ehc->i.err_mask, ap->sactive, ehc->i.serror,
1431c6fd2807SJeff Garzik 				ehc->i.action, frozen);
1432c6fd2807SJeff Garzik 		if (desc)
1433c6fd2807SJeff Garzik 			ata_port_printk(ap, KERN_ERR, "(%s)\n", desc);
1434c6fd2807SJeff Garzik 	}
1435c6fd2807SJeff Garzik 
1436c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1437c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1438c6fd2807SJeff Garzik 
1439c6fd2807SJeff Garzik 		if (!(qc->flags & ATA_QCFLAG_FAILED) || !qc->err_mask)
1440c6fd2807SJeff Garzik 			continue;
1441c6fd2807SJeff Garzik 
1442c6fd2807SJeff Garzik 		ata_dev_printk(qc->dev, KERN_ERR, "tag %d cmd 0x%x "
1443c6fd2807SJeff Garzik 			       "Emask 0x%x stat 0x%x err 0x%x (%s)\n",
1444c6fd2807SJeff Garzik 			       qc->tag, qc->tf.command, qc->err_mask,
1445c6fd2807SJeff Garzik 			       qc->result_tf.command, qc->result_tf.feature,
1446c6fd2807SJeff Garzik 			       ata_err_string(qc->err_mask));
1447c6fd2807SJeff Garzik 	}
1448c6fd2807SJeff Garzik }
1449c6fd2807SJeff Garzik 
1450c6fd2807SJeff Garzik static int ata_do_reset(struct ata_port *ap, ata_reset_fn_t reset,
1451c6fd2807SJeff Garzik 			unsigned int *classes)
1452c6fd2807SJeff Garzik {
1453c6fd2807SJeff Garzik 	int i, rc;
1454c6fd2807SJeff Garzik 
1455c6fd2807SJeff Garzik 	for (i = 0; i < ATA_MAX_DEVICES; i++)
1456c6fd2807SJeff Garzik 		classes[i] = ATA_DEV_UNKNOWN;
1457c6fd2807SJeff Garzik 
1458c6fd2807SJeff Garzik 	rc = reset(ap, classes);
1459c6fd2807SJeff Garzik 	if (rc)
1460c6fd2807SJeff Garzik 		return rc;
1461c6fd2807SJeff Garzik 
1462c6fd2807SJeff Garzik 	/* If any class isn't ATA_DEV_UNKNOWN, consider classification
1463c6fd2807SJeff Garzik 	 * is complete and convert all ATA_DEV_UNKNOWN to
1464c6fd2807SJeff Garzik 	 * ATA_DEV_NONE.
1465c6fd2807SJeff Garzik 	 */
1466c6fd2807SJeff Garzik 	for (i = 0; i < ATA_MAX_DEVICES; i++)
1467c6fd2807SJeff Garzik 		if (classes[i] != ATA_DEV_UNKNOWN)
1468c6fd2807SJeff Garzik 			break;
1469c6fd2807SJeff Garzik 
1470c6fd2807SJeff Garzik 	if (i < ATA_MAX_DEVICES)
1471c6fd2807SJeff Garzik 		for (i = 0; i < ATA_MAX_DEVICES; i++)
1472c6fd2807SJeff Garzik 			if (classes[i] == ATA_DEV_UNKNOWN)
1473c6fd2807SJeff Garzik 				classes[i] = ATA_DEV_NONE;
1474c6fd2807SJeff Garzik 
1475c6fd2807SJeff Garzik 	return 0;
1476c6fd2807SJeff Garzik }
1477c6fd2807SJeff Garzik 
1478c6fd2807SJeff Garzik static int ata_eh_followup_srst_needed(int rc, int classify,
1479c6fd2807SJeff Garzik 				       const unsigned int *classes)
1480c6fd2807SJeff Garzik {
1481c6fd2807SJeff Garzik 	if (rc == -EAGAIN)
1482c6fd2807SJeff Garzik 		return 1;
1483c6fd2807SJeff Garzik 	if (rc != 0)
1484c6fd2807SJeff Garzik 		return 0;
1485c6fd2807SJeff Garzik 	if (classify && classes[0] == ATA_DEV_UNKNOWN)
1486c6fd2807SJeff Garzik 		return 1;
1487c6fd2807SJeff Garzik 	return 0;
1488c6fd2807SJeff Garzik }
1489c6fd2807SJeff Garzik 
1490c6fd2807SJeff Garzik static int ata_eh_reset(struct ata_port *ap, int classify,
1491c6fd2807SJeff Garzik 			ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
1492c6fd2807SJeff Garzik 			ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
1493c6fd2807SJeff Garzik {
1494c6fd2807SJeff Garzik 	struct ata_eh_context *ehc = &ap->eh_context;
1495c6fd2807SJeff Garzik 	unsigned int *classes = ehc->classes;
1496c6fd2807SJeff Garzik 	int tries = ATA_EH_RESET_TRIES;
1497c6fd2807SJeff Garzik 	int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
1498c6fd2807SJeff Garzik 	unsigned int action;
1499c6fd2807SJeff Garzik 	ata_reset_fn_t reset;
1500c6fd2807SJeff Garzik 	int i, did_followup_srst, rc;
1501c6fd2807SJeff Garzik 
1502c6fd2807SJeff Garzik 	/* about to reset */
1503c6fd2807SJeff Garzik 	ata_eh_about_to_do(ap, NULL, ehc->i.action & ATA_EH_RESET_MASK);
1504c6fd2807SJeff Garzik 
1505c6fd2807SJeff Garzik 	/* Determine which reset to use and record in ehc->i.action.
1506c6fd2807SJeff Garzik 	 * prereset() may examine and modify it.
1507c6fd2807SJeff Garzik 	 */
1508c6fd2807SJeff Garzik 	action = ehc->i.action;
1509c6fd2807SJeff Garzik 	ehc->i.action &= ~ATA_EH_RESET_MASK;
1510c6fd2807SJeff Garzik 	if (softreset && (!hardreset || (!sata_set_spd_needed(ap) &&
1511c6fd2807SJeff Garzik 					 !(action & ATA_EH_HARDRESET))))
1512c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_SOFTRESET;
1513c6fd2807SJeff Garzik 	else
1514c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_HARDRESET;
1515c6fd2807SJeff Garzik 
1516c6fd2807SJeff Garzik 	if (prereset) {
1517c6fd2807SJeff Garzik 		rc = prereset(ap);
1518c6fd2807SJeff Garzik 		if (rc) {
1519c6fd2807SJeff Garzik 			ata_port_printk(ap, KERN_ERR,
1520c6fd2807SJeff Garzik 					"prereset failed (errno=%d)\n", rc);
1521c6fd2807SJeff Garzik 			return rc;
1522c6fd2807SJeff Garzik 		}
1523c6fd2807SJeff Garzik 	}
1524c6fd2807SJeff Garzik 
1525c6fd2807SJeff Garzik 	/* prereset() might have modified ehc->i.action */
1526c6fd2807SJeff Garzik 	if (ehc->i.action & ATA_EH_HARDRESET)
1527c6fd2807SJeff Garzik 		reset = hardreset;
1528c6fd2807SJeff Garzik 	else if (ehc->i.action & ATA_EH_SOFTRESET)
1529c6fd2807SJeff Garzik 		reset = softreset;
1530c6fd2807SJeff Garzik 	else {
1531c6fd2807SJeff Garzik 		/* prereset told us not to reset, bang classes and return */
1532c6fd2807SJeff Garzik 		for (i = 0; i < ATA_MAX_DEVICES; i++)
1533c6fd2807SJeff Garzik 			classes[i] = ATA_DEV_NONE;
1534c6fd2807SJeff Garzik 		return 0;
1535c6fd2807SJeff Garzik 	}
1536c6fd2807SJeff Garzik 
1537c6fd2807SJeff Garzik 	/* did prereset() screw up?  if so, fix up to avoid oopsing */
1538c6fd2807SJeff Garzik 	if (!reset) {
1539c6fd2807SJeff Garzik 		ata_port_printk(ap, KERN_ERR, "BUG: prereset() requested "
1540c6fd2807SJeff Garzik 				"invalid reset type\n");
1541c6fd2807SJeff Garzik 		if (softreset)
1542c6fd2807SJeff Garzik 			reset = softreset;
1543c6fd2807SJeff Garzik 		else
1544c6fd2807SJeff Garzik 			reset = hardreset;
1545c6fd2807SJeff Garzik 	}
1546c6fd2807SJeff Garzik 
1547c6fd2807SJeff Garzik  retry:
1548c6fd2807SJeff Garzik 	/* shut up during boot probing */
1549c6fd2807SJeff Garzik 	if (verbose)
1550c6fd2807SJeff Garzik 		ata_port_printk(ap, KERN_INFO, "%s resetting port\n",
1551c6fd2807SJeff Garzik 				reset == softreset ? "soft" : "hard");
1552c6fd2807SJeff Garzik 
1553c6fd2807SJeff Garzik 	/* mark that this EH session started with reset */
1554c6fd2807SJeff Garzik 	ehc->i.flags |= ATA_EHI_DID_RESET;
1555c6fd2807SJeff Garzik 
1556c6fd2807SJeff Garzik 	rc = ata_do_reset(ap, reset, classes);
1557c6fd2807SJeff Garzik 
1558c6fd2807SJeff Garzik 	did_followup_srst = 0;
1559c6fd2807SJeff Garzik 	if (reset == hardreset &&
1560c6fd2807SJeff Garzik 	    ata_eh_followup_srst_needed(rc, classify, classes)) {
1561c6fd2807SJeff Garzik 		/* okay, let's do follow-up softreset */
1562c6fd2807SJeff Garzik 		did_followup_srst = 1;
1563c6fd2807SJeff Garzik 		reset = softreset;
1564c6fd2807SJeff Garzik 
1565c6fd2807SJeff Garzik 		if (!reset) {
1566c6fd2807SJeff Garzik 			ata_port_printk(ap, KERN_ERR,
1567c6fd2807SJeff Garzik 					"follow-up softreset required "
1568c6fd2807SJeff Garzik 					"but no softreset avaliable\n");
1569c6fd2807SJeff Garzik 			return -EINVAL;
1570c6fd2807SJeff Garzik 		}
1571c6fd2807SJeff Garzik 
1572c6fd2807SJeff Garzik 		ata_eh_about_to_do(ap, NULL, ATA_EH_RESET_MASK);
1573c6fd2807SJeff Garzik 		rc = ata_do_reset(ap, reset, classes);
1574c6fd2807SJeff Garzik 
1575c6fd2807SJeff Garzik 		if (rc == 0 && classify &&
1576c6fd2807SJeff Garzik 		    classes[0] == ATA_DEV_UNKNOWN) {
1577c6fd2807SJeff Garzik 			ata_port_printk(ap, KERN_ERR,
1578c6fd2807SJeff Garzik 					"classification failed\n");
1579c6fd2807SJeff Garzik 			return -EINVAL;
1580c6fd2807SJeff Garzik 		}
1581c6fd2807SJeff Garzik 	}
1582c6fd2807SJeff Garzik 
1583c6fd2807SJeff Garzik 	if (rc && --tries) {
1584c6fd2807SJeff Garzik 		const char *type;
1585c6fd2807SJeff Garzik 
1586c6fd2807SJeff Garzik 		if (reset == softreset) {
1587c6fd2807SJeff Garzik 			if (did_followup_srst)
1588c6fd2807SJeff Garzik 				type = "follow-up soft";
1589c6fd2807SJeff Garzik 			else
1590c6fd2807SJeff Garzik 				type = "soft";
1591c6fd2807SJeff Garzik 		} else
1592c6fd2807SJeff Garzik 			type = "hard";
1593c6fd2807SJeff Garzik 
1594c6fd2807SJeff Garzik 		ata_port_printk(ap, KERN_WARNING,
1595c6fd2807SJeff Garzik 				"%sreset failed, retrying in 5 secs\n", type);
1596c6fd2807SJeff Garzik 		ssleep(5);
1597c6fd2807SJeff Garzik 
1598c6fd2807SJeff Garzik 		if (reset == hardreset)
1599c6fd2807SJeff Garzik 			sata_down_spd_limit(ap);
1600c6fd2807SJeff Garzik 		if (hardreset)
1601c6fd2807SJeff Garzik 			reset = hardreset;
1602c6fd2807SJeff Garzik 		goto retry;
1603c6fd2807SJeff Garzik 	}
1604c6fd2807SJeff Garzik 
1605c6fd2807SJeff Garzik 	if (rc == 0) {
1606c6fd2807SJeff Garzik 		/* After the reset, the device state is PIO 0 and the
1607c6fd2807SJeff Garzik 		 * controller state is undefined.  Record the mode.
1608c6fd2807SJeff Garzik 		 */
1609c6fd2807SJeff Garzik 		for (i = 0; i < ATA_MAX_DEVICES; i++)
1610c6fd2807SJeff Garzik 			ap->device[i].pio_mode = XFER_PIO_0;
1611c6fd2807SJeff Garzik 
1612c6fd2807SJeff Garzik 		if (postreset)
1613c6fd2807SJeff Garzik 			postreset(ap, classes);
1614c6fd2807SJeff Garzik 
1615c6fd2807SJeff Garzik 		/* reset successful, schedule revalidation */
1616c6fd2807SJeff Garzik 		ata_eh_done(ap, NULL, ehc->i.action & ATA_EH_RESET_MASK);
1617c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_REVALIDATE;
1618c6fd2807SJeff Garzik 	}
1619c6fd2807SJeff Garzik 
1620c6fd2807SJeff Garzik 	return rc;
1621c6fd2807SJeff Garzik }
1622c6fd2807SJeff Garzik 
1623c6fd2807SJeff Garzik static int ata_eh_revalidate_and_attach(struct ata_port *ap,
1624c6fd2807SJeff Garzik 					struct ata_device **r_failed_dev)
1625c6fd2807SJeff Garzik {
1626c6fd2807SJeff Garzik 	struct ata_eh_context *ehc = &ap->eh_context;
1627c6fd2807SJeff Garzik 	struct ata_device *dev;
1628c6fd2807SJeff Garzik 	unsigned long flags;
1629c6fd2807SJeff Garzik 	int i, rc = 0;
1630c6fd2807SJeff Garzik 
1631c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
1632c6fd2807SJeff Garzik 
1633c6fd2807SJeff Garzik 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
1634c6fd2807SJeff Garzik 		unsigned int action;
1635c6fd2807SJeff Garzik 
1636c6fd2807SJeff Garzik 		dev = &ap->device[i];
1637c6fd2807SJeff Garzik 		action = ata_eh_dev_action(dev);
1638c6fd2807SJeff Garzik 
1639c6fd2807SJeff Garzik 		if (action & ATA_EH_REVALIDATE && ata_dev_ready(dev)) {
1640c6fd2807SJeff Garzik 			if (ata_port_offline(ap)) {
1641c6fd2807SJeff Garzik 				rc = -EIO;
1642c6fd2807SJeff Garzik 				break;
1643c6fd2807SJeff Garzik 			}
1644c6fd2807SJeff Garzik 
1645c6fd2807SJeff Garzik 			ata_eh_about_to_do(ap, dev, ATA_EH_REVALIDATE);
1646c6fd2807SJeff Garzik 			rc = ata_dev_revalidate(dev,
1647c6fd2807SJeff Garzik 					ehc->i.flags & ATA_EHI_DID_RESET);
1648c6fd2807SJeff Garzik 			if (rc)
1649c6fd2807SJeff Garzik 				break;
1650c6fd2807SJeff Garzik 
1651c6fd2807SJeff Garzik 			ata_eh_done(ap, dev, ATA_EH_REVALIDATE);
1652c6fd2807SJeff Garzik 
1653c6fd2807SJeff Garzik 			/* schedule the scsi_rescan_device() here */
1654c6fd2807SJeff Garzik 			queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
1655c6fd2807SJeff Garzik 		} else if (dev->class == ATA_DEV_UNKNOWN &&
1656c6fd2807SJeff Garzik 			   ehc->tries[dev->devno] &&
1657c6fd2807SJeff Garzik 			   ata_class_enabled(ehc->classes[dev->devno])) {
1658c6fd2807SJeff Garzik 			dev->class = ehc->classes[dev->devno];
1659c6fd2807SJeff Garzik 
1660c6fd2807SJeff Garzik 			rc = ata_dev_read_id(dev, &dev->class, 1, dev->id);
1661c6fd2807SJeff Garzik 			if (rc == 0)
1662c6fd2807SJeff Garzik 				rc = ata_dev_configure(dev, 1);
1663c6fd2807SJeff Garzik 
1664c6fd2807SJeff Garzik 			if (rc) {
1665c6fd2807SJeff Garzik 				dev->class = ATA_DEV_UNKNOWN;
1666c6fd2807SJeff Garzik 				break;
1667c6fd2807SJeff Garzik 			}
1668c6fd2807SJeff Garzik 
1669c6fd2807SJeff Garzik 			spin_lock_irqsave(ap->lock, flags);
1670c6fd2807SJeff Garzik 			ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
1671c6fd2807SJeff Garzik 			spin_unlock_irqrestore(ap->lock, flags);
1672c6fd2807SJeff Garzik 		}
1673c6fd2807SJeff Garzik 	}
1674c6fd2807SJeff Garzik 
1675c6fd2807SJeff Garzik 	if (rc)
1676c6fd2807SJeff Garzik 		*r_failed_dev = dev;
1677c6fd2807SJeff Garzik 
1678c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
1679c6fd2807SJeff Garzik 	return rc;
1680c6fd2807SJeff Garzik }
1681c6fd2807SJeff Garzik 
1682c6fd2807SJeff Garzik /**
1683c6fd2807SJeff Garzik  *	ata_eh_suspend - handle suspend EH action
1684c6fd2807SJeff Garzik  *	@ap: target host port
1685c6fd2807SJeff Garzik  *	@r_failed_dev: result parameter to indicate failing device
1686c6fd2807SJeff Garzik  *
1687c6fd2807SJeff Garzik  *	Handle suspend EH action.  Disk devices are spinned down and
1688c6fd2807SJeff Garzik  *	other types of devices are just marked suspended.  Once
1689c6fd2807SJeff Garzik  *	suspended, no EH action to the device is allowed until it is
1690c6fd2807SJeff Garzik  *	resumed.
1691c6fd2807SJeff Garzik  *
1692c6fd2807SJeff Garzik  *	LOCKING:
1693c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1694c6fd2807SJeff Garzik  *
1695c6fd2807SJeff Garzik  *	RETURNS:
1696c6fd2807SJeff Garzik  *	0 on success, -errno otherwise
1697c6fd2807SJeff Garzik  */
1698c6fd2807SJeff Garzik static int ata_eh_suspend(struct ata_port *ap, struct ata_device **r_failed_dev)
1699c6fd2807SJeff Garzik {
1700c6fd2807SJeff Garzik 	struct ata_device *dev;
1701c6fd2807SJeff Garzik 	int i, rc = 0;
1702c6fd2807SJeff Garzik 
1703c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
1704c6fd2807SJeff Garzik 
1705c6fd2807SJeff Garzik 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
1706c6fd2807SJeff Garzik 		unsigned long flags;
1707c6fd2807SJeff Garzik 		unsigned int action, err_mask;
1708c6fd2807SJeff Garzik 
1709c6fd2807SJeff Garzik 		dev = &ap->device[i];
1710c6fd2807SJeff Garzik 		action = ata_eh_dev_action(dev);
1711c6fd2807SJeff Garzik 
1712c6fd2807SJeff Garzik 		if (!ata_dev_enabled(dev) || !(action & ATA_EH_SUSPEND))
1713c6fd2807SJeff Garzik 			continue;
1714c6fd2807SJeff Garzik 
1715c6fd2807SJeff Garzik 		WARN_ON(dev->flags & ATA_DFLAG_SUSPENDED);
1716c6fd2807SJeff Garzik 
1717c6fd2807SJeff Garzik 		ata_eh_about_to_do(ap, dev, ATA_EH_SUSPEND);
1718c6fd2807SJeff Garzik 
1719c6fd2807SJeff Garzik 		if (dev->class == ATA_DEV_ATA && !(action & ATA_EH_PM_FREEZE)) {
1720c6fd2807SJeff Garzik 			/* flush cache */
1721c6fd2807SJeff Garzik 			rc = ata_flush_cache(dev);
1722c6fd2807SJeff Garzik 			if (rc)
1723c6fd2807SJeff Garzik 				break;
1724c6fd2807SJeff Garzik 
1725c6fd2807SJeff Garzik 			/* spin down */
1726c6fd2807SJeff Garzik 			err_mask = ata_do_simple_cmd(dev, ATA_CMD_STANDBYNOW1);
1727c6fd2807SJeff Garzik 			if (err_mask) {
1728c6fd2807SJeff Garzik 				ata_dev_printk(dev, KERN_ERR, "failed to "
1729c6fd2807SJeff Garzik 					       "spin down (err_mask=0x%x)\n",
1730c6fd2807SJeff Garzik 					       err_mask);
1731c6fd2807SJeff Garzik 				rc = -EIO;
1732c6fd2807SJeff Garzik 				break;
1733c6fd2807SJeff Garzik 			}
1734c6fd2807SJeff Garzik 		}
1735c6fd2807SJeff Garzik 
1736c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
1737c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_SUSPENDED;
1738c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1739c6fd2807SJeff Garzik 
1740c6fd2807SJeff Garzik 		ata_eh_done(ap, dev, ATA_EH_SUSPEND);
1741c6fd2807SJeff Garzik 	}
1742c6fd2807SJeff Garzik 
1743c6fd2807SJeff Garzik 	if (rc)
1744c6fd2807SJeff Garzik 		*r_failed_dev = dev;
1745c6fd2807SJeff Garzik 
1746c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
1747c6fd2807SJeff Garzik 	return 0;
1748c6fd2807SJeff Garzik }
1749c6fd2807SJeff Garzik 
1750c6fd2807SJeff Garzik /**
1751c6fd2807SJeff Garzik  *	ata_eh_prep_resume - prep for resume EH action
1752c6fd2807SJeff Garzik  *	@ap: target host port
1753c6fd2807SJeff Garzik  *
1754c6fd2807SJeff Garzik  *	Clear SUSPENDED in preparation for scheduled resume actions.
1755c6fd2807SJeff Garzik  *	This allows other parts of EH to access the devices being
1756c6fd2807SJeff Garzik  *	resumed.
1757c6fd2807SJeff Garzik  *
1758c6fd2807SJeff Garzik  *	LOCKING:
1759c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1760c6fd2807SJeff Garzik  */
1761c6fd2807SJeff Garzik static void ata_eh_prep_resume(struct ata_port *ap)
1762c6fd2807SJeff Garzik {
1763c6fd2807SJeff Garzik 	struct ata_device *dev;
1764c6fd2807SJeff Garzik 	unsigned long flags;
1765c6fd2807SJeff Garzik 	int i;
1766c6fd2807SJeff Garzik 
1767c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
1768c6fd2807SJeff Garzik 
1769c6fd2807SJeff Garzik 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
1770c6fd2807SJeff Garzik 		unsigned int action;
1771c6fd2807SJeff Garzik 
1772c6fd2807SJeff Garzik 		dev = &ap->device[i];
1773c6fd2807SJeff Garzik 		action = ata_eh_dev_action(dev);
1774c6fd2807SJeff Garzik 
1775c6fd2807SJeff Garzik 		if (!ata_dev_enabled(dev) || !(action & ATA_EH_RESUME))
1776c6fd2807SJeff Garzik 			continue;
1777c6fd2807SJeff Garzik 
1778c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
1779c6fd2807SJeff Garzik 		dev->flags &= ~ATA_DFLAG_SUSPENDED;
1780c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1781c6fd2807SJeff Garzik 	}
1782c6fd2807SJeff Garzik 
1783c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
1784c6fd2807SJeff Garzik }
1785c6fd2807SJeff Garzik 
1786c6fd2807SJeff Garzik /**
1787c6fd2807SJeff Garzik  *	ata_eh_resume - handle resume EH action
1788c6fd2807SJeff Garzik  *	@ap: target host port
1789c6fd2807SJeff Garzik  *	@r_failed_dev: result parameter to indicate failing device
1790c6fd2807SJeff Garzik  *
1791c6fd2807SJeff Garzik  *	Handle resume EH action.  Target devices are already reset and
1792c6fd2807SJeff Garzik  *	revalidated.  Spinning up is the only operation left.
1793c6fd2807SJeff Garzik  *
1794c6fd2807SJeff Garzik  *	LOCKING:
1795c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1796c6fd2807SJeff Garzik  *
1797c6fd2807SJeff Garzik  *	RETURNS:
1798c6fd2807SJeff Garzik  *	0 on success, -errno otherwise
1799c6fd2807SJeff Garzik  */
1800c6fd2807SJeff Garzik static int ata_eh_resume(struct ata_port *ap, struct ata_device **r_failed_dev)
1801c6fd2807SJeff Garzik {
1802c6fd2807SJeff Garzik 	struct ata_device *dev;
1803c6fd2807SJeff Garzik 	int i, rc = 0;
1804c6fd2807SJeff Garzik 
1805c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
1806c6fd2807SJeff Garzik 
1807c6fd2807SJeff Garzik 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
1808c6fd2807SJeff Garzik 		unsigned int action, err_mask;
1809c6fd2807SJeff Garzik 
1810c6fd2807SJeff Garzik 		dev = &ap->device[i];
1811c6fd2807SJeff Garzik 		action = ata_eh_dev_action(dev);
1812c6fd2807SJeff Garzik 
1813c6fd2807SJeff Garzik 		if (!ata_dev_enabled(dev) || !(action & ATA_EH_RESUME))
1814c6fd2807SJeff Garzik 			continue;
1815c6fd2807SJeff Garzik 
1816c6fd2807SJeff Garzik 		ata_eh_about_to_do(ap, dev, ATA_EH_RESUME);
1817c6fd2807SJeff Garzik 
1818c6fd2807SJeff Garzik 		if (dev->class == ATA_DEV_ATA && !(action & ATA_EH_PM_FREEZE)) {
1819c6fd2807SJeff Garzik 			err_mask = ata_do_simple_cmd(dev,
1820c6fd2807SJeff Garzik 						     ATA_CMD_IDLEIMMEDIATE);
1821c6fd2807SJeff Garzik 			if (err_mask) {
1822c6fd2807SJeff Garzik 				ata_dev_printk(dev, KERN_ERR, "failed to "
1823c6fd2807SJeff Garzik 					       "spin up (err_mask=0x%x)\n",
1824c6fd2807SJeff Garzik 					       err_mask);
1825c6fd2807SJeff Garzik 				rc = -EIO;
1826c6fd2807SJeff Garzik 				break;
1827c6fd2807SJeff Garzik 			}
1828c6fd2807SJeff Garzik 		}
1829c6fd2807SJeff Garzik 
1830c6fd2807SJeff Garzik 		ata_eh_done(ap, dev, ATA_EH_RESUME);
1831c6fd2807SJeff Garzik 	}
1832c6fd2807SJeff Garzik 
1833c6fd2807SJeff Garzik 	if (rc)
1834c6fd2807SJeff Garzik 		*r_failed_dev = dev;
1835c6fd2807SJeff Garzik 
1836c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
1837c6fd2807SJeff Garzik 	return 0;
1838c6fd2807SJeff Garzik }
1839c6fd2807SJeff Garzik 
1840c6fd2807SJeff Garzik static int ata_port_nr_enabled(struct ata_port *ap)
1841c6fd2807SJeff Garzik {
1842c6fd2807SJeff Garzik 	int i, cnt = 0;
1843c6fd2807SJeff Garzik 
1844c6fd2807SJeff Garzik 	for (i = 0; i < ATA_MAX_DEVICES; i++)
1845c6fd2807SJeff Garzik 		if (ata_dev_enabled(&ap->device[i]))
1846c6fd2807SJeff Garzik 			cnt++;
1847c6fd2807SJeff Garzik 	return cnt;
1848c6fd2807SJeff Garzik }
1849c6fd2807SJeff Garzik 
1850c6fd2807SJeff Garzik static int ata_port_nr_vacant(struct ata_port *ap)
1851c6fd2807SJeff Garzik {
1852c6fd2807SJeff Garzik 	int i, cnt = 0;
1853c6fd2807SJeff Garzik 
1854c6fd2807SJeff Garzik 	for (i = 0; i < ATA_MAX_DEVICES; i++)
1855c6fd2807SJeff Garzik 		if (ap->device[i].class == ATA_DEV_UNKNOWN)
1856c6fd2807SJeff Garzik 			cnt++;
1857c6fd2807SJeff Garzik 	return cnt;
1858c6fd2807SJeff Garzik }
1859c6fd2807SJeff Garzik 
1860c6fd2807SJeff Garzik static int ata_eh_skip_recovery(struct ata_port *ap)
1861c6fd2807SJeff Garzik {
1862c6fd2807SJeff Garzik 	struct ata_eh_context *ehc = &ap->eh_context;
1863c6fd2807SJeff Garzik 	int i;
1864c6fd2807SJeff Garzik 
1865c6fd2807SJeff Garzik 	/* skip if all possible devices are suspended */
1866c6fd2807SJeff Garzik 	for (i = 0; i < ata_port_max_devices(ap); i++) {
1867c6fd2807SJeff Garzik 		struct ata_device *dev = &ap->device[i];
1868c6fd2807SJeff Garzik 
1869c6fd2807SJeff Garzik 		if (!(dev->flags & ATA_DFLAG_SUSPENDED))
1870c6fd2807SJeff Garzik 			break;
1871c6fd2807SJeff Garzik 	}
1872c6fd2807SJeff Garzik 
1873c6fd2807SJeff Garzik 	if (i == ata_port_max_devices(ap))
1874c6fd2807SJeff Garzik 		return 1;
1875c6fd2807SJeff Garzik 
1876c6fd2807SJeff Garzik 	/* thaw frozen port, resume link and recover failed devices */
1877c6fd2807SJeff Garzik 	if ((ap->pflags & ATA_PFLAG_FROZEN) ||
1878c6fd2807SJeff Garzik 	    (ehc->i.flags & ATA_EHI_RESUME_LINK) || ata_port_nr_enabled(ap))
1879c6fd2807SJeff Garzik 		return 0;
1880c6fd2807SJeff Garzik 
1881c6fd2807SJeff Garzik 	/* skip if class codes for all vacant slots are ATA_DEV_NONE */
1882c6fd2807SJeff Garzik 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
1883c6fd2807SJeff Garzik 		struct ata_device *dev = &ap->device[i];
1884c6fd2807SJeff Garzik 
1885c6fd2807SJeff Garzik 		if (dev->class == ATA_DEV_UNKNOWN &&
1886c6fd2807SJeff Garzik 		    ehc->classes[dev->devno] != ATA_DEV_NONE)
1887c6fd2807SJeff Garzik 			return 0;
1888c6fd2807SJeff Garzik 	}
1889c6fd2807SJeff Garzik 
1890c6fd2807SJeff Garzik 	return 1;
1891c6fd2807SJeff Garzik }
1892c6fd2807SJeff Garzik 
1893c6fd2807SJeff Garzik /**
1894c6fd2807SJeff Garzik  *	ata_eh_recover - recover host port after error
1895c6fd2807SJeff Garzik  *	@ap: host port to recover
1896c6fd2807SJeff Garzik  *	@prereset: prereset method (can be NULL)
1897c6fd2807SJeff Garzik  *	@softreset: softreset method (can be NULL)
1898c6fd2807SJeff Garzik  *	@hardreset: hardreset method (can be NULL)
1899c6fd2807SJeff Garzik  *	@postreset: postreset method (can be NULL)
1900c6fd2807SJeff Garzik  *
1901c6fd2807SJeff Garzik  *	This is the alpha and omega, eum and yang, heart and soul of
1902c6fd2807SJeff Garzik  *	libata exception handling.  On entry, actions required to
1903c6fd2807SJeff Garzik  *	recover the port and hotplug requests are recorded in
1904c6fd2807SJeff Garzik  *	eh_context.  This function executes all the operations with
1905c6fd2807SJeff Garzik  *	appropriate retrials and fallbacks to resurrect failed
1906c6fd2807SJeff Garzik  *	devices, detach goners and greet newcomers.
1907c6fd2807SJeff Garzik  *
1908c6fd2807SJeff Garzik  *	LOCKING:
1909c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1910c6fd2807SJeff Garzik  *
1911c6fd2807SJeff Garzik  *	RETURNS:
1912c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
1913c6fd2807SJeff Garzik  */
1914c6fd2807SJeff Garzik static int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
1915c6fd2807SJeff Garzik 			  ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
1916c6fd2807SJeff Garzik 			  ata_postreset_fn_t postreset)
1917c6fd2807SJeff Garzik {
1918c6fd2807SJeff Garzik 	struct ata_eh_context *ehc = &ap->eh_context;
1919c6fd2807SJeff Garzik 	struct ata_device *dev;
1920c6fd2807SJeff Garzik 	int down_xfermask, i, rc;
1921c6fd2807SJeff Garzik 
1922c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
1923c6fd2807SJeff Garzik 
1924c6fd2807SJeff Garzik 	/* prep for recovery */
1925c6fd2807SJeff Garzik 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
1926c6fd2807SJeff Garzik 		dev = &ap->device[i];
1927c6fd2807SJeff Garzik 
1928c6fd2807SJeff Garzik 		ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
1929c6fd2807SJeff Garzik 
1930c6fd2807SJeff Garzik 		/* process hotplug request */
1931c6fd2807SJeff Garzik 		if (dev->flags & ATA_DFLAG_DETACH)
1932c6fd2807SJeff Garzik 			ata_eh_detach_dev(dev);
1933c6fd2807SJeff Garzik 
1934c6fd2807SJeff Garzik 		if (!ata_dev_enabled(dev) &&
1935c6fd2807SJeff Garzik 		    ((ehc->i.probe_mask & (1 << dev->devno)) &&
1936c6fd2807SJeff Garzik 		     !(ehc->did_probe_mask & (1 << dev->devno)))) {
1937c6fd2807SJeff Garzik 			ata_eh_detach_dev(dev);
1938c6fd2807SJeff Garzik 			ata_dev_init(dev);
1939c6fd2807SJeff Garzik 			ehc->did_probe_mask |= (1 << dev->devno);
1940c6fd2807SJeff Garzik 			ehc->i.action |= ATA_EH_SOFTRESET;
1941c6fd2807SJeff Garzik 		}
1942c6fd2807SJeff Garzik 	}
1943c6fd2807SJeff Garzik 
1944c6fd2807SJeff Garzik  retry:
1945c6fd2807SJeff Garzik 	down_xfermask = 0;
1946c6fd2807SJeff Garzik 	rc = 0;
1947c6fd2807SJeff Garzik 
1948c6fd2807SJeff Garzik 	/* if UNLOADING, finish immediately */
1949c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_UNLOADING)
1950c6fd2807SJeff Garzik 		goto out;
1951c6fd2807SJeff Garzik 
1952c6fd2807SJeff Garzik 	/* prep for resume */
1953c6fd2807SJeff Garzik 	ata_eh_prep_resume(ap);
1954c6fd2807SJeff Garzik 
1955c6fd2807SJeff Garzik 	/* skip EH if possible. */
1956c6fd2807SJeff Garzik 	if (ata_eh_skip_recovery(ap))
1957c6fd2807SJeff Garzik 		ehc->i.action = 0;
1958c6fd2807SJeff Garzik 
1959c6fd2807SJeff Garzik 	for (i = 0; i < ATA_MAX_DEVICES; i++)
1960c6fd2807SJeff Garzik 		ehc->classes[i] = ATA_DEV_UNKNOWN;
1961c6fd2807SJeff Garzik 
1962c6fd2807SJeff Garzik 	/* reset */
1963c6fd2807SJeff Garzik 	if (ehc->i.action & ATA_EH_RESET_MASK) {
1964c6fd2807SJeff Garzik 		ata_eh_freeze_port(ap);
1965c6fd2807SJeff Garzik 
1966c6fd2807SJeff Garzik 		rc = ata_eh_reset(ap, ata_port_nr_vacant(ap), prereset,
1967c6fd2807SJeff Garzik 				  softreset, hardreset, postreset);
1968c6fd2807SJeff Garzik 		if (rc) {
1969c6fd2807SJeff Garzik 			ata_port_printk(ap, KERN_ERR,
1970c6fd2807SJeff Garzik 					"reset failed, giving up\n");
1971c6fd2807SJeff Garzik 			goto out;
1972c6fd2807SJeff Garzik 		}
1973c6fd2807SJeff Garzik 
1974c6fd2807SJeff Garzik 		ata_eh_thaw_port(ap);
1975c6fd2807SJeff Garzik 	}
1976c6fd2807SJeff Garzik 
1977c6fd2807SJeff Garzik 	/* revalidate existing devices and attach new ones */
1978c6fd2807SJeff Garzik 	rc = ata_eh_revalidate_and_attach(ap, &dev);
1979c6fd2807SJeff Garzik 	if (rc)
1980c6fd2807SJeff Garzik 		goto dev_fail;
1981c6fd2807SJeff Garzik 
1982c6fd2807SJeff Garzik 	/* resume devices */
1983c6fd2807SJeff Garzik 	rc = ata_eh_resume(ap, &dev);
1984c6fd2807SJeff Garzik 	if (rc)
1985c6fd2807SJeff Garzik 		goto dev_fail;
1986c6fd2807SJeff Garzik 
1987c6fd2807SJeff Garzik 	/* configure transfer mode if the port has been reset */
1988c6fd2807SJeff Garzik 	if (ehc->i.flags & ATA_EHI_DID_RESET) {
1989c6fd2807SJeff Garzik 		rc = ata_set_mode(ap, &dev);
1990c6fd2807SJeff Garzik 		if (rc) {
1991c6fd2807SJeff Garzik 			down_xfermask = 1;
1992c6fd2807SJeff Garzik 			goto dev_fail;
1993c6fd2807SJeff Garzik 		}
1994c6fd2807SJeff Garzik 	}
1995c6fd2807SJeff Garzik 
1996c6fd2807SJeff Garzik 	/* suspend devices */
1997c6fd2807SJeff Garzik 	rc = ata_eh_suspend(ap, &dev);
1998c6fd2807SJeff Garzik 	if (rc)
1999c6fd2807SJeff Garzik 		goto dev_fail;
2000c6fd2807SJeff Garzik 
2001c6fd2807SJeff Garzik 	goto out;
2002c6fd2807SJeff Garzik 
2003c6fd2807SJeff Garzik  dev_fail:
2004c6fd2807SJeff Garzik 	switch (rc) {
2005c6fd2807SJeff Garzik 	case -ENODEV:
2006c6fd2807SJeff Garzik 		/* device missing, schedule probing */
2007c6fd2807SJeff Garzik 		ehc->i.probe_mask |= (1 << dev->devno);
2008c6fd2807SJeff Garzik 	case -EINVAL:
2009c6fd2807SJeff Garzik 		ehc->tries[dev->devno] = 0;
2010c6fd2807SJeff Garzik 		break;
2011c6fd2807SJeff Garzik 	case -EIO:
2012c6fd2807SJeff Garzik 		sata_down_spd_limit(ap);
2013c6fd2807SJeff Garzik 	default:
2014c6fd2807SJeff Garzik 		ehc->tries[dev->devno]--;
2015c6fd2807SJeff Garzik 		if (down_xfermask &&
2016c6fd2807SJeff Garzik 		    ata_down_xfermask_limit(dev, ehc->tries[dev->devno] == 1))
2017c6fd2807SJeff Garzik 			ehc->tries[dev->devno] = 0;
2018c6fd2807SJeff Garzik 	}
2019c6fd2807SJeff Garzik 
2020c6fd2807SJeff Garzik 	if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2021c6fd2807SJeff Garzik 		/* disable device if it has used up all its chances */
2022c6fd2807SJeff Garzik 		ata_dev_disable(dev);
2023c6fd2807SJeff Garzik 
2024c6fd2807SJeff Garzik 		/* detach if offline */
2025c6fd2807SJeff Garzik 		if (ata_port_offline(ap))
2026c6fd2807SJeff Garzik 			ata_eh_detach_dev(dev);
2027c6fd2807SJeff Garzik 
2028c6fd2807SJeff Garzik 		/* probe if requested */
2029c6fd2807SJeff Garzik 		if ((ehc->i.probe_mask & (1 << dev->devno)) &&
2030c6fd2807SJeff Garzik 		    !(ehc->did_probe_mask & (1 << dev->devno))) {
2031c6fd2807SJeff Garzik 			ata_eh_detach_dev(dev);
2032c6fd2807SJeff Garzik 			ata_dev_init(dev);
2033c6fd2807SJeff Garzik 
2034c6fd2807SJeff Garzik 			ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2035c6fd2807SJeff Garzik 			ehc->did_probe_mask |= (1 << dev->devno);
2036c6fd2807SJeff Garzik 			ehc->i.action |= ATA_EH_SOFTRESET;
2037c6fd2807SJeff Garzik 		}
2038c6fd2807SJeff Garzik 	} else {
2039c6fd2807SJeff Garzik 		/* soft didn't work?  be haaaaard */
2040c6fd2807SJeff Garzik 		if (ehc->i.flags & ATA_EHI_DID_RESET)
2041c6fd2807SJeff Garzik 			ehc->i.action |= ATA_EH_HARDRESET;
2042c6fd2807SJeff Garzik 		else
2043c6fd2807SJeff Garzik 			ehc->i.action |= ATA_EH_SOFTRESET;
2044c6fd2807SJeff Garzik 	}
2045c6fd2807SJeff Garzik 
2046c6fd2807SJeff Garzik 	if (ata_port_nr_enabled(ap)) {
2047c6fd2807SJeff Garzik 		ata_port_printk(ap, KERN_WARNING, "failed to recover some "
2048c6fd2807SJeff Garzik 				"devices, retrying in 5 secs\n");
2049c6fd2807SJeff Garzik 		ssleep(5);
2050c6fd2807SJeff Garzik 	} else {
2051c6fd2807SJeff Garzik 		/* no device left, repeat fast */
2052c6fd2807SJeff Garzik 		msleep(500);
2053c6fd2807SJeff Garzik 	}
2054c6fd2807SJeff Garzik 
2055c6fd2807SJeff Garzik 	goto retry;
2056c6fd2807SJeff Garzik 
2057c6fd2807SJeff Garzik  out:
2058c6fd2807SJeff Garzik 	if (rc) {
2059c6fd2807SJeff Garzik 		for (i = 0; i < ATA_MAX_DEVICES; i++)
2060c6fd2807SJeff Garzik 			ata_dev_disable(&ap->device[i]);
2061c6fd2807SJeff Garzik 	}
2062c6fd2807SJeff Garzik 
2063c6fd2807SJeff Garzik 	DPRINTK("EXIT, rc=%d\n", rc);
2064c6fd2807SJeff Garzik 	return rc;
2065c6fd2807SJeff Garzik }
2066c6fd2807SJeff Garzik 
2067c6fd2807SJeff Garzik /**
2068c6fd2807SJeff Garzik  *	ata_eh_finish - finish up EH
2069c6fd2807SJeff Garzik  *	@ap: host port to finish EH for
2070c6fd2807SJeff Garzik  *
2071c6fd2807SJeff Garzik  *	Recovery is complete.  Clean up EH states and retry or finish
2072c6fd2807SJeff Garzik  *	failed qcs.
2073c6fd2807SJeff Garzik  *
2074c6fd2807SJeff Garzik  *	LOCKING:
2075c6fd2807SJeff Garzik  *	None.
2076c6fd2807SJeff Garzik  */
2077c6fd2807SJeff Garzik static void ata_eh_finish(struct ata_port *ap)
2078c6fd2807SJeff Garzik {
2079c6fd2807SJeff Garzik 	int tag;
2080c6fd2807SJeff Garzik 
2081c6fd2807SJeff Garzik 	/* retry or finish qcs */
2082c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2083c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2084c6fd2807SJeff Garzik 
2085c6fd2807SJeff Garzik 		if (!(qc->flags & ATA_QCFLAG_FAILED))
2086c6fd2807SJeff Garzik 			continue;
2087c6fd2807SJeff Garzik 
2088c6fd2807SJeff Garzik 		if (qc->err_mask) {
2089c6fd2807SJeff Garzik 			/* FIXME: Once EH migration is complete,
2090c6fd2807SJeff Garzik 			 * generate sense data in this function,
2091c6fd2807SJeff Garzik 			 * considering both err_mask and tf.
2092c6fd2807SJeff Garzik 			 */
2093c6fd2807SJeff Garzik 			if (qc->err_mask & AC_ERR_INVALID)
2094c6fd2807SJeff Garzik 				ata_eh_qc_complete(qc);
2095c6fd2807SJeff Garzik 			else
2096c6fd2807SJeff Garzik 				ata_eh_qc_retry(qc);
2097c6fd2807SJeff Garzik 		} else {
2098c6fd2807SJeff Garzik 			if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2099c6fd2807SJeff Garzik 				ata_eh_qc_complete(qc);
2100c6fd2807SJeff Garzik 			} else {
2101c6fd2807SJeff Garzik 				/* feed zero TF to sense generation */
2102c6fd2807SJeff Garzik 				memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2103c6fd2807SJeff Garzik 				ata_eh_qc_retry(qc);
2104c6fd2807SJeff Garzik 			}
2105c6fd2807SJeff Garzik 		}
2106c6fd2807SJeff Garzik 	}
2107c6fd2807SJeff Garzik }
2108c6fd2807SJeff Garzik 
2109c6fd2807SJeff Garzik /**
2110c6fd2807SJeff Garzik  *	ata_do_eh - do standard error handling
2111c6fd2807SJeff Garzik  *	@ap: host port to handle error for
2112c6fd2807SJeff Garzik  *	@prereset: prereset method (can be NULL)
2113c6fd2807SJeff Garzik  *	@softreset: softreset method (can be NULL)
2114c6fd2807SJeff Garzik  *	@hardreset: hardreset method (can be NULL)
2115c6fd2807SJeff Garzik  *	@postreset: postreset method (can be NULL)
2116c6fd2807SJeff Garzik  *
2117c6fd2807SJeff Garzik  *	Perform standard error handling sequence.
2118c6fd2807SJeff Garzik  *
2119c6fd2807SJeff Garzik  *	LOCKING:
2120c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
2121c6fd2807SJeff Garzik  */
2122c6fd2807SJeff Garzik void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2123c6fd2807SJeff Garzik 	       ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2124c6fd2807SJeff Garzik 	       ata_postreset_fn_t postreset)
2125c6fd2807SJeff Garzik {
2126c6fd2807SJeff Garzik 	ata_eh_autopsy(ap);
2127c6fd2807SJeff Garzik 	ata_eh_report(ap);
2128c6fd2807SJeff Garzik 	ata_eh_recover(ap, prereset, softreset, hardreset, postreset);
2129c6fd2807SJeff Garzik 	ata_eh_finish(ap);
2130c6fd2807SJeff Garzik }
2131c6fd2807SJeff Garzik 
2132c6fd2807SJeff Garzik /**
2133c6fd2807SJeff Garzik  *	ata_eh_handle_port_suspend - perform port suspend operation
2134c6fd2807SJeff Garzik  *	@ap: port to suspend
2135c6fd2807SJeff Garzik  *
2136c6fd2807SJeff Garzik  *	Suspend @ap.
2137c6fd2807SJeff Garzik  *
2138c6fd2807SJeff Garzik  *	LOCKING:
2139c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
2140c6fd2807SJeff Garzik  */
2141c6fd2807SJeff Garzik static void ata_eh_handle_port_suspend(struct ata_port *ap)
2142c6fd2807SJeff Garzik {
2143c6fd2807SJeff Garzik 	unsigned long flags;
2144c6fd2807SJeff Garzik 	int rc = 0;
2145c6fd2807SJeff Garzik 
2146c6fd2807SJeff Garzik 	/* are we suspending? */
2147c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
2148c6fd2807SJeff Garzik 	if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2149c6fd2807SJeff Garzik 	    ap->pm_mesg.event == PM_EVENT_ON) {
2150c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
2151c6fd2807SJeff Garzik 		return;
2152c6fd2807SJeff Garzik 	}
2153c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
2154c6fd2807SJeff Garzik 
2155c6fd2807SJeff Garzik 	WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
2156c6fd2807SJeff Garzik 
2157c6fd2807SJeff Garzik 	/* suspend */
2158c6fd2807SJeff Garzik 	ata_eh_freeze_port(ap);
2159c6fd2807SJeff Garzik 
2160c6fd2807SJeff Garzik 	if (ap->ops->port_suspend)
2161c6fd2807SJeff Garzik 		rc = ap->ops->port_suspend(ap, ap->pm_mesg);
2162c6fd2807SJeff Garzik 
2163c6fd2807SJeff Garzik 	/* report result */
2164c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
2165c6fd2807SJeff Garzik 
2166c6fd2807SJeff Garzik 	ap->pflags &= ~ATA_PFLAG_PM_PENDING;
2167c6fd2807SJeff Garzik 	if (rc == 0)
2168c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_SUSPENDED;
2169c6fd2807SJeff Garzik 	else
2170c6fd2807SJeff Garzik 		ata_port_schedule_eh(ap);
2171c6fd2807SJeff Garzik 
2172c6fd2807SJeff Garzik 	if (ap->pm_result) {
2173c6fd2807SJeff Garzik 		*ap->pm_result = rc;
2174c6fd2807SJeff Garzik 		ap->pm_result = NULL;
2175c6fd2807SJeff Garzik 	}
2176c6fd2807SJeff Garzik 
2177c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
2178c6fd2807SJeff Garzik 
2179c6fd2807SJeff Garzik 	return;
2180c6fd2807SJeff Garzik }
2181c6fd2807SJeff Garzik 
2182c6fd2807SJeff Garzik /**
2183c6fd2807SJeff Garzik  *	ata_eh_handle_port_resume - perform port resume operation
2184c6fd2807SJeff Garzik  *	@ap: port to resume
2185c6fd2807SJeff Garzik  *
2186c6fd2807SJeff Garzik  *	Resume @ap.
2187c6fd2807SJeff Garzik  *
2188c6fd2807SJeff Garzik  *	This function also waits upto one second until all devices
2189c6fd2807SJeff Garzik  *	hanging off this port requests resume EH action.  This is to
2190c6fd2807SJeff Garzik  *	prevent invoking EH and thus reset multiple times on resume.
2191c6fd2807SJeff Garzik  *
2192c6fd2807SJeff Garzik  *	On DPM resume, where some of devices might not be resumed
2193c6fd2807SJeff Garzik  *	together, this may delay port resume upto one second, but such
2194c6fd2807SJeff Garzik  *	DPM resumes are rare and 1 sec delay isn't too bad.
2195c6fd2807SJeff Garzik  *
2196c6fd2807SJeff Garzik  *	LOCKING:
2197c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
2198c6fd2807SJeff Garzik  */
2199c6fd2807SJeff Garzik static void ata_eh_handle_port_resume(struct ata_port *ap)
2200c6fd2807SJeff Garzik {
2201c6fd2807SJeff Garzik 	unsigned long timeout;
2202c6fd2807SJeff Garzik 	unsigned long flags;
2203c6fd2807SJeff Garzik 	int i, rc = 0;
2204c6fd2807SJeff Garzik 
2205c6fd2807SJeff Garzik 	/* are we resuming? */
2206c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
2207c6fd2807SJeff Garzik 	if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2208c6fd2807SJeff Garzik 	    ap->pm_mesg.event != PM_EVENT_ON) {
2209c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
2210c6fd2807SJeff Garzik 		return;
2211c6fd2807SJeff Garzik 	}
2212c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
2213c6fd2807SJeff Garzik 
2214c6fd2807SJeff Garzik 	/* spurious? */
2215c6fd2807SJeff Garzik 	if (!(ap->pflags & ATA_PFLAG_SUSPENDED))
2216c6fd2807SJeff Garzik 		goto done;
2217c6fd2807SJeff Garzik 
2218c6fd2807SJeff Garzik 	if (ap->ops->port_resume)
2219c6fd2807SJeff Garzik 		rc = ap->ops->port_resume(ap);
2220c6fd2807SJeff Garzik 
2221c6fd2807SJeff Garzik 	/* give devices time to request EH */
2222c6fd2807SJeff Garzik 	timeout = jiffies + HZ; /* 1s max */
2223c6fd2807SJeff Garzik 	while (1) {
2224c6fd2807SJeff Garzik 		for (i = 0; i < ATA_MAX_DEVICES; i++) {
2225c6fd2807SJeff Garzik 			struct ata_device *dev = &ap->device[i];
2226c6fd2807SJeff Garzik 			unsigned int action = ata_eh_dev_action(dev);
2227c6fd2807SJeff Garzik 
2228c6fd2807SJeff Garzik 			if ((dev->flags & ATA_DFLAG_SUSPENDED) &&
2229c6fd2807SJeff Garzik 			    !(action & ATA_EH_RESUME))
2230c6fd2807SJeff Garzik 				break;
2231c6fd2807SJeff Garzik 		}
2232c6fd2807SJeff Garzik 
2233c6fd2807SJeff Garzik 		if (i == ATA_MAX_DEVICES || time_after(jiffies, timeout))
2234c6fd2807SJeff Garzik 			break;
2235c6fd2807SJeff Garzik 		msleep(10);
2236c6fd2807SJeff Garzik 	}
2237c6fd2807SJeff Garzik 
2238c6fd2807SJeff Garzik  done:
2239c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
2240c6fd2807SJeff Garzik 	ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
2241c6fd2807SJeff Garzik 	if (ap->pm_result) {
2242c6fd2807SJeff Garzik 		*ap->pm_result = rc;
2243c6fd2807SJeff Garzik 		ap->pm_result = NULL;
2244c6fd2807SJeff Garzik 	}
2245c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
2246c6fd2807SJeff Garzik }
2247