[PATCH] libata: shift host flag constants
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / scsi / libata-eh.c
CommitLineData
ece1d636
TH
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>
f8bbfc24 42#include "scsi_transport_api.h"
ece1d636
TH
43
44#include <linux/libata.h>
45
46#include "libata.h"
47
ad9e2762
TH
48static void __ata_port_freeze(struct ata_port *ap);
49
0c247c55
TH
50static void ata_ering_record(struct ata_ering *ering, int is_io,
51 unsigned int err_mask)
52{
53 struct ata_ering_entry *ent;
54
55 WARN_ON(!err_mask);
56
57 ering->cursor++;
58 ering->cursor %= ATA_ERING_SIZE;
59
60 ent = &ering->ring[ering->cursor];
61 ent->is_io = is_io;
62 ent->err_mask = err_mask;
63 ent->timestamp = get_jiffies_64();
64}
65
66static struct ata_ering_entry * ata_ering_top(struct ata_ering *ering)
67{
68 struct ata_ering_entry *ent = &ering->ring[ering->cursor];
69 if (!ent->err_mask)
70 return NULL;
71 return ent;
72}
73
74static int ata_ering_map(struct ata_ering *ering,
75 int (*map_fn)(struct ata_ering_entry *, void *),
76 void *arg)
77{
78 int idx, rc = 0;
79 struct ata_ering_entry *ent;
80
81 idx = ering->cursor;
82 do {
83 ent = &ering->ring[idx];
84 if (!ent->err_mask)
85 break;
86 rc = map_fn(ent, arg);
87 if (rc)
88 break;
89 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
90 } while (idx != ering->cursor);
91
92 return rc;
93}
94
ece1d636
TH
95/**
96 * ata_scsi_timed_out - SCSI layer time out callback
97 * @cmd: timed out SCSI command
98 *
99 * Handles SCSI layer timeout. We race with normal completion of
100 * the qc for @cmd. If the qc is already gone, we lose and let
101 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
102 * timed out and EH should be invoked. Prevent ata_qc_complete()
103 * from finishing it by setting EH_SCHEDULED and return
104 * EH_NOT_HANDLED.
105 *
ad9e2762
TH
106 * TODO: kill this function once old EH is gone.
107 *
ece1d636
TH
108 * LOCKING:
109 * Called from timer context
110 *
111 * RETURNS:
112 * EH_HANDLED or EH_NOT_HANDLED
113 */
114enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
115{
116 struct Scsi_Host *host = cmd->device->host;
35bb94b1 117 struct ata_port *ap = ata_shost_to_port(host);
ece1d636
TH
118 unsigned long flags;
119 struct ata_queued_cmd *qc;
ad9e2762 120 enum scsi_eh_timer_return ret;
ece1d636
TH
121
122 DPRINTK("ENTER\n");
123
ad9e2762
TH
124 if (ap->ops->error_handler) {
125 ret = EH_NOT_HANDLED;
126 goto out;
127 }
128
129 ret = EH_HANDLED;
ece1d636
TH
130 spin_lock_irqsave(&ap->host_set->lock, flags);
131 qc = ata_qc_from_tag(ap, ap->active_tag);
132 if (qc) {
133 WARN_ON(qc->scsicmd != cmd);
134 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
135 qc->err_mask |= AC_ERR_TIMEOUT;
136 ret = EH_NOT_HANDLED;
137 }
138 spin_unlock_irqrestore(&ap->host_set->lock, flags);
139
ad9e2762 140 out:
ece1d636
TH
141 DPRINTK("EXIT, ret=%d\n", ret);
142 return ret;
143}
144
145/**
146 * ata_scsi_error - SCSI layer error handler callback
147 * @host: SCSI host on which error occurred
148 *
149 * Handles SCSI-layer-thrown error events.
150 *
151 * LOCKING:
152 * Inherited from SCSI layer (none, can sleep)
153 *
154 * RETURNS:
155 * Zero.
156 */
381544bb 157void ata_scsi_error(struct Scsi_Host *host)
ece1d636 158{
35bb94b1 159 struct ata_port *ap = ata_shost_to_port(host);
ad9e2762
TH
160 spinlock_t *hs_lock = &ap->host_set->lock;
161 int i, repeat_cnt = ATA_EH_MAX_REPEAT;
162 unsigned long flags;
ece1d636
TH
163
164 DPRINTK("ENTER\n");
165
ad9e2762 166 /* synchronize with port task */
ece1d636
TH
167 ata_port_flush_task(ap);
168
ad9e2762
TH
169 /* synchronize with host_set lock and sort out timeouts */
170
171 /* For new EH, all qcs are finished in one of three ways -
172 * normal completion, error completion, and SCSI timeout.
173 * Both cmpletions can race against SCSI timeout. When normal
174 * completion wins, the qc never reaches EH. When error
175 * completion wins, the qc has ATA_QCFLAG_FAILED set.
176 *
177 * When SCSI timeout wins, things are a bit more complex.
178 * Normal or error completion can occur after the timeout but
179 * before this point. In such cases, both types of
180 * completions are honored. A scmd is determined to have
181 * timed out iff its associated qc is active and not failed.
182 */
183 if (ap->ops->error_handler) {
184 struct scsi_cmnd *scmd, *tmp;
185 int nr_timedout = 0;
186
187 spin_lock_irqsave(hs_lock, flags);
188
189 list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
190 struct ata_queued_cmd *qc;
191
192 for (i = 0; i < ATA_MAX_QUEUE; i++) {
193 qc = __ata_qc_from_tag(ap, i);
194 if (qc->flags & ATA_QCFLAG_ACTIVE &&
195 qc->scsicmd == scmd)
196 break;
197 }
198
199 if (i < ATA_MAX_QUEUE) {
200 /* the scmd has an associated qc */
201 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
202 /* which hasn't failed yet, timeout */
203 qc->err_mask |= AC_ERR_TIMEOUT;
204 qc->flags |= ATA_QCFLAG_FAILED;
205 nr_timedout++;
206 }
207 } else {
208 /* Normal completion occurred after
209 * SCSI timeout but before this point.
210 * Successfully complete it.
211 */
212 scmd->retries = scmd->allowed;
213 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
214 }
215 }
216
217 /* If we have timed out qcs. They belong to EH from
218 * this point but the state of the controller is
219 * unknown. Freeze the port to make sure the IRQ
220 * handler doesn't diddle with those qcs. This must
221 * be done atomically w.r.t. setting QCFLAG_FAILED.
222 */
223 if (nr_timedout)
224 __ata_port_freeze(ap);
225
226 spin_unlock_irqrestore(hs_lock, flags);
227 } else
228 spin_unlock_wait(hs_lock);
229
230 repeat:
231 /* invoke error handler */
232 if (ap->ops->error_handler) {
f3e81b19 233 /* fetch & clear EH info */
ad9e2762 234 spin_lock_irqsave(hs_lock, flags);
f3e81b19
TH
235
236 memset(&ap->eh_context, 0, sizeof(ap->eh_context));
237 ap->eh_context.i = ap->eh_info;
238 memset(&ap->eh_info, 0, sizeof(ap->eh_info));
239
ad9e2762 240 ap->flags &= ~ATA_FLAG_EH_PENDING;
f3e81b19 241
ad9e2762
TH
242 spin_unlock_irqrestore(hs_lock, flags);
243
244 /* invoke EH */
245 ap->ops->error_handler(ap);
246
247 /* Exception might have happend after ->error_handler
248 * recovered the port but before this point. Repeat
249 * EH in such case.
250 */
251 spin_lock_irqsave(hs_lock, flags);
252
253 if (ap->flags & ATA_FLAG_EH_PENDING) {
254 if (--repeat_cnt) {
255 ata_port_printk(ap, KERN_INFO,
256 "EH pending after completion, "
257 "repeating EH (cnt=%d)\n", repeat_cnt);
258 spin_unlock_irqrestore(hs_lock, flags);
259 goto repeat;
260 }
261 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
262 "tries, giving up\n", ATA_EH_MAX_REPEAT);
263 }
264
f3e81b19
TH
265 /* this run is complete, make sure EH info is clear */
266 memset(&ap->eh_info, 0, sizeof(ap->eh_info));
267
ad9e2762
TH
268 /* Clear host_eh_scheduled while holding hs_lock such
269 * that if exception occurs after this point but
270 * before EH completion, SCSI midlayer will
271 * re-initiate EH.
272 */
273 host->host_eh_scheduled = 0;
274
275 spin_unlock_irqrestore(hs_lock, flags);
276 } else {
277 WARN_ON(ata_qc_from_tag(ap, ap->active_tag) == NULL);
278 ap->ops->eng_timeout(ap);
279 }
ece1d636 280
ad9e2762 281 /* finish or retry handled scmd's and clean up */
ece1d636
TH
282 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
283
284 scsi_eh_flush_done_q(&ap->eh_done_q);
285
ad9e2762
TH
286 /* clean up */
287 spin_lock_irqsave(hs_lock, flags);
288
289 if (ap->flags & ATA_FLAG_RECOVERED)
290 ata_port_printk(ap, KERN_INFO, "EH complete\n");
291 ap->flags &= ~ATA_FLAG_RECOVERED;
292
293 spin_unlock_irqrestore(hs_lock, flags);
294
ece1d636 295 DPRINTK("EXIT\n");
ece1d636
TH
296}
297
298/**
299 * ata_qc_timeout - Handle timeout of queued command
300 * @qc: Command that timed out
301 *
302 * Some part of the kernel (currently, only the SCSI layer)
303 * has noticed that the active command on port @ap has not
304 * completed after a specified length of time. Handle this
305 * condition by disabling DMA (if necessary) and completing
306 * transactions, with error if necessary.
307 *
308 * This also handles the case of the "lost interrupt", where
309 * for some reason (possibly hardware bug, possibly driver bug)
310 * an interrupt was not delivered to the driver, even though the
311 * transaction completed successfully.
312 *
ad9e2762
TH
313 * TODO: kill this function once old EH is gone.
314 *
ece1d636
TH
315 * LOCKING:
316 * Inherited from SCSI layer (none, can sleep)
317 */
318static void ata_qc_timeout(struct ata_queued_cmd *qc)
319{
320 struct ata_port *ap = qc->ap;
321 struct ata_host_set *host_set = ap->host_set;
322 u8 host_stat = 0, drv_stat;
323 unsigned long flags;
324
325 DPRINTK("ENTER\n");
326
327 ap->hsm_task_state = HSM_ST_IDLE;
328
329 spin_lock_irqsave(&host_set->lock, flags);
330
331 switch (qc->tf.protocol) {
332
333 case ATA_PROT_DMA:
334 case ATA_PROT_ATAPI_DMA:
335 host_stat = ap->ops->bmdma_status(ap);
336
337 /* before we do anything else, clear DMA-Start bit */
338 ap->ops->bmdma_stop(qc);
339
340 /* fall through */
341
342 default:
343 ata_altstatus(ap);
344 drv_stat = ata_chk_status(ap);
345
346 /* ack bmdma irq events */
347 ap->ops->irq_clear(ap);
348
f15a1daf
TH
349 ata_dev_printk(qc->dev, KERN_ERR, "command 0x%x timeout, "
350 "stat 0x%x host_stat 0x%x\n",
351 qc->tf.command, drv_stat, host_stat);
ece1d636
TH
352
353 /* complete taskfile transaction */
c13b56a1 354 qc->err_mask |= AC_ERR_TIMEOUT;
ece1d636
TH
355 break;
356 }
357
358 spin_unlock_irqrestore(&host_set->lock, flags);
359
360 ata_eh_qc_complete(qc);
361
362 DPRINTK("EXIT\n");
363}
364
365/**
366 * ata_eng_timeout - Handle timeout of queued command
367 * @ap: Port on which timed-out command is active
368 *
369 * Some part of the kernel (currently, only the SCSI layer)
370 * has noticed that the active command on port @ap has not
371 * completed after a specified length of time. Handle this
372 * condition by disabling DMA (if necessary) and completing
373 * transactions, with error if necessary.
374 *
375 * This also handles the case of the "lost interrupt", where
376 * for some reason (possibly hardware bug, possibly driver bug)
377 * an interrupt was not delivered to the driver, even though the
378 * transaction completed successfully.
379 *
ad9e2762
TH
380 * TODO: kill this function once old EH is gone.
381 *
ece1d636
TH
382 * LOCKING:
383 * Inherited from SCSI layer (none, can sleep)
384 */
385void ata_eng_timeout(struct ata_port *ap)
386{
387 DPRINTK("ENTER\n");
388
389 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
390
391 DPRINTK("EXIT\n");
392}
393
f686bcb8
TH
394/**
395 * ata_qc_schedule_eh - schedule qc for error handling
396 * @qc: command to schedule error handling for
397 *
398 * Schedule error handling for @qc. EH will kick in as soon as
399 * other commands are drained.
400 *
401 * LOCKING:
402 * spin_lock_irqsave(host_set lock)
403 */
404void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
405{
406 struct ata_port *ap = qc->ap;
407
408 WARN_ON(!ap->ops->error_handler);
409
410 qc->flags |= ATA_QCFLAG_FAILED;
411 qc->ap->flags |= ATA_FLAG_EH_PENDING;
412
413 /* The following will fail if timeout has already expired.
414 * ata_scsi_error() takes care of such scmds on EH entry.
415 * Note that ATA_QCFLAG_FAILED is unconditionally set after
416 * this function completes.
417 */
418 scsi_req_abort_cmd(qc->scsicmd);
419}
420
7b70fc03
TH
421/**
422 * ata_port_schedule_eh - schedule error handling without a qc
423 * @ap: ATA port to schedule EH for
424 *
425 * Schedule error handling for @ap. EH will kick in as soon as
426 * all commands are drained.
427 *
428 * LOCKING:
429 * spin_lock_irqsave(host_set lock)
430 */
431void ata_port_schedule_eh(struct ata_port *ap)
432{
433 WARN_ON(!ap->ops->error_handler);
434
435 ap->flags |= ATA_FLAG_EH_PENDING;
f8bbfc24 436 scsi_schedule_eh(ap->host);
7b70fc03
TH
437
438 DPRINTK("port EH scheduled\n");
439}
440
441/**
442 * ata_port_abort - abort all qc's on the port
443 * @ap: ATA port to abort qc's for
444 *
445 * Abort all active qc's of @ap and schedule EH.
446 *
447 * LOCKING:
448 * spin_lock_irqsave(host_set lock)
449 *
450 * RETURNS:
451 * Number of aborted qc's.
452 */
453int ata_port_abort(struct ata_port *ap)
454{
455 int tag, nr_aborted = 0;
456
457 WARN_ON(!ap->ops->error_handler);
458
459 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
460 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
461
462 if (qc) {
463 qc->flags |= ATA_QCFLAG_FAILED;
464 ata_qc_complete(qc);
465 nr_aborted++;
466 }
467 }
468
469 if (!nr_aborted)
470 ata_port_schedule_eh(ap);
471
472 return nr_aborted;
473}
474
e3180499
TH
475/**
476 * __ata_port_freeze - freeze port
477 * @ap: ATA port to freeze
478 *
479 * This function is called when HSM violation or some other
480 * condition disrupts normal operation of the port. Frozen port
481 * is not allowed to perform any operation until the port is
482 * thawed, which usually follows a successful reset.
483 *
484 * ap->ops->freeze() callback can be used for freezing the port
485 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
486 * port cannot be frozen hardware-wise, the interrupt handler
487 * must ack and clear interrupts unconditionally while the port
488 * is frozen.
489 *
490 * LOCKING:
491 * spin_lock_irqsave(host_set lock)
492 */
493static void __ata_port_freeze(struct ata_port *ap)
494{
495 WARN_ON(!ap->ops->error_handler);
496
497 if (ap->ops->freeze)
498 ap->ops->freeze(ap);
499
500 ap->flags |= ATA_FLAG_FROZEN;
501
502 DPRINTK("ata%u port frozen\n", ap->id);
503}
504
505/**
506 * ata_port_freeze - abort & freeze port
507 * @ap: ATA port to freeze
508 *
509 * Abort and freeze @ap.
510 *
511 * LOCKING:
512 * spin_lock_irqsave(host_set lock)
513 *
514 * RETURNS:
515 * Number of aborted commands.
516 */
517int ata_port_freeze(struct ata_port *ap)
518{
519 int nr_aborted;
520
521 WARN_ON(!ap->ops->error_handler);
522
523 nr_aborted = ata_port_abort(ap);
524 __ata_port_freeze(ap);
525
526 return nr_aborted;
527}
528
529/**
530 * ata_eh_freeze_port - EH helper to freeze port
531 * @ap: ATA port to freeze
532 *
533 * Freeze @ap.
534 *
535 * LOCKING:
536 * None.
537 */
538void ata_eh_freeze_port(struct ata_port *ap)
539{
540 unsigned long flags;
541
542 if (!ap->ops->error_handler)
543 return;
544
545 spin_lock_irqsave(&ap->host_set->lock, flags);
546 __ata_port_freeze(ap);
547 spin_unlock_irqrestore(&ap->host_set->lock, flags);
548}
549
550/**
551 * ata_port_thaw_port - EH helper to thaw port
552 * @ap: ATA port to thaw
553 *
554 * Thaw frozen port @ap.
555 *
556 * LOCKING:
557 * None.
558 */
559void ata_eh_thaw_port(struct ata_port *ap)
560{
561 unsigned long flags;
562
563 if (!ap->ops->error_handler)
564 return;
565
566 spin_lock_irqsave(&ap->host_set->lock, flags);
567
568 ap->flags &= ~ATA_FLAG_FROZEN;
569
570 if (ap->ops->thaw)
571 ap->ops->thaw(ap);
572
573 spin_unlock_irqrestore(&ap->host_set->lock, flags);
574
575 DPRINTK("ata%u port thawed\n", ap->id);
576}
577
ece1d636
TH
578static void ata_eh_scsidone(struct scsi_cmnd *scmd)
579{
580 /* nada */
581}
582
583static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
584{
585 struct ata_port *ap = qc->ap;
586 struct scsi_cmnd *scmd = qc->scsicmd;
587 unsigned long flags;
588
589 spin_lock_irqsave(&ap->host_set->lock, flags);
590 qc->scsidone = ata_eh_scsidone;
591 __ata_qc_complete(qc);
592 WARN_ON(ata_tag_valid(qc->tag));
593 spin_unlock_irqrestore(&ap->host_set->lock, flags);
594
595 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
596}
597
598/**
599 * ata_eh_qc_complete - Complete an active ATA command from EH
600 * @qc: Command to complete
601 *
602 * Indicate to the mid and upper layers that an ATA command has
603 * completed. To be used from EH.
604 */
605void ata_eh_qc_complete(struct ata_queued_cmd *qc)
606{
607 struct scsi_cmnd *scmd = qc->scsicmd;
608 scmd->retries = scmd->allowed;
609 __ata_eh_qc_complete(qc);
610}
611
612/**
613 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
614 * @qc: Command to retry
615 *
616 * Indicate to the mid and upper layers that an ATA command
617 * should be retried. To be used from EH.
618 *
619 * SCSI midlayer limits the number of retries to scmd->allowed.
620 * scmd->retries is decremented for commands which get retried
621 * due to unrelated failures (qc->err_mask is zero).
622 */
623void ata_eh_qc_retry(struct ata_queued_cmd *qc)
624{
625 struct scsi_cmnd *scmd = qc->scsicmd;
626 if (!qc->err_mask && scmd->retries)
627 scmd->retries--;
628 __ata_eh_qc_complete(qc);
629}
022bdb07
TH
630
631/**
632 * ata_eh_about_to_do - about to perform eh_action
633 * @ap: target ATA port
634 * @action: action about to be performed
635 *
636 * Called just before performing EH actions to clear related bits
637 * in @ap->eh_info such that eh actions are not unnecessarily
638 * repeated.
639 *
640 * LOCKING:
641 * None.
642 */
643static void ata_eh_about_to_do(struct ata_port *ap, unsigned int action)
644{
645 unsigned long flags;
646
647 spin_lock_irqsave(&ap->host_set->lock, flags);
648 ap->eh_info.action &= ~action;
649 ap->flags |= ATA_FLAG_RECOVERED;
650 spin_unlock_irqrestore(&ap->host_set->lock, flags);
651}
652
653/**
654 * ata_err_string - convert err_mask to descriptive string
655 * @err_mask: error mask to convert to string
656 *
657 * Convert @err_mask to descriptive string. Errors are
658 * prioritized according to severity and only the most severe
659 * error is reported.
660 *
661 * LOCKING:
662 * None.
663 *
664 * RETURNS:
665 * Descriptive string for @err_mask
666 */
667static const char * ata_err_string(unsigned int err_mask)
668{
669 if (err_mask & AC_ERR_HOST_BUS)
670 return "host bus error";
671 if (err_mask & AC_ERR_ATA_BUS)
672 return "ATA bus error";
673 if (err_mask & AC_ERR_TIMEOUT)
674 return "timeout";
675 if (err_mask & AC_ERR_HSM)
676 return "HSM violation";
677 if (err_mask & AC_ERR_SYSTEM)
678 return "internal error";
679 if (err_mask & AC_ERR_MEDIA)
680 return "media error";
681 if (err_mask & AC_ERR_INVALID)
682 return "invalid argument";
683 if (err_mask & AC_ERR_DEV)
684 return "device error";
685 return "unknown error";
686}
687
e8ee8451
TH
688/**
689 * ata_read_log_page - read a specific log page
690 * @dev: target device
691 * @page: page to read
692 * @buf: buffer to store read page
693 * @sectors: number of sectors to read
694 *
695 * Read log page using READ_LOG_EXT command.
696 *
697 * LOCKING:
698 * Kernel thread context (may sleep).
699 *
700 * RETURNS:
701 * 0 on success, AC_ERR_* mask otherwise.
702 */
703static unsigned int ata_read_log_page(struct ata_device *dev,
704 u8 page, void *buf, unsigned int sectors)
705{
706 struct ata_taskfile tf;
707 unsigned int err_mask;
708
709 DPRINTK("read log page - page %d\n", page);
710
711 ata_tf_init(dev, &tf);
712 tf.command = ATA_CMD_READ_LOG_EXT;
713 tf.lbal = page;
714 tf.nsect = sectors;
715 tf.hob_nsect = sectors >> 8;
716 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
717 tf.protocol = ATA_PROT_PIO;
718
719 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
720 buf, sectors * ATA_SECT_SIZE);
721
722 DPRINTK("EXIT, err_mask=%x\n", err_mask);
723 return err_mask;
724}
725
726/**
727 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
728 * @dev: Device to read log page 10h from
729 * @tag: Resulting tag of the failed command
730 * @tf: Resulting taskfile registers of the failed command
731 *
732 * Read log page 10h to obtain NCQ error details and clear error
733 * condition.
734 *
735 * LOCKING:
736 * Kernel thread context (may sleep).
737 *
738 * RETURNS:
739 * 0 on success, -errno otherwise.
740 */
741static int ata_eh_read_log_10h(struct ata_device *dev,
742 int *tag, struct ata_taskfile *tf)
743{
744 u8 *buf = dev->ap->sector_buf;
745 unsigned int err_mask;
746 u8 csum;
747 int i;
748
749 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
750 if (err_mask)
751 return -EIO;
752
753 csum = 0;
754 for (i = 0; i < ATA_SECT_SIZE; i++)
755 csum += buf[i];
756 if (csum)
757 ata_dev_printk(dev, KERN_WARNING,
758 "invalid checksum 0x%x on log page 10h\n", csum);
759
760 if (buf[0] & 0x80)
761 return -ENOENT;
762
763 *tag = buf[0] & 0x1f;
764
765 tf->command = buf[2];
766 tf->feature = buf[3];
767 tf->lbal = buf[4];
768 tf->lbam = buf[5];
769 tf->lbah = buf[6];
770 tf->device = buf[7];
771 tf->hob_lbal = buf[8];
772 tf->hob_lbam = buf[9];
773 tf->hob_lbah = buf[10];
774 tf->nsect = buf[12];
775 tf->hob_nsect = buf[13];
776
777 return 0;
778}
779
022bdb07
TH
780/**
781 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
782 * @dev: device to perform REQUEST_SENSE to
783 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
784 *
785 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
786 * SENSE. This function is EH helper.
787 *
788 * LOCKING:
789 * Kernel thread context (may sleep).
790 *
791 * RETURNS:
792 * 0 on success, AC_ERR_* mask on failure
793 */
794static unsigned int atapi_eh_request_sense(struct ata_device *dev,
795 unsigned char *sense_buf)
796{
797 struct ata_port *ap = dev->ap;
798 struct ata_taskfile tf;
799 u8 cdb[ATAPI_CDB_LEN];
800
801 DPRINTK("ATAPI request sense\n");
802
803 ata_tf_init(dev, &tf);
804
805 /* FIXME: is this needed? */
806 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
807
808 /* XXX: why tf_read here? */
809 ap->ops->tf_read(ap, &tf);
810
811 /* fill these in, for the case where they are -not- overwritten */
812 sense_buf[0] = 0x70;
813 sense_buf[2] = tf.feature >> 4;
814
815 memset(cdb, 0, ATAPI_CDB_LEN);
816 cdb[0] = REQUEST_SENSE;
817 cdb[4] = SCSI_SENSE_BUFFERSIZE;
818
819 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
820 tf.command = ATA_CMD_PACKET;
821
822 /* is it pointless to prefer PIO for "safety reasons"? */
823 if (ap->flags & ATA_FLAG_PIO_DMA) {
824 tf.protocol = ATA_PROT_ATAPI_DMA;
825 tf.feature |= ATAPI_PKT_DMA;
826 } else {
827 tf.protocol = ATA_PROT_ATAPI;
828 tf.lbam = (8 * 1024) & 0xff;
829 tf.lbah = (8 * 1024) >> 8;
830 }
831
832 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
833 sense_buf, SCSI_SENSE_BUFFERSIZE);
834}
835
836/**
837 * ata_eh_analyze_serror - analyze SError for a failed port
838 * @ap: ATA port to analyze SError for
839 *
840 * Analyze SError if available and further determine cause of
841 * failure.
842 *
843 * LOCKING:
844 * None.
845 */
846static void ata_eh_analyze_serror(struct ata_port *ap)
847{
848 struct ata_eh_context *ehc = &ap->eh_context;
849 u32 serror = ehc->i.serror;
850 unsigned int err_mask = 0, action = 0;
851
852 if (serror & SERR_PERSISTENT) {
853 err_mask |= AC_ERR_ATA_BUS;
854 action |= ATA_EH_HARDRESET;
855 }
856 if (serror &
857 (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
858 err_mask |= AC_ERR_ATA_BUS;
859 action |= ATA_EH_SOFTRESET;
860 }
861 if (serror & SERR_PROTOCOL) {
862 err_mask |= AC_ERR_HSM;
863 action |= ATA_EH_SOFTRESET;
864 }
865 if (serror & SERR_INTERNAL) {
866 err_mask |= AC_ERR_SYSTEM;
867 action |= ATA_EH_SOFTRESET;
868 }
869 if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG)) {
870 err_mask |= AC_ERR_ATA_BUS;
871 action |= ATA_EH_HARDRESET;
872 }
873
874 ehc->i.err_mask |= err_mask;
875 ehc->i.action |= action;
876}
877
e8ee8451
TH
878/**
879 * ata_eh_analyze_ncq_error - analyze NCQ error
880 * @ap: ATA port to analyze NCQ error for
881 *
882 * Read log page 10h, determine the offending qc and acquire
883 * error status TF. For NCQ device errors, all LLDDs have to do
884 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
885 * care of the rest.
886 *
887 * LOCKING:
888 * Kernel thread context (may sleep).
889 */
890static void ata_eh_analyze_ncq_error(struct ata_port *ap)
891{
892 struct ata_eh_context *ehc = &ap->eh_context;
893 struct ata_device *dev = ap->device;
894 struct ata_queued_cmd *qc;
895 struct ata_taskfile tf;
896 int tag, rc;
897
898 /* if frozen, we can't do much */
899 if (ap->flags & ATA_FLAG_FROZEN)
900 return;
901
902 /* is it NCQ device error? */
903 if (!ap->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
904 return;
905
906 /* has LLDD analyzed already? */
907 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
908 qc = __ata_qc_from_tag(ap, tag);
909
910 if (!(qc->flags & ATA_QCFLAG_FAILED))
911 continue;
912
913 if (qc->err_mask)
914 return;
915 }
916
917 /* okay, this error is ours */
918 rc = ata_eh_read_log_10h(dev, &tag, &tf);
919 if (rc) {
920 ata_port_printk(ap, KERN_ERR, "failed to read log page 10h "
921 "(errno=%d)\n", rc);
922 return;
923 }
924
925 if (!(ap->sactive & (1 << tag))) {
926 ata_port_printk(ap, KERN_ERR, "log page 10h reported "
927 "inactive tag %d\n", tag);
928 return;
929 }
930
931 /* we've got the perpetrator, condemn it */
932 qc = __ata_qc_from_tag(ap, tag);
933 memcpy(&qc->result_tf, &tf, sizeof(tf));
934 qc->err_mask |= AC_ERR_DEV;
935 ehc->i.err_mask &= ~AC_ERR_DEV;
936}
937
022bdb07
TH
938/**
939 * ata_eh_analyze_tf - analyze taskfile of a failed qc
940 * @qc: qc to analyze
941 * @tf: Taskfile registers to analyze
942 *
943 * Analyze taskfile of @qc and further determine cause of
944 * failure. This function also requests ATAPI sense data if
945 * avaliable.
946 *
947 * LOCKING:
948 * Kernel thread context (may sleep).
949 *
950 * RETURNS:
951 * Determined recovery action
952 */
953static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
954 const struct ata_taskfile *tf)
955{
956 unsigned int tmp, action = 0;
957 u8 stat = tf->command, err = tf->feature;
958
959 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
960 qc->err_mask |= AC_ERR_HSM;
961 return ATA_EH_SOFTRESET;
962 }
963
964 if (!(qc->err_mask & AC_ERR_DEV))
965 return 0;
966
967 switch (qc->dev->class) {
968 case ATA_DEV_ATA:
969 if (err & ATA_ICRC)
970 qc->err_mask |= AC_ERR_ATA_BUS;
971 if (err & ATA_UNC)
972 qc->err_mask |= AC_ERR_MEDIA;
973 if (err & ATA_IDNF)
974 qc->err_mask |= AC_ERR_INVALID;
975 break;
976
977 case ATA_DEV_ATAPI:
978 tmp = atapi_eh_request_sense(qc->dev,
979 qc->scsicmd->sense_buffer);
980 if (!tmp) {
981 /* ATA_QCFLAG_SENSE_VALID is used to tell
982 * atapi_qc_complete() that sense data is
983 * already valid.
984 *
985 * TODO: interpret sense data and set
986 * appropriate err_mask.
987 */
988 qc->flags |= ATA_QCFLAG_SENSE_VALID;
989 } else
990 qc->err_mask |= tmp;
991 }
992
993 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
994 action |= ATA_EH_SOFTRESET;
995
996 return action;
997}
998
999static int ata_eh_categorize_ering_entry(struct ata_ering_entry *ent)
1000{
1001 if (ent->err_mask & (AC_ERR_ATA_BUS | AC_ERR_TIMEOUT))
1002 return 1;
1003
1004 if (ent->is_io) {
1005 if (ent->err_mask & AC_ERR_HSM)
1006 return 1;
1007 if ((ent->err_mask &
1008 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1009 return 2;
1010 }
1011
1012 return 0;
1013}
1014
1015struct speed_down_needed_arg {
1016 u64 since;
1017 int nr_errors[3];
1018};
1019
1020static int speed_down_needed_cb(struct ata_ering_entry *ent, void *void_arg)
1021{
1022 struct speed_down_needed_arg *arg = void_arg;
1023
1024 if (ent->timestamp < arg->since)
1025 return -1;
1026
1027 arg->nr_errors[ata_eh_categorize_ering_entry(ent)]++;
1028 return 0;
1029}
1030
1031/**
1032 * ata_eh_speed_down_needed - Determine wheter speed down is necessary
1033 * @dev: Device of interest
1034 *
1035 * This function examines error ring of @dev and determines
1036 * whether speed down is necessary. Speed down is necessary if
1037 * there have been more than 3 of Cat-1 errors or 10 of Cat-2
1038 * errors during last 15 minutes.
1039 *
1040 * Cat-1 errors are ATA_BUS, TIMEOUT for any command and HSM
1041 * violation for known supported commands.
1042 *
1043 * Cat-2 errors are unclassified DEV error for known supported
1044 * command.
1045 *
1046 * LOCKING:
1047 * Inherited from caller.
1048 *
1049 * RETURNS:
1050 * 1 if speed down is necessary, 0 otherwise
1051 */
1052static int ata_eh_speed_down_needed(struct ata_device *dev)
1053{
1054 const u64 interval = 15LLU * 60 * HZ;
1055 static const int err_limits[3] = { -1, 3, 10 };
1056 struct speed_down_needed_arg arg;
1057 struct ata_ering_entry *ent;
1058 int err_cat;
1059 u64 j64;
1060
1061 ent = ata_ering_top(&dev->ering);
1062 if (!ent)
1063 return 0;
1064
1065 err_cat = ata_eh_categorize_ering_entry(ent);
1066 if (err_cat == 0)
1067 return 0;
1068
1069 memset(&arg, 0, sizeof(arg));
1070
1071 j64 = get_jiffies_64();
1072 if (j64 >= interval)
1073 arg.since = j64 - interval;
1074 else
1075 arg.since = 0;
1076
1077 ata_ering_map(&dev->ering, speed_down_needed_cb, &arg);
1078
1079 return arg.nr_errors[err_cat] > err_limits[err_cat];
1080}
1081
1082/**
1083 * ata_eh_speed_down - record error and speed down if necessary
1084 * @dev: Failed device
1085 * @is_io: Did the device fail during normal IO?
1086 * @err_mask: err_mask of the error
1087 *
1088 * Record error and examine error history to determine whether
1089 * adjusting transmission speed is necessary. It also sets
1090 * transmission limits appropriately if such adjustment is
1091 * necessary.
1092 *
1093 * LOCKING:
1094 * Kernel thread context (may sleep).
1095 *
1096 * RETURNS:
1097 * 0 on success, -errno otherwise
1098 */
1099static int ata_eh_speed_down(struct ata_device *dev, int is_io,
1100 unsigned int err_mask)
1101{
1102 if (!err_mask)
1103 return 0;
1104
1105 /* record error and determine whether speed down is necessary */
1106 ata_ering_record(&dev->ering, is_io, err_mask);
1107
1108 if (!ata_eh_speed_down_needed(dev))
1109 return 0;
1110
1111 /* speed down SATA link speed if possible */
1112 if (sata_down_spd_limit(dev->ap) == 0)
1113 return ATA_EH_HARDRESET;
1114
1115 /* lower transfer mode */
1116 if (ata_down_xfermask_limit(dev, 0) == 0)
1117 return ATA_EH_SOFTRESET;
1118
1119 ata_dev_printk(dev, KERN_ERR,
1120 "speed down requested but no transfer mode left\n");
1121 return 0;
1122}
1123
1124/**
1125 * ata_eh_autopsy - analyze error and determine recovery action
1126 * @ap: ATA port to perform autopsy on
1127 *
1128 * Analyze why @ap failed and determine which recovery action is
1129 * needed. This function also sets more detailed AC_ERR_* values
1130 * and fills sense data for ATAPI CHECK SENSE.
1131 *
1132 * LOCKING:
1133 * Kernel thread context (may sleep).
1134 */
1135static void ata_eh_autopsy(struct ata_port *ap)
1136{
1137 struct ata_eh_context *ehc = &ap->eh_context;
1138 unsigned int action = ehc->i.action;
1139 struct ata_device *failed_dev = NULL;
1140 unsigned int all_err_mask = 0;
1141 int tag, is_io = 0;
1142 u32 serror;
1143 int rc;
1144
1145 DPRINTK("ENTER\n");
1146
1147 /* obtain and analyze SError */
1148 rc = sata_scr_read(ap, SCR_ERROR, &serror);
1149 if (rc == 0) {
1150 ehc->i.serror |= serror;
1151 ata_eh_analyze_serror(ap);
1152 } else if (rc != -EOPNOTSUPP)
1153 action |= ATA_EH_HARDRESET;
1154
e8ee8451
TH
1155 /* analyze NCQ failure */
1156 ata_eh_analyze_ncq_error(ap);
1157
022bdb07
TH
1158 /* any real error trumps AC_ERR_OTHER */
1159 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1160 ehc->i.err_mask &= ~AC_ERR_OTHER;
1161
1162 all_err_mask |= ehc->i.err_mask;
1163
1164 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1165 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1166
1167 if (!(qc->flags & ATA_QCFLAG_FAILED))
1168 continue;
1169
1170 /* inherit upper level err_mask */
1171 qc->err_mask |= ehc->i.err_mask;
1172
022bdb07
TH
1173 /* analyze TF */
1174 action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1175
1176 /* DEV errors are probably spurious in case of ATA_BUS error */
1177 if (qc->err_mask & AC_ERR_ATA_BUS)
1178 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1179 AC_ERR_INVALID);
1180
1181 /* any real error trumps unknown error */
1182 if (qc->err_mask & ~AC_ERR_OTHER)
1183 qc->err_mask &= ~AC_ERR_OTHER;
1184
1185 /* SENSE_VALID trumps dev/unknown error and revalidation */
1186 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1187 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1188 action &= ~ATA_EH_REVALIDATE;
1189 }
1190
1191 /* accumulate error info */
1192 failed_dev = qc->dev;
1193 all_err_mask |= qc->err_mask;
1194 if (qc->flags & ATA_QCFLAG_IO)
1195 is_io = 1;
1196 }
1197
1198 /* speed down iff command was in progress */
1199 if (failed_dev)
1200 action |= ata_eh_speed_down(failed_dev, is_io, all_err_mask);
1201
a20f33ff
TH
1202 /* enforce default EH actions */
1203 if (ap->flags & ATA_FLAG_FROZEN ||
1204 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
1205 action |= ATA_EH_SOFTRESET;
1206 else if (all_err_mask)
022bdb07
TH
1207 action |= ATA_EH_REVALIDATE;
1208
a20f33ff 1209 /* record autopsy result */
022bdb07
TH
1210 ehc->i.dev = failed_dev;
1211 ehc->i.action = action;
1212
1213 DPRINTK("EXIT\n");
1214}
1215
1216/**
1217 * ata_eh_report - report error handling to user
1218 * @ap: ATA port EH is going on
1219 *
1220 * Report EH to user.
1221 *
1222 * LOCKING:
1223 * None.
1224 */
1225static void ata_eh_report(struct ata_port *ap)
1226{
1227 struct ata_eh_context *ehc = &ap->eh_context;
1228 const char *frozen, *desc;
1229 int tag, nr_failed = 0;
1230
1231 desc = NULL;
1232 if (ehc->i.desc[0] != '\0')
1233 desc = ehc->i.desc;
1234
1235 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1236 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1237
1238 if (!(qc->flags & ATA_QCFLAG_FAILED))
1239 continue;
1240 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1241 continue;
1242
1243 nr_failed++;
1244 }
1245
1246 if (!nr_failed && !ehc->i.err_mask)
1247 return;
1248
1249 frozen = "";
1250 if (ap->flags & ATA_FLAG_FROZEN)
1251 frozen = " frozen";
1252
1253 if (ehc->i.dev) {
e8ee8451
TH
1254 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1255 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1256 ehc->i.err_mask, ap->sactive, ehc->i.serror,
1257 ehc->i.action, frozen);
022bdb07
TH
1258 if (desc)
1259 ata_dev_printk(ehc->i.dev, KERN_ERR, "(%s)\n", desc);
1260 } else {
e8ee8451
TH
1261 ata_port_printk(ap, KERN_ERR, "exception Emask 0x%x "
1262 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1263 ehc->i.err_mask, ap->sactive, ehc->i.serror,
1264 ehc->i.action, frozen);
022bdb07
TH
1265 if (desc)
1266 ata_port_printk(ap, KERN_ERR, "(%s)\n", desc);
1267 }
1268
1269 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1270 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1271
1272 if (!(qc->flags & ATA_QCFLAG_FAILED) || !qc->err_mask)
1273 continue;
1274
1275 ata_dev_printk(qc->dev, KERN_ERR, "tag %d cmd 0x%x "
1276 "Emask 0x%x stat 0x%x err 0x%x (%s)\n",
1277 qc->tag, qc->tf.command, qc->err_mask,
1278 qc->result_tf.command, qc->result_tf.feature,
1279 ata_err_string(qc->err_mask));
1280 }
1281}
1282
1283static int ata_eh_reset(struct ata_port *ap, ata_reset_fn_t softreset,
1284 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
1285{
1286 struct ata_eh_context *ehc = &ap->eh_context;
1287 unsigned int classes[ATA_MAX_DEVICES];
1288 int tries = ATA_EH_RESET_TRIES;
1289 ata_reset_fn_t reset;
20952b69 1290 int i, rc;
022bdb07
TH
1291
1292 if (softreset && (!hardreset || (!sata_set_spd_needed(ap) &&
1293 !(ehc->i.action & ATA_EH_HARDRESET))))
1294 reset = softreset;
1295 else
1296 reset = hardreset;
1297
1298 retry:
1299 ata_port_printk(ap, KERN_INFO, "%s resetting port\n",
1300 reset == softreset ? "soft" : "hard");
1301
1302 /* reset */
1303 ata_eh_about_to_do(ap, ATA_EH_RESET_MASK);
1304 ehc->i.flags |= ATA_EHI_DID_RESET;
1305
1306 rc = ata_do_reset(ap, reset, classes);
1307
1308 if (rc && --tries) {
1309 ata_port_printk(ap, KERN_WARNING,
1310 "%sreset failed, retrying in 5 secs\n",
1311 reset == softreset ? "soft" : "hard");
1312 ssleep(5);
1313
1314 if (reset == hardreset)
1315 sata_down_spd_limit(ap);
1316 if (hardreset)
1317 reset = hardreset;
1318 goto retry;
1319 }
1320
1321 if (rc == 0) {
20952b69
TH
1322 /* After the reset, the device state is PIO 0 and the
1323 * controller state is undefined. Record the mode.
1324 */
1325 for (i = 0; i < ATA_MAX_DEVICES; i++)
1326 ap->device[i].pio_mode = XFER_PIO_0;
1327
022bdb07
TH
1328 if (postreset)
1329 postreset(ap, classes);
1330
1331 /* reset successful, schedule revalidation */
1332 ehc->i.dev = NULL;
1333 ehc->i.action &= ~ATA_EH_RESET_MASK;
1334 ehc->i.action |= ATA_EH_REVALIDATE;
1335 }
1336
1337 return rc;
1338}
1339
1340static int ata_eh_revalidate(struct ata_port *ap,
1341 struct ata_device **r_failed_dev)
1342{
1343 struct ata_eh_context *ehc = &ap->eh_context;
1344 struct ata_device *dev;
1345 int i, rc = 0;
1346
1347 DPRINTK("ENTER\n");
1348
1349 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1350 dev = &ap->device[i];
1351
1352 if (ehc->i.action & ATA_EH_REVALIDATE && ata_dev_enabled(dev) &&
1353 (!ehc->i.dev || ehc->i.dev == dev)) {
1354 if (ata_port_offline(ap)) {
1355 rc = -EIO;
1356 break;
1357 }
1358
1359 ata_eh_about_to_do(ap, ATA_EH_REVALIDATE);
1360 rc = ata_dev_revalidate(dev,
1361 ehc->i.flags & ATA_EHI_DID_RESET);
1362 if (rc)
1363 break;
1364
1365 ehc->i.action &= ~ATA_EH_REVALIDATE;
1366 }
1367 }
1368
1369 if (rc)
1370 *r_failed_dev = dev;
1371
1372 DPRINTK("EXIT\n");
1373 return rc;
1374}
1375
1376static int ata_port_nr_enabled(struct ata_port *ap)
1377{
1378 int i, cnt = 0;
1379
1380 for (i = 0; i < ATA_MAX_DEVICES; i++)
1381 if (ata_dev_enabled(&ap->device[i]))
1382 cnt++;
1383 return cnt;
1384}
1385
1386/**
1387 * ata_eh_recover - recover host port after error
1388 * @ap: host port to recover
1389 * @softreset: softreset method (can be NULL)
1390 * @hardreset: hardreset method (can be NULL)
1391 * @postreset: postreset method (can be NULL)
1392 *
1393 * This is the alpha and omega, eum and yang, heart and soul of
1394 * libata exception handling. On entry, actions required to
1395 * recover each devices are recorded in eh_context. This
1396 * function executes all the operations with appropriate retrials
1397 * and fallbacks to resurrect failed devices.
1398 *
1399 * LOCKING:
1400 * Kernel thread context (may sleep).
1401 *
1402 * RETURNS:
1403 * 0 on success, -errno on failure.
1404 */
1405static int ata_eh_recover(struct ata_port *ap, ata_reset_fn_t softreset,
1406 ata_reset_fn_t hardreset,
1407 ata_postreset_fn_t postreset)
1408{
1409 struct ata_eh_context *ehc = &ap->eh_context;
1410 struct ata_device *dev;
1411 int down_xfermask, i, rc;
1412
1413 DPRINTK("ENTER\n");
1414
1415 /* prep for recovery */
1416 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1417 dev = &ap->device[i];
1418
1419 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
1420 }
1421
1422 retry:
1423 down_xfermask = 0;
1424 rc = 0;
1425
1426 /* skip EH if possible. */
1427 if (!ata_port_nr_enabled(ap) && !(ap->flags & ATA_FLAG_FROZEN))
1428 ehc->i.action = 0;
1429
1430 /* reset */
1431 if (ehc->i.action & ATA_EH_RESET_MASK) {
1432 ata_eh_freeze_port(ap);
1433
1434 rc = ata_eh_reset(ap, softreset, hardreset, postreset);
1435 if (rc) {
1436 ata_port_printk(ap, KERN_ERR,
1437 "reset failed, giving up\n");
1438 goto out;
1439 }
1440
1441 ata_eh_thaw_port(ap);
1442 }
1443
1444 /* revalidate existing devices */
1445 rc = ata_eh_revalidate(ap, &dev);
1446 if (rc)
1447 goto dev_fail;
1448
1449 /* configure transfer mode if the port has been reset */
1450 if (ehc->i.flags & ATA_EHI_DID_RESET) {
1451 rc = ata_set_mode(ap, &dev);
1452 if (rc) {
1453 down_xfermask = 1;
1454 goto dev_fail;
1455 }
1456 }
1457
1458 goto out;
1459
1460 dev_fail:
1461 switch (rc) {
1462 case -ENODEV:
1463 case -EINVAL:
1464 ehc->tries[dev->devno] = 0;
1465 break;
1466 case -EIO:
1467 sata_down_spd_limit(ap);
1468 default:
1469 ehc->tries[dev->devno]--;
1470 if (down_xfermask &&
1471 ata_down_xfermask_limit(dev, ehc->tries[dev->devno] == 1))
1472 ehc->tries[dev->devno] = 0;
1473 }
1474
1475 /* disable device if it has used up all its chances */
1476 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno])
1477 ata_dev_disable(dev);
1478
1479 /* soft didn't work? be haaaaard */
1480 if (ehc->i.flags & ATA_EHI_DID_RESET)
1481 ehc->i.action |= ATA_EH_HARDRESET;
1482 else
1483 ehc->i.action |= ATA_EH_SOFTRESET;
1484
1485 if (ata_port_nr_enabled(ap)) {
1486 ata_port_printk(ap, KERN_WARNING, "failed to recover some "
1487 "devices, retrying in 5 secs\n");
1488 ssleep(5);
1489 } else {
1490 /* no device left, repeat fast */
1491 msleep(500);
1492 }
1493
1494 goto retry;
1495
1496 out:
1497 if (rc) {
1498 for (i = 0; i < ATA_MAX_DEVICES; i++)
1499 ata_dev_disable(&ap->device[i]);
1500 }
1501
1502 DPRINTK("EXIT, rc=%d\n", rc);
1503 return rc;
1504}
1505
1506/**
1507 * ata_eh_finish - finish up EH
1508 * @ap: host port to finish EH for
1509 *
1510 * Recovery is complete. Clean up EH states and retry or finish
1511 * failed qcs.
1512 *
1513 * LOCKING:
1514 * None.
1515 */
1516static void ata_eh_finish(struct ata_port *ap)
1517{
1518 int tag;
1519
1520 /* retry or finish qcs */
1521 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1522 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1523
1524 if (!(qc->flags & ATA_QCFLAG_FAILED))
1525 continue;
1526
1527 if (qc->err_mask) {
1528 /* FIXME: Once EH migration is complete,
1529 * generate sense data in this function,
1530 * considering both err_mask and tf.
1531 */
1532 if (qc->err_mask & AC_ERR_INVALID)
1533 ata_eh_qc_complete(qc);
1534 else
1535 ata_eh_qc_retry(qc);
1536 } else {
1537 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1538 ata_eh_qc_complete(qc);
1539 } else {
1540 /* feed zero TF to sense generation */
1541 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
1542 ata_eh_qc_retry(qc);
1543 }
1544 }
1545 }
1546}
1547
1548/**
1549 * ata_do_eh - do standard error handling
1550 * @ap: host port to handle error for
1551 * @softreset: softreset method (can be NULL)
1552 * @hardreset: hardreset method (can be NULL)
1553 * @postreset: postreset method (can be NULL)
1554 *
1555 * Perform standard error handling sequence.
1556 *
1557 * LOCKING:
1558 * Kernel thread context (may sleep).
1559 */
1560void ata_do_eh(struct ata_port *ap, ata_reset_fn_t softreset,
1561 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
1562{
1563 ata_eh_autopsy(ap);
1564 ata_eh_report(ap);
1565 ata_eh_recover(ap, softreset, hardreset, postreset);
1566 ata_eh_finish(ap);
1567}