xref: /openbmc/linux/drivers/ata/libata-eh.c (revision 2855568b1ee4f58ef2c0a13ddfceb4b0b216b7ed)
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>
36*2855568bSJeff Garzik #include <linux/pci.h>
37c6fd2807SJeff Garzik #include <scsi/scsi.h>
38c6fd2807SJeff Garzik #include <scsi/scsi_host.h>
39c6fd2807SJeff Garzik #include <scsi/scsi_eh.h>
40c6fd2807SJeff Garzik #include <scsi/scsi_device.h>
41c6fd2807SJeff Garzik #include <scsi/scsi_cmnd.h>
42c6fd2807SJeff Garzik #include "../scsi/scsi_transport_api.h"
43c6fd2807SJeff Garzik 
44c6fd2807SJeff Garzik #include <linux/libata.h>
45c6fd2807SJeff Garzik 
46c6fd2807SJeff Garzik #include "libata.h"
47c6fd2807SJeff Garzik 
487d47e8d4STejun Heo enum {
497d47e8d4STejun Heo 	ATA_EH_SPDN_NCQ_OFF		= (1 << 0),
507d47e8d4STejun Heo 	ATA_EH_SPDN_SPEED_DOWN		= (1 << 1),
517d47e8d4STejun Heo 	ATA_EH_SPDN_FALLBACK_TO_PIO	= (1 << 2),
527d47e8d4STejun Heo };
537d47e8d4STejun Heo 
5431daabdaSTejun Heo /* Waiting in ->prereset can never be reliable.  It's sometimes nice
5531daabdaSTejun Heo  * to wait there but it can't be depended upon; otherwise, we wouldn't
5631daabdaSTejun Heo  * be resetting.  Just give it enough time for most drives to spin up.
5731daabdaSTejun Heo  */
5831daabdaSTejun Heo enum {
5931daabdaSTejun Heo 	ATA_EH_PRERESET_TIMEOUT		= 10 * HZ,
605ddf24c5STejun Heo 	ATA_EH_FASTDRAIN_INTERVAL	= 3 * HZ,
6131daabdaSTejun Heo };
6231daabdaSTejun Heo 
6331daabdaSTejun Heo /* The following table determines how we sequence resets.  Each entry
6431daabdaSTejun Heo  * represents timeout for that try.  The first try can be soft or
6531daabdaSTejun Heo  * hardreset.  All others are hardreset if available.  In most cases
6631daabdaSTejun Heo  * the first reset w/ 10sec timeout should succeed.  Following entries
6731daabdaSTejun Heo  * are mostly for error handling, hotplug and retarded devices.
6831daabdaSTejun Heo  */
6931daabdaSTejun Heo static const unsigned long ata_eh_reset_timeouts[] = {
7031daabdaSTejun Heo 	10 * HZ,	/* most drives spin up by 10sec */
7131daabdaSTejun Heo 	10 * HZ,	/* > 99% working drives spin up before 20sec */
7231daabdaSTejun Heo 	35 * HZ,	/* give > 30 secs of idleness for retarded devices */
7331daabdaSTejun Heo 	5 * HZ,		/* and sweet one last chance */
7431daabdaSTejun Heo 	/* > 1 min has elapsed, give up */
7531daabdaSTejun Heo };
7631daabdaSTejun Heo 
77c6fd2807SJeff Garzik static void __ata_port_freeze(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 
154cbcdd875STejun Heo /**
155cbcdd875STejun Heo  *	ata_port_desc - append port description
156cbcdd875STejun Heo  *	@ap: target ATA port
157cbcdd875STejun Heo  *	@fmt: printf format string
158cbcdd875STejun Heo  *
159cbcdd875STejun Heo  *	Format string according to @fmt and append it to port
160cbcdd875STejun Heo  *	description.  If port description is not empty, " " is added
161cbcdd875STejun Heo  *	in-between.  This function is to be used while initializing
162cbcdd875STejun Heo  *	ata_host.  The description is printed on host registration.
163cbcdd875STejun Heo  *
164cbcdd875STejun Heo  *	LOCKING:
165cbcdd875STejun Heo  *	None.
166cbcdd875STejun Heo  */
167cbcdd875STejun Heo void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
168cbcdd875STejun Heo {
169cbcdd875STejun Heo 	va_list args;
170cbcdd875STejun Heo 
171cbcdd875STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
172cbcdd875STejun Heo 
173cbcdd875STejun Heo 	if (ap->link.eh_info.desc_len)
174cbcdd875STejun Heo 		__ata_ehi_push_desc(&ap->link.eh_info, " ");
175cbcdd875STejun Heo 
176cbcdd875STejun Heo 	va_start(args, fmt);
177cbcdd875STejun Heo 	__ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
178cbcdd875STejun Heo 	va_end(args);
179cbcdd875STejun Heo }
180cbcdd875STejun Heo 
181cbcdd875STejun Heo #ifdef CONFIG_PCI
182cbcdd875STejun Heo 
183cbcdd875STejun Heo /**
184cbcdd875STejun Heo  *	ata_port_pbar_desc - append PCI BAR description
185cbcdd875STejun Heo  *	@ap: target ATA port
186cbcdd875STejun Heo  *	@bar: target PCI BAR
187cbcdd875STejun Heo  *	@offset: offset into PCI BAR
188cbcdd875STejun Heo  *	@name: name of the area
189cbcdd875STejun Heo  *
190cbcdd875STejun Heo  *	If @offset is negative, this function formats a string which
191cbcdd875STejun Heo  *	contains the name, address, size and type of the BAR and
192cbcdd875STejun Heo  *	appends it to the port description.  If @offset is zero or
193cbcdd875STejun Heo  *	positive, only name and offsetted address is appended.
194cbcdd875STejun Heo  *
195cbcdd875STejun Heo  *	LOCKING:
196cbcdd875STejun Heo  *	None.
197cbcdd875STejun Heo  */
198cbcdd875STejun Heo void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
199cbcdd875STejun Heo 			const char *name)
200cbcdd875STejun Heo {
201cbcdd875STejun Heo 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
202cbcdd875STejun Heo 	char *type = "";
203cbcdd875STejun Heo 	unsigned long long start, len;
204cbcdd875STejun Heo 
205cbcdd875STejun Heo 	if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
206cbcdd875STejun Heo 		type = "m";
207cbcdd875STejun Heo 	else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
208cbcdd875STejun Heo 		type = "i";
209cbcdd875STejun Heo 
210cbcdd875STejun Heo 	start = (unsigned long long)pci_resource_start(pdev, bar);
211cbcdd875STejun Heo 	len = (unsigned long long)pci_resource_len(pdev, bar);
212cbcdd875STejun Heo 
213cbcdd875STejun Heo 	if (offset < 0)
214cbcdd875STejun Heo 		ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
215cbcdd875STejun Heo 	else
216cbcdd875STejun Heo 		ata_port_desc(ap, "%s 0x%llx", name, start + offset);
217cbcdd875STejun Heo }
218cbcdd875STejun Heo 
219cbcdd875STejun Heo #endif /* CONFIG_PCI */
220cbcdd875STejun Heo 
221c6fd2807SJeff Garzik static void ata_ering_record(struct ata_ering *ering, int is_io,
222c6fd2807SJeff Garzik 			     unsigned int err_mask)
223c6fd2807SJeff Garzik {
224c6fd2807SJeff Garzik 	struct ata_ering_entry *ent;
225c6fd2807SJeff Garzik 
226c6fd2807SJeff Garzik 	WARN_ON(!err_mask);
227c6fd2807SJeff Garzik 
228c6fd2807SJeff Garzik 	ering->cursor++;
229c6fd2807SJeff Garzik 	ering->cursor %= ATA_ERING_SIZE;
230c6fd2807SJeff Garzik 
231c6fd2807SJeff Garzik 	ent = &ering->ring[ering->cursor];
232c6fd2807SJeff Garzik 	ent->is_io = is_io;
233c6fd2807SJeff Garzik 	ent->err_mask = err_mask;
234c6fd2807SJeff Garzik 	ent->timestamp = get_jiffies_64();
235c6fd2807SJeff Garzik }
236c6fd2807SJeff Garzik 
2377d47e8d4STejun Heo static void ata_ering_clear(struct ata_ering *ering)
238c6fd2807SJeff Garzik {
2397d47e8d4STejun Heo 	memset(ering, 0, sizeof(*ering));
240c6fd2807SJeff Garzik }
241c6fd2807SJeff Garzik 
242c6fd2807SJeff Garzik static int ata_ering_map(struct ata_ering *ering,
243c6fd2807SJeff Garzik 			 int (*map_fn)(struct ata_ering_entry *, void *),
244c6fd2807SJeff Garzik 			 void *arg)
245c6fd2807SJeff Garzik {
246c6fd2807SJeff Garzik 	int idx, rc = 0;
247c6fd2807SJeff Garzik 	struct ata_ering_entry *ent;
248c6fd2807SJeff Garzik 
249c6fd2807SJeff Garzik 	idx = ering->cursor;
250c6fd2807SJeff Garzik 	do {
251c6fd2807SJeff Garzik 		ent = &ering->ring[idx];
252c6fd2807SJeff Garzik 		if (!ent->err_mask)
253c6fd2807SJeff Garzik 			break;
254c6fd2807SJeff Garzik 		rc = map_fn(ent, arg);
255c6fd2807SJeff Garzik 		if (rc)
256c6fd2807SJeff Garzik 			break;
257c6fd2807SJeff Garzik 		idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
258c6fd2807SJeff Garzik 	} while (idx != ering->cursor);
259c6fd2807SJeff Garzik 
260c6fd2807SJeff Garzik 	return rc;
261c6fd2807SJeff Garzik }
262c6fd2807SJeff Garzik 
263c6fd2807SJeff Garzik static unsigned int ata_eh_dev_action(struct ata_device *dev)
264c6fd2807SJeff Garzik {
2659af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
266c6fd2807SJeff Garzik 
267c6fd2807SJeff Garzik 	return ehc->i.action | ehc->i.dev_action[dev->devno];
268c6fd2807SJeff Garzik }
269c6fd2807SJeff Garzik 
270f58229f8STejun Heo static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
271c6fd2807SJeff Garzik 				struct ata_eh_info *ehi, unsigned int action)
272c6fd2807SJeff Garzik {
273f58229f8STejun Heo 	struct ata_device *tdev;
274c6fd2807SJeff Garzik 
275c6fd2807SJeff Garzik 	if (!dev) {
276c6fd2807SJeff Garzik 		ehi->action &= ~action;
277f58229f8STejun Heo 		ata_link_for_each_dev(tdev, link)
278f58229f8STejun Heo 			ehi->dev_action[tdev->devno] &= ~action;
279c6fd2807SJeff Garzik 	} else {
280c6fd2807SJeff Garzik 		/* doesn't make sense for port-wide EH actions */
281c6fd2807SJeff Garzik 		WARN_ON(!(action & ATA_EH_PERDEV_MASK));
282c6fd2807SJeff Garzik 
283c6fd2807SJeff Garzik 		/* break ehi->action into ehi->dev_action */
284c6fd2807SJeff Garzik 		if (ehi->action & action) {
285f58229f8STejun Heo 			ata_link_for_each_dev(tdev, link)
286f58229f8STejun Heo 				ehi->dev_action[tdev->devno] |=
287f58229f8STejun Heo 					ehi->action & action;
288c6fd2807SJeff Garzik 			ehi->action &= ~action;
289c6fd2807SJeff Garzik 		}
290c6fd2807SJeff Garzik 
291c6fd2807SJeff Garzik 		/* turn off the specified per-dev action */
292c6fd2807SJeff Garzik 		ehi->dev_action[dev->devno] &= ~action;
293c6fd2807SJeff Garzik 	}
294c6fd2807SJeff Garzik }
295c6fd2807SJeff Garzik 
296c6fd2807SJeff Garzik /**
297c6fd2807SJeff Garzik  *	ata_scsi_timed_out - SCSI layer time out callback
298c6fd2807SJeff Garzik  *	@cmd: timed out SCSI command
299c6fd2807SJeff Garzik  *
300c6fd2807SJeff Garzik  *	Handles SCSI layer timeout.  We race with normal completion of
301c6fd2807SJeff Garzik  *	the qc for @cmd.  If the qc is already gone, we lose and let
302c6fd2807SJeff Garzik  *	the scsi command finish (EH_HANDLED).  Otherwise, the qc has
303c6fd2807SJeff Garzik  *	timed out and EH should be invoked.  Prevent ata_qc_complete()
304c6fd2807SJeff Garzik  *	from finishing it by setting EH_SCHEDULED and return
305c6fd2807SJeff Garzik  *	EH_NOT_HANDLED.
306c6fd2807SJeff Garzik  *
307c6fd2807SJeff Garzik  *	TODO: kill this function once old EH is gone.
308c6fd2807SJeff Garzik  *
309c6fd2807SJeff Garzik  *	LOCKING:
310c6fd2807SJeff Garzik  *	Called from timer context
311c6fd2807SJeff Garzik  *
312c6fd2807SJeff Garzik  *	RETURNS:
313c6fd2807SJeff Garzik  *	EH_HANDLED or EH_NOT_HANDLED
314c6fd2807SJeff Garzik  */
315c6fd2807SJeff Garzik enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
316c6fd2807SJeff Garzik {
317c6fd2807SJeff Garzik 	struct Scsi_Host *host = cmd->device->host;
318c6fd2807SJeff Garzik 	struct ata_port *ap = ata_shost_to_port(host);
319c6fd2807SJeff Garzik 	unsigned long flags;
320c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
321c6fd2807SJeff Garzik 	enum scsi_eh_timer_return ret;
322c6fd2807SJeff Garzik 
323c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
324c6fd2807SJeff Garzik 
325c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
326c6fd2807SJeff Garzik 		ret = EH_NOT_HANDLED;
327c6fd2807SJeff Garzik 		goto out;
328c6fd2807SJeff Garzik 	}
329c6fd2807SJeff Garzik 
330c6fd2807SJeff Garzik 	ret = EH_HANDLED;
331c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
3329af5c9c9STejun Heo 	qc = ata_qc_from_tag(ap, ap->link.active_tag);
333c6fd2807SJeff Garzik 	if (qc) {
334c6fd2807SJeff Garzik 		WARN_ON(qc->scsicmd != cmd);
335c6fd2807SJeff Garzik 		qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
336c6fd2807SJeff Garzik 		qc->err_mask |= AC_ERR_TIMEOUT;
337c6fd2807SJeff Garzik 		ret = EH_NOT_HANDLED;
338c6fd2807SJeff Garzik 	}
339c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
340c6fd2807SJeff Garzik 
341c6fd2807SJeff Garzik  out:
342c6fd2807SJeff Garzik 	DPRINTK("EXIT, ret=%d\n", ret);
343c6fd2807SJeff Garzik 	return ret;
344c6fd2807SJeff Garzik }
345c6fd2807SJeff Garzik 
346c6fd2807SJeff Garzik /**
347c6fd2807SJeff Garzik  *	ata_scsi_error - SCSI layer error handler callback
348c6fd2807SJeff Garzik  *	@host: SCSI host on which error occurred
349c6fd2807SJeff Garzik  *
350c6fd2807SJeff Garzik  *	Handles SCSI-layer-thrown error events.
351c6fd2807SJeff Garzik  *
352c6fd2807SJeff Garzik  *	LOCKING:
353c6fd2807SJeff Garzik  *	Inherited from SCSI layer (none, can sleep)
354c6fd2807SJeff Garzik  *
355c6fd2807SJeff Garzik  *	RETURNS:
356c6fd2807SJeff Garzik  *	Zero.
357c6fd2807SJeff Garzik  */
358c6fd2807SJeff Garzik void ata_scsi_error(struct Scsi_Host *host)
359c6fd2807SJeff Garzik {
360c6fd2807SJeff Garzik 	struct ata_port *ap = ata_shost_to_port(host);
361a1e10f7eSTejun Heo 	int i;
362c6fd2807SJeff Garzik 	unsigned long flags;
363c6fd2807SJeff Garzik 
364c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
365c6fd2807SJeff Garzik 
366c6fd2807SJeff Garzik 	/* synchronize with port task */
367c6fd2807SJeff Garzik 	ata_port_flush_task(ap);
368c6fd2807SJeff Garzik 
369cca3974eSJeff Garzik 	/* synchronize with host lock and sort out timeouts */
370c6fd2807SJeff Garzik 
371c6fd2807SJeff Garzik 	/* For new EH, all qcs are finished in one of three ways -
372c6fd2807SJeff Garzik 	 * normal completion, error completion, and SCSI timeout.
373c6fd2807SJeff Garzik 	 * Both cmpletions can race against SCSI timeout.  When normal
374c6fd2807SJeff Garzik 	 * completion wins, the qc never reaches EH.  When error
375c6fd2807SJeff Garzik 	 * completion wins, the qc has ATA_QCFLAG_FAILED set.
376c6fd2807SJeff Garzik 	 *
377c6fd2807SJeff Garzik 	 * When SCSI timeout wins, things are a bit more complex.
378c6fd2807SJeff Garzik 	 * Normal or error completion can occur after the timeout but
379c6fd2807SJeff Garzik 	 * before this point.  In such cases, both types of
380c6fd2807SJeff Garzik 	 * completions are honored.  A scmd is determined to have
381c6fd2807SJeff Garzik 	 * timed out iff its associated qc is active and not failed.
382c6fd2807SJeff Garzik 	 */
383c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
384c6fd2807SJeff Garzik 		struct scsi_cmnd *scmd, *tmp;
385c6fd2807SJeff Garzik 		int nr_timedout = 0;
386c6fd2807SJeff Garzik 
387c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
388c6fd2807SJeff Garzik 
389c6fd2807SJeff Garzik 		list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
390c6fd2807SJeff Garzik 			struct ata_queued_cmd *qc;
391c6fd2807SJeff Garzik 
392c6fd2807SJeff Garzik 			for (i = 0; i < ATA_MAX_QUEUE; i++) {
393c6fd2807SJeff Garzik 				qc = __ata_qc_from_tag(ap, i);
394c6fd2807SJeff Garzik 				if (qc->flags & ATA_QCFLAG_ACTIVE &&
395c6fd2807SJeff Garzik 				    qc->scsicmd == scmd)
396c6fd2807SJeff Garzik 					break;
397c6fd2807SJeff Garzik 			}
398c6fd2807SJeff Garzik 
399c6fd2807SJeff Garzik 			if (i < ATA_MAX_QUEUE) {
400c6fd2807SJeff Garzik 				/* the scmd has an associated qc */
401c6fd2807SJeff Garzik 				if (!(qc->flags & ATA_QCFLAG_FAILED)) {
402c6fd2807SJeff Garzik 					/* which hasn't failed yet, timeout */
403c6fd2807SJeff Garzik 					qc->err_mask |= AC_ERR_TIMEOUT;
404c6fd2807SJeff Garzik 					qc->flags |= ATA_QCFLAG_FAILED;
405c6fd2807SJeff Garzik 					nr_timedout++;
406c6fd2807SJeff Garzik 				}
407c6fd2807SJeff Garzik 			} else {
408c6fd2807SJeff Garzik 				/* Normal completion occurred after
409c6fd2807SJeff Garzik 				 * SCSI timeout but before this point.
410c6fd2807SJeff Garzik 				 * Successfully complete it.
411c6fd2807SJeff Garzik 				 */
412c6fd2807SJeff Garzik 				scmd->retries = scmd->allowed;
413c6fd2807SJeff Garzik 				scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
414c6fd2807SJeff Garzik 			}
415c6fd2807SJeff Garzik 		}
416c6fd2807SJeff Garzik 
417c6fd2807SJeff Garzik 		/* If we have timed out qcs.  They belong to EH from
418c6fd2807SJeff Garzik 		 * this point but the state of the controller is
419c6fd2807SJeff Garzik 		 * unknown.  Freeze the port to make sure the IRQ
420c6fd2807SJeff Garzik 		 * handler doesn't diddle with those qcs.  This must
421c6fd2807SJeff Garzik 		 * be done atomically w.r.t. setting QCFLAG_FAILED.
422c6fd2807SJeff Garzik 		 */
423c6fd2807SJeff Garzik 		if (nr_timedout)
424c6fd2807SJeff Garzik 			__ata_port_freeze(ap);
425c6fd2807SJeff Garzik 
426c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
427a1e10f7eSTejun Heo 
428a1e10f7eSTejun Heo 		/* initialize eh_tries */
429a1e10f7eSTejun Heo 		ap->eh_tries = ATA_EH_MAX_TRIES;
430c6fd2807SJeff Garzik 	} else
431c6fd2807SJeff Garzik 		spin_unlock_wait(ap->lock);
432c6fd2807SJeff Garzik 
433c6fd2807SJeff Garzik  repeat:
434c6fd2807SJeff Garzik 	/* invoke error handler */
435c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
436cf1b86c8STejun Heo 		struct ata_link *link;
437cf1b86c8STejun Heo 
4385ddf24c5STejun Heo 		/* kill fast drain timer */
4395ddf24c5STejun Heo 		del_timer_sync(&ap->fastdrain_timer);
4405ddf24c5STejun Heo 
441c6fd2807SJeff Garzik 		/* process port resume request */
442c6fd2807SJeff Garzik 		ata_eh_handle_port_resume(ap);
443c6fd2807SJeff Garzik 
444c6fd2807SJeff Garzik 		/* fetch & clear EH info */
445c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
446c6fd2807SJeff Garzik 
447cf1b86c8STejun Heo 		__ata_port_for_each_link(link, ap) {
448cf1b86c8STejun Heo 			memset(&link->eh_context, 0, sizeof(link->eh_context));
449cf1b86c8STejun Heo 			link->eh_context.i = link->eh_info;
450cf1b86c8STejun Heo 			memset(&link->eh_info, 0, sizeof(link->eh_info));
451cf1b86c8STejun Heo 		}
452c6fd2807SJeff Garzik 
453c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
454c6fd2807SJeff Garzik 		ap->pflags &= ~ATA_PFLAG_EH_PENDING;
455da917d69STejun Heo 		ap->excl_link = NULL;	/* don't maintain exclusion over EH */
456c6fd2807SJeff Garzik 
457c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
458c6fd2807SJeff Garzik 
459c6fd2807SJeff Garzik 		/* invoke EH, skip if unloading or suspended */
460c6fd2807SJeff Garzik 		if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
461c6fd2807SJeff Garzik 			ap->ops->error_handler(ap);
462c6fd2807SJeff Garzik 		else
463c6fd2807SJeff Garzik 			ata_eh_finish(ap);
464c6fd2807SJeff Garzik 
465c6fd2807SJeff Garzik 		/* process port suspend request */
466c6fd2807SJeff Garzik 		ata_eh_handle_port_suspend(ap);
467c6fd2807SJeff Garzik 
468c6fd2807SJeff Garzik 		/* Exception might have happend after ->error_handler
469c6fd2807SJeff Garzik 		 * recovered the port but before this point.  Repeat
470c6fd2807SJeff Garzik 		 * EH in such case.
471c6fd2807SJeff Garzik 		 */
472c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
473c6fd2807SJeff Garzik 
474c6fd2807SJeff Garzik 		if (ap->pflags & ATA_PFLAG_EH_PENDING) {
475a1e10f7eSTejun Heo 			if (--ap->eh_tries) {
476c6fd2807SJeff Garzik 				spin_unlock_irqrestore(ap->lock, flags);
477c6fd2807SJeff Garzik 				goto repeat;
478c6fd2807SJeff Garzik 			}
479c6fd2807SJeff Garzik 			ata_port_printk(ap, KERN_ERR, "EH pending after %d "
480a1e10f7eSTejun Heo 					"tries, giving up\n", ATA_EH_MAX_TRIES);
481914616a3STejun Heo 			ap->pflags &= ~ATA_PFLAG_EH_PENDING;
482c6fd2807SJeff Garzik 		}
483c6fd2807SJeff Garzik 
484c6fd2807SJeff Garzik 		/* this run is complete, make sure EH info is clear */
485cf1b86c8STejun Heo 		__ata_port_for_each_link(link, ap)
486cf1b86c8STejun Heo 			memset(&link->eh_info, 0, sizeof(link->eh_info));
487c6fd2807SJeff Garzik 
488c6fd2807SJeff Garzik 		/* Clear host_eh_scheduled while holding ap->lock such
489c6fd2807SJeff Garzik 		 * that if exception occurs after this point but
490c6fd2807SJeff Garzik 		 * before EH completion, SCSI midlayer will
491c6fd2807SJeff Garzik 		 * re-initiate EH.
492c6fd2807SJeff Garzik 		 */
493c6fd2807SJeff Garzik 		host->host_eh_scheduled = 0;
494c6fd2807SJeff Garzik 
495c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
496c6fd2807SJeff Garzik 	} else {
4979af5c9c9STejun Heo 		WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
498c6fd2807SJeff Garzik 		ap->ops->eng_timeout(ap);
499c6fd2807SJeff Garzik 	}
500c6fd2807SJeff Garzik 
501c6fd2807SJeff Garzik 	/* finish or retry handled scmd's and clean up */
502c6fd2807SJeff Garzik 	WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
503c6fd2807SJeff Garzik 
504c6fd2807SJeff Garzik 	scsi_eh_flush_done_q(&ap->eh_done_q);
505c6fd2807SJeff Garzik 
506c6fd2807SJeff Garzik 	/* clean up */
507c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
508c6fd2807SJeff Garzik 
509c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_LOADING)
510c6fd2807SJeff Garzik 		ap->pflags &= ~ATA_PFLAG_LOADING;
511c6fd2807SJeff Garzik 	else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
51252bad64dSDavid Howells 		queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0);
513c6fd2807SJeff Garzik 
514c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_RECOVERED)
515c6fd2807SJeff Garzik 		ata_port_printk(ap, KERN_INFO, "EH complete\n");
516c6fd2807SJeff Garzik 
517c6fd2807SJeff Garzik 	ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
518c6fd2807SJeff Garzik 
519c6fd2807SJeff Garzik 	/* tell wait_eh that we're done */
520c6fd2807SJeff Garzik 	ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
521c6fd2807SJeff Garzik 	wake_up_all(&ap->eh_wait_q);
522c6fd2807SJeff Garzik 
523c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
524c6fd2807SJeff Garzik 
525c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
526c6fd2807SJeff Garzik }
527c6fd2807SJeff Garzik 
528c6fd2807SJeff Garzik /**
529c6fd2807SJeff Garzik  *	ata_port_wait_eh - Wait for the currently pending EH to complete
530c6fd2807SJeff Garzik  *	@ap: Port to wait EH for
531c6fd2807SJeff Garzik  *
532c6fd2807SJeff Garzik  *	Wait until the currently pending EH is complete.
533c6fd2807SJeff Garzik  *
534c6fd2807SJeff Garzik  *	LOCKING:
535c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
536c6fd2807SJeff Garzik  */
537c6fd2807SJeff Garzik void ata_port_wait_eh(struct ata_port *ap)
538c6fd2807SJeff Garzik {
539c6fd2807SJeff Garzik 	unsigned long flags;
540c6fd2807SJeff Garzik 	DEFINE_WAIT(wait);
541c6fd2807SJeff Garzik 
542c6fd2807SJeff Garzik  retry:
543c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
544c6fd2807SJeff Garzik 
545c6fd2807SJeff Garzik 	while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
546c6fd2807SJeff Garzik 		prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
547c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
548c6fd2807SJeff Garzik 		schedule();
549c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
550c6fd2807SJeff Garzik 	}
551c6fd2807SJeff Garzik 	finish_wait(&ap->eh_wait_q, &wait);
552c6fd2807SJeff Garzik 
553c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
554c6fd2807SJeff Garzik 
555c6fd2807SJeff Garzik 	/* make sure SCSI EH is complete */
556cca3974eSJeff Garzik 	if (scsi_host_in_recovery(ap->scsi_host)) {
557c6fd2807SJeff Garzik 		msleep(10);
558c6fd2807SJeff Garzik 		goto retry;
559c6fd2807SJeff Garzik 	}
560c6fd2807SJeff Garzik }
561c6fd2807SJeff Garzik 
562c6fd2807SJeff Garzik /**
563c6fd2807SJeff Garzik  *	ata_qc_timeout - Handle timeout of queued command
564c6fd2807SJeff Garzik  *	@qc: Command that timed out
565c6fd2807SJeff Garzik  *
566c6fd2807SJeff Garzik  *	Some part of the kernel (currently, only the SCSI layer)
567c6fd2807SJeff Garzik  *	has noticed that the active command on port @ap has not
568c6fd2807SJeff Garzik  *	completed after a specified length of time.  Handle this
569c6fd2807SJeff Garzik  *	condition by disabling DMA (if necessary) and completing
570c6fd2807SJeff Garzik  *	transactions, with error if necessary.
571c6fd2807SJeff Garzik  *
572c6fd2807SJeff Garzik  *	This also handles the case of the "lost interrupt", where
573c6fd2807SJeff Garzik  *	for some reason (possibly hardware bug, possibly driver bug)
574c6fd2807SJeff Garzik  *	an interrupt was not delivered to the driver, even though the
575c6fd2807SJeff Garzik  *	transaction completed successfully.
576c6fd2807SJeff Garzik  *
577c6fd2807SJeff Garzik  *	TODO: kill this function once old EH is gone.
578c6fd2807SJeff Garzik  *
579c6fd2807SJeff Garzik  *	LOCKING:
580c6fd2807SJeff Garzik  *	Inherited from SCSI layer (none, can sleep)
581c6fd2807SJeff Garzik  */
582c6fd2807SJeff Garzik static void ata_qc_timeout(struct ata_queued_cmd *qc)
583c6fd2807SJeff Garzik {
584c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
585c6fd2807SJeff Garzik 	u8 host_stat = 0, drv_stat;
586c6fd2807SJeff Garzik 	unsigned long flags;
587c6fd2807SJeff Garzik 
588c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
589c6fd2807SJeff Garzik 
590c6fd2807SJeff Garzik 	ap->hsm_task_state = HSM_ST_IDLE;
591c6fd2807SJeff Garzik 
592c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
593c6fd2807SJeff Garzik 
594c6fd2807SJeff Garzik 	switch (qc->tf.protocol) {
595c6fd2807SJeff Garzik 
596c6fd2807SJeff Garzik 	case ATA_PROT_DMA:
597c6fd2807SJeff Garzik 	case ATA_PROT_ATAPI_DMA:
598c6fd2807SJeff Garzik 		host_stat = ap->ops->bmdma_status(ap);
599c6fd2807SJeff Garzik 
600c6fd2807SJeff Garzik 		/* before we do anything else, clear DMA-Start bit */
601c6fd2807SJeff Garzik 		ap->ops->bmdma_stop(qc);
602c6fd2807SJeff Garzik 
603c6fd2807SJeff Garzik 		/* fall through */
604c6fd2807SJeff Garzik 
605c6fd2807SJeff Garzik 	default:
606c6fd2807SJeff Garzik 		ata_altstatus(ap);
607c6fd2807SJeff Garzik 		drv_stat = ata_chk_status(ap);
608c6fd2807SJeff Garzik 
609c6fd2807SJeff Garzik 		/* ack bmdma irq events */
610c6fd2807SJeff Garzik 		ap->ops->irq_clear(ap);
611c6fd2807SJeff Garzik 
612c6fd2807SJeff Garzik 		ata_dev_printk(qc->dev, KERN_ERR, "command 0x%x timeout, "
613c6fd2807SJeff Garzik 			       "stat 0x%x host_stat 0x%x\n",
614c6fd2807SJeff Garzik 			       qc->tf.command, drv_stat, host_stat);
615c6fd2807SJeff Garzik 
616c6fd2807SJeff Garzik 		/* complete taskfile transaction */
617c6fd2807SJeff Garzik 		qc->err_mask |= AC_ERR_TIMEOUT;
618c6fd2807SJeff Garzik 		break;
619c6fd2807SJeff Garzik 	}
620c6fd2807SJeff Garzik 
621c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
622c6fd2807SJeff Garzik 
623c6fd2807SJeff Garzik 	ata_eh_qc_complete(qc);
624c6fd2807SJeff Garzik 
625c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
626c6fd2807SJeff Garzik }
627c6fd2807SJeff Garzik 
628c6fd2807SJeff Garzik /**
629c6fd2807SJeff Garzik  *	ata_eng_timeout - Handle timeout of queued command
630c6fd2807SJeff Garzik  *	@ap: Port on which timed-out command is active
631c6fd2807SJeff Garzik  *
632c6fd2807SJeff Garzik  *	Some part of the kernel (currently, only the SCSI layer)
633c6fd2807SJeff Garzik  *	has noticed that the active command on port @ap has not
634c6fd2807SJeff Garzik  *	completed after a specified length of time.  Handle this
635c6fd2807SJeff Garzik  *	condition by disabling DMA (if necessary) and completing
636c6fd2807SJeff Garzik  *	transactions, with error if necessary.
637c6fd2807SJeff Garzik  *
638c6fd2807SJeff Garzik  *	This also handles the case of the "lost interrupt", where
639c6fd2807SJeff Garzik  *	for some reason (possibly hardware bug, possibly driver bug)
640c6fd2807SJeff Garzik  *	an interrupt was not delivered to the driver, even though the
641c6fd2807SJeff Garzik  *	transaction completed successfully.
642c6fd2807SJeff Garzik  *
643c6fd2807SJeff Garzik  *	TODO: kill this function once old EH is gone.
644c6fd2807SJeff Garzik  *
645c6fd2807SJeff Garzik  *	LOCKING:
646c6fd2807SJeff Garzik  *	Inherited from SCSI layer (none, can sleep)
647c6fd2807SJeff Garzik  */
648c6fd2807SJeff Garzik void ata_eng_timeout(struct ata_port *ap)
649c6fd2807SJeff Garzik {
650c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
651c6fd2807SJeff Garzik 
6529af5c9c9STejun Heo 	ata_qc_timeout(ata_qc_from_tag(ap, ap->link.active_tag));
653c6fd2807SJeff Garzik 
654c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
655c6fd2807SJeff Garzik }
656c6fd2807SJeff Garzik 
6575ddf24c5STejun Heo static int ata_eh_nr_in_flight(struct ata_port *ap)
6585ddf24c5STejun Heo {
6595ddf24c5STejun Heo 	unsigned int tag;
6605ddf24c5STejun Heo 	int nr = 0;
6615ddf24c5STejun Heo 
6625ddf24c5STejun Heo 	/* count only non-internal commands */
6635ddf24c5STejun Heo 	for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++)
6645ddf24c5STejun Heo 		if (ata_qc_from_tag(ap, tag))
6655ddf24c5STejun Heo 			nr++;
6665ddf24c5STejun Heo 
6675ddf24c5STejun Heo 	return nr;
6685ddf24c5STejun Heo }
6695ddf24c5STejun Heo 
6705ddf24c5STejun Heo void ata_eh_fastdrain_timerfn(unsigned long arg)
6715ddf24c5STejun Heo {
6725ddf24c5STejun Heo 	struct ata_port *ap = (void *)arg;
6735ddf24c5STejun Heo 	unsigned long flags;
6745ddf24c5STejun Heo 	int cnt;
6755ddf24c5STejun Heo 
6765ddf24c5STejun Heo 	spin_lock_irqsave(ap->lock, flags);
6775ddf24c5STejun Heo 
6785ddf24c5STejun Heo 	cnt = ata_eh_nr_in_flight(ap);
6795ddf24c5STejun Heo 
6805ddf24c5STejun Heo 	/* are we done? */
6815ddf24c5STejun Heo 	if (!cnt)
6825ddf24c5STejun Heo 		goto out_unlock;
6835ddf24c5STejun Heo 
6845ddf24c5STejun Heo 	if (cnt == ap->fastdrain_cnt) {
6855ddf24c5STejun Heo 		unsigned int tag;
6865ddf24c5STejun Heo 
6875ddf24c5STejun Heo 		/* No progress during the last interval, tag all
6885ddf24c5STejun Heo 		 * in-flight qcs as timed out and freeze the port.
6895ddf24c5STejun Heo 		 */
6905ddf24c5STejun Heo 		for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) {
6915ddf24c5STejun Heo 			struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
6925ddf24c5STejun Heo 			if (qc)
6935ddf24c5STejun Heo 				qc->err_mask |= AC_ERR_TIMEOUT;
6945ddf24c5STejun Heo 		}
6955ddf24c5STejun Heo 
6965ddf24c5STejun Heo 		ata_port_freeze(ap);
6975ddf24c5STejun Heo 	} else {
6985ddf24c5STejun Heo 		/* some qcs have finished, give it another chance */
6995ddf24c5STejun Heo 		ap->fastdrain_cnt = cnt;
7005ddf24c5STejun Heo 		ap->fastdrain_timer.expires =
7015ddf24c5STejun Heo 			jiffies + ATA_EH_FASTDRAIN_INTERVAL;
7025ddf24c5STejun Heo 		add_timer(&ap->fastdrain_timer);
7035ddf24c5STejun Heo 	}
7045ddf24c5STejun Heo 
7055ddf24c5STejun Heo  out_unlock:
7065ddf24c5STejun Heo 	spin_unlock_irqrestore(ap->lock, flags);
7075ddf24c5STejun Heo }
7085ddf24c5STejun Heo 
7095ddf24c5STejun Heo /**
7105ddf24c5STejun Heo  *	ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
7115ddf24c5STejun Heo  *	@ap: target ATA port
7125ddf24c5STejun Heo  *	@fastdrain: activate fast drain
7135ddf24c5STejun Heo  *
7145ddf24c5STejun Heo  *	Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
7155ddf24c5STejun Heo  *	is non-zero and EH wasn't pending before.  Fast drain ensures
7165ddf24c5STejun Heo  *	that EH kicks in in timely manner.
7175ddf24c5STejun Heo  *
7185ddf24c5STejun Heo  *	LOCKING:
7195ddf24c5STejun Heo  *	spin_lock_irqsave(host lock)
7205ddf24c5STejun Heo  */
7215ddf24c5STejun Heo static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
7225ddf24c5STejun Heo {
7235ddf24c5STejun Heo 	int cnt;
7245ddf24c5STejun Heo 
7255ddf24c5STejun Heo 	/* already scheduled? */
7265ddf24c5STejun Heo 	if (ap->pflags & ATA_PFLAG_EH_PENDING)
7275ddf24c5STejun Heo 		return;
7285ddf24c5STejun Heo 
7295ddf24c5STejun Heo 	ap->pflags |= ATA_PFLAG_EH_PENDING;
7305ddf24c5STejun Heo 
7315ddf24c5STejun Heo 	if (!fastdrain)
7325ddf24c5STejun Heo 		return;
7335ddf24c5STejun Heo 
7345ddf24c5STejun Heo 	/* do we have in-flight qcs? */
7355ddf24c5STejun Heo 	cnt = ata_eh_nr_in_flight(ap);
7365ddf24c5STejun Heo 	if (!cnt)
7375ddf24c5STejun Heo 		return;
7385ddf24c5STejun Heo 
7395ddf24c5STejun Heo 	/* activate fast drain */
7405ddf24c5STejun Heo 	ap->fastdrain_cnt = cnt;
7415ddf24c5STejun Heo 	ap->fastdrain_timer.expires = jiffies + ATA_EH_FASTDRAIN_INTERVAL;
7425ddf24c5STejun Heo 	add_timer(&ap->fastdrain_timer);
7435ddf24c5STejun Heo }
7445ddf24c5STejun Heo 
745c6fd2807SJeff Garzik /**
746c6fd2807SJeff Garzik  *	ata_qc_schedule_eh - schedule qc for error handling
747c6fd2807SJeff Garzik  *	@qc: command to schedule error handling for
748c6fd2807SJeff Garzik  *
749c6fd2807SJeff Garzik  *	Schedule error handling for @qc.  EH will kick in as soon as
750c6fd2807SJeff Garzik  *	other commands are drained.
751c6fd2807SJeff Garzik  *
752c6fd2807SJeff Garzik  *	LOCKING:
753cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
754c6fd2807SJeff Garzik  */
755c6fd2807SJeff Garzik void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
756c6fd2807SJeff Garzik {
757c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
758c6fd2807SJeff Garzik 
759c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
760c6fd2807SJeff Garzik 
761c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_FAILED;
7625ddf24c5STejun Heo 	ata_eh_set_pending(ap, 1);
763c6fd2807SJeff Garzik 
764c6fd2807SJeff Garzik 	/* The following will fail if timeout has already expired.
765c6fd2807SJeff Garzik 	 * ata_scsi_error() takes care of such scmds on EH entry.
766c6fd2807SJeff Garzik 	 * Note that ATA_QCFLAG_FAILED is unconditionally set after
767c6fd2807SJeff Garzik 	 * this function completes.
768c6fd2807SJeff Garzik 	 */
769c6fd2807SJeff Garzik 	scsi_req_abort_cmd(qc->scsicmd);
770c6fd2807SJeff Garzik }
771c6fd2807SJeff Garzik 
772c6fd2807SJeff Garzik /**
773c6fd2807SJeff Garzik  *	ata_port_schedule_eh - schedule error handling without a qc
774c6fd2807SJeff Garzik  *	@ap: ATA port to schedule EH for
775c6fd2807SJeff Garzik  *
776c6fd2807SJeff Garzik  *	Schedule error handling for @ap.  EH will kick in as soon as
777c6fd2807SJeff Garzik  *	all commands are drained.
778c6fd2807SJeff Garzik  *
779c6fd2807SJeff Garzik  *	LOCKING:
780cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
781c6fd2807SJeff Garzik  */
782c6fd2807SJeff Garzik void ata_port_schedule_eh(struct ata_port *ap)
783c6fd2807SJeff Garzik {
784c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
785c6fd2807SJeff Garzik 
786f4d6d004STejun Heo 	if (ap->pflags & ATA_PFLAG_INITIALIZING)
787f4d6d004STejun Heo 		return;
788f4d6d004STejun Heo 
7895ddf24c5STejun Heo 	ata_eh_set_pending(ap, 1);
790cca3974eSJeff Garzik 	scsi_schedule_eh(ap->scsi_host);
791c6fd2807SJeff Garzik 
792c6fd2807SJeff Garzik 	DPRINTK("port EH scheduled\n");
793c6fd2807SJeff Garzik }
794c6fd2807SJeff Garzik 
795dbd82616STejun Heo static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
796c6fd2807SJeff Garzik {
797c6fd2807SJeff Garzik 	int tag, nr_aborted = 0;
798c6fd2807SJeff Garzik 
799c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
800c6fd2807SJeff Garzik 
8015ddf24c5STejun Heo 	/* we're gonna abort all commands, no need for fast drain */
8025ddf24c5STejun Heo 	ata_eh_set_pending(ap, 0);
8035ddf24c5STejun Heo 
804c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
805c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
806c6fd2807SJeff Garzik 
807dbd82616STejun Heo 		if (qc && (!link || qc->dev->link == link)) {
808c6fd2807SJeff Garzik 			qc->flags |= ATA_QCFLAG_FAILED;
809c6fd2807SJeff Garzik 			ata_qc_complete(qc);
810c6fd2807SJeff Garzik 			nr_aborted++;
811c6fd2807SJeff Garzik 		}
812c6fd2807SJeff Garzik 	}
813c6fd2807SJeff Garzik 
814c6fd2807SJeff Garzik 	if (!nr_aborted)
815c6fd2807SJeff Garzik 		ata_port_schedule_eh(ap);
816c6fd2807SJeff Garzik 
817c6fd2807SJeff Garzik 	return nr_aborted;
818c6fd2807SJeff Garzik }
819c6fd2807SJeff Garzik 
820c6fd2807SJeff Garzik /**
821dbd82616STejun Heo  *	ata_link_abort - abort all qc's on the link
822dbd82616STejun Heo  *	@link: ATA link to abort qc's for
823dbd82616STejun Heo  *
824dbd82616STejun Heo  *	Abort all active qc's active on @link and schedule EH.
825dbd82616STejun Heo  *
826dbd82616STejun Heo  *	LOCKING:
827dbd82616STejun Heo  *	spin_lock_irqsave(host lock)
828dbd82616STejun Heo  *
829dbd82616STejun Heo  *	RETURNS:
830dbd82616STejun Heo  *	Number of aborted qc's.
831dbd82616STejun Heo  */
832dbd82616STejun Heo int ata_link_abort(struct ata_link *link)
833dbd82616STejun Heo {
834dbd82616STejun Heo 	return ata_do_link_abort(link->ap, link);
835dbd82616STejun Heo }
836dbd82616STejun Heo 
837dbd82616STejun Heo /**
838dbd82616STejun Heo  *	ata_port_abort - abort all qc's on the port
839dbd82616STejun Heo  *	@ap: ATA port to abort qc's for
840dbd82616STejun Heo  *
841dbd82616STejun Heo  *	Abort all active qc's of @ap and schedule EH.
842dbd82616STejun Heo  *
843dbd82616STejun Heo  *	LOCKING:
844dbd82616STejun Heo  *	spin_lock_irqsave(host_set lock)
845dbd82616STejun Heo  *
846dbd82616STejun Heo  *	RETURNS:
847dbd82616STejun Heo  *	Number of aborted qc's.
848dbd82616STejun Heo  */
849dbd82616STejun Heo int ata_port_abort(struct ata_port *ap)
850dbd82616STejun Heo {
851dbd82616STejun Heo 	return ata_do_link_abort(ap, NULL);
852dbd82616STejun Heo }
853dbd82616STejun Heo 
854dbd82616STejun Heo /**
855c6fd2807SJeff Garzik  *	__ata_port_freeze - freeze port
856c6fd2807SJeff Garzik  *	@ap: ATA port to freeze
857c6fd2807SJeff Garzik  *
858c6fd2807SJeff Garzik  *	This function is called when HSM violation or some other
859c6fd2807SJeff Garzik  *	condition disrupts normal operation of the port.  Frozen port
860c6fd2807SJeff Garzik  *	is not allowed to perform any operation until the port is
861c6fd2807SJeff Garzik  *	thawed, which usually follows a successful reset.
862c6fd2807SJeff Garzik  *
863c6fd2807SJeff Garzik  *	ap->ops->freeze() callback can be used for freezing the port
864c6fd2807SJeff Garzik  *	hardware-wise (e.g. mask interrupt and stop DMA engine).  If a
865c6fd2807SJeff Garzik  *	port cannot be frozen hardware-wise, the interrupt handler
866c6fd2807SJeff Garzik  *	must ack and clear interrupts unconditionally while the port
867c6fd2807SJeff Garzik  *	is frozen.
868c6fd2807SJeff Garzik  *
869c6fd2807SJeff Garzik  *	LOCKING:
870cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
871c6fd2807SJeff Garzik  */
872c6fd2807SJeff Garzik static void __ata_port_freeze(struct ata_port *ap)
873c6fd2807SJeff Garzik {
874c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
875c6fd2807SJeff Garzik 
876c6fd2807SJeff Garzik 	if (ap->ops->freeze)
877c6fd2807SJeff Garzik 		ap->ops->freeze(ap);
878c6fd2807SJeff Garzik 
879c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_FROZEN;
880c6fd2807SJeff Garzik 
88144877b4eSTejun Heo 	DPRINTK("ata%u port frozen\n", ap->print_id);
882c6fd2807SJeff Garzik }
883c6fd2807SJeff Garzik 
884c6fd2807SJeff Garzik /**
885c6fd2807SJeff Garzik  *	ata_port_freeze - abort & freeze port
886c6fd2807SJeff Garzik  *	@ap: ATA port to freeze
887c6fd2807SJeff Garzik  *
888c6fd2807SJeff Garzik  *	Abort and freeze @ap.
889c6fd2807SJeff Garzik  *
890c6fd2807SJeff Garzik  *	LOCKING:
891cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
892c6fd2807SJeff Garzik  *
893c6fd2807SJeff Garzik  *	RETURNS:
894c6fd2807SJeff Garzik  *	Number of aborted commands.
895c6fd2807SJeff Garzik  */
896c6fd2807SJeff Garzik int ata_port_freeze(struct ata_port *ap)
897c6fd2807SJeff Garzik {
898c6fd2807SJeff Garzik 	int nr_aborted;
899c6fd2807SJeff Garzik 
900c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
901c6fd2807SJeff Garzik 
902c6fd2807SJeff Garzik 	nr_aborted = ata_port_abort(ap);
903c6fd2807SJeff Garzik 	__ata_port_freeze(ap);
904c6fd2807SJeff Garzik 
905c6fd2807SJeff Garzik 	return nr_aborted;
906c6fd2807SJeff Garzik }
907c6fd2807SJeff Garzik 
908c6fd2807SJeff Garzik /**
9097d77b247STejun Heo  *	sata_async_notification - SATA async notification handler
9107d77b247STejun Heo  *	@ap: ATA port where async notification is received
9117d77b247STejun Heo  *
9127d77b247STejun Heo  *	Handler to be called when async notification via SDB FIS is
9137d77b247STejun Heo  *	received.  This function schedules EH if necessary.
9147d77b247STejun Heo  *
9157d77b247STejun Heo  *	LOCKING:
9167d77b247STejun Heo  *	spin_lock_irqsave(host lock)
9177d77b247STejun Heo  *
9187d77b247STejun Heo  *	RETURNS:
9197d77b247STejun Heo  *	1 if EH is scheduled, 0 otherwise.
9207d77b247STejun Heo  */
9217d77b247STejun Heo int sata_async_notification(struct ata_port *ap)
9227d77b247STejun Heo {
9237d77b247STejun Heo 	u32 sntf;
9247d77b247STejun Heo 	int rc;
9257d77b247STejun Heo 
9267d77b247STejun Heo 	if (!(ap->flags & ATA_FLAG_AN))
9277d77b247STejun Heo 		return 0;
9287d77b247STejun Heo 
9297d77b247STejun Heo 	rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
9307d77b247STejun Heo 	if (rc == 0)
9317d77b247STejun Heo 		sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
9327d77b247STejun Heo 
9337d77b247STejun Heo 	if (!ap->nr_pmp_links || rc) {
9347d77b247STejun Heo 		/* PMP is not attached or SNTF is not available */
9357d77b247STejun Heo 		if (!ap->nr_pmp_links) {
9367d77b247STejun Heo 			/* PMP is not attached.  Check whether ATAPI
9377d77b247STejun Heo 			 * AN is configured.  If so, notify media
9387d77b247STejun Heo 			 * change.
9397d77b247STejun Heo 			 */
9407d77b247STejun Heo 			struct ata_device *dev = ap->link.device;
9417d77b247STejun Heo 
9427d77b247STejun Heo 			if ((dev->class == ATA_DEV_ATAPI) &&
9437d77b247STejun Heo 			    (dev->flags & ATA_DFLAG_AN))
9447d77b247STejun Heo 				ata_scsi_media_change_notify(dev);
9457d77b247STejun Heo 			return 0;
9467d77b247STejun Heo 		} else {
9477d77b247STejun Heo 			/* PMP is attached but SNTF is not available.
9487d77b247STejun Heo 			 * ATAPI async media change notification is
9497d77b247STejun Heo 			 * not used.  The PMP must be reporting PHY
9507d77b247STejun Heo 			 * status change, schedule EH.
9517d77b247STejun Heo 			 */
9527d77b247STejun Heo 			ata_port_schedule_eh(ap);
9537d77b247STejun Heo 			return 1;
9547d77b247STejun Heo 		}
9557d77b247STejun Heo 	} else {
9567d77b247STejun Heo 		/* PMP is attached and SNTF is available */
9577d77b247STejun Heo 		struct ata_link *link;
9587d77b247STejun Heo 
9597d77b247STejun Heo 		/* check and notify ATAPI AN */
9607d77b247STejun Heo 		ata_port_for_each_link(link, ap) {
9617d77b247STejun Heo 			if (!(sntf & (1 << link->pmp)))
9627d77b247STejun Heo 				continue;
9637d77b247STejun Heo 
9647d77b247STejun Heo 			if ((link->device->class == ATA_DEV_ATAPI) &&
9657d77b247STejun Heo 			    (link->device->flags & ATA_DFLAG_AN))
9667d77b247STejun Heo 				ata_scsi_media_change_notify(link->device);
9677d77b247STejun Heo 		}
9687d77b247STejun Heo 
9697d77b247STejun Heo 		/* If PMP is reporting that PHY status of some
9707d77b247STejun Heo 		 * downstream ports has changed, schedule EH.
9717d77b247STejun Heo 		 */
9727d77b247STejun Heo 		if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
9737d77b247STejun Heo 			ata_port_schedule_eh(ap);
9747d77b247STejun Heo 			return 1;
9757d77b247STejun Heo 		}
9767d77b247STejun Heo 
9777d77b247STejun Heo 		return 0;
9787d77b247STejun Heo 	}
9797d77b247STejun Heo }
9807d77b247STejun Heo 
9817d77b247STejun Heo /**
982c6fd2807SJeff Garzik  *	ata_eh_freeze_port - EH helper to freeze port
983c6fd2807SJeff Garzik  *	@ap: ATA port to freeze
984c6fd2807SJeff Garzik  *
985c6fd2807SJeff Garzik  *	Freeze @ap.
986c6fd2807SJeff Garzik  *
987c6fd2807SJeff Garzik  *	LOCKING:
988c6fd2807SJeff Garzik  *	None.
989c6fd2807SJeff Garzik  */
990c6fd2807SJeff Garzik void ata_eh_freeze_port(struct ata_port *ap)
991c6fd2807SJeff Garzik {
992c6fd2807SJeff Garzik 	unsigned long flags;
993c6fd2807SJeff Garzik 
994c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
995c6fd2807SJeff Garzik 		return;
996c6fd2807SJeff Garzik 
997c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
998c6fd2807SJeff Garzik 	__ata_port_freeze(ap);
999c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1000c6fd2807SJeff Garzik }
1001c6fd2807SJeff Garzik 
1002c6fd2807SJeff Garzik /**
1003c6fd2807SJeff Garzik  *	ata_port_thaw_port - EH helper to thaw port
1004c6fd2807SJeff Garzik  *	@ap: ATA port to thaw
1005c6fd2807SJeff Garzik  *
1006c6fd2807SJeff Garzik  *	Thaw frozen port @ap.
1007c6fd2807SJeff Garzik  *
1008c6fd2807SJeff Garzik  *	LOCKING:
1009c6fd2807SJeff Garzik  *	None.
1010c6fd2807SJeff Garzik  */
1011c6fd2807SJeff Garzik void ata_eh_thaw_port(struct ata_port *ap)
1012c6fd2807SJeff Garzik {
1013c6fd2807SJeff Garzik 	unsigned long flags;
1014c6fd2807SJeff Garzik 
1015c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
1016c6fd2807SJeff Garzik 		return;
1017c6fd2807SJeff Garzik 
1018c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1019c6fd2807SJeff Garzik 
1020c6fd2807SJeff Garzik 	ap->pflags &= ~ATA_PFLAG_FROZEN;
1021c6fd2807SJeff Garzik 
1022c6fd2807SJeff Garzik 	if (ap->ops->thaw)
1023c6fd2807SJeff Garzik 		ap->ops->thaw(ap);
1024c6fd2807SJeff Garzik 
1025c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1026c6fd2807SJeff Garzik 
102744877b4eSTejun Heo 	DPRINTK("ata%u port thawed\n", ap->print_id);
1028c6fd2807SJeff Garzik }
1029c6fd2807SJeff Garzik 
1030c6fd2807SJeff Garzik static void ata_eh_scsidone(struct scsi_cmnd *scmd)
1031c6fd2807SJeff Garzik {
1032c6fd2807SJeff Garzik 	/* nada */
1033c6fd2807SJeff Garzik }
1034c6fd2807SJeff Garzik 
1035c6fd2807SJeff Garzik static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
1036c6fd2807SJeff Garzik {
1037c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
1038c6fd2807SJeff Garzik 	struct scsi_cmnd *scmd = qc->scsicmd;
1039c6fd2807SJeff Garzik 	unsigned long flags;
1040c6fd2807SJeff Garzik 
1041c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1042c6fd2807SJeff Garzik 	qc->scsidone = ata_eh_scsidone;
1043c6fd2807SJeff Garzik 	__ata_qc_complete(qc);
1044c6fd2807SJeff Garzik 	WARN_ON(ata_tag_valid(qc->tag));
1045c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1046c6fd2807SJeff Garzik 
1047c6fd2807SJeff Garzik 	scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
1048c6fd2807SJeff Garzik }
1049c6fd2807SJeff Garzik 
1050c6fd2807SJeff Garzik /**
1051c6fd2807SJeff Garzik  *	ata_eh_qc_complete - Complete an active ATA command from EH
1052c6fd2807SJeff Garzik  *	@qc: Command to complete
1053c6fd2807SJeff Garzik  *
1054c6fd2807SJeff Garzik  *	Indicate to the mid and upper layers that an ATA command has
1055c6fd2807SJeff Garzik  *	completed.  To be used from EH.
1056c6fd2807SJeff Garzik  */
1057c6fd2807SJeff Garzik void ata_eh_qc_complete(struct ata_queued_cmd *qc)
1058c6fd2807SJeff Garzik {
1059c6fd2807SJeff Garzik 	struct scsi_cmnd *scmd = qc->scsicmd;
1060c6fd2807SJeff Garzik 	scmd->retries = scmd->allowed;
1061c6fd2807SJeff Garzik 	__ata_eh_qc_complete(qc);
1062c6fd2807SJeff Garzik }
1063c6fd2807SJeff Garzik 
1064c6fd2807SJeff Garzik /**
1065c6fd2807SJeff Garzik  *	ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
1066c6fd2807SJeff Garzik  *	@qc: Command to retry
1067c6fd2807SJeff Garzik  *
1068c6fd2807SJeff Garzik  *	Indicate to the mid and upper layers that an ATA command
1069c6fd2807SJeff Garzik  *	should be retried.  To be used from EH.
1070c6fd2807SJeff Garzik  *
1071c6fd2807SJeff Garzik  *	SCSI midlayer limits the number of retries to scmd->allowed.
1072c6fd2807SJeff Garzik  *	scmd->retries is decremented for commands which get retried
1073c6fd2807SJeff Garzik  *	due to unrelated failures (qc->err_mask is zero).
1074c6fd2807SJeff Garzik  */
1075c6fd2807SJeff Garzik void ata_eh_qc_retry(struct ata_queued_cmd *qc)
1076c6fd2807SJeff Garzik {
1077c6fd2807SJeff Garzik 	struct scsi_cmnd *scmd = qc->scsicmd;
1078c6fd2807SJeff Garzik 	if (!qc->err_mask && scmd->retries)
1079c6fd2807SJeff Garzik 		scmd->retries--;
1080c6fd2807SJeff Garzik 	__ata_eh_qc_complete(qc);
1081c6fd2807SJeff Garzik }
1082c6fd2807SJeff Garzik 
1083c6fd2807SJeff Garzik /**
1084c6fd2807SJeff Garzik  *	ata_eh_detach_dev - detach ATA device
1085c6fd2807SJeff Garzik  *	@dev: ATA device to detach
1086c6fd2807SJeff Garzik  *
1087c6fd2807SJeff Garzik  *	Detach @dev.
1088c6fd2807SJeff Garzik  *
1089c6fd2807SJeff Garzik  *	LOCKING:
1090c6fd2807SJeff Garzik  *	None.
1091c6fd2807SJeff Garzik  */
1092fb7fd614STejun Heo void ata_eh_detach_dev(struct ata_device *dev)
1093c6fd2807SJeff Garzik {
1094f58229f8STejun Heo 	struct ata_link *link = dev->link;
1095f58229f8STejun Heo 	struct ata_port *ap = link->ap;
1096c6fd2807SJeff Garzik 	unsigned long flags;
1097c6fd2807SJeff Garzik 
1098c6fd2807SJeff Garzik 	ata_dev_disable(dev);
1099c6fd2807SJeff Garzik 
1100c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1101c6fd2807SJeff Garzik 
1102c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_DETACH;
1103c6fd2807SJeff Garzik 
1104c6fd2807SJeff Garzik 	if (ata_scsi_offline_dev(dev)) {
1105c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_DETACHED;
1106c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
1107c6fd2807SJeff Garzik 	}
1108c6fd2807SJeff Garzik 
1109c6fd2807SJeff Garzik 	/* clear per-dev EH actions */
1110f58229f8STejun Heo 	ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
1111f58229f8STejun Heo 	ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
1112c6fd2807SJeff Garzik 
1113c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1114c6fd2807SJeff Garzik }
1115c6fd2807SJeff Garzik 
1116c6fd2807SJeff Garzik /**
1117c6fd2807SJeff Garzik  *	ata_eh_about_to_do - about to perform eh_action
1118955e57dfSTejun Heo  *	@link: target ATA link
1119c6fd2807SJeff Garzik  *	@dev: target ATA dev for per-dev action (can be NULL)
1120c6fd2807SJeff Garzik  *	@action: action about to be performed
1121c6fd2807SJeff Garzik  *
1122c6fd2807SJeff Garzik  *	Called just before performing EH actions to clear related bits
1123955e57dfSTejun Heo  *	in @link->eh_info such that eh actions are not unnecessarily
1124955e57dfSTejun Heo  *	repeated.
1125c6fd2807SJeff Garzik  *
1126c6fd2807SJeff Garzik  *	LOCKING:
1127c6fd2807SJeff Garzik  *	None.
1128c6fd2807SJeff Garzik  */
1129fb7fd614STejun Heo void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
1130c6fd2807SJeff Garzik 			unsigned int action)
1131c6fd2807SJeff Garzik {
1132955e57dfSTejun Heo 	struct ata_port *ap = link->ap;
1133955e57dfSTejun Heo 	struct ata_eh_info *ehi = &link->eh_info;
1134955e57dfSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
1135c6fd2807SJeff Garzik 	unsigned long flags;
1136c6fd2807SJeff Garzik 
1137c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1138c6fd2807SJeff Garzik 
1139c6fd2807SJeff Garzik 	/* Reset is represented by combination of actions and EHI
1140c6fd2807SJeff Garzik 	 * flags.  Suck in all related bits before clearing eh_info to
1141c6fd2807SJeff Garzik 	 * avoid losing requested action.
1142c6fd2807SJeff Garzik 	 */
1143c6fd2807SJeff Garzik 	if (action & ATA_EH_RESET_MASK) {
1144c6fd2807SJeff Garzik 		ehc->i.action |= ehi->action & ATA_EH_RESET_MASK;
1145c6fd2807SJeff Garzik 		ehc->i.flags |= ehi->flags & ATA_EHI_RESET_MODIFIER_MASK;
1146c6fd2807SJeff Garzik 
1147c6fd2807SJeff Garzik 		/* make sure all reset actions are cleared & clear EHI flags */
1148c6fd2807SJeff Garzik 		action |= ATA_EH_RESET_MASK;
1149c6fd2807SJeff Garzik 		ehi->flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
1150c6fd2807SJeff Garzik 	}
1151c6fd2807SJeff Garzik 
1152955e57dfSTejun Heo 	ata_eh_clear_action(link, dev, ehi, action);
1153c6fd2807SJeff Garzik 
1154c6fd2807SJeff Garzik 	if (!(ehc->i.flags & ATA_EHI_QUIET))
1155c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_RECOVERED;
1156c6fd2807SJeff Garzik 
1157c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1158c6fd2807SJeff Garzik }
1159c6fd2807SJeff Garzik 
1160c6fd2807SJeff Garzik /**
1161c6fd2807SJeff Garzik  *	ata_eh_done - EH action complete
1162c6fd2807SJeff Garzik *	@ap: target ATA port
1163c6fd2807SJeff Garzik  *	@dev: target ATA dev for per-dev action (can be NULL)
1164c6fd2807SJeff Garzik  *	@action: action just completed
1165c6fd2807SJeff Garzik  *
1166c6fd2807SJeff Garzik  *	Called right after performing EH actions to clear related bits
1167955e57dfSTejun Heo  *	in @link->eh_context.
1168c6fd2807SJeff Garzik  *
1169c6fd2807SJeff Garzik  *	LOCKING:
1170c6fd2807SJeff Garzik  *	None.
1171c6fd2807SJeff Garzik  */
1172fb7fd614STejun Heo void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1173c6fd2807SJeff Garzik 		 unsigned int action)
1174c6fd2807SJeff Garzik {
1175955e57dfSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
11769af5c9c9STejun Heo 
1177c6fd2807SJeff Garzik 	/* if reset is complete, clear all reset actions & reset modifier */
1178c6fd2807SJeff Garzik 	if (action & ATA_EH_RESET_MASK) {
1179c6fd2807SJeff Garzik 		action |= ATA_EH_RESET_MASK;
11809af5c9c9STejun Heo 		ehc->i.flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
1181c6fd2807SJeff Garzik 	}
1182c6fd2807SJeff Garzik 
1183955e57dfSTejun Heo 	ata_eh_clear_action(link, dev, &ehc->i, action);
1184c6fd2807SJeff Garzik }
1185c6fd2807SJeff Garzik 
1186c6fd2807SJeff Garzik /**
1187c6fd2807SJeff Garzik  *	ata_err_string - convert err_mask to descriptive string
1188c6fd2807SJeff Garzik  *	@err_mask: error mask to convert to string
1189c6fd2807SJeff Garzik  *
1190c6fd2807SJeff Garzik  *	Convert @err_mask to descriptive string.  Errors are
1191c6fd2807SJeff Garzik  *	prioritized according to severity and only the most severe
1192c6fd2807SJeff Garzik  *	error is reported.
1193c6fd2807SJeff Garzik  *
1194c6fd2807SJeff Garzik  *	LOCKING:
1195c6fd2807SJeff Garzik  *	None.
1196c6fd2807SJeff Garzik  *
1197c6fd2807SJeff Garzik  *	RETURNS:
1198c6fd2807SJeff Garzik  *	Descriptive string for @err_mask
1199c6fd2807SJeff Garzik  */
1200c6fd2807SJeff Garzik static const char * ata_err_string(unsigned int err_mask)
1201c6fd2807SJeff Garzik {
1202c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_HOST_BUS)
1203c6fd2807SJeff Garzik 		return "host bus error";
1204c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_ATA_BUS)
1205c6fd2807SJeff Garzik 		return "ATA bus error";
1206c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_TIMEOUT)
1207c6fd2807SJeff Garzik 		return "timeout";
1208c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_HSM)
1209c6fd2807SJeff Garzik 		return "HSM violation";
1210c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_SYSTEM)
1211c6fd2807SJeff Garzik 		return "internal error";
1212c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_MEDIA)
1213c6fd2807SJeff Garzik 		return "media error";
1214c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_INVALID)
1215c6fd2807SJeff Garzik 		return "invalid argument";
1216c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_DEV)
1217c6fd2807SJeff Garzik 		return "device error";
1218c6fd2807SJeff Garzik 	return "unknown error";
1219c6fd2807SJeff Garzik }
1220c6fd2807SJeff Garzik 
1221c6fd2807SJeff Garzik /**
1222c6fd2807SJeff Garzik  *	ata_read_log_page - read a specific log page
1223c6fd2807SJeff Garzik  *	@dev: target device
1224c6fd2807SJeff Garzik  *	@page: page to read
1225c6fd2807SJeff Garzik  *	@buf: buffer to store read page
1226c6fd2807SJeff Garzik  *	@sectors: number of sectors to read
1227c6fd2807SJeff Garzik  *
1228c6fd2807SJeff Garzik  *	Read log page using READ_LOG_EXT command.
1229c6fd2807SJeff Garzik  *
1230c6fd2807SJeff Garzik  *	LOCKING:
1231c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1232c6fd2807SJeff Garzik  *
1233c6fd2807SJeff Garzik  *	RETURNS:
1234c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
1235c6fd2807SJeff Garzik  */
1236c6fd2807SJeff Garzik static unsigned int ata_read_log_page(struct ata_device *dev,
1237c6fd2807SJeff Garzik 				      u8 page, void *buf, unsigned int sectors)
1238c6fd2807SJeff Garzik {
1239c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1240c6fd2807SJeff Garzik 	unsigned int err_mask;
1241c6fd2807SJeff Garzik 
1242c6fd2807SJeff Garzik 	DPRINTK("read log page - page %d\n", page);
1243c6fd2807SJeff Garzik 
1244c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
1245c6fd2807SJeff Garzik 	tf.command = ATA_CMD_READ_LOG_EXT;
1246c6fd2807SJeff Garzik 	tf.lbal = page;
1247c6fd2807SJeff Garzik 	tf.nsect = sectors;
1248c6fd2807SJeff Garzik 	tf.hob_nsect = sectors >> 8;
1249c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1250c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_PIO;
1251c6fd2807SJeff Garzik 
1252c6fd2807SJeff Garzik 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
12532b789108STejun Heo 				     buf, sectors * ATA_SECT_SIZE, 0);
1254c6fd2807SJeff Garzik 
1255c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
1256c6fd2807SJeff Garzik 	return err_mask;
1257c6fd2807SJeff Garzik }
1258c6fd2807SJeff Garzik 
1259c6fd2807SJeff Garzik /**
1260c6fd2807SJeff Garzik  *	ata_eh_read_log_10h - Read log page 10h for NCQ error details
1261c6fd2807SJeff Garzik  *	@dev: Device to read log page 10h from
1262c6fd2807SJeff Garzik  *	@tag: Resulting tag of the failed command
1263c6fd2807SJeff Garzik  *	@tf: Resulting taskfile registers of the failed command
1264c6fd2807SJeff Garzik  *
1265c6fd2807SJeff Garzik  *	Read log page 10h to obtain NCQ error details and clear error
1266c6fd2807SJeff Garzik  *	condition.
1267c6fd2807SJeff Garzik  *
1268c6fd2807SJeff Garzik  *	LOCKING:
1269c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1270c6fd2807SJeff Garzik  *
1271c6fd2807SJeff Garzik  *	RETURNS:
1272c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
1273c6fd2807SJeff Garzik  */
1274c6fd2807SJeff Garzik static int ata_eh_read_log_10h(struct ata_device *dev,
1275c6fd2807SJeff Garzik 			       int *tag, struct ata_taskfile *tf)
1276c6fd2807SJeff Garzik {
12779af5c9c9STejun Heo 	u8 *buf = dev->link->ap->sector_buf;
1278c6fd2807SJeff Garzik 	unsigned int err_mask;
1279c6fd2807SJeff Garzik 	u8 csum;
1280c6fd2807SJeff Garzik 	int i;
1281c6fd2807SJeff Garzik 
1282c6fd2807SJeff Garzik 	err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
1283c6fd2807SJeff Garzik 	if (err_mask)
1284c6fd2807SJeff Garzik 		return -EIO;
1285c6fd2807SJeff Garzik 
1286c6fd2807SJeff Garzik 	csum = 0;
1287c6fd2807SJeff Garzik 	for (i = 0; i < ATA_SECT_SIZE; i++)
1288c6fd2807SJeff Garzik 		csum += buf[i];
1289c6fd2807SJeff Garzik 	if (csum)
1290c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_WARNING,
1291c6fd2807SJeff Garzik 			       "invalid checksum 0x%x on log page 10h\n", csum);
1292c6fd2807SJeff Garzik 
1293c6fd2807SJeff Garzik 	if (buf[0] & 0x80)
1294c6fd2807SJeff Garzik 		return -ENOENT;
1295c6fd2807SJeff Garzik 
1296c6fd2807SJeff Garzik 	*tag = buf[0] & 0x1f;
1297c6fd2807SJeff Garzik 
1298c6fd2807SJeff Garzik 	tf->command = buf[2];
1299c6fd2807SJeff Garzik 	tf->feature = buf[3];
1300c6fd2807SJeff Garzik 	tf->lbal = buf[4];
1301c6fd2807SJeff Garzik 	tf->lbam = buf[5];
1302c6fd2807SJeff Garzik 	tf->lbah = buf[6];
1303c6fd2807SJeff Garzik 	tf->device = buf[7];
1304c6fd2807SJeff Garzik 	tf->hob_lbal = buf[8];
1305c6fd2807SJeff Garzik 	tf->hob_lbam = buf[9];
1306c6fd2807SJeff Garzik 	tf->hob_lbah = buf[10];
1307c6fd2807SJeff Garzik 	tf->nsect = buf[12];
1308c6fd2807SJeff Garzik 	tf->hob_nsect = buf[13];
1309c6fd2807SJeff Garzik 
1310c6fd2807SJeff Garzik 	return 0;
1311c6fd2807SJeff Garzik }
1312c6fd2807SJeff Garzik 
1313c6fd2807SJeff Garzik /**
1314c6fd2807SJeff Garzik  *	atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1315c6fd2807SJeff Garzik  *	@dev: device to perform REQUEST_SENSE to
1316c6fd2807SJeff Garzik  *	@sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
1317c6fd2807SJeff Garzik  *
1318c6fd2807SJeff Garzik  *	Perform ATAPI REQUEST_SENSE after the device reported CHECK
1319c6fd2807SJeff Garzik  *	SENSE.  This function is EH helper.
1320c6fd2807SJeff Garzik  *
1321c6fd2807SJeff Garzik  *	LOCKING:
1322c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1323c6fd2807SJeff Garzik  *
1324c6fd2807SJeff Garzik  *	RETURNS:
1325c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask on failure
1326c6fd2807SJeff Garzik  */
132756287768SAlbert Lee static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc)
1328c6fd2807SJeff Garzik {
132956287768SAlbert Lee 	struct ata_device *dev = qc->dev;
133056287768SAlbert Lee 	unsigned char *sense_buf = qc->scsicmd->sense_buffer;
13319af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
1332c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1333c6fd2807SJeff Garzik 	u8 cdb[ATAPI_CDB_LEN];
1334c6fd2807SJeff Garzik 
1335c6fd2807SJeff Garzik 	DPRINTK("ATAPI request sense\n");
1336c6fd2807SJeff Garzik 
1337c6fd2807SJeff Garzik 	/* FIXME: is this needed? */
1338c6fd2807SJeff Garzik 	memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1339c6fd2807SJeff Garzik 
134056287768SAlbert Lee 	/* initialize sense_buf with the error register,
134156287768SAlbert Lee 	 * for the case where they are -not- overwritten
134256287768SAlbert Lee 	 */
1343c6fd2807SJeff Garzik 	sense_buf[0] = 0x70;
134456287768SAlbert Lee 	sense_buf[2] = qc->result_tf.feature >> 4;
134556287768SAlbert Lee 
134656287768SAlbert Lee 	/* some devices time out if garbage left in tf */
134756287768SAlbert Lee 	ata_tf_init(dev, &tf);
1348c6fd2807SJeff Garzik 
1349c6fd2807SJeff Garzik 	memset(cdb, 0, ATAPI_CDB_LEN);
1350c6fd2807SJeff Garzik 	cdb[0] = REQUEST_SENSE;
1351c6fd2807SJeff Garzik 	cdb[4] = SCSI_SENSE_BUFFERSIZE;
1352c6fd2807SJeff Garzik 
1353c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1354c6fd2807SJeff Garzik 	tf.command = ATA_CMD_PACKET;
1355c6fd2807SJeff Garzik 
1356c6fd2807SJeff Garzik 	/* is it pointless to prefer PIO for "safety reasons"? */
1357c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_PIO_DMA) {
1358c6fd2807SJeff Garzik 		tf.protocol = ATA_PROT_ATAPI_DMA;
1359c6fd2807SJeff Garzik 		tf.feature |= ATAPI_PKT_DMA;
1360c6fd2807SJeff Garzik 	} else {
1361c6fd2807SJeff Garzik 		tf.protocol = ATA_PROT_ATAPI;
1362c6fd2807SJeff Garzik 		tf.lbam = (8 * 1024) & 0xff;
1363c6fd2807SJeff Garzik 		tf.lbah = (8 * 1024) >> 8;
1364c6fd2807SJeff Garzik 	}
1365c6fd2807SJeff Garzik 
1366c6fd2807SJeff Garzik 	return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
13672b789108STejun Heo 				 sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
1368c6fd2807SJeff Garzik }
1369c6fd2807SJeff Garzik 
1370c6fd2807SJeff Garzik /**
1371c6fd2807SJeff Garzik  *	ata_eh_analyze_serror - analyze SError for a failed port
13720260731fSTejun Heo  *	@link: ATA link to analyze SError for
1373c6fd2807SJeff Garzik  *
1374c6fd2807SJeff Garzik  *	Analyze SError if available and further determine cause of
1375c6fd2807SJeff Garzik  *	failure.
1376c6fd2807SJeff Garzik  *
1377c6fd2807SJeff Garzik  *	LOCKING:
1378c6fd2807SJeff Garzik  *	None.
1379c6fd2807SJeff Garzik  */
13800260731fSTejun Heo static void ata_eh_analyze_serror(struct ata_link *link)
1381c6fd2807SJeff Garzik {
13820260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
1383c6fd2807SJeff Garzik 	u32 serror = ehc->i.serror;
1384c6fd2807SJeff Garzik 	unsigned int err_mask = 0, action = 0;
1385f9df58cbSTejun Heo 	u32 hotplug_mask;
1386c6fd2807SJeff Garzik 
1387c6fd2807SJeff Garzik 	if (serror & SERR_PERSISTENT) {
1388c6fd2807SJeff Garzik 		err_mask |= AC_ERR_ATA_BUS;
1389c6fd2807SJeff Garzik 		action |= ATA_EH_HARDRESET;
1390c6fd2807SJeff Garzik 	}
1391c6fd2807SJeff Garzik 	if (serror &
1392c6fd2807SJeff Garzik 	    (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
1393c6fd2807SJeff Garzik 		err_mask |= AC_ERR_ATA_BUS;
1394c6fd2807SJeff Garzik 		action |= ATA_EH_SOFTRESET;
1395c6fd2807SJeff Garzik 	}
1396c6fd2807SJeff Garzik 	if (serror & SERR_PROTOCOL) {
1397c6fd2807SJeff Garzik 		err_mask |= AC_ERR_HSM;
1398c6fd2807SJeff Garzik 		action |= ATA_EH_SOFTRESET;
1399c6fd2807SJeff Garzik 	}
1400c6fd2807SJeff Garzik 	if (serror & SERR_INTERNAL) {
1401c6fd2807SJeff Garzik 		err_mask |= AC_ERR_SYSTEM;
1402771b8dadSTejun Heo 		action |= ATA_EH_HARDRESET;
1403c6fd2807SJeff Garzik 	}
1404f9df58cbSTejun Heo 
1405f9df58cbSTejun Heo 	/* Determine whether a hotplug event has occurred.  Both
1406f9df58cbSTejun Heo 	 * SError.N/X are considered hotplug events for enabled or
1407f9df58cbSTejun Heo 	 * host links.  For disabled PMP links, only N bit is
1408f9df58cbSTejun Heo 	 * considered as X bit is left at 1 for link plugging.
1409f9df58cbSTejun Heo 	 */
1410f9df58cbSTejun Heo 	hotplug_mask = 0;
1411f9df58cbSTejun Heo 
1412f9df58cbSTejun Heo 	if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
1413f9df58cbSTejun Heo 		hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
1414f9df58cbSTejun Heo 	else
1415f9df58cbSTejun Heo 		hotplug_mask = SERR_PHYRDY_CHG;
1416f9df58cbSTejun Heo 
1417f9df58cbSTejun Heo 	if (serror & hotplug_mask)
1418c6fd2807SJeff Garzik 		ata_ehi_hotplugged(&ehc->i);
1419c6fd2807SJeff Garzik 
1420c6fd2807SJeff Garzik 	ehc->i.err_mask |= err_mask;
1421c6fd2807SJeff Garzik 	ehc->i.action |= action;
1422c6fd2807SJeff Garzik }
1423c6fd2807SJeff Garzik 
1424c6fd2807SJeff Garzik /**
1425c6fd2807SJeff Garzik  *	ata_eh_analyze_ncq_error - analyze NCQ error
14260260731fSTejun Heo  *	@link: ATA link to analyze NCQ error for
1427c6fd2807SJeff Garzik  *
1428c6fd2807SJeff Garzik  *	Read log page 10h, determine the offending qc and acquire
1429c6fd2807SJeff Garzik  *	error status TF.  For NCQ device errors, all LLDDs have to do
1430c6fd2807SJeff Garzik  *	is setting AC_ERR_DEV in ehi->err_mask.  This function takes
1431c6fd2807SJeff Garzik  *	care of the rest.
1432c6fd2807SJeff Garzik  *
1433c6fd2807SJeff Garzik  *	LOCKING:
1434c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1435c6fd2807SJeff Garzik  */
14360260731fSTejun Heo static void ata_eh_analyze_ncq_error(struct ata_link *link)
1437c6fd2807SJeff Garzik {
14380260731fSTejun Heo 	struct ata_port *ap = link->ap;
14390260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
14400260731fSTejun Heo 	struct ata_device *dev = link->device;
1441c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
1442c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1443c6fd2807SJeff Garzik 	int tag, rc;
1444c6fd2807SJeff Garzik 
1445c6fd2807SJeff Garzik 	/* if frozen, we can't do much */
1446c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN)
1447c6fd2807SJeff Garzik 		return;
1448c6fd2807SJeff Garzik 
1449c6fd2807SJeff Garzik 	/* is it NCQ device error? */
14500260731fSTejun Heo 	if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1451c6fd2807SJeff Garzik 		return;
1452c6fd2807SJeff Garzik 
1453c6fd2807SJeff Garzik 	/* has LLDD analyzed already? */
1454c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1455c6fd2807SJeff Garzik 		qc = __ata_qc_from_tag(ap, tag);
1456c6fd2807SJeff Garzik 
1457c6fd2807SJeff Garzik 		if (!(qc->flags & ATA_QCFLAG_FAILED))
1458c6fd2807SJeff Garzik 			continue;
1459c6fd2807SJeff Garzik 
1460c6fd2807SJeff Garzik 		if (qc->err_mask)
1461c6fd2807SJeff Garzik 			return;
1462c6fd2807SJeff Garzik 	}
1463c6fd2807SJeff Garzik 
1464c6fd2807SJeff Garzik 	/* okay, this error is ours */
1465c6fd2807SJeff Garzik 	rc = ata_eh_read_log_10h(dev, &tag, &tf);
1466c6fd2807SJeff Garzik 	if (rc) {
14670260731fSTejun Heo 		ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
1468c6fd2807SJeff Garzik 				"(errno=%d)\n", rc);
1469c6fd2807SJeff Garzik 		return;
1470c6fd2807SJeff Garzik 	}
1471c6fd2807SJeff Garzik 
14720260731fSTejun Heo 	if (!(link->sactive & (1 << tag))) {
14730260731fSTejun Heo 		ata_link_printk(link, KERN_ERR, "log page 10h reported "
1474c6fd2807SJeff Garzik 				"inactive tag %d\n", tag);
1475c6fd2807SJeff Garzik 		return;
1476c6fd2807SJeff Garzik 	}
1477c6fd2807SJeff Garzik 
1478c6fd2807SJeff Garzik 	/* we've got the perpetrator, condemn it */
1479c6fd2807SJeff Garzik 	qc = __ata_qc_from_tag(ap, tag);
1480c6fd2807SJeff Garzik 	memcpy(&qc->result_tf, &tf, sizeof(tf));
14815335b729STejun Heo 	qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
1482c6fd2807SJeff Garzik 	ehc->i.err_mask &= ~AC_ERR_DEV;
1483c6fd2807SJeff Garzik }
1484c6fd2807SJeff Garzik 
1485c6fd2807SJeff Garzik /**
1486c6fd2807SJeff Garzik  *	ata_eh_analyze_tf - analyze taskfile of a failed qc
1487c6fd2807SJeff Garzik  *	@qc: qc to analyze
1488c6fd2807SJeff Garzik  *	@tf: Taskfile registers to analyze
1489c6fd2807SJeff Garzik  *
1490c6fd2807SJeff Garzik  *	Analyze taskfile of @qc and further determine cause of
1491c6fd2807SJeff Garzik  *	failure.  This function also requests ATAPI sense data if
1492c6fd2807SJeff Garzik  *	avaliable.
1493c6fd2807SJeff Garzik  *
1494c6fd2807SJeff Garzik  *	LOCKING:
1495c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1496c6fd2807SJeff Garzik  *
1497c6fd2807SJeff Garzik  *	RETURNS:
1498c6fd2807SJeff Garzik  *	Determined recovery action
1499c6fd2807SJeff Garzik  */
1500c6fd2807SJeff Garzik static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1501c6fd2807SJeff Garzik 				      const struct ata_taskfile *tf)
1502c6fd2807SJeff Garzik {
1503c6fd2807SJeff Garzik 	unsigned int tmp, action = 0;
1504c6fd2807SJeff Garzik 	u8 stat = tf->command, err = tf->feature;
1505c6fd2807SJeff Garzik 
1506c6fd2807SJeff Garzik 	if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1507c6fd2807SJeff Garzik 		qc->err_mask |= AC_ERR_HSM;
1508c6fd2807SJeff Garzik 		return ATA_EH_SOFTRESET;
1509c6fd2807SJeff Garzik 	}
1510c6fd2807SJeff Garzik 
1511a51d644aSTejun Heo 	if (stat & (ATA_ERR | ATA_DF))
1512a51d644aSTejun Heo 		qc->err_mask |= AC_ERR_DEV;
1513a51d644aSTejun Heo 	else
1514c6fd2807SJeff Garzik 		return 0;
1515c6fd2807SJeff Garzik 
1516c6fd2807SJeff Garzik 	switch (qc->dev->class) {
1517c6fd2807SJeff Garzik 	case ATA_DEV_ATA:
1518c6fd2807SJeff Garzik 		if (err & ATA_ICRC)
1519c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_ATA_BUS;
1520c6fd2807SJeff Garzik 		if (err & ATA_UNC)
1521c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_MEDIA;
1522c6fd2807SJeff Garzik 		if (err & ATA_IDNF)
1523c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_INVALID;
1524c6fd2807SJeff Garzik 		break;
1525c6fd2807SJeff Garzik 
1526c6fd2807SJeff Garzik 	case ATA_DEV_ATAPI:
1527a569a30dSTejun Heo 		if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
152856287768SAlbert Lee 			tmp = atapi_eh_request_sense(qc);
1529c6fd2807SJeff Garzik 			if (!tmp) {
1530a569a30dSTejun Heo 				/* ATA_QCFLAG_SENSE_VALID is used to
1531a569a30dSTejun Heo 				 * tell atapi_qc_complete() that sense
1532a569a30dSTejun Heo 				 * data is already valid.
1533c6fd2807SJeff Garzik 				 *
1534c6fd2807SJeff Garzik 				 * TODO: interpret sense data and set
1535c6fd2807SJeff Garzik 				 * appropriate err_mask.
1536c6fd2807SJeff Garzik 				 */
1537c6fd2807SJeff Garzik 				qc->flags |= ATA_QCFLAG_SENSE_VALID;
1538c6fd2807SJeff Garzik 			} else
1539c6fd2807SJeff Garzik 				qc->err_mask |= tmp;
1540c6fd2807SJeff Garzik 		}
1541a569a30dSTejun Heo 	}
1542c6fd2807SJeff Garzik 
1543c6fd2807SJeff Garzik 	if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1544c6fd2807SJeff Garzik 		action |= ATA_EH_SOFTRESET;
1545c6fd2807SJeff Garzik 
1546c6fd2807SJeff Garzik 	return action;
1547c6fd2807SJeff Garzik }
1548c6fd2807SJeff Garzik 
15497d47e8d4STejun Heo static int ata_eh_categorize_error(int is_io, unsigned int err_mask)
1550c6fd2807SJeff Garzik {
15517d47e8d4STejun Heo 	if (err_mask & AC_ERR_ATA_BUS)
1552c6fd2807SJeff Garzik 		return 1;
1553c6fd2807SJeff Garzik 
15547d47e8d4STejun Heo 	if (err_mask & AC_ERR_TIMEOUT)
1555c6fd2807SJeff Garzik 		return 2;
15567d47e8d4STejun Heo 
15577d47e8d4STejun Heo 	if (is_io) {
15587d47e8d4STejun Heo 		if (err_mask & AC_ERR_HSM)
15597d47e8d4STejun Heo 			return 2;
15607d47e8d4STejun Heo 		if ((err_mask &
15617d47e8d4STejun Heo 		     (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
15627d47e8d4STejun Heo 			return 3;
1563c6fd2807SJeff Garzik 	}
1564c6fd2807SJeff Garzik 
1565c6fd2807SJeff Garzik 	return 0;
1566c6fd2807SJeff Garzik }
1567c6fd2807SJeff Garzik 
15687d47e8d4STejun Heo struct speed_down_verdict_arg {
1569c6fd2807SJeff Garzik 	u64 since;
15707d47e8d4STejun Heo 	int nr_errors[4];
1571c6fd2807SJeff Garzik };
1572c6fd2807SJeff Garzik 
15737d47e8d4STejun Heo static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
1574c6fd2807SJeff Garzik {
15757d47e8d4STejun Heo 	struct speed_down_verdict_arg *arg = void_arg;
15767d47e8d4STejun Heo 	int cat = ata_eh_categorize_error(ent->is_io, ent->err_mask);
1577c6fd2807SJeff Garzik 
1578c6fd2807SJeff Garzik 	if (ent->timestamp < arg->since)
1579c6fd2807SJeff Garzik 		return -1;
1580c6fd2807SJeff Garzik 
15817d47e8d4STejun Heo 	arg->nr_errors[cat]++;
1582c6fd2807SJeff Garzik 	return 0;
1583c6fd2807SJeff Garzik }
1584c6fd2807SJeff Garzik 
1585c6fd2807SJeff Garzik /**
15867d47e8d4STejun Heo  *	ata_eh_speed_down_verdict - Determine speed down verdict
1587c6fd2807SJeff Garzik  *	@dev: Device of interest
1588c6fd2807SJeff Garzik  *
1589c6fd2807SJeff Garzik  *	This function examines error ring of @dev and determines
15907d47e8d4STejun Heo  *	whether NCQ needs to be turned off, transfer speed should be
15917d47e8d4STejun Heo  *	stepped down, or falling back to PIO is necessary.
1592c6fd2807SJeff Garzik  *
15937d47e8d4STejun Heo  *	Cat-1 is ATA_BUS error for any command.
1594c6fd2807SJeff Garzik  *
15957d47e8d4STejun Heo  *	Cat-2 is TIMEOUT for any command or HSM violation for known
15967d47e8d4STejun Heo  *	supported commands.
15977d47e8d4STejun Heo  *
15987d47e8d4STejun Heo  *	Cat-3 is is unclassified DEV error for known supported
1599c6fd2807SJeff Garzik  *	command.
1600c6fd2807SJeff Garzik  *
16017d47e8d4STejun Heo  *	NCQ needs to be turned off if there have been more than 3
16027d47e8d4STejun Heo  *	Cat-2 + Cat-3 errors during last 10 minutes.
16037d47e8d4STejun Heo  *
16047d47e8d4STejun Heo  *	Speed down is necessary if there have been more than 3 Cat-1 +
16057d47e8d4STejun Heo  *	Cat-2 errors or 10 Cat-3 errors during last 10 minutes.
16067d47e8d4STejun Heo  *
16077d47e8d4STejun Heo  *	Falling back to PIO mode is necessary if there have been more
16087d47e8d4STejun Heo  *	than 10 Cat-1 + Cat-2 + Cat-3 errors during last 5 minutes.
16097d47e8d4STejun Heo  *
1610c6fd2807SJeff Garzik  *	LOCKING:
1611c6fd2807SJeff Garzik  *	Inherited from caller.
1612c6fd2807SJeff Garzik  *
1613c6fd2807SJeff Garzik  *	RETURNS:
16147d47e8d4STejun Heo  *	OR of ATA_EH_SPDN_* flags.
1615c6fd2807SJeff Garzik  */
16167d47e8d4STejun Heo static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
1617c6fd2807SJeff Garzik {
16187d47e8d4STejun Heo 	const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
16197d47e8d4STejun Heo 	u64 j64 = get_jiffies_64();
16207d47e8d4STejun Heo 	struct speed_down_verdict_arg arg;
16217d47e8d4STejun Heo 	unsigned int verdict = 0;
1622c6fd2807SJeff Garzik 
16237d47e8d4STejun Heo 	/* scan past 10 mins of error history */
1624c6fd2807SJeff Garzik 	memset(&arg, 0, sizeof(arg));
16257d47e8d4STejun Heo 	arg.since = j64 - min(j64, j10mins);
16267d47e8d4STejun Heo 	ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1627c6fd2807SJeff Garzik 
16287d47e8d4STejun Heo 	if (arg.nr_errors[2] + arg.nr_errors[3] > 3)
16297d47e8d4STejun Heo 		verdict |= ATA_EH_SPDN_NCQ_OFF;
16307d47e8d4STejun Heo 	if (arg.nr_errors[1] + arg.nr_errors[2] > 3 || arg.nr_errors[3] > 10)
16317d47e8d4STejun Heo 		verdict |= ATA_EH_SPDN_SPEED_DOWN;
1632c6fd2807SJeff Garzik 
16337d47e8d4STejun Heo 	/* scan past 3 mins of error history */
16347d47e8d4STejun Heo 	memset(&arg, 0, sizeof(arg));
16357d47e8d4STejun Heo 	arg.since = j64 - min(j64, j5mins);
16367d47e8d4STejun Heo 	ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1637c6fd2807SJeff Garzik 
16387d47e8d4STejun Heo 	if (arg.nr_errors[1] + arg.nr_errors[2] + arg.nr_errors[3] > 10)
16397d47e8d4STejun Heo 		verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
16407d47e8d4STejun Heo 
16417d47e8d4STejun Heo 	return verdict;
1642c6fd2807SJeff Garzik }
1643c6fd2807SJeff Garzik 
1644c6fd2807SJeff Garzik /**
1645c6fd2807SJeff Garzik  *	ata_eh_speed_down - record error and speed down if necessary
1646c6fd2807SJeff Garzik  *	@dev: Failed device
1647c6fd2807SJeff Garzik  *	@is_io: Did the device fail during normal IO?
1648c6fd2807SJeff Garzik  *	@err_mask: err_mask of the error
1649c6fd2807SJeff Garzik  *
1650c6fd2807SJeff Garzik  *	Record error and examine error history to determine whether
1651c6fd2807SJeff Garzik  *	adjusting transmission speed is necessary.  It also sets
1652c6fd2807SJeff Garzik  *	transmission limits appropriately if such adjustment is
1653c6fd2807SJeff Garzik  *	necessary.
1654c6fd2807SJeff Garzik  *
1655c6fd2807SJeff Garzik  *	LOCKING:
1656c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1657c6fd2807SJeff Garzik  *
1658c6fd2807SJeff Garzik  *	RETURNS:
16597d47e8d4STejun Heo  *	Determined recovery action.
1660c6fd2807SJeff Garzik  */
16617d47e8d4STejun Heo static unsigned int ata_eh_speed_down(struct ata_device *dev, int is_io,
1662c6fd2807SJeff Garzik 				      unsigned int err_mask)
1663c6fd2807SJeff Garzik {
16647d47e8d4STejun Heo 	unsigned int verdict;
16657d47e8d4STejun Heo 	unsigned int action = 0;
16667d47e8d4STejun Heo 
16677d47e8d4STejun Heo 	/* don't bother if Cat-0 error */
16687d47e8d4STejun Heo 	if (ata_eh_categorize_error(is_io, err_mask) == 0)
1669c6fd2807SJeff Garzik 		return 0;
1670c6fd2807SJeff Garzik 
1671c6fd2807SJeff Garzik 	/* record error and determine whether speed down is necessary */
1672c6fd2807SJeff Garzik 	ata_ering_record(&dev->ering, is_io, err_mask);
16737d47e8d4STejun Heo 	verdict = ata_eh_speed_down_verdict(dev);
1674c6fd2807SJeff Garzik 
16757d47e8d4STejun Heo 	/* turn off NCQ? */
16767d47e8d4STejun Heo 	if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
16777d47e8d4STejun Heo 	    (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
16787d47e8d4STejun Heo 			   ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
16797d47e8d4STejun Heo 		dev->flags |= ATA_DFLAG_NCQ_OFF;
16807d47e8d4STejun Heo 		ata_dev_printk(dev, KERN_WARNING,
16817d47e8d4STejun Heo 			       "NCQ disabled due to excessive errors\n");
16827d47e8d4STejun Heo 		goto done;
16837d47e8d4STejun Heo 	}
1684c6fd2807SJeff Garzik 
16857d47e8d4STejun Heo 	/* speed down? */
16867d47e8d4STejun Heo 	if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1687c6fd2807SJeff Garzik 		/* speed down SATA link speed if possible */
1688936fd732STejun Heo 		if (sata_down_spd_limit(dev->link) == 0) {
16897d47e8d4STejun Heo 			action |= ATA_EH_HARDRESET;
16907d47e8d4STejun Heo 			goto done;
16917d47e8d4STejun Heo 		}
1692c6fd2807SJeff Garzik 
1693c6fd2807SJeff Garzik 		/* lower transfer mode */
16947d47e8d4STejun Heo 		if (dev->spdn_cnt < 2) {
16957d47e8d4STejun Heo 			static const int dma_dnxfer_sel[] =
16967d47e8d4STejun Heo 				{ ATA_DNXFER_DMA, ATA_DNXFER_40C };
16977d47e8d4STejun Heo 			static const int pio_dnxfer_sel[] =
16987d47e8d4STejun Heo 				{ ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
16997d47e8d4STejun Heo 			int sel;
1700c6fd2807SJeff Garzik 
17017d47e8d4STejun Heo 			if (dev->xfer_shift != ATA_SHIFT_PIO)
17027d47e8d4STejun Heo 				sel = dma_dnxfer_sel[dev->spdn_cnt];
17037d47e8d4STejun Heo 			else
17047d47e8d4STejun Heo 				sel = pio_dnxfer_sel[dev->spdn_cnt];
17057d47e8d4STejun Heo 
17067d47e8d4STejun Heo 			dev->spdn_cnt++;
17077d47e8d4STejun Heo 
17087d47e8d4STejun Heo 			if (ata_down_xfermask_limit(dev, sel) == 0) {
17097d47e8d4STejun Heo 				action |= ATA_EH_SOFTRESET;
17107d47e8d4STejun Heo 				goto done;
17117d47e8d4STejun Heo 			}
17127d47e8d4STejun Heo 		}
17137d47e8d4STejun Heo 	}
17147d47e8d4STejun Heo 
17157d47e8d4STejun Heo 	/* Fall back to PIO?  Slowing down to PIO is meaningless for
17167d47e8d4STejun Heo 	 * SATA.  Consider it only for PATA.
17177d47e8d4STejun Heo 	 */
17187d47e8d4STejun Heo 	if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
17199af5c9c9STejun Heo 	    (dev->link->ap->cbl != ATA_CBL_SATA) &&
17207d47e8d4STejun Heo 	    (dev->xfer_shift != ATA_SHIFT_PIO)) {
17217d47e8d4STejun Heo 		if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
17227d47e8d4STejun Heo 			dev->spdn_cnt = 0;
17237d47e8d4STejun Heo 			action |= ATA_EH_SOFTRESET;
17247d47e8d4STejun Heo 			goto done;
17257d47e8d4STejun Heo 		}
17267d47e8d4STejun Heo 	}
17277d47e8d4STejun Heo 
1728c6fd2807SJeff Garzik 	return 0;
17297d47e8d4STejun Heo  done:
17307d47e8d4STejun Heo 	/* device has been slowed down, blow error history */
17317d47e8d4STejun Heo 	ata_ering_clear(&dev->ering);
17327d47e8d4STejun Heo 	return action;
1733c6fd2807SJeff Garzik }
1734c6fd2807SJeff Garzik 
1735c6fd2807SJeff Garzik /**
17369b1e2658STejun Heo  *	ata_eh_link_autopsy - analyze error and determine recovery action
17379b1e2658STejun Heo  *	@link: host link to perform autopsy on
1738c6fd2807SJeff Garzik  *
17390260731fSTejun Heo  *	Analyze why @link failed and determine which recovery actions
17400260731fSTejun Heo  *	are needed.  This function also sets more detailed AC_ERR_*
17410260731fSTejun Heo  *	values and fills sense data for ATAPI CHECK SENSE.
1742c6fd2807SJeff Garzik  *
1743c6fd2807SJeff Garzik  *	LOCKING:
1744c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1745c6fd2807SJeff Garzik  */
17469b1e2658STejun Heo static void ata_eh_link_autopsy(struct ata_link *link)
1747c6fd2807SJeff Garzik {
17480260731fSTejun Heo 	struct ata_port *ap = link->ap;
1749936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
1750c6fd2807SJeff Garzik 	unsigned int all_err_mask = 0;
1751c6fd2807SJeff Garzik 	int tag, is_io = 0;
1752c6fd2807SJeff Garzik 	u32 serror;
1753c6fd2807SJeff Garzik 	int rc;
1754c6fd2807SJeff Garzik 
1755c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
1756c6fd2807SJeff Garzik 
1757c6fd2807SJeff Garzik 	if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1758c6fd2807SJeff Garzik 		return;
1759c6fd2807SJeff Garzik 
1760c6fd2807SJeff Garzik 	/* obtain and analyze SError */
1761936fd732STejun Heo 	rc = sata_scr_read(link, SCR_ERROR, &serror);
1762c6fd2807SJeff Garzik 	if (rc == 0) {
1763c6fd2807SJeff Garzik 		ehc->i.serror |= serror;
17640260731fSTejun Heo 		ata_eh_analyze_serror(link);
17654e57c517STejun Heo 	} else if (rc != -EOPNOTSUPP) {
17664e57c517STejun Heo 		/* SError read failed, force hardreset and probing */
17674e57c517STejun Heo 		ata_ehi_schedule_probe(&ehc->i);
1768c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_HARDRESET;
17694e57c517STejun Heo 		ehc->i.err_mask |= AC_ERR_OTHER;
17704e57c517STejun Heo 	}
1771c6fd2807SJeff Garzik 
1772c6fd2807SJeff Garzik 	/* analyze NCQ failure */
17730260731fSTejun Heo 	ata_eh_analyze_ncq_error(link);
1774c6fd2807SJeff Garzik 
1775c6fd2807SJeff Garzik 	/* any real error trumps AC_ERR_OTHER */
1776c6fd2807SJeff Garzik 	if (ehc->i.err_mask & ~AC_ERR_OTHER)
1777c6fd2807SJeff Garzik 		ehc->i.err_mask &= ~AC_ERR_OTHER;
1778c6fd2807SJeff Garzik 
1779c6fd2807SJeff Garzik 	all_err_mask |= ehc->i.err_mask;
1780c6fd2807SJeff Garzik 
1781c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1782c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1783c6fd2807SJeff Garzik 
17840260731fSTejun Heo 		if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
1785c6fd2807SJeff Garzik 			continue;
1786c6fd2807SJeff Garzik 
1787c6fd2807SJeff Garzik 		/* inherit upper level err_mask */
1788c6fd2807SJeff Garzik 		qc->err_mask |= ehc->i.err_mask;
1789c6fd2807SJeff Garzik 
1790c6fd2807SJeff Garzik 		/* analyze TF */
1791c6fd2807SJeff Garzik 		ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1792c6fd2807SJeff Garzik 
1793c6fd2807SJeff Garzik 		/* DEV errors are probably spurious in case of ATA_BUS error */
1794c6fd2807SJeff Garzik 		if (qc->err_mask & AC_ERR_ATA_BUS)
1795c6fd2807SJeff Garzik 			qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1796c6fd2807SJeff Garzik 					  AC_ERR_INVALID);
1797c6fd2807SJeff Garzik 
1798c6fd2807SJeff Garzik 		/* any real error trumps unknown error */
1799c6fd2807SJeff Garzik 		if (qc->err_mask & ~AC_ERR_OTHER)
1800c6fd2807SJeff Garzik 			qc->err_mask &= ~AC_ERR_OTHER;
1801c6fd2807SJeff Garzik 
1802c6fd2807SJeff Garzik 		/* SENSE_VALID trumps dev/unknown error and revalidation */
1803c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1804c6fd2807SJeff Garzik 			qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1805c6fd2807SJeff Garzik 			ehc->i.action &= ~ATA_EH_REVALIDATE;
1806c6fd2807SJeff Garzik 		}
1807c6fd2807SJeff Garzik 
1808c6fd2807SJeff Garzik 		/* accumulate error info */
1809c6fd2807SJeff Garzik 		ehc->i.dev = qc->dev;
1810c6fd2807SJeff Garzik 		all_err_mask |= qc->err_mask;
1811c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_IO)
1812c6fd2807SJeff Garzik 			is_io = 1;
1813c6fd2807SJeff Garzik 	}
1814c6fd2807SJeff Garzik 
1815c6fd2807SJeff Garzik 	/* enforce default EH actions */
1816c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN ||
1817c6fd2807SJeff Garzik 	    all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
1818c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_SOFTRESET;
1819c6fd2807SJeff Garzik 	else if (all_err_mask)
1820c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_REVALIDATE;
1821c6fd2807SJeff Garzik 
1822c6fd2807SJeff Garzik 	/* if we have offending qcs and the associated failed device */
1823c6fd2807SJeff Garzik 	if (ehc->i.dev) {
1824c6fd2807SJeff Garzik 		/* speed down */
1825c6fd2807SJeff Garzik 		ehc->i.action |= ata_eh_speed_down(ehc->i.dev, is_io,
1826c6fd2807SJeff Garzik 						   all_err_mask);
1827c6fd2807SJeff Garzik 
1828c6fd2807SJeff Garzik 		/* perform per-dev EH action only on the offending device */
1829c6fd2807SJeff Garzik 		ehc->i.dev_action[ehc->i.dev->devno] |=
1830c6fd2807SJeff Garzik 			ehc->i.action & ATA_EH_PERDEV_MASK;
1831c6fd2807SJeff Garzik 		ehc->i.action &= ~ATA_EH_PERDEV_MASK;
1832c6fd2807SJeff Garzik 	}
1833c6fd2807SJeff Garzik 
1834c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
1835c6fd2807SJeff Garzik }
1836c6fd2807SJeff Garzik 
1837c6fd2807SJeff Garzik /**
18389b1e2658STejun Heo  *	ata_eh_autopsy - analyze error and determine recovery action
18399b1e2658STejun Heo  *	@ap: host port to perform autopsy on
18409b1e2658STejun Heo  *
18419b1e2658STejun Heo  *	Analyze all links of @ap and determine why they failed and
18429b1e2658STejun Heo  *	which recovery actions are needed.
18439b1e2658STejun Heo  *
18449b1e2658STejun Heo  *	LOCKING:
18459b1e2658STejun Heo  *	Kernel thread context (may sleep).
18469b1e2658STejun Heo  */
1847fb7fd614STejun Heo void ata_eh_autopsy(struct ata_port *ap)
18489b1e2658STejun Heo {
18499b1e2658STejun Heo 	struct ata_link *link;
18509b1e2658STejun Heo 
18519b1e2658STejun Heo 	__ata_port_for_each_link(link, ap)
18529b1e2658STejun Heo 		ata_eh_link_autopsy(link);
18539b1e2658STejun Heo }
18549b1e2658STejun Heo 
18559b1e2658STejun Heo /**
18569b1e2658STejun Heo  *	ata_eh_link_report - report error handling to user
18570260731fSTejun Heo  *	@link: ATA link EH is going on
1858c6fd2807SJeff Garzik  *
1859c6fd2807SJeff Garzik  *	Report EH to user.
1860c6fd2807SJeff Garzik  *
1861c6fd2807SJeff Garzik  *	LOCKING:
1862c6fd2807SJeff Garzik  *	None.
1863c6fd2807SJeff Garzik  */
18649b1e2658STejun Heo static void ata_eh_link_report(struct ata_link *link)
1865c6fd2807SJeff Garzik {
18660260731fSTejun Heo 	struct ata_port *ap = link->ap;
18670260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
1868c6fd2807SJeff Garzik 	const char *frozen, *desc;
1869a1e10f7eSTejun Heo 	char tries_buf[6];
1870c6fd2807SJeff Garzik 	int tag, nr_failed = 0;
1871c6fd2807SJeff Garzik 
187294ff3d54STejun Heo 	if (ehc->i.flags & ATA_EHI_QUIET)
187394ff3d54STejun Heo 		return;
187494ff3d54STejun Heo 
1875c6fd2807SJeff Garzik 	desc = NULL;
1876c6fd2807SJeff Garzik 	if (ehc->i.desc[0] != '\0')
1877c6fd2807SJeff Garzik 		desc = ehc->i.desc;
1878c6fd2807SJeff Garzik 
1879c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1880c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1881c6fd2807SJeff Garzik 
18820260731fSTejun Heo 		if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
1883c6fd2807SJeff Garzik 			continue;
1884c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1885c6fd2807SJeff Garzik 			continue;
1886c6fd2807SJeff Garzik 
1887c6fd2807SJeff Garzik 		nr_failed++;
1888c6fd2807SJeff Garzik 	}
1889c6fd2807SJeff Garzik 
1890c6fd2807SJeff Garzik 	if (!nr_failed && !ehc->i.err_mask)
1891c6fd2807SJeff Garzik 		return;
1892c6fd2807SJeff Garzik 
1893c6fd2807SJeff Garzik 	frozen = "";
1894c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN)
1895c6fd2807SJeff Garzik 		frozen = " frozen";
1896c6fd2807SJeff Garzik 
1897a1e10f7eSTejun Heo 	memset(tries_buf, 0, sizeof(tries_buf));
1898a1e10f7eSTejun Heo 	if (ap->eh_tries < ATA_EH_MAX_TRIES)
1899a1e10f7eSTejun Heo 		snprintf(tries_buf, sizeof(tries_buf) - 1, " t%d",
1900a1e10f7eSTejun Heo 			 ap->eh_tries);
1901a1e10f7eSTejun Heo 
1902c6fd2807SJeff Garzik 	if (ehc->i.dev) {
1903c6fd2807SJeff Garzik 		ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1904a1e10f7eSTejun Heo 			       "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1905a1e10f7eSTejun Heo 			       ehc->i.err_mask, link->sactive, ehc->i.serror,
1906a1e10f7eSTejun Heo 			       ehc->i.action, frozen, tries_buf);
1907c6fd2807SJeff Garzik 		if (desc)
1908b64bbc39STejun Heo 			ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
1909c6fd2807SJeff Garzik 	} else {
19100260731fSTejun Heo 		ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
1911a1e10f7eSTejun Heo 				"SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1912a1e10f7eSTejun Heo 				ehc->i.err_mask, link->sactive, ehc->i.serror,
1913a1e10f7eSTejun Heo 				ehc->i.action, frozen, tries_buf);
1914c6fd2807SJeff Garzik 		if (desc)
19150260731fSTejun Heo 			ata_link_printk(link, KERN_ERR, "%s\n", desc);
1916c6fd2807SJeff Garzik 	}
1917c6fd2807SJeff Garzik 
19181333e194SRobert Hancock 	if (ehc->i.serror)
19191333e194SRobert Hancock 		ata_port_printk(ap, KERN_ERR,
19201333e194SRobert Hancock 		  "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
19211333e194SRobert Hancock 		  ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
19221333e194SRobert Hancock 		  ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
19231333e194SRobert Hancock 		  ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
19241333e194SRobert Hancock 		  ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
19251333e194SRobert Hancock 		  ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
19261333e194SRobert Hancock 		  ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
19271333e194SRobert Hancock 		  ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
19281333e194SRobert Hancock 		  ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
19291333e194SRobert Hancock 		  ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
19301333e194SRobert Hancock 		  ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
19311333e194SRobert Hancock 		  ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
19321333e194SRobert Hancock 		  ehc->i.serror & SERR_CRC ? "BadCRC " : "",
19331333e194SRobert Hancock 		  ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
19341333e194SRobert Hancock 		  ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
19351333e194SRobert Hancock 		  ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
19361333e194SRobert Hancock 		  ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
19371333e194SRobert Hancock 		  ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "" );
19381333e194SRobert Hancock 
1939c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
19408a937581STejun Heo 		static const char *dma_str[] = {
19418a937581STejun Heo 			[DMA_BIDIRECTIONAL]	= "bidi",
19428a937581STejun Heo 			[DMA_TO_DEVICE]		= "out",
19438a937581STejun Heo 			[DMA_FROM_DEVICE]	= "in",
19448a937581STejun Heo 			[DMA_NONE]		= "",
19458a937581STejun Heo 		};
1946c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
19478a937581STejun Heo 		struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
1948c6fd2807SJeff Garzik 
19490260731fSTejun Heo 		if (!(qc->flags & ATA_QCFLAG_FAILED) ||
19500260731fSTejun Heo 		    qc->dev->link != link || !qc->err_mask)
1951c6fd2807SJeff Garzik 			continue;
1952c6fd2807SJeff Garzik 
19538a937581STejun Heo 		ata_dev_printk(qc->dev, KERN_ERR,
19548a937581STejun Heo 			"cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
1955664e8503STejun Heo 			"tag %d cdb 0x%x data %u %s\n         "
19568a937581STejun Heo 			"res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
19575335b729STejun Heo 			"Emask 0x%x (%s)%s\n",
19588a937581STejun Heo 			cmd->command, cmd->feature, cmd->nsect,
19598a937581STejun Heo 			cmd->lbal, cmd->lbam, cmd->lbah,
19608a937581STejun Heo 			cmd->hob_feature, cmd->hob_nsect,
19618a937581STejun Heo 			cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
1962726f0785STejun Heo 			cmd->device, qc->tag, qc->cdb[0], qc->nbytes,
1963664e8503STejun Heo 			dma_str[qc->dma_dir],
19648a937581STejun Heo 			res->command, res->feature, res->nsect,
19658a937581STejun Heo 			res->lbal, res->lbam, res->lbah,
19668a937581STejun Heo 			res->hob_feature, res->hob_nsect,
19678a937581STejun Heo 			res->hob_lbal, res->hob_lbam, res->hob_lbah,
19685335b729STejun Heo 			res->device, qc->err_mask, ata_err_string(qc->err_mask),
19695335b729STejun Heo 			qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
19701333e194SRobert Hancock 
19711333e194SRobert Hancock 		if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
19721333e194SRobert Hancock 				    ATA_ERR) ) {
19731333e194SRobert Hancock 			if (res->command & ATA_BUSY)
19741333e194SRobert Hancock 				ata_dev_printk(qc->dev, KERN_ERR,
19751333e194SRobert Hancock 				  "status: { Busy }\n" );
19761333e194SRobert Hancock 			else
19771333e194SRobert Hancock 				ata_dev_printk(qc->dev, KERN_ERR,
19781333e194SRobert Hancock 				  "status: { %s%s%s%s}\n",
19791333e194SRobert Hancock 				  res->command & ATA_DRDY ? "DRDY " : "",
19801333e194SRobert Hancock 				  res->command & ATA_DF ? "DF " : "",
19811333e194SRobert Hancock 				  res->command & ATA_DRQ ? "DRQ " : "",
19821333e194SRobert Hancock 				  res->command & ATA_ERR ? "ERR " : "" );
19831333e194SRobert Hancock 		}
19841333e194SRobert Hancock 
19851333e194SRobert Hancock 		if (cmd->command != ATA_CMD_PACKET &&
19861333e194SRobert Hancock 		    (res->feature & (ATA_ICRC | ATA_UNC | ATA_IDNF |
19871333e194SRobert Hancock 				     ATA_ABORTED)))
19881333e194SRobert Hancock 			ata_dev_printk(qc->dev, KERN_ERR,
19891333e194SRobert Hancock 			  "error: { %s%s%s%s}\n",
19901333e194SRobert Hancock 			  res->feature & ATA_ICRC ? "ICRC " : "",
19911333e194SRobert Hancock 			  res->feature & ATA_UNC ? "UNC " : "",
19921333e194SRobert Hancock 			  res->feature & ATA_IDNF ? "IDNF " : "",
19931333e194SRobert Hancock 			  res->feature & ATA_ABORTED ? "ABRT " : "" );
1994c6fd2807SJeff Garzik 	}
1995c6fd2807SJeff Garzik }
1996c6fd2807SJeff Garzik 
19979b1e2658STejun Heo /**
19989b1e2658STejun Heo  *	ata_eh_report - report error handling to user
19999b1e2658STejun Heo  *	@ap: ATA port to report EH about
20009b1e2658STejun Heo  *
20019b1e2658STejun Heo  *	Report EH to user.
20029b1e2658STejun Heo  *
20039b1e2658STejun Heo  *	LOCKING:
20049b1e2658STejun Heo  *	None.
20059b1e2658STejun Heo  */
2006fb7fd614STejun Heo void ata_eh_report(struct ata_port *ap)
20079b1e2658STejun Heo {
20089b1e2658STejun Heo 	struct ata_link *link;
20099b1e2658STejun Heo 
20109b1e2658STejun Heo 	__ata_port_for_each_link(link, ap)
20119b1e2658STejun Heo 		ata_eh_link_report(link);
20129b1e2658STejun Heo }
20139b1e2658STejun Heo 
2014cc0680a5STejun Heo static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
2015d4b2bab4STejun Heo 			unsigned int *classes, unsigned long deadline)
2016c6fd2807SJeff Garzik {
2017f58229f8STejun Heo 	struct ata_device *dev;
2018f58229f8STejun Heo 	int rc;
2019c6fd2807SJeff Garzik 
2020cc0680a5STejun Heo 	ata_link_for_each_dev(dev, link)
2021f58229f8STejun Heo 		classes[dev->devno] = ATA_DEV_UNKNOWN;
2022c6fd2807SJeff Garzik 
2023cc0680a5STejun Heo 	rc = reset(link, classes, deadline);
2024c6fd2807SJeff Garzik 	if (rc)
2025c6fd2807SJeff Garzik 		return rc;
2026c6fd2807SJeff Garzik 
2027c6fd2807SJeff Garzik 	/* If any class isn't ATA_DEV_UNKNOWN, consider classification
2028c6fd2807SJeff Garzik 	 * is complete and convert all ATA_DEV_UNKNOWN to
2029c6fd2807SJeff Garzik 	 * ATA_DEV_NONE.
2030c6fd2807SJeff Garzik 	 */
2031cc0680a5STejun Heo 	ata_link_for_each_dev(dev, link)
2032f58229f8STejun Heo 		if (classes[dev->devno] != ATA_DEV_UNKNOWN)
2033c6fd2807SJeff Garzik 			break;
2034c6fd2807SJeff Garzik 
2035f58229f8STejun Heo 	if (dev) {
2036cc0680a5STejun Heo 		ata_link_for_each_dev(dev, link) {
2037f58229f8STejun Heo 			if (classes[dev->devno] == ATA_DEV_UNKNOWN)
2038f58229f8STejun Heo 				classes[dev->devno] = ATA_DEV_NONE;
2039f58229f8STejun Heo 		}
2040f58229f8STejun Heo 	}
2041c6fd2807SJeff Garzik 
2042c6fd2807SJeff Garzik 	return 0;
2043c6fd2807SJeff Garzik }
2044c6fd2807SJeff Garzik 
2045ae791c05STejun Heo static int ata_eh_followup_srst_needed(struct ata_link *link,
2046ae791c05STejun Heo 				       int rc, int classify,
2047c6fd2807SJeff Garzik 				       const unsigned int *classes)
2048c6fd2807SJeff Garzik {
2049ae791c05STejun Heo 	if (link->flags & ATA_LFLAG_NO_SRST)
2050ae791c05STejun Heo 		return 0;
2051c6fd2807SJeff Garzik 	if (rc == -EAGAIN)
2052c6fd2807SJeff Garzik 		return 1;
2053c6fd2807SJeff Garzik 	if (rc != 0)
2054c6fd2807SJeff Garzik 		return 0;
20553495de73STejun Heo 	if ((link->ap->flags & ATA_FLAG_PMP) && ata_is_host_link(link))
20563495de73STejun Heo 		return 1;
2057ae791c05STejun Heo 	if (classify && !(link->flags & ATA_LFLAG_ASSUME_CLASS) &&
2058ae791c05STejun Heo 	    classes[0] == ATA_DEV_UNKNOWN)
2059c6fd2807SJeff Garzik 		return 1;
2060c6fd2807SJeff Garzik 	return 0;
2061c6fd2807SJeff Garzik }
2062c6fd2807SJeff Garzik 
2063fb7fd614STejun Heo int ata_eh_reset(struct ata_link *link, int classify,
2064c6fd2807SJeff Garzik 		 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
2065c6fd2807SJeff Garzik 		 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
2066c6fd2807SJeff Garzik {
2067afaa5c37STejun Heo 	struct ata_port *ap = link->ap;
2068936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
2069c6fd2807SJeff Garzik 	unsigned int *classes = ehc->classes;
2070c6fd2807SJeff Garzik 	int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
207131daabdaSTejun Heo 	int try = 0;
2072f58229f8STejun Heo 	struct ata_device *dev;
207331daabdaSTejun Heo 	unsigned long deadline;
2074c6fd2807SJeff Garzik 	unsigned int action;
2075c6fd2807SJeff Garzik 	ata_reset_fn_t reset;
2076afaa5c37STejun Heo 	unsigned long flags;
2077f58229f8STejun Heo 	int rc;
2078c6fd2807SJeff Garzik 
2079c6fd2807SJeff Garzik 	/* about to reset */
2080afaa5c37STejun Heo 	spin_lock_irqsave(ap->lock, flags);
2081afaa5c37STejun Heo 	ap->pflags |= ATA_PFLAG_RESETTING;
2082afaa5c37STejun Heo 	spin_unlock_irqrestore(ap->lock, flags);
2083afaa5c37STejun Heo 
2084955e57dfSTejun Heo 	ata_eh_about_to_do(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
2085c6fd2807SJeff Garzik 
2086c6fd2807SJeff Garzik 	/* Determine which reset to use and record in ehc->i.action.
2087c6fd2807SJeff Garzik 	 * prereset() may examine and modify it.
2088c6fd2807SJeff Garzik 	 */
2089c6fd2807SJeff Garzik 	action = ehc->i.action;
2090c6fd2807SJeff Garzik 	ehc->i.action &= ~ATA_EH_RESET_MASK;
2091ae791c05STejun Heo 	if (softreset && (!hardreset || (!(link->flags & ATA_LFLAG_NO_SRST) &&
2092ae791c05STejun Heo 					 !sata_set_spd_needed(link) &&
2093c6fd2807SJeff Garzik 					 !(action & ATA_EH_HARDRESET))))
2094c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_SOFTRESET;
2095c6fd2807SJeff Garzik 	else
2096c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_HARDRESET;
2097c6fd2807SJeff Garzik 
2098c6fd2807SJeff Garzik 	if (prereset) {
2099cc0680a5STejun Heo 		rc = prereset(link, jiffies + ATA_EH_PRERESET_TIMEOUT);
2100c6fd2807SJeff Garzik 		if (rc) {
2101c961922bSAlan Cox 			if (rc == -ENOENT) {
2102cc0680a5STejun Heo 				ata_link_printk(link, KERN_DEBUG,
21034aa9ab67STejun Heo 						"port disabled. ignoring.\n");
21049af5c9c9STejun Heo 				ehc->i.action &= ~ATA_EH_RESET_MASK;
21054aa9ab67STejun Heo 
2106936fd732STejun Heo 				ata_link_for_each_dev(dev, link)
2107f58229f8STejun Heo 					classes[dev->devno] = ATA_DEV_NONE;
21084aa9ab67STejun Heo 
21094aa9ab67STejun Heo 				rc = 0;
2110c961922bSAlan Cox 			} else
2111cc0680a5STejun Heo 				ata_link_printk(link, KERN_ERR,
2112c6fd2807SJeff Garzik 					"prereset failed (errno=%d)\n", rc);
2113fccb6ea5STejun Heo 			goto out;
2114c6fd2807SJeff Garzik 		}
2115c6fd2807SJeff Garzik 	}
2116c6fd2807SJeff Garzik 
2117c6fd2807SJeff Garzik 	/* prereset() might have modified ehc->i.action */
2118c6fd2807SJeff Garzik 	if (ehc->i.action & ATA_EH_HARDRESET)
2119c6fd2807SJeff Garzik 		reset = hardreset;
2120c6fd2807SJeff Garzik 	else if (ehc->i.action & ATA_EH_SOFTRESET)
2121c6fd2807SJeff Garzik 		reset = softreset;
2122c6fd2807SJeff Garzik 	else {
2123c6fd2807SJeff Garzik 		/* prereset told us not to reset, bang classes and return */
2124936fd732STejun Heo 		ata_link_for_each_dev(dev, link)
2125f58229f8STejun Heo 			classes[dev->devno] = ATA_DEV_NONE;
2126fccb6ea5STejun Heo 		rc = 0;
2127fccb6ea5STejun Heo 		goto out;
2128c6fd2807SJeff Garzik 	}
2129c6fd2807SJeff Garzik 
2130c6fd2807SJeff Garzik 	/* did prereset() screw up?  if so, fix up to avoid oopsing */
2131c6fd2807SJeff Garzik 	if (!reset) {
2132c6fd2807SJeff Garzik 		if (softreset)
2133c6fd2807SJeff Garzik 			reset = softreset;
2134c6fd2807SJeff Garzik 		else
2135c6fd2807SJeff Garzik 			reset = hardreset;
2136c6fd2807SJeff Garzik 	}
2137c6fd2807SJeff Garzik 
2138c6fd2807SJeff Garzik  retry:
213931daabdaSTejun Heo 	deadline = jiffies + ata_eh_reset_timeouts[try++];
214031daabdaSTejun Heo 
2141c6fd2807SJeff Garzik 	/* shut up during boot probing */
2142c6fd2807SJeff Garzik 	if (verbose)
2143cc0680a5STejun Heo 		ata_link_printk(link, KERN_INFO, "%s resetting link\n",
2144c6fd2807SJeff Garzik 				reset == softreset ? "soft" : "hard");
2145c6fd2807SJeff Garzik 
2146c6fd2807SJeff Garzik 	/* mark that this EH session started with reset */
21470d64a233STejun Heo 	if (reset == hardreset)
21480d64a233STejun Heo 		ehc->i.flags |= ATA_EHI_DID_HARDRESET;
21490d64a233STejun Heo 	else
21500d64a233STejun Heo 		ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
2151c6fd2807SJeff Garzik 
2152cc0680a5STejun Heo 	rc = ata_do_reset(link, reset, classes, deadline);
2153c6fd2807SJeff Garzik 
2154c6fd2807SJeff Garzik 	if (reset == hardreset &&
2155ae791c05STejun Heo 	    ata_eh_followup_srst_needed(link, rc, classify, classes)) {
2156c6fd2807SJeff Garzik 		/* okay, let's do follow-up softreset */
2157c6fd2807SJeff Garzik 		reset = softreset;
2158c6fd2807SJeff Garzik 
2159c6fd2807SJeff Garzik 		if (!reset) {
2160cc0680a5STejun Heo 			ata_link_printk(link, KERN_ERR,
2161c6fd2807SJeff Garzik 					"follow-up softreset required "
2162c6fd2807SJeff Garzik 					"but no softreset avaliable\n");
2163fccb6ea5STejun Heo 			rc = -EINVAL;
2164fccb6ea5STejun Heo 			goto out;
2165c6fd2807SJeff Garzik 		}
2166c6fd2807SJeff Garzik 
2167955e57dfSTejun Heo 		ata_eh_about_to_do(link, NULL, ATA_EH_RESET_MASK);
2168cc0680a5STejun Heo 		rc = ata_do_reset(link, reset, classes, deadline);
2169c6fd2807SJeff Garzik 
2170ae791c05STejun Heo 		if (rc == 0 && classify && classes[0] == ATA_DEV_UNKNOWN &&
2171ae791c05STejun Heo 		    !(link->flags & ATA_LFLAG_ASSUME_CLASS)) {
2172cc0680a5STejun Heo 			ata_link_printk(link, KERN_ERR,
2173c6fd2807SJeff Garzik 					"classification failed\n");
2174fccb6ea5STejun Heo 			rc = -EINVAL;
2175fccb6ea5STejun Heo 			goto out;
2176c6fd2807SJeff Garzik 		}
2177c6fd2807SJeff Garzik 	}
2178c6fd2807SJeff Garzik 
2179ae791c05STejun Heo 	/* if we skipped follow-up srst, clear rc */
2180ae791c05STejun Heo 	if (rc == -EAGAIN)
2181ae791c05STejun Heo 		rc = 0;
2182ae791c05STejun Heo 
2183668108d7STejun Heo 	if (rc && rc != -ERESTART && try < ARRAY_SIZE(ata_eh_reset_timeouts)) {
218431daabdaSTejun Heo 		unsigned long now = jiffies;
2185c6fd2807SJeff Garzik 
218631daabdaSTejun Heo 		if (time_before(now, deadline)) {
218731daabdaSTejun Heo 			unsigned long delta = deadline - jiffies;
2188c6fd2807SJeff Garzik 
2189cc0680a5STejun Heo 			ata_link_printk(link, KERN_WARNING, "reset failed "
219031daabdaSTejun Heo 				"(errno=%d), retrying in %u secs\n",
219131daabdaSTejun Heo 				rc, (jiffies_to_msecs(delta) + 999) / 1000);
2192c6fd2807SJeff Garzik 
219390738683STejun Heo 			while (delta)
219490738683STejun Heo 				delta = schedule_timeout_uninterruptible(delta);
219531daabdaSTejun Heo 		}
219631daabdaSTejun Heo 
2197f1545154STejun Heo 		if (rc == -EPIPE ||
219831daabdaSTejun Heo 		    try == ARRAY_SIZE(ata_eh_reset_timeouts) - 1)
2199936fd732STejun Heo 			sata_down_spd_limit(link);
2200c6fd2807SJeff Garzik 		if (hardreset)
2201c6fd2807SJeff Garzik 			reset = hardreset;
2202c6fd2807SJeff Garzik 		goto retry;
2203c6fd2807SJeff Garzik 	}
2204c6fd2807SJeff Garzik 
2205c6fd2807SJeff Garzik 	if (rc == 0) {
2206008a7896STejun Heo 		u32 sstatus;
2207008a7896STejun Heo 
2208ae791c05STejun Heo 		ata_link_for_each_dev(dev, link) {
2209ae791c05STejun Heo 			/* After the reset, the device state is PIO 0
2210ae791c05STejun Heo 			 * and the controller state is undefined.
2211ae791c05STejun Heo 			 * Record the mode.
2212c6fd2807SJeff Garzik 			 */
2213f58229f8STejun Heo 			dev->pio_mode = XFER_PIO_0;
2214c6fd2807SJeff Garzik 
2215ae791c05STejun Heo 			if (ata_link_offline(link))
2216ae791c05STejun Heo 				continue;
2217ae791c05STejun Heo 
2218ae791c05STejun Heo 			/* apply class override and convert UNKNOWN to NONE */
2219ae791c05STejun Heo 			if (link->flags & ATA_LFLAG_ASSUME_ATA)
2220ae791c05STejun Heo 				classes[dev->devno] = ATA_DEV_ATA;
2221ae791c05STejun Heo 			else if (link->flags & ATA_LFLAG_ASSUME_SEMB)
2222ae791c05STejun Heo 				classes[dev->devno] = ATA_DEV_SEMB_UNSUP; /* not yet */
2223ae791c05STejun Heo 			else if (classes[dev->devno] == ATA_DEV_UNKNOWN)
2224ae791c05STejun Heo 				classes[dev->devno] = ATA_DEV_NONE;
2225ae791c05STejun Heo 		}
2226ae791c05STejun Heo 
2227008a7896STejun Heo 		/* record current link speed */
2228936fd732STejun Heo 		if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2229936fd732STejun Heo 			link->sata_spd = (sstatus >> 4) & 0xf;
2230008a7896STejun Heo 
2231c6fd2807SJeff Garzik 		if (postreset)
2232cc0680a5STejun Heo 			postreset(link, classes);
2233c6fd2807SJeff Garzik 
2234c6fd2807SJeff Garzik 		/* reset successful, schedule revalidation */
2235955e57dfSTejun Heo 		ata_eh_done(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
2236c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_REVALIDATE;
2237c6fd2807SJeff Garzik 	}
2238fccb6ea5STejun Heo  out:
2239fccb6ea5STejun Heo 	/* clear hotplug flag */
2240fccb6ea5STejun Heo 	ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2241afaa5c37STejun Heo 
2242afaa5c37STejun Heo 	spin_lock_irqsave(ap->lock, flags);
2243afaa5c37STejun Heo 	ap->pflags &= ~ATA_PFLAG_RESETTING;
2244afaa5c37STejun Heo 	spin_unlock_irqrestore(ap->lock, flags);
2245afaa5c37STejun Heo 
2246c6fd2807SJeff Garzik 	return rc;
2247c6fd2807SJeff Garzik }
2248c6fd2807SJeff Garzik 
22490260731fSTejun Heo static int ata_eh_revalidate_and_attach(struct ata_link *link,
2250c6fd2807SJeff Garzik 					struct ata_device **r_failed_dev)
2251c6fd2807SJeff Garzik {
22520260731fSTejun Heo 	struct ata_port *ap = link->ap;
22530260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
2254c6fd2807SJeff Garzik 	struct ata_device *dev;
22558c3c52a8STejun Heo 	unsigned int new_mask = 0;
2256c6fd2807SJeff Garzik 	unsigned long flags;
2257f58229f8STejun Heo 	int rc = 0;
2258c6fd2807SJeff Garzik 
2259c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
2260c6fd2807SJeff Garzik 
22618c3c52a8STejun Heo 	/* For PATA drive side cable detection to work, IDENTIFY must
22628c3c52a8STejun Heo 	 * be done backwards such that PDIAG- is released by the slave
22638c3c52a8STejun Heo 	 * device before the master device is identified.
22648c3c52a8STejun Heo 	 */
22650260731fSTejun Heo 	ata_link_for_each_dev_reverse(dev, link) {
2266f58229f8STejun Heo 		unsigned int action = ata_eh_dev_action(dev);
2267f58229f8STejun Heo 		unsigned int readid_flags = 0;
2268c6fd2807SJeff Garzik 
2269bff04647STejun Heo 		if (ehc->i.flags & ATA_EHI_DID_RESET)
2270bff04647STejun Heo 			readid_flags |= ATA_READID_POSTRESET;
2271bff04647STejun Heo 
22729666f400STejun Heo 		if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
2273633273a3STejun Heo 			WARN_ON(dev->class == ATA_DEV_PMP);
2274633273a3STejun Heo 
22750260731fSTejun Heo 			if (ata_link_offline(link)) {
2276c6fd2807SJeff Garzik 				rc = -EIO;
22778c3c52a8STejun Heo 				goto err;
2278c6fd2807SJeff Garzik 			}
2279c6fd2807SJeff Garzik 
22800260731fSTejun Heo 			ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
2281422c9daaSTejun Heo 			rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
2282422c9daaSTejun Heo 						readid_flags);
2283c6fd2807SJeff Garzik 			if (rc)
22848c3c52a8STejun Heo 				goto err;
2285c6fd2807SJeff Garzik 
22860260731fSTejun Heo 			ata_eh_done(link, dev, ATA_EH_REVALIDATE);
2287c6fd2807SJeff Garzik 
2288baa1e78aSTejun Heo 			/* Configuration may have changed, reconfigure
2289baa1e78aSTejun Heo 			 * transfer mode.
2290baa1e78aSTejun Heo 			 */
2291baa1e78aSTejun Heo 			ehc->i.flags |= ATA_EHI_SETMODE;
2292baa1e78aSTejun Heo 
2293c6fd2807SJeff Garzik 			/* schedule the scsi_rescan_device() here */
2294c6fd2807SJeff Garzik 			queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
2295c6fd2807SJeff Garzik 		} else if (dev->class == ATA_DEV_UNKNOWN &&
2296c6fd2807SJeff Garzik 			   ehc->tries[dev->devno] &&
2297c6fd2807SJeff Garzik 			   ata_class_enabled(ehc->classes[dev->devno])) {
2298c6fd2807SJeff Garzik 			dev->class = ehc->classes[dev->devno];
2299c6fd2807SJeff Garzik 
2300633273a3STejun Heo 			if (dev->class == ATA_DEV_PMP)
2301633273a3STejun Heo 				rc = sata_pmp_attach(dev);
2302633273a3STejun Heo 			else
2303633273a3STejun Heo 				rc = ata_dev_read_id(dev, &dev->class,
2304633273a3STejun Heo 						     readid_flags, dev->id);
23058c3c52a8STejun Heo 			switch (rc) {
23068c3c52a8STejun Heo 			case 0:
2307f58229f8STejun Heo 				new_mask |= 1 << dev->devno;
23088c3c52a8STejun Heo 				break;
23098c3c52a8STejun Heo 			case -ENOENT:
231055a8e2c8STejun Heo 				/* IDENTIFY was issued to non-existent
231155a8e2c8STejun Heo 				 * device.  No need to reset.  Just
231255a8e2c8STejun Heo 				 * thaw and kill the device.
231355a8e2c8STejun Heo 				 */
231455a8e2c8STejun Heo 				ata_eh_thaw_port(ap);
231555a8e2c8STejun Heo 				dev->class = ATA_DEV_UNKNOWN;
2316c6fd2807SJeff Garzik 				break;
23178c3c52a8STejun Heo 			default:
23188c3c52a8STejun Heo 				dev->class = ATA_DEV_UNKNOWN;
23198c3c52a8STejun Heo 				goto err;
23208c3c52a8STejun Heo 			}
23218c3c52a8STejun Heo 		}
2322c6fd2807SJeff Garzik 	}
2323c6fd2807SJeff Garzik 
2324c1c4e8d5STejun Heo 	/* PDIAG- should have been released, ask cable type if post-reset */
23259b1e2658STejun Heo 	if (ata_is_host_link(link) && ap->ops->cable_detect &&
23269b1e2658STejun Heo 	    (ehc->i.flags & ATA_EHI_DID_RESET))
2327c1c4e8d5STejun Heo 		ap->cbl = ap->ops->cable_detect(ap);
2328c1c4e8d5STejun Heo 
23298c3c52a8STejun Heo 	/* Configure new devices forward such that user doesn't see
23308c3c52a8STejun Heo 	 * device detection messages backwards.
23318c3c52a8STejun Heo 	 */
23320260731fSTejun Heo 	ata_link_for_each_dev(dev, link) {
2333633273a3STejun Heo 		if (!(new_mask & (1 << dev->devno)) ||
2334633273a3STejun Heo 		    dev->class == ATA_DEV_PMP)
23358c3c52a8STejun Heo 			continue;
23368c3c52a8STejun Heo 
23378c3c52a8STejun Heo 		ehc->i.flags |= ATA_EHI_PRINTINFO;
23388c3c52a8STejun Heo 		rc = ata_dev_configure(dev);
23398c3c52a8STejun Heo 		ehc->i.flags &= ~ATA_EHI_PRINTINFO;
23408c3c52a8STejun Heo 		if (rc)
23418c3c52a8STejun Heo 			goto err;
23428c3c52a8STejun Heo 
2343c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
2344c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
2345c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
2346baa1e78aSTejun Heo 
234755a8e2c8STejun Heo 		/* new device discovered, configure xfermode */
2348baa1e78aSTejun Heo 		ehc->i.flags |= ATA_EHI_SETMODE;
2349c6fd2807SJeff Garzik 	}
2350c6fd2807SJeff Garzik 
23518c3c52a8STejun Heo 	return 0;
23528c3c52a8STejun Heo 
23538c3c52a8STejun Heo  err:
2354c6fd2807SJeff Garzik 	*r_failed_dev = dev;
23558c3c52a8STejun Heo 	DPRINTK("EXIT rc=%d\n", rc);
2356c6fd2807SJeff Garzik 	return rc;
2357c6fd2807SJeff Garzik }
2358c6fd2807SJeff Garzik 
23590260731fSTejun Heo static int ata_link_nr_enabled(struct ata_link *link)
2360c6fd2807SJeff Garzik {
2361f58229f8STejun Heo 	struct ata_device *dev;
2362f58229f8STejun Heo 	int cnt = 0;
2363c6fd2807SJeff Garzik 
23640260731fSTejun Heo 	ata_link_for_each_dev(dev, link)
2365f58229f8STejun Heo 		if (ata_dev_enabled(dev))
2366c6fd2807SJeff Garzik 			cnt++;
2367c6fd2807SJeff Garzik 	return cnt;
2368c6fd2807SJeff Garzik }
2369c6fd2807SJeff Garzik 
23700260731fSTejun Heo static int ata_link_nr_vacant(struct ata_link *link)
2371c6fd2807SJeff Garzik {
2372f58229f8STejun Heo 	struct ata_device *dev;
2373f58229f8STejun Heo 	int cnt = 0;
2374c6fd2807SJeff Garzik 
23750260731fSTejun Heo 	ata_link_for_each_dev(dev, link)
2376f58229f8STejun Heo 		if (dev->class == ATA_DEV_UNKNOWN)
2377c6fd2807SJeff Garzik 			cnt++;
2378c6fd2807SJeff Garzik 	return cnt;
2379c6fd2807SJeff Garzik }
2380c6fd2807SJeff Garzik 
23810260731fSTejun Heo static int ata_eh_skip_recovery(struct ata_link *link)
2382c6fd2807SJeff Garzik {
23830260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
2384f58229f8STejun Heo 	struct ata_device *dev;
2385c6fd2807SJeff Garzik 
2386f9df58cbSTejun Heo 	/* skip disabled links */
2387f9df58cbSTejun Heo 	if (link->flags & ATA_LFLAG_DISABLED)
2388f9df58cbSTejun Heo 		return 1;
2389f9df58cbSTejun Heo 
2390c6fd2807SJeff Garzik 	/* thaw frozen port, resume link and recover failed devices */
23910260731fSTejun Heo 	if ((link->ap->pflags & ATA_PFLAG_FROZEN) ||
23920260731fSTejun Heo 	    (ehc->i.flags & ATA_EHI_RESUME_LINK) || ata_link_nr_enabled(link))
2393c6fd2807SJeff Garzik 		return 0;
2394c6fd2807SJeff Garzik 
2395c6fd2807SJeff Garzik 	/* skip if class codes for all vacant slots are ATA_DEV_NONE */
23960260731fSTejun Heo 	ata_link_for_each_dev(dev, link) {
2397c6fd2807SJeff Garzik 		if (dev->class == ATA_DEV_UNKNOWN &&
2398c6fd2807SJeff Garzik 		    ehc->classes[dev->devno] != ATA_DEV_NONE)
2399c6fd2807SJeff Garzik 			return 0;
2400c6fd2807SJeff Garzik 	}
2401c6fd2807SJeff Garzik 
2402c6fd2807SJeff Garzik 	return 1;
2403c6fd2807SJeff Garzik }
2404c6fd2807SJeff Garzik 
24059b1e2658STejun Heo static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
2406fee7ca72STejun Heo {
24079af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
2408fee7ca72STejun Heo 
2409fee7ca72STejun Heo 	ehc->tries[dev->devno]--;
2410fee7ca72STejun Heo 
2411fee7ca72STejun Heo 	switch (err) {
2412fee7ca72STejun Heo 	case -ENODEV:
2413fee7ca72STejun Heo 		/* device missing or wrong IDENTIFY data, schedule probing */
2414fee7ca72STejun Heo 		ehc->i.probe_mask |= (1 << dev->devno);
2415fee7ca72STejun Heo 	case -EINVAL:
2416fee7ca72STejun Heo 		/* give it just one more chance */
2417fee7ca72STejun Heo 		ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
2418fee7ca72STejun Heo 	case -EIO:
2419fee7ca72STejun Heo 		if (ehc->tries[dev->devno] == 1) {
2420fee7ca72STejun Heo 			/* This is the last chance, better to slow
2421fee7ca72STejun Heo 			 * down than lose it.
2422fee7ca72STejun Heo 			 */
2423936fd732STejun Heo 			sata_down_spd_limit(dev->link);
2424fee7ca72STejun Heo 			ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2425fee7ca72STejun Heo 		}
2426fee7ca72STejun Heo 	}
2427fee7ca72STejun Heo 
2428fee7ca72STejun Heo 	if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2429fee7ca72STejun Heo 		/* disable device if it has used up all its chances */
2430fee7ca72STejun Heo 		ata_dev_disable(dev);
2431fee7ca72STejun Heo 
2432fee7ca72STejun Heo 		/* detach if offline */
2433936fd732STejun Heo 		if (ata_link_offline(dev->link))
2434fee7ca72STejun Heo 			ata_eh_detach_dev(dev);
2435fee7ca72STejun Heo 
2436fee7ca72STejun Heo 		/* probe if requested */
2437fee7ca72STejun Heo 		if ((ehc->i.probe_mask & (1 << dev->devno)) &&
2438fee7ca72STejun Heo 		    !(ehc->did_probe_mask & (1 << dev->devno))) {
2439fee7ca72STejun Heo 			ata_eh_detach_dev(dev);
2440fee7ca72STejun Heo 			ata_dev_init(dev);
2441fee7ca72STejun Heo 
2442fee7ca72STejun Heo 			ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2443fee7ca72STejun Heo 			ehc->did_probe_mask |= (1 << dev->devno);
2444fee7ca72STejun Heo 			ehc->i.action |= ATA_EH_SOFTRESET;
2445fee7ca72STejun Heo 		}
24469b1e2658STejun Heo 
24479b1e2658STejun Heo 		return 1;
2448fee7ca72STejun Heo 	} else {
2449fee7ca72STejun Heo 		/* soft didn't work?  be haaaaard */
2450fee7ca72STejun Heo 		if (ehc->i.flags & ATA_EHI_DID_RESET)
2451fee7ca72STejun Heo 			ehc->i.action |= ATA_EH_HARDRESET;
2452fee7ca72STejun Heo 		else
2453fee7ca72STejun Heo 			ehc->i.action |= ATA_EH_SOFTRESET;
24549b1e2658STejun Heo 
24559b1e2658STejun Heo 		return 0;
2456fee7ca72STejun Heo 	}
2457fee7ca72STejun Heo }
2458fee7ca72STejun Heo 
2459c6fd2807SJeff Garzik /**
2460c6fd2807SJeff Garzik  *	ata_eh_recover - recover host port after error
2461c6fd2807SJeff Garzik  *	@ap: host port to recover
2462c6fd2807SJeff Garzik  *	@prereset: prereset method (can be NULL)
2463c6fd2807SJeff Garzik  *	@softreset: softreset method (can be NULL)
2464c6fd2807SJeff Garzik  *	@hardreset: hardreset method (can be NULL)
2465c6fd2807SJeff Garzik  *	@postreset: postreset method (can be NULL)
24669b1e2658STejun Heo  *	@r_failed_link: out parameter for failed link
2467c6fd2807SJeff Garzik  *
2468c6fd2807SJeff Garzik  *	This is the alpha and omega, eum and yang, heart and soul of
2469c6fd2807SJeff Garzik  *	libata exception handling.  On entry, actions required to
24709b1e2658STejun Heo  *	recover each link and hotplug requests are recorded in the
24719b1e2658STejun Heo  *	link's eh_context.  This function executes all the operations
24729b1e2658STejun Heo  *	with appropriate retrials and fallbacks to resurrect failed
2473c6fd2807SJeff Garzik  *	devices, detach goners and greet newcomers.
2474c6fd2807SJeff Garzik  *
2475c6fd2807SJeff Garzik  *	LOCKING:
2476c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
2477c6fd2807SJeff Garzik  *
2478c6fd2807SJeff Garzik  *	RETURNS:
2479c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
2480c6fd2807SJeff Garzik  */
2481fb7fd614STejun Heo int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
2482c6fd2807SJeff Garzik 		   ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
24839b1e2658STejun Heo 		   ata_postreset_fn_t postreset,
24849b1e2658STejun Heo 		   struct ata_link **r_failed_link)
2485c6fd2807SJeff Garzik {
24869b1e2658STejun Heo 	struct ata_link *link;
2487c6fd2807SJeff Garzik 	struct ata_device *dev;
24889b1e2658STejun Heo 	int nr_failed_devs, nr_disabled_devs;
24899b1e2658STejun Heo 	int reset, rc;
2490f9df58cbSTejun Heo 	unsigned long flags;
2491c6fd2807SJeff Garzik 
2492c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
2493c6fd2807SJeff Garzik 
2494c6fd2807SJeff Garzik 	/* prep for recovery */
24959b1e2658STejun Heo 	ata_port_for_each_link(link, ap) {
24969b1e2658STejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
24979b1e2658STejun Heo 
2498f9df58cbSTejun Heo 		/* re-enable link? */
2499f9df58cbSTejun Heo 		if (ehc->i.action & ATA_EH_ENABLE_LINK) {
2500f9df58cbSTejun Heo 			ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
2501f9df58cbSTejun Heo 			spin_lock_irqsave(ap->lock, flags);
2502f9df58cbSTejun Heo 			link->flags &= ~ATA_LFLAG_DISABLED;
2503f9df58cbSTejun Heo 			spin_unlock_irqrestore(ap->lock, flags);
2504f9df58cbSTejun Heo 			ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
2505f9df58cbSTejun Heo 		}
2506f9df58cbSTejun Heo 
25070260731fSTejun Heo 		ata_link_for_each_dev(dev, link) {
2508fd995f70STejun Heo 			if (link->flags & ATA_LFLAG_NO_RETRY)
2509fd995f70STejun Heo 				ehc->tries[dev->devno] = 1;
2510fd995f70STejun Heo 			else
2511c6fd2807SJeff Garzik 				ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2512c6fd2807SJeff Garzik 
251379a55b72STejun Heo 			/* collect port action mask recorded in dev actions */
25149b1e2658STejun Heo 			ehc->i.action |= ehc->i.dev_action[dev->devno] &
25159b1e2658STejun Heo 					 ~ATA_EH_PERDEV_MASK;
2516f58229f8STejun Heo 			ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
251779a55b72STejun Heo 
2518c6fd2807SJeff Garzik 			/* process hotplug request */
2519c6fd2807SJeff Garzik 			if (dev->flags & ATA_DFLAG_DETACH)
2520c6fd2807SJeff Garzik 				ata_eh_detach_dev(dev);
2521c6fd2807SJeff Garzik 
2522c6fd2807SJeff Garzik 			if (!ata_dev_enabled(dev) &&
2523c6fd2807SJeff Garzik 			    ((ehc->i.probe_mask & (1 << dev->devno)) &&
2524c6fd2807SJeff Garzik 			     !(ehc->did_probe_mask & (1 << dev->devno)))) {
2525c6fd2807SJeff Garzik 				ata_eh_detach_dev(dev);
2526c6fd2807SJeff Garzik 				ata_dev_init(dev);
2527c6fd2807SJeff Garzik 				ehc->did_probe_mask |= (1 << dev->devno);
2528c6fd2807SJeff Garzik 				ehc->i.action |= ATA_EH_SOFTRESET;
2529c6fd2807SJeff Garzik 			}
2530c6fd2807SJeff Garzik 		}
25319b1e2658STejun Heo 	}
2532c6fd2807SJeff Garzik 
2533c6fd2807SJeff Garzik  retry:
2534c6fd2807SJeff Garzik 	rc = 0;
25359b1e2658STejun Heo 	nr_failed_devs = 0;
25369b1e2658STejun Heo 	nr_disabled_devs = 0;
25379b1e2658STejun Heo 	reset = 0;
2538c6fd2807SJeff Garzik 
2539c6fd2807SJeff Garzik 	/* if UNLOADING, finish immediately */
2540c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_UNLOADING)
2541c6fd2807SJeff Garzik 		goto out;
2542c6fd2807SJeff Garzik 
25439b1e2658STejun Heo 	/* prep for EH */
25449b1e2658STejun Heo 	ata_port_for_each_link(link, ap) {
25459b1e2658STejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
25469b1e2658STejun Heo 
2547c6fd2807SJeff Garzik 		/* skip EH if possible. */
25480260731fSTejun Heo 		if (ata_eh_skip_recovery(link))
2549c6fd2807SJeff Garzik 			ehc->i.action = 0;
2550c6fd2807SJeff Garzik 
25519b1e2658STejun Heo 		/* do we need to reset? */
25529b1e2658STejun Heo 		if (ehc->i.action & ATA_EH_RESET_MASK)
25539b1e2658STejun Heo 			reset = 1;
25549b1e2658STejun Heo 
25550260731fSTejun Heo 		ata_link_for_each_dev(dev, link)
2556f58229f8STejun Heo 			ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
25579b1e2658STejun Heo 	}
2558c6fd2807SJeff Garzik 
2559c6fd2807SJeff Garzik 	/* reset */
25609b1e2658STejun Heo 	if (reset) {
2561b06ce3e5STejun Heo 		/* if PMP is attached, this function only deals with
2562b06ce3e5STejun Heo 		 * downstream links, port should stay thawed.
2563b06ce3e5STejun Heo 		 */
2564b06ce3e5STejun Heo 		if (!ap->nr_pmp_links)
2565c6fd2807SJeff Garzik 			ata_eh_freeze_port(ap);
2566c6fd2807SJeff Garzik 
25679b1e2658STejun Heo 		ata_port_for_each_link(link, ap) {
25689b1e2658STejun Heo 			struct ata_eh_context *ehc = &link->eh_context;
25699b1e2658STejun Heo 
25709b1e2658STejun Heo 			if (!(ehc->i.action & ATA_EH_RESET_MASK))
25719b1e2658STejun Heo 				continue;
25729b1e2658STejun Heo 
25739b1e2658STejun Heo 			rc = ata_eh_reset(link, ata_link_nr_vacant(link),
25749b1e2658STejun Heo 					  prereset, softreset, hardreset,
25759b1e2658STejun Heo 					  postreset);
2576c6fd2807SJeff Garzik 			if (rc) {
25770260731fSTejun Heo 				ata_link_printk(link, KERN_ERR,
2578c6fd2807SJeff Garzik 						"reset failed, giving up\n");
2579c6fd2807SJeff Garzik 				goto out;
2580c6fd2807SJeff Garzik 			}
25819b1e2658STejun Heo 		}
2582c6fd2807SJeff Garzik 
2583b06ce3e5STejun Heo 		if (!ap->nr_pmp_links)
2584c6fd2807SJeff Garzik 			ata_eh_thaw_port(ap);
2585c6fd2807SJeff Garzik 	}
2586c6fd2807SJeff Garzik 
25879b1e2658STejun Heo 	/* the rest */
25889b1e2658STejun Heo 	ata_port_for_each_link(link, ap) {
25899b1e2658STejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
25909b1e2658STejun Heo 
2591c6fd2807SJeff Garzik 		/* revalidate existing devices and attach new ones */
25920260731fSTejun Heo 		rc = ata_eh_revalidate_and_attach(link, &dev);
2593c6fd2807SJeff Garzik 		if (rc)
2594c6fd2807SJeff Garzik 			goto dev_fail;
2595c6fd2807SJeff Garzik 
2596633273a3STejun Heo 		/* if PMP got attached, return, pmp EH will take care of it */
2597633273a3STejun Heo 		if (link->device->class == ATA_DEV_PMP) {
2598633273a3STejun Heo 			ehc->i.action = 0;
2599633273a3STejun Heo 			return 0;
2600633273a3STejun Heo 		}
2601633273a3STejun Heo 
2602baa1e78aSTejun Heo 		/* configure transfer mode if necessary */
2603baa1e78aSTejun Heo 		if (ehc->i.flags & ATA_EHI_SETMODE) {
26040260731fSTejun Heo 			rc = ata_set_mode(link, &dev);
26054ae72a1eSTejun Heo 			if (rc)
2606c6fd2807SJeff Garzik 				goto dev_fail;
2607baa1e78aSTejun Heo 			ehc->i.flags &= ~ATA_EHI_SETMODE;
2608c6fd2807SJeff Garzik 		}
2609c6fd2807SJeff Garzik 
26109b1e2658STejun Heo 		/* this link is okay now */
26119b1e2658STejun Heo 		ehc->i.flags = 0;
26129b1e2658STejun Heo 		continue;
2613c6fd2807SJeff Garzik 
2614c6fd2807SJeff Garzik 	dev_fail:
26159b1e2658STejun Heo 		nr_failed_devs++;
26169b1e2658STejun Heo 		if (ata_eh_handle_dev_fail(dev, rc))
26179b1e2658STejun Heo 			nr_disabled_devs++;
2618c6fd2807SJeff Garzik 
2619b06ce3e5STejun Heo 		if (ap->pflags & ATA_PFLAG_FROZEN) {
2620b06ce3e5STejun Heo 			/* PMP reset requires working host port.
2621b06ce3e5STejun Heo 			 * Can't retry if it's frozen.
2622b06ce3e5STejun Heo 			 */
2623b06ce3e5STejun Heo 			if (ap->nr_pmp_links)
2624b06ce3e5STejun Heo 				goto out;
26259b1e2658STejun Heo 			break;
26269b1e2658STejun Heo 		}
2627b06ce3e5STejun Heo 	}
26289b1e2658STejun Heo 
26299b1e2658STejun Heo 	if (nr_failed_devs) {
26309b1e2658STejun Heo 		if (nr_failed_devs != nr_disabled_devs) {
26319b1e2658STejun Heo 			ata_port_printk(ap, KERN_WARNING, "failed to recover "
26329b1e2658STejun Heo 					"some devices, retrying in 5 secs\n");
2633c6fd2807SJeff Garzik 			ssleep(5);
2634c6fd2807SJeff Garzik 		} else {
26359b1e2658STejun Heo 			/* no device left to recover, repeat fast */
2636c6fd2807SJeff Garzik 			msleep(500);
2637c6fd2807SJeff Garzik 		}
2638c6fd2807SJeff Garzik 
2639c6fd2807SJeff Garzik 		goto retry;
26409b1e2658STejun Heo 	}
2641c6fd2807SJeff Garzik 
2642c6fd2807SJeff Garzik  out:
26439b1e2658STejun Heo 	if (rc && r_failed_link)
26449b1e2658STejun Heo 		*r_failed_link = link;
2645c6fd2807SJeff Garzik 
2646c6fd2807SJeff Garzik 	DPRINTK("EXIT, rc=%d\n", rc);
2647c6fd2807SJeff Garzik 	return rc;
2648c6fd2807SJeff Garzik }
2649c6fd2807SJeff Garzik 
2650c6fd2807SJeff Garzik /**
2651c6fd2807SJeff Garzik  *	ata_eh_finish - finish up EH
2652c6fd2807SJeff Garzik  *	@ap: host port to finish EH for
2653c6fd2807SJeff Garzik  *
2654c6fd2807SJeff Garzik  *	Recovery is complete.  Clean up EH states and retry or finish
2655c6fd2807SJeff Garzik  *	failed qcs.
2656c6fd2807SJeff Garzik  *
2657c6fd2807SJeff Garzik  *	LOCKING:
2658c6fd2807SJeff Garzik  *	None.
2659c6fd2807SJeff Garzik  */
2660fb7fd614STejun Heo void ata_eh_finish(struct ata_port *ap)
2661c6fd2807SJeff Garzik {
2662c6fd2807SJeff Garzik 	int tag;
2663c6fd2807SJeff Garzik 
2664c6fd2807SJeff Garzik 	/* retry or finish qcs */
2665c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2666c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2667c6fd2807SJeff Garzik 
2668c6fd2807SJeff Garzik 		if (!(qc->flags & ATA_QCFLAG_FAILED))
2669c6fd2807SJeff Garzik 			continue;
2670c6fd2807SJeff Garzik 
2671c6fd2807SJeff Garzik 		if (qc->err_mask) {
2672c6fd2807SJeff Garzik 			/* FIXME: Once EH migration is complete,
2673c6fd2807SJeff Garzik 			 * generate sense data in this function,
2674c6fd2807SJeff Garzik 			 * considering both err_mask and tf.
2675c6fd2807SJeff Garzik 			 */
2676c6fd2807SJeff Garzik 			if (qc->err_mask & AC_ERR_INVALID)
2677c6fd2807SJeff Garzik 				ata_eh_qc_complete(qc);
2678c6fd2807SJeff Garzik 			else
2679c6fd2807SJeff Garzik 				ata_eh_qc_retry(qc);
2680c6fd2807SJeff Garzik 		} else {
2681c6fd2807SJeff Garzik 			if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2682c6fd2807SJeff Garzik 				ata_eh_qc_complete(qc);
2683c6fd2807SJeff Garzik 			} else {
2684c6fd2807SJeff Garzik 				/* feed zero TF to sense generation */
2685c6fd2807SJeff Garzik 				memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2686c6fd2807SJeff Garzik 				ata_eh_qc_retry(qc);
2687c6fd2807SJeff Garzik 			}
2688c6fd2807SJeff Garzik 		}
2689c6fd2807SJeff Garzik 	}
2690da917d69STejun Heo 
2691da917d69STejun Heo 	/* make sure nr_active_links is zero after EH */
2692da917d69STejun Heo 	WARN_ON(ap->nr_active_links);
2693da917d69STejun Heo 	ap->nr_active_links = 0;
2694c6fd2807SJeff Garzik }
2695c6fd2807SJeff Garzik 
2696c6fd2807SJeff Garzik /**
2697c6fd2807SJeff Garzik  *	ata_do_eh - do standard error handling
2698c6fd2807SJeff Garzik  *	@ap: host port to handle error for
2699c6fd2807SJeff Garzik  *	@prereset: prereset method (can be NULL)
2700c6fd2807SJeff Garzik  *	@softreset: softreset method (can be NULL)
2701c6fd2807SJeff Garzik  *	@hardreset: hardreset method (can be NULL)
2702c6fd2807SJeff Garzik  *	@postreset: postreset method (can be NULL)
2703c6fd2807SJeff Garzik  *
2704c6fd2807SJeff Garzik  *	Perform standard error handling sequence.
2705c6fd2807SJeff Garzik  *
2706c6fd2807SJeff Garzik  *	LOCKING:
2707c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
2708c6fd2807SJeff Garzik  */
2709c6fd2807SJeff Garzik void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2710c6fd2807SJeff Garzik 	       ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2711c6fd2807SJeff Garzik 	       ata_postreset_fn_t postreset)
2712c6fd2807SJeff Garzik {
27139b1e2658STejun Heo 	struct ata_device *dev;
27149b1e2658STejun Heo 	int rc;
27159b1e2658STejun Heo 
27169b1e2658STejun Heo 	ata_eh_autopsy(ap);
27179b1e2658STejun Heo 	ata_eh_report(ap);
27189b1e2658STejun Heo 
27199b1e2658STejun Heo 	rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
27209b1e2658STejun Heo 			    NULL);
27219b1e2658STejun Heo 	if (rc) {
27229b1e2658STejun Heo 		ata_link_for_each_dev(dev, &ap->link)
27239b1e2658STejun Heo 			ata_dev_disable(dev);
27249b1e2658STejun Heo 	}
27259b1e2658STejun Heo 
2726c6fd2807SJeff Garzik 	ata_eh_finish(ap);
2727c6fd2807SJeff Garzik }
2728c6fd2807SJeff Garzik 
27296ffa01d8STejun Heo #ifdef CONFIG_PM
2730c6fd2807SJeff Garzik /**
2731c6fd2807SJeff Garzik  *	ata_eh_handle_port_suspend - perform port suspend operation
2732c6fd2807SJeff Garzik  *	@ap: port to suspend
2733c6fd2807SJeff Garzik  *
2734c6fd2807SJeff Garzik  *	Suspend @ap.
2735c6fd2807SJeff Garzik  *
2736c6fd2807SJeff Garzik  *	LOCKING:
2737c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
2738c6fd2807SJeff Garzik  */
2739c6fd2807SJeff Garzik static void ata_eh_handle_port_suspend(struct ata_port *ap)
2740c6fd2807SJeff Garzik {
2741c6fd2807SJeff Garzik 	unsigned long flags;
2742c6fd2807SJeff Garzik 	int rc = 0;
2743c6fd2807SJeff Garzik 
2744c6fd2807SJeff Garzik 	/* are we suspending? */
2745c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
2746c6fd2807SJeff Garzik 	if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2747c6fd2807SJeff Garzik 	    ap->pm_mesg.event == PM_EVENT_ON) {
2748c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
2749c6fd2807SJeff Garzik 		return;
2750c6fd2807SJeff Garzik 	}
2751c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
2752c6fd2807SJeff Garzik 
2753c6fd2807SJeff Garzik 	WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
2754c6fd2807SJeff Garzik 
275564578a3dSTejun Heo 	/* tell ACPI we're suspending */
275664578a3dSTejun Heo 	rc = ata_acpi_on_suspend(ap);
275764578a3dSTejun Heo 	if (rc)
275864578a3dSTejun Heo 		goto out;
275964578a3dSTejun Heo 
2760c6fd2807SJeff Garzik 	/* suspend */
2761c6fd2807SJeff Garzik 	ata_eh_freeze_port(ap);
2762c6fd2807SJeff Garzik 
2763c6fd2807SJeff Garzik 	if (ap->ops->port_suspend)
2764c6fd2807SJeff Garzik 		rc = ap->ops->port_suspend(ap, ap->pm_mesg);
2765c6fd2807SJeff Garzik 
276664578a3dSTejun Heo  out:
2767c6fd2807SJeff Garzik 	/* report result */
2768c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
2769c6fd2807SJeff Garzik 
2770c6fd2807SJeff Garzik 	ap->pflags &= ~ATA_PFLAG_PM_PENDING;
2771c6fd2807SJeff Garzik 	if (rc == 0)
2772c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_SUSPENDED;
277364578a3dSTejun Heo 	else if (ap->pflags & ATA_PFLAG_FROZEN)
2774c6fd2807SJeff Garzik 		ata_port_schedule_eh(ap);
2775c6fd2807SJeff Garzik 
2776c6fd2807SJeff Garzik 	if (ap->pm_result) {
2777c6fd2807SJeff Garzik 		*ap->pm_result = rc;
2778c6fd2807SJeff Garzik 		ap->pm_result = NULL;
2779c6fd2807SJeff Garzik 	}
2780c6fd2807SJeff Garzik 
2781c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
2782c6fd2807SJeff Garzik 
2783c6fd2807SJeff Garzik 	return;
2784c6fd2807SJeff Garzik }
2785c6fd2807SJeff Garzik 
2786c6fd2807SJeff Garzik /**
2787c6fd2807SJeff Garzik  *	ata_eh_handle_port_resume - perform port resume operation
2788c6fd2807SJeff Garzik  *	@ap: port to resume
2789c6fd2807SJeff Garzik  *
2790c6fd2807SJeff Garzik  *	Resume @ap.
2791c6fd2807SJeff Garzik  *
2792c6fd2807SJeff Garzik  *	LOCKING:
2793c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
2794c6fd2807SJeff Garzik  */
2795c6fd2807SJeff Garzik static void ata_eh_handle_port_resume(struct ata_port *ap)
2796c6fd2807SJeff Garzik {
2797c6fd2807SJeff Garzik 	unsigned long flags;
27989666f400STejun Heo 	int rc = 0;
2799c6fd2807SJeff Garzik 
2800c6fd2807SJeff Garzik 	/* are we resuming? */
2801c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
2802c6fd2807SJeff Garzik 	if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2803c6fd2807SJeff Garzik 	    ap->pm_mesg.event != PM_EVENT_ON) {
2804c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
2805c6fd2807SJeff Garzik 		return;
2806c6fd2807SJeff Garzik 	}
2807c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
2808c6fd2807SJeff Garzik 
28099666f400STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
2810c6fd2807SJeff Garzik 
2811c6fd2807SJeff Garzik 	if (ap->ops->port_resume)
2812c6fd2807SJeff Garzik 		rc = ap->ops->port_resume(ap);
2813c6fd2807SJeff Garzik 
28146746544cSTejun Heo 	/* tell ACPI that we're resuming */
28156746544cSTejun Heo 	ata_acpi_on_resume(ap);
28166746544cSTejun Heo 
28179666f400STejun Heo 	/* report result */
2818c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
2819c6fd2807SJeff Garzik 	ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
2820c6fd2807SJeff Garzik 	if (ap->pm_result) {
2821c6fd2807SJeff Garzik 		*ap->pm_result = rc;
2822c6fd2807SJeff Garzik 		ap->pm_result = NULL;
2823c6fd2807SJeff Garzik 	}
2824c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
2825c6fd2807SJeff Garzik }
28266ffa01d8STejun Heo #endif /* CONFIG_PM */
2827