xref: /openbmc/linux/drivers/ata/libata-eh.c (revision 848e4c68c4695beae563f9a3d59fce596b466a74)
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>
36242f9dcbSJens Axboe #include <linux/blkdev.h>
372855568bSJeff Garzik #include <linux/pci.h>
38c6fd2807SJeff Garzik #include <scsi/scsi.h>
39c6fd2807SJeff Garzik #include <scsi/scsi_host.h>
40c6fd2807SJeff Garzik #include <scsi/scsi_eh.h>
41c6fd2807SJeff Garzik #include <scsi/scsi_device.h>
42c6fd2807SJeff Garzik #include <scsi/scsi_cmnd.h>
43c6fd2807SJeff Garzik #include "../scsi/scsi_transport_api.h"
44c6fd2807SJeff Garzik 
45c6fd2807SJeff Garzik #include <linux/libata.h>
46c6fd2807SJeff Garzik 
47c6fd2807SJeff Garzik #include "libata.h"
48c6fd2807SJeff Garzik 
497d47e8d4STejun Heo enum {
503884f7b0STejun Heo 	/* speed down verdicts */
517d47e8d4STejun Heo 	ATA_EH_SPDN_NCQ_OFF		= (1 << 0),
527d47e8d4STejun Heo 	ATA_EH_SPDN_SPEED_DOWN		= (1 << 1),
537d47e8d4STejun Heo 	ATA_EH_SPDN_FALLBACK_TO_PIO	= (1 << 2),
5476326ac1STejun Heo 	ATA_EH_SPDN_KEEP_ERRORS		= (1 << 3),
553884f7b0STejun Heo 
563884f7b0STejun Heo 	/* error flags */
573884f7b0STejun Heo 	ATA_EFLAG_IS_IO			= (1 << 0),
5876326ac1STejun Heo 	ATA_EFLAG_DUBIOUS_XFER		= (1 << 1),
593884f7b0STejun Heo 
603884f7b0STejun Heo 	/* error categories */
613884f7b0STejun Heo 	ATA_ECAT_NONE			= 0,
623884f7b0STejun Heo 	ATA_ECAT_ATA_BUS		= 1,
633884f7b0STejun Heo 	ATA_ECAT_TOUT_HSM		= 2,
643884f7b0STejun Heo 	ATA_ECAT_UNK_DEV		= 3,
6575f9cafcSTejun Heo 	ATA_ECAT_DUBIOUS_NONE		= 4,
6675f9cafcSTejun Heo 	ATA_ECAT_DUBIOUS_ATA_BUS	= 5,
6775f9cafcSTejun Heo 	ATA_ECAT_DUBIOUS_TOUT_HSM	= 6,
6875f9cafcSTejun Heo 	ATA_ECAT_DUBIOUS_UNK_DEV	= 7,
6975f9cafcSTejun Heo 	ATA_ECAT_NR			= 8,
707d47e8d4STejun Heo 
7187fbc5a0STejun Heo 	ATA_EH_CMD_DFL_TIMEOUT		=  5000,
7287fbc5a0STejun Heo 
730a2c0f56STejun Heo 	/* always put at least this amount of time between resets */
740a2c0f56STejun Heo 	ATA_EH_RESET_COOL_DOWN		=  5000,
750a2c0f56STejun Heo 
76341c2c95STejun Heo 	/* Waiting in ->prereset can never be reliable.  It's
77341c2c95STejun Heo 	 * sometimes nice to wait there but it can't be depended upon;
78341c2c95STejun Heo 	 * otherwise, we wouldn't be resetting.  Just give it enough
79341c2c95STejun Heo 	 * time for most drives to spin up.
8031daabdaSTejun Heo 	 */
81341c2c95STejun Heo 	ATA_EH_PRERESET_TIMEOUT		= 10000,
82341c2c95STejun Heo 	ATA_EH_FASTDRAIN_INTERVAL	=  3000,
8311fc33daSTejun Heo 
8411fc33daSTejun Heo 	ATA_EH_UA_TRIES			= 5,
8531daabdaSTejun Heo };
8631daabdaSTejun Heo 
8731daabdaSTejun Heo /* The following table determines how we sequence resets.  Each entry
8831daabdaSTejun Heo  * represents timeout for that try.  The first try can be soft or
8931daabdaSTejun Heo  * hardreset.  All others are hardreset if available.  In most cases
9031daabdaSTejun Heo  * the first reset w/ 10sec timeout should succeed.  Following entries
9131daabdaSTejun Heo  * are mostly for error handling, hotplug and retarded devices.
9231daabdaSTejun Heo  */
9331daabdaSTejun Heo static const unsigned long ata_eh_reset_timeouts[] = {
94341c2c95STejun Heo 	10000,	/* most drives spin up by 10sec */
95341c2c95STejun Heo 	10000,	/* > 99% working drives spin up before 20sec */
96341c2c95STejun Heo 	35000,	/* give > 30 secs of idleness for retarded devices */
97341c2c95STejun Heo 	 5000,	/* and sweet one last chance */
98d8af0eb6STejun Heo 	ULONG_MAX, /* > 1 min has elapsed, give up */
9931daabdaSTejun Heo };
10031daabdaSTejun Heo 
10187fbc5a0STejun Heo static const unsigned long ata_eh_identify_timeouts[] = {
10287fbc5a0STejun Heo 	 5000,	/* covers > 99% of successes and not too boring on failures */
10387fbc5a0STejun Heo 	10000,  /* combined time till here is enough even for media access */
10487fbc5a0STejun Heo 	30000,	/* for true idiots */
10587fbc5a0STejun Heo 	ULONG_MAX,
10687fbc5a0STejun Heo };
10787fbc5a0STejun Heo 
10887fbc5a0STejun Heo static const unsigned long ata_eh_other_timeouts[] = {
10987fbc5a0STejun Heo 	 5000,	/* same rationale as identify timeout */
11087fbc5a0STejun Heo 	10000,	/* ditto */
11187fbc5a0STejun Heo 	/* but no merciful 30sec for other commands, it just isn't worth it */
11287fbc5a0STejun Heo 	ULONG_MAX,
11387fbc5a0STejun Heo };
11487fbc5a0STejun Heo 
11587fbc5a0STejun Heo struct ata_eh_cmd_timeout_ent {
11687fbc5a0STejun Heo 	const u8		*commands;
11787fbc5a0STejun Heo 	const unsigned long	*timeouts;
11887fbc5a0STejun Heo };
11987fbc5a0STejun Heo 
12087fbc5a0STejun Heo /* The following table determines timeouts to use for EH internal
12187fbc5a0STejun Heo  * commands.  Each table entry is a command class and matches the
12287fbc5a0STejun Heo  * commands the entry applies to and the timeout table to use.
12387fbc5a0STejun Heo  *
12487fbc5a0STejun Heo  * On the retry after a command timed out, the next timeout value from
12587fbc5a0STejun Heo  * the table is used.  If the table doesn't contain further entries,
12687fbc5a0STejun Heo  * the last value is used.
12787fbc5a0STejun Heo  *
12887fbc5a0STejun Heo  * ehc->cmd_timeout_idx keeps track of which timeout to use per
12987fbc5a0STejun Heo  * command class, so if SET_FEATURES times out on the first try, the
13087fbc5a0STejun Heo  * next try will use the second timeout value only for that class.
13187fbc5a0STejun Heo  */
13287fbc5a0STejun Heo #define CMDS(cmds...)	(const u8 []){ cmds, 0 }
13387fbc5a0STejun Heo static const struct ata_eh_cmd_timeout_ent
13487fbc5a0STejun Heo ata_eh_cmd_timeout_table[ATA_EH_CMD_TIMEOUT_TABLE_SIZE] = {
13587fbc5a0STejun Heo 	{ .commands = CMDS(ATA_CMD_ID_ATA, ATA_CMD_ID_ATAPI),
13687fbc5a0STejun Heo 	  .timeouts = ata_eh_identify_timeouts, },
13787fbc5a0STejun Heo 	{ .commands = CMDS(ATA_CMD_READ_NATIVE_MAX, ATA_CMD_READ_NATIVE_MAX_EXT),
13887fbc5a0STejun Heo 	  .timeouts = ata_eh_other_timeouts, },
13987fbc5a0STejun Heo 	{ .commands = CMDS(ATA_CMD_SET_MAX, ATA_CMD_SET_MAX_EXT),
14087fbc5a0STejun Heo 	  .timeouts = ata_eh_other_timeouts, },
14187fbc5a0STejun Heo 	{ .commands = CMDS(ATA_CMD_SET_FEATURES),
14287fbc5a0STejun Heo 	  .timeouts = ata_eh_other_timeouts, },
14387fbc5a0STejun Heo 	{ .commands = CMDS(ATA_CMD_INIT_DEV_PARAMS),
14487fbc5a0STejun Heo 	  .timeouts = ata_eh_other_timeouts, },
14587fbc5a0STejun Heo };
14687fbc5a0STejun Heo #undef CMDS
14787fbc5a0STejun Heo 
148c6fd2807SJeff Garzik static void __ata_port_freeze(struct ata_port *ap);
1496ffa01d8STejun Heo #ifdef CONFIG_PM
150c6fd2807SJeff Garzik static void ata_eh_handle_port_suspend(struct ata_port *ap);
151c6fd2807SJeff Garzik static void ata_eh_handle_port_resume(struct ata_port *ap);
1526ffa01d8STejun Heo #else /* CONFIG_PM */
1536ffa01d8STejun Heo static void ata_eh_handle_port_suspend(struct ata_port *ap)
1546ffa01d8STejun Heo { }
1556ffa01d8STejun Heo 
1566ffa01d8STejun Heo static void ata_eh_handle_port_resume(struct ata_port *ap)
1576ffa01d8STejun Heo { }
1586ffa01d8STejun Heo #endif /* CONFIG_PM */
159c6fd2807SJeff Garzik 
160b64bbc39STejun Heo static void __ata_ehi_pushv_desc(struct ata_eh_info *ehi, const char *fmt,
161b64bbc39STejun Heo 				 va_list args)
162b64bbc39STejun Heo {
163b64bbc39STejun Heo 	ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
164b64bbc39STejun Heo 				     ATA_EH_DESC_LEN - ehi->desc_len,
165b64bbc39STejun Heo 				     fmt, args);
166b64bbc39STejun Heo }
167b64bbc39STejun Heo 
168b64bbc39STejun Heo /**
169b64bbc39STejun Heo  *	__ata_ehi_push_desc - push error description without adding separator
170b64bbc39STejun Heo  *	@ehi: target EHI
171b64bbc39STejun Heo  *	@fmt: printf format string
172b64bbc39STejun Heo  *
173b64bbc39STejun Heo  *	Format string according to @fmt and append it to @ehi->desc.
174b64bbc39STejun Heo  *
175b64bbc39STejun Heo  *	LOCKING:
176b64bbc39STejun Heo  *	spin_lock_irqsave(host lock)
177b64bbc39STejun Heo  */
178b64bbc39STejun Heo void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
179b64bbc39STejun Heo {
180b64bbc39STejun Heo 	va_list args;
181b64bbc39STejun Heo 
182b64bbc39STejun Heo 	va_start(args, fmt);
183b64bbc39STejun Heo 	__ata_ehi_pushv_desc(ehi, fmt, args);
184b64bbc39STejun Heo 	va_end(args);
185b64bbc39STejun Heo }
186b64bbc39STejun Heo 
187b64bbc39STejun Heo /**
188b64bbc39STejun Heo  *	ata_ehi_push_desc - push error description with separator
189b64bbc39STejun Heo  *	@ehi: target EHI
190b64bbc39STejun Heo  *	@fmt: printf format string
191b64bbc39STejun Heo  *
192b64bbc39STejun Heo  *	Format string according to @fmt and append it to @ehi->desc.
193b64bbc39STejun Heo  *	If @ehi->desc is not empty, ", " is added in-between.
194b64bbc39STejun Heo  *
195b64bbc39STejun Heo  *	LOCKING:
196b64bbc39STejun Heo  *	spin_lock_irqsave(host lock)
197b64bbc39STejun Heo  */
198b64bbc39STejun Heo void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
199b64bbc39STejun Heo {
200b64bbc39STejun Heo 	va_list args;
201b64bbc39STejun Heo 
202b64bbc39STejun Heo 	if (ehi->desc_len)
203b64bbc39STejun Heo 		__ata_ehi_push_desc(ehi, ", ");
204b64bbc39STejun Heo 
205b64bbc39STejun Heo 	va_start(args, fmt);
206b64bbc39STejun Heo 	__ata_ehi_pushv_desc(ehi, fmt, args);
207b64bbc39STejun Heo 	va_end(args);
208b64bbc39STejun Heo }
209b64bbc39STejun Heo 
210b64bbc39STejun Heo /**
211b64bbc39STejun Heo  *	ata_ehi_clear_desc - clean error description
212b64bbc39STejun Heo  *	@ehi: target EHI
213b64bbc39STejun Heo  *
214b64bbc39STejun Heo  *	Clear @ehi->desc.
215b64bbc39STejun Heo  *
216b64bbc39STejun Heo  *	LOCKING:
217b64bbc39STejun Heo  *	spin_lock_irqsave(host lock)
218b64bbc39STejun Heo  */
219b64bbc39STejun Heo void ata_ehi_clear_desc(struct ata_eh_info *ehi)
220b64bbc39STejun Heo {
221b64bbc39STejun Heo 	ehi->desc[0] = '\0';
222b64bbc39STejun Heo 	ehi->desc_len = 0;
223b64bbc39STejun Heo }
224b64bbc39STejun Heo 
225cbcdd875STejun Heo /**
226cbcdd875STejun Heo  *	ata_port_desc - append port description
227cbcdd875STejun Heo  *	@ap: target ATA port
228cbcdd875STejun Heo  *	@fmt: printf format string
229cbcdd875STejun Heo  *
230cbcdd875STejun Heo  *	Format string according to @fmt and append it to port
231cbcdd875STejun Heo  *	description.  If port description is not empty, " " is added
232cbcdd875STejun Heo  *	in-between.  This function is to be used while initializing
233cbcdd875STejun Heo  *	ata_host.  The description is printed on host registration.
234cbcdd875STejun Heo  *
235cbcdd875STejun Heo  *	LOCKING:
236cbcdd875STejun Heo  *	None.
237cbcdd875STejun Heo  */
238cbcdd875STejun Heo void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
239cbcdd875STejun Heo {
240cbcdd875STejun Heo 	va_list args;
241cbcdd875STejun Heo 
242cbcdd875STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
243cbcdd875STejun Heo 
244cbcdd875STejun Heo 	if (ap->link.eh_info.desc_len)
245cbcdd875STejun Heo 		__ata_ehi_push_desc(&ap->link.eh_info, " ");
246cbcdd875STejun Heo 
247cbcdd875STejun Heo 	va_start(args, fmt);
248cbcdd875STejun Heo 	__ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
249cbcdd875STejun Heo 	va_end(args);
250cbcdd875STejun Heo }
251cbcdd875STejun Heo 
252cbcdd875STejun Heo #ifdef CONFIG_PCI
253cbcdd875STejun Heo 
254cbcdd875STejun Heo /**
255cbcdd875STejun Heo  *	ata_port_pbar_desc - append PCI BAR description
256cbcdd875STejun Heo  *	@ap: target ATA port
257cbcdd875STejun Heo  *	@bar: target PCI BAR
258cbcdd875STejun Heo  *	@offset: offset into PCI BAR
259cbcdd875STejun Heo  *	@name: name of the area
260cbcdd875STejun Heo  *
261cbcdd875STejun Heo  *	If @offset is negative, this function formats a string which
262cbcdd875STejun Heo  *	contains the name, address, size and type of the BAR and
263cbcdd875STejun Heo  *	appends it to the port description.  If @offset is zero or
264cbcdd875STejun Heo  *	positive, only name and offsetted address is appended.
265cbcdd875STejun Heo  *
266cbcdd875STejun Heo  *	LOCKING:
267cbcdd875STejun Heo  *	None.
268cbcdd875STejun Heo  */
269cbcdd875STejun Heo void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
270cbcdd875STejun Heo 			const char *name)
271cbcdd875STejun Heo {
272cbcdd875STejun Heo 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
273cbcdd875STejun Heo 	char *type = "";
274cbcdd875STejun Heo 	unsigned long long start, len;
275cbcdd875STejun Heo 
276cbcdd875STejun Heo 	if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
277cbcdd875STejun Heo 		type = "m";
278cbcdd875STejun Heo 	else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
279cbcdd875STejun Heo 		type = "i";
280cbcdd875STejun Heo 
281cbcdd875STejun Heo 	start = (unsigned long long)pci_resource_start(pdev, bar);
282cbcdd875STejun Heo 	len = (unsigned long long)pci_resource_len(pdev, bar);
283cbcdd875STejun Heo 
284cbcdd875STejun Heo 	if (offset < 0)
285cbcdd875STejun Heo 		ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
286cbcdd875STejun Heo 	else
287e6a73ab1SAndrew Morton 		ata_port_desc(ap, "%s 0x%llx", name,
288e6a73ab1SAndrew Morton 				start + (unsigned long long)offset);
289cbcdd875STejun Heo }
290cbcdd875STejun Heo 
291cbcdd875STejun Heo #endif /* CONFIG_PCI */
292cbcdd875STejun Heo 
29387fbc5a0STejun Heo static int ata_lookup_timeout_table(u8 cmd)
29487fbc5a0STejun Heo {
29587fbc5a0STejun Heo 	int i;
29687fbc5a0STejun Heo 
29787fbc5a0STejun Heo 	for (i = 0; i < ATA_EH_CMD_TIMEOUT_TABLE_SIZE; i++) {
29887fbc5a0STejun Heo 		const u8 *cur;
29987fbc5a0STejun Heo 
30087fbc5a0STejun Heo 		for (cur = ata_eh_cmd_timeout_table[i].commands; *cur; cur++)
30187fbc5a0STejun Heo 			if (*cur == cmd)
30287fbc5a0STejun Heo 				return i;
30387fbc5a0STejun Heo 	}
30487fbc5a0STejun Heo 
30587fbc5a0STejun Heo 	return -1;
30687fbc5a0STejun Heo }
30787fbc5a0STejun Heo 
30887fbc5a0STejun Heo /**
30987fbc5a0STejun Heo  *	ata_internal_cmd_timeout - determine timeout for an internal command
31087fbc5a0STejun Heo  *	@dev: target device
31187fbc5a0STejun Heo  *	@cmd: internal command to be issued
31287fbc5a0STejun Heo  *
31387fbc5a0STejun Heo  *	Determine timeout for internal command @cmd for @dev.
31487fbc5a0STejun Heo  *
31587fbc5a0STejun Heo  *	LOCKING:
31687fbc5a0STejun Heo  *	EH context.
31787fbc5a0STejun Heo  *
31887fbc5a0STejun Heo  *	RETURNS:
31987fbc5a0STejun Heo  *	Determined timeout.
32087fbc5a0STejun Heo  */
32187fbc5a0STejun Heo unsigned long ata_internal_cmd_timeout(struct ata_device *dev, u8 cmd)
32287fbc5a0STejun Heo {
32387fbc5a0STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
32487fbc5a0STejun Heo 	int ent = ata_lookup_timeout_table(cmd);
32587fbc5a0STejun Heo 	int idx;
32687fbc5a0STejun Heo 
32787fbc5a0STejun Heo 	if (ent < 0)
32887fbc5a0STejun Heo 		return ATA_EH_CMD_DFL_TIMEOUT;
32987fbc5a0STejun Heo 
33087fbc5a0STejun Heo 	idx = ehc->cmd_timeout_idx[dev->devno][ent];
33187fbc5a0STejun Heo 	return ata_eh_cmd_timeout_table[ent].timeouts[idx];
33287fbc5a0STejun Heo }
33387fbc5a0STejun Heo 
33487fbc5a0STejun Heo /**
33587fbc5a0STejun Heo  *	ata_internal_cmd_timed_out - notification for internal command timeout
33687fbc5a0STejun Heo  *	@dev: target device
33787fbc5a0STejun Heo  *	@cmd: internal command which timed out
33887fbc5a0STejun Heo  *
33987fbc5a0STejun Heo  *	Notify EH that internal command @cmd for @dev timed out.  This
34087fbc5a0STejun Heo  *	function should be called only for commands whose timeouts are
34187fbc5a0STejun Heo  *	determined using ata_internal_cmd_timeout().
34287fbc5a0STejun Heo  *
34387fbc5a0STejun Heo  *	LOCKING:
34487fbc5a0STejun Heo  *	EH context.
34587fbc5a0STejun Heo  */
34687fbc5a0STejun Heo void ata_internal_cmd_timed_out(struct ata_device *dev, u8 cmd)
34787fbc5a0STejun Heo {
34887fbc5a0STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
34987fbc5a0STejun Heo 	int ent = ata_lookup_timeout_table(cmd);
35087fbc5a0STejun Heo 	int idx;
35187fbc5a0STejun Heo 
35287fbc5a0STejun Heo 	if (ent < 0)
35387fbc5a0STejun Heo 		return;
35487fbc5a0STejun Heo 
35587fbc5a0STejun Heo 	idx = ehc->cmd_timeout_idx[dev->devno][ent];
35687fbc5a0STejun Heo 	if (ata_eh_cmd_timeout_table[ent].timeouts[idx + 1] != ULONG_MAX)
35787fbc5a0STejun Heo 		ehc->cmd_timeout_idx[dev->devno][ent]++;
35887fbc5a0STejun Heo }
35987fbc5a0STejun Heo 
3603884f7b0STejun Heo static void ata_ering_record(struct ata_ering *ering, unsigned int eflags,
361c6fd2807SJeff Garzik 			     unsigned int err_mask)
362c6fd2807SJeff Garzik {
363c6fd2807SJeff Garzik 	struct ata_ering_entry *ent;
364c6fd2807SJeff Garzik 
365c6fd2807SJeff Garzik 	WARN_ON(!err_mask);
366c6fd2807SJeff Garzik 
367c6fd2807SJeff Garzik 	ering->cursor++;
368c6fd2807SJeff Garzik 	ering->cursor %= ATA_ERING_SIZE;
369c6fd2807SJeff Garzik 
370c6fd2807SJeff Garzik 	ent = &ering->ring[ering->cursor];
3713884f7b0STejun Heo 	ent->eflags = eflags;
372c6fd2807SJeff Garzik 	ent->err_mask = err_mask;
373c6fd2807SJeff Garzik 	ent->timestamp = get_jiffies_64();
374c6fd2807SJeff Garzik }
375c6fd2807SJeff Garzik 
37676326ac1STejun Heo static struct ata_ering_entry *ata_ering_top(struct ata_ering *ering)
37776326ac1STejun Heo {
37876326ac1STejun Heo 	struct ata_ering_entry *ent = &ering->ring[ering->cursor];
37976326ac1STejun Heo 
38076326ac1STejun Heo 	if (ent->err_mask)
38176326ac1STejun Heo 		return ent;
38276326ac1STejun Heo 	return NULL;
38376326ac1STejun Heo }
38476326ac1STejun Heo 
3857d47e8d4STejun Heo static void ata_ering_clear(struct ata_ering *ering)
386c6fd2807SJeff Garzik {
3877d47e8d4STejun Heo 	memset(ering, 0, sizeof(*ering));
388c6fd2807SJeff Garzik }
389c6fd2807SJeff Garzik 
390c6fd2807SJeff Garzik static int ata_ering_map(struct ata_ering *ering,
391c6fd2807SJeff Garzik 			 int (*map_fn)(struct ata_ering_entry *, void *),
392c6fd2807SJeff Garzik 			 void *arg)
393c6fd2807SJeff Garzik {
394c6fd2807SJeff Garzik 	int idx, rc = 0;
395c6fd2807SJeff Garzik 	struct ata_ering_entry *ent;
396c6fd2807SJeff Garzik 
397c6fd2807SJeff Garzik 	idx = ering->cursor;
398c6fd2807SJeff Garzik 	do {
399c6fd2807SJeff Garzik 		ent = &ering->ring[idx];
400c6fd2807SJeff Garzik 		if (!ent->err_mask)
401c6fd2807SJeff Garzik 			break;
402c6fd2807SJeff Garzik 		rc = map_fn(ent, arg);
403c6fd2807SJeff Garzik 		if (rc)
404c6fd2807SJeff Garzik 			break;
405c6fd2807SJeff Garzik 		idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
406c6fd2807SJeff Garzik 	} while (idx != ering->cursor);
407c6fd2807SJeff Garzik 
408c6fd2807SJeff Garzik 	return rc;
409c6fd2807SJeff Garzik }
410c6fd2807SJeff Garzik 
411c6fd2807SJeff Garzik static unsigned int ata_eh_dev_action(struct ata_device *dev)
412c6fd2807SJeff Garzik {
4139af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
414c6fd2807SJeff Garzik 
415c6fd2807SJeff Garzik 	return ehc->i.action | ehc->i.dev_action[dev->devno];
416c6fd2807SJeff Garzik }
417c6fd2807SJeff Garzik 
418f58229f8STejun Heo static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
419c6fd2807SJeff Garzik 				struct ata_eh_info *ehi, unsigned int action)
420c6fd2807SJeff Garzik {
421f58229f8STejun Heo 	struct ata_device *tdev;
422c6fd2807SJeff Garzik 
423c6fd2807SJeff Garzik 	if (!dev) {
424c6fd2807SJeff Garzik 		ehi->action &= ~action;
425f58229f8STejun Heo 		ata_link_for_each_dev(tdev, link)
426f58229f8STejun Heo 			ehi->dev_action[tdev->devno] &= ~action;
427c6fd2807SJeff Garzik 	} else {
428c6fd2807SJeff Garzik 		/* doesn't make sense for port-wide EH actions */
429c6fd2807SJeff Garzik 		WARN_ON(!(action & ATA_EH_PERDEV_MASK));
430c6fd2807SJeff Garzik 
431c6fd2807SJeff Garzik 		/* break ehi->action into ehi->dev_action */
432c6fd2807SJeff Garzik 		if (ehi->action & action) {
433f58229f8STejun Heo 			ata_link_for_each_dev(tdev, link)
434f58229f8STejun Heo 				ehi->dev_action[tdev->devno] |=
435f58229f8STejun Heo 					ehi->action & action;
436c6fd2807SJeff Garzik 			ehi->action &= ~action;
437c6fd2807SJeff Garzik 		}
438c6fd2807SJeff Garzik 
439c6fd2807SJeff Garzik 		/* turn off the specified per-dev action */
440c6fd2807SJeff Garzik 		ehi->dev_action[dev->devno] &= ~action;
441c6fd2807SJeff Garzik 	}
442c6fd2807SJeff Garzik }
443c6fd2807SJeff Garzik 
444c6fd2807SJeff Garzik /**
445c6fd2807SJeff Garzik  *	ata_scsi_timed_out - SCSI layer time out callback
446c6fd2807SJeff Garzik  *	@cmd: timed out SCSI command
447c6fd2807SJeff Garzik  *
448c6fd2807SJeff Garzik  *	Handles SCSI layer timeout.  We race with normal completion of
449c6fd2807SJeff Garzik  *	the qc for @cmd.  If the qc is already gone, we lose and let
450c6fd2807SJeff Garzik  *	the scsi command finish (EH_HANDLED).  Otherwise, the qc has
451c6fd2807SJeff Garzik  *	timed out and EH should be invoked.  Prevent ata_qc_complete()
452c6fd2807SJeff Garzik  *	from finishing it by setting EH_SCHEDULED and return
453c6fd2807SJeff Garzik  *	EH_NOT_HANDLED.
454c6fd2807SJeff Garzik  *
455c6fd2807SJeff Garzik  *	TODO: kill this function once old EH is gone.
456c6fd2807SJeff Garzik  *
457c6fd2807SJeff Garzik  *	LOCKING:
458c6fd2807SJeff Garzik  *	Called from timer context
459c6fd2807SJeff Garzik  *
460c6fd2807SJeff Garzik  *	RETURNS:
461c6fd2807SJeff Garzik  *	EH_HANDLED or EH_NOT_HANDLED
462c6fd2807SJeff Garzik  */
463242f9dcbSJens Axboe enum blk_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
464c6fd2807SJeff Garzik {
465c6fd2807SJeff Garzik 	struct Scsi_Host *host = cmd->device->host;
466c6fd2807SJeff Garzik 	struct ata_port *ap = ata_shost_to_port(host);
467c6fd2807SJeff Garzik 	unsigned long flags;
468c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
469242f9dcbSJens Axboe 	enum blk_eh_timer_return ret;
470c6fd2807SJeff Garzik 
471c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
472c6fd2807SJeff Garzik 
473c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
474242f9dcbSJens Axboe 		ret = BLK_EH_NOT_HANDLED;
475c6fd2807SJeff Garzik 		goto out;
476c6fd2807SJeff Garzik 	}
477c6fd2807SJeff Garzik 
478242f9dcbSJens Axboe 	ret = BLK_EH_HANDLED;
479c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
4809af5c9c9STejun Heo 	qc = ata_qc_from_tag(ap, ap->link.active_tag);
481c6fd2807SJeff Garzik 	if (qc) {
482c6fd2807SJeff Garzik 		WARN_ON(qc->scsicmd != cmd);
483c6fd2807SJeff Garzik 		qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
484c6fd2807SJeff Garzik 		qc->err_mask |= AC_ERR_TIMEOUT;
485242f9dcbSJens Axboe 		ret = BLK_EH_NOT_HANDLED;
486c6fd2807SJeff Garzik 	}
487c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
488c6fd2807SJeff Garzik 
489c6fd2807SJeff Garzik  out:
490c6fd2807SJeff Garzik 	DPRINTK("EXIT, ret=%d\n", ret);
491c6fd2807SJeff Garzik 	return ret;
492c6fd2807SJeff Garzik }
493c6fd2807SJeff Garzik 
494c6fd2807SJeff Garzik /**
495c6fd2807SJeff Garzik  *	ata_scsi_error - SCSI layer error handler callback
496c6fd2807SJeff Garzik  *	@host: SCSI host on which error occurred
497c6fd2807SJeff Garzik  *
498c6fd2807SJeff Garzik  *	Handles SCSI-layer-thrown error events.
499c6fd2807SJeff Garzik  *
500c6fd2807SJeff Garzik  *	LOCKING:
501c6fd2807SJeff Garzik  *	Inherited from SCSI layer (none, can sleep)
502c6fd2807SJeff Garzik  *
503c6fd2807SJeff Garzik  *	RETURNS:
504c6fd2807SJeff Garzik  *	Zero.
505c6fd2807SJeff Garzik  */
506c6fd2807SJeff Garzik void ata_scsi_error(struct Scsi_Host *host)
507c6fd2807SJeff Garzik {
508c6fd2807SJeff Garzik 	struct ata_port *ap = ata_shost_to_port(host);
509a1e10f7eSTejun Heo 	int i;
510c6fd2807SJeff Garzik 	unsigned long flags;
511c6fd2807SJeff Garzik 
512c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
513c6fd2807SJeff Garzik 
514c6fd2807SJeff Garzik 	/* synchronize with port task */
515c6fd2807SJeff Garzik 	ata_port_flush_task(ap);
516c6fd2807SJeff Garzik 
517cca3974eSJeff Garzik 	/* synchronize with host lock and sort out timeouts */
518c6fd2807SJeff Garzik 
519c6fd2807SJeff Garzik 	/* For new EH, all qcs are finished in one of three ways -
520c6fd2807SJeff Garzik 	 * normal completion, error completion, and SCSI timeout.
521c6fd2807SJeff Garzik 	 * Both cmpletions can race against SCSI timeout.  When normal
522c6fd2807SJeff Garzik 	 * completion wins, the qc never reaches EH.  When error
523c6fd2807SJeff Garzik 	 * completion wins, the qc has ATA_QCFLAG_FAILED set.
524c6fd2807SJeff Garzik 	 *
525c6fd2807SJeff Garzik 	 * When SCSI timeout wins, things are a bit more complex.
526c6fd2807SJeff Garzik 	 * Normal or error completion can occur after the timeout but
527c6fd2807SJeff Garzik 	 * before this point.  In such cases, both types of
528c6fd2807SJeff Garzik 	 * completions are honored.  A scmd is determined to have
529c6fd2807SJeff Garzik 	 * timed out iff its associated qc is active and not failed.
530c6fd2807SJeff Garzik 	 */
531c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
532c6fd2807SJeff Garzik 		struct scsi_cmnd *scmd, *tmp;
533c6fd2807SJeff Garzik 		int nr_timedout = 0;
534c6fd2807SJeff Garzik 
535c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
536c6fd2807SJeff Garzik 
537c6fd2807SJeff Garzik 		list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
538c6fd2807SJeff Garzik 			struct ata_queued_cmd *qc;
539c6fd2807SJeff Garzik 
540c6fd2807SJeff Garzik 			for (i = 0; i < ATA_MAX_QUEUE; i++) {
541c6fd2807SJeff Garzik 				qc = __ata_qc_from_tag(ap, i);
542c6fd2807SJeff Garzik 				if (qc->flags & ATA_QCFLAG_ACTIVE &&
543c6fd2807SJeff Garzik 				    qc->scsicmd == scmd)
544c6fd2807SJeff Garzik 					break;
545c6fd2807SJeff Garzik 			}
546c6fd2807SJeff Garzik 
547c6fd2807SJeff Garzik 			if (i < ATA_MAX_QUEUE) {
548c6fd2807SJeff Garzik 				/* the scmd has an associated qc */
549c6fd2807SJeff Garzik 				if (!(qc->flags & ATA_QCFLAG_FAILED)) {
550c6fd2807SJeff Garzik 					/* which hasn't failed yet, timeout */
551c6fd2807SJeff Garzik 					qc->err_mask |= AC_ERR_TIMEOUT;
552c6fd2807SJeff Garzik 					qc->flags |= ATA_QCFLAG_FAILED;
553c6fd2807SJeff Garzik 					nr_timedout++;
554c6fd2807SJeff Garzik 				}
555c6fd2807SJeff Garzik 			} else {
556c6fd2807SJeff Garzik 				/* Normal completion occurred after
557c6fd2807SJeff Garzik 				 * SCSI timeout but before this point.
558c6fd2807SJeff Garzik 				 * Successfully complete it.
559c6fd2807SJeff Garzik 				 */
560c6fd2807SJeff Garzik 				scmd->retries = scmd->allowed;
561c6fd2807SJeff Garzik 				scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
562c6fd2807SJeff Garzik 			}
563c6fd2807SJeff Garzik 		}
564c6fd2807SJeff Garzik 
565c6fd2807SJeff Garzik 		/* If we have timed out qcs.  They belong to EH from
566c6fd2807SJeff Garzik 		 * this point but the state of the controller is
567c6fd2807SJeff Garzik 		 * unknown.  Freeze the port to make sure the IRQ
568c6fd2807SJeff Garzik 		 * handler doesn't diddle with those qcs.  This must
569c6fd2807SJeff Garzik 		 * be done atomically w.r.t. setting QCFLAG_FAILED.
570c6fd2807SJeff Garzik 		 */
571c6fd2807SJeff Garzik 		if (nr_timedout)
572c6fd2807SJeff Garzik 			__ata_port_freeze(ap);
573c6fd2807SJeff Garzik 
574c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
575a1e10f7eSTejun Heo 
576a1e10f7eSTejun Heo 		/* initialize eh_tries */
577a1e10f7eSTejun Heo 		ap->eh_tries = ATA_EH_MAX_TRIES;
578c6fd2807SJeff Garzik 	} else
579c6fd2807SJeff Garzik 		spin_unlock_wait(ap->lock);
580c6fd2807SJeff Garzik 
581c6fd2807SJeff Garzik  repeat:
582c6fd2807SJeff Garzik 	/* invoke error handler */
583c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
584cf1b86c8STejun Heo 		struct ata_link *link;
585cf1b86c8STejun Heo 
5865ddf24c5STejun Heo 		/* kill fast drain timer */
5875ddf24c5STejun Heo 		del_timer_sync(&ap->fastdrain_timer);
5885ddf24c5STejun Heo 
589c6fd2807SJeff Garzik 		/* process port resume request */
590c6fd2807SJeff Garzik 		ata_eh_handle_port_resume(ap);
591c6fd2807SJeff Garzik 
592c6fd2807SJeff Garzik 		/* fetch & clear EH info */
593c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
594c6fd2807SJeff Garzik 
595cf1b86c8STejun Heo 		__ata_port_for_each_link(link, ap) {
59600115e0fSTejun Heo 			struct ata_eh_context *ehc = &link->eh_context;
59700115e0fSTejun Heo 			struct ata_device *dev;
59800115e0fSTejun Heo 
599cf1b86c8STejun Heo 			memset(&link->eh_context, 0, sizeof(link->eh_context));
600cf1b86c8STejun Heo 			link->eh_context.i = link->eh_info;
601cf1b86c8STejun Heo 			memset(&link->eh_info, 0, sizeof(link->eh_info));
60200115e0fSTejun Heo 
60300115e0fSTejun Heo 			ata_link_for_each_dev(dev, link) {
60400115e0fSTejun Heo 				int devno = dev->devno;
60500115e0fSTejun Heo 
60600115e0fSTejun Heo 				ehc->saved_xfer_mode[devno] = dev->xfer_mode;
60700115e0fSTejun Heo 				if (ata_ncq_enabled(dev))
60800115e0fSTejun Heo 					ehc->saved_ncq_enabled |= 1 << devno;
60900115e0fSTejun Heo 			}
6100a2c0f56STejun Heo 
6110a2c0f56STejun Heo 			/* set last reset timestamp to some time in the past */
6120a2c0f56STejun Heo 			ehc->last_reset = jiffies - 60 * HZ;
613cf1b86c8STejun Heo 		}
614c6fd2807SJeff Garzik 
615c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
616c6fd2807SJeff Garzik 		ap->pflags &= ~ATA_PFLAG_EH_PENDING;
617da917d69STejun Heo 		ap->excl_link = NULL;	/* don't maintain exclusion over EH */
618c6fd2807SJeff Garzik 
619c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
620c6fd2807SJeff Garzik 
621c6fd2807SJeff Garzik 		/* invoke EH, skip if unloading or suspended */
622c6fd2807SJeff Garzik 		if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
623c6fd2807SJeff Garzik 			ap->ops->error_handler(ap);
624c6fd2807SJeff Garzik 		else
625c6fd2807SJeff Garzik 			ata_eh_finish(ap);
626c6fd2807SJeff Garzik 
627c6fd2807SJeff Garzik 		/* process port suspend request */
628c6fd2807SJeff Garzik 		ata_eh_handle_port_suspend(ap);
629c6fd2807SJeff Garzik 
630c6fd2807SJeff Garzik 		/* Exception might have happend after ->error_handler
631c6fd2807SJeff Garzik 		 * recovered the port but before this point.  Repeat
632c6fd2807SJeff Garzik 		 * EH in such case.
633c6fd2807SJeff Garzik 		 */
634c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
635c6fd2807SJeff Garzik 
636c6fd2807SJeff Garzik 		if (ap->pflags & ATA_PFLAG_EH_PENDING) {
637a1e10f7eSTejun Heo 			if (--ap->eh_tries) {
638c6fd2807SJeff Garzik 				spin_unlock_irqrestore(ap->lock, flags);
639c6fd2807SJeff Garzik 				goto repeat;
640c6fd2807SJeff Garzik 			}
641c6fd2807SJeff Garzik 			ata_port_printk(ap, KERN_ERR, "EH pending after %d "
642a1e10f7eSTejun Heo 					"tries, giving up\n", ATA_EH_MAX_TRIES);
643914616a3STejun Heo 			ap->pflags &= ~ATA_PFLAG_EH_PENDING;
644c6fd2807SJeff Garzik 		}
645c6fd2807SJeff Garzik 
646c6fd2807SJeff Garzik 		/* this run is complete, make sure EH info is clear */
647cf1b86c8STejun Heo 		__ata_port_for_each_link(link, ap)
648cf1b86c8STejun Heo 			memset(&link->eh_info, 0, sizeof(link->eh_info));
649c6fd2807SJeff Garzik 
650c6fd2807SJeff Garzik 		/* Clear host_eh_scheduled while holding ap->lock such
651c6fd2807SJeff Garzik 		 * that if exception occurs after this point but
652c6fd2807SJeff Garzik 		 * before EH completion, SCSI midlayer will
653c6fd2807SJeff Garzik 		 * re-initiate EH.
654c6fd2807SJeff Garzik 		 */
655c6fd2807SJeff Garzik 		host->host_eh_scheduled = 0;
656c6fd2807SJeff Garzik 
657c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
658c6fd2807SJeff Garzik 	} else {
6599af5c9c9STejun Heo 		WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
660c6fd2807SJeff Garzik 		ap->ops->eng_timeout(ap);
661c6fd2807SJeff Garzik 	}
662c6fd2807SJeff Garzik 
663c6fd2807SJeff Garzik 	/* finish or retry handled scmd's and clean up */
664c6fd2807SJeff Garzik 	WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
665c6fd2807SJeff Garzik 
666c6fd2807SJeff Garzik 	scsi_eh_flush_done_q(&ap->eh_done_q);
667c6fd2807SJeff Garzik 
668c6fd2807SJeff Garzik 	/* clean up */
669c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
670c6fd2807SJeff Garzik 
671c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_LOADING)
672c6fd2807SJeff Garzik 		ap->pflags &= ~ATA_PFLAG_LOADING;
673c6fd2807SJeff Garzik 	else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
67452bad64dSDavid Howells 		queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0);
675c6fd2807SJeff Garzik 
676c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_RECOVERED)
677c6fd2807SJeff Garzik 		ata_port_printk(ap, KERN_INFO, "EH complete\n");
678c6fd2807SJeff Garzik 
679c6fd2807SJeff Garzik 	ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
680c6fd2807SJeff Garzik 
681c6fd2807SJeff Garzik 	/* tell wait_eh that we're done */
682c6fd2807SJeff Garzik 	ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
683c6fd2807SJeff Garzik 	wake_up_all(&ap->eh_wait_q);
684c6fd2807SJeff Garzik 
685c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
686c6fd2807SJeff Garzik 
687c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
688c6fd2807SJeff Garzik }
689c6fd2807SJeff Garzik 
690c6fd2807SJeff Garzik /**
691c6fd2807SJeff Garzik  *	ata_port_wait_eh - Wait for the currently pending EH to complete
692c6fd2807SJeff Garzik  *	@ap: Port to wait EH for
693c6fd2807SJeff Garzik  *
694c6fd2807SJeff Garzik  *	Wait until the currently pending EH is complete.
695c6fd2807SJeff Garzik  *
696c6fd2807SJeff Garzik  *	LOCKING:
697c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
698c6fd2807SJeff Garzik  */
699c6fd2807SJeff Garzik void ata_port_wait_eh(struct ata_port *ap)
700c6fd2807SJeff Garzik {
701c6fd2807SJeff Garzik 	unsigned long flags;
702c6fd2807SJeff Garzik 	DEFINE_WAIT(wait);
703c6fd2807SJeff Garzik 
704c6fd2807SJeff Garzik  retry:
705c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
706c6fd2807SJeff Garzik 
707c6fd2807SJeff Garzik 	while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
708c6fd2807SJeff Garzik 		prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
709c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
710c6fd2807SJeff Garzik 		schedule();
711c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
712c6fd2807SJeff Garzik 	}
713c6fd2807SJeff Garzik 	finish_wait(&ap->eh_wait_q, &wait);
714c6fd2807SJeff Garzik 
715c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
716c6fd2807SJeff Garzik 
717c6fd2807SJeff Garzik 	/* make sure SCSI EH is complete */
718cca3974eSJeff Garzik 	if (scsi_host_in_recovery(ap->scsi_host)) {
719c6fd2807SJeff Garzik 		msleep(10);
720c6fd2807SJeff Garzik 		goto retry;
721c6fd2807SJeff Garzik 	}
722c6fd2807SJeff Garzik }
723c6fd2807SJeff Garzik 
7245ddf24c5STejun Heo static int ata_eh_nr_in_flight(struct ata_port *ap)
7255ddf24c5STejun Heo {
7265ddf24c5STejun Heo 	unsigned int tag;
7275ddf24c5STejun Heo 	int nr = 0;
7285ddf24c5STejun Heo 
7295ddf24c5STejun Heo 	/* count only non-internal commands */
7305ddf24c5STejun Heo 	for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++)
7315ddf24c5STejun Heo 		if (ata_qc_from_tag(ap, tag))
7325ddf24c5STejun Heo 			nr++;
7335ddf24c5STejun Heo 
7345ddf24c5STejun Heo 	return nr;
7355ddf24c5STejun Heo }
7365ddf24c5STejun Heo 
7375ddf24c5STejun Heo void ata_eh_fastdrain_timerfn(unsigned long arg)
7385ddf24c5STejun Heo {
7395ddf24c5STejun Heo 	struct ata_port *ap = (void *)arg;
7405ddf24c5STejun Heo 	unsigned long flags;
7415ddf24c5STejun Heo 	int cnt;
7425ddf24c5STejun Heo 
7435ddf24c5STejun Heo 	spin_lock_irqsave(ap->lock, flags);
7445ddf24c5STejun Heo 
7455ddf24c5STejun Heo 	cnt = ata_eh_nr_in_flight(ap);
7465ddf24c5STejun Heo 
7475ddf24c5STejun Heo 	/* are we done? */
7485ddf24c5STejun Heo 	if (!cnt)
7495ddf24c5STejun Heo 		goto out_unlock;
7505ddf24c5STejun Heo 
7515ddf24c5STejun Heo 	if (cnt == ap->fastdrain_cnt) {
7525ddf24c5STejun Heo 		unsigned int tag;
7535ddf24c5STejun Heo 
7545ddf24c5STejun Heo 		/* No progress during the last interval, tag all
7555ddf24c5STejun Heo 		 * in-flight qcs as timed out and freeze the port.
7565ddf24c5STejun Heo 		 */
7575ddf24c5STejun Heo 		for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) {
7585ddf24c5STejun Heo 			struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
7595ddf24c5STejun Heo 			if (qc)
7605ddf24c5STejun Heo 				qc->err_mask |= AC_ERR_TIMEOUT;
7615ddf24c5STejun Heo 		}
7625ddf24c5STejun Heo 
7635ddf24c5STejun Heo 		ata_port_freeze(ap);
7645ddf24c5STejun Heo 	} else {
7655ddf24c5STejun Heo 		/* some qcs have finished, give it another chance */
7665ddf24c5STejun Heo 		ap->fastdrain_cnt = cnt;
7675ddf24c5STejun Heo 		ap->fastdrain_timer.expires =
768341c2c95STejun Heo 			ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
7695ddf24c5STejun Heo 		add_timer(&ap->fastdrain_timer);
7705ddf24c5STejun Heo 	}
7715ddf24c5STejun Heo 
7725ddf24c5STejun Heo  out_unlock:
7735ddf24c5STejun Heo 	spin_unlock_irqrestore(ap->lock, flags);
7745ddf24c5STejun Heo }
7755ddf24c5STejun Heo 
7765ddf24c5STejun Heo /**
7775ddf24c5STejun Heo  *	ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
7785ddf24c5STejun Heo  *	@ap: target ATA port
7795ddf24c5STejun Heo  *	@fastdrain: activate fast drain
7805ddf24c5STejun Heo  *
7815ddf24c5STejun Heo  *	Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
7825ddf24c5STejun Heo  *	is non-zero and EH wasn't pending before.  Fast drain ensures
7835ddf24c5STejun Heo  *	that EH kicks in in timely manner.
7845ddf24c5STejun Heo  *
7855ddf24c5STejun Heo  *	LOCKING:
7865ddf24c5STejun Heo  *	spin_lock_irqsave(host lock)
7875ddf24c5STejun Heo  */
7885ddf24c5STejun Heo static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
7895ddf24c5STejun Heo {
7905ddf24c5STejun Heo 	int cnt;
7915ddf24c5STejun Heo 
7925ddf24c5STejun Heo 	/* already scheduled? */
7935ddf24c5STejun Heo 	if (ap->pflags & ATA_PFLAG_EH_PENDING)
7945ddf24c5STejun Heo 		return;
7955ddf24c5STejun Heo 
7965ddf24c5STejun Heo 	ap->pflags |= ATA_PFLAG_EH_PENDING;
7975ddf24c5STejun Heo 
7985ddf24c5STejun Heo 	if (!fastdrain)
7995ddf24c5STejun Heo 		return;
8005ddf24c5STejun Heo 
8015ddf24c5STejun Heo 	/* do we have in-flight qcs? */
8025ddf24c5STejun Heo 	cnt = ata_eh_nr_in_flight(ap);
8035ddf24c5STejun Heo 	if (!cnt)
8045ddf24c5STejun Heo 		return;
8055ddf24c5STejun Heo 
8065ddf24c5STejun Heo 	/* activate fast drain */
8075ddf24c5STejun Heo 	ap->fastdrain_cnt = cnt;
808341c2c95STejun Heo 	ap->fastdrain_timer.expires =
809341c2c95STejun Heo 		ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
8105ddf24c5STejun Heo 	add_timer(&ap->fastdrain_timer);
8115ddf24c5STejun Heo }
8125ddf24c5STejun Heo 
813c6fd2807SJeff Garzik /**
814c6fd2807SJeff Garzik  *	ata_qc_schedule_eh - schedule qc for error handling
815c6fd2807SJeff Garzik  *	@qc: command to schedule error handling for
816c6fd2807SJeff Garzik  *
817c6fd2807SJeff Garzik  *	Schedule error handling for @qc.  EH will kick in as soon as
818c6fd2807SJeff Garzik  *	other commands are drained.
819c6fd2807SJeff Garzik  *
820c6fd2807SJeff Garzik  *	LOCKING:
821cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
822c6fd2807SJeff Garzik  */
823c6fd2807SJeff Garzik void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
824c6fd2807SJeff Garzik {
825c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
826c6fd2807SJeff Garzik 
827c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
828c6fd2807SJeff Garzik 
829c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_FAILED;
8305ddf24c5STejun Heo 	ata_eh_set_pending(ap, 1);
831c6fd2807SJeff Garzik 
832c6fd2807SJeff Garzik 	/* The following will fail if timeout has already expired.
833c6fd2807SJeff Garzik 	 * ata_scsi_error() takes care of such scmds on EH entry.
834c6fd2807SJeff Garzik 	 * Note that ATA_QCFLAG_FAILED is unconditionally set after
835c6fd2807SJeff Garzik 	 * this function completes.
836c6fd2807SJeff Garzik 	 */
837242f9dcbSJens Axboe 	blk_abort_request(qc->scsicmd->request);
838c6fd2807SJeff Garzik }
839c6fd2807SJeff Garzik 
840c6fd2807SJeff Garzik /**
841c6fd2807SJeff Garzik  *	ata_port_schedule_eh - schedule error handling without a qc
842c6fd2807SJeff Garzik  *	@ap: ATA port to schedule EH for
843c6fd2807SJeff Garzik  *
844c6fd2807SJeff Garzik  *	Schedule error handling for @ap.  EH will kick in as soon as
845c6fd2807SJeff Garzik  *	all commands are drained.
846c6fd2807SJeff Garzik  *
847c6fd2807SJeff Garzik  *	LOCKING:
848cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
849c6fd2807SJeff Garzik  */
850c6fd2807SJeff Garzik void ata_port_schedule_eh(struct ata_port *ap)
851c6fd2807SJeff Garzik {
852c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
853c6fd2807SJeff Garzik 
854f4d6d004STejun Heo 	if (ap->pflags & ATA_PFLAG_INITIALIZING)
855f4d6d004STejun Heo 		return;
856f4d6d004STejun Heo 
8575ddf24c5STejun Heo 	ata_eh_set_pending(ap, 1);
858cca3974eSJeff Garzik 	scsi_schedule_eh(ap->scsi_host);
859c6fd2807SJeff Garzik 
860c6fd2807SJeff Garzik 	DPRINTK("port EH scheduled\n");
861c6fd2807SJeff Garzik }
862c6fd2807SJeff Garzik 
863dbd82616STejun Heo static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
864c6fd2807SJeff Garzik {
865c6fd2807SJeff Garzik 	int tag, nr_aborted = 0;
866c6fd2807SJeff Garzik 
867c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
868c6fd2807SJeff Garzik 
8695ddf24c5STejun Heo 	/* we're gonna abort all commands, no need for fast drain */
8705ddf24c5STejun Heo 	ata_eh_set_pending(ap, 0);
8715ddf24c5STejun Heo 
872c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
873c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
874c6fd2807SJeff Garzik 
875dbd82616STejun Heo 		if (qc && (!link || qc->dev->link == link)) {
876c6fd2807SJeff Garzik 			qc->flags |= ATA_QCFLAG_FAILED;
877c6fd2807SJeff Garzik 			ata_qc_complete(qc);
878c6fd2807SJeff Garzik 			nr_aborted++;
879c6fd2807SJeff Garzik 		}
880c6fd2807SJeff Garzik 	}
881c6fd2807SJeff Garzik 
882c6fd2807SJeff Garzik 	if (!nr_aborted)
883c6fd2807SJeff Garzik 		ata_port_schedule_eh(ap);
884c6fd2807SJeff Garzik 
885c6fd2807SJeff Garzik 	return nr_aborted;
886c6fd2807SJeff Garzik }
887c6fd2807SJeff Garzik 
888c6fd2807SJeff Garzik /**
889dbd82616STejun Heo  *	ata_link_abort - abort all qc's on the link
890dbd82616STejun Heo  *	@link: ATA link to abort qc's for
891dbd82616STejun Heo  *
892dbd82616STejun Heo  *	Abort all active qc's active on @link and schedule EH.
893dbd82616STejun Heo  *
894dbd82616STejun Heo  *	LOCKING:
895dbd82616STejun Heo  *	spin_lock_irqsave(host lock)
896dbd82616STejun Heo  *
897dbd82616STejun Heo  *	RETURNS:
898dbd82616STejun Heo  *	Number of aborted qc's.
899dbd82616STejun Heo  */
900dbd82616STejun Heo int ata_link_abort(struct ata_link *link)
901dbd82616STejun Heo {
902dbd82616STejun Heo 	return ata_do_link_abort(link->ap, link);
903dbd82616STejun Heo }
904dbd82616STejun Heo 
905dbd82616STejun Heo /**
906dbd82616STejun Heo  *	ata_port_abort - abort all qc's on the port
907dbd82616STejun Heo  *	@ap: ATA port to abort qc's for
908dbd82616STejun Heo  *
909dbd82616STejun Heo  *	Abort all active qc's of @ap and schedule EH.
910dbd82616STejun Heo  *
911dbd82616STejun Heo  *	LOCKING:
912dbd82616STejun Heo  *	spin_lock_irqsave(host_set lock)
913dbd82616STejun Heo  *
914dbd82616STejun Heo  *	RETURNS:
915dbd82616STejun Heo  *	Number of aborted qc's.
916dbd82616STejun Heo  */
917dbd82616STejun Heo int ata_port_abort(struct ata_port *ap)
918dbd82616STejun Heo {
919dbd82616STejun Heo 	return ata_do_link_abort(ap, NULL);
920dbd82616STejun Heo }
921dbd82616STejun Heo 
922dbd82616STejun Heo /**
923c6fd2807SJeff Garzik  *	__ata_port_freeze - freeze port
924c6fd2807SJeff Garzik  *	@ap: ATA port to freeze
925c6fd2807SJeff Garzik  *
926c6fd2807SJeff Garzik  *	This function is called when HSM violation or some other
927c6fd2807SJeff Garzik  *	condition disrupts normal operation of the port.  Frozen port
928c6fd2807SJeff Garzik  *	is not allowed to perform any operation until the port is
929c6fd2807SJeff Garzik  *	thawed, which usually follows a successful reset.
930c6fd2807SJeff Garzik  *
931c6fd2807SJeff Garzik  *	ap->ops->freeze() callback can be used for freezing the port
932c6fd2807SJeff Garzik  *	hardware-wise (e.g. mask interrupt and stop DMA engine).  If a
933c6fd2807SJeff Garzik  *	port cannot be frozen hardware-wise, the interrupt handler
934c6fd2807SJeff Garzik  *	must ack and clear interrupts unconditionally while the port
935c6fd2807SJeff Garzik  *	is frozen.
936c6fd2807SJeff Garzik  *
937c6fd2807SJeff Garzik  *	LOCKING:
938cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
939c6fd2807SJeff Garzik  */
940c6fd2807SJeff Garzik static void __ata_port_freeze(struct ata_port *ap)
941c6fd2807SJeff Garzik {
942c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
943c6fd2807SJeff Garzik 
944c6fd2807SJeff Garzik 	if (ap->ops->freeze)
945c6fd2807SJeff Garzik 		ap->ops->freeze(ap);
946c6fd2807SJeff Garzik 
947c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_FROZEN;
948c6fd2807SJeff Garzik 
94944877b4eSTejun Heo 	DPRINTK("ata%u port frozen\n", ap->print_id);
950c6fd2807SJeff Garzik }
951c6fd2807SJeff Garzik 
952c6fd2807SJeff Garzik /**
953c6fd2807SJeff Garzik  *	ata_port_freeze - abort & freeze port
954c6fd2807SJeff Garzik  *	@ap: ATA port to freeze
955c6fd2807SJeff Garzik  *
956c6fd2807SJeff Garzik  *	Abort and freeze @ap.
957c6fd2807SJeff Garzik  *
958c6fd2807SJeff Garzik  *	LOCKING:
959cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
960c6fd2807SJeff Garzik  *
961c6fd2807SJeff Garzik  *	RETURNS:
962c6fd2807SJeff Garzik  *	Number of aborted commands.
963c6fd2807SJeff Garzik  */
964c6fd2807SJeff Garzik int ata_port_freeze(struct ata_port *ap)
965c6fd2807SJeff Garzik {
966c6fd2807SJeff Garzik 	int nr_aborted;
967c6fd2807SJeff Garzik 
968c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
969c6fd2807SJeff Garzik 
970c6fd2807SJeff Garzik 	nr_aborted = ata_port_abort(ap);
971c6fd2807SJeff Garzik 	__ata_port_freeze(ap);
972c6fd2807SJeff Garzik 
973c6fd2807SJeff Garzik 	return nr_aborted;
974c6fd2807SJeff Garzik }
975c6fd2807SJeff Garzik 
976c6fd2807SJeff Garzik /**
9777d77b247STejun Heo  *	sata_async_notification - SATA async notification handler
9787d77b247STejun Heo  *	@ap: ATA port where async notification is received
9797d77b247STejun Heo  *
9807d77b247STejun Heo  *	Handler to be called when async notification via SDB FIS is
9817d77b247STejun Heo  *	received.  This function schedules EH if necessary.
9827d77b247STejun Heo  *
9837d77b247STejun Heo  *	LOCKING:
9847d77b247STejun Heo  *	spin_lock_irqsave(host lock)
9857d77b247STejun Heo  *
9867d77b247STejun Heo  *	RETURNS:
9877d77b247STejun Heo  *	1 if EH is scheduled, 0 otherwise.
9887d77b247STejun Heo  */
9897d77b247STejun Heo int sata_async_notification(struct ata_port *ap)
9907d77b247STejun Heo {
9917d77b247STejun Heo 	u32 sntf;
9927d77b247STejun Heo 	int rc;
9937d77b247STejun Heo 
9947d77b247STejun Heo 	if (!(ap->flags & ATA_FLAG_AN))
9957d77b247STejun Heo 		return 0;
9967d77b247STejun Heo 
9977d77b247STejun Heo 	rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
9987d77b247STejun Heo 	if (rc == 0)
9997d77b247STejun Heo 		sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
10007d77b247STejun Heo 
1001071f44b1STejun Heo 	if (!sata_pmp_attached(ap) || rc) {
10027d77b247STejun Heo 		/* PMP is not attached or SNTF is not available */
1003071f44b1STejun Heo 		if (!sata_pmp_attached(ap)) {
10047d77b247STejun Heo 			/* PMP is not attached.  Check whether ATAPI
10057d77b247STejun Heo 			 * AN is configured.  If so, notify media
10067d77b247STejun Heo 			 * change.
10077d77b247STejun Heo 			 */
10087d77b247STejun Heo 			struct ata_device *dev = ap->link.device;
10097d77b247STejun Heo 
10107d77b247STejun Heo 			if ((dev->class == ATA_DEV_ATAPI) &&
10117d77b247STejun Heo 			    (dev->flags & ATA_DFLAG_AN))
10127d77b247STejun Heo 				ata_scsi_media_change_notify(dev);
10137d77b247STejun Heo 			return 0;
10147d77b247STejun Heo 		} else {
10157d77b247STejun Heo 			/* PMP is attached but SNTF is not available.
10167d77b247STejun Heo 			 * ATAPI async media change notification is
10177d77b247STejun Heo 			 * not used.  The PMP must be reporting PHY
10187d77b247STejun Heo 			 * status change, schedule EH.
10197d77b247STejun Heo 			 */
10207d77b247STejun Heo 			ata_port_schedule_eh(ap);
10217d77b247STejun Heo 			return 1;
10227d77b247STejun Heo 		}
10237d77b247STejun Heo 	} else {
10247d77b247STejun Heo 		/* PMP is attached and SNTF is available */
10257d77b247STejun Heo 		struct ata_link *link;
10267d77b247STejun Heo 
10277d77b247STejun Heo 		/* check and notify ATAPI AN */
10287d77b247STejun Heo 		ata_port_for_each_link(link, ap) {
10297d77b247STejun Heo 			if (!(sntf & (1 << link->pmp)))
10307d77b247STejun Heo 				continue;
10317d77b247STejun Heo 
10327d77b247STejun Heo 			if ((link->device->class == ATA_DEV_ATAPI) &&
10337d77b247STejun Heo 			    (link->device->flags & ATA_DFLAG_AN))
10347d77b247STejun Heo 				ata_scsi_media_change_notify(link->device);
10357d77b247STejun Heo 		}
10367d77b247STejun Heo 
10377d77b247STejun Heo 		/* If PMP is reporting that PHY status of some
10387d77b247STejun Heo 		 * downstream ports has changed, schedule EH.
10397d77b247STejun Heo 		 */
10407d77b247STejun Heo 		if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
10417d77b247STejun Heo 			ata_port_schedule_eh(ap);
10427d77b247STejun Heo 			return 1;
10437d77b247STejun Heo 		}
10447d77b247STejun Heo 
10457d77b247STejun Heo 		return 0;
10467d77b247STejun Heo 	}
10477d77b247STejun Heo }
10487d77b247STejun Heo 
10497d77b247STejun Heo /**
1050c6fd2807SJeff Garzik  *	ata_eh_freeze_port - EH helper to freeze port
1051c6fd2807SJeff Garzik  *	@ap: ATA port to freeze
1052c6fd2807SJeff Garzik  *
1053c6fd2807SJeff Garzik  *	Freeze @ap.
1054c6fd2807SJeff Garzik  *
1055c6fd2807SJeff Garzik  *	LOCKING:
1056c6fd2807SJeff Garzik  *	None.
1057c6fd2807SJeff Garzik  */
1058c6fd2807SJeff Garzik void ata_eh_freeze_port(struct ata_port *ap)
1059c6fd2807SJeff Garzik {
1060c6fd2807SJeff Garzik 	unsigned long flags;
1061c6fd2807SJeff Garzik 
1062c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
1063c6fd2807SJeff Garzik 		return;
1064c6fd2807SJeff Garzik 
1065c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1066c6fd2807SJeff Garzik 	__ata_port_freeze(ap);
1067c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1068c6fd2807SJeff Garzik }
1069c6fd2807SJeff Garzik 
1070c6fd2807SJeff Garzik /**
1071c6fd2807SJeff Garzik  *	ata_port_thaw_port - EH helper to thaw port
1072c6fd2807SJeff Garzik  *	@ap: ATA port to thaw
1073c6fd2807SJeff Garzik  *
1074c6fd2807SJeff Garzik  *	Thaw frozen port @ap.
1075c6fd2807SJeff Garzik  *
1076c6fd2807SJeff Garzik  *	LOCKING:
1077c6fd2807SJeff Garzik  *	None.
1078c6fd2807SJeff Garzik  */
1079c6fd2807SJeff Garzik void ata_eh_thaw_port(struct ata_port *ap)
1080c6fd2807SJeff Garzik {
1081c6fd2807SJeff Garzik 	unsigned long flags;
1082c6fd2807SJeff Garzik 
1083c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
1084c6fd2807SJeff Garzik 		return;
1085c6fd2807SJeff Garzik 
1086c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1087c6fd2807SJeff Garzik 
1088c6fd2807SJeff Garzik 	ap->pflags &= ~ATA_PFLAG_FROZEN;
1089c6fd2807SJeff Garzik 
1090c6fd2807SJeff Garzik 	if (ap->ops->thaw)
1091c6fd2807SJeff Garzik 		ap->ops->thaw(ap);
1092c6fd2807SJeff Garzik 
1093c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1094c6fd2807SJeff Garzik 
109544877b4eSTejun Heo 	DPRINTK("ata%u port thawed\n", ap->print_id);
1096c6fd2807SJeff Garzik }
1097c6fd2807SJeff Garzik 
1098c6fd2807SJeff Garzik static void ata_eh_scsidone(struct scsi_cmnd *scmd)
1099c6fd2807SJeff Garzik {
1100c6fd2807SJeff Garzik 	/* nada */
1101c6fd2807SJeff Garzik }
1102c6fd2807SJeff Garzik 
1103c6fd2807SJeff Garzik static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
1104c6fd2807SJeff Garzik {
1105c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
1106c6fd2807SJeff Garzik 	struct scsi_cmnd *scmd = qc->scsicmd;
1107c6fd2807SJeff Garzik 	unsigned long flags;
1108c6fd2807SJeff Garzik 
1109c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1110c6fd2807SJeff Garzik 	qc->scsidone = ata_eh_scsidone;
1111c6fd2807SJeff Garzik 	__ata_qc_complete(qc);
1112c6fd2807SJeff Garzik 	WARN_ON(ata_tag_valid(qc->tag));
1113c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1114c6fd2807SJeff Garzik 
1115c6fd2807SJeff Garzik 	scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
1116c6fd2807SJeff Garzik }
1117c6fd2807SJeff Garzik 
1118c6fd2807SJeff Garzik /**
1119c6fd2807SJeff Garzik  *	ata_eh_qc_complete - Complete an active ATA command from EH
1120c6fd2807SJeff Garzik  *	@qc: Command to complete
1121c6fd2807SJeff Garzik  *
1122c6fd2807SJeff Garzik  *	Indicate to the mid and upper layers that an ATA command has
1123c6fd2807SJeff Garzik  *	completed.  To be used from EH.
1124c6fd2807SJeff Garzik  */
1125c6fd2807SJeff Garzik void ata_eh_qc_complete(struct ata_queued_cmd *qc)
1126c6fd2807SJeff Garzik {
1127c6fd2807SJeff Garzik 	struct scsi_cmnd *scmd = qc->scsicmd;
1128c6fd2807SJeff Garzik 	scmd->retries = scmd->allowed;
1129c6fd2807SJeff Garzik 	__ata_eh_qc_complete(qc);
1130c6fd2807SJeff Garzik }
1131c6fd2807SJeff Garzik 
1132c6fd2807SJeff Garzik /**
1133c6fd2807SJeff Garzik  *	ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
1134c6fd2807SJeff Garzik  *	@qc: Command to retry
1135c6fd2807SJeff Garzik  *
1136c6fd2807SJeff Garzik  *	Indicate to the mid and upper layers that an ATA command
1137c6fd2807SJeff Garzik  *	should be retried.  To be used from EH.
1138c6fd2807SJeff Garzik  *
1139c6fd2807SJeff Garzik  *	SCSI midlayer limits the number of retries to scmd->allowed.
1140c6fd2807SJeff Garzik  *	scmd->retries is decremented for commands which get retried
1141c6fd2807SJeff Garzik  *	due to unrelated failures (qc->err_mask is zero).
1142c6fd2807SJeff Garzik  */
1143c6fd2807SJeff Garzik void ata_eh_qc_retry(struct ata_queued_cmd *qc)
1144c6fd2807SJeff Garzik {
1145c6fd2807SJeff Garzik 	struct scsi_cmnd *scmd = qc->scsicmd;
1146c6fd2807SJeff Garzik 	if (!qc->err_mask && scmd->retries)
1147c6fd2807SJeff Garzik 		scmd->retries--;
1148c6fd2807SJeff Garzik 	__ata_eh_qc_complete(qc);
1149c6fd2807SJeff Garzik }
1150c6fd2807SJeff Garzik 
1151c6fd2807SJeff Garzik /**
1152c6fd2807SJeff Garzik  *	ata_eh_detach_dev - detach ATA device
1153c6fd2807SJeff Garzik  *	@dev: ATA device to detach
1154c6fd2807SJeff Garzik  *
1155c6fd2807SJeff Garzik  *	Detach @dev.
1156c6fd2807SJeff Garzik  *
1157c6fd2807SJeff Garzik  *	LOCKING:
1158c6fd2807SJeff Garzik  *	None.
1159c6fd2807SJeff Garzik  */
1160fb7fd614STejun Heo void ata_eh_detach_dev(struct ata_device *dev)
1161c6fd2807SJeff Garzik {
1162f58229f8STejun Heo 	struct ata_link *link = dev->link;
1163f58229f8STejun Heo 	struct ata_port *ap = link->ap;
1164c6fd2807SJeff Garzik 	unsigned long flags;
1165c6fd2807SJeff Garzik 
1166c6fd2807SJeff Garzik 	ata_dev_disable(dev);
1167c6fd2807SJeff Garzik 
1168c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1169c6fd2807SJeff Garzik 
1170c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_DETACH;
1171c6fd2807SJeff Garzik 
1172c6fd2807SJeff Garzik 	if (ata_scsi_offline_dev(dev)) {
1173c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_DETACHED;
1174c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
1175c6fd2807SJeff Garzik 	}
1176c6fd2807SJeff Garzik 
1177c6fd2807SJeff Garzik 	/* clear per-dev EH actions */
1178f58229f8STejun Heo 	ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
1179f58229f8STejun Heo 	ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
1180c6fd2807SJeff Garzik 
1181c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1182c6fd2807SJeff Garzik }
1183c6fd2807SJeff Garzik 
1184c6fd2807SJeff Garzik /**
1185c6fd2807SJeff Garzik  *	ata_eh_about_to_do - about to perform eh_action
1186955e57dfSTejun Heo  *	@link: target ATA link
1187c6fd2807SJeff Garzik  *	@dev: target ATA dev for per-dev action (can be NULL)
1188c6fd2807SJeff Garzik  *	@action: action about to be performed
1189c6fd2807SJeff Garzik  *
1190c6fd2807SJeff Garzik  *	Called just before performing EH actions to clear related bits
1191955e57dfSTejun Heo  *	in @link->eh_info such that eh actions are not unnecessarily
1192955e57dfSTejun Heo  *	repeated.
1193c6fd2807SJeff Garzik  *
1194c6fd2807SJeff Garzik  *	LOCKING:
1195c6fd2807SJeff Garzik  *	None.
1196c6fd2807SJeff Garzik  */
1197fb7fd614STejun Heo void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
1198c6fd2807SJeff Garzik 			unsigned int action)
1199c6fd2807SJeff Garzik {
1200955e57dfSTejun Heo 	struct ata_port *ap = link->ap;
1201955e57dfSTejun Heo 	struct ata_eh_info *ehi = &link->eh_info;
1202955e57dfSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
1203c6fd2807SJeff Garzik 	unsigned long flags;
1204c6fd2807SJeff Garzik 
1205c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1206c6fd2807SJeff Garzik 
1207955e57dfSTejun Heo 	ata_eh_clear_action(link, dev, ehi, action);
1208c6fd2807SJeff Garzik 
1209c6fd2807SJeff Garzik 	if (!(ehc->i.flags & ATA_EHI_QUIET))
1210c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_RECOVERED;
1211c6fd2807SJeff Garzik 
1212c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1213c6fd2807SJeff Garzik }
1214c6fd2807SJeff Garzik 
1215c6fd2807SJeff Garzik /**
1216c6fd2807SJeff Garzik  *	ata_eh_done - EH action complete
1217c6fd2807SJeff Garzik *	@ap: target ATA port
1218c6fd2807SJeff Garzik  *	@dev: target ATA dev for per-dev action (can be NULL)
1219c6fd2807SJeff Garzik  *	@action: action just completed
1220c6fd2807SJeff Garzik  *
1221c6fd2807SJeff Garzik  *	Called right after performing EH actions to clear related bits
1222955e57dfSTejun Heo  *	in @link->eh_context.
1223c6fd2807SJeff Garzik  *
1224c6fd2807SJeff Garzik  *	LOCKING:
1225c6fd2807SJeff Garzik  *	None.
1226c6fd2807SJeff Garzik  */
1227fb7fd614STejun Heo void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1228c6fd2807SJeff Garzik 		 unsigned int action)
1229c6fd2807SJeff Garzik {
1230955e57dfSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
12319af5c9c9STejun Heo 
1232955e57dfSTejun Heo 	ata_eh_clear_action(link, dev, &ehc->i, action);
1233c6fd2807SJeff Garzik }
1234c6fd2807SJeff Garzik 
1235c6fd2807SJeff Garzik /**
1236c6fd2807SJeff Garzik  *	ata_err_string - convert err_mask to descriptive string
1237c6fd2807SJeff Garzik  *	@err_mask: error mask to convert to string
1238c6fd2807SJeff Garzik  *
1239c6fd2807SJeff Garzik  *	Convert @err_mask to descriptive string.  Errors are
1240c6fd2807SJeff Garzik  *	prioritized according to severity and only the most severe
1241c6fd2807SJeff Garzik  *	error is reported.
1242c6fd2807SJeff Garzik  *
1243c6fd2807SJeff Garzik  *	LOCKING:
1244c6fd2807SJeff Garzik  *	None.
1245c6fd2807SJeff Garzik  *
1246c6fd2807SJeff Garzik  *	RETURNS:
1247c6fd2807SJeff Garzik  *	Descriptive string for @err_mask
1248c6fd2807SJeff Garzik  */
1249c6fd2807SJeff Garzik static const char *ata_err_string(unsigned int err_mask)
1250c6fd2807SJeff Garzik {
1251c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_HOST_BUS)
1252c6fd2807SJeff Garzik 		return "host bus error";
1253c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_ATA_BUS)
1254c6fd2807SJeff Garzik 		return "ATA bus error";
1255c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_TIMEOUT)
1256c6fd2807SJeff Garzik 		return "timeout";
1257c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_HSM)
1258c6fd2807SJeff Garzik 		return "HSM violation";
1259c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_SYSTEM)
1260c6fd2807SJeff Garzik 		return "internal error";
1261c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_MEDIA)
1262c6fd2807SJeff Garzik 		return "media error";
1263c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_INVALID)
1264c6fd2807SJeff Garzik 		return "invalid argument";
1265c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_DEV)
1266c6fd2807SJeff Garzik 		return "device error";
1267c6fd2807SJeff Garzik 	return "unknown error";
1268c6fd2807SJeff Garzik }
1269c6fd2807SJeff Garzik 
1270c6fd2807SJeff Garzik /**
1271c6fd2807SJeff Garzik  *	ata_read_log_page - read a specific log page
1272c6fd2807SJeff Garzik  *	@dev: target device
1273c6fd2807SJeff Garzik  *	@page: page to read
1274c6fd2807SJeff Garzik  *	@buf: buffer to store read page
1275c6fd2807SJeff Garzik  *	@sectors: number of sectors to read
1276c6fd2807SJeff Garzik  *
1277c6fd2807SJeff Garzik  *	Read log page using READ_LOG_EXT command.
1278c6fd2807SJeff Garzik  *
1279c6fd2807SJeff Garzik  *	LOCKING:
1280c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1281c6fd2807SJeff Garzik  *
1282c6fd2807SJeff Garzik  *	RETURNS:
1283c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
1284c6fd2807SJeff Garzik  */
1285c6fd2807SJeff Garzik static unsigned int ata_read_log_page(struct ata_device *dev,
1286c6fd2807SJeff Garzik 				      u8 page, void *buf, unsigned int sectors)
1287c6fd2807SJeff Garzik {
1288c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1289c6fd2807SJeff Garzik 	unsigned int err_mask;
1290c6fd2807SJeff Garzik 
1291c6fd2807SJeff Garzik 	DPRINTK("read log page - page %d\n", page);
1292c6fd2807SJeff Garzik 
1293c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
1294c6fd2807SJeff Garzik 	tf.command = ATA_CMD_READ_LOG_EXT;
1295c6fd2807SJeff Garzik 	tf.lbal = page;
1296c6fd2807SJeff Garzik 	tf.nsect = sectors;
1297c6fd2807SJeff Garzik 	tf.hob_nsect = sectors >> 8;
1298c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1299c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_PIO;
1300c6fd2807SJeff Garzik 
1301c6fd2807SJeff Garzik 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
13022b789108STejun Heo 				     buf, sectors * ATA_SECT_SIZE, 0);
1303c6fd2807SJeff Garzik 
1304c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
1305c6fd2807SJeff Garzik 	return err_mask;
1306c6fd2807SJeff Garzik }
1307c6fd2807SJeff Garzik 
1308c6fd2807SJeff Garzik /**
1309c6fd2807SJeff Garzik  *	ata_eh_read_log_10h - Read log page 10h for NCQ error details
1310c6fd2807SJeff Garzik  *	@dev: Device to read log page 10h from
1311c6fd2807SJeff Garzik  *	@tag: Resulting tag of the failed command
1312c6fd2807SJeff Garzik  *	@tf: Resulting taskfile registers of the failed command
1313c6fd2807SJeff Garzik  *
1314c6fd2807SJeff Garzik  *	Read log page 10h to obtain NCQ error details and clear error
1315c6fd2807SJeff Garzik  *	condition.
1316c6fd2807SJeff Garzik  *
1317c6fd2807SJeff Garzik  *	LOCKING:
1318c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1319c6fd2807SJeff Garzik  *
1320c6fd2807SJeff Garzik  *	RETURNS:
1321c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
1322c6fd2807SJeff Garzik  */
1323c6fd2807SJeff Garzik static int ata_eh_read_log_10h(struct ata_device *dev,
1324c6fd2807SJeff Garzik 			       int *tag, struct ata_taskfile *tf)
1325c6fd2807SJeff Garzik {
13269af5c9c9STejun Heo 	u8 *buf = dev->link->ap->sector_buf;
1327c6fd2807SJeff Garzik 	unsigned int err_mask;
1328c6fd2807SJeff Garzik 	u8 csum;
1329c6fd2807SJeff Garzik 	int i;
1330c6fd2807SJeff Garzik 
1331c6fd2807SJeff Garzik 	err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
1332c6fd2807SJeff Garzik 	if (err_mask)
1333c6fd2807SJeff Garzik 		return -EIO;
1334c6fd2807SJeff Garzik 
1335c6fd2807SJeff Garzik 	csum = 0;
1336c6fd2807SJeff Garzik 	for (i = 0; i < ATA_SECT_SIZE; i++)
1337c6fd2807SJeff Garzik 		csum += buf[i];
1338c6fd2807SJeff Garzik 	if (csum)
1339c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_WARNING,
1340c6fd2807SJeff Garzik 			       "invalid checksum 0x%x on log page 10h\n", csum);
1341c6fd2807SJeff Garzik 
1342c6fd2807SJeff Garzik 	if (buf[0] & 0x80)
1343c6fd2807SJeff Garzik 		return -ENOENT;
1344c6fd2807SJeff Garzik 
1345c6fd2807SJeff Garzik 	*tag = buf[0] & 0x1f;
1346c6fd2807SJeff Garzik 
1347c6fd2807SJeff Garzik 	tf->command = buf[2];
1348c6fd2807SJeff Garzik 	tf->feature = buf[3];
1349c6fd2807SJeff Garzik 	tf->lbal = buf[4];
1350c6fd2807SJeff Garzik 	tf->lbam = buf[5];
1351c6fd2807SJeff Garzik 	tf->lbah = buf[6];
1352c6fd2807SJeff Garzik 	tf->device = buf[7];
1353c6fd2807SJeff Garzik 	tf->hob_lbal = buf[8];
1354c6fd2807SJeff Garzik 	tf->hob_lbam = buf[9];
1355c6fd2807SJeff Garzik 	tf->hob_lbah = buf[10];
1356c6fd2807SJeff Garzik 	tf->nsect = buf[12];
1357c6fd2807SJeff Garzik 	tf->hob_nsect = buf[13];
1358c6fd2807SJeff Garzik 
1359c6fd2807SJeff Garzik 	return 0;
1360c6fd2807SJeff Garzik }
1361c6fd2807SJeff Garzik 
1362c6fd2807SJeff Garzik /**
136311fc33daSTejun Heo  *	atapi_eh_tur - perform ATAPI TEST_UNIT_READY
136411fc33daSTejun Heo  *	@dev: target ATAPI device
136511fc33daSTejun Heo  *	@r_sense_key: out parameter for sense_key
136611fc33daSTejun Heo  *
136711fc33daSTejun Heo  *	Perform ATAPI TEST_UNIT_READY.
136811fc33daSTejun Heo  *
136911fc33daSTejun Heo  *	LOCKING:
137011fc33daSTejun Heo  *	EH context (may sleep).
137111fc33daSTejun Heo  *
137211fc33daSTejun Heo  *	RETURNS:
137311fc33daSTejun Heo  *	0 on success, AC_ERR_* mask on failure.
137411fc33daSTejun Heo  */
137511fc33daSTejun Heo static unsigned int atapi_eh_tur(struct ata_device *dev, u8 *r_sense_key)
137611fc33daSTejun Heo {
137711fc33daSTejun Heo 	u8 cdb[ATAPI_CDB_LEN] = { TEST_UNIT_READY, 0, 0, 0, 0, 0 };
137811fc33daSTejun Heo 	struct ata_taskfile tf;
137911fc33daSTejun Heo 	unsigned int err_mask;
138011fc33daSTejun Heo 
138111fc33daSTejun Heo 	ata_tf_init(dev, &tf);
138211fc33daSTejun Heo 
138311fc33daSTejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
138411fc33daSTejun Heo 	tf.command = ATA_CMD_PACKET;
138511fc33daSTejun Heo 	tf.protocol = ATAPI_PROT_NODATA;
138611fc33daSTejun Heo 
138711fc33daSTejun Heo 	err_mask = ata_exec_internal(dev, &tf, cdb, DMA_NONE, NULL, 0, 0);
138811fc33daSTejun Heo 	if (err_mask == AC_ERR_DEV)
138911fc33daSTejun Heo 		*r_sense_key = tf.feature >> 4;
139011fc33daSTejun Heo 	return err_mask;
139111fc33daSTejun Heo }
139211fc33daSTejun Heo 
139311fc33daSTejun Heo /**
1394c6fd2807SJeff Garzik  *	atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1395c6fd2807SJeff Garzik  *	@dev: device to perform REQUEST_SENSE to
1396c6fd2807SJeff Garzik  *	@sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
13973eabddb8STejun Heo  *	@dfl_sense_key: default sense key to use
1398c6fd2807SJeff Garzik  *
1399c6fd2807SJeff Garzik  *	Perform ATAPI REQUEST_SENSE after the device reported CHECK
1400c6fd2807SJeff Garzik  *	SENSE.  This function is EH helper.
1401c6fd2807SJeff Garzik  *
1402c6fd2807SJeff Garzik  *	LOCKING:
1403c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1404c6fd2807SJeff Garzik  *
1405c6fd2807SJeff Garzik  *	RETURNS:
1406c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask on failure
1407c6fd2807SJeff Garzik  */
14083eabddb8STejun Heo static unsigned int atapi_eh_request_sense(struct ata_device *dev,
14093eabddb8STejun Heo 					   u8 *sense_buf, u8 dfl_sense_key)
1410c6fd2807SJeff Garzik {
14113eabddb8STejun Heo 	u8 cdb[ATAPI_CDB_LEN] =
14123eabddb8STejun Heo 		{ REQUEST_SENSE, 0, 0, 0, SCSI_SENSE_BUFFERSIZE, 0 };
14139af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
1414c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1415c6fd2807SJeff Garzik 
1416c6fd2807SJeff Garzik 	DPRINTK("ATAPI request sense\n");
1417c6fd2807SJeff Garzik 
1418c6fd2807SJeff Garzik 	/* FIXME: is this needed? */
1419c6fd2807SJeff Garzik 	memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1420c6fd2807SJeff Garzik 
142156287768SAlbert Lee 	/* initialize sense_buf with the error register,
142256287768SAlbert Lee 	 * for the case where they are -not- overwritten
142356287768SAlbert Lee 	 */
1424c6fd2807SJeff Garzik 	sense_buf[0] = 0x70;
14253eabddb8STejun Heo 	sense_buf[2] = dfl_sense_key;
142656287768SAlbert Lee 
142756287768SAlbert Lee 	/* some devices time out if garbage left in tf */
142856287768SAlbert Lee 	ata_tf_init(dev, &tf);
1429c6fd2807SJeff Garzik 
1430c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1431c6fd2807SJeff Garzik 	tf.command = ATA_CMD_PACKET;
1432c6fd2807SJeff Garzik 
1433c6fd2807SJeff Garzik 	/* is it pointless to prefer PIO for "safety reasons"? */
1434c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_PIO_DMA) {
14350dc36888STejun Heo 		tf.protocol = ATAPI_PROT_DMA;
1436c6fd2807SJeff Garzik 		tf.feature |= ATAPI_PKT_DMA;
1437c6fd2807SJeff Garzik 	} else {
14380dc36888STejun Heo 		tf.protocol = ATAPI_PROT_PIO;
1439f2dfc1a1STejun Heo 		tf.lbam = SCSI_SENSE_BUFFERSIZE;
1440f2dfc1a1STejun Heo 		tf.lbah = 0;
1441c6fd2807SJeff Garzik 	}
1442c6fd2807SJeff Garzik 
1443c6fd2807SJeff Garzik 	return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
14442b789108STejun Heo 				 sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
1445c6fd2807SJeff Garzik }
1446c6fd2807SJeff Garzik 
1447c6fd2807SJeff Garzik /**
1448c6fd2807SJeff Garzik  *	ata_eh_analyze_serror - analyze SError for a failed port
14490260731fSTejun Heo  *	@link: ATA link to analyze SError for
1450c6fd2807SJeff Garzik  *
1451c6fd2807SJeff Garzik  *	Analyze SError if available and further determine cause of
1452c6fd2807SJeff Garzik  *	failure.
1453c6fd2807SJeff Garzik  *
1454c6fd2807SJeff Garzik  *	LOCKING:
1455c6fd2807SJeff Garzik  *	None.
1456c6fd2807SJeff Garzik  */
14570260731fSTejun Heo static void ata_eh_analyze_serror(struct ata_link *link)
1458c6fd2807SJeff Garzik {
14590260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
1460c6fd2807SJeff Garzik 	u32 serror = ehc->i.serror;
1461c6fd2807SJeff Garzik 	unsigned int err_mask = 0, action = 0;
1462f9df58cbSTejun Heo 	u32 hotplug_mask;
1463c6fd2807SJeff Garzik 
1464e0614db2STejun Heo 	if (serror & (SERR_PERSISTENT | SERR_DATA)) {
1465c6fd2807SJeff Garzik 		err_mask |= AC_ERR_ATA_BUS;
1466cf480626STejun Heo 		action |= ATA_EH_RESET;
1467c6fd2807SJeff Garzik 	}
1468c6fd2807SJeff Garzik 	if (serror & SERR_PROTOCOL) {
1469c6fd2807SJeff Garzik 		err_mask |= AC_ERR_HSM;
1470cf480626STejun Heo 		action |= ATA_EH_RESET;
1471c6fd2807SJeff Garzik 	}
1472c6fd2807SJeff Garzik 	if (serror & SERR_INTERNAL) {
1473c6fd2807SJeff Garzik 		err_mask |= AC_ERR_SYSTEM;
1474cf480626STejun Heo 		action |= ATA_EH_RESET;
1475c6fd2807SJeff Garzik 	}
1476f9df58cbSTejun Heo 
1477f9df58cbSTejun Heo 	/* Determine whether a hotplug event has occurred.  Both
1478f9df58cbSTejun Heo 	 * SError.N/X are considered hotplug events for enabled or
1479f9df58cbSTejun Heo 	 * host links.  For disabled PMP links, only N bit is
1480f9df58cbSTejun Heo 	 * considered as X bit is left at 1 for link plugging.
1481f9df58cbSTejun Heo 	 */
1482f9df58cbSTejun Heo 	hotplug_mask = 0;
1483f9df58cbSTejun Heo 
1484f9df58cbSTejun Heo 	if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
1485f9df58cbSTejun Heo 		hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
1486f9df58cbSTejun Heo 	else
1487f9df58cbSTejun Heo 		hotplug_mask = SERR_PHYRDY_CHG;
1488f9df58cbSTejun Heo 
1489f9df58cbSTejun Heo 	if (serror & hotplug_mask)
1490c6fd2807SJeff Garzik 		ata_ehi_hotplugged(&ehc->i);
1491c6fd2807SJeff Garzik 
1492c6fd2807SJeff Garzik 	ehc->i.err_mask |= err_mask;
1493c6fd2807SJeff Garzik 	ehc->i.action |= action;
1494c6fd2807SJeff Garzik }
1495c6fd2807SJeff Garzik 
1496c6fd2807SJeff Garzik /**
1497c6fd2807SJeff Garzik  *	ata_eh_analyze_ncq_error - analyze NCQ error
14980260731fSTejun Heo  *	@link: ATA link to analyze NCQ error for
1499c6fd2807SJeff Garzik  *
1500c6fd2807SJeff Garzik  *	Read log page 10h, determine the offending qc and acquire
1501c6fd2807SJeff Garzik  *	error status TF.  For NCQ device errors, all LLDDs have to do
1502c6fd2807SJeff Garzik  *	is setting AC_ERR_DEV in ehi->err_mask.  This function takes
1503c6fd2807SJeff Garzik  *	care of the rest.
1504c6fd2807SJeff Garzik  *
1505c6fd2807SJeff Garzik  *	LOCKING:
1506c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1507c6fd2807SJeff Garzik  */
150810acf3b0SMark Lord void ata_eh_analyze_ncq_error(struct ata_link *link)
1509c6fd2807SJeff Garzik {
15100260731fSTejun Heo 	struct ata_port *ap = link->ap;
15110260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
15120260731fSTejun Heo 	struct ata_device *dev = link->device;
1513c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
1514c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1515c6fd2807SJeff Garzik 	int tag, rc;
1516c6fd2807SJeff Garzik 
1517c6fd2807SJeff Garzik 	/* if frozen, we can't do much */
1518c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN)
1519c6fd2807SJeff Garzik 		return;
1520c6fd2807SJeff Garzik 
1521c6fd2807SJeff Garzik 	/* is it NCQ device error? */
15220260731fSTejun Heo 	if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1523c6fd2807SJeff Garzik 		return;
1524c6fd2807SJeff Garzik 
1525c6fd2807SJeff Garzik 	/* has LLDD analyzed already? */
1526c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1527c6fd2807SJeff Garzik 		qc = __ata_qc_from_tag(ap, tag);
1528c6fd2807SJeff Garzik 
1529c6fd2807SJeff Garzik 		if (!(qc->flags & ATA_QCFLAG_FAILED))
1530c6fd2807SJeff Garzik 			continue;
1531c6fd2807SJeff Garzik 
1532c6fd2807SJeff Garzik 		if (qc->err_mask)
1533c6fd2807SJeff Garzik 			return;
1534c6fd2807SJeff Garzik 	}
1535c6fd2807SJeff Garzik 
1536c6fd2807SJeff Garzik 	/* okay, this error is ours */
1537c6fd2807SJeff Garzik 	rc = ata_eh_read_log_10h(dev, &tag, &tf);
1538c6fd2807SJeff Garzik 	if (rc) {
15390260731fSTejun Heo 		ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
1540c6fd2807SJeff Garzik 				"(errno=%d)\n", rc);
1541c6fd2807SJeff Garzik 		return;
1542c6fd2807SJeff Garzik 	}
1543c6fd2807SJeff Garzik 
15440260731fSTejun Heo 	if (!(link->sactive & (1 << tag))) {
15450260731fSTejun Heo 		ata_link_printk(link, KERN_ERR, "log page 10h reported "
1546c6fd2807SJeff Garzik 				"inactive tag %d\n", tag);
1547c6fd2807SJeff Garzik 		return;
1548c6fd2807SJeff Garzik 	}
1549c6fd2807SJeff Garzik 
1550c6fd2807SJeff Garzik 	/* we've got the perpetrator, condemn it */
1551c6fd2807SJeff Garzik 	qc = __ata_qc_from_tag(ap, tag);
1552c6fd2807SJeff Garzik 	memcpy(&qc->result_tf, &tf, sizeof(tf));
1553a6116c9eSMark Lord 	qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
15545335b729STejun Heo 	qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
1555c6fd2807SJeff Garzik 	ehc->i.err_mask &= ~AC_ERR_DEV;
1556c6fd2807SJeff Garzik }
1557c6fd2807SJeff Garzik 
1558c6fd2807SJeff Garzik /**
1559c6fd2807SJeff Garzik  *	ata_eh_analyze_tf - analyze taskfile of a failed qc
1560c6fd2807SJeff Garzik  *	@qc: qc to analyze
1561c6fd2807SJeff Garzik  *	@tf: Taskfile registers to analyze
1562c6fd2807SJeff Garzik  *
1563c6fd2807SJeff Garzik  *	Analyze taskfile of @qc and further determine cause of
1564c6fd2807SJeff Garzik  *	failure.  This function also requests ATAPI sense data if
1565c6fd2807SJeff Garzik  *	avaliable.
1566c6fd2807SJeff Garzik  *
1567c6fd2807SJeff Garzik  *	LOCKING:
1568c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1569c6fd2807SJeff Garzik  *
1570c6fd2807SJeff Garzik  *	RETURNS:
1571c6fd2807SJeff Garzik  *	Determined recovery action
1572c6fd2807SJeff Garzik  */
1573c6fd2807SJeff Garzik static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1574c6fd2807SJeff Garzik 				      const struct ata_taskfile *tf)
1575c6fd2807SJeff Garzik {
1576c6fd2807SJeff Garzik 	unsigned int tmp, action = 0;
1577c6fd2807SJeff Garzik 	u8 stat = tf->command, err = tf->feature;
1578c6fd2807SJeff Garzik 
1579c6fd2807SJeff Garzik 	if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1580c6fd2807SJeff Garzik 		qc->err_mask |= AC_ERR_HSM;
1581cf480626STejun Heo 		return ATA_EH_RESET;
1582c6fd2807SJeff Garzik 	}
1583c6fd2807SJeff Garzik 
1584a51d644aSTejun Heo 	if (stat & (ATA_ERR | ATA_DF))
1585a51d644aSTejun Heo 		qc->err_mask |= AC_ERR_DEV;
1586a51d644aSTejun Heo 	else
1587c6fd2807SJeff Garzik 		return 0;
1588c6fd2807SJeff Garzik 
1589c6fd2807SJeff Garzik 	switch (qc->dev->class) {
1590c6fd2807SJeff Garzik 	case ATA_DEV_ATA:
1591c6fd2807SJeff Garzik 		if (err & ATA_ICRC)
1592c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_ATA_BUS;
1593c6fd2807SJeff Garzik 		if (err & ATA_UNC)
1594c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_MEDIA;
1595c6fd2807SJeff Garzik 		if (err & ATA_IDNF)
1596c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_INVALID;
1597c6fd2807SJeff Garzik 		break;
1598c6fd2807SJeff Garzik 
1599c6fd2807SJeff Garzik 	case ATA_DEV_ATAPI:
1600a569a30dSTejun Heo 		if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
16013eabddb8STejun Heo 			tmp = atapi_eh_request_sense(qc->dev,
16023eabddb8STejun Heo 						qc->scsicmd->sense_buffer,
16033eabddb8STejun Heo 						qc->result_tf.feature >> 4);
1604c6fd2807SJeff Garzik 			if (!tmp) {
1605a569a30dSTejun Heo 				/* ATA_QCFLAG_SENSE_VALID is used to
1606a569a30dSTejun Heo 				 * tell atapi_qc_complete() that sense
1607a569a30dSTejun Heo 				 * data is already valid.
1608c6fd2807SJeff Garzik 				 *
1609c6fd2807SJeff Garzik 				 * TODO: interpret sense data and set
1610c6fd2807SJeff Garzik 				 * appropriate err_mask.
1611c6fd2807SJeff Garzik 				 */
1612c6fd2807SJeff Garzik 				qc->flags |= ATA_QCFLAG_SENSE_VALID;
1613c6fd2807SJeff Garzik 			} else
1614c6fd2807SJeff Garzik 				qc->err_mask |= tmp;
1615c6fd2807SJeff Garzik 		}
1616a569a30dSTejun Heo 	}
1617c6fd2807SJeff Garzik 
1618c6fd2807SJeff Garzik 	if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1619cf480626STejun Heo 		action |= ATA_EH_RESET;
1620c6fd2807SJeff Garzik 
1621c6fd2807SJeff Garzik 	return action;
1622c6fd2807SJeff Garzik }
1623c6fd2807SJeff Garzik 
162476326ac1STejun Heo static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask,
162576326ac1STejun Heo 				   int *xfer_ok)
1626c6fd2807SJeff Garzik {
162776326ac1STejun Heo 	int base = 0;
162876326ac1STejun Heo 
162976326ac1STejun Heo 	if (!(eflags & ATA_EFLAG_DUBIOUS_XFER))
163076326ac1STejun Heo 		*xfer_ok = 1;
163176326ac1STejun Heo 
163276326ac1STejun Heo 	if (!*xfer_ok)
163375f9cafcSTejun Heo 		base = ATA_ECAT_DUBIOUS_NONE;
163476326ac1STejun Heo 
16357d47e8d4STejun Heo 	if (err_mask & AC_ERR_ATA_BUS)
163676326ac1STejun Heo 		return base + ATA_ECAT_ATA_BUS;
1637c6fd2807SJeff Garzik 
16387d47e8d4STejun Heo 	if (err_mask & AC_ERR_TIMEOUT)
163976326ac1STejun Heo 		return base + ATA_ECAT_TOUT_HSM;
16407d47e8d4STejun Heo 
16413884f7b0STejun Heo 	if (eflags & ATA_EFLAG_IS_IO) {
16427d47e8d4STejun Heo 		if (err_mask & AC_ERR_HSM)
164376326ac1STejun Heo 			return base + ATA_ECAT_TOUT_HSM;
16447d47e8d4STejun Heo 		if ((err_mask &
16457d47e8d4STejun Heo 		     (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
164676326ac1STejun Heo 			return base + ATA_ECAT_UNK_DEV;
1647c6fd2807SJeff Garzik 	}
1648c6fd2807SJeff Garzik 
1649c6fd2807SJeff Garzik 	return 0;
1650c6fd2807SJeff Garzik }
1651c6fd2807SJeff Garzik 
16527d47e8d4STejun Heo struct speed_down_verdict_arg {
1653c6fd2807SJeff Garzik 	u64 since;
165476326ac1STejun Heo 	int xfer_ok;
16553884f7b0STejun Heo 	int nr_errors[ATA_ECAT_NR];
1656c6fd2807SJeff Garzik };
1657c6fd2807SJeff Garzik 
16587d47e8d4STejun Heo static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
1659c6fd2807SJeff Garzik {
16607d47e8d4STejun Heo 	struct speed_down_verdict_arg *arg = void_arg;
166176326ac1STejun Heo 	int cat;
1662c6fd2807SJeff Garzik 
1663c6fd2807SJeff Garzik 	if (ent->timestamp < arg->since)
1664c6fd2807SJeff Garzik 		return -1;
1665c6fd2807SJeff Garzik 
166676326ac1STejun Heo 	cat = ata_eh_categorize_error(ent->eflags, ent->err_mask,
166776326ac1STejun Heo 				      &arg->xfer_ok);
16687d47e8d4STejun Heo 	arg->nr_errors[cat]++;
166976326ac1STejun Heo 
1670c6fd2807SJeff Garzik 	return 0;
1671c6fd2807SJeff Garzik }
1672c6fd2807SJeff Garzik 
1673c6fd2807SJeff Garzik /**
16747d47e8d4STejun Heo  *	ata_eh_speed_down_verdict - Determine speed down verdict
1675c6fd2807SJeff Garzik  *	@dev: Device of interest
1676c6fd2807SJeff Garzik  *
1677c6fd2807SJeff Garzik  *	This function examines error ring of @dev and determines
16787d47e8d4STejun Heo  *	whether NCQ needs to be turned off, transfer speed should be
16797d47e8d4STejun Heo  *	stepped down, or falling back to PIO is necessary.
1680c6fd2807SJeff Garzik  *
16813884f7b0STejun Heo  *	ECAT_ATA_BUS	: ATA_BUS error for any command
1682c6fd2807SJeff Garzik  *
16833884f7b0STejun Heo  *	ECAT_TOUT_HSM	: TIMEOUT for any command or HSM violation for
16843884f7b0STejun Heo  *			  IO commands
16857d47e8d4STejun Heo  *
16863884f7b0STejun Heo  *	ECAT_UNK_DEV	: Unknown DEV error for IO commands
1687c6fd2807SJeff Garzik  *
168876326ac1STejun Heo  *	ECAT_DUBIOUS_*	: Identical to above three but occurred while
168976326ac1STejun Heo  *			  data transfer hasn't been verified.
169076326ac1STejun Heo  *
16913884f7b0STejun Heo  *	Verdicts are
16927d47e8d4STejun Heo  *
16933884f7b0STejun Heo  *	NCQ_OFF		: Turn off NCQ.
16947d47e8d4STejun Heo  *
16953884f7b0STejun Heo  *	SPEED_DOWN	: Speed down transfer speed but don't fall back
16963884f7b0STejun Heo  *			  to PIO.
16973884f7b0STejun Heo  *
16983884f7b0STejun Heo  *	FALLBACK_TO_PIO	: Fall back to PIO.
16993884f7b0STejun Heo  *
17003884f7b0STejun Heo  *	Even if multiple verdicts are returned, only one action is
170176326ac1STejun Heo  *	taken per error.  An action triggered by non-DUBIOUS errors
170276326ac1STejun Heo  *	clears ering, while one triggered by DUBIOUS_* errors doesn't.
170376326ac1STejun Heo  *	This is to expedite speed down decisions right after device is
170476326ac1STejun Heo  *	initially configured.
17053884f7b0STejun Heo  *
170676326ac1STejun Heo  *	The followings are speed down rules.  #1 and #2 deal with
170776326ac1STejun Heo  *	DUBIOUS errors.
170876326ac1STejun Heo  *
170976326ac1STejun Heo  *	1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors
171076326ac1STejun Heo  *	   occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO.
171176326ac1STejun Heo  *
171276326ac1STejun Heo  *	2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors
171376326ac1STejun Heo  *	   occurred during last 5 mins, NCQ_OFF.
171476326ac1STejun Heo  *
171576326ac1STejun Heo  *	3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors
17163884f7b0STejun Heo  *	   ocurred during last 5 mins, FALLBACK_TO_PIO
17173884f7b0STejun Heo  *
171876326ac1STejun Heo  *	4. If more than 3 TOUT_HSM or UNK_DEV errors occurred
17193884f7b0STejun Heo  *	   during last 10 mins, NCQ_OFF.
17203884f7b0STejun Heo  *
172176326ac1STejun Heo  *	5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6
17223884f7b0STejun Heo  *	   UNK_DEV errors occurred during last 10 mins, SPEED_DOWN.
17237d47e8d4STejun Heo  *
1724c6fd2807SJeff Garzik  *	LOCKING:
1725c6fd2807SJeff Garzik  *	Inherited from caller.
1726c6fd2807SJeff Garzik  *
1727c6fd2807SJeff Garzik  *	RETURNS:
17287d47e8d4STejun Heo  *	OR of ATA_EH_SPDN_* flags.
1729c6fd2807SJeff Garzik  */
17307d47e8d4STejun Heo static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
1731c6fd2807SJeff Garzik {
17327d47e8d4STejun Heo 	const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
17337d47e8d4STejun Heo 	u64 j64 = get_jiffies_64();
17347d47e8d4STejun Heo 	struct speed_down_verdict_arg arg;
17357d47e8d4STejun Heo 	unsigned int verdict = 0;
1736c6fd2807SJeff Garzik 
17373884f7b0STejun Heo 	/* scan past 5 mins of error history */
17383884f7b0STejun Heo 	memset(&arg, 0, sizeof(arg));
17393884f7b0STejun Heo 	arg.since = j64 - min(j64, j5mins);
17403884f7b0STejun Heo 	ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
17413884f7b0STejun Heo 
174276326ac1STejun Heo 	if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] +
174376326ac1STejun Heo 	    arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1)
174476326ac1STejun Heo 		verdict |= ATA_EH_SPDN_SPEED_DOWN |
174576326ac1STejun Heo 			ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS;
174676326ac1STejun Heo 
174776326ac1STejun Heo 	if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] +
174876326ac1STejun Heo 	    arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1)
174976326ac1STejun Heo 		verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS;
175076326ac1STejun Heo 
17513884f7b0STejun Heo 	if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
17523884f7b0STejun Heo 	    arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1753663f99b8STejun Heo 	    arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
17543884f7b0STejun Heo 		verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
17553884f7b0STejun Heo 
17567d47e8d4STejun Heo 	/* scan past 10 mins of error history */
1757c6fd2807SJeff Garzik 	memset(&arg, 0, sizeof(arg));
17587d47e8d4STejun Heo 	arg.since = j64 - min(j64, j10mins);
17597d47e8d4STejun Heo 	ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1760c6fd2807SJeff Garzik 
17613884f7b0STejun Heo 	if (arg.nr_errors[ATA_ECAT_TOUT_HSM] +
17623884f7b0STejun Heo 	    arg.nr_errors[ATA_ECAT_UNK_DEV] > 3)
17637d47e8d4STejun Heo 		verdict |= ATA_EH_SPDN_NCQ_OFF;
17643884f7b0STejun Heo 
17653884f7b0STejun Heo 	if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
17663884f7b0STejun Heo 	    arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 ||
1767663f99b8STejun Heo 	    arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
17687d47e8d4STejun Heo 		verdict |= ATA_EH_SPDN_SPEED_DOWN;
1769c6fd2807SJeff Garzik 
17707d47e8d4STejun Heo 	return verdict;
1771c6fd2807SJeff Garzik }
1772c6fd2807SJeff Garzik 
1773c6fd2807SJeff Garzik /**
1774c6fd2807SJeff Garzik  *	ata_eh_speed_down - record error and speed down if necessary
1775c6fd2807SJeff Garzik  *	@dev: Failed device
17763884f7b0STejun Heo  *	@eflags: mask of ATA_EFLAG_* flags
1777c6fd2807SJeff Garzik  *	@err_mask: err_mask of the error
1778c6fd2807SJeff Garzik  *
1779c6fd2807SJeff Garzik  *	Record error and examine error history to determine whether
1780c6fd2807SJeff Garzik  *	adjusting transmission speed is necessary.  It also sets
1781c6fd2807SJeff Garzik  *	transmission limits appropriately if such adjustment is
1782c6fd2807SJeff Garzik  *	necessary.
1783c6fd2807SJeff Garzik  *
1784c6fd2807SJeff Garzik  *	LOCKING:
1785c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1786c6fd2807SJeff Garzik  *
1787c6fd2807SJeff Garzik  *	RETURNS:
17887d47e8d4STejun Heo  *	Determined recovery action.
1789c6fd2807SJeff Garzik  */
17903884f7b0STejun Heo static unsigned int ata_eh_speed_down(struct ata_device *dev,
17913884f7b0STejun Heo 				unsigned int eflags, unsigned int err_mask)
1792c6fd2807SJeff Garzik {
1793b1c72916STejun Heo 	struct ata_link *link = ata_dev_phys_link(dev);
179476326ac1STejun Heo 	int xfer_ok = 0;
17957d47e8d4STejun Heo 	unsigned int verdict;
17967d47e8d4STejun Heo 	unsigned int action = 0;
17977d47e8d4STejun Heo 
17987d47e8d4STejun Heo 	/* don't bother if Cat-0 error */
179976326ac1STejun Heo 	if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0)
1800c6fd2807SJeff Garzik 		return 0;
1801c6fd2807SJeff Garzik 
1802c6fd2807SJeff Garzik 	/* record error and determine whether speed down is necessary */
18033884f7b0STejun Heo 	ata_ering_record(&dev->ering, eflags, err_mask);
18047d47e8d4STejun Heo 	verdict = ata_eh_speed_down_verdict(dev);
1805c6fd2807SJeff Garzik 
18067d47e8d4STejun Heo 	/* turn off NCQ? */
18077d47e8d4STejun Heo 	if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
18087d47e8d4STejun Heo 	    (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
18097d47e8d4STejun Heo 			   ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
18107d47e8d4STejun Heo 		dev->flags |= ATA_DFLAG_NCQ_OFF;
18117d47e8d4STejun Heo 		ata_dev_printk(dev, KERN_WARNING,
18127d47e8d4STejun Heo 			       "NCQ disabled due to excessive errors\n");
18137d47e8d4STejun Heo 		goto done;
18147d47e8d4STejun Heo 	}
1815c6fd2807SJeff Garzik 
18167d47e8d4STejun Heo 	/* speed down? */
18177d47e8d4STejun Heo 	if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1818c6fd2807SJeff Garzik 		/* speed down SATA link speed if possible */
18193884f7b0STejun Heo 		if (sata_down_spd_limit(link) == 0) {
1820cf480626STejun Heo 			action |= ATA_EH_RESET;
18217d47e8d4STejun Heo 			goto done;
18227d47e8d4STejun Heo 		}
1823c6fd2807SJeff Garzik 
1824c6fd2807SJeff Garzik 		/* lower transfer mode */
18257d47e8d4STejun Heo 		if (dev->spdn_cnt < 2) {
18267d47e8d4STejun Heo 			static const int dma_dnxfer_sel[] =
18277d47e8d4STejun Heo 				{ ATA_DNXFER_DMA, ATA_DNXFER_40C };
18287d47e8d4STejun Heo 			static const int pio_dnxfer_sel[] =
18297d47e8d4STejun Heo 				{ ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
18307d47e8d4STejun Heo 			int sel;
1831c6fd2807SJeff Garzik 
18327d47e8d4STejun Heo 			if (dev->xfer_shift != ATA_SHIFT_PIO)
18337d47e8d4STejun Heo 				sel = dma_dnxfer_sel[dev->spdn_cnt];
18347d47e8d4STejun Heo 			else
18357d47e8d4STejun Heo 				sel = pio_dnxfer_sel[dev->spdn_cnt];
18367d47e8d4STejun Heo 
18377d47e8d4STejun Heo 			dev->spdn_cnt++;
18387d47e8d4STejun Heo 
18397d47e8d4STejun Heo 			if (ata_down_xfermask_limit(dev, sel) == 0) {
1840cf480626STejun Heo 				action |= ATA_EH_RESET;
18417d47e8d4STejun Heo 				goto done;
18427d47e8d4STejun Heo 			}
18437d47e8d4STejun Heo 		}
18447d47e8d4STejun Heo 	}
18457d47e8d4STejun Heo 
18467d47e8d4STejun Heo 	/* Fall back to PIO?  Slowing down to PIO is meaningless for
1847663f99b8STejun Heo 	 * SATA ATA devices.  Consider it only for PATA and SATAPI.
18487d47e8d4STejun Heo 	 */
18497d47e8d4STejun Heo 	if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
1850663f99b8STejun Heo 	    (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) &&
18517d47e8d4STejun Heo 	    (dev->xfer_shift != ATA_SHIFT_PIO)) {
18527d47e8d4STejun Heo 		if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
18537d47e8d4STejun Heo 			dev->spdn_cnt = 0;
1854cf480626STejun Heo 			action |= ATA_EH_RESET;
18557d47e8d4STejun Heo 			goto done;
18567d47e8d4STejun Heo 		}
18577d47e8d4STejun Heo 	}
18587d47e8d4STejun Heo 
1859c6fd2807SJeff Garzik 	return 0;
18607d47e8d4STejun Heo  done:
18617d47e8d4STejun Heo 	/* device has been slowed down, blow error history */
186276326ac1STejun Heo 	if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS))
18637d47e8d4STejun Heo 		ata_ering_clear(&dev->ering);
18647d47e8d4STejun Heo 	return action;
1865c6fd2807SJeff Garzik }
1866c6fd2807SJeff Garzik 
1867c6fd2807SJeff Garzik /**
18689b1e2658STejun Heo  *	ata_eh_link_autopsy - analyze error and determine recovery action
18699b1e2658STejun Heo  *	@link: host link to perform autopsy on
1870c6fd2807SJeff Garzik  *
18710260731fSTejun Heo  *	Analyze why @link failed and determine which recovery actions
18720260731fSTejun Heo  *	are needed.  This function also sets more detailed AC_ERR_*
18730260731fSTejun Heo  *	values and fills sense data for ATAPI CHECK SENSE.
1874c6fd2807SJeff Garzik  *
1875c6fd2807SJeff Garzik  *	LOCKING:
1876c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1877c6fd2807SJeff Garzik  */
18789b1e2658STejun Heo static void ata_eh_link_autopsy(struct ata_link *link)
1879c6fd2807SJeff Garzik {
18800260731fSTejun Heo 	struct ata_port *ap = link->ap;
1881936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
1882dfcc173dSTejun Heo 	struct ata_device *dev;
18833884f7b0STejun Heo 	unsigned int all_err_mask = 0, eflags = 0;
18843884f7b0STejun Heo 	int tag;
1885c6fd2807SJeff Garzik 	u32 serror;
1886c6fd2807SJeff Garzik 	int rc;
1887c6fd2807SJeff Garzik 
1888c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
1889c6fd2807SJeff Garzik 
1890c6fd2807SJeff Garzik 	if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1891c6fd2807SJeff Garzik 		return;
1892c6fd2807SJeff Garzik 
1893c6fd2807SJeff Garzik 	/* obtain and analyze SError */
1894936fd732STejun Heo 	rc = sata_scr_read(link, SCR_ERROR, &serror);
1895c6fd2807SJeff Garzik 	if (rc == 0) {
1896c6fd2807SJeff Garzik 		ehc->i.serror |= serror;
18970260731fSTejun Heo 		ata_eh_analyze_serror(link);
18984e57c517STejun Heo 	} else if (rc != -EOPNOTSUPP) {
1899cf480626STejun Heo 		/* SError read failed, force reset and probing */
1900b558edddSTejun Heo 		ehc->i.probe_mask |= ATA_ALL_DEVICES;
1901cf480626STejun Heo 		ehc->i.action |= ATA_EH_RESET;
19024e57c517STejun Heo 		ehc->i.err_mask |= AC_ERR_OTHER;
19034e57c517STejun Heo 	}
1904c6fd2807SJeff Garzik 
1905c6fd2807SJeff Garzik 	/* analyze NCQ failure */
19060260731fSTejun Heo 	ata_eh_analyze_ncq_error(link);
1907c6fd2807SJeff Garzik 
1908c6fd2807SJeff Garzik 	/* any real error trumps AC_ERR_OTHER */
1909c6fd2807SJeff Garzik 	if (ehc->i.err_mask & ~AC_ERR_OTHER)
1910c6fd2807SJeff Garzik 		ehc->i.err_mask &= ~AC_ERR_OTHER;
1911c6fd2807SJeff Garzik 
1912c6fd2807SJeff Garzik 	all_err_mask |= ehc->i.err_mask;
1913c6fd2807SJeff Garzik 
1914c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1915c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1916c6fd2807SJeff Garzik 
1917b1c72916STejun Heo 		if (!(qc->flags & ATA_QCFLAG_FAILED) ||
1918b1c72916STejun Heo 		    ata_dev_phys_link(qc->dev) != link)
1919c6fd2807SJeff Garzik 			continue;
1920c6fd2807SJeff Garzik 
1921c6fd2807SJeff Garzik 		/* inherit upper level err_mask */
1922c6fd2807SJeff Garzik 		qc->err_mask |= ehc->i.err_mask;
1923c6fd2807SJeff Garzik 
1924c6fd2807SJeff Garzik 		/* analyze TF */
1925c6fd2807SJeff Garzik 		ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1926c6fd2807SJeff Garzik 
1927c6fd2807SJeff Garzik 		/* DEV errors are probably spurious in case of ATA_BUS error */
1928c6fd2807SJeff Garzik 		if (qc->err_mask & AC_ERR_ATA_BUS)
1929c6fd2807SJeff Garzik 			qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1930c6fd2807SJeff Garzik 					  AC_ERR_INVALID);
1931c6fd2807SJeff Garzik 
1932c6fd2807SJeff Garzik 		/* any real error trumps unknown error */
1933c6fd2807SJeff Garzik 		if (qc->err_mask & ~AC_ERR_OTHER)
1934c6fd2807SJeff Garzik 			qc->err_mask &= ~AC_ERR_OTHER;
1935c6fd2807SJeff Garzik 
1936c6fd2807SJeff Garzik 		/* SENSE_VALID trumps dev/unknown error and revalidation */
1937f90f0828STejun Heo 		if (qc->flags & ATA_QCFLAG_SENSE_VALID)
1938c6fd2807SJeff Garzik 			qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1939c6fd2807SJeff Garzik 
194003faab78STejun Heo 		/* determine whether the command is worth retrying */
194103faab78STejun Heo 		if (!(qc->err_mask & AC_ERR_INVALID) &&
194203faab78STejun Heo 		    ((qc->flags & ATA_QCFLAG_IO) || qc->err_mask != AC_ERR_DEV))
194303faab78STejun Heo 			qc->flags |= ATA_QCFLAG_RETRY;
194403faab78STejun Heo 
1945c6fd2807SJeff Garzik 		/* accumulate error info */
1946c6fd2807SJeff Garzik 		ehc->i.dev = qc->dev;
1947c6fd2807SJeff Garzik 		all_err_mask |= qc->err_mask;
1948c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_IO)
19493884f7b0STejun Heo 			eflags |= ATA_EFLAG_IS_IO;
1950c6fd2807SJeff Garzik 	}
1951c6fd2807SJeff Garzik 
1952c6fd2807SJeff Garzik 	/* enforce default EH actions */
1953c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN ||
1954c6fd2807SJeff Garzik 	    all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
1955cf480626STejun Heo 		ehc->i.action |= ATA_EH_RESET;
19563884f7b0STejun Heo 	else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) ||
19573884f7b0STejun Heo 		 (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV)))
1958c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_REVALIDATE;
1959c6fd2807SJeff Garzik 
1960dfcc173dSTejun Heo 	/* If we have offending qcs and the associated failed device,
1961dfcc173dSTejun Heo 	 * perform per-dev EH action only on the offending device.
1962dfcc173dSTejun Heo 	 */
1963c6fd2807SJeff Garzik 	if (ehc->i.dev) {
1964c6fd2807SJeff Garzik 		ehc->i.dev_action[ehc->i.dev->devno] |=
1965c6fd2807SJeff Garzik 			ehc->i.action & ATA_EH_PERDEV_MASK;
1966c6fd2807SJeff Garzik 		ehc->i.action &= ~ATA_EH_PERDEV_MASK;
1967c6fd2807SJeff Garzik 	}
1968c6fd2807SJeff Garzik 
19692695e366STejun Heo 	/* propagate timeout to host link */
19702695e366STejun Heo 	if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link))
19712695e366STejun Heo 		ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT;
19722695e366STejun Heo 
19732695e366STejun Heo 	/* record error and consider speeding down */
1974dfcc173dSTejun Heo 	dev = ehc->i.dev;
19752695e366STejun Heo 	if (!dev && ((ata_link_max_devices(link) == 1 &&
19762695e366STejun Heo 		      ata_dev_enabled(link->device))))
1977dfcc173dSTejun Heo 	    dev = link->device;
1978dfcc173dSTejun Heo 
197976326ac1STejun Heo 	if (dev) {
198076326ac1STejun Heo 		if (dev->flags & ATA_DFLAG_DUBIOUS_XFER)
198176326ac1STejun Heo 			eflags |= ATA_EFLAG_DUBIOUS_XFER;
19823884f7b0STejun Heo 		ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask);
198376326ac1STejun Heo 	}
1984dfcc173dSTejun Heo 
1985c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
1986c6fd2807SJeff Garzik }
1987c6fd2807SJeff Garzik 
1988c6fd2807SJeff Garzik /**
19899b1e2658STejun Heo  *	ata_eh_autopsy - analyze error and determine recovery action
19909b1e2658STejun Heo  *	@ap: host port to perform autopsy on
19919b1e2658STejun Heo  *
19929b1e2658STejun Heo  *	Analyze all links of @ap and determine why they failed and
19939b1e2658STejun Heo  *	which recovery actions are needed.
19949b1e2658STejun Heo  *
19959b1e2658STejun Heo  *	LOCKING:
19969b1e2658STejun Heo  *	Kernel thread context (may sleep).
19979b1e2658STejun Heo  */
1998fb7fd614STejun Heo void ata_eh_autopsy(struct ata_port *ap)
19999b1e2658STejun Heo {
20009b1e2658STejun Heo 	struct ata_link *link;
20019b1e2658STejun Heo 
20022695e366STejun Heo 	ata_port_for_each_link(link, ap)
20039b1e2658STejun Heo 		ata_eh_link_autopsy(link);
20042695e366STejun Heo 
2005b1c72916STejun Heo 	/* Handle the frigging slave link.  Autopsy is done similarly
2006b1c72916STejun Heo 	 * but actions and flags are transferred over to the master
2007b1c72916STejun Heo 	 * link and handled from there.
2008b1c72916STejun Heo 	 */
2009b1c72916STejun Heo 	if (ap->slave_link) {
2010b1c72916STejun Heo 		struct ata_eh_context *mehc = &ap->link.eh_context;
2011b1c72916STejun Heo 		struct ata_eh_context *sehc = &ap->slave_link->eh_context;
2012b1c72916STejun Heo 
2013*848e4c68STejun Heo 		/* transfer control flags from master to slave */
2014*848e4c68STejun Heo 		sehc->i.flags |= mehc->i.flags & ATA_EHI_TO_SLAVE_MASK;
2015*848e4c68STejun Heo 
2016*848e4c68STejun Heo 		/* perform autopsy on the slave link */
2017b1c72916STejun Heo 		ata_eh_link_autopsy(ap->slave_link);
2018b1c72916STejun Heo 
2019*848e4c68STejun Heo 		/* transfer actions from slave to master and clear slave */
2020b1c72916STejun Heo 		ata_eh_about_to_do(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
2021b1c72916STejun Heo 		mehc->i.action		|= sehc->i.action;
2022b1c72916STejun Heo 		mehc->i.dev_action[1]	|= sehc->i.dev_action[1];
2023b1c72916STejun Heo 		mehc->i.flags		|= sehc->i.flags;
2024b1c72916STejun Heo 		ata_eh_done(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
2025b1c72916STejun Heo 	}
2026b1c72916STejun Heo 
20272695e366STejun Heo 	/* Autopsy of fanout ports can affect host link autopsy.
20282695e366STejun Heo 	 * Perform host link autopsy last.
20292695e366STejun Heo 	 */
2030071f44b1STejun Heo 	if (sata_pmp_attached(ap))
20312695e366STejun Heo 		ata_eh_link_autopsy(&ap->link);
20329b1e2658STejun Heo }
20339b1e2658STejun Heo 
20349b1e2658STejun Heo /**
20359b1e2658STejun Heo  *	ata_eh_link_report - report error handling to user
20360260731fSTejun Heo  *	@link: ATA link EH is going on
2037c6fd2807SJeff Garzik  *
2038c6fd2807SJeff Garzik  *	Report EH to user.
2039c6fd2807SJeff Garzik  *
2040c6fd2807SJeff Garzik  *	LOCKING:
2041c6fd2807SJeff Garzik  *	None.
2042c6fd2807SJeff Garzik  */
20439b1e2658STejun Heo static void ata_eh_link_report(struct ata_link *link)
2044c6fd2807SJeff Garzik {
20450260731fSTejun Heo 	struct ata_port *ap = link->ap;
20460260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
2047c6fd2807SJeff Garzik 	const char *frozen, *desc;
2048a1e10f7eSTejun Heo 	char tries_buf[6];
2049c6fd2807SJeff Garzik 	int tag, nr_failed = 0;
2050c6fd2807SJeff Garzik 
205194ff3d54STejun Heo 	if (ehc->i.flags & ATA_EHI_QUIET)
205294ff3d54STejun Heo 		return;
205394ff3d54STejun Heo 
2054c6fd2807SJeff Garzik 	desc = NULL;
2055c6fd2807SJeff Garzik 	if (ehc->i.desc[0] != '\0')
2056c6fd2807SJeff Garzik 		desc = ehc->i.desc;
2057c6fd2807SJeff Garzik 
2058c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2059c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2060c6fd2807SJeff Garzik 
2061b1c72916STejun Heo 		if (!(qc->flags & ATA_QCFLAG_FAILED) ||
2062b1c72916STejun Heo 		    ata_dev_phys_link(qc->dev) != link ||
2063e027bd36STejun Heo 		    ((qc->flags & ATA_QCFLAG_QUIET) &&
2064e027bd36STejun Heo 		     qc->err_mask == AC_ERR_DEV))
2065c6fd2807SJeff Garzik 			continue;
2066c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
2067c6fd2807SJeff Garzik 			continue;
2068c6fd2807SJeff Garzik 
2069c6fd2807SJeff Garzik 		nr_failed++;
2070c6fd2807SJeff Garzik 	}
2071c6fd2807SJeff Garzik 
2072c6fd2807SJeff Garzik 	if (!nr_failed && !ehc->i.err_mask)
2073c6fd2807SJeff Garzik 		return;
2074c6fd2807SJeff Garzik 
2075c6fd2807SJeff Garzik 	frozen = "";
2076c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN)
2077c6fd2807SJeff Garzik 		frozen = " frozen";
2078c6fd2807SJeff Garzik 
2079a1e10f7eSTejun Heo 	memset(tries_buf, 0, sizeof(tries_buf));
2080a1e10f7eSTejun Heo 	if (ap->eh_tries < ATA_EH_MAX_TRIES)
2081a1e10f7eSTejun Heo 		snprintf(tries_buf, sizeof(tries_buf) - 1, " t%d",
2082a1e10f7eSTejun Heo 			 ap->eh_tries);
2083a1e10f7eSTejun Heo 
2084c6fd2807SJeff Garzik 	if (ehc->i.dev) {
2085c6fd2807SJeff Garzik 		ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
2086a1e10f7eSTejun Heo 			       "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2087a1e10f7eSTejun Heo 			       ehc->i.err_mask, link->sactive, ehc->i.serror,
2088a1e10f7eSTejun Heo 			       ehc->i.action, frozen, tries_buf);
2089c6fd2807SJeff Garzik 		if (desc)
2090b64bbc39STejun Heo 			ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
2091c6fd2807SJeff Garzik 	} else {
20920260731fSTejun Heo 		ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
2093a1e10f7eSTejun Heo 				"SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2094a1e10f7eSTejun Heo 				ehc->i.err_mask, link->sactive, ehc->i.serror,
2095a1e10f7eSTejun Heo 				ehc->i.action, frozen, tries_buf);
2096c6fd2807SJeff Garzik 		if (desc)
20970260731fSTejun Heo 			ata_link_printk(link, KERN_ERR, "%s\n", desc);
2098c6fd2807SJeff Garzik 	}
2099c6fd2807SJeff Garzik 
21001333e194SRobert Hancock 	if (ehc->i.serror)
2101da0e21d3STejun Heo 		ata_link_printk(link, KERN_ERR,
21021333e194SRobert Hancock 		  "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
21031333e194SRobert Hancock 		  ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
21041333e194SRobert Hancock 		  ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
21051333e194SRobert Hancock 		  ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
21061333e194SRobert Hancock 		  ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
21071333e194SRobert Hancock 		  ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
21081333e194SRobert Hancock 		  ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
21091333e194SRobert Hancock 		  ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
21101333e194SRobert Hancock 		  ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
21111333e194SRobert Hancock 		  ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
21121333e194SRobert Hancock 		  ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
21131333e194SRobert Hancock 		  ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
21141333e194SRobert Hancock 		  ehc->i.serror & SERR_CRC ? "BadCRC " : "",
21151333e194SRobert Hancock 		  ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
21161333e194SRobert Hancock 		  ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
21171333e194SRobert Hancock 		  ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
21181333e194SRobert Hancock 		  ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
21191333e194SRobert Hancock 		  ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
21201333e194SRobert Hancock 
2121c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2122c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
21238a937581STejun Heo 		struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
2124abb6a889STejun Heo 		const u8 *cdb = qc->cdb;
2125abb6a889STejun Heo 		char data_buf[20] = "";
2126abb6a889STejun Heo 		char cdb_buf[70] = "";
2127c6fd2807SJeff Garzik 
21280260731fSTejun Heo 		if (!(qc->flags & ATA_QCFLAG_FAILED) ||
2129b1c72916STejun Heo 		    ata_dev_phys_link(qc->dev) != link || !qc->err_mask)
2130c6fd2807SJeff Garzik 			continue;
2131c6fd2807SJeff Garzik 
2132abb6a889STejun Heo 		if (qc->dma_dir != DMA_NONE) {
2133abb6a889STejun Heo 			static const char *dma_str[] = {
2134abb6a889STejun Heo 				[DMA_BIDIRECTIONAL]	= "bidi",
2135abb6a889STejun Heo 				[DMA_TO_DEVICE]		= "out",
2136abb6a889STejun Heo 				[DMA_FROM_DEVICE]	= "in",
2137abb6a889STejun Heo 			};
2138abb6a889STejun Heo 			static const char *prot_str[] = {
2139abb6a889STejun Heo 				[ATA_PROT_PIO]		= "pio",
2140abb6a889STejun Heo 				[ATA_PROT_DMA]		= "dma",
2141abb6a889STejun Heo 				[ATA_PROT_NCQ]		= "ncq",
21420dc36888STejun Heo 				[ATAPI_PROT_PIO]	= "pio",
21430dc36888STejun Heo 				[ATAPI_PROT_DMA]	= "dma",
2144abb6a889STejun Heo 			};
2145abb6a889STejun Heo 
2146abb6a889STejun Heo 			snprintf(data_buf, sizeof(data_buf), " %s %u %s",
2147abb6a889STejun Heo 				 prot_str[qc->tf.protocol], qc->nbytes,
2148abb6a889STejun Heo 				 dma_str[qc->dma_dir]);
2149abb6a889STejun Heo 		}
2150abb6a889STejun Heo 
2151e39eec13SJeff Garzik 		if (ata_is_atapi(qc->tf.protocol))
2152abb6a889STejun Heo 			snprintf(cdb_buf, sizeof(cdb_buf),
2153abb6a889STejun Heo 				 "cdb %02x %02x %02x %02x %02x %02x %02x %02x  "
2154abb6a889STejun Heo 				 "%02x %02x %02x %02x %02x %02x %02x %02x\n         ",
2155abb6a889STejun Heo 				 cdb[0], cdb[1], cdb[2], cdb[3],
2156abb6a889STejun Heo 				 cdb[4], cdb[5], cdb[6], cdb[7],
2157abb6a889STejun Heo 				 cdb[8], cdb[9], cdb[10], cdb[11],
2158abb6a889STejun Heo 				 cdb[12], cdb[13], cdb[14], cdb[15]);
2159abb6a889STejun Heo 
21608a937581STejun Heo 		ata_dev_printk(qc->dev, KERN_ERR,
21618a937581STejun Heo 			"cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
2162abb6a889STejun Heo 			"tag %d%s\n         %s"
21638a937581STejun Heo 			"res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
21645335b729STejun Heo 			"Emask 0x%x (%s)%s\n",
21658a937581STejun Heo 			cmd->command, cmd->feature, cmd->nsect,
21668a937581STejun Heo 			cmd->lbal, cmd->lbam, cmd->lbah,
21678a937581STejun Heo 			cmd->hob_feature, cmd->hob_nsect,
21688a937581STejun Heo 			cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
2169abb6a889STejun Heo 			cmd->device, qc->tag, data_buf, cdb_buf,
21708a937581STejun Heo 			res->command, res->feature, res->nsect,
21718a937581STejun Heo 			res->lbal, res->lbam, res->lbah,
21728a937581STejun Heo 			res->hob_feature, res->hob_nsect,
21738a937581STejun Heo 			res->hob_lbal, res->hob_lbam, res->hob_lbah,
21745335b729STejun Heo 			res->device, qc->err_mask, ata_err_string(qc->err_mask),
21755335b729STejun Heo 			qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
21761333e194SRobert Hancock 
21771333e194SRobert Hancock 		if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
21781333e194SRobert Hancock 				    ATA_ERR)) {
21791333e194SRobert Hancock 			if (res->command & ATA_BUSY)
21801333e194SRobert Hancock 				ata_dev_printk(qc->dev, KERN_ERR,
21811333e194SRobert Hancock 				  "status: { Busy }\n");
21821333e194SRobert Hancock 			else
21831333e194SRobert Hancock 				ata_dev_printk(qc->dev, KERN_ERR,
21841333e194SRobert Hancock 				  "status: { %s%s%s%s}\n",
21851333e194SRobert Hancock 				  res->command & ATA_DRDY ? "DRDY " : "",
21861333e194SRobert Hancock 				  res->command & ATA_DF ? "DF " : "",
21871333e194SRobert Hancock 				  res->command & ATA_DRQ ? "DRQ " : "",
21881333e194SRobert Hancock 				  res->command & ATA_ERR ? "ERR " : "");
21891333e194SRobert Hancock 		}
21901333e194SRobert Hancock 
21911333e194SRobert Hancock 		if (cmd->command != ATA_CMD_PACKET &&
21921333e194SRobert Hancock 		    (res->feature & (ATA_ICRC | ATA_UNC | ATA_IDNF |
21931333e194SRobert Hancock 				     ATA_ABORTED)))
21941333e194SRobert Hancock 			ata_dev_printk(qc->dev, KERN_ERR,
21951333e194SRobert Hancock 			  "error: { %s%s%s%s}\n",
21961333e194SRobert Hancock 			  res->feature & ATA_ICRC ? "ICRC " : "",
21971333e194SRobert Hancock 			  res->feature & ATA_UNC ? "UNC " : "",
21981333e194SRobert Hancock 			  res->feature & ATA_IDNF ? "IDNF " : "",
21991333e194SRobert Hancock 			  res->feature & ATA_ABORTED ? "ABRT " : "");
2200c6fd2807SJeff Garzik 	}
2201c6fd2807SJeff Garzik }
2202c6fd2807SJeff Garzik 
22039b1e2658STejun Heo /**
22049b1e2658STejun Heo  *	ata_eh_report - report error handling to user
22059b1e2658STejun Heo  *	@ap: ATA port to report EH about
22069b1e2658STejun Heo  *
22079b1e2658STejun Heo  *	Report EH to user.
22089b1e2658STejun Heo  *
22099b1e2658STejun Heo  *	LOCKING:
22109b1e2658STejun Heo  *	None.
22119b1e2658STejun Heo  */
2212fb7fd614STejun Heo void ata_eh_report(struct ata_port *ap)
22139b1e2658STejun Heo {
22149b1e2658STejun Heo 	struct ata_link *link;
22159b1e2658STejun Heo 
22169b1e2658STejun Heo 	__ata_port_for_each_link(link, ap)
22179b1e2658STejun Heo 		ata_eh_link_report(link);
22189b1e2658STejun Heo }
22199b1e2658STejun Heo 
2220cc0680a5STejun Heo static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
2221b1c72916STejun Heo 			unsigned int *classes, unsigned long deadline,
2222b1c72916STejun Heo 			bool clear_classes)
2223c6fd2807SJeff Garzik {
2224f58229f8STejun Heo 	struct ata_device *dev;
2225c6fd2807SJeff Garzik 
2226b1c72916STejun Heo 	if (clear_classes)
2227cc0680a5STejun Heo 		ata_link_for_each_dev(dev, link)
2228f58229f8STejun Heo 			classes[dev->devno] = ATA_DEV_UNKNOWN;
2229c6fd2807SJeff Garzik 
2230f046519fSTejun Heo 	return reset(link, classes, deadline);
2231c6fd2807SJeff Garzik }
2232c6fd2807SJeff Garzik 
2233ae791c05STejun Heo static int ata_eh_followup_srst_needed(struct ata_link *link,
22345dbfc9cbSTejun Heo 				       int rc, const unsigned int *classes)
2235c6fd2807SJeff Garzik {
223645db2f6cSTejun Heo 	if ((link->flags & ATA_LFLAG_NO_SRST) || ata_link_offline(link))
2237ae791c05STejun Heo 		return 0;
22385dbfc9cbSTejun Heo 	if (rc == -EAGAIN)
2239c6fd2807SJeff Garzik 		return 1;
2240071f44b1STejun Heo 	if (sata_pmp_supported(link->ap) && ata_is_host_link(link))
22413495de73STejun Heo 		return 1;
2242c6fd2807SJeff Garzik 	return 0;
2243c6fd2807SJeff Garzik }
2244c6fd2807SJeff Garzik 
2245fb7fd614STejun Heo int ata_eh_reset(struct ata_link *link, int classify,
2246c6fd2807SJeff Garzik 		 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
2247c6fd2807SJeff Garzik 		 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
2248c6fd2807SJeff Garzik {
2249afaa5c37STejun Heo 	struct ata_port *ap = link->ap;
2250b1c72916STejun Heo 	struct ata_link *slave = ap->slave_link;
2251936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
2252b1c72916STejun Heo 	struct ata_eh_context *sehc = &slave->eh_context;
2253c6fd2807SJeff Garzik 	unsigned int *classes = ehc->classes;
2254416dc9edSTejun Heo 	unsigned int lflags = link->flags;
2255c6fd2807SJeff Garzik 	int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
2256d8af0eb6STejun Heo 	int max_tries = 0, try = 0;
2257b1c72916STejun Heo 	struct ata_link *failed_link;
2258f58229f8STejun Heo 	struct ata_device *dev;
2259416dc9edSTejun Heo 	unsigned long deadline, now;
2260c6fd2807SJeff Garzik 	ata_reset_fn_t reset;
2261afaa5c37STejun Heo 	unsigned long flags;
2262416dc9edSTejun Heo 	u32 sstatus;
2263b1c72916STejun Heo 	int nr_unknown, rc;
2264c6fd2807SJeff Garzik 
2265932648b0STejun Heo 	/*
2266932648b0STejun Heo 	 * Prepare to reset
2267932648b0STejun Heo 	 */
2268d8af0eb6STejun Heo 	while (ata_eh_reset_timeouts[max_tries] != ULONG_MAX)
2269d8af0eb6STejun Heo 		max_tries++;
227005944bdfSTejun Heo 	if (link->flags & ATA_LFLAG_NO_HRST)
227105944bdfSTejun Heo 		hardreset = NULL;
227205944bdfSTejun Heo 	if (link->flags & ATA_LFLAG_NO_SRST)
227305944bdfSTejun Heo 		softreset = NULL;
2274d8af0eb6STejun Heo 
22750a2c0f56STejun Heo 	now = jiffies;
22760a2c0f56STejun Heo 	deadline = ata_deadline(ehc->last_reset, ATA_EH_RESET_COOL_DOWN);
22770a2c0f56STejun Heo 	if (time_before(now, deadline))
22780a2c0f56STejun Heo 		schedule_timeout_uninterruptible(deadline - now);
22790a2c0f56STejun Heo 
2280afaa5c37STejun Heo 	spin_lock_irqsave(ap->lock, flags);
2281afaa5c37STejun Heo 	ap->pflags |= ATA_PFLAG_RESETTING;
2282afaa5c37STejun Heo 	spin_unlock_irqrestore(ap->lock, flags);
2283afaa5c37STejun Heo 
2284cf480626STejun Heo 	ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
22850a2c0f56STejun Heo 	ehc->last_reset = jiffies;
2286c6fd2807SJeff Garzik 
2287cdeab114STejun Heo 	ata_link_for_each_dev(dev, link) {
2288cdeab114STejun Heo 		/* If we issue an SRST then an ATA drive (not ATAPI)
2289cdeab114STejun Heo 		 * may change configuration and be in PIO0 timing. If
2290cdeab114STejun Heo 		 * we do a hard reset (or are coming from power on)
2291cdeab114STejun Heo 		 * this is true for ATA or ATAPI. Until we've set a
2292cdeab114STejun Heo 		 * suitable controller mode we should not touch the
2293cdeab114STejun Heo 		 * bus as we may be talking too fast.
2294cdeab114STejun Heo 		 */
2295cdeab114STejun Heo 		dev->pio_mode = XFER_PIO_0;
2296cdeab114STejun Heo 
2297cdeab114STejun Heo 		/* If the controller has a pio mode setup function
2298cdeab114STejun Heo 		 * then use it to set the chipset to rights. Don't
2299cdeab114STejun Heo 		 * touch the DMA setup as that will be dealt with when
2300cdeab114STejun Heo 		 * configuring devices.
2301cdeab114STejun Heo 		 */
2302cdeab114STejun Heo 		if (ap->ops->set_piomode)
2303cdeab114STejun Heo 			ap->ops->set_piomode(ap, dev);
2304cdeab114STejun Heo 	}
2305cdeab114STejun Heo 
2306cf480626STejun Heo 	/* prefer hardreset */
2307932648b0STejun Heo 	reset = NULL;
2308cf480626STejun Heo 	ehc->i.action &= ~ATA_EH_RESET;
2309cf480626STejun Heo 	if (hardreset) {
2310cf480626STejun Heo 		reset = hardreset;
2311a674050eSTejun Heo 		ehc->i.action |= ATA_EH_HARDRESET;
23124f7faa3fSTejun Heo 	} else if (softreset) {
2313cf480626STejun Heo 		reset = softreset;
2314a674050eSTejun Heo 		ehc->i.action |= ATA_EH_SOFTRESET;
2315cf480626STejun Heo 	}
2316c6fd2807SJeff Garzik 
2317c6fd2807SJeff Garzik 	if (prereset) {
2318b1c72916STejun Heo 		unsigned long deadline = ata_deadline(jiffies,
2319b1c72916STejun Heo 						      ATA_EH_PRERESET_TIMEOUT);
2320b1c72916STejun Heo 
2321b1c72916STejun Heo 		if (slave) {
2322b1c72916STejun Heo 			sehc->i.action &= ~ATA_EH_RESET;
2323b1c72916STejun Heo 			sehc->i.action |= ehc->i.action;
2324b1c72916STejun Heo 		}
2325b1c72916STejun Heo 
2326b1c72916STejun Heo 		rc = prereset(link, deadline);
2327b1c72916STejun Heo 
2328b1c72916STejun Heo 		/* If present, do prereset on slave link too.  Reset
2329b1c72916STejun Heo 		 * is skipped iff both master and slave links report
2330b1c72916STejun Heo 		 * -ENOENT or clear ATA_EH_RESET.
2331b1c72916STejun Heo 		 */
2332b1c72916STejun Heo 		if (slave && (rc == 0 || rc == -ENOENT)) {
2333b1c72916STejun Heo 			int tmp;
2334b1c72916STejun Heo 
2335b1c72916STejun Heo 			tmp = prereset(slave, deadline);
2336b1c72916STejun Heo 			if (tmp != -ENOENT)
2337b1c72916STejun Heo 				rc = tmp;
2338b1c72916STejun Heo 
2339b1c72916STejun Heo 			ehc->i.action |= sehc->i.action;
2340b1c72916STejun Heo 		}
2341b1c72916STejun Heo 
2342c6fd2807SJeff Garzik 		if (rc) {
2343c961922bSAlan Cox 			if (rc == -ENOENT) {
2344cc0680a5STejun Heo 				ata_link_printk(link, KERN_DEBUG,
23454aa9ab67STejun Heo 						"port disabled. ignoring.\n");
2346cf480626STejun Heo 				ehc->i.action &= ~ATA_EH_RESET;
23474aa9ab67STejun Heo 
2348936fd732STejun Heo 				ata_link_for_each_dev(dev, link)
2349f58229f8STejun Heo 					classes[dev->devno] = ATA_DEV_NONE;
23504aa9ab67STejun Heo 
23514aa9ab67STejun Heo 				rc = 0;
2352c961922bSAlan Cox 			} else
2353cc0680a5STejun Heo 				ata_link_printk(link, KERN_ERR,
2354c6fd2807SJeff Garzik 					"prereset failed (errno=%d)\n", rc);
2355fccb6ea5STejun Heo 			goto out;
2356c6fd2807SJeff Garzik 		}
2357c6fd2807SJeff Garzik 
2358932648b0STejun Heo 		/* prereset() might have cleared ATA_EH_RESET.  If so,
2359932648b0STejun Heo 		 * bang classes and return.
2360932648b0STejun Heo 		 */
2361932648b0STejun Heo 		if (reset && !(ehc->i.action & ATA_EH_RESET)) {
2362936fd732STejun Heo 			ata_link_for_each_dev(dev, link)
2363f58229f8STejun Heo 				classes[dev->devno] = ATA_DEV_NONE;
2364fccb6ea5STejun Heo 			rc = 0;
2365fccb6ea5STejun Heo 			goto out;
2366c6fd2807SJeff Garzik 		}
2367932648b0STejun Heo 	}
2368c6fd2807SJeff Garzik 
2369c6fd2807SJeff Garzik  retry:
2370932648b0STejun Heo 	/*
2371932648b0STejun Heo 	 * Perform reset
2372932648b0STejun Heo 	 */
23730a2c0f56STejun Heo 	ehc->last_reset = jiffies;
2374dc98c32cSTejun Heo 	if (ata_is_host_link(link))
2375dc98c32cSTejun Heo 		ata_eh_freeze_port(ap);
2376dc98c32cSTejun Heo 
2377341c2c95STejun Heo 	deadline = ata_deadline(jiffies, ata_eh_reset_timeouts[try++]);
237831daabdaSTejun Heo 
2379932648b0STejun Heo 	if (reset) {
2380c6fd2807SJeff Garzik 		if (verbose)
2381cc0680a5STejun Heo 			ata_link_printk(link, KERN_INFO, "%s resetting link\n",
2382c6fd2807SJeff Garzik 					reset == softreset ? "soft" : "hard");
2383c6fd2807SJeff Garzik 
2384c6fd2807SJeff Garzik 		/* mark that this EH session started with reset */
23850d64a233STejun Heo 		if (reset == hardreset)
23860d64a233STejun Heo 			ehc->i.flags |= ATA_EHI_DID_HARDRESET;
23870d64a233STejun Heo 		else
23880d64a233STejun Heo 			ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
2389c6fd2807SJeff Garzik 
2390b1c72916STejun Heo 		rc = ata_do_reset(link, reset, classes, deadline, true);
2391b1c72916STejun Heo 		if (rc && rc != -EAGAIN) {
2392b1c72916STejun Heo 			failed_link = link;
23935dbfc9cbSTejun Heo 			goto fail;
2394b1c72916STejun Heo 		}
2395c6fd2807SJeff Garzik 
2396b1c72916STejun Heo 		/* hardreset slave link if existent */
2397b1c72916STejun Heo 		if (slave && reset == hardreset) {
2398b1c72916STejun Heo 			int tmp;
2399b1c72916STejun Heo 
2400b1c72916STejun Heo 			if (verbose)
2401b1c72916STejun Heo 				ata_link_printk(slave, KERN_INFO,
2402b1c72916STejun Heo 						"hard resetting link\n");
2403b1c72916STejun Heo 
2404b1c72916STejun Heo 			ata_eh_about_to_do(slave, NULL, ATA_EH_RESET);
2405b1c72916STejun Heo 			tmp = ata_do_reset(slave, reset, classes, deadline,
2406b1c72916STejun Heo 					   false);
2407b1c72916STejun Heo 			switch (tmp) {
2408b1c72916STejun Heo 			case -EAGAIN:
2409b1c72916STejun Heo 				rc = -EAGAIN;
2410b1c72916STejun Heo 			case 0:
2411b1c72916STejun Heo 				break;
2412b1c72916STejun Heo 			default:
2413b1c72916STejun Heo 				failed_link = slave;
2414b1c72916STejun Heo 				rc = tmp;
2415b1c72916STejun Heo 				goto fail;
2416b1c72916STejun Heo 			}
2417b1c72916STejun Heo 		}
2418b1c72916STejun Heo 
2419b1c72916STejun Heo 		/* perform follow-up SRST if necessary */
2420c6fd2807SJeff Garzik 		if (reset == hardreset &&
24215dbfc9cbSTejun Heo 		    ata_eh_followup_srst_needed(link, rc, classes)) {
2422c6fd2807SJeff Garzik 			reset = softreset;
2423c6fd2807SJeff Garzik 
2424c6fd2807SJeff Garzik 			if (!reset) {
2425cc0680a5STejun Heo 				ata_link_printk(link, KERN_ERR,
2426c6fd2807SJeff Garzik 						"follow-up softreset required "
2427c6fd2807SJeff Garzik 						"but no softreset avaliable\n");
2428b1c72916STejun Heo 				failed_link = link;
2429fccb6ea5STejun Heo 				rc = -EINVAL;
243008cf69d0STejun Heo 				goto fail;
2431c6fd2807SJeff Garzik 			}
2432c6fd2807SJeff Garzik 
2433cf480626STejun Heo 			ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
2434b1c72916STejun Heo 			rc = ata_do_reset(link, reset, classes, deadline, true);
2435c6fd2807SJeff Garzik 		}
2436932648b0STejun Heo 	} else {
2437932648b0STejun Heo 		if (verbose)
2438932648b0STejun Heo 			ata_link_printk(link, KERN_INFO, "no reset method "
2439932648b0STejun Heo 					"available, skipping reset\n");
2440932648b0STejun Heo 		if (!(lflags & ATA_LFLAG_ASSUME_CLASS))
2441932648b0STejun Heo 			lflags |= ATA_LFLAG_ASSUME_ATA;
2442932648b0STejun Heo 	}
2443008a7896STejun Heo 
2444932648b0STejun Heo 	/*
2445932648b0STejun Heo 	 * Post-reset processing
2446932648b0STejun Heo 	 */
2447ae791c05STejun Heo 	ata_link_for_each_dev(dev, link) {
2448416dc9edSTejun Heo 		/* After the reset, the device state is PIO 0 and the
2449416dc9edSTejun Heo 		 * controller state is undefined.  Reset also wakes up
2450416dc9edSTejun Heo 		 * drives from sleeping mode.
2451c6fd2807SJeff Garzik 		 */
2452f58229f8STejun Heo 		dev->pio_mode = XFER_PIO_0;
2453054a5fbaSTejun Heo 		dev->flags &= ~ATA_DFLAG_SLEEPING;
2454c6fd2807SJeff Garzik 
2455b1c72916STejun Heo 		if (ata_phys_link_offline(ata_dev_phys_link(dev)))
2456ae791c05STejun Heo 			continue;
2457ae791c05STejun Heo 
24584ccd3329STejun Heo 		/* apply class override */
2459416dc9edSTejun Heo 		if (lflags & ATA_LFLAG_ASSUME_ATA)
2460ae791c05STejun Heo 			classes[dev->devno] = ATA_DEV_ATA;
2461416dc9edSTejun Heo 		else if (lflags & ATA_LFLAG_ASSUME_SEMB)
2462ae791c05STejun Heo 			classes[dev->devno] = ATA_DEV_SEMB_UNSUP; /* not yet */
2463ae791c05STejun Heo 	}
2464ae791c05STejun Heo 
2465008a7896STejun Heo 	/* record current link speed */
2466936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2467936fd732STejun Heo 		link->sata_spd = (sstatus >> 4) & 0xf;
2468b1c72916STejun Heo 	if (slave && sata_scr_read(slave, SCR_STATUS, &sstatus) == 0)
2469b1c72916STejun Heo 		slave->sata_spd = (sstatus >> 4) & 0xf;
2470008a7896STejun Heo 
2471dc98c32cSTejun Heo 	/* thaw the port */
2472dc98c32cSTejun Heo 	if (ata_is_host_link(link))
2473dc98c32cSTejun Heo 		ata_eh_thaw_port(ap);
2474dc98c32cSTejun Heo 
2475f046519fSTejun Heo 	/* postreset() should clear hardware SError.  Although SError
2476f046519fSTejun Heo 	 * is cleared during link resume, clearing SError here is
2477f046519fSTejun Heo 	 * necessary as some PHYs raise hotplug events after SRST.
2478f046519fSTejun Heo 	 * This introduces race condition where hotplug occurs between
2479f046519fSTejun Heo 	 * reset and here.  This race is mediated by cross checking
2480f046519fSTejun Heo 	 * link onlineness and classification result later.
2481f046519fSTejun Heo 	 */
2482b1c72916STejun Heo 	if (postreset) {
2483cc0680a5STejun Heo 		postreset(link, classes);
2484b1c72916STejun Heo 		if (slave)
2485b1c72916STejun Heo 			postreset(slave, classes);
2486b1c72916STejun Heo 	}
2487c6fd2807SJeff Garzik 
2488f046519fSTejun Heo 	/* clear cached SError */
2489f046519fSTejun Heo 	spin_lock_irqsave(link->ap->lock, flags);
2490f046519fSTejun Heo 	link->eh_info.serror = 0;
2491b1c72916STejun Heo 	if (slave)
2492b1c72916STejun Heo 		slave->eh_info.serror = 0;
2493f046519fSTejun Heo 	spin_unlock_irqrestore(link->ap->lock, flags);
2494f046519fSTejun Heo 
2495f046519fSTejun Heo 	/* Make sure onlineness and classification result correspond.
2496f046519fSTejun Heo 	 * Hotplug could have happened during reset and some
2497f046519fSTejun Heo 	 * controllers fail to wait while a drive is spinning up after
2498f046519fSTejun Heo 	 * being hotplugged causing misdetection.  By cross checking
2499f046519fSTejun Heo 	 * link onlineness and classification result, those conditions
2500f046519fSTejun Heo 	 * can be reliably detected and retried.
2501f046519fSTejun Heo 	 */
2502b1c72916STejun Heo 	nr_unknown = 0;
2503f046519fSTejun Heo 	ata_link_for_each_dev(dev, link) {
2504f046519fSTejun Heo 		/* convert all ATA_DEV_UNKNOWN to ATA_DEV_NONE */
2505b1c72916STejun Heo 		if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
2506f046519fSTejun Heo 			classes[dev->devno] = ATA_DEV_NONE;
2507b1c72916STejun Heo 			if (ata_phys_link_online(ata_dev_phys_link(dev)))
2508b1c72916STejun Heo 				nr_unknown++;
2509b1c72916STejun Heo 		}
2510f046519fSTejun Heo 	}
2511f046519fSTejun Heo 
2512b1c72916STejun Heo 	if (classify && nr_unknown) {
2513f046519fSTejun Heo 		if (try < max_tries) {
2514f046519fSTejun Heo 			ata_link_printk(link, KERN_WARNING, "link online but "
2515f046519fSTejun Heo 				       "device misclassified, retrying\n");
2516b1c72916STejun Heo 			failed_link = link;
2517f046519fSTejun Heo 			rc = -EAGAIN;
2518f046519fSTejun Heo 			goto fail;
2519f046519fSTejun Heo 		}
2520f046519fSTejun Heo 		ata_link_printk(link, KERN_WARNING,
2521f046519fSTejun Heo 			       "link online but device misclassified, "
2522f046519fSTejun Heo 			       "device detection might fail\n");
2523f046519fSTejun Heo 	}
2524f046519fSTejun Heo 
2525c6fd2807SJeff Garzik 	/* reset successful, schedule revalidation */
2526cf480626STejun Heo 	ata_eh_done(link, NULL, ATA_EH_RESET);
2527b1c72916STejun Heo 	if (slave)
2528b1c72916STejun Heo 		ata_eh_done(slave, NULL, ATA_EH_RESET);
25290a2c0f56STejun Heo 	ehc->last_reset = jiffies;
2530c6fd2807SJeff Garzik 	ehc->i.action |= ATA_EH_REVALIDATE;
2531416dc9edSTejun Heo 
2532416dc9edSTejun Heo 	rc = 0;
2533fccb6ea5STejun Heo  out:
2534fccb6ea5STejun Heo 	/* clear hotplug flag */
2535fccb6ea5STejun Heo 	ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2536b1c72916STejun Heo 	if (slave)
2537b1c72916STejun Heo 		sehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2538afaa5c37STejun Heo 
2539afaa5c37STejun Heo 	spin_lock_irqsave(ap->lock, flags);
2540afaa5c37STejun Heo 	ap->pflags &= ~ATA_PFLAG_RESETTING;
2541afaa5c37STejun Heo 	spin_unlock_irqrestore(ap->lock, flags);
2542afaa5c37STejun Heo 
2543c6fd2807SJeff Garzik 	return rc;
2544416dc9edSTejun Heo 
2545416dc9edSTejun Heo  fail:
25465958e302STejun Heo 	/* if SCR isn't accessible on a fan-out port, PMP needs to be reset */
25475958e302STejun Heo 	if (!ata_is_host_link(link) &&
25485958e302STejun Heo 	    sata_scr_read(link, SCR_STATUS, &sstatus))
25495958e302STejun Heo 		rc = -ERESTART;
25505958e302STejun Heo 
2551416dc9edSTejun Heo 	if (rc == -ERESTART || try >= max_tries)
2552416dc9edSTejun Heo 		goto out;
2553416dc9edSTejun Heo 
2554416dc9edSTejun Heo 	now = jiffies;
2555416dc9edSTejun Heo 	if (time_before(now, deadline)) {
2556416dc9edSTejun Heo 		unsigned long delta = deadline - now;
2557416dc9edSTejun Heo 
2558b1c72916STejun Heo 		ata_link_printk(failed_link, KERN_WARNING,
25590a2c0f56STejun Heo 			"reset failed (errno=%d), retrying in %u secs\n",
25600a2c0f56STejun Heo 			rc, DIV_ROUND_UP(jiffies_to_msecs(delta), 1000));
2561416dc9edSTejun Heo 
2562416dc9edSTejun Heo 		while (delta)
2563416dc9edSTejun Heo 			delta = schedule_timeout_uninterruptible(delta);
2564416dc9edSTejun Heo 	}
2565416dc9edSTejun Heo 
2566b1c72916STejun Heo 	if (try == max_tries - 1) {
2567416dc9edSTejun Heo 		sata_down_spd_limit(link);
2568b1c72916STejun Heo 		if (slave)
2569b1c72916STejun Heo 			sata_down_spd_limit(slave);
2570b1c72916STejun Heo 	} else if (rc == -EPIPE)
2571b1c72916STejun Heo 		sata_down_spd_limit(failed_link);
2572b1c72916STejun Heo 
2573416dc9edSTejun Heo 	if (hardreset)
2574416dc9edSTejun Heo 		reset = hardreset;
2575416dc9edSTejun Heo 	goto retry;
2576c6fd2807SJeff Garzik }
2577c6fd2807SJeff Garzik 
257845fabbb7SElias Oltmanns static inline void ata_eh_pull_park_action(struct ata_port *ap)
257945fabbb7SElias Oltmanns {
258045fabbb7SElias Oltmanns 	struct ata_link *link;
258145fabbb7SElias Oltmanns 	struct ata_device *dev;
258245fabbb7SElias Oltmanns 	unsigned long flags;
258345fabbb7SElias Oltmanns 
258445fabbb7SElias Oltmanns 	/*
258545fabbb7SElias Oltmanns 	 * This function can be thought of as an extended version of
258645fabbb7SElias Oltmanns 	 * ata_eh_about_to_do() specially crafted to accommodate the
258745fabbb7SElias Oltmanns 	 * requirements of ATA_EH_PARK handling. Since the EH thread
258845fabbb7SElias Oltmanns 	 * does not leave the do {} while () loop in ata_eh_recover as
258945fabbb7SElias Oltmanns 	 * long as the timeout for a park request to *one* device on
259045fabbb7SElias Oltmanns 	 * the port has not expired, and since we still want to pick
259145fabbb7SElias Oltmanns 	 * up park requests to other devices on the same port or
259245fabbb7SElias Oltmanns 	 * timeout updates for the same device, we have to pull
259345fabbb7SElias Oltmanns 	 * ATA_EH_PARK actions from eh_info into eh_context.i
259445fabbb7SElias Oltmanns 	 * ourselves at the beginning of each pass over the loop.
259545fabbb7SElias Oltmanns 	 *
259645fabbb7SElias Oltmanns 	 * Additionally, all write accesses to &ap->park_req_pending
259745fabbb7SElias Oltmanns 	 * through INIT_COMPLETION() (see below) or complete_all()
259845fabbb7SElias Oltmanns 	 * (see ata_scsi_park_store()) are protected by the host lock.
259945fabbb7SElias Oltmanns 	 * As a result we have that park_req_pending.done is zero on
260045fabbb7SElias Oltmanns 	 * exit from this function, i.e. when ATA_EH_PARK actions for
260145fabbb7SElias Oltmanns 	 * *all* devices on port ap have been pulled into the
260245fabbb7SElias Oltmanns 	 * respective eh_context structs. If, and only if,
260345fabbb7SElias Oltmanns 	 * park_req_pending.done is non-zero by the time we reach
260445fabbb7SElias Oltmanns 	 * wait_for_completion_timeout(), another ATA_EH_PARK action
260545fabbb7SElias Oltmanns 	 * has been scheduled for at least one of the devices on port
260645fabbb7SElias Oltmanns 	 * ap and we have to cycle over the do {} while () loop in
260745fabbb7SElias Oltmanns 	 * ata_eh_recover() again.
260845fabbb7SElias Oltmanns 	 */
260945fabbb7SElias Oltmanns 
261045fabbb7SElias Oltmanns 	spin_lock_irqsave(ap->lock, flags);
261145fabbb7SElias Oltmanns 	INIT_COMPLETION(ap->park_req_pending);
261245fabbb7SElias Oltmanns 	ata_port_for_each_link(link, ap) {
261345fabbb7SElias Oltmanns 		ata_link_for_each_dev(dev, link) {
261445fabbb7SElias Oltmanns 			struct ata_eh_info *ehi = &link->eh_info;
261545fabbb7SElias Oltmanns 
261645fabbb7SElias Oltmanns 			link->eh_context.i.dev_action[dev->devno] |=
261745fabbb7SElias Oltmanns 				ehi->dev_action[dev->devno] & ATA_EH_PARK;
261845fabbb7SElias Oltmanns 			ata_eh_clear_action(link, dev, ehi, ATA_EH_PARK);
261945fabbb7SElias Oltmanns 		}
262045fabbb7SElias Oltmanns 	}
262145fabbb7SElias Oltmanns 	spin_unlock_irqrestore(ap->lock, flags);
262245fabbb7SElias Oltmanns }
262345fabbb7SElias Oltmanns 
262445fabbb7SElias Oltmanns static void ata_eh_park_issue_cmd(struct ata_device *dev, int park)
262545fabbb7SElias Oltmanns {
262645fabbb7SElias Oltmanns 	struct ata_eh_context *ehc = &dev->link->eh_context;
262745fabbb7SElias Oltmanns 	struct ata_taskfile tf;
262845fabbb7SElias Oltmanns 	unsigned int err_mask;
262945fabbb7SElias Oltmanns 
263045fabbb7SElias Oltmanns 	ata_tf_init(dev, &tf);
263145fabbb7SElias Oltmanns 	if (park) {
263245fabbb7SElias Oltmanns 		ehc->unloaded_mask |= 1 << dev->devno;
263345fabbb7SElias Oltmanns 		tf.command = ATA_CMD_IDLEIMMEDIATE;
263445fabbb7SElias Oltmanns 		tf.feature = 0x44;
263545fabbb7SElias Oltmanns 		tf.lbal = 0x4c;
263645fabbb7SElias Oltmanns 		tf.lbam = 0x4e;
263745fabbb7SElias Oltmanns 		tf.lbah = 0x55;
263845fabbb7SElias Oltmanns 	} else {
263945fabbb7SElias Oltmanns 		ehc->unloaded_mask &= ~(1 << dev->devno);
264045fabbb7SElias Oltmanns 		tf.command = ATA_CMD_CHK_POWER;
264145fabbb7SElias Oltmanns 	}
264245fabbb7SElias Oltmanns 
264345fabbb7SElias Oltmanns 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
264445fabbb7SElias Oltmanns 	tf.protocol |= ATA_PROT_NODATA;
264545fabbb7SElias Oltmanns 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
264645fabbb7SElias Oltmanns 	if (park && (err_mask || tf.lbal != 0xc4)) {
264745fabbb7SElias Oltmanns 		ata_dev_printk(dev, KERN_ERR, "head unload failed!\n");
264845fabbb7SElias Oltmanns 		ehc->unloaded_mask &= ~(1 << dev->devno);
264945fabbb7SElias Oltmanns 	}
265045fabbb7SElias Oltmanns }
265145fabbb7SElias Oltmanns 
26520260731fSTejun Heo static int ata_eh_revalidate_and_attach(struct ata_link *link,
2653c6fd2807SJeff Garzik 					struct ata_device **r_failed_dev)
2654c6fd2807SJeff Garzik {
26550260731fSTejun Heo 	struct ata_port *ap = link->ap;
26560260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
2657c6fd2807SJeff Garzik 	struct ata_device *dev;
26588c3c52a8STejun Heo 	unsigned int new_mask = 0;
2659c6fd2807SJeff Garzik 	unsigned long flags;
2660f58229f8STejun Heo 	int rc = 0;
2661c6fd2807SJeff Garzik 
2662c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
2663c6fd2807SJeff Garzik 
26648c3c52a8STejun Heo 	/* For PATA drive side cable detection to work, IDENTIFY must
26658c3c52a8STejun Heo 	 * be done backwards such that PDIAG- is released by the slave
26668c3c52a8STejun Heo 	 * device before the master device is identified.
26678c3c52a8STejun Heo 	 */
26680260731fSTejun Heo 	ata_link_for_each_dev_reverse(dev, link) {
2669f58229f8STejun Heo 		unsigned int action = ata_eh_dev_action(dev);
2670f58229f8STejun Heo 		unsigned int readid_flags = 0;
2671c6fd2807SJeff Garzik 
2672bff04647STejun Heo 		if (ehc->i.flags & ATA_EHI_DID_RESET)
2673bff04647STejun Heo 			readid_flags |= ATA_READID_POSTRESET;
2674bff04647STejun Heo 
26759666f400STejun Heo 		if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
2676633273a3STejun Heo 			WARN_ON(dev->class == ATA_DEV_PMP);
2677633273a3STejun Heo 
2678b1c72916STejun Heo 			if (ata_phys_link_offline(ata_dev_phys_link(dev))) {
2679c6fd2807SJeff Garzik 				rc = -EIO;
26808c3c52a8STejun Heo 				goto err;
2681c6fd2807SJeff Garzik 			}
2682c6fd2807SJeff Garzik 
26830260731fSTejun Heo 			ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
2684422c9daaSTejun Heo 			rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
2685422c9daaSTejun Heo 						readid_flags);
2686c6fd2807SJeff Garzik 			if (rc)
26878c3c52a8STejun Heo 				goto err;
2688c6fd2807SJeff Garzik 
26890260731fSTejun Heo 			ata_eh_done(link, dev, ATA_EH_REVALIDATE);
2690c6fd2807SJeff Garzik 
2691baa1e78aSTejun Heo 			/* Configuration may have changed, reconfigure
2692baa1e78aSTejun Heo 			 * transfer mode.
2693baa1e78aSTejun Heo 			 */
2694baa1e78aSTejun Heo 			ehc->i.flags |= ATA_EHI_SETMODE;
2695baa1e78aSTejun Heo 
2696c6fd2807SJeff Garzik 			/* schedule the scsi_rescan_device() here */
2697c6fd2807SJeff Garzik 			queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
2698c6fd2807SJeff Garzik 		} else if (dev->class == ATA_DEV_UNKNOWN &&
2699c6fd2807SJeff Garzik 			   ehc->tries[dev->devno] &&
2700c6fd2807SJeff Garzik 			   ata_class_enabled(ehc->classes[dev->devno])) {
2701c6fd2807SJeff Garzik 			dev->class = ehc->classes[dev->devno];
2702c6fd2807SJeff Garzik 
2703633273a3STejun Heo 			if (dev->class == ATA_DEV_PMP)
2704633273a3STejun Heo 				rc = sata_pmp_attach(dev);
2705633273a3STejun Heo 			else
2706633273a3STejun Heo 				rc = ata_dev_read_id(dev, &dev->class,
2707633273a3STejun Heo 						     readid_flags, dev->id);
27088c3c52a8STejun Heo 			switch (rc) {
27098c3c52a8STejun Heo 			case 0:
2710f58229f8STejun Heo 				new_mask |= 1 << dev->devno;
27118c3c52a8STejun Heo 				break;
27128c3c52a8STejun Heo 			case -ENOENT:
271355a8e2c8STejun Heo 				/* IDENTIFY was issued to non-existent
271455a8e2c8STejun Heo 				 * device.  No need to reset.  Just
271555a8e2c8STejun Heo 				 * thaw and kill the device.
271655a8e2c8STejun Heo 				 */
271755a8e2c8STejun Heo 				ata_eh_thaw_port(ap);
271855a8e2c8STejun Heo 				dev->class = ATA_DEV_UNKNOWN;
2719c6fd2807SJeff Garzik 				break;
27208c3c52a8STejun Heo 			default:
27218c3c52a8STejun Heo 				dev->class = ATA_DEV_UNKNOWN;
27228c3c52a8STejun Heo 				goto err;
27238c3c52a8STejun Heo 			}
27248c3c52a8STejun Heo 		}
2725c6fd2807SJeff Garzik 	}
2726c6fd2807SJeff Garzik 
2727c1c4e8d5STejun Heo 	/* PDIAG- should have been released, ask cable type if post-reset */
272833267325STejun Heo 	if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) {
272933267325STejun Heo 		if (ap->ops->cable_detect)
2730c1c4e8d5STejun Heo 			ap->cbl = ap->ops->cable_detect(ap);
273133267325STejun Heo 		ata_force_cbl(ap);
273233267325STejun Heo 	}
2733c1c4e8d5STejun Heo 
27348c3c52a8STejun Heo 	/* Configure new devices forward such that user doesn't see
27358c3c52a8STejun Heo 	 * device detection messages backwards.
27368c3c52a8STejun Heo 	 */
27370260731fSTejun Heo 	ata_link_for_each_dev(dev, link) {
2738633273a3STejun Heo 		if (!(new_mask & (1 << dev->devno)) ||
2739633273a3STejun Heo 		    dev->class == ATA_DEV_PMP)
27408c3c52a8STejun Heo 			continue;
27418c3c52a8STejun Heo 
27428c3c52a8STejun Heo 		ehc->i.flags |= ATA_EHI_PRINTINFO;
27438c3c52a8STejun Heo 		rc = ata_dev_configure(dev);
27448c3c52a8STejun Heo 		ehc->i.flags &= ~ATA_EHI_PRINTINFO;
27458c3c52a8STejun Heo 		if (rc)
27468c3c52a8STejun Heo 			goto err;
27478c3c52a8STejun Heo 
2748c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
2749c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
2750c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
2751baa1e78aSTejun Heo 
275255a8e2c8STejun Heo 		/* new device discovered, configure xfermode */
2753baa1e78aSTejun Heo 		ehc->i.flags |= ATA_EHI_SETMODE;
2754c6fd2807SJeff Garzik 	}
2755c6fd2807SJeff Garzik 
27568c3c52a8STejun Heo 	return 0;
27578c3c52a8STejun Heo 
27588c3c52a8STejun Heo  err:
2759c6fd2807SJeff Garzik 	*r_failed_dev = dev;
27608c3c52a8STejun Heo 	DPRINTK("EXIT rc=%d\n", rc);
2761c6fd2807SJeff Garzik 	return rc;
2762c6fd2807SJeff Garzik }
2763c6fd2807SJeff Garzik 
27646f1d1e3aSTejun Heo /**
27656f1d1e3aSTejun Heo  *	ata_set_mode - Program timings and issue SET FEATURES - XFER
27666f1d1e3aSTejun Heo  *	@link: link on which timings will be programmed
27676f1d1e3aSTejun Heo  *	@r_failed_dev: out paramter for failed device
27686f1d1e3aSTejun Heo  *
27696f1d1e3aSTejun Heo  *	Set ATA device disk transfer mode (PIO3, UDMA6, etc.).  If
27706f1d1e3aSTejun Heo  *	ata_set_mode() fails, pointer to the failing device is
27716f1d1e3aSTejun Heo  *	returned in @r_failed_dev.
27726f1d1e3aSTejun Heo  *
27736f1d1e3aSTejun Heo  *	LOCKING:
27746f1d1e3aSTejun Heo  *	PCI/etc. bus probe sem.
27756f1d1e3aSTejun Heo  *
27766f1d1e3aSTejun Heo  *	RETURNS:
27776f1d1e3aSTejun Heo  *	0 on success, negative errno otherwise
27786f1d1e3aSTejun Heo  */
27796f1d1e3aSTejun Heo int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
27806f1d1e3aSTejun Heo {
27816f1d1e3aSTejun Heo 	struct ata_port *ap = link->ap;
278200115e0fSTejun Heo 	struct ata_device *dev;
278300115e0fSTejun Heo 	int rc;
27846f1d1e3aSTejun Heo 
278576326ac1STejun Heo 	/* if data transfer is verified, clear DUBIOUS_XFER on ering top */
278676326ac1STejun Heo 	ata_link_for_each_dev(dev, link) {
278776326ac1STejun Heo 		if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
278876326ac1STejun Heo 			struct ata_ering_entry *ent;
278976326ac1STejun Heo 
279076326ac1STejun Heo 			ent = ata_ering_top(&dev->ering);
279176326ac1STejun Heo 			if (ent)
279276326ac1STejun Heo 				ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER;
279376326ac1STejun Heo 		}
279476326ac1STejun Heo 	}
279576326ac1STejun Heo 
27966f1d1e3aSTejun Heo 	/* has private set_mode? */
27976f1d1e3aSTejun Heo 	if (ap->ops->set_mode)
279800115e0fSTejun Heo 		rc = ap->ops->set_mode(link, r_failed_dev);
279900115e0fSTejun Heo 	else
280000115e0fSTejun Heo 		rc = ata_do_set_mode(link, r_failed_dev);
280100115e0fSTejun Heo 
280200115e0fSTejun Heo 	/* if transfer mode has changed, set DUBIOUS_XFER on device */
280300115e0fSTejun Heo 	ata_link_for_each_dev(dev, link) {
280400115e0fSTejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
280500115e0fSTejun Heo 		u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
280600115e0fSTejun Heo 		u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
280700115e0fSTejun Heo 
280800115e0fSTejun Heo 		if (dev->xfer_mode != saved_xfer_mode ||
280900115e0fSTejun Heo 		    ata_ncq_enabled(dev) != saved_ncq)
281000115e0fSTejun Heo 			dev->flags |= ATA_DFLAG_DUBIOUS_XFER;
281100115e0fSTejun Heo 	}
281200115e0fSTejun Heo 
281300115e0fSTejun Heo 	return rc;
28146f1d1e3aSTejun Heo }
28156f1d1e3aSTejun Heo 
281611fc33daSTejun Heo /**
281711fc33daSTejun Heo  *	atapi_eh_clear_ua - Clear ATAPI UNIT ATTENTION after reset
281811fc33daSTejun Heo  *	@dev: ATAPI device to clear UA for
281911fc33daSTejun Heo  *
282011fc33daSTejun Heo  *	Resets and other operations can make an ATAPI device raise
282111fc33daSTejun Heo  *	UNIT ATTENTION which causes the next operation to fail.  This
282211fc33daSTejun Heo  *	function clears UA.
282311fc33daSTejun Heo  *
282411fc33daSTejun Heo  *	LOCKING:
282511fc33daSTejun Heo  *	EH context (may sleep).
282611fc33daSTejun Heo  *
282711fc33daSTejun Heo  *	RETURNS:
282811fc33daSTejun Heo  *	0 on success, -errno on failure.
282911fc33daSTejun Heo  */
283011fc33daSTejun Heo static int atapi_eh_clear_ua(struct ata_device *dev)
283111fc33daSTejun Heo {
283211fc33daSTejun Heo 	int i;
283311fc33daSTejun Heo 
283411fc33daSTejun Heo 	for (i = 0; i < ATA_EH_UA_TRIES; i++) {
283511fc33daSTejun Heo 		u8 sense_buffer[SCSI_SENSE_BUFFERSIZE];
283611fc33daSTejun Heo 		u8 sense_key = 0;
283711fc33daSTejun Heo 		unsigned int err_mask;
283811fc33daSTejun Heo 
283911fc33daSTejun Heo 		err_mask = atapi_eh_tur(dev, &sense_key);
284011fc33daSTejun Heo 		if (err_mask != 0 && err_mask != AC_ERR_DEV) {
284111fc33daSTejun Heo 			ata_dev_printk(dev, KERN_WARNING, "TEST_UNIT_READY "
284211fc33daSTejun Heo 				"failed (err_mask=0x%x)\n", err_mask);
284311fc33daSTejun Heo 			return -EIO;
284411fc33daSTejun Heo 		}
284511fc33daSTejun Heo 
284611fc33daSTejun Heo 		if (!err_mask || sense_key != UNIT_ATTENTION)
284711fc33daSTejun Heo 			return 0;
284811fc33daSTejun Heo 
284911fc33daSTejun Heo 		err_mask = atapi_eh_request_sense(dev, sense_buffer, sense_key);
285011fc33daSTejun Heo 		if (err_mask) {
285111fc33daSTejun Heo 			ata_dev_printk(dev, KERN_WARNING, "failed to clear "
285211fc33daSTejun Heo 				"UNIT ATTENTION (err_mask=0x%x)\n", err_mask);
285311fc33daSTejun Heo 			return -EIO;
285411fc33daSTejun Heo 		}
285511fc33daSTejun Heo 	}
285611fc33daSTejun Heo 
285711fc33daSTejun Heo 	ata_dev_printk(dev, KERN_WARNING,
285811fc33daSTejun Heo 		"UNIT ATTENTION persists after %d tries\n", ATA_EH_UA_TRIES);
285911fc33daSTejun Heo 
286011fc33daSTejun Heo 	return 0;
286111fc33daSTejun Heo }
286211fc33daSTejun Heo 
28630260731fSTejun Heo static int ata_link_nr_enabled(struct ata_link *link)
2864c6fd2807SJeff Garzik {
2865f58229f8STejun Heo 	struct ata_device *dev;
2866f58229f8STejun Heo 	int cnt = 0;
2867c6fd2807SJeff Garzik 
28680260731fSTejun Heo 	ata_link_for_each_dev(dev, link)
2869f58229f8STejun Heo 		if (ata_dev_enabled(dev))
2870c6fd2807SJeff Garzik 			cnt++;
2871c6fd2807SJeff Garzik 	return cnt;
2872c6fd2807SJeff Garzik }
2873c6fd2807SJeff Garzik 
28740260731fSTejun Heo static int ata_link_nr_vacant(struct ata_link *link)
2875c6fd2807SJeff Garzik {
2876f58229f8STejun Heo 	struct ata_device *dev;
2877f58229f8STejun Heo 	int cnt = 0;
2878c6fd2807SJeff Garzik 
28790260731fSTejun Heo 	ata_link_for_each_dev(dev, link)
2880f58229f8STejun Heo 		if (dev->class == ATA_DEV_UNKNOWN)
2881c6fd2807SJeff Garzik 			cnt++;
2882c6fd2807SJeff Garzik 	return cnt;
2883c6fd2807SJeff Garzik }
2884c6fd2807SJeff Garzik 
28850260731fSTejun Heo static int ata_eh_skip_recovery(struct ata_link *link)
2886c6fd2807SJeff Garzik {
2887672b2d65STejun Heo 	struct ata_port *ap = link->ap;
28880260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
2889f58229f8STejun Heo 	struct ata_device *dev;
2890c6fd2807SJeff Garzik 
2891f9df58cbSTejun Heo 	/* skip disabled links */
2892f9df58cbSTejun Heo 	if (link->flags & ATA_LFLAG_DISABLED)
2893f9df58cbSTejun Heo 		return 1;
2894f9df58cbSTejun Heo 
2895672b2d65STejun Heo 	/* thaw frozen port and recover failed devices */
2896672b2d65STejun Heo 	if ((ap->pflags & ATA_PFLAG_FROZEN) || ata_link_nr_enabled(link))
2897672b2d65STejun Heo 		return 0;
2898672b2d65STejun Heo 
2899672b2d65STejun Heo 	/* reset at least once if reset is requested */
2900672b2d65STejun Heo 	if ((ehc->i.action & ATA_EH_RESET) &&
2901672b2d65STejun Heo 	    !(ehc->i.flags & ATA_EHI_DID_RESET))
2902c6fd2807SJeff Garzik 		return 0;
2903c6fd2807SJeff Garzik 
2904c6fd2807SJeff Garzik 	/* skip if class codes for all vacant slots are ATA_DEV_NONE */
29050260731fSTejun Heo 	ata_link_for_each_dev(dev, link) {
2906c6fd2807SJeff Garzik 		if (dev->class == ATA_DEV_UNKNOWN &&
2907c6fd2807SJeff Garzik 		    ehc->classes[dev->devno] != ATA_DEV_NONE)
2908c6fd2807SJeff Garzik 			return 0;
2909c6fd2807SJeff Garzik 	}
2910c6fd2807SJeff Garzik 
2911c6fd2807SJeff Garzik 	return 1;
2912c6fd2807SJeff Garzik }
2913c6fd2807SJeff Garzik 
291402c05a27STejun Heo static int ata_eh_schedule_probe(struct ata_device *dev)
291502c05a27STejun Heo {
291602c05a27STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
291702c05a27STejun Heo 
291802c05a27STejun Heo 	if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
291902c05a27STejun Heo 	    (ehc->did_probe_mask & (1 << dev->devno)))
292002c05a27STejun Heo 		return 0;
292102c05a27STejun Heo 
292202c05a27STejun Heo 	ata_eh_detach_dev(dev);
292302c05a27STejun Heo 	ata_dev_init(dev);
292402c05a27STejun Heo 	ehc->did_probe_mask |= (1 << dev->devno);
2925cf480626STejun Heo 	ehc->i.action |= ATA_EH_RESET;
292600115e0fSTejun Heo 	ehc->saved_xfer_mode[dev->devno] = 0;
292700115e0fSTejun Heo 	ehc->saved_ncq_enabled &= ~(1 << dev->devno);
292802c05a27STejun Heo 
292902c05a27STejun Heo 	return 1;
293002c05a27STejun Heo }
293102c05a27STejun Heo 
29329b1e2658STejun Heo static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
2933fee7ca72STejun Heo {
29349af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
2935fee7ca72STejun Heo 
2936fee7ca72STejun Heo 	ehc->tries[dev->devno]--;
2937fee7ca72STejun Heo 
2938fee7ca72STejun Heo 	switch (err) {
2939fee7ca72STejun Heo 	case -ENODEV:
2940fee7ca72STejun Heo 		/* device missing or wrong IDENTIFY data, schedule probing */
2941fee7ca72STejun Heo 		ehc->i.probe_mask |= (1 << dev->devno);
2942fee7ca72STejun Heo 	case -EINVAL:
2943fee7ca72STejun Heo 		/* give it just one more chance */
2944fee7ca72STejun Heo 		ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
2945fee7ca72STejun Heo 	case -EIO:
29464fb4615bSTejun Heo 		if (ehc->tries[dev->devno] == 1 && dev->pio_mode > XFER_PIO_0) {
2947fee7ca72STejun Heo 			/* This is the last chance, better to slow
2948fee7ca72STejun Heo 			 * down than lose it.
2949fee7ca72STejun Heo 			 */
2950b1c72916STejun Heo 			sata_down_spd_limit(ata_dev_phys_link(dev));
2951fee7ca72STejun Heo 			ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2952fee7ca72STejun Heo 		}
2953fee7ca72STejun Heo 	}
2954fee7ca72STejun Heo 
2955fee7ca72STejun Heo 	if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2956fee7ca72STejun Heo 		/* disable device if it has used up all its chances */
2957fee7ca72STejun Heo 		ata_dev_disable(dev);
2958fee7ca72STejun Heo 
2959fee7ca72STejun Heo 		/* detach if offline */
2960b1c72916STejun Heo 		if (ata_phys_link_offline(ata_dev_phys_link(dev)))
2961fee7ca72STejun Heo 			ata_eh_detach_dev(dev);
2962fee7ca72STejun Heo 
296302c05a27STejun Heo 		/* schedule probe if necessary */
296487fbc5a0STejun Heo 		if (ata_eh_schedule_probe(dev)) {
2965fee7ca72STejun Heo 			ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
296687fbc5a0STejun Heo 			memset(ehc->cmd_timeout_idx[dev->devno], 0,
296787fbc5a0STejun Heo 			       sizeof(ehc->cmd_timeout_idx[dev->devno]));
296887fbc5a0STejun Heo 		}
29699b1e2658STejun Heo 
29709b1e2658STejun Heo 		return 1;
2971fee7ca72STejun Heo 	} else {
2972cf480626STejun Heo 		ehc->i.action |= ATA_EH_RESET;
29739b1e2658STejun Heo 		return 0;
2974fee7ca72STejun Heo 	}
2975fee7ca72STejun Heo }
2976fee7ca72STejun Heo 
2977c6fd2807SJeff Garzik /**
2978c6fd2807SJeff Garzik  *	ata_eh_recover - recover host port after error
2979c6fd2807SJeff Garzik  *	@ap: host port to recover
2980c6fd2807SJeff Garzik  *	@prereset: prereset method (can be NULL)
2981c6fd2807SJeff Garzik  *	@softreset: softreset method (can be NULL)
2982c6fd2807SJeff Garzik  *	@hardreset: hardreset method (can be NULL)
2983c6fd2807SJeff Garzik  *	@postreset: postreset method (can be NULL)
29849b1e2658STejun Heo  *	@r_failed_link: out parameter for failed link
2985c6fd2807SJeff Garzik  *
2986c6fd2807SJeff Garzik  *	This is the alpha and omega, eum and yang, heart and soul of
2987c6fd2807SJeff Garzik  *	libata exception handling.  On entry, actions required to
29889b1e2658STejun Heo  *	recover each link and hotplug requests are recorded in the
29899b1e2658STejun Heo  *	link's eh_context.  This function executes all the operations
29909b1e2658STejun Heo  *	with appropriate retrials and fallbacks to resurrect failed
2991c6fd2807SJeff Garzik  *	devices, detach goners and greet newcomers.
2992c6fd2807SJeff Garzik  *
2993c6fd2807SJeff Garzik  *	LOCKING:
2994c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
2995c6fd2807SJeff Garzik  *
2996c6fd2807SJeff Garzik  *	RETURNS:
2997c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
2998c6fd2807SJeff Garzik  */
2999fb7fd614STejun Heo int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
3000c6fd2807SJeff Garzik 		   ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
30019b1e2658STejun Heo 		   ata_postreset_fn_t postreset,
30029b1e2658STejun Heo 		   struct ata_link **r_failed_link)
3003c6fd2807SJeff Garzik {
30049b1e2658STejun Heo 	struct ata_link *link;
3005c6fd2807SJeff Garzik 	struct ata_device *dev;
30060a2c0f56STejun Heo 	int nr_failed_devs;
3007dc98c32cSTejun Heo 	int rc;
300845fabbb7SElias Oltmanns 	unsigned long flags, deadline;
3009c6fd2807SJeff Garzik 
3010c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
3011c6fd2807SJeff Garzik 
3012c6fd2807SJeff Garzik 	/* prep for recovery */
30139b1e2658STejun Heo 	ata_port_for_each_link(link, ap) {
30149b1e2658STejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
30159b1e2658STejun Heo 
3016f9df58cbSTejun Heo 		/* re-enable link? */
3017f9df58cbSTejun Heo 		if (ehc->i.action & ATA_EH_ENABLE_LINK) {
3018f9df58cbSTejun Heo 			ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
3019f9df58cbSTejun Heo 			spin_lock_irqsave(ap->lock, flags);
3020f9df58cbSTejun Heo 			link->flags &= ~ATA_LFLAG_DISABLED;
3021f9df58cbSTejun Heo 			spin_unlock_irqrestore(ap->lock, flags);
3022f9df58cbSTejun Heo 			ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
3023f9df58cbSTejun Heo 		}
3024f9df58cbSTejun Heo 
30250260731fSTejun Heo 		ata_link_for_each_dev(dev, link) {
3026fd995f70STejun Heo 			if (link->flags & ATA_LFLAG_NO_RETRY)
3027fd995f70STejun Heo 				ehc->tries[dev->devno] = 1;
3028fd995f70STejun Heo 			else
3029c6fd2807SJeff Garzik 				ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
3030c6fd2807SJeff Garzik 
303179a55b72STejun Heo 			/* collect port action mask recorded in dev actions */
30329b1e2658STejun Heo 			ehc->i.action |= ehc->i.dev_action[dev->devno] &
30339b1e2658STejun Heo 					 ~ATA_EH_PERDEV_MASK;
3034f58229f8STejun Heo 			ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
303579a55b72STejun Heo 
3036c6fd2807SJeff Garzik 			/* process hotplug request */
3037c6fd2807SJeff Garzik 			if (dev->flags & ATA_DFLAG_DETACH)
3038c6fd2807SJeff Garzik 				ata_eh_detach_dev(dev);
3039c6fd2807SJeff Garzik 
304002c05a27STejun Heo 			/* schedule probe if necessary */
304102c05a27STejun Heo 			if (!ata_dev_enabled(dev))
304202c05a27STejun Heo 				ata_eh_schedule_probe(dev);
3043c6fd2807SJeff Garzik 		}
30449b1e2658STejun Heo 	}
3045c6fd2807SJeff Garzik 
3046c6fd2807SJeff Garzik  retry:
3047c6fd2807SJeff Garzik 	rc = 0;
30489b1e2658STejun Heo 	nr_failed_devs = 0;
3049c6fd2807SJeff Garzik 
3050c6fd2807SJeff Garzik 	/* if UNLOADING, finish immediately */
3051c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_UNLOADING)
3052c6fd2807SJeff Garzik 		goto out;
3053c6fd2807SJeff Garzik 
30549b1e2658STejun Heo 	/* prep for EH */
30559b1e2658STejun Heo 	ata_port_for_each_link(link, ap) {
30569b1e2658STejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
30579b1e2658STejun Heo 
3058c6fd2807SJeff Garzik 		/* skip EH if possible. */
30590260731fSTejun Heo 		if (ata_eh_skip_recovery(link))
3060c6fd2807SJeff Garzik 			ehc->i.action = 0;
3061c6fd2807SJeff Garzik 
30620260731fSTejun Heo 		ata_link_for_each_dev(dev, link)
3063f58229f8STejun Heo 			ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
30649b1e2658STejun Heo 	}
3065c6fd2807SJeff Garzik 
3066c6fd2807SJeff Garzik 	/* reset */
30679b1e2658STejun Heo 	ata_port_for_each_link(link, ap) {
30689b1e2658STejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
30699b1e2658STejun Heo 
3070cf480626STejun Heo 		if (!(ehc->i.action & ATA_EH_RESET))
30719b1e2658STejun Heo 			continue;
30729b1e2658STejun Heo 
30739b1e2658STejun Heo 		rc = ata_eh_reset(link, ata_link_nr_vacant(link),
3074dc98c32cSTejun Heo 				  prereset, softreset, hardreset, postreset);
3075c6fd2807SJeff Garzik 		if (rc) {
30760260731fSTejun Heo 			ata_link_printk(link, KERN_ERR,
3077c6fd2807SJeff Garzik 					"reset failed, giving up\n");
3078c6fd2807SJeff Garzik 			goto out;
3079c6fd2807SJeff Garzik 		}
30809b1e2658STejun Heo 	}
3081c6fd2807SJeff Garzik 
308245fabbb7SElias Oltmanns 	do {
308345fabbb7SElias Oltmanns 		unsigned long now;
308445fabbb7SElias Oltmanns 
308545fabbb7SElias Oltmanns 		/*
308645fabbb7SElias Oltmanns 		 * clears ATA_EH_PARK in eh_info and resets
308745fabbb7SElias Oltmanns 		 * ap->park_req_pending
308845fabbb7SElias Oltmanns 		 */
308945fabbb7SElias Oltmanns 		ata_eh_pull_park_action(ap);
309045fabbb7SElias Oltmanns 
309145fabbb7SElias Oltmanns 		deadline = jiffies;
309245fabbb7SElias Oltmanns 		ata_port_for_each_link(link, ap) {
309345fabbb7SElias Oltmanns 			ata_link_for_each_dev(dev, link) {
309445fabbb7SElias Oltmanns 				struct ata_eh_context *ehc = &link->eh_context;
309545fabbb7SElias Oltmanns 				unsigned long tmp;
309645fabbb7SElias Oltmanns 
309745fabbb7SElias Oltmanns 				if (dev->class != ATA_DEV_ATA)
309845fabbb7SElias Oltmanns 					continue;
309945fabbb7SElias Oltmanns 				if (!(ehc->i.dev_action[dev->devno] &
310045fabbb7SElias Oltmanns 				      ATA_EH_PARK))
310145fabbb7SElias Oltmanns 					continue;
310245fabbb7SElias Oltmanns 				tmp = dev->unpark_deadline;
310345fabbb7SElias Oltmanns 				if (time_before(deadline, tmp))
310445fabbb7SElias Oltmanns 					deadline = tmp;
310545fabbb7SElias Oltmanns 				else if (time_before_eq(tmp, jiffies))
310645fabbb7SElias Oltmanns 					continue;
310745fabbb7SElias Oltmanns 				if (ehc->unloaded_mask & (1 << dev->devno))
310845fabbb7SElias Oltmanns 					continue;
310945fabbb7SElias Oltmanns 
311045fabbb7SElias Oltmanns 				ata_eh_park_issue_cmd(dev, 1);
311145fabbb7SElias Oltmanns 			}
311245fabbb7SElias Oltmanns 		}
311345fabbb7SElias Oltmanns 
311445fabbb7SElias Oltmanns 		now = jiffies;
311545fabbb7SElias Oltmanns 		if (time_before_eq(deadline, now))
311645fabbb7SElias Oltmanns 			break;
311745fabbb7SElias Oltmanns 
311845fabbb7SElias Oltmanns 		deadline = wait_for_completion_timeout(&ap->park_req_pending,
311945fabbb7SElias Oltmanns 						       deadline - now);
312045fabbb7SElias Oltmanns 	} while (deadline);
312145fabbb7SElias Oltmanns 	ata_port_for_each_link(link, ap) {
312245fabbb7SElias Oltmanns 		ata_link_for_each_dev(dev, link) {
312345fabbb7SElias Oltmanns 			if (!(link->eh_context.unloaded_mask &
312445fabbb7SElias Oltmanns 			      (1 << dev->devno)))
312545fabbb7SElias Oltmanns 				continue;
312645fabbb7SElias Oltmanns 
312745fabbb7SElias Oltmanns 			ata_eh_park_issue_cmd(dev, 0);
312845fabbb7SElias Oltmanns 			ata_eh_done(link, dev, ATA_EH_PARK);
312945fabbb7SElias Oltmanns 		}
313045fabbb7SElias Oltmanns 	}
313145fabbb7SElias Oltmanns 
31329b1e2658STejun Heo 	/* the rest */
31339b1e2658STejun Heo 	ata_port_for_each_link(link, ap) {
31349b1e2658STejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
31359b1e2658STejun Heo 
3136c6fd2807SJeff Garzik 		/* revalidate existing devices and attach new ones */
31370260731fSTejun Heo 		rc = ata_eh_revalidate_and_attach(link, &dev);
3138c6fd2807SJeff Garzik 		if (rc)
3139c6fd2807SJeff Garzik 			goto dev_fail;
3140c6fd2807SJeff Garzik 
3141633273a3STejun Heo 		/* if PMP got attached, return, pmp EH will take care of it */
3142633273a3STejun Heo 		if (link->device->class == ATA_DEV_PMP) {
3143633273a3STejun Heo 			ehc->i.action = 0;
3144633273a3STejun Heo 			return 0;
3145633273a3STejun Heo 		}
3146633273a3STejun Heo 
3147baa1e78aSTejun Heo 		/* configure transfer mode if necessary */
3148baa1e78aSTejun Heo 		if (ehc->i.flags & ATA_EHI_SETMODE) {
31490260731fSTejun Heo 			rc = ata_set_mode(link, &dev);
31504ae72a1eSTejun Heo 			if (rc)
3151c6fd2807SJeff Garzik 				goto dev_fail;
3152baa1e78aSTejun Heo 			ehc->i.flags &= ~ATA_EHI_SETMODE;
3153c6fd2807SJeff Garzik 		}
3154c6fd2807SJeff Garzik 
315511fc33daSTejun Heo 		/* If reset has been issued, clear UA to avoid
315611fc33daSTejun Heo 		 * disrupting the current users of the device.
315711fc33daSTejun Heo 		 */
315811fc33daSTejun Heo 		if (ehc->i.flags & ATA_EHI_DID_RESET) {
315911fc33daSTejun Heo 			ata_link_for_each_dev(dev, link) {
316011fc33daSTejun Heo 				if (dev->class != ATA_DEV_ATAPI)
316111fc33daSTejun Heo 					continue;
316211fc33daSTejun Heo 				rc = atapi_eh_clear_ua(dev);
316311fc33daSTejun Heo 				if (rc)
316411fc33daSTejun Heo 					goto dev_fail;
316511fc33daSTejun Heo 			}
316611fc33daSTejun Heo 		}
316711fc33daSTejun Heo 
316811fc33daSTejun Heo 		/* configure link power saving */
31693ec25ebdSTejun Heo 		if (ehc->i.action & ATA_EH_LPM)
3170ca77329fSKristen Carlson Accardi 			ata_link_for_each_dev(dev, link)
3171ca77329fSKristen Carlson Accardi 				ata_dev_enable_pm(dev, ap->pm_policy);
3172ca77329fSKristen Carlson Accardi 
31739b1e2658STejun Heo 		/* this link is okay now */
31749b1e2658STejun Heo 		ehc->i.flags = 0;
31759b1e2658STejun Heo 		continue;
3176c6fd2807SJeff Garzik 
3177c6fd2807SJeff Garzik dev_fail:
31789b1e2658STejun Heo 		nr_failed_devs++;
31790a2c0f56STejun Heo 		ata_eh_handle_dev_fail(dev, rc);
3180c6fd2807SJeff Garzik 
3181b06ce3e5STejun Heo 		if (ap->pflags & ATA_PFLAG_FROZEN) {
3182b06ce3e5STejun Heo 			/* PMP reset requires working host port.
3183b06ce3e5STejun Heo 			 * Can't retry if it's frozen.
3184b06ce3e5STejun Heo 			 */
3185071f44b1STejun Heo 			if (sata_pmp_attached(ap))
3186b06ce3e5STejun Heo 				goto out;
31879b1e2658STejun Heo 			break;
31889b1e2658STejun Heo 		}
3189b06ce3e5STejun Heo 	}
31909b1e2658STejun Heo 
31910a2c0f56STejun Heo 	if (nr_failed_devs)
3192c6fd2807SJeff Garzik 		goto retry;
3193c6fd2807SJeff Garzik 
3194c6fd2807SJeff Garzik  out:
31959b1e2658STejun Heo 	if (rc && r_failed_link)
31969b1e2658STejun Heo 		*r_failed_link = link;
3197c6fd2807SJeff Garzik 
3198c6fd2807SJeff Garzik 	DPRINTK("EXIT, rc=%d\n", rc);
3199c6fd2807SJeff Garzik 	return rc;
3200c6fd2807SJeff Garzik }
3201c6fd2807SJeff Garzik 
3202c6fd2807SJeff Garzik /**
3203c6fd2807SJeff Garzik  *	ata_eh_finish - finish up EH
3204c6fd2807SJeff Garzik  *	@ap: host port to finish EH for
3205c6fd2807SJeff Garzik  *
3206c6fd2807SJeff Garzik  *	Recovery is complete.  Clean up EH states and retry or finish
3207c6fd2807SJeff Garzik  *	failed qcs.
3208c6fd2807SJeff Garzik  *
3209c6fd2807SJeff Garzik  *	LOCKING:
3210c6fd2807SJeff Garzik  *	None.
3211c6fd2807SJeff Garzik  */
3212fb7fd614STejun Heo void ata_eh_finish(struct ata_port *ap)
3213c6fd2807SJeff Garzik {
3214c6fd2807SJeff Garzik 	int tag;
3215c6fd2807SJeff Garzik 
3216c6fd2807SJeff Garzik 	/* retry or finish qcs */
3217c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
3218c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
3219c6fd2807SJeff Garzik 
3220c6fd2807SJeff Garzik 		if (!(qc->flags & ATA_QCFLAG_FAILED))
3221c6fd2807SJeff Garzik 			continue;
3222c6fd2807SJeff Garzik 
3223c6fd2807SJeff Garzik 		if (qc->err_mask) {
3224c6fd2807SJeff Garzik 			/* FIXME: Once EH migration is complete,
3225c6fd2807SJeff Garzik 			 * generate sense data in this function,
3226c6fd2807SJeff Garzik 			 * considering both err_mask and tf.
3227c6fd2807SJeff Garzik 			 */
322803faab78STejun Heo 			if (qc->flags & ATA_QCFLAG_RETRY)
3229c6fd2807SJeff Garzik 				ata_eh_qc_retry(qc);
323003faab78STejun Heo 			else
323103faab78STejun Heo 				ata_eh_qc_complete(qc);
3232c6fd2807SJeff Garzik 		} else {
3233c6fd2807SJeff Garzik 			if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
3234c6fd2807SJeff Garzik 				ata_eh_qc_complete(qc);
3235c6fd2807SJeff Garzik 			} else {
3236c6fd2807SJeff Garzik 				/* feed zero TF to sense generation */
3237c6fd2807SJeff Garzik 				memset(&qc->result_tf, 0, sizeof(qc->result_tf));
3238c6fd2807SJeff Garzik 				ata_eh_qc_retry(qc);
3239c6fd2807SJeff Garzik 			}
3240c6fd2807SJeff Garzik 		}
3241c6fd2807SJeff Garzik 	}
3242da917d69STejun Heo 
3243da917d69STejun Heo 	/* make sure nr_active_links is zero after EH */
3244da917d69STejun Heo 	WARN_ON(ap->nr_active_links);
3245da917d69STejun Heo 	ap->nr_active_links = 0;
3246c6fd2807SJeff Garzik }
3247c6fd2807SJeff Garzik 
3248c6fd2807SJeff Garzik /**
3249c6fd2807SJeff Garzik  *	ata_do_eh - do standard error handling
3250c6fd2807SJeff Garzik  *	@ap: host port to handle error for
3251a1efdabaSTejun Heo  *
3252c6fd2807SJeff Garzik  *	@prereset: prereset method (can be NULL)
3253c6fd2807SJeff Garzik  *	@softreset: softreset method (can be NULL)
3254c6fd2807SJeff Garzik  *	@hardreset: hardreset method (can be NULL)
3255c6fd2807SJeff Garzik  *	@postreset: postreset method (can be NULL)
3256c6fd2807SJeff Garzik  *
3257c6fd2807SJeff Garzik  *	Perform standard error handling sequence.
3258c6fd2807SJeff Garzik  *
3259c6fd2807SJeff Garzik  *	LOCKING:
3260c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
3261c6fd2807SJeff Garzik  */
3262c6fd2807SJeff Garzik void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
3263c6fd2807SJeff Garzik 	       ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
3264c6fd2807SJeff Garzik 	       ata_postreset_fn_t postreset)
3265c6fd2807SJeff Garzik {
32669b1e2658STejun Heo 	struct ata_device *dev;
32679b1e2658STejun Heo 	int rc;
32689b1e2658STejun Heo 
32699b1e2658STejun Heo 	ata_eh_autopsy(ap);
32709b1e2658STejun Heo 	ata_eh_report(ap);
32719b1e2658STejun Heo 
32729b1e2658STejun Heo 	rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
32739b1e2658STejun Heo 			    NULL);
32749b1e2658STejun Heo 	if (rc) {
32759b1e2658STejun Heo 		ata_link_for_each_dev(dev, &ap->link)
32769b1e2658STejun Heo 			ata_dev_disable(dev);
32779b1e2658STejun Heo 	}
32789b1e2658STejun Heo 
3279c6fd2807SJeff Garzik 	ata_eh_finish(ap);
3280c6fd2807SJeff Garzik }
3281c6fd2807SJeff Garzik 
3282a1efdabaSTejun Heo /**
3283a1efdabaSTejun Heo  *	ata_std_error_handler - standard error handler
3284a1efdabaSTejun Heo  *	@ap: host port to handle error for
3285a1efdabaSTejun Heo  *
3286a1efdabaSTejun Heo  *	Standard error handler
3287a1efdabaSTejun Heo  *
3288a1efdabaSTejun Heo  *	LOCKING:
3289a1efdabaSTejun Heo  *	Kernel thread context (may sleep).
3290a1efdabaSTejun Heo  */
3291a1efdabaSTejun Heo void ata_std_error_handler(struct ata_port *ap)
3292a1efdabaSTejun Heo {
3293a1efdabaSTejun Heo 	struct ata_port_operations *ops = ap->ops;
3294a1efdabaSTejun Heo 	ata_reset_fn_t hardreset = ops->hardreset;
3295a1efdabaSTejun Heo 
329657c9efdfSTejun Heo 	/* ignore built-in hardreset if SCR access is not available */
329757c9efdfSTejun Heo 	if (ata_is_builtin_hardreset(hardreset) && !sata_scr_valid(&ap->link))
3298a1efdabaSTejun Heo 		hardreset = NULL;
3299a1efdabaSTejun Heo 
3300a1efdabaSTejun Heo 	ata_do_eh(ap, ops->prereset, ops->softreset, hardreset, ops->postreset);
3301a1efdabaSTejun Heo }
3302a1efdabaSTejun Heo 
33036ffa01d8STejun Heo #ifdef CONFIG_PM
3304c6fd2807SJeff Garzik /**
3305c6fd2807SJeff Garzik  *	ata_eh_handle_port_suspend - perform port suspend operation
3306c6fd2807SJeff Garzik  *	@ap: port to suspend
3307c6fd2807SJeff Garzik  *
3308c6fd2807SJeff Garzik  *	Suspend @ap.
3309c6fd2807SJeff Garzik  *
3310c6fd2807SJeff Garzik  *	LOCKING:
3311c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
3312c6fd2807SJeff Garzik  */
3313c6fd2807SJeff Garzik static void ata_eh_handle_port_suspend(struct ata_port *ap)
3314c6fd2807SJeff Garzik {
3315c6fd2807SJeff Garzik 	unsigned long flags;
3316c6fd2807SJeff Garzik 	int rc = 0;
3317c6fd2807SJeff Garzik 
3318c6fd2807SJeff Garzik 	/* are we suspending? */
3319c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
3320c6fd2807SJeff Garzik 	if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
3321c6fd2807SJeff Garzik 	    ap->pm_mesg.event == PM_EVENT_ON) {
3322c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
3323c6fd2807SJeff Garzik 		return;
3324c6fd2807SJeff Garzik 	}
3325c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
3326c6fd2807SJeff Garzik 
3327c6fd2807SJeff Garzik 	WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
3328c6fd2807SJeff Garzik 
332964578a3dSTejun Heo 	/* tell ACPI we're suspending */
333064578a3dSTejun Heo 	rc = ata_acpi_on_suspend(ap);
333164578a3dSTejun Heo 	if (rc)
333264578a3dSTejun Heo 		goto out;
333364578a3dSTejun Heo 
3334c6fd2807SJeff Garzik 	/* suspend */
3335c6fd2807SJeff Garzik 	ata_eh_freeze_port(ap);
3336c6fd2807SJeff Garzik 
3337c6fd2807SJeff Garzik 	if (ap->ops->port_suspend)
3338c6fd2807SJeff Garzik 		rc = ap->ops->port_suspend(ap, ap->pm_mesg);
3339c6fd2807SJeff Garzik 
3340bd3adca5SShaohua Li 	ata_acpi_set_state(ap, PMSG_SUSPEND);
334164578a3dSTejun Heo  out:
3342c6fd2807SJeff Garzik 	/* report result */
3343c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
3344c6fd2807SJeff Garzik 
3345c6fd2807SJeff Garzik 	ap->pflags &= ~ATA_PFLAG_PM_PENDING;
3346c6fd2807SJeff Garzik 	if (rc == 0)
3347c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_SUSPENDED;
334864578a3dSTejun Heo 	else if (ap->pflags & ATA_PFLAG_FROZEN)
3349c6fd2807SJeff Garzik 		ata_port_schedule_eh(ap);
3350c6fd2807SJeff Garzik 
3351c6fd2807SJeff Garzik 	if (ap->pm_result) {
3352c6fd2807SJeff Garzik 		*ap->pm_result = rc;
3353c6fd2807SJeff Garzik 		ap->pm_result = NULL;
3354c6fd2807SJeff Garzik 	}
3355c6fd2807SJeff Garzik 
3356c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
3357c6fd2807SJeff Garzik 
3358c6fd2807SJeff Garzik 	return;
3359c6fd2807SJeff Garzik }
3360c6fd2807SJeff Garzik 
3361c6fd2807SJeff Garzik /**
3362c6fd2807SJeff Garzik  *	ata_eh_handle_port_resume - perform port resume operation
3363c6fd2807SJeff Garzik  *	@ap: port to resume
3364c6fd2807SJeff Garzik  *
3365c6fd2807SJeff Garzik  *	Resume @ap.
3366c6fd2807SJeff Garzik  *
3367c6fd2807SJeff Garzik  *	LOCKING:
3368c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
3369c6fd2807SJeff Garzik  */
3370c6fd2807SJeff Garzik static void ata_eh_handle_port_resume(struct ata_port *ap)
3371c6fd2807SJeff Garzik {
3372c6fd2807SJeff Garzik 	unsigned long flags;
33739666f400STejun Heo 	int rc = 0;
3374c6fd2807SJeff Garzik 
3375c6fd2807SJeff Garzik 	/* are we resuming? */
3376c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
3377c6fd2807SJeff Garzik 	if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
3378c6fd2807SJeff Garzik 	    ap->pm_mesg.event != PM_EVENT_ON) {
3379c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
3380c6fd2807SJeff Garzik 		return;
3381c6fd2807SJeff Garzik 	}
3382c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
3383c6fd2807SJeff Garzik 
33849666f400STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
3385c6fd2807SJeff Garzik 
3386bd3adca5SShaohua Li 	ata_acpi_set_state(ap, PMSG_ON);
3387bd3adca5SShaohua Li 
3388c6fd2807SJeff Garzik 	if (ap->ops->port_resume)
3389c6fd2807SJeff Garzik 		rc = ap->ops->port_resume(ap);
3390c6fd2807SJeff Garzik 
33916746544cSTejun Heo 	/* tell ACPI that we're resuming */
33926746544cSTejun Heo 	ata_acpi_on_resume(ap);
33936746544cSTejun Heo 
33949666f400STejun Heo 	/* report result */
3395c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
3396c6fd2807SJeff Garzik 	ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
3397c6fd2807SJeff Garzik 	if (ap->pm_result) {
3398c6fd2807SJeff Garzik 		*ap->pm_result = rc;
3399c6fd2807SJeff Garzik 		ap->pm_result = NULL;
3400c6fd2807SJeff Garzik 	}
3401c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
3402c6fd2807SJeff Garzik }
34036ffa01d8STejun Heo #endif /* CONFIG_PM */
3404