xref: /openbmc/linux/drivers/ata/libata-eh.c (revision a52fbcfc7b38b50b5b651bab80041a93ba7b29b9)
1c82ee6d3SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2c6fd2807SJeff Garzik /*
3c6fd2807SJeff Garzik  *  libata-eh.c - libata error handling
4c6fd2807SJeff Garzik  *
5c6fd2807SJeff Garzik  *  Copyright 2006 Tejun Heo <htejun@gmail.com>
6c6fd2807SJeff Garzik  *
7c6fd2807SJeff Garzik  *  libata documentation is available via 'make {ps|pdf}docs',
89bb9a39cSMauro Carvalho Chehab  *  as Documentation/driver-api/libata.rst
9c6fd2807SJeff Garzik  *
10c6fd2807SJeff Garzik  *  Hardware documentation available from http://www.t13.org/ and
11c6fd2807SJeff Garzik  *  http://www.sata-io.org/
12c6fd2807SJeff Garzik  */
13c6fd2807SJeff Garzik 
14c6fd2807SJeff Garzik #include <linux/kernel.h>
15242f9dcbSJens Axboe #include <linux/blkdev.h>
1638789fdaSPaul Gortmaker #include <linux/export.h>
172855568bSJeff Garzik #include <linux/pci.h>
18c6fd2807SJeff Garzik #include <scsi/scsi.h>
19c6fd2807SJeff Garzik #include <scsi/scsi_host.h>
20c6fd2807SJeff Garzik #include <scsi/scsi_eh.h>
21c6fd2807SJeff Garzik #include <scsi/scsi_device.h>
22c6fd2807SJeff Garzik #include <scsi/scsi_cmnd.h>
236521148cSRobert Hancock #include <scsi/scsi_dbg.h>
24c6fd2807SJeff Garzik #include "../scsi/scsi_transport_api.h"
25c6fd2807SJeff Garzik 
26c6fd2807SJeff Garzik #include <linux/libata.h>
27c6fd2807SJeff Garzik 
28255c03d1SHannes Reinecke #include <trace/events/libata.h>
29c6fd2807SJeff Garzik #include "libata.h"
30c6fd2807SJeff Garzik 
317d47e8d4STejun Heo enum {
323884f7b0STejun Heo 	/* speed down verdicts */
337d47e8d4STejun Heo 	ATA_EH_SPDN_NCQ_OFF		= (1 << 0),
347d47e8d4STejun Heo 	ATA_EH_SPDN_SPEED_DOWN		= (1 << 1),
357d47e8d4STejun Heo 	ATA_EH_SPDN_FALLBACK_TO_PIO	= (1 << 2),
3676326ac1STejun Heo 	ATA_EH_SPDN_KEEP_ERRORS		= (1 << 3),
373884f7b0STejun Heo 
383884f7b0STejun Heo 	/* error flags */
393884f7b0STejun Heo 	ATA_EFLAG_IS_IO			= (1 << 0),
4076326ac1STejun Heo 	ATA_EFLAG_DUBIOUS_XFER		= (1 << 1),
41d9027470SGwendal Grignou 	ATA_EFLAG_OLD_ER                = (1 << 31),
423884f7b0STejun Heo 
433884f7b0STejun Heo 	/* error categories */
443884f7b0STejun Heo 	ATA_ECAT_NONE			= 0,
453884f7b0STejun Heo 	ATA_ECAT_ATA_BUS		= 1,
463884f7b0STejun Heo 	ATA_ECAT_TOUT_HSM		= 2,
473884f7b0STejun Heo 	ATA_ECAT_UNK_DEV		= 3,
4875f9cafcSTejun Heo 	ATA_ECAT_DUBIOUS_NONE		= 4,
4975f9cafcSTejun Heo 	ATA_ECAT_DUBIOUS_ATA_BUS	= 5,
5075f9cafcSTejun Heo 	ATA_ECAT_DUBIOUS_TOUT_HSM	= 6,
5175f9cafcSTejun Heo 	ATA_ECAT_DUBIOUS_UNK_DEV	= 7,
5275f9cafcSTejun Heo 	ATA_ECAT_NR			= 8,
537d47e8d4STejun Heo 
5487fbc5a0STejun Heo 	ATA_EH_CMD_DFL_TIMEOUT		=  5000,
5587fbc5a0STejun Heo 
560a2c0f56STejun Heo 	/* always put at least this amount of time between resets */
570a2c0f56STejun Heo 	ATA_EH_RESET_COOL_DOWN		=  5000,
580a2c0f56STejun Heo 
59341c2c95STejun Heo 	/* Waiting in ->prereset can never be reliable.  It's
60341c2c95STejun Heo 	 * sometimes nice to wait there but it can't be depended upon;
61341c2c95STejun Heo 	 * otherwise, we wouldn't be resetting.  Just give it enough
62341c2c95STejun Heo 	 * time for most drives to spin up.
6331daabdaSTejun Heo 	 */
64341c2c95STejun Heo 	ATA_EH_PRERESET_TIMEOUT		= 10000,
65341c2c95STejun Heo 	ATA_EH_FASTDRAIN_INTERVAL	=  3000,
6611fc33daSTejun Heo 
6711fc33daSTejun Heo 	ATA_EH_UA_TRIES			= 5,
68c2c7a89cSTejun Heo 
69c2c7a89cSTejun Heo 	/* probe speed down parameters, see ata_eh_schedule_probe() */
70c2c7a89cSTejun Heo 	ATA_EH_PROBE_TRIAL_INTERVAL	= 60000,	/* 1 min */
71c2c7a89cSTejun Heo 	ATA_EH_PROBE_TRIALS		= 2,
7231daabdaSTejun Heo };
7331daabdaSTejun Heo 
7431daabdaSTejun Heo /* The following table determines how we sequence resets.  Each entry
7531daabdaSTejun Heo  * represents timeout for that try.  The first try can be soft or
7631daabdaSTejun Heo  * hardreset.  All others are hardreset if available.  In most cases
7731daabdaSTejun Heo  * the first reset w/ 10sec timeout should succeed.  Following entries
7835bf8821SDan Williams  * are mostly for error handling, hotplug and those outlier devices that
7935bf8821SDan Williams  * take an exceptionally long time to recover from reset.
8031daabdaSTejun Heo  */
8131daabdaSTejun Heo static const unsigned long ata_eh_reset_timeouts[] = {
82341c2c95STejun Heo 	10000,	/* most drives spin up by 10sec */
83341c2c95STejun Heo 	10000,	/* > 99% working drives spin up before 20sec */
8435bf8821SDan Williams 	35000,	/* give > 30 secs of idleness for outlier devices */
85341c2c95STejun Heo 	 5000,	/* and sweet one last chance */
86d8af0eb6STejun Heo 	ULONG_MAX, /* > 1 min has elapsed, give up */
8731daabdaSTejun Heo };
8831daabdaSTejun Heo 
8987fbc5a0STejun Heo static const unsigned long ata_eh_identify_timeouts[] = {
9087fbc5a0STejun Heo 	 5000,	/* covers > 99% of successes and not too boring on failures */
9187fbc5a0STejun Heo 	10000,  /* combined time till here is enough even for media access */
9287fbc5a0STejun Heo 	30000,	/* for true idiots */
9387fbc5a0STejun Heo 	ULONG_MAX,
9487fbc5a0STejun Heo };
9587fbc5a0STejun Heo 
966013efd8STejun Heo static const unsigned long ata_eh_flush_timeouts[] = {
976013efd8STejun Heo 	15000,	/* be generous with flush */
986013efd8STejun Heo 	15000,  /* ditto */
996013efd8STejun Heo 	30000,	/* and even more generous */
1006013efd8STejun Heo 	ULONG_MAX,
1016013efd8STejun Heo };
1026013efd8STejun Heo 
10387fbc5a0STejun Heo static const unsigned long ata_eh_other_timeouts[] = {
10487fbc5a0STejun Heo 	 5000,	/* same rationale as identify timeout */
10587fbc5a0STejun Heo 	10000,	/* ditto */
10687fbc5a0STejun Heo 	/* but no merciful 30sec for other commands, it just isn't worth it */
10787fbc5a0STejun Heo 	ULONG_MAX,
10887fbc5a0STejun Heo };
10987fbc5a0STejun Heo 
11087fbc5a0STejun Heo struct ata_eh_cmd_timeout_ent {
11187fbc5a0STejun Heo 	const u8		*commands;
11287fbc5a0STejun Heo 	const unsigned long	*timeouts;
11387fbc5a0STejun Heo };
11487fbc5a0STejun Heo 
11587fbc5a0STejun Heo /* The following table determines timeouts to use for EH internal
11687fbc5a0STejun Heo  * commands.  Each table entry is a command class and matches the
11787fbc5a0STejun Heo  * commands the entry applies to and the timeout table to use.
11887fbc5a0STejun Heo  *
11987fbc5a0STejun Heo  * On the retry after a command timed out, the next timeout value from
12087fbc5a0STejun Heo  * the table is used.  If the table doesn't contain further entries,
12187fbc5a0STejun Heo  * the last value is used.
12287fbc5a0STejun Heo  *
12387fbc5a0STejun Heo  * ehc->cmd_timeout_idx keeps track of which timeout to use per
12487fbc5a0STejun Heo  * command class, so if SET_FEATURES times out on the first try, the
12587fbc5a0STejun Heo  * next try will use the second timeout value only for that class.
12687fbc5a0STejun Heo  */
12787fbc5a0STejun Heo #define CMDS(cmds...)	(const u8 []){ cmds, 0 }
12887fbc5a0STejun Heo static const struct ata_eh_cmd_timeout_ent
12987fbc5a0STejun Heo ata_eh_cmd_timeout_table[ATA_EH_CMD_TIMEOUT_TABLE_SIZE] = {
13087fbc5a0STejun Heo 	{ .commands = CMDS(ATA_CMD_ID_ATA, ATA_CMD_ID_ATAPI),
13187fbc5a0STejun Heo 	  .timeouts = ata_eh_identify_timeouts, },
13287fbc5a0STejun Heo 	{ .commands = CMDS(ATA_CMD_READ_NATIVE_MAX, ATA_CMD_READ_NATIVE_MAX_EXT),
13387fbc5a0STejun Heo 	  .timeouts = ata_eh_other_timeouts, },
13487fbc5a0STejun Heo 	{ .commands = CMDS(ATA_CMD_SET_MAX, ATA_CMD_SET_MAX_EXT),
13587fbc5a0STejun Heo 	  .timeouts = ata_eh_other_timeouts, },
13687fbc5a0STejun Heo 	{ .commands = CMDS(ATA_CMD_SET_FEATURES),
13787fbc5a0STejun Heo 	  .timeouts = ata_eh_other_timeouts, },
13887fbc5a0STejun Heo 	{ .commands = CMDS(ATA_CMD_INIT_DEV_PARAMS),
13987fbc5a0STejun Heo 	  .timeouts = ata_eh_other_timeouts, },
1406013efd8STejun Heo 	{ .commands = CMDS(ATA_CMD_FLUSH, ATA_CMD_FLUSH_EXT),
1416013efd8STejun Heo 	  .timeouts = ata_eh_flush_timeouts },
14287fbc5a0STejun Heo };
14387fbc5a0STejun Heo #undef CMDS
14487fbc5a0STejun Heo 
145c6fd2807SJeff Garzik static void __ata_port_freeze(struct ata_port *ap);
1466ffa01d8STejun Heo #ifdef CONFIG_PM
147c6fd2807SJeff Garzik static void ata_eh_handle_port_suspend(struct ata_port *ap);
148c6fd2807SJeff Garzik static void ata_eh_handle_port_resume(struct ata_port *ap);
1496ffa01d8STejun Heo #else /* CONFIG_PM */
1506ffa01d8STejun Heo static void ata_eh_handle_port_suspend(struct ata_port *ap)
1516ffa01d8STejun Heo { }
1526ffa01d8STejun Heo 
1536ffa01d8STejun Heo static void ata_eh_handle_port_resume(struct ata_port *ap)
1546ffa01d8STejun Heo { }
1556ffa01d8STejun Heo #endif /* CONFIG_PM */
156c6fd2807SJeff Garzik 
1570d74d872SMathieu Malaterre static __printf(2, 0) void __ata_ehi_pushv_desc(struct ata_eh_info *ehi,
1580d74d872SMathieu Malaterre 				 const char *fmt, va_list args)
159b64bbc39STejun Heo {
160b64bbc39STejun Heo 	ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
161b64bbc39STejun Heo 				     ATA_EH_DESC_LEN - ehi->desc_len,
162b64bbc39STejun Heo 				     fmt, args);
163b64bbc39STejun Heo }
164b64bbc39STejun Heo 
165b64bbc39STejun Heo /**
166b64bbc39STejun Heo  *	__ata_ehi_push_desc - push error description without adding separator
167b64bbc39STejun Heo  *	@ehi: target EHI
168b64bbc39STejun Heo  *	@fmt: printf format string
169b64bbc39STejun Heo  *
170b64bbc39STejun Heo  *	Format string according to @fmt and append it to @ehi->desc.
171b64bbc39STejun Heo  *
172b64bbc39STejun Heo  *	LOCKING:
173b64bbc39STejun Heo  *	spin_lock_irqsave(host lock)
174b64bbc39STejun Heo  */
175b64bbc39STejun Heo void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
176b64bbc39STejun Heo {
177b64bbc39STejun Heo 	va_list args;
178b64bbc39STejun Heo 
179b64bbc39STejun Heo 	va_start(args, fmt);
180b64bbc39STejun Heo 	__ata_ehi_pushv_desc(ehi, fmt, args);
181b64bbc39STejun Heo 	va_end(args);
182b64bbc39STejun Heo }
183*a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
184b64bbc39STejun Heo 
185b64bbc39STejun Heo /**
186b64bbc39STejun Heo  *	ata_ehi_push_desc - push error description with separator
187b64bbc39STejun Heo  *	@ehi: target EHI
188b64bbc39STejun Heo  *	@fmt: printf format string
189b64bbc39STejun Heo  *
190b64bbc39STejun Heo  *	Format string according to @fmt and append it to @ehi->desc.
191b64bbc39STejun Heo  *	If @ehi->desc is not empty, ", " is added in-between.
192b64bbc39STejun Heo  *
193b64bbc39STejun Heo  *	LOCKING:
194b64bbc39STejun Heo  *	spin_lock_irqsave(host lock)
195b64bbc39STejun Heo  */
196b64bbc39STejun Heo void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
197b64bbc39STejun Heo {
198b64bbc39STejun Heo 	va_list args;
199b64bbc39STejun Heo 
200b64bbc39STejun Heo 	if (ehi->desc_len)
201b64bbc39STejun Heo 		__ata_ehi_push_desc(ehi, ", ");
202b64bbc39STejun Heo 
203b64bbc39STejun Heo 	va_start(args, fmt);
204b64bbc39STejun Heo 	__ata_ehi_pushv_desc(ehi, fmt, args);
205b64bbc39STejun Heo 	va_end(args);
206b64bbc39STejun Heo }
207*a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
208b64bbc39STejun Heo 
209b64bbc39STejun Heo /**
210b64bbc39STejun Heo  *	ata_ehi_clear_desc - clean error description
211b64bbc39STejun Heo  *	@ehi: target EHI
212b64bbc39STejun Heo  *
213b64bbc39STejun Heo  *	Clear @ehi->desc.
214b64bbc39STejun Heo  *
215b64bbc39STejun Heo  *	LOCKING:
216b64bbc39STejun Heo  *	spin_lock_irqsave(host lock)
217b64bbc39STejun Heo  */
218b64bbc39STejun Heo void ata_ehi_clear_desc(struct ata_eh_info *ehi)
219b64bbc39STejun Heo {
220b64bbc39STejun Heo 	ehi->desc[0] = '\0';
221b64bbc39STejun Heo 	ehi->desc_len = 0;
222b64bbc39STejun Heo }
223*a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
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 }
251*a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_port_desc);
252cbcdd875STejun Heo 
253cbcdd875STejun Heo #ifdef CONFIG_PCI
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 }
290*a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
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 
385d9027470SGwendal Grignou int ata_ering_map(struct ata_ering *ering,
386c6fd2807SJeff Garzik 		  int (*map_fn)(struct ata_ering_entry *, void *),
387c6fd2807SJeff Garzik 		  void *arg)
388c6fd2807SJeff Garzik {
389c6fd2807SJeff Garzik 	int idx, rc = 0;
390c6fd2807SJeff Garzik 	struct ata_ering_entry *ent;
391c6fd2807SJeff Garzik 
392c6fd2807SJeff Garzik 	idx = ering->cursor;
393c6fd2807SJeff Garzik 	do {
394c6fd2807SJeff Garzik 		ent = &ering->ring[idx];
395c6fd2807SJeff Garzik 		if (!ent->err_mask)
396c6fd2807SJeff Garzik 			break;
397c6fd2807SJeff Garzik 		rc = map_fn(ent, arg);
398c6fd2807SJeff Garzik 		if (rc)
399c6fd2807SJeff Garzik 			break;
400c6fd2807SJeff Garzik 		idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
401c6fd2807SJeff Garzik 	} while (idx != ering->cursor);
402c6fd2807SJeff Garzik 
403c6fd2807SJeff Garzik 	return rc;
404c6fd2807SJeff Garzik }
405c6fd2807SJeff Garzik 
40660428407SH Hartley Sweeten static int ata_ering_clear_cb(struct ata_ering_entry *ent, void *void_arg)
407d9027470SGwendal Grignou {
408d9027470SGwendal Grignou 	ent->eflags |= ATA_EFLAG_OLD_ER;
409d9027470SGwendal Grignou 	return 0;
410d9027470SGwendal Grignou }
411d9027470SGwendal Grignou 
412d9027470SGwendal Grignou static void ata_ering_clear(struct ata_ering *ering)
413d9027470SGwendal Grignou {
414d9027470SGwendal Grignou 	ata_ering_map(ering, ata_ering_clear_cb, NULL);
415d9027470SGwendal Grignou }
416d9027470SGwendal Grignou 
417c6fd2807SJeff Garzik static unsigned int ata_eh_dev_action(struct ata_device *dev)
418c6fd2807SJeff Garzik {
4199af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
420c6fd2807SJeff Garzik 
421c6fd2807SJeff Garzik 	return ehc->i.action | ehc->i.dev_action[dev->devno];
422c6fd2807SJeff Garzik }
423c6fd2807SJeff Garzik 
424f58229f8STejun Heo static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
425c6fd2807SJeff Garzik 				struct ata_eh_info *ehi, unsigned int action)
426c6fd2807SJeff Garzik {
427f58229f8STejun Heo 	struct ata_device *tdev;
428c6fd2807SJeff Garzik 
429c6fd2807SJeff Garzik 	if (!dev) {
430c6fd2807SJeff Garzik 		ehi->action &= ~action;
4311eca4365STejun Heo 		ata_for_each_dev(tdev, link, ALL)
432f58229f8STejun Heo 			ehi->dev_action[tdev->devno] &= ~action;
433c6fd2807SJeff Garzik 	} else {
434c6fd2807SJeff Garzik 		/* doesn't make sense for port-wide EH actions */
435c6fd2807SJeff Garzik 		WARN_ON(!(action & ATA_EH_PERDEV_MASK));
436c6fd2807SJeff Garzik 
437c6fd2807SJeff Garzik 		/* break ehi->action into ehi->dev_action */
438c6fd2807SJeff Garzik 		if (ehi->action & action) {
4391eca4365STejun Heo 			ata_for_each_dev(tdev, link, ALL)
440f58229f8STejun Heo 				ehi->dev_action[tdev->devno] |=
441f58229f8STejun Heo 					ehi->action & action;
442c6fd2807SJeff Garzik 			ehi->action &= ~action;
443c6fd2807SJeff Garzik 		}
444c6fd2807SJeff Garzik 
445c6fd2807SJeff Garzik 		/* turn off the specified per-dev action */
446c6fd2807SJeff Garzik 		ehi->dev_action[dev->devno] &= ~action;
447c6fd2807SJeff Garzik 	}
448c6fd2807SJeff Garzik }
449c6fd2807SJeff Garzik 
450c6fd2807SJeff Garzik /**
451c0c362b6STejun Heo  *	ata_eh_acquire - acquire EH ownership
452c0c362b6STejun Heo  *	@ap: ATA port to acquire EH ownership for
453c0c362b6STejun Heo  *
454c0c362b6STejun Heo  *	Acquire EH ownership for @ap.  This is the basic exclusion
455c0c362b6STejun Heo  *	mechanism for ports sharing a host.  Only one port hanging off
456c0c362b6STejun Heo  *	the same host can claim the ownership of EH.
457c0c362b6STejun Heo  *
458c0c362b6STejun Heo  *	LOCKING:
459c0c362b6STejun Heo  *	EH context.
460c0c362b6STejun Heo  */
461c0c362b6STejun Heo void ata_eh_acquire(struct ata_port *ap)
462c0c362b6STejun Heo {
463c0c362b6STejun Heo 	mutex_lock(&ap->host->eh_mutex);
464c0c362b6STejun Heo 	WARN_ON_ONCE(ap->host->eh_owner);
465c0c362b6STejun Heo 	ap->host->eh_owner = current;
466c0c362b6STejun Heo }
467c0c362b6STejun Heo 
468c0c362b6STejun Heo /**
469c0c362b6STejun Heo  *	ata_eh_release - release EH ownership
470c0c362b6STejun Heo  *	@ap: ATA port to release EH ownership for
471c0c362b6STejun Heo  *
472c0c362b6STejun Heo  *	Release EH ownership for @ap if the caller.  The caller must
473c0c362b6STejun Heo  *	have acquired EH ownership using ata_eh_acquire() previously.
474c0c362b6STejun Heo  *
475c0c362b6STejun Heo  *	LOCKING:
476c0c362b6STejun Heo  *	EH context.
477c0c362b6STejun Heo  */
478c0c362b6STejun Heo void ata_eh_release(struct ata_port *ap)
479c0c362b6STejun Heo {
480c0c362b6STejun Heo 	WARN_ON_ONCE(ap->host->eh_owner != current);
481c0c362b6STejun Heo 	ap->host->eh_owner = NULL;
482c0c362b6STejun Heo 	mutex_unlock(&ap->host->eh_mutex);
483c0c362b6STejun Heo }
484c0c362b6STejun Heo 
485ece180d1STejun Heo static void ata_eh_unload(struct ata_port *ap)
486ece180d1STejun Heo {
487ece180d1STejun Heo 	struct ata_link *link;
488ece180d1STejun Heo 	struct ata_device *dev;
489ece180d1STejun Heo 	unsigned long flags;
490ece180d1STejun Heo 
491ece180d1STejun Heo 	/* Restore SControl IPM and SPD for the next driver and
492ece180d1STejun Heo 	 * disable attached devices.
493ece180d1STejun Heo 	 */
494ece180d1STejun Heo 	ata_for_each_link(link, ap, PMP_FIRST) {
495ece180d1STejun Heo 		sata_scr_write(link, SCR_CONTROL, link->saved_scontrol & 0xff0);
496ece180d1STejun Heo 		ata_for_each_dev(dev, link, ALL)
497ece180d1STejun Heo 			ata_dev_disable(dev);
498ece180d1STejun Heo 	}
499ece180d1STejun Heo 
500ece180d1STejun Heo 	/* freeze and set UNLOADED */
501ece180d1STejun Heo 	spin_lock_irqsave(ap->lock, flags);
502ece180d1STejun Heo 
503ece180d1STejun Heo 	ata_port_freeze(ap);			/* won't be thawed */
504ece180d1STejun Heo 	ap->pflags &= ~ATA_PFLAG_EH_PENDING;	/* clear pending from freeze */
505ece180d1STejun Heo 	ap->pflags |= ATA_PFLAG_UNLOADED;
506ece180d1STejun Heo 
507ece180d1STejun Heo 	spin_unlock_irqrestore(ap->lock, flags);
508ece180d1STejun Heo }
509ece180d1STejun Heo 
510c6fd2807SJeff Garzik /**
511c6fd2807SJeff Garzik  *	ata_scsi_error - SCSI layer error handler callback
512c6fd2807SJeff Garzik  *	@host: SCSI host on which error occurred
513c6fd2807SJeff Garzik  *
514c6fd2807SJeff Garzik  *	Handles SCSI-layer-thrown error events.
515c6fd2807SJeff Garzik  *
516c6fd2807SJeff Garzik  *	LOCKING:
517c6fd2807SJeff Garzik  *	Inherited from SCSI layer (none, can sleep)
518c6fd2807SJeff Garzik  *
519c6fd2807SJeff Garzik  *	RETURNS:
520c6fd2807SJeff Garzik  *	Zero.
521c6fd2807SJeff Garzik  */
522c6fd2807SJeff Garzik void ata_scsi_error(struct Scsi_Host *host)
523c6fd2807SJeff Garzik {
524c6fd2807SJeff Garzik 	struct ata_port *ap = ata_shost_to_port(host);
525c6fd2807SJeff Garzik 	unsigned long flags;
526c34aeebcSJames Bottomley 	LIST_HEAD(eh_work_q);
527c6fd2807SJeff Garzik 
528c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
529c6fd2807SJeff Garzik 
530c34aeebcSJames Bottomley 	spin_lock_irqsave(host->host_lock, flags);
531c34aeebcSJames Bottomley 	list_splice_init(&host->eh_cmd_q, &eh_work_q);
532c34aeebcSJames Bottomley 	spin_unlock_irqrestore(host->host_lock, flags);
533c34aeebcSJames Bottomley 
5340e0b494cSJames Bottomley 	ata_scsi_cmd_error_handler(host, ap, &eh_work_q);
5350e0b494cSJames Bottomley 
5360e0b494cSJames Bottomley 	/* If we timed raced normal completion and there is nothing to
5370e0b494cSJames Bottomley 	   recover nr_timedout == 0 why exactly are we doing error recovery ? */
5380e0b494cSJames Bottomley 	ata_scsi_port_error_handler(host, ap);
5390e0b494cSJames Bottomley 
5400e0b494cSJames Bottomley 	/* finish or retry handled scmd's and clean up */
54172d8c36eSWei Fang 	WARN_ON(!list_empty(&eh_work_q));
5420e0b494cSJames Bottomley 
5430e0b494cSJames Bottomley 	DPRINTK("EXIT\n");
5440e0b494cSJames Bottomley }
5450e0b494cSJames Bottomley 
5460e0b494cSJames Bottomley /**
5470e0b494cSJames Bottomley  * ata_scsi_cmd_error_handler - error callback for a list of commands
5480e0b494cSJames Bottomley  * @host:	scsi host containing the port
5490e0b494cSJames Bottomley  * @ap:		ATA port within the host
5500e0b494cSJames Bottomley  * @eh_work_q:	list of commands to process
5510e0b494cSJames Bottomley  *
5520e0b494cSJames Bottomley  * process the given list of commands and return those finished to the
5530e0b494cSJames Bottomley  * ap->eh_done_q.  This function is the first part of the libata error
5540e0b494cSJames Bottomley  * handler which processes a given list of failed commands.
5550e0b494cSJames Bottomley  */
5560e0b494cSJames Bottomley void ata_scsi_cmd_error_handler(struct Scsi_Host *host, struct ata_port *ap,
5570e0b494cSJames Bottomley 				struct list_head *eh_work_q)
5580e0b494cSJames Bottomley {
5590e0b494cSJames Bottomley 	int i;
5600e0b494cSJames Bottomley 	unsigned long flags;
5610e0b494cSJames Bottomley 
562c429137aSTejun Heo 	/* make sure sff pio task is not running */
563c429137aSTejun Heo 	ata_sff_flush_pio_task(ap);
564c6fd2807SJeff Garzik 
565cca3974eSJeff Garzik 	/* synchronize with host lock and sort out timeouts */
566c6fd2807SJeff Garzik 
567c6fd2807SJeff Garzik 	/* For new EH, all qcs are finished in one of three ways -
568c6fd2807SJeff Garzik 	 * normal completion, error completion, and SCSI timeout.
569c96f1732SAlan Cox 	 * Both completions can race against SCSI timeout.  When normal
570c6fd2807SJeff Garzik 	 * completion wins, the qc never reaches EH.  When error
571c6fd2807SJeff Garzik 	 * completion wins, the qc has ATA_QCFLAG_FAILED set.
572c6fd2807SJeff Garzik 	 *
573c6fd2807SJeff Garzik 	 * When SCSI timeout wins, things are a bit more complex.
574c6fd2807SJeff Garzik 	 * Normal or error completion can occur after the timeout but
575c6fd2807SJeff Garzik 	 * before this point.  In such cases, both types of
576c6fd2807SJeff Garzik 	 * completions are honored.  A scmd is determined to have
577c6fd2807SJeff Garzik 	 * timed out iff its associated qc is active and not failed.
578c6fd2807SJeff Garzik 	 */
579a4f08141SPaul E. McKenney 	spin_lock_irqsave(ap->lock, flags);
580c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
581c6fd2807SJeff Garzik 		struct scsi_cmnd *scmd, *tmp;
582c6fd2807SJeff Garzik 		int nr_timedout = 0;
583c6fd2807SJeff Garzik 
584c96f1732SAlan Cox 		/* This must occur under the ap->lock as we don't want
585c96f1732SAlan Cox 		   a polled recovery to race the real interrupt handler
586c96f1732SAlan Cox 
587c96f1732SAlan Cox 		   The lost_interrupt handler checks for any completed but
588c96f1732SAlan Cox 		   non-notified command and completes much like an IRQ handler.
589c96f1732SAlan Cox 
590c96f1732SAlan Cox 		   We then fall into the error recovery code which will treat
591c96f1732SAlan Cox 		   this as if normal completion won the race */
592c96f1732SAlan Cox 
593c96f1732SAlan Cox 		if (ap->ops->lost_interrupt)
594c96f1732SAlan Cox 			ap->ops->lost_interrupt(ap);
595c96f1732SAlan Cox 
5960e0b494cSJames Bottomley 		list_for_each_entry_safe(scmd, tmp, eh_work_q, eh_entry) {
597c6fd2807SJeff Garzik 			struct ata_queued_cmd *qc;
598c6fd2807SJeff Garzik 
599258c4e5cSJens Axboe 			ata_qc_for_each_raw(ap, qc, i) {
600c6fd2807SJeff Garzik 				if (qc->flags & ATA_QCFLAG_ACTIVE &&
601c6fd2807SJeff Garzik 				    qc->scsicmd == scmd)
602c6fd2807SJeff Garzik 					break;
603c6fd2807SJeff Garzik 			}
604c6fd2807SJeff Garzik 
605c6fd2807SJeff Garzik 			if (i < ATA_MAX_QUEUE) {
606c6fd2807SJeff Garzik 				/* the scmd has an associated qc */
607c6fd2807SJeff Garzik 				if (!(qc->flags & ATA_QCFLAG_FAILED)) {
608c6fd2807SJeff Garzik 					/* which hasn't failed yet, timeout */
609c6fd2807SJeff Garzik 					qc->err_mask |= AC_ERR_TIMEOUT;
610c6fd2807SJeff Garzik 					qc->flags |= ATA_QCFLAG_FAILED;
611c6fd2807SJeff Garzik 					nr_timedout++;
612c6fd2807SJeff Garzik 				}
613c6fd2807SJeff Garzik 			} else {
614c6fd2807SJeff Garzik 				/* Normal completion occurred after
615c6fd2807SJeff Garzik 				 * SCSI timeout but before this point.
616c6fd2807SJeff Garzik 				 * Successfully complete it.
617c6fd2807SJeff Garzik 				 */
618c6fd2807SJeff Garzik 				scmd->retries = scmd->allowed;
619c6fd2807SJeff Garzik 				scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
620c6fd2807SJeff Garzik 			}
621c6fd2807SJeff Garzik 		}
622c6fd2807SJeff Garzik 
623c6fd2807SJeff Garzik 		/* If we have timed out qcs.  They belong to EH from
624c6fd2807SJeff Garzik 		 * this point but the state of the controller is
625c6fd2807SJeff Garzik 		 * unknown.  Freeze the port to make sure the IRQ
626c6fd2807SJeff Garzik 		 * handler doesn't diddle with those qcs.  This must
627c6fd2807SJeff Garzik 		 * be done atomically w.r.t. setting QCFLAG_FAILED.
628c6fd2807SJeff Garzik 		 */
629c6fd2807SJeff Garzik 		if (nr_timedout)
630c6fd2807SJeff Garzik 			__ata_port_freeze(ap);
631c6fd2807SJeff Garzik 
632a1e10f7eSTejun Heo 
633a1e10f7eSTejun Heo 		/* initialize eh_tries */
634a1e10f7eSTejun Heo 		ap->eh_tries = ATA_EH_MAX_TRIES;
635a4f08141SPaul E. McKenney 	}
636a4f08141SPaul E. McKenney 	spin_unlock_irqrestore(ap->lock, flags);
637c6fd2807SJeff Garzik 
6380e0b494cSJames Bottomley }
6390e0b494cSJames Bottomley EXPORT_SYMBOL(ata_scsi_cmd_error_handler);
6400e0b494cSJames Bottomley 
6410e0b494cSJames Bottomley /**
6420e0b494cSJames Bottomley  * ata_scsi_port_error_handler - recover the port after the commands
6430e0b494cSJames Bottomley  * @host:	SCSI host containing the port
6440e0b494cSJames Bottomley  * @ap:		the ATA port
6450e0b494cSJames Bottomley  *
6460e0b494cSJames Bottomley  * Handle the recovery of the port @ap after all the commands
6470e0b494cSJames Bottomley  * have been recovered.
6480e0b494cSJames Bottomley  */
6490e0b494cSJames Bottomley void ata_scsi_port_error_handler(struct Scsi_Host *host, struct ata_port *ap)
6500e0b494cSJames Bottomley {
6510e0b494cSJames Bottomley 	unsigned long flags;
652c96f1732SAlan Cox 
653c6fd2807SJeff Garzik 	/* invoke error handler */
654c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
655cf1b86c8STejun Heo 		struct ata_link *link;
656cf1b86c8STejun Heo 
657c0c362b6STejun Heo 		/* acquire EH ownership */
658c0c362b6STejun Heo 		ata_eh_acquire(ap);
659c0c362b6STejun Heo  repeat:
6605ddf24c5STejun Heo 		/* kill fast drain timer */
6615ddf24c5STejun Heo 		del_timer_sync(&ap->fastdrain_timer);
6625ddf24c5STejun Heo 
663c6fd2807SJeff Garzik 		/* process port resume request */
664c6fd2807SJeff Garzik 		ata_eh_handle_port_resume(ap);
665c6fd2807SJeff Garzik 
666c6fd2807SJeff Garzik 		/* fetch & clear EH info */
667c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
668c6fd2807SJeff Garzik 
6691eca4365STejun Heo 		ata_for_each_link(link, ap, HOST_FIRST) {
67000115e0fSTejun Heo 			struct ata_eh_context *ehc = &link->eh_context;
67100115e0fSTejun Heo 			struct ata_device *dev;
67200115e0fSTejun Heo 
673cf1b86c8STejun Heo 			memset(&link->eh_context, 0, sizeof(link->eh_context));
674cf1b86c8STejun Heo 			link->eh_context.i = link->eh_info;
675cf1b86c8STejun Heo 			memset(&link->eh_info, 0, sizeof(link->eh_info));
67600115e0fSTejun Heo 
6771eca4365STejun Heo 			ata_for_each_dev(dev, link, ENABLED) {
67800115e0fSTejun Heo 				int devno = dev->devno;
67900115e0fSTejun Heo 
68000115e0fSTejun Heo 				ehc->saved_xfer_mode[devno] = dev->xfer_mode;
68100115e0fSTejun Heo 				if (ata_ncq_enabled(dev))
68200115e0fSTejun Heo 					ehc->saved_ncq_enabled |= 1 << devno;
68300115e0fSTejun Heo 			}
684cf1b86c8STejun Heo 		}
685c6fd2807SJeff Garzik 
686c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
687c6fd2807SJeff Garzik 		ap->pflags &= ~ATA_PFLAG_EH_PENDING;
688da917d69STejun Heo 		ap->excl_link = NULL;	/* don't maintain exclusion over EH */
689c6fd2807SJeff Garzik 
690c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
691c6fd2807SJeff Garzik 
692c6fd2807SJeff Garzik 		/* invoke EH, skip if unloading or suspended */
693c6fd2807SJeff Garzik 		if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
694c6fd2807SJeff Garzik 			ap->ops->error_handler(ap);
695ece180d1STejun Heo 		else {
696ece180d1STejun Heo 			/* if unloading, commence suicide */
697ece180d1STejun Heo 			if ((ap->pflags & ATA_PFLAG_UNLOADING) &&
698ece180d1STejun Heo 			    !(ap->pflags & ATA_PFLAG_UNLOADED))
699ece180d1STejun Heo 				ata_eh_unload(ap);
700c6fd2807SJeff Garzik 			ata_eh_finish(ap);
701ece180d1STejun Heo 		}
702c6fd2807SJeff Garzik 
703c6fd2807SJeff Garzik 		/* process port suspend request */
704c6fd2807SJeff Garzik 		ata_eh_handle_port_suspend(ap);
705c6fd2807SJeff Garzik 
70625985edcSLucas De Marchi 		/* Exception might have happened after ->error_handler
707c6fd2807SJeff Garzik 		 * recovered the port but before this point.  Repeat
708c6fd2807SJeff Garzik 		 * EH in such case.
709c6fd2807SJeff Garzik 		 */
710c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
711c6fd2807SJeff Garzik 
712c6fd2807SJeff Garzik 		if (ap->pflags & ATA_PFLAG_EH_PENDING) {
713a1e10f7eSTejun Heo 			if (--ap->eh_tries) {
714c6fd2807SJeff Garzik 				spin_unlock_irqrestore(ap->lock, flags);
715c6fd2807SJeff Garzik 				goto repeat;
716c6fd2807SJeff Garzik 			}
717a9a79dfeSJoe Perches 			ata_port_err(ap,
718a9a79dfeSJoe Perches 				     "EH pending after %d tries, giving up\n",
719a9a79dfeSJoe Perches 				     ATA_EH_MAX_TRIES);
720914616a3STejun Heo 			ap->pflags &= ~ATA_PFLAG_EH_PENDING;
721c6fd2807SJeff Garzik 		}
722c6fd2807SJeff Garzik 
723c6fd2807SJeff Garzik 		/* this run is complete, make sure EH info is clear */
7241eca4365STejun Heo 		ata_for_each_link(link, ap, HOST_FIRST)
725cf1b86c8STejun Heo 			memset(&link->eh_info, 0, sizeof(link->eh_info));
726c6fd2807SJeff Garzik 
727e4a9c373SDan Williams 		/* end eh (clear host_eh_scheduled) while holding
728e4a9c373SDan Williams 		 * ap->lock such that if exception occurs after this
729e4a9c373SDan Williams 		 * point but before EH completion, SCSI midlayer will
730c6fd2807SJeff Garzik 		 * re-initiate EH.
731c6fd2807SJeff Garzik 		 */
732e4a9c373SDan Williams 		ap->ops->end_eh(ap);
733c6fd2807SJeff Garzik 
734c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
735c0c362b6STejun Heo 		ata_eh_release(ap);
736c6fd2807SJeff Garzik 	} else {
7379af5c9c9STejun Heo 		WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
738c6fd2807SJeff Garzik 		ap->ops->eng_timeout(ap);
739c6fd2807SJeff Garzik 	}
740c6fd2807SJeff Garzik 
741c6fd2807SJeff Garzik 	scsi_eh_flush_done_q(&ap->eh_done_q);
742c6fd2807SJeff Garzik 
743c6fd2807SJeff Garzik 	/* clean up */
744c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
745c6fd2807SJeff Garzik 
746c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_LOADING)
747c6fd2807SJeff Garzik 		ap->pflags &= ~ATA_PFLAG_LOADING;
7486f54120eSJason Yan 	else if ((ap->pflags & ATA_PFLAG_SCSI_HOTPLUG) &&
7496f54120eSJason Yan 		!(ap->flags & ATA_FLAG_SAS_HOST))
750ad72cf98STejun Heo 		schedule_delayed_work(&ap->hotplug_task, 0);
751c6fd2807SJeff Garzik 
752c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_RECOVERED)
753a9a79dfeSJoe Perches 		ata_port_info(ap, "EH complete\n");
754c6fd2807SJeff Garzik 
755c6fd2807SJeff Garzik 	ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
756c6fd2807SJeff Garzik 
757c6fd2807SJeff Garzik 	/* tell wait_eh that we're done */
758c6fd2807SJeff Garzik 	ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
759c6fd2807SJeff Garzik 	wake_up_all(&ap->eh_wait_q);
760c6fd2807SJeff Garzik 
761c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
762c6fd2807SJeff Garzik }
7630e0b494cSJames Bottomley EXPORT_SYMBOL_GPL(ata_scsi_port_error_handler);
764c6fd2807SJeff Garzik 
765c6fd2807SJeff Garzik /**
766c6fd2807SJeff Garzik  *	ata_port_wait_eh - Wait for the currently pending EH to complete
767c6fd2807SJeff Garzik  *	@ap: Port to wait EH for
768c6fd2807SJeff Garzik  *
769c6fd2807SJeff Garzik  *	Wait until the currently pending EH is complete.
770c6fd2807SJeff Garzik  *
771c6fd2807SJeff Garzik  *	LOCKING:
772c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
773c6fd2807SJeff Garzik  */
774c6fd2807SJeff Garzik void ata_port_wait_eh(struct ata_port *ap)
775c6fd2807SJeff Garzik {
776c6fd2807SJeff Garzik 	unsigned long flags;
777c6fd2807SJeff Garzik 	DEFINE_WAIT(wait);
778c6fd2807SJeff Garzik 
779c6fd2807SJeff Garzik  retry:
780c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
781c6fd2807SJeff Garzik 
782c6fd2807SJeff Garzik 	while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
783c6fd2807SJeff Garzik 		prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
784c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
785c6fd2807SJeff Garzik 		schedule();
786c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
787c6fd2807SJeff Garzik 	}
788c6fd2807SJeff Garzik 	finish_wait(&ap->eh_wait_q, &wait);
789c6fd2807SJeff Garzik 
790c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
791c6fd2807SJeff Garzik 
792c6fd2807SJeff Garzik 	/* make sure SCSI EH is complete */
793cca3974eSJeff Garzik 	if (scsi_host_in_recovery(ap->scsi_host)) {
79497750cebSTejun Heo 		ata_msleep(ap, 10);
795c6fd2807SJeff Garzik 		goto retry;
796c6fd2807SJeff Garzik 	}
797c6fd2807SJeff Garzik }
79881c757bcSDan Williams EXPORT_SYMBOL_GPL(ata_port_wait_eh);
799c6fd2807SJeff Garzik 
8005ddf24c5STejun Heo static int ata_eh_nr_in_flight(struct ata_port *ap)
8015ddf24c5STejun Heo {
802258c4e5cSJens Axboe 	struct ata_queued_cmd *qc;
8035ddf24c5STejun Heo 	unsigned int tag;
8045ddf24c5STejun Heo 	int nr = 0;
8055ddf24c5STejun Heo 
8065ddf24c5STejun Heo 	/* count only non-internal commands */
807258c4e5cSJens Axboe 	ata_qc_for_each(ap, qc, tag) {
808258c4e5cSJens Axboe 		if (qc)
8095ddf24c5STejun Heo 			nr++;
8109d207accSJens Axboe 	}
8115ddf24c5STejun Heo 
8125ddf24c5STejun Heo 	return nr;
8135ddf24c5STejun Heo }
8145ddf24c5STejun Heo 
815b93ab338SKees Cook void ata_eh_fastdrain_timerfn(struct timer_list *t)
8165ddf24c5STejun Heo {
817b93ab338SKees Cook 	struct ata_port *ap = from_timer(ap, t, fastdrain_timer);
8185ddf24c5STejun Heo 	unsigned long flags;
8195ddf24c5STejun Heo 	int cnt;
8205ddf24c5STejun Heo 
8215ddf24c5STejun Heo 	spin_lock_irqsave(ap->lock, flags);
8225ddf24c5STejun Heo 
8235ddf24c5STejun Heo 	cnt = ata_eh_nr_in_flight(ap);
8245ddf24c5STejun Heo 
8255ddf24c5STejun Heo 	/* are we done? */
8265ddf24c5STejun Heo 	if (!cnt)
8275ddf24c5STejun Heo 		goto out_unlock;
8285ddf24c5STejun Heo 
8295ddf24c5STejun Heo 	if (cnt == ap->fastdrain_cnt) {
830258c4e5cSJens Axboe 		struct ata_queued_cmd *qc;
8315ddf24c5STejun Heo 		unsigned int tag;
8325ddf24c5STejun Heo 
8335ddf24c5STejun Heo 		/* No progress during the last interval, tag all
8345ddf24c5STejun Heo 		 * in-flight qcs as timed out and freeze the port.
8355ddf24c5STejun Heo 		 */
836258c4e5cSJens Axboe 		ata_qc_for_each(ap, qc, tag) {
8375ddf24c5STejun Heo 			if (qc)
8385ddf24c5STejun Heo 				qc->err_mask |= AC_ERR_TIMEOUT;
8395ddf24c5STejun Heo 		}
8405ddf24c5STejun Heo 
8415ddf24c5STejun Heo 		ata_port_freeze(ap);
8425ddf24c5STejun Heo 	} else {
8435ddf24c5STejun Heo 		/* some qcs have finished, give it another chance */
8445ddf24c5STejun Heo 		ap->fastdrain_cnt = cnt;
8455ddf24c5STejun Heo 		ap->fastdrain_timer.expires =
846341c2c95STejun Heo 			ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
8475ddf24c5STejun Heo 		add_timer(&ap->fastdrain_timer);
8485ddf24c5STejun Heo 	}
8495ddf24c5STejun Heo 
8505ddf24c5STejun Heo  out_unlock:
8515ddf24c5STejun Heo 	spin_unlock_irqrestore(ap->lock, flags);
8525ddf24c5STejun Heo }
8535ddf24c5STejun Heo 
8545ddf24c5STejun Heo /**
8555ddf24c5STejun Heo  *	ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
8565ddf24c5STejun Heo  *	@ap: target ATA port
8575ddf24c5STejun Heo  *	@fastdrain: activate fast drain
8585ddf24c5STejun Heo  *
8595ddf24c5STejun Heo  *	Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
8605ddf24c5STejun Heo  *	is non-zero and EH wasn't pending before.  Fast drain ensures
8615ddf24c5STejun Heo  *	that EH kicks in in timely manner.
8625ddf24c5STejun Heo  *
8635ddf24c5STejun Heo  *	LOCKING:
8645ddf24c5STejun Heo  *	spin_lock_irqsave(host lock)
8655ddf24c5STejun Heo  */
8665ddf24c5STejun Heo static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
8675ddf24c5STejun Heo {
8685ddf24c5STejun Heo 	int cnt;
8695ddf24c5STejun Heo 
8705ddf24c5STejun Heo 	/* already scheduled? */
8715ddf24c5STejun Heo 	if (ap->pflags & ATA_PFLAG_EH_PENDING)
8725ddf24c5STejun Heo 		return;
8735ddf24c5STejun Heo 
8745ddf24c5STejun Heo 	ap->pflags |= ATA_PFLAG_EH_PENDING;
8755ddf24c5STejun Heo 
8765ddf24c5STejun Heo 	if (!fastdrain)
8775ddf24c5STejun Heo 		return;
8785ddf24c5STejun Heo 
8795ddf24c5STejun Heo 	/* do we have in-flight qcs? */
8805ddf24c5STejun Heo 	cnt = ata_eh_nr_in_flight(ap);
8815ddf24c5STejun Heo 	if (!cnt)
8825ddf24c5STejun Heo 		return;
8835ddf24c5STejun Heo 
8845ddf24c5STejun Heo 	/* activate fast drain */
8855ddf24c5STejun Heo 	ap->fastdrain_cnt = cnt;
886341c2c95STejun Heo 	ap->fastdrain_timer.expires =
887341c2c95STejun Heo 		ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
8885ddf24c5STejun Heo 	add_timer(&ap->fastdrain_timer);
8895ddf24c5STejun Heo }
8905ddf24c5STejun Heo 
891c6fd2807SJeff Garzik /**
892c6fd2807SJeff Garzik  *	ata_qc_schedule_eh - schedule qc for error handling
893c6fd2807SJeff Garzik  *	@qc: command to schedule error handling for
894c6fd2807SJeff Garzik  *
895c6fd2807SJeff Garzik  *	Schedule error handling for @qc.  EH will kick in as soon as
896c6fd2807SJeff Garzik  *	other commands are drained.
897c6fd2807SJeff Garzik  *
898c6fd2807SJeff Garzik  *	LOCKING:
899cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
900c6fd2807SJeff Garzik  */
901c6fd2807SJeff Garzik void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
902c6fd2807SJeff Garzik {
903c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
904c6fd2807SJeff Garzik 
905c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
906c6fd2807SJeff Garzik 
907c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_FAILED;
9085ddf24c5STejun Heo 	ata_eh_set_pending(ap, 1);
909c6fd2807SJeff Garzik 
910c6fd2807SJeff Garzik 	/* The following will fail if timeout has already expired.
911c6fd2807SJeff Garzik 	 * ata_scsi_error() takes care of such scmds on EH entry.
912c6fd2807SJeff Garzik 	 * Note that ATA_QCFLAG_FAILED is unconditionally set after
913c6fd2807SJeff Garzik 	 * this function completes.
914c6fd2807SJeff Garzik 	 */
915242f9dcbSJens Axboe 	blk_abort_request(qc->scsicmd->request);
916c6fd2807SJeff Garzik }
917c6fd2807SJeff Garzik 
918c6fd2807SJeff Garzik /**
919e4a9c373SDan Williams  * ata_std_sched_eh - non-libsas ata_ports issue eh with this common routine
920e4a9c373SDan Williams  * @ap: ATA port to schedule EH for
921e4a9c373SDan Williams  *
922e4a9c373SDan Williams  *	LOCKING: inherited from ata_port_schedule_eh
923e4a9c373SDan Williams  *	spin_lock_irqsave(host lock)
924e4a9c373SDan Williams  */
925e4a9c373SDan Williams void ata_std_sched_eh(struct ata_port *ap)
926e4a9c373SDan Williams {
927e4a9c373SDan Williams 	WARN_ON(!ap->ops->error_handler);
928e4a9c373SDan Williams 
929e4a9c373SDan Williams 	if (ap->pflags & ATA_PFLAG_INITIALIZING)
930e4a9c373SDan Williams 		return;
931e4a9c373SDan Williams 
932e4a9c373SDan Williams 	ata_eh_set_pending(ap, 1);
933e4a9c373SDan Williams 	scsi_schedule_eh(ap->scsi_host);
934e4a9c373SDan Williams 
935e4a9c373SDan Williams 	DPRINTK("port EH scheduled\n");
936e4a9c373SDan Williams }
937e4a9c373SDan Williams EXPORT_SYMBOL_GPL(ata_std_sched_eh);
938e4a9c373SDan Williams 
939e4a9c373SDan Williams /**
940e4a9c373SDan Williams  * ata_std_end_eh - non-libsas ata_ports complete eh with this common routine
941e4a9c373SDan Williams  * @ap: ATA port to end EH for
942e4a9c373SDan Williams  *
943e4a9c373SDan Williams  * In the libata object model there is a 1:1 mapping of ata_port to
944e4a9c373SDan Williams  * shost, so host fields can be directly manipulated under ap->lock, in
945e4a9c373SDan Williams  * the libsas case we need to hold a lock at the ha->level to coordinate
946e4a9c373SDan Williams  * these events.
947e4a9c373SDan Williams  *
948e4a9c373SDan Williams  *	LOCKING:
949e4a9c373SDan Williams  *	spin_lock_irqsave(host lock)
950e4a9c373SDan Williams  */
951e4a9c373SDan Williams void ata_std_end_eh(struct ata_port *ap)
952e4a9c373SDan Williams {
953e4a9c373SDan Williams 	struct Scsi_Host *host = ap->scsi_host;
954e4a9c373SDan Williams 
955e4a9c373SDan Williams 	host->host_eh_scheduled = 0;
956e4a9c373SDan Williams }
957e4a9c373SDan Williams EXPORT_SYMBOL(ata_std_end_eh);
958e4a9c373SDan Williams 
959e4a9c373SDan Williams 
960e4a9c373SDan Williams /**
961c6fd2807SJeff Garzik  *	ata_port_schedule_eh - schedule error handling without a qc
962c6fd2807SJeff Garzik  *	@ap: ATA port to schedule EH for
963c6fd2807SJeff Garzik  *
964c6fd2807SJeff Garzik  *	Schedule error handling for @ap.  EH will kick in as soon as
965c6fd2807SJeff Garzik  *	all commands are drained.
966c6fd2807SJeff Garzik  *
967c6fd2807SJeff Garzik  *	LOCKING:
968cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
969c6fd2807SJeff Garzik  */
970c6fd2807SJeff Garzik void ata_port_schedule_eh(struct ata_port *ap)
971c6fd2807SJeff Garzik {
972e4a9c373SDan Williams 	/* see: ata_std_sched_eh, unless you know better */
973e4a9c373SDan Williams 	ap->ops->sched_eh(ap);
974c6fd2807SJeff Garzik }
975*a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
976c6fd2807SJeff Garzik 
977dbd82616STejun Heo static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
978c6fd2807SJeff Garzik {
979258c4e5cSJens Axboe 	struct ata_queued_cmd *qc;
980c6fd2807SJeff Garzik 	int tag, nr_aborted = 0;
981c6fd2807SJeff Garzik 
982c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
983c6fd2807SJeff Garzik 
9845ddf24c5STejun Heo 	/* we're gonna abort all commands, no need for fast drain */
9855ddf24c5STejun Heo 	ata_eh_set_pending(ap, 0);
9865ddf24c5STejun Heo 
98728361c40SJens Axboe 	/* include internal tag in iteration */
988258c4e5cSJens Axboe 	ata_qc_for_each_with_internal(ap, qc, tag) {
989dbd82616STejun Heo 		if (qc && (!link || qc->dev->link == link)) {
990c6fd2807SJeff Garzik 			qc->flags |= ATA_QCFLAG_FAILED;
991c6fd2807SJeff Garzik 			ata_qc_complete(qc);
992c6fd2807SJeff Garzik 			nr_aborted++;
993c6fd2807SJeff Garzik 		}
994c6fd2807SJeff Garzik 	}
995c6fd2807SJeff Garzik 
996c6fd2807SJeff Garzik 	if (!nr_aborted)
997c6fd2807SJeff Garzik 		ata_port_schedule_eh(ap);
998c6fd2807SJeff Garzik 
999c6fd2807SJeff Garzik 	return nr_aborted;
1000c6fd2807SJeff Garzik }
1001c6fd2807SJeff Garzik 
1002c6fd2807SJeff Garzik /**
1003dbd82616STejun Heo  *	ata_link_abort - abort all qc's on the link
1004dbd82616STejun Heo  *	@link: ATA link to abort qc's for
1005dbd82616STejun Heo  *
1006dbd82616STejun Heo  *	Abort all active qc's active on @link and schedule EH.
1007dbd82616STejun Heo  *
1008dbd82616STejun Heo  *	LOCKING:
1009dbd82616STejun Heo  *	spin_lock_irqsave(host lock)
1010dbd82616STejun Heo  *
1011dbd82616STejun Heo  *	RETURNS:
1012dbd82616STejun Heo  *	Number of aborted qc's.
1013dbd82616STejun Heo  */
1014dbd82616STejun Heo int ata_link_abort(struct ata_link *link)
1015dbd82616STejun Heo {
1016dbd82616STejun Heo 	return ata_do_link_abort(link->ap, link);
1017dbd82616STejun Heo }
1018*a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_link_abort);
1019dbd82616STejun Heo 
1020dbd82616STejun Heo /**
1021dbd82616STejun Heo  *	ata_port_abort - abort all qc's on the port
1022dbd82616STejun Heo  *	@ap: ATA port to abort qc's for
1023dbd82616STejun Heo  *
1024dbd82616STejun Heo  *	Abort all active qc's of @ap and schedule EH.
1025dbd82616STejun Heo  *
1026dbd82616STejun Heo  *	LOCKING:
1027dbd82616STejun Heo  *	spin_lock_irqsave(host_set lock)
1028dbd82616STejun Heo  *
1029dbd82616STejun Heo  *	RETURNS:
1030dbd82616STejun Heo  *	Number of aborted qc's.
1031dbd82616STejun Heo  */
1032dbd82616STejun Heo int ata_port_abort(struct ata_port *ap)
1033dbd82616STejun Heo {
1034dbd82616STejun Heo 	return ata_do_link_abort(ap, NULL);
1035dbd82616STejun Heo }
1036*a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_port_abort);
1037dbd82616STejun Heo 
1038dbd82616STejun Heo /**
1039c6fd2807SJeff Garzik  *	__ata_port_freeze - freeze port
1040c6fd2807SJeff Garzik  *	@ap: ATA port to freeze
1041c6fd2807SJeff Garzik  *
1042c6fd2807SJeff Garzik  *	This function is called when HSM violation or some other
1043c6fd2807SJeff Garzik  *	condition disrupts normal operation of the port.  Frozen port
1044c6fd2807SJeff Garzik  *	is not allowed to perform any operation until the port is
1045c6fd2807SJeff Garzik  *	thawed, which usually follows a successful reset.
1046c6fd2807SJeff Garzik  *
1047c6fd2807SJeff Garzik  *	ap->ops->freeze() callback can be used for freezing the port
1048c6fd2807SJeff Garzik  *	hardware-wise (e.g. mask interrupt and stop DMA engine).  If a
1049c6fd2807SJeff Garzik  *	port cannot be frozen hardware-wise, the interrupt handler
1050c6fd2807SJeff Garzik  *	must ack and clear interrupts unconditionally while the port
1051c6fd2807SJeff Garzik  *	is frozen.
1052c6fd2807SJeff Garzik  *
1053c6fd2807SJeff Garzik  *	LOCKING:
1054cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
1055c6fd2807SJeff Garzik  */
1056c6fd2807SJeff Garzik static void __ata_port_freeze(struct ata_port *ap)
1057c6fd2807SJeff Garzik {
1058c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
1059c6fd2807SJeff Garzik 
1060c6fd2807SJeff Garzik 	if (ap->ops->freeze)
1061c6fd2807SJeff Garzik 		ap->ops->freeze(ap);
1062c6fd2807SJeff Garzik 
1063c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_FROZEN;
1064c6fd2807SJeff Garzik 
106544877b4eSTejun Heo 	DPRINTK("ata%u port frozen\n", ap->print_id);
1066c6fd2807SJeff Garzik }
1067c6fd2807SJeff Garzik 
1068c6fd2807SJeff Garzik /**
1069c6fd2807SJeff Garzik  *	ata_port_freeze - abort & freeze port
1070c6fd2807SJeff Garzik  *	@ap: ATA port to freeze
1071c6fd2807SJeff Garzik  *
107254c38444SJeff Garzik  *	Abort and freeze @ap.  The freeze operation must be called
107354c38444SJeff Garzik  *	first, because some hardware requires special operations
107454c38444SJeff Garzik  *	before the taskfile registers are accessible.
1075c6fd2807SJeff Garzik  *
1076c6fd2807SJeff Garzik  *	LOCKING:
1077cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
1078c6fd2807SJeff Garzik  *
1079c6fd2807SJeff Garzik  *	RETURNS:
1080c6fd2807SJeff Garzik  *	Number of aborted commands.
1081c6fd2807SJeff Garzik  */
1082c6fd2807SJeff Garzik int ata_port_freeze(struct ata_port *ap)
1083c6fd2807SJeff Garzik {
1084c6fd2807SJeff Garzik 	int nr_aborted;
1085c6fd2807SJeff Garzik 
1086c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
1087c6fd2807SJeff Garzik 
1088c6fd2807SJeff Garzik 	__ata_port_freeze(ap);
108954c38444SJeff Garzik 	nr_aborted = ata_port_abort(ap);
1090c6fd2807SJeff Garzik 
1091c6fd2807SJeff Garzik 	return nr_aborted;
1092c6fd2807SJeff Garzik }
1093*a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_port_freeze);
1094c6fd2807SJeff Garzik 
1095c6fd2807SJeff Garzik /**
10967d77b247STejun Heo  *	sata_async_notification - SATA async notification handler
10977d77b247STejun Heo  *	@ap: ATA port where async notification is received
10987d77b247STejun Heo  *
10997d77b247STejun Heo  *	Handler to be called when async notification via SDB FIS is
11007d77b247STejun Heo  *	received.  This function schedules EH if necessary.
11017d77b247STejun Heo  *
11027d77b247STejun Heo  *	LOCKING:
11037d77b247STejun Heo  *	spin_lock_irqsave(host lock)
11047d77b247STejun Heo  *
11057d77b247STejun Heo  *	RETURNS:
11067d77b247STejun Heo  *	1 if EH is scheduled, 0 otherwise.
11077d77b247STejun Heo  */
11087d77b247STejun Heo int sata_async_notification(struct ata_port *ap)
11097d77b247STejun Heo {
11107d77b247STejun Heo 	u32 sntf;
11117d77b247STejun Heo 	int rc;
11127d77b247STejun Heo 
11137d77b247STejun Heo 	if (!(ap->flags & ATA_FLAG_AN))
11147d77b247STejun Heo 		return 0;
11157d77b247STejun Heo 
11167d77b247STejun Heo 	rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
11177d77b247STejun Heo 	if (rc == 0)
11187d77b247STejun Heo 		sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
11197d77b247STejun Heo 
1120071f44b1STejun Heo 	if (!sata_pmp_attached(ap) || rc) {
11217d77b247STejun Heo 		/* PMP is not attached or SNTF is not available */
1122071f44b1STejun Heo 		if (!sata_pmp_attached(ap)) {
11237d77b247STejun Heo 			/* PMP is not attached.  Check whether ATAPI
11247d77b247STejun Heo 			 * AN is configured.  If so, notify media
11257d77b247STejun Heo 			 * change.
11267d77b247STejun Heo 			 */
11277d77b247STejun Heo 			struct ata_device *dev = ap->link.device;
11287d77b247STejun Heo 
11297d77b247STejun Heo 			if ((dev->class == ATA_DEV_ATAPI) &&
11307d77b247STejun Heo 			    (dev->flags & ATA_DFLAG_AN))
11317d77b247STejun Heo 				ata_scsi_media_change_notify(dev);
11327d77b247STejun Heo 			return 0;
11337d77b247STejun Heo 		} else {
11347d77b247STejun Heo 			/* PMP is attached but SNTF is not available.
11357d77b247STejun Heo 			 * ATAPI async media change notification is
11367d77b247STejun Heo 			 * not used.  The PMP must be reporting PHY
11377d77b247STejun Heo 			 * status change, schedule EH.
11387d77b247STejun Heo 			 */
11397d77b247STejun Heo 			ata_port_schedule_eh(ap);
11407d77b247STejun Heo 			return 1;
11417d77b247STejun Heo 		}
11427d77b247STejun Heo 	} else {
11437d77b247STejun Heo 		/* PMP is attached and SNTF is available */
11447d77b247STejun Heo 		struct ata_link *link;
11457d77b247STejun Heo 
11467d77b247STejun Heo 		/* check and notify ATAPI AN */
11471eca4365STejun Heo 		ata_for_each_link(link, ap, EDGE) {
11487d77b247STejun Heo 			if (!(sntf & (1 << link->pmp)))
11497d77b247STejun Heo 				continue;
11507d77b247STejun Heo 
11517d77b247STejun Heo 			if ((link->device->class == ATA_DEV_ATAPI) &&
11527d77b247STejun Heo 			    (link->device->flags & ATA_DFLAG_AN))
11537d77b247STejun Heo 				ata_scsi_media_change_notify(link->device);
11547d77b247STejun Heo 		}
11557d77b247STejun Heo 
11567d77b247STejun Heo 		/* If PMP is reporting that PHY status of some
11577d77b247STejun Heo 		 * downstream ports has changed, schedule EH.
11587d77b247STejun Heo 		 */
11597d77b247STejun Heo 		if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
11607d77b247STejun Heo 			ata_port_schedule_eh(ap);
11617d77b247STejun Heo 			return 1;
11627d77b247STejun Heo 		}
11637d77b247STejun Heo 
11647d77b247STejun Heo 		return 0;
11657d77b247STejun Heo 	}
11667d77b247STejun Heo }
1167*a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(sata_async_notification);
11687d77b247STejun Heo 
11697d77b247STejun Heo /**
1170c6fd2807SJeff Garzik  *	ata_eh_freeze_port - EH helper to freeze port
1171c6fd2807SJeff Garzik  *	@ap: ATA port to freeze
1172c6fd2807SJeff Garzik  *
1173c6fd2807SJeff Garzik  *	Freeze @ap.
1174c6fd2807SJeff Garzik  *
1175c6fd2807SJeff Garzik  *	LOCKING:
1176c6fd2807SJeff Garzik  *	None.
1177c6fd2807SJeff Garzik  */
1178c6fd2807SJeff Garzik void ata_eh_freeze_port(struct ata_port *ap)
1179c6fd2807SJeff Garzik {
1180c6fd2807SJeff Garzik 	unsigned long flags;
1181c6fd2807SJeff Garzik 
1182c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
1183c6fd2807SJeff Garzik 		return;
1184c6fd2807SJeff Garzik 
1185c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1186c6fd2807SJeff Garzik 	__ata_port_freeze(ap);
1187c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1188c6fd2807SJeff Garzik }
1189*a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
1190c6fd2807SJeff Garzik 
1191c6fd2807SJeff Garzik /**
1192c6fd2807SJeff Garzik  *	ata_port_thaw_port - EH helper to thaw port
1193c6fd2807SJeff Garzik  *	@ap: ATA port to thaw
1194c6fd2807SJeff Garzik  *
1195c6fd2807SJeff Garzik  *	Thaw frozen port @ap.
1196c6fd2807SJeff Garzik  *
1197c6fd2807SJeff Garzik  *	LOCKING:
1198c6fd2807SJeff Garzik  *	None.
1199c6fd2807SJeff Garzik  */
1200c6fd2807SJeff Garzik void ata_eh_thaw_port(struct ata_port *ap)
1201c6fd2807SJeff Garzik {
1202c6fd2807SJeff Garzik 	unsigned long flags;
1203c6fd2807SJeff Garzik 
1204c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
1205c6fd2807SJeff Garzik 		return;
1206c6fd2807SJeff Garzik 
1207c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1208c6fd2807SJeff Garzik 
1209c6fd2807SJeff Garzik 	ap->pflags &= ~ATA_PFLAG_FROZEN;
1210c6fd2807SJeff Garzik 
1211c6fd2807SJeff Garzik 	if (ap->ops->thaw)
1212c6fd2807SJeff Garzik 		ap->ops->thaw(ap);
1213c6fd2807SJeff Garzik 
1214c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1215c6fd2807SJeff Garzik 
121644877b4eSTejun Heo 	DPRINTK("ata%u port thawed\n", ap->print_id);
1217c6fd2807SJeff Garzik }
1218*a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
1219c6fd2807SJeff Garzik 
1220c6fd2807SJeff Garzik static void ata_eh_scsidone(struct scsi_cmnd *scmd)
1221c6fd2807SJeff Garzik {
1222c6fd2807SJeff Garzik 	/* nada */
1223c6fd2807SJeff Garzik }
1224c6fd2807SJeff Garzik 
1225c6fd2807SJeff Garzik static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
1226c6fd2807SJeff Garzik {
1227c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
1228c6fd2807SJeff Garzik 	struct scsi_cmnd *scmd = qc->scsicmd;
1229c6fd2807SJeff Garzik 	unsigned long flags;
1230c6fd2807SJeff Garzik 
1231c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1232c6fd2807SJeff Garzik 	qc->scsidone = ata_eh_scsidone;
1233c6fd2807SJeff Garzik 	__ata_qc_complete(qc);
1234c6fd2807SJeff Garzik 	WARN_ON(ata_tag_valid(qc->tag));
1235c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1236c6fd2807SJeff Garzik 
1237c6fd2807SJeff Garzik 	scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
1238c6fd2807SJeff Garzik }
1239c6fd2807SJeff Garzik 
1240c6fd2807SJeff Garzik /**
1241c6fd2807SJeff Garzik  *	ata_eh_qc_complete - Complete an active ATA command from EH
1242c6fd2807SJeff Garzik  *	@qc: Command to complete
1243c6fd2807SJeff Garzik  *
1244c6fd2807SJeff Garzik  *	Indicate to the mid and upper layers that an ATA command has
1245c6fd2807SJeff Garzik  *	completed.  To be used from EH.
1246c6fd2807SJeff Garzik  */
1247c6fd2807SJeff Garzik void ata_eh_qc_complete(struct ata_queued_cmd *qc)
1248c6fd2807SJeff Garzik {
1249c6fd2807SJeff Garzik 	struct scsi_cmnd *scmd = qc->scsicmd;
1250c6fd2807SJeff Garzik 	scmd->retries = scmd->allowed;
1251c6fd2807SJeff Garzik 	__ata_eh_qc_complete(qc);
1252c6fd2807SJeff Garzik }
1253*a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
1254c6fd2807SJeff Garzik 
1255c6fd2807SJeff Garzik /**
1256c6fd2807SJeff Garzik  *	ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
1257c6fd2807SJeff Garzik  *	@qc: Command to retry
1258c6fd2807SJeff Garzik  *
1259c6fd2807SJeff Garzik  *	Indicate to the mid and upper layers that an ATA command
1260c6fd2807SJeff Garzik  *	should be retried.  To be used from EH.
1261c6fd2807SJeff Garzik  *
1262c6fd2807SJeff Garzik  *	SCSI midlayer limits the number of retries to scmd->allowed.
1263f13e2201SGwendal Grignou  *	scmd->allowed is incremented for commands which get retried
1264c6fd2807SJeff Garzik  *	due to unrelated failures (qc->err_mask is zero).
1265c6fd2807SJeff Garzik  */
1266c6fd2807SJeff Garzik void ata_eh_qc_retry(struct ata_queued_cmd *qc)
1267c6fd2807SJeff Garzik {
1268c6fd2807SJeff Garzik 	struct scsi_cmnd *scmd = qc->scsicmd;
1269f13e2201SGwendal Grignou 	if (!qc->err_mask)
1270f13e2201SGwendal Grignou 		scmd->allowed++;
1271c6fd2807SJeff Garzik 	__ata_eh_qc_complete(qc);
1272c6fd2807SJeff Garzik }
1273*a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
1274c6fd2807SJeff Garzik 
1275c6fd2807SJeff Garzik /**
1276678afac6STejun Heo  *	ata_dev_disable - disable ATA device
1277678afac6STejun Heo  *	@dev: ATA device to disable
1278678afac6STejun Heo  *
1279678afac6STejun Heo  *	Disable @dev.
1280678afac6STejun Heo  *
1281678afac6STejun Heo  *	Locking:
1282678afac6STejun Heo  *	EH context.
1283678afac6STejun Heo  */
1284678afac6STejun Heo void ata_dev_disable(struct ata_device *dev)
1285678afac6STejun Heo {
1286678afac6STejun Heo 	if (!ata_dev_enabled(dev))
1287678afac6STejun Heo 		return;
1288678afac6STejun Heo 
1289678afac6STejun Heo 	if (ata_msg_drv(dev->link->ap))
1290a9a79dfeSJoe Perches 		ata_dev_warn(dev, "disabled\n");
1291678afac6STejun Heo 	ata_acpi_on_disable(dev);
1292678afac6STejun Heo 	ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 | ATA_DNXFER_QUIET);
1293678afac6STejun Heo 	dev->class++;
129499cf610aSTejun Heo 
129599cf610aSTejun Heo 	/* From now till the next successful probe, ering is used to
129699cf610aSTejun Heo 	 * track probe failures.  Clear accumulated device error info.
129799cf610aSTejun Heo 	 */
129899cf610aSTejun Heo 	ata_ering_clear(&dev->ering);
1299678afac6STejun Heo }
1300*a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_dev_disable);
1301678afac6STejun Heo 
1302678afac6STejun Heo /**
1303c6fd2807SJeff Garzik  *	ata_eh_detach_dev - detach ATA device
1304c6fd2807SJeff Garzik  *	@dev: ATA device to detach
1305c6fd2807SJeff Garzik  *
1306c6fd2807SJeff Garzik  *	Detach @dev.
1307c6fd2807SJeff Garzik  *
1308c6fd2807SJeff Garzik  *	LOCKING:
1309c6fd2807SJeff Garzik  *	None.
1310c6fd2807SJeff Garzik  */
1311fb7fd614STejun Heo void ata_eh_detach_dev(struct ata_device *dev)
1312c6fd2807SJeff Garzik {
1313f58229f8STejun Heo 	struct ata_link *link = dev->link;
1314f58229f8STejun Heo 	struct ata_port *ap = link->ap;
131590484ebfSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
1316c6fd2807SJeff Garzik 	unsigned long flags;
1317c6fd2807SJeff Garzik 
1318c6fd2807SJeff Garzik 	ata_dev_disable(dev);
1319c6fd2807SJeff Garzik 
1320c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1321c6fd2807SJeff Garzik 
1322c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_DETACH;
1323c6fd2807SJeff Garzik 
1324c6fd2807SJeff Garzik 	if (ata_scsi_offline_dev(dev)) {
1325c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_DETACHED;
1326c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
1327c6fd2807SJeff Garzik 	}
1328c6fd2807SJeff Garzik 
132990484ebfSTejun Heo 	/* clear per-dev EH info */
1330f58229f8STejun Heo 	ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
1331f58229f8STejun Heo 	ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
133290484ebfSTejun Heo 	ehc->saved_xfer_mode[dev->devno] = 0;
133390484ebfSTejun Heo 	ehc->saved_ncq_enabled &= ~(1 << dev->devno);
1334c6fd2807SJeff Garzik 
1335c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1336c6fd2807SJeff Garzik }
1337c6fd2807SJeff Garzik 
1338c6fd2807SJeff Garzik /**
1339c6fd2807SJeff Garzik  *	ata_eh_about_to_do - about to perform eh_action
1340955e57dfSTejun Heo  *	@link: target ATA link
1341c6fd2807SJeff Garzik  *	@dev: target ATA dev for per-dev action (can be NULL)
1342c6fd2807SJeff Garzik  *	@action: action about to be performed
1343c6fd2807SJeff Garzik  *
1344c6fd2807SJeff Garzik  *	Called just before performing EH actions to clear related bits
1345955e57dfSTejun Heo  *	in @link->eh_info such that eh actions are not unnecessarily
1346955e57dfSTejun Heo  *	repeated.
1347c6fd2807SJeff Garzik  *
1348c6fd2807SJeff Garzik  *	LOCKING:
1349c6fd2807SJeff Garzik  *	None.
1350c6fd2807SJeff Garzik  */
1351fb7fd614STejun Heo void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
1352c6fd2807SJeff Garzik 			unsigned int action)
1353c6fd2807SJeff Garzik {
1354955e57dfSTejun Heo 	struct ata_port *ap = link->ap;
1355955e57dfSTejun Heo 	struct ata_eh_info *ehi = &link->eh_info;
1356955e57dfSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
1357c6fd2807SJeff Garzik 	unsigned long flags;
1358c6fd2807SJeff Garzik 
1359c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1360c6fd2807SJeff Garzik 
1361955e57dfSTejun Heo 	ata_eh_clear_action(link, dev, ehi, action);
1362c6fd2807SJeff Garzik 
1363a568d1d2STejun Heo 	/* About to take EH action, set RECOVERED.  Ignore actions on
1364a568d1d2STejun Heo 	 * slave links as master will do them again.
1365a568d1d2STejun Heo 	 */
1366a568d1d2STejun Heo 	if (!(ehc->i.flags & ATA_EHI_QUIET) && link != ap->slave_link)
1367c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_RECOVERED;
1368c6fd2807SJeff Garzik 
1369c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1370c6fd2807SJeff Garzik }
1371c6fd2807SJeff Garzik 
1372c6fd2807SJeff Garzik /**
1373c6fd2807SJeff Garzik  *	ata_eh_done - EH action complete
13742f60e1abSJonathan Corbet  *	@link: ATA link for which EH actions are complete
1375c6fd2807SJeff Garzik  *	@dev: target ATA dev for per-dev action (can be NULL)
1376c6fd2807SJeff Garzik  *	@action: action just completed
1377c6fd2807SJeff Garzik  *
1378c6fd2807SJeff Garzik  *	Called right after performing EH actions to clear related bits
1379955e57dfSTejun Heo  *	in @link->eh_context.
1380c6fd2807SJeff Garzik  *
1381c6fd2807SJeff Garzik  *	LOCKING:
1382c6fd2807SJeff Garzik  *	None.
1383c6fd2807SJeff Garzik  */
1384fb7fd614STejun Heo void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1385c6fd2807SJeff Garzik 		 unsigned int action)
1386c6fd2807SJeff Garzik {
1387955e57dfSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
13889af5c9c9STejun Heo 
1389955e57dfSTejun Heo 	ata_eh_clear_action(link, dev, &ehc->i, action);
1390c6fd2807SJeff Garzik }
1391c6fd2807SJeff Garzik 
1392c6fd2807SJeff Garzik /**
1393c6fd2807SJeff Garzik  *	ata_err_string - convert err_mask to descriptive string
1394c6fd2807SJeff Garzik  *	@err_mask: error mask to convert to string
1395c6fd2807SJeff Garzik  *
1396c6fd2807SJeff Garzik  *	Convert @err_mask to descriptive string.  Errors are
1397c6fd2807SJeff Garzik  *	prioritized according to severity and only the most severe
1398c6fd2807SJeff Garzik  *	error is reported.
1399c6fd2807SJeff Garzik  *
1400c6fd2807SJeff Garzik  *	LOCKING:
1401c6fd2807SJeff Garzik  *	None.
1402c6fd2807SJeff Garzik  *
1403c6fd2807SJeff Garzik  *	RETURNS:
1404c6fd2807SJeff Garzik  *	Descriptive string for @err_mask
1405c6fd2807SJeff Garzik  */
1406c6fd2807SJeff Garzik static const char *ata_err_string(unsigned int err_mask)
1407c6fd2807SJeff Garzik {
1408c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_HOST_BUS)
1409c6fd2807SJeff Garzik 		return "host bus error";
1410c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_ATA_BUS)
1411c6fd2807SJeff Garzik 		return "ATA bus error";
1412c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_TIMEOUT)
1413c6fd2807SJeff Garzik 		return "timeout";
1414c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_HSM)
1415c6fd2807SJeff Garzik 		return "HSM violation";
1416c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_SYSTEM)
1417c6fd2807SJeff Garzik 		return "internal error";
1418c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_MEDIA)
1419c6fd2807SJeff Garzik 		return "media error";
1420c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_INVALID)
1421c6fd2807SJeff Garzik 		return "invalid argument";
1422c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_DEV)
1423c6fd2807SJeff Garzik 		return "device error";
142454fb131bSDamien Le Moal 	if (err_mask & AC_ERR_NCQ)
142554fb131bSDamien Le Moal 		return "NCQ error";
142654fb131bSDamien Le Moal 	if (err_mask & AC_ERR_NODEV_HINT)
142754fb131bSDamien Le Moal 		return "Polling detection error";
1428c6fd2807SJeff Garzik 	return "unknown error";
1429c6fd2807SJeff Garzik }
1430c6fd2807SJeff Garzik 
1431c6fd2807SJeff Garzik /**
1432c6fd2807SJeff Garzik  *	ata_eh_read_log_10h - Read log page 10h for NCQ error details
1433c6fd2807SJeff Garzik  *	@dev: Device to read log page 10h from
1434c6fd2807SJeff Garzik  *	@tag: Resulting tag of the failed command
1435c6fd2807SJeff Garzik  *	@tf: Resulting taskfile registers of the failed command
1436c6fd2807SJeff Garzik  *
1437c6fd2807SJeff Garzik  *	Read log page 10h to obtain NCQ error details and clear error
1438c6fd2807SJeff Garzik  *	condition.
1439c6fd2807SJeff Garzik  *
1440c6fd2807SJeff Garzik  *	LOCKING:
1441c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1442c6fd2807SJeff Garzik  *
1443c6fd2807SJeff Garzik  *	RETURNS:
1444c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
1445c6fd2807SJeff Garzik  */
1446c6fd2807SJeff Garzik static int ata_eh_read_log_10h(struct ata_device *dev,
1447c6fd2807SJeff Garzik 			       int *tag, struct ata_taskfile *tf)
1448c6fd2807SJeff Garzik {
14499af5c9c9STejun Heo 	u8 *buf = dev->link->ap->sector_buf;
1450c6fd2807SJeff Garzik 	unsigned int err_mask;
1451c6fd2807SJeff Garzik 	u8 csum;
1452c6fd2807SJeff Garzik 	int i;
1453c6fd2807SJeff Garzik 
145465fe1f0fSShane Huang 	err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, 0, buf, 1);
1455c6fd2807SJeff Garzik 	if (err_mask)
1456c6fd2807SJeff Garzik 		return -EIO;
1457c6fd2807SJeff Garzik 
1458c6fd2807SJeff Garzik 	csum = 0;
1459c6fd2807SJeff Garzik 	for (i = 0; i < ATA_SECT_SIZE; i++)
1460c6fd2807SJeff Garzik 		csum += buf[i];
1461c6fd2807SJeff Garzik 	if (csum)
1462a9a79dfeSJoe Perches 		ata_dev_warn(dev, "invalid checksum 0x%x on log page 10h\n",
1463a9a79dfeSJoe Perches 			     csum);
1464c6fd2807SJeff Garzik 
1465c6fd2807SJeff Garzik 	if (buf[0] & 0x80)
1466c6fd2807SJeff Garzik 		return -ENOENT;
1467c6fd2807SJeff Garzik 
1468c6fd2807SJeff Garzik 	*tag = buf[0] & 0x1f;
1469c6fd2807SJeff Garzik 
1470c6fd2807SJeff Garzik 	tf->command = buf[2];
1471c6fd2807SJeff Garzik 	tf->feature = buf[3];
1472c6fd2807SJeff Garzik 	tf->lbal = buf[4];
1473c6fd2807SJeff Garzik 	tf->lbam = buf[5];
1474c6fd2807SJeff Garzik 	tf->lbah = buf[6];
1475c6fd2807SJeff Garzik 	tf->device = buf[7];
1476c6fd2807SJeff Garzik 	tf->hob_lbal = buf[8];
1477c6fd2807SJeff Garzik 	tf->hob_lbam = buf[9];
1478c6fd2807SJeff Garzik 	tf->hob_lbah = buf[10];
1479c6fd2807SJeff Garzik 	tf->nsect = buf[12];
1480c6fd2807SJeff Garzik 	tf->hob_nsect = buf[13];
1481ca156e00STejun Heo 	if (dev->class == ATA_DEV_ZAC && ata_id_has_ncq_autosense(dev->id))
14825b01e4b9SHannes Reinecke 		tf->auxiliary = buf[14] << 16 | buf[15] << 8 | buf[16];
1483c6fd2807SJeff Garzik 
1484c6fd2807SJeff Garzik 	return 0;
1485c6fd2807SJeff Garzik }
1486c6fd2807SJeff Garzik 
1487c6fd2807SJeff Garzik /**
148811fc33daSTejun Heo  *	atapi_eh_tur - perform ATAPI TEST_UNIT_READY
148911fc33daSTejun Heo  *	@dev: target ATAPI device
149011fc33daSTejun Heo  *	@r_sense_key: out parameter for sense_key
149111fc33daSTejun Heo  *
149211fc33daSTejun Heo  *	Perform ATAPI TEST_UNIT_READY.
149311fc33daSTejun Heo  *
149411fc33daSTejun Heo  *	LOCKING:
149511fc33daSTejun Heo  *	EH context (may sleep).
149611fc33daSTejun Heo  *
149711fc33daSTejun Heo  *	RETURNS:
149811fc33daSTejun Heo  *	0 on success, AC_ERR_* mask on failure.
149911fc33daSTejun Heo  */
15003dc67440SAaron Lu unsigned int atapi_eh_tur(struct ata_device *dev, u8 *r_sense_key)
150111fc33daSTejun Heo {
150211fc33daSTejun Heo 	u8 cdb[ATAPI_CDB_LEN] = { TEST_UNIT_READY, 0, 0, 0, 0, 0 };
150311fc33daSTejun Heo 	struct ata_taskfile tf;
150411fc33daSTejun Heo 	unsigned int err_mask;
150511fc33daSTejun Heo 
150611fc33daSTejun Heo 	ata_tf_init(dev, &tf);
150711fc33daSTejun Heo 
150811fc33daSTejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
150911fc33daSTejun Heo 	tf.command = ATA_CMD_PACKET;
151011fc33daSTejun Heo 	tf.protocol = ATAPI_PROT_NODATA;
151111fc33daSTejun Heo 
151211fc33daSTejun Heo 	err_mask = ata_exec_internal(dev, &tf, cdb, DMA_NONE, NULL, 0, 0);
151311fc33daSTejun Heo 	if (err_mask == AC_ERR_DEV)
151411fc33daSTejun Heo 		*r_sense_key = tf.feature >> 4;
151511fc33daSTejun Heo 	return err_mask;
151611fc33daSTejun Heo }
151711fc33daSTejun Heo 
151811fc33daSTejun Heo /**
1519e87fd28cSHannes Reinecke  *	ata_eh_request_sense - perform REQUEST_SENSE_DATA_EXT
15202f60e1abSJonathan Corbet  *	@qc: qc to perform REQUEST_SENSE_SENSE_DATA_EXT to
1521e87fd28cSHannes Reinecke  *	@cmd: scsi command for which the sense code should be set
1522e87fd28cSHannes Reinecke  *
1523e87fd28cSHannes Reinecke  *	Perform REQUEST_SENSE_DATA_EXT after the device reported CHECK
1524e87fd28cSHannes Reinecke  *	SENSE.  This function is an EH helper.
1525e87fd28cSHannes Reinecke  *
1526e87fd28cSHannes Reinecke  *	LOCKING:
1527e87fd28cSHannes Reinecke  *	Kernel thread context (may sleep).
1528e87fd28cSHannes Reinecke  */
1529e87fd28cSHannes Reinecke static void ata_eh_request_sense(struct ata_queued_cmd *qc,
1530e87fd28cSHannes Reinecke 				 struct scsi_cmnd *cmd)
1531e87fd28cSHannes Reinecke {
1532e87fd28cSHannes Reinecke 	struct ata_device *dev = qc->dev;
1533e87fd28cSHannes Reinecke 	struct ata_taskfile tf;
1534e87fd28cSHannes Reinecke 	unsigned int err_mask;
1535e87fd28cSHannes Reinecke 
1536e87fd28cSHannes Reinecke 	if (qc->ap->pflags & ATA_PFLAG_FROZEN) {
1537e87fd28cSHannes Reinecke 		ata_dev_warn(dev, "sense data available but port frozen\n");
1538e87fd28cSHannes Reinecke 		return;
1539e87fd28cSHannes Reinecke 	}
1540e87fd28cSHannes Reinecke 
1541d238ffd5SHannes Reinecke 	if (!cmd || qc->flags & ATA_QCFLAG_SENSE_VALID)
1542e87fd28cSHannes Reinecke 		return;
1543e87fd28cSHannes Reinecke 
1544e87fd28cSHannes Reinecke 	if (!ata_id_sense_reporting_enabled(dev->id)) {
1545e87fd28cSHannes Reinecke 		ata_dev_warn(qc->dev, "sense data reporting disabled\n");
1546e87fd28cSHannes Reinecke 		return;
1547e87fd28cSHannes Reinecke 	}
1548e87fd28cSHannes Reinecke 
1549e87fd28cSHannes Reinecke 	DPRINTK("ATA request sense\n");
1550e87fd28cSHannes Reinecke 
1551e87fd28cSHannes Reinecke 	ata_tf_init(dev, &tf);
1552e87fd28cSHannes Reinecke 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1553e87fd28cSHannes Reinecke 	tf.flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
1554e87fd28cSHannes Reinecke 	tf.command = ATA_CMD_REQ_SENSE_DATA;
1555e87fd28cSHannes Reinecke 	tf.protocol = ATA_PROT_NODATA;
1556e87fd28cSHannes Reinecke 
1557e87fd28cSHannes Reinecke 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1558e87fd28cSHannes Reinecke 	/* Ignore err_mask; ATA_ERR might be set */
1559e87fd28cSHannes Reinecke 	if (tf.command & ATA_SENSE) {
156006dbde5fSHannes Reinecke 		ata_scsi_set_sense(dev, cmd, tf.lbah, tf.lbam, tf.lbal);
1561e87fd28cSHannes Reinecke 		qc->flags |= ATA_QCFLAG_SENSE_VALID;
1562e87fd28cSHannes Reinecke 	} else {
1563e87fd28cSHannes Reinecke 		ata_dev_warn(dev, "request sense failed stat %02x emask %x\n",
1564e87fd28cSHannes Reinecke 			     tf.command, err_mask);
1565e87fd28cSHannes Reinecke 	}
1566e87fd28cSHannes Reinecke }
1567e87fd28cSHannes Reinecke 
1568e87fd28cSHannes Reinecke /**
1569c6fd2807SJeff Garzik  *	atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1570c6fd2807SJeff Garzik  *	@dev: device to perform REQUEST_SENSE to
1571c6fd2807SJeff Garzik  *	@sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
15723eabddb8STejun Heo  *	@dfl_sense_key: default sense key to use
1573c6fd2807SJeff Garzik  *
1574c6fd2807SJeff Garzik  *	Perform ATAPI REQUEST_SENSE after the device reported CHECK
1575c6fd2807SJeff Garzik  *	SENSE.  This function is EH helper.
1576c6fd2807SJeff Garzik  *
1577c6fd2807SJeff Garzik  *	LOCKING:
1578c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1579c6fd2807SJeff Garzik  *
1580c6fd2807SJeff Garzik  *	RETURNS:
1581c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask on failure
1582c6fd2807SJeff Garzik  */
15833dc67440SAaron Lu unsigned int atapi_eh_request_sense(struct ata_device *dev,
15843eabddb8STejun Heo 					   u8 *sense_buf, u8 dfl_sense_key)
1585c6fd2807SJeff Garzik {
15863eabddb8STejun Heo 	u8 cdb[ATAPI_CDB_LEN] =
15873eabddb8STejun Heo 		{ REQUEST_SENSE, 0, 0, 0, SCSI_SENSE_BUFFERSIZE, 0 };
15889af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
1589c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1590c6fd2807SJeff Garzik 
1591c6fd2807SJeff Garzik 	DPRINTK("ATAPI request sense\n");
1592c6fd2807SJeff Garzik 
1593c6fd2807SJeff Garzik 	memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1594c6fd2807SJeff Garzik 
159556287768SAlbert Lee 	/* initialize sense_buf with the error register,
159656287768SAlbert Lee 	 * for the case where they are -not- overwritten
159756287768SAlbert Lee 	 */
1598c6fd2807SJeff Garzik 	sense_buf[0] = 0x70;
15993eabddb8STejun Heo 	sense_buf[2] = dfl_sense_key;
160056287768SAlbert Lee 
160156287768SAlbert Lee 	/* some devices time out if garbage left in tf */
160256287768SAlbert Lee 	ata_tf_init(dev, &tf);
1603c6fd2807SJeff Garzik 
1604c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1605c6fd2807SJeff Garzik 	tf.command = ATA_CMD_PACKET;
1606c6fd2807SJeff Garzik 
1607c6fd2807SJeff Garzik 	/* is it pointless to prefer PIO for "safety reasons"? */
1608c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_PIO_DMA) {
16090dc36888STejun Heo 		tf.protocol = ATAPI_PROT_DMA;
1610c6fd2807SJeff Garzik 		tf.feature |= ATAPI_PKT_DMA;
1611c6fd2807SJeff Garzik 	} else {
16120dc36888STejun Heo 		tf.protocol = ATAPI_PROT_PIO;
1613f2dfc1a1STejun Heo 		tf.lbam = SCSI_SENSE_BUFFERSIZE;
1614f2dfc1a1STejun Heo 		tf.lbah = 0;
1615c6fd2807SJeff Garzik 	}
1616c6fd2807SJeff Garzik 
1617c6fd2807SJeff Garzik 	return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
16182b789108STejun Heo 				 sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
1619c6fd2807SJeff Garzik }
1620c6fd2807SJeff Garzik 
1621c6fd2807SJeff Garzik /**
1622c6fd2807SJeff Garzik  *	ata_eh_analyze_serror - analyze SError for a failed port
16230260731fSTejun Heo  *	@link: ATA link to analyze SError for
1624c6fd2807SJeff Garzik  *
1625c6fd2807SJeff Garzik  *	Analyze SError if available and further determine cause of
1626c6fd2807SJeff Garzik  *	failure.
1627c6fd2807SJeff Garzik  *
1628c6fd2807SJeff Garzik  *	LOCKING:
1629c6fd2807SJeff Garzik  *	None.
1630c6fd2807SJeff Garzik  */
16310260731fSTejun Heo static void ata_eh_analyze_serror(struct ata_link *link)
1632c6fd2807SJeff Garzik {
16330260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
1634c6fd2807SJeff Garzik 	u32 serror = ehc->i.serror;
1635c6fd2807SJeff Garzik 	unsigned int err_mask = 0, action = 0;
1636f9df58cbSTejun Heo 	u32 hotplug_mask;
1637c6fd2807SJeff Garzik 
1638e0614db2STejun Heo 	if (serror & (SERR_PERSISTENT | SERR_DATA)) {
1639c6fd2807SJeff Garzik 		err_mask |= AC_ERR_ATA_BUS;
1640cf480626STejun Heo 		action |= ATA_EH_RESET;
1641c6fd2807SJeff Garzik 	}
1642c6fd2807SJeff Garzik 	if (serror & SERR_PROTOCOL) {
1643c6fd2807SJeff Garzik 		err_mask |= AC_ERR_HSM;
1644cf480626STejun Heo 		action |= ATA_EH_RESET;
1645c6fd2807SJeff Garzik 	}
1646c6fd2807SJeff Garzik 	if (serror & SERR_INTERNAL) {
1647c6fd2807SJeff Garzik 		err_mask |= AC_ERR_SYSTEM;
1648cf480626STejun Heo 		action |= ATA_EH_RESET;
1649c6fd2807SJeff Garzik 	}
1650f9df58cbSTejun Heo 
1651f9df58cbSTejun Heo 	/* Determine whether a hotplug event has occurred.  Both
1652f9df58cbSTejun Heo 	 * SError.N/X are considered hotplug events for enabled or
1653f9df58cbSTejun Heo 	 * host links.  For disabled PMP links, only N bit is
1654f9df58cbSTejun Heo 	 * considered as X bit is left at 1 for link plugging.
1655f9df58cbSTejun Heo 	 */
1656eb0e85e3STejun Heo 	if (link->lpm_policy > ATA_LPM_MAX_POWER)
16576b7ae954STejun Heo 		hotplug_mask = 0;	/* hotplug doesn't work w/ LPM */
16586b7ae954STejun Heo 	else if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
1659f9df58cbSTejun Heo 		hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
1660f9df58cbSTejun Heo 	else
1661f9df58cbSTejun Heo 		hotplug_mask = SERR_PHYRDY_CHG;
1662f9df58cbSTejun Heo 
1663f9df58cbSTejun Heo 	if (serror & hotplug_mask)
1664c6fd2807SJeff Garzik 		ata_ehi_hotplugged(&ehc->i);
1665c6fd2807SJeff Garzik 
1666c6fd2807SJeff Garzik 	ehc->i.err_mask |= err_mask;
1667c6fd2807SJeff Garzik 	ehc->i.action |= action;
1668c6fd2807SJeff Garzik }
1669c6fd2807SJeff Garzik 
1670c6fd2807SJeff Garzik /**
1671c6fd2807SJeff Garzik  *	ata_eh_analyze_ncq_error - analyze NCQ error
16720260731fSTejun Heo  *	@link: ATA link to analyze NCQ error for
1673c6fd2807SJeff Garzik  *
1674c6fd2807SJeff Garzik  *	Read log page 10h, determine the offending qc and acquire
1675c6fd2807SJeff Garzik  *	error status TF.  For NCQ device errors, all LLDDs have to do
1676c6fd2807SJeff Garzik  *	is setting AC_ERR_DEV in ehi->err_mask.  This function takes
1677c6fd2807SJeff Garzik  *	care of the rest.
1678c6fd2807SJeff Garzik  *
1679c6fd2807SJeff Garzik  *	LOCKING:
1680c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1681c6fd2807SJeff Garzik  */
168210acf3b0SMark Lord void ata_eh_analyze_ncq_error(struct ata_link *link)
1683c6fd2807SJeff Garzik {
16840260731fSTejun Heo 	struct ata_port *ap = link->ap;
16850260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
16860260731fSTejun Heo 	struct ata_device *dev = link->device;
1687c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
1688c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1689c6fd2807SJeff Garzik 	int tag, rc;
1690c6fd2807SJeff Garzik 
1691c6fd2807SJeff Garzik 	/* if frozen, we can't do much */
1692c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN)
1693c6fd2807SJeff Garzik 		return;
1694c6fd2807SJeff Garzik 
1695c6fd2807SJeff Garzik 	/* is it NCQ device error? */
16960260731fSTejun Heo 	if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1697c6fd2807SJeff Garzik 		return;
1698c6fd2807SJeff Garzik 
1699c6fd2807SJeff Garzik 	/* has LLDD analyzed already? */
1700258c4e5cSJens Axboe 	ata_qc_for_each_raw(ap, qc, tag) {
1701c6fd2807SJeff Garzik 		if (!(qc->flags & ATA_QCFLAG_FAILED))
1702c6fd2807SJeff Garzik 			continue;
1703c6fd2807SJeff Garzik 
1704c6fd2807SJeff Garzik 		if (qc->err_mask)
1705c6fd2807SJeff Garzik 			return;
1706c6fd2807SJeff Garzik 	}
1707c6fd2807SJeff Garzik 
1708c6fd2807SJeff Garzik 	/* okay, this error is ours */
1709a09bf4cdSJeff Garzik 	memset(&tf, 0, sizeof(tf));
1710c6fd2807SJeff Garzik 	rc = ata_eh_read_log_10h(dev, &tag, &tf);
1711c6fd2807SJeff Garzik 	if (rc) {
1712a9a79dfeSJoe Perches 		ata_link_err(link, "failed to read log page 10h (errno=%d)\n",
1713a9a79dfeSJoe Perches 			     rc);
1714c6fd2807SJeff Garzik 		return;
1715c6fd2807SJeff Garzik 	}
1716c6fd2807SJeff Garzik 
17170260731fSTejun Heo 	if (!(link->sactive & (1 << tag))) {
1718a9a79dfeSJoe Perches 		ata_link_err(link, "log page 10h reported inactive tag %d\n",
1719a9a79dfeSJoe Perches 			     tag);
1720c6fd2807SJeff Garzik 		return;
1721c6fd2807SJeff Garzik 	}
1722c6fd2807SJeff Garzik 
1723c6fd2807SJeff Garzik 	/* we've got the perpetrator, condemn it */
1724c6fd2807SJeff Garzik 	qc = __ata_qc_from_tag(ap, tag);
1725c6fd2807SJeff Garzik 	memcpy(&qc->result_tf, &tf, sizeof(tf));
1726a6116c9eSMark Lord 	qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
17275335b729STejun Heo 	qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
1728ca156e00STejun Heo 	if (dev->class == ATA_DEV_ZAC &&
1729ca156e00STejun Heo 	    ((qc->result_tf.command & ATA_SENSE) || qc->result_tf.auxiliary)) {
17305b01e4b9SHannes Reinecke 		char sense_key, asc, ascq;
17315b01e4b9SHannes Reinecke 
17325b01e4b9SHannes Reinecke 		sense_key = (qc->result_tf.auxiliary >> 16) & 0xff;
17335b01e4b9SHannes Reinecke 		asc = (qc->result_tf.auxiliary >> 8) & 0xff;
17345b01e4b9SHannes Reinecke 		ascq = qc->result_tf.auxiliary & 0xff;
173506dbde5fSHannes Reinecke 		ata_scsi_set_sense(dev, qc->scsicmd, sense_key, asc, ascq);
1736492bf621SHannes Reinecke 		ata_scsi_set_sense_information(dev, qc->scsicmd,
1737492bf621SHannes Reinecke 					       &qc->result_tf);
17385b01e4b9SHannes Reinecke 		qc->flags |= ATA_QCFLAG_SENSE_VALID;
17395b01e4b9SHannes Reinecke 	}
17405b01e4b9SHannes Reinecke 
1741c6fd2807SJeff Garzik 	ehc->i.err_mask &= ~AC_ERR_DEV;
1742c6fd2807SJeff Garzik }
1743*a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);
1744c6fd2807SJeff Garzik 
1745c6fd2807SJeff Garzik /**
1746c6fd2807SJeff Garzik  *	ata_eh_analyze_tf - analyze taskfile of a failed qc
1747c6fd2807SJeff Garzik  *	@qc: qc to analyze
1748c6fd2807SJeff Garzik  *	@tf: Taskfile registers to analyze
1749c6fd2807SJeff Garzik  *
1750c6fd2807SJeff Garzik  *	Analyze taskfile of @qc and further determine cause of
1751c6fd2807SJeff Garzik  *	failure.  This function also requests ATAPI sense data if
175225985edcSLucas De Marchi  *	available.
1753c6fd2807SJeff Garzik  *
1754c6fd2807SJeff Garzik  *	LOCKING:
1755c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1756c6fd2807SJeff Garzik  *
1757c6fd2807SJeff Garzik  *	RETURNS:
1758c6fd2807SJeff Garzik  *	Determined recovery action
1759c6fd2807SJeff Garzik  */
1760c6fd2807SJeff Garzik static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1761c6fd2807SJeff Garzik 				      const struct ata_taskfile *tf)
1762c6fd2807SJeff Garzik {
1763c6fd2807SJeff Garzik 	unsigned int tmp, action = 0;
1764c6fd2807SJeff Garzik 	u8 stat = tf->command, err = tf->feature;
1765c6fd2807SJeff Garzik 
1766c6fd2807SJeff Garzik 	if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1767c6fd2807SJeff Garzik 		qc->err_mask |= AC_ERR_HSM;
1768cf480626STejun Heo 		return ATA_EH_RESET;
1769c6fd2807SJeff Garzik 	}
1770c6fd2807SJeff Garzik 
1771e87fd28cSHannes Reinecke 	if (stat & (ATA_ERR | ATA_DF)) {
1772a51d644aSTejun Heo 		qc->err_mask |= AC_ERR_DEV;
1773e87fd28cSHannes Reinecke 		/*
1774e87fd28cSHannes Reinecke 		 * Sense data reporting does not work if the
1775e87fd28cSHannes Reinecke 		 * device fault bit is set.
1776e87fd28cSHannes Reinecke 		 */
1777e87fd28cSHannes Reinecke 		if (stat & ATA_DF)
1778e87fd28cSHannes Reinecke 			stat &= ~ATA_SENSE;
1779e87fd28cSHannes Reinecke 	} else {
1780c6fd2807SJeff Garzik 		return 0;
1781e87fd28cSHannes Reinecke 	}
1782c6fd2807SJeff Garzik 
1783c6fd2807SJeff Garzik 	switch (qc->dev->class) {
17849162c657SHannes Reinecke 	case ATA_DEV_ZAC:
1785e87fd28cSHannes Reinecke 		if (stat & ATA_SENSE)
1786e87fd28cSHannes Reinecke 			ata_eh_request_sense(qc, qc->scsicmd);
1787ca156e00STejun Heo 		/* fall through */
1788ca156e00STejun Heo 	case ATA_DEV_ATA:
1789c6fd2807SJeff Garzik 		if (err & ATA_ICRC)
1790c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_ATA_BUS;
1791eec7e1c1SAlexey Asemov 		if (err & (ATA_UNC | ATA_AMNF))
1792c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_MEDIA;
1793c6fd2807SJeff Garzik 		if (err & ATA_IDNF)
1794c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_INVALID;
1795c6fd2807SJeff Garzik 		break;
1796c6fd2807SJeff Garzik 
1797c6fd2807SJeff Garzik 	case ATA_DEV_ATAPI:
1798a569a30dSTejun Heo 		if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
17993eabddb8STejun Heo 			tmp = atapi_eh_request_sense(qc->dev,
18003eabddb8STejun Heo 						qc->scsicmd->sense_buffer,
18013eabddb8STejun Heo 						qc->result_tf.feature >> 4);
18023852e373SHannes Reinecke 			if (!tmp)
1803c6fd2807SJeff Garzik 				qc->flags |= ATA_QCFLAG_SENSE_VALID;
18043852e373SHannes Reinecke 			else
1805c6fd2807SJeff Garzik 				qc->err_mask |= tmp;
1806c6fd2807SJeff Garzik 		}
1807a569a30dSTejun Heo 	}
1808c6fd2807SJeff Garzik 
18093852e373SHannes Reinecke 	if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
18103852e373SHannes Reinecke 		int ret = scsi_check_sense(qc->scsicmd);
18113852e373SHannes Reinecke 		/*
181279487259SDamien Le Moal 		 * SUCCESS here means that the sense code could be
18133852e373SHannes Reinecke 		 * evaluated and should be passed to the upper layers
18143852e373SHannes Reinecke 		 * for correct evaluation.
181579487259SDamien Le Moal 		 * FAILED means the sense code could not be interpreted
18163852e373SHannes Reinecke 		 * and the device would need to be reset.
18173852e373SHannes Reinecke 		 * NEEDS_RETRY and ADD_TO_MLQUEUE means that the
18183852e373SHannes Reinecke 		 * command would need to be retried.
18193852e373SHannes Reinecke 		 */
18203852e373SHannes Reinecke 		if (ret == NEEDS_RETRY || ret == ADD_TO_MLQUEUE) {
18213852e373SHannes Reinecke 			qc->flags |= ATA_QCFLAG_RETRY;
18223852e373SHannes Reinecke 			qc->err_mask |= AC_ERR_OTHER;
18233852e373SHannes Reinecke 		} else if (ret != SUCCESS) {
18243852e373SHannes Reinecke 			qc->err_mask |= AC_ERR_HSM;
18253852e373SHannes Reinecke 		}
18263852e373SHannes Reinecke 	}
1827c6fd2807SJeff Garzik 	if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1828cf480626STejun Heo 		action |= ATA_EH_RESET;
1829c6fd2807SJeff Garzik 
1830c6fd2807SJeff Garzik 	return action;
1831c6fd2807SJeff Garzik }
1832c6fd2807SJeff Garzik 
183376326ac1STejun Heo static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask,
183476326ac1STejun Heo 				   int *xfer_ok)
1835c6fd2807SJeff Garzik {
183676326ac1STejun Heo 	int base = 0;
183776326ac1STejun Heo 
183876326ac1STejun Heo 	if (!(eflags & ATA_EFLAG_DUBIOUS_XFER))
183976326ac1STejun Heo 		*xfer_ok = 1;
184076326ac1STejun Heo 
184176326ac1STejun Heo 	if (!*xfer_ok)
184275f9cafcSTejun Heo 		base = ATA_ECAT_DUBIOUS_NONE;
184376326ac1STejun Heo 
18447d47e8d4STejun Heo 	if (err_mask & AC_ERR_ATA_BUS)
184576326ac1STejun Heo 		return base + ATA_ECAT_ATA_BUS;
1846c6fd2807SJeff Garzik 
18477d47e8d4STejun Heo 	if (err_mask & AC_ERR_TIMEOUT)
184876326ac1STejun Heo 		return base + ATA_ECAT_TOUT_HSM;
18497d47e8d4STejun Heo 
18503884f7b0STejun Heo 	if (eflags & ATA_EFLAG_IS_IO) {
18517d47e8d4STejun Heo 		if (err_mask & AC_ERR_HSM)
185276326ac1STejun Heo 			return base + ATA_ECAT_TOUT_HSM;
18537d47e8d4STejun Heo 		if ((err_mask &
18547d47e8d4STejun Heo 		     (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
185576326ac1STejun Heo 			return base + ATA_ECAT_UNK_DEV;
1856c6fd2807SJeff Garzik 	}
1857c6fd2807SJeff Garzik 
1858c6fd2807SJeff Garzik 	return 0;
1859c6fd2807SJeff Garzik }
1860c6fd2807SJeff Garzik 
18617d47e8d4STejun Heo struct speed_down_verdict_arg {
1862c6fd2807SJeff Garzik 	u64 since;
186376326ac1STejun Heo 	int xfer_ok;
18643884f7b0STejun Heo 	int nr_errors[ATA_ECAT_NR];
1865c6fd2807SJeff Garzik };
1866c6fd2807SJeff Garzik 
18677d47e8d4STejun Heo static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
1868c6fd2807SJeff Garzik {
18697d47e8d4STejun Heo 	struct speed_down_verdict_arg *arg = void_arg;
187076326ac1STejun Heo 	int cat;
1871c6fd2807SJeff Garzik 
1872d9027470SGwendal Grignou 	if ((ent->eflags & ATA_EFLAG_OLD_ER) || (ent->timestamp < arg->since))
1873c6fd2807SJeff Garzik 		return -1;
1874c6fd2807SJeff Garzik 
187576326ac1STejun Heo 	cat = ata_eh_categorize_error(ent->eflags, ent->err_mask,
187676326ac1STejun Heo 				      &arg->xfer_ok);
18777d47e8d4STejun Heo 	arg->nr_errors[cat]++;
187876326ac1STejun Heo 
1879c6fd2807SJeff Garzik 	return 0;
1880c6fd2807SJeff Garzik }
1881c6fd2807SJeff Garzik 
1882c6fd2807SJeff Garzik /**
18837d47e8d4STejun Heo  *	ata_eh_speed_down_verdict - Determine speed down verdict
1884c6fd2807SJeff Garzik  *	@dev: Device of interest
1885c6fd2807SJeff Garzik  *
1886c6fd2807SJeff Garzik  *	This function examines error ring of @dev and determines
18877d47e8d4STejun Heo  *	whether NCQ needs to be turned off, transfer speed should be
18887d47e8d4STejun Heo  *	stepped down, or falling back to PIO is necessary.
1889c6fd2807SJeff Garzik  *
18903884f7b0STejun Heo  *	ECAT_ATA_BUS	: ATA_BUS error for any command
1891c6fd2807SJeff Garzik  *
18923884f7b0STejun Heo  *	ECAT_TOUT_HSM	: TIMEOUT for any command or HSM violation for
18933884f7b0STejun Heo  *			  IO commands
18947d47e8d4STejun Heo  *
18953884f7b0STejun Heo  *	ECAT_UNK_DEV	: Unknown DEV error for IO commands
1896c6fd2807SJeff Garzik  *
189776326ac1STejun Heo  *	ECAT_DUBIOUS_*	: Identical to above three but occurred while
189876326ac1STejun Heo  *			  data transfer hasn't been verified.
189976326ac1STejun Heo  *
19003884f7b0STejun Heo  *	Verdicts are
19017d47e8d4STejun Heo  *
19023884f7b0STejun Heo  *	NCQ_OFF		: Turn off NCQ.
19037d47e8d4STejun Heo  *
19043884f7b0STejun Heo  *	SPEED_DOWN	: Speed down transfer speed but don't fall back
19053884f7b0STejun Heo  *			  to PIO.
19063884f7b0STejun Heo  *
19073884f7b0STejun Heo  *	FALLBACK_TO_PIO	: Fall back to PIO.
19083884f7b0STejun Heo  *
19093884f7b0STejun Heo  *	Even if multiple verdicts are returned, only one action is
191076326ac1STejun Heo  *	taken per error.  An action triggered by non-DUBIOUS errors
191176326ac1STejun Heo  *	clears ering, while one triggered by DUBIOUS_* errors doesn't.
191276326ac1STejun Heo  *	This is to expedite speed down decisions right after device is
191376326ac1STejun Heo  *	initially configured.
19143884f7b0STejun Heo  *
19154091fb95SMasahiro Yamada  *	The following are speed down rules.  #1 and #2 deal with
191676326ac1STejun Heo  *	DUBIOUS errors.
191776326ac1STejun Heo  *
191876326ac1STejun Heo  *	1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors
191976326ac1STejun Heo  *	   occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO.
192076326ac1STejun Heo  *
192176326ac1STejun Heo  *	2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors
192276326ac1STejun Heo  *	   occurred during last 5 mins, NCQ_OFF.
192376326ac1STejun Heo  *
192476326ac1STejun Heo  *	3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors
192525985edcSLucas De Marchi  *	   occurred during last 5 mins, FALLBACK_TO_PIO
19263884f7b0STejun Heo  *
192776326ac1STejun Heo  *	4. If more than 3 TOUT_HSM or UNK_DEV errors occurred
19283884f7b0STejun Heo  *	   during last 10 mins, NCQ_OFF.
19293884f7b0STejun Heo  *
193076326ac1STejun Heo  *	5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6
19313884f7b0STejun Heo  *	   UNK_DEV errors occurred during last 10 mins, SPEED_DOWN.
19327d47e8d4STejun Heo  *
1933c6fd2807SJeff Garzik  *	LOCKING:
1934c6fd2807SJeff Garzik  *	Inherited from caller.
1935c6fd2807SJeff Garzik  *
1936c6fd2807SJeff Garzik  *	RETURNS:
19377d47e8d4STejun Heo  *	OR of ATA_EH_SPDN_* flags.
1938c6fd2807SJeff Garzik  */
19397d47e8d4STejun Heo static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
1940c6fd2807SJeff Garzik {
19417d47e8d4STejun Heo 	const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
19427d47e8d4STejun Heo 	u64 j64 = get_jiffies_64();
19437d47e8d4STejun Heo 	struct speed_down_verdict_arg arg;
19447d47e8d4STejun Heo 	unsigned int verdict = 0;
1945c6fd2807SJeff Garzik 
19463884f7b0STejun Heo 	/* scan past 5 mins of error history */
19473884f7b0STejun Heo 	memset(&arg, 0, sizeof(arg));
19483884f7b0STejun Heo 	arg.since = j64 - min(j64, j5mins);
19493884f7b0STejun Heo 	ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
19503884f7b0STejun Heo 
195176326ac1STejun Heo 	if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] +
195276326ac1STejun Heo 	    arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1)
195376326ac1STejun Heo 		verdict |= ATA_EH_SPDN_SPEED_DOWN |
195476326ac1STejun Heo 			ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS;
195576326ac1STejun Heo 
195676326ac1STejun Heo 	if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] +
195776326ac1STejun Heo 	    arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1)
195876326ac1STejun Heo 		verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS;
195976326ac1STejun Heo 
19603884f7b0STejun Heo 	if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
19613884f7b0STejun Heo 	    arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1962663f99b8STejun Heo 	    arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
19633884f7b0STejun Heo 		verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
19643884f7b0STejun Heo 
19657d47e8d4STejun Heo 	/* scan past 10 mins of error history */
1966c6fd2807SJeff Garzik 	memset(&arg, 0, sizeof(arg));
19677d47e8d4STejun Heo 	arg.since = j64 - min(j64, j10mins);
19687d47e8d4STejun Heo 	ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1969c6fd2807SJeff Garzik 
19703884f7b0STejun Heo 	if (arg.nr_errors[ATA_ECAT_TOUT_HSM] +
19713884f7b0STejun Heo 	    arg.nr_errors[ATA_ECAT_UNK_DEV] > 3)
19727d47e8d4STejun Heo 		verdict |= ATA_EH_SPDN_NCQ_OFF;
19733884f7b0STejun Heo 
19743884f7b0STejun Heo 	if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
19753884f7b0STejun Heo 	    arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 ||
1976663f99b8STejun Heo 	    arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
19777d47e8d4STejun Heo 		verdict |= ATA_EH_SPDN_SPEED_DOWN;
1978c6fd2807SJeff Garzik 
19797d47e8d4STejun Heo 	return verdict;
1980c6fd2807SJeff Garzik }
1981c6fd2807SJeff Garzik 
1982c6fd2807SJeff Garzik /**
1983c6fd2807SJeff Garzik  *	ata_eh_speed_down - record error and speed down if necessary
1984c6fd2807SJeff Garzik  *	@dev: Failed device
19853884f7b0STejun Heo  *	@eflags: mask of ATA_EFLAG_* flags
1986c6fd2807SJeff Garzik  *	@err_mask: err_mask of the error
1987c6fd2807SJeff Garzik  *
1988c6fd2807SJeff Garzik  *	Record error and examine error history to determine whether
1989c6fd2807SJeff Garzik  *	adjusting transmission speed is necessary.  It also sets
1990c6fd2807SJeff Garzik  *	transmission limits appropriately if such adjustment is
1991c6fd2807SJeff Garzik  *	necessary.
1992c6fd2807SJeff Garzik  *
1993c6fd2807SJeff Garzik  *	LOCKING:
1994c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1995c6fd2807SJeff Garzik  *
1996c6fd2807SJeff Garzik  *	RETURNS:
19977d47e8d4STejun Heo  *	Determined recovery action.
1998c6fd2807SJeff Garzik  */
19993884f7b0STejun Heo static unsigned int ata_eh_speed_down(struct ata_device *dev,
20003884f7b0STejun Heo 				unsigned int eflags, unsigned int err_mask)
2001c6fd2807SJeff Garzik {
2002b1c72916STejun Heo 	struct ata_link *link = ata_dev_phys_link(dev);
200376326ac1STejun Heo 	int xfer_ok = 0;
20047d47e8d4STejun Heo 	unsigned int verdict;
20057d47e8d4STejun Heo 	unsigned int action = 0;
20067d47e8d4STejun Heo 
20077d47e8d4STejun Heo 	/* don't bother if Cat-0 error */
200876326ac1STejun Heo 	if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0)
2009c6fd2807SJeff Garzik 		return 0;
2010c6fd2807SJeff Garzik 
2011c6fd2807SJeff Garzik 	/* record error and determine whether speed down is necessary */
20123884f7b0STejun Heo 	ata_ering_record(&dev->ering, eflags, err_mask);
20137d47e8d4STejun Heo 	verdict = ata_eh_speed_down_verdict(dev);
2014c6fd2807SJeff Garzik 
20157d47e8d4STejun Heo 	/* turn off NCQ? */
20167d47e8d4STejun Heo 	if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
20177d47e8d4STejun Heo 	    (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
20187d47e8d4STejun Heo 			   ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
20197d47e8d4STejun Heo 		dev->flags |= ATA_DFLAG_NCQ_OFF;
2020a9a79dfeSJoe Perches 		ata_dev_warn(dev, "NCQ disabled due to excessive errors\n");
20217d47e8d4STejun Heo 		goto done;
20227d47e8d4STejun Heo 	}
2023c6fd2807SJeff Garzik 
20247d47e8d4STejun Heo 	/* speed down? */
20257d47e8d4STejun Heo 	if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
2026c6fd2807SJeff Garzik 		/* speed down SATA link speed if possible */
2027a07d499bSTejun Heo 		if (sata_down_spd_limit(link, 0) == 0) {
2028cf480626STejun Heo 			action |= ATA_EH_RESET;
20297d47e8d4STejun Heo 			goto done;
20307d47e8d4STejun Heo 		}
2031c6fd2807SJeff Garzik 
2032c6fd2807SJeff Garzik 		/* lower transfer mode */
20337d47e8d4STejun Heo 		if (dev->spdn_cnt < 2) {
20347d47e8d4STejun Heo 			static const int dma_dnxfer_sel[] =
20357d47e8d4STejun Heo 				{ ATA_DNXFER_DMA, ATA_DNXFER_40C };
20367d47e8d4STejun Heo 			static const int pio_dnxfer_sel[] =
20377d47e8d4STejun Heo 				{ ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
20387d47e8d4STejun Heo 			int sel;
2039c6fd2807SJeff Garzik 
20407d47e8d4STejun Heo 			if (dev->xfer_shift != ATA_SHIFT_PIO)
20417d47e8d4STejun Heo 				sel = dma_dnxfer_sel[dev->spdn_cnt];
20427d47e8d4STejun Heo 			else
20437d47e8d4STejun Heo 				sel = pio_dnxfer_sel[dev->spdn_cnt];
20447d47e8d4STejun Heo 
20457d47e8d4STejun Heo 			dev->spdn_cnt++;
20467d47e8d4STejun Heo 
20477d47e8d4STejun Heo 			if (ata_down_xfermask_limit(dev, sel) == 0) {
2048cf480626STejun Heo 				action |= ATA_EH_RESET;
20497d47e8d4STejun Heo 				goto done;
20507d47e8d4STejun Heo 			}
20517d47e8d4STejun Heo 		}
20527d47e8d4STejun Heo 	}
20537d47e8d4STejun Heo 
20547d47e8d4STejun Heo 	/* Fall back to PIO?  Slowing down to PIO is meaningless for
2055663f99b8STejun Heo 	 * SATA ATA devices.  Consider it only for PATA and SATAPI.
20567d47e8d4STejun Heo 	 */
20577d47e8d4STejun Heo 	if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
2058663f99b8STejun Heo 	    (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) &&
20597d47e8d4STejun Heo 	    (dev->xfer_shift != ATA_SHIFT_PIO)) {
20607d47e8d4STejun Heo 		if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
20617d47e8d4STejun Heo 			dev->spdn_cnt = 0;
2062cf480626STejun Heo 			action |= ATA_EH_RESET;
20637d47e8d4STejun Heo 			goto done;
20647d47e8d4STejun Heo 		}
20657d47e8d4STejun Heo 	}
20667d47e8d4STejun Heo 
2067c6fd2807SJeff Garzik 	return 0;
20687d47e8d4STejun Heo  done:
20697d47e8d4STejun Heo 	/* device has been slowed down, blow error history */
207076326ac1STejun Heo 	if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS))
20717d47e8d4STejun Heo 		ata_ering_clear(&dev->ering);
20727d47e8d4STejun Heo 	return action;
2073c6fd2807SJeff Garzik }
2074c6fd2807SJeff Garzik 
2075c6fd2807SJeff Garzik /**
20768d899e70SMark Lord  *	ata_eh_worth_retry - analyze error and decide whether to retry
20778d899e70SMark Lord  *	@qc: qc to possibly retry
20788d899e70SMark Lord  *
20798d899e70SMark Lord  *	Look at the cause of the error and decide if a retry
20808d899e70SMark Lord  * 	might be useful or not.  We don't want to retry media errors
20818d899e70SMark Lord  *	because the drive itself has probably already taken 10-30 seconds
20828d899e70SMark Lord  *	doing its own internal retries before reporting the failure.
20838d899e70SMark Lord  */
20848d899e70SMark Lord static inline int ata_eh_worth_retry(struct ata_queued_cmd *qc)
20858d899e70SMark Lord {
20861eaca39aSBian Yu 	if (qc->err_mask & AC_ERR_MEDIA)
20878d899e70SMark Lord 		return 0;	/* don't retry media errors */
20888d899e70SMark Lord 	if (qc->flags & ATA_QCFLAG_IO)
20898d899e70SMark Lord 		return 1;	/* otherwise retry anything from fs stack */
20908d899e70SMark Lord 	if (qc->err_mask & AC_ERR_INVALID)
20918d899e70SMark Lord 		return 0;	/* don't retry these */
20928d899e70SMark Lord 	return qc->err_mask != AC_ERR_DEV;  /* retry if not dev error */
20938d899e70SMark Lord }
20948d899e70SMark Lord 
20958d899e70SMark Lord /**
20967eb49509SDamien Le Moal  *      ata_eh_quiet - check if we need to be quiet about a command error
20977eb49509SDamien Le Moal  *      @qc: qc to check
20987eb49509SDamien Le Moal  *
20997eb49509SDamien Le Moal  *      Look at the qc flags anbd its scsi command request flags to determine
21007eb49509SDamien Le Moal  *      if we need to be quiet about the command failure.
21017eb49509SDamien Le Moal  */
21027eb49509SDamien Le Moal static inline bool ata_eh_quiet(struct ata_queued_cmd *qc)
21037eb49509SDamien Le Moal {
21047eb49509SDamien Le Moal 	if (qc->scsicmd &&
21057eb49509SDamien Le Moal 	    qc->scsicmd->request->rq_flags & RQF_QUIET)
21067eb49509SDamien Le Moal 		qc->flags |= ATA_QCFLAG_QUIET;
21077eb49509SDamien Le Moal 	return qc->flags & ATA_QCFLAG_QUIET;
21087eb49509SDamien Le Moal }
21097eb49509SDamien Le Moal 
21107eb49509SDamien Le Moal /**
21119b1e2658STejun Heo  *	ata_eh_link_autopsy - analyze error and determine recovery action
21129b1e2658STejun Heo  *	@link: host link to perform autopsy on
2113c6fd2807SJeff Garzik  *
21140260731fSTejun Heo  *	Analyze why @link failed and determine which recovery actions
21150260731fSTejun Heo  *	are needed.  This function also sets more detailed AC_ERR_*
21160260731fSTejun Heo  *	values and fills sense data for ATAPI CHECK SENSE.
2117c6fd2807SJeff Garzik  *
2118c6fd2807SJeff Garzik  *	LOCKING:
2119c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
2120c6fd2807SJeff Garzik  */
21219b1e2658STejun Heo static void ata_eh_link_autopsy(struct ata_link *link)
2122c6fd2807SJeff Garzik {
21230260731fSTejun Heo 	struct ata_port *ap = link->ap;
2124936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
2125258c4e5cSJens Axboe 	struct ata_queued_cmd *qc;
2126dfcc173dSTejun Heo 	struct ata_device *dev;
21273884f7b0STejun Heo 	unsigned int all_err_mask = 0, eflags = 0;
21287eb49509SDamien Le Moal 	int tag, nr_failed = 0, nr_quiet = 0;
2129c6fd2807SJeff Garzik 	u32 serror;
2130c6fd2807SJeff Garzik 	int rc;
2131c6fd2807SJeff Garzik 
2132c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
2133c6fd2807SJeff Garzik 
2134c6fd2807SJeff Garzik 	if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
2135c6fd2807SJeff Garzik 		return;
2136c6fd2807SJeff Garzik 
2137c6fd2807SJeff Garzik 	/* obtain and analyze SError */
2138936fd732STejun Heo 	rc = sata_scr_read(link, SCR_ERROR, &serror);
2139c6fd2807SJeff Garzik 	if (rc == 0) {
2140c6fd2807SJeff Garzik 		ehc->i.serror |= serror;
21410260731fSTejun Heo 		ata_eh_analyze_serror(link);
21424e57c517STejun Heo 	} else if (rc != -EOPNOTSUPP) {
2143cf480626STejun Heo 		/* SError read failed, force reset and probing */
2144b558edddSTejun Heo 		ehc->i.probe_mask |= ATA_ALL_DEVICES;
2145cf480626STejun Heo 		ehc->i.action |= ATA_EH_RESET;
21464e57c517STejun Heo 		ehc->i.err_mask |= AC_ERR_OTHER;
21474e57c517STejun Heo 	}
2148c6fd2807SJeff Garzik 
2149c6fd2807SJeff Garzik 	/* analyze NCQ failure */
21500260731fSTejun Heo 	ata_eh_analyze_ncq_error(link);
2151c6fd2807SJeff Garzik 
2152c6fd2807SJeff Garzik 	/* any real error trumps AC_ERR_OTHER */
2153c6fd2807SJeff Garzik 	if (ehc->i.err_mask & ~AC_ERR_OTHER)
2154c6fd2807SJeff Garzik 		ehc->i.err_mask &= ~AC_ERR_OTHER;
2155c6fd2807SJeff Garzik 
2156c6fd2807SJeff Garzik 	all_err_mask |= ehc->i.err_mask;
2157c6fd2807SJeff Garzik 
2158258c4e5cSJens Axboe 	ata_qc_for_each_raw(ap, qc, tag) {
2159b1c72916STejun Heo 		if (!(qc->flags & ATA_QCFLAG_FAILED) ||
2160b1c72916STejun Heo 		    ata_dev_phys_link(qc->dev) != link)
2161c6fd2807SJeff Garzik 			continue;
2162c6fd2807SJeff Garzik 
2163c6fd2807SJeff Garzik 		/* inherit upper level err_mask */
2164c6fd2807SJeff Garzik 		qc->err_mask |= ehc->i.err_mask;
2165c6fd2807SJeff Garzik 
2166c6fd2807SJeff Garzik 		/* analyze TF */
2167c6fd2807SJeff Garzik 		ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
2168c6fd2807SJeff Garzik 
2169c6fd2807SJeff Garzik 		/* DEV errors are probably spurious in case of ATA_BUS error */
2170c6fd2807SJeff Garzik 		if (qc->err_mask & AC_ERR_ATA_BUS)
2171c6fd2807SJeff Garzik 			qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
2172c6fd2807SJeff Garzik 					  AC_ERR_INVALID);
2173c6fd2807SJeff Garzik 
2174c6fd2807SJeff Garzik 		/* any real error trumps unknown error */
2175c6fd2807SJeff Garzik 		if (qc->err_mask & ~AC_ERR_OTHER)
2176c6fd2807SJeff Garzik 			qc->err_mask &= ~AC_ERR_OTHER;
2177c6fd2807SJeff Garzik 
2178804689adSDamien Le Moal 		/*
2179804689adSDamien Le Moal 		 * SENSE_VALID trumps dev/unknown error and revalidation. Upper
2180804689adSDamien Le Moal 		 * layers will determine whether the command is worth retrying
2181804689adSDamien Le Moal 		 * based on the sense data and device class/type. Otherwise,
2182804689adSDamien Le Moal 		 * determine directly if the command is worth retrying using its
2183804689adSDamien Le Moal 		 * error mask and flags.
2184804689adSDamien Le Moal 		 */
2185f90f0828STejun Heo 		if (qc->flags & ATA_QCFLAG_SENSE_VALID)
2186c6fd2807SJeff Garzik 			qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
2187804689adSDamien Le Moal 		else if (ata_eh_worth_retry(qc))
218803faab78STejun Heo 			qc->flags |= ATA_QCFLAG_RETRY;
218903faab78STejun Heo 
2190c6fd2807SJeff Garzik 		/* accumulate error info */
2191c6fd2807SJeff Garzik 		ehc->i.dev = qc->dev;
2192c6fd2807SJeff Garzik 		all_err_mask |= qc->err_mask;
2193c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_IO)
21943884f7b0STejun Heo 			eflags |= ATA_EFLAG_IS_IO;
2195255c03d1SHannes Reinecke 		trace_ata_eh_link_autopsy_qc(qc);
21967eb49509SDamien Le Moal 
21977eb49509SDamien Le Moal 		/* Count quiet errors */
21987eb49509SDamien Le Moal 		if (ata_eh_quiet(qc))
21997eb49509SDamien Le Moal 			nr_quiet++;
22007eb49509SDamien Le Moal 		nr_failed++;
2201c6fd2807SJeff Garzik 	}
2202c6fd2807SJeff Garzik 
22037eb49509SDamien Le Moal 	/* If all failed commands requested silence, then be quiet */
22047eb49509SDamien Le Moal 	if (nr_quiet == nr_failed)
22057eb49509SDamien Le Moal 		ehc->i.flags |= ATA_EHI_QUIET;
22067eb49509SDamien Le Moal 
2207c6fd2807SJeff Garzik 	/* enforce default EH actions */
2208c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN ||
2209c6fd2807SJeff Garzik 	    all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
2210cf480626STejun Heo 		ehc->i.action |= ATA_EH_RESET;
22113884f7b0STejun Heo 	else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) ||
22123884f7b0STejun Heo 		 (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV)))
2213c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_REVALIDATE;
2214c6fd2807SJeff Garzik 
2215dfcc173dSTejun Heo 	/* If we have offending qcs and the associated failed device,
2216dfcc173dSTejun Heo 	 * perform per-dev EH action only on the offending device.
2217dfcc173dSTejun Heo 	 */
2218c6fd2807SJeff Garzik 	if (ehc->i.dev) {
2219c6fd2807SJeff Garzik 		ehc->i.dev_action[ehc->i.dev->devno] |=
2220c6fd2807SJeff Garzik 			ehc->i.action & ATA_EH_PERDEV_MASK;
2221c6fd2807SJeff Garzik 		ehc->i.action &= ~ATA_EH_PERDEV_MASK;
2222c6fd2807SJeff Garzik 	}
2223c6fd2807SJeff Garzik 
22242695e366STejun Heo 	/* propagate timeout to host link */
22252695e366STejun Heo 	if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link))
22262695e366STejun Heo 		ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT;
22272695e366STejun Heo 
22282695e366STejun Heo 	/* record error and consider speeding down */
2229dfcc173dSTejun Heo 	dev = ehc->i.dev;
22302695e366STejun Heo 	if (!dev && ((ata_link_max_devices(link) == 1 &&
22312695e366STejun Heo 		      ata_dev_enabled(link->device))))
2232dfcc173dSTejun Heo 	    dev = link->device;
2233dfcc173dSTejun Heo 
223476326ac1STejun Heo 	if (dev) {
223576326ac1STejun Heo 		if (dev->flags & ATA_DFLAG_DUBIOUS_XFER)
223676326ac1STejun Heo 			eflags |= ATA_EFLAG_DUBIOUS_XFER;
22373884f7b0STejun Heo 		ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask);
2238255c03d1SHannes Reinecke 		trace_ata_eh_link_autopsy(dev, ehc->i.action, all_err_mask);
2239f1601113SRameshwar Prasad Sahu 	}
2240c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
2241c6fd2807SJeff Garzik }
2242c6fd2807SJeff Garzik 
2243c6fd2807SJeff Garzik /**
22449b1e2658STejun Heo  *	ata_eh_autopsy - analyze error and determine recovery action
22459b1e2658STejun Heo  *	@ap: host port to perform autopsy on
22469b1e2658STejun Heo  *
22479b1e2658STejun Heo  *	Analyze all links of @ap and determine why they failed and
22489b1e2658STejun Heo  *	which recovery actions are needed.
22499b1e2658STejun Heo  *
22509b1e2658STejun Heo  *	LOCKING:
22519b1e2658STejun Heo  *	Kernel thread context (may sleep).
22529b1e2658STejun Heo  */
2253fb7fd614STejun Heo void ata_eh_autopsy(struct ata_port *ap)
22549b1e2658STejun Heo {
22559b1e2658STejun Heo 	struct ata_link *link;
22569b1e2658STejun Heo 
22571eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE)
22589b1e2658STejun Heo 		ata_eh_link_autopsy(link);
22592695e366STejun Heo 
2260b1c72916STejun Heo 	/* Handle the frigging slave link.  Autopsy is done similarly
2261b1c72916STejun Heo 	 * but actions and flags are transferred over to the master
2262b1c72916STejun Heo 	 * link and handled from there.
2263b1c72916STejun Heo 	 */
2264b1c72916STejun Heo 	if (ap->slave_link) {
2265b1c72916STejun Heo 		struct ata_eh_context *mehc = &ap->link.eh_context;
2266b1c72916STejun Heo 		struct ata_eh_context *sehc = &ap->slave_link->eh_context;
2267b1c72916STejun Heo 
2268848e4c68STejun Heo 		/* transfer control flags from master to slave */
2269848e4c68STejun Heo 		sehc->i.flags |= mehc->i.flags & ATA_EHI_TO_SLAVE_MASK;
2270848e4c68STejun Heo 
2271848e4c68STejun Heo 		/* perform autopsy on the slave link */
2272b1c72916STejun Heo 		ata_eh_link_autopsy(ap->slave_link);
2273b1c72916STejun Heo 
2274848e4c68STejun Heo 		/* transfer actions from slave to master and clear slave */
2275b1c72916STejun Heo 		ata_eh_about_to_do(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
2276b1c72916STejun Heo 		mehc->i.action		|= sehc->i.action;
2277b1c72916STejun Heo 		mehc->i.dev_action[1]	|= sehc->i.dev_action[1];
2278b1c72916STejun Heo 		mehc->i.flags		|= sehc->i.flags;
2279b1c72916STejun Heo 		ata_eh_done(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
2280b1c72916STejun Heo 	}
2281b1c72916STejun Heo 
22822695e366STejun Heo 	/* Autopsy of fanout ports can affect host link autopsy.
22832695e366STejun Heo 	 * Perform host link autopsy last.
22842695e366STejun Heo 	 */
2285071f44b1STejun Heo 	if (sata_pmp_attached(ap))
22862695e366STejun Heo 		ata_eh_link_autopsy(&ap->link);
22879b1e2658STejun Heo }
22889b1e2658STejun Heo 
22899b1e2658STejun Heo /**
22906521148cSRobert Hancock  *	ata_get_cmd_descript - get description for ATA command
22916521148cSRobert Hancock  *	@command: ATA command code to get description for
22926521148cSRobert Hancock  *
22936521148cSRobert Hancock  *	Return a textual description of the given command, or NULL if the
22946521148cSRobert Hancock  *	command is not known.
22956521148cSRobert Hancock  *
22966521148cSRobert Hancock  *	LOCKING:
22976521148cSRobert Hancock  *	None
22986521148cSRobert Hancock  */
22996521148cSRobert Hancock const char *ata_get_cmd_descript(u8 command)
23006521148cSRobert Hancock {
23016521148cSRobert Hancock #ifdef CONFIG_ATA_VERBOSE_ERROR
23026521148cSRobert Hancock 	static const struct
23036521148cSRobert Hancock 	{
23046521148cSRobert Hancock 		u8 command;
23056521148cSRobert Hancock 		const char *text;
23066521148cSRobert Hancock 	} cmd_descr[] = {
23076521148cSRobert Hancock 		{ ATA_CMD_DEV_RESET,		"DEVICE RESET" },
23086521148cSRobert Hancock 		{ ATA_CMD_CHK_POWER,		"CHECK POWER MODE" },
23096521148cSRobert Hancock 		{ ATA_CMD_STANDBY,		"STANDBY" },
23106521148cSRobert Hancock 		{ ATA_CMD_IDLE,			"IDLE" },
23116521148cSRobert Hancock 		{ ATA_CMD_EDD,			"EXECUTE DEVICE DIAGNOSTIC" },
23126521148cSRobert Hancock 		{ ATA_CMD_DOWNLOAD_MICRO,	"DOWNLOAD MICROCODE" },
23133915c3b5SRobert Hancock 		{ ATA_CMD_DOWNLOAD_MICRO_DMA,	"DOWNLOAD MICROCODE DMA" },
23146521148cSRobert Hancock 		{ ATA_CMD_NOP,			"NOP" },
23156521148cSRobert Hancock 		{ ATA_CMD_FLUSH,		"FLUSH CACHE" },
23166521148cSRobert Hancock 		{ ATA_CMD_FLUSH_EXT,		"FLUSH CACHE EXT" },
23176521148cSRobert Hancock 		{ ATA_CMD_ID_ATA,		"IDENTIFY DEVICE" },
23186521148cSRobert Hancock 		{ ATA_CMD_ID_ATAPI,		"IDENTIFY PACKET DEVICE" },
23196521148cSRobert Hancock 		{ ATA_CMD_SERVICE,		"SERVICE" },
23206521148cSRobert Hancock 		{ ATA_CMD_READ,			"READ DMA" },
23216521148cSRobert Hancock 		{ ATA_CMD_READ_EXT,		"READ DMA EXT" },
23226521148cSRobert Hancock 		{ ATA_CMD_READ_QUEUED,		"READ DMA QUEUED" },
23236521148cSRobert Hancock 		{ ATA_CMD_READ_STREAM_EXT,	"READ STREAM EXT" },
23246521148cSRobert Hancock 		{ ATA_CMD_READ_STREAM_DMA_EXT,  "READ STREAM DMA EXT" },
23256521148cSRobert Hancock 		{ ATA_CMD_WRITE,		"WRITE DMA" },
23266521148cSRobert Hancock 		{ ATA_CMD_WRITE_EXT,		"WRITE DMA EXT" },
23276521148cSRobert Hancock 		{ ATA_CMD_WRITE_QUEUED,		"WRITE DMA QUEUED EXT" },
23286521148cSRobert Hancock 		{ ATA_CMD_WRITE_STREAM_EXT,	"WRITE STREAM EXT" },
23296521148cSRobert Hancock 		{ ATA_CMD_WRITE_STREAM_DMA_EXT, "WRITE STREAM DMA EXT" },
23306521148cSRobert Hancock 		{ ATA_CMD_WRITE_FUA_EXT,	"WRITE DMA FUA EXT" },
23316521148cSRobert Hancock 		{ ATA_CMD_WRITE_QUEUED_FUA_EXT, "WRITE DMA QUEUED FUA EXT" },
23326521148cSRobert Hancock 		{ ATA_CMD_FPDMA_READ,		"READ FPDMA QUEUED" },
23336521148cSRobert Hancock 		{ ATA_CMD_FPDMA_WRITE,		"WRITE FPDMA QUEUED" },
23343915c3b5SRobert Hancock 		{ ATA_CMD_FPDMA_SEND,		"SEND FPDMA QUEUED" },
23353915c3b5SRobert Hancock 		{ ATA_CMD_FPDMA_RECV,		"RECEIVE FPDMA QUEUED" },
23366521148cSRobert Hancock 		{ ATA_CMD_PIO_READ,		"READ SECTOR(S)" },
23376521148cSRobert Hancock 		{ ATA_CMD_PIO_READ_EXT,		"READ SECTOR(S) EXT" },
23386521148cSRobert Hancock 		{ ATA_CMD_PIO_WRITE,		"WRITE SECTOR(S)" },
23396521148cSRobert Hancock 		{ ATA_CMD_PIO_WRITE_EXT,	"WRITE SECTOR(S) EXT" },
23406521148cSRobert Hancock 		{ ATA_CMD_READ_MULTI,		"READ MULTIPLE" },
23416521148cSRobert Hancock 		{ ATA_CMD_READ_MULTI_EXT,	"READ MULTIPLE EXT" },
23426521148cSRobert Hancock 		{ ATA_CMD_WRITE_MULTI,		"WRITE MULTIPLE" },
23436521148cSRobert Hancock 		{ ATA_CMD_WRITE_MULTI_EXT,	"WRITE MULTIPLE EXT" },
23446521148cSRobert Hancock 		{ ATA_CMD_WRITE_MULTI_FUA_EXT,	"WRITE MULTIPLE FUA EXT" },
23456521148cSRobert Hancock 		{ ATA_CMD_SET_FEATURES,		"SET FEATURES" },
23466521148cSRobert Hancock 		{ ATA_CMD_SET_MULTI,		"SET MULTIPLE MODE" },
23476521148cSRobert Hancock 		{ ATA_CMD_VERIFY,		"READ VERIFY SECTOR(S)" },
23486521148cSRobert Hancock 		{ ATA_CMD_VERIFY_EXT,		"READ VERIFY SECTOR(S) EXT" },
23496521148cSRobert Hancock 		{ ATA_CMD_WRITE_UNCORR_EXT,	"WRITE UNCORRECTABLE EXT" },
23506521148cSRobert Hancock 		{ ATA_CMD_STANDBYNOW1,		"STANDBY IMMEDIATE" },
23516521148cSRobert Hancock 		{ ATA_CMD_IDLEIMMEDIATE,	"IDLE IMMEDIATE" },
23526521148cSRobert Hancock 		{ ATA_CMD_SLEEP,		"SLEEP" },
23536521148cSRobert Hancock 		{ ATA_CMD_INIT_DEV_PARAMS,	"INITIALIZE DEVICE PARAMETERS" },
23546521148cSRobert Hancock 		{ ATA_CMD_READ_NATIVE_MAX,	"READ NATIVE MAX ADDRESS" },
23556521148cSRobert Hancock 		{ ATA_CMD_READ_NATIVE_MAX_EXT,	"READ NATIVE MAX ADDRESS EXT" },
23566521148cSRobert Hancock 		{ ATA_CMD_SET_MAX,		"SET MAX ADDRESS" },
23576521148cSRobert Hancock 		{ ATA_CMD_SET_MAX_EXT,		"SET MAX ADDRESS EXT" },
23586521148cSRobert Hancock 		{ ATA_CMD_READ_LOG_EXT,		"READ LOG EXT" },
23596521148cSRobert Hancock 		{ ATA_CMD_WRITE_LOG_EXT,	"WRITE LOG EXT" },
23606521148cSRobert Hancock 		{ ATA_CMD_READ_LOG_DMA_EXT,	"READ LOG DMA EXT" },
23616521148cSRobert Hancock 		{ ATA_CMD_WRITE_LOG_DMA_EXT,	"WRITE LOG DMA EXT" },
23623915c3b5SRobert Hancock 		{ ATA_CMD_TRUSTED_NONDATA,	"TRUSTED NON-DATA" },
23636521148cSRobert Hancock 		{ ATA_CMD_TRUSTED_RCV,		"TRUSTED RECEIVE" },
23646521148cSRobert Hancock 		{ ATA_CMD_TRUSTED_RCV_DMA,	"TRUSTED RECEIVE DMA" },
23656521148cSRobert Hancock 		{ ATA_CMD_TRUSTED_SND,		"TRUSTED SEND" },
23666521148cSRobert Hancock 		{ ATA_CMD_TRUSTED_SND_DMA,	"TRUSTED SEND DMA" },
23676521148cSRobert Hancock 		{ ATA_CMD_PMP_READ,		"READ BUFFER" },
23683915c3b5SRobert Hancock 		{ ATA_CMD_PMP_READ_DMA,		"READ BUFFER DMA" },
23696521148cSRobert Hancock 		{ ATA_CMD_PMP_WRITE,		"WRITE BUFFER" },
23703915c3b5SRobert Hancock 		{ ATA_CMD_PMP_WRITE_DMA,	"WRITE BUFFER DMA" },
23716521148cSRobert Hancock 		{ ATA_CMD_CONF_OVERLAY,		"DEVICE CONFIGURATION OVERLAY" },
23726521148cSRobert Hancock 		{ ATA_CMD_SEC_SET_PASS,		"SECURITY SET PASSWORD" },
23736521148cSRobert Hancock 		{ ATA_CMD_SEC_UNLOCK,		"SECURITY UNLOCK" },
23746521148cSRobert Hancock 		{ ATA_CMD_SEC_ERASE_PREP,	"SECURITY ERASE PREPARE" },
23756521148cSRobert Hancock 		{ ATA_CMD_SEC_ERASE_UNIT,	"SECURITY ERASE UNIT" },
23766521148cSRobert Hancock 		{ ATA_CMD_SEC_FREEZE_LOCK,	"SECURITY FREEZE LOCK" },
23776521148cSRobert Hancock 		{ ATA_CMD_SEC_DISABLE_PASS,	"SECURITY DISABLE PASSWORD" },
23786521148cSRobert Hancock 		{ ATA_CMD_CONFIG_STREAM,	"CONFIGURE STREAM" },
23796521148cSRobert Hancock 		{ ATA_CMD_SMART,		"SMART" },
23806521148cSRobert Hancock 		{ ATA_CMD_MEDIA_LOCK,		"DOOR LOCK" },
23816521148cSRobert Hancock 		{ ATA_CMD_MEDIA_UNLOCK,		"DOOR UNLOCK" },
2382acad7627SFUJITA Tomonori 		{ ATA_CMD_DSM,			"DATA SET MANAGEMENT" },
23836521148cSRobert Hancock 		{ ATA_CMD_CHK_MED_CRD_TYP,	"CHECK MEDIA CARD TYPE" },
23846521148cSRobert Hancock 		{ ATA_CMD_CFA_REQ_EXT_ERR,	"CFA REQUEST EXTENDED ERROR" },
23856521148cSRobert Hancock 		{ ATA_CMD_CFA_WRITE_NE,		"CFA WRITE SECTORS WITHOUT ERASE" },
23866521148cSRobert Hancock 		{ ATA_CMD_CFA_TRANS_SECT,	"CFA TRANSLATE SECTOR" },
23876521148cSRobert Hancock 		{ ATA_CMD_CFA_ERASE,		"CFA ERASE SECTORS" },
23886521148cSRobert Hancock 		{ ATA_CMD_CFA_WRITE_MULT_NE,	"CFA WRITE MULTIPLE WITHOUT ERASE" },
23893915c3b5SRobert Hancock 		{ ATA_CMD_REQ_SENSE_DATA,	"REQUEST SENSE DATA EXT" },
23903915c3b5SRobert Hancock 		{ ATA_CMD_SANITIZE_DEVICE,	"SANITIZE DEVICE" },
239128a3fc22SHannes Reinecke 		{ ATA_CMD_ZAC_MGMT_IN,		"ZAC MANAGEMENT IN" },
239227708a95SHannes Reinecke 		{ ATA_CMD_ZAC_MGMT_OUT,		"ZAC MANAGEMENT OUT" },
23936521148cSRobert Hancock 		{ ATA_CMD_READ_LONG,		"READ LONG (with retries)" },
23946521148cSRobert Hancock 		{ ATA_CMD_READ_LONG_ONCE,	"READ LONG (without retries)" },
23956521148cSRobert Hancock 		{ ATA_CMD_WRITE_LONG,		"WRITE LONG (with retries)" },
23966521148cSRobert Hancock 		{ ATA_CMD_WRITE_LONG_ONCE,	"WRITE LONG (without retries)" },
23976521148cSRobert Hancock 		{ ATA_CMD_RESTORE,		"RECALIBRATE" },
23986521148cSRobert Hancock 		{ 0,				NULL } /* terminate list */
23996521148cSRobert Hancock 	};
24006521148cSRobert Hancock 
24016521148cSRobert Hancock 	unsigned int i;
24026521148cSRobert Hancock 	for (i = 0; cmd_descr[i].text; i++)
24036521148cSRobert Hancock 		if (cmd_descr[i].command == command)
24046521148cSRobert Hancock 			return cmd_descr[i].text;
24056521148cSRobert Hancock #endif
24066521148cSRobert Hancock 
24076521148cSRobert Hancock 	return NULL;
24086521148cSRobert Hancock }
240936aae28eSAndy Shevchenko EXPORT_SYMBOL_GPL(ata_get_cmd_descript);
24106521148cSRobert Hancock 
24116521148cSRobert Hancock /**
24129b1e2658STejun Heo  *	ata_eh_link_report - report error handling to user
24130260731fSTejun Heo  *	@link: ATA link EH is going on
2414c6fd2807SJeff Garzik  *
2415c6fd2807SJeff Garzik  *	Report EH to user.
2416c6fd2807SJeff Garzik  *
2417c6fd2807SJeff Garzik  *	LOCKING:
2418c6fd2807SJeff Garzik  *	None.
2419c6fd2807SJeff Garzik  */
24209b1e2658STejun Heo static void ata_eh_link_report(struct ata_link *link)
2421c6fd2807SJeff Garzik {
24220260731fSTejun Heo 	struct ata_port *ap = link->ap;
24230260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
2424258c4e5cSJens Axboe 	struct ata_queued_cmd *qc;
2425c6fd2807SJeff Garzik 	const char *frozen, *desc;
2426462098b0SLevente Kurusa 	char tries_buf[6] = "";
2427c6fd2807SJeff Garzik 	int tag, nr_failed = 0;
2428c6fd2807SJeff Garzik 
242994ff3d54STejun Heo 	if (ehc->i.flags & ATA_EHI_QUIET)
243094ff3d54STejun Heo 		return;
243194ff3d54STejun Heo 
2432c6fd2807SJeff Garzik 	desc = NULL;
2433c6fd2807SJeff Garzik 	if (ehc->i.desc[0] != '\0')
2434c6fd2807SJeff Garzik 		desc = ehc->i.desc;
2435c6fd2807SJeff Garzik 
2436258c4e5cSJens Axboe 	ata_qc_for_each_raw(ap, qc, tag) {
2437b1c72916STejun Heo 		if (!(qc->flags & ATA_QCFLAG_FAILED) ||
2438b1c72916STejun Heo 		    ata_dev_phys_link(qc->dev) != link ||
2439e027bd36STejun Heo 		    ((qc->flags & ATA_QCFLAG_QUIET) &&
2440e027bd36STejun Heo 		     qc->err_mask == AC_ERR_DEV))
2441c6fd2807SJeff Garzik 			continue;
2442c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
2443c6fd2807SJeff Garzik 			continue;
2444c6fd2807SJeff Garzik 
2445c6fd2807SJeff Garzik 		nr_failed++;
2446c6fd2807SJeff Garzik 	}
2447c6fd2807SJeff Garzik 
2448c6fd2807SJeff Garzik 	if (!nr_failed && !ehc->i.err_mask)
2449c6fd2807SJeff Garzik 		return;
2450c6fd2807SJeff Garzik 
2451c6fd2807SJeff Garzik 	frozen = "";
2452c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN)
2453c6fd2807SJeff Garzik 		frozen = " frozen";
2454c6fd2807SJeff Garzik 
2455a1e10f7eSTejun Heo 	if (ap->eh_tries < ATA_EH_MAX_TRIES)
2456462098b0SLevente Kurusa 		snprintf(tries_buf, sizeof(tries_buf), " t%d",
2457a1e10f7eSTejun Heo 			 ap->eh_tries);
2458a1e10f7eSTejun Heo 
2459c6fd2807SJeff Garzik 	if (ehc->i.dev) {
2460a9a79dfeSJoe Perches 		ata_dev_err(ehc->i.dev, "exception Emask 0x%x "
2461a1e10f7eSTejun Heo 			    "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2462a1e10f7eSTejun Heo 			    ehc->i.err_mask, link->sactive, ehc->i.serror,
2463a1e10f7eSTejun Heo 			    ehc->i.action, frozen, tries_buf);
2464c6fd2807SJeff Garzik 		if (desc)
2465a9a79dfeSJoe Perches 			ata_dev_err(ehc->i.dev, "%s\n", desc);
2466c6fd2807SJeff Garzik 	} else {
2467a9a79dfeSJoe Perches 		ata_link_err(link, "exception Emask 0x%x "
2468a1e10f7eSTejun Heo 			     "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2469a1e10f7eSTejun Heo 			     ehc->i.err_mask, link->sactive, ehc->i.serror,
2470a1e10f7eSTejun Heo 			     ehc->i.action, frozen, tries_buf);
2471c6fd2807SJeff Garzik 		if (desc)
2472a9a79dfeSJoe Perches 			ata_link_err(link, "%s\n", desc);
2473c6fd2807SJeff Garzik 	}
2474c6fd2807SJeff Garzik 
24756521148cSRobert Hancock #ifdef CONFIG_ATA_VERBOSE_ERROR
24761333e194SRobert Hancock 	if (ehc->i.serror)
2477a9a79dfeSJoe Perches 		ata_link_err(link,
24781333e194SRobert Hancock 		  "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
24791333e194SRobert Hancock 		  ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
24801333e194SRobert Hancock 		  ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
24811333e194SRobert Hancock 		  ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
24821333e194SRobert Hancock 		  ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
24831333e194SRobert Hancock 		  ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
24841333e194SRobert Hancock 		  ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
24851333e194SRobert Hancock 		  ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
24861333e194SRobert Hancock 		  ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
24871333e194SRobert Hancock 		  ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
24881333e194SRobert Hancock 		  ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
24891333e194SRobert Hancock 		  ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
24901333e194SRobert Hancock 		  ehc->i.serror & SERR_CRC ? "BadCRC " : "",
24911333e194SRobert Hancock 		  ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
24921333e194SRobert Hancock 		  ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
24931333e194SRobert Hancock 		  ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
24941333e194SRobert Hancock 		  ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
24951333e194SRobert Hancock 		  ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
24966521148cSRobert Hancock #endif
24971333e194SRobert Hancock 
2498258c4e5cSJens Axboe 	ata_qc_for_each_raw(ap, qc, tag) {
24998a937581STejun Heo 		struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
2500abb6a889STejun Heo 		char data_buf[20] = "";
2501abb6a889STejun Heo 		char cdb_buf[70] = "";
2502c6fd2807SJeff Garzik 
25030260731fSTejun Heo 		if (!(qc->flags & ATA_QCFLAG_FAILED) ||
2504b1c72916STejun Heo 		    ata_dev_phys_link(qc->dev) != link || !qc->err_mask)
2505c6fd2807SJeff Garzik 			continue;
2506c6fd2807SJeff Garzik 
2507abb6a889STejun Heo 		if (qc->dma_dir != DMA_NONE) {
2508abb6a889STejun Heo 			static const char *dma_str[] = {
2509abb6a889STejun Heo 				[DMA_BIDIRECTIONAL]	= "bidi",
2510abb6a889STejun Heo 				[DMA_TO_DEVICE]		= "out",
2511abb6a889STejun Heo 				[DMA_FROM_DEVICE]	= "in",
2512abb6a889STejun Heo 			};
2513fb1b8b11SGeert Uytterhoeven 			const char *prot_str = NULL;
2514abb6a889STejun Heo 
2515fb1b8b11SGeert Uytterhoeven 			switch (qc->tf.protocol) {
2516fb1b8b11SGeert Uytterhoeven 			case ATA_PROT_UNKNOWN:
2517fb1b8b11SGeert Uytterhoeven 				prot_str = "unknown";
2518fb1b8b11SGeert Uytterhoeven 				break;
2519fb1b8b11SGeert Uytterhoeven 			case ATA_PROT_NODATA:
2520fb1b8b11SGeert Uytterhoeven 				prot_str = "nodata";
2521fb1b8b11SGeert Uytterhoeven 				break;
2522fb1b8b11SGeert Uytterhoeven 			case ATA_PROT_PIO:
2523fb1b8b11SGeert Uytterhoeven 				prot_str = "pio";
2524fb1b8b11SGeert Uytterhoeven 				break;
2525fb1b8b11SGeert Uytterhoeven 			case ATA_PROT_DMA:
2526fb1b8b11SGeert Uytterhoeven 				prot_str = "dma";
2527fb1b8b11SGeert Uytterhoeven 				break;
2528fb1b8b11SGeert Uytterhoeven 			case ATA_PROT_NCQ:
2529fb1b8b11SGeert Uytterhoeven 				prot_str = "ncq dma";
2530fb1b8b11SGeert Uytterhoeven 				break;
2531fb1b8b11SGeert Uytterhoeven 			case ATA_PROT_NCQ_NODATA:
2532fb1b8b11SGeert Uytterhoeven 				prot_str = "ncq nodata";
2533fb1b8b11SGeert Uytterhoeven 				break;
2534fb1b8b11SGeert Uytterhoeven 			case ATAPI_PROT_NODATA:
2535fb1b8b11SGeert Uytterhoeven 				prot_str = "nodata";
2536fb1b8b11SGeert Uytterhoeven 				break;
2537fb1b8b11SGeert Uytterhoeven 			case ATAPI_PROT_PIO:
2538fb1b8b11SGeert Uytterhoeven 				prot_str = "pio";
2539fb1b8b11SGeert Uytterhoeven 				break;
2540fb1b8b11SGeert Uytterhoeven 			case ATAPI_PROT_DMA:
2541fb1b8b11SGeert Uytterhoeven 				prot_str = "dma";
2542fb1b8b11SGeert Uytterhoeven 				break;
2543fb1b8b11SGeert Uytterhoeven 			}
2544abb6a889STejun Heo 			snprintf(data_buf, sizeof(data_buf), " %s %u %s",
2545fb1b8b11SGeert Uytterhoeven 				 prot_str, qc->nbytes, dma_str[qc->dma_dir]);
2546abb6a889STejun Heo 		}
2547abb6a889STejun Heo 
25486521148cSRobert Hancock 		if (ata_is_atapi(qc->tf.protocol)) {
2549a13b0c9dSHannes Reinecke 			const u8 *cdb = qc->cdb;
2550a13b0c9dSHannes Reinecke 			size_t cdb_len = qc->dev->cdb_len;
2551a13b0c9dSHannes Reinecke 
2552cbba5b0eSHannes Reinecke 			if (qc->scsicmd) {
2553cbba5b0eSHannes Reinecke 				cdb = qc->scsicmd->cmnd;
2554cbba5b0eSHannes Reinecke 				cdb_len = qc->scsicmd->cmd_len;
2555cbba5b0eSHannes Reinecke 			}
2556cbba5b0eSHannes Reinecke 			__scsi_format_command(cdb_buf, sizeof(cdb_buf),
2557cbba5b0eSHannes Reinecke 					      cdb, cdb_len);
25586521148cSRobert Hancock 		} else {
25596521148cSRobert Hancock 			const char *descr = ata_get_cmd_descript(cmd->command);
25606521148cSRobert Hancock 			if (descr)
2561a9a79dfeSJoe Perches 				ata_dev_err(qc->dev, "failed command: %s\n",
2562a9a79dfeSJoe Perches 					    descr);
25636521148cSRobert Hancock 		}
2564abb6a889STejun Heo 
2565a9a79dfeSJoe Perches 		ata_dev_err(qc->dev,
25668a937581STejun Heo 			"cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
2567abb6a889STejun Heo 			"tag %d%s\n         %s"
25688a937581STejun Heo 			"res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
25695335b729STejun Heo 			"Emask 0x%x (%s)%s\n",
25708a937581STejun Heo 			cmd->command, cmd->feature, cmd->nsect,
25718a937581STejun Heo 			cmd->lbal, cmd->lbam, cmd->lbah,
25728a937581STejun Heo 			cmd->hob_feature, cmd->hob_nsect,
25738a937581STejun Heo 			cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
2574abb6a889STejun Heo 			cmd->device, qc->tag, data_buf, cdb_buf,
25758a937581STejun Heo 			res->command, res->feature, res->nsect,
25768a937581STejun Heo 			res->lbal, res->lbam, res->lbah,
25778a937581STejun Heo 			res->hob_feature, res->hob_nsect,
25788a937581STejun Heo 			res->hob_lbal, res->hob_lbam, res->hob_lbah,
25795335b729STejun Heo 			res->device, qc->err_mask, ata_err_string(qc->err_mask),
25805335b729STejun Heo 			qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
25811333e194SRobert Hancock 
25826521148cSRobert Hancock #ifdef CONFIG_ATA_VERBOSE_ERROR
25831333e194SRobert Hancock 		if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
2584e87fd28cSHannes Reinecke 				    ATA_SENSE | ATA_ERR)) {
25851333e194SRobert Hancock 			if (res->command & ATA_BUSY)
2586a9a79dfeSJoe Perches 				ata_dev_err(qc->dev, "status: { Busy }\n");
25871333e194SRobert Hancock 			else
2588e87fd28cSHannes Reinecke 				ata_dev_err(qc->dev, "status: { %s%s%s%s%s}\n",
25891333e194SRobert Hancock 				  res->command & ATA_DRDY ? "DRDY " : "",
25901333e194SRobert Hancock 				  res->command & ATA_DF ? "DF " : "",
25911333e194SRobert Hancock 				  res->command & ATA_DRQ ? "DRQ " : "",
2592e87fd28cSHannes Reinecke 				  res->command & ATA_SENSE ? "SENSE " : "",
25931333e194SRobert Hancock 				  res->command & ATA_ERR ? "ERR " : "");
25941333e194SRobert Hancock 		}
25951333e194SRobert Hancock 
25961333e194SRobert Hancock 		if (cmd->command != ATA_CMD_PACKET &&
2597eec7e1c1SAlexey Asemov 		    (res->feature & (ATA_ICRC | ATA_UNC | ATA_AMNF |
2598eec7e1c1SAlexey Asemov 				     ATA_IDNF | ATA_ABORTED)))
2599eec7e1c1SAlexey Asemov 			ata_dev_err(qc->dev, "error: { %s%s%s%s%s}\n",
26001333e194SRobert Hancock 			  res->feature & ATA_ICRC ? "ICRC " : "",
26011333e194SRobert Hancock 			  res->feature & ATA_UNC ? "UNC " : "",
2602eec7e1c1SAlexey Asemov 			  res->feature & ATA_AMNF ? "AMNF " : "",
26031333e194SRobert Hancock 			  res->feature & ATA_IDNF ? "IDNF " : "",
26041333e194SRobert Hancock 			  res->feature & ATA_ABORTED ? "ABRT " : "");
26056521148cSRobert Hancock #endif
2606c6fd2807SJeff Garzik 	}
2607c6fd2807SJeff Garzik }
2608c6fd2807SJeff Garzik 
26099b1e2658STejun Heo /**
26109b1e2658STejun Heo  *	ata_eh_report - report error handling to user
26119b1e2658STejun Heo  *	@ap: ATA port to report EH about
26129b1e2658STejun Heo  *
26139b1e2658STejun Heo  *	Report EH to user.
26149b1e2658STejun Heo  *
26159b1e2658STejun Heo  *	LOCKING:
26169b1e2658STejun Heo  *	None.
26179b1e2658STejun Heo  */
2618fb7fd614STejun Heo void ata_eh_report(struct ata_port *ap)
26199b1e2658STejun Heo {
26209b1e2658STejun Heo 	struct ata_link *link;
26219b1e2658STejun Heo 
26221eca4365STejun Heo 	ata_for_each_link(link, ap, HOST_FIRST)
26239b1e2658STejun Heo 		ata_eh_link_report(link);
26249b1e2658STejun Heo }
26259b1e2658STejun Heo 
2626cc0680a5STejun Heo static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
2627b1c72916STejun Heo 			unsigned int *classes, unsigned long deadline,
2628b1c72916STejun Heo 			bool clear_classes)
2629c6fd2807SJeff Garzik {
2630f58229f8STejun Heo 	struct ata_device *dev;
2631c6fd2807SJeff Garzik 
2632b1c72916STejun Heo 	if (clear_classes)
26331eca4365STejun Heo 		ata_for_each_dev(dev, link, ALL)
2634f58229f8STejun Heo 			classes[dev->devno] = ATA_DEV_UNKNOWN;
2635c6fd2807SJeff Garzik 
2636f046519fSTejun Heo 	return reset(link, classes, deadline);
2637c6fd2807SJeff Garzik }
2638c6fd2807SJeff Garzik 
2639e8411fbaSSergei Shtylyov static int ata_eh_followup_srst_needed(struct ata_link *link, int rc)
2640c6fd2807SJeff Garzik {
264145db2f6cSTejun Heo 	if ((link->flags & ATA_LFLAG_NO_SRST) || ata_link_offline(link))
2642ae791c05STejun Heo 		return 0;
26435dbfc9cbSTejun Heo 	if (rc == -EAGAIN)
2644c6fd2807SJeff Garzik 		return 1;
2645071f44b1STejun Heo 	if (sata_pmp_supported(link->ap) && ata_is_host_link(link))
26463495de73STejun Heo 		return 1;
2647c6fd2807SJeff Garzik 	return 0;
2648c6fd2807SJeff Garzik }
2649c6fd2807SJeff Garzik 
2650fb7fd614STejun Heo int ata_eh_reset(struct ata_link *link, int classify,
2651c6fd2807SJeff Garzik 		 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
2652c6fd2807SJeff Garzik 		 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
2653c6fd2807SJeff Garzik {
2654afaa5c37STejun Heo 	struct ata_port *ap = link->ap;
2655b1c72916STejun Heo 	struct ata_link *slave = ap->slave_link;
2656936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
2657705d2014SBartlomiej Zolnierkiewicz 	struct ata_eh_context *sehc = slave ? &slave->eh_context : NULL;
2658c6fd2807SJeff Garzik 	unsigned int *classes = ehc->classes;
2659416dc9edSTejun Heo 	unsigned int lflags = link->flags;
2660c6fd2807SJeff Garzik 	int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
2661d8af0eb6STejun Heo 	int max_tries = 0, try = 0;
2662b1c72916STejun Heo 	struct ata_link *failed_link;
2663f58229f8STejun Heo 	struct ata_device *dev;
2664416dc9edSTejun Heo 	unsigned long deadline, now;
2665c6fd2807SJeff Garzik 	ata_reset_fn_t reset;
2666afaa5c37STejun Heo 	unsigned long flags;
2667416dc9edSTejun Heo 	u32 sstatus;
2668b1c72916STejun Heo 	int nr_unknown, rc;
2669c6fd2807SJeff Garzik 
2670932648b0STejun Heo 	/*
2671932648b0STejun Heo 	 * Prepare to reset
2672932648b0STejun Heo 	 */
2673d8af0eb6STejun Heo 	while (ata_eh_reset_timeouts[max_tries] != ULONG_MAX)
2674d8af0eb6STejun Heo 		max_tries++;
2675ca6d43b0SDan Williams 	if (link->flags & ATA_LFLAG_RST_ONCE)
2676ca6d43b0SDan Williams 		max_tries = 1;
267705944bdfSTejun Heo 	if (link->flags & ATA_LFLAG_NO_HRST)
267805944bdfSTejun Heo 		hardreset = NULL;
267905944bdfSTejun Heo 	if (link->flags & ATA_LFLAG_NO_SRST)
268005944bdfSTejun Heo 		softreset = NULL;
2681d8af0eb6STejun Heo 
268225985edcSLucas De Marchi 	/* make sure each reset attempt is at least COOL_DOWN apart */
268319b72321STejun Heo 	if (ehc->i.flags & ATA_EHI_DID_RESET) {
26840a2c0f56STejun Heo 		now = jiffies;
268519b72321STejun Heo 		WARN_ON(time_after(ehc->last_reset, now));
268619b72321STejun Heo 		deadline = ata_deadline(ehc->last_reset,
268719b72321STejun Heo 					ATA_EH_RESET_COOL_DOWN);
26880a2c0f56STejun Heo 		if (time_before(now, deadline))
26890a2c0f56STejun Heo 			schedule_timeout_uninterruptible(deadline - now);
269019b72321STejun Heo 	}
26910a2c0f56STejun Heo 
2692afaa5c37STejun Heo 	spin_lock_irqsave(ap->lock, flags);
2693afaa5c37STejun Heo 	ap->pflags |= ATA_PFLAG_RESETTING;
2694afaa5c37STejun Heo 	spin_unlock_irqrestore(ap->lock, flags);
2695afaa5c37STejun Heo 
2696cf480626STejun Heo 	ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
2697c6fd2807SJeff Garzik 
26981eca4365STejun Heo 	ata_for_each_dev(dev, link, ALL) {
2699cdeab114STejun Heo 		/* If we issue an SRST then an ATA drive (not ATAPI)
2700cdeab114STejun Heo 		 * may change configuration and be in PIO0 timing. If
2701cdeab114STejun Heo 		 * we do a hard reset (or are coming from power on)
2702cdeab114STejun Heo 		 * this is true for ATA or ATAPI. Until we've set a
2703cdeab114STejun Heo 		 * suitable controller mode we should not touch the
2704cdeab114STejun Heo 		 * bus as we may be talking too fast.
2705cdeab114STejun Heo 		 */
2706cdeab114STejun Heo 		dev->pio_mode = XFER_PIO_0;
27075416912aSAaron Lu 		dev->dma_mode = 0xff;
2708cdeab114STejun Heo 
2709cdeab114STejun Heo 		/* If the controller has a pio mode setup function
2710cdeab114STejun Heo 		 * then use it to set the chipset to rights. Don't
2711cdeab114STejun Heo 		 * touch the DMA setup as that will be dealt with when
2712cdeab114STejun Heo 		 * configuring devices.
2713cdeab114STejun Heo 		 */
2714cdeab114STejun Heo 		if (ap->ops->set_piomode)
2715cdeab114STejun Heo 			ap->ops->set_piomode(ap, dev);
2716cdeab114STejun Heo 	}
2717cdeab114STejun Heo 
2718cf480626STejun Heo 	/* prefer hardreset */
2719932648b0STejun Heo 	reset = NULL;
2720cf480626STejun Heo 	ehc->i.action &= ~ATA_EH_RESET;
2721cf480626STejun Heo 	if (hardreset) {
2722cf480626STejun Heo 		reset = hardreset;
2723a674050eSTejun Heo 		ehc->i.action |= ATA_EH_HARDRESET;
27244f7faa3fSTejun Heo 	} else if (softreset) {
2725cf480626STejun Heo 		reset = softreset;
2726a674050eSTejun Heo 		ehc->i.action |= ATA_EH_SOFTRESET;
2727cf480626STejun Heo 	}
2728c6fd2807SJeff Garzik 
2729c6fd2807SJeff Garzik 	if (prereset) {
2730b1c72916STejun Heo 		unsigned long deadline = ata_deadline(jiffies,
2731b1c72916STejun Heo 						      ATA_EH_PRERESET_TIMEOUT);
2732b1c72916STejun Heo 
2733b1c72916STejun Heo 		if (slave) {
2734b1c72916STejun Heo 			sehc->i.action &= ~ATA_EH_RESET;
2735b1c72916STejun Heo 			sehc->i.action |= ehc->i.action;
2736b1c72916STejun Heo 		}
2737b1c72916STejun Heo 
2738b1c72916STejun Heo 		rc = prereset(link, deadline);
2739b1c72916STejun Heo 
2740b1c72916STejun Heo 		/* If present, do prereset on slave link too.  Reset
2741b1c72916STejun Heo 		 * is skipped iff both master and slave links report
2742b1c72916STejun Heo 		 * -ENOENT or clear ATA_EH_RESET.
2743b1c72916STejun Heo 		 */
2744b1c72916STejun Heo 		if (slave && (rc == 0 || rc == -ENOENT)) {
2745b1c72916STejun Heo 			int tmp;
2746b1c72916STejun Heo 
2747b1c72916STejun Heo 			tmp = prereset(slave, deadline);
2748b1c72916STejun Heo 			if (tmp != -ENOENT)
2749b1c72916STejun Heo 				rc = tmp;
2750b1c72916STejun Heo 
2751b1c72916STejun Heo 			ehc->i.action |= sehc->i.action;
2752b1c72916STejun Heo 		}
2753b1c72916STejun Heo 
2754c6fd2807SJeff Garzik 		if (rc) {
2755c961922bSAlan Cox 			if (rc == -ENOENT) {
2756a9a79dfeSJoe Perches 				ata_link_dbg(link, "port disabled--ignoring\n");
2757cf480626STejun Heo 				ehc->i.action &= ~ATA_EH_RESET;
27584aa9ab67STejun Heo 
27591eca4365STejun Heo 				ata_for_each_dev(dev, link, ALL)
2760f58229f8STejun Heo 					classes[dev->devno] = ATA_DEV_NONE;
27614aa9ab67STejun Heo 
27624aa9ab67STejun Heo 				rc = 0;
2763c961922bSAlan Cox 			} else
2764a9a79dfeSJoe Perches 				ata_link_err(link,
2765a9a79dfeSJoe Perches 					     "prereset failed (errno=%d)\n",
2766a9a79dfeSJoe Perches 					     rc);
2767fccb6ea5STejun Heo 			goto out;
2768c6fd2807SJeff Garzik 		}
2769c6fd2807SJeff Garzik 
2770932648b0STejun Heo 		/* prereset() might have cleared ATA_EH_RESET.  If so,
2771d6515e6fSTejun Heo 		 * bang classes, thaw and return.
2772932648b0STejun Heo 		 */
2773932648b0STejun Heo 		if (reset && !(ehc->i.action & ATA_EH_RESET)) {
27741eca4365STejun Heo 			ata_for_each_dev(dev, link, ALL)
2775f58229f8STejun Heo 				classes[dev->devno] = ATA_DEV_NONE;
2776d6515e6fSTejun Heo 			if ((ap->pflags & ATA_PFLAG_FROZEN) &&
2777d6515e6fSTejun Heo 			    ata_is_host_link(link))
2778d6515e6fSTejun Heo 				ata_eh_thaw_port(ap);
2779fccb6ea5STejun Heo 			rc = 0;
2780fccb6ea5STejun Heo 			goto out;
2781c6fd2807SJeff Garzik 		}
2782932648b0STejun Heo 	}
2783c6fd2807SJeff Garzik 
2784c6fd2807SJeff Garzik  retry:
2785932648b0STejun Heo 	/*
2786932648b0STejun Heo 	 * Perform reset
2787932648b0STejun Heo 	 */
2788dc98c32cSTejun Heo 	if (ata_is_host_link(link))
2789dc98c32cSTejun Heo 		ata_eh_freeze_port(ap);
2790dc98c32cSTejun Heo 
2791341c2c95STejun Heo 	deadline = ata_deadline(jiffies, ata_eh_reset_timeouts[try++]);
279231daabdaSTejun Heo 
2793932648b0STejun Heo 	if (reset) {
2794c6fd2807SJeff Garzik 		if (verbose)
2795a9a79dfeSJoe Perches 			ata_link_info(link, "%s resetting link\n",
2796c6fd2807SJeff Garzik 				      reset == softreset ? "soft" : "hard");
2797c6fd2807SJeff Garzik 
2798c6fd2807SJeff Garzik 		/* mark that this EH session started with reset */
279919b72321STejun Heo 		ehc->last_reset = jiffies;
28000d64a233STejun Heo 		if (reset == hardreset)
28010d64a233STejun Heo 			ehc->i.flags |= ATA_EHI_DID_HARDRESET;
28020d64a233STejun Heo 		else
28030d64a233STejun Heo 			ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
2804c6fd2807SJeff Garzik 
2805b1c72916STejun Heo 		rc = ata_do_reset(link, reset, classes, deadline, true);
2806b1c72916STejun Heo 		if (rc && rc != -EAGAIN) {
2807b1c72916STejun Heo 			failed_link = link;
28085dbfc9cbSTejun Heo 			goto fail;
2809b1c72916STejun Heo 		}
2810c6fd2807SJeff Garzik 
2811b1c72916STejun Heo 		/* hardreset slave link if existent */
2812b1c72916STejun Heo 		if (slave && reset == hardreset) {
2813b1c72916STejun Heo 			int tmp;
2814b1c72916STejun Heo 
2815b1c72916STejun Heo 			if (verbose)
2816a9a79dfeSJoe Perches 				ata_link_info(slave, "hard resetting link\n");
2817b1c72916STejun Heo 
2818b1c72916STejun Heo 			ata_eh_about_to_do(slave, NULL, ATA_EH_RESET);
2819b1c72916STejun Heo 			tmp = ata_do_reset(slave, reset, classes, deadline,
2820b1c72916STejun Heo 					   false);
2821b1c72916STejun Heo 			switch (tmp) {
2822b1c72916STejun Heo 			case -EAGAIN:
2823b1c72916STejun Heo 				rc = -EAGAIN;
2824b1c72916STejun Heo 			case 0:
2825b1c72916STejun Heo 				break;
2826b1c72916STejun Heo 			default:
2827b1c72916STejun Heo 				failed_link = slave;
2828b1c72916STejun Heo 				rc = tmp;
2829b1c72916STejun Heo 				goto fail;
2830b1c72916STejun Heo 			}
2831b1c72916STejun Heo 		}
2832b1c72916STejun Heo 
2833b1c72916STejun Heo 		/* perform follow-up SRST if necessary */
2834c6fd2807SJeff Garzik 		if (reset == hardreset &&
2835e8411fbaSSergei Shtylyov 		    ata_eh_followup_srst_needed(link, rc)) {
2836c6fd2807SJeff Garzik 			reset = softreset;
2837c6fd2807SJeff Garzik 
2838c6fd2807SJeff Garzik 			if (!reset) {
2839a9a79dfeSJoe Perches 				ata_link_err(link,
2840a9a79dfeSJoe Perches 	     "follow-up softreset required but no softreset available\n");
2841b1c72916STejun Heo 				failed_link = link;
2842fccb6ea5STejun Heo 				rc = -EINVAL;
284308cf69d0STejun Heo 				goto fail;
2844c6fd2807SJeff Garzik 			}
2845c6fd2807SJeff Garzik 
2846cf480626STejun Heo 			ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
2847b1c72916STejun Heo 			rc = ata_do_reset(link, reset, classes, deadline, true);
2848fe2c4d01STejun Heo 			if (rc) {
2849fe2c4d01STejun Heo 				failed_link = link;
2850fe2c4d01STejun Heo 				goto fail;
2851fe2c4d01STejun Heo 			}
2852c6fd2807SJeff Garzik 		}
2853932648b0STejun Heo 	} else {
2854932648b0STejun Heo 		if (verbose)
2855a9a79dfeSJoe Perches 			ata_link_info(link,
2856a9a79dfeSJoe Perches 	"no reset method available, skipping reset\n");
2857932648b0STejun Heo 		if (!(lflags & ATA_LFLAG_ASSUME_CLASS))
2858932648b0STejun Heo 			lflags |= ATA_LFLAG_ASSUME_ATA;
2859932648b0STejun Heo 	}
2860008a7896STejun Heo 
2861932648b0STejun Heo 	/*
2862932648b0STejun Heo 	 * Post-reset processing
2863932648b0STejun Heo 	 */
28641eca4365STejun Heo 	ata_for_each_dev(dev, link, ALL) {
2865416dc9edSTejun Heo 		/* After the reset, the device state is PIO 0 and the
2866416dc9edSTejun Heo 		 * controller state is undefined.  Reset also wakes up
2867416dc9edSTejun Heo 		 * drives from sleeping mode.
2868c6fd2807SJeff Garzik 		 */
2869f58229f8STejun Heo 		dev->pio_mode = XFER_PIO_0;
2870054a5fbaSTejun Heo 		dev->flags &= ~ATA_DFLAG_SLEEPING;
2871c6fd2807SJeff Garzik 
28723b761d3dSTejun Heo 		if (ata_phys_link_offline(ata_dev_phys_link(dev)))
28733b761d3dSTejun Heo 			continue;
28743b761d3dSTejun Heo 
28754ccd3329STejun Heo 		/* apply class override */
2876416dc9edSTejun Heo 		if (lflags & ATA_LFLAG_ASSUME_ATA)
2877ae791c05STejun Heo 			classes[dev->devno] = ATA_DEV_ATA;
2878416dc9edSTejun Heo 		else if (lflags & ATA_LFLAG_ASSUME_SEMB)
2879816ab897STejun Heo 			classes[dev->devno] = ATA_DEV_SEMB_UNSUP;
2880ae791c05STejun Heo 	}
2881ae791c05STejun Heo 
2882008a7896STejun Heo 	/* record current link speed */
2883936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2884936fd732STejun Heo 		link->sata_spd = (sstatus >> 4) & 0xf;
2885b1c72916STejun Heo 	if (slave && sata_scr_read(slave, SCR_STATUS, &sstatus) == 0)
2886b1c72916STejun Heo 		slave->sata_spd = (sstatus >> 4) & 0xf;
2887008a7896STejun Heo 
2888dc98c32cSTejun Heo 	/* thaw the port */
2889dc98c32cSTejun Heo 	if (ata_is_host_link(link))
2890dc98c32cSTejun Heo 		ata_eh_thaw_port(ap);
2891dc98c32cSTejun Heo 
2892f046519fSTejun Heo 	/* postreset() should clear hardware SError.  Although SError
2893f046519fSTejun Heo 	 * is cleared during link resume, clearing SError here is
2894f046519fSTejun Heo 	 * necessary as some PHYs raise hotplug events after SRST.
2895f046519fSTejun Heo 	 * This introduces race condition where hotplug occurs between
2896f046519fSTejun Heo 	 * reset and here.  This race is mediated by cross checking
2897f046519fSTejun Heo 	 * link onlineness and classification result later.
2898f046519fSTejun Heo 	 */
2899b1c72916STejun Heo 	if (postreset) {
2900cc0680a5STejun Heo 		postreset(link, classes);
2901b1c72916STejun Heo 		if (slave)
2902b1c72916STejun Heo 			postreset(slave, classes);
2903b1c72916STejun Heo 	}
2904c6fd2807SJeff Garzik 
29051e641060STejun Heo 	/*
29068c56caccSTejun Heo 	 * Some controllers can't be frozen very well and may set spurious
29078c56caccSTejun Heo 	 * error conditions during reset.  Clear accumulated error
29088c56caccSTejun Heo 	 * information and re-thaw the port if frozen.  As reset is the
29098c56caccSTejun Heo 	 * final recovery action and we cross check link onlineness against
29108c56caccSTejun Heo 	 * device classification later, no hotplug event is lost by this.
29111e641060STejun Heo 	 */
2912f046519fSTejun Heo 	spin_lock_irqsave(link->ap->lock, flags);
29131e641060STejun Heo 	memset(&link->eh_info, 0, sizeof(link->eh_info));
2914b1c72916STejun Heo 	if (slave)
29151e641060STejun Heo 		memset(&slave->eh_info, 0, sizeof(link->eh_info));
29161e641060STejun Heo 	ap->pflags &= ~ATA_PFLAG_EH_PENDING;
2917f046519fSTejun Heo 	spin_unlock_irqrestore(link->ap->lock, flags);
2918f046519fSTejun Heo 
29198c56caccSTejun Heo 	if (ap->pflags & ATA_PFLAG_FROZEN)
29208c56caccSTejun Heo 		ata_eh_thaw_port(ap);
29218c56caccSTejun Heo 
29223b761d3dSTejun Heo 	/*
29233b761d3dSTejun Heo 	 * Make sure onlineness and classification result correspond.
2924f046519fSTejun Heo 	 * Hotplug could have happened during reset and some
2925f046519fSTejun Heo 	 * controllers fail to wait while a drive is spinning up after
2926f046519fSTejun Heo 	 * being hotplugged causing misdetection.  By cross checking
29273b761d3dSTejun Heo 	 * link on/offlineness and classification result, those
29283b761d3dSTejun Heo 	 * conditions can be reliably detected and retried.
2929f046519fSTejun Heo 	 */
2930b1c72916STejun Heo 	nr_unknown = 0;
29311eca4365STejun Heo 	ata_for_each_dev(dev, link, ALL) {
29323b761d3dSTejun Heo 		if (ata_phys_link_online(ata_dev_phys_link(dev))) {
2933b1c72916STejun Heo 			if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
2934a9a79dfeSJoe Perches 				ata_dev_dbg(dev, "link online but device misclassified\n");
2935f046519fSTejun Heo 				classes[dev->devno] = ATA_DEV_NONE;
2936b1c72916STejun Heo 				nr_unknown++;
2937b1c72916STejun Heo 			}
29383b761d3dSTejun Heo 		} else if (ata_phys_link_offline(ata_dev_phys_link(dev))) {
29393b761d3dSTejun Heo 			if (ata_class_enabled(classes[dev->devno]))
2940a9a79dfeSJoe Perches 				ata_dev_dbg(dev,
2941a9a79dfeSJoe Perches 					    "link offline, clearing class %d to NONE\n",
29423b761d3dSTejun Heo 					    classes[dev->devno]);
29433b761d3dSTejun Heo 			classes[dev->devno] = ATA_DEV_NONE;
29443b761d3dSTejun Heo 		} else if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
2945a9a79dfeSJoe Perches 			ata_dev_dbg(dev,
2946a9a79dfeSJoe Perches 				    "link status unknown, clearing UNKNOWN to NONE\n");
29473b761d3dSTejun Heo 			classes[dev->devno] = ATA_DEV_NONE;
29483b761d3dSTejun Heo 		}
2949f046519fSTejun Heo 	}
2950f046519fSTejun Heo 
2951b1c72916STejun Heo 	if (classify && nr_unknown) {
2952f046519fSTejun Heo 		if (try < max_tries) {
2953a9a79dfeSJoe Perches 			ata_link_warn(link,
2954a9a79dfeSJoe Perches 				      "link online but %d devices misclassified, retrying\n",
29553b761d3dSTejun Heo 				      nr_unknown);
2956b1c72916STejun Heo 			failed_link = link;
2957f046519fSTejun Heo 			rc = -EAGAIN;
2958f046519fSTejun Heo 			goto fail;
2959f046519fSTejun Heo 		}
2960a9a79dfeSJoe Perches 		ata_link_warn(link,
29613b761d3dSTejun Heo 			      "link online but %d devices misclassified, "
29623b761d3dSTejun Heo 			      "device detection might fail\n", nr_unknown);
2963f046519fSTejun Heo 	}
2964f046519fSTejun Heo 
2965c6fd2807SJeff Garzik 	/* reset successful, schedule revalidation */
2966cf480626STejun Heo 	ata_eh_done(link, NULL, ATA_EH_RESET);
2967b1c72916STejun Heo 	if (slave)
2968b1c72916STejun Heo 		ata_eh_done(slave, NULL, ATA_EH_RESET);
296919b72321STejun Heo 	ehc->last_reset = jiffies;		/* update to completion time */
2970c6fd2807SJeff Garzik 	ehc->i.action |= ATA_EH_REVALIDATE;
29716b7ae954STejun Heo 	link->lpm_policy = ATA_LPM_UNKNOWN;	/* reset LPM state */
2972416dc9edSTejun Heo 
2973416dc9edSTejun Heo 	rc = 0;
2974fccb6ea5STejun Heo  out:
2975fccb6ea5STejun Heo 	/* clear hotplug flag */
2976fccb6ea5STejun Heo 	ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2977b1c72916STejun Heo 	if (slave)
2978b1c72916STejun Heo 		sehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2979afaa5c37STejun Heo 
2980afaa5c37STejun Heo 	spin_lock_irqsave(ap->lock, flags);
2981afaa5c37STejun Heo 	ap->pflags &= ~ATA_PFLAG_RESETTING;
2982afaa5c37STejun Heo 	spin_unlock_irqrestore(ap->lock, flags);
2983afaa5c37STejun Heo 
2984c6fd2807SJeff Garzik 	return rc;
2985416dc9edSTejun Heo 
2986416dc9edSTejun Heo  fail:
29875958e302STejun Heo 	/* if SCR isn't accessible on a fan-out port, PMP needs to be reset */
29885958e302STejun Heo 	if (!ata_is_host_link(link) &&
29895958e302STejun Heo 	    sata_scr_read(link, SCR_STATUS, &sstatus))
29905958e302STejun Heo 		rc = -ERESTART;
29915958e302STejun Heo 
29927a46c078SGwendal Grignou 	if (try >= max_tries) {
29938ea7645cSTejun Heo 		/*
29948ea7645cSTejun Heo 		 * Thaw host port even if reset failed, so that the port
29958ea7645cSTejun Heo 		 * can be retried on the next phy event.  This risks
29968ea7645cSTejun Heo 		 * repeated EH runs but seems to be a better tradeoff than
29978ea7645cSTejun Heo 		 * shutting down a port after a botched hotplug attempt.
29988ea7645cSTejun Heo 		 */
29998ea7645cSTejun Heo 		if (ata_is_host_link(link))
30008ea7645cSTejun Heo 			ata_eh_thaw_port(ap);
3001416dc9edSTejun Heo 		goto out;
30028ea7645cSTejun Heo 	}
3003416dc9edSTejun Heo 
3004416dc9edSTejun Heo 	now = jiffies;
3005416dc9edSTejun Heo 	if (time_before(now, deadline)) {
3006416dc9edSTejun Heo 		unsigned long delta = deadline - now;
3007416dc9edSTejun Heo 
3008a9a79dfeSJoe Perches 		ata_link_warn(failed_link,
30090a2c0f56STejun Heo 			"reset failed (errno=%d), retrying in %u secs\n",
30100a2c0f56STejun Heo 			rc, DIV_ROUND_UP(jiffies_to_msecs(delta), 1000));
3011416dc9edSTejun Heo 
3012c0c362b6STejun Heo 		ata_eh_release(ap);
3013416dc9edSTejun Heo 		while (delta)
3014416dc9edSTejun Heo 			delta = schedule_timeout_uninterruptible(delta);
3015c0c362b6STejun Heo 		ata_eh_acquire(ap);
3016416dc9edSTejun Heo 	}
3017416dc9edSTejun Heo 
30187a46c078SGwendal Grignou 	/*
30197a46c078SGwendal Grignou 	 * While disks spinup behind PMP, some controllers fail sending SRST.
30207a46c078SGwendal Grignou 	 * They need to be reset - as well as the PMP - before retrying.
30217a46c078SGwendal Grignou 	 */
30227a46c078SGwendal Grignou 	if (rc == -ERESTART) {
30237a46c078SGwendal Grignou 		if (ata_is_host_link(link))
30247a46c078SGwendal Grignou 			ata_eh_thaw_port(ap);
30257a46c078SGwendal Grignou 		goto out;
30267a46c078SGwendal Grignou 	}
30277a46c078SGwendal Grignou 
3028b1c72916STejun Heo 	if (try == max_tries - 1) {
3029a07d499bSTejun Heo 		sata_down_spd_limit(link, 0);
3030b1c72916STejun Heo 		if (slave)
3031a07d499bSTejun Heo 			sata_down_spd_limit(slave, 0);
3032b1c72916STejun Heo 	} else if (rc == -EPIPE)
3033a07d499bSTejun Heo 		sata_down_spd_limit(failed_link, 0);
3034b1c72916STejun Heo 
3035416dc9edSTejun Heo 	if (hardreset)
3036416dc9edSTejun Heo 		reset = hardreset;
3037416dc9edSTejun Heo 	goto retry;
3038c6fd2807SJeff Garzik }
3039c6fd2807SJeff Garzik 
304045fabbb7SElias Oltmanns static inline void ata_eh_pull_park_action(struct ata_port *ap)
304145fabbb7SElias Oltmanns {
304245fabbb7SElias Oltmanns 	struct ata_link *link;
304345fabbb7SElias Oltmanns 	struct ata_device *dev;
304445fabbb7SElias Oltmanns 	unsigned long flags;
304545fabbb7SElias Oltmanns 
304645fabbb7SElias Oltmanns 	/*
304745fabbb7SElias Oltmanns 	 * This function can be thought of as an extended version of
304845fabbb7SElias Oltmanns 	 * ata_eh_about_to_do() specially crafted to accommodate the
304945fabbb7SElias Oltmanns 	 * requirements of ATA_EH_PARK handling. Since the EH thread
305045fabbb7SElias Oltmanns 	 * does not leave the do {} while () loop in ata_eh_recover as
305145fabbb7SElias Oltmanns 	 * long as the timeout for a park request to *one* device on
305245fabbb7SElias Oltmanns 	 * the port has not expired, and since we still want to pick
305345fabbb7SElias Oltmanns 	 * up park requests to other devices on the same port or
305445fabbb7SElias Oltmanns 	 * timeout updates for the same device, we have to pull
305545fabbb7SElias Oltmanns 	 * ATA_EH_PARK actions from eh_info into eh_context.i
305645fabbb7SElias Oltmanns 	 * ourselves at the beginning of each pass over the loop.
305745fabbb7SElias Oltmanns 	 *
305845fabbb7SElias Oltmanns 	 * Additionally, all write accesses to &ap->park_req_pending
305916735d02SWolfram Sang 	 * through reinit_completion() (see below) or complete_all()
306045fabbb7SElias Oltmanns 	 * (see ata_scsi_park_store()) are protected by the host lock.
306145fabbb7SElias Oltmanns 	 * As a result we have that park_req_pending.done is zero on
306245fabbb7SElias Oltmanns 	 * exit from this function, i.e. when ATA_EH_PARK actions for
306345fabbb7SElias Oltmanns 	 * *all* devices on port ap have been pulled into the
306445fabbb7SElias Oltmanns 	 * respective eh_context structs. If, and only if,
306545fabbb7SElias Oltmanns 	 * park_req_pending.done is non-zero by the time we reach
306645fabbb7SElias Oltmanns 	 * wait_for_completion_timeout(), another ATA_EH_PARK action
306745fabbb7SElias Oltmanns 	 * has been scheduled for at least one of the devices on port
306845fabbb7SElias Oltmanns 	 * ap and we have to cycle over the do {} while () loop in
306945fabbb7SElias Oltmanns 	 * ata_eh_recover() again.
307045fabbb7SElias Oltmanns 	 */
307145fabbb7SElias Oltmanns 
307245fabbb7SElias Oltmanns 	spin_lock_irqsave(ap->lock, flags);
307316735d02SWolfram Sang 	reinit_completion(&ap->park_req_pending);
30741eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE) {
30751eca4365STejun Heo 		ata_for_each_dev(dev, link, ALL) {
307645fabbb7SElias Oltmanns 			struct ata_eh_info *ehi = &link->eh_info;
307745fabbb7SElias Oltmanns 
307845fabbb7SElias Oltmanns 			link->eh_context.i.dev_action[dev->devno] |=
307945fabbb7SElias Oltmanns 				ehi->dev_action[dev->devno] & ATA_EH_PARK;
308045fabbb7SElias Oltmanns 			ata_eh_clear_action(link, dev, ehi, ATA_EH_PARK);
308145fabbb7SElias Oltmanns 		}
308245fabbb7SElias Oltmanns 	}
308345fabbb7SElias Oltmanns 	spin_unlock_irqrestore(ap->lock, flags);
308445fabbb7SElias Oltmanns }
308545fabbb7SElias Oltmanns 
308645fabbb7SElias Oltmanns static void ata_eh_park_issue_cmd(struct ata_device *dev, int park)
308745fabbb7SElias Oltmanns {
308845fabbb7SElias Oltmanns 	struct ata_eh_context *ehc = &dev->link->eh_context;
308945fabbb7SElias Oltmanns 	struct ata_taskfile tf;
309045fabbb7SElias Oltmanns 	unsigned int err_mask;
309145fabbb7SElias Oltmanns 
309245fabbb7SElias Oltmanns 	ata_tf_init(dev, &tf);
309345fabbb7SElias Oltmanns 	if (park) {
309445fabbb7SElias Oltmanns 		ehc->unloaded_mask |= 1 << dev->devno;
309545fabbb7SElias Oltmanns 		tf.command = ATA_CMD_IDLEIMMEDIATE;
309645fabbb7SElias Oltmanns 		tf.feature = 0x44;
309745fabbb7SElias Oltmanns 		tf.lbal = 0x4c;
309845fabbb7SElias Oltmanns 		tf.lbam = 0x4e;
309945fabbb7SElias Oltmanns 		tf.lbah = 0x55;
310045fabbb7SElias Oltmanns 	} else {
310145fabbb7SElias Oltmanns 		ehc->unloaded_mask &= ~(1 << dev->devno);
310245fabbb7SElias Oltmanns 		tf.command = ATA_CMD_CHK_POWER;
310345fabbb7SElias Oltmanns 	}
310445fabbb7SElias Oltmanns 
310545fabbb7SElias Oltmanns 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
3106bd18bc04SHannes Reinecke 	tf.protocol = ATA_PROT_NODATA;
310745fabbb7SElias Oltmanns 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
310845fabbb7SElias Oltmanns 	if (park && (err_mask || tf.lbal != 0xc4)) {
3109a9a79dfeSJoe Perches 		ata_dev_err(dev, "head unload failed!\n");
311045fabbb7SElias Oltmanns 		ehc->unloaded_mask &= ~(1 << dev->devno);
311145fabbb7SElias Oltmanns 	}
311245fabbb7SElias Oltmanns }
311345fabbb7SElias Oltmanns 
31140260731fSTejun Heo static int ata_eh_revalidate_and_attach(struct ata_link *link,
3115c6fd2807SJeff Garzik 					struct ata_device **r_failed_dev)
3116c6fd2807SJeff Garzik {
31170260731fSTejun Heo 	struct ata_port *ap = link->ap;
31180260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
3119c6fd2807SJeff Garzik 	struct ata_device *dev;
31208c3c52a8STejun Heo 	unsigned int new_mask = 0;
3121c6fd2807SJeff Garzik 	unsigned long flags;
3122f58229f8STejun Heo 	int rc = 0;
3123c6fd2807SJeff Garzik 
3124c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
3125c6fd2807SJeff Garzik 
31268c3c52a8STejun Heo 	/* For PATA drive side cable detection to work, IDENTIFY must
31278c3c52a8STejun Heo 	 * be done backwards such that PDIAG- is released by the slave
31288c3c52a8STejun Heo 	 * device before the master device is identified.
31298c3c52a8STejun Heo 	 */
31301eca4365STejun Heo 	ata_for_each_dev(dev, link, ALL_REVERSE) {
3131f58229f8STejun Heo 		unsigned int action = ata_eh_dev_action(dev);
3132f58229f8STejun Heo 		unsigned int readid_flags = 0;
3133c6fd2807SJeff Garzik 
3134bff04647STejun Heo 		if (ehc->i.flags & ATA_EHI_DID_RESET)
3135bff04647STejun Heo 			readid_flags |= ATA_READID_POSTRESET;
3136bff04647STejun Heo 
31379666f400STejun Heo 		if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
3138633273a3STejun Heo 			WARN_ON(dev->class == ATA_DEV_PMP);
3139633273a3STejun Heo 
3140b1c72916STejun Heo 			if (ata_phys_link_offline(ata_dev_phys_link(dev))) {
3141c6fd2807SJeff Garzik 				rc = -EIO;
31428c3c52a8STejun Heo 				goto err;
3143c6fd2807SJeff Garzik 			}
3144c6fd2807SJeff Garzik 
31450260731fSTejun Heo 			ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
3146422c9daaSTejun Heo 			rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
3147422c9daaSTejun Heo 						readid_flags);
3148c6fd2807SJeff Garzik 			if (rc)
31498c3c52a8STejun Heo 				goto err;
3150c6fd2807SJeff Garzik 
31510260731fSTejun Heo 			ata_eh_done(link, dev, ATA_EH_REVALIDATE);
3152c6fd2807SJeff Garzik 
3153baa1e78aSTejun Heo 			/* Configuration may have changed, reconfigure
3154baa1e78aSTejun Heo 			 * transfer mode.
3155baa1e78aSTejun Heo 			 */
3156baa1e78aSTejun Heo 			ehc->i.flags |= ATA_EHI_SETMODE;
3157baa1e78aSTejun Heo 
3158c6fd2807SJeff Garzik 			/* schedule the scsi_rescan_device() here */
3159ad72cf98STejun Heo 			schedule_work(&(ap->scsi_rescan_task));
3160c6fd2807SJeff Garzik 		} else if (dev->class == ATA_DEV_UNKNOWN &&
3161c6fd2807SJeff Garzik 			   ehc->tries[dev->devno] &&
3162c6fd2807SJeff Garzik 			   ata_class_enabled(ehc->classes[dev->devno])) {
3163842faa6cSTejun Heo 			/* Temporarily set dev->class, it will be
3164842faa6cSTejun Heo 			 * permanently set once all configurations are
3165842faa6cSTejun Heo 			 * complete.  This is necessary because new
3166842faa6cSTejun Heo 			 * device configuration is done in two
3167842faa6cSTejun Heo 			 * separate loops.
3168842faa6cSTejun Heo 			 */
3169c6fd2807SJeff Garzik 			dev->class = ehc->classes[dev->devno];
3170c6fd2807SJeff Garzik 
3171633273a3STejun Heo 			if (dev->class == ATA_DEV_PMP)
3172633273a3STejun Heo 				rc = sata_pmp_attach(dev);
3173633273a3STejun Heo 			else
3174633273a3STejun Heo 				rc = ata_dev_read_id(dev, &dev->class,
3175633273a3STejun Heo 						     readid_flags, dev->id);
3176842faa6cSTejun Heo 
3177842faa6cSTejun Heo 			/* read_id might have changed class, store and reset */
3178842faa6cSTejun Heo 			ehc->classes[dev->devno] = dev->class;
3179842faa6cSTejun Heo 			dev->class = ATA_DEV_UNKNOWN;
3180842faa6cSTejun Heo 
31818c3c52a8STejun Heo 			switch (rc) {
31828c3c52a8STejun Heo 			case 0:
318399cf610aSTejun Heo 				/* clear error info accumulated during probe */
318499cf610aSTejun Heo 				ata_ering_clear(&dev->ering);
3185f58229f8STejun Heo 				new_mask |= 1 << dev->devno;
31868c3c52a8STejun Heo 				break;
31878c3c52a8STejun Heo 			case -ENOENT:
318855a8e2c8STejun Heo 				/* IDENTIFY was issued to non-existent
318955a8e2c8STejun Heo 				 * device.  No need to reset.  Just
3190842faa6cSTejun Heo 				 * thaw and ignore the device.
319155a8e2c8STejun Heo 				 */
319255a8e2c8STejun Heo 				ata_eh_thaw_port(ap);
3193c6fd2807SJeff Garzik 				break;
31948c3c52a8STejun Heo 			default:
31958c3c52a8STejun Heo 				goto err;
31968c3c52a8STejun Heo 			}
31978c3c52a8STejun Heo 		}
3198c6fd2807SJeff Garzik 	}
3199c6fd2807SJeff Garzik 
3200c1c4e8d5STejun Heo 	/* PDIAG- should have been released, ask cable type if post-reset */
320133267325STejun Heo 	if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) {
320233267325STejun Heo 		if (ap->ops->cable_detect)
3203c1c4e8d5STejun Heo 			ap->cbl = ap->ops->cable_detect(ap);
320433267325STejun Heo 		ata_force_cbl(ap);
320533267325STejun Heo 	}
3206c1c4e8d5STejun Heo 
32078c3c52a8STejun Heo 	/* Configure new devices forward such that user doesn't see
32088c3c52a8STejun Heo 	 * device detection messages backwards.
32098c3c52a8STejun Heo 	 */
32101eca4365STejun Heo 	ata_for_each_dev(dev, link, ALL) {
32114f7c2874STejun Heo 		if (!(new_mask & (1 << dev->devno)))
32128c3c52a8STejun Heo 			continue;
32138c3c52a8STejun Heo 
3214842faa6cSTejun Heo 		dev->class = ehc->classes[dev->devno];
3215842faa6cSTejun Heo 
32164f7c2874STejun Heo 		if (dev->class == ATA_DEV_PMP)
32174f7c2874STejun Heo 			continue;
32184f7c2874STejun Heo 
32198c3c52a8STejun Heo 		ehc->i.flags |= ATA_EHI_PRINTINFO;
32208c3c52a8STejun Heo 		rc = ata_dev_configure(dev);
32218c3c52a8STejun Heo 		ehc->i.flags &= ~ATA_EHI_PRINTINFO;
3222842faa6cSTejun Heo 		if (rc) {
3223842faa6cSTejun Heo 			dev->class = ATA_DEV_UNKNOWN;
32248c3c52a8STejun Heo 			goto err;
3225842faa6cSTejun Heo 		}
32268c3c52a8STejun Heo 
3227c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
3228c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
3229c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
3230baa1e78aSTejun Heo 
323155a8e2c8STejun Heo 		/* new device discovered, configure xfermode */
3232baa1e78aSTejun Heo 		ehc->i.flags |= ATA_EHI_SETMODE;
3233c6fd2807SJeff Garzik 	}
3234c6fd2807SJeff Garzik 
32358c3c52a8STejun Heo 	return 0;
32368c3c52a8STejun Heo 
32378c3c52a8STejun Heo  err:
3238c6fd2807SJeff Garzik 	*r_failed_dev = dev;
32398c3c52a8STejun Heo 	DPRINTK("EXIT rc=%d\n", rc);
3240c6fd2807SJeff Garzik 	return rc;
3241c6fd2807SJeff Garzik }
3242c6fd2807SJeff Garzik 
32436f1d1e3aSTejun Heo /**
32446f1d1e3aSTejun Heo  *	ata_set_mode - Program timings and issue SET FEATURES - XFER
32456f1d1e3aSTejun Heo  *	@link: link on which timings will be programmed
324698a1708dSMartin Olsson  *	@r_failed_dev: out parameter for failed device
32476f1d1e3aSTejun Heo  *
32486f1d1e3aSTejun Heo  *	Set ATA device disk transfer mode (PIO3, UDMA6, etc.).  If
32496f1d1e3aSTejun Heo  *	ata_set_mode() fails, pointer to the failing device is
32506f1d1e3aSTejun Heo  *	returned in @r_failed_dev.
32516f1d1e3aSTejun Heo  *
32526f1d1e3aSTejun Heo  *	LOCKING:
32536f1d1e3aSTejun Heo  *	PCI/etc. bus probe sem.
32546f1d1e3aSTejun Heo  *
32556f1d1e3aSTejun Heo  *	RETURNS:
32566f1d1e3aSTejun Heo  *	0 on success, negative errno otherwise
32576f1d1e3aSTejun Heo  */
32586f1d1e3aSTejun Heo int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
32596f1d1e3aSTejun Heo {
32606f1d1e3aSTejun Heo 	struct ata_port *ap = link->ap;
326100115e0fSTejun Heo 	struct ata_device *dev;
326200115e0fSTejun Heo 	int rc;
32636f1d1e3aSTejun Heo 
326476326ac1STejun Heo 	/* if data transfer is verified, clear DUBIOUS_XFER on ering top */
32651eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
326676326ac1STejun Heo 		if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
326776326ac1STejun Heo 			struct ata_ering_entry *ent;
326876326ac1STejun Heo 
326976326ac1STejun Heo 			ent = ata_ering_top(&dev->ering);
327076326ac1STejun Heo 			if (ent)
327176326ac1STejun Heo 				ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER;
327276326ac1STejun Heo 		}
327376326ac1STejun Heo 	}
327476326ac1STejun Heo 
32756f1d1e3aSTejun Heo 	/* has private set_mode? */
32766f1d1e3aSTejun Heo 	if (ap->ops->set_mode)
327700115e0fSTejun Heo 		rc = ap->ops->set_mode(link, r_failed_dev);
327800115e0fSTejun Heo 	else
327900115e0fSTejun Heo 		rc = ata_do_set_mode(link, r_failed_dev);
328000115e0fSTejun Heo 
328100115e0fSTejun Heo 	/* if transfer mode has changed, set DUBIOUS_XFER on device */
32821eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
328300115e0fSTejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
328400115e0fSTejun Heo 		u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
328500115e0fSTejun Heo 		u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
328600115e0fSTejun Heo 
328700115e0fSTejun Heo 		if (dev->xfer_mode != saved_xfer_mode ||
328800115e0fSTejun Heo 		    ata_ncq_enabled(dev) != saved_ncq)
328900115e0fSTejun Heo 			dev->flags |= ATA_DFLAG_DUBIOUS_XFER;
329000115e0fSTejun Heo 	}
329100115e0fSTejun Heo 
329200115e0fSTejun Heo 	return rc;
32936f1d1e3aSTejun Heo }
32946f1d1e3aSTejun Heo 
329511fc33daSTejun Heo /**
329611fc33daSTejun Heo  *	atapi_eh_clear_ua - Clear ATAPI UNIT ATTENTION after reset
329711fc33daSTejun Heo  *	@dev: ATAPI device to clear UA for
329811fc33daSTejun Heo  *
329911fc33daSTejun Heo  *	Resets and other operations can make an ATAPI device raise
330011fc33daSTejun Heo  *	UNIT ATTENTION which causes the next operation to fail.  This
330111fc33daSTejun Heo  *	function clears UA.
330211fc33daSTejun Heo  *
330311fc33daSTejun Heo  *	LOCKING:
330411fc33daSTejun Heo  *	EH context (may sleep).
330511fc33daSTejun Heo  *
330611fc33daSTejun Heo  *	RETURNS:
330711fc33daSTejun Heo  *	0 on success, -errno on failure.
330811fc33daSTejun Heo  */
330911fc33daSTejun Heo static int atapi_eh_clear_ua(struct ata_device *dev)
331011fc33daSTejun Heo {
331111fc33daSTejun Heo 	int i;
331211fc33daSTejun Heo 
331311fc33daSTejun Heo 	for (i = 0; i < ATA_EH_UA_TRIES; i++) {
3314b5357081STejun Heo 		u8 *sense_buffer = dev->link->ap->sector_buf;
331511fc33daSTejun Heo 		u8 sense_key = 0;
331611fc33daSTejun Heo 		unsigned int err_mask;
331711fc33daSTejun Heo 
331811fc33daSTejun Heo 		err_mask = atapi_eh_tur(dev, &sense_key);
331911fc33daSTejun Heo 		if (err_mask != 0 && err_mask != AC_ERR_DEV) {
3320a9a79dfeSJoe Perches 			ata_dev_warn(dev,
3321a9a79dfeSJoe Perches 				     "TEST_UNIT_READY failed (err_mask=0x%x)\n",
3322a9a79dfeSJoe Perches 				     err_mask);
332311fc33daSTejun Heo 			return -EIO;
332411fc33daSTejun Heo 		}
332511fc33daSTejun Heo 
332611fc33daSTejun Heo 		if (!err_mask || sense_key != UNIT_ATTENTION)
332711fc33daSTejun Heo 			return 0;
332811fc33daSTejun Heo 
332911fc33daSTejun Heo 		err_mask = atapi_eh_request_sense(dev, sense_buffer, sense_key);
333011fc33daSTejun Heo 		if (err_mask) {
3331a9a79dfeSJoe Perches 			ata_dev_warn(dev, "failed to clear "
333211fc33daSTejun Heo 				"UNIT ATTENTION (err_mask=0x%x)\n", err_mask);
333311fc33daSTejun Heo 			return -EIO;
333411fc33daSTejun Heo 		}
333511fc33daSTejun Heo 	}
333611fc33daSTejun Heo 
3337a9a79dfeSJoe Perches 	ata_dev_warn(dev, "UNIT ATTENTION persists after %d tries\n",
3338a9a79dfeSJoe Perches 		     ATA_EH_UA_TRIES);
333911fc33daSTejun Heo 
334011fc33daSTejun Heo 	return 0;
334111fc33daSTejun Heo }
334211fc33daSTejun Heo 
33436013efd8STejun Heo /**
33446013efd8STejun Heo  *	ata_eh_maybe_retry_flush - Retry FLUSH if necessary
33456013efd8STejun Heo  *	@dev: ATA device which may need FLUSH retry
33466013efd8STejun Heo  *
33476013efd8STejun Heo  *	If @dev failed FLUSH, it needs to be reported upper layer
33486013efd8STejun Heo  *	immediately as it means that @dev failed to remap and already
33496013efd8STejun Heo  *	lost at least a sector and further FLUSH retrials won't make
33506013efd8STejun Heo  *	any difference to the lost sector.  However, if FLUSH failed
33516013efd8STejun Heo  *	for other reasons, for example transmission error, FLUSH needs
33526013efd8STejun Heo  *	to be retried.
33536013efd8STejun Heo  *
33546013efd8STejun Heo  *	This function determines whether FLUSH failure retry is
33556013efd8STejun Heo  *	necessary and performs it if so.
33566013efd8STejun Heo  *
33576013efd8STejun Heo  *	RETURNS:
33586013efd8STejun Heo  *	0 if EH can continue, -errno if EH needs to be repeated.
33596013efd8STejun Heo  */
33606013efd8STejun Heo static int ata_eh_maybe_retry_flush(struct ata_device *dev)
33616013efd8STejun Heo {
33626013efd8STejun Heo 	struct ata_link *link = dev->link;
33636013efd8STejun Heo 	struct ata_port *ap = link->ap;
33646013efd8STejun Heo 	struct ata_queued_cmd *qc;
33656013efd8STejun Heo 	struct ata_taskfile tf;
33666013efd8STejun Heo 	unsigned int err_mask;
33676013efd8STejun Heo 	int rc = 0;
33686013efd8STejun Heo 
33696013efd8STejun Heo 	/* did flush fail for this device? */
33706013efd8STejun Heo 	if (!ata_tag_valid(link->active_tag))
33716013efd8STejun Heo 		return 0;
33726013efd8STejun Heo 
33736013efd8STejun Heo 	qc = __ata_qc_from_tag(ap, link->active_tag);
33746013efd8STejun Heo 	if (qc->dev != dev || (qc->tf.command != ATA_CMD_FLUSH_EXT &&
33756013efd8STejun Heo 			       qc->tf.command != ATA_CMD_FLUSH))
33766013efd8STejun Heo 		return 0;
33776013efd8STejun Heo 
33786013efd8STejun Heo 	/* if the device failed it, it should be reported to upper layers */
33796013efd8STejun Heo 	if (qc->err_mask & AC_ERR_DEV)
33806013efd8STejun Heo 		return 0;
33816013efd8STejun Heo 
33826013efd8STejun Heo 	/* flush failed for some other reason, give it another shot */
33836013efd8STejun Heo 	ata_tf_init(dev, &tf);
33846013efd8STejun Heo 
33856013efd8STejun Heo 	tf.command = qc->tf.command;
33866013efd8STejun Heo 	tf.flags |= ATA_TFLAG_DEVICE;
33876013efd8STejun Heo 	tf.protocol = ATA_PROT_NODATA;
33886013efd8STejun Heo 
3389a9a79dfeSJoe Perches 	ata_dev_warn(dev, "retrying FLUSH 0x%x Emask 0x%x\n",
33906013efd8STejun Heo 		       tf.command, qc->err_mask);
33916013efd8STejun Heo 
33926013efd8STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
33936013efd8STejun Heo 	if (!err_mask) {
33946013efd8STejun Heo 		/*
33956013efd8STejun Heo 		 * FLUSH is complete but there's no way to
33966013efd8STejun Heo 		 * successfully complete a failed command from EH.
33976013efd8STejun Heo 		 * Making sure retry is allowed at least once and
33986013efd8STejun Heo 		 * retrying it should do the trick - whatever was in
33996013efd8STejun Heo 		 * the cache is already on the platter and this won't
34006013efd8STejun Heo 		 * cause infinite loop.
34016013efd8STejun Heo 		 */
34026013efd8STejun Heo 		qc->scsicmd->allowed = max(qc->scsicmd->allowed, 1);
34036013efd8STejun Heo 	} else {
3404a9a79dfeSJoe Perches 		ata_dev_warn(dev, "FLUSH failed Emask 0x%x\n",
34056013efd8STejun Heo 			       err_mask);
34066013efd8STejun Heo 		rc = -EIO;
34076013efd8STejun Heo 
34086013efd8STejun Heo 		/* if device failed it, report it to upper layers */
34096013efd8STejun Heo 		if (err_mask & AC_ERR_DEV) {
34106013efd8STejun Heo 			qc->err_mask |= AC_ERR_DEV;
34116013efd8STejun Heo 			qc->result_tf = tf;
34126013efd8STejun Heo 			if (!(ap->pflags & ATA_PFLAG_FROZEN))
34136013efd8STejun Heo 				rc = 0;
34146013efd8STejun Heo 		}
34156013efd8STejun Heo 	}
34166013efd8STejun Heo 	return rc;
34176013efd8STejun Heo }
34186013efd8STejun Heo 
34196b7ae954STejun Heo /**
34206b7ae954STejun Heo  *	ata_eh_set_lpm - configure SATA interface power management
34216b7ae954STejun Heo  *	@link: link to configure power management
34226b7ae954STejun Heo  *	@policy: the link power management policy
34236b7ae954STejun Heo  *	@r_failed_dev: out parameter for failed device
34246b7ae954STejun Heo  *
34256b7ae954STejun Heo  *	Enable SATA Interface power management.  This will enable
3426f4ac6476SHans de Goede  *	Device Interface Power Management (DIPM) for min_power and
3427f4ac6476SHans de Goede  *	medium_power_with_dipm policies, and then call driver specific
3428f4ac6476SHans de Goede  *	callbacks for enabling Host Initiated Power management.
34296b7ae954STejun Heo  *
34306b7ae954STejun Heo  *	LOCKING:
34316b7ae954STejun Heo  *	EH context.
34326b7ae954STejun Heo  *
34336b7ae954STejun Heo  *	RETURNS:
34346b7ae954STejun Heo  *	0 on success, -errno on failure.
34356b7ae954STejun Heo  */
34366b7ae954STejun Heo static int ata_eh_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
34376b7ae954STejun Heo 			  struct ata_device **r_failed_dev)
34386b7ae954STejun Heo {
34396c8ea89cSTejun Heo 	struct ata_port *ap = ata_is_host_link(link) ? link->ap : NULL;
34406b7ae954STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
34416b7ae954STejun Heo 	struct ata_device *dev, *link_dev = NULL, *lpm_dev = NULL;
3442e5005b15STejun Heo 	enum ata_lpm_policy old_policy = link->lpm_policy;
34435f6f12ccSTejun Heo 	bool no_dipm = link->ap->flags & ATA_FLAG_NO_DIPM;
34446b7ae954STejun Heo 	unsigned int hints = ATA_LPM_EMPTY | ATA_LPM_HIPM;
34456b7ae954STejun Heo 	unsigned int err_mask;
34466b7ae954STejun Heo 	int rc;
34476b7ae954STejun Heo 
34486b7ae954STejun Heo 	/* if the link or host doesn't do LPM, noop */
34496b7ae954STejun Heo 	if ((link->flags & ATA_LFLAG_NO_LPM) || (ap && !ap->ops->set_lpm))
34506b7ae954STejun Heo 		return 0;
34516b7ae954STejun Heo 
34526b7ae954STejun Heo 	/*
34536b7ae954STejun Heo 	 * DIPM is enabled only for MIN_POWER as some devices
34546b7ae954STejun Heo 	 * misbehave when the host NACKs transition to SLUMBER.  Order
34556b7ae954STejun Heo 	 * device and link configurations such that the host always
34566b7ae954STejun Heo 	 * allows DIPM requests.
34576b7ae954STejun Heo 	 */
34586b7ae954STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
34596b7ae954STejun Heo 		bool hipm = ata_id_has_hipm(dev->id);
3460ae01b249STejun Heo 		bool dipm = ata_id_has_dipm(dev->id) && !no_dipm;
34616b7ae954STejun Heo 
34626b7ae954STejun Heo 		/* find the first enabled and LPM enabled devices */
34636b7ae954STejun Heo 		if (!link_dev)
34646b7ae954STejun Heo 			link_dev = dev;
34656b7ae954STejun Heo 
34666b7ae954STejun Heo 		if (!lpm_dev && (hipm || dipm))
34676b7ae954STejun Heo 			lpm_dev = dev;
34686b7ae954STejun Heo 
34696b7ae954STejun Heo 		hints &= ~ATA_LPM_EMPTY;
34706b7ae954STejun Heo 		if (!hipm)
34716b7ae954STejun Heo 			hints &= ~ATA_LPM_HIPM;
34726b7ae954STejun Heo 
34736b7ae954STejun Heo 		/* disable DIPM before changing link config */
3474f4ac6476SHans de Goede 		if (policy < ATA_LPM_MED_POWER_WITH_DIPM && dipm) {
34756b7ae954STejun Heo 			err_mask = ata_dev_set_feature(dev,
34766b7ae954STejun Heo 					SETFEATURES_SATA_DISABLE, SATA_DIPM);
34776b7ae954STejun Heo 			if (err_mask && err_mask != AC_ERR_DEV) {
3478a9a79dfeSJoe Perches 				ata_dev_warn(dev,
34796b7ae954STejun Heo 					     "failed to disable DIPM, Emask 0x%x\n",
34806b7ae954STejun Heo 					     err_mask);
34816b7ae954STejun Heo 				rc = -EIO;
34826b7ae954STejun Heo 				goto fail;
34836b7ae954STejun Heo 			}
34846b7ae954STejun Heo 		}
34856b7ae954STejun Heo 	}
34866b7ae954STejun Heo 
34876c8ea89cSTejun Heo 	if (ap) {
34886b7ae954STejun Heo 		rc = ap->ops->set_lpm(link, policy, hints);
34896b7ae954STejun Heo 		if (!rc && ap->slave_link)
34906b7ae954STejun Heo 			rc = ap->ops->set_lpm(ap->slave_link, policy, hints);
34916c8ea89cSTejun Heo 	} else
34926c8ea89cSTejun Heo 		rc = sata_pmp_set_lpm(link, policy, hints);
34936b7ae954STejun Heo 
34946b7ae954STejun Heo 	/*
34956b7ae954STejun Heo 	 * Attribute link config failure to the first (LPM) enabled
34966b7ae954STejun Heo 	 * device on the link.
34976b7ae954STejun Heo 	 */
34986b7ae954STejun Heo 	if (rc) {
34996b7ae954STejun Heo 		if (rc == -EOPNOTSUPP) {
35006b7ae954STejun Heo 			link->flags |= ATA_LFLAG_NO_LPM;
35016b7ae954STejun Heo 			return 0;
35026b7ae954STejun Heo 		}
35036b7ae954STejun Heo 		dev = lpm_dev ? lpm_dev : link_dev;
35046b7ae954STejun Heo 		goto fail;
35056b7ae954STejun Heo 	}
35066b7ae954STejun Heo 
3507e5005b15STejun Heo 	/*
3508e5005b15STejun Heo 	 * Low level driver acked the transition.  Issue DIPM command
3509e5005b15STejun Heo 	 * with the new policy set.
3510e5005b15STejun Heo 	 */
3511e5005b15STejun Heo 	link->lpm_policy = policy;
3512e5005b15STejun Heo 	if (ap && ap->slave_link)
3513e5005b15STejun Heo 		ap->slave_link->lpm_policy = policy;
3514e5005b15STejun Heo 
35156b7ae954STejun Heo 	/* host config updated, enable DIPM if transitioning to MIN_POWER */
35166b7ae954STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
3517f4ac6476SHans de Goede 		if (policy >= ATA_LPM_MED_POWER_WITH_DIPM && !no_dipm &&
3518ae01b249STejun Heo 		    ata_id_has_dipm(dev->id)) {
35196b7ae954STejun Heo 			err_mask = ata_dev_set_feature(dev,
35206b7ae954STejun Heo 					SETFEATURES_SATA_ENABLE, SATA_DIPM);
35216b7ae954STejun Heo 			if (err_mask && err_mask != AC_ERR_DEV) {
3522a9a79dfeSJoe Perches 				ata_dev_warn(dev,
35236b7ae954STejun Heo 					"failed to enable DIPM, Emask 0x%x\n",
35246b7ae954STejun Heo 					err_mask);
35256b7ae954STejun Heo 				rc = -EIO;
35266b7ae954STejun Heo 				goto fail;
35276b7ae954STejun Heo 			}
35286b7ae954STejun Heo 		}
35296b7ae954STejun Heo 	}
35306b7ae954STejun Heo 
353109c5b480SGabriele Mazzotta 	link->last_lpm_change = jiffies;
353209c5b480SGabriele Mazzotta 	link->flags |= ATA_LFLAG_CHANGED;
353309c5b480SGabriele Mazzotta 
35346b7ae954STejun Heo 	return 0;
35356b7ae954STejun Heo 
35366b7ae954STejun Heo fail:
3537e5005b15STejun Heo 	/* restore the old policy */
3538e5005b15STejun Heo 	link->lpm_policy = old_policy;
3539e5005b15STejun Heo 	if (ap && ap->slave_link)
3540e5005b15STejun Heo 		ap->slave_link->lpm_policy = old_policy;
3541e5005b15STejun Heo 
35426b7ae954STejun Heo 	/* if no device or only one more chance is left, disable LPM */
35436b7ae954STejun Heo 	if (!dev || ehc->tries[dev->devno] <= 2) {
3544a9a79dfeSJoe Perches 		ata_link_warn(link, "disabling LPM on the link\n");
35456b7ae954STejun Heo 		link->flags |= ATA_LFLAG_NO_LPM;
35466b7ae954STejun Heo 	}
35476b7ae954STejun Heo 	if (r_failed_dev)
35486b7ae954STejun Heo 		*r_failed_dev = dev;
35496b7ae954STejun Heo 	return rc;
35506b7ae954STejun Heo }
35516b7ae954STejun Heo 
35528a745f1fSKristen Carlson Accardi int ata_link_nr_enabled(struct ata_link *link)
3553c6fd2807SJeff Garzik {
3554f58229f8STejun Heo 	struct ata_device *dev;
3555f58229f8STejun Heo 	int cnt = 0;
3556c6fd2807SJeff Garzik 
35571eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED)
3558c6fd2807SJeff Garzik 		cnt++;
3559c6fd2807SJeff Garzik 	return cnt;
3560c6fd2807SJeff Garzik }
3561c6fd2807SJeff Garzik 
35620260731fSTejun Heo static int ata_link_nr_vacant(struct ata_link *link)
3563c6fd2807SJeff Garzik {
3564f58229f8STejun Heo 	struct ata_device *dev;
3565f58229f8STejun Heo 	int cnt = 0;
3566c6fd2807SJeff Garzik 
35671eca4365STejun Heo 	ata_for_each_dev(dev, link, ALL)
3568f58229f8STejun Heo 		if (dev->class == ATA_DEV_UNKNOWN)
3569c6fd2807SJeff Garzik 			cnt++;
3570c6fd2807SJeff Garzik 	return cnt;
3571c6fd2807SJeff Garzik }
3572c6fd2807SJeff Garzik 
35730260731fSTejun Heo static int ata_eh_skip_recovery(struct ata_link *link)
3574c6fd2807SJeff Garzik {
3575672b2d65STejun Heo 	struct ata_port *ap = link->ap;
35760260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
3577f58229f8STejun Heo 	struct ata_device *dev;
3578c6fd2807SJeff Garzik 
3579f9df58cbSTejun Heo 	/* skip disabled links */
3580f9df58cbSTejun Heo 	if (link->flags & ATA_LFLAG_DISABLED)
3581f9df58cbSTejun Heo 		return 1;
3582f9df58cbSTejun Heo 
3583e2f3d75fSTejun Heo 	/* skip if explicitly requested */
3584e2f3d75fSTejun Heo 	if (ehc->i.flags & ATA_EHI_NO_RECOVERY)
3585e2f3d75fSTejun Heo 		return 1;
3586e2f3d75fSTejun Heo 
3587672b2d65STejun Heo 	/* thaw frozen port and recover failed devices */
3588672b2d65STejun Heo 	if ((ap->pflags & ATA_PFLAG_FROZEN) || ata_link_nr_enabled(link))
3589672b2d65STejun Heo 		return 0;
3590672b2d65STejun Heo 
3591672b2d65STejun Heo 	/* reset at least once if reset is requested */
3592672b2d65STejun Heo 	if ((ehc->i.action & ATA_EH_RESET) &&
3593672b2d65STejun Heo 	    !(ehc->i.flags & ATA_EHI_DID_RESET))
3594c6fd2807SJeff Garzik 		return 0;
3595c6fd2807SJeff Garzik 
3596c6fd2807SJeff Garzik 	/* skip if class codes for all vacant slots are ATA_DEV_NONE */
35971eca4365STejun Heo 	ata_for_each_dev(dev, link, ALL) {
3598c6fd2807SJeff Garzik 		if (dev->class == ATA_DEV_UNKNOWN &&
3599c6fd2807SJeff Garzik 		    ehc->classes[dev->devno] != ATA_DEV_NONE)
3600c6fd2807SJeff Garzik 			return 0;
3601c6fd2807SJeff Garzik 	}
3602c6fd2807SJeff Garzik 
3603c6fd2807SJeff Garzik 	return 1;
3604c6fd2807SJeff Garzik }
3605c6fd2807SJeff Garzik 
3606c2c7a89cSTejun Heo static int ata_count_probe_trials_cb(struct ata_ering_entry *ent, void *void_arg)
3607c2c7a89cSTejun Heo {
3608c2c7a89cSTejun Heo 	u64 interval = msecs_to_jiffies(ATA_EH_PROBE_TRIAL_INTERVAL);
3609c2c7a89cSTejun Heo 	u64 now = get_jiffies_64();
3610c2c7a89cSTejun Heo 	int *trials = void_arg;
3611c2c7a89cSTejun Heo 
36126868225eSLin Ming 	if ((ent->eflags & ATA_EFLAG_OLD_ER) ||
36136868225eSLin Ming 	    (ent->timestamp < now - min(now, interval)))
3614c2c7a89cSTejun Heo 		return -1;
3615c2c7a89cSTejun Heo 
3616c2c7a89cSTejun Heo 	(*trials)++;
3617c2c7a89cSTejun Heo 	return 0;
3618c2c7a89cSTejun Heo }
3619c2c7a89cSTejun Heo 
362002c05a27STejun Heo static int ata_eh_schedule_probe(struct ata_device *dev)
362102c05a27STejun Heo {
362202c05a27STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
3623c2c7a89cSTejun Heo 	struct ata_link *link = ata_dev_phys_link(dev);
3624c2c7a89cSTejun Heo 	int trials = 0;
362502c05a27STejun Heo 
362602c05a27STejun Heo 	if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
362702c05a27STejun Heo 	    (ehc->did_probe_mask & (1 << dev->devno)))
362802c05a27STejun Heo 		return 0;
362902c05a27STejun Heo 
363002c05a27STejun Heo 	ata_eh_detach_dev(dev);
363102c05a27STejun Heo 	ata_dev_init(dev);
363202c05a27STejun Heo 	ehc->did_probe_mask |= (1 << dev->devno);
3633cf480626STejun Heo 	ehc->i.action |= ATA_EH_RESET;
363400115e0fSTejun Heo 	ehc->saved_xfer_mode[dev->devno] = 0;
363500115e0fSTejun Heo 	ehc->saved_ncq_enabled &= ~(1 << dev->devno);
363602c05a27STejun Heo 
36376b7ae954STejun Heo 	/* the link maybe in a deep sleep, wake it up */
36386c8ea89cSTejun Heo 	if (link->lpm_policy > ATA_LPM_MAX_POWER) {
36396c8ea89cSTejun Heo 		if (ata_is_host_link(link))
36406c8ea89cSTejun Heo 			link->ap->ops->set_lpm(link, ATA_LPM_MAX_POWER,
36416c8ea89cSTejun Heo 					       ATA_LPM_EMPTY);
36426c8ea89cSTejun Heo 		else
36436c8ea89cSTejun Heo 			sata_pmp_set_lpm(link, ATA_LPM_MAX_POWER,
36446c8ea89cSTejun Heo 					 ATA_LPM_EMPTY);
36456c8ea89cSTejun Heo 	}
36466b7ae954STejun Heo 
3647c2c7a89cSTejun Heo 	/* Record and count probe trials on the ering.  The specific
3648c2c7a89cSTejun Heo 	 * error mask used is irrelevant.  Because a successful device
3649c2c7a89cSTejun Heo 	 * detection clears the ering, this count accumulates only if
3650c2c7a89cSTejun Heo 	 * there are consecutive failed probes.
3651c2c7a89cSTejun Heo 	 *
3652c2c7a89cSTejun Heo 	 * If the count is equal to or higher than ATA_EH_PROBE_TRIALS
3653c2c7a89cSTejun Heo 	 * in the last ATA_EH_PROBE_TRIAL_INTERVAL, link speed is
3654c2c7a89cSTejun Heo 	 * forced to 1.5Gbps.
3655c2c7a89cSTejun Heo 	 *
3656c2c7a89cSTejun Heo 	 * This is to work around cases where failed link speed
3657c2c7a89cSTejun Heo 	 * negotiation results in device misdetection leading to
3658c2c7a89cSTejun Heo 	 * infinite DEVXCHG or PHRDY CHG events.
3659c2c7a89cSTejun Heo 	 */
3660c2c7a89cSTejun Heo 	ata_ering_record(&dev->ering, 0, AC_ERR_OTHER);
3661c2c7a89cSTejun Heo 	ata_ering_map(&dev->ering, ata_count_probe_trials_cb, &trials);
3662c2c7a89cSTejun Heo 
3663c2c7a89cSTejun Heo 	if (trials > ATA_EH_PROBE_TRIALS)
3664c2c7a89cSTejun Heo 		sata_down_spd_limit(link, 1);
3665c2c7a89cSTejun Heo 
366602c05a27STejun Heo 	return 1;
366702c05a27STejun Heo }
366802c05a27STejun Heo 
36699b1e2658STejun Heo static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
3670fee7ca72STejun Heo {
36719af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
3672fee7ca72STejun Heo 
3673cf9a590aSTejun Heo 	/* -EAGAIN from EH routine indicates retry without prejudice.
3674cf9a590aSTejun Heo 	 * The requester is responsible for ensuring forward progress.
3675cf9a590aSTejun Heo 	 */
3676cf9a590aSTejun Heo 	if (err != -EAGAIN)
3677fee7ca72STejun Heo 		ehc->tries[dev->devno]--;
3678fee7ca72STejun Heo 
3679fee7ca72STejun Heo 	switch (err) {
3680fee7ca72STejun Heo 	case -ENODEV:
3681fee7ca72STejun Heo 		/* device missing or wrong IDENTIFY data, schedule probing */
3682fee7ca72STejun Heo 		ehc->i.probe_mask |= (1 << dev->devno);
368305b83605SGustavo A. R. Silva 		/* fall through */
3684fee7ca72STejun Heo 	case -EINVAL:
3685fee7ca72STejun Heo 		/* give it just one more chance */
3686fee7ca72STejun Heo 		ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
368705b83605SGustavo A. R. Silva 		/* fall through */
3688fee7ca72STejun Heo 	case -EIO:
3689d89293abSTejun Heo 		if (ehc->tries[dev->devno] == 1) {
3690fee7ca72STejun Heo 			/* This is the last chance, better to slow
3691fee7ca72STejun Heo 			 * down than lose it.
3692fee7ca72STejun Heo 			 */
3693a07d499bSTejun Heo 			sata_down_spd_limit(ata_dev_phys_link(dev), 0);
3694d89293abSTejun Heo 			if (dev->pio_mode > XFER_PIO_0)
3695fee7ca72STejun Heo 				ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
3696fee7ca72STejun Heo 		}
3697fee7ca72STejun Heo 	}
3698fee7ca72STejun Heo 
3699fee7ca72STejun Heo 	if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
3700fee7ca72STejun Heo 		/* disable device if it has used up all its chances */
3701fee7ca72STejun Heo 		ata_dev_disable(dev);
3702fee7ca72STejun Heo 
3703fee7ca72STejun Heo 		/* detach if offline */
3704b1c72916STejun Heo 		if (ata_phys_link_offline(ata_dev_phys_link(dev)))
3705fee7ca72STejun Heo 			ata_eh_detach_dev(dev);
3706fee7ca72STejun Heo 
370702c05a27STejun Heo 		/* schedule probe if necessary */
370887fbc5a0STejun Heo 		if (ata_eh_schedule_probe(dev)) {
3709fee7ca72STejun Heo 			ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
371087fbc5a0STejun Heo 			memset(ehc->cmd_timeout_idx[dev->devno], 0,
371187fbc5a0STejun Heo 			       sizeof(ehc->cmd_timeout_idx[dev->devno]));
371287fbc5a0STejun Heo 		}
37139b1e2658STejun Heo 
37149b1e2658STejun Heo 		return 1;
3715fee7ca72STejun Heo 	} else {
3716cf480626STejun Heo 		ehc->i.action |= ATA_EH_RESET;
37179b1e2658STejun Heo 		return 0;
3718fee7ca72STejun Heo 	}
3719fee7ca72STejun Heo }
3720fee7ca72STejun Heo 
3721c6fd2807SJeff Garzik /**
3722c6fd2807SJeff Garzik  *	ata_eh_recover - recover host port after error
3723c6fd2807SJeff Garzik  *	@ap: host port to recover
3724c6fd2807SJeff Garzik  *	@prereset: prereset method (can be NULL)
3725c6fd2807SJeff Garzik  *	@softreset: softreset method (can be NULL)
3726c6fd2807SJeff Garzik  *	@hardreset: hardreset method (can be NULL)
3727c6fd2807SJeff Garzik  *	@postreset: postreset method (can be NULL)
37289b1e2658STejun Heo  *	@r_failed_link: out parameter for failed link
3729c6fd2807SJeff Garzik  *
3730c6fd2807SJeff Garzik  *	This is the alpha and omega, eum and yang, heart and soul of
3731c6fd2807SJeff Garzik  *	libata exception handling.  On entry, actions required to
37329b1e2658STejun Heo  *	recover each link and hotplug requests are recorded in the
37339b1e2658STejun Heo  *	link's eh_context.  This function executes all the operations
37349b1e2658STejun Heo  *	with appropriate retrials and fallbacks to resurrect failed
3735c6fd2807SJeff Garzik  *	devices, detach goners and greet newcomers.
3736c6fd2807SJeff Garzik  *
3737c6fd2807SJeff Garzik  *	LOCKING:
3738c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
3739c6fd2807SJeff Garzik  *
3740c6fd2807SJeff Garzik  *	RETURNS:
3741c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
3742c6fd2807SJeff Garzik  */
3743fb7fd614STejun Heo int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
3744c6fd2807SJeff Garzik 		   ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
37459b1e2658STejun Heo 		   ata_postreset_fn_t postreset,
37469b1e2658STejun Heo 		   struct ata_link **r_failed_link)
3747c6fd2807SJeff Garzik {
37489b1e2658STejun Heo 	struct ata_link *link;
3749c6fd2807SJeff Garzik 	struct ata_device *dev;
37506b7ae954STejun Heo 	int rc, nr_fails;
375145fabbb7SElias Oltmanns 	unsigned long flags, deadline;
3752c6fd2807SJeff Garzik 
3753c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
3754c6fd2807SJeff Garzik 
3755c6fd2807SJeff Garzik 	/* prep for recovery */
37561eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE) {
37579b1e2658STejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
37589b1e2658STejun Heo 
3759f9df58cbSTejun Heo 		/* re-enable link? */
3760f9df58cbSTejun Heo 		if (ehc->i.action & ATA_EH_ENABLE_LINK) {
3761f9df58cbSTejun Heo 			ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
3762f9df58cbSTejun Heo 			spin_lock_irqsave(ap->lock, flags);
3763f9df58cbSTejun Heo 			link->flags &= ~ATA_LFLAG_DISABLED;
3764f9df58cbSTejun Heo 			spin_unlock_irqrestore(ap->lock, flags);
3765f9df58cbSTejun Heo 			ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
3766f9df58cbSTejun Heo 		}
3767f9df58cbSTejun Heo 
37681eca4365STejun Heo 		ata_for_each_dev(dev, link, ALL) {
3769fd995f70STejun Heo 			if (link->flags & ATA_LFLAG_NO_RETRY)
3770fd995f70STejun Heo 				ehc->tries[dev->devno] = 1;
3771fd995f70STejun Heo 			else
3772c6fd2807SJeff Garzik 				ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
3773c6fd2807SJeff Garzik 
377479a55b72STejun Heo 			/* collect port action mask recorded in dev actions */
37759b1e2658STejun Heo 			ehc->i.action |= ehc->i.dev_action[dev->devno] &
37769b1e2658STejun Heo 					 ~ATA_EH_PERDEV_MASK;
3777f58229f8STejun Heo 			ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
377879a55b72STejun Heo 
3779c6fd2807SJeff Garzik 			/* process hotplug request */
3780c6fd2807SJeff Garzik 			if (dev->flags & ATA_DFLAG_DETACH)
3781c6fd2807SJeff Garzik 				ata_eh_detach_dev(dev);
3782c6fd2807SJeff Garzik 
378302c05a27STejun Heo 			/* schedule probe if necessary */
378402c05a27STejun Heo 			if (!ata_dev_enabled(dev))
378502c05a27STejun Heo 				ata_eh_schedule_probe(dev);
3786c6fd2807SJeff Garzik 		}
37879b1e2658STejun Heo 	}
3788c6fd2807SJeff Garzik 
3789c6fd2807SJeff Garzik  retry:
3790c6fd2807SJeff Garzik 	rc = 0;
3791c6fd2807SJeff Garzik 
3792c6fd2807SJeff Garzik 	/* if UNLOADING, finish immediately */
3793c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_UNLOADING)
3794c6fd2807SJeff Garzik 		goto out;
3795c6fd2807SJeff Garzik 
37969b1e2658STejun Heo 	/* prep for EH */
37971eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE) {
37989b1e2658STejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
37999b1e2658STejun Heo 
3800c6fd2807SJeff Garzik 		/* skip EH if possible. */
38010260731fSTejun Heo 		if (ata_eh_skip_recovery(link))
3802c6fd2807SJeff Garzik 			ehc->i.action = 0;
3803c6fd2807SJeff Garzik 
38041eca4365STejun Heo 		ata_for_each_dev(dev, link, ALL)
3805f58229f8STejun Heo 			ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
38069b1e2658STejun Heo 	}
3807c6fd2807SJeff Garzik 
3808c6fd2807SJeff Garzik 	/* reset */
38091eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE) {
38109b1e2658STejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
38119b1e2658STejun Heo 
3812cf480626STejun Heo 		if (!(ehc->i.action & ATA_EH_RESET))
38139b1e2658STejun Heo 			continue;
38149b1e2658STejun Heo 
38159b1e2658STejun Heo 		rc = ata_eh_reset(link, ata_link_nr_vacant(link),
3816dc98c32cSTejun Heo 				  prereset, softreset, hardreset, postreset);
3817c6fd2807SJeff Garzik 		if (rc) {
3818a9a79dfeSJoe Perches 			ata_link_err(link, "reset failed, giving up\n");
3819c6fd2807SJeff Garzik 			goto out;
3820c6fd2807SJeff Garzik 		}
38219b1e2658STejun Heo 	}
3822c6fd2807SJeff Garzik 
382345fabbb7SElias Oltmanns 	do {
382445fabbb7SElias Oltmanns 		unsigned long now;
382545fabbb7SElias Oltmanns 
382645fabbb7SElias Oltmanns 		/*
382745fabbb7SElias Oltmanns 		 * clears ATA_EH_PARK in eh_info and resets
382845fabbb7SElias Oltmanns 		 * ap->park_req_pending
382945fabbb7SElias Oltmanns 		 */
383045fabbb7SElias Oltmanns 		ata_eh_pull_park_action(ap);
383145fabbb7SElias Oltmanns 
383245fabbb7SElias Oltmanns 		deadline = jiffies;
38331eca4365STejun Heo 		ata_for_each_link(link, ap, EDGE) {
38341eca4365STejun Heo 			ata_for_each_dev(dev, link, ALL) {
383545fabbb7SElias Oltmanns 				struct ata_eh_context *ehc = &link->eh_context;
383645fabbb7SElias Oltmanns 				unsigned long tmp;
383745fabbb7SElias Oltmanns 
38389162c657SHannes Reinecke 				if (dev->class != ATA_DEV_ATA &&
38399162c657SHannes Reinecke 				    dev->class != ATA_DEV_ZAC)
384045fabbb7SElias Oltmanns 					continue;
384145fabbb7SElias Oltmanns 				if (!(ehc->i.dev_action[dev->devno] &
384245fabbb7SElias Oltmanns 				      ATA_EH_PARK))
384345fabbb7SElias Oltmanns 					continue;
384445fabbb7SElias Oltmanns 				tmp = dev->unpark_deadline;
384545fabbb7SElias Oltmanns 				if (time_before(deadline, tmp))
384645fabbb7SElias Oltmanns 					deadline = tmp;
384745fabbb7SElias Oltmanns 				else if (time_before_eq(tmp, jiffies))
384845fabbb7SElias Oltmanns 					continue;
384945fabbb7SElias Oltmanns 				if (ehc->unloaded_mask & (1 << dev->devno))
385045fabbb7SElias Oltmanns 					continue;
385145fabbb7SElias Oltmanns 
385245fabbb7SElias Oltmanns 				ata_eh_park_issue_cmd(dev, 1);
385345fabbb7SElias Oltmanns 			}
385445fabbb7SElias Oltmanns 		}
385545fabbb7SElias Oltmanns 
385645fabbb7SElias Oltmanns 		now = jiffies;
385745fabbb7SElias Oltmanns 		if (time_before_eq(deadline, now))
385845fabbb7SElias Oltmanns 			break;
385945fabbb7SElias Oltmanns 
3860c0c362b6STejun Heo 		ata_eh_release(ap);
386145fabbb7SElias Oltmanns 		deadline = wait_for_completion_timeout(&ap->park_req_pending,
386245fabbb7SElias Oltmanns 						       deadline - now);
3863c0c362b6STejun Heo 		ata_eh_acquire(ap);
386445fabbb7SElias Oltmanns 	} while (deadline);
38651eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE) {
38661eca4365STejun Heo 		ata_for_each_dev(dev, link, ALL) {
386745fabbb7SElias Oltmanns 			if (!(link->eh_context.unloaded_mask &
386845fabbb7SElias Oltmanns 			      (1 << dev->devno)))
386945fabbb7SElias Oltmanns 				continue;
387045fabbb7SElias Oltmanns 
387145fabbb7SElias Oltmanns 			ata_eh_park_issue_cmd(dev, 0);
387245fabbb7SElias Oltmanns 			ata_eh_done(link, dev, ATA_EH_PARK);
387345fabbb7SElias Oltmanns 		}
387445fabbb7SElias Oltmanns 	}
387545fabbb7SElias Oltmanns 
38769b1e2658STejun Heo 	/* the rest */
38776b7ae954STejun Heo 	nr_fails = 0;
38786b7ae954STejun Heo 	ata_for_each_link(link, ap, PMP_FIRST) {
38799b1e2658STejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
38809b1e2658STejun Heo 
38816b7ae954STejun Heo 		if (sata_pmp_attached(ap) && ata_is_host_link(link))
38826b7ae954STejun Heo 			goto config_lpm;
38836b7ae954STejun Heo 
3884c6fd2807SJeff Garzik 		/* revalidate existing devices and attach new ones */
38850260731fSTejun Heo 		rc = ata_eh_revalidate_and_attach(link, &dev);
3886c6fd2807SJeff Garzik 		if (rc)
38876b7ae954STejun Heo 			goto rest_fail;
3888c6fd2807SJeff Garzik 
3889633273a3STejun Heo 		/* if PMP got attached, return, pmp EH will take care of it */
3890633273a3STejun Heo 		if (link->device->class == ATA_DEV_PMP) {
3891633273a3STejun Heo 			ehc->i.action = 0;
3892633273a3STejun Heo 			return 0;
3893633273a3STejun Heo 		}
3894633273a3STejun Heo 
3895baa1e78aSTejun Heo 		/* configure transfer mode if necessary */
3896baa1e78aSTejun Heo 		if (ehc->i.flags & ATA_EHI_SETMODE) {
38970260731fSTejun Heo 			rc = ata_set_mode(link, &dev);
38984ae72a1eSTejun Heo 			if (rc)
38996b7ae954STejun Heo 				goto rest_fail;
3900baa1e78aSTejun Heo 			ehc->i.flags &= ~ATA_EHI_SETMODE;
3901c6fd2807SJeff Garzik 		}
3902c6fd2807SJeff Garzik 
390311fc33daSTejun Heo 		/* If reset has been issued, clear UA to avoid
390411fc33daSTejun Heo 		 * disrupting the current users of the device.
390511fc33daSTejun Heo 		 */
390611fc33daSTejun Heo 		if (ehc->i.flags & ATA_EHI_DID_RESET) {
39071eca4365STejun Heo 			ata_for_each_dev(dev, link, ALL) {
390811fc33daSTejun Heo 				if (dev->class != ATA_DEV_ATAPI)
390911fc33daSTejun Heo 					continue;
391011fc33daSTejun Heo 				rc = atapi_eh_clear_ua(dev);
391111fc33daSTejun Heo 				if (rc)
39126b7ae954STejun Heo 					goto rest_fail;
391321334205SAaron Lu 				if (zpodd_dev_enabled(dev))
391421334205SAaron Lu 					zpodd_post_poweron(dev);
391511fc33daSTejun Heo 			}
391611fc33daSTejun Heo 		}
391711fc33daSTejun Heo 
39186013efd8STejun Heo 		/* retry flush if necessary */
39196013efd8STejun Heo 		ata_for_each_dev(dev, link, ALL) {
39209162c657SHannes Reinecke 			if (dev->class != ATA_DEV_ATA &&
39219162c657SHannes Reinecke 			    dev->class != ATA_DEV_ZAC)
39226013efd8STejun Heo 				continue;
39236013efd8STejun Heo 			rc = ata_eh_maybe_retry_flush(dev);
39246013efd8STejun Heo 			if (rc)
39256b7ae954STejun Heo 				goto rest_fail;
39266013efd8STejun Heo 		}
39276013efd8STejun Heo 
39286b7ae954STejun Heo 	config_lpm:
392911fc33daSTejun Heo 		/* configure link power saving */
39306b7ae954STejun Heo 		if (link->lpm_policy != ap->target_lpm_policy) {
39316b7ae954STejun Heo 			rc = ata_eh_set_lpm(link, ap->target_lpm_policy, &dev);
39326b7ae954STejun Heo 			if (rc)
39336b7ae954STejun Heo 				goto rest_fail;
39346b7ae954STejun Heo 		}
3935ca77329fSKristen Carlson Accardi 
39369b1e2658STejun Heo 		/* this link is okay now */
39379b1e2658STejun Heo 		ehc->i.flags = 0;
39389b1e2658STejun Heo 		continue;
3939c6fd2807SJeff Garzik 
39406b7ae954STejun Heo 	rest_fail:
39416b7ae954STejun Heo 		nr_fails++;
39426b7ae954STejun Heo 		if (dev)
39430a2c0f56STejun Heo 			ata_eh_handle_dev_fail(dev, rc);
3944c6fd2807SJeff Garzik 
3945b06ce3e5STejun Heo 		if (ap->pflags & ATA_PFLAG_FROZEN) {
3946b06ce3e5STejun Heo 			/* PMP reset requires working host port.
3947b06ce3e5STejun Heo 			 * Can't retry if it's frozen.
3948b06ce3e5STejun Heo 			 */
3949071f44b1STejun Heo 			if (sata_pmp_attached(ap))
3950b06ce3e5STejun Heo 				goto out;
39519b1e2658STejun Heo 			break;
39529b1e2658STejun Heo 		}
3953b06ce3e5STejun Heo 	}
39549b1e2658STejun Heo 
39556b7ae954STejun Heo 	if (nr_fails)
3956c6fd2807SJeff Garzik 		goto retry;
3957c6fd2807SJeff Garzik 
3958c6fd2807SJeff Garzik  out:
39599b1e2658STejun Heo 	if (rc && r_failed_link)
39609b1e2658STejun Heo 		*r_failed_link = link;
3961c6fd2807SJeff Garzik 
3962c6fd2807SJeff Garzik 	DPRINTK("EXIT, rc=%d\n", rc);
3963c6fd2807SJeff Garzik 	return rc;
3964c6fd2807SJeff Garzik }
3965c6fd2807SJeff Garzik 
3966c6fd2807SJeff Garzik /**
3967c6fd2807SJeff Garzik  *	ata_eh_finish - finish up EH
3968c6fd2807SJeff Garzik  *	@ap: host port to finish EH for
3969c6fd2807SJeff Garzik  *
3970c6fd2807SJeff Garzik  *	Recovery is complete.  Clean up EH states and retry or finish
3971c6fd2807SJeff Garzik  *	failed qcs.
3972c6fd2807SJeff Garzik  *
3973c6fd2807SJeff Garzik  *	LOCKING:
3974c6fd2807SJeff Garzik  *	None.
3975c6fd2807SJeff Garzik  */
3976fb7fd614STejun Heo void ata_eh_finish(struct ata_port *ap)
3977c6fd2807SJeff Garzik {
3978258c4e5cSJens Axboe 	struct ata_queued_cmd *qc;
3979c6fd2807SJeff Garzik 	int tag;
3980c6fd2807SJeff Garzik 
3981c6fd2807SJeff Garzik 	/* retry or finish qcs */
3982258c4e5cSJens Axboe 	ata_qc_for_each_raw(ap, qc, tag) {
3983c6fd2807SJeff Garzik 		if (!(qc->flags & ATA_QCFLAG_FAILED))
3984c6fd2807SJeff Garzik 			continue;
3985c6fd2807SJeff Garzik 
3986c6fd2807SJeff Garzik 		if (qc->err_mask) {
3987c6fd2807SJeff Garzik 			/* FIXME: Once EH migration is complete,
3988c6fd2807SJeff Garzik 			 * generate sense data in this function,
3989c6fd2807SJeff Garzik 			 * considering both err_mask and tf.
3990c6fd2807SJeff Garzik 			 */
399103faab78STejun Heo 			if (qc->flags & ATA_QCFLAG_RETRY)
3992c6fd2807SJeff Garzik 				ata_eh_qc_retry(qc);
399303faab78STejun Heo 			else
399403faab78STejun Heo 				ata_eh_qc_complete(qc);
3995c6fd2807SJeff Garzik 		} else {
3996c6fd2807SJeff Garzik 			if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
3997c6fd2807SJeff Garzik 				ata_eh_qc_complete(qc);
3998c6fd2807SJeff Garzik 			} else {
3999c6fd2807SJeff Garzik 				/* feed zero TF to sense generation */
4000c6fd2807SJeff Garzik 				memset(&qc->result_tf, 0, sizeof(qc->result_tf));
4001c6fd2807SJeff Garzik 				ata_eh_qc_retry(qc);
4002c6fd2807SJeff Garzik 			}
4003c6fd2807SJeff Garzik 		}
4004c6fd2807SJeff Garzik 	}
4005da917d69STejun Heo 
4006da917d69STejun Heo 	/* make sure nr_active_links is zero after EH */
4007da917d69STejun Heo 	WARN_ON(ap->nr_active_links);
4008da917d69STejun Heo 	ap->nr_active_links = 0;
4009c6fd2807SJeff Garzik }
4010c6fd2807SJeff Garzik 
4011c6fd2807SJeff Garzik /**
4012c6fd2807SJeff Garzik  *	ata_do_eh - do standard error handling
4013c6fd2807SJeff Garzik  *	@ap: host port to handle error for
4014a1efdabaSTejun Heo  *
4015c6fd2807SJeff Garzik  *	@prereset: prereset method (can be NULL)
4016c6fd2807SJeff Garzik  *	@softreset: softreset method (can be NULL)
4017c6fd2807SJeff Garzik  *	@hardreset: hardreset method (can be NULL)
4018c6fd2807SJeff Garzik  *	@postreset: postreset method (can be NULL)
4019c6fd2807SJeff Garzik  *
4020c6fd2807SJeff Garzik  *	Perform standard error handling sequence.
4021c6fd2807SJeff Garzik  *
4022c6fd2807SJeff Garzik  *	LOCKING:
4023c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
4024c6fd2807SJeff Garzik  */
4025c6fd2807SJeff Garzik void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
4026c6fd2807SJeff Garzik 	       ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
4027c6fd2807SJeff Garzik 	       ata_postreset_fn_t postreset)
4028c6fd2807SJeff Garzik {
40299b1e2658STejun Heo 	struct ata_device *dev;
40309b1e2658STejun Heo 	int rc;
40319b1e2658STejun Heo 
40329b1e2658STejun Heo 	ata_eh_autopsy(ap);
40339b1e2658STejun Heo 	ata_eh_report(ap);
40349b1e2658STejun Heo 
40359b1e2658STejun Heo 	rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
40369b1e2658STejun Heo 			    NULL);
40379b1e2658STejun Heo 	if (rc) {
40381eca4365STejun Heo 		ata_for_each_dev(dev, &ap->link, ALL)
40399b1e2658STejun Heo 			ata_dev_disable(dev);
40409b1e2658STejun Heo 	}
40419b1e2658STejun Heo 
4042c6fd2807SJeff Garzik 	ata_eh_finish(ap);
4043c6fd2807SJeff Garzik }
4044*a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_do_eh);
4045c6fd2807SJeff Garzik 
4046a1efdabaSTejun Heo /**
4047a1efdabaSTejun Heo  *	ata_std_error_handler - standard error handler
4048a1efdabaSTejun Heo  *	@ap: host port to handle error for
4049a1efdabaSTejun Heo  *
4050a1efdabaSTejun Heo  *	Standard error handler
4051a1efdabaSTejun Heo  *
4052a1efdabaSTejun Heo  *	LOCKING:
4053a1efdabaSTejun Heo  *	Kernel thread context (may sleep).
4054a1efdabaSTejun Heo  */
4055a1efdabaSTejun Heo void ata_std_error_handler(struct ata_port *ap)
4056a1efdabaSTejun Heo {
4057a1efdabaSTejun Heo 	struct ata_port_operations *ops = ap->ops;
4058a1efdabaSTejun Heo 	ata_reset_fn_t hardreset = ops->hardreset;
4059a1efdabaSTejun Heo 
406057c9efdfSTejun Heo 	/* ignore built-in hardreset if SCR access is not available */
4061fe06e5f9STejun Heo 	if (hardreset == sata_std_hardreset && !sata_scr_valid(&ap->link))
4062a1efdabaSTejun Heo 		hardreset = NULL;
4063a1efdabaSTejun Heo 
4064a1efdabaSTejun Heo 	ata_do_eh(ap, ops->prereset, ops->softreset, hardreset, ops->postreset);
4065a1efdabaSTejun Heo }
4066*a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_std_error_handler);
4067a1efdabaSTejun Heo 
40686ffa01d8STejun Heo #ifdef CONFIG_PM
4069c6fd2807SJeff Garzik /**
4070c6fd2807SJeff Garzik  *	ata_eh_handle_port_suspend - perform port suspend operation
4071c6fd2807SJeff Garzik  *	@ap: port to suspend
4072c6fd2807SJeff Garzik  *
4073c6fd2807SJeff Garzik  *	Suspend @ap.
4074c6fd2807SJeff Garzik  *
4075c6fd2807SJeff Garzik  *	LOCKING:
4076c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
4077c6fd2807SJeff Garzik  */
4078c6fd2807SJeff Garzik static void ata_eh_handle_port_suspend(struct ata_port *ap)
4079c6fd2807SJeff Garzik {
4080c6fd2807SJeff Garzik 	unsigned long flags;
4081c6fd2807SJeff Garzik 	int rc = 0;
40823dc67440SAaron Lu 	struct ata_device *dev;
4083c6fd2807SJeff Garzik 
4084c6fd2807SJeff Garzik 	/* are we suspending? */
4085c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
4086c6fd2807SJeff Garzik 	if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
4087a7ff60dbSAaron Lu 	    ap->pm_mesg.event & PM_EVENT_RESUME) {
4088c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
4089c6fd2807SJeff Garzik 		return;
4090c6fd2807SJeff Garzik 	}
4091c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
4092c6fd2807SJeff Garzik 
4093c6fd2807SJeff Garzik 	WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
4094c6fd2807SJeff Garzik 
40953dc67440SAaron Lu 	/*
40963dc67440SAaron Lu 	 * If we have a ZPODD attached, check its zero
40973dc67440SAaron Lu 	 * power ready status before the port is frozen.
4098a7ff60dbSAaron Lu 	 * Only needed for runtime suspend.
40993dc67440SAaron Lu 	 */
4100a7ff60dbSAaron Lu 	if (PMSG_IS_AUTO(ap->pm_mesg)) {
41013dc67440SAaron Lu 		ata_for_each_dev(dev, &ap->link, ENABLED) {
41023dc67440SAaron Lu 			if (zpodd_dev_enabled(dev))
41033dc67440SAaron Lu 				zpodd_on_suspend(dev);
41043dc67440SAaron Lu 		}
4105a7ff60dbSAaron Lu 	}
41063dc67440SAaron Lu 
410764578a3dSTejun Heo 	/* tell ACPI we're suspending */
410864578a3dSTejun Heo 	rc = ata_acpi_on_suspend(ap);
410964578a3dSTejun Heo 	if (rc)
411064578a3dSTejun Heo 		goto out;
411164578a3dSTejun Heo 
4112c6fd2807SJeff Garzik 	/* suspend */
4113c6fd2807SJeff Garzik 	ata_eh_freeze_port(ap);
4114c6fd2807SJeff Garzik 
4115c6fd2807SJeff Garzik 	if (ap->ops->port_suspend)
4116c6fd2807SJeff Garzik 		rc = ap->ops->port_suspend(ap, ap->pm_mesg);
4117c6fd2807SJeff Garzik 
4118a7ff60dbSAaron Lu 	ata_acpi_set_state(ap, ap->pm_mesg);
411964578a3dSTejun Heo  out:
4120bc6e7c4bSDan Williams 	/* update the flags */
4121c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
4122c6fd2807SJeff Garzik 
4123c6fd2807SJeff Garzik 	ap->pflags &= ~ATA_PFLAG_PM_PENDING;
4124c6fd2807SJeff Garzik 	if (rc == 0)
4125c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_SUSPENDED;
412664578a3dSTejun Heo 	else if (ap->pflags & ATA_PFLAG_FROZEN)
4127c6fd2807SJeff Garzik 		ata_port_schedule_eh(ap);
4128c6fd2807SJeff Garzik 
4129c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
4130c6fd2807SJeff Garzik 
4131c6fd2807SJeff Garzik 	return;
4132c6fd2807SJeff Garzik }
4133c6fd2807SJeff Garzik 
4134c6fd2807SJeff Garzik /**
4135c6fd2807SJeff Garzik  *	ata_eh_handle_port_resume - perform port resume operation
4136c6fd2807SJeff Garzik  *	@ap: port to resume
4137c6fd2807SJeff Garzik  *
4138c6fd2807SJeff Garzik  *	Resume @ap.
4139c6fd2807SJeff Garzik  *
4140c6fd2807SJeff Garzik  *	LOCKING:
4141c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
4142c6fd2807SJeff Garzik  */
4143c6fd2807SJeff Garzik static void ata_eh_handle_port_resume(struct ata_port *ap)
4144c6fd2807SJeff Garzik {
41456f9c1ea2STejun Heo 	struct ata_link *link;
41466f9c1ea2STejun Heo 	struct ata_device *dev;
4147c6fd2807SJeff Garzik 	unsigned long flags;
4148c6fd2807SJeff Garzik 
4149c6fd2807SJeff Garzik 	/* are we resuming? */
4150c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
4151c6fd2807SJeff Garzik 	if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
4152a7ff60dbSAaron Lu 	    !(ap->pm_mesg.event & PM_EVENT_RESUME)) {
4153c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
4154c6fd2807SJeff Garzik 		return;
4155c6fd2807SJeff Garzik 	}
4156c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
4157c6fd2807SJeff Garzik 
41589666f400STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
4159c6fd2807SJeff Garzik 
41606f9c1ea2STejun Heo 	/*
41616f9c1ea2STejun Heo 	 * Error timestamps are in jiffies which doesn't run while
41626f9c1ea2STejun Heo 	 * suspended and PHY events during resume isn't too uncommon.
41636f9c1ea2STejun Heo 	 * When the two are combined, it can lead to unnecessary speed
41646f9c1ea2STejun Heo 	 * downs if the machine is suspended and resumed repeatedly.
41656f9c1ea2STejun Heo 	 * Clear error history.
41666f9c1ea2STejun Heo 	 */
41676f9c1ea2STejun Heo 	ata_for_each_link(link, ap, HOST_FIRST)
41686f9c1ea2STejun Heo 		ata_for_each_dev(dev, link, ALL)
41696f9c1ea2STejun Heo 			ata_ering_clear(&dev->ering);
41706f9c1ea2STejun Heo 
4171a7ff60dbSAaron Lu 	ata_acpi_set_state(ap, ap->pm_mesg);
4172bd3adca5SShaohua Li 
4173c6fd2807SJeff Garzik 	if (ap->ops->port_resume)
4174ae867937SKefeng Wang 		ap->ops->port_resume(ap);
4175c6fd2807SJeff Garzik 
41766746544cSTejun Heo 	/* tell ACPI that we're resuming */
41776746544cSTejun Heo 	ata_acpi_on_resume(ap);
41786746544cSTejun Heo 
4179bc6e7c4bSDan Williams 	/* update the flags */
4180c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
4181c6fd2807SJeff Garzik 	ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
4182c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
4183c6fd2807SJeff Garzik }
41846ffa01d8STejun Heo #endif /* CONFIG_PM */
4185