libata: kill dead code paths in reset path
[GitHub/mt8127/android_kernel_alcatel_ttab.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 35#include <linux/kernel.h>
2855568b 36#include <linux/pci.h>
ece1d636
TH
37#include <scsi/scsi.h>
38#include <scsi/scsi_host.h>
39#include <scsi/scsi_eh.h>
40#include <scsi/scsi_device.h>
41#include <scsi/scsi_cmnd.h>
c6fd2807 42#include "../scsi/scsi_transport_api.h"
ece1d636
TH
43
44#include <linux/libata.h>
45
46#include "libata.h"
47
7d47e8d4 48enum {
3884f7b0 49 /* speed down verdicts */
7d47e8d4
TH
50 ATA_EH_SPDN_NCQ_OFF = (1 << 0),
51 ATA_EH_SPDN_SPEED_DOWN = (1 << 1),
52 ATA_EH_SPDN_FALLBACK_TO_PIO = (1 << 2),
76326ac1 53 ATA_EH_SPDN_KEEP_ERRORS = (1 << 3),
3884f7b0
TH
54
55 /* error flags */
56 ATA_EFLAG_IS_IO = (1 << 0),
76326ac1 57 ATA_EFLAG_DUBIOUS_XFER = (1 << 1),
3884f7b0
TH
58
59 /* error categories */
60 ATA_ECAT_NONE = 0,
61 ATA_ECAT_ATA_BUS = 1,
62 ATA_ECAT_TOUT_HSM = 2,
63 ATA_ECAT_UNK_DEV = 3,
75f9cafc
TH
64 ATA_ECAT_DUBIOUS_NONE = 4,
65 ATA_ECAT_DUBIOUS_ATA_BUS = 5,
66 ATA_ECAT_DUBIOUS_TOUT_HSM = 6,
67 ATA_ECAT_DUBIOUS_UNK_DEV = 7,
68 ATA_ECAT_NR = 8,
7d47e8d4
TH
69};
70
31daabda
TH
71/* Waiting in ->prereset can never be reliable. It's sometimes nice
72 * to wait there but it can't be depended upon; otherwise, we wouldn't
73 * be resetting. Just give it enough time for most drives to spin up.
74 */
75enum {
76 ATA_EH_PRERESET_TIMEOUT = 10 * HZ,
5ddf24c5 77 ATA_EH_FASTDRAIN_INTERVAL = 3 * HZ,
31daabda
TH
78};
79
80/* The following table determines how we sequence resets. Each entry
81 * represents timeout for that try. The first try can be soft or
82 * hardreset. All others are hardreset if available. In most cases
83 * the first reset w/ 10sec timeout should succeed. Following entries
84 * are mostly for error handling, hotplug and retarded devices.
85 */
86static const unsigned long ata_eh_reset_timeouts[] = {
87 10 * HZ, /* most drives spin up by 10sec */
88 10 * HZ, /* > 99% working drives spin up before 20sec */
89 35 * HZ, /* give > 30 secs of idleness for retarded devices */
90 5 * HZ, /* and sweet one last chance */
91 /* > 1 min has elapsed, give up */
92};
93
ad9e2762 94static void __ata_port_freeze(struct ata_port *ap);
6ffa01d8 95#ifdef CONFIG_PM
500530f6
TH
96static void ata_eh_handle_port_suspend(struct ata_port *ap);
97static void ata_eh_handle_port_resume(struct ata_port *ap);
6ffa01d8
TH
98#else /* CONFIG_PM */
99static void ata_eh_handle_port_suspend(struct ata_port *ap)
100{ }
101
102static void ata_eh_handle_port_resume(struct ata_port *ap)
103{ }
6ffa01d8 104#endif /* CONFIG_PM */
ad9e2762 105
b64bbc39
TH
106static void __ata_ehi_pushv_desc(struct ata_eh_info *ehi, const char *fmt,
107 va_list args)
108{
109 ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
110 ATA_EH_DESC_LEN - ehi->desc_len,
111 fmt, args);
112}
113
114/**
115 * __ata_ehi_push_desc - push error description without adding separator
116 * @ehi: target EHI
117 * @fmt: printf format string
118 *
119 * Format string according to @fmt and append it to @ehi->desc.
120 *
121 * LOCKING:
122 * spin_lock_irqsave(host lock)
123 */
124void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
125{
126 va_list args;
127
128 va_start(args, fmt);
129 __ata_ehi_pushv_desc(ehi, fmt, args);
130 va_end(args);
131}
132
133/**
134 * ata_ehi_push_desc - push error description with separator
135 * @ehi: target EHI
136 * @fmt: printf format string
137 *
138 * Format string according to @fmt and append it to @ehi->desc.
139 * If @ehi->desc is not empty, ", " is added in-between.
140 *
141 * LOCKING:
142 * spin_lock_irqsave(host lock)
143 */
144void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
145{
146 va_list args;
147
148 if (ehi->desc_len)
149 __ata_ehi_push_desc(ehi, ", ");
150
151 va_start(args, fmt);
152 __ata_ehi_pushv_desc(ehi, fmt, args);
153 va_end(args);
154}
155
156/**
157 * ata_ehi_clear_desc - clean error description
158 * @ehi: target EHI
159 *
160 * Clear @ehi->desc.
161 *
162 * LOCKING:
163 * spin_lock_irqsave(host lock)
164 */
165void ata_ehi_clear_desc(struct ata_eh_info *ehi)
166{
167 ehi->desc[0] = '\0';
168 ehi->desc_len = 0;
169}
170
cbcdd875
TH
171/**
172 * ata_port_desc - append port description
173 * @ap: target ATA port
174 * @fmt: printf format string
175 *
176 * Format string according to @fmt and append it to port
177 * description. If port description is not empty, " " is added
178 * in-between. This function is to be used while initializing
179 * ata_host. The description is printed on host registration.
180 *
181 * LOCKING:
182 * None.
183 */
184void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
185{
186 va_list args;
187
188 WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
189
190 if (ap->link.eh_info.desc_len)
191 __ata_ehi_push_desc(&ap->link.eh_info, " ");
192
193 va_start(args, fmt);
194 __ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
195 va_end(args);
196}
197
198#ifdef CONFIG_PCI
199
200/**
201 * ata_port_pbar_desc - append PCI BAR description
202 * @ap: target ATA port
203 * @bar: target PCI BAR
204 * @offset: offset into PCI BAR
205 * @name: name of the area
206 *
207 * If @offset is negative, this function formats a string which
208 * contains the name, address, size and type of the BAR and
209 * appends it to the port description. If @offset is zero or
210 * positive, only name and offsetted address is appended.
211 *
212 * LOCKING:
213 * None.
214 */
215void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
216 const char *name)
217{
218 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
219 char *type = "";
220 unsigned long long start, len;
221
222 if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
223 type = "m";
224 else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
225 type = "i";
226
227 start = (unsigned long long)pci_resource_start(pdev, bar);
228 len = (unsigned long long)pci_resource_len(pdev, bar);
229
230 if (offset < 0)
231 ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
232 else
e6a73ab1
AM
233 ata_port_desc(ap, "%s 0x%llx", name,
234 start + (unsigned long long)offset);
cbcdd875
TH
235}
236
237#endif /* CONFIG_PCI */
238
3884f7b0 239static void ata_ering_record(struct ata_ering *ering, unsigned int eflags,
0c247c55
TH
240 unsigned int err_mask)
241{
242 struct ata_ering_entry *ent;
243
244 WARN_ON(!err_mask);
245
246 ering->cursor++;
247 ering->cursor %= ATA_ERING_SIZE;
248
249 ent = &ering->ring[ering->cursor];
3884f7b0 250 ent->eflags = eflags;
0c247c55
TH
251 ent->err_mask = err_mask;
252 ent->timestamp = get_jiffies_64();
253}
254
76326ac1
TH
255static struct ata_ering_entry *ata_ering_top(struct ata_ering *ering)
256{
257 struct ata_ering_entry *ent = &ering->ring[ering->cursor];
258
259 if (ent->err_mask)
260 return ent;
261 return NULL;
262}
263
7d47e8d4 264static void ata_ering_clear(struct ata_ering *ering)
0c247c55 265{
7d47e8d4 266 memset(ering, 0, sizeof(*ering));
0c247c55
TH
267}
268
269static int ata_ering_map(struct ata_ering *ering,
270 int (*map_fn)(struct ata_ering_entry *, void *),
271 void *arg)
272{
273 int idx, rc = 0;
274 struct ata_ering_entry *ent;
275
276 idx = ering->cursor;
277 do {
278 ent = &ering->ring[idx];
279 if (!ent->err_mask)
280 break;
281 rc = map_fn(ent, arg);
282 if (rc)
283 break;
284 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
285 } while (idx != ering->cursor);
286
287 return rc;
288}
289
64f65ca6
TH
290static unsigned int ata_eh_dev_action(struct ata_device *dev)
291{
9af5c9c9 292 struct ata_eh_context *ehc = &dev->link->eh_context;
64f65ca6
TH
293
294 return ehc->i.action | ehc->i.dev_action[dev->devno];
295}
296
f58229f8 297static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
af181c2d
TH
298 struct ata_eh_info *ehi, unsigned int action)
299{
f58229f8 300 struct ata_device *tdev;
af181c2d
TH
301
302 if (!dev) {
303 ehi->action &= ~action;
f58229f8
TH
304 ata_link_for_each_dev(tdev, link)
305 ehi->dev_action[tdev->devno] &= ~action;
af181c2d
TH
306 } else {
307 /* doesn't make sense for port-wide EH actions */
308 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
309
310 /* break ehi->action into ehi->dev_action */
311 if (ehi->action & action) {
f58229f8
TH
312 ata_link_for_each_dev(tdev, link)
313 ehi->dev_action[tdev->devno] |=
314 ehi->action & action;
af181c2d
TH
315 ehi->action &= ~action;
316 }
317
318 /* turn off the specified per-dev action */
319 ehi->dev_action[dev->devno] &= ~action;
320 }
321}
322
ece1d636
TH
323/**
324 * ata_scsi_timed_out - SCSI layer time out callback
325 * @cmd: timed out SCSI command
326 *
327 * Handles SCSI layer timeout. We race with normal completion of
328 * the qc for @cmd. If the qc is already gone, we lose and let
329 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
330 * timed out and EH should be invoked. Prevent ata_qc_complete()
331 * from finishing it by setting EH_SCHEDULED and return
332 * EH_NOT_HANDLED.
333 *
ad9e2762
TH
334 * TODO: kill this function once old EH is gone.
335 *
ece1d636
TH
336 * LOCKING:
337 * Called from timer context
338 *
339 * RETURNS:
340 * EH_HANDLED or EH_NOT_HANDLED
341 */
342enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
343{
344 struct Scsi_Host *host = cmd->device->host;
35bb94b1 345 struct ata_port *ap = ata_shost_to_port(host);
ece1d636
TH
346 unsigned long flags;
347 struct ata_queued_cmd *qc;
ad9e2762 348 enum scsi_eh_timer_return ret;
ece1d636
TH
349
350 DPRINTK("ENTER\n");
351
ad9e2762
TH
352 if (ap->ops->error_handler) {
353 ret = EH_NOT_HANDLED;
354 goto out;
355 }
356
357 ret = EH_HANDLED;
ba6a1308 358 spin_lock_irqsave(ap->lock, flags);
9af5c9c9 359 qc = ata_qc_from_tag(ap, ap->link.active_tag);
ece1d636
TH
360 if (qc) {
361 WARN_ON(qc->scsicmd != cmd);
362 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
363 qc->err_mask |= AC_ERR_TIMEOUT;
364 ret = EH_NOT_HANDLED;
365 }
ba6a1308 366 spin_unlock_irqrestore(ap->lock, flags);
ece1d636 367
ad9e2762 368 out:
ece1d636
TH
369 DPRINTK("EXIT, ret=%d\n", ret);
370 return ret;
371}
372
373/**
374 * ata_scsi_error - SCSI layer error handler callback
375 * @host: SCSI host on which error occurred
376 *
377 * Handles SCSI-layer-thrown error events.
378 *
379 * LOCKING:
380 * Inherited from SCSI layer (none, can sleep)
381 *
382 * RETURNS:
383 * Zero.
384 */
381544bb 385void ata_scsi_error(struct Scsi_Host *host)
ece1d636 386{
35bb94b1 387 struct ata_port *ap = ata_shost_to_port(host);
a1e10f7e 388 int i;
ad9e2762 389 unsigned long flags;
ece1d636
TH
390
391 DPRINTK("ENTER\n");
392
ad9e2762 393 /* synchronize with port task */
ece1d636
TH
394 ata_port_flush_task(ap);
395
cca3974e 396 /* synchronize with host lock and sort out timeouts */
ad9e2762
TH
397
398 /* For new EH, all qcs are finished in one of three ways -
399 * normal completion, error completion, and SCSI timeout.
400 * Both cmpletions can race against SCSI timeout. When normal
401 * completion wins, the qc never reaches EH. When error
402 * completion wins, the qc has ATA_QCFLAG_FAILED set.
403 *
404 * When SCSI timeout wins, things are a bit more complex.
405 * Normal or error completion can occur after the timeout but
406 * before this point. In such cases, both types of
407 * completions are honored. A scmd is determined to have
408 * timed out iff its associated qc is active and not failed.
409 */
410 if (ap->ops->error_handler) {
411 struct scsi_cmnd *scmd, *tmp;
412 int nr_timedout = 0;
413
e30349d2 414 spin_lock_irqsave(ap->lock, flags);
ad9e2762
TH
415
416 list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
417 struct ata_queued_cmd *qc;
418
419 for (i = 0; i < ATA_MAX_QUEUE; i++) {
420 qc = __ata_qc_from_tag(ap, i);
421 if (qc->flags & ATA_QCFLAG_ACTIVE &&
422 qc->scsicmd == scmd)
423 break;
424 }
425
426 if (i < ATA_MAX_QUEUE) {
427 /* the scmd has an associated qc */
428 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
429 /* which hasn't failed yet, timeout */
430 qc->err_mask |= AC_ERR_TIMEOUT;
431 qc->flags |= ATA_QCFLAG_FAILED;
432 nr_timedout++;
433 }
434 } else {
435 /* Normal completion occurred after
436 * SCSI timeout but before this point.
437 * Successfully complete it.
438 */
439 scmd->retries = scmd->allowed;
440 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
441 }
442 }
443
444 /* If we have timed out qcs. They belong to EH from
445 * this point but the state of the controller is
446 * unknown. Freeze the port to make sure the IRQ
447 * handler doesn't diddle with those qcs. This must
448 * be done atomically w.r.t. setting QCFLAG_FAILED.
449 */
450 if (nr_timedout)
451 __ata_port_freeze(ap);
452
e30349d2 453 spin_unlock_irqrestore(ap->lock, flags);
a1e10f7e
TH
454
455 /* initialize eh_tries */
456 ap->eh_tries = ATA_EH_MAX_TRIES;
ad9e2762 457 } else
e30349d2 458 spin_unlock_wait(ap->lock);
ad9e2762
TH
459
460 repeat:
461 /* invoke error handler */
462 if (ap->ops->error_handler) {
cf1b86c8
TH
463 struct ata_link *link;
464
5ddf24c5
TH
465 /* kill fast drain timer */
466 del_timer_sync(&ap->fastdrain_timer);
467
500530f6
TH
468 /* process port resume request */
469 ata_eh_handle_port_resume(ap);
470
f3e81b19 471 /* fetch & clear EH info */
e30349d2 472 spin_lock_irqsave(ap->lock, flags);
f3e81b19 473
cf1b86c8 474 __ata_port_for_each_link(link, ap) {
00115e0f
TH
475 struct ata_eh_context *ehc = &link->eh_context;
476 struct ata_device *dev;
477
cf1b86c8
TH
478 memset(&link->eh_context, 0, sizeof(link->eh_context));
479 link->eh_context.i = link->eh_info;
480 memset(&link->eh_info, 0, sizeof(link->eh_info));
00115e0f
TH
481
482 ata_link_for_each_dev(dev, link) {
483 int devno = dev->devno;
484
485 ehc->saved_xfer_mode[devno] = dev->xfer_mode;
486 if (ata_ncq_enabled(dev))
487 ehc->saved_ncq_enabled |= 1 << devno;
488 }
cf1b86c8 489 }
f3e81b19 490
b51e9e5d
TH
491 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
492 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
da917d69 493 ap->excl_link = NULL; /* don't maintain exclusion over EH */
f3e81b19 494
e30349d2 495 spin_unlock_irqrestore(ap->lock, flags);
ad9e2762 496
500530f6
TH
497 /* invoke EH, skip if unloading or suspended */
498 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
720ba126
TH
499 ap->ops->error_handler(ap);
500 else
501 ata_eh_finish(ap);
ad9e2762 502
500530f6
TH
503 /* process port suspend request */
504 ata_eh_handle_port_suspend(ap);
505
ad9e2762
TH
506 /* Exception might have happend after ->error_handler
507 * recovered the port but before this point. Repeat
508 * EH in such case.
509 */
e30349d2 510 spin_lock_irqsave(ap->lock, flags);
ad9e2762 511
b51e9e5d 512 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
a1e10f7e 513 if (--ap->eh_tries) {
e30349d2 514 spin_unlock_irqrestore(ap->lock, flags);
ad9e2762
TH
515 goto repeat;
516 }
517 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
a1e10f7e 518 "tries, giving up\n", ATA_EH_MAX_TRIES);
914616a3 519 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
ad9e2762
TH
520 }
521
f3e81b19 522 /* this run is complete, make sure EH info is clear */
cf1b86c8
TH
523 __ata_port_for_each_link(link, ap)
524 memset(&link->eh_info, 0, sizeof(link->eh_info));
f3e81b19 525
e30349d2 526 /* Clear host_eh_scheduled while holding ap->lock such
ad9e2762
TH
527 * that if exception occurs after this point but
528 * before EH completion, SCSI midlayer will
529 * re-initiate EH.
530 */
531 host->host_eh_scheduled = 0;
532
e30349d2 533 spin_unlock_irqrestore(ap->lock, flags);
ad9e2762 534 } else {
9af5c9c9 535 WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
ad9e2762
TH
536 ap->ops->eng_timeout(ap);
537 }
ece1d636 538
ad9e2762 539 /* finish or retry handled scmd's and clean up */
ece1d636
TH
540 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
541
542 scsi_eh_flush_done_q(&ap->eh_done_q);
543
ad9e2762 544 /* clean up */
e30349d2 545 spin_lock_irqsave(ap->lock, flags);
ad9e2762 546
1cdaf534 547 if (ap->pflags & ATA_PFLAG_LOADING)
b51e9e5d 548 ap->pflags &= ~ATA_PFLAG_LOADING;
1cdaf534 549 else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
52bad64d 550 queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0);
1cdaf534
TH
551
552 if (ap->pflags & ATA_PFLAG_RECOVERED)
553 ata_port_printk(ap, KERN_INFO, "EH complete\n");
580b2102 554
b51e9e5d 555 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
ad9e2762 556
c6cf9e99 557 /* tell wait_eh that we're done */
b51e9e5d 558 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
c6cf9e99
TH
559 wake_up_all(&ap->eh_wait_q);
560
e30349d2 561 spin_unlock_irqrestore(ap->lock, flags);
ad9e2762 562
ece1d636 563 DPRINTK("EXIT\n");
ece1d636
TH
564}
565
c6cf9e99
TH
566/**
567 * ata_port_wait_eh - Wait for the currently pending EH to complete
568 * @ap: Port to wait EH for
569 *
570 * Wait until the currently pending EH is complete.
571 *
572 * LOCKING:
573 * Kernel thread context (may sleep).
574 */
575void ata_port_wait_eh(struct ata_port *ap)
576{
577 unsigned long flags;
578 DEFINE_WAIT(wait);
579
580 retry:
ba6a1308 581 spin_lock_irqsave(ap->lock, flags);
c6cf9e99 582
b51e9e5d 583 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
c6cf9e99 584 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
ba6a1308 585 spin_unlock_irqrestore(ap->lock, flags);
c6cf9e99 586 schedule();
ba6a1308 587 spin_lock_irqsave(ap->lock, flags);
c6cf9e99 588 }
0a1b622e 589 finish_wait(&ap->eh_wait_q, &wait);
c6cf9e99 590
ba6a1308 591 spin_unlock_irqrestore(ap->lock, flags);
c6cf9e99
TH
592
593 /* make sure SCSI EH is complete */
cca3974e 594 if (scsi_host_in_recovery(ap->scsi_host)) {
c6cf9e99
TH
595 msleep(10);
596 goto retry;
597 }
598}
599
5ddf24c5
TH
600static int ata_eh_nr_in_flight(struct ata_port *ap)
601{
602 unsigned int tag;
603 int nr = 0;
604
605 /* count only non-internal commands */
606 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++)
607 if (ata_qc_from_tag(ap, tag))
608 nr++;
609
610 return nr;
611}
612
613void ata_eh_fastdrain_timerfn(unsigned long arg)
614{
615 struct ata_port *ap = (void *)arg;
616 unsigned long flags;
617 int cnt;
618
619 spin_lock_irqsave(ap->lock, flags);
620
621 cnt = ata_eh_nr_in_flight(ap);
622
623 /* are we done? */
624 if (!cnt)
625 goto out_unlock;
626
627 if (cnt == ap->fastdrain_cnt) {
628 unsigned int tag;
629
630 /* No progress during the last interval, tag all
631 * in-flight qcs as timed out and freeze the port.
632 */
633 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) {
634 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
635 if (qc)
636 qc->err_mask |= AC_ERR_TIMEOUT;
637 }
638
639 ata_port_freeze(ap);
640 } else {
641 /* some qcs have finished, give it another chance */
642 ap->fastdrain_cnt = cnt;
643 ap->fastdrain_timer.expires =
644 jiffies + ATA_EH_FASTDRAIN_INTERVAL;
645 add_timer(&ap->fastdrain_timer);
646 }
647
648 out_unlock:
649 spin_unlock_irqrestore(ap->lock, flags);
650}
651
652/**
653 * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
654 * @ap: target ATA port
655 * @fastdrain: activate fast drain
656 *
657 * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
658 * is non-zero and EH wasn't pending before. Fast drain ensures
659 * that EH kicks in in timely manner.
660 *
661 * LOCKING:
662 * spin_lock_irqsave(host lock)
663 */
664static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
665{
666 int cnt;
667
668 /* already scheduled? */
669 if (ap->pflags & ATA_PFLAG_EH_PENDING)
670 return;
671
672 ap->pflags |= ATA_PFLAG_EH_PENDING;
673
674 if (!fastdrain)
675 return;
676
677 /* do we have in-flight qcs? */
678 cnt = ata_eh_nr_in_flight(ap);
679 if (!cnt)
680 return;
681
682 /* activate fast drain */
683 ap->fastdrain_cnt = cnt;
684 ap->fastdrain_timer.expires = jiffies + ATA_EH_FASTDRAIN_INTERVAL;
685 add_timer(&ap->fastdrain_timer);
686}
687
f686bcb8
TH
688/**
689 * ata_qc_schedule_eh - schedule qc for error handling
690 * @qc: command to schedule error handling for
691 *
692 * Schedule error handling for @qc. EH will kick in as soon as
693 * other commands are drained.
694 *
695 * LOCKING:
cca3974e 696 * spin_lock_irqsave(host lock)
f686bcb8
TH
697 */
698void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
699{
700 struct ata_port *ap = qc->ap;
701
702 WARN_ON(!ap->ops->error_handler);
703
704 qc->flags |= ATA_QCFLAG_FAILED;
5ddf24c5 705 ata_eh_set_pending(ap, 1);
f686bcb8
TH
706
707 /* The following will fail if timeout has already expired.
708 * ata_scsi_error() takes care of such scmds on EH entry.
709 * Note that ATA_QCFLAG_FAILED is unconditionally set after
710 * this function completes.
711 */
712 scsi_req_abort_cmd(qc->scsicmd);
713}
714
7b70fc03
TH
715/**
716 * ata_port_schedule_eh - schedule error handling without a qc
717 * @ap: ATA port to schedule EH for
718 *
719 * Schedule error handling for @ap. EH will kick in as soon as
720 * all commands are drained.
721 *
722 * LOCKING:
cca3974e 723 * spin_lock_irqsave(host lock)
7b70fc03
TH
724 */
725void ata_port_schedule_eh(struct ata_port *ap)
726{
727 WARN_ON(!ap->ops->error_handler);
728
f4d6d004
TH
729 if (ap->pflags & ATA_PFLAG_INITIALIZING)
730 return;
731
5ddf24c5 732 ata_eh_set_pending(ap, 1);
cca3974e 733 scsi_schedule_eh(ap->scsi_host);
7b70fc03
TH
734
735 DPRINTK("port EH scheduled\n");
736}
737
dbd82616 738static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
7b70fc03
TH
739{
740 int tag, nr_aborted = 0;
741
742 WARN_ON(!ap->ops->error_handler);
743
5ddf24c5
TH
744 /* we're gonna abort all commands, no need for fast drain */
745 ata_eh_set_pending(ap, 0);
746
7b70fc03
TH
747 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
748 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
749
dbd82616 750 if (qc && (!link || qc->dev->link == link)) {
7b70fc03
TH
751 qc->flags |= ATA_QCFLAG_FAILED;
752 ata_qc_complete(qc);
753 nr_aborted++;
754 }
755 }
756
757 if (!nr_aborted)
758 ata_port_schedule_eh(ap);
759
760 return nr_aborted;
761}
762
dbd82616
TH
763/**
764 * ata_link_abort - abort all qc's on the link
765 * @link: ATA link to abort qc's for
766 *
767 * Abort all active qc's active on @link and schedule EH.
768 *
769 * LOCKING:
770 * spin_lock_irqsave(host lock)
771 *
772 * RETURNS:
773 * Number of aborted qc's.
774 */
775int ata_link_abort(struct ata_link *link)
776{
777 return ata_do_link_abort(link->ap, link);
778}
779
780/**
781 * ata_port_abort - abort all qc's on the port
782 * @ap: ATA port to abort qc's for
783 *
784 * Abort all active qc's of @ap and schedule EH.
785 *
786 * LOCKING:
787 * spin_lock_irqsave(host_set lock)
788 *
789 * RETURNS:
790 * Number of aborted qc's.
791 */
792int ata_port_abort(struct ata_port *ap)
793{
794 return ata_do_link_abort(ap, NULL);
795}
796
e3180499
TH
797/**
798 * __ata_port_freeze - freeze port
799 * @ap: ATA port to freeze
800 *
801 * This function is called when HSM violation or some other
802 * condition disrupts normal operation of the port. Frozen port
803 * is not allowed to perform any operation until the port is
804 * thawed, which usually follows a successful reset.
805 *
806 * ap->ops->freeze() callback can be used for freezing the port
807 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
808 * port cannot be frozen hardware-wise, the interrupt handler
809 * must ack and clear interrupts unconditionally while the port
810 * is frozen.
811 *
812 * LOCKING:
cca3974e 813 * spin_lock_irqsave(host lock)
e3180499
TH
814 */
815static void __ata_port_freeze(struct ata_port *ap)
816{
817 WARN_ON(!ap->ops->error_handler);
818
819 if (ap->ops->freeze)
820 ap->ops->freeze(ap);
821
b51e9e5d 822 ap->pflags |= ATA_PFLAG_FROZEN;
e3180499 823
44877b4e 824 DPRINTK("ata%u port frozen\n", ap->print_id);
e3180499
TH
825}
826
827/**
828 * ata_port_freeze - abort & freeze port
829 * @ap: ATA port to freeze
830 *
831 * Abort and freeze @ap.
832 *
833 * LOCKING:
cca3974e 834 * spin_lock_irqsave(host lock)
e3180499
TH
835 *
836 * RETURNS:
837 * Number of aborted commands.
838 */
839int ata_port_freeze(struct ata_port *ap)
840{
841 int nr_aborted;
842
843 WARN_ON(!ap->ops->error_handler);
844
845 nr_aborted = ata_port_abort(ap);
846 __ata_port_freeze(ap);
847
848 return nr_aborted;
849}
850
7d77b247
TH
851/**
852 * sata_async_notification - SATA async notification handler
853 * @ap: ATA port where async notification is received
854 *
855 * Handler to be called when async notification via SDB FIS is
856 * received. This function schedules EH if necessary.
857 *
858 * LOCKING:
859 * spin_lock_irqsave(host lock)
860 *
861 * RETURNS:
862 * 1 if EH is scheduled, 0 otherwise.
863 */
864int sata_async_notification(struct ata_port *ap)
865{
866 u32 sntf;
867 int rc;
868
869 if (!(ap->flags & ATA_FLAG_AN))
870 return 0;
871
872 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
873 if (rc == 0)
874 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
875
071f44b1 876 if (!sata_pmp_attached(ap) || rc) {
7d77b247 877 /* PMP is not attached or SNTF is not available */
071f44b1 878 if (!sata_pmp_attached(ap)) {
7d77b247
TH
879 /* PMP is not attached. Check whether ATAPI
880 * AN is configured. If so, notify media
881 * change.
882 */
883 struct ata_device *dev = ap->link.device;
884
885 if ((dev->class == ATA_DEV_ATAPI) &&
886 (dev->flags & ATA_DFLAG_AN))
887 ata_scsi_media_change_notify(dev);
888 return 0;
889 } else {
890 /* PMP is attached but SNTF is not available.
891 * ATAPI async media change notification is
892 * not used. The PMP must be reporting PHY
893 * status change, schedule EH.
894 */
895 ata_port_schedule_eh(ap);
896 return 1;
897 }
898 } else {
899 /* PMP is attached and SNTF is available */
900 struct ata_link *link;
901
902 /* check and notify ATAPI AN */
903 ata_port_for_each_link(link, ap) {
904 if (!(sntf & (1 << link->pmp)))
905 continue;
906
907 if ((link->device->class == ATA_DEV_ATAPI) &&
908 (link->device->flags & ATA_DFLAG_AN))
909 ata_scsi_media_change_notify(link->device);
910 }
911
912 /* If PMP is reporting that PHY status of some
913 * downstream ports has changed, schedule EH.
914 */
915 if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
916 ata_port_schedule_eh(ap);
917 return 1;
918 }
919
920 return 0;
921 }
922}
923
e3180499
TH
924/**
925 * ata_eh_freeze_port - EH helper to freeze port
926 * @ap: ATA port to freeze
927 *
928 * Freeze @ap.
929 *
930 * LOCKING:
931 * None.
932 */
933void ata_eh_freeze_port(struct ata_port *ap)
934{
935 unsigned long flags;
936
937 if (!ap->ops->error_handler)
938 return;
939
ba6a1308 940 spin_lock_irqsave(ap->lock, flags);
e3180499 941 __ata_port_freeze(ap);
ba6a1308 942 spin_unlock_irqrestore(ap->lock, flags);
e3180499
TH
943}
944
945/**
946 * ata_port_thaw_port - EH helper to thaw port
947 * @ap: ATA port to thaw
948 *
949 * Thaw frozen port @ap.
950 *
951 * LOCKING:
952 * None.
953 */
954void ata_eh_thaw_port(struct ata_port *ap)
955{
956 unsigned long flags;
957
958 if (!ap->ops->error_handler)
959 return;
960
ba6a1308 961 spin_lock_irqsave(ap->lock, flags);
e3180499 962
b51e9e5d 963 ap->pflags &= ~ATA_PFLAG_FROZEN;
e3180499
TH
964
965 if (ap->ops->thaw)
966 ap->ops->thaw(ap);
967
ba6a1308 968 spin_unlock_irqrestore(ap->lock, flags);
e3180499 969
44877b4e 970 DPRINTK("ata%u port thawed\n", ap->print_id);
e3180499
TH
971}
972
ece1d636
TH
973static void ata_eh_scsidone(struct scsi_cmnd *scmd)
974{
975 /* nada */
976}
977
978static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
979{
980 struct ata_port *ap = qc->ap;
981 struct scsi_cmnd *scmd = qc->scsicmd;
982 unsigned long flags;
983
ba6a1308 984 spin_lock_irqsave(ap->lock, flags);
ece1d636
TH
985 qc->scsidone = ata_eh_scsidone;
986 __ata_qc_complete(qc);
987 WARN_ON(ata_tag_valid(qc->tag));
ba6a1308 988 spin_unlock_irqrestore(ap->lock, flags);
ece1d636
TH
989
990 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
991}
992
993/**
994 * ata_eh_qc_complete - Complete an active ATA command from EH
995 * @qc: Command to complete
996 *
997 * Indicate to the mid and upper layers that an ATA command has
998 * completed. To be used from EH.
999 */
1000void ata_eh_qc_complete(struct ata_queued_cmd *qc)
1001{
1002 struct scsi_cmnd *scmd = qc->scsicmd;
1003 scmd->retries = scmd->allowed;
1004 __ata_eh_qc_complete(qc);
1005}
1006
1007/**
1008 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
1009 * @qc: Command to retry
1010 *
1011 * Indicate to the mid and upper layers that an ATA command
1012 * should be retried. To be used from EH.
1013 *
1014 * SCSI midlayer limits the number of retries to scmd->allowed.
1015 * scmd->retries is decremented for commands which get retried
1016 * due to unrelated failures (qc->err_mask is zero).
1017 */
1018void ata_eh_qc_retry(struct ata_queued_cmd *qc)
1019{
1020 struct scsi_cmnd *scmd = qc->scsicmd;
1021 if (!qc->err_mask && scmd->retries)
1022 scmd->retries--;
1023 __ata_eh_qc_complete(qc);
1024}
022bdb07 1025
0ea035a3
TH
1026/**
1027 * ata_eh_detach_dev - detach ATA device
1028 * @dev: ATA device to detach
1029 *
1030 * Detach @dev.
1031 *
1032 * LOCKING:
1033 * None.
1034 */
fb7fd614 1035void ata_eh_detach_dev(struct ata_device *dev)
0ea035a3 1036{
f58229f8
TH
1037 struct ata_link *link = dev->link;
1038 struct ata_port *ap = link->ap;
0ea035a3
TH
1039 unsigned long flags;
1040
1041 ata_dev_disable(dev);
1042
ba6a1308 1043 spin_lock_irqsave(ap->lock, flags);
0ea035a3
TH
1044
1045 dev->flags &= ~ATA_DFLAG_DETACH;
1046
1047 if (ata_scsi_offline_dev(dev)) {
1048 dev->flags |= ATA_DFLAG_DETACHED;
b51e9e5d 1049 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
0ea035a3
TH
1050 }
1051
beb07c1a 1052 /* clear per-dev EH actions */
f58229f8
TH
1053 ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
1054 ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
beb07c1a 1055
ba6a1308 1056 spin_unlock_irqrestore(ap->lock, flags);
0ea035a3
TH
1057}
1058
022bdb07
TH
1059/**
1060 * ata_eh_about_to_do - about to perform eh_action
955e57df 1061 * @link: target ATA link
47005f25 1062 * @dev: target ATA dev for per-dev action (can be NULL)
022bdb07
TH
1063 * @action: action about to be performed
1064 *
1065 * Called just before performing EH actions to clear related bits
955e57df
TH
1066 * in @link->eh_info such that eh actions are not unnecessarily
1067 * repeated.
022bdb07
TH
1068 *
1069 * LOCKING:
1070 * None.
1071 */
fb7fd614
TH
1072void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
1073 unsigned int action)
022bdb07 1074{
955e57df
TH
1075 struct ata_port *ap = link->ap;
1076 struct ata_eh_info *ehi = &link->eh_info;
1077 struct ata_eh_context *ehc = &link->eh_context;
022bdb07
TH
1078 unsigned long flags;
1079
ba6a1308 1080 spin_lock_irqsave(ap->lock, flags);
1cdaf534 1081
955e57df 1082 ata_eh_clear_action(link, dev, ehi, action);
1cdaf534 1083
13abf50d 1084 if (!(ehc->i.flags & ATA_EHI_QUIET))
1cdaf534
TH
1085 ap->pflags |= ATA_PFLAG_RECOVERED;
1086
ba6a1308 1087 spin_unlock_irqrestore(ap->lock, flags);
022bdb07
TH
1088}
1089
47005f25
TH
1090/**
1091 * ata_eh_done - EH action complete
955e57df 1092* @ap: target ATA port
47005f25
TH
1093 * @dev: target ATA dev for per-dev action (can be NULL)
1094 * @action: action just completed
1095 *
1096 * Called right after performing EH actions to clear related bits
955e57df 1097 * in @link->eh_context.
47005f25
TH
1098 *
1099 * LOCKING:
1100 * None.
1101 */
fb7fd614
TH
1102void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1103 unsigned int action)
47005f25 1104{
955e57df 1105 struct ata_eh_context *ehc = &link->eh_context;
9af5c9c9 1106
955e57df 1107 ata_eh_clear_action(link, dev, &ehc->i, action);
47005f25
TH
1108}
1109
022bdb07
TH
1110/**
1111 * ata_err_string - convert err_mask to descriptive string
1112 * @err_mask: error mask to convert to string
1113 *
1114 * Convert @err_mask to descriptive string. Errors are
1115 * prioritized according to severity and only the most severe
1116 * error is reported.
1117 *
1118 * LOCKING:
1119 * None.
1120 *
1121 * RETURNS:
1122 * Descriptive string for @err_mask
1123 */
2dcb407e 1124static const char *ata_err_string(unsigned int err_mask)
022bdb07
TH
1125{
1126 if (err_mask & AC_ERR_HOST_BUS)
1127 return "host bus error";
1128 if (err_mask & AC_ERR_ATA_BUS)
1129 return "ATA bus error";
1130 if (err_mask & AC_ERR_TIMEOUT)
1131 return "timeout";
1132 if (err_mask & AC_ERR_HSM)
1133 return "HSM violation";
1134 if (err_mask & AC_ERR_SYSTEM)
1135 return "internal error";
1136 if (err_mask & AC_ERR_MEDIA)
1137 return "media error";
1138 if (err_mask & AC_ERR_INVALID)
1139 return "invalid argument";
1140 if (err_mask & AC_ERR_DEV)
1141 return "device error";
1142 return "unknown error";
1143}
1144
e8ee8451
TH
1145/**
1146 * ata_read_log_page - read a specific log page
1147 * @dev: target device
1148 * @page: page to read
1149 * @buf: buffer to store read page
1150 * @sectors: number of sectors to read
1151 *
1152 * Read log page using READ_LOG_EXT command.
1153 *
1154 * LOCKING:
1155 * Kernel thread context (may sleep).
1156 *
1157 * RETURNS:
1158 * 0 on success, AC_ERR_* mask otherwise.
1159 */
1160static unsigned int ata_read_log_page(struct ata_device *dev,
1161 u8 page, void *buf, unsigned int sectors)
1162{
1163 struct ata_taskfile tf;
1164 unsigned int err_mask;
1165
1166 DPRINTK("read log page - page %d\n", page);
1167
1168 ata_tf_init(dev, &tf);
1169 tf.command = ATA_CMD_READ_LOG_EXT;
1170 tf.lbal = page;
1171 tf.nsect = sectors;
1172 tf.hob_nsect = sectors >> 8;
1173 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1174 tf.protocol = ATA_PROT_PIO;
1175
1176 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
2b789108 1177 buf, sectors * ATA_SECT_SIZE, 0);
e8ee8451
TH
1178
1179 DPRINTK("EXIT, err_mask=%x\n", err_mask);
1180 return err_mask;
1181}
1182
1183/**
1184 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
1185 * @dev: Device to read log page 10h from
1186 * @tag: Resulting tag of the failed command
1187 * @tf: Resulting taskfile registers of the failed command
1188 *
1189 * Read log page 10h to obtain NCQ error details and clear error
1190 * condition.
1191 *
1192 * LOCKING:
1193 * Kernel thread context (may sleep).
1194 *
1195 * RETURNS:
1196 * 0 on success, -errno otherwise.
1197 */
1198static int ata_eh_read_log_10h(struct ata_device *dev,
1199 int *tag, struct ata_taskfile *tf)
1200{
9af5c9c9 1201 u8 *buf = dev->link->ap->sector_buf;
e8ee8451
TH
1202 unsigned int err_mask;
1203 u8 csum;
1204 int i;
1205
1206 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
1207 if (err_mask)
1208 return -EIO;
1209
1210 csum = 0;
1211 for (i = 0; i < ATA_SECT_SIZE; i++)
1212 csum += buf[i];
1213 if (csum)
1214 ata_dev_printk(dev, KERN_WARNING,
1215 "invalid checksum 0x%x on log page 10h\n", csum);
1216
1217 if (buf[0] & 0x80)
1218 return -ENOENT;
1219
1220 *tag = buf[0] & 0x1f;
1221
1222 tf->command = buf[2];
1223 tf->feature = buf[3];
1224 tf->lbal = buf[4];
1225 tf->lbam = buf[5];
1226 tf->lbah = buf[6];
1227 tf->device = buf[7];
1228 tf->hob_lbal = buf[8];
1229 tf->hob_lbam = buf[9];
1230 tf->hob_lbah = buf[10];
1231 tf->nsect = buf[12];
1232 tf->hob_nsect = buf[13];
1233
1234 return 0;
1235}
1236
022bdb07
TH
1237/**
1238 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1239 * @dev: device to perform REQUEST_SENSE to
1240 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
1241 *
1242 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
1243 * SENSE. This function is EH helper.
1244 *
1245 * LOCKING:
1246 * Kernel thread context (may sleep).
1247 *
1248 * RETURNS:
1249 * 0 on success, AC_ERR_* mask on failure
1250 */
56287768 1251static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc)
022bdb07 1252{
56287768
AL
1253 struct ata_device *dev = qc->dev;
1254 unsigned char *sense_buf = qc->scsicmd->sense_buffer;
9af5c9c9 1255 struct ata_port *ap = dev->link->ap;
022bdb07
TH
1256 struct ata_taskfile tf;
1257 u8 cdb[ATAPI_CDB_LEN];
1258
1259 DPRINTK("ATAPI request sense\n");
1260
022bdb07
TH
1261 /* FIXME: is this needed? */
1262 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1263
56287768
AL
1264 /* initialize sense_buf with the error register,
1265 * for the case where they are -not- overwritten
1266 */
022bdb07 1267 sense_buf[0] = 0x70;
56287768
AL
1268 sense_buf[2] = qc->result_tf.feature >> 4;
1269
a617c09f 1270 /* some devices time out if garbage left in tf */
56287768 1271 ata_tf_init(dev, &tf);
022bdb07
TH
1272
1273 memset(cdb, 0, ATAPI_CDB_LEN);
1274 cdb[0] = REQUEST_SENSE;
1275 cdb[4] = SCSI_SENSE_BUFFERSIZE;
1276
1277 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1278 tf.command = ATA_CMD_PACKET;
1279
1280 /* is it pointless to prefer PIO for "safety reasons"? */
1281 if (ap->flags & ATA_FLAG_PIO_DMA) {
0dc36888 1282 tf.protocol = ATAPI_PROT_DMA;
022bdb07
TH
1283 tf.feature |= ATAPI_PKT_DMA;
1284 } else {
0dc36888 1285 tf.protocol = ATAPI_PROT_PIO;
f2dfc1a1
TH
1286 tf.lbam = SCSI_SENSE_BUFFERSIZE;
1287 tf.lbah = 0;
022bdb07
TH
1288 }
1289
1290 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
2b789108 1291 sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
022bdb07
TH
1292}
1293
1294/**
1295 * ata_eh_analyze_serror - analyze SError for a failed port
0260731f 1296 * @link: ATA link to analyze SError for
022bdb07
TH
1297 *
1298 * Analyze SError if available and further determine cause of
1299 * failure.
1300 *
1301 * LOCKING:
1302 * None.
1303 */
0260731f 1304static void ata_eh_analyze_serror(struct ata_link *link)
022bdb07 1305{
0260731f 1306 struct ata_eh_context *ehc = &link->eh_context;
022bdb07
TH
1307 u32 serror = ehc->i.serror;
1308 unsigned int err_mask = 0, action = 0;
f9df58cb 1309 u32 hotplug_mask;
022bdb07
TH
1310
1311 if (serror & SERR_PERSISTENT) {
1312 err_mask |= AC_ERR_ATA_BUS;
cf480626 1313 action |= ATA_EH_RESET;
022bdb07
TH
1314 }
1315 if (serror &
1316 (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
1317 err_mask |= AC_ERR_ATA_BUS;
cf480626 1318 action |= ATA_EH_RESET;
022bdb07
TH
1319 }
1320 if (serror & SERR_PROTOCOL) {
1321 err_mask |= AC_ERR_HSM;
cf480626 1322 action |= ATA_EH_RESET;
022bdb07
TH
1323 }
1324 if (serror & SERR_INTERNAL) {
1325 err_mask |= AC_ERR_SYSTEM;
cf480626 1326 action |= ATA_EH_RESET;
022bdb07 1327 }
f9df58cb
TH
1328
1329 /* Determine whether a hotplug event has occurred. Both
1330 * SError.N/X are considered hotplug events for enabled or
1331 * host links. For disabled PMP links, only N bit is
1332 * considered as X bit is left at 1 for link plugging.
1333 */
1334 hotplug_mask = 0;
1335
1336 if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
1337 hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
1338 else
1339 hotplug_mask = SERR_PHYRDY_CHG;
1340
1341 if (serror & hotplug_mask)
084fe639 1342 ata_ehi_hotplugged(&ehc->i);
022bdb07
TH
1343
1344 ehc->i.err_mask |= err_mask;
1345 ehc->i.action |= action;
1346}
1347
e8ee8451
TH
1348/**
1349 * ata_eh_analyze_ncq_error - analyze NCQ error
0260731f 1350 * @link: ATA link to analyze NCQ error for
e8ee8451
TH
1351 *
1352 * Read log page 10h, determine the offending qc and acquire
1353 * error status TF. For NCQ device errors, all LLDDs have to do
1354 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1355 * care of the rest.
1356 *
1357 * LOCKING:
1358 * Kernel thread context (may sleep).
1359 */
0260731f 1360static void ata_eh_analyze_ncq_error(struct ata_link *link)
e8ee8451 1361{
0260731f
TH
1362 struct ata_port *ap = link->ap;
1363 struct ata_eh_context *ehc = &link->eh_context;
1364 struct ata_device *dev = link->device;
e8ee8451
TH
1365 struct ata_queued_cmd *qc;
1366 struct ata_taskfile tf;
1367 int tag, rc;
1368
1369 /* if frozen, we can't do much */
b51e9e5d 1370 if (ap->pflags & ATA_PFLAG_FROZEN)
e8ee8451
TH
1371 return;
1372
1373 /* is it NCQ device error? */
0260731f 1374 if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
e8ee8451
TH
1375 return;
1376
1377 /* has LLDD analyzed already? */
1378 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1379 qc = __ata_qc_from_tag(ap, tag);
1380
1381 if (!(qc->flags & ATA_QCFLAG_FAILED))
1382 continue;
1383
1384 if (qc->err_mask)
1385 return;
1386 }
1387
1388 /* okay, this error is ours */
1389 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1390 if (rc) {
0260731f 1391 ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
e8ee8451
TH
1392 "(errno=%d)\n", rc);
1393 return;
1394 }
1395
0260731f
TH
1396 if (!(link->sactive & (1 << tag))) {
1397 ata_link_printk(link, KERN_ERR, "log page 10h reported "
e8ee8451
TH
1398 "inactive tag %d\n", tag);
1399 return;
1400 }
1401
1402 /* we've got the perpetrator, condemn it */
1403 qc = __ata_qc_from_tag(ap, tag);
1404 memcpy(&qc->result_tf, &tf, sizeof(tf));
5335b729 1405 qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
e8ee8451
TH
1406 ehc->i.err_mask &= ~AC_ERR_DEV;
1407}
1408
022bdb07
TH
1409/**
1410 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1411 * @qc: qc to analyze
1412 * @tf: Taskfile registers to analyze
1413 *
1414 * Analyze taskfile of @qc and further determine cause of
1415 * failure. This function also requests ATAPI sense data if
1416 * avaliable.
1417 *
1418 * LOCKING:
1419 * Kernel thread context (may sleep).
1420 *
1421 * RETURNS:
1422 * Determined recovery action
1423 */
1424static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1425 const struct ata_taskfile *tf)
1426{
1427 unsigned int tmp, action = 0;
1428 u8 stat = tf->command, err = tf->feature;
1429
1430 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1431 qc->err_mask |= AC_ERR_HSM;
cf480626 1432 return ATA_EH_RESET;
022bdb07
TH
1433 }
1434
a51d644a
TH
1435 if (stat & (ATA_ERR | ATA_DF))
1436 qc->err_mask |= AC_ERR_DEV;
1437 else
022bdb07
TH
1438 return 0;
1439
1440 switch (qc->dev->class) {
1441 case ATA_DEV_ATA:
1442 if (err & ATA_ICRC)
1443 qc->err_mask |= AC_ERR_ATA_BUS;
1444 if (err & ATA_UNC)
1445 qc->err_mask |= AC_ERR_MEDIA;
1446 if (err & ATA_IDNF)
1447 qc->err_mask |= AC_ERR_INVALID;
1448 break;
1449
1450 case ATA_DEV_ATAPI:
a569a30d 1451 if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
56287768 1452 tmp = atapi_eh_request_sense(qc);
a569a30d
TH
1453 if (!tmp) {
1454 /* ATA_QCFLAG_SENSE_VALID is used to
1455 * tell atapi_qc_complete() that sense
1456 * data is already valid.
1457 *
1458 * TODO: interpret sense data and set
1459 * appropriate err_mask.
1460 */
1461 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1462 } else
1463 qc->err_mask |= tmp;
1464 }
022bdb07
TH
1465 }
1466
1467 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
cf480626 1468 action |= ATA_EH_RESET;
022bdb07
TH
1469
1470 return action;
1471}
1472
76326ac1
TH
1473static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask,
1474 int *xfer_ok)
022bdb07 1475{
76326ac1
TH
1476 int base = 0;
1477
1478 if (!(eflags & ATA_EFLAG_DUBIOUS_XFER))
1479 *xfer_ok = 1;
1480
1481 if (!*xfer_ok)
75f9cafc 1482 base = ATA_ECAT_DUBIOUS_NONE;
76326ac1 1483
7d47e8d4 1484 if (err_mask & AC_ERR_ATA_BUS)
76326ac1 1485 return base + ATA_ECAT_ATA_BUS;
022bdb07 1486
7d47e8d4 1487 if (err_mask & AC_ERR_TIMEOUT)
76326ac1 1488 return base + ATA_ECAT_TOUT_HSM;
7d47e8d4 1489
3884f7b0 1490 if (eflags & ATA_EFLAG_IS_IO) {
7d47e8d4 1491 if (err_mask & AC_ERR_HSM)
76326ac1 1492 return base + ATA_ECAT_TOUT_HSM;
7d47e8d4
TH
1493 if ((err_mask &
1494 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
76326ac1 1495 return base + ATA_ECAT_UNK_DEV;
022bdb07
TH
1496 }
1497
1498 return 0;
1499}
1500
7d47e8d4 1501struct speed_down_verdict_arg {
022bdb07 1502 u64 since;
76326ac1 1503 int xfer_ok;
3884f7b0 1504 int nr_errors[ATA_ECAT_NR];
022bdb07
TH
1505};
1506
7d47e8d4 1507static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
022bdb07 1508{
7d47e8d4 1509 struct speed_down_verdict_arg *arg = void_arg;
76326ac1 1510 int cat;
022bdb07
TH
1511
1512 if (ent->timestamp < arg->since)
1513 return -1;
1514
76326ac1
TH
1515 cat = ata_eh_categorize_error(ent->eflags, ent->err_mask,
1516 &arg->xfer_ok);
7d47e8d4 1517 arg->nr_errors[cat]++;
76326ac1 1518
022bdb07
TH
1519 return 0;
1520}
1521
1522/**
7d47e8d4 1523 * ata_eh_speed_down_verdict - Determine speed down verdict
022bdb07
TH
1524 * @dev: Device of interest
1525 *
1526 * This function examines error ring of @dev and determines
7d47e8d4
TH
1527 * whether NCQ needs to be turned off, transfer speed should be
1528 * stepped down, or falling back to PIO is necessary.
022bdb07 1529 *
3884f7b0
TH
1530 * ECAT_ATA_BUS : ATA_BUS error for any command
1531 *
1532 * ECAT_TOUT_HSM : TIMEOUT for any command or HSM violation for
1533 * IO commands
1534 *
1535 * ECAT_UNK_DEV : Unknown DEV error for IO commands
1536 *
76326ac1
TH
1537 * ECAT_DUBIOUS_* : Identical to above three but occurred while
1538 * data transfer hasn't been verified.
1539 *
3884f7b0
TH
1540 * Verdicts are
1541 *
1542 * NCQ_OFF : Turn off NCQ.
022bdb07 1543 *
3884f7b0
TH
1544 * SPEED_DOWN : Speed down transfer speed but don't fall back
1545 * to PIO.
7d47e8d4 1546 *
3884f7b0 1547 * FALLBACK_TO_PIO : Fall back to PIO.
022bdb07 1548 *
3884f7b0 1549 * Even if multiple verdicts are returned, only one action is
76326ac1
TH
1550 * taken per error. An action triggered by non-DUBIOUS errors
1551 * clears ering, while one triggered by DUBIOUS_* errors doesn't.
1552 * This is to expedite speed down decisions right after device is
1553 * initially configured.
1554 *
1555 * The followings are speed down rules. #1 and #2 deal with
1556 * DUBIOUS errors.
7d47e8d4 1557 *
76326ac1
TH
1558 * 1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors
1559 * occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO.
1560 *
1561 * 2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors
1562 * occurred during last 5 mins, NCQ_OFF.
1563 *
1564 * 3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors
3884f7b0 1565 * ocurred during last 5 mins, FALLBACK_TO_PIO
7d47e8d4 1566 *
76326ac1 1567 * 4. If more than 3 TOUT_HSM or UNK_DEV errors occurred
3884f7b0
TH
1568 * during last 10 mins, NCQ_OFF.
1569 *
76326ac1 1570 * 5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6
3884f7b0 1571 * UNK_DEV errors occurred during last 10 mins, SPEED_DOWN.
7d47e8d4 1572 *
022bdb07
TH
1573 * LOCKING:
1574 * Inherited from caller.
1575 *
1576 * RETURNS:
7d47e8d4 1577 * OR of ATA_EH_SPDN_* flags.
022bdb07 1578 */
7d47e8d4 1579static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
022bdb07 1580{
7d47e8d4
TH
1581 const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
1582 u64 j64 = get_jiffies_64();
1583 struct speed_down_verdict_arg arg;
1584 unsigned int verdict = 0;
022bdb07 1585
3884f7b0 1586 /* scan past 5 mins of error history */
7d47e8d4 1587 memset(&arg, 0, sizeof(arg));
3884f7b0 1588 arg.since = j64 - min(j64, j5mins);
7d47e8d4 1589 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
022bdb07 1590
76326ac1
TH
1591 if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] +
1592 arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1)
1593 verdict |= ATA_EH_SPDN_SPEED_DOWN |
1594 ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS;
1595
1596 if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] +
1597 arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1)
1598 verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS;
1599
3884f7b0
TH
1600 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1601 arg.nr_errors[ATA_ECAT_TOUT_HSM] +
663f99b8 1602 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
3884f7b0 1603 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
022bdb07 1604
3884f7b0 1605 /* scan past 10 mins of error history */
022bdb07 1606 memset(&arg, 0, sizeof(arg));
3884f7b0 1607 arg.since = j64 - min(j64, j10mins);
7d47e8d4 1608 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
022bdb07 1609
3884f7b0
TH
1610 if (arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1611 arg.nr_errors[ATA_ECAT_UNK_DEV] > 3)
1612 verdict |= ATA_EH_SPDN_NCQ_OFF;
1613
1614 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1615 arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 ||
663f99b8 1616 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
3884f7b0 1617 verdict |= ATA_EH_SPDN_SPEED_DOWN;
022bdb07 1618
7d47e8d4 1619 return verdict;
022bdb07
TH
1620}
1621
1622/**
1623 * ata_eh_speed_down - record error and speed down if necessary
1624 * @dev: Failed device
3884f7b0 1625 * @eflags: mask of ATA_EFLAG_* flags
022bdb07
TH
1626 * @err_mask: err_mask of the error
1627 *
1628 * Record error and examine error history to determine whether
1629 * adjusting transmission speed is necessary. It also sets
1630 * transmission limits appropriately if such adjustment is
1631 * necessary.
1632 *
1633 * LOCKING:
1634 * Kernel thread context (may sleep).
1635 *
1636 * RETURNS:
7d47e8d4 1637 * Determined recovery action.
022bdb07 1638 */
3884f7b0
TH
1639static unsigned int ata_eh_speed_down(struct ata_device *dev,
1640 unsigned int eflags, unsigned int err_mask)
022bdb07 1641{
3884f7b0 1642 struct ata_link *link = dev->link;
76326ac1 1643 int xfer_ok = 0;
7d47e8d4
TH
1644 unsigned int verdict;
1645 unsigned int action = 0;
1646
1647 /* don't bother if Cat-0 error */
76326ac1 1648 if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0)
022bdb07
TH
1649 return 0;
1650
1651 /* record error and determine whether speed down is necessary */
3884f7b0 1652 ata_ering_record(&dev->ering, eflags, err_mask);
7d47e8d4 1653 verdict = ata_eh_speed_down_verdict(dev);
022bdb07 1654
7d47e8d4
TH
1655 /* turn off NCQ? */
1656 if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
1657 (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
1658 ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
1659 dev->flags |= ATA_DFLAG_NCQ_OFF;
1660 ata_dev_printk(dev, KERN_WARNING,
1661 "NCQ disabled due to excessive errors\n");
1662 goto done;
1663 }
022bdb07 1664
7d47e8d4
TH
1665 /* speed down? */
1666 if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1667 /* speed down SATA link speed if possible */
3884f7b0 1668 if (sata_down_spd_limit(link) == 0) {
cf480626 1669 action |= ATA_EH_RESET;
7d47e8d4
TH
1670 goto done;
1671 }
022bdb07 1672
7d47e8d4
TH
1673 /* lower transfer mode */
1674 if (dev->spdn_cnt < 2) {
1675 static const int dma_dnxfer_sel[] =
1676 { ATA_DNXFER_DMA, ATA_DNXFER_40C };
1677 static const int pio_dnxfer_sel[] =
1678 { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
1679 int sel;
1680
1681 if (dev->xfer_shift != ATA_SHIFT_PIO)
1682 sel = dma_dnxfer_sel[dev->spdn_cnt];
1683 else
1684 sel = pio_dnxfer_sel[dev->spdn_cnt];
1685
1686 dev->spdn_cnt++;
1687
1688 if (ata_down_xfermask_limit(dev, sel) == 0) {
cf480626 1689 action |= ATA_EH_RESET;
7d47e8d4
TH
1690 goto done;
1691 }
1692 }
1693 }
1694
1695 /* Fall back to PIO? Slowing down to PIO is meaningless for
663f99b8 1696 * SATA ATA devices. Consider it only for PATA and SATAPI.
7d47e8d4
TH
1697 */
1698 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
663f99b8 1699 (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) &&
7d47e8d4
TH
1700 (dev->xfer_shift != ATA_SHIFT_PIO)) {
1701 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
1702 dev->spdn_cnt = 0;
cf480626 1703 action |= ATA_EH_RESET;
7d47e8d4
TH
1704 goto done;
1705 }
1706 }
022bdb07 1707
022bdb07 1708 return 0;
7d47e8d4
TH
1709 done:
1710 /* device has been slowed down, blow error history */
76326ac1
TH
1711 if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS))
1712 ata_ering_clear(&dev->ering);
7d47e8d4 1713 return action;
022bdb07
TH
1714}
1715
1716/**
9b1e2658
TH
1717 * ata_eh_link_autopsy - analyze error and determine recovery action
1718 * @link: host link to perform autopsy on
022bdb07 1719 *
0260731f
TH
1720 * Analyze why @link failed and determine which recovery actions
1721 * are needed. This function also sets more detailed AC_ERR_*
1722 * values and fills sense data for ATAPI CHECK SENSE.
022bdb07
TH
1723 *
1724 * LOCKING:
1725 * Kernel thread context (may sleep).
1726 */
9b1e2658 1727static void ata_eh_link_autopsy(struct ata_link *link)
022bdb07 1728{
0260731f 1729 struct ata_port *ap = link->ap;
936fd732 1730 struct ata_eh_context *ehc = &link->eh_context;
dfcc173d 1731 struct ata_device *dev;
3884f7b0
TH
1732 unsigned int all_err_mask = 0, eflags = 0;
1733 int tag;
022bdb07
TH
1734 u32 serror;
1735 int rc;
1736
1737 DPRINTK("ENTER\n");
1738
1cdaf534
TH
1739 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1740 return;
1741
022bdb07 1742 /* obtain and analyze SError */
936fd732 1743 rc = sata_scr_read(link, SCR_ERROR, &serror);
022bdb07
TH
1744 if (rc == 0) {
1745 ehc->i.serror |= serror;
0260731f 1746 ata_eh_analyze_serror(link);
4e57c517 1747 } else if (rc != -EOPNOTSUPP) {
cf480626 1748 /* SError read failed, force reset and probing */
b558eddd 1749 ehc->i.probe_mask |= ATA_ALL_DEVICES;
cf480626 1750 ehc->i.action |= ATA_EH_RESET;
4e57c517
TH
1751 ehc->i.err_mask |= AC_ERR_OTHER;
1752 }
022bdb07 1753
e8ee8451 1754 /* analyze NCQ failure */
0260731f 1755 ata_eh_analyze_ncq_error(link);
e8ee8451 1756
022bdb07
TH
1757 /* any real error trumps AC_ERR_OTHER */
1758 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1759 ehc->i.err_mask &= ~AC_ERR_OTHER;
1760
1761 all_err_mask |= ehc->i.err_mask;
1762
1763 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1764 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1765
0260731f 1766 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
022bdb07
TH
1767 continue;
1768
1769 /* inherit upper level err_mask */
1770 qc->err_mask |= ehc->i.err_mask;
1771
022bdb07 1772 /* analyze TF */
4528e4da 1773 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
022bdb07
TH
1774
1775 /* DEV errors are probably spurious in case of ATA_BUS error */
1776 if (qc->err_mask & AC_ERR_ATA_BUS)
1777 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1778 AC_ERR_INVALID);
1779
1780 /* any real error trumps unknown error */
1781 if (qc->err_mask & ~AC_ERR_OTHER)
1782 qc->err_mask &= ~AC_ERR_OTHER;
1783
1784 /* SENSE_VALID trumps dev/unknown error and revalidation */
f90f0828 1785 if (qc->flags & ATA_QCFLAG_SENSE_VALID)
022bdb07 1786 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
022bdb07 1787
03faab78
TH
1788 /* determine whether the command is worth retrying */
1789 if (!(qc->err_mask & AC_ERR_INVALID) &&
1790 ((qc->flags & ATA_QCFLAG_IO) || qc->err_mask != AC_ERR_DEV))
1791 qc->flags |= ATA_QCFLAG_RETRY;
1792
022bdb07 1793 /* accumulate error info */
4528e4da 1794 ehc->i.dev = qc->dev;
022bdb07
TH
1795 all_err_mask |= qc->err_mask;
1796 if (qc->flags & ATA_QCFLAG_IO)
3884f7b0 1797 eflags |= ATA_EFLAG_IS_IO;
022bdb07
TH
1798 }
1799
a20f33ff 1800 /* enforce default EH actions */
b51e9e5d 1801 if (ap->pflags & ATA_PFLAG_FROZEN ||
a20f33ff 1802 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
cf480626 1803 ehc->i.action |= ATA_EH_RESET;
3884f7b0
TH
1804 else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) ||
1805 (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV)))
4528e4da 1806 ehc->i.action |= ATA_EH_REVALIDATE;
022bdb07 1807
dfcc173d
TH
1808 /* If we have offending qcs and the associated failed device,
1809 * perform per-dev EH action only on the offending device.
1810 */
4528e4da 1811 if (ehc->i.dev) {
4528e4da
TH
1812 ehc->i.dev_action[ehc->i.dev->devno] |=
1813 ehc->i.action & ATA_EH_PERDEV_MASK;
1814 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
47005f25
TH
1815 }
1816
2695e366
TH
1817 /* propagate timeout to host link */
1818 if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link))
1819 ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT;
1820
1821 /* record error and consider speeding down */
dfcc173d 1822 dev = ehc->i.dev;
2695e366
TH
1823 if (!dev && ((ata_link_max_devices(link) == 1 &&
1824 ata_dev_enabled(link->device))))
1825 dev = link->device;
dfcc173d 1826
76326ac1
TH
1827 if (dev) {
1828 if (dev->flags & ATA_DFLAG_DUBIOUS_XFER)
1829 eflags |= ATA_EFLAG_DUBIOUS_XFER;
3884f7b0 1830 ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask);
76326ac1 1831 }
dfcc173d 1832
022bdb07
TH
1833 DPRINTK("EXIT\n");
1834}
1835
1836/**
9b1e2658
TH
1837 * ata_eh_autopsy - analyze error and determine recovery action
1838 * @ap: host port to perform autopsy on
1839 *
1840 * Analyze all links of @ap and determine why they failed and
1841 * which recovery actions are needed.
1842 *
1843 * LOCKING:
1844 * Kernel thread context (may sleep).
1845 */
fb7fd614 1846void ata_eh_autopsy(struct ata_port *ap)
9b1e2658
TH
1847{
1848 struct ata_link *link;
1849
2695e366 1850 ata_port_for_each_link(link, ap)
9b1e2658 1851 ata_eh_link_autopsy(link);
2695e366
TH
1852
1853 /* Autopsy of fanout ports can affect host link autopsy.
1854 * Perform host link autopsy last.
1855 */
071f44b1 1856 if (sata_pmp_attached(ap))
2695e366 1857 ata_eh_link_autopsy(&ap->link);
9b1e2658
TH
1858}
1859
1860/**
1861 * ata_eh_link_report - report error handling to user
0260731f 1862 * @link: ATA link EH is going on
022bdb07
TH
1863 *
1864 * Report EH to user.
1865 *
1866 * LOCKING:
1867 * None.
1868 */
9b1e2658 1869static void ata_eh_link_report(struct ata_link *link)
022bdb07 1870{
0260731f
TH
1871 struct ata_port *ap = link->ap;
1872 struct ata_eh_context *ehc = &link->eh_context;
022bdb07 1873 const char *frozen, *desc;
a1e10f7e 1874 char tries_buf[6];
022bdb07
TH
1875 int tag, nr_failed = 0;
1876
94ff3d54
TH
1877 if (ehc->i.flags & ATA_EHI_QUIET)
1878 return;
1879
022bdb07
TH
1880 desc = NULL;
1881 if (ehc->i.desc[0] != '\0')
1882 desc = ehc->i.desc;
1883
1884 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1885 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1886
e027bd36
TH
1887 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link ||
1888 ((qc->flags & ATA_QCFLAG_QUIET) &&
1889 qc->err_mask == AC_ERR_DEV))
022bdb07
TH
1890 continue;
1891 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1892 continue;
1893
1894 nr_failed++;
1895 }
1896
1897 if (!nr_failed && !ehc->i.err_mask)
1898 return;
1899
1900 frozen = "";
b51e9e5d 1901 if (ap->pflags & ATA_PFLAG_FROZEN)
022bdb07
TH
1902 frozen = " frozen";
1903
a1e10f7e
TH
1904 memset(tries_buf, 0, sizeof(tries_buf));
1905 if (ap->eh_tries < ATA_EH_MAX_TRIES)
1906 snprintf(tries_buf, sizeof(tries_buf) - 1, " t%d",
1907 ap->eh_tries);
1908
022bdb07 1909 if (ehc->i.dev) {
e8ee8451 1910 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
a1e10f7e
TH
1911 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1912 ehc->i.err_mask, link->sactive, ehc->i.serror,
1913 ehc->i.action, frozen, tries_buf);
022bdb07 1914 if (desc)
b64bbc39 1915 ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
022bdb07 1916 } else {
0260731f 1917 ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
a1e10f7e
TH
1918 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1919 ehc->i.err_mask, link->sactive, ehc->i.serror,
1920 ehc->i.action, frozen, tries_buf);
022bdb07 1921 if (desc)
0260731f 1922 ata_link_printk(link, KERN_ERR, "%s\n", desc);
022bdb07
TH
1923 }
1924
1333e194
RH
1925 if (ehc->i.serror)
1926 ata_port_printk(ap, KERN_ERR,
1927 "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
1928 ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
1929 ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
1930 ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
1931 ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
1932 ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
1933 ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
1934 ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
1935 ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
1936 ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
1937 ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
1938 ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
1939 ehc->i.serror & SERR_CRC ? "BadCRC " : "",
1940 ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
1941 ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
1942 ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
1943 ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
2dcb407e 1944 ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
1333e194 1945
022bdb07
TH
1946 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1947 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
8a937581 1948 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
abb6a889
TH
1949 const u8 *cdb = qc->cdb;
1950 char data_buf[20] = "";
1951 char cdb_buf[70] = "";
022bdb07 1952
0260731f
TH
1953 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
1954 qc->dev->link != link || !qc->err_mask)
022bdb07
TH
1955 continue;
1956
abb6a889
TH
1957 if (qc->dma_dir != DMA_NONE) {
1958 static const char *dma_str[] = {
1959 [DMA_BIDIRECTIONAL] = "bidi",
1960 [DMA_TO_DEVICE] = "out",
1961 [DMA_FROM_DEVICE] = "in",
1962 };
1963 static const char *prot_str[] = {
1964 [ATA_PROT_PIO] = "pio",
1965 [ATA_PROT_DMA] = "dma",
1966 [ATA_PROT_NCQ] = "ncq",
0dc36888
TH
1967 [ATAPI_PROT_PIO] = "pio",
1968 [ATAPI_PROT_DMA] = "dma",
abb6a889
TH
1969 };
1970
1971 snprintf(data_buf, sizeof(data_buf), " %s %u %s",
1972 prot_str[qc->tf.protocol], qc->nbytes,
1973 dma_str[qc->dma_dir]);
1974 }
1975
e39eec13 1976 if (ata_is_atapi(qc->tf.protocol))
abb6a889
TH
1977 snprintf(cdb_buf, sizeof(cdb_buf),
1978 "cdb %02x %02x %02x %02x %02x %02x %02x %02x "
1979 "%02x %02x %02x %02x %02x %02x %02x %02x\n ",
1980 cdb[0], cdb[1], cdb[2], cdb[3],
1981 cdb[4], cdb[5], cdb[6], cdb[7],
1982 cdb[8], cdb[9], cdb[10], cdb[11],
1983 cdb[12], cdb[13], cdb[14], cdb[15]);
1984
8a937581
TH
1985 ata_dev_printk(qc->dev, KERN_ERR,
1986 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
abb6a889 1987 "tag %d%s\n %s"
8a937581 1988 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
5335b729 1989 "Emask 0x%x (%s)%s\n",
8a937581
TH
1990 cmd->command, cmd->feature, cmd->nsect,
1991 cmd->lbal, cmd->lbam, cmd->lbah,
1992 cmd->hob_feature, cmd->hob_nsect,
1993 cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
abb6a889 1994 cmd->device, qc->tag, data_buf, cdb_buf,
8a937581
TH
1995 res->command, res->feature, res->nsect,
1996 res->lbal, res->lbam, res->lbah,
1997 res->hob_feature, res->hob_nsect,
1998 res->hob_lbal, res->hob_lbam, res->hob_lbah,
5335b729
TH
1999 res->device, qc->err_mask, ata_err_string(qc->err_mask),
2000 qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
1333e194
RH
2001
2002 if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
2dcb407e 2003 ATA_ERR)) {
1333e194
RH
2004 if (res->command & ATA_BUSY)
2005 ata_dev_printk(qc->dev, KERN_ERR,
2dcb407e 2006 "status: { Busy }\n");
1333e194
RH
2007 else
2008 ata_dev_printk(qc->dev, KERN_ERR,
2009 "status: { %s%s%s%s}\n",
2010 res->command & ATA_DRDY ? "DRDY " : "",
2011 res->command & ATA_DF ? "DF " : "",
2012 res->command & ATA_DRQ ? "DRQ " : "",
2dcb407e 2013 res->command & ATA_ERR ? "ERR " : "");
1333e194
RH
2014 }
2015
2016 if (cmd->command != ATA_CMD_PACKET &&
2017 (res->feature & (ATA_ICRC | ATA_UNC | ATA_IDNF |
2018 ATA_ABORTED)))
2019 ata_dev_printk(qc->dev, KERN_ERR,
2020 "error: { %s%s%s%s}\n",
2021 res->feature & ATA_ICRC ? "ICRC " : "",
2022 res->feature & ATA_UNC ? "UNC " : "",
2023 res->feature & ATA_IDNF ? "IDNF " : "",
2dcb407e 2024 res->feature & ATA_ABORTED ? "ABRT " : "");
022bdb07
TH
2025 }
2026}
2027
9b1e2658
TH
2028/**
2029 * ata_eh_report - report error handling to user
2030 * @ap: ATA port to report EH about
2031 *
2032 * Report EH to user.
2033 *
2034 * LOCKING:
2035 * None.
2036 */
fb7fd614 2037void ata_eh_report(struct ata_port *ap)
9b1e2658
TH
2038{
2039 struct ata_link *link;
2040
2041 __ata_port_for_each_link(link, ap)
2042 ata_eh_link_report(link);
2043}
2044
cc0680a5 2045static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
d4b2bab4 2046 unsigned int *classes, unsigned long deadline)
d87fa38e 2047{
f58229f8
TH
2048 struct ata_device *dev;
2049 int rc;
d87fa38e 2050
cc0680a5 2051 ata_link_for_each_dev(dev, link)
f58229f8 2052 classes[dev->devno] = ATA_DEV_UNKNOWN;
d87fa38e 2053
cc0680a5 2054 rc = reset(link, classes, deadline);
d87fa38e 2055
305d2a1a 2056 /* convert all ATA_DEV_UNKNOWN to ATA_DEV_NONE */
cc0680a5 2057 ata_link_for_each_dev(dev, link)
305d2a1a
TH
2058 if (classes[dev->devno] == ATA_DEV_UNKNOWN)
2059 classes[dev->devno] = ATA_DEV_NONE;
d87fa38e 2060
2a0c15ca 2061 return rc;
d87fa38e
TH
2062}
2063
ae791c05
TH
2064static int ata_eh_followup_srst_needed(struct ata_link *link,
2065 int rc, int classify,
664faf09
TH
2066 const unsigned int *classes)
2067{
ae791c05
TH
2068 if (link->flags & ATA_LFLAG_NO_SRST)
2069 return 0;
305d2a1a
TH
2070 if (rc == -EAGAIN) {
2071 if (classify)
2072 return 1;
2073 rc = 0;
2074 }
664faf09
TH
2075 if (rc != 0)
2076 return 0;
071f44b1 2077 if (sata_pmp_supported(link->ap) && ata_is_host_link(link))
3495de73 2078 return 1;
664faf09
TH
2079 return 0;
2080}
2081
fb7fd614
TH
2082int ata_eh_reset(struct ata_link *link, int classify,
2083 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
2084 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
022bdb07 2085{
416dc9ed 2086 const int max_tries = ARRAY_SIZE(ata_eh_reset_timeouts);
afaa5c37 2087 struct ata_port *ap = link->ap;
936fd732 2088 struct ata_eh_context *ehc = &link->eh_context;
664faf09 2089 unsigned int *classes = ehc->classes;
416dc9ed 2090 unsigned int lflags = link->flags;
1cdaf534 2091 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
31daabda 2092 int try = 0;
f58229f8 2093 struct ata_device *dev;
416dc9ed 2094 unsigned long deadline, now;
022bdb07 2095 ata_reset_fn_t reset;
afaa5c37 2096 unsigned long flags;
416dc9ed 2097 u32 sstatus;
f58229f8 2098 int rc;
022bdb07 2099
13abf50d 2100 /* about to reset */
afaa5c37
TH
2101 spin_lock_irqsave(ap->lock, flags);
2102 ap->pflags |= ATA_PFLAG_RESETTING;
2103 spin_unlock_irqrestore(ap->lock, flags);
2104
cf480626 2105 ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
13abf50d 2106
cdeab114
TH
2107 ata_link_for_each_dev(dev, link) {
2108 /* If we issue an SRST then an ATA drive (not ATAPI)
2109 * may change configuration and be in PIO0 timing. If
2110 * we do a hard reset (or are coming from power on)
2111 * this is true for ATA or ATAPI. Until we've set a
2112 * suitable controller mode we should not touch the
2113 * bus as we may be talking too fast.
2114 */
2115 dev->pio_mode = XFER_PIO_0;
2116
2117 /* If the controller has a pio mode setup function
2118 * then use it to set the chipset to rights. Don't
2119 * touch the DMA setup as that will be dealt with when
2120 * configuring devices.
2121 */
2122 if (ap->ops->set_piomode)
2123 ap->ops->set_piomode(ap, dev);
2124 }
2125
eec59f76
TH
2126 if (!softreset && !hardreset) {
2127 if (verbose)
2128 ata_link_printk(link, KERN_INFO, "no reset method "
2129 "available, skipping reset\n");
2130 if (!(lflags & ATA_LFLAG_ASSUME_CLASS))
2131 lflags |= ATA_LFLAG_ASSUME_ATA;
2132 goto done;
2133 }
2134
cf480626
TH
2135 /* prefer hardreset */
2136 ehc->i.action &= ~ATA_EH_RESET;
2137 if (hardreset) {
2138 reset = hardreset;
2139 ehc->i.action = ATA_EH_HARDRESET;
2140 } else {
2141 reset = softreset;
2142 ehc->i.action = ATA_EH_SOFTRESET;
2143 }
f5914a46
TH
2144
2145 if (prereset) {
cc0680a5 2146 rc = prereset(link, jiffies + ATA_EH_PRERESET_TIMEOUT);
f5914a46 2147 if (rc) {
c961922b 2148 if (rc == -ENOENT) {
cc0680a5 2149 ata_link_printk(link, KERN_DEBUG,
4aa9ab67 2150 "port disabled. ignoring.\n");
cf480626 2151 ehc->i.action &= ~ATA_EH_RESET;
4aa9ab67 2152
936fd732 2153 ata_link_for_each_dev(dev, link)
f58229f8 2154 classes[dev->devno] = ATA_DEV_NONE;
4aa9ab67
TH
2155
2156 rc = 0;
c961922b 2157 } else
cc0680a5 2158 ata_link_printk(link, KERN_ERR,
f5914a46 2159 "prereset failed (errno=%d)\n", rc);
fccb6ea5 2160 goto out;
f5914a46
TH
2161 }
2162 }
2163
cf480626
TH
2164 /* prereset() might have cleared ATA_EH_RESET */
2165 if (!(ehc->i.action & ATA_EH_RESET)) {
f5914a46 2166 /* prereset told us not to reset, bang classes and return */
936fd732 2167 ata_link_for_each_dev(dev, link)
f58229f8 2168 classes[dev->devno] = ATA_DEV_NONE;
fccb6ea5
TH
2169 rc = 0;
2170 goto out;
f5914a46
TH
2171 }
2172
022bdb07 2173 retry:
31daabda
TH
2174 deadline = jiffies + ata_eh_reset_timeouts[try++];
2175
3e706399
TH
2176 /* shut up during boot probing */
2177 if (verbose)
cc0680a5 2178 ata_link_printk(link, KERN_INFO, "%s resetting link\n",
3e706399 2179 reset == softreset ? "soft" : "hard");
022bdb07 2180
13abf50d 2181 /* mark that this EH session started with reset */
0d64a233
TH
2182 if (reset == hardreset)
2183 ehc->i.flags |= ATA_EHI_DID_HARDRESET;
2184 else
2185 ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
022bdb07 2186
cc0680a5 2187 rc = ata_do_reset(link, reset, classes, deadline);
022bdb07 2188
664faf09 2189 if (reset == hardreset &&
ae791c05 2190 ata_eh_followup_srst_needed(link, rc, classify, classes)) {
664faf09 2191 /* okay, let's do follow-up softreset */
664faf09
TH
2192 reset = softreset;
2193
2194 if (!reset) {
cc0680a5 2195 ata_link_printk(link, KERN_ERR,
664faf09
TH
2196 "follow-up softreset required "
2197 "but no softreset avaliable\n");
fccb6ea5 2198 rc = -EINVAL;
08cf69d0 2199 goto fail;
664faf09
TH
2200 }
2201
cf480626 2202 ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
cc0680a5 2203 rc = ata_do_reset(link, reset, classes, deadline);
664faf09
TH
2204 }
2205
416dc9ed
TH
2206 /* -EAGAIN can happen if we skipped followup SRST */
2207 if (rc && rc != -EAGAIN)
2208 goto fail;
664faf09 2209
eec59f76 2210 done:
416dc9ed
TH
2211 ata_link_for_each_dev(dev, link) {
2212 /* After the reset, the device state is PIO 0 and the
2213 * controller state is undefined. Reset also wakes up
2214 * drives from sleeping mode.
2215 */
2216 dev->pio_mode = XFER_PIO_0;
2217 dev->flags &= ~ATA_DFLAG_SLEEPING;
31daabda 2218
416dc9ed
TH
2219 if (ata_link_offline(link))
2220 continue;
022bdb07 2221
4ccd3329 2222 /* apply class override */
416dc9ed
TH
2223 if (lflags & ATA_LFLAG_ASSUME_ATA)
2224 classes[dev->devno] = ATA_DEV_ATA;
2225 else if (lflags & ATA_LFLAG_ASSUME_SEMB)
2226 classes[dev->devno] = ATA_DEV_SEMB_UNSUP; /* not yet */
022bdb07
TH
2227 }
2228
416dc9ed
TH
2229 /* record current link speed */
2230 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2231 link->sata_spd = (sstatus >> 4) & 0xf;
008a7896 2232
416dc9ed
TH
2233 if (postreset)
2234 postreset(link, classes);
20952b69 2235
416dc9ed 2236 /* reset successful, schedule revalidation */
cf480626 2237 ata_eh_done(link, NULL, ATA_EH_RESET);
416dc9ed 2238 ehc->i.action |= ATA_EH_REVALIDATE;
ae791c05 2239
416dc9ed 2240 rc = 0;
fccb6ea5
TH
2241 out:
2242 /* clear hotplug flag */
2243 ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
afaa5c37
TH
2244
2245 spin_lock_irqsave(ap->lock, flags);
2246 ap->pflags &= ~ATA_PFLAG_RESETTING;
2247 spin_unlock_irqrestore(ap->lock, flags);
2248
022bdb07 2249 return rc;
416dc9ed
TH
2250
2251 fail:
5958e302
TH
2252 /* if SCR isn't accessible on a fan-out port, PMP needs to be reset */
2253 if (!ata_is_host_link(link) &&
2254 sata_scr_read(link, SCR_STATUS, &sstatus))
2255 rc = -ERESTART;
2256
416dc9ed
TH
2257 if (rc == -ERESTART || try >= max_tries)
2258 goto out;
2259
2260 now = jiffies;
2261 if (time_before(now, deadline)) {
2262 unsigned long delta = deadline - now;
2263
2264 ata_link_printk(link, KERN_WARNING, "reset failed "
2265 "(errno=%d), retrying in %u secs\n",
2266 rc, (jiffies_to_msecs(delta) + 999) / 1000);
2267
2268 while (delta)
2269 delta = schedule_timeout_uninterruptible(delta);
2270 }
2271
2272 if (rc == -EPIPE || try == max_tries - 1)
2273 sata_down_spd_limit(link);
2274 if (hardreset)
2275 reset = hardreset;
2276 goto retry;
022bdb07
TH
2277}
2278
0260731f 2279static int ata_eh_revalidate_and_attach(struct ata_link *link,
084fe639 2280 struct ata_device **r_failed_dev)
022bdb07 2281{
0260731f
TH
2282 struct ata_port *ap = link->ap;
2283 struct ata_eh_context *ehc = &link->eh_context;
022bdb07 2284 struct ata_device *dev;
8c3c52a8 2285 unsigned int new_mask = 0;
084fe639 2286 unsigned long flags;
f58229f8 2287 int rc = 0;
022bdb07
TH
2288
2289 DPRINTK("ENTER\n");
2290
8c3c52a8
TH
2291 /* For PATA drive side cable detection to work, IDENTIFY must
2292 * be done backwards such that PDIAG- is released by the slave
2293 * device before the master device is identified.
2294 */
0260731f 2295 ata_link_for_each_dev_reverse(dev, link) {
f58229f8
TH
2296 unsigned int action = ata_eh_dev_action(dev);
2297 unsigned int readid_flags = 0;
022bdb07 2298
bff04647
TH
2299 if (ehc->i.flags & ATA_EHI_DID_RESET)
2300 readid_flags |= ATA_READID_POSTRESET;
2301
9666f400 2302 if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
633273a3
TH
2303 WARN_ON(dev->class == ATA_DEV_PMP);
2304
0260731f 2305 if (ata_link_offline(link)) {
022bdb07 2306 rc = -EIO;
8c3c52a8 2307 goto err;
022bdb07
TH
2308 }
2309
0260731f 2310 ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
422c9daa
TH
2311 rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
2312 readid_flags);
022bdb07 2313 if (rc)
8c3c52a8 2314 goto err;
022bdb07 2315
0260731f 2316 ata_eh_done(link, dev, ATA_EH_REVALIDATE);
47005f25 2317
baa1e78a
TH
2318 /* Configuration may have changed, reconfigure
2319 * transfer mode.
2320 */
2321 ehc->i.flags |= ATA_EHI_SETMODE;
2322
3057ac3c 2323 /* schedule the scsi_rescan_device() here */
2324 queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
084fe639
TH
2325 } else if (dev->class == ATA_DEV_UNKNOWN &&
2326 ehc->tries[dev->devno] &&
2327 ata_class_enabled(ehc->classes[dev->devno])) {
2328 dev->class = ehc->classes[dev->devno];
2329
633273a3
TH
2330 if (dev->class == ATA_DEV_PMP)
2331 rc = sata_pmp_attach(dev);
2332 else
2333 rc = ata_dev_read_id(dev, &dev->class,
2334 readid_flags, dev->id);
8c3c52a8
TH
2335 switch (rc) {
2336 case 0:
f58229f8 2337 new_mask |= 1 << dev->devno;
8c3c52a8
TH
2338 break;
2339 case -ENOENT:
55a8e2c8
TH
2340 /* IDENTIFY was issued to non-existent
2341 * device. No need to reset. Just
2342 * thaw and kill the device.
2343 */
2344 ata_eh_thaw_port(ap);
084fe639
TH
2345 dev->class = ATA_DEV_UNKNOWN;
2346 break;
8c3c52a8
TH
2347 default:
2348 dev->class = ATA_DEV_UNKNOWN;
2349 goto err;
084fe639 2350 }
8c3c52a8
TH
2351 }
2352 }
084fe639 2353
c1c4e8d5 2354 /* PDIAG- should have been released, ask cable type if post-reset */
33267325
TH
2355 if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) {
2356 if (ap->ops->cable_detect)
2357 ap->cbl = ap->ops->cable_detect(ap);
2358 ata_force_cbl(ap);
2359 }
c1c4e8d5 2360
8c3c52a8
TH
2361 /* Configure new devices forward such that user doesn't see
2362 * device detection messages backwards.
2363 */
0260731f 2364 ata_link_for_each_dev(dev, link) {
633273a3
TH
2365 if (!(new_mask & (1 << dev->devno)) ||
2366 dev->class == ATA_DEV_PMP)
8c3c52a8
TH
2367 continue;
2368
2369 ehc->i.flags |= ATA_EHI_PRINTINFO;
2370 rc = ata_dev_configure(dev);
2371 ehc->i.flags &= ~ATA_EHI_PRINTINFO;
2372 if (rc)
2373 goto err;
2374
2375 spin_lock_irqsave(ap->lock, flags);
2376 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
2377 spin_unlock_irqrestore(ap->lock, flags);
2378
2379 /* new device discovered, configure xfermode */
2380 ehc->i.flags |= ATA_EHI_SETMODE;
022bdb07
TH
2381 }
2382
8c3c52a8 2383 return 0;
022bdb07 2384
8c3c52a8
TH
2385 err:
2386 *r_failed_dev = dev;
2387 DPRINTK("EXIT rc=%d\n", rc);
022bdb07
TH
2388 return rc;
2389}
2390
6f1d1e3a
TH
2391/**
2392 * ata_set_mode - Program timings and issue SET FEATURES - XFER
2393 * @link: link on which timings will be programmed
2394 * @r_failed_dev: out paramter for failed device
2395 *
2396 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
2397 * ata_set_mode() fails, pointer to the failing device is
2398 * returned in @r_failed_dev.
2399 *
2400 * LOCKING:
2401 * PCI/etc. bus probe sem.
2402 *
2403 * RETURNS:
2404 * 0 on success, negative errno otherwise
2405 */
2406int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
2407{
2408 struct ata_port *ap = link->ap;
00115e0f
TH
2409 struct ata_device *dev;
2410 int rc;
6f1d1e3a 2411
76326ac1
TH
2412 /* if data transfer is verified, clear DUBIOUS_XFER on ering top */
2413 ata_link_for_each_dev(dev, link) {
2414 if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
2415 struct ata_ering_entry *ent;
2416
2417 ent = ata_ering_top(&dev->ering);
2418 if (ent)
2419 ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER;
2420 }
2421 }
2422
6f1d1e3a
TH
2423 /* has private set_mode? */
2424 if (ap->ops->set_mode)
00115e0f
TH
2425 rc = ap->ops->set_mode(link, r_failed_dev);
2426 else
2427 rc = ata_do_set_mode(link, r_failed_dev);
2428
2429 /* if transfer mode has changed, set DUBIOUS_XFER on device */
2430 ata_link_for_each_dev(dev, link) {
2431 struct ata_eh_context *ehc = &link->eh_context;
2432 u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
2433 u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
2434
2435 if (dev->xfer_mode != saved_xfer_mode ||
2436 ata_ncq_enabled(dev) != saved_ncq)
2437 dev->flags |= ATA_DFLAG_DUBIOUS_XFER;
2438 }
2439
2440 return rc;
6f1d1e3a
TH
2441}
2442
0260731f 2443static int ata_link_nr_enabled(struct ata_link *link)
022bdb07 2444{
f58229f8
TH
2445 struct ata_device *dev;
2446 int cnt = 0;
022bdb07 2447
0260731f 2448 ata_link_for_each_dev(dev, link)
f58229f8 2449 if (ata_dev_enabled(dev))
022bdb07
TH
2450 cnt++;
2451 return cnt;
2452}
2453
0260731f 2454static int ata_link_nr_vacant(struct ata_link *link)
084fe639 2455{
f58229f8
TH
2456 struct ata_device *dev;
2457 int cnt = 0;
084fe639 2458
0260731f 2459 ata_link_for_each_dev(dev, link)
f58229f8 2460 if (dev->class == ATA_DEV_UNKNOWN)
084fe639
TH
2461 cnt++;
2462 return cnt;
2463}
2464
0260731f 2465static int ata_eh_skip_recovery(struct ata_link *link)
084fe639 2466{
672b2d65 2467 struct ata_port *ap = link->ap;
0260731f 2468 struct ata_eh_context *ehc = &link->eh_context;
f58229f8 2469 struct ata_device *dev;
084fe639 2470
f9df58cb
TH
2471 /* skip disabled links */
2472 if (link->flags & ATA_LFLAG_DISABLED)
2473 return 1;
2474
672b2d65
TH
2475 /* thaw frozen port and recover failed devices */
2476 if ((ap->pflags & ATA_PFLAG_FROZEN) || ata_link_nr_enabled(link))
2477 return 0;
2478
2479 /* reset at least once if reset is requested */
2480 if ((ehc->i.action & ATA_EH_RESET) &&
2481 !(ehc->i.flags & ATA_EHI_DID_RESET))
084fe639
TH
2482 return 0;
2483
2484 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
0260731f 2485 ata_link_for_each_dev(dev, link) {
084fe639
TH
2486 if (dev->class == ATA_DEV_UNKNOWN &&
2487 ehc->classes[dev->devno] != ATA_DEV_NONE)
2488 return 0;
2489 }
2490
2491 return 1;
2492}
2493
02c05a27
TH
2494static int ata_eh_schedule_probe(struct ata_device *dev)
2495{
2496 struct ata_eh_context *ehc = &dev->link->eh_context;
2497
2498 if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
2499 (ehc->did_probe_mask & (1 << dev->devno)))
2500 return 0;
2501
2502 ata_eh_detach_dev(dev);
2503 ata_dev_init(dev);
2504 ehc->did_probe_mask |= (1 << dev->devno);
cf480626 2505 ehc->i.action |= ATA_EH_RESET;
00115e0f
TH
2506 ehc->saved_xfer_mode[dev->devno] = 0;
2507 ehc->saved_ncq_enabled &= ~(1 << dev->devno);
02c05a27
TH
2508
2509 return 1;
2510}
2511
9b1e2658 2512static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
fee7ca72 2513{
9af5c9c9 2514 struct ata_eh_context *ehc = &dev->link->eh_context;
fee7ca72
TH
2515
2516 ehc->tries[dev->devno]--;
2517
2518 switch (err) {
2519 case -ENODEV:
2520 /* device missing or wrong IDENTIFY data, schedule probing */
2521 ehc->i.probe_mask |= (1 << dev->devno);
2522 case -EINVAL:
2523 /* give it just one more chance */
2524 ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
2525 case -EIO:
4fb4615b 2526 if (ehc->tries[dev->devno] == 1 && dev->pio_mode > XFER_PIO_0) {
fee7ca72
TH
2527 /* This is the last chance, better to slow
2528 * down than lose it.
2529 */
936fd732 2530 sata_down_spd_limit(dev->link);
fee7ca72
TH
2531 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2532 }
2533 }
2534
2535 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2536 /* disable device if it has used up all its chances */
2537 ata_dev_disable(dev);
2538
2539 /* detach if offline */
936fd732 2540 if (ata_link_offline(dev->link))
fee7ca72
TH
2541 ata_eh_detach_dev(dev);
2542
02c05a27
TH
2543 /* schedule probe if necessary */
2544 if (ata_eh_schedule_probe(dev))
fee7ca72 2545 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
9b1e2658
TH
2546
2547 return 1;
fee7ca72 2548 } else {
cf480626 2549 ehc->i.action |= ATA_EH_RESET;
9b1e2658 2550 return 0;
fee7ca72
TH
2551 }
2552}
2553
022bdb07
TH
2554/**
2555 * ata_eh_recover - recover host port after error
2556 * @ap: host port to recover
f5914a46 2557 * @prereset: prereset method (can be NULL)
022bdb07
TH
2558 * @softreset: softreset method (can be NULL)
2559 * @hardreset: hardreset method (can be NULL)
2560 * @postreset: postreset method (can be NULL)
9b1e2658 2561 * @r_failed_link: out parameter for failed link
022bdb07
TH
2562 *
2563 * This is the alpha and omega, eum and yang, heart and soul of
2564 * libata exception handling. On entry, actions required to
9b1e2658
TH
2565 * recover each link and hotplug requests are recorded in the
2566 * link's eh_context. This function executes all the operations
2567 * with appropriate retrials and fallbacks to resurrect failed
084fe639 2568 * devices, detach goners and greet newcomers.
022bdb07
TH
2569 *
2570 * LOCKING:
2571 * Kernel thread context (may sleep).
2572 *
2573 * RETURNS:
2574 * 0 on success, -errno on failure.
2575 */
fb7fd614
TH
2576int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
2577 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2578 ata_postreset_fn_t postreset,
2579 struct ata_link **r_failed_link)
022bdb07 2580{
9b1e2658 2581 struct ata_link *link;
022bdb07 2582 struct ata_device *dev;
9b1e2658
TH
2583 int nr_failed_devs, nr_disabled_devs;
2584 int reset, rc;
f9df58cb 2585 unsigned long flags;
022bdb07
TH
2586
2587 DPRINTK("ENTER\n");
2588
2589 /* prep for recovery */
9b1e2658
TH
2590 ata_port_for_each_link(link, ap) {
2591 struct ata_eh_context *ehc = &link->eh_context;
084fe639 2592
f9df58cb
TH
2593 /* re-enable link? */
2594 if (ehc->i.action & ATA_EH_ENABLE_LINK) {
2595 ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
2596 spin_lock_irqsave(ap->lock, flags);
2597 link->flags &= ~ATA_LFLAG_DISABLED;
2598 spin_unlock_irqrestore(ap->lock, flags);
2599 ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
2600 }
2601
9b1e2658 2602 ata_link_for_each_dev(dev, link) {
fd995f70
TH
2603 if (link->flags & ATA_LFLAG_NO_RETRY)
2604 ehc->tries[dev->devno] = 1;
2605 else
2606 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
084fe639 2607
9b1e2658
TH
2608 /* collect port action mask recorded in dev actions */
2609 ehc->i.action |= ehc->i.dev_action[dev->devno] &
2610 ~ATA_EH_PERDEV_MASK;
2611 ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
2612
2613 /* process hotplug request */
2614 if (dev->flags & ATA_DFLAG_DETACH)
2615 ata_eh_detach_dev(dev);
2616
02c05a27
TH
2617 /* schedule probe if necessary */
2618 if (!ata_dev_enabled(dev))
2619 ata_eh_schedule_probe(dev);
084fe639 2620 }
022bdb07
TH
2621 }
2622
2623 retry:
022bdb07 2624 rc = 0;
9b1e2658
TH
2625 nr_failed_devs = 0;
2626 nr_disabled_devs = 0;
2627 reset = 0;
022bdb07 2628
aeb2ecd6 2629 /* if UNLOADING, finish immediately */
b51e9e5d 2630 if (ap->pflags & ATA_PFLAG_UNLOADING)
aeb2ecd6
TH
2631 goto out;
2632
9b1e2658
TH
2633 /* prep for EH */
2634 ata_port_for_each_link(link, ap) {
2635 struct ata_eh_context *ehc = &link->eh_context;
022bdb07 2636
9b1e2658
TH
2637 /* skip EH if possible. */
2638 if (ata_eh_skip_recovery(link))
2639 ehc->i.action = 0;
2640
2641 /* do we need to reset? */
cf480626 2642 if (ehc->i.action & ATA_EH_RESET)
9b1e2658
TH
2643 reset = 1;
2644
2645 ata_link_for_each_dev(dev, link)
2646 ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
2647 }
084fe639 2648
022bdb07 2649 /* reset */
9b1e2658 2650 if (reset) {
b06ce3e5
TH
2651 /* if PMP is attached, this function only deals with
2652 * downstream links, port should stay thawed.
2653 */
071f44b1 2654 if (!sata_pmp_attached(ap))
b06ce3e5 2655 ata_eh_freeze_port(ap);
022bdb07 2656
9b1e2658
TH
2657 ata_port_for_each_link(link, ap) {
2658 struct ata_eh_context *ehc = &link->eh_context;
2659
cf480626 2660 if (!(ehc->i.action & ATA_EH_RESET))
9b1e2658
TH
2661 continue;
2662
2663 rc = ata_eh_reset(link, ata_link_nr_vacant(link),
2664 prereset, softreset, hardreset,
2665 postreset);
2666 if (rc) {
2667 ata_link_printk(link, KERN_ERR,
2668 "reset failed, giving up\n");
2669 goto out;
2670 }
022bdb07
TH
2671 }
2672
071f44b1 2673 if (!sata_pmp_attached(ap))
b06ce3e5 2674 ata_eh_thaw_port(ap);
022bdb07
TH
2675 }
2676
9b1e2658
TH
2677 /* the rest */
2678 ata_port_for_each_link(link, ap) {
2679 struct ata_eh_context *ehc = &link->eh_context;
022bdb07 2680
9b1e2658
TH
2681 /* revalidate existing devices and attach new ones */
2682 rc = ata_eh_revalidate_and_attach(link, &dev);
4ae72a1e 2683 if (rc)
022bdb07 2684 goto dev_fail;
022bdb07 2685
633273a3
TH
2686 /* if PMP got attached, return, pmp EH will take care of it */
2687 if (link->device->class == ATA_DEV_PMP) {
2688 ehc->i.action = 0;
2689 return 0;
2690 }
2691
9b1e2658
TH
2692 /* configure transfer mode if necessary */
2693 if (ehc->i.flags & ATA_EHI_SETMODE) {
2694 rc = ata_set_mode(link, &dev);
2695 if (rc)
2696 goto dev_fail;
2697 ehc->i.flags &= ~ATA_EHI_SETMODE;
2698 }
2699
3ec25ebd 2700 if (ehc->i.action & ATA_EH_LPM)
ca77329f
KCA
2701 ata_link_for_each_dev(dev, link)
2702 ata_dev_enable_pm(dev, ap->pm_policy);
2703
9b1e2658
TH
2704 /* this link is okay now */
2705 ehc->i.flags = 0;
2706 continue;
022bdb07 2707
2dcb407e 2708dev_fail:
9b1e2658
TH
2709 nr_failed_devs++;
2710 if (ata_eh_handle_dev_fail(dev, rc))
2711 nr_disabled_devs++;
022bdb07 2712
b06ce3e5
TH
2713 if (ap->pflags & ATA_PFLAG_FROZEN) {
2714 /* PMP reset requires working host port.
2715 * Can't retry if it's frozen.
2716 */
071f44b1 2717 if (sata_pmp_attached(ap))
b06ce3e5 2718 goto out;
9b1e2658 2719 break;
b06ce3e5 2720 }
022bdb07
TH
2721 }
2722
9b1e2658
TH
2723 if (nr_failed_devs) {
2724 if (nr_failed_devs != nr_disabled_devs) {
2725 ata_port_printk(ap, KERN_WARNING, "failed to recover "
2726 "some devices, retrying in 5 secs\n");
2727 ssleep(5);
2728 } else {
2729 /* no device left to recover, repeat fast */
2730 msleep(500);
2731 }
022bdb07 2732
9b1e2658 2733 goto retry;
022bdb07
TH
2734 }
2735
9b1e2658
TH
2736 out:
2737 if (rc && r_failed_link)
2738 *r_failed_link = link;
2739
022bdb07
TH
2740 DPRINTK("EXIT, rc=%d\n", rc);
2741 return rc;
2742}
2743
2744/**
2745 * ata_eh_finish - finish up EH
2746 * @ap: host port to finish EH for
2747 *
2748 * Recovery is complete. Clean up EH states and retry or finish
2749 * failed qcs.
2750 *
2751 * LOCKING:
2752 * None.
2753 */
fb7fd614 2754void ata_eh_finish(struct ata_port *ap)
022bdb07
TH
2755{
2756 int tag;
2757
2758 /* retry or finish qcs */
2759 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2760 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2761
2762 if (!(qc->flags & ATA_QCFLAG_FAILED))
2763 continue;
2764
2765 if (qc->err_mask) {
2766 /* FIXME: Once EH migration is complete,
2767 * generate sense data in this function,
2768 * considering both err_mask and tf.
2769 */
03faab78 2770 if (qc->flags & ATA_QCFLAG_RETRY)
022bdb07 2771 ata_eh_qc_retry(qc);
03faab78
TH
2772 else
2773 ata_eh_qc_complete(qc);
022bdb07
TH
2774 } else {
2775 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2776 ata_eh_qc_complete(qc);
2777 } else {
2778 /* feed zero TF to sense generation */
2779 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2780 ata_eh_qc_retry(qc);
2781 }
2782 }
2783 }
da917d69
TH
2784
2785 /* make sure nr_active_links is zero after EH */
2786 WARN_ON(ap->nr_active_links);
2787 ap->nr_active_links = 0;
022bdb07
TH
2788}
2789
2790/**
2791 * ata_do_eh - do standard error handling
2792 * @ap: host port to handle error for
a1efdaba 2793 *
f5914a46 2794 * @prereset: prereset method (can be NULL)
022bdb07
TH
2795 * @softreset: softreset method (can be NULL)
2796 * @hardreset: hardreset method (can be NULL)
2797 * @postreset: postreset method (can be NULL)
2798 *
2799 * Perform standard error handling sequence.
2800 *
2801 * LOCKING:
2802 * Kernel thread context (may sleep).
2803 */
f5914a46
TH
2804void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2805 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2806 ata_postreset_fn_t postreset)
022bdb07 2807{
9b1e2658
TH
2808 struct ata_device *dev;
2809 int rc;
2810
2811 ata_eh_autopsy(ap);
2812 ata_eh_report(ap);
2813
2814 rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
2815 NULL);
2816 if (rc) {
2817 ata_link_for_each_dev(dev, &ap->link)
2818 ata_dev_disable(dev);
2819 }
2820
022bdb07
TH
2821 ata_eh_finish(ap);
2822}
500530f6 2823
a1efdaba
TH
2824/**
2825 * ata_std_error_handler - standard error handler
2826 * @ap: host port to handle error for
2827 *
2828 * Standard error handler
2829 *
2830 * LOCKING:
2831 * Kernel thread context (may sleep).
2832 */
2833void ata_std_error_handler(struct ata_port *ap)
2834{
2835 struct ata_port_operations *ops = ap->ops;
2836 ata_reset_fn_t hardreset = ops->hardreset;
2837
57c9efdf
TH
2838 /* ignore built-in hardreset if SCR access is not available */
2839 if (ata_is_builtin_hardreset(hardreset) && !sata_scr_valid(&ap->link))
a1efdaba
TH
2840 hardreset = NULL;
2841
2842 ata_do_eh(ap, ops->prereset, ops->softreset, hardreset, ops->postreset);
2843}
2844
6ffa01d8 2845#ifdef CONFIG_PM
500530f6
TH
2846/**
2847 * ata_eh_handle_port_suspend - perform port suspend operation
2848 * @ap: port to suspend
2849 *
2850 * Suspend @ap.
2851 *
2852 * LOCKING:
2853 * Kernel thread context (may sleep).
2854 */
2855static void ata_eh_handle_port_suspend(struct ata_port *ap)
2856{
2857 unsigned long flags;
2858 int rc = 0;
2859
2860 /* are we suspending? */
2861 spin_lock_irqsave(ap->lock, flags);
2862 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2863 ap->pm_mesg.event == PM_EVENT_ON) {
2864 spin_unlock_irqrestore(ap->lock, flags);
2865 return;
2866 }
2867 spin_unlock_irqrestore(ap->lock, flags);
2868
2869 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
2870
64578a3d
TH
2871 /* tell ACPI we're suspending */
2872 rc = ata_acpi_on_suspend(ap);
2873 if (rc)
2874 goto out;
2875
500530f6
TH
2876 /* suspend */
2877 ata_eh_freeze_port(ap);
2878
2879 if (ap->ops->port_suspend)
2880 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
2881
bd3adca5 2882 ata_acpi_set_state(ap, PMSG_SUSPEND);
64578a3d 2883 out:
500530f6
TH
2884 /* report result */
2885 spin_lock_irqsave(ap->lock, flags);
2886
2887 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
2888 if (rc == 0)
2889 ap->pflags |= ATA_PFLAG_SUSPENDED;
64578a3d 2890 else if (ap->pflags & ATA_PFLAG_FROZEN)
500530f6
TH
2891 ata_port_schedule_eh(ap);
2892
2893 if (ap->pm_result) {
2894 *ap->pm_result = rc;
2895 ap->pm_result = NULL;
2896 }
2897
2898 spin_unlock_irqrestore(ap->lock, flags);
2899
2900 return;
2901}
2902
2903/**
2904 * ata_eh_handle_port_resume - perform port resume operation
2905 * @ap: port to resume
2906 *
2907 * Resume @ap.
2908 *
500530f6
TH
2909 * LOCKING:
2910 * Kernel thread context (may sleep).
2911 */
2912static void ata_eh_handle_port_resume(struct ata_port *ap)
2913{
500530f6 2914 unsigned long flags;
9666f400 2915 int rc = 0;
500530f6
TH
2916
2917 /* are we resuming? */
2918 spin_lock_irqsave(ap->lock, flags);
2919 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2920 ap->pm_mesg.event != PM_EVENT_ON) {
2921 spin_unlock_irqrestore(ap->lock, flags);
2922 return;
2923 }
2924 spin_unlock_irqrestore(ap->lock, flags);
2925
9666f400 2926 WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
500530f6 2927
bd3adca5
SL
2928 ata_acpi_set_state(ap, PMSG_ON);
2929
500530f6
TH
2930 if (ap->ops->port_resume)
2931 rc = ap->ops->port_resume(ap);
2932
6746544c
TH
2933 /* tell ACPI that we're resuming */
2934 ata_acpi_on_resume(ap);
2935
9666f400 2936 /* report result */
500530f6
TH
2937 spin_lock_irqsave(ap->lock, flags);
2938 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
2939 if (ap->pm_result) {
2940 *ap->pm_result = rc;
2941 ap->pm_result = NULL;
2942 }
2943 spin_unlock_irqrestore(ap->lock, flags);
2944}
6ffa01d8 2945#endif /* CONFIG_PM */