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