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