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