]> git.ipfire.org Git - people/ms/linux.git/blame - drivers/ata/libata-eh.c
ata: libata-eh: Add missing command name
[people/ms/linux.git] / drivers / ata / libata-eh.c
CommitLineData
c82ee6d3 1// SPDX-License-Identifier: GPL-2.0-or-later
ece1d636
TH
2/*
3 * libata-eh.c - libata error handling
4 *
ece1d636
TH
5 * Copyright 2006 Tejun Heo <htejun@gmail.com>
6 *
ece1d636 7 * libata documentation is available via 'make {ps|pdf}docs',
9bb9a39c 8 * as Documentation/driver-api/libata.rst
ece1d636
TH
9 *
10 * Hardware documentation available from http://www.t13.org/ and
11 * http://www.sata-io.org/
ece1d636
TH
12 */
13
ece1d636 14#include <linux/kernel.h>
242f9dcb 15#include <linux/blkdev.h>
38789fda 16#include <linux/export.h>
2855568b 17#include <linux/pci.h>
ece1d636
TH
18#include <scsi/scsi.h>
19#include <scsi/scsi_host.h>
20#include <scsi/scsi_eh.h>
21#include <scsi/scsi_device.h>
22#include <scsi/scsi_cmnd.h>
6521148c 23#include <scsi/scsi_dbg.h>
c6fd2807 24#include "../scsi/scsi_transport_api.h"
ece1d636
TH
25
26#include <linux/libata.h>
27
255c03d1 28#include <trace/events/libata.h>
ece1d636
TH
29#include "libata.h"
30
7d47e8d4 31enum {
3884f7b0 32 /* speed down verdicts */
7d47e8d4
TH
33 ATA_EH_SPDN_NCQ_OFF = (1 << 0),
34 ATA_EH_SPDN_SPEED_DOWN = (1 << 1),
35 ATA_EH_SPDN_FALLBACK_TO_PIO = (1 << 2),
76326ac1 36 ATA_EH_SPDN_KEEP_ERRORS = (1 << 3),
3884f7b0
TH
37
38 /* error flags */
39 ATA_EFLAG_IS_IO = (1 << 0),
76326ac1 40 ATA_EFLAG_DUBIOUS_XFER = (1 << 1),
d9027470 41 ATA_EFLAG_OLD_ER = (1 << 31),
3884f7b0
TH
42
43 /* error categories */
44 ATA_ECAT_NONE = 0,
45 ATA_ECAT_ATA_BUS = 1,
46 ATA_ECAT_TOUT_HSM = 2,
47 ATA_ECAT_UNK_DEV = 3,
75f9cafc
TH
48 ATA_ECAT_DUBIOUS_NONE = 4,
49 ATA_ECAT_DUBIOUS_ATA_BUS = 5,
50 ATA_ECAT_DUBIOUS_TOUT_HSM = 6,
51 ATA_ECAT_DUBIOUS_UNK_DEV = 7,
52 ATA_ECAT_NR = 8,
7d47e8d4 53
87fbc5a0
TH
54 ATA_EH_CMD_DFL_TIMEOUT = 5000,
55
0a2c0f56
TH
56 /* always put at least this amount of time between resets */
57 ATA_EH_RESET_COOL_DOWN = 5000,
58
341c2c95
TH
59 /* Waiting in ->prereset can never be reliable. It's
60 * sometimes nice to wait there but it can't be depended upon;
61 * otherwise, we wouldn't be resetting. Just give it enough
62 * time for most drives to spin up.
63 */
64 ATA_EH_PRERESET_TIMEOUT = 10000,
65 ATA_EH_FASTDRAIN_INTERVAL = 3000,
11fc33da
TH
66
67 ATA_EH_UA_TRIES = 5,
c2c7a89c
TH
68
69 /* probe speed down parameters, see ata_eh_schedule_probe() */
70 ATA_EH_PROBE_TRIAL_INTERVAL = 60000, /* 1 min */
71 ATA_EH_PROBE_TRIALS = 2,
31daabda
TH
72};
73
74/* The following table determines how we sequence resets. Each entry
75 * represents timeout for that try. The first try can be soft or
76 * hardreset. All others are hardreset if available. In most cases
77 * the first reset w/ 10sec timeout should succeed. Following entries
35bf8821
DW
78 * are mostly for error handling, hotplug and those outlier devices that
79 * take an exceptionally long time to recover from reset.
31daabda
TH
80 */
81static const unsigned long ata_eh_reset_timeouts[] = {
341c2c95
TH
82 10000, /* most drives spin up by 10sec */
83 10000, /* > 99% working drives spin up before 20sec */
35bf8821 84 35000, /* give > 30 secs of idleness for outlier devices */
341c2c95 85 5000, /* and sweet one last chance */
d8af0eb6 86 ULONG_MAX, /* > 1 min has elapsed, give up */
31daabda
TH
87};
88
e06233f9 89static const unsigned int ata_eh_identify_timeouts[] = {
87fbc5a0
TH
90 5000, /* covers > 99% of successes and not too boring on failures */
91 10000, /* combined time till here is enough even for media access */
92 30000, /* for true idiots */
e06233f9 93 UINT_MAX,
87fbc5a0
TH
94};
95
e06233f9 96static const unsigned int ata_eh_revalidate_timeouts[] = {
68dbbe7d
DLM
97 15000, /* Some drives are slow to read log pages when waking-up */
98 15000, /* combined time till here is enough even for media access */
e06233f9 99 UINT_MAX,
68dbbe7d
DLM
100};
101
e06233f9 102static const unsigned int ata_eh_flush_timeouts[] = {
6013efd8
TH
103 15000, /* be generous with flush */
104 15000, /* ditto */
105 30000, /* and even more generous */
e06233f9 106 UINT_MAX,
6013efd8
TH
107};
108
e06233f9 109static const unsigned int ata_eh_other_timeouts[] = {
87fbc5a0
TH
110 5000, /* same rationale as identify timeout */
111 10000, /* ditto */
112 /* but no merciful 30sec for other commands, it just isn't worth it */
e06233f9 113 UINT_MAX,
87fbc5a0
TH
114};
115
116struct ata_eh_cmd_timeout_ent {
117 const u8 *commands;
e06233f9 118 const unsigned int *timeouts;
87fbc5a0
TH
119};
120
121/* The following table determines timeouts to use for EH internal
122 * commands. Each table entry is a command class and matches the
123 * commands the entry applies to and the timeout table to use.
124 *
125 * On the retry after a command timed out, the next timeout value from
126 * the table is used. If the table doesn't contain further entries,
127 * the last value is used.
128 *
129 * ehc->cmd_timeout_idx keeps track of which timeout to use per
130 * command class, so if SET_FEATURES times out on the first try, the
131 * next try will use the second timeout value only for that class.
132 */
133#define CMDS(cmds...) (const u8 []){ cmds, 0 }
134static const struct ata_eh_cmd_timeout_ent
135ata_eh_cmd_timeout_table[ATA_EH_CMD_TIMEOUT_TABLE_SIZE] = {
136 { .commands = CMDS(ATA_CMD_ID_ATA, ATA_CMD_ID_ATAPI),
137 .timeouts = ata_eh_identify_timeouts, },
68dbbe7d
DLM
138 { .commands = CMDS(ATA_CMD_READ_LOG_EXT, ATA_CMD_READ_LOG_DMA_EXT),
139 .timeouts = ata_eh_revalidate_timeouts, },
87fbc5a0
TH
140 { .commands = CMDS(ATA_CMD_READ_NATIVE_MAX, ATA_CMD_READ_NATIVE_MAX_EXT),
141 .timeouts = ata_eh_other_timeouts, },
142 { .commands = CMDS(ATA_CMD_SET_MAX, ATA_CMD_SET_MAX_EXT),
143 .timeouts = ata_eh_other_timeouts, },
144 { .commands = CMDS(ATA_CMD_SET_FEATURES),
145 .timeouts = ata_eh_other_timeouts, },
146 { .commands = CMDS(ATA_CMD_INIT_DEV_PARAMS),
147 .timeouts = ata_eh_other_timeouts, },
6013efd8
TH
148 { .commands = CMDS(ATA_CMD_FLUSH, ATA_CMD_FLUSH_EXT),
149 .timeouts = ata_eh_flush_timeouts },
87fbc5a0
TH
150};
151#undef CMDS
152
ad9e2762 153static void __ata_port_freeze(struct ata_port *ap);
6ffa01d8 154#ifdef CONFIG_PM
500530f6
TH
155static void ata_eh_handle_port_suspend(struct ata_port *ap);
156static void ata_eh_handle_port_resume(struct ata_port *ap);
6ffa01d8
TH
157#else /* CONFIG_PM */
158static void ata_eh_handle_port_suspend(struct ata_port *ap)
159{ }
160
161static void ata_eh_handle_port_resume(struct ata_port *ap)
162{ }
6ffa01d8 163#endif /* CONFIG_PM */
ad9e2762 164
0d74d872
MM
165static __printf(2, 0) void __ata_ehi_pushv_desc(struct ata_eh_info *ehi,
166 const char *fmt, va_list args)
b64bbc39
TH
167{
168 ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
169 ATA_EH_DESC_LEN - ehi->desc_len,
170 fmt, args);
171}
172
173/**
174 * __ata_ehi_push_desc - push error description without adding separator
175 * @ehi: target EHI
176 * @fmt: printf format string
177 *
178 * Format string according to @fmt and append it to @ehi->desc.
179 *
180 * LOCKING:
181 * spin_lock_irqsave(host lock)
182 */
183void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
184{
185 va_list args;
186
187 va_start(args, fmt);
188 __ata_ehi_pushv_desc(ehi, fmt, args);
189 va_end(args);
190}
a52fbcfc 191EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
b64bbc39
TH
192
193/**
194 * ata_ehi_push_desc - push error description with separator
195 * @ehi: target EHI
196 * @fmt: printf format string
197 *
198 * Format string according to @fmt and append it to @ehi->desc.
199 * If @ehi->desc is not empty, ", " is added in-between.
200 *
201 * LOCKING:
202 * spin_lock_irqsave(host lock)
203 */
204void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
205{
206 va_list args;
207
208 if (ehi->desc_len)
209 __ata_ehi_push_desc(ehi, ", ");
210
211 va_start(args, fmt);
212 __ata_ehi_pushv_desc(ehi, fmt, args);
213 va_end(args);
214}
a52fbcfc 215EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
b64bbc39
TH
216
217/**
218 * ata_ehi_clear_desc - clean error description
219 * @ehi: target EHI
220 *
221 * Clear @ehi->desc.
222 *
223 * LOCKING:
224 * spin_lock_irqsave(host lock)
225 */
226void ata_ehi_clear_desc(struct ata_eh_info *ehi)
227{
228 ehi->desc[0] = '\0';
229 ehi->desc_len = 0;
230}
a52fbcfc 231EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
b64bbc39 232
cbcdd875
TH
233/**
234 * ata_port_desc - append port description
235 * @ap: target ATA port
236 * @fmt: printf format string
237 *
238 * Format string according to @fmt and append it to port
239 * description. If port description is not empty, " " is added
240 * in-between. This function is to be used while initializing
241 * ata_host. The description is printed on host registration.
242 *
243 * LOCKING:
244 * None.
245 */
246void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
247{
248 va_list args;
249
250 WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
251
252 if (ap->link.eh_info.desc_len)
253 __ata_ehi_push_desc(&ap->link.eh_info, " ");
254
255 va_start(args, fmt);
256 __ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
257 va_end(args);
258}
a52fbcfc 259EXPORT_SYMBOL_GPL(ata_port_desc);
cbcdd875
TH
260
261#ifdef CONFIG_PCI
cbcdd875
TH
262/**
263 * ata_port_pbar_desc - append PCI BAR description
264 * @ap: target ATA port
265 * @bar: target PCI BAR
266 * @offset: offset into PCI BAR
267 * @name: name of the area
268 *
269 * If @offset is negative, this function formats a string which
270 * contains the name, address, size and type of the BAR and
271 * appends it to the port description. If @offset is zero or
272 * positive, only name and offsetted address is appended.
273 *
274 * LOCKING:
275 * None.
276 */
277void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
278 const char *name)
279{
280 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
281 char *type = "";
282 unsigned long long start, len;
283
284 if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
285 type = "m";
286 else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
287 type = "i";
288
289 start = (unsigned long long)pci_resource_start(pdev, bar);
290 len = (unsigned long long)pci_resource_len(pdev, bar);
291
292 if (offset < 0)
293 ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
294 else
e6a73ab1
AM
295 ata_port_desc(ap, "%s 0x%llx", name,
296 start + (unsigned long long)offset);
cbcdd875 297}
a52fbcfc 298EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
cbcdd875
TH
299#endif /* CONFIG_PCI */
300
87fbc5a0
TH
301static int ata_lookup_timeout_table(u8 cmd)
302{
303 int i;
304
305 for (i = 0; i < ATA_EH_CMD_TIMEOUT_TABLE_SIZE; i++) {
306 const u8 *cur;
307
308 for (cur = ata_eh_cmd_timeout_table[i].commands; *cur; cur++)
309 if (*cur == cmd)
310 return i;
311 }
312
313 return -1;
314}
315
316/**
317 * ata_internal_cmd_timeout - determine timeout for an internal command
318 * @dev: target device
319 * @cmd: internal command to be issued
320 *
321 * Determine timeout for internal command @cmd for @dev.
322 *
323 * LOCKING:
324 * EH context.
325 *
326 * RETURNS:
327 * Determined timeout.
328 */
e06233f9 329unsigned int ata_internal_cmd_timeout(struct ata_device *dev, u8 cmd)
87fbc5a0
TH
330{
331 struct ata_eh_context *ehc = &dev->link->eh_context;
332 int ent = ata_lookup_timeout_table(cmd);
333 int idx;
334
335 if (ent < 0)
336 return ATA_EH_CMD_DFL_TIMEOUT;
337
338 idx = ehc->cmd_timeout_idx[dev->devno][ent];
339 return ata_eh_cmd_timeout_table[ent].timeouts[idx];
340}
341
342/**
343 * ata_internal_cmd_timed_out - notification for internal command timeout
344 * @dev: target device
345 * @cmd: internal command which timed out
346 *
347 * Notify EH that internal command @cmd for @dev timed out. This
348 * function should be called only for commands whose timeouts are
349 * determined using ata_internal_cmd_timeout().
350 *
351 * LOCKING:
352 * EH context.
353 */
354void ata_internal_cmd_timed_out(struct ata_device *dev, u8 cmd)
355{
356 struct ata_eh_context *ehc = &dev->link->eh_context;
357 int ent = ata_lookup_timeout_table(cmd);
358 int idx;
359
360 if (ent < 0)
361 return;
362
363 idx = ehc->cmd_timeout_idx[dev->devno][ent];
e06233f9 364 if (ata_eh_cmd_timeout_table[ent].timeouts[idx + 1] != UINT_MAX)
87fbc5a0
TH
365 ehc->cmd_timeout_idx[dev->devno][ent]++;
366}
367
3884f7b0 368static void ata_ering_record(struct ata_ering *ering, unsigned int eflags,
0c247c55
TH
369 unsigned int err_mask)
370{
371 struct ata_ering_entry *ent;
372
373 WARN_ON(!err_mask);
374
375 ering->cursor++;
376 ering->cursor %= ATA_ERING_SIZE;
377
378 ent = &ering->ring[ering->cursor];
3884f7b0 379 ent->eflags = eflags;
0c247c55
TH
380 ent->err_mask = err_mask;
381 ent->timestamp = get_jiffies_64();
382}
383
76326ac1
TH
384static struct ata_ering_entry *ata_ering_top(struct ata_ering *ering)
385{
386 struct ata_ering_entry *ent = &ering->ring[ering->cursor];
387
388 if (ent->err_mask)
389 return ent;
390 return NULL;
391}
392
d9027470
GG
393int ata_ering_map(struct ata_ering *ering,
394 int (*map_fn)(struct ata_ering_entry *, void *),
395 void *arg)
0c247c55
TH
396{
397 int idx, rc = 0;
398 struct ata_ering_entry *ent;
399
400 idx = ering->cursor;
401 do {
402 ent = &ering->ring[idx];
403 if (!ent->err_mask)
404 break;
405 rc = map_fn(ent, arg);
406 if (rc)
407 break;
408 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
409 } while (idx != ering->cursor);
410
411 return rc;
412}
413
60428407 414static int ata_ering_clear_cb(struct ata_ering_entry *ent, void *void_arg)
d9027470
GG
415{
416 ent->eflags |= ATA_EFLAG_OLD_ER;
417 return 0;
418}
419
420static void ata_ering_clear(struct ata_ering *ering)
421{
422 ata_ering_map(ering, ata_ering_clear_cb, NULL);
423}
424
64f65ca6
TH
425static unsigned int ata_eh_dev_action(struct ata_device *dev)
426{
9af5c9c9 427 struct ata_eh_context *ehc = &dev->link->eh_context;
64f65ca6
TH
428
429 return ehc->i.action | ehc->i.dev_action[dev->devno];
430}
431
f58229f8 432static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
af181c2d
TH
433 struct ata_eh_info *ehi, unsigned int action)
434{
f58229f8 435 struct ata_device *tdev;
af181c2d
TH
436
437 if (!dev) {
438 ehi->action &= ~action;
1eca4365 439 ata_for_each_dev(tdev, link, ALL)
f58229f8 440 ehi->dev_action[tdev->devno] &= ~action;
af181c2d
TH
441 } else {
442 /* doesn't make sense for port-wide EH actions */
443 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
444
445 /* break ehi->action into ehi->dev_action */
446 if (ehi->action & action) {
1eca4365 447 ata_for_each_dev(tdev, link, ALL)
f58229f8
TH
448 ehi->dev_action[tdev->devno] |=
449 ehi->action & action;
af181c2d
TH
450 ehi->action &= ~action;
451 }
452
453 /* turn off the specified per-dev action */
454 ehi->dev_action[dev->devno] &= ~action;
455 }
456}
457
c0c362b6
TH
458/**
459 * ata_eh_acquire - acquire EH ownership
460 * @ap: ATA port to acquire EH ownership for
461 *
462 * Acquire EH ownership for @ap. This is the basic exclusion
463 * mechanism for ports sharing a host. Only one port hanging off
464 * the same host can claim the ownership of EH.
465 *
466 * LOCKING:
467 * EH context.
468 */
469void ata_eh_acquire(struct ata_port *ap)
470{
471 mutex_lock(&ap->host->eh_mutex);
472 WARN_ON_ONCE(ap->host->eh_owner);
473 ap->host->eh_owner = current;
474}
475
476/**
477 * ata_eh_release - release EH ownership
478 * @ap: ATA port to release EH ownership for
479 *
480 * Release EH ownership for @ap if the caller. The caller must
481 * have acquired EH ownership using ata_eh_acquire() previously.
482 *
483 * LOCKING:
484 * EH context.
485 */
486void ata_eh_release(struct ata_port *ap)
487{
488 WARN_ON_ONCE(ap->host->eh_owner != current);
489 ap->host->eh_owner = NULL;
490 mutex_unlock(&ap->host->eh_mutex);
491}
492
ece180d1
TH
493static void ata_eh_unload(struct ata_port *ap)
494{
495 struct ata_link *link;
496 struct ata_device *dev;
497 unsigned long flags;
498
499 /* Restore SControl IPM and SPD for the next driver and
500 * disable attached devices.
501 */
502 ata_for_each_link(link, ap, PMP_FIRST) {
503 sata_scr_write(link, SCR_CONTROL, link->saved_scontrol & 0xff0);
504 ata_for_each_dev(dev, link, ALL)
505 ata_dev_disable(dev);
506 }
507
508 /* freeze and set UNLOADED */
509 spin_lock_irqsave(ap->lock, flags);
510
511 ata_port_freeze(ap); /* won't be thawed */
512 ap->pflags &= ~ATA_PFLAG_EH_PENDING; /* clear pending from freeze */
513 ap->pflags |= ATA_PFLAG_UNLOADED;
514
515 spin_unlock_irqrestore(ap->lock, flags);
516}
517
ece1d636
TH
518/**
519 * ata_scsi_error - SCSI layer error handler callback
520 * @host: SCSI host on which error occurred
521 *
522 * Handles SCSI-layer-thrown error events.
523 *
524 * LOCKING:
525 * Inherited from SCSI layer (none, can sleep)
526 *
527 * RETURNS:
528 * Zero.
529 */
381544bb 530void ata_scsi_error(struct Scsi_Host *host)
ece1d636 531{
35bb94b1 532 struct ata_port *ap = ata_shost_to_port(host);
ad9e2762 533 unsigned long flags;
c34aeebc 534 LIST_HEAD(eh_work_q);
ece1d636 535
c34aeebc
JB
536 spin_lock_irqsave(host->host_lock, flags);
537 list_splice_init(&host->eh_cmd_q, &eh_work_q);
538 spin_unlock_irqrestore(host->host_lock, flags);
539
0e0b494c
JB
540 ata_scsi_cmd_error_handler(host, ap, &eh_work_q);
541
542 /* If we timed raced normal completion and there is nothing to
543 recover nr_timedout == 0 why exactly are we doing error recovery ? */
544 ata_scsi_port_error_handler(host, ap);
545
546 /* finish or retry handled scmd's and clean up */
72d8c36e 547 WARN_ON(!list_empty(&eh_work_q));
0e0b494c 548
0e0b494c
JB
549}
550
551/**
552 * ata_scsi_cmd_error_handler - error callback for a list of commands
553 * @host: scsi host containing the port
554 * @ap: ATA port within the host
555 * @eh_work_q: list of commands to process
556 *
557 * process the given list of commands and return those finished to the
558 * ap->eh_done_q. This function is the first part of the libata error
559 * handler which processes a given list of failed commands.
560 */
561void ata_scsi_cmd_error_handler(struct Scsi_Host *host, struct ata_port *ap,
562 struct list_head *eh_work_q)
563{
564 int i;
565 unsigned long flags;
566
c429137a
TH
567 /* make sure sff pio task is not running */
568 ata_sff_flush_pio_task(ap);
ece1d636 569
cca3974e 570 /* synchronize with host lock and sort out timeouts */
ad9e2762
TH
571
572 /* For new EH, all qcs are finished in one of three ways -
573 * normal completion, error completion, and SCSI timeout.
c96f1732 574 * Both completions can race against SCSI timeout. When normal
ad9e2762
TH
575 * completion wins, the qc never reaches EH. When error
576 * completion wins, the qc has ATA_QCFLAG_FAILED set.
577 *
578 * When SCSI timeout wins, things are a bit more complex.
579 * Normal or error completion can occur after the timeout but
580 * before this point. In such cases, both types of
581 * completions are honored. A scmd is determined to have
582 * timed out iff its associated qc is active and not failed.
583 */
a4f08141 584 spin_lock_irqsave(ap->lock, flags);
ad9e2762
TH
585 if (ap->ops->error_handler) {
586 struct scsi_cmnd *scmd, *tmp;
587 int nr_timedout = 0;
588
c96f1732
AC
589 /* This must occur under the ap->lock as we don't want
590 a polled recovery to race the real interrupt handler
d9027470 591
c96f1732
AC
592 The lost_interrupt handler checks for any completed but
593 non-notified command and completes much like an IRQ handler.
d9027470 594
c96f1732
AC
595 We then fall into the error recovery code which will treat
596 this as if normal completion won the race */
597
598 if (ap->ops->lost_interrupt)
599 ap->ops->lost_interrupt(ap);
d9027470 600
0e0b494c 601 list_for_each_entry_safe(scmd, tmp, eh_work_q, eh_entry) {
ad9e2762
TH
602 struct ata_queued_cmd *qc;
603
258c4e5c 604 ata_qc_for_each_raw(ap, qc, i) {
ad9e2762
TH
605 if (qc->flags & ATA_QCFLAG_ACTIVE &&
606 qc->scsicmd == scmd)
607 break;
608 }
609
610 if (i < ATA_MAX_QUEUE) {
611 /* the scmd has an associated qc */
612 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
613 /* which hasn't failed yet, timeout */
614 qc->err_mask |= AC_ERR_TIMEOUT;
615 qc->flags |= ATA_QCFLAG_FAILED;
616 nr_timedout++;
617 }
618 } else {
619 /* Normal completion occurred after
620 * SCSI timeout but before this point.
621 * Successfully complete it.
622 */
623 scmd->retries = scmd->allowed;
624 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
625 }
626 }
627
628 /* If we have timed out qcs. They belong to EH from
629 * this point but the state of the controller is
630 * unknown. Freeze the port to make sure the IRQ
631 * handler doesn't diddle with those qcs. This must
632 * be done atomically w.r.t. setting QCFLAG_FAILED.
633 */
634 if (nr_timedout)
635 __ata_port_freeze(ap);
636
a1e10f7e
TH
637
638 /* initialize eh_tries */
639 ap->eh_tries = ATA_EH_MAX_TRIES;
a4f08141
PM
640 }
641 spin_unlock_irqrestore(ap->lock, flags);
d9027470 642
0e0b494c
JB
643}
644EXPORT_SYMBOL(ata_scsi_cmd_error_handler);
645
646/**
647 * ata_scsi_port_error_handler - recover the port after the commands
648 * @host: SCSI host containing the port
649 * @ap: the ATA port
650 *
651 * Handle the recovery of the port @ap after all the commands
652 * have been recovered.
653 */
654void ata_scsi_port_error_handler(struct Scsi_Host *host, struct ata_port *ap)
655{
656 unsigned long flags;
ad9e2762 657
ad9e2762
TH
658 /* invoke error handler */
659 if (ap->ops->error_handler) {
cf1b86c8
TH
660 struct ata_link *link;
661
c0c362b6
TH
662 /* acquire EH ownership */
663 ata_eh_acquire(ap);
664 repeat:
5ddf24c5
TH
665 /* kill fast drain timer */
666 del_timer_sync(&ap->fastdrain_timer);
667
500530f6
TH
668 /* process port resume request */
669 ata_eh_handle_port_resume(ap);
670
f3e81b19 671 /* fetch & clear EH info */
e30349d2 672 spin_lock_irqsave(ap->lock, flags);
f3e81b19 673
1eca4365 674 ata_for_each_link(link, ap, HOST_FIRST) {
00115e0f
TH
675 struct ata_eh_context *ehc = &link->eh_context;
676 struct ata_device *dev;
677
cf1b86c8
TH
678 memset(&link->eh_context, 0, sizeof(link->eh_context));
679 link->eh_context.i = link->eh_info;
680 memset(&link->eh_info, 0, sizeof(link->eh_info));
00115e0f 681
1eca4365 682 ata_for_each_dev(dev, link, ENABLED) {
00115e0f
TH
683 int devno = dev->devno;
684
685 ehc->saved_xfer_mode[devno] = dev->xfer_mode;
686 if (ata_ncq_enabled(dev))
687 ehc->saved_ncq_enabled |= 1 << devno;
688 }
cf1b86c8 689 }
f3e81b19 690
b51e9e5d
TH
691 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
692 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
da917d69 693 ap->excl_link = NULL; /* don't maintain exclusion over EH */
f3e81b19 694
e30349d2 695 spin_unlock_irqrestore(ap->lock, flags);
ad9e2762 696
500530f6
TH
697 /* invoke EH, skip if unloading or suspended */
698 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
720ba126 699 ap->ops->error_handler(ap);
ece180d1
TH
700 else {
701 /* if unloading, commence suicide */
702 if ((ap->pflags & ATA_PFLAG_UNLOADING) &&
703 !(ap->pflags & ATA_PFLAG_UNLOADED))
704 ata_eh_unload(ap);
720ba126 705 ata_eh_finish(ap);
ece180d1 706 }
ad9e2762 707
500530f6
TH
708 /* process port suspend request */
709 ata_eh_handle_port_suspend(ap);
710
25985edc 711 /* Exception might have happened after ->error_handler
ad9e2762
TH
712 * recovered the port but before this point. Repeat
713 * EH in such case.
714 */
e30349d2 715 spin_lock_irqsave(ap->lock, flags);
ad9e2762 716
b51e9e5d 717 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
a1e10f7e 718 if (--ap->eh_tries) {
e30349d2 719 spin_unlock_irqrestore(ap->lock, flags);
ad9e2762
TH
720 goto repeat;
721 }
a9a79dfe
JP
722 ata_port_err(ap,
723 "EH pending after %d tries, giving up\n",
724 ATA_EH_MAX_TRIES);
914616a3 725 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
ad9e2762
TH
726 }
727
f3e81b19 728 /* this run is complete, make sure EH info is clear */
1eca4365 729 ata_for_each_link(link, ap, HOST_FIRST)
cf1b86c8 730 memset(&link->eh_info, 0, sizeof(link->eh_info));
f3e81b19 731
e4a9c373
DW
732 /* end eh (clear host_eh_scheduled) while holding
733 * ap->lock such that if exception occurs after this
734 * point but before EH completion, SCSI midlayer will
ad9e2762
TH
735 * re-initiate EH.
736 */
e4a9c373 737 ap->ops->end_eh(ap);
ad9e2762 738
e30349d2 739 spin_unlock_irqrestore(ap->lock, flags);
c0c362b6 740 ata_eh_release(ap);
ad9e2762 741 } else {
9af5c9c9 742 WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
ad9e2762
TH
743 ap->ops->eng_timeout(ap);
744 }
ece1d636 745
ece1d636
TH
746 scsi_eh_flush_done_q(&ap->eh_done_q);
747
ad9e2762 748 /* clean up */
e30349d2 749 spin_lock_irqsave(ap->lock, flags);
ad9e2762 750
1cdaf534 751 if (ap->pflags & ATA_PFLAG_LOADING)
b51e9e5d 752 ap->pflags &= ~ATA_PFLAG_LOADING;
6f54120e
JY
753 else if ((ap->pflags & ATA_PFLAG_SCSI_HOTPLUG) &&
754 !(ap->flags & ATA_FLAG_SAS_HOST))
ad72cf98 755 schedule_delayed_work(&ap->hotplug_task, 0);
1cdaf534
TH
756
757 if (ap->pflags & ATA_PFLAG_RECOVERED)
a9a79dfe 758 ata_port_info(ap, "EH complete\n");
580b2102 759
b51e9e5d 760 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
ad9e2762 761
c6cf9e99 762 /* tell wait_eh that we're done */
b51e9e5d 763 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
c6cf9e99
TH
764 wake_up_all(&ap->eh_wait_q);
765
e30349d2 766 spin_unlock_irqrestore(ap->lock, flags);
ece1d636 767}
0e0b494c 768EXPORT_SYMBOL_GPL(ata_scsi_port_error_handler);
ece1d636 769
c6cf9e99
TH
770/**
771 * ata_port_wait_eh - Wait for the currently pending EH to complete
772 * @ap: Port to wait EH for
773 *
774 * Wait until the currently pending EH is complete.
775 *
776 * LOCKING:
777 * Kernel thread context (may sleep).
778 */
779void ata_port_wait_eh(struct ata_port *ap)
780{
781 unsigned long flags;
782 DEFINE_WAIT(wait);
783
784 retry:
ba6a1308 785 spin_lock_irqsave(ap->lock, flags);
c6cf9e99 786
b51e9e5d 787 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
c6cf9e99 788 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
ba6a1308 789 spin_unlock_irqrestore(ap->lock, flags);
c6cf9e99 790 schedule();
ba6a1308 791 spin_lock_irqsave(ap->lock, flags);
c6cf9e99 792 }
0a1b622e 793 finish_wait(&ap->eh_wait_q, &wait);
c6cf9e99 794
ba6a1308 795 spin_unlock_irqrestore(ap->lock, flags);
c6cf9e99
TH
796
797 /* make sure SCSI EH is complete */
cca3974e 798 if (scsi_host_in_recovery(ap->scsi_host)) {
97750ceb 799 ata_msleep(ap, 10);
c6cf9e99
TH
800 goto retry;
801 }
802}
81c757bc 803EXPORT_SYMBOL_GPL(ata_port_wait_eh);
c6cf9e99 804
afae461a 805static unsigned int ata_eh_nr_in_flight(struct ata_port *ap)
5ddf24c5 806{
258c4e5c 807 struct ata_queued_cmd *qc;
5ddf24c5 808 unsigned int tag;
afae461a 809 unsigned int nr = 0;
5ddf24c5
TH
810
811 /* count only non-internal commands */
258c4e5c
JA
812 ata_qc_for_each(ap, qc, tag) {
813 if (qc)
5ddf24c5 814 nr++;
9d207acc 815 }
5ddf24c5
TH
816
817 return nr;
818}
819
b93ab338 820void ata_eh_fastdrain_timerfn(struct timer_list *t)
5ddf24c5 821{
b93ab338 822 struct ata_port *ap = from_timer(ap, t, fastdrain_timer);
5ddf24c5 823 unsigned long flags;
afae461a 824 unsigned int cnt;
5ddf24c5
TH
825
826 spin_lock_irqsave(ap->lock, flags);
827
828 cnt = ata_eh_nr_in_flight(ap);
829
830 /* are we done? */
831 if (!cnt)
832 goto out_unlock;
833
834 if (cnt == ap->fastdrain_cnt) {
258c4e5c 835 struct ata_queued_cmd *qc;
5ddf24c5
TH
836 unsigned int tag;
837
838 /* No progress during the last interval, tag all
839 * in-flight qcs as timed out and freeze the port.
840 */
258c4e5c 841 ata_qc_for_each(ap, qc, tag) {
5ddf24c5
TH
842 if (qc)
843 qc->err_mask |= AC_ERR_TIMEOUT;
844 }
845
846 ata_port_freeze(ap);
847 } else {
848 /* some qcs have finished, give it another chance */
849 ap->fastdrain_cnt = cnt;
850 ap->fastdrain_timer.expires =
341c2c95 851 ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
5ddf24c5
TH
852 add_timer(&ap->fastdrain_timer);
853 }
854
855 out_unlock:
856 spin_unlock_irqrestore(ap->lock, flags);
857}
858
859/**
860 * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
861 * @ap: target ATA port
862 * @fastdrain: activate fast drain
863 *
864 * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
865 * is non-zero and EH wasn't pending before. Fast drain ensures
866 * that EH kicks in in timely manner.
867 *
868 * LOCKING:
869 * spin_lock_irqsave(host lock)
870 */
871static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
872{
afae461a 873 unsigned int cnt;
5ddf24c5
TH
874
875 /* already scheduled? */
876 if (ap->pflags & ATA_PFLAG_EH_PENDING)
877 return;
878
879 ap->pflags |= ATA_PFLAG_EH_PENDING;
880
881 if (!fastdrain)
882 return;
883
884 /* do we have in-flight qcs? */
885 cnt = ata_eh_nr_in_flight(ap);
886 if (!cnt)
887 return;
888
889 /* activate fast drain */
890 ap->fastdrain_cnt = cnt;
341c2c95
TH
891 ap->fastdrain_timer.expires =
892 ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
5ddf24c5
TH
893 add_timer(&ap->fastdrain_timer);
894}
895
f686bcb8
TH
896/**
897 * ata_qc_schedule_eh - schedule qc for error handling
898 * @qc: command to schedule error handling for
899 *
900 * Schedule error handling for @qc. EH will kick in as soon as
901 * other commands are drained.
902 *
903 * LOCKING:
cca3974e 904 * spin_lock_irqsave(host lock)
f686bcb8
TH
905 */
906void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
907{
908 struct ata_port *ap = qc->ap;
909
910 WARN_ON(!ap->ops->error_handler);
911
912 qc->flags |= ATA_QCFLAG_FAILED;
5ddf24c5 913 ata_eh_set_pending(ap, 1);
f686bcb8
TH
914
915 /* The following will fail if timeout has already expired.
916 * ata_scsi_error() takes care of such scmds on EH entry.
917 * Note that ATA_QCFLAG_FAILED is unconditionally set after
918 * this function completes.
919 */
c8329cd5 920 blk_abort_request(scsi_cmd_to_rq(qc->scsicmd));
f686bcb8
TH
921}
922
7b70fc03 923/**
e4a9c373
DW
924 * ata_std_sched_eh - non-libsas ata_ports issue eh with this common routine
925 * @ap: ATA port to schedule EH for
7b70fc03 926 *
e4a9c373 927 * LOCKING: inherited from ata_port_schedule_eh
cca3974e 928 * spin_lock_irqsave(host lock)
7b70fc03 929 */
e4a9c373 930void ata_std_sched_eh(struct ata_port *ap)
7b70fc03
TH
931{
932 WARN_ON(!ap->ops->error_handler);
933
f4d6d004
TH
934 if (ap->pflags & ATA_PFLAG_INITIALIZING)
935 return;
936
5ddf24c5 937 ata_eh_set_pending(ap, 1);
cca3974e 938 scsi_schedule_eh(ap->scsi_host);
7b70fc03 939
c318458c 940 trace_ata_std_sched_eh(ap);
7b70fc03 941}
e4a9c373
DW
942EXPORT_SYMBOL_GPL(ata_std_sched_eh);
943
944/**
945 * ata_std_end_eh - non-libsas ata_ports complete eh with this common routine
946 * @ap: ATA port to end EH for
947 *
948 * In the libata object model there is a 1:1 mapping of ata_port to
949 * shost, so host fields can be directly manipulated under ap->lock, in
950 * the libsas case we need to hold a lock at the ha->level to coordinate
951 * these events.
952 *
953 * LOCKING:
954 * spin_lock_irqsave(host lock)
955 */
956void ata_std_end_eh(struct ata_port *ap)
957{
958 struct Scsi_Host *host = ap->scsi_host;
959
960 host->host_eh_scheduled = 0;
961}
962EXPORT_SYMBOL(ata_std_end_eh);
963
964
965/**
966 * ata_port_schedule_eh - schedule error handling without a qc
967 * @ap: ATA port to schedule EH for
968 *
969 * Schedule error handling for @ap. EH will kick in as soon as
970 * all commands are drained.
971 *
972 * LOCKING:
973 * spin_lock_irqsave(host lock)
974 */
975void ata_port_schedule_eh(struct ata_port *ap)
976{
977 /* see: ata_std_sched_eh, unless you know better */
978 ap->ops->sched_eh(ap);
979}
a52fbcfc 980EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
7b70fc03 981
dbd82616 982static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
7b70fc03 983{
258c4e5c 984 struct ata_queued_cmd *qc;
7b70fc03
TH
985 int tag, nr_aborted = 0;
986
987 WARN_ON(!ap->ops->error_handler);
988
5ddf24c5
TH
989 /* we're gonna abort all commands, no need for fast drain */
990 ata_eh_set_pending(ap, 0);
991
28361c40 992 /* include internal tag in iteration */
258c4e5c 993 ata_qc_for_each_with_internal(ap, qc, tag) {
dbd82616 994 if (qc && (!link || qc->dev->link == link)) {
7b70fc03
TH
995 qc->flags |= ATA_QCFLAG_FAILED;
996 ata_qc_complete(qc);
997 nr_aborted++;
998 }
999 }
1000
1001 if (!nr_aborted)
1002 ata_port_schedule_eh(ap);
1003
1004 return nr_aborted;
1005}
1006
dbd82616
TH
1007/**
1008 * ata_link_abort - abort all qc's on the link
1009 * @link: ATA link to abort qc's for
1010 *
1011 * Abort all active qc's active on @link and schedule EH.
1012 *
1013 * LOCKING:
1014 * spin_lock_irqsave(host lock)
1015 *
1016 * RETURNS:
1017 * Number of aborted qc's.
1018 */
1019int ata_link_abort(struct ata_link *link)
1020{
1021 return ata_do_link_abort(link->ap, link);
1022}
a52fbcfc 1023EXPORT_SYMBOL_GPL(ata_link_abort);
dbd82616
TH
1024
1025/**
1026 * ata_port_abort - abort all qc's on the port
1027 * @ap: ATA port to abort qc's for
1028 *
1029 * Abort all active qc's of @ap and schedule EH.
1030 *
1031 * LOCKING:
1032 * spin_lock_irqsave(host_set lock)
1033 *
1034 * RETURNS:
1035 * Number of aborted qc's.
1036 */
1037int ata_port_abort(struct ata_port *ap)
1038{
1039 return ata_do_link_abort(ap, NULL);
1040}
a52fbcfc 1041EXPORT_SYMBOL_GPL(ata_port_abort);
dbd82616 1042
e3180499
TH
1043/**
1044 * __ata_port_freeze - freeze port
1045 * @ap: ATA port to freeze
1046 *
1047 * This function is called when HSM violation or some other
1048 * condition disrupts normal operation of the port. Frozen port
1049 * is not allowed to perform any operation until the port is
1050 * thawed, which usually follows a successful reset.
1051 *
1052 * ap->ops->freeze() callback can be used for freezing the port
1053 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
1054 * port cannot be frozen hardware-wise, the interrupt handler
1055 * must ack and clear interrupts unconditionally while the port
1056 * is frozen.
1057 *
1058 * LOCKING:
cca3974e 1059 * spin_lock_irqsave(host lock)
e3180499
TH
1060 */
1061static void __ata_port_freeze(struct ata_port *ap)
1062{
1063 WARN_ON(!ap->ops->error_handler);
1064
1065 if (ap->ops->freeze)
1066 ap->ops->freeze(ap);
1067
b51e9e5d 1068 ap->pflags |= ATA_PFLAG_FROZEN;
e3180499 1069
c318458c 1070 trace_ata_port_freeze(ap);
e3180499
TH
1071}
1072
1073/**
1074 * ata_port_freeze - abort & freeze port
1075 * @ap: ATA port to freeze
1076 *
54c38444
JG
1077 * Abort and freeze @ap. The freeze operation must be called
1078 * first, because some hardware requires special operations
1079 * before the taskfile registers are accessible.
e3180499
TH
1080 *
1081 * LOCKING:
cca3974e 1082 * spin_lock_irqsave(host lock)
e3180499
TH
1083 *
1084 * RETURNS:
1085 * Number of aborted commands.
1086 */
1087int ata_port_freeze(struct ata_port *ap)
1088{
1089 int nr_aborted;
1090
1091 WARN_ON(!ap->ops->error_handler);
1092
e3180499 1093 __ata_port_freeze(ap);
54c38444 1094 nr_aborted = ata_port_abort(ap);
e3180499
TH
1095
1096 return nr_aborted;
1097}
a52fbcfc 1098EXPORT_SYMBOL_GPL(ata_port_freeze);
e3180499
TH
1099
1100/**
1101 * ata_eh_freeze_port - EH helper to freeze port
1102 * @ap: ATA port to freeze
1103 *
1104 * Freeze @ap.
1105 *
1106 * LOCKING:
1107 * None.
1108 */
1109void ata_eh_freeze_port(struct ata_port *ap)
1110{
1111 unsigned long flags;
1112
1113 if (!ap->ops->error_handler)
1114 return;
1115
ba6a1308 1116 spin_lock_irqsave(ap->lock, flags);
e3180499 1117 __ata_port_freeze(ap);
ba6a1308 1118 spin_unlock_irqrestore(ap->lock, flags);
e3180499 1119}
a52fbcfc 1120EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
e3180499
TH
1121
1122/**
94bd5719 1123 * ata_eh_thaw_port - EH helper to thaw port
e3180499
TH
1124 * @ap: ATA port to thaw
1125 *
1126 * Thaw frozen port @ap.
1127 *
1128 * LOCKING:
1129 * None.
1130 */
1131void ata_eh_thaw_port(struct ata_port *ap)
1132{
1133 unsigned long flags;
1134
1135 if (!ap->ops->error_handler)
1136 return;
1137
ba6a1308 1138 spin_lock_irqsave(ap->lock, flags);
e3180499 1139
b51e9e5d 1140 ap->pflags &= ~ATA_PFLAG_FROZEN;
e3180499
TH
1141
1142 if (ap->ops->thaw)
1143 ap->ops->thaw(ap);
1144
ba6a1308 1145 spin_unlock_irqrestore(ap->lock, flags);
e3180499 1146
c318458c 1147 trace_ata_port_thaw(ap);
e3180499
TH
1148}
1149
ece1d636
TH
1150static void ata_eh_scsidone(struct scsi_cmnd *scmd)
1151{
1152 /* nada */
1153}
1154
1155static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
1156{
1157 struct ata_port *ap = qc->ap;
1158 struct scsi_cmnd *scmd = qc->scsicmd;
1159 unsigned long flags;
1160
ba6a1308 1161 spin_lock_irqsave(ap->lock, flags);
ece1d636
TH
1162 qc->scsidone = ata_eh_scsidone;
1163 __ata_qc_complete(qc);
1164 WARN_ON(ata_tag_valid(qc->tag));
ba6a1308 1165 spin_unlock_irqrestore(ap->lock, flags);
ece1d636
TH
1166
1167 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
1168}
1169
1170/**
1171 * ata_eh_qc_complete - Complete an active ATA command from EH
1172 * @qc: Command to complete
1173 *
1174 * Indicate to the mid and upper layers that an ATA command has
1175 * completed. To be used from EH.
1176 */
1177void ata_eh_qc_complete(struct ata_queued_cmd *qc)
1178{
1179 struct scsi_cmnd *scmd = qc->scsicmd;
1180 scmd->retries = scmd->allowed;
1181 __ata_eh_qc_complete(qc);
1182}
1183
1184/**
1185 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
1186 * @qc: Command to retry
1187 *
1188 * Indicate to the mid and upper layers that an ATA command
1189 * should be retried. To be used from EH.
1190 *
1191 * SCSI midlayer limits the number of retries to scmd->allowed.
f13e2201 1192 * scmd->allowed is incremented for commands which get retried
ece1d636
TH
1193 * due to unrelated failures (qc->err_mask is zero).
1194 */
1195void ata_eh_qc_retry(struct ata_queued_cmd *qc)
1196{
1197 struct scsi_cmnd *scmd = qc->scsicmd;
f13e2201
GG
1198 if (!qc->err_mask)
1199 scmd->allowed++;
ece1d636
TH
1200 __ata_eh_qc_complete(qc);
1201}
022bdb07 1202
678afac6
TH
1203/**
1204 * ata_dev_disable - disable ATA device
1205 * @dev: ATA device to disable
1206 *
1207 * Disable @dev.
1208 *
1209 * Locking:
1210 * EH context.
1211 */
1212void ata_dev_disable(struct ata_device *dev)
1213{
1214 if (!ata_dev_enabled(dev))
1215 return;
1216
1c95a27c 1217 ata_dev_warn(dev, "disable device\n");
678afac6
TH
1218 ata_acpi_on_disable(dev);
1219 ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 | ATA_DNXFER_QUIET);
1220 dev->class++;
99cf610a
TH
1221
1222 /* From now till the next successful probe, ering is used to
1223 * track probe failures. Clear accumulated device error info.
1224 */
1225 ata_ering_clear(&dev->ering);
678afac6 1226}
a52fbcfc 1227EXPORT_SYMBOL_GPL(ata_dev_disable);
678afac6 1228
0ea035a3
TH
1229/**
1230 * ata_eh_detach_dev - detach ATA device
1231 * @dev: ATA device to detach
1232 *
1233 * Detach @dev.
1234 *
1235 * LOCKING:
1236 * None.
1237 */
fb7fd614 1238void ata_eh_detach_dev(struct ata_device *dev)
0ea035a3 1239{
f58229f8
TH
1240 struct ata_link *link = dev->link;
1241 struct ata_port *ap = link->ap;
90484ebf 1242 struct ata_eh_context *ehc = &link->eh_context;
0ea035a3
TH
1243 unsigned long flags;
1244
1245 ata_dev_disable(dev);
1246
ba6a1308 1247 spin_lock_irqsave(ap->lock, flags);
0ea035a3
TH
1248
1249 dev->flags &= ~ATA_DFLAG_DETACH;
1250
1251 if (ata_scsi_offline_dev(dev)) {
1252 dev->flags |= ATA_DFLAG_DETACHED;
b51e9e5d 1253 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
0ea035a3
TH
1254 }
1255
90484ebf 1256 /* clear per-dev EH info */
f58229f8
TH
1257 ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
1258 ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
90484ebf
TH
1259 ehc->saved_xfer_mode[dev->devno] = 0;
1260 ehc->saved_ncq_enabled &= ~(1 << dev->devno);
beb07c1a 1261
ba6a1308 1262 spin_unlock_irqrestore(ap->lock, flags);
0ea035a3
TH
1263}
1264
022bdb07
TH
1265/**
1266 * ata_eh_about_to_do - about to perform eh_action
955e57df 1267 * @link: target ATA link
47005f25 1268 * @dev: target ATA dev for per-dev action (can be NULL)
022bdb07
TH
1269 * @action: action about to be performed
1270 *
1271 * Called just before performing EH actions to clear related bits
955e57df
TH
1272 * in @link->eh_info such that eh actions are not unnecessarily
1273 * repeated.
022bdb07
TH
1274 *
1275 * LOCKING:
1276 * None.
1277 */
fb7fd614
TH
1278void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
1279 unsigned int action)
022bdb07 1280{
955e57df
TH
1281 struct ata_port *ap = link->ap;
1282 struct ata_eh_info *ehi = &link->eh_info;
1283 struct ata_eh_context *ehc = &link->eh_context;
022bdb07
TH
1284 unsigned long flags;
1285
c318458c
HR
1286 trace_ata_eh_about_to_do(link, dev ? dev->devno : 0, action);
1287
ba6a1308 1288 spin_lock_irqsave(ap->lock, flags);
1cdaf534 1289
955e57df 1290 ata_eh_clear_action(link, dev, ehi, action);
1cdaf534 1291
a568d1d2
TH
1292 /* About to take EH action, set RECOVERED. Ignore actions on
1293 * slave links as master will do them again.
1294 */
1295 if (!(ehc->i.flags & ATA_EHI_QUIET) && link != ap->slave_link)
1cdaf534
TH
1296 ap->pflags |= ATA_PFLAG_RECOVERED;
1297
ba6a1308 1298 spin_unlock_irqrestore(ap->lock, flags);
022bdb07
TH
1299}
1300
47005f25
TH
1301/**
1302 * ata_eh_done - EH action complete
2f60e1ab 1303 * @link: ATA link for which EH actions are complete
47005f25
TH
1304 * @dev: target ATA dev for per-dev action (can be NULL)
1305 * @action: action just completed
1306 *
1307 * Called right after performing EH actions to clear related bits
955e57df 1308 * in @link->eh_context.
47005f25
TH
1309 *
1310 * LOCKING:
1311 * None.
1312 */
fb7fd614
TH
1313void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1314 unsigned int action)
47005f25 1315{
955e57df 1316 struct ata_eh_context *ehc = &link->eh_context;
9af5c9c9 1317
c318458c
HR
1318 trace_ata_eh_done(link, dev ? dev->devno : 0, action);
1319
955e57df 1320 ata_eh_clear_action(link, dev, &ehc->i, action);
47005f25
TH
1321}
1322
022bdb07
TH
1323/**
1324 * ata_err_string - convert err_mask to descriptive string
1325 * @err_mask: error mask to convert to string
1326 *
1327 * Convert @err_mask to descriptive string. Errors are
1328 * prioritized according to severity and only the most severe
1329 * error is reported.
1330 *
1331 * LOCKING:
1332 * None.
1333 *
1334 * RETURNS:
1335 * Descriptive string for @err_mask
1336 */
2dcb407e 1337static const char *ata_err_string(unsigned int err_mask)
022bdb07
TH
1338{
1339 if (err_mask & AC_ERR_HOST_BUS)
1340 return "host bus error";
1341 if (err_mask & AC_ERR_ATA_BUS)
1342 return "ATA bus error";
1343 if (err_mask & AC_ERR_TIMEOUT)
1344 return "timeout";
1345 if (err_mask & AC_ERR_HSM)
1346 return "HSM violation";
1347 if (err_mask & AC_ERR_SYSTEM)
1348 return "internal error";
1349 if (err_mask & AC_ERR_MEDIA)
1350 return "media error";
1351 if (err_mask & AC_ERR_INVALID)
1352 return "invalid argument";
1353 if (err_mask & AC_ERR_DEV)
1354 return "device error";
54fb131b
DLM
1355 if (err_mask & AC_ERR_NCQ)
1356 return "NCQ error";
1357 if (err_mask & AC_ERR_NODEV_HINT)
1358 return "Polling detection error";
022bdb07
TH
1359 return "unknown error";
1360}
1361
11fc33da
TH
1362/**
1363 * atapi_eh_tur - perform ATAPI TEST_UNIT_READY
1364 * @dev: target ATAPI device
1365 * @r_sense_key: out parameter for sense_key
1366 *
1367 * Perform ATAPI TEST_UNIT_READY.
1368 *
1369 * LOCKING:
1370 * EH context (may sleep).
1371 *
1372 * RETURNS:
1373 * 0 on success, AC_ERR_* mask on failure.
1374 */
3dc67440 1375unsigned int atapi_eh_tur(struct ata_device *dev, u8 *r_sense_key)
11fc33da
TH
1376{
1377 u8 cdb[ATAPI_CDB_LEN] = { TEST_UNIT_READY, 0, 0, 0, 0, 0 };
1378 struct ata_taskfile tf;
1379 unsigned int err_mask;
1380
1381 ata_tf_init(dev, &tf);
1382
1383 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1384 tf.command = ATA_CMD_PACKET;
1385 tf.protocol = ATAPI_PROT_NODATA;
1386
1387 err_mask = ata_exec_internal(dev, &tf, cdb, DMA_NONE, NULL, 0, 0);
1388 if (err_mask == AC_ERR_DEV)
efcef265 1389 *r_sense_key = tf.error >> 4;
11fc33da
TH
1390 return err_mask;
1391}
1392
e87fd28c
HR
1393/**
1394 * ata_eh_request_sense - perform REQUEST_SENSE_DATA_EXT
2f60e1ab 1395 * @qc: qc to perform REQUEST_SENSE_SENSE_DATA_EXT to
e87fd28c
HR
1396 * @cmd: scsi command for which the sense code should be set
1397 *
1398 * Perform REQUEST_SENSE_DATA_EXT after the device reported CHECK
1399 * SENSE. This function is an EH helper.
1400 *
1401 * LOCKING:
1402 * Kernel thread context (may sleep).
1403 */
1404static void ata_eh_request_sense(struct ata_queued_cmd *qc,
1405 struct scsi_cmnd *cmd)
1406{
1407 struct ata_device *dev = qc->dev;
1408 struct ata_taskfile tf;
1409 unsigned int err_mask;
1410
1411 if (qc->ap->pflags & ATA_PFLAG_FROZEN) {
1412 ata_dev_warn(dev, "sense data available but port frozen\n");
1413 return;
1414 }
1415
d238ffd5 1416 if (!cmd || qc->flags & ATA_QCFLAG_SENSE_VALID)
e87fd28c
HR
1417 return;
1418
1419 if (!ata_id_sense_reporting_enabled(dev->id)) {
1420 ata_dev_warn(qc->dev, "sense data reporting disabled\n");
1421 return;
1422 }
1423
e87fd28c
HR
1424 ata_tf_init(dev, &tf);
1425 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1426 tf.flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
1427 tf.command = ATA_CMD_REQ_SENSE_DATA;
1428 tf.protocol = ATA_PROT_NODATA;
1429
1430 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1431 /* Ignore err_mask; ATA_ERR might be set */
efcef265 1432 if (tf.status & ATA_SENSE) {
06dbde5f 1433 ata_scsi_set_sense(dev, cmd, tf.lbah, tf.lbam, tf.lbal);
e87fd28c
HR
1434 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1435 } else {
1436 ata_dev_warn(dev, "request sense failed stat %02x emask %x\n",
efcef265 1437 tf.status, err_mask);
e87fd28c
HR
1438 }
1439}
1440
022bdb07
TH
1441/**
1442 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1443 * @dev: device to perform REQUEST_SENSE to
1444 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
3eabddb8 1445 * @dfl_sense_key: default sense key to use
022bdb07
TH
1446 *
1447 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
1448 * SENSE. This function is EH helper.
1449 *
1450 * LOCKING:
1451 * Kernel thread context (may sleep).
1452 *
1453 * RETURNS:
1454 * 0 on success, AC_ERR_* mask on failure
1455 */
3dc67440 1456unsigned int atapi_eh_request_sense(struct ata_device *dev,
3eabddb8 1457 u8 *sense_buf, u8 dfl_sense_key)
022bdb07 1458{
3eabddb8
TH
1459 u8 cdb[ATAPI_CDB_LEN] =
1460 { REQUEST_SENSE, 0, 0, 0, SCSI_SENSE_BUFFERSIZE, 0 };
9af5c9c9 1461 struct ata_port *ap = dev->link->ap;
022bdb07 1462 struct ata_taskfile tf;
022bdb07 1463
022bdb07
TH
1464 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1465
56287768
AL
1466 /* initialize sense_buf with the error register,
1467 * for the case where they are -not- overwritten
1468 */
022bdb07 1469 sense_buf[0] = 0x70;
3eabddb8 1470 sense_buf[2] = dfl_sense_key;
56287768 1471
a617c09f 1472 /* some devices time out if garbage left in tf */
56287768 1473 ata_tf_init(dev, &tf);
022bdb07 1474
022bdb07
TH
1475 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1476 tf.command = ATA_CMD_PACKET;
1477
1478 /* is it pointless to prefer PIO for "safety reasons"? */
1479 if (ap->flags & ATA_FLAG_PIO_DMA) {
0dc36888 1480 tf.protocol = ATAPI_PROT_DMA;
022bdb07
TH
1481 tf.feature |= ATAPI_PKT_DMA;
1482 } else {
0dc36888 1483 tf.protocol = ATAPI_PROT_PIO;
f2dfc1a1
TH
1484 tf.lbam = SCSI_SENSE_BUFFERSIZE;
1485 tf.lbah = 0;
022bdb07
TH
1486 }
1487
1488 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
2b789108 1489 sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
022bdb07
TH
1490}
1491
1492/**
1493 * ata_eh_analyze_serror - analyze SError for a failed port
0260731f 1494 * @link: ATA link to analyze SError for
022bdb07
TH
1495 *
1496 * Analyze SError if available and further determine cause of
1497 * failure.
1498 *
1499 * LOCKING:
1500 * None.
1501 */
0260731f 1502static void ata_eh_analyze_serror(struct ata_link *link)
022bdb07 1503{
0260731f 1504 struct ata_eh_context *ehc = &link->eh_context;
022bdb07
TH
1505 u32 serror = ehc->i.serror;
1506 unsigned int err_mask = 0, action = 0;
f9df58cb 1507 u32 hotplug_mask;
022bdb07 1508
e0614db2 1509 if (serror & (SERR_PERSISTENT | SERR_DATA)) {
022bdb07 1510 err_mask |= AC_ERR_ATA_BUS;
cf480626 1511 action |= ATA_EH_RESET;
022bdb07
TH
1512 }
1513 if (serror & SERR_PROTOCOL) {
1514 err_mask |= AC_ERR_HSM;
cf480626 1515 action |= ATA_EH_RESET;
022bdb07
TH
1516 }
1517 if (serror & SERR_INTERNAL) {
1518 err_mask |= AC_ERR_SYSTEM;
cf480626 1519 action |= ATA_EH_RESET;
022bdb07 1520 }
f9df58cb
TH
1521
1522 /* Determine whether a hotplug event has occurred. Both
1523 * SError.N/X are considered hotplug events for enabled or
1524 * host links. For disabled PMP links, only N bit is
1525 * considered as X bit is left at 1 for link plugging.
1526 */
eb0e85e3 1527 if (link->lpm_policy > ATA_LPM_MAX_POWER)
6b7ae954
TH
1528 hotplug_mask = 0; /* hotplug doesn't work w/ LPM */
1529 else if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
f9df58cb
TH
1530 hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
1531 else
1532 hotplug_mask = SERR_PHYRDY_CHG;
1533
1534 if (serror & hotplug_mask)
084fe639 1535 ata_ehi_hotplugged(&ehc->i);
022bdb07
TH
1536
1537 ehc->i.err_mask |= err_mask;
1538 ehc->i.action |= action;
1539}
1540
1541/**
1542 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1543 * @qc: qc to analyze
1544 * @tf: Taskfile registers to analyze
1545 *
1546 * Analyze taskfile of @qc and further determine cause of
1547 * failure. This function also requests ATAPI sense data if
25985edc 1548 * available.
022bdb07
TH
1549 *
1550 * LOCKING:
1551 * Kernel thread context (may sleep).
1552 *
1553 * RETURNS:
1554 * Determined recovery action
1555 */
1556static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1557 const struct ata_taskfile *tf)
1558{
1559 unsigned int tmp, action = 0;
efcef265 1560 u8 stat = tf->status, err = tf->error;
022bdb07
TH
1561
1562 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1563 qc->err_mask |= AC_ERR_HSM;
cf480626 1564 return ATA_EH_RESET;
022bdb07
TH
1565 }
1566
e87fd28c 1567 if (stat & (ATA_ERR | ATA_DF)) {
a51d644a 1568 qc->err_mask |= AC_ERR_DEV;
e87fd28c
HR
1569 /*
1570 * Sense data reporting does not work if the
1571 * device fault bit is set.
1572 */
1573 if (stat & ATA_DF)
1574 stat &= ~ATA_SENSE;
1575 } else {
022bdb07 1576 return 0;
e87fd28c 1577 }
022bdb07
TH
1578
1579 switch (qc->dev->class) {
9162c657 1580 case ATA_DEV_ZAC:
e87fd28c
HR
1581 if (stat & ATA_SENSE)
1582 ata_eh_request_sense(qc, qc->scsicmd);
df561f66 1583 fallthrough;
ca156e00 1584 case ATA_DEV_ATA:
022bdb07
TH
1585 if (err & ATA_ICRC)
1586 qc->err_mask |= AC_ERR_ATA_BUS;
eec7e1c1 1587 if (err & (ATA_UNC | ATA_AMNF))
022bdb07
TH
1588 qc->err_mask |= AC_ERR_MEDIA;
1589 if (err & ATA_IDNF)
1590 qc->err_mask |= AC_ERR_INVALID;
1591 break;
1592
1593 case ATA_DEV_ATAPI:
a569a30d 1594 if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
3eabddb8
TH
1595 tmp = atapi_eh_request_sense(qc->dev,
1596 qc->scsicmd->sense_buffer,
efcef265 1597 qc->result_tf.error >> 4);
3852e373 1598 if (!tmp)
a569a30d 1599 qc->flags |= ATA_QCFLAG_SENSE_VALID;
3852e373 1600 else
a569a30d
TH
1601 qc->err_mask |= tmp;
1602 }
022bdb07
TH
1603 }
1604
3852e373 1605 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
b8e162f9 1606 enum scsi_disposition ret = scsi_check_sense(qc->scsicmd);
3852e373 1607 /*
79487259 1608 * SUCCESS here means that the sense code could be
3852e373
HR
1609 * evaluated and should be passed to the upper layers
1610 * for correct evaluation.
79487259 1611 * FAILED means the sense code could not be interpreted
3852e373
HR
1612 * and the device would need to be reset.
1613 * NEEDS_RETRY and ADD_TO_MLQUEUE means that the
1614 * command would need to be retried.
1615 */
1616 if (ret == NEEDS_RETRY || ret == ADD_TO_MLQUEUE) {
1617 qc->flags |= ATA_QCFLAG_RETRY;
1618 qc->err_mask |= AC_ERR_OTHER;
1619 } else if (ret != SUCCESS) {
1620 qc->err_mask |= AC_ERR_HSM;
1621 }
1622 }
022bdb07 1623 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
cf480626 1624 action |= ATA_EH_RESET;
022bdb07
TH
1625
1626 return action;
1627}
1628
76326ac1
TH
1629static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask,
1630 int *xfer_ok)
022bdb07 1631{
76326ac1
TH
1632 int base = 0;
1633
1634 if (!(eflags & ATA_EFLAG_DUBIOUS_XFER))
1635 *xfer_ok = 1;
1636
1637 if (!*xfer_ok)
75f9cafc 1638 base = ATA_ECAT_DUBIOUS_NONE;
76326ac1 1639
7d47e8d4 1640 if (err_mask & AC_ERR_ATA_BUS)
76326ac1 1641 return base + ATA_ECAT_ATA_BUS;
022bdb07 1642
7d47e8d4 1643 if (err_mask & AC_ERR_TIMEOUT)
76326ac1 1644 return base + ATA_ECAT_TOUT_HSM;
7d47e8d4 1645
3884f7b0 1646 if (eflags & ATA_EFLAG_IS_IO) {
7d47e8d4 1647 if (err_mask & AC_ERR_HSM)
76326ac1 1648 return base + ATA_ECAT_TOUT_HSM;
7d47e8d4
TH
1649 if ((err_mask &
1650 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
76326ac1 1651 return base + ATA_ECAT_UNK_DEV;
022bdb07
TH
1652 }
1653
1654 return 0;
1655}
1656
7d47e8d4 1657struct speed_down_verdict_arg {
022bdb07 1658 u64 since;
76326ac1 1659 int xfer_ok;
3884f7b0 1660 int nr_errors[ATA_ECAT_NR];
022bdb07
TH
1661};
1662
7d47e8d4 1663static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
022bdb07 1664{
7d47e8d4 1665 struct speed_down_verdict_arg *arg = void_arg;
76326ac1 1666 int cat;
022bdb07 1667
d9027470 1668 if ((ent->eflags & ATA_EFLAG_OLD_ER) || (ent->timestamp < arg->since))
022bdb07
TH
1669 return -1;
1670
76326ac1
TH
1671 cat = ata_eh_categorize_error(ent->eflags, ent->err_mask,
1672 &arg->xfer_ok);
7d47e8d4 1673 arg->nr_errors[cat]++;
76326ac1 1674
022bdb07
TH
1675 return 0;
1676}
1677
1678/**
7d47e8d4 1679 * ata_eh_speed_down_verdict - Determine speed down verdict
022bdb07
TH
1680 * @dev: Device of interest
1681 *
1682 * This function examines error ring of @dev and determines
7d47e8d4
TH
1683 * whether NCQ needs to be turned off, transfer speed should be
1684 * stepped down, or falling back to PIO is necessary.
022bdb07 1685 *
3884f7b0
TH
1686 * ECAT_ATA_BUS : ATA_BUS error for any command
1687 *
1688 * ECAT_TOUT_HSM : TIMEOUT for any command or HSM violation for
1689 * IO commands
1690 *
1691 * ECAT_UNK_DEV : Unknown DEV error for IO commands
1692 *
76326ac1
TH
1693 * ECAT_DUBIOUS_* : Identical to above three but occurred while
1694 * data transfer hasn't been verified.
1695 *
3884f7b0
TH
1696 * Verdicts are
1697 *
1698 * NCQ_OFF : Turn off NCQ.
022bdb07 1699 *
3884f7b0
TH
1700 * SPEED_DOWN : Speed down transfer speed but don't fall back
1701 * to PIO.
7d47e8d4 1702 *
3884f7b0 1703 * FALLBACK_TO_PIO : Fall back to PIO.
022bdb07 1704 *
3884f7b0 1705 * Even if multiple verdicts are returned, only one action is
76326ac1
TH
1706 * taken per error. An action triggered by non-DUBIOUS errors
1707 * clears ering, while one triggered by DUBIOUS_* errors doesn't.
1708 * This is to expedite speed down decisions right after device is
1709 * initially configured.
1710 *
4091fb95 1711 * The following are speed down rules. #1 and #2 deal with
76326ac1 1712 * DUBIOUS errors.
7d47e8d4 1713 *
76326ac1
TH
1714 * 1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors
1715 * occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO.
1716 *
1717 * 2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors
1718 * occurred during last 5 mins, NCQ_OFF.
1719 *
1720 * 3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors
25985edc 1721 * occurred during last 5 mins, FALLBACK_TO_PIO
7d47e8d4 1722 *
76326ac1 1723 * 4. If more than 3 TOUT_HSM or UNK_DEV errors occurred
3884f7b0
TH
1724 * during last 10 mins, NCQ_OFF.
1725 *
76326ac1 1726 * 5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6
3884f7b0 1727 * UNK_DEV errors occurred during last 10 mins, SPEED_DOWN.
7d47e8d4 1728 *
022bdb07
TH
1729 * LOCKING:
1730 * Inherited from caller.
1731 *
1732 * RETURNS:
7d47e8d4 1733 * OR of ATA_EH_SPDN_* flags.
022bdb07 1734 */
7d47e8d4 1735static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
022bdb07 1736{
7d47e8d4
TH
1737 const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
1738 u64 j64 = get_jiffies_64();
1739 struct speed_down_verdict_arg arg;
1740 unsigned int verdict = 0;
022bdb07 1741
3884f7b0 1742 /* scan past 5 mins of error history */
7d47e8d4 1743 memset(&arg, 0, sizeof(arg));
3884f7b0 1744 arg.since = j64 - min(j64, j5mins);
7d47e8d4 1745 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
022bdb07 1746
76326ac1
TH
1747 if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] +
1748 arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1)
1749 verdict |= ATA_EH_SPDN_SPEED_DOWN |
1750 ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS;
1751
1752 if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] +
1753 arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1)
1754 verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS;
1755
3884f7b0
TH
1756 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1757 arg.nr_errors[ATA_ECAT_TOUT_HSM] +
663f99b8 1758 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
3884f7b0 1759 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
022bdb07 1760
3884f7b0 1761 /* scan past 10 mins of error history */
022bdb07 1762 memset(&arg, 0, sizeof(arg));
3884f7b0 1763 arg.since = j64 - min(j64, j10mins);
7d47e8d4 1764 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
022bdb07 1765
3884f7b0
TH
1766 if (arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1767 arg.nr_errors[ATA_ECAT_UNK_DEV] > 3)
1768 verdict |= ATA_EH_SPDN_NCQ_OFF;
1769
1770 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1771 arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 ||
663f99b8 1772 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
3884f7b0 1773 verdict |= ATA_EH_SPDN_SPEED_DOWN;
022bdb07 1774
7d47e8d4 1775 return verdict;
022bdb07
TH
1776}
1777
1778/**
1779 * ata_eh_speed_down - record error and speed down if necessary
1780 * @dev: Failed device
3884f7b0 1781 * @eflags: mask of ATA_EFLAG_* flags
022bdb07
TH
1782 * @err_mask: err_mask of the error
1783 *
1784 * Record error and examine error history to determine whether
1785 * adjusting transmission speed is necessary. It also sets
1786 * transmission limits appropriately if such adjustment is
1787 * necessary.
1788 *
1789 * LOCKING:
1790 * Kernel thread context (may sleep).
1791 *
1792 * RETURNS:
7d47e8d4 1793 * Determined recovery action.
022bdb07 1794 */
3884f7b0
TH
1795static unsigned int ata_eh_speed_down(struct ata_device *dev,
1796 unsigned int eflags, unsigned int err_mask)
022bdb07 1797{
b1c72916 1798 struct ata_link *link = ata_dev_phys_link(dev);
76326ac1 1799 int xfer_ok = 0;
7d47e8d4
TH
1800 unsigned int verdict;
1801 unsigned int action = 0;
1802
1803 /* don't bother if Cat-0 error */
76326ac1 1804 if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0)
022bdb07
TH
1805 return 0;
1806
1807 /* record error and determine whether speed down is necessary */
3884f7b0 1808 ata_ering_record(&dev->ering, eflags, err_mask);
7d47e8d4 1809 verdict = ata_eh_speed_down_verdict(dev);
022bdb07 1810
7d47e8d4
TH
1811 /* turn off NCQ? */
1812 if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
1813 (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
1814 ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
1815 dev->flags |= ATA_DFLAG_NCQ_OFF;
a9a79dfe 1816 ata_dev_warn(dev, "NCQ disabled due to excessive errors\n");
7d47e8d4
TH
1817 goto done;
1818 }
022bdb07 1819
7d47e8d4
TH
1820 /* speed down? */
1821 if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1822 /* speed down SATA link speed if possible */
a07d499b 1823 if (sata_down_spd_limit(link, 0) == 0) {
cf480626 1824 action |= ATA_EH_RESET;
7d47e8d4
TH
1825 goto done;
1826 }
022bdb07 1827
7d47e8d4
TH
1828 /* lower transfer mode */
1829 if (dev->spdn_cnt < 2) {
1830 static const int dma_dnxfer_sel[] =
1831 { ATA_DNXFER_DMA, ATA_DNXFER_40C };
1832 static const int pio_dnxfer_sel[] =
1833 { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
1834 int sel;
1835
1836 if (dev->xfer_shift != ATA_SHIFT_PIO)
1837 sel = dma_dnxfer_sel[dev->spdn_cnt];
1838 else
1839 sel = pio_dnxfer_sel[dev->spdn_cnt];
1840
1841 dev->spdn_cnt++;
1842
1843 if (ata_down_xfermask_limit(dev, sel) == 0) {
cf480626 1844 action |= ATA_EH_RESET;
7d47e8d4
TH
1845 goto done;
1846 }
1847 }
1848 }
1849
1850 /* Fall back to PIO? Slowing down to PIO is meaningless for
663f99b8 1851 * SATA ATA devices. Consider it only for PATA and SATAPI.
7d47e8d4
TH
1852 */
1853 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
663f99b8 1854 (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) &&
7d47e8d4
TH
1855 (dev->xfer_shift != ATA_SHIFT_PIO)) {
1856 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
1857 dev->spdn_cnt = 0;
cf480626 1858 action |= ATA_EH_RESET;
7d47e8d4
TH
1859 goto done;
1860 }
1861 }
022bdb07 1862
022bdb07 1863 return 0;
7d47e8d4
TH
1864 done:
1865 /* device has been slowed down, blow error history */
76326ac1
TH
1866 if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS))
1867 ata_ering_clear(&dev->ering);
7d47e8d4 1868 return action;
022bdb07
TH
1869}
1870
8d899e70
ML
1871/**
1872 * ata_eh_worth_retry - analyze error and decide whether to retry
1873 * @qc: qc to possibly retry
1874 *
1875 * Look at the cause of the error and decide if a retry
1876 * might be useful or not. We don't want to retry media errors
1877 * because the drive itself has probably already taken 10-30 seconds
1878 * doing its own internal retries before reporting the failure.
1879 */
1880static inline int ata_eh_worth_retry(struct ata_queued_cmd *qc)
1881{
1eaca39a 1882 if (qc->err_mask & AC_ERR_MEDIA)
8d899e70
ML
1883 return 0; /* don't retry media errors */
1884 if (qc->flags & ATA_QCFLAG_IO)
1885 return 1; /* otherwise retry anything from fs stack */
1886 if (qc->err_mask & AC_ERR_INVALID)
1887 return 0; /* don't retry these */
1888 return qc->err_mask != AC_ERR_DEV; /* retry if not dev error */
1889}
1890
7eb49509
DLM
1891/**
1892 * ata_eh_quiet - check if we need to be quiet about a command error
1893 * @qc: qc to check
1894 *
1895 * Look at the qc flags anbd its scsi command request flags to determine
1896 * if we need to be quiet about the command failure.
1897 */
1898static inline bool ata_eh_quiet(struct ata_queued_cmd *qc)
1899{
c8329cd5 1900 if (qc->scsicmd && scsi_cmd_to_rq(qc->scsicmd)->rq_flags & RQF_QUIET)
7eb49509
DLM
1901 qc->flags |= ATA_QCFLAG_QUIET;
1902 return qc->flags & ATA_QCFLAG_QUIET;
1903}
1904
022bdb07 1905/**
9b1e2658
TH
1906 * ata_eh_link_autopsy - analyze error and determine recovery action
1907 * @link: host link to perform autopsy on
022bdb07 1908 *
0260731f
TH
1909 * Analyze why @link failed and determine which recovery actions
1910 * are needed. This function also sets more detailed AC_ERR_*
1911 * values and fills sense data for ATAPI CHECK SENSE.
022bdb07
TH
1912 *
1913 * LOCKING:
1914 * Kernel thread context (may sleep).
1915 */
9b1e2658 1916static void ata_eh_link_autopsy(struct ata_link *link)
022bdb07 1917{
0260731f 1918 struct ata_port *ap = link->ap;
936fd732 1919 struct ata_eh_context *ehc = &link->eh_context;
258c4e5c 1920 struct ata_queued_cmd *qc;
dfcc173d 1921 struct ata_device *dev;
3884f7b0 1922 unsigned int all_err_mask = 0, eflags = 0;
7eb49509 1923 int tag, nr_failed = 0, nr_quiet = 0;
022bdb07
TH
1924 u32 serror;
1925 int rc;
1926
1cdaf534
TH
1927 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1928 return;
1929
022bdb07 1930 /* obtain and analyze SError */
936fd732 1931 rc = sata_scr_read(link, SCR_ERROR, &serror);
022bdb07
TH
1932 if (rc == 0) {
1933 ehc->i.serror |= serror;
0260731f 1934 ata_eh_analyze_serror(link);
4e57c517 1935 } else if (rc != -EOPNOTSUPP) {
cf480626 1936 /* SError read failed, force reset and probing */
b558eddd 1937 ehc->i.probe_mask |= ATA_ALL_DEVICES;
cf480626 1938 ehc->i.action |= ATA_EH_RESET;
4e57c517
TH
1939 ehc->i.err_mask |= AC_ERR_OTHER;
1940 }
022bdb07 1941
e8ee8451 1942 /* analyze NCQ failure */
0260731f 1943 ata_eh_analyze_ncq_error(link);
e8ee8451 1944
022bdb07
TH
1945 /* any real error trumps AC_ERR_OTHER */
1946 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1947 ehc->i.err_mask &= ~AC_ERR_OTHER;
1948
1949 all_err_mask |= ehc->i.err_mask;
1950
258c4e5c 1951 ata_qc_for_each_raw(ap, qc, tag) {
b1c72916
TH
1952 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
1953 ata_dev_phys_link(qc->dev) != link)
022bdb07
TH
1954 continue;
1955
1956 /* inherit upper level err_mask */
1957 qc->err_mask |= ehc->i.err_mask;
1958
022bdb07 1959 /* analyze TF */
4528e4da 1960 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
022bdb07
TH
1961
1962 /* DEV errors are probably spurious in case of ATA_BUS error */
1963 if (qc->err_mask & AC_ERR_ATA_BUS)
1964 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1965 AC_ERR_INVALID);
1966
1967 /* any real error trumps unknown error */
1968 if (qc->err_mask & ~AC_ERR_OTHER)
1969 qc->err_mask &= ~AC_ERR_OTHER;
1970
804689ad
DLM
1971 /*
1972 * SENSE_VALID trumps dev/unknown error and revalidation. Upper
1973 * layers will determine whether the command is worth retrying
1974 * based on the sense data and device class/type. Otherwise,
1975 * determine directly if the command is worth retrying using its
1976 * error mask and flags.
1977 */
f90f0828 1978 if (qc->flags & ATA_QCFLAG_SENSE_VALID)
022bdb07 1979 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
804689ad 1980 else if (ata_eh_worth_retry(qc))
03faab78
TH
1981 qc->flags |= ATA_QCFLAG_RETRY;
1982
022bdb07 1983 /* accumulate error info */
4528e4da 1984 ehc->i.dev = qc->dev;
022bdb07
TH
1985 all_err_mask |= qc->err_mask;
1986 if (qc->flags & ATA_QCFLAG_IO)
3884f7b0 1987 eflags |= ATA_EFLAG_IS_IO;
255c03d1 1988 trace_ata_eh_link_autopsy_qc(qc);
7eb49509
DLM
1989
1990 /* Count quiet errors */
1991 if (ata_eh_quiet(qc))
1992 nr_quiet++;
1993 nr_failed++;
022bdb07
TH
1994 }
1995
7eb49509
DLM
1996 /* If all failed commands requested silence, then be quiet */
1997 if (nr_quiet == nr_failed)
1998 ehc->i.flags |= ATA_EHI_QUIET;
1999
a20f33ff 2000 /* enforce default EH actions */
b51e9e5d 2001 if (ap->pflags & ATA_PFLAG_FROZEN ||
a20f33ff 2002 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
cf480626 2003 ehc->i.action |= ATA_EH_RESET;
3884f7b0
TH
2004 else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) ||
2005 (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV)))
4528e4da 2006 ehc->i.action |= ATA_EH_REVALIDATE;
022bdb07 2007
dfcc173d
TH
2008 /* If we have offending qcs and the associated failed device,
2009 * perform per-dev EH action only on the offending device.
2010 */
4528e4da 2011 if (ehc->i.dev) {
4528e4da
TH
2012 ehc->i.dev_action[ehc->i.dev->devno] |=
2013 ehc->i.action & ATA_EH_PERDEV_MASK;
2014 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
47005f25
TH
2015 }
2016
2695e366
TH
2017 /* propagate timeout to host link */
2018 if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link))
2019 ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT;
2020
2021 /* record error and consider speeding down */
dfcc173d 2022 dev = ehc->i.dev;
2695e366
TH
2023 if (!dev && ((ata_link_max_devices(link) == 1 &&
2024 ata_dev_enabled(link->device))))
2025 dev = link->device;
dfcc173d 2026
76326ac1
TH
2027 if (dev) {
2028 if (dev->flags & ATA_DFLAG_DUBIOUS_XFER)
2029 eflags |= ATA_EFLAG_DUBIOUS_XFER;
3884f7b0 2030 ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask);
f1601113 2031 trace_ata_eh_link_autopsy(dev, ehc->i.action, all_err_mask);
76326ac1 2032 }
022bdb07
TH
2033}
2034
2035/**
9b1e2658
TH
2036 * ata_eh_autopsy - analyze error and determine recovery action
2037 * @ap: host port to perform autopsy on
2038 *
2039 * Analyze all links of @ap and determine why they failed and
2040 * which recovery actions are needed.
2041 *
2042 * LOCKING:
2043 * Kernel thread context (may sleep).
2044 */
fb7fd614 2045void ata_eh_autopsy(struct ata_port *ap)
9b1e2658
TH
2046{
2047 struct ata_link *link;
2048
1eca4365 2049 ata_for_each_link(link, ap, EDGE)
9b1e2658 2050 ata_eh_link_autopsy(link);
2695e366 2051
b1c72916
TH
2052 /* Handle the frigging slave link. Autopsy is done similarly
2053 * but actions and flags are transferred over to the master
2054 * link and handled from there.
2055 */
2056 if (ap->slave_link) {
2057 struct ata_eh_context *mehc = &ap->link.eh_context;
2058 struct ata_eh_context *sehc = &ap->slave_link->eh_context;
2059
848e4c68
TH
2060 /* transfer control flags from master to slave */
2061 sehc->i.flags |= mehc->i.flags & ATA_EHI_TO_SLAVE_MASK;
2062
2063 /* perform autopsy on the slave link */
b1c72916
TH
2064 ata_eh_link_autopsy(ap->slave_link);
2065
848e4c68 2066 /* transfer actions from slave to master and clear slave */
b1c72916
TH
2067 ata_eh_about_to_do(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
2068 mehc->i.action |= sehc->i.action;
2069 mehc->i.dev_action[1] |= sehc->i.dev_action[1];
2070 mehc->i.flags |= sehc->i.flags;
2071 ata_eh_done(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
2072 }
2073
2695e366
TH
2074 /* Autopsy of fanout ports can affect host link autopsy.
2075 * Perform host link autopsy last.
2076 */
071f44b1 2077 if (sata_pmp_attached(ap))
2695e366 2078 ata_eh_link_autopsy(&ap->link);
9b1e2658
TH
2079}
2080
6521148c 2081/**
d4520903
HR
2082 * ata_get_cmd_name - get name for ATA command
2083 * @command: ATA command code to get name for
6521148c 2084 *
d4520903 2085 * Return a textual name of the given command or "unknown"
6521148c
RH
2086 *
2087 * LOCKING:
2088 * None
2089 */
d4520903 2090const char *ata_get_cmd_name(u8 command)
6521148c
RH
2091{
2092#ifdef CONFIG_ATA_VERBOSE_ERROR
2093 static const struct
2094 {
2095 u8 command;
2096 const char *text;
2097 } cmd_descr[] = {
2098 { ATA_CMD_DEV_RESET, "DEVICE RESET" },
825e2d87
HR
2099 { ATA_CMD_CHK_POWER, "CHECK POWER MODE" },
2100 { ATA_CMD_STANDBY, "STANDBY" },
2101 { ATA_CMD_IDLE, "IDLE" },
2102 { ATA_CMD_EDD, "EXECUTE DEVICE DIAGNOSTIC" },
2103 { ATA_CMD_DOWNLOAD_MICRO, "DOWNLOAD MICROCODE" },
3915c3b5 2104 { ATA_CMD_DOWNLOAD_MICRO_DMA, "DOWNLOAD MICROCODE DMA" },
6521148c 2105 { ATA_CMD_NOP, "NOP" },
825e2d87
HR
2106 { ATA_CMD_FLUSH, "FLUSH CACHE" },
2107 { ATA_CMD_FLUSH_EXT, "FLUSH CACHE EXT" },
2108 { ATA_CMD_ID_ATA, "IDENTIFY DEVICE" },
2109 { ATA_CMD_ID_ATAPI, "IDENTIFY PACKET DEVICE" },
2110 { ATA_CMD_SERVICE, "SERVICE" },
2111 { ATA_CMD_READ, "READ DMA" },
2112 { ATA_CMD_READ_EXT, "READ DMA EXT" },
2113 { ATA_CMD_READ_QUEUED, "READ DMA QUEUED" },
2114 { ATA_CMD_READ_STREAM_EXT, "READ STREAM EXT" },
6521148c 2115 { ATA_CMD_READ_STREAM_DMA_EXT, "READ STREAM DMA EXT" },
825e2d87
HR
2116 { ATA_CMD_WRITE, "WRITE DMA" },
2117 { ATA_CMD_WRITE_EXT, "WRITE DMA EXT" },
2118 { ATA_CMD_WRITE_QUEUED, "WRITE DMA QUEUED EXT" },
2119 { ATA_CMD_WRITE_STREAM_EXT, "WRITE STREAM EXT" },
6521148c
RH
2120 { ATA_CMD_WRITE_STREAM_DMA_EXT, "WRITE STREAM DMA EXT" },
2121 { ATA_CMD_WRITE_FUA_EXT, "WRITE DMA FUA EXT" },
2122 { ATA_CMD_WRITE_QUEUED_FUA_EXT, "WRITE DMA QUEUED FUA EXT" },
2123 { ATA_CMD_FPDMA_READ, "READ FPDMA QUEUED" },
2124 { ATA_CMD_FPDMA_WRITE, "WRITE FPDMA QUEUED" },
d3122bf9 2125 { ATA_CMD_NCQ_NON_DATA, "NCQ NON-DATA" },
3915c3b5
RH
2126 { ATA_CMD_FPDMA_SEND, "SEND FPDMA QUEUED" },
2127 { ATA_CMD_FPDMA_RECV, "RECEIVE FPDMA QUEUED" },
6521148c
RH
2128 { ATA_CMD_PIO_READ, "READ SECTOR(S)" },
2129 { ATA_CMD_PIO_READ_EXT, "READ SECTOR(S) EXT" },
2130 { ATA_CMD_PIO_WRITE, "WRITE SECTOR(S)" },
2131 { ATA_CMD_PIO_WRITE_EXT, "WRITE SECTOR(S) EXT" },
2132 { ATA_CMD_READ_MULTI, "READ MULTIPLE" },
2133 { ATA_CMD_READ_MULTI_EXT, "READ MULTIPLE EXT" },
2134 { ATA_CMD_WRITE_MULTI, "WRITE MULTIPLE" },
2135 { ATA_CMD_WRITE_MULTI_EXT, "WRITE MULTIPLE EXT" },
825e2d87 2136 { ATA_CMD_WRITE_MULTI_FUA_EXT, "WRITE MULTIPLE FUA EXT" },
6521148c
RH
2137 { ATA_CMD_SET_FEATURES, "SET FEATURES" },
2138 { ATA_CMD_SET_MULTI, "SET MULTIPLE MODE" },
2139 { ATA_CMD_VERIFY, "READ VERIFY SECTOR(S)" },
2140 { ATA_CMD_VERIFY_EXT, "READ VERIFY SECTOR(S) EXT" },
2141 { ATA_CMD_WRITE_UNCORR_EXT, "WRITE UNCORRECTABLE EXT" },
2142 { ATA_CMD_STANDBYNOW1, "STANDBY IMMEDIATE" },
2143 { ATA_CMD_IDLEIMMEDIATE, "IDLE IMMEDIATE" },
2144 { ATA_CMD_SLEEP, "SLEEP" },
2145 { ATA_CMD_INIT_DEV_PARAMS, "INITIALIZE DEVICE PARAMETERS" },
2146 { ATA_CMD_READ_NATIVE_MAX, "READ NATIVE MAX ADDRESS" },
2147 { ATA_CMD_READ_NATIVE_MAX_EXT, "READ NATIVE MAX ADDRESS EXT" },
2148 { ATA_CMD_SET_MAX, "SET MAX ADDRESS" },
2149 { ATA_CMD_SET_MAX_EXT, "SET MAX ADDRESS EXT" },
2150 { ATA_CMD_READ_LOG_EXT, "READ LOG EXT" },
2151 { ATA_CMD_WRITE_LOG_EXT, "WRITE LOG EXT" },
2152 { ATA_CMD_READ_LOG_DMA_EXT, "READ LOG DMA EXT" },
825e2d87 2153 { ATA_CMD_WRITE_LOG_DMA_EXT, "WRITE LOG DMA EXT" },
3915c3b5 2154 { ATA_CMD_TRUSTED_NONDATA, "TRUSTED NON-DATA" },
6521148c 2155 { ATA_CMD_TRUSTED_RCV, "TRUSTED RECEIVE" },
825e2d87 2156 { ATA_CMD_TRUSTED_RCV_DMA, "TRUSTED RECEIVE DMA" },
6521148c 2157 { ATA_CMD_TRUSTED_SND, "TRUSTED SEND" },
825e2d87 2158 { ATA_CMD_TRUSTED_SND_DMA, "TRUSTED SEND DMA" },
6521148c 2159 { ATA_CMD_PMP_READ, "READ BUFFER" },
3915c3b5 2160 { ATA_CMD_PMP_READ_DMA, "READ BUFFER DMA" },
6521148c 2161 { ATA_CMD_PMP_WRITE, "WRITE BUFFER" },
3915c3b5 2162 { ATA_CMD_PMP_WRITE_DMA, "WRITE BUFFER DMA" },
6521148c
RH
2163 { ATA_CMD_CONF_OVERLAY, "DEVICE CONFIGURATION OVERLAY" },
2164 { ATA_CMD_SEC_SET_PASS, "SECURITY SET PASSWORD" },
2165 { ATA_CMD_SEC_UNLOCK, "SECURITY UNLOCK" },
2166 { ATA_CMD_SEC_ERASE_PREP, "SECURITY ERASE PREPARE" },
2167 { ATA_CMD_SEC_ERASE_UNIT, "SECURITY ERASE UNIT" },
2168 { ATA_CMD_SEC_FREEZE_LOCK, "SECURITY FREEZE LOCK" },
2169 { ATA_CMD_SEC_DISABLE_PASS, "SECURITY DISABLE PASSWORD" },
2170 { ATA_CMD_CONFIG_STREAM, "CONFIGURE STREAM" },
2171 { ATA_CMD_SMART, "SMART" },
2172 { ATA_CMD_MEDIA_LOCK, "DOOR LOCK" },
2173 { ATA_CMD_MEDIA_UNLOCK, "DOOR UNLOCK" },
acad7627 2174 { ATA_CMD_DSM, "DATA SET MANAGEMENT" },
825e2d87
HR
2175 { ATA_CMD_CHK_MED_CRD_TYP, "CHECK MEDIA CARD TYPE" },
2176 { ATA_CMD_CFA_REQ_EXT_ERR, "CFA REQUEST EXTENDED ERROR" },
6521148c
RH
2177 { ATA_CMD_CFA_WRITE_NE, "CFA WRITE SECTORS WITHOUT ERASE" },
2178 { ATA_CMD_CFA_TRANS_SECT, "CFA TRANSLATE SECTOR" },
2179 { ATA_CMD_CFA_ERASE, "CFA ERASE SECTORS" },
825e2d87 2180 { ATA_CMD_CFA_WRITE_MULT_NE, "CFA WRITE MULTIPLE WITHOUT ERASE" },
3915c3b5
RH
2181 { ATA_CMD_REQ_SENSE_DATA, "REQUEST SENSE DATA EXT" },
2182 { ATA_CMD_SANITIZE_DEVICE, "SANITIZE DEVICE" },
28a3fc22 2183 { ATA_CMD_ZAC_MGMT_IN, "ZAC MANAGEMENT IN" },
27708a95 2184 { ATA_CMD_ZAC_MGMT_OUT, "ZAC MANAGEMENT OUT" },
6521148c
RH
2185 { ATA_CMD_READ_LONG, "READ LONG (with retries)" },
2186 { ATA_CMD_READ_LONG_ONCE, "READ LONG (without retries)" },
2187 { ATA_CMD_WRITE_LONG, "WRITE LONG (with retries)" },
2188 { ATA_CMD_WRITE_LONG_ONCE, "WRITE LONG (without retries)" },
2189 { ATA_CMD_RESTORE, "RECALIBRATE" },
2190 { 0, NULL } /* terminate list */
2191 };
2192
2193 unsigned int i;
2194 for (i = 0; cmd_descr[i].text; i++)
2195 if (cmd_descr[i].command == command)
2196 return cmd_descr[i].text;
2197#endif
2198
d4520903 2199 return "unknown";
6521148c 2200}
d4520903 2201EXPORT_SYMBOL_GPL(ata_get_cmd_name);
6521148c 2202
9b1e2658
TH
2203/**
2204 * ata_eh_link_report - report error handling to user
0260731f 2205 * @link: ATA link EH is going on
022bdb07
TH
2206 *
2207 * Report EH to user.
2208 *
2209 * LOCKING:
2210 * None.
2211 */
9b1e2658 2212static void ata_eh_link_report(struct ata_link *link)
022bdb07 2213{
0260731f
TH
2214 struct ata_port *ap = link->ap;
2215 struct ata_eh_context *ehc = &link->eh_context;
258c4e5c 2216 struct ata_queued_cmd *qc;
022bdb07 2217 const char *frozen, *desc;
462098b0 2218 char tries_buf[6] = "";
022bdb07
TH
2219 int tag, nr_failed = 0;
2220
94ff3d54
TH
2221 if (ehc->i.flags & ATA_EHI_QUIET)
2222 return;
2223
022bdb07
TH
2224 desc = NULL;
2225 if (ehc->i.desc[0] != '\0')
2226 desc = ehc->i.desc;
2227
258c4e5c 2228 ata_qc_for_each_raw(ap, qc, tag) {
b1c72916
TH
2229 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
2230 ata_dev_phys_link(qc->dev) != link ||
e027bd36
TH
2231 ((qc->flags & ATA_QCFLAG_QUIET) &&
2232 qc->err_mask == AC_ERR_DEV))
022bdb07
TH
2233 continue;
2234 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
2235 continue;
2236
2237 nr_failed++;
2238 }
2239
2240 if (!nr_failed && !ehc->i.err_mask)
2241 return;
2242
2243 frozen = "";
b51e9e5d 2244 if (ap->pflags & ATA_PFLAG_FROZEN)
022bdb07
TH
2245 frozen = " frozen";
2246
a1e10f7e 2247 if (ap->eh_tries < ATA_EH_MAX_TRIES)
462098b0 2248 snprintf(tries_buf, sizeof(tries_buf), " t%d",
a1e10f7e
TH
2249 ap->eh_tries);
2250
022bdb07 2251 if (ehc->i.dev) {
a9a79dfe
JP
2252 ata_dev_err(ehc->i.dev, "exception Emask 0x%x "
2253 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2254 ehc->i.err_mask, link->sactive, ehc->i.serror,
2255 ehc->i.action, frozen, tries_buf);
022bdb07 2256 if (desc)
a9a79dfe 2257 ata_dev_err(ehc->i.dev, "%s\n", desc);
022bdb07 2258 } else {
a9a79dfe
JP
2259 ata_link_err(link, "exception Emask 0x%x "
2260 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2261 ehc->i.err_mask, link->sactive, ehc->i.serror,
2262 ehc->i.action, frozen, tries_buf);
022bdb07 2263 if (desc)
a9a79dfe 2264 ata_link_err(link, "%s\n", desc);
022bdb07
TH
2265 }
2266
6521148c 2267#ifdef CONFIG_ATA_VERBOSE_ERROR
1333e194 2268 if (ehc->i.serror)
a9a79dfe 2269 ata_link_err(link,
1333e194
RH
2270 "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
2271 ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
2272 ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
2273 ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
2274 ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
2275 ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
2276 ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
2277 ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
2278 ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
2279 ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
2280 ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
2281 ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
2282 ehc->i.serror & SERR_CRC ? "BadCRC " : "",
2283 ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
2284 ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
2285 ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
2286 ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
2dcb407e 2287 ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
6521148c 2288#endif
1333e194 2289
258c4e5c 2290 ata_qc_for_each_raw(ap, qc, tag) {
8a937581 2291 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
abb6a889
TH
2292 char data_buf[20] = "";
2293 char cdb_buf[70] = "";
022bdb07 2294
0260731f 2295 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
b1c72916 2296 ata_dev_phys_link(qc->dev) != link || !qc->err_mask)
022bdb07
TH
2297 continue;
2298
abb6a889
TH
2299 if (qc->dma_dir != DMA_NONE) {
2300 static const char *dma_str[] = {
2301 [DMA_BIDIRECTIONAL] = "bidi",
2302 [DMA_TO_DEVICE] = "out",
2303 [DMA_FROM_DEVICE] = "in",
2304 };
fb1b8b11
GU
2305 const char *prot_str = NULL;
2306
2307 switch (qc->tf.protocol) {
2308 case ATA_PROT_UNKNOWN:
2309 prot_str = "unknown";
2310 break;
2311 case ATA_PROT_NODATA:
2312 prot_str = "nodata";
2313 break;
2314 case ATA_PROT_PIO:
2315 prot_str = "pio";
2316 break;
2317 case ATA_PROT_DMA:
2318 prot_str = "dma";
2319 break;
2320 case ATA_PROT_NCQ:
2321 prot_str = "ncq dma";
2322 break;
2323 case ATA_PROT_NCQ_NODATA:
2324 prot_str = "ncq nodata";
2325 break;
2326 case ATAPI_PROT_NODATA:
2327 prot_str = "nodata";
2328 break;
2329 case ATAPI_PROT_PIO:
2330 prot_str = "pio";
2331 break;
2332 case ATAPI_PROT_DMA:
2333 prot_str = "dma";
2334 break;
2335 }
abb6a889 2336 snprintf(data_buf, sizeof(data_buf), " %s %u %s",
fb1b8b11 2337 prot_str, qc->nbytes, dma_str[qc->dma_dir]);
abb6a889
TH
2338 }
2339
6521148c 2340 if (ata_is_atapi(qc->tf.protocol)) {
a13b0c9d
HR
2341 const u8 *cdb = qc->cdb;
2342 size_t cdb_len = qc->dev->cdb_len;
2343
cbba5b0e
HR
2344 if (qc->scsicmd) {
2345 cdb = qc->scsicmd->cmnd;
2346 cdb_len = qc->scsicmd->cmd_len;
2347 }
2348 __scsi_format_command(cdb_buf, sizeof(cdb_buf),
2349 cdb, cdb_len);
d4520903
HR
2350 } else
2351 ata_dev_err(qc->dev, "failed command: %s\n",
2352 ata_get_cmd_name(cmd->command));
abb6a889 2353
a9a79dfe 2354 ata_dev_err(qc->dev,
8a937581 2355 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
abb6a889 2356 "tag %d%s\n %s"
8a937581 2357 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
5335b729 2358 "Emask 0x%x (%s)%s\n",
8a937581
TH
2359 cmd->command, cmd->feature, cmd->nsect,
2360 cmd->lbal, cmd->lbam, cmd->lbah,
2361 cmd->hob_feature, cmd->hob_nsect,
2362 cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
abb6a889 2363 cmd->device, qc->tag, data_buf, cdb_buf,
efcef265 2364 res->status, res->error, res->nsect,
8a937581
TH
2365 res->lbal, res->lbam, res->lbah,
2366 res->hob_feature, res->hob_nsect,
2367 res->hob_lbal, res->hob_lbam, res->hob_lbah,
5335b729
TH
2368 res->device, qc->err_mask, ata_err_string(qc->err_mask),
2369 qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
1333e194 2370
6521148c 2371#ifdef CONFIG_ATA_VERBOSE_ERROR
efcef265
SS
2372 if (res->status & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
2373 ATA_SENSE | ATA_ERR)) {
2374 if (res->status & ATA_BUSY)
a9a79dfe 2375 ata_dev_err(qc->dev, "status: { Busy }\n");
1333e194 2376 else
e87fd28c 2377 ata_dev_err(qc->dev, "status: { %s%s%s%s%s}\n",
efcef265
SS
2378 res->status & ATA_DRDY ? "DRDY " : "",
2379 res->status & ATA_DF ? "DF " : "",
2380 res->status & ATA_DRQ ? "DRQ " : "",
2381 res->status & ATA_SENSE ? "SENSE " : "",
2382 res->status & ATA_ERR ? "ERR " : "");
1333e194
RH
2383 }
2384
2385 if (cmd->command != ATA_CMD_PACKET &&
efcef265
SS
2386 (res->error & (ATA_ICRC | ATA_UNC | ATA_AMNF | ATA_IDNF |
2387 ATA_ABORTED)))
eec7e1c1 2388 ata_dev_err(qc->dev, "error: { %s%s%s%s%s}\n",
efcef265
SS
2389 res->error & ATA_ICRC ? "ICRC " : "",
2390 res->error & ATA_UNC ? "UNC " : "",
2391 res->error & ATA_AMNF ? "AMNF " : "",
2392 res->error & ATA_IDNF ? "IDNF " : "",
2393 res->error & ATA_ABORTED ? "ABRT " : "");
6521148c 2394#endif
022bdb07
TH
2395 }
2396}
2397
9b1e2658
TH
2398/**
2399 * ata_eh_report - report error handling to user
2400 * @ap: ATA port to report EH about
2401 *
2402 * Report EH to user.
2403 *
2404 * LOCKING:
2405 * None.
2406 */
fb7fd614 2407void ata_eh_report(struct ata_port *ap)
9b1e2658
TH
2408{
2409 struct ata_link *link;
2410
1eca4365 2411 ata_for_each_link(link, ap, HOST_FIRST)
9b1e2658
TH
2412 ata_eh_link_report(link);
2413}
2414
cc0680a5 2415static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
b1c72916
TH
2416 unsigned int *classes, unsigned long deadline,
2417 bool clear_classes)
d87fa38e 2418{
f58229f8 2419 struct ata_device *dev;
d87fa38e 2420
b1c72916 2421 if (clear_classes)
1eca4365 2422 ata_for_each_dev(dev, link, ALL)
b1c72916 2423 classes[dev->devno] = ATA_DEV_UNKNOWN;
d87fa38e 2424
f046519f 2425 return reset(link, classes, deadline);
d87fa38e
TH
2426}
2427
e8411fba 2428static int ata_eh_followup_srst_needed(struct ata_link *link, int rc)
664faf09 2429{
45db2f6c 2430 if ((link->flags & ATA_LFLAG_NO_SRST) || ata_link_offline(link))
ae791c05 2431 return 0;
5dbfc9cb
TH
2432 if (rc == -EAGAIN)
2433 return 1;
071f44b1 2434 if (sata_pmp_supported(link->ap) && ata_is_host_link(link))
3495de73 2435 return 1;
664faf09
TH
2436 return 0;
2437}
2438
fb7fd614
TH
2439int ata_eh_reset(struct ata_link *link, int classify,
2440 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
2441 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
022bdb07 2442{
afaa5c37 2443 struct ata_port *ap = link->ap;
b1c72916 2444 struct ata_link *slave = ap->slave_link;
936fd732 2445 struct ata_eh_context *ehc = &link->eh_context;
705d2014 2446 struct ata_eh_context *sehc = slave ? &slave->eh_context : NULL;
664faf09 2447 unsigned int *classes = ehc->classes;
416dc9ed 2448 unsigned int lflags = link->flags;
1cdaf534 2449 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
d8af0eb6 2450 int max_tries = 0, try = 0;
b1c72916 2451 struct ata_link *failed_link;
f58229f8 2452 struct ata_device *dev;
416dc9ed 2453 unsigned long deadline, now;
022bdb07 2454 ata_reset_fn_t reset;
afaa5c37 2455 unsigned long flags;
416dc9ed 2456 u32 sstatus;
b1c72916 2457 int nr_unknown, rc;
022bdb07 2458
932648b0
TH
2459 /*
2460 * Prepare to reset
2461 */
d8af0eb6
TH
2462 while (ata_eh_reset_timeouts[max_tries] != ULONG_MAX)
2463 max_tries++;
ca6d43b0
DW
2464 if (link->flags & ATA_LFLAG_RST_ONCE)
2465 max_tries = 1;
05944bdf
TH
2466 if (link->flags & ATA_LFLAG_NO_HRST)
2467 hardreset = NULL;
2468 if (link->flags & ATA_LFLAG_NO_SRST)
2469 softreset = NULL;
d8af0eb6 2470
25985edc 2471 /* make sure each reset attempt is at least COOL_DOWN apart */
19b72321
TH
2472 if (ehc->i.flags & ATA_EHI_DID_RESET) {
2473 now = jiffies;
2474 WARN_ON(time_after(ehc->last_reset, now));
2475 deadline = ata_deadline(ehc->last_reset,
2476 ATA_EH_RESET_COOL_DOWN);
2477 if (time_before(now, deadline))
2478 schedule_timeout_uninterruptible(deadline - now);
2479 }
0a2c0f56 2480
afaa5c37
TH
2481 spin_lock_irqsave(ap->lock, flags);
2482 ap->pflags |= ATA_PFLAG_RESETTING;
2483 spin_unlock_irqrestore(ap->lock, flags);
2484
cf480626 2485 ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
13abf50d 2486
1eca4365 2487 ata_for_each_dev(dev, link, ALL) {
cdeab114
TH
2488 /* If we issue an SRST then an ATA drive (not ATAPI)
2489 * may change configuration and be in PIO0 timing. If
2490 * we do a hard reset (or are coming from power on)
2491 * this is true for ATA or ATAPI. Until we've set a
2492 * suitable controller mode we should not touch the
2493 * bus as we may be talking too fast.
2494 */
2495 dev->pio_mode = XFER_PIO_0;
5416912a 2496 dev->dma_mode = 0xff;
cdeab114
TH
2497
2498 /* If the controller has a pio mode setup function
2499 * then use it to set the chipset to rights. Don't
2500 * touch the DMA setup as that will be dealt with when
2501 * configuring devices.
2502 */
2503 if (ap->ops->set_piomode)
2504 ap->ops->set_piomode(ap, dev);
2505 }
2506
cf480626 2507 /* prefer hardreset */
932648b0 2508 reset = NULL;
cf480626
TH
2509 ehc->i.action &= ~ATA_EH_RESET;
2510 if (hardreset) {
2511 reset = hardreset;
a674050e 2512 ehc->i.action |= ATA_EH_HARDRESET;
4f7faa3f 2513 } else if (softreset) {
cf480626 2514 reset = softreset;
a674050e 2515 ehc->i.action |= ATA_EH_SOFTRESET;
cf480626 2516 }
f5914a46
TH
2517
2518 if (prereset) {
b1c72916
TH
2519 unsigned long deadline = ata_deadline(jiffies,
2520 ATA_EH_PRERESET_TIMEOUT);
2521
2522 if (slave) {
2523 sehc->i.action &= ~ATA_EH_RESET;
2524 sehc->i.action |= ehc->i.action;
2525 }
2526
2527 rc = prereset(link, deadline);
2528
2529 /* If present, do prereset on slave link too. Reset
2530 * is skipped iff both master and slave links report
2531 * -ENOENT or clear ATA_EH_RESET.
2532 */
2533 if (slave && (rc == 0 || rc == -ENOENT)) {
2534 int tmp;
2535
2536 tmp = prereset(slave, deadline);
2537 if (tmp != -ENOENT)
2538 rc = tmp;
2539
2540 ehc->i.action |= sehc->i.action;
2541 }
2542
f5914a46 2543 if (rc) {
c961922b 2544 if (rc == -ENOENT) {
a9a79dfe 2545 ata_link_dbg(link, "port disabled--ignoring\n");
cf480626 2546 ehc->i.action &= ~ATA_EH_RESET;
4aa9ab67 2547
1eca4365 2548 ata_for_each_dev(dev, link, ALL)
f58229f8 2549 classes[dev->devno] = ATA_DEV_NONE;
4aa9ab67
TH
2550
2551 rc = 0;
c961922b 2552 } else
a9a79dfe
JP
2553 ata_link_err(link,
2554 "prereset failed (errno=%d)\n",
2555 rc);
fccb6ea5 2556 goto out;
f5914a46 2557 }
f5914a46 2558
932648b0 2559 /* prereset() might have cleared ATA_EH_RESET. If so,
d6515e6f 2560 * bang classes, thaw and return.
932648b0
TH
2561 */
2562 if (reset && !(ehc->i.action & ATA_EH_RESET)) {
1eca4365 2563 ata_for_each_dev(dev, link, ALL)
932648b0 2564 classes[dev->devno] = ATA_DEV_NONE;
d6515e6f
TH
2565 if ((ap->pflags & ATA_PFLAG_FROZEN) &&
2566 ata_is_host_link(link))
2567 ata_eh_thaw_port(ap);
932648b0
TH
2568 rc = 0;
2569 goto out;
2570 }
f5914a46
TH
2571 }
2572
022bdb07 2573 retry:
932648b0
TH
2574 /*
2575 * Perform reset
2576 */
dc98c32c
TH
2577 if (ata_is_host_link(link))
2578 ata_eh_freeze_port(ap);
2579
341c2c95 2580 deadline = ata_deadline(jiffies, ata_eh_reset_timeouts[try++]);
31daabda 2581
932648b0
TH
2582 if (reset) {
2583 if (verbose)
a9a79dfe
JP
2584 ata_link_info(link, "%s resetting link\n",
2585 reset == softreset ? "soft" : "hard");
932648b0
TH
2586
2587 /* mark that this EH session started with reset */
19b72321 2588 ehc->last_reset = jiffies;
f8ec26d0 2589 if (reset == hardreset) {
932648b0 2590 ehc->i.flags |= ATA_EHI_DID_HARDRESET;
f8ec26d0
HR
2591 trace_ata_link_hardreset_begin(link, classes, deadline);
2592 } else {
932648b0 2593 ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
f8ec26d0
HR
2594 trace_ata_link_softreset_begin(link, classes, deadline);
2595 }
022bdb07 2596
b1c72916 2597 rc = ata_do_reset(link, reset, classes, deadline, true);
f8ec26d0
HR
2598 if (reset == hardreset)
2599 trace_ata_link_hardreset_end(link, classes, rc);
2600 else
2601 trace_ata_link_softreset_end(link, classes, rc);
b1c72916
TH
2602 if (rc && rc != -EAGAIN) {
2603 failed_link = link;
5dbfc9cb 2604 goto fail;
b1c72916
TH
2605 }
2606
2607 /* hardreset slave link if existent */
2608 if (slave && reset == hardreset) {
2609 int tmp;
2610
2611 if (verbose)
a9a79dfe 2612 ata_link_info(slave, "hard resetting link\n");
b1c72916
TH
2613
2614 ata_eh_about_to_do(slave, NULL, ATA_EH_RESET);
f8ec26d0
HR
2615 trace_ata_slave_hardreset_begin(slave, classes,
2616 deadline);
b1c72916
TH
2617 tmp = ata_do_reset(slave, reset, classes, deadline,
2618 false);
f8ec26d0 2619 trace_ata_slave_hardreset_end(slave, classes, tmp);
b1c72916
TH
2620 switch (tmp) {
2621 case -EAGAIN:
2622 rc = -EAGAIN;
e06abcc6 2623 break;
b1c72916
TH
2624 case 0:
2625 break;
2626 default:
2627 failed_link = slave;
2628 rc = tmp;
2629 goto fail;
2630 }
2631 }
022bdb07 2632
b1c72916 2633 /* perform follow-up SRST if necessary */
932648b0 2634 if (reset == hardreset &&
e8411fba 2635 ata_eh_followup_srst_needed(link, rc)) {
932648b0 2636 reset = softreset;
022bdb07 2637
932648b0 2638 if (!reset) {
a9a79dfe
JP
2639 ata_link_err(link,
2640 "follow-up softreset required but no softreset available\n");
b1c72916 2641 failed_link = link;
932648b0
TH
2642 rc = -EINVAL;
2643 goto fail;
2644 }
664faf09 2645
932648b0 2646 ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
f8ec26d0 2647 trace_ata_link_softreset_begin(link, classes, deadline);
b1c72916 2648 rc = ata_do_reset(link, reset, classes, deadline, true);
f8ec26d0 2649 trace_ata_link_softreset_end(link, classes, rc);
fe2c4d01
TH
2650 if (rc) {
2651 failed_link = link;
2652 goto fail;
2653 }
664faf09 2654 }
932648b0
TH
2655 } else {
2656 if (verbose)
a9a79dfe
JP
2657 ata_link_info(link,
2658 "no reset method available, skipping reset\n");
932648b0
TH
2659 if (!(lflags & ATA_LFLAG_ASSUME_CLASS))
2660 lflags |= ATA_LFLAG_ASSUME_ATA;
664faf09
TH
2661 }
2662
932648b0
TH
2663 /*
2664 * Post-reset processing
2665 */
1eca4365 2666 ata_for_each_dev(dev, link, ALL) {
416dc9ed
TH
2667 /* After the reset, the device state is PIO 0 and the
2668 * controller state is undefined. Reset also wakes up
2669 * drives from sleeping mode.
2670 */
2671 dev->pio_mode = XFER_PIO_0;
2672 dev->flags &= ~ATA_DFLAG_SLEEPING;
31daabda 2673
3b761d3d
TH
2674 if (ata_phys_link_offline(ata_dev_phys_link(dev)))
2675 continue;
2676
2677 /* apply class override */
2678 if (lflags & ATA_LFLAG_ASSUME_ATA)
2679 classes[dev->devno] = ATA_DEV_ATA;
2680 else if (lflags & ATA_LFLAG_ASSUME_SEMB)
2681 classes[dev->devno] = ATA_DEV_SEMB_UNSUP;
022bdb07
TH
2682 }
2683
416dc9ed
TH
2684 /* record current link speed */
2685 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2686 link->sata_spd = (sstatus >> 4) & 0xf;
b1c72916
TH
2687 if (slave && sata_scr_read(slave, SCR_STATUS, &sstatus) == 0)
2688 slave->sata_spd = (sstatus >> 4) & 0xf;
008a7896 2689
dc98c32c
TH
2690 /* thaw the port */
2691 if (ata_is_host_link(link))
2692 ata_eh_thaw_port(ap);
2693
f046519f
TH
2694 /* postreset() should clear hardware SError. Although SError
2695 * is cleared during link resume, clearing SError here is
2696 * necessary as some PHYs raise hotplug events after SRST.
2697 * This introduces race condition where hotplug occurs between
2698 * reset and here. This race is mediated by cross checking
2699 * link onlineness and classification result later.
2700 */
b1c72916 2701 if (postreset) {
416dc9ed 2702 postreset(link, classes);
f8ec26d0
HR
2703 trace_ata_link_postreset(link, classes, rc);
2704 if (slave) {
b1c72916 2705 postreset(slave, classes);
f8ec26d0
HR
2706 trace_ata_slave_postreset(slave, classes, rc);
2707 }
b1c72916 2708 }
20952b69 2709
1e641060 2710 /*
8c56cacc
TH
2711 * Some controllers can't be frozen very well and may set spurious
2712 * error conditions during reset. Clear accumulated error
2713 * information and re-thaw the port if frozen. As reset is the
2714 * final recovery action and we cross check link onlineness against
2715 * device classification later, no hotplug event is lost by this.
1e641060 2716 */
f046519f 2717 spin_lock_irqsave(link->ap->lock, flags);
1e641060 2718 memset(&link->eh_info, 0, sizeof(link->eh_info));
b1c72916 2719 if (slave)
1e641060
TH
2720 memset(&slave->eh_info, 0, sizeof(link->eh_info));
2721 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
f046519f
TH
2722 spin_unlock_irqrestore(link->ap->lock, flags);
2723
8c56cacc
TH
2724 if (ap->pflags & ATA_PFLAG_FROZEN)
2725 ata_eh_thaw_port(ap);
2726
3b761d3d
TH
2727 /*
2728 * Make sure onlineness and classification result correspond.
f046519f
TH
2729 * Hotplug could have happened during reset and some
2730 * controllers fail to wait while a drive is spinning up after
2731 * being hotplugged causing misdetection. By cross checking
3b761d3d
TH
2732 * link on/offlineness and classification result, those
2733 * conditions can be reliably detected and retried.
f046519f 2734 */
b1c72916 2735 nr_unknown = 0;
1eca4365 2736 ata_for_each_dev(dev, link, ALL) {
3b761d3d
TH
2737 if (ata_phys_link_online(ata_dev_phys_link(dev))) {
2738 if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
a9a79dfe 2739 ata_dev_dbg(dev, "link online but device misclassified\n");
3b761d3d 2740 classes[dev->devno] = ATA_DEV_NONE;
b1c72916 2741 nr_unknown++;
3b761d3d
TH
2742 }
2743 } else if (ata_phys_link_offline(ata_dev_phys_link(dev))) {
2744 if (ata_class_enabled(classes[dev->devno]))
a9a79dfe
JP
2745 ata_dev_dbg(dev,
2746 "link offline, clearing class %d to NONE\n",
2747 classes[dev->devno]);
3b761d3d
TH
2748 classes[dev->devno] = ATA_DEV_NONE;
2749 } else if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
a9a79dfe
JP
2750 ata_dev_dbg(dev,
2751 "link status unknown, clearing UNKNOWN to NONE\n");
3b761d3d 2752 classes[dev->devno] = ATA_DEV_NONE;
b1c72916 2753 }
f046519f
TH
2754 }
2755
b1c72916 2756 if (classify && nr_unknown) {
f046519f 2757 if (try < max_tries) {
a9a79dfe
JP
2758 ata_link_warn(link,
2759 "link online but %d devices misclassified, retrying\n",
2760 nr_unknown);
b1c72916 2761 failed_link = link;
f046519f
TH
2762 rc = -EAGAIN;
2763 goto fail;
2764 }
a9a79dfe
JP
2765 ata_link_warn(link,
2766 "link online but %d devices misclassified, "
2767 "device detection might fail\n", nr_unknown);
f046519f
TH
2768 }
2769
416dc9ed 2770 /* reset successful, schedule revalidation */
cf480626 2771 ata_eh_done(link, NULL, ATA_EH_RESET);
b1c72916
TH
2772 if (slave)
2773 ata_eh_done(slave, NULL, ATA_EH_RESET);
6b7ae954 2774 ehc->last_reset = jiffies; /* update to completion time */
416dc9ed 2775 ehc->i.action |= ATA_EH_REVALIDATE;
6b7ae954 2776 link->lpm_policy = ATA_LPM_UNKNOWN; /* reset LPM state */
ae791c05 2777
416dc9ed 2778 rc = 0;
fccb6ea5
TH
2779 out:
2780 /* clear hotplug flag */
2781 ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
b1c72916
TH
2782 if (slave)
2783 sehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
afaa5c37
TH
2784
2785 spin_lock_irqsave(ap->lock, flags);
2786 ap->pflags &= ~ATA_PFLAG_RESETTING;
2787 spin_unlock_irqrestore(ap->lock, flags);
2788
022bdb07 2789 return rc;
416dc9ed
TH
2790
2791 fail:
5958e302
TH
2792 /* if SCR isn't accessible on a fan-out port, PMP needs to be reset */
2793 if (!ata_is_host_link(link) &&
2794 sata_scr_read(link, SCR_STATUS, &sstatus))
2795 rc = -ERESTART;
2796
7a46c078 2797 if (try >= max_tries) {
8ea7645c
TH
2798 /*
2799 * Thaw host port even if reset failed, so that the port
2800 * can be retried on the next phy event. This risks
2801 * repeated EH runs but seems to be a better tradeoff than
2802 * shutting down a port after a botched hotplug attempt.
2803 */
2804 if (ata_is_host_link(link))
2805 ata_eh_thaw_port(ap);
416dc9ed 2806 goto out;
8ea7645c 2807 }
416dc9ed
TH
2808
2809 now = jiffies;
2810 if (time_before(now, deadline)) {
2811 unsigned long delta = deadline - now;
2812
a9a79dfe 2813 ata_link_warn(failed_link,
0a2c0f56
TH
2814 "reset failed (errno=%d), retrying in %u secs\n",
2815 rc, DIV_ROUND_UP(jiffies_to_msecs(delta), 1000));
416dc9ed 2816
c0c362b6 2817 ata_eh_release(ap);
416dc9ed
TH
2818 while (delta)
2819 delta = schedule_timeout_uninterruptible(delta);
c0c362b6 2820 ata_eh_acquire(ap);
416dc9ed
TH
2821 }
2822
7a46c078
GG
2823 /*
2824 * While disks spinup behind PMP, some controllers fail sending SRST.
2825 * They need to be reset - as well as the PMP - before retrying.
2826 */
2827 if (rc == -ERESTART) {
2828 if (ata_is_host_link(link))
2829 ata_eh_thaw_port(ap);
2830 goto out;
2831 }
2832
b1c72916 2833 if (try == max_tries - 1) {
a07d499b 2834 sata_down_spd_limit(link, 0);
b1c72916 2835 if (slave)
a07d499b 2836 sata_down_spd_limit(slave, 0);
b1c72916 2837 } else if (rc == -EPIPE)
a07d499b 2838 sata_down_spd_limit(failed_link, 0);
b1c72916 2839
416dc9ed
TH
2840 if (hardreset)
2841 reset = hardreset;
2842 goto retry;
022bdb07
TH
2843}
2844
45fabbb7
EO
2845static inline void ata_eh_pull_park_action(struct ata_port *ap)
2846{
2847 struct ata_link *link;
2848 struct ata_device *dev;
2849 unsigned long flags;
2850
2851 /*
2852 * This function can be thought of as an extended version of
2853 * ata_eh_about_to_do() specially crafted to accommodate the
2854 * requirements of ATA_EH_PARK handling. Since the EH thread
2855 * does not leave the do {} while () loop in ata_eh_recover as
2856 * long as the timeout for a park request to *one* device on
2857 * the port has not expired, and since we still want to pick
2858 * up park requests to other devices on the same port or
2859 * timeout updates for the same device, we have to pull
2860 * ATA_EH_PARK actions from eh_info into eh_context.i
2861 * ourselves at the beginning of each pass over the loop.
2862 *
2863 * Additionally, all write accesses to &ap->park_req_pending
16735d02 2864 * through reinit_completion() (see below) or complete_all()
45fabbb7
EO
2865 * (see ata_scsi_park_store()) are protected by the host lock.
2866 * As a result we have that park_req_pending.done is zero on
2867 * exit from this function, i.e. when ATA_EH_PARK actions for
2868 * *all* devices on port ap have been pulled into the
2869 * respective eh_context structs. If, and only if,
2870 * park_req_pending.done is non-zero by the time we reach
2871 * wait_for_completion_timeout(), another ATA_EH_PARK action
2872 * has been scheduled for at least one of the devices on port
2873 * ap and we have to cycle over the do {} while () loop in
2874 * ata_eh_recover() again.
2875 */
2876
2877 spin_lock_irqsave(ap->lock, flags);
16735d02 2878 reinit_completion(&ap->park_req_pending);
1eca4365
TH
2879 ata_for_each_link(link, ap, EDGE) {
2880 ata_for_each_dev(dev, link, ALL) {
45fabbb7
EO
2881 struct ata_eh_info *ehi = &link->eh_info;
2882
2883 link->eh_context.i.dev_action[dev->devno] |=
2884 ehi->dev_action[dev->devno] & ATA_EH_PARK;
2885 ata_eh_clear_action(link, dev, ehi, ATA_EH_PARK);
2886 }
2887 }
2888 spin_unlock_irqrestore(ap->lock, flags);
2889}
2890
2891static void ata_eh_park_issue_cmd(struct ata_device *dev, int park)
2892{
2893 struct ata_eh_context *ehc = &dev->link->eh_context;
2894 struct ata_taskfile tf;
2895 unsigned int err_mask;
2896
2897 ata_tf_init(dev, &tf);
2898 if (park) {
2899 ehc->unloaded_mask |= 1 << dev->devno;
2900 tf.command = ATA_CMD_IDLEIMMEDIATE;
2901 tf.feature = 0x44;
2902 tf.lbal = 0x4c;
2903 tf.lbam = 0x4e;
2904 tf.lbah = 0x55;
2905 } else {
2906 ehc->unloaded_mask &= ~(1 << dev->devno);
2907 tf.command = ATA_CMD_CHK_POWER;
2908 }
2909
2910 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
bd18bc04 2911 tf.protocol = ATA_PROT_NODATA;
45fabbb7
EO
2912 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
2913 if (park && (err_mask || tf.lbal != 0xc4)) {
a9a79dfe 2914 ata_dev_err(dev, "head unload failed!\n");
45fabbb7
EO
2915 ehc->unloaded_mask &= ~(1 << dev->devno);
2916 }
2917}
2918
0260731f 2919static int ata_eh_revalidate_and_attach(struct ata_link *link,
084fe639 2920 struct ata_device **r_failed_dev)
022bdb07 2921{
0260731f
TH
2922 struct ata_port *ap = link->ap;
2923 struct ata_eh_context *ehc = &link->eh_context;
022bdb07 2924 struct ata_device *dev;
8c3c52a8 2925 unsigned int new_mask = 0;
084fe639 2926 unsigned long flags;
f58229f8 2927 int rc = 0;
022bdb07 2928
8c3c52a8
TH
2929 /* For PATA drive side cable detection to work, IDENTIFY must
2930 * be done backwards such that PDIAG- is released by the slave
2931 * device before the master device is identified.
2932 */
1eca4365 2933 ata_for_each_dev(dev, link, ALL_REVERSE) {
f58229f8
TH
2934 unsigned int action = ata_eh_dev_action(dev);
2935 unsigned int readid_flags = 0;
022bdb07 2936
bff04647
TH
2937 if (ehc->i.flags & ATA_EHI_DID_RESET)
2938 readid_flags |= ATA_READID_POSTRESET;
2939
9666f400 2940 if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
633273a3
TH
2941 WARN_ON(dev->class == ATA_DEV_PMP);
2942
b1c72916 2943 if (ata_phys_link_offline(ata_dev_phys_link(dev))) {
022bdb07 2944 rc = -EIO;
8c3c52a8 2945 goto err;
022bdb07
TH
2946 }
2947
0260731f 2948 ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
422c9daa
TH
2949 rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
2950 readid_flags);
022bdb07 2951 if (rc)
8c3c52a8 2952 goto err;
022bdb07 2953
0260731f 2954 ata_eh_done(link, dev, ATA_EH_REVALIDATE);
47005f25 2955
baa1e78a
TH
2956 /* Configuration may have changed, reconfigure
2957 * transfer mode.
2958 */
2959 ehc->i.flags |= ATA_EHI_SETMODE;
2960
3057ac3c 2961 /* schedule the scsi_rescan_device() here */
ad72cf98 2962 schedule_work(&(ap->scsi_rescan_task));
084fe639
TH
2963 } else if (dev->class == ATA_DEV_UNKNOWN &&
2964 ehc->tries[dev->devno] &&
2965 ata_class_enabled(ehc->classes[dev->devno])) {
842faa6c
TH
2966 /* Temporarily set dev->class, it will be
2967 * permanently set once all configurations are
2968 * complete. This is necessary because new
2969 * device configuration is done in two
2970 * separate loops.
2971 */
084fe639
TH
2972 dev->class = ehc->classes[dev->devno];
2973
633273a3
TH
2974 if (dev->class == ATA_DEV_PMP)
2975 rc = sata_pmp_attach(dev);
2976 else
2977 rc = ata_dev_read_id(dev, &dev->class,
2978 readid_flags, dev->id);
842faa6c
TH
2979
2980 /* read_id might have changed class, store and reset */
2981 ehc->classes[dev->devno] = dev->class;
2982 dev->class = ATA_DEV_UNKNOWN;
2983
8c3c52a8
TH
2984 switch (rc) {
2985 case 0:
99cf610a
TH
2986 /* clear error info accumulated during probe */
2987 ata_ering_clear(&dev->ering);
f58229f8 2988 new_mask |= 1 << dev->devno;
8c3c52a8
TH
2989 break;
2990 case -ENOENT:
55a8e2c8
TH
2991 /* IDENTIFY was issued to non-existent
2992 * device. No need to reset. Just
842faa6c 2993 * thaw and ignore the device.
55a8e2c8
TH
2994 */
2995 ata_eh_thaw_port(ap);
084fe639 2996 break;
8c3c52a8 2997 default:
8c3c52a8 2998 goto err;
084fe639 2999 }
8c3c52a8
TH
3000 }
3001 }
084fe639 3002
c1c4e8d5 3003 /* PDIAG- should have been released, ask cable type if post-reset */
33267325
TH
3004 if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) {
3005 if (ap->ops->cable_detect)
3006 ap->cbl = ap->ops->cable_detect(ap);
3007 ata_force_cbl(ap);
3008 }
c1c4e8d5 3009
8c3c52a8
TH
3010 /* Configure new devices forward such that user doesn't see
3011 * device detection messages backwards.
3012 */
1eca4365 3013 ata_for_each_dev(dev, link, ALL) {
4f7c2874 3014 if (!(new_mask & (1 << dev->devno)))
8c3c52a8
TH
3015 continue;
3016
842faa6c
TH
3017 dev->class = ehc->classes[dev->devno];
3018
4f7c2874
TH
3019 if (dev->class == ATA_DEV_PMP)
3020 continue;
3021
8c3c52a8
TH
3022 ehc->i.flags |= ATA_EHI_PRINTINFO;
3023 rc = ata_dev_configure(dev);
3024 ehc->i.flags &= ~ATA_EHI_PRINTINFO;
842faa6c
TH
3025 if (rc) {
3026 dev->class = ATA_DEV_UNKNOWN;
8c3c52a8 3027 goto err;
842faa6c 3028 }
8c3c52a8
TH
3029
3030 spin_lock_irqsave(ap->lock, flags);
3031 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
3032 spin_unlock_irqrestore(ap->lock, flags);
3033
3034 /* new device discovered, configure xfermode */
3035 ehc->i.flags |= ATA_EHI_SETMODE;
022bdb07
TH
3036 }
3037
8c3c52a8 3038 return 0;
022bdb07 3039
8c3c52a8
TH
3040 err:
3041 *r_failed_dev = dev;
022bdb07
TH
3042 return rc;
3043}
3044
6f1d1e3a
TH
3045/**
3046 * ata_set_mode - Program timings and issue SET FEATURES - XFER
3047 * @link: link on which timings will be programmed
98a1708d 3048 * @r_failed_dev: out parameter for failed device
6f1d1e3a
TH
3049 *
3050 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3051 * ata_set_mode() fails, pointer to the failing device is
3052 * returned in @r_failed_dev.
3053 *
3054 * LOCKING:
3055 * PCI/etc. bus probe sem.
3056 *
3057 * RETURNS:
3058 * 0 on success, negative errno otherwise
3059 */
3060int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3061{
3062 struct ata_port *ap = link->ap;
00115e0f
TH
3063 struct ata_device *dev;
3064 int rc;
6f1d1e3a 3065
76326ac1 3066 /* if data transfer is verified, clear DUBIOUS_XFER on ering top */
1eca4365 3067 ata_for_each_dev(dev, link, ENABLED) {
76326ac1
TH
3068 if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
3069 struct ata_ering_entry *ent;
3070
3071 ent = ata_ering_top(&dev->ering);
3072 if (ent)
3073 ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER;
3074 }
3075 }
3076
6f1d1e3a
TH
3077 /* has private set_mode? */
3078 if (ap->ops->set_mode)
00115e0f
TH
3079 rc = ap->ops->set_mode(link, r_failed_dev);
3080 else
3081 rc = ata_do_set_mode(link, r_failed_dev);
3082
3083 /* if transfer mode has changed, set DUBIOUS_XFER on device */
1eca4365 3084 ata_for_each_dev(dev, link, ENABLED) {
00115e0f
TH
3085 struct ata_eh_context *ehc = &link->eh_context;
3086 u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
3087 u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
3088
3089 if (dev->xfer_mode != saved_xfer_mode ||
3090 ata_ncq_enabled(dev) != saved_ncq)
3091 dev->flags |= ATA_DFLAG_DUBIOUS_XFER;
3092 }
3093
3094 return rc;
6f1d1e3a
TH
3095}
3096
11fc33da
TH
3097/**
3098 * atapi_eh_clear_ua - Clear ATAPI UNIT ATTENTION after reset
3099 * @dev: ATAPI device to clear UA for
3100 *
3101 * Resets and other operations can make an ATAPI device raise
3102 * UNIT ATTENTION which causes the next operation to fail. This
3103 * function clears UA.
3104 *
3105 * LOCKING:
3106 * EH context (may sleep).
3107 *
3108 * RETURNS:
3109 * 0 on success, -errno on failure.
3110 */
3111static int atapi_eh_clear_ua(struct ata_device *dev)
3112{
3113 int i;
3114
3115 for (i = 0; i < ATA_EH_UA_TRIES; i++) {
b5357081 3116 u8 *sense_buffer = dev->link->ap->sector_buf;
11fc33da
TH
3117 u8 sense_key = 0;
3118 unsigned int err_mask;
3119
3120 err_mask = atapi_eh_tur(dev, &sense_key);
3121 if (err_mask != 0 && err_mask != AC_ERR_DEV) {
a9a79dfe
JP
3122 ata_dev_warn(dev,
3123 "TEST_UNIT_READY failed (err_mask=0x%x)\n",
3124 err_mask);
11fc33da
TH
3125 return -EIO;
3126 }
3127
3128 if (!err_mask || sense_key != UNIT_ATTENTION)
3129 return 0;
3130
3131 err_mask = atapi_eh_request_sense(dev, sense_buffer, sense_key);
3132 if (err_mask) {
a9a79dfe 3133 ata_dev_warn(dev, "failed to clear "
11fc33da
TH
3134 "UNIT ATTENTION (err_mask=0x%x)\n", err_mask);
3135 return -EIO;
3136 }
3137 }
3138
a9a79dfe
JP
3139 ata_dev_warn(dev, "UNIT ATTENTION persists after %d tries\n",
3140 ATA_EH_UA_TRIES);
11fc33da
TH
3141
3142 return 0;
3143}
3144
6013efd8
TH
3145/**
3146 * ata_eh_maybe_retry_flush - Retry FLUSH if necessary
3147 * @dev: ATA device which may need FLUSH retry
3148 *
3149 * If @dev failed FLUSH, it needs to be reported upper layer
3150 * immediately as it means that @dev failed to remap and already
3151 * lost at least a sector and further FLUSH retrials won't make
3152 * any difference to the lost sector. However, if FLUSH failed
3153 * for other reasons, for example transmission error, FLUSH needs
3154 * to be retried.
3155 *
3156 * This function determines whether FLUSH failure retry is
3157 * necessary and performs it if so.
3158 *
3159 * RETURNS:
3160 * 0 if EH can continue, -errno if EH needs to be repeated.
3161 */
3162static int ata_eh_maybe_retry_flush(struct ata_device *dev)
3163{
3164 struct ata_link *link = dev->link;
3165 struct ata_port *ap = link->ap;
3166 struct ata_queued_cmd *qc;
3167 struct ata_taskfile tf;
3168 unsigned int err_mask;
3169 int rc = 0;
3170
3171 /* did flush fail for this device? */
3172 if (!ata_tag_valid(link->active_tag))
3173 return 0;
3174
3175 qc = __ata_qc_from_tag(ap, link->active_tag);
3176 if (qc->dev != dev || (qc->tf.command != ATA_CMD_FLUSH_EXT &&
3177 qc->tf.command != ATA_CMD_FLUSH))
3178 return 0;
3179
3180 /* if the device failed it, it should be reported to upper layers */
3181 if (qc->err_mask & AC_ERR_DEV)
3182 return 0;
3183
3184 /* flush failed for some other reason, give it another shot */
3185 ata_tf_init(dev, &tf);
3186
3187 tf.command = qc->tf.command;
3188 tf.flags |= ATA_TFLAG_DEVICE;
3189 tf.protocol = ATA_PROT_NODATA;
3190
a9a79dfe 3191 ata_dev_warn(dev, "retrying FLUSH 0x%x Emask 0x%x\n",
6013efd8
TH
3192 tf.command, qc->err_mask);
3193
3194 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
3195 if (!err_mask) {
3196 /*
3197 * FLUSH is complete but there's no way to
3198 * successfully complete a failed command from EH.
3199 * Making sure retry is allowed at least once and
3200 * retrying it should do the trick - whatever was in
3201 * the cache is already on the platter and this won't
3202 * cause infinite loop.
3203 */
3204 qc->scsicmd->allowed = max(qc->scsicmd->allowed, 1);
3205 } else {
a9a79dfe 3206 ata_dev_warn(dev, "FLUSH failed Emask 0x%x\n",
6013efd8
TH
3207 err_mask);
3208 rc = -EIO;
3209
3210 /* if device failed it, report it to upper layers */
3211 if (err_mask & AC_ERR_DEV) {
3212 qc->err_mask |= AC_ERR_DEV;
3213 qc->result_tf = tf;
3214 if (!(ap->pflags & ATA_PFLAG_FROZEN))
3215 rc = 0;
3216 }
3217 }
3218 return rc;
3219}
3220
6b7ae954
TH
3221/**
3222 * ata_eh_set_lpm - configure SATA interface power management
3223 * @link: link to configure power management
3224 * @policy: the link power management policy
3225 * @r_failed_dev: out parameter for failed device
3226 *
3227 * Enable SATA Interface power management. This will enable
f4ac6476
HG
3228 * Device Interface Power Management (DIPM) for min_power and
3229 * medium_power_with_dipm policies, and then call driver specific
3230 * callbacks for enabling Host Initiated Power management.
6b7ae954
TH
3231 *
3232 * LOCKING:
3233 * EH context.
3234 *
3235 * RETURNS:
3236 * 0 on success, -errno on failure.
3237 */
3238static int ata_eh_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
3239 struct ata_device **r_failed_dev)
3240{
6c8ea89c 3241 struct ata_port *ap = ata_is_host_link(link) ? link->ap : NULL;
6b7ae954
TH
3242 struct ata_eh_context *ehc = &link->eh_context;
3243 struct ata_device *dev, *link_dev = NULL, *lpm_dev = NULL;
e5005b15 3244 enum ata_lpm_policy old_policy = link->lpm_policy;
5f6f12cc 3245 bool no_dipm = link->ap->flags & ATA_FLAG_NO_DIPM;
6b7ae954
TH
3246 unsigned int hints = ATA_LPM_EMPTY | ATA_LPM_HIPM;
3247 unsigned int err_mask;
3248 int rc;
3249
3250 /* if the link or host doesn't do LPM, noop */
4c9029e7
BZ
3251 if (!IS_ENABLED(CONFIG_SATA_HOST) ||
3252 (link->flags & ATA_LFLAG_NO_LPM) || (ap && !ap->ops->set_lpm))
6b7ae954
TH
3253 return 0;
3254
3255 /*
3256 * DIPM is enabled only for MIN_POWER as some devices
3257 * misbehave when the host NACKs transition to SLUMBER. Order
3258 * device and link configurations such that the host always
3259 * allows DIPM requests.
3260 */
3261 ata_for_each_dev(dev, link, ENABLED) {
3262 bool hipm = ata_id_has_hipm(dev->id);
ae01b249 3263 bool dipm = ata_id_has_dipm(dev->id) && !no_dipm;
6b7ae954
TH
3264
3265 /* find the first enabled and LPM enabled devices */
3266 if (!link_dev)
3267 link_dev = dev;
3268
3269 if (!lpm_dev && (hipm || dipm))
3270 lpm_dev = dev;
3271
3272 hints &= ~ATA_LPM_EMPTY;
3273 if (!hipm)
3274 hints &= ~ATA_LPM_HIPM;
3275
3276 /* disable DIPM before changing link config */
f4ac6476 3277 if (policy < ATA_LPM_MED_POWER_WITH_DIPM && dipm) {
6b7ae954
TH
3278 err_mask = ata_dev_set_feature(dev,
3279 SETFEATURES_SATA_DISABLE, SATA_DIPM);
3280 if (err_mask && err_mask != AC_ERR_DEV) {
a9a79dfe
JP
3281 ata_dev_warn(dev,
3282 "failed to disable DIPM, Emask 0x%x\n",
3283 err_mask);
6b7ae954
TH
3284 rc = -EIO;
3285 goto fail;
3286 }
3287 }
3288 }
3289
6c8ea89c
TH
3290 if (ap) {
3291 rc = ap->ops->set_lpm(link, policy, hints);
3292 if (!rc && ap->slave_link)
3293 rc = ap->ops->set_lpm(ap->slave_link, policy, hints);
3294 } else
3295 rc = sata_pmp_set_lpm(link, policy, hints);
6b7ae954
TH
3296
3297 /*
3298 * Attribute link config failure to the first (LPM) enabled
3299 * device on the link.
3300 */
3301 if (rc) {
3302 if (rc == -EOPNOTSUPP) {
3303 link->flags |= ATA_LFLAG_NO_LPM;
3304 return 0;
3305 }
3306 dev = lpm_dev ? lpm_dev : link_dev;
3307 goto fail;
3308 }
3309
e5005b15
TH
3310 /*
3311 * Low level driver acked the transition. Issue DIPM command
3312 * with the new policy set.
3313 */
3314 link->lpm_policy = policy;
3315 if (ap && ap->slave_link)
3316 ap->slave_link->lpm_policy = policy;
3317
6b7ae954
TH
3318 /* host config updated, enable DIPM if transitioning to MIN_POWER */
3319 ata_for_each_dev(dev, link, ENABLED) {
f4ac6476 3320 if (policy >= ATA_LPM_MED_POWER_WITH_DIPM && !no_dipm &&
ae01b249 3321 ata_id_has_dipm(dev->id)) {
6b7ae954
TH
3322 err_mask = ata_dev_set_feature(dev,
3323 SETFEATURES_SATA_ENABLE, SATA_DIPM);
3324 if (err_mask && err_mask != AC_ERR_DEV) {
a9a79dfe 3325 ata_dev_warn(dev,
6b7ae954
TH
3326 "failed to enable DIPM, Emask 0x%x\n",
3327 err_mask);
3328 rc = -EIO;
3329 goto fail;
3330 }
3331 }
3332 }
3333
09c5b480
GM
3334 link->last_lpm_change = jiffies;
3335 link->flags |= ATA_LFLAG_CHANGED;
3336
6b7ae954
TH
3337 return 0;
3338
3339fail:
e5005b15
TH
3340 /* restore the old policy */
3341 link->lpm_policy = old_policy;
3342 if (ap && ap->slave_link)
3343 ap->slave_link->lpm_policy = old_policy;
3344
6b7ae954
TH
3345 /* if no device or only one more chance is left, disable LPM */
3346 if (!dev || ehc->tries[dev->devno] <= 2) {
a9a79dfe 3347 ata_link_warn(link, "disabling LPM on the link\n");
6b7ae954
TH
3348 link->flags |= ATA_LFLAG_NO_LPM;
3349 }
3350 if (r_failed_dev)
3351 *r_failed_dev = dev;
3352 return rc;
3353}
3354
8a745f1f 3355int ata_link_nr_enabled(struct ata_link *link)
022bdb07 3356{
f58229f8
TH
3357 struct ata_device *dev;
3358 int cnt = 0;
022bdb07 3359
1eca4365
TH
3360 ata_for_each_dev(dev, link, ENABLED)
3361 cnt++;
022bdb07
TH
3362 return cnt;
3363}
3364
0260731f 3365static int ata_link_nr_vacant(struct ata_link *link)
084fe639 3366{
f58229f8
TH
3367 struct ata_device *dev;
3368 int cnt = 0;
084fe639 3369
1eca4365 3370 ata_for_each_dev(dev, link, ALL)
f58229f8 3371 if (dev->class == ATA_DEV_UNKNOWN)
084fe639
TH
3372 cnt++;
3373 return cnt;
3374}
3375
0260731f 3376static int ata_eh_skip_recovery(struct ata_link *link)
084fe639 3377{
672b2d65 3378 struct ata_port *ap = link->ap;
0260731f 3379 struct ata_eh_context *ehc = &link->eh_context;
f58229f8 3380 struct ata_device *dev;
084fe639 3381
f9df58cb
TH
3382 /* skip disabled links */
3383 if (link->flags & ATA_LFLAG_DISABLED)
3384 return 1;
3385
e2f3d75f
TH
3386 /* skip if explicitly requested */
3387 if (ehc->i.flags & ATA_EHI_NO_RECOVERY)
3388 return 1;
3389
672b2d65
TH
3390 /* thaw frozen port and recover failed devices */
3391 if ((ap->pflags & ATA_PFLAG_FROZEN) || ata_link_nr_enabled(link))
3392 return 0;
3393
3394 /* reset at least once if reset is requested */
3395 if ((ehc->i.action & ATA_EH_RESET) &&
3396 !(ehc->i.flags & ATA_EHI_DID_RESET))
084fe639
TH
3397 return 0;
3398
3399 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
1eca4365 3400 ata_for_each_dev(dev, link, ALL) {
084fe639
TH
3401 if (dev->class == ATA_DEV_UNKNOWN &&
3402 ehc->classes[dev->devno] != ATA_DEV_NONE)
3403 return 0;
3404 }
3405
3406 return 1;
3407}
3408
c2c7a89c
TH
3409static int ata_count_probe_trials_cb(struct ata_ering_entry *ent, void *void_arg)
3410{
3411 u64 interval = msecs_to_jiffies(ATA_EH_PROBE_TRIAL_INTERVAL);
3412 u64 now = get_jiffies_64();
3413 int *trials = void_arg;
3414
6868225e
LM
3415 if ((ent->eflags & ATA_EFLAG_OLD_ER) ||
3416 (ent->timestamp < now - min(now, interval)))
c2c7a89c
TH
3417 return -1;
3418
3419 (*trials)++;
3420 return 0;
3421}
3422
02c05a27
TH
3423static int ata_eh_schedule_probe(struct ata_device *dev)
3424{
3425 struct ata_eh_context *ehc = &dev->link->eh_context;
c2c7a89c
TH
3426 struct ata_link *link = ata_dev_phys_link(dev);
3427 int trials = 0;
02c05a27
TH
3428
3429 if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
3430 (ehc->did_probe_mask & (1 << dev->devno)))
3431 return 0;
3432
3433 ata_eh_detach_dev(dev);
3434 ata_dev_init(dev);
3435 ehc->did_probe_mask |= (1 << dev->devno);
cf480626 3436 ehc->i.action |= ATA_EH_RESET;
00115e0f
TH
3437 ehc->saved_xfer_mode[dev->devno] = 0;
3438 ehc->saved_ncq_enabled &= ~(1 << dev->devno);
02c05a27 3439
6b7ae954 3440 /* the link maybe in a deep sleep, wake it up */
6c8ea89c
TH
3441 if (link->lpm_policy > ATA_LPM_MAX_POWER) {
3442 if (ata_is_host_link(link))
3443 link->ap->ops->set_lpm(link, ATA_LPM_MAX_POWER,
3444 ATA_LPM_EMPTY);
3445 else
3446 sata_pmp_set_lpm(link, ATA_LPM_MAX_POWER,
3447 ATA_LPM_EMPTY);
3448 }
6b7ae954 3449
c2c7a89c
TH
3450 /* Record and count probe trials on the ering. The specific
3451 * error mask used is irrelevant. Because a successful device
3452 * detection clears the ering, this count accumulates only if
3453 * there are consecutive failed probes.
3454 *
3455 * If the count is equal to or higher than ATA_EH_PROBE_TRIALS
3456 * in the last ATA_EH_PROBE_TRIAL_INTERVAL, link speed is
3457 * forced to 1.5Gbps.
3458 *
3459 * This is to work around cases where failed link speed
3460 * negotiation results in device misdetection leading to
3461 * infinite DEVXCHG or PHRDY CHG events.
3462 */
3463 ata_ering_record(&dev->ering, 0, AC_ERR_OTHER);
3464 ata_ering_map(&dev->ering, ata_count_probe_trials_cb, &trials);
3465
3466 if (trials > ATA_EH_PROBE_TRIALS)
3467 sata_down_spd_limit(link, 1);
3468
02c05a27
TH
3469 return 1;
3470}
3471
9b1e2658 3472static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
fee7ca72 3473{
9af5c9c9 3474 struct ata_eh_context *ehc = &dev->link->eh_context;
fee7ca72 3475
cf9a590a
TH
3476 /* -EAGAIN from EH routine indicates retry without prejudice.
3477 * The requester is responsible for ensuring forward progress.
3478 */
3479 if (err != -EAGAIN)
3480 ehc->tries[dev->devno]--;
fee7ca72
TH
3481
3482 switch (err) {
3483 case -ENODEV:
3484 /* device missing or wrong IDENTIFY data, schedule probing */
3485 ehc->i.probe_mask |= (1 << dev->devno);
df561f66 3486 fallthrough;
fee7ca72
TH
3487 case -EINVAL:
3488 /* give it just one more chance */
3489 ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
df561f66 3490 fallthrough;
fee7ca72 3491 case -EIO:
d89293ab 3492 if (ehc->tries[dev->devno] == 1) {
fee7ca72
TH
3493 /* This is the last chance, better to slow
3494 * down than lose it.
3495 */
a07d499b 3496 sata_down_spd_limit(ata_dev_phys_link(dev), 0);
d89293ab
TH
3497 if (dev->pio_mode > XFER_PIO_0)
3498 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
fee7ca72
TH
3499 }
3500 }
3501
3502 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
3503 /* disable device if it has used up all its chances */
3504 ata_dev_disable(dev);
3505
3506 /* detach if offline */
b1c72916 3507 if (ata_phys_link_offline(ata_dev_phys_link(dev)))
fee7ca72
TH
3508 ata_eh_detach_dev(dev);
3509
02c05a27 3510 /* schedule probe if necessary */
87fbc5a0 3511 if (ata_eh_schedule_probe(dev)) {
fee7ca72 3512 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
87fbc5a0
TH
3513 memset(ehc->cmd_timeout_idx[dev->devno], 0,
3514 sizeof(ehc->cmd_timeout_idx[dev->devno]));
3515 }
9b1e2658
TH
3516
3517 return 1;
fee7ca72 3518 } else {
cf480626 3519 ehc->i.action |= ATA_EH_RESET;
9b1e2658 3520 return 0;
fee7ca72
TH
3521 }
3522}
3523
022bdb07
TH
3524/**
3525 * ata_eh_recover - recover host port after error
3526 * @ap: host port to recover
f5914a46 3527 * @prereset: prereset method (can be NULL)
022bdb07
TH
3528 * @softreset: softreset method (can be NULL)
3529 * @hardreset: hardreset method (can be NULL)
3530 * @postreset: postreset method (can be NULL)
9b1e2658 3531 * @r_failed_link: out parameter for failed link
022bdb07
TH
3532 *
3533 * This is the alpha and omega, eum and yang, heart and soul of
3534 * libata exception handling. On entry, actions required to
9b1e2658
TH
3535 * recover each link and hotplug requests are recorded in the
3536 * link's eh_context. This function executes all the operations
3537 * with appropriate retrials and fallbacks to resurrect failed
084fe639 3538 * devices, detach goners and greet newcomers.
022bdb07
TH
3539 *
3540 * LOCKING:
3541 * Kernel thread context (may sleep).
3542 *
3543 * RETURNS:
3544 * 0 on success, -errno on failure.
3545 */
fb7fd614
TH
3546int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
3547 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
3548 ata_postreset_fn_t postreset,
3549 struct ata_link **r_failed_link)
022bdb07 3550{
9b1e2658 3551 struct ata_link *link;
022bdb07 3552 struct ata_device *dev;
6b7ae954 3553 int rc, nr_fails;
45fabbb7 3554 unsigned long flags, deadline;
022bdb07 3555
022bdb07 3556 /* prep for recovery */
1eca4365 3557 ata_for_each_link(link, ap, EDGE) {
9b1e2658 3558 struct ata_eh_context *ehc = &link->eh_context;
084fe639 3559
f9df58cb
TH
3560 /* re-enable link? */
3561 if (ehc->i.action & ATA_EH_ENABLE_LINK) {
3562 ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
3563 spin_lock_irqsave(ap->lock, flags);
3564 link->flags &= ~ATA_LFLAG_DISABLED;
3565 spin_unlock_irqrestore(ap->lock, flags);
3566 ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
3567 }
3568
1eca4365 3569 ata_for_each_dev(dev, link, ALL) {
fd995f70
TH
3570 if (link->flags & ATA_LFLAG_NO_RETRY)
3571 ehc->tries[dev->devno] = 1;
3572 else
3573 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
084fe639 3574
9b1e2658
TH
3575 /* collect port action mask recorded in dev actions */
3576 ehc->i.action |= ehc->i.dev_action[dev->devno] &
3577 ~ATA_EH_PERDEV_MASK;
3578 ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
3579
3580 /* process hotplug request */
3581 if (dev->flags & ATA_DFLAG_DETACH)
3582 ata_eh_detach_dev(dev);
3583
02c05a27
TH
3584 /* schedule probe if necessary */
3585 if (!ata_dev_enabled(dev))
3586 ata_eh_schedule_probe(dev);
084fe639 3587 }
022bdb07
TH
3588 }
3589
3590 retry:
022bdb07
TH
3591 rc = 0;
3592
aeb2ecd6 3593 /* if UNLOADING, finish immediately */
b51e9e5d 3594 if (ap->pflags & ATA_PFLAG_UNLOADING)
aeb2ecd6
TH
3595 goto out;
3596
9b1e2658 3597 /* prep for EH */
1eca4365 3598 ata_for_each_link(link, ap, EDGE) {
9b1e2658 3599 struct ata_eh_context *ehc = &link->eh_context;
022bdb07 3600
9b1e2658
TH
3601 /* skip EH if possible. */
3602 if (ata_eh_skip_recovery(link))
3603 ehc->i.action = 0;
3604
1eca4365 3605 ata_for_each_dev(dev, link, ALL)
9b1e2658
TH
3606 ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
3607 }
084fe639 3608
022bdb07 3609 /* reset */
1eca4365 3610 ata_for_each_link(link, ap, EDGE) {
dc98c32c 3611 struct ata_eh_context *ehc = &link->eh_context;
9b1e2658 3612
dc98c32c
TH
3613 if (!(ehc->i.action & ATA_EH_RESET))
3614 continue;
9b1e2658 3615
dc98c32c
TH
3616 rc = ata_eh_reset(link, ata_link_nr_vacant(link),
3617 prereset, softreset, hardreset, postreset);
3618 if (rc) {
a9a79dfe 3619 ata_link_err(link, "reset failed, giving up\n");
dc98c32c 3620 goto out;
022bdb07 3621 }
022bdb07
TH
3622 }
3623
45fabbb7
EO
3624 do {
3625 unsigned long now;
3626
3627 /*
3628 * clears ATA_EH_PARK in eh_info and resets
3629 * ap->park_req_pending
3630 */
3631 ata_eh_pull_park_action(ap);
3632
3633 deadline = jiffies;
1eca4365
TH
3634 ata_for_each_link(link, ap, EDGE) {
3635 ata_for_each_dev(dev, link, ALL) {
45fabbb7
EO
3636 struct ata_eh_context *ehc = &link->eh_context;
3637 unsigned long tmp;
3638
9162c657
HR
3639 if (dev->class != ATA_DEV_ATA &&
3640 dev->class != ATA_DEV_ZAC)
45fabbb7
EO
3641 continue;
3642 if (!(ehc->i.dev_action[dev->devno] &
3643 ATA_EH_PARK))
3644 continue;
3645 tmp = dev->unpark_deadline;
3646 if (time_before(deadline, tmp))
3647 deadline = tmp;
3648 else if (time_before_eq(tmp, jiffies))
3649 continue;
3650 if (ehc->unloaded_mask & (1 << dev->devno))
3651 continue;
3652
3653 ata_eh_park_issue_cmd(dev, 1);
3654 }
3655 }
3656
3657 now = jiffies;
3658 if (time_before_eq(deadline, now))
3659 break;
3660
c0c362b6 3661 ata_eh_release(ap);
45fabbb7
EO
3662 deadline = wait_for_completion_timeout(&ap->park_req_pending,
3663 deadline - now);
c0c362b6 3664 ata_eh_acquire(ap);
45fabbb7 3665 } while (deadline);
1eca4365
TH
3666 ata_for_each_link(link, ap, EDGE) {
3667 ata_for_each_dev(dev, link, ALL) {
45fabbb7
EO
3668 if (!(link->eh_context.unloaded_mask &
3669 (1 << dev->devno)))
3670 continue;
3671
3672 ata_eh_park_issue_cmd(dev, 0);
3673 ata_eh_done(link, dev, ATA_EH_PARK);
3674 }
3675 }
3676
9b1e2658 3677 /* the rest */
6b7ae954
TH
3678 nr_fails = 0;
3679 ata_for_each_link(link, ap, PMP_FIRST) {
9b1e2658 3680 struct ata_eh_context *ehc = &link->eh_context;
022bdb07 3681
6b7ae954
TH
3682 if (sata_pmp_attached(ap) && ata_is_host_link(link))
3683 goto config_lpm;
3684
9b1e2658
TH
3685 /* revalidate existing devices and attach new ones */
3686 rc = ata_eh_revalidate_and_attach(link, &dev);
4ae72a1e 3687 if (rc)
6b7ae954 3688 goto rest_fail;
022bdb07 3689
633273a3
TH
3690 /* if PMP got attached, return, pmp EH will take care of it */
3691 if (link->device->class == ATA_DEV_PMP) {
3692 ehc->i.action = 0;
3693 return 0;
3694 }
3695
9b1e2658
TH
3696 /* configure transfer mode if necessary */
3697 if (ehc->i.flags & ATA_EHI_SETMODE) {
3698 rc = ata_set_mode(link, &dev);
3699 if (rc)
6b7ae954 3700 goto rest_fail;
9b1e2658
TH
3701 ehc->i.flags &= ~ATA_EHI_SETMODE;
3702 }
3703
11fc33da
TH
3704 /* If reset has been issued, clear UA to avoid
3705 * disrupting the current users of the device.
3706 */
3707 if (ehc->i.flags & ATA_EHI_DID_RESET) {
1eca4365 3708 ata_for_each_dev(dev, link, ALL) {
11fc33da
TH
3709 if (dev->class != ATA_DEV_ATAPI)
3710 continue;
3711 rc = atapi_eh_clear_ua(dev);
3712 if (rc)
6b7ae954 3713 goto rest_fail;
21334205
AL
3714 if (zpodd_dev_enabled(dev))
3715 zpodd_post_poweron(dev);
11fc33da
TH
3716 }
3717 }
3718
6013efd8
TH
3719 /* retry flush if necessary */
3720 ata_for_each_dev(dev, link, ALL) {
9162c657
HR
3721 if (dev->class != ATA_DEV_ATA &&
3722 dev->class != ATA_DEV_ZAC)
6013efd8
TH
3723 continue;
3724 rc = ata_eh_maybe_retry_flush(dev);
3725 if (rc)
6b7ae954 3726 goto rest_fail;
6013efd8
TH
3727 }
3728
6b7ae954 3729 config_lpm:
11fc33da 3730 /* configure link power saving */
6b7ae954
TH
3731 if (link->lpm_policy != ap->target_lpm_policy) {
3732 rc = ata_eh_set_lpm(link, ap->target_lpm_policy, &dev);
3733 if (rc)
3734 goto rest_fail;
3735 }
ca77329f 3736
9b1e2658
TH
3737 /* this link is okay now */
3738 ehc->i.flags = 0;
3739 continue;
022bdb07 3740
6b7ae954
TH
3741 rest_fail:
3742 nr_fails++;
3743 if (dev)
3744 ata_eh_handle_dev_fail(dev, rc);
022bdb07 3745
b06ce3e5
TH
3746 if (ap->pflags & ATA_PFLAG_FROZEN) {
3747 /* PMP reset requires working host port.
3748 * Can't retry if it's frozen.
3749 */
071f44b1 3750 if (sata_pmp_attached(ap))
b06ce3e5 3751 goto out;
9b1e2658 3752 break;
b06ce3e5 3753 }
022bdb07
TH
3754 }
3755
6b7ae954 3756 if (nr_fails)
9b1e2658 3757 goto retry;
022bdb07 3758
9b1e2658
TH
3759 out:
3760 if (rc && r_failed_link)
3761 *r_failed_link = link;
3762
022bdb07
TH
3763 return rc;
3764}
3765
3766/**
3767 * ata_eh_finish - finish up EH
3768 * @ap: host port to finish EH for
3769 *
3770 * Recovery is complete. Clean up EH states and retry or finish
3771 * failed qcs.
3772 *
3773 * LOCKING:
3774 * None.
3775 */
fb7fd614 3776void ata_eh_finish(struct ata_port *ap)
022bdb07 3777{
258c4e5c 3778 struct ata_queued_cmd *qc;
022bdb07
TH
3779 int tag;
3780
3781 /* retry or finish qcs */
258c4e5c 3782 ata_qc_for_each_raw(ap, qc, tag) {
022bdb07
TH
3783 if (!(qc->flags & ATA_QCFLAG_FAILED))
3784 continue;
3785
3786 if (qc->err_mask) {
3787 /* FIXME: Once EH migration is complete,
3788 * generate sense data in this function,
3789 * considering both err_mask and tf.
3790 */
03faab78 3791 if (qc->flags & ATA_QCFLAG_RETRY)
022bdb07 3792 ata_eh_qc_retry(qc);
03faab78
TH
3793 else
3794 ata_eh_qc_complete(qc);
022bdb07
TH
3795 } else {
3796 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
3797 ata_eh_qc_complete(qc);
3798 } else {
3799 /* feed zero TF to sense generation */
3800 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
3801 ata_eh_qc_retry(qc);
3802 }
3803 }
3804 }
da917d69
TH
3805
3806 /* make sure nr_active_links is zero after EH */
3807 WARN_ON(ap->nr_active_links);
3808 ap->nr_active_links = 0;
022bdb07
TH
3809}
3810
3811/**
3812 * ata_do_eh - do standard error handling
3813 * @ap: host port to handle error for
a1efdaba 3814 *
f5914a46 3815 * @prereset: prereset method (can be NULL)
022bdb07
TH
3816 * @softreset: softreset method (can be NULL)
3817 * @hardreset: hardreset method (can be NULL)
3818 * @postreset: postreset method (can be NULL)
3819 *
3820 * Perform standard error handling sequence.
3821 *
3822 * LOCKING:
3823 * Kernel thread context (may sleep).
3824 */
f5914a46
TH
3825void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
3826 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
3827 ata_postreset_fn_t postreset)
022bdb07 3828{
9b1e2658
TH
3829 struct ata_device *dev;
3830 int rc;
3831
3832 ata_eh_autopsy(ap);
3833 ata_eh_report(ap);
3834
3835 rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
3836 NULL);
3837 if (rc) {
1eca4365 3838 ata_for_each_dev(dev, &ap->link, ALL)
9b1e2658
TH
3839 ata_dev_disable(dev);
3840 }
3841
022bdb07
TH
3842 ata_eh_finish(ap);
3843}
500530f6 3844
a1efdaba
TH
3845/**
3846 * ata_std_error_handler - standard error handler
3847 * @ap: host port to handle error for
3848 *
3849 * Standard error handler
3850 *
3851 * LOCKING:
3852 * Kernel thread context (may sleep).
3853 */
3854void ata_std_error_handler(struct ata_port *ap)
3855{
3856 struct ata_port_operations *ops = ap->ops;
3857 ata_reset_fn_t hardreset = ops->hardreset;
3858
57c9efdf 3859 /* ignore built-in hardreset if SCR access is not available */
fe06e5f9 3860 if (hardreset == sata_std_hardreset && !sata_scr_valid(&ap->link))
a1efdaba
TH
3861 hardreset = NULL;
3862
3863 ata_do_eh(ap, ops->prereset, ops->softreset, hardreset, ops->postreset);
3864}
a52fbcfc 3865EXPORT_SYMBOL_GPL(ata_std_error_handler);
a1efdaba 3866
6ffa01d8 3867#ifdef CONFIG_PM
500530f6
TH
3868/**
3869 * ata_eh_handle_port_suspend - perform port suspend operation
3870 * @ap: port to suspend
3871 *
3872 * Suspend @ap.
3873 *
3874 * LOCKING:
3875 * Kernel thread context (may sleep).
3876 */
3877static void ata_eh_handle_port_suspend(struct ata_port *ap)
3878{
3879 unsigned long flags;
3880 int rc = 0;
3dc67440 3881 struct ata_device *dev;
500530f6
TH
3882
3883 /* are we suspending? */
3884 spin_lock_irqsave(ap->lock, flags);
3885 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
a7ff60db 3886 ap->pm_mesg.event & PM_EVENT_RESUME) {
500530f6
TH
3887 spin_unlock_irqrestore(ap->lock, flags);
3888 return;
3889 }
3890 spin_unlock_irqrestore(ap->lock, flags);
3891
3892 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
3893
3dc67440
AL
3894 /*
3895 * If we have a ZPODD attached, check its zero
3896 * power ready status before the port is frozen.
a7ff60db 3897 * Only needed for runtime suspend.
3dc67440 3898 */
a7ff60db
AL
3899 if (PMSG_IS_AUTO(ap->pm_mesg)) {
3900 ata_for_each_dev(dev, &ap->link, ENABLED) {
3901 if (zpodd_dev_enabled(dev))
3902 zpodd_on_suspend(dev);
3903 }
3dc67440
AL
3904 }
3905
500530f6
TH
3906 /* suspend */
3907 ata_eh_freeze_port(ap);
3908
3909 if (ap->ops->port_suspend)
3910 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
3911
a7ff60db 3912 ata_acpi_set_state(ap, ap->pm_mesg);
2a7b02ea 3913
bc6e7c4b 3914 /* update the flags */
500530f6
TH
3915 spin_lock_irqsave(ap->lock, flags);
3916
3917 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
3918 if (rc == 0)
3919 ap->pflags |= ATA_PFLAG_SUSPENDED;
64578a3d 3920 else if (ap->pflags & ATA_PFLAG_FROZEN)
500530f6
TH
3921 ata_port_schedule_eh(ap);
3922
500530f6
TH
3923 spin_unlock_irqrestore(ap->lock, flags);
3924
3925 return;
3926}
3927
3928/**
3929 * ata_eh_handle_port_resume - perform port resume operation
3930 * @ap: port to resume
3931 *
3932 * Resume @ap.
3933 *
500530f6
TH
3934 * LOCKING:
3935 * Kernel thread context (may sleep).
3936 */
3937static void ata_eh_handle_port_resume(struct ata_port *ap)
3938{
6f9c1ea2
TH
3939 struct ata_link *link;
3940 struct ata_device *dev;
500530f6 3941 unsigned long flags;
500530f6
TH
3942
3943 /* are we resuming? */
3944 spin_lock_irqsave(ap->lock, flags);
3945 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
a7ff60db 3946 !(ap->pm_mesg.event & PM_EVENT_RESUME)) {
500530f6
TH
3947 spin_unlock_irqrestore(ap->lock, flags);
3948 return;
3949 }
3950 spin_unlock_irqrestore(ap->lock, flags);
3951
9666f400 3952 WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
500530f6 3953
6f9c1ea2
TH
3954 /*
3955 * Error timestamps are in jiffies which doesn't run while
3956 * suspended and PHY events during resume isn't too uncommon.
3957 * When the two are combined, it can lead to unnecessary speed
3958 * downs if the machine is suspended and resumed repeatedly.
3959 * Clear error history.
3960 */
3961 ata_for_each_link(link, ap, HOST_FIRST)
3962 ata_for_each_dev(dev, link, ALL)
3963 ata_ering_clear(&dev->ering);
3964
a7ff60db 3965 ata_acpi_set_state(ap, ap->pm_mesg);
bd3adca5 3966
500530f6 3967 if (ap->ops->port_resume)
ae867937 3968 ap->ops->port_resume(ap);
500530f6 3969
6746544c
TH
3970 /* tell ACPI that we're resuming */
3971 ata_acpi_on_resume(ap);
3972
bc6e7c4b 3973 /* update the flags */
500530f6
TH
3974 spin_lock_irqsave(ap->lock, flags);
3975 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
500530f6
TH
3976 spin_unlock_irqrestore(ap->lock, flags);
3977}
6ffa01d8 3978#endif /* CONFIG_PM */