xref: /openbmc/linux/drivers/ata/libata-eh.c (revision 22246614)
1 /*
2  *  libata-eh.c - libata error handling
3  *
4  *  Maintained by:  Jeff Garzik <jgarzik@pobox.com>
5  *    		    Please ALWAYS copy linux-ide@vger.kernel.org
6  *		    on emails.
7  *
8  *  Copyright 2006 Tejun Heo <htejun@gmail.com>
9  *
10  *
11  *  This program is free software; you can redistribute it and/or
12  *  modify it under the terms of the GNU General Public License as
13  *  published by the Free Software Foundation; either version 2, or
14  *  (at your option) any later version.
15  *
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  *  General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; see the file COPYING.  If not, write to
23  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
24  *  USA.
25  *
26  *
27  *  libata documentation is available via 'make {ps|pdf}docs',
28  *  as Documentation/DocBook/libata.*
29  *
30  *  Hardware documentation available from http://www.t13.org/ and
31  *  http://www.sata-io.org/
32  *
33  */
34 
35 #include <linux/kernel.h>
36 #include <linux/pci.h>
37 #include <scsi/scsi.h>
38 #include <scsi/scsi_host.h>
39 #include <scsi/scsi_eh.h>
40 #include <scsi/scsi_device.h>
41 #include <scsi/scsi_cmnd.h>
42 #include "../scsi/scsi_transport_api.h"
43 
44 #include <linux/libata.h>
45 
46 #include "libata.h"
47 
48 enum {
49 	/* speed down verdicts */
50 	ATA_EH_SPDN_NCQ_OFF		= (1 << 0),
51 	ATA_EH_SPDN_SPEED_DOWN		= (1 << 1),
52 	ATA_EH_SPDN_FALLBACK_TO_PIO	= (1 << 2),
53 	ATA_EH_SPDN_KEEP_ERRORS		= (1 << 3),
54 
55 	/* error flags */
56 	ATA_EFLAG_IS_IO			= (1 << 0),
57 	ATA_EFLAG_DUBIOUS_XFER		= (1 << 1),
58 
59 	/* error categories */
60 	ATA_ECAT_NONE			= 0,
61 	ATA_ECAT_ATA_BUS		= 1,
62 	ATA_ECAT_TOUT_HSM		= 2,
63 	ATA_ECAT_UNK_DEV		= 3,
64 	ATA_ECAT_DUBIOUS_NONE		= 4,
65 	ATA_ECAT_DUBIOUS_ATA_BUS	= 5,
66 	ATA_ECAT_DUBIOUS_TOUT_HSM	= 6,
67 	ATA_ECAT_DUBIOUS_UNK_DEV	= 7,
68 	ATA_ECAT_NR			= 8,
69 };
70 
71 /* Waiting in ->prereset can never be reliable.  It's sometimes nice
72  * to wait there but it can't be depended upon; otherwise, we wouldn't
73  * be resetting.  Just give it enough time for most drives to spin up.
74  */
75 enum {
76 	ATA_EH_PRERESET_TIMEOUT		= 10 * HZ,
77 	ATA_EH_FASTDRAIN_INTERVAL	= 3 * HZ,
78 };
79 
80 /* The following table determines how we sequence resets.  Each entry
81  * represents timeout for that try.  The first try can be soft or
82  * hardreset.  All others are hardreset if available.  In most cases
83  * the first reset w/ 10sec timeout should succeed.  Following entries
84  * are mostly for error handling, hotplug and retarded devices.
85  */
86 static const unsigned long ata_eh_reset_timeouts[] = {
87 	10 * HZ,	/* most drives spin up by 10sec */
88 	10 * HZ,	/* > 99% working drives spin up before 20sec */
89 	35 * HZ,	/* give > 30 secs of idleness for retarded devices */
90 	5 * HZ,		/* and sweet one last chance */
91 	/* > 1 min has elapsed, give up */
92 };
93 
94 static void __ata_port_freeze(struct ata_port *ap);
95 #ifdef CONFIG_PM
96 static void ata_eh_handle_port_suspend(struct ata_port *ap);
97 static void ata_eh_handle_port_resume(struct ata_port *ap);
98 #else /* CONFIG_PM */
99 static void ata_eh_handle_port_suspend(struct ata_port *ap)
100 { }
101 
102 static void ata_eh_handle_port_resume(struct ata_port *ap)
103 { }
104 #endif /* CONFIG_PM */
105 
106 static void __ata_ehi_pushv_desc(struct ata_eh_info *ehi, const char *fmt,
107 				 va_list args)
108 {
109 	ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
110 				     ATA_EH_DESC_LEN - ehi->desc_len,
111 				     fmt, args);
112 }
113 
114 /**
115  *	__ata_ehi_push_desc - push error description without adding separator
116  *	@ehi: target EHI
117  *	@fmt: printf format string
118  *
119  *	Format string according to @fmt and append it to @ehi->desc.
120  *
121  *	LOCKING:
122  *	spin_lock_irqsave(host lock)
123  */
124 void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
125 {
126 	va_list args;
127 
128 	va_start(args, fmt);
129 	__ata_ehi_pushv_desc(ehi, fmt, args);
130 	va_end(args);
131 }
132 
133 /**
134  *	ata_ehi_push_desc - push error description with separator
135  *	@ehi: target EHI
136  *	@fmt: printf format string
137  *
138  *	Format string according to @fmt and append it to @ehi->desc.
139  *	If @ehi->desc is not empty, ", " is added in-between.
140  *
141  *	LOCKING:
142  *	spin_lock_irqsave(host lock)
143  */
144 void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
145 {
146 	va_list args;
147 
148 	if (ehi->desc_len)
149 		__ata_ehi_push_desc(ehi, ", ");
150 
151 	va_start(args, fmt);
152 	__ata_ehi_pushv_desc(ehi, fmt, args);
153 	va_end(args);
154 }
155 
156 /**
157  *	ata_ehi_clear_desc - clean error description
158  *	@ehi: target EHI
159  *
160  *	Clear @ehi->desc.
161  *
162  *	LOCKING:
163  *	spin_lock_irqsave(host lock)
164  */
165 void ata_ehi_clear_desc(struct ata_eh_info *ehi)
166 {
167 	ehi->desc[0] = '\0';
168 	ehi->desc_len = 0;
169 }
170 
171 /**
172  *	ata_port_desc - append port description
173  *	@ap: target ATA port
174  *	@fmt: printf format string
175  *
176  *	Format string according to @fmt and append it to port
177  *	description.  If port description is not empty, " " is added
178  *	in-between.  This function is to be used while initializing
179  *	ata_host.  The description is printed on host registration.
180  *
181  *	LOCKING:
182  *	None.
183  */
184 void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
185 {
186 	va_list args;
187 
188 	WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
189 
190 	if (ap->link.eh_info.desc_len)
191 		__ata_ehi_push_desc(&ap->link.eh_info, " ");
192 
193 	va_start(args, fmt);
194 	__ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
195 	va_end(args);
196 }
197 
198 #ifdef CONFIG_PCI
199 
200 /**
201  *	ata_port_pbar_desc - append PCI BAR description
202  *	@ap: target ATA port
203  *	@bar: target PCI BAR
204  *	@offset: offset into PCI BAR
205  *	@name: name of the area
206  *
207  *	If @offset is negative, this function formats a string which
208  *	contains the name, address, size and type of the BAR and
209  *	appends it to the port description.  If @offset is zero or
210  *	positive, only name and offsetted address is appended.
211  *
212  *	LOCKING:
213  *	None.
214  */
215 void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
216 			const char *name)
217 {
218 	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
219 	char *type = "";
220 	unsigned long long start, len;
221 
222 	if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
223 		type = "m";
224 	else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
225 		type = "i";
226 
227 	start = (unsigned long long)pci_resource_start(pdev, bar);
228 	len = (unsigned long long)pci_resource_len(pdev, bar);
229 
230 	if (offset < 0)
231 		ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
232 	else
233 		ata_port_desc(ap, "%s 0x%llx", name,
234 				start + (unsigned long long)offset);
235 }
236 
237 #endif /* CONFIG_PCI */
238 
239 static void ata_ering_record(struct ata_ering *ering, unsigned int eflags,
240 			     unsigned int err_mask)
241 {
242 	struct ata_ering_entry *ent;
243 
244 	WARN_ON(!err_mask);
245 
246 	ering->cursor++;
247 	ering->cursor %= ATA_ERING_SIZE;
248 
249 	ent = &ering->ring[ering->cursor];
250 	ent->eflags = eflags;
251 	ent->err_mask = err_mask;
252 	ent->timestamp = get_jiffies_64();
253 }
254 
255 static struct ata_ering_entry *ata_ering_top(struct ata_ering *ering)
256 {
257 	struct ata_ering_entry *ent = &ering->ring[ering->cursor];
258 
259 	if (ent->err_mask)
260 		return ent;
261 	return NULL;
262 }
263 
264 static void ata_ering_clear(struct ata_ering *ering)
265 {
266 	memset(ering, 0, sizeof(*ering));
267 }
268 
269 static int ata_ering_map(struct ata_ering *ering,
270 			 int (*map_fn)(struct ata_ering_entry *, void *),
271 			 void *arg)
272 {
273 	int idx, rc = 0;
274 	struct ata_ering_entry *ent;
275 
276 	idx = ering->cursor;
277 	do {
278 		ent = &ering->ring[idx];
279 		if (!ent->err_mask)
280 			break;
281 		rc = map_fn(ent, arg);
282 		if (rc)
283 			break;
284 		idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
285 	} while (idx != ering->cursor);
286 
287 	return rc;
288 }
289 
290 static unsigned int ata_eh_dev_action(struct ata_device *dev)
291 {
292 	struct ata_eh_context *ehc = &dev->link->eh_context;
293 
294 	return ehc->i.action | ehc->i.dev_action[dev->devno];
295 }
296 
297 static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
298 				struct ata_eh_info *ehi, unsigned int action)
299 {
300 	struct ata_device *tdev;
301 
302 	if (!dev) {
303 		ehi->action &= ~action;
304 		ata_link_for_each_dev(tdev, link)
305 			ehi->dev_action[tdev->devno] &= ~action;
306 	} else {
307 		/* doesn't make sense for port-wide EH actions */
308 		WARN_ON(!(action & ATA_EH_PERDEV_MASK));
309 
310 		/* break ehi->action into ehi->dev_action */
311 		if (ehi->action & action) {
312 			ata_link_for_each_dev(tdev, link)
313 				ehi->dev_action[tdev->devno] |=
314 					ehi->action & action;
315 			ehi->action &= ~action;
316 		}
317 
318 		/* turn off the specified per-dev action */
319 		ehi->dev_action[dev->devno] &= ~action;
320 	}
321 }
322 
323 /**
324  *	ata_scsi_timed_out - SCSI layer time out callback
325  *	@cmd: timed out SCSI command
326  *
327  *	Handles SCSI layer timeout.  We race with normal completion of
328  *	the qc for @cmd.  If the qc is already gone, we lose and let
329  *	the scsi command finish (EH_HANDLED).  Otherwise, the qc has
330  *	timed out and EH should be invoked.  Prevent ata_qc_complete()
331  *	from finishing it by setting EH_SCHEDULED and return
332  *	EH_NOT_HANDLED.
333  *
334  *	TODO: kill this function once old EH is gone.
335  *
336  *	LOCKING:
337  *	Called from timer context
338  *
339  *	RETURNS:
340  *	EH_HANDLED or EH_NOT_HANDLED
341  */
342 enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
343 {
344 	struct Scsi_Host *host = cmd->device->host;
345 	struct ata_port *ap = ata_shost_to_port(host);
346 	unsigned long flags;
347 	struct ata_queued_cmd *qc;
348 	enum scsi_eh_timer_return ret;
349 
350 	DPRINTK("ENTER\n");
351 
352 	if (ap->ops->error_handler) {
353 		ret = EH_NOT_HANDLED;
354 		goto out;
355 	}
356 
357 	ret = EH_HANDLED;
358 	spin_lock_irqsave(ap->lock, flags);
359 	qc = ata_qc_from_tag(ap, ap->link.active_tag);
360 	if (qc) {
361 		WARN_ON(qc->scsicmd != cmd);
362 		qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
363 		qc->err_mask |= AC_ERR_TIMEOUT;
364 		ret = EH_NOT_HANDLED;
365 	}
366 	spin_unlock_irqrestore(ap->lock, flags);
367 
368  out:
369 	DPRINTK("EXIT, ret=%d\n", ret);
370 	return ret;
371 }
372 
373 /**
374  *	ata_scsi_error - SCSI layer error handler callback
375  *	@host: SCSI host on which error occurred
376  *
377  *	Handles SCSI-layer-thrown error events.
378  *
379  *	LOCKING:
380  *	Inherited from SCSI layer (none, can sleep)
381  *
382  *	RETURNS:
383  *	Zero.
384  */
385 void ata_scsi_error(struct Scsi_Host *host)
386 {
387 	struct ata_port *ap = ata_shost_to_port(host);
388 	int i;
389 	unsigned long flags;
390 
391 	DPRINTK("ENTER\n");
392 
393 	/* synchronize with port task */
394 	ata_port_flush_task(ap);
395 
396 	/* synchronize with host lock and sort out timeouts */
397 
398 	/* For new EH, all qcs are finished in one of three ways -
399 	 * normal completion, error completion, and SCSI timeout.
400 	 * Both cmpletions can race against SCSI timeout.  When normal
401 	 * completion wins, the qc never reaches EH.  When error
402 	 * completion wins, the qc has ATA_QCFLAG_FAILED set.
403 	 *
404 	 * When SCSI timeout wins, things are a bit more complex.
405 	 * Normal or error completion can occur after the timeout but
406 	 * before this point.  In such cases, both types of
407 	 * completions are honored.  A scmd is determined to have
408 	 * timed out iff its associated qc is active and not failed.
409 	 */
410 	if (ap->ops->error_handler) {
411 		struct scsi_cmnd *scmd, *tmp;
412 		int nr_timedout = 0;
413 
414 		spin_lock_irqsave(ap->lock, flags);
415 
416 		list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
417 			struct ata_queued_cmd *qc;
418 
419 			for (i = 0; i < ATA_MAX_QUEUE; i++) {
420 				qc = __ata_qc_from_tag(ap, i);
421 				if (qc->flags & ATA_QCFLAG_ACTIVE &&
422 				    qc->scsicmd == scmd)
423 					break;
424 			}
425 
426 			if (i < ATA_MAX_QUEUE) {
427 				/* the scmd has an associated qc */
428 				if (!(qc->flags & ATA_QCFLAG_FAILED)) {
429 					/* which hasn't failed yet, timeout */
430 					qc->err_mask |= AC_ERR_TIMEOUT;
431 					qc->flags |= ATA_QCFLAG_FAILED;
432 					nr_timedout++;
433 				}
434 			} else {
435 				/* Normal completion occurred after
436 				 * SCSI timeout but before this point.
437 				 * Successfully complete it.
438 				 */
439 				scmd->retries = scmd->allowed;
440 				scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
441 			}
442 		}
443 
444 		/* If we have timed out qcs.  They belong to EH from
445 		 * this point but the state of the controller is
446 		 * unknown.  Freeze the port to make sure the IRQ
447 		 * handler doesn't diddle with those qcs.  This must
448 		 * be done atomically w.r.t. setting QCFLAG_FAILED.
449 		 */
450 		if (nr_timedout)
451 			__ata_port_freeze(ap);
452 
453 		spin_unlock_irqrestore(ap->lock, flags);
454 
455 		/* initialize eh_tries */
456 		ap->eh_tries = ATA_EH_MAX_TRIES;
457 	} else
458 		spin_unlock_wait(ap->lock);
459 
460  repeat:
461 	/* invoke error handler */
462 	if (ap->ops->error_handler) {
463 		struct ata_link *link;
464 
465 		/* kill fast drain timer */
466 		del_timer_sync(&ap->fastdrain_timer);
467 
468 		/* process port resume request */
469 		ata_eh_handle_port_resume(ap);
470 
471 		/* fetch & clear EH info */
472 		spin_lock_irqsave(ap->lock, flags);
473 
474 		__ata_port_for_each_link(link, ap) {
475 			struct ata_eh_context *ehc = &link->eh_context;
476 			struct ata_device *dev;
477 
478 			memset(&link->eh_context, 0, sizeof(link->eh_context));
479 			link->eh_context.i = link->eh_info;
480 			memset(&link->eh_info, 0, sizeof(link->eh_info));
481 
482 			ata_link_for_each_dev(dev, link) {
483 				int devno = dev->devno;
484 
485 				ehc->saved_xfer_mode[devno] = dev->xfer_mode;
486 				if (ata_ncq_enabled(dev))
487 					ehc->saved_ncq_enabled |= 1 << devno;
488 			}
489 		}
490 
491 		ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
492 		ap->pflags &= ~ATA_PFLAG_EH_PENDING;
493 		ap->excl_link = NULL;	/* don't maintain exclusion over EH */
494 
495 		spin_unlock_irqrestore(ap->lock, flags);
496 
497 		/* invoke EH, skip if unloading or suspended */
498 		if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
499 			ap->ops->error_handler(ap);
500 		else
501 			ata_eh_finish(ap);
502 
503 		/* process port suspend request */
504 		ata_eh_handle_port_suspend(ap);
505 
506 		/* Exception might have happend after ->error_handler
507 		 * recovered the port but before this point.  Repeat
508 		 * EH in such case.
509 		 */
510 		spin_lock_irqsave(ap->lock, flags);
511 
512 		if (ap->pflags & ATA_PFLAG_EH_PENDING) {
513 			if (--ap->eh_tries) {
514 				spin_unlock_irqrestore(ap->lock, flags);
515 				goto repeat;
516 			}
517 			ata_port_printk(ap, KERN_ERR, "EH pending after %d "
518 					"tries, giving up\n", ATA_EH_MAX_TRIES);
519 			ap->pflags &= ~ATA_PFLAG_EH_PENDING;
520 		}
521 
522 		/* this run is complete, make sure EH info is clear */
523 		__ata_port_for_each_link(link, ap)
524 			memset(&link->eh_info, 0, sizeof(link->eh_info));
525 
526 		/* Clear host_eh_scheduled while holding ap->lock such
527 		 * that if exception occurs after this point but
528 		 * before EH completion, SCSI midlayer will
529 		 * re-initiate EH.
530 		 */
531 		host->host_eh_scheduled = 0;
532 
533 		spin_unlock_irqrestore(ap->lock, flags);
534 	} else {
535 		WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
536 		ap->ops->eng_timeout(ap);
537 	}
538 
539 	/* finish or retry handled scmd's and clean up */
540 	WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
541 
542 	scsi_eh_flush_done_q(&ap->eh_done_q);
543 
544 	/* clean up */
545 	spin_lock_irqsave(ap->lock, flags);
546 
547 	if (ap->pflags & ATA_PFLAG_LOADING)
548 		ap->pflags &= ~ATA_PFLAG_LOADING;
549 	else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
550 		queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0);
551 
552 	if (ap->pflags & ATA_PFLAG_RECOVERED)
553 		ata_port_printk(ap, KERN_INFO, "EH complete\n");
554 
555 	ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
556 
557 	/* tell wait_eh that we're done */
558 	ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
559 	wake_up_all(&ap->eh_wait_q);
560 
561 	spin_unlock_irqrestore(ap->lock, flags);
562 
563 	DPRINTK("EXIT\n");
564 }
565 
566 /**
567  *	ata_port_wait_eh - Wait for the currently pending EH to complete
568  *	@ap: Port to wait EH for
569  *
570  *	Wait until the currently pending EH is complete.
571  *
572  *	LOCKING:
573  *	Kernel thread context (may sleep).
574  */
575 void ata_port_wait_eh(struct ata_port *ap)
576 {
577 	unsigned long flags;
578 	DEFINE_WAIT(wait);
579 
580  retry:
581 	spin_lock_irqsave(ap->lock, flags);
582 
583 	while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
584 		prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
585 		spin_unlock_irqrestore(ap->lock, flags);
586 		schedule();
587 		spin_lock_irqsave(ap->lock, flags);
588 	}
589 	finish_wait(&ap->eh_wait_q, &wait);
590 
591 	spin_unlock_irqrestore(ap->lock, flags);
592 
593 	/* make sure SCSI EH is complete */
594 	if (scsi_host_in_recovery(ap->scsi_host)) {
595 		msleep(10);
596 		goto retry;
597 	}
598 }
599 
600 static int ata_eh_nr_in_flight(struct ata_port *ap)
601 {
602 	unsigned int tag;
603 	int nr = 0;
604 
605 	/* count only non-internal commands */
606 	for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++)
607 		if (ata_qc_from_tag(ap, tag))
608 			nr++;
609 
610 	return nr;
611 }
612 
613 void ata_eh_fastdrain_timerfn(unsigned long arg)
614 {
615 	struct ata_port *ap = (void *)arg;
616 	unsigned long flags;
617 	int cnt;
618 
619 	spin_lock_irqsave(ap->lock, flags);
620 
621 	cnt = ata_eh_nr_in_flight(ap);
622 
623 	/* are we done? */
624 	if (!cnt)
625 		goto out_unlock;
626 
627 	if (cnt == ap->fastdrain_cnt) {
628 		unsigned int tag;
629 
630 		/* No progress during the last interval, tag all
631 		 * in-flight qcs as timed out and freeze the port.
632 		 */
633 		for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) {
634 			struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
635 			if (qc)
636 				qc->err_mask |= AC_ERR_TIMEOUT;
637 		}
638 
639 		ata_port_freeze(ap);
640 	} else {
641 		/* some qcs have finished, give it another chance */
642 		ap->fastdrain_cnt = cnt;
643 		ap->fastdrain_timer.expires =
644 			jiffies + ATA_EH_FASTDRAIN_INTERVAL;
645 		add_timer(&ap->fastdrain_timer);
646 	}
647 
648  out_unlock:
649 	spin_unlock_irqrestore(ap->lock, flags);
650 }
651 
652 /**
653  *	ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
654  *	@ap: target ATA port
655  *	@fastdrain: activate fast drain
656  *
657  *	Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
658  *	is non-zero and EH wasn't pending before.  Fast drain ensures
659  *	that EH kicks in in timely manner.
660  *
661  *	LOCKING:
662  *	spin_lock_irqsave(host lock)
663  */
664 static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
665 {
666 	int cnt;
667 
668 	/* already scheduled? */
669 	if (ap->pflags & ATA_PFLAG_EH_PENDING)
670 		return;
671 
672 	ap->pflags |= ATA_PFLAG_EH_PENDING;
673 
674 	if (!fastdrain)
675 		return;
676 
677 	/* do we have in-flight qcs? */
678 	cnt = ata_eh_nr_in_flight(ap);
679 	if (!cnt)
680 		return;
681 
682 	/* activate fast drain */
683 	ap->fastdrain_cnt = cnt;
684 	ap->fastdrain_timer.expires = jiffies + ATA_EH_FASTDRAIN_INTERVAL;
685 	add_timer(&ap->fastdrain_timer);
686 }
687 
688 /**
689  *	ata_qc_schedule_eh - schedule qc for error handling
690  *	@qc: command to schedule error handling for
691  *
692  *	Schedule error handling for @qc.  EH will kick in as soon as
693  *	other commands are drained.
694  *
695  *	LOCKING:
696  *	spin_lock_irqsave(host lock)
697  */
698 void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
699 {
700 	struct ata_port *ap = qc->ap;
701 
702 	WARN_ON(!ap->ops->error_handler);
703 
704 	qc->flags |= ATA_QCFLAG_FAILED;
705 	ata_eh_set_pending(ap, 1);
706 
707 	/* The following will fail if timeout has already expired.
708 	 * ata_scsi_error() takes care of such scmds on EH entry.
709 	 * Note that ATA_QCFLAG_FAILED is unconditionally set after
710 	 * this function completes.
711 	 */
712 	scsi_req_abort_cmd(qc->scsicmd);
713 }
714 
715 /**
716  *	ata_port_schedule_eh - schedule error handling without a qc
717  *	@ap: ATA port to schedule EH for
718  *
719  *	Schedule error handling for @ap.  EH will kick in as soon as
720  *	all commands are drained.
721  *
722  *	LOCKING:
723  *	spin_lock_irqsave(host lock)
724  */
725 void ata_port_schedule_eh(struct ata_port *ap)
726 {
727 	WARN_ON(!ap->ops->error_handler);
728 
729 	if (ap->pflags & ATA_PFLAG_INITIALIZING)
730 		return;
731 
732 	ata_eh_set_pending(ap, 1);
733 	scsi_schedule_eh(ap->scsi_host);
734 
735 	DPRINTK("port EH scheduled\n");
736 }
737 
738 static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
739 {
740 	int tag, nr_aborted = 0;
741 
742 	WARN_ON(!ap->ops->error_handler);
743 
744 	/* we're gonna abort all commands, no need for fast drain */
745 	ata_eh_set_pending(ap, 0);
746 
747 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
748 		struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
749 
750 		if (qc && (!link || qc->dev->link == link)) {
751 			qc->flags |= ATA_QCFLAG_FAILED;
752 			ata_qc_complete(qc);
753 			nr_aborted++;
754 		}
755 	}
756 
757 	if (!nr_aborted)
758 		ata_port_schedule_eh(ap);
759 
760 	return nr_aborted;
761 }
762 
763 /**
764  *	ata_link_abort - abort all qc's on the link
765  *	@link: ATA link to abort qc's for
766  *
767  *	Abort all active qc's active on @link and schedule EH.
768  *
769  *	LOCKING:
770  *	spin_lock_irqsave(host lock)
771  *
772  *	RETURNS:
773  *	Number of aborted qc's.
774  */
775 int ata_link_abort(struct ata_link *link)
776 {
777 	return ata_do_link_abort(link->ap, link);
778 }
779 
780 /**
781  *	ata_port_abort - abort all qc's on the port
782  *	@ap: ATA port to abort qc's for
783  *
784  *	Abort all active qc's of @ap and schedule EH.
785  *
786  *	LOCKING:
787  *	spin_lock_irqsave(host_set lock)
788  *
789  *	RETURNS:
790  *	Number of aborted qc's.
791  */
792 int ata_port_abort(struct ata_port *ap)
793 {
794 	return ata_do_link_abort(ap, NULL);
795 }
796 
797 /**
798  *	__ata_port_freeze - freeze port
799  *	@ap: ATA port to freeze
800  *
801  *	This function is called when HSM violation or some other
802  *	condition disrupts normal operation of the port.  Frozen port
803  *	is not allowed to perform any operation until the port is
804  *	thawed, which usually follows a successful reset.
805  *
806  *	ap->ops->freeze() callback can be used for freezing the port
807  *	hardware-wise (e.g. mask interrupt and stop DMA engine).  If a
808  *	port cannot be frozen hardware-wise, the interrupt handler
809  *	must ack and clear interrupts unconditionally while the port
810  *	is frozen.
811  *
812  *	LOCKING:
813  *	spin_lock_irqsave(host lock)
814  */
815 static void __ata_port_freeze(struct ata_port *ap)
816 {
817 	WARN_ON(!ap->ops->error_handler);
818 
819 	if (ap->ops->freeze)
820 		ap->ops->freeze(ap);
821 
822 	ap->pflags |= ATA_PFLAG_FROZEN;
823 
824 	DPRINTK("ata%u port frozen\n", ap->print_id);
825 }
826 
827 /**
828  *	ata_port_freeze - abort & freeze port
829  *	@ap: ATA port to freeze
830  *
831  *	Abort and freeze @ap.
832  *
833  *	LOCKING:
834  *	spin_lock_irqsave(host lock)
835  *
836  *	RETURNS:
837  *	Number of aborted commands.
838  */
839 int ata_port_freeze(struct ata_port *ap)
840 {
841 	int nr_aborted;
842 
843 	WARN_ON(!ap->ops->error_handler);
844 
845 	nr_aborted = ata_port_abort(ap);
846 	__ata_port_freeze(ap);
847 
848 	return nr_aborted;
849 }
850 
851 /**
852  *	sata_async_notification - SATA async notification handler
853  *	@ap: ATA port where async notification is received
854  *
855  *	Handler to be called when async notification via SDB FIS is
856  *	received.  This function schedules EH if necessary.
857  *
858  *	LOCKING:
859  *	spin_lock_irqsave(host lock)
860  *
861  *	RETURNS:
862  *	1 if EH is scheduled, 0 otherwise.
863  */
864 int sata_async_notification(struct ata_port *ap)
865 {
866 	u32 sntf;
867 	int rc;
868 
869 	if (!(ap->flags & ATA_FLAG_AN))
870 		return 0;
871 
872 	rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
873 	if (rc == 0)
874 		sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
875 
876 	if (!sata_pmp_attached(ap) || rc) {
877 		/* PMP is not attached or SNTF is not available */
878 		if (!sata_pmp_attached(ap)) {
879 			/* PMP is not attached.  Check whether ATAPI
880 			 * AN is configured.  If so, notify media
881 			 * change.
882 			 */
883 			struct ata_device *dev = ap->link.device;
884 
885 			if ((dev->class == ATA_DEV_ATAPI) &&
886 			    (dev->flags & ATA_DFLAG_AN))
887 				ata_scsi_media_change_notify(dev);
888 			return 0;
889 		} else {
890 			/* PMP is attached but SNTF is not available.
891 			 * ATAPI async media change notification is
892 			 * not used.  The PMP must be reporting PHY
893 			 * status change, schedule EH.
894 			 */
895 			ata_port_schedule_eh(ap);
896 			return 1;
897 		}
898 	} else {
899 		/* PMP is attached and SNTF is available */
900 		struct ata_link *link;
901 
902 		/* check and notify ATAPI AN */
903 		ata_port_for_each_link(link, ap) {
904 			if (!(sntf & (1 << link->pmp)))
905 				continue;
906 
907 			if ((link->device->class == ATA_DEV_ATAPI) &&
908 			    (link->device->flags & ATA_DFLAG_AN))
909 				ata_scsi_media_change_notify(link->device);
910 		}
911 
912 		/* If PMP is reporting that PHY status of some
913 		 * downstream ports has changed, schedule EH.
914 		 */
915 		if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
916 			ata_port_schedule_eh(ap);
917 			return 1;
918 		}
919 
920 		return 0;
921 	}
922 }
923 
924 /**
925  *	ata_eh_freeze_port - EH helper to freeze port
926  *	@ap: ATA port to freeze
927  *
928  *	Freeze @ap.
929  *
930  *	LOCKING:
931  *	None.
932  */
933 void ata_eh_freeze_port(struct ata_port *ap)
934 {
935 	unsigned long flags;
936 
937 	if (!ap->ops->error_handler)
938 		return;
939 
940 	spin_lock_irqsave(ap->lock, flags);
941 	__ata_port_freeze(ap);
942 	spin_unlock_irqrestore(ap->lock, flags);
943 }
944 
945 /**
946  *	ata_port_thaw_port - EH helper to thaw port
947  *	@ap: ATA port to thaw
948  *
949  *	Thaw frozen port @ap.
950  *
951  *	LOCKING:
952  *	None.
953  */
954 void ata_eh_thaw_port(struct ata_port *ap)
955 {
956 	unsigned long flags;
957 
958 	if (!ap->ops->error_handler)
959 		return;
960 
961 	spin_lock_irqsave(ap->lock, flags);
962 
963 	ap->pflags &= ~ATA_PFLAG_FROZEN;
964 
965 	if (ap->ops->thaw)
966 		ap->ops->thaw(ap);
967 
968 	spin_unlock_irqrestore(ap->lock, flags);
969 
970 	DPRINTK("ata%u port thawed\n", ap->print_id);
971 }
972 
973 static void ata_eh_scsidone(struct scsi_cmnd *scmd)
974 {
975 	/* nada */
976 }
977 
978 static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
979 {
980 	struct ata_port *ap = qc->ap;
981 	struct scsi_cmnd *scmd = qc->scsicmd;
982 	unsigned long flags;
983 
984 	spin_lock_irqsave(ap->lock, flags);
985 	qc->scsidone = ata_eh_scsidone;
986 	__ata_qc_complete(qc);
987 	WARN_ON(ata_tag_valid(qc->tag));
988 	spin_unlock_irqrestore(ap->lock, flags);
989 
990 	scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
991 }
992 
993 /**
994  *	ata_eh_qc_complete - Complete an active ATA command from EH
995  *	@qc: Command to complete
996  *
997  *	Indicate to the mid and upper layers that an ATA command has
998  *	completed.  To be used from EH.
999  */
1000 void ata_eh_qc_complete(struct ata_queued_cmd *qc)
1001 {
1002 	struct scsi_cmnd *scmd = qc->scsicmd;
1003 	scmd->retries = scmd->allowed;
1004 	__ata_eh_qc_complete(qc);
1005 }
1006 
1007 /**
1008  *	ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
1009  *	@qc: Command to retry
1010  *
1011  *	Indicate to the mid and upper layers that an ATA command
1012  *	should be retried.  To be used from EH.
1013  *
1014  *	SCSI midlayer limits the number of retries to scmd->allowed.
1015  *	scmd->retries is decremented for commands which get retried
1016  *	due to unrelated failures (qc->err_mask is zero).
1017  */
1018 void ata_eh_qc_retry(struct ata_queued_cmd *qc)
1019 {
1020 	struct scsi_cmnd *scmd = qc->scsicmd;
1021 	if (!qc->err_mask && scmd->retries)
1022 		scmd->retries--;
1023 	__ata_eh_qc_complete(qc);
1024 }
1025 
1026 /**
1027  *	ata_eh_detach_dev - detach ATA device
1028  *	@dev: ATA device to detach
1029  *
1030  *	Detach @dev.
1031  *
1032  *	LOCKING:
1033  *	None.
1034  */
1035 void ata_eh_detach_dev(struct ata_device *dev)
1036 {
1037 	struct ata_link *link = dev->link;
1038 	struct ata_port *ap = link->ap;
1039 	unsigned long flags;
1040 
1041 	ata_dev_disable(dev);
1042 
1043 	spin_lock_irqsave(ap->lock, flags);
1044 
1045 	dev->flags &= ~ATA_DFLAG_DETACH;
1046 
1047 	if (ata_scsi_offline_dev(dev)) {
1048 		dev->flags |= ATA_DFLAG_DETACHED;
1049 		ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
1050 	}
1051 
1052 	/* clear per-dev EH actions */
1053 	ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
1054 	ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
1055 
1056 	spin_unlock_irqrestore(ap->lock, flags);
1057 }
1058 
1059 /**
1060  *	ata_eh_about_to_do - about to perform eh_action
1061  *	@link: target ATA link
1062  *	@dev: target ATA dev for per-dev action (can be NULL)
1063  *	@action: action about to be performed
1064  *
1065  *	Called just before performing EH actions to clear related bits
1066  *	in @link->eh_info such that eh actions are not unnecessarily
1067  *	repeated.
1068  *
1069  *	LOCKING:
1070  *	None.
1071  */
1072 void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
1073 			unsigned int action)
1074 {
1075 	struct ata_port *ap = link->ap;
1076 	struct ata_eh_info *ehi = &link->eh_info;
1077 	struct ata_eh_context *ehc = &link->eh_context;
1078 	unsigned long flags;
1079 
1080 	spin_lock_irqsave(ap->lock, flags);
1081 
1082 	ata_eh_clear_action(link, dev, ehi, action);
1083 
1084 	if (!(ehc->i.flags & ATA_EHI_QUIET))
1085 		ap->pflags |= ATA_PFLAG_RECOVERED;
1086 
1087 	spin_unlock_irqrestore(ap->lock, flags);
1088 }
1089 
1090 /**
1091  *	ata_eh_done - EH action complete
1092 *	@ap: target ATA port
1093  *	@dev: target ATA dev for per-dev action (can be NULL)
1094  *	@action: action just completed
1095  *
1096  *	Called right after performing EH actions to clear related bits
1097  *	in @link->eh_context.
1098  *
1099  *	LOCKING:
1100  *	None.
1101  */
1102 void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1103 		 unsigned int action)
1104 {
1105 	struct ata_eh_context *ehc = &link->eh_context;
1106 
1107 	ata_eh_clear_action(link, dev, &ehc->i, action);
1108 }
1109 
1110 /**
1111  *	ata_err_string - convert err_mask to descriptive string
1112  *	@err_mask: error mask to convert to string
1113  *
1114  *	Convert @err_mask to descriptive string.  Errors are
1115  *	prioritized according to severity and only the most severe
1116  *	error is reported.
1117  *
1118  *	LOCKING:
1119  *	None.
1120  *
1121  *	RETURNS:
1122  *	Descriptive string for @err_mask
1123  */
1124 static const char *ata_err_string(unsigned int err_mask)
1125 {
1126 	if (err_mask & AC_ERR_HOST_BUS)
1127 		return "host bus error";
1128 	if (err_mask & AC_ERR_ATA_BUS)
1129 		return "ATA bus error";
1130 	if (err_mask & AC_ERR_TIMEOUT)
1131 		return "timeout";
1132 	if (err_mask & AC_ERR_HSM)
1133 		return "HSM violation";
1134 	if (err_mask & AC_ERR_SYSTEM)
1135 		return "internal error";
1136 	if (err_mask & AC_ERR_MEDIA)
1137 		return "media error";
1138 	if (err_mask & AC_ERR_INVALID)
1139 		return "invalid argument";
1140 	if (err_mask & AC_ERR_DEV)
1141 		return "device error";
1142 	return "unknown error";
1143 }
1144 
1145 /**
1146  *	ata_read_log_page - read a specific log page
1147  *	@dev: target device
1148  *	@page: page to read
1149  *	@buf: buffer to store read page
1150  *	@sectors: number of sectors to read
1151  *
1152  *	Read log page using READ_LOG_EXT command.
1153  *
1154  *	LOCKING:
1155  *	Kernel thread context (may sleep).
1156  *
1157  *	RETURNS:
1158  *	0 on success, AC_ERR_* mask otherwise.
1159  */
1160 static unsigned int ata_read_log_page(struct ata_device *dev,
1161 				      u8 page, void *buf, unsigned int sectors)
1162 {
1163 	struct ata_taskfile tf;
1164 	unsigned int err_mask;
1165 
1166 	DPRINTK("read log page - page %d\n", page);
1167 
1168 	ata_tf_init(dev, &tf);
1169 	tf.command = ATA_CMD_READ_LOG_EXT;
1170 	tf.lbal = page;
1171 	tf.nsect = sectors;
1172 	tf.hob_nsect = sectors >> 8;
1173 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1174 	tf.protocol = ATA_PROT_PIO;
1175 
1176 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
1177 				     buf, sectors * ATA_SECT_SIZE, 0);
1178 
1179 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
1180 	return err_mask;
1181 }
1182 
1183 /**
1184  *	ata_eh_read_log_10h - Read log page 10h for NCQ error details
1185  *	@dev: Device to read log page 10h from
1186  *	@tag: Resulting tag of the failed command
1187  *	@tf: Resulting taskfile registers of the failed command
1188  *
1189  *	Read log page 10h to obtain NCQ error details and clear error
1190  *	condition.
1191  *
1192  *	LOCKING:
1193  *	Kernel thread context (may sleep).
1194  *
1195  *	RETURNS:
1196  *	0 on success, -errno otherwise.
1197  */
1198 static int ata_eh_read_log_10h(struct ata_device *dev,
1199 			       int *tag, struct ata_taskfile *tf)
1200 {
1201 	u8 *buf = dev->link->ap->sector_buf;
1202 	unsigned int err_mask;
1203 	u8 csum;
1204 	int i;
1205 
1206 	err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
1207 	if (err_mask)
1208 		return -EIO;
1209 
1210 	csum = 0;
1211 	for (i = 0; i < ATA_SECT_SIZE; i++)
1212 		csum += buf[i];
1213 	if (csum)
1214 		ata_dev_printk(dev, KERN_WARNING,
1215 			       "invalid checksum 0x%x on log page 10h\n", csum);
1216 
1217 	if (buf[0] & 0x80)
1218 		return -ENOENT;
1219 
1220 	*tag = buf[0] & 0x1f;
1221 
1222 	tf->command = buf[2];
1223 	tf->feature = buf[3];
1224 	tf->lbal = buf[4];
1225 	tf->lbam = buf[5];
1226 	tf->lbah = buf[6];
1227 	tf->device = buf[7];
1228 	tf->hob_lbal = buf[8];
1229 	tf->hob_lbam = buf[9];
1230 	tf->hob_lbah = buf[10];
1231 	tf->nsect = buf[12];
1232 	tf->hob_nsect = buf[13];
1233 
1234 	return 0;
1235 }
1236 
1237 /**
1238  *	atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1239  *	@dev: device to perform REQUEST_SENSE to
1240  *	@sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
1241  *
1242  *	Perform ATAPI REQUEST_SENSE after the device reported CHECK
1243  *	SENSE.  This function is EH helper.
1244  *
1245  *	LOCKING:
1246  *	Kernel thread context (may sleep).
1247  *
1248  *	RETURNS:
1249  *	0 on success, AC_ERR_* mask on failure
1250  */
1251 static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc)
1252 {
1253 	struct ata_device *dev = qc->dev;
1254 	unsigned char *sense_buf = qc->scsicmd->sense_buffer;
1255 	struct ata_port *ap = dev->link->ap;
1256 	struct ata_taskfile tf;
1257 	u8 cdb[ATAPI_CDB_LEN];
1258 
1259 	DPRINTK("ATAPI request sense\n");
1260 
1261 	/* FIXME: is this needed? */
1262 	memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1263 
1264 	/* initialize sense_buf with the error register,
1265 	 * for the case where they are -not- overwritten
1266 	 */
1267 	sense_buf[0] = 0x70;
1268 	sense_buf[2] = qc->result_tf.feature >> 4;
1269 
1270 	/* some devices time out if garbage left in tf */
1271 	ata_tf_init(dev, &tf);
1272 
1273 	memset(cdb, 0, ATAPI_CDB_LEN);
1274 	cdb[0] = REQUEST_SENSE;
1275 	cdb[4] = SCSI_SENSE_BUFFERSIZE;
1276 
1277 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1278 	tf.command = ATA_CMD_PACKET;
1279 
1280 	/* is it pointless to prefer PIO for "safety reasons"? */
1281 	if (ap->flags & ATA_FLAG_PIO_DMA) {
1282 		tf.protocol = ATAPI_PROT_DMA;
1283 		tf.feature |= ATAPI_PKT_DMA;
1284 	} else {
1285 		tf.protocol = ATAPI_PROT_PIO;
1286 		tf.lbam = SCSI_SENSE_BUFFERSIZE;
1287 		tf.lbah = 0;
1288 	}
1289 
1290 	return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
1291 				 sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
1292 }
1293 
1294 /**
1295  *	ata_eh_analyze_serror - analyze SError for a failed port
1296  *	@link: ATA link to analyze SError for
1297  *
1298  *	Analyze SError if available and further determine cause of
1299  *	failure.
1300  *
1301  *	LOCKING:
1302  *	None.
1303  */
1304 static void ata_eh_analyze_serror(struct ata_link *link)
1305 {
1306 	struct ata_eh_context *ehc = &link->eh_context;
1307 	u32 serror = ehc->i.serror;
1308 	unsigned int err_mask = 0, action = 0;
1309 	u32 hotplug_mask;
1310 
1311 	if (serror & SERR_PERSISTENT) {
1312 		err_mask |= AC_ERR_ATA_BUS;
1313 		action |= ATA_EH_RESET;
1314 	}
1315 	if (serror &
1316 	    (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
1317 		err_mask |= AC_ERR_ATA_BUS;
1318 		action |= ATA_EH_RESET;
1319 	}
1320 	if (serror & SERR_PROTOCOL) {
1321 		err_mask |= AC_ERR_HSM;
1322 		action |= ATA_EH_RESET;
1323 	}
1324 	if (serror & SERR_INTERNAL) {
1325 		err_mask |= AC_ERR_SYSTEM;
1326 		action |= ATA_EH_RESET;
1327 	}
1328 
1329 	/* Determine whether a hotplug event has occurred.  Both
1330 	 * SError.N/X are considered hotplug events for enabled or
1331 	 * host links.  For disabled PMP links, only N bit is
1332 	 * considered as X bit is left at 1 for link plugging.
1333 	 */
1334 	hotplug_mask = 0;
1335 
1336 	if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
1337 		hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
1338 	else
1339 		hotplug_mask = SERR_PHYRDY_CHG;
1340 
1341 	if (serror & hotplug_mask)
1342 		ata_ehi_hotplugged(&ehc->i);
1343 
1344 	ehc->i.err_mask |= err_mask;
1345 	ehc->i.action |= action;
1346 }
1347 
1348 /**
1349  *	ata_eh_analyze_ncq_error - analyze NCQ error
1350  *	@link: ATA link to analyze NCQ error for
1351  *
1352  *	Read log page 10h, determine the offending qc and acquire
1353  *	error status TF.  For NCQ device errors, all LLDDs have to do
1354  *	is setting AC_ERR_DEV in ehi->err_mask.  This function takes
1355  *	care of the rest.
1356  *
1357  *	LOCKING:
1358  *	Kernel thread context (may sleep).
1359  */
1360 void ata_eh_analyze_ncq_error(struct ata_link *link)
1361 {
1362 	struct ata_port *ap = link->ap;
1363 	struct ata_eh_context *ehc = &link->eh_context;
1364 	struct ata_device *dev = link->device;
1365 	struct ata_queued_cmd *qc;
1366 	struct ata_taskfile tf;
1367 	int tag, rc;
1368 
1369 	/* if frozen, we can't do much */
1370 	if (ap->pflags & ATA_PFLAG_FROZEN)
1371 		return;
1372 
1373 	/* is it NCQ device error? */
1374 	if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1375 		return;
1376 
1377 	/* has LLDD analyzed already? */
1378 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1379 		qc = __ata_qc_from_tag(ap, tag);
1380 
1381 		if (!(qc->flags & ATA_QCFLAG_FAILED))
1382 			continue;
1383 
1384 		if (qc->err_mask)
1385 			return;
1386 	}
1387 
1388 	/* okay, this error is ours */
1389 	rc = ata_eh_read_log_10h(dev, &tag, &tf);
1390 	if (rc) {
1391 		ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
1392 				"(errno=%d)\n", rc);
1393 		return;
1394 	}
1395 
1396 	if (!(link->sactive & (1 << tag))) {
1397 		ata_link_printk(link, KERN_ERR, "log page 10h reported "
1398 				"inactive tag %d\n", tag);
1399 		return;
1400 	}
1401 
1402 	/* we've got the perpetrator, condemn it */
1403 	qc = __ata_qc_from_tag(ap, tag);
1404 	memcpy(&qc->result_tf, &tf, sizeof(tf));
1405 	qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
1406 	qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
1407 	ehc->i.err_mask &= ~AC_ERR_DEV;
1408 }
1409 
1410 /**
1411  *	ata_eh_analyze_tf - analyze taskfile of a failed qc
1412  *	@qc: qc to analyze
1413  *	@tf: Taskfile registers to analyze
1414  *
1415  *	Analyze taskfile of @qc and further determine cause of
1416  *	failure.  This function also requests ATAPI sense data if
1417  *	avaliable.
1418  *
1419  *	LOCKING:
1420  *	Kernel thread context (may sleep).
1421  *
1422  *	RETURNS:
1423  *	Determined recovery action
1424  */
1425 static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1426 				      const struct ata_taskfile *tf)
1427 {
1428 	unsigned int tmp, action = 0;
1429 	u8 stat = tf->command, err = tf->feature;
1430 
1431 	if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1432 		qc->err_mask |= AC_ERR_HSM;
1433 		return ATA_EH_RESET;
1434 	}
1435 
1436 	if (stat & (ATA_ERR | ATA_DF))
1437 		qc->err_mask |= AC_ERR_DEV;
1438 	else
1439 		return 0;
1440 
1441 	switch (qc->dev->class) {
1442 	case ATA_DEV_ATA:
1443 		if (err & ATA_ICRC)
1444 			qc->err_mask |= AC_ERR_ATA_BUS;
1445 		if (err & ATA_UNC)
1446 			qc->err_mask |= AC_ERR_MEDIA;
1447 		if (err & ATA_IDNF)
1448 			qc->err_mask |= AC_ERR_INVALID;
1449 		break;
1450 
1451 	case ATA_DEV_ATAPI:
1452 		if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
1453 			tmp = atapi_eh_request_sense(qc);
1454 			if (!tmp) {
1455 				/* ATA_QCFLAG_SENSE_VALID is used to
1456 				 * tell atapi_qc_complete() that sense
1457 				 * data is already valid.
1458 				 *
1459 				 * TODO: interpret sense data and set
1460 				 * appropriate err_mask.
1461 				 */
1462 				qc->flags |= ATA_QCFLAG_SENSE_VALID;
1463 			} else
1464 				qc->err_mask |= tmp;
1465 		}
1466 	}
1467 
1468 	if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1469 		action |= ATA_EH_RESET;
1470 
1471 	return action;
1472 }
1473 
1474 static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask,
1475 				   int *xfer_ok)
1476 {
1477 	int base = 0;
1478 
1479 	if (!(eflags & ATA_EFLAG_DUBIOUS_XFER))
1480 		*xfer_ok = 1;
1481 
1482 	if (!*xfer_ok)
1483 		base = ATA_ECAT_DUBIOUS_NONE;
1484 
1485 	if (err_mask & AC_ERR_ATA_BUS)
1486 		return base + ATA_ECAT_ATA_BUS;
1487 
1488 	if (err_mask & AC_ERR_TIMEOUT)
1489 		return base + ATA_ECAT_TOUT_HSM;
1490 
1491 	if (eflags & ATA_EFLAG_IS_IO) {
1492 		if (err_mask & AC_ERR_HSM)
1493 			return base + ATA_ECAT_TOUT_HSM;
1494 		if ((err_mask &
1495 		     (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1496 			return base + ATA_ECAT_UNK_DEV;
1497 	}
1498 
1499 	return 0;
1500 }
1501 
1502 struct speed_down_verdict_arg {
1503 	u64 since;
1504 	int xfer_ok;
1505 	int nr_errors[ATA_ECAT_NR];
1506 };
1507 
1508 static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
1509 {
1510 	struct speed_down_verdict_arg *arg = void_arg;
1511 	int cat;
1512 
1513 	if (ent->timestamp < arg->since)
1514 		return -1;
1515 
1516 	cat = ata_eh_categorize_error(ent->eflags, ent->err_mask,
1517 				      &arg->xfer_ok);
1518 	arg->nr_errors[cat]++;
1519 
1520 	return 0;
1521 }
1522 
1523 /**
1524  *	ata_eh_speed_down_verdict - Determine speed down verdict
1525  *	@dev: Device of interest
1526  *
1527  *	This function examines error ring of @dev and determines
1528  *	whether NCQ needs to be turned off, transfer speed should be
1529  *	stepped down, or falling back to PIO is necessary.
1530  *
1531  *	ECAT_ATA_BUS	: ATA_BUS error for any command
1532  *
1533  *	ECAT_TOUT_HSM	: TIMEOUT for any command or HSM violation for
1534  *			  IO commands
1535  *
1536  *	ECAT_UNK_DEV	: Unknown DEV error for IO commands
1537  *
1538  *	ECAT_DUBIOUS_*	: Identical to above three but occurred while
1539  *			  data transfer hasn't been verified.
1540  *
1541  *	Verdicts are
1542  *
1543  *	NCQ_OFF		: Turn off NCQ.
1544  *
1545  *	SPEED_DOWN	: Speed down transfer speed but don't fall back
1546  *			  to PIO.
1547  *
1548  *	FALLBACK_TO_PIO	: Fall back to PIO.
1549  *
1550  *	Even if multiple verdicts are returned, only one action is
1551  *	taken per error.  An action triggered by non-DUBIOUS errors
1552  *	clears ering, while one triggered by DUBIOUS_* errors doesn't.
1553  *	This is to expedite speed down decisions right after device is
1554  *	initially configured.
1555  *
1556  *	The followings are speed down rules.  #1 and #2 deal with
1557  *	DUBIOUS errors.
1558  *
1559  *	1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors
1560  *	   occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO.
1561  *
1562  *	2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors
1563  *	   occurred during last 5 mins, NCQ_OFF.
1564  *
1565  *	3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors
1566  *	   ocurred during last 5 mins, FALLBACK_TO_PIO
1567  *
1568  *	4. If more than 3 TOUT_HSM or UNK_DEV errors occurred
1569  *	   during last 10 mins, NCQ_OFF.
1570  *
1571  *	5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6
1572  *	   UNK_DEV errors occurred during last 10 mins, SPEED_DOWN.
1573  *
1574  *	LOCKING:
1575  *	Inherited from caller.
1576  *
1577  *	RETURNS:
1578  *	OR of ATA_EH_SPDN_* flags.
1579  */
1580 static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
1581 {
1582 	const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
1583 	u64 j64 = get_jiffies_64();
1584 	struct speed_down_verdict_arg arg;
1585 	unsigned int verdict = 0;
1586 
1587 	/* scan past 5 mins of error history */
1588 	memset(&arg, 0, sizeof(arg));
1589 	arg.since = j64 - min(j64, j5mins);
1590 	ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1591 
1592 	if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] +
1593 	    arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1)
1594 		verdict |= ATA_EH_SPDN_SPEED_DOWN |
1595 			ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS;
1596 
1597 	if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] +
1598 	    arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1)
1599 		verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS;
1600 
1601 	if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1602 	    arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1603 	    arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
1604 		verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
1605 
1606 	/* scan past 10 mins of error history */
1607 	memset(&arg, 0, sizeof(arg));
1608 	arg.since = j64 - min(j64, j10mins);
1609 	ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1610 
1611 	if (arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1612 	    arg.nr_errors[ATA_ECAT_UNK_DEV] > 3)
1613 		verdict |= ATA_EH_SPDN_NCQ_OFF;
1614 
1615 	if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1616 	    arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 ||
1617 	    arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
1618 		verdict |= ATA_EH_SPDN_SPEED_DOWN;
1619 
1620 	return verdict;
1621 }
1622 
1623 /**
1624  *	ata_eh_speed_down - record error and speed down if necessary
1625  *	@dev: Failed device
1626  *	@eflags: mask of ATA_EFLAG_* flags
1627  *	@err_mask: err_mask of the error
1628  *
1629  *	Record error and examine error history to determine whether
1630  *	adjusting transmission speed is necessary.  It also sets
1631  *	transmission limits appropriately if such adjustment is
1632  *	necessary.
1633  *
1634  *	LOCKING:
1635  *	Kernel thread context (may sleep).
1636  *
1637  *	RETURNS:
1638  *	Determined recovery action.
1639  */
1640 static unsigned int ata_eh_speed_down(struct ata_device *dev,
1641 				unsigned int eflags, unsigned int err_mask)
1642 {
1643 	struct ata_link *link = dev->link;
1644 	int xfer_ok = 0;
1645 	unsigned int verdict;
1646 	unsigned int action = 0;
1647 
1648 	/* don't bother if Cat-0 error */
1649 	if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0)
1650 		return 0;
1651 
1652 	/* record error and determine whether speed down is necessary */
1653 	ata_ering_record(&dev->ering, eflags, err_mask);
1654 	verdict = ata_eh_speed_down_verdict(dev);
1655 
1656 	/* turn off NCQ? */
1657 	if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
1658 	    (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
1659 			   ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
1660 		dev->flags |= ATA_DFLAG_NCQ_OFF;
1661 		ata_dev_printk(dev, KERN_WARNING,
1662 			       "NCQ disabled due to excessive errors\n");
1663 		goto done;
1664 	}
1665 
1666 	/* speed down? */
1667 	if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1668 		/* speed down SATA link speed if possible */
1669 		if (sata_down_spd_limit(link) == 0) {
1670 			action |= ATA_EH_RESET;
1671 			goto done;
1672 		}
1673 
1674 		/* lower transfer mode */
1675 		if (dev->spdn_cnt < 2) {
1676 			static const int dma_dnxfer_sel[] =
1677 				{ ATA_DNXFER_DMA, ATA_DNXFER_40C };
1678 			static const int pio_dnxfer_sel[] =
1679 				{ ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
1680 			int sel;
1681 
1682 			if (dev->xfer_shift != ATA_SHIFT_PIO)
1683 				sel = dma_dnxfer_sel[dev->spdn_cnt];
1684 			else
1685 				sel = pio_dnxfer_sel[dev->spdn_cnt];
1686 
1687 			dev->spdn_cnt++;
1688 
1689 			if (ata_down_xfermask_limit(dev, sel) == 0) {
1690 				action |= ATA_EH_RESET;
1691 				goto done;
1692 			}
1693 		}
1694 	}
1695 
1696 	/* Fall back to PIO?  Slowing down to PIO is meaningless for
1697 	 * SATA ATA devices.  Consider it only for PATA and SATAPI.
1698 	 */
1699 	if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
1700 	    (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) &&
1701 	    (dev->xfer_shift != ATA_SHIFT_PIO)) {
1702 		if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
1703 			dev->spdn_cnt = 0;
1704 			action |= ATA_EH_RESET;
1705 			goto done;
1706 		}
1707 	}
1708 
1709 	return 0;
1710  done:
1711 	/* device has been slowed down, blow error history */
1712 	if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS))
1713 		ata_ering_clear(&dev->ering);
1714 	return action;
1715 }
1716 
1717 /**
1718  *	ata_eh_link_autopsy - analyze error and determine recovery action
1719  *	@link: host link to perform autopsy on
1720  *
1721  *	Analyze why @link failed and determine which recovery actions
1722  *	are needed.  This function also sets more detailed AC_ERR_*
1723  *	values and fills sense data for ATAPI CHECK SENSE.
1724  *
1725  *	LOCKING:
1726  *	Kernel thread context (may sleep).
1727  */
1728 static void ata_eh_link_autopsy(struct ata_link *link)
1729 {
1730 	struct ata_port *ap = link->ap;
1731 	struct ata_eh_context *ehc = &link->eh_context;
1732 	struct ata_device *dev;
1733 	unsigned int all_err_mask = 0, eflags = 0;
1734 	int tag;
1735 	u32 serror;
1736 	int rc;
1737 
1738 	DPRINTK("ENTER\n");
1739 
1740 	if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1741 		return;
1742 
1743 	/* obtain and analyze SError */
1744 	rc = sata_scr_read(link, SCR_ERROR, &serror);
1745 	if (rc == 0) {
1746 		ehc->i.serror |= serror;
1747 		ata_eh_analyze_serror(link);
1748 	} else if (rc != -EOPNOTSUPP) {
1749 		/* SError read failed, force reset and probing */
1750 		ehc->i.probe_mask |= ATA_ALL_DEVICES;
1751 		ehc->i.action |= ATA_EH_RESET;
1752 		ehc->i.err_mask |= AC_ERR_OTHER;
1753 	}
1754 
1755 	/* analyze NCQ failure */
1756 	ata_eh_analyze_ncq_error(link);
1757 
1758 	/* any real error trumps AC_ERR_OTHER */
1759 	if (ehc->i.err_mask & ~AC_ERR_OTHER)
1760 		ehc->i.err_mask &= ~AC_ERR_OTHER;
1761 
1762 	all_err_mask |= ehc->i.err_mask;
1763 
1764 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1765 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1766 
1767 		if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
1768 			continue;
1769 
1770 		/* inherit upper level err_mask */
1771 		qc->err_mask |= ehc->i.err_mask;
1772 
1773 		/* analyze TF */
1774 		ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1775 
1776 		/* DEV errors are probably spurious in case of ATA_BUS error */
1777 		if (qc->err_mask & AC_ERR_ATA_BUS)
1778 			qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1779 					  AC_ERR_INVALID);
1780 
1781 		/* any real error trumps unknown error */
1782 		if (qc->err_mask & ~AC_ERR_OTHER)
1783 			qc->err_mask &= ~AC_ERR_OTHER;
1784 
1785 		/* SENSE_VALID trumps dev/unknown error and revalidation */
1786 		if (qc->flags & ATA_QCFLAG_SENSE_VALID)
1787 			qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1788 
1789 		/* determine whether the command is worth retrying */
1790 		if (!(qc->err_mask & AC_ERR_INVALID) &&
1791 		    ((qc->flags & ATA_QCFLAG_IO) || qc->err_mask != AC_ERR_DEV))
1792 			qc->flags |= ATA_QCFLAG_RETRY;
1793 
1794 		/* accumulate error info */
1795 		ehc->i.dev = qc->dev;
1796 		all_err_mask |= qc->err_mask;
1797 		if (qc->flags & ATA_QCFLAG_IO)
1798 			eflags |= ATA_EFLAG_IS_IO;
1799 	}
1800 
1801 	/* enforce default EH actions */
1802 	if (ap->pflags & ATA_PFLAG_FROZEN ||
1803 	    all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
1804 		ehc->i.action |= ATA_EH_RESET;
1805 	else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) ||
1806 		 (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV)))
1807 		ehc->i.action |= ATA_EH_REVALIDATE;
1808 
1809 	/* If we have offending qcs and the associated failed device,
1810 	 * perform per-dev EH action only on the offending device.
1811 	 */
1812 	if (ehc->i.dev) {
1813 		ehc->i.dev_action[ehc->i.dev->devno] |=
1814 			ehc->i.action & ATA_EH_PERDEV_MASK;
1815 		ehc->i.action &= ~ATA_EH_PERDEV_MASK;
1816 	}
1817 
1818 	/* propagate timeout to host link */
1819 	if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link))
1820 		ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT;
1821 
1822 	/* record error and consider speeding down */
1823 	dev = ehc->i.dev;
1824 	if (!dev && ((ata_link_max_devices(link) == 1 &&
1825 		      ata_dev_enabled(link->device))))
1826 	    dev = link->device;
1827 
1828 	if (dev) {
1829 		if (dev->flags & ATA_DFLAG_DUBIOUS_XFER)
1830 			eflags |= ATA_EFLAG_DUBIOUS_XFER;
1831 		ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask);
1832 	}
1833 
1834 	DPRINTK("EXIT\n");
1835 }
1836 
1837 /**
1838  *	ata_eh_autopsy - analyze error and determine recovery action
1839  *	@ap: host port to perform autopsy on
1840  *
1841  *	Analyze all links of @ap and determine why they failed and
1842  *	which recovery actions are needed.
1843  *
1844  *	LOCKING:
1845  *	Kernel thread context (may sleep).
1846  */
1847 void ata_eh_autopsy(struct ata_port *ap)
1848 {
1849 	struct ata_link *link;
1850 
1851 	ata_port_for_each_link(link, ap)
1852 		ata_eh_link_autopsy(link);
1853 
1854 	/* Autopsy of fanout ports can affect host link autopsy.
1855 	 * Perform host link autopsy last.
1856 	 */
1857 	if (sata_pmp_attached(ap))
1858 		ata_eh_link_autopsy(&ap->link);
1859 }
1860 
1861 /**
1862  *	ata_eh_link_report - report error handling to user
1863  *	@link: ATA link EH is going on
1864  *
1865  *	Report EH to user.
1866  *
1867  *	LOCKING:
1868  *	None.
1869  */
1870 static void ata_eh_link_report(struct ata_link *link)
1871 {
1872 	struct ata_port *ap = link->ap;
1873 	struct ata_eh_context *ehc = &link->eh_context;
1874 	const char *frozen, *desc;
1875 	char tries_buf[6];
1876 	int tag, nr_failed = 0;
1877 
1878 	if (ehc->i.flags & ATA_EHI_QUIET)
1879 		return;
1880 
1881 	desc = NULL;
1882 	if (ehc->i.desc[0] != '\0')
1883 		desc = ehc->i.desc;
1884 
1885 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1886 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1887 
1888 		if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link ||
1889 		    ((qc->flags & ATA_QCFLAG_QUIET) &&
1890 		     qc->err_mask == AC_ERR_DEV))
1891 			continue;
1892 		if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1893 			continue;
1894 
1895 		nr_failed++;
1896 	}
1897 
1898 	if (!nr_failed && !ehc->i.err_mask)
1899 		return;
1900 
1901 	frozen = "";
1902 	if (ap->pflags & ATA_PFLAG_FROZEN)
1903 		frozen = " frozen";
1904 
1905 	memset(tries_buf, 0, sizeof(tries_buf));
1906 	if (ap->eh_tries < ATA_EH_MAX_TRIES)
1907 		snprintf(tries_buf, sizeof(tries_buf) - 1, " t%d",
1908 			 ap->eh_tries);
1909 
1910 	if (ehc->i.dev) {
1911 		ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1912 			       "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1913 			       ehc->i.err_mask, link->sactive, ehc->i.serror,
1914 			       ehc->i.action, frozen, tries_buf);
1915 		if (desc)
1916 			ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
1917 	} else {
1918 		ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
1919 				"SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1920 				ehc->i.err_mask, link->sactive, ehc->i.serror,
1921 				ehc->i.action, frozen, tries_buf);
1922 		if (desc)
1923 			ata_link_printk(link, KERN_ERR, "%s\n", desc);
1924 	}
1925 
1926 	if (ehc->i.serror)
1927 		ata_port_printk(ap, KERN_ERR,
1928 		  "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
1929 		  ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
1930 		  ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
1931 		  ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
1932 		  ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
1933 		  ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
1934 		  ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
1935 		  ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
1936 		  ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
1937 		  ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
1938 		  ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
1939 		  ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
1940 		  ehc->i.serror & SERR_CRC ? "BadCRC " : "",
1941 		  ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
1942 		  ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
1943 		  ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
1944 		  ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
1945 		  ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
1946 
1947 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1948 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1949 		struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
1950 		const u8 *cdb = qc->cdb;
1951 		char data_buf[20] = "";
1952 		char cdb_buf[70] = "";
1953 
1954 		if (!(qc->flags & ATA_QCFLAG_FAILED) ||
1955 		    qc->dev->link != link || !qc->err_mask)
1956 			continue;
1957 
1958 		if (qc->dma_dir != DMA_NONE) {
1959 			static const char *dma_str[] = {
1960 				[DMA_BIDIRECTIONAL]	= "bidi",
1961 				[DMA_TO_DEVICE]		= "out",
1962 				[DMA_FROM_DEVICE]	= "in",
1963 			};
1964 			static const char *prot_str[] = {
1965 				[ATA_PROT_PIO]		= "pio",
1966 				[ATA_PROT_DMA]		= "dma",
1967 				[ATA_PROT_NCQ]		= "ncq",
1968 				[ATAPI_PROT_PIO]	= "pio",
1969 				[ATAPI_PROT_DMA]	= "dma",
1970 			};
1971 
1972 			snprintf(data_buf, sizeof(data_buf), " %s %u %s",
1973 				 prot_str[qc->tf.protocol], qc->nbytes,
1974 				 dma_str[qc->dma_dir]);
1975 		}
1976 
1977 		if (ata_is_atapi(qc->tf.protocol))
1978 			snprintf(cdb_buf, sizeof(cdb_buf),
1979 				 "cdb %02x %02x %02x %02x %02x %02x %02x %02x  "
1980 				 "%02x %02x %02x %02x %02x %02x %02x %02x\n         ",
1981 				 cdb[0], cdb[1], cdb[2], cdb[3],
1982 				 cdb[4], cdb[5], cdb[6], cdb[7],
1983 				 cdb[8], cdb[9], cdb[10], cdb[11],
1984 				 cdb[12], cdb[13], cdb[14], cdb[15]);
1985 
1986 		ata_dev_printk(qc->dev, KERN_ERR,
1987 			"cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
1988 			"tag %d%s\n         %s"
1989 			"res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
1990 			"Emask 0x%x (%s)%s\n",
1991 			cmd->command, cmd->feature, cmd->nsect,
1992 			cmd->lbal, cmd->lbam, cmd->lbah,
1993 			cmd->hob_feature, cmd->hob_nsect,
1994 			cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
1995 			cmd->device, qc->tag, data_buf, cdb_buf,
1996 			res->command, res->feature, res->nsect,
1997 			res->lbal, res->lbam, res->lbah,
1998 			res->hob_feature, res->hob_nsect,
1999 			res->hob_lbal, res->hob_lbam, res->hob_lbah,
2000 			res->device, qc->err_mask, ata_err_string(qc->err_mask),
2001 			qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
2002 
2003 		if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
2004 				    ATA_ERR)) {
2005 			if (res->command & ATA_BUSY)
2006 				ata_dev_printk(qc->dev, KERN_ERR,
2007 				  "status: { Busy }\n");
2008 			else
2009 				ata_dev_printk(qc->dev, KERN_ERR,
2010 				  "status: { %s%s%s%s}\n",
2011 				  res->command & ATA_DRDY ? "DRDY " : "",
2012 				  res->command & ATA_DF ? "DF " : "",
2013 				  res->command & ATA_DRQ ? "DRQ " : "",
2014 				  res->command & ATA_ERR ? "ERR " : "");
2015 		}
2016 
2017 		if (cmd->command != ATA_CMD_PACKET &&
2018 		    (res->feature & (ATA_ICRC | ATA_UNC | ATA_IDNF |
2019 				     ATA_ABORTED)))
2020 			ata_dev_printk(qc->dev, KERN_ERR,
2021 			  "error: { %s%s%s%s}\n",
2022 			  res->feature & ATA_ICRC ? "ICRC " : "",
2023 			  res->feature & ATA_UNC ? "UNC " : "",
2024 			  res->feature & ATA_IDNF ? "IDNF " : "",
2025 			  res->feature & ATA_ABORTED ? "ABRT " : "");
2026 	}
2027 }
2028 
2029 /**
2030  *	ata_eh_report - report error handling to user
2031  *	@ap: ATA port to report EH about
2032  *
2033  *	Report EH to user.
2034  *
2035  *	LOCKING:
2036  *	None.
2037  */
2038 void ata_eh_report(struct ata_port *ap)
2039 {
2040 	struct ata_link *link;
2041 
2042 	__ata_port_for_each_link(link, ap)
2043 		ata_eh_link_report(link);
2044 }
2045 
2046 static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
2047 			unsigned int *classes, unsigned long deadline)
2048 {
2049 	struct ata_device *dev;
2050 	int rc;
2051 
2052 	ata_link_for_each_dev(dev, link)
2053 		classes[dev->devno] = ATA_DEV_UNKNOWN;
2054 
2055 	rc = reset(link, classes, deadline);
2056 
2057 	/* convert all ATA_DEV_UNKNOWN to ATA_DEV_NONE */
2058 	ata_link_for_each_dev(dev, link)
2059 		if (classes[dev->devno] == ATA_DEV_UNKNOWN)
2060 			classes[dev->devno] = ATA_DEV_NONE;
2061 
2062 	return rc;
2063 }
2064 
2065 static int ata_eh_followup_srst_needed(struct ata_link *link,
2066 				       int rc, int classify,
2067 				       const unsigned int *classes)
2068 {
2069 	if ((link->flags & ATA_LFLAG_NO_SRST) || ata_link_offline(link))
2070 		return 0;
2071 	if (rc == -EAGAIN) {
2072 		if (classify)
2073 			return 1;
2074 		rc = 0;
2075 	}
2076 	if (rc != 0)
2077 		return 0;
2078 	if (sata_pmp_supported(link->ap) && ata_is_host_link(link))
2079 		return 1;
2080 	return 0;
2081 }
2082 
2083 int ata_eh_reset(struct ata_link *link, int classify,
2084 		 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
2085 		 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
2086 {
2087 	const int max_tries = ARRAY_SIZE(ata_eh_reset_timeouts);
2088 	struct ata_port *ap = link->ap;
2089 	struct ata_eh_context *ehc = &link->eh_context;
2090 	unsigned int *classes = ehc->classes;
2091 	unsigned int lflags = link->flags;
2092 	int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
2093 	int try = 0;
2094 	struct ata_device *dev;
2095 	unsigned long deadline, now;
2096 	ata_reset_fn_t reset;
2097 	unsigned long flags;
2098 	u32 sstatus;
2099 	int rc;
2100 
2101 	/* about to reset */
2102 	spin_lock_irqsave(ap->lock, flags);
2103 	ap->pflags |= ATA_PFLAG_RESETTING;
2104 	spin_unlock_irqrestore(ap->lock, flags);
2105 
2106 	ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
2107 
2108 	ata_link_for_each_dev(dev, link) {
2109 		/* If we issue an SRST then an ATA drive (not ATAPI)
2110 		 * may change configuration and be in PIO0 timing. If
2111 		 * we do a hard reset (or are coming from power on)
2112 		 * this is true for ATA or ATAPI. Until we've set a
2113 		 * suitable controller mode we should not touch the
2114 		 * bus as we may be talking too fast.
2115 		 */
2116 		dev->pio_mode = XFER_PIO_0;
2117 
2118 		/* If the controller has a pio mode setup function
2119 		 * then use it to set the chipset to rights. Don't
2120 		 * touch the DMA setup as that will be dealt with when
2121 		 * configuring devices.
2122 		 */
2123 		if (ap->ops->set_piomode)
2124 			ap->ops->set_piomode(ap, dev);
2125 	}
2126 
2127 	if (!softreset && !hardreset) {
2128 		if (verbose)
2129 			ata_link_printk(link, KERN_INFO, "no reset method "
2130 					"available, skipping reset\n");
2131 		if (!(lflags & ATA_LFLAG_ASSUME_CLASS))
2132 			lflags |= ATA_LFLAG_ASSUME_ATA;
2133 		goto done;
2134 	}
2135 
2136 	/* prefer hardreset */
2137 	ehc->i.action &= ~ATA_EH_RESET;
2138 	if (hardreset) {
2139 		reset = hardreset;
2140 		ehc->i.action = ATA_EH_HARDRESET;
2141 	} else if (softreset) {
2142 		reset = softreset;
2143 		ehc->i.action = ATA_EH_SOFTRESET;
2144 	} else {
2145 		ata_link_printk(link, KERN_ERR, "BUG: no reset method, "
2146 				"please report to linux-ide@vger.kernel.org\n");
2147 		dump_stack();
2148 		return -EINVAL;
2149 	}
2150 
2151 	if (prereset) {
2152 		rc = prereset(link, jiffies + ATA_EH_PRERESET_TIMEOUT);
2153 		if (rc) {
2154 			if (rc == -ENOENT) {
2155 				ata_link_printk(link, KERN_DEBUG,
2156 						"port disabled. ignoring.\n");
2157 				ehc->i.action &= ~ATA_EH_RESET;
2158 
2159 				ata_link_for_each_dev(dev, link)
2160 					classes[dev->devno] = ATA_DEV_NONE;
2161 
2162 				rc = 0;
2163 			} else
2164 				ata_link_printk(link, KERN_ERR,
2165 					"prereset failed (errno=%d)\n", rc);
2166 			goto out;
2167 		}
2168 	}
2169 
2170 	/* prereset() might have cleared ATA_EH_RESET */
2171 	if (!(ehc->i.action & ATA_EH_RESET)) {
2172 		/* prereset told us not to reset, bang classes and return */
2173 		ata_link_for_each_dev(dev, link)
2174 			classes[dev->devno] = ATA_DEV_NONE;
2175 		rc = 0;
2176 		goto out;
2177 	}
2178 
2179  retry:
2180 	deadline = jiffies + ata_eh_reset_timeouts[try++];
2181 
2182 	/* shut up during boot probing */
2183 	if (verbose)
2184 		ata_link_printk(link, KERN_INFO, "%s resetting link\n",
2185 				reset == softreset ? "soft" : "hard");
2186 
2187 	/* mark that this EH session started with reset */
2188 	if (reset == hardreset)
2189 		ehc->i.flags |= ATA_EHI_DID_HARDRESET;
2190 	else
2191 		ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
2192 
2193 	rc = ata_do_reset(link, reset, classes, deadline);
2194 
2195 	if (reset == hardreset &&
2196 	    ata_eh_followup_srst_needed(link, rc, classify, classes)) {
2197 		/* okay, let's do follow-up softreset */
2198 		reset = softreset;
2199 
2200 		if (!reset) {
2201 			ata_link_printk(link, KERN_ERR,
2202 					"follow-up softreset required "
2203 					"but no softreset avaliable\n");
2204 			rc = -EINVAL;
2205 			goto fail;
2206 		}
2207 
2208 		ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
2209 		rc = ata_do_reset(link, reset, classes, deadline);
2210 	}
2211 
2212 	/* -EAGAIN can happen if we skipped followup SRST */
2213 	if (rc && rc != -EAGAIN)
2214 		goto fail;
2215 
2216  done:
2217 	ata_link_for_each_dev(dev, link) {
2218 		/* After the reset, the device state is PIO 0 and the
2219 		 * controller state is undefined.  Reset also wakes up
2220 		 * drives from sleeping mode.
2221 		 */
2222 		dev->pio_mode = XFER_PIO_0;
2223 		dev->flags &= ~ATA_DFLAG_SLEEPING;
2224 
2225 		if (ata_link_offline(link))
2226 			continue;
2227 
2228 		/* apply class override */
2229 		if (lflags & ATA_LFLAG_ASSUME_ATA)
2230 			classes[dev->devno] = ATA_DEV_ATA;
2231 		else if (lflags & ATA_LFLAG_ASSUME_SEMB)
2232 			classes[dev->devno] = ATA_DEV_SEMB_UNSUP; /* not yet */
2233 	}
2234 
2235 	/* record current link speed */
2236 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2237 		link->sata_spd = (sstatus >> 4) & 0xf;
2238 
2239 	if (postreset)
2240 		postreset(link, classes);
2241 
2242 	/* reset successful, schedule revalidation */
2243 	ata_eh_done(link, NULL, ATA_EH_RESET);
2244 	ehc->i.action |= ATA_EH_REVALIDATE;
2245 
2246 	rc = 0;
2247  out:
2248 	/* clear hotplug flag */
2249 	ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
2250 
2251 	spin_lock_irqsave(ap->lock, flags);
2252 	ap->pflags &= ~ATA_PFLAG_RESETTING;
2253 	spin_unlock_irqrestore(ap->lock, flags);
2254 
2255 	return rc;
2256 
2257  fail:
2258 	/* if SCR isn't accessible on a fan-out port, PMP needs to be reset */
2259 	if (!ata_is_host_link(link) &&
2260 	    sata_scr_read(link, SCR_STATUS, &sstatus))
2261 		rc = -ERESTART;
2262 
2263 	if (rc == -ERESTART || try >= max_tries)
2264 		goto out;
2265 
2266 	now = jiffies;
2267 	if (time_before(now, deadline)) {
2268 		unsigned long delta = deadline - now;
2269 
2270 		ata_link_printk(link, KERN_WARNING, "reset failed "
2271 				"(errno=%d), retrying in %u secs\n",
2272 				rc, (jiffies_to_msecs(delta) + 999) / 1000);
2273 
2274 		while (delta)
2275 			delta = schedule_timeout_uninterruptible(delta);
2276 	}
2277 
2278 	if (rc == -EPIPE || try == max_tries - 1)
2279 		sata_down_spd_limit(link);
2280 	if (hardreset)
2281 		reset = hardreset;
2282 	goto retry;
2283 }
2284 
2285 static int ata_eh_revalidate_and_attach(struct ata_link *link,
2286 					struct ata_device **r_failed_dev)
2287 {
2288 	struct ata_port *ap = link->ap;
2289 	struct ata_eh_context *ehc = &link->eh_context;
2290 	struct ata_device *dev;
2291 	unsigned int new_mask = 0;
2292 	unsigned long flags;
2293 	int rc = 0;
2294 
2295 	DPRINTK("ENTER\n");
2296 
2297 	/* For PATA drive side cable detection to work, IDENTIFY must
2298 	 * be done backwards such that PDIAG- is released by the slave
2299 	 * device before the master device is identified.
2300 	 */
2301 	ata_link_for_each_dev_reverse(dev, link) {
2302 		unsigned int action = ata_eh_dev_action(dev);
2303 		unsigned int readid_flags = 0;
2304 
2305 		if (ehc->i.flags & ATA_EHI_DID_RESET)
2306 			readid_flags |= ATA_READID_POSTRESET;
2307 
2308 		if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
2309 			WARN_ON(dev->class == ATA_DEV_PMP);
2310 
2311 			if (ata_link_offline(link)) {
2312 				rc = -EIO;
2313 				goto err;
2314 			}
2315 
2316 			ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
2317 			rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
2318 						readid_flags);
2319 			if (rc)
2320 				goto err;
2321 
2322 			ata_eh_done(link, dev, ATA_EH_REVALIDATE);
2323 
2324 			/* Configuration may have changed, reconfigure
2325 			 * transfer mode.
2326 			 */
2327 			ehc->i.flags |= ATA_EHI_SETMODE;
2328 
2329 			/* schedule the scsi_rescan_device() here */
2330 			queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
2331 		} else if (dev->class == ATA_DEV_UNKNOWN &&
2332 			   ehc->tries[dev->devno] &&
2333 			   ata_class_enabled(ehc->classes[dev->devno])) {
2334 			dev->class = ehc->classes[dev->devno];
2335 
2336 			if (dev->class == ATA_DEV_PMP)
2337 				rc = sata_pmp_attach(dev);
2338 			else
2339 				rc = ata_dev_read_id(dev, &dev->class,
2340 						     readid_flags, dev->id);
2341 			switch (rc) {
2342 			case 0:
2343 				new_mask |= 1 << dev->devno;
2344 				break;
2345 			case -ENOENT:
2346 				/* IDENTIFY was issued to non-existent
2347 				 * device.  No need to reset.  Just
2348 				 * thaw and kill the device.
2349 				 */
2350 				ata_eh_thaw_port(ap);
2351 				dev->class = ATA_DEV_UNKNOWN;
2352 				break;
2353 			default:
2354 				dev->class = ATA_DEV_UNKNOWN;
2355 				goto err;
2356 			}
2357 		}
2358 	}
2359 
2360 	/* PDIAG- should have been released, ask cable type if post-reset */
2361 	if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) {
2362 		if (ap->ops->cable_detect)
2363 			ap->cbl = ap->ops->cable_detect(ap);
2364 		ata_force_cbl(ap);
2365 	}
2366 
2367 	/* Configure new devices forward such that user doesn't see
2368 	 * device detection messages backwards.
2369 	 */
2370 	ata_link_for_each_dev(dev, link) {
2371 		if (!(new_mask & (1 << dev->devno)) ||
2372 		    dev->class == ATA_DEV_PMP)
2373 			continue;
2374 
2375 		ehc->i.flags |= ATA_EHI_PRINTINFO;
2376 		rc = ata_dev_configure(dev);
2377 		ehc->i.flags &= ~ATA_EHI_PRINTINFO;
2378 		if (rc)
2379 			goto err;
2380 
2381 		spin_lock_irqsave(ap->lock, flags);
2382 		ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
2383 		spin_unlock_irqrestore(ap->lock, flags);
2384 
2385 		/* new device discovered, configure xfermode */
2386 		ehc->i.flags |= ATA_EHI_SETMODE;
2387 	}
2388 
2389 	return 0;
2390 
2391  err:
2392 	*r_failed_dev = dev;
2393 	DPRINTK("EXIT rc=%d\n", rc);
2394 	return rc;
2395 }
2396 
2397 /**
2398  *	ata_set_mode - Program timings and issue SET FEATURES - XFER
2399  *	@link: link on which timings will be programmed
2400  *	@r_failed_dev: out paramter for failed device
2401  *
2402  *	Set ATA device disk transfer mode (PIO3, UDMA6, etc.).  If
2403  *	ata_set_mode() fails, pointer to the failing device is
2404  *	returned in @r_failed_dev.
2405  *
2406  *	LOCKING:
2407  *	PCI/etc. bus probe sem.
2408  *
2409  *	RETURNS:
2410  *	0 on success, negative errno otherwise
2411  */
2412 int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
2413 {
2414 	struct ata_port *ap = link->ap;
2415 	struct ata_device *dev;
2416 	int rc;
2417 
2418 	/* if data transfer is verified, clear DUBIOUS_XFER on ering top */
2419 	ata_link_for_each_dev(dev, link) {
2420 		if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
2421 			struct ata_ering_entry *ent;
2422 
2423 			ent = ata_ering_top(&dev->ering);
2424 			if (ent)
2425 				ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER;
2426 		}
2427 	}
2428 
2429 	/* has private set_mode? */
2430 	if (ap->ops->set_mode)
2431 		rc = ap->ops->set_mode(link, r_failed_dev);
2432 	else
2433 		rc = ata_do_set_mode(link, r_failed_dev);
2434 
2435 	/* if transfer mode has changed, set DUBIOUS_XFER on device */
2436 	ata_link_for_each_dev(dev, link) {
2437 		struct ata_eh_context *ehc = &link->eh_context;
2438 		u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
2439 		u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
2440 
2441 		if (dev->xfer_mode != saved_xfer_mode ||
2442 		    ata_ncq_enabled(dev) != saved_ncq)
2443 			dev->flags |= ATA_DFLAG_DUBIOUS_XFER;
2444 	}
2445 
2446 	return rc;
2447 }
2448 
2449 static int ata_link_nr_enabled(struct ata_link *link)
2450 {
2451 	struct ata_device *dev;
2452 	int cnt = 0;
2453 
2454 	ata_link_for_each_dev(dev, link)
2455 		if (ata_dev_enabled(dev))
2456 			cnt++;
2457 	return cnt;
2458 }
2459 
2460 static int ata_link_nr_vacant(struct ata_link *link)
2461 {
2462 	struct ata_device *dev;
2463 	int cnt = 0;
2464 
2465 	ata_link_for_each_dev(dev, link)
2466 		if (dev->class == ATA_DEV_UNKNOWN)
2467 			cnt++;
2468 	return cnt;
2469 }
2470 
2471 static int ata_eh_skip_recovery(struct ata_link *link)
2472 {
2473 	struct ata_port *ap = link->ap;
2474 	struct ata_eh_context *ehc = &link->eh_context;
2475 	struct ata_device *dev;
2476 
2477 	/* skip disabled links */
2478 	if (link->flags & ATA_LFLAG_DISABLED)
2479 		return 1;
2480 
2481 	/* thaw frozen port and recover failed devices */
2482 	if ((ap->pflags & ATA_PFLAG_FROZEN) || ata_link_nr_enabled(link))
2483 		return 0;
2484 
2485 	/* reset at least once if reset is requested */
2486 	if ((ehc->i.action & ATA_EH_RESET) &&
2487 	    !(ehc->i.flags & ATA_EHI_DID_RESET))
2488 		return 0;
2489 
2490 	/* skip if class codes for all vacant slots are ATA_DEV_NONE */
2491 	ata_link_for_each_dev(dev, link) {
2492 		if (dev->class == ATA_DEV_UNKNOWN &&
2493 		    ehc->classes[dev->devno] != ATA_DEV_NONE)
2494 			return 0;
2495 	}
2496 
2497 	return 1;
2498 }
2499 
2500 static int ata_eh_schedule_probe(struct ata_device *dev)
2501 {
2502 	struct ata_eh_context *ehc = &dev->link->eh_context;
2503 
2504 	if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
2505 	    (ehc->did_probe_mask & (1 << dev->devno)))
2506 		return 0;
2507 
2508 	ata_eh_detach_dev(dev);
2509 	ata_dev_init(dev);
2510 	ehc->did_probe_mask |= (1 << dev->devno);
2511 	ehc->i.action |= ATA_EH_RESET;
2512 	ehc->saved_xfer_mode[dev->devno] = 0;
2513 	ehc->saved_ncq_enabled &= ~(1 << dev->devno);
2514 
2515 	return 1;
2516 }
2517 
2518 static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
2519 {
2520 	struct ata_eh_context *ehc = &dev->link->eh_context;
2521 
2522 	ehc->tries[dev->devno]--;
2523 
2524 	switch (err) {
2525 	case -ENODEV:
2526 		/* device missing or wrong IDENTIFY data, schedule probing */
2527 		ehc->i.probe_mask |= (1 << dev->devno);
2528 	case -EINVAL:
2529 		/* give it just one more chance */
2530 		ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
2531 	case -EIO:
2532 		if (ehc->tries[dev->devno] == 1 && dev->pio_mode > XFER_PIO_0) {
2533 			/* This is the last chance, better to slow
2534 			 * down than lose it.
2535 			 */
2536 			sata_down_spd_limit(dev->link);
2537 			ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2538 		}
2539 	}
2540 
2541 	if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2542 		/* disable device if it has used up all its chances */
2543 		ata_dev_disable(dev);
2544 
2545 		/* detach if offline */
2546 		if (ata_link_offline(dev->link))
2547 			ata_eh_detach_dev(dev);
2548 
2549 		/* schedule probe if necessary */
2550 		if (ata_eh_schedule_probe(dev))
2551 			ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2552 
2553 		return 1;
2554 	} else {
2555 		ehc->i.action |= ATA_EH_RESET;
2556 		return 0;
2557 	}
2558 }
2559 
2560 /**
2561  *	ata_eh_recover - recover host port after error
2562  *	@ap: host port to recover
2563  *	@prereset: prereset method (can be NULL)
2564  *	@softreset: softreset method (can be NULL)
2565  *	@hardreset: hardreset method (can be NULL)
2566  *	@postreset: postreset method (can be NULL)
2567  *	@r_failed_link: out parameter for failed link
2568  *
2569  *	This is the alpha and omega, eum and yang, heart and soul of
2570  *	libata exception handling.  On entry, actions required to
2571  *	recover each link and hotplug requests are recorded in the
2572  *	link's eh_context.  This function executes all the operations
2573  *	with appropriate retrials and fallbacks to resurrect failed
2574  *	devices, detach goners and greet newcomers.
2575  *
2576  *	LOCKING:
2577  *	Kernel thread context (may sleep).
2578  *
2579  *	RETURNS:
2580  *	0 on success, -errno on failure.
2581  */
2582 int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
2583 		   ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2584 		   ata_postreset_fn_t postreset,
2585 		   struct ata_link **r_failed_link)
2586 {
2587 	struct ata_link *link;
2588 	struct ata_device *dev;
2589 	int nr_failed_devs, nr_disabled_devs;
2590 	int reset, rc;
2591 	unsigned long flags;
2592 
2593 	DPRINTK("ENTER\n");
2594 
2595 	/* prep for recovery */
2596 	ata_port_for_each_link(link, ap) {
2597 		struct ata_eh_context *ehc = &link->eh_context;
2598 
2599 		/* re-enable link? */
2600 		if (ehc->i.action & ATA_EH_ENABLE_LINK) {
2601 			ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
2602 			spin_lock_irqsave(ap->lock, flags);
2603 			link->flags &= ~ATA_LFLAG_DISABLED;
2604 			spin_unlock_irqrestore(ap->lock, flags);
2605 			ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
2606 		}
2607 
2608 		ata_link_for_each_dev(dev, link) {
2609 			if (link->flags & ATA_LFLAG_NO_RETRY)
2610 				ehc->tries[dev->devno] = 1;
2611 			else
2612 				ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2613 
2614 			/* collect port action mask recorded in dev actions */
2615 			ehc->i.action |= ehc->i.dev_action[dev->devno] &
2616 					 ~ATA_EH_PERDEV_MASK;
2617 			ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
2618 
2619 			/* process hotplug request */
2620 			if (dev->flags & ATA_DFLAG_DETACH)
2621 				ata_eh_detach_dev(dev);
2622 
2623 			/* schedule probe if necessary */
2624 			if (!ata_dev_enabled(dev))
2625 				ata_eh_schedule_probe(dev);
2626 		}
2627 	}
2628 
2629  retry:
2630 	rc = 0;
2631 	nr_failed_devs = 0;
2632 	nr_disabled_devs = 0;
2633 	reset = 0;
2634 
2635 	/* if UNLOADING, finish immediately */
2636 	if (ap->pflags & ATA_PFLAG_UNLOADING)
2637 		goto out;
2638 
2639 	/* prep for EH */
2640 	ata_port_for_each_link(link, ap) {
2641 		struct ata_eh_context *ehc = &link->eh_context;
2642 
2643 		/* skip EH if possible. */
2644 		if (ata_eh_skip_recovery(link))
2645 			ehc->i.action = 0;
2646 
2647 		/* do we need to reset? */
2648 		if (ehc->i.action & ATA_EH_RESET)
2649 			reset = 1;
2650 
2651 		ata_link_for_each_dev(dev, link)
2652 			ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
2653 	}
2654 
2655 	/* reset */
2656 	if (reset) {
2657 		/* if PMP is attached, this function only deals with
2658 		 * downstream links, port should stay thawed.
2659 		 */
2660 		if (!sata_pmp_attached(ap))
2661 			ata_eh_freeze_port(ap);
2662 
2663 		ata_port_for_each_link(link, ap) {
2664 			struct ata_eh_context *ehc = &link->eh_context;
2665 
2666 			if (!(ehc->i.action & ATA_EH_RESET))
2667 				continue;
2668 
2669 			rc = ata_eh_reset(link, ata_link_nr_vacant(link),
2670 					  prereset, softreset, hardreset,
2671 					  postreset);
2672 			if (rc) {
2673 				ata_link_printk(link, KERN_ERR,
2674 						"reset failed, giving up\n");
2675 				goto out;
2676 			}
2677 		}
2678 
2679 		if (!sata_pmp_attached(ap))
2680 			ata_eh_thaw_port(ap);
2681 	}
2682 
2683 	/* the rest */
2684 	ata_port_for_each_link(link, ap) {
2685 		struct ata_eh_context *ehc = &link->eh_context;
2686 
2687 		/* revalidate existing devices and attach new ones */
2688 		rc = ata_eh_revalidate_and_attach(link, &dev);
2689 		if (rc)
2690 			goto dev_fail;
2691 
2692 		/* if PMP got attached, return, pmp EH will take care of it */
2693 		if (link->device->class == ATA_DEV_PMP) {
2694 			ehc->i.action = 0;
2695 			return 0;
2696 		}
2697 
2698 		/* configure transfer mode if necessary */
2699 		if (ehc->i.flags & ATA_EHI_SETMODE) {
2700 			rc = ata_set_mode(link, &dev);
2701 			if (rc)
2702 				goto dev_fail;
2703 			ehc->i.flags &= ~ATA_EHI_SETMODE;
2704 		}
2705 
2706 		if (ehc->i.action & ATA_EH_LPM)
2707 			ata_link_for_each_dev(dev, link)
2708 				ata_dev_enable_pm(dev, ap->pm_policy);
2709 
2710 		/* this link is okay now */
2711 		ehc->i.flags = 0;
2712 		continue;
2713 
2714 dev_fail:
2715 		nr_failed_devs++;
2716 		if (ata_eh_handle_dev_fail(dev, rc))
2717 			nr_disabled_devs++;
2718 
2719 		if (ap->pflags & ATA_PFLAG_FROZEN) {
2720 			/* PMP reset requires working host port.
2721 			 * Can't retry if it's frozen.
2722 			 */
2723 			if (sata_pmp_attached(ap))
2724 				goto out;
2725 			break;
2726 		}
2727 	}
2728 
2729 	if (nr_failed_devs) {
2730 		if (nr_failed_devs != nr_disabled_devs) {
2731 			ata_port_printk(ap, KERN_WARNING, "failed to recover "
2732 					"some devices, retrying in 5 secs\n");
2733 			ssleep(5);
2734 		} else {
2735 			/* no device left to recover, repeat fast */
2736 			msleep(500);
2737 		}
2738 
2739 		goto retry;
2740 	}
2741 
2742  out:
2743 	if (rc && r_failed_link)
2744 		*r_failed_link = link;
2745 
2746 	DPRINTK("EXIT, rc=%d\n", rc);
2747 	return rc;
2748 }
2749 
2750 /**
2751  *	ata_eh_finish - finish up EH
2752  *	@ap: host port to finish EH for
2753  *
2754  *	Recovery is complete.  Clean up EH states and retry or finish
2755  *	failed qcs.
2756  *
2757  *	LOCKING:
2758  *	None.
2759  */
2760 void ata_eh_finish(struct ata_port *ap)
2761 {
2762 	int tag;
2763 
2764 	/* retry or finish qcs */
2765 	for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2766 		struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2767 
2768 		if (!(qc->flags & ATA_QCFLAG_FAILED))
2769 			continue;
2770 
2771 		if (qc->err_mask) {
2772 			/* FIXME: Once EH migration is complete,
2773 			 * generate sense data in this function,
2774 			 * considering both err_mask and tf.
2775 			 */
2776 			if (qc->flags & ATA_QCFLAG_RETRY)
2777 				ata_eh_qc_retry(qc);
2778 			else
2779 				ata_eh_qc_complete(qc);
2780 		} else {
2781 			if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2782 				ata_eh_qc_complete(qc);
2783 			} else {
2784 				/* feed zero TF to sense generation */
2785 				memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2786 				ata_eh_qc_retry(qc);
2787 			}
2788 		}
2789 	}
2790 
2791 	/* make sure nr_active_links is zero after EH */
2792 	WARN_ON(ap->nr_active_links);
2793 	ap->nr_active_links = 0;
2794 }
2795 
2796 /**
2797  *	ata_do_eh - do standard error handling
2798  *	@ap: host port to handle error for
2799  *
2800  *	@prereset: prereset method (can be NULL)
2801  *	@softreset: softreset method (can be NULL)
2802  *	@hardreset: hardreset method (can be NULL)
2803  *	@postreset: postreset method (can be NULL)
2804  *
2805  *	Perform standard error handling sequence.
2806  *
2807  *	LOCKING:
2808  *	Kernel thread context (may sleep).
2809  */
2810 void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2811 	       ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2812 	       ata_postreset_fn_t postreset)
2813 {
2814 	struct ata_device *dev;
2815 	int rc;
2816 
2817 	ata_eh_autopsy(ap);
2818 	ata_eh_report(ap);
2819 
2820 	rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
2821 			    NULL);
2822 	if (rc) {
2823 		ata_link_for_each_dev(dev, &ap->link)
2824 			ata_dev_disable(dev);
2825 	}
2826 
2827 	ata_eh_finish(ap);
2828 }
2829 
2830 /**
2831  *	ata_std_error_handler - standard error handler
2832  *	@ap: host port to handle error for
2833  *
2834  *	Standard error handler
2835  *
2836  *	LOCKING:
2837  *	Kernel thread context (may sleep).
2838  */
2839 void ata_std_error_handler(struct ata_port *ap)
2840 {
2841 	struct ata_port_operations *ops = ap->ops;
2842 	ata_reset_fn_t hardreset = ops->hardreset;
2843 
2844 	/* ignore built-in hardreset if SCR access is not available */
2845 	if (ata_is_builtin_hardreset(hardreset) && !sata_scr_valid(&ap->link))
2846 		hardreset = NULL;
2847 
2848 	ata_do_eh(ap, ops->prereset, ops->softreset, hardreset, ops->postreset);
2849 }
2850 
2851 #ifdef CONFIG_PM
2852 /**
2853  *	ata_eh_handle_port_suspend - perform port suspend operation
2854  *	@ap: port to suspend
2855  *
2856  *	Suspend @ap.
2857  *
2858  *	LOCKING:
2859  *	Kernel thread context (may sleep).
2860  */
2861 static void ata_eh_handle_port_suspend(struct ata_port *ap)
2862 {
2863 	unsigned long flags;
2864 	int rc = 0;
2865 
2866 	/* are we suspending? */
2867 	spin_lock_irqsave(ap->lock, flags);
2868 	if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2869 	    ap->pm_mesg.event == PM_EVENT_ON) {
2870 		spin_unlock_irqrestore(ap->lock, flags);
2871 		return;
2872 	}
2873 	spin_unlock_irqrestore(ap->lock, flags);
2874 
2875 	WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
2876 
2877 	/* tell ACPI we're suspending */
2878 	rc = ata_acpi_on_suspend(ap);
2879 	if (rc)
2880 		goto out;
2881 
2882 	/* suspend */
2883 	ata_eh_freeze_port(ap);
2884 
2885 	if (ap->ops->port_suspend)
2886 		rc = ap->ops->port_suspend(ap, ap->pm_mesg);
2887 
2888 	ata_acpi_set_state(ap, PMSG_SUSPEND);
2889  out:
2890 	/* report result */
2891 	spin_lock_irqsave(ap->lock, flags);
2892 
2893 	ap->pflags &= ~ATA_PFLAG_PM_PENDING;
2894 	if (rc == 0)
2895 		ap->pflags |= ATA_PFLAG_SUSPENDED;
2896 	else if (ap->pflags & ATA_PFLAG_FROZEN)
2897 		ata_port_schedule_eh(ap);
2898 
2899 	if (ap->pm_result) {
2900 		*ap->pm_result = rc;
2901 		ap->pm_result = NULL;
2902 	}
2903 
2904 	spin_unlock_irqrestore(ap->lock, flags);
2905 
2906 	return;
2907 }
2908 
2909 /**
2910  *	ata_eh_handle_port_resume - perform port resume operation
2911  *	@ap: port to resume
2912  *
2913  *	Resume @ap.
2914  *
2915  *	LOCKING:
2916  *	Kernel thread context (may sleep).
2917  */
2918 static void ata_eh_handle_port_resume(struct ata_port *ap)
2919 {
2920 	unsigned long flags;
2921 	int rc = 0;
2922 
2923 	/* are we resuming? */
2924 	spin_lock_irqsave(ap->lock, flags);
2925 	if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2926 	    ap->pm_mesg.event != PM_EVENT_ON) {
2927 		spin_unlock_irqrestore(ap->lock, flags);
2928 		return;
2929 	}
2930 	spin_unlock_irqrestore(ap->lock, flags);
2931 
2932 	WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
2933 
2934 	ata_acpi_set_state(ap, PMSG_ON);
2935 
2936 	if (ap->ops->port_resume)
2937 		rc = ap->ops->port_resume(ap);
2938 
2939 	/* tell ACPI that we're resuming */
2940 	ata_acpi_on_resume(ap);
2941 
2942 	/* report result */
2943 	spin_lock_irqsave(ap->lock, flags);
2944 	ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
2945 	if (ap->pm_result) {
2946 		*ap->pm_result = rc;
2947 		ap->pm_result = NULL;
2948 	}
2949 	spin_unlock_irqrestore(ap->lock, flags);
2950 }
2951 #endif /* CONFIG_PM */
2952