xref: /openbmc/linux/drivers/ata/libata-eh.c (revision 8b5bb2fa3d1c2a90ca921b6bfbb7e2de1e6dd273)
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 
477d47e8d4STejun Heo enum {
487d47e8d4STejun Heo 	ATA_EH_SPDN_NCQ_OFF		= (1 << 0),
497d47e8d4STejun Heo 	ATA_EH_SPDN_SPEED_DOWN		= (1 << 1),
507d47e8d4STejun Heo 	ATA_EH_SPDN_FALLBACK_TO_PIO	= (1 << 2),
517d47e8d4STejun Heo };
527d47e8d4STejun Heo 
5331daabdaSTejun Heo /* Waiting in ->prereset can never be reliable.  It's sometimes nice
5431daabdaSTejun Heo  * to wait there but it can't be depended upon; otherwise, we wouldn't
5531daabdaSTejun Heo  * be resetting.  Just give it enough time for most drives to spin up.
5631daabdaSTejun Heo  */
5731daabdaSTejun Heo enum {
5831daabdaSTejun Heo 	ATA_EH_PRERESET_TIMEOUT		= 10 * HZ,
5931daabdaSTejun Heo };
6031daabdaSTejun Heo 
6131daabdaSTejun Heo /* The following table determines how we sequence resets.  Each entry
6231daabdaSTejun Heo  * represents timeout for that try.  The first try can be soft or
6331daabdaSTejun Heo  * hardreset.  All others are hardreset if available.  In most cases
6431daabdaSTejun Heo  * the first reset w/ 10sec timeout should succeed.  Following entries
6531daabdaSTejun Heo  * are mostly for error handling, hotplug and retarded devices.
6631daabdaSTejun Heo  */
6731daabdaSTejun Heo static const unsigned long ata_eh_reset_timeouts[] = {
6831daabdaSTejun Heo 	10 * HZ,	/* most drives spin up by 10sec */
6931daabdaSTejun Heo 	10 * HZ,	/* > 99% working drives spin up before 20sec */
7031daabdaSTejun Heo 	35 * HZ,	/* give > 30 secs of idleness for retarded devices */
7131daabdaSTejun Heo 	5 * HZ,		/* and sweet one last chance */
7231daabdaSTejun Heo 	/* > 1 min has elapsed, give up */
7331daabdaSTejun Heo };
7431daabdaSTejun Heo 
75c6fd2807SJeff Garzik static void __ata_port_freeze(struct ata_port *ap);
76c6fd2807SJeff Garzik static void ata_eh_finish(struct ata_port *ap);
776ffa01d8STejun Heo #ifdef CONFIG_PM
78c6fd2807SJeff Garzik static void ata_eh_handle_port_suspend(struct ata_port *ap);
79c6fd2807SJeff Garzik static void ata_eh_handle_port_resume(struct ata_port *ap);
806ffa01d8STejun Heo #else /* CONFIG_PM */
816ffa01d8STejun Heo static void ata_eh_handle_port_suspend(struct ata_port *ap)
826ffa01d8STejun Heo { }
836ffa01d8STejun Heo 
846ffa01d8STejun Heo static void ata_eh_handle_port_resume(struct ata_port *ap)
856ffa01d8STejun Heo { }
866ffa01d8STejun Heo #endif /* CONFIG_PM */
87c6fd2807SJeff Garzik 
88c6fd2807SJeff Garzik static void ata_ering_record(struct ata_ering *ering, int is_io,
89c6fd2807SJeff Garzik 			     unsigned int err_mask)
90c6fd2807SJeff Garzik {
91c6fd2807SJeff Garzik 	struct ata_ering_entry *ent;
92c6fd2807SJeff Garzik 
93c6fd2807SJeff Garzik 	WARN_ON(!err_mask);
94c6fd2807SJeff Garzik 
95c6fd2807SJeff Garzik 	ering->cursor++;
96c6fd2807SJeff Garzik 	ering->cursor %= ATA_ERING_SIZE;
97c6fd2807SJeff Garzik 
98c6fd2807SJeff Garzik 	ent = &ering->ring[ering->cursor];
99c6fd2807SJeff Garzik 	ent->is_io = is_io;
100c6fd2807SJeff Garzik 	ent->err_mask = err_mask;
101c6fd2807SJeff Garzik 	ent->timestamp = get_jiffies_64();
102c6fd2807SJeff Garzik }
103c6fd2807SJeff Garzik 
1047d47e8d4STejun Heo static void ata_ering_clear(struct ata_ering *ering)
105c6fd2807SJeff Garzik {
1067d47e8d4STejun Heo 	memset(ering, 0, sizeof(*ering));
107c6fd2807SJeff Garzik }
108c6fd2807SJeff Garzik 
109c6fd2807SJeff Garzik static int ata_ering_map(struct ata_ering *ering,
110c6fd2807SJeff Garzik 			 int (*map_fn)(struct ata_ering_entry *, void *),
111c6fd2807SJeff Garzik 			 void *arg)
112c6fd2807SJeff Garzik {
113c6fd2807SJeff Garzik 	int idx, rc = 0;
114c6fd2807SJeff Garzik 	struct ata_ering_entry *ent;
115c6fd2807SJeff Garzik 
116c6fd2807SJeff Garzik 	idx = ering->cursor;
117c6fd2807SJeff Garzik 	do {
118c6fd2807SJeff Garzik 		ent = &ering->ring[idx];
119c6fd2807SJeff Garzik 		if (!ent->err_mask)
120c6fd2807SJeff Garzik 			break;
121c6fd2807SJeff Garzik 		rc = map_fn(ent, arg);
122c6fd2807SJeff Garzik 		if (rc)
123c6fd2807SJeff Garzik 			break;
124c6fd2807SJeff Garzik 		idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
125c6fd2807SJeff Garzik 	} while (idx != ering->cursor);
126c6fd2807SJeff Garzik 
127c6fd2807SJeff Garzik 	return rc;
128c6fd2807SJeff Garzik }
129c6fd2807SJeff Garzik 
130c6fd2807SJeff Garzik static unsigned int ata_eh_dev_action(struct ata_device *dev)
131c6fd2807SJeff Garzik {
132c6fd2807SJeff Garzik 	struct ata_eh_context *ehc = &dev->ap->eh_context;
133c6fd2807SJeff Garzik 
134c6fd2807SJeff Garzik 	return ehc->i.action | ehc->i.dev_action[dev->devno];
135c6fd2807SJeff Garzik }
136c6fd2807SJeff Garzik 
137c6fd2807SJeff Garzik static void ata_eh_clear_action(struct ata_device *dev,
138c6fd2807SJeff Garzik 				struct ata_eh_info *ehi, unsigned int action)
139c6fd2807SJeff Garzik {
140c6fd2807SJeff Garzik 	int i;
141c6fd2807SJeff Garzik 
142c6fd2807SJeff Garzik 	if (!dev) {
143c6fd2807SJeff Garzik 		ehi->action &= ~action;
144c6fd2807SJeff Garzik 		for (i = 0; i < ATA_MAX_DEVICES; i++)
145c6fd2807SJeff Garzik 			ehi->dev_action[i] &= ~action;
146c6fd2807SJeff Garzik 	} else {
147c6fd2807SJeff Garzik 		/* doesn't make sense for port-wide EH actions */
148c6fd2807SJeff Garzik 		WARN_ON(!(action & ATA_EH_PERDEV_MASK));
149c6fd2807SJeff Garzik 
150c6fd2807SJeff Garzik 		/* break ehi->action into ehi->dev_action */
151c6fd2807SJeff Garzik 		if (ehi->action & action) {
152c6fd2807SJeff Garzik 			for (i = 0; i < ATA_MAX_DEVICES; i++)
153c6fd2807SJeff Garzik 				ehi->dev_action[i] |= ehi->action & action;
154c6fd2807SJeff Garzik 			ehi->action &= ~action;
155c6fd2807SJeff Garzik 		}
156c6fd2807SJeff Garzik 
157c6fd2807SJeff Garzik 		/* turn off the specified per-dev action */
158c6fd2807SJeff Garzik 		ehi->dev_action[dev->devno] &= ~action;
159c6fd2807SJeff Garzik 	}
160c6fd2807SJeff Garzik }
161c6fd2807SJeff Garzik 
162c6fd2807SJeff Garzik /**
163c6fd2807SJeff Garzik  *	ata_scsi_timed_out - SCSI layer time out callback
164c6fd2807SJeff Garzik  *	@cmd: timed out SCSI command
165c6fd2807SJeff Garzik  *
166c6fd2807SJeff Garzik  *	Handles SCSI layer timeout.  We race with normal completion of
167c6fd2807SJeff Garzik  *	the qc for @cmd.  If the qc is already gone, we lose and let
168c6fd2807SJeff Garzik  *	the scsi command finish (EH_HANDLED).  Otherwise, the qc has
169c6fd2807SJeff Garzik  *	timed out and EH should be invoked.  Prevent ata_qc_complete()
170c6fd2807SJeff Garzik  *	from finishing it by setting EH_SCHEDULED and return
171c6fd2807SJeff Garzik  *	EH_NOT_HANDLED.
172c6fd2807SJeff Garzik  *
173c6fd2807SJeff Garzik  *	TODO: kill this function once old EH is gone.
174c6fd2807SJeff Garzik  *
175c6fd2807SJeff Garzik  *	LOCKING:
176c6fd2807SJeff Garzik  *	Called from timer context
177c6fd2807SJeff Garzik  *
178c6fd2807SJeff Garzik  *	RETURNS:
179c6fd2807SJeff Garzik  *	EH_HANDLED or EH_NOT_HANDLED
180c6fd2807SJeff Garzik  */
181c6fd2807SJeff Garzik enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
182c6fd2807SJeff Garzik {
183c6fd2807SJeff Garzik 	struct Scsi_Host *host = cmd->device->host;
184c6fd2807SJeff Garzik 	struct ata_port *ap = ata_shost_to_port(host);
185c6fd2807SJeff Garzik 	unsigned long flags;
186c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
187c6fd2807SJeff Garzik 	enum scsi_eh_timer_return ret;
188c6fd2807SJeff Garzik 
189c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
190c6fd2807SJeff Garzik 
191c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
192c6fd2807SJeff Garzik 		ret = EH_NOT_HANDLED;
193c6fd2807SJeff Garzik 		goto out;
194c6fd2807SJeff Garzik 	}
195c6fd2807SJeff Garzik 
196c6fd2807SJeff Garzik 	ret = EH_HANDLED;
197c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
198c6fd2807SJeff Garzik 	qc = ata_qc_from_tag(ap, ap->active_tag);
199c6fd2807SJeff Garzik 	if (qc) {
200c6fd2807SJeff Garzik 		WARN_ON(qc->scsicmd != cmd);
201c6fd2807SJeff Garzik 		qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
202c6fd2807SJeff Garzik 		qc->err_mask |= AC_ERR_TIMEOUT;
203c6fd2807SJeff Garzik 		ret = EH_NOT_HANDLED;
204c6fd2807SJeff Garzik 	}
205c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
206c6fd2807SJeff Garzik 
207c6fd2807SJeff Garzik  out:
208c6fd2807SJeff Garzik 	DPRINTK("EXIT, ret=%d\n", ret);
209c6fd2807SJeff Garzik 	return ret;
210c6fd2807SJeff Garzik }
211c6fd2807SJeff Garzik 
212c6fd2807SJeff Garzik /**
213c6fd2807SJeff Garzik  *	ata_scsi_error - SCSI layer error handler callback
214c6fd2807SJeff Garzik  *	@host: SCSI host on which error occurred
215c6fd2807SJeff Garzik  *
216c6fd2807SJeff Garzik  *	Handles SCSI-layer-thrown error events.
217c6fd2807SJeff Garzik  *
218c6fd2807SJeff Garzik  *	LOCKING:
219c6fd2807SJeff Garzik  *	Inherited from SCSI layer (none, can sleep)
220c6fd2807SJeff Garzik  *
221c6fd2807SJeff Garzik  *	RETURNS:
222c6fd2807SJeff Garzik  *	Zero.
223c6fd2807SJeff Garzik  */
224c6fd2807SJeff Garzik void ata_scsi_error(struct Scsi_Host *host)
225c6fd2807SJeff Garzik {
226c6fd2807SJeff Garzik 	struct ata_port *ap = ata_shost_to_port(host);
227c6fd2807SJeff Garzik 	int i, repeat_cnt = ATA_EH_MAX_REPEAT;
228c6fd2807SJeff Garzik 	unsigned long flags;
229c6fd2807SJeff Garzik 
230c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
231c6fd2807SJeff Garzik 
232c6fd2807SJeff Garzik 	/* synchronize with port task */
233c6fd2807SJeff Garzik 	ata_port_flush_task(ap);
234c6fd2807SJeff Garzik 
235cca3974eSJeff Garzik 	/* synchronize with host lock and sort out timeouts */
236c6fd2807SJeff Garzik 
237c6fd2807SJeff Garzik 	/* For new EH, all qcs are finished in one of three ways -
238c6fd2807SJeff Garzik 	 * normal completion, error completion, and SCSI timeout.
239c6fd2807SJeff Garzik 	 * Both cmpletions can race against SCSI timeout.  When normal
240c6fd2807SJeff Garzik 	 * completion wins, the qc never reaches EH.  When error
241c6fd2807SJeff Garzik 	 * completion wins, the qc has ATA_QCFLAG_FAILED set.
242c6fd2807SJeff Garzik 	 *
243c6fd2807SJeff Garzik 	 * When SCSI timeout wins, things are a bit more complex.
244c6fd2807SJeff Garzik 	 * Normal or error completion can occur after the timeout but
245c6fd2807SJeff Garzik 	 * before this point.  In such cases, both types of
246c6fd2807SJeff Garzik 	 * completions are honored.  A scmd is determined to have
247c6fd2807SJeff Garzik 	 * timed out iff its associated qc is active and not failed.
248c6fd2807SJeff Garzik 	 */
249c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
250c6fd2807SJeff Garzik 		struct scsi_cmnd *scmd, *tmp;
251c6fd2807SJeff Garzik 		int nr_timedout = 0;
252c6fd2807SJeff Garzik 
253c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
254c6fd2807SJeff Garzik 
255c6fd2807SJeff Garzik 		list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
256c6fd2807SJeff Garzik 			struct ata_queued_cmd *qc;
257c6fd2807SJeff Garzik 
258c6fd2807SJeff Garzik 			for (i = 0; i < ATA_MAX_QUEUE; i++) {
259c6fd2807SJeff Garzik 				qc = __ata_qc_from_tag(ap, i);
260c6fd2807SJeff Garzik 				if (qc->flags & ATA_QCFLAG_ACTIVE &&
261c6fd2807SJeff Garzik 				    qc->scsicmd == scmd)
262c6fd2807SJeff Garzik 					break;
263c6fd2807SJeff Garzik 			}
264c6fd2807SJeff Garzik 
265c6fd2807SJeff Garzik 			if (i < ATA_MAX_QUEUE) {
266c6fd2807SJeff Garzik 				/* the scmd has an associated qc */
267c6fd2807SJeff Garzik 				if (!(qc->flags & ATA_QCFLAG_FAILED)) {
268c6fd2807SJeff Garzik 					/* which hasn't failed yet, timeout */
269c6fd2807SJeff Garzik 					qc->err_mask |= AC_ERR_TIMEOUT;
270c6fd2807SJeff Garzik 					qc->flags |= ATA_QCFLAG_FAILED;
271c6fd2807SJeff Garzik 					nr_timedout++;
272c6fd2807SJeff Garzik 				}
273c6fd2807SJeff Garzik 			} else {
274c6fd2807SJeff Garzik 				/* Normal completion occurred after
275c6fd2807SJeff Garzik 				 * SCSI timeout but before this point.
276c6fd2807SJeff Garzik 				 * Successfully complete it.
277c6fd2807SJeff Garzik 				 */
278c6fd2807SJeff Garzik 				scmd->retries = scmd->allowed;
279c6fd2807SJeff Garzik 				scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
280c6fd2807SJeff Garzik 			}
281c6fd2807SJeff Garzik 		}
282c6fd2807SJeff Garzik 
283c6fd2807SJeff Garzik 		/* If we have timed out qcs.  They belong to EH from
284c6fd2807SJeff Garzik 		 * this point but the state of the controller is
285c6fd2807SJeff Garzik 		 * unknown.  Freeze the port to make sure the IRQ
286c6fd2807SJeff Garzik 		 * handler doesn't diddle with those qcs.  This must
287c6fd2807SJeff Garzik 		 * be done atomically w.r.t. setting QCFLAG_FAILED.
288c6fd2807SJeff Garzik 		 */
289c6fd2807SJeff Garzik 		if (nr_timedout)
290c6fd2807SJeff Garzik 			__ata_port_freeze(ap);
291c6fd2807SJeff Garzik 
292c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
293c6fd2807SJeff Garzik 	} else
294c6fd2807SJeff Garzik 		spin_unlock_wait(ap->lock);
295c6fd2807SJeff Garzik 
296c6fd2807SJeff Garzik  repeat:
297c6fd2807SJeff Garzik 	/* invoke error handler */
298c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
299c6fd2807SJeff Garzik 		/* process port resume request */
300c6fd2807SJeff Garzik 		ata_eh_handle_port_resume(ap);
301c6fd2807SJeff Garzik 
302c6fd2807SJeff Garzik 		/* fetch & clear EH info */
303c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
304c6fd2807SJeff Garzik 
305c6fd2807SJeff Garzik 		memset(&ap->eh_context, 0, sizeof(ap->eh_context));
306c6fd2807SJeff Garzik 		ap->eh_context.i = ap->eh_info;
307c6fd2807SJeff Garzik 		memset(&ap->eh_info, 0, sizeof(ap->eh_info));
308c6fd2807SJeff Garzik 
309c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
310c6fd2807SJeff Garzik 		ap->pflags &= ~ATA_PFLAG_EH_PENDING;
311c6fd2807SJeff Garzik 
312c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
313c6fd2807SJeff Garzik 
314c6fd2807SJeff Garzik 		/* invoke EH, skip if unloading or suspended */
315c6fd2807SJeff Garzik 		if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
316c6fd2807SJeff Garzik 			ap->ops->error_handler(ap);
317c6fd2807SJeff Garzik 		else
318c6fd2807SJeff Garzik 			ata_eh_finish(ap);
319c6fd2807SJeff Garzik 
320c6fd2807SJeff Garzik 		/* process port suspend request */
321c6fd2807SJeff Garzik 		ata_eh_handle_port_suspend(ap);
322c6fd2807SJeff Garzik 
323c6fd2807SJeff Garzik 		/* Exception might have happend after ->error_handler
324c6fd2807SJeff Garzik 		 * recovered the port but before this point.  Repeat
325c6fd2807SJeff Garzik 		 * EH in such case.
326c6fd2807SJeff Garzik 		 */
327c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
328c6fd2807SJeff Garzik 
329c6fd2807SJeff Garzik 		if (ap->pflags & ATA_PFLAG_EH_PENDING) {
330c6fd2807SJeff Garzik 			if (--repeat_cnt) {
331c6fd2807SJeff Garzik 				ata_port_printk(ap, KERN_INFO,
332c6fd2807SJeff Garzik 					"EH pending after completion, "
333c6fd2807SJeff Garzik 					"repeating EH (cnt=%d)\n", repeat_cnt);
334c6fd2807SJeff Garzik 				spin_unlock_irqrestore(ap->lock, flags);
335c6fd2807SJeff Garzik 				goto repeat;
336c6fd2807SJeff Garzik 			}
337c6fd2807SJeff Garzik 			ata_port_printk(ap, KERN_ERR, "EH pending after %d "
338c6fd2807SJeff Garzik 					"tries, giving up\n", ATA_EH_MAX_REPEAT);
339c6fd2807SJeff Garzik 		}
340c6fd2807SJeff Garzik 
341c6fd2807SJeff Garzik 		/* this run is complete, make sure EH info is clear */
342c6fd2807SJeff Garzik 		memset(&ap->eh_info, 0, sizeof(ap->eh_info));
343c6fd2807SJeff Garzik 
344c6fd2807SJeff Garzik 		/* Clear host_eh_scheduled while holding ap->lock such
345c6fd2807SJeff Garzik 		 * that if exception occurs after this point but
346c6fd2807SJeff Garzik 		 * before EH completion, SCSI midlayer will
347c6fd2807SJeff Garzik 		 * re-initiate EH.
348c6fd2807SJeff Garzik 		 */
349c6fd2807SJeff Garzik 		host->host_eh_scheduled = 0;
350c6fd2807SJeff Garzik 
351c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
352c6fd2807SJeff Garzik 	} else {
353c6fd2807SJeff Garzik 		WARN_ON(ata_qc_from_tag(ap, ap->active_tag) == NULL);
354c6fd2807SJeff Garzik 		ap->ops->eng_timeout(ap);
355c6fd2807SJeff Garzik 	}
356c6fd2807SJeff Garzik 
357c6fd2807SJeff Garzik 	/* finish or retry handled scmd's and clean up */
358c6fd2807SJeff Garzik 	WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
359c6fd2807SJeff Garzik 
360c6fd2807SJeff Garzik 	scsi_eh_flush_done_q(&ap->eh_done_q);
361c6fd2807SJeff Garzik 
362c6fd2807SJeff Garzik 	/* clean up */
363c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
364c6fd2807SJeff Garzik 
365c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_LOADING)
366c6fd2807SJeff Garzik 		ap->pflags &= ~ATA_PFLAG_LOADING;
367c6fd2807SJeff Garzik 	else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
36852bad64dSDavid Howells 		queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0);
369c6fd2807SJeff Garzik 
370c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_RECOVERED)
371c6fd2807SJeff Garzik 		ata_port_printk(ap, KERN_INFO, "EH complete\n");
372c6fd2807SJeff Garzik 
373c6fd2807SJeff Garzik 	ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
374c6fd2807SJeff Garzik 
375c6fd2807SJeff Garzik 	/* tell wait_eh that we're done */
376c6fd2807SJeff Garzik 	ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
377c6fd2807SJeff Garzik 	wake_up_all(&ap->eh_wait_q);
378c6fd2807SJeff Garzik 
379c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
380c6fd2807SJeff Garzik 
381c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
382c6fd2807SJeff Garzik }
383c6fd2807SJeff Garzik 
384c6fd2807SJeff Garzik /**
385c6fd2807SJeff Garzik  *	ata_port_wait_eh - Wait for the currently pending EH to complete
386c6fd2807SJeff Garzik  *	@ap: Port to wait EH for
387c6fd2807SJeff Garzik  *
388c6fd2807SJeff Garzik  *	Wait until the currently pending EH is complete.
389c6fd2807SJeff Garzik  *
390c6fd2807SJeff Garzik  *	LOCKING:
391c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
392c6fd2807SJeff Garzik  */
393c6fd2807SJeff Garzik void ata_port_wait_eh(struct ata_port *ap)
394c6fd2807SJeff Garzik {
395c6fd2807SJeff Garzik 	unsigned long flags;
396c6fd2807SJeff Garzik 	DEFINE_WAIT(wait);
397c6fd2807SJeff Garzik 
398c6fd2807SJeff Garzik  retry:
399c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
400c6fd2807SJeff Garzik 
401c6fd2807SJeff Garzik 	while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
402c6fd2807SJeff Garzik 		prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
403c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
404c6fd2807SJeff Garzik 		schedule();
405c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
406c6fd2807SJeff Garzik 	}
407c6fd2807SJeff Garzik 	finish_wait(&ap->eh_wait_q, &wait);
408c6fd2807SJeff Garzik 
409c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
410c6fd2807SJeff Garzik 
411c6fd2807SJeff Garzik 	/* make sure SCSI EH is complete */
412cca3974eSJeff Garzik 	if (scsi_host_in_recovery(ap->scsi_host)) {
413c6fd2807SJeff Garzik 		msleep(10);
414c6fd2807SJeff Garzik 		goto retry;
415c6fd2807SJeff Garzik 	}
416c6fd2807SJeff Garzik }
417c6fd2807SJeff Garzik 
418c6fd2807SJeff Garzik /**
419c6fd2807SJeff Garzik  *	ata_qc_timeout - Handle timeout of queued command
420c6fd2807SJeff Garzik  *	@qc: Command that timed out
421c6fd2807SJeff Garzik  *
422c6fd2807SJeff Garzik  *	Some part of the kernel (currently, only the SCSI layer)
423c6fd2807SJeff Garzik  *	has noticed that the active command on port @ap has not
424c6fd2807SJeff Garzik  *	completed after a specified length of time.  Handle this
425c6fd2807SJeff Garzik  *	condition by disabling DMA (if necessary) and completing
426c6fd2807SJeff Garzik  *	transactions, with error if necessary.
427c6fd2807SJeff Garzik  *
428c6fd2807SJeff Garzik  *	This also handles the case of the "lost interrupt", where
429c6fd2807SJeff Garzik  *	for some reason (possibly hardware bug, possibly driver bug)
430c6fd2807SJeff Garzik  *	an interrupt was not delivered to the driver, even though the
431c6fd2807SJeff Garzik  *	transaction completed successfully.
432c6fd2807SJeff Garzik  *
433c6fd2807SJeff Garzik  *	TODO: kill this function once old EH is gone.
434c6fd2807SJeff Garzik  *
435c6fd2807SJeff Garzik  *	LOCKING:
436c6fd2807SJeff Garzik  *	Inherited from SCSI layer (none, can sleep)
437c6fd2807SJeff Garzik  */
438c6fd2807SJeff Garzik static void ata_qc_timeout(struct ata_queued_cmd *qc)
439c6fd2807SJeff Garzik {
440c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
441c6fd2807SJeff Garzik 	u8 host_stat = 0, drv_stat;
442c6fd2807SJeff Garzik 	unsigned long flags;
443c6fd2807SJeff Garzik 
444c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
445c6fd2807SJeff Garzik 
446c6fd2807SJeff Garzik 	ap->hsm_task_state = HSM_ST_IDLE;
447c6fd2807SJeff Garzik 
448c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
449c6fd2807SJeff Garzik 
450c6fd2807SJeff Garzik 	switch (qc->tf.protocol) {
451c6fd2807SJeff Garzik 
452c6fd2807SJeff Garzik 	case ATA_PROT_DMA:
453c6fd2807SJeff Garzik 	case ATA_PROT_ATAPI_DMA:
454c6fd2807SJeff Garzik 		host_stat = ap->ops->bmdma_status(ap);
455c6fd2807SJeff Garzik 
456c6fd2807SJeff Garzik 		/* before we do anything else, clear DMA-Start bit */
457c6fd2807SJeff Garzik 		ap->ops->bmdma_stop(qc);
458c6fd2807SJeff Garzik 
459c6fd2807SJeff Garzik 		/* fall through */
460c6fd2807SJeff Garzik 
461c6fd2807SJeff Garzik 	default:
462c6fd2807SJeff Garzik 		ata_altstatus(ap);
463c6fd2807SJeff Garzik 		drv_stat = ata_chk_status(ap);
464c6fd2807SJeff Garzik 
465c6fd2807SJeff Garzik 		/* ack bmdma irq events */
466c6fd2807SJeff Garzik 		ap->ops->irq_clear(ap);
467c6fd2807SJeff Garzik 
468c6fd2807SJeff Garzik 		ata_dev_printk(qc->dev, KERN_ERR, "command 0x%x timeout, "
469c6fd2807SJeff Garzik 			       "stat 0x%x host_stat 0x%x\n",
470c6fd2807SJeff Garzik 			       qc->tf.command, drv_stat, host_stat);
471c6fd2807SJeff Garzik 
472c6fd2807SJeff Garzik 		/* complete taskfile transaction */
473c6fd2807SJeff Garzik 		qc->err_mask |= AC_ERR_TIMEOUT;
474c6fd2807SJeff Garzik 		break;
475c6fd2807SJeff Garzik 	}
476c6fd2807SJeff Garzik 
477c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
478c6fd2807SJeff Garzik 
479c6fd2807SJeff Garzik 	ata_eh_qc_complete(qc);
480c6fd2807SJeff Garzik 
481c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
482c6fd2807SJeff Garzik }
483c6fd2807SJeff Garzik 
484c6fd2807SJeff Garzik /**
485c6fd2807SJeff Garzik  *	ata_eng_timeout - Handle timeout of queued command
486c6fd2807SJeff Garzik  *	@ap: Port on which timed-out command is active
487c6fd2807SJeff Garzik  *
488c6fd2807SJeff Garzik  *	Some part of the kernel (currently, only the SCSI layer)
489c6fd2807SJeff Garzik  *	has noticed that the active command on port @ap has not
490c6fd2807SJeff Garzik  *	completed after a specified length of time.  Handle this
491c6fd2807SJeff Garzik  *	condition by disabling DMA (if necessary) and completing
492c6fd2807SJeff Garzik  *	transactions, with error if necessary.
493c6fd2807SJeff Garzik  *
494c6fd2807SJeff Garzik  *	This also handles the case of the "lost interrupt", where
495c6fd2807SJeff Garzik  *	for some reason (possibly hardware bug, possibly driver bug)
496c6fd2807SJeff Garzik  *	an interrupt was not delivered to the driver, even though the
497c6fd2807SJeff Garzik  *	transaction completed successfully.
498c6fd2807SJeff Garzik  *
499c6fd2807SJeff Garzik  *	TODO: kill this function once old EH is gone.
500c6fd2807SJeff Garzik  *
501c6fd2807SJeff Garzik  *	LOCKING:
502c6fd2807SJeff Garzik  *	Inherited from SCSI layer (none, can sleep)
503c6fd2807SJeff Garzik  */
504c6fd2807SJeff Garzik void ata_eng_timeout(struct ata_port *ap)
505c6fd2807SJeff Garzik {
506c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
507c6fd2807SJeff Garzik 
508c6fd2807SJeff Garzik 	ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
509c6fd2807SJeff Garzik 
510c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
511c6fd2807SJeff Garzik }
512c6fd2807SJeff Garzik 
513c6fd2807SJeff Garzik /**
514c6fd2807SJeff Garzik  *	ata_qc_schedule_eh - schedule qc for error handling
515c6fd2807SJeff Garzik  *	@qc: command to schedule error handling for
516c6fd2807SJeff Garzik  *
517c6fd2807SJeff Garzik  *	Schedule error handling for @qc.  EH will kick in as soon as
518c6fd2807SJeff Garzik  *	other commands are drained.
519c6fd2807SJeff Garzik  *
520c6fd2807SJeff Garzik  *	LOCKING:
521cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
522c6fd2807SJeff Garzik  */
523c6fd2807SJeff Garzik void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
524c6fd2807SJeff Garzik {
525c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
526c6fd2807SJeff Garzik 
527c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
528c6fd2807SJeff Garzik 
529c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_FAILED;
530c6fd2807SJeff Garzik 	qc->ap->pflags |= ATA_PFLAG_EH_PENDING;
531c6fd2807SJeff Garzik 
532c6fd2807SJeff Garzik 	/* The following will fail if timeout has already expired.
533c6fd2807SJeff Garzik 	 * ata_scsi_error() takes care of such scmds on EH entry.
534c6fd2807SJeff Garzik 	 * Note that ATA_QCFLAG_FAILED is unconditionally set after
535c6fd2807SJeff Garzik 	 * this function completes.
536c6fd2807SJeff Garzik 	 */
537c6fd2807SJeff Garzik 	scsi_req_abort_cmd(qc->scsicmd);
538c6fd2807SJeff Garzik }
539c6fd2807SJeff Garzik 
540c6fd2807SJeff Garzik /**
541c6fd2807SJeff Garzik  *	ata_port_schedule_eh - schedule error handling without a qc
542c6fd2807SJeff Garzik  *	@ap: ATA port to schedule EH for
543c6fd2807SJeff Garzik  *
544c6fd2807SJeff Garzik  *	Schedule error handling for @ap.  EH will kick in as soon as
545c6fd2807SJeff Garzik  *	all commands are drained.
546c6fd2807SJeff Garzik  *
547c6fd2807SJeff Garzik  *	LOCKING:
548cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
549c6fd2807SJeff Garzik  */
550c6fd2807SJeff Garzik void ata_port_schedule_eh(struct ata_port *ap)
551c6fd2807SJeff Garzik {
552c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
553c6fd2807SJeff Garzik 
554f4d6d004STejun Heo 	if (ap->pflags & ATA_PFLAG_INITIALIZING)
555f4d6d004STejun Heo 		return;
556f4d6d004STejun Heo 
557c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_EH_PENDING;
558cca3974eSJeff Garzik 	scsi_schedule_eh(ap->scsi_host);
559c6fd2807SJeff Garzik 
560c6fd2807SJeff Garzik 	DPRINTK("port EH scheduled\n");
561c6fd2807SJeff Garzik }
562c6fd2807SJeff Garzik 
563c6fd2807SJeff Garzik /**
564c6fd2807SJeff Garzik  *	ata_port_abort - abort all qc's on the port
565c6fd2807SJeff Garzik  *	@ap: ATA port to abort qc's for
566c6fd2807SJeff Garzik  *
567c6fd2807SJeff Garzik  *	Abort all active qc's of @ap and schedule EH.
568c6fd2807SJeff Garzik  *
569c6fd2807SJeff Garzik  *	LOCKING:
570cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
571c6fd2807SJeff Garzik  *
572c6fd2807SJeff Garzik  *	RETURNS:
573c6fd2807SJeff Garzik  *	Number of aborted qc's.
574c6fd2807SJeff Garzik  */
575c6fd2807SJeff Garzik int ata_port_abort(struct ata_port *ap)
576c6fd2807SJeff Garzik {
577c6fd2807SJeff Garzik 	int tag, nr_aborted = 0;
578c6fd2807SJeff Garzik 
579c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
580c6fd2807SJeff Garzik 
581c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
582c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
583c6fd2807SJeff Garzik 
584c6fd2807SJeff Garzik 		if (qc) {
585c6fd2807SJeff Garzik 			qc->flags |= ATA_QCFLAG_FAILED;
586c6fd2807SJeff Garzik 			ata_qc_complete(qc);
587c6fd2807SJeff Garzik 			nr_aborted++;
588c6fd2807SJeff Garzik 		}
589c6fd2807SJeff Garzik 	}
590c6fd2807SJeff Garzik 
591c6fd2807SJeff Garzik 	if (!nr_aborted)
592c6fd2807SJeff Garzik 		ata_port_schedule_eh(ap);
593c6fd2807SJeff Garzik 
594c6fd2807SJeff Garzik 	return nr_aborted;
595c6fd2807SJeff Garzik }
596c6fd2807SJeff Garzik 
597c6fd2807SJeff Garzik /**
598c6fd2807SJeff Garzik  *	__ata_port_freeze - freeze port
599c6fd2807SJeff Garzik  *	@ap: ATA port to freeze
600c6fd2807SJeff Garzik  *
601c6fd2807SJeff Garzik  *	This function is called when HSM violation or some other
602c6fd2807SJeff Garzik  *	condition disrupts normal operation of the port.  Frozen port
603c6fd2807SJeff Garzik  *	is not allowed to perform any operation until the port is
604c6fd2807SJeff Garzik  *	thawed, which usually follows a successful reset.
605c6fd2807SJeff Garzik  *
606c6fd2807SJeff Garzik  *	ap->ops->freeze() callback can be used for freezing the port
607c6fd2807SJeff Garzik  *	hardware-wise (e.g. mask interrupt and stop DMA engine).  If a
608c6fd2807SJeff Garzik  *	port cannot be frozen hardware-wise, the interrupt handler
609c6fd2807SJeff Garzik  *	must ack and clear interrupts unconditionally while the port
610c6fd2807SJeff Garzik  *	is frozen.
611c6fd2807SJeff Garzik  *
612c6fd2807SJeff Garzik  *	LOCKING:
613cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
614c6fd2807SJeff Garzik  */
615c6fd2807SJeff Garzik static void __ata_port_freeze(struct ata_port *ap)
616c6fd2807SJeff Garzik {
617c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
618c6fd2807SJeff Garzik 
619c6fd2807SJeff Garzik 	if (ap->ops->freeze)
620c6fd2807SJeff Garzik 		ap->ops->freeze(ap);
621c6fd2807SJeff Garzik 
622c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_FROZEN;
623c6fd2807SJeff Garzik 
62444877b4eSTejun Heo 	DPRINTK("ata%u port frozen\n", ap->print_id);
625c6fd2807SJeff Garzik }
626c6fd2807SJeff Garzik 
627c6fd2807SJeff Garzik /**
628c6fd2807SJeff Garzik  *	ata_port_freeze - abort & freeze port
629c6fd2807SJeff Garzik  *	@ap: ATA port to freeze
630c6fd2807SJeff Garzik  *
631c6fd2807SJeff Garzik  *	Abort and freeze @ap.
632c6fd2807SJeff Garzik  *
633c6fd2807SJeff Garzik  *	LOCKING:
634cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
635c6fd2807SJeff Garzik  *
636c6fd2807SJeff Garzik  *	RETURNS:
637c6fd2807SJeff Garzik  *	Number of aborted commands.
638c6fd2807SJeff Garzik  */
639c6fd2807SJeff Garzik int ata_port_freeze(struct ata_port *ap)
640c6fd2807SJeff Garzik {
641c6fd2807SJeff Garzik 	int nr_aborted;
642c6fd2807SJeff Garzik 
643c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
644c6fd2807SJeff Garzik 
645c6fd2807SJeff Garzik 	nr_aborted = ata_port_abort(ap);
646c6fd2807SJeff Garzik 	__ata_port_freeze(ap);
647c6fd2807SJeff Garzik 
648c6fd2807SJeff Garzik 	return nr_aborted;
649c6fd2807SJeff Garzik }
650c6fd2807SJeff Garzik 
651c6fd2807SJeff Garzik /**
652c6fd2807SJeff Garzik  *	ata_eh_freeze_port - EH helper to freeze port
653c6fd2807SJeff Garzik  *	@ap: ATA port to freeze
654c6fd2807SJeff Garzik  *
655c6fd2807SJeff Garzik  *	Freeze @ap.
656c6fd2807SJeff Garzik  *
657c6fd2807SJeff Garzik  *	LOCKING:
658c6fd2807SJeff Garzik  *	None.
659c6fd2807SJeff Garzik  */
660c6fd2807SJeff Garzik void ata_eh_freeze_port(struct ata_port *ap)
661c6fd2807SJeff Garzik {
662c6fd2807SJeff Garzik 	unsigned long flags;
663c6fd2807SJeff Garzik 
664c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
665c6fd2807SJeff Garzik 		return;
666c6fd2807SJeff Garzik 
667c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
668c6fd2807SJeff Garzik 	__ata_port_freeze(ap);
669c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
670c6fd2807SJeff Garzik }
671c6fd2807SJeff Garzik 
672c6fd2807SJeff Garzik /**
673c6fd2807SJeff Garzik  *	ata_port_thaw_port - EH helper to thaw port
674c6fd2807SJeff Garzik  *	@ap: ATA port to thaw
675c6fd2807SJeff Garzik  *
676c6fd2807SJeff Garzik  *	Thaw frozen port @ap.
677c6fd2807SJeff Garzik  *
678c6fd2807SJeff Garzik  *	LOCKING:
679c6fd2807SJeff Garzik  *	None.
680c6fd2807SJeff Garzik  */
681c6fd2807SJeff Garzik void ata_eh_thaw_port(struct ata_port *ap)
682c6fd2807SJeff Garzik {
683c6fd2807SJeff Garzik 	unsigned long flags;
684c6fd2807SJeff Garzik 
685c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
686c6fd2807SJeff Garzik 		return;
687c6fd2807SJeff Garzik 
688c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
689c6fd2807SJeff Garzik 
690c6fd2807SJeff Garzik 	ap->pflags &= ~ATA_PFLAG_FROZEN;
691c6fd2807SJeff Garzik 
692c6fd2807SJeff Garzik 	if (ap->ops->thaw)
693c6fd2807SJeff Garzik 		ap->ops->thaw(ap);
694c6fd2807SJeff Garzik 
695c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
696c6fd2807SJeff Garzik 
69744877b4eSTejun Heo 	DPRINTK("ata%u port thawed\n", ap->print_id);
698c6fd2807SJeff Garzik }
699c6fd2807SJeff Garzik 
700c6fd2807SJeff Garzik static void ata_eh_scsidone(struct scsi_cmnd *scmd)
701c6fd2807SJeff Garzik {
702c6fd2807SJeff Garzik 	/* nada */
703c6fd2807SJeff Garzik }
704c6fd2807SJeff Garzik 
705c6fd2807SJeff Garzik static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
706c6fd2807SJeff Garzik {
707c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
708c6fd2807SJeff Garzik 	struct scsi_cmnd *scmd = qc->scsicmd;
709c6fd2807SJeff Garzik 	unsigned long flags;
710c6fd2807SJeff Garzik 
711c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
712c6fd2807SJeff Garzik 	qc->scsidone = ata_eh_scsidone;
713c6fd2807SJeff Garzik 	__ata_qc_complete(qc);
714c6fd2807SJeff Garzik 	WARN_ON(ata_tag_valid(qc->tag));
715c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
716c6fd2807SJeff Garzik 
717c6fd2807SJeff Garzik 	scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
718c6fd2807SJeff Garzik }
719c6fd2807SJeff Garzik 
720c6fd2807SJeff Garzik /**
721c6fd2807SJeff Garzik  *	ata_eh_qc_complete - Complete an active ATA command from EH
722c6fd2807SJeff Garzik  *	@qc: Command to complete
723c6fd2807SJeff Garzik  *
724c6fd2807SJeff Garzik  *	Indicate to the mid and upper layers that an ATA command has
725c6fd2807SJeff Garzik  *	completed.  To be used from EH.
726c6fd2807SJeff Garzik  */
727c6fd2807SJeff Garzik void ata_eh_qc_complete(struct ata_queued_cmd *qc)
728c6fd2807SJeff Garzik {
729c6fd2807SJeff Garzik 	struct scsi_cmnd *scmd = qc->scsicmd;
730c6fd2807SJeff Garzik 	scmd->retries = scmd->allowed;
731c6fd2807SJeff Garzik 	__ata_eh_qc_complete(qc);
732c6fd2807SJeff Garzik }
733c6fd2807SJeff Garzik 
734c6fd2807SJeff Garzik /**
735c6fd2807SJeff Garzik  *	ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
736c6fd2807SJeff Garzik  *	@qc: Command to retry
737c6fd2807SJeff Garzik  *
738c6fd2807SJeff Garzik  *	Indicate to the mid and upper layers that an ATA command
739c6fd2807SJeff Garzik  *	should be retried.  To be used from EH.
740c6fd2807SJeff Garzik  *
741c6fd2807SJeff Garzik  *	SCSI midlayer limits the number of retries to scmd->allowed.
742c6fd2807SJeff Garzik  *	scmd->retries is decremented for commands which get retried
743c6fd2807SJeff Garzik  *	due to unrelated failures (qc->err_mask is zero).
744c6fd2807SJeff Garzik  */
745c6fd2807SJeff Garzik void ata_eh_qc_retry(struct ata_queued_cmd *qc)
746c6fd2807SJeff Garzik {
747c6fd2807SJeff Garzik 	struct scsi_cmnd *scmd = qc->scsicmd;
748c6fd2807SJeff Garzik 	if (!qc->err_mask && scmd->retries)
749c6fd2807SJeff Garzik 		scmd->retries--;
750c6fd2807SJeff Garzik 	__ata_eh_qc_complete(qc);
751c6fd2807SJeff Garzik }
752c6fd2807SJeff Garzik 
753c6fd2807SJeff Garzik /**
754c6fd2807SJeff Garzik  *	ata_eh_detach_dev - detach ATA device
755c6fd2807SJeff Garzik  *	@dev: ATA device to detach
756c6fd2807SJeff Garzik  *
757c6fd2807SJeff Garzik  *	Detach @dev.
758c6fd2807SJeff Garzik  *
759c6fd2807SJeff Garzik  *	LOCKING:
760c6fd2807SJeff Garzik  *	None.
761c6fd2807SJeff Garzik  */
762c6fd2807SJeff Garzik static void ata_eh_detach_dev(struct ata_device *dev)
763c6fd2807SJeff Garzik {
764c6fd2807SJeff Garzik 	struct ata_port *ap = dev->ap;
765c6fd2807SJeff Garzik 	unsigned long flags;
766c6fd2807SJeff Garzik 
767c6fd2807SJeff Garzik 	ata_dev_disable(dev);
768c6fd2807SJeff Garzik 
769c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
770c6fd2807SJeff Garzik 
771c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_DETACH;
772c6fd2807SJeff Garzik 
773c6fd2807SJeff Garzik 	if (ata_scsi_offline_dev(dev)) {
774c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_DETACHED;
775c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
776c6fd2807SJeff Garzik 	}
777c6fd2807SJeff Garzik 
778c6fd2807SJeff Garzik 	/* clear per-dev EH actions */
779c6fd2807SJeff Garzik 	ata_eh_clear_action(dev, &ap->eh_info, ATA_EH_PERDEV_MASK);
780c6fd2807SJeff Garzik 	ata_eh_clear_action(dev, &ap->eh_context.i, ATA_EH_PERDEV_MASK);
781c6fd2807SJeff Garzik 
782c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
783c6fd2807SJeff Garzik }
784c6fd2807SJeff Garzik 
785c6fd2807SJeff Garzik /**
786c6fd2807SJeff Garzik  *	ata_eh_about_to_do - about to perform eh_action
787c6fd2807SJeff Garzik  *	@ap: target ATA port
788c6fd2807SJeff Garzik  *	@dev: target ATA dev for per-dev action (can be NULL)
789c6fd2807SJeff Garzik  *	@action: action about to be performed
790c6fd2807SJeff Garzik  *
791c6fd2807SJeff Garzik  *	Called just before performing EH actions to clear related bits
792c6fd2807SJeff Garzik  *	in @ap->eh_info such that eh actions are not unnecessarily
793c6fd2807SJeff Garzik  *	repeated.
794c6fd2807SJeff Garzik  *
795c6fd2807SJeff Garzik  *	LOCKING:
796c6fd2807SJeff Garzik  *	None.
797c6fd2807SJeff Garzik  */
798c6fd2807SJeff Garzik static void ata_eh_about_to_do(struct ata_port *ap, struct ata_device *dev,
799c6fd2807SJeff Garzik 			       unsigned int action)
800c6fd2807SJeff Garzik {
801c6fd2807SJeff Garzik 	unsigned long flags;
802c6fd2807SJeff Garzik 	struct ata_eh_info *ehi = &ap->eh_info;
803c6fd2807SJeff Garzik 	struct ata_eh_context *ehc = &ap->eh_context;
804c6fd2807SJeff Garzik 
805c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
806c6fd2807SJeff Garzik 
807c6fd2807SJeff Garzik 	/* Reset is represented by combination of actions and EHI
808c6fd2807SJeff Garzik 	 * flags.  Suck in all related bits before clearing eh_info to
809c6fd2807SJeff Garzik 	 * avoid losing requested action.
810c6fd2807SJeff Garzik 	 */
811c6fd2807SJeff Garzik 	if (action & ATA_EH_RESET_MASK) {
812c6fd2807SJeff Garzik 		ehc->i.action |= ehi->action & ATA_EH_RESET_MASK;
813c6fd2807SJeff Garzik 		ehc->i.flags |= ehi->flags & ATA_EHI_RESET_MODIFIER_MASK;
814c6fd2807SJeff Garzik 
815c6fd2807SJeff Garzik 		/* make sure all reset actions are cleared & clear EHI flags */
816c6fd2807SJeff Garzik 		action |= ATA_EH_RESET_MASK;
817c6fd2807SJeff Garzik 		ehi->flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
818c6fd2807SJeff Garzik 	}
819c6fd2807SJeff Garzik 
820c6fd2807SJeff Garzik 	ata_eh_clear_action(dev, ehi, action);
821c6fd2807SJeff Garzik 
822c6fd2807SJeff Garzik 	if (!(ehc->i.flags & ATA_EHI_QUIET))
823c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_RECOVERED;
824c6fd2807SJeff Garzik 
825c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
826c6fd2807SJeff Garzik }
827c6fd2807SJeff Garzik 
828c6fd2807SJeff Garzik /**
829c6fd2807SJeff Garzik  *	ata_eh_done - EH action complete
830c6fd2807SJeff Garzik  *	@ap: target ATA port
831c6fd2807SJeff Garzik  *	@dev: target ATA dev for per-dev action (can be NULL)
832c6fd2807SJeff Garzik  *	@action: action just completed
833c6fd2807SJeff Garzik  *
834c6fd2807SJeff Garzik  *	Called right after performing EH actions to clear related bits
835c6fd2807SJeff Garzik  *	in @ap->eh_context.
836c6fd2807SJeff Garzik  *
837c6fd2807SJeff Garzik  *	LOCKING:
838c6fd2807SJeff Garzik  *	None.
839c6fd2807SJeff Garzik  */
840c6fd2807SJeff Garzik static void ata_eh_done(struct ata_port *ap, struct ata_device *dev,
841c6fd2807SJeff Garzik 			unsigned int action)
842c6fd2807SJeff Garzik {
843c6fd2807SJeff Garzik 	/* if reset is complete, clear all reset actions & reset modifier */
844c6fd2807SJeff Garzik 	if (action & ATA_EH_RESET_MASK) {
845c6fd2807SJeff Garzik 		action |= ATA_EH_RESET_MASK;
846c6fd2807SJeff Garzik 		ap->eh_context.i.flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
847c6fd2807SJeff Garzik 	}
848c6fd2807SJeff Garzik 
849c6fd2807SJeff Garzik 	ata_eh_clear_action(dev, &ap->eh_context.i, action);
850c6fd2807SJeff Garzik }
851c6fd2807SJeff Garzik 
852c6fd2807SJeff Garzik /**
853c6fd2807SJeff Garzik  *	ata_err_string - convert err_mask to descriptive string
854c6fd2807SJeff Garzik  *	@err_mask: error mask to convert to string
855c6fd2807SJeff Garzik  *
856c6fd2807SJeff Garzik  *	Convert @err_mask to descriptive string.  Errors are
857c6fd2807SJeff Garzik  *	prioritized according to severity and only the most severe
858c6fd2807SJeff Garzik  *	error is reported.
859c6fd2807SJeff Garzik  *
860c6fd2807SJeff Garzik  *	LOCKING:
861c6fd2807SJeff Garzik  *	None.
862c6fd2807SJeff Garzik  *
863c6fd2807SJeff Garzik  *	RETURNS:
864c6fd2807SJeff Garzik  *	Descriptive string for @err_mask
865c6fd2807SJeff Garzik  */
866c6fd2807SJeff Garzik static const char * ata_err_string(unsigned int err_mask)
867c6fd2807SJeff Garzik {
868c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_HOST_BUS)
869c6fd2807SJeff Garzik 		return "host bus error";
870c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_ATA_BUS)
871c6fd2807SJeff Garzik 		return "ATA bus error";
872c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_TIMEOUT)
873c6fd2807SJeff Garzik 		return "timeout";
874c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_HSM)
875c6fd2807SJeff Garzik 		return "HSM violation";
876c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_SYSTEM)
877c6fd2807SJeff Garzik 		return "internal error";
878c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_MEDIA)
879c6fd2807SJeff Garzik 		return "media error";
880c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_INVALID)
881c6fd2807SJeff Garzik 		return "invalid argument";
882c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_DEV)
883c6fd2807SJeff Garzik 		return "device error";
884c6fd2807SJeff Garzik 	return "unknown error";
885c6fd2807SJeff Garzik }
886c6fd2807SJeff Garzik 
887c6fd2807SJeff Garzik /**
888c6fd2807SJeff Garzik  *	ata_read_log_page - read a specific log page
889c6fd2807SJeff Garzik  *	@dev: target device
890c6fd2807SJeff Garzik  *	@page: page to read
891c6fd2807SJeff Garzik  *	@buf: buffer to store read page
892c6fd2807SJeff Garzik  *	@sectors: number of sectors to read
893c6fd2807SJeff Garzik  *
894c6fd2807SJeff Garzik  *	Read log page using READ_LOG_EXT command.
895c6fd2807SJeff Garzik  *
896c6fd2807SJeff Garzik  *	LOCKING:
897c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
898c6fd2807SJeff Garzik  *
899c6fd2807SJeff Garzik  *	RETURNS:
900c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
901c6fd2807SJeff Garzik  */
902c6fd2807SJeff Garzik static unsigned int ata_read_log_page(struct ata_device *dev,
903c6fd2807SJeff Garzik 				      u8 page, void *buf, unsigned int sectors)
904c6fd2807SJeff Garzik {
905c6fd2807SJeff Garzik 	struct ata_taskfile tf;
906c6fd2807SJeff Garzik 	unsigned int err_mask;
907c6fd2807SJeff Garzik 
908c6fd2807SJeff Garzik 	DPRINTK("read log page - page %d\n", page);
909c6fd2807SJeff Garzik 
910c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
911c6fd2807SJeff Garzik 	tf.command = ATA_CMD_READ_LOG_EXT;
912c6fd2807SJeff Garzik 	tf.lbal = page;
913c6fd2807SJeff Garzik 	tf.nsect = sectors;
914c6fd2807SJeff Garzik 	tf.hob_nsect = sectors >> 8;
915c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
916c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_PIO;
917c6fd2807SJeff Garzik 
918c6fd2807SJeff Garzik 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
919c6fd2807SJeff Garzik 				     buf, sectors * ATA_SECT_SIZE);
920c6fd2807SJeff Garzik 
921c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
922c6fd2807SJeff Garzik 	return err_mask;
923c6fd2807SJeff Garzik }
924c6fd2807SJeff Garzik 
925c6fd2807SJeff Garzik /**
926c6fd2807SJeff Garzik  *	ata_eh_read_log_10h - Read log page 10h for NCQ error details
927c6fd2807SJeff Garzik  *	@dev: Device to read log page 10h from
928c6fd2807SJeff Garzik  *	@tag: Resulting tag of the failed command
929c6fd2807SJeff Garzik  *	@tf: Resulting taskfile registers of the failed command
930c6fd2807SJeff Garzik  *
931c6fd2807SJeff Garzik  *	Read log page 10h to obtain NCQ error details and clear error
932c6fd2807SJeff Garzik  *	condition.
933c6fd2807SJeff Garzik  *
934c6fd2807SJeff Garzik  *	LOCKING:
935c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
936c6fd2807SJeff Garzik  *
937c6fd2807SJeff Garzik  *	RETURNS:
938c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
939c6fd2807SJeff Garzik  */
940c6fd2807SJeff Garzik static int ata_eh_read_log_10h(struct ata_device *dev,
941c6fd2807SJeff Garzik 			       int *tag, struct ata_taskfile *tf)
942c6fd2807SJeff Garzik {
943c6fd2807SJeff Garzik 	u8 *buf = dev->ap->sector_buf;
944c6fd2807SJeff Garzik 	unsigned int err_mask;
945c6fd2807SJeff Garzik 	u8 csum;
946c6fd2807SJeff Garzik 	int i;
947c6fd2807SJeff Garzik 
948c6fd2807SJeff Garzik 	err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
949c6fd2807SJeff Garzik 	if (err_mask)
950c6fd2807SJeff Garzik 		return -EIO;
951c6fd2807SJeff Garzik 
952c6fd2807SJeff Garzik 	csum = 0;
953c6fd2807SJeff Garzik 	for (i = 0; i < ATA_SECT_SIZE; i++)
954c6fd2807SJeff Garzik 		csum += buf[i];
955c6fd2807SJeff Garzik 	if (csum)
956c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_WARNING,
957c6fd2807SJeff Garzik 			       "invalid checksum 0x%x on log page 10h\n", csum);
958c6fd2807SJeff Garzik 
959c6fd2807SJeff Garzik 	if (buf[0] & 0x80)
960c6fd2807SJeff Garzik 		return -ENOENT;
961c6fd2807SJeff Garzik 
962c6fd2807SJeff Garzik 	*tag = buf[0] & 0x1f;
963c6fd2807SJeff Garzik 
964c6fd2807SJeff Garzik 	tf->command = buf[2];
965c6fd2807SJeff Garzik 	tf->feature = buf[3];
966c6fd2807SJeff Garzik 	tf->lbal = buf[4];
967c6fd2807SJeff Garzik 	tf->lbam = buf[5];
968c6fd2807SJeff Garzik 	tf->lbah = buf[6];
969c6fd2807SJeff Garzik 	tf->device = buf[7];
970c6fd2807SJeff Garzik 	tf->hob_lbal = buf[8];
971c6fd2807SJeff Garzik 	tf->hob_lbam = buf[9];
972c6fd2807SJeff Garzik 	tf->hob_lbah = buf[10];
973c6fd2807SJeff Garzik 	tf->nsect = buf[12];
974c6fd2807SJeff Garzik 	tf->hob_nsect = buf[13];
975c6fd2807SJeff Garzik 
976c6fd2807SJeff Garzik 	return 0;
977c6fd2807SJeff Garzik }
978c6fd2807SJeff Garzik 
979c6fd2807SJeff Garzik /**
980c6fd2807SJeff Garzik  *	atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
981c6fd2807SJeff Garzik  *	@dev: device to perform REQUEST_SENSE to
982c6fd2807SJeff Garzik  *	@sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
983c6fd2807SJeff Garzik  *
984c6fd2807SJeff Garzik  *	Perform ATAPI REQUEST_SENSE after the device reported CHECK
985c6fd2807SJeff Garzik  *	SENSE.  This function is EH helper.
986c6fd2807SJeff Garzik  *
987c6fd2807SJeff Garzik  *	LOCKING:
988c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
989c6fd2807SJeff Garzik  *
990c6fd2807SJeff Garzik  *	RETURNS:
991c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask on failure
992c6fd2807SJeff Garzik  */
99356287768SAlbert Lee static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc)
994c6fd2807SJeff Garzik {
99556287768SAlbert Lee 	struct ata_device *dev = qc->dev;
99656287768SAlbert Lee 	unsigned char *sense_buf = qc->scsicmd->sense_buffer;
997c6fd2807SJeff Garzik 	struct ata_port *ap = dev->ap;
998c6fd2807SJeff Garzik 	struct ata_taskfile tf;
999c6fd2807SJeff Garzik 	u8 cdb[ATAPI_CDB_LEN];
1000c6fd2807SJeff Garzik 
1001c6fd2807SJeff Garzik 	DPRINTK("ATAPI request sense\n");
1002c6fd2807SJeff Garzik 
1003c6fd2807SJeff Garzik 	/* FIXME: is this needed? */
1004c6fd2807SJeff Garzik 	memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1005c6fd2807SJeff Garzik 
100656287768SAlbert Lee 	/* initialize sense_buf with the error register,
100756287768SAlbert Lee 	 * for the case where they are -not- overwritten
100856287768SAlbert Lee 	 */
1009c6fd2807SJeff Garzik 	sense_buf[0] = 0x70;
101056287768SAlbert Lee 	sense_buf[2] = qc->result_tf.feature >> 4;
101156287768SAlbert Lee 
101256287768SAlbert Lee 	/* some devices time out if garbage left in tf */
101356287768SAlbert Lee 	ata_tf_init(dev, &tf);
1014c6fd2807SJeff Garzik 
1015c6fd2807SJeff Garzik 	memset(cdb, 0, ATAPI_CDB_LEN);
1016c6fd2807SJeff Garzik 	cdb[0] = REQUEST_SENSE;
1017c6fd2807SJeff Garzik 	cdb[4] = SCSI_SENSE_BUFFERSIZE;
1018c6fd2807SJeff Garzik 
1019c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1020c6fd2807SJeff Garzik 	tf.command = ATA_CMD_PACKET;
1021c6fd2807SJeff Garzik 
1022c6fd2807SJeff Garzik 	/* is it pointless to prefer PIO for "safety reasons"? */
1023c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_PIO_DMA) {
1024c6fd2807SJeff Garzik 		tf.protocol = ATA_PROT_ATAPI_DMA;
1025c6fd2807SJeff Garzik 		tf.feature |= ATAPI_PKT_DMA;
1026c6fd2807SJeff Garzik 	} else {
1027c6fd2807SJeff Garzik 		tf.protocol = ATA_PROT_ATAPI;
1028c6fd2807SJeff Garzik 		tf.lbam = (8 * 1024) & 0xff;
1029c6fd2807SJeff Garzik 		tf.lbah = (8 * 1024) >> 8;
1030c6fd2807SJeff Garzik 	}
1031c6fd2807SJeff Garzik 
1032c6fd2807SJeff Garzik 	return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
1033c6fd2807SJeff Garzik 				 sense_buf, SCSI_SENSE_BUFFERSIZE);
1034c6fd2807SJeff Garzik }
1035c6fd2807SJeff Garzik 
1036c6fd2807SJeff Garzik /**
1037c6fd2807SJeff Garzik  *	ata_eh_analyze_serror - analyze SError for a failed port
1038c6fd2807SJeff Garzik  *	@ap: ATA port to analyze SError for
1039c6fd2807SJeff Garzik  *
1040c6fd2807SJeff Garzik  *	Analyze SError if available and further determine cause of
1041c6fd2807SJeff Garzik  *	failure.
1042c6fd2807SJeff Garzik  *
1043c6fd2807SJeff Garzik  *	LOCKING:
1044c6fd2807SJeff Garzik  *	None.
1045c6fd2807SJeff Garzik  */
1046c6fd2807SJeff Garzik static void ata_eh_analyze_serror(struct ata_port *ap)
1047c6fd2807SJeff Garzik {
1048c6fd2807SJeff Garzik 	struct ata_eh_context *ehc = &ap->eh_context;
1049c6fd2807SJeff Garzik 	u32 serror = ehc->i.serror;
1050c6fd2807SJeff Garzik 	unsigned int err_mask = 0, action = 0;
1051c6fd2807SJeff Garzik 
1052c6fd2807SJeff Garzik 	if (serror & SERR_PERSISTENT) {
1053c6fd2807SJeff Garzik 		err_mask |= AC_ERR_ATA_BUS;
1054c6fd2807SJeff Garzik 		action |= ATA_EH_HARDRESET;
1055c6fd2807SJeff Garzik 	}
1056c6fd2807SJeff Garzik 	if (serror &
1057c6fd2807SJeff Garzik 	    (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
1058c6fd2807SJeff Garzik 		err_mask |= AC_ERR_ATA_BUS;
1059c6fd2807SJeff Garzik 		action |= ATA_EH_SOFTRESET;
1060c6fd2807SJeff Garzik 	}
1061c6fd2807SJeff Garzik 	if (serror & SERR_PROTOCOL) {
1062c6fd2807SJeff Garzik 		err_mask |= AC_ERR_HSM;
1063c6fd2807SJeff Garzik 		action |= ATA_EH_SOFTRESET;
1064c6fd2807SJeff Garzik 	}
1065c6fd2807SJeff Garzik 	if (serror & SERR_INTERNAL) {
1066c6fd2807SJeff Garzik 		err_mask |= AC_ERR_SYSTEM;
1067771b8dadSTejun Heo 		action |= ATA_EH_HARDRESET;
1068c6fd2807SJeff Garzik 	}
1069c6fd2807SJeff Garzik 	if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG))
1070c6fd2807SJeff Garzik 		ata_ehi_hotplugged(&ehc->i);
1071c6fd2807SJeff Garzik 
1072c6fd2807SJeff Garzik 	ehc->i.err_mask |= err_mask;
1073c6fd2807SJeff Garzik 	ehc->i.action |= action;
1074c6fd2807SJeff Garzik }
1075c6fd2807SJeff Garzik 
1076c6fd2807SJeff Garzik /**
1077c6fd2807SJeff Garzik  *	ata_eh_analyze_ncq_error - analyze NCQ error
1078c6fd2807SJeff Garzik  *	@ap: ATA port to analyze NCQ error for
1079c6fd2807SJeff Garzik  *
1080c6fd2807SJeff Garzik  *	Read log page 10h, determine the offending qc and acquire
1081c6fd2807SJeff Garzik  *	error status TF.  For NCQ device errors, all LLDDs have to do
1082c6fd2807SJeff Garzik  *	is setting AC_ERR_DEV in ehi->err_mask.  This function takes
1083c6fd2807SJeff Garzik  *	care of the rest.
1084c6fd2807SJeff Garzik  *
1085c6fd2807SJeff Garzik  *	LOCKING:
1086c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1087c6fd2807SJeff Garzik  */
1088c6fd2807SJeff Garzik static void ata_eh_analyze_ncq_error(struct ata_port *ap)
1089c6fd2807SJeff Garzik {
1090c6fd2807SJeff Garzik 	struct ata_eh_context *ehc = &ap->eh_context;
1091c6fd2807SJeff Garzik 	struct ata_device *dev = ap->device;
1092c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
1093c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1094c6fd2807SJeff Garzik 	int tag, rc;
1095c6fd2807SJeff Garzik 
1096c6fd2807SJeff Garzik 	/* if frozen, we can't do much */
1097c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN)
1098c6fd2807SJeff Garzik 		return;
1099c6fd2807SJeff Garzik 
1100c6fd2807SJeff Garzik 	/* is it NCQ device error? */
1101c6fd2807SJeff Garzik 	if (!ap->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1102c6fd2807SJeff Garzik 		return;
1103c6fd2807SJeff Garzik 
1104c6fd2807SJeff Garzik 	/* has LLDD analyzed already? */
1105c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1106c6fd2807SJeff Garzik 		qc = __ata_qc_from_tag(ap, tag);
1107c6fd2807SJeff Garzik 
1108c6fd2807SJeff Garzik 		if (!(qc->flags & ATA_QCFLAG_FAILED))
1109c6fd2807SJeff Garzik 			continue;
1110c6fd2807SJeff Garzik 
1111c6fd2807SJeff Garzik 		if (qc->err_mask)
1112c6fd2807SJeff Garzik 			return;
1113c6fd2807SJeff Garzik 	}
1114c6fd2807SJeff Garzik 
1115c6fd2807SJeff Garzik 	/* okay, this error is ours */
1116c6fd2807SJeff Garzik 	rc = ata_eh_read_log_10h(dev, &tag, &tf);
1117c6fd2807SJeff Garzik 	if (rc) {
1118c6fd2807SJeff Garzik 		ata_port_printk(ap, KERN_ERR, "failed to read log page 10h "
1119c6fd2807SJeff Garzik 				"(errno=%d)\n", rc);
1120c6fd2807SJeff Garzik 		return;
1121c6fd2807SJeff Garzik 	}
1122c6fd2807SJeff Garzik 
1123c6fd2807SJeff Garzik 	if (!(ap->sactive & (1 << tag))) {
1124c6fd2807SJeff Garzik 		ata_port_printk(ap, KERN_ERR, "log page 10h reported "
1125c6fd2807SJeff Garzik 				"inactive tag %d\n", tag);
1126c6fd2807SJeff Garzik 		return;
1127c6fd2807SJeff Garzik 	}
1128c6fd2807SJeff Garzik 
1129c6fd2807SJeff Garzik 	/* we've got the perpetrator, condemn it */
1130c6fd2807SJeff Garzik 	qc = __ata_qc_from_tag(ap, tag);
1131c6fd2807SJeff Garzik 	memcpy(&qc->result_tf, &tf, sizeof(tf));
1132c6fd2807SJeff Garzik 	qc->err_mask |= AC_ERR_DEV;
1133c6fd2807SJeff Garzik 	ehc->i.err_mask &= ~AC_ERR_DEV;
1134c6fd2807SJeff Garzik }
1135c6fd2807SJeff Garzik 
1136c6fd2807SJeff Garzik /**
1137c6fd2807SJeff Garzik  *	ata_eh_analyze_tf - analyze taskfile of a failed qc
1138c6fd2807SJeff Garzik  *	@qc: qc to analyze
1139c6fd2807SJeff Garzik  *	@tf: Taskfile registers to analyze
1140c6fd2807SJeff Garzik  *
1141c6fd2807SJeff Garzik  *	Analyze taskfile of @qc and further determine cause of
1142c6fd2807SJeff Garzik  *	failure.  This function also requests ATAPI sense data if
1143c6fd2807SJeff Garzik  *	avaliable.
1144c6fd2807SJeff Garzik  *
1145c6fd2807SJeff Garzik  *	LOCKING:
1146c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1147c6fd2807SJeff Garzik  *
1148c6fd2807SJeff Garzik  *	RETURNS:
1149c6fd2807SJeff Garzik  *	Determined recovery action
1150c6fd2807SJeff Garzik  */
1151c6fd2807SJeff Garzik static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1152c6fd2807SJeff Garzik 				      const struct ata_taskfile *tf)
1153c6fd2807SJeff Garzik {
1154c6fd2807SJeff Garzik 	unsigned int tmp, action = 0;
1155c6fd2807SJeff Garzik 	u8 stat = tf->command, err = tf->feature;
1156c6fd2807SJeff Garzik 
1157c6fd2807SJeff Garzik 	if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1158c6fd2807SJeff Garzik 		qc->err_mask |= AC_ERR_HSM;
1159c6fd2807SJeff Garzik 		return ATA_EH_SOFTRESET;
1160c6fd2807SJeff Garzik 	}
1161c6fd2807SJeff Garzik 
1162a51d644aSTejun Heo 	if (stat & (ATA_ERR | ATA_DF))
1163a51d644aSTejun Heo 		qc->err_mask |= AC_ERR_DEV;
1164a51d644aSTejun Heo 	else
1165c6fd2807SJeff Garzik 		return 0;
1166c6fd2807SJeff Garzik 
1167c6fd2807SJeff Garzik 	switch (qc->dev->class) {
1168c6fd2807SJeff Garzik 	case ATA_DEV_ATA:
1169c6fd2807SJeff Garzik 		if (err & ATA_ICRC)
1170c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_ATA_BUS;
1171c6fd2807SJeff Garzik 		if (err & ATA_UNC)
1172c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_MEDIA;
1173c6fd2807SJeff Garzik 		if (err & ATA_IDNF)
1174c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_INVALID;
1175c6fd2807SJeff Garzik 		break;
1176c6fd2807SJeff Garzik 
1177c6fd2807SJeff Garzik 	case ATA_DEV_ATAPI:
1178a569a30dSTejun Heo 		if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
117956287768SAlbert Lee 			tmp = atapi_eh_request_sense(qc);
1180c6fd2807SJeff Garzik 			if (!tmp) {
1181a569a30dSTejun Heo 				/* ATA_QCFLAG_SENSE_VALID is used to
1182a569a30dSTejun Heo 				 * tell atapi_qc_complete() that sense
1183a569a30dSTejun Heo 				 * data is already valid.
1184c6fd2807SJeff Garzik 				 *
1185c6fd2807SJeff Garzik 				 * TODO: interpret sense data and set
1186c6fd2807SJeff Garzik 				 * appropriate err_mask.
1187c6fd2807SJeff Garzik 				 */
1188c6fd2807SJeff Garzik 				qc->flags |= ATA_QCFLAG_SENSE_VALID;
1189c6fd2807SJeff Garzik 			} else
1190c6fd2807SJeff Garzik 				qc->err_mask |= tmp;
1191c6fd2807SJeff Garzik 		}
1192a569a30dSTejun Heo 	}
1193c6fd2807SJeff Garzik 
1194c6fd2807SJeff Garzik 	if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1195c6fd2807SJeff Garzik 		action |= ATA_EH_SOFTRESET;
1196c6fd2807SJeff Garzik 
1197c6fd2807SJeff Garzik 	return action;
1198c6fd2807SJeff Garzik }
1199c6fd2807SJeff Garzik 
12007d47e8d4STejun Heo static int ata_eh_categorize_error(int is_io, unsigned int err_mask)
1201c6fd2807SJeff Garzik {
12027d47e8d4STejun Heo 	if (err_mask & AC_ERR_ATA_BUS)
1203c6fd2807SJeff Garzik 		return 1;
1204c6fd2807SJeff Garzik 
12057d47e8d4STejun Heo 	if (err_mask & AC_ERR_TIMEOUT)
1206c6fd2807SJeff Garzik 		return 2;
12077d47e8d4STejun Heo 
12087d47e8d4STejun Heo 	if (is_io) {
12097d47e8d4STejun Heo 		if (err_mask & AC_ERR_HSM)
12107d47e8d4STejun Heo 			return 2;
12117d47e8d4STejun Heo 		if ((err_mask &
12127d47e8d4STejun Heo 		     (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
12137d47e8d4STejun Heo 			return 3;
1214c6fd2807SJeff Garzik 	}
1215c6fd2807SJeff Garzik 
1216c6fd2807SJeff Garzik 	return 0;
1217c6fd2807SJeff Garzik }
1218c6fd2807SJeff Garzik 
12197d47e8d4STejun Heo struct speed_down_verdict_arg {
1220c6fd2807SJeff Garzik 	u64 since;
12217d47e8d4STejun Heo 	int nr_errors[4];
1222c6fd2807SJeff Garzik };
1223c6fd2807SJeff Garzik 
12247d47e8d4STejun Heo static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
1225c6fd2807SJeff Garzik {
12267d47e8d4STejun Heo 	struct speed_down_verdict_arg *arg = void_arg;
12277d47e8d4STejun Heo 	int cat = ata_eh_categorize_error(ent->is_io, ent->err_mask);
1228c6fd2807SJeff Garzik 
1229c6fd2807SJeff Garzik 	if (ent->timestamp < arg->since)
1230c6fd2807SJeff Garzik 		return -1;
1231c6fd2807SJeff Garzik 
12327d47e8d4STejun Heo 	arg->nr_errors[cat]++;
1233c6fd2807SJeff Garzik 	return 0;
1234c6fd2807SJeff Garzik }
1235c6fd2807SJeff Garzik 
1236c6fd2807SJeff Garzik /**
12377d47e8d4STejun Heo  *	ata_eh_speed_down_verdict - Determine speed down verdict
1238c6fd2807SJeff Garzik  *	@dev: Device of interest
1239c6fd2807SJeff Garzik  *
1240c6fd2807SJeff Garzik  *	This function examines error ring of @dev and determines
12417d47e8d4STejun Heo  *	whether NCQ needs to be turned off, transfer speed should be
12427d47e8d4STejun Heo  *	stepped down, or falling back to PIO is necessary.
1243c6fd2807SJeff Garzik  *
12447d47e8d4STejun Heo  *	Cat-1 is ATA_BUS error for any command.
1245c6fd2807SJeff Garzik  *
12467d47e8d4STejun Heo  *	Cat-2 is TIMEOUT for any command or HSM violation for known
12477d47e8d4STejun Heo  *	supported commands.
12487d47e8d4STejun Heo  *
12497d47e8d4STejun Heo  *	Cat-3 is is unclassified DEV error for known supported
1250c6fd2807SJeff Garzik  *	command.
1251c6fd2807SJeff Garzik  *
12527d47e8d4STejun Heo  *	NCQ needs to be turned off if there have been more than 3
12537d47e8d4STejun Heo  *	Cat-2 + Cat-3 errors during last 10 minutes.
12547d47e8d4STejun Heo  *
12557d47e8d4STejun Heo  *	Speed down is necessary if there have been more than 3 Cat-1 +
12567d47e8d4STejun Heo  *	Cat-2 errors or 10 Cat-3 errors during last 10 minutes.
12577d47e8d4STejun Heo  *
12587d47e8d4STejun Heo  *	Falling back to PIO mode is necessary if there have been more
12597d47e8d4STejun Heo  *	than 10 Cat-1 + Cat-2 + Cat-3 errors during last 5 minutes.
12607d47e8d4STejun Heo  *
1261c6fd2807SJeff Garzik  *	LOCKING:
1262c6fd2807SJeff Garzik  *	Inherited from caller.
1263c6fd2807SJeff Garzik  *
1264c6fd2807SJeff Garzik  *	RETURNS:
12657d47e8d4STejun Heo  *	OR of ATA_EH_SPDN_* flags.
1266c6fd2807SJeff Garzik  */
12677d47e8d4STejun Heo static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
1268c6fd2807SJeff Garzik {
12697d47e8d4STejun Heo 	const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
12707d47e8d4STejun Heo 	u64 j64 = get_jiffies_64();
12717d47e8d4STejun Heo 	struct speed_down_verdict_arg arg;
12727d47e8d4STejun Heo 	unsigned int verdict = 0;
1273c6fd2807SJeff Garzik 
12747d47e8d4STejun Heo 	/* scan past 10 mins of error history */
1275c6fd2807SJeff Garzik 	memset(&arg, 0, sizeof(arg));
12767d47e8d4STejun Heo 	arg.since = j64 - min(j64, j10mins);
12777d47e8d4STejun Heo 	ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1278c6fd2807SJeff Garzik 
12797d47e8d4STejun Heo 	if (arg.nr_errors[2] + arg.nr_errors[3] > 3)
12807d47e8d4STejun Heo 		verdict |= ATA_EH_SPDN_NCQ_OFF;
12817d47e8d4STejun Heo 	if (arg.nr_errors[1] + arg.nr_errors[2] > 3 || arg.nr_errors[3] > 10)
12827d47e8d4STejun Heo 		verdict |= ATA_EH_SPDN_SPEED_DOWN;
1283c6fd2807SJeff Garzik 
12847d47e8d4STejun Heo 	/* scan past 3 mins of error history */
12857d47e8d4STejun Heo 	memset(&arg, 0, sizeof(arg));
12867d47e8d4STejun Heo 	arg.since = j64 - min(j64, j5mins);
12877d47e8d4STejun Heo 	ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1288c6fd2807SJeff Garzik 
12897d47e8d4STejun Heo 	if (arg.nr_errors[1] + arg.nr_errors[2] + arg.nr_errors[3] > 10)
12907d47e8d4STejun Heo 		verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
12917d47e8d4STejun Heo 
12927d47e8d4STejun Heo 	return verdict;
1293c6fd2807SJeff Garzik }
1294c6fd2807SJeff Garzik 
1295c6fd2807SJeff Garzik /**
1296c6fd2807SJeff Garzik  *	ata_eh_speed_down - record error and speed down if necessary
1297c6fd2807SJeff Garzik  *	@dev: Failed device
1298c6fd2807SJeff Garzik  *	@is_io: Did the device fail during normal IO?
1299c6fd2807SJeff Garzik  *	@err_mask: err_mask of the error
1300c6fd2807SJeff Garzik  *
1301c6fd2807SJeff Garzik  *	Record error and examine error history to determine whether
1302c6fd2807SJeff Garzik  *	adjusting transmission speed is necessary.  It also sets
1303c6fd2807SJeff Garzik  *	transmission limits appropriately if such adjustment is
1304c6fd2807SJeff Garzik  *	necessary.
1305c6fd2807SJeff Garzik  *
1306c6fd2807SJeff Garzik  *	LOCKING:
1307c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1308c6fd2807SJeff Garzik  *
1309c6fd2807SJeff Garzik  *	RETURNS:
13107d47e8d4STejun Heo  *	Determined recovery action.
1311c6fd2807SJeff Garzik  */
13127d47e8d4STejun Heo static unsigned int ata_eh_speed_down(struct ata_device *dev, int is_io,
1313c6fd2807SJeff Garzik 				      unsigned int err_mask)
1314c6fd2807SJeff Garzik {
13157d47e8d4STejun Heo 	unsigned int verdict;
13167d47e8d4STejun Heo 	unsigned int action = 0;
13177d47e8d4STejun Heo 
13187d47e8d4STejun Heo 	/* don't bother if Cat-0 error */
13197d47e8d4STejun Heo 	if (ata_eh_categorize_error(is_io, err_mask) == 0)
1320c6fd2807SJeff Garzik 		return 0;
1321c6fd2807SJeff Garzik 
1322c6fd2807SJeff Garzik 	/* record error and determine whether speed down is necessary */
1323c6fd2807SJeff Garzik 	ata_ering_record(&dev->ering, is_io, err_mask);
13247d47e8d4STejun Heo 	verdict = ata_eh_speed_down_verdict(dev);
1325c6fd2807SJeff Garzik 
13267d47e8d4STejun Heo 	/* turn off NCQ? */
13277d47e8d4STejun Heo 	if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
13287d47e8d4STejun Heo 	    (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
13297d47e8d4STejun Heo 			   ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
13307d47e8d4STejun Heo 		dev->flags |= ATA_DFLAG_NCQ_OFF;
13317d47e8d4STejun Heo 		ata_dev_printk(dev, KERN_WARNING,
13327d47e8d4STejun Heo 			       "NCQ disabled due to excessive errors\n");
13337d47e8d4STejun Heo 		goto done;
13347d47e8d4STejun Heo 	}
1335c6fd2807SJeff Garzik 
13367d47e8d4STejun Heo 	/* speed down? */
13377d47e8d4STejun Heo 	if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1338c6fd2807SJeff Garzik 		/* speed down SATA link speed if possible */
13397d47e8d4STejun Heo 		if (sata_down_spd_limit(dev->ap) == 0) {
13407d47e8d4STejun Heo 			action |= ATA_EH_HARDRESET;
13417d47e8d4STejun Heo 			goto done;
13427d47e8d4STejun Heo 		}
1343c6fd2807SJeff Garzik 
1344c6fd2807SJeff Garzik 		/* lower transfer mode */
13457d47e8d4STejun Heo 		if (dev->spdn_cnt < 2) {
13467d47e8d4STejun Heo 			static const int dma_dnxfer_sel[] =
13477d47e8d4STejun Heo 				{ ATA_DNXFER_DMA, ATA_DNXFER_40C };
13487d47e8d4STejun Heo 			static const int pio_dnxfer_sel[] =
13497d47e8d4STejun Heo 				{ ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
13507d47e8d4STejun Heo 			int sel;
1351c6fd2807SJeff Garzik 
13527d47e8d4STejun Heo 			if (dev->xfer_shift != ATA_SHIFT_PIO)
13537d47e8d4STejun Heo 				sel = dma_dnxfer_sel[dev->spdn_cnt];
13547d47e8d4STejun Heo 			else
13557d47e8d4STejun Heo 				sel = pio_dnxfer_sel[dev->spdn_cnt];
13567d47e8d4STejun Heo 
13577d47e8d4STejun Heo 			dev->spdn_cnt++;
13587d47e8d4STejun Heo 
13597d47e8d4STejun Heo 			if (ata_down_xfermask_limit(dev, sel) == 0) {
13607d47e8d4STejun Heo 				action |= ATA_EH_SOFTRESET;
13617d47e8d4STejun Heo 				goto done;
13627d47e8d4STejun Heo 			}
13637d47e8d4STejun Heo 		}
13647d47e8d4STejun Heo 	}
13657d47e8d4STejun Heo 
13667d47e8d4STejun Heo 	/* Fall back to PIO?  Slowing down to PIO is meaningless for
13677d47e8d4STejun Heo 	 * SATA.  Consider it only for PATA.
13687d47e8d4STejun Heo 	 */
13697d47e8d4STejun Heo 	if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
13707d47e8d4STejun Heo 	    (dev->ap->cbl != ATA_CBL_SATA) &&
13717d47e8d4STejun Heo 	    (dev->xfer_shift != ATA_SHIFT_PIO)) {
13727d47e8d4STejun Heo 		if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
13737d47e8d4STejun Heo 			dev->spdn_cnt = 0;
13747d47e8d4STejun Heo 			action |= ATA_EH_SOFTRESET;
13757d47e8d4STejun Heo 			goto done;
13767d47e8d4STejun Heo 		}
13777d47e8d4STejun Heo 	}
13787d47e8d4STejun Heo 
1379c6fd2807SJeff Garzik 	return 0;
13807d47e8d4STejun Heo  done:
13817d47e8d4STejun Heo 	/* device has been slowed down, blow error history */
13827d47e8d4STejun Heo 	ata_ering_clear(&dev->ering);
13837d47e8d4STejun Heo 	return action;
1384c6fd2807SJeff Garzik }
1385c6fd2807SJeff Garzik 
1386c6fd2807SJeff Garzik /**
1387c6fd2807SJeff Garzik  *	ata_eh_autopsy - analyze error and determine recovery action
1388c6fd2807SJeff Garzik  *	@ap: ATA port to perform autopsy on
1389c6fd2807SJeff Garzik  *
1390c6fd2807SJeff Garzik  *	Analyze why @ap failed and determine which recovery action is
1391c6fd2807SJeff Garzik  *	needed.  This function also sets more detailed AC_ERR_* values
1392c6fd2807SJeff Garzik  *	and fills sense data for ATAPI CHECK SENSE.
1393c6fd2807SJeff Garzik  *
1394c6fd2807SJeff Garzik  *	LOCKING:
1395c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1396c6fd2807SJeff Garzik  */
1397c6fd2807SJeff Garzik static void ata_eh_autopsy(struct ata_port *ap)
1398c6fd2807SJeff Garzik {
1399c6fd2807SJeff Garzik 	struct ata_eh_context *ehc = &ap->eh_context;
1400c6fd2807SJeff Garzik 	unsigned int all_err_mask = 0;
1401c6fd2807SJeff Garzik 	int tag, is_io = 0;
1402c6fd2807SJeff Garzik 	u32 serror;
1403c6fd2807SJeff Garzik 	int rc;
1404c6fd2807SJeff Garzik 
1405c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
1406c6fd2807SJeff Garzik 
1407c6fd2807SJeff Garzik 	if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1408c6fd2807SJeff Garzik 		return;
1409c6fd2807SJeff Garzik 
1410c6fd2807SJeff Garzik 	/* obtain and analyze SError */
1411c6fd2807SJeff Garzik 	rc = sata_scr_read(ap, SCR_ERROR, &serror);
1412c6fd2807SJeff Garzik 	if (rc == 0) {
1413c6fd2807SJeff Garzik 		ehc->i.serror |= serror;
1414c6fd2807SJeff Garzik 		ata_eh_analyze_serror(ap);
1415c6fd2807SJeff Garzik 	} else if (rc != -EOPNOTSUPP)
1416c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_HARDRESET;
1417c6fd2807SJeff Garzik 
1418c6fd2807SJeff Garzik 	/* analyze NCQ failure */
1419c6fd2807SJeff Garzik 	ata_eh_analyze_ncq_error(ap);
1420c6fd2807SJeff Garzik 
1421c6fd2807SJeff Garzik 	/* any real error trumps AC_ERR_OTHER */
1422c6fd2807SJeff Garzik 	if (ehc->i.err_mask & ~AC_ERR_OTHER)
1423c6fd2807SJeff Garzik 		ehc->i.err_mask &= ~AC_ERR_OTHER;
1424c6fd2807SJeff Garzik 
1425c6fd2807SJeff Garzik 	all_err_mask |= ehc->i.err_mask;
1426c6fd2807SJeff Garzik 
1427c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1428c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1429c6fd2807SJeff Garzik 
1430c6fd2807SJeff Garzik 		if (!(qc->flags & ATA_QCFLAG_FAILED))
1431c6fd2807SJeff Garzik 			continue;
1432c6fd2807SJeff Garzik 
1433c6fd2807SJeff Garzik 		/* inherit upper level err_mask */
1434c6fd2807SJeff Garzik 		qc->err_mask |= ehc->i.err_mask;
1435c6fd2807SJeff Garzik 
1436c6fd2807SJeff Garzik 		/* analyze TF */
1437c6fd2807SJeff Garzik 		ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1438c6fd2807SJeff Garzik 
1439c6fd2807SJeff Garzik 		/* DEV errors are probably spurious in case of ATA_BUS error */
1440c6fd2807SJeff Garzik 		if (qc->err_mask & AC_ERR_ATA_BUS)
1441c6fd2807SJeff Garzik 			qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1442c6fd2807SJeff Garzik 					  AC_ERR_INVALID);
1443c6fd2807SJeff Garzik 
1444c6fd2807SJeff Garzik 		/* any real error trumps unknown error */
1445c6fd2807SJeff Garzik 		if (qc->err_mask & ~AC_ERR_OTHER)
1446c6fd2807SJeff Garzik 			qc->err_mask &= ~AC_ERR_OTHER;
1447c6fd2807SJeff Garzik 
1448c6fd2807SJeff Garzik 		/* SENSE_VALID trumps dev/unknown error and revalidation */
1449c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1450c6fd2807SJeff Garzik 			qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1451c6fd2807SJeff Garzik 			ehc->i.action &= ~ATA_EH_REVALIDATE;
1452c6fd2807SJeff Garzik 		}
1453c6fd2807SJeff Garzik 
1454c6fd2807SJeff Garzik 		/* accumulate error info */
1455c6fd2807SJeff Garzik 		ehc->i.dev = qc->dev;
1456c6fd2807SJeff Garzik 		all_err_mask |= qc->err_mask;
1457c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_IO)
1458c6fd2807SJeff Garzik 			is_io = 1;
1459c6fd2807SJeff Garzik 	}
1460c6fd2807SJeff Garzik 
1461c6fd2807SJeff Garzik 	/* enforce default EH actions */
1462c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN ||
1463c6fd2807SJeff Garzik 	    all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
1464c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_SOFTRESET;
1465c6fd2807SJeff Garzik 	else if (all_err_mask)
1466c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_REVALIDATE;
1467c6fd2807SJeff Garzik 
1468c6fd2807SJeff Garzik 	/* if we have offending qcs and the associated failed device */
1469c6fd2807SJeff Garzik 	if (ehc->i.dev) {
1470c6fd2807SJeff Garzik 		/* speed down */
1471c6fd2807SJeff Garzik 		ehc->i.action |= ata_eh_speed_down(ehc->i.dev, is_io,
1472c6fd2807SJeff Garzik 						   all_err_mask);
1473c6fd2807SJeff Garzik 
1474c6fd2807SJeff Garzik 		/* perform per-dev EH action only on the offending device */
1475c6fd2807SJeff Garzik 		ehc->i.dev_action[ehc->i.dev->devno] |=
1476c6fd2807SJeff Garzik 			ehc->i.action & ATA_EH_PERDEV_MASK;
1477c6fd2807SJeff Garzik 		ehc->i.action &= ~ATA_EH_PERDEV_MASK;
1478c6fd2807SJeff Garzik 	}
1479c6fd2807SJeff Garzik 
1480c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
1481c6fd2807SJeff Garzik }
1482c6fd2807SJeff Garzik 
1483c6fd2807SJeff Garzik /**
1484c6fd2807SJeff Garzik  *	ata_eh_report - report error handling to user
1485c6fd2807SJeff Garzik  *	@ap: ATA port EH is going on
1486c6fd2807SJeff Garzik  *
1487c6fd2807SJeff Garzik  *	Report EH to user.
1488c6fd2807SJeff Garzik  *
1489c6fd2807SJeff Garzik  *	LOCKING:
1490c6fd2807SJeff Garzik  *	None.
1491c6fd2807SJeff Garzik  */
1492c6fd2807SJeff Garzik static void ata_eh_report(struct ata_port *ap)
1493c6fd2807SJeff Garzik {
1494c6fd2807SJeff Garzik 	struct ata_eh_context *ehc = &ap->eh_context;
1495c6fd2807SJeff Garzik 	const char *frozen, *desc;
1496c6fd2807SJeff Garzik 	int tag, nr_failed = 0;
1497c6fd2807SJeff Garzik 
1498c6fd2807SJeff Garzik 	desc = NULL;
1499c6fd2807SJeff Garzik 	if (ehc->i.desc[0] != '\0')
1500c6fd2807SJeff Garzik 		desc = ehc->i.desc;
1501c6fd2807SJeff Garzik 
1502c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1503c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1504c6fd2807SJeff Garzik 
1505c6fd2807SJeff Garzik 		if (!(qc->flags & ATA_QCFLAG_FAILED))
1506c6fd2807SJeff Garzik 			continue;
1507c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1508c6fd2807SJeff Garzik 			continue;
1509c6fd2807SJeff Garzik 
1510c6fd2807SJeff Garzik 		nr_failed++;
1511c6fd2807SJeff Garzik 	}
1512c6fd2807SJeff Garzik 
1513c6fd2807SJeff Garzik 	if (!nr_failed && !ehc->i.err_mask)
1514c6fd2807SJeff Garzik 		return;
1515c6fd2807SJeff Garzik 
1516c6fd2807SJeff Garzik 	frozen = "";
1517c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN)
1518c6fd2807SJeff Garzik 		frozen = " frozen";
1519c6fd2807SJeff Garzik 
1520c6fd2807SJeff Garzik 	if (ehc->i.dev) {
1521c6fd2807SJeff Garzik 		ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1522c6fd2807SJeff Garzik 			       "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1523c6fd2807SJeff Garzik 			       ehc->i.err_mask, ap->sactive, ehc->i.serror,
1524c6fd2807SJeff Garzik 			       ehc->i.action, frozen);
1525c6fd2807SJeff Garzik 		if (desc)
1526c6fd2807SJeff Garzik 			ata_dev_printk(ehc->i.dev, KERN_ERR, "(%s)\n", desc);
1527c6fd2807SJeff Garzik 	} else {
1528c6fd2807SJeff Garzik 		ata_port_printk(ap, KERN_ERR, "exception Emask 0x%x "
1529c6fd2807SJeff Garzik 				"SAct 0x%x SErr 0x%x action 0x%x%s\n",
1530c6fd2807SJeff Garzik 				ehc->i.err_mask, ap->sactive, ehc->i.serror,
1531c6fd2807SJeff Garzik 				ehc->i.action, frozen);
1532c6fd2807SJeff Garzik 		if (desc)
1533c6fd2807SJeff Garzik 			ata_port_printk(ap, KERN_ERR, "(%s)\n", desc);
1534c6fd2807SJeff Garzik 	}
1535c6fd2807SJeff Garzik 
1536c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
15378a937581STejun Heo 		static const char *dma_str[] = {
15388a937581STejun Heo 			[DMA_BIDIRECTIONAL]	= "bidi",
15398a937581STejun Heo 			[DMA_TO_DEVICE]		= "out",
15408a937581STejun Heo 			[DMA_FROM_DEVICE]	= "in",
15418a937581STejun Heo 			[DMA_NONE]		= "",
15428a937581STejun Heo 		};
1543c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
15448a937581STejun Heo 		struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
1545c6fd2807SJeff Garzik 
1546c6fd2807SJeff Garzik 		if (!(qc->flags & ATA_QCFLAG_FAILED) || !qc->err_mask)
1547c6fd2807SJeff Garzik 			continue;
1548c6fd2807SJeff Garzik 
15498a937581STejun Heo 		ata_dev_printk(qc->dev, KERN_ERR,
15508a937581STejun Heo 			"cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
1551664e8503STejun Heo 			"tag %d cdb 0x%x data %u %s\n         "
15528a937581STejun Heo 			"res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
15538a937581STejun Heo 			"Emask 0x%x (%s)\n",
15548a937581STejun Heo 			cmd->command, cmd->feature, cmd->nsect,
15558a937581STejun Heo 			cmd->lbal, cmd->lbam, cmd->lbah,
15568a937581STejun Heo 			cmd->hob_feature, cmd->hob_nsect,
15578a937581STejun Heo 			cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
1558726f0785STejun Heo 			cmd->device, qc->tag, qc->cdb[0], qc->nbytes,
1559664e8503STejun Heo 			dma_str[qc->dma_dir],
15608a937581STejun Heo 			res->command, res->feature, res->nsect,
15618a937581STejun Heo 			res->lbal, res->lbam, res->lbah,
15628a937581STejun Heo 			res->hob_feature, res->hob_nsect,
15638a937581STejun Heo 			res->hob_lbal, res->hob_lbam, res->hob_lbah,
15648a937581STejun Heo 			res->device, qc->err_mask, ata_err_string(qc->err_mask));
1565c6fd2807SJeff Garzik 	}
1566c6fd2807SJeff Garzik }
1567c6fd2807SJeff Garzik 
1568c6fd2807SJeff Garzik static int ata_do_reset(struct ata_port *ap, ata_reset_fn_t reset,
1569d4b2bab4STejun Heo 			unsigned int *classes, unsigned long deadline)
1570c6fd2807SJeff Garzik {
1571c6fd2807SJeff Garzik 	int i, rc;
1572c6fd2807SJeff Garzik 
1573c6fd2807SJeff Garzik 	for (i = 0; i < ATA_MAX_DEVICES; i++)
1574c6fd2807SJeff Garzik 		classes[i] = ATA_DEV_UNKNOWN;
1575c6fd2807SJeff Garzik 
1576d4b2bab4STejun Heo 	rc = reset(ap, classes, deadline);
1577c6fd2807SJeff Garzik 	if (rc)
1578c6fd2807SJeff Garzik 		return rc;
1579c6fd2807SJeff Garzik 
1580c6fd2807SJeff Garzik 	/* If any class isn't ATA_DEV_UNKNOWN, consider classification
1581c6fd2807SJeff Garzik 	 * is complete and convert all ATA_DEV_UNKNOWN to
1582c6fd2807SJeff Garzik 	 * ATA_DEV_NONE.
1583c6fd2807SJeff Garzik 	 */
1584c6fd2807SJeff Garzik 	for (i = 0; i < ATA_MAX_DEVICES; i++)
1585c6fd2807SJeff Garzik 		if (classes[i] != ATA_DEV_UNKNOWN)
1586c6fd2807SJeff Garzik 			break;
1587c6fd2807SJeff Garzik 
1588c6fd2807SJeff Garzik 	if (i < ATA_MAX_DEVICES)
1589c6fd2807SJeff Garzik 		for (i = 0; i < ATA_MAX_DEVICES; i++)
1590c6fd2807SJeff Garzik 			if (classes[i] == ATA_DEV_UNKNOWN)
1591c6fd2807SJeff Garzik 				classes[i] = ATA_DEV_NONE;
1592c6fd2807SJeff Garzik 
1593c6fd2807SJeff Garzik 	return 0;
1594c6fd2807SJeff Garzik }
1595c6fd2807SJeff Garzik 
1596c6fd2807SJeff Garzik static int ata_eh_followup_srst_needed(int rc, int classify,
1597c6fd2807SJeff Garzik 				       const unsigned int *classes)
1598c6fd2807SJeff Garzik {
1599c6fd2807SJeff Garzik 	if (rc == -EAGAIN)
1600c6fd2807SJeff Garzik 		return 1;
1601c6fd2807SJeff Garzik 	if (rc != 0)
1602c6fd2807SJeff Garzik 		return 0;
1603c6fd2807SJeff Garzik 	if (classify && classes[0] == ATA_DEV_UNKNOWN)
1604c6fd2807SJeff Garzik 		return 1;
1605c6fd2807SJeff Garzik 	return 0;
1606c6fd2807SJeff Garzik }
1607c6fd2807SJeff Garzik 
1608c6fd2807SJeff Garzik static int ata_eh_reset(struct ata_port *ap, int classify,
1609c6fd2807SJeff Garzik 			ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
1610c6fd2807SJeff Garzik 			ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
1611c6fd2807SJeff Garzik {
1612c6fd2807SJeff Garzik 	struct ata_eh_context *ehc = &ap->eh_context;
1613c6fd2807SJeff Garzik 	unsigned int *classes = ehc->classes;
1614c6fd2807SJeff Garzik 	int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
161531daabdaSTejun Heo 	int try = 0;
161631daabdaSTejun Heo 	unsigned long deadline;
1617c6fd2807SJeff Garzik 	unsigned int action;
1618c6fd2807SJeff Garzik 	ata_reset_fn_t reset;
1619*8b5bb2faSTejun Heo 	int i, rc;
1620c6fd2807SJeff Garzik 
1621c6fd2807SJeff Garzik 	/* about to reset */
1622c6fd2807SJeff Garzik 	ata_eh_about_to_do(ap, NULL, ehc->i.action & ATA_EH_RESET_MASK);
1623c6fd2807SJeff Garzik 
1624c6fd2807SJeff Garzik 	/* Determine which reset to use and record in ehc->i.action.
1625c6fd2807SJeff Garzik 	 * prereset() may examine and modify it.
1626c6fd2807SJeff Garzik 	 */
1627c6fd2807SJeff Garzik 	action = ehc->i.action;
1628c6fd2807SJeff Garzik 	ehc->i.action &= ~ATA_EH_RESET_MASK;
1629c6fd2807SJeff Garzik 	if (softreset && (!hardreset || (!sata_set_spd_needed(ap) &&
1630c6fd2807SJeff Garzik 					 !(action & ATA_EH_HARDRESET))))
1631c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_SOFTRESET;
1632c6fd2807SJeff Garzik 	else
1633c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_HARDRESET;
1634c6fd2807SJeff Garzik 
1635c6fd2807SJeff Garzik 	if (prereset) {
163631daabdaSTejun Heo 		rc = prereset(ap, jiffies + ATA_EH_PRERESET_TIMEOUT);
1637c6fd2807SJeff Garzik 		if (rc) {
1638c961922bSAlan Cox 			if (rc == -ENOENT) {
16394aa9ab67STejun Heo 				ata_port_printk(ap, KERN_DEBUG,
16404aa9ab67STejun Heo 						"port disabled. ignoring.\n");
1641c961922bSAlan Cox 				ap->eh_context.i.action &= ~ATA_EH_RESET_MASK;
16424aa9ab67STejun Heo 
16434aa9ab67STejun Heo 				for (i = 0; i < ATA_MAX_DEVICES; i++)
16444aa9ab67STejun Heo 					classes[i] = ATA_DEV_NONE;
16454aa9ab67STejun Heo 
16464aa9ab67STejun Heo 				rc = 0;
1647c961922bSAlan Cox 			} else
1648c6fd2807SJeff Garzik 				ata_port_printk(ap, KERN_ERR,
1649c6fd2807SJeff Garzik 					"prereset failed (errno=%d)\n", rc);
1650c6fd2807SJeff Garzik 			return rc;
1651c6fd2807SJeff Garzik 		}
1652c6fd2807SJeff Garzik 	}
1653c6fd2807SJeff Garzik 
1654c6fd2807SJeff Garzik 	/* prereset() might have modified ehc->i.action */
1655c6fd2807SJeff Garzik 	if (ehc->i.action & ATA_EH_HARDRESET)
1656c6fd2807SJeff Garzik 		reset = hardreset;
1657c6fd2807SJeff Garzik 	else if (ehc->i.action & ATA_EH_SOFTRESET)
1658c6fd2807SJeff Garzik 		reset = softreset;
1659c6fd2807SJeff Garzik 	else {
1660c6fd2807SJeff Garzik 		/* prereset told us not to reset, bang classes and return */
1661c6fd2807SJeff Garzik 		for (i = 0; i < ATA_MAX_DEVICES; i++)
1662c6fd2807SJeff Garzik 			classes[i] = ATA_DEV_NONE;
1663c6fd2807SJeff Garzik 		return 0;
1664c6fd2807SJeff Garzik 	}
1665c6fd2807SJeff Garzik 
1666c6fd2807SJeff Garzik 	/* did prereset() screw up?  if so, fix up to avoid oopsing */
1667c6fd2807SJeff Garzik 	if (!reset) {
1668c6fd2807SJeff Garzik 		if (softreset)
1669c6fd2807SJeff Garzik 			reset = softreset;
1670c6fd2807SJeff Garzik 		else
1671c6fd2807SJeff Garzik 			reset = hardreset;
1672c6fd2807SJeff Garzik 	}
1673c6fd2807SJeff Garzik 
1674c6fd2807SJeff Garzik  retry:
167531daabdaSTejun Heo 	deadline = jiffies + ata_eh_reset_timeouts[try++];
167631daabdaSTejun Heo 
1677c6fd2807SJeff Garzik 	/* shut up during boot probing */
1678c6fd2807SJeff Garzik 	if (verbose)
1679c6fd2807SJeff Garzik 		ata_port_printk(ap, KERN_INFO, "%s resetting port\n",
1680c6fd2807SJeff Garzik 				reset == softreset ? "soft" : "hard");
1681c6fd2807SJeff Garzik 
1682c6fd2807SJeff Garzik 	/* mark that this EH session started with reset */
16830d64a233STejun Heo 	if (reset == hardreset)
16840d64a233STejun Heo 		ehc->i.flags |= ATA_EHI_DID_HARDRESET;
16850d64a233STejun Heo 	else
16860d64a233STejun Heo 		ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
1687c6fd2807SJeff Garzik 
168831daabdaSTejun Heo 	rc = ata_do_reset(ap, reset, classes, deadline);
1689c6fd2807SJeff Garzik 
1690c6fd2807SJeff Garzik 	if (reset == hardreset &&
1691c6fd2807SJeff Garzik 	    ata_eh_followup_srst_needed(rc, classify, classes)) {
1692c6fd2807SJeff Garzik 		/* okay, let's do follow-up softreset */
1693c6fd2807SJeff Garzik 		reset = softreset;
1694c6fd2807SJeff Garzik 
1695c6fd2807SJeff Garzik 		if (!reset) {
1696c6fd2807SJeff Garzik 			ata_port_printk(ap, KERN_ERR,
1697c6fd2807SJeff Garzik 					"follow-up softreset required "
1698c6fd2807SJeff Garzik 					"but no softreset avaliable\n");
1699c6fd2807SJeff Garzik 			return -EINVAL;
1700c6fd2807SJeff Garzik 		}
1701c6fd2807SJeff Garzik 
1702c6fd2807SJeff Garzik 		ata_eh_about_to_do(ap, NULL, ATA_EH_RESET_MASK);
170331daabdaSTejun Heo 		rc = ata_do_reset(ap, reset, classes, deadline);
1704c6fd2807SJeff Garzik 
1705c6fd2807SJeff Garzik 		if (rc == 0 && classify &&
1706c6fd2807SJeff Garzik 		    classes[0] == ATA_DEV_UNKNOWN) {
1707c6fd2807SJeff Garzik 			ata_port_printk(ap, KERN_ERR,
1708c6fd2807SJeff Garzik 					"classification failed\n");
1709c6fd2807SJeff Garzik 			return -EINVAL;
1710c6fd2807SJeff Garzik 		}
1711c6fd2807SJeff Garzik 	}
1712c6fd2807SJeff Garzik 
171331daabdaSTejun Heo 	if (rc && try < ARRAY_SIZE(ata_eh_reset_timeouts)) {
171431daabdaSTejun Heo 		unsigned long now = jiffies;
1715c6fd2807SJeff Garzik 
171631daabdaSTejun Heo 		if (time_before(now, deadline)) {
171731daabdaSTejun Heo 			unsigned long delta = deadline - jiffies;
1718c6fd2807SJeff Garzik 
171931daabdaSTejun Heo 			ata_port_printk(ap, KERN_WARNING, "reset failed "
172031daabdaSTejun Heo 				"(errno=%d), retrying in %u secs\n",
172131daabdaSTejun Heo 				rc, (jiffies_to_msecs(delta) + 999) / 1000);
1722c6fd2807SJeff Garzik 
172331daabdaSTejun Heo 			schedule_timeout_uninterruptible(delta);
172431daabdaSTejun Heo 		}
172531daabdaSTejun Heo 
172631daabdaSTejun Heo 		if (reset == hardreset &&
172731daabdaSTejun Heo 		    try == ARRAY_SIZE(ata_eh_reset_timeouts) - 1)
1728c6fd2807SJeff Garzik 			sata_down_spd_limit(ap);
1729c6fd2807SJeff Garzik 		if (hardreset)
1730c6fd2807SJeff Garzik 			reset = hardreset;
1731c6fd2807SJeff Garzik 		goto retry;
1732c6fd2807SJeff Garzik 	}
1733c6fd2807SJeff Garzik 
1734c6fd2807SJeff Garzik 	if (rc == 0) {
1735c6fd2807SJeff Garzik 		/* After the reset, the device state is PIO 0 and the
1736c6fd2807SJeff Garzik 		 * controller state is undefined.  Record the mode.
1737c6fd2807SJeff Garzik 		 */
1738c6fd2807SJeff Garzik 		for (i = 0; i < ATA_MAX_DEVICES; i++)
1739c6fd2807SJeff Garzik 			ap->device[i].pio_mode = XFER_PIO_0;
1740c6fd2807SJeff Garzik 
1741c6fd2807SJeff Garzik 		if (postreset)
1742c6fd2807SJeff Garzik 			postreset(ap, classes);
1743c6fd2807SJeff Garzik 
1744c6fd2807SJeff Garzik 		/* reset successful, schedule revalidation */
1745c6fd2807SJeff Garzik 		ata_eh_done(ap, NULL, ehc->i.action & ATA_EH_RESET_MASK);
1746c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_REVALIDATE;
1747c6fd2807SJeff Garzik 	}
1748c6fd2807SJeff Garzik 
1749c6fd2807SJeff Garzik 	return rc;
1750c6fd2807SJeff Garzik }
1751c6fd2807SJeff Garzik 
1752c6fd2807SJeff Garzik static int ata_eh_revalidate_and_attach(struct ata_port *ap,
1753c6fd2807SJeff Garzik 					struct ata_device **r_failed_dev)
1754c6fd2807SJeff Garzik {
1755c6fd2807SJeff Garzik 	struct ata_eh_context *ehc = &ap->eh_context;
1756c6fd2807SJeff Garzik 	struct ata_device *dev;
17578c3c52a8STejun Heo 	unsigned int new_mask = 0;
1758c6fd2807SJeff Garzik 	unsigned long flags;
1759c6fd2807SJeff Garzik 	int i, rc = 0;
1760c6fd2807SJeff Garzik 
1761c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
1762c6fd2807SJeff Garzik 
17638c3c52a8STejun Heo 	/* For PATA drive side cable detection to work, IDENTIFY must
17648c3c52a8STejun Heo 	 * be done backwards such that PDIAG- is released by the slave
17658c3c52a8STejun Heo 	 * device before the master device is identified.
17668c3c52a8STejun Heo 	 */
17678c3c52a8STejun Heo 	for (i = ATA_MAX_DEVICES - 1; i >= 0; i--) {
1768bff04647STejun Heo 		unsigned int action, readid_flags = 0;
1769c6fd2807SJeff Garzik 
1770c6fd2807SJeff Garzik 		dev = &ap->device[i];
1771c6fd2807SJeff Garzik 		action = ata_eh_dev_action(dev);
1772c6fd2807SJeff Garzik 
1773bff04647STejun Heo 		if (ehc->i.flags & ATA_EHI_DID_RESET)
1774bff04647STejun Heo 			readid_flags |= ATA_READID_POSTRESET;
1775bff04647STejun Heo 
17769666f400STejun Heo 		if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
1777c6fd2807SJeff Garzik 			if (ata_port_offline(ap)) {
1778c6fd2807SJeff Garzik 				rc = -EIO;
17798c3c52a8STejun Heo 				goto err;
1780c6fd2807SJeff Garzik 			}
1781c6fd2807SJeff Garzik 
1782c6fd2807SJeff Garzik 			ata_eh_about_to_do(ap, dev, ATA_EH_REVALIDATE);
1783bff04647STejun Heo 			rc = ata_dev_revalidate(dev, readid_flags);
1784c6fd2807SJeff Garzik 			if (rc)
17858c3c52a8STejun Heo 				goto err;
1786c6fd2807SJeff Garzik 
1787c6fd2807SJeff Garzik 			ata_eh_done(ap, dev, ATA_EH_REVALIDATE);
1788c6fd2807SJeff Garzik 
1789baa1e78aSTejun Heo 			/* Configuration may have changed, reconfigure
1790baa1e78aSTejun Heo 			 * transfer mode.
1791baa1e78aSTejun Heo 			 */
1792baa1e78aSTejun Heo 			ehc->i.flags |= ATA_EHI_SETMODE;
1793baa1e78aSTejun Heo 
1794c6fd2807SJeff Garzik 			/* schedule the scsi_rescan_device() here */
1795c6fd2807SJeff Garzik 			queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
1796c6fd2807SJeff Garzik 		} else if (dev->class == ATA_DEV_UNKNOWN &&
1797c6fd2807SJeff Garzik 			   ehc->tries[dev->devno] &&
1798c6fd2807SJeff Garzik 			   ata_class_enabled(ehc->classes[dev->devno])) {
1799c6fd2807SJeff Garzik 			dev->class = ehc->classes[dev->devno];
1800c6fd2807SJeff Garzik 
1801bff04647STejun Heo 			rc = ata_dev_read_id(dev, &dev->class, readid_flags,
1802bff04647STejun Heo 					     dev->id);
18038c3c52a8STejun Heo 			switch (rc) {
18048c3c52a8STejun Heo 			case 0:
18058c3c52a8STejun Heo 				new_mask |= 1 << i;
18068c3c52a8STejun Heo 				break;
18078c3c52a8STejun Heo 			case -ENOENT:
180855a8e2c8STejun Heo 				/* IDENTIFY was issued to non-existent
180955a8e2c8STejun Heo 				 * device.  No need to reset.  Just
181055a8e2c8STejun Heo 				 * thaw and kill the device.
181155a8e2c8STejun Heo 				 */
181255a8e2c8STejun Heo 				ata_eh_thaw_port(ap);
181355a8e2c8STejun Heo 				dev->class = ATA_DEV_UNKNOWN;
1814c6fd2807SJeff Garzik 				break;
18158c3c52a8STejun Heo 			default:
18168c3c52a8STejun Heo 				dev->class = ATA_DEV_UNKNOWN;
18178c3c52a8STejun Heo 				goto err;
18188c3c52a8STejun Heo 			}
18198c3c52a8STejun Heo 		}
1820c6fd2807SJeff Garzik 	}
1821c6fd2807SJeff Garzik 
1822c1c4e8d5STejun Heo 	/* PDIAG- should have been released, ask cable type if post-reset */
1823c1c4e8d5STejun Heo 	if ((ehc->i.flags & ATA_EHI_DID_RESET) && ap->ops->cable_detect)
1824c1c4e8d5STejun Heo 		ap->cbl = ap->ops->cable_detect(ap);
1825c1c4e8d5STejun Heo 
18268c3c52a8STejun Heo 	/* Configure new devices forward such that user doesn't see
18278c3c52a8STejun Heo 	 * device detection messages backwards.
18288c3c52a8STejun Heo 	 */
18298c3c52a8STejun Heo 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
18308c3c52a8STejun Heo 		dev = &ap->device[i];
18318c3c52a8STejun Heo 
18328c3c52a8STejun Heo 		if (!(new_mask & (1 << i)))
18338c3c52a8STejun Heo 			continue;
18348c3c52a8STejun Heo 
18358c3c52a8STejun Heo 		ehc->i.flags |= ATA_EHI_PRINTINFO;
18368c3c52a8STejun Heo 		rc = ata_dev_configure(dev);
18378c3c52a8STejun Heo 		ehc->i.flags &= ~ATA_EHI_PRINTINFO;
18388c3c52a8STejun Heo 		if (rc)
18398c3c52a8STejun Heo 			goto err;
18408c3c52a8STejun Heo 
1841c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
1842c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
1843c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1844baa1e78aSTejun Heo 
184555a8e2c8STejun Heo 		/* new device discovered, configure xfermode */
1846baa1e78aSTejun Heo 		ehc->i.flags |= ATA_EHI_SETMODE;
1847c6fd2807SJeff Garzik 	}
1848c6fd2807SJeff Garzik 
18498c3c52a8STejun Heo 	return 0;
18508c3c52a8STejun Heo 
18518c3c52a8STejun Heo  err:
1852c6fd2807SJeff Garzik 	*r_failed_dev = dev;
18538c3c52a8STejun Heo 	DPRINTK("EXIT rc=%d\n", rc);
1854c6fd2807SJeff Garzik 	return rc;
1855c6fd2807SJeff Garzik }
1856c6fd2807SJeff Garzik 
1857c6fd2807SJeff Garzik static int ata_port_nr_enabled(struct ata_port *ap)
1858c6fd2807SJeff Garzik {
1859c6fd2807SJeff Garzik 	int i, cnt = 0;
1860c6fd2807SJeff Garzik 
1861c6fd2807SJeff Garzik 	for (i = 0; i < ATA_MAX_DEVICES; i++)
1862c6fd2807SJeff Garzik 		if (ata_dev_enabled(&ap->device[i]))
1863c6fd2807SJeff Garzik 			cnt++;
1864c6fd2807SJeff Garzik 	return cnt;
1865c6fd2807SJeff Garzik }
1866c6fd2807SJeff Garzik 
1867c6fd2807SJeff Garzik static int ata_port_nr_vacant(struct ata_port *ap)
1868c6fd2807SJeff Garzik {
1869c6fd2807SJeff Garzik 	int i, cnt = 0;
1870c6fd2807SJeff Garzik 
1871c6fd2807SJeff Garzik 	for (i = 0; i < ATA_MAX_DEVICES; i++)
1872c6fd2807SJeff Garzik 		if (ap->device[i].class == ATA_DEV_UNKNOWN)
1873c6fd2807SJeff Garzik 			cnt++;
1874c6fd2807SJeff Garzik 	return cnt;
1875c6fd2807SJeff Garzik }
1876c6fd2807SJeff Garzik 
1877c6fd2807SJeff Garzik static int ata_eh_skip_recovery(struct ata_port *ap)
1878c6fd2807SJeff Garzik {
1879c6fd2807SJeff Garzik 	struct ata_eh_context *ehc = &ap->eh_context;
1880c6fd2807SJeff Garzik 	int i;
1881c6fd2807SJeff Garzik 
1882c6fd2807SJeff Garzik 	/* thaw frozen port, resume link and recover failed devices */
1883c6fd2807SJeff Garzik 	if ((ap->pflags & ATA_PFLAG_FROZEN) ||
1884c6fd2807SJeff Garzik 	    (ehc->i.flags & ATA_EHI_RESUME_LINK) || ata_port_nr_enabled(ap))
1885c6fd2807SJeff Garzik 		return 0;
1886c6fd2807SJeff Garzik 
1887c6fd2807SJeff Garzik 	/* skip if class codes for all vacant slots are ATA_DEV_NONE */
1888c6fd2807SJeff Garzik 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
1889c6fd2807SJeff Garzik 		struct ata_device *dev = &ap->device[i];
1890c6fd2807SJeff Garzik 
1891c6fd2807SJeff Garzik 		if (dev->class == ATA_DEV_UNKNOWN &&
1892c6fd2807SJeff Garzik 		    ehc->classes[dev->devno] != ATA_DEV_NONE)
1893c6fd2807SJeff Garzik 			return 0;
1894c6fd2807SJeff Garzik 	}
1895c6fd2807SJeff Garzik 
1896c6fd2807SJeff Garzik 	return 1;
1897c6fd2807SJeff Garzik }
1898c6fd2807SJeff Garzik 
1899c6fd2807SJeff Garzik /**
1900c6fd2807SJeff Garzik  *	ata_eh_recover - recover host port after error
1901c6fd2807SJeff Garzik  *	@ap: host port to recover
1902c6fd2807SJeff Garzik  *	@prereset: prereset method (can be NULL)
1903c6fd2807SJeff Garzik  *	@softreset: softreset method (can be NULL)
1904c6fd2807SJeff Garzik  *	@hardreset: hardreset method (can be NULL)
1905c6fd2807SJeff Garzik  *	@postreset: postreset method (can be NULL)
1906c6fd2807SJeff Garzik  *
1907c6fd2807SJeff Garzik  *	This is the alpha and omega, eum and yang, heart and soul of
1908c6fd2807SJeff Garzik  *	libata exception handling.  On entry, actions required to
1909c6fd2807SJeff Garzik  *	recover the port and hotplug requests are recorded in
1910c6fd2807SJeff Garzik  *	eh_context.  This function executes all the operations with
1911c6fd2807SJeff Garzik  *	appropriate retrials and fallbacks to resurrect failed
1912c6fd2807SJeff Garzik  *	devices, detach goners and greet newcomers.
1913c6fd2807SJeff Garzik  *
1914c6fd2807SJeff Garzik  *	LOCKING:
1915c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1916c6fd2807SJeff Garzik  *
1917c6fd2807SJeff Garzik  *	RETURNS:
1918c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
1919c6fd2807SJeff Garzik  */
1920c6fd2807SJeff Garzik static int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
1921c6fd2807SJeff Garzik 			  ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
1922c6fd2807SJeff Garzik 			  ata_postreset_fn_t postreset)
1923c6fd2807SJeff Garzik {
1924c6fd2807SJeff Garzik 	struct ata_eh_context *ehc = &ap->eh_context;
1925c6fd2807SJeff Garzik 	struct ata_device *dev;
19264ae72a1eSTejun Heo 	int i, rc;
1927c6fd2807SJeff Garzik 
1928c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
1929c6fd2807SJeff Garzik 
1930c6fd2807SJeff Garzik 	/* prep for recovery */
1931c6fd2807SJeff Garzik 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
1932c6fd2807SJeff Garzik 		dev = &ap->device[i];
1933c6fd2807SJeff Garzik 
1934c6fd2807SJeff Garzik 		ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
1935c6fd2807SJeff Garzik 
193679a55b72STejun Heo 		/* collect port action mask recorded in dev actions */
193779a55b72STejun Heo 		ehc->i.action |= ehc->i.dev_action[i] & ~ATA_EH_PERDEV_MASK;
193879a55b72STejun Heo 		ehc->i.dev_action[i] &= ATA_EH_PERDEV_MASK;
193979a55b72STejun Heo 
1940c6fd2807SJeff Garzik 		/* process hotplug request */
1941c6fd2807SJeff Garzik 		if (dev->flags & ATA_DFLAG_DETACH)
1942c6fd2807SJeff Garzik 			ata_eh_detach_dev(dev);
1943c6fd2807SJeff Garzik 
1944c6fd2807SJeff Garzik 		if (!ata_dev_enabled(dev) &&
1945c6fd2807SJeff Garzik 		    ((ehc->i.probe_mask & (1 << dev->devno)) &&
1946c6fd2807SJeff Garzik 		     !(ehc->did_probe_mask & (1 << dev->devno)))) {
1947c6fd2807SJeff Garzik 			ata_eh_detach_dev(dev);
1948c6fd2807SJeff Garzik 			ata_dev_init(dev);
1949c6fd2807SJeff Garzik 			ehc->did_probe_mask |= (1 << dev->devno);
1950c6fd2807SJeff Garzik 			ehc->i.action |= ATA_EH_SOFTRESET;
1951c6fd2807SJeff Garzik 		}
1952c6fd2807SJeff Garzik 	}
1953c6fd2807SJeff Garzik 
1954c6fd2807SJeff Garzik  retry:
1955c6fd2807SJeff Garzik 	rc = 0;
1956c6fd2807SJeff Garzik 
1957c6fd2807SJeff Garzik 	/* if UNLOADING, finish immediately */
1958c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_UNLOADING)
1959c6fd2807SJeff Garzik 		goto out;
1960c6fd2807SJeff Garzik 
1961c6fd2807SJeff Garzik 	/* skip EH if possible. */
1962c6fd2807SJeff Garzik 	if (ata_eh_skip_recovery(ap))
1963c6fd2807SJeff Garzik 		ehc->i.action = 0;
1964c6fd2807SJeff Garzik 
1965c6fd2807SJeff Garzik 	for (i = 0; i < ATA_MAX_DEVICES; i++)
1966c6fd2807SJeff Garzik 		ehc->classes[i] = ATA_DEV_UNKNOWN;
1967c6fd2807SJeff Garzik 
1968c6fd2807SJeff Garzik 	/* reset */
1969c6fd2807SJeff Garzik 	if (ehc->i.action & ATA_EH_RESET_MASK) {
1970c6fd2807SJeff Garzik 		ata_eh_freeze_port(ap);
1971c6fd2807SJeff Garzik 
1972c6fd2807SJeff Garzik 		rc = ata_eh_reset(ap, ata_port_nr_vacant(ap), prereset,
1973c6fd2807SJeff Garzik 				  softreset, hardreset, postreset);
1974c6fd2807SJeff Garzik 		if (rc) {
1975c6fd2807SJeff Garzik 			ata_port_printk(ap, KERN_ERR,
1976c6fd2807SJeff Garzik 					"reset failed, giving up\n");
1977c6fd2807SJeff Garzik 			goto out;
1978c6fd2807SJeff Garzik 		}
1979c6fd2807SJeff Garzik 
1980c6fd2807SJeff Garzik 		ata_eh_thaw_port(ap);
1981c6fd2807SJeff Garzik 	}
1982c6fd2807SJeff Garzik 
1983c6fd2807SJeff Garzik 	/* revalidate existing devices and attach new ones */
1984c6fd2807SJeff Garzik 	rc = ata_eh_revalidate_and_attach(ap, &dev);
1985c6fd2807SJeff Garzik 	if (rc)
1986c6fd2807SJeff Garzik 		goto dev_fail;
1987c6fd2807SJeff Garzik 
1988baa1e78aSTejun Heo 	/* configure transfer mode if necessary */
1989baa1e78aSTejun Heo 	if (ehc->i.flags & ATA_EHI_SETMODE) {
1990c6fd2807SJeff Garzik 		rc = ata_set_mode(ap, &dev);
19914ae72a1eSTejun Heo 		if (rc)
1992c6fd2807SJeff Garzik 			goto dev_fail;
1993baa1e78aSTejun Heo 		ehc->i.flags &= ~ATA_EHI_SETMODE;
1994c6fd2807SJeff Garzik 	}
1995c6fd2807SJeff Garzik 
1996c6fd2807SJeff Garzik 	goto out;
1997c6fd2807SJeff Garzik 
1998c6fd2807SJeff Garzik  dev_fail:
19994ae72a1eSTejun Heo 	ehc->tries[dev->devno]--;
20004ae72a1eSTejun Heo 
2001c6fd2807SJeff Garzik 	switch (rc) {
20024ae72a1eSTejun Heo 	case -ENODEV:
20034ae72a1eSTejun Heo 		/* device missing or wrong IDENTIFY data, schedule probing */
20044ae72a1eSTejun Heo 		ehc->i.probe_mask |= (1 << dev->devno);
20058575b814STejun Heo 	case -EINVAL:
20064ae72a1eSTejun Heo 		/* give it just one more chance */
20074ae72a1eSTejun Heo 		ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
2008c6fd2807SJeff Garzik 	case -EIO:
20094ae72a1eSTejun Heo 		if (ehc->tries[dev->devno] == 1) {
20104ae72a1eSTejun Heo 			/* This is the last chance, better to slow
20114ae72a1eSTejun Heo 			 * down than lose it.
20124ae72a1eSTejun Heo 			 */
2013c6fd2807SJeff Garzik 			sata_down_spd_limit(ap);
20144ae72a1eSTejun Heo 			ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
20154ae72a1eSTejun Heo 		}
2016c6fd2807SJeff Garzik 	}
2017c6fd2807SJeff Garzik 
2018c6fd2807SJeff Garzik 	if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2019c6fd2807SJeff Garzik 		/* disable device if it has used up all its chances */
2020c6fd2807SJeff Garzik 		ata_dev_disable(dev);
2021c6fd2807SJeff Garzik 
2022c6fd2807SJeff Garzik 		/* detach if offline */
2023c6fd2807SJeff Garzik 		if (ata_port_offline(ap))
2024c6fd2807SJeff Garzik 			ata_eh_detach_dev(dev);
2025c6fd2807SJeff Garzik 
2026c6fd2807SJeff Garzik 		/* probe if requested */
2027c6fd2807SJeff Garzik 		if ((ehc->i.probe_mask & (1 << dev->devno)) &&
2028c6fd2807SJeff Garzik 		    !(ehc->did_probe_mask & (1 << dev->devno))) {
2029c6fd2807SJeff Garzik 			ata_eh_detach_dev(dev);
2030c6fd2807SJeff Garzik 			ata_dev_init(dev);
2031c6fd2807SJeff Garzik 
2032c6fd2807SJeff Garzik 			ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2033c6fd2807SJeff Garzik 			ehc->did_probe_mask |= (1 << dev->devno);
2034c6fd2807SJeff Garzik 			ehc->i.action |= ATA_EH_SOFTRESET;
2035c6fd2807SJeff Garzik 		}
2036c6fd2807SJeff Garzik 	} else {
2037c6fd2807SJeff Garzik 		/* soft didn't work?  be haaaaard */
2038c6fd2807SJeff Garzik 		if (ehc->i.flags & ATA_EHI_DID_RESET)
2039c6fd2807SJeff Garzik 			ehc->i.action |= ATA_EH_HARDRESET;
2040c6fd2807SJeff Garzik 		else
2041c6fd2807SJeff Garzik 			ehc->i.action |= ATA_EH_SOFTRESET;
2042c6fd2807SJeff Garzik 	}
2043c6fd2807SJeff Garzik 
2044c6fd2807SJeff Garzik 	if (ata_port_nr_enabled(ap)) {
2045c6fd2807SJeff Garzik 		ata_port_printk(ap, KERN_WARNING, "failed to recover some "
2046c6fd2807SJeff Garzik 				"devices, retrying in 5 secs\n");
2047c6fd2807SJeff Garzik 		ssleep(5);
2048c6fd2807SJeff Garzik 	} else {
2049c6fd2807SJeff Garzik 		/* no device left, repeat fast */
2050c6fd2807SJeff Garzik 		msleep(500);
2051c6fd2807SJeff Garzik 	}
2052c6fd2807SJeff Garzik 
2053c6fd2807SJeff Garzik 	goto retry;
2054c6fd2807SJeff Garzik 
2055c6fd2807SJeff Garzik  out:
2056c6fd2807SJeff Garzik 	if (rc) {
2057c6fd2807SJeff Garzik 		for (i = 0; i < ATA_MAX_DEVICES; i++)
2058c6fd2807SJeff Garzik 			ata_dev_disable(&ap->device[i]);
2059c6fd2807SJeff Garzik 	}
2060c6fd2807SJeff Garzik 
2061c6fd2807SJeff Garzik 	DPRINTK("EXIT, rc=%d\n", rc);
2062c6fd2807SJeff Garzik 	return rc;
2063c6fd2807SJeff Garzik }
2064c6fd2807SJeff Garzik 
2065c6fd2807SJeff Garzik /**
2066c6fd2807SJeff Garzik  *	ata_eh_finish - finish up EH
2067c6fd2807SJeff Garzik  *	@ap: host port to finish EH for
2068c6fd2807SJeff Garzik  *
2069c6fd2807SJeff Garzik  *	Recovery is complete.  Clean up EH states and retry or finish
2070c6fd2807SJeff Garzik  *	failed qcs.
2071c6fd2807SJeff Garzik  *
2072c6fd2807SJeff Garzik  *	LOCKING:
2073c6fd2807SJeff Garzik  *	None.
2074c6fd2807SJeff Garzik  */
2075c6fd2807SJeff Garzik static void ata_eh_finish(struct ata_port *ap)
2076c6fd2807SJeff Garzik {
2077c6fd2807SJeff Garzik 	int tag;
2078c6fd2807SJeff Garzik 
2079c6fd2807SJeff Garzik 	/* retry or finish qcs */
2080c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2081c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2082c6fd2807SJeff Garzik 
2083c6fd2807SJeff Garzik 		if (!(qc->flags & ATA_QCFLAG_FAILED))
2084c6fd2807SJeff Garzik 			continue;
2085c6fd2807SJeff Garzik 
2086c6fd2807SJeff Garzik 		if (qc->err_mask) {
2087c6fd2807SJeff Garzik 			/* FIXME: Once EH migration is complete,
2088c6fd2807SJeff Garzik 			 * generate sense data in this function,
2089c6fd2807SJeff Garzik 			 * considering both err_mask and tf.
2090c6fd2807SJeff Garzik 			 */
2091c6fd2807SJeff Garzik 			if (qc->err_mask & AC_ERR_INVALID)
2092c6fd2807SJeff Garzik 				ata_eh_qc_complete(qc);
2093c6fd2807SJeff Garzik 			else
2094c6fd2807SJeff Garzik 				ata_eh_qc_retry(qc);
2095c6fd2807SJeff Garzik 		} else {
2096c6fd2807SJeff Garzik 			if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2097c6fd2807SJeff Garzik 				ata_eh_qc_complete(qc);
2098c6fd2807SJeff Garzik 			} else {
2099c6fd2807SJeff Garzik 				/* feed zero TF to sense generation */
2100c6fd2807SJeff Garzik 				memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2101c6fd2807SJeff Garzik 				ata_eh_qc_retry(qc);
2102c6fd2807SJeff Garzik 			}
2103c6fd2807SJeff Garzik 		}
2104c6fd2807SJeff Garzik 	}
2105c6fd2807SJeff Garzik }
2106c6fd2807SJeff Garzik 
2107c6fd2807SJeff Garzik /**
2108c6fd2807SJeff Garzik  *	ata_do_eh - do standard error handling
2109c6fd2807SJeff Garzik  *	@ap: host port to handle error for
2110c6fd2807SJeff Garzik  *	@prereset: prereset method (can be NULL)
2111c6fd2807SJeff Garzik  *	@softreset: softreset method (can be NULL)
2112c6fd2807SJeff Garzik  *	@hardreset: hardreset method (can be NULL)
2113c6fd2807SJeff Garzik  *	@postreset: postreset method (can be NULL)
2114c6fd2807SJeff Garzik  *
2115c6fd2807SJeff Garzik  *	Perform standard error handling sequence.
2116c6fd2807SJeff Garzik  *
2117c6fd2807SJeff Garzik  *	LOCKING:
2118c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
2119c6fd2807SJeff Garzik  */
2120c6fd2807SJeff Garzik void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2121c6fd2807SJeff Garzik 	       ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2122c6fd2807SJeff Garzik 	       ata_postreset_fn_t postreset)
2123c6fd2807SJeff Garzik {
2124c6fd2807SJeff Garzik 	ata_eh_autopsy(ap);
2125c6fd2807SJeff Garzik 	ata_eh_report(ap);
2126c6fd2807SJeff Garzik 	ata_eh_recover(ap, prereset, softreset, hardreset, postreset);
2127c6fd2807SJeff Garzik 	ata_eh_finish(ap);
2128c6fd2807SJeff Garzik }
2129c6fd2807SJeff Garzik 
21306ffa01d8STejun Heo #ifdef CONFIG_PM
2131c6fd2807SJeff Garzik /**
2132c6fd2807SJeff Garzik  *	ata_eh_handle_port_suspend - perform port suspend operation
2133c6fd2807SJeff Garzik  *	@ap: port to suspend
2134c6fd2807SJeff Garzik  *
2135c6fd2807SJeff Garzik  *	Suspend @ap.
2136c6fd2807SJeff Garzik  *
2137c6fd2807SJeff Garzik  *	LOCKING:
2138c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
2139c6fd2807SJeff Garzik  */
2140c6fd2807SJeff Garzik static void ata_eh_handle_port_suspend(struct ata_port *ap)
2141c6fd2807SJeff Garzik {
2142c6fd2807SJeff Garzik 	unsigned long flags;
2143c6fd2807SJeff Garzik 	int rc = 0;
2144c6fd2807SJeff Garzik 
2145c6fd2807SJeff Garzik 	/* are we suspending? */
2146c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
2147c6fd2807SJeff Garzik 	if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2148c6fd2807SJeff Garzik 	    ap->pm_mesg.event == PM_EVENT_ON) {
2149c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
2150c6fd2807SJeff Garzik 		return;
2151c6fd2807SJeff Garzik 	}
2152c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
2153c6fd2807SJeff Garzik 
2154c6fd2807SJeff Garzik 	WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
2155c6fd2807SJeff Garzik 
2156c6fd2807SJeff Garzik 	/* suspend */
2157c6fd2807SJeff Garzik 	ata_eh_freeze_port(ap);
2158c6fd2807SJeff Garzik 
2159c6fd2807SJeff Garzik 	if (ap->ops->port_suspend)
2160c6fd2807SJeff Garzik 		rc = ap->ops->port_suspend(ap, ap->pm_mesg);
2161c6fd2807SJeff Garzik 
2162c6fd2807SJeff Garzik 	/* report result */
2163c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
2164c6fd2807SJeff Garzik 
2165c6fd2807SJeff Garzik 	ap->pflags &= ~ATA_PFLAG_PM_PENDING;
2166c6fd2807SJeff Garzik 	if (rc == 0)
2167c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_SUSPENDED;
2168c6fd2807SJeff Garzik 	else
2169c6fd2807SJeff Garzik 		ata_port_schedule_eh(ap);
2170c6fd2807SJeff Garzik 
2171c6fd2807SJeff Garzik 	if (ap->pm_result) {
2172c6fd2807SJeff Garzik 		*ap->pm_result = rc;
2173c6fd2807SJeff Garzik 		ap->pm_result = NULL;
2174c6fd2807SJeff Garzik 	}
2175c6fd2807SJeff Garzik 
2176c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
2177c6fd2807SJeff Garzik 
2178c6fd2807SJeff Garzik 	return;
2179c6fd2807SJeff Garzik }
2180c6fd2807SJeff Garzik 
2181c6fd2807SJeff Garzik /**
2182c6fd2807SJeff Garzik  *	ata_eh_handle_port_resume - perform port resume operation
2183c6fd2807SJeff Garzik  *	@ap: port to resume
2184c6fd2807SJeff Garzik  *
2185c6fd2807SJeff Garzik  *	Resume @ap.
2186c6fd2807SJeff Garzik  *
2187c6fd2807SJeff Garzik  *	LOCKING:
2188c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
2189c6fd2807SJeff Garzik  */
2190c6fd2807SJeff Garzik static void ata_eh_handle_port_resume(struct ata_port *ap)
2191c6fd2807SJeff Garzik {
2192c6fd2807SJeff Garzik 	unsigned long flags;
21939666f400STejun Heo 	int rc = 0;
2194c6fd2807SJeff Garzik 
2195c6fd2807SJeff Garzik 	/* are we resuming? */
2196c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
2197c6fd2807SJeff Garzik 	if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2198c6fd2807SJeff Garzik 	    ap->pm_mesg.event != PM_EVENT_ON) {
2199c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
2200c6fd2807SJeff Garzik 		return;
2201c6fd2807SJeff Garzik 	}
2202c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
2203c6fd2807SJeff Garzik 
22049666f400STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
2205c6fd2807SJeff Garzik 
2206c6fd2807SJeff Garzik 	if (ap->ops->port_resume)
2207c6fd2807SJeff Garzik 		rc = ap->ops->port_resume(ap);
2208c6fd2807SJeff Garzik 
22099666f400STejun Heo 	/* report result */
2210c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
2211c6fd2807SJeff Garzik 	ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
2212c6fd2807SJeff Garzik 	if (ap->pm_result) {
2213c6fd2807SJeff Garzik 		*ap->pm_result = rc;
2214c6fd2807SJeff Garzik 		ap->pm_result = NULL;
2215c6fd2807SJeff Garzik 	}
2216c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
2217c6fd2807SJeff Garzik }
22186ffa01d8STejun Heo #endif /* CONFIG_PM */
2219