xref: /openbmc/linux/drivers/ata/libata-eh.c (revision 341c2c958ec7bdd9f54733a8b0b432fe76842a82)
1c6fd2807SJeff Garzik /*
2c6fd2807SJeff Garzik  *  libata-eh.c - libata error handling
3c6fd2807SJeff Garzik  *
4c6fd2807SJeff Garzik  *  Maintained by:  Jeff Garzik <jgarzik@pobox.com>
5c6fd2807SJeff Garzik  *    		    Please ALWAYS copy linux-ide@vger.kernel.org
6c6fd2807SJeff Garzik  *		    on emails.
7c6fd2807SJeff Garzik  *
8c6fd2807SJeff Garzik  *  Copyright 2006 Tejun Heo <htejun@gmail.com>
9c6fd2807SJeff Garzik  *
10c6fd2807SJeff Garzik  *
11c6fd2807SJeff Garzik  *  This program is free software; you can redistribute it and/or
12c6fd2807SJeff Garzik  *  modify it under the terms of the GNU General Public License as
13c6fd2807SJeff Garzik  *  published by the Free Software Foundation; either version 2, or
14c6fd2807SJeff Garzik  *  (at your option) any later version.
15c6fd2807SJeff Garzik  *
16c6fd2807SJeff Garzik  *  This program is distributed in the hope that it will be useful,
17c6fd2807SJeff Garzik  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18c6fd2807SJeff Garzik  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19c6fd2807SJeff Garzik  *  General Public License for more details.
20c6fd2807SJeff Garzik  *
21c6fd2807SJeff Garzik  *  You should have received a copy of the GNU General Public License
22c6fd2807SJeff Garzik  *  along with this program; see the file COPYING.  If not, write to
23c6fd2807SJeff Garzik  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
24c6fd2807SJeff Garzik  *  USA.
25c6fd2807SJeff Garzik  *
26c6fd2807SJeff Garzik  *
27c6fd2807SJeff Garzik  *  libata documentation is available via 'make {ps|pdf}docs',
28c6fd2807SJeff Garzik  *  as Documentation/DocBook/libata.*
29c6fd2807SJeff Garzik  *
30c6fd2807SJeff Garzik  *  Hardware documentation available from http://www.t13.org/ and
31c6fd2807SJeff Garzik  *  http://www.sata-io.org/
32c6fd2807SJeff Garzik  *
33c6fd2807SJeff Garzik  */
34c6fd2807SJeff Garzik 
35c6fd2807SJeff Garzik #include <linux/kernel.h>
362855568bSJeff Garzik #include <linux/pci.h>
37c6fd2807SJeff Garzik #include <scsi/scsi.h>
38c6fd2807SJeff Garzik #include <scsi/scsi_host.h>
39c6fd2807SJeff Garzik #include <scsi/scsi_eh.h>
40c6fd2807SJeff Garzik #include <scsi/scsi_device.h>
41c6fd2807SJeff Garzik #include <scsi/scsi_cmnd.h>
42c6fd2807SJeff Garzik #include "../scsi/scsi_transport_api.h"
43c6fd2807SJeff Garzik 
44c6fd2807SJeff Garzik #include <linux/libata.h>
45c6fd2807SJeff Garzik 
46c6fd2807SJeff Garzik #include "libata.h"
47c6fd2807SJeff Garzik 
487d47e8d4STejun Heo enum {
493884f7b0STejun Heo 	/* speed down verdicts */
507d47e8d4STejun Heo 	ATA_EH_SPDN_NCQ_OFF		= (1 << 0),
517d47e8d4STejun Heo 	ATA_EH_SPDN_SPEED_DOWN		= (1 << 1),
527d47e8d4STejun Heo 	ATA_EH_SPDN_FALLBACK_TO_PIO	= (1 << 2),
5376326ac1STejun Heo 	ATA_EH_SPDN_KEEP_ERRORS		= (1 << 3),
543884f7b0STejun Heo 
553884f7b0STejun Heo 	/* error flags */
563884f7b0STejun Heo 	ATA_EFLAG_IS_IO			= (1 << 0),
5776326ac1STejun Heo 	ATA_EFLAG_DUBIOUS_XFER		= (1 << 1),
583884f7b0STejun Heo 
593884f7b0STejun Heo 	/* error categories */
603884f7b0STejun Heo 	ATA_ECAT_NONE			= 0,
613884f7b0STejun Heo 	ATA_ECAT_ATA_BUS		= 1,
623884f7b0STejun Heo 	ATA_ECAT_TOUT_HSM		= 2,
633884f7b0STejun Heo 	ATA_ECAT_UNK_DEV		= 3,
6475f9cafcSTejun Heo 	ATA_ECAT_DUBIOUS_NONE		= 4,
6575f9cafcSTejun Heo 	ATA_ECAT_DUBIOUS_ATA_BUS	= 5,
6675f9cafcSTejun Heo 	ATA_ECAT_DUBIOUS_TOUT_HSM	= 6,
6775f9cafcSTejun Heo 	ATA_ECAT_DUBIOUS_UNK_DEV	= 7,
6875f9cafcSTejun Heo 	ATA_ECAT_NR			= 8,
697d47e8d4STejun Heo 
70*341c2c95STejun Heo 	/* Waiting in ->prereset can never be reliable.  It's
71*341c2c95STejun Heo 	 * sometimes nice to wait there but it can't be depended upon;
72*341c2c95STejun Heo 	 * otherwise, we wouldn't be resetting.  Just give it enough
73*341c2c95STejun Heo 	 * time for most drives to spin up.
7431daabdaSTejun Heo 	 */
75*341c2c95STejun Heo 	ATA_EH_PRERESET_TIMEOUT		= 10000,
76*341c2c95STejun Heo 	ATA_EH_FASTDRAIN_INTERVAL	=  3000,
7731daabdaSTejun Heo };
7831daabdaSTejun Heo 
7931daabdaSTejun Heo /* The following table determines how we sequence resets.  Each entry
8031daabdaSTejun Heo  * represents timeout for that try.  The first try can be soft or
8131daabdaSTejun Heo  * hardreset.  All others are hardreset if available.  In most cases
8231daabdaSTejun Heo  * the first reset w/ 10sec timeout should succeed.  Following entries
8331daabdaSTejun Heo  * are mostly for error handling, hotplug and retarded devices.
8431daabdaSTejun Heo  */
8531daabdaSTejun Heo static const unsigned long ata_eh_reset_timeouts[] = {
86*341c2c95STejun Heo 	10000,	/* most drives spin up by 10sec */
87*341c2c95STejun Heo 	10000,	/* > 99% working drives spin up before 20sec */
88*341c2c95STejun Heo 	35000,	/* give > 30 secs of idleness for retarded devices */
89*341c2c95STejun Heo 	 5000,	/* and sweet one last chance */
9031daabdaSTejun Heo 	/* > 1 min has elapsed, give up */
9131daabdaSTejun Heo };
9231daabdaSTejun Heo 
93c6fd2807SJeff Garzik static void __ata_port_freeze(struct ata_port *ap);
946ffa01d8STejun Heo #ifdef CONFIG_PM
95c6fd2807SJeff Garzik static void ata_eh_handle_port_suspend(struct ata_port *ap);
96c6fd2807SJeff Garzik static void ata_eh_handle_port_resume(struct ata_port *ap);
976ffa01d8STejun Heo #else /* CONFIG_PM */
986ffa01d8STejun Heo static void ata_eh_handle_port_suspend(struct ata_port *ap)
996ffa01d8STejun Heo { }
1006ffa01d8STejun Heo 
1016ffa01d8STejun Heo static void ata_eh_handle_port_resume(struct ata_port *ap)
1026ffa01d8STejun Heo { }
1036ffa01d8STejun Heo #endif /* CONFIG_PM */
104c6fd2807SJeff Garzik 
105b64bbc39STejun Heo static void __ata_ehi_pushv_desc(struct ata_eh_info *ehi, const char *fmt,
106b64bbc39STejun Heo 				 va_list args)
107b64bbc39STejun Heo {
108b64bbc39STejun Heo 	ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
109b64bbc39STejun Heo 				     ATA_EH_DESC_LEN - ehi->desc_len,
110b64bbc39STejun Heo 				     fmt, args);
111b64bbc39STejun Heo }
112b64bbc39STejun Heo 
113b64bbc39STejun Heo /**
114b64bbc39STejun Heo  *	__ata_ehi_push_desc - push error description without adding separator
115b64bbc39STejun Heo  *	@ehi: target EHI
116b64bbc39STejun Heo  *	@fmt: printf format string
117b64bbc39STejun Heo  *
118b64bbc39STejun Heo  *	Format string according to @fmt and append it to @ehi->desc.
119b64bbc39STejun Heo  *
120b64bbc39STejun Heo  *	LOCKING:
121b64bbc39STejun Heo  *	spin_lock_irqsave(host lock)
122b64bbc39STejun Heo  */
123b64bbc39STejun Heo void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
124b64bbc39STejun Heo {
125b64bbc39STejun Heo 	va_list args;
126b64bbc39STejun Heo 
127b64bbc39STejun Heo 	va_start(args, fmt);
128b64bbc39STejun Heo 	__ata_ehi_pushv_desc(ehi, fmt, args);
129b64bbc39STejun Heo 	va_end(args);
130b64bbc39STejun Heo }
131b64bbc39STejun Heo 
132b64bbc39STejun Heo /**
133b64bbc39STejun Heo  *	ata_ehi_push_desc - push error description with separator
134b64bbc39STejun Heo  *	@ehi: target EHI
135b64bbc39STejun Heo  *	@fmt: printf format string
136b64bbc39STejun Heo  *
137b64bbc39STejun Heo  *	Format string according to @fmt and append it to @ehi->desc.
138b64bbc39STejun Heo  *	If @ehi->desc is not empty, ", " is added in-between.
139b64bbc39STejun Heo  *
140b64bbc39STejun Heo  *	LOCKING:
141b64bbc39STejun Heo  *	spin_lock_irqsave(host lock)
142b64bbc39STejun Heo  */
143b64bbc39STejun Heo void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
144b64bbc39STejun Heo {
145b64bbc39STejun Heo 	va_list args;
146b64bbc39STejun Heo 
147b64bbc39STejun Heo 	if (ehi->desc_len)
148b64bbc39STejun Heo 		__ata_ehi_push_desc(ehi, ", ");
149b64bbc39STejun Heo 
150b64bbc39STejun Heo 	va_start(args, fmt);
151b64bbc39STejun Heo 	__ata_ehi_pushv_desc(ehi, fmt, args);
152b64bbc39STejun Heo 	va_end(args);
153b64bbc39STejun Heo }
154b64bbc39STejun Heo 
155b64bbc39STejun Heo /**
156b64bbc39STejun Heo  *	ata_ehi_clear_desc - clean error description
157b64bbc39STejun Heo  *	@ehi: target EHI
158b64bbc39STejun Heo  *
159b64bbc39STejun Heo  *	Clear @ehi->desc.
160b64bbc39STejun Heo  *
161b64bbc39STejun Heo  *	LOCKING:
162b64bbc39STejun Heo  *	spin_lock_irqsave(host lock)
163b64bbc39STejun Heo  */
164b64bbc39STejun Heo void ata_ehi_clear_desc(struct ata_eh_info *ehi)
165b64bbc39STejun Heo {
166b64bbc39STejun Heo 	ehi->desc[0] = '\0';
167b64bbc39STejun Heo 	ehi->desc_len = 0;
168b64bbc39STejun Heo }
169b64bbc39STejun Heo 
170cbcdd875STejun Heo /**
171cbcdd875STejun Heo  *	ata_port_desc - append port description
172cbcdd875STejun Heo  *	@ap: target ATA port
173cbcdd875STejun Heo  *	@fmt: printf format string
174cbcdd875STejun Heo  *
175cbcdd875STejun Heo  *	Format string according to @fmt and append it to port
176cbcdd875STejun Heo  *	description.  If port description is not empty, " " is added
177cbcdd875STejun Heo  *	in-between.  This function is to be used while initializing
178cbcdd875STejun Heo  *	ata_host.  The description is printed on host registration.
179cbcdd875STejun Heo  *
180cbcdd875STejun Heo  *	LOCKING:
181cbcdd875STejun Heo  *	None.
182cbcdd875STejun Heo  */
183cbcdd875STejun Heo void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
184cbcdd875STejun Heo {
185cbcdd875STejun Heo 	va_list args;
186cbcdd875STejun Heo 
187cbcdd875STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
188cbcdd875STejun Heo 
189cbcdd875STejun Heo 	if (ap->link.eh_info.desc_len)
190cbcdd875STejun Heo 		__ata_ehi_push_desc(&ap->link.eh_info, " ");
191cbcdd875STejun Heo 
192cbcdd875STejun Heo 	va_start(args, fmt);
193cbcdd875STejun Heo 	__ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
194cbcdd875STejun Heo 	va_end(args);
195cbcdd875STejun Heo }
196cbcdd875STejun Heo 
197cbcdd875STejun Heo #ifdef CONFIG_PCI
198cbcdd875STejun Heo 
199cbcdd875STejun Heo /**
200cbcdd875STejun Heo  *	ata_port_pbar_desc - append PCI BAR description
201cbcdd875STejun Heo  *	@ap: target ATA port
202cbcdd875STejun Heo  *	@bar: target PCI BAR
203cbcdd875STejun Heo  *	@offset: offset into PCI BAR
204cbcdd875STejun Heo  *	@name: name of the area
205cbcdd875STejun Heo  *
206cbcdd875STejun Heo  *	If @offset is negative, this function formats a string which
207cbcdd875STejun Heo  *	contains the name, address, size and type of the BAR and
208cbcdd875STejun Heo  *	appends it to the port description.  If @offset is zero or
209cbcdd875STejun Heo  *	positive, only name and offsetted address is appended.
210cbcdd875STejun Heo  *
211cbcdd875STejun Heo  *	LOCKING:
212cbcdd875STejun Heo  *	None.
213cbcdd875STejun Heo  */
214cbcdd875STejun Heo void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
215cbcdd875STejun Heo 			const char *name)
216cbcdd875STejun Heo {
217cbcdd875STejun Heo 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
218cbcdd875STejun Heo 	char *type = "";
219cbcdd875STejun Heo 	unsigned long long start, len;
220cbcdd875STejun Heo 
221cbcdd875STejun Heo 	if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
222cbcdd875STejun Heo 		type = "m";
223cbcdd875STejun Heo 	else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
224cbcdd875STejun Heo 		type = "i";
225cbcdd875STejun Heo 
226cbcdd875STejun Heo 	start = (unsigned long long)pci_resource_start(pdev, bar);
227cbcdd875STejun Heo 	len = (unsigned long long)pci_resource_len(pdev, bar);
228cbcdd875STejun Heo 
229cbcdd875STejun Heo 	if (offset < 0)
230cbcdd875STejun Heo 		ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
231cbcdd875STejun Heo 	else
232e6a73ab1SAndrew Morton 		ata_port_desc(ap, "%s 0x%llx", name,
233e6a73ab1SAndrew Morton 				start + (unsigned long long)offset);
234cbcdd875STejun Heo }
235cbcdd875STejun Heo 
236cbcdd875STejun Heo #endif /* CONFIG_PCI */
237cbcdd875STejun Heo 
2383884f7b0STejun Heo static void ata_ering_record(struct ata_ering *ering, unsigned int eflags,
239c6fd2807SJeff Garzik 			     unsigned int err_mask)
240c6fd2807SJeff Garzik {
241c6fd2807SJeff Garzik 	struct ata_ering_entry *ent;
242c6fd2807SJeff Garzik 
243c6fd2807SJeff Garzik 	WARN_ON(!err_mask);
244c6fd2807SJeff Garzik 
245c6fd2807SJeff Garzik 	ering->cursor++;
246c6fd2807SJeff Garzik 	ering->cursor %= ATA_ERING_SIZE;
247c6fd2807SJeff Garzik 
248c6fd2807SJeff Garzik 	ent = &ering->ring[ering->cursor];
2493884f7b0STejun Heo 	ent->eflags = eflags;
250c6fd2807SJeff Garzik 	ent->err_mask = err_mask;
251c6fd2807SJeff Garzik 	ent->timestamp = get_jiffies_64();
252c6fd2807SJeff Garzik }
253c6fd2807SJeff Garzik 
25476326ac1STejun Heo static struct ata_ering_entry *ata_ering_top(struct ata_ering *ering)
25576326ac1STejun Heo {
25676326ac1STejun Heo 	struct ata_ering_entry *ent = &ering->ring[ering->cursor];
25776326ac1STejun Heo 
25876326ac1STejun Heo 	if (ent->err_mask)
25976326ac1STejun Heo 		return ent;
26076326ac1STejun Heo 	return NULL;
26176326ac1STejun Heo }
26276326ac1STejun Heo 
2637d47e8d4STejun Heo static void ata_ering_clear(struct ata_ering *ering)
264c6fd2807SJeff Garzik {
2657d47e8d4STejun Heo 	memset(ering, 0, sizeof(*ering));
266c6fd2807SJeff Garzik }
267c6fd2807SJeff Garzik 
268c6fd2807SJeff Garzik static int ata_ering_map(struct ata_ering *ering,
269c6fd2807SJeff Garzik 			 int (*map_fn)(struct ata_ering_entry *, void *),
270c6fd2807SJeff Garzik 			 void *arg)
271c6fd2807SJeff Garzik {
272c6fd2807SJeff Garzik 	int idx, rc = 0;
273c6fd2807SJeff Garzik 	struct ata_ering_entry *ent;
274c6fd2807SJeff Garzik 
275c6fd2807SJeff Garzik 	idx = ering->cursor;
276c6fd2807SJeff Garzik 	do {
277c6fd2807SJeff Garzik 		ent = &ering->ring[idx];
278c6fd2807SJeff Garzik 		if (!ent->err_mask)
279c6fd2807SJeff Garzik 			break;
280c6fd2807SJeff Garzik 		rc = map_fn(ent, arg);
281c6fd2807SJeff Garzik 		if (rc)
282c6fd2807SJeff Garzik 			break;
283c6fd2807SJeff Garzik 		idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
284c6fd2807SJeff Garzik 	} while (idx != ering->cursor);
285c6fd2807SJeff Garzik 
286c6fd2807SJeff Garzik 	return rc;
287c6fd2807SJeff Garzik }
288c6fd2807SJeff Garzik 
289c6fd2807SJeff Garzik static unsigned int ata_eh_dev_action(struct ata_device *dev)
290c6fd2807SJeff Garzik {
2919af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
292c6fd2807SJeff Garzik 
293c6fd2807SJeff Garzik 	return ehc->i.action | ehc->i.dev_action[dev->devno];
294c6fd2807SJeff Garzik }
295c6fd2807SJeff Garzik 
296f58229f8STejun Heo static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
297c6fd2807SJeff Garzik 				struct ata_eh_info *ehi, unsigned int action)
298c6fd2807SJeff Garzik {
299f58229f8STejun Heo 	struct ata_device *tdev;
300c6fd2807SJeff Garzik 
301c6fd2807SJeff Garzik 	if (!dev) {
302c6fd2807SJeff Garzik 		ehi->action &= ~action;
303f58229f8STejun Heo 		ata_link_for_each_dev(tdev, link)
304f58229f8STejun Heo 			ehi->dev_action[tdev->devno] &= ~action;
305c6fd2807SJeff Garzik 	} else {
306c6fd2807SJeff Garzik 		/* doesn't make sense for port-wide EH actions */
307c6fd2807SJeff Garzik 		WARN_ON(!(action & ATA_EH_PERDEV_MASK));
308c6fd2807SJeff Garzik 
309c6fd2807SJeff Garzik 		/* break ehi->action into ehi->dev_action */
310c6fd2807SJeff Garzik 		if (ehi->action & action) {
311f58229f8STejun Heo 			ata_link_for_each_dev(tdev, link)
312f58229f8STejun Heo 				ehi->dev_action[tdev->devno] |=
313f58229f8STejun Heo 					ehi->action & action;
314c6fd2807SJeff Garzik 			ehi->action &= ~action;
315c6fd2807SJeff Garzik 		}
316c6fd2807SJeff Garzik 
317c6fd2807SJeff Garzik 		/* turn off the specified per-dev action */
318c6fd2807SJeff Garzik 		ehi->dev_action[dev->devno] &= ~action;
319c6fd2807SJeff Garzik 	}
320c6fd2807SJeff Garzik }
321c6fd2807SJeff Garzik 
322c6fd2807SJeff Garzik /**
323c6fd2807SJeff Garzik  *	ata_scsi_timed_out - SCSI layer time out callback
324c6fd2807SJeff Garzik  *	@cmd: timed out SCSI command
325c6fd2807SJeff Garzik  *
326c6fd2807SJeff Garzik  *	Handles SCSI layer timeout.  We race with normal completion of
327c6fd2807SJeff Garzik  *	the qc for @cmd.  If the qc is already gone, we lose and let
328c6fd2807SJeff Garzik  *	the scsi command finish (EH_HANDLED).  Otherwise, the qc has
329c6fd2807SJeff Garzik  *	timed out and EH should be invoked.  Prevent ata_qc_complete()
330c6fd2807SJeff Garzik  *	from finishing it by setting EH_SCHEDULED and return
331c6fd2807SJeff Garzik  *	EH_NOT_HANDLED.
332c6fd2807SJeff Garzik  *
333c6fd2807SJeff Garzik  *	TODO: kill this function once old EH is gone.
334c6fd2807SJeff Garzik  *
335c6fd2807SJeff Garzik  *	LOCKING:
336c6fd2807SJeff Garzik  *	Called from timer context
337c6fd2807SJeff Garzik  *
338c6fd2807SJeff Garzik  *	RETURNS:
339c6fd2807SJeff Garzik  *	EH_HANDLED or EH_NOT_HANDLED
340c6fd2807SJeff Garzik  */
341c6fd2807SJeff Garzik enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
342c6fd2807SJeff Garzik {
343c6fd2807SJeff Garzik 	struct Scsi_Host *host = cmd->device->host;
344c6fd2807SJeff Garzik 	struct ata_port *ap = ata_shost_to_port(host);
345c6fd2807SJeff Garzik 	unsigned long flags;
346c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
347c6fd2807SJeff Garzik 	enum scsi_eh_timer_return ret;
348c6fd2807SJeff Garzik 
349c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
350c6fd2807SJeff Garzik 
351c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
352c6fd2807SJeff Garzik 		ret = EH_NOT_HANDLED;
353c6fd2807SJeff Garzik 		goto out;
354c6fd2807SJeff Garzik 	}
355c6fd2807SJeff Garzik 
356c6fd2807SJeff Garzik 	ret = EH_HANDLED;
357c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
3589af5c9c9STejun Heo 	qc = ata_qc_from_tag(ap, ap->link.active_tag);
359c6fd2807SJeff Garzik 	if (qc) {
360c6fd2807SJeff Garzik 		WARN_ON(qc->scsicmd != cmd);
361c6fd2807SJeff Garzik 		qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
362c6fd2807SJeff Garzik 		qc->err_mask |= AC_ERR_TIMEOUT;
363c6fd2807SJeff Garzik 		ret = EH_NOT_HANDLED;
364c6fd2807SJeff Garzik 	}
365c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
366c6fd2807SJeff Garzik 
367c6fd2807SJeff Garzik  out:
368c6fd2807SJeff Garzik 	DPRINTK("EXIT, ret=%d\n", ret);
369c6fd2807SJeff Garzik 	return ret;
370c6fd2807SJeff Garzik }
371c6fd2807SJeff Garzik 
372c6fd2807SJeff Garzik /**
373c6fd2807SJeff Garzik  *	ata_scsi_error - SCSI layer error handler callback
374c6fd2807SJeff Garzik  *	@host: SCSI host on which error occurred
375c6fd2807SJeff Garzik  *
376c6fd2807SJeff Garzik  *	Handles SCSI-layer-thrown error events.
377c6fd2807SJeff Garzik  *
378c6fd2807SJeff Garzik  *	LOCKING:
379c6fd2807SJeff Garzik  *	Inherited from SCSI layer (none, can sleep)
380c6fd2807SJeff Garzik  *
381c6fd2807SJeff Garzik  *	RETURNS:
382c6fd2807SJeff Garzik  *	Zero.
383c6fd2807SJeff Garzik  */
384c6fd2807SJeff Garzik void ata_scsi_error(struct Scsi_Host *host)
385c6fd2807SJeff Garzik {
386c6fd2807SJeff Garzik 	struct ata_port *ap = ata_shost_to_port(host);
387a1e10f7eSTejun Heo 	int i;
388c6fd2807SJeff Garzik 	unsigned long flags;
389c6fd2807SJeff Garzik 
390c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
391c6fd2807SJeff Garzik 
392c6fd2807SJeff Garzik 	/* synchronize with port task */
393c6fd2807SJeff Garzik 	ata_port_flush_task(ap);
394c6fd2807SJeff Garzik 
395cca3974eSJeff Garzik 	/* synchronize with host lock and sort out timeouts */
396c6fd2807SJeff Garzik 
397c6fd2807SJeff Garzik 	/* For new EH, all qcs are finished in one of three ways -
398c6fd2807SJeff Garzik 	 * normal completion, error completion, and SCSI timeout.
399c6fd2807SJeff Garzik 	 * Both cmpletions can race against SCSI timeout.  When normal
400c6fd2807SJeff Garzik 	 * completion wins, the qc never reaches EH.  When error
401c6fd2807SJeff Garzik 	 * completion wins, the qc has ATA_QCFLAG_FAILED set.
402c6fd2807SJeff Garzik 	 *
403c6fd2807SJeff Garzik 	 * When SCSI timeout wins, things are a bit more complex.
404c6fd2807SJeff Garzik 	 * Normal or error completion can occur after the timeout but
405c6fd2807SJeff Garzik 	 * before this point.  In such cases, both types of
406c6fd2807SJeff Garzik 	 * completions are honored.  A scmd is determined to have
407c6fd2807SJeff Garzik 	 * timed out iff its associated qc is active and not failed.
408c6fd2807SJeff Garzik 	 */
409c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
410c6fd2807SJeff Garzik 		struct scsi_cmnd *scmd, *tmp;
411c6fd2807SJeff Garzik 		int nr_timedout = 0;
412c6fd2807SJeff Garzik 
413c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
414c6fd2807SJeff Garzik 
415c6fd2807SJeff Garzik 		list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
416c6fd2807SJeff Garzik 			struct ata_queued_cmd *qc;
417c6fd2807SJeff Garzik 
418c6fd2807SJeff Garzik 			for (i = 0; i < ATA_MAX_QUEUE; i++) {
419c6fd2807SJeff Garzik 				qc = __ata_qc_from_tag(ap, i);
420c6fd2807SJeff Garzik 				if (qc->flags & ATA_QCFLAG_ACTIVE &&
421c6fd2807SJeff Garzik 				    qc->scsicmd == scmd)
422c6fd2807SJeff Garzik 					break;
423c6fd2807SJeff Garzik 			}
424c6fd2807SJeff Garzik 
425c6fd2807SJeff Garzik 			if (i < ATA_MAX_QUEUE) {
426c6fd2807SJeff Garzik 				/* the scmd has an associated qc */
427c6fd2807SJeff Garzik 				if (!(qc->flags & ATA_QCFLAG_FAILED)) {
428c6fd2807SJeff Garzik 					/* which hasn't failed yet, timeout */
429c6fd2807SJeff Garzik 					qc->err_mask |= AC_ERR_TIMEOUT;
430c6fd2807SJeff Garzik 					qc->flags |= ATA_QCFLAG_FAILED;
431c6fd2807SJeff Garzik 					nr_timedout++;
432c6fd2807SJeff Garzik 				}
433c6fd2807SJeff Garzik 			} else {
434c6fd2807SJeff Garzik 				/* Normal completion occurred after
435c6fd2807SJeff Garzik 				 * SCSI timeout but before this point.
436c6fd2807SJeff Garzik 				 * Successfully complete it.
437c6fd2807SJeff Garzik 				 */
438c6fd2807SJeff Garzik 				scmd->retries = scmd->allowed;
439c6fd2807SJeff Garzik 				scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
440c6fd2807SJeff Garzik 			}
441c6fd2807SJeff Garzik 		}
442c6fd2807SJeff Garzik 
443c6fd2807SJeff Garzik 		/* If we have timed out qcs.  They belong to EH from
444c6fd2807SJeff Garzik 		 * this point but the state of the controller is
445c6fd2807SJeff Garzik 		 * unknown.  Freeze the port to make sure the IRQ
446c6fd2807SJeff Garzik 		 * handler doesn't diddle with those qcs.  This must
447c6fd2807SJeff Garzik 		 * be done atomically w.r.t. setting QCFLAG_FAILED.
448c6fd2807SJeff Garzik 		 */
449c6fd2807SJeff Garzik 		if (nr_timedout)
450c6fd2807SJeff Garzik 			__ata_port_freeze(ap);
451c6fd2807SJeff Garzik 
452c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
453a1e10f7eSTejun Heo 
454a1e10f7eSTejun Heo 		/* initialize eh_tries */
455a1e10f7eSTejun Heo 		ap->eh_tries = ATA_EH_MAX_TRIES;
456c6fd2807SJeff Garzik 	} else
457c6fd2807SJeff Garzik 		spin_unlock_wait(ap->lock);
458c6fd2807SJeff Garzik 
459c6fd2807SJeff Garzik  repeat:
460c6fd2807SJeff Garzik 	/* invoke error handler */
461c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
462cf1b86c8STejun Heo 		struct ata_link *link;
463cf1b86c8STejun Heo 
4645ddf24c5STejun Heo 		/* kill fast drain timer */
4655ddf24c5STejun Heo 		del_timer_sync(&ap->fastdrain_timer);
4665ddf24c5STejun Heo 
467c6fd2807SJeff Garzik 		/* process port resume request */
468c6fd2807SJeff Garzik 		ata_eh_handle_port_resume(ap);
469c6fd2807SJeff Garzik 
470c6fd2807SJeff Garzik 		/* fetch & clear EH info */
471c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
472c6fd2807SJeff Garzik 
473cf1b86c8STejun Heo 		__ata_port_for_each_link(link, ap) {
47400115e0fSTejun Heo 			struct ata_eh_context *ehc = &link->eh_context;
47500115e0fSTejun Heo 			struct ata_device *dev;
47600115e0fSTejun Heo 
477cf1b86c8STejun Heo 			memset(&link->eh_context, 0, sizeof(link->eh_context));
478cf1b86c8STejun Heo 			link->eh_context.i = link->eh_info;
479cf1b86c8STejun Heo 			memset(&link->eh_info, 0, sizeof(link->eh_info));
48000115e0fSTejun Heo 
48100115e0fSTejun Heo 			ata_link_for_each_dev(dev, link) {
48200115e0fSTejun Heo 				int devno = dev->devno;
48300115e0fSTejun Heo 
48400115e0fSTejun Heo 				ehc->saved_xfer_mode[devno] = dev->xfer_mode;
48500115e0fSTejun Heo 				if (ata_ncq_enabled(dev))
48600115e0fSTejun Heo 					ehc->saved_ncq_enabled |= 1 << devno;
48700115e0fSTejun Heo 			}
488cf1b86c8STejun Heo 		}
489c6fd2807SJeff Garzik 
490c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
491c6fd2807SJeff Garzik 		ap->pflags &= ~ATA_PFLAG_EH_PENDING;
492da917d69STejun Heo 		ap->excl_link = NULL;	/* don't maintain exclusion over EH */
493c6fd2807SJeff Garzik 
494c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
495c6fd2807SJeff Garzik 
496c6fd2807SJeff Garzik 		/* invoke EH, skip if unloading or suspended */
497c6fd2807SJeff Garzik 		if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
498c6fd2807SJeff Garzik 			ap->ops->error_handler(ap);
499c6fd2807SJeff Garzik 		else
500c6fd2807SJeff Garzik 			ata_eh_finish(ap);
501c6fd2807SJeff Garzik 
502c6fd2807SJeff Garzik 		/* process port suspend request */
503c6fd2807SJeff Garzik 		ata_eh_handle_port_suspend(ap);
504c6fd2807SJeff Garzik 
505c6fd2807SJeff Garzik 		/* Exception might have happend after ->error_handler
506c6fd2807SJeff Garzik 		 * recovered the port but before this point.  Repeat
507c6fd2807SJeff Garzik 		 * EH in such case.
508c6fd2807SJeff Garzik 		 */
509c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
510c6fd2807SJeff Garzik 
511c6fd2807SJeff Garzik 		if (ap->pflags & ATA_PFLAG_EH_PENDING) {
512a1e10f7eSTejun Heo 			if (--ap->eh_tries) {
513c6fd2807SJeff Garzik 				spin_unlock_irqrestore(ap->lock, flags);
514c6fd2807SJeff Garzik 				goto repeat;
515c6fd2807SJeff Garzik 			}
516c6fd2807SJeff Garzik 			ata_port_printk(ap, KERN_ERR, "EH pending after %d "
517a1e10f7eSTejun Heo 					"tries, giving up\n", ATA_EH_MAX_TRIES);
518914616a3STejun Heo 			ap->pflags &= ~ATA_PFLAG_EH_PENDING;
519c6fd2807SJeff Garzik 		}
520c6fd2807SJeff Garzik 
521c6fd2807SJeff Garzik 		/* this run is complete, make sure EH info is clear */
522cf1b86c8STejun Heo 		__ata_port_for_each_link(link, ap)
523cf1b86c8STejun Heo 			memset(&link->eh_info, 0, sizeof(link->eh_info));
524c6fd2807SJeff Garzik 
525c6fd2807SJeff Garzik 		/* Clear host_eh_scheduled while holding ap->lock such
526c6fd2807SJeff Garzik 		 * that if exception occurs after this point but
527c6fd2807SJeff Garzik 		 * before EH completion, SCSI midlayer will
528c6fd2807SJeff Garzik 		 * re-initiate EH.
529c6fd2807SJeff Garzik 		 */
530c6fd2807SJeff Garzik 		host->host_eh_scheduled = 0;
531c6fd2807SJeff Garzik 
532c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
533c6fd2807SJeff Garzik 	} else {
5349af5c9c9STejun Heo 		WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
535c6fd2807SJeff Garzik 		ap->ops->eng_timeout(ap);
536c6fd2807SJeff Garzik 	}
537c6fd2807SJeff Garzik 
538c6fd2807SJeff Garzik 	/* finish or retry handled scmd's and clean up */
539c6fd2807SJeff Garzik 	WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
540c6fd2807SJeff Garzik 
541c6fd2807SJeff Garzik 	scsi_eh_flush_done_q(&ap->eh_done_q);
542c6fd2807SJeff Garzik 
543c6fd2807SJeff Garzik 	/* clean up */
544c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
545c6fd2807SJeff Garzik 
546c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_LOADING)
547c6fd2807SJeff Garzik 		ap->pflags &= ~ATA_PFLAG_LOADING;
548c6fd2807SJeff Garzik 	else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
54952bad64dSDavid Howells 		queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0);
550c6fd2807SJeff Garzik 
551c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_RECOVERED)
552c6fd2807SJeff Garzik 		ata_port_printk(ap, KERN_INFO, "EH complete\n");
553c6fd2807SJeff Garzik 
554c6fd2807SJeff Garzik 	ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
555c6fd2807SJeff Garzik 
556c6fd2807SJeff Garzik 	/* tell wait_eh that we're done */
557c6fd2807SJeff Garzik 	ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
558c6fd2807SJeff Garzik 	wake_up_all(&ap->eh_wait_q);
559c6fd2807SJeff Garzik 
560c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
561c6fd2807SJeff Garzik 
562c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
563c6fd2807SJeff Garzik }
564c6fd2807SJeff Garzik 
565c6fd2807SJeff Garzik /**
566c6fd2807SJeff Garzik  *	ata_port_wait_eh - Wait for the currently pending EH to complete
567c6fd2807SJeff Garzik  *	@ap: Port to wait EH for
568c6fd2807SJeff Garzik  *
569c6fd2807SJeff Garzik  *	Wait until the currently pending EH is complete.
570c6fd2807SJeff Garzik  *
571c6fd2807SJeff Garzik  *	LOCKING:
572c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
573c6fd2807SJeff Garzik  */
574c6fd2807SJeff Garzik void ata_port_wait_eh(struct ata_port *ap)
575c6fd2807SJeff Garzik {
576c6fd2807SJeff Garzik 	unsigned long flags;
577c6fd2807SJeff Garzik 	DEFINE_WAIT(wait);
578c6fd2807SJeff Garzik 
579c6fd2807SJeff Garzik  retry:
580c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
581c6fd2807SJeff Garzik 
582c6fd2807SJeff Garzik 	while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
583c6fd2807SJeff Garzik 		prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
584c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
585c6fd2807SJeff Garzik 		schedule();
586c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
587c6fd2807SJeff Garzik 	}
588c6fd2807SJeff Garzik 	finish_wait(&ap->eh_wait_q, &wait);
589c6fd2807SJeff Garzik 
590c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
591c6fd2807SJeff Garzik 
592c6fd2807SJeff Garzik 	/* make sure SCSI EH is complete */
593cca3974eSJeff Garzik 	if (scsi_host_in_recovery(ap->scsi_host)) {
594c6fd2807SJeff Garzik 		msleep(10);
595c6fd2807SJeff Garzik 		goto retry;
596c6fd2807SJeff Garzik 	}
597c6fd2807SJeff Garzik }
598c6fd2807SJeff Garzik 
5995ddf24c5STejun Heo static int ata_eh_nr_in_flight(struct ata_port *ap)
6005ddf24c5STejun Heo {
6015ddf24c5STejun Heo 	unsigned int tag;
6025ddf24c5STejun Heo 	int nr = 0;
6035ddf24c5STejun Heo 
6045ddf24c5STejun Heo 	/* count only non-internal commands */
6055ddf24c5STejun Heo 	for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++)
6065ddf24c5STejun Heo 		if (ata_qc_from_tag(ap, tag))
6075ddf24c5STejun Heo 			nr++;
6085ddf24c5STejun Heo 
6095ddf24c5STejun Heo 	return nr;
6105ddf24c5STejun Heo }
6115ddf24c5STejun Heo 
6125ddf24c5STejun Heo void ata_eh_fastdrain_timerfn(unsigned long arg)
6135ddf24c5STejun Heo {
6145ddf24c5STejun Heo 	struct ata_port *ap = (void *)arg;
6155ddf24c5STejun Heo 	unsigned long flags;
6165ddf24c5STejun Heo 	int cnt;
6175ddf24c5STejun Heo 
6185ddf24c5STejun Heo 	spin_lock_irqsave(ap->lock, flags);
6195ddf24c5STejun Heo 
6205ddf24c5STejun Heo 	cnt = ata_eh_nr_in_flight(ap);
6215ddf24c5STejun Heo 
6225ddf24c5STejun Heo 	/* are we done? */
6235ddf24c5STejun Heo 	if (!cnt)
6245ddf24c5STejun Heo 		goto out_unlock;
6255ddf24c5STejun Heo 
6265ddf24c5STejun Heo 	if (cnt == ap->fastdrain_cnt) {
6275ddf24c5STejun Heo 		unsigned int tag;
6285ddf24c5STejun Heo 
6295ddf24c5STejun Heo 		/* No progress during the last interval, tag all
6305ddf24c5STejun Heo 		 * in-flight qcs as timed out and freeze the port.
6315ddf24c5STejun Heo 		 */
6325ddf24c5STejun Heo 		for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) {
6335ddf24c5STejun Heo 			struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
6345ddf24c5STejun Heo 			if (qc)
6355ddf24c5STejun Heo 				qc->err_mask |= AC_ERR_TIMEOUT;
6365ddf24c5STejun Heo 		}
6375ddf24c5STejun Heo 
6385ddf24c5STejun Heo 		ata_port_freeze(ap);
6395ddf24c5STejun Heo 	} else {
6405ddf24c5STejun Heo 		/* some qcs have finished, give it another chance */
6415ddf24c5STejun Heo 		ap->fastdrain_cnt = cnt;
6425ddf24c5STejun Heo 		ap->fastdrain_timer.expires =
643*341c2c95STejun Heo 			ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
6445ddf24c5STejun Heo 		add_timer(&ap->fastdrain_timer);
6455ddf24c5STejun Heo 	}
6465ddf24c5STejun Heo 
6475ddf24c5STejun Heo  out_unlock:
6485ddf24c5STejun Heo 	spin_unlock_irqrestore(ap->lock, flags);
6495ddf24c5STejun Heo }
6505ddf24c5STejun Heo 
6515ddf24c5STejun Heo /**
6525ddf24c5STejun Heo  *	ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
6535ddf24c5STejun Heo  *	@ap: target ATA port
6545ddf24c5STejun Heo  *	@fastdrain: activate fast drain
6555ddf24c5STejun Heo  *
6565ddf24c5STejun Heo  *	Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
6575ddf24c5STejun Heo  *	is non-zero and EH wasn't pending before.  Fast drain ensures
6585ddf24c5STejun Heo  *	that EH kicks in in timely manner.
6595ddf24c5STejun Heo  *
6605ddf24c5STejun Heo  *	LOCKING:
6615ddf24c5STejun Heo  *	spin_lock_irqsave(host lock)
6625ddf24c5STejun Heo  */
6635ddf24c5STejun Heo static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
6645ddf24c5STejun Heo {
6655ddf24c5STejun Heo 	int cnt;
6665ddf24c5STejun Heo 
6675ddf24c5STejun Heo 	/* already scheduled? */
6685ddf24c5STejun Heo 	if (ap->pflags & ATA_PFLAG_EH_PENDING)
6695ddf24c5STejun Heo 		return;
6705ddf24c5STejun Heo 
6715ddf24c5STejun Heo 	ap->pflags |= ATA_PFLAG_EH_PENDING;
6725ddf24c5STejun Heo 
6735ddf24c5STejun Heo 	if (!fastdrain)
6745ddf24c5STejun Heo 		return;
6755ddf24c5STejun Heo 
6765ddf24c5STejun Heo 	/* do we have in-flight qcs? */
6775ddf24c5STejun Heo 	cnt = ata_eh_nr_in_flight(ap);
6785ddf24c5STejun Heo 	if (!cnt)
6795ddf24c5STejun Heo 		return;
6805ddf24c5STejun Heo 
6815ddf24c5STejun Heo 	/* activate fast drain */
6825ddf24c5STejun Heo 	ap->fastdrain_cnt = cnt;
683*341c2c95STejun Heo 	ap->fastdrain_timer.expires =
684*341c2c95STejun Heo 		ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
6855ddf24c5STejun Heo 	add_timer(&ap->fastdrain_timer);
6865ddf24c5STejun Heo }
6875ddf24c5STejun Heo 
688c6fd2807SJeff Garzik /**
689c6fd2807SJeff Garzik  *	ata_qc_schedule_eh - schedule qc for error handling
690c6fd2807SJeff Garzik  *	@qc: command to schedule error handling for
691c6fd2807SJeff Garzik  *
692c6fd2807SJeff Garzik  *	Schedule error handling for @qc.  EH will kick in as soon as
693c6fd2807SJeff Garzik  *	other commands are drained.
694c6fd2807SJeff Garzik  *
695c6fd2807SJeff Garzik  *	LOCKING:
696cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
697c6fd2807SJeff Garzik  */
698c6fd2807SJeff Garzik void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
699c6fd2807SJeff Garzik {
700c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
701c6fd2807SJeff Garzik 
702c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
703c6fd2807SJeff Garzik 
704c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_FAILED;
7055ddf24c5STejun Heo 	ata_eh_set_pending(ap, 1);
706c6fd2807SJeff Garzik 
707c6fd2807SJeff Garzik 	/* The following will fail if timeout has already expired.
708c6fd2807SJeff Garzik 	 * ata_scsi_error() takes care of such scmds on EH entry.
709c6fd2807SJeff Garzik 	 * Note that ATA_QCFLAG_FAILED is unconditionally set after
710c6fd2807SJeff Garzik 	 * this function completes.
711c6fd2807SJeff Garzik 	 */
712c6fd2807SJeff Garzik 	scsi_req_abort_cmd(qc->scsicmd);
713c6fd2807SJeff Garzik }
714c6fd2807SJeff Garzik 
715c6fd2807SJeff Garzik /**
716c6fd2807SJeff Garzik  *	ata_port_schedule_eh - schedule error handling without a qc
717c6fd2807SJeff Garzik  *	@ap: ATA port to schedule EH for
718c6fd2807SJeff Garzik  *
719c6fd2807SJeff Garzik  *	Schedule error handling for @ap.  EH will kick in as soon as
720c6fd2807SJeff Garzik  *	all commands are drained.
721c6fd2807SJeff Garzik  *
722c6fd2807SJeff Garzik  *	LOCKING:
723cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
724c6fd2807SJeff Garzik  */
725c6fd2807SJeff Garzik void ata_port_schedule_eh(struct ata_port *ap)
726c6fd2807SJeff Garzik {
727c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
728c6fd2807SJeff Garzik 
729f4d6d004STejun Heo 	if (ap->pflags & ATA_PFLAG_INITIALIZING)
730f4d6d004STejun Heo 		return;
731f4d6d004STejun Heo 
7325ddf24c5STejun Heo 	ata_eh_set_pending(ap, 1);
733cca3974eSJeff Garzik 	scsi_schedule_eh(ap->scsi_host);
734c6fd2807SJeff Garzik 
735c6fd2807SJeff Garzik 	DPRINTK("port EH scheduled\n");
736c6fd2807SJeff Garzik }
737c6fd2807SJeff Garzik 
738dbd82616STejun Heo static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
739c6fd2807SJeff Garzik {
740c6fd2807SJeff Garzik 	int tag, nr_aborted = 0;
741c6fd2807SJeff Garzik 
742c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
743c6fd2807SJeff Garzik 
7445ddf24c5STejun Heo 	/* we're gonna abort all commands, no need for fast drain */
7455ddf24c5STejun Heo 	ata_eh_set_pending(ap, 0);
7465ddf24c5STejun Heo 
747c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
748c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
749c6fd2807SJeff Garzik 
750dbd82616STejun Heo 		if (qc && (!link || qc->dev->link == link)) {
751c6fd2807SJeff Garzik 			qc->flags |= ATA_QCFLAG_FAILED;
752c6fd2807SJeff Garzik 			ata_qc_complete(qc);
753c6fd2807SJeff Garzik 			nr_aborted++;
754c6fd2807SJeff Garzik 		}
755c6fd2807SJeff Garzik 	}
756c6fd2807SJeff Garzik 
757c6fd2807SJeff Garzik 	if (!nr_aborted)
758c6fd2807SJeff Garzik 		ata_port_schedule_eh(ap);
759c6fd2807SJeff Garzik 
760c6fd2807SJeff Garzik 	return nr_aborted;
761c6fd2807SJeff Garzik }
762c6fd2807SJeff Garzik 
763c6fd2807SJeff Garzik /**
764dbd82616STejun Heo  *	ata_link_abort - abort all qc's on the link
765dbd82616STejun Heo  *	@link: ATA link to abort qc's for
766dbd82616STejun Heo  *
767dbd82616STejun Heo  *	Abort all active qc's active on @link and schedule EH.
768dbd82616STejun Heo  *
769dbd82616STejun Heo  *	LOCKING:
770dbd82616STejun Heo  *	spin_lock_irqsave(host lock)
771dbd82616STejun Heo  *
772dbd82616STejun Heo  *	RETURNS:
773dbd82616STejun Heo  *	Number of aborted qc's.
774dbd82616STejun Heo  */
775dbd82616STejun Heo int ata_link_abort(struct ata_link *link)
776dbd82616STejun Heo {
777dbd82616STejun Heo 	return ata_do_link_abort(link->ap, link);
778dbd82616STejun Heo }
779dbd82616STejun Heo 
780dbd82616STejun Heo /**
781dbd82616STejun Heo  *	ata_port_abort - abort all qc's on the port
782dbd82616STejun Heo  *	@ap: ATA port to abort qc's for
783dbd82616STejun Heo  *
784dbd82616STejun Heo  *	Abort all active qc's of @ap and schedule EH.
785dbd82616STejun Heo  *
786dbd82616STejun Heo  *	LOCKING:
787dbd82616STejun Heo  *	spin_lock_irqsave(host_set lock)
788dbd82616STejun Heo  *
789dbd82616STejun Heo  *	RETURNS:
790dbd82616STejun Heo  *	Number of aborted qc's.
791dbd82616STejun Heo  */
792dbd82616STejun Heo int ata_port_abort(struct ata_port *ap)
793dbd82616STejun Heo {
794dbd82616STejun Heo 	return ata_do_link_abort(ap, NULL);
795dbd82616STejun Heo }
796dbd82616STejun Heo 
797dbd82616STejun Heo /**
798c6fd2807SJeff Garzik  *	__ata_port_freeze - freeze port
799c6fd2807SJeff Garzik  *	@ap: ATA port to freeze
800c6fd2807SJeff Garzik  *
801c6fd2807SJeff Garzik  *	This function is called when HSM violation or some other
802c6fd2807SJeff Garzik  *	condition disrupts normal operation of the port.  Frozen port
803c6fd2807SJeff Garzik  *	is not allowed to perform any operation until the port is
804c6fd2807SJeff Garzik  *	thawed, which usually follows a successful reset.
805c6fd2807SJeff Garzik  *
806c6fd2807SJeff Garzik  *	ap->ops->freeze() callback can be used for freezing the port
807c6fd2807SJeff Garzik  *	hardware-wise (e.g. mask interrupt and stop DMA engine).  If a
808c6fd2807SJeff Garzik  *	port cannot be frozen hardware-wise, the interrupt handler
809c6fd2807SJeff Garzik  *	must ack and clear interrupts unconditionally while the port
810c6fd2807SJeff Garzik  *	is frozen.
811c6fd2807SJeff Garzik  *
812c6fd2807SJeff Garzik  *	LOCKING:
813cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
814c6fd2807SJeff Garzik  */
815c6fd2807SJeff Garzik static void __ata_port_freeze(struct ata_port *ap)
816c6fd2807SJeff Garzik {
817c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
818c6fd2807SJeff Garzik 
819c6fd2807SJeff Garzik 	if (ap->ops->freeze)
820c6fd2807SJeff Garzik 		ap->ops->freeze(ap);
821c6fd2807SJeff Garzik 
822c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_FROZEN;
823c6fd2807SJeff Garzik 
82444877b4eSTejun Heo 	DPRINTK("ata%u port frozen\n", ap->print_id);
825c6fd2807SJeff Garzik }
826c6fd2807SJeff Garzik 
827c6fd2807SJeff Garzik /**
828c6fd2807SJeff Garzik  *	ata_port_freeze - abort & freeze port
829c6fd2807SJeff Garzik  *	@ap: ATA port to freeze
830c6fd2807SJeff Garzik  *
831c6fd2807SJeff Garzik  *	Abort and freeze @ap.
832c6fd2807SJeff Garzik  *
833c6fd2807SJeff Garzik  *	LOCKING:
834cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
835c6fd2807SJeff Garzik  *
836c6fd2807SJeff Garzik  *	RETURNS:
837c6fd2807SJeff Garzik  *	Number of aborted commands.
838c6fd2807SJeff Garzik  */
839c6fd2807SJeff Garzik int ata_port_freeze(struct ata_port *ap)
840c6fd2807SJeff Garzik {
841c6fd2807SJeff Garzik 	int nr_aborted;
842c6fd2807SJeff Garzik 
843c6fd2807SJeff Garzik 	WARN_ON(!ap->ops->error_handler);
844c6fd2807SJeff Garzik 
845c6fd2807SJeff Garzik 	nr_aborted = ata_port_abort(ap);
846c6fd2807SJeff Garzik 	__ata_port_freeze(ap);
847c6fd2807SJeff Garzik 
848c6fd2807SJeff Garzik 	return nr_aborted;
849c6fd2807SJeff Garzik }
850c6fd2807SJeff Garzik 
851c6fd2807SJeff Garzik /**
8527d77b247STejun Heo  *	sata_async_notification - SATA async notification handler
8537d77b247STejun Heo  *	@ap: ATA port where async notification is received
8547d77b247STejun Heo  *
8557d77b247STejun Heo  *	Handler to be called when async notification via SDB FIS is
8567d77b247STejun Heo  *	received.  This function schedules EH if necessary.
8577d77b247STejun Heo  *
8587d77b247STejun Heo  *	LOCKING:
8597d77b247STejun Heo  *	spin_lock_irqsave(host lock)
8607d77b247STejun Heo  *
8617d77b247STejun Heo  *	RETURNS:
8627d77b247STejun Heo  *	1 if EH is scheduled, 0 otherwise.
8637d77b247STejun Heo  */
8647d77b247STejun Heo int sata_async_notification(struct ata_port *ap)
8657d77b247STejun Heo {
8667d77b247STejun Heo 	u32 sntf;
8677d77b247STejun Heo 	int rc;
8687d77b247STejun Heo 
8697d77b247STejun Heo 	if (!(ap->flags & ATA_FLAG_AN))
8707d77b247STejun Heo 		return 0;
8717d77b247STejun Heo 
8727d77b247STejun Heo 	rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
8737d77b247STejun Heo 	if (rc == 0)
8747d77b247STejun Heo 		sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
8757d77b247STejun Heo 
876071f44b1STejun Heo 	if (!sata_pmp_attached(ap) || rc) {
8777d77b247STejun Heo 		/* PMP is not attached or SNTF is not available */
878071f44b1STejun Heo 		if (!sata_pmp_attached(ap)) {
8797d77b247STejun Heo 			/* PMP is not attached.  Check whether ATAPI
8807d77b247STejun Heo 			 * AN is configured.  If so, notify media
8817d77b247STejun Heo 			 * change.
8827d77b247STejun Heo 			 */
8837d77b247STejun Heo 			struct ata_device *dev = ap->link.device;
8847d77b247STejun Heo 
8857d77b247STejun Heo 			if ((dev->class == ATA_DEV_ATAPI) &&
8867d77b247STejun Heo 			    (dev->flags & ATA_DFLAG_AN))
8877d77b247STejun Heo 				ata_scsi_media_change_notify(dev);
8887d77b247STejun Heo 			return 0;
8897d77b247STejun Heo 		} else {
8907d77b247STejun Heo 			/* PMP is attached but SNTF is not available.
8917d77b247STejun Heo 			 * ATAPI async media change notification is
8927d77b247STejun Heo 			 * not used.  The PMP must be reporting PHY
8937d77b247STejun Heo 			 * status change, schedule EH.
8947d77b247STejun Heo 			 */
8957d77b247STejun Heo 			ata_port_schedule_eh(ap);
8967d77b247STejun Heo 			return 1;
8977d77b247STejun Heo 		}
8987d77b247STejun Heo 	} else {
8997d77b247STejun Heo 		/* PMP is attached and SNTF is available */
9007d77b247STejun Heo 		struct ata_link *link;
9017d77b247STejun Heo 
9027d77b247STejun Heo 		/* check and notify ATAPI AN */
9037d77b247STejun Heo 		ata_port_for_each_link(link, ap) {
9047d77b247STejun Heo 			if (!(sntf & (1 << link->pmp)))
9057d77b247STejun Heo 				continue;
9067d77b247STejun Heo 
9077d77b247STejun Heo 			if ((link->device->class == ATA_DEV_ATAPI) &&
9087d77b247STejun Heo 			    (link->device->flags & ATA_DFLAG_AN))
9097d77b247STejun Heo 				ata_scsi_media_change_notify(link->device);
9107d77b247STejun Heo 		}
9117d77b247STejun Heo 
9127d77b247STejun Heo 		/* If PMP is reporting that PHY status of some
9137d77b247STejun Heo 		 * downstream ports has changed, schedule EH.
9147d77b247STejun Heo 		 */
9157d77b247STejun Heo 		if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
9167d77b247STejun Heo 			ata_port_schedule_eh(ap);
9177d77b247STejun Heo 			return 1;
9187d77b247STejun Heo 		}
9197d77b247STejun Heo 
9207d77b247STejun Heo 		return 0;
9217d77b247STejun Heo 	}
9227d77b247STejun Heo }
9237d77b247STejun Heo 
9247d77b247STejun Heo /**
925c6fd2807SJeff Garzik  *	ata_eh_freeze_port - EH helper to freeze port
926c6fd2807SJeff Garzik  *	@ap: ATA port to freeze
927c6fd2807SJeff Garzik  *
928c6fd2807SJeff Garzik  *	Freeze @ap.
929c6fd2807SJeff Garzik  *
930c6fd2807SJeff Garzik  *	LOCKING:
931c6fd2807SJeff Garzik  *	None.
932c6fd2807SJeff Garzik  */
933c6fd2807SJeff Garzik void ata_eh_freeze_port(struct ata_port *ap)
934c6fd2807SJeff Garzik {
935c6fd2807SJeff Garzik 	unsigned long flags;
936c6fd2807SJeff Garzik 
937c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
938c6fd2807SJeff Garzik 		return;
939c6fd2807SJeff Garzik 
940c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
941c6fd2807SJeff Garzik 	__ata_port_freeze(ap);
942c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
943c6fd2807SJeff Garzik }
944c6fd2807SJeff Garzik 
945c6fd2807SJeff Garzik /**
946c6fd2807SJeff Garzik  *	ata_port_thaw_port - EH helper to thaw port
947c6fd2807SJeff Garzik  *	@ap: ATA port to thaw
948c6fd2807SJeff Garzik  *
949c6fd2807SJeff Garzik  *	Thaw frozen port @ap.
950c6fd2807SJeff Garzik  *
951c6fd2807SJeff Garzik  *	LOCKING:
952c6fd2807SJeff Garzik  *	None.
953c6fd2807SJeff Garzik  */
954c6fd2807SJeff Garzik void ata_eh_thaw_port(struct ata_port *ap)
955c6fd2807SJeff Garzik {
956c6fd2807SJeff Garzik 	unsigned long flags;
957c6fd2807SJeff Garzik 
958c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
959c6fd2807SJeff Garzik 		return;
960c6fd2807SJeff Garzik 
961c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
962c6fd2807SJeff Garzik 
963c6fd2807SJeff Garzik 	ap->pflags &= ~ATA_PFLAG_FROZEN;
964c6fd2807SJeff Garzik 
965c6fd2807SJeff Garzik 	if (ap->ops->thaw)
966c6fd2807SJeff Garzik 		ap->ops->thaw(ap);
967c6fd2807SJeff Garzik 
968c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
969c6fd2807SJeff Garzik 
97044877b4eSTejun Heo 	DPRINTK("ata%u port thawed\n", ap->print_id);
971c6fd2807SJeff Garzik }
972c6fd2807SJeff Garzik 
973c6fd2807SJeff Garzik static void ata_eh_scsidone(struct scsi_cmnd *scmd)
974c6fd2807SJeff Garzik {
975c6fd2807SJeff Garzik 	/* nada */
976c6fd2807SJeff Garzik }
977c6fd2807SJeff Garzik 
978c6fd2807SJeff Garzik static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
979c6fd2807SJeff Garzik {
980c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
981c6fd2807SJeff Garzik 	struct scsi_cmnd *scmd = qc->scsicmd;
982c6fd2807SJeff Garzik 	unsigned long flags;
983c6fd2807SJeff Garzik 
984c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
985c6fd2807SJeff Garzik 	qc->scsidone = ata_eh_scsidone;
986c6fd2807SJeff Garzik 	__ata_qc_complete(qc);
987c6fd2807SJeff Garzik 	WARN_ON(ata_tag_valid(qc->tag));
988c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
989c6fd2807SJeff Garzik 
990c6fd2807SJeff Garzik 	scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
991c6fd2807SJeff Garzik }
992c6fd2807SJeff Garzik 
993c6fd2807SJeff Garzik /**
994c6fd2807SJeff Garzik  *	ata_eh_qc_complete - Complete an active ATA command from EH
995c6fd2807SJeff Garzik  *	@qc: Command to complete
996c6fd2807SJeff Garzik  *
997c6fd2807SJeff Garzik  *	Indicate to the mid and upper layers that an ATA command has
998c6fd2807SJeff Garzik  *	completed.  To be used from EH.
999c6fd2807SJeff Garzik  */
1000c6fd2807SJeff Garzik void ata_eh_qc_complete(struct ata_queued_cmd *qc)
1001c6fd2807SJeff Garzik {
1002c6fd2807SJeff Garzik 	struct scsi_cmnd *scmd = qc->scsicmd;
1003c6fd2807SJeff Garzik 	scmd->retries = scmd->allowed;
1004c6fd2807SJeff Garzik 	__ata_eh_qc_complete(qc);
1005c6fd2807SJeff Garzik }
1006c6fd2807SJeff Garzik 
1007c6fd2807SJeff Garzik /**
1008c6fd2807SJeff Garzik  *	ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
1009c6fd2807SJeff Garzik  *	@qc: Command to retry
1010c6fd2807SJeff Garzik  *
1011c6fd2807SJeff Garzik  *	Indicate to the mid and upper layers that an ATA command
1012c6fd2807SJeff Garzik  *	should be retried.  To be used from EH.
1013c6fd2807SJeff Garzik  *
1014c6fd2807SJeff Garzik  *	SCSI midlayer limits the number of retries to scmd->allowed.
1015c6fd2807SJeff Garzik  *	scmd->retries is decremented for commands which get retried
1016c6fd2807SJeff Garzik  *	due to unrelated failures (qc->err_mask is zero).
1017c6fd2807SJeff Garzik  */
1018c6fd2807SJeff Garzik void ata_eh_qc_retry(struct ata_queued_cmd *qc)
1019c6fd2807SJeff Garzik {
1020c6fd2807SJeff Garzik 	struct scsi_cmnd *scmd = qc->scsicmd;
1021c6fd2807SJeff Garzik 	if (!qc->err_mask && scmd->retries)
1022c6fd2807SJeff Garzik 		scmd->retries--;
1023c6fd2807SJeff Garzik 	__ata_eh_qc_complete(qc);
1024c6fd2807SJeff Garzik }
1025c6fd2807SJeff Garzik 
1026c6fd2807SJeff Garzik /**
1027c6fd2807SJeff Garzik  *	ata_eh_detach_dev - detach ATA device
1028c6fd2807SJeff Garzik  *	@dev: ATA device to detach
1029c6fd2807SJeff Garzik  *
1030c6fd2807SJeff Garzik  *	Detach @dev.
1031c6fd2807SJeff Garzik  *
1032c6fd2807SJeff Garzik  *	LOCKING:
1033c6fd2807SJeff Garzik  *	None.
1034c6fd2807SJeff Garzik  */
1035fb7fd614STejun Heo void ata_eh_detach_dev(struct ata_device *dev)
1036c6fd2807SJeff Garzik {
1037f58229f8STejun Heo 	struct ata_link *link = dev->link;
1038f58229f8STejun Heo 	struct ata_port *ap = link->ap;
1039c6fd2807SJeff Garzik 	unsigned long flags;
1040c6fd2807SJeff Garzik 
1041c6fd2807SJeff Garzik 	ata_dev_disable(dev);
1042c6fd2807SJeff Garzik 
1043c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1044c6fd2807SJeff Garzik 
1045c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_DETACH;
1046c6fd2807SJeff Garzik 
1047c6fd2807SJeff Garzik 	if (ata_scsi_offline_dev(dev)) {
1048c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_DETACHED;
1049c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
1050c6fd2807SJeff Garzik 	}
1051c6fd2807SJeff Garzik 
1052c6fd2807SJeff Garzik 	/* clear per-dev EH actions */
1053f58229f8STejun Heo 	ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
1054f58229f8STejun Heo 	ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
1055c6fd2807SJeff Garzik 
1056c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1057c6fd2807SJeff Garzik }
1058c6fd2807SJeff Garzik 
1059c6fd2807SJeff Garzik /**
1060c6fd2807SJeff Garzik  *	ata_eh_about_to_do - about to perform eh_action
1061955e57dfSTejun Heo  *	@link: target ATA link
1062c6fd2807SJeff Garzik  *	@dev: target ATA dev for per-dev action (can be NULL)
1063c6fd2807SJeff Garzik  *	@action: action about to be performed
1064c6fd2807SJeff Garzik  *
1065c6fd2807SJeff Garzik  *	Called just before performing EH actions to clear related bits
1066955e57dfSTejun Heo  *	in @link->eh_info such that eh actions are not unnecessarily
1067955e57dfSTejun Heo  *	repeated.
1068c6fd2807SJeff Garzik  *
1069c6fd2807SJeff Garzik  *	LOCKING:
1070c6fd2807SJeff Garzik  *	None.
1071c6fd2807SJeff Garzik  */
1072fb7fd614STejun Heo void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
1073c6fd2807SJeff Garzik 			unsigned int action)
1074c6fd2807SJeff Garzik {
1075955e57dfSTejun Heo 	struct ata_port *ap = link->ap;
1076955e57dfSTejun Heo 	struct ata_eh_info *ehi = &link->eh_info;
1077955e57dfSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
1078c6fd2807SJeff Garzik 	unsigned long flags;
1079c6fd2807SJeff Garzik 
1080c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1081c6fd2807SJeff Garzik 
1082955e57dfSTejun Heo 	ata_eh_clear_action(link, dev, ehi, action);
1083c6fd2807SJeff Garzik 
1084c6fd2807SJeff Garzik 	if (!(ehc->i.flags & ATA_EHI_QUIET))
1085c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_RECOVERED;
1086c6fd2807SJeff Garzik 
1087c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1088c6fd2807SJeff Garzik }
1089c6fd2807SJeff Garzik 
1090c6fd2807SJeff Garzik /**
1091c6fd2807SJeff Garzik  *	ata_eh_done - EH action complete
1092c6fd2807SJeff Garzik *	@ap: target ATA port
1093c6fd2807SJeff Garzik  *	@dev: target ATA dev for per-dev action (can be NULL)
1094c6fd2807SJeff Garzik  *	@action: action just completed
1095c6fd2807SJeff Garzik  *
1096c6fd2807SJeff Garzik  *	Called right after performing EH actions to clear related bits
1097955e57dfSTejun Heo  *	in @link->eh_context.
1098c6fd2807SJeff Garzik  *
1099c6fd2807SJeff Garzik  *	LOCKING:
1100c6fd2807SJeff Garzik  *	None.
1101c6fd2807SJeff Garzik  */
1102fb7fd614STejun Heo void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1103c6fd2807SJeff Garzik 		 unsigned int action)
1104c6fd2807SJeff Garzik {
1105955e57dfSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
11069af5c9c9STejun Heo 
1107955e57dfSTejun Heo 	ata_eh_clear_action(link, dev, &ehc->i, action);
1108c6fd2807SJeff Garzik }
1109c6fd2807SJeff Garzik 
1110c6fd2807SJeff Garzik /**
1111c6fd2807SJeff Garzik  *	ata_err_string - convert err_mask to descriptive string
1112c6fd2807SJeff Garzik  *	@err_mask: error mask to convert to string
1113c6fd2807SJeff Garzik  *
1114c6fd2807SJeff Garzik  *	Convert @err_mask to descriptive string.  Errors are
1115c6fd2807SJeff Garzik  *	prioritized according to severity and only the most severe
1116c6fd2807SJeff Garzik  *	error is reported.
1117c6fd2807SJeff Garzik  *
1118c6fd2807SJeff Garzik  *	LOCKING:
1119c6fd2807SJeff Garzik  *	None.
1120c6fd2807SJeff Garzik  *
1121c6fd2807SJeff Garzik  *	RETURNS:
1122c6fd2807SJeff Garzik  *	Descriptive string for @err_mask
1123c6fd2807SJeff Garzik  */
1124c6fd2807SJeff Garzik static const char *ata_err_string(unsigned int err_mask)
1125c6fd2807SJeff Garzik {
1126c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_HOST_BUS)
1127c6fd2807SJeff Garzik 		return "host bus error";
1128c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_ATA_BUS)
1129c6fd2807SJeff Garzik 		return "ATA bus error";
1130c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_TIMEOUT)
1131c6fd2807SJeff Garzik 		return "timeout";
1132c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_HSM)
1133c6fd2807SJeff Garzik 		return "HSM violation";
1134c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_SYSTEM)
1135c6fd2807SJeff Garzik 		return "internal error";
1136c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_MEDIA)
1137c6fd2807SJeff Garzik 		return "media error";
1138c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_INVALID)
1139c6fd2807SJeff Garzik 		return "invalid argument";
1140c6fd2807SJeff Garzik 	if (err_mask & AC_ERR_DEV)
1141c6fd2807SJeff Garzik 		return "device error";
1142c6fd2807SJeff Garzik 	return "unknown error";
1143c6fd2807SJeff Garzik }
1144c6fd2807SJeff Garzik 
1145c6fd2807SJeff Garzik /**
1146c6fd2807SJeff Garzik  *	ata_read_log_page - read a specific log page
1147c6fd2807SJeff Garzik  *	@dev: target device
1148c6fd2807SJeff Garzik  *	@page: page to read
1149c6fd2807SJeff Garzik  *	@buf: buffer to store read page
1150c6fd2807SJeff Garzik  *	@sectors: number of sectors to read
1151c6fd2807SJeff Garzik  *
1152c6fd2807SJeff Garzik  *	Read log page using READ_LOG_EXT command.
1153c6fd2807SJeff Garzik  *
1154c6fd2807SJeff Garzik  *	LOCKING:
1155c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1156c6fd2807SJeff Garzik  *
1157c6fd2807SJeff Garzik  *	RETURNS:
1158c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
1159c6fd2807SJeff Garzik  */
1160c6fd2807SJeff Garzik static unsigned int ata_read_log_page(struct ata_device *dev,
1161c6fd2807SJeff Garzik 				      u8 page, void *buf, unsigned int sectors)
1162c6fd2807SJeff Garzik {
1163c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1164c6fd2807SJeff Garzik 	unsigned int err_mask;
1165c6fd2807SJeff Garzik 
1166c6fd2807SJeff Garzik 	DPRINTK("read log page - page %d\n", page);
1167c6fd2807SJeff Garzik 
1168c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
1169c6fd2807SJeff Garzik 	tf.command = ATA_CMD_READ_LOG_EXT;
1170c6fd2807SJeff Garzik 	tf.lbal = page;
1171c6fd2807SJeff Garzik 	tf.nsect = sectors;
1172c6fd2807SJeff Garzik 	tf.hob_nsect = sectors >> 8;
1173c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1174c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_PIO;
1175c6fd2807SJeff Garzik 
1176c6fd2807SJeff Garzik 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
11772b789108STejun Heo 				     buf, sectors * ATA_SECT_SIZE, 0);
1178c6fd2807SJeff Garzik 
1179c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
1180c6fd2807SJeff Garzik 	return err_mask;
1181c6fd2807SJeff Garzik }
1182c6fd2807SJeff Garzik 
1183c6fd2807SJeff Garzik /**
1184c6fd2807SJeff Garzik  *	ata_eh_read_log_10h - Read log page 10h for NCQ error details
1185c6fd2807SJeff Garzik  *	@dev: Device to read log page 10h from
1186c6fd2807SJeff Garzik  *	@tag: Resulting tag of the failed command
1187c6fd2807SJeff Garzik  *	@tf: Resulting taskfile registers of the failed command
1188c6fd2807SJeff Garzik  *
1189c6fd2807SJeff Garzik  *	Read log page 10h to obtain NCQ error details and clear error
1190c6fd2807SJeff Garzik  *	condition.
1191c6fd2807SJeff Garzik  *
1192c6fd2807SJeff Garzik  *	LOCKING:
1193c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1194c6fd2807SJeff Garzik  *
1195c6fd2807SJeff Garzik  *	RETURNS:
1196c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
1197c6fd2807SJeff Garzik  */
1198c6fd2807SJeff Garzik static int ata_eh_read_log_10h(struct ata_device *dev,
1199c6fd2807SJeff Garzik 			       int *tag, struct ata_taskfile *tf)
1200c6fd2807SJeff Garzik {
12019af5c9c9STejun Heo 	u8 *buf = dev->link->ap->sector_buf;
1202c6fd2807SJeff Garzik 	unsigned int err_mask;
1203c6fd2807SJeff Garzik 	u8 csum;
1204c6fd2807SJeff Garzik 	int i;
1205c6fd2807SJeff Garzik 
1206c6fd2807SJeff Garzik 	err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
1207c6fd2807SJeff Garzik 	if (err_mask)
1208c6fd2807SJeff Garzik 		return -EIO;
1209c6fd2807SJeff Garzik 
1210c6fd2807SJeff Garzik 	csum = 0;
1211c6fd2807SJeff Garzik 	for (i = 0; i < ATA_SECT_SIZE; i++)
1212c6fd2807SJeff Garzik 		csum += buf[i];
1213c6fd2807SJeff Garzik 	if (csum)
1214c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_WARNING,
1215c6fd2807SJeff Garzik 			       "invalid checksum 0x%x on log page 10h\n", csum);
1216c6fd2807SJeff Garzik 
1217c6fd2807SJeff Garzik 	if (buf[0] & 0x80)
1218c6fd2807SJeff Garzik 		return -ENOENT;
1219c6fd2807SJeff Garzik 
1220c6fd2807SJeff Garzik 	*tag = buf[0] & 0x1f;
1221c6fd2807SJeff Garzik 
1222c6fd2807SJeff Garzik 	tf->command = buf[2];
1223c6fd2807SJeff Garzik 	tf->feature = buf[3];
1224c6fd2807SJeff Garzik 	tf->lbal = buf[4];
1225c6fd2807SJeff Garzik 	tf->lbam = buf[5];
1226c6fd2807SJeff Garzik 	tf->lbah = buf[6];
1227c6fd2807SJeff Garzik 	tf->device = buf[7];
1228c6fd2807SJeff Garzik 	tf->hob_lbal = buf[8];
1229c6fd2807SJeff Garzik 	tf->hob_lbam = buf[9];
1230c6fd2807SJeff Garzik 	tf->hob_lbah = buf[10];
1231c6fd2807SJeff Garzik 	tf->nsect = buf[12];
1232c6fd2807SJeff Garzik 	tf->hob_nsect = buf[13];
1233c6fd2807SJeff Garzik 
1234c6fd2807SJeff Garzik 	return 0;
1235c6fd2807SJeff Garzik }
1236c6fd2807SJeff Garzik 
1237c6fd2807SJeff Garzik /**
1238c6fd2807SJeff Garzik  *	atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1239c6fd2807SJeff Garzik  *	@dev: device to perform REQUEST_SENSE to
1240c6fd2807SJeff Garzik  *	@sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
1241c6fd2807SJeff Garzik  *
1242c6fd2807SJeff Garzik  *	Perform ATAPI REQUEST_SENSE after the device reported CHECK
1243c6fd2807SJeff Garzik  *	SENSE.  This function is EH helper.
1244c6fd2807SJeff Garzik  *
1245c6fd2807SJeff Garzik  *	LOCKING:
1246c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1247c6fd2807SJeff Garzik  *
1248c6fd2807SJeff Garzik  *	RETURNS:
1249c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask on failure
1250c6fd2807SJeff Garzik  */
125156287768SAlbert Lee static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc)
1252c6fd2807SJeff Garzik {
125356287768SAlbert Lee 	struct ata_device *dev = qc->dev;
125456287768SAlbert Lee 	unsigned char *sense_buf = qc->scsicmd->sense_buffer;
12559af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
1256c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1257c6fd2807SJeff Garzik 	u8 cdb[ATAPI_CDB_LEN];
1258c6fd2807SJeff Garzik 
1259c6fd2807SJeff Garzik 	DPRINTK("ATAPI request sense\n");
1260c6fd2807SJeff Garzik 
1261c6fd2807SJeff Garzik 	/* FIXME: is this needed? */
1262c6fd2807SJeff Garzik 	memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1263c6fd2807SJeff Garzik 
126456287768SAlbert Lee 	/* initialize sense_buf with the error register,
126556287768SAlbert Lee 	 * for the case where they are -not- overwritten
126656287768SAlbert Lee 	 */
1267c6fd2807SJeff Garzik 	sense_buf[0] = 0x70;
126856287768SAlbert Lee 	sense_buf[2] = qc->result_tf.feature >> 4;
126956287768SAlbert Lee 
127056287768SAlbert Lee 	/* some devices time out if garbage left in tf */
127156287768SAlbert Lee 	ata_tf_init(dev, &tf);
1272c6fd2807SJeff Garzik 
1273c6fd2807SJeff Garzik 	memset(cdb, 0, ATAPI_CDB_LEN);
1274c6fd2807SJeff Garzik 	cdb[0] = REQUEST_SENSE;
1275c6fd2807SJeff Garzik 	cdb[4] = SCSI_SENSE_BUFFERSIZE;
1276c6fd2807SJeff Garzik 
1277c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1278c6fd2807SJeff Garzik 	tf.command = ATA_CMD_PACKET;
1279c6fd2807SJeff Garzik 
1280c6fd2807SJeff Garzik 	/* is it pointless to prefer PIO for "safety reasons"? */
1281c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_PIO_DMA) {
12820dc36888STejun Heo 		tf.protocol = ATAPI_PROT_DMA;
1283c6fd2807SJeff Garzik 		tf.feature |= ATAPI_PKT_DMA;
1284c6fd2807SJeff Garzik 	} else {
12850dc36888STejun Heo 		tf.protocol = ATAPI_PROT_PIO;
1286f2dfc1a1STejun Heo 		tf.lbam = SCSI_SENSE_BUFFERSIZE;
1287f2dfc1a1STejun Heo 		tf.lbah = 0;
1288c6fd2807SJeff Garzik 	}
1289c6fd2807SJeff Garzik 
1290c6fd2807SJeff Garzik 	return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
12912b789108STejun Heo 				 sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
1292c6fd2807SJeff Garzik }
1293c6fd2807SJeff Garzik 
1294c6fd2807SJeff Garzik /**
1295c6fd2807SJeff Garzik  *	ata_eh_analyze_serror - analyze SError for a failed port
12960260731fSTejun Heo  *	@link: ATA link to analyze SError for
1297c6fd2807SJeff Garzik  *
1298c6fd2807SJeff Garzik  *	Analyze SError if available and further determine cause of
1299c6fd2807SJeff Garzik  *	failure.
1300c6fd2807SJeff Garzik  *
1301c6fd2807SJeff Garzik  *	LOCKING:
1302c6fd2807SJeff Garzik  *	None.
1303c6fd2807SJeff Garzik  */
13040260731fSTejun Heo static void ata_eh_analyze_serror(struct ata_link *link)
1305c6fd2807SJeff Garzik {
13060260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
1307c6fd2807SJeff Garzik 	u32 serror = ehc->i.serror;
1308c6fd2807SJeff Garzik 	unsigned int err_mask = 0, action = 0;
1309f9df58cbSTejun Heo 	u32 hotplug_mask;
1310c6fd2807SJeff Garzik 
1311e0614db2STejun Heo 	if (serror & (SERR_PERSISTENT | SERR_DATA)) {
1312c6fd2807SJeff Garzik 		err_mask |= AC_ERR_ATA_BUS;
1313cf480626STejun Heo 		action |= ATA_EH_RESET;
1314c6fd2807SJeff Garzik 	}
1315c6fd2807SJeff Garzik 	if (serror & SERR_PROTOCOL) {
1316c6fd2807SJeff Garzik 		err_mask |= AC_ERR_HSM;
1317cf480626STejun Heo 		action |= ATA_EH_RESET;
1318c6fd2807SJeff Garzik 	}
1319c6fd2807SJeff Garzik 	if (serror & SERR_INTERNAL) {
1320c6fd2807SJeff Garzik 		err_mask |= AC_ERR_SYSTEM;
1321cf480626STejun Heo 		action |= ATA_EH_RESET;
1322c6fd2807SJeff Garzik 	}
1323f9df58cbSTejun Heo 
1324f9df58cbSTejun Heo 	/* Determine whether a hotplug event has occurred.  Both
1325f9df58cbSTejun Heo 	 * SError.N/X are considered hotplug events for enabled or
1326f9df58cbSTejun Heo 	 * host links.  For disabled PMP links, only N bit is
1327f9df58cbSTejun Heo 	 * considered as X bit is left at 1 for link plugging.
1328f9df58cbSTejun Heo 	 */
1329f9df58cbSTejun Heo 	hotplug_mask = 0;
1330f9df58cbSTejun Heo 
1331f9df58cbSTejun Heo 	if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
1332f9df58cbSTejun Heo 		hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
1333f9df58cbSTejun Heo 	else
1334f9df58cbSTejun Heo 		hotplug_mask = SERR_PHYRDY_CHG;
1335f9df58cbSTejun Heo 
1336f9df58cbSTejun Heo 	if (serror & hotplug_mask)
1337c6fd2807SJeff Garzik 		ata_ehi_hotplugged(&ehc->i);
1338c6fd2807SJeff Garzik 
1339c6fd2807SJeff Garzik 	ehc->i.err_mask |= err_mask;
1340c6fd2807SJeff Garzik 	ehc->i.action |= action;
1341c6fd2807SJeff Garzik }
1342c6fd2807SJeff Garzik 
1343c6fd2807SJeff Garzik /**
1344c6fd2807SJeff Garzik  *	ata_eh_analyze_ncq_error - analyze NCQ error
13450260731fSTejun Heo  *	@link: ATA link to analyze NCQ error for
1346c6fd2807SJeff Garzik  *
1347c6fd2807SJeff Garzik  *	Read log page 10h, determine the offending qc and acquire
1348c6fd2807SJeff Garzik  *	error status TF.  For NCQ device errors, all LLDDs have to do
1349c6fd2807SJeff Garzik  *	is setting AC_ERR_DEV in ehi->err_mask.  This function takes
1350c6fd2807SJeff Garzik  *	care of the rest.
1351c6fd2807SJeff Garzik  *
1352c6fd2807SJeff Garzik  *	LOCKING:
1353c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1354c6fd2807SJeff Garzik  */
135510acf3b0SMark Lord void ata_eh_analyze_ncq_error(struct ata_link *link)
1356c6fd2807SJeff Garzik {
13570260731fSTejun Heo 	struct ata_port *ap = link->ap;
13580260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
13590260731fSTejun Heo 	struct ata_device *dev = link->device;
1360c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
1361c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1362c6fd2807SJeff Garzik 	int tag, rc;
1363c6fd2807SJeff Garzik 
1364c6fd2807SJeff Garzik 	/* if frozen, we can't do much */
1365c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN)
1366c6fd2807SJeff Garzik 		return;
1367c6fd2807SJeff Garzik 
1368c6fd2807SJeff Garzik 	/* is it NCQ device error? */
13690260731fSTejun Heo 	if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1370c6fd2807SJeff Garzik 		return;
1371c6fd2807SJeff Garzik 
1372c6fd2807SJeff Garzik 	/* has LLDD analyzed already? */
1373c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1374c6fd2807SJeff Garzik 		qc = __ata_qc_from_tag(ap, tag);
1375c6fd2807SJeff Garzik 
1376c6fd2807SJeff Garzik 		if (!(qc->flags & ATA_QCFLAG_FAILED))
1377c6fd2807SJeff Garzik 			continue;
1378c6fd2807SJeff Garzik 
1379c6fd2807SJeff Garzik 		if (qc->err_mask)
1380c6fd2807SJeff Garzik 			return;
1381c6fd2807SJeff Garzik 	}
1382c6fd2807SJeff Garzik 
1383c6fd2807SJeff Garzik 	/* okay, this error is ours */
1384c6fd2807SJeff Garzik 	rc = ata_eh_read_log_10h(dev, &tag, &tf);
1385c6fd2807SJeff Garzik 	if (rc) {
13860260731fSTejun Heo 		ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
1387c6fd2807SJeff Garzik 				"(errno=%d)\n", rc);
1388c6fd2807SJeff Garzik 		return;
1389c6fd2807SJeff Garzik 	}
1390c6fd2807SJeff Garzik 
13910260731fSTejun Heo 	if (!(link->sactive & (1 << tag))) {
13920260731fSTejun Heo 		ata_link_printk(link, KERN_ERR, "log page 10h reported "
1393c6fd2807SJeff Garzik 				"inactive tag %d\n", tag);
1394c6fd2807SJeff Garzik 		return;
1395c6fd2807SJeff Garzik 	}
1396c6fd2807SJeff Garzik 
1397c6fd2807SJeff Garzik 	/* we've got the perpetrator, condemn it */
1398c6fd2807SJeff Garzik 	qc = __ata_qc_from_tag(ap, tag);
1399c6fd2807SJeff Garzik 	memcpy(&qc->result_tf, &tf, sizeof(tf));
1400a6116c9eSMark Lord 	qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
14015335b729STejun Heo 	qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
1402c6fd2807SJeff Garzik 	ehc->i.err_mask &= ~AC_ERR_DEV;
1403c6fd2807SJeff Garzik }
1404c6fd2807SJeff Garzik 
1405c6fd2807SJeff Garzik /**
1406c6fd2807SJeff Garzik  *	ata_eh_analyze_tf - analyze taskfile of a failed qc
1407c6fd2807SJeff Garzik  *	@qc: qc to analyze
1408c6fd2807SJeff Garzik  *	@tf: Taskfile registers to analyze
1409c6fd2807SJeff Garzik  *
1410c6fd2807SJeff Garzik  *	Analyze taskfile of @qc and further determine cause of
1411c6fd2807SJeff Garzik  *	failure.  This function also requests ATAPI sense data if
1412c6fd2807SJeff Garzik  *	avaliable.
1413c6fd2807SJeff Garzik  *
1414c6fd2807SJeff Garzik  *	LOCKING:
1415c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1416c6fd2807SJeff Garzik  *
1417c6fd2807SJeff Garzik  *	RETURNS:
1418c6fd2807SJeff Garzik  *	Determined recovery action
1419c6fd2807SJeff Garzik  */
1420c6fd2807SJeff Garzik static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1421c6fd2807SJeff Garzik 				      const struct ata_taskfile *tf)
1422c6fd2807SJeff Garzik {
1423c6fd2807SJeff Garzik 	unsigned int tmp, action = 0;
1424c6fd2807SJeff Garzik 	u8 stat = tf->command, err = tf->feature;
1425c6fd2807SJeff Garzik 
1426c6fd2807SJeff Garzik 	if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1427c6fd2807SJeff Garzik 		qc->err_mask |= AC_ERR_HSM;
1428cf480626STejun Heo 		return ATA_EH_RESET;
1429c6fd2807SJeff Garzik 	}
1430c6fd2807SJeff Garzik 
1431a51d644aSTejun Heo 	if (stat & (ATA_ERR | ATA_DF))
1432a51d644aSTejun Heo 		qc->err_mask |= AC_ERR_DEV;
1433a51d644aSTejun Heo 	else
1434c6fd2807SJeff Garzik 		return 0;
1435c6fd2807SJeff Garzik 
1436c6fd2807SJeff Garzik 	switch (qc->dev->class) {
1437c6fd2807SJeff Garzik 	case ATA_DEV_ATA:
1438c6fd2807SJeff Garzik 		if (err & ATA_ICRC)
1439c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_ATA_BUS;
1440c6fd2807SJeff Garzik 		if (err & ATA_UNC)
1441c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_MEDIA;
1442c6fd2807SJeff Garzik 		if (err & ATA_IDNF)
1443c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_INVALID;
1444c6fd2807SJeff Garzik 		break;
1445c6fd2807SJeff Garzik 
1446c6fd2807SJeff Garzik 	case ATA_DEV_ATAPI:
1447a569a30dSTejun Heo 		if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
144856287768SAlbert Lee 			tmp = atapi_eh_request_sense(qc);
1449c6fd2807SJeff Garzik 			if (!tmp) {
1450a569a30dSTejun Heo 				/* ATA_QCFLAG_SENSE_VALID is used to
1451a569a30dSTejun Heo 				 * tell atapi_qc_complete() that sense
1452a569a30dSTejun Heo 				 * data is already valid.
1453c6fd2807SJeff Garzik 				 *
1454c6fd2807SJeff Garzik 				 * TODO: interpret sense data and set
1455c6fd2807SJeff Garzik 				 * appropriate err_mask.
1456c6fd2807SJeff Garzik 				 */
1457c6fd2807SJeff Garzik 				qc->flags |= ATA_QCFLAG_SENSE_VALID;
1458c6fd2807SJeff Garzik 			} else
1459c6fd2807SJeff Garzik 				qc->err_mask |= tmp;
1460c6fd2807SJeff Garzik 		}
1461a569a30dSTejun Heo 	}
1462c6fd2807SJeff Garzik 
1463c6fd2807SJeff Garzik 	if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1464cf480626STejun Heo 		action |= ATA_EH_RESET;
1465c6fd2807SJeff Garzik 
1466c6fd2807SJeff Garzik 	return action;
1467c6fd2807SJeff Garzik }
1468c6fd2807SJeff Garzik 
146976326ac1STejun Heo static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask,
147076326ac1STejun Heo 				   int *xfer_ok)
1471c6fd2807SJeff Garzik {
147276326ac1STejun Heo 	int base = 0;
147376326ac1STejun Heo 
147476326ac1STejun Heo 	if (!(eflags & ATA_EFLAG_DUBIOUS_XFER))
147576326ac1STejun Heo 		*xfer_ok = 1;
147676326ac1STejun Heo 
147776326ac1STejun Heo 	if (!*xfer_ok)
147875f9cafcSTejun Heo 		base = ATA_ECAT_DUBIOUS_NONE;
147976326ac1STejun Heo 
14807d47e8d4STejun Heo 	if (err_mask & AC_ERR_ATA_BUS)
148176326ac1STejun Heo 		return base + ATA_ECAT_ATA_BUS;
1482c6fd2807SJeff Garzik 
14837d47e8d4STejun Heo 	if (err_mask & AC_ERR_TIMEOUT)
148476326ac1STejun Heo 		return base + ATA_ECAT_TOUT_HSM;
14857d47e8d4STejun Heo 
14863884f7b0STejun Heo 	if (eflags & ATA_EFLAG_IS_IO) {
14877d47e8d4STejun Heo 		if (err_mask & AC_ERR_HSM)
148876326ac1STejun Heo 			return base + ATA_ECAT_TOUT_HSM;
14897d47e8d4STejun Heo 		if ((err_mask &
14907d47e8d4STejun Heo 		     (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
149176326ac1STejun Heo 			return base + ATA_ECAT_UNK_DEV;
1492c6fd2807SJeff Garzik 	}
1493c6fd2807SJeff Garzik 
1494c6fd2807SJeff Garzik 	return 0;
1495c6fd2807SJeff Garzik }
1496c6fd2807SJeff Garzik 
14977d47e8d4STejun Heo struct speed_down_verdict_arg {
1498c6fd2807SJeff Garzik 	u64 since;
149976326ac1STejun Heo 	int xfer_ok;
15003884f7b0STejun Heo 	int nr_errors[ATA_ECAT_NR];
1501c6fd2807SJeff Garzik };
1502c6fd2807SJeff Garzik 
15037d47e8d4STejun Heo static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
1504c6fd2807SJeff Garzik {
15057d47e8d4STejun Heo 	struct speed_down_verdict_arg *arg = void_arg;
150676326ac1STejun Heo 	int cat;
1507c6fd2807SJeff Garzik 
1508c6fd2807SJeff Garzik 	if (ent->timestamp < arg->since)
1509c6fd2807SJeff Garzik 		return -1;
1510c6fd2807SJeff Garzik 
151176326ac1STejun Heo 	cat = ata_eh_categorize_error(ent->eflags, ent->err_mask,
151276326ac1STejun Heo 				      &arg->xfer_ok);
15137d47e8d4STejun Heo 	arg->nr_errors[cat]++;
151476326ac1STejun Heo 
1515c6fd2807SJeff Garzik 	return 0;
1516c6fd2807SJeff Garzik }
1517c6fd2807SJeff Garzik 
1518c6fd2807SJeff Garzik /**
15197d47e8d4STejun Heo  *	ata_eh_speed_down_verdict - Determine speed down verdict
1520c6fd2807SJeff Garzik  *	@dev: Device of interest
1521c6fd2807SJeff Garzik  *
1522c6fd2807SJeff Garzik  *	This function examines error ring of @dev and determines
15237d47e8d4STejun Heo  *	whether NCQ needs to be turned off, transfer speed should be
15247d47e8d4STejun Heo  *	stepped down, or falling back to PIO is necessary.
1525c6fd2807SJeff Garzik  *
15263884f7b0STejun Heo  *	ECAT_ATA_BUS	: ATA_BUS error for any command
1527c6fd2807SJeff Garzik  *
15283884f7b0STejun Heo  *	ECAT_TOUT_HSM	: TIMEOUT for any command or HSM violation for
15293884f7b0STejun Heo  *			  IO commands
15307d47e8d4STejun Heo  *
15313884f7b0STejun Heo  *	ECAT_UNK_DEV	: Unknown DEV error for IO commands
1532c6fd2807SJeff Garzik  *
153376326ac1STejun Heo  *	ECAT_DUBIOUS_*	: Identical to above three but occurred while
153476326ac1STejun Heo  *			  data transfer hasn't been verified.
153576326ac1STejun Heo  *
15363884f7b0STejun Heo  *	Verdicts are
15377d47e8d4STejun Heo  *
15383884f7b0STejun Heo  *	NCQ_OFF		: Turn off NCQ.
15397d47e8d4STejun Heo  *
15403884f7b0STejun Heo  *	SPEED_DOWN	: Speed down transfer speed but don't fall back
15413884f7b0STejun Heo  *			  to PIO.
15423884f7b0STejun Heo  *
15433884f7b0STejun Heo  *	FALLBACK_TO_PIO	: Fall back to PIO.
15443884f7b0STejun Heo  *
15453884f7b0STejun Heo  *	Even if multiple verdicts are returned, only one action is
154676326ac1STejun Heo  *	taken per error.  An action triggered by non-DUBIOUS errors
154776326ac1STejun Heo  *	clears ering, while one triggered by DUBIOUS_* errors doesn't.
154876326ac1STejun Heo  *	This is to expedite speed down decisions right after device is
154976326ac1STejun Heo  *	initially configured.
15503884f7b0STejun Heo  *
155176326ac1STejun Heo  *	The followings are speed down rules.  #1 and #2 deal with
155276326ac1STejun Heo  *	DUBIOUS errors.
155376326ac1STejun Heo  *
155476326ac1STejun Heo  *	1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors
155576326ac1STejun Heo  *	   occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO.
155676326ac1STejun Heo  *
155776326ac1STejun Heo  *	2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors
155876326ac1STejun Heo  *	   occurred during last 5 mins, NCQ_OFF.
155976326ac1STejun Heo  *
156076326ac1STejun Heo  *	3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors
15613884f7b0STejun Heo  *	   ocurred during last 5 mins, FALLBACK_TO_PIO
15623884f7b0STejun Heo  *
156376326ac1STejun Heo  *	4. If more than 3 TOUT_HSM or UNK_DEV errors occurred
15643884f7b0STejun Heo  *	   during last 10 mins, NCQ_OFF.
15653884f7b0STejun Heo  *
156676326ac1STejun Heo  *	5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6
15673884f7b0STejun Heo  *	   UNK_DEV errors occurred during last 10 mins, SPEED_DOWN.
15687d47e8d4STejun Heo  *
1569c6fd2807SJeff Garzik  *	LOCKING:
1570c6fd2807SJeff Garzik  *	Inherited from caller.
1571c6fd2807SJeff Garzik  *
1572c6fd2807SJeff Garzik  *	RETURNS:
15737d47e8d4STejun Heo  *	OR of ATA_EH_SPDN_* flags.
1574c6fd2807SJeff Garzik  */
15757d47e8d4STejun Heo static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
1576c6fd2807SJeff Garzik {
15777d47e8d4STejun Heo 	const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
15787d47e8d4STejun Heo 	u64 j64 = get_jiffies_64();
15797d47e8d4STejun Heo 	struct speed_down_verdict_arg arg;
15807d47e8d4STejun Heo 	unsigned int verdict = 0;
1581c6fd2807SJeff Garzik 
15823884f7b0STejun Heo 	/* scan past 5 mins of error history */
15833884f7b0STejun Heo 	memset(&arg, 0, sizeof(arg));
15843884f7b0STejun Heo 	arg.since = j64 - min(j64, j5mins);
15853884f7b0STejun Heo 	ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
15863884f7b0STejun Heo 
158776326ac1STejun Heo 	if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] +
158876326ac1STejun Heo 	    arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1)
158976326ac1STejun Heo 		verdict |= ATA_EH_SPDN_SPEED_DOWN |
159076326ac1STejun Heo 			ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS;
159176326ac1STejun Heo 
159276326ac1STejun Heo 	if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] +
159376326ac1STejun Heo 	    arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1)
159476326ac1STejun Heo 		verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS;
159576326ac1STejun Heo 
15963884f7b0STejun Heo 	if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
15973884f7b0STejun Heo 	    arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1598663f99b8STejun Heo 	    arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
15993884f7b0STejun Heo 		verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
16003884f7b0STejun Heo 
16017d47e8d4STejun Heo 	/* scan past 10 mins of error history */
1602c6fd2807SJeff Garzik 	memset(&arg, 0, sizeof(arg));
16037d47e8d4STejun Heo 	arg.since = j64 - min(j64, j10mins);
16047d47e8d4STejun Heo 	ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1605c6fd2807SJeff Garzik 
16063884f7b0STejun Heo 	if (arg.nr_errors[ATA_ECAT_TOUT_HSM] +
16073884f7b0STejun Heo 	    arg.nr_errors[ATA_ECAT_UNK_DEV] > 3)
16087d47e8d4STejun Heo 		verdict |= ATA_EH_SPDN_NCQ_OFF;
16093884f7b0STejun Heo 
16103884f7b0STejun Heo 	if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
16113884f7b0STejun Heo 	    arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 ||
1612663f99b8STejun Heo 	    arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
16137d47e8d4STejun Heo 		verdict |= ATA_EH_SPDN_SPEED_DOWN;
1614c6fd2807SJeff Garzik 
16157d47e8d4STejun Heo 	return verdict;
1616c6fd2807SJeff Garzik }
1617c6fd2807SJeff Garzik 
1618c6fd2807SJeff Garzik /**
1619c6fd2807SJeff Garzik  *	ata_eh_speed_down - record error and speed down if necessary
1620c6fd2807SJeff Garzik  *	@dev: Failed device
16213884f7b0STejun Heo  *	@eflags: mask of ATA_EFLAG_* flags
1622c6fd2807SJeff Garzik  *	@err_mask: err_mask of the error
1623c6fd2807SJeff Garzik  *
1624c6fd2807SJeff Garzik  *	Record error and examine error history to determine whether
1625c6fd2807SJeff Garzik  *	adjusting transmission speed is necessary.  It also sets
1626c6fd2807SJeff Garzik  *	transmission limits appropriately if such adjustment is
1627c6fd2807SJeff Garzik  *	necessary.
1628c6fd2807SJeff Garzik  *
1629c6fd2807SJeff Garzik  *	LOCKING:
1630c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1631c6fd2807SJeff Garzik  *
1632c6fd2807SJeff Garzik  *	RETURNS:
16337d47e8d4STejun Heo  *	Determined recovery action.
1634c6fd2807SJeff Garzik  */
16353884f7b0STejun Heo static unsigned int ata_eh_speed_down(struct ata_device *dev,
16363884f7b0STejun Heo 				unsigned int eflags, unsigned int err_mask)
1637c6fd2807SJeff Garzik {
16383884f7b0STejun Heo 	struct ata_link *link = dev->link;
163976326ac1STejun Heo 	int xfer_ok = 0;
16407d47e8d4STejun Heo 	unsigned int verdict;
16417d47e8d4STejun Heo 	unsigned int action = 0;
16427d47e8d4STejun Heo 
16437d47e8d4STejun Heo 	/* don't bother if Cat-0 error */
164476326ac1STejun Heo 	if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0)
1645c6fd2807SJeff Garzik 		return 0;
1646c6fd2807SJeff Garzik 
1647c6fd2807SJeff Garzik 	/* record error and determine whether speed down is necessary */
16483884f7b0STejun Heo 	ata_ering_record(&dev->ering, eflags, err_mask);
16497d47e8d4STejun Heo 	verdict = ata_eh_speed_down_verdict(dev);
1650c6fd2807SJeff Garzik 
16517d47e8d4STejun Heo 	/* turn off NCQ? */
16527d47e8d4STejun Heo 	if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
16537d47e8d4STejun Heo 	    (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
16547d47e8d4STejun Heo 			   ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
16557d47e8d4STejun Heo 		dev->flags |= ATA_DFLAG_NCQ_OFF;
16567d47e8d4STejun Heo 		ata_dev_printk(dev, KERN_WARNING,
16577d47e8d4STejun Heo 			       "NCQ disabled due to excessive errors\n");
16587d47e8d4STejun Heo 		goto done;
16597d47e8d4STejun Heo 	}
1660c6fd2807SJeff Garzik 
16617d47e8d4STejun Heo 	/* speed down? */
16627d47e8d4STejun Heo 	if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1663c6fd2807SJeff Garzik 		/* speed down SATA link speed if possible */
16643884f7b0STejun Heo 		if (sata_down_spd_limit(link) == 0) {
1665cf480626STejun Heo 			action |= ATA_EH_RESET;
16667d47e8d4STejun Heo 			goto done;
16677d47e8d4STejun Heo 		}
1668c6fd2807SJeff Garzik 
1669c6fd2807SJeff Garzik 		/* lower transfer mode */
16707d47e8d4STejun Heo 		if (dev->spdn_cnt < 2) {
16717d47e8d4STejun Heo 			static const int dma_dnxfer_sel[] =
16727d47e8d4STejun Heo 				{ ATA_DNXFER_DMA, ATA_DNXFER_40C };
16737d47e8d4STejun Heo 			static const int pio_dnxfer_sel[] =
16747d47e8d4STejun Heo 				{ ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
16757d47e8d4STejun Heo 			int sel;
1676c6fd2807SJeff Garzik 
16777d47e8d4STejun Heo 			if (dev->xfer_shift != ATA_SHIFT_PIO)
16787d47e8d4STejun Heo 				sel = dma_dnxfer_sel[dev->spdn_cnt];
16797d47e8d4STejun Heo 			else
16807d47e8d4STejun Heo 				sel = pio_dnxfer_sel[dev->spdn_cnt];
16817d47e8d4STejun Heo 
16827d47e8d4STejun Heo 			dev->spdn_cnt++;
16837d47e8d4STejun Heo 
16847d47e8d4STejun Heo 			if (ata_down_xfermask_limit(dev, sel) == 0) {
1685cf480626STejun Heo 				action |= ATA_EH_RESET;
16867d47e8d4STejun Heo 				goto done;
16877d47e8d4STejun Heo 			}
16887d47e8d4STejun Heo 		}
16897d47e8d4STejun Heo 	}
16907d47e8d4STejun Heo 
16917d47e8d4STejun Heo 	/* Fall back to PIO?  Slowing down to PIO is meaningless for
1692663f99b8STejun Heo 	 * SATA ATA devices.  Consider it only for PATA and SATAPI.
16937d47e8d4STejun Heo 	 */
16947d47e8d4STejun Heo 	if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
1695663f99b8STejun Heo 	    (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) &&
16967d47e8d4STejun Heo 	    (dev->xfer_shift != ATA_SHIFT_PIO)) {
16977d47e8d4STejun Heo 		if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
16987d47e8d4STejun Heo 			dev->spdn_cnt = 0;
1699cf480626STejun Heo 			action |= ATA_EH_RESET;
17007d47e8d4STejun Heo 			goto done;
17017d47e8d4STejun Heo 		}
17027d47e8d4STejun Heo 	}
17037d47e8d4STejun Heo 
1704c6fd2807SJeff Garzik 	return 0;
17057d47e8d4STejun Heo  done:
17067d47e8d4STejun Heo 	/* device has been slowed down, blow error history */
170776326ac1STejun Heo 	if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS))
17087d47e8d4STejun Heo 		ata_ering_clear(&dev->ering);
17097d47e8d4STejun Heo 	return action;
1710c6fd2807SJeff Garzik }
1711c6fd2807SJeff Garzik 
1712c6fd2807SJeff Garzik /**
17139b1e2658STejun Heo  *	ata_eh_link_autopsy - analyze error and determine recovery action
17149b1e2658STejun Heo  *	@link: host link to perform autopsy on
1715c6fd2807SJeff Garzik  *
17160260731fSTejun Heo  *	Analyze why @link failed and determine which recovery actions
17170260731fSTejun Heo  *	are needed.  This function also sets more detailed AC_ERR_*
17180260731fSTejun Heo  *	values and fills sense data for ATAPI CHECK SENSE.
1719c6fd2807SJeff Garzik  *
1720c6fd2807SJeff Garzik  *	LOCKING:
1721c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1722c6fd2807SJeff Garzik  */
17239b1e2658STejun Heo static void ata_eh_link_autopsy(struct ata_link *link)
1724c6fd2807SJeff Garzik {
17250260731fSTejun Heo 	struct ata_port *ap = link->ap;
1726936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
1727dfcc173dSTejun Heo 	struct ata_device *dev;
17283884f7b0STejun Heo 	unsigned int all_err_mask = 0, eflags = 0;
17293884f7b0STejun Heo 	int tag;
1730c6fd2807SJeff Garzik 	u32 serror;
1731c6fd2807SJeff Garzik 	int rc;
1732c6fd2807SJeff Garzik 
1733c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
1734c6fd2807SJeff Garzik 
1735c6fd2807SJeff Garzik 	if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1736c6fd2807SJeff Garzik 		return;
1737c6fd2807SJeff Garzik 
1738c6fd2807SJeff Garzik 	/* obtain and analyze SError */
1739936fd732STejun Heo 	rc = sata_scr_read(link, SCR_ERROR, &serror);
1740c6fd2807SJeff Garzik 	if (rc == 0) {
1741c6fd2807SJeff Garzik 		ehc->i.serror |= serror;
17420260731fSTejun Heo 		ata_eh_analyze_serror(link);
17434e57c517STejun Heo 	} else if (rc != -EOPNOTSUPP) {
1744cf480626STejun Heo 		/* SError read failed, force reset and probing */
1745b558edddSTejun Heo 		ehc->i.probe_mask |= ATA_ALL_DEVICES;
1746cf480626STejun Heo 		ehc->i.action |= ATA_EH_RESET;
17474e57c517STejun Heo 		ehc->i.err_mask |= AC_ERR_OTHER;
17484e57c517STejun Heo 	}
1749c6fd2807SJeff Garzik 
1750c6fd2807SJeff Garzik 	/* analyze NCQ failure */
17510260731fSTejun Heo 	ata_eh_analyze_ncq_error(link);
1752c6fd2807SJeff Garzik 
1753c6fd2807SJeff Garzik 	/* any real error trumps AC_ERR_OTHER */
1754c6fd2807SJeff Garzik 	if (ehc->i.err_mask & ~AC_ERR_OTHER)
1755c6fd2807SJeff Garzik 		ehc->i.err_mask &= ~AC_ERR_OTHER;
1756c6fd2807SJeff Garzik 
1757c6fd2807SJeff Garzik 	all_err_mask |= ehc->i.err_mask;
1758c6fd2807SJeff Garzik 
1759c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1760c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1761c6fd2807SJeff Garzik 
17620260731fSTejun Heo 		if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
1763c6fd2807SJeff Garzik 			continue;
1764c6fd2807SJeff Garzik 
1765c6fd2807SJeff Garzik 		/* inherit upper level err_mask */
1766c6fd2807SJeff Garzik 		qc->err_mask |= ehc->i.err_mask;
1767c6fd2807SJeff Garzik 
1768c6fd2807SJeff Garzik 		/* analyze TF */
1769c6fd2807SJeff Garzik 		ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1770c6fd2807SJeff Garzik 
1771c6fd2807SJeff Garzik 		/* DEV errors are probably spurious in case of ATA_BUS error */
1772c6fd2807SJeff Garzik 		if (qc->err_mask & AC_ERR_ATA_BUS)
1773c6fd2807SJeff Garzik 			qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1774c6fd2807SJeff Garzik 					  AC_ERR_INVALID);
1775c6fd2807SJeff Garzik 
1776c6fd2807SJeff Garzik 		/* any real error trumps unknown error */
1777c6fd2807SJeff Garzik 		if (qc->err_mask & ~AC_ERR_OTHER)
1778c6fd2807SJeff Garzik 			qc->err_mask &= ~AC_ERR_OTHER;
1779c6fd2807SJeff Garzik 
1780c6fd2807SJeff Garzik 		/* SENSE_VALID trumps dev/unknown error and revalidation */
1781f90f0828STejun Heo 		if (qc->flags & ATA_QCFLAG_SENSE_VALID)
1782c6fd2807SJeff Garzik 			qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1783c6fd2807SJeff Garzik 
178403faab78STejun Heo 		/* determine whether the command is worth retrying */
178503faab78STejun Heo 		if (!(qc->err_mask & AC_ERR_INVALID) &&
178603faab78STejun Heo 		    ((qc->flags & ATA_QCFLAG_IO) || qc->err_mask != AC_ERR_DEV))
178703faab78STejun Heo 			qc->flags |= ATA_QCFLAG_RETRY;
178803faab78STejun Heo 
1789c6fd2807SJeff Garzik 		/* accumulate error info */
1790c6fd2807SJeff Garzik 		ehc->i.dev = qc->dev;
1791c6fd2807SJeff Garzik 		all_err_mask |= qc->err_mask;
1792c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_IO)
17933884f7b0STejun Heo 			eflags |= ATA_EFLAG_IS_IO;
1794c6fd2807SJeff Garzik 	}
1795c6fd2807SJeff Garzik 
1796c6fd2807SJeff Garzik 	/* enforce default EH actions */
1797c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN ||
1798c6fd2807SJeff Garzik 	    all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
1799cf480626STejun Heo 		ehc->i.action |= ATA_EH_RESET;
18003884f7b0STejun Heo 	else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) ||
18013884f7b0STejun Heo 		 (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV)))
1802c6fd2807SJeff Garzik 		ehc->i.action |= ATA_EH_REVALIDATE;
1803c6fd2807SJeff Garzik 
1804dfcc173dSTejun Heo 	/* If we have offending qcs and the associated failed device,
1805dfcc173dSTejun Heo 	 * perform per-dev EH action only on the offending device.
1806dfcc173dSTejun Heo 	 */
1807c6fd2807SJeff Garzik 	if (ehc->i.dev) {
1808c6fd2807SJeff Garzik 		ehc->i.dev_action[ehc->i.dev->devno] |=
1809c6fd2807SJeff Garzik 			ehc->i.action & ATA_EH_PERDEV_MASK;
1810c6fd2807SJeff Garzik 		ehc->i.action &= ~ATA_EH_PERDEV_MASK;
1811c6fd2807SJeff Garzik 	}
1812c6fd2807SJeff Garzik 
18132695e366STejun Heo 	/* propagate timeout to host link */
18142695e366STejun Heo 	if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link))
18152695e366STejun Heo 		ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT;
18162695e366STejun Heo 
18172695e366STejun Heo 	/* record error and consider speeding down */
1818dfcc173dSTejun Heo 	dev = ehc->i.dev;
18192695e366STejun Heo 	if (!dev && ((ata_link_max_devices(link) == 1 &&
18202695e366STejun Heo 		      ata_dev_enabled(link->device))))
1821dfcc173dSTejun Heo 	    dev = link->device;
1822dfcc173dSTejun Heo 
182376326ac1STejun Heo 	if (dev) {
182476326ac1STejun Heo 		if (dev->flags & ATA_DFLAG_DUBIOUS_XFER)
182576326ac1STejun Heo 			eflags |= ATA_EFLAG_DUBIOUS_XFER;
18263884f7b0STejun Heo 		ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask);
182776326ac1STejun Heo 	}
1828dfcc173dSTejun Heo 
1829c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
1830c6fd2807SJeff Garzik }
1831c6fd2807SJeff Garzik 
1832c6fd2807SJeff Garzik /**
18339b1e2658STejun Heo  *	ata_eh_autopsy - analyze error and determine recovery action
18349b1e2658STejun Heo  *	@ap: host port to perform autopsy on
18359b1e2658STejun Heo  *
18369b1e2658STejun Heo  *	Analyze all links of @ap and determine why they failed and
18379b1e2658STejun Heo  *	which recovery actions are needed.
18389b1e2658STejun Heo  *
18399b1e2658STejun Heo  *	LOCKING:
18409b1e2658STejun Heo  *	Kernel thread context (may sleep).
18419b1e2658STejun Heo  */
1842fb7fd614STejun Heo void ata_eh_autopsy(struct ata_port *ap)
18439b1e2658STejun Heo {
18449b1e2658STejun Heo 	struct ata_link *link;
18459b1e2658STejun Heo 
18462695e366STejun Heo 	ata_port_for_each_link(link, ap)
18479b1e2658STejun Heo 		ata_eh_link_autopsy(link);
18482695e366STejun Heo 
18492695e366STejun Heo 	/* Autopsy of fanout ports can affect host link autopsy.
18502695e366STejun Heo 	 * Perform host link autopsy last.
18512695e366STejun Heo 	 */
1852071f44b1STejun Heo 	if (sata_pmp_attached(ap))
18532695e366STejun Heo 		ata_eh_link_autopsy(&ap->link);
18549b1e2658STejun Heo }
18559b1e2658STejun Heo 
18569b1e2658STejun Heo /**
18579b1e2658STejun Heo  *	ata_eh_link_report - report error handling to user
18580260731fSTejun Heo  *	@link: ATA link EH is going on
1859c6fd2807SJeff Garzik  *
1860c6fd2807SJeff Garzik  *	Report EH to user.
1861c6fd2807SJeff Garzik  *
1862c6fd2807SJeff Garzik  *	LOCKING:
1863c6fd2807SJeff Garzik  *	None.
1864c6fd2807SJeff Garzik  */
18659b1e2658STejun Heo static void ata_eh_link_report(struct ata_link *link)
1866c6fd2807SJeff Garzik {
18670260731fSTejun Heo 	struct ata_port *ap = link->ap;
18680260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
1869c6fd2807SJeff Garzik 	const char *frozen, *desc;
1870a1e10f7eSTejun Heo 	char tries_buf[6];
1871c6fd2807SJeff Garzik 	int tag, nr_failed = 0;
1872c6fd2807SJeff Garzik 
187394ff3d54STejun Heo 	if (ehc->i.flags & ATA_EHI_QUIET)
187494ff3d54STejun Heo 		return;
187594ff3d54STejun Heo 
1876c6fd2807SJeff Garzik 	desc = NULL;
1877c6fd2807SJeff Garzik 	if (ehc->i.desc[0] != '\0')
1878c6fd2807SJeff Garzik 		desc = ehc->i.desc;
1879c6fd2807SJeff Garzik 
1880c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1881c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1882c6fd2807SJeff Garzik 
1883e027bd36STejun Heo 		if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link ||
1884e027bd36STejun Heo 		    ((qc->flags & ATA_QCFLAG_QUIET) &&
1885e027bd36STejun Heo 		     qc->err_mask == AC_ERR_DEV))
1886c6fd2807SJeff Garzik 			continue;
1887c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1888c6fd2807SJeff Garzik 			continue;
1889c6fd2807SJeff Garzik 
1890c6fd2807SJeff Garzik 		nr_failed++;
1891c6fd2807SJeff Garzik 	}
1892c6fd2807SJeff Garzik 
1893c6fd2807SJeff Garzik 	if (!nr_failed && !ehc->i.err_mask)
1894c6fd2807SJeff Garzik 		return;
1895c6fd2807SJeff Garzik 
1896c6fd2807SJeff Garzik 	frozen = "";
1897c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN)
1898c6fd2807SJeff Garzik 		frozen = " frozen";
1899c6fd2807SJeff Garzik 
1900a1e10f7eSTejun Heo 	memset(tries_buf, 0, sizeof(tries_buf));
1901a1e10f7eSTejun Heo 	if (ap->eh_tries < ATA_EH_MAX_TRIES)
1902a1e10f7eSTejun Heo 		snprintf(tries_buf, sizeof(tries_buf) - 1, " t%d",
1903a1e10f7eSTejun Heo 			 ap->eh_tries);
1904a1e10f7eSTejun Heo 
1905c6fd2807SJeff Garzik 	if (ehc->i.dev) {
1906c6fd2807SJeff Garzik 		ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1907a1e10f7eSTejun Heo 			       "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1908a1e10f7eSTejun Heo 			       ehc->i.err_mask, link->sactive, ehc->i.serror,
1909a1e10f7eSTejun Heo 			       ehc->i.action, frozen, tries_buf);
1910c6fd2807SJeff Garzik 		if (desc)
1911b64bbc39STejun Heo 			ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
1912c6fd2807SJeff Garzik 	} else {
19130260731fSTejun Heo 		ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
1914a1e10f7eSTejun Heo 				"SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1915a1e10f7eSTejun Heo 				ehc->i.err_mask, link->sactive, ehc->i.serror,
1916a1e10f7eSTejun Heo 				ehc->i.action, frozen, tries_buf);
1917c6fd2807SJeff Garzik 		if (desc)
19180260731fSTejun Heo 			ata_link_printk(link, KERN_ERR, "%s\n", desc);
1919c6fd2807SJeff Garzik 	}
1920c6fd2807SJeff Garzik 
19211333e194SRobert Hancock 	if (ehc->i.serror)
19221333e194SRobert Hancock 		ata_port_printk(ap, KERN_ERR,
19231333e194SRobert Hancock 		  "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
19241333e194SRobert Hancock 		  ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
19251333e194SRobert Hancock 		  ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
19261333e194SRobert Hancock 		  ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
19271333e194SRobert Hancock 		  ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
19281333e194SRobert Hancock 		  ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
19291333e194SRobert Hancock 		  ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
19301333e194SRobert Hancock 		  ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
19311333e194SRobert Hancock 		  ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
19321333e194SRobert Hancock 		  ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
19331333e194SRobert Hancock 		  ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
19341333e194SRobert Hancock 		  ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
19351333e194SRobert Hancock 		  ehc->i.serror & SERR_CRC ? "BadCRC " : "",
19361333e194SRobert Hancock 		  ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
19371333e194SRobert Hancock 		  ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
19381333e194SRobert Hancock 		  ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
19391333e194SRobert Hancock 		  ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
19401333e194SRobert Hancock 		  ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
19411333e194SRobert Hancock 
1942c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1943c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
19448a937581STejun Heo 		struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
1945abb6a889STejun Heo 		const u8 *cdb = qc->cdb;
1946abb6a889STejun Heo 		char data_buf[20] = "";
1947abb6a889STejun Heo 		char cdb_buf[70] = "";
1948c6fd2807SJeff Garzik 
19490260731fSTejun Heo 		if (!(qc->flags & ATA_QCFLAG_FAILED) ||
19500260731fSTejun Heo 		    qc->dev->link != link || !qc->err_mask)
1951c6fd2807SJeff Garzik 			continue;
1952c6fd2807SJeff Garzik 
1953abb6a889STejun Heo 		if (qc->dma_dir != DMA_NONE) {
1954abb6a889STejun Heo 			static const char *dma_str[] = {
1955abb6a889STejun Heo 				[DMA_BIDIRECTIONAL]	= "bidi",
1956abb6a889STejun Heo 				[DMA_TO_DEVICE]		= "out",
1957abb6a889STejun Heo 				[DMA_FROM_DEVICE]	= "in",
1958abb6a889STejun Heo 			};
1959abb6a889STejun Heo 			static const char *prot_str[] = {
1960abb6a889STejun Heo 				[ATA_PROT_PIO]		= "pio",
1961abb6a889STejun Heo 				[ATA_PROT_DMA]		= "dma",
1962abb6a889STejun Heo 				[ATA_PROT_NCQ]		= "ncq",
19630dc36888STejun Heo 				[ATAPI_PROT_PIO]	= "pio",
19640dc36888STejun Heo 				[ATAPI_PROT_DMA]	= "dma",
1965abb6a889STejun Heo 			};
1966abb6a889STejun Heo 
1967abb6a889STejun Heo 			snprintf(data_buf, sizeof(data_buf), " %s %u %s",
1968abb6a889STejun Heo 				 prot_str[qc->tf.protocol], qc->nbytes,
1969abb6a889STejun Heo 				 dma_str[qc->dma_dir]);
1970abb6a889STejun Heo 		}
1971abb6a889STejun Heo 
1972e39eec13SJeff Garzik 		if (ata_is_atapi(qc->tf.protocol))
1973abb6a889STejun Heo 			snprintf(cdb_buf, sizeof(cdb_buf),
1974abb6a889STejun Heo 				 "cdb %02x %02x %02x %02x %02x %02x %02x %02x  "
1975abb6a889STejun Heo 				 "%02x %02x %02x %02x %02x %02x %02x %02x\n         ",
1976abb6a889STejun Heo 				 cdb[0], cdb[1], cdb[2], cdb[3],
1977abb6a889STejun Heo 				 cdb[4], cdb[5], cdb[6], cdb[7],
1978abb6a889STejun Heo 				 cdb[8], cdb[9], cdb[10], cdb[11],
1979abb6a889STejun Heo 				 cdb[12], cdb[13], cdb[14], cdb[15]);
1980abb6a889STejun Heo 
19818a937581STejun Heo 		ata_dev_printk(qc->dev, KERN_ERR,
19828a937581STejun Heo 			"cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
1983abb6a889STejun Heo 			"tag %d%s\n         %s"
19848a937581STejun Heo 			"res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
19855335b729STejun Heo 			"Emask 0x%x (%s)%s\n",
19868a937581STejun Heo 			cmd->command, cmd->feature, cmd->nsect,
19878a937581STejun Heo 			cmd->lbal, cmd->lbam, cmd->lbah,
19888a937581STejun Heo 			cmd->hob_feature, cmd->hob_nsect,
19898a937581STejun Heo 			cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
1990abb6a889STejun Heo 			cmd->device, qc->tag, data_buf, cdb_buf,
19918a937581STejun Heo 			res->command, res->feature, res->nsect,
19928a937581STejun Heo 			res->lbal, res->lbam, res->lbah,
19938a937581STejun Heo 			res->hob_feature, res->hob_nsect,
19948a937581STejun Heo 			res->hob_lbal, res->hob_lbam, res->hob_lbah,
19955335b729STejun Heo 			res->device, qc->err_mask, ata_err_string(qc->err_mask),
19965335b729STejun Heo 			qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
19971333e194SRobert Hancock 
19981333e194SRobert Hancock 		if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
19991333e194SRobert Hancock 				    ATA_ERR)) {
20001333e194SRobert Hancock 			if (res->command & ATA_BUSY)
20011333e194SRobert Hancock 				ata_dev_printk(qc->dev, KERN_ERR,
20021333e194SRobert Hancock 				  "status: { Busy }\n");
20031333e194SRobert Hancock 			else
20041333e194SRobert Hancock 				ata_dev_printk(qc->dev, KERN_ERR,
20051333e194SRobert Hancock 				  "status: { %s%s%s%s}\n",
20061333e194SRobert Hancock 				  res->command & ATA_DRDY ? "DRDY " : "",
20071333e194SRobert Hancock 				  res->command & ATA_DF ? "DF " : "",
20081333e194SRobert Hancock 				  res->command & ATA_DRQ ? "DRQ " : "",
20091333e194SRobert Hancock 				  res->command & ATA_ERR ? "ERR " : "");
20101333e194SRobert Hancock 		}
20111333e194SRobert Hancock 
20121333e194SRobert Hancock 		if (cmd->command != ATA_CMD_PACKET &&
20131333e194SRobert Hancock 		    (res->feature & (ATA_ICRC | ATA_UNC | ATA_IDNF |
20141333e194SRobert Hancock 				     ATA_ABORTED)))
20151333e194SRobert Hancock 			ata_dev_printk(qc->dev, KERN_ERR,
20161333e194SRobert Hancock 			  "error: { %s%s%s%s}\n",
20171333e194SRobert Hancock 			  res->feature & ATA_ICRC ? "ICRC " : "",
20181333e194SRobert Hancock 			  res->feature & ATA_UNC ? "UNC " : "",
20191333e194SRobert Hancock 			  res->feature & ATA_IDNF ? "IDNF " : "",
20201333e194SRobert Hancock 			  res->feature & ATA_ABORTED ? "ABRT " : "");
2021c6fd2807SJeff Garzik 	}
2022c6fd2807SJeff Garzik }
2023c6fd2807SJeff Garzik 
20249b1e2658STejun Heo /**
20259b1e2658STejun Heo  *	ata_eh_report - report error handling to user
20269b1e2658STejun Heo  *	@ap: ATA port to report EH about
20279b1e2658STejun Heo  *
20289b1e2658STejun Heo  *	Report EH to user.
20299b1e2658STejun Heo  *
20309b1e2658STejun Heo  *	LOCKING:
20319b1e2658STejun Heo  *	None.
20329b1e2658STejun Heo  */
2033fb7fd614STejun Heo void ata_eh_report(struct ata_port *ap)
20349b1e2658STejun Heo {
20359b1e2658STejun Heo 	struct ata_link *link;
20369b1e2658STejun Heo 
20379b1e2658STejun Heo 	__ata_port_for_each_link(link, ap)
20389b1e2658STejun Heo 		ata_eh_link_report(link);
20399b1e2658STejun Heo }
20409b1e2658STejun Heo 
2041cc0680a5STejun Heo static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
2042d4b2bab4STejun Heo 			unsigned int *classes, unsigned long deadline)
2043c6fd2807SJeff Garzik {
2044f58229f8STejun Heo 	struct ata_device *dev;
2045c6fd2807SJeff Garzik 
2046cc0680a5STejun Heo 	ata_link_for_each_dev(dev, link)
2047f58229f8STejun Heo 		classes[dev->devno] = ATA_DEV_UNKNOWN;
2048c6fd2807SJeff Garzik 
2049f046519fSTejun Heo 	return reset(link, classes, deadline);
2050c6fd2807SJeff Garzik }
2051c6fd2807SJeff Garzik 
2052ae791c05STejun Heo static int ata_eh_followup_srst_needed(struct ata_link *link,
2053ae791c05STejun Heo 				       int rc, int classify,
2054c6fd2807SJeff Garzik 				       const unsigned int *classes)
2055c6fd2807SJeff Garzik {
205645db2f6cSTejun Heo 	if ((link->flags & ATA_LFLAG_NO_SRST) || ata_link_offline(link))
2057ae791c05STejun Heo 		return 0;
2058305d2a1aSTejun Heo 	if (rc == -EAGAIN) {
2059305d2a1aSTejun Heo 		if (classify)
2060c6fd2807SJeff Garzik 			return 1;
2061305d2a1aSTejun Heo 		rc = 0;
2062305d2a1aSTejun Heo 	}
2063c6fd2807SJeff Garzik 	if (rc != 0)
2064c6fd2807SJeff Garzik 		return 0;
2065071f44b1STejun Heo 	if (sata_pmp_supported(link->ap) && ata_is_host_link(link))
20663495de73STejun Heo 		return 1;
2067c6fd2807SJeff Garzik 	return 0;
2068c6fd2807SJeff Garzik }
2069c6fd2807SJeff Garzik 
2070fb7fd614STejun Heo int ata_eh_reset(struct ata_link *link, int classify,
2071c6fd2807SJeff Garzik 		 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
2072c6fd2807SJeff Garzik 		 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
2073c6fd2807SJeff Garzik {
2074416dc9edSTejun Heo 	const int max_tries = ARRAY_SIZE(ata_eh_reset_timeouts);
2075afaa5c37STejun Heo 	struct ata_port *ap = link->ap;
2076936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
2077c6fd2807SJeff Garzik 	unsigned int *classes = ehc->classes;
2078416dc9edSTejun Heo 	unsigned int lflags = link->flags;
2079c6fd2807SJeff Garzik 	int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
208031daabdaSTejun Heo 	int try = 0;
2081f58229f8STejun Heo 	struct ata_device *dev;
2082416dc9edSTejun Heo 	unsigned long deadline, now;
2083c6fd2807SJeff Garzik 	ata_reset_fn_t reset;
2084afaa5c37STejun Heo 	unsigned long flags;
2085416dc9edSTejun Heo 	u32 sstatus;
2086f046519fSTejun Heo 	int nr_known, rc;
2087c6fd2807SJeff Garzik 
2088932648b0STejun Heo 	/*
2089932648b0STejun Heo 	 * Prepare to reset
2090932648b0STejun Heo 	 */
2091afaa5c37STejun Heo 	spin_lock_irqsave(ap->lock, flags);
2092afaa5c37STejun Heo 	ap->pflags |= ATA_PFLAG_RESETTING;
2093afaa5c37STejun Heo 	spin_unlock_irqrestore(ap->lock, flags);
2094afaa5c37STejun Heo 
2095cf480626STejun Heo 	ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
2096c6fd2807SJeff Garzik 
2097cdeab114STejun Heo 	ata_link_for_each_dev(dev, link) {
2098cdeab114STejun Heo 		/* If we issue an SRST then an ATA drive (not ATAPI)
2099cdeab114STejun Heo 		 * may change configuration and be in PIO0 timing. If
2100cdeab114STejun Heo 		 * we do a hard reset (or are coming from power on)
2101cdeab114STejun Heo 		 * this is true for ATA or ATAPI. Until we've set a
2102cdeab114STejun Heo 		 * suitable controller mode we should not touch the
2103cdeab114STejun Heo 		 * bus as we may be talking too fast.
2104cdeab114STejun Heo 		 */
2105cdeab114STejun Heo 		dev->pio_mode = XFER_PIO_0;
2106cdeab114STejun Heo 
2107cdeab114STejun Heo 		/* If the controller has a pio mode setup function
2108cdeab114STejun Heo 		 * then use it to set the chipset to rights. Don't
2109cdeab114STejun Heo 		 * touch the DMA setup as that will be dealt with when
2110cdeab114STejun Heo 		 * configuring devices.
2111cdeab114STejun Heo 		 */
2112cdeab114STejun Heo 		if (ap->ops->set_piomode)
2113cdeab114STejun Heo 			ap->ops->set_piomode(ap, dev);
2114cdeab114STejun Heo 	}
2115cdeab114STejun Heo 
2116cf480626STejun Heo 	/* prefer hardreset */
2117932648b0STejun Heo 	reset = NULL;
2118cf480626STejun Heo 	ehc->i.action &= ~ATA_EH_RESET;
2119cf480626STejun Heo 	if (hardreset) {
2120cf480626STejun Heo 		reset = hardreset;
2121cf480626STejun Heo 		ehc->i.action = ATA_EH_HARDRESET;
21224f7faa3fSTejun Heo 	} else if (softreset) {
2123cf480626STejun Heo 		reset = softreset;
2124cf480626STejun Heo 		ehc->i.action = ATA_EH_SOFTRESET;
2125cf480626STejun Heo 	}
2126c6fd2807SJeff Garzik 
2127c6fd2807SJeff Garzik 	if (prereset) {
2128*341c2c95STejun Heo 		rc = prereset(link,
2129*341c2c95STejun Heo 			      ata_deadline(jiffies, ATA_EH_PRERESET_TIMEOUT));
2130c6fd2807SJeff Garzik 		if (rc) {
2131c961922bSAlan Cox 			if (rc == -ENOENT) {
2132cc0680a5STejun Heo 				ata_link_printk(link, KERN_DEBUG,
21334aa9ab67STejun Heo 						"port disabled. ignoring.\n");
2134cf480626STejun Heo 				ehc->i.action &= ~ATA_EH_RESET;
21354aa9ab67STejun Heo 
2136936fd732STejun Heo 				ata_link_for_each_dev(dev, link)
2137f58229f8STejun Heo 					classes[dev->devno] = ATA_DEV_NONE;
21384aa9ab67STejun Heo 
21394aa9ab67STejun Heo 				rc = 0;
2140c961922bSAlan Cox 			} else
2141cc0680a5STejun Heo 				ata_link_printk(link, KERN_ERR,
2142c6fd2807SJeff Garzik 					"prereset failed (errno=%d)\n", rc);
2143fccb6ea5STejun Heo 			goto out;
2144c6fd2807SJeff Garzik 		}
2145c6fd2807SJeff Garzik 
2146932648b0STejun Heo 		/* prereset() might have cleared ATA_EH_RESET.  If so,
2147932648b0STejun Heo 		 * bang classes and return.
2148932648b0STejun Heo 		 */
2149932648b0STejun Heo 		if (reset && !(ehc->i.action & ATA_EH_RESET)) {
2150936fd732STejun Heo 			ata_link_for_each_dev(dev, link)
2151f58229f8STejun Heo 				classes[dev->devno] = ATA_DEV_NONE;
2152fccb6ea5STejun Heo 			rc = 0;
2153fccb6ea5STejun Heo 			goto out;
2154c6fd2807SJeff Garzik 		}
2155932648b0STejun Heo 	}
2156c6fd2807SJeff Garzik 
2157c6fd2807SJeff Garzik  retry:
2158932648b0STejun Heo 	/*
2159932648b0STejun Heo 	 * Perform reset
2160932648b0STejun Heo 	 */
2161dc98c32cSTejun Heo 	if (ata_is_host_link(link))
2162dc98c32cSTejun Heo 		ata_eh_freeze_port(ap);
2163dc98c32cSTejun Heo 
2164*341c2c95STejun Heo 	deadline = ata_deadline(jiffies, ata_eh_reset_timeouts[try++]);
216531daabdaSTejun Heo 
2166932648b0STejun Heo 	if (reset) {
2167c6fd2807SJeff Garzik 		if (verbose)
2168cc0680a5STejun Heo 			ata_link_printk(link, KERN_INFO, "%s resetting link\n",
2169c6fd2807SJeff Garzik 					reset == softreset ? "soft" : "hard");
2170c6fd2807SJeff Garzik 
2171c6fd2807SJeff Garzik 		/* mark that this EH session started with reset */
21720d64a233STejun Heo 		if (reset == hardreset)
21730d64a233STejun Heo 			ehc->i.flags |= ATA_EHI_DID_HARDRESET;
21740d64a233STejun Heo 		else
21750d64a233STejun Heo 			ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
2176c6fd2807SJeff Garzik 
2177cc0680a5STejun Heo 		rc = ata_do_reset(link, reset, classes, deadline);
2178c6fd2807SJeff Garzik 
2179c6fd2807SJeff Garzik 		if (reset == hardreset &&
2180ae791c05STejun Heo 		    ata_eh_followup_srst_needed(link, rc, classify, classes)) {
2181c6fd2807SJeff Garzik 			/* okay, let's do follow-up softreset */
2182c6fd2807SJeff Garzik 			reset = softreset;
2183c6fd2807SJeff Garzik 
2184c6fd2807SJeff Garzik 			if (!reset) {
2185cc0680a5STejun Heo 				ata_link_printk(link, KERN_ERR,
2186c6fd2807SJeff Garzik 						"follow-up softreset required "
2187c6fd2807SJeff Garzik 						"but no softreset avaliable\n");
2188fccb6ea5STejun Heo 				rc = -EINVAL;
218908cf69d0STejun Heo 				goto fail;
2190c6fd2807SJeff Garzik 			}
2191c6fd2807SJeff Garzik 
2192cf480626STejun Heo 			ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
2193cc0680a5STejun Heo 			rc = ata_do_reset(link, reset, classes, deadline);
2194c6fd2807SJeff Garzik 		}
2195c6fd2807SJeff Garzik 
2196416dc9edSTejun Heo 		/* -EAGAIN can happen if we skipped followup SRST */
2197416dc9edSTejun Heo 		if (rc && rc != -EAGAIN)
2198416dc9edSTejun Heo 			goto fail;
2199932648b0STejun Heo 	} else {
2200932648b0STejun Heo 		if (verbose)
2201932648b0STejun Heo 			ata_link_printk(link, KERN_INFO, "no reset method "
2202932648b0STejun Heo 					"available, skipping reset\n");
2203932648b0STejun Heo 		if (!(lflags & ATA_LFLAG_ASSUME_CLASS))
2204932648b0STejun Heo 			lflags |= ATA_LFLAG_ASSUME_ATA;
2205932648b0STejun Heo 	}
2206008a7896STejun Heo 
2207932648b0STejun Heo 	/*
2208932648b0STejun Heo 	 * Post-reset processing
2209932648b0STejun Heo 	 */
2210ae791c05STejun Heo 	ata_link_for_each_dev(dev, link) {
2211416dc9edSTejun Heo 		/* After the reset, the device state is PIO 0 and the
2212416dc9edSTejun Heo 		 * controller state is undefined.  Reset also wakes up
2213416dc9edSTejun Heo 		 * drives from sleeping mode.
2214c6fd2807SJeff Garzik 		 */
2215f58229f8STejun Heo 		dev->pio_mode = XFER_PIO_0;
2216054a5fbaSTejun Heo 		dev->flags &= ~ATA_DFLAG_SLEEPING;
2217c6fd2807SJeff Garzik 
2218ae791c05STejun Heo 		if (ata_link_offline(link))
2219ae791c05STejun Heo 			continue;
2220ae791c05STejun Heo 
22214ccd3329STejun Heo 		/* apply class override */
2222416dc9edSTejun Heo 		if (lflags & ATA_LFLAG_ASSUME_ATA)
2223ae791c05STejun Heo 			classes[dev->devno] = ATA_DEV_ATA;
2224416dc9edSTejun Heo 		else if (lflags & ATA_LFLAG_ASSUME_SEMB)
2225ae791c05STejun Heo 			classes[dev->devno] = ATA_DEV_SEMB_UNSUP; /* not yet */
2226ae791c05STejun Heo 	}
2227ae791c05STejun Heo 
2228008a7896STejun Heo 	/* record current link speed */
2229936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2230936fd732STejun Heo 		link->sata_spd = (sstatus >> 4) & 0xf;
2231008a7896STejun Heo 
2232dc98c32cSTejun Heo 	/* thaw the port */
2233dc98c32cSTejun Heo 	if (ata_is_host_link(link))
2234dc98c32cSTejun Heo 		ata_eh_thaw_port(ap);
2235dc98c32cSTejun Heo 
2236f046519fSTejun Heo 	/* postreset() should clear hardware SError.  Although SError
2237f046519fSTejun Heo 	 * is cleared during link resume, clearing SError here is
2238f046519fSTejun Heo 	 * necessary as some PHYs raise hotplug events after SRST.
2239f046519fSTejun Heo 	 * This introduces race condition where hotplug occurs between
2240f046519fSTejun Heo 	 * reset and here.  This race is mediated by cross checking
2241f046519fSTejun Heo 	 * link onlineness and classification result later.
2242f046519fSTejun Heo 	 */
2243c6fd2807SJeff Garzik 	if (postreset)
2244cc0680a5STejun Heo 		postreset(link, classes);
2245c6fd2807SJeff Garzik 
2246f046519fSTejun Heo 	/* clear cached SError */
2247f046519fSTejun Heo 	spin_lock_irqsave(link->ap->lock, flags);
2248f046519fSTejun Heo 	link->eh_info.serror = 0;
2249f046519fSTejun Heo 	spin_unlock_irqrestore(link->ap->lock, flags);
2250f046519fSTejun Heo 
2251f046519fSTejun Heo 	/* Make sure onlineness and classification result correspond.
2252f046519fSTejun Heo 	 * Hotplug could have happened during reset and some
2253f046519fSTejun Heo 	 * controllers fail to wait while a drive is spinning up after
2254f046519fSTejun Heo 	 * being hotplugged causing misdetection.  By cross checking
2255f046519fSTejun Heo 	 * link onlineness and classification result, those conditions
2256f046519fSTejun Heo 	 * can be reliably detected and retried.
2257f046519fSTejun Heo 	 */
2258f046519fSTejun Heo 	nr_known = 0;
2259f046519fSTejun Heo 	ata_link_for_each_dev(dev, link) {
2260f046519fSTejun Heo 		/* convert all ATA_DEV_UNKNOWN to ATA_DEV_NONE */
2261f046519fSTejun Heo 		if (classes[dev->devno] == ATA_DEV_UNKNOWN)
2262f046519fSTejun Heo 			classes[dev->devno] = ATA_DEV_NONE;
2263f046519fSTejun Heo 		else
2264f046519fSTejun Heo 			nr_known++;
2265f046519fSTejun Heo 	}
2266f046519fSTejun Heo 
2267f046519fSTejun Heo 	if (classify && !nr_known && ata_link_online(link)) {
2268f046519fSTejun Heo 		if (try < max_tries) {
2269f046519fSTejun Heo 			ata_link_printk(link, KERN_WARNING, "link online but "
2270f046519fSTejun Heo 				       "device misclassified, retrying\n");
2271f046519fSTejun Heo 			rc = -EAGAIN;
2272f046519fSTejun Heo 			goto fail;
2273f046519fSTejun Heo 		}
2274f046519fSTejun Heo 		ata_link_printk(link, KERN_WARNING,
2275f046519fSTejun Heo 			       "link online but device misclassified, "
2276f046519fSTejun Heo 			       "device detection might fail\n");
2277f046519fSTejun Heo 	}
2278f046519fSTejun Heo 
2279c6fd2807SJeff Garzik 	/* reset successful, schedule revalidation */
2280cf480626STejun Heo 	ata_eh_done(link, NULL, ATA_EH_RESET);
2281c6fd2807SJeff Garzik 	ehc->i.action |= ATA_EH_REVALIDATE;
2282416dc9edSTejun Heo 
2283416dc9edSTejun Heo 	rc = 0;
2284fccb6ea5STejun Heo  out:
2285fccb6ea5STejun Heo 	/* clear hotplug flag */
2286fccb6ea5STejun Heo 	ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2287afaa5c37STejun Heo 
2288afaa5c37STejun Heo 	spin_lock_irqsave(ap->lock, flags);
2289afaa5c37STejun Heo 	ap->pflags &= ~ATA_PFLAG_RESETTING;
2290afaa5c37STejun Heo 	spin_unlock_irqrestore(ap->lock, flags);
2291afaa5c37STejun Heo 
2292c6fd2807SJeff Garzik 	return rc;
2293416dc9edSTejun Heo 
2294416dc9edSTejun Heo  fail:
22955958e302STejun Heo 	/* if SCR isn't accessible on a fan-out port, PMP needs to be reset */
22965958e302STejun Heo 	if (!ata_is_host_link(link) &&
22975958e302STejun Heo 	    sata_scr_read(link, SCR_STATUS, &sstatus))
22985958e302STejun Heo 		rc = -ERESTART;
22995958e302STejun Heo 
2300416dc9edSTejun Heo 	if (rc == -ERESTART || try >= max_tries)
2301416dc9edSTejun Heo 		goto out;
2302416dc9edSTejun Heo 
2303416dc9edSTejun Heo 	now = jiffies;
2304416dc9edSTejun Heo 	if (time_before(now, deadline)) {
2305416dc9edSTejun Heo 		unsigned long delta = deadline - now;
2306416dc9edSTejun Heo 
2307416dc9edSTejun Heo 		ata_link_printk(link, KERN_WARNING, "reset failed "
2308416dc9edSTejun Heo 				"(errno=%d), retrying in %u secs\n",
2309416dc9edSTejun Heo 				rc, (jiffies_to_msecs(delta) + 999) / 1000);
2310416dc9edSTejun Heo 
2311416dc9edSTejun Heo 		while (delta)
2312416dc9edSTejun Heo 			delta = schedule_timeout_uninterruptible(delta);
2313416dc9edSTejun Heo 	}
2314416dc9edSTejun Heo 
2315416dc9edSTejun Heo 	if (rc == -EPIPE || try == max_tries - 1)
2316416dc9edSTejun Heo 		sata_down_spd_limit(link);
2317416dc9edSTejun Heo 	if (hardreset)
2318416dc9edSTejun Heo 		reset = hardreset;
2319416dc9edSTejun Heo 	goto retry;
2320c6fd2807SJeff Garzik }
2321c6fd2807SJeff Garzik 
23220260731fSTejun Heo static int ata_eh_revalidate_and_attach(struct ata_link *link,
2323c6fd2807SJeff Garzik 					struct ata_device **r_failed_dev)
2324c6fd2807SJeff Garzik {
23250260731fSTejun Heo 	struct ata_port *ap = link->ap;
23260260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
2327c6fd2807SJeff Garzik 	struct ata_device *dev;
23288c3c52a8STejun Heo 	unsigned int new_mask = 0;
2329c6fd2807SJeff Garzik 	unsigned long flags;
2330f58229f8STejun Heo 	int rc = 0;
2331c6fd2807SJeff Garzik 
2332c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
2333c6fd2807SJeff Garzik 
23348c3c52a8STejun Heo 	/* For PATA drive side cable detection to work, IDENTIFY must
23358c3c52a8STejun Heo 	 * be done backwards such that PDIAG- is released by the slave
23368c3c52a8STejun Heo 	 * device before the master device is identified.
23378c3c52a8STejun Heo 	 */
23380260731fSTejun Heo 	ata_link_for_each_dev_reverse(dev, link) {
2339f58229f8STejun Heo 		unsigned int action = ata_eh_dev_action(dev);
2340f58229f8STejun Heo 		unsigned int readid_flags = 0;
2341c6fd2807SJeff Garzik 
2342bff04647STejun Heo 		if (ehc->i.flags & ATA_EHI_DID_RESET)
2343bff04647STejun Heo 			readid_flags |= ATA_READID_POSTRESET;
2344bff04647STejun Heo 
23459666f400STejun Heo 		if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
2346633273a3STejun Heo 			WARN_ON(dev->class == ATA_DEV_PMP);
2347633273a3STejun Heo 
23480260731fSTejun Heo 			if (ata_link_offline(link)) {
2349c6fd2807SJeff Garzik 				rc = -EIO;
23508c3c52a8STejun Heo 				goto err;
2351c6fd2807SJeff Garzik 			}
2352c6fd2807SJeff Garzik 
23530260731fSTejun Heo 			ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
2354422c9daaSTejun Heo 			rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
2355422c9daaSTejun Heo 						readid_flags);
2356c6fd2807SJeff Garzik 			if (rc)
23578c3c52a8STejun Heo 				goto err;
2358c6fd2807SJeff Garzik 
23590260731fSTejun Heo 			ata_eh_done(link, dev, ATA_EH_REVALIDATE);
2360c6fd2807SJeff Garzik 
2361baa1e78aSTejun Heo 			/* Configuration may have changed, reconfigure
2362baa1e78aSTejun Heo 			 * transfer mode.
2363baa1e78aSTejun Heo 			 */
2364baa1e78aSTejun Heo 			ehc->i.flags |= ATA_EHI_SETMODE;
2365baa1e78aSTejun Heo 
2366c6fd2807SJeff Garzik 			/* schedule the scsi_rescan_device() here */
2367c6fd2807SJeff Garzik 			queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
2368c6fd2807SJeff Garzik 		} else if (dev->class == ATA_DEV_UNKNOWN &&
2369c6fd2807SJeff Garzik 			   ehc->tries[dev->devno] &&
2370c6fd2807SJeff Garzik 			   ata_class_enabled(ehc->classes[dev->devno])) {
2371c6fd2807SJeff Garzik 			dev->class = ehc->classes[dev->devno];
2372c6fd2807SJeff Garzik 
2373633273a3STejun Heo 			if (dev->class == ATA_DEV_PMP)
2374633273a3STejun Heo 				rc = sata_pmp_attach(dev);
2375633273a3STejun Heo 			else
2376633273a3STejun Heo 				rc = ata_dev_read_id(dev, &dev->class,
2377633273a3STejun Heo 						     readid_flags, dev->id);
23788c3c52a8STejun Heo 			switch (rc) {
23798c3c52a8STejun Heo 			case 0:
2380f58229f8STejun Heo 				new_mask |= 1 << dev->devno;
23818c3c52a8STejun Heo 				break;
23828c3c52a8STejun Heo 			case -ENOENT:
238355a8e2c8STejun Heo 				/* IDENTIFY was issued to non-existent
238455a8e2c8STejun Heo 				 * device.  No need to reset.  Just
238555a8e2c8STejun Heo 				 * thaw and kill the device.
238655a8e2c8STejun Heo 				 */
238755a8e2c8STejun Heo 				ata_eh_thaw_port(ap);
238855a8e2c8STejun Heo 				dev->class = ATA_DEV_UNKNOWN;
2389c6fd2807SJeff Garzik 				break;
23908c3c52a8STejun Heo 			default:
23918c3c52a8STejun Heo 				dev->class = ATA_DEV_UNKNOWN;
23928c3c52a8STejun Heo 				goto err;
23938c3c52a8STejun Heo 			}
23948c3c52a8STejun Heo 		}
2395c6fd2807SJeff Garzik 	}
2396c6fd2807SJeff Garzik 
2397c1c4e8d5STejun Heo 	/* PDIAG- should have been released, ask cable type if post-reset */
239833267325STejun Heo 	if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) {
239933267325STejun Heo 		if (ap->ops->cable_detect)
2400c1c4e8d5STejun Heo 			ap->cbl = ap->ops->cable_detect(ap);
240133267325STejun Heo 		ata_force_cbl(ap);
240233267325STejun Heo 	}
2403c1c4e8d5STejun Heo 
24048c3c52a8STejun Heo 	/* Configure new devices forward such that user doesn't see
24058c3c52a8STejun Heo 	 * device detection messages backwards.
24068c3c52a8STejun Heo 	 */
24070260731fSTejun Heo 	ata_link_for_each_dev(dev, link) {
2408633273a3STejun Heo 		if (!(new_mask & (1 << dev->devno)) ||
2409633273a3STejun Heo 		    dev->class == ATA_DEV_PMP)
24108c3c52a8STejun Heo 			continue;
24118c3c52a8STejun Heo 
24128c3c52a8STejun Heo 		ehc->i.flags |= ATA_EHI_PRINTINFO;
24138c3c52a8STejun Heo 		rc = ata_dev_configure(dev);
24148c3c52a8STejun Heo 		ehc->i.flags &= ~ATA_EHI_PRINTINFO;
24158c3c52a8STejun Heo 		if (rc)
24168c3c52a8STejun Heo 			goto err;
24178c3c52a8STejun Heo 
2418c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
2419c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
2420c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
2421baa1e78aSTejun Heo 
242255a8e2c8STejun Heo 		/* new device discovered, configure xfermode */
2423baa1e78aSTejun Heo 		ehc->i.flags |= ATA_EHI_SETMODE;
2424c6fd2807SJeff Garzik 	}
2425c6fd2807SJeff Garzik 
24268c3c52a8STejun Heo 	return 0;
24278c3c52a8STejun Heo 
24288c3c52a8STejun Heo  err:
2429c6fd2807SJeff Garzik 	*r_failed_dev = dev;
24308c3c52a8STejun Heo 	DPRINTK("EXIT rc=%d\n", rc);
2431c6fd2807SJeff Garzik 	return rc;
2432c6fd2807SJeff Garzik }
2433c6fd2807SJeff Garzik 
24346f1d1e3aSTejun Heo /**
24356f1d1e3aSTejun Heo  *	ata_set_mode - Program timings and issue SET FEATURES - XFER
24366f1d1e3aSTejun Heo  *	@link: link on which timings will be programmed
24376f1d1e3aSTejun Heo  *	@r_failed_dev: out paramter for failed device
24386f1d1e3aSTejun Heo  *
24396f1d1e3aSTejun Heo  *	Set ATA device disk transfer mode (PIO3, UDMA6, etc.).  If
24406f1d1e3aSTejun Heo  *	ata_set_mode() fails, pointer to the failing device is
24416f1d1e3aSTejun Heo  *	returned in @r_failed_dev.
24426f1d1e3aSTejun Heo  *
24436f1d1e3aSTejun Heo  *	LOCKING:
24446f1d1e3aSTejun Heo  *	PCI/etc. bus probe sem.
24456f1d1e3aSTejun Heo  *
24466f1d1e3aSTejun Heo  *	RETURNS:
24476f1d1e3aSTejun Heo  *	0 on success, negative errno otherwise
24486f1d1e3aSTejun Heo  */
24496f1d1e3aSTejun Heo int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
24506f1d1e3aSTejun Heo {
24516f1d1e3aSTejun Heo 	struct ata_port *ap = link->ap;
245200115e0fSTejun Heo 	struct ata_device *dev;
245300115e0fSTejun Heo 	int rc;
24546f1d1e3aSTejun Heo 
245576326ac1STejun Heo 	/* if data transfer is verified, clear DUBIOUS_XFER on ering top */
245676326ac1STejun Heo 	ata_link_for_each_dev(dev, link) {
245776326ac1STejun Heo 		if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
245876326ac1STejun Heo 			struct ata_ering_entry *ent;
245976326ac1STejun Heo 
246076326ac1STejun Heo 			ent = ata_ering_top(&dev->ering);
246176326ac1STejun Heo 			if (ent)
246276326ac1STejun Heo 				ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER;
246376326ac1STejun Heo 		}
246476326ac1STejun Heo 	}
246576326ac1STejun Heo 
24666f1d1e3aSTejun Heo 	/* has private set_mode? */
24676f1d1e3aSTejun Heo 	if (ap->ops->set_mode)
246800115e0fSTejun Heo 		rc = ap->ops->set_mode(link, r_failed_dev);
246900115e0fSTejun Heo 	else
247000115e0fSTejun Heo 		rc = ata_do_set_mode(link, r_failed_dev);
247100115e0fSTejun Heo 
247200115e0fSTejun Heo 	/* if transfer mode has changed, set DUBIOUS_XFER on device */
247300115e0fSTejun Heo 	ata_link_for_each_dev(dev, link) {
247400115e0fSTejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
247500115e0fSTejun Heo 		u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
247600115e0fSTejun Heo 		u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
247700115e0fSTejun Heo 
247800115e0fSTejun Heo 		if (dev->xfer_mode != saved_xfer_mode ||
247900115e0fSTejun Heo 		    ata_ncq_enabled(dev) != saved_ncq)
248000115e0fSTejun Heo 			dev->flags |= ATA_DFLAG_DUBIOUS_XFER;
248100115e0fSTejun Heo 	}
248200115e0fSTejun Heo 
248300115e0fSTejun Heo 	return rc;
24846f1d1e3aSTejun Heo }
24856f1d1e3aSTejun Heo 
24860260731fSTejun Heo static int ata_link_nr_enabled(struct ata_link *link)
2487c6fd2807SJeff Garzik {
2488f58229f8STejun Heo 	struct ata_device *dev;
2489f58229f8STejun Heo 	int cnt = 0;
2490c6fd2807SJeff Garzik 
24910260731fSTejun Heo 	ata_link_for_each_dev(dev, link)
2492f58229f8STejun Heo 		if (ata_dev_enabled(dev))
2493c6fd2807SJeff Garzik 			cnt++;
2494c6fd2807SJeff Garzik 	return cnt;
2495c6fd2807SJeff Garzik }
2496c6fd2807SJeff Garzik 
24970260731fSTejun Heo static int ata_link_nr_vacant(struct ata_link *link)
2498c6fd2807SJeff Garzik {
2499f58229f8STejun Heo 	struct ata_device *dev;
2500f58229f8STejun Heo 	int cnt = 0;
2501c6fd2807SJeff Garzik 
25020260731fSTejun Heo 	ata_link_for_each_dev(dev, link)
2503f58229f8STejun Heo 		if (dev->class == ATA_DEV_UNKNOWN)
2504c6fd2807SJeff Garzik 			cnt++;
2505c6fd2807SJeff Garzik 	return cnt;
2506c6fd2807SJeff Garzik }
2507c6fd2807SJeff Garzik 
25080260731fSTejun Heo static int ata_eh_skip_recovery(struct ata_link *link)
2509c6fd2807SJeff Garzik {
2510672b2d65STejun Heo 	struct ata_port *ap = link->ap;
25110260731fSTejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
2512f58229f8STejun Heo 	struct ata_device *dev;
2513c6fd2807SJeff Garzik 
2514f9df58cbSTejun Heo 	/* skip disabled links */
2515f9df58cbSTejun Heo 	if (link->flags & ATA_LFLAG_DISABLED)
2516f9df58cbSTejun Heo 		return 1;
2517f9df58cbSTejun Heo 
2518672b2d65STejun Heo 	/* thaw frozen port and recover failed devices */
2519672b2d65STejun Heo 	if ((ap->pflags & ATA_PFLAG_FROZEN) || ata_link_nr_enabled(link))
2520672b2d65STejun Heo 		return 0;
2521672b2d65STejun Heo 
2522672b2d65STejun Heo 	/* reset at least once if reset is requested */
2523672b2d65STejun Heo 	if ((ehc->i.action & ATA_EH_RESET) &&
2524672b2d65STejun Heo 	    !(ehc->i.flags & ATA_EHI_DID_RESET))
2525c6fd2807SJeff Garzik 		return 0;
2526c6fd2807SJeff Garzik 
2527c6fd2807SJeff Garzik 	/* skip if class codes for all vacant slots are ATA_DEV_NONE */
25280260731fSTejun Heo 	ata_link_for_each_dev(dev, link) {
2529c6fd2807SJeff Garzik 		if (dev->class == ATA_DEV_UNKNOWN &&
2530c6fd2807SJeff Garzik 		    ehc->classes[dev->devno] != ATA_DEV_NONE)
2531c6fd2807SJeff Garzik 			return 0;
2532c6fd2807SJeff Garzik 	}
2533c6fd2807SJeff Garzik 
2534c6fd2807SJeff Garzik 	return 1;
2535c6fd2807SJeff Garzik }
2536c6fd2807SJeff Garzik 
253702c05a27STejun Heo static int ata_eh_schedule_probe(struct ata_device *dev)
253802c05a27STejun Heo {
253902c05a27STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
254002c05a27STejun Heo 
254102c05a27STejun Heo 	if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
254202c05a27STejun Heo 	    (ehc->did_probe_mask & (1 << dev->devno)))
254302c05a27STejun Heo 		return 0;
254402c05a27STejun Heo 
254502c05a27STejun Heo 	ata_eh_detach_dev(dev);
254602c05a27STejun Heo 	ata_dev_init(dev);
254702c05a27STejun Heo 	ehc->did_probe_mask |= (1 << dev->devno);
2548cf480626STejun Heo 	ehc->i.action |= ATA_EH_RESET;
254900115e0fSTejun Heo 	ehc->saved_xfer_mode[dev->devno] = 0;
255000115e0fSTejun Heo 	ehc->saved_ncq_enabled &= ~(1 << dev->devno);
255102c05a27STejun Heo 
255202c05a27STejun Heo 	return 1;
255302c05a27STejun Heo }
255402c05a27STejun Heo 
25559b1e2658STejun Heo static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
2556fee7ca72STejun Heo {
25579af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
2558fee7ca72STejun Heo 
2559fee7ca72STejun Heo 	ehc->tries[dev->devno]--;
2560fee7ca72STejun Heo 
2561fee7ca72STejun Heo 	switch (err) {
2562fee7ca72STejun Heo 	case -ENODEV:
2563fee7ca72STejun Heo 		/* device missing or wrong IDENTIFY data, schedule probing */
2564fee7ca72STejun Heo 		ehc->i.probe_mask |= (1 << dev->devno);
2565fee7ca72STejun Heo 	case -EINVAL:
2566fee7ca72STejun Heo 		/* give it just one more chance */
2567fee7ca72STejun Heo 		ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
2568fee7ca72STejun Heo 	case -EIO:
25694fb4615bSTejun Heo 		if (ehc->tries[dev->devno] == 1 && dev->pio_mode > XFER_PIO_0) {
2570fee7ca72STejun Heo 			/* This is the last chance, better to slow
2571fee7ca72STejun Heo 			 * down than lose it.
2572fee7ca72STejun Heo 			 */
2573936fd732STejun Heo 			sata_down_spd_limit(dev->link);
2574fee7ca72STejun Heo 			ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2575fee7ca72STejun Heo 		}
2576fee7ca72STejun Heo 	}
2577fee7ca72STejun Heo 
2578fee7ca72STejun Heo 	if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2579fee7ca72STejun Heo 		/* disable device if it has used up all its chances */
2580fee7ca72STejun Heo 		ata_dev_disable(dev);
2581fee7ca72STejun Heo 
2582fee7ca72STejun Heo 		/* detach if offline */
2583936fd732STejun Heo 		if (ata_link_offline(dev->link))
2584fee7ca72STejun Heo 			ata_eh_detach_dev(dev);
2585fee7ca72STejun Heo 
258602c05a27STejun Heo 		/* schedule probe if necessary */
258702c05a27STejun Heo 		if (ata_eh_schedule_probe(dev))
2588fee7ca72STejun Heo 			ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
25899b1e2658STejun Heo 
25909b1e2658STejun Heo 		return 1;
2591fee7ca72STejun Heo 	} else {
2592cf480626STejun Heo 		ehc->i.action |= ATA_EH_RESET;
25939b1e2658STejun Heo 		return 0;
2594fee7ca72STejun Heo 	}
2595fee7ca72STejun Heo }
2596fee7ca72STejun Heo 
2597c6fd2807SJeff Garzik /**
2598c6fd2807SJeff Garzik  *	ata_eh_recover - recover host port after error
2599c6fd2807SJeff Garzik  *	@ap: host port to recover
2600c6fd2807SJeff Garzik  *	@prereset: prereset method (can be NULL)
2601c6fd2807SJeff Garzik  *	@softreset: softreset method (can be NULL)
2602c6fd2807SJeff Garzik  *	@hardreset: hardreset method (can be NULL)
2603c6fd2807SJeff Garzik  *	@postreset: postreset method (can be NULL)
26049b1e2658STejun Heo  *	@r_failed_link: out parameter for failed link
2605c6fd2807SJeff Garzik  *
2606c6fd2807SJeff Garzik  *	This is the alpha and omega, eum and yang, heart and soul of
2607c6fd2807SJeff Garzik  *	libata exception handling.  On entry, actions required to
26089b1e2658STejun Heo  *	recover each link and hotplug requests are recorded in the
26099b1e2658STejun Heo  *	link's eh_context.  This function executes all the operations
26109b1e2658STejun Heo  *	with appropriate retrials and fallbacks to resurrect failed
2611c6fd2807SJeff Garzik  *	devices, detach goners and greet newcomers.
2612c6fd2807SJeff Garzik  *
2613c6fd2807SJeff Garzik  *	LOCKING:
2614c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
2615c6fd2807SJeff Garzik  *
2616c6fd2807SJeff Garzik  *	RETURNS:
2617c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
2618c6fd2807SJeff Garzik  */
2619fb7fd614STejun Heo int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
2620c6fd2807SJeff Garzik 		   ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
26219b1e2658STejun Heo 		   ata_postreset_fn_t postreset,
26229b1e2658STejun Heo 		   struct ata_link **r_failed_link)
2623c6fd2807SJeff Garzik {
26249b1e2658STejun Heo 	struct ata_link *link;
2625c6fd2807SJeff Garzik 	struct ata_device *dev;
26269b1e2658STejun Heo 	int nr_failed_devs, nr_disabled_devs;
2627dc98c32cSTejun Heo 	int rc;
2628f9df58cbSTejun Heo 	unsigned long flags;
2629c6fd2807SJeff Garzik 
2630c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
2631c6fd2807SJeff Garzik 
2632c6fd2807SJeff Garzik 	/* prep for recovery */
26339b1e2658STejun Heo 	ata_port_for_each_link(link, ap) {
26349b1e2658STejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
26359b1e2658STejun Heo 
2636f9df58cbSTejun Heo 		/* re-enable link? */
2637f9df58cbSTejun Heo 		if (ehc->i.action & ATA_EH_ENABLE_LINK) {
2638f9df58cbSTejun Heo 			ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
2639f9df58cbSTejun Heo 			spin_lock_irqsave(ap->lock, flags);
2640f9df58cbSTejun Heo 			link->flags &= ~ATA_LFLAG_DISABLED;
2641f9df58cbSTejun Heo 			spin_unlock_irqrestore(ap->lock, flags);
2642f9df58cbSTejun Heo 			ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
2643f9df58cbSTejun Heo 		}
2644f9df58cbSTejun Heo 
26450260731fSTejun Heo 		ata_link_for_each_dev(dev, link) {
2646fd995f70STejun Heo 			if (link->flags & ATA_LFLAG_NO_RETRY)
2647fd995f70STejun Heo 				ehc->tries[dev->devno] = 1;
2648fd995f70STejun Heo 			else
2649c6fd2807SJeff Garzik 				ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2650c6fd2807SJeff Garzik 
265179a55b72STejun Heo 			/* collect port action mask recorded in dev actions */
26529b1e2658STejun Heo 			ehc->i.action |= ehc->i.dev_action[dev->devno] &
26539b1e2658STejun Heo 					 ~ATA_EH_PERDEV_MASK;
2654f58229f8STejun Heo 			ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
265579a55b72STejun Heo 
2656c6fd2807SJeff Garzik 			/* process hotplug request */
2657c6fd2807SJeff Garzik 			if (dev->flags & ATA_DFLAG_DETACH)
2658c6fd2807SJeff Garzik 				ata_eh_detach_dev(dev);
2659c6fd2807SJeff Garzik 
266002c05a27STejun Heo 			/* schedule probe if necessary */
266102c05a27STejun Heo 			if (!ata_dev_enabled(dev))
266202c05a27STejun Heo 				ata_eh_schedule_probe(dev);
2663c6fd2807SJeff Garzik 		}
26649b1e2658STejun Heo 	}
2665c6fd2807SJeff Garzik 
2666c6fd2807SJeff Garzik  retry:
2667c6fd2807SJeff Garzik 	rc = 0;
26689b1e2658STejun Heo 	nr_failed_devs = 0;
26699b1e2658STejun Heo 	nr_disabled_devs = 0;
2670c6fd2807SJeff Garzik 
2671c6fd2807SJeff Garzik 	/* if UNLOADING, finish immediately */
2672c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_UNLOADING)
2673c6fd2807SJeff Garzik 		goto out;
2674c6fd2807SJeff Garzik 
26759b1e2658STejun Heo 	/* prep for EH */
26769b1e2658STejun Heo 	ata_port_for_each_link(link, ap) {
26779b1e2658STejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
26789b1e2658STejun Heo 
2679c6fd2807SJeff Garzik 		/* skip EH if possible. */
26800260731fSTejun Heo 		if (ata_eh_skip_recovery(link))
2681c6fd2807SJeff Garzik 			ehc->i.action = 0;
2682c6fd2807SJeff Garzik 
26830260731fSTejun Heo 		ata_link_for_each_dev(dev, link)
2684f58229f8STejun Heo 			ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
26859b1e2658STejun Heo 	}
2686c6fd2807SJeff Garzik 
2687c6fd2807SJeff Garzik 	/* reset */
26889b1e2658STejun Heo 	ata_port_for_each_link(link, ap) {
26899b1e2658STejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
26909b1e2658STejun Heo 
2691cf480626STejun Heo 		if (!(ehc->i.action & ATA_EH_RESET))
26929b1e2658STejun Heo 			continue;
26939b1e2658STejun Heo 
26949b1e2658STejun Heo 		rc = ata_eh_reset(link, ata_link_nr_vacant(link),
2695dc98c32cSTejun Heo 				  prereset, softreset, hardreset, postreset);
2696c6fd2807SJeff Garzik 		if (rc) {
26970260731fSTejun Heo 			ata_link_printk(link, KERN_ERR,
2698c6fd2807SJeff Garzik 					"reset failed, giving up\n");
2699c6fd2807SJeff Garzik 			goto out;
2700c6fd2807SJeff Garzik 		}
27019b1e2658STejun Heo 	}
2702c6fd2807SJeff Garzik 
27039b1e2658STejun Heo 	/* the rest */
27049b1e2658STejun Heo 	ata_port_for_each_link(link, ap) {
27059b1e2658STejun Heo 		struct ata_eh_context *ehc = &link->eh_context;
27069b1e2658STejun Heo 
2707c6fd2807SJeff Garzik 		/* revalidate existing devices and attach new ones */
27080260731fSTejun Heo 		rc = ata_eh_revalidate_and_attach(link, &dev);
2709c6fd2807SJeff Garzik 		if (rc)
2710c6fd2807SJeff Garzik 			goto dev_fail;
2711c6fd2807SJeff Garzik 
2712633273a3STejun Heo 		/* if PMP got attached, return, pmp EH will take care of it */
2713633273a3STejun Heo 		if (link->device->class == ATA_DEV_PMP) {
2714633273a3STejun Heo 			ehc->i.action = 0;
2715633273a3STejun Heo 			return 0;
2716633273a3STejun Heo 		}
2717633273a3STejun Heo 
2718baa1e78aSTejun Heo 		/* configure transfer mode if necessary */
2719baa1e78aSTejun Heo 		if (ehc->i.flags & ATA_EHI_SETMODE) {
27200260731fSTejun Heo 			rc = ata_set_mode(link, &dev);
27214ae72a1eSTejun Heo 			if (rc)
2722c6fd2807SJeff Garzik 				goto dev_fail;
2723baa1e78aSTejun Heo 			ehc->i.flags &= ~ATA_EHI_SETMODE;
2724c6fd2807SJeff Garzik 		}
2725c6fd2807SJeff Garzik 
27263ec25ebdSTejun Heo 		if (ehc->i.action & ATA_EH_LPM)
2727ca77329fSKristen Carlson Accardi 			ata_link_for_each_dev(dev, link)
2728ca77329fSKristen Carlson Accardi 				ata_dev_enable_pm(dev, ap->pm_policy);
2729ca77329fSKristen Carlson Accardi 
27309b1e2658STejun Heo 		/* this link is okay now */
27319b1e2658STejun Heo 		ehc->i.flags = 0;
27329b1e2658STejun Heo 		continue;
2733c6fd2807SJeff Garzik 
2734c6fd2807SJeff Garzik dev_fail:
27359b1e2658STejun Heo 		nr_failed_devs++;
27369b1e2658STejun Heo 		if (ata_eh_handle_dev_fail(dev, rc))
27379b1e2658STejun Heo 			nr_disabled_devs++;
2738c6fd2807SJeff Garzik 
2739b06ce3e5STejun Heo 		if (ap->pflags & ATA_PFLAG_FROZEN) {
2740b06ce3e5STejun Heo 			/* PMP reset requires working host port.
2741b06ce3e5STejun Heo 			 * Can't retry if it's frozen.
2742b06ce3e5STejun Heo 			 */
2743071f44b1STejun Heo 			if (sata_pmp_attached(ap))
2744b06ce3e5STejun Heo 				goto out;
27459b1e2658STejun Heo 			break;
27469b1e2658STejun Heo 		}
2747b06ce3e5STejun Heo 	}
27489b1e2658STejun Heo 
27499b1e2658STejun Heo 	if (nr_failed_devs) {
27509b1e2658STejun Heo 		if (nr_failed_devs != nr_disabled_devs) {
27519b1e2658STejun Heo 			ata_port_printk(ap, KERN_WARNING, "failed to recover "
27529b1e2658STejun Heo 					"some devices, retrying in 5 secs\n");
2753c6fd2807SJeff Garzik 			ssleep(5);
2754c6fd2807SJeff Garzik 		} else {
27559b1e2658STejun Heo 			/* no device left to recover, repeat fast */
2756c6fd2807SJeff Garzik 			msleep(500);
2757c6fd2807SJeff Garzik 		}
2758c6fd2807SJeff Garzik 
2759c6fd2807SJeff Garzik 		goto retry;
27609b1e2658STejun Heo 	}
2761c6fd2807SJeff Garzik 
2762c6fd2807SJeff Garzik  out:
27639b1e2658STejun Heo 	if (rc && r_failed_link)
27649b1e2658STejun Heo 		*r_failed_link = link;
2765c6fd2807SJeff Garzik 
2766c6fd2807SJeff Garzik 	DPRINTK("EXIT, rc=%d\n", rc);
2767c6fd2807SJeff Garzik 	return rc;
2768c6fd2807SJeff Garzik }
2769c6fd2807SJeff Garzik 
2770c6fd2807SJeff Garzik /**
2771c6fd2807SJeff Garzik  *	ata_eh_finish - finish up EH
2772c6fd2807SJeff Garzik  *	@ap: host port to finish EH for
2773c6fd2807SJeff Garzik  *
2774c6fd2807SJeff Garzik  *	Recovery is complete.  Clean up EH states and retry or finish
2775c6fd2807SJeff Garzik  *	failed qcs.
2776c6fd2807SJeff Garzik  *
2777c6fd2807SJeff Garzik  *	LOCKING:
2778c6fd2807SJeff Garzik  *	None.
2779c6fd2807SJeff Garzik  */
2780fb7fd614STejun Heo void ata_eh_finish(struct ata_port *ap)
2781c6fd2807SJeff Garzik {
2782c6fd2807SJeff Garzik 	int tag;
2783c6fd2807SJeff Garzik 
2784c6fd2807SJeff Garzik 	/* retry or finish qcs */
2785c6fd2807SJeff Garzik 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2786c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2787c6fd2807SJeff Garzik 
2788c6fd2807SJeff Garzik 		if (!(qc->flags & ATA_QCFLAG_FAILED))
2789c6fd2807SJeff Garzik 			continue;
2790c6fd2807SJeff Garzik 
2791c6fd2807SJeff Garzik 		if (qc->err_mask) {
2792c6fd2807SJeff Garzik 			/* FIXME: Once EH migration is complete,
2793c6fd2807SJeff Garzik 			 * generate sense data in this function,
2794c6fd2807SJeff Garzik 			 * considering both err_mask and tf.
2795c6fd2807SJeff Garzik 			 */
279603faab78STejun Heo 			if (qc->flags & ATA_QCFLAG_RETRY)
2797c6fd2807SJeff Garzik 				ata_eh_qc_retry(qc);
279803faab78STejun Heo 			else
279903faab78STejun Heo 				ata_eh_qc_complete(qc);
2800c6fd2807SJeff Garzik 		} else {
2801c6fd2807SJeff Garzik 			if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2802c6fd2807SJeff Garzik 				ata_eh_qc_complete(qc);
2803c6fd2807SJeff Garzik 			} else {
2804c6fd2807SJeff Garzik 				/* feed zero TF to sense generation */
2805c6fd2807SJeff Garzik 				memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2806c6fd2807SJeff Garzik 				ata_eh_qc_retry(qc);
2807c6fd2807SJeff Garzik 			}
2808c6fd2807SJeff Garzik 		}
2809c6fd2807SJeff Garzik 	}
2810da917d69STejun Heo 
2811da917d69STejun Heo 	/* make sure nr_active_links is zero after EH */
2812da917d69STejun Heo 	WARN_ON(ap->nr_active_links);
2813da917d69STejun Heo 	ap->nr_active_links = 0;
2814c6fd2807SJeff Garzik }
2815c6fd2807SJeff Garzik 
2816c6fd2807SJeff Garzik /**
2817c6fd2807SJeff Garzik  *	ata_do_eh - do standard error handling
2818c6fd2807SJeff Garzik  *	@ap: host port to handle error for
2819a1efdabaSTejun Heo  *
2820c6fd2807SJeff Garzik  *	@prereset: prereset method (can be NULL)
2821c6fd2807SJeff Garzik  *	@softreset: softreset method (can be NULL)
2822c6fd2807SJeff Garzik  *	@hardreset: hardreset method (can be NULL)
2823c6fd2807SJeff Garzik  *	@postreset: postreset method (can be NULL)
2824c6fd2807SJeff Garzik  *
2825c6fd2807SJeff Garzik  *	Perform standard error handling sequence.
2826c6fd2807SJeff Garzik  *
2827c6fd2807SJeff Garzik  *	LOCKING:
2828c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
2829c6fd2807SJeff Garzik  */
2830c6fd2807SJeff Garzik void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2831c6fd2807SJeff Garzik 	       ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2832c6fd2807SJeff Garzik 	       ata_postreset_fn_t postreset)
2833c6fd2807SJeff Garzik {
28349b1e2658STejun Heo 	struct ata_device *dev;
28359b1e2658STejun Heo 	int rc;
28369b1e2658STejun Heo 
28379b1e2658STejun Heo 	ata_eh_autopsy(ap);
28389b1e2658STejun Heo 	ata_eh_report(ap);
28399b1e2658STejun Heo 
28409b1e2658STejun Heo 	rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
28419b1e2658STejun Heo 			    NULL);
28429b1e2658STejun Heo 	if (rc) {
28439b1e2658STejun Heo 		ata_link_for_each_dev(dev, &ap->link)
28449b1e2658STejun Heo 			ata_dev_disable(dev);
28459b1e2658STejun Heo 	}
28469b1e2658STejun Heo 
2847c6fd2807SJeff Garzik 	ata_eh_finish(ap);
2848c6fd2807SJeff Garzik }
2849c6fd2807SJeff Garzik 
2850a1efdabaSTejun Heo /**
2851a1efdabaSTejun Heo  *	ata_std_error_handler - standard error handler
2852a1efdabaSTejun Heo  *	@ap: host port to handle error for
2853a1efdabaSTejun Heo  *
2854a1efdabaSTejun Heo  *	Standard error handler
2855a1efdabaSTejun Heo  *
2856a1efdabaSTejun Heo  *	LOCKING:
2857a1efdabaSTejun Heo  *	Kernel thread context (may sleep).
2858a1efdabaSTejun Heo  */
2859a1efdabaSTejun Heo void ata_std_error_handler(struct ata_port *ap)
2860a1efdabaSTejun Heo {
2861a1efdabaSTejun Heo 	struct ata_port_operations *ops = ap->ops;
2862a1efdabaSTejun Heo 	ata_reset_fn_t hardreset = ops->hardreset;
2863a1efdabaSTejun Heo 
286457c9efdfSTejun Heo 	/* ignore built-in hardreset if SCR access is not available */
286557c9efdfSTejun Heo 	if (ata_is_builtin_hardreset(hardreset) && !sata_scr_valid(&ap->link))
2866a1efdabaSTejun Heo 		hardreset = NULL;
2867a1efdabaSTejun Heo 
2868a1efdabaSTejun Heo 	ata_do_eh(ap, ops->prereset, ops->softreset, hardreset, ops->postreset);
2869a1efdabaSTejun Heo }
2870a1efdabaSTejun Heo 
28716ffa01d8STejun Heo #ifdef CONFIG_PM
2872c6fd2807SJeff Garzik /**
2873c6fd2807SJeff Garzik  *	ata_eh_handle_port_suspend - perform port suspend operation
2874c6fd2807SJeff Garzik  *	@ap: port to suspend
2875c6fd2807SJeff Garzik  *
2876c6fd2807SJeff Garzik  *	Suspend @ap.
2877c6fd2807SJeff Garzik  *
2878c6fd2807SJeff Garzik  *	LOCKING:
2879c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
2880c6fd2807SJeff Garzik  */
2881c6fd2807SJeff Garzik static void ata_eh_handle_port_suspend(struct ata_port *ap)
2882c6fd2807SJeff Garzik {
2883c6fd2807SJeff Garzik 	unsigned long flags;
2884c6fd2807SJeff Garzik 	int rc = 0;
2885c6fd2807SJeff Garzik 
2886c6fd2807SJeff Garzik 	/* are we suspending? */
2887c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
2888c6fd2807SJeff Garzik 	if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2889c6fd2807SJeff Garzik 	    ap->pm_mesg.event == PM_EVENT_ON) {
2890c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
2891c6fd2807SJeff Garzik 		return;
2892c6fd2807SJeff Garzik 	}
2893c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
2894c6fd2807SJeff Garzik 
2895c6fd2807SJeff Garzik 	WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
2896c6fd2807SJeff Garzik 
289764578a3dSTejun Heo 	/* tell ACPI we're suspending */
289864578a3dSTejun Heo 	rc = ata_acpi_on_suspend(ap);
289964578a3dSTejun Heo 	if (rc)
290064578a3dSTejun Heo 		goto out;
290164578a3dSTejun Heo 
2902c6fd2807SJeff Garzik 	/* suspend */
2903c6fd2807SJeff Garzik 	ata_eh_freeze_port(ap);
2904c6fd2807SJeff Garzik 
2905c6fd2807SJeff Garzik 	if (ap->ops->port_suspend)
2906c6fd2807SJeff Garzik 		rc = ap->ops->port_suspend(ap, ap->pm_mesg);
2907c6fd2807SJeff Garzik 
2908bd3adca5SShaohua Li 	ata_acpi_set_state(ap, PMSG_SUSPEND);
290964578a3dSTejun Heo  out:
2910c6fd2807SJeff Garzik 	/* report result */
2911c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
2912c6fd2807SJeff Garzik 
2913c6fd2807SJeff Garzik 	ap->pflags &= ~ATA_PFLAG_PM_PENDING;
2914c6fd2807SJeff Garzik 	if (rc == 0)
2915c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_SUSPENDED;
291664578a3dSTejun Heo 	else if (ap->pflags & ATA_PFLAG_FROZEN)
2917c6fd2807SJeff Garzik 		ata_port_schedule_eh(ap);
2918c6fd2807SJeff Garzik 
2919c6fd2807SJeff Garzik 	if (ap->pm_result) {
2920c6fd2807SJeff Garzik 		*ap->pm_result = rc;
2921c6fd2807SJeff Garzik 		ap->pm_result = NULL;
2922c6fd2807SJeff Garzik 	}
2923c6fd2807SJeff Garzik 
2924c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
2925c6fd2807SJeff Garzik 
2926c6fd2807SJeff Garzik 	return;
2927c6fd2807SJeff Garzik }
2928c6fd2807SJeff Garzik 
2929c6fd2807SJeff Garzik /**
2930c6fd2807SJeff Garzik  *	ata_eh_handle_port_resume - perform port resume operation
2931c6fd2807SJeff Garzik  *	@ap: port to resume
2932c6fd2807SJeff Garzik  *
2933c6fd2807SJeff Garzik  *	Resume @ap.
2934c6fd2807SJeff Garzik  *
2935c6fd2807SJeff Garzik  *	LOCKING:
2936c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
2937c6fd2807SJeff Garzik  */
2938c6fd2807SJeff Garzik static void ata_eh_handle_port_resume(struct ata_port *ap)
2939c6fd2807SJeff Garzik {
2940c6fd2807SJeff Garzik 	unsigned long flags;
29419666f400STejun Heo 	int rc = 0;
2942c6fd2807SJeff Garzik 
2943c6fd2807SJeff Garzik 	/* are we resuming? */
2944c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
2945c6fd2807SJeff Garzik 	if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2946c6fd2807SJeff Garzik 	    ap->pm_mesg.event != PM_EVENT_ON) {
2947c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
2948c6fd2807SJeff Garzik 		return;
2949c6fd2807SJeff Garzik 	}
2950c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
2951c6fd2807SJeff Garzik 
29529666f400STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
2953c6fd2807SJeff Garzik 
2954bd3adca5SShaohua Li 	ata_acpi_set_state(ap, PMSG_ON);
2955bd3adca5SShaohua Li 
2956c6fd2807SJeff Garzik 	if (ap->ops->port_resume)
2957c6fd2807SJeff Garzik 		rc = ap->ops->port_resume(ap);
2958c6fd2807SJeff Garzik 
29596746544cSTejun Heo 	/* tell ACPI that we're resuming */
29606746544cSTejun Heo 	ata_acpi_on_resume(ap);
29616746544cSTejun Heo 
29629666f400STejun Heo 	/* report result */
2963c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
2964c6fd2807SJeff Garzik 	ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
2965c6fd2807SJeff Garzik 	if (ap->pm_result) {
2966c6fd2807SJeff Garzik 		*ap->pm_result = rc;
2967c6fd2807SJeff Garzik 		ap->pm_result = NULL;
2968c6fd2807SJeff Garzik 	}
2969c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
2970c6fd2807SJeff Garzik }
29716ffa01d8STejun Heo #endif /* CONFIG_PM */
2972