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