xref: /openbmc/linux/drivers/ata/libata-eh.c (revision 3eabddb8ed4f488664ff5d67968392bb424836a3)
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 {
493884f7b0STejun Heo 	/* speed down verdicts */
507d47e8d4STejun Heo 	ATA_EH_SPDN_NCQ_OFF		= (1 << 0),
517d47e8d4STejun Heo 	ATA_EH_SPDN_SPEED_DOWN		= (1 << 1),
527d47e8d4STejun Heo 	ATA_EH_SPDN_FALLBACK_TO_PIO	= (1 << 2),
5376326ac1STejun Heo 	ATA_EH_SPDN_KEEP_ERRORS		= (1 << 3),
543884f7b0STejun Heo 
553884f7b0STejun Heo 	/* error flags */
563884f7b0STejun Heo 	ATA_EFLAG_IS_IO			= (1 << 0),
5776326ac1STejun Heo 	ATA_EFLAG_DUBIOUS_XFER		= (1 << 1),
583884f7b0STejun Heo 
593884f7b0STejun Heo 	/* error categories */
603884f7b0STejun Heo 	ATA_ECAT_NONE			= 0,
613884f7b0STejun Heo 	ATA_ECAT_ATA_BUS		= 1,
623884f7b0STejun Heo 	ATA_ECAT_TOUT_HSM		= 2,
633884f7b0STejun Heo 	ATA_ECAT_UNK_DEV		= 3,
6475f9cafcSTejun Heo 	ATA_ECAT_DUBIOUS_NONE		= 4,
6575f9cafcSTejun Heo 	ATA_ECAT_DUBIOUS_ATA_BUS	= 5,
6675f9cafcSTejun Heo 	ATA_ECAT_DUBIOUS_TOUT_HSM	= 6,
6775f9cafcSTejun Heo 	ATA_ECAT_DUBIOUS_UNK_DEV	= 7,
6875f9cafcSTejun Heo 	ATA_ECAT_NR			= 8,
697d47e8d4STejun Heo 
7087fbc5a0STejun Heo 	ATA_EH_CMD_DFL_TIMEOUT		=  5000,
7187fbc5a0STejun Heo 
720a2c0f56STejun Heo 	/* always put at least this amount of time between resets */
730a2c0f56STejun Heo 	ATA_EH_RESET_COOL_DOWN		=  5000,
740a2c0f56STejun Heo 
75341c2c95STejun Heo 	/* Waiting in ->prereset can never be reliable.  It's
76341c2c95STejun Heo 	 * sometimes nice to wait there but it can't be depended upon;
77341c2c95STejun Heo 	 * otherwise, we wouldn't be resetting.  Just give it enough
78341c2c95STejun Heo 	 * time for most drives to spin up.
7931daabdaSTejun Heo 	 */
80341c2c95STejun Heo 	ATA_EH_PRERESET_TIMEOUT		= 10000,
81341c2c95STejun Heo 	ATA_EH_FASTDRAIN_INTERVAL	=  3000,
8231daabdaSTejun Heo };
8331daabdaSTejun Heo 
8431daabdaSTejun Heo /* The following table determines how we sequence resets.  Each entry
8531daabdaSTejun Heo  * represents timeout for that try.  The first try can be soft or
8631daabdaSTejun Heo  * hardreset.  All others are hardreset if available.  In most cases
8731daabdaSTejun Heo  * the first reset w/ 10sec timeout should succeed.  Following entries
8831daabdaSTejun Heo  * are mostly for error handling, hotplug and retarded devices.
8931daabdaSTejun Heo  */
9031daabdaSTejun Heo static const unsigned long ata_eh_reset_timeouts[] = {
91341c2c95STejun Heo 	10000,	/* most drives spin up by 10sec */
92341c2c95STejun Heo 	10000,	/* > 99% working drives spin up before 20sec */
93341c2c95STejun Heo 	35000,	/* give > 30 secs of idleness for retarded devices */
94341c2c95STejun Heo 	 5000,	/* and sweet one last chance */
95d8af0eb6STejun Heo 	ULONG_MAX, /* > 1 min has elapsed, give up */
9631daabdaSTejun Heo };
9731daabdaSTejun Heo 
9887fbc5a0STejun Heo static const unsigned long ata_eh_identify_timeouts[] = {
9987fbc5a0STejun Heo 	 5000,	/* covers > 99% of successes and not too boring on failures */
10087fbc5a0STejun Heo 	10000,  /* combined time till here is enough even for media access */
10187fbc5a0STejun Heo 	30000,	/* for true idiots */
10287fbc5a0STejun Heo 	ULONG_MAX,
10387fbc5a0STejun Heo };
10487fbc5a0STejun Heo 
10587fbc5a0STejun Heo static const unsigned long ata_eh_other_timeouts[] = {
10687fbc5a0STejun Heo 	 5000,	/* same rationale as identify timeout */
10787fbc5a0STejun Heo 	10000,	/* ditto */
10887fbc5a0STejun Heo 	/* but no merciful 30sec for other commands, it just isn't worth it */
10987fbc5a0STejun Heo 	ULONG_MAX,
11087fbc5a0STejun Heo };
11187fbc5a0STejun Heo 
11287fbc5a0STejun Heo struct ata_eh_cmd_timeout_ent {
11387fbc5a0STejun Heo 	const u8		*commands;
11487fbc5a0STejun Heo 	const unsigned long	*timeouts;
11587fbc5a0STejun Heo };
11687fbc5a0STejun Heo 
11787fbc5a0STejun Heo /* The following table determines timeouts to use for EH internal
11887fbc5a0STejun Heo  * commands.  Each table entry is a command class and matches the
11987fbc5a0STejun Heo  * commands the entry applies to and the timeout table to use.
12087fbc5a0STejun Heo  *
12187fbc5a0STejun Heo  * On the retry after a command timed out, the next timeout value from
12287fbc5a0STejun Heo  * the table is used.  If the table doesn't contain further entries,
12387fbc5a0STejun Heo  * the last value is used.
12487fbc5a0STejun Heo  *
12587fbc5a0STejun Heo  * ehc->cmd_timeout_idx keeps track of which timeout to use per
12687fbc5a0STejun Heo  * command class, so if SET_FEATURES times out on the first try, the
12787fbc5a0STejun Heo  * next try will use the second timeout value only for that class.
12887fbc5a0STejun Heo  */
12987fbc5a0STejun Heo #define CMDS(cmds...)	(const u8 []){ cmds, 0 }
13087fbc5a0STejun Heo static const struct ata_eh_cmd_timeout_ent
13187fbc5a0STejun Heo ata_eh_cmd_timeout_table[ATA_EH_CMD_TIMEOUT_TABLE_SIZE] = {
13287fbc5a0STejun Heo 	{ .commands = CMDS(ATA_CMD_ID_ATA, ATA_CMD_ID_ATAPI),
13387fbc5a0STejun Heo 	  .timeouts = ata_eh_identify_timeouts, },
13487fbc5a0STejun Heo 	{ .commands = CMDS(ATA_CMD_READ_NATIVE_MAX, ATA_CMD_READ_NATIVE_MAX_EXT),
13587fbc5a0STejun Heo 	  .timeouts = ata_eh_other_timeouts, },
13687fbc5a0STejun Heo 	{ .commands = CMDS(ATA_CMD_SET_MAX, ATA_CMD_SET_MAX_EXT),
13787fbc5a0STejun Heo 	  .timeouts = ata_eh_other_timeouts, },
13887fbc5a0STejun Heo 	{ .commands = CMDS(ATA_CMD_SET_FEATURES),
13987fbc5a0STejun Heo 	  .timeouts = ata_eh_other_timeouts, },
14087fbc5a0STejun Heo 	{ .commands = CMDS(ATA_CMD_INIT_DEV_PARAMS),
14187fbc5a0STejun Heo 	  .timeouts = ata_eh_other_timeouts, },
14287fbc5a0STejun Heo };
14387fbc5a0STejun Heo #undef CMDS
14487fbc5a0STejun Heo 
145c6fd2807SJeff Garzik static void __ata_port_freeze(struct ata_port *ap);
1466ffa01d8STejun Heo #ifdef CONFIG_PM
147c6fd2807SJeff Garzik static void ata_eh_handle_port_suspend(struct ata_port *ap);
148c6fd2807SJeff Garzik static void ata_eh_handle_port_resume(struct ata_port *ap);
1496ffa01d8STejun Heo #else /* CONFIG_PM */
1506ffa01d8STejun Heo static void ata_eh_handle_port_suspend(struct ata_port *ap)
1516ffa01d8STejun Heo { }
1526ffa01d8STejun Heo 
1536ffa01d8STejun Heo static void ata_eh_handle_port_resume(struct ata_port *ap)
1546ffa01d8STejun Heo { }
1556ffa01d8STejun Heo #endif /* CONFIG_PM */
156c6fd2807SJeff Garzik 
157b64bbc39STejun Heo static void __ata_ehi_pushv_desc(struct ata_eh_info *ehi, const char *fmt,
158b64bbc39STejun Heo 				 va_list args)
159b64bbc39STejun Heo {
160b64bbc39STejun Heo 	ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
161b64bbc39STejun Heo 				     ATA_EH_DESC_LEN - ehi->desc_len,
162b64bbc39STejun Heo 				     fmt, args);
163b64bbc39STejun Heo }
164b64bbc39STejun Heo 
165b64bbc39STejun Heo /**
166b64bbc39STejun Heo  *	__ata_ehi_push_desc - push error description without adding separator
167b64bbc39STejun Heo  *	@ehi: target EHI
168b64bbc39STejun Heo  *	@fmt: printf format string
169b64bbc39STejun Heo  *
170b64bbc39STejun Heo  *	Format string according to @fmt and append it to @ehi->desc.
171b64bbc39STejun Heo  *
172b64bbc39STejun Heo  *	LOCKING:
173b64bbc39STejun Heo  *	spin_lock_irqsave(host lock)
174b64bbc39STejun Heo  */
175b64bbc39STejun Heo void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
176b64bbc39STejun Heo {
177b64bbc39STejun Heo 	va_list args;
178b64bbc39STejun Heo 
179b64bbc39STejun Heo 	va_start(args, fmt);
180b64bbc39STejun Heo 	__ata_ehi_pushv_desc(ehi, fmt, args);
181b64bbc39STejun Heo 	va_end(args);
182b64bbc39STejun Heo }
183b64bbc39STejun Heo 
184b64bbc39STejun Heo /**
185b64bbc39STejun Heo  *	ata_ehi_push_desc - push error description with separator
186b64bbc39STejun Heo  *	@ehi: target EHI
187b64bbc39STejun Heo  *	@fmt: printf format string
188b64bbc39STejun Heo  *
189b64bbc39STejun Heo  *	Format string according to @fmt and append it to @ehi->desc.
190b64bbc39STejun Heo  *	If @ehi->desc is not empty, ", " is added in-between.
191b64bbc39STejun Heo  *
192b64bbc39STejun Heo  *	LOCKING:
193b64bbc39STejun Heo  *	spin_lock_irqsave(host lock)
194b64bbc39STejun Heo  */
195b64bbc39STejun Heo void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
196b64bbc39STejun Heo {
197b64bbc39STejun Heo 	va_list args;
198b64bbc39STejun Heo 
199b64bbc39STejun Heo 	if (ehi->desc_len)
200b64bbc39STejun Heo 		__ata_ehi_push_desc(ehi, ", ");
201b64bbc39STejun Heo 
202b64bbc39STejun Heo 	va_start(args, fmt);
203b64bbc39STejun Heo 	__ata_ehi_pushv_desc(ehi, fmt, args);
204b64bbc39STejun Heo 	va_end(args);
205b64bbc39STejun Heo }
206b64bbc39STejun Heo 
207b64bbc39STejun Heo /**
208b64bbc39STejun Heo  *	ata_ehi_clear_desc - clean error description
209b64bbc39STejun Heo  *	@ehi: target EHI
210b64bbc39STejun Heo  *
211b64bbc39STejun Heo  *	Clear @ehi->desc.
212b64bbc39STejun Heo  *
213b64bbc39STejun Heo  *	LOCKING:
214b64bbc39STejun Heo  *	spin_lock_irqsave(host lock)
215b64bbc39STejun Heo  */
216b64bbc39STejun Heo void ata_ehi_clear_desc(struct ata_eh_info *ehi)
217b64bbc39STejun Heo {
218b64bbc39STejun Heo 	ehi->desc[0] = '\0';
219b64bbc39STejun Heo 	ehi->desc_len = 0;
220b64bbc39STejun Heo }
221b64bbc39STejun Heo 
222cbcdd875STejun Heo /**
223cbcdd875STejun Heo  *	ata_port_desc - append port description
224cbcdd875STejun Heo  *	@ap: target ATA port
225cbcdd875STejun Heo  *	@fmt: printf format string
226cbcdd875STejun Heo  *
227cbcdd875STejun Heo  *	Format string according to @fmt and append it to port
228cbcdd875STejun Heo  *	description.  If port description is not empty, " " is added
229cbcdd875STejun Heo  *	in-between.  This function is to be used while initializing
230cbcdd875STejun Heo  *	ata_host.  The description is printed on host registration.
231cbcdd875STejun Heo  *
232cbcdd875STejun Heo  *	LOCKING:
233cbcdd875STejun Heo  *	None.
234cbcdd875STejun Heo  */
235cbcdd875STejun Heo void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
236cbcdd875STejun Heo {
237cbcdd875STejun Heo 	va_list args;
238cbcdd875STejun Heo 
239cbcdd875STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
240cbcdd875STejun Heo 
241cbcdd875STejun Heo 	if (ap->link.eh_info.desc_len)
242cbcdd875STejun Heo 		__ata_ehi_push_desc(&ap->link.eh_info, " ");
243cbcdd875STejun Heo 
244cbcdd875STejun Heo 	va_start(args, fmt);
245cbcdd875STejun Heo 	__ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
246cbcdd875STejun Heo 	va_end(args);
247cbcdd875STejun Heo }
248cbcdd875STejun Heo 
249cbcdd875STejun Heo #ifdef CONFIG_PCI
250cbcdd875STejun Heo 
251cbcdd875STejun Heo /**
252cbcdd875STejun Heo  *	ata_port_pbar_desc - append PCI BAR description
253cbcdd875STejun Heo  *	@ap: target ATA port
254cbcdd875STejun Heo  *	@bar: target PCI BAR
255cbcdd875STejun Heo  *	@offset: offset into PCI BAR
256cbcdd875STejun Heo  *	@name: name of the area
257cbcdd875STejun Heo  *
258cbcdd875STejun Heo  *	If @offset is negative, this function formats a string which
259cbcdd875STejun Heo  *	contains the name, address, size and type of the BAR and
260cbcdd875STejun Heo  *	appends it to the port description.  If @offset is zero or
261cbcdd875STejun Heo  *	positive, only name and offsetted address is appended.
262cbcdd875STejun Heo  *
263cbcdd875STejun Heo  *	LOCKING:
264cbcdd875STejun Heo  *	None.
265cbcdd875STejun Heo  */
266cbcdd875STejun Heo void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
267cbcdd875STejun Heo 			const char *name)
268cbcdd875STejun Heo {
269cbcdd875STejun Heo 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
270cbcdd875STejun Heo 	char *type = "";
271cbcdd875STejun Heo 	unsigned long long start, len;
272cbcdd875STejun Heo 
273cbcdd875STejun Heo 	if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
274cbcdd875STejun Heo 		type = "m";
275cbcdd875STejun Heo 	else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
276cbcdd875STejun Heo 		type = "i";
277cbcdd875STejun Heo 
278cbcdd875STejun Heo 	start = (unsigned long long)pci_resource_start(pdev, bar);
279cbcdd875STejun Heo 	len = (unsigned long long)pci_resource_len(pdev, bar);
280cbcdd875STejun Heo 
281cbcdd875STejun Heo 	if (offset < 0)
282cbcdd875STejun Heo 		ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
283cbcdd875STejun Heo 	else
284e6a73ab1SAndrew Morton 		ata_port_desc(ap, "%s 0x%llx", name,
285e6a73ab1SAndrew Morton 				start + (unsigned long long)offset);
286cbcdd875STejun Heo }
287cbcdd875STejun Heo 
288cbcdd875STejun Heo #endif /* CONFIG_PCI */
289cbcdd875STejun Heo 
29087fbc5a0STejun Heo static int ata_lookup_timeout_table(u8 cmd)
29187fbc5a0STejun Heo {
29287fbc5a0STejun Heo 	int i;
29387fbc5a0STejun Heo 
29487fbc5a0STejun Heo 	for (i = 0; i < ATA_EH_CMD_TIMEOUT_TABLE_SIZE; i++) {
29587fbc5a0STejun Heo 		const u8 *cur;
29687fbc5a0STejun Heo 
29787fbc5a0STejun Heo 		for (cur = ata_eh_cmd_timeout_table[i].commands; *cur; cur++)
29887fbc5a0STejun Heo 			if (*cur == cmd)
29987fbc5a0STejun Heo 				return i;
30087fbc5a0STejun Heo 	}
30187fbc5a0STejun Heo 
30287fbc5a0STejun Heo 	return -1;
30387fbc5a0STejun Heo }
30487fbc5a0STejun Heo 
30587fbc5a0STejun Heo /**
30687fbc5a0STejun Heo  *	ata_internal_cmd_timeout - determine timeout for an internal command
30787fbc5a0STejun Heo  *	@dev: target device
30887fbc5a0STejun Heo  *	@cmd: internal command to be issued
30987fbc5a0STejun Heo  *
31087fbc5a0STejun Heo  *	Determine timeout for internal command @cmd for @dev.
31187fbc5a0STejun Heo  *
31287fbc5a0STejun Heo  *	LOCKING:
31387fbc5a0STejun Heo  *	EH context.
31487fbc5a0STejun Heo  *
31587fbc5a0STejun Heo  *	RETURNS:
31687fbc5a0STejun Heo  *	Determined timeout.
31787fbc5a0STejun Heo  */
31887fbc5a0STejun Heo unsigned long ata_internal_cmd_timeout(struct ata_device *dev, u8 cmd)
31987fbc5a0STejun Heo {
32087fbc5a0STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
32187fbc5a0STejun Heo 	int ent = ata_lookup_timeout_table(cmd);
32287fbc5a0STejun Heo 	int idx;
32387fbc5a0STejun Heo 
32487fbc5a0STejun Heo 	if (ent < 0)
32587fbc5a0STejun Heo 		return ATA_EH_CMD_DFL_TIMEOUT;
32687fbc5a0STejun Heo 
32787fbc5a0STejun Heo 	idx = ehc->cmd_timeout_idx[dev->devno][ent];
32887fbc5a0STejun Heo 	return ata_eh_cmd_timeout_table[ent].timeouts[idx];
32987fbc5a0STejun Heo }
33087fbc5a0STejun Heo 
33187fbc5a0STejun Heo /**
33287fbc5a0STejun Heo  *	ata_internal_cmd_timed_out - notification for internal command timeout
33387fbc5a0STejun Heo  *	@dev: target device
33487fbc5a0STejun Heo  *	@cmd: internal command which timed out
33587fbc5a0STejun Heo  *
33687fbc5a0STejun Heo  *	Notify EH that internal command @cmd for @dev timed out.  This
33787fbc5a0STejun Heo  *	function should be called only for commands whose timeouts are
33887fbc5a0STejun Heo  *	determined using ata_internal_cmd_timeout().
33987fbc5a0STejun Heo  *
34087fbc5a0STejun Heo  *	LOCKING:
34187fbc5a0STejun Heo  *	EH context.
34287fbc5a0STejun Heo  */
34387fbc5a0STejun Heo void ata_internal_cmd_timed_out(struct ata_device *dev, u8 cmd)
34487fbc5a0STejun Heo {
34587fbc5a0STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
34687fbc5a0STejun Heo 	int ent = ata_lookup_timeout_table(cmd);
34787fbc5a0STejun Heo 	int idx;
34887fbc5a0STejun Heo 
34987fbc5a0STejun Heo 	if (ent < 0)
35087fbc5a0STejun Heo 		return;
35187fbc5a0STejun Heo 
35287fbc5a0STejun Heo 	idx = ehc->cmd_timeout_idx[dev->devno][ent];
35387fbc5a0STejun Heo 	if (ata_eh_cmd_timeout_table[ent].timeouts[idx + 1] != ULONG_MAX)
35487fbc5a0STejun Heo 		ehc->cmd_timeout_idx[dev->devno][ent]++;
35587fbc5a0STejun Heo }
35687fbc5a0STejun Heo 
3573884f7b0STejun Heo static void ata_ering_record(struct ata_ering *ering, unsigned int eflags,
358c6fd2807SJeff Garzik 			     unsigned int err_mask)
359c6fd2807SJeff Garzik {
360c6fd2807SJeff Garzik 	struct ata_ering_entry *ent;
361c6fd2807SJeff Garzik 
362c6fd2807SJeff Garzik 	WARN_ON(!err_mask);
363c6fd2807SJeff Garzik 
364c6fd2807SJeff Garzik 	ering->cursor++;
365c6fd2807SJeff Garzik 	ering->cursor %= ATA_ERING_SIZE;
366c6fd2807SJeff Garzik 
367c6fd2807SJeff Garzik 	ent = &ering->ring[ering->cursor];
3683884f7b0STejun Heo 	ent->eflags = eflags;
369c6fd2807SJeff Garzik 	ent->err_mask = err_mask;
370c6fd2807SJeff Garzik 	ent->timestamp = get_jiffies_64();
371c6fd2807SJeff Garzik }
372c6fd2807SJeff Garzik 
37376326ac1STejun Heo static struct ata_ering_entry *ata_ering_top(struct ata_ering *ering)
37476326ac1STejun Heo {
37576326ac1STejun Heo 	struct ata_ering_entry *ent = &ering->ring[ering->cursor];
37676326ac1STejun Heo 
37776326ac1STejun Heo 	if (ent->err_mask)
37876326ac1STejun Heo 		return ent;
37976326ac1STejun Heo 	return NULL;
38076326ac1STejun Heo }
38176326ac1STejun Heo 
3827d47e8d4STejun Heo static void ata_ering_clear(struct ata_ering *ering)
383c6fd2807SJeff Garzik {
3847d47e8d4STejun Heo 	memset(ering, 0, sizeof(*ering));
385c6fd2807SJeff Garzik }
386c6fd2807SJeff Garzik 
387c6fd2807SJeff Garzik static int ata_ering_map(struct ata_ering *ering,
388c6fd2807SJeff Garzik 			 int (*map_fn)(struct ata_ering_entry *, void *),
389c6fd2807SJeff Garzik 			 void *arg)
390c6fd2807SJeff Garzik {
391c6fd2807SJeff Garzik 	int idx, rc = 0;
392c6fd2807SJeff Garzik 	struct ata_ering_entry *ent;
393c6fd2807SJeff Garzik 
394c6fd2807SJeff Garzik 	idx = ering->cursor;
395c6fd2807SJeff Garzik 	do {
396c6fd2807SJeff Garzik 		ent = &ering->ring[idx];
397c6fd2807SJeff Garzik 		if (!ent->err_mask)
398c6fd2807SJeff Garzik 			break;
399c6fd2807SJeff Garzik 		rc = map_fn(ent, arg);
400c6fd2807SJeff Garzik 		if (rc)
401c6fd2807SJeff Garzik 			break;
402c6fd2807SJeff Garzik 		idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
403c6fd2807SJeff Garzik 	} while (idx != ering->cursor);
404c6fd2807SJeff Garzik 
405c6fd2807SJeff Garzik 	return rc;
406c6fd2807SJeff Garzik }
407c6fd2807SJeff Garzik 
408c6fd2807SJeff Garzik static unsigned int ata_eh_dev_action(struct ata_device *dev)
409c6fd2807SJeff Garzik {
4109af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
411c6fd2807SJeff Garzik 
412c6fd2807SJeff Garzik 	return ehc->i.action | ehc->i.dev_action[dev->devno];
413c6fd2807SJeff Garzik }
414c6fd2807SJeff Garzik 
415f58229f8STejun Heo static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
416c6fd2807SJeff Garzik 				struct ata_eh_info *ehi, unsigned int action)
417c6fd2807SJeff Garzik {
418f58229f8STejun Heo 	struct ata_device *tdev;
419c6fd2807SJeff Garzik 
420c6fd2807SJeff Garzik 	if (!dev) {
421c6fd2807SJeff Garzik 		ehi->action &= ~action;
422f58229f8STejun Heo 		ata_link_for_each_dev(tdev, link)
423f58229f8STejun Heo 			ehi->dev_action[tdev->devno] &= ~action;
424c6fd2807SJeff Garzik 	} else {
425c6fd2807SJeff Garzik 		/* doesn't make sense for port-wide EH actions */
426c6fd2807SJeff Garzik 		WARN_ON(!(action & ATA_EH_PERDEV_MASK));
427c6fd2807SJeff Garzik 
428c6fd2807SJeff Garzik 		/* break ehi->action into ehi->dev_action */
429c6fd2807SJeff Garzik 		if (ehi->action & action) {
430f58229f8STejun Heo 			ata_link_for_each_dev(tdev, link)
431f58229f8STejun Heo 				ehi->dev_action[tdev->devno] |=
432f58229f8STejun Heo 					ehi->action & action;
433c6fd2807SJeff Garzik 			ehi->action &= ~action;
434c6fd2807SJeff Garzik 		}
435c6fd2807SJeff Garzik 
436c6fd2807SJeff Garzik 		/* turn off the specified per-dev action */
437c6fd2807SJeff Garzik 		ehi->dev_action[dev->devno] &= ~action;
438c6fd2807SJeff Garzik 	}
439c6fd2807SJeff Garzik }
440c6fd2807SJeff Garzik 
441c6fd2807SJeff Garzik /**
442c6fd2807SJeff Garzik  *	ata_scsi_timed_out - SCSI layer time out callback
443c6fd2807SJeff Garzik  *	@cmd: timed out SCSI command
444c6fd2807SJeff Garzik  *
445c6fd2807SJeff Garzik  *	Handles SCSI layer timeout.  We race with normal completion of
446c6fd2807SJeff Garzik  *	the qc for @cmd.  If the qc is already gone, we lose and let
447c6fd2807SJeff Garzik  *	the scsi command finish (EH_HANDLED).  Otherwise, the qc has
448c6fd2807SJeff Garzik  *	timed out and EH should be invoked.  Prevent ata_qc_complete()
449c6fd2807SJeff Garzik  *	from finishing it by setting EH_SCHEDULED and return
450c6fd2807SJeff Garzik  *	EH_NOT_HANDLED.
451c6fd2807SJeff Garzik  *
452c6fd2807SJeff Garzik  *	TODO: kill this function once old EH is gone.
453c6fd2807SJeff Garzik  *
454c6fd2807SJeff Garzik  *	LOCKING:
455c6fd2807SJeff Garzik  *	Called from timer context
456c6fd2807SJeff Garzik  *
457c6fd2807SJeff Garzik  *	RETURNS:
458c6fd2807SJeff Garzik  *	EH_HANDLED or EH_NOT_HANDLED
459c6fd2807SJeff Garzik  */
460c6fd2807SJeff Garzik enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
461c6fd2807SJeff Garzik {
462c6fd2807SJeff Garzik 	struct Scsi_Host *host = cmd->device->host;
463c6fd2807SJeff Garzik 	struct ata_port *ap = ata_shost_to_port(host);
464c6fd2807SJeff Garzik 	unsigned long flags;
465c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
466c6fd2807SJeff Garzik 	enum scsi_eh_timer_return ret;
467c6fd2807SJeff Garzik 
468c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
469c6fd2807SJeff Garzik 
470c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
471c6fd2807SJeff Garzik 		ret = EH_NOT_HANDLED;
472c6fd2807SJeff Garzik 		goto out;
473c6fd2807SJeff Garzik 	}
474c6fd2807SJeff Garzik 
475c6fd2807SJeff Garzik 	ret = EH_HANDLED;
476c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
4779af5c9c9STejun Heo 	qc = ata_qc_from_tag(ap, ap->link.active_tag);
478c6fd2807SJeff Garzik 	if (qc) {
479c6fd2807SJeff Garzik 		WARN_ON(qc->scsicmd != cmd);
480c6fd2807SJeff Garzik 		qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
481c6fd2807SJeff Garzik 		qc->err_mask |= AC_ERR_TIMEOUT;
482c6fd2807SJeff Garzik 		ret = EH_NOT_HANDLED;
483c6fd2807SJeff Garzik 	}
484c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
485c6fd2807SJeff Garzik 
486c6fd2807SJeff Garzik  out:
487c6fd2807SJeff Garzik 	DPRINTK("EXIT, ret=%d\n", ret);
488c6fd2807SJeff Garzik 	return ret;
489c6fd2807SJeff Garzik }
490c6fd2807SJeff Garzik 
491c6fd2807SJeff Garzik /**
492c6fd2807SJeff Garzik  *	ata_scsi_error - SCSI layer error handler callback
493c6fd2807SJeff Garzik  *	@host: SCSI host on which error occurred
494c6fd2807SJeff Garzik  *
495c6fd2807SJeff Garzik  *	Handles SCSI-layer-thrown error events.
496c6fd2807SJeff Garzik  *
497c6fd2807SJeff Garzik  *	LOCKING:
498c6fd2807SJeff Garzik  *	Inherited from SCSI layer (none, can sleep)
499c6fd2807SJeff Garzik  *
500c6fd2807SJeff Garzik  *	RETURNS:
501c6fd2807SJeff Garzik  *	Zero.
502c6fd2807SJeff Garzik  */
503c6fd2807SJeff Garzik void ata_scsi_error(struct Scsi_Host *host)
504c6fd2807SJeff Garzik {
505c6fd2807SJeff Garzik 	struct ata_port *ap = ata_shost_to_port(host);
506a1e10f7eSTejun Heo 	int i;
507c6fd2807SJeff Garzik 	unsigned long flags;
508c6fd2807SJeff Garzik 
509c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
510c6fd2807SJeff Garzik 
511c6fd2807SJeff Garzik 	/* synchronize with port task */
512c6fd2807SJeff Garzik 	ata_port_flush_task(ap);
513c6fd2807SJeff Garzik 
514cca3974eSJeff Garzik 	/* synchronize with host lock and sort out timeouts */
515c6fd2807SJeff Garzik 
516c6fd2807SJeff Garzik 	/* For new EH, all qcs are finished in one of three ways -
517c6fd2807SJeff Garzik 	 * normal completion, error completion, and SCSI timeout.
518c6fd2807SJeff Garzik 	 * Both cmpletions can race against SCSI timeout.  When normal
519c6fd2807SJeff Garzik 	 * completion wins, the qc never reaches EH.  When error
520c6fd2807SJeff Garzik 	 * completion wins, the qc has ATA_QCFLAG_FAILED set.
521c6fd2807SJeff Garzik 	 *
522c6fd2807SJeff Garzik 	 * When SCSI timeout wins, things are a bit more complex.
523c6fd2807SJeff Garzik 	 * Normal or error completion can occur after the timeout but
524c6fd2807SJeff Garzik 	 * before this point.  In such cases, both types of
525c6fd2807SJeff Garzik 	 * completions are honored.  A scmd is determined to have
526c6fd2807SJeff Garzik 	 * timed out iff its associated qc is active and not failed.
527c6fd2807SJeff Garzik 	 */
528c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
529c6fd2807SJeff Garzik 		struct scsi_cmnd *scmd, *tmp;
530c6fd2807SJeff Garzik 		int nr_timedout = 0;
531c6fd2807SJeff Garzik 
532c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
533c6fd2807SJeff Garzik 
534c6fd2807SJeff Garzik 		list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
535c6fd2807SJeff Garzik 			struct ata_queued_cmd *qc;
536c6fd2807SJeff Garzik 
537c6fd2807SJeff Garzik 			for (i = 0; i < ATA_MAX_QUEUE; i++) {
538c6fd2807SJeff Garzik 				qc = __ata_qc_from_tag(ap, i);
539c6fd2807SJeff Garzik 				if (qc->flags & ATA_QCFLAG_ACTIVE &&
540c6fd2807SJeff Garzik 				    qc->scsicmd == scmd)
541c6fd2807SJeff Garzik 					break;
542c6fd2807SJeff Garzik 			}
543c6fd2807SJeff Garzik 
544c6fd2807SJeff Garzik 			if (i < ATA_MAX_QUEUE) {
545c6fd2807SJeff Garzik 				/* the scmd has an associated qc */
546c6fd2807SJeff Garzik 				if (!(qc->flags & ATA_QCFLAG_FAILED)) {
547c6fd2807SJeff Garzik 					/* which hasn't failed yet, timeout */
548c6fd2807SJeff Garzik 					qc->err_mask |= AC_ERR_TIMEOUT;
549c6fd2807SJeff Garzik 					qc->flags |= ATA_QCFLAG_FAILED;
550c6fd2807SJeff Garzik 					nr_timedout++;
551c6fd2807SJeff Garzik 				}
552c6fd2807SJeff Garzik 			} else {
553c6fd2807SJeff Garzik 				/* Normal completion occurred after
554c6fd2807SJeff Garzik 				 * SCSI timeout but before this point.
555c6fd2807SJeff Garzik 				 * Successfully complete it.
556c6fd2807SJeff Garzik 				 */
557c6fd2807SJeff Garzik 				scmd->retries = scmd->allowed;
558c6fd2807SJeff Garzik 				scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
559c6fd2807SJeff Garzik 			}
560c6fd2807SJeff Garzik 		}
561c6fd2807SJeff Garzik 
562c6fd2807SJeff Garzik 		/* If we have timed out qcs.  They belong to EH from
563c6fd2807SJeff Garzik 		 * this point but the state of the controller is
564c6fd2807SJeff Garzik 		 * unknown.  Freeze the port to make sure the IRQ
565c6fd2807SJeff Garzik 		 * handler doesn't diddle with those qcs.  This must
566c6fd2807SJeff Garzik 		 * be done atomically w.r.t. setting QCFLAG_FAILED.
567c6fd2807SJeff Garzik 		 */
568c6fd2807SJeff Garzik 		if (nr_timedout)
569c6fd2807SJeff Garzik 			__ata_port_freeze(ap);
570c6fd2807SJeff Garzik 
571c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
572a1e10f7eSTejun Heo 
573a1e10f7eSTejun Heo 		/* initialize eh_tries */
574a1e10f7eSTejun Heo 		ap->eh_tries = ATA_EH_MAX_TRIES;
575c6fd2807SJeff Garzik 	} else
576c6fd2807SJeff Garzik 		spin_unlock_wait(ap->lock);
577c6fd2807SJeff Garzik 
578c6fd2807SJeff Garzik  repeat:
579c6fd2807SJeff Garzik 	/* invoke error handler */
580c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
581cf1b86c8STejun Heo 		struct ata_link *link;
582cf1b86c8STejun Heo 
5835ddf24c5STejun Heo 		/* kill fast drain timer */
5845ddf24c5STejun Heo 		del_timer_sync(&ap->fastdrain_timer);
5855ddf24c5STejun Heo 
586c6fd2807SJeff Garzik 		/* process port resume request */
587c6fd2807SJeff Garzik 		ata_eh_handle_port_resume(ap);
588c6fd2807SJeff Garzik 
589c6fd2807SJeff Garzik 		/* fetch & clear EH info */
590c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
591c6fd2807SJeff Garzik 
592cf1b86c8STejun Heo 		__ata_port_for_each_link(link, ap) {
59300115e0fSTejun Heo 			struct ata_eh_context *ehc = &link->eh_context;
59400115e0fSTejun Heo 			struct ata_device *dev;
59500115e0fSTejun Heo 
596cf1b86c8STejun Heo 			memset(&link->eh_context, 0, sizeof(link->eh_context));
597cf1b86c8STejun Heo 			link->eh_context.i = link->eh_info;
598cf1b86c8STejun Heo 			memset(&link->eh_info, 0, sizeof(link->eh_info));
59900115e0fSTejun Heo 
60000115e0fSTejun Heo 			ata_link_for_each_dev(dev, link) {
60100115e0fSTejun Heo 				int devno = dev->devno;
60200115e0fSTejun Heo 
60300115e0fSTejun Heo 				ehc->saved_xfer_mode[devno] = dev->xfer_mode;
60400115e0fSTejun Heo 				if (ata_ncq_enabled(dev))
60500115e0fSTejun Heo 					ehc->saved_ncq_enabled |= 1 << devno;
60600115e0fSTejun Heo 			}
6070a2c0f56STejun Heo 
6080a2c0f56STejun Heo 			/* set last reset timestamp to some time in the past */
6090a2c0f56STejun Heo 			ehc->last_reset = jiffies - 60 * HZ;
610cf1b86c8STejun Heo 		}
611c6fd2807SJeff Garzik 
612c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
613c6fd2807SJeff Garzik 		ap->pflags &= ~ATA_PFLAG_EH_PENDING;
614da917d69STejun Heo 		ap->excl_link = NULL;	/* don't maintain exclusion over EH */
615c6fd2807SJeff Garzik 
616c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
617c6fd2807SJeff Garzik 
618c6fd2807SJeff Garzik 		/* invoke EH, skip if unloading or suspended */
619c6fd2807SJeff Garzik 		if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
620c6fd2807SJeff Garzik 			ap->ops->error_handler(ap);
621c6fd2807SJeff Garzik 		else
622c6fd2807SJeff Garzik 			ata_eh_finish(ap);
623c6fd2807SJeff Garzik 
624c6fd2807SJeff Garzik 		/* process port suspend request */
625c6fd2807SJeff Garzik 		ata_eh_handle_port_suspend(ap);
626c6fd2807SJeff Garzik 
627c6fd2807SJeff Garzik 		/* Exception might have happend after ->error_handler
628c6fd2807SJeff Garzik 		 * recovered the port but before this point.  Repeat
629c6fd2807SJeff Garzik 		 * EH in such case.
630c6fd2807SJeff Garzik 		 */
631c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
632c6fd2807SJeff Garzik 
633c6fd2807SJeff Garzik 		if (ap->pflags & ATA_PFLAG_EH_PENDING) {
634a1e10f7eSTejun Heo 			if (--ap->eh_tries) {
635c6fd2807SJeff Garzik 				spin_unlock_irqrestore(ap->lock, flags);
636c6fd2807SJeff Garzik 				goto repeat;
637c6fd2807SJeff Garzik 			}
638c6fd2807SJeff Garzik 			ata_port_printk(ap, KERN_ERR, "EH pending after %d "
639a1e10f7eSTejun Heo 					"tries, giving up\n", ATA_EH_MAX_TRIES);
640914616a3STejun Heo 			ap->pflags &= ~ATA_PFLAG_EH_PENDING;
641c6fd2807SJeff Garzik 		}
642c6fd2807SJeff Garzik 
643c6fd2807SJeff Garzik 		/* this run is complete, make sure EH info is clear */
644cf1b86c8STejun Heo 		__ata_port_for_each_link(link, ap)
645cf1b86c8STejun Heo 			memset(&link->eh_info, 0, sizeof(link->eh_info));
646c6fd2807SJeff Garzik 
647c6fd2807SJeff Garzik 		/* Clear host_eh_scheduled while holding ap->lock such
648c6fd2807SJeff Garzik 		 * that if exception occurs after this point but
649c6fd2807SJeff Garzik 		 * before EH completion, SCSI midlayer will
650c6fd2807SJeff Garzik 		 * re-initiate EH.
651c6fd2807SJeff Garzik 		 */
652c6fd2807SJeff Garzik 		host->host_eh_scheduled = 0;
653c6fd2807SJeff Garzik 
654c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
655c6fd2807SJeff Garzik 	} else {
6569af5c9c9STejun Heo 		WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
657c6fd2807SJeff Garzik 		ap->ops->eng_timeout(ap);
658c6fd2807SJeff Garzik 	}
659c6fd2807SJeff Garzik 
660c6fd2807SJeff Garzik 	/* finish or retry handled scmd's and clean up */
661c6fd2807SJeff Garzik 	WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
662c6fd2807SJeff Garzik 
663c6fd2807SJeff Garzik 	scsi_eh_flush_done_q(&ap->eh_done_q);
664c6fd2807SJeff Garzik 
665c6fd2807SJeff Garzik 	/* clean up */
666c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
667c6fd2807SJeff Garzik 
668c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_LOADING)
669c6fd2807SJeff Garzik 		ap->pflags &= ~ATA_PFLAG_LOADING;
670c6fd2807SJeff Garzik 	else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
67152bad64dSDavid Howells 		queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0);
672c6fd2807SJeff Garzik 
673c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_RECOVERED)
674c6fd2807SJeff Garzik 		ata_port_printk(ap, KERN_INFO, "EH complete\n");
675c6fd2807SJeff Garzik 
676c6fd2807SJeff Garzik 	ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
677c6fd2807SJeff Garzik 
678c6fd2807SJeff Garzik 	/* tell wait_eh that we're done */
679c6fd2807SJeff Garzik 	ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
680c6fd2807SJeff Garzik 	wake_up_all(&ap->eh_wait_q);
681c6fd2807SJeff Garzik 
682c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
683c6fd2807SJeff Garzik 
684c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
685c6fd2807SJeff Garzik }
686c6fd2807SJeff Garzik 
687c6fd2807SJeff Garzik /**
688c6fd2807SJeff Garzik  *	ata_port_wait_eh - Wait for the currently pending EH to complete
689c6fd2807SJeff Garzik  *	@ap: Port to wait EH for
690c6fd2807SJeff Garzik  *
691c6fd2807SJeff Garzik  *	Wait until the currently pending EH is complete.
692c6fd2807SJeff Garzik  *
693c6fd2807SJeff Garzik  *	LOCKING:
694c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
695c6fd2807SJeff Garzik  */
696c6fd2807SJeff Garzik void ata_port_wait_eh(struct ata_port *ap)
697c6fd2807SJeff Garzik {
698c6fd2807SJeff Garzik 	unsigned long flags;
699c6fd2807SJeff Garzik 	DEFINE_WAIT(wait);
700c6fd2807SJeff Garzik 
701c6fd2807SJeff Garzik  retry:
702c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
703c6fd2807SJeff Garzik 
704c6fd2807SJeff Garzik 	while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
705c6fd2807SJeff Garzik 		prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
706c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
707c6fd2807SJeff Garzik 		schedule();
708c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
709c6fd2807SJeff Garzik 	}
710c6fd2807SJeff Garzik 	finish_wait(&ap->eh_wait_q, &wait);
711c6fd2807SJeff Garzik 
712c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
713c6fd2807SJeff Garzik 
714c6fd2807SJeff Garzik 	/* make sure SCSI EH is complete */
715cca3974eSJeff Garzik 	if (scsi_host_in_recovery(ap->scsi_host)) {
716c6fd2807SJeff Garzik 		msleep(10);
717c6fd2807SJeff Garzik 		goto retry;
718c6fd2807SJeff Garzik 	}
719c6fd2807SJeff Garzik }
720c6fd2807SJeff Garzik 
7215ddf24c5STejun Heo static int ata_eh_nr_in_flight(struct ata_port *ap)
7225ddf24c5STejun Heo {
7235ddf24c5STejun Heo 	unsigned int tag;
7245ddf24c5STejun Heo 	int nr = 0;
7255ddf24c5STejun Heo 
7265ddf24c5STejun Heo 	/* count only non-internal commands */
7275ddf24c5STejun Heo 	for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++)
7285ddf24c5STejun Heo 		if (ata_qc_from_tag(ap, tag))
7295ddf24c5STejun Heo 			nr++;
7305ddf24c5STejun Heo 
7315ddf24c5STejun Heo 	return nr;
7325ddf24c5STejun Heo }
7335ddf24c5STejun Heo 
7345ddf24c5STejun Heo void ata_eh_fastdrain_timerfn(unsigned long arg)
7355ddf24c5STejun Heo {
7365ddf24c5STejun Heo 	struct ata_port *ap = (void *)arg;
7375ddf24c5STejun Heo 	unsigned long flags;
7385ddf24c5STejun Heo 	int cnt;
7395ddf24c5STejun Heo 
7405ddf24c5STejun Heo 	spin_lock_irqsave(ap->lock, flags);
7415ddf24c5STejun Heo 
7425ddf24c5STejun Heo 	cnt = ata_eh_nr_in_flight(ap);
7435ddf24c5STejun Heo 
7445ddf24c5STejun Heo 	/* are we done? */
7455ddf24c5STejun Heo 	if (!cnt)
7465ddf24c5STejun Heo 		goto out_unlock;
7475ddf24c5STejun Heo 
7485ddf24c5STejun Heo 	if (cnt == ap->fastdrain_cnt) {
7495ddf24c5STejun Heo 		unsigned int tag;
7505ddf24c5STejun Heo 
7515ddf24c5STejun Heo 		/* No progress during the last interval, tag all
7525ddf24c5STejun Heo 		 * in-flight qcs as timed out and freeze the port.
7535ddf24c5STejun Heo 		 */
7545ddf24c5STejun Heo 		for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) {
7555ddf24c5STejun Heo 			struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
7565ddf24c5STejun Heo 			if (qc)
7575ddf24c5STejun Heo 				qc->err_mask |= AC_ERR_TIMEOUT;
7585ddf24c5STejun Heo 		}
7595ddf24c5STejun Heo 
7605ddf24c5STejun Heo 		ata_port_freeze(ap);
7615ddf24c5STejun Heo 	} else {
7625ddf24c5STejun Heo 		/* some qcs have finished, give it another chance */
7635ddf24c5STejun Heo 		ap->fastdrain_cnt = cnt;
7645ddf24c5STejun Heo 		ap->fastdrain_timer.expires =
765341c2c95STejun Heo 			ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
7665ddf24c5STejun Heo 		add_timer(&ap->fastdrain_timer);
7675ddf24c5STejun Heo 	}
7685ddf24c5STejun Heo 
7695ddf24c5STejun Heo  out_unlock:
7705ddf24c5STejun Heo 	spin_unlock_irqrestore(ap->lock, flags);
7715ddf24c5STejun Heo }
7725ddf24c5STejun Heo 
7735ddf24c5STejun Heo /**
7745ddf24c5STejun Heo  *	ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
7755ddf24c5STejun Heo  *	@ap: target ATA port
7765ddf24c5STejun Heo  *	@fastdrain: activate fast drain
7775ddf24c5STejun Heo  *
7785ddf24c5STejun Heo  *	Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
7795ddf24c5STejun Heo  *	is non-zero and EH wasn't pending before.  Fast drain ensures
7805ddf24c5STejun Heo  *	that EH kicks in in timely manner.
7815ddf24c5STejun Heo  *
7825ddf24c5STejun Heo  *	LOCKING:
7835ddf24c5STejun Heo  *	spin_lock_irqsave(host lock)
7845ddf24c5STejun Heo  */
7855ddf24c5STejun Heo static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
7865ddf24c5STejun Heo {
7875ddf24c5STejun Heo 	int cnt;
7885ddf24c5STejun Heo 
7895ddf24c5STejun Heo 	/* already scheduled? */
7905ddf24c5STejun Heo 	if (ap->pflags & ATA_PFLAG_EH_PENDING)
7915ddf24c5STejun Heo 		return;
7925ddf24c5STejun Heo 
7935ddf24c5STejun Heo 	ap->pflags |= ATA_PFLAG_EH_PENDING;
7945ddf24c5STejun Heo 
7955ddf24c5STejun Heo 	if (!fastdrain)
7965ddf24c5STejun Heo 		return;
7975ddf24c5STejun Heo 
7985ddf24c5STejun Heo 	/* do we have in-flight qcs? */
7995ddf24c5STejun Heo 	cnt = ata_eh_nr_in_flight(ap);
8005ddf24c5STejun Heo 	if (!cnt)
8015ddf24c5STejun Heo 		return;
8025ddf24c5STejun Heo 
8035ddf24c5STejun Heo 	/* activate fast drain */
8045ddf24c5STejun Heo 	ap->fastdrain_cnt = cnt;
805341c2c95STejun Heo 	ap->fastdrain_timer.expires =
806341c2c95STejun Heo 		ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
8075ddf24c5STejun Heo 	add_timer(&ap->fastdrain_timer);
8085ddf24c5STejun Heo }
8095ddf24c5STejun Heo 
810c6fd2807SJeff Garzik /**
811c6fd2807SJeff Garzik  *	ata_qc_schedule_eh - schedule qc for error handling
812c6fd2807SJeff Garzik  *	@qc: command to schedule error handling for
813c6fd2807SJeff Garzik  *
814c6fd2807SJeff Garzik  *	Schedule error handling for @qc.  EH will kick in as soon as
815c6fd2807SJeff Garzik  *	other commands are drained.
816c6fd2807SJeff Garzik  *
817c6fd2807SJeff Garzik  *	LOCKING:
818cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
819c6fd2807SJeff Garzik  */
820c6fd2807SJeff Garzik void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
821c6fd2807SJeff Garzik {
822c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
823c6fd2807SJeff Garzik 
824c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
825c6fd2807SJeff Garzik 
826c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_FAILED;
8275ddf24c5STejun Heo 	ata_eh_set_pending(ap, 1);
828c6fd2807SJeff Garzik 
829c6fd2807SJeff Garzik 	/* The following will fail if timeout has already expired.
830c6fd2807SJeff Garzik 	 * ata_scsi_error() takes care of such scmds on EH entry.
831c6fd2807SJeff Garzik 	 * Note that ATA_QCFLAG_FAILED is unconditionally set after
832c6fd2807SJeff Garzik 	 * this function completes.
833c6fd2807SJeff Garzik 	 */
834c6fd2807SJeff Garzik 	scsi_req_abort_cmd(qc->scsicmd);
835c6fd2807SJeff Garzik }
836c6fd2807SJeff Garzik 
837c6fd2807SJeff Garzik /**
838c6fd2807SJeff Garzik  *	ata_port_schedule_eh - schedule error handling without a qc
839c6fd2807SJeff Garzik  *	@ap: ATA port to schedule EH for
840c6fd2807SJeff Garzik  *
841c6fd2807SJeff Garzik  *	Schedule error handling for @ap.  EH will kick in as soon as
842c6fd2807SJeff Garzik  *	all commands are drained.
843c6fd2807SJeff Garzik  *
844c6fd2807SJeff Garzik  *	LOCKING:
845cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
846c6fd2807SJeff Garzik  */
847c6fd2807SJeff Garzik void ata_port_schedule_eh(struct ata_port *ap)
848c6fd2807SJeff Garzik {
849c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
850c6fd2807SJeff Garzik 
851f4d6d004STejun Heo 	if (ap->pflags & ATA_PFLAG_INITIALIZING)
852f4d6d004STejun Heo 		return;
853f4d6d004STejun Heo 
8545ddf24c5STejun Heo 	ata_eh_set_pending(ap, 1);
855cca3974eSJeff Garzik 	scsi_schedule_eh(ap->scsi_host);
856c6fd2807SJeff Garzik 
857c6fd2807SJeff Garzik 	DPRINTK("port EH scheduled\n");
858c6fd2807SJeff Garzik }
859c6fd2807SJeff Garzik 
860dbd82616STejun Heo static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
861c6fd2807SJeff Garzik {
862c6fd2807SJeff Garzik 	int tag, nr_aborted = 0;
863c6fd2807SJeff Garzik 
864c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
865c6fd2807SJeff Garzik 
8665ddf24c5STejun Heo 	/* we're gonna abort all commands, no need for fast drain */
8675ddf24c5STejun Heo 	ata_eh_set_pending(ap, 0);
8685ddf24c5STejun Heo 
869c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
870c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
871c6fd2807SJeff Garzik 
872dbd82616STejun Heo 		if (qc && (!link || qc->dev->link == link)) {
873c6fd2807SJeff Garzik 			qc->flags |= ATA_QCFLAG_FAILED;
874c6fd2807SJeff Garzik 			ata_qc_complete(qc);
875c6fd2807SJeff Garzik 			nr_aborted++;
876c6fd2807SJeff Garzik 		}
877c6fd2807SJeff Garzik 	}
878c6fd2807SJeff Garzik 
879c6fd2807SJeff Garzik 	if (!nr_aborted)
880c6fd2807SJeff Garzik 		ata_port_schedule_eh(ap);
881c6fd2807SJeff Garzik 
882c6fd2807SJeff Garzik 	return nr_aborted;
883c6fd2807SJeff Garzik }
884c6fd2807SJeff Garzik 
885c6fd2807SJeff Garzik /**
886dbd82616STejun Heo  *	ata_link_abort - abort all qc's on the link
887dbd82616STejun Heo  *	@link: ATA link to abort qc's for
888dbd82616STejun Heo  *
889dbd82616STejun Heo  *	Abort all active qc's active on @link and schedule EH.
890dbd82616STejun Heo  *
891dbd82616STejun Heo  *	LOCKING:
892dbd82616STejun Heo  *	spin_lock_irqsave(host lock)
893dbd82616STejun Heo  *
894dbd82616STejun Heo  *	RETURNS:
895dbd82616STejun Heo  *	Number of aborted qc's.
896dbd82616STejun Heo  */
897dbd82616STejun Heo int ata_link_abort(struct ata_link *link)
898dbd82616STejun Heo {
899dbd82616STejun Heo 	return ata_do_link_abort(link->ap, link);
900dbd82616STejun Heo }
901dbd82616STejun Heo 
902dbd82616STejun Heo /**
903dbd82616STejun Heo  *	ata_port_abort - abort all qc's on the port
904dbd82616STejun Heo  *	@ap: ATA port to abort qc's for
905dbd82616STejun Heo  *
906dbd82616STejun Heo  *	Abort all active qc's of @ap and schedule EH.
907dbd82616STejun Heo  *
908dbd82616STejun Heo  *	LOCKING:
909dbd82616STejun Heo  *	spin_lock_irqsave(host_set lock)
910dbd82616STejun Heo  *
911dbd82616STejun Heo  *	RETURNS:
912dbd82616STejun Heo  *	Number of aborted qc's.
913dbd82616STejun Heo  */
914dbd82616STejun Heo int ata_port_abort(struct ata_port *ap)
915dbd82616STejun Heo {
916dbd82616STejun Heo 	return ata_do_link_abort(ap, NULL);
917dbd82616STejun Heo }
918dbd82616STejun Heo 
919dbd82616STejun Heo /**
920c6fd2807SJeff Garzik  *	__ata_port_freeze - freeze port
921c6fd2807SJeff Garzik  *	@ap: ATA port to freeze
922c6fd2807SJeff Garzik  *
923c6fd2807SJeff Garzik  *	This function is called when HSM violation or some other
924c6fd2807SJeff Garzik  *	condition disrupts normal operation of the port.  Frozen port
925c6fd2807SJeff Garzik  *	is not allowed to perform any operation until the port is
926c6fd2807SJeff Garzik  *	thawed, which usually follows a successful reset.
927c6fd2807SJeff Garzik  *
928c6fd2807SJeff Garzik  *	ap->ops->freeze() callback can be used for freezing the port
929c6fd2807SJeff Garzik  *	hardware-wise (e.g. mask interrupt and stop DMA engine).  If a
930c6fd2807SJeff Garzik  *	port cannot be frozen hardware-wise, the interrupt handler
931c6fd2807SJeff Garzik  *	must ack and clear interrupts unconditionally while the port
932c6fd2807SJeff Garzik  *	is frozen.
933c6fd2807SJeff Garzik  *
934c6fd2807SJeff Garzik  *	LOCKING:
935cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
936c6fd2807SJeff Garzik  */
937c6fd2807SJeff Garzik static void __ata_port_freeze(struct ata_port *ap)
938c6fd2807SJeff Garzik {
939c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
940c6fd2807SJeff Garzik 
941c6fd2807SJeff Garzik 	if (ap->ops->freeze)
942c6fd2807SJeff Garzik 		ap->ops->freeze(ap);
943c6fd2807SJeff Garzik 
944c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_FROZEN;
945c6fd2807SJeff Garzik 
94644877b4eSTejun Heo 	DPRINTK("ata%u port frozen\n", ap->print_id);
947c6fd2807SJeff Garzik }
948c6fd2807SJeff Garzik 
949c6fd2807SJeff Garzik /**
950c6fd2807SJeff Garzik  *	ata_port_freeze - abort & freeze port
951c6fd2807SJeff Garzik  *	@ap: ATA port to freeze
952c6fd2807SJeff Garzik  *
953c6fd2807SJeff Garzik  *	Abort and freeze @ap.
954c6fd2807SJeff Garzik  *
955c6fd2807SJeff Garzik  *	LOCKING:
956cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
957c6fd2807SJeff Garzik  *
958c6fd2807SJeff Garzik  *	RETURNS:
959c6fd2807SJeff Garzik  *	Number of aborted commands.
960c6fd2807SJeff Garzik  */
961c6fd2807SJeff Garzik int ata_port_freeze(struct ata_port *ap)
962c6fd2807SJeff Garzik {
963c6fd2807SJeff Garzik 	int nr_aborted;
964c6fd2807SJeff Garzik 
965c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
966c6fd2807SJeff Garzik 
967c6fd2807SJeff Garzik 	nr_aborted = ata_port_abort(ap);
968c6fd2807SJeff Garzik 	__ata_port_freeze(ap);
969c6fd2807SJeff Garzik 
970c6fd2807SJeff Garzik 	return nr_aborted;
971c6fd2807SJeff Garzik }
972c6fd2807SJeff Garzik 
973c6fd2807SJeff Garzik /**
9747d77b247STejun Heo  *	sata_async_notification - SATA async notification handler
9757d77b247STejun Heo  *	@ap: ATA port where async notification is received
9767d77b247STejun Heo  *
9777d77b247STejun Heo  *	Handler to be called when async notification via SDB FIS is
9787d77b247STejun Heo  *	received.  This function schedules EH if necessary.
9797d77b247STejun Heo  *
9807d77b247STejun Heo  *	LOCKING:
9817d77b247STejun Heo  *	spin_lock_irqsave(host lock)
9827d77b247STejun Heo  *
9837d77b247STejun Heo  *	RETURNS:
9847d77b247STejun Heo  *	1 if EH is scheduled, 0 otherwise.
9857d77b247STejun Heo  */
9867d77b247STejun Heo int sata_async_notification(struct ata_port *ap)
9877d77b247STejun Heo {
9887d77b247STejun Heo 	u32 sntf;
9897d77b247STejun Heo 	int rc;
9907d77b247STejun Heo 
9917d77b247STejun Heo 	if (!(ap->flags & ATA_FLAG_AN))
9927d77b247STejun Heo 		return 0;
9937d77b247STejun Heo 
9947d77b247STejun Heo 	rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
9957d77b247STejun Heo 	if (rc == 0)
9967d77b247STejun Heo 		sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
9977d77b247STejun Heo 
998071f44b1STejun Heo 	if (!sata_pmp_attached(ap) || rc) {
9997d77b247STejun Heo 		/* PMP is not attached or SNTF is not available */
1000071f44b1STejun Heo 		if (!sata_pmp_attached(ap)) {
10017d77b247STejun Heo 			/* PMP is not attached.  Check whether ATAPI
10027d77b247STejun Heo 			 * AN is configured.  If so, notify media
10037d77b247STejun Heo 			 * change.
10047d77b247STejun Heo 			 */
10057d77b247STejun Heo 			struct ata_device *dev = ap->link.device;
10067d77b247STejun Heo 
10077d77b247STejun Heo 			if ((dev->class == ATA_DEV_ATAPI) &&
10087d77b247STejun Heo 			    (dev->flags & ATA_DFLAG_AN))
10097d77b247STejun Heo 				ata_scsi_media_change_notify(dev);
10107d77b247STejun Heo 			return 0;
10117d77b247STejun Heo 		} else {
10127d77b247STejun Heo 			/* PMP is attached but SNTF is not available.
10137d77b247STejun Heo 			 * ATAPI async media change notification is
10147d77b247STejun Heo 			 * not used.  The PMP must be reporting PHY
10157d77b247STejun Heo 			 * status change, schedule EH.
10167d77b247STejun Heo 			 */
10177d77b247STejun Heo 			ata_port_schedule_eh(ap);
10187d77b247STejun Heo 			return 1;
10197d77b247STejun Heo 		}
10207d77b247STejun Heo 	} else {
10217d77b247STejun Heo 		/* PMP is attached and SNTF is available */
10227d77b247STejun Heo 		struct ata_link *link;
10237d77b247STejun Heo 
10247d77b247STejun Heo 		/* check and notify ATAPI AN */
10257d77b247STejun Heo 		ata_port_for_each_link(link, ap) {
10267d77b247STejun Heo 			if (!(sntf & (1 << link->pmp)))
10277d77b247STejun Heo 				continue;
10287d77b247STejun Heo 
10297d77b247STejun Heo 			if ((link->device->class == ATA_DEV_ATAPI) &&
10307d77b247STejun Heo 			    (link->device->flags & ATA_DFLAG_AN))
10317d77b247STejun Heo 				ata_scsi_media_change_notify(link->device);
10327d77b247STejun Heo 		}
10337d77b247STejun Heo 
10347d77b247STejun Heo 		/* If PMP is reporting that PHY status of some
10357d77b247STejun Heo 		 * downstream ports has changed, schedule EH.
10367d77b247STejun Heo 		 */
10377d77b247STejun Heo 		if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
10387d77b247STejun Heo 			ata_port_schedule_eh(ap);
10397d77b247STejun Heo 			return 1;
10407d77b247STejun Heo 		}
10417d77b247STejun Heo 
10427d77b247STejun Heo 		return 0;
10437d77b247STejun Heo 	}
10447d77b247STejun Heo }
10457d77b247STejun Heo 
10467d77b247STejun Heo /**
1047c6fd2807SJeff Garzik  *	ata_eh_freeze_port - EH helper to freeze port
1048c6fd2807SJeff Garzik  *	@ap: ATA port to freeze
1049c6fd2807SJeff Garzik  *
1050c6fd2807SJeff Garzik  *	Freeze @ap.
1051c6fd2807SJeff Garzik  *
1052c6fd2807SJeff Garzik  *	LOCKING:
1053c6fd2807SJeff Garzik  *	None.
1054c6fd2807SJeff Garzik  */
1055c6fd2807SJeff Garzik void ata_eh_freeze_port(struct ata_port *ap)
1056c6fd2807SJeff Garzik {
1057c6fd2807SJeff Garzik 	unsigned long flags;
1058c6fd2807SJeff Garzik 
1059c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
1060c6fd2807SJeff Garzik 		return;
1061c6fd2807SJeff Garzik 
1062c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1063c6fd2807SJeff Garzik 	__ata_port_freeze(ap);
1064c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1065c6fd2807SJeff Garzik }
1066c6fd2807SJeff Garzik 
1067c6fd2807SJeff Garzik /**
1068c6fd2807SJeff Garzik  *	ata_port_thaw_port - EH helper to thaw port
1069c6fd2807SJeff Garzik  *	@ap: ATA port to thaw
1070c6fd2807SJeff Garzik  *
1071c6fd2807SJeff Garzik  *	Thaw frozen port @ap.
1072c6fd2807SJeff Garzik  *
1073c6fd2807SJeff Garzik  *	LOCKING:
1074c6fd2807SJeff Garzik  *	None.
1075c6fd2807SJeff Garzik  */
1076c6fd2807SJeff Garzik void ata_eh_thaw_port(struct ata_port *ap)
1077c6fd2807SJeff Garzik {
1078c6fd2807SJeff Garzik 	unsigned long flags;
1079c6fd2807SJeff Garzik 
1080c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
1081c6fd2807SJeff Garzik 		return;
1082c6fd2807SJeff Garzik 
1083c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1084c6fd2807SJeff Garzik 
1085c6fd2807SJeff Garzik 	ap->pflags &= ~ATA_PFLAG_FROZEN;
1086c6fd2807SJeff Garzik 
1087c6fd2807SJeff Garzik 	if (ap->ops->thaw)
1088c6fd2807SJeff Garzik 		ap->ops->thaw(ap);
1089c6fd2807SJeff Garzik 
1090c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1091c6fd2807SJeff Garzik 
109244877b4eSTejun Heo 	DPRINTK("ata%u port thawed\n", ap->print_id);
1093c6fd2807SJeff Garzik }
1094c6fd2807SJeff Garzik 
1095c6fd2807SJeff Garzik static void ata_eh_scsidone(struct scsi_cmnd *scmd)
1096c6fd2807SJeff Garzik {
1097c6fd2807SJeff Garzik 	/* nada */
1098c6fd2807SJeff Garzik }
1099c6fd2807SJeff Garzik 
1100c6fd2807SJeff Garzik static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
1101c6fd2807SJeff Garzik {
1102c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
1103c6fd2807SJeff Garzik 	struct scsi_cmnd *scmd = qc->scsicmd;
1104c6fd2807SJeff Garzik 	unsigned long flags;
1105c6fd2807SJeff Garzik 
1106c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1107c6fd2807SJeff Garzik 	qc->scsidone = ata_eh_scsidone;
1108c6fd2807SJeff Garzik 	__ata_qc_complete(qc);
1109c6fd2807SJeff Garzik 	WARN_ON(ata_tag_valid(qc->tag));
1110c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1111c6fd2807SJeff Garzik 
1112c6fd2807SJeff Garzik 	scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
1113c6fd2807SJeff Garzik }
1114c6fd2807SJeff Garzik 
1115c6fd2807SJeff Garzik /**
1116c6fd2807SJeff Garzik  *	ata_eh_qc_complete - Complete an active ATA command from EH
1117c6fd2807SJeff Garzik  *	@qc: Command to complete
1118c6fd2807SJeff Garzik  *
1119c6fd2807SJeff Garzik  *	Indicate to the mid and upper layers that an ATA command has
1120c6fd2807SJeff Garzik  *	completed.  To be used from EH.
1121c6fd2807SJeff Garzik  */
1122c6fd2807SJeff Garzik void ata_eh_qc_complete(struct ata_queued_cmd *qc)
1123c6fd2807SJeff Garzik {
1124c6fd2807SJeff Garzik 	struct scsi_cmnd *scmd = qc->scsicmd;
1125c6fd2807SJeff Garzik 	scmd->retries = scmd->allowed;
1126c6fd2807SJeff Garzik 	__ata_eh_qc_complete(qc);
1127c6fd2807SJeff Garzik }
1128c6fd2807SJeff Garzik 
1129c6fd2807SJeff Garzik /**
1130c6fd2807SJeff Garzik  *	ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
1131c6fd2807SJeff Garzik  *	@qc: Command to retry
1132c6fd2807SJeff Garzik  *
1133c6fd2807SJeff Garzik  *	Indicate to the mid and upper layers that an ATA command
1134c6fd2807SJeff Garzik  *	should be retried.  To be used from EH.
1135c6fd2807SJeff Garzik  *
1136c6fd2807SJeff Garzik  *	SCSI midlayer limits the number of retries to scmd->allowed.
1137c6fd2807SJeff Garzik  *	scmd->retries is decremented for commands which get retried
1138c6fd2807SJeff Garzik  *	due to unrelated failures (qc->err_mask is zero).
1139c6fd2807SJeff Garzik  */
1140c6fd2807SJeff Garzik void ata_eh_qc_retry(struct ata_queued_cmd *qc)
1141c6fd2807SJeff Garzik {
1142c6fd2807SJeff Garzik 	struct scsi_cmnd *scmd = qc->scsicmd;
1143c6fd2807SJeff Garzik 	if (!qc->err_mask && scmd->retries)
1144c6fd2807SJeff Garzik 		scmd->retries--;
1145c6fd2807SJeff Garzik 	__ata_eh_qc_complete(qc);
1146c6fd2807SJeff Garzik }
1147c6fd2807SJeff Garzik 
1148c6fd2807SJeff Garzik /**
1149c6fd2807SJeff Garzik  *	ata_eh_detach_dev - detach ATA device
1150c6fd2807SJeff Garzik  *	@dev: ATA device to detach
1151c6fd2807SJeff Garzik  *
1152c6fd2807SJeff Garzik  *	Detach @dev.
1153c6fd2807SJeff Garzik  *
1154c6fd2807SJeff Garzik  *	LOCKING:
1155c6fd2807SJeff Garzik  *	None.
1156c6fd2807SJeff Garzik  */
1157fb7fd614STejun Heo void ata_eh_detach_dev(struct ata_device *dev)
1158c6fd2807SJeff Garzik {
1159f58229f8STejun Heo 	struct ata_link *link = dev->link;
1160f58229f8STejun Heo 	struct ata_port *ap = link->ap;
1161c6fd2807SJeff Garzik 	unsigned long flags;
1162c6fd2807SJeff Garzik 
1163c6fd2807SJeff Garzik 	ata_dev_disable(dev);
1164c6fd2807SJeff Garzik 
1165c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1166c6fd2807SJeff Garzik 
1167c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_DETACH;
1168c6fd2807SJeff Garzik 
1169c6fd2807SJeff Garzik 	if (ata_scsi_offline_dev(dev)) {
1170c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_DETACHED;
1171c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
1172c6fd2807SJeff Garzik 	}
1173c6fd2807SJeff Garzik 
1174c6fd2807SJeff Garzik 	/* clear per-dev EH actions */
1175f58229f8STejun Heo 	ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
1176f58229f8STejun Heo 	ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
1177c6fd2807SJeff Garzik 
1178c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1179c6fd2807SJeff Garzik }
1180c6fd2807SJeff Garzik 
1181c6fd2807SJeff Garzik /**
1182c6fd2807SJeff Garzik  *	ata_eh_about_to_do - about to perform eh_action
1183955e57dfSTejun Heo  *	@link: target ATA link
1184c6fd2807SJeff Garzik  *	@dev: target ATA dev for per-dev action (can be NULL)
1185c6fd2807SJeff Garzik  *	@action: action about to be performed
1186c6fd2807SJeff Garzik  *
1187c6fd2807SJeff Garzik  *	Called just before performing EH actions to clear related bits
1188955e57dfSTejun Heo  *	in @link->eh_info such that eh actions are not unnecessarily
1189955e57dfSTejun Heo  *	repeated.
1190c6fd2807SJeff Garzik  *
1191c6fd2807SJeff Garzik  *	LOCKING:
1192c6fd2807SJeff Garzik  *	None.
1193c6fd2807SJeff Garzik  */
1194fb7fd614STejun Heo void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
1195c6fd2807SJeff Garzik 			unsigned int action)
1196c6fd2807SJeff Garzik {
1197955e57dfSTejun Heo 	struct ata_port *ap = link->ap;
1198955e57dfSTejun Heo 	struct ata_eh_info *ehi = &link->eh_info;
1199955e57dfSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
1200c6fd2807SJeff Garzik 	unsigned long flags;
1201c6fd2807SJeff Garzik 
1202c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1203c6fd2807SJeff Garzik 
1204955e57dfSTejun Heo 	ata_eh_clear_action(link, dev, ehi, action);
1205c6fd2807SJeff Garzik 
1206c6fd2807SJeff Garzik 	if (!(ehc->i.flags & ATA_EHI_QUIET))
1207c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_RECOVERED;
1208c6fd2807SJeff Garzik 
1209c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1210c6fd2807SJeff Garzik }
1211c6fd2807SJeff Garzik 
1212c6fd2807SJeff Garzik /**
1213c6fd2807SJeff Garzik  *	ata_eh_done - EH action complete
1214c6fd2807SJeff Garzik *	@ap: target ATA port
1215c6fd2807SJeff Garzik  *	@dev: target ATA dev for per-dev action (can be NULL)
1216c6fd2807SJeff Garzik  *	@action: action just completed
1217c6fd2807SJeff Garzik  *
1218c6fd2807SJeff Garzik  *	Called right after performing EH actions to clear related bits
1219955e57dfSTejun Heo  *	in @link->eh_context.
1220c6fd2807SJeff Garzik  *
1221c6fd2807SJeff Garzik  *	LOCKING:
1222c6fd2807SJeff Garzik  *	None.
1223c6fd2807SJeff Garzik  */
1224fb7fd614STejun Heo void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1225c6fd2807SJeff Garzik 		 unsigned int action)
1226c6fd2807SJeff Garzik {
1227955e57dfSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
12289af5c9c9STejun Heo 
1229955e57dfSTejun Heo 	ata_eh_clear_action(link, dev, &ehc->i, action);
1230c6fd2807SJeff Garzik }
1231c6fd2807SJeff Garzik 
1232c6fd2807SJeff Garzik /**
1233c6fd2807SJeff Garzik  *	ata_err_string - convert err_mask to descriptive string
1234c6fd2807SJeff Garzik  *	@err_mask: error mask to convert to string
1235c6fd2807SJeff Garzik  *
1236c6fd2807SJeff Garzik  *	Convert @err_mask to descriptive string.  Errors are
1237c6fd2807SJeff Garzik  *	prioritized according to severity and only the most severe
1238c6fd2807SJeff Garzik  *	error is reported.
1239c6fd2807SJeff Garzik  *
1240c6fd2807SJeff Garzik  *	LOCKING:
1241c6fd2807SJeff Garzik  *	None.
1242c6fd2807SJeff Garzik  *
1243c6fd2807SJeff Garzik  *	RETURNS:
1244c6fd2807SJeff Garzik  *	Descriptive string for @err_mask
1245c6fd2807SJeff Garzik  */
1246c6fd2807SJeff Garzik static const char *ata_err_string(unsigned int err_mask)
1247c6fd2807SJeff Garzik {
1248c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_HOST_BUS)
1249c6fd2807SJeff Garzik 		return "host bus error";
1250c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_ATA_BUS)
1251c6fd2807SJeff Garzik 		return "ATA bus error";
1252c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_TIMEOUT)
1253c6fd2807SJeff Garzik 		return "timeout";
1254c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_HSM)
1255c6fd2807SJeff Garzik 		return "HSM violation";
1256c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_SYSTEM)
1257c6fd2807SJeff Garzik 		return "internal error";
1258c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_MEDIA)
1259c6fd2807SJeff Garzik 		return "media error";
1260c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_INVALID)
1261c6fd2807SJeff Garzik 		return "invalid argument";
1262c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_DEV)
1263c6fd2807SJeff Garzik 		return "device error";
1264c6fd2807SJeff Garzik 	return "unknown error";
1265c6fd2807SJeff Garzik }
1266c6fd2807SJeff Garzik 
1267c6fd2807SJeff Garzik /**
1268c6fd2807SJeff Garzik  *	ata_read_log_page - read a specific log page
1269c6fd2807SJeff Garzik  *	@dev: target device
1270c6fd2807SJeff Garzik  *	@page: page to read
1271c6fd2807SJeff Garzik  *	@buf: buffer to store read page
1272c6fd2807SJeff Garzik  *	@sectors: number of sectors to read
1273c6fd2807SJeff Garzik  *
1274c6fd2807SJeff Garzik  *	Read log page using READ_LOG_EXT command.
1275c6fd2807SJeff Garzik  *
1276c6fd2807SJeff Garzik  *	LOCKING:
1277c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1278c6fd2807SJeff Garzik  *
1279c6fd2807SJeff Garzik  *	RETURNS:
1280c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
1281c6fd2807SJeff Garzik  */
1282c6fd2807SJeff Garzik static unsigned int ata_read_log_page(struct ata_device *dev,
1283c6fd2807SJeff Garzik 				      u8 page, void *buf, unsigned int sectors)
1284c6fd2807SJeff Garzik {
1285c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1286c6fd2807SJeff Garzik 	unsigned int err_mask;
1287c6fd2807SJeff Garzik 
1288c6fd2807SJeff Garzik 	DPRINTK("read log page - page %d\n", page);
1289c6fd2807SJeff Garzik 
1290c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
1291c6fd2807SJeff Garzik 	tf.command = ATA_CMD_READ_LOG_EXT;
1292c6fd2807SJeff Garzik 	tf.lbal = page;
1293c6fd2807SJeff Garzik 	tf.nsect = sectors;
1294c6fd2807SJeff Garzik 	tf.hob_nsect = sectors >> 8;
1295c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1296c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_PIO;
1297c6fd2807SJeff Garzik 
1298c6fd2807SJeff Garzik 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
12992b789108STejun Heo 				     buf, sectors * ATA_SECT_SIZE, 0);
1300c6fd2807SJeff Garzik 
1301c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
1302c6fd2807SJeff Garzik 	return err_mask;
1303c6fd2807SJeff Garzik }
1304c6fd2807SJeff Garzik 
1305c6fd2807SJeff Garzik /**
1306c6fd2807SJeff Garzik  *	ata_eh_read_log_10h - Read log page 10h for NCQ error details
1307c6fd2807SJeff Garzik  *	@dev: Device to read log page 10h from
1308c6fd2807SJeff Garzik  *	@tag: Resulting tag of the failed command
1309c6fd2807SJeff Garzik  *	@tf: Resulting taskfile registers of the failed command
1310c6fd2807SJeff Garzik  *
1311c6fd2807SJeff Garzik  *	Read log page 10h to obtain NCQ error details and clear error
1312c6fd2807SJeff Garzik  *	condition.
1313c6fd2807SJeff Garzik  *
1314c6fd2807SJeff Garzik  *	LOCKING:
1315c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1316c6fd2807SJeff Garzik  *
1317c6fd2807SJeff Garzik  *	RETURNS:
1318c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
1319c6fd2807SJeff Garzik  */
1320c6fd2807SJeff Garzik static int ata_eh_read_log_10h(struct ata_device *dev,
1321c6fd2807SJeff Garzik 			       int *tag, struct ata_taskfile *tf)
1322c6fd2807SJeff Garzik {
13239af5c9c9STejun Heo 	u8 *buf = dev->link->ap->sector_buf;
1324c6fd2807SJeff Garzik 	unsigned int err_mask;
1325c6fd2807SJeff Garzik 	u8 csum;
1326c6fd2807SJeff Garzik 	int i;
1327c6fd2807SJeff Garzik 
1328c6fd2807SJeff Garzik 	err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
1329c6fd2807SJeff Garzik 	if (err_mask)
1330c6fd2807SJeff Garzik 		return -EIO;
1331c6fd2807SJeff Garzik 
1332c6fd2807SJeff Garzik 	csum = 0;
1333c6fd2807SJeff Garzik 	for (i = 0; i < ATA_SECT_SIZE; i++)
1334c6fd2807SJeff Garzik 		csum += buf[i];
1335c6fd2807SJeff Garzik 	if (csum)
1336c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_WARNING,
1337c6fd2807SJeff Garzik 			       "invalid checksum 0x%x on log page 10h\n", csum);
1338c6fd2807SJeff Garzik 
1339c6fd2807SJeff Garzik 	if (buf[0] & 0x80)
1340c6fd2807SJeff Garzik 		return -ENOENT;
1341c6fd2807SJeff Garzik 
1342c6fd2807SJeff Garzik 	*tag = buf[0] & 0x1f;
1343c6fd2807SJeff Garzik 
1344c6fd2807SJeff Garzik 	tf->command = buf[2];
1345c6fd2807SJeff Garzik 	tf->feature = buf[3];
1346c6fd2807SJeff Garzik 	tf->lbal = buf[4];
1347c6fd2807SJeff Garzik 	tf->lbam = buf[5];
1348c6fd2807SJeff Garzik 	tf->lbah = buf[6];
1349c6fd2807SJeff Garzik 	tf->device = buf[7];
1350c6fd2807SJeff Garzik 	tf->hob_lbal = buf[8];
1351c6fd2807SJeff Garzik 	tf->hob_lbam = buf[9];
1352c6fd2807SJeff Garzik 	tf->hob_lbah = buf[10];
1353c6fd2807SJeff Garzik 	tf->nsect = buf[12];
1354c6fd2807SJeff Garzik 	tf->hob_nsect = buf[13];
1355c6fd2807SJeff Garzik 
1356c6fd2807SJeff Garzik 	return 0;
1357c6fd2807SJeff Garzik }
1358c6fd2807SJeff Garzik 
1359c6fd2807SJeff Garzik /**
1360c6fd2807SJeff Garzik  *	atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1361c6fd2807SJeff Garzik  *	@dev: device to perform REQUEST_SENSE to
1362c6fd2807SJeff Garzik  *	@sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
1363*3eabddb8STejun Heo  *	@dfl_sense_key: default sense key to use
1364c6fd2807SJeff Garzik  *
1365c6fd2807SJeff Garzik  *	Perform ATAPI REQUEST_SENSE after the device reported CHECK
1366c6fd2807SJeff Garzik  *	SENSE.  This function is EH helper.
1367c6fd2807SJeff Garzik  *
1368c6fd2807SJeff Garzik  *	LOCKING:
1369c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1370c6fd2807SJeff Garzik  *
1371c6fd2807SJeff Garzik  *	RETURNS:
1372c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask on failure
1373c6fd2807SJeff Garzik  */
1374*3eabddb8STejun Heo static unsigned int atapi_eh_request_sense(struct ata_device *dev,
1375*3eabddb8STejun Heo 					   u8 *sense_buf, u8 dfl_sense_key)
1376c6fd2807SJeff Garzik {
1377*3eabddb8STejun Heo 	u8 cdb[ATAPI_CDB_LEN] =
1378*3eabddb8STejun Heo 		{ REQUEST_SENSE, 0, 0, 0, SCSI_SENSE_BUFFERSIZE, 0 };
13799af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
1380c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1381c6fd2807SJeff Garzik 
1382c6fd2807SJeff Garzik 	DPRINTK("ATAPI request sense\n");
1383c6fd2807SJeff Garzik 
1384c6fd2807SJeff Garzik 	/* FIXME: is this needed? */
1385c6fd2807SJeff Garzik 	memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1386c6fd2807SJeff Garzik 
138756287768SAlbert Lee 	/* initialize sense_buf with the error register,
138856287768SAlbert Lee 	 * for the case where they are -not- overwritten
138956287768SAlbert Lee 	 */
1390c6fd2807SJeff Garzik 	sense_buf[0] = 0x70;
1391*3eabddb8STejun Heo 	sense_buf[2] = dfl_sense_key;
139256287768SAlbert Lee 
139356287768SAlbert Lee 	/* some devices time out if garbage left in tf */
139456287768SAlbert Lee 	ata_tf_init(dev, &tf);
1395c6fd2807SJeff Garzik 
1396c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1397c6fd2807SJeff Garzik 	tf.command = ATA_CMD_PACKET;
1398c6fd2807SJeff Garzik 
1399c6fd2807SJeff Garzik 	/* is it pointless to prefer PIO for "safety reasons"? */
1400c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_PIO_DMA) {
14010dc36888STejun Heo 		tf.protocol = ATAPI_PROT_DMA;
1402c6fd2807SJeff Garzik 		tf.feature |= ATAPI_PKT_DMA;
1403c6fd2807SJeff Garzik 	} else {
14040dc36888STejun Heo 		tf.protocol = ATAPI_PROT_PIO;
1405f2dfc1a1STejun Heo 		tf.lbam = SCSI_SENSE_BUFFERSIZE;
1406f2dfc1a1STejun Heo 		tf.lbah = 0;
1407c6fd2807SJeff Garzik 	}
1408c6fd2807SJeff Garzik 
1409c6fd2807SJeff Garzik 	return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
14102b789108STejun Heo 				 sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
1411c6fd2807SJeff Garzik }
1412c6fd2807SJeff Garzik 
1413c6fd2807SJeff Garzik /**
1414c6fd2807SJeff Garzik  *	ata_eh_analyze_serror - analyze SError for a failed port
14150260731fSTejun Heo  *	@link: ATA link to analyze SError for
1416c6fd2807SJeff Garzik  *
1417c6fd2807SJeff Garzik  *	Analyze SError if available and further determine cause of
1418c6fd2807SJeff Garzik  *	failure.
1419c6fd2807SJeff Garzik  *
1420c6fd2807SJeff Garzik  *	LOCKING:
1421c6fd2807SJeff Garzik  *	None.
1422c6fd2807SJeff Garzik  */
14230260731fSTejun Heo static void ata_eh_analyze_serror(struct ata_link *link)
1424c6fd2807SJeff Garzik {
14250260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
1426c6fd2807SJeff Garzik 	u32 serror = ehc->i.serror;
1427c6fd2807SJeff Garzik 	unsigned int err_mask = 0, action = 0;
1428f9df58cbSTejun Heo 	u32 hotplug_mask;
1429c6fd2807SJeff Garzik 
1430e0614db2STejun Heo 	if (serror & (SERR_PERSISTENT | SERR_DATA)) {
1431c6fd2807SJeff Garzik 		err_mask |= AC_ERR_ATA_BUS;
1432cf480626STejun Heo 		action |= ATA_EH_RESET;
1433c6fd2807SJeff Garzik 	}
1434c6fd2807SJeff Garzik 	if (serror & SERR_PROTOCOL) {
1435c6fd2807SJeff Garzik 		err_mask |= AC_ERR_HSM;
1436cf480626STejun Heo 		action |= ATA_EH_RESET;
1437c6fd2807SJeff Garzik 	}
1438c6fd2807SJeff Garzik 	if (serror & SERR_INTERNAL) {
1439c6fd2807SJeff Garzik 		err_mask |= AC_ERR_SYSTEM;
1440cf480626STejun Heo 		action |= ATA_EH_RESET;
1441c6fd2807SJeff Garzik 	}
1442f9df58cbSTejun Heo 
1443f9df58cbSTejun Heo 	/* Determine whether a hotplug event has occurred.  Both
1444f9df58cbSTejun Heo 	 * SError.N/X are considered hotplug events for enabled or
1445f9df58cbSTejun Heo 	 * host links.  For disabled PMP links, only N bit is
1446f9df58cbSTejun Heo 	 * considered as X bit is left at 1 for link plugging.
1447f9df58cbSTejun Heo 	 */
1448f9df58cbSTejun Heo 	hotplug_mask = 0;
1449f9df58cbSTejun Heo 
1450f9df58cbSTejun Heo 	if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
1451f9df58cbSTejun Heo 		hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
1452f9df58cbSTejun Heo 	else
1453f9df58cbSTejun Heo 		hotplug_mask = SERR_PHYRDY_CHG;
1454f9df58cbSTejun Heo 
1455f9df58cbSTejun Heo 	if (serror & hotplug_mask)
1456c6fd2807SJeff Garzik 		ata_ehi_hotplugged(&ehc->i);
1457c6fd2807SJeff Garzik 
1458c6fd2807SJeff Garzik 	ehc->i.err_mask |= err_mask;
1459c6fd2807SJeff Garzik 	ehc->i.action |= action;
1460c6fd2807SJeff Garzik }
1461c6fd2807SJeff Garzik 
1462c6fd2807SJeff Garzik /**
1463c6fd2807SJeff Garzik  *	ata_eh_analyze_ncq_error - analyze NCQ error
14640260731fSTejun Heo  *	@link: ATA link to analyze NCQ error for
1465c6fd2807SJeff Garzik  *
1466c6fd2807SJeff Garzik  *	Read log page 10h, determine the offending qc and acquire
1467c6fd2807SJeff Garzik  *	error status TF.  For NCQ device errors, all LLDDs have to do
1468c6fd2807SJeff Garzik  *	is setting AC_ERR_DEV in ehi->err_mask.  This function takes
1469c6fd2807SJeff Garzik  *	care of the rest.
1470c6fd2807SJeff Garzik  *
1471c6fd2807SJeff Garzik  *	LOCKING:
1472c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1473c6fd2807SJeff Garzik  */
147410acf3b0SMark Lord void ata_eh_analyze_ncq_error(struct ata_link *link)
1475c6fd2807SJeff Garzik {
14760260731fSTejun Heo 	struct ata_port *ap = link->ap;
14770260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
14780260731fSTejun Heo 	struct ata_device *dev = link->device;
1479c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
1480c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1481c6fd2807SJeff Garzik 	int tag, rc;
1482c6fd2807SJeff Garzik 
1483c6fd2807SJeff Garzik 	/* if frozen, we can't do much */
1484c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN)
1485c6fd2807SJeff Garzik 		return;
1486c6fd2807SJeff Garzik 
1487c6fd2807SJeff Garzik 	/* is it NCQ device error? */
14880260731fSTejun Heo 	if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1489c6fd2807SJeff Garzik 		return;
1490c6fd2807SJeff Garzik 
1491c6fd2807SJeff Garzik 	/* has LLDD analyzed already? */
1492c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1493c6fd2807SJeff Garzik 		qc = __ata_qc_from_tag(ap, tag);
1494c6fd2807SJeff Garzik 
1495c6fd2807SJeff Garzik 		if (!(qc->flags & ATA_QCFLAG_FAILED))
1496c6fd2807SJeff Garzik 			continue;
1497c6fd2807SJeff Garzik 
1498c6fd2807SJeff Garzik 		if (qc->err_mask)
1499c6fd2807SJeff Garzik 			return;
1500c6fd2807SJeff Garzik 	}
1501c6fd2807SJeff Garzik 
1502c6fd2807SJeff Garzik 	/* okay, this error is ours */
1503c6fd2807SJeff Garzik 	rc = ata_eh_read_log_10h(dev, &tag, &tf);
1504c6fd2807SJeff Garzik 	if (rc) {
15050260731fSTejun Heo 		ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
1506c6fd2807SJeff Garzik 				"(errno=%d)\n", rc);
1507c6fd2807SJeff Garzik 		return;
1508c6fd2807SJeff Garzik 	}
1509c6fd2807SJeff Garzik 
15100260731fSTejun Heo 	if (!(link->sactive & (1 << tag))) {
15110260731fSTejun Heo 		ata_link_printk(link, KERN_ERR, "log page 10h reported "
1512c6fd2807SJeff Garzik 				"inactive tag %d\n", tag);
1513c6fd2807SJeff Garzik 		return;
1514c6fd2807SJeff Garzik 	}
1515c6fd2807SJeff Garzik 
1516c6fd2807SJeff Garzik 	/* we've got the perpetrator, condemn it */
1517c6fd2807SJeff Garzik 	qc = __ata_qc_from_tag(ap, tag);
1518c6fd2807SJeff Garzik 	memcpy(&qc->result_tf, &tf, sizeof(tf));
1519a6116c9eSMark Lord 	qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
15205335b729STejun Heo 	qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
1521c6fd2807SJeff Garzik 	ehc->i.err_mask &= ~AC_ERR_DEV;
1522c6fd2807SJeff Garzik }
1523c6fd2807SJeff Garzik 
1524c6fd2807SJeff Garzik /**
1525c6fd2807SJeff Garzik  *	ata_eh_analyze_tf - analyze taskfile of a failed qc
1526c6fd2807SJeff Garzik  *	@qc: qc to analyze
1527c6fd2807SJeff Garzik  *	@tf: Taskfile registers to analyze
1528c6fd2807SJeff Garzik  *
1529c6fd2807SJeff Garzik  *	Analyze taskfile of @qc and further determine cause of
1530c6fd2807SJeff Garzik  *	failure.  This function also requests ATAPI sense data if
1531c6fd2807SJeff Garzik  *	avaliable.
1532c6fd2807SJeff Garzik  *
1533c6fd2807SJeff Garzik  *	LOCKING:
1534c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1535c6fd2807SJeff Garzik  *
1536c6fd2807SJeff Garzik  *	RETURNS:
1537c6fd2807SJeff Garzik  *	Determined recovery action
1538c6fd2807SJeff Garzik  */
1539c6fd2807SJeff Garzik static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1540c6fd2807SJeff Garzik 				      const struct ata_taskfile *tf)
1541c6fd2807SJeff Garzik {
1542c6fd2807SJeff Garzik 	unsigned int tmp, action = 0;
1543c6fd2807SJeff Garzik 	u8 stat = tf->command, err = tf->feature;
1544c6fd2807SJeff Garzik 
1545c6fd2807SJeff Garzik 	if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1546c6fd2807SJeff Garzik 		qc->err_mask |= AC_ERR_HSM;
1547cf480626STejun Heo 		return ATA_EH_RESET;
1548c6fd2807SJeff Garzik 	}
1549c6fd2807SJeff Garzik 
1550a51d644aSTejun Heo 	if (stat & (ATA_ERR | ATA_DF))
1551a51d644aSTejun Heo 		qc->err_mask |= AC_ERR_DEV;
1552a51d644aSTejun Heo 	else
1553c6fd2807SJeff Garzik 		return 0;
1554c6fd2807SJeff Garzik 
1555c6fd2807SJeff Garzik 	switch (qc->dev->class) {
1556c6fd2807SJeff Garzik 	case ATA_DEV_ATA:
1557c6fd2807SJeff Garzik 		if (err & ATA_ICRC)
1558c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_ATA_BUS;
1559c6fd2807SJeff Garzik 		if (err & ATA_UNC)
1560c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_MEDIA;
1561c6fd2807SJeff Garzik 		if (err & ATA_IDNF)
1562c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_INVALID;
1563c6fd2807SJeff Garzik 		break;
1564c6fd2807SJeff Garzik 
1565c6fd2807SJeff Garzik 	case ATA_DEV_ATAPI:
1566a569a30dSTejun Heo 		if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
1567*3eabddb8STejun Heo 			tmp = atapi_eh_request_sense(qc->dev,
1568*3eabddb8STejun Heo 						qc->scsicmd->sense_buffer,
1569*3eabddb8STejun Heo 						qc->result_tf.feature >> 4);
1570c6fd2807SJeff Garzik 			if (!tmp) {
1571a569a30dSTejun Heo 				/* ATA_QCFLAG_SENSE_VALID is used to
1572a569a30dSTejun Heo 				 * tell atapi_qc_complete() that sense
1573a569a30dSTejun Heo 				 * data is already valid.
1574c6fd2807SJeff Garzik 				 *
1575c6fd2807SJeff Garzik 				 * TODO: interpret sense data and set
1576c6fd2807SJeff Garzik 				 * appropriate err_mask.
1577c6fd2807SJeff Garzik 				 */
1578c6fd2807SJeff Garzik 				qc->flags |= ATA_QCFLAG_SENSE_VALID;
1579c6fd2807SJeff Garzik 			} else
1580c6fd2807SJeff Garzik 				qc->err_mask |= tmp;
1581c6fd2807SJeff Garzik 		}
1582a569a30dSTejun Heo 	}
1583c6fd2807SJeff Garzik 
1584c6fd2807SJeff Garzik 	if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1585cf480626STejun Heo 		action |= ATA_EH_RESET;
1586c6fd2807SJeff Garzik 
1587c6fd2807SJeff Garzik 	return action;
1588c6fd2807SJeff Garzik }
1589c6fd2807SJeff Garzik 
159076326ac1STejun Heo static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask,
159176326ac1STejun Heo 				   int *xfer_ok)
1592c6fd2807SJeff Garzik {
159376326ac1STejun Heo 	int base = 0;
159476326ac1STejun Heo 
159576326ac1STejun Heo 	if (!(eflags & ATA_EFLAG_DUBIOUS_XFER))
159676326ac1STejun Heo 		*xfer_ok = 1;
159776326ac1STejun Heo 
159876326ac1STejun Heo 	if (!*xfer_ok)
159975f9cafcSTejun Heo 		base = ATA_ECAT_DUBIOUS_NONE;
160076326ac1STejun Heo 
16017d47e8d4STejun Heo 	if (err_mask & AC_ERR_ATA_BUS)
160276326ac1STejun Heo 		return base + ATA_ECAT_ATA_BUS;
1603c6fd2807SJeff Garzik 
16047d47e8d4STejun Heo 	if (err_mask & AC_ERR_TIMEOUT)
160576326ac1STejun Heo 		return base + ATA_ECAT_TOUT_HSM;
16067d47e8d4STejun Heo 
16073884f7b0STejun Heo 	if (eflags & ATA_EFLAG_IS_IO) {
16087d47e8d4STejun Heo 		if (err_mask & AC_ERR_HSM)
160976326ac1STejun Heo 			return base + ATA_ECAT_TOUT_HSM;
16107d47e8d4STejun Heo 		if ((err_mask &
16117d47e8d4STejun Heo 		     (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
161276326ac1STejun Heo 			return base + ATA_ECAT_UNK_DEV;
1613c6fd2807SJeff Garzik 	}
1614c6fd2807SJeff Garzik 
1615c6fd2807SJeff Garzik 	return 0;
1616c6fd2807SJeff Garzik }
1617c6fd2807SJeff Garzik 
16187d47e8d4STejun Heo struct speed_down_verdict_arg {
1619c6fd2807SJeff Garzik 	u64 since;
162076326ac1STejun Heo 	int xfer_ok;
16213884f7b0STejun Heo 	int nr_errors[ATA_ECAT_NR];
1622c6fd2807SJeff Garzik };
1623c6fd2807SJeff Garzik 
16247d47e8d4STejun Heo static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
1625c6fd2807SJeff Garzik {
16267d47e8d4STejun Heo 	struct speed_down_verdict_arg *arg = void_arg;
162776326ac1STejun Heo 	int cat;
1628c6fd2807SJeff Garzik 
1629c6fd2807SJeff Garzik 	if (ent->timestamp < arg->since)
1630c6fd2807SJeff Garzik 		return -1;
1631c6fd2807SJeff Garzik 
163276326ac1STejun Heo 	cat = ata_eh_categorize_error(ent->eflags, ent->err_mask,
163376326ac1STejun Heo 				      &arg->xfer_ok);
16347d47e8d4STejun Heo 	arg->nr_errors[cat]++;
163576326ac1STejun Heo 
1636c6fd2807SJeff Garzik 	return 0;
1637c6fd2807SJeff Garzik }
1638c6fd2807SJeff Garzik 
1639c6fd2807SJeff Garzik /**
16407d47e8d4STejun Heo  *	ata_eh_speed_down_verdict - Determine speed down verdict
1641c6fd2807SJeff Garzik  *	@dev: Device of interest
1642c6fd2807SJeff Garzik  *
1643c6fd2807SJeff Garzik  *	This function examines error ring of @dev and determines
16447d47e8d4STejun Heo  *	whether NCQ needs to be turned off, transfer speed should be
16457d47e8d4STejun Heo  *	stepped down, or falling back to PIO is necessary.
1646c6fd2807SJeff Garzik  *
16473884f7b0STejun Heo  *	ECAT_ATA_BUS	: ATA_BUS error for any command
1648c6fd2807SJeff Garzik  *
16493884f7b0STejun Heo  *	ECAT_TOUT_HSM	: TIMEOUT for any command or HSM violation for
16503884f7b0STejun Heo  *			  IO commands
16517d47e8d4STejun Heo  *
16523884f7b0STejun Heo  *	ECAT_UNK_DEV	: Unknown DEV error for IO commands
1653c6fd2807SJeff Garzik  *
165476326ac1STejun Heo  *	ECAT_DUBIOUS_*	: Identical to above three but occurred while
165576326ac1STejun Heo  *			  data transfer hasn't been verified.
165676326ac1STejun Heo  *
16573884f7b0STejun Heo  *	Verdicts are
16587d47e8d4STejun Heo  *
16593884f7b0STejun Heo  *	NCQ_OFF		: Turn off NCQ.
16607d47e8d4STejun Heo  *
16613884f7b0STejun Heo  *	SPEED_DOWN	: Speed down transfer speed but don't fall back
16623884f7b0STejun Heo  *			  to PIO.
16633884f7b0STejun Heo  *
16643884f7b0STejun Heo  *	FALLBACK_TO_PIO	: Fall back to PIO.
16653884f7b0STejun Heo  *
16663884f7b0STejun Heo  *	Even if multiple verdicts are returned, only one action is
166776326ac1STejun Heo  *	taken per error.  An action triggered by non-DUBIOUS errors
166876326ac1STejun Heo  *	clears ering, while one triggered by DUBIOUS_* errors doesn't.
166976326ac1STejun Heo  *	This is to expedite speed down decisions right after device is
167076326ac1STejun Heo  *	initially configured.
16713884f7b0STejun Heo  *
167276326ac1STejun Heo  *	The followings are speed down rules.  #1 and #2 deal with
167376326ac1STejun Heo  *	DUBIOUS errors.
167476326ac1STejun Heo  *
167576326ac1STejun Heo  *	1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors
167676326ac1STejun Heo  *	   occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO.
167776326ac1STejun Heo  *
167876326ac1STejun Heo  *	2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors
167976326ac1STejun Heo  *	   occurred during last 5 mins, NCQ_OFF.
168076326ac1STejun Heo  *
168176326ac1STejun Heo  *	3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors
16823884f7b0STejun Heo  *	   ocurred during last 5 mins, FALLBACK_TO_PIO
16833884f7b0STejun Heo  *
168476326ac1STejun Heo  *	4. If more than 3 TOUT_HSM or UNK_DEV errors occurred
16853884f7b0STejun Heo  *	   during last 10 mins, NCQ_OFF.
16863884f7b0STejun Heo  *
168776326ac1STejun Heo  *	5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6
16883884f7b0STejun Heo  *	   UNK_DEV errors occurred during last 10 mins, SPEED_DOWN.
16897d47e8d4STejun Heo  *
1690c6fd2807SJeff Garzik  *	LOCKING:
1691c6fd2807SJeff Garzik  *	Inherited from caller.
1692c6fd2807SJeff Garzik  *
1693c6fd2807SJeff Garzik  *	RETURNS:
16947d47e8d4STejun Heo  *	OR of ATA_EH_SPDN_* flags.
1695c6fd2807SJeff Garzik  */
16967d47e8d4STejun Heo static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
1697c6fd2807SJeff Garzik {
16987d47e8d4STejun Heo 	const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
16997d47e8d4STejun Heo 	u64 j64 = get_jiffies_64();
17007d47e8d4STejun Heo 	struct speed_down_verdict_arg arg;
17017d47e8d4STejun Heo 	unsigned int verdict = 0;
1702c6fd2807SJeff Garzik 
17033884f7b0STejun Heo 	/* scan past 5 mins of error history */
17043884f7b0STejun Heo 	memset(&arg, 0, sizeof(arg));
17053884f7b0STejun Heo 	arg.since = j64 - min(j64, j5mins);
17063884f7b0STejun Heo 	ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
17073884f7b0STejun Heo 
170876326ac1STejun Heo 	if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] +
170976326ac1STejun Heo 	    arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1)
171076326ac1STejun Heo 		verdict |= ATA_EH_SPDN_SPEED_DOWN |
171176326ac1STejun Heo 			ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS;
171276326ac1STejun Heo 
171376326ac1STejun Heo 	if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] +
171476326ac1STejun Heo 	    arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1)
171576326ac1STejun Heo 		verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS;
171676326ac1STejun Heo 
17173884f7b0STejun Heo 	if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
17183884f7b0STejun Heo 	    arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1719663f99b8STejun Heo 	    arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
17203884f7b0STejun Heo 		verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
17213884f7b0STejun Heo 
17227d47e8d4STejun Heo 	/* scan past 10 mins of error history */
1723c6fd2807SJeff Garzik 	memset(&arg, 0, sizeof(arg));
17247d47e8d4STejun Heo 	arg.since = j64 - min(j64, j10mins);
17257d47e8d4STejun Heo 	ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1726c6fd2807SJeff Garzik 
17273884f7b0STejun Heo 	if (arg.nr_errors[ATA_ECAT_TOUT_HSM] +
17283884f7b0STejun Heo 	    arg.nr_errors[ATA_ECAT_UNK_DEV] > 3)
17297d47e8d4STejun Heo 		verdict |= ATA_EH_SPDN_NCQ_OFF;
17303884f7b0STejun Heo 
17313884f7b0STejun Heo 	if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
17323884f7b0STejun Heo 	    arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 ||
1733663f99b8STejun Heo 	    arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
17347d47e8d4STejun Heo 		verdict |= ATA_EH_SPDN_SPEED_DOWN;
1735c6fd2807SJeff Garzik 
17367d47e8d4STejun Heo 	return verdict;
1737c6fd2807SJeff Garzik }
1738c6fd2807SJeff Garzik 
1739c6fd2807SJeff Garzik /**
1740c6fd2807SJeff Garzik  *	ata_eh_speed_down - record error and speed down if necessary
1741c6fd2807SJeff Garzik  *	@dev: Failed device
17423884f7b0STejun Heo  *	@eflags: mask of ATA_EFLAG_* flags
1743c6fd2807SJeff Garzik  *	@err_mask: err_mask of the error
1744c6fd2807SJeff Garzik  *
1745c6fd2807SJeff Garzik  *	Record error and examine error history to determine whether
1746c6fd2807SJeff Garzik  *	adjusting transmission speed is necessary.  It also sets
1747c6fd2807SJeff Garzik  *	transmission limits appropriately if such adjustment is
1748c6fd2807SJeff Garzik  *	necessary.
1749c6fd2807SJeff Garzik  *
1750c6fd2807SJeff Garzik  *	LOCKING:
1751c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1752c6fd2807SJeff Garzik  *
1753c6fd2807SJeff Garzik  *	RETURNS:
17547d47e8d4STejun Heo  *	Determined recovery action.
1755c6fd2807SJeff Garzik  */
17563884f7b0STejun Heo static unsigned int ata_eh_speed_down(struct ata_device *dev,
17573884f7b0STejun Heo 				unsigned int eflags, unsigned int err_mask)
1758c6fd2807SJeff Garzik {
17593884f7b0STejun Heo 	struct ata_link *link = dev->link;
176076326ac1STejun Heo 	int xfer_ok = 0;
17617d47e8d4STejun Heo 	unsigned int verdict;
17627d47e8d4STejun Heo 	unsigned int action = 0;
17637d47e8d4STejun Heo 
17647d47e8d4STejun Heo 	/* don't bother if Cat-0 error */
176576326ac1STejun Heo 	if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0)
1766c6fd2807SJeff Garzik 		return 0;
1767c6fd2807SJeff Garzik 
1768c6fd2807SJeff Garzik 	/* record error and determine whether speed down is necessary */
17693884f7b0STejun Heo 	ata_ering_record(&dev->ering, eflags, err_mask);
17707d47e8d4STejun Heo 	verdict = ata_eh_speed_down_verdict(dev);
1771c6fd2807SJeff Garzik 
17727d47e8d4STejun Heo 	/* turn off NCQ? */
17737d47e8d4STejun Heo 	if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
17747d47e8d4STejun Heo 	    (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
17757d47e8d4STejun Heo 			   ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
17767d47e8d4STejun Heo 		dev->flags |= ATA_DFLAG_NCQ_OFF;
17777d47e8d4STejun Heo 		ata_dev_printk(dev, KERN_WARNING,
17787d47e8d4STejun Heo 			       "NCQ disabled due to excessive errors\n");
17797d47e8d4STejun Heo 		goto done;
17807d47e8d4STejun Heo 	}
1781c6fd2807SJeff Garzik 
17827d47e8d4STejun Heo 	/* speed down? */
17837d47e8d4STejun Heo 	if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1784c6fd2807SJeff Garzik 		/* speed down SATA link speed if possible */
17853884f7b0STejun Heo 		if (sata_down_spd_limit(link) == 0) {
1786cf480626STejun Heo 			action |= ATA_EH_RESET;
17877d47e8d4STejun Heo 			goto done;
17887d47e8d4STejun Heo 		}
1789c6fd2807SJeff Garzik 
1790c6fd2807SJeff Garzik 		/* lower transfer mode */
17917d47e8d4STejun Heo 		if (dev->spdn_cnt < 2) {
17927d47e8d4STejun Heo 			static const int dma_dnxfer_sel[] =
17937d47e8d4STejun Heo 				{ ATA_DNXFER_DMA, ATA_DNXFER_40C };
17947d47e8d4STejun Heo 			static const int pio_dnxfer_sel[] =
17957d47e8d4STejun Heo 				{ ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
17967d47e8d4STejun Heo 			int sel;
1797c6fd2807SJeff Garzik 
17987d47e8d4STejun Heo 			if (dev->xfer_shift != ATA_SHIFT_PIO)
17997d47e8d4STejun Heo 				sel = dma_dnxfer_sel[dev->spdn_cnt];
18007d47e8d4STejun Heo 			else
18017d47e8d4STejun Heo 				sel = pio_dnxfer_sel[dev->spdn_cnt];
18027d47e8d4STejun Heo 
18037d47e8d4STejun Heo 			dev->spdn_cnt++;
18047d47e8d4STejun Heo 
18057d47e8d4STejun Heo 			if (ata_down_xfermask_limit(dev, sel) == 0) {
1806cf480626STejun Heo 				action |= ATA_EH_RESET;
18077d47e8d4STejun Heo 				goto done;
18087d47e8d4STejun Heo 			}
18097d47e8d4STejun Heo 		}
18107d47e8d4STejun Heo 	}
18117d47e8d4STejun Heo 
18127d47e8d4STejun Heo 	/* Fall back to PIO?  Slowing down to PIO is meaningless for
1813663f99b8STejun Heo 	 * SATA ATA devices.  Consider it only for PATA and SATAPI.
18147d47e8d4STejun Heo 	 */
18157d47e8d4STejun Heo 	if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
1816663f99b8STejun Heo 	    (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) &&
18177d47e8d4STejun Heo 	    (dev->xfer_shift != ATA_SHIFT_PIO)) {
18187d47e8d4STejun Heo 		if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
18197d47e8d4STejun Heo 			dev->spdn_cnt = 0;
1820cf480626STejun Heo 			action |= ATA_EH_RESET;
18217d47e8d4STejun Heo 			goto done;
18227d47e8d4STejun Heo 		}
18237d47e8d4STejun Heo 	}
18247d47e8d4STejun Heo 
1825c6fd2807SJeff Garzik 	return 0;
18267d47e8d4STejun Heo  done:
18277d47e8d4STejun Heo 	/* device has been slowed down, blow error history */
182876326ac1STejun Heo 	if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS))
18297d47e8d4STejun Heo 		ata_ering_clear(&dev->ering);
18307d47e8d4STejun Heo 	return action;
1831c6fd2807SJeff Garzik }
1832c6fd2807SJeff Garzik 
1833c6fd2807SJeff Garzik /**
18349b1e2658STejun Heo  *	ata_eh_link_autopsy - analyze error and determine recovery action
18359b1e2658STejun Heo  *	@link: host link to perform autopsy on
1836c6fd2807SJeff Garzik  *
18370260731fSTejun Heo  *	Analyze why @link failed and determine which recovery actions
18380260731fSTejun Heo  *	are needed.  This function also sets more detailed AC_ERR_*
18390260731fSTejun Heo  *	values and fills sense data for ATAPI CHECK SENSE.
1840c6fd2807SJeff Garzik  *
1841c6fd2807SJeff Garzik  *	LOCKING:
1842c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1843c6fd2807SJeff Garzik  */
18449b1e2658STejun Heo static void ata_eh_link_autopsy(struct ata_link *link)
1845c6fd2807SJeff Garzik {
18460260731fSTejun Heo 	struct ata_port *ap = link->ap;
1847936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
1848dfcc173dSTejun Heo 	struct ata_device *dev;
18493884f7b0STejun Heo 	unsigned int all_err_mask = 0, eflags = 0;
18503884f7b0STejun Heo 	int tag;
1851c6fd2807SJeff Garzik 	u32 serror;
1852c6fd2807SJeff Garzik 	int rc;
1853c6fd2807SJeff Garzik 
1854c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
1855c6fd2807SJeff Garzik 
1856c6fd2807SJeff Garzik 	if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1857c6fd2807SJeff Garzik 		return;
1858c6fd2807SJeff Garzik 
1859c6fd2807SJeff Garzik 	/* obtain and analyze SError */
1860936fd732STejun Heo 	rc = sata_scr_read(link, SCR_ERROR, &serror);
1861c6fd2807SJeff Garzik 	if (rc == 0) {
1862c6fd2807SJeff Garzik 		ehc->i.serror |= serror;
18630260731fSTejun Heo 		ata_eh_analyze_serror(link);
18644e57c517STejun Heo 	} else if (rc != -EOPNOTSUPP) {
1865cf480626STejun Heo 		/* SError read failed, force reset and probing */
1866b558edddSTejun Heo 		ehc->i.probe_mask |= ATA_ALL_DEVICES;
1867cf480626STejun Heo 		ehc->i.action |= ATA_EH_RESET;
18684e57c517STejun Heo 		ehc->i.err_mask |= AC_ERR_OTHER;
18694e57c517STejun Heo 	}
1870c6fd2807SJeff Garzik 
1871c6fd2807SJeff Garzik 	/* analyze NCQ failure */
18720260731fSTejun Heo 	ata_eh_analyze_ncq_error(link);
1873c6fd2807SJeff Garzik 
1874c6fd2807SJeff Garzik 	/* any real error trumps AC_ERR_OTHER */
1875c6fd2807SJeff Garzik 	if (ehc->i.err_mask & ~AC_ERR_OTHER)
1876c6fd2807SJeff Garzik 		ehc->i.err_mask &= ~AC_ERR_OTHER;
1877c6fd2807SJeff Garzik 
1878c6fd2807SJeff Garzik 	all_err_mask |= ehc->i.err_mask;
1879c6fd2807SJeff Garzik 
1880c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1881c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1882c6fd2807SJeff Garzik 
18830260731fSTejun Heo 		if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
1884c6fd2807SJeff Garzik 			continue;
1885c6fd2807SJeff Garzik 
1886c6fd2807SJeff Garzik 		/* inherit upper level err_mask */
1887c6fd2807SJeff Garzik 		qc->err_mask |= ehc->i.err_mask;
1888c6fd2807SJeff Garzik 
1889c6fd2807SJeff Garzik 		/* analyze TF */
1890c6fd2807SJeff Garzik 		ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1891c6fd2807SJeff Garzik 
1892c6fd2807SJeff Garzik 		/* DEV errors are probably spurious in case of ATA_BUS error */
1893c6fd2807SJeff Garzik 		if (qc->err_mask & AC_ERR_ATA_BUS)
1894c6fd2807SJeff Garzik 			qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1895c6fd2807SJeff Garzik 					  AC_ERR_INVALID);
1896c6fd2807SJeff Garzik 
1897c6fd2807SJeff Garzik 		/* any real error trumps unknown error */
1898c6fd2807SJeff Garzik 		if (qc->err_mask & ~AC_ERR_OTHER)
1899c6fd2807SJeff Garzik 			qc->err_mask &= ~AC_ERR_OTHER;
1900c6fd2807SJeff Garzik 
1901c6fd2807SJeff Garzik 		/* SENSE_VALID trumps dev/unknown error and revalidation */
1902f90f0828STejun Heo 		if (qc->flags & ATA_QCFLAG_SENSE_VALID)
1903c6fd2807SJeff Garzik 			qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1904c6fd2807SJeff Garzik 
190503faab78STejun Heo 		/* determine whether the command is worth retrying */
190603faab78STejun Heo 		if (!(qc->err_mask & AC_ERR_INVALID) &&
190703faab78STejun Heo 		    ((qc->flags & ATA_QCFLAG_IO) || qc->err_mask != AC_ERR_DEV))
190803faab78STejun Heo 			qc->flags |= ATA_QCFLAG_RETRY;
190903faab78STejun Heo 
1910c6fd2807SJeff Garzik 		/* accumulate error info */
1911c6fd2807SJeff Garzik 		ehc->i.dev = qc->dev;
1912c6fd2807SJeff Garzik 		all_err_mask |= qc->err_mask;
1913c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_IO)
19143884f7b0STejun Heo 			eflags |= ATA_EFLAG_IS_IO;
1915c6fd2807SJeff Garzik 	}
1916c6fd2807SJeff Garzik 
1917c6fd2807SJeff Garzik 	/* enforce default EH actions */
1918c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN ||
1919c6fd2807SJeff Garzik 	    all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
1920cf480626STejun Heo 		ehc->i.action |= ATA_EH_RESET;
19213884f7b0STejun Heo 	else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) ||
19223884f7b0STejun Heo 		 (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV)))
1923c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_REVALIDATE;
1924c6fd2807SJeff Garzik 
1925dfcc173dSTejun Heo 	/* If we have offending qcs and the associated failed device,
1926dfcc173dSTejun Heo 	 * perform per-dev EH action only on the offending device.
1927dfcc173dSTejun Heo 	 */
1928c6fd2807SJeff Garzik 	if (ehc->i.dev) {
1929c6fd2807SJeff Garzik 		ehc->i.dev_action[ehc->i.dev->devno] |=
1930c6fd2807SJeff Garzik 			ehc->i.action & ATA_EH_PERDEV_MASK;
1931c6fd2807SJeff Garzik 		ehc->i.action &= ~ATA_EH_PERDEV_MASK;
1932c6fd2807SJeff Garzik 	}
1933c6fd2807SJeff Garzik 
19342695e366STejun Heo 	/* propagate timeout to host link */
19352695e366STejun Heo 	if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link))
19362695e366STejun Heo 		ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT;
19372695e366STejun Heo 
19382695e366STejun Heo 	/* record error and consider speeding down */
1939dfcc173dSTejun Heo 	dev = ehc->i.dev;
19402695e366STejun Heo 	if (!dev && ((ata_link_max_devices(link) == 1 &&
19412695e366STejun Heo 		      ata_dev_enabled(link->device))))
1942dfcc173dSTejun Heo 	    dev = link->device;
1943dfcc173dSTejun Heo 
194476326ac1STejun Heo 	if (dev) {
194576326ac1STejun Heo 		if (dev->flags & ATA_DFLAG_DUBIOUS_XFER)
194676326ac1STejun Heo 			eflags |= ATA_EFLAG_DUBIOUS_XFER;
19473884f7b0STejun Heo 		ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask);
194876326ac1STejun Heo 	}
1949dfcc173dSTejun Heo 
1950c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
1951c6fd2807SJeff Garzik }
1952c6fd2807SJeff Garzik 
1953c6fd2807SJeff Garzik /**
19549b1e2658STejun Heo  *	ata_eh_autopsy - analyze error and determine recovery action
19559b1e2658STejun Heo  *	@ap: host port to perform autopsy on
19569b1e2658STejun Heo  *
19579b1e2658STejun Heo  *	Analyze all links of @ap and determine why they failed and
19589b1e2658STejun Heo  *	which recovery actions are needed.
19599b1e2658STejun Heo  *
19609b1e2658STejun Heo  *	LOCKING:
19619b1e2658STejun Heo  *	Kernel thread context (may sleep).
19629b1e2658STejun Heo  */
1963fb7fd614STejun Heo void ata_eh_autopsy(struct ata_port *ap)
19649b1e2658STejun Heo {
19659b1e2658STejun Heo 	struct ata_link *link;
19669b1e2658STejun Heo 
19672695e366STejun Heo 	ata_port_for_each_link(link, ap)
19689b1e2658STejun Heo 		ata_eh_link_autopsy(link);
19692695e366STejun Heo 
19702695e366STejun Heo 	/* Autopsy of fanout ports can affect host link autopsy.
19712695e366STejun Heo 	 * Perform host link autopsy last.
19722695e366STejun Heo 	 */
1973071f44b1STejun Heo 	if (sata_pmp_attached(ap))
19742695e366STejun Heo 		ata_eh_link_autopsy(&ap->link);
19759b1e2658STejun Heo }
19769b1e2658STejun Heo 
19779b1e2658STejun Heo /**
19789b1e2658STejun Heo  *	ata_eh_link_report - report error handling to user
19790260731fSTejun Heo  *	@link: ATA link EH is going on
1980c6fd2807SJeff Garzik  *
1981c6fd2807SJeff Garzik  *	Report EH to user.
1982c6fd2807SJeff Garzik  *
1983c6fd2807SJeff Garzik  *	LOCKING:
1984c6fd2807SJeff Garzik  *	None.
1985c6fd2807SJeff Garzik  */
19869b1e2658STejun Heo static void ata_eh_link_report(struct ata_link *link)
1987c6fd2807SJeff Garzik {
19880260731fSTejun Heo 	struct ata_port *ap = link->ap;
19890260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
1990c6fd2807SJeff Garzik 	const char *frozen, *desc;
1991a1e10f7eSTejun Heo 	char tries_buf[6];
1992c6fd2807SJeff Garzik 	int tag, nr_failed = 0;
1993c6fd2807SJeff Garzik 
199494ff3d54STejun Heo 	if (ehc->i.flags & ATA_EHI_QUIET)
199594ff3d54STejun Heo 		return;
199694ff3d54STejun Heo 
1997c6fd2807SJeff Garzik 	desc = NULL;
1998c6fd2807SJeff Garzik 	if (ehc->i.desc[0] != '\0')
1999c6fd2807SJeff Garzik 		desc = ehc->i.desc;
2000c6fd2807SJeff Garzik 
2001c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2002c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2003c6fd2807SJeff Garzik 
2004e027bd36STejun Heo 		if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link ||
2005e027bd36STejun Heo 		    ((qc->flags & ATA_QCFLAG_QUIET) &&
2006e027bd36STejun Heo 		     qc->err_mask == AC_ERR_DEV))
2007c6fd2807SJeff Garzik 			continue;
2008c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
2009c6fd2807SJeff Garzik 			continue;
2010c6fd2807SJeff Garzik 
2011c6fd2807SJeff Garzik 		nr_failed++;
2012c6fd2807SJeff Garzik 	}
2013c6fd2807SJeff Garzik 
2014c6fd2807SJeff Garzik 	if (!nr_failed && !ehc->i.err_mask)
2015c6fd2807SJeff Garzik 		return;
2016c6fd2807SJeff Garzik 
2017c6fd2807SJeff Garzik 	frozen = "";
2018c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN)
2019c6fd2807SJeff Garzik 		frozen = " frozen";
2020c6fd2807SJeff Garzik 
2021a1e10f7eSTejun Heo 	memset(tries_buf, 0, sizeof(tries_buf));
2022a1e10f7eSTejun Heo 	if (ap->eh_tries < ATA_EH_MAX_TRIES)
2023a1e10f7eSTejun Heo 		snprintf(tries_buf, sizeof(tries_buf) - 1, " t%d",
2024a1e10f7eSTejun Heo 			 ap->eh_tries);
2025a1e10f7eSTejun Heo 
2026c6fd2807SJeff Garzik 	if (ehc->i.dev) {
2027c6fd2807SJeff Garzik 		ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
2028a1e10f7eSTejun Heo 			       "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2029a1e10f7eSTejun Heo 			       ehc->i.err_mask, link->sactive, ehc->i.serror,
2030a1e10f7eSTejun Heo 			       ehc->i.action, frozen, tries_buf);
2031c6fd2807SJeff Garzik 		if (desc)
2032b64bbc39STejun Heo 			ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
2033c6fd2807SJeff Garzik 	} else {
20340260731fSTejun Heo 		ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
2035a1e10f7eSTejun Heo 				"SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2036a1e10f7eSTejun Heo 				ehc->i.err_mask, link->sactive, ehc->i.serror,
2037a1e10f7eSTejun Heo 				ehc->i.action, frozen, tries_buf);
2038c6fd2807SJeff Garzik 		if (desc)
20390260731fSTejun Heo 			ata_link_printk(link, KERN_ERR, "%s\n", desc);
2040c6fd2807SJeff Garzik 	}
2041c6fd2807SJeff Garzik 
20421333e194SRobert Hancock 	if (ehc->i.serror)
20431333e194SRobert Hancock 		ata_port_printk(ap, KERN_ERR,
20441333e194SRobert Hancock 		  "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
20451333e194SRobert Hancock 		  ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
20461333e194SRobert Hancock 		  ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
20471333e194SRobert Hancock 		  ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
20481333e194SRobert Hancock 		  ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
20491333e194SRobert Hancock 		  ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
20501333e194SRobert Hancock 		  ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
20511333e194SRobert Hancock 		  ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
20521333e194SRobert Hancock 		  ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
20531333e194SRobert Hancock 		  ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
20541333e194SRobert Hancock 		  ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
20551333e194SRobert Hancock 		  ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
20561333e194SRobert Hancock 		  ehc->i.serror & SERR_CRC ? "BadCRC " : "",
20571333e194SRobert Hancock 		  ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
20581333e194SRobert Hancock 		  ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
20591333e194SRobert Hancock 		  ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
20601333e194SRobert Hancock 		  ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
20611333e194SRobert Hancock 		  ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
20621333e194SRobert Hancock 
2063c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2064c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
20658a937581STejun Heo 		struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
2066abb6a889STejun Heo 		const u8 *cdb = qc->cdb;
2067abb6a889STejun Heo 		char data_buf[20] = "";
2068abb6a889STejun Heo 		char cdb_buf[70] = "";
2069c6fd2807SJeff Garzik 
20700260731fSTejun Heo 		if (!(qc->flags & ATA_QCFLAG_FAILED) ||
20710260731fSTejun Heo 		    qc->dev->link != link || !qc->err_mask)
2072c6fd2807SJeff Garzik 			continue;
2073c6fd2807SJeff Garzik 
2074abb6a889STejun Heo 		if (qc->dma_dir != DMA_NONE) {
2075abb6a889STejun Heo 			static const char *dma_str[] = {
2076abb6a889STejun Heo 				[DMA_BIDIRECTIONAL]	= "bidi",
2077abb6a889STejun Heo 				[DMA_TO_DEVICE]		= "out",
2078abb6a889STejun Heo 				[DMA_FROM_DEVICE]	= "in",
2079abb6a889STejun Heo 			};
2080abb6a889STejun Heo 			static const char *prot_str[] = {
2081abb6a889STejun Heo 				[ATA_PROT_PIO]		= "pio",
2082abb6a889STejun Heo 				[ATA_PROT_DMA]		= "dma",
2083abb6a889STejun Heo 				[ATA_PROT_NCQ]		= "ncq",
20840dc36888STejun Heo 				[ATAPI_PROT_PIO]	= "pio",
20850dc36888STejun Heo 				[ATAPI_PROT_DMA]	= "dma",
2086abb6a889STejun Heo 			};
2087abb6a889STejun Heo 
2088abb6a889STejun Heo 			snprintf(data_buf, sizeof(data_buf), " %s %u %s",
2089abb6a889STejun Heo 				 prot_str[qc->tf.protocol], qc->nbytes,
2090abb6a889STejun Heo 				 dma_str[qc->dma_dir]);
2091abb6a889STejun Heo 		}
2092abb6a889STejun Heo 
2093e39eec13SJeff Garzik 		if (ata_is_atapi(qc->tf.protocol))
2094abb6a889STejun Heo 			snprintf(cdb_buf, sizeof(cdb_buf),
2095abb6a889STejun Heo 				 "cdb %02x %02x %02x %02x %02x %02x %02x %02x  "
2096abb6a889STejun Heo 				 "%02x %02x %02x %02x %02x %02x %02x %02x\n         ",
2097abb6a889STejun Heo 				 cdb[0], cdb[1], cdb[2], cdb[3],
2098abb6a889STejun Heo 				 cdb[4], cdb[5], cdb[6], cdb[7],
2099abb6a889STejun Heo 				 cdb[8], cdb[9], cdb[10], cdb[11],
2100abb6a889STejun Heo 				 cdb[12], cdb[13], cdb[14], cdb[15]);
2101abb6a889STejun Heo 
21028a937581STejun Heo 		ata_dev_printk(qc->dev, KERN_ERR,
21038a937581STejun Heo 			"cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
2104abb6a889STejun Heo 			"tag %d%s\n         %s"
21058a937581STejun Heo 			"res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
21065335b729STejun Heo 			"Emask 0x%x (%s)%s\n",
21078a937581STejun Heo 			cmd->command, cmd->feature, cmd->nsect,
21088a937581STejun Heo 			cmd->lbal, cmd->lbam, cmd->lbah,
21098a937581STejun Heo 			cmd->hob_feature, cmd->hob_nsect,
21108a937581STejun Heo 			cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
2111abb6a889STejun Heo 			cmd->device, qc->tag, data_buf, cdb_buf,
21128a937581STejun Heo 			res->command, res->feature, res->nsect,
21138a937581STejun Heo 			res->lbal, res->lbam, res->lbah,
21148a937581STejun Heo 			res->hob_feature, res->hob_nsect,
21158a937581STejun Heo 			res->hob_lbal, res->hob_lbam, res->hob_lbah,
21165335b729STejun Heo 			res->device, qc->err_mask, ata_err_string(qc->err_mask),
21175335b729STejun Heo 			qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
21181333e194SRobert Hancock 
21191333e194SRobert Hancock 		if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
21201333e194SRobert Hancock 				    ATA_ERR)) {
21211333e194SRobert Hancock 			if (res->command & ATA_BUSY)
21221333e194SRobert Hancock 				ata_dev_printk(qc->dev, KERN_ERR,
21231333e194SRobert Hancock 				  "status: { Busy }\n");
21241333e194SRobert Hancock 			else
21251333e194SRobert Hancock 				ata_dev_printk(qc->dev, KERN_ERR,
21261333e194SRobert Hancock 				  "status: { %s%s%s%s}\n",
21271333e194SRobert Hancock 				  res->command & ATA_DRDY ? "DRDY " : "",
21281333e194SRobert Hancock 				  res->command & ATA_DF ? "DF " : "",
21291333e194SRobert Hancock 				  res->command & ATA_DRQ ? "DRQ " : "",
21301333e194SRobert Hancock 				  res->command & ATA_ERR ? "ERR " : "");
21311333e194SRobert Hancock 		}
21321333e194SRobert Hancock 
21331333e194SRobert Hancock 		if (cmd->command != ATA_CMD_PACKET &&
21341333e194SRobert Hancock 		    (res->feature & (ATA_ICRC | ATA_UNC | ATA_IDNF |
21351333e194SRobert Hancock 				     ATA_ABORTED)))
21361333e194SRobert Hancock 			ata_dev_printk(qc->dev, KERN_ERR,
21371333e194SRobert Hancock 			  "error: { %s%s%s%s}\n",
21381333e194SRobert Hancock 			  res->feature & ATA_ICRC ? "ICRC " : "",
21391333e194SRobert Hancock 			  res->feature & ATA_UNC ? "UNC " : "",
21401333e194SRobert Hancock 			  res->feature & ATA_IDNF ? "IDNF " : "",
21411333e194SRobert Hancock 			  res->feature & ATA_ABORTED ? "ABRT " : "");
2142c6fd2807SJeff Garzik 	}
2143c6fd2807SJeff Garzik }
2144c6fd2807SJeff Garzik 
21459b1e2658STejun Heo /**
21469b1e2658STejun Heo  *	ata_eh_report - report error handling to user
21479b1e2658STejun Heo  *	@ap: ATA port to report EH about
21489b1e2658STejun Heo  *
21499b1e2658STejun Heo  *	Report EH to user.
21509b1e2658STejun Heo  *
21519b1e2658STejun Heo  *	LOCKING:
21529b1e2658STejun Heo  *	None.
21539b1e2658STejun Heo  */
2154fb7fd614STejun Heo void ata_eh_report(struct ata_port *ap)
21559b1e2658STejun Heo {
21569b1e2658STejun Heo 	struct ata_link *link;
21579b1e2658STejun Heo 
21589b1e2658STejun Heo 	__ata_port_for_each_link(link, ap)
21599b1e2658STejun Heo 		ata_eh_link_report(link);
21609b1e2658STejun Heo }
21619b1e2658STejun Heo 
2162cc0680a5STejun Heo static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
2163d4b2bab4STejun Heo 			unsigned int *classes, unsigned long deadline)
2164c6fd2807SJeff Garzik {
2165f58229f8STejun Heo 	struct ata_device *dev;
2166c6fd2807SJeff Garzik 
2167cc0680a5STejun Heo 	ata_link_for_each_dev(dev, link)
2168f58229f8STejun Heo 		classes[dev->devno] = ATA_DEV_UNKNOWN;
2169c6fd2807SJeff Garzik 
2170f046519fSTejun Heo 	return reset(link, classes, deadline);
2171c6fd2807SJeff Garzik }
2172c6fd2807SJeff Garzik 
2173ae791c05STejun Heo static int ata_eh_followup_srst_needed(struct ata_link *link,
2174ae791c05STejun Heo 				       int rc, int classify,
2175c6fd2807SJeff Garzik 				       const unsigned int *classes)
2176c6fd2807SJeff Garzik {
217745db2f6cSTejun Heo 	if ((link->flags & ATA_LFLAG_NO_SRST) || ata_link_offline(link))
2178ae791c05STejun Heo 		return 0;
2179305d2a1aSTejun Heo 	if (rc == -EAGAIN) {
2180305d2a1aSTejun Heo 		if (classify)
2181c6fd2807SJeff Garzik 			return 1;
2182305d2a1aSTejun Heo 		rc = 0;
2183305d2a1aSTejun Heo 	}
2184c6fd2807SJeff Garzik 	if (rc != 0)
2185c6fd2807SJeff Garzik 		return 0;
2186071f44b1STejun Heo 	if (sata_pmp_supported(link->ap) && ata_is_host_link(link))
21873495de73STejun Heo 		return 1;
2188c6fd2807SJeff Garzik 	return 0;
2189c6fd2807SJeff Garzik }
2190c6fd2807SJeff Garzik 
2191fb7fd614STejun Heo int ata_eh_reset(struct ata_link *link, int classify,
2192c6fd2807SJeff Garzik 		 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
2193c6fd2807SJeff Garzik 		 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
2194c6fd2807SJeff Garzik {
2195afaa5c37STejun Heo 	struct ata_port *ap = link->ap;
2196936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
2197c6fd2807SJeff Garzik 	unsigned int *classes = ehc->classes;
2198416dc9edSTejun Heo 	unsigned int lflags = link->flags;
2199c6fd2807SJeff Garzik 	int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
2200d8af0eb6STejun Heo 	int max_tries = 0, try = 0;
2201f58229f8STejun Heo 	struct ata_device *dev;
2202416dc9edSTejun Heo 	unsigned long deadline, now;
2203c6fd2807SJeff Garzik 	ata_reset_fn_t reset;
2204afaa5c37STejun Heo 	unsigned long flags;
2205416dc9edSTejun Heo 	u32 sstatus;
2206f046519fSTejun Heo 	int nr_known, rc;
2207c6fd2807SJeff Garzik 
2208932648b0STejun Heo 	/*
2209932648b0STejun Heo 	 * Prepare to reset
2210932648b0STejun Heo 	 */
2211d8af0eb6STejun Heo 	while (ata_eh_reset_timeouts[max_tries] != ULONG_MAX)
2212d8af0eb6STejun Heo 		max_tries++;
2213d8af0eb6STejun Heo 
22140a2c0f56STejun Heo 	now = jiffies;
22150a2c0f56STejun Heo 	deadline = ata_deadline(ehc->last_reset, ATA_EH_RESET_COOL_DOWN);
22160a2c0f56STejun Heo 	if (time_before(now, deadline))
22170a2c0f56STejun Heo 		schedule_timeout_uninterruptible(deadline - now);
22180a2c0f56STejun Heo 
2219afaa5c37STejun Heo 	spin_lock_irqsave(ap->lock, flags);
2220afaa5c37STejun Heo 	ap->pflags |= ATA_PFLAG_RESETTING;
2221afaa5c37STejun Heo 	spin_unlock_irqrestore(ap->lock, flags);
2222afaa5c37STejun Heo 
2223cf480626STejun Heo 	ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
22240a2c0f56STejun Heo 	ehc->last_reset = jiffies;
2225c6fd2807SJeff Garzik 
2226cdeab114STejun Heo 	ata_link_for_each_dev(dev, link) {
2227cdeab114STejun Heo 		/* If we issue an SRST then an ATA drive (not ATAPI)
2228cdeab114STejun Heo 		 * may change configuration and be in PIO0 timing. If
2229cdeab114STejun Heo 		 * we do a hard reset (or are coming from power on)
2230cdeab114STejun Heo 		 * this is true for ATA or ATAPI. Until we've set a
2231cdeab114STejun Heo 		 * suitable controller mode we should not touch the
2232cdeab114STejun Heo 		 * bus as we may be talking too fast.
2233cdeab114STejun Heo 		 */
2234cdeab114STejun Heo 		dev->pio_mode = XFER_PIO_0;
2235cdeab114STejun Heo 
2236cdeab114STejun Heo 		/* If the controller has a pio mode setup function
2237cdeab114STejun Heo 		 * then use it to set the chipset to rights. Don't
2238cdeab114STejun Heo 		 * touch the DMA setup as that will be dealt with when
2239cdeab114STejun Heo 		 * configuring devices.
2240cdeab114STejun Heo 		 */
2241cdeab114STejun Heo 		if (ap->ops->set_piomode)
2242cdeab114STejun Heo 			ap->ops->set_piomode(ap, dev);
2243cdeab114STejun Heo 	}
2244cdeab114STejun Heo 
2245cf480626STejun Heo 	/* prefer hardreset */
2246932648b0STejun Heo 	reset = NULL;
2247cf480626STejun Heo 	ehc->i.action &= ~ATA_EH_RESET;
2248cf480626STejun Heo 	if (hardreset) {
2249cf480626STejun Heo 		reset = hardreset;
2250cf480626STejun Heo 		ehc->i.action = ATA_EH_HARDRESET;
22514f7faa3fSTejun Heo 	} else if (softreset) {
2252cf480626STejun Heo 		reset = softreset;
2253cf480626STejun Heo 		ehc->i.action = ATA_EH_SOFTRESET;
2254cf480626STejun Heo 	}
2255c6fd2807SJeff Garzik 
2256c6fd2807SJeff Garzik 	if (prereset) {
2257341c2c95STejun Heo 		rc = prereset(link,
2258341c2c95STejun Heo 			      ata_deadline(jiffies, ATA_EH_PRERESET_TIMEOUT));
2259c6fd2807SJeff Garzik 		if (rc) {
2260c961922bSAlan Cox 			if (rc == -ENOENT) {
2261cc0680a5STejun Heo 				ata_link_printk(link, KERN_DEBUG,
22624aa9ab67STejun Heo 						"port disabled. ignoring.\n");
2263cf480626STejun Heo 				ehc->i.action &= ~ATA_EH_RESET;
22644aa9ab67STejun Heo 
2265936fd732STejun Heo 				ata_link_for_each_dev(dev, link)
2266f58229f8STejun Heo 					classes[dev->devno] = ATA_DEV_NONE;
22674aa9ab67STejun Heo 
22684aa9ab67STejun Heo 				rc = 0;
2269c961922bSAlan Cox 			} else
2270cc0680a5STejun Heo 				ata_link_printk(link, KERN_ERR,
2271c6fd2807SJeff Garzik 					"prereset failed (errno=%d)\n", rc);
2272fccb6ea5STejun Heo 			goto out;
2273c6fd2807SJeff Garzik 		}
2274c6fd2807SJeff Garzik 
2275932648b0STejun Heo 		/* prereset() might have cleared ATA_EH_RESET.  If so,
2276932648b0STejun Heo 		 * bang classes and return.
2277932648b0STejun Heo 		 */
2278932648b0STejun Heo 		if (reset && !(ehc->i.action & ATA_EH_RESET)) {
2279936fd732STejun Heo 			ata_link_for_each_dev(dev, link)
2280f58229f8STejun Heo 				classes[dev->devno] = ATA_DEV_NONE;
2281fccb6ea5STejun Heo 			rc = 0;
2282fccb6ea5STejun Heo 			goto out;
2283c6fd2807SJeff Garzik 		}
2284932648b0STejun Heo 	}
2285c6fd2807SJeff Garzik 
2286c6fd2807SJeff Garzik  retry:
2287932648b0STejun Heo 	/*
2288932648b0STejun Heo 	 * Perform reset
2289932648b0STejun Heo 	 */
22900a2c0f56STejun Heo 	ehc->last_reset = jiffies;
2291dc98c32cSTejun Heo 	if (ata_is_host_link(link))
2292dc98c32cSTejun Heo 		ata_eh_freeze_port(ap);
2293dc98c32cSTejun Heo 
2294341c2c95STejun Heo 	deadline = ata_deadline(jiffies, ata_eh_reset_timeouts[try++]);
229531daabdaSTejun Heo 
2296932648b0STejun Heo 	if (reset) {
2297c6fd2807SJeff Garzik 		if (verbose)
2298cc0680a5STejun Heo 			ata_link_printk(link, KERN_INFO, "%s resetting link\n",
2299c6fd2807SJeff Garzik 					reset == softreset ? "soft" : "hard");
2300c6fd2807SJeff Garzik 
2301c6fd2807SJeff Garzik 		/* mark that this EH session started with reset */
23020d64a233STejun Heo 		if (reset == hardreset)
23030d64a233STejun Heo 			ehc->i.flags |= ATA_EHI_DID_HARDRESET;
23040d64a233STejun Heo 		else
23050d64a233STejun Heo 			ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
2306c6fd2807SJeff Garzik 
2307cc0680a5STejun Heo 		rc = ata_do_reset(link, reset, classes, deadline);
2308c6fd2807SJeff Garzik 
2309c6fd2807SJeff Garzik 		if (reset == hardreset &&
2310ae791c05STejun Heo 		    ata_eh_followup_srst_needed(link, rc, classify, classes)) {
2311c6fd2807SJeff Garzik 			/* okay, let's do follow-up softreset */
2312c6fd2807SJeff Garzik 			reset = softreset;
2313c6fd2807SJeff Garzik 
2314c6fd2807SJeff Garzik 			if (!reset) {
2315cc0680a5STejun Heo 				ata_link_printk(link, KERN_ERR,
2316c6fd2807SJeff Garzik 						"follow-up softreset required "
2317c6fd2807SJeff Garzik 						"but no softreset avaliable\n");
2318fccb6ea5STejun Heo 				rc = -EINVAL;
231908cf69d0STejun Heo 				goto fail;
2320c6fd2807SJeff Garzik 			}
2321c6fd2807SJeff Garzik 
2322cf480626STejun Heo 			ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
2323cc0680a5STejun Heo 			rc = ata_do_reset(link, reset, classes, deadline);
2324c6fd2807SJeff Garzik 		}
2325c6fd2807SJeff Garzik 
2326416dc9edSTejun Heo 		/* -EAGAIN can happen if we skipped followup SRST */
2327416dc9edSTejun Heo 		if (rc && rc != -EAGAIN)
2328416dc9edSTejun Heo 			goto fail;
2329932648b0STejun Heo 	} else {
2330932648b0STejun Heo 		if (verbose)
2331932648b0STejun Heo 			ata_link_printk(link, KERN_INFO, "no reset method "
2332932648b0STejun Heo 					"available, skipping reset\n");
2333932648b0STejun Heo 		if (!(lflags & ATA_LFLAG_ASSUME_CLASS))
2334932648b0STejun Heo 			lflags |= ATA_LFLAG_ASSUME_ATA;
2335932648b0STejun Heo 	}
2336008a7896STejun Heo 
2337932648b0STejun Heo 	/*
2338932648b0STejun Heo 	 * Post-reset processing
2339932648b0STejun Heo 	 */
2340ae791c05STejun Heo 	ata_link_for_each_dev(dev, link) {
2341416dc9edSTejun Heo 		/* After the reset, the device state is PIO 0 and the
2342416dc9edSTejun Heo 		 * controller state is undefined.  Reset also wakes up
2343416dc9edSTejun Heo 		 * drives from sleeping mode.
2344c6fd2807SJeff Garzik 		 */
2345f58229f8STejun Heo 		dev->pio_mode = XFER_PIO_0;
2346054a5fbaSTejun Heo 		dev->flags &= ~ATA_DFLAG_SLEEPING;
2347c6fd2807SJeff Garzik 
2348ae791c05STejun Heo 		if (ata_link_offline(link))
2349ae791c05STejun Heo 			continue;
2350ae791c05STejun Heo 
23514ccd3329STejun Heo 		/* apply class override */
2352416dc9edSTejun Heo 		if (lflags & ATA_LFLAG_ASSUME_ATA)
2353ae791c05STejun Heo 			classes[dev->devno] = ATA_DEV_ATA;
2354416dc9edSTejun Heo 		else if (lflags & ATA_LFLAG_ASSUME_SEMB)
2355ae791c05STejun Heo 			classes[dev->devno] = ATA_DEV_SEMB_UNSUP; /* not yet */
2356ae791c05STejun Heo 	}
2357ae791c05STejun Heo 
2358008a7896STejun Heo 	/* record current link speed */
2359936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2360936fd732STejun Heo 		link->sata_spd = (sstatus >> 4) & 0xf;
2361008a7896STejun Heo 
2362dc98c32cSTejun Heo 	/* thaw the port */
2363dc98c32cSTejun Heo 	if (ata_is_host_link(link))
2364dc98c32cSTejun Heo 		ata_eh_thaw_port(ap);
2365dc98c32cSTejun Heo 
2366f046519fSTejun Heo 	/* postreset() should clear hardware SError.  Although SError
2367f046519fSTejun Heo 	 * is cleared during link resume, clearing SError here is
2368f046519fSTejun Heo 	 * necessary as some PHYs raise hotplug events after SRST.
2369f046519fSTejun Heo 	 * This introduces race condition where hotplug occurs between
2370f046519fSTejun Heo 	 * reset and here.  This race is mediated by cross checking
2371f046519fSTejun Heo 	 * link onlineness and classification result later.
2372f046519fSTejun Heo 	 */
2373c6fd2807SJeff Garzik 	if (postreset)
2374cc0680a5STejun Heo 		postreset(link, classes);
2375c6fd2807SJeff Garzik 
2376f046519fSTejun Heo 	/* clear cached SError */
2377f046519fSTejun Heo 	spin_lock_irqsave(link->ap->lock, flags);
2378f046519fSTejun Heo 	link->eh_info.serror = 0;
2379f046519fSTejun Heo 	spin_unlock_irqrestore(link->ap->lock, flags);
2380f046519fSTejun Heo 
2381f046519fSTejun Heo 	/* Make sure onlineness and classification result correspond.
2382f046519fSTejun Heo 	 * Hotplug could have happened during reset and some
2383f046519fSTejun Heo 	 * controllers fail to wait while a drive is spinning up after
2384f046519fSTejun Heo 	 * being hotplugged causing misdetection.  By cross checking
2385f046519fSTejun Heo 	 * link onlineness and classification result, those conditions
2386f046519fSTejun Heo 	 * can be reliably detected and retried.
2387f046519fSTejun Heo 	 */
2388f046519fSTejun Heo 	nr_known = 0;
2389f046519fSTejun Heo 	ata_link_for_each_dev(dev, link) {
2390f046519fSTejun Heo 		/* convert all ATA_DEV_UNKNOWN to ATA_DEV_NONE */
2391f046519fSTejun Heo 		if (classes[dev->devno] == ATA_DEV_UNKNOWN)
2392f046519fSTejun Heo 			classes[dev->devno] = ATA_DEV_NONE;
2393f046519fSTejun Heo 		else
2394f046519fSTejun Heo 			nr_known++;
2395f046519fSTejun Heo 	}
2396f046519fSTejun Heo 
2397f046519fSTejun Heo 	if (classify && !nr_known && ata_link_online(link)) {
2398f046519fSTejun Heo 		if (try < max_tries) {
2399f046519fSTejun Heo 			ata_link_printk(link, KERN_WARNING, "link online but "
2400f046519fSTejun Heo 				       "device misclassified, retrying\n");
2401f046519fSTejun Heo 			rc = -EAGAIN;
2402f046519fSTejun Heo 			goto fail;
2403f046519fSTejun Heo 		}
2404f046519fSTejun Heo 		ata_link_printk(link, KERN_WARNING,
2405f046519fSTejun Heo 			       "link online but device misclassified, "
2406f046519fSTejun Heo 			       "device detection might fail\n");
2407f046519fSTejun Heo 	}
2408f046519fSTejun Heo 
2409c6fd2807SJeff Garzik 	/* reset successful, schedule revalidation */
2410cf480626STejun Heo 	ata_eh_done(link, NULL, ATA_EH_RESET);
24110a2c0f56STejun Heo 	ehc->last_reset = jiffies;
2412c6fd2807SJeff Garzik 	ehc->i.action |= ATA_EH_REVALIDATE;
2413416dc9edSTejun Heo 
2414416dc9edSTejun Heo 	rc = 0;
2415fccb6ea5STejun Heo  out:
2416fccb6ea5STejun Heo 	/* clear hotplug flag */
2417fccb6ea5STejun Heo 	ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2418afaa5c37STejun Heo 
2419afaa5c37STejun Heo 	spin_lock_irqsave(ap->lock, flags);
2420afaa5c37STejun Heo 	ap->pflags &= ~ATA_PFLAG_RESETTING;
2421afaa5c37STejun Heo 	spin_unlock_irqrestore(ap->lock, flags);
2422afaa5c37STejun Heo 
2423c6fd2807SJeff Garzik 	return rc;
2424416dc9edSTejun Heo 
2425416dc9edSTejun Heo  fail:
24265958e302STejun Heo 	/* if SCR isn't accessible on a fan-out port, PMP needs to be reset */
24275958e302STejun Heo 	if (!ata_is_host_link(link) &&
24285958e302STejun Heo 	    sata_scr_read(link, SCR_STATUS, &sstatus))
24295958e302STejun Heo 		rc = -ERESTART;
24305958e302STejun Heo 
2431416dc9edSTejun Heo 	if (rc == -ERESTART || try >= max_tries)
2432416dc9edSTejun Heo 		goto out;
2433416dc9edSTejun Heo 
2434416dc9edSTejun Heo 	now = jiffies;
2435416dc9edSTejun Heo 	if (time_before(now, deadline)) {
2436416dc9edSTejun Heo 		unsigned long delta = deadline - now;
2437416dc9edSTejun Heo 
24380a2c0f56STejun Heo 		ata_link_printk(link, KERN_WARNING,
24390a2c0f56STejun Heo 			"reset failed (errno=%d), retrying in %u secs\n",
24400a2c0f56STejun Heo 			rc, DIV_ROUND_UP(jiffies_to_msecs(delta), 1000));
2441416dc9edSTejun Heo 
2442416dc9edSTejun Heo 		while (delta)
2443416dc9edSTejun Heo 			delta = schedule_timeout_uninterruptible(delta);
2444416dc9edSTejun Heo 	}
2445416dc9edSTejun Heo 
2446416dc9edSTejun Heo 	if (rc == -EPIPE || try == max_tries - 1)
2447416dc9edSTejun Heo 		sata_down_spd_limit(link);
2448416dc9edSTejun Heo 	if (hardreset)
2449416dc9edSTejun Heo 		reset = hardreset;
2450416dc9edSTejun Heo 	goto retry;
2451c6fd2807SJeff Garzik }
2452c6fd2807SJeff Garzik 
24530260731fSTejun Heo static int ata_eh_revalidate_and_attach(struct ata_link *link,
2454c6fd2807SJeff Garzik 					struct ata_device **r_failed_dev)
2455c6fd2807SJeff Garzik {
24560260731fSTejun Heo 	struct ata_port *ap = link->ap;
24570260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
2458c6fd2807SJeff Garzik 	struct ata_device *dev;
24598c3c52a8STejun Heo 	unsigned int new_mask = 0;
2460c6fd2807SJeff Garzik 	unsigned long flags;
2461f58229f8STejun Heo 	int rc = 0;
2462c6fd2807SJeff Garzik 
2463c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
2464c6fd2807SJeff Garzik 
24658c3c52a8STejun Heo 	/* For PATA drive side cable detection to work, IDENTIFY must
24668c3c52a8STejun Heo 	 * be done backwards such that PDIAG- is released by the slave
24678c3c52a8STejun Heo 	 * device before the master device is identified.
24688c3c52a8STejun Heo 	 */
24690260731fSTejun Heo 	ata_link_for_each_dev_reverse(dev, link) {
2470f58229f8STejun Heo 		unsigned int action = ata_eh_dev_action(dev);
2471f58229f8STejun Heo 		unsigned int readid_flags = 0;
2472c6fd2807SJeff Garzik 
2473bff04647STejun Heo 		if (ehc->i.flags & ATA_EHI_DID_RESET)
2474bff04647STejun Heo 			readid_flags |= ATA_READID_POSTRESET;
2475bff04647STejun Heo 
24769666f400STejun Heo 		if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
2477633273a3STejun Heo 			WARN_ON(dev->class == ATA_DEV_PMP);
2478633273a3STejun Heo 
24790260731fSTejun Heo 			if (ata_link_offline(link)) {
2480c6fd2807SJeff Garzik 				rc = -EIO;
24818c3c52a8STejun Heo 				goto err;
2482c6fd2807SJeff Garzik 			}
2483c6fd2807SJeff Garzik 
24840260731fSTejun Heo 			ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
2485422c9daaSTejun Heo 			rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
2486422c9daaSTejun Heo 						readid_flags);
2487c6fd2807SJeff Garzik 			if (rc)
24888c3c52a8STejun Heo 				goto err;
2489c6fd2807SJeff Garzik 
24900260731fSTejun Heo 			ata_eh_done(link, dev, ATA_EH_REVALIDATE);
2491c6fd2807SJeff Garzik 
2492baa1e78aSTejun Heo 			/* Configuration may have changed, reconfigure
2493baa1e78aSTejun Heo 			 * transfer mode.
2494baa1e78aSTejun Heo 			 */
2495baa1e78aSTejun Heo 			ehc->i.flags |= ATA_EHI_SETMODE;
2496baa1e78aSTejun Heo 
2497c6fd2807SJeff Garzik 			/* schedule the scsi_rescan_device() here */
2498c6fd2807SJeff Garzik 			queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
2499c6fd2807SJeff Garzik 		} else if (dev->class == ATA_DEV_UNKNOWN &&
2500c6fd2807SJeff Garzik 			   ehc->tries[dev->devno] &&
2501c6fd2807SJeff Garzik 			   ata_class_enabled(ehc->classes[dev->devno])) {
2502c6fd2807SJeff Garzik 			dev->class = ehc->classes[dev->devno];
2503c6fd2807SJeff Garzik 
2504633273a3STejun Heo 			if (dev->class == ATA_DEV_PMP)
2505633273a3STejun Heo 				rc = sata_pmp_attach(dev);
2506633273a3STejun Heo 			else
2507633273a3STejun Heo 				rc = ata_dev_read_id(dev, &dev->class,
2508633273a3STejun Heo 						     readid_flags, dev->id);
25098c3c52a8STejun Heo 			switch (rc) {
25108c3c52a8STejun Heo 			case 0:
2511f58229f8STejun Heo 				new_mask |= 1 << dev->devno;
25128c3c52a8STejun Heo 				break;
25138c3c52a8STejun Heo 			case -ENOENT:
251455a8e2c8STejun Heo 				/* IDENTIFY was issued to non-existent
251555a8e2c8STejun Heo 				 * device.  No need to reset.  Just
251655a8e2c8STejun Heo 				 * thaw and kill the device.
251755a8e2c8STejun Heo 				 */
251855a8e2c8STejun Heo 				ata_eh_thaw_port(ap);
251955a8e2c8STejun Heo 				dev->class = ATA_DEV_UNKNOWN;
2520c6fd2807SJeff Garzik 				break;
25218c3c52a8STejun Heo 			default:
25228c3c52a8STejun Heo 				dev->class = ATA_DEV_UNKNOWN;
25238c3c52a8STejun Heo 				goto err;
25248c3c52a8STejun Heo 			}
25258c3c52a8STejun Heo 		}
2526c6fd2807SJeff Garzik 	}
2527c6fd2807SJeff Garzik 
2528c1c4e8d5STejun Heo 	/* PDIAG- should have been released, ask cable type if post-reset */
252933267325STejun Heo 	if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) {
253033267325STejun Heo 		if (ap->ops->cable_detect)
2531c1c4e8d5STejun Heo 			ap->cbl = ap->ops->cable_detect(ap);
253233267325STejun Heo 		ata_force_cbl(ap);
253333267325STejun Heo 	}
2534c1c4e8d5STejun Heo 
25358c3c52a8STejun Heo 	/* Configure new devices forward such that user doesn't see
25368c3c52a8STejun Heo 	 * device detection messages backwards.
25378c3c52a8STejun Heo 	 */
25380260731fSTejun Heo 	ata_link_for_each_dev(dev, link) {
2539633273a3STejun Heo 		if (!(new_mask & (1 << dev->devno)) ||
2540633273a3STejun Heo 		    dev->class == ATA_DEV_PMP)
25418c3c52a8STejun Heo 			continue;
25428c3c52a8STejun Heo 
25438c3c52a8STejun Heo 		ehc->i.flags |= ATA_EHI_PRINTINFO;
25448c3c52a8STejun Heo 		rc = ata_dev_configure(dev);
25458c3c52a8STejun Heo 		ehc->i.flags &= ~ATA_EHI_PRINTINFO;
25468c3c52a8STejun Heo 		if (rc)
25478c3c52a8STejun Heo 			goto err;
25488c3c52a8STejun Heo 
2549c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
2550c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
2551c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
2552baa1e78aSTejun Heo 
255355a8e2c8STejun Heo 		/* new device discovered, configure xfermode */
2554baa1e78aSTejun Heo 		ehc->i.flags |= ATA_EHI_SETMODE;
2555c6fd2807SJeff Garzik 	}
2556c6fd2807SJeff Garzik 
25578c3c52a8STejun Heo 	return 0;
25588c3c52a8STejun Heo 
25598c3c52a8STejun Heo  err:
2560c6fd2807SJeff Garzik 	*r_failed_dev = dev;
25618c3c52a8STejun Heo 	DPRINTK("EXIT rc=%d\n", rc);
2562c6fd2807SJeff Garzik 	return rc;
2563c6fd2807SJeff Garzik }
2564c6fd2807SJeff Garzik 
25656f1d1e3aSTejun Heo /**
25666f1d1e3aSTejun Heo  *	ata_set_mode - Program timings and issue SET FEATURES - XFER
25676f1d1e3aSTejun Heo  *	@link: link on which timings will be programmed
25686f1d1e3aSTejun Heo  *	@r_failed_dev: out paramter for failed device
25696f1d1e3aSTejun Heo  *
25706f1d1e3aSTejun Heo  *	Set ATA device disk transfer mode (PIO3, UDMA6, etc.).  If
25716f1d1e3aSTejun Heo  *	ata_set_mode() fails, pointer to the failing device is
25726f1d1e3aSTejun Heo  *	returned in @r_failed_dev.
25736f1d1e3aSTejun Heo  *
25746f1d1e3aSTejun Heo  *	LOCKING:
25756f1d1e3aSTejun Heo  *	PCI/etc. bus probe sem.
25766f1d1e3aSTejun Heo  *
25776f1d1e3aSTejun Heo  *	RETURNS:
25786f1d1e3aSTejun Heo  *	0 on success, negative errno otherwise
25796f1d1e3aSTejun Heo  */
25806f1d1e3aSTejun Heo int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
25816f1d1e3aSTejun Heo {
25826f1d1e3aSTejun Heo 	struct ata_port *ap = link->ap;
258300115e0fSTejun Heo 	struct ata_device *dev;
258400115e0fSTejun Heo 	int rc;
25856f1d1e3aSTejun Heo 
258676326ac1STejun Heo 	/* if data transfer is verified, clear DUBIOUS_XFER on ering top */
258776326ac1STejun Heo 	ata_link_for_each_dev(dev, link) {
258876326ac1STejun Heo 		if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
258976326ac1STejun Heo 			struct ata_ering_entry *ent;
259076326ac1STejun Heo 
259176326ac1STejun Heo 			ent = ata_ering_top(&dev->ering);
259276326ac1STejun Heo 			if (ent)
259376326ac1STejun Heo 				ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER;
259476326ac1STejun Heo 		}
259576326ac1STejun Heo 	}
259676326ac1STejun Heo 
25976f1d1e3aSTejun Heo 	/* has private set_mode? */
25986f1d1e3aSTejun Heo 	if (ap->ops->set_mode)
259900115e0fSTejun Heo 		rc = ap->ops->set_mode(link, r_failed_dev);
260000115e0fSTejun Heo 	else
260100115e0fSTejun Heo 		rc = ata_do_set_mode(link, r_failed_dev);
260200115e0fSTejun Heo 
260300115e0fSTejun Heo 	/* if transfer mode has changed, set DUBIOUS_XFER on device */
260400115e0fSTejun Heo 	ata_link_for_each_dev(dev, link) {
260500115e0fSTejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
260600115e0fSTejun Heo 		u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
260700115e0fSTejun Heo 		u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
260800115e0fSTejun Heo 
260900115e0fSTejun Heo 		if (dev->xfer_mode != saved_xfer_mode ||
261000115e0fSTejun Heo 		    ata_ncq_enabled(dev) != saved_ncq)
261100115e0fSTejun Heo 			dev->flags |= ATA_DFLAG_DUBIOUS_XFER;
261200115e0fSTejun Heo 	}
261300115e0fSTejun Heo 
261400115e0fSTejun Heo 	return rc;
26156f1d1e3aSTejun Heo }
26166f1d1e3aSTejun Heo 
26170260731fSTejun Heo static int ata_link_nr_enabled(struct ata_link *link)
2618c6fd2807SJeff Garzik {
2619f58229f8STejun Heo 	struct ata_device *dev;
2620f58229f8STejun Heo 	int cnt = 0;
2621c6fd2807SJeff Garzik 
26220260731fSTejun Heo 	ata_link_for_each_dev(dev, link)
2623f58229f8STejun Heo 		if (ata_dev_enabled(dev))
2624c6fd2807SJeff Garzik 			cnt++;
2625c6fd2807SJeff Garzik 	return cnt;
2626c6fd2807SJeff Garzik }
2627c6fd2807SJeff Garzik 
26280260731fSTejun Heo static int ata_link_nr_vacant(struct ata_link *link)
2629c6fd2807SJeff Garzik {
2630f58229f8STejun Heo 	struct ata_device *dev;
2631f58229f8STejun Heo 	int cnt = 0;
2632c6fd2807SJeff Garzik 
26330260731fSTejun Heo 	ata_link_for_each_dev(dev, link)
2634f58229f8STejun Heo 		if (dev->class == ATA_DEV_UNKNOWN)
2635c6fd2807SJeff Garzik 			cnt++;
2636c6fd2807SJeff Garzik 	return cnt;
2637c6fd2807SJeff Garzik }
2638c6fd2807SJeff Garzik 
26390260731fSTejun Heo static int ata_eh_skip_recovery(struct ata_link *link)
2640c6fd2807SJeff Garzik {
2641672b2d65STejun Heo 	struct ata_port *ap = link->ap;
26420260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
2643f58229f8STejun Heo 	struct ata_device *dev;
2644c6fd2807SJeff Garzik 
2645f9df58cbSTejun Heo 	/* skip disabled links */
2646f9df58cbSTejun Heo 	if (link->flags & ATA_LFLAG_DISABLED)
2647f9df58cbSTejun Heo 		return 1;
2648f9df58cbSTejun Heo 
2649672b2d65STejun Heo 	/* thaw frozen port and recover failed devices */
2650672b2d65STejun Heo 	if ((ap->pflags & ATA_PFLAG_FROZEN) || ata_link_nr_enabled(link))
2651672b2d65STejun Heo 		return 0;
2652672b2d65STejun Heo 
2653672b2d65STejun Heo 	/* reset at least once if reset is requested */
2654672b2d65STejun Heo 	if ((ehc->i.action & ATA_EH_RESET) &&
2655672b2d65STejun Heo 	    !(ehc->i.flags & ATA_EHI_DID_RESET))
2656c6fd2807SJeff Garzik 		return 0;
2657c6fd2807SJeff Garzik 
2658c6fd2807SJeff Garzik 	/* skip if class codes for all vacant slots are ATA_DEV_NONE */
26590260731fSTejun Heo 	ata_link_for_each_dev(dev, link) {
2660c6fd2807SJeff Garzik 		if (dev->class == ATA_DEV_UNKNOWN &&
2661c6fd2807SJeff Garzik 		    ehc->classes[dev->devno] != ATA_DEV_NONE)
2662c6fd2807SJeff Garzik 			return 0;
2663c6fd2807SJeff Garzik 	}
2664c6fd2807SJeff Garzik 
2665c6fd2807SJeff Garzik 	return 1;
2666c6fd2807SJeff Garzik }
2667c6fd2807SJeff Garzik 
266802c05a27STejun Heo static int ata_eh_schedule_probe(struct ata_device *dev)
266902c05a27STejun Heo {
267002c05a27STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
267102c05a27STejun Heo 
267202c05a27STejun Heo 	if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
267302c05a27STejun Heo 	    (ehc->did_probe_mask & (1 << dev->devno)))
267402c05a27STejun Heo 		return 0;
267502c05a27STejun Heo 
267602c05a27STejun Heo 	ata_eh_detach_dev(dev);
267702c05a27STejun Heo 	ata_dev_init(dev);
267802c05a27STejun Heo 	ehc->did_probe_mask |= (1 << dev->devno);
2679cf480626STejun Heo 	ehc->i.action |= ATA_EH_RESET;
268000115e0fSTejun Heo 	ehc->saved_xfer_mode[dev->devno] = 0;
268100115e0fSTejun Heo 	ehc->saved_ncq_enabled &= ~(1 << dev->devno);
268202c05a27STejun Heo 
268302c05a27STejun Heo 	return 1;
268402c05a27STejun Heo }
268502c05a27STejun Heo 
26869b1e2658STejun Heo static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
2687fee7ca72STejun Heo {
26889af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
2689fee7ca72STejun Heo 
2690fee7ca72STejun Heo 	ehc->tries[dev->devno]--;
2691fee7ca72STejun Heo 
2692fee7ca72STejun Heo 	switch (err) {
2693fee7ca72STejun Heo 	case -ENODEV:
2694fee7ca72STejun Heo 		/* device missing or wrong IDENTIFY data, schedule probing */
2695fee7ca72STejun Heo 		ehc->i.probe_mask |= (1 << dev->devno);
2696fee7ca72STejun Heo 	case -EINVAL:
2697fee7ca72STejun Heo 		/* give it just one more chance */
2698fee7ca72STejun Heo 		ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
2699fee7ca72STejun Heo 	case -EIO:
27004fb4615bSTejun Heo 		if (ehc->tries[dev->devno] == 1 && dev->pio_mode > XFER_PIO_0) {
2701fee7ca72STejun Heo 			/* This is the last chance, better to slow
2702fee7ca72STejun Heo 			 * down than lose it.
2703fee7ca72STejun Heo 			 */
2704936fd732STejun Heo 			sata_down_spd_limit(dev->link);
2705fee7ca72STejun Heo 			ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2706fee7ca72STejun Heo 		}
2707fee7ca72STejun Heo 	}
2708fee7ca72STejun Heo 
2709fee7ca72STejun Heo 	if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2710fee7ca72STejun Heo 		/* disable device if it has used up all its chances */
2711fee7ca72STejun Heo 		ata_dev_disable(dev);
2712fee7ca72STejun Heo 
2713fee7ca72STejun Heo 		/* detach if offline */
2714936fd732STejun Heo 		if (ata_link_offline(dev->link))
2715fee7ca72STejun Heo 			ata_eh_detach_dev(dev);
2716fee7ca72STejun Heo 
271702c05a27STejun Heo 		/* schedule probe if necessary */
271887fbc5a0STejun Heo 		if (ata_eh_schedule_probe(dev)) {
2719fee7ca72STejun Heo 			ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
272087fbc5a0STejun Heo 			memset(ehc->cmd_timeout_idx[dev->devno], 0,
272187fbc5a0STejun Heo 			       sizeof(ehc->cmd_timeout_idx[dev->devno]));
272287fbc5a0STejun Heo 		}
27239b1e2658STejun Heo 
27249b1e2658STejun Heo 		return 1;
2725fee7ca72STejun Heo 	} else {
2726cf480626STejun Heo 		ehc->i.action |= ATA_EH_RESET;
27279b1e2658STejun Heo 		return 0;
2728fee7ca72STejun Heo 	}
2729fee7ca72STejun Heo }
2730fee7ca72STejun Heo 
2731c6fd2807SJeff Garzik /**
2732c6fd2807SJeff Garzik  *	ata_eh_recover - recover host port after error
2733c6fd2807SJeff Garzik  *	@ap: host port to recover
2734c6fd2807SJeff Garzik  *	@prereset: prereset method (can be NULL)
2735c6fd2807SJeff Garzik  *	@softreset: softreset method (can be NULL)
2736c6fd2807SJeff Garzik  *	@hardreset: hardreset method (can be NULL)
2737c6fd2807SJeff Garzik  *	@postreset: postreset method (can be NULL)
27389b1e2658STejun Heo  *	@r_failed_link: out parameter for failed link
2739c6fd2807SJeff Garzik  *
2740c6fd2807SJeff Garzik  *	This is the alpha and omega, eum and yang, heart and soul of
2741c6fd2807SJeff Garzik  *	libata exception handling.  On entry, actions required to
27429b1e2658STejun Heo  *	recover each link and hotplug requests are recorded in the
27439b1e2658STejun Heo  *	link's eh_context.  This function executes all the operations
27449b1e2658STejun Heo  *	with appropriate retrials and fallbacks to resurrect failed
2745c6fd2807SJeff Garzik  *	devices, detach goners and greet newcomers.
2746c6fd2807SJeff Garzik  *
2747c6fd2807SJeff Garzik  *	LOCKING:
2748c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
2749c6fd2807SJeff Garzik  *
2750c6fd2807SJeff Garzik  *	RETURNS:
2751c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
2752c6fd2807SJeff Garzik  */
2753fb7fd614STejun Heo int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
2754c6fd2807SJeff Garzik 		   ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
27559b1e2658STejun Heo 		   ata_postreset_fn_t postreset,
27569b1e2658STejun Heo 		   struct ata_link **r_failed_link)
2757c6fd2807SJeff Garzik {
27589b1e2658STejun Heo 	struct ata_link *link;
2759c6fd2807SJeff Garzik 	struct ata_device *dev;
27600a2c0f56STejun Heo 	int nr_failed_devs;
2761dc98c32cSTejun Heo 	int rc;
2762f9df58cbSTejun Heo 	unsigned long flags;
2763c6fd2807SJeff Garzik 
2764c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
2765c6fd2807SJeff Garzik 
2766c6fd2807SJeff Garzik 	/* prep for recovery */
27679b1e2658STejun Heo 	ata_port_for_each_link(link, ap) {
27689b1e2658STejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
27699b1e2658STejun Heo 
2770f9df58cbSTejun Heo 		/* re-enable link? */
2771f9df58cbSTejun Heo 		if (ehc->i.action & ATA_EH_ENABLE_LINK) {
2772f9df58cbSTejun Heo 			ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
2773f9df58cbSTejun Heo 			spin_lock_irqsave(ap->lock, flags);
2774f9df58cbSTejun Heo 			link->flags &= ~ATA_LFLAG_DISABLED;
2775f9df58cbSTejun Heo 			spin_unlock_irqrestore(ap->lock, flags);
2776f9df58cbSTejun Heo 			ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
2777f9df58cbSTejun Heo 		}
2778f9df58cbSTejun Heo 
27790260731fSTejun Heo 		ata_link_for_each_dev(dev, link) {
2780fd995f70STejun Heo 			if (link->flags & ATA_LFLAG_NO_RETRY)
2781fd995f70STejun Heo 				ehc->tries[dev->devno] = 1;
2782fd995f70STejun Heo 			else
2783c6fd2807SJeff Garzik 				ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2784c6fd2807SJeff Garzik 
278579a55b72STejun Heo 			/* collect port action mask recorded in dev actions */
27869b1e2658STejun Heo 			ehc->i.action |= ehc->i.dev_action[dev->devno] &
27879b1e2658STejun Heo 					 ~ATA_EH_PERDEV_MASK;
2788f58229f8STejun Heo 			ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
278979a55b72STejun Heo 
2790c6fd2807SJeff Garzik 			/* process hotplug request */
2791c6fd2807SJeff Garzik 			if (dev->flags & ATA_DFLAG_DETACH)
2792c6fd2807SJeff Garzik 				ata_eh_detach_dev(dev);
2793c6fd2807SJeff Garzik 
279402c05a27STejun Heo 			/* schedule probe if necessary */
279502c05a27STejun Heo 			if (!ata_dev_enabled(dev))
279602c05a27STejun Heo 				ata_eh_schedule_probe(dev);
2797c6fd2807SJeff Garzik 		}
27989b1e2658STejun Heo 	}
2799c6fd2807SJeff Garzik 
2800c6fd2807SJeff Garzik  retry:
2801c6fd2807SJeff Garzik 	rc = 0;
28029b1e2658STejun Heo 	nr_failed_devs = 0;
2803c6fd2807SJeff Garzik 
2804c6fd2807SJeff Garzik 	/* if UNLOADING, finish immediately */
2805c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_UNLOADING)
2806c6fd2807SJeff Garzik 		goto out;
2807c6fd2807SJeff Garzik 
28089b1e2658STejun Heo 	/* prep for EH */
28099b1e2658STejun Heo 	ata_port_for_each_link(link, ap) {
28109b1e2658STejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
28119b1e2658STejun Heo 
2812c6fd2807SJeff Garzik 		/* skip EH if possible. */
28130260731fSTejun Heo 		if (ata_eh_skip_recovery(link))
2814c6fd2807SJeff Garzik 			ehc->i.action = 0;
2815c6fd2807SJeff Garzik 
28160260731fSTejun Heo 		ata_link_for_each_dev(dev, link)
2817f58229f8STejun Heo 			ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
28189b1e2658STejun Heo 	}
2819c6fd2807SJeff Garzik 
2820c6fd2807SJeff Garzik 	/* reset */
28219b1e2658STejun Heo 	ata_port_for_each_link(link, ap) {
28229b1e2658STejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
28239b1e2658STejun Heo 
2824cf480626STejun Heo 		if (!(ehc->i.action & ATA_EH_RESET))
28259b1e2658STejun Heo 			continue;
28269b1e2658STejun Heo 
28279b1e2658STejun Heo 		rc = ata_eh_reset(link, ata_link_nr_vacant(link),
2828dc98c32cSTejun Heo 				  prereset, softreset, hardreset, postreset);
2829c6fd2807SJeff Garzik 		if (rc) {
28300260731fSTejun Heo 			ata_link_printk(link, KERN_ERR,
2831c6fd2807SJeff Garzik 					"reset failed, giving up\n");
2832c6fd2807SJeff Garzik 			goto out;
2833c6fd2807SJeff Garzik 		}
28349b1e2658STejun Heo 	}
2835c6fd2807SJeff Garzik 
28369b1e2658STejun Heo 	/* the rest */
28379b1e2658STejun Heo 	ata_port_for_each_link(link, ap) {
28389b1e2658STejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
28399b1e2658STejun Heo 
2840c6fd2807SJeff Garzik 		/* revalidate existing devices and attach new ones */
28410260731fSTejun Heo 		rc = ata_eh_revalidate_and_attach(link, &dev);
2842c6fd2807SJeff Garzik 		if (rc)
2843c6fd2807SJeff Garzik 			goto dev_fail;
2844c6fd2807SJeff Garzik 
2845633273a3STejun Heo 		/* if PMP got attached, return, pmp EH will take care of it */
2846633273a3STejun Heo 		if (link->device->class == ATA_DEV_PMP) {
2847633273a3STejun Heo 			ehc->i.action = 0;
2848633273a3STejun Heo 			return 0;
2849633273a3STejun Heo 		}
2850633273a3STejun Heo 
2851baa1e78aSTejun Heo 		/* configure transfer mode if necessary */
2852baa1e78aSTejun Heo 		if (ehc->i.flags & ATA_EHI_SETMODE) {
28530260731fSTejun Heo 			rc = ata_set_mode(link, &dev);
28544ae72a1eSTejun Heo 			if (rc)
2855c6fd2807SJeff Garzik 				goto dev_fail;
2856baa1e78aSTejun Heo 			ehc->i.flags &= ~ATA_EHI_SETMODE;
2857c6fd2807SJeff Garzik 		}
2858c6fd2807SJeff Garzik 
28593ec25ebdSTejun Heo 		if (ehc->i.action & ATA_EH_LPM)
2860ca77329fSKristen Carlson Accardi 			ata_link_for_each_dev(dev, link)
2861ca77329fSKristen Carlson Accardi 				ata_dev_enable_pm(dev, ap->pm_policy);
2862ca77329fSKristen Carlson Accardi 
28639b1e2658STejun Heo 		/* this link is okay now */
28649b1e2658STejun Heo 		ehc->i.flags = 0;
28659b1e2658STejun Heo 		continue;
2866c6fd2807SJeff Garzik 
2867c6fd2807SJeff Garzik dev_fail:
28689b1e2658STejun Heo 		nr_failed_devs++;
28690a2c0f56STejun Heo 		ata_eh_handle_dev_fail(dev, rc);
2870c6fd2807SJeff Garzik 
2871b06ce3e5STejun Heo 		if (ap->pflags & ATA_PFLAG_FROZEN) {
2872b06ce3e5STejun Heo 			/* PMP reset requires working host port.
2873b06ce3e5STejun Heo 			 * Can't retry if it's frozen.
2874b06ce3e5STejun Heo 			 */
2875071f44b1STejun Heo 			if (sata_pmp_attached(ap))
2876b06ce3e5STejun Heo 				goto out;
28779b1e2658STejun Heo 			break;
28789b1e2658STejun Heo 		}
2879b06ce3e5STejun Heo 	}
28809b1e2658STejun Heo 
28810a2c0f56STejun Heo 	if (nr_failed_devs)
2882c6fd2807SJeff Garzik 		goto retry;
2883c6fd2807SJeff Garzik 
2884c6fd2807SJeff Garzik  out:
28859b1e2658STejun Heo 	if (rc && r_failed_link)
28869b1e2658STejun Heo 		*r_failed_link = link;
2887c6fd2807SJeff Garzik 
2888c6fd2807SJeff Garzik 	DPRINTK("EXIT, rc=%d\n", rc);
2889c6fd2807SJeff Garzik 	return rc;
2890c6fd2807SJeff Garzik }
2891c6fd2807SJeff Garzik 
2892c6fd2807SJeff Garzik /**
2893c6fd2807SJeff Garzik  *	ata_eh_finish - finish up EH
2894c6fd2807SJeff Garzik  *	@ap: host port to finish EH for
2895c6fd2807SJeff Garzik  *
2896c6fd2807SJeff Garzik  *	Recovery is complete.  Clean up EH states and retry or finish
2897c6fd2807SJeff Garzik  *	failed qcs.
2898c6fd2807SJeff Garzik  *
2899c6fd2807SJeff Garzik  *	LOCKING:
2900c6fd2807SJeff Garzik  *	None.
2901c6fd2807SJeff Garzik  */
2902fb7fd614STejun Heo void ata_eh_finish(struct ata_port *ap)
2903c6fd2807SJeff Garzik {
2904c6fd2807SJeff Garzik 	int tag;
2905c6fd2807SJeff Garzik 
2906c6fd2807SJeff Garzik 	/* retry or finish qcs */
2907c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2908c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2909c6fd2807SJeff Garzik 
2910c6fd2807SJeff Garzik 		if (!(qc->flags & ATA_QCFLAG_FAILED))
2911c6fd2807SJeff Garzik 			continue;
2912c6fd2807SJeff Garzik 
2913c6fd2807SJeff Garzik 		if (qc->err_mask) {
2914c6fd2807SJeff Garzik 			/* FIXME: Once EH migration is complete,
2915c6fd2807SJeff Garzik 			 * generate sense data in this function,
2916c6fd2807SJeff Garzik 			 * considering both err_mask and tf.
2917c6fd2807SJeff Garzik 			 */
291803faab78STejun Heo 			if (qc->flags & ATA_QCFLAG_RETRY)
2919c6fd2807SJeff Garzik 				ata_eh_qc_retry(qc);
292003faab78STejun Heo 			else
292103faab78STejun Heo 				ata_eh_qc_complete(qc);
2922c6fd2807SJeff Garzik 		} else {
2923c6fd2807SJeff Garzik 			if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2924c6fd2807SJeff Garzik 				ata_eh_qc_complete(qc);
2925c6fd2807SJeff Garzik 			} else {
2926c6fd2807SJeff Garzik 				/* feed zero TF to sense generation */
2927c6fd2807SJeff Garzik 				memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2928c6fd2807SJeff Garzik 				ata_eh_qc_retry(qc);
2929c6fd2807SJeff Garzik 			}
2930c6fd2807SJeff Garzik 		}
2931c6fd2807SJeff Garzik 	}
2932da917d69STejun Heo 
2933da917d69STejun Heo 	/* make sure nr_active_links is zero after EH */
2934da917d69STejun Heo 	WARN_ON(ap->nr_active_links);
2935da917d69STejun Heo 	ap->nr_active_links = 0;
2936c6fd2807SJeff Garzik }
2937c6fd2807SJeff Garzik 
2938c6fd2807SJeff Garzik /**
2939c6fd2807SJeff Garzik  *	ata_do_eh - do standard error handling
2940c6fd2807SJeff Garzik  *	@ap: host port to handle error for
2941a1efdabaSTejun Heo  *
2942c6fd2807SJeff Garzik  *	@prereset: prereset method (can be NULL)
2943c6fd2807SJeff Garzik  *	@softreset: softreset method (can be NULL)
2944c6fd2807SJeff Garzik  *	@hardreset: hardreset method (can be NULL)
2945c6fd2807SJeff Garzik  *	@postreset: postreset method (can be NULL)
2946c6fd2807SJeff Garzik  *
2947c6fd2807SJeff Garzik  *	Perform standard error handling sequence.
2948c6fd2807SJeff Garzik  *
2949c6fd2807SJeff Garzik  *	LOCKING:
2950c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
2951c6fd2807SJeff Garzik  */
2952c6fd2807SJeff Garzik void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2953c6fd2807SJeff Garzik 	       ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2954c6fd2807SJeff Garzik 	       ata_postreset_fn_t postreset)
2955c6fd2807SJeff Garzik {
29569b1e2658STejun Heo 	struct ata_device *dev;
29579b1e2658STejun Heo 	int rc;
29589b1e2658STejun Heo 
29599b1e2658STejun Heo 	ata_eh_autopsy(ap);
29609b1e2658STejun Heo 	ata_eh_report(ap);
29619b1e2658STejun Heo 
29629b1e2658STejun Heo 	rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
29639b1e2658STejun Heo 			    NULL);
29649b1e2658STejun Heo 	if (rc) {
29659b1e2658STejun Heo 		ata_link_for_each_dev(dev, &ap->link)
29669b1e2658STejun Heo 			ata_dev_disable(dev);
29679b1e2658STejun Heo 	}
29689b1e2658STejun Heo 
2969c6fd2807SJeff Garzik 	ata_eh_finish(ap);
2970c6fd2807SJeff Garzik }
2971c6fd2807SJeff Garzik 
2972a1efdabaSTejun Heo /**
2973a1efdabaSTejun Heo  *	ata_std_error_handler - standard error handler
2974a1efdabaSTejun Heo  *	@ap: host port to handle error for
2975a1efdabaSTejun Heo  *
2976a1efdabaSTejun Heo  *	Standard error handler
2977a1efdabaSTejun Heo  *
2978a1efdabaSTejun Heo  *	LOCKING:
2979a1efdabaSTejun Heo  *	Kernel thread context (may sleep).
2980a1efdabaSTejun Heo  */
2981a1efdabaSTejun Heo void ata_std_error_handler(struct ata_port *ap)
2982a1efdabaSTejun Heo {
2983a1efdabaSTejun Heo 	struct ata_port_operations *ops = ap->ops;
2984a1efdabaSTejun Heo 	ata_reset_fn_t hardreset = ops->hardreset;
2985a1efdabaSTejun Heo 
298657c9efdfSTejun Heo 	/* ignore built-in hardreset if SCR access is not available */
298757c9efdfSTejun Heo 	if (ata_is_builtin_hardreset(hardreset) && !sata_scr_valid(&ap->link))
2988a1efdabaSTejun Heo 		hardreset = NULL;
2989a1efdabaSTejun Heo 
2990a1efdabaSTejun Heo 	ata_do_eh(ap, ops->prereset, ops->softreset, hardreset, ops->postreset);
2991a1efdabaSTejun Heo }
2992a1efdabaSTejun Heo 
29936ffa01d8STejun Heo #ifdef CONFIG_PM
2994c6fd2807SJeff Garzik /**
2995c6fd2807SJeff Garzik  *	ata_eh_handle_port_suspend - perform port suspend operation
2996c6fd2807SJeff Garzik  *	@ap: port to suspend
2997c6fd2807SJeff Garzik  *
2998c6fd2807SJeff Garzik  *	Suspend @ap.
2999c6fd2807SJeff Garzik  *
3000c6fd2807SJeff Garzik  *	LOCKING:
3001c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
3002c6fd2807SJeff Garzik  */
3003c6fd2807SJeff Garzik static void ata_eh_handle_port_suspend(struct ata_port *ap)
3004c6fd2807SJeff Garzik {
3005c6fd2807SJeff Garzik 	unsigned long flags;
3006c6fd2807SJeff Garzik 	int rc = 0;
3007c6fd2807SJeff Garzik 
3008c6fd2807SJeff Garzik 	/* are we suspending? */
3009c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
3010c6fd2807SJeff Garzik 	if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
3011c6fd2807SJeff Garzik 	    ap->pm_mesg.event == PM_EVENT_ON) {
3012c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
3013c6fd2807SJeff Garzik 		return;
3014c6fd2807SJeff Garzik 	}
3015c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
3016c6fd2807SJeff Garzik 
3017c6fd2807SJeff Garzik 	WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
3018c6fd2807SJeff Garzik 
301964578a3dSTejun Heo 	/* tell ACPI we're suspending */
302064578a3dSTejun Heo 	rc = ata_acpi_on_suspend(ap);
302164578a3dSTejun Heo 	if (rc)
302264578a3dSTejun Heo 		goto out;
302364578a3dSTejun Heo 
3024c6fd2807SJeff Garzik 	/* suspend */
3025c6fd2807SJeff Garzik 	ata_eh_freeze_port(ap);
3026c6fd2807SJeff Garzik 
3027c6fd2807SJeff Garzik 	if (ap->ops->port_suspend)
3028c6fd2807SJeff Garzik 		rc = ap->ops->port_suspend(ap, ap->pm_mesg);
3029c6fd2807SJeff Garzik 
3030bd3adca5SShaohua Li 	ata_acpi_set_state(ap, PMSG_SUSPEND);
303164578a3dSTejun Heo  out:
3032c6fd2807SJeff Garzik 	/* report result */
3033c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
3034c6fd2807SJeff Garzik 
3035c6fd2807SJeff Garzik 	ap->pflags &= ~ATA_PFLAG_PM_PENDING;
3036c6fd2807SJeff Garzik 	if (rc == 0)
3037c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_SUSPENDED;
303864578a3dSTejun Heo 	else if (ap->pflags & ATA_PFLAG_FROZEN)
3039c6fd2807SJeff Garzik 		ata_port_schedule_eh(ap);
3040c6fd2807SJeff Garzik 
3041c6fd2807SJeff Garzik 	if (ap->pm_result) {
3042c6fd2807SJeff Garzik 		*ap->pm_result = rc;
3043c6fd2807SJeff Garzik 		ap->pm_result = NULL;
3044c6fd2807SJeff Garzik 	}
3045c6fd2807SJeff Garzik 
3046c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
3047c6fd2807SJeff Garzik 
3048c6fd2807SJeff Garzik 	return;
3049c6fd2807SJeff Garzik }
3050c6fd2807SJeff Garzik 
3051c6fd2807SJeff Garzik /**
3052c6fd2807SJeff Garzik  *	ata_eh_handle_port_resume - perform port resume operation
3053c6fd2807SJeff Garzik  *	@ap: port to resume
3054c6fd2807SJeff Garzik  *
3055c6fd2807SJeff Garzik  *	Resume @ap.
3056c6fd2807SJeff Garzik  *
3057c6fd2807SJeff Garzik  *	LOCKING:
3058c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
3059c6fd2807SJeff Garzik  */
3060c6fd2807SJeff Garzik static void ata_eh_handle_port_resume(struct ata_port *ap)
3061c6fd2807SJeff Garzik {
3062c6fd2807SJeff Garzik 	unsigned long flags;
30639666f400STejun Heo 	int rc = 0;
3064c6fd2807SJeff Garzik 
3065c6fd2807SJeff Garzik 	/* are we resuming? */
3066c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
3067c6fd2807SJeff Garzik 	if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
3068c6fd2807SJeff Garzik 	    ap->pm_mesg.event != PM_EVENT_ON) {
3069c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
3070c6fd2807SJeff Garzik 		return;
3071c6fd2807SJeff Garzik 	}
3072c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
3073c6fd2807SJeff Garzik 
30749666f400STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
3075c6fd2807SJeff Garzik 
3076bd3adca5SShaohua Li 	ata_acpi_set_state(ap, PMSG_ON);
3077bd3adca5SShaohua Li 
3078c6fd2807SJeff Garzik 	if (ap->ops->port_resume)
3079c6fd2807SJeff Garzik 		rc = ap->ops->port_resume(ap);
3080c6fd2807SJeff Garzik 
30816746544cSTejun Heo 	/* tell ACPI that we're resuming */
30826746544cSTejun Heo 	ata_acpi_on_resume(ap);
30836746544cSTejun Heo 
30849666f400STejun Heo 	/* report result */
3085c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
3086c6fd2807SJeff Garzik 	ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
3087c6fd2807SJeff Garzik 	if (ap->pm_result) {
3088c6fd2807SJeff Garzik 		*ap->pm_result = rc;
3089c6fd2807SJeff Garzik 		ap->pm_result = NULL;
3090c6fd2807SJeff Garzik 	}
3091c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
3092c6fd2807SJeff Garzik }
30936ffa01d8STejun Heo #endif /* CONFIG_PM */
3094