blob: 1d4a6f1e88cd15b94101858ba3f489f63183ee79 [file] [log] [blame]
Thomas Gleixnerc82ee6d2019-05-19 15:51:48 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Tejun Heoece1d632006-04-02 18:51:53 +09002/*
3 * libata-eh.c - libata error handling
4 *
Tejun Heoece1d632006-04-02 18:51:53 +09005 * Copyright 2006 Tejun Heo <htejun@gmail.com>
6 *
Tejun Heoece1d632006-04-02 18:51:53 +09007 * libata documentation is available via 'make {ps|pdf}docs',
Mauro Carvalho Chehab9bb9a392017-05-16 09:16:37 -03008 * as Documentation/driver-api/libata.rst
Tejun Heoece1d632006-04-02 18:51:53 +09009 *
10 * Hardware documentation available from http://www.t13.org/ and
11 * http://www.sata-io.org/
Tejun Heoece1d632006-04-02 18:51:53 +090012 */
13
Tejun Heoece1d632006-04-02 18:51:53 +090014#include <linux/kernel.h>
Jens Axboe242f9dc2008-09-14 05:55:09 -070015#include <linux/blkdev.h>
Paul Gortmaker38789fd2011-07-17 15:33:58 -040016#include <linux/export.h>
Jeff Garzik28555682007-10-11 17:12:35 -040017#include <linux/pci.h>
Tejun Heoece1d632006-04-02 18:51:53 +090018#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>
Robert Hancock65211482009-07-14 20:43:39 -060023#include <scsi/scsi_dbg.h>
Jeff Garzikc6fd2802006-08-10 07:31:37 -040024#include "../scsi/scsi_transport_api.h"
Tejun Heoece1d632006-04-02 18:51:53 +090025
26#include <linux/libata.h>
27
Hannes Reinecke255c03d2015-03-27 16:46:38 +010028#include <trace/events/libata.h>
Tejun Heoece1d632006-04-02 18:51:53 +090029#include "libata.h"
30
Tejun Heo7d47e8d2007-02-02 16:22:31 +090031enum {
Tejun Heo3884f7b2007-11-27 19:28:56 +090032 /* speed down verdicts */
Tejun Heo7d47e8d2007-02-02 16:22:31 +090033 ATA_EH_SPDN_NCQ_OFF = (1 << 0),
34 ATA_EH_SPDN_SPEED_DOWN = (1 << 1),
35 ATA_EH_SPDN_FALLBACK_TO_PIO = (1 << 2),
Tejun Heo76326ac2007-11-27 19:28:59 +090036 ATA_EH_SPDN_KEEP_ERRORS = (1 << 3),
Tejun Heo3884f7b2007-11-27 19:28:56 +090037
38 /* error flags */
39 ATA_EFLAG_IS_IO = (1 << 0),
Tejun Heo76326ac2007-11-27 19:28:59 +090040 ATA_EFLAG_DUBIOUS_XFER = (1 << 1),
Gwendal Grignoud9027472010-05-25 12:31:38 -070041 ATA_EFLAG_OLD_ER = (1 << 31),
Tejun Heo3884f7b2007-11-27 19:28:56 +090042
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,
Tejun Heo75f9caf2008-01-03 01:21:14 +090048 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,
Tejun Heo7d47e8d2007-02-02 16:22:31 +090053
Tejun Heo87fbc5a2008-05-20 02:17:54 +090054 ATA_EH_CMD_DFL_TIMEOUT = 5000,
55
Tejun Heo0a2c0f52008-05-20 02:17:52 +090056 /* always put at least this amount of time between resets */
57 ATA_EH_RESET_COOL_DOWN = 5000,
58
Tejun Heo341c2c92008-05-20 02:17:51 +090059 /* 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,
Tejun Heo11fc33d2008-08-30 14:20:01 +020066
67 ATA_EH_UA_TRIES = 5,
Tejun Heoc2c7a892009-01-29 20:31:34 +090068
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,
Tejun Heo31daabd2007-02-02 16:50:52 +090072};
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
Dan Williams35bf8822014-03-07 14:22:48 -080078 * are mostly for error handling, hotplug and those outlier devices that
79 * take an exceptionally long time to recover from reset.
Tejun Heo31daabd2007-02-02 16:50:52 +090080 */
81static const unsigned long ata_eh_reset_timeouts[] = {
Tejun Heo341c2c92008-05-20 02:17:51 +090082 10000, /* most drives spin up by 10sec */
83 10000, /* > 99% working drives spin up before 20sec */
Dan Williams35bf8822014-03-07 14:22:48 -080084 35000, /* give > 30 secs of idleness for outlier devices */
Tejun Heo341c2c92008-05-20 02:17:51 +090085 5000, /* and sweet one last chance */
Tejun Heod8af0eb2008-05-20 02:17:53 +090086 ULONG_MAX, /* > 1 min has elapsed, give up */
Tejun Heo31daabd2007-02-02 16:50:52 +090087};
88
Tejun Heo87fbc5a2008-05-20 02:17:54 +090089static const unsigned long 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 ULONG_MAX,
94};
95
Damien Le Moal68dbbe72021-11-04 17:31:58 +090096static const unsigned long 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 ULONG_MAX,
100};
101
Tejun Heo6013efd2009-11-19 15:36:45 +0900102static const unsigned long ata_eh_flush_timeouts[] = {
103 15000, /* be generous with flush */
104 15000, /* ditto */
105 30000, /* and even more generous */
106 ULONG_MAX,
107};
108
Tejun Heo87fbc5a2008-05-20 02:17:54 +0900109static const unsigned long ata_eh_other_timeouts[] = {
110 5000, /* same rationale as identify timeout */
111 10000, /* ditto */
112 /* but no merciful 30sec for other commands, it just isn't worth it */
113 ULONG_MAX,
114};
115
116struct ata_eh_cmd_timeout_ent {
117 const u8 *commands;
118 const unsigned long *timeouts;
119};
120
121/* The following table determines timeouts to use for EH internal
122 * commands. Each table entry is a command class and matches the
123 * commands the entry applies to and the timeout table to use.
124 *
125 * On the retry after a command timed out, the next timeout value from
126 * the table is used. If the table doesn't contain further entries,
127 * the last value is used.
128 *
129 * ehc->cmd_timeout_idx keeps track of which timeout to use per
130 * command class, so if SET_FEATURES times out on the first try, the
131 * next try will use the second timeout value only for that class.
132 */
133#define CMDS(cmds...) (const u8 []){ cmds, 0 }
134static const struct ata_eh_cmd_timeout_ent
135ata_eh_cmd_timeout_table[ATA_EH_CMD_TIMEOUT_TABLE_SIZE] = {
136 { .commands = CMDS(ATA_CMD_ID_ATA, ATA_CMD_ID_ATAPI),
137 .timeouts = ata_eh_identify_timeouts, },
Damien Le Moal68dbbe72021-11-04 17:31:58 +0900138 { .commands = CMDS(ATA_CMD_READ_LOG_EXT, ATA_CMD_READ_LOG_DMA_EXT),
139 .timeouts = ata_eh_revalidate_timeouts, },
Tejun Heo87fbc5a2008-05-20 02:17:54 +0900140 { .commands = CMDS(ATA_CMD_READ_NATIVE_MAX, ATA_CMD_READ_NATIVE_MAX_EXT),
141 .timeouts = ata_eh_other_timeouts, },
142 { .commands = CMDS(ATA_CMD_SET_MAX, ATA_CMD_SET_MAX_EXT),
143 .timeouts = ata_eh_other_timeouts, },
144 { .commands = CMDS(ATA_CMD_SET_FEATURES),
145 .timeouts = ata_eh_other_timeouts, },
146 { .commands = CMDS(ATA_CMD_INIT_DEV_PARAMS),
147 .timeouts = ata_eh_other_timeouts, },
Tejun Heo6013efd2009-11-19 15:36:45 +0900148 { .commands = CMDS(ATA_CMD_FLUSH, ATA_CMD_FLUSH_EXT),
149 .timeouts = ata_eh_flush_timeouts },
Tejun Heo87fbc5a2008-05-20 02:17:54 +0900150};
151#undef CMDS
152
Tejun Heoad9e2762006-05-15 20:58:12 +0900153static void __ata_port_freeze(struct ata_port *ap);
Tejun Heo6ffa01d2007-03-02 17:32:47 +0900154#ifdef CONFIG_PM
Tejun Heo500530f2006-07-03 16:07:27 +0900155static void ata_eh_handle_port_suspend(struct ata_port *ap);
156static void ata_eh_handle_port_resume(struct ata_port *ap);
Tejun Heo6ffa01d2007-03-02 17:32:47 +0900157#else /* CONFIG_PM */
158static void ata_eh_handle_port_suspend(struct ata_port *ap)
159{ }
160
161static void ata_eh_handle_port_resume(struct ata_port *ap)
162{ }
Tejun Heo6ffa01d2007-03-02 17:32:47 +0900163#endif /* CONFIG_PM */
Tejun Heoad9e2762006-05-15 20:58:12 +0900164
Mathieu Malaterre0d74d872018-05-05 22:00:37 +0200165static __printf(2, 0) void __ata_ehi_pushv_desc(struct ata_eh_info *ehi,
166 const char *fmt, va_list args)
Tejun Heob64bbc32007-07-16 14:29:39 +0900167{
168 ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
169 ATA_EH_DESC_LEN - ehi->desc_len,
170 fmt, args);
171}
172
173/**
174 * __ata_ehi_push_desc - push error description without adding separator
175 * @ehi: target EHI
176 * @fmt: printf format string
177 *
178 * Format string according to @fmt and append it to @ehi->desc.
179 *
180 * LOCKING:
181 * spin_lock_irqsave(host lock)
182 */
183void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
184{
185 va_list args;
186
187 va_start(args, fmt);
188 __ata_ehi_pushv_desc(ehi, fmt, args);
189 va_end(args);
190}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100191EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
Tejun Heob64bbc32007-07-16 14:29:39 +0900192
193/**
194 * ata_ehi_push_desc - push error description with separator
195 * @ehi: target EHI
196 * @fmt: printf format string
197 *
198 * Format string according to @fmt and append it to @ehi->desc.
199 * If @ehi->desc is not empty, ", " is added in-between.
200 *
201 * LOCKING:
202 * spin_lock_irqsave(host lock)
203 */
204void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
205{
206 va_list args;
207
208 if (ehi->desc_len)
209 __ata_ehi_push_desc(ehi, ", ");
210
211 va_start(args, fmt);
212 __ata_ehi_pushv_desc(ehi, fmt, args);
213 va_end(args);
214}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100215EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
Tejun Heob64bbc32007-07-16 14:29:39 +0900216
217/**
218 * ata_ehi_clear_desc - clean error description
219 * @ehi: target EHI
220 *
221 * Clear @ehi->desc.
222 *
223 * LOCKING:
224 * spin_lock_irqsave(host lock)
225 */
226void ata_ehi_clear_desc(struct ata_eh_info *ehi)
227{
228 ehi->desc[0] = '\0';
229 ehi->desc_len = 0;
230}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100231EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
Tejun Heob64bbc32007-07-16 14:29:39 +0900232
Tejun Heocbcdd872007-08-18 13:14:55 +0900233/**
234 * ata_port_desc - append port description
235 * @ap: target ATA port
236 * @fmt: printf format string
237 *
238 * Format string according to @fmt and append it to port
239 * description. If port description is not empty, " " is added
240 * in-between. This function is to be used while initializing
241 * ata_host. The description is printed on host registration.
242 *
243 * LOCKING:
244 * None.
245 */
246void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
247{
248 va_list args;
249
250 WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
251
252 if (ap->link.eh_info.desc_len)
253 __ata_ehi_push_desc(&ap->link.eh_info, " ");
254
255 va_start(args, fmt);
256 __ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
257 va_end(args);
258}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100259EXPORT_SYMBOL_GPL(ata_port_desc);
Tejun Heocbcdd872007-08-18 13:14:55 +0900260
261#ifdef CONFIG_PCI
Tejun Heocbcdd872007-08-18 13:14:55 +0900262/**
263 * ata_port_pbar_desc - append PCI BAR description
264 * @ap: target ATA port
265 * @bar: target PCI BAR
266 * @offset: offset into PCI BAR
267 * @name: name of the area
268 *
269 * If @offset is negative, this function formats a string which
270 * contains the name, address, size and type of the BAR and
271 * appends it to the port description. If @offset is zero or
272 * positive, only name and offsetted address is appended.
273 *
274 * LOCKING:
275 * None.
276 */
277void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
278 const char *name)
279{
280 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
281 char *type = "";
282 unsigned long long start, len;
283
284 if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
285 type = "m";
286 else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
287 type = "i";
288
289 start = (unsigned long long)pci_resource_start(pdev, bar);
290 len = (unsigned long long)pci_resource_len(pdev, bar);
291
292 if (offset < 0)
293 ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
294 else
Andrew Mortone6a73ab2007-12-13 16:01:37 -0800295 ata_port_desc(ap, "%s 0x%llx", name,
296 start + (unsigned long long)offset);
Tejun Heocbcdd872007-08-18 13:14:55 +0900297}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100298EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
Tejun Heocbcdd872007-08-18 13:14:55 +0900299#endif /* CONFIG_PCI */
300
Tejun Heo87fbc5a2008-05-20 02:17:54 +0900301static int ata_lookup_timeout_table(u8 cmd)
302{
303 int i;
304
305 for (i = 0; i < ATA_EH_CMD_TIMEOUT_TABLE_SIZE; i++) {
306 const u8 *cur;
307
308 for (cur = ata_eh_cmd_timeout_table[i].commands; *cur; cur++)
309 if (*cur == cmd)
310 return i;
311 }
312
313 return -1;
314}
315
316/**
317 * ata_internal_cmd_timeout - determine timeout for an internal command
318 * @dev: target device
319 * @cmd: internal command to be issued
320 *
321 * Determine timeout for internal command @cmd for @dev.
322 *
323 * LOCKING:
324 * EH context.
325 *
326 * RETURNS:
327 * Determined timeout.
328 */
329unsigned long ata_internal_cmd_timeout(struct ata_device *dev, u8 cmd)
330{
331 struct ata_eh_context *ehc = &dev->link->eh_context;
332 int ent = ata_lookup_timeout_table(cmd);
333 int idx;
334
335 if (ent < 0)
336 return ATA_EH_CMD_DFL_TIMEOUT;
337
338 idx = ehc->cmd_timeout_idx[dev->devno][ent];
339 return ata_eh_cmd_timeout_table[ent].timeouts[idx];
340}
341
342/**
343 * ata_internal_cmd_timed_out - notification for internal command timeout
344 * @dev: target device
345 * @cmd: internal command which timed out
346 *
347 * Notify EH that internal command @cmd for @dev timed out. This
348 * function should be called only for commands whose timeouts are
349 * determined using ata_internal_cmd_timeout().
350 *
351 * LOCKING:
352 * EH context.
353 */
354void ata_internal_cmd_timed_out(struct ata_device *dev, u8 cmd)
355{
356 struct ata_eh_context *ehc = &dev->link->eh_context;
357 int ent = ata_lookup_timeout_table(cmd);
358 int idx;
359
360 if (ent < 0)
361 return;
362
363 idx = ehc->cmd_timeout_idx[dev->devno][ent];
364 if (ata_eh_cmd_timeout_table[ent].timeouts[idx + 1] != ULONG_MAX)
365 ehc->cmd_timeout_idx[dev->devno][ent]++;
366}
367
Tejun Heo3884f7b2007-11-27 19:28:56 +0900368static void ata_ering_record(struct ata_ering *ering, unsigned int eflags,
Tejun Heo0c247c52006-05-15 20:58:19 +0900369 unsigned int err_mask)
370{
371 struct ata_ering_entry *ent;
372
373 WARN_ON(!err_mask);
374
375 ering->cursor++;
376 ering->cursor %= ATA_ERING_SIZE;
377
378 ent = &ering->ring[ering->cursor];
Tejun Heo3884f7b2007-11-27 19:28:56 +0900379 ent->eflags = eflags;
Tejun Heo0c247c52006-05-15 20:58:19 +0900380 ent->err_mask = err_mask;
381 ent->timestamp = get_jiffies_64();
382}
383
Tejun Heo76326ac2007-11-27 19:28:59 +0900384static struct ata_ering_entry *ata_ering_top(struct ata_ering *ering)
385{
386 struct ata_ering_entry *ent = &ering->ring[ering->cursor];
387
388 if (ent->err_mask)
389 return ent;
390 return NULL;
391}
392
Gwendal Grignoud9027472010-05-25 12:31:38 -0700393int ata_ering_map(struct ata_ering *ering,
394 int (*map_fn)(struct ata_ering_entry *, void *),
395 void *arg)
Tejun Heo0c247c52006-05-15 20:58:19 +0900396{
397 int idx, rc = 0;
398 struct ata_ering_entry *ent;
399
400 idx = ering->cursor;
401 do {
402 ent = &ering->ring[idx];
403 if (!ent->err_mask)
404 break;
405 rc = map_fn(ent, arg);
406 if (rc)
407 break;
408 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
409 } while (idx != ering->cursor);
410
411 return rc;
412}
413
H Hartley Sweeten60428402012-04-12 15:40:37 -0700414static int ata_ering_clear_cb(struct ata_ering_entry *ent, void *void_arg)
Gwendal Grignoud9027472010-05-25 12:31:38 -0700415{
416 ent->eflags |= ATA_EFLAG_OLD_ER;
417 return 0;
418}
419
420static void ata_ering_clear(struct ata_ering *ering)
421{
422 ata_ering_map(ering, ata_ering_clear_cb, NULL);
423}
424
Tejun Heo64f65ca2006-06-24 20:30:18 +0900425static unsigned int ata_eh_dev_action(struct ata_device *dev)
426{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900427 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heo64f65ca2006-06-24 20:30:18 +0900428
429 return ehc->i.action | ehc->i.dev_action[dev->devno];
430}
431
Tejun Heof58229f2007-08-06 18:36:23 +0900432static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
Tejun Heoaf181c22006-06-24 20:30:18 +0900433 struct ata_eh_info *ehi, unsigned int action)
434{
Tejun Heof58229f2007-08-06 18:36:23 +0900435 struct ata_device *tdev;
Tejun Heoaf181c22006-06-24 20:30:18 +0900436
437 if (!dev) {
438 ehi->action &= ~action;
Tejun Heo1eca4362008-11-03 20:03:17 +0900439 ata_for_each_dev(tdev, link, ALL)
Tejun Heof58229f2007-08-06 18:36:23 +0900440 ehi->dev_action[tdev->devno] &= ~action;
Tejun Heoaf181c22006-06-24 20:30:18 +0900441 } else {
442 /* doesn't make sense for port-wide EH actions */
443 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
444
445 /* break ehi->action into ehi->dev_action */
446 if (ehi->action & action) {
Tejun Heo1eca4362008-11-03 20:03:17 +0900447 ata_for_each_dev(tdev, link, ALL)
Tejun Heof58229f2007-08-06 18:36:23 +0900448 ehi->dev_action[tdev->devno] |=
449 ehi->action & action;
Tejun Heoaf181c22006-06-24 20:30:18 +0900450 ehi->action &= ~action;
451 }
452
453 /* turn off the specified per-dev action */
454 ehi->dev_action[dev->devno] &= ~action;
455 }
456}
457
Tejun Heoece1d632006-04-02 18:51:53 +0900458/**
Tejun Heoc0c362b2010-09-06 17:57:14 +0200459 * ata_eh_acquire - acquire EH ownership
460 * @ap: ATA port to acquire EH ownership for
461 *
462 * Acquire EH ownership for @ap. This is the basic exclusion
463 * mechanism for ports sharing a host. Only one port hanging off
464 * the same host can claim the ownership of EH.
465 *
466 * LOCKING:
467 * EH context.
468 */
469void ata_eh_acquire(struct ata_port *ap)
470{
471 mutex_lock(&ap->host->eh_mutex);
472 WARN_ON_ONCE(ap->host->eh_owner);
473 ap->host->eh_owner = current;
474}
475
476/**
477 * ata_eh_release - release EH ownership
478 * @ap: ATA port to release EH ownership for
479 *
480 * Release EH ownership for @ap if the caller. The caller must
481 * have acquired EH ownership using ata_eh_acquire() previously.
482 *
483 * LOCKING:
484 * EH context.
485 */
486void ata_eh_release(struct ata_port *ap)
487{
488 WARN_ON_ONCE(ap->host->eh_owner != current);
489 ap->host->eh_owner = NULL;
490 mutex_unlock(&ap->host->eh_mutex);
491}
492
Tejun Heoece180d2008-11-03 20:04:37 +0900493static void ata_eh_unload(struct ata_port *ap)
494{
495 struct ata_link *link;
496 struct ata_device *dev;
497 unsigned long flags;
498
499 /* Restore SControl IPM and SPD for the next driver and
500 * disable attached devices.
501 */
502 ata_for_each_link(link, ap, PMP_FIRST) {
503 sata_scr_write(link, SCR_CONTROL, link->saved_scontrol & 0xff0);
504 ata_for_each_dev(dev, link, ALL)
505 ata_dev_disable(dev);
506 }
507
508 /* freeze and set UNLOADED */
509 spin_lock_irqsave(ap->lock, flags);
510
511 ata_port_freeze(ap); /* won't be thawed */
512 ap->pflags &= ~ATA_PFLAG_EH_PENDING; /* clear pending from freeze */
513 ap->pflags |= ATA_PFLAG_UNLOADED;
514
515 spin_unlock_irqrestore(ap->lock, flags);
516}
517
Tejun Heoece1d632006-04-02 18:51:53 +0900518/**
519 * ata_scsi_error - SCSI layer error handler callback
520 * @host: SCSI host on which error occurred
521 *
522 * Handles SCSI-layer-thrown error events.
523 *
524 * LOCKING:
525 * Inherited from SCSI layer (none, can sleep)
526 *
527 * RETURNS:
528 * Zero.
529 */
Jeff Garzik381544b2006-04-11 13:04:39 -0400530void ata_scsi_error(struct Scsi_Host *host)
Tejun Heoece1d632006-04-02 18:51:53 +0900531{
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400532 struct ata_port *ap = ata_shost_to_port(host);
Tejun Heoad9e2762006-05-15 20:58:12 +0900533 unsigned long flags;
James Bottomleyc34aeeb2011-01-23 08:31:14 -0600534 LIST_HEAD(eh_work_q);
Tejun Heoece1d632006-04-02 18:51:53 +0900535
536 DPRINTK("ENTER\n");
537
James Bottomleyc34aeeb2011-01-23 08:31:14 -0600538 spin_lock_irqsave(host->host_lock, flags);
539 list_splice_init(&host->eh_cmd_q, &eh_work_q);
540 spin_unlock_irqrestore(host->host_lock, flags);
541
James Bottomley0e0b494c2011-01-23 09:42:50 -0600542 ata_scsi_cmd_error_handler(host, ap, &eh_work_q);
543
544 /* If we timed raced normal completion and there is nothing to
545 recover nr_timedout == 0 why exactly are we doing error recovery ? */
546 ata_scsi_port_error_handler(host, ap);
547
548 /* finish or retry handled scmd's and clean up */
Wei Fang72d8c362016-06-07 14:53:56 +0800549 WARN_ON(!list_empty(&eh_work_q));
James Bottomley0e0b494c2011-01-23 09:42:50 -0600550
551 DPRINTK("EXIT\n");
552}
553
554/**
555 * ata_scsi_cmd_error_handler - error callback for a list of commands
556 * @host: scsi host containing the port
557 * @ap: ATA port within the host
558 * @eh_work_q: list of commands to process
559 *
560 * process the given list of commands and return those finished to the
561 * ap->eh_done_q. This function is the first part of the libata error
562 * handler which processes a given list of failed commands.
563 */
564void ata_scsi_cmd_error_handler(struct Scsi_Host *host, struct ata_port *ap,
565 struct list_head *eh_work_q)
566{
567 int i;
568 unsigned long flags;
569
Tejun Heoc4291372010-05-10 21:41:38 +0200570 /* make sure sff pio task is not running */
571 ata_sff_flush_pio_task(ap);
Tejun Heoece1d632006-04-02 18:51:53 +0900572
Jeff Garzikcca39742006-08-24 03:19:22 -0400573 /* synchronize with host lock and sort out timeouts */
Tejun Heoece1d632006-04-02 18:51:53 +0900574
Tejun Heoad9e2762006-05-15 20:58:12 +0900575 /* For new EH, all qcs are finished in one of three ways -
576 * normal completion, error completion, and SCSI timeout.
Alan Coxc96f1732009-03-24 10:23:46 +0000577 * Both completions can race against SCSI timeout. When normal
Tejun Heoad9e2762006-05-15 20:58:12 +0900578 * completion wins, the qc never reaches EH. When error
579 * completion wins, the qc has ATA_QCFLAG_FAILED set.
580 *
581 * When SCSI timeout wins, things are a bit more complex.
582 * Normal or error completion can occur after the timeout but
583 * before this point. In such cases, both types of
584 * completions are honored. A scmd is determined to have
585 * timed out iff its associated qc is active and not failed.
586 */
Paul E. McKenneya4f08142017-06-29 13:10:47 -0700587 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900588 if (ap->ops->error_handler) {
589 struct scsi_cmnd *scmd, *tmp;
590 int nr_timedout = 0;
Tejun Heoece1d632006-04-02 18:51:53 +0900591
Alan Coxc96f1732009-03-24 10:23:46 +0000592 /* This must occur under the ap->lock as we don't want
593 a polled recovery to race the real interrupt handler
Gwendal Grignoud9027472010-05-25 12:31:38 -0700594
Alan Coxc96f1732009-03-24 10:23:46 +0000595 The lost_interrupt handler checks for any completed but
596 non-notified command and completes much like an IRQ handler.
Gwendal Grignoud9027472010-05-25 12:31:38 -0700597
Alan Coxc96f1732009-03-24 10:23:46 +0000598 We then fall into the error recovery code which will treat
599 this as if normal completion won the race */
Tejun Heoad9e2762006-05-15 20:58:12 +0900600
Alan Coxc96f1732009-03-24 10:23:46 +0000601 if (ap->ops->lost_interrupt)
602 ap->ops->lost_interrupt(ap);
Gwendal Grignoud9027472010-05-25 12:31:38 -0700603
James Bottomley0e0b494c2011-01-23 09:42:50 -0600604 list_for_each_entry_safe(scmd, tmp, eh_work_q, eh_entry) {
Tejun Heoad9e2762006-05-15 20:58:12 +0900605 struct ata_queued_cmd *qc;
606
Jens Axboe258c4e52018-06-19 10:12:49 -0600607 ata_qc_for_each_raw(ap, qc, i) {
Tejun Heoad9e2762006-05-15 20:58:12 +0900608 if (qc->flags & ATA_QCFLAG_ACTIVE &&
609 qc->scsicmd == scmd)
610 break;
611 }
612
613 if (i < ATA_MAX_QUEUE) {
614 /* the scmd has an associated qc */
615 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
616 /* which hasn't failed yet, timeout */
617 qc->err_mask |= AC_ERR_TIMEOUT;
618 qc->flags |= ATA_QCFLAG_FAILED;
619 nr_timedout++;
620 }
621 } else {
622 /* Normal completion occurred after
623 * SCSI timeout but before this point.
624 * Successfully complete it.
625 */
626 scmd->retries = scmd->allowed;
627 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
628 }
629 }
630
631 /* If we have timed out qcs. They belong to EH from
632 * this point but the state of the controller is
633 * unknown. Freeze the port to make sure the IRQ
634 * handler doesn't diddle with those qcs. This must
635 * be done atomically w.r.t. setting QCFLAG_FAILED.
636 */
637 if (nr_timedout)
638 __ata_port_freeze(ap);
639
Tejun Heoa1e10f72007-08-18 13:28:49 +0900640
641 /* initialize eh_tries */
642 ap->eh_tries = ATA_EH_MAX_TRIES;
Paul E. McKenneya4f08142017-06-29 13:10:47 -0700643 }
644 spin_unlock_irqrestore(ap->lock, flags);
Gwendal Grignoud9027472010-05-25 12:31:38 -0700645
James Bottomley0e0b494c2011-01-23 09:42:50 -0600646}
647EXPORT_SYMBOL(ata_scsi_cmd_error_handler);
648
649/**
650 * ata_scsi_port_error_handler - recover the port after the commands
651 * @host: SCSI host containing the port
652 * @ap: the ATA port
653 *
654 * Handle the recovery of the port @ap after all the commands
655 * have been recovered.
656 */
657void ata_scsi_port_error_handler(struct Scsi_Host *host, struct ata_port *ap)
658{
659 unsigned long flags;
Tejun Heoad9e2762006-05-15 20:58:12 +0900660
Tejun Heoad9e2762006-05-15 20:58:12 +0900661 /* invoke error handler */
662 if (ap->ops->error_handler) {
Tejun Heocf1b86c2007-08-06 18:36:23 +0900663 struct ata_link *link;
664
Tejun Heoc0c362b2010-09-06 17:57:14 +0200665 /* acquire EH ownership */
666 ata_eh_acquire(ap);
667 repeat:
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900668 /* kill fast drain timer */
669 del_timer_sync(&ap->fastdrain_timer);
670
Tejun Heo500530f2006-07-03 16:07:27 +0900671 /* process port resume request */
672 ata_eh_handle_port_resume(ap);
673
Tejun Heof3e81b12006-05-15 20:58:21 +0900674 /* fetch & clear EH info */
Tejun Heoe30349d2006-07-03 03:02:15 +0900675 spin_lock_irqsave(ap->lock, flags);
Tejun Heof3e81b12006-05-15 20:58:21 +0900676
Tejun Heo1eca4362008-11-03 20:03:17 +0900677 ata_for_each_link(link, ap, HOST_FIRST) {
Tejun Heo00115e02007-11-27 19:28:58 +0900678 struct ata_eh_context *ehc = &link->eh_context;
679 struct ata_device *dev;
680
Tejun Heocf1b86c2007-08-06 18:36:23 +0900681 memset(&link->eh_context, 0, sizeof(link->eh_context));
682 link->eh_context.i = link->eh_info;
683 memset(&link->eh_info, 0, sizeof(link->eh_info));
Tejun Heo00115e02007-11-27 19:28:58 +0900684
Tejun Heo1eca4362008-11-03 20:03:17 +0900685 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo00115e02007-11-27 19:28:58 +0900686 int devno = dev->devno;
687
688 ehc->saved_xfer_mode[devno] = dev->xfer_mode;
689 if (ata_ncq_enabled(dev))
690 ehc->saved_ncq_enabled |= 1 << devno;
691 }
Tejun Heocf1b86c2007-08-06 18:36:23 +0900692 }
Tejun Heof3e81b12006-05-15 20:58:21 +0900693
Tejun Heob51e9e52006-06-29 01:29:30 +0900694 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
695 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
Tejun Heoda917d62007-09-23 13:14:12 +0900696 ap->excl_link = NULL; /* don't maintain exclusion over EH */
Tejun Heof3e81b12006-05-15 20:58:21 +0900697
Tejun Heoe30349d2006-07-03 03:02:15 +0900698 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900699
Tejun Heo500530f2006-07-03 16:07:27 +0900700 /* invoke EH, skip if unloading or suspended */
701 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
Tejun Heo720ba122006-05-31 18:28:13 +0900702 ap->ops->error_handler(ap);
Tejun Heoece180d2008-11-03 20:04:37 +0900703 else {
704 /* if unloading, commence suicide */
705 if ((ap->pflags & ATA_PFLAG_UNLOADING) &&
706 !(ap->pflags & ATA_PFLAG_UNLOADED))
707 ata_eh_unload(ap);
Tejun Heo720ba122006-05-31 18:28:13 +0900708 ata_eh_finish(ap);
Tejun Heoece180d2008-11-03 20:04:37 +0900709 }
Tejun Heoad9e2762006-05-15 20:58:12 +0900710
Tejun Heo500530f2006-07-03 16:07:27 +0900711 /* process port suspend request */
712 ata_eh_handle_port_suspend(ap);
713
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300714 /* Exception might have happened after ->error_handler
Tejun Heoad9e2762006-05-15 20:58:12 +0900715 * recovered the port but before this point. Repeat
716 * EH in such case.
717 */
Tejun Heoe30349d2006-07-03 03:02:15 +0900718 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900719
Tejun Heob51e9e52006-06-29 01:29:30 +0900720 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
Tejun Heoa1e10f72007-08-18 13:28:49 +0900721 if (--ap->eh_tries) {
Tejun Heoe30349d2006-07-03 03:02:15 +0900722 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900723 goto repeat;
724 }
Joe Perchesa9a79df2011-04-15 15:51:59 -0700725 ata_port_err(ap,
726 "EH pending after %d tries, giving up\n",
727 ATA_EH_MAX_TRIES);
Tejun Heo914616a2007-06-25 21:47:11 +0900728 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
Tejun Heoad9e2762006-05-15 20:58:12 +0900729 }
730
Tejun Heof3e81b12006-05-15 20:58:21 +0900731 /* this run is complete, make sure EH info is clear */
Tejun Heo1eca4362008-11-03 20:03:17 +0900732 ata_for_each_link(link, ap, HOST_FIRST)
Tejun Heocf1b86c2007-08-06 18:36:23 +0900733 memset(&link->eh_info, 0, sizeof(link->eh_info));
Tejun Heof3e81b12006-05-15 20:58:21 +0900734
Dan Williamse4a9c372012-06-21 23:25:27 -0700735 /* end eh (clear host_eh_scheduled) while holding
736 * ap->lock such that if exception occurs after this
737 * point but before EH completion, SCSI midlayer will
Tejun Heoad9e2762006-05-15 20:58:12 +0900738 * re-initiate EH.
739 */
Dan Williamse4a9c372012-06-21 23:25:27 -0700740 ap->ops->end_eh(ap);
Tejun Heoad9e2762006-05-15 20:58:12 +0900741
Tejun Heoe30349d2006-07-03 03:02:15 +0900742 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc0c362b2010-09-06 17:57:14 +0200743 ata_eh_release(ap);
Tejun Heoad9e2762006-05-15 20:58:12 +0900744 } else {
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900745 WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
Tejun Heoad9e2762006-05-15 20:58:12 +0900746 ap->ops->eng_timeout(ap);
747 }
748
Tejun Heoece1d632006-04-02 18:51:53 +0900749 scsi_eh_flush_done_q(&ap->eh_done_q);
750
Tejun Heoad9e2762006-05-15 20:58:12 +0900751 /* clean up */
Tejun Heoe30349d2006-07-03 03:02:15 +0900752 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900753
Tejun Heo1cdaf532006-07-03 16:07:26 +0900754 if (ap->pflags & ATA_PFLAG_LOADING)
Tejun Heob51e9e52006-06-29 01:29:30 +0900755 ap->pflags &= ~ATA_PFLAG_LOADING;
Jason Yan6f541202018-02-28 09:11:10 +0800756 else if ((ap->pflags & ATA_PFLAG_SCSI_HOTPLUG) &&
757 !(ap->flags & ATA_FLAG_SAS_HOST))
Tejun Heoad72cf92010-07-02 10:03:52 +0200758 schedule_delayed_work(&ap->hotplug_task, 0);
Tejun Heo1cdaf532006-07-03 16:07:26 +0900759
760 if (ap->pflags & ATA_PFLAG_RECOVERED)
Joe Perchesa9a79df2011-04-15 15:51:59 -0700761 ata_port_info(ap, "EH complete\n");
Tejun Heo580b21022006-05-31 18:28:05 +0900762
Tejun Heob51e9e52006-06-29 01:29:30 +0900763 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
Tejun Heoad9e2762006-05-15 20:58:12 +0900764
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900765 /* tell wait_eh that we're done */
Tejun Heob51e9e52006-06-29 01:29:30 +0900766 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900767 wake_up_all(&ap->eh_wait_q);
768
Tejun Heoe30349d2006-07-03 03:02:15 +0900769 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900770}
James Bottomley0e0b494c2011-01-23 09:42:50 -0600771EXPORT_SYMBOL_GPL(ata_scsi_port_error_handler);
Tejun Heoece1d632006-04-02 18:51:53 +0900772
773/**
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900774 * ata_port_wait_eh - Wait for the currently pending EH to complete
775 * @ap: Port to wait EH for
776 *
777 * Wait until the currently pending EH is complete.
778 *
779 * LOCKING:
780 * Kernel thread context (may sleep).
781 */
782void ata_port_wait_eh(struct ata_port *ap)
783{
784 unsigned long flags;
785 DEFINE_WAIT(wait);
786
787 retry:
Jeff Garzikba6a1302006-06-22 23:46:10 -0400788 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900789
Tejun Heob51e9e52006-06-29 01:29:30 +0900790 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900791 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
Jeff Garzikba6a1302006-06-22 23:46:10 -0400792 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900793 schedule();
Jeff Garzikba6a1302006-06-22 23:46:10 -0400794 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900795 }
Tejun Heo0a1b6222006-06-11 11:01:38 +0900796 finish_wait(&ap->eh_wait_q, &wait);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900797
Jeff Garzikba6a1302006-06-22 23:46:10 -0400798 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900799
800 /* make sure SCSI EH is complete */
Jeff Garzikcca39742006-08-24 03:19:22 -0400801 if (scsi_host_in_recovery(ap->scsi_host)) {
Tejun Heo97750ce2010-09-06 17:56:29 +0200802 ata_msleep(ap, 10);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900803 goto retry;
804 }
805}
Dan Williams81c757b2011-12-02 16:07:01 -0800806EXPORT_SYMBOL_GPL(ata_port_wait_eh);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900807
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900808static int ata_eh_nr_in_flight(struct ata_port *ap)
809{
Jens Axboe258c4e52018-06-19 10:12:49 -0600810 struct ata_queued_cmd *qc;
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900811 unsigned int tag;
812 int nr = 0;
813
814 /* count only non-internal commands */
Jens Axboe258c4e52018-06-19 10:12:49 -0600815 ata_qc_for_each(ap, qc, tag) {
816 if (qc)
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900817 nr++;
Jens Axboe9d207ac2018-05-11 12:51:07 -0600818 }
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900819
820 return nr;
821}
822
Kees Cookb93ab332017-10-16 14:56:42 -0700823void ata_eh_fastdrain_timerfn(struct timer_list *t)
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900824{
Kees Cookb93ab332017-10-16 14:56:42 -0700825 struct ata_port *ap = from_timer(ap, t, fastdrain_timer);
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900826 unsigned long flags;
827 int cnt;
828
829 spin_lock_irqsave(ap->lock, flags);
830
831 cnt = ata_eh_nr_in_flight(ap);
832
833 /* are we done? */
834 if (!cnt)
835 goto out_unlock;
836
837 if (cnt == ap->fastdrain_cnt) {
Jens Axboe258c4e52018-06-19 10:12:49 -0600838 struct ata_queued_cmd *qc;
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900839 unsigned int tag;
840
841 /* No progress during the last interval, tag all
842 * in-flight qcs as timed out and freeze the port.
843 */
Jens Axboe258c4e52018-06-19 10:12:49 -0600844 ata_qc_for_each(ap, qc, tag) {
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900845 if (qc)
846 qc->err_mask |= AC_ERR_TIMEOUT;
847 }
848
849 ata_port_freeze(ap);
850 } else {
851 /* some qcs have finished, give it another chance */
852 ap->fastdrain_cnt = cnt;
853 ap->fastdrain_timer.expires =
Tejun Heo341c2c92008-05-20 02:17:51 +0900854 ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900855 add_timer(&ap->fastdrain_timer);
856 }
857
858 out_unlock:
859 spin_unlock_irqrestore(ap->lock, flags);
860}
861
862/**
863 * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
864 * @ap: target ATA port
865 * @fastdrain: activate fast drain
866 *
867 * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
868 * is non-zero and EH wasn't pending before. Fast drain ensures
869 * that EH kicks in in timely manner.
870 *
871 * LOCKING:
872 * spin_lock_irqsave(host lock)
873 */
874static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
875{
876 int cnt;
877
878 /* already scheduled? */
879 if (ap->pflags & ATA_PFLAG_EH_PENDING)
880 return;
881
882 ap->pflags |= ATA_PFLAG_EH_PENDING;
883
884 if (!fastdrain)
885 return;
886
887 /* do we have in-flight qcs? */
888 cnt = ata_eh_nr_in_flight(ap);
889 if (!cnt)
890 return;
891
892 /* activate fast drain */
893 ap->fastdrain_cnt = cnt;
Tejun Heo341c2c92008-05-20 02:17:51 +0900894 ap->fastdrain_timer.expires =
895 ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900896 add_timer(&ap->fastdrain_timer);
897}
898
Tejun Heof686bcb2006-05-15 20:58:05 +0900899/**
900 * ata_qc_schedule_eh - schedule qc for error handling
901 * @qc: command to schedule error handling for
902 *
903 * Schedule error handling for @qc. EH will kick in as soon as
904 * other commands are drained.
905 *
906 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400907 * spin_lock_irqsave(host lock)
Tejun Heof686bcb2006-05-15 20:58:05 +0900908 */
909void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
910{
911 struct ata_port *ap = qc->ap;
912
913 WARN_ON(!ap->ops->error_handler);
914
915 qc->flags |= ATA_QCFLAG_FAILED;
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900916 ata_eh_set_pending(ap, 1);
Tejun Heof686bcb2006-05-15 20:58:05 +0900917
918 /* The following will fail if timeout has already expired.
919 * ata_scsi_error() takes care of such scmds on EH entry.
920 * Note that ATA_QCFLAG_FAILED is unconditionally set after
921 * this function completes.
922 */
Bart Van Asschec8329cd2021-08-09 16:03:10 -0700923 blk_abort_request(scsi_cmd_to_rq(qc->scsicmd));
Tejun Heof686bcb2006-05-15 20:58:05 +0900924}
925
Tejun Heo7b70fc02006-05-15 20:58:07 +0900926/**
Dan Williamse4a9c372012-06-21 23:25:27 -0700927 * ata_std_sched_eh - non-libsas ata_ports issue eh with this common routine
928 * @ap: ATA port to schedule EH for
929 *
930 * LOCKING: inherited from ata_port_schedule_eh
931 * spin_lock_irqsave(host lock)
932 */
933void ata_std_sched_eh(struct ata_port *ap)
934{
935 WARN_ON(!ap->ops->error_handler);
936
937 if (ap->pflags & ATA_PFLAG_INITIALIZING)
938 return;
939
940 ata_eh_set_pending(ap, 1);
941 scsi_schedule_eh(ap->scsi_host);
942
943 DPRINTK("port EH scheduled\n");
944}
945EXPORT_SYMBOL_GPL(ata_std_sched_eh);
946
947/**
948 * ata_std_end_eh - non-libsas ata_ports complete eh with this common routine
949 * @ap: ATA port to end EH for
950 *
951 * In the libata object model there is a 1:1 mapping of ata_port to
952 * shost, so host fields can be directly manipulated under ap->lock, in
953 * the libsas case we need to hold a lock at the ha->level to coordinate
954 * these events.
955 *
956 * LOCKING:
957 * spin_lock_irqsave(host lock)
958 */
959void ata_std_end_eh(struct ata_port *ap)
960{
961 struct Scsi_Host *host = ap->scsi_host;
962
963 host->host_eh_scheduled = 0;
964}
965EXPORT_SYMBOL(ata_std_end_eh);
966
967
968/**
Tejun Heo7b70fc02006-05-15 20:58:07 +0900969 * ata_port_schedule_eh - schedule error handling without a qc
970 * @ap: ATA port to schedule EH for
971 *
972 * Schedule error handling for @ap. EH will kick in as soon as
973 * all commands are drained.
974 *
975 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400976 * spin_lock_irqsave(host lock)
Tejun Heo7b70fc02006-05-15 20:58:07 +0900977 */
978void ata_port_schedule_eh(struct ata_port *ap)
979{
Dan Williamse4a9c372012-06-21 23:25:27 -0700980 /* see: ata_std_sched_eh, unless you know better */
981 ap->ops->sched_eh(ap);
Tejun Heo7b70fc02006-05-15 20:58:07 +0900982}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +0100983EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
Tejun Heo7b70fc02006-05-15 20:58:07 +0900984
Tejun Heodbd82612007-08-06 18:36:23 +0900985static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
Tejun Heo7b70fc02006-05-15 20:58:07 +0900986{
Jens Axboe258c4e52018-06-19 10:12:49 -0600987 struct ata_queued_cmd *qc;
Tejun Heo7b70fc02006-05-15 20:58:07 +0900988 int tag, nr_aborted = 0;
989
990 WARN_ON(!ap->ops->error_handler);
991
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900992 /* we're gonna abort all commands, no need for fast drain */
993 ata_eh_set_pending(ap, 0);
994
Jens Axboe28361c42018-05-11 12:51:09 -0600995 /* include internal tag in iteration */
Jens Axboe258c4e52018-06-19 10:12:49 -0600996 ata_qc_for_each_with_internal(ap, qc, tag) {
Tejun Heodbd82612007-08-06 18:36:23 +0900997 if (qc && (!link || qc->dev->link == link)) {
Tejun Heo7b70fc02006-05-15 20:58:07 +0900998 qc->flags |= ATA_QCFLAG_FAILED;
999 ata_qc_complete(qc);
1000 nr_aborted++;
1001 }
1002 }
1003
1004 if (!nr_aborted)
1005 ata_port_schedule_eh(ap);
1006
1007 return nr_aborted;
1008}
1009
Tejun Heoe3180492006-05-15 20:58:09 +09001010/**
Tejun Heodbd82612007-08-06 18:36:23 +09001011 * ata_link_abort - abort all qc's on the link
1012 * @link: ATA link to abort qc's for
1013 *
1014 * Abort all active qc's active on @link and schedule EH.
1015 *
1016 * LOCKING:
1017 * spin_lock_irqsave(host lock)
1018 *
1019 * RETURNS:
1020 * Number of aborted qc's.
1021 */
1022int ata_link_abort(struct ata_link *link)
1023{
1024 return ata_do_link_abort(link->ap, link);
1025}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001026EXPORT_SYMBOL_GPL(ata_link_abort);
Tejun Heodbd82612007-08-06 18:36:23 +09001027
1028/**
1029 * ata_port_abort - abort all qc's on the port
1030 * @ap: ATA port to abort qc's for
1031 *
1032 * Abort all active qc's of @ap and schedule EH.
1033 *
1034 * LOCKING:
1035 * spin_lock_irqsave(host_set lock)
1036 *
1037 * RETURNS:
1038 * Number of aborted qc's.
1039 */
1040int ata_port_abort(struct ata_port *ap)
1041{
1042 return ata_do_link_abort(ap, NULL);
1043}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001044EXPORT_SYMBOL_GPL(ata_port_abort);
Tejun Heodbd82612007-08-06 18:36:23 +09001045
1046/**
Tejun Heoe3180492006-05-15 20:58:09 +09001047 * __ata_port_freeze - freeze port
1048 * @ap: ATA port to freeze
1049 *
1050 * This function is called when HSM violation or some other
1051 * condition disrupts normal operation of the port. Frozen port
1052 * is not allowed to perform any operation until the port is
1053 * thawed, which usually follows a successful reset.
1054 *
1055 * ap->ops->freeze() callback can be used for freezing the port
1056 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
1057 * port cannot be frozen hardware-wise, the interrupt handler
1058 * must ack and clear interrupts unconditionally while the port
1059 * is frozen.
1060 *
1061 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001062 * spin_lock_irqsave(host lock)
Tejun Heoe3180492006-05-15 20:58:09 +09001063 */
1064static void __ata_port_freeze(struct ata_port *ap)
1065{
1066 WARN_ON(!ap->ops->error_handler);
1067
1068 if (ap->ops->freeze)
1069 ap->ops->freeze(ap);
1070
Tejun Heob51e9e52006-06-29 01:29:30 +09001071 ap->pflags |= ATA_PFLAG_FROZEN;
Tejun Heoe3180492006-05-15 20:58:09 +09001072
Tejun Heo44877b42007-02-21 01:06:51 +09001073 DPRINTK("ata%u port frozen\n", ap->print_id);
Tejun Heoe3180492006-05-15 20:58:09 +09001074}
1075
1076/**
1077 * ata_port_freeze - abort & freeze port
1078 * @ap: ATA port to freeze
1079 *
Jeff Garzik54c38442009-04-07 19:13:15 -04001080 * Abort and freeze @ap. The freeze operation must be called
1081 * first, because some hardware requires special operations
1082 * before the taskfile registers are accessible.
Tejun Heoe3180492006-05-15 20:58:09 +09001083 *
1084 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001085 * spin_lock_irqsave(host lock)
Tejun Heoe3180492006-05-15 20:58:09 +09001086 *
1087 * RETURNS:
1088 * Number of aborted commands.
1089 */
1090int ata_port_freeze(struct ata_port *ap)
1091{
1092 int nr_aborted;
1093
1094 WARN_ON(!ap->ops->error_handler);
1095
Tejun Heoe3180492006-05-15 20:58:09 +09001096 __ata_port_freeze(ap);
Jeff Garzik54c38442009-04-07 19:13:15 -04001097 nr_aborted = ata_port_abort(ap);
Tejun Heoe3180492006-05-15 20:58:09 +09001098
1099 return nr_aborted;
1100}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001101EXPORT_SYMBOL_GPL(ata_port_freeze);
Tejun Heoe3180492006-05-15 20:58:09 +09001102
1103/**
1104 * ata_eh_freeze_port - EH helper to freeze port
1105 * @ap: ATA port to freeze
1106 *
1107 * Freeze @ap.
1108 *
1109 * LOCKING:
1110 * None.
1111 */
1112void ata_eh_freeze_port(struct ata_port *ap)
1113{
1114 unsigned long flags;
1115
1116 if (!ap->ops->error_handler)
1117 return;
1118
Jeff Garzikba6a1302006-06-22 23:46:10 -04001119 spin_lock_irqsave(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001120 __ata_port_freeze(ap);
Jeff Garzikba6a1302006-06-22 23:46:10 -04001121 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001122}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001123EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
Tejun Heoe3180492006-05-15 20:58:09 +09001124
1125/**
Mauro Carvalho Chehab94bd5712020-10-23 18:32:55 +02001126 * ata_eh_thaw_port - EH helper to thaw port
Tejun Heoe3180492006-05-15 20:58:09 +09001127 * @ap: ATA port to thaw
1128 *
1129 * Thaw frozen port @ap.
1130 *
1131 * LOCKING:
1132 * None.
1133 */
1134void ata_eh_thaw_port(struct ata_port *ap)
1135{
1136 unsigned long flags;
1137
1138 if (!ap->ops->error_handler)
1139 return;
1140
Jeff Garzikba6a1302006-06-22 23:46:10 -04001141 spin_lock_irqsave(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001142
Tejun Heob51e9e52006-06-29 01:29:30 +09001143 ap->pflags &= ~ATA_PFLAG_FROZEN;
Tejun Heoe3180492006-05-15 20:58:09 +09001144
1145 if (ap->ops->thaw)
1146 ap->ops->thaw(ap);
1147
Jeff Garzikba6a1302006-06-22 23:46:10 -04001148 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001149
Tejun Heo44877b42007-02-21 01:06:51 +09001150 DPRINTK("ata%u port thawed\n", ap->print_id);
Tejun Heoe3180492006-05-15 20:58:09 +09001151}
1152
Tejun Heoece1d632006-04-02 18:51:53 +09001153static void ata_eh_scsidone(struct scsi_cmnd *scmd)
1154{
1155 /* nada */
1156}
1157
1158static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
1159{
1160 struct ata_port *ap = qc->ap;
1161 struct scsi_cmnd *scmd = qc->scsicmd;
1162 unsigned long flags;
1163
Jeff Garzikba6a1302006-06-22 23:46:10 -04001164 spin_lock_irqsave(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +09001165 qc->scsidone = ata_eh_scsidone;
1166 __ata_qc_complete(qc);
1167 WARN_ON(ata_tag_valid(qc->tag));
Jeff Garzikba6a1302006-06-22 23:46:10 -04001168 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +09001169
1170 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
1171}
1172
1173/**
1174 * ata_eh_qc_complete - Complete an active ATA command from EH
1175 * @qc: Command to complete
1176 *
1177 * Indicate to the mid and upper layers that an ATA command has
1178 * completed. To be used from EH.
1179 */
1180void ata_eh_qc_complete(struct ata_queued_cmd *qc)
1181{
1182 struct scsi_cmnd *scmd = qc->scsicmd;
1183 scmd->retries = scmd->allowed;
1184 __ata_eh_qc_complete(qc);
1185}
1186
1187/**
1188 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
1189 * @qc: Command to retry
1190 *
1191 * Indicate to the mid and upper layers that an ATA command
1192 * should be retried. To be used from EH.
1193 *
1194 * SCSI midlayer limits the number of retries to scmd->allowed.
Gwendal Grignouf13e2202009-08-07 16:17:49 -07001195 * scmd->allowed is incremented for commands which get retried
Tejun Heoece1d632006-04-02 18:51:53 +09001196 * due to unrelated failures (qc->err_mask is zero).
1197 */
1198void ata_eh_qc_retry(struct ata_queued_cmd *qc)
1199{
1200 struct scsi_cmnd *scmd = qc->scsicmd;
Gwendal Grignouf13e2202009-08-07 16:17:49 -07001201 if (!qc->err_mask)
1202 scmd->allowed++;
Tejun Heoece1d632006-04-02 18:51:53 +09001203 __ata_eh_qc_complete(qc);
1204}
Tejun Heo022bdb02006-05-15 20:58:22 +09001205
1206/**
Tejun Heo678afac62009-01-29 20:31:30 +09001207 * ata_dev_disable - disable ATA device
1208 * @dev: ATA device to disable
1209 *
1210 * Disable @dev.
1211 *
1212 * Locking:
1213 * EH context.
1214 */
1215void ata_dev_disable(struct ata_device *dev)
1216{
1217 if (!ata_dev_enabled(dev))
1218 return;
1219
1220 if (ata_msg_drv(dev->link->ap))
Joe Perchesa9a79df2011-04-15 15:51:59 -07001221 ata_dev_warn(dev, "disabled\n");
Tejun Heo678afac62009-01-29 20:31:30 +09001222 ata_acpi_on_disable(dev);
1223 ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 | ATA_DNXFER_QUIET);
1224 dev->class++;
Tejun Heo99cf6102009-01-29 20:31:32 +09001225
1226 /* From now till the next successful probe, ering is used to
1227 * track probe failures. Clear accumulated device error info.
1228 */
1229 ata_ering_clear(&dev->ering);
Tejun Heo678afac62009-01-29 20:31:30 +09001230}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01001231EXPORT_SYMBOL_GPL(ata_dev_disable);
Tejun Heo678afac62009-01-29 20:31:30 +09001232
1233/**
Tejun Heo0ea035a2006-05-31 18:28:01 +09001234 * ata_eh_detach_dev - detach ATA device
1235 * @dev: ATA device to detach
1236 *
1237 * Detach @dev.
1238 *
1239 * LOCKING:
1240 * None.
1241 */
Tejun Heofb7fd612007-09-23 13:14:12 +09001242void ata_eh_detach_dev(struct ata_device *dev)
Tejun Heo0ea035a2006-05-31 18:28:01 +09001243{
Tejun Heof58229f2007-08-06 18:36:23 +09001244 struct ata_link *link = dev->link;
1245 struct ata_port *ap = link->ap;
Tejun Heo90484eb2008-10-26 15:43:03 +09001246 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo0ea035a2006-05-31 18:28:01 +09001247 unsigned long flags;
1248
1249 ata_dev_disable(dev);
1250
Jeff Garzikba6a1302006-06-22 23:46:10 -04001251 spin_lock_irqsave(ap->lock, flags);
Tejun Heo0ea035a2006-05-31 18:28:01 +09001252
1253 dev->flags &= ~ATA_DFLAG_DETACH;
1254
1255 if (ata_scsi_offline_dev(dev)) {
1256 dev->flags |= ATA_DFLAG_DETACHED;
Tejun Heob51e9e52006-06-29 01:29:30 +09001257 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
Tejun Heo0ea035a2006-05-31 18:28:01 +09001258 }
1259
Tejun Heo90484eb2008-10-26 15:43:03 +09001260 /* clear per-dev EH info */
Tejun Heof58229f2007-08-06 18:36:23 +09001261 ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
1262 ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
Tejun Heo90484eb2008-10-26 15:43:03 +09001263 ehc->saved_xfer_mode[dev->devno] = 0;
1264 ehc->saved_ncq_enabled &= ~(1 << dev->devno);
Tejun Heobeb07c12006-06-24 20:30:19 +09001265
Jeff Garzikba6a1302006-06-22 23:46:10 -04001266 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo0ea035a2006-05-31 18:28:01 +09001267}
1268
1269/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001270 * ata_eh_about_to_do - about to perform eh_action
Tejun Heo955e57d2007-08-06 18:36:23 +09001271 * @link: target ATA link
Tejun Heo47005f22006-06-19 18:27:23 +09001272 * @dev: target ATA dev for per-dev action (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09001273 * @action: action about to be performed
1274 *
1275 * Called just before performing EH actions to clear related bits
Tejun Heo955e57d2007-08-06 18:36:23 +09001276 * in @link->eh_info such that eh actions are not unnecessarily
1277 * repeated.
Tejun Heo022bdb02006-05-15 20:58:22 +09001278 *
1279 * LOCKING:
1280 * None.
1281 */
Tejun Heofb7fd612007-09-23 13:14:12 +09001282void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
1283 unsigned int action)
Tejun Heo022bdb02006-05-15 20:58:22 +09001284{
Tejun Heo955e57d2007-08-06 18:36:23 +09001285 struct ata_port *ap = link->ap;
1286 struct ata_eh_info *ehi = &link->eh_info;
1287 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001288 unsigned long flags;
1289
Jeff Garzikba6a1302006-06-22 23:46:10 -04001290 spin_lock_irqsave(ap->lock, flags);
Tejun Heo1cdaf532006-07-03 16:07:26 +09001291
Tejun Heo955e57d2007-08-06 18:36:23 +09001292 ata_eh_clear_action(link, dev, ehi, action);
Tejun Heo13abf502006-07-10 23:18:46 +09001293
Tejun Heoa568d1d2008-10-21 20:37:21 +09001294 /* About to take EH action, set RECOVERED. Ignore actions on
1295 * slave links as master will do them again.
1296 */
1297 if (!(ehc->i.flags & ATA_EHI_QUIET) && link != ap->slave_link)
Tejun Heo1cdaf532006-07-03 16:07:26 +09001298 ap->pflags |= ATA_PFLAG_RECOVERED;
1299
Jeff Garzikba6a1302006-06-22 23:46:10 -04001300 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo022bdb02006-05-15 20:58:22 +09001301}
1302
1303/**
Tejun Heo47005f22006-06-19 18:27:23 +09001304 * ata_eh_done - EH action complete
Jonathan Corbet2f60e1a2017-07-30 16:16:04 -06001305 * @link: ATA link for which EH actions are complete
Tejun Heo47005f22006-06-19 18:27:23 +09001306 * @dev: target ATA dev for per-dev action (can be NULL)
1307 * @action: action just completed
1308 *
1309 * Called right after performing EH actions to clear related bits
Tejun Heo955e57d2007-08-06 18:36:23 +09001310 * in @link->eh_context.
Tejun Heo47005f22006-06-19 18:27:23 +09001311 *
1312 * LOCKING:
1313 * None.
1314 */
Tejun Heofb7fd612007-09-23 13:14:12 +09001315void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1316 unsigned int action)
Tejun Heo47005f22006-06-19 18:27:23 +09001317{
Tejun Heo955e57d2007-08-06 18:36:23 +09001318 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001319
Tejun Heo955e57d2007-08-06 18:36:23 +09001320 ata_eh_clear_action(link, dev, &ehc->i, action);
Tejun Heo47005f22006-06-19 18:27:23 +09001321}
1322
1323/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001324 * ata_err_string - convert err_mask to descriptive string
1325 * @err_mask: error mask to convert to string
1326 *
1327 * Convert @err_mask to descriptive string. Errors are
1328 * prioritized according to severity and only the most severe
1329 * error is reported.
1330 *
1331 * LOCKING:
1332 * None.
1333 *
1334 * RETURNS:
1335 * Descriptive string for @err_mask
1336 */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001337static const char *ata_err_string(unsigned int err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001338{
1339 if (err_mask & AC_ERR_HOST_BUS)
1340 return "host bus error";
1341 if (err_mask & AC_ERR_ATA_BUS)
1342 return "ATA bus error";
1343 if (err_mask & AC_ERR_TIMEOUT)
1344 return "timeout";
1345 if (err_mask & AC_ERR_HSM)
1346 return "HSM violation";
1347 if (err_mask & AC_ERR_SYSTEM)
1348 return "internal error";
1349 if (err_mask & AC_ERR_MEDIA)
1350 return "media error";
1351 if (err_mask & AC_ERR_INVALID)
1352 return "invalid argument";
1353 if (err_mask & AC_ERR_DEV)
1354 return "device error";
Damien Le Moal54fb1312018-05-09 09:28:09 +09001355 if (err_mask & AC_ERR_NCQ)
1356 return "NCQ error";
1357 if (err_mask & AC_ERR_NODEV_HINT)
1358 return "Polling detection error";
Tejun Heo022bdb02006-05-15 20:58:22 +09001359 return "unknown error";
1360}
1361
1362/**
Tejun Heo11fc33d2008-08-30 14:20:01 +02001363 * atapi_eh_tur - perform ATAPI TEST_UNIT_READY
1364 * @dev: target ATAPI device
1365 * @r_sense_key: out parameter for sense_key
1366 *
1367 * Perform ATAPI TEST_UNIT_READY.
1368 *
1369 * LOCKING:
1370 * EH context (may sleep).
1371 *
1372 * RETURNS:
1373 * 0 on success, AC_ERR_* mask on failure.
1374 */
Aaron Lu3dc67442013-01-15 17:21:00 +08001375unsigned int atapi_eh_tur(struct ata_device *dev, u8 *r_sense_key)
Tejun Heo11fc33d2008-08-30 14:20:01 +02001376{
1377 u8 cdb[ATAPI_CDB_LEN] = { TEST_UNIT_READY, 0, 0, 0, 0, 0 };
1378 struct ata_taskfile tf;
1379 unsigned int err_mask;
1380
1381 ata_tf_init(dev, &tf);
1382
1383 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1384 tf.command = ATA_CMD_PACKET;
1385 tf.protocol = ATAPI_PROT_NODATA;
1386
1387 err_mask = ata_exec_internal(dev, &tf, cdb, DMA_NONE, NULL, 0, 0);
1388 if (err_mask == AC_ERR_DEV)
1389 *r_sense_key = tf.feature >> 4;
1390 return err_mask;
1391}
1392
1393/**
Hannes Reineckee87fd282016-04-04 11:43:55 +02001394 * ata_eh_request_sense - perform REQUEST_SENSE_DATA_EXT
Jonathan Corbet2f60e1a2017-07-30 16:16:04 -06001395 * @qc: qc to perform REQUEST_SENSE_SENSE_DATA_EXT to
Hannes Reineckee87fd282016-04-04 11:43:55 +02001396 * @cmd: scsi command for which the sense code should be set
1397 *
1398 * Perform REQUEST_SENSE_DATA_EXT after the device reported CHECK
1399 * SENSE. This function is an EH helper.
1400 *
1401 * LOCKING:
1402 * Kernel thread context (may sleep).
1403 */
1404static void ata_eh_request_sense(struct ata_queued_cmd *qc,
1405 struct scsi_cmnd *cmd)
1406{
1407 struct ata_device *dev = qc->dev;
1408 struct ata_taskfile tf;
1409 unsigned int err_mask;
1410
1411 if (qc->ap->pflags & ATA_PFLAG_FROZEN) {
1412 ata_dev_warn(dev, "sense data available but port frozen\n");
1413 return;
1414 }
1415
Hannes Reinecked238ffd2016-04-25 12:45:43 +02001416 if (!cmd || qc->flags & ATA_QCFLAG_SENSE_VALID)
Hannes Reineckee87fd282016-04-04 11:43:55 +02001417 return;
1418
1419 if (!ata_id_sense_reporting_enabled(dev->id)) {
1420 ata_dev_warn(qc->dev, "sense data reporting disabled\n");
1421 return;
1422 }
1423
1424 DPRINTK("ATA request sense\n");
1425
1426 ata_tf_init(dev, &tf);
1427 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1428 tf.flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
1429 tf.command = ATA_CMD_REQ_SENSE_DATA;
1430 tf.protocol = ATA_PROT_NODATA;
1431
1432 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1433 /* Ignore err_mask; ATA_ERR might be set */
1434 if (tf.command & ATA_SENSE) {
Hannes Reinecke06dbde52016-04-04 11:44:03 +02001435 ata_scsi_set_sense(dev, cmd, tf.lbah, tf.lbam, tf.lbal);
Hannes Reineckee87fd282016-04-04 11:43:55 +02001436 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1437 } else {
1438 ata_dev_warn(dev, "request sense failed stat %02x emask %x\n",
1439 tf.command, err_mask);
1440 }
1441}
1442
1443/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001444 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1445 * @dev: device to perform REQUEST_SENSE to
1446 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
Tejun Heo3eabddb2008-06-10 18:28:05 +09001447 * @dfl_sense_key: default sense key to use
Tejun Heo022bdb02006-05-15 20:58:22 +09001448 *
1449 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
1450 * SENSE. This function is EH helper.
1451 *
1452 * LOCKING:
1453 * Kernel thread context (may sleep).
1454 *
1455 * RETURNS:
1456 * 0 on success, AC_ERR_* mask on failure
1457 */
Aaron Lu3dc67442013-01-15 17:21:00 +08001458unsigned int atapi_eh_request_sense(struct ata_device *dev,
Tejun Heo3eabddb2008-06-10 18:28:05 +09001459 u8 *sense_buf, u8 dfl_sense_key)
Tejun Heo022bdb02006-05-15 20:58:22 +09001460{
Tejun Heo3eabddb2008-06-10 18:28:05 +09001461 u8 cdb[ATAPI_CDB_LEN] =
1462 { REQUEST_SENSE, 0, 0, 0, SCSI_SENSE_BUFFERSIZE, 0 };
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001463 struct ata_port *ap = dev->link->ap;
Tejun Heo022bdb02006-05-15 20:58:22 +09001464 struct ata_taskfile tf;
Tejun Heo022bdb02006-05-15 20:58:22 +09001465
1466 DPRINTK("ATAPI request sense\n");
1467
Tejun Heo022bdb02006-05-15 20:58:22 +09001468 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1469
Albert Lee56287762007-04-02 11:30:46 +08001470 /* initialize sense_buf with the error register,
1471 * for the case where they are -not- overwritten
1472 */
Tejun Heo022bdb02006-05-15 20:58:22 +09001473 sense_buf[0] = 0x70;
Tejun Heo3eabddb2008-06-10 18:28:05 +09001474 sense_buf[2] = dfl_sense_key;
Albert Lee56287762007-04-02 11:30:46 +08001475
Jeff Garzika617c092007-05-21 20:14:23 -04001476 /* some devices time out if garbage left in tf */
Albert Lee56287762007-04-02 11:30:46 +08001477 ata_tf_init(dev, &tf);
Tejun Heo022bdb02006-05-15 20:58:22 +09001478
Tejun Heo022bdb02006-05-15 20:58:22 +09001479 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1480 tf.command = ATA_CMD_PACKET;
1481
1482 /* is it pointless to prefer PIO for "safety reasons"? */
1483 if (ap->flags & ATA_FLAG_PIO_DMA) {
Tejun Heo0dc36882007-12-18 16:34:43 -05001484 tf.protocol = ATAPI_PROT_DMA;
Tejun Heo022bdb02006-05-15 20:58:22 +09001485 tf.feature |= ATAPI_PKT_DMA;
1486 } else {
Tejun Heo0dc36882007-12-18 16:34:43 -05001487 tf.protocol = ATAPI_PROT_PIO;
Tejun Heof2dfc1a2007-12-12 12:12:46 +09001488 tf.lbam = SCSI_SENSE_BUFFERSIZE;
1489 tf.lbah = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09001490 }
1491
1492 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
Tejun Heo2b789102007-10-09 15:05:44 +09001493 sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
Tejun Heo022bdb02006-05-15 20:58:22 +09001494}
1495
1496/**
1497 * ata_eh_analyze_serror - analyze SError for a failed port
Tejun Heo02607312007-08-06 18:36:23 +09001498 * @link: ATA link to analyze SError for
Tejun Heo022bdb02006-05-15 20:58:22 +09001499 *
1500 * Analyze SError if available and further determine cause of
1501 * failure.
1502 *
1503 * LOCKING:
1504 * None.
1505 */
Tejun Heo02607312007-08-06 18:36:23 +09001506static void ata_eh_analyze_serror(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001507{
Tejun Heo02607312007-08-06 18:36:23 +09001508 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001509 u32 serror = ehc->i.serror;
1510 unsigned int err_mask = 0, action = 0;
Tejun Heof9df58c2007-09-23 13:14:13 +09001511 u32 hotplug_mask;
Tejun Heo022bdb02006-05-15 20:58:22 +09001512
Tejun Heoe0614db2008-05-19 01:15:09 +09001513 if (serror & (SERR_PERSISTENT | SERR_DATA)) {
Tejun Heo022bdb02006-05-15 20:58:22 +09001514 err_mask |= AC_ERR_ATA_BUS;
Tejun Heocf480622008-01-24 00:05:14 +09001515 action |= ATA_EH_RESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001516 }
1517 if (serror & SERR_PROTOCOL) {
1518 err_mask |= AC_ERR_HSM;
Tejun Heocf480622008-01-24 00:05:14 +09001519 action |= ATA_EH_RESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001520 }
1521 if (serror & SERR_INTERNAL) {
1522 err_mask |= AC_ERR_SYSTEM;
Tejun Heocf480622008-01-24 00:05:14 +09001523 action |= ATA_EH_RESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001524 }
Tejun Heof9df58c2007-09-23 13:14:13 +09001525
1526 /* Determine whether a hotplug event has occurred. Both
1527 * SError.N/X are considered hotplug events for enabled or
1528 * host links. For disabled PMP links, only N bit is
1529 * considered as X bit is left at 1 for link plugging.
1530 */
Tejun Heoeb0e85e2011-02-24 19:30:37 +01001531 if (link->lpm_policy > ATA_LPM_MAX_POWER)
Tejun Heo6b7ae952010-09-01 17:50:06 +02001532 hotplug_mask = 0; /* hotplug doesn't work w/ LPM */
1533 else if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
Tejun Heof9df58c2007-09-23 13:14:13 +09001534 hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
1535 else
1536 hotplug_mask = SERR_PHYRDY_CHG;
1537
1538 if (serror & hotplug_mask)
Tejun Heo084fe632006-05-31 18:28:03 +09001539 ata_ehi_hotplugged(&ehc->i);
Tejun Heo022bdb02006-05-15 20:58:22 +09001540
1541 ehc->i.err_mask |= err_mask;
1542 ehc->i.action |= action;
1543}
1544
1545/**
1546 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1547 * @qc: qc to analyze
1548 * @tf: Taskfile registers to analyze
1549 *
1550 * Analyze taskfile of @qc and further determine cause of
1551 * failure. This function also requests ATAPI sense data if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001552 * available.
Tejun Heo022bdb02006-05-15 20:58:22 +09001553 *
1554 * LOCKING:
1555 * Kernel thread context (may sleep).
1556 *
1557 * RETURNS:
1558 * Determined recovery action
1559 */
1560static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1561 const struct ata_taskfile *tf)
1562{
1563 unsigned int tmp, action = 0;
1564 u8 stat = tf->command, err = tf->feature;
1565
1566 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1567 qc->err_mask |= AC_ERR_HSM;
Tejun Heocf480622008-01-24 00:05:14 +09001568 return ATA_EH_RESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001569 }
1570
Hannes Reineckee87fd282016-04-04 11:43:55 +02001571 if (stat & (ATA_ERR | ATA_DF)) {
Tejun Heoa51d6442007-03-20 15:24:11 +09001572 qc->err_mask |= AC_ERR_DEV;
Hannes Reineckee87fd282016-04-04 11:43:55 +02001573 /*
1574 * Sense data reporting does not work if the
1575 * device fault bit is set.
1576 */
1577 if (stat & ATA_DF)
1578 stat &= ~ATA_SENSE;
1579 } else {
Tejun Heo022bdb02006-05-15 20:58:22 +09001580 return 0;
Hannes Reineckee87fd282016-04-04 11:43:55 +02001581 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001582
1583 switch (qc->dev->class) {
Hannes Reinecke9162c652014-11-05 13:08:21 +01001584 case ATA_DEV_ZAC:
Hannes Reineckee87fd282016-04-04 11:43:55 +02001585 if (stat & ATA_SENSE)
1586 ata_eh_request_sense(qc, qc->scsicmd);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001587 fallthrough;
Tejun Heoca156e02019-06-24 09:32:50 -07001588 case ATA_DEV_ATA:
Tejun Heo022bdb02006-05-15 20:58:22 +09001589 if (err & ATA_ICRC)
1590 qc->err_mask |= AC_ERR_ATA_BUS;
Alexey Asemoveec7e1c2014-07-15 10:28:42 +04001591 if (err & (ATA_UNC | ATA_AMNF))
Tejun Heo022bdb02006-05-15 20:58:22 +09001592 qc->err_mask |= AC_ERR_MEDIA;
1593 if (err & ATA_IDNF)
1594 qc->err_mask |= AC_ERR_INVALID;
1595 break;
1596
1597 case ATA_DEV_ATAPI:
Tejun Heoa569a302006-11-21 10:40:51 +09001598 if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
Tejun Heo3eabddb2008-06-10 18:28:05 +09001599 tmp = atapi_eh_request_sense(qc->dev,
1600 qc->scsicmd->sense_buffer,
1601 qc->result_tf.feature >> 4);
Hannes Reinecke3852e372016-04-04 11:44:01 +02001602 if (!tmp)
Tejun Heoa569a302006-11-21 10:40:51 +09001603 qc->flags |= ATA_QCFLAG_SENSE_VALID;
Hannes Reinecke3852e372016-04-04 11:44:01 +02001604 else
Tejun Heoa569a302006-11-21 10:40:51 +09001605 qc->err_mask |= tmp;
1606 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001607 }
1608
Hannes Reinecke3852e372016-04-04 11:44:01 +02001609 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
Bart Van Asscheb8e162f2021-04-15 15:08:11 -07001610 enum scsi_disposition ret = scsi_check_sense(qc->scsicmd);
Hannes Reinecke3852e372016-04-04 11:44:01 +02001611 /*
Damien Le Moal79487252018-05-09 09:28:08 +09001612 * SUCCESS here means that the sense code could be
Hannes Reinecke3852e372016-04-04 11:44:01 +02001613 * evaluated and should be passed to the upper layers
1614 * for correct evaluation.
Damien Le Moal79487252018-05-09 09:28:08 +09001615 * FAILED means the sense code could not be interpreted
Hannes Reinecke3852e372016-04-04 11:44:01 +02001616 * and the device would need to be reset.
1617 * NEEDS_RETRY and ADD_TO_MLQUEUE means that the
1618 * command would need to be retried.
1619 */
1620 if (ret == NEEDS_RETRY || ret == ADD_TO_MLQUEUE) {
1621 qc->flags |= ATA_QCFLAG_RETRY;
1622 qc->err_mask |= AC_ERR_OTHER;
1623 } else if (ret != SUCCESS) {
1624 qc->err_mask |= AC_ERR_HSM;
1625 }
1626 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001627 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
Tejun Heocf480622008-01-24 00:05:14 +09001628 action |= ATA_EH_RESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001629
1630 return action;
1631}
1632
Tejun Heo76326ac2007-11-27 19:28:59 +09001633static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask,
1634 int *xfer_ok)
Tejun Heo022bdb02006-05-15 20:58:22 +09001635{
Tejun Heo76326ac2007-11-27 19:28:59 +09001636 int base = 0;
1637
1638 if (!(eflags & ATA_EFLAG_DUBIOUS_XFER))
1639 *xfer_ok = 1;
1640
1641 if (!*xfer_ok)
Tejun Heo75f9caf2008-01-03 01:21:14 +09001642 base = ATA_ECAT_DUBIOUS_NONE;
Tejun Heo76326ac2007-11-27 19:28:59 +09001643
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001644 if (err_mask & AC_ERR_ATA_BUS)
Tejun Heo76326ac2007-11-27 19:28:59 +09001645 return base + ATA_ECAT_ATA_BUS;
Tejun Heo022bdb02006-05-15 20:58:22 +09001646
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001647 if (err_mask & AC_ERR_TIMEOUT)
Tejun Heo76326ac2007-11-27 19:28:59 +09001648 return base + ATA_ECAT_TOUT_HSM;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001649
Tejun Heo3884f7b2007-11-27 19:28:56 +09001650 if (eflags & ATA_EFLAG_IS_IO) {
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001651 if (err_mask & AC_ERR_HSM)
Tejun Heo76326ac2007-11-27 19:28:59 +09001652 return base + ATA_ECAT_TOUT_HSM;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001653 if ((err_mask &
1654 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
Tejun Heo76326ac2007-11-27 19:28:59 +09001655 return base + ATA_ECAT_UNK_DEV;
Tejun Heo022bdb02006-05-15 20:58:22 +09001656 }
1657
1658 return 0;
1659}
1660
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001661struct speed_down_verdict_arg {
Tejun Heo022bdb02006-05-15 20:58:22 +09001662 u64 since;
Tejun Heo76326ac2007-11-27 19:28:59 +09001663 int xfer_ok;
Tejun Heo3884f7b2007-11-27 19:28:56 +09001664 int nr_errors[ATA_ECAT_NR];
Tejun Heo022bdb02006-05-15 20:58:22 +09001665};
1666
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001667static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
Tejun Heo022bdb02006-05-15 20:58:22 +09001668{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001669 struct speed_down_verdict_arg *arg = void_arg;
Tejun Heo76326ac2007-11-27 19:28:59 +09001670 int cat;
Tejun Heo022bdb02006-05-15 20:58:22 +09001671
Gwendal Grignoud9027472010-05-25 12:31:38 -07001672 if ((ent->eflags & ATA_EFLAG_OLD_ER) || (ent->timestamp < arg->since))
Tejun Heo022bdb02006-05-15 20:58:22 +09001673 return -1;
1674
Tejun Heo76326ac2007-11-27 19:28:59 +09001675 cat = ata_eh_categorize_error(ent->eflags, ent->err_mask,
1676 &arg->xfer_ok);
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001677 arg->nr_errors[cat]++;
Tejun Heo76326ac2007-11-27 19:28:59 +09001678
Tejun Heo022bdb02006-05-15 20:58:22 +09001679 return 0;
1680}
1681
1682/**
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001683 * ata_eh_speed_down_verdict - Determine speed down verdict
Tejun Heo022bdb02006-05-15 20:58:22 +09001684 * @dev: Device of interest
1685 *
1686 * This function examines error ring of @dev and determines
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001687 * whether NCQ needs to be turned off, transfer speed should be
1688 * stepped down, or falling back to PIO is necessary.
Tejun Heo022bdb02006-05-15 20:58:22 +09001689 *
Tejun Heo3884f7b2007-11-27 19:28:56 +09001690 * ECAT_ATA_BUS : ATA_BUS error for any command
Tejun Heo022bdb02006-05-15 20:58:22 +09001691 *
Tejun Heo3884f7b2007-11-27 19:28:56 +09001692 * ECAT_TOUT_HSM : TIMEOUT for any command or HSM violation for
1693 * IO commands
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001694 *
Tejun Heo3884f7b2007-11-27 19:28:56 +09001695 * ECAT_UNK_DEV : Unknown DEV error for IO commands
Tejun Heo022bdb02006-05-15 20:58:22 +09001696 *
Tejun Heo76326ac2007-11-27 19:28:59 +09001697 * ECAT_DUBIOUS_* : Identical to above three but occurred while
1698 * data transfer hasn't been verified.
1699 *
Tejun Heo3884f7b2007-11-27 19:28:56 +09001700 * Verdicts are
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001701 *
Tejun Heo3884f7b2007-11-27 19:28:56 +09001702 * NCQ_OFF : Turn off NCQ.
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001703 *
Tejun Heo3884f7b2007-11-27 19:28:56 +09001704 * SPEED_DOWN : Speed down transfer speed but don't fall back
1705 * to PIO.
1706 *
1707 * FALLBACK_TO_PIO : Fall back to PIO.
1708 *
1709 * Even if multiple verdicts are returned, only one action is
Tejun Heo76326ac2007-11-27 19:28:59 +09001710 * taken per error. An action triggered by non-DUBIOUS errors
1711 * clears ering, while one triggered by DUBIOUS_* errors doesn't.
1712 * This is to expedite speed down decisions right after device is
1713 * initially configured.
Tejun Heo3884f7b2007-11-27 19:28:56 +09001714 *
Masahiro Yamada4091fb92017-02-27 14:29:56 -08001715 * The following are speed down rules. #1 and #2 deal with
Tejun Heo76326ac2007-11-27 19:28:59 +09001716 * DUBIOUS errors.
1717 *
1718 * 1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors
1719 * occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO.
1720 *
1721 * 2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors
1722 * occurred during last 5 mins, NCQ_OFF.
1723 *
1724 * 3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001725 * occurred during last 5 mins, FALLBACK_TO_PIO
Tejun Heo3884f7b2007-11-27 19:28:56 +09001726 *
Tejun Heo76326ac2007-11-27 19:28:59 +09001727 * 4. If more than 3 TOUT_HSM or UNK_DEV errors occurred
Tejun Heo3884f7b2007-11-27 19:28:56 +09001728 * during last 10 mins, NCQ_OFF.
1729 *
Tejun Heo76326ac2007-11-27 19:28:59 +09001730 * 5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6
Tejun Heo3884f7b2007-11-27 19:28:56 +09001731 * UNK_DEV errors occurred during last 10 mins, SPEED_DOWN.
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001732 *
Tejun Heo022bdb02006-05-15 20:58:22 +09001733 * LOCKING:
1734 * Inherited from caller.
1735 *
1736 * RETURNS:
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001737 * OR of ATA_EH_SPDN_* flags.
Tejun Heo022bdb02006-05-15 20:58:22 +09001738 */
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001739static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
Tejun Heo022bdb02006-05-15 20:58:22 +09001740{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001741 const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
1742 u64 j64 = get_jiffies_64();
1743 struct speed_down_verdict_arg arg;
1744 unsigned int verdict = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09001745
Tejun Heo3884f7b2007-11-27 19:28:56 +09001746 /* scan past 5 mins of error history */
1747 memset(&arg, 0, sizeof(arg));
1748 arg.since = j64 - min(j64, j5mins);
1749 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1750
Tejun Heo76326ac2007-11-27 19:28:59 +09001751 if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] +
1752 arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1)
1753 verdict |= ATA_EH_SPDN_SPEED_DOWN |
1754 ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS;
1755
1756 if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] +
1757 arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1)
1758 verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS;
1759
Tejun Heo3884f7b2007-11-27 19:28:56 +09001760 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1761 arg.nr_errors[ATA_ECAT_TOUT_HSM] +
Tejun Heo663f99b82007-11-27 19:28:57 +09001762 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
Tejun Heo3884f7b2007-11-27 19:28:56 +09001763 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
1764
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001765 /* scan past 10 mins of error history */
Tejun Heo022bdb02006-05-15 20:58:22 +09001766 memset(&arg, 0, sizeof(arg));
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001767 arg.since = j64 - min(j64, j10mins);
1768 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
Tejun Heo022bdb02006-05-15 20:58:22 +09001769
Tejun Heo3884f7b2007-11-27 19:28:56 +09001770 if (arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1771 arg.nr_errors[ATA_ECAT_UNK_DEV] > 3)
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001772 verdict |= ATA_EH_SPDN_NCQ_OFF;
Tejun Heo3884f7b2007-11-27 19:28:56 +09001773
1774 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1775 arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 ||
Tejun Heo663f99b82007-11-27 19:28:57 +09001776 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001777 verdict |= ATA_EH_SPDN_SPEED_DOWN;
Tejun Heo022bdb02006-05-15 20:58:22 +09001778
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001779 return verdict;
Tejun Heo022bdb02006-05-15 20:58:22 +09001780}
1781
1782/**
1783 * ata_eh_speed_down - record error and speed down if necessary
1784 * @dev: Failed device
Tejun Heo3884f7b2007-11-27 19:28:56 +09001785 * @eflags: mask of ATA_EFLAG_* flags
Tejun Heo022bdb02006-05-15 20:58:22 +09001786 * @err_mask: err_mask of the error
1787 *
1788 * Record error and examine error history to determine whether
1789 * adjusting transmission speed is necessary. It also sets
1790 * transmission limits appropriately if such adjustment is
1791 * necessary.
1792 *
1793 * LOCKING:
1794 * Kernel thread context (may sleep).
1795 *
1796 * RETURNS:
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001797 * Determined recovery action.
Tejun Heo022bdb02006-05-15 20:58:22 +09001798 */
Tejun Heo3884f7b2007-11-27 19:28:56 +09001799static unsigned int ata_eh_speed_down(struct ata_device *dev,
1800 unsigned int eflags, unsigned int err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001801{
Tejun Heob1c72912008-07-31 17:02:43 +09001802 struct ata_link *link = ata_dev_phys_link(dev);
Tejun Heo76326ac2007-11-27 19:28:59 +09001803 int xfer_ok = 0;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001804 unsigned int verdict;
1805 unsigned int action = 0;
1806
1807 /* don't bother if Cat-0 error */
Tejun Heo76326ac2007-11-27 19:28:59 +09001808 if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0)
Tejun Heo022bdb02006-05-15 20:58:22 +09001809 return 0;
1810
1811 /* record error and determine whether speed down is necessary */
Tejun Heo3884f7b2007-11-27 19:28:56 +09001812 ata_ering_record(&dev->ering, eflags, err_mask);
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001813 verdict = ata_eh_speed_down_verdict(dev);
Tejun Heo022bdb02006-05-15 20:58:22 +09001814
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001815 /* turn off NCQ? */
1816 if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
1817 (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
1818 ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
1819 dev->flags |= ATA_DFLAG_NCQ_OFF;
Joe Perchesa9a79df2011-04-15 15:51:59 -07001820 ata_dev_warn(dev, "NCQ disabled due to excessive errors\n");
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001821 goto done;
1822 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001823
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001824 /* speed down? */
1825 if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1826 /* speed down SATA link speed if possible */
Tejun Heoa07d4992009-01-29 20:31:33 +09001827 if (sata_down_spd_limit(link, 0) == 0) {
Tejun Heocf480622008-01-24 00:05:14 +09001828 action |= ATA_EH_RESET;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001829 goto done;
1830 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001831
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001832 /* lower transfer mode */
1833 if (dev->spdn_cnt < 2) {
1834 static const int dma_dnxfer_sel[] =
1835 { ATA_DNXFER_DMA, ATA_DNXFER_40C };
1836 static const int pio_dnxfer_sel[] =
1837 { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
1838 int sel;
Tejun Heo022bdb02006-05-15 20:58:22 +09001839
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001840 if (dev->xfer_shift != ATA_SHIFT_PIO)
1841 sel = dma_dnxfer_sel[dev->spdn_cnt];
1842 else
1843 sel = pio_dnxfer_sel[dev->spdn_cnt];
1844
1845 dev->spdn_cnt++;
1846
1847 if (ata_down_xfermask_limit(dev, sel) == 0) {
Tejun Heocf480622008-01-24 00:05:14 +09001848 action |= ATA_EH_RESET;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001849 goto done;
1850 }
1851 }
1852 }
1853
1854 /* Fall back to PIO? Slowing down to PIO is meaningless for
Tejun Heo663f99b82007-11-27 19:28:57 +09001855 * SATA ATA devices. Consider it only for PATA and SATAPI.
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001856 */
1857 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
Tejun Heo663f99b82007-11-27 19:28:57 +09001858 (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) &&
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001859 (dev->xfer_shift != ATA_SHIFT_PIO)) {
1860 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
1861 dev->spdn_cnt = 0;
Tejun Heocf480622008-01-24 00:05:14 +09001862 action |= ATA_EH_RESET;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001863 goto done;
1864 }
1865 }
1866
Tejun Heo022bdb02006-05-15 20:58:22 +09001867 return 0;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001868 done:
1869 /* device has been slowed down, blow error history */
Tejun Heo76326ac2007-11-27 19:28:59 +09001870 if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS))
1871 ata_ering_clear(&dev->ering);
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001872 return action;
Tejun Heo022bdb02006-05-15 20:58:22 +09001873}
1874
1875/**
Mark Lord8d899e72012-05-02 15:22:52 -04001876 * ata_eh_worth_retry - analyze error and decide whether to retry
1877 * @qc: qc to possibly retry
1878 *
1879 * Look at the cause of the error and decide if a retry
1880 * might be useful or not. We don't want to retry media errors
1881 * because the drive itself has probably already taken 10-30 seconds
1882 * doing its own internal retries before reporting the failure.
1883 */
1884static inline int ata_eh_worth_retry(struct ata_queued_cmd *qc)
1885{
Bian Yu1eaca392012-12-12 22:26:58 -05001886 if (qc->err_mask & AC_ERR_MEDIA)
Mark Lord8d899e72012-05-02 15:22:52 -04001887 return 0; /* don't retry media errors */
1888 if (qc->flags & ATA_QCFLAG_IO)
1889 return 1; /* otherwise retry anything from fs stack */
1890 if (qc->err_mask & AC_ERR_INVALID)
1891 return 0; /* don't retry these */
1892 return qc->err_mask != AC_ERR_DEV; /* retry if not dev error */
1893}
1894
1895/**
Damien Le Moal7eb49502018-05-09 09:28:11 +09001896 * ata_eh_quiet - check if we need to be quiet about a command error
1897 * @qc: qc to check
1898 *
1899 * Look at the qc flags anbd its scsi command request flags to determine
1900 * if we need to be quiet about the command failure.
1901 */
1902static inline bool ata_eh_quiet(struct ata_queued_cmd *qc)
1903{
Bart Van Asschec8329cd2021-08-09 16:03:10 -07001904 if (qc->scsicmd && scsi_cmd_to_rq(qc->scsicmd)->rq_flags & RQF_QUIET)
Damien Le Moal7eb49502018-05-09 09:28:11 +09001905 qc->flags |= ATA_QCFLAG_QUIET;
1906 return qc->flags & ATA_QCFLAG_QUIET;
1907}
1908
1909/**
Tejun Heo9b1e2652007-08-06 18:36:24 +09001910 * ata_eh_link_autopsy - analyze error and determine recovery action
1911 * @link: host link to perform autopsy on
Tejun Heo022bdb02006-05-15 20:58:22 +09001912 *
Tejun Heo02607312007-08-06 18:36:23 +09001913 * Analyze why @link failed and determine which recovery actions
1914 * are needed. This function also sets more detailed AC_ERR_*
1915 * values and fills sense data for ATAPI CHECK SENSE.
Tejun Heo022bdb02006-05-15 20:58:22 +09001916 *
1917 * LOCKING:
1918 * Kernel thread context (may sleep).
1919 */
Tejun Heo9b1e2652007-08-06 18:36:24 +09001920static void ata_eh_link_autopsy(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001921{
Tejun Heo02607312007-08-06 18:36:23 +09001922 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09001923 struct ata_eh_context *ehc = &link->eh_context;
Jens Axboe258c4e52018-06-19 10:12:49 -06001924 struct ata_queued_cmd *qc;
Tejun Heodfcc1732007-10-31 10:17:05 +09001925 struct ata_device *dev;
Tejun Heo3884f7b2007-11-27 19:28:56 +09001926 unsigned int all_err_mask = 0, eflags = 0;
Damien Le Moal7eb49502018-05-09 09:28:11 +09001927 int tag, nr_failed = 0, nr_quiet = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09001928 u32 serror;
1929 int rc;
1930
1931 DPRINTK("ENTER\n");
1932
Tejun Heo1cdaf532006-07-03 16:07:26 +09001933 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1934 return;
1935
Tejun Heo022bdb02006-05-15 20:58:22 +09001936 /* obtain and analyze SError */
Tejun Heo936fd732007-08-06 18:36:23 +09001937 rc = sata_scr_read(link, SCR_ERROR, &serror);
Tejun Heo022bdb02006-05-15 20:58:22 +09001938 if (rc == 0) {
1939 ehc->i.serror |= serror;
Tejun Heo02607312007-08-06 18:36:23 +09001940 ata_eh_analyze_serror(link);
Tejun Heo4e57c512007-07-16 14:29:41 +09001941 } else if (rc != -EOPNOTSUPP) {
Tejun Heocf480622008-01-24 00:05:14 +09001942 /* SError read failed, force reset and probing */
Tejun Heob558edd2008-01-24 00:05:14 +09001943 ehc->i.probe_mask |= ATA_ALL_DEVICES;
Tejun Heocf480622008-01-24 00:05:14 +09001944 ehc->i.action |= ATA_EH_RESET;
Tejun Heo4e57c512007-07-16 14:29:41 +09001945 ehc->i.err_mask |= AC_ERR_OTHER;
1946 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001947
Tejun Heoe8ee8452006-05-15 21:03:46 +09001948 /* analyze NCQ failure */
Tejun Heo02607312007-08-06 18:36:23 +09001949 ata_eh_analyze_ncq_error(link);
Tejun Heoe8ee8452006-05-15 21:03:46 +09001950
Tejun Heo022bdb02006-05-15 20:58:22 +09001951 /* any real error trumps AC_ERR_OTHER */
1952 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1953 ehc->i.err_mask &= ~AC_ERR_OTHER;
1954
1955 all_err_mask |= ehc->i.err_mask;
1956
Jens Axboe258c4e52018-06-19 10:12:49 -06001957 ata_qc_for_each_raw(ap, qc, tag) {
Tejun Heob1c72912008-07-31 17:02:43 +09001958 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
1959 ata_dev_phys_link(qc->dev) != link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001960 continue;
1961
1962 /* inherit upper level err_mask */
1963 qc->err_mask |= ehc->i.err_mask;
1964
Tejun Heo022bdb02006-05-15 20:58:22 +09001965 /* analyze TF */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001966 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
Tejun Heo022bdb02006-05-15 20:58:22 +09001967
1968 /* DEV errors are probably spurious in case of ATA_BUS error */
1969 if (qc->err_mask & AC_ERR_ATA_BUS)
1970 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1971 AC_ERR_INVALID);
1972
1973 /* any real error trumps unknown error */
1974 if (qc->err_mask & ~AC_ERR_OTHER)
1975 qc->err_mask &= ~AC_ERR_OTHER;
1976
Damien Le Moal804689a2018-05-09 09:28:12 +09001977 /*
1978 * SENSE_VALID trumps dev/unknown error and revalidation. Upper
1979 * layers will determine whether the command is worth retrying
1980 * based on the sense data and device class/type. Otherwise,
1981 * determine directly if the command is worth retrying using its
1982 * error mask and flags.
1983 */
Tejun Heof90f0822007-10-26 16:12:41 +09001984 if (qc->flags & ATA_QCFLAG_SENSE_VALID)
Tejun Heo022bdb02006-05-15 20:58:22 +09001985 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
Damien Le Moal804689a2018-05-09 09:28:12 +09001986 else if (ata_eh_worth_retry(qc))
Tejun Heo03faab72008-03-27 19:14:24 +09001987 qc->flags |= ATA_QCFLAG_RETRY;
1988
Tejun Heo022bdb02006-05-15 20:58:22 +09001989 /* accumulate error info */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001990 ehc->i.dev = qc->dev;
Tejun Heo022bdb02006-05-15 20:58:22 +09001991 all_err_mask |= qc->err_mask;
1992 if (qc->flags & ATA_QCFLAG_IO)
Tejun Heo3884f7b2007-11-27 19:28:56 +09001993 eflags |= ATA_EFLAG_IS_IO;
Hannes Reinecke255c03d2015-03-27 16:46:38 +01001994 trace_ata_eh_link_autopsy_qc(qc);
Damien Le Moal7eb49502018-05-09 09:28:11 +09001995
1996 /* Count quiet errors */
1997 if (ata_eh_quiet(qc))
1998 nr_quiet++;
1999 nr_failed++;
Tejun Heo022bdb02006-05-15 20:58:22 +09002000 }
2001
Damien Le Moal7eb49502018-05-09 09:28:11 +09002002 /* If all failed commands requested silence, then be quiet */
2003 if (nr_quiet == nr_failed)
2004 ehc->i.flags |= ATA_EHI_QUIET;
2005
Tejun Heoa20f33f2006-05-16 12:58:24 +09002006 /* enforce default EH actions */
Tejun Heob51e9e52006-06-29 01:29:30 +09002007 if (ap->pflags & ATA_PFLAG_FROZEN ||
Tejun Heoa20f33f2006-05-16 12:58:24 +09002008 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
Tejun Heocf480622008-01-24 00:05:14 +09002009 ehc->i.action |= ATA_EH_RESET;
Tejun Heo3884f7b2007-11-27 19:28:56 +09002010 else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) ||
2011 (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV)))
Tejun Heo4528e4d2006-07-08 20:17:26 +09002012 ehc->i.action |= ATA_EH_REVALIDATE;
Tejun Heo022bdb02006-05-15 20:58:22 +09002013
Tejun Heodfcc1732007-10-31 10:17:05 +09002014 /* If we have offending qcs and the associated failed device,
2015 * perform per-dev EH action only on the offending device.
2016 */
Tejun Heo4528e4d2006-07-08 20:17:26 +09002017 if (ehc->i.dev) {
Tejun Heo4528e4d2006-07-08 20:17:26 +09002018 ehc->i.dev_action[ehc->i.dev->devno] |=
2019 ehc->i.action & ATA_EH_PERDEV_MASK;
2020 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
Tejun Heo47005f22006-06-19 18:27:23 +09002021 }
2022
Tejun Heo2695e362008-01-10 13:41:23 +09002023 /* propagate timeout to host link */
2024 if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link))
2025 ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT;
2026
2027 /* record error and consider speeding down */
Tejun Heodfcc1732007-10-31 10:17:05 +09002028 dev = ehc->i.dev;
Tejun Heo2695e362008-01-10 13:41:23 +09002029 if (!dev && ((ata_link_max_devices(link) == 1 &&
2030 ata_dev_enabled(link->device))))
2031 dev = link->device;
Tejun Heodfcc1732007-10-31 10:17:05 +09002032
Tejun Heo76326ac2007-11-27 19:28:59 +09002033 if (dev) {
2034 if (dev->flags & ATA_DFLAG_DUBIOUS_XFER)
2035 eflags |= ATA_EFLAG_DUBIOUS_XFER;
Tejun Heo3884f7b2007-11-27 19:28:56 +09002036 ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask);
Rameshwar Prasad Sahuf1601112017-11-02 16:31:07 +05302037 trace_ata_eh_link_autopsy(dev, ehc->i.action, all_err_mask);
Tejun Heo76326ac2007-11-27 19:28:59 +09002038 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002039 DPRINTK("EXIT\n");
2040}
2041
2042/**
Tejun Heo9b1e2652007-08-06 18:36:24 +09002043 * ata_eh_autopsy - analyze error and determine recovery action
2044 * @ap: host port to perform autopsy on
2045 *
2046 * Analyze all links of @ap and determine why they failed and
2047 * which recovery actions are needed.
2048 *
2049 * LOCKING:
2050 * Kernel thread context (may sleep).
2051 */
Tejun Heofb7fd612007-09-23 13:14:12 +09002052void ata_eh_autopsy(struct ata_port *ap)
Tejun Heo9b1e2652007-08-06 18:36:24 +09002053{
2054 struct ata_link *link;
2055
Tejun Heo1eca4362008-11-03 20:03:17 +09002056 ata_for_each_link(link, ap, EDGE)
Tejun Heo9b1e2652007-08-06 18:36:24 +09002057 ata_eh_link_autopsy(link);
Tejun Heo2695e362008-01-10 13:41:23 +09002058
Tejun Heob1c72912008-07-31 17:02:43 +09002059 /* Handle the frigging slave link. Autopsy is done similarly
2060 * but actions and flags are transferred over to the master
2061 * link and handled from there.
2062 */
2063 if (ap->slave_link) {
2064 struct ata_eh_context *mehc = &ap->link.eh_context;
2065 struct ata_eh_context *sehc = &ap->slave_link->eh_context;
2066
Tejun Heo848e4c62008-10-21 14:26:39 +09002067 /* transfer control flags from master to slave */
2068 sehc->i.flags |= mehc->i.flags & ATA_EHI_TO_SLAVE_MASK;
2069
2070 /* perform autopsy on the slave link */
Tejun Heob1c72912008-07-31 17:02:43 +09002071 ata_eh_link_autopsy(ap->slave_link);
2072
Tejun Heo848e4c62008-10-21 14:26:39 +09002073 /* transfer actions from slave to master and clear slave */
Tejun Heob1c72912008-07-31 17:02:43 +09002074 ata_eh_about_to_do(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
2075 mehc->i.action |= sehc->i.action;
2076 mehc->i.dev_action[1] |= sehc->i.dev_action[1];
2077 mehc->i.flags |= sehc->i.flags;
2078 ata_eh_done(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
2079 }
2080
Tejun Heo2695e362008-01-10 13:41:23 +09002081 /* Autopsy of fanout ports can affect host link autopsy.
2082 * Perform host link autopsy last.
2083 */
Tejun Heo071f44b2008-04-07 22:47:22 +09002084 if (sata_pmp_attached(ap))
Tejun Heo2695e362008-01-10 13:41:23 +09002085 ata_eh_link_autopsy(&ap->link);
Tejun Heo9b1e2652007-08-06 18:36:24 +09002086}
2087
2088/**
Robert Hancock65211482009-07-14 20:43:39 -06002089 * ata_get_cmd_descript - get description for ATA command
2090 * @command: ATA command code to get description for
2091 *
2092 * Return a textual description of the given command, or NULL if the
2093 * command is not known.
2094 *
2095 * LOCKING:
2096 * None
2097 */
2098const char *ata_get_cmd_descript(u8 command)
2099{
2100#ifdef CONFIG_ATA_VERBOSE_ERROR
2101 static const struct
2102 {
2103 u8 command;
2104 const char *text;
2105 } cmd_descr[] = {
2106 { ATA_CMD_DEV_RESET, "DEVICE RESET" },
Hannes Reinecke825e2d82015-03-27 16:46:31 +01002107 { ATA_CMD_CHK_POWER, "CHECK POWER MODE" },
2108 { ATA_CMD_STANDBY, "STANDBY" },
2109 { ATA_CMD_IDLE, "IDLE" },
2110 { ATA_CMD_EDD, "EXECUTE DEVICE DIAGNOSTIC" },
2111 { ATA_CMD_DOWNLOAD_MICRO, "DOWNLOAD MICROCODE" },
Robert Hancock3915c3b2013-10-21 19:26:30 -06002112 { ATA_CMD_DOWNLOAD_MICRO_DMA, "DOWNLOAD MICROCODE DMA" },
Robert Hancock65211482009-07-14 20:43:39 -06002113 { ATA_CMD_NOP, "NOP" },
Hannes Reinecke825e2d82015-03-27 16:46:31 +01002114 { ATA_CMD_FLUSH, "FLUSH CACHE" },
2115 { ATA_CMD_FLUSH_EXT, "FLUSH CACHE EXT" },
2116 { ATA_CMD_ID_ATA, "IDENTIFY DEVICE" },
2117 { ATA_CMD_ID_ATAPI, "IDENTIFY PACKET DEVICE" },
2118 { ATA_CMD_SERVICE, "SERVICE" },
2119 { ATA_CMD_READ, "READ DMA" },
2120 { ATA_CMD_READ_EXT, "READ DMA EXT" },
2121 { ATA_CMD_READ_QUEUED, "READ DMA QUEUED" },
2122 { ATA_CMD_READ_STREAM_EXT, "READ STREAM EXT" },
Robert Hancock65211482009-07-14 20:43:39 -06002123 { ATA_CMD_READ_STREAM_DMA_EXT, "READ STREAM DMA EXT" },
Hannes Reinecke825e2d82015-03-27 16:46:31 +01002124 { ATA_CMD_WRITE, "WRITE DMA" },
2125 { ATA_CMD_WRITE_EXT, "WRITE DMA EXT" },
2126 { ATA_CMD_WRITE_QUEUED, "WRITE DMA QUEUED EXT" },
2127 { ATA_CMD_WRITE_STREAM_EXT, "WRITE STREAM EXT" },
Robert Hancock65211482009-07-14 20:43:39 -06002128 { ATA_CMD_WRITE_STREAM_DMA_EXT, "WRITE STREAM DMA EXT" },
2129 { ATA_CMD_WRITE_FUA_EXT, "WRITE DMA FUA EXT" },
2130 { ATA_CMD_WRITE_QUEUED_FUA_EXT, "WRITE DMA QUEUED FUA EXT" },
2131 { ATA_CMD_FPDMA_READ, "READ FPDMA QUEUED" },
2132 { ATA_CMD_FPDMA_WRITE, "WRITE FPDMA QUEUED" },
Robert Hancock3915c3b2013-10-21 19:26:30 -06002133 { ATA_CMD_FPDMA_SEND, "SEND FPDMA QUEUED" },
2134 { ATA_CMD_FPDMA_RECV, "RECEIVE FPDMA QUEUED" },
Robert Hancock65211482009-07-14 20:43:39 -06002135 { ATA_CMD_PIO_READ, "READ SECTOR(S)" },
2136 { ATA_CMD_PIO_READ_EXT, "READ SECTOR(S) EXT" },
2137 { ATA_CMD_PIO_WRITE, "WRITE SECTOR(S)" },
2138 { ATA_CMD_PIO_WRITE_EXT, "WRITE SECTOR(S) EXT" },
2139 { ATA_CMD_READ_MULTI, "READ MULTIPLE" },
2140 { ATA_CMD_READ_MULTI_EXT, "READ MULTIPLE EXT" },
2141 { ATA_CMD_WRITE_MULTI, "WRITE MULTIPLE" },
2142 { ATA_CMD_WRITE_MULTI_EXT, "WRITE MULTIPLE EXT" },
Hannes Reinecke825e2d82015-03-27 16:46:31 +01002143 { ATA_CMD_WRITE_MULTI_FUA_EXT, "WRITE MULTIPLE FUA EXT" },
Robert Hancock65211482009-07-14 20:43:39 -06002144 { ATA_CMD_SET_FEATURES, "SET FEATURES" },
2145 { ATA_CMD_SET_MULTI, "SET MULTIPLE MODE" },
2146 { ATA_CMD_VERIFY, "READ VERIFY SECTOR(S)" },
2147 { ATA_CMD_VERIFY_EXT, "READ VERIFY SECTOR(S) EXT" },
2148 { ATA_CMD_WRITE_UNCORR_EXT, "WRITE UNCORRECTABLE EXT" },
2149 { ATA_CMD_STANDBYNOW1, "STANDBY IMMEDIATE" },
2150 { ATA_CMD_IDLEIMMEDIATE, "IDLE IMMEDIATE" },
2151 { ATA_CMD_SLEEP, "SLEEP" },
2152 { ATA_CMD_INIT_DEV_PARAMS, "INITIALIZE DEVICE PARAMETERS" },
2153 { ATA_CMD_READ_NATIVE_MAX, "READ NATIVE MAX ADDRESS" },
2154 { ATA_CMD_READ_NATIVE_MAX_EXT, "READ NATIVE MAX ADDRESS EXT" },
2155 { ATA_CMD_SET_MAX, "SET MAX ADDRESS" },
2156 { ATA_CMD_SET_MAX_EXT, "SET MAX ADDRESS EXT" },
2157 { ATA_CMD_READ_LOG_EXT, "READ LOG EXT" },
2158 { ATA_CMD_WRITE_LOG_EXT, "WRITE LOG EXT" },
2159 { ATA_CMD_READ_LOG_DMA_EXT, "READ LOG DMA EXT" },
Hannes Reinecke825e2d82015-03-27 16:46:31 +01002160 { ATA_CMD_WRITE_LOG_DMA_EXT, "WRITE LOG DMA EXT" },
Robert Hancock3915c3b2013-10-21 19:26:30 -06002161 { ATA_CMD_TRUSTED_NONDATA, "TRUSTED NON-DATA" },
Robert Hancock65211482009-07-14 20:43:39 -06002162 { ATA_CMD_TRUSTED_RCV, "TRUSTED RECEIVE" },
Hannes Reinecke825e2d82015-03-27 16:46:31 +01002163 { ATA_CMD_TRUSTED_RCV_DMA, "TRUSTED RECEIVE DMA" },
Robert Hancock65211482009-07-14 20:43:39 -06002164 { ATA_CMD_TRUSTED_SND, "TRUSTED SEND" },
Hannes Reinecke825e2d82015-03-27 16:46:31 +01002165 { ATA_CMD_TRUSTED_SND_DMA, "TRUSTED SEND DMA" },
Robert Hancock65211482009-07-14 20:43:39 -06002166 { ATA_CMD_PMP_READ, "READ BUFFER" },
Robert Hancock3915c3b2013-10-21 19:26:30 -06002167 { ATA_CMD_PMP_READ_DMA, "READ BUFFER DMA" },
Robert Hancock65211482009-07-14 20:43:39 -06002168 { ATA_CMD_PMP_WRITE, "WRITE BUFFER" },
Robert Hancock3915c3b2013-10-21 19:26:30 -06002169 { ATA_CMD_PMP_WRITE_DMA, "WRITE BUFFER DMA" },
Robert Hancock65211482009-07-14 20:43:39 -06002170 { ATA_CMD_CONF_OVERLAY, "DEVICE CONFIGURATION OVERLAY" },
2171 { ATA_CMD_SEC_SET_PASS, "SECURITY SET PASSWORD" },
2172 { ATA_CMD_SEC_UNLOCK, "SECURITY UNLOCK" },
2173 { ATA_CMD_SEC_ERASE_PREP, "SECURITY ERASE PREPARE" },
2174 { ATA_CMD_SEC_ERASE_UNIT, "SECURITY ERASE UNIT" },
2175 { ATA_CMD_SEC_FREEZE_LOCK, "SECURITY FREEZE LOCK" },
2176 { ATA_CMD_SEC_DISABLE_PASS, "SECURITY DISABLE PASSWORD" },
2177 { ATA_CMD_CONFIG_STREAM, "CONFIGURE STREAM" },
2178 { ATA_CMD_SMART, "SMART" },
2179 { ATA_CMD_MEDIA_LOCK, "DOOR LOCK" },
2180 { ATA_CMD_MEDIA_UNLOCK, "DOOR UNLOCK" },
FUJITA Tomonoriacad762722010-07-05 15:45:20 +09002181 { ATA_CMD_DSM, "DATA SET MANAGEMENT" },
Hannes Reinecke825e2d82015-03-27 16:46:31 +01002182 { ATA_CMD_CHK_MED_CRD_TYP, "CHECK MEDIA CARD TYPE" },
2183 { ATA_CMD_CFA_REQ_EXT_ERR, "CFA REQUEST EXTENDED ERROR" },
Robert Hancock65211482009-07-14 20:43:39 -06002184 { ATA_CMD_CFA_WRITE_NE, "CFA WRITE SECTORS WITHOUT ERASE" },
2185 { ATA_CMD_CFA_TRANS_SECT, "CFA TRANSLATE SECTOR" },
2186 { ATA_CMD_CFA_ERASE, "CFA ERASE SECTORS" },
Hannes Reinecke825e2d82015-03-27 16:46:31 +01002187 { ATA_CMD_CFA_WRITE_MULT_NE, "CFA WRITE MULTIPLE WITHOUT ERASE" },
Robert Hancock3915c3b2013-10-21 19:26:30 -06002188 { ATA_CMD_REQ_SENSE_DATA, "REQUEST SENSE DATA EXT" },
2189 { ATA_CMD_SANITIZE_DEVICE, "SANITIZE DEVICE" },
Hannes Reinecke28a3fc22016-04-25 12:45:52 +02002190 { ATA_CMD_ZAC_MGMT_IN, "ZAC MANAGEMENT IN" },
Hannes Reinecke27708a92016-04-25 12:45:53 +02002191 { ATA_CMD_ZAC_MGMT_OUT, "ZAC MANAGEMENT OUT" },
Robert Hancock65211482009-07-14 20:43:39 -06002192 { ATA_CMD_READ_LONG, "READ LONG (with retries)" },
2193 { ATA_CMD_READ_LONG_ONCE, "READ LONG (without retries)" },
2194 { ATA_CMD_WRITE_LONG, "WRITE LONG (with retries)" },
2195 { ATA_CMD_WRITE_LONG_ONCE, "WRITE LONG (without retries)" },
2196 { ATA_CMD_RESTORE, "RECALIBRATE" },
2197 { 0, NULL } /* terminate list */
2198 };
2199
2200 unsigned int i;
2201 for (i = 0; cmd_descr[i].text; i++)
2202 if (cmd_descr[i].command == command)
2203 return cmd_descr[i].text;
2204#endif
2205
2206 return NULL;
2207}
Andy Shevchenko36aae282014-12-12 17:16:31 +02002208EXPORT_SYMBOL_GPL(ata_get_cmd_descript);
Robert Hancock65211482009-07-14 20:43:39 -06002209
2210/**
Tejun Heo9b1e2652007-08-06 18:36:24 +09002211 * ata_eh_link_report - report error handling to user
Tejun Heo02607312007-08-06 18:36:23 +09002212 * @link: ATA link EH is going on
Tejun Heo022bdb02006-05-15 20:58:22 +09002213 *
2214 * Report EH to user.
2215 *
2216 * LOCKING:
2217 * None.
2218 */
Tejun Heo9b1e2652007-08-06 18:36:24 +09002219static void ata_eh_link_report(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09002220{
Tejun Heo02607312007-08-06 18:36:23 +09002221 struct ata_port *ap = link->ap;
2222 struct ata_eh_context *ehc = &link->eh_context;
Jens Axboe258c4e52018-06-19 10:12:49 -06002223 struct ata_queued_cmd *qc;
Tejun Heo022bdb02006-05-15 20:58:22 +09002224 const char *frozen, *desc;
Levente Kurusa462098b2013-10-29 20:01:46 +01002225 char tries_buf[6] = "";
Tejun Heo022bdb02006-05-15 20:58:22 +09002226 int tag, nr_failed = 0;
2227
Tejun Heo94ff3d52007-10-09 14:57:56 +09002228 if (ehc->i.flags & ATA_EHI_QUIET)
2229 return;
2230
Tejun Heo022bdb02006-05-15 20:58:22 +09002231 desc = NULL;
2232 if (ehc->i.desc[0] != '\0')
2233 desc = ehc->i.desc;
2234
Jens Axboe258c4e52018-06-19 10:12:49 -06002235 ata_qc_for_each_raw(ap, qc, tag) {
Tejun Heob1c72912008-07-31 17:02:43 +09002236 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
2237 ata_dev_phys_link(qc->dev) != link ||
Tejun Heoe027bd32007-10-26 16:19:26 +09002238 ((qc->flags & ATA_QCFLAG_QUIET) &&
2239 qc->err_mask == AC_ERR_DEV))
Tejun Heo022bdb02006-05-15 20:58:22 +09002240 continue;
2241 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
2242 continue;
2243
2244 nr_failed++;
2245 }
2246
2247 if (!nr_failed && !ehc->i.err_mask)
2248 return;
2249
2250 frozen = "";
Tejun Heob51e9e52006-06-29 01:29:30 +09002251 if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heo022bdb02006-05-15 20:58:22 +09002252 frozen = " frozen";
2253
Tejun Heoa1e10f72007-08-18 13:28:49 +09002254 if (ap->eh_tries < ATA_EH_MAX_TRIES)
Levente Kurusa462098b2013-10-29 20:01:46 +01002255 snprintf(tries_buf, sizeof(tries_buf), " t%d",
Tejun Heoa1e10f72007-08-18 13:28:49 +09002256 ap->eh_tries);
2257
Tejun Heo022bdb02006-05-15 20:58:22 +09002258 if (ehc->i.dev) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002259 ata_dev_err(ehc->i.dev, "exception Emask 0x%x "
2260 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2261 ehc->i.err_mask, link->sactive, ehc->i.serror,
2262 ehc->i.action, frozen, tries_buf);
Tejun Heo022bdb02006-05-15 20:58:22 +09002263 if (desc)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002264 ata_dev_err(ehc->i.dev, "%s\n", desc);
Tejun Heo022bdb02006-05-15 20:58:22 +09002265 } else {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002266 ata_link_err(link, "exception Emask 0x%x "
2267 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2268 ehc->i.err_mask, link->sactive, ehc->i.serror,
2269 ehc->i.action, frozen, tries_buf);
Tejun Heo022bdb02006-05-15 20:58:22 +09002270 if (desc)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002271 ata_link_err(link, "%s\n", desc);
Tejun Heo022bdb02006-05-15 20:58:22 +09002272 }
2273
Robert Hancock65211482009-07-14 20:43:39 -06002274#ifdef CONFIG_ATA_VERBOSE_ERROR
Robert Hancock1333e192007-10-02 11:22:02 -04002275 if (ehc->i.serror)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002276 ata_link_err(link,
Robert Hancock1333e192007-10-02 11:22:02 -04002277 "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
2278 ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
2279 ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
2280 ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
2281 ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
2282 ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
2283 ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
2284 ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
2285 ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
2286 ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
2287 ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
2288 ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
2289 ehc->i.serror & SERR_CRC ? "BadCRC " : "",
2290 ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
2291 ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
2292 ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
2293 ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002294 ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
Robert Hancock65211482009-07-14 20:43:39 -06002295#endif
Robert Hancock1333e192007-10-02 11:22:02 -04002296
Jens Axboe258c4e52018-06-19 10:12:49 -06002297 ata_qc_for_each_raw(ap, qc, tag) {
Tejun Heo8a937582006-11-14 22:36:12 +09002298 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
Tejun Heoabb6a882007-11-28 23:16:09 +09002299 char data_buf[20] = "";
2300 char cdb_buf[70] = "";
Tejun Heo022bdb02006-05-15 20:58:22 +09002301
Tejun Heo02607312007-08-06 18:36:23 +09002302 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
Tejun Heob1c72912008-07-31 17:02:43 +09002303 ata_dev_phys_link(qc->dev) != link || !qc->err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09002304 continue;
2305
Tejun Heoabb6a882007-11-28 23:16:09 +09002306 if (qc->dma_dir != DMA_NONE) {
2307 static const char *dma_str[] = {
2308 [DMA_BIDIRECTIONAL] = "bidi",
2309 [DMA_TO_DEVICE] = "out",
2310 [DMA_FROM_DEVICE] = "in",
2311 };
Geert Uytterhoevenfb1b8b12017-01-09 15:49:28 +01002312 const char *prot_str = NULL;
Tejun Heoabb6a882007-11-28 23:16:09 +09002313
Geert Uytterhoevenfb1b8b12017-01-09 15:49:28 +01002314 switch (qc->tf.protocol) {
2315 case ATA_PROT_UNKNOWN:
2316 prot_str = "unknown";
2317 break;
2318 case ATA_PROT_NODATA:
2319 prot_str = "nodata";
2320 break;
2321 case ATA_PROT_PIO:
2322 prot_str = "pio";
2323 break;
2324 case ATA_PROT_DMA:
2325 prot_str = "dma";
2326 break;
2327 case ATA_PROT_NCQ:
2328 prot_str = "ncq dma";
2329 break;
2330 case ATA_PROT_NCQ_NODATA:
2331 prot_str = "ncq nodata";
2332 break;
2333 case ATAPI_PROT_NODATA:
2334 prot_str = "nodata";
2335 break;
2336 case ATAPI_PROT_PIO:
2337 prot_str = "pio";
2338 break;
2339 case ATAPI_PROT_DMA:
2340 prot_str = "dma";
2341 break;
2342 }
Tejun Heoabb6a882007-11-28 23:16:09 +09002343 snprintf(data_buf, sizeof(data_buf), " %s %u %s",
Geert Uytterhoevenfb1b8b12017-01-09 15:49:28 +01002344 prot_str, qc->nbytes, dma_str[qc->dma_dir]);
Tejun Heoabb6a882007-11-28 23:16:09 +09002345 }
2346
Robert Hancock65211482009-07-14 20:43:39 -06002347 if (ata_is_atapi(qc->tf.protocol)) {
Hannes Reineckea13b0c92015-01-19 16:55:38 +01002348 const u8 *cdb = qc->cdb;
2349 size_t cdb_len = qc->dev->cdb_len;
2350
Hannes Reineckecbba5b02015-01-08 07:43:45 +01002351 if (qc->scsicmd) {
2352 cdb = qc->scsicmd->cmnd;
2353 cdb_len = qc->scsicmd->cmd_len;
2354 }
2355 __scsi_format_command(cdb_buf, sizeof(cdb_buf),
2356 cdb, cdb_len);
Robert Hancock65211482009-07-14 20:43:39 -06002357 } else {
2358 const char *descr = ata_get_cmd_descript(cmd->command);
2359 if (descr)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002360 ata_dev_err(qc->dev, "failed command: %s\n",
2361 descr);
Robert Hancock65211482009-07-14 20:43:39 -06002362 }
Tejun Heoabb6a882007-11-28 23:16:09 +09002363
Joe Perchesa9a79df2011-04-15 15:51:59 -07002364 ata_dev_err(qc->dev,
Tejun Heo8a937582006-11-14 22:36:12 +09002365 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
Tejun Heoabb6a882007-11-28 23:16:09 +09002366 "tag %d%s\n %s"
Tejun Heo8a937582006-11-14 22:36:12 +09002367 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
Tejun Heo5335b722007-07-16 14:29:40 +09002368 "Emask 0x%x (%s)%s\n",
Tejun Heo8a937582006-11-14 22:36:12 +09002369 cmd->command, cmd->feature, cmd->nsect,
2370 cmd->lbal, cmd->lbam, cmd->lbah,
2371 cmd->hob_feature, cmd->hob_nsect,
2372 cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
Tejun Heoabb6a882007-11-28 23:16:09 +09002373 cmd->device, qc->tag, data_buf, cdb_buf,
Tejun Heo8a937582006-11-14 22:36:12 +09002374 res->command, res->feature, res->nsect,
2375 res->lbal, res->lbam, res->lbah,
2376 res->hob_feature, res->hob_nsect,
2377 res->hob_lbal, res->hob_lbam, res->hob_lbah,
Tejun Heo5335b722007-07-16 14:29:40 +09002378 res->device, qc->err_mask, ata_err_string(qc->err_mask),
2379 qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
Robert Hancock1333e192007-10-02 11:22:02 -04002380
Robert Hancock65211482009-07-14 20:43:39 -06002381#ifdef CONFIG_ATA_VERBOSE_ERROR
Robert Hancock1333e192007-10-02 11:22:02 -04002382 if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
Hannes Reineckee87fd282016-04-04 11:43:55 +02002383 ATA_SENSE | ATA_ERR)) {
Robert Hancock1333e192007-10-02 11:22:02 -04002384 if (res->command & ATA_BUSY)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002385 ata_dev_err(qc->dev, "status: { Busy }\n");
Robert Hancock1333e192007-10-02 11:22:02 -04002386 else
Hannes Reineckee87fd282016-04-04 11:43:55 +02002387 ata_dev_err(qc->dev, "status: { %s%s%s%s%s}\n",
Robert Hancock1333e192007-10-02 11:22:02 -04002388 res->command & ATA_DRDY ? "DRDY " : "",
2389 res->command & ATA_DF ? "DF " : "",
2390 res->command & ATA_DRQ ? "DRQ " : "",
Hannes Reineckee87fd282016-04-04 11:43:55 +02002391 res->command & ATA_SENSE ? "SENSE " : "",
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002392 res->command & ATA_ERR ? "ERR " : "");
Robert Hancock1333e192007-10-02 11:22:02 -04002393 }
2394
2395 if (cmd->command != ATA_CMD_PACKET &&
Alexey Asemoveec7e1c2014-07-15 10:28:42 +04002396 (res->feature & (ATA_ICRC | ATA_UNC | ATA_AMNF |
2397 ATA_IDNF | ATA_ABORTED)))
2398 ata_dev_err(qc->dev, "error: { %s%s%s%s%s}\n",
Robert Hancock1333e192007-10-02 11:22:02 -04002399 res->feature & ATA_ICRC ? "ICRC " : "",
2400 res->feature & ATA_UNC ? "UNC " : "",
Alexey Asemoveec7e1c2014-07-15 10:28:42 +04002401 res->feature & ATA_AMNF ? "AMNF " : "",
Robert Hancock1333e192007-10-02 11:22:02 -04002402 res->feature & ATA_IDNF ? "IDNF " : "",
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002403 res->feature & ATA_ABORTED ? "ABRT " : "");
Robert Hancock65211482009-07-14 20:43:39 -06002404#endif
Tejun Heo022bdb02006-05-15 20:58:22 +09002405 }
2406}
2407
Tejun Heo9b1e2652007-08-06 18:36:24 +09002408/**
2409 * ata_eh_report - report error handling to user
2410 * @ap: ATA port to report EH about
2411 *
2412 * Report EH to user.
2413 *
2414 * LOCKING:
2415 * None.
2416 */
Tejun Heofb7fd612007-09-23 13:14:12 +09002417void ata_eh_report(struct ata_port *ap)
Tejun Heo9b1e2652007-08-06 18:36:24 +09002418{
2419 struct ata_link *link;
2420
Tejun Heo1eca4362008-11-03 20:03:17 +09002421 ata_for_each_link(link, ap, HOST_FIRST)
Tejun Heo9b1e2652007-08-06 18:36:24 +09002422 ata_eh_link_report(link);
2423}
2424
Tejun Heocc0680a2007-08-06 18:36:23 +09002425static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
Tejun Heob1c72912008-07-31 17:02:43 +09002426 unsigned int *classes, unsigned long deadline,
2427 bool clear_classes)
Tejun Heod87fa382006-05-31 18:28:24 +09002428{
Tejun Heof58229f2007-08-06 18:36:23 +09002429 struct ata_device *dev;
Tejun Heod87fa382006-05-31 18:28:24 +09002430
Tejun Heob1c72912008-07-31 17:02:43 +09002431 if (clear_classes)
Tejun Heo1eca4362008-11-03 20:03:17 +09002432 ata_for_each_dev(dev, link, ALL)
Tejun Heob1c72912008-07-31 17:02:43 +09002433 classes[dev->devno] = ATA_DEV_UNKNOWN;
Tejun Heod87fa382006-05-31 18:28:24 +09002434
Tejun Heof0465192008-05-19 01:15:08 +09002435 return reset(link, classes, deadline);
Tejun Heod87fa382006-05-31 18:28:24 +09002436}
2437
Sergei Shtylyove8411fb2011-08-09 23:25:50 +04002438static int ata_eh_followup_srst_needed(struct ata_link *link, int rc)
Tejun Heo664faf02006-05-31 18:27:50 +09002439{
Tejun Heo45db2f62008-04-08 01:46:56 +09002440 if ((link->flags & ATA_LFLAG_NO_SRST) || ata_link_offline(link))
Tejun Heoae791c02007-09-23 13:14:12 +09002441 return 0;
Tejun Heo5dbfc9c2008-07-31 16:08:02 +09002442 if (rc == -EAGAIN)
2443 return 1;
Tejun Heo071f44b2008-04-07 22:47:22 +09002444 if (sata_pmp_supported(link->ap) && ata_is_host_link(link))
Tejun Heo3495de72007-09-23 13:19:53 +09002445 return 1;
Tejun Heo664faf02006-05-31 18:27:50 +09002446 return 0;
2447}
2448
Tejun Heofb7fd612007-09-23 13:14:12 +09002449int ata_eh_reset(struct ata_link *link, int classify,
2450 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
2451 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
Tejun Heo022bdb02006-05-15 20:58:22 +09002452{
Tejun Heoafaa5c32007-10-09 15:06:10 +09002453 struct ata_port *ap = link->ap;
Tejun Heob1c72912008-07-31 17:02:43 +09002454 struct ata_link *slave = ap->slave_link;
Tejun Heo936fd732007-08-06 18:36:23 +09002455 struct ata_eh_context *ehc = &link->eh_context;
Bartlomiej Zolnierkiewicz705d2012009-07-26 16:21:01 +02002456 struct ata_eh_context *sehc = slave ? &slave->eh_context : NULL;
Tejun Heo664faf02006-05-31 18:27:50 +09002457 unsigned int *classes = ehc->classes;
Tejun Heo416dc9e2007-10-31 10:17:03 +09002458 unsigned int lflags = link->flags;
Tejun Heo1cdaf532006-07-03 16:07:26 +09002459 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
Tejun Heod8af0eb2008-05-20 02:17:53 +09002460 int max_tries = 0, try = 0;
Tejun Heob1c72912008-07-31 17:02:43 +09002461 struct ata_link *failed_link;
Tejun Heof58229f2007-08-06 18:36:23 +09002462 struct ata_device *dev;
Tejun Heo416dc9e2007-10-31 10:17:03 +09002463 unsigned long deadline, now;
Tejun Heo022bdb02006-05-15 20:58:22 +09002464 ata_reset_fn_t reset;
Tejun Heoafaa5c32007-10-09 15:06:10 +09002465 unsigned long flags;
Tejun Heo416dc9e2007-10-31 10:17:03 +09002466 u32 sstatus;
Tejun Heob1c72912008-07-31 17:02:43 +09002467 int nr_unknown, rc;
Tejun Heo022bdb02006-05-15 20:58:22 +09002468
Tejun Heo932648b2008-05-19 01:15:06 +09002469 /*
2470 * Prepare to reset
2471 */
Tejun Heod8af0eb2008-05-20 02:17:53 +09002472 while (ata_eh_reset_timeouts[max_tries] != ULONG_MAX)
2473 max_tries++;
Dan Williamsca6d43b2012-06-21 23:41:41 -07002474 if (link->flags & ATA_LFLAG_RST_ONCE)
2475 max_tries = 1;
Tejun Heo05944bd2008-08-13 20:19:09 +09002476 if (link->flags & ATA_LFLAG_NO_HRST)
2477 hardreset = NULL;
2478 if (link->flags & ATA_LFLAG_NO_SRST)
2479 softreset = NULL;
Tejun Heod8af0eb2008-05-20 02:17:53 +09002480
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002481 /* make sure each reset attempt is at least COOL_DOWN apart */
Tejun Heo19b72322008-11-04 17:08:40 +09002482 if (ehc->i.flags & ATA_EHI_DID_RESET) {
2483 now = jiffies;
2484 WARN_ON(time_after(ehc->last_reset, now));
2485 deadline = ata_deadline(ehc->last_reset,
2486 ATA_EH_RESET_COOL_DOWN);
2487 if (time_before(now, deadline))
2488 schedule_timeout_uninterruptible(deadline - now);
2489 }
Tejun Heo0a2c0f52008-05-20 02:17:52 +09002490
Tejun Heoafaa5c32007-10-09 15:06:10 +09002491 spin_lock_irqsave(ap->lock, flags);
2492 ap->pflags |= ATA_PFLAG_RESETTING;
2493 spin_unlock_irqrestore(ap->lock, flags);
2494
Tejun Heocf480622008-01-24 00:05:14 +09002495 ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
Tejun Heo13abf502006-07-10 23:18:46 +09002496
Tejun Heo1eca4362008-11-03 20:03:17 +09002497 ata_for_each_dev(dev, link, ALL) {
Tejun Heocdeab112007-10-29 16:41:09 +09002498 /* If we issue an SRST then an ATA drive (not ATAPI)
2499 * may change configuration and be in PIO0 timing. If
2500 * we do a hard reset (or are coming from power on)
2501 * this is true for ATA or ATAPI. Until we've set a
2502 * suitable controller mode we should not touch the
2503 * bus as we may be talking too fast.
2504 */
2505 dev->pio_mode = XFER_PIO_0;
Aaron Lu54169122012-12-03 11:35:02 +08002506 dev->dma_mode = 0xff;
Tejun Heocdeab112007-10-29 16:41:09 +09002507
2508 /* If the controller has a pio mode setup function
2509 * then use it to set the chipset to rights. Don't
2510 * touch the DMA setup as that will be dealt with when
2511 * configuring devices.
2512 */
2513 if (ap->ops->set_piomode)
2514 ap->ops->set_piomode(ap, dev);
2515 }
2516
Tejun Heocf480622008-01-24 00:05:14 +09002517 /* prefer hardreset */
Tejun Heo932648b2008-05-19 01:15:06 +09002518 reset = NULL;
Tejun Heocf480622008-01-24 00:05:14 +09002519 ehc->i.action &= ~ATA_EH_RESET;
2520 if (hardreset) {
2521 reset = hardreset;
Tejun Heoa6740502008-07-31 16:07:04 +09002522 ehc->i.action |= ATA_EH_HARDRESET;
Tejun Heo4f7faa32008-01-30 18:18:26 +09002523 } else if (softreset) {
Tejun Heocf480622008-01-24 00:05:14 +09002524 reset = softreset;
Tejun Heoa6740502008-07-31 16:07:04 +09002525 ehc->i.action |= ATA_EH_SOFTRESET;
Tejun Heocf480622008-01-24 00:05:14 +09002526 }
Tejun Heof5914a42006-05-31 18:27:48 +09002527
2528 if (prereset) {
Tejun Heob1c72912008-07-31 17:02:43 +09002529 unsigned long deadline = ata_deadline(jiffies,
2530 ATA_EH_PRERESET_TIMEOUT);
2531
2532 if (slave) {
2533 sehc->i.action &= ~ATA_EH_RESET;
2534 sehc->i.action |= ehc->i.action;
2535 }
2536
2537 rc = prereset(link, deadline);
2538
2539 /* If present, do prereset on slave link too. Reset
2540 * is skipped iff both master and slave links report
2541 * -ENOENT or clear ATA_EH_RESET.
2542 */
2543 if (slave && (rc == 0 || rc == -ENOENT)) {
2544 int tmp;
2545
2546 tmp = prereset(slave, deadline);
2547 if (tmp != -ENOENT)
2548 rc = tmp;
2549
2550 ehc->i.action |= sehc->i.action;
2551 }
2552
Tejun Heof5914a42006-05-31 18:27:48 +09002553 if (rc) {
Alan Coxc9619222006-09-26 17:53:38 +01002554 if (rc == -ENOENT) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002555 ata_link_dbg(link, "port disabled--ignoring\n");
Tejun Heocf480622008-01-24 00:05:14 +09002556 ehc->i.action &= ~ATA_EH_RESET;
Tejun Heo4aa9ab62007-03-12 17:24:08 +09002557
Tejun Heo1eca4362008-11-03 20:03:17 +09002558 ata_for_each_dev(dev, link, ALL)
Tejun Heof58229f2007-08-06 18:36:23 +09002559 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heo4aa9ab62007-03-12 17:24:08 +09002560
2561 rc = 0;
Alan Coxc9619222006-09-26 17:53:38 +01002562 } else
Joe Perchesa9a79df2011-04-15 15:51:59 -07002563 ata_link_err(link,
2564 "prereset failed (errno=%d)\n",
2565 rc);
Tejun Heofccb6ea2007-07-16 14:29:41 +09002566 goto out;
Tejun Heof5914a42006-05-31 18:27:48 +09002567 }
Tejun Heof5914a42006-05-31 18:27:48 +09002568
Tejun Heo932648b2008-05-19 01:15:06 +09002569 /* prereset() might have cleared ATA_EH_RESET. If so,
Tejun Heod6515e62009-03-04 15:59:30 +09002570 * bang classes, thaw and return.
Tejun Heo932648b2008-05-19 01:15:06 +09002571 */
2572 if (reset && !(ehc->i.action & ATA_EH_RESET)) {
Tejun Heo1eca4362008-11-03 20:03:17 +09002573 ata_for_each_dev(dev, link, ALL)
Tejun Heo932648b2008-05-19 01:15:06 +09002574 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heod6515e62009-03-04 15:59:30 +09002575 if ((ap->pflags & ATA_PFLAG_FROZEN) &&
2576 ata_is_host_link(link))
2577 ata_eh_thaw_port(ap);
Tejun Heo932648b2008-05-19 01:15:06 +09002578 rc = 0;
2579 goto out;
2580 }
Tejun Heof5914a42006-05-31 18:27:48 +09002581 }
2582
Tejun Heo022bdb02006-05-15 20:58:22 +09002583 retry:
Tejun Heo932648b2008-05-19 01:15:06 +09002584 /*
2585 * Perform reset
2586 */
Tejun Heodc98c322008-05-19 01:15:07 +09002587 if (ata_is_host_link(link))
2588 ata_eh_freeze_port(ap);
2589
Tejun Heo341c2c92008-05-20 02:17:51 +09002590 deadline = ata_deadline(jiffies, ata_eh_reset_timeouts[try++]);
Tejun Heo31daabd2007-02-02 16:50:52 +09002591
Tejun Heo932648b2008-05-19 01:15:06 +09002592 if (reset) {
2593 if (verbose)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002594 ata_link_info(link, "%s resetting link\n",
2595 reset == softreset ? "soft" : "hard");
Tejun Heo022bdb02006-05-15 20:58:22 +09002596
Tejun Heo932648b2008-05-19 01:15:06 +09002597 /* mark that this EH session started with reset */
Tejun Heo19b72322008-11-04 17:08:40 +09002598 ehc->last_reset = jiffies;
Tejun Heo932648b2008-05-19 01:15:06 +09002599 if (reset == hardreset)
2600 ehc->i.flags |= ATA_EHI_DID_HARDRESET;
2601 else
2602 ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09002603
Tejun Heob1c72912008-07-31 17:02:43 +09002604 rc = ata_do_reset(link, reset, classes, deadline, true);
2605 if (rc && rc != -EAGAIN) {
2606 failed_link = link;
Tejun Heo5dbfc9c2008-07-31 16:08:02 +09002607 goto fail;
Tejun Heob1c72912008-07-31 17:02:43 +09002608 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002609
Tejun Heob1c72912008-07-31 17:02:43 +09002610 /* hardreset slave link if existent */
2611 if (slave && reset == hardreset) {
2612 int tmp;
2613
2614 if (verbose)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002615 ata_link_info(slave, "hard resetting link\n");
Tejun Heob1c72912008-07-31 17:02:43 +09002616
2617 ata_eh_about_to_do(slave, NULL, ATA_EH_RESET);
2618 tmp = ata_do_reset(slave, reset, classes, deadline,
2619 false);
2620 switch (tmp) {
2621 case -EAGAIN:
2622 rc = -EAGAIN;
Gustavo A. R. Silvae06abcc2021-04-20 15:11:29 -05002623 break;
Tejun Heob1c72912008-07-31 17:02:43 +09002624 case 0:
2625 break;
2626 default:
2627 failed_link = slave;
2628 rc = tmp;
2629 goto fail;
2630 }
2631 }
2632
2633 /* perform follow-up SRST if necessary */
Tejun Heo932648b2008-05-19 01:15:06 +09002634 if (reset == hardreset &&
Sergei Shtylyove8411fb2011-08-09 23:25:50 +04002635 ata_eh_followup_srst_needed(link, rc)) {
Tejun Heo932648b2008-05-19 01:15:06 +09002636 reset = softreset;
Tejun Heo664faf02006-05-31 18:27:50 +09002637
Tejun Heo932648b2008-05-19 01:15:06 +09002638 if (!reset) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002639 ata_link_err(link,
2640 "follow-up softreset required but no softreset available\n");
Tejun Heob1c72912008-07-31 17:02:43 +09002641 failed_link = link;
Tejun Heo932648b2008-05-19 01:15:06 +09002642 rc = -EINVAL;
2643 goto fail;
2644 }
2645
2646 ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
Tejun Heob1c72912008-07-31 17:02:43 +09002647 rc = ata_do_reset(link, reset, classes, deadline, true);
Tejun Heofe2c4d02009-07-08 12:16:37 +09002648 if (rc) {
2649 failed_link = link;
2650 goto fail;
2651 }
Tejun Heo664faf02006-05-31 18:27:50 +09002652 }
Tejun Heo932648b2008-05-19 01:15:06 +09002653 } else {
2654 if (verbose)
Joe Perchesa9a79df2011-04-15 15:51:59 -07002655 ata_link_info(link,
2656 "no reset method available, skipping reset\n");
Tejun Heo932648b2008-05-19 01:15:06 +09002657 if (!(lflags & ATA_LFLAG_ASSUME_CLASS))
2658 lflags |= ATA_LFLAG_ASSUME_ATA;
Tejun Heo664faf02006-05-31 18:27:50 +09002659 }
2660
Tejun Heo932648b2008-05-19 01:15:06 +09002661 /*
2662 * Post-reset processing
2663 */
Tejun Heo1eca4362008-11-03 20:03:17 +09002664 ata_for_each_dev(dev, link, ALL) {
Tejun Heo416dc9e2007-10-31 10:17:03 +09002665 /* After the reset, the device state is PIO 0 and the
2666 * controller state is undefined. Reset also wakes up
2667 * drives from sleeping mode.
2668 */
2669 dev->pio_mode = XFER_PIO_0;
2670 dev->flags &= ~ATA_DFLAG_SLEEPING;
Tejun Heo664faf02006-05-31 18:27:50 +09002671
Tejun Heo3b761d32009-10-06 17:08:40 +09002672 if (ata_phys_link_offline(ata_dev_phys_link(dev)))
2673 continue;
2674
2675 /* apply class override */
2676 if (lflags & ATA_LFLAG_ASSUME_ATA)
2677 classes[dev->devno] = ATA_DEV_ATA;
2678 else if (lflags & ATA_LFLAG_ASSUME_SEMB)
2679 classes[dev->devno] = ATA_DEV_SEMB_UNSUP;
Tejun Heo022bdb02006-05-15 20:58:22 +09002680 }
2681
Tejun Heo416dc9e2007-10-31 10:17:03 +09002682 /* record current link speed */
2683 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2684 link->sata_spd = (sstatus >> 4) & 0xf;
Tejun Heob1c72912008-07-31 17:02:43 +09002685 if (slave && sata_scr_read(slave, SCR_STATUS, &sstatus) == 0)
2686 slave->sata_spd = (sstatus >> 4) & 0xf;
Tejun Heo008a7892007-07-16 14:29:40 +09002687
Tejun Heodc98c322008-05-19 01:15:07 +09002688 /* thaw the port */
2689 if (ata_is_host_link(link))
2690 ata_eh_thaw_port(ap);
2691
Tejun Heof0465192008-05-19 01:15:08 +09002692 /* postreset() should clear hardware SError. Although SError
2693 * is cleared during link resume, clearing SError here is
2694 * necessary as some PHYs raise hotplug events after SRST.
2695 * This introduces race condition where hotplug occurs between
2696 * reset and here. This race is mediated by cross checking
2697 * link onlineness and classification result later.
2698 */
Tejun Heob1c72912008-07-31 17:02:43 +09002699 if (postreset) {
Tejun Heo416dc9e2007-10-31 10:17:03 +09002700 postreset(link, classes);
Tejun Heob1c72912008-07-31 17:02:43 +09002701 if (slave)
2702 postreset(slave, classes);
2703 }
Tejun Heo20952b62006-05-31 18:27:23 +09002704
Tejun Heo1e641062009-07-17 11:58:33 +09002705 /*
Tejun Heo8c56cac2011-05-25 13:19:39 +02002706 * Some controllers can't be frozen very well and may set spurious
2707 * error conditions during reset. Clear accumulated error
2708 * information and re-thaw the port if frozen. As reset is the
2709 * final recovery action and we cross check link onlineness against
2710 * device classification later, no hotplug event is lost by this.
Tejun Heo1e641062009-07-17 11:58:33 +09002711 */
Tejun Heof0465192008-05-19 01:15:08 +09002712 spin_lock_irqsave(link->ap->lock, flags);
Tejun Heo1e641062009-07-17 11:58:33 +09002713 memset(&link->eh_info, 0, sizeof(link->eh_info));
Tejun Heob1c72912008-07-31 17:02:43 +09002714 if (slave)
Tejun Heo1e641062009-07-17 11:58:33 +09002715 memset(&slave->eh_info, 0, sizeof(link->eh_info));
2716 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
Tejun Heof0465192008-05-19 01:15:08 +09002717 spin_unlock_irqrestore(link->ap->lock, flags);
2718
Tejun Heo8c56cac2011-05-25 13:19:39 +02002719 if (ap->pflags & ATA_PFLAG_FROZEN)
2720 ata_eh_thaw_port(ap);
2721
Tejun Heo3b761d32009-10-06 17:08:40 +09002722 /*
2723 * Make sure onlineness and classification result correspond.
Tejun Heof0465192008-05-19 01:15:08 +09002724 * Hotplug could have happened during reset and some
2725 * controllers fail to wait while a drive is spinning up after
2726 * being hotplugged causing misdetection. By cross checking
Tejun Heo3b761d32009-10-06 17:08:40 +09002727 * link on/offlineness and classification result, those
2728 * conditions can be reliably detected and retried.
Tejun Heof0465192008-05-19 01:15:08 +09002729 */
Tejun Heob1c72912008-07-31 17:02:43 +09002730 nr_unknown = 0;
Tejun Heo1eca4362008-11-03 20:03:17 +09002731 ata_for_each_dev(dev, link, ALL) {
Tejun Heo3b761d32009-10-06 17:08:40 +09002732 if (ata_phys_link_online(ata_dev_phys_link(dev))) {
2733 if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002734 ata_dev_dbg(dev, "link online but device misclassified\n");
Tejun Heo3b761d32009-10-06 17:08:40 +09002735 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heob1c72912008-07-31 17:02:43 +09002736 nr_unknown++;
Tejun Heo3b761d32009-10-06 17:08:40 +09002737 }
2738 } else if (ata_phys_link_offline(ata_dev_phys_link(dev))) {
2739 if (ata_class_enabled(classes[dev->devno]))
Joe Perchesa9a79df2011-04-15 15:51:59 -07002740 ata_dev_dbg(dev,
2741 "link offline, clearing class %d to NONE\n",
2742 classes[dev->devno]);
Tejun Heo3b761d32009-10-06 17:08:40 +09002743 classes[dev->devno] = ATA_DEV_NONE;
2744 } else if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002745 ata_dev_dbg(dev,
2746 "link status unknown, clearing UNKNOWN to NONE\n");
Tejun Heo3b761d32009-10-06 17:08:40 +09002747 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heob1c72912008-07-31 17:02:43 +09002748 }
Tejun Heof0465192008-05-19 01:15:08 +09002749 }
2750
Tejun Heob1c72912008-07-31 17:02:43 +09002751 if (classify && nr_unknown) {
Tejun Heof0465192008-05-19 01:15:08 +09002752 if (try < max_tries) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002753 ata_link_warn(link,
2754 "link online but %d devices misclassified, retrying\n",
2755 nr_unknown);
Tejun Heob1c72912008-07-31 17:02:43 +09002756 failed_link = link;
Tejun Heof0465192008-05-19 01:15:08 +09002757 rc = -EAGAIN;
2758 goto fail;
2759 }
Joe Perchesa9a79df2011-04-15 15:51:59 -07002760 ata_link_warn(link,
2761 "link online but %d devices misclassified, "
2762 "device detection might fail\n", nr_unknown);
Tejun Heof0465192008-05-19 01:15:08 +09002763 }
2764
Tejun Heo416dc9e2007-10-31 10:17:03 +09002765 /* reset successful, schedule revalidation */
Tejun Heocf480622008-01-24 00:05:14 +09002766 ata_eh_done(link, NULL, ATA_EH_RESET);
Tejun Heob1c72912008-07-31 17:02:43 +09002767 if (slave)
2768 ata_eh_done(slave, NULL, ATA_EH_RESET);
Tejun Heo6b7ae952010-09-01 17:50:06 +02002769 ehc->last_reset = jiffies; /* update to completion time */
Tejun Heo416dc9e2007-10-31 10:17:03 +09002770 ehc->i.action |= ATA_EH_REVALIDATE;
Tejun Heo6b7ae952010-09-01 17:50:06 +02002771 link->lpm_policy = ATA_LPM_UNKNOWN; /* reset LPM state */
Tejun Heoae791c02007-09-23 13:14:12 +09002772
Tejun Heo416dc9e2007-10-31 10:17:03 +09002773 rc = 0;
Tejun Heofccb6ea2007-07-16 14:29:41 +09002774 out:
2775 /* clear hotplug flag */
2776 ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
Tejun Heob1c72912008-07-31 17:02:43 +09002777 if (slave)
2778 sehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
Tejun Heoafaa5c32007-10-09 15:06:10 +09002779
2780 spin_lock_irqsave(ap->lock, flags);
2781 ap->pflags &= ~ATA_PFLAG_RESETTING;
2782 spin_unlock_irqrestore(ap->lock, flags);
2783
Tejun Heo022bdb02006-05-15 20:58:22 +09002784 return rc;
Tejun Heo416dc9e2007-10-31 10:17:03 +09002785
2786 fail:
Tejun Heo5958e302008-04-07 22:47:20 +09002787 /* if SCR isn't accessible on a fan-out port, PMP needs to be reset */
2788 if (!ata_is_host_link(link) &&
2789 sata_scr_read(link, SCR_STATUS, &sstatus))
2790 rc = -ERESTART;
2791
Gwendal Grignou7a46c072011-10-19 17:17:02 -07002792 if (try >= max_tries) {
Tejun Heo8ea76452011-05-25 12:23:56 +02002793 /*
2794 * Thaw host port even if reset failed, so that the port
2795 * can be retried on the next phy event. This risks
2796 * repeated EH runs but seems to be a better tradeoff than
2797 * shutting down a port after a botched hotplug attempt.
2798 */
2799 if (ata_is_host_link(link))
2800 ata_eh_thaw_port(ap);
Tejun Heo416dc9e2007-10-31 10:17:03 +09002801 goto out;
Tejun Heo8ea76452011-05-25 12:23:56 +02002802 }
Tejun Heo416dc9e2007-10-31 10:17:03 +09002803
2804 now = jiffies;
2805 if (time_before(now, deadline)) {
2806 unsigned long delta = deadline - now;
2807
Joe Perchesa9a79df2011-04-15 15:51:59 -07002808 ata_link_warn(failed_link,
Tejun Heo0a2c0f52008-05-20 02:17:52 +09002809 "reset failed (errno=%d), retrying in %u secs\n",
2810 rc, DIV_ROUND_UP(jiffies_to_msecs(delta), 1000));
Tejun Heo416dc9e2007-10-31 10:17:03 +09002811
Tejun Heoc0c362b2010-09-06 17:57:14 +02002812 ata_eh_release(ap);
Tejun Heo416dc9e2007-10-31 10:17:03 +09002813 while (delta)
2814 delta = schedule_timeout_uninterruptible(delta);
Tejun Heoc0c362b2010-09-06 17:57:14 +02002815 ata_eh_acquire(ap);
Tejun Heo416dc9e2007-10-31 10:17:03 +09002816 }
2817
Gwendal Grignou7a46c072011-10-19 17:17:02 -07002818 /*
2819 * While disks spinup behind PMP, some controllers fail sending SRST.
2820 * They need to be reset - as well as the PMP - before retrying.
2821 */
2822 if (rc == -ERESTART) {
2823 if (ata_is_host_link(link))
2824 ata_eh_thaw_port(ap);
2825 goto out;
2826 }
2827
Tejun Heob1c72912008-07-31 17:02:43 +09002828 if (try == max_tries - 1) {
Tejun Heoa07d4992009-01-29 20:31:33 +09002829 sata_down_spd_limit(link, 0);
Tejun Heob1c72912008-07-31 17:02:43 +09002830 if (slave)
Tejun Heoa07d4992009-01-29 20:31:33 +09002831 sata_down_spd_limit(slave, 0);
Tejun Heob1c72912008-07-31 17:02:43 +09002832 } else if (rc == -EPIPE)
Tejun Heoa07d4992009-01-29 20:31:33 +09002833 sata_down_spd_limit(failed_link, 0);
Tejun Heob1c72912008-07-31 17:02:43 +09002834
Tejun Heo416dc9e2007-10-31 10:17:03 +09002835 if (hardreset)
2836 reset = hardreset;
2837 goto retry;
Tejun Heo022bdb02006-05-15 20:58:22 +09002838}
2839
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02002840static inline void ata_eh_pull_park_action(struct ata_port *ap)
2841{
2842 struct ata_link *link;
2843 struct ata_device *dev;
2844 unsigned long flags;
2845
2846 /*
2847 * This function can be thought of as an extended version of
2848 * ata_eh_about_to_do() specially crafted to accommodate the
2849 * requirements of ATA_EH_PARK handling. Since the EH thread
2850 * does not leave the do {} while () loop in ata_eh_recover as
2851 * long as the timeout for a park request to *one* device on
2852 * the port has not expired, and since we still want to pick
2853 * up park requests to other devices on the same port or
2854 * timeout updates for the same device, we have to pull
2855 * ATA_EH_PARK actions from eh_info into eh_context.i
2856 * ourselves at the beginning of each pass over the loop.
2857 *
2858 * Additionally, all write accesses to &ap->park_req_pending
Wolfram Sang16735d02013-11-14 14:32:02 -08002859 * through reinit_completion() (see below) or complete_all()
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02002860 * (see ata_scsi_park_store()) are protected by the host lock.
2861 * As a result we have that park_req_pending.done is zero on
2862 * exit from this function, i.e. when ATA_EH_PARK actions for
2863 * *all* devices on port ap have been pulled into the
2864 * respective eh_context structs. If, and only if,
2865 * park_req_pending.done is non-zero by the time we reach
2866 * wait_for_completion_timeout(), another ATA_EH_PARK action
2867 * has been scheduled for at least one of the devices on port
2868 * ap and we have to cycle over the do {} while () loop in
2869 * ata_eh_recover() again.
2870 */
2871
2872 spin_lock_irqsave(ap->lock, flags);
Wolfram Sang16735d02013-11-14 14:32:02 -08002873 reinit_completion(&ap->park_req_pending);
Tejun Heo1eca4362008-11-03 20:03:17 +09002874 ata_for_each_link(link, ap, EDGE) {
2875 ata_for_each_dev(dev, link, ALL) {
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02002876 struct ata_eh_info *ehi = &link->eh_info;
2877
2878 link->eh_context.i.dev_action[dev->devno] |=
2879 ehi->dev_action[dev->devno] & ATA_EH_PARK;
2880 ata_eh_clear_action(link, dev, ehi, ATA_EH_PARK);
2881 }
2882 }
2883 spin_unlock_irqrestore(ap->lock, flags);
2884}
2885
2886static void ata_eh_park_issue_cmd(struct ata_device *dev, int park)
2887{
2888 struct ata_eh_context *ehc = &dev->link->eh_context;
2889 struct ata_taskfile tf;
2890 unsigned int err_mask;
2891
2892 ata_tf_init(dev, &tf);
2893 if (park) {
2894 ehc->unloaded_mask |= 1 << dev->devno;
2895 tf.command = ATA_CMD_IDLEIMMEDIATE;
2896 tf.feature = 0x44;
2897 tf.lbal = 0x4c;
2898 tf.lbam = 0x4e;
2899 tf.lbah = 0x55;
2900 } else {
2901 ehc->unloaded_mask &= ~(1 << dev->devno);
2902 tf.command = ATA_CMD_CHK_POWER;
2903 }
2904
2905 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
Hannes Reineckebd18bc02016-07-14 09:05:46 +09002906 tf.protocol = ATA_PROT_NODATA;
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02002907 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
2908 if (park && (err_mask || tf.lbal != 0xc4)) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07002909 ata_dev_err(dev, "head unload failed!\n");
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02002910 ehc->unloaded_mask &= ~(1 << dev->devno);
2911 }
2912}
2913
Tejun Heo02607312007-08-06 18:36:23 +09002914static int ata_eh_revalidate_and_attach(struct ata_link *link,
Tejun Heo084fe632006-05-31 18:28:03 +09002915 struct ata_device **r_failed_dev)
Tejun Heo022bdb02006-05-15 20:58:22 +09002916{
Tejun Heo02607312007-08-06 18:36:23 +09002917 struct ata_port *ap = link->ap;
2918 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09002919 struct ata_device *dev;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002920 unsigned int new_mask = 0;
Tejun Heo084fe632006-05-31 18:28:03 +09002921 unsigned long flags;
Tejun Heof58229f2007-08-06 18:36:23 +09002922 int rc = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09002923
2924 DPRINTK("ENTER\n");
2925
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002926 /* For PATA drive side cable detection to work, IDENTIFY must
2927 * be done backwards such that PDIAG- is released by the slave
2928 * device before the master device is identified.
2929 */
Tejun Heo1eca4362008-11-03 20:03:17 +09002930 ata_for_each_dev(dev, link, ALL_REVERSE) {
Tejun Heof58229f2007-08-06 18:36:23 +09002931 unsigned int action = ata_eh_dev_action(dev);
2932 unsigned int readid_flags = 0;
Tejun Heo47005f22006-06-19 18:27:23 +09002933
Tejun Heobff04642006-11-10 18:08:10 +09002934 if (ehc->i.flags & ATA_EHI_DID_RESET)
2935 readid_flags |= ATA_READID_POSTRESET;
2936
Tejun Heo9666f402007-05-04 21:27:47 +02002937 if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
Tejun Heo633273a2007-09-23 13:19:54 +09002938 WARN_ON(dev->class == ATA_DEV_PMP);
2939
Tejun Heob1c72912008-07-31 17:02:43 +09002940 if (ata_phys_link_offline(ata_dev_phys_link(dev))) {
Tejun Heo022bdb02006-05-15 20:58:22 +09002941 rc = -EIO;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002942 goto err;
Tejun Heo022bdb02006-05-15 20:58:22 +09002943 }
2944
Tejun Heo02607312007-08-06 18:36:23 +09002945 ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
Tejun Heo422c9da2007-09-23 13:14:12 +09002946 rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
2947 readid_flags);
Tejun Heo022bdb02006-05-15 20:58:22 +09002948 if (rc)
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002949 goto err;
Tejun Heo022bdb02006-05-15 20:58:22 +09002950
Tejun Heo02607312007-08-06 18:36:23 +09002951 ata_eh_done(link, dev, ATA_EH_REVALIDATE);
Tejun Heo47005f22006-06-19 18:27:23 +09002952
Tejun Heobaa1e782006-11-01 18:39:27 +09002953 /* Configuration may have changed, reconfigure
2954 * transfer mode.
2955 */
2956 ehc->i.flags |= ATA_EHI_SETMODE;
2957
zhao, forrest3057ac32006-06-12 12:01:34 +08002958 /* schedule the scsi_rescan_device() here */
Tejun Heoad72cf92010-07-02 10:03:52 +02002959 schedule_work(&(ap->scsi_rescan_task));
Tejun Heo084fe632006-05-31 18:28:03 +09002960 } else if (dev->class == ATA_DEV_UNKNOWN &&
2961 ehc->tries[dev->devno] &&
2962 ata_class_enabled(ehc->classes[dev->devno])) {
Tejun Heo842faa62009-05-10 01:06:54 +09002963 /* Temporarily set dev->class, it will be
2964 * permanently set once all configurations are
2965 * complete. This is necessary because new
2966 * device configuration is done in two
2967 * separate loops.
2968 */
Tejun Heo084fe632006-05-31 18:28:03 +09002969 dev->class = ehc->classes[dev->devno];
2970
Tejun Heo633273a2007-09-23 13:19:54 +09002971 if (dev->class == ATA_DEV_PMP)
2972 rc = sata_pmp_attach(dev);
2973 else
2974 rc = ata_dev_read_id(dev, &dev->class,
2975 readid_flags, dev->id);
Tejun Heo842faa62009-05-10 01:06:54 +09002976
2977 /* read_id might have changed class, store and reset */
2978 ehc->classes[dev->devno] = dev->class;
2979 dev->class = ATA_DEV_UNKNOWN;
2980
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002981 switch (rc) {
2982 case 0:
Tejun Heo99cf6102009-01-29 20:31:32 +09002983 /* clear error info accumulated during probe */
2984 ata_ering_clear(&dev->ering);
Tejun Heof58229f2007-08-06 18:36:23 +09002985 new_mask |= 1 << dev->devno;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002986 break;
2987 case -ENOENT:
Tejun Heo55a8e2c2006-11-10 18:08:10 +09002988 /* IDENTIFY was issued to non-existent
2989 * device. No need to reset. Just
Tejun Heo842faa62009-05-10 01:06:54 +09002990 * thaw and ignore the device.
Tejun Heo55a8e2c2006-11-10 18:08:10 +09002991 */
2992 ata_eh_thaw_port(ap);
Tejun Heo084fe632006-05-31 18:28:03 +09002993 break;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002994 default:
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002995 goto err;
Tejun Heo55a8e2c2006-11-10 18:08:10 +09002996 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002997 }
2998 }
2999
Tejun Heoc1c4e8d2007-04-23 02:05:53 +09003000 /* PDIAG- should have been released, ask cable type if post-reset */
Tejun Heo33267322008-02-13 09:15:09 +09003001 if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) {
3002 if (ap->ops->cable_detect)
3003 ap->cbl = ap->ops->cable_detect(ap);
3004 ata_force_cbl(ap);
3005 }
Tejun Heoc1c4e8d2007-04-23 02:05:53 +09003006
Tejun Heo8c3c52a2007-03-22 22:24:19 +09003007 /* Configure new devices forward such that user doesn't see
3008 * device detection messages backwards.
3009 */
Tejun Heo1eca4362008-11-03 20:03:17 +09003010 ata_for_each_dev(dev, link, ALL) {
Tejun Heo4f7c2872009-10-15 23:37:32 +09003011 if (!(new_mask & (1 << dev->devno)))
Tejun Heo8c3c52a2007-03-22 22:24:19 +09003012 continue;
3013
Tejun Heo842faa62009-05-10 01:06:54 +09003014 dev->class = ehc->classes[dev->devno];
3015
Tejun Heo4f7c2872009-10-15 23:37:32 +09003016 if (dev->class == ATA_DEV_PMP)
3017 continue;
3018
Tejun Heo8c3c52a2007-03-22 22:24:19 +09003019 ehc->i.flags |= ATA_EHI_PRINTINFO;
3020 rc = ata_dev_configure(dev);
3021 ehc->i.flags &= ~ATA_EHI_PRINTINFO;
Tejun Heo842faa62009-05-10 01:06:54 +09003022 if (rc) {
3023 dev->class = ATA_DEV_UNKNOWN;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09003024 goto err;
Tejun Heo842faa62009-05-10 01:06:54 +09003025 }
Tejun Heo8c3c52a2007-03-22 22:24:19 +09003026
3027 spin_lock_irqsave(ap->lock, flags);
3028 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
3029 spin_unlock_irqrestore(ap->lock, flags);
3030
3031 /* new device discovered, configure xfermode */
3032 ehc->i.flags |= ATA_EHI_SETMODE;
3033 }
3034
3035 return 0;
3036
3037 err:
3038 *r_failed_dev = dev;
3039 DPRINTK("EXIT rc=%d\n", rc);
Tejun Heo022bdb02006-05-15 20:58:22 +09003040 return rc;
3041}
3042
Tejun Heo6f1d1e32007-11-27 19:28:55 +09003043/**
3044 * ata_set_mode - Program timings and issue SET FEATURES - XFER
3045 * @link: link on which timings will be programmed
Martin Olsson98a17082009-04-22 18:21:29 +02003046 * @r_failed_dev: out parameter for failed device
Tejun Heo6f1d1e32007-11-27 19:28:55 +09003047 *
3048 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3049 * ata_set_mode() fails, pointer to the failing device is
3050 * returned in @r_failed_dev.
3051 *
3052 * LOCKING:
3053 * PCI/etc. bus probe sem.
3054 *
3055 * RETURNS:
3056 * 0 on success, negative errno otherwise
3057 */
3058int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3059{
3060 struct ata_port *ap = link->ap;
Tejun Heo00115e02007-11-27 19:28:58 +09003061 struct ata_device *dev;
3062 int rc;
Tejun Heo6f1d1e32007-11-27 19:28:55 +09003063
Tejun Heo76326ac2007-11-27 19:28:59 +09003064 /* if data transfer is verified, clear DUBIOUS_XFER on ering top */
Tejun Heo1eca4362008-11-03 20:03:17 +09003065 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo76326ac2007-11-27 19:28:59 +09003066 if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
3067 struct ata_ering_entry *ent;
3068
3069 ent = ata_ering_top(&dev->ering);
3070 if (ent)
3071 ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER;
3072 }
3073 }
3074
Tejun Heo6f1d1e32007-11-27 19:28:55 +09003075 /* has private set_mode? */
3076 if (ap->ops->set_mode)
Tejun Heo00115e02007-11-27 19:28:58 +09003077 rc = ap->ops->set_mode(link, r_failed_dev);
3078 else
3079 rc = ata_do_set_mode(link, r_failed_dev);
3080
3081 /* if transfer mode has changed, set DUBIOUS_XFER on device */
Tejun Heo1eca4362008-11-03 20:03:17 +09003082 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo00115e02007-11-27 19:28:58 +09003083 struct ata_eh_context *ehc = &link->eh_context;
3084 u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
3085 u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
3086
3087 if (dev->xfer_mode != saved_xfer_mode ||
3088 ata_ncq_enabled(dev) != saved_ncq)
3089 dev->flags |= ATA_DFLAG_DUBIOUS_XFER;
3090 }
3091
3092 return rc;
Tejun Heo6f1d1e32007-11-27 19:28:55 +09003093}
3094
Tejun Heo11fc33d2008-08-30 14:20:01 +02003095/**
3096 * atapi_eh_clear_ua - Clear ATAPI UNIT ATTENTION after reset
3097 * @dev: ATAPI device to clear UA for
3098 *
3099 * Resets and other operations can make an ATAPI device raise
3100 * UNIT ATTENTION which causes the next operation to fail. This
3101 * function clears UA.
3102 *
3103 * LOCKING:
3104 * EH context (may sleep).
3105 *
3106 * RETURNS:
3107 * 0 on success, -errno on failure.
3108 */
3109static int atapi_eh_clear_ua(struct ata_device *dev)
3110{
3111 int i;
3112
3113 for (i = 0; i < ATA_EH_UA_TRIES; i++) {
Tejun Heob5357082009-03-02 18:55:16 +09003114 u8 *sense_buffer = dev->link->ap->sector_buf;
Tejun Heo11fc33d2008-08-30 14:20:01 +02003115 u8 sense_key = 0;
3116 unsigned int err_mask;
3117
3118 err_mask = atapi_eh_tur(dev, &sense_key);
3119 if (err_mask != 0 && err_mask != AC_ERR_DEV) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003120 ata_dev_warn(dev,
3121 "TEST_UNIT_READY failed (err_mask=0x%x)\n",
3122 err_mask);
Tejun Heo11fc33d2008-08-30 14:20:01 +02003123 return -EIO;
3124 }
3125
3126 if (!err_mask || sense_key != UNIT_ATTENTION)
3127 return 0;
3128
3129 err_mask = atapi_eh_request_sense(dev, sense_buffer, sense_key);
3130 if (err_mask) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003131 ata_dev_warn(dev, "failed to clear "
Tejun Heo11fc33d2008-08-30 14:20:01 +02003132 "UNIT ATTENTION (err_mask=0x%x)\n", err_mask);
3133 return -EIO;
3134 }
3135 }
3136
Joe Perchesa9a79df2011-04-15 15:51:59 -07003137 ata_dev_warn(dev, "UNIT ATTENTION persists after %d tries\n",
3138 ATA_EH_UA_TRIES);
Tejun Heo11fc33d2008-08-30 14:20:01 +02003139
3140 return 0;
3141}
3142
Tejun Heo6013efd2009-11-19 15:36:45 +09003143/**
3144 * ata_eh_maybe_retry_flush - Retry FLUSH if necessary
3145 * @dev: ATA device which may need FLUSH retry
3146 *
3147 * If @dev failed FLUSH, it needs to be reported upper layer
3148 * immediately as it means that @dev failed to remap and already
3149 * lost at least a sector and further FLUSH retrials won't make
3150 * any difference to the lost sector. However, if FLUSH failed
3151 * for other reasons, for example transmission error, FLUSH needs
3152 * to be retried.
3153 *
3154 * This function determines whether FLUSH failure retry is
3155 * necessary and performs it if so.
3156 *
3157 * RETURNS:
3158 * 0 if EH can continue, -errno if EH needs to be repeated.
3159 */
3160static int ata_eh_maybe_retry_flush(struct ata_device *dev)
3161{
3162 struct ata_link *link = dev->link;
3163 struct ata_port *ap = link->ap;
3164 struct ata_queued_cmd *qc;
3165 struct ata_taskfile tf;
3166 unsigned int err_mask;
3167 int rc = 0;
3168
3169 /* did flush fail for this device? */
3170 if (!ata_tag_valid(link->active_tag))
3171 return 0;
3172
3173 qc = __ata_qc_from_tag(ap, link->active_tag);
3174 if (qc->dev != dev || (qc->tf.command != ATA_CMD_FLUSH_EXT &&
3175 qc->tf.command != ATA_CMD_FLUSH))
3176 return 0;
3177
3178 /* if the device failed it, it should be reported to upper layers */
3179 if (qc->err_mask & AC_ERR_DEV)
3180 return 0;
3181
3182 /* flush failed for some other reason, give it another shot */
3183 ata_tf_init(dev, &tf);
3184
3185 tf.command = qc->tf.command;
3186 tf.flags |= ATA_TFLAG_DEVICE;
3187 tf.protocol = ATA_PROT_NODATA;
3188
Joe Perchesa9a79df2011-04-15 15:51:59 -07003189 ata_dev_warn(dev, "retrying FLUSH 0x%x Emask 0x%x\n",
Tejun Heo6013efd2009-11-19 15:36:45 +09003190 tf.command, qc->err_mask);
3191
3192 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
3193 if (!err_mask) {
3194 /*
3195 * FLUSH is complete but there's no way to
3196 * successfully complete a failed command from EH.
3197 * Making sure retry is allowed at least once and
3198 * retrying it should do the trick - whatever was in
3199 * the cache is already on the platter and this won't
3200 * cause infinite loop.
3201 */
3202 qc->scsicmd->allowed = max(qc->scsicmd->allowed, 1);
3203 } else {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003204 ata_dev_warn(dev, "FLUSH failed Emask 0x%x\n",
Tejun Heo6013efd2009-11-19 15:36:45 +09003205 err_mask);
3206 rc = -EIO;
3207
3208 /* if device failed it, report it to upper layers */
3209 if (err_mask & AC_ERR_DEV) {
3210 qc->err_mask |= AC_ERR_DEV;
3211 qc->result_tf = tf;
3212 if (!(ap->pflags & ATA_PFLAG_FROZEN))
3213 rc = 0;
3214 }
3215 }
3216 return rc;
3217}
3218
Tejun Heo6b7ae952010-09-01 17:50:06 +02003219/**
3220 * ata_eh_set_lpm - configure SATA interface power management
3221 * @link: link to configure power management
3222 * @policy: the link power management policy
3223 * @r_failed_dev: out parameter for failed device
3224 *
3225 * Enable SATA Interface power management. This will enable
Hans de Goedef4ac6472017-09-14 12:35:36 +02003226 * Device Interface Power Management (DIPM) for min_power and
3227 * medium_power_with_dipm policies, and then call driver specific
3228 * callbacks for enabling Host Initiated Power management.
Tejun Heo6b7ae952010-09-01 17:50:06 +02003229 *
3230 * LOCKING:
3231 * EH context.
3232 *
3233 * RETURNS:
3234 * 0 on success, -errno on failure.
3235 */
3236static int ata_eh_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
3237 struct ata_device **r_failed_dev)
3238{
Tejun Heo6c8ea892010-09-01 17:50:07 +02003239 struct ata_port *ap = ata_is_host_link(link) ? link->ap : NULL;
Tejun Heo6b7ae952010-09-01 17:50:06 +02003240 struct ata_eh_context *ehc = &link->eh_context;
3241 struct ata_device *dev, *link_dev = NULL, *lpm_dev = NULL;
Tejun Heoe5005b12010-12-09 16:13:15 +01003242 enum ata_lpm_policy old_policy = link->lpm_policy;
Tejun Heo5f6f12c2011-05-09 16:04:11 +02003243 bool no_dipm = link->ap->flags & ATA_FLAG_NO_DIPM;
Tejun Heo6b7ae952010-09-01 17:50:06 +02003244 unsigned int hints = ATA_LPM_EMPTY | ATA_LPM_HIPM;
3245 unsigned int err_mask;
3246 int rc;
3247
3248 /* if the link or host doesn't do LPM, noop */
Bartlomiej Zolnierkiewicz4c9029e2020-03-26 16:58:10 +01003249 if (!IS_ENABLED(CONFIG_SATA_HOST) ||
3250 (link->flags & ATA_LFLAG_NO_LPM) || (ap && !ap->ops->set_lpm))
Tejun Heo6b7ae952010-09-01 17:50:06 +02003251 return 0;
3252
3253 /*
3254 * DIPM is enabled only for MIN_POWER as some devices
3255 * misbehave when the host NACKs transition to SLUMBER. Order
3256 * device and link configurations such that the host always
3257 * allows DIPM requests.
3258 */
3259 ata_for_each_dev(dev, link, ENABLED) {
3260 bool hipm = ata_id_has_hipm(dev->id);
Tejun Heoae01b242011-03-16 11:14:55 +01003261 bool dipm = ata_id_has_dipm(dev->id) && !no_dipm;
Tejun Heo6b7ae952010-09-01 17:50:06 +02003262
3263 /* find the first enabled and LPM enabled devices */
3264 if (!link_dev)
3265 link_dev = dev;
3266
3267 if (!lpm_dev && (hipm || dipm))
3268 lpm_dev = dev;
3269
3270 hints &= ~ATA_LPM_EMPTY;
3271 if (!hipm)
3272 hints &= ~ATA_LPM_HIPM;
3273
3274 /* disable DIPM before changing link config */
Hans de Goedef4ac6472017-09-14 12:35:36 +02003275 if (policy < ATA_LPM_MED_POWER_WITH_DIPM && dipm) {
Tejun Heo6b7ae952010-09-01 17:50:06 +02003276 err_mask = ata_dev_set_feature(dev,
3277 SETFEATURES_SATA_DISABLE, SATA_DIPM);
3278 if (err_mask && err_mask != AC_ERR_DEV) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003279 ata_dev_warn(dev,
3280 "failed to disable DIPM, Emask 0x%x\n",
3281 err_mask);
Tejun Heo6b7ae952010-09-01 17:50:06 +02003282 rc = -EIO;
3283 goto fail;
3284 }
3285 }
3286 }
3287
Tejun Heo6c8ea892010-09-01 17:50:07 +02003288 if (ap) {
3289 rc = ap->ops->set_lpm(link, policy, hints);
3290 if (!rc && ap->slave_link)
3291 rc = ap->ops->set_lpm(ap->slave_link, policy, hints);
3292 } else
3293 rc = sata_pmp_set_lpm(link, policy, hints);
Tejun Heo6b7ae952010-09-01 17:50:06 +02003294
3295 /*
3296 * Attribute link config failure to the first (LPM) enabled
3297 * device on the link.
3298 */
3299 if (rc) {
3300 if (rc == -EOPNOTSUPP) {
3301 link->flags |= ATA_LFLAG_NO_LPM;
3302 return 0;
3303 }
3304 dev = lpm_dev ? lpm_dev : link_dev;
3305 goto fail;
3306 }
3307
Tejun Heoe5005b12010-12-09 16:13:15 +01003308 /*
3309 * Low level driver acked the transition. Issue DIPM command
3310 * with the new policy set.
3311 */
3312 link->lpm_policy = policy;
3313 if (ap && ap->slave_link)
3314 ap->slave_link->lpm_policy = policy;
3315
Tejun Heo6b7ae952010-09-01 17:50:06 +02003316 /* host config updated, enable DIPM if transitioning to MIN_POWER */
3317 ata_for_each_dev(dev, link, ENABLED) {
Hans de Goedef4ac6472017-09-14 12:35:36 +02003318 if (policy >= ATA_LPM_MED_POWER_WITH_DIPM && !no_dipm &&
Tejun Heoae01b242011-03-16 11:14:55 +01003319 ata_id_has_dipm(dev->id)) {
Tejun Heo6b7ae952010-09-01 17:50:06 +02003320 err_mask = ata_dev_set_feature(dev,
3321 SETFEATURES_SATA_ENABLE, SATA_DIPM);
3322 if (err_mask && err_mask != AC_ERR_DEV) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003323 ata_dev_warn(dev,
Tejun Heo6b7ae952010-09-01 17:50:06 +02003324 "failed to enable DIPM, Emask 0x%x\n",
3325 err_mask);
3326 rc = -EIO;
3327 goto fail;
3328 }
3329 }
3330 }
3331
Gabriele Mazzotta09c5b482015-04-25 19:52:37 +02003332 link->last_lpm_change = jiffies;
3333 link->flags |= ATA_LFLAG_CHANGED;
3334
Tejun Heo6b7ae952010-09-01 17:50:06 +02003335 return 0;
3336
3337fail:
Tejun Heoe5005b12010-12-09 16:13:15 +01003338 /* restore the old policy */
3339 link->lpm_policy = old_policy;
3340 if (ap && ap->slave_link)
3341 ap->slave_link->lpm_policy = old_policy;
3342
Tejun Heo6b7ae952010-09-01 17:50:06 +02003343 /* if no device or only one more chance is left, disable LPM */
3344 if (!dev || ehc->tries[dev->devno] <= 2) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003345 ata_link_warn(link, "disabling LPM on the link\n");
Tejun Heo6b7ae952010-09-01 17:50:06 +02003346 link->flags |= ATA_LFLAG_NO_LPM;
3347 }
3348 if (r_failed_dev)
3349 *r_failed_dev = dev;
3350 return rc;
3351}
3352
Kristen Carlson Accardi8a745f12011-03-04 10:24:11 -08003353int ata_link_nr_enabled(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09003354{
Tejun Heof58229f2007-08-06 18:36:23 +09003355 struct ata_device *dev;
3356 int cnt = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09003357
Tejun Heo1eca4362008-11-03 20:03:17 +09003358 ata_for_each_dev(dev, link, ENABLED)
3359 cnt++;
Tejun Heo022bdb02006-05-15 20:58:22 +09003360 return cnt;
3361}
3362
Tejun Heo02607312007-08-06 18:36:23 +09003363static int ata_link_nr_vacant(struct ata_link *link)
Tejun Heo084fe632006-05-31 18:28:03 +09003364{
Tejun Heof58229f2007-08-06 18:36:23 +09003365 struct ata_device *dev;
3366 int cnt = 0;
Tejun Heo084fe632006-05-31 18:28:03 +09003367
Tejun Heo1eca4362008-11-03 20:03:17 +09003368 ata_for_each_dev(dev, link, ALL)
Tejun Heof58229f2007-08-06 18:36:23 +09003369 if (dev->class == ATA_DEV_UNKNOWN)
Tejun Heo084fe632006-05-31 18:28:03 +09003370 cnt++;
3371 return cnt;
3372}
3373
Tejun Heo02607312007-08-06 18:36:23 +09003374static int ata_eh_skip_recovery(struct ata_link *link)
Tejun Heo084fe632006-05-31 18:28:03 +09003375{
Tejun Heo672b2d62008-01-24 00:05:14 +09003376 struct ata_port *ap = link->ap;
Tejun Heo02607312007-08-06 18:36:23 +09003377 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heof58229f2007-08-06 18:36:23 +09003378 struct ata_device *dev;
Tejun Heo084fe632006-05-31 18:28:03 +09003379
Tejun Heof9df58c2007-09-23 13:14:13 +09003380 /* skip disabled links */
3381 if (link->flags & ATA_LFLAG_DISABLED)
3382 return 1;
3383
Tejun Heoe2f3d752010-09-07 14:05:31 +02003384 /* skip if explicitly requested */
3385 if (ehc->i.flags & ATA_EHI_NO_RECOVERY)
3386 return 1;
3387
Tejun Heo672b2d62008-01-24 00:05:14 +09003388 /* thaw frozen port and recover failed devices */
3389 if ((ap->pflags & ATA_PFLAG_FROZEN) || ata_link_nr_enabled(link))
3390 return 0;
3391
3392 /* reset at least once if reset is requested */
3393 if ((ehc->i.action & ATA_EH_RESET) &&
3394 !(ehc->i.flags & ATA_EHI_DID_RESET))
Tejun Heo084fe632006-05-31 18:28:03 +09003395 return 0;
3396
3397 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
Tejun Heo1eca4362008-11-03 20:03:17 +09003398 ata_for_each_dev(dev, link, ALL) {
Tejun Heo084fe632006-05-31 18:28:03 +09003399 if (dev->class == ATA_DEV_UNKNOWN &&
3400 ehc->classes[dev->devno] != ATA_DEV_NONE)
3401 return 0;
3402 }
3403
3404 return 1;
3405}
3406
Tejun Heoc2c7a892009-01-29 20:31:34 +09003407static int ata_count_probe_trials_cb(struct ata_ering_entry *ent, void *void_arg)
3408{
3409 u64 interval = msecs_to_jiffies(ATA_EH_PROBE_TRIAL_INTERVAL);
3410 u64 now = get_jiffies_64();
3411 int *trials = void_arg;
3412
Lin Ming68682252012-05-03 22:15:07 +08003413 if ((ent->eflags & ATA_EFLAG_OLD_ER) ||
3414 (ent->timestamp < now - min(now, interval)))
Tejun Heoc2c7a892009-01-29 20:31:34 +09003415 return -1;
3416
3417 (*trials)++;
3418 return 0;
3419}
3420
Tejun Heo02c05a22007-11-27 19:28:54 +09003421static int ata_eh_schedule_probe(struct ata_device *dev)
3422{
3423 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heoc2c7a892009-01-29 20:31:34 +09003424 struct ata_link *link = ata_dev_phys_link(dev);
3425 int trials = 0;
Tejun Heo02c05a22007-11-27 19:28:54 +09003426
3427 if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
3428 (ehc->did_probe_mask & (1 << dev->devno)))
3429 return 0;
3430
3431 ata_eh_detach_dev(dev);
3432 ata_dev_init(dev);
3433 ehc->did_probe_mask |= (1 << dev->devno);
Tejun Heocf480622008-01-24 00:05:14 +09003434 ehc->i.action |= ATA_EH_RESET;
Tejun Heo00115e02007-11-27 19:28:58 +09003435 ehc->saved_xfer_mode[dev->devno] = 0;
3436 ehc->saved_ncq_enabled &= ~(1 << dev->devno);
Tejun Heo02c05a22007-11-27 19:28:54 +09003437
Tejun Heo6b7ae952010-09-01 17:50:06 +02003438 /* the link maybe in a deep sleep, wake it up */
Tejun Heo6c8ea892010-09-01 17:50:07 +02003439 if (link->lpm_policy > ATA_LPM_MAX_POWER) {
3440 if (ata_is_host_link(link))
3441 link->ap->ops->set_lpm(link, ATA_LPM_MAX_POWER,
3442 ATA_LPM_EMPTY);
3443 else
3444 sata_pmp_set_lpm(link, ATA_LPM_MAX_POWER,
3445 ATA_LPM_EMPTY);
3446 }
Tejun Heo6b7ae952010-09-01 17:50:06 +02003447
Tejun Heoc2c7a892009-01-29 20:31:34 +09003448 /* Record and count probe trials on the ering. The specific
3449 * error mask used is irrelevant. Because a successful device
3450 * detection clears the ering, this count accumulates only if
3451 * there are consecutive failed probes.
3452 *
3453 * If the count is equal to or higher than ATA_EH_PROBE_TRIALS
3454 * in the last ATA_EH_PROBE_TRIAL_INTERVAL, link speed is
3455 * forced to 1.5Gbps.
3456 *
3457 * This is to work around cases where failed link speed
3458 * negotiation results in device misdetection leading to
3459 * infinite DEVXCHG or PHRDY CHG events.
3460 */
3461 ata_ering_record(&dev->ering, 0, AC_ERR_OTHER);
3462 ata_ering_map(&dev->ering, ata_count_probe_trials_cb, &trials);
3463
3464 if (trials > ATA_EH_PROBE_TRIALS)
3465 sata_down_spd_limit(link, 1);
3466
Tejun Heo02c05a22007-11-27 19:28:54 +09003467 return 1;
3468}
3469
Tejun Heo9b1e2652007-08-06 18:36:24 +09003470static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
Tejun Heofee7ca72007-07-01 19:05:58 +09003471{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003472 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heofee7ca72007-07-01 19:05:58 +09003473
Tejun Heocf9a5902009-01-29 20:31:35 +09003474 /* -EAGAIN from EH routine indicates retry without prejudice.
3475 * The requester is responsible for ensuring forward progress.
3476 */
3477 if (err != -EAGAIN)
3478 ehc->tries[dev->devno]--;
Tejun Heofee7ca72007-07-01 19:05:58 +09003479
3480 switch (err) {
3481 case -ENODEV:
3482 /* device missing or wrong IDENTIFY data, schedule probing */
3483 ehc->i.probe_mask |= (1 << dev->devno);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003484 fallthrough;
Tejun Heofee7ca72007-07-01 19:05:58 +09003485 case -EINVAL:
3486 /* give it just one more chance */
3487 ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003488 fallthrough;
Tejun Heofee7ca72007-07-01 19:05:58 +09003489 case -EIO:
Tejun Heod89293a2009-01-29 20:31:29 +09003490 if (ehc->tries[dev->devno] == 1) {
Tejun Heofee7ca72007-07-01 19:05:58 +09003491 /* This is the last chance, better to slow
3492 * down than lose it.
3493 */
Tejun Heoa07d4992009-01-29 20:31:33 +09003494 sata_down_spd_limit(ata_dev_phys_link(dev), 0);
Tejun Heod89293a2009-01-29 20:31:29 +09003495 if (dev->pio_mode > XFER_PIO_0)
3496 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
Tejun Heofee7ca72007-07-01 19:05:58 +09003497 }
3498 }
3499
3500 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
3501 /* disable device if it has used up all its chances */
3502 ata_dev_disable(dev);
3503
3504 /* detach if offline */
Tejun Heob1c72912008-07-31 17:02:43 +09003505 if (ata_phys_link_offline(ata_dev_phys_link(dev)))
Tejun Heofee7ca72007-07-01 19:05:58 +09003506 ata_eh_detach_dev(dev);
3507
Tejun Heo02c05a22007-11-27 19:28:54 +09003508 /* schedule probe if necessary */
Tejun Heo87fbc5a2008-05-20 02:17:54 +09003509 if (ata_eh_schedule_probe(dev)) {
Tejun Heofee7ca72007-07-01 19:05:58 +09003510 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
Tejun Heo87fbc5a2008-05-20 02:17:54 +09003511 memset(ehc->cmd_timeout_idx[dev->devno], 0,
3512 sizeof(ehc->cmd_timeout_idx[dev->devno]));
3513 }
Tejun Heo9b1e2652007-08-06 18:36:24 +09003514
3515 return 1;
Tejun Heofee7ca72007-07-01 19:05:58 +09003516 } else {
Tejun Heocf480622008-01-24 00:05:14 +09003517 ehc->i.action |= ATA_EH_RESET;
Tejun Heo9b1e2652007-08-06 18:36:24 +09003518 return 0;
Tejun Heofee7ca72007-07-01 19:05:58 +09003519 }
3520}
3521
Tejun Heo022bdb02006-05-15 20:58:22 +09003522/**
3523 * ata_eh_recover - recover host port after error
3524 * @ap: host port to recover
Tejun Heof5914a42006-05-31 18:27:48 +09003525 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09003526 * @softreset: softreset method (can be NULL)
3527 * @hardreset: hardreset method (can be NULL)
3528 * @postreset: postreset method (can be NULL)
Tejun Heo9b1e2652007-08-06 18:36:24 +09003529 * @r_failed_link: out parameter for failed link
Tejun Heo022bdb02006-05-15 20:58:22 +09003530 *
3531 * This is the alpha and omega, eum and yang, heart and soul of
3532 * libata exception handling. On entry, actions required to
Tejun Heo9b1e2652007-08-06 18:36:24 +09003533 * recover each link and hotplug requests are recorded in the
3534 * link's eh_context. This function executes all the operations
3535 * with appropriate retrials and fallbacks to resurrect failed
Tejun Heo084fe632006-05-31 18:28:03 +09003536 * devices, detach goners and greet newcomers.
Tejun Heo022bdb02006-05-15 20:58:22 +09003537 *
3538 * LOCKING:
3539 * Kernel thread context (may sleep).
3540 *
3541 * RETURNS:
3542 * 0 on success, -errno on failure.
3543 */
Tejun Heofb7fd612007-09-23 13:14:12 +09003544int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
3545 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
3546 ata_postreset_fn_t postreset,
3547 struct ata_link **r_failed_link)
Tejun Heo022bdb02006-05-15 20:58:22 +09003548{
Tejun Heo9b1e2652007-08-06 18:36:24 +09003549 struct ata_link *link;
Tejun Heo022bdb02006-05-15 20:58:22 +09003550 struct ata_device *dev;
Tejun Heo6b7ae952010-09-01 17:50:06 +02003551 int rc, nr_fails;
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02003552 unsigned long flags, deadline;
Tejun Heo022bdb02006-05-15 20:58:22 +09003553
3554 DPRINTK("ENTER\n");
3555
3556 /* prep for recovery */
Tejun Heo1eca4362008-11-03 20:03:17 +09003557 ata_for_each_link(link, ap, EDGE) {
Tejun Heo9b1e2652007-08-06 18:36:24 +09003558 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo084fe632006-05-31 18:28:03 +09003559
Tejun Heof9df58c2007-09-23 13:14:13 +09003560 /* re-enable link? */
3561 if (ehc->i.action & ATA_EH_ENABLE_LINK) {
3562 ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
3563 spin_lock_irqsave(ap->lock, flags);
3564 link->flags &= ~ATA_LFLAG_DISABLED;
3565 spin_unlock_irqrestore(ap->lock, flags);
3566 ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
3567 }
3568
Tejun Heo1eca4362008-11-03 20:03:17 +09003569 ata_for_each_dev(dev, link, ALL) {
Tejun Heofd995f72007-09-23 13:14:12 +09003570 if (link->flags & ATA_LFLAG_NO_RETRY)
3571 ehc->tries[dev->devno] = 1;
3572 else
3573 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
Tejun Heo79a55b72007-01-18 17:22:18 +09003574
Tejun Heo9b1e2652007-08-06 18:36:24 +09003575 /* collect port action mask recorded in dev actions */
3576 ehc->i.action |= ehc->i.dev_action[dev->devno] &
3577 ~ATA_EH_PERDEV_MASK;
3578 ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
Tejun Heo084fe632006-05-31 18:28:03 +09003579
Tejun Heo9b1e2652007-08-06 18:36:24 +09003580 /* process hotplug request */
3581 if (dev->flags & ATA_DFLAG_DETACH)
3582 ata_eh_detach_dev(dev);
3583
Tejun Heo02c05a22007-11-27 19:28:54 +09003584 /* schedule probe if necessary */
3585 if (!ata_dev_enabled(dev))
3586 ata_eh_schedule_probe(dev);
Tejun Heo084fe632006-05-31 18:28:03 +09003587 }
Tejun Heo022bdb02006-05-15 20:58:22 +09003588 }
3589
3590 retry:
Tejun Heo022bdb02006-05-15 20:58:22 +09003591 rc = 0;
3592
Tejun Heoaeb2ecd2006-06-12 14:11:43 +09003593 /* if UNLOADING, finish immediately */
Tejun Heob51e9e52006-06-29 01:29:30 +09003594 if (ap->pflags & ATA_PFLAG_UNLOADING)
Tejun Heoaeb2ecd2006-06-12 14:11:43 +09003595 goto out;
3596
Tejun Heo9b1e2652007-08-06 18:36:24 +09003597 /* prep for EH */
Tejun Heo1eca4362008-11-03 20:03:17 +09003598 ata_for_each_link(link, ap, EDGE) {
Tejun Heo9b1e2652007-08-06 18:36:24 +09003599 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09003600
Tejun Heo9b1e2652007-08-06 18:36:24 +09003601 /* skip EH if possible. */
3602 if (ata_eh_skip_recovery(link))
3603 ehc->i.action = 0;
3604
Tejun Heo1eca4362008-11-03 20:03:17 +09003605 ata_for_each_dev(dev, link, ALL)
Tejun Heo9b1e2652007-08-06 18:36:24 +09003606 ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
3607 }
Tejun Heo084fe632006-05-31 18:28:03 +09003608
Tejun Heo022bdb02006-05-15 20:58:22 +09003609 /* reset */
Tejun Heo1eca4362008-11-03 20:03:17 +09003610 ata_for_each_link(link, ap, EDGE) {
Tejun Heodc98c322008-05-19 01:15:07 +09003611 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09003612
Tejun Heodc98c322008-05-19 01:15:07 +09003613 if (!(ehc->i.action & ATA_EH_RESET))
3614 continue;
Tejun Heo9b1e2652007-08-06 18:36:24 +09003615
Tejun Heodc98c322008-05-19 01:15:07 +09003616 rc = ata_eh_reset(link, ata_link_nr_vacant(link),
3617 prereset, softreset, hardreset, postreset);
3618 if (rc) {
Joe Perchesa9a79df2011-04-15 15:51:59 -07003619 ata_link_err(link, "reset failed, giving up\n");
Tejun Heodc98c322008-05-19 01:15:07 +09003620 goto out;
Tejun Heo022bdb02006-05-15 20:58:22 +09003621 }
Tejun Heo022bdb02006-05-15 20:58:22 +09003622 }
3623
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02003624 do {
3625 unsigned long now;
3626
3627 /*
3628 * clears ATA_EH_PARK in eh_info and resets
3629 * ap->park_req_pending
3630 */
3631 ata_eh_pull_park_action(ap);
3632
3633 deadline = jiffies;
Tejun Heo1eca4362008-11-03 20:03:17 +09003634 ata_for_each_link(link, ap, EDGE) {
3635 ata_for_each_dev(dev, link, ALL) {
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02003636 struct ata_eh_context *ehc = &link->eh_context;
3637 unsigned long tmp;
3638
Hannes Reinecke9162c652014-11-05 13:08:21 +01003639 if (dev->class != ATA_DEV_ATA &&
3640 dev->class != ATA_DEV_ZAC)
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02003641 continue;
3642 if (!(ehc->i.dev_action[dev->devno] &
3643 ATA_EH_PARK))
3644 continue;
3645 tmp = dev->unpark_deadline;
3646 if (time_before(deadline, tmp))
3647 deadline = tmp;
3648 else if (time_before_eq(tmp, jiffies))
3649 continue;
3650 if (ehc->unloaded_mask & (1 << dev->devno))
3651 continue;
3652
3653 ata_eh_park_issue_cmd(dev, 1);
3654 }
3655 }
3656
3657 now = jiffies;
3658 if (time_before_eq(deadline, now))
3659 break;
3660
Tejun Heoc0c362b2010-09-06 17:57:14 +02003661 ata_eh_release(ap);
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02003662 deadline = wait_for_completion_timeout(&ap->park_req_pending,
3663 deadline - now);
Tejun Heoc0c362b2010-09-06 17:57:14 +02003664 ata_eh_acquire(ap);
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02003665 } while (deadline);
Tejun Heo1eca4362008-11-03 20:03:17 +09003666 ata_for_each_link(link, ap, EDGE) {
3667 ata_for_each_dev(dev, link, ALL) {
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02003668 if (!(link->eh_context.unloaded_mask &
3669 (1 << dev->devno)))
3670 continue;
3671
3672 ata_eh_park_issue_cmd(dev, 0);
3673 ata_eh_done(link, dev, ATA_EH_PARK);
3674 }
3675 }
3676
Tejun Heo9b1e2652007-08-06 18:36:24 +09003677 /* the rest */
Tejun Heo6b7ae952010-09-01 17:50:06 +02003678 nr_fails = 0;
3679 ata_for_each_link(link, ap, PMP_FIRST) {
Tejun Heo9b1e2652007-08-06 18:36:24 +09003680 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09003681
Tejun Heo6b7ae952010-09-01 17:50:06 +02003682 if (sata_pmp_attached(ap) && ata_is_host_link(link))
3683 goto config_lpm;
3684
Tejun Heo9b1e2652007-08-06 18:36:24 +09003685 /* revalidate existing devices and attach new ones */
3686 rc = ata_eh_revalidate_and_attach(link, &dev);
Tejun Heo4ae72a12007-02-02 16:22:30 +09003687 if (rc)
Tejun Heo6b7ae952010-09-01 17:50:06 +02003688 goto rest_fail;
Tejun Heo9b1e2652007-08-06 18:36:24 +09003689
Tejun Heo633273a2007-09-23 13:19:54 +09003690 /* if PMP got attached, return, pmp EH will take care of it */
3691 if (link->device->class == ATA_DEV_PMP) {
3692 ehc->i.action = 0;
3693 return 0;
3694 }
3695
Tejun Heo9b1e2652007-08-06 18:36:24 +09003696 /* configure transfer mode if necessary */
3697 if (ehc->i.flags & ATA_EHI_SETMODE) {
3698 rc = ata_set_mode(link, &dev);
3699 if (rc)
Tejun Heo6b7ae952010-09-01 17:50:06 +02003700 goto rest_fail;
Tejun Heo9b1e2652007-08-06 18:36:24 +09003701 ehc->i.flags &= ~ATA_EHI_SETMODE;
3702 }
3703
Tejun Heo11fc33d2008-08-30 14:20:01 +02003704 /* If reset has been issued, clear UA to avoid
3705 * disrupting the current users of the device.
3706 */
3707 if (ehc->i.flags & ATA_EHI_DID_RESET) {
Tejun Heo1eca4362008-11-03 20:03:17 +09003708 ata_for_each_dev(dev, link, ALL) {
Tejun Heo11fc33d2008-08-30 14:20:01 +02003709 if (dev->class != ATA_DEV_ATAPI)
3710 continue;
3711 rc = atapi_eh_clear_ua(dev);
3712 if (rc)
Tejun Heo6b7ae952010-09-01 17:50:06 +02003713 goto rest_fail;
Aaron Lu21334202013-01-15 17:21:01 +08003714 if (zpodd_dev_enabled(dev))
3715 zpodd_post_poweron(dev);
Tejun Heo11fc33d2008-08-30 14:20:01 +02003716 }
3717 }
3718
Tejun Heo6013efd2009-11-19 15:36:45 +09003719 /* retry flush if necessary */
3720 ata_for_each_dev(dev, link, ALL) {
Hannes Reinecke9162c652014-11-05 13:08:21 +01003721 if (dev->class != ATA_DEV_ATA &&
3722 dev->class != ATA_DEV_ZAC)
Tejun Heo6013efd2009-11-19 15:36:45 +09003723 continue;
3724 rc = ata_eh_maybe_retry_flush(dev);
3725 if (rc)
Tejun Heo6b7ae952010-09-01 17:50:06 +02003726 goto rest_fail;
Tejun Heo6013efd2009-11-19 15:36:45 +09003727 }
3728
Tejun Heo6b7ae952010-09-01 17:50:06 +02003729 config_lpm:
Tejun Heo11fc33d2008-08-30 14:20:01 +02003730 /* configure link power saving */
Tejun Heo6b7ae952010-09-01 17:50:06 +02003731 if (link->lpm_policy != ap->target_lpm_policy) {
3732 rc = ata_eh_set_lpm(link, ap->target_lpm_policy, &dev);
3733 if (rc)
3734 goto rest_fail;
3735 }
Kristen Carlson Accardica773292007-10-25 00:58:59 -04003736
Tejun Heo9b1e2652007-08-06 18:36:24 +09003737 /* this link is okay now */
3738 ehc->i.flags = 0;
3739 continue;
3740
Tejun Heo6b7ae952010-09-01 17:50:06 +02003741 rest_fail:
3742 nr_fails++;
3743 if (dev)
3744 ata_eh_handle_dev_fail(dev, rc);
Tejun Heo9b1e2652007-08-06 18:36:24 +09003745
Tejun Heob06ce3e2007-10-09 15:06:48 +09003746 if (ap->pflags & ATA_PFLAG_FROZEN) {
3747 /* PMP reset requires working host port.
3748 * Can't retry if it's frozen.
3749 */
Tejun Heo071f44b2008-04-07 22:47:22 +09003750 if (sata_pmp_attached(ap))
Tejun Heob06ce3e2007-10-09 15:06:48 +09003751 goto out;
Tejun Heo9b1e2652007-08-06 18:36:24 +09003752 break;
Tejun Heob06ce3e2007-10-09 15:06:48 +09003753 }
Tejun Heo022bdb02006-05-15 20:58:22 +09003754 }
3755
Tejun Heo6b7ae952010-09-01 17:50:06 +02003756 if (nr_fails)
Tejun Heo9b1e2652007-08-06 18:36:24 +09003757 goto retry;
Tejun Heo022bdb02006-05-15 20:58:22 +09003758
Tejun Heo022bdb02006-05-15 20:58:22 +09003759 out:
Tejun Heo9b1e2652007-08-06 18:36:24 +09003760 if (rc && r_failed_link)
3761 *r_failed_link = link;
Tejun Heo022bdb02006-05-15 20:58:22 +09003762
3763 DPRINTK("EXIT, rc=%d\n", rc);
3764 return rc;
3765}
3766
3767/**
3768 * ata_eh_finish - finish up EH
3769 * @ap: host port to finish EH for
3770 *
3771 * Recovery is complete. Clean up EH states and retry or finish
3772 * failed qcs.
3773 *
3774 * LOCKING:
3775 * None.
3776 */
Tejun Heofb7fd612007-09-23 13:14:12 +09003777void ata_eh_finish(struct ata_port *ap)
Tejun Heo022bdb02006-05-15 20:58:22 +09003778{
Jens Axboe258c4e52018-06-19 10:12:49 -06003779 struct ata_queued_cmd *qc;
Tejun Heo022bdb02006-05-15 20:58:22 +09003780 int tag;
3781
3782 /* retry or finish qcs */
Jens Axboe258c4e52018-06-19 10:12:49 -06003783 ata_qc_for_each_raw(ap, qc, tag) {
Tejun Heo022bdb02006-05-15 20:58:22 +09003784 if (!(qc->flags & ATA_QCFLAG_FAILED))
3785 continue;
3786
3787 if (qc->err_mask) {
3788 /* FIXME: Once EH migration is complete,
3789 * generate sense data in this function,
3790 * considering both err_mask and tf.
3791 */
Tejun Heo03faab72008-03-27 19:14:24 +09003792 if (qc->flags & ATA_QCFLAG_RETRY)
Tejun Heo022bdb02006-05-15 20:58:22 +09003793 ata_eh_qc_retry(qc);
Tejun Heo03faab72008-03-27 19:14:24 +09003794 else
3795 ata_eh_qc_complete(qc);
Tejun Heo022bdb02006-05-15 20:58:22 +09003796 } else {
3797 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
3798 ata_eh_qc_complete(qc);
3799 } else {
3800 /* feed zero TF to sense generation */
3801 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
3802 ata_eh_qc_retry(qc);
3803 }
3804 }
3805 }
Tejun Heoda917d62007-09-23 13:14:12 +09003806
3807 /* make sure nr_active_links is zero after EH */
3808 WARN_ON(ap->nr_active_links);
3809 ap->nr_active_links = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09003810}
3811
3812/**
3813 * ata_do_eh - do standard error handling
3814 * @ap: host port to handle error for
Tejun Heoa1efdab2008-03-25 12:22:50 +09003815 *
Tejun Heof5914a42006-05-31 18:27:48 +09003816 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09003817 * @softreset: softreset method (can be NULL)
3818 * @hardreset: hardreset method (can be NULL)
3819 * @postreset: postreset method (can be NULL)
3820 *
3821 * Perform standard error handling sequence.
3822 *
3823 * LOCKING:
3824 * Kernel thread context (may sleep).
3825 */
Tejun Heof5914a42006-05-31 18:27:48 +09003826void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
3827 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
3828 ata_postreset_fn_t postreset)
Tejun Heo022bdb02006-05-15 20:58:22 +09003829{
Tejun Heo9b1e2652007-08-06 18:36:24 +09003830 struct ata_device *dev;
3831 int rc;
3832
3833 ata_eh_autopsy(ap);
3834 ata_eh_report(ap);
3835
3836 rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
3837 NULL);
3838 if (rc) {
Tejun Heo1eca4362008-11-03 20:03:17 +09003839 ata_for_each_dev(dev, &ap->link, ALL)
Tejun Heo9b1e2652007-08-06 18:36:24 +09003840 ata_dev_disable(dev);
3841 }
3842
Tejun Heo022bdb02006-05-15 20:58:22 +09003843 ata_eh_finish(ap);
3844}
Tejun Heo500530f2006-07-03 16:07:27 +09003845
Tejun Heoa1efdab2008-03-25 12:22:50 +09003846/**
3847 * ata_std_error_handler - standard error handler
3848 * @ap: host port to handle error for
3849 *
3850 * Standard error handler
3851 *
3852 * LOCKING:
3853 * Kernel thread context (may sleep).
3854 */
3855void ata_std_error_handler(struct ata_port *ap)
3856{
3857 struct ata_port_operations *ops = ap->ops;
3858 ata_reset_fn_t hardreset = ops->hardreset;
3859
Tejun Heo57c9efd2008-04-07 22:47:19 +09003860 /* ignore built-in hardreset if SCR access is not available */
Tejun Heofe06e5f2010-05-10 21:41:39 +02003861 if (hardreset == sata_std_hardreset && !sata_scr_valid(&ap->link))
Tejun Heoa1efdab2008-03-25 12:22:50 +09003862 hardreset = NULL;
3863
3864 ata_do_eh(ap, ops->prereset, ops->softreset, hardreset, ops->postreset);
3865}
Bartlomiej Zolnierkiewicza52fbcf2020-03-26 16:58:04 +01003866EXPORT_SYMBOL_GPL(ata_std_error_handler);
Tejun Heoa1efdab2008-03-25 12:22:50 +09003867
Tejun Heo6ffa01d2007-03-02 17:32:47 +09003868#ifdef CONFIG_PM
Tejun Heo500530f2006-07-03 16:07:27 +09003869/**
3870 * ata_eh_handle_port_suspend - perform port suspend operation
3871 * @ap: port to suspend
3872 *
3873 * Suspend @ap.
3874 *
3875 * LOCKING:
3876 * Kernel thread context (may sleep).
3877 */
3878static void ata_eh_handle_port_suspend(struct ata_port *ap)
3879{
3880 unsigned long flags;
3881 int rc = 0;
Aaron Lu3dc67442013-01-15 17:21:00 +08003882 struct ata_device *dev;
Tejun Heo500530f2006-07-03 16:07:27 +09003883
3884 /* are we suspending? */
3885 spin_lock_irqsave(ap->lock, flags);
3886 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
Aaron Lua7ff60d2013-01-25 14:29:35 +08003887 ap->pm_mesg.event & PM_EVENT_RESUME) {
Tejun Heo500530f2006-07-03 16:07:27 +09003888 spin_unlock_irqrestore(ap->lock, flags);
3889 return;
3890 }
3891 spin_unlock_irqrestore(ap->lock, flags);
3892
3893 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
3894
Aaron Lu3dc67442013-01-15 17:21:00 +08003895 /*
3896 * If we have a ZPODD attached, check its zero
3897 * power ready status before the port is frozen.
Aaron Lua7ff60d2013-01-25 14:29:35 +08003898 * Only needed for runtime suspend.
Aaron Lu3dc67442013-01-15 17:21:00 +08003899 */
Aaron Lua7ff60d2013-01-25 14:29:35 +08003900 if (PMSG_IS_AUTO(ap->pm_mesg)) {
3901 ata_for_each_dev(dev, &ap->link, ENABLED) {
3902 if (zpodd_dev_enabled(dev))
3903 zpodd_on_suspend(dev);
3904 }
Aaron Lu3dc67442013-01-15 17:21:00 +08003905 }
3906
Tejun Heo64578a3de2007-05-15 03:28:16 +09003907 /* tell ACPI we're suspending */
3908 rc = ata_acpi_on_suspend(ap);
3909 if (rc)
3910 goto out;
3911
Tejun Heo500530f2006-07-03 16:07:27 +09003912 /* suspend */
3913 ata_eh_freeze_port(ap);
3914
3915 if (ap->ops->port_suspend)
3916 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
3917
Aaron Lua7ff60d2013-01-25 14:29:35 +08003918 ata_acpi_set_state(ap, ap->pm_mesg);
Tejun Heo64578a3de2007-05-15 03:28:16 +09003919 out:
Dan Williamsbc6e7c42014-03-14 13:52:48 -07003920 /* update the flags */
Tejun Heo500530f2006-07-03 16:07:27 +09003921 spin_lock_irqsave(ap->lock, flags);
3922
3923 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
3924 if (rc == 0)
3925 ap->pflags |= ATA_PFLAG_SUSPENDED;
Tejun Heo64578a3de2007-05-15 03:28:16 +09003926 else if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heo500530f2006-07-03 16:07:27 +09003927 ata_port_schedule_eh(ap);
3928
Tejun Heo500530f2006-07-03 16:07:27 +09003929 spin_unlock_irqrestore(ap->lock, flags);
3930
3931 return;
3932}
3933
3934/**
3935 * ata_eh_handle_port_resume - perform port resume operation
3936 * @ap: port to resume
3937 *
3938 * Resume @ap.
3939 *
Tejun Heo500530f2006-07-03 16:07:27 +09003940 * LOCKING:
3941 * Kernel thread context (may sleep).
3942 */
3943static void ata_eh_handle_port_resume(struct ata_port *ap)
3944{
Tejun Heo6f9c1ea2009-04-23 09:55:28 +09003945 struct ata_link *link;
3946 struct ata_device *dev;
Tejun Heo500530f2006-07-03 16:07:27 +09003947 unsigned long flags;
Tejun Heo500530f2006-07-03 16:07:27 +09003948
3949 /* are we resuming? */
3950 spin_lock_irqsave(ap->lock, flags);
3951 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
Aaron Lua7ff60d2013-01-25 14:29:35 +08003952 !(ap->pm_mesg.event & PM_EVENT_RESUME)) {
Tejun Heo500530f2006-07-03 16:07:27 +09003953 spin_unlock_irqrestore(ap->lock, flags);
3954 return;
3955 }
3956 spin_unlock_irqrestore(ap->lock, flags);
3957
Tejun Heo9666f402007-05-04 21:27:47 +02003958 WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
Tejun Heo500530f2006-07-03 16:07:27 +09003959
Tejun Heo6f9c1ea2009-04-23 09:55:28 +09003960 /*
3961 * Error timestamps are in jiffies which doesn't run while
3962 * suspended and PHY events during resume isn't too uncommon.
3963 * When the two are combined, it can lead to unnecessary speed
3964 * downs if the machine is suspended and resumed repeatedly.
3965 * Clear error history.
3966 */
3967 ata_for_each_link(link, ap, HOST_FIRST)
3968 ata_for_each_dev(dev, link, ALL)
3969 ata_ering_clear(&dev->ering);
3970
Aaron Lua7ff60d2013-01-25 14:29:35 +08003971 ata_acpi_set_state(ap, ap->pm_mesg);
Shaohua Libd3adca2007-11-02 09:32:38 +08003972
Tejun Heo500530f2006-07-03 16:07:27 +09003973 if (ap->ops->port_resume)
Kefeng Wangae867932017-07-13 16:07:58 +08003974 ap->ops->port_resume(ap);
Tejun Heo500530f2006-07-03 16:07:27 +09003975
Tejun Heo67465442007-05-15 03:28:16 +09003976 /* tell ACPI that we're resuming */
3977 ata_acpi_on_resume(ap);
3978
Dan Williamsbc6e7c42014-03-14 13:52:48 -07003979 /* update the flags */
Tejun Heo500530f2006-07-03 16:07:27 +09003980 spin_lock_irqsave(ap->lock, flags);
3981 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
Tejun Heo500530f2006-07-03 16:07:27 +09003982 spin_unlock_irqrestore(ap->lock, flags);
3983}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09003984#endif /* CONFIG_PM */