xref: /openbmc/linux/drivers/ata/libata-eh.c (revision a07d499b4759881db1359dd8812eecd00b0e0a28)
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;
4251eca4365STejun Heo 		ata_for_each_dev(tdev, link, ALL)
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) {
4331eca4365STejun Heo 			ata_for_each_dev(tdev, link, ALL)
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 
494ece180d1STejun Heo static void ata_eh_unload(struct ata_port *ap)
495ece180d1STejun Heo {
496ece180d1STejun Heo 	struct ata_link *link;
497ece180d1STejun Heo 	struct ata_device *dev;
498ece180d1STejun Heo 	unsigned long flags;
499ece180d1STejun Heo 
500ece180d1STejun Heo 	/* Restore SControl IPM and SPD for the next driver and
501ece180d1STejun Heo 	 * disable attached devices.
502ece180d1STejun Heo 	 */
503ece180d1STejun Heo 	ata_for_each_link(link, ap, PMP_FIRST) {
504ece180d1STejun Heo 		sata_scr_write(link, SCR_CONTROL, link->saved_scontrol & 0xff0);
505ece180d1STejun Heo 		ata_for_each_dev(dev, link, ALL)
506ece180d1STejun Heo 			ata_dev_disable(dev);
507ece180d1STejun Heo 	}
508ece180d1STejun Heo 
509ece180d1STejun Heo 	/* freeze and set UNLOADED */
510ece180d1STejun Heo 	spin_lock_irqsave(ap->lock, flags);
511ece180d1STejun Heo 
512ece180d1STejun Heo 	ata_port_freeze(ap);			/* won't be thawed */
513ece180d1STejun Heo 	ap->pflags &= ~ATA_PFLAG_EH_PENDING;	/* clear pending from freeze */
514ece180d1STejun Heo 	ap->pflags |= ATA_PFLAG_UNLOADED;
515ece180d1STejun Heo 
516ece180d1STejun Heo 	spin_unlock_irqrestore(ap->lock, flags);
517ece180d1STejun Heo }
518ece180d1STejun Heo 
519c6fd2807SJeff Garzik /**
520c6fd2807SJeff Garzik  *	ata_scsi_error - SCSI layer error handler callback
521c6fd2807SJeff Garzik  *	@host: SCSI host on which error occurred
522c6fd2807SJeff Garzik  *
523c6fd2807SJeff Garzik  *	Handles SCSI-layer-thrown error events.
524c6fd2807SJeff Garzik  *
525c6fd2807SJeff Garzik  *	LOCKING:
526c6fd2807SJeff Garzik  *	Inherited from SCSI layer (none, can sleep)
527c6fd2807SJeff Garzik  *
528c6fd2807SJeff Garzik  *	RETURNS:
529c6fd2807SJeff Garzik  *	Zero.
530c6fd2807SJeff Garzik  */
531c6fd2807SJeff Garzik void ata_scsi_error(struct Scsi_Host *host)
532c6fd2807SJeff Garzik {
533c6fd2807SJeff Garzik 	struct ata_port *ap = ata_shost_to_port(host);
534a1e10f7eSTejun Heo 	int i;
535c6fd2807SJeff Garzik 	unsigned long flags;
536c6fd2807SJeff Garzik 
537c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
538c6fd2807SJeff Garzik 
539c6fd2807SJeff Garzik 	/* synchronize with port task */
540c6fd2807SJeff Garzik 	ata_port_flush_task(ap);
541c6fd2807SJeff Garzik 
542cca3974eSJeff Garzik 	/* synchronize with host lock and sort out timeouts */
543c6fd2807SJeff Garzik 
544c6fd2807SJeff Garzik 	/* For new EH, all qcs are finished in one of three ways -
545c6fd2807SJeff Garzik 	 * normal completion, error completion, and SCSI timeout.
546c6fd2807SJeff Garzik 	 * Both cmpletions can race against SCSI timeout.  When normal
547c6fd2807SJeff Garzik 	 * completion wins, the qc never reaches EH.  When error
548c6fd2807SJeff Garzik 	 * completion wins, the qc has ATA_QCFLAG_FAILED set.
549c6fd2807SJeff Garzik 	 *
550c6fd2807SJeff Garzik 	 * When SCSI timeout wins, things are a bit more complex.
551c6fd2807SJeff Garzik 	 * Normal or error completion can occur after the timeout but
552c6fd2807SJeff Garzik 	 * before this point.  In such cases, both types of
553c6fd2807SJeff Garzik 	 * completions are honored.  A scmd is determined to have
554c6fd2807SJeff Garzik 	 * timed out iff its associated qc is active and not failed.
555c6fd2807SJeff Garzik 	 */
556c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
557c6fd2807SJeff Garzik 		struct scsi_cmnd *scmd, *tmp;
558c6fd2807SJeff Garzik 		int nr_timedout = 0;
559c6fd2807SJeff Garzik 
560c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
561c6fd2807SJeff Garzik 
562c6fd2807SJeff Garzik 		list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
563c6fd2807SJeff Garzik 			struct ata_queued_cmd *qc;
564c6fd2807SJeff Garzik 
565c6fd2807SJeff Garzik 			for (i = 0; i < ATA_MAX_QUEUE; i++) {
566c6fd2807SJeff Garzik 				qc = __ata_qc_from_tag(ap, i);
567c6fd2807SJeff Garzik 				if (qc->flags & ATA_QCFLAG_ACTIVE &&
568c6fd2807SJeff Garzik 				    qc->scsicmd == scmd)
569c6fd2807SJeff Garzik 					break;
570c6fd2807SJeff Garzik 			}
571c6fd2807SJeff Garzik 
572c6fd2807SJeff Garzik 			if (i < ATA_MAX_QUEUE) {
573c6fd2807SJeff Garzik 				/* the scmd has an associated qc */
574c6fd2807SJeff Garzik 				if (!(qc->flags & ATA_QCFLAG_FAILED)) {
575c6fd2807SJeff Garzik 					/* which hasn't failed yet, timeout */
576c6fd2807SJeff Garzik 					qc->err_mask |= AC_ERR_TIMEOUT;
577c6fd2807SJeff Garzik 					qc->flags |= ATA_QCFLAG_FAILED;
578c6fd2807SJeff Garzik 					nr_timedout++;
579c6fd2807SJeff Garzik 				}
580c6fd2807SJeff Garzik 			} else {
581c6fd2807SJeff Garzik 				/* Normal completion occurred after
582c6fd2807SJeff Garzik 				 * SCSI timeout but before this point.
583c6fd2807SJeff Garzik 				 * Successfully complete it.
584c6fd2807SJeff Garzik 				 */
585c6fd2807SJeff Garzik 				scmd->retries = scmd->allowed;
586c6fd2807SJeff Garzik 				scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
587c6fd2807SJeff Garzik 			}
588c6fd2807SJeff Garzik 		}
589c6fd2807SJeff Garzik 
590c6fd2807SJeff Garzik 		/* If we have timed out qcs.  They belong to EH from
591c6fd2807SJeff Garzik 		 * this point but the state of the controller is
592c6fd2807SJeff Garzik 		 * unknown.  Freeze the port to make sure the IRQ
593c6fd2807SJeff Garzik 		 * handler doesn't diddle with those qcs.  This must
594c6fd2807SJeff Garzik 		 * be done atomically w.r.t. setting QCFLAG_FAILED.
595c6fd2807SJeff Garzik 		 */
596c6fd2807SJeff Garzik 		if (nr_timedout)
597c6fd2807SJeff Garzik 			__ata_port_freeze(ap);
598c6fd2807SJeff Garzik 
599c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
600a1e10f7eSTejun Heo 
601a1e10f7eSTejun Heo 		/* initialize eh_tries */
602a1e10f7eSTejun Heo 		ap->eh_tries = ATA_EH_MAX_TRIES;
603c6fd2807SJeff Garzik 	} else
604c6fd2807SJeff Garzik 		spin_unlock_wait(ap->lock);
605c6fd2807SJeff Garzik 
606c6fd2807SJeff Garzik  repeat:
607c6fd2807SJeff Garzik 	/* invoke error handler */
608c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
609cf1b86c8STejun Heo 		struct ata_link *link;
610cf1b86c8STejun Heo 
6115ddf24c5STejun Heo 		/* kill fast drain timer */
6125ddf24c5STejun Heo 		del_timer_sync(&ap->fastdrain_timer);
6135ddf24c5STejun Heo 
614c6fd2807SJeff Garzik 		/* process port resume request */
615c6fd2807SJeff Garzik 		ata_eh_handle_port_resume(ap);
616c6fd2807SJeff Garzik 
617c6fd2807SJeff Garzik 		/* fetch & clear EH info */
618c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
619c6fd2807SJeff Garzik 
6201eca4365STejun Heo 		ata_for_each_link(link, ap, HOST_FIRST) {
62100115e0fSTejun Heo 			struct ata_eh_context *ehc = &link->eh_context;
62200115e0fSTejun Heo 			struct ata_device *dev;
62300115e0fSTejun Heo 
624cf1b86c8STejun Heo 			memset(&link->eh_context, 0, sizeof(link->eh_context));
625cf1b86c8STejun Heo 			link->eh_context.i = link->eh_info;
626cf1b86c8STejun Heo 			memset(&link->eh_info, 0, sizeof(link->eh_info));
62700115e0fSTejun Heo 
6281eca4365STejun Heo 			ata_for_each_dev(dev, link, ENABLED) {
62900115e0fSTejun Heo 				int devno = dev->devno;
63000115e0fSTejun Heo 
63100115e0fSTejun Heo 				ehc->saved_xfer_mode[devno] = dev->xfer_mode;
63200115e0fSTejun Heo 				if (ata_ncq_enabled(dev))
63300115e0fSTejun Heo 					ehc->saved_ncq_enabled |= 1 << devno;
63400115e0fSTejun Heo 			}
635cf1b86c8STejun Heo 		}
636c6fd2807SJeff Garzik 
637c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
638c6fd2807SJeff Garzik 		ap->pflags &= ~ATA_PFLAG_EH_PENDING;
639da917d69STejun Heo 		ap->excl_link = NULL;	/* don't maintain exclusion over EH */
640c6fd2807SJeff Garzik 
641c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
642c6fd2807SJeff Garzik 
643c6fd2807SJeff Garzik 		/* invoke EH, skip if unloading or suspended */
644c6fd2807SJeff Garzik 		if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
645c6fd2807SJeff Garzik 			ap->ops->error_handler(ap);
646ece180d1STejun Heo 		else {
647ece180d1STejun Heo 			/* if unloading, commence suicide */
648ece180d1STejun Heo 			if ((ap->pflags & ATA_PFLAG_UNLOADING) &&
649ece180d1STejun Heo 			    !(ap->pflags & ATA_PFLAG_UNLOADED))
650ece180d1STejun Heo 				ata_eh_unload(ap);
651c6fd2807SJeff Garzik 			ata_eh_finish(ap);
652ece180d1STejun Heo 		}
653c6fd2807SJeff Garzik 
654c6fd2807SJeff Garzik 		/* process port suspend request */
655c6fd2807SJeff Garzik 		ata_eh_handle_port_suspend(ap);
656c6fd2807SJeff Garzik 
657c6fd2807SJeff Garzik 		/* Exception might have happend after ->error_handler
658c6fd2807SJeff Garzik 		 * recovered the port but before this point.  Repeat
659c6fd2807SJeff Garzik 		 * EH in such case.
660c6fd2807SJeff Garzik 		 */
661c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
662c6fd2807SJeff Garzik 
663c6fd2807SJeff Garzik 		if (ap->pflags & ATA_PFLAG_EH_PENDING) {
664a1e10f7eSTejun Heo 			if (--ap->eh_tries) {
665c6fd2807SJeff Garzik 				spin_unlock_irqrestore(ap->lock, flags);
666c6fd2807SJeff Garzik 				goto repeat;
667c6fd2807SJeff Garzik 			}
668c6fd2807SJeff Garzik 			ata_port_printk(ap, KERN_ERR, "EH pending after %d "
669a1e10f7eSTejun Heo 					"tries, giving up\n", ATA_EH_MAX_TRIES);
670914616a3STejun Heo 			ap->pflags &= ~ATA_PFLAG_EH_PENDING;
671c6fd2807SJeff Garzik 		}
672c6fd2807SJeff Garzik 
673c6fd2807SJeff Garzik 		/* this run is complete, make sure EH info is clear */
6741eca4365STejun Heo 		ata_for_each_link(link, ap, HOST_FIRST)
675cf1b86c8STejun Heo 			memset(&link->eh_info, 0, sizeof(link->eh_info));
676c6fd2807SJeff Garzik 
677c6fd2807SJeff Garzik 		/* Clear host_eh_scheduled while holding ap->lock such
678c6fd2807SJeff Garzik 		 * that if exception occurs after this point but
679c6fd2807SJeff Garzik 		 * before EH completion, SCSI midlayer will
680c6fd2807SJeff Garzik 		 * re-initiate EH.
681c6fd2807SJeff Garzik 		 */
682c6fd2807SJeff Garzik 		host->host_eh_scheduled = 0;
683c6fd2807SJeff Garzik 
684c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
685c6fd2807SJeff Garzik 	} else {
6869af5c9c9STejun Heo 		WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
687c6fd2807SJeff Garzik 		ap->ops->eng_timeout(ap);
688c6fd2807SJeff Garzik 	}
689c6fd2807SJeff Garzik 
690c6fd2807SJeff Garzik 	/* finish or retry handled scmd's and clean up */
691c6fd2807SJeff Garzik 	WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
692c6fd2807SJeff Garzik 
693c6fd2807SJeff Garzik 	scsi_eh_flush_done_q(&ap->eh_done_q);
694c6fd2807SJeff Garzik 
695c6fd2807SJeff Garzik 	/* clean up */
696c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
697c6fd2807SJeff Garzik 
698c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_LOADING)
699c6fd2807SJeff Garzik 		ap->pflags &= ~ATA_PFLAG_LOADING;
700c6fd2807SJeff Garzik 	else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
70152bad64dSDavid Howells 		queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0);
702c6fd2807SJeff Garzik 
703c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_RECOVERED)
704c6fd2807SJeff Garzik 		ata_port_printk(ap, KERN_INFO, "EH complete\n");
705c6fd2807SJeff Garzik 
706c6fd2807SJeff Garzik 	ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
707c6fd2807SJeff Garzik 
708c6fd2807SJeff Garzik 	/* tell wait_eh that we're done */
709c6fd2807SJeff Garzik 	ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
710c6fd2807SJeff Garzik 	wake_up_all(&ap->eh_wait_q);
711c6fd2807SJeff Garzik 
712c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
713c6fd2807SJeff Garzik 
714c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
715c6fd2807SJeff Garzik }
716c6fd2807SJeff Garzik 
717c6fd2807SJeff Garzik /**
718c6fd2807SJeff Garzik  *	ata_port_wait_eh - Wait for the currently pending EH to complete
719c6fd2807SJeff Garzik  *	@ap: Port to wait EH for
720c6fd2807SJeff Garzik  *
721c6fd2807SJeff Garzik  *	Wait until the currently pending EH is complete.
722c6fd2807SJeff Garzik  *
723c6fd2807SJeff Garzik  *	LOCKING:
724c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
725c6fd2807SJeff Garzik  */
726c6fd2807SJeff Garzik void ata_port_wait_eh(struct ata_port *ap)
727c6fd2807SJeff Garzik {
728c6fd2807SJeff Garzik 	unsigned long flags;
729c6fd2807SJeff Garzik 	DEFINE_WAIT(wait);
730c6fd2807SJeff Garzik 
731c6fd2807SJeff Garzik  retry:
732c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
733c6fd2807SJeff Garzik 
734c6fd2807SJeff Garzik 	while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
735c6fd2807SJeff Garzik 		prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
736c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
737c6fd2807SJeff Garzik 		schedule();
738c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
739c6fd2807SJeff Garzik 	}
740c6fd2807SJeff Garzik 	finish_wait(&ap->eh_wait_q, &wait);
741c6fd2807SJeff Garzik 
742c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
743c6fd2807SJeff Garzik 
744c6fd2807SJeff Garzik 	/* make sure SCSI EH is complete */
745cca3974eSJeff Garzik 	if (scsi_host_in_recovery(ap->scsi_host)) {
746c6fd2807SJeff Garzik 		msleep(10);
747c6fd2807SJeff Garzik 		goto retry;
748c6fd2807SJeff Garzik 	}
749c6fd2807SJeff Garzik }
750c6fd2807SJeff Garzik 
7515ddf24c5STejun Heo static int ata_eh_nr_in_flight(struct ata_port *ap)
7525ddf24c5STejun Heo {
7535ddf24c5STejun Heo 	unsigned int tag;
7545ddf24c5STejun Heo 	int nr = 0;
7555ddf24c5STejun Heo 
7565ddf24c5STejun Heo 	/* count only non-internal commands */
7575ddf24c5STejun Heo 	for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++)
7585ddf24c5STejun Heo 		if (ata_qc_from_tag(ap, tag))
7595ddf24c5STejun Heo 			nr++;
7605ddf24c5STejun Heo 
7615ddf24c5STejun Heo 	return nr;
7625ddf24c5STejun Heo }
7635ddf24c5STejun Heo 
7645ddf24c5STejun Heo void ata_eh_fastdrain_timerfn(unsigned long arg)
7655ddf24c5STejun Heo {
7665ddf24c5STejun Heo 	struct ata_port *ap = (void *)arg;
7675ddf24c5STejun Heo 	unsigned long flags;
7685ddf24c5STejun Heo 	int cnt;
7695ddf24c5STejun Heo 
7705ddf24c5STejun Heo 	spin_lock_irqsave(ap->lock, flags);
7715ddf24c5STejun Heo 
7725ddf24c5STejun Heo 	cnt = ata_eh_nr_in_flight(ap);
7735ddf24c5STejun Heo 
7745ddf24c5STejun Heo 	/* are we done? */
7755ddf24c5STejun Heo 	if (!cnt)
7765ddf24c5STejun Heo 		goto out_unlock;
7775ddf24c5STejun Heo 
7785ddf24c5STejun Heo 	if (cnt == ap->fastdrain_cnt) {
7795ddf24c5STejun Heo 		unsigned int tag;
7805ddf24c5STejun Heo 
7815ddf24c5STejun Heo 		/* No progress during the last interval, tag all
7825ddf24c5STejun Heo 		 * in-flight qcs as timed out and freeze the port.
7835ddf24c5STejun Heo 		 */
7845ddf24c5STejun Heo 		for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) {
7855ddf24c5STejun Heo 			struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
7865ddf24c5STejun Heo 			if (qc)
7875ddf24c5STejun Heo 				qc->err_mask |= AC_ERR_TIMEOUT;
7885ddf24c5STejun Heo 		}
7895ddf24c5STejun Heo 
7905ddf24c5STejun Heo 		ata_port_freeze(ap);
7915ddf24c5STejun Heo 	} else {
7925ddf24c5STejun Heo 		/* some qcs have finished, give it another chance */
7935ddf24c5STejun Heo 		ap->fastdrain_cnt = cnt;
7945ddf24c5STejun Heo 		ap->fastdrain_timer.expires =
795341c2c95STejun Heo 			ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
7965ddf24c5STejun Heo 		add_timer(&ap->fastdrain_timer);
7975ddf24c5STejun Heo 	}
7985ddf24c5STejun Heo 
7995ddf24c5STejun Heo  out_unlock:
8005ddf24c5STejun Heo 	spin_unlock_irqrestore(ap->lock, flags);
8015ddf24c5STejun Heo }
8025ddf24c5STejun Heo 
8035ddf24c5STejun Heo /**
8045ddf24c5STejun Heo  *	ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
8055ddf24c5STejun Heo  *	@ap: target ATA port
8065ddf24c5STejun Heo  *	@fastdrain: activate fast drain
8075ddf24c5STejun Heo  *
8085ddf24c5STejun Heo  *	Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
8095ddf24c5STejun Heo  *	is non-zero and EH wasn't pending before.  Fast drain ensures
8105ddf24c5STejun Heo  *	that EH kicks in in timely manner.
8115ddf24c5STejun Heo  *
8125ddf24c5STejun Heo  *	LOCKING:
8135ddf24c5STejun Heo  *	spin_lock_irqsave(host lock)
8145ddf24c5STejun Heo  */
8155ddf24c5STejun Heo static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
8165ddf24c5STejun Heo {
8175ddf24c5STejun Heo 	int cnt;
8185ddf24c5STejun Heo 
8195ddf24c5STejun Heo 	/* already scheduled? */
8205ddf24c5STejun Heo 	if (ap->pflags & ATA_PFLAG_EH_PENDING)
8215ddf24c5STejun Heo 		return;
8225ddf24c5STejun Heo 
8235ddf24c5STejun Heo 	ap->pflags |= ATA_PFLAG_EH_PENDING;
8245ddf24c5STejun Heo 
8255ddf24c5STejun Heo 	if (!fastdrain)
8265ddf24c5STejun Heo 		return;
8275ddf24c5STejun Heo 
8285ddf24c5STejun Heo 	/* do we have in-flight qcs? */
8295ddf24c5STejun Heo 	cnt = ata_eh_nr_in_flight(ap);
8305ddf24c5STejun Heo 	if (!cnt)
8315ddf24c5STejun Heo 		return;
8325ddf24c5STejun Heo 
8335ddf24c5STejun Heo 	/* activate fast drain */
8345ddf24c5STejun Heo 	ap->fastdrain_cnt = cnt;
835341c2c95STejun Heo 	ap->fastdrain_timer.expires =
836341c2c95STejun Heo 		ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
8375ddf24c5STejun Heo 	add_timer(&ap->fastdrain_timer);
8385ddf24c5STejun Heo }
8395ddf24c5STejun Heo 
840c6fd2807SJeff Garzik /**
841c6fd2807SJeff Garzik  *	ata_qc_schedule_eh - schedule qc for error handling
842c6fd2807SJeff Garzik  *	@qc: command to schedule error handling for
843c6fd2807SJeff Garzik  *
844c6fd2807SJeff Garzik  *	Schedule error handling for @qc.  EH will kick in as soon as
845c6fd2807SJeff Garzik  *	other commands are drained.
846c6fd2807SJeff Garzik  *
847c6fd2807SJeff Garzik  *	LOCKING:
848cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
849c6fd2807SJeff Garzik  */
850c6fd2807SJeff Garzik void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
851c6fd2807SJeff Garzik {
852c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
853c6fd2807SJeff Garzik 
854c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
855c6fd2807SJeff Garzik 
856c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_FAILED;
8575ddf24c5STejun Heo 	ata_eh_set_pending(ap, 1);
858c6fd2807SJeff Garzik 
859c6fd2807SJeff Garzik 	/* The following will fail if timeout has already expired.
860c6fd2807SJeff Garzik 	 * ata_scsi_error() takes care of such scmds on EH entry.
861c6fd2807SJeff Garzik 	 * Note that ATA_QCFLAG_FAILED is unconditionally set after
862c6fd2807SJeff Garzik 	 * this function completes.
863c6fd2807SJeff Garzik 	 */
864242f9dcbSJens Axboe 	blk_abort_request(qc->scsicmd->request);
865c6fd2807SJeff Garzik }
866c6fd2807SJeff Garzik 
867c6fd2807SJeff Garzik /**
868c6fd2807SJeff Garzik  *	ata_port_schedule_eh - schedule error handling without a qc
869c6fd2807SJeff Garzik  *	@ap: ATA port to schedule EH for
870c6fd2807SJeff Garzik  *
871c6fd2807SJeff Garzik  *	Schedule error handling for @ap.  EH will kick in as soon as
872c6fd2807SJeff Garzik  *	all commands are drained.
873c6fd2807SJeff Garzik  *
874c6fd2807SJeff Garzik  *	LOCKING:
875cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
876c6fd2807SJeff Garzik  */
877c6fd2807SJeff Garzik void ata_port_schedule_eh(struct ata_port *ap)
878c6fd2807SJeff Garzik {
879c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
880c6fd2807SJeff Garzik 
881f4d6d004STejun Heo 	if (ap->pflags & ATA_PFLAG_INITIALIZING)
882f4d6d004STejun Heo 		return;
883f4d6d004STejun Heo 
8845ddf24c5STejun Heo 	ata_eh_set_pending(ap, 1);
885cca3974eSJeff Garzik 	scsi_schedule_eh(ap->scsi_host);
886c6fd2807SJeff Garzik 
887c6fd2807SJeff Garzik 	DPRINTK("port EH scheduled\n");
888c6fd2807SJeff Garzik }
889c6fd2807SJeff Garzik 
890dbd82616STejun Heo static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
891c6fd2807SJeff Garzik {
892c6fd2807SJeff Garzik 	int tag, nr_aborted = 0;
893c6fd2807SJeff Garzik 
894c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
895c6fd2807SJeff Garzik 
8965ddf24c5STejun Heo 	/* we're gonna abort all commands, no need for fast drain */
8975ddf24c5STejun Heo 	ata_eh_set_pending(ap, 0);
8985ddf24c5STejun Heo 
899c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
900c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
901c6fd2807SJeff Garzik 
902dbd82616STejun Heo 		if (qc && (!link || qc->dev->link == link)) {
903c6fd2807SJeff Garzik 			qc->flags |= ATA_QCFLAG_FAILED;
904c6fd2807SJeff Garzik 			ata_qc_complete(qc);
905c6fd2807SJeff Garzik 			nr_aborted++;
906c6fd2807SJeff Garzik 		}
907c6fd2807SJeff Garzik 	}
908c6fd2807SJeff Garzik 
909c6fd2807SJeff Garzik 	if (!nr_aborted)
910c6fd2807SJeff Garzik 		ata_port_schedule_eh(ap);
911c6fd2807SJeff Garzik 
912c6fd2807SJeff Garzik 	return nr_aborted;
913c6fd2807SJeff Garzik }
914c6fd2807SJeff Garzik 
915c6fd2807SJeff Garzik /**
916dbd82616STejun Heo  *	ata_link_abort - abort all qc's on the link
917dbd82616STejun Heo  *	@link: ATA link to abort qc's for
918dbd82616STejun Heo  *
919dbd82616STejun Heo  *	Abort all active qc's active on @link and schedule EH.
920dbd82616STejun Heo  *
921dbd82616STejun Heo  *	LOCKING:
922dbd82616STejun Heo  *	spin_lock_irqsave(host lock)
923dbd82616STejun Heo  *
924dbd82616STejun Heo  *	RETURNS:
925dbd82616STejun Heo  *	Number of aborted qc's.
926dbd82616STejun Heo  */
927dbd82616STejun Heo int ata_link_abort(struct ata_link *link)
928dbd82616STejun Heo {
929dbd82616STejun Heo 	return ata_do_link_abort(link->ap, link);
930dbd82616STejun Heo }
931dbd82616STejun Heo 
932dbd82616STejun Heo /**
933dbd82616STejun Heo  *	ata_port_abort - abort all qc's on the port
934dbd82616STejun Heo  *	@ap: ATA port to abort qc's for
935dbd82616STejun Heo  *
936dbd82616STejun Heo  *	Abort all active qc's of @ap and schedule EH.
937dbd82616STejun Heo  *
938dbd82616STejun Heo  *	LOCKING:
939dbd82616STejun Heo  *	spin_lock_irqsave(host_set lock)
940dbd82616STejun Heo  *
941dbd82616STejun Heo  *	RETURNS:
942dbd82616STejun Heo  *	Number of aborted qc's.
943dbd82616STejun Heo  */
944dbd82616STejun Heo int ata_port_abort(struct ata_port *ap)
945dbd82616STejun Heo {
946dbd82616STejun Heo 	return ata_do_link_abort(ap, NULL);
947dbd82616STejun Heo }
948dbd82616STejun Heo 
949dbd82616STejun Heo /**
950c6fd2807SJeff Garzik  *	__ata_port_freeze - freeze port
951c6fd2807SJeff Garzik  *	@ap: ATA port to freeze
952c6fd2807SJeff Garzik  *
953c6fd2807SJeff Garzik  *	This function is called when HSM violation or some other
954c6fd2807SJeff Garzik  *	condition disrupts normal operation of the port.  Frozen port
955c6fd2807SJeff Garzik  *	is not allowed to perform any operation until the port is
956c6fd2807SJeff Garzik  *	thawed, which usually follows a successful reset.
957c6fd2807SJeff Garzik  *
958c6fd2807SJeff Garzik  *	ap->ops->freeze() callback can be used for freezing the port
959c6fd2807SJeff Garzik  *	hardware-wise (e.g. mask interrupt and stop DMA engine).  If a
960c6fd2807SJeff Garzik  *	port cannot be frozen hardware-wise, the interrupt handler
961c6fd2807SJeff Garzik  *	must ack and clear interrupts unconditionally while the port
962c6fd2807SJeff Garzik  *	is frozen.
963c6fd2807SJeff Garzik  *
964c6fd2807SJeff Garzik  *	LOCKING:
965cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
966c6fd2807SJeff Garzik  */
967c6fd2807SJeff Garzik static void __ata_port_freeze(struct ata_port *ap)
968c6fd2807SJeff Garzik {
969c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
970c6fd2807SJeff Garzik 
971c6fd2807SJeff Garzik 	if (ap->ops->freeze)
972c6fd2807SJeff Garzik 		ap->ops->freeze(ap);
973c6fd2807SJeff Garzik 
974c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_FROZEN;
975c6fd2807SJeff Garzik 
97644877b4eSTejun Heo 	DPRINTK("ata%u port frozen\n", ap->print_id);
977c6fd2807SJeff Garzik }
978c6fd2807SJeff Garzik 
979c6fd2807SJeff Garzik /**
980c6fd2807SJeff Garzik  *	ata_port_freeze - abort & freeze port
981c6fd2807SJeff Garzik  *	@ap: ATA port to freeze
982c6fd2807SJeff Garzik  *
983c6fd2807SJeff Garzik  *	Abort and freeze @ap.
984c6fd2807SJeff Garzik  *
985c6fd2807SJeff Garzik  *	LOCKING:
986cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
987c6fd2807SJeff Garzik  *
988c6fd2807SJeff Garzik  *	RETURNS:
989c6fd2807SJeff Garzik  *	Number of aborted commands.
990c6fd2807SJeff Garzik  */
991c6fd2807SJeff Garzik int ata_port_freeze(struct ata_port *ap)
992c6fd2807SJeff Garzik {
993c6fd2807SJeff Garzik 	int nr_aborted;
994c6fd2807SJeff Garzik 
995c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
996c6fd2807SJeff Garzik 
997c6fd2807SJeff Garzik 	nr_aborted = ata_port_abort(ap);
998c6fd2807SJeff Garzik 	__ata_port_freeze(ap);
999c6fd2807SJeff Garzik 
1000c6fd2807SJeff Garzik 	return nr_aborted;
1001c6fd2807SJeff Garzik }
1002c6fd2807SJeff Garzik 
1003c6fd2807SJeff Garzik /**
10047d77b247STejun Heo  *	sata_async_notification - SATA async notification handler
10057d77b247STejun Heo  *	@ap: ATA port where async notification is received
10067d77b247STejun Heo  *
10077d77b247STejun Heo  *	Handler to be called when async notification via SDB FIS is
10087d77b247STejun Heo  *	received.  This function schedules EH if necessary.
10097d77b247STejun Heo  *
10107d77b247STejun Heo  *	LOCKING:
10117d77b247STejun Heo  *	spin_lock_irqsave(host lock)
10127d77b247STejun Heo  *
10137d77b247STejun Heo  *	RETURNS:
10147d77b247STejun Heo  *	1 if EH is scheduled, 0 otherwise.
10157d77b247STejun Heo  */
10167d77b247STejun Heo int sata_async_notification(struct ata_port *ap)
10177d77b247STejun Heo {
10187d77b247STejun Heo 	u32 sntf;
10197d77b247STejun Heo 	int rc;
10207d77b247STejun Heo 
10217d77b247STejun Heo 	if (!(ap->flags & ATA_FLAG_AN))
10227d77b247STejun Heo 		return 0;
10237d77b247STejun Heo 
10247d77b247STejun Heo 	rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
10257d77b247STejun Heo 	if (rc == 0)
10267d77b247STejun Heo 		sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
10277d77b247STejun Heo 
1028071f44b1STejun Heo 	if (!sata_pmp_attached(ap) || rc) {
10297d77b247STejun Heo 		/* PMP is not attached or SNTF is not available */
1030071f44b1STejun Heo 		if (!sata_pmp_attached(ap)) {
10317d77b247STejun Heo 			/* PMP is not attached.  Check whether ATAPI
10327d77b247STejun Heo 			 * AN is configured.  If so, notify media
10337d77b247STejun Heo 			 * change.
10347d77b247STejun Heo 			 */
10357d77b247STejun Heo 			struct ata_device *dev = ap->link.device;
10367d77b247STejun Heo 
10377d77b247STejun Heo 			if ((dev->class == ATA_DEV_ATAPI) &&
10387d77b247STejun Heo 			    (dev->flags & ATA_DFLAG_AN))
10397d77b247STejun Heo 				ata_scsi_media_change_notify(dev);
10407d77b247STejun Heo 			return 0;
10417d77b247STejun Heo 		} else {
10427d77b247STejun Heo 			/* PMP is attached but SNTF is not available.
10437d77b247STejun Heo 			 * ATAPI async media change notification is
10447d77b247STejun Heo 			 * not used.  The PMP must be reporting PHY
10457d77b247STejun Heo 			 * status change, schedule EH.
10467d77b247STejun Heo 			 */
10477d77b247STejun Heo 			ata_port_schedule_eh(ap);
10487d77b247STejun Heo 			return 1;
10497d77b247STejun Heo 		}
10507d77b247STejun Heo 	} else {
10517d77b247STejun Heo 		/* PMP is attached and SNTF is available */
10527d77b247STejun Heo 		struct ata_link *link;
10537d77b247STejun Heo 
10547d77b247STejun Heo 		/* check and notify ATAPI AN */
10551eca4365STejun Heo 		ata_for_each_link(link, ap, EDGE) {
10567d77b247STejun Heo 			if (!(sntf & (1 << link->pmp)))
10577d77b247STejun Heo 				continue;
10587d77b247STejun Heo 
10597d77b247STejun Heo 			if ((link->device->class == ATA_DEV_ATAPI) &&
10607d77b247STejun Heo 			    (link->device->flags & ATA_DFLAG_AN))
10617d77b247STejun Heo 				ata_scsi_media_change_notify(link->device);
10627d77b247STejun Heo 		}
10637d77b247STejun Heo 
10647d77b247STejun Heo 		/* If PMP is reporting that PHY status of some
10657d77b247STejun Heo 		 * downstream ports has changed, schedule EH.
10667d77b247STejun Heo 		 */
10677d77b247STejun Heo 		if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
10687d77b247STejun Heo 			ata_port_schedule_eh(ap);
10697d77b247STejun Heo 			return 1;
10707d77b247STejun Heo 		}
10717d77b247STejun Heo 
10727d77b247STejun Heo 		return 0;
10737d77b247STejun Heo 	}
10747d77b247STejun Heo }
10757d77b247STejun Heo 
10767d77b247STejun Heo /**
1077c6fd2807SJeff Garzik  *	ata_eh_freeze_port - EH helper to freeze port
1078c6fd2807SJeff Garzik  *	@ap: ATA port to freeze
1079c6fd2807SJeff Garzik  *
1080c6fd2807SJeff Garzik  *	Freeze @ap.
1081c6fd2807SJeff Garzik  *
1082c6fd2807SJeff Garzik  *	LOCKING:
1083c6fd2807SJeff Garzik  *	None.
1084c6fd2807SJeff Garzik  */
1085c6fd2807SJeff Garzik void ata_eh_freeze_port(struct ata_port *ap)
1086c6fd2807SJeff Garzik {
1087c6fd2807SJeff Garzik 	unsigned long flags;
1088c6fd2807SJeff Garzik 
1089c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
1090c6fd2807SJeff Garzik 		return;
1091c6fd2807SJeff Garzik 
1092c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1093c6fd2807SJeff Garzik 	__ata_port_freeze(ap);
1094c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1095c6fd2807SJeff Garzik }
1096c6fd2807SJeff Garzik 
1097c6fd2807SJeff Garzik /**
1098c6fd2807SJeff Garzik  *	ata_port_thaw_port - EH helper to thaw port
1099c6fd2807SJeff Garzik  *	@ap: ATA port to thaw
1100c6fd2807SJeff Garzik  *
1101c6fd2807SJeff Garzik  *	Thaw frozen port @ap.
1102c6fd2807SJeff Garzik  *
1103c6fd2807SJeff Garzik  *	LOCKING:
1104c6fd2807SJeff Garzik  *	None.
1105c6fd2807SJeff Garzik  */
1106c6fd2807SJeff Garzik void ata_eh_thaw_port(struct ata_port *ap)
1107c6fd2807SJeff Garzik {
1108c6fd2807SJeff Garzik 	unsigned long flags;
1109c6fd2807SJeff Garzik 
1110c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
1111c6fd2807SJeff Garzik 		return;
1112c6fd2807SJeff Garzik 
1113c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1114c6fd2807SJeff Garzik 
1115c6fd2807SJeff Garzik 	ap->pflags &= ~ATA_PFLAG_FROZEN;
1116c6fd2807SJeff Garzik 
1117c6fd2807SJeff Garzik 	if (ap->ops->thaw)
1118c6fd2807SJeff Garzik 		ap->ops->thaw(ap);
1119c6fd2807SJeff Garzik 
1120c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1121c6fd2807SJeff Garzik 
112244877b4eSTejun Heo 	DPRINTK("ata%u port thawed\n", ap->print_id);
1123c6fd2807SJeff Garzik }
1124c6fd2807SJeff Garzik 
1125c6fd2807SJeff Garzik static void ata_eh_scsidone(struct scsi_cmnd *scmd)
1126c6fd2807SJeff Garzik {
1127c6fd2807SJeff Garzik 	/* nada */
1128c6fd2807SJeff Garzik }
1129c6fd2807SJeff Garzik 
1130c6fd2807SJeff Garzik static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
1131c6fd2807SJeff Garzik {
1132c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
1133c6fd2807SJeff Garzik 	struct scsi_cmnd *scmd = qc->scsicmd;
1134c6fd2807SJeff Garzik 	unsigned long flags;
1135c6fd2807SJeff Garzik 
1136c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1137c6fd2807SJeff Garzik 	qc->scsidone = ata_eh_scsidone;
1138c6fd2807SJeff Garzik 	__ata_qc_complete(qc);
1139c6fd2807SJeff Garzik 	WARN_ON(ata_tag_valid(qc->tag));
1140c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1141c6fd2807SJeff Garzik 
1142c6fd2807SJeff Garzik 	scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
1143c6fd2807SJeff Garzik }
1144c6fd2807SJeff Garzik 
1145c6fd2807SJeff Garzik /**
1146c6fd2807SJeff Garzik  *	ata_eh_qc_complete - Complete an active ATA command from EH
1147c6fd2807SJeff Garzik  *	@qc: Command to complete
1148c6fd2807SJeff Garzik  *
1149c6fd2807SJeff Garzik  *	Indicate to the mid and upper layers that an ATA command has
1150c6fd2807SJeff Garzik  *	completed.  To be used from EH.
1151c6fd2807SJeff Garzik  */
1152c6fd2807SJeff Garzik void ata_eh_qc_complete(struct ata_queued_cmd *qc)
1153c6fd2807SJeff Garzik {
1154c6fd2807SJeff Garzik 	struct scsi_cmnd *scmd = qc->scsicmd;
1155c6fd2807SJeff Garzik 	scmd->retries = scmd->allowed;
1156c6fd2807SJeff Garzik 	__ata_eh_qc_complete(qc);
1157c6fd2807SJeff Garzik }
1158c6fd2807SJeff Garzik 
1159c6fd2807SJeff Garzik /**
1160c6fd2807SJeff Garzik  *	ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
1161c6fd2807SJeff Garzik  *	@qc: Command to retry
1162c6fd2807SJeff Garzik  *
1163c6fd2807SJeff Garzik  *	Indicate to the mid and upper layers that an ATA command
1164c6fd2807SJeff Garzik  *	should be retried.  To be used from EH.
1165c6fd2807SJeff Garzik  *
1166c6fd2807SJeff Garzik  *	SCSI midlayer limits the number of retries to scmd->allowed.
1167c6fd2807SJeff Garzik  *	scmd->retries is decremented for commands which get retried
1168c6fd2807SJeff Garzik  *	due to unrelated failures (qc->err_mask is zero).
1169c6fd2807SJeff Garzik  */
1170c6fd2807SJeff Garzik void ata_eh_qc_retry(struct ata_queued_cmd *qc)
1171c6fd2807SJeff Garzik {
1172c6fd2807SJeff Garzik 	struct scsi_cmnd *scmd = qc->scsicmd;
1173c6fd2807SJeff Garzik 	if (!qc->err_mask && scmd->retries)
1174c6fd2807SJeff Garzik 		scmd->retries--;
1175c6fd2807SJeff Garzik 	__ata_eh_qc_complete(qc);
1176c6fd2807SJeff Garzik }
1177c6fd2807SJeff Garzik 
1178c6fd2807SJeff Garzik /**
1179678afac6STejun Heo  *	ata_dev_disable - disable ATA device
1180678afac6STejun Heo  *	@dev: ATA device to disable
1181678afac6STejun Heo  *
1182678afac6STejun Heo  *	Disable @dev.
1183678afac6STejun Heo  *
1184678afac6STejun Heo  *	Locking:
1185678afac6STejun Heo  *	EH context.
1186678afac6STejun Heo  */
1187678afac6STejun Heo void ata_dev_disable(struct ata_device *dev)
1188678afac6STejun Heo {
1189678afac6STejun Heo 	if (!ata_dev_enabled(dev))
1190678afac6STejun Heo 		return;
1191678afac6STejun Heo 
1192678afac6STejun Heo 	if (ata_msg_drv(dev->link->ap))
1193678afac6STejun Heo 		ata_dev_printk(dev, KERN_WARNING, "disabled\n");
1194678afac6STejun Heo 	ata_acpi_on_disable(dev);
1195678afac6STejun Heo 	ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 | ATA_DNXFER_QUIET);
1196678afac6STejun Heo 	dev->class++;
119799cf610aSTejun Heo 
119899cf610aSTejun Heo 	/* From now till the next successful probe, ering is used to
119999cf610aSTejun Heo 	 * track probe failures.  Clear accumulated device error info.
120099cf610aSTejun Heo 	 */
120199cf610aSTejun Heo 	ata_ering_clear(&dev->ering);
1202678afac6STejun Heo }
1203678afac6STejun Heo 
1204678afac6STejun Heo /**
1205c6fd2807SJeff Garzik  *	ata_eh_detach_dev - detach ATA device
1206c6fd2807SJeff Garzik  *	@dev: ATA device to detach
1207c6fd2807SJeff Garzik  *
1208c6fd2807SJeff Garzik  *	Detach @dev.
1209c6fd2807SJeff Garzik  *
1210c6fd2807SJeff Garzik  *	LOCKING:
1211c6fd2807SJeff Garzik  *	None.
1212c6fd2807SJeff Garzik  */
1213fb7fd614STejun Heo void ata_eh_detach_dev(struct ata_device *dev)
1214c6fd2807SJeff Garzik {
1215f58229f8STejun Heo 	struct ata_link *link = dev->link;
1216f58229f8STejun Heo 	struct ata_port *ap = link->ap;
121790484ebfSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
1218c6fd2807SJeff Garzik 	unsigned long flags;
1219c6fd2807SJeff Garzik 
1220c6fd2807SJeff Garzik 	ata_dev_disable(dev);
1221c6fd2807SJeff Garzik 
1222c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1223c6fd2807SJeff Garzik 
1224c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_DETACH;
1225c6fd2807SJeff Garzik 
1226c6fd2807SJeff Garzik 	if (ata_scsi_offline_dev(dev)) {
1227c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_DETACHED;
1228c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
1229c6fd2807SJeff Garzik 	}
1230c6fd2807SJeff Garzik 
123190484ebfSTejun Heo 	/* clear per-dev EH info */
1232f58229f8STejun Heo 	ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
1233f58229f8STejun Heo 	ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
123490484ebfSTejun Heo 	ehc->saved_xfer_mode[dev->devno] = 0;
123590484ebfSTejun Heo 	ehc->saved_ncq_enabled &= ~(1 << dev->devno);
1236c6fd2807SJeff Garzik 
1237c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1238c6fd2807SJeff Garzik }
1239c6fd2807SJeff Garzik 
1240c6fd2807SJeff Garzik /**
1241c6fd2807SJeff Garzik  *	ata_eh_about_to_do - about to perform eh_action
1242955e57dfSTejun Heo  *	@link: target ATA link
1243c6fd2807SJeff Garzik  *	@dev: target ATA dev for per-dev action (can be NULL)
1244c6fd2807SJeff Garzik  *	@action: action about to be performed
1245c6fd2807SJeff Garzik  *
1246c6fd2807SJeff Garzik  *	Called just before performing EH actions to clear related bits
1247955e57dfSTejun Heo  *	in @link->eh_info such that eh actions are not unnecessarily
1248955e57dfSTejun Heo  *	repeated.
1249c6fd2807SJeff Garzik  *
1250c6fd2807SJeff Garzik  *	LOCKING:
1251c6fd2807SJeff Garzik  *	None.
1252c6fd2807SJeff Garzik  */
1253fb7fd614STejun Heo void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
1254c6fd2807SJeff Garzik 			unsigned int action)
1255c6fd2807SJeff Garzik {
1256955e57dfSTejun Heo 	struct ata_port *ap = link->ap;
1257955e57dfSTejun Heo 	struct ata_eh_info *ehi = &link->eh_info;
1258955e57dfSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
1259c6fd2807SJeff Garzik 	unsigned long flags;
1260c6fd2807SJeff Garzik 
1261c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1262c6fd2807SJeff Garzik 
1263955e57dfSTejun Heo 	ata_eh_clear_action(link, dev, ehi, action);
1264c6fd2807SJeff Garzik 
1265a568d1d2STejun Heo 	/* About to take EH action, set RECOVERED.  Ignore actions on
1266a568d1d2STejun Heo 	 * slave links as master will do them again.
1267a568d1d2STejun Heo 	 */
1268a568d1d2STejun Heo 	if (!(ehc->i.flags & ATA_EHI_QUIET) && link != ap->slave_link)
1269c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_RECOVERED;
1270c6fd2807SJeff Garzik 
1271c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1272c6fd2807SJeff Garzik }
1273c6fd2807SJeff Garzik 
1274c6fd2807SJeff Garzik /**
1275c6fd2807SJeff Garzik  *	ata_eh_done - EH action complete
1276c6fd2807SJeff Garzik *	@ap: target ATA port
1277c6fd2807SJeff Garzik  *	@dev: target ATA dev for per-dev action (can be NULL)
1278c6fd2807SJeff Garzik  *	@action: action just completed
1279c6fd2807SJeff Garzik  *
1280c6fd2807SJeff Garzik  *	Called right after performing EH actions to clear related bits
1281955e57dfSTejun Heo  *	in @link->eh_context.
1282c6fd2807SJeff Garzik  *
1283c6fd2807SJeff Garzik  *	LOCKING:
1284c6fd2807SJeff Garzik  *	None.
1285c6fd2807SJeff Garzik  */
1286fb7fd614STejun Heo void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1287c6fd2807SJeff Garzik 		 unsigned int action)
1288c6fd2807SJeff Garzik {
1289955e57dfSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
12909af5c9c9STejun Heo 
1291955e57dfSTejun Heo 	ata_eh_clear_action(link, dev, &ehc->i, action);
1292c6fd2807SJeff Garzik }
1293c6fd2807SJeff Garzik 
1294c6fd2807SJeff Garzik /**
1295c6fd2807SJeff Garzik  *	ata_err_string - convert err_mask to descriptive string
1296c6fd2807SJeff Garzik  *	@err_mask: error mask to convert to string
1297c6fd2807SJeff Garzik  *
1298c6fd2807SJeff Garzik  *	Convert @err_mask to descriptive string.  Errors are
1299c6fd2807SJeff Garzik  *	prioritized according to severity and only the most severe
1300c6fd2807SJeff Garzik  *	error is reported.
1301c6fd2807SJeff Garzik  *
1302c6fd2807SJeff Garzik  *	LOCKING:
1303c6fd2807SJeff Garzik  *	None.
1304c6fd2807SJeff Garzik  *
1305c6fd2807SJeff Garzik  *	RETURNS:
1306c6fd2807SJeff Garzik  *	Descriptive string for @err_mask
1307c6fd2807SJeff Garzik  */
1308c6fd2807SJeff Garzik static const char *ata_err_string(unsigned int err_mask)
1309c6fd2807SJeff Garzik {
1310c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_HOST_BUS)
1311c6fd2807SJeff Garzik 		return "host bus error";
1312c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_ATA_BUS)
1313c6fd2807SJeff Garzik 		return "ATA bus error";
1314c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_TIMEOUT)
1315c6fd2807SJeff Garzik 		return "timeout";
1316c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_HSM)
1317c6fd2807SJeff Garzik 		return "HSM violation";
1318c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_SYSTEM)
1319c6fd2807SJeff Garzik 		return "internal error";
1320c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_MEDIA)
1321c6fd2807SJeff Garzik 		return "media error";
1322c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_INVALID)
1323c6fd2807SJeff Garzik 		return "invalid argument";
1324c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_DEV)
1325c6fd2807SJeff Garzik 		return "device error";
1326c6fd2807SJeff Garzik 	return "unknown error";
1327c6fd2807SJeff Garzik }
1328c6fd2807SJeff Garzik 
1329c6fd2807SJeff Garzik /**
1330c6fd2807SJeff Garzik  *	ata_read_log_page - read a specific log page
1331c6fd2807SJeff Garzik  *	@dev: target device
1332c6fd2807SJeff Garzik  *	@page: page to read
1333c6fd2807SJeff Garzik  *	@buf: buffer to store read page
1334c6fd2807SJeff Garzik  *	@sectors: number of sectors to read
1335c6fd2807SJeff Garzik  *
1336c6fd2807SJeff Garzik  *	Read log page using READ_LOG_EXT command.
1337c6fd2807SJeff Garzik  *
1338c6fd2807SJeff Garzik  *	LOCKING:
1339c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1340c6fd2807SJeff Garzik  *
1341c6fd2807SJeff Garzik  *	RETURNS:
1342c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
1343c6fd2807SJeff Garzik  */
1344c6fd2807SJeff Garzik static unsigned int ata_read_log_page(struct ata_device *dev,
1345c6fd2807SJeff Garzik 				      u8 page, void *buf, unsigned int sectors)
1346c6fd2807SJeff Garzik {
1347c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1348c6fd2807SJeff Garzik 	unsigned int err_mask;
1349c6fd2807SJeff Garzik 
1350c6fd2807SJeff Garzik 	DPRINTK("read log page - page %d\n", page);
1351c6fd2807SJeff Garzik 
1352c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
1353c6fd2807SJeff Garzik 	tf.command = ATA_CMD_READ_LOG_EXT;
1354c6fd2807SJeff Garzik 	tf.lbal = page;
1355c6fd2807SJeff Garzik 	tf.nsect = sectors;
1356c6fd2807SJeff Garzik 	tf.hob_nsect = sectors >> 8;
1357c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1358c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_PIO;
1359c6fd2807SJeff Garzik 
1360c6fd2807SJeff Garzik 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
13612b789108STejun Heo 				     buf, sectors * ATA_SECT_SIZE, 0);
1362c6fd2807SJeff Garzik 
1363c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
1364c6fd2807SJeff Garzik 	return err_mask;
1365c6fd2807SJeff Garzik }
1366c6fd2807SJeff Garzik 
1367c6fd2807SJeff Garzik /**
1368c6fd2807SJeff Garzik  *	ata_eh_read_log_10h - Read log page 10h for NCQ error details
1369c6fd2807SJeff Garzik  *	@dev: Device to read log page 10h from
1370c6fd2807SJeff Garzik  *	@tag: Resulting tag of the failed command
1371c6fd2807SJeff Garzik  *	@tf: Resulting taskfile registers of the failed command
1372c6fd2807SJeff Garzik  *
1373c6fd2807SJeff Garzik  *	Read log page 10h to obtain NCQ error details and clear error
1374c6fd2807SJeff Garzik  *	condition.
1375c6fd2807SJeff Garzik  *
1376c6fd2807SJeff Garzik  *	LOCKING:
1377c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1378c6fd2807SJeff Garzik  *
1379c6fd2807SJeff Garzik  *	RETURNS:
1380c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
1381c6fd2807SJeff Garzik  */
1382c6fd2807SJeff Garzik static int ata_eh_read_log_10h(struct ata_device *dev,
1383c6fd2807SJeff Garzik 			       int *tag, struct ata_taskfile *tf)
1384c6fd2807SJeff Garzik {
13859af5c9c9STejun Heo 	u8 *buf = dev->link->ap->sector_buf;
1386c6fd2807SJeff Garzik 	unsigned int err_mask;
1387c6fd2807SJeff Garzik 	u8 csum;
1388c6fd2807SJeff Garzik 	int i;
1389c6fd2807SJeff Garzik 
1390c6fd2807SJeff Garzik 	err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
1391c6fd2807SJeff Garzik 	if (err_mask)
1392c6fd2807SJeff Garzik 		return -EIO;
1393c6fd2807SJeff Garzik 
1394c6fd2807SJeff Garzik 	csum = 0;
1395c6fd2807SJeff Garzik 	for (i = 0; i < ATA_SECT_SIZE; i++)
1396c6fd2807SJeff Garzik 		csum += buf[i];
1397c6fd2807SJeff Garzik 	if (csum)
1398c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_WARNING,
1399c6fd2807SJeff Garzik 			       "invalid checksum 0x%x on log page 10h\n", csum);
1400c6fd2807SJeff Garzik 
1401c6fd2807SJeff Garzik 	if (buf[0] & 0x80)
1402c6fd2807SJeff Garzik 		return -ENOENT;
1403c6fd2807SJeff Garzik 
1404c6fd2807SJeff Garzik 	*tag = buf[0] & 0x1f;
1405c6fd2807SJeff Garzik 
1406c6fd2807SJeff Garzik 	tf->command = buf[2];
1407c6fd2807SJeff Garzik 	tf->feature = buf[3];
1408c6fd2807SJeff Garzik 	tf->lbal = buf[4];
1409c6fd2807SJeff Garzik 	tf->lbam = buf[5];
1410c6fd2807SJeff Garzik 	tf->lbah = buf[6];
1411c6fd2807SJeff Garzik 	tf->device = buf[7];
1412c6fd2807SJeff Garzik 	tf->hob_lbal = buf[8];
1413c6fd2807SJeff Garzik 	tf->hob_lbam = buf[9];
1414c6fd2807SJeff Garzik 	tf->hob_lbah = buf[10];
1415c6fd2807SJeff Garzik 	tf->nsect = buf[12];
1416c6fd2807SJeff Garzik 	tf->hob_nsect = buf[13];
1417c6fd2807SJeff Garzik 
1418c6fd2807SJeff Garzik 	return 0;
1419c6fd2807SJeff Garzik }
1420c6fd2807SJeff Garzik 
1421c6fd2807SJeff Garzik /**
142211fc33daSTejun Heo  *	atapi_eh_tur - perform ATAPI TEST_UNIT_READY
142311fc33daSTejun Heo  *	@dev: target ATAPI device
142411fc33daSTejun Heo  *	@r_sense_key: out parameter for sense_key
142511fc33daSTejun Heo  *
142611fc33daSTejun Heo  *	Perform ATAPI TEST_UNIT_READY.
142711fc33daSTejun Heo  *
142811fc33daSTejun Heo  *	LOCKING:
142911fc33daSTejun Heo  *	EH context (may sleep).
143011fc33daSTejun Heo  *
143111fc33daSTejun Heo  *	RETURNS:
143211fc33daSTejun Heo  *	0 on success, AC_ERR_* mask on failure.
143311fc33daSTejun Heo  */
143411fc33daSTejun Heo static unsigned int atapi_eh_tur(struct ata_device *dev, u8 *r_sense_key)
143511fc33daSTejun Heo {
143611fc33daSTejun Heo 	u8 cdb[ATAPI_CDB_LEN] = { TEST_UNIT_READY, 0, 0, 0, 0, 0 };
143711fc33daSTejun Heo 	struct ata_taskfile tf;
143811fc33daSTejun Heo 	unsigned int err_mask;
143911fc33daSTejun Heo 
144011fc33daSTejun Heo 	ata_tf_init(dev, &tf);
144111fc33daSTejun Heo 
144211fc33daSTejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
144311fc33daSTejun Heo 	tf.command = ATA_CMD_PACKET;
144411fc33daSTejun Heo 	tf.protocol = ATAPI_PROT_NODATA;
144511fc33daSTejun Heo 
144611fc33daSTejun Heo 	err_mask = ata_exec_internal(dev, &tf, cdb, DMA_NONE, NULL, 0, 0);
144711fc33daSTejun Heo 	if (err_mask == AC_ERR_DEV)
144811fc33daSTejun Heo 		*r_sense_key = tf.feature >> 4;
144911fc33daSTejun Heo 	return err_mask;
145011fc33daSTejun Heo }
145111fc33daSTejun Heo 
145211fc33daSTejun Heo /**
1453c6fd2807SJeff Garzik  *	atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1454c6fd2807SJeff Garzik  *	@dev: device to perform REQUEST_SENSE to
1455c6fd2807SJeff Garzik  *	@sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
14563eabddb8STejun Heo  *	@dfl_sense_key: default sense key to use
1457c6fd2807SJeff Garzik  *
1458c6fd2807SJeff Garzik  *	Perform ATAPI REQUEST_SENSE after the device reported CHECK
1459c6fd2807SJeff Garzik  *	SENSE.  This function is EH helper.
1460c6fd2807SJeff Garzik  *
1461c6fd2807SJeff Garzik  *	LOCKING:
1462c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1463c6fd2807SJeff Garzik  *
1464c6fd2807SJeff Garzik  *	RETURNS:
1465c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask on failure
1466c6fd2807SJeff Garzik  */
14673eabddb8STejun Heo static unsigned int atapi_eh_request_sense(struct ata_device *dev,
14683eabddb8STejun Heo 					   u8 *sense_buf, u8 dfl_sense_key)
1469c6fd2807SJeff Garzik {
14703eabddb8STejun Heo 	u8 cdb[ATAPI_CDB_LEN] =
14713eabddb8STejun Heo 		{ REQUEST_SENSE, 0, 0, 0, SCSI_SENSE_BUFFERSIZE, 0 };
14729af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
1473c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1474c6fd2807SJeff Garzik 
1475c6fd2807SJeff Garzik 	DPRINTK("ATAPI request sense\n");
1476c6fd2807SJeff Garzik 
1477c6fd2807SJeff Garzik 	/* FIXME: is this needed? */
1478c6fd2807SJeff Garzik 	memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1479c6fd2807SJeff Garzik 
148056287768SAlbert Lee 	/* initialize sense_buf with the error register,
148156287768SAlbert Lee 	 * for the case where they are -not- overwritten
148256287768SAlbert Lee 	 */
1483c6fd2807SJeff Garzik 	sense_buf[0] = 0x70;
14843eabddb8STejun Heo 	sense_buf[2] = dfl_sense_key;
148556287768SAlbert Lee 
148656287768SAlbert Lee 	/* some devices time out if garbage left in tf */
148756287768SAlbert Lee 	ata_tf_init(dev, &tf);
1488c6fd2807SJeff Garzik 
1489c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1490c6fd2807SJeff Garzik 	tf.command = ATA_CMD_PACKET;
1491c6fd2807SJeff Garzik 
1492c6fd2807SJeff Garzik 	/* is it pointless to prefer PIO for "safety reasons"? */
1493c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_PIO_DMA) {
14940dc36888STejun Heo 		tf.protocol = ATAPI_PROT_DMA;
1495c6fd2807SJeff Garzik 		tf.feature |= ATAPI_PKT_DMA;
1496c6fd2807SJeff Garzik 	} else {
14970dc36888STejun Heo 		tf.protocol = ATAPI_PROT_PIO;
1498f2dfc1a1STejun Heo 		tf.lbam = SCSI_SENSE_BUFFERSIZE;
1499f2dfc1a1STejun Heo 		tf.lbah = 0;
1500c6fd2807SJeff Garzik 	}
1501c6fd2807SJeff Garzik 
1502c6fd2807SJeff Garzik 	return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
15032b789108STejun Heo 				 sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
1504c6fd2807SJeff Garzik }
1505c6fd2807SJeff Garzik 
1506c6fd2807SJeff Garzik /**
1507c6fd2807SJeff Garzik  *	ata_eh_analyze_serror - analyze SError for a failed port
15080260731fSTejun Heo  *	@link: ATA link to analyze SError for
1509c6fd2807SJeff Garzik  *
1510c6fd2807SJeff Garzik  *	Analyze SError if available and further determine cause of
1511c6fd2807SJeff Garzik  *	failure.
1512c6fd2807SJeff Garzik  *
1513c6fd2807SJeff Garzik  *	LOCKING:
1514c6fd2807SJeff Garzik  *	None.
1515c6fd2807SJeff Garzik  */
15160260731fSTejun Heo static void ata_eh_analyze_serror(struct ata_link *link)
1517c6fd2807SJeff Garzik {
15180260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
1519c6fd2807SJeff Garzik 	u32 serror = ehc->i.serror;
1520c6fd2807SJeff Garzik 	unsigned int err_mask = 0, action = 0;
1521f9df58cbSTejun Heo 	u32 hotplug_mask;
1522c6fd2807SJeff Garzik 
1523e0614db2STejun Heo 	if (serror & (SERR_PERSISTENT | SERR_DATA)) {
1524c6fd2807SJeff Garzik 		err_mask |= AC_ERR_ATA_BUS;
1525cf480626STejun Heo 		action |= ATA_EH_RESET;
1526c6fd2807SJeff Garzik 	}
1527c6fd2807SJeff Garzik 	if (serror & SERR_PROTOCOL) {
1528c6fd2807SJeff Garzik 		err_mask |= AC_ERR_HSM;
1529cf480626STejun Heo 		action |= ATA_EH_RESET;
1530c6fd2807SJeff Garzik 	}
1531c6fd2807SJeff Garzik 	if (serror & SERR_INTERNAL) {
1532c6fd2807SJeff Garzik 		err_mask |= AC_ERR_SYSTEM;
1533cf480626STejun Heo 		action |= ATA_EH_RESET;
1534c6fd2807SJeff Garzik 	}
1535f9df58cbSTejun Heo 
1536f9df58cbSTejun Heo 	/* Determine whether a hotplug event has occurred.  Both
1537f9df58cbSTejun Heo 	 * SError.N/X are considered hotplug events for enabled or
1538f9df58cbSTejun Heo 	 * host links.  For disabled PMP links, only N bit is
1539f9df58cbSTejun Heo 	 * considered as X bit is left at 1 for link plugging.
1540f9df58cbSTejun Heo 	 */
1541f9df58cbSTejun Heo 	hotplug_mask = 0;
1542f9df58cbSTejun Heo 
1543f9df58cbSTejun Heo 	if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
1544f9df58cbSTejun Heo 		hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
1545f9df58cbSTejun Heo 	else
1546f9df58cbSTejun Heo 		hotplug_mask = SERR_PHYRDY_CHG;
1547f9df58cbSTejun Heo 
1548f9df58cbSTejun Heo 	if (serror & hotplug_mask)
1549c6fd2807SJeff Garzik 		ata_ehi_hotplugged(&ehc->i);
1550c6fd2807SJeff Garzik 
1551c6fd2807SJeff Garzik 	ehc->i.err_mask |= err_mask;
1552c6fd2807SJeff Garzik 	ehc->i.action |= action;
1553c6fd2807SJeff Garzik }
1554c6fd2807SJeff Garzik 
1555c6fd2807SJeff Garzik /**
1556c6fd2807SJeff Garzik  *	ata_eh_analyze_ncq_error - analyze NCQ error
15570260731fSTejun Heo  *	@link: ATA link to analyze NCQ error for
1558c6fd2807SJeff Garzik  *
1559c6fd2807SJeff Garzik  *	Read log page 10h, determine the offending qc and acquire
1560c6fd2807SJeff Garzik  *	error status TF.  For NCQ device errors, all LLDDs have to do
1561c6fd2807SJeff Garzik  *	is setting AC_ERR_DEV in ehi->err_mask.  This function takes
1562c6fd2807SJeff Garzik  *	care of the rest.
1563c6fd2807SJeff Garzik  *
1564c6fd2807SJeff Garzik  *	LOCKING:
1565c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1566c6fd2807SJeff Garzik  */
156710acf3b0SMark Lord void ata_eh_analyze_ncq_error(struct ata_link *link)
1568c6fd2807SJeff Garzik {
15690260731fSTejun Heo 	struct ata_port *ap = link->ap;
15700260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
15710260731fSTejun Heo 	struct ata_device *dev = link->device;
1572c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
1573c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1574c6fd2807SJeff Garzik 	int tag, rc;
1575c6fd2807SJeff Garzik 
1576c6fd2807SJeff Garzik 	/* if frozen, we can't do much */
1577c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN)
1578c6fd2807SJeff Garzik 		return;
1579c6fd2807SJeff Garzik 
1580c6fd2807SJeff Garzik 	/* is it NCQ device error? */
15810260731fSTejun Heo 	if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1582c6fd2807SJeff Garzik 		return;
1583c6fd2807SJeff Garzik 
1584c6fd2807SJeff Garzik 	/* has LLDD analyzed already? */
1585c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1586c6fd2807SJeff Garzik 		qc = __ata_qc_from_tag(ap, tag);
1587c6fd2807SJeff Garzik 
1588c6fd2807SJeff Garzik 		if (!(qc->flags & ATA_QCFLAG_FAILED))
1589c6fd2807SJeff Garzik 			continue;
1590c6fd2807SJeff Garzik 
1591c6fd2807SJeff Garzik 		if (qc->err_mask)
1592c6fd2807SJeff Garzik 			return;
1593c6fd2807SJeff Garzik 	}
1594c6fd2807SJeff Garzik 
1595c6fd2807SJeff Garzik 	/* okay, this error is ours */
1596c6fd2807SJeff Garzik 	rc = ata_eh_read_log_10h(dev, &tag, &tf);
1597c6fd2807SJeff Garzik 	if (rc) {
15980260731fSTejun Heo 		ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
1599c6fd2807SJeff Garzik 				"(errno=%d)\n", rc);
1600c6fd2807SJeff Garzik 		return;
1601c6fd2807SJeff Garzik 	}
1602c6fd2807SJeff Garzik 
16030260731fSTejun Heo 	if (!(link->sactive & (1 << tag))) {
16040260731fSTejun Heo 		ata_link_printk(link, KERN_ERR, "log page 10h reported "
1605c6fd2807SJeff Garzik 				"inactive tag %d\n", tag);
1606c6fd2807SJeff Garzik 		return;
1607c6fd2807SJeff Garzik 	}
1608c6fd2807SJeff Garzik 
1609c6fd2807SJeff Garzik 	/* we've got the perpetrator, condemn it */
1610c6fd2807SJeff Garzik 	qc = __ata_qc_from_tag(ap, tag);
1611c6fd2807SJeff Garzik 	memcpy(&qc->result_tf, &tf, sizeof(tf));
1612a6116c9eSMark Lord 	qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
16135335b729STejun Heo 	qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
1614c6fd2807SJeff Garzik 	ehc->i.err_mask &= ~AC_ERR_DEV;
1615c6fd2807SJeff Garzik }
1616c6fd2807SJeff Garzik 
1617c6fd2807SJeff Garzik /**
1618c6fd2807SJeff Garzik  *	ata_eh_analyze_tf - analyze taskfile of a failed qc
1619c6fd2807SJeff Garzik  *	@qc: qc to analyze
1620c6fd2807SJeff Garzik  *	@tf: Taskfile registers to analyze
1621c6fd2807SJeff Garzik  *
1622c6fd2807SJeff Garzik  *	Analyze taskfile of @qc and further determine cause of
1623c6fd2807SJeff Garzik  *	failure.  This function also requests ATAPI sense data if
1624c6fd2807SJeff Garzik  *	avaliable.
1625c6fd2807SJeff Garzik  *
1626c6fd2807SJeff Garzik  *	LOCKING:
1627c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1628c6fd2807SJeff Garzik  *
1629c6fd2807SJeff Garzik  *	RETURNS:
1630c6fd2807SJeff Garzik  *	Determined recovery action
1631c6fd2807SJeff Garzik  */
1632c6fd2807SJeff Garzik static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1633c6fd2807SJeff Garzik 				      const struct ata_taskfile *tf)
1634c6fd2807SJeff Garzik {
1635c6fd2807SJeff Garzik 	unsigned int tmp, action = 0;
1636c6fd2807SJeff Garzik 	u8 stat = tf->command, err = tf->feature;
1637c6fd2807SJeff Garzik 
1638c6fd2807SJeff Garzik 	if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1639c6fd2807SJeff Garzik 		qc->err_mask |= AC_ERR_HSM;
1640cf480626STejun Heo 		return ATA_EH_RESET;
1641c6fd2807SJeff Garzik 	}
1642c6fd2807SJeff Garzik 
1643a51d644aSTejun Heo 	if (stat & (ATA_ERR | ATA_DF))
1644a51d644aSTejun Heo 		qc->err_mask |= AC_ERR_DEV;
1645a51d644aSTejun Heo 	else
1646c6fd2807SJeff Garzik 		return 0;
1647c6fd2807SJeff Garzik 
1648c6fd2807SJeff Garzik 	switch (qc->dev->class) {
1649c6fd2807SJeff Garzik 	case ATA_DEV_ATA:
1650c6fd2807SJeff Garzik 		if (err & ATA_ICRC)
1651c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_ATA_BUS;
1652c6fd2807SJeff Garzik 		if (err & ATA_UNC)
1653c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_MEDIA;
1654c6fd2807SJeff Garzik 		if (err & ATA_IDNF)
1655c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_INVALID;
1656c6fd2807SJeff Garzik 		break;
1657c6fd2807SJeff Garzik 
1658c6fd2807SJeff Garzik 	case ATA_DEV_ATAPI:
1659a569a30dSTejun Heo 		if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
16603eabddb8STejun Heo 			tmp = atapi_eh_request_sense(qc->dev,
16613eabddb8STejun Heo 						qc->scsicmd->sense_buffer,
16623eabddb8STejun Heo 						qc->result_tf.feature >> 4);
1663c6fd2807SJeff Garzik 			if (!tmp) {
1664a569a30dSTejun Heo 				/* ATA_QCFLAG_SENSE_VALID is used to
1665a569a30dSTejun Heo 				 * tell atapi_qc_complete() that sense
1666a569a30dSTejun Heo 				 * data is already valid.
1667c6fd2807SJeff Garzik 				 *
1668c6fd2807SJeff Garzik 				 * TODO: interpret sense data and set
1669c6fd2807SJeff Garzik 				 * appropriate err_mask.
1670c6fd2807SJeff Garzik 				 */
1671c6fd2807SJeff Garzik 				qc->flags |= ATA_QCFLAG_SENSE_VALID;
1672c6fd2807SJeff Garzik 			} else
1673c6fd2807SJeff Garzik 				qc->err_mask |= tmp;
1674c6fd2807SJeff Garzik 		}
1675a569a30dSTejun Heo 	}
1676c6fd2807SJeff Garzik 
1677c6fd2807SJeff Garzik 	if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1678cf480626STejun Heo 		action |= ATA_EH_RESET;
1679c6fd2807SJeff Garzik 
1680c6fd2807SJeff Garzik 	return action;
1681c6fd2807SJeff Garzik }
1682c6fd2807SJeff Garzik 
168376326ac1STejun Heo static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask,
168476326ac1STejun Heo 				   int *xfer_ok)
1685c6fd2807SJeff Garzik {
168676326ac1STejun Heo 	int base = 0;
168776326ac1STejun Heo 
168876326ac1STejun Heo 	if (!(eflags & ATA_EFLAG_DUBIOUS_XFER))
168976326ac1STejun Heo 		*xfer_ok = 1;
169076326ac1STejun Heo 
169176326ac1STejun Heo 	if (!*xfer_ok)
169275f9cafcSTejun Heo 		base = ATA_ECAT_DUBIOUS_NONE;
169376326ac1STejun Heo 
16947d47e8d4STejun Heo 	if (err_mask & AC_ERR_ATA_BUS)
169576326ac1STejun Heo 		return base + ATA_ECAT_ATA_BUS;
1696c6fd2807SJeff Garzik 
16977d47e8d4STejun Heo 	if (err_mask & AC_ERR_TIMEOUT)
169876326ac1STejun Heo 		return base + ATA_ECAT_TOUT_HSM;
16997d47e8d4STejun Heo 
17003884f7b0STejun Heo 	if (eflags & ATA_EFLAG_IS_IO) {
17017d47e8d4STejun Heo 		if (err_mask & AC_ERR_HSM)
170276326ac1STejun Heo 			return base + ATA_ECAT_TOUT_HSM;
17037d47e8d4STejun Heo 		if ((err_mask &
17047d47e8d4STejun Heo 		     (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
170576326ac1STejun Heo 			return base + ATA_ECAT_UNK_DEV;
1706c6fd2807SJeff Garzik 	}
1707c6fd2807SJeff Garzik 
1708c6fd2807SJeff Garzik 	return 0;
1709c6fd2807SJeff Garzik }
1710c6fd2807SJeff Garzik 
17117d47e8d4STejun Heo struct speed_down_verdict_arg {
1712c6fd2807SJeff Garzik 	u64 since;
171376326ac1STejun Heo 	int xfer_ok;
17143884f7b0STejun Heo 	int nr_errors[ATA_ECAT_NR];
1715c6fd2807SJeff Garzik };
1716c6fd2807SJeff Garzik 
17177d47e8d4STejun Heo static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
1718c6fd2807SJeff Garzik {
17197d47e8d4STejun Heo 	struct speed_down_verdict_arg *arg = void_arg;
172076326ac1STejun Heo 	int cat;
1721c6fd2807SJeff Garzik 
1722c6fd2807SJeff Garzik 	if (ent->timestamp < arg->since)
1723c6fd2807SJeff Garzik 		return -1;
1724c6fd2807SJeff Garzik 
172576326ac1STejun Heo 	cat = ata_eh_categorize_error(ent->eflags, ent->err_mask,
172676326ac1STejun Heo 				      &arg->xfer_ok);
17277d47e8d4STejun Heo 	arg->nr_errors[cat]++;
172876326ac1STejun Heo 
1729c6fd2807SJeff Garzik 	return 0;
1730c6fd2807SJeff Garzik }
1731c6fd2807SJeff Garzik 
1732c6fd2807SJeff Garzik /**
17337d47e8d4STejun Heo  *	ata_eh_speed_down_verdict - Determine speed down verdict
1734c6fd2807SJeff Garzik  *	@dev: Device of interest
1735c6fd2807SJeff Garzik  *
1736c6fd2807SJeff Garzik  *	This function examines error ring of @dev and determines
17377d47e8d4STejun Heo  *	whether NCQ needs to be turned off, transfer speed should be
17387d47e8d4STejun Heo  *	stepped down, or falling back to PIO is necessary.
1739c6fd2807SJeff Garzik  *
17403884f7b0STejun Heo  *	ECAT_ATA_BUS	: ATA_BUS error for any command
1741c6fd2807SJeff Garzik  *
17423884f7b0STejun Heo  *	ECAT_TOUT_HSM	: TIMEOUT for any command or HSM violation for
17433884f7b0STejun Heo  *			  IO commands
17447d47e8d4STejun Heo  *
17453884f7b0STejun Heo  *	ECAT_UNK_DEV	: Unknown DEV error for IO commands
1746c6fd2807SJeff Garzik  *
174776326ac1STejun Heo  *	ECAT_DUBIOUS_*	: Identical to above three but occurred while
174876326ac1STejun Heo  *			  data transfer hasn't been verified.
174976326ac1STejun Heo  *
17503884f7b0STejun Heo  *	Verdicts are
17517d47e8d4STejun Heo  *
17523884f7b0STejun Heo  *	NCQ_OFF		: Turn off NCQ.
17537d47e8d4STejun Heo  *
17543884f7b0STejun Heo  *	SPEED_DOWN	: Speed down transfer speed but don't fall back
17553884f7b0STejun Heo  *			  to PIO.
17563884f7b0STejun Heo  *
17573884f7b0STejun Heo  *	FALLBACK_TO_PIO	: Fall back to PIO.
17583884f7b0STejun Heo  *
17593884f7b0STejun Heo  *	Even if multiple verdicts are returned, only one action is
176076326ac1STejun Heo  *	taken per error.  An action triggered by non-DUBIOUS errors
176176326ac1STejun Heo  *	clears ering, while one triggered by DUBIOUS_* errors doesn't.
176276326ac1STejun Heo  *	This is to expedite speed down decisions right after device is
176376326ac1STejun Heo  *	initially configured.
17643884f7b0STejun Heo  *
176576326ac1STejun Heo  *	The followings are speed down rules.  #1 and #2 deal with
176676326ac1STejun Heo  *	DUBIOUS errors.
176776326ac1STejun Heo  *
176876326ac1STejun Heo  *	1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors
176976326ac1STejun Heo  *	   occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO.
177076326ac1STejun Heo  *
177176326ac1STejun Heo  *	2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors
177276326ac1STejun Heo  *	   occurred during last 5 mins, NCQ_OFF.
177376326ac1STejun Heo  *
177476326ac1STejun Heo  *	3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors
17753884f7b0STejun Heo  *	   ocurred during last 5 mins, FALLBACK_TO_PIO
17763884f7b0STejun Heo  *
177776326ac1STejun Heo  *	4. If more than 3 TOUT_HSM or UNK_DEV errors occurred
17783884f7b0STejun Heo  *	   during last 10 mins, NCQ_OFF.
17793884f7b0STejun Heo  *
178076326ac1STejun Heo  *	5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6
17813884f7b0STejun Heo  *	   UNK_DEV errors occurred during last 10 mins, SPEED_DOWN.
17827d47e8d4STejun Heo  *
1783c6fd2807SJeff Garzik  *	LOCKING:
1784c6fd2807SJeff Garzik  *	Inherited from caller.
1785c6fd2807SJeff Garzik  *
1786c6fd2807SJeff Garzik  *	RETURNS:
17877d47e8d4STejun Heo  *	OR of ATA_EH_SPDN_* flags.
1788c6fd2807SJeff Garzik  */
17897d47e8d4STejun Heo static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
1790c6fd2807SJeff Garzik {
17917d47e8d4STejun Heo 	const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
17927d47e8d4STejun Heo 	u64 j64 = get_jiffies_64();
17937d47e8d4STejun Heo 	struct speed_down_verdict_arg arg;
17947d47e8d4STejun Heo 	unsigned int verdict = 0;
1795c6fd2807SJeff Garzik 
17963884f7b0STejun Heo 	/* scan past 5 mins of error history */
17973884f7b0STejun Heo 	memset(&arg, 0, sizeof(arg));
17983884f7b0STejun Heo 	arg.since = j64 - min(j64, j5mins);
17993884f7b0STejun Heo 	ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
18003884f7b0STejun Heo 
180176326ac1STejun Heo 	if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] +
180276326ac1STejun Heo 	    arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1)
180376326ac1STejun Heo 		verdict |= ATA_EH_SPDN_SPEED_DOWN |
180476326ac1STejun Heo 			ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS;
180576326ac1STejun Heo 
180676326ac1STejun Heo 	if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] +
180776326ac1STejun Heo 	    arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1)
180876326ac1STejun Heo 		verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS;
180976326ac1STejun Heo 
18103884f7b0STejun Heo 	if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
18113884f7b0STejun Heo 	    arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1812663f99b8STejun Heo 	    arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
18133884f7b0STejun Heo 		verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
18143884f7b0STejun Heo 
18157d47e8d4STejun Heo 	/* scan past 10 mins of error history */
1816c6fd2807SJeff Garzik 	memset(&arg, 0, sizeof(arg));
18177d47e8d4STejun Heo 	arg.since = j64 - min(j64, j10mins);
18187d47e8d4STejun Heo 	ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1819c6fd2807SJeff Garzik 
18203884f7b0STejun Heo 	if (arg.nr_errors[ATA_ECAT_TOUT_HSM] +
18213884f7b0STejun Heo 	    arg.nr_errors[ATA_ECAT_UNK_DEV] > 3)
18227d47e8d4STejun Heo 		verdict |= ATA_EH_SPDN_NCQ_OFF;
18233884f7b0STejun Heo 
18243884f7b0STejun Heo 	if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
18253884f7b0STejun Heo 	    arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 ||
1826663f99b8STejun Heo 	    arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
18277d47e8d4STejun Heo 		verdict |= ATA_EH_SPDN_SPEED_DOWN;
1828c6fd2807SJeff Garzik 
18297d47e8d4STejun Heo 	return verdict;
1830c6fd2807SJeff Garzik }
1831c6fd2807SJeff Garzik 
1832c6fd2807SJeff Garzik /**
1833c6fd2807SJeff Garzik  *	ata_eh_speed_down - record error and speed down if necessary
1834c6fd2807SJeff Garzik  *	@dev: Failed device
18353884f7b0STejun Heo  *	@eflags: mask of ATA_EFLAG_* flags
1836c6fd2807SJeff Garzik  *	@err_mask: err_mask of the error
1837c6fd2807SJeff Garzik  *
1838c6fd2807SJeff Garzik  *	Record error and examine error history to determine whether
1839c6fd2807SJeff Garzik  *	adjusting transmission speed is necessary.  It also sets
1840c6fd2807SJeff Garzik  *	transmission limits appropriately if such adjustment is
1841c6fd2807SJeff Garzik  *	necessary.
1842c6fd2807SJeff Garzik  *
1843c6fd2807SJeff Garzik  *	LOCKING:
1844c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1845c6fd2807SJeff Garzik  *
1846c6fd2807SJeff Garzik  *	RETURNS:
18477d47e8d4STejun Heo  *	Determined recovery action.
1848c6fd2807SJeff Garzik  */
18493884f7b0STejun Heo static unsigned int ata_eh_speed_down(struct ata_device *dev,
18503884f7b0STejun Heo 				unsigned int eflags, unsigned int err_mask)
1851c6fd2807SJeff Garzik {
1852b1c72916STejun Heo 	struct ata_link *link = ata_dev_phys_link(dev);
185376326ac1STejun Heo 	int xfer_ok = 0;
18547d47e8d4STejun Heo 	unsigned int verdict;
18557d47e8d4STejun Heo 	unsigned int action = 0;
18567d47e8d4STejun Heo 
18577d47e8d4STejun Heo 	/* don't bother if Cat-0 error */
185876326ac1STejun Heo 	if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0)
1859c6fd2807SJeff Garzik 		return 0;
1860c6fd2807SJeff Garzik 
1861c6fd2807SJeff Garzik 	/* record error and determine whether speed down is necessary */
18623884f7b0STejun Heo 	ata_ering_record(&dev->ering, eflags, err_mask);
18637d47e8d4STejun Heo 	verdict = ata_eh_speed_down_verdict(dev);
1864c6fd2807SJeff Garzik 
18657d47e8d4STejun Heo 	/* turn off NCQ? */
18667d47e8d4STejun Heo 	if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
18677d47e8d4STejun Heo 	    (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
18687d47e8d4STejun Heo 			   ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
18697d47e8d4STejun Heo 		dev->flags |= ATA_DFLAG_NCQ_OFF;
18707d47e8d4STejun Heo 		ata_dev_printk(dev, KERN_WARNING,
18717d47e8d4STejun Heo 			       "NCQ disabled due to excessive errors\n");
18727d47e8d4STejun Heo 		goto done;
18737d47e8d4STejun Heo 	}
1874c6fd2807SJeff Garzik 
18757d47e8d4STejun Heo 	/* speed down? */
18767d47e8d4STejun Heo 	if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1877c6fd2807SJeff Garzik 		/* speed down SATA link speed if possible */
1878*a07d499bSTejun Heo 		if (sata_down_spd_limit(link, 0) == 0) {
1879cf480626STejun Heo 			action |= ATA_EH_RESET;
18807d47e8d4STejun Heo 			goto done;
18817d47e8d4STejun Heo 		}
1882c6fd2807SJeff Garzik 
1883c6fd2807SJeff Garzik 		/* lower transfer mode */
18847d47e8d4STejun Heo 		if (dev->spdn_cnt < 2) {
18857d47e8d4STejun Heo 			static const int dma_dnxfer_sel[] =
18867d47e8d4STejun Heo 				{ ATA_DNXFER_DMA, ATA_DNXFER_40C };
18877d47e8d4STejun Heo 			static const int pio_dnxfer_sel[] =
18887d47e8d4STejun Heo 				{ ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
18897d47e8d4STejun Heo 			int sel;
1890c6fd2807SJeff Garzik 
18917d47e8d4STejun Heo 			if (dev->xfer_shift != ATA_SHIFT_PIO)
18927d47e8d4STejun Heo 				sel = dma_dnxfer_sel[dev->spdn_cnt];
18937d47e8d4STejun Heo 			else
18947d47e8d4STejun Heo 				sel = pio_dnxfer_sel[dev->spdn_cnt];
18957d47e8d4STejun Heo 
18967d47e8d4STejun Heo 			dev->spdn_cnt++;
18977d47e8d4STejun Heo 
18987d47e8d4STejun Heo 			if (ata_down_xfermask_limit(dev, sel) == 0) {
1899cf480626STejun Heo 				action |= ATA_EH_RESET;
19007d47e8d4STejun Heo 				goto done;
19017d47e8d4STejun Heo 			}
19027d47e8d4STejun Heo 		}
19037d47e8d4STejun Heo 	}
19047d47e8d4STejun Heo 
19057d47e8d4STejun Heo 	/* Fall back to PIO?  Slowing down to PIO is meaningless for
1906663f99b8STejun Heo 	 * SATA ATA devices.  Consider it only for PATA and SATAPI.
19077d47e8d4STejun Heo 	 */
19087d47e8d4STejun Heo 	if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
1909663f99b8STejun Heo 	    (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) &&
19107d47e8d4STejun Heo 	    (dev->xfer_shift != ATA_SHIFT_PIO)) {
19117d47e8d4STejun Heo 		if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
19127d47e8d4STejun Heo 			dev->spdn_cnt = 0;
1913cf480626STejun Heo 			action |= ATA_EH_RESET;
19147d47e8d4STejun Heo 			goto done;
19157d47e8d4STejun Heo 		}
19167d47e8d4STejun Heo 	}
19177d47e8d4STejun Heo 
1918c6fd2807SJeff Garzik 	return 0;
19197d47e8d4STejun Heo  done:
19207d47e8d4STejun Heo 	/* device has been slowed down, blow error history */
192176326ac1STejun Heo 	if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS))
19227d47e8d4STejun Heo 		ata_ering_clear(&dev->ering);
19237d47e8d4STejun Heo 	return action;
1924c6fd2807SJeff Garzik }
1925c6fd2807SJeff Garzik 
1926c6fd2807SJeff Garzik /**
19279b1e2658STejun Heo  *	ata_eh_link_autopsy - analyze error and determine recovery action
19289b1e2658STejun Heo  *	@link: host link to perform autopsy on
1929c6fd2807SJeff Garzik  *
19300260731fSTejun Heo  *	Analyze why @link failed and determine which recovery actions
19310260731fSTejun Heo  *	are needed.  This function also sets more detailed AC_ERR_*
19320260731fSTejun Heo  *	values and fills sense data for ATAPI CHECK SENSE.
1933c6fd2807SJeff Garzik  *
1934c6fd2807SJeff Garzik  *	LOCKING:
1935c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1936c6fd2807SJeff Garzik  */
19379b1e2658STejun Heo static void ata_eh_link_autopsy(struct ata_link *link)
1938c6fd2807SJeff Garzik {
19390260731fSTejun Heo 	struct ata_port *ap = link->ap;
1940936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
1941dfcc173dSTejun Heo 	struct ata_device *dev;
19423884f7b0STejun Heo 	unsigned int all_err_mask = 0, eflags = 0;
19433884f7b0STejun Heo 	int tag;
1944c6fd2807SJeff Garzik 	u32 serror;
1945c6fd2807SJeff Garzik 	int rc;
1946c6fd2807SJeff Garzik 
1947c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
1948c6fd2807SJeff Garzik 
1949c6fd2807SJeff Garzik 	if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1950c6fd2807SJeff Garzik 		return;
1951c6fd2807SJeff Garzik 
1952c6fd2807SJeff Garzik 	/* obtain and analyze SError */
1953936fd732STejun Heo 	rc = sata_scr_read(link, SCR_ERROR, &serror);
1954c6fd2807SJeff Garzik 	if (rc == 0) {
1955c6fd2807SJeff Garzik 		ehc->i.serror |= serror;
19560260731fSTejun Heo 		ata_eh_analyze_serror(link);
19574e57c517STejun Heo 	} else if (rc != -EOPNOTSUPP) {
1958cf480626STejun Heo 		/* SError read failed, force reset and probing */
1959b558edddSTejun Heo 		ehc->i.probe_mask |= ATA_ALL_DEVICES;
1960cf480626STejun Heo 		ehc->i.action |= ATA_EH_RESET;
19614e57c517STejun Heo 		ehc->i.err_mask |= AC_ERR_OTHER;
19624e57c517STejun Heo 	}
1963c6fd2807SJeff Garzik 
1964c6fd2807SJeff Garzik 	/* analyze NCQ failure */
19650260731fSTejun Heo 	ata_eh_analyze_ncq_error(link);
1966c6fd2807SJeff Garzik 
1967c6fd2807SJeff Garzik 	/* any real error trumps AC_ERR_OTHER */
1968c6fd2807SJeff Garzik 	if (ehc->i.err_mask & ~AC_ERR_OTHER)
1969c6fd2807SJeff Garzik 		ehc->i.err_mask &= ~AC_ERR_OTHER;
1970c6fd2807SJeff Garzik 
1971c6fd2807SJeff Garzik 	all_err_mask |= ehc->i.err_mask;
1972c6fd2807SJeff Garzik 
1973c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1974c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1975c6fd2807SJeff Garzik 
1976b1c72916STejun Heo 		if (!(qc->flags & ATA_QCFLAG_FAILED) ||
1977b1c72916STejun Heo 		    ata_dev_phys_link(qc->dev) != link)
1978c6fd2807SJeff Garzik 			continue;
1979c6fd2807SJeff Garzik 
1980c6fd2807SJeff Garzik 		/* inherit upper level err_mask */
1981c6fd2807SJeff Garzik 		qc->err_mask |= ehc->i.err_mask;
1982c6fd2807SJeff Garzik 
1983c6fd2807SJeff Garzik 		/* analyze TF */
1984c6fd2807SJeff Garzik 		ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1985c6fd2807SJeff Garzik 
1986c6fd2807SJeff Garzik 		/* DEV errors are probably spurious in case of ATA_BUS error */
1987c6fd2807SJeff Garzik 		if (qc->err_mask & AC_ERR_ATA_BUS)
1988c6fd2807SJeff Garzik 			qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1989c6fd2807SJeff Garzik 					  AC_ERR_INVALID);
1990c6fd2807SJeff Garzik 
1991c6fd2807SJeff Garzik 		/* any real error trumps unknown error */
1992c6fd2807SJeff Garzik 		if (qc->err_mask & ~AC_ERR_OTHER)
1993c6fd2807SJeff Garzik 			qc->err_mask &= ~AC_ERR_OTHER;
1994c6fd2807SJeff Garzik 
1995c6fd2807SJeff Garzik 		/* SENSE_VALID trumps dev/unknown error and revalidation */
1996f90f0828STejun Heo 		if (qc->flags & ATA_QCFLAG_SENSE_VALID)
1997c6fd2807SJeff Garzik 			qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1998c6fd2807SJeff Garzik 
199903faab78STejun Heo 		/* determine whether the command is worth retrying */
200003faab78STejun Heo 		if (!(qc->err_mask & AC_ERR_INVALID) &&
200103faab78STejun Heo 		    ((qc->flags & ATA_QCFLAG_IO) || qc->err_mask != AC_ERR_DEV))
200203faab78STejun Heo 			qc->flags |= ATA_QCFLAG_RETRY;
200303faab78STejun Heo 
2004c6fd2807SJeff Garzik 		/* accumulate error info */
2005c6fd2807SJeff Garzik 		ehc->i.dev = qc->dev;
2006c6fd2807SJeff Garzik 		all_err_mask |= qc->err_mask;
2007c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_IO)
20083884f7b0STejun Heo 			eflags |= ATA_EFLAG_IS_IO;
2009c6fd2807SJeff Garzik 	}
2010c6fd2807SJeff Garzik 
2011c6fd2807SJeff Garzik 	/* enforce default EH actions */
2012c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN ||
2013c6fd2807SJeff Garzik 	    all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
2014cf480626STejun Heo 		ehc->i.action |= ATA_EH_RESET;
20153884f7b0STejun Heo 	else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) ||
20163884f7b0STejun Heo 		 (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV)))
2017c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_REVALIDATE;
2018c6fd2807SJeff Garzik 
2019dfcc173dSTejun Heo 	/* If we have offending qcs and the associated failed device,
2020dfcc173dSTejun Heo 	 * perform per-dev EH action only on the offending device.
2021dfcc173dSTejun Heo 	 */
2022c6fd2807SJeff Garzik 	if (ehc->i.dev) {
2023c6fd2807SJeff Garzik 		ehc->i.dev_action[ehc->i.dev->devno] |=
2024c6fd2807SJeff Garzik 			ehc->i.action & ATA_EH_PERDEV_MASK;
2025c6fd2807SJeff Garzik 		ehc->i.action &= ~ATA_EH_PERDEV_MASK;
2026c6fd2807SJeff Garzik 	}
2027c6fd2807SJeff Garzik 
20282695e366STejun Heo 	/* propagate timeout to host link */
20292695e366STejun Heo 	if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link))
20302695e366STejun Heo 		ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT;
20312695e366STejun Heo 
20322695e366STejun Heo 	/* record error and consider speeding down */
2033dfcc173dSTejun Heo 	dev = ehc->i.dev;
20342695e366STejun Heo 	if (!dev && ((ata_link_max_devices(link) == 1 &&
20352695e366STejun Heo 		      ata_dev_enabled(link->device))))
2036dfcc173dSTejun Heo 	    dev = link->device;
2037dfcc173dSTejun Heo 
203876326ac1STejun Heo 	if (dev) {
203976326ac1STejun Heo 		if (dev->flags & ATA_DFLAG_DUBIOUS_XFER)
204076326ac1STejun Heo 			eflags |= ATA_EFLAG_DUBIOUS_XFER;
20413884f7b0STejun Heo 		ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask);
204276326ac1STejun Heo 	}
2043dfcc173dSTejun Heo 
2044c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
2045c6fd2807SJeff Garzik }
2046c6fd2807SJeff Garzik 
2047c6fd2807SJeff Garzik /**
20489b1e2658STejun Heo  *	ata_eh_autopsy - analyze error and determine recovery action
20499b1e2658STejun Heo  *	@ap: host port to perform autopsy on
20509b1e2658STejun Heo  *
20519b1e2658STejun Heo  *	Analyze all links of @ap and determine why they failed and
20529b1e2658STejun Heo  *	which recovery actions are needed.
20539b1e2658STejun Heo  *
20549b1e2658STejun Heo  *	LOCKING:
20559b1e2658STejun Heo  *	Kernel thread context (may sleep).
20569b1e2658STejun Heo  */
2057fb7fd614STejun Heo void ata_eh_autopsy(struct ata_port *ap)
20589b1e2658STejun Heo {
20599b1e2658STejun Heo 	struct ata_link *link;
20609b1e2658STejun Heo 
20611eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE)
20629b1e2658STejun Heo 		ata_eh_link_autopsy(link);
20632695e366STejun Heo 
2064b1c72916STejun Heo 	/* Handle the frigging slave link.  Autopsy is done similarly
2065b1c72916STejun Heo 	 * but actions and flags are transferred over to the master
2066b1c72916STejun Heo 	 * link and handled from there.
2067b1c72916STejun Heo 	 */
2068b1c72916STejun Heo 	if (ap->slave_link) {
2069b1c72916STejun Heo 		struct ata_eh_context *mehc = &ap->link.eh_context;
2070b1c72916STejun Heo 		struct ata_eh_context *sehc = &ap->slave_link->eh_context;
2071b1c72916STejun Heo 
2072848e4c68STejun Heo 		/* transfer control flags from master to slave */
2073848e4c68STejun Heo 		sehc->i.flags |= mehc->i.flags & ATA_EHI_TO_SLAVE_MASK;
2074848e4c68STejun Heo 
2075848e4c68STejun Heo 		/* perform autopsy on the slave link */
2076b1c72916STejun Heo 		ata_eh_link_autopsy(ap->slave_link);
2077b1c72916STejun Heo 
2078848e4c68STejun Heo 		/* transfer actions from slave to master and clear slave */
2079b1c72916STejun Heo 		ata_eh_about_to_do(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
2080b1c72916STejun Heo 		mehc->i.action		|= sehc->i.action;
2081b1c72916STejun Heo 		mehc->i.dev_action[1]	|= sehc->i.dev_action[1];
2082b1c72916STejun Heo 		mehc->i.flags		|= sehc->i.flags;
2083b1c72916STejun Heo 		ata_eh_done(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
2084b1c72916STejun Heo 	}
2085b1c72916STejun Heo 
20862695e366STejun Heo 	/* Autopsy of fanout ports can affect host link autopsy.
20872695e366STejun Heo 	 * Perform host link autopsy last.
20882695e366STejun Heo 	 */
2089071f44b1STejun Heo 	if (sata_pmp_attached(ap))
20902695e366STejun Heo 		ata_eh_link_autopsy(&ap->link);
20919b1e2658STejun Heo }
20929b1e2658STejun Heo 
20939b1e2658STejun Heo /**
20949b1e2658STejun Heo  *	ata_eh_link_report - report error handling to user
20950260731fSTejun Heo  *	@link: ATA link EH is going on
2096c6fd2807SJeff Garzik  *
2097c6fd2807SJeff Garzik  *	Report EH to user.
2098c6fd2807SJeff Garzik  *
2099c6fd2807SJeff Garzik  *	LOCKING:
2100c6fd2807SJeff Garzik  *	None.
2101c6fd2807SJeff Garzik  */
21029b1e2658STejun Heo static void ata_eh_link_report(struct ata_link *link)
2103c6fd2807SJeff Garzik {
21040260731fSTejun Heo 	struct ata_port *ap = link->ap;
21050260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
2106c6fd2807SJeff Garzik 	const char *frozen, *desc;
2107a1e10f7eSTejun Heo 	char tries_buf[6];
2108c6fd2807SJeff Garzik 	int tag, nr_failed = 0;
2109c6fd2807SJeff Garzik 
211094ff3d54STejun Heo 	if (ehc->i.flags & ATA_EHI_QUIET)
211194ff3d54STejun Heo 		return;
211294ff3d54STejun Heo 
2113c6fd2807SJeff Garzik 	desc = NULL;
2114c6fd2807SJeff Garzik 	if (ehc->i.desc[0] != '\0')
2115c6fd2807SJeff Garzik 		desc = ehc->i.desc;
2116c6fd2807SJeff Garzik 
2117c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2118c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2119c6fd2807SJeff Garzik 
2120b1c72916STejun Heo 		if (!(qc->flags & ATA_QCFLAG_FAILED) ||
2121b1c72916STejun Heo 		    ata_dev_phys_link(qc->dev) != link ||
2122e027bd36STejun Heo 		    ((qc->flags & ATA_QCFLAG_QUIET) &&
2123e027bd36STejun Heo 		     qc->err_mask == AC_ERR_DEV))
2124c6fd2807SJeff Garzik 			continue;
2125c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
2126c6fd2807SJeff Garzik 			continue;
2127c6fd2807SJeff Garzik 
2128c6fd2807SJeff Garzik 		nr_failed++;
2129c6fd2807SJeff Garzik 	}
2130c6fd2807SJeff Garzik 
2131c6fd2807SJeff Garzik 	if (!nr_failed && !ehc->i.err_mask)
2132c6fd2807SJeff Garzik 		return;
2133c6fd2807SJeff Garzik 
2134c6fd2807SJeff Garzik 	frozen = "";
2135c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN)
2136c6fd2807SJeff Garzik 		frozen = " frozen";
2137c6fd2807SJeff Garzik 
2138a1e10f7eSTejun Heo 	memset(tries_buf, 0, sizeof(tries_buf));
2139a1e10f7eSTejun Heo 	if (ap->eh_tries < ATA_EH_MAX_TRIES)
2140a1e10f7eSTejun Heo 		snprintf(tries_buf, sizeof(tries_buf) - 1, " t%d",
2141a1e10f7eSTejun Heo 			 ap->eh_tries);
2142a1e10f7eSTejun Heo 
2143c6fd2807SJeff Garzik 	if (ehc->i.dev) {
2144c6fd2807SJeff Garzik 		ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
2145a1e10f7eSTejun Heo 			       "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2146a1e10f7eSTejun Heo 			       ehc->i.err_mask, link->sactive, ehc->i.serror,
2147a1e10f7eSTejun Heo 			       ehc->i.action, frozen, tries_buf);
2148c6fd2807SJeff Garzik 		if (desc)
2149b64bbc39STejun Heo 			ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
2150c6fd2807SJeff Garzik 	} else {
21510260731fSTejun Heo 		ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
2152a1e10f7eSTejun Heo 				"SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2153a1e10f7eSTejun Heo 				ehc->i.err_mask, link->sactive, ehc->i.serror,
2154a1e10f7eSTejun Heo 				ehc->i.action, frozen, tries_buf);
2155c6fd2807SJeff Garzik 		if (desc)
21560260731fSTejun Heo 			ata_link_printk(link, KERN_ERR, "%s\n", desc);
2157c6fd2807SJeff Garzik 	}
2158c6fd2807SJeff Garzik 
21591333e194SRobert Hancock 	if (ehc->i.serror)
2160da0e21d3STejun Heo 		ata_link_printk(link, KERN_ERR,
21611333e194SRobert Hancock 		  "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
21621333e194SRobert Hancock 		  ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
21631333e194SRobert Hancock 		  ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
21641333e194SRobert Hancock 		  ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
21651333e194SRobert Hancock 		  ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
21661333e194SRobert Hancock 		  ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
21671333e194SRobert Hancock 		  ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
21681333e194SRobert Hancock 		  ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
21691333e194SRobert Hancock 		  ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
21701333e194SRobert Hancock 		  ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
21711333e194SRobert Hancock 		  ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
21721333e194SRobert Hancock 		  ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
21731333e194SRobert Hancock 		  ehc->i.serror & SERR_CRC ? "BadCRC " : "",
21741333e194SRobert Hancock 		  ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
21751333e194SRobert Hancock 		  ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
21761333e194SRobert Hancock 		  ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
21771333e194SRobert Hancock 		  ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
21781333e194SRobert Hancock 		  ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
21791333e194SRobert Hancock 
2180c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2181c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
21828a937581STejun Heo 		struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
2183abb6a889STejun Heo 		const u8 *cdb = qc->cdb;
2184abb6a889STejun Heo 		char data_buf[20] = "";
2185abb6a889STejun Heo 		char cdb_buf[70] = "";
2186c6fd2807SJeff Garzik 
21870260731fSTejun Heo 		if (!(qc->flags & ATA_QCFLAG_FAILED) ||
2188b1c72916STejun Heo 		    ata_dev_phys_link(qc->dev) != link || !qc->err_mask)
2189c6fd2807SJeff Garzik 			continue;
2190c6fd2807SJeff Garzik 
2191abb6a889STejun Heo 		if (qc->dma_dir != DMA_NONE) {
2192abb6a889STejun Heo 			static const char *dma_str[] = {
2193abb6a889STejun Heo 				[DMA_BIDIRECTIONAL]	= "bidi",
2194abb6a889STejun Heo 				[DMA_TO_DEVICE]		= "out",
2195abb6a889STejun Heo 				[DMA_FROM_DEVICE]	= "in",
2196abb6a889STejun Heo 			};
2197abb6a889STejun Heo 			static const char *prot_str[] = {
2198abb6a889STejun Heo 				[ATA_PROT_PIO]		= "pio",
2199abb6a889STejun Heo 				[ATA_PROT_DMA]		= "dma",
2200abb6a889STejun Heo 				[ATA_PROT_NCQ]		= "ncq",
22010dc36888STejun Heo 				[ATAPI_PROT_PIO]	= "pio",
22020dc36888STejun Heo 				[ATAPI_PROT_DMA]	= "dma",
2203abb6a889STejun Heo 			};
2204abb6a889STejun Heo 
2205abb6a889STejun Heo 			snprintf(data_buf, sizeof(data_buf), " %s %u %s",
2206abb6a889STejun Heo 				 prot_str[qc->tf.protocol], qc->nbytes,
2207abb6a889STejun Heo 				 dma_str[qc->dma_dir]);
2208abb6a889STejun Heo 		}
2209abb6a889STejun Heo 
2210e39eec13SJeff Garzik 		if (ata_is_atapi(qc->tf.protocol))
2211abb6a889STejun Heo 			snprintf(cdb_buf, sizeof(cdb_buf),
2212abb6a889STejun Heo 				 "cdb %02x %02x %02x %02x %02x %02x %02x %02x  "
2213abb6a889STejun Heo 				 "%02x %02x %02x %02x %02x %02x %02x %02x\n         ",
2214abb6a889STejun Heo 				 cdb[0], cdb[1], cdb[2], cdb[3],
2215abb6a889STejun Heo 				 cdb[4], cdb[5], cdb[6], cdb[7],
2216abb6a889STejun Heo 				 cdb[8], cdb[9], cdb[10], cdb[11],
2217abb6a889STejun Heo 				 cdb[12], cdb[13], cdb[14], cdb[15]);
2218abb6a889STejun Heo 
22198a937581STejun Heo 		ata_dev_printk(qc->dev, KERN_ERR,
22208a937581STejun Heo 			"cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
2221abb6a889STejun Heo 			"tag %d%s\n         %s"
22228a937581STejun Heo 			"res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
22235335b729STejun Heo 			"Emask 0x%x (%s)%s\n",
22248a937581STejun Heo 			cmd->command, cmd->feature, cmd->nsect,
22258a937581STejun Heo 			cmd->lbal, cmd->lbam, cmd->lbah,
22268a937581STejun Heo 			cmd->hob_feature, cmd->hob_nsect,
22278a937581STejun Heo 			cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
2228abb6a889STejun Heo 			cmd->device, qc->tag, data_buf, cdb_buf,
22298a937581STejun Heo 			res->command, res->feature, res->nsect,
22308a937581STejun Heo 			res->lbal, res->lbam, res->lbah,
22318a937581STejun Heo 			res->hob_feature, res->hob_nsect,
22328a937581STejun Heo 			res->hob_lbal, res->hob_lbam, res->hob_lbah,
22335335b729STejun Heo 			res->device, qc->err_mask, ata_err_string(qc->err_mask),
22345335b729STejun Heo 			qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
22351333e194SRobert Hancock 
22361333e194SRobert Hancock 		if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
22371333e194SRobert Hancock 				    ATA_ERR)) {
22381333e194SRobert Hancock 			if (res->command & ATA_BUSY)
22391333e194SRobert Hancock 				ata_dev_printk(qc->dev, KERN_ERR,
22401333e194SRobert Hancock 				  "status: { Busy }\n");
22411333e194SRobert Hancock 			else
22421333e194SRobert Hancock 				ata_dev_printk(qc->dev, KERN_ERR,
22431333e194SRobert Hancock 				  "status: { %s%s%s%s}\n",
22441333e194SRobert Hancock 				  res->command & ATA_DRDY ? "DRDY " : "",
22451333e194SRobert Hancock 				  res->command & ATA_DF ? "DF " : "",
22461333e194SRobert Hancock 				  res->command & ATA_DRQ ? "DRQ " : "",
22471333e194SRobert Hancock 				  res->command & ATA_ERR ? "ERR " : "");
22481333e194SRobert Hancock 		}
22491333e194SRobert Hancock 
22501333e194SRobert Hancock 		if (cmd->command != ATA_CMD_PACKET &&
22511333e194SRobert Hancock 		    (res->feature & (ATA_ICRC | ATA_UNC | ATA_IDNF |
22521333e194SRobert Hancock 				     ATA_ABORTED)))
22531333e194SRobert Hancock 			ata_dev_printk(qc->dev, KERN_ERR,
22541333e194SRobert Hancock 			  "error: { %s%s%s%s}\n",
22551333e194SRobert Hancock 			  res->feature & ATA_ICRC ? "ICRC " : "",
22561333e194SRobert Hancock 			  res->feature & ATA_UNC ? "UNC " : "",
22571333e194SRobert Hancock 			  res->feature & ATA_IDNF ? "IDNF " : "",
22581333e194SRobert Hancock 			  res->feature & ATA_ABORTED ? "ABRT " : "");
2259c6fd2807SJeff Garzik 	}
2260c6fd2807SJeff Garzik }
2261c6fd2807SJeff Garzik 
22629b1e2658STejun Heo /**
22639b1e2658STejun Heo  *	ata_eh_report - report error handling to user
22649b1e2658STejun Heo  *	@ap: ATA port to report EH about
22659b1e2658STejun Heo  *
22669b1e2658STejun Heo  *	Report EH to user.
22679b1e2658STejun Heo  *
22689b1e2658STejun Heo  *	LOCKING:
22699b1e2658STejun Heo  *	None.
22709b1e2658STejun Heo  */
2271fb7fd614STejun Heo void ata_eh_report(struct ata_port *ap)
22729b1e2658STejun Heo {
22739b1e2658STejun Heo 	struct ata_link *link;
22749b1e2658STejun Heo 
22751eca4365STejun Heo 	ata_for_each_link(link, ap, HOST_FIRST)
22769b1e2658STejun Heo 		ata_eh_link_report(link);
22779b1e2658STejun Heo }
22789b1e2658STejun Heo 
2279cc0680a5STejun Heo static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
2280b1c72916STejun Heo 			unsigned int *classes, unsigned long deadline,
2281b1c72916STejun Heo 			bool clear_classes)
2282c6fd2807SJeff Garzik {
2283f58229f8STejun Heo 	struct ata_device *dev;
2284c6fd2807SJeff Garzik 
2285b1c72916STejun Heo 	if (clear_classes)
22861eca4365STejun Heo 		ata_for_each_dev(dev, link, ALL)
2287f58229f8STejun Heo 			classes[dev->devno] = ATA_DEV_UNKNOWN;
2288c6fd2807SJeff Garzik 
2289f046519fSTejun Heo 	return reset(link, classes, deadline);
2290c6fd2807SJeff Garzik }
2291c6fd2807SJeff Garzik 
2292ae791c05STejun Heo static int ata_eh_followup_srst_needed(struct ata_link *link,
22935dbfc9cbSTejun Heo 				       int rc, const unsigned int *classes)
2294c6fd2807SJeff Garzik {
229545db2f6cSTejun Heo 	if ((link->flags & ATA_LFLAG_NO_SRST) || ata_link_offline(link))
2296ae791c05STejun Heo 		return 0;
22975dbfc9cbSTejun Heo 	if (rc == -EAGAIN)
2298c6fd2807SJeff Garzik 		return 1;
2299071f44b1STejun Heo 	if (sata_pmp_supported(link->ap) && ata_is_host_link(link))
23003495de73STejun Heo 		return 1;
2301c6fd2807SJeff Garzik 	return 0;
2302c6fd2807SJeff Garzik }
2303c6fd2807SJeff Garzik 
2304fb7fd614STejun Heo int ata_eh_reset(struct ata_link *link, int classify,
2305c6fd2807SJeff Garzik 		 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
2306c6fd2807SJeff Garzik 		 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
2307c6fd2807SJeff Garzik {
2308afaa5c37STejun Heo 	struct ata_port *ap = link->ap;
2309b1c72916STejun Heo 	struct ata_link *slave = ap->slave_link;
2310936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
2311b1c72916STejun Heo 	struct ata_eh_context *sehc = &slave->eh_context;
2312c6fd2807SJeff Garzik 	unsigned int *classes = ehc->classes;
2313416dc9edSTejun Heo 	unsigned int lflags = link->flags;
2314c6fd2807SJeff Garzik 	int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
2315d8af0eb6STejun Heo 	int max_tries = 0, try = 0;
2316b1c72916STejun Heo 	struct ata_link *failed_link;
2317f58229f8STejun Heo 	struct ata_device *dev;
2318416dc9edSTejun Heo 	unsigned long deadline, now;
2319c6fd2807SJeff Garzik 	ata_reset_fn_t reset;
2320afaa5c37STejun Heo 	unsigned long flags;
2321416dc9edSTejun Heo 	u32 sstatus;
2322b1c72916STejun Heo 	int nr_unknown, rc;
2323c6fd2807SJeff Garzik 
2324932648b0STejun Heo 	/*
2325932648b0STejun Heo 	 * Prepare to reset
2326932648b0STejun Heo 	 */
2327d8af0eb6STejun Heo 	while (ata_eh_reset_timeouts[max_tries] != ULONG_MAX)
2328d8af0eb6STejun Heo 		max_tries++;
232905944bdfSTejun Heo 	if (link->flags & ATA_LFLAG_NO_HRST)
233005944bdfSTejun Heo 		hardreset = NULL;
233105944bdfSTejun Heo 	if (link->flags & ATA_LFLAG_NO_SRST)
233205944bdfSTejun Heo 		softreset = NULL;
2333d8af0eb6STejun Heo 
233419b72321STejun Heo 	/* make sure each reset attemp is at least COOL_DOWN apart */
233519b72321STejun Heo 	if (ehc->i.flags & ATA_EHI_DID_RESET) {
23360a2c0f56STejun Heo 		now = jiffies;
233719b72321STejun Heo 		WARN_ON(time_after(ehc->last_reset, now));
233819b72321STejun Heo 		deadline = ata_deadline(ehc->last_reset,
233919b72321STejun Heo 					ATA_EH_RESET_COOL_DOWN);
23400a2c0f56STejun Heo 		if (time_before(now, deadline))
23410a2c0f56STejun Heo 			schedule_timeout_uninterruptible(deadline - now);
234219b72321STejun Heo 	}
23430a2c0f56STejun Heo 
2344afaa5c37STejun Heo 	spin_lock_irqsave(ap->lock, flags);
2345afaa5c37STejun Heo 	ap->pflags |= ATA_PFLAG_RESETTING;
2346afaa5c37STejun Heo 	spin_unlock_irqrestore(ap->lock, flags);
2347afaa5c37STejun Heo 
2348cf480626STejun Heo 	ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
2349c6fd2807SJeff Garzik 
23501eca4365STejun Heo 	ata_for_each_dev(dev, link, ALL) {
2351cdeab114STejun Heo 		/* If we issue an SRST then an ATA drive (not ATAPI)
2352cdeab114STejun Heo 		 * may change configuration and be in PIO0 timing. If
2353cdeab114STejun Heo 		 * we do a hard reset (or are coming from power on)
2354cdeab114STejun Heo 		 * this is true for ATA or ATAPI. Until we've set a
2355cdeab114STejun Heo 		 * suitable controller mode we should not touch the
2356cdeab114STejun Heo 		 * bus as we may be talking too fast.
2357cdeab114STejun Heo 		 */
2358cdeab114STejun Heo 		dev->pio_mode = XFER_PIO_0;
2359cdeab114STejun Heo 
2360cdeab114STejun Heo 		/* If the controller has a pio mode setup function
2361cdeab114STejun Heo 		 * then use it to set the chipset to rights. Don't
2362cdeab114STejun Heo 		 * touch the DMA setup as that will be dealt with when
2363cdeab114STejun Heo 		 * configuring devices.
2364cdeab114STejun Heo 		 */
2365cdeab114STejun Heo 		if (ap->ops->set_piomode)
2366cdeab114STejun Heo 			ap->ops->set_piomode(ap, dev);
2367cdeab114STejun Heo 	}
2368cdeab114STejun Heo 
2369cf480626STejun Heo 	/* prefer hardreset */
2370932648b0STejun Heo 	reset = NULL;
2371cf480626STejun Heo 	ehc->i.action &= ~ATA_EH_RESET;
2372cf480626STejun Heo 	if (hardreset) {
2373cf480626STejun Heo 		reset = hardreset;
2374a674050eSTejun Heo 		ehc->i.action |= ATA_EH_HARDRESET;
23754f7faa3fSTejun Heo 	} else if (softreset) {
2376cf480626STejun Heo 		reset = softreset;
2377a674050eSTejun Heo 		ehc->i.action |= ATA_EH_SOFTRESET;
2378cf480626STejun Heo 	}
2379c6fd2807SJeff Garzik 
2380c6fd2807SJeff Garzik 	if (prereset) {
2381b1c72916STejun Heo 		unsigned long deadline = ata_deadline(jiffies,
2382b1c72916STejun Heo 						      ATA_EH_PRERESET_TIMEOUT);
2383b1c72916STejun Heo 
2384b1c72916STejun Heo 		if (slave) {
2385b1c72916STejun Heo 			sehc->i.action &= ~ATA_EH_RESET;
2386b1c72916STejun Heo 			sehc->i.action |= ehc->i.action;
2387b1c72916STejun Heo 		}
2388b1c72916STejun Heo 
2389b1c72916STejun Heo 		rc = prereset(link, deadline);
2390b1c72916STejun Heo 
2391b1c72916STejun Heo 		/* If present, do prereset on slave link too.  Reset
2392b1c72916STejun Heo 		 * is skipped iff both master and slave links report
2393b1c72916STejun Heo 		 * -ENOENT or clear ATA_EH_RESET.
2394b1c72916STejun Heo 		 */
2395b1c72916STejun Heo 		if (slave && (rc == 0 || rc == -ENOENT)) {
2396b1c72916STejun Heo 			int tmp;
2397b1c72916STejun Heo 
2398b1c72916STejun Heo 			tmp = prereset(slave, deadline);
2399b1c72916STejun Heo 			if (tmp != -ENOENT)
2400b1c72916STejun Heo 				rc = tmp;
2401b1c72916STejun Heo 
2402b1c72916STejun Heo 			ehc->i.action |= sehc->i.action;
2403b1c72916STejun Heo 		}
2404b1c72916STejun Heo 
2405c6fd2807SJeff Garzik 		if (rc) {
2406c961922bSAlan Cox 			if (rc == -ENOENT) {
2407cc0680a5STejun Heo 				ata_link_printk(link, KERN_DEBUG,
24084aa9ab67STejun Heo 						"port disabled. ignoring.\n");
2409cf480626STejun Heo 				ehc->i.action &= ~ATA_EH_RESET;
24104aa9ab67STejun Heo 
24111eca4365STejun Heo 				ata_for_each_dev(dev, link, ALL)
2412f58229f8STejun Heo 					classes[dev->devno] = ATA_DEV_NONE;
24134aa9ab67STejun Heo 
24144aa9ab67STejun Heo 				rc = 0;
2415c961922bSAlan Cox 			} else
2416cc0680a5STejun Heo 				ata_link_printk(link, KERN_ERR,
2417c6fd2807SJeff Garzik 					"prereset failed (errno=%d)\n", rc);
2418fccb6ea5STejun Heo 			goto out;
2419c6fd2807SJeff Garzik 		}
2420c6fd2807SJeff Garzik 
2421932648b0STejun Heo 		/* prereset() might have cleared ATA_EH_RESET.  If so,
2422932648b0STejun Heo 		 * bang classes and return.
2423932648b0STejun Heo 		 */
2424932648b0STejun Heo 		if (reset && !(ehc->i.action & ATA_EH_RESET)) {
24251eca4365STejun Heo 			ata_for_each_dev(dev, link, ALL)
2426f58229f8STejun Heo 				classes[dev->devno] = ATA_DEV_NONE;
2427fccb6ea5STejun Heo 			rc = 0;
2428fccb6ea5STejun Heo 			goto out;
2429c6fd2807SJeff Garzik 		}
2430932648b0STejun Heo 	}
2431c6fd2807SJeff Garzik 
2432c6fd2807SJeff Garzik  retry:
2433932648b0STejun Heo 	/*
2434932648b0STejun Heo 	 * Perform reset
2435932648b0STejun Heo 	 */
2436dc98c32cSTejun Heo 	if (ata_is_host_link(link))
2437dc98c32cSTejun Heo 		ata_eh_freeze_port(ap);
2438dc98c32cSTejun Heo 
2439341c2c95STejun Heo 	deadline = ata_deadline(jiffies, ata_eh_reset_timeouts[try++]);
244031daabdaSTejun Heo 
2441932648b0STejun Heo 	if (reset) {
2442c6fd2807SJeff Garzik 		if (verbose)
2443cc0680a5STejun Heo 			ata_link_printk(link, KERN_INFO, "%s resetting link\n",
2444c6fd2807SJeff Garzik 					reset == softreset ? "soft" : "hard");
2445c6fd2807SJeff Garzik 
2446c6fd2807SJeff Garzik 		/* mark that this EH session started with reset */
244719b72321STejun Heo 		ehc->last_reset = jiffies;
24480d64a233STejun Heo 		if (reset == hardreset)
24490d64a233STejun Heo 			ehc->i.flags |= ATA_EHI_DID_HARDRESET;
24500d64a233STejun Heo 		else
24510d64a233STejun Heo 			ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
2452c6fd2807SJeff Garzik 
2453b1c72916STejun Heo 		rc = ata_do_reset(link, reset, classes, deadline, true);
2454b1c72916STejun Heo 		if (rc && rc != -EAGAIN) {
2455b1c72916STejun Heo 			failed_link = link;
24565dbfc9cbSTejun Heo 			goto fail;
2457b1c72916STejun Heo 		}
2458c6fd2807SJeff Garzik 
2459b1c72916STejun Heo 		/* hardreset slave link if existent */
2460b1c72916STejun Heo 		if (slave && reset == hardreset) {
2461b1c72916STejun Heo 			int tmp;
2462b1c72916STejun Heo 
2463b1c72916STejun Heo 			if (verbose)
2464b1c72916STejun Heo 				ata_link_printk(slave, KERN_INFO,
2465b1c72916STejun Heo 						"hard resetting link\n");
2466b1c72916STejun Heo 
2467b1c72916STejun Heo 			ata_eh_about_to_do(slave, NULL, ATA_EH_RESET);
2468b1c72916STejun Heo 			tmp = ata_do_reset(slave, reset, classes, deadline,
2469b1c72916STejun Heo 					   false);
2470b1c72916STejun Heo 			switch (tmp) {
2471b1c72916STejun Heo 			case -EAGAIN:
2472b1c72916STejun Heo 				rc = -EAGAIN;
2473b1c72916STejun Heo 			case 0:
2474b1c72916STejun Heo 				break;
2475b1c72916STejun Heo 			default:
2476b1c72916STejun Heo 				failed_link = slave;
2477b1c72916STejun Heo 				rc = tmp;
2478b1c72916STejun Heo 				goto fail;
2479b1c72916STejun Heo 			}
2480b1c72916STejun Heo 		}
2481b1c72916STejun Heo 
2482b1c72916STejun Heo 		/* perform follow-up SRST if necessary */
2483c6fd2807SJeff Garzik 		if (reset == hardreset &&
24845dbfc9cbSTejun Heo 		    ata_eh_followup_srst_needed(link, rc, classes)) {
2485c6fd2807SJeff Garzik 			reset = softreset;
2486c6fd2807SJeff Garzik 
2487c6fd2807SJeff Garzik 			if (!reset) {
2488cc0680a5STejun Heo 				ata_link_printk(link, KERN_ERR,
2489c6fd2807SJeff Garzik 						"follow-up softreset required "
2490c6fd2807SJeff Garzik 						"but no softreset avaliable\n");
2491b1c72916STejun Heo 				failed_link = link;
2492fccb6ea5STejun Heo 				rc = -EINVAL;
249308cf69d0STejun Heo 				goto fail;
2494c6fd2807SJeff Garzik 			}
2495c6fd2807SJeff Garzik 
2496cf480626STejun Heo 			ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
2497b1c72916STejun Heo 			rc = ata_do_reset(link, reset, classes, deadline, true);
2498c6fd2807SJeff Garzik 		}
2499932648b0STejun Heo 	} else {
2500932648b0STejun Heo 		if (verbose)
2501932648b0STejun Heo 			ata_link_printk(link, KERN_INFO, "no reset method "
2502932648b0STejun Heo 					"available, skipping reset\n");
2503932648b0STejun Heo 		if (!(lflags & ATA_LFLAG_ASSUME_CLASS))
2504932648b0STejun Heo 			lflags |= ATA_LFLAG_ASSUME_ATA;
2505932648b0STejun Heo 	}
2506008a7896STejun Heo 
2507932648b0STejun Heo 	/*
2508932648b0STejun Heo 	 * Post-reset processing
2509932648b0STejun Heo 	 */
25101eca4365STejun Heo 	ata_for_each_dev(dev, link, ALL) {
2511416dc9edSTejun Heo 		/* After the reset, the device state is PIO 0 and the
2512416dc9edSTejun Heo 		 * controller state is undefined.  Reset also wakes up
2513416dc9edSTejun Heo 		 * drives from sleeping mode.
2514c6fd2807SJeff Garzik 		 */
2515f58229f8STejun Heo 		dev->pio_mode = XFER_PIO_0;
2516054a5fbaSTejun Heo 		dev->flags &= ~ATA_DFLAG_SLEEPING;
2517c6fd2807SJeff Garzik 
2518816ab897STejun Heo 		if (!ata_phys_link_offline(ata_dev_phys_link(dev))) {
25194ccd3329STejun Heo 			/* apply class override */
2520416dc9edSTejun Heo 			if (lflags & ATA_LFLAG_ASSUME_ATA)
2521ae791c05STejun Heo 				classes[dev->devno] = ATA_DEV_ATA;
2522416dc9edSTejun Heo 			else if (lflags & ATA_LFLAG_ASSUME_SEMB)
2523816ab897STejun Heo 				classes[dev->devno] = ATA_DEV_SEMB_UNSUP;
2524816ab897STejun Heo 		} else
2525816ab897STejun Heo 			classes[dev->devno] = ATA_DEV_NONE;
2526ae791c05STejun Heo 	}
2527ae791c05STejun Heo 
2528008a7896STejun Heo 	/* record current link speed */
2529936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2530936fd732STejun Heo 		link->sata_spd = (sstatus >> 4) & 0xf;
2531b1c72916STejun Heo 	if (slave && sata_scr_read(slave, SCR_STATUS, &sstatus) == 0)
2532b1c72916STejun Heo 		slave->sata_spd = (sstatus >> 4) & 0xf;
2533008a7896STejun Heo 
2534dc98c32cSTejun Heo 	/* thaw the port */
2535dc98c32cSTejun Heo 	if (ata_is_host_link(link))
2536dc98c32cSTejun Heo 		ata_eh_thaw_port(ap);
2537dc98c32cSTejun Heo 
2538f046519fSTejun Heo 	/* postreset() should clear hardware SError.  Although SError
2539f046519fSTejun Heo 	 * is cleared during link resume, clearing SError here is
2540f046519fSTejun Heo 	 * necessary as some PHYs raise hotplug events after SRST.
2541f046519fSTejun Heo 	 * This introduces race condition where hotplug occurs between
2542f046519fSTejun Heo 	 * reset and here.  This race is mediated by cross checking
2543f046519fSTejun Heo 	 * link onlineness and classification result later.
2544f046519fSTejun Heo 	 */
2545b1c72916STejun Heo 	if (postreset) {
2546cc0680a5STejun Heo 		postreset(link, classes);
2547b1c72916STejun Heo 		if (slave)
2548b1c72916STejun Heo 			postreset(slave, classes);
2549b1c72916STejun Heo 	}
2550c6fd2807SJeff Garzik 
2551f046519fSTejun Heo 	/* clear cached SError */
2552f046519fSTejun Heo 	spin_lock_irqsave(link->ap->lock, flags);
2553f046519fSTejun Heo 	link->eh_info.serror = 0;
2554b1c72916STejun Heo 	if (slave)
2555b1c72916STejun Heo 		slave->eh_info.serror = 0;
2556f046519fSTejun Heo 	spin_unlock_irqrestore(link->ap->lock, flags);
2557f046519fSTejun Heo 
2558f046519fSTejun Heo 	/* Make sure onlineness and classification result correspond.
2559f046519fSTejun Heo 	 * Hotplug could have happened during reset and some
2560f046519fSTejun Heo 	 * controllers fail to wait while a drive is spinning up after
2561f046519fSTejun Heo 	 * being hotplugged causing misdetection.  By cross checking
2562f046519fSTejun Heo 	 * link onlineness and classification result, those conditions
2563f046519fSTejun Heo 	 * can be reliably detected and retried.
2564f046519fSTejun Heo 	 */
2565b1c72916STejun Heo 	nr_unknown = 0;
25661eca4365STejun Heo 	ata_for_each_dev(dev, link, ALL) {
2567f046519fSTejun Heo 		/* convert all ATA_DEV_UNKNOWN to ATA_DEV_NONE */
2568b1c72916STejun Heo 		if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
2569f046519fSTejun Heo 			classes[dev->devno] = ATA_DEV_NONE;
2570b1c72916STejun Heo 			if (ata_phys_link_online(ata_dev_phys_link(dev)))
2571b1c72916STejun Heo 				nr_unknown++;
2572b1c72916STejun Heo 		}
2573f046519fSTejun Heo 	}
2574f046519fSTejun Heo 
2575b1c72916STejun Heo 	if (classify && nr_unknown) {
2576f046519fSTejun Heo 		if (try < max_tries) {
2577f046519fSTejun Heo 			ata_link_printk(link, KERN_WARNING, "link online but "
2578f046519fSTejun Heo 				       "device misclassified, retrying\n");
2579b1c72916STejun Heo 			failed_link = link;
2580f046519fSTejun Heo 			rc = -EAGAIN;
2581f046519fSTejun Heo 			goto fail;
2582f046519fSTejun Heo 		}
2583f046519fSTejun Heo 		ata_link_printk(link, KERN_WARNING,
2584f046519fSTejun Heo 			       "link online but device misclassified, "
2585f046519fSTejun Heo 			       "device detection might fail\n");
2586f046519fSTejun Heo 	}
2587f046519fSTejun Heo 
2588c6fd2807SJeff Garzik 	/* reset successful, schedule revalidation */
2589cf480626STejun Heo 	ata_eh_done(link, NULL, ATA_EH_RESET);
2590b1c72916STejun Heo 	if (slave)
2591b1c72916STejun Heo 		ata_eh_done(slave, NULL, ATA_EH_RESET);
259219b72321STejun Heo 	ehc->last_reset = jiffies;	/* update to completion time */
2593c6fd2807SJeff Garzik 	ehc->i.action |= ATA_EH_REVALIDATE;
2594416dc9edSTejun Heo 
2595416dc9edSTejun Heo 	rc = 0;
2596fccb6ea5STejun Heo  out:
2597fccb6ea5STejun Heo 	/* clear hotplug flag */
2598fccb6ea5STejun Heo 	ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2599b1c72916STejun Heo 	if (slave)
2600b1c72916STejun Heo 		sehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2601afaa5c37STejun Heo 
2602afaa5c37STejun Heo 	spin_lock_irqsave(ap->lock, flags);
2603afaa5c37STejun Heo 	ap->pflags &= ~ATA_PFLAG_RESETTING;
2604afaa5c37STejun Heo 	spin_unlock_irqrestore(ap->lock, flags);
2605afaa5c37STejun Heo 
2606c6fd2807SJeff Garzik 	return rc;
2607416dc9edSTejun Heo 
2608416dc9edSTejun Heo  fail:
26095958e302STejun Heo 	/* if SCR isn't accessible on a fan-out port, PMP needs to be reset */
26105958e302STejun Heo 	if (!ata_is_host_link(link) &&
26115958e302STejun Heo 	    sata_scr_read(link, SCR_STATUS, &sstatus))
26125958e302STejun Heo 		rc = -ERESTART;
26135958e302STejun Heo 
2614416dc9edSTejun Heo 	if (rc == -ERESTART || try >= max_tries)
2615416dc9edSTejun Heo 		goto out;
2616416dc9edSTejun Heo 
2617416dc9edSTejun Heo 	now = jiffies;
2618416dc9edSTejun Heo 	if (time_before(now, deadline)) {
2619416dc9edSTejun Heo 		unsigned long delta = deadline - now;
2620416dc9edSTejun Heo 
2621b1c72916STejun Heo 		ata_link_printk(failed_link, KERN_WARNING,
26220a2c0f56STejun Heo 			"reset failed (errno=%d), retrying in %u secs\n",
26230a2c0f56STejun Heo 			rc, DIV_ROUND_UP(jiffies_to_msecs(delta), 1000));
2624416dc9edSTejun Heo 
2625416dc9edSTejun Heo 		while (delta)
2626416dc9edSTejun Heo 			delta = schedule_timeout_uninterruptible(delta);
2627416dc9edSTejun Heo 	}
2628416dc9edSTejun Heo 
2629b1c72916STejun Heo 	if (try == max_tries - 1) {
2630*a07d499bSTejun Heo 		sata_down_spd_limit(link, 0);
2631b1c72916STejun Heo 		if (slave)
2632*a07d499bSTejun Heo 			sata_down_spd_limit(slave, 0);
2633b1c72916STejun Heo 	} else if (rc == -EPIPE)
2634*a07d499bSTejun Heo 		sata_down_spd_limit(failed_link, 0);
2635b1c72916STejun Heo 
2636416dc9edSTejun Heo 	if (hardreset)
2637416dc9edSTejun Heo 		reset = hardreset;
2638416dc9edSTejun Heo 	goto retry;
2639c6fd2807SJeff Garzik }
2640c6fd2807SJeff Garzik 
264145fabbb7SElias Oltmanns static inline void ata_eh_pull_park_action(struct ata_port *ap)
264245fabbb7SElias Oltmanns {
264345fabbb7SElias Oltmanns 	struct ata_link *link;
264445fabbb7SElias Oltmanns 	struct ata_device *dev;
264545fabbb7SElias Oltmanns 	unsigned long flags;
264645fabbb7SElias Oltmanns 
264745fabbb7SElias Oltmanns 	/*
264845fabbb7SElias Oltmanns 	 * This function can be thought of as an extended version of
264945fabbb7SElias Oltmanns 	 * ata_eh_about_to_do() specially crafted to accommodate the
265045fabbb7SElias Oltmanns 	 * requirements of ATA_EH_PARK handling. Since the EH thread
265145fabbb7SElias Oltmanns 	 * does not leave the do {} while () loop in ata_eh_recover as
265245fabbb7SElias Oltmanns 	 * long as the timeout for a park request to *one* device on
265345fabbb7SElias Oltmanns 	 * the port has not expired, and since we still want to pick
265445fabbb7SElias Oltmanns 	 * up park requests to other devices on the same port or
265545fabbb7SElias Oltmanns 	 * timeout updates for the same device, we have to pull
265645fabbb7SElias Oltmanns 	 * ATA_EH_PARK actions from eh_info into eh_context.i
265745fabbb7SElias Oltmanns 	 * ourselves at the beginning of each pass over the loop.
265845fabbb7SElias Oltmanns 	 *
265945fabbb7SElias Oltmanns 	 * Additionally, all write accesses to &ap->park_req_pending
266045fabbb7SElias Oltmanns 	 * through INIT_COMPLETION() (see below) or complete_all()
266145fabbb7SElias Oltmanns 	 * (see ata_scsi_park_store()) are protected by the host lock.
266245fabbb7SElias Oltmanns 	 * As a result we have that park_req_pending.done is zero on
266345fabbb7SElias Oltmanns 	 * exit from this function, i.e. when ATA_EH_PARK actions for
266445fabbb7SElias Oltmanns 	 * *all* devices on port ap have been pulled into the
266545fabbb7SElias Oltmanns 	 * respective eh_context structs. If, and only if,
266645fabbb7SElias Oltmanns 	 * park_req_pending.done is non-zero by the time we reach
266745fabbb7SElias Oltmanns 	 * wait_for_completion_timeout(), another ATA_EH_PARK action
266845fabbb7SElias Oltmanns 	 * has been scheduled for at least one of the devices on port
266945fabbb7SElias Oltmanns 	 * ap and we have to cycle over the do {} while () loop in
267045fabbb7SElias Oltmanns 	 * ata_eh_recover() again.
267145fabbb7SElias Oltmanns 	 */
267245fabbb7SElias Oltmanns 
267345fabbb7SElias Oltmanns 	spin_lock_irqsave(ap->lock, flags);
267445fabbb7SElias Oltmanns 	INIT_COMPLETION(ap->park_req_pending);
26751eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE) {
26761eca4365STejun Heo 		ata_for_each_dev(dev, link, ALL) {
267745fabbb7SElias Oltmanns 			struct ata_eh_info *ehi = &link->eh_info;
267845fabbb7SElias Oltmanns 
267945fabbb7SElias Oltmanns 			link->eh_context.i.dev_action[dev->devno] |=
268045fabbb7SElias Oltmanns 				ehi->dev_action[dev->devno] & ATA_EH_PARK;
268145fabbb7SElias Oltmanns 			ata_eh_clear_action(link, dev, ehi, ATA_EH_PARK);
268245fabbb7SElias Oltmanns 		}
268345fabbb7SElias Oltmanns 	}
268445fabbb7SElias Oltmanns 	spin_unlock_irqrestore(ap->lock, flags);
268545fabbb7SElias Oltmanns }
268645fabbb7SElias Oltmanns 
268745fabbb7SElias Oltmanns static void ata_eh_park_issue_cmd(struct ata_device *dev, int park)
268845fabbb7SElias Oltmanns {
268945fabbb7SElias Oltmanns 	struct ata_eh_context *ehc = &dev->link->eh_context;
269045fabbb7SElias Oltmanns 	struct ata_taskfile tf;
269145fabbb7SElias Oltmanns 	unsigned int err_mask;
269245fabbb7SElias Oltmanns 
269345fabbb7SElias Oltmanns 	ata_tf_init(dev, &tf);
269445fabbb7SElias Oltmanns 	if (park) {
269545fabbb7SElias Oltmanns 		ehc->unloaded_mask |= 1 << dev->devno;
269645fabbb7SElias Oltmanns 		tf.command = ATA_CMD_IDLEIMMEDIATE;
269745fabbb7SElias Oltmanns 		tf.feature = 0x44;
269845fabbb7SElias Oltmanns 		tf.lbal = 0x4c;
269945fabbb7SElias Oltmanns 		tf.lbam = 0x4e;
270045fabbb7SElias Oltmanns 		tf.lbah = 0x55;
270145fabbb7SElias Oltmanns 	} else {
270245fabbb7SElias Oltmanns 		ehc->unloaded_mask &= ~(1 << dev->devno);
270345fabbb7SElias Oltmanns 		tf.command = ATA_CMD_CHK_POWER;
270445fabbb7SElias Oltmanns 	}
270545fabbb7SElias Oltmanns 
270645fabbb7SElias Oltmanns 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
270745fabbb7SElias Oltmanns 	tf.protocol |= ATA_PROT_NODATA;
270845fabbb7SElias Oltmanns 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
270945fabbb7SElias Oltmanns 	if (park && (err_mask || tf.lbal != 0xc4)) {
271045fabbb7SElias Oltmanns 		ata_dev_printk(dev, KERN_ERR, "head unload failed!\n");
271145fabbb7SElias Oltmanns 		ehc->unloaded_mask &= ~(1 << dev->devno);
271245fabbb7SElias Oltmanns 	}
271345fabbb7SElias Oltmanns }
271445fabbb7SElias Oltmanns 
27150260731fSTejun Heo static int ata_eh_revalidate_and_attach(struct ata_link *link,
2716c6fd2807SJeff Garzik 					struct ata_device **r_failed_dev)
2717c6fd2807SJeff Garzik {
27180260731fSTejun Heo 	struct ata_port *ap = link->ap;
27190260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
2720c6fd2807SJeff Garzik 	struct ata_device *dev;
27218c3c52a8STejun Heo 	unsigned int new_mask = 0;
2722c6fd2807SJeff Garzik 	unsigned long flags;
2723f58229f8STejun Heo 	int rc = 0;
2724c6fd2807SJeff Garzik 
2725c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
2726c6fd2807SJeff Garzik 
27278c3c52a8STejun Heo 	/* For PATA drive side cable detection to work, IDENTIFY must
27288c3c52a8STejun Heo 	 * be done backwards such that PDIAG- is released by the slave
27298c3c52a8STejun Heo 	 * device before the master device is identified.
27308c3c52a8STejun Heo 	 */
27311eca4365STejun Heo 	ata_for_each_dev(dev, link, ALL_REVERSE) {
2732f58229f8STejun Heo 		unsigned int action = ata_eh_dev_action(dev);
2733f58229f8STejun Heo 		unsigned int readid_flags = 0;
2734c6fd2807SJeff Garzik 
2735bff04647STejun Heo 		if (ehc->i.flags & ATA_EHI_DID_RESET)
2736bff04647STejun Heo 			readid_flags |= ATA_READID_POSTRESET;
2737bff04647STejun Heo 
27389666f400STejun Heo 		if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
2739633273a3STejun Heo 			WARN_ON(dev->class == ATA_DEV_PMP);
2740633273a3STejun Heo 
2741b1c72916STejun Heo 			if (ata_phys_link_offline(ata_dev_phys_link(dev))) {
2742c6fd2807SJeff Garzik 				rc = -EIO;
27438c3c52a8STejun Heo 				goto err;
2744c6fd2807SJeff Garzik 			}
2745c6fd2807SJeff Garzik 
27460260731fSTejun Heo 			ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
2747422c9daaSTejun Heo 			rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
2748422c9daaSTejun Heo 						readid_flags);
2749c6fd2807SJeff Garzik 			if (rc)
27508c3c52a8STejun Heo 				goto err;
2751c6fd2807SJeff Garzik 
27520260731fSTejun Heo 			ata_eh_done(link, dev, ATA_EH_REVALIDATE);
2753c6fd2807SJeff Garzik 
2754baa1e78aSTejun Heo 			/* Configuration may have changed, reconfigure
2755baa1e78aSTejun Heo 			 * transfer mode.
2756baa1e78aSTejun Heo 			 */
2757baa1e78aSTejun Heo 			ehc->i.flags |= ATA_EHI_SETMODE;
2758baa1e78aSTejun Heo 
2759c6fd2807SJeff Garzik 			/* schedule the scsi_rescan_device() here */
2760c6fd2807SJeff Garzik 			queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
2761c6fd2807SJeff Garzik 		} else if (dev->class == ATA_DEV_UNKNOWN &&
2762c6fd2807SJeff Garzik 			   ehc->tries[dev->devno] &&
2763c6fd2807SJeff Garzik 			   ata_class_enabled(ehc->classes[dev->devno])) {
2764c6fd2807SJeff Garzik 			dev->class = ehc->classes[dev->devno];
2765c6fd2807SJeff Garzik 
2766633273a3STejun Heo 			if (dev->class == ATA_DEV_PMP)
2767633273a3STejun Heo 				rc = sata_pmp_attach(dev);
2768633273a3STejun Heo 			else
2769633273a3STejun Heo 				rc = ata_dev_read_id(dev, &dev->class,
2770633273a3STejun Heo 						     readid_flags, dev->id);
27718c3c52a8STejun Heo 			switch (rc) {
27728c3c52a8STejun Heo 			case 0:
277399cf610aSTejun Heo 				/* clear error info accumulated during probe */
277499cf610aSTejun Heo 				ata_ering_clear(&dev->ering);
2775f58229f8STejun Heo 				new_mask |= 1 << dev->devno;
27768c3c52a8STejun Heo 				break;
27778c3c52a8STejun Heo 			case -ENOENT:
277855a8e2c8STejun Heo 				/* IDENTIFY was issued to non-existent
277955a8e2c8STejun Heo 				 * device.  No need to reset.  Just
278055a8e2c8STejun Heo 				 * thaw and kill the device.
278155a8e2c8STejun Heo 				 */
278255a8e2c8STejun Heo 				ata_eh_thaw_port(ap);
278355a8e2c8STejun Heo 				dev->class = ATA_DEV_UNKNOWN;
2784c6fd2807SJeff Garzik 				break;
27858c3c52a8STejun Heo 			default:
27868c3c52a8STejun Heo 				dev->class = ATA_DEV_UNKNOWN;
27878c3c52a8STejun Heo 				goto err;
27888c3c52a8STejun Heo 			}
27898c3c52a8STejun Heo 		}
2790c6fd2807SJeff Garzik 	}
2791c6fd2807SJeff Garzik 
2792c1c4e8d5STejun Heo 	/* PDIAG- should have been released, ask cable type if post-reset */
279333267325STejun Heo 	if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) {
279433267325STejun Heo 		if (ap->ops->cable_detect)
2795c1c4e8d5STejun Heo 			ap->cbl = ap->ops->cable_detect(ap);
279633267325STejun Heo 		ata_force_cbl(ap);
279733267325STejun Heo 	}
2798c1c4e8d5STejun Heo 
27998c3c52a8STejun Heo 	/* Configure new devices forward such that user doesn't see
28008c3c52a8STejun Heo 	 * device detection messages backwards.
28018c3c52a8STejun Heo 	 */
28021eca4365STejun Heo 	ata_for_each_dev(dev, link, ALL) {
2803633273a3STejun Heo 		if (!(new_mask & (1 << dev->devno)) ||
2804633273a3STejun Heo 		    dev->class == ATA_DEV_PMP)
28058c3c52a8STejun Heo 			continue;
28068c3c52a8STejun Heo 
28078c3c52a8STejun Heo 		ehc->i.flags |= ATA_EHI_PRINTINFO;
28088c3c52a8STejun Heo 		rc = ata_dev_configure(dev);
28098c3c52a8STejun Heo 		ehc->i.flags &= ~ATA_EHI_PRINTINFO;
28108c3c52a8STejun Heo 		if (rc)
28118c3c52a8STejun Heo 			goto err;
28128c3c52a8STejun Heo 
2813c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
2814c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
2815c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
2816baa1e78aSTejun Heo 
281755a8e2c8STejun Heo 		/* new device discovered, configure xfermode */
2818baa1e78aSTejun Heo 		ehc->i.flags |= ATA_EHI_SETMODE;
2819c6fd2807SJeff Garzik 	}
2820c6fd2807SJeff Garzik 
28218c3c52a8STejun Heo 	return 0;
28228c3c52a8STejun Heo 
28238c3c52a8STejun Heo  err:
2824c6fd2807SJeff Garzik 	*r_failed_dev = dev;
28258c3c52a8STejun Heo 	DPRINTK("EXIT rc=%d\n", rc);
2826c6fd2807SJeff Garzik 	return rc;
2827c6fd2807SJeff Garzik }
2828c6fd2807SJeff Garzik 
28296f1d1e3aSTejun Heo /**
28306f1d1e3aSTejun Heo  *	ata_set_mode - Program timings and issue SET FEATURES - XFER
28316f1d1e3aSTejun Heo  *	@link: link on which timings will be programmed
28326f1d1e3aSTejun Heo  *	@r_failed_dev: out paramter for failed device
28336f1d1e3aSTejun Heo  *
28346f1d1e3aSTejun Heo  *	Set ATA device disk transfer mode (PIO3, UDMA6, etc.).  If
28356f1d1e3aSTejun Heo  *	ata_set_mode() fails, pointer to the failing device is
28366f1d1e3aSTejun Heo  *	returned in @r_failed_dev.
28376f1d1e3aSTejun Heo  *
28386f1d1e3aSTejun Heo  *	LOCKING:
28396f1d1e3aSTejun Heo  *	PCI/etc. bus probe sem.
28406f1d1e3aSTejun Heo  *
28416f1d1e3aSTejun Heo  *	RETURNS:
28426f1d1e3aSTejun Heo  *	0 on success, negative errno otherwise
28436f1d1e3aSTejun Heo  */
28446f1d1e3aSTejun Heo int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
28456f1d1e3aSTejun Heo {
28466f1d1e3aSTejun Heo 	struct ata_port *ap = link->ap;
284700115e0fSTejun Heo 	struct ata_device *dev;
284800115e0fSTejun Heo 	int rc;
28496f1d1e3aSTejun Heo 
285076326ac1STejun Heo 	/* if data transfer is verified, clear DUBIOUS_XFER on ering top */
28511eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
285276326ac1STejun Heo 		if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
285376326ac1STejun Heo 			struct ata_ering_entry *ent;
285476326ac1STejun Heo 
285576326ac1STejun Heo 			ent = ata_ering_top(&dev->ering);
285676326ac1STejun Heo 			if (ent)
285776326ac1STejun Heo 				ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER;
285876326ac1STejun Heo 		}
285976326ac1STejun Heo 	}
286076326ac1STejun Heo 
28616f1d1e3aSTejun Heo 	/* has private set_mode? */
28626f1d1e3aSTejun Heo 	if (ap->ops->set_mode)
286300115e0fSTejun Heo 		rc = ap->ops->set_mode(link, r_failed_dev);
286400115e0fSTejun Heo 	else
286500115e0fSTejun Heo 		rc = ata_do_set_mode(link, r_failed_dev);
286600115e0fSTejun Heo 
286700115e0fSTejun Heo 	/* if transfer mode has changed, set DUBIOUS_XFER on device */
28681eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
286900115e0fSTejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
287000115e0fSTejun Heo 		u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
287100115e0fSTejun Heo 		u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
287200115e0fSTejun Heo 
287300115e0fSTejun Heo 		if (dev->xfer_mode != saved_xfer_mode ||
287400115e0fSTejun Heo 		    ata_ncq_enabled(dev) != saved_ncq)
287500115e0fSTejun Heo 			dev->flags |= ATA_DFLAG_DUBIOUS_XFER;
287600115e0fSTejun Heo 	}
287700115e0fSTejun Heo 
287800115e0fSTejun Heo 	return rc;
28796f1d1e3aSTejun Heo }
28806f1d1e3aSTejun Heo 
288111fc33daSTejun Heo /**
288211fc33daSTejun Heo  *	atapi_eh_clear_ua - Clear ATAPI UNIT ATTENTION after reset
288311fc33daSTejun Heo  *	@dev: ATAPI device to clear UA for
288411fc33daSTejun Heo  *
288511fc33daSTejun Heo  *	Resets and other operations can make an ATAPI device raise
288611fc33daSTejun Heo  *	UNIT ATTENTION which causes the next operation to fail.  This
288711fc33daSTejun Heo  *	function clears UA.
288811fc33daSTejun Heo  *
288911fc33daSTejun Heo  *	LOCKING:
289011fc33daSTejun Heo  *	EH context (may sleep).
289111fc33daSTejun Heo  *
289211fc33daSTejun Heo  *	RETURNS:
289311fc33daSTejun Heo  *	0 on success, -errno on failure.
289411fc33daSTejun Heo  */
289511fc33daSTejun Heo static int atapi_eh_clear_ua(struct ata_device *dev)
289611fc33daSTejun Heo {
289711fc33daSTejun Heo 	int i;
289811fc33daSTejun Heo 
289911fc33daSTejun Heo 	for (i = 0; i < ATA_EH_UA_TRIES; i++) {
290011fc33daSTejun Heo 		u8 sense_buffer[SCSI_SENSE_BUFFERSIZE];
290111fc33daSTejun Heo 		u8 sense_key = 0;
290211fc33daSTejun Heo 		unsigned int err_mask;
290311fc33daSTejun Heo 
290411fc33daSTejun Heo 		err_mask = atapi_eh_tur(dev, &sense_key);
290511fc33daSTejun Heo 		if (err_mask != 0 && err_mask != AC_ERR_DEV) {
290611fc33daSTejun Heo 			ata_dev_printk(dev, KERN_WARNING, "TEST_UNIT_READY "
290711fc33daSTejun Heo 				"failed (err_mask=0x%x)\n", err_mask);
290811fc33daSTejun Heo 			return -EIO;
290911fc33daSTejun Heo 		}
291011fc33daSTejun Heo 
291111fc33daSTejun Heo 		if (!err_mask || sense_key != UNIT_ATTENTION)
291211fc33daSTejun Heo 			return 0;
291311fc33daSTejun Heo 
291411fc33daSTejun Heo 		err_mask = atapi_eh_request_sense(dev, sense_buffer, sense_key);
291511fc33daSTejun Heo 		if (err_mask) {
291611fc33daSTejun Heo 			ata_dev_printk(dev, KERN_WARNING, "failed to clear "
291711fc33daSTejun Heo 				"UNIT ATTENTION (err_mask=0x%x)\n", err_mask);
291811fc33daSTejun Heo 			return -EIO;
291911fc33daSTejun Heo 		}
292011fc33daSTejun Heo 	}
292111fc33daSTejun Heo 
292211fc33daSTejun Heo 	ata_dev_printk(dev, KERN_WARNING,
292311fc33daSTejun Heo 		"UNIT ATTENTION persists after %d tries\n", ATA_EH_UA_TRIES);
292411fc33daSTejun Heo 
292511fc33daSTejun Heo 	return 0;
292611fc33daSTejun Heo }
292711fc33daSTejun Heo 
29280260731fSTejun Heo static int ata_link_nr_enabled(struct ata_link *link)
2929c6fd2807SJeff Garzik {
2930f58229f8STejun Heo 	struct ata_device *dev;
2931f58229f8STejun Heo 	int cnt = 0;
2932c6fd2807SJeff Garzik 
29331eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED)
2934c6fd2807SJeff Garzik 		cnt++;
2935c6fd2807SJeff Garzik 	return cnt;
2936c6fd2807SJeff Garzik }
2937c6fd2807SJeff Garzik 
29380260731fSTejun Heo static int ata_link_nr_vacant(struct ata_link *link)
2939c6fd2807SJeff Garzik {
2940f58229f8STejun Heo 	struct ata_device *dev;
2941f58229f8STejun Heo 	int cnt = 0;
2942c6fd2807SJeff Garzik 
29431eca4365STejun Heo 	ata_for_each_dev(dev, link, ALL)
2944f58229f8STejun Heo 		if (dev->class == ATA_DEV_UNKNOWN)
2945c6fd2807SJeff Garzik 			cnt++;
2946c6fd2807SJeff Garzik 	return cnt;
2947c6fd2807SJeff Garzik }
2948c6fd2807SJeff Garzik 
29490260731fSTejun Heo static int ata_eh_skip_recovery(struct ata_link *link)
2950c6fd2807SJeff Garzik {
2951672b2d65STejun Heo 	struct ata_port *ap = link->ap;
29520260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
2953f58229f8STejun Heo 	struct ata_device *dev;
2954c6fd2807SJeff Garzik 
2955f9df58cbSTejun Heo 	/* skip disabled links */
2956f9df58cbSTejun Heo 	if (link->flags & ATA_LFLAG_DISABLED)
2957f9df58cbSTejun Heo 		return 1;
2958f9df58cbSTejun Heo 
2959672b2d65STejun Heo 	/* thaw frozen port and recover failed devices */
2960672b2d65STejun Heo 	if ((ap->pflags & ATA_PFLAG_FROZEN) || ata_link_nr_enabled(link))
2961672b2d65STejun Heo 		return 0;
2962672b2d65STejun Heo 
2963672b2d65STejun Heo 	/* reset at least once if reset is requested */
2964672b2d65STejun Heo 	if ((ehc->i.action & ATA_EH_RESET) &&
2965672b2d65STejun Heo 	    !(ehc->i.flags & ATA_EHI_DID_RESET))
2966c6fd2807SJeff Garzik 		return 0;
2967c6fd2807SJeff Garzik 
2968c6fd2807SJeff Garzik 	/* skip if class codes for all vacant slots are ATA_DEV_NONE */
29691eca4365STejun Heo 	ata_for_each_dev(dev, link, ALL) {
2970c6fd2807SJeff Garzik 		if (dev->class == ATA_DEV_UNKNOWN &&
2971c6fd2807SJeff Garzik 		    ehc->classes[dev->devno] != ATA_DEV_NONE)
2972c6fd2807SJeff Garzik 			return 0;
2973c6fd2807SJeff Garzik 	}
2974c6fd2807SJeff Garzik 
2975c6fd2807SJeff Garzik 	return 1;
2976c6fd2807SJeff Garzik }
2977c6fd2807SJeff Garzik 
297802c05a27STejun Heo static int ata_eh_schedule_probe(struct ata_device *dev)
297902c05a27STejun Heo {
298002c05a27STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
298102c05a27STejun Heo 
298202c05a27STejun Heo 	if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
298302c05a27STejun Heo 	    (ehc->did_probe_mask & (1 << dev->devno)))
298402c05a27STejun Heo 		return 0;
298502c05a27STejun Heo 
298602c05a27STejun Heo 	ata_eh_detach_dev(dev);
298702c05a27STejun Heo 	ata_dev_init(dev);
298802c05a27STejun Heo 	ehc->did_probe_mask |= (1 << dev->devno);
2989cf480626STejun Heo 	ehc->i.action |= ATA_EH_RESET;
299000115e0fSTejun Heo 	ehc->saved_xfer_mode[dev->devno] = 0;
299100115e0fSTejun Heo 	ehc->saved_ncq_enabled &= ~(1 << dev->devno);
299202c05a27STejun Heo 
299302c05a27STejun Heo 	return 1;
299402c05a27STejun Heo }
299502c05a27STejun Heo 
29969b1e2658STejun Heo static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
2997fee7ca72STejun Heo {
29989af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
2999fee7ca72STejun Heo 
3000fee7ca72STejun Heo 	ehc->tries[dev->devno]--;
3001fee7ca72STejun Heo 
3002fee7ca72STejun Heo 	switch (err) {
3003fee7ca72STejun Heo 	case -ENODEV:
3004fee7ca72STejun Heo 		/* device missing or wrong IDENTIFY data, schedule probing */
3005fee7ca72STejun Heo 		ehc->i.probe_mask |= (1 << dev->devno);
3006fee7ca72STejun Heo 	case -EINVAL:
3007fee7ca72STejun Heo 		/* give it just one more chance */
3008fee7ca72STejun Heo 		ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
3009fee7ca72STejun Heo 	case -EIO:
3010d89293abSTejun Heo 		if (ehc->tries[dev->devno] == 1) {
3011fee7ca72STejun Heo 			/* This is the last chance, better to slow
3012fee7ca72STejun Heo 			 * down than lose it.
3013fee7ca72STejun Heo 			 */
3014*a07d499bSTejun Heo 			sata_down_spd_limit(ata_dev_phys_link(dev), 0);
3015d89293abSTejun Heo 			if (dev->pio_mode > XFER_PIO_0)
3016fee7ca72STejun Heo 				ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
3017fee7ca72STejun Heo 		}
3018fee7ca72STejun Heo 	}
3019fee7ca72STejun Heo 
3020fee7ca72STejun Heo 	if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
3021fee7ca72STejun Heo 		/* disable device if it has used up all its chances */
3022fee7ca72STejun Heo 		ata_dev_disable(dev);
3023fee7ca72STejun Heo 
3024fee7ca72STejun Heo 		/* detach if offline */
3025b1c72916STejun Heo 		if (ata_phys_link_offline(ata_dev_phys_link(dev)))
3026fee7ca72STejun Heo 			ata_eh_detach_dev(dev);
3027fee7ca72STejun Heo 
302802c05a27STejun Heo 		/* schedule probe if necessary */
302987fbc5a0STejun Heo 		if (ata_eh_schedule_probe(dev)) {
3030fee7ca72STejun Heo 			ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
303187fbc5a0STejun Heo 			memset(ehc->cmd_timeout_idx[dev->devno], 0,
303287fbc5a0STejun Heo 			       sizeof(ehc->cmd_timeout_idx[dev->devno]));
303387fbc5a0STejun Heo 		}
30349b1e2658STejun Heo 
30359b1e2658STejun Heo 		return 1;
3036fee7ca72STejun Heo 	} else {
3037cf480626STejun Heo 		ehc->i.action |= ATA_EH_RESET;
30389b1e2658STejun Heo 		return 0;
3039fee7ca72STejun Heo 	}
3040fee7ca72STejun Heo }
3041fee7ca72STejun Heo 
3042c6fd2807SJeff Garzik /**
3043c6fd2807SJeff Garzik  *	ata_eh_recover - recover host port after error
3044c6fd2807SJeff Garzik  *	@ap: host port to recover
3045c6fd2807SJeff Garzik  *	@prereset: prereset method (can be NULL)
3046c6fd2807SJeff Garzik  *	@softreset: softreset method (can be NULL)
3047c6fd2807SJeff Garzik  *	@hardreset: hardreset method (can be NULL)
3048c6fd2807SJeff Garzik  *	@postreset: postreset method (can be NULL)
30499b1e2658STejun Heo  *	@r_failed_link: out parameter for failed link
3050c6fd2807SJeff Garzik  *
3051c6fd2807SJeff Garzik  *	This is the alpha and omega, eum and yang, heart and soul of
3052c6fd2807SJeff Garzik  *	libata exception handling.  On entry, actions required to
30539b1e2658STejun Heo  *	recover each link and hotplug requests are recorded in the
30549b1e2658STejun Heo  *	link's eh_context.  This function executes all the operations
30559b1e2658STejun Heo  *	with appropriate retrials and fallbacks to resurrect failed
3056c6fd2807SJeff Garzik  *	devices, detach goners and greet newcomers.
3057c6fd2807SJeff Garzik  *
3058c6fd2807SJeff Garzik  *	LOCKING:
3059c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
3060c6fd2807SJeff Garzik  *
3061c6fd2807SJeff Garzik  *	RETURNS:
3062c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
3063c6fd2807SJeff Garzik  */
3064fb7fd614STejun Heo int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
3065c6fd2807SJeff Garzik 		   ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
30669b1e2658STejun Heo 		   ata_postreset_fn_t postreset,
30679b1e2658STejun Heo 		   struct ata_link **r_failed_link)
3068c6fd2807SJeff Garzik {
30699b1e2658STejun Heo 	struct ata_link *link;
3070c6fd2807SJeff Garzik 	struct ata_device *dev;
30710a2c0f56STejun Heo 	int nr_failed_devs;
3072dc98c32cSTejun Heo 	int rc;
307345fabbb7SElias Oltmanns 	unsigned long flags, deadline;
3074c6fd2807SJeff Garzik 
3075c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
3076c6fd2807SJeff Garzik 
3077c6fd2807SJeff Garzik 	/* prep for recovery */
30781eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE) {
30799b1e2658STejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
30809b1e2658STejun Heo 
3081f9df58cbSTejun Heo 		/* re-enable link? */
3082f9df58cbSTejun Heo 		if (ehc->i.action & ATA_EH_ENABLE_LINK) {
3083f9df58cbSTejun Heo 			ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
3084f9df58cbSTejun Heo 			spin_lock_irqsave(ap->lock, flags);
3085f9df58cbSTejun Heo 			link->flags &= ~ATA_LFLAG_DISABLED;
3086f9df58cbSTejun Heo 			spin_unlock_irqrestore(ap->lock, flags);
3087f9df58cbSTejun Heo 			ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
3088f9df58cbSTejun Heo 		}
3089f9df58cbSTejun Heo 
30901eca4365STejun Heo 		ata_for_each_dev(dev, link, ALL) {
3091fd995f70STejun Heo 			if (link->flags & ATA_LFLAG_NO_RETRY)
3092fd995f70STejun Heo 				ehc->tries[dev->devno] = 1;
3093fd995f70STejun Heo 			else
3094c6fd2807SJeff Garzik 				ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
3095c6fd2807SJeff Garzik 
309679a55b72STejun Heo 			/* collect port action mask recorded in dev actions */
30979b1e2658STejun Heo 			ehc->i.action |= ehc->i.dev_action[dev->devno] &
30989b1e2658STejun Heo 					 ~ATA_EH_PERDEV_MASK;
3099f58229f8STejun Heo 			ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
310079a55b72STejun Heo 
3101c6fd2807SJeff Garzik 			/* process hotplug request */
3102c6fd2807SJeff Garzik 			if (dev->flags & ATA_DFLAG_DETACH)
3103c6fd2807SJeff Garzik 				ata_eh_detach_dev(dev);
3104c6fd2807SJeff Garzik 
310502c05a27STejun Heo 			/* schedule probe if necessary */
310602c05a27STejun Heo 			if (!ata_dev_enabled(dev))
310702c05a27STejun Heo 				ata_eh_schedule_probe(dev);
3108c6fd2807SJeff Garzik 		}
31099b1e2658STejun Heo 	}
3110c6fd2807SJeff Garzik 
3111c6fd2807SJeff Garzik  retry:
3112c6fd2807SJeff Garzik 	rc = 0;
31139b1e2658STejun Heo 	nr_failed_devs = 0;
3114c6fd2807SJeff Garzik 
3115c6fd2807SJeff Garzik 	/* if UNLOADING, finish immediately */
3116c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_UNLOADING)
3117c6fd2807SJeff Garzik 		goto out;
3118c6fd2807SJeff Garzik 
31199b1e2658STejun Heo 	/* prep for EH */
31201eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE) {
31219b1e2658STejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
31229b1e2658STejun Heo 
3123c6fd2807SJeff Garzik 		/* skip EH if possible. */
31240260731fSTejun Heo 		if (ata_eh_skip_recovery(link))
3125c6fd2807SJeff Garzik 			ehc->i.action = 0;
3126c6fd2807SJeff Garzik 
31271eca4365STejun Heo 		ata_for_each_dev(dev, link, ALL)
3128f58229f8STejun Heo 			ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
31299b1e2658STejun Heo 	}
3130c6fd2807SJeff Garzik 
3131c6fd2807SJeff Garzik 	/* reset */
31321eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE) {
31339b1e2658STejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
31349b1e2658STejun Heo 
3135cf480626STejun Heo 		if (!(ehc->i.action & ATA_EH_RESET))
31369b1e2658STejun Heo 			continue;
31379b1e2658STejun Heo 
31389b1e2658STejun Heo 		rc = ata_eh_reset(link, ata_link_nr_vacant(link),
3139dc98c32cSTejun Heo 				  prereset, softreset, hardreset, postreset);
3140c6fd2807SJeff Garzik 		if (rc) {
31410260731fSTejun Heo 			ata_link_printk(link, KERN_ERR,
3142c6fd2807SJeff Garzik 					"reset failed, giving up\n");
3143c6fd2807SJeff Garzik 			goto out;
3144c6fd2807SJeff Garzik 		}
31459b1e2658STejun Heo 	}
3146c6fd2807SJeff Garzik 
314745fabbb7SElias Oltmanns 	do {
314845fabbb7SElias Oltmanns 		unsigned long now;
314945fabbb7SElias Oltmanns 
315045fabbb7SElias Oltmanns 		/*
315145fabbb7SElias Oltmanns 		 * clears ATA_EH_PARK in eh_info and resets
315245fabbb7SElias Oltmanns 		 * ap->park_req_pending
315345fabbb7SElias Oltmanns 		 */
315445fabbb7SElias Oltmanns 		ata_eh_pull_park_action(ap);
315545fabbb7SElias Oltmanns 
315645fabbb7SElias Oltmanns 		deadline = jiffies;
31571eca4365STejun Heo 		ata_for_each_link(link, ap, EDGE) {
31581eca4365STejun Heo 			ata_for_each_dev(dev, link, ALL) {
315945fabbb7SElias Oltmanns 				struct ata_eh_context *ehc = &link->eh_context;
316045fabbb7SElias Oltmanns 				unsigned long tmp;
316145fabbb7SElias Oltmanns 
316245fabbb7SElias Oltmanns 				if (dev->class != ATA_DEV_ATA)
316345fabbb7SElias Oltmanns 					continue;
316445fabbb7SElias Oltmanns 				if (!(ehc->i.dev_action[dev->devno] &
316545fabbb7SElias Oltmanns 				      ATA_EH_PARK))
316645fabbb7SElias Oltmanns 					continue;
316745fabbb7SElias Oltmanns 				tmp = dev->unpark_deadline;
316845fabbb7SElias Oltmanns 				if (time_before(deadline, tmp))
316945fabbb7SElias Oltmanns 					deadline = tmp;
317045fabbb7SElias Oltmanns 				else if (time_before_eq(tmp, jiffies))
317145fabbb7SElias Oltmanns 					continue;
317245fabbb7SElias Oltmanns 				if (ehc->unloaded_mask & (1 << dev->devno))
317345fabbb7SElias Oltmanns 					continue;
317445fabbb7SElias Oltmanns 
317545fabbb7SElias Oltmanns 				ata_eh_park_issue_cmd(dev, 1);
317645fabbb7SElias Oltmanns 			}
317745fabbb7SElias Oltmanns 		}
317845fabbb7SElias Oltmanns 
317945fabbb7SElias Oltmanns 		now = jiffies;
318045fabbb7SElias Oltmanns 		if (time_before_eq(deadline, now))
318145fabbb7SElias Oltmanns 			break;
318245fabbb7SElias Oltmanns 
318345fabbb7SElias Oltmanns 		deadline = wait_for_completion_timeout(&ap->park_req_pending,
318445fabbb7SElias Oltmanns 						       deadline - now);
318545fabbb7SElias Oltmanns 	} while (deadline);
31861eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE) {
31871eca4365STejun Heo 		ata_for_each_dev(dev, link, ALL) {
318845fabbb7SElias Oltmanns 			if (!(link->eh_context.unloaded_mask &
318945fabbb7SElias Oltmanns 			      (1 << dev->devno)))
319045fabbb7SElias Oltmanns 				continue;
319145fabbb7SElias Oltmanns 
319245fabbb7SElias Oltmanns 			ata_eh_park_issue_cmd(dev, 0);
319345fabbb7SElias Oltmanns 			ata_eh_done(link, dev, ATA_EH_PARK);
319445fabbb7SElias Oltmanns 		}
319545fabbb7SElias Oltmanns 	}
319645fabbb7SElias Oltmanns 
31979b1e2658STejun Heo 	/* the rest */
31981eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE) {
31999b1e2658STejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
32009b1e2658STejun Heo 
3201c6fd2807SJeff Garzik 		/* revalidate existing devices and attach new ones */
32020260731fSTejun Heo 		rc = ata_eh_revalidate_and_attach(link, &dev);
3203c6fd2807SJeff Garzik 		if (rc)
3204c6fd2807SJeff Garzik 			goto dev_fail;
3205c6fd2807SJeff Garzik 
3206633273a3STejun Heo 		/* if PMP got attached, return, pmp EH will take care of it */
3207633273a3STejun Heo 		if (link->device->class == ATA_DEV_PMP) {
3208633273a3STejun Heo 			ehc->i.action = 0;
3209633273a3STejun Heo 			return 0;
3210633273a3STejun Heo 		}
3211633273a3STejun Heo 
3212baa1e78aSTejun Heo 		/* configure transfer mode if necessary */
3213baa1e78aSTejun Heo 		if (ehc->i.flags & ATA_EHI_SETMODE) {
32140260731fSTejun Heo 			rc = ata_set_mode(link, &dev);
32154ae72a1eSTejun Heo 			if (rc)
3216c6fd2807SJeff Garzik 				goto dev_fail;
3217baa1e78aSTejun Heo 			ehc->i.flags &= ~ATA_EHI_SETMODE;
3218c6fd2807SJeff Garzik 		}
3219c6fd2807SJeff Garzik 
322011fc33daSTejun Heo 		/* If reset has been issued, clear UA to avoid
322111fc33daSTejun Heo 		 * disrupting the current users of the device.
322211fc33daSTejun Heo 		 */
322311fc33daSTejun Heo 		if (ehc->i.flags & ATA_EHI_DID_RESET) {
32241eca4365STejun Heo 			ata_for_each_dev(dev, link, ALL) {
322511fc33daSTejun Heo 				if (dev->class != ATA_DEV_ATAPI)
322611fc33daSTejun Heo 					continue;
322711fc33daSTejun Heo 				rc = atapi_eh_clear_ua(dev);
322811fc33daSTejun Heo 				if (rc)
322911fc33daSTejun Heo 					goto dev_fail;
323011fc33daSTejun Heo 			}
323111fc33daSTejun Heo 		}
323211fc33daSTejun Heo 
323311fc33daSTejun Heo 		/* configure link power saving */
32343ec25ebdSTejun Heo 		if (ehc->i.action & ATA_EH_LPM)
32351eca4365STejun Heo 			ata_for_each_dev(dev, link, ALL)
3236ca77329fSKristen Carlson Accardi 				ata_dev_enable_pm(dev, ap->pm_policy);
3237ca77329fSKristen Carlson Accardi 
32389b1e2658STejun Heo 		/* this link is okay now */
32399b1e2658STejun Heo 		ehc->i.flags = 0;
32409b1e2658STejun Heo 		continue;
3241c6fd2807SJeff Garzik 
3242c6fd2807SJeff Garzik dev_fail:
32439b1e2658STejun Heo 		nr_failed_devs++;
32440a2c0f56STejun Heo 		ata_eh_handle_dev_fail(dev, rc);
3245c6fd2807SJeff Garzik 
3246b06ce3e5STejun Heo 		if (ap->pflags & ATA_PFLAG_FROZEN) {
3247b06ce3e5STejun Heo 			/* PMP reset requires working host port.
3248b06ce3e5STejun Heo 			 * Can't retry if it's frozen.
3249b06ce3e5STejun Heo 			 */
3250071f44b1STejun Heo 			if (sata_pmp_attached(ap))
3251b06ce3e5STejun Heo 				goto out;
32529b1e2658STejun Heo 			break;
32539b1e2658STejun Heo 		}
3254b06ce3e5STejun Heo 	}
32559b1e2658STejun Heo 
32560a2c0f56STejun Heo 	if (nr_failed_devs)
3257c6fd2807SJeff Garzik 		goto retry;
3258c6fd2807SJeff Garzik 
3259c6fd2807SJeff Garzik  out:
32609b1e2658STejun Heo 	if (rc && r_failed_link)
32619b1e2658STejun Heo 		*r_failed_link = link;
3262c6fd2807SJeff Garzik 
3263c6fd2807SJeff Garzik 	DPRINTK("EXIT, rc=%d\n", rc);
3264c6fd2807SJeff Garzik 	return rc;
3265c6fd2807SJeff Garzik }
3266c6fd2807SJeff Garzik 
3267c6fd2807SJeff Garzik /**
3268c6fd2807SJeff Garzik  *	ata_eh_finish - finish up EH
3269c6fd2807SJeff Garzik  *	@ap: host port to finish EH for
3270c6fd2807SJeff Garzik  *
3271c6fd2807SJeff Garzik  *	Recovery is complete.  Clean up EH states and retry or finish
3272c6fd2807SJeff Garzik  *	failed qcs.
3273c6fd2807SJeff Garzik  *
3274c6fd2807SJeff Garzik  *	LOCKING:
3275c6fd2807SJeff Garzik  *	None.
3276c6fd2807SJeff Garzik  */
3277fb7fd614STejun Heo void ata_eh_finish(struct ata_port *ap)
3278c6fd2807SJeff Garzik {
3279c6fd2807SJeff Garzik 	int tag;
3280c6fd2807SJeff Garzik 
3281c6fd2807SJeff Garzik 	/* retry or finish qcs */
3282c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
3283c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
3284c6fd2807SJeff Garzik 
3285c6fd2807SJeff Garzik 		if (!(qc->flags & ATA_QCFLAG_FAILED))
3286c6fd2807SJeff Garzik 			continue;
3287c6fd2807SJeff Garzik 
3288c6fd2807SJeff Garzik 		if (qc->err_mask) {
3289c6fd2807SJeff Garzik 			/* FIXME: Once EH migration is complete,
3290c6fd2807SJeff Garzik 			 * generate sense data in this function,
3291c6fd2807SJeff Garzik 			 * considering both err_mask and tf.
3292c6fd2807SJeff Garzik 			 */
329303faab78STejun Heo 			if (qc->flags & ATA_QCFLAG_RETRY)
3294c6fd2807SJeff Garzik 				ata_eh_qc_retry(qc);
329503faab78STejun Heo 			else
329603faab78STejun Heo 				ata_eh_qc_complete(qc);
3297c6fd2807SJeff Garzik 		} else {
3298c6fd2807SJeff Garzik 			if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
3299c6fd2807SJeff Garzik 				ata_eh_qc_complete(qc);
3300c6fd2807SJeff Garzik 			} else {
3301c6fd2807SJeff Garzik 				/* feed zero TF to sense generation */
3302c6fd2807SJeff Garzik 				memset(&qc->result_tf, 0, sizeof(qc->result_tf));
3303c6fd2807SJeff Garzik 				ata_eh_qc_retry(qc);
3304c6fd2807SJeff Garzik 			}
3305c6fd2807SJeff Garzik 		}
3306c6fd2807SJeff Garzik 	}
3307da917d69STejun Heo 
3308da917d69STejun Heo 	/* make sure nr_active_links is zero after EH */
3309da917d69STejun Heo 	WARN_ON(ap->nr_active_links);
3310da917d69STejun Heo 	ap->nr_active_links = 0;
3311c6fd2807SJeff Garzik }
3312c6fd2807SJeff Garzik 
3313c6fd2807SJeff Garzik /**
3314c6fd2807SJeff Garzik  *	ata_do_eh - do standard error handling
3315c6fd2807SJeff Garzik  *	@ap: host port to handle error for
3316a1efdabaSTejun Heo  *
3317c6fd2807SJeff Garzik  *	@prereset: prereset method (can be NULL)
3318c6fd2807SJeff Garzik  *	@softreset: softreset method (can be NULL)
3319c6fd2807SJeff Garzik  *	@hardreset: hardreset method (can be NULL)
3320c6fd2807SJeff Garzik  *	@postreset: postreset method (can be NULL)
3321c6fd2807SJeff Garzik  *
3322c6fd2807SJeff Garzik  *	Perform standard error handling sequence.
3323c6fd2807SJeff Garzik  *
3324c6fd2807SJeff Garzik  *	LOCKING:
3325c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
3326c6fd2807SJeff Garzik  */
3327c6fd2807SJeff Garzik void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
3328c6fd2807SJeff Garzik 	       ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
3329c6fd2807SJeff Garzik 	       ata_postreset_fn_t postreset)
3330c6fd2807SJeff Garzik {
33319b1e2658STejun Heo 	struct ata_device *dev;
33329b1e2658STejun Heo 	int rc;
33339b1e2658STejun Heo 
33349b1e2658STejun Heo 	ata_eh_autopsy(ap);
33359b1e2658STejun Heo 	ata_eh_report(ap);
33369b1e2658STejun Heo 
33379b1e2658STejun Heo 	rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
33389b1e2658STejun Heo 			    NULL);
33399b1e2658STejun Heo 	if (rc) {
33401eca4365STejun Heo 		ata_for_each_dev(dev, &ap->link, ALL)
33419b1e2658STejun Heo 			ata_dev_disable(dev);
33429b1e2658STejun Heo 	}
33439b1e2658STejun Heo 
3344c6fd2807SJeff Garzik 	ata_eh_finish(ap);
3345c6fd2807SJeff Garzik }
3346c6fd2807SJeff Garzik 
3347a1efdabaSTejun Heo /**
3348a1efdabaSTejun Heo  *	ata_std_error_handler - standard error handler
3349a1efdabaSTejun Heo  *	@ap: host port to handle error for
3350a1efdabaSTejun Heo  *
3351a1efdabaSTejun Heo  *	Standard error handler
3352a1efdabaSTejun Heo  *
3353a1efdabaSTejun Heo  *	LOCKING:
3354a1efdabaSTejun Heo  *	Kernel thread context (may sleep).
3355a1efdabaSTejun Heo  */
3356a1efdabaSTejun Heo void ata_std_error_handler(struct ata_port *ap)
3357a1efdabaSTejun Heo {
3358a1efdabaSTejun Heo 	struct ata_port_operations *ops = ap->ops;
3359a1efdabaSTejun Heo 	ata_reset_fn_t hardreset = ops->hardreset;
3360a1efdabaSTejun Heo 
336157c9efdfSTejun Heo 	/* ignore built-in hardreset if SCR access is not available */
336257c9efdfSTejun Heo 	if (ata_is_builtin_hardreset(hardreset) && !sata_scr_valid(&ap->link))
3363a1efdabaSTejun Heo 		hardreset = NULL;
3364a1efdabaSTejun Heo 
3365a1efdabaSTejun Heo 	ata_do_eh(ap, ops->prereset, ops->softreset, hardreset, ops->postreset);
3366a1efdabaSTejun Heo }
3367a1efdabaSTejun Heo 
33686ffa01d8STejun Heo #ifdef CONFIG_PM
3369c6fd2807SJeff Garzik /**
3370c6fd2807SJeff Garzik  *	ata_eh_handle_port_suspend - perform port suspend operation
3371c6fd2807SJeff Garzik  *	@ap: port to suspend
3372c6fd2807SJeff Garzik  *
3373c6fd2807SJeff Garzik  *	Suspend @ap.
3374c6fd2807SJeff Garzik  *
3375c6fd2807SJeff Garzik  *	LOCKING:
3376c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
3377c6fd2807SJeff Garzik  */
3378c6fd2807SJeff Garzik static void ata_eh_handle_port_suspend(struct ata_port *ap)
3379c6fd2807SJeff Garzik {
3380c6fd2807SJeff Garzik 	unsigned long flags;
3381c6fd2807SJeff Garzik 	int rc = 0;
3382c6fd2807SJeff Garzik 
3383c6fd2807SJeff Garzik 	/* are we suspending? */
3384c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
3385c6fd2807SJeff Garzik 	if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
3386c6fd2807SJeff Garzik 	    ap->pm_mesg.event == PM_EVENT_ON) {
3387c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
3388c6fd2807SJeff Garzik 		return;
3389c6fd2807SJeff Garzik 	}
3390c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
3391c6fd2807SJeff Garzik 
3392c6fd2807SJeff Garzik 	WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
3393c6fd2807SJeff Garzik 
339464578a3dSTejun Heo 	/* tell ACPI we're suspending */
339564578a3dSTejun Heo 	rc = ata_acpi_on_suspend(ap);
339664578a3dSTejun Heo 	if (rc)
339764578a3dSTejun Heo 		goto out;
339864578a3dSTejun Heo 
3399c6fd2807SJeff Garzik 	/* suspend */
3400c6fd2807SJeff Garzik 	ata_eh_freeze_port(ap);
3401c6fd2807SJeff Garzik 
3402c6fd2807SJeff Garzik 	if (ap->ops->port_suspend)
3403c6fd2807SJeff Garzik 		rc = ap->ops->port_suspend(ap, ap->pm_mesg);
3404c6fd2807SJeff Garzik 
3405bd3adca5SShaohua Li 	ata_acpi_set_state(ap, PMSG_SUSPEND);
340664578a3dSTejun Heo  out:
3407c6fd2807SJeff Garzik 	/* report result */
3408c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
3409c6fd2807SJeff Garzik 
3410c6fd2807SJeff Garzik 	ap->pflags &= ~ATA_PFLAG_PM_PENDING;
3411c6fd2807SJeff Garzik 	if (rc == 0)
3412c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_SUSPENDED;
341364578a3dSTejun Heo 	else if (ap->pflags & ATA_PFLAG_FROZEN)
3414c6fd2807SJeff Garzik 		ata_port_schedule_eh(ap);
3415c6fd2807SJeff Garzik 
3416c6fd2807SJeff Garzik 	if (ap->pm_result) {
3417c6fd2807SJeff Garzik 		*ap->pm_result = rc;
3418c6fd2807SJeff Garzik 		ap->pm_result = NULL;
3419c6fd2807SJeff Garzik 	}
3420c6fd2807SJeff Garzik 
3421c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
3422c6fd2807SJeff Garzik 
3423c6fd2807SJeff Garzik 	return;
3424c6fd2807SJeff Garzik }
3425c6fd2807SJeff Garzik 
3426c6fd2807SJeff Garzik /**
3427c6fd2807SJeff Garzik  *	ata_eh_handle_port_resume - perform port resume operation
3428c6fd2807SJeff Garzik  *	@ap: port to resume
3429c6fd2807SJeff Garzik  *
3430c6fd2807SJeff Garzik  *	Resume @ap.
3431c6fd2807SJeff Garzik  *
3432c6fd2807SJeff Garzik  *	LOCKING:
3433c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
3434c6fd2807SJeff Garzik  */
3435c6fd2807SJeff Garzik static void ata_eh_handle_port_resume(struct ata_port *ap)
3436c6fd2807SJeff Garzik {
3437c6fd2807SJeff Garzik 	unsigned long flags;
34389666f400STejun Heo 	int rc = 0;
3439c6fd2807SJeff Garzik 
3440c6fd2807SJeff Garzik 	/* are we resuming? */
3441c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
3442c6fd2807SJeff Garzik 	if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
3443c6fd2807SJeff Garzik 	    ap->pm_mesg.event != PM_EVENT_ON) {
3444c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
3445c6fd2807SJeff Garzik 		return;
3446c6fd2807SJeff Garzik 	}
3447c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
3448c6fd2807SJeff Garzik 
34499666f400STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
3450c6fd2807SJeff Garzik 
3451bd3adca5SShaohua Li 	ata_acpi_set_state(ap, PMSG_ON);
3452bd3adca5SShaohua Li 
3453c6fd2807SJeff Garzik 	if (ap->ops->port_resume)
3454c6fd2807SJeff Garzik 		rc = ap->ops->port_resume(ap);
3455c6fd2807SJeff Garzik 
34566746544cSTejun Heo 	/* tell ACPI that we're resuming */
34576746544cSTejun Heo 	ata_acpi_on_resume(ap);
34586746544cSTejun Heo 
34599666f400STejun Heo 	/* report result */
3460c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
3461c6fd2807SJeff Garzik 	ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
3462c6fd2807SJeff Garzik 	if (ap->pm_result) {
3463c6fd2807SJeff Garzik 		*ap->pm_result = rc;
3464c6fd2807SJeff Garzik 		ap->pm_result = NULL;
3465c6fd2807SJeff Garzik 	}
3466c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
3467c6fd2807SJeff Garzik }
34686ffa01d8STejun Heo #endif /* CONFIG_PM */
3469