aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/ata/libata-eh.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/ata/libata-eh.c')
-rw-r--r--drivers/ata/libata-eh.c2246
1 files changed, 2246 insertions, 0 deletions
diff --git a/drivers/ata/libata-eh.c b/drivers/ata/libata-eh.c
new file mode 100644
index 000000000000..2c476eee463f
--- /dev/null
+++ b/drivers/ata/libata-eh.c
@@ -0,0 +1,2246 @@
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/config.h>
36#include <linux/kernel.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
48static void __ata_port_freeze(struct ata_port *ap);
49static void ata_eh_finish(struct ata_port *ap);
50static void ata_eh_handle_port_suspend(struct ata_port *ap);
51static void ata_eh_handle_port_resume(struct ata_port *ap);
52
53static void ata_ering_record(struct ata_ering *ering, int is_io,
54 unsigned int err_mask)
55{
56 struct ata_ering_entry *ent;
57
58 WARN_ON(!err_mask);
59
60 ering->cursor++;
61 ering->cursor %= ATA_ERING_SIZE;
62
63 ent = &ering->ring[ering->cursor];
64 ent->is_io = is_io;
65 ent->err_mask = err_mask;
66 ent->timestamp = get_jiffies_64();
67}
68
69static struct ata_ering_entry * ata_ering_top(struct ata_ering *ering)
70{
71 struct ata_ering_entry *ent = &ering->ring[ering->cursor];
72 if (!ent->err_mask)
73 return NULL;
74 return ent;
75}
76
77static int ata_ering_map(struct ata_ering *ering,
78 int (*map_fn)(struct ata_ering_entry *, void *),
79 void *arg)
80{
81 int idx, rc = 0;
82 struct ata_ering_entry *ent;
83
84 idx = ering->cursor;
85 do {
86 ent = &ering->ring[idx];
87 if (!ent->err_mask)
88 break;
89 rc = map_fn(ent, arg);
90 if (rc)
91 break;
92 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
93 } while (idx != ering->cursor);
94
95 return rc;
96}
97
98static unsigned int ata_eh_dev_action(struct ata_device *dev)
99{
100 struct ata_eh_context *ehc = &dev->ap->eh_context;
101
102 return ehc->i.action | ehc->i.dev_action[dev->devno];
103}
104
105static void ata_eh_clear_action(struct ata_device *dev,
106 struct ata_eh_info *ehi, unsigned int action)
107{
108 int i;
109
110 if (!dev) {
111 ehi->action &= ~action;
112 for (i = 0; i < ATA_MAX_DEVICES; i++)
113 ehi->dev_action[i] &= ~action;
114 } else {
115 /* doesn't make sense for port-wide EH actions */
116 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
117
118 /* break ehi->action into ehi->dev_action */
119 if (ehi->action & action) {
120 for (i = 0; i < ATA_MAX_DEVICES; i++)
121 ehi->dev_action[i] |= ehi->action & action;
122 ehi->action &= ~action;
123 }
124
125 /* turn off the specified per-dev action */
126 ehi->dev_action[dev->devno] &= ~action;
127 }
128}
129
130/**
131 * ata_scsi_timed_out - SCSI layer time out callback
132 * @cmd: timed out SCSI command
133 *
134 * Handles SCSI layer timeout. We race with normal completion of
135 * the qc for @cmd. If the qc is already gone, we lose and let
136 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
137 * timed out and EH should be invoked. Prevent ata_qc_complete()
138 * from finishing it by setting EH_SCHEDULED and return
139 * EH_NOT_HANDLED.
140 *
141 * TODO: kill this function once old EH is gone.
142 *
143 * LOCKING:
144 * Called from timer context
145 *
146 * RETURNS:
147 * EH_HANDLED or EH_NOT_HANDLED
148 */
149enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
150{
151 struct Scsi_Host *host = cmd->device->host;
152 struct ata_port *ap = ata_shost_to_port(host);
153 unsigned long flags;
154 struct ata_queued_cmd *qc;
155 enum scsi_eh_timer_return ret;
156
157 DPRINTK("ENTER\n");
158
159 if (ap->ops->error_handler) {
160 ret = EH_NOT_HANDLED;
161 goto out;
162 }
163
164 ret = EH_HANDLED;
165 spin_lock_irqsave(ap->lock, flags);
166 qc = ata_qc_from_tag(ap, ap->active_tag);
167 if (qc) {
168 WARN_ON(qc->scsicmd != cmd);
169 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
170 qc->err_mask |= AC_ERR_TIMEOUT;
171 ret = EH_NOT_HANDLED;
172 }
173 spin_unlock_irqrestore(ap->lock, flags);
174
175 out:
176 DPRINTK("EXIT, ret=%d\n", ret);
177 return ret;
178}
179
180/**
181 * ata_scsi_error - SCSI layer error handler callback
182 * @host: SCSI host on which error occurred
183 *
184 * Handles SCSI-layer-thrown error events.
185 *
186 * LOCKING:
187 * Inherited from SCSI layer (none, can sleep)
188 *
189 * RETURNS:
190 * Zero.
191 */
192void ata_scsi_error(struct Scsi_Host *host)
193{
194 struct ata_port *ap = ata_shost_to_port(host);
195 int i, repeat_cnt = ATA_EH_MAX_REPEAT;
196 unsigned long flags;
197
198 DPRINTK("ENTER\n");
199
200 /* synchronize with port task */
201 ata_port_flush_task(ap);
202
203 /* synchronize with host_set lock and sort out timeouts */
204
205 /* For new EH, all qcs are finished in one of three ways -
206 * normal completion, error completion, and SCSI timeout.
207 * Both cmpletions can race against SCSI timeout. When normal
208 * completion wins, the qc never reaches EH. When error
209 * completion wins, the qc has ATA_QCFLAG_FAILED set.
210 *
211 * When SCSI timeout wins, things are a bit more complex.
212 * Normal or error completion can occur after the timeout but
213 * before this point. In such cases, both types of
214 * completions are honored. A scmd is determined to have
215 * timed out iff its associated qc is active and not failed.
216 */
217 if (ap->ops->error_handler) {
218 struct scsi_cmnd *scmd, *tmp;
219 int nr_timedout = 0;
220
221 spin_lock_irqsave(ap->lock, flags);
222
223 list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
224 struct ata_queued_cmd *qc;
225
226 for (i = 0; i < ATA_MAX_QUEUE; i++) {
227 qc = __ata_qc_from_tag(ap, i);
228 if (qc->flags & ATA_QCFLAG_ACTIVE &&
229 qc->scsicmd == scmd)
230 break;
231 }
232
233 if (i < ATA_MAX_QUEUE) {
234 /* the scmd has an associated qc */
235 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
236 /* which hasn't failed yet, timeout */
237 qc->err_mask |= AC_ERR_TIMEOUT;
238 qc->flags |= ATA_QCFLAG_FAILED;
239 nr_timedout++;
240 }
241 } else {
242 /* Normal completion occurred after
243 * SCSI timeout but before this point.
244 * Successfully complete it.
245 */
246 scmd->retries = scmd->allowed;
247 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
248 }
249 }
250
251 /* If we have timed out qcs. They belong to EH from
252 * this point but the state of the controller is
253 * unknown. Freeze the port to make sure the IRQ
254 * handler doesn't diddle with those qcs. This must
255 * be done atomically w.r.t. setting QCFLAG_FAILED.
256 */
257 if (nr_timedout)
258 __ata_port_freeze(ap);
259
260 spin_unlock_irqrestore(ap->lock, flags);
261 } else
262 spin_unlock_wait(ap->lock);
263
264 repeat:
265 /* invoke error handler */
266 if (ap->ops->error_handler) {
267 /* process port resume request */
268 ata_eh_handle_port_resume(ap);
269
270 /* fetch & clear EH info */
271 spin_lock_irqsave(ap->lock, flags);
272
273 memset(&ap->eh_context, 0, sizeof(ap->eh_context));
274 ap->eh_context.i = ap->eh_info;
275 memset(&ap->eh_info, 0, sizeof(ap->eh_info));
276
277 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
278 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
279
280 spin_unlock_irqrestore(ap->lock, flags);
281
282 /* invoke EH, skip if unloading or suspended */
283 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
284 ap->ops->error_handler(ap);
285 else
286 ata_eh_finish(ap);
287
288 /* process port suspend request */
289 ata_eh_handle_port_suspend(ap);
290
291 /* Exception might have happend after ->error_handler
292 * recovered the port but before this point. Repeat
293 * EH in such case.
294 */
295 spin_lock_irqsave(ap->lock, flags);
296
297 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
298 if (--repeat_cnt) {
299 ata_port_printk(ap, KERN_INFO,
300 "EH pending after completion, "
301 "repeating EH (cnt=%d)\n", repeat_cnt);
302 spin_unlock_irqrestore(ap->lock, flags);
303 goto repeat;
304 }
305 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
306 "tries, giving up\n", ATA_EH_MAX_REPEAT);
307 }
308
309 /* this run is complete, make sure EH info is clear */
310 memset(&ap->eh_info, 0, sizeof(ap->eh_info));
311
312 /* Clear host_eh_scheduled while holding ap->lock such
313 * that if exception occurs after this point but
314 * before EH completion, SCSI midlayer will
315 * re-initiate EH.
316 */
317 host->host_eh_scheduled = 0;
318
319 spin_unlock_irqrestore(ap->lock, flags);
320 } else {
321 WARN_ON(ata_qc_from_tag(ap, ap->active_tag) == NULL);
322 ap->ops->eng_timeout(ap);
323 }
324
325 /* finish or retry handled scmd's and clean up */
326 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
327
328 scsi_eh_flush_done_q(&ap->eh_done_q);
329
330 /* clean up */
331 spin_lock_irqsave(ap->lock, flags);
332
333 if (ap->pflags & ATA_PFLAG_LOADING)
334 ap->pflags &= ~ATA_PFLAG_LOADING;
335 else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
336 queue_work(ata_aux_wq, &ap->hotplug_task);
337
338 if (ap->pflags & ATA_PFLAG_RECOVERED)
339 ata_port_printk(ap, KERN_INFO, "EH complete\n");
340
341 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
342
343 /* tell wait_eh that we're done */
344 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
345 wake_up_all(&ap->eh_wait_q);
346
347 spin_unlock_irqrestore(ap->lock, flags);
348
349 DPRINTK("EXIT\n");
350}
351
352/**
353 * ata_port_wait_eh - Wait for the currently pending EH to complete
354 * @ap: Port to wait EH for
355 *
356 * Wait until the currently pending EH is complete.
357 *
358 * LOCKING:
359 * Kernel thread context (may sleep).
360 */
361void ata_port_wait_eh(struct ata_port *ap)
362{
363 unsigned long flags;
364 DEFINE_WAIT(wait);
365
366 retry:
367 spin_lock_irqsave(ap->lock, flags);
368
369 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
370 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
371 spin_unlock_irqrestore(ap->lock, flags);
372 schedule();
373 spin_lock_irqsave(ap->lock, flags);
374 }
375 finish_wait(&ap->eh_wait_q, &wait);
376
377 spin_unlock_irqrestore(ap->lock, flags);
378
379 /* make sure SCSI EH is complete */
380 if (scsi_host_in_recovery(ap->host)) {
381 msleep(10);
382 goto retry;
383 }
384}
385
386/**
387 * ata_qc_timeout - Handle timeout of queued command
388 * @qc: Command that timed out
389 *
390 * Some part of the kernel (currently, only the SCSI layer)
391 * has noticed that the active command on port @ap has not
392 * completed after a specified length of time. Handle this
393 * condition by disabling DMA (if necessary) and completing
394 * transactions, with error if necessary.
395 *
396 * This also handles the case of the "lost interrupt", where
397 * for some reason (possibly hardware bug, possibly driver bug)
398 * an interrupt was not delivered to the driver, even though the
399 * transaction completed successfully.
400 *
401 * TODO: kill this function once old EH is gone.
402 *
403 * LOCKING:
404 * Inherited from SCSI layer (none, can sleep)
405 */
406static void ata_qc_timeout(struct ata_queued_cmd *qc)
407{
408 struct ata_port *ap = qc->ap;
409 u8 host_stat = 0, drv_stat;
410 unsigned long flags;
411
412 DPRINTK("ENTER\n");
413
414 ap->hsm_task_state = HSM_ST_IDLE;
415
416 spin_lock_irqsave(ap->lock, flags);
417
418 switch (qc->tf.protocol) {
419
420 case ATA_PROT_DMA:
421 case ATA_PROT_ATAPI_DMA:
422 host_stat = ap->ops->bmdma_status(ap);
423
424 /* before we do anything else, clear DMA-Start bit */
425 ap->ops->bmdma_stop(qc);
426
427 /* fall through */
428
429 default:
430 ata_altstatus(ap);
431 drv_stat = ata_chk_status(ap);
432
433 /* ack bmdma irq events */
434 ap->ops->irq_clear(ap);
435
436 ata_dev_printk(qc->dev, KERN_ERR, "command 0x%x timeout, "
437 "stat 0x%x host_stat 0x%x\n",
438 qc->tf.command, drv_stat, host_stat);
439
440 /* complete taskfile transaction */
441 qc->err_mask |= AC_ERR_TIMEOUT;
442 break;
443 }
444
445 spin_unlock_irqrestore(ap->lock, flags);
446
447 ata_eh_qc_complete(qc);
448
449 DPRINTK("EXIT\n");
450}
451
452/**
453 * ata_eng_timeout - Handle timeout of queued command
454 * @ap: Port on which timed-out command is active
455 *
456 * Some part of the kernel (currently, only the SCSI layer)
457 * has noticed that the active command on port @ap has not
458 * completed after a specified length of time. Handle this
459 * condition by disabling DMA (if necessary) and completing
460 * transactions, with error if necessary.
461 *
462 * This also handles the case of the "lost interrupt", where
463 * for some reason (possibly hardware bug, possibly driver bug)
464 * an interrupt was not delivered to the driver, even though the
465 * transaction completed successfully.
466 *
467 * TODO: kill this function once old EH is gone.
468 *
469 * LOCKING:
470 * Inherited from SCSI layer (none, can sleep)
471 */
472void ata_eng_timeout(struct ata_port *ap)
473{
474 DPRINTK("ENTER\n");
475
476 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
477
478 DPRINTK("EXIT\n");
479}
480
481/**
482 * ata_qc_schedule_eh - schedule qc for error handling
483 * @qc: command to schedule error handling for
484 *
485 * Schedule error handling for @qc. EH will kick in as soon as
486 * other commands are drained.
487 *
488 * LOCKING:
489 * spin_lock_irqsave(host_set lock)
490 */
491void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
492{
493 struct ata_port *ap = qc->ap;
494
495 WARN_ON(!ap->ops->error_handler);
496
497 qc->flags |= ATA_QCFLAG_FAILED;
498 qc->ap->pflags |= ATA_PFLAG_EH_PENDING;
499
500 /* The following will fail if timeout has already expired.
501 * ata_scsi_error() takes care of such scmds on EH entry.
502 * Note that ATA_QCFLAG_FAILED is unconditionally set after
503 * this function completes.
504 */
505 scsi_req_abort_cmd(qc->scsicmd);
506}
507
508/**
509 * ata_port_schedule_eh - schedule error handling without a qc
510 * @ap: ATA port to schedule EH for
511 *
512 * Schedule error handling for @ap. EH will kick in as soon as
513 * all commands are drained.
514 *
515 * LOCKING:
516 * spin_lock_irqsave(host_set lock)
517 */
518void ata_port_schedule_eh(struct ata_port *ap)
519{
520 WARN_ON(!ap->ops->error_handler);
521
522 ap->pflags |= ATA_PFLAG_EH_PENDING;
523 scsi_schedule_eh(ap->host);
524
525 DPRINTK("port EH scheduled\n");
526}
527
528/**
529 * ata_port_abort - abort all qc's on the port
530 * @ap: ATA port to abort qc's for
531 *
532 * Abort all active qc's of @ap and schedule EH.
533 *
534 * LOCKING:
535 * spin_lock_irqsave(host_set lock)
536 *
537 * RETURNS:
538 * Number of aborted qc's.
539 */
540int ata_port_abort(struct ata_port *ap)
541{
542 int tag, nr_aborted = 0;
543
544 WARN_ON(!ap->ops->error_handler);
545
546 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
547 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
548
549 if (qc) {
550 qc->flags |= ATA_QCFLAG_FAILED;
551 ata_qc_complete(qc);
552 nr_aborted++;
553 }
554 }
555
556 if (!nr_aborted)
557 ata_port_schedule_eh(ap);
558
559 return nr_aborted;
560}
561
562/**
563 * __ata_port_freeze - freeze port
564 * @ap: ATA port to freeze
565 *
566 * This function is called when HSM violation or some other
567 * condition disrupts normal operation of the port. Frozen port
568 * is not allowed to perform any operation until the port is
569 * thawed, which usually follows a successful reset.
570 *
571 * ap->ops->freeze() callback can be used for freezing the port
572 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
573 * port cannot be frozen hardware-wise, the interrupt handler
574 * must ack and clear interrupts unconditionally while the port
575 * is frozen.
576 *
577 * LOCKING:
578 * spin_lock_irqsave(host_set lock)
579 */
580static void __ata_port_freeze(struct ata_port *ap)
581{
582 WARN_ON(!ap->ops->error_handler);
583
584 if (ap->ops->freeze)
585 ap->ops->freeze(ap);
586
587 ap->pflags |= ATA_PFLAG_FROZEN;
588
589 DPRINTK("ata%u port frozen\n", ap->id);
590}
591
592/**
593 * ata_port_freeze - abort & freeze port
594 * @ap: ATA port to freeze
595 *
596 * Abort and freeze @ap.
597 *
598 * LOCKING:
599 * spin_lock_irqsave(host_set lock)
600 *
601 * RETURNS:
602 * Number of aborted commands.
603 */
604int ata_port_freeze(struct ata_port *ap)
605{
606 int nr_aborted;
607
608 WARN_ON(!ap->ops->error_handler);
609
610 nr_aborted = ata_port_abort(ap);
611 __ata_port_freeze(ap);
612
613 return nr_aborted;
614}
615
616/**
617 * ata_eh_freeze_port - EH helper to freeze port
618 * @ap: ATA port to freeze
619 *
620 * Freeze @ap.
621 *
622 * LOCKING:
623 * None.
624 */
625void ata_eh_freeze_port(struct ata_port *ap)
626{
627 unsigned long flags;
628
629 if (!ap->ops->error_handler)
630 return;
631
632 spin_lock_irqsave(ap->lock, flags);
633 __ata_port_freeze(ap);
634 spin_unlock_irqrestore(ap->lock, flags);
635}
636
637/**
638 * ata_port_thaw_port - EH helper to thaw port
639 * @ap: ATA port to thaw
640 *
641 * Thaw frozen port @ap.
642 *
643 * LOCKING:
644 * None.
645 */
646void ata_eh_thaw_port(struct ata_port *ap)
647{
648 unsigned long flags;
649
650 if (!ap->ops->error_handler)
651 return;
652
653 spin_lock_irqsave(ap->lock, flags);
654
655 ap->pflags &= ~ATA_PFLAG_FROZEN;
656
657 if (ap->ops->thaw)
658 ap->ops->thaw(ap);
659
660 spin_unlock_irqrestore(ap->lock, flags);
661
662 DPRINTK("ata%u port thawed\n", ap->id);
663}
664
665static void ata_eh_scsidone(struct scsi_cmnd *scmd)
666{
667 /* nada */
668}
669
670static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
671{
672 struct ata_port *ap = qc->ap;
673 struct scsi_cmnd *scmd = qc->scsicmd;
674 unsigned long flags;
675
676 spin_lock_irqsave(ap->lock, flags);
677 qc->scsidone = ata_eh_scsidone;
678 __ata_qc_complete(qc);
679 WARN_ON(ata_tag_valid(qc->tag));
680 spin_unlock_irqrestore(ap->lock, flags);
681
682 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
683}
684
685/**
686 * ata_eh_qc_complete - Complete an active ATA command from EH
687 * @qc: Command to complete
688 *
689 * Indicate to the mid and upper layers that an ATA command has
690 * completed. To be used from EH.
691 */
692void ata_eh_qc_complete(struct ata_queued_cmd *qc)
693{
694 struct scsi_cmnd *scmd = qc->scsicmd;
695 scmd->retries = scmd->allowed;
696 __ata_eh_qc_complete(qc);
697}
698
699/**
700 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
701 * @qc: Command to retry
702 *
703 * Indicate to the mid and upper layers that an ATA command
704 * should be retried. To be used from EH.
705 *
706 * SCSI midlayer limits the number of retries to scmd->allowed.
707 * scmd->retries is decremented for commands which get retried
708 * due to unrelated failures (qc->err_mask is zero).
709 */
710void ata_eh_qc_retry(struct ata_queued_cmd *qc)
711{
712 struct scsi_cmnd *scmd = qc->scsicmd;
713 if (!qc->err_mask && scmd->retries)
714 scmd->retries--;
715 __ata_eh_qc_complete(qc);
716}
717
718/**
719 * ata_eh_detach_dev - detach ATA device
720 * @dev: ATA device to detach
721 *
722 * Detach @dev.
723 *
724 * LOCKING:
725 * None.
726 */
727static void ata_eh_detach_dev(struct ata_device *dev)
728{
729 struct ata_port *ap = dev->ap;
730 unsigned long flags;
731
732 ata_dev_disable(dev);
733
734 spin_lock_irqsave(ap->lock, flags);
735
736 dev->flags &= ~ATA_DFLAG_DETACH;
737
738 if (ata_scsi_offline_dev(dev)) {
739 dev->flags |= ATA_DFLAG_DETACHED;
740 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
741 }
742
743 /* clear per-dev EH actions */
744 ata_eh_clear_action(dev, &ap->eh_info, ATA_EH_PERDEV_MASK);
745 ata_eh_clear_action(dev, &ap->eh_context.i, ATA_EH_PERDEV_MASK);
746
747 spin_unlock_irqrestore(ap->lock, flags);
748}
749
750/**
751 * ata_eh_about_to_do - about to perform eh_action
752 * @ap: target ATA port
753 * @dev: target ATA dev for per-dev action (can be NULL)
754 * @action: action about to be performed
755 *
756 * Called just before performing EH actions to clear related bits
757 * in @ap->eh_info such that eh actions are not unnecessarily
758 * repeated.
759 *
760 * LOCKING:
761 * None.
762 */
763static void ata_eh_about_to_do(struct ata_port *ap, struct ata_device *dev,
764 unsigned int action)
765{
766 unsigned long flags;
767 struct ata_eh_info *ehi = &ap->eh_info;
768 struct ata_eh_context *ehc = &ap->eh_context;
769
770 spin_lock_irqsave(ap->lock, flags);
771
772 /* Reset is represented by combination of actions and EHI
773 * flags. Suck in all related bits before clearing eh_info to
774 * avoid losing requested action.
775 */
776 if (action & ATA_EH_RESET_MASK) {
777 ehc->i.action |= ehi->action & ATA_EH_RESET_MASK;
778 ehc->i.flags |= ehi->flags & ATA_EHI_RESET_MODIFIER_MASK;
779
780 /* make sure all reset actions are cleared & clear EHI flags */
781 action |= ATA_EH_RESET_MASK;
782 ehi->flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
783 }
784
785 ata_eh_clear_action(dev, ehi, action);
786
787 if (!(ehc->i.flags & ATA_EHI_QUIET))
788 ap->pflags |= ATA_PFLAG_RECOVERED;
789
790 spin_unlock_irqrestore(ap->lock, flags);
791}
792
793/**
794 * ata_eh_done - EH action complete
795 * @ap: target ATA port
796 * @dev: target ATA dev for per-dev action (can be NULL)
797 * @action: action just completed
798 *
799 * Called right after performing EH actions to clear related bits
800 * in @ap->eh_context.
801 *
802 * LOCKING:
803 * None.
804 */
805static void ata_eh_done(struct ata_port *ap, struct ata_device *dev,
806 unsigned int action)
807{
808 /* if reset is complete, clear all reset actions & reset modifier */
809 if (action & ATA_EH_RESET_MASK) {
810 action |= ATA_EH_RESET_MASK;
811 ap->eh_context.i.flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
812 }
813
814 ata_eh_clear_action(dev, &ap->eh_context.i, action);
815}
816
817/**
818 * ata_err_string - convert err_mask to descriptive string
819 * @err_mask: error mask to convert to string
820 *
821 * Convert @err_mask to descriptive string. Errors are
822 * prioritized according to severity and only the most severe
823 * error is reported.
824 *
825 * LOCKING:
826 * None.
827 *
828 * RETURNS:
829 * Descriptive string for @err_mask
830 */
831static const char * ata_err_string(unsigned int err_mask)
832{
833 if (err_mask & AC_ERR_HOST_BUS)
834 return "host bus error";
835 if (err_mask & AC_ERR_ATA_BUS)
836 return "ATA bus error";
837 if (err_mask & AC_ERR_TIMEOUT)
838 return "timeout";
839 if (err_mask & AC_ERR_HSM)
840 return "HSM violation";
841 if (err_mask & AC_ERR_SYSTEM)
842 return "internal error";
843 if (err_mask & AC_ERR_MEDIA)
844 return "media error";
845 if (err_mask & AC_ERR_INVALID)
846 return "invalid argument";
847 if (err_mask & AC_ERR_DEV)
848 return "device error";
849 return "unknown error";
850}
851
852/**
853 * ata_read_log_page - read a specific log page
854 * @dev: target device
855 * @page: page to read
856 * @buf: buffer to store read page
857 * @sectors: number of sectors to read
858 *
859 * Read log page using READ_LOG_EXT command.
860 *
861 * LOCKING:
862 * Kernel thread context (may sleep).
863 *
864 * RETURNS:
865 * 0 on success, AC_ERR_* mask otherwise.
866 */
867static unsigned int ata_read_log_page(struct ata_device *dev,
868 u8 page, void *buf, unsigned int sectors)
869{
870 struct ata_taskfile tf;
871 unsigned int err_mask;
872
873 DPRINTK("read log page - page %d\n", page);
874
875 ata_tf_init(dev, &tf);
876 tf.command = ATA_CMD_READ_LOG_EXT;
877 tf.lbal = page;
878 tf.nsect = sectors;
879 tf.hob_nsect = sectors >> 8;
880 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
881 tf.protocol = ATA_PROT_PIO;
882
883 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
884 buf, sectors * ATA_SECT_SIZE);
885
886 DPRINTK("EXIT, err_mask=%x\n", err_mask);
887 return err_mask;
888}
889
890/**
891 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
892 * @dev: Device to read log page 10h from
893 * @tag: Resulting tag of the failed command
894 * @tf: Resulting taskfile registers of the failed command
895 *
896 * Read log page 10h to obtain NCQ error details and clear error
897 * condition.
898 *
899 * LOCKING:
900 * Kernel thread context (may sleep).
901 *
902 * RETURNS:
903 * 0 on success, -errno otherwise.
904 */
905static int ata_eh_read_log_10h(struct ata_device *dev,
906 int *tag, struct ata_taskfile *tf)
907{
908 u8 *buf = dev->ap->sector_buf;
909 unsigned int err_mask;
910 u8 csum;
911 int i;
912
913 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
914 if (err_mask)
915 return -EIO;
916
917 csum = 0;
918 for (i = 0; i < ATA_SECT_SIZE; i++)
919 csum += buf[i];
920 if (csum)
921 ata_dev_printk(dev, KERN_WARNING,
922 "invalid checksum 0x%x on log page 10h\n", csum);
923
924 if (buf[0] & 0x80)
925 return -ENOENT;
926
927 *tag = buf[0] & 0x1f;
928
929 tf->command = buf[2];
930 tf->feature = buf[3];
931 tf->lbal = buf[4];
932 tf->lbam = buf[5];
933 tf->lbah = buf[6];
934 tf->device = buf[7];
935 tf->hob_lbal = buf[8];
936 tf->hob_lbam = buf[9];
937 tf->hob_lbah = buf[10];
938 tf->nsect = buf[12];
939 tf->hob_nsect = buf[13];
940
941 return 0;
942}
943
944/**
945 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
946 * @dev: device to perform REQUEST_SENSE to
947 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
948 *
949 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
950 * SENSE. This function is EH helper.
951 *
952 * LOCKING:
953 * Kernel thread context (may sleep).
954 *
955 * RETURNS:
956 * 0 on success, AC_ERR_* mask on failure
957 */
958static unsigned int atapi_eh_request_sense(struct ata_device *dev,
959 unsigned char *sense_buf)
960{
961 struct ata_port *ap = dev->ap;
962 struct ata_taskfile tf;
963 u8 cdb[ATAPI_CDB_LEN];
964
965 DPRINTK("ATAPI request sense\n");
966
967 ata_tf_init(dev, &tf);
968
969 /* FIXME: is this needed? */
970 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
971
972 /* XXX: why tf_read here? */
973 ap->ops->tf_read(ap, &tf);
974
975 /* fill these in, for the case where they are -not- overwritten */
976 sense_buf[0] = 0x70;
977 sense_buf[2] = tf.feature >> 4;
978
979 memset(cdb, 0, ATAPI_CDB_LEN);
980 cdb[0] = REQUEST_SENSE;
981 cdb[4] = SCSI_SENSE_BUFFERSIZE;
982
983 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
984 tf.command = ATA_CMD_PACKET;
985
986 /* is it pointless to prefer PIO for "safety reasons"? */
987 if (ap->flags & ATA_FLAG_PIO_DMA) {
988 tf.protocol = ATA_PROT_ATAPI_DMA;
989 tf.feature |= ATAPI_PKT_DMA;
990 } else {
991 tf.protocol = ATA_PROT_ATAPI;
992 tf.lbam = (8 * 1024) & 0xff;
993 tf.lbah = (8 * 1024) >> 8;
994 }
995
996 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
997 sense_buf, SCSI_SENSE_BUFFERSIZE);
998}
999
1000/**
1001 * ata_eh_analyze_serror - analyze SError for a failed port
1002 * @ap: ATA port to analyze SError for
1003 *
1004 * Analyze SError if available and further determine cause of
1005 * failure.
1006 *
1007 * LOCKING:
1008 * None.
1009 */
1010static void ata_eh_analyze_serror(struct ata_port *ap)
1011{
1012 struct ata_eh_context *ehc = &ap->eh_context;
1013 u32 serror = ehc->i.serror;
1014 unsigned int err_mask = 0, action = 0;
1015
1016 if (serror & SERR_PERSISTENT) {
1017 err_mask |= AC_ERR_ATA_BUS;
1018 action |= ATA_EH_HARDRESET;
1019 }
1020 if (serror &
1021 (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
1022 err_mask |= AC_ERR_ATA_BUS;
1023 action |= ATA_EH_SOFTRESET;
1024 }
1025 if (serror & SERR_PROTOCOL) {
1026 err_mask |= AC_ERR_HSM;
1027 action |= ATA_EH_SOFTRESET;
1028 }
1029 if (serror & SERR_INTERNAL) {
1030 err_mask |= AC_ERR_SYSTEM;
1031 action |= ATA_EH_SOFTRESET;
1032 }
1033 if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG))
1034 ata_ehi_hotplugged(&ehc->i);
1035
1036 ehc->i.err_mask |= err_mask;
1037 ehc->i.action |= action;
1038}
1039
1040/**
1041 * ata_eh_analyze_ncq_error - analyze NCQ error
1042 * @ap: ATA port to analyze NCQ error for
1043 *
1044 * Read log page 10h, determine the offending qc and acquire
1045 * error status TF. For NCQ device errors, all LLDDs have to do
1046 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1047 * care of the rest.
1048 *
1049 * LOCKING:
1050 * Kernel thread context (may sleep).
1051 */
1052static void ata_eh_analyze_ncq_error(struct ata_port *ap)
1053{
1054 struct ata_eh_context *ehc = &ap->eh_context;
1055 struct ata_device *dev = ap->device;
1056 struct ata_queued_cmd *qc;
1057 struct ata_taskfile tf;
1058 int tag, rc;
1059
1060 /* if frozen, we can't do much */
1061 if (ap->pflags & ATA_PFLAG_FROZEN)
1062 return;
1063
1064 /* is it NCQ device error? */
1065 if (!ap->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1066 return;
1067
1068 /* has LLDD analyzed already? */
1069 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1070 qc = __ata_qc_from_tag(ap, tag);
1071
1072 if (!(qc->flags & ATA_QCFLAG_FAILED))
1073 continue;
1074
1075 if (qc->err_mask)
1076 return;
1077 }
1078
1079 /* okay, this error is ours */
1080 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1081 if (rc) {
1082 ata_port_printk(ap, KERN_ERR, "failed to read log page 10h "
1083 "(errno=%d)\n", rc);
1084 return;
1085 }
1086
1087 if (!(ap->sactive & (1 << tag))) {
1088 ata_port_printk(ap, KERN_ERR, "log page 10h reported "
1089 "inactive tag %d\n", tag);
1090 return;
1091 }
1092
1093 /* we've got the perpetrator, condemn it */
1094 qc = __ata_qc_from_tag(ap, tag);
1095 memcpy(&qc->result_tf, &tf, sizeof(tf));
1096 qc->err_mask |= AC_ERR_DEV;
1097 ehc->i.err_mask &= ~AC_ERR_DEV;
1098}
1099
1100/**
1101 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1102 * @qc: qc to analyze
1103 * @tf: Taskfile registers to analyze
1104 *
1105 * Analyze taskfile of @qc and further determine cause of
1106 * failure. This function also requests ATAPI sense data if
1107 * avaliable.
1108 *
1109 * LOCKING:
1110 * Kernel thread context (may sleep).
1111 *
1112 * RETURNS:
1113 * Determined recovery action
1114 */
1115static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1116 const struct ata_taskfile *tf)
1117{
1118 unsigned int tmp, action = 0;
1119 u8 stat = tf->command, err = tf->feature;
1120
1121 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1122 qc->err_mask |= AC_ERR_HSM;
1123 return ATA_EH_SOFTRESET;
1124 }
1125
1126 if (!(qc->err_mask & AC_ERR_DEV))
1127 return 0;
1128
1129 switch (qc->dev->class) {
1130 case ATA_DEV_ATA:
1131 if (err & ATA_ICRC)
1132 qc->err_mask |= AC_ERR_ATA_BUS;
1133 if (err & ATA_UNC)
1134 qc->err_mask |= AC_ERR_MEDIA;
1135 if (err & ATA_IDNF)
1136 qc->err_mask |= AC_ERR_INVALID;
1137 break;
1138
1139 case ATA_DEV_ATAPI:
1140 tmp = atapi_eh_request_sense(qc->dev,
1141 qc->scsicmd->sense_buffer);
1142 if (!tmp) {
1143 /* ATA_QCFLAG_SENSE_VALID is used to tell
1144 * atapi_qc_complete() that sense data is
1145 * already valid.
1146 *
1147 * TODO: interpret sense data and set
1148 * appropriate err_mask.
1149 */
1150 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1151 } else
1152 qc->err_mask |= tmp;
1153 }
1154
1155 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1156 action |= ATA_EH_SOFTRESET;
1157
1158 return action;
1159}
1160
1161static int ata_eh_categorize_ering_entry(struct ata_ering_entry *ent)
1162{
1163 if (ent->err_mask & (AC_ERR_ATA_BUS | AC_ERR_TIMEOUT))
1164 return 1;
1165
1166 if (ent->is_io) {
1167 if (ent->err_mask & AC_ERR_HSM)
1168 return 1;
1169 if ((ent->err_mask &
1170 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1171 return 2;
1172 }
1173
1174 return 0;
1175}
1176
1177struct speed_down_needed_arg {
1178 u64 since;
1179 int nr_errors[3];
1180};
1181
1182static int speed_down_needed_cb(struct ata_ering_entry *ent, void *void_arg)
1183{
1184 struct speed_down_needed_arg *arg = void_arg;
1185
1186 if (ent->timestamp < arg->since)
1187 return -1;
1188
1189 arg->nr_errors[ata_eh_categorize_ering_entry(ent)]++;
1190 return 0;
1191}
1192
1193/**
1194 * ata_eh_speed_down_needed - Determine wheter speed down is necessary
1195 * @dev: Device of interest
1196 *
1197 * This function examines error ring of @dev and determines
1198 * whether speed down is necessary. Speed down is necessary if
1199 * there have been more than 3 of Cat-1 errors or 10 of Cat-2
1200 * errors during last 15 minutes.
1201 *
1202 * Cat-1 errors are ATA_BUS, TIMEOUT for any command and HSM
1203 * violation for known supported commands.
1204 *
1205 * Cat-2 errors are unclassified DEV error for known supported
1206 * command.
1207 *
1208 * LOCKING:
1209 * Inherited from caller.
1210 *
1211 * RETURNS:
1212 * 1 if speed down is necessary, 0 otherwise
1213 */
1214static int ata_eh_speed_down_needed(struct ata_device *dev)
1215{
1216 const u64 interval = 15LLU * 60 * HZ;
1217 static const int err_limits[3] = { -1, 3, 10 };
1218 struct speed_down_needed_arg arg;
1219 struct ata_ering_entry *ent;
1220 int err_cat;
1221 u64 j64;
1222
1223 ent = ata_ering_top(&dev->ering);
1224 if (!ent)
1225 return 0;
1226
1227 err_cat = ata_eh_categorize_ering_entry(ent);
1228 if (err_cat == 0)
1229 return 0;
1230
1231 memset(&arg, 0, sizeof(arg));
1232
1233 j64 = get_jiffies_64();
1234 if (j64 >= interval)
1235 arg.since = j64 - interval;
1236 else
1237 arg.since = 0;
1238
1239 ata_ering_map(&dev->ering, speed_down_needed_cb, &arg);
1240
1241 return arg.nr_errors[err_cat] > err_limits[err_cat];
1242}
1243
1244/**
1245 * ata_eh_speed_down - record error and speed down if necessary
1246 * @dev: Failed device
1247 * @is_io: Did the device fail during normal IO?
1248 * @err_mask: err_mask of the error
1249 *
1250 * Record error and examine error history to determine whether
1251 * adjusting transmission speed is necessary. It also sets
1252 * transmission limits appropriately if such adjustment is
1253 * necessary.
1254 *
1255 * LOCKING:
1256 * Kernel thread context (may sleep).
1257 *
1258 * RETURNS:
1259 * 0 on success, -errno otherwise
1260 */
1261static int ata_eh_speed_down(struct ata_device *dev, int is_io,
1262 unsigned int err_mask)
1263{
1264 if (!err_mask)
1265 return 0;
1266
1267 /* record error and determine whether speed down is necessary */
1268 ata_ering_record(&dev->ering, is_io, err_mask);
1269
1270 if (!ata_eh_speed_down_needed(dev))
1271 return 0;
1272
1273 /* speed down SATA link speed if possible */
1274 if (sata_down_spd_limit(dev->ap) == 0)
1275 return ATA_EH_HARDRESET;
1276
1277 /* lower transfer mode */
1278 if (ata_down_xfermask_limit(dev, 0) == 0)
1279 return ATA_EH_SOFTRESET;
1280
1281 ata_dev_printk(dev, KERN_ERR,
1282 "speed down requested but no transfer mode left\n");
1283 return 0;
1284}
1285
1286/**
1287 * ata_eh_autopsy - analyze error and determine recovery action
1288 * @ap: ATA port to perform autopsy on
1289 *
1290 * Analyze why @ap failed and determine which recovery action is
1291 * needed. This function also sets more detailed AC_ERR_* values
1292 * and fills sense data for ATAPI CHECK SENSE.
1293 *
1294 * LOCKING:
1295 * Kernel thread context (may sleep).
1296 */
1297static void ata_eh_autopsy(struct ata_port *ap)
1298{
1299 struct ata_eh_context *ehc = &ap->eh_context;
1300 unsigned int all_err_mask = 0;
1301 int tag, is_io = 0;
1302 u32 serror;
1303 int rc;
1304
1305 DPRINTK("ENTER\n");
1306
1307 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1308 return;
1309
1310 /* obtain and analyze SError */
1311 rc = sata_scr_read(ap, SCR_ERROR, &serror);
1312 if (rc == 0) {
1313 ehc->i.serror |= serror;
1314 ata_eh_analyze_serror(ap);
1315 } else if (rc != -EOPNOTSUPP)
1316 ehc->i.action |= ATA_EH_HARDRESET;
1317
1318 /* analyze NCQ failure */
1319 ata_eh_analyze_ncq_error(ap);
1320
1321 /* any real error trumps AC_ERR_OTHER */
1322 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1323 ehc->i.err_mask &= ~AC_ERR_OTHER;
1324
1325 all_err_mask |= ehc->i.err_mask;
1326
1327 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1328 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1329
1330 if (!(qc->flags & ATA_QCFLAG_FAILED))
1331 continue;
1332
1333 /* inherit upper level err_mask */
1334 qc->err_mask |= ehc->i.err_mask;
1335
1336 /* analyze TF */
1337 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1338
1339 /* DEV errors are probably spurious in case of ATA_BUS error */
1340 if (qc->err_mask & AC_ERR_ATA_BUS)
1341 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1342 AC_ERR_INVALID);
1343
1344 /* any real error trumps unknown error */
1345 if (qc->err_mask & ~AC_ERR_OTHER)
1346 qc->err_mask &= ~AC_ERR_OTHER;
1347
1348 /* SENSE_VALID trumps dev/unknown error and revalidation */
1349 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1350 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1351 ehc->i.action &= ~ATA_EH_REVALIDATE;
1352 }
1353
1354 /* accumulate error info */
1355 ehc->i.dev = qc->dev;
1356 all_err_mask |= qc->err_mask;
1357 if (qc->flags & ATA_QCFLAG_IO)
1358 is_io = 1;
1359 }
1360
1361 /* enforce default EH actions */
1362 if (ap->pflags & ATA_PFLAG_FROZEN ||
1363 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
1364 ehc->i.action |= ATA_EH_SOFTRESET;
1365 else if (all_err_mask)
1366 ehc->i.action |= ATA_EH_REVALIDATE;
1367
1368 /* if we have offending qcs and the associated failed device */
1369 if (ehc->i.dev) {
1370 /* speed down */
1371 ehc->i.action |= ata_eh_speed_down(ehc->i.dev, is_io,
1372 all_err_mask);
1373
1374 /* perform per-dev EH action only on the offending device */
1375 ehc->i.dev_action[ehc->i.dev->devno] |=
1376 ehc->i.action & ATA_EH_PERDEV_MASK;
1377 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
1378 }
1379
1380 DPRINTK("EXIT\n");
1381}
1382
1383/**
1384 * ata_eh_report - report error handling to user
1385 * @ap: ATA port EH is going on
1386 *
1387 * Report EH to user.
1388 *
1389 * LOCKING:
1390 * None.
1391 */
1392static void ata_eh_report(struct ata_port *ap)
1393{
1394 struct ata_eh_context *ehc = &ap->eh_context;
1395 const char *frozen, *desc;
1396 int tag, nr_failed = 0;
1397
1398 desc = NULL;
1399 if (ehc->i.desc[0] != '\0')
1400 desc = ehc->i.desc;
1401
1402 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1403 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1404
1405 if (!(qc->flags & ATA_QCFLAG_FAILED))
1406 continue;
1407 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1408 continue;
1409
1410 nr_failed++;
1411 }
1412
1413 if (!nr_failed && !ehc->i.err_mask)
1414 return;
1415
1416 frozen = "";
1417 if (ap->pflags & ATA_PFLAG_FROZEN)
1418 frozen = " frozen";
1419
1420 if (ehc->i.dev) {
1421 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1422 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1423 ehc->i.err_mask, ap->sactive, ehc->i.serror,
1424 ehc->i.action, frozen);
1425 if (desc)
1426 ata_dev_printk(ehc->i.dev, KERN_ERR, "(%s)\n", desc);
1427 } else {
1428 ata_port_printk(ap, KERN_ERR, "exception Emask 0x%x "
1429 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1430 ehc->i.err_mask, ap->sactive, ehc->i.serror,
1431 ehc->i.action, frozen);
1432 if (desc)
1433 ata_port_printk(ap, KERN_ERR, "(%s)\n", desc);
1434 }
1435
1436 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1437 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1438
1439 if (!(qc->flags & ATA_QCFLAG_FAILED) || !qc->err_mask)
1440 continue;
1441
1442 ata_dev_printk(qc->dev, KERN_ERR, "tag %d cmd 0x%x "
1443 "Emask 0x%x stat 0x%x err 0x%x (%s)\n",
1444 qc->tag, qc->tf.command, qc->err_mask,
1445 qc->result_tf.command, qc->result_tf.feature,
1446 ata_err_string(qc->err_mask));
1447 }
1448}
1449
1450static int ata_do_reset(struct ata_port *ap, ata_reset_fn_t reset,
1451 unsigned int *classes)
1452{
1453 int i, rc;
1454
1455 for (i = 0; i < ATA_MAX_DEVICES; i++)
1456 classes[i] = ATA_DEV_UNKNOWN;
1457
1458 rc = reset(ap, classes);
1459 if (rc)
1460 return rc;
1461
1462 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
1463 * is complete and convert all ATA_DEV_UNKNOWN to
1464 * ATA_DEV_NONE.
1465 */
1466 for (i = 0; i < ATA_MAX_DEVICES; i++)
1467 if (classes[i] != ATA_DEV_UNKNOWN)
1468 break;
1469
1470 if (i < ATA_MAX_DEVICES)
1471 for (i = 0; i < ATA_MAX_DEVICES; i++)
1472 if (classes[i] == ATA_DEV_UNKNOWN)
1473 classes[i] = ATA_DEV_NONE;
1474
1475 return 0;
1476}
1477
1478static int ata_eh_followup_srst_needed(int rc, int classify,
1479 const unsigned int *classes)
1480{
1481 if (rc == -EAGAIN)
1482 return 1;
1483 if (rc != 0)
1484 return 0;
1485 if (classify && classes[0] == ATA_DEV_UNKNOWN)
1486 return 1;
1487 return 0;
1488}
1489
1490static int ata_eh_reset(struct ata_port *ap, int classify,
1491 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
1492 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
1493{
1494 struct ata_eh_context *ehc = &ap->eh_context;
1495 unsigned int *classes = ehc->classes;
1496 int tries = ATA_EH_RESET_TRIES;
1497 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
1498 unsigned int action;
1499 ata_reset_fn_t reset;
1500 int i, did_followup_srst, rc;
1501
1502 /* about to reset */
1503 ata_eh_about_to_do(ap, NULL, ehc->i.action & ATA_EH_RESET_MASK);
1504
1505 /* Determine which reset to use and record in ehc->i.action.
1506 * prereset() may examine and modify it.
1507 */
1508 action = ehc->i.action;
1509 ehc->i.action &= ~ATA_EH_RESET_MASK;
1510 if (softreset && (!hardreset || (!sata_set_spd_needed(ap) &&
1511 !(action & ATA_EH_HARDRESET))))
1512 ehc->i.action |= ATA_EH_SOFTRESET;
1513 else
1514 ehc->i.action |= ATA_EH_HARDRESET;
1515
1516 if (prereset) {
1517 rc = prereset(ap);
1518 if (rc) {
1519 ata_port_printk(ap, KERN_ERR,
1520 "prereset failed (errno=%d)\n", rc);
1521 return rc;
1522 }
1523 }
1524
1525 /* prereset() might have modified ehc->i.action */
1526 if (ehc->i.action & ATA_EH_HARDRESET)
1527 reset = hardreset;
1528 else if (ehc->i.action & ATA_EH_SOFTRESET)
1529 reset = softreset;
1530 else {
1531 /* prereset told us not to reset, bang classes and return */
1532 for (i = 0; i < ATA_MAX_DEVICES; i++)
1533 classes[i] = ATA_DEV_NONE;
1534 return 0;
1535 }
1536
1537 /* did prereset() screw up? if so, fix up to avoid oopsing */
1538 if (!reset) {
1539 ata_port_printk(ap, KERN_ERR, "BUG: prereset() requested "
1540 "invalid reset type\n");
1541 if (softreset)
1542 reset = softreset;
1543 else
1544 reset = hardreset;
1545 }
1546
1547 retry:
1548 /* shut up during boot probing */
1549 if (verbose)
1550 ata_port_printk(ap, KERN_INFO, "%s resetting port\n",
1551 reset == softreset ? "soft" : "hard");
1552
1553 /* mark that this EH session started with reset */
1554 ehc->i.flags |= ATA_EHI_DID_RESET;
1555
1556 rc = ata_do_reset(ap, reset, classes);
1557
1558 did_followup_srst = 0;
1559 if (reset == hardreset &&
1560 ata_eh_followup_srst_needed(rc, classify, classes)) {
1561 /* okay, let's do follow-up softreset */
1562 did_followup_srst = 1;
1563 reset = softreset;
1564
1565 if (!reset) {
1566 ata_port_printk(ap, KERN_ERR,
1567 "follow-up softreset required "
1568 "but no softreset avaliable\n");
1569 return -EINVAL;
1570 }
1571
1572 ata_eh_about_to_do(ap, NULL, ATA_EH_RESET_MASK);
1573 rc = ata_do_reset(ap, reset, classes);
1574
1575 if (rc == 0 && classify &&
1576 classes[0] == ATA_DEV_UNKNOWN) {
1577 ata_port_printk(ap, KERN_ERR,
1578 "classification failed\n");
1579 return -EINVAL;
1580 }
1581 }
1582
1583 if (rc && --tries) {
1584 const char *type;
1585
1586 if (reset == softreset) {
1587 if (did_followup_srst)
1588 type = "follow-up soft";
1589 else
1590 type = "soft";
1591 } else
1592 type = "hard";
1593
1594 ata_port_printk(ap, KERN_WARNING,
1595 "%sreset failed, retrying in 5 secs\n", type);
1596 ssleep(5);
1597
1598 if (reset == hardreset)
1599 sata_down_spd_limit(ap);
1600 if (hardreset)
1601 reset = hardreset;
1602 goto retry;
1603 }
1604
1605 if (rc == 0) {
1606 /* After the reset, the device state is PIO 0 and the
1607 * controller state is undefined. Record the mode.
1608 */
1609 for (i = 0; i < ATA_MAX_DEVICES; i++)
1610 ap->device[i].pio_mode = XFER_PIO_0;
1611
1612 if (postreset)
1613 postreset(ap, classes);
1614
1615 /* reset successful, schedule revalidation */
1616 ata_eh_done(ap, NULL, ehc->i.action & ATA_EH_RESET_MASK);
1617 ehc->i.action |= ATA_EH_REVALIDATE;
1618 }
1619
1620 return rc;
1621}
1622
1623static int ata_eh_revalidate_and_attach(struct ata_port *ap,
1624 struct ata_device **r_failed_dev)
1625{
1626 struct ata_eh_context *ehc = &ap->eh_context;
1627 struct ata_device *dev;
1628 unsigned long flags;
1629 int i, rc = 0;
1630
1631 DPRINTK("ENTER\n");
1632
1633 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1634 unsigned int action;
1635
1636 dev = &ap->device[i];
1637 action = ata_eh_dev_action(dev);
1638
1639 if (action & ATA_EH_REVALIDATE && ata_dev_ready(dev)) {
1640 if (ata_port_offline(ap)) {
1641 rc = -EIO;
1642 break;
1643 }
1644
1645 ata_eh_about_to_do(ap, dev, ATA_EH_REVALIDATE);
1646 rc = ata_dev_revalidate(dev,
1647 ehc->i.flags & ATA_EHI_DID_RESET);
1648 if (rc)
1649 break;
1650
1651 ata_eh_done(ap, dev, ATA_EH_REVALIDATE);
1652
1653 /* schedule the scsi_rescan_device() here */
1654 queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
1655 } else if (dev->class == ATA_DEV_UNKNOWN &&
1656 ehc->tries[dev->devno] &&
1657 ata_class_enabled(ehc->classes[dev->devno])) {
1658 dev->class = ehc->classes[dev->devno];
1659
1660 rc = ata_dev_read_id(dev, &dev->class, 1, dev->id);
1661 if (rc == 0)
1662 rc = ata_dev_configure(dev, 1);
1663
1664 if (rc) {
1665 dev->class = ATA_DEV_UNKNOWN;
1666 break;
1667 }
1668
1669 spin_lock_irqsave(ap->lock, flags);
1670 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
1671 spin_unlock_irqrestore(ap->lock, flags);
1672 }
1673 }
1674
1675 if (rc)
1676 *r_failed_dev = dev;
1677
1678 DPRINTK("EXIT\n");
1679 return rc;
1680}
1681
1682/**
1683 * ata_eh_suspend - handle suspend EH action
1684 * @ap: target host port
1685 * @r_failed_dev: result parameter to indicate failing device
1686 *
1687 * Handle suspend EH action. Disk devices are spinned down and
1688 * other types of devices are just marked suspended. Once
1689 * suspended, no EH action to the device is allowed until it is
1690 * resumed.
1691 *
1692 * LOCKING:
1693 * Kernel thread context (may sleep).
1694 *
1695 * RETURNS:
1696 * 0 on success, -errno otherwise
1697 */
1698static int ata_eh_suspend(struct ata_port *ap, struct ata_device **r_failed_dev)
1699{
1700 struct ata_device *dev;
1701 int i, rc = 0;
1702
1703 DPRINTK("ENTER\n");
1704
1705 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1706 unsigned long flags;
1707 unsigned int action, err_mask;
1708
1709 dev = &ap->device[i];
1710 action = ata_eh_dev_action(dev);
1711
1712 if (!ata_dev_enabled(dev) || !(action & ATA_EH_SUSPEND))
1713 continue;
1714
1715 WARN_ON(dev->flags & ATA_DFLAG_SUSPENDED);
1716
1717 ata_eh_about_to_do(ap, dev, ATA_EH_SUSPEND);
1718
1719 if (dev->class == ATA_DEV_ATA && !(action & ATA_EH_PM_FREEZE)) {
1720 /* flush cache */
1721 rc = ata_flush_cache(dev);
1722 if (rc)
1723 break;
1724
1725 /* spin down */
1726 err_mask = ata_do_simple_cmd(dev, ATA_CMD_STANDBYNOW1);
1727 if (err_mask) {
1728 ata_dev_printk(dev, KERN_ERR, "failed to "
1729 "spin down (err_mask=0x%x)\n",
1730 err_mask);
1731 rc = -EIO;
1732 break;
1733 }
1734 }
1735
1736 spin_lock_irqsave(ap->lock, flags);
1737 dev->flags |= ATA_DFLAG_SUSPENDED;
1738 spin_unlock_irqrestore(ap->lock, flags);
1739
1740 ata_eh_done(ap, dev, ATA_EH_SUSPEND);
1741 }
1742
1743 if (rc)
1744 *r_failed_dev = dev;
1745
1746 DPRINTK("EXIT\n");
1747 return 0;
1748}
1749
1750/**
1751 * ata_eh_prep_resume - prep for resume EH action
1752 * @ap: target host port
1753 *
1754 * Clear SUSPENDED in preparation for scheduled resume actions.
1755 * This allows other parts of EH to access the devices being
1756 * resumed.
1757 *
1758 * LOCKING:
1759 * Kernel thread context (may sleep).
1760 */
1761static void ata_eh_prep_resume(struct ata_port *ap)
1762{
1763 struct ata_device *dev;
1764 unsigned long flags;
1765 int i;
1766
1767 DPRINTK("ENTER\n");
1768
1769 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1770 unsigned int action;
1771
1772 dev = &ap->device[i];
1773 action = ata_eh_dev_action(dev);
1774
1775 if (!ata_dev_enabled(dev) || !(action & ATA_EH_RESUME))
1776 continue;
1777
1778 spin_lock_irqsave(ap->lock, flags);
1779 dev->flags &= ~ATA_DFLAG_SUSPENDED;
1780 spin_unlock_irqrestore(ap->lock, flags);
1781 }
1782
1783 DPRINTK("EXIT\n");
1784}
1785
1786/**
1787 * ata_eh_resume - handle resume EH action
1788 * @ap: target host port
1789 * @r_failed_dev: result parameter to indicate failing device
1790 *
1791 * Handle resume EH action. Target devices are already reset and
1792 * revalidated. Spinning up is the only operation left.
1793 *
1794 * LOCKING:
1795 * Kernel thread context (may sleep).
1796 *
1797 * RETURNS:
1798 * 0 on success, -errno otherwise
1799 */
1800static int ata_eh_resume(struct ata_port *ap, struct ata_device **r_failed_dev)
1801{
1802 struct ata_device *dev;
1803 int i, rc = 0;
1804
1805 DPRINTK("ENTER\n");
1806
1807 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1808 unsigned int action, err_mask;
1809
1810 dev = &ap->device[i];
1811 action = ata_eh_dev_action(dev);
1812
1813 if (!ata_dev_enabled(dev) || !(action & ATA_EH_RESUME))
1814 continue;
1815
1816 ata_eh_about_to_do(ap, dev, ATA_EH_RESUME);
1817
1818 if (dev->class == ATA_DEV_ATA && !(action & ATA_EH_PM_FREEZE)) {
1819 err_mask = ata_do_simple_cmd(dev,
1820 ATA_CMD_IDLEIMMEDIATE);
1821 if (err_mask) {
1822 ata_dev_printk(dev, KERN_ERR, "failed to "
1823 "spin up (err_mask=0x%x)\n",
1824 err_mask);
1825 rc = -EIO;
1826 break;
1827 }
1828 }
1829
1830 ata_eh_done(ap, dev, ATA_EH_RESUME);
1831 }
1832
1833 if (rc)
1834 *r_failed_dev = dev;
1835
1836 DPRINTK("EXIT\n");
1837 return 0;
1838}
1839
1840static int ata_port_nr_enabled(struct ata_port *ap)
1841{
1842 int i, cnt = 0;
1843
1844 for (i = 0; i < ATA_MAX_DEVICES; i++)
1845 if (ata_dev_enabled(&ap->device[i]))
1846 cnt++;
1847 return cnt;
1848}
1849
1850static int ata_port_nr_vacant(struct ata_port *ap)
1851{
1852 int i, cnt = 0;
1853
1854 for (i = 0; i < ATA_MAX_DEVICES; i++)
1855 if (ap->device[i].class == ATA_DEV_UNKNOWN)
1856 cnt++;
1857 return cnt;
1858}
1859
1860static int ata_eh_skip_recovery(struct ata_port *ap)
1861{
1862 struct ata_eh_context *ehc = &ap->eh_context;
1863 int i;
1864
1865 /* skip if all possible devices are suspended */
1866 for (i = 0; i < ata_port_max_devices(ap); i++) {
1867 struct ata_device *dev = &ap->device[i];
1868
1869 if (!(dev->flags & ATA_DFLAG_SUSPENDED))
1870 break;
1871 }
1872
1873 if (i == ata_port_max_devices(ap))
1874 return 1;
1875
1876 /* thaw frozen port, resume link and recover failed devices */
1877 if ((ap->pflags & ATA_PFLAG_FROZEN) ||
1878 (ehc->i.flags & ATA_EHI_RESUME_LINK) || ata_port_nr_enabled(ap))
1879 return 0;
1880
1881 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
1882 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1883 struct ata_device *dev = &ap->device[i];
1884
1885 if (dev->class == ATA_DEV_UNKNOWN &&
1886 ehc->classes[dev->devno] != ATA_DEV_NONE)
1887 return 0;
1888 }
1889
1890 return 1;
1891}
1892
1893/**
1894 * ata_eh_recover - recover host port after error
1895 * @ap: host port to recover
1896 * @prereset: prereset method (can be NULL)
1897 * @softreset: softreset method (can be NULL)
1898 * @hardreset: hardreset method (can be NULL)
1899 * @postreset: postreset method (can be NULL)
1900 *
1901 * This is the alpha and omega, eum and yang, heart and soul of
1902 * libata exception handling. On entry, actions required to
1903 * recover the port and hotplug requests are recorded in
1904 * eh_context. This function executes all the operations with
1905 * appropriate retrials and fallbacks to resurrect failed
1906 * devices, detach goners and greet newcomers.
1907 *
1908 * LOCKING:
1909 * Kernel thread context (may sleep).
1910 *
1911 * RETURNS:
1912 * 0 on success, -errno on failure.
1913 */
1914static int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
1915 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
1916 ata_postreset_fn_t postreset)
1917{
1918 struct ata_eh_context *ehc = &ap->eh_context;
1919 struct ata_device *dev;
1920 int down_xfermask, i, rc;
1921
1922 DPRINTK("ENTER\n");
1923
1924 /* prep for recovery */
1925 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1926 dev = &ap->device[i];
1927
1928 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
1929
1930 /* process hotplug request */
1931 if (dev->flags & ATA_DFLAG_DETACH)
1932 ata_eh_detach_dev(dev);
1933
1934 if (!ata_dev_enabled(dev) &&
1935 ((ehc->i.probe_mask & (1 << dev->devno)) &&
1936 !(ehc->did_probe_mask & (1 << dev->devno)))) {
1937 ata_eh_detach_dev(dev);
1938 ata_dev_init(dev);
1939 ehc->did_probe_mask |= (1 << dev->devno);
1940 ehc->i.action |= ATA_EH_SOFTRESET;
1941 }
1942 }
1943
1944 retry:
1945 down_xfermask = 0;
1946 rc = 0;
1947
1948 /* if UNLOADING, finish immediately */
1949 if (ap->pflags & ATA_PFLAG_UNLOADING)
1950 goto out;
1951
1952 /* prep for resume */
1953 ata_eh_prep_resume(ap);
1954
1955 /* skip EH if possible. */
1956 if (ata_eh_skip_recovery(ap))
1957 ehc->i.action = 0;
1958
1959 for (i = 0; i < ATA_MAX_DEVICES; i++)
1960 ehc->classes[i] = ATA_DEV_UNKNOWN;
1961
1962 /* reset */
1963 if (ehc->i.action & ATA_EH_RESET_MASK) {
1964 ata_eh_freeze_port(ap);
1965
1966 rc = ata_eh_reset(ap, ata_port_nr_vacant(ap), prereset,
1967 softreset, hardreset, postreset);
1968 if (rc) {
1969 ata_port_printk(ap, KERN_ERR,
1970 "reset failed, giving up\n");
1971 goto out;
1972 }
1973
1974 ata_eh_thaw_port(ap);
1975 }
1976
1977 /* revalidate existing devices and attach new ones */
1978 rc = ata_eh_revalidate_and_attach(ap, &dev);
1979 if (rc)
1980 goto dev_fail;
1981
1982 /* resume devices */
1983 rc = ata_eh_resume(ap, &dev);
1984 if (rc)
1985 goto dev_fail;
1986
1987 /* configure transfer mode if the port has been reset */
1988 if (ehc->i.flags & ATA_EHI_DID_RESET) {
1989 rc = ata_set_mode(ap, &dev);
1990 if (rc) {
1991 down_xfermask = 1;
1992 goto dev_fail;
1993 }
1994 }
1995
1996 /* suspend devices */
1997 rc = ata_eh_suspend(ap, &dev);
1998 if (rc)
1999 goto dev_fail;
2000
2001 goto out;
2002
2003 dev_fail:
2004 switch (rc) {
2005 case -ENODEV:
2006 /* device missing, schedule probing */
2007 ehc->i.probe_mask |= (1 << dev->devno);
2008 case -EINVAL:
2009 ehc->tries[dev->devno] = 0;
2010 break;
2011 case -EIO:
2012 sata_down_spd_limit(ap);
2013 default:
2014 ehc->tries[dev->devno]--;
2015 if (down_xfermask &&
2016 ata_down_xfermask_limit(dev, ehc->tries[dev->devno] == 1))
2017 ehc->tries[dev->devno] = 0;
2018 }
2019
2020 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2021 /* disable device if it has used up all its chances */
2022 ata_dev_disable(dev);
2023
2024 /* detach if offline */
2025 if (ata_port_offline(ap))
2026 ata_eh_detach_dev(dev);
2027
2028 /* probe if requested */
2029 if ((ehc->i.probe_mask & (1 << dev->devno)) &&
2030 !(ehc->did_probe_mask & (1 << dev->devno))) {
2031 ata_eh_detach_dev(dev);
2032 ata_dev_init(dev);
2033
2034 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2035 ehc->did_probe_mask |= (1 << dev->devno);
2036 ehc->i.action |= ATA_EH_SOFTRESET;
2037 }
2038 } else {
2039 /* soft didn't work? be haaaaard */
2040 if (ehc->i.flags & ATA_EHI_DID_RESET)
2041 ehc->i.action |= ATA_EH_HARDRESET;
2042 else
2043 ehc->i.action |= ATA_EH_SOFTRESET;
2044 }
2045
2046 if (ata_port_nr_enabled(ap)) {
2047 ata_port_printk(ap, KERN_WARNING, "failed to recover some "
2048 "devices, retrying in 5 secs\n");
2049 ssleep(5);
2050 } else {
2051 /* no device left, repeat fast */
2052 msleep(500);
2053 }
2054
2055 goto retry;
2056
2057 out:
2058 if (rc) {
2059 for (i = 0; i < ATA_MAX_DEVICES; i++)
2060 ata_dev_disable(&ap->device[i]);
2061 }
2062
2063 DPRINTK("EXIT, rc=%d\n", rc);
2064 return rc;
2065}
2066
2067/**
2068 * ata_eh_finish - finish up EH
2069 * @ap: host port to finish EH for
2070 *
2071 * Recovery is complete. Clean up EH states and retry or finish
2072 * failed qcs.
2073 *
2074 * LOCKING:
2075 * None.
2076 */
2077static void ata_eh_finish(struct ata_port *ap)
2078{
2079 int tag;
2080
2081 /* retry or finish qcs */
2082 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2083 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2084
2085 if (!(qc->flags & ATA_QCFLAG_FAILED))
2086 continue;
2087
2088 if (qc->err_mask) {
2089 /* FIXME: Once EH migration is complete,
2090 * generate sense data in this function,
2091 * considering both err_mask and tf.
2092 */
2093 if (qc->err_mask & AC_ERR_INVALID)
2094 ata_eh_qc_complete(qc);
2095 else
2096 ata_eh_qc_retry(qc);
2097 } else {
2098 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2099 ata_eh_qc_complete(qc);
2100 } else {
2101 /* feed zero TF to sense generation */
2102 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2103 ata_eh_qc_retry(qc);
2104 }
2105 }
2106 }
2107}
2108
2109/**
2110 * ata_do_eh - do standard error handling
2111 * @ap: host port to handle error for
2112 * @prereset: prereset method (can be NULL)
2113 * @softreset: softreset method (can be NULL)
2114 * @hardreset: hardreset method (can be NULL)
2115 * @postreset: postreset method (can be NULL)
2116 *
2117 * Perform standard error handling sequence.
2118 *
2119 * LOCKING:
2120 * Kernel thread context (may sleep).
2121 */
2122void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2123 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2124 ata_postreset_fn_t postreset)
2125{
2126 ata_eh_autopsy(ap);
2127 ata_eh_report(ap);
2128 ata_eh_recover(ap, prereset, softreset, hardreset, postreset);
2129 ata_eh_finish(ap);
2130}
2131
2132/**
2133 * ata_eh_handle_port_suspend - perform port suspend operation
2134 * @ap: port to suspend
2135 *
2136 * Suspend @ap.
2137 *
2138 * LOCKING:
2139 * Kernel thread context (may sleep).
2140 */
2141static void ata_eh_handle_port_suspend(struct ata_port *ap)
2142{
2143 unsigned long flags;
2144 int rc = 0;
2145
2146 /* are we suspending? */
2147 spin_lock_irqsave(ap->lock, flags);
2148 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2149 ap->pm_mesg.event == PM_EVENT_ON) {
2150 spin_unlock_irqrestore(ap->lock, flags);
2151 return;
2152 }
2153 spin_unlock_irqrestore(ap->lock, flags);
2154
2155 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
2156
2157 /* suspend */
2158 ata_eh_freeze_port(ap);
2159
2160 if (ap->ops->port_suspend)
2161 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
2162
2163 /* report result */
2164 spin_lock_irqsave(ap->lock, flags);
2165
2166 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
2167 if (rc == 0)
2168 ap->pflags |= ATA_PFLAG_SUSPENDED;
2169 else
2170 ata_port_schedule_eh(ap);
2171
2172 if (ap->pm_result) {
2173 *ap->pm_result = rc;
2174 ap->pm_result = NULL;
2175 }
2176
2177 spin_unlock_irqrestore(ap->lock, flags);
2178
2179 return;
2180}
2181
2182/**
2183 * ata_eh_handle_port_resume - perform port resume operation
2184 * @ap: port to resume
2185 *
2186 * Resume @ap.
2187 *
2188 * This function also waits upto one second until all devices
2189 * hanging off this port requests resume EH action. This is to
2190 * prevent invoking EH and thus reset multiple times on resume.
2191 *
2192 * On DPM resume, where some of devices might not be resumed
2193 * together, this may delay port resume upto one second, but such
2194 * DPM resumes are rare and 1 sec delay isn't too bad.
2195 *
2196 * LOCKING:
2197 * Kernel thread context (may sleep).
2198 */
2199static void ata_eh_handle_port_resume(struct ata_port *ap)
2200{
2201 unsigned long timeout;
2202 unsigned long flags;
2203 int i, rc = 0;
2204
2205 /* are we resuming? */
2206 spin_lock_irqsave(ap->lock, flags);
2207 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2208 ap->pm_mesg.event != PM_EVENT_ON) {
2209 spin_unlock_irqrestore(ap->lock, flags);
2210 return;
2211 }
2212 spin_unlock_irqrestore(ap->lock, flags);
2213
2214 /* spurious? */
2215 if (!(ap->pflags & ATA_PFLAG_SUSPENDED))
2216 goto done;
2217
2218 if (ap->ops->port_resume)
2219 rc = ap->ops->port_resume(ap);
2220
2221 /* give devices time to request EH */
2222 timeout = jiffies + HZ; /* 1s max */
2223 while (1) {
2224 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2225 struct ata_device *dev = &ap->device[i];
2226 unsigned int action = ata_eh_dev_action(dev);
2227
2228 if ((dev->flags & ATA_DFLAG_SUSPENDED) &&
2229 !(action & ATA_EH_RESUME))
2230 break;
2231 }
2232
2233 if (i == ATA_MAX_DEVICES || time_after(jiffies, timeout))
2234 break;
2235 msleep(10);
2236 }
2237
2238 done:
2239 spin_lock_irqsave(ap->lock, flags);
2240 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
2241 if (ap->pm_result) {
2242 *ap->pm_result = rc;
2243 ap->pm_result = NULL;
2244 }
2245 spin_unlock_irqrestore(ap->lock, flags);
2246}