]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/ata/libata-eh.c
libata: implement libata.force module parameter
[mirror_ubuntu-bionic-kernel.git] / drivers / ata / libata-eh.c
CommitLineData
ece1d636
TH
1/*
2 * libata-eh.c - libata error handling
3 *
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
7 *
8 * Copyright 2006 Tejun Heo <htejun@gmail.com>
9 *
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; see the file COPYING. If not, write to
23 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
24 * USA.
25 *
26 *
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
29 *
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
32 *
33 */
34
ece1d636 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
876 if (!ap->nr_pmp_links || rc) {
877 /* PMP is not attached or SNTF is not available */
878 if (!ap->nr_pmp_links) {
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
13abf50d
TH
1082 /* Reset is represented by combination of actions and EHI
1083 * flags. Suck in all related bits before clearing eh_info to
1084 * avoid losing requested action.
1085 */
1086 if (action & ATA_EH_RESET_MASK) {
1087 ehc->i.action |= ehi->action & ATA_EH_RESET_MASK;
1088 ehc->i.flags |= ehi->flags & ATA_EHI_RESET_MODIFIER_MASK;
1089
1090 /* make sure all reset actions are cleared & clear EHI flags */
1091 action |= ATA_EH_RESET_MASK;
1092 ehi->flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
1093 }
1094
955e57df 1095 ata_eh_clear_action(link, dev, ehi, action);
1cdaf534 1096
13abf50d 1097 if (!(ehc->i.flags & ATA_EHI_QUIET))
1cdaf534
TH
1098 ap->pflags |= ATA_PFLAG_RECOVERED;
1099
ba6a1308 1100 spin_unlock_irqrestore(ap->lock, flags);
022bdb07
TH
1101}
1102
47005f25
TH
1103/**
1104 * ata_eh_done - EH action complete
955e57df 1105* @ap: target ATA port
47005f25
TH
1106 * @dev: target ATA dev for per-dev action (can be NULL)
1107 * @action: action just completed
1108 *
1109 * Called right after performing EH actions to clear related bits
955e57df 1110 * in @link->eh_context.
47005f25
TH
1111 *
1112 * LOCKING:
1113 * None.
1114 */
fb7fd614
TH
1115void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1116 unsigned int action)
47005f25 1117{
955e57df 1118 struct ata_eh_context *ehc = &link->eh_context;
9af5c9c9 1119
13abf50d
TH
1120 /* if reset is complete, clear all reset actions & reset modifier */
1121 if (action & ATA_EH_RESET_MASK) {
1122 action |= ATA_EH_RESET_MASK;
9af5c9c9 1123 ehc->i.flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
13abf50d
TH
1124 }
1125
955e57df 1126 ata_eh_clear_action(link, dev, &ehc->i, action);
47005f25
TH
1127}
1128
022bdb07
TH
1129/**
1130 * ata_err_string - convert err_mask to descriptive string
1131 * @err_mask: error mask to convert to string
1132 *
1133 * Convert @err_mask to descriptive string. Errors are
1134 * prioritized according to severity and only the most severe
1135 * error is reported.
1136 *
1137 * LOCKING:
1138 * None.
1139 *
1140 * RETURNS:
1141 * Descriptive string for @err_mask
1142 */
2dcb407e 1143static const char *ata_err_string(unsigned int err_mask)
022bdb07
TH
1144{
1145 if (err_mask & AC_ERR_HOST_BUS)
1146 return "host bus error";
1147 if (err_mask & AC_ERR_ATA_BUS)
1148 return "ATA bus error";
1149 if (err_mask & AC_ERR_TIMEOUT)
1150 return "timeout";
1151 if (err_mask & AC_ERR_HSM)
1152 return "HSM violation";
1153 if (err_mask & AC_ERR_SYSTEM)
1154 return "internal error";
1155 if (err_mask & AC_ERR_MEDIA)
1156 return "media error";
1157 if (err_mask & AC_ERR_INVALID)
1158 return "invalid argument";
1159 if (err_mask & AC_ERR_DEV)
1160 return "device error";
1161 return "unknown error";
1162}
1163
e8ee8451
TH
1164/**
1165 * ata_read_log_page - read a specific log page
1166 * @dev: target device
1167 * @page: page to read
1168 * @buf: buffer to store read page
1169 * @sectors: number of sectors to read
1170 *
1171 * Read log page using READ_LOG_EXT command.
1172 *
1173 * LOCKING:
1174 * Kernel thread context (may sleep).
1175 *
1176 * RETURNS:
1177 * 0 on success, AC_ERR_* mask otherwise.
1178 */
1179static unsigned int ata_read_log_page(struct ata_device *dev,
1180 u8 page, void *buf, unsigned int sectors)
1181{
1182 struct ata_taskfile tf;
1183 unsigned int err_mask;
1184
1185 DPRINTK("read log page - page %d\n", page);
1186
1187 ata_tf_init(dev, &tf);
1188 tf.command = ATA_CMD_READ_LOG_EXT;
1189 tf.lbal = page;
1190 tf.nsect = sectors;
1191 tf.hob_nsect = sectors >> 8;
1192 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1193 tf.protocol = ATA_PROT_PIO;
1194
1195 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
2b789108 1196 buf, sectors * ATA_SECT_SIZE, 0);
e8ee8451
TH
1197
1198 DPRINTK("EXIT, err_mask=%x\n", err_mask);
1199 return err_mask;
1200}
1201
1202/**
1203 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
1204 * @dev: Device to read log page 10h from
1205 * @tag: Resulting tag of the failed command
1206 * @tf: Resulting taskfile registers of the failed command
1207 *
1208 * Read log page 10h to obtain NCQ error details and clear error
1209 * condition.
1210 *
1211 * LOCKING:
1212 * Kernel thread context (may sleep).
1213 *
1214 * RETURNS:
1215 * 0 on success, -errno otherwise.
1216 */
1217static int ata_eh_read_log_10h(struct ata_device *dev,
1218 int *tag, struct ata_taskfile *tf)
1219{
9af5c9c9 1220 u8 *buf = dev->link->ap->sector_buf;
e8ee8451
TH
1221 unsigned int err_mask;
1222 u8 csum;
1223 int i;
1224
1225 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
1226 if (err_mask)
1227 return -EIO;
1228
1229 csum = 0;
1230 for (i = 0; i < ATA_SECT_SIZE; i++)
1231 csum += buf[i];
1232 if (csum)
1233 ata_dev_printk(dev, KERN_WARNING,
1234 "invalid checksum 0x%x on log page 10h\n", csum);
1235
1236 if (buf[0] & 0x80)
1237 return -ENOENT;
1238
1239 *tag = buf[0] & 0x1f;
1240
1241 tf->command = buf[2];
1242 tf->feature = buf[3];
1243 tf->lbal = buf[4];
1244 tf->lbam = buf[5];
1245 tf->lbah = buf[6];
1246 tf->device = buf[7];
1247 tf->hob_lbal = buf[8];
1248 tf->hob_lbam = buf[9];
1249 tf->hob_lbah = buf[10];
1250 tf->nsect = buf[12];
1251 tf->hob_nsect = buf[13];
1252
1253 return 0;
1254}
1255
022bdb07
TH
1256/**
1257 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1258 * @dev: device to perform REQUEST_SENSE to
1259 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
1260 *
1261 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
1262 * SENSE. This function is EH helper.
1263 *
1264 * LOCKING:
1265 * Kernel thread context (may sleep).
1266 *
1267 * RETURNS:
1268 * 0 on success, AC_ERR_* mask on failure
1269 */
56287768 1270static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc)
022bdb07 1271{
56287768
AL
1272 struct ata_device *dev = qc->dev;
1273 unsigned char *sense_buf = qc->scsicmd->sense_buffer;
9af5c9c9 1274 struct ata_port *ap = dev->link->ap;
022bdb07
TH
1275 struct ata_taskfile tf;
1276 u8 cdb[ATAPI_CDB_LEN];
1277
1278 DPRINTK("ATAPI request sense\n");
1279
022bdb07
TH
1280 /* FIXME: is this needed? */
1281 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1282
56287768
AL
1283 /* initialize sense_buf with the error register,
1284 * for the case where they are -not- overwritten
1285 */
022bdb07 1286 sense_buf[0] = 0x70;
56287768
AL
1287 sense_buf[2] = qc->result_tf.feature >> 4;
1288
a617c09f 1289 /* some devices time out if garbage left in tf */
56287768 1290 ata_tf_init(dev, &tf);
022bdb07
TH
1291
1292 memset(cdb, 0, ATAPI_CDB_LEN);
1293 cdb[0] = REQUEST_SENSE;
1294 cdb[4] = SCSI_SENSE_BUFFERSIZE;
1295
1296 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1297 tf.command = ATA_CMD_PACKET;
1298
1299 /* is it pointless to prefer PIO for "safety reasons"? */
1300 if (ap->flags & ATA_FLAG_PIO_DMA) {
0dc36888 1301 tf.protocol = ATAPI_PROT_DMA;
022bdb07
TH
1302 tf.feature |= ATAPI_PKT_DMA;
1303 } else {
0dc36888 1304 tf.protocol = ATAPI_PROT_PIO;
f2dfc1a1
TH
1305 tf.lbam = SCSI_SENSE_BUFFERSIZE;
1306 tf.lbah = 0;
022bdb07
TH
1307 }
1308
1309 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
2b789108 1310 sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
022bdb07
TH
1311}
1312
1313/**
1314 * ata_eh_analyze_serror - analyze SError for a failed port
0260731f 1315 * @link: ATA link to analyze SError for
022bdb07
TH
1316 *
1317 * Analyze SError if available and further determine cause of
1318 * failure.
1319 *
1320 * LOCKING:
1321 * None.
1322 */
0260731f 1323static void ata_eh_analyze_serror(struct ata_link *link)
022bdb07 1324{
0260731f 1325 struct ata_eh_context *ehc = &link->eh_context;
022bdb07
TH
1326 u32 serror = ehc->i.serror;
1327 unsigned int err_mask = 0, action = 0;
f9df58cb 1328 u32 hotplug_mask;
022bdb07
TH
1329
1330 if (serror & SERR_PERSISTENT) {
1331 err_mask |= AC_ERR_ATA_BUS;
1332 action |= ATA_EH_HARDRESET;
1333 }
1334 if (serror &
1335 (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
1336 err_mask |= AC_ERR_ATA_BUS;
1337 action |= ATA_EH_SOFTRESET;
1338 }
1339 if (serror & SERR_PROTOCOL) {
1340 err_mask |= AC_ERR_HSM;
1341 action |= ATA_EH_SOFTRESET;
1342 }
1343 if (serror & SERR_INTERNAL) {
1344 err_mask |= AC_ERR_SYSTEM;
771b8dad 1345 action |= ATA_EH_HARDRESET;
022bdb07 1346 }
f9df58cb
TH
1347
1348 /* Determine whether a hotplug event has occurred. Both
1349 * SError.N/X are considered hotplug events for enabled or
1350 * host links. For disabled PMP links, only N bit is
1351 * considered as X bit is left at 1 for link plugging.
1352 */
1353 hotplug_mask = 0;
1354
1355 if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
1356 hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
1357 else
1358 hotplug_mask = SERR_PHYRDY_CHG;
1359
1360 if (serror & hotplug_mask)
084fe639 1361 ata_ehi_hotplugged(&ehc->i);
022bdb07
TH
1362
1363 ehc->i.err_mask |= err_mask;
1364 ehc->i.action |= action;
1365}
1366
e8ee8451
TH
1367/**
1368 * ata_eh_analyze_ncq_error - analyze NCQ error
0260731f 1369 * @link: ATA link to analyze NCQ error for
e8ee8451
TH
1370 *
1371 * Read log page 10h, determine the offending qc and acquire
1372 * error status TF. For NCQ device errors, all LLDDs have to do
1373 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1374 * care of the rest.
1375 *
1376 * LOCKING:
1377 * Kernel thread context (may sleep).
1378 */
0260731f 1379static void ata_eh_analyze_ncq_error(struct ata_link *link)
e8ee8451 1380{
0260731f
TH
1381 struct ata_port *ap = link->ap;
1382 struct ata_eh_context *ehc = &link->eh_context;
1383 struct ata_device *dev = link->device;
e8ee8451
TH
1384 struct ata_queued_cmd *qc;
1385 struct ata_taskfile tf;
1386 int tag, rc;
1387
1388 /* if frozen, we can't do much */
b51e9e5d 1389 if (ap->pflags & ATA_PFLAG_FROZEN)
e8ee8451
TH
1390 return;
1391
1392 /* is it NCQ device error? */
0260731f 1393 if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
e8ee8451
TH
1394 return;
1395
1396 /* has LLDD analyzed already? */
1397 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1398 qc = __ata_qc_from_tag(ap, tag);
1399
1400 if (!(qc->flags & ATA_QCFLAG_FAILED))
1401 continue;
1402
1403 if (qc->err_mask)
1404 return;
1405 }
1406
1407 /* okay, this error is ours */
1408 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1409 if (rc) {
0260731f 1410 ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
e8ee8451
TH
1411 "(errno=%d)\n", rc);
1412 return;
1413 }
1414
0260731f
TH
1415 if (!(link->sactive & (1 << tag))) {
1416 ata_link_printk(link, KERN_ERR, "log page 10h reported "
e8ee8451
TH
1417 "inactive tag %d\n", tag);
1418 return;
1419 }
1420
1421 /* we've got the perpetrator, condemn it */
1422 qc = __ata_qc_from_tag(ap, tag);
1423 memcpy(&qc->result_tf, &tf, sizeof(tf));
5335b729 1424 qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
e8ee8451
TH
1425 ehc->i.err_mask &= ~AC_ERR_DEV;
1426}
1427
022bdb07
TH
1428/**
1429 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1430 * @qc: qc to analyze
1431 * @tf: Taskfile registers to analyze
1432 *
1433 * Analyze taskfile of @qc and further determine cause of
1434 * failure. This function also requests ATAPI sense data if
1435 * avaliable.
1436 *
1437 * LOCKING:
1438 * Kernel thread context (may sleep).
1439 *
1440 * RETURNS:
1441 * Determined recovery action
1442 */
1443static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1444 const struct ata_taskfile *tf)
1445{
1446 unsigned int tmp, action = 0;
1447 u8 stat = tf->command, err = tf->feature;
1448
1449 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1450 qc->err_mask |= AC_ERR_HSM;
1451 return ATA_EH_SOFTRESET;
1452 }
1453
a51d644a
TH
1454 if (stat & (ATA_ERR | ATA_DF))
1455 qc->err_mask |= AC_ERR_DEV;
1456 else
022bdb07
TH
1457 return 0;
1458
1459 switch (qc->dev->class) {
1460 case ATA_DEV_ATA:
1461 if (err & ATA_ICRC)
1462 qc->err_mask |= AC_ERR_ATA_BUS;
1463 if (err & ATA_UNC)
1464 qc->err_mask |= AC_ERR_MEDIA;
1465 if (err & ATA_IDNF)
1466 qc->err_mask |= AC_ERR_INVALID;
1467 break;
1468
1469 case ATA_DEV_ATAPI:
a569a30d 1470 if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
56287768 1471 tmp = atapi_eh_request_sense(qc);
a569a30d
TH
1472 if (!tmp) {
1473 /* ATA_QCFLAG_SENSE_VALID is used to
1474 * tell atapi_qc_complete() that sense
1475 * data is already valid.
1476 *
1477 * TODO: interpret sense data and set
1478 * appropriate err_mask.
1479 */
1480 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1481 } else
1482 qc->err_mask |= tmp;
1483 }
022bdb07
TH
1484 }
1485
1486 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1487 action |= ATA_EH_SOFTRESET;
1488
1489 return action;
1490}
1491
76326ac1
TH
1492static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask,
1493 int *xfer_ok)
022bdb07 1494{
76326ac1
TH
1495 int base = 0;
1496
1497 if (!(eflags & ATA_EFLAG_DUBIOUS_XFER))
1498 *xfer_ok = 1;
1499
1500 if (!*xfer_ok)
75f9cafc 1501 base = ATA_ECAT_DUBIOUS_NONE;
76326ac1 1502
7d47e8d4 1503 if (err_mask & AC_ERR_ATA_BUS)
76326ac1 1504 return base + ATA_ECAT_ATA_BUS;
022bdb07 1505
7d47e8d4 1506 if (err_mask & AC_ERR_TIMEOUT)
76326ac1 1507 return base + ATA_ECAT_TOUT_HSM;
7d47e8d4 1508
3884f7b0 1509 if (eflags & ATA_EFLAG_IS_IO) {
7d47e8d4 1510 if (err_mask & AC_ERR_HSM)
76326ac1 1511 return base + ATA_ECAT_TOUT_HSM;
7d47e8d4
TH
1512 if ((err_mask &
1513 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
76326ac1 1514 return base + ATA_ECAT_UNK_DEV;
022bdb07
TH
1515 }
1516
1517 return 0;
1518}
1519
7d47e8d4 1520struct speed_down_verdict_arg {
022bdb07 1521 u64 since;
76326ac1 1522 int xfer_ok;
3884f7b0 1523 int nr_errors[ATA_ECAT_NR];
022bdb07
TH
1524};
1525
7d47e8d4 1526static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
022bdb07 1527{
7d47e8d4 1528 struct speed_down_verdict_arg *arg = void_arg;
76326ac1 1529 int cat;
022bdb07
TH
1530
1531 if (ent->timestamp < arg->since)
1532 return -1;
1533
76326ac1
TH
1534 cat = ata_eh_categorize_error(ent->eflags, ent->err_mask,
1535 &arg->xfer_ok);
7d47e8d4 1536 arg->nr_errors[cat]++;
76326ac1 1537
022bdb07
TH
1538 return 0;
1539}
1540
1541/**
7d47e8d4 1542 * ata_eh_speed_down_verdict - Determine speed down verdict
022bdb07
TH
1543 * @dev: Device of interest
1544 *
1545 * This function examines error ring of @dev and determines
7d47e8d4
TH
1546 * whether NCQ needs to be turned off, transfer speed should be
1547 * stepped down, or falling back to PIO is necessary.
022bdb07 1548 *
3884f7b0
TH
1549 * ECAT_ATA_BUS : ATA_BUS error for any command
1550 *
1551 * ECAT_TOUT_HSM : TIMEOUT for any command or HSM violation for
1552 * IO commands
1553 *
1554 * ECAT_UNK_DEV : Unknown DEV error for IO commands
1555 *
76326ac1
TH
1556 * ECAT_DUBIOUS_* : Identical to above three but occurred while
1557 * data transfer hasn't been verified.
1558 *
3884f7b0
TH
1559 * Verdicts are
1560 *
1561 * NCQ_OFF : Turn off NCQ.
022bdb07 1562 *
3884f7b0
TH
1563 * SPEED_DOWN : Speed down transfer speed but don't fall back
1564 * to PIO.
7d47e8d4 1565 *
3884f7b0 1566 * FALLBACK_TO_PIO : Fall back to PIO.
022bdb07 1567 *
3884f7b0 1568 * Even if multiple verdicts are returned, only one action is
76326ac1
TH
1569 * taken per error. An action triggered by non-DUBIOUS errors
1570 * clears ering, while one triggered by DUBIOUS_* errors doesn't.
1571 * This is to expedite speed down decisions right after device is
1572 * initially configured.
1573 *
1574 * The followings are speed down rules. #1 and #2 deal with
1575 * DUBIOUS errors.
7d47e8d4 1576 *
76326ac1
TH
1577 * 1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors
1578 * occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO.
1579 *
1580 * 2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors
1581 * occurred during last 5 mins, NCQ_OFF.
1582 *
1583 * 3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors
3884f7b0 1584 * ocurred during last 5 mins, FALLBACK_TO_PIO
7d47e8d4 1585 *
76326ac1 1586 * 4. If more than 3 TOUT_HSM or UNK_DEV errors occurred
3884f7b0
TH
1587 * during last 10 mins, NCQ_OFF.
1588 *
76326ac1 1589 * 5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6
3884f7b0 1590 * UNK_DEV errors occurred during last 10 mins, SPEED_DOWN.
7d47e8d4 1591 *
022bdb07
TH
1592 * LOCKING:
1593 * Inherited from caller.
1594 *
1595 * RETURNS:
7d47e8d4 1596 * OR of ATA_EH_SPDN_* flags.
022bdb07 1597 */
7d47e8d4 1598static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
022bdb07 1599{
7d47e8d4
TH
1600 const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
1601 u64 j64 = get_jiffies_64();
1602 struct speed_down_verdict_arg arg;
1603 unsigned int verdict = 0;
022bdb07 1604
3884f7b0 1605 /* scan past 5 mins of error history */
7d47e8d4 1606 memset(&arg, 0, sizeof(arg));
3884f7b0 1607 arg.since = j64 - min(j64, j5mins);
7d47e8d4 1608 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
022bdb07 1609
76326ac1
TH
1610 if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] +
1611 arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1)
1612 verdict |= ATA_EH_SPDN_SPEED_DOWN |
1613 ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS;
1614
1615 if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] +
1616 arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1)
1617 verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS;
1618
3884f7b0
TH
1619 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1620 arg.nr_errors[ATA_ECAT_TOUT_HSM] +
663f99b8 1621 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
3884f7b0 1622 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
022bdb07 1623
3884f7b0 1624 /* scan past 10 mins of error history */
022bdb07 1625 memset(&arg, 0, sizeof(arg));
3884f7b0 1626 arg.since = j64 - min(j64, j10mins);
7d47e8d4 1627 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
022bdb07 1628
3884f7b0
TH
1629 if (arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1630 arg.nr_errors[ATA_ECAT_UNK_DEV] > 3)
1631 verdict |= ATA_EH_SPDN_NCQ_OFF;
1632
1633 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1634 arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 ||
663f99b8 1635 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
3884f7b0 1636 verdict |= ATA_EH_SPDN_SPEED_DOWN;
022bdb07 1637
7d47e8d4 1638 return verdict;
022bdb07
TH
1639}
1640
1641/**
1642 * ata_eh_speed_down - record error and speed down if necessary
1643 * @dev: Failed device
3884f7b0 1644 * @eflags: mask of ATA_EFLAG_* flags
022bdb07
TH
1645 * @err_mask: err_mask of the error
1646 *
1647 * Record error and examine error history to determine whether
1648 * adjusting transmission speed is necessary. It also sets
1649 * transmission limits appropriately if such adjustment is
1650 * necessary.
1651 *
1652 * LOCKING:
1653 * Kernel thread context (may sleep).
1654 *
1655 * RETURNS:
7d47e8d4 1656 * Determined recovery action.
022bdb07 1657 */
3884f7b0
TH
1658static unsigned int ata_eh_speed_down(struct ata_device *dev,
1659 unsigned int eflags, unsigned int err_mask)
022bdb07 1660{
3884f7b0 1661 struct ata_link *link = dev->link;
76326ac1 1662 int xfer_ok = 0;
7d47e8d4
TH
1663 unsigned int verdict;
1664 unsigned int action = 0;
1665
1666 /* don't bother if Cat-0 error */
76326ac1 1667 if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0)
022bdb07
TH
1668 return 0;
1669
1670 /* record error and determine whether speed down is necessary */
3884f7b0 1671 ata_ering_record(&dev->ering, eflags, err_mask);
7d47e8d4 1672 verdict = ata_eh_speed_down_verdict(dev);
022bdb07 1673
7d47e8d4
TH
1674 /* turn off NCQ? */
1675 if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
1676 (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
1677 ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
1678 dev->flags |= ATA_DFLAG_NCQ_OFF;
1679 ata_dev_printk(dev, KERN_WARNING,
1680 "NCQ disabled due to excessive errors\n");
1681 goto done;
1682 }
022bdb07 1683
7d47e8d4
TH
1684 /* speed down? */
1685 if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1686 /* speed down SATA link speed if possible */
3884f7b0 1687 if (sata_down_spd_limit(link) == 0) {
7d47e8d4
TH
1688 action |= ATA_EH_HARDRESET;
1689 goto done;
1690 }
022bdb07 1691
7d47e8d4
TH
1692 /* lower transfer mode */
1693 if (dev->spdn_cnt < 2) {
1694 static const int dma_dnxfer_sel[] =
1695 { ATA_DNXFER_DMA, ATA_DNXFER_40C };
1696 static const int pio_dnxfer_sel[] =
1697 { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
1698 int sel;
1699
1700 if (dev->xfer_shift != ATA_SHIFT_PIO)
1701 sel = dma_dnxfer_sel[dev->spdn_cnt];
1702 else
1703 sel = pio_dnxfer_sel[dev->spdn_cnt];
1704
1705 dev->spdn_cnt++;
1706
1707 if (ata_down_xfermask_limit(dev, sel) == 0) {
1708 action |= ATA_EH_SOFTRESET;
1709 goto done;
1710 }
1711 }
1712 }
1713
1714 /* Fall back to PIO? Slowing down to PIO is meaningless for
663f99b8 1715 * SATA ATA devices. Consider it only for PATA and SATAPI.
7d47e8d4
TH
1716 */
1717 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
663f99b8 1718 (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) &&
7d47e8d4
TH
1719 (dev->xfer_shift != ATA_SHIFT_PIO)) {
1720 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
1721 dev->spdn_cnt = 0;
1722 action |= ATA_EH_SOFTRESET;
1723 goto done;
1724 }
1725 }
022bdb07 1726
022bdb07 1727 return 0;
7d47e8d4
TH
1728 done:
1729 /* device has been slowed down, blow error history */
76326ac1
TH
1730 if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS))
1731 ata_ering_clear(&dev->ering);
7d47e8d4 1732 return action;
022bdb07
TH
1733}
1734
1735/**
9b1e2658
TH
1736 * ata_eh_link_autopsy - analyze error and determine recovery action
1737 * @link: host link to perform autopsy on
022bdb07 1738 *
0260731f
TH
1739 * Analyze why @link failed and determine which recovery actions
1740 * are needed. This function also sets more detailed AC_ERR_*
1741 * values and fills sense data for ATAPI CHECK SENSE.
022bdb07
TH
1742 *
1743 * LOCKING:
1744 * Kernel thread context (may sleep).
1745 */
9b1e2658 1746static void ata_eh_link_autopsy(struct ata_link *link)
022bdb07 1747{
0260731f 1748 struct ata_port *ap = link->ap;
936fd732 1749 struct ata_eh_context *ehc = &link->eh_context;
dfcc173d 1750 struct ata_device *dev;
3884f7b0
TH
1751 unsigned int all_err_mask = 0, eflags = 0;
1752 int tag;
022bdb07
TH
1753 u32 serror;
1754 int rc;
1755
1756 DPRINTK("ENTER\n");
1757
1cdaf534
TH
1758 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1759 return;
1760
022bdb07 1761 /* obtain and analyze SError */
936fd732 1762 rc = sata_scr_read(link, SCR_ERROR, &serror);
022bdb07
TH
1763 if (rc == 0) {
1764 ehc->i.serror |= serror;
0260731f 1765 ata_eh_analyze_serror(link);
4e57c517
TH
1766 } else if (rc != -EOPNOTSUPP) {
1767 /* SError read failed, force hardreset and probing */
1768 ata_ehi_schedule_probe(&ehc->i);
4528e4da 1769 ehc->i.action |= ATA_EH_HARDRESET;
4e57c517
TH
1770 ehc->i.err_mask |= AC_ERR_OTHER;
1771 }
022bdb07 1772
e8ee8451 1773 /* analyze NCQ failure */
0260731f 1774 ata_eh_analyze_ncq_error(link);
e8ee8451 1775
022bdb07
TH
1776 /* any real error trumps AC_ERR_OTHER */
1777 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1778 ehc->i.err_mask &= ~AC_ERR_OTHER;
1779
1780 all_err_mask |= ehc->i.err_mask;
1781
1782 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1783 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1784
0260731f 1785 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
022bdb07
TH
1786 continue;
1787
1788 /* inherit upper level err_mask */
1789 qc->err_mask |= ehc->i.err_mask;
1790
022bdb07 1791 /* analyze TF */
4528e4da 1792 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
022bdb07
TH
1793
1794 /* DEV errors are probably spurious in case of ATA_BUS error */
1795 if (qc->err_mask & AC_ERR_ATA_BUS)
1796 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1797 AC_ERR_INVALID);
1798
1799 /* any real error trumps unknown error */
1800 if (qc->err_mask & ~AC_ERR_OTHER)
1801 qc->err_mask &= ~AC_ERR_OTHER;
1802
1803 /* SENSE_VALID trumps dev/unknown error and revalidation */
f90f0828 1804 if (qc->flags & ATA_QCFLAG_SENSE_VALID)
022bdb07 1805 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
022bdb07
TH
1806
1807 /* accumulate error info */
4528e4da 1808 ehc->i.dev = qc->dev;
022bdb07
TH
1809 all_err_mask |= qc->err_mask;
1810 if (qc->flags & ATA_QCFLAG_IO)
3884f7b0 1811 eflags |= ATA_EFLAG_IS_IO;
022bdb07
TH
1812 }
1813
a20f33ff 1814 /* enforce default EH actions */
b51e9e5d 1815 if (ap->pflags & ATA_PFLAG_FROZEN ||
a20f33ff 1816 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
4528e4da 1817 ehc->i.action |= ATA_EH_SOFTRESET;
3884f7b0
TH
1818 else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) ||
1819 (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV)))
4528e4da 1820 ehc->i.action |= ATA_EH_REVALIDATE;
022bdb07 1821
dfcc173d
TH
1822 /* If we have offending qcs and the associated failed device,
1823 * perform per-dev EH action only on the offending device.
1824 */
4528e4da 1825 if (ehc->i.dev) {
4528e4da
TH
1826 ehc->i.dev_action[ehc->i.dev->devno] |=
1827 ehc->i.action & ATA_EH_PERDEV_MASK;
1828 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
47005f25
TH
1829 }
1830
2695e366
TH
1831 /* propagate timeout to host link */
1832 if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link))
1833 ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT;
1834
1835 /* record error and consider speeding down */
dfcc173d 1836 dev = ehc->i.dev;
2695e366
TH
1837 if (!dev && ((ata_link_max_devices(link) == 1 &&
1838 ata_dev_enabled(link->device))))
1839 dev = link->device;
dfcc173d 1840
76326ac1
TH
1841 if (dev) {
1842 if (dev->flags & ATA_DFLAG_DUBIOUS_XFER)
1843 eflags |= ATA_EFLAG_DUBIOUS_XFER;
3884f7b0 1844 ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask);
76326ac1 1845 }
dfcc173d 1846
022bdb07
TH
1847 DPRINTK("EXIT\n");
1848}
1849
1850/**
9b1e2658
TH
1851 * ata_eh_autopsy - analyze error and determine recovery action
1852 * @ap: host port to perform autopsy on
1853 *
1854 * Analyze all links of @ap and determine why they failed and
1855 * which recovery actions are needed.
1856 *
1857 * LOCKING:
1858 * Kernel thread context (may sleep).
1859 */
fb7fd614 1860void ata_eh_autopsy(struct ata_port *ap)
9b1e2658
TH
1861{
1862 struct ata_link *link;
1863
2695e366 1864 ata_port_for_each_link(link, ap)
9b1e2658 1865 ata_eh_link_autopsy(link);
2695e366
TH
1866
1867 /* Autopsy of fanout ports can affect host link autopsy.
1868 * Perform host link autopsy last.
1869 */
1870 if (ap->nr_pmp_links)
1871 ata_eh_link_autopsy(&ap->link);
9b1e2658
TH
1872}
1873
1874/**
1875 * ata_eh_link_report - report error handling to user
0260731f 1876 * @link: ATA link EH is going on
022bdb07
TH
1877 *
1878 * Report EH to user.
1879 *
1880 * LOCKING:
1881 * None.
1882 */
9b1e2658 1883static void ata_eh_link_report(struct ata_link *link)
022bdb07 1884{
0260731f
TH
1885 struct ata_port *ap = link->ap;
1886 struct ata_eh_context *ehc = &link->eh_context;
022bdb07 1887 const char *frozen, *desc;
a1e10f7e 1888 char tries_buf[6];
022bdb07
TH
1889 int tag, nr_failed = 0;
1890
94ff3d54
TH
1891 if (ehc->i.flags & ATA_EHI_QUIET)
1892 return;
1893
022bdb07
TH
1894 desc = NULL;
1895 if (ehc->i.desc[0] != '\0')
1896 desc = ehc->i.desc;
1897
1898 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1899 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1900
e027bd36
TH
1901 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link ||
1902 ((qc->flags & ATA_QCFLAG_QUIET) &&
1903 qc->err_mask == AC_ERR_DEV))
022bdb07
TH
1904 continue;
1905 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1906 continue;
1907
1908 nr_failed++;
1909 }
1910
1911 if (!nr_failed && !ehc->i.err_mask)
1912 return;
1913
1914 frozen = "";
b51e9e5d 1915 if (ap->pflags & ATA_PFLAG_FROZEN)
022bdb07
TH
1916 frozen = " frozen";
1917
a1e10f7e
TH
1918 memset(tries_buf, 0, sizeof(tries_buf));
1919 if (ap->eh_tries < ATA_EH_MAX_TRIES)
1920 snprintf(tries_buf, sizeof(tries_buf) - 1, " t%d",
1921 ap->eh_tries);
1922
022bdb07 1923 if (ehc->i.dev) {
e8ee8451 1924 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
a1e10f7e
TH
1925 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1926 ehc->i.err_mask, link->sactive, ehc->i.serror,
1927 ehc->i.action, frozen, tries_buf);
022bdb07 1928 if (desc)
b64bbc39 1929 ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
022bdb07 1930 } else {
0260731f 1931 ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
a1e10f7e
TH
1932 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1933 ehc->i.err_mask, link->sactive, ehc->i.serror,
1934 ehc->i.action, frozen, tries_buf);
022bdb07 1935 if (desc)
0260731f 1936 ata_link_printk(link, KERN_ERR, "%s\n", desc);
022bdb07
TH
1937 }
1938
1333e194
RH
1939 if (ehc->i.serror)
1940 ata_port_printk(ap, KERN_ERR,
1941 "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
1942 ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
1943 ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
1944 ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
1945 ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
1946 ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
1947 ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
1948 ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
1949 ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
1950 ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
1951 ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
1952 ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
1953 ehc->i.serror & SERR_CRC ? "BadCRC " : "",
1954 ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
1955 ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
1956 ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
1957 ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
2dcb407e 1958 ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
1333e194 1959
022bdb07
TH
1960 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1961 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
8a937581 1962 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
abb6a889
TH
1963 const u8 *cdb = qc->cdb;
1964 char data_buf[20] = "";
1965 char cdb_buf[70] = "";
022bdb07 1966
0260731f
TH
1967 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
1968 qc->dev->link != link || !qc->err_mask)
022bdb07
TH
1969 continue;
1970
abb6a889
TH
1971 if (qc->dma_dir != DMA_NONE) {
1972 static const char *dma_str[] = {
1973 [DMA_BIDIRECTIONAL] = "bidi",
1974 [DMA_TO_DEVICE] = "out",
1975 [DMA_FROM_DEVICE] = "in",
1976 };
1977 static const char *prot_str[] = {
1978 [ATA_PROT_PIO] = "pio",
1979 [ATA_PROT_DMA] = "dma",
1980 [ATA_PROT_NCQ] = "ncq",
0dc36888
TH
1981 [ATAPI_PROT_PIO] = "pio",
1982 [ATAPI_PROT_DMA] = "dma",
abb6a889
TH
1983 };
1984
1985 snprintf(data_buf, sizeof(data_buf), " %s %u %s",
1986 prot_str[qc->tf.protocol], qc->nbytes,
1987 dma_str[qc->dma_dir]);
1988 }
1989
e39eec13 1990 if (ata_is_atapi(qc->tf.protocol))
abb6a889
TH
1991 snprintf(cdb_buf, sizeof(cdb_buf),
1992 "cdb %02x %02x %02x %02x %02x %02x %02x %02x "
1993 "%02x %02x %02x %02x %02x %02x %02x %02x\n ",
1994 cdb[0], cdb[1], cdb[2], cdb[3],
1995 cdb[4], cdb[5], cdb[6], cdb[7],
1996 cdb[8], cdb[9], cdb[10], cdb[11],
1997 cdb[12], cdb[13], cdb[14], cdb[15]);
1998
8a937581
TH
1999 ata_dev_printk(qc->dev, KERN_ERR,
2000 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
abb6a889 2001 "tag %d%s\n %s"
8a937581 2002 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
5335b729 2003 "Emask 0x%x (%s)%s\n",
8a937581
TH
2004 cmd->command, cmd->feature, cmd->nsect,
2005 cmd->lbal, cmd->lbam, cmd->lbah,
2006 cmd->hob_feature, cmd->hob_nsect,
2007 cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
abb6a889 2008 cmd->device, qc->tag, data_buf, cdb_buf,
8a937581
TH
2009 res->command, res->feature, res->nsect,
2010 res->lbal, res->lbam, res->lbah,
2011 res->hob_feature, res->hob_nsect,
2012 res->hob_lbal, res->hob_lbam, res->hob_lbah,
5335b729
TH
2013 res->device, qc->err_mask, ata_err_string(qc->err_mask),
2014 qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
1333e194
RH
2015
2016 if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
2dcb407e 2017 ATA_ERR)) {
1333e194
RH
2018 if (res->command & ATA_BUSY)
2019 ata_dev_printk(qc->dev, KERN_ERR,
2dcb407e 2020 "status: { Busy }\n");
1333e194
RH
2021 else
2022 ata_dev_printk(qc->dev, KERN_ERR,
2023 "status: { %s%s%s%s}\n",
2024 res->command & ATA_DRDY ? "DRDY " : "",
2025 res->command & ATA_DF ? "DF " : "",
2026 res->command & ATA_DRQ ? "DRQ " : "",
2dcb407e 2027 res->command & ATA_ERR ? "ERR " : "");
1333e194
RH
2028 }
2029
2030 if (cmd->command != ATA_CMD_PACKET &&
2031 (res->feature & (ATA_ICRC | ATA_UNC | ATA_IDNF |
2032 ATA_ABORTED)))
2033 ata_dev_printk(qc->dev, KERN_ERR,
2034 "error: { %s%s%s%s}\n",
2035 res->feature & ATA_ICRC ? "ICRC " : "",
2036 res->feature & ATA_UNC ? "UNC " : "",
2037 res->feature & ATA_IDNF ? "IDNF " : "",
2dcb407e 2038 res->feature & ATA_ABORTED ? "ABRT " : "");
022bdb07
TH
2039 }
2040}
2041
9b1e2658
TH
2042/**
2043 * ata_eh_report - report error handling to user
2044 * @ap: ATA port to report EH about
2045 *
2046 * Report EH to user.
2047 *
2048 * LOCKING:
2049 * None.
2050 */
fb7fd614 2051void ata_eh_report(struct ata_port *ap)
9b1e2658
TH
2052{
2053 struct ata_link *link;
2054
2055 __ata_port_for_each_link(link, ap)
2056 ata_eh_link_report(link);
2057}
2058
cc0680a5 2059static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
d4b2bab4 2060 unsigned int *classes, unsigned long deadline)
d87fa38e 2061{
f58229f8
TH
2062 struct ata_device *dev;
2063 int rc;
d87fa38e 2064
cc0680a5 2065 ata_link_for_each_dev(dev, link)
f58229f8 2066 classes[dev->devno] = ATA_DEV_UNKNOWN;
d87fa38e 2067
cc0680a5 2068 rc = reset(link, classes, deadline);
d87fa38e
TH
2069 if (rc)
2070 return rc;
2071
2072 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2073 * is complete and convert all ATA_DEV_UNKNOWN to
2074 * ATA_DEV_NONE.
2075 */
cc0680a5 2076 ata_link_for_each_dev(dev, link)
f58229f8 2077 if (classes[dev->devno] != ATA_DEV_UNKNOWN)
d87fa38e
TH
2078 break;
2079
f58229f8 2080 if (dev) {
cc0680a5 2081 ata_link_for_each_dev(dev, link) {
f58229f8
TH
2082 if (classes[dev->devno] == ATA_DEV_UNKNOWN)
2083 classes[dev->devno] = ATA_DEV_NONE;
2084 }
2085 }
d87fa38e
TH
2086
2087 return 0;
2088}
2089
ae791c05
TH
2090static int ata_eh_followup_srst_needed(struct ata_link *link,
2091 int rc, int classify,
664faf09
TH
2092 const unsigned int *classes)
2093{
ae791c05
TH
2094 if (link->flags & ATA_LFLAG_NO_SRST)
2095 return 0;
664faf09
TH
2096 if (rc == -EAGAIN)
2097 return 1;
2098 if (rc != 0)
2099 return 0;
3495de73
TH
2100 if ((link->ap->flags & ATA_FLAG_PMP) && ata_is_host_link(link))
2101 return 1;
ae791c05
TH
2102 if (classify && !(link->flags & ATA_LFLAG_ASSUME_CLASS) &&
2103 classes[0] == ATA_DEV_UNKNOWN)
664faf09
TH
2104 return 1;
2105 return 0;
2106}
2107
fb7fd614
TH
2108int ata_eh_reset(struct ata_link *link, int classify,
2109 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
2110 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
022bdb07 2111{
416dc9ed 2112 const int max_tries = ARRAY_SIZE(ata_eh_reset_timeouts);
afaa5c37 2113 struct ata_port *ap = link->ap;
936fd732 2114 struct ata_eh_context *ehc = &link->eh_context;
664faf09 2115 unsigned int *classes = ehc->classes;
416dc9ed 2116 unsigned int lflags = link->flags;
1cdaf534 2117 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
31daabda 2118 int try = 0;
f58229f8 2119 struct ata_device *dev;
416dc9ed 2120 unsigned long deadline, now;
0e06d9ce 2121 unsigned int tmp_action;
022bdb07 2122 ata_reset_fn_t reset;
afaa5c37 2123 unsigned long flags;
416dc9ed 2124 u32 sstatus;
f58229f8 2125 int rc;
022bdb07 2126
13abf50d 2127 /* about to reset */
afaa5c37
TH
2128 spin_lock_irqsave(ap->lock, flags);
2129 ap->pflags |= ATA_PFLAG_RESETTING;
2130 spin_unlock_irqrestore(ap->lock, flags);
2131
955e57df 2132 ata_eh_about_to_do(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
13abf50d 2133
cdeab114
TH
2134 ata_link_for_each_dev(dev, link) {
2135 /* If we issue an SRST then an ATA drive (not ATAPI)
2136 * may change configuration and be in PIO0 timing. If
2137 * we do a hard reset (or are coming from power on)
2138 * this is true for ATA or ATAPI. Until we've set a
2139 * suitable controller mode we should not touch the
2140 * bus as we may be talking too fast.
2141 */
2142 dev->pio_mode = XFER_PIO_0;
2143
2144 /* If the controller has a pio mode setup function
2145 * then use it to set the chipset to rights. Don't
2146 * touch the DMA setup as that will be dealt with when
2147 * configuring devices.
2148 */
2149 if (ap->ops->set_piomode)
2150 ap->ops->set_piomode(ap, dev);
2151 }
2152
f5914a46
TH
2153 /* Determine which reset to use and record in ehc->i.action.
2154 * prereset() may examine and modify it.
2155 */
416dc9ed 2156 if (softreset && (!hardreset || (!(lflags & ATA_LFLAG_NO_SRST) &&
ae791c05 2157 !sata_set_spd_needed(link) &&
0e06d9ce
TH
2158 !(ehc->i.action & ATA_EH_HARDRESET))))
2159 tmp_action = ATA_EH_SOFTRESET;
022bdb07 2160 else
0e06d9ce
TH
2161 tmp_action = ATA_EH_HARDRESET;
2162
2163 ehc->i.action = (ehc->i.action & ~ATA_EH_RESET_MASK) | tmp_action;
f5914a46
TH
2164
2165 if (prereset) {
cc0680a5 2166 rc = prereset(link, jiffies + ATA_EH_PRERESET_TIMEOUT);
f5914a46 2167 if (rc) {
c961922b 2168 if (rc == -ENOENT) {
cc0680a5 2169 ata_link_printk(link, KERN_DEBUG,
4aa9ab67 2170 "port disabled. ignoring.\n");
9af5c9c9 2171 ehc->i.action &= ~ATA_EH_RESET_MASK;
4aa9ab67 2172
936fd732 2173 ata_link_for_each_dev(dev, link)
f58229f8 2174 classes[dev->devno] = ATA_DEV_NONE;
4aa9ab67
TH
2175
2176 rc = 0;
c961922b 2177 } else
cc0680a5 2178 ata_link_printk(link, KERN_ERR,
f5914a46 2179 "prereset failed (errno=%d)\n", rc);
fccb6ea5 2180 goto out;
f5914a46
TH
2181 }
2182 }
2183
2184 /* prereset() might have modified ehc->i.action */
2185 if (ehc->i.action & ATA_EH_HARDRESET)
022bdb07 2186 reset = hardreset;
f5914a46
TH
2187 else if (ehc->i.action & ATA_EH_SOFTRESET)
2188 reset = softreset;
2189 else {
2190 /* prereset told us not to reset, bang classes and return */
936fd732 2191 ata_link_for_each_dev(dev, link)
f58229f8 2192 classes[dev->devno] = ATA_DEV_NONE;
fccb6ea5
TH
2193 rc = 0;
2194 goto out;
f5914a46
TH
2195 }
2196
2197 /* did prereset() screw up? if so, fix up to avoid oopsing */
2198 if (!reset) {
f5914a46
TH
2199 if (softreset)
2200 reset = softreset;
2201 else
2202 reset = hardreset;
2203 }
022bdb07
TH
2204
2205 retry:
31daabda
TH
2206 deadline = jiffies + ata_eh_reset_timeouts[try++];
2207
3e706399
TH
2208 /* shut up during boot probing */
2209 if (verbose)
cc0680a5 2210 ata_link_printk(link, KERN_INFO, "%s resetting link\n",
3e706399 2211 reset == softreset ? "soft" : "hard");
022bdb07 2212
13abf50d 2213 /* mark that this EH session started with reset */
0d64a233
TH
2214 if (reset == hardreset)
2215 ehc->i.flags |= ATA_EHI_DID_HARDRESET;
2216 else
2217 ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
022bdb07 2218
cc0680a5 2219 rc = ata_do_reset(link, reset, classes, deadline);
022bdb07 2220
664faf09 2221 if (reset == hardreset &&
ae791c05 2222 ata_eh_followup_srst_needed(link, rc, classify, classes)) {
664faf09 2223 /* okay, let's do follow-up softreset */
664faf09
TH
2224 reset = softreset;
2225
2226 if (!reset) {
cc0680a5 2227 ata_link_printk(link, KERN_ERR,
664faf09
TH
2228 "follow-up softreset required "
2229 "but no softreset avaliable\n");
fccb6ea5 2230 rc = -EINVAL;
08cf69d0 2231 goto fail;
664faf09
TH
2232 }
2233
955e57df 2234 ata_eh_about_to_do(link, NULL, ATA_EH_RESET_MASK);
cc0680a5 2235 rc = ata_do_reset(link, reset, classes, deadline);
664faf09
TH
2236 }
2237
416dc9ed
TH
2238 /* -EAGAIN can happen if we skipped followup SRST */
2239 if (rc && rc != -EAGAIN)
2240 goto fail;
664faf09 2241
08cf69d0
TH
2242 /* was classification successful? */
2243 if (classify && classes[0] == ATA_DEV_UNKNOWN &&
2244 !(lflags & ATA_LFLAG_ASSUME_CLASS)) {
2245 if (try < max_tries) {
2246 ata_link_printk(link, KERN_WARNING,
2247 "classification failed\n");
2248 rc = -EINVAL;
2249 goto fail;
2250 }
2251
2252 ata_link_printk(link, KERN_WARNING,
2253 "classfication failed, assuming ATA\n");
2254 lflags |= ATA_LFLAG_ASSUME_ATA;
2255 }
2256
416dc9ed
TH
2257 ata_link_for_each_dev(dev, link) {
2258 /* After the reset, the device state is PIO 0 and the
2259 * controller state is undefined. Reset also wakes up
2260 * drives from sleeping mode.
2261 */
2262 dev->pio_mode = XFER_PIO_0;
2263 dev->flags &= ~ATA_DFLAG_SLEEPING;
31daabda 2264
416dc9ed
TH
2265 if (ata_link_offline(link))
2266 continue;
022bdb07 2267
4ccd3329 2268 /* apply class override */
416dc9ed
TH
2269 if (lflags & ATA_LFLAG_ASSUME_ATA)
2270 classes[dev->devno] = ATA_DEV_ATA;
2271 else if (lflags & ATA_LFLAG_ASSUME_SEMB)
2272 classes[dev->devno] = ATA_DEV_SEMB_UNSUP; /* not yet */
022bdb07
TH
2273 }
2274
416dc9ed
TH
2275 /* record current link speed */
2276 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2277 link->sata_spd = (sstatus >> 4) & 0xf;
008a7896 2278
416dc9ed
TH
2279 if (postreset)
2280 postreset(link, classes);
20952b69 2281
416dc9ed
TH
2282 /* reset successful, schedule revalidation */
2283 ata_eh_done(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
2284 ehc->i.action |= ATA_EH_REVALIDATE;
ae791c05 2285
416dc9ed 2286 rc = 0;
fccb6ea5
TH
2287 out:
2288 /* clear hotplug flag */
2289 ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
afaa5c37
TH
2290
2291 spin_lock_irqsave(ap->lock, flags);
2292 ap->pflags &= ~ATA_PFLAG_RESETTING;
2293 spin_unlock_irqrestore(ap->lock, flags);
2294
022bdb07 2295 return rc;
416dc9ed
TH
2296
2297 fail:
2298 if (rc == -ERESTART || try >= max_tries)
2299 goto out;
2300
2301 now = jiffies;
2302 if (time_before(now, deadline)) {
2303 unsigned long delta = deadline - now;
2304
2305 ata_link_printk(link, KERN_WARNING, "reset failed "
2306 "(errno=%d), retrying in %u secs\n",
2307 rc, (jiffies_to_msecs(delta) + 999) / 1000);
2308
2309 while (delta)
2310 delta = schedule_timeout_uninterruptible(delta);
2311 }
2312
2313 if (rc == -EPIPE || try == max_tries - 1)
2314 sata_down_spd_limit(link);
2315 if (hardreset)
2316 reset = hardreset;
2317 goto retry;
022bdb07
TH
2318}
2319
0260731f 2320static int ata_eh_revalidate_and_attach(struct ata_link *link,
084fe639 2321 struct ata_device **r_failed_dev)
022bdb07 2322{
0260731f
TH
2323 struct ata_port *ap = link->ap;
2324 struct ata_eh_context *ehc = &link->eh_context;
022bdb07 2325 struct ata_device *dev;
8c3c52a8 2326 unsigned int new_mask = 0;
084fe639 2327 unsigned long flags;
f58229f8 2328 int rc = 0;
022bdb07
TH
2329
2330 DPRINTK("ENTER\n");
2331
8c3c52a8
TH
2332 /* For PATA drive side cable detection to work, IDENTIFY must
2333 * be done backwards such that PDIAG- is released by the slave
2334 * device before the master device is identified.
2335 */
0260731f 2336 ata_link_for_each_dev_reverse(dev, link) {
f58229f8
TH
2337 unsigned int action = ata_eh_dev_action(dev);
2338 unsigned int readid_flags = 0;
022bdb07 2339
bff04647
TH
2340 if (ehc->i.flags & ATA_EHI_DID_RESET)
2341 readid_flags |= ATA_READID_POSTRESET;
2342
9666f400 2343 if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
633273a3
TH
2344 WARN_ON(dev->class == ATA_DEV_PMP);
2345
0260731f 2346 if (ata_link_offline(link)) {
022bdb07 2347 rc = -EIO;
8c3c52a8 2348 goto err;
022bdb07
TH
2349 }
2350
0260731f 2351 ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
422c9daa
TH
2352 rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
2353 readid_flags);
022bdb07 2354 if (rc)
8c3c52a8 2355 goto err;
022bdb07 2356
0260731f 2357 ata_eh_done(link, dev, ATA_EH_REVALIDATE);
47005f25 2358
baa1e78a
TH
2359 /* Configuration may have changed, reconfigure
2360 * transfer mode.
2361 */
2362 ehc->i.flags |= ATA_EHI_SETMODE;
2363
3057ac3c 2364 /* schedule the scsi_rescan_device() here */
2365 queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
084fe639
TH
2366 } else if (dev->class == ATA_DEV_UNKNOWN &&
2367 ehc->tries[dev->devno] &&
2368 ata_class_enabled(ehc->classes[dev->devno])) {
2369 dev->class = ehc->classes[dev->devno];
2370
633273a3
TH
2371 if (dev->class == ATA_DEV_PMP)
2372 rc = sata_pmp_attach(dev);
2373 else
2374 rc = ata_dev_read_id(dev, &dev->class,
2375 readid_flags, dev->id);
8c3c52a8
TH
2376 switch (rc) {
2377 case 0:
f58229f8 2378 new_mask |= 1 << dev->devno;
8c3c52a8
TH
2379 break;
2380 case -ENOENT:
55a8e2c8
TH
2381 /* IDENTIFY was issued to non-existent
2382 * device. No need to reset. Just
2383 * thaw and kill the device.
2384 */
2385 ata_eh_thaw_port(ap);
084fe639
TH
2386 dev->class = ATA_DEV_UNKNOWN;
2387 break;
8c3c52a8
TH
2388 default:
2389 dev->class = ATA_DEV_UNKNOWN;
2390 goto err;
084fe639 2391 }
8c3c52a8
TH
2392 }
2393 }
084fe639 2394
c1c4e8d5 2395 /* PDIAG- should have been released, ask cable type if post-reset */
33267325
TH
2396 if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) {
2397 if (ap->ops->cable_detect)
2398 ap->cbl = ap->ops->cable_detect(ap);
2399 ata_force_cbl(ap);
2400 }
c1c4e8d5 2401
8c3c52a8
TH
2402 /* Configure new devices forward such that user doesn't see
2403 * device detection messages backwards.
2404 */
0260731f 2405 ata_link_for_each_dev(dev, link) {
633273a3
TH
2406 if (!(new_mask & (1 << dev->devno)) ||
2407 dev->class == ATA_DEV_PMP)
8c3c52a8
TH
2408 continue;
2409
2410 ehc->i.flags |= ATA_EHI_PRINTINFO;
2411 rc = ata_dev_configure(dev);
2412 ehc->i.flags &= ~ATA_EHI_PRINTINFO;
2413 if (rc)
2414 goto err;
2415
2416 spin_lock_irqsave(ap->lock, flags);
2417 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
2418 spin_unlock_irqrestore(ap->lock, flags);
2419
2420 /* new device discovered, configure xfermode */
2421 ehc->i.flags |= ATA_EHI_SETMODE;
022bdb07
TH
2422 }
2423
8c3c52a8 2424 return 0;
022bdb07 2425
8c3c52a8
TH
2426 err:
2427 *r_failed_dev = dev;
2428 DPRINTK("EXIT rc=%d\n", rc);
022bdb07
TH
2429 return rc;
2430}
2431
6f1d1e3a
TH
2432/**
2433 * ata_set_mode - Program timings and issue SET FEATURES - XFER
2434 * @link: link on which timings will be programmed
2435 * @r_failed_dev: out paramter for failed device
2436 *
2437 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
2438 * ata_set_mode() fails, pointer to the failing device is
2439 * returned in @r_failed_dev.
2440 *
2441 * LOCKING:
2442 * PCI/etc. bus probe sem.
2443 *
2444 * RETURNS:
2445 * 0 on success, negative errno otherwise
2446 */
2447int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
2448{
2449 struct ata_port *ap = link->ap;
00115e0f
TH
2450 struct ata_device *dev;
2451 int rc;
6f1d1e3a 2452
76326ac1
TH
2453 /* if data transfer is verified, clear DUBIOUS_XFER on ering top */
2454 ata_link_for_each_dev(dev, link) {
2455 if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
2456 struct ata_ering_entry *ent;
2457
2458 ent = ata_ering_top(&dev->ering);
2459 if (ent)
2460 ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER;
2461 }
2462 }
2463
6f1d1e3a
TH
2464 /* has private set_mode? */
2465 if (ap->ops->set_mode)
00115e0f
TH
2466 rc = ap->ops->set_mode(link, r_failed_dev);
2467 else
2468 rc = ata_do_set_mode(link, r_failed_dev);
2469
2470 /* if transfer mode has changed, set DUBIOUS_XFER on device */
2471 ata_link_for_each_dev(dev, link) {
2472 struct ata_eh_context *ehc = &link->eh_context;
2473 u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
2474 u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
2475
2476 if (dev->xfer_mode != saved_xfer_mode ||
2477 ata_ncq_enabled(dev) != saved_ncq)
2478 dev->flags |= ATA_DFLAG_DUBIOUS_XFER;
2479 }
2480
2481 return rc;
6f1d1e3a
TH
2482}
2483
0260731f 2484static int ata_link_nr_enabled(struct ata_link *link)
022bdb07 2485{
f58229f8
TH
2486 struct ata_device *dev;
2487 int cnt = 0;
022bdb07 2488
0260731f 2489 ata_link_for_each_dev(dev, link)
f58229f8 2490 if (ata_dev_enabled(dev))
022bdb07
TH
2491 cnt++;
2492 return cnt;
2493}
2494
0260731f 2495static int ata_link_nr_vacant(struct ata_link *link)
084fe639 2496{
f58229f8
TH
2497 struct ata_device *dev;
2498 int cnt = 0;
084fe639 2499
0260731f 2500 ata_link_for_each_dev(dev, link)
f58229f8 2501 if (dev->class == ATA_DEV_UNKNOWN)
084fe639
TH
2502 cnt++;
2503 return cnt;
2504}
2505
0260731f 2506static int ata_eh_skip_recovery(struct ata_link *link)
084fe639 2507{
0260731f 2508 struct ata_eh_context *ehc = &link->eh_context;
f58229f8 2509 struct ata_device *dev;
084fe639 2510
f9df58cb
TH
2511 /* skip disabled links */
2512 if (link->flags & ATA_LFLAG_DISABLED)
2513 return 1;
2514
7c8c2cff 2515 /* thaw frozen port, resume link and recover failed devices */
0260731f
TH
2516 if ((link->ap->pflags & ATA_PFLAG_FROZEN) ||
2517 (ehc->i.flags & ATA_EHI_RESUME_LINK) || ata_link_nr_enabled(link))
084fe639
TH
2518 return 0;
2519
2520 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
0260731f 2521 ata_link_for_each_dev(dev, link) {
084fe639
TH
2522 if (dev->class == ATA_DEV_UNKNOWN &&
2523 ehc->classes[dev->devno] != ATA_DEV_NONE)
2524 return 0;
2525 }
2526
2527 return 1;
2528}
2529
02c05a27
TH
2530static int ata_eh_schedule_probe(struct ata_device *dev)
2531{
2532 struct ata_eh_context *ehc = &dev->link->eh_context;
2533
2534 if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
2535 (ehc->did_probe_mask & (1 << dev->devno)))
2536 return 0;
2537
2538 ata_eh_detach_dev(dev);
2539 ata_dev_init(dev);
2540 ehc->did_probe_mask |= (1 << dev->devno);
2541 ehc->i.action |= ATA_EH_SOFTRESET;
00115e0f
TH
2542 ehc->saved_xfer_mode[dev->devno] = 0;
2543 ehc->saved_ncq_enabled &= ~(1 << dev->devno);
02c05a27
TH
2544
2545 return 1;
2546}
2547
9b1e2658 2548static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
fee7ca72 2549{
9af5c9c9 2550 struct ata_eh_context *ehc = &dev->link->eh_context;
fee7ca72
TH
2551
2552 ehc->tries[dev->devno]--;
2553
2554 switch (err) {
2555 case -ENODEV:
2556 /* device missing or wrong IDENTIFY data, schedule probing */
2557 ehc->i.probe_mask |= (1 << dev->devno);
2558 case -EINVAL:
2559 /* give it just one more chance */
2560 ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
2561 case -EIO:
4fb4615b 2562 if (ehc->tries[dev->devno] == 1 && dev->pio_mode > XFER_PIO_0) {
fee7ca72
TH
2563 /* This is the last chance, better to slow
2564 * down than lose it.
2565 */
936fd732 2566 sata_down_spd_limit(dev->link);
fee7ca72
TH
2567 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2568 }
2569 }
2570
2571 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2572 /* disable device if it has used up all its chances */
2573 ata_dev_disable(dev);
2574
2575 /* detach if offline */
936fd732 2576 if (ata_link_offline(dev->link))
fee7ca72
TH
2577 ata_eh_detach_dev(dev);
2578
02c05a27
TH
2579 /* schedule probe if necessary */
2580 if (ata_eh_schedule_probe(dev))
fee7ca72 2581 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
9b1e2658
TH
2582
2583 return 1;
fee7ca72
TH
2584 } else {
2585 /* soft didn't work? be haaaaard */
2586 if (ehc->i.flags & ATA_EHI_DID_RESET)
2587 ehc->i.action |= ATA_EH_HARDRESET;
2588 else
2589 ehc->i.action |= ATA_EH_SOFTRESET;
9b1e2658
TH
2590
2591 return 0;
fee7ca72
TH
2592 }
2593}
2594
022bdb07
TH
2595/**
2596 * ata_eh_recover - recover host port after error
2597 * @ap: host port to recover
f5914a46 2598 * @prereset: prereset method (can be NULL)
022bdb07
TH
2599 * @softreset: softreset method (can be NULL)
2600 * @hardreset: hardreset method (can be NULL)
2601 * @postreset: postreset method (can be NULL)
9b1e2658 2602 * @r_failed_link: out parameter for failed link
022bdb07
TH
2603 *
2604 * This is the alpha and omega, eum and yang, heart and soul of
2605 * libata exception handling. On entry, actions required to
9b1e2658
TH
2606 * recover each link and hotplug requests are recorded in the
2607 * link's eh_context. This function executes all the operations
2608 * with appropriate retrials and fallbacks to resurrect failed
084fe639 2609 * devices, detach goners and greet newcomers.
022bdb07
TH
2610 *
2611 * LOCKING:
2612 * Kernel thread context (may sleep).
2613 *
2614 * RETURNS:
2615 * 0 on success, -errno on failure.
2616 */
fb7fd614
TH
2617int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
2618 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2619 ata_postreset_fn_t postreset,
2620 struct ata_link **r_failed_link)
022bdb07 2621{
9b1e2658 2622 struct ata_link *link;
022bdb07 2623 struct ata_device *dev;
9b1e2658
TH
2624 int nr_failed_devs, nr_disabled_devs;
2625 int reset, rc;
f9df58cb 2626 unsigned long flags;
022bdb07
TH
2627
2628 DPRINTK("ENTER\n");
2629
2630 /* prep for recovery */
9b1e2658
TH
2631 ata_port_for_each_link(link, ap) {
2632 struct ata_eh_context *ehc = &link->eh_context;
084fe639 2633
f9df58cb
TH
2634 /* re-enable link? */
2635 if (ehc->i.action & ATA_EH_ENABLE_LINK) {
2636 ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
2637 spin_lock_irqsave(ap->lock, flags);
2638 link->flags &= ~ATA_LFLAG_DISABLED;
2639 spin_unlock_irqrestore(ap->lock, flags);
2640 ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
2641 }
2642
9b1e2658 2643 ata_link_for_each_dev(dev, link) {
fd995f70
TH
2644 if (link->flags & ATA_LFLAG_NO_RETRY)
2645 ehc->tries[dev->devno] = 1;
2646 else
2647 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
084fe639 2648
9b1e2658
TH
2649 /* collect port action mask recorded in dev actions */
2650 ehc->i.action |= ehc->i.dev_action[dev->devno] &
2651 ~ATA_EH_PERDEV_MASK;
2652 ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
2653
2654 /* process hotplug request */
2655 if (dev->flags & ATA_DFLAG_DETACH)
2656 ata_eh_detach_dev(dev);
2657
02c05a27
TH
2658 /* schedule probe if necessary */
2659 if (!ata_dev_enabled(dev))
2660 ata_eh_schedule_probe(dev);
084fe639 2661 }
022bdb07
TH
2662 }
2663
2664 retry:
022bdb07 2665 rc = 0;
9b1e2658
TH
2666 nr_failed_devs = 0;
2667 nr_disabled_devs = 0;
2668 reset = 0;
022bdb07 2669
aeb2ecd6 2670 /* if UNLOADING, finish immediately */
b51e9e5d 2671 if (ap->pflags & ATA_PFLAG_UNLOADING)
aeb2ecd6
TH
2672 goto out;
2673
9b1e2658
TH
2674 /* prep for EH */
2675 ata_port_for_each_link(link, ap) {
2676 struct ata_eh_context *ehc = &link->eh_context;
022bdb07 2677
9b1e2658
TH
2678 /* skip EH if possible. */
2679 if (ata_eh_skip_recovery(link))
2680 ehc->i.action = 0;
2681
2682 /* do we need to reset? */
2683 if (ehc->i.action & ATA_EH_RESET_MASK)
2684 reset = 1;
2685
2686 ata_link_for_each_dev(dev, link)
2687 ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
2688 }
084fe639 2689
022bdb07 2690 /* reset */
9b1e2658 2691 if (reset) {
b06ce3e5
TH
2692 /* if PMP is attached, this function only deals with
2693 * downstream links, port should stay thawed.
2694 */
2695 if (!ap->nr_pmp_links)
2696 ata_eh_freeze_port(ap);
022bdb07 2697
9b1e2658
TH
2698 ata_port_for_each_link(link, ap) {
2699 struct ata_eh_context *ehc = &link->eh_context;
2700
2701 if (!(ehc->i.action & ATA_EH_RESET_MASK))
2702 continue;
2703
2704 rc = ata_eh_reset(link, ata_link_nr_vacant(link),
2705 prereset, softreset, hardreset,
2706 postreset);
2707 if (rc) {
2708 ata_link_printk(link, KERN_ERR,
2709 "reset failed, giving up\n");
2710 goto out;
2711 }
022bdb07
TH
2712 }
2713
b06ce3e5
TH
2714 if (!ap->nr_pmp_links)
2715 ata_eh_thaw_port(ap);
022bdb07
TH
2716 }
2717
9b1e2658
TH
2718 /* the rest */
2719 ata_port_for_each_link(link, ap) {
2720 struct ata_eh_context *ehc = &link->eh_context;
022bdb07 2721
9b1e2658
TH
2722 /* revalidate existing devices and attach new ones */
2723 rc = ata_eh_revalidate_and_attach(link, &dev);
4ae72a1e 2724 if (rc)
022bdb07 2725 goto dev_fail;
022bdb07 2726
633273a3
TH
2727 /* if PMP got attached, return, pmp EH will take care of it */
2728 if (link->device->class == ATA_DEV_PMP) {
2729 ehc->i.action = 0;
2730 return 0;
2731 }
2732
9b1e2658
TH
2733 /* configure transfer mode if necessary */
2734 if (ehc->i.flags & ATA_EHI_SETMODE) {
2735 rc = ata_set_mode(link, &dev);
2736 if (rc)
2737 goto dev_fail;
2738 ehc->i.flags &= ~ATA_EHI_SETMODE;
2739 }
2740
ca77329f
KCA
2741 if (ehc->i.action & ATA_EHI_LPM)
2742 ata_link_for_each_dev(dev, link)
2743 ata_dev_enable_pm(dev, ap->pm_policy);
2744
9b1e2658
TH
2745 /* this link is okay now */
2746 ehc->i.flags = 0;
2747 continue;
022bdb07 2748
2dcb407e 2749dev_fail:
9b1e2658
TH
2750 nr_failed_devs++;
2751 if (ata_eh_handle_dev_fail(dev, rc))
2752 nr_disabled_devs++;
022bdb07 2753
b06ce3e5
TH
2754 if (ap->pflags & ATA_PFLAG_FROZEN) {
2755 /* PMP reset requires working host port.
2756 * Can't retry if it's frozen.
2757 */
2758 if (ap->nr_pmp_links)
2759 goto out;
9b1e2658 2760 break;
b06ce3e5 2761 }
022bdb07
TH
2762 }
2763
9b1e2658
TH
2764 if (nr_failed_devs) {
2765 if (nr_failed_devs != nr_disabled_devs) {
2766 ata_port_printk(ap, KERN_WARNING, "failed to recover "
2767 "some devices, retrying in 5 secs\n");
2768 ssleep(5);
2769 } else {
2770 /* no device left to recover, repeat fast */
2771 msleep(500);
2772 }
022bdb07 2773
9b1e2658 2774 goto retry;
022bdb07
TH
2775 }
2776
9b1e2658
TH
2777 out:
2778 if (rc && r_failed_link)
2779 *r_failed_link = link;
2780
022bdb07
TH
2781 DPRINTK("EXIT, rc=%d\n", rc);
2782 return rc;
2783}
2784
2785/**
2786 * ata_eh_finish - finish up EH
2787 * @ap: host port to finish EH for
2788 *
2789 * Recovery is complete. Clean up EH states and retry or finish
2790 * failed qcs.
2791 *
2792 * LOCKING:
2793 * None.
2794 */
fb7fd614 2795void ata_eh_finish(struct ata_port *ap)
022bdb07
TH
2796{
2797 int tag;
2798
2799 /* retry or finish qcs */
2800 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2801 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2802
2803 if (!(qc->flags & ATA_QCFLAG_FAILED))
2804 continue;
2805
2806 if (qc->err_mask) {
2807 /* FIXME: Once EH migration is complete,
2808 * generate sense data in this function,
2809 * considering both err_mask and tf.
f90f0828
TH
2810 *
2811 * There's no point in retrying invalid
2812 * (detected by libata) and non-IO device
2813 * errors (rejected by device). Finish them
2814 * immediately.
022bdb07 2815 */
f90f0828
TH
2816 if ((qc->err_mask & AC_ERR_INVALID) ||
2817 (!(qc->flags & ATA_QCFLAG_IO) &&
2818 qc->err_mask == AC_ERR_DEV))
022bdb07
TH
2819 ata_eh_qc_complete(qc);
2820 else
2821 ata_eh_qc_retry(qc);
2822 } else {
2823 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2824 ata_eh_qc_complete(qc);
2825 } else {
2826 /* feed zero TF to sense generation */
2827 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2828 ata_eh_qc_retry(qc);
2829 }
2830 }
2831 }
da917d69
TH
2832
2833 /* make sure nr_active_links is zero after EH */
2834 WARN_ON(ap->nr_active_links);
2835 ap->nr_active_links = 0;
022bdb07
TH
2836}
2837
2838/**
2839 * ata_do_eh - do standard error handling
2840 * @ap: host port to handle error for
f5914a46 2841 * @prereset: prereset method (can be NULL)
022bdb07
TH
2842 * @softreset: softreset method (can be NULL)
2843 * @hardreset: hardreset method (can be NULL)
2844 * @postreset: postreset method (can be NULL)
2845 *
2846 * Perform standard error handling sequence.
2847 *
2848 * LOCKING:
2849 * Kernel thread context (may sleep).
2850 */
f5914a46
TH
2851void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2852 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2853 ata_postreset_fn_t postreset)
022bdb07 2854{
9b1e2658
TH
2855 struct ata_device *dev;
2856 int rc;
2857
2858 ata_eh_autopsy(ap);
2859 ata_eh_report(ap);
2860
2861 rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
2862 NULL);
2863 if (rc) {
2864 ata_link_for_each_dev(dev, &ap->link)
2865 ata_dev_disable(dev);
2866 }
2867
022bdb07
TH
2868 ata_eh_finish(ap);
2869}
500530f6 2870
6ffa01d8 2871#ifdef CONFIG_PM
500530f6
TH
2872/**
2873 * ata_eh_handle_port_suspend - perform port suspend operation
2874 * @ap: port to suspend
2875 *
2876 * Suspend @ap.
2877 *
2878 * LOCKING:
2879 * Kernel thread context (may sleep).
2880 */
2881static void ata_eh_handle_port_suspend(struct ata_port *ap)
2882{
2883 unsigned long flags;
2884 int rc = 0;
2885
2886 /* are we suspending? */
2887 spin_lock_irqsave(ap->lock, flags);
2888 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2889 ap->pm_mesg.event == PM_EVENT_ON) {
2890 spin_unlock_irqrestore(ap->lock, flags);
2891 return;
2892 }
2893 spin_unlock_irqrestore(ap->lock, flags);
2894
2895 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
2896
64578a3d
TH
2897 /* tell ACPI we're suspending */
2898 rc = ata_acpi_on_suspend(ap);
2899 if (rc)
2900 goto out;
2901
500530f6
TH
2902 /* suspend */
2903 ata_eh_freeze_port(ap);
2904
2905 if (ap->ops->port_suspend)
2906 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
2907
bd3adca5 2908 ata_acpi_set_state(ap, PMSG_SUSPEND);
64578a3d 2909 out:
500530f6
TH
2910 /* report result */
2911 spin_lock_irqsave(ap->lock, flags);
2912
2913 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
2914 if (rc == 0)
2915 ap->pflags |= ATA_PFLAG_SUSPENDED;
64578a3d 2916 else if (ap->pflags & ATA_PFLAG_FROZEN)
500530f6
TH
2917 ata_port_schedule_eh(ap);
2918
2919 if (ap->pm_result) {
2920 *ap->pm_result = rc;
2921 ap->pm_result = NULL;
2922 }
2923
2924 spin_unlock_irqrestore(ap->lock, flags);
2925
2926 return;
2927}
2928
2929/**
2930 * ata_eh_handle_port_resume - perform port resume operation
2931 * @ap: port to resume
2932 *
2933 * Resume @ap.
2934 *
500530f6
TH
2935 * LOCKING:
2936 * Kernel thread context (may sleep).
2937 */
2938static void ata_eh_handle_port_resume(struct ata_port *ap)
2939{
500530f6 2940 unsigned long flags;
9666f400 2941 int rc = 0;
500530f6
TH
2942
2943 /* are we resuming? */
2944 spin_lock_irqsave(ap->lock, flags);
2945 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2946 ap->pm_mesg.event != PM_EVENT_ON) {
2947 spin_unlock_irqrestore(ap->lock, flags);
2948 return;
2949 }
2950 spin_unlock_irqrestore(ap->lock, flags);
2951
9666f400 2952 WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
500530f6 2953
bd3adca5
SL
2954 ata_acpi_set_state(ap, PMSG_ON);
2955
500530f6
TH
2956 if (ap->ops->port_resume)
2957 rc = ap->ops->port_resume(ap);
2958
6746544c
TH
2959 /* tell ACPI that we're resuming */
2960 ata_acpi_on_resume(ap);
2961
9666f400 2962 /* report result */
500530f6
TH
2963 spin_lock_irqsave(ap->lock, flags);
2964 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
2965 if (ap->pm_result) {
2966 *ap->pm_result = rc;
2967 ap->pm_result = NULL;
2968 }
2969 spin_unlock_irqrestore(ap->lock, flags);
2970}
6ffa01d8 2971#endif /* CONFIG_PM */