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