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