xref: /openbmc/linux/drivers/ata/libata-eh.c (revision 2695e36616c3ece5e8e30666868fc7c90dc3fc75)
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>
362855568bSJeff 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 
5625ddf24c5STejun Heo static int ata_eh_nr_in_flight(struct ata_port *ap)
5635ddf24c5STejun Heo {
5645ddf24c5STejun Heo 	unsigned int tag;
5655ddf24c5STejun Heo 	int nr = 0;
5665ddf24c5STejun Heo 
5675ddf24c5STejun Heo 	/* count only non-internal commands */
5685ddf24c5STejun Heo 	for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++)
5695ddf24c5STejun Heo 		if (ata_qc_from_tag(ap, tag))
5705ddf24c5STejun Heo 			nr++;
5715ddf24c5STejun Heo 
5725ddf24c5STejun Heo 	return nr;
5735ddf24c5STejun Heo }
5745ddf24c5STejun Heo 
5755ddf24c5STejun Heo void ata_eh_fastdrain_timerfn(unsigned long arg)
5765ddf24c5STejun Heo {
5775ddf24c5STejun Heo 	struct ata_port *ap = (void *)arg;
5785ddf24c5STejun Heo 	unsigned long flags;
5795ddf24c5STejun Heo 	int cnt;
5805ddf24c5STejun Heo 
5815ddf24c5STejun Heo 	spin_lock_irqsave(ap->lock, flags);
5825ddf24c5STejun Heo 
5835ddf24c5STejun Heo 	cnt = ata_eh_nr_in_flight(ap);
5845ddf24c5STejun Heo 
5855ddf24c5STejun Heo 	/* are we done? */
5865ddf24c5STejun Heo 	if (!cnt)
5875ddf24c5STejun Heo 		goto out_unlock;
5885ddf24c5STejun Heo 
5895ddf24c5STejun Heo 	if (cnt == ap->fastdrain_cnt) {
5905ddf24c5STejun Heo 		unsigned int tag;
5915ddf24c5STejun Heo 
5925ddf24c5STejun Heo 		/* No progress during the last interval, tag all
5935ddf24c5STejun Heo 		 * in-flight qcs as timed out and freeze the port.
5945ddf24c5STejun Heo 		 */
5955ddf24c5STejun Heo 		for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) {
5965ddf24c5STejun Heo 			struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
5975ddf24c5STejun Heo 			if (qc)
5985ddf24c5STejun Heo 				qc->err_mask |= AC_ERR_TIMEOUT;
5995ddf24c5STejun Heo 		}
6005ddf24c5STejun Heo 
6015ddf24c5STejun Heo 		ata_port_freeze(ap);
6025ddf24c5STejun Heo 	} else {
6035ddf24c5STejun Heo 		/* some qcs have finished, give it another chance */
6045ddf24c5STejun Heo 		ap->fastdrain_cnt = cnt;
6055ddf24c5STejun Heo 		ap->fastdrain_timer.expires =
6065ddf24c5STejun Heo 			jiffies + ATA_EH_FASTDRAIN_INTERVAL;
6075ddf24c5STejun Heo 		add_timer(&ap->fastdrain_timer);
6085ddf24c5STejun Heo 	}
6095ddf24c5STejun Heo 
6105ddf24c5STejun Heo  out_unlock:
6115ddf24c5STejun Heo 	spin_unlock_irqrestore(ap->lock, flags);
6125ddf24c5STejun Heo }
6135ddf24c5STejun Heo 
6145ddf24c5STejun Heo /**
6155ddf24c5STejun Heo  *	ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
6165ddf24c5STejun Heo  *	@ap: target ATA port
6175ddf24c5STejun Heo  *	@fastdrain: activate fast drain
6185ddf24c5STejun Heo  *
6195ddf24c5STejun Heo  *	Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
6205ddf24c5STejun Heo  *	is non-zero and EH wasn't pending before.  Fast drain ensures
6215ddf24c5STejun Heo  *	that EH kicks in in timely manner.
6225ddf24c5STejun Heo  *
6235ddf24c5STejun Heo  *	LOCKING:
6245ddf24c5STejun Heo  *	spin_lock_irqsave(host lock)
6255ddf24c5STejun Heo  */
6265ddf24c5STejun Heo static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
6275ddf24c5STejun Heo {
6285ddf24c5STejun Heo 	int cnt;
6295ddf24c5STejun Heo 
6305ddf24c5STejun Heo 	/* already scheduled? */
6315ddf24c5STejun Heo 	if (ap->pflags & ATA_PFLAG_EH_PENDING)
6325ddf24c5STejun Heo 		return;
6335ddf24c5STejun Heo 
6345ddf24c5STejun Heo 	ap->pflags |= ATA_PFLAG_EH_PENDING;
6355ddf24c5STejun Heo 
6365ddf24c5STejun Heo 	if (!fastdrain)
6375ddf24c5STejun Heo 		return;
6385ddf24c5STejun Heo 
6395ddf24c5STejun Heo 	/* do we have in-flight qcs? */
6405ddf24c5STejun Heo 	cnt = ata_eh_nr_in_flight(ap);
6415ddf24c5STejun Heo 	if (!cnt)
6425ddf24c5STejun Heo 		return;
6435ddf24c5STejun Heo 
6445ddf24c5STejun Heo 	/* activate fast drain */
6455ddf24c5STejun Heo 	ap->fastdrain_cnt = cnt;
6465ddf24c5STejun Heo 	ap->fastdrain_timer.expires = jiffies + ATA_EH_FASTDRAIN_INTERVAL;
6475ddf24c5STejun Heo 	add_timer(&ap->fastdrain_timer);
6485ddf24c5STejun Heo }
6495ddf24c5STejun Heo 
650c6fd2807SJeff Garzik /**
651c6fd2807SJeff Garzik  *	ata_qc_schedule_eh - schedule qc for error handling
652c6fd2807SJeff Garzik  *	@qc: command to schedule error handling for
653c6fd2807SJeff Garzik  *
654c6fd2807SJeff Garzik  *	Schedule error handling for @qc.  EH will kick in as soon as
655c6fd2807SJeff Garzik  *	other commands are drained.
656c6fd2807SJeff Garzik  *
657c6fd2807SJeff Garzik  *	LOCKING:
658cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
659c6fd2807SJeff Garzik  */
660c6fd2807SJeff Garzik void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
661c6fd2807SJeff Garzik {
662c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
663c6fd2807SJeff Garzik 
664c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
665c6fd2807SJeff Garzik 
666c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_FAILED;
6675ddf24c5STejun Heo 	ata_eh_set_pending(ap, 1);
668c6fd2807SJeff Garzik 
669c6fd2807SJeff Garzik 	/* The following will fail if timeout has already expired.
670c6fd2807SJeff Garzik 	 * ata_scsi_error() takes care of such scmds on EH entry.
671c6fd2807SJeff Garzik 	 * Note that ATA_QCFLAG_FAILED is unconditionally set after
672c6fd2807SJeff Garzik 	 * this function completes.
673c6fd2807SJeff Garzik 	 */
674c6fd2807SJeff Garzik 	scsi_req_abort_cmd(qc->scsicmd);
675c6fd2807SJeff Garzik }
676c6fd2807SJeff Garzik 
677c6fd2807SJeff Garzik /**
678c6fd2807SJeff Garzik  *	ata_port_schedule_eh - schedule error handling without a qc
679c6fd2807SJeff Garzik  *	@ap: ATA port to schedule EH for
680c6fd2807SJeff Garzik  *
681c6fd2807SJeff Garzik  *	Schedule error handling for @ap.  EH will kick in as soon as
682c6fd2807SJeff Garzik  *	all commands are drained.
683c6fd2807SJeff Garzik  *
684c6fd2807SJeff Garzik  *	LOCKING:
685cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
686c6fd2807SJeff Garzik  */
687c6fd2807SJeff Garzik void ata_port_schedule_eh(struct ata_port *ap)
688c6fd2807SJeff Garzik {
689c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
690c6fd2807SJeff Garzik 
691f4d6d004STejun Heo 	if (ap->pflags & ATA_PFLAG_INITIALIZING)
692f4d6d004STejun Heo 		return;
693f4d6d004STejun Heo 
6945ddf24c5STejun Heo 	ata_eh_set_pending(ap, 1);
695cca3974eSJeff Garzik 	scsi_schedule_eh(ap->scsi_host);
696c6fd2807SJeff Garzik 
697c6fd2807SJeff Garzik 	DPRINTK("port EH scheduled\n");
698c6fd2807SJeff Garzik }
699c6fd2807SJeff Garzik 
700dbd82616STejun Heo static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
701c6fd2807SJeff Garzik {
702c6fd2807SJeff Garzik 	int tag, nr_aborted = 0;
703c6fd2807SJeff Garzik 
704c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
705c6fd2807SJeff Garzik 
7065ddf24c5STejun Heo 	/* we're gonna abort all commands, no need for fast drain */
7075ddf24c5STejun Heo 	ata_eh_set_pending(ap, 0);
7085ddf24c5STejun Heo 
709c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
710c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
711c6fd2807SJeff Garzik 
712dbd82616STejun Heo 		if (qc && (!link || qc->dev->link == link)) {
713c6fd2807SJeff Garzik 			qc->flags |= ATA_QCFLAG_FAILED;
714c6fd2807SJeff Garzik 			ata_qc_complete(qc);
715c6fd2807SJeff Garzik 			nr_aborted++;
716c6fd2807SJeff Garzik 		}
717c6fd2807SJeff Garzik 	}
718c6fd2807SJeff Garzik 
719c6fd2807SJeff Garzik 	if (!nr_aborted)
720c6fd2807SJeff Garzik 		ata_port_schedule_eh(ap);
721c6fd2807SJeff Garzik 
722c6fd2807SJeff Garzik 	return nr_aborted;
723c6fd2807SJeff Garzik }
724c6fd2807SJeff Garzik 
725c6fd2807SJeff Garzik /**
726dbd82616STejun Heo  *	ata_link_abort - abort all qc's on the link
727dbd82616STejun Heo  *	@link: ATA link to abort qc's for
728dbd82616STejun Heo  *
729dbd82616STejun Heo  *	Abort all active qc's active on @link and schedule EH.
730dbd82616STejun Heo  *
731dbd82616STejun Heo  *	LOCKING:
732dbd82616STejun Heo  *	spin_lock_irqsave(host lock)
733dbd82616STejun Heo  *
734dbd82616STejun Heo  *	RETURNS:
735dbd82616STejun Heo  *	Number of aborted qc's.
736dbd82616STejun Heo  */
737dbd82616STejun Heo int ata_link_abort(struct ata_link *link)
738dbd82616STejun Heo {
739dbd82616STejun Heo 	return ata_do_link_abort(link->ap, link);
740dbd82616STejun Heo }
741dbd82616STejun Heo 
742dbd82616STejun Heo /**
743dbd82616STejun Heo  *	ata_port_abort - abort all qc's on the port
744dbd82616STejun Heo  *	@ap: ATA port to abort qc's for
745dbd82616STejun Heo  *
746dbd82616STejun Heo  *	Abort all active qc's of @ap and schedule EH.
747dbd82616STejun Heo  *
748dbd82616STejun Heo  *	LOCKING:
749dbd82616STejun Heo  *	spin_lock_irqsave(host_set lock)
750dbd82616STejun Heo  *
751dbd82616STejun Heo  *	RETURNS:
752dbd82616STejun Heo  *	Number of aborted qc's.
753dbd82616STejun Heo  */
754dbd82616STejun Heo int ata_port_abort(struct ata_port *ap)
755dbd82616STejun Heo {
756dbd82616STejun Heo 	return ata_do_link_abort(ap, NULL);
757dbd82616STejun Heo }
758dbd82616STejun Heo 
759dbd82616STejun Heo /**
760c6fd2807SJeff Garzik  *	__ata_port_freeze - freeze port
761c6fd2807SJeff Garzik  *	@ap: ATA port to freeze
762c6fd2807SJeff Garzik  *
763c6fd2807SJeff Garzik  *	This function is called when HSM violation or some other
764c6fd2807SJeff Garzik  *	condition disrupts normal operation of the port.  Frozen port
765c6fd2807SJeff Garzik  *	is not allowed to perform any operation until the port is
766c6fd2807SJeff Garzik  *	thawed, which usually follows a successful reset.
767c6fd2807SJeff Garzik  *
768c6fd2807SJeff Garzik  *	ap->ops->freeze() callback can be used for freezing the port
769c6fd2807SJeff Garzik  *	hardware-wise (e.g. mask interrupt and stop DMA engine).  If a
770c6fd2807SJeff Garzik  *	port cannot be frozen hardware-wise, the interrupt handler
771c6fd2807SJeff Garzik  *	must ack and clear interrupts unconditionally while the port
772c6fd2807SJeff Garzik  *	is frozen.
773c6fd2807SJeff Garzik  *
774c6fd2807SJeff Garzik  *	LOCKING:
775cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
776c6fd2807SJeff Garzik  */
777c6fd2807SJeff Garzik static void __ata_port_freeze(struct ata_port *ap)
778c6fd2807SJeff Garzik {
779c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
780c6fd2807SJeff Garzik 
781c6fd2807SJeff Garzik 	if (ap->ops->freeze)
782c6fd2807SJeff Garzik 		ap->ops->freeze(ap);
783c6fd2807SJeff Garzik 
784c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_FROZEN;
785c6fd2807SJeff Garzik 
78644877b4eSTejun Heo 	DPRINTK("ata%u port frozen\n", ap->print_id);
787c6fd2807SJeff Garzik }
788c6fd2807SJeff Garzik 
789c6fd2807SJeff Garzik /**
790c6fd2807SJeff Garzik  *	ata_port_freeze - abort & freeze port
791c6fd2807SJeff Garzik  *	@ap: ATA port to freeze
792c6fd2807SJeff Garzik  *
793c6fd2807SJeff Garzik  *	Abort and freeze @ap.
794c6fd2807SJeff Garzik  *
795c6fd2807SJeff Garzik  *	LOCKING:
796cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
797c6fd2807SJeff Garzik  *
798c6fd2807SJeff Garzik  *	RETURNS:
799c6fd2807SJeff Garzik  *	Number of aborted commands.
800c6fd2807SJeff Garzik  */
801c6fd2807SJeff Garzik int ata_port_freeze(struct ata_port *ap)
802c6fd2807SJeff Garzik {
803c6fd2807SJeff Garzik 	int nr_aborted;
804c6fd2807SJeff Garzik 
805c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
806c6fd2807SJeff Garzik 
807c6fd2807SJeff Garzik 	nr_aborted = ata_port_abort(ap);
808c6fd2807SJeff Garzik 	__ata_port_freeze(ap);
809c6fd2807SJeff Garzik 
810c6fd2807SJeff Garzik 	return nr_aborted;
811c6fd2807SJeff Garzik }
812c6fd2807SJeff Garzik 
813c6fd2807SJeff Garzik /**
8147d77b247STejun Heo  *	sata_async_notification - SATA async notification handler
8157d77b247STejun Heo  *	@ap: ATA port where async notification is received
8167d77b247STejun Heo  *
8177d77b247STejun Heo  *	Handler to be called when async notification via SDB FIS is
8187d77b247STejun Heo  *	received.  This function schedules EH if necessary.
8197d77b247STejun Heo  *
8207d77b247STejun Heo  *	LOCKING:
8217d77b247STejun Heo  *	spin_lock_irqsave(host lock)
8227d77b247STejun Heo  *
8237d77b247STejun Heo  *	RETURNS:
8247d77b247STejun Heo  *	1 if EH is scheduled, 0 otherwise.
8257d77b247STejun Heo  */
8267d77b247STejun Heo int sata_async_notification(struct ata_port *ap)
8277d77b247STejun Heo {
8287d77b247STejun Heo 	u32 sntf;
8297d77b247STejun Heo 	int rc;
8307d77b247STejun Heo 
8317d77b247STejun Heo 	if (!(ap->flags & ATA_FLAG_AN))
8327d77b247STejun Heo 		return 0;
8337d77b247STejun Heo 
8347d77b247STejun Heo 	rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
8357d77b247STejun Heo 	if (rc == 0)
8367d77b247STejun Heo 		sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
8377d77b247STejun Heo 
8387d77b247STejun Heo 	if (!ap->nr_pmp_links || rc) {
8397d77b247STejun Heo 		/* PMP is not attached or SNTF is not available */
8407d77b247STejun Heo 		if (!ap->nr_pmp_links) {
8417d77b247STejun Heo 			/* PMP is not attached.  Check whether ATAPI
8427d77b247STejun Heo 			 * AN is configured.  If so, notify media
8437d77b247STejun Heo 			 * change.
8447d77b247STejun Heo 			 */
8457d77b247STejun Heo 			struct ata_device *dev = ap->link.device;
8467d77b247STejun Heo 
8477d77b247STejun Heo 			if ((dev->class == ATA_DEV_ATAPI) &&
8487d77b247STejun Heo 			    (dev->flags & ATA_DFLAG_AN))
8497d77b247STejun Heo 				ata_scsi_media_change_notify(dev);
8507d77b247STejun Heo 			return 0;
8517d77b247STejun Heo 		} else {
8527d77b247STejun Heo 			/* PMP is attached but SNTF is not available.
8537d77b247STejun Heo 			 * ATAPI async media change notification is
8547d77b247STejun Heo 			 * not used.  The PMP must be reporting PHY
8557d77b247STejun Heo 			 * status change, schedule EH.
8567d77b247STejun Heo 			 */
8577d77b247STejun Heo 			ata_port_schedule_eh(ap);
8587d77b247STejun Heo 			return 1;
8597d77b247STejun Heo 		}
8607d77b247STejun Heo 	} else {
8617d77b247STejun Heo 		/* PMP is attached and SNTF is available */
8627d77b247STejun Heo 		struct ata_link *link;
8637d77b247STejun Heo 
8647d77b247STejun Heo 		/* check and notify ATAPI AN */
8657d77b247STejun Heo 		ata_port_for_each_link(link, ap) {
8667d77b247STejun Heo 			if (!(sntf & (1 << link->pmp)))
8677d77b247STejun Heo 				continue;
8687d77b247STejun Heo 
8697d77b247STejun Heo 			if ((link->device->class == ATA_DEV_ATAPI) &&
8707d77b247STejun Heo 			    (link->device->flags & ATA_DFLAG_AN))
8717d77b247STejun Heo 				ata_scsi_media_change_notify(link->device);
8727d77b247STejun Heo 		}
8737d77b247STejun Heo 
8747d77b247STejun Heo 		/* If PMP is reporting that PHY status of some
8757d77b247STejun Heo 		 * downstream ports has changed, schedule EH.
8767d77b247STejun Heo 		 */
8777d77b247STejun Heo 		if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
8787d77b247STejun Heo 			ata_port_schedule_eh(ap);
8797d77b247STejun Heo 			return 1;
8807d77b247STejun Heo 		}
8817d77b247STejun Heo 
8827d77b247STejun Heo 		return 0;
8837d77b247STejun Heo 	}
8847d77b247STejun Heo }
8857d77b247STejun Heo 
8867d77b247STejun Heo /**
887c6fd2807SJeff Garzik  *	ata_eh_freeze_port - EH helper to freeze port
888c6fd2807SJeff Garzik  *	@ap: ATA port to freeze
889c6fd2807SJeff Garzik  *
890c6fd2807SJeff Garzik  *	Freeze @ap.
891c6fd2807SJeff Garzik  *
892c6fd2807SJeff Garzik  *	LOCKING:
893c6fd2807SJeff Garzik  *	None.
894c6fd2807SJeff Garzik  */
895c6fd2807SJeff Garzik void ata_eh_freeze_port(struct ata_port *ap)
896c6fd2807SJeff Garzik {
897c6fd2807SJeff Garzik 	unsigned long flags;
898c6fd2807SJeff Garzik 
899c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
900c6fd2807SJeff Garzik 		return;
901c6fd2807SJeff Garzik 
902c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
903c6fd2807SJeff Garzik 	__ata_port_freeze(ap);
904c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
905c6fd2807SJeff Garzik }
906c6fd2807SJeff Garzik 
907c6fd2807SJeff Garzik /**
908c6fd2807SJeff Garzik  *	ata_port_thaw_port - EH helper to thaw port
909c6fd2807SJeff Garzik  *	@ap: ATA port to thaw
910c6fd2807SJeff Garzik  *
911c6fd2807SJeff Garzik  *	Thaw frozen port @ap.
912c6fd2807SJeff Garzik  *
913c6fd2807SJeff Garzik  *	LOCKING:
914c6fd2807SJeff Garzik  *	None.
915c6fd2807SJeff Garzik  */
916c6fd2807SJeff Garzik void ata_eh_thaw_port(struct ata_port *ap)
917c6fd2807SJeff Garzik {
918c6fd2807SJeff Garzik 	unsigned long flags;
919c6fd2807SJeff Garzik 
920c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
921c6fd2807SJeff Garzik 		return;
922c6fd2807SJeff Garzik 
923c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
924c6fd2807SJeff Garzik 
925c6fd2807SJeff Garzik 	ap->pflags &= ~ATA_PFLAG_FROZEN;
926c6fd2807SJeff Garzik 
927c6fd2807SJeff Garzik 	if (ap->ops->thaw)
928c6fd2807SJeff Garzik 		ap->ops->thaw(ap);
929c6fd2807SJeff Garzik 
930c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
931c6fd2807SJeff Garzik 
93244877b4eSTejun Heo 	DPRINTK("ata%u port thawed\n", ap->print_id);
933c6fd2807SJeff Garzik }
934c6fd2807SJeff Garzik 
935c6fd2807SJeff Garzik static void ata_eh_scsidone(struct scsi_cmnd *scmd)
936c6fd2807SJeff Garzik {
937c6fd2807SJeff Garzik 	/* nada */
938c6fd2807SJeff Garzik }
939c6fd2807SJeff Garzik 
940c6fd2807SJeff Garzik static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
941c6fd2807SJeff Garzik {
942c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
943c6fd2807SJeff Garzik 	struct scsi_cmnd *scmd = qc->scsicmd;
944c6fd2807SJeff Garzik 	unsigned long flags;
945c6fd2807SJeff Garzik 
946c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
947c6fd2807SJeff Garzik 	qc->scsidone = ata_eh_scsidone;
948c6fd2807SJeff Garzik 	__ata_qc_complete(qc);
949c6fd2807SJeff Garzik 	WARN_ON(ata_tag_valid(qc->tag));
950c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
951c6fd2807SJeff Garzik 
952c6fd2807SJeff Garzik 	scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
953c6fd2807SJeff Garzik }
954c6fd2807SJeff Garzik 
955c6fd2807SJeff Garzik /**
956c6fd2807SJeff Garzik  *	ata_eh_qc_complete - Complete an active ATA command from EH
957c6fd2807SJeff Garzik  *	@qc: Command to complete
958c6fd2807SJeff Garzik  *
959c6fd2807SJeff Garzik  *	Indicate to the mid and upper layers that an ATA command has
960c6fd2807SJeff Garzik  *	completed.  To be used from EH.
961c6fd2807SJeff Garzik  */
962c6fd2807SJeff Garzik void ata_eh_qc_complete(struct ata_queued_cmd *qc)
963c6fd2807SJeff Garzik {
964c6fd2807SJeff Garzik 	struct scsi_cmnd *scmd = qc->scsicmd;
965c6fd2807SJeff Garzik 	scmd->retries = scmd->allowed;
966c6fd2807SJeff Garzik 	__ata_eh_qc_complete(qc);
967c6fd2807SJeff Garzik }
968c6fd2807SJeff Garzik 
969c6fd2807SJeff Garzik /**
970c6fd2807SJeff Garzik  *	ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
971c6fd2807SJeff Garzik  *	@qc: Command to retry
972c6fd2807SJeff Garzik  *
973c6fd2807SJeff Garzik  *	Indicate to the mid and upper layers that an ATA command
974c6fd2807SJeff Garzik  *	should be retried.  To be used from EH.
975c6fd2807SJeff Garzik  *
976c6fd2807SJeff Garzik  *	SCSI midlayer limits the number of retries to scmd->allowed.
977c6fd2807SJeff Garzik  *	scmd->retries is decremented for commands which get retried
978c6fd2807SJeff Garzik  *	due to unrelated failures (qc->err_mask is zero).
979c6fd2807SJeff Garzik  */
980c6fd2807SJeff Garzik void ata_eh_qc_retry(struct ata_queued_cmd *qc)
981c6fd2807SJeff Garzik {
982c6fd2807SJeff Garzik 	struct scsi_cmnd *scmd = qc->scsicmd;
983c6fd2807SJeff Garzik 	if (!qc->err_mask && scmd->retries)
984c6fd2807SJeff Garzik 		scmd->retries--;
985c6fd2807SJeff Garzik 	__ata_eh_qc_complete(qc);
986c6fd2807SJeff Garzik }
987c6fd2807SJeff Garzik 
988c6fd2807SJeff Garzik /**
989c6fd2807SJeff Garzik  *	ata_eh_detach_dev - detach ATA device
990c6fd2807SJeff Garzik  *	@dev: ATA device to detach
991c6fd2807SJeff Garzik  *
992c6fd2807SJeff Garzik  *	Detach @dev.
993c6fd2807SJeff Garzik  *
994c6fd2807SJeff Garzik  *	LOCKING:
995c6fd2807SJeff Garzik  *	None.
996c6fd2807SJeff Garzik  */
997fb7fd614STejun Heo void ata_eh_detach_dev(struct ata_device *dev)
998c6fd2807SJeff Garzik {
999f58229f8STejun Heo 	struct ata_link *link = dev->link;
1000f58229f8STejun Heo 	struct ata_port *ap = link->ap;
1001c6fd2807SJeff Garzik 	unsigned long flags;
1002c6fd2807SJeff Garzik 
1003c6fd2807SJeff Garzik 	ata_dev_disable(dev);
1004c6fd2807SJeff Garzik 
1005c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1006c6fd2807SJeff Garzik 
1007c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_DETACH;
1008c6fd2807SJeff Garzik 
1009c6fd2807SJeff Garzik 	if (ata_scsi_offline_dev(dev)) {
1010c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_DETACHED;
1011c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
1012c6fd2807SJeff Garzik 	}
1013c6fd2807SJeff Garzik 
1014c6fd2807SJeff Garzik 	/* clear per-dev EH actions */
1015f58229f8STejun Heo 	ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
1016f58229f8STejun Heo 	ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
1017c6fd2807SJeff Garzik 
1018c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1019c6fd2807SJeff Garzik }
1020c6fd2807SJeff Garzik 
1021c6fd2807SJeff Garzik /**
1022c6fd2807SJeff Garzik  *	ata_eh_about_to_do - about to perform eh_action
1023955e57dfSTejun Heo  *	@link: target ATA link
1024c6fd2807SJeff Garzik  *	@dev: target ATA dev for per-dev action (can be NULL)
1025c6fd2807SJeff Garzik  *	@action: action about to be performed
1026c6fd2807SJeff Garzik  *
1027c6fd2807SJeff Garzik  *	Called just before performing EH actions to clear related bits
1028955e57dfSTejun Heo  *	in @link->eh_info such that eh actions are not unnecessarily
1029955e57dfSTejun Heo  *	repeated.
1030c6fd2807SJeff Garzik  *
1031c6fd2807SJeff Garzik  *	LOCKING:
1032c6fd2807SJeff Garzik  *	None.
1033c6fd2807SJeff Garzik  */
1034fb7fd614STejun Heo void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
1035c6fd2807SJeff Garzik 			unsigned int action)
1036c6fd2807SJeff Garzik {
1037955e57dfSTejun Heo 	struct ata_port *ap = link->ap;
1038955e57dfSTejun Heo 	struct ata_eh_info *ehi = &link->eh_info;
1039955e57dfSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
1040c6fd2807SJeff Garzik 	unsigned long flags;
1041c6fd2807SJeff Garzik 
1042c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1043c6fd2807SJeff Garzik 
1044c6fd2807SJeff Garzik 	/* Reset is represented by combination of actions and EHI
1045c6fd2807SJeff Garzik 	 * flags.  Suck in all related bits before clearing eh_info to
1046c6fd2807SJeff Garzik 	 * avoid losing requested action.
1047c6fd2807SJeff Garzik 	 */
1048c6fd2807SJeff Garzik 	if (action & ATA_EH_RESET_MASK) {
1049c6fd2807SJeff Garzik 		ehc->i.action |= ehi->action & ATA_EH_RESET_MASK;
1050c6fd2807SJeff Garzik 		ehc->i.flags |= ehi->flags & ATA_EHI_RESET_MODIFIER_MASK;
1051c6fd2807SJeff Garzik 
1052c6fd2807SJeff Garzik 		/* make sure all reset actions are cleared & clear EHI flags */
1053c6fd2807SJeff Garzik 		action |= ATA_EH_RESET_MASK;
1054c6fd2807SJeff Garzik 		ehi->flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
1055c6fd2807SJeff Garzik 	}
1056c6fd2807SJeff Garzik 
1057955e57dfSTejun Heo 	ata_eh_clear_action(link, dev, ehi, action);
1058c6fd2807SJeff Garzik 
1059c6fd2807SJeff Garzik 	if (!(ehc->i.flags & ATA_EHI_QUIET))
1060c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_RECOVERED;
1061c6fd2807SJeff Garzik 
1062c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1063c6fd2807SJeff Garzik }
1064c6fd2807SJeff Garzik 
1065c6fd2807SJeff Garzik /**
1066c6fd2807SJeff Garzik  *	ata_eh_done - EH action complete
1067c6fd2807SJeff Garzik *	@ap: target ATA port
1068c6fd2807SJeff Garzik  *	@dev: target ATA dev for per-dev action (can be NULL)
1069c6fd2807SJeff Garzik  *	@action: action just completed
1070c6fd2807SJeff Garzik  *
1071c6fd2807SJeff Garzik  *	Called right after performing EH actions to clear related bits
1072955e57dfSTejun Heo  *	in @link->eh_context.
1073c6fd2807SJeff Garzik  *
1074c6fd2807SJeff Garzik  *	LOCKING:
1075c6fd2807SJeff Garzik  *	None.
1076c6fd2807SJeff Garzik  */
1077fb7fd614STejun Heo void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1078c6fd2807SJeff Garzik 		 unsigned int action)
1079c6fd2807SJeff Garzik {
1080955e57dfSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
10819af5c9c9STejun Heo 
1082c6fd2807SJeff Garzik 	/* if reset is complete, clear all reset actions & reset modifier */
1083c6fd2807SJeff Garzik 	if (action & ATA_EH_RESET_MASK) {
1084c6fd2807SJeff Garzik 		action |= ATA_EH_RESET_MASK;
10859af5c9c9STejun Heo 		ehc->i.flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
1086c6fd2807SJeff Garzik 	}
1087c6fd2807SJeff Garzik 
1088955e57dfSTejun Heo 	ata_eh_clear_action(link, dev, &ehc->i, action);
1089c6fd2807SJeff Garzik }
1090c6fd2807SJeff Garzik 
1091c6fd2807SJeff Garzik /**
1092c6fd2807SJeff Garzik  *	ata_err_string - convert err_mask to descriptive string
1093c6fd2807SJeff Garzik  *	@err_mask: error mask to convert to string
1094c6fd2807SJeff Garzik  *
1095c6fd2807SJeff Garzik  *	Convert @err_mask to descriptive string.  Errors are
1096c6fd2807SJeff Garzik  *	prioritized according to severity and only the most severe
1097c6fd2807SJeff Garzik  *	error is reported.
1098c6fd2807SJeff Garzik  *
1099c6fd2807SJeff Garzik  *	LOCKING:
1100c6fd2807SJeff Garzik  *	None.
1101c6fd2807SJeff Garzik  *
1102c6fd2807SJeff Garzik  *	RETURNS:
1103c6fd2807SJeff Garzik  *	Descriptive string for @err_mask
1104c6fd2807SJeff Garzik  */
1105c6fd2807SJeff Garzik static const char *ata_err_string(unsigned int err_mask)
1106c6fd2807SJeff Garzik {
1107c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_HOST_BUS)
1108c6fd2807SJeff Garzik 		return "host bus error";
1109c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_ATA_BUS)
1110c6fd2807SJeff Garzik 		return "ATA bus error";
1111c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_TIMEOUT)
1112c6fd2807SJeff Garzik 		return "timeout";
1113c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_HSM)
1114c6fd2807SJeff Garzik 		return "HSM violation";
1115c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_SYSTEM)
1116c6fd2807SJeff Garzik 		return "internal error";
1117c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_MEDIA)
1118c6fd2807SJeff Garzik 		return "media error";
1119c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_INVALID)
1120c6fd2807SJeff Garzik 		return "invalid argument";
1121c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_DEV)
1122c6fd2807SJeff Garzik 		return "device error";
1123c6fd2807SJeff Garzik 	return "unknown error";
1124c6fd2807SJeff Garzik }
1125c6fd2807SJeff Garzik 
1126c6fd2807SJeff Garzik /**
1127c6fd2807SJeff Garzik  *	ata_read_log_page - read a specific log page
1128c6fd2807SJeff Garzik  *	@dev: target device
1129c6fd2807SJeff Garzik  *	@page: page to read
1130c6fd2807SJeff Garzik  *	@buf: buffer to store read page
1131c6fd2807SJeff Garzik  *	@sectors: number of sectors to read
1132c6fd2807SJeff Garzik  *
1133c6fd2807SJeff Garzik  *	Read log page using READ_LOG_EXT command.
1134c6fd2807SJeff Garzik  *
1135c6fd2807SJeff Garzik  *	LOCKING:
1136c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1137c6fd2807SJeff Garzik  *
1138c6fd2807SJeff Garzik  *	RETURNS:
1139c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
1140c6fd2807SJeff Garzik  */
1141c6fd2807SJeff Garzik static unsigned int ata_read_log_page(struct ata_device *dev,
1142c6fd2807SJeff Garzik 				      u8 page, void *buf, unsigned int sectors)
1143c6fd2807SJeff Garzik {
1144c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1145c6fd2807SJeff Garzik 	unsigned int err_mask;
1146c6fd2807SJeff Garzik 
1147c6fd2807SJeff Garzik 	DPRINTK("read log page - page %d\n", page);
1148c6fd2807SJeff Garzik 
1149c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
1150c6fd2807SJeff Garzik 	tf.command = ATA_CMD_READ_LOG_EXT;
1151c6fd2807SJeff Garzik 	tf.lbal = page;
1152c6fd2807SJeff Garzik 	tf.nsect = sectors;
1153c6fd2807SJeff Garzik 	tf.hob_nsect = sectors >> 8;
1154c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1155c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_PIO;
1156c6fd2807SJeff Garzik 
1157c6fd2807SJeff Garzik 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
11582b789108STejun Heo 				     buf, sectors * ATA_SECT_SIZE, 0);
1159c6fd2807SJeff Garzik 
1160c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
1161c6fd2807SJeff Garzik 	return err_mask;
1162c6fd2807SJeff Garzik }
1163c6fd2807SJeff Garzik 
1164c6fd2807SJeff Garzik /**
1165c6fd2807SJeff Garzik  *	ata_eh_read_log_10h - Read log page 10h for NCQ error details
1166c6fd2807SJeff Garzik  *	@dev: Device to read log page 10h from
1167c6fd2807SJeff Garzik  *	@tag: Resulting tag of the failed command
1168c6fd2807SJeff Garzik  *	@tf: Resulting taskfile registers of the failed command
1169c6fd2807SJeff Garzik  *
1170c6fd2807SJeff Garzik  *	Read log page 10h to obtain NCQ error details and clear error
1171c6fd2807SJeff Garzik  *	condition.
1172c6fd2807SJeff Garzik  *
1173c6fd2807SJeff Garzik  *	LOCKING:
1174c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1175c6fd2807SJeff Garzik  *
1176c6fd2807SJeff Garzik  *	RETURNS:
1177c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
1178c6fd2807SJeff Garzik  */
1179c6fd2807SJeff Garzik static int ata_eh_read_log_10h(struct ata_device *dev,
1180c6fd2807SJeff Garzik 			       int *tag, struct ata_taskfile *tf)
1181c6fd2807SJeff Garzik {
11829af5c9c9STejun Heo 	u8 *buf = dev->link->ap->sector_buf;
1183c6fd2807SJeff Garzik 	unsigned int err_mask;
1184c6fd2807SJeff Garzik 	u8 csum;
1185c6fd2807SJeff Garzik 	int i;
1186c6fd2807SJeff Garzik 
1187c6fd2807SJeff Garzik 	err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
1188c6fd2807SJeff Garzik 	if (err_mask)
1189c6fd2807SJeff Garzik 		return -EIO;
1190c6fd2807SJeff Garzik 
1191c6fd2807SJeff Garzik 	csum = 0;
1192c6fd2807SJeff Garzik 	for (i = 0; i < ATA_SECT_SIZE; i++)
1193c6fd2807SJeff Garzik 		csum += buf[i];
1194c6fd2807SJeff Garzik 	if (csum)
1195c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_WARNING,
1196c6fd2807SJeff Garzik 			       "invalid checksum 0x%x on log page 10h\n", csum);
1197c6fd2807SJeff Garzik 
1198c6fd2807SJeff Garzik 	if (buf[0] & 0x80)
1199c6fd2807SJeff Garzik 		return -ENOENT;
1200c6fd2807SJeff Garzik 
1201c6fd2807SJeff Garzik 	*tag = buf[0] & 0x1f;
1202c6fd2807SJeff Garzik 
1203c6fd2807SJeff Garzik 	tf->command = buf[2];
1204c6fd2807SJeff Garzik 	tf->feature = buf[3];
1205c6fd2807SJeff Garzik 	tf->lbal = buf[4];
1206c6fd2807SJeff Garzik 	tf->lbam = buf[5];
1207c6fd2807SJeff Garzik 	tf->lbah = buf[6];
1208c6fd2807SJeff Garzik 	tf->device = buf[7];
1209c6fd2807SJeff Garzik 	tf->hob_lbal = buf[8];
1210c6fd2807SJeff Garzik 	tf->hob_lbam = buf[9];
1211c6fd2807SJeff Garzik 	tf->hob_lbah = buf[10];
1212c6fd2807SJeff Garzik 	tf->nsect = buf[12];
1213c6fd2807SJeff Garzik 	tf->hob_nsect = buf[13];
1214c6fd2807SJeff Garzik 
1215c6fd2807SJeff Garzik 	return 0;
1216c6fd2807SJeff Garzik }
1217c6fd2807SJeff Garzik 
1218c6fd2807SJeff Garzik /**
1219c6fd2807SJeff Garzik  *	atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1220c6fd2807SJeff Garzik  *	@dev: device to perform REQUEST_SENSE to
1221c6fd2807SJeff Garzik  *	@sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
1222c6fd2807SJeff Garzik  *
1223c6fd2807SJeff Garzik  *	Perform ATAPI REQUEST_SENSE after the device reported CHECK
1224c6fd2807SJeff Garzik  *	SENSE.  This function is EH helper.
1225c6fd2807SJeff Garzik  *
1226c6fd2807SJeff Garzik  *	LOCKING:
1227c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1228c6fd2807SJeff Garzik  *
1229c6fd2807SJeff Garzik  *	RETURNS:
1230c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask on failure
1231c6fd2807SJeff Garzik  */
123256287768SAlbert Lee static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc)
1233c6fd2807SJeff Garzik {
123456287768SAlbert Lee 	struct ata_device *dev = qc->dev;
123556287768SAlbert Lee 	unsigned char *sense_buf = qc->scsicmd->sense_buffer;
12369af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
1237c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1238c6fd2807SJeff Garzik 	u8 cdb[ATAPI_CDB_LEN];
1239c6fd2807SJeff Garzik 
1240c6fd2807SJeff Garzik 	DPRINTK("ATAPI request sense\n");
1241c6fd2807SJeff Garzik 
1242c6fd2807SJeff Garzik 	/* FIXME: is this needed? */
1243c6fd2807SJeff Garzik 	memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1244c6fd2807SJeff Garzik 
124556287768SAlbert Lee 	/* initialize sense_buf with the error register,
124656287768SAlbert Lee 	 * for the case where they are -not- overwritten
124756287768SAlbert Lee 	 */
1248c6fd2807SJeff Garzik 	sense_buf[0] = 0x70;
124956287768SAlbert Lee 	sense_buf[2] = qc->result_tf.feature >> 4;
125056287768SAlbert Lee 
125156287768SAlbert Lee 	/* some devices time out if garbage left in tf */
125256287768SAlbert Lee 	ata_tf_init(dev, &tf);
1253c6fd2807SJeff Garzik 
1254c6fd2807SJeff Garzik 	memset(cdb, 0, ATAPI_CDB_LEN);
1255c6fd2807SJeff Garzik 	cdb[0] = REQUEST_SENSE;
1256c6fd2807SJeff Garzik 	cdb[4] = SCSI_SENSE_BUFFERSIZE;
1257c6fd2807SJeff Garzik 
1258c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1259c6fd2807SJeff Garzik 	tf.command = ATA_CMD_PACKET;
1260c6fd2807SJeff Garzik 
1261c6fd2807SJeff Garzik 	/* is it pointless to prefer PIO for "safety reasons"? */
1262c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_PIO_DMA) {
1263c6fd2807SJeff Garzik 		tf.protocol = ATA_PROT_ATAPI_DMA;
1264c6fd2807SJeff Garzik 		tf.feature |= ATAPI_PKT_DMA;
1265c6fd2807SJeff Garzik 	} else {
1266c6fd2807SJeff Garzik 		tf.protocol = ATA_PROT_ATAPI;
1267f2dfc1a1STejun Heo 		tf.lbam = SCSI_SENSE_BUFFERSIZE;
1268f2dfc1a1STejun Heo 		tf.lbah = 0;
1269c6fd2807SJeff Garzik 	}
1270c6fd2807SJeff Garzik 
1271c6fd2807SJeff Garzik 	return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
12722b789108STejun Heo 				 sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
1273c6fd2807SJeff Garzik }
1274c6fd2807SJeff Garzik 
1275c6fd2807SJeff Garzik /**
1276c6fd2807SJeff Garzik  *	ata_eh_analyze_serror - analyze SError for a failed port
12770260731fSTejun Heo  *	@link: ATA link to analyze SError for
1278c6fd2807SJeff Garzik  *
1279c6fd2807SJeff Garzik  *	Analyze SError if available and further determine cause of
1280c6fd2807SJeff Garzik  *	failure.
1281c6fd2807SJeff Garzik  *
1282c6fd2807SJeff Garzik  *	LOCKING:
1283c6fd2807SJeff Garzik  *	None.
1284c6fd2807SJeff Garzik  */
12850260731fSTejun Heo static void ata_eh_analyze_serror(struct ata_link *link)
1286c6fd2807SJeff Garzik {
12870260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
1288c6fd2807SJeff Garzik 	u32 serror = ehc->i.serror;
1289c6fd2807SJeff Garzik 	unsigned int err_mask = 0, action = 0;
1290f9df58cbSTejun Heo 	u32 hotplug_mask;
1291c6fd2807SJeff Garzik 
1292c6fd2807SJeff Garzik 	if (serror & SERR_PERSISTENT) {
1293c6fd2807SJeff Garzik 		err_mask |= AC_ERR_ATA_BUS;
1294c6fd2807SJeff Garzik 		action |= ATA_EH_HARDRESET;
1295c6fd2807SJeff Garzik 	}
1296c6fd2807SJeff Garzik 	if (serror &
1297c6fd2807SJeff Garzik 	    (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
1298c6fd2807SJeff Garzik 		err_mask |= AC_ERR_ATA_BUS;
1299c6fd2807SJeff Garzik 		action |= ATA_EH_SOFTRESET;
1300c6fd2807SJeff Garzik 	}
1301c6fd2807SJeff Garzik 	if (serror & SERR_PROTOCOL) {
1302c6fd2807SJeff Garzik 		err_mask |= AC_ERR_HSM;
1303c6fd2807SJeff Garzik 		action |= ATA_EH_SOFTRESET;
1304c6fd2807SJeff Garzik 	}
1305c6fd2807SJeff Garzik 	if (serror & SERR_INTERNAL) {
1306c6fd2807SJeff Garzik 		err_mask |= AC_ERR_SYSTEM;
1307771b8dadSTejun Heo 		action |= ATA_EH_HARDRESET;
1308c6fd2807SJeff Garzik 	}
1309f9df58cbSTejun Heo 
1310f9df58cbSTejun Heo 	/* Determine whether a hotplug event has occurred.  Both
1311f9df58cbSTejun Heo 	 * SError.N/X are considered hotplug events for enabled or
1312f9df58cbSTejun Heo 	 * host links.  For disabled PMP links, only N bit is
1313f9df58cbSTejun Heo 	 * considered as X bit is left at 1 for link plugging.
1314f9df58cbSTejun Heo 	 */
1315f9df58cbSTejun Heo 	hotplug_mask = 0;
1316f9df58cbSTejun Heo 
1317f9df58cbSTejun Heo 	if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
1318f9df58cbSTejun Heo 		hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
1319f9df58cbSTejun Heo 	else
1320f9df58cbSTejun Heo 		hotplug_mask = SERR_PHYRDY_CHG;
1321f9df58cbSTejun Heo 
1322f9df58cbSTejun Heo 	if (serror & hotplug_mask)
1323c6fd2807SJeff Garzik 		ata_ehi_hotplugged(&ehc->i);
1324c6fd2807SJeff Garzik 
1325c6fd2807SJeff Garzik 	ehc->i.err_mask |= err_mask;
1326c6fd2807SJeff Garzik 	ehc->i.action |= action;
1327c6fd2807SJeff Garzik }
1328c6fd2807SJeff Garzik 
1329c6fd2807SJeff Garzik /**
1330c6fd2807SJeff Garzik  *	ata_eh_analyze_ncq_error - analyze NCQ error
13310260731fSTejun Heo  *	@link: ATA link to analyze NCQ error for
1332c6fd2807SJeff Garzik  *
1333c6fd2807SJeff Garzik  *	Read log page 10h, determine the offending qc and acquire
1334c6fd2807SJeff Garzik  *	error status TF.  For NCQ device errors, all LLDDs have to do
1335c6fd2807SJeff Garzik  *	is setting AC_ERR_DEV in ehi->err_mask.  This function takes
1336c6fd2807SJeff Garzik  *	care of the rest.
1337c6fd2807SJeff Garzik  *
1338c6fd2807SJeff Garzik  *	LOCKING:
1339c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1340c6fd2807SJeff Garzik  */
13410260731fSTejun Heo static void ata_eh_analyze_ncq_error(struct ata_link *link)
1342c6fd2807SJeff Garzik {
13430260731fSTejun Heo 	struct ata_port *ap = link->ap;
13440260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
13450260731fSTejun Heo 	struct ata_device *dev = link->device;
1346c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
1347c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1348c6fd2807SJeff Garzik 	int tag, rc;
1349c6fd2807SJeff Garzik 
1350c6fd2807SJeff Garzik 	/* if frozen, we can't do much */
1351c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN)
1352c6fd2807SJeff Garzik 		return;
1353c6fd2807SJeff Garzik 
1354c6fd2807SJeff Garzik 	/* is it NCQ device error? */
13550260731fSTejun Heo 	if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1356c6fd2807SJeff Garzik 		return;
1357c6fd2807SJeff Garzik 
1358c6fd2807SJeff Garzik 	/* has LLDD analyzed already? */
1359c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1360c6fd2807SJeff Garzik 		qc = __ata_qc_from_tag(ap, tag);
1361c6fd2807SJeff Garzik 
1362c6fd2807SJeff Garzik 		if (!(qc->flags & ATA_QCFLAG_FAILED))
1363c6fd2807SJeff Garzik 			continue;
1364c6fd2807SJeff Garzik 
1365c6fd2807SJeff Garzik 		if (qc->err_mask)
1366c6fd2807SJeff Garzik 			return;
1367c6fd2807SJeff Garzik 	}
1368c6fd2807SJeff Garzik 
1369c6fd2807SJeff Garzik 	/* okay, this error is ours */
1370c6fd2807SJeff Garzik 	rc = ata_eh_read_log_10h(dev, &tag, &tf);
1371c6fd2807SJeff Garzik 	if (rc) {
13720260731fSTejun Heo 		ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
1373c6fd2807SJeff Garzik 				"(errno=%d)\n", rc);
1374c6fd2807SJeff Garzik 		return;
1375c6fd2807SJeff Garzik 	}
1376c6fd2807SJeff Garzik 
13770260731fSTejun Heo 	if (!(link->sactive & (1 << tag))) {
13780260731fSTejun Heo 		ata_link_printk(link, KERN_ERR, "log page 10h reported "
1379c6fd2807SJeff Garzik 				"inactive tag %d\n", tag);
1380c6fd2807SJeff Garzik 		return;
1381c6fd2807SJeff Garzik 	}
1382c6fd2807SJeff Garzik 
1383c6fd2807SJeff Garzik 	/* we've got the perpetrator, condemn it */
1384c6fd2807SJeff Garzik 	qc = __ata_qc_from_tag(ap, tag);
1385c6fd2807SJeff Garzik 	memcpy(&qc->result_tf, &tf, sizeof(tf));
13865335b729STejun Heo 	qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
1387c6fd2807SJeff Garzik 	ehc->i.err_mask &= ~AC_ERR_DEV;
1388c6fd2807SJeff Garzik }
1389c6fd2807SJeff Garzik 
1390c6fd2807SJeff Garzik /**
1391c6fd2807SJeff Garzik  *	ata_eh_analyze_tf - analyze taskfile of a failed qc
1392c6fd2807SJeff Garzik  *	@qc: qc to analyze
1393c6fd2807SJeff Garzik  *	@tf: Taskfile registers to analyze
1394c6fd2807SJeff Garzik  *
1395c6fd2807SJeff Garzik  *	Analyze taskfile of @qc and further determine cause of
1396c6fd2807SJeff Garzik  *	failure.  This function also requests ATAPI sense data if
1397c6fd2807SJeff Garzik  *	avaliable.
1398c6fd2807SJeff Garzik  *
1399c6fd2807SJeff Garzik  *	LOCKING:
1400c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1401c6fd2807SJeff Garzik  *
1402c6fd2807SJeff Garzik  *	RETURNS:
1403c6fd2807SJeff Garzik  *	Determined recovery action
1404c6fd2807SJeff Garzik  */
1405c6fd2807SJeff Garzik static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1406c6fd2807SJeff Garzik 				      const struct ata_taskfile *tf)
1407c6fd2807SJeff Garzik {
1408c6fd2807SJeff Garzik 	unsigned int tmp, action = 0;
1409c6fd2807SJeff Garzik 	u8 stat = tf->command, err = tf->feature;
1410c6fd2807SJeff Garzik 
1411c6fd2807SJeff Garzik 	if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1412c6fd2807SJeff Garzik 		qc->err_mask |= AC_ERR_HSM;
1413c6fd2807SJeff Garzik 		return ATA_EH_SOFTRESET;
1414c6fd2807SJeff Garzik 	}
1415c6fd2807SJeff Garzik 
1416a51d644aSTejun Heo 	if (stat & (ATA_ERR | ATA_DF))
1417a51d644aSTejun Heo 		qc->err_mask |= AC_ERR_DEV;
1418a51d644aSTejun Heo 	else
1419c6fd2807SJeff Garzik 		return 0;
1420c6fd2807SJeff Garzik 
1421c6fd2807SJeff Garzik 	switch (qc->dev->class) {
1422c6fd2807SJeff Garzik 	case ATA_DEV_ATA:
1423c6fd2807SJeff Garzik 		if (err & ATA_ICRC)
1424c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_ATA_BUS;
1425c6fd2807SJeff Garzik 		if (err & ATA_UNC)
1426c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_MEDIA;
1427c6fd2807SJeff Garzik 		if (err & ATA_IDNF)
1428c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_INVALID;
1429c6fd2807SJeff Garzik 		break;
1430c6fd2807SJeff Garzik 
1431c6fd2807SJeff Garzik 	case ATA_DEV_ATAPI:
1432a569a30dSTejun Heo 		if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
143356287768SAlbert Lee 			tmp = atapi_eh_request_sense(qc);
1434c6fd2807SJeff Garzik 			if (!tmp) {
1435a569a30dSTejun Heo 				/* ATA_QCFLAG_SENSE_VALID is used to
1436a569a30dSTejun Heo 				 * tell atapi_qc_complete() that sense
1437a569a30dSTejun Heo 				 * data is already valid.
1438c6fd2807SJeff Garzik 				 *
1439c6fd2807SJeff Garzik 				 * TODO: interpret sense data and set
1440c6fd2807SJeff Garzik 				 * appropriate err_mask.
1441c6fd2807SJeff Garzik 				 */
1442c6fd2807SJeff Garzik 				qc->flags |= ATA_QCFLAG_SENSE_VALID;
1443c6fd2807SJeff Garzik 			} else
1444c6fd2807SJeff Garzik 				qc->err_mask |= tmp;
1445c6fd2807SJeff Garzik 		}
1446a569a30dSTejun Heo 	}
1447c6fd2807SJeff Garzik 
1448c6fd2807SJeff Garzik 	if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1449c6fd2807SJeff Garzik 		action |= ATA_EH_SOFTRESET;
1450c6fd2807SJeff Garzik 
1451c6fd2807SJeff Garzik 	return action;
1452c6fd2807SJeff Garzik }
1453c6fd2807SJeff Garzik 
14547d47e8d4STejun Heo static int ata_eh_categorize_error(int is_io, unsigned int err_mask)
1455c6fd2807SJeff Garzik {
14567d47e8d4STejun Heo 	if (err_mask & AC_ERR_ATA_BUS)
1457c6fd2807SJeff Garzik 		return 1;
1458c6fd2807SJeff Garzik 
14597d47e8d4STejun Heo 	if (err_mask & AC_ERR_TIMEOUT)
1460c6fd2807SJeff Garzik 		return 2;
14617d47e8d4STejun Heo 
14627d47e8d4STejun Heo 	if (is_io) {
14637d47e8d4STejun Heo 		if (err_mask & AC_ERR_HSM)
14647d47e8d4STejun Heo 			return 2;
14657d47e8d4STejun Heo 		if ((err_mask &
14667d47e8d4STejun Heo 		     (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
14677d47e8d4STejun Heo 			return 3;
1468c6fd2807SJeff Garzik 	}
1469c6fd2807SJeff Garzik 
1470c6fd2807SJeff Garzik 	return 0;
1471c6fd2807SJeff Garzik }
1472c6fd2807SJeff Garzik 
14737d47e8d4STejun Heo struct speed_down_verdict_arg {
1474c6fd2807SJeff Garzik 	u64 since;
14757d47e8d4STejun Heo 	int nr_errors[4];
1476c6fd2807SJeff Garzik };
1477c6fd2807SJeff Garzik 
14787d47e8d4STejun Heo static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
1479c6fd2807SJeff Garzik {
14807d47e8d4STejun Heo 	struct speed_down_verdict_arg *arg = void_arg;
14817d47e8d4STejun Heo 	int cat = ata_eh_categorize_error(ent->is_io, ent->err_mask);
1482c6fd2807SJeff Garzik 
1483c6fd2807SJeff Garzik 	if (ent->timestamp < arg->since)
1484c6fd2807SJeff Garzik 		return -1;
1485c6fd2807SJeff Garzik 
14867d47e8d4STejun Heo 	arg->nr_errors[cat]++;
1487c6fd2807SJeff Garzik 	return 0;
1488c6fd2807SJeff Garzik }
1489c6fd2807SJeff Garzik 
1490c6fd2807SJeff Garzik /**
14917d47e8d4STejun Heo  *	ata_eh_speed_down_verdict - Determine speed down verdict
1492c6fd2807SJeff Garzik  *	@dev: Device of interest
1493c6fd2807SJeff Garzik  *
1494c6fd2807SJeff Garzik  *	This function examines error ring of @dev and determines
14957d47e8d4STejun Heo  *	whether NCQ needs to be turned off, transfer speed should be
14967d47e8d4STejun Heo  *	stepped down, or falling back to PIO is necessary.
1497c6fd2807SJeff Garzik  *
14987d47e8d4STejun Heo  *	Cat-1 is ATA_BUS error for any command.
1499c6fd2807SJeff Garzik  *
15007d47e8d4STejun Heo  *	Cat-2 is TIMEOUT for any command or HSM violation for known
15017d47e8d4STejun Heo  *	supported commands.
15027d47e8d4STejun Heo  *
15037d47e8d4STejun Heo  *	Cat-3 is is unclassified DEV error for known supported
1504c6fd2807SJeff Garzik  *	command.
1505c6fd2807SJeff Garzik  *
15067d47e8d4STejun Heo  *	NCQ needs to be turned off if there have been more than 3
15077d47e8d4STejun Heo  *	Cat-2 + Cat-3 errors during last 10 minutes.
15087d47e8d4STejun Heo  *
15097d47e8d4STejun Heo  *	Speed down is necessary if there have been more than 3 Cat-1 +
15107d47e8d4STejun Heo  *	Cat-2 errors or 10 Cat-3 errors during last 10 minutes.
15117d47e8d4STejun Heo  *
15127d47e8d4STejun Heo  *	Falling back to PIO mode is necessary if there have been more
15137d47e8d4STejun Heo  *	than 10 Cat-1 + Cat-2 + Cat-3 errors during last 5 minutes.
15147d47e8d4STejun Heo  *
1515c6fd2807SJeff Garzik  *	LOCKING:
1516c6fd2807SJeff Garzik  *	Inherited from caller.
1517c6fd2807SJeff Garzik  *
1518c6fd2807SJeff Garzik  *	RETURNS:
15197d47e8d4STejun Heo  *	OR of ATA_EH_SPDN_* flags.
1520c6fd2807SJeff Garzik  */
15217d47e8d4STejun Heo static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
1522c6fd2807SJeff Garzik {
15237d47e8d4STejun Heo 	const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
15247d47e8d4STejun Heo 	u64 j64 = get_jiffies_64();
15257d47e8d4STejun Heo 	struct speed_down_verdict_arg arg;
15267d47e8d4STejun Heo 	unsigned int verdict = 0;
1527c6fd2807SJeff Garzik 
15287d47e8d4STejun Heo 	/* scan past 10 mins of error history */
1529c6fd2807SJeff Garzik 	memset(&arg, 0, sizeof(arg));
15307d47e8d4STejun Heo 	arg.since = j64 - min(j64, j10mins);
15317d47e8d4STejun Heo 	ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1532c6fd2807SJeff Garzik 
15337d47e8d4STejun Heo 	if (arg.nr_errors[2] + arg.nr_errors[3] > 3)
15347d47e8d4STejun Heo 		verdict |= ATA_EH_SPDN_NCQ_OFF;
15357d47e8d4STejun Heo 	if (arg.nr_errors[1] + arg.nr_errors[2] > 3 || arg.nr_errors[3] > 10)
15367d47e8d4STejun Heo 		verdict |= ATA_EH_SPDN_SPEED_DOWN;
1537c6fd2807SJeff Garzik 
15387d47e8d4STejun Heo 	/* scan past 3 mins of error history */
15397d47e8d4STejun Heo 	memset(&arg, 0, sizeof(arg));
15407d47e8d4STejun Heo 	arg.since = j64 - min(j64, j5mins);
15417d47e8d4STejun Heo 	ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1542c6fd2807SJeff Garzik 
15437d47e8d4STejun Heo 	if (arg.nr_errors[1] + arg.nr_errors[2] + arg.nr_errors[3] > 10)
15447d47e8d4STejun Heo 		verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
15457d47e8d4STejun Heo 
15467d47e8d4STejun Heo 	return verdict;
1547c6fd2807SJeff Garzik }
1548c6fd2807SJeff Garzik 
1549c6fd2807SJeff Garzik /**
1550c6fd2807SJeff Garzik  *	ata_eh_speed_down - record error and speed down if necessary
1551c6fd2807SJeff Garzik  *	@dev: Failed device
1552c6fd2807SJeff Garzik  *	@is_io: Did the device fail during normal IO?
1553c6fd2807SJeff Garzik  *	@err_mask: err_mask of the error
1554c6fd2807SJeff Garzik  *
1555c6fd2807SJeff Garzik  *	Record error and examine error history to determine whether
1556c6fd2807SJeff Garzik  *	adjusting transmission speed is necessary.  It also sets
1557c6fd2807SJeff Garzik  *	transmission limits appropriately if such adjustment is
1558c6fd2807SJeff Garzik  *	necessary.
1559c6fd2807SJeff Garzik  *
1560c6fd2807SJeff Garzik  *	LOCKING:
1561c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1562c6fd2807SJeff Garzik  *
1563c6fd2807SJeff Garzik  *	RETURNS:
15647d47e8d4STejun Heo  *	Determined recovery action.
1565c6fd2807SJeff Garzik  */
15667d47e8d4STejun Heo static unsigned int ata_eh_speed_down(struct ata_device *dev, int is_io,
1567c6fd2807SJeff Garzik 				      unsigned int err_mask)
1568c6fd2807SJeff Garzik {
15697d47e8d4STejun Heo 	unsigned int verdict;
15707d47e8d4STejun Heo 	unsigned int action = 0;
15717d47e8d4STejun Heo 
15727d47e8d4STejun Heo 	/* don't bother if Cat-0 error */
15737d47e8d4STejun Heo 	if (ata_eh_categorize_error(is_io, err_mask) == 0)
1574c6fd2807SJeff Garzik 		return 0;
1575c6fd2807SJeff Garzik 
1576c6fd2807SJeff Garzik 	/* record error and determine whether speed down is necessary */
1577c6fd2807SJeff Garzik 	ata_ering_record(&dev->ering, is_io, err_mask);
15787d47e8d4STejun Heo 	verdict = ata_eh_speed_down_verdict(dev);
1579c6fd2807SJeff Garzik 
15807d47e8d4STejun Heo 	/* turn off NCQ? */
15817d47e8d4STejun Heo 	if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
15827d47e8d4STejun Heo 	    (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
15837d47e8d4STejun Heo 			   ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
15847d47e8d4STejun Heo 		dev->flags |= ATA_DFLAG_NCQ_OFF;
15857d47e8d4STejun Heo 		ata_dev_printk(dev, KERN_WARNING,
15867d47e8d4STejun Heo 			       "NCQ disabled due to excessive errors\n");
15877d47e8d4STejun Heo 		goto done;
15887d47e8d4STejun Heo 	}
1589c6fd2807SJeff Garzik 
15907d47e8d4STejun Heo 	/* speed down? */
15917d47e8d4STejun Heo 	if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1592c6fd2807SJeff Garzik 		/* speed down SATA link speed if possible */
1593936fd732STejun Heo 		if (sata_down_spd_limit(dev->link) == 0) {
15947d47e8d4STejun Heo 			action |= ATA_EH_HARDRESET;
15957d47e8d4STejun Heo 			goto done;
15967d47e8d4STejun Heo 		}
1597c6fd2807SJeff Garzik 
1598c6fd2807SJeff Garzik 		/* lower transfer mode */
15997d47e8d4STejun Heo 		if (dev->spdn_cnt < 2) {
16007d47e8d4STejun Heo 			static const int dma_dnxfer_sel[] =
16017d47e8d4STejun Heo 				{ ATA_DNXFER_DMA, ATA_DNXFER_40C };
16027d47e8d4STejun Heo 			static const int pio_dnxfer_sel[] =
16037d47e8d4STejun Heo 				{ ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
16047d47e8d4STejun Heo 			int sel;
1605c6fd2807SJeff Garzik 
16067d47e8d4STejun Heo 			if (dev->xfer_shift != ATA_SHIFT_PIO)
16077d47e8d4STejun Heo 				sel = dma_dnxfer_sel[dev->spdn_cnt];
16087d47e8d4STejun Heo 			else
16097d47e8d4STejun Heo 				sel = pio_dnxfer_sel[dev->spdn_cnt];
16107d47e8d4STejun Heo 
16117d47e8d4STejun Heo 			dev->spdn_cnt++;
16127d47e8d4STejun Heo 
16137d47e8d4STejun Heo 			if (ata_down_xfermask_limit(dev, sel) == 0) {
16147d47e8d4STejun Heo 				action |= ATA_EH_SOFTRESET;
16157d47e8d4STejun Heo 				goto done;
16167d47e8d4STejun Heo 			}
16177d47e8d4STejun Heo 		}
16187d47e8d4STejun Heo 	}
16197d47e8d4STejun Heo 
16207d47e8d4STejun Heo 	/* Fall back to PIO?  Slowing down to PIO is meaningless for
16217d47e8d4STejun Heo 	 * SATA.  Consider it only for PATA.
16227d47e8d4STejun Heo 	 */
16237d47e8d4STejun Heo 	if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
16249af5c9c9STejun Heo 	    (dev->link->ap->cbl != ATA_CBL_SATA) &&
16257d47e8d4STejun Heo 	    (dev->xfer_shift != ATA_SHIFT_PIO)) {
16267d47e8d4STejun Heo 		if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
16277d47e8d4STejun Heo 			dev->spdn_cnt = 0;
16287d47e8d4STejun Heo 			action |= ATA_EH_SOFTRESET;
16297d47e8d4STejun Heo 			goto done;
16307d47e8d4STejun Heo 		}
16317d47e8d4STejun Heo 	}
16327d47e8d4STejun Heo 
1633c6fd2807SJeff Garzik 	return 0;
16347d47e8d4STejun Heo  done:
16357d47e8d4STejun Heo 	/* device has been slowed down, blow error history */
16367d47e8d4STejun Heo 	ata_ering_clear(&dev->ering);
16377d47e8d4STejun Heo 	return action;
1638c6fd2807SJeff Garzik }
1639c6fd2807SJeff Garzik 
1640c6fd2807SJeff Garzik /**
16419b1e2658STejun Heo  *	ata_eh_link_autopsy - analyze error and determine recovery action
16429b1e2658STejun Heo  *	@link: host link to perform autopsy on
1643c6fd2807SJeff Garzik  *
16440260731fSTejun Heo  *	Analyze why @link failed and determine which recovery actions
16450260731fSTejun Heo  *	are needed.  This function also sets more detailed AC_ERR_*
16460260731fSTejun Heo  *	values and fills sense data for ATAPI CHECK SENSE.
1647c6fd2807SJeff Garzik  *
1648c6fd2807SJeff Garzik  *	LOCKING:
1649c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1650c6fd2807SJeff Garzik  */
16519b1e2658STejun Heo static void ata_eh_link_autopsy(struct ata_link *link)
1652c6fd2807SJeff Garzik {
16530260731fSTejun Heo 	struct ata_port *ap = link->ap;
1654936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
1655dfcc173dSTejun Heo 	struct ata_device *dev;
1656c6fd2807SJeff Garzik 	unsigned int all_err_mask = 0;
1657c6fd2807SJeff Garzik 	int tag, is_io = 0;
1658c6fd2807SJeff Garzik 	u32 serror;
1659c6fd2807SJeff Garzik 	int rc;
1660c6fd2807SJeff Garzik 
1661c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
1662c6fd2807SJeff Garzik 
1663c6fd2807SJeff Garzik 	if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1664c6fd2807SJeff Garzik 		return;
1665c6fd2807SJeff Garzik 
1666c6fd2807SJeff Garzik 	/* obtain and analyze SError */
1667936fd732STejun Heo 	rc = sata_scr_read(link, SCR_ERROR, &serror);
1668c6fd2807SJeff Garzik 	if (rc == 0) {
1669c6fd2807SJeff Garzik 		ehc->i.serror |= serror;
16700260731fSTejun Heo 		ata_eh_analyze_serror(link);
16714e57c517STejun Heo 	} else if (rc != -EOPNOTSUPP) {
16724e57c517STejun Heo 		/* SError read failed, force hardreset and probing */
16734e57c517STejun Heo 		ata_ehi_schedule_probe(&ehc->i);
1674c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_HARDRESET;
16754e57c517STejun Heo 		ehc->i.err_mask |= AC_ERR_OTHER;
16764e57c517STejun Heo 	}
1677c6fd2807SJeff Garzik 
1678c6fd2807SJeff Garzik 	/* analyze NCQ failure */
16790260731fSTejun Heo 	ata_eh_analyze_ncq_error(link);
1680c6fd2807SJeff Garzik 
1681c6fd2807SJeff Garzik 	/* any real error trumps AC_ERR_OTHER */
1682c6fd2807SJeff Garzik 	if (ehc->i.err_mask & ~AC_ERR_OTHER)
1683c6fd2807SJeff Garzik 		ehc->i.err_mask &= ~AC_ERR_OTHER;
1684c6fd2807SJeff Garzik 
1685c6fd2807SJeff Garzik 	all_err_mask |= ehc->i.err_mask;
1686c6fd2807SJeff Garzik 
1687c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1688c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1689c6fd2807SJeff Garzik 
16900260731fSTejun Heo 		if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
1691c6fd2807SJeff Garzik 			continue;
1692c6fd2807SJeff Garzik 
1693c6fd2807SJeff Garzik 		/* inherit upper level err_mask */
1694c6fd2807SJeff Garzik 		qc->err_mask |= ehc->i.err_mask;
1695c6fd2807SJeff Garzik 
1696c6fd2807SJeff Garzik 		/* analyze TF */
1697c6fd2807SJeff Garzik 		ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1698c6fd2807SJeff Garzik 
1699c6fd2807SJeff Garzik 		/* DEV errors are probably spurious in case of ATA_BUS error */
1700c6fd2807SJeff Garzik 		if (qc->err_mask & AC_ERR_ATA_BUS)
1701c6fd2807SJeff Garzik 			qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1702c6fd2807SJeff Garzik 					  AC_ERR_INVALID);
1703c6fd2807SJeff Garzik 
1704c6fd2807SJeff Garzik 		/* any real error trumps unknown error */
1705c6fd2807SJeff Garzik 		if (qc->err_mask & ~AC_ERR_OTHER)
1706c6fd2807SJeff Garzik 			qc->err_mask &= ~AC_ERR_OTHER;
1707c6fd2807SJeff Garzik 
1708c6fd2807SJeff Garzik 		/* SENSE_VALID trumps dev/unknown error and revalidation */
1709f90f0828STejun Heo 		if (qc->flags & ATA_QCFLAG_SENSE_VALID)
1710c6fd2807SJeff Garzik 			qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1711c6fd2807SJeff Garzik 
1712c6fd2807SJeff Garzik 		/* accumulate error info */
1713c6fd2807SJeff Garzik 		ehc->i.dev = qc->dev;
1714c6fd2807SJeff Garzik 		all_err_mask |= qc->err_mask;
1715c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_IO)
1716c6fd2807SJeff Garzik 			is_io = 1;
1717c6fd2807SJeff Garzik 	}
1718c6fd2807SJeff Garzik 
1719c6fd2807SJeff Garzik 	/* enforce default EH actions */
1720c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN ||
1721c6fd2807SJeff Garzik 	    all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
1722c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_SOFTRESET;
1723f90f0828STejun Heo 	else if ((is_io && all_err_mask) ||
1724f90f0828STejun Heo 		 (!is_io && (all_err_mask & ~AC_ERR_DEV)))
1725c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_REVALIDATE;
1726c6fd2807SJeff Garzik 
1727dfcc173dSTejun Heo 	/* If we have offending qcs and the associated failed device,
1728dfcc173dSTejun Heo 	 * perform per-dev EH action only on the offending device.
1729dfcc173dSTejun Heo 	 */
1730c6fd2807SJeff Garzik 	if (ehc->i.dev) {
1731c6fd2807SJeff Garzik 		ehc->i.dev_action[ehc->i.dev->devno] |=
1732c6fd2807SJeff Garzik 			ehc->i.action & ATA_EH_PERDEV_MASK;
1733c6fd2807SJeff Garzik 		ehc->i.action &= ~ATA_EH_PERDEV_MASK;
1734c6fd2807SJeff Garzik 	}
1735c6fd2807SJeff Garzik 
1736*2695e366STejun Heo 	/* propagate timeout to host link */
1737*2695e366STejun Heo 	if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link))
1738*2695e366STejun Heo 		ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT;
1739*2695e366STejun Heo 
1740*2695e366STejun Heo 	/* record error and consider speeding down */
1741dfcc173dSTejun Heo 	dev = ehc->i.dev;
1742*2695e366STejun Heo 	if (!dev && ((ata_link_max_devices(link) == 1 &&
1743*2695e366STejun Heo 		      ata_dev_enabled(link->device))))
1744dfcc173dSTejun Heo 	    dev = link->device;
1745dfcc173dSTejun Heo 
1746dfcc173dSTejun Heo 	if (dev)
1747dfcc173dSTejun Heo 		ehc->i.action |= ata_eh_speed_down(dev, is_io, all_err_mask);
1748dfcc173dSTejun Heo 
1749c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
1750c6fd2807SJeff Garzik }
1751c6fd2807SJeff Garzik 
1752c6fd2807SJeff Garzik /**
17539b1e2658STejun Heo  *	ata_eh_autopsy - analyze error and determine recovery action
17549b1e2658STejun Heo  *	@ap: host port to perform autopsy on
17559b1e2658STejun Heo  *
17569b1e2658STejun Heo  *	Analyze all links of @ap and determine why they failed and
17579b1e2658STejun Heo  *	which recovery actions are needed.
17589b1e2658STejun Heo  *
17599b1e2658STejun Heo  *	LOCKING:
17609b1e2658STejun Heo  *	Kernel thread context (may sleep).
17619b1e2658STejun Heo  */
1762fb7fd614STejun Heo void ata_eh_autopsy(struct ata_port *ap)
17639b1e2658STejun Heo {
17649b1e2658STejun Heo 	struct ata_link *link;
17659b1e2658STejun Heo 
1766*2695e366STejun Heo 	ata_port_for_each_link(link, ap)
17679b1e2658STejun Heo 		ata_eh_link_autopsy(link);
1768*2695e366STejun Heo 
1769*2695e366STejun Heo 	/* Autopsy of fanout ports can affect host link autopsy.
1770*2695e366STejun Heo 	 * Perform host link autopsy last.
1771*2695e366STejun Heo 	 */
1772*2695e366STejun Heo 	if (ap->nr_pmp_links)
1773*2695e366STejun Heo 		ata_eh_link_autopsy(&ap->link);
17749b1e2658STejun Heo }
17759b1e2658STejun Heo 
17769b1e2658STejun Heo /**
17779b1e2658STejun Heo  *	ata_eh_link_report - report error handling to user
17780260731fSTejun Heo  *	@link: ATA link EH is going on
1779c6fd2807SJeff Garzik  *
1780c6fd2807SJeff Garzik  *	Report EH to user.
1781c6fd2807SJeff Garzik  *
1782c6fd2807SJeff Garzik  *	LOCKING:
1783c6fd2807SJeff Garzik  *	None.
1784c6fd2807SJeff Garzik  */
17859b1e2658STejun Heo static void ata_eh_link_report(struct ata_link *link)
1786c6fd2807SJeff Garzik {
17870260731fSTejun Heo 	struct ata_port *ap = link->ap;
17880260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
1789c6fd2807SJeff Garzik 	const char *frozen, *desc;
1790a1e10f7eSTejun Heo 	char tries_buf[6];
1791c6fd2807SJeff Garzik 	int tag, nr_failed = 0;
1792c6fd2807SJeff Garzik 
179394ff3d54STejun Heo 	if (ehc->i.flags & ATA_EHI_QUIET)
179494ff3d54STejun Heo 		return;
179594ff3d54STejun Heo 
1796c6fd2807SJeff Garzik 	desc = NULL;
1797c6fd2807SJeff Garzik 	if (ehc->i.desc[0] != '\0')
1798c6fd2807SJeff Garzik 		desc = ehc->i.desc;
1799c6fd2807SJeff Garzik 
1800c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1801c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1802c6fd2807SJeff Garzik 
1803e027bd36STejun Heo 		if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link ||
1804e027bd36STejun Heo 		    ((qc->flags & ATA_QCFLAG_QUIET) &&
1805e027bd36STejun Heo 		     qc->err_mask == AC_ERR_DEV))
1806c6fd2807SJeff Garzik 			continue;
1807c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1808c6fd2807SJeff Garzik 			continue;
1809c6fd2807SJeff Garzik 
1810c6fd2807SJeff Garzik 		nr_failed++;
1811c6fd2807SJeff Garzik 	}
1812c6fd2807SJeff Garzik 
1813c6fd2807SJeff Garzik 	if (!nr_failed && !ehc->i.err_mask)
1814c6fd2807SJeff Garzik 		return;
1815c6fd2807SJeff Garzik 
1816c6fd2807SJeff Garzik 	frozen = "";
1817c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN)
1818c6fd2807SJeff Garzik 		frozen = " frozen";
1819c6fd2807SJeff Garzik 
1820a1e10f7eSTejun Heo 	memset(tries_buf, 0, sizeof(tries_buf));
1821a1e10f7eSTejun Heo 	if (ap->eh_tries < ATA_EH_MAX_TRIES)
1822a1e10f7eSTejun Heo 		snprintf(tries_buf, sizeof(tries_buf) - 1, " t%d",
1823a1e10f7eSTejun Heo 			 ap->eh_tries);
1824a1e10f7eSTejun Heo 
1825c6fd2807SJeff Garzik 	if (ehc->i.dev) {
1826c6fd2807SJeff Garzik 		ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1827a1e10f7eSTejun Heo 			       "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1828a1e10f7eSTejun Heo 			       ehc->i.err_mask, link->sactive, ehc->i.serror,
1829a1e10f7eSTejun Heo 			       ehc->i.action, frozen, tries_buf);
1830c6fd2807SJeff Garzik 		if (desc)
1831b64bbc39STejun Heo 			ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
1832c6fd2807SJeff Garzik 	} else {
18330260731fSTejun Heo 		ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
1834a1e10f7eSTejun Heo 				"SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1835a1e10f7eSTejun Heo 				ehc->i.err_mask, link->sactive, ehc->i.serror,
1836a1e10f7eSTejun Heo 				ehc->i.action, frozen, tries_buf);
1837c6fd2807SJeff Garzik 		if (desc)
18380260731fSTejun Heo 			ata_link_printk(link, KERN_ERR, "%s\n", desc);
1839c6fd2807SJeff Garzik 	}
1840c6fd2807SJeff Garzik 
18411333e194SRobert Hancock 	if (ehc->i.serror)
18421333e194SRobert Hancock 		ata_port_printk(ap, KERN_ERR,
18431333e194SRobert Hancock 		  "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
18441333e194SRobert Hancock 		  ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
18451333e194SRobert Hancock 		  ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
18461333e194SRobert Hancock 		  ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
18471333e194SRobert Hancock 		  ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
18481333e194SRobert Hancock 		  ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
18491333e194SRobert Hancock 		  ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
18501333e194SRobert Hancock 		  ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
18511333e194SRobert Hancock 		  ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
18521333e194SRobert Hancock 		  ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
18531333e194SRobert Hancock 		  ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
18541333e194SRobert Hancock 		  ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
18551333e194SRobert Hancock 		  ehc->i.serror & SERR_CRC ? "BadCRC " : "",
18561333e194SRobert Hancock 		  ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
18571333e194SRobert Hancock 		  ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
18581333e194SRobert Hancock 		  ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
18591333e194SRobert Hancock 		  ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
18601333e194SRobert Hancock 		  ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
18611333e194SRobert Hancock 
1862c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1863c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
18648a937581STejun Heo 		struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
1865abb6a889STejun Heo 		const u8 *cdb = qc->cdb;
1866abb6a889STejun Heo 		char data_buf[20] = "";
1867abb6a889STejun Heo 		char cdb_buf[70] = "";
1868c6fd2807SJeff Garzik 
18690260731fSTejun Heo 		if (!(qc->flags & ATA_QCFLAG_FAILED) ||
18700260731fSTejun Heo 		    qc->dev->link != link || !qc->err_mask)
1871c6fd2807SJeff Garzik 			continue;
1872c6fd2807SJeff Garzik 
1873abb6a889STejun Heo 		if (qc->dma_dir != DMA_NONE) {
1874abb6a889STejun Heo 			static const char *dma_str[] = {
1875abb6a889STejun Heo 				[DMA_BIDIRECTIONAL]	= "bidi",
1876abb6a889STejun Heo 				[DMA_TO_DEVICE]		= "out",
1877abb6a889STejun Heo 				[DMA_FROM_DEVICE]	= "in",
1878abb6a889STejun Heo 			};
1879abb6a889STejun Heo 			static const char *prot_str[] = {
1880abb6a889STejun Heo 				[ATA_PROT_PIO]		= "pio",
1881abb6a889STejun Heo 				[ATA_PROT_DMA]		= "dma",
1882abb6a889STejun Heo 				[ATA_PROT_NCQ]		= "ncq",
1883abb6a889STejun Heo 				[ATA_PROT_ATAPI]	= "pio",
1884abb6a889STejun Heo 				[ATA_PROT_ATAPI_DMA]	= "dma",
1885abb6a889STejun Heo 			};
1886abb6a889STejun Heo 
1887abb6a889STejun Heo 			snprintf(data_buf, sizeof(data_buf), " %s %u %s",
1888abb6a889STejun Heo 				 prot_str[qc->tf.protocol], qc->nbytes,
1889abb6a889STejun Heo 				 dma_str[qc->dma_dir]);
1890abb6a889STejun Heo 		}
1891abb6a889STejun Heo 
1892abb6a889STejun Heo 		if (is_atapi_taskfile(&qc->tf))
1893abb6a889STejun Heo 			snprintf(cdb_buf, sizeof(cdb_buf),
1894abb6a889STejun Heo 				 "cdb %02x %02x %02x %02x %02x %02x %02x %02x  "
1895abb6a889STejun Heo 				 "%02x %02x %02x %02x %02x %02x %02x %02x\n         ",
1896abb6a889STejun Heo 				 cdb[0], cdb[1], cdb[2], cdb[3],
1897abb6a889STejun Heo 				 cdb[4], cdb[5], cdb[6], cdb[7],
1898abb6a889STejun Heo 				 cdb[8], cdb[9], cdb[10], cdb[11],
1899abb6a889STejun Heo 				 cdb[12], cdb[13], cdb[14], cdb[15]);
1900abb6a889STejun Heo 
19018a937581STejun Heo 		ata_dev_printk(qc->dev, KERN_ERR,
19028a937581STejun Heo 			"cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
1903abb6a889STejun Heo 			"tag %d%s\n         %s"
19048a937581STejun Heo 			"res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
19055335b729STejun Heo 			"Emask 0x%x (%s)%s\n",
19068a937581STejun Heo 			cmd->command, cmd->feature, cmd->nsect,
19078a937581STejun Heo 			cmd->lbal, cmd->lbam, cmd->lbah,
19088a937581STejun Heo 			cmd->hob_feature, cmd->hob_nsect,
19098a937581STejun Heo 			cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
1910abb6a889STejun Heo 			cmd->device, qc->tag, data_buf, cdb_buf,
19118a937581STejun Heo 			res->command, res->feature, res->nsect,
19128a937581STejun Heo 			res->lbal, res->lbam, res->lbah,
19138a937581STejun Heo 			res->hob_feature, res->hob_nsect,
19148a937581STejun Heo 			res->hob_lbal, res->hob_lbam, res->hob_lbah,
19155335b729STejun Heo 			res->device, qc->err_mask, ata_err_string(qc->err_mask),
19165335b729STejun Heo 			qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
19171333e194SRobert Hancock 
19181333e194SRobert Hancock 		if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
19191333e194SRobert Hancock 				    ATA_ERR)) {
19201333e194SRobert Hancock 			if (res->command & ATA_BUSY)
19211333e194SRobert Hancock 				ata_dev_printk(qc->dev, KERN_ERR,
19221333e194SRobert Hancock 				  "status: { Busy }\n");
19231333e194SRobert Hancock 			else
19241333e194SRobert Hancock 				ata_dev_printk(qc->dev, KERN_ERR,
19251333e194SRobert Hancock 				  "status: { %s%s%s%s}\n",
19261333e194SRobert Hancock 				  res->command & ATA_DRDY ? "DRDY " : "",
19271333e194SRobert Hancock 				  res->command & ATA_DF ? "DF " : "",
19281333e194SRobert Hancock 				  res->command & ATA_DRQ ? "DRQ " : "",
19291333e194SRobert Hancock 				  res->command & ATA_ERR ? "ERR " : "");
19301333e194SRobert Hancock 		}
19311333e194SRobert Hancock 
19321333e194SRobert Hancock 		if (cmd->command != ATA_CMD_PACKET &&
19331333e194SRobert Hancock 		    (res->feature & (ATA_ICRC | ATA_UNC | ATA_IDNF |
19341333e194SRobert Hancock 				     ATA_ABORTED)))
19351333e194SRobert Hancock 			ata_dev_printk(qc->dev, KERN_ERR,
19361333e194SRobert Hancock 			  "error: { %s%s%s%s}\n",
19371333e194SRobert Hancock 			  res->feature & ATA_ICRC ? "ICRC " : "",
19381333e194SRobert Hancock 			  res->feature & ATA_UNC ? "UNC " : "",
19391333e194SRobert Hancock 			  res->feature & ATA_IDNF ? "IDNF " : "",
19401333e194SRobert Hancock 			  res->feature & ATA_ABORTED ? "ABRT " : "");
1941c6fd2807SJeff Garzik 	}
1942c6fd2807SJeff Garzik }
1943c6fd2807SJeff Garzik 
19449b1e2658STejun Heo /**
19459b1e2658STejun Heo  *	ata_eh_report - report error handling to user
19469b1e2658STejun Heo  *	@ap: ATA port to report EH about
19479b1e2658STejun Heo  *
19489b1e2658STejun Heo  *	Report EH to user.
19499b1e2658STejun Heo  *
19509b1e2658STejun Heo  *	LOCKING:
19519b1e2658STejun Heo  *	None.
19529b1e2658STejun Heo  */
1953fb7fd614STejun Heo void ata_eh_report(struct ata_port *ap)
19549b1e2658STejun Heo {
19559b1e2658STejun Heo 	struct ata_link *link;
19569b1e2658STejun Heo 
19579b1e2658STejun Heo 	__ata_port_for_each_link(link, ap)
19589b1e2658STejun Heo 		ata_eh_link_report(link);
19599b1e2658STejun Heo }
19609b1e2658STejun Heo 
1961cc0680a5STejun Heo static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
1962d4b2bab4STejun Heo 			unsigned int *classes, unsigned long deadline)
1963c6fd2807SJeff Garzik {
1964f58229f8STejun Heo 	struct ata_device *dev;
1965f58229f8STejun Heo 	int rc;
1966c6fd2807SJeff Garzik 
1967cc0680a5STejun Heo 	ata_link_for_each_dev(dev, link)
1968f58229f8STejun Heo 		classes[dev->devno] = ATA_DEV_UNKNOWN;
1969c6fd2807SJeff Garzik 
1970cc0680a5STejun Heo 	rc = reset(link, classes, deadline);
1971c6fd2807SJeff Garzik 	if (rc)
1972c6fd2807SJeff Garzik 		return rc;
1973c6fd2807SJeff Garzik 
1974c6fd2807SJeff Garzik 	/* If any class isn't ATA_DEV_UNKNOWN, consider classification
1975c6fd2807SJeff Garzik 	 * is complete and convert all ATA_DEV_UNKNOWN to
1976c6fd2807SJeff Garzik 	 * ATA_DEV_NONE.
1977c6fd2807SJeff Garzik 	 */
1978cc0680a5STejun Heo 	ata_link_for_each_dev(dev, link)
1979f58229f8STejun Heo 		if (classes[dev->devno] != ATA_DEV_UNKNOWN)
1980c6fd2807SJeff Garzik 			break;
1981c6fd2807SJeff Garzik 
1982f58229f8STejun Heo 	if (dev) {
1983cc0680a5STejun Heo 		ata_link_for_each_dev(dev, link) {
1984f58229f8STejun Heo 			if (classes[dev->devno] == ATA_DEV_UNKNOWN)
1985f58229f8STejun Heo 				classes[dev->devno] = ATA_DEV_NONE;
1986f58229f8STejun Heo 		}
1987f58229f8STejun Heo 	}
1988c6fd2807SJeff Garzik 
1989c6fd2807SJeff Garzik 	return 0;
1990c6fd2807SJeff Garzik }
1991c6fd2807SJeff Garzik 
1992ae791c05STejun Heo static int ata_eh_followup_srst_needed(struct ata_link *link,
1993ae791c05STejun Heo 				       int rc, int classify,
1994c6fd2807SJeff Garzik 				       const unsigned int *classes)
1995c6fd2807SJeff Garzik {
1996ae791c05STejun Heo 	if (link->flags & ATA_LFLAG_NO_SRST)
1997ae791c05STejun Heo 		return 0;
1998c6fd2807SJeff Garzik 	if (rc == -EAGAIN)
1999c6fd2807SJeff Garzik 		return 1;
2000c6fd2807SJeff Garzik 	if (rc != 0)
2001c6fd2807SJeff Garzik 		return 0;
20023495de73STejun Heo 	if ((link->ap->flags & ATA_FLAG_PMP) && ata_is_host_link(link))
20033495de73STejun Heo 		return 1;
2004ae791c05STejun Heo 	if (classify && !(link->flags & ATA_LFLAG_ASSUME_CLASS) &&
2005ae791c05STejun Heo 	    classes[0] == ATA_DEV_UNKNOWN)
2006c6fd2807SJeff Garzik 		return 1;
2007c6fd2807SJeff Garzik 	return 0;
2008c6fd2807SJeff Garzik }
2009c6fd2807SJeff Garzik 
2010fb7fd614STejun Heo int ata_eh_reset(struct ata_link *link, int classify,
2011c6fd2807SJeff Garzik 		 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
2012c6fd2807SJeff Garzik 		 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
2013c6fd2807SJeff Garzik {
2014416dc9edSTejun Heo 	const int max_tries = ARRAY_SIZE(ata_eh_reset_timeouts);
2015afaa5c37STejun Heo 	struct ata_port *ap = link->ap;
2016936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
2017c6fd2807SJeff Garzik 	unsigned int *classes = ehc->classes;
2018416dc9edSTejun Heo 	unsigned int lflags = link->flags;
2019c6fd2807SJeff Garzik 	int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
202031daabdaSTejun Heo 	int try = 0;
2021f58229f8STejun Heo 	struct ata_device *dev;
2022416dc9edSTejun Heo 	unsigned long deadline, now;
20230e06d9ceSTejun Heo 	unsigned int tmp_action;
2024c6fd2807SJeff Garzik 	ata_reset_fn_t reset;
2025afaa5c37STejun Heo 	unsigned long flags;
2026416dc9edSTejun Heo 	u32 sstatus;
2027f58229f8STejun Heo 	int rc;
2028c6fd2807SJeff Garzik 
2029c6fd2807SJeff Garzik 	/* about to reset */
2030afaa5c37STejun Heo 	spin_lock_irqsave(ap->lock, flags);
2031afaa5c37STejun Heo 	ap->pflags |= ATA_PFLAG_RESETTING;
2032afaa5c37STejun Heo 	spin_unlock_irqrestore(ap->lock, flags);
2033afaa5c37STejun Heo 
2034955e57dfSTejun Heo 	ata_eh_about_to_do(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
2035c6fd2807SJeff Garzik 
2036cdeab114STejun Heo 	ata_link_for_each_dev(dev, link) {
2037cdeab114STejun Heo 		/* If we issue an SRST then an ATA drive (not ATAPI)
2038cdeab114STejun Heo 		 * may change configuration and be in PIO0 timing. If
2039cdeab114STejun Heo 		 * we do a hard reset (or are coming from power on)
2040cdeab114STejun Heo 		 * this is true for ATA or ATAPI. Until we've set a
2041cdeab114STejun Heo 		 * suitable controller mode we should not touch the
2042cdeab114STejun Heo 		 * bus as we may be talking too fast.
2043cdeab114STejun Heo 		 */
2044cdeab114STejun Heo 		dev->pio_mode = XFER_PIO_0;
2045cdeab114STejun Heo 
2046cdeab114STejun Heo 		/* If the controller has a pio mode setup function
2047cdeab114STejun Heo 		 * then use it to set the chipset to rights. Don't
2048cdeab114STejun Heo 		 * touch the DMA setup as that will be dealt with when
2049cdeab114STejun Heo 		 * configuring devices.
2050cdeab114STejun Heo 		 */
2051cdeab114STejun Heo 		if (ap->ops->set_piomode)
2052cdeab114STejun Heo 			ap->ops->set_piomode(ap, dev);
2053cdeab114STejun Heo 	}
2054cdeab114STejun Heo 
2055c6fd2807SJeff Garzik 	/* Determine which reset to use and record in ehc->i.action.
2056c6fd2807SJeff Garzik 	 * prereset() may examine and modify it.
2057c6fd2807SJeff Garzik 	 */
2058416dc9edSTejun Heo 	if (softreset && (!hardreset || (!(lflags & ATA_LFLAG_NO_SRST) &&
2059ae791c05STejun Heo 					 !sata_set_spd_needed(link) &&
20600e06d9ceSTejun Heo 					 !(ehc->i.action & ATA_EH_HARDRESET))))
20610e06d9ceSTejun Heo 		tmp_action = ATA_EH_SOFTRESET;
2062c6fd2807SJeff Garzik 	else
20630e06d9ceSTejun Heo 		tmp_action = ATA_EH_HARDRESET;
20640e06d9ceSTejun Heo 
20650e06d9ceSTejun Heo 	ehc->i.action = (ehc->i.action & ~ATA_EH_RESET_MASK) | tmp_action;
2066c6fd2807SJeff Garzik 
2067c6fd2807SJeff Garzik 	if (prereset) {
2068cc0680a5STejun Heo 		rc = prereset(link, jiffies + ATA_EH_PRERESET_TIMEOUT);
2069c6fd2807SJeff Garzik 		if (rc) {
2070c961922bSAlan Cox 			if (rc == -ENOENT) {
2071cc0680a5STejun Heo 				ata_link_printk(link, KERN_DEBUG,
20724aa9ab67STejun Heo 						"port disabled. ignoring.\n");
20739af5c9c9STejun Heo 				ehc->i.action &= ~ATA_EH_RESET_MASK;
20744aa9ab67STejun Heo 
2075936fd732STejun Heo 				ata_link_for_each_dev(dev, link)
2076f58229f8STejun Heo 					classes[dev->devno] = ATA_DEV_NONE;
20774aa9ab67STejun Heo 
20784aa9ab67STejun Heo 				rc = 0;
2079c961922bSAlan Cox 			} else
2080cc0680a5STejun Heo 				ata_link_printk(link, KERN_ERR,
2081c6fd2807SJeff Garzik 					"prereset failed (errno=%d)\n", rc);
2082fccb6ea5STejun Heo 			goto out;
2083c6fd2807SJeff Garzik 		}
2084c6fd2807SJeff Garzik 	}
2085c6fd2807SJeff Garzik 
2086c6fd2807SJeff Garzik 	/* prereset() might have modified ehc->i.action */
2087c6fd2807SJeff Garzik 	if (ehc->i.action & ATA_EH_HARDRESET)
2088c6fd2807SJeff Garzik 		reset = hardreset;
2089c6fd2807SJeff Garzik 	else if (ehc->i.action & ATA_EH_SOFTRESET)
2090c6fd2807SJeff Garzik 		reset = softreset;
2091c6fd2807SJeff Garzik 	else {
2092c6fd2807SJeff Garzik 		/* prereset told us not to reset, bang classes and return */
2093936fd732STejun Heo 		ata_link_for_each_dev(dev, link)
2094f58229f8STejun Heo 			classes[dev->devno] = ATA_DEV_NONE;
2095fccb6ea5STejun Heo 		rc = 0;
2096fccb6ea5STejun Heo 		goto out;
2097c6fd2807SJeff Garzik 	}
2098c6fd2807SJeff Garzik 
2099c6fd2807SJeff Garzik 	/* did prereset() screw up?  if so, fix up to avoid oopsing */
2100c6fd2807SJeff Garzik 	if (!reset) {
2101c6fd2807SJeff Garzik 		if (softreset)
2102c6fd2807SJeff Garzik 			reset = softreset;
2103c6fd2807SJeff Garzik 		else
2104c6fd2807SJeff Garzik 			reset = hardreset;
2105c6fd2807SJeff Garzik 	}
2106c6fd2807SJeff Garzik 
2107c6fd2807SJeff Garzik  retry:
210831daabdaSTejun Heo 	deadline = jiffies + ata_eh_reset_timeouts[try++];
210931daabdaSTejun Heo 
2110c6fd2807SJeff Garzik 	/* shut up during boot probing */
2111c6fd2807SJeff Garzik 	if (verbose)
2112cc0680a5STejun Heo 		ata_link_printk(link, KERN_INFO, "%s resetting link\n",
2113c6fd2807SJeff Garzik 				reset == softreset ? "soft" : "hard");
2114c6fd2807SJeff Garzik 
2115c6fd2807SJeff Garzik 	/* mark that this EH session started with reset */
21160d64a233STejun Heo 	if (reset == hardreset)
21170d64a233STejun Heo 		ehc->i.flags |= ATA_EHI_DID_HARDRESET;
21180d64a233STejun Heo 	else
21190d64a233STejun Heo 		ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
2120c6fd2807SJeff Garzik 
2121cc0680a5STejun Heo 	rc = ata_do_reset(link, reset, classes, deadline);
2122c6fd2807SJeff Garzik 
2123c6fd2807SJeff Garzik 	if (reset == hardreset &&
2124ae791c05STejun Heo 	    ata_eh_followup_srst_needed(link, rc, classify, classes)) {
2125c6fd2807SJeff Garzik 		/* okay, let's do follow-up softreset */
2126c6fd2807SJeff Garzik 		reset = softreset;
2127c6fd2807SJeff Garzik 
2128c6fd2807SJeff Garzik 		if (!reset) {
2129cc0680a5STejun Heo 			ata_link_printk(link, KERN_ERR,
2130c6fd2807SJeff Garzik 					"follow-up softreset required "
2131c6fd2807SJeff Garzik 					"but no softreset avaliable\n");
2132fccb6ea5STejun Heo 			rc = -EINVAL;
213308cf69d0STejun Heo 			goto fail;
2134c6fd2807SJeff Garzik 		}
2135c6fd2807SJeff Garzik 
2136955e57dfSTejun Heo 		ata_eh_about_to_do(link, NULL, ATA_EH_RESET_MASK);
2137cc0680a5STejun Heo 		rc = ata_do_reset(link, reset, classes, deadline);
2138c6fd2807SJeff Garzik 	}
2139c6fd2807SJeff Garzik 
2140416dc9edSTejun Heo 	/* -EAGAIN can happen if we skipped followup SRST */
2141416dc9edSTejun Heo 	if (rc && rc != -EAGAIN)
2142416dc9edSTejun Heo 		goto fail;
2143008a7896STejun Heo 
214408cf69d0STejun Heo 	/* was classification successful? */
214508cf69d0STejun Heo 	if (classify && classes[0] == ATA_DEV_UNKNOWN &&
214608cf69d0STejun Heo 	    !(lflags & ATA_LFLAG_ASSUME_CLASS)) {
214708cf69d0STejun Heo 		if (try < max_tries) {
214808cf69d0STejun Heo 			ata_link_printk(link, KERN_WARNING,
214908cf69d0STejun Heo 					"classification failed\n");
215008cf69d0STejun Heo 			rc = -EINVAL;
215108cf69d0STejun Heo 			goto fail;
215208cf69d0STejun Heo 		}
215308cf69d0STejun Heo 
215408cf69d0STejun Heo 		ata_link_printk(link, KERN_WARNING,
215508cf69d0STejun Heo 				"classfication failed, assuming ATA\n");
215608cf69d0STejun Heo 		lflags |= ATA_LFLAG_ASSUME_ATA;
215708cf69d0STejun Heo 	}
215808cf69d0STejun Heo 
2159ae791c05STejun Heo 	ata_link_for_each_dev(dev, link) {
2160416dc9edSTejun Heo 		/* After the reset, the device state is PIO 0 and the
2161416dc9edSTejun Heo 		 * controller state is undefined.  Reset also wakes up
2162416dc9edSTejun Heo 		 * drives from sleeping mode.
2163c6fd2807SJeff Garzik 		 */
2164f58229f8STejun Heo 		dev->pio_mode = XFER_PIO_0;
2165054a5fbaSTejun Heo 		dev->flags &= ~ATA_DFLAG_SLEEPING;
2166c6fd2807SJeff Garzik 
2167ae791c05STejun Heo 		if (ata_link_offline(link))
2168ae791c05STejun Heo 			continue;
2169ae791c05STejun Heo 
2170ae791c05STejun Heo 		/* apply class override and convert UNKNOWN to NONE */
2171416dc9edSTejun Heo 		if (lflags & ATA_LFLAG_ASSUME_ATA)
2172ae791c05STejun Heo 			classes[dev->devno] = ATA_DEV_ATA;
2173416dc9edSTejun Heo 		else if (lflags & ATA_LFLAG_ASSUME_SEMB)
2174ae791c05STejun Heo 			classes[dev->devno] = ATA_DEV_SEMB_UNSUP; /* not yet */
2175ae791c05STejun Heo 		else if (classes[dev->devno] == ATA_DEV_UNKNOWN)
2176ae791c05STejun Heo 			classes[dev->devno] = ATA_DEV_NONE;
2177ae791c05STejun Heo 	}
2178ae791c05STejun Heo 
2179008a7896STejun Heo 	/* record current link speed */
2180936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2181936fd732STejun Heo 		link->sata_spd = (sstatus >> 4) & 0xf;
2182008a7896STejun Heo 
2183c6fd2807SJeff Garzik 	if (postreset)
2184cc0680a5STejun Heo 		postreset(link, classes);
2185c6fd2807SJeff Garzik 
2186c6fd2807SJeff Garzik 	/* reset successful, schedule revalidation */
2187955e57dfSTejun Heo 	ata_eh_done(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
2188c6fd2807SJeff Garzik 	ehc->i.action |= ATA_EH_REVALIDATE;
2189416dc9edSTejun Heo 
2190416dc9edSTejun Heo 	rc = 0;
2191fccb6ea5STejun Heo  out:
2192fccb6ea5STejun Heo 	/* clear hotplug flag */
2193fccb6ea5STejun Heo 	ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2194afaa5c37STejun Heo 
2195afaa5c37STejun Heo 	spin_lock_irqsave(ap->lock, flags);
2196afaa5c37STejun Heo 	ap->pflags &= ~ATA_PFLAG_RESETTING;
2197afaa5c37STejun Heo 	spin_unlock_irqrestore(ap->lock, flags);
2198afaa5c37STejun Heo 
2199c6fd2807SJeff Garzik 	return rc;
2200416dc9edSTejun Heo 
2201416dc9edSTejun Heo  fail:
2202416dc9edSTejun Heo 	if (rc == -ERESTART || try >= max_tries)
2203416dc9edSTejun Heo 		goto out;
2204416dc9edSTejun Heo 
2205416dc9edSTejun Heo 	now = jiffies;
2206416dc9edSTejun Heo 	if (time_before(now, deadline)) {
2207416dc9edSTejun Heo 		unsigned long delta = deadline - now;
2208416dc9edSTejun Heo 
2209416dc9edSTejun Heo 		ata_link_printk(link, KERN_WARNING, "reset failed "
2210416dc9edSTejun Heo 				"(errno=%d), retrying in %u secs\n",
2211416dc9edSTejun Heo 				rc, (jiffies_to_msecs(delta) + 999) / 1000);
2212416dc9edSTejun Heo 
2213416dc9edSTejun Heo 		while (delta)
2214416dc9edSTejun Heo 			delta = schedule_timeout_uninterruptible(delta);
2215416dc9edSTejun Heo 	}
2216416dc9edSTejun Heo 
2217416dc9edSTejun Heo 	if (rc == -EPIPE || try == max_tries - 1)
2218416dc9edSTejun Heo 		sata_down_spd_limit(link);
2219416dc9edSTejun Heo 	if (hardreset)
2220416dc9edSTejun Heo 		reset = hardreset;
2221416dc9edSTejun Heo 	goto retry;
2222c6fd2807SJeff Garzik }
2223c6fd2807SJeff Garzik 
22240260731fSTejun Heo static int ata_eh_revalidate_and_attach(struct ata_link *link,
2225c6fd2807SJeff Garzik 					struct ata_device **r_failed_dev)
2226c6fd2807SJeff Garzik {
22270260731fSTejun Heo 	struct ata_port *ap = link->ap;
22280260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
2229c6fd2807SJeff Garzik 	struct ata_device *dev;
22308c3c52a8STejun Heo 	unsigned int new_mask = 0;
2231c6fd2807SJeff Garzik 	unsigned long flags;
2232f58229f8STejun Heo 	int rc = 0;
2233c6fd2807SJeff Garzik 
2234c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
2235c6fd2807SJeff Garzik 
22368c3c52a8STejun Heo 	/* For PATA drive side cable detection to work, IDENTIFY must
22378c3c52a8STejun Heo 	 * be done backwards such that PDIAG- is released by the slave
22388c3c52a8STejun Heo 	 * device before the master device is identified.
22398c3c52a8STejun Heo 	 */
22400260731fSTejun Heo 	ata_link_for_each_dev_reverse(dev, link) {
2241f58229f8STejun Heo 		unsigned int action = ata_eh_dev_action(dev);
2242f58229f8STejun Heo 		unsigned int readid_flags = 0;
2243c6fd2807SJeff Garzik 
2244bff04647STejun Heo 		if (ehc->i.flags & ATA_EHI_DID_RESET)
2245bff04647STejun Heo 			readid_flags |= ATA_READID_POSTRESET;
2246bff04647STejun Heo 
22479666f400STejun Heo 		if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
2248633273a3STejun Heo 			WARN_ON(dev->class == ATA_DEV_PMP);
2249633273a3STejun Heo 
22500260731fSTejun Heo 			if (ata_link_offline(link)) {
2251c6fd2807SJeff Garzik 				rc = -EIO;
22528c3c52a8STejun Heo 				goto err;
2253c6fd2807SJeff Garzik 			}
2254c6fd2807SJeff Garzik 
22550260731fSTejun Heo 			ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
2256422c9daaSTejun Heo 			rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
2257422c9daaSTejun Heo 						readid_flags);
2258c6fd2807SJeff Garzik 			if (rc)
22598c3c52a8STejun Heo 				goto err;
2260c6fd2807SJeff Garzik 
22610260731fSTejun Heo 			ata_eh_done(link, dev, ATA_EH_REVALIDATE);
2262c6fd2807SJeff Garzik 
2263baa1e78aSTejun Heo 			/* Configuration may have changed, reconfigure
2264baa1e78aSTejun Heo 			 * transfer mode.
2265baa1e78aSTejun Heo 			 */
2266baa1e78aSTejun Heo 			ehc->i.flags |= ATA_EHI_SETMODE;
2267baa1e78aSTejun Heo 
2268c6fd2807SJeff Garzik 			/* schedule the scsi_rescan_device() here */
2269c6fd2807SJeff Garzik 			queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
2270c6fd2807SJeff Garzik 		} else if (dev->class == ATA_DEV_UNKNOWN &&
2271c6fd2807SJeff Garzik 			   ehc->tries[dev->devno] &&
2272c6fd2807SJeff Garzik 			   ata_class_enabled(ehc->classes[dev->devno])) {
2273c6fd2807SJeff Garzik 			dev->class = ehc->classes[dev->devno];
2274c6fd2807SJeff Garzik 
2275633273a3STejun Heo 			if (dev->class == ATA_DEV_PMP)
2276633273a3STejun Heo 				rc = sata_pmp_attach(dev);
2277633273a3STejun Heo 			else
2278633273a3STejun Heo 				rc = ata_dev_read_id(dev, &dev->class,
2279633273a3STejun Heo 						     readid_flags, dev->id);
22808c3c52a8STejun Heo 			switch (rc) {
22818c3c52a8STejun Heo 			case 0:
2282f58229f8STejun Heo 				new_mask |= 1 << dev->devno;
22838c3c52a8STejun Heo 				break;
22848c3c52a8STejun Heo 			case -ENOENT:
228555a8e2c8STejun Heo 				/* IDENTIFY was issued to non-existent
228655a8e2c8STejun Heo 				 * device.  No need to reset.  Just
228755a8e2c8STejun Heo 				 * thaw and kill the device.
228855a8e2c8STejun Heo 				 */
228955a8e2c8STejun Heo 				ata_eh_thaw_port(ap);
229055a8e2c8STejun Heo 				dev->class = ATA_DEV_UNKNOWN;
2291c6fd2807SJeff Garzik 				break;
22928c3c52a8STejun Heo 			default:
22938c3c52a8STejun Heo 				dev->class = ATA_DEV_UNKNOWN;
22948c3c52a8STejun Heo 				goto err;
22958c3c52a8STejun Heo 			}
22968c3c52a8STejun Heo 		}
2297c6fd2807SJeff Garzik 	}
2298c6fd2807SJeff Garzik 
2299c1c4e8d5STejun Heo 	/* PDIAG- should have been released, ask cable type if post-reset */
23009b1e2658STejun Heo 	if (ata_is_host_link(link) && ap->ops->cable_detect &&
23019b1e2658STejun Heo 	    (ehc->i.flags & ATA_EHI_DID_RESET))
2302c1c4e8d5STejun Heo 		ap->cbl = ap->ops->cable_detect(ap);
2303c1c4e8d5STejun Heo 
23048c3c52a8STejun Heo 	/* Configure new devices forward such that user doesn't see
23058c3c52a8STejun Heo 	 * device detection messages backwards.
23068c3c52a8STejun Heo 	 */
23070260731fSTejun Heo 	ata_link_for_each_dev(dev, link) {
2308633273a3STejun Heo 		if (!(new_mask & (1 << dev->devno)) ||
2309633273a3STejun Heo 		    dev->class == ATA_DEV_PMP)
23108c3c52a8STejun Heo 			continue;
23118c3c52a8STejun Heo 
23128c3c52a8STejun Heo 		ehc->i.flags |= ATA_EHI_PRINTINFO;
23138c3c52a8STejun Heo 		rc = ata_dev_configure(dev);
23148c3c52a8STejun Heo 		ehc->i.flags &= ~ATA_EHI_PRINTINFO;
23158c3c52a8STejun Heo 		if (rc)
23168c3c52a8STejun Heo 			goto err;
23178c3c52a8STejun Heo 
2318c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
2319c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
2320c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
2321baa1e78aSTejun Heo 
232255a8e2c8STejun Heo 		/* new device discovered, configure xfermode */
2323baa1e78aSTejun Heo 		ehc->i.flags |= ATA_EHI_SETMODE;
2324c6fd2807SJeff Garzik 	}
2325c6fd2807SJeff Garzik 
23268c3c52a8STejun Heo 	return 0;
23278c3c52a8STejun Heo 
23288c3c52a8STejun Heo  err:
2329c6fd2807SJeff Garzik 	*r_failed_dev = dev;
23308c3c52a8STejun Heo 	DPRINTK("EXIT rc=%d\n", rc);
2331c6fd2807SJeff Garzik 	return rc;
2332c6fd2807SJeff Garzik }
2333c6fd2807SJeff Garzik 
23340260731fSTejun Heo static int ata_link_nr_enabled(struct ata_link *link)
2335c6fd2807SJeff Garzik {
2336f58229f8STejun Heo 	struct ata_device *dev;
2337f58229f8STejun Heo 	int cnt = 0;
2338c6fd2807SJeff Garzik 
23390260731fSTejun Heo 	ata_link_for_each_dev(dev, link)
2340f58229f8STejun Heo 		if (ata_dev_enabled(dev))
2341c6fd2807SJeff Garzik 			cnt++;
2342c6fd2807SJeff Garzik 	return cnt;
2343c6fd2807SJeff Garzik }
2344c6fd2807SJeff Garzik 
23450260731fSTejun Heo static int ata_link_nr_vacant(struct ata_link *link)
2346c6fd2807SJeff Garzik {
2347f58229f8STejun Heo 	struct ata_device *dev;
2348f58229f8STejun Heo 	int cnt = 0;
2349c6fd2807SJeff Garzik 
23500260731fSTejun Heo 	ata_link_for_each_dev(dev, link)
2351f58229f8STejun Heo 		if (dev->class == ATA_DEV_UNKNOWN)
2352c6fd2807SJeff Garzik 			cnt++;
2353c6fd2807SJeff Garzik 	return cnt;
2354c6fd2807SJeff Garzik }
2355c6fd2807SJeff Garzik 
23560260731fSTejun Heo static int ata_eh_skip_recovery(struct ata_link *link)
2357c6fd2807SJeff Garzik {
23580260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
2359f58229f8STejun Heo 	struct ata_device *dev;
2360c6fd2807SJeff Garzik 
2361f9df58cbSTejun Heo 	/* skip disabled links */
2362f9df58cbSTejun Heo 	if (link->flags & ATA_LFLAG_DISABLED)
2363f9df58cbSTejun Heo 		return 1;
2364f9df58cbSTejun Heo 
2365c6fd2807SJeff Garzik 	/* thaw frozen port, resume link and recover failed devices */
23660260731fSTejun Heo 	if ((link->ap->pflags & ATA_PFLAG_FROZEN) ||
23670260731fSTejun Heo 	    (ehc->i.flags & ATA_EHI_RESUME_LINK) || ata_link_nr_enabled(link))
2368c6fd2807SJeff Garzik 		return 0;
2369c6fd2807SJeff Garzik 
2370c6fd2807SJeff Garzik 	/* skip if class codes for all vacant slots are ATA_DEV_NONE */
23710260731fSTejun Heo 	ata_link_for_each_dev(dev, link) {
2372c6fd2807SJeff Garzik 		if (dev->class == ATA_DEV_UNKNOWN &&
2373c6fd2807SJeff Garzik 		    ehc->classes[dev->devno] != ATA_DEV_NONE)
2374c6fd2807SJeff Garzik 			return 0;
2375c6fd2807SJeff Garzik 	}
2376c6fd2807SJeff Garzik 
2377c6fd2807SJeff Garzik 	return 1;
2378c6fd2807SJeff Garzik }
2379c6fd2807SJeff Garzik 
23809b1e2658STejun Heo static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
2381fee7ca72STejun Heo {
23829af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
2383fee7ca72STejun Heo 
2384fee7ca72STejun Heo 	ehc->tries[dev->devno]--;
2385fee7ca72STejun Heo 
2386fee7ca72STejun Heo 	switch (err) {
2387fee7ca72STejun Heo 	case -ENODEV:
2388fee7ca72STejun Heo 		/* device missing or wrong IDENTIFY data, schedule probing */
2389fee7ca72STejun Heo 		ehc->i.probe_mask |= (1 << dev->devno);
2390fee7ca72STejun Heo 	case -EINVAL:
2391fee7ca72STejun Heo 		/* give it just one more chance */
2392fee7ca72STejun Heo 		ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
2393fee7ca72STejun Heo 	case -EIO:
23944fb4615bSTejun Heo 		if (ehc->tries[dev->devno] == 1 && dev->pio_mode > XFER_PIO_0) {
2395fee7ca72STejun Heo 			/* This is the last chance, better to slow
2396fee7ca72STejun Heo 			 * down than lose it.
2397fee7ca72STejun Heo 			 */
2398936fd732STejun Heo 			sata_down_spd_limit(dev->link);
2399fee7ca72STejun Heo 			ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2400fee7ca72STejun Heo 		}
2401fee7ca72STejun Heo 	}
2402fee7ca72STejun Heo 
2403fee7ca72STejun Heo 	if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2404fee7ca72STejun Heo 		/* disable device if it has used up all its chances */
2405fee7ca72STejun Heo 		ata_dev_disable(dev);
2406fee7ca72STejun Heo 
2407fee7ca72STejun Heo 		/* detach if offline */
2408936fd732STejun Heo 		if (ata_link_offline(dev->link))
2409fee7ca72STejun Heo 			ata_eh_detach_dev(dev);
2410fee7ca72STejun Heo 
2411fee7ca72STejun Heo 		/* probe if requested */
2412fee7ca72STejun Heo 		if ((ehc->i.probe_mask & (1 << dev->devno)) &&
2413fee7ca72STejun Heo 		    !(ehc->did_probe_mask & (1 << dev->devno))) {
2414fee7ca72STejun Heo 			ata_eh_detach_dev(dev);
2415fee7ca72STejun Heo 			ata_dev_init(dev);
2416fee7ca72STejun Heo 
2417fee7ca72STejun Heo 			ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2418fee7ca72STejun Heo 			ehc->did_probe_mask |= (1 << dev->devno);
2419fee7ca72STejun Heo 			ehc->i.action |= ATA_EH_SOFTRESET;
2420fee7ca72STejun Heo 		}
24219b1e2658STejun Heo 
24229b1e2658STejun Heo 		return 1;
2423fee7ca72STejun Heo 	} else {
2424fee7ca72STejun Heo 		/* soft didn't work?  be haaaaard */
2425fee7ca72STejun Heo 		if (ehc->i.flags & ATA_EHI_DID_RESET)
2426fee7ca72STejun Heo 			ehc->i.action |= ATA_EH_HARDRESET;
2427fee7ca72STejun Heo 		else
2428fee7ca72STejun Heo 			ehc->i.action |= ATA_EH_SOFTRESET;
24299b1e2658STejun Heo 
24309b1e2658STejun Heo 		return 0;
2431fee7ca72STejun Heo 	}
2432fee7ca72STejun Heo }
2433fee7ca72STejun Heo 
2434c6fd2807SJeff Garzik /**
2435c6fd2807SJeff Garzik  *	ata_eh_recover - recover host port after error
2436c6fd2807SJeff Garzik  *	@ap: host port to recover
2437c6fd2807SJeff Garzik  *	@prereset: prereset method (can be NULL)
2438c6fd2807SJeff Garzik  *	@softreset: softreset method (can be NULL)
2439c6fd2807SJeff Garzik  *	@hardreset: hardreset method (can be NULL)
2440c6fd2807SJeff Garzik  *	@postreset: postreset method (can be NULL)
24419b1e2658STejun Heo  *	@r_failed_link: out parameter for failed link
2442c6fd2807SJeff Garzik  *
2443c6fd2807SJeff Garzik  *	This is the alpha and omega, eum and yang, heart and soul of
2444c6fd2807SJeff Garzik  *	libata exception handling.  On entry, actions required to
24459b1e2658STejun Heo  *	recover each link and hotplug requests are recorded in the
24469b1e2658STejun Heo  *	link's eh_context.  This function executes all the operations
24479b1e2658STejun Heo  *	with appropriate retrials and fallbacks to resurrect failed
2448c6fd2807SJeff Garzik  *	devices, detach goners and greet newcomers.
2449c6fd2807SJeff Garzik  *
2450c6fd2807SJeff Garzik  *	LOCKING:
2451c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
2452c6fd2807SJeff Garzik  *
2453c6fd2807SJeff Garzik  *	RETURNS:
2454c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
2455c6fd2807SJeff Garzik  */
2456fb7fd614STejun Heo int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
2457c6fd2807SJeff Garzik 		   ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
24589b1e2658STejun Heo 		   ata_postreset_fn_t postreset,
24599b1e2658STejun Heo 		   struct ata_link **r_failed_link)
2460c6fd2807SJeff Garzik {
24619b1e2658STejun Heo 	struct ata_link *link;
2462c6fd2807SJeff Garzik 	struct ata_device *dev;
24639b1e2658STejun Heo 	int nr_failed_devs, nr_disabled_devs;
24649b1e2658STejun Heo 	int reset, rc;
2465f9df58cbSTejun Heo 	unsigned long flags;
2466c6fd2807SJeff Garzik 
2467c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
2468c6fd2807SJeff Garzik 
2469c6fd2807SJeff Garzik 	/* prep for recovery */
24709b1e2658STejun Heo 	ata_port_for_each_link(link, ap) {
24719b1e2658STejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
24729b1e2658STejun Heo 
2473f9df58cbSTejun Heo 		/* re-enable link? */
2474f9df58cbSTejun Heo 		if (ehc->i.action & ATA_EH_ENABLE_LINK) {
2475f9df58cbSTejun Heo 			ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
2476f9df58cbSTejun Heo 			spin_lock_irqsave(ap->lock, flags);
2477f9df58cbSTejun Heo 			link->flags &= ~ATA_LFLAG_DISABLED;
2478f9df58cbSTejun Heo 			spin_unlock_irqrestore(ap->lock, flags);
2479f9df58cbSTejun Heo 			ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
2480f9df58cbSTejun Heo 		}
2481f9df58cbSTejun Heo 
24820260731fSTejun Heo 		ata_link_for_each_dev(dev, link) {
2483fd995f70STejun Heo 			if (link->flags & ATA_LFLAG_NO_RETRY)
2484fd995f70STejun Heo 				ehc->tries[dev->devno] = 1;
2485fd995f70STejun Heo 			else
2486c6fd2807SJeff Garzik 				ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2487c6fd2807SJeff Garzik 
248879a55b72STejun Heo 			/* collect port action mask recorded in dev actions */
24899b1e2658STejun Heo 			ehc->i.action |= ehc->i.dev_action[dev->devno] &
24909b1e2658STejun Heo 					 ~ATA_EH_PERDEV_MASK;
2491f58229f8STejun Heo 			ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
249279a55b72STejun Heo 
2493c6fd2807SJeff Garzik 			/* process hotplug request */
2494c6fd2807SJeff Garzik 			if (dev->flags & ATA_DFLAG_DETACH)
2495c6fd2807SJeff Garzik 				ata_eh_detach_dev(dev);
2496c6fd2807SJeff Garzik 
2497c6fd2807SJeff Garzik 			if (!ata_dev_enabled(dev) &&
2498c6fd2807SJeff Garzik 			    ((ehc->i.probe_mask & (1 << dev->devno)) &&
2499c6fd2807SJeff Garzik 			     !(ehc->did_probe_mask & (1 << dev->devno)))) {
2500c6fd2807SJeff Garzik 				ata_eh_detach_dev(dev);
2501c6fd2807SJeff Garzik 				ata_dev_init(dev);
2502c6fd2807SJeff Garzik 				ehc->did_probe_mask |= (1 << dev->devno);
2503c6fd2807SJeff Garzik 				ehc->i.action |= ATA_EH_SOFTRESET;
2504c6fd2807SJeff Garzik 			}
2505c6fd2807SJeff Garzik 		}
25069b1e2658STejun Heo 	}
2507c6fd2807SJeff Garzik 
2508c6fd2807SJeff Garzik  retry:
2509c6fd2807SJeff Garzik 	rc = 0;
25109b1e2658STejun Heo 	nr_failed_devs = 0;
25119b1e2658STejun Heo 	nr_disabled_devs = 0;
25129b1e2658STejun Heo 	reset = 0;
2513c6fd2807SJeff Garzik 
2514c6fd2807SJeff Garzik 	/* if UNLOADING, finish immediately */
2515c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_UNLOADING)
2516c6fd2807SJeff Garzik 		goto out;
2517c6fd2807SJeff Garzik 
25189b1e2658STejun Heo 	/* prep for EH */
25199b1e2658STejun Heo 	ata_port_for_each_link(link, ap) {
25209b1e2658STejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
25219b1e2658STejun Heo 
2522c6fd2807SJeff Garzik 		/* skip EH if possible. */
25230260731fSTejun Heo 		if (ata_eh_skip_recovery(link))
2524c6fd2807SJeff Garzik 			ehc->i.action = 0;
2525c6fd2807SJeff Garzik 
25269b1e2658STejun Heo 		/* do we need to reset? */
25279b1e2658STejun Heo 		if (ehc->i.action & ATA_EH_RESET_MASK)
25289b1e2658STejun Heo 			reset = 1;
25299b1e2658STejun Heo 
25300260731fSTejun Heo 		ata_link_for_each_dev(dev, link)
2531f58229f8STejun Heo 			ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
25329b1e2658STejun Heo 	}
2533c6fd2807SJeff Garzik 
2534c6fd2807SJeff Garzik 	/* reset */
25359b1e2658STejun Heo 	if (reset) {
2536b06ce3e5STejun Heo 		/* if PMP is attached, this function only deals with
2537b06ce3e5STejun Heo 		 * downstream links, port should stay thawed.
2538b06ce3e5STejun Heo 		 */
2539b06ce3e5STejun Heo 		if (!ap->nr_pmp_links)
2540c6fd2807SJeff Garzik 			ata_eh_freeze_port(ap);
2541c6fd2807SJeff Garzik 
25429b1e2658STejun Heo 		ata_port_for_each_link(link, ap) {
25439b1e2658STejun Heo 			struct ata_eh_context *ehc = &link->eh_context;
25449b1e2658STejun Heo 
25459b1e2658STejun Heo 			if (!(ehc->i.action & ATA_EH_RESET_MASK))
25469b1e2658STejun Heo 				continue;
25479b1e2658STejun Heo 
25489b1e2658STejun Heo 			rc = ata_eh_reset(link, ata_link_nr_vacant(link),
25499b1e2658STejun Heo 					  prereset, softreset, hardreset,
25509b1e2658STejun Heo 					  postreset);
2551c6fd2807SJeff Garzik 			if (rc) {
25520260731fSTejun Heo 				ata_link_printk(link, KERN_ERR,
2553c6fd2807SJeff Garzik 						"reset failed, giving up\n");
2554c6fd2807SJeff Garzik 				goto out;
2555c6fd2807SJeff Garzik 			}
25569b1e2658STejun Heo 		}
2557c6fd2807SJeff Garzik 
2558b06ce3e5STejun Heo 		if (!ap->nr_pmp_links)
2559c6fd2807SJeff Garzik 			ata_eh_thaw_port(ap);
2560c6fd2807SJeff Garzik 	}
2561c6fd2807SJeff Garzik 
25629b1e2658STejun Heo 	/* the rest */
25639b1e2658STejun Heo 	ata_port_for_each_link(link, ap) {
25649b1e2658STejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
25659b1e2658STejun Heo 
2566c6fd2807SJeff Garzik 		/* revalidate existing devices and attach new ones */
25670260731fSTejun Heo 		rc = ata_eh_revalidate_and_attach(link, &dev);
2568c6fd2807SJeff Garzik 		if (rc)
2569c6fd2807SJeff Garzik 			goto dev_fail;
2570c6fd2807SJeff Garzik 
2571633273a3STejun Heo 		/* if PMP got attached, return, pmp EH will take care of it */
2572633273a3STejun Heo 		if (link->device->class == ATA_DEV_PMP) {
2573633273a3STejun Heo 			ehc->i.action = 0;
2574633273a3STejun Heo 			return 0;
2575633273a3STejun Heo 		}
2576633273a3STejun Heo 
2577baa1e78aSTejun Heo 		/* configure transfer mode if necessary */
2578baa1e78aSTejun Heo 		if (ehc->i.flags & ATA_EHI_SETMODE) {
25790260731fSTejun Heo 			rc = ata_set_mode(link, &dev);
25804ae72a1eSTejun Heo 			if (rc)
2581c6fd2807SJeff Garzik 				goto dev_fail;
2582baa1e78aSTejun Heo 			ehc->i.flags &= ~ATA_EHI_SETMODE;
2583c6fd2807SJeff Garzik 		}
2584c6fd2807SJeff Garzik 
2585ca77329fSKristen Carlson Accardi 		if (ehc->i.action & ATA_EHI_LPM)
2586ca77329fSKristen Carlson Accardi 			ata_link_for_each_dev(dev, link)
2587ca77329fSKristen Carlson Accardi 				ata_dev_enable_pm(dev, ap->pm_policy);
2588ca77329fSKristen Carlson Accardi 
25899b1e2658STejun Heo 		/* this link is okay now */
25909b1e2658STejun Heo 		ehc->i.flags = 0;
25919b1e2658STejun Heo 		continue;
2592c6fd2807SJeff Garzik 
2593c6fd2807SJeff Garzik dev_fail:
25949b1e2658STejun Heo 		nr_failed_devs++;
25959b1e2658STejun Heo 		if (ata_eh_handle_dev_fail(dev, rc))
25969b1e2658STejun Heo 			nr_disabled_devs++;
2597c6fd2807SJeff Garzik 
2598b06ce3e5STejun Heo 		if (ap->pflags & ATA_PFLAG_FROZEN) {
2599b06ce3e5STejun Heo 			/* PMP reset requires working host port.
2600b06ce3e5STejun Heo 			 * Can't retry if it's frozen.
2601b06ce3e5STejun Heo 			 */
2602b06ce3e5STejun Heo 			if (ap->nr_pmp_links)
2603b06ce3e5STejun Heo 				goto out;
26049b1e2658STejun Heo 			break;
26059b1e2658STejun Heo 		}
2606b06ce3e5STejun Heo 	}
26079b1e2658STejun Heo 
26089b1e2658STejun Heo 	if (nr_failed_devs) {
26099b1e2658STejun Heo 		if (nr_failed_devs != nr_disabled_devs) {
26109b1e2658STejun Heo 			ata_port_printk(ap, KERN_WARNING, "failed to recover "
26119b1e2658STejun Heo 					"some devices, retrying in 5 secs\n");
2612c6fd2807SJeff Garzik 			ssleep(5);
2613c6fd2807SJeff Garzik 		} else {
26149b1e2658STejun Heo 			/* no device left to recover, repeat fast */
2615c6fd2807SJeff Garzik 			msleep(500);
2616c6fd2807SJeff Garzik 		}
2617c6fd2807SJeff Garzik 
2618c6fd2807SJeff Garzik 		goto retry;
26199b1e2658STejun Heo 	}
2620c6fd2807SJeff Garzik 
2621c6fd2807SJeff Garzik  out:
26229b1e2658STejun Heo 	if (rc && r_failed_link)
26239b1e2658STejun Heo 		*r_failed_link = link;
2624c6fd2807SJeff Garzik 
2625c6fd2807SJeff Garzik 	DPRINTK("EXIT, rc=%d\n", rc);
2626c6fd2807SJeff Garzik 	return rc;
2627c6fd2807SJeff Garzik }
2628c6fd2807SJeff Garzik 
2629c6fd2807SJeff Garzik /**
2630c6fd2807SJeff Garzik  *	ata_eh_finish - finish up EH
2631c6fd2807SJeff Garzik  *	@ap: host port to finish EH for
2632c6fd2807SJeff Garzik  *
2633c6fd2807SJeff Garzik  *	Recovery is complete.  Clean up EH states and retry or finish
2634c6fd2807SJeff Garzik  *	failed qcs.
2635c6fd2807SJeff Garzik  *
2636c6fd2807SJeff Garzik  *	LOCKING:
2637c6fd2807SJeff Garzik  *	None.
2638c6fd2807SJeff Garzik  */
2639fb7fd614STejun Heo void ata_eh_finish(struct ata_port *ap)
2640c6fd2807SJeff Garzik {
2641c6fd2807SJeff Garzik 	int tag;
2642c6fd2807SJeff Garzik 
2643c6fd2807SJeff Garzik 	/* retry or finish qcs */
2644c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2645c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2646c6fd2807SJeff Garzik 
2647c6fd2807SJeff Garzik 		if (!(qc->flags & ATA_QCFLAG_FAILED))
2648c6fd2807SJeff Garzik 			continue;
2649c6fd2807SJeff Garzik 
2650c6fd2807SJeff Garzik 		if (qc->err_mask) {
2651c6fd2807SJeff Garzik 			/* FIXME: Once EH migration is complete,
2652c6fd2807SJeff Garzik 			 * generate sense data in this function,
2653c6fd2807SJeff Garzik 			 * considering both err_mask and tf.
2654f90f0828STejun Heo 			 *
2655f90f0828STejun Heo 			 * There's no point in retrying invalid
2656f90f0828STejun Heo 			 * (detected by libata) and non-IO device
2657f90f0828STejun Heo 			 * errors (rejected by device).  Finish them
2658f90f0828STejun Heo 			 * immediately.
2659c6fd2807SJeff Garzik 			 */
2660f90f0828STejun Heo 			if ((qc->err_mask & AC_ERR_INVALID) ||
2661f90f0828STejun Heo 			    (!(qc->flags & ATA_QCFLAG_IO) &&
2662f90f0828STejun Heo 			     qc->err_mask == AC_ERR_DEV))
2663c6fd2807SJeff Garzik 				ata_eh_qc_complete(qc);
2664c6fd2807SJeff Garzik 			else
2665c6fd2807SJeff Garzik 				ata_eh_qc_retry(qc);
2666c6fd2807SJeff Garzik 		} else {
2667c6fd2807SJeff Garzik 			if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2668c6fd2807SJeff Garzik 				ata_eh_qc_complete(qc);
2669c6fd2807SJeff Garzik 			} else {
2670c6fd2807SJeff Garzik 				/* feed zero TF to sense generation */
2671c6fd2807SJeff Garzik 				memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2672c6fd2807SJeff Garzik 				ata_eh_qc_retry(qc);
2673c6fd2807SJeff Garzik 			}
2674c6fd2807SJeff Garzik 		}
2675c6fd2807SJeff Garzik 	}
2676da917d69STejun Heo 
2677da917d69STejun Heo 	/* make sure nr_active_links is zero after EH */
2678da917d69STejun Heo 	WARN_ON(ap->nr_active_links);
2679da917d69STejun Heo 	ap->nr_active_links = 0;
2680c6fd2807SJeff Garzik }
2681c6fd2807SJeff Garzik 
2682c6fd2807SJeff Garzik /**
2683c6fd2807SJeff Garzik  *	ata_do_eh - do standard error handling
2684c6fd2807SJeff Garzik  *	@ap: host port to handle error for
2685c6fd2807SJeff Garzik  *	@prereset: prereset method (can be NULL)
2686c6fd2807SJeff Garzik  *	@softreset: softreset method (can be NULL)
2687c6fd2807SJeff Garzik  *	@hardreset: hardreset method (can be NULL)
2688c6fd2807SJeff Garzik  *	@postreset: postreset method (can be NULL)
2689c6fd2807SJeff Garzik  *
2690c6fd2807SJeff Garzik  *	Perform standard error handling sequence.
2691c6fd2807SJeff Garzik  *
2692c6fd2807SJeff Garzik  *	LOCKING:
2693c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
2694c6fd2807SJeff Garzik  */
2695c6fd2807SJeff Garzik void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2696c6fd2807SJeff Garzik 	       ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2697c6fd2807SJeff Garzik 	       ata_postreset_fn_t postreset)
2698c6fd2807SJeff Garzik {
26999b1e2658STejun Heo 	struct ata_device *dev;
27009b1e2658STejun Heo 	int rc;
27019b1e2658STejun Heo 
27029b1e2658STejun Heo 	ata_eh_autopsy(ap);
27039b1e2658STejun Heo 	ata_eh_report(ap);
27049b1e2658STejun Heo 
27059b1e2658STejun Heo 	rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
27069b1e2658STejun Heo 			    NULL);
27079b1e2658STejun Heo 	if (rc) {
27089b1e2658STejun Heo 		ata_link_for_each_dev(dev, &ap->link)
27099b1e2658STejun Heo 			ata_dev_disable(dev);
27109b1e2658STejun Heo 	}
27119b1e2658STejun Heo 
2712c6fd2807SJeff Garzik 	ata_eh_finish(ap);
2713c6fd2807SJeff Garzik }
2714c6fd2807SJeff Garzik 
27156ffa01d8STejun Heo #ifdef CONFIG_PM
2716c6fd2807SJeff Garzik /**
2717c6fd2807SJeff Garzik  *	ata_eh_handle_port_suspend - perform port suspend operation
2718c6fd2807SJeff Garzik  *	@ap: port to suspend
2719c6fd2807SJeff Garzik  *
2720c6fd2807SJeff Garzik  *	Suspend @ap.
2721c6fd2807SJeff Garzik  *
2722c6fd2807SJeff Garzik  *	LOCKING:
2723c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
2724c6fd2807SJeff Garzik  */
2725c6fd2807SJeff Garzik static void ata_eh_handle_port_suspend(struct ata_port *ap)
2726c6fd2807SJeff Garzik {
2727c6fd2807SJeff Garzik 	unsigned long flags;
2728c6fd2807SJeff Garzik 	int rc = 0;
2729c6fd2807SJeff Garzik 
2730c6fd2807SJeff Garzik 	/* are we suspending? */
2731c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
2732c6fd2807SJeff Garzik 	if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2733c6fd2807SJeff Garzik 	    ap->pm_mesg.event == PM_EVENT_ON) {
2734c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
2735c6fd2807SJeff Garzik 		return;
2736c6fd2807SJeff Garzik 	}
2737c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
2738c6fd2807SJeff Garzik 
2739c6fd2807SJeff Garzik 	WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
2740c6fd2807SJeff Garzik 
274164578a3dSTejun Heo 	/* tell ACPI we're suspending */
274264578a3dSTejun Heo 	rc = ata_acpi_on_suspend(ap);
274364578a3dSTejun Heo 	if (rc)
274464578a3dSTejun Heo 		goto out;
274564578a3dSTejun Heo 
2746c6fd2807SJeff Garzik 	/* suspend */
2747c6fd2807SJeff Garzik 	ata_eh_freeze_port(ap);
2748c6fd2807SJeff Garzik 
2749c6fd2807SJeff Garzik 	if (ap->ops->port_suspend)
2750c6fd2807SJeff Garzik 		rc = ap->ops->port_suspend(ap, ap->pm_mesg);
2751c6fd2807SJeff Garzik 
275264578a3dSTejun Heo  out:
2753c6fd2807SJeff Garzik 	/* report result */
2754c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
2755c6fd2807SJeff Garzik 
2756c6fd2807SJeff Garzik 	ap->pflags &= ~ATA_PFLAG_PM_PENDING;
2757c6fd2807SJeff Garzik 	if (rc == 0)
2758c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_SUSPENDED;
275964578a3dSTejun Heo 	else if (ap->pflags & ATA_PFLAG_FROZEN)
2760c6fd2807SJeff Garzik 		ata_port_schedule_eh(ap);
2761c6fd2807SJeff Garzik 
2762c6fd2807SJeff Garzik 	if (ap->pm_result) {
2763c6fd2807SJeff Garzik 		*ap->pm_result = rc;
2764c6fd2807SJeff Garzik 		ap->pm_result = NULL;
2765c6fd2807SJeff Garzik 	}
2766c6fd2807SJeff Garzik 
2767c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
2768c6fd2807SJeff Garzik 
2769c6fd2807SJeff Garzik 	return;
2770c6fd2807SJeff Garzik }
2771c6fd2807SJeff Garzik 
2772c6fd2807SJeff Garzik /**
2773c6fd2807SJeff Garzik  *	ata_eh_handle_port_resume - perform port resume operation
2774c6fd2807SJeff Garzik  *	@ap: port to resume
2775c6fd2807SJeff Garzik  *
2776c6fd2807SJeff Garzik  *	Resume @ap.
2777c6fd2807SJeff Garzik  *
2778c6fd2807SJeff Garzik  *	LOCKING:
2779c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
2780c6fd2807SJeff Garzik  */
2781c6fd2807SJeff Garzik static void ata_eh_handle_port_resume(struct ata_port *ap)
2782c6fd2807SJeff Garzik {
2783c6fd2807SJeff Garzik 	unsigned long flags;
27849666f400STejun Heo 	int rc = 0;
2785c6fd2807SJeff Garzik 
2786c6fd2807SJeff Garzik 	/* are we resuming? */
2787c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
2788c6fd2807SJeff Garzik 	if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2789c6fd2807SJeff Garzik 	    ap->pm_mesg.event != PM_EVENT_ON) {
2790c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
2791c6fd2807SJeff Garzik 		return;
2792c6fd2807SJeff Garzik 	}
2793c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
2794c6fd2807SJeff Garzik 
27959666f400STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
2796c6fd2807SJeff Garzik 
2797c6fd2807SJeff Garzik 	if (ap->ops->port_resume)
2798c6fd2807SJeff Garzik 		rc = ap->ops->port_resume(ap);
2799c6fd2807SJeff Garzik 
28006746544cSTejun Heo 	/* tell ACPI that we're resuming */
28016746544cSTejun Heo 	ata_acpi_on_resume(ap);
28026746544cSTejun Heo 
28039666f400STejun Heo 	/* report result */
2804c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
2805c6fd2807SJeff Garzik 	ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
2806c6fd2807SJeff Garzik 	if (ap->pm_result) {
2807c6fd2807SJeff Garzik 		*ap->pm_result = rc;
2808c6fd2807SJeff Garzik 		ap->pm_result = NULL;
2809c6fd2807SJeff Garzik 	}
2810c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
2811c6fd2807SJeff Garzik }
28126ffa01d8STejun Heo #endif /* CONFIG_PM */
2813