xref: /openbmc/linux/drivers/ata/libata-eh.c (revision 9b1e2658faf3f3095a96558c333b333c0e29dbc0)
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,
595ddf24c5STejun Heo 	ATA_EH_FASTDRAIN_INTERVAL	= 3 * HZ,
6031daabdaSTejun Heo };
6131daabdaSTejun Heo 
6231daabdaSTejun Heo /* The following table determines how we sequence resets.  Each entry
6331daabdaSTejun Heo  * represents timeout for that try.  The first try can be soft or
6431daabdaSTejun Heo  * hardreset.  All others are hardreset if available.  In most cases
6531daabdaSTejun Heo  * the first reset w/ 10sec timeout should succeed.  Following entries
6631daabdaSTejun Heo  * are mostly for error handling, hotplug and retarded devices.
6731daabdaSTejun Heo  */
6831daabdaSTejun Heo static const unsigned long ata_eh_reset_timeouts[] = {
6931daabdaSTejun Heo 	10 * HZ,	/* most drives spin up by 10sec */
7031daabdaSTejun Heo 	10 * HZ,	/* > 99% working drives spin up before 20sec */
7131daabdaSTejun Heo 	35 * HZ,	/* give > 30 secs of idleness for retarded devices */
7231daabdaSTejun Heo 	5 * HZ,		/* and sweet one last chance */
7331daabdaSTejun Heo 	/* > 1 min has elapsed, give up */
7431daabdaSTejun Heo };
7531daabdaSTejun Heo 
76c6fd2807SJeff Garzik static void __ata_port_freeze(struct ata_port *ap);
77c6fd2807SJeff Garzik static void ata_eh_finish(struct ata_port *ap);
786ffa01d8STejun Heo #ifdef CONFIG_PM
79c6fd2807SJeff Garzik static void ata_eh_handle_port_suspend(struct ata_port *ap);
80c6fd2807SJeff Garzik static void ata_eh_handle_port_resume(struct ata_port *ap);
816ffa01d8STejun Heo #else /* CONFIG_PM */
826ffa01d8STejun Heo static void ata_eh_handle_port_suspend(struct ata_port *ap)
836ffa01d8STejun Heo { }
846ffa01d8STejun Heo 
856ffa01d8STejun Heo static void ata_eh_handle_port_resume(struct ata_port *ap)
866ffa01d8STejun Heo { }
876ffa01d8STejun Heo #endif /* CONFIG_PM */
88c6fd2807SJeff Garzik 
89b64bbc39STejun Heo static void __ata_ehi_pushv_desc(struct ata_eh_info *ehi, const char *fmt,
90b64bbc39STejun Heo 				 va_list args)
91b64bbc39STejun Heo {
92b64bbc39STejun Heo 	ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
93b64bbc39STejun Heo 				     ATA_EH_DESC_LEN - ehi->desc_len,
94b64bbc39STejun Heo 				     fmt, args);
95b64bbc39STejun Heo }
96b64bbc39STejun Heo 
97b64bbc39STejun Heo /**
98b64bbc39STejun Heo  *	__ata_ehi_push_desc - push error description without adding separator
99b64bbc39STejun Heo  *	@ehi: target EHI
100b64bbc39STejun Heo  *	@fmt: printf format string
101b64bbc39STejun Heo  *
102b64bbc39STejun Heo  *	Format string according to @fmt and append it to @ehi->desc.
103b64bbc39STejun Heo  *
104b64bbc39STejun Heo  *	LOCKING:
105b64bbc39STejun Heo  *	spin_lock_irqsave(host lock)
106b64bbc39STejun Heo  */
107b64bbc39STejun Heo void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
108b64bbc39STejun Heo {
109b64bbc39STejun Heo 	va_list args;
110b64bbc39STejun Heo 
111b64bbc39STejun Heo 	va_start(args, fmt);
112b64bbc39STejun Heo 	__ata_ehi_pushv_desc(ehi, fmt, args);
113b64bbc39STejun Heo 	va_end(args);
114b64bbc39STejun Heo }
115b64bbc39STejun Heo 
116b64bbc39STejun Heo /**
117b64bbc39STejun Heo  *	ata_ehi_push_desc - push error description with separator
118b64bbc39STejun Heo  *	@ehi: target EHI
119b64bbc39STejun Heo  *	@fmt: printf format string
120b64bbc39STejun Heo  *
121b64bbc39STejun Heo  *	Format string according to @fmt and append it to @ehi->desc.
122b64bbc39STejun Heo  *	If @ehi->desc is not empty, ", " is added in-between.
123b64bbc39STejun Heo  *
124b64bbc39STejun Heo  *	LOCKING:
125b64bbc39STejun Heo  *	spin_lock_irqsave(host lock)
126b64bbc39STejun Heo  */
127b64bbc39STejun Heo void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
128b64bbc39STejun Heo {
129b64bbc39STejun Heo 	va_list args;
130b64bbc39STejun Heo 
131b64bbc39STejun Heo 	if (ehi->desc_len)
132b64bbc39STejun Heo 		__ata_ehi_push_desc(ehi, ", ");
133b64bbc39STejun Heo 
134b64bbc39STejun Heo 	va_start(args, fmt);
135b64bbc39STejun Heo 	__ata_ehi_pushv_desc(ehi, fmt, args);
136b64bbc39STejun Heo 	va_end(args);
137b64bbc39STejun Heo }
138b64bbc39STejun Heo 
139b64bbc39STejun Heo /**
140b64bbc39STejun Heo  *	ata_ehi_clear_desc - clean error description
141b64bbc39STejun Heo  *	@ehi: target EHI
142b64bbc39STejun Heo  *
143b64bbc39STejun Heo  *	Clear @ehi->desc.
144b64bbc39STejun Heo  *
145b64bbc39STejun Heo  *	LOCKING:
146b64bbc39STejun Heo  *	spin_lock_irqsave(host lock)
147b64bbc39STejun Heo  */
148b64bbc39STejun Heo void ata_ehi_clear_desc(struct ata_eh_info *ehi)
149b64bbc39STejun Heo {
150b64bbc39STejun Heo 	ehi->desc[0] = '\0';
151b64bbc39STejun Heo 	ehi->desc_len = 0;
152b64bbc39STejun Heo }
153b64bbc39STejun Heo 
154c6fd2807SJeff Garzik static void ata_ering_record(struct ata_ering *ering, int is_io,
155c6fd2807SJeff Garzik 			     unsigned int err_mask)
156c6fd2807SJeff Garzik {
157c6fd2807SJeff Garzik 	struct ata_ering_entry *ent;
158c6fd2807SJeff Garzik 
159c6fd2807SJeff Garzik 	WARN_ON(!err_mask);
160c6fd2807SJeff Garzik 
161c6fd2807SJeff Garzik 	ering->cursor++;
162c6fd2807SJeff Garzik 	ering->cursor %= ATA_ERING_SIZE;
163c6fd2807SJeff Garzik 
164c6fd2807SJeff Garzik 	ent = &ering->ring[ering->cursor];
165c6fd2807SJeff Garzik 	ent->is_io = is_io;
166c6fd2807SJeff Garzik 	ent->err_mask = err_mask;
167c6fd2807SJeff Garzik 	ent->timestamp = get_jiffies_64();
168c6fd2807SJeff Garzik }
169c6fd2807SJeff Garzik 
1707d47e8d4STejun Heo static void ata_ering_clear(struct ata_ering *ering)
171c6fd2807SJeff Garzik {
1727d47e8d4STejun Heo 	memset(ering, 0, sizeof(*ering));
173c6fd2807SJeff Garzik }
174c6fd2807SJeff Garzik 
175c6fd2807SJeff Garzik static int ata_ering_map(struct ata_ering *ering,
176c6fd2807SJeff Garzik 			 int (*map_fn)(struct ata_ering_entry *, void *),
177c6fd2807SJeff Garzik 			 void *arg)
178c6fd2807SJeff Garzik {
179c6fd2807SJeff Garzik 	int idx, rc = 0;
180c6fd2807SJeff Garzik 	struct ata_ering_entry *ent;
181c6fd2807SJeff Garzik 
182c6fd2807SJeff Garzik 	idx = ering->cursor;
183c6fd2807SJeff Garzik 	do {
184c6fd2807SJeff Garzik 		ent = &ering->ring[idx];
185c6fd2807SJeff Garzik 		if (!ent->err_mask)
186c6fd2807SJeff Garzik 			break;
187c6fd2807SJeff Garzik 		rc = map_fn(ent, arg);
188c6fd2807SJeff Garzik 		if (rc)
189c6fd2807SJeff Garzik 			break;
190c6fd2807SJeff Garzik 		idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
191c6fd2807SJeff Garzik 	} while (idx != ering->cursor);
192c6fd2807SJeff Garzik 
193c6fd2807SJeff Garzik 	return rc;
194c6fd2807SJeff Garzik }
195c6fd2807SJeff Garzik 
196c6fd2807SJeff Garzik static unsigned int ata_eh_dev_action(struct ata_device *dev)
197c6fd2807SJeff Garzik {
1989af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
199c6fd2807SJeff Garzik 
200c6fd2807SJeff Garzik 	return ehc->i.action | ehc->i.dev_action[dev->devno];
201c6fd2807SJeff Garzik }
202c6fd2807SJeff Garzik 
203f58229f8STejun Heo static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
204c6fd2807SJeff Garzik 				struct ata_eh_info *ehi, unsigned int action)
205c6fd2807SJeff Garzik {
206f58229f8STejun Heo 	struct ata_device *tdev;
207c6fd2807SJeff Garzik 
208c6fd2807SJeff Garzik 	if (!dev) {
209c6fd2807SJeff Garzik 		ehi->action &= ~action;
210f58229f8STejun Heo 		ata_link_for_each_dev(tdev, link)
211f58229f8STejun Heo 			ehi->dev_action[tdev->devno] &= ~action;
212c6fd2807SJeff Garzik 	} else {
213c6fd2807SJeff Garzik 		/* doesn't make sense for port-wide EH actions */
214c6fd2807SJeff Garzik 		WARN_ON(!(action & ATA_EH_PERDEV_MASK));
215c6fd2807SJeff Garzik 
216c6fd2807SJeff Garzik 		/* break ehi->action into ehi->dev_action */
217c6fd2807SJeff Garzik 		if (ehi->action & action) {
218f58229f8STejun Heo 			ata_link_for_each_dev(tdev, link)
219f58229f8STejun Heo 				ehi->dev_action[tdev->devno] |=
220f58229f8STejun Heo 					ehi->action & action;
221c6fd2807SJeff Garzik 			ehi->action &= ~action;
222c6fd2807SJeff Garzik 		}
223c6fd2807SJeff Garzik 
224c6fd2807SJeff Garzik 		/* turn off the specified per-dev action */
225c6fd2807SJeff Garzik 		ehi->dev_action[dev->devno] &= ~action;
226c6fd2807SJeff Garzik 	}
227c6fd2807SJeff Garzik }
228c6fd2807SJeff Garzik 
229c6fd2807SJeff Garzik /**
230c6fd2807SJeff Garzik  *	ata_scsi_timed_out - SCSI layer time out callback
231c6fd2807SJeff Garzik  *	@cmd: timed out SCSI command
232c6fd2807SJeff Garzik  *
233c6fd2807SJeff Garzik  *	Handles SCSI layer timeout.  We race with normal completion of
234c6fd2807SJeff Garzik  *	the qc for @cmd.  If the qc is already gone, we lose and let
235c6fd2807SJeff Garzik  *	the scsi command finish (EH_HANDLED).  Otherwise, the qc has
236c6fd2807SJeff Garzik  *	timed out and EH should be invoked.  Prevent ata_qc_complete()
237c6fd2807SJeff Garzik  *	from finishing it by setting EH_SCHEDULED and return
238c6fd2807SJeff Garzik  *	EH_NOT_HANDLED.
239c6fd2807SJeff Garzik  *
240c6fd2807SJeff Garzik  *	TODO: kill this function once old EH is gone.
241c6fd2807SJeff Garzik  *
242c6fd2807SJeff Garzik  *	LOCKING:
243c6fd2807SJeff Garzik  *	Called from timer context
244c6fd2807SJeff Garzik  *
245c6fd2807SJeff Garzik  *	RETURNS:
246c6fd2807SJeff Garzik  *	EH_HANDLED or EH_NOT_HANDLED
247c6fd2807SJeff Garzik  */
248c6fd2807SJeff Garzik enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
249c6fd2807SJeff Garzik {
250c6fd2807SJeff Garzik 	struct Scsi_Host *host = cmd->device->host;
251c6fd2807SJeff Garzik 	struct ata_port *ap = ata_shost_to_port(host);
252c6fd2807SJeff Garzik 	unsigned long flags;
253c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
254c6fd2807SJeff Garzik 	enum scsi_eh_timer_return ret;
255c6fd2807SJeff Garzik 
256c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
257c6fd2807SJeff Garzik 
258c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
259c6fd2807SJeff Garzik 		ret = EH_NOT_HANDLED;
260c6fd2807SJeff Garzik 		goto out;
261c6fd2807SJeff Garzik 	}
262c6fd2807SJeff Garzik 
263c6fd2807SJeff Garzik 	ret = EH_HANDLED;
264c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
2659af5c9c9STejun Heo 	qc = ata_qc_from_tag(ap, ap->link.active_tag);
266c6fd2807SJeff Garzik 	if (qc) {
267c6fd2807SJeff Garzik 		WARN_ON(qc->scsicmd != cmd);
268c6fd2807SJeff Garzik 		qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
269c6fd2807SJeff Garzik 		qc->err_mask |= AC_ERR_TIMEOUT;
270c6fd2807SJeff Garzik 		ret = EH_NOT_HANDLED;
271c6fd2807SJeff Garzik 	}
272c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
273c6fd2807SJeff Garzik 
274c6fd2807SJeff Garzik  out:
275c6fd2807SJeff Garzik 	DPRINTK("EXIT, ret=%d\n", ret);
276c6fd2807SJeff Garzik 	return ret;
277c6fd2807SJeff Garzik }
278c6fd2807SJeff Garzik 
279c6fd2807SJeff Garzik /**
280c6fd2807SJeff Garzik  *	ata_scsi_error - SCSI layer error handler callback
281c6fd2807SJeff Garzik  *	@host: SCSI host on which error occurred
282c6fd2807SJeff Garzik  *
283c6fd2807SJeff Garzik  *	Handles SCSI-layer-thrown error events.
284c6fd2807SJeff Garzik  *
285c6fd2807SJeff Garzik  *	LOCKING:
286c6fd2807SJeff Garzik  *	Inherited from SCSI layer (none, can sleep)
287c6fd2807SJeff Garzik  *
288c6fd2807SJeff Garzik  *	RETURNS:
289c6fd2807SJeff Garzik  *	Zero.
290c6fd2807SJeff Garzik  */
291c6fd2807SJeff Garzik void ata_scsi_error(struct Scsi_Host *host)
292c6fd2807SJeff Garzik {
293c6fd2807SJeff Garzik 	struct ata_port *ap = ata_shost_to_port(host);
294c6fd2807SJeff Garzik 	int i, repeat_cnt = ATA_EH_MAX_REPEAT;
295c6fd2807SJeff Garzik 	unsigned long flags;
296c6fd2807SJeff Garzik 
297c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
298c6fd2807SJeff Garzik 
299c6fd2807SJeff Garzik 	/* synchronize with port task */
300c6fd2807SJeff Garzik 	ata_port_flush_task(ap);
301c6fd2807SJeff Garzik 
302cca3974eSJeff Garzik 	/* synchronize with host lock and sort out timeouts */
303c6fd2807SJeff Garzik 
304c6fd2807SJeff Garzik 	/* For new EH, all qcs are finished in one of three ways -
305c6fd2807SJeff Garzik 	 * normal completion, error completion, and SCSI timeout.
306c6fd2807SJeff Garzik 	 * Both cmpletions can race against SCSI timeout.  When normal
307c6fd2807SJeff Garzik 	 * completion wins, the qc never reaches EH.  When error
308c6fd2807SJeff Garzik 	 * completion wins, the qc has ATA_QCFLAG_FAILED set.
309c6fd2807SJeff Garzik 	 *
310c6fd2807SJeff Garzik 	 * When SCSI timeout wins, things are a bit more complex.
311c6fd2807SJeff Garzik 	 * Normal or error completion can occur after the timeout but
312c6fd2807SJeff Garzik 	 * before this point.  In such cases, both types of
313c6fd2807SJeff Garzik 	 * completions are honored.  A scmd is determined to have
314c6fd2807SJeff Garzik 	 * timed out iff its associated qc is active and not failed.
315c6fd2807SJeff Garzik 	 */
316c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
317c6fd2807SJeff Garzik 		struct scsi_cmnd *scmd, *tmp;
318c6fd2807SJeff Garzik 		int nr_timedout = 0;
319c6fd2807SJeff Garzik 
320c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
321c6fd2807SJeff Garzik 
322c6fd2807SJeff Garzik 		list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
323c6fd2807SJeff Garzik 			struct ata_queued_cmd *qc;
324c6fd2807SJeff Garzik 
325c6fd2807SJeff Garzik 			for (i = 0; i < ATA_MAX_QUEUE; i++) {
326c6fd2807SJeff Garzik 				qc = __ata_qc_from_tag(ap, i);
327c6fd2807SJeff Garzik 				if (qc->flags & ATA_QCFLAG_ACTIVE &&
328c6fd2807SJeff Garzik 				    qc->scsicmd == scmd)
329c6fd2807SJeff Garzik 					break;
330c6fd2807SJeff Garzik 			}
331c6fd2807SJeff Garzik 
332c6fd2807SJeff Garzik 			if (i < ATA_MAX_QUEUE) {
333c6fd2807SJeff Garzik 				/* the scmd has an associated qc */
334c6fd2807SJeff Garzik 				if (!(qc->flags & ATA_QCFLAG_FAILED)) {
335c6fd2807SJeff Garzik 					/* which hasn't failed yet, timeout */
336c6fd2807SJeff Garzik 					qc->err_mask |= AC_ERR_TIMEOUT;
337c6fd2807SJeff Garzik 					qc->flags |= ATA_QCFLAG_FAILED;
338c6fd2807SJeff Garzik 					nr_timedout++;
339c6fd2807SJeff Garzik 				}
340c6fd2807SJeff Garzik 			} else {
341c6fd2807SJeff Garzik 				/* Normal completion occurred after
342c6fd2807SJeff Garzik 				 * SCSI timeout but before this point.
343c6fd2807SJeff Garzik 				 * Successfully complete it.
344c6fd2807SJeff Garzik 				 */
345c6fd2807SJeff Garzik 				scmd->retries = scmd->allowed;
346c6fd2807SJeff Garzik 				scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
347c6fd2807SJeff Garzik 			}
348c6fd2807SJeff Garzik 		}
349c6fd2807SJeff Garzik 
350c6fd2807SJeff Garzik 		/* If we have timed out qcs.  They belong to EH from
351c6fd2807SJeff Garzik 		 * this point but the state of the controller is
352c6fd2807SJeff Garzik 		 * unknown.  Freeze the port to make sure the IRQ
353c6fd2807SJeff Garzik 		 * handler doesn't diddle with those qcs.  This must
354c6fd2807SJeff Garzik 		 * be done atomically w.r.t. setting QCFLAG_FAILED.
355c6fd2807SJeff Garzik 		 */
356c6fd2807SJeff Garzik 		if (nr_timedout)
357c6fd2807SJeff Garzik 			__ata_port_freeze(ap);
358c6fd2807SJeff Garzik 
359c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
360c6fd2807SJeff Garzik 	} else
361c6fd2807SJeff Garzik 		spin_unlock_wait(ap->lock);
362c6fd2807SJeff Garzik 
363c6fd2807SJeff Garzik  repeat:
364c6fd2807SJeff Garzik 	/* invoke error handler */
365c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
366cf1b86c8STejun Heo 		struct ata_link *link;
367cf1b86c8STejun Heo 
3685ddf24c5STejun Heo 		/* kill fast drain timer */
3695ddf24c5STejun Heo 		del_timer_sync(&ap->fastdrain_timer);
3705ddf24c5STejun Heo 
371c6fd2807SJeff Garzik 		/* process port resume request */
372c6fd2807SJeff Garzik 		ata_eh_handle_port_resume(ap);
373c6fd2807SJeff Garzik 
374c6fd2807SJeff Garzik 		/* fetch & clear EH info */
375c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
376c6fd2807SJeff Garzik 
377cf1b86c8STejun Heo 		__ata_port_for_each_link(link, ap) {
378cf1b86c8STejun Heo 			memset(&link->eh_context, 0, sizeof(link->eh_context));
379cf1b86c8STejun Heo 			link->eh_context.i = link->eh_info;
380cf1b86c8STejun Heo 			memset(&link->eh_info, 0, sizeof(link->eh_info));
381cf1b86c8STejun Heo 		}
382c6fd2807SJeff Garzik 
383c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
384c6fd2807SJeff Garzik 		ap->pflags &= ~ATA_PFLAG_EH_PENDING;
385c6fd2807SJeff Garzik 
386c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
387c6fd2807SJeff Garzik 
388c6fd2807SJeff Garzik 		/* invoke EH, skip if unloading or suspended */
389c6fd2807SJeff Garzik 		if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
390c6fd2807SJeff Garzik 			ap->ops->error_handler(ap);
391c6fd2807SJeff Garzik 		else
392c6fd2807SJeff Garzik 			ata_eh_finish(ap);
393c6fd2807SJeff Garzik 
394c6fd2807SJeff Garzik 		/* process port suspend request */
395c6fd2807SJeff Garzik 		ata_eh_handle_port_suspend(ap);
396c6fd2807SJeff Garzik 
397c6fd2807SJeff Garzik 		/* Exception might have happend after ->error_handler
398c6fd2807SJeff Garzik 		 * recovered the port but before this point.  Repeat
399c6fd2807SJeff Garzik 		 * EH in such case.
400c6fd2807SJeff Garzik 		 */
401c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
402c6fd2807SJeff Garzik 
403c6fd2807SJeff Garzik 		if (ap->pflags & ATA_PFLAG_EH_PENDING) {
404c6fd2807SJeff Garzik 			if (--repeat_cnt) {
405c6fd2807SJeff Garzik 				ata_port_printk(ap, KERN_INFO,
406c6fd2807SJeff Garzik 					"EH pending after completion, "
407c6fd2807SJeff Garzik 					"repeating EH (cnt=%d)\n", repeat_cnt);
408c6fd2807SJeff Garzik 				spin_unlock_irqrestore(ap->lock, flags);
409c6fd2807SJeff Garzik 				goto repeat;
410c6fd2807SJeff Garzik 			}
411c6fd2807SJeff Garzik 			ata_port_printk(ap, KERN_ERR, "EH pending after %d "
412c6fd2807SJeff Garzik 					"tries, giving up\n", ATA_EH_MAX_REPEAT);
413914616a3STejun Heo 			ap->pflags &= ~ATA_PFLAG_EH_PENDING;
414c6fd2807SJeff Garzik 		}
415c6fd2807SJeff Garzik 
416c6fd2807SJeff Garzik 		/* this run is complete, make sure EH info is clear */
417cf1b86c8STejun Heo 		__ata_port_for_each_link(link, ap)
418cf1b86c8STejun Heo 			memset(&link->eh_info, 0, sizeof(link->eh_info));
419c6fd2807SJeff Garzik 
420c6fd2807SJeff Garzik 		/* Clear host_eh_scheduled while holding ap->lock such
421c6fd2807SJeff Garzik 		 * that if exception occurs after this point but
422c6fd2807SJeff Garzik 		 * before EH completion, SCSI midlayer will
423c6fd2807SJeff Garzik 		 * re-initiate EH.
424c6fd2807SJeff Garzik 		 */
425c6fd2807SJeff Garzik 		host->host_eh_scheduled = 0;
426c6fd2807SJeff Garzik 
427c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
428c6fd2807SJeff Garzik 	} else {
4299af5c9c9STejun Heo 		WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
430c6fd2807SJeff Garzik 		ap->ops->eng_timeout(ap);
431c6fd2807SJeff Garzik 	}
432c6fd2807SJeff Garzik 
433c6fd2807SJeff Garzik 	/* finish or retry handled scmd's and clean up */
434c6fd2807SJeff Garzik 	WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
435c6fd2807SJeff Garzik 
436c6fd2807SJeff Garzik 	scsi_eh_flush_done_q(&ap->eh_done_q);
437c6fd2807SJeff Garzik 
438c6fd2807SJeff Garzik 	/* clean up */
439c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
440c6fd2807SJeff Garzik 
441c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_LOADING)
442c6fd2807SJeff Garzik 		ap->pflags &= ~ATA_PFLAG_LOADING;
443c6fd2807SJeff Garzik 	else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
44452bad64dSDavid Howells 		queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0);
445c6fd2807SJeff Garzik 
446c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_RECOVERED)
447c6fd2807SJeff Garzik 		ata_port_printk(ap, KERN_INFO, "EH complete\n");
448c6fd2807SJeff Garzik 
449c6fd2807SJeff Garzik 	ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
450c6fd2807SJeff Garzik 
451c6fd2807SJeff Garzik 	/* tell wait_eh that we're done */
452c6fd2807SJeff Garzik 	ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
453c6fd2807SJeff Garzik 	wake_up_all(&ap->eh_wait_q);
454c6fd2807SJeff Garzik 
455c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
456c6fd2807SJeff Garzik 
457c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
458c6fd2807SJeff Garzik }
459c6fd2807SJeff Garzik 
460c6fd2807SJeff Garzik /**
461c6fd2807SJeff Garzik  *	ata_port_wait_eh - Wait for the currently pending EH to complete
462c6fd2807SJeff Garzik  *	@ap: Port to wait EH for
463c6fd2807SJeff Garzik  *
464c6fd2807SJeff Garzik  *	Wait until the currently pending EH is complete.
465c6fd2807SJeff Garzik  *
466c6fd2807SJeff Garzik  *	LOCKING:
467c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
468c6fd2807SJeff Garzik  */
469c6fd2807SJeff Garzik void ata_port_wait_eh(struct ata_port *ap)
470c6fd2807SJeff Garzik {
471c6fd2807SJeff Garzik 	unsigned long flags;
472c6fd2807SJeff Garzik 	DEFINE_WAIT(wait);
473c6fd2807SJeff Garzik 
474c6fd2807SJeff Garzik  retry:
475c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
476c6fd2807SJeff Garzik 
477c6fd2807SJeff Garzik 	while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
478c6fd2807SJeff Garzik 		prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
479c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
480c6fd2807SJeff Garzik 		schedule();
481c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
482c6fd2807SJeff Garzik 	}
483c6fd2807SJeff Garzik 	finish_wait(&ap->eh_wait_q, &wait);
484c6fd2807SJeff Garzik 
485c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
486c6fd2807SJeff Garzik 
487c6fd2807SJeff Garzik 	/* make sure SCSI EH is complete */
488cca3974eSJeff Garzik 	if (scsi_host_in_recovery(ap->scsi_host)) {
489c6fd2807SJeff Garzik 		msleep(10);
490c6fd2807SJeff Garzik 		goto retry;
491c6fd2807SJeff Garzik 	}
492c6fd2807SJeff Garzik }
493c6fd2807SJeff Garzik 
494c6fd2807SJeff Garzik /**
495c6fd2807SJeff Garzik  *	ata_qc_timeout - Handle timeout of queued command
496c6fd2807SJeff Garzik  *	@qc: Command that timed out
497c6fd2807SJeff Garzik  *
498c6fd2807SJeff Garzik  *	Some part of the kernel (currently, only the SCSI layer)
499c6fd2807SJeff Garzik  *	has noticed that the active command on port @ap has not
500c6fd2807SJeff Garzik  *	completed after a specified length of time.  Handle this
501c6fd2807SJeff Garzik  *	condition by disabling DMA (if necessary) and completing
502c6fd2807SJeff Garzik  *	transactions, with error if necessary.
503c6fd2807SJeff Garzik  *
504c6fd2807SJeff Garzik  *	This also handles the case of the "lost interrupt", where
505c6fd2807SJeff Garzik  *	for some reason (possibly hardware bug, possibly driver bug)
506c6fd2807SJeff Garzik  *	an interrupt was not delivered to the driver, even though the
507c6fd2807SJeff Garzik  *	transaction completed successfully.
508c6fd2807SJeff Garzik  *
509c6fd2807SJeff Garzik  *	TODO: kill this function once old EH is gone.
510c6fd2807SJeff Garzik  *
511c6fd2807SJeff Garzik  *	LOCKING:
512c6fd2807SJeff Garzik  *	Inherited from SCSI layer (none, can sleep)
513c6fd2807SJeff Garzik  */
514c6fd2807SJeff Garzik static void ata_qc_timeout(struct ata_queued_cmd *qc)
515c6fd2807SJeff Garzik {
516c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
517c6fd2807SJeff Garzik 	u8 host_stat = 0, drv_stat;
518c6fd2807SJeff Garzik 	unsigned long flags;
519c6fd2807SJeff Garzik 
520c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
521c6fd2807SJeff Garzik 
522c6fd2807SJeff Garzik 	ap->hsm_task_state = HSM_ST_IDLE;
523c6fd2807SJeff Garzik 
524c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
525c6fd2807SJeff Garzik 
526c6fd2807SJeff Garzik 	switch (qc->tf.protocol) {
527c6fd2807SJeff Garzik 
528c6fd2807SJeff Garzik 	case ATA_PROT_DMA:
529c6fd2807SJeff Garzik 	case ATA_PROT_ATAPI_DMA:
530c6fd2807SJeff Garzik 		host_stat = ap->ops->bmdma_status(ap);
531c6fd2807SJeff Garzik 
532c6fd2807SJeff Garzik 		/* before we do anything else, clear DMA-Start bit */
533c6fd2807SJeff Garzik 		ap->ops->bmdma_stop(qc);
534c6fd2807SJeff Garzik 
535c6fd2807SJeff Garzik 		/* fall through */
536c6fd2807SJeff Garzik 
537c6fd2807SJeff Garzik 	default:
538c6fd2807SJeff Garzik 		ata_altstatus(ap);
539c6fd2807SJeff Garzik 		drv_stat = ata_chk_status(ap);
540c6fd2807SJeff Garzik 
541c6fd2807SJeff Garzik 		/* ack bmdma irq events */
542c6fd2807SJeff Garzik 		ap->ops->irq_clear(ap);
543c6fd2807SJeff Garzik 
544c6fd2807SJeff Garzik 		ata_dev_printk(qc->dev, KERN_ERR, "command 0x%x timeout, "
545c6fd2807SJeff Garzik 			       "stat 0x%x host_stat 0x%x\n",
546c6fd2807SJeff Garzik 			       qc->tf.command, drv_stat, host_stat);
547c6fd2807SJeff Garzik 
548c6fd2807SJeff Garzik 		/* complete taskfile transaction */
549c6fd2807SJeff Garzik 		qc->err_mask |= AC_ERR_TIMEOUT;
550c6fd2807SJeff Garzik 		break;
551c6fd2807SJeff Garzik 	}
552c6fd2807SJeff Garzik 
553c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
554c6fd2807SJeff Garzik 
555c6fd2807SJeff Garzik 	ata_eh_qc_complete(qc);
556c6fd2807SJeff Garzik 
557c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
558c6fd2807SJeff Garzik }
559c6fd2807SJeff Garzik 
560c6fd2807SJeff Garzik /**
561c6fd2807SJeff Garzik  *	ata_eng_timeout - Handle timeout of queued command
562c6fd2807SJeff Garzik  *	@ap: Port on which timed-out command is active
563c6fd2807SJeff Garzik  *
564c6fd2807SJeff Garzik  *	Some part of the kernel (currently, only the SCSI layer)
565c6fd2807SJeff Garzik  *	has noticed that the active command on port @ap has not
566c6fd2807SJeff Garzik  *	completed after a specified length of time.  Handle this
567c6fd2807SJeff Garzik  *	condition by disabling DMA (if necessary) and completing
568c6fd2807SJeff Garzik  *	transactions, with error if necessary.
569c6fd2807SJeff Garzik  *
570c6fd2807SJeff Garzik  *	This also handles the case of the "lost interrupt", where
571c6fd2807SJeff Garzik  *	for some reason (possibly hardware bug, possibly driver bug)
572c6fd2807SJeff Garzik  *	an interrupt was not delivered to the driver, even though the
573c6fd2807SJeff Garzik  *	transaction completed successfully.
574c6fd2807SJeff Garzik  *
575c6fd2807SJeff Garzik  *	TODO: kill this function once old EH is gone.
576c6fd2807SJeff Garzik  *
577c6fd2807SJeff Garzik  *	LOCKING:
578c6fd2807SJeff Garzik  *	Inherited from SCSI layer (none, can sleep)
579c6fd2807SJeff Garzik  */
580c6fd2807SJeff Garzik void ata_eng_timeout(struct ata_port *ap)
581c6fd2807SJeff Garzik {
582c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
583c6fd2807SJeff Garzik 
5849af5c9c9STejun Heo 	ata_qc_timeout(ata_qc_from_tag(ap, ap->link.active_tag));
585c6fd2807SJeff Garzik 
586c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
587c6fd2807SJeff Garzik }
588c6fd2807SJeff Garzik 
5895ddf24c5STejun Heo static int ata_eh_nr_in_flight(struct ata_port *ap)
5905ddf24c5STejun Heo {
5915ddf24c5STejun Heo 	unsigned int tag;
5925ddf24c5STejun Heo 	int nr = 0;
5935ddf24c5STejun Heo 
5945ddf24c5STejun Heo 	/* count only non-internal commands */
5955ddf24c5STejun Heo 	for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++)
5965ddf24c5STejun Heo 		if (ata_qc_from_tag(ap, tag))
5975ddf24c5STejun Heo 			nr++;
5985ddf24c5STejun Heo 
5995ddf24c5STejun Heo 	return nr;
6005ddf24c5STejun Heo }
6015ddf24c5STejun Heo 
6025ddf24c5STejun Heo void ata_eh_fastdrain_timerfn(unsigned long arg)
6035ddf24c5STejun Heo {
6045ddf24c5STejun Heo 	struct ata_port *ap = (void *)arg;
6055ddf24c5STejun Heo 	unsigned long flags;
6065ddf24c5STejun Heo 	int cnt;
6075ddf24c5STejun Heo 
6085ddf24c5STejun Heo 	spin_lock_irqsave(ap->lock, flags);
6095ddf24c5STejun Heo 
6105ddf24c5STejun Heo 	cnt = ata_eh_nr_in_flight(ap);
6115ddf24c5STejun Heo 
6125ddf24c5STejun Heo 	/* are we done? */
6135ddf24c5STejun Heo 	if (!cnt)
6145ddf24c5STejun Heo 		goto out_unlock;
6155ddf24c5STejun Heo 
6165ddf24c5STejun Heo 	if (cnt == ap->fastdrain_cnt) {
6175ddf24c5STejun Heo 		unsigned int tag;
6185ddf24c5STejun Heo 
6195ddf24c5STejun Heo 		/* No progress during the last interval, tag all
6205ddf24c5STejun Heo 		 * in-flight qcs as timed out and freeze the port.
6215ddf24c5STejun Heo 		 */
6225ddf24c5STejun Heo 		for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) {
6235ddf24c5STejun Heo 			struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
6245ddf24c5STejun Heo 			if (qc)
6255ddf24c5STejun Heo 				qc->err_mask |= AC_ERR_TIMEOUT;
6265ddf24c5STejun Heo 		}
6275ddf24c5STejun Heo 
6285ddf24c5STejun Heo 		ata_port_freeze(ap);
6295ddf24c5STejun Heo 	} else {
6305ddf24c5STejun Heo 		/* some qcs have finished, give it another chance */
6315ddf24c5STejun Heo 		ap->fastdrain_cnt = cnt;
6325ddf24c5STejun Heo 		ap->fastdrain_timer.expires =
6335ddf24c5STejun Heo 			jiffies + ATA_EH_FASTDRAIN_INTERVAL;
6345ddf24c5STejun Heo 		add_timer(&ap->fastdrain_timer);
6355ddf24c5STejun Heo 	}
6365ddf24c5STejun Heo 
6375ddf24c5STejun Heo  out_unlock:
6385ddf24c5STejun Heo 	spin_unlock_irqrestore(ap->lock, flags);
6395ddf24c5STejun Heo }
6405ddf24c5STejun Heo 
6415ddf24c5STejun Heo /**
6425ddf24c5STejun Heo  *	ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
6435ddf24c5STejun Heo  *	@ap: target ATA port
6445ddf24c5STejun Heo  *	@fastdrain: activate fast drain
6455ddf24c5STejun Heo  *
6465ddf24c5STejun Heo  *	Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
6475ddf24c5STejun Heo  *	is non-zero and EH wasn't pending before.  Fast drain ensures
6485ddf24c5STejun Heo  *	that EH kicks in in timely manner.
6495ddf24c5STejun Heo  *
6505ddf24c5STejun Heo  *	LOCKING:
6515ddf24c5STejun Heo  *	spin_lock_irqsave(host lock)
6525ddf24c5STejun Heo  */
6535ddf24c5STejun Heo static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
6545ddf24c5STejun Heo {
6555ddf24c5STejun Heo 	int cnt;
6565ddf24c5STejun Heo 
6575ddf24c5STejun Heo 	/* already scheduled? */
6585ddf24c5STejun Heo 	if (ap->pflags & ATA_PFLAG_EH_PENDING)
6595ddf24c5STejun Heo 		return;
6605ddf24c5STejun Heo 
6615ddf24c5STejun Heo 	ap->pflags |= ATA_PFLAG_EH_PENDING;
6625ddf24c5STejun Heo 
6635ddf24c5STejun Heo 	if (!fastdrain)
6645ddf24c5STejun Heo 		return;
6655ddf24c5STejun Heo 
6665ddf24c5STejun Heo 	/* do we have in-flight qcs? */
6675ddf24c5STejun Heo 	cnt = ata_eh_nr_in_flight(ap);
6685ddf24c5STejun Heo 	if (!cnt)
6695ddf24c5STejun Heo 		return;
6705ddf24c5STejun Heo 
6715ddf24c5STejun Heo 	/* activate fast drain */
6725ddf24c5STejun Heo 	ap->fastdrain_cnt = cnt;
6735ddf24c5STejun Heo 	ap->fastdrain_timer.expires = jiffies + ATA_EH_FASTDRAIN_INTERVAL;
6745ddf24c5STejun Heo 	add_timer(&ap->fastdrain_timer);
6755ddf24c5STejun Heo }
6765ddf24c5STejun Heo 
677c6fd2807SJeff Garzik /**
678c6fd2807SJeff Garzik  *	ata_qc_schedule_eh - schedule qc for error handling
679c6fd2807SJeff Garzik  *	@qc: command to schedule error handling for
680c6fd2807SJeff Garzik  *
681c6fd2807SJeff Garzik  *	Schedule error handling for @qc.  EH will kick in as soon as
682c6fd2807SJeff Garzik  *	other commands are drained.
683c6fd2807SJeff Garzik  *
684c6fd2807SJeff Garzik  *	LOCKING:
685cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
686c6fd2807SJeff Garzik  */
687c6fd2807SJeff Garzik void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
688c6fd2807SJeff Garzik {
689c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
690c6fd2807SJeff Garzik 
691c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
692c6fd2807SJeff Garzik 
693c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_FAILED;
6945ddf24c5STejun Heo 	ata_eh_set_pending(ap, 1);
695c6fd2807SJeff Garzik 
696c6fd2807SJeff Garzik 	/* The following will fail if timeout has already expired.
697c6fd2807SJeff Garzik 	 * ata_scsi_error() takes care of such scmds on EH entry.
698c6fd2807SJeff Garzik 	 * Note that ATA_QCFLAG_FAILED is unconditionally set after
699c6fd2807SJeff Garzik 	 * this function completes.
700c6fd2807SJeff Garzik 	 */
701c6fd2807SJeff Garzik 	scsi_req_abort_cmd(qc->scsicmd);
702c6fd2807SJeff Garzik }
703c6fd2807SJeff Garzik 
704c6fd2807SJeff Garzik /**
705c6fd2807SJeff Garzik  *	ata_port_schedule_eh - schedule error handling without a qc
706c6fd2807SJeff Garzik  *	@ap: ATA port to schedule EH for
707c6fd2807SJeff Garzik  *
708c6fd2807SJeff Garzik  *	Schedule error handling for @ap.  EH will kick in as soon as
709c6fd2807SJeff Garzik  *	all commands are drained.
710c6fd2807SJeff Garzik  *
711c6fd2807SJeff Garzik  *	LOCKING:
712cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
713c6fd2807SJeff Garzik  */
714c6fd2807SJeff Garzik void ata_port_schedule_eh(struct ata_port *ap)
715c6fd2807SJeff Garzik {
716c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
717c6fd2807SJeff Garzik 
718f4d6d004STejun Heo 	if (ap->pflags & ATA_PFLAG_INITIALIZING)
719f4d6d004STejun Heo 		return;
720f4d6d004STejun Heo 
7215ddf24c5STejun Heo 	ata_eh_set_pending(ap, 1);
722cca3974eSJeff Garzik 	scsi_schedule_eh(ap->scsi_host);
723c6fd2807SJeff Garzik 
724c6fd2807SJeff Garzik 	DPRINTK("port EH scheduled\n");
725c6fd2807SJeff Garzik }
726c6fd2807SJeff Garzik 
727dbd82616STejun Heo static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
728c6fd2807SJeff Garzik {
729c6fd2807SJeff Garzik 	int tag, nr_aborted = 0;
730c6fd2807SJeff Garzik 
731c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
732c6fd2807SJeff Garzik 
7335ddf24c5STejun Heo 	/* we're gonna abort all commands, no need for fast drain */
7345ddf24c5STejun Heo 	ata_eh_set_pending(ap, 0);
7355ddf24c5STejun Heo 
736c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
737c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
738c6fd2807SJeff Garzik 
739dbd82616STejun Heo 		if (qc && (!link || qc->dev->link == link)) {
740c6fd2807SJeff Garzik 			qc->flags |= ATA_QCFLAG_FAILED;
741c6fd2807SJeff Garzik 			ata_qc_complete(qc);
742c6fd2807SJeff Garzik 			nr_aborted++;
743c6fd2807SJeff Garzik 		}
744c6fd2807SJeff Garzik 	}
745c6fd2807SJeff Garzik 
746c6fd2807SJeff Garzik 	if (!nr_aborted)
747c6fd2807SJeff Garzik 		ata_port_schedule_eh(ap);
748c6fd2807SJeff Garzik 
749c6fd2807SJeff Garzik 	return nr_aborted;
750c6fd2807SJeff Garzik }
751c6fd2807SJeff Garzik 
752c6fd2807SJeff Garzik /**
753dbd82616STejun Heo  *	ata_link_abort - abort all qc's on the link
754dbd82616STejun Heo  *	@link: ATA link to abort qc's for
755dbd82616STejun Heo  *
756dbd82616STejun Heo  *	Abort all active qc's active on @link and schedule EH.
757dbd82616STejun Heo  *
758dbd82616STejun Heo  *	LOCKING:
759dbd82616STejun Heo  *	spin_lock_irqsave(host lock)
760dbd82616STejun Heo  *
761dbd82616STejun Heo  *	RETURNS:
762dbd82616STejun Heo  *	Number of aborted qc's.
763dbd82616STejun Heo  */
764dbd82616STejun Heo int ata_link_abort(struct ata_link *link)
765dbd82616STejun Heo {
766dbd82616STejun Heo 	return ata_do_link_abort(link->ap, link);
767dbd82616STejun Heo }
768dbd82616STejun Heo 
769dbd82616STejun Heo /**
770dbd82616STejun Heo  *	ata_port_abort - abort all qc's on the port
771dbd82616STejun Heo  *	@ap: ATA port to abort qc's for
772dbd82616STejun Heo  *
773dbd82616STejun Heo  *	Abort all active qc's of @ap and schedule EH.
774dbd82616STejun Heo  *
775dbd82616STejun Heo  *	LOCKING:
776dbd82616STejun Heo  *	spin_lock_irqsave(host_set lock)
777dbd82616STejun Heo  *
778dbd82616STejun Heo  *	RETURNS:
779dbd82616STejun Heo  *	Number of aborted qc's.
780dbd82616STejun Heo  */
781dbd82616STejun Heo int ata_port_abort(struct ata_port *ap)
782dbd82616STejun Heo {
783dbd82616STejun Heo 	return ata_do_link_abort(ap, NULL);
784dbd82616STejun Heo }
785dbd82616STejun Heo 
786dbd82616STejun Heo /**
787c6fd2807SJeff Garzik  *	__ata_port_freeze - freeze port
788c6fd2807SJeff Garzik  *	@ap: ATA port to freeze
789c6fd2807SJeff Garzik  *
790c6fd2807SJeff Garzik  *	This function is called when HSM violation or some other
791c6fd2807SJeff Garzik  *	condition disrupts normal operation of the port.  Frozen port
792c6fd2807SJeff Garzik  *	is not allowed to perform any operation until the port is
793c6fd2807SJeff Garzik  *	thawed, which usually follows a successful reset.
794c6fd2807SJeff Garzik  *
795c6fd2807SJeff Garzik  *	ap->ops->freeze() callback can be used for freezing the port
796c6fd2807SJeff Garzik  *	hardware-wise (e.g. mask interrupt and stop DMA engine).  If a
797c6fd2807SJeff Garzik  *	port cannot be frozen hardware-wise, the interrupt handler
798c6fd2807SJeff Garzik  *	must ack and clear interrupts unconditionally while the port
799c6fd2807SJeff Garzik  *	is frozen.
800c6fd2807SJeff Garzik  *
801c6fd2807SJeff Garzik  *	LOCKING:
802cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
803c6fd2807SJeff Garzik  */
804c6fd2807SJeff Garzik static void __ata_port_freeze(struct ata_port *ap)
805c6fd2807SJeff Garzik {
806c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
807c6fd2807SJeff Garzik 
808c6fd2807SJeff Garzik 	if (ap->ops->freeze)
809c6fd2807SJeff Garzik 		ap->ops->freeze(ap);
810c6fd2807SJeff Garzik 
811c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_FROZEN;
812c6fd2807SJeff Garzik 
81344877b4eSTejun Heo 	DPRINTK("ata%u port frozen\n", ap->print_id);
814c6fd2807SJeff Garzik }
815c6fd2807SJeff Garzik 
816c6fd2807SJeff Garzik /**
817c6fd2807SJeff Garzik  *	ata_port_freeze - abort & freeze port
818c6fd2807SJeff Garzik  *	@ap: ATA port to freeze
819c6fd2807SJeff Garzik  *
820c6fd2807SJeff Garzik  *	Abort and freeze @ap.
821c6fd2807SJeff Garzik  *
822c6fd2807SJeff Garzik  *	LOCKING:
823cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
824c6fd2807SJeff Garzik  *
825c6fd2807SJeff Garzik  *	RETURNS:
826c6fd2807SJeff Garzik  *	Number of aborted commands.
827c6fd2807SJeff Garzik  */
828c6fd2807SJeff Garzik int ata_port_freeze(struct ata_port *ap)
829c6fd2807SJeff Garzik {
830c6fd2807SJeff Garzik 	int nr_aborted;
831c6fd2807SJeff Garzik 
832c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
833c6fd2807SJeff Garzik 
834c6fd2807SJeff Garzik 	nr_aborted = ata_port_abort(ap);
835c6fd2807SJeff Garzik 	__ata_port_freeze(ap);
836c6fd2807SJeff Garzik 
837c6fd2807SJeff Garzik 	return nr_aborted;
838c6fd2807SJeff Garzik }
839c6fd2807SJeff Garzik 
840c6fd2807SJeff Garzik /**
841c6fd2807SJeff Garzik  *	ata_eh_freeze_port - EH helper to freeze port
842c6fd2807SJeff Garzik  *	@ap: ATA port to freeze
843c6fd2807SJeff Garzik  *
844c6fd2807SJeff Garzik  *	Freeze @ap.
845c6fd2807SJeff Garzik  *
846c6fd2807SJeff Garzik  *	LOCKING:
847c6fd2807SJeff Garzik  *	None.
848c6fd2807SJeff Garzik  */
849c6fd2807SJeff Garzik void ata_eh_freeze_port(struct ata_port *ap)
850c6fd2807SJeff Garzik {
851c6fd2807SJeff Garzik 	unsigned long flags;
852c6fd2807SJeff Garzik 
853c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
854c6fd2807SJeff Garzik 		return;
855c6fd2807SJeff Garzik 
856c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
857c6fd2807SJeff Garzik 	__ata_port_freeze(ap);
858c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
859c6fd2807SJeff Garzik }
860c6fd2807SJeff Garzik 
861c6fd2807SJeff Garzik /**
862c6fd2807SJeff Garzik  *	ata_port_thaw_port - EH helper to thaw port
863c6fd2807SJeff Garzik  *	@ap: ATA port to thaw
864c6fd2807SJeff Garzik  *
865c6fd2807SJeff Garzik  *	Thaw frozen port @ap.
866c6fd2807SJeff Garzik  *
867c6fd2807SJeff Garzik  *	LOCKING:
868c6fd2807SJeff Garzik  *	None.
869c6fd2807SJeff Garzik  */
870c6fd2807SJeff Garzik void ata_eh_thaw_port(struct ata_port *ap)
871c6fd2807SJeff Garzik {
872c6fd2807SJeff Garzik 	unsigned long flags;
873c6fd2807SJeff Garzik 
874c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
875c6fd2807SJeff Garzik 		return;
876c6fd2807SJeff Garzik 
877c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
878c6fd2807SJeff Garzik 
879c6fd2807SJeff Garzik 	ap->pflags &= ~ATA_PFLAG_FROZEN;
880c6fd2807SJeff Garzik 
881c6fd2807SJeff Garzik 	if (ap->ops->thaw)
882c6fd2807SJeff Garzik 		ap->ops->thaw(ap);
883c6fd2807SJeff Garzik 
884c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
885c6fd2807SJeff Garzik 
88644877b4eSTejun Heo 	DPRINTK("ata%u port thawed\n", ap->print_id);
887c6fd2807SJeff Garzik }
888c6fd2807SJeff Garzik 
889c6fd2807SJeff Garzik static void ata_eh_scsidone(struct scsi_cmnd *scmd)
890c6fd2807SJeff Garzik {
891c6fd2807SJeff Garzik 	/* nada */
892c6fd2807SJeff Garzik }
893c6fd2807SJeff Garzik 
894c6fd2807SJeff Garzik static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
895c6fd2807SJeff Garzik {
896c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
897c6fd2807SJeff Garzik 	struct scsi_cmnd *scmd = qc->scsicmd;
898c6fd2807SJeff Garzik 	unsigned long flags;
899c6fd2807SJeff Garzik 
900c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
901c6fd2807SJeff Garzik 	qc->scsidone = ata_eh_scsidone;
902c6fd2807SJeff Garzik 	__ata_qc_complete(qc);
903c6fd2807SJeff Garzik 	WARN_ON(ata_tag_valid(qc->tag));
904c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
905c6fd2807SJeff Garzik 
906c6fd2807SJeff Garzik 	scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
907c6fd2807SJeff Garzik }
908c6fd2807SJeff Garzik 
909c6fd2807SJeff Garzik /**
910c6fd2807SJeff Garzik  *	ata_eh_qc_complete - Complete an active ATA command from EH
911c6fd2807SJeff Garzik  *	@qc: Command to complete
912c6fd2807SJeff Garzik  *
913c6fd2807SJeff Garzik  *	Indicate to the mid and upper layers that an ATA command has
914c6fd2807SJeff Garzik  *	completed.  To be used from EH.
915c6fd2807SJeff Garzik  */
916c6fd2807SJeff Garzik void ata_eh_qc_complete(struct ata_queued_cmd *qc)
917c6fd2807SJeff Garzik {
918c6fd2807SJeff Garzik 	struct scsi_cmnd *scmd = qc->scsicmd;
919c6fd2807SJeff Garzik 	scmd->retries = scmd->allowed;
920c6fd2807SJeff Garzik 	__ata_eh_qc_complete(qc);
921c6fd2807SJeff Garzik }
922c6fd2807SJeff Garzik 
923c6fd2807SJeff Garzik /**
924c6fd2807SJeff Garzik  *	ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
925c6fd2807SJeff Garzik  *	@qc: Command to retry
926c6fd2807SJeff Garzik  *
927c6fd2807SJeff Garzik  *	Indicate to the mid and upper layers that an ATA command
928c6fd2807SJeff Garzik  *	should be retried.  To be used from EH.
929c6fd2807SJeff Garzik  *
930c6fd2807SJeff Garzik  *	SCSI midlayer limits the number of retries to scmd->allowed.
931c6fd2807SJeff Garzik  *	scmd->retries is decremented for commands which get retried
932c6fd2807SJeff Garzik  *	due to unrelated failures (qc->err_mask is zero).
933c6fd2807SJeff Garzik  */
934c6fd2807SJeff Garzik void ata_eh_qc_retry(struct ata_queued_cmd *qc)
935c6fd2807SJeff Garzik {
936c6fd2807SJeff Garzik 	struct scsi_cmnd *scmd = qc->scsicmd;
937c6fd2807SJeff Garzik 	if (!qc->err_mask && scmd->retries)
938c6fd2807SJeff Garzik 		scmd->retries--;
939c6fd2807SJeff Garzik 	__ata_eh_qc_complete(qc);
940c6fd2807SJeff Garzik }
941c6fd2807SJeff Garzik 
942c6fd2807SJeff Garzik /**
943c6fd2807SJeff Garzik  *	ata_eh_detach_dev - detach ATA device
944c6fd2807SJeff Garzik  *	@dev: ATA device to detach
945c6fd2807SJeff Garzik  *
946c6fd2807SJeff Garzik  *	Detach @dev.
947c6fd2807SJeff Garzik  *
948c6fd2807SJeff Garzik  *	LOCKING:
949c6fd2807SJeff Garzik  *	None.
950c6fd2807SJeff Garzik  */
951c6fd2807SJeff Garzik static void ata_eh_detach_dev(struct ata_device *dev)
952c6fd2807SJeff Garzik {
953f58229f8STejun Heo 	struct ata_link *link = dev->link;
954f58229f8STejun Heo 	struct ata_port *ap = link->ap;
955c6fd2807SJeff Garzik 	unsigned long flags;
956c6fd2807SJeff Garzik 
957c6fd2807SJeff Garzik 	ata_dev_disable(dev);
958c6fd2807SJeff Garzik 
959c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
960c6fd2807SJeff Garzik 
961c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_DETACH;
962c6fd2807SJeff Garzik 
963c6fd2807SJeff Garzik 	if (ata_scsi_offline_dev(dev)) {
964c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_DETACHED;
965c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
966c6fd2807SJeff Garzik 	}
967c6fd2807SJeff Garzik 
968c6fd2807SJeff Garzik 	/* clear per-dev EH actions */
969f58229f8STejun Heo 	ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
970f58229f8STejun Heo 	ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
971c6fd2807SJeff Garzik 
972c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
973c6fd2807SJeff Garzik }
974c6fd2807SJeff Garzik 
975c6fd2807SJeff Garzik /**
976c6fd2807SJeff Garzik  *	ata_eh_about_to_do - about to perform eh_action
977955e57dfSTejun Heo  *	@link: target ATA link
978c6fd2807SJeff Garzik  *	@dev: target ATA dev for per-dev action (can be NULL)
979c6fd2807SJeff Garzik  *	@action: action about to be performed
980c6fd2807SJeff Garzik  *
981c6fd2807SJeff Garzik  *	Called just before performing EH actions to clear related bits
982955e57dfSTejun Heo  *	in @link->eh_info such that eh actions are not unnecessarily
983955e57dfSTejun Heo  *	repeated.
984c6fd2807SJeff Garzik  *
985c6fd2807SJeff Garzik  *	LOCKING:
986c6fd2807SJeff Garzik  *	None.
987c6fd2807SJeff Garzik  */
988955e57dfSTejun Heo static void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
989c6fd2807SJeff Garzik 			       unsigned int action)
990c6fd2807SJeff Garzik {
991955e57dfSTejun Heo 	struct ata_port *ap = link->ap;
992955e57dfSTejun Heo 	struct ata_eh_info *ehi = &link->eh_info;
993955e57dfSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
994c6fd2807SJeff Garzik 	unsigned long flags;
995c6fd2807SJeff Garzik 
996c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
997c6fd2807SJeff Garzik 
998c6fd2807SJeff Garzik 	/* Reset is represented by combination of actions and EHI
999c6fd2807SJeff Garzik 	 * flags.  Suck in all related bits before clearing eh_info to
1000c6fd2807SJeff Garzik 	 * avoid losing requested action.
1001c6fd2807SJeff Garzik 	 */
1002c6fd2807SJeff Garzik 	if (action & ATA_EH_RESET_MASK) {
1003c6fd2807SJeff Garzik 		ehc->i.action |= ehi->action & ATA_EH_RESET_MASK;
1004c6fd2807SJeff Garzik 		ehc->i.flags |= ehi->flags & ATA_EHI_RESET_MODIFIER_MASK;
1005c6fd2807SJeff Garzik 
1006c6fd2807SJeff Garzik 		/* make sure all reset actions are cleared & clear EHI flags */
1007c6fd2807SJeff Garzik 		action |= ATA_EH_RESET_MASK;
1008c6fd2807SJeff Garzik 		ehi->flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
1009c6fd2807SJeff Garzik 	}
1010c6fd2807SJeff Garzik 
1011955e57dfSTejun Heo 	ata_eh_clear_action(link, dev, ehi, action);
1012c6fd2807SJeff Garzik 
1013c6fd2807SJeff Garzik 	if (!(ehc->i.flags & ATA_EHI_QUIET))
1014c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_RECOVERED;
1015c6fd2807SJeff Garzik 
1016c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1017c6fd2807SJeff Garzik }
1018c6fd2807SJeff Garzik 
1019c6fd2807SJeff Garzik /**
1020c6fd2807SJeff Garzik  *	ata_eh_done - EH action complete
1021c6fd2807SJeff Garzik *	@ap: target ATA port
1022c6fd2807SJeff Garzik  *	@dev: target ATA dev for per-dev action (can be NULL)
1023c6fd2807SJeff Garzik  *	@action: action just completed
1024c6fd2807SJeff Garzik  *
1025c6fd2807SJeff Garzik  *	Called right after performing EH actions to clear related bits
1026955e57dfSTejun Heo  *	in @link->eh_context.
1027c6fd2807SJeff Garzik  *
1028c6fd2807SJeff Garzik  *	LOCKING:
1029c6fd2807SJeff Garzik  *	None.
1030c6fd2807SJeff Garzik  */
1031955e57dfSTejun Heo static void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1032c6fd2807SJeff Garzik 			unsigned int action)
1033c6fd2807SJeff Garzik {
1034955e57dfSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
10359af5c9c9STejun Heo 
1036c6fd2807SJeff Garzik 	/* if reset is complete, clear all reset actions & reset modifier */
1037c6fd2807SJeff Garzik 	if (action & ATA_EH_RESET_MASK) {
1038c6fd2807SJeff Garzik 		action |= ATA_EH_RESET_MASK;
10399af5c9c9STejun Heo 		ehc->i.flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
1040c6fd2807SJeff Garzik 	}
1041c6fd2807SJeff Garzik 
1042955e57dfSTejun Heo 	ata_eh_clear_action(link, dev, &ehc->i, action);
1043c6fd2807SJeff Garzik }
1044c6fd2807SJeff Garzik 
1045c6fd2807SJeff Garzik /**
1046c6fd2807SJeff Garzik  *	ata_err_string - convert err_mask to descriptive string
1047c6fd2807SJeff Garzik  *	@err_mask: error mask to convert to string
1048c6fd2807SJeff Garzik  *
1049c6fd2807SJeff Garzik  *	Convert @err_mask to descriptive string.  Errors are
1050c6fd2807SJeff Garzik  *	prioritized according to severity and only the most severe
1051c6fd2807SJeff Garzik  *	error is reported.
1052c6fd2807SJeff Garzik  *
1053c6fd2807SJeff Garzik  *	LOCKING:
1054c6fd2807SJeff Garzik  *	None.
1055c6fd2807SJeff Garzik  *
1056c6fd2807SJeff Garzik  *	RETURNS:
1057c6fd2807SJeff Garzik  *	Descriptive string for @err_mask
1058c6fd2807SJeff Garzik  */
1059c6fd2807SJeff Garzik static const char * ata_err_string(unsigned int err_mask)
1060c6fd2807SJeff Garzik {
1061c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_HOST_BUS)
1062c6fd2807SJeff Garzik 		return "host bus error";
1063c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_ATA_BUS)
1064c6fd2807SJeff Garzik 		return "ATA bus error";
1065c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_TIMEOUT)
1066c6fd2807SJeff Garzik 		return "timeout";
1067c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_HSM)
1068c6fd2807SJeff Garzik 		return "HSM violation";
1069c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_SYSTEM)
1070c6fd2807SJeff Garzik 		return "internal error";
1071c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_MEDIA)
1072c6fd2807SJeff Garzik 		return "media error";
1073c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_INVALID)
1074c6fd2807SJeff Garzik 		return "invalid argument";
1075c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_DEV)
1076c6fd2807SJeff Garzik 		return "device error";
1077c6fd2807SJeff Garzik 	return "unknown error";
1078c6fd2807SJeff Garzik }
1079c6fd2807SJeff Garzik 
1080c6fd2807SJeff Garzik /**
1081c6fd2807SJeff Garzik  *	ata_read_log_page - read a specific log page
1082c6fd2807SJeff Garzik  *	@dev: target device
1083c6fd2807SJeff Garzik  *	@page: page to read
1084c6fd2807SJeff Garzik  *	@buf: buffer to store read page
1085c6fd2807SJeff Garzik  *	@sectors: number of sectors to read
1086c6fd2807SJeff Garzik  *
1087c6fd2807SJeff Garzik  *	Read log page using READ_LOG_EXT command.
1088c6fd2807SJeff Garzik  *
1089c6fd2807SJeff Garzik  *	LOCKING:
1090c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1091c6fd2807SJeff Garzik  *
1092c6fd2807SJeff Garzik  *	RETURNS:
1093c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
1094c6fd2807SJeff Garzik  */
1095c6fd2807SJeff Garzik static unsigned int ata_read_log_page(struct ata_device *dev,
1096c6fd2807SJeff Garzik 				      u8 page, void *buf, unsigned int sectors)
1097c6fd2807SJeff Garzik {
1098c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1099c6fd2807SJeff Garzik 	unsigned int err_mask;
1100c6fd2807SJeff Garzik 
1101c6fd2807SJeff Garzik 	DPRINTK("read log page - page %d\n", page);
1102c6fd2807SJeff Garzik 
1103c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
1104c6fd2807SJeff Garzik 	tf.command = ATA_CMD_READ_LOG_EXT;
1105c6fd2807SJeff Garzik 	tf.lbal = page;
1106c6fd2807SJeff Garzik 	tf.nsect = sectors;
1107c6fd2807SJeff Garzik 	tf.hob_nsect = sectors >> 8;
1108c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1109c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_PIO;
1110c6fd2807SJeff Garzik 
1111c6fd2807SJeff Garzik 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
1112c6fd2807SJeff Garzik 				     buf, sectors * ATA_SECT_SIZE);
1113c6fd2807SJeff Garzik 
1114c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
1115c6fd2807SJeff Garzik 	return err_mask;
1116c6fd2807SJeff Garzik }
1117c6fd2807SJeff Garzik 
1118c6fd2807SJeff Garzik /**
1119c6fd2807SJeff Garzik  *	ata_eh_read_log_10h - Read log page 10h for NCQ error details
1120c6fd2807SJeff Garzik  *	@dev: Device to read log page 10h from
1121c6fd2807SJeff Garzik  *	@tag: Resulting tag of the failed command
1122c6fd2807SJeff Garzik  *	@tf: Resulting taskfile registers of the failed command
1123c6fd2807SJeff Garzik  *
1124c6fd2807SJeff Garzik  *	Read log page 10h to obtain NCQ error details and clear error
1125c6fd2807SJeff Garzik  *	condition.
1126c6fd2807SJeff Garzik  *
1127c6fd2807SJeff Garzik  *	LOCKING:
1128c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1129c6fd2807SJeff Garzik  *
1130c6fd2807SJeff Garzik  *	RETURNS:
1131c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
1132c6fd2807SJeff Garzik  */
1133c6fd2807SJeff Garzik static int ata_eh_read_log_10h(struct ata_device *dev,
1134c6fd2807SJeff Garzik 			       int *tag, struct ata_taskfile *tf)
1135c6fd2807SJeff Garzik {
11369af5c9c9STejun Heo 	u8 *buf = dev->link->ap->sector_buf;
1137c6fd2807SJeff Garzik 	unsigned int err_mask;
1138c6fd2807SJeff Garzik 	u8 csum;
1139c6fd2807SJeff Garzik 	int i;
1140c6fd2807SJeff Garzik 
1141c6fd2807SJeff Garzik 	err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
1142c6fd2807SJeff Garzik 	if (err_mask)
1143c6fd2807SJeff Garzik 		return -EIO;
1144c6fd2807SJeff Garzik 
1145c6fd2807SJeff Garzik 	csum = 0;
1146c6fd2807SJeff Garzik 	for (i = 0; i < ATA_SECT_SIZE; i++)
1147c6fd2807SJeff Garzik 		csum += buf[i];
1148c6fd2807SJeff Garzik 	if (csum)
1149c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_WARNING,
1150c6fd2807SJeff Garzik 			       "invalid checksum 0x%x on log page 10h\n", csum);
1151c6fd2807SJeff Garzik 
1152c6fd2807SJeff Garzik 	if (buf[0] & 0x80)
1153c6fd2807SJeff Garzik 		return -ENOENT;
1154c6fd2807SJeff Garzik 
1155c6fd2807SJeff Garzik 	*tag = buf[0] & 0x1f;
1156c6fd2807SJeff Garzik 
1157c6fd2807SJeff Garzik 	tf->command = buf[2];
1158c6fd2807SJeff Garzik 	tf->feature = buf[3];
1159c6fd2807SJeff Garzik 	tf->lbal = buf[4];
1160c6fd2807SJeff Garzik 	tf->lbam = buf[5];
1161c6fd2807SJeff Garzik 	tf->lbah = buf[6];
1162c6fd2807SJeff Garzik 	tf->device = buf[7];
1163c6fd2807SJeff Garzik 	tf->hob_lbal = buf[8];
1164c6fd2807SJeff Garzik 	tf->hob_lbam = buf[9];
1165c6fd2807SJeff Garzik 	tf->hob_lbah = buf[10];
1166c6fd2807SJeff Garzik 	tf->nsect = buf[12];
1167c6fd2807SJeff Garzik 	tf->hob_nsect = buf[13];
1168c6fd2807SJeff Garzik 
1169c6fd2807SJeff Garzik 	return 0;
1170c6fd2807SJeff Garzik }
1171c6fd2807SJeff Garzik 
1172c6fd2807SJeff Garzik /**
1173c6fd2807SJeff Garzik  *	atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1174c6fd2807SJeff Garzik  *	@dev: device to perform REQUEST_SENSE to
1175c6fd2807SJeff Garzik  *	@sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
1176c6fd2807SJeff Garzik  *
1177c6fd2807SJeff Garzik  *	Perform ATAPI REQUEST_SENSE after the device reported CHECK
1178c6fd2807SJeff Garzik  *	SENSE.  This function is EH helper.
1179c6fd2807SJeff Garzik  *
1180c6fd2807SJeff Garzik  *	LOCKING:
1181c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1182c6fd2807SJeff Garzik  *
1183c6fd2807SJeff Garzik  *	RETURNS:
1184c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask on failure
1185c6fd2807SJeff Garzik  */
118656287768SAlbert Lee static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc)
1187c6fd2807SJeff Garzik {
118856287768SAlbert Lee 	struct ata_device *dev = qc->dev;
118956287768SAlbert Lee 	unsigned char *sense_buf = qc->scsicmd->sense_buffer;
11909af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
1191c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1192c6fd2807SJeff Garzik 	u8 cdb[ATAPI_CDB_LEN];
1193c6fd2807SJeff Garzik 
1194c6fd2807SJeff Garzik 	DPRINTK("ATAPI request sense\n");
1195c6fd2807SJeff Garzik 
1196c6fd2807SJeff Garzik 	/* FIXME: is this needed? */
1197c6fd2807SJeff Garzik 	memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1198c6fd2807SJeff Garzik 
119956287768SAlbert Lee 	/* initialize sense_buf with the error register,
120056287768SAlbert Lee 	 * for the case where they are -not- overwritten
120156287768SAlbert Lee 	 */
1202c6fd2807SJeff Garzik 	sense_buf[0] = 0x70;
120356287768SAlbert Lee 	sense_buf[2] = qc->result_tf.feature >> 4;
120456287768SAlbert Lee 
120556287768SAlbert Lee 	/* some devices time out if garbage left in tf */
120656287768SAlbert Lee 	ata_tf_init(dev, &tf);
1207c6fd2807SJeff Garzik 
1208c6fd2807SJeff Garzik 	memset(cdb, 0, ATAPI_CDB_LEN);
1209c6fd2807SJeff Garzik 	cdb[0] = REQUEST_SENSE;
1210c6fd2807SJeff Garzik 	cdb[4] = SCSI_SENSE_BUFFERSIZE;
1211c6fd2807SJeff Garzik 
1212c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1213c6fd2807SJeff Garzik 	tf.command = ATA_CMD_PACKET;
1214c6fd2807SJeff Garzik 
1215c6fd2807SJeff Garzik 	/* is it pointless to prefer PIO for "safety reasons"? */
1216c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_PIO_DMA) {
1217c6fd2807SJeff Garzik 		tf.protocol = ATA_PROT_ATAPI_DMA;
1218c6fd2807SJeff Garzik 		tf.feature |= ATAPI_PKT_DMA;
1219c6fd2807SJeff Garzik 	} else {
1220c6fd2807SJeff Garzik 		tf.protocol = ATA_PROT_ATAPI;
1221c6fd2807SJeff Garzik 		tf.lbam = (8 * 1024) & 0xff;
1222c6fd2807SJeff Garzik 		tf.lbah = (8 * 1024) >> 8;
1223c6fd2807SJeff Garzik 	}
1224c6fd2807SJeff Garzik 
1225c6fd2807SJeff Garzik 	return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
1226c6fd2807SJeff Garzik 				 sense_buf, SCSI_SENSE_BUFFERSIZE);
1227c6fd2807SJeff Garzik }
1228c6fd2807SJeff Garzik 
1229c6fd2807SJeff Garzik /**
1230c6fd2807SJeff Garzik  *	ata_eh_analyze_serror - analyze SError for a failed port
12310260731fSTejun Heo  *	@link: ATA link to analyze SError for
1232c6fd2807SJeff Garzik  *
1233c6fd2807SJeff Garzik  *	Analyze SError if available and further determine cause of
1234c6fd2807SJeff Garzik  *	failure.
1235c6fd2807SJeff Garzik  *
1236c6fd2807SJeff Garzik  *	LOCKING:
1237c6fd2807SJeff Garzik  *	None.
1238c6fd2807SJeff Garzik  */
12390260731fSTejun Heo static void ata_eh_analyze_serror(struct ata_link *link)
1240c6fd2807SJeff Garzik {
12410260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
1242c6fd2807SJeff Garzik 	u32 serror = ehc->i.serror;
1243c6fd2807SJeff Garzik 	unsigned int err_mask = 0, action = 0;
1244c6fd2807SJeff Garzik 
1245c6fd2807SJeff Garzik 	if (serror & SERR_PERSISTENT) {
1246c6fd2807SJeff Garzik 		err_mask |= AC_ERR_ATA_BUS;
1247c6fd2807SJeff Garzik 		action |= ATA_EH_HARDRESET;
1248c6fd2807SJeff Garzik 	}
1249c6fd2807SJeff Garzik 	if (serror &
1250c6fd2807SJeff Garzik 	    (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
1251c6fd2807SJeff Garzik 		err_mask |= AC_ERR_ATA_BUS;
1252c6fd2807SJeff Garzik 		action |= ATA_EH_SOFTRESET;
1253c6fd2807SJeff Garzik 	}
1254c6fd2807SJeff Garzik 	if (serror & SERR_PROTOCOL) {
1255c6fd2807SJeff Garzik 		err_mask |= AC_ERR_HSM;
1256c6fd2807SJeff Garzik 		action |= ATA_EH_SOFTRESET;
1257c6fd2807SJeff Garzik 	}
1258c6fd2807SJeff Garzik 	if (serror & SERR_INTERNAL) {
1259c6fd2807SJeff Garzik 		err_mask |= AC_ERR_SYSTEM;
1260771b8dadSTejun Heo 		action |= ATA_EH_HARDRESET;
1261c6fd2807SJeff Garzik 	}
1262c6fd2807SJeff Garzik 	if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG))
1263c6fd2807SJeff Garzik 		ata_ehi_hotplugged(&ehc->i);
1264c6fd2807SJeff Garzik 
1265c6fd2807SJeff Garzik 	ehc->i.err_mask |= err_mask;
1266c6fd2807SJeff Garzik 	ehc->i.action |= action;
1267c6fd2807SJeff Garzik }
1268c6fd2807SJeff Garzik 
1269c6fd2807SJeff Garzik /**
1270c6fd2807SJeff Garzik  *	ata_eh_analyze_ncq_error - analyze NCQ error
12710260731fSTejun Heo  *	@link: ATA link to analyze NCQ error for
1272c6fd2807SJeff Garzik  *
1273c6fd2807SJeff Garzik  *	Read log page 10h, determine the offending qc and acquire
1274c6fd2807SJeff Garzik  *	error status TF.  For NCQ device errors, all LLDDs have to do
1275c6fd2807SJeff Garzik  *	is setting AC_ERR_DEV in ehi->err_mask.  This function takes
1276c6fd2807SJeff Garzik  *	care of the rest.
1277c6fd2807SJeff Garzik  *
1278c6fd2807SJeff Garzik  *	LOCKING:
1279c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1280c6fd2807SJeff Garzik  */
12810260731fSTejun Heo static void ata_eh_analyze_ncq_error(struct ata_link *link)
1282c6fd2807SJeff Garzik {
12830260731fSTejun Heo 	struct ata_port *ap = link->ap;
12840260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
12850260731fSTejun Heo 	struct ata_device *dev = link->device;
1286c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
1287c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1288c6fd2807SJeff Garzik 	int tag, rc;
1289c6fd2807SJeff Garzik 
1290c6fd2807SJeff Garzik 	/* if frozen, we can't do much */
1291c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN)
1292c6fd2807SJeff Garzik 		return;
1293c6fd2807SJeff Garzik 
1294c6fd2807SJeff Garzik 	/* is it NCQ device error? */
12950260731fSTejun Heo 	if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1296c6fd2807SJeff Garzik 		return;
1297c6fd2807SJeff Garzik 
1298c6fd2807SJeff Garzik 	/* has LLDD analyzed already? */
1299c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1300c6fd2807SJeff Garzik 		qc = __ata_qc_from_tag(ap, tag);
1301c6fd2807SJeff Garzik 
1302c6fd2807SJeff Garzik 		if (!(qc->flags & ATA_QCFLAG_FAILED))
1303c6fd2807SJeff Garzik 			continue;
1304c6fd2807SJeff Garzik 
1305c6fd2807SJeff Garzik 		if (qc->err_mask)
1306c6fd2807SJeff Garzik 			return;
1307c6fd2807SJeff Garzik 	}
1308c6fd2807SJeff Garzik 
1309c6fd2807SJeff Garzik 	/* okay, this error is ours */
1310c6fd2807SJeff Garzik 	rc = ata_eh_read_log_10h(dev, &tag, &tf);
1311c6fd2807SJeff Garzik 	if (rc) {
13120260731fSTejun Heo 		ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
1313c6fd2807SJeff Garzik 				"(errno=%d)\n", rc);
1314c6fd2807SJeff Garzik 		return;
1315c6fd2807SJeff Garzik 	}
1316c6fd2807SJeff Garzik 
13170260731fSTejun Heo 	if (!(link->sactive & (1 << tag))) {
13180260731fSTejun Heo 		ata_link_printk(link, KERN_ERR, "log page 10h reported "
1319c6fd2807SJeff Garzik 				"inactive tag %d\n", tag);
1320c6fd2807SJeff Garzik 		return;
1321c6fd2807SJeff Garzik 	}
1322c6fd2807SJeff Garzik 
1323c6fd2807SJeff Garzik 	/* we've got the perpetrator, condemn it */
1324c6fd2807SJeff Garzik 	qc = __ata_qc_from_tag(ap, tag);
1325c6fd2807SJeff Garzik 	memcpy(&qc->result_tf, &tf, sizeof(tf));
13265335b729STejun Heo 	qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
1327c6fd2807SJeff Garzik 	ehc->i.err_mask &= ~AC_ERR_DEV;
1328c6fd2807SJeff Garzik }
1329c6fd2807SJeff Garzik 
1330c6fd2807SJeff Garzik /**
1331c6fd2807SJeff Garzik  *	ata_eh_analyze_tf - analyze taskfile of a failed qc
1332c6fd2807SJeff Garzik  *	@qc: qc to analyze
1333c6fd2807SJeff Garzik  *	@tf: Taskfile registers to analyze
1334c6fd2807SJeff Garzik  *
1335c6fd2807SJeff Garzik  *	Analyze taskfile of @qc and further determine cause of
1336c6fd2807SJeff Garzik  *	failure.  This function also requests ATAPI sense data if
1337c6fd2807SJeff Garzik  *	avaliable.
1338c6fd2807SJeff Garzik  *
1339c6fd2807SJeff Garzik  *	LOCKING:
1340c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1341c6fd2807SJeff Garzik  *
1342c6fd2807SJeff Garzik  *	RETURNS:
1343c6fd2807SJeff Garzik  *	Determined recovery action
1344c6fd2807SJeff Garzik  */
1345c6fd2807SJeff Garzik static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1346c6fd2807SJeff Garzik 				      const struct ata_taskfile *tf)
1347c6fd2807SJeff Garzik {
1348c6fd2807SJeff Garzik 	unsigned int tmp, action = 0;
1349c6fd2807SJeff Garzik 	u8 stat = tf->command, err = tf->feature;
1350c6fd2807SJeff Garzik 
1351c6fd2807SJeff Garzik 	if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1352c6fd2807SJeff Garzik 		qc->err_mask |= AC_ERR_HSM;
1353c6fd2807SJeff Garzik 		return ATA_EH_SOFTRESET;
1354c6fd2807SJeff Garzik 	}
1355c6fd2807SJeff Garzik 
1356a51d644aSTejun Heo 	if (stat & (ATA_ERR | ATA_DF))
1357a51d644aSTejun Heo 		qc->err_mask |= AC_ERR_DEV;
1358a51d644aSTejun Heo 	else
1359c6fd2807SJeff Garzik 		return 0;
1360c6fd2807SJeff Garzik 
1361c6fd2807SJeff Garzik 	switch (qc->dev->class) {
1362c6fd2807SJeff Garzik 	case ATA_DEV_ATA:
1363c6fd2807SJeff Garzik 		if (err & ATA_ICRC)
1364c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_ATA_BUS;
1365c6fd2807SJeff Garzik 		if (err & ATA_UNC)
1366c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_MEDIA;
1367c6fd2807SJeff Garzik 		if (err & ATA_IDNF)
1368c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_INVALID;
1369c6fd2807SJeff Garzik 		break;
1370c6fd2807SJeff Garzik 
1371c6fd2807SJeff Garzik 	case ATA_DEV_ATAPI:
1372a569a30dSTejun Heo 		if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
137356287768SAlbert Lee 			tmp = atapi_eh_request_sense(qc);
1374c6fd2807SJeff Garzik 			if (!tmp) {
1375a569a30dSTejun Heo 				/* ATA_QCFLAG_SENSE_VALID is used to
1376a569a30dSTejun Heo 				 * tell atapi_qc_complete() that sense
1377a569a30dSTejun Heo 				 * data is already valid.
1378c6fd2807SJeff Garzik 				 *
1379c6fd2807SJeff Garzik 				 * TODO: interpret sense data and set
1380c6fd2807SJeff Garzik 				 * appropriate err_mask.
1381c6fd2807SJeff Garzik 				 */
1382c6fd2807SJeff Garzik 				qc->flags |= ATA_QCFLAG_SENSE_VALID;
1383c6fd2807SJeff Garzik 			} else
1384c6fd2807SJeff Garzik 				qc->err_mask |= tmp;
1385c6fd2807SJeff Garzik 		}
1386a569a30dSTejun Heo 	}
1387c6fd2807SJeff Garzik 
1388c6fd2807SJeff Garzik 	if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1389c6fd2807SJeff Garzik 		action |= ATA_EH_SOFTRESET;
1390c6fd2807SJeff Garzik 
1391c6fd2807SJeff Garzik 	return action;
1392c6fd2807SJeff Garzik }
1393c6fd2807SJeff Garzik 
13947d47e8d4STejun Heo static int ata_eh_categorize_error(int is_io, unsigned int err_mask)
1395c6fd2807SJeff Garzik {
13967d47e8d4STejun Heo 	if (err_mask & AC_ERR_ATA_BUS)
1397c6fd2807SJeff Garzik 		return 1;
1398c6fd2807SJeff Garzik 
13997d47e8d4STejun Heo 	if (err_mask & AC_ERR_TIMEOUT)
1400c6fd2807SJeff Garzik 		return 2;
14017d47e8d4STejun Heo 
14027d47e8d4STejun Heo 	if (is_io) {
14037d47e8d4STejun Heo 		if (err_mask & AC_ERR_HSM)
14047d47e8d4STejun Heo 			return 2;
14057d47e8d4STejun Heo 		if ((err_mask &
14067d47e8d4STejun Heo 		     (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
14077d47e8d4STejun Heo 			return 3;
1408c6fd2807SJeff Garzik 	}
1409c6fd2807SJeff Garzik 
1410c6fd2807SJeff Garzik 	return 0;
1411c6fd2807SJeff Garzik }
1412c6fd2807SJeff Garzik 
14137d47e8d4STejun Heo struct speed_down_verdict_arg {
1414c6fd2807SJeff Garzik 	u64 since;
14157d47e8d4STejun Heo 	int nr_errors[4];
1416c6fd2807SJeff Garzik };
1417c6fd2807SJeff Garzik 
14187d47e8d4STejun Heo static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
1419c6fd2807SJeff Garzik {
14207d47e8d4STejun Heo 	struct speed_down_verdict_arg *arg = void_arg;
14217d47e8d4STejun Heo 	int cat = ata_eh_categorize_error(ent->is_io, ent->err_mask);
1422c6fd2807SJeff Garzik 
1423c6fd2807SJeff Garzik 	if (ent->timestamp < arg->since)
1424c6fd2807SJeff Garzik 		return -1;
1425c6fd2807SJeff Garzik 
14267d47e8d4STejun Heo 	arg->nr_errors[cat]++;
1427c6fd2807SJeff Garzik 	return 0;
1428c6fd2807SJeff Garzik }
1429c6fd2807SJeff Garzik 
1430c6fd2807SJeff Garzik /**
14317d47e8d4STejun Heo  *	ata_eh_speed_down_verdict - Determine speed down verdict
1432c6fd2807SJeff Garzik  *	@dev: Device of interest
1433c6fd2807SJeff Garzik  *
1434c6fd2807SJeff Garzik  *	This function examines error ring of @dev and determines
14357d47e8d4STejun Heo  *	whether NCQ needs to be turned off, transfer speed should be
14367d47e8d4STejun Heo  *	stepped down, or falling back to PIO is necessary.
1437c6fd2807SJeff Garzik  *
14387d47e8d4STejun Heo  *	Cat-1 is ATA_BUS error for any command.
1439c6fd2807SJeff Garzik  *
14407d47e8d4STejun Heo  *	Cat-2 is TIMEOUT for any command or HSM violation for known
14417d47e8d4STejun Heo  *	supported commands.
14427d47e8d4STejun Heo  *
14437d47e8d4STejun Heo  *	Cat-3 is is unclassified DEV error for known supported
1444c6fd2807SJeff Garzik  *	command.
1445c6fd2807SJeff Garzik  *
14467d47e8d4STejun Heo  *	NCQ needs to be turned off if there have been more than 3
14477d47e8d4STejun Heo  *	Cat-2 + Cat-3 errors during last 10 minutes.
14487d47e8d4STejun Heo  *
14497d47e8d4STejun Heo  *	Speed down is necessary if there have been more than 3 Cat-1 +
14507d47e8d4STejun Heo  *	Cat-2 errors or 10 Cat-3 errors during last 10 minutes.
14517d47e8d4STejun Heo  *
14527d47e8d4STejun Heo  *	Falling back to PIO mode is necessary if there have been more
14537d47e8d4STejun Heo  *	than 10 Cat-1 + Cat-2 + Cat-3 errors during last 5 minutes.
14547d47e8d4STejun Heo  *
1455c6fd2807SJeff Garzik  *	LOCKING:
1456c6fd2807SJeff Garzik  *	Inherited from caller.
1457c6fd2807SJeff Garzik  *
1458c6fd2807SJeff Garzik  *	RETURNS:
14597d47e8d4STejun Heo  *	OR of ATA_EH_SPDN_* flags.
1460c6fd2807SJeff Garzik  */
14617d47e8d4STejun Heo static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
1462c6fd2807SJeff Garzik {
14637d47e8d4STejun Heo 	const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
14647d47e8d4STejun Heo 	u64 j64 = get_jiffies_64();
14657d47e8d4STejun Heo 	struct speed_down_verdict_arg arg;
14667d47e8d4STejun Heo 	unsigned int verdict = 0;
1467c6fd2807SJeff Garzik 
14687d47e8d4STejun Heo 	/* scan past 10 mins of error history */
1469c6fd2807SJeff Garzik 	memset(&arg, 0, sizeof(arg));
14707d47e8d4STejun Heo 	arg.since = j64 - min(j64, j10mins);
14717d47e8d4STejun Heo 	ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1472c6fd2807SJeff Garzik 
14737d47e8d4STejun Heo 	if (arg.nr_errors[2] + arg.nr_errors[3] > 3)
14747d47e8d4STejun Heo 		verdict |= ATA_EH_SPDN_NCQ_OFF;
14757d47e8d4STejun Heo 	if (arg.nr_errors[1] + arg.nr_errors[2] > 3 || arg.nr_errors[3] > 10)
14767d47e8d4STejun Heo 		verdict |= ATA_EH_SPDN_SPEED_DOWN;
1477c6fd2807SJeff Garzik 
14787d47e8d4STejun Heo 	/* scan past 3 mins of error history */
14797d47e8d4STejun Heo 	memset(&arg, 0, sizeof(arg));
14807d47e8d4STejun Heo 	arg.since = j64 - min(j64, j5mins);
14817d47e8d4STejun Heo 	ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1482c6fd2807SJeff Garzik 
14837d47e8d4STejun Heo 	if (arg.nr_errors[1] + arg.nr_errors[2] + arg.nr_errors[3] > 10)
14847d47e8d4STejun Heo 		verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
14857d47e8d4STejun Heo 
14867d47e8d4STejun Heo 	return verdict;
1487c6fd2807SJeff Garzik }
1488c6fd2807SJeff Garzik 
1489c6fd2807SJeff Garzik /**
1490c6fd2807SJeff Garzik  *	ata_eh_speed_down - record error and speed down if necessary
1491c6fd2807SJeff Garzik  *	@dev: Failed device
1492c6fd2807SJeff Garzik  *	@is_io: Did the device fail during normal IO?
1493c6fd2807SJeff Garzik  *	@err_mask: err_mask of the error
1494c6fd2807SJeff Garzik  *
1495c6fd2807SJeff Garzik  *	Record error and examine error history to determine whether
1496c6fd2807SJeff Garzik  *	adjusting transmission speed is necessary.  It also sets
1497c6fd2807SJeff Garzik  *	transmission limits appropriately if such adjustment is
1498c6fd2807SJeff Garzik  *	necessary.
1499c6fd2807SJeff Garzik  *
1500c6fd2807SJeff Garzik  *	LOCKING:
1501c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1502c6fd2807SJeff Garzik  *
1503c6fd2807SJeff Garzik  *	RETURNS:
15047d47e8d4STejun Heo  *	Determined recovery action.
1505c6fd2807SJeff Garzik  */
15067d47e8d4STejun Heo static unsigned int ata_eh_speed_down(struct ata_device *dev, int is_io,
1507c6fd2807SJeff Garzik 				      unsigned int err_mask)
1508c6fd2807SJeff Garzik {
15097d47e8d4STejun Heo 	unsigned int verdict;
15107d47e8d4STejun Heo 	unsigned int action = 0;
15117d47e8d4STejun Heo 
15127d47e8d4STejun Heo 	/* don't bother if Cat-0 error */
15137d47e8d4STejun Heo 	if (ata_eh_categorize_error(is_io, err_mask) == 0)
1514c6fd2807SJeff Garzik 		return 0;
1515c6fd2807SJeff Garzik 
1516c6fd2807SJeff Garzik 	/* record error and determine whether speed down is necessary */
1517c6fd2807SJeff Garzik 	ata_ering_record(&dev->ering, is_io, err_mask);
15187d47e8d4STejun Heo 	verdict = ata_eh_speed_down_verdict(dev);
1519c6fd2807SJeff Garzik 
15207d47e8d4STejun Heo 	/* turn off NCQ? */
15217d47e8d4STejun Heo 	if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
15227d47e8d4STejun Heo 	    (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
15237d47e8d4STejun Heo 			   ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
15247d47e8d4STejun Heo 		dev->flags |= ATA_DFLAG_NCQ_OFF;
15257d47e8d4STejun Heo 		ata_dev_printk(dev, KERN_WARNING,
15267d47e8d4STejun Heo 			       "NCQ disabled due to excessive errors\n");
15277d47e8d4STejun Heo 		goto done;
15287d47e8d4STejun Heo 	}
1529c6fd2807SJeff Garzik 
15307d47e8d4STejun Heo 	/* speed down? */
15317d47e8d4STejun Heo 	if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1532c6fd2807SJeff Garzik 		/* speed down SATA link speed if possible */
1533936fd732STejun Heo 		if (sata_down_spd_limit(dev->link) == 0) {
15347d47e8d4STejun Heo 			action |= ATA_EH_HARDRESET;
15357d47e8d4STejun Heo 			goto done;
15367d47e8d4STejun Heo 		}
1537c6fd2807SJeff Garzik 
1538c6fd2807SJeff Garzik 		/* lower transfer mode */
15397d47e8d4STejun Heo 		if (dev->spdn_cnt < 2) {
15407d47e8d4STejun Heo 			static const int dma_dnxfer_sel[] =
15417d47e8d4STejun Heo 				{ ATA_DNXFER_DMA, ATA_DNXFER_40C };
15427d47e8d4STejun Heo 			static const int pio_dnxfer_sel[] =
15437d47e8d4STejun Heo 				{ ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
15447d47e8d4STejun Heo 			int sel;
1545c6fd2807SJeff Garzik 
15467d47e8d4STejun Heo 			if (dev->xfer_shift != ATA_SHIFT_PIO)
15477d47e8d4STejun Heo 				sel = dma_dnxfer_sel[dev->spdn_cnt];
15487d47e8d4STejun Heo 			else
15497d47e8d4STejun Heo 				sel = pio_dnxfer_sel[dev->spdn_cnt];
15507d47e8d4STejun Heo 
15517d47e8d4STejun Heo 			dev->spdn_cnt++;
15527d47e8d4STejun Heo 
15537d47e8d4STejun Heo 			if (ata_down_xfermask_limit(dev, sel) == 0) {
15547d47e8d4STejun Heo 				action |= ATA_EH_SOFTRESET;
15557d47e8d4STejun Heo 				goto done;
15567d47e8d4STejun Heo 			}
15577d47e8d4STejun Heo 		}
15587d47e8d4STejun Heo 	}
15597d47e8d4STejun Heo 
15607d47e8d4STejun Heo 	/* Fall back to PIO?  Slowing down to PIO is meaningless for
15617d47e8d4STejun Heo 	 * SATA.  Consider it only for PATA.
15627d47e8d4STejun Heo 	 */
15637d47e8d4STejun Heo 	if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
15649af5c9c9STejun Heo 	    (dev->link->ap->cbl != ATA_CBL_SATA) &&
15657d47e8d4STejun Heo 	    (dev->xfer_shift != ATA_SHIFT_PIO)) {
15667d47e8d4STejun Heo 		if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
15677d47e8d4STejun Heo 			dev->spdn_cnt = 0;
15687d47e8d4STejun Heo 			action |= ATA_EH_SOFTRESET;
15697d47e8d4STejun Heo 			goto done;
15707d47e8d4STejun Heo 		}
15717d47e8d4STejun Heo 	}
15727d47e8d4STejun Heo 
1573c6fd2807SJeff Garzik 	return 0;
15747d47e8d4STejun Heo  done:
15757d47e8d4STejun Heo 	/* device has been slowed down, blow error history */
15767d47e8d4STejun Heo 	ata_ering_clear(&dev->ering);
15777d47e8d4STejun Heo 	return action;
1578c6fd2807SJeff Garzik }
1579c6fd2807SJeff Garzik 
1580c6fd2807SJeff Garzik /**
1581*9b1e2658STejun Heo  *	ata_eh_link_autopsy - analyze error and determine recovery action
1582*9b1e2658STejun Heo  *	@link: host link to perform autopsy on
1583c6fd2807SJeff Garzik  *
15840260731fSTejun Heo  *	Analyze why @link failed and determine which recovery actions
15850260731fSTejun Heo  *	are needed.  This function also sets more detailed AC_ERR_*
15860260731fSTejun Heo  *	values and fills sense data for ATAPI CHECK SENSE.
1587c6fd2807SJeff Garzik  *
1588c6fd2807SJeff Garzik  *	LOCKING:
1589c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1590c6fd2807SJeff Garzik  */
1591*9b1e2658STejun Heo static void ata_eh_link_autopsy(struct ata_link *link)
1592c6fd2807SJeff Garzik {
15930260731fSTejun Heo 	struct ata_port *ap = link->ap;
1594936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
1595c6fd2807SJeff Garzik 	unsigned int all_err_mask = 0;
1596c6fd2807SJeff Garzik 	int tag, is_io = 0;
1597c6fd2807SJeff Garzik 	u32 serror;
1598c6fd2807SJeff Garzik 	int rc;
1599c6fd2807SJeff Garzik 
1600c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
1601c6fd2807SJeff Garzik 
1602c6fd2807SJeff Garzik 	if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1603c6fd2807SJeff Garzik 		return;
1604c6fd2807SJeff Garzik 
1605c6fd2807SJeff Garzik 	/* obtain and analyze SError */
1606936fd732STejun Heo 	rc = sata_scr_read(link, SCR_ERROR, &serror);
1607c6fd2807SJeff Garzik 	if (rc == 0) {
1608c6fd2807SJeff Garzik 		ehc->i.serror |= serror;
16090260731fSTejun Heo 		ata_eh_analyze_serror(link);
16104e57c517STejun Heo 	} else if (rc != -EOPNOTSUPP) {
16114e57c517STejun Heo 		/* SError read failed, force hardreset and probing */
16124e57c517STejun Heo 		ata_ehi_schedule_probe(&ehc->i);
1613c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_HARDRESET;
16144e57c517STejun Heo 		ehc->i.err_mask |= AC_ERR_OTHER;
16154e57c517STejun Heo 	}
1616c6fd2807SJeff Garzik 
1617c6fd2807SJeff Garzik 	/* analyze NCQ failure */
16180260731fSTejun Heo 	ata_eh_analyze_ncq_error(link);
1619c6fd2807SJeff Garzik 
1620c6fd2807SJeff Garzik 	/* any real error trumps AC_ERR_OTHER */
1621c6fd2807SJeff Garzik 	if (ehc->i.err_mask & ~AC_ERR_OTHER)
1622c6fd2807SJeff Garzik 		ehc->i.err_mask &= ~AC_ERR_OTHER;
1623c6fd2807SJeff Garzik 
1624c6fd2807SJeff Garzik 	all_err_mask |= ehc->i.err_mask;
1625c6fd2807SJeff Garzik 
1626c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1627c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1628c6fd2807SJeff Garzik 
16290260731fSTejun Heo 		if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
1630c6fd2807SJeff Garzik 			continue;
1631c6fd2807SJeff Garzik 
1632c6fd2807SJeff Garzik 		/* inherit upper level err_mask */
1633c6fd2807SJeff Garzik 		qc->err_mask |= ehc->i.err_mask;
1634c6fd2807SJeff Garzik 
1635c6fd2807SJeff Garzik 		/* analyze TF */
1636c6fd2807SJeff Garzik 		ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1637c6fd2807SJeff Garzik 
1638c6fd2807SJeff Garzik 		/* DEV errors are probably spurious in case of ATA_BUS error */
1639c6fd2807SJeff Garzik 		if (qc->err_mask & AC_ERR_ATA_BUS)
1640c6fd2807SJeff Garzik 			qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1641c6fd2807SJeff Garzik 					  AC_ERR_INVALID);
1642c6fd2807SJeff Garzik 
1643c6fd2807SJeff Garzik 		/* any real error trumps unknown error */
1644c6fd2807SJeff Garzik 		if (qc->err_mask & ~AC_ERR_OTHER)
1645c6fd2807SJeff Garzik 			qc->err_mask &= ~AC_ERR_OTHER;
1646c6fd2807SJeff Garzik 
1647c6fd2807SJeff Garzik 		/* SENSE_VALID trumps dev/unknown error and revalidation */
1648c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1649c6fd2807SJeff Garzik 			qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1650c6fd2807SJeff Garzik 			ehc->i.action &= ~ATA_EH_REVALIDATE;
1651c6fd2807SJeff Garzik 		}
1652c6fd2807SJeff Garzik 
1653c6fd2807SJeff Garzik 		/* accumulate error info */
1654c6fd2807SJeff Garzik 		ehc->i.dev = qc->dev;
1655c6fd2807SJeff Garzik 		all_err_mask |= qc->err_mask;
1656c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_IO)
1657c6fd2807SJeff Garzik 			is_io = 1;
1658c6fd2807SJeff Garzik 	}
1659c6fd2807SJeff Garzik 
1660c6fd2807SJeff Garzik 	/* enforce default EH actions */
1661c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN ||
1662c6fd2807SJeff Garzik 	    all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
1663c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_SOFTRESET;
1664c6fd2807SJeff Garzik 	else if (all_err_mask)
1665c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_REVALIDATE;
1666c6fd2807SJeff Garzik 
1667c6fd2807SJeff Garzik 	/* if we have offending qcs and the associated failed device */
1668c6fd2807SJeff Garzik 	if (ehc->i.dev) {
1669c6fd2807SJeff Garzik 		/* speed down */
1670c6fd2807SJeff Garzik 		ehc->i.action |= ata_eh_speed_down(ehc->i.dev, is_io,
1671c6fd2807SJeff Garzik 						   all_err_mask);
1672c6fd2807SJeff Garzik 
1673c6fd2807SJeff Garzik 		/* perform per-dev EH action only on the offending device */
1674c6fd2807SJeff Garzik 		ehc->i.dev_action[ehc->i.dev->devno] |=
1675c6fd2807SJeff Garzik 			ehc->i.action & ATA_EH_PERDEV_MASK;
1676c6fd2807SJeff Garzik 		ehc->i.action &= ~ATA_EH_PERDEV_MASK;
1677c6fd2807SJeff Garzik 	}
1678c6fd2807SJeff Garzik 
1679c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
1680c6fd2807SJeff Garzik }
1681c6fd2807SJeff Garzik 
1682c6fd2807SJeff Garzik /**
1683*9b1e2658STejun Heo  *	ata_eh_autopsy - analyze error and determine recovery action
1684*9b1e2658STejun Heo  *	@ap: host port to perform autopsy on
1685*9b1e2658STejun Heo  *
1686*9b1e2658STejun Heo  *	Analyze all links of @ap and determine why they failed and
1687*9b1e2658STejun Heo  *	which recovery actions are needed.
1688*9b1e2658STejun Heo  *
1689*9b1e2658STejun Heo  *	LOCKING:
1690*9b1e2658STejun Heo  *	Kernel thread context (may sleep).
1691*9b1e2658STejun Heo  */
1692*9b1e2658STejun Heo static void ata_eh_autopsy(struct ata_port *ap)
1693*9b1e2658STejun Heo {
1694*9b1e2658STejun Heo 	struct ata_link *link;
1695*9b1e2658STejun Heo 
1696*9b1e2658STejun Heo 	__ata_port_for_each_link(link, ap)
1697*9b1e2658STejun Heo 		ata_eh_link_autopsy(link);
1698*9b1e2658STejun Heo }
1699*9b1e2658STejun Heo 
1700*9b1e2658STejun Heo /**
1701*9b1e2658STejun Heo  *	ata_eh_link_report - report error handling to user
17020260731fSTejun Heo  *	@link: ATA link EH is going on
1703c6fd2807SJeff Garzik  *
1704c6fd2807SJeff Garzik  *	Report EH to user.
1705c6fd2807SJeff Garzik  *
1706c6fd2807SJeff Garzik  *	LOCKING:
1707c6fd2807SJeff Garzik  *	None.
1708c6fd2807SJeff Garzik  */
1709*9b1e2658STejun Heo static void ata_eh_link_report(struct ata_link *link)
1710c6fd2807SJeff Garzik {
17110260731fSTejun Heo 	struct ata_port *ap = link->ap;
17120260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
1713c6fd2807SJeff Garzik 	const char *frozen, *desc;
1714c6fd2807SJeff Garzik 	int tag, nr_failed = 0;
1715c6fd2807SJeff Garzik 
1716c6fd2807SJeff Garzik 	desc = NULL;
1717c6fd2807SJeff Garzik 	if (ehc->i.desc[0] != '\0')
1718c6fd2807SJeff Garzik 		desc = ehc->i.desc;
1719c6fd2807SJeff Garzik 
1720c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1721c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1722c6fd2807SJeff Garzik 
17230260731fSTejun Heo 		if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
1724c6fd2807SJeff Garzik 			continue;
1725c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1726c6fd2807SJeff Garzik 			continue;
1727c6fd2807SJeff Garzik 
1728c6fd2807SJeff Garzik 		nr_failed++;
1729c6fd2807SJeff Garzik 	}
1730c6fd2807SJeff Garzik 
1731c6fd2807SJeff Garzik 	if (!nr_failed && !ehc->i.err_mask)
1732c6fd2807SJeff Garzik 		return;
1733c6fd2807SJeff Garzik 
1734c6fd2807SJeff Garzik 	frozen = "";
1735c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN)
1736c6fd2807SJeff Garzik 		frozen = " frozen";
1737c6fd2807SJeff Garzik 
1738c6fd2807SJeff Garzik 	if (ehc->i.dev) {
1739c6fd2807SJeff Garzik 		ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1740c6fd2807SJeff Garzik 			       "SAct 0x%x SErr 0x%x action 0x%x%s\n",
17410260731fSTejun Heo 			       ehc->i.err_mask, link->sactive,
17429af5c9c9STejun Heo 			       ehc->i.serror, ehc->i.action, frozen);
1743c6fd2807SJeff Garzik 		if (desc)
1744b64bbc39STejun Heo 			ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
1745c6fd2807SJeff Garzik 	} else {
17460260731fSTejun Heo 		ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
1747c6fd2807SJeff Garzik 				"SAct 0x%x SErr 0x%x action 0x%x%s\n",
17480260731fSTejun Heo 				ehc->i.err_mask, link->sactive,
17499af5c9c9STejun Heo 				ehc->i.serror, ehc->i.action, frozen);
1750c6fd2807SJeff Garzik 		if (desc)
17510260731fSTejun Heo 			ata_link_printk(link, KERN_ERR, "%s\n", desc);
1752c6fd2807SJeff Garzik 	}
1753c6fd2807SJeff Garzik 
1754c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
17558a937581STejun Heo 		static const char *dma_str[] = {
17568a937581STejun Heo 			[DMA_BIDIRECTIONAL]	= "bidi",
17578a937581STejun Heo 			[DMA_TO_DEVICE]		= "out",
17588a937581STejun Heo 			[DMA_FROM_DEVICE]	= "in",
17598a937581STejun Heo 			[DMA_NONE]		= "",
17608a937581STejun Heo 		};
1761c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
17628a937581STejun Heo 		struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
1763c6fd2807SJeff Garzik 
17640260731fSTejun Heo 		if (!(qc->flags & ATA_QCFLAG_FAILED) ||
17650260731fSTejun Heo 		    qc->dev->link != link || !qc->err_mask)
1766c6fd2807SJeff Garzik 			continue;
1767c6fd2807SJeff Garzik 
17688a937581STejun Heo 		ata_dev_printk(qc->dev, KERN_ERR,
17698a937581STejun Heo 			"cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
1770664e8503STejun Heo 			"tag %d cdb 0x%x data %u %s\n         "
17718a937581STejun Heo 			"res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
17725335b729STejun Heo 			"Emask 0x%x (%s)%s\n",
17738a937581STejun Heo 			cmd->command, cmd->feature, cmd->nsect,
17748a937581STejun Heo 			cmd->lbal, cmd->lbam, cmd->lbah,
17758a937581STejun Heo 			cmd->hob_feature, cmd->hob_nsect,
17768a937581STejun Heo 			cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
1777726f0785STejun Heo 			cmd->device, qc->tag, qc->cdb[0], qc->nbytes,
1778664e8503STejun Heo 			dma_str[qc->dma_dir],
17798a937581STejun Heo 			res->command, res->feature, res->nsect,
17808a937581STejun Heo 			res->lbal, res->lbam, res->lbah,
17818a937581STejun Heo 			res->hob_feature, res->hob_nsect,
17828a937581STejun Heo 			res->hob_lbal, res->hob_lbam, res->hob_lbah,
17835335b729STejun Heo 			res->device, qc->err_mask, ata_err_string(qc->err_mask),
17845335b729STejun Heo 			qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
1785c6fd2807SJeff Garzik 	}
1786c6fd2807SJeff Garzik }
1787c6fd2807SJeff Garzik 
1788*9b1e2658STejun Heo /**
1789*9b1e2658STejun Heo  *	ata_eh_report - report error handling to user
1790*9b1e2658STejun Heo  *	@ap: ATA port to report EH about
1791*9b1e2658STejun Heo  *
1792*9b1e2658STejun Heo  *	Report EH to user.
1793*9b1e2658STejun Heo  *
1794*9b1e2658STejun Heo  *	LOCKING:
1795*9b1e2658STejun Heo  *	None.
1796*9b1e2658STejun Heo  */
1797*9b1e2658STejun Heo static void ata_eh_report(struct ata_port *ap)
1798*9b1e2658STejun Heo {
1799*9b1e2658STejun Heo 	struct ata_link *link;
1800*9b1e2658STejun Heo 
1801*9b1e2658STejun Heo 	__ata_port_for_each_link(link, ap)
1802*9b1e2658STejun Heo 		ata_eh_link_report(link);
1803*9b1e2658STejun Heo }
1804*9b1e2658STejun Heo 
1805cc0680a5STejun Heo static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
1806d4b2bab4STejun Heo 			unsigned int *classes, unsigned long deadline)
1807c6fd2807SJeff Garzik {
1808f58229f8STejun Heo 	struct ata_device *dev;
1809f58229f8STejun Heo 	int rc;
1810c6fd2807SJeff Garzik 
1811cc0680a5STejun Heo 	ata_link_for_each_dev(dev, link)
1812f58229f8STejun Heo 		classes[dev->devno] = ATA_DEV_UNKNOWN;
1813c6fd2807SJeff Garzik 
1814cc0680a5STejun Heo 	rc = reset(link, classes, deadline);
1815c6fd2807SJeff Garzik 	if (rc)
1816c6fd2807SJeff Garzik 		return rc;
1817c6fd2807SJeff Garzik 
1818c6fd2807SJeff Garzik 	/* If any class isn't ATA_DEV_UNKNOWN, consider classification
1819c6fd2807SJeff Garzik 	 * is complete and convert all ATA_DEV_UNKNOWN to
1820c6fd2807SJeff Garzik 	 * ATA_DEV_NONE.
1821c6fd2807SJeff Garzik 	 */
1822cc0680a5STejun Heo 	ata_link_for_each_dev(dev, link)
1823f58229f8STejun Heo 		if (classes[dev->devno] != ATA_DEV_UNKNOWN)
1824c6fd2807SJeff Garzik 			break;
1825c6fd2807SJeff Garzik 
1826f58229f8STejun Heo 	if (dev) {
1827cc0680a5STejun Heo 		ata_link_for_each_dev(dev, link) {
1828f58229f8STejun Heo 			if (classes[dev->devno] == ATA_DEV_UNKNOWN)
1829f58229f8STejun Heo 				classes[dev->devno] = ATA_DEV_NONE;
1830f58229f8STejun Heo 		}
1831f58229f8STejun Heo 	}
1832c6fd2807SJeff Garzik 
1833c6fd2807SJeff Garzik 	return 0;
1834c6fd2807SJeff Garzik }
1835c6fd2807SJeff Garzik 
1836c6fd2807SJeff Garzik static int ata_eh_followup_srst_needed(int rc, int classify,
1837c6fd2807SJeff Garzik 				       const unsigned int *classes)
1838c6fd2807SJeff Garzik {
1839c6fd2807SJeff Garzik 	if (rc == -EAGAIN)
1840c6fd2807SJeff Garzik 		return 1;
1841c6fd2807SJeff Garzik 	if (rc != 0)
1842c6fd2807SJeff Garzik 		return 0;
1843c6fd2807SJeff Garzik 	if (classify && classes[0] == ATA_DEV_UNKNOWN)
1844c6fd2807SJeff Garzik 		return 1;
1845c6fd2807SJeff Garzik 	return 0;
1846c6fd2807SJeff Garzik }
1847c6fd2807SJeff Garzik 
1848cc0680a5STejun Heo static int ata_eh_reset(struct ata_link *link, int classify,
1849c6fd2807SJeff Garzik 			ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
1850c6fd2807SJeff Garzik 			ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
1851c6fd2807SJeff Garzik {
1852936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
1853c6fd2807SJeff Garzik 	unsigned int *classes = ehc->classes;
1854c6fd2807SJeff Garzik 	int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
185531daabdaSTejun Heo 	int try = 0;
1856f58229f8STejun Heo 	struct ata_device *dev;
185731daabdaSTejun Heo 	unsigned long deadline;
1858c6fd2807SJeff Garzik 	unsigned int action;
1859c6fd2807SJeff Garzik 	ata_reset_fn_t reset;
1860f58229f8STejun Heo 	int rc;
1861c6fd2807SJeff Garzik 
1862c6fd2807SJeff Garzik 	/* about to reset */
1863955e57dfSTejun Heo 	ata_eh_about_to_do(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
1864c6fd2807SJeff Garzik 
1865c6fd2807SJeff Garzik 	/* Determine which reset to use and record in ehc->i.action.
1866c6fd2807SJeff Garzik 	 * prereset() may examine and modify it.
1867c6fd2807SJeff Garzik 	 */
1868c6fd2807SJeff Garzik 	action = ehc->i.action;
1869c6fd2807SJeff Garzik 	ehc->i.action &= ~ATA_EH_RESET_MASK;
1870936fd732STejun Heo 	if (softreset && (!hardreset || (!sata_set_spd_needed(link) &&
1871c6fd2807SJeff Garzik 					 !(action & ATA_EH_HARDRESET))))
1872c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_SOFTRESET;
1873c6fd2807SJeff Garzik 	else
1874c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_HARDRESET;
1875c6fd2807SJeff Garzik 
1876c6fd2807SJeff Garzik 	if (prereset) {
1877cc0680a5STejun Heo 		rc = prereset(link, jiffies + ATA_EH_PRERESET_TIMEOUT);
1878c6fd2807SJeff Garzik 		if (rc) {
1879c961922bSAlan Cox 			if (rc == -ENOENT) {
1880cc0680a5STejun Heo 				ata_link_printk(link, KERN_DEBUG,
18814aa9ab67STejun Heo 						"port disabled. ignoring.\n");
18829af5c9c9STejun Heo 				ehc->i.action &= ~ATA_EH_RESET_MASK;
18834aa9ab67STejun Heo 
1884936fd732STejun Heo 				ata_link_for_each_dev(dev, link)
1885f58229f8STejun Heo 					classes[dev->devno] = ATA_DEV_NONE;
18864aa9ab67STejun Heo 
18874aa9ab67STejun Heo 				rc = 0;
1888c961922bSAlan Cox 			} else
1889cc0680a5STejun Heo 				ata_link_printk(link, KERN_ERR,
1890c6fd2807SJeff Garzik 					"prereset failed (errno=%d)\n", rc);
1891fccb6ea5STejun Heo 			goto out;
1892c6fd2807SJeff Garzik 		}
1893c6fd2807SJeff Garzik 	}
1894c6fd2807SJeff Garzik 
1895c6fd2807SJeff Garzik 	/* prereset() might have modified ehc->i.action */
1896c6fd2807SJeff Garzik 	if (ehc->i.action & ATA_EH_HARDRESET)
1897c6fd2807SJeff Garzik 		reset = hardreset;
1898c6fd2807SJeff Garzik 	else if (ehc->i.action & ATA_EH_SOFTRESET)
1899c6fd2807SJeff Garzik 		reset = softreset;
1900c6fd2807SJeff Garzik 	else {
1901c6fd2807SJeff Garzik 		/* prereset told us not to reset, bang classes and return */
1902936fd732STejun Heo 		ata_link_for_each_dev(dev, link)
1903f58229f8STejun Heo 			classes[dev->devno] = ATA_DEV_NONE;
1904fccb6ea5STejun Heo 		rc = 0;
1905fccb6ea5STejun Heo 		goto out;
1906c6fd2807SJeff Garzik 	}
1907c6fd2807SJeff Garzik 
1908c6fd2807SJeff Garzik 	/* did prereset() screw up?  if so, fix up to avoid oopsing */
1909c6fd2807SJeff Garzik 	if (!reset) {
1910c6fd2807SJeff Garzik 		if (softreset)
1911c6fd2807SJeff Garzik 			reset = softreset;
1912c6fd2807SJeff Garzik 		else
1913c6fd2807SJeff Garzik 			reset = hardreset;
1914c6fd2807SJeff Garzik 	}
1915c6fd2807SJeff Garzik 
1916c6fd2807SJeff Garzik  retry:
191731daabdaSTejun Heo 	deadline = jiffies + ata_eh_reset_timeouts[try++];
191831daabdaSTejun Heo 
1919c6fd2807SJeff Garzik 	/* shut up during boot probing */
1920c6fd2807SJeff Garzik 	if (verbose)
1921cc0680a5STejun Heo 		ata_link_printk(link, KERN_INFO, "%s resetting link\n",
1922c6fd2807SJeff Garzik 				reset == softreset ? "soft" : "hard");
1923c6fd2807SJeff Garzik 
1924c6fd2807SJeff Garzik 	/* mark that this EH session started with reset */
19250d64a233STejun Heo 	if (reset == hardreset)
19260d64a233STejun Heo 		ehc->i.flags |= ATA_EHI_DID_HARDRESET;
19270d64a233STejun Heo 	else
19280d64a233STejun Heo 		ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
1929c6fd2807SJeff Garzik 
1930cc0680a5STejun Heo 	rc = ata_do_reset(link, reset, classes, deadline);
1931c6fd2807SJeff Garzik 
1932c6fd2807SJeff Garzik 	if (reset == hardreset &&
1933c6fd2807SJeff Garzik 	    ata_eh_followup_srst_needed(rc, classify, classes)) {
1934c6fd2807SJeff Garzik 		/* okay, let's do follow-up softreset */
1935c6fd2807SJeff Garzik 		reset = softreset;
1936c6fd2807SJeff Garzik 
1937c6fd2807SJeff Garzik 		if (!reset) {
1938cc0680a5STejun Heo 			ata_link_printk(link, KERN_ERR,
1939c6fd2807SJeff Garzik 					"follow-up softreset required "
1940c6fd2807SJeff Garzik 					"but no softreset avaliable\n");
1941fccb6ea5STejun Heo 			rc = -EINVAL;
1942fccb6ea5STejun Heo 			goto out;
1943c6fd2807SJeff Garzik 		}
1944c6fd2807SJeff Garzik 
1945955e57dfSTejun Heo 		ata_eh_about_to_do(link, NULL, ATA_EH_RESET_MASK);
1946cc0680a5STejun Heo 		rc = ata_do_reset(link, reset, classes, deadline);
1947c6fd2807SJeff Garzik 
1948c6fd2807SJeff Garzik 		if (rc == 0 && classify &&
1949c6fd2807SJeff Garzik 		    classes[0] == ATA_DEV_UNKNOWN) {
1950cc0680a5STejun Heo 			ata_link_printk(link, KERN_ERR,
1951c6fd2807SJeff Garzik 					"classification failed\n");
1952fccb6ea5STejun Heo 			rc = -EINVAL;
1953fccb6ea5STejun Heo 			goto out;
1954c6fd2807SJeff Garzik 		}
1955c6fd2807SJeff Garzik 	}
1956c6fd2807SJeff Garzik 
195731daabdaSTejun Heo 	if (rc && try < ARRAY_SIZE(ata_eh_reset_timeouts)) {
195831daabdaSTejun Heo 		unsigned long now = jiffies;
1959c6fd2807SJeff Garzik 
196031daabdaSTejun Heo 		if (time_before(now, deadline)) {
196131daabdaSTejun Heo 			unsigned long delta = deadline - jiffies;
1962c6fd2807SJeff Garzik 
1963cc0680a5STejun Heo 			ata_link_printk(link, KERN_WARNING, "reset failed "
196431daabdaSTejun Heo 				"(errno=%d), retrying in %u secs\n",
196531daabdaSTejun Heo 				rc, (jiffies_to_msecs(delta) + 999) / 1000);
1966c6fd2807SJeff Garzik 
196731daabdaSTejun Heo 			schedule_timeout_uninterruptible(delta);
196831daabdaSTejun Heo 		}
196931daabdaSTejun Heo 
1970f1545154STejun Heo 		if (rc == -EPIPE ||
197131daabdaSTejun Heo 		    try == ARRAY_SIZE(ata_eh_reset_timeouts) - 1)
1972936fd732STejun Heo 			sata_down_spd_limit(link);
1973c6fd2807SJeff Garzik 		if (hardreset)
1974c6fd2807SJeff Garzik 			reset = hardreset;
1975c6fd2807SJeff Garzik 		goto retry;
1976c6fd2807SJeff Garzik 	}
1977c6fd2807SJeff Garzik 
1978c6fd2807SJeff Garzik 	if (rc == 0) {
1979008a7896STejun Heo 		u32 sstatus;
1980008a7896STejun Heo 
1981c6fd2807SJeff Garzik 		/* After the reset, the device state is PIO 0 and the
1982c6fd2807SJeff Garzik 		 * controller state is undefined.  Record the mode.
1983c6fd2807SJeff Garzik 		 */
1984936fd732STejun Heo 		ata_link_for_each_dev(dev, link)
1985f58229f8STejun Heo 			dev->pio_mode = XFER_PIO_0;
1986c6fd2807SJeff Garzik 
1987008a7896STejun Heo 		/* record current link speed */
1988936fd732STejun Heo 		if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
1989936fd732STejun Heo 			link->sata_spd = (sstatus >> 4) & 0xf;
1990008a7896STejun Heo 
1991c6fd2807SJeff Garzik 		if (postreset)
1992cc0680a5STejun Heo 			postreset(link, classes);
1993c6fd2807SJeff Garzik 
1994c6fd2807SJeff Garzik 		/* reset successful, schedule revalidation */
1995955e57dfSTejun Heo 		ata_eh_done(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
1996c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_REVALIDATE;
1997c6fd2807SJeff Garzik 	}
1998fccb6ea5STejun Heo  out:
1999fccb6ea5STejun Heo 	/* clear hotplug flag */
2000fccb6ea5STejun Heo 	ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2001c6fd2807SJeff Garzik 	return rc;
2002c6fd2807SJeff Garzik }
2003c6fd2807SJeff Garzik 
20040260731fSTejun Heo static int ata_eh_revalidate_and_attach(struct ata_link *link,
2005c6fd2807SJeff Garzik 					struct ata_device **r_failed_dev)
2006c6fd2807SJeff Garzik {
20070260731fSTejun Heo 	struct ata_port *ap = link->ap;
20080260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
2009c6fd2807SJeff Garzik 	struct ata_device *dev;
20108c3c52a8STejun Heo 	unsigned int new_mask = 0;
2011c6fd2807SJeff Garzik 	unsigned long flags;
2012f58229f8STejun Heo 	int rc = 0;
2013c6fd2807SJeff Garzik 
2014c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
2015c6fd2807SJeff Garzik 
20168c3c52a8STejun Heo 	/* For PATA drive side cable detection to work, IDENTIFY must
20178c3c52a8STejun Heo 	 * be done backwards such that PDIAG- is released by the slave
20188c3c52a8STejun Heo 	 * device before the master device is identified.
20198c3c52a8STejun Heo 	 */
20200260731fSTejun Heo 	ata_link_for_each_dev_reverse(dev, link) {
2021f58229f8STejun Heo 		unsigned int action = ata_eh_dev_action(dev);
2022f58229f8STejun Heo 		unsigned int readid_flags = 0;
2023c6fd2807SJeff Garzik 
2024bff04647STejun Heo 		if (ehc->i.flags & ATA_EHI_DID_RESET)
2025bff04647STejun Heo 			readid_flags |= ATA_READID_POSTRESET;
2026bff04647STejun Heo 
20279666f400STejun Heo 		if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
20280260731fSTejun Heo 			if (ata_link_offline(link)) {
2029c6fd2807SJeff Garzik 				rc = -EIO;
20308c3c52a8STejun Heo 				goto err;
2031c6fd2807SJeff Garzik 			}
2032c6fd2807SJeff Garzik 
20330260731fSTejun Heo 			ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
2034bff04647STejun Heo 			rc = ata_dev_revalidate(dev, readid_flags);
2035c6fd2807SJeff Garzik 			if (rc)
20368c3c52a8STejun Heo 				goto err;
2037c6fd2807SJeff Garzik 
20380260731fSTejun Heo 			ata_eh_done(link, dev, ATA_EH_REVALIDATE);
2039c6fd2807SJeff Garzik 
2040baa1e78aSTejun Heo 			/* Configuration may have changed, reconfigure
2041baa1e78aSTejun Heo 			 * transfer mode.
2042baa1e78aSTejun Heo 			 */
2043baa1e78aSTejun Heo 			ehc->i.flags |= ATA_EHI_SETMODE;
2044baa1e78aSTejun Heo 
2045c6fd2807SJeff Garzik 			/* schedule the scsi_rescan_device() here */
2046c6fd2807SJeff Garzik 			queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
2047c6fd2807SJeff Garzik 		} else if (dev->class == ATA_DEV_UNKNOWN &&
2048c6fd2807SJeff Garzik 			   ehc->tries[dev->devno] &&
2049c6fd2807SJeff Garzik 			   ata_class_enabled(ehc->classes[dev->devno])) {
2050c6fd2807SJeff Garzik 			dev->class = ehc->classes[dev->devno];
2051c6fd2807SJeff Garzik 
2052bff04647STejun Heo 			rc = ata_dev_read_id(dev, &dev->class, readid_flags,
2053bff04647STejun Heo 					     dev->id);
20548c3c52a8STejun Heo 			switch (rc) {
20558c3c52a8STejun Heo 			case 0:
2056f58229f8STejun Heo 				new_mask |= 1 << dev->devno;
20578c3c52a8STejun Heo 				break;
20588c3c52a8STejun Heo 			case -ENOENT:
205955a8e2c8STejun Heo 				/* IDENTIFY was issued to non-existent
206055a8e2c8STejun Heo 				 * device.  No need to reset.  Just
206155a8e2c8STejun Heo 				 * thaw and kill the device.
206255a8e2c8STejun Heo 				 */
206355a8e2c8STejun Heo 				ata_eh_thaw_port(ap);
206455a8e2c8STejun Heo 				dev->class = ATA_DEV_UNKNOWN;
2065c6fd2807SJeff Garzik 				break;
20668c3c52a8STejun Heo 			default:
20678c3c52a8STejun Heo 				dev->class = ATA_DEV_UNKNOWN;
20688c3c52a8STejun Heo 				goto err;
20698c3c52a8STejun Heo 			}
20708c3c52a8STejun Heo 		}
2071c6fd2807SJeff Garzik 	}
2072c6fd2807SJeff Garzik 
2073c1c4e8d5STejun Heo 	/* PDIAG- should have been released, ask cable type if post-reset */
2074*9b1e2658STejun Heo 	if (ata_is_host_link(link) && ap->ops->cable_detect &&
2075*9b1e2658STejun Heo 	    (ehc->i.flags & ATA_EHI_DID_RESET))
2076c1c4e8d5STejun Heo 		ap->cbl = ap->ops->cable_detect(ap);
2077c1c4e8d5STejun Heo 
20788c3c52a8STejun Heo 	/* Configure new devices forward such that user doesn't see
20798c3c52a8STejun Heo 	 * device detection messages backwards.
20808c3c52a8STejun Heo 	 */
20810260731fSTejun Heo 	ata_link_for_each_dev(dev, link) {
2082f58229f8STejun Heo 		if (!(new_mask & (1 << dev->devno)))
20838c3c52a8STejun Heo 			continue;
20848c3c52a8STejun Heo 
20858c3c52a8STejun Heo 		ehc->i.flags |= ATA_EHI_PRINTINFO;
20868c3c52a8STejun Heo 		rc = ata_dev_configure(dev);
20878c3c52a8STejun Heo 		ehc->i.flags &= ~ATA_EHI_PRINTINFO;
20888c3c52a8STejun Heo 		if (rc)
20898c3c52a8STejun Heo 			goto err;
20908c3c52a8STejun Heo 
2091c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
2092c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
2093c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
2094baa1e78aSTejun Heo 
209555a8e2c8STejun Heo 		/* new device discovered, configure xfermode */
2096baa1e78aSTejun Heo 		ehc->i.flags |= ATA_EHI_SETMODE;
2097c6fd2807SJeff Garzik 	}
2098c6fd2807SJeff Garzik 
20998c3c52a8STejun Heo 	return 0;
21008c3c52a8STejun Heo 
21018c3c52a8STejun Heo  err:
2102c6fd2807SJeff Garzik 	*r_failed_dev = dev;
21038c3c52a8STejun Heo 	DPRINTK("EXIT rc=%d\n", rc);
2104c6fd2807SJeff Garzik 	return rc;
2105c6fd2807SJeff Garzik }
2106c6fd2807SJeff Garzik 
21070260731fSTejun Heo static int ata_link_nr_enabled(struct ata_link *link)
2108c6fd2807SJeff Garzik {
2109f58229f8STejun Heo 	struct ata_device *dev;
2110f58229f8STejun Heo 	int cnt = 0;
2111c6fd2807SJeff Garzik 
21120260731fSTejun Heo 	ata_link_for_each_dev(dev, link)
2113f58229f8STejun Heo 		if (ata_dev_enabled(dev))
2114c6fd2807SJeff Garzik 			cnt++;
2115c6fd2807SJeff Garzik 	return cnt;
2116c6fd2807SJeff Garzik }
2117c6fd2807SJeff Garzik 
21180260731fSTejun Heo static int ata_link_nr_vacant(struct ata_link *link)
2119c6fd2807SJeff Garzik {
2120f58229f8STejun Heo 	struct ata_device *dev;
2121f58229f8STejun Heo 	int cnt = 0;
2122c6fd2807SJeff Garzik 
21230260731fSTejun Heo 	ata_link_for_each_dev(dev, link)
2124f58229f8STejun Heo 		if (dev->class == ATA_DEV_UNKNOWN)
2125c6fd2807SJeff Garzik 			cnt++;
2126c6fd2807SJeff Garzik 	return cnt;
2127c6fd2807SJeff Garzik }
2128c6fd2807SJeff Garzik 
21290260731fSTejun Heo static int ata_eh_skip_recovery(struct ata_link *link)
2130c6fd2807SJeff Garzik {
21310260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
2132f58229f8STejun Heo 	struct ata_device *dev;
2133c6fd2807SJeff Garzik 
2134c6fd2807SJeff Garzik 	/* thaw frozen port, resume link and recover failed devices */
21350260731fSTejun Heo 	if ((link->ap->pflags & ATA_PFLAG_FROZEN) ||
21360260731fSTejun Heo 	    (ehc->i.flags & ATA_EHI_RESUME_LINK) || ata_link_nr_enabled(link))
2137c6fd2807SJeff Garzik 		return 0;
2138c6fd2807SJeff Garzik 
2139c6fd2807SJeff Garzik 	/* skip if class codes for all vacant slots are ATA_DEV_NONE */
21400260731fSTejun Heo 	ata_link_for_each_dev(dev, link) {
2141c6fd2807SJeff Garzik 		if (dev->class == ATA_DEV_UNKNOWN &&
2142c6fd2807SJeff Garzik 		    ehc->classes[dev->devno] != ATA_DEV_NONE)
2143c6fd2807SJeff Garzik 			return 0;
2144c6fd2807SJeff Garzik 	}
2145c6fd2807SJeff Garzik 
2146c6fd2807SJeff Garzik 	return 1;
2147c6fd2807SJeff Garzik }
2148c6fd2807SJeff Garzik 
2149*9b1e2658STejun Heo static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
2150fee7ca72STejun Heo {
21519af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
2152fee7ca72STejun Heo 
2153fee7ca72STejun Heo 	ehc->tries[dev->devno]--;
2154fee7ca72STejun Heo 
2155fee7ca72STejun Heo 	switch (err) {
2156fee7ca72STejun Heo 	case -ENODEV:
2157fee7ca72STejun Heo 		/* device missing or wrong IDENTIFY data, schedule probing */
2158fee7ca72STejun Heo 		ehc->i.probe_mask |= (1 << dev->devno);
2159fee7ca72STejun Heo 	case -EINVAL:
2160fee7ca72STejun Heo 		/* give it just one more chance */
2161fee7ca72STejun Heo 		ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
2162fee7ca72STejun Heo 	case -EIO:
2163fee7ca72STejun Heo 		if (ehc->tries[dev->devno] == 1) {
2164fee7ca72STejun Heo 			/* This is the last chance, better to slow
2165fee7ca72STejun Heo 			 * down than lose it.
2166fee7ca72STejun Heo 			 */
2167936fd732STejun Heo 			sata_down_spd_limit(dev->link);
2168fee7ca72STejun Heo 			ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2169fee7ca72STejun Heo 		}
2170fee7ca72STejun Heo 	}
2171fee7ca72STejun Heo 
2172fee7ca72STejun Heo 	if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2173fee7ca72STejun Heo 		/* disable device if it has used up all its chances */
2174fee7ca72STejun Heo 		ata_dev_disable(dev);
2175fee7ca72STejun Heo 
2176fee7ca72STejun Heo 		/* detach if offline */
2177936fd732STejun Heo 		if (ata_link_offline(dev->link))
2178fee7ca72STejun Heo 			ata_eh_detach_dev(dev);
2179fee7ca72STejun Heo 
2180fee7ca72STejun Heo 		/* probe if requested */
2181fee7ca72STejun Heo 		if ((ehc->i.probe_mask & (1 << dev->devno)) &&
2182fee7ca72STejun Heo 		    !(ehc->did_probe_mask & (1 << dev->devno))) {
2183fee7ca72STejun Heo 			ata_eh_detach_dev(dev);
2184fee7ca72STejun Heo 			ata_dev_init(dev);
2185fee7ca72STejun Heo 
2186fee7ca72STejun Heo 			ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2187fee7ca72STejun Heo 			ehc->did_probe_mask |= (1 << dev->devno);
2188fee7ca72STejun Heo 			ehc->i.action |= ATA_EH_SOFTRESET;
2189fee7ca72STejun Heo 		}
2190*9b1e2658STejun Heo 
2191*9b1e2658STejun Heo 		return 1;
2192fee7ca72STejun Heo 	} else {
2193fee7ca72STejun Heo 		/* soft didn't work?  be haaaaard */
2194fee7ca72STejun Heo 		if (ehc->i.flags & ATA_EHI_DID_RESET)
2195fee7ca72STejun Heo 			ehc->i.action |= ATA_EH_HARDRESET;
2196fee7ca72STejun Heo 		else
2197fee7ca72STejun Heo 			ehc->i.action |= ATA_EH_SOFTRESET;
2198*9b1e2658STejun Heo 
2199*9b1e2658STejun Heo 		return 0;
2200fee7ca72STejun Heo 	}
2201fee7ca72STejun Heo }
2202fee7ca72STejun Heo 
2203c6fd2807SJeff Garzik /**
2204c6fd2807SJeff Garzik  *	ata_eh_recover - recover host port after error
2205c6fd2807SJeff Garzik  *	@ap: host port to recover
2206c6fd2807SJeff Garzik  *	@prereset: prereset method (can be NULL)
2207c6fd2807SJeff Garzik  *	@softreset: softreset method (can be NULL)
2208c6fd2807SJeff Garzik  *	@hardreset: hardreset method (can be NULL)
2209c6fd2807SJeff Garzik  *	@postreset: postreset method (can be NULL)
2210*9b1e2658STejun Heo  *	@r_failed_link: out parameter for failed link
2211c6fd2807SJeff Garzik  *
2212c6fd2807SJeff Garzik  *	This is the alpha and omega, eum and yang, heart and soul of
2213c6fd2807SJeff Garzik  *	libata exception handling.  On entry, actions required to
2214*9b1e2658STejun Heo  *	recover each link and hotplug requests are recorded in the
2215*9b1e2658STejun Heo  *	link's eh_context.  This function executes all the operations
2216*9b1e2658STejun Heo  *	with appropriate retrials and fallbacks to resurrect failed
2217c6fd2807SJeff Garzik  *	devices, detach goners and greet newcomers.
2218c6fd2807SJeff Garzik  *
2219c6fd2807SJeff Garzik  *	LOCKING:
2220c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
2221c6fd2807SJeff Garzik  *
2222c6fd2807SJeff Garzik  *	RETURNS:
2223c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
2224c6fd2807SJeff Garzik  */
2225c6fd2807SJeff Garzik static int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
2226c6fd2807SJeff Garzik 			  ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2227*9b1e2658STejun Heo 			  ata_postreset_fn_t postreset,
2228*9b1e2658STejun Heo 			  struct ata_link **r_failed_link)
2229c6fd2807SJeff Garzik {
2230*9b1e2658STejun Heo 	struct ata_link *link;
2231c6fd2807SJeff Garzik 	struct ata_device *dev;
2232*9b1e2658STejun Heo 	int nr_failed_devs, nr_disabled_devs;
2233*9b1e2658STejun Heo 	int reset, rc;
2234c6fd2807SJeff Garzik 
2235c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
2236c6fd2807SJeff Garzik 
2237c6fd2807SJeff Garzik 	/* prep for recovery */
2238*9b1e2658STejun Heo 	ata_port_for_each_link(link, ap) {
2239*9b1e2658STejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
2240*9b1e2658STejun Heo 
22410260731fSTejun Heo 		ata_link_for_each_dev(dev, link) {
2242c6fd2807SJeff Garzik 			ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2243c6fd2807SJeff Garzik 
224479a55b72STejun Heo 			/* collect port action mask recorded in dev actions */
2245*9b1e2658STejun Heo 			ehc->i.action |= ehc->i.dev_action[dev->devno] &
2246*9b1e2658STejun Heo 					 ~ATA_EH_PERDEV_MASK;
2247f58229f8STejun Heo 			ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
224879a55b72STejun Heo 
2249c6fd2807SJeff Garzik 			/* process hotplug request */
2250c6fd2807SJeff Garzik 			if (dev->flags & ATA_DFLAG_DETACH)
2251c6fd2807SJeff Garzik 				ata_eh_detach_dev(dev);
2252c6fd2807SJeff Garzik 
2253c6fd2807SJeff Garzik 			if (!ata_dev_enabled(dev) &&
2254c6fd2807SJeff Garzik 			    ((ehc->i.probe_mask & (1 << dev->devno)) &&
2255c6fd2807SJeff Garzik 			     !(ehc->did_probe_mask & (1 << dev->devno)))) {
2256c6fd2807SJeff Garzik 				ata_eh_detach_dev(dev);
2257c6fd2807SJeff Garzik 				ata_dev_init(dev);
2258c6fd2807SJeff Garzik 				ehc->did_probe_mask |= (1 << dev->devno);
2259c6fd2807SJeff Garzik 				ehc->i.action |= ATA_EH_SOFTRESET;
2260c6fd2807SJeff Garzik 			}
2261c6fd2807SJeff Garzik 		}
2262*9b1e2658STejun Heo 	}
2263c6fd2807SJeff Garzik 
2264c6fd2807SJeff Garzik  retry:
2265c6fd2807SJeff Garzik 	rc = 0;
2266*9b1e2658STejun Heo 	nr_failed_devs = 0;
2267*9b1e2658STejun Heo 	nr_disabled_devs = 0;
2268*9b1e2658STejun Heo 	reset = 0;
2269c6fd2807SJeff Garzik 
2270c6fd2807SJeff Garzik 	/* if UNLOADING, finish immediately */
2271c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_UNLOADING)
2272c6fd2807SJeff Garzik 		goto out;
2273c6fd2807SJeff Garzik 
2274*9b1e2658STejun Heo 	/* prep for EH */
2275*9b1e2658STejun Heo 	ata_port_for_each_link(link, ap) {
2276*9b1e2658STejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
2277*9b1e2658STejun Heo 
2278c6fd2807SJeff Garzik 		/* skip EH if possible. */
22790260731fSTejun Heo 		if (ata_eh_skip_recovery(link))
2280c6fd2807SJeff Garzik 			ehc->i.action = 0;
2281c6fd2807SJeff Garzik 
2282*9b1e2658STejun Heo 		/* do we need to reset? */
2283*9b1e2658STejun Heo 		if (ehc->i.action & ATA_EH_RESET_MASK)
2284*9b1e2658STejun Heo 			reset = 1;
2285*9b1e2658STejun Heo 
22860260731fSTejun Heo 		ata_link_for_each_dev(dev, link)
2287f58229f8STejun Heo 			ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
2288*9b1e2658STejun Heo 	}
2289c6fd2807SJeff Garzik 
2290c6fd2807SJeff Garzik 	/* reset */
2291*9b1e2658STejun Heo 	if (reset) {
2292c6fd2807SJeff Garzik 		ata_eh_freeze_port(ap);
2293c6fd2807SJeff Garzik 
2294*9b1e2658STejun Heo 		ata_port_for_each_link(link, ap) {
2295*9b1e2658STejun Heo 			struct ata_eh_context *ehc = &link->eh_context;
2296*9b1e2658STejun Heo 
2297*9b1e2658STejun Heo 			if (!(ehc->i.action & ATA_EH_RESET_MASK))
2298*9b1e2658STejun Heo 				continue;
2299*9b1e2658STejun Heo 
2300*9b1e2658STejun Heo 			rc = ata_eh_reset(link, ata_link_nr_vacant(link),
2301*9b1e2658STejun Heo 					  prereset, softreset, hardreset,
2302*9b1e2658STejun Heo 					  postreset);
2303c6fd2807SJeff Garzik 			if (rc) {
23040260731fSTejun Heo 				ata_link_printk(link, KERN_ERR,
2305c6fd2807SJeff Garzik 						"reset failed, giving up\n");
2306c6fd2807SJeff Garzik 				goto out;
2307c6fd2807SJeff Garzik 			}
2308*9b1e2658STejun Heo 		}
2309c6fd2807SJeff Garzik 
2310c6fd2807SJeff Garzik 		ata_eh_thaw_port(ap);
2311c6fd2807SJeff Garzik 	}
2312c6fd2807SJeff Garzik 
2313*9b1e2658STejun Heo 	/* the rest */
2314*9b1e2658STejun Heo 	ata_port_for_each_link(link, ap) {
2315*9b1e2658STejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
2316*9b1e2658STejun Heo 
2317c6fd2807SJeff Garzik 		/* revalidate existing devices and attach new ones */
23180260731fSTejun Heo 		rc = ata_eh_revalidate_and_attach(link, &dev);
2319c6fd2807SJeff Garzik 		if (rc)
2320c6fd2807SJeff Garzik 			goto dev_fail;
2321c6fd2807SJeff Garzik 
2322baa1e78aSTejun Heo 		/* configure transfer mode if necessary */
2323baa1e78aSTejun Heo 		if (ehc->i.flags & ATA_EHI_SETMODE) {
23240260731fSTejun Heo 			rc = ata_set_mode(link, &dev);
23254ae72a1eSTejun Heo 			if (rc)
2326c6fd2807SJeff Garzik 				goto dev_fail;
2327baa1e78aSTejun Heo 			ehc->i.flags &= ~ATA_EHI_SETMODE;
2328c6fd2807SJeff Garzik 		}
2329c6fd2807SJeff Garzik 
2330*9b1e2658STejun Heo 		/* this link is okay now */
2331*9b1e2658STejun Heo 		ehc->i.flags = 0;
2332*9b1e2658STejun Heo 		continue;
2333c6fd2807SJeff Garzik 
2334c6fd2807SJeff Garzik 	dev_fail:
2335*9b1e2658STejun Heo 		nr_failed_devs++;
2336*9b1e2658STejun Heo 		if (ata_eh_handle_dev_fail(dev, rc))
2337*9b1e2658STejun Heo 			nr_disabled_devs++;
2338c6fd2807SJeff Garzik 
2339*9b1e2658STejun Heo 		if (ap->pflags & ATA_PFLAG_FROZEN)
2340*9b1e2658STejun Heo 			break;
2341*9b1e2658STejun Heo 	}
2342*9b1e2658STejun Heo 
2343*9b1e2658STejun Heo 	if (nr_failed_devs) {
2344*9b1e2658STejun Heo 		if (nr_failed_devs != nr_disabled_devs) {
2345*9b1e2658STejun Heo 			ata_port_printk(ap, KERN_WARNING, "failed to recover "
2346*9b1e2658STejun Heo 					"some devices, retrying in 5 secs\n");
2347c6fd2807SJeff Garzik 			ssleep(5);
2348c6fd2807SJeff Garzik 		} else {
2349*9b1e2658STejun Heo 			/* no device left to recover, repeat fast */
2350c6fd2807SJeff Garzik 			msleep(500);
2351c6fd2807SJeff Garzik 		}
2352c6fd2807SJeff Garzik 
2353c6fd2807SJeff Garzik 		goto retry;
2354*9b1e2658STejun Heo 	}
2355c6fd2807SJeff Garzik 
2356c6fd2807SJeff Garzik  out:
2357*9b1e2658STejun Heo 	if (rc && r_failed_link)
2358*9b1e2658STejun Heo 		*r_failed_link = link;
2359c6fd2807SJeff Garzik 
2360c6fd2807SJeff Garzik 	DPRINTK("EXIT, rc=%d\n", rc);
2361c6fd2807SJeff Garzik 	return rc;
2362c6fd2807SJeff Garzik }
2363c6fd2807SJeff Garzik 
2364c6fd2807SJeff Garzik /**
2365c6fd2807SJeff Garzik  *	ata_eh_finish - finish up EH
2366c6fd2807SJeff Garzik  *	@ap: host port to finish EH for
2367c6fd2807SJeff Garzik  *
2368c6fd2807SJeff Garzik  *	Recovery is complete.  Clean up EH states and retry or finish
2369c6fd2807SJeff Garzik  *	failed qcs.
2370c6fd2807SJeff Garzik  *
2371c6fd2807SJeff Garzik  *	LOCKING:
2372c6fd2807SJeff Garzik  *	None.
2373c6fd2807SJeff Garzik  */
2374c6fd2807SJeff Garzik static void ata_eh_finish(struct ata_port *ap)
2375c6fd2807SJeff Garzik {
2376c6fd2807SJeff Garzik 	int tag;
2377c6fd2807SJeff Garzik 
2378c6fd2807SJeff Garzik 	/* retry or finish qcs */
2379c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2380c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2381c6fd2807SJeff Garzik 
2382c6fd2807SJeff Garzik 		if (!(qc->flags & ATA_QCFLAG_FAILED))
2383c6fd2807SJeff Garzik 			continue;
2384c6fd2807SJeff Garzik 
2385c6fd2807SJeff Garzik 		if (qc->err_mask) {
2386c6fd2807SJeff Garzik 			/* FIXME: Once EH migration is complete,
2387c6fd2807SJeff Garzik 			 * generate sense data in this function,
2388c6fd2807SJeff Garzik 			 * considering both err_mask and tf.
2389c6fd2807SJeff Garzik 			 */
2390c6fd2807SJeff Garzik 			if (qc->err_mask & AC_ERR_INVALID)
2391c6fd2807SJeff Garzik 				ata_eh_qc_complete(qc);
2392c6fd2807SJeff Garzik 			else
2393c6fd2807SJeff Garzik 				ata_eh_qc_retry(qc);
2394c6fd2807SJeff Garzik 		} else {
2395c6fd2807SJeff Garzik 			if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2396c6fd2807SJeff Garzik 				ata_eh_qc_complete(qc);
2397c6fd2807SJeff Garzik 			} else {
2398c6fd2807SJeff Garzik 				/* feed zero TF to sense generation */
2399c6fd2807SJeff Garzik 				memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2400c6fd2807SJeff Garzik 				ata_eh_qc_retry(qc);
2401c6fd2807SJeff Garzik 			}
2402c6fd2807SJeff Garzik 		}
2403c6fd2807SJeff Garzik 	}
2404c6fd2807SJeff Garzik }
2405c6fd2807SJeff Garzik 
2406c6fd2807SJeff Garzik /**
2407c6fd2807SJeff Garzik  *	ata_do_eh - do standard error handling
2408c6fd2807SJeff Garzik  *	@ap: host port to handle error for
2409c6fd2807SJeff Garzik  *	@prereset: prereset method (can be NULL)
2410c6fd2807SJeff Garzik  *	@softreset: softreset method (can be NULL)
2411c6fd2807SJeff Garzik  *	@hardreset: hardreset method (can be NULL)
2412c6fd2807SJeff Garzik  *	@postreset: postreset method (can be NULL)
2413c6fd2807SJeff Garzik  *
2414c6fd2807SJeff Garzik  *	Perform standard error handling sequence.
2415c6fd2807SJeff Garzik  *
2416c6fd2807SJeff Garzik  *	LOCKING:
2417c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
2418c6fd2807SJeff Garzik  */
2419c6fd2807SJeff Garzik void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2420c6fd2807SJeff Garzik 	       ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2421c6fd2807SJeff Garzik 	       ata_postreset_fn_t postreset)
2422c6fd2807SJeff Garzik {
2423*9b1e2658STejun Heo 	struct ata_device *dev;
2424*9b1e2658STejun Heo 	int rc;
2425*9b1e2658STejun Heo 
2426*9b1e2658STejun Heo 	ata_eh_autopsy(ap);
2427*9b1e2658STejun Heo 	ata_eh_report(ap);
2428*9b1e2658STejun Heo 
2429*9b1e2658STejun Heo 	rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
2430*9b1e2658STejun Heo 			    NULL);
2431*9b1e2658STejun Heo 	if (rc) {
2432*9b1e2658STejun Heo 		ata_link_for_each_dev(dev, &ap->link)
2433*9b1e2658STejun Heo 			ata_dev_disable(dev);
2434*9b1e2658STejun Heo 	}
2435*9b1e2658STejun Heo 
2436c6fd2807SJeff Garzik 	ata_eh_finish(ap);
2437c6fd2807SJeff Garzik }
2438c6fd2807SJeff Garzik 
24396ffa01d8STejun Heo #ifdef CONFIG_PM
2440c6fd2807SJeff Garzik /**
2441c6fd2807SJeff Garzik  *	ata_eh_handle_port_suspend - perform port suspend operation
2442c6fd2807SJeff Garzik  *	@ap: port to suspend
2443c6fd2807SJeff Garzik  *
2444c6fd2807SJeff Garzik  *	Suspend @ap.
2445c6fd2807SJeff Garzik  *
2446c6fd2807SJeff Garzik  *	LOCKING:
2447c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
2448c6fd2807SJeff Garzik  */
2449c6fd2807SJeff Garzik static void ata_eh_handle_port_suspend(struct ata_port *ap)
2450c6fd2807SJeff Garzik {
2451c6fd2807SJeff Garzik 	unsigned long flags;
2452c6fd2807SJeff Garzik 	int rc = 0;
2453c6fd2807SJeff Garzik 
2454c6fd2807SJeff Garzik 	/* are we suspending? */
2455c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
2456c6fd2807SJeff Garzik 	if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2457c6fd2807SJeff Garzik 	    ap->pm_mesg.event == PM_EVENT_ON) {
2458c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
2459c6fd2807SJeff Garzik 		return;
2460c6fd2807SJeff Garzik 	}
2461c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
2462c6fd2807SJeff Garzik 
2463c6fd2807SJeff Garzik 	WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
2464c6fd2807SJeff Garzik 
246564578a3dSTejun Heo 	/* tell ACPI we're suspending */
246664578a3dSTejun Heo 	rc = ata_acpi_on_suspend(ap);
246764578a3dSTejun Heo 	if (rc)
246864578a3dSTejun Heo 		goto out;
246964578a3dSTejun Heo 
2470c6fd2807SJeff Garzik 	/* suspend */
2471c6fd2807SJeff Garzik 	ata_eh_freeze_port(ap);
2472c6fd2807SJeff Garzik 
2473c6fd2807SJeff Garzik 	if (ap->ops->port_suspend)
2474c6fd2807SJeff Garzik 		rc = ap->ops->port_suspend(ap, ap->pm_mesg);
2475c6fd2807SJeff Garzik 
247664578a3dSTejun Heo  out:
2477c6fd2807SJeff Garzik 	/* report result */
2478c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
2479c6fd2807SJeff Garzik 
2480c6fd2807SJeff Garzik 	ap->pflags &= ~ATA_PFLAG_PM_PENDING;
2481c6fd2807SJeff Garzik 	if (rc == 0)
2482c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_SUSPENDED;
248364578a3dSTejun Heo 	else if (ap->pflags & ATA_PFLAG_FROZEN)
2484c6fd2807SJeff Garzik 		ata_port_schedule_eh(ap);
2485c6fd2807SJeff Garzik 
2486c6fd2807SJeff Garzik 	if (ap->pm_result) {
2487c6fd2807SJeff Garzik 		*ap->pm_result = rc;
2488c6fd2807SJeff Garzik 		ap->pm_result = NULL;
2489c6fd2807SJeff Garzik 	}
2490c6fd2807SJeff Garzik 
2491c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
2492c6fd2807SJeff Garzik 
2493c6fd2807SJeff Garzik 	return;
2494c6fd2807SJeff Garzik }
2495c6fd2807SJeff Garzik 
2496c6fd2807SJeff Garzik /**
2497c6fd2807SJeff Garzik  *	ata_eh_handle_port_resume - perform port resume operation
2498c6fd2807SJeff Garzik  *	@ap: port to resume
2499c6fd2807SJeff Garzik  *
2500c6fd2807SJeff Garzik  *	Resume @ap.
2501c6fd2807SJeff Garzik  *
2502c6fd2807SJeff Garzik  *	LOCKING:
2503c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
2504c6fd2807SJeff Garzik  */
2505c6fd2807SJeff Garzik static void ata_eh_handle_port_resume(struct ata_port *ap)
2506c6fd2807SJeff Garzik {
2507c6fd2807SJeff Garzik 	unsigned long flags;
25089666f400STejun Heo 	int rc = 0;
2509c6fd2807SJeff Garzik 
2510c6fd2807SJeff Garzik 	/* are we resuming? */
2511c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
2512c6fd2807SJeff Garzik 	if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2513c6fd2807SJeff Garzik 	    ap->pm_mesg.event != PM_EVENT_ON) {
2514c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
2515c6fd2807SJeff Garzik 		return;
2516c6fd2807SJeff Garzik 	}
2517c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
2518c6fd2807SJeff Garzik 
25199666f400STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
2520c6fd2807SJeff Garzik 
2521c6fd2807SJeff Garzik 	if (ap->ops->port_resume)
2522c6fd2807SJeff Garzik 		rc = ap->ops->port_resume(ap);
2523c6fd2807SJeff Garzik 
25246746544cSTejun Heo 	/* tell ACPI that we're resuming */
25256746544cSTejun Heo 	ata_acpi_on_resume(ap);
25266746544cSTejun Heo 
25279666f400STejun Heo 	/* report result */
2528c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
2529c6fd2807SJeff Garzik 	ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
2530c6fd2807SJeff Garzik 	if (ap->pm_result) {
2531c6fd2807SJeff Garzik 		*ap->pm_result = rc;
2532c6fd2807SJeff Garzik 		ap->pm_result = NULL;
2533c6fd2807SJeff Garzik 	}
2534c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
2535c6fd2807SJeff Garzik }
25366ffa01d8STejun Heo #endif /* CONFIG_PM */
2537