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