blob: 8219e2d7104505e4d4350f18e4052df1dda25cc0 [file] [log] [blame]
Tejun Heoece1d632006-04-02 18:51:53 +09001/*
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
Tejun Heoece1d632006-04-02 18:51:53 +090035#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>
Jeff Garzikc6fd2802006-08-10 07:31:37 -040041#include "../scsi/scsi_transport_api.h"
Tejun Heoece1d632006-04-02 18:51:53 +090042
43#include <linux/libata.h>
44
45#include "libata.h"
46
Tejun Heo7d47e8d2007-02-02 16:22:31 +090047enum {
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
Tejun Heo31daabd2007-02-02 16:50:52 +090053/* 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 */
57enum {
58 ATA_EH_PRERESET_TIMEOUT = 10 * HZ,
Tejun Heo5ddf24c2007-07-16 14:29:41 +090059 ATA_EH_FASTDRAIN_INTERVAL = 3 * HZ,
Tejun Heo31daabd2007-02-02 16:50:52 +090060};
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 */
68static 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
Tejun Heoad9e2762006-05-15 20:58:12 +090076static void __ata_port_freeze(struct ata_port *ap);
Tejun Heo720ba122006-05-31 18:28:13 +090077static void ata_eh_finish(struct ata_port *ap);
Tejun Heo6ffa01d2007-03-02 17:32:47 +090078#ifdef CONFIG_PM
Tejun Heo500530f2006-07-03 16:07:27 +090079static void ata_eh_handle_port_suspend(struct ata_port *ap);
80static void ata_eh_handle_port_resume(struct ata_port *ap);
Tejun Heo6ffa01d2007-03-02 17:32:47 +090081#else /* CONFIG_PM */
82static void ata_eh_handle_port_suspend(struct ata_port *ap)
83{ }
84
85static void ata_eh_handle_port_resume(struct ata_port *ap)
86{ }
Tejun Heo6ffa01d2007-03-02 17:32:47 +090087#endif /* CONFIG_PM */
Tejun Heoad9e2762006-05-15 20:58:12 +090088
Tejun Heob64bbc32007-07-16 14:29:39 +090089static void __ata_ehi_pushv_desc(struct ata_eh_info *ehi, const char *fmt,
90 va_list args)
91{
92 ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
93 ATA_EH_DESC_LEN - ehi->desc_len,
94 fmt, args);
95}
96
97/**
98 * __ata_ehi_push_desc - push error description without adding separator
99 * @ehi: target EHI
100 * @fmt: printf format string
101 *
102 * Format string according to @fmt and append it to @ehi->desc.
103 *
104 * LOCKING:
105 * spin_lock_irqsave(host lock)
106 */
107void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
108{
109 va_list args;
110
111 va_start(args, fmt);
112 __ata_ehi_pushv_desc(ehi, fmt, args);
113 va_end(args);
114}
115
116/**
117 * ata_ehi_push_desc - push error description with separator
118 * @ehi: target EHI
119 * @fmt: printf format string
120 *
121 * Format string according to @fmt and append it to @ehi->desc.
122 * If @ehi->desc is not empty, ", " is added in-between.
123 *
124 * LOCKING:
125 * spin_lock_irqsave(host lock)
126 */
127void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
128{
129 va_list args;
130
131 if (ehi->desc_len)
132 __ata_ehi_push_desc(ehi, ", ");
133
134 va_start(args, fmt);
135 __ata_ehi_pushv_desc(ehi, fmt, args);
136 va_end(args);
137}
138
139/**
140 * ata_ehi_clear_desc - clean error description
141 * @ehi: target EHI
142 *
143 * Clear @ehi->desc.
144 *
145 * LOCKING:
146 * spin_lock_irqsave(host lock)
147 */
148void ata_ehi_clear_desc(struct ata_eh_info *ehi)
149{
150 ehi->desc[0] = '\0';
151 ehi->desc_len = 0;
152}
153
Tejun Heocbcdd872007-08-18 13:14:55 +0900154/**
155 * ata_port_desc - append port description
156 * @ap: target ATA port
157 * @fmt: printf format string
158 *
159 * Format string according to @fmt and append it to port
160 * description. If port description is not empty, " " is added
161 * in-between. This function is to be used while initializing
162 * ata_host. The description is printed on host registration.
163 *
164 * LOCKING:
165 * None.
166 */
167void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
168{
169 va_list args;
170
171 WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
172
173 if (ap->link.eh_info.desc_len)
174 __ata_ehi_push_desc(&ap->link.eh_info, " ");
175
176 va_start(args, fmt);
177 __ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
178 va_end(args);
179}
180
181#ifdef CONFIG_PCI
182
183/**
184 * ata_port_pbar_desc - append PCI BAR description
185 * @ap: target ATA port
186 * @bar: target PCI BAR
187 * @offset: offset into PCI BAR
188 * @name: name of the area
189 *
190 * If @offset is negative, this function formats a string which
191 * contains the name, address, size and type of the BAR and
192 * appends it to the port description. If @offset is zero or
193 * positive, only name and offsetted address is appended.
194 *
195 * LOCKING:
196 * None.
197 */
198void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
199 const char *name)
200{
201 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
202 char *type = "";
203 unsigned long long start, len;
204
205 if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
206 type = "m";
207 else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
208 type = "i";
209
210 start = (unsigned long long)pci_resource_start(pdev, bar);
211 len = (unsigned long long)pci_resource_len(pdev, bar);
212
213 if (offset < 0)
214 ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
215 else
216 ata_port_desc(ap, "%s 0x%llx", name, start + offset);
217}
218
219#endif /* CONFIG_PCI */
220
Tejun Heo0c247c52006-05-15 20:58:19 +0900221static void ata_ering_record(struct ata_ering *ering, int is_io,
222 unsigned int err_mask)
223{
224 struct ata_ering_entry *ent;
225
226 WARN_ON(!err_mask);
227
228 ering->cursor++;
229 ering->cursor %= ATA_ERING_SIZE;
230
231 ent = &ering->ring[ering->cursor];
232 ent->is_io = is_io;
233 ent->err_mask = err_mask;
234 ent->timestamp = get_jiffies_64();
235}
236
Tejun Heo7d47e8d2007-02-02 16:22:31 +0900237static void ata_ering_clear(struct ata_ering *ering)
Tejun Heo0c247c52006-05-15 20:58:19 +0900238{
Tejun Heo7d47e8d2007-02-02 16:22:31 +0900239 memset(ering, 0, sizeof(*ering));
Tejun Heo0c247c52006-05-15 20:58:19 +0900240}
241
242static int ata_ering_map(struct ata_ering *ering,
243 int (*map_fn)(struct ata_ering_entry *, void *),
244 void *arg)
245{
246 int idx, rc = 0;
247 struct ata_ering_entry *ent;
248
249 idx = ering->cursor;
250 do {
251 ent = &ering->ring[idx];
252 if (!ent->err_mask)
253 break;
254 rc = map_fn(ent, arg);
255 if (rc)
256 break;
257 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
258 } while (idx != ering->cursor);
259
260 return rc;
261}
262
Tejun Heo64f65ca2006-06-24 20:30:18 +0900263static unsigned int ata_eh_dev_action(struct ata_device *dev)
264{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900265 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heo64f65ca2006-06-24 20:30:18 +0900266
267 return ehc->i.action | ehc->i.dev_action[dev->devno];
268}
269
Tejun Heof58229f2007-08-06 18:36:23 +0900270static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
Tejun Heoaf181c22006-06-24 20:30:18 +0900271 struct ata_eh_info *ehi, unsigned int action)
272{
Tejun Heof58229f2007-08-06 18:36:23 +0900273 struct ata_device *tdev;
Tejun Heoaf181c22006-06-24 20:30:18 +0900274
275 if (!dev) {
276 ehi->action &= ~action;
Tejun Heof58229f2007-08-06 18:36:23 +0900277 ata_link_for_each_dev(tdev, link)
278 ehi->dev_action[tdev->devno] &= ~action;
Tejun Heoaf181c22006-06-24 20:30:18 +0900279 } else {
280 /* doesn't make sense for port-wide EH actions */
281 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
282
283 /* break ehi->action into ehi->dev_action */
284 if (ehi->action & action) {
Tejun Heof58229f2007-08-06 18:36:23 +0900285 ata_link_for_each_dev(tdev, link)
286 ehi->dev_action[tdev->devno] |=
287 ehi->action & action;
Tejun Heoaf181c22006-06-24 20:30:18 +0900288 ehi->action &= ~action;
289 }
290
291 /* turn off the specified per-dev action */
292 ehi->dev_action[dev->devno] &= ~action;
293 }
294}
295
Tejun Heoece1d632006-04-02 18:51:53 +0900296/**
297 * ata_scsi_timed_out - SCSI layer time out callback
298 * @cmd: timed out SCSI command
299 *
300 * Handles SCSI layer timeout. We race with normal completion of
301 * the qc for @cmd. If the qc is already gone, we lose and let
302 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
303 * timed out and EH should be invoked. Prevent ata_qc_complete()
304 * from finishing it by setting EH_SCHEDULED and return
305 * EH_NOT_HANDLED.
306 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900307 * TODO: kill this function once old EH is gone.
308 *
Tejun Heoece1d632006-04-02 18:51:53 +0900309 * LOCKING:
310 * Called from timer context
311 *
312 * RETURNS:
313 * EH_HANDLED or EH_NOT_HANDLED
314 */
315enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
316{
317 struct Scsi_Host *host = cmd->device->host;
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400318 struct ata_port *ap = ata_shost_to_port(host);
Tejun Heoece1d632006-04-02 18:51:53 +0900319 unsigned long flags;
320 struct ata_queued_cmd *qc;
Tejun Heoad9e2762006-05-15 20:58:12 +0900321 enum scsi_eh_timer_return ret;
Tejun Heoece1d632006-04-02 18:51:53 +0900322
323 DPRINTK("ENTER\n");
324
Tejun Heoad9e2762006-05-15 20:58:12 +0900325 if (ap->ops->error_handler) {
326 ret = EH_NOT_HANDLED;
327 goto out;
328 }
329
330 ret = EH_HANDLED;
Jeff Garzikba6a1302006-06-22 23:46:10 -0400331 spin_lock_irqsave(ap->lock, flags);
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900332 qc = ata_qc_from_tag(ap, ap->link.active_tag);
Tejun Heoece1d632006-04-02 18:51:53 +0900333 if (qc) {
334 WARN_ON(qc->scsicmd != cmd);
335 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
336 qc->err_mask |= AC_ERR_TIMEOUT;
337 ret = EH_NOT_HANDLED;
338 }
Jeff Garzikba6a1302006-06-22 23:46:10 -0400339 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900340
Tejun Heoad9e2762006-05-15 20:58:12 +0900341 out:
Tejun Heoece1d632006-04-02 18:51:53 +0900342 DPRINTK("EXIT, ret=%d\n", ret);
343 return ret;
344}
345
346/**
347 * ata_scsi_error - SCSI layer error handler callback
348 * @host: SCSI host on which error occurred
349 *
350 * Handles SCSI-layer-thrown error events.
351 *
352 * LOCKING:
353 * Inherited from SCSI layer (none, can sleep)
354 *
355 * RETURNS:
356 * Zero.
357 */
Jeff Garzik381544b2006-04-11 13:04:39 -0400358void ata_scsi_error(struct Scsi_Host *host)
Tejun Heoece1d632006-04-02 18:51:53 +0900359{
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400360 struct ata_port *ap = ata_shost_to_port(host);
Tejun Heoad9e2762006-05-15 20:58:12 +0900361 int i, repeat_cnt = ATA_EH_MAX_REPEAT;
362 unsigned long flags;
Tejun Heoece1d632006-04-02 18:51:53 +0900363
364 DPRINTK("ENTER\n");
365
Tejun Heoad9e2762006-05-15 20:58:12 +0900366 /* synchronize with port task */
Tejun Heoece1d632006-04-02 18:51:53 +0900367 ata_port_flush_task(ap);
368
Jeff Garzikcca39742006-08-24 03:19:22 -0400369 /* synchronize with host lock and sort out timeouts */
Tejun Heoece1d632006-04-02 18:51:53 +0900370
Tejun Heoad9e2762006-05-15 20:58:12 +0900371 /* For new EH, all qcs are finished in one of three ways -
372 * normal completion, error completion, and SCSI timeout.
373 * Both cmpletions can race against SCSI timeout. When normal
374 * completion wins, the qc never reaches EH. When error
375 * completion wins, the qc has ATA_QCFLAG_FAILED set.
376 *
377 * When SCSI timeout wins, things are a bit more complex.
378 * Normal or error completion can occur after the timeout but
379 * before this point. In such cases, both types of
380 * completions are honored. A scmd is determined to have
381 * timed out iff its associated qc is active and not failed.
382 */
383 if (ap->ops->error_handler) {
384 struct scsi_cmnd *scmd, *tmp;
385 int nr_timedout = 0;
Tejun Heoece1d632006-04-02 18:51:53 +0900386
Tejun Heoe30349d2006-07-03 03:02:15 +0900387 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900388
389 list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
390 struct ata_queued_cmd *qc;
391
392 for (i = 0; i < ATA_MAX_QUEUE; i++) {
393 qc = __ata_qc_from_tag(ap, i);
394 if (qc->flags & ATA_QCFLAG_ACTIVE &&
395 qc->scsicmd == scmd)
396 break;
397 }
398
399 if (i < ATA_MAX_QUEUE) {
400 /* the scmd has an associated qc */
401 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
402 /* which hasn't failed yet, timeout */
403 qc->err_mask |= AC_ERR_TIMEOUT;
404 qc->flags |= ATA_QCFLAG_FAILED;
405 nr_timedout++;
406 }
407 } else {
408 /* Normal completion occurred after
409 * SCSI timeout but before this point.
410 * Successfully complete it.
411 */
412 scmd->retries = scmd->allowed;
413 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
414 }
415 }
416
417 /* If we have timed out qcs. They belong to EH from
418 * this point but the state of the controller is
419 * unknown. Freeze the port to make sure the IRQ
420 * handler doesn't diddle with those qcs. This must
421 * be done atomically w.r.t. setting QCFLAG_FAILED.
422 */
423 if (nr_timedout)
424 __ata_port_freeze(ap);
425
Tejun Heoe30349d2006-07-03 03:02:15 +0900426 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900427 } else
Tejun Heoe30349d2006-07-03 03:02:15 +0900428 spin_unlock_wait(ap->lock);
Tejun Heoad9e2762006-05-15 20:58:12 +0900429
430 repeat:
431 /* invoke error handler */
432 if (ap->ops->error_handler) {
Tejun Heocf1b86c2007-08-06 18:36:23 +0900433 struct ata_link *link;
434
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900435 /* kill fast drain timer */
436 del_timer_sync(&ap->fastdrain_timer);
437
Tejun Heo500530f2006-07-03 16:07:27 +0900438 /* process port resume request */
439 ata_eh_handle_port_resume(ap);
440
Tejun Heof3e81b12006-05-15 20:58:21 +0900441 /* fetch & clear EH info */
Tejun Heoe30349d2006-07-03 03:02:15 +0900442 spin_lock_irqsave(ap->lock, flags);
Tejun Heof3e81b12006-05-15 20:58:21 +0900443
Tejun Heocf1b86c2007-08-06 18:36:23 +0900444 __ata_port_for_each_link(link, ap) {
445 memset(&link->eh_context, 0, sizeof(link->eh_context));
446 link->eh_context.i = link->eh_info;
447 memset(&link->eh_info, 0, sizeof(link->eh_info));
448 }
Tejun Heof3e81b12006-05-15 20:58:21 +0900449
Tejun Heob51e9e52006-06-29 01:29:30 +0900450 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
451 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
Tejun Heof3e81b12006-05-15 20:58:21 +0900452
Tejun Heoe30349d2006-07-03 03:02:15 +0900453 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900454
Tejun Heo500530f2006-07-03 16:07:27 +0900455 /* invoke EH, skip if unloading or suspended */
456 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
Tejun Heo720ba122006-05-31 18:28:13 +0900457 ap->ops->error_handler(ap);
458 else
459 ata_eh_finish(ap);
Tejun Heoad9e2762006-05-15 20:58:12 +0900460
Tejun Heo500530f2006-07-03 16:07:27 +0900461 /* process port suspend request */
462 ata_eh_handle_port_suspend(ap);
463
Tejun Heoad9e2762006-05-15 20:58:12 +0900464 /* Exception might have happend after ->error_handler
465 * recovered the port but before this point. Repeat
466 * EH in such case.
467 */
Tejun Heoe30349d2006-07-03 03:02:15 +0900468 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900469
Tejun Heob51e9e52006-06-29 01:29:30 +0900470 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
Tejun Heoad9e2762006-05-15 20:58:12 +0900471 if (--repeat_cnt) {
472 ata_port_printk(ap, KERN_INFO,
473 "EH pending after completion, "
474 "repeating EH (cnt=%d)\n", repeat_cnt);
Tejun Heoe30349d2006-07-03 03:02:15 +0900475 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900476 goto repeat;
477 }
478 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
479 "tries, giving up\n", ATA_EH_MAX_REPEAT);
Tejun Heo914616a2007-06-25 21:47:11 +0900480 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
Tejun Heoad9e2762006-05-15 20:58:12 +0900481 }
482
Tejun Heof3e81b12006-05-15 20:58:21 +0900483 /* this run is complete, make sure EH info is clear */
Tejun Heocf1b86c2007-08-06 18:36:23 +0900484 __ata_port_for_each_link(link, ap)
485 memset(&link->eh_info, 0, sizeof(link->eh_info));
Tejun Heof3e81b12006-05-15 20:58:21 +0900486
Tejun Heoe30349d2006-07-03 03:02:15 +0900487 /* Clear host_eh_scheduled while holding ap->lock such
Tejun Heoad9e2762006-05-15 20:58:12 +0900488 * 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
Tejun Heoe30349d2006-07-03 03:02:15 +0900494 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900495 } else {
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900496 WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
Tejun Heoad9e2762006-05-15 20:58:12 +0900497 ap->ops->eng_timeout(ap);
498 }
499
500 /* finish or retry handled scmd's and clean up */
Tejun Heoece1d632006-04-02 18:51:53 +0900501 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
502
503 scsi_eh_flush_done_q(&ap->eh_done_q);
504
Tejun Heoad9e2762006-05-15 20:58:12 +0900505 /* clean up */
Tejun Heoe30349d2006-07-03 03:02:15 +0900506 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900507
Tejun Heo1cdaf532006-07-03 16:07:26 +0900508 if (ap->pflags & ATA_PFLAG_LOADING)
Tejun Heob51e9e52006-06-29 01:29:30 +0900509 ap->pflags &= ~ATA_PFLAG_LOADING;
Tejun Heo1cdaf532006-07-03 16:07:26 +0900510 else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
David Howells52bad642006-11-22 14:54:01 +0000511 queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0);
Tejun Heo1cdaf532006-07-03 16:07:26 +0900512
513 if (ap->pflags & ATA_PFLAG_RECOVERED)
514 ata_port_printk(ap, KERN_INFO, "EH complete\n");
Tejun Heo580b2102006-05-31 18:28:05 +0900515
Tejun Heob51e9e52006-06-29 01:29:30 +0900516 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
Tejun Heoad9e2762006-05-15 20:58:12 +0900517
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900518 /* tell wait_eh that we're done */
Tejun Heob51e9e52006-06-29 01:29:30 +0900519 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900520 wake_up_all(&ap->eh_wait_q);
521
Tejun Heoe30349d2006-07-03 03:02:15 +0900522 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900523
Tejun Heoece1d632006-04-02 18:51:53 +0900524 DPRINTK("EXIT\n");
Tejun Heoece1d632006-04-02 18:51:53 +0900525}
526
527/**
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900528 * 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 */
536void ata_port_wait_eh(struct ata_port *ap)
537{
538 unsigned long flags;
539 DEFINE_WAIT(wait);
540
541 retry:
Jeff Garzikba6a1302006-06-22 23:46:10 -0400542 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900543
Tejun Heob51e9e52006-06-29 01:29:30 +0900544 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900545 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
Jeff Garzikba6a1302006-06-22 23:46:10 -0400546 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900547 schedule();
Jeff Garzikba6a1302006-06-22 23:46:10 -0400548 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900549 }
Tejun Heo0a1b6222006-06-11 11:01:38 +0900550 finish_wait(&ap->eh_wait_q, &wait);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900551
Jeff Garzikba6a1302006-06-22 23:46:10 -0400552 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900553
554 /* make sure SCSI EH is complete */
Jeff Garzikcca39742006-08-24 03:19:22 -0400555 if (scsi_host_in_recovery(ap->scsi_host)) {
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900556 msleep(10);
557 goto retry;
558 }
559}
560
561/**
Tejun Heoece1d632006-04-02 18:51:53 +0900562 * 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 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900576 * TODO: kill this function once old EH is gone.
577 *
Tejun Heoece1d632006-04-02 18:51:53 +0900578 * LOCKING:
579 * Inherited from SCSI layer (none, can sleep)
580 */
581static void ata_qc_timeout(struct ata_queued_cmd *qc)
582{
583 struct ata_port *ap = qc->ap;
Tejun Heoece1d632006-04-02 18:51:53 +0900584 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
Jeff Garzikba6a1302006-06-22 23:46:10 -0400591 spin_lock_irqsave(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900592
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
Tejun Heof15a1da2006-05-15 20:57:56 +0900611 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);
Tejun Heoece1d632006-04-02 18:51:53 +0900614
615 /* complete taskfile transaction */
Jeff Garzikc13b56a2006-04-02 10:34:24 -0400616 qc->err_mask |= AC_ERR_TIMEOUT;
Tejun Heoece1d632006-04-02 18:51:53 +0900617 break;
618 }
619
Jeff Garzikba6a1302006-06-22 23:46:10 -0400620 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900621
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 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900642 * TODO: kill this function once old EH is gone.
643 *
Tejun Heoece1d632006-04-02 18:51:53 +0900644 * LOCKING:
645 * Inherited from SCSI layer (none, can sleep)
646 */
647void ata_eng_timeout(struct ata_port *ap)
648{
649 DPRINTK("ENTER\n");
650
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900651 ata_qc_timeout(ata_qc_from_tag(ap, ap->link.active_tag));
Tejun Heoece1d632006-04-02 18:51:53 +0900652
653 DPRINTK("EXIT\n");
654}
655
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900656static 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
669void 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 */
720static 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
Tejun Heof686bcb2006-05-15 20:58:05 +0900744/**
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:
Jeff Garzikcca39742006-08-24 03:19:22 -0400752 * spin_lock_irqsave(host lock)
Tejun Heof686bcb2006-05-15 20:58:05 +0900753 */
754void 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;
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900761 ata_eh_set_pending(ap, 1);
Tejun Heof686bcb2006-05-15 20:58:05 +0900762
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
Tejun Heo7b70fc02006-05-15 20:58:07 +0900771/**
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:
Jeff Garzikcca39742006-08-24 03:19:22 -0400779 * spin_lock_irqsave(host lock)
Tejun Heo7b70fc02006-05-15 20:58:07 +0900780 */
781void ata_port_schedule_eh(struct ata_port *ap)
782{
783 WARN_ON(!ap->ops->error_handler);
784
Tejun Heof4d6d002007-05-01 11:50:15 +0200785 if (ap->pflags & ATA_PFLAG_INITIALIZING)
786 return;
787
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900788 ata_eh_set_pending(ap, 1);
Jeff Garzikcca39742006-08-24 03:19:22 -0400789 scsi_schedule_eh(ap->scsi_host);
Tejun Heo7b70fc02006-05-15 20:58:07 +0900790
791 DPRINTK("port EH scheduled\n");
792}
793
Tejun Heodbd82612007-08-06 18:36:23 +0900794static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
Tejun Heo7b70fc02006-05-15 20:58:07 +0900795{
796 int tag, nr_aborted = 0;
797
798 WARN_ON(!ap->ops->error_handler);
799
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900800 /* we're gonna abort all commands, no need for fast drain */
801 ata_eh_set_pending(ap, 0);
802
Tejun Heo7b70fc02006-05-15 20:58:07 +0900803 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
804 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
805
Tejun Heodbd82612007-08-06 18:36:23 +0900806 if (qc && (!link || qc->dev->link == link)) {
Tejun Heo7b70fc02006-05-15 20:58:07 +0900807 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
Tejun Heoe3180492006-05-15 20:58:09 +0900819/**
Tejun Heodbd82612007-08-06 18:36:23 +0900820 * 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 */
831int 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 */
848int ata_port_abort(struct ata_port *ap)
849{
850 return ata_do_link_abort(ap, NULL);
851}
852
853/**
Tejun Heoe3180492006-05-15 20:58:09 +0900854 * __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:
Jeff Garzikcca39742006-08-24 03:19:22 -0400869 * spin_lock_irqsave(host lock)
Tejun Heoe3180492006-05-15 20:58:09 +0900870 */
871static 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
Tejun Heob51e9e52006-06-29 01:29:30 +0900878 ap->pflags |= ATA_PFLAG_FROZEN;
Tejun Heoe3180492006-05-15 20:58:09 +0900879
Tejun Heo44877b42007-02-21 01:06:51 +0900880 DPRINTK("ata%u port frozen\n", ap->print_id);
Tejun Heoe3180492006-05-15 20:58:09 +0900881}
882
883/**
884 * ata_port_freeze - abort & freeze port
885 * @ap: ATA port to freeze
886 *
887 * Abort and freeze @ap.
888 *
889 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400890 * spin_lock_irqsave(host lock)
Tejun Heoe3180492006-05-15 20:58:09 +0900891 *
892 * RETURNS:
893 * Number of aborted commands.
894 */
895int 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 */
916void ata_eh_freeze_port(struct ata_port *ap)
917{
918 unsigned long flags;
919
920 if (!ap->ops->error_handler)
921 return;
922
Jeff Garzikba6a1302006-06-22 23:46:10 -0400923 spin_lock_irqsave(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900924 __ata_port_freeze(ap);
Jeff Garzikba6a1302006-06-22 23:46:10 -0400925 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900926}
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 */
937void ata_eh_thaw_port(struct ata_port *ap)
938{
939 unsigned long flags;
940
941 if (!ap->ops->error_handler)
942 return;
943
Jeff Garzikba6a1302006-06-22 23:46:10 -0400944 spin_lock_irqsave(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900945
Tejun Heob51e9e52006-06-29 01:29:30 +0900946 ap->pflags &= ~ATA_PFLAG_FROZEN;
Tejun Heoe3180492006-05-15 20:58:09 +0900947
948 if (ap->ops->thaw)
949 ap->ops->thaw(ap);
950
Jeff Garzikba6a1302006-06-22 23:46:10 -0400951 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900952
Tejun Heo44877b42007-02-21 01:06:51 +0900953 DPRINTK("ata%u port thawed\n", ap->print_id);
Tejun Heoe3180492006-05-15 20:58:09 +0900954}
955
Tejun Heoece1d632006-04-02 18:51:53 +0900956static void ata_eh_scsidone(struct scsi_cmnd *scmd)
957{
958 /* nada */
959}
960
961static 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
Jeff Garzikba6a1302006-06-22 23:46:10 -0400967 spin_lock_irqsave(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900968 qc->scsidone = ata_eh_scsidone;
969 __ata_qc_complete(qc);
970 WARN_ON(ata_tag_valid(qc->tag));
Jeff Garzikba6a1302006-06-22 23:46:10 -0400971 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900972
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 */
983void 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 */
1001void 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}
Tejun Heo022bdb02006-05-15 20:58:22 +09001008
1009/**
Tejun Heo0ea035a2006-05-31 18:28:01 +09001010 * ata_eh_detach_dev - detach ATA device
1011 * @dev: ATA device to detach
1012 *
1013 * Detach @dev.
1014 *
1015 * LOCKING:
1016 * None.
1017 */
1018static void ata_eh_detach_dev(struct ata_device *dev)
1019{
Tejun Heof58229f2007-08-06 18:36:23 +09001020 struct ata_link *link = dev->link;
1021 struct ata_port *ap = link->ap;
Tejun Heo0ea035a2006-05-31 18:28:01 +09001022 unsigned long flags;
1023
1024 ata_dev_disable(dev);
1025
Jeff Garzikba6a1302006-06-22 23:46:10 -04001026 spin_lock_irqsave(ap->lock, flags);
Tejun Heo0ea035a2006-05-31 18:28:01 +09001027
1028 dev->flags &= ~ATA_DFLAG_DETACH;
1029
1030 if (ata_scsi_offline_dev(dev)) {
1031 dev->flags |= ATA_DFLAG_DETACHED;
Tejun Heob51e9e52006-06-29 01:29:30 +09001032 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
Tejun Heo0ea035a2006-05-31 18:28:01 +09001033 }
1034
Tejun Heobeb07c12006-06-24 20:30:19 +09001035 /* clear per-dev EH actions */
Tejun Heof58229f2007-08-06 18:36:23 +09001036 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);
Tejun Heobeb07c12006-06-24 20:30:19 +09001038
Jeff Garzikba6a1302006-06-22 23:46:10 -04001039 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo0ea035a2006-05-31 18:28:01 +09001040}
1041
1042/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001043 * ata_eh_about_to_do - about to perform eh_action
Tejun Heo955e57d2007-08-06 18:36:23 +09001044 * @link: target ATA link
Tejun Heo47005f22006-06-19 18:27:23 +09001045 * @dev: target ATA dev for per-dev action (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09001046 * @action: action about to be performed
1047 *
1048 * Called just before performing EH actions to clear related bits
Tejun Heo955e57d2007-08-06 18:36:23 +09001049 * in @link->eh_info such that eh actions are not unnecessarily
1050 * repeated.
Tejun Heo022bdb02006-05-15 20:58:22 +09001051 *
1052 * LOCKING:
1053 * None.
1054 */
Tejun Heo955e57d2007-08-06 18:36:23 +09001055static void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
Tejun Heo47005f22006-06-19 18:27:23 +09001056 unsigned int action)
Tejun Heo022bdb02006-05-15 20:58:22 +09001057{
Tejun Heo955e57d2007-08-06 18:36:23 +09001058 struct ata_port *ap = link->ap;
1059 struct ata_eh_info *ehi = &link->eh_info;
1060 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001061 unsigned long flags;
1062
Jeff Garzikba6a1302006-06-22 23:46:10 -04001063 spin_lock_irqsave(ap->lock, flags);
Tejun Heo1cdaf532006-07-03 16:07:26 +09001064
Tejun Heo13abf502006-07-10 23:18:46 +09001065 /* 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;
Tejun Heo1cdaf532006-07-03 16:07:26 +09001072
Tejun Heo13abf502006-07-10 23:18:46 +09001073 /* 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
Tejun Heo955e57d2007-08-06 18:36:23 +09001078 ata_eh_clear_action(link, dev, ehi, action);
Tejun Heo13abf502006-07-10 23:18:46 +09001079
1080 if (!(ehc->i.flags & ATA_EHI_QUIET))
Tejun Heo1cdaf532006-07-03 16:07:26 +09001081 ap->pflags |= ATA_PFLAG_RECOVERED;
1082
Jeff Garzikba6a1302006-06-22 23:46:10 -04001083 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo022bdb02006-05-15 20:58:22 +09001084}
1085
1086/**
Tejun Heo47005f22006-06-19 18:27:23 +09001087 * ata_eh_done - EH action complete
Tejun Heo955e57d2007-08-06 18:36:23 +09001088* @ap: target ATA port
Tejun Heo47005f22006-06-19 18:27:23 +09001089 * @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
Tejun Heo955e57d2007-08-06 18:36:23 +09001093 * in @link->eh_context.
Tejun Heo47005f22006-06-19 18:27:23 +09001094 *
1095 * LOCKING:
1096 * None.
1097 */
Tejun Heo955e57d2007-08-06 18:36:23 +09001098static void ata_eh_done(struct ata_link *link, struct ata_device *dev,
Tejun Heo47005f22006-06-19 18:27:23 +09001099 unsigned int action)
1100{
Tejun Heo955e57d2007-08-06 18:36:23 +09001101 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001102
Tejun Heo13abf502006-07-10 23:18:46 +09001103 /* if reset is complete, clear all reset actions & reset modifier */
1104 if (action & ATA_EH_RESET_MASK) {
1105 action |= ATA_EH_RESET_MASK;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001106 ehc->i.flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
Tejun Heo13abf502006-07-10 23:18:46 +09001107 }
1108
Tejun Heo955e57d2007-08-06 18:36:23 +09001109 ata_eh_clear_action(link, dev, &ehc->i, action);
Tejun Heo47005f22006-06-19 18:27:23 +09001110}
1111
1112/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001113 * 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 */
1126static 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
1147/**
Tejun Heoe8ee8452006-05-15 21:03:46 +09001148 * 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 */
1162static 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 */
1200static int ata_eh_read_log_10h(struct ata_device *dev,
1201 int *tag, struct ata_taskfile *tf)
1202{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001203 u8 *buf = dev->link->ap->sector_buf;
Tejun Heoe8ee8452006-05-15 21:03:46 +09001204 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
1239/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001240 * 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 */
Albert Lee56287762007-04-02 11:30:46 +08001253static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc)
Tejun Heo022bdb02006-05-15 20:58:22 +09001254{
Albert Lee56287762007-04-02 11:30:46 +08001255 struct ata_device *dev = qc->dev;
1256 unsigned char *sense_buf = qc->scsicmd->sense_buffer;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001257 struct ata_port *ap = dev->link->ap;
Tejun Heo022bdb02006-05-15 20:58:22 +09001258 struct ata_taskfile tf;
1259 u8 cdb[ATAPI_CDB_LEN];
1260
1261 DPRINTK("ATAPI request sense\n");
1262
Tejun Heo022bdb02006-05-15 20:58:22 +09001263 /* FIXME: is this needed? */
1264 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1265
Albert Lee56287762007-04-02 11:30:46 +08001266 /* initialize sense_buf with the error register,
1267 * for the case where they are -not- overwritten
1268 */
Tejun Heo022bdb02006-05-15 20:58:22 +09001269 sense_buf[0] = 0x70;
Albert Lee56287762007-04-02 11:30:46 +08001270 sense_buf[2] = qc->result_tf.feature >> 4;
1271
Jeff Garzika617c092007-05-21 20:14:23 -04001272 /* some devices time out if garbage left in tf */
Albert Lee56287762007-04-02 11:30:46 +08001273 ata_tf_init(dev, &tf);
Tejun Heo022bdb02006-05-15 20:58:22 +09001274
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
Tejun Heo02607312007-08-06 18:36:23 +09001298 * @link: ATA link to analyze SError for
Tejun Heo022bdb02006-05-15 20:58:22 +09001299 *
1300 * Analyze SError if available and further determine cause of
1301 * failure.
1302 *
1303 * LOCKING:
1304 * None.
1305 */
Tejun Heo02607312007-08-06 18:36:23 +09001306static void ata_eh_analyze_serror(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001307{
Tejun Heo02607312007-08-06 18:36:23 +09001308 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001309 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;
Tejun Heo771b8da2007-03-14 01:20:51 +09001327 action |= ATA_EH_HARDRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001328 }
Tejun Heo084fe632006-05-31 18:28:03 +09001329 if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG))
1330 ata_ehi_hotplugged(&ehc->i);
Tejun Heo022bdb02006-05-15 20:58:22 +09001331
1332 ehc->i.err_mask |= err_mask;
1333 ehc->i.action |= action;
1334}
1335
1336/**
Tejun Heoe8ee8452006-05-15 21:03:46 +09001337 * ata_eh_analyze_ncq_error - analyze NCQ error
Tejun Heo02607312007-08-06 18:36:23 +09001338 * @link: ATA link to analyze NCQ error for
Tejun Heoe8ee8452006-05-15 21:03:46 +09001339 *
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 */
Tejun Heo02607312007-08-06 18:36:23 +09001348static void ata_eh_analyze_ncq_error(struct ata_link *link)
Tejun Heoe8ee8452006-05-15 21:03:46 +09001349{
Tejun Heo02607312007-08-06 18:36:23 +09001350 struct ata_port *ap = link->ap;
1351 struct ata_eh_context *ehc = &link->eh_context;
1352 struct ata_device *dev = link->device;
Tejun Heoe8ee8452006-05-15 21:03:46 +09001353 struct ata_queued_cmd *qc;
1354 struct ata_taskfile tf;
1355 int tag, rc;
1356
1357 /* if frozen, we can't do much */
Tejun Heob51e9e52006-06-29 01:29:30 +09001358 if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heoe8ee8452006-05-15 21:03:46 +09001359 return;
1360
1361 /* is it NCQ device error? */
Tejun Heo02607312007-08-06 18:36:23 +09001362 if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
Tejun Heoe8ee8452006-05-15 21:03:46 +09001363 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) {
Tejun Heo02607312007-08-06 18:36:23 +09001379 ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
Tejun Heoe8ee8452006-05-15 21:03:46 +09001380 "(errno=%d)\n", rc);
1381 return;
1382 }
1383
Tejun Heo02607312007-08-06 18:36:23 +09001384 if (!(link->sactive & (1 << tag))) {
1385 ata_link_printk(link, KERN_ERR, "log page 10h reported "
Tejun Heoe8ee8452006-05-15 21:03:46 +09001386 "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));
Tejun Heo5335b722007-07-16 14:29:40 +09001393 qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
Tejun Heoe8ee8452006-05-15 21:03:46 +09001394 ehc->i.err_mask &= ~AC_ERR_DEV;
1395}
1396
1397/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001398 * 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 */
1412static 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
Tejun Heoa51d6442007-03-20 15:24:11 +09001423 if (stat & (ATA_ERR | ATA_DF))
1424 qc->err_mask |= AC_ERR_DEV;
1425 else
Tejun Heo022bdb02006-05-15 20:58:22 +09001426 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:
Tejun Heoa569a302006-11-21 10:40:51 +09001439 if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
Albert Lee56287762007-04-02 11:30:46 +08001440 tmp = atapi_eh_request_sense(qc);
Tejun Heoa569a302006-11-21 10:40:51 +09001441 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 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001453 }
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
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001461static int ata_eh_categorize_error(int is_io, unsigned int err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001462{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001463 if (err_mask & AC_ERR_ATA_BUS)
Tejun Heo022bdb02006-05-15 20:58:22 +09001464 return 1;
1465
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001466 if (err_mask & AC_ERR_TIMEOUT)
1467 return 2;
1468
1469 if (is_io) {
1470 if (err_mask & AC_ERR_HSM)
Tejun Heo022bdb02006-05-15 20:58:22 +09001471 return 2;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001472 if ((err_mask &
1473 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1474 return 3;
Tejun Heo022bdb02006-05-15 20:58:22 +09001475 }
1476
1477 return 0;
1478}
1479
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001480struct speed_down_verdict_arg {
Tejun Heo022bdb02006-05-15 20:58:22 +09001481 u64 since;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001482 int nr_errors[4];
Tejun Heo022bdb02006-05-15 20:58:22 +09001483};
1484
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001485static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
Tejun Heo022bdb02006-05-15 20:58:22 +09001486{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001487 struct speed_down_verdict_arg *arg = void_arg;
1488 int cat = ata_eh_categorize_error(ent->is_io, ent->err_mask);
Tejun Heo022bdb02006-05-15 20:58:22 +09001489
1490 if (ent->timestamp < arg->since)
1491 return -1;
1492
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001493 arg->nr_errors[cat]++;
Tejun Heo022bdb02006-05-15 20:58:22 +09001494 return 0;
1495}
1496
1497/**
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001498 * ata_eh_speed_down_verdict - Determine speed down verdict
Tejun Heo022bdb02006-05-15 20:58:22 +09001499 * @dev: Device of interest
1500 *
1501 * This function examines error ring of @dev and determines
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001502 * whether NCQ needs to be turned off, transfer speed should be
1503 * stepped down, or falling back to PIO is necessary.
Tejun Heo022bdb02006-05-15 20:58:22 +09001504 *
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001505 * Cat-1 is ATA_BUS error for any command.
Tejun Heo022bdb02006-05-15 20:58:22 +09001506 *
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001507 * 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
Tejun Heo022bdb02006-05-15 20:58:22 +09001511 * command.
1512 *
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001513 * 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 *
Tejun Heo022bdb02006-05-15 20:58:22 +09001522 * LOCKING:
1523 * Inherited from caller.
1524 *
1525 * RETURNS:
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001526 * OR of ATA_EH_SPDN_* flags.
Tejun Heo022bdb02006-05-15 20:58:22 +09001527 */
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001528static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
Tejun Heo022bdb02006-05-15 20:58:22 +09001529{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001530 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;
Tejun Heo022bdb02006-05-15 20:58:22 +09001534
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001535 /* scan past 10 mins of error history */
Tejun Heo022bdb02006-05-15 20:58:22 +09001536 memset(&arg, 0, sizeof(arg));
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001537 arg.since = j64 - min(j64, j10mins);
1538 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
Tejun Heo022bdb02006-05-15 20:58:22 +09001539
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001540 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;
Tejun Heo022bdb02006-05-15 20:58:22 +09001544
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001545 /* scan past 3 mins of error history */
1546 memset(&arg, 0, sizeof(arg));
1547 arg.since = j64 - min(j64, j5mins);
1548 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
Tejun Heo022bdb02006-05-15 20:58:22 +09001549
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001550 if (arg.nr_errors[1] + arg.nr_errors[2] + arg.nr_errors[3] > 10)
1551 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
1552
1553 return verdict;
Tejun Heo022bdb02006-05-15 20:58:22 +09001554}
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:
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001571 * Determined recovery action.
Tejun Heo022bdb02006-05-15 20:58:22 +09001572 */
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001573static unsigned int ata_eh_speed_down(struct ata_device *dev, int is_io,
1574 unsigned int err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001575{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001576 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)
Tejun Heo022bdb02006-05-15 20:58:22 +09001581 return 0;
1582
1583 /* record error and determine whether speed down is necessary */
1584 ata_ering_record(&dev->ering, is_io, err_mask);
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001585 verdict = ata_eh_speed_down_verdict(dev);
Tejun Heo022bdb02006-05-15 20:58:22 +09001586
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001587 /* 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 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001596
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001597 /* speed down? */
1598 if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1599 /* speed down SATA link speed if possible */
Tejun Heo936fd732007-08-06 18:36:23 +09001600 if (sata_down_spd_limit(dev->link) == 0) {
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001601 action |= ATA_EH_HARDRESET;
1602 goto done;
1603 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001604
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001605 /* 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;
Tejun Heo022bdb02006-05-15 20:58:22 +09001612
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001613 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) &&
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001631 (dev->link->ap->cbl != ATA_CBL_SATA) &&
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001632 (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 }
1639
Tejun Heo022bdb02006-05-15 20:58:22 +09001640 return 0;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001641 done:
1642 /* device has been slowed down, blow error history */
1643 ata_ering_clear(&dev->ering);
1644 return action;
Tejun Heo022bdb02006-05-15 20:58:22 +09001645}
1646
1647/**
Tejun Heo9b1e2652007-08-06 18:36:24 +09001648 * ata_eh_link_autopsy - analyze error and determine recovery action
1649 * @link: host link to perform autopsy on
Tejun Heo022bdb02006-05-15 20:58:22 +09001650 *
Tejun Heo02607312007-08-06 18:36:23 +09001651 * 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.
Tejun Heo022bdb02006-05-15 20:58:22 +09001654 *
1655 * LOCKING:
1656 * Kernel thread context (may sleep).
1657 */
Tejun Heo9b1e2652007-08-06 18:36:24 +09001658static void ata_eh_link_autopsy(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001659{
Tejun Heo02607312007-08-06 18:36:23 +09001660 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09001661 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001662 unsigned int all_err_mask = 0;
1663 int tag, is_io = 0;
1664 u32 serror;
1665 int rc;
1666
1667 DPRINTK("ENTER\n");
1668
Tejun Heo1cdaf532006-07-03 16:07:26 +09001669 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1670 return;
1671
Tejun Heo022bdb02006-05-15 20:58:22 +09001672 /* obtain and analyze SError */
Tejun Heo936fd732007-08-06 18:36:23 +09001673 rc = sata_scr_read(link, SCR_ERROR, &serror);
Tejun Heo022bdb02006-05-15 20:58:22 +09001674 if (rc == 0) {
1675 ehc->i.serror |= serror;
Tejun Heo02607312007-08-06 18:36:23 +09001676 ata_eh_analyze_serror(link);
Tejun Heo4e57c512007-07-16 14:29:41 +09001677 } else if (rc != -EOPNOTSUPP) {
1678 /* SError read failed, force hardreset and probing */
1679 ata_ehi_schedule_probe(&ehc->i);
Tejun Heo4528e4d2006-07-08 20:17:26 +09001680 ehc->i.action |= ATA_EH_HARDRESET;
Tejun Heo4e57c512007-07-16 14:29:41 +09001681 ehc->i.err_mask |= AC_ERR_OTHER;
1682 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001683
Tejun Heoe8ee8452006-05-15 21:03:46 +09001684 /* analyze NCQ failure */
Tejun Heo02607312007-08-06 18:36:23 +09001685 ata_eh_analyze_ncq_error(link);
Tejun Heoe8ee8452006-05-15 21:03:46 +09001686
Tejun Heo022bdb02006-05-15 20:58:22 +09001687 /* 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
Tejun Heo02607312007-08-06 18:36:23 +09001696 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001697 continue;
1698
1699 /* inherit upper level err_mask */
1700 qc->err_mask |= ehc->i.err_mask;
1701
Tejun Heo022bdb02006-05-15 20:58:22 +09001702 /* analyze TF */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001703 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
Tejun Heo022bdb02006-05-15 20:58:22 +09001704
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);
Tejun Heo4528e4d2006-07-08 20:17:26 +09001717 ehc->i.action &= ~ATA_EH_REVALIDATE;
Tejun Heo022bdb02006-05-15 20:58:22 +09001718 }
1719
1720 /* accumulate error info */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001721 ehc->i.dev = qc->dev;
Tejun Heo022bdb02006-05-15 20:58:22 +09001722 all_err_mask |= qc->err_mask;
1723 if (qc->flags & ATA_QCFLAG_IO)
1724 is_io = 1;
1725 }
1726
Tejun Heoa20f33f2006-05-16 12:58:24 +09001727 /* enforce default EH actions */
Tejun Heob51e9e52006-06-29 01:29:30 +09001728 if (ap->pflags & ATA_PFLAG_FROZEN ||
Tejun Heoa20f33f2006-05-16 12:58:24 +09001729 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
Tejun Heo4528e4d2006-07-08 20:17:26 +09001730 ehc->i.action |= ATA_EH_SOFTRESET;
Tejun Heoa20f33f2006-05-16 12:58:24 +09001731 else if (all_err_mask)
Tejun Heo4528e4d2006-07-08 20:17:26 +09001732 ehc->i.action |= ATA_EH_REVALIDATE;
Tejun Heo022bdb02006-05-15 20:58:22 +09001733
Tejun Heo47005f22006-06-19 18:27:23 +09001734 /* if we have offending qcs and the associated failed device */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001735 if (ehc->i.dev) {
Tejun Heo47005f22006-06-19 18:27:23 +09001736 /* speed down */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001737 ehc->i.action |= ata_eh_speed_down(ehc->i.dev, is_io,
1738 all_err_mask);
Tejun Heo47005f22006-06-19 18:27:23 +09001739
1740 /* perform per-dev EH action only on the offending device */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001741 ehc->i.dev_action[ehc->i.dev->devno] |=
1742 ehc->i.action & ATA_EH_PERDEV_MASK;
1743 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
Tejun Heo47005f22006-06-19 18:27:23 +09001744 }
1745
Tejun Heo022bdb02006-05-15 20:58:22 +09001746 DPRINTK("EXIT\n");
1747}
1748
1749/**
Tejun Heo9b1e2652007-08-06 18:36:24 +09001750 * 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 */
1759static void ata_eh_autopsy(struct ata_port *ap)
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
Tejun Heo02607312007-08-06 18:36:23 +09001769 * @link: ATA link EH is going on
Tejun Heo022bdb02006-05-15 20:58:22 +09001770 *
1771 * Report EH to user.
1772 *
1773 * LOCKING:
1774 * None.
1775 */
Tejun Heo9b1e2652007-08-06 18:36:24 +09001776static void ata_eh_link_report(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001777{
Tejun Heo02607312007-08-06 18:36:23 +09001778 struct ata_port *ap = link->ap;
1779 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001780 const char *frozen, *desc;
1781 int tag, nr_failed = 0;
1782
1783 desc = NULL;
1784 if (ehc->i.desc[0] != '\0')
1785 desc = ehc->i.desc;
1786
1787 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1788 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1789
Tejun Heo02607312007-08-06 18:36:23 +09001790 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001791 continue;
1792 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1793 continue;
1794
1795 nr_failed++;
1796 }
1797
1798 if (!nr_failed && !ehc->i.err_mask)
1799 return;
1800
1801 frozen = "";
Tejun Heob51e9e52006-06-29 01:29:30 +09001802 if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heo022bdb02006-05-15 20:58:22 +09001803 frozen = " frozen";
1804
1805 if (ehc->i.dev) {
Tejun Heoe8ee8452006-05-15 21:03:46 +09001806 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1807 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
Tejun Heo02607312007-08-06 18:36:23 +09001808 ehc->i.err_mask, link->sactive,
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001809 ehc->i.serror, ehc->i.action, frozen);
Tejun Heo022bdb02006-05-15 20:58:22 +09001810 if (desc)
Tejun Heob64bbc32007-07-16 14:29:39 +09001811 ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
Tejun Heo022bdb02006-05-15 20:58:22 +09001812 } else {
Tejun Heo02607312007-08-06 18:36:23 +09001813 ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
Tejun Heoe8ee8452006-05-15 21:03:46 +09001814 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
Tejun Heo02607312007-08-06 18:36:23 +09001815 ehc->i.err_mask, link->sactive,
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001816 ehc->i.serror, ehc->i.action, frozen);
Tejun Heo022bdb02006-05-15 20:58:22 +09001817 if (desc)
Tejun Heo02607312007-08-06 18:36:23 +09001818 ata_link_printk(link, KERN_ERR, "%s\n", desc);
Tejun Heo022bdb02006-05-15 20:58:22 +09001819 }
1820
1821 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
Tejun Heo8a937582006-11-14 22:36:12 +09001822 static const char *dma_str[] = {
1823 [DMA_BIDIRECTIONAL] = "bidi",
1824 [DMA_TO_DEVICE] = "out",
1825 [DMA_FROM_DEVICE] = "in",
1826 [DMA_NONE] = "",
1827 };
Tejun Heo022bdb02006-05-15 20:58:22 +09001828 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
Tejun Heo8a937582006-11-14 22:36:12 +09001829 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
Tejun Heo022bdb02006-05-15 20:58:22 +09001830
Tejun Heo02607312007-08-06 18:36:23 +09001831 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
1832 qc->dev->link != link || !qc->err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001833 continue;
1834
Tejun Heo8a937582006-11-14 22:36:12 +09001835 ata_dev_printk(qc->dev, KERN_ERR,
1836 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
Tejun Heo664e8502006-11-20 16:05:34 +09001837 "tag %d cdb 0x%x data %u %s\n "
Tejun Heo8a937582006-11-14 22:36:12 +09001838 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
Tejun Heo5335b722007-07-16 14:29:40 +09001839 "Emask 0x%x (%s)%s\n",
Tejun Heo8a937582006-11-14 22:36:12 +09001840 cmd->command, cmd->feature, cmd->nsect,
1841 cmd->lbal, cmd->lbam, cmd->lbah,
1842 cmd->hob_feature, cmd->hob_nsect,
1843 cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
Tejun Heo726f0782007-01-03 17:30:39 +09001844 cmd->device, qc->tag, qc->cdb[0], qc->nbytes,
Tejun Heo664e8502006-11-20 16:05:34 +09001845 dma_str[qc->dma_dir],
Tejun Heo8a937582006-11-14 22:36:12 +09001846 res->command, res->feature, res->nsect,
1847 res->lbal, res->lbam, res->lbah,
1848 res->hob_feature, res->hob_nsect,
1849 res->hob_lbal, res->hob_lbam, res->hob_lbah,
Tejun Heo5335b722007-07-16 14:29:40 +09001850 res->device, qc->err_mask, ata_err_string(qc->err_mask),
1851 qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
Tejun Heo022bdb02006-05-15 20:58:22 +09001852 }
1853}
1854
Tejun Heo9b1e2652007-08-06 18:36:24 +09001855/**
1856 * ata_eh_report - report error handling to user
1857 * @ap: ATA port to report EH about
1858 *
1859 * Report EH to user.
1860 *
1861 * LOCKING:
1862 * None.
1863 */
1864static void ata_eh_report(struct ata_port *ap)
1865{
1866 struct ata_link *link;
1867
1868 __ata_port_for_each_link(link, ap)
1869 ata_eh_link_report(link);
1870}
1871
Tejun Heocc0680a2007-08-06 18:36:23 +09001872static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
Tejun Heod4b2bab2007-02-02 16:50:52 +09001873 unsigned int *classes, unsigned long deadline)
Tejun Heod87fa382006-05-31 18:28:24 +09001874{
Tejun Heof58229f2007-08-06 18:36:23 +09001875 struct ata_device *dev;
1876 int rc;
Tejun Heod87fa382006-05-31 18:28:24 +09001877
Tejun Heocc0680a2007-08-06 18:36:23 +09001878 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09001879 classes[dev->devno] = ATA_DEV_UNKNOWN;
Tejun Heod87fa382006-05-31 18:28:24 +09001880
Tejun Heocc0680a2007-08-06 18:36:23 +09001881 rc = reset(link, classes, deadline);
Tejun Heod87fa382006-05-31 18:28:24 +09001882 if (rc)
1883 return rc;
1884
1885 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
1886 * is complete and convert all ATA_DEV_UNKNOWN to
1887 * ATA_DEV_NONE.
1888 */
Tejun Heocc0680a2007-08-06 18:36:23 +09001889 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09001890 if (classes[dev->devno] != ATA_DEV_UNKNOWN)
Tejun Heod87fa382006-05-31 18:28:24 +09001891 break;
1892
Tejun Heof58229f2007-08-06 18:36:23 +09001893 if (dev) {
Tejun Heocc0680a2007-08-06 18:36:23 +09001894 ata_link_for_each_dev(dev, link) {
Tejun Heof58229f2007-08-06 18:36:23 +09001895 if (classes[dev->devno] == ATA_DEV_UNKNOWN)
1896 classes[dev->devno] = ATA_DEV_NONE;
1897 }
1898 }
Tejun Heod87fa382006-05-31 18:28:24 +09001899
1900 return 0;
1901}
1902
Tejun Heo664faf02006-05-31 18:27:50 +09001903static int ata_eh_followup_srst_needed(int rc, int classify,
1904 const unsigned int *classes)
1905{
1906 if (rc == -EAGAIN)
1907 return 1;
1908 if (rc != 0)
1909 return 0;
1910 if (classify && classes[0] == ATA_DEV_UNKNOWN)
1911 return 1;
1912 return 0;
1913}
1914
Tejun Heocc0680a2007-08-06 18:36:23 +09001915static int ata_eh_reset(struct ata_link *link, int classify,
Tejun Heof5914a42006-05-31 18:27:48 +09001916 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
Tejun Heo022bdb02006-05-15 20:58:22 +09001917 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
1918{
Tejun Heo936fd732007-08-06 18:36:23 +09001919 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo664faf02006-05-31 18:27:50 +09001920 unsigned int *classes = ehc->classes;
Tejun Heo1cdaf532006-07-03 16:07:26 +09001921 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
Tejun Heo31daabd2007-02-02 16:50:52 +09001922 int try = 0;
Tejun Heof58229f2007-08-06 18:36:23 +09001923 struct ata_device *dev;
Tejun Heo31daabd2007-02-02 16:50:52 +09001924 unsigned long deadline;
Tejun Heof5914a42006-05-31 18:27:48 +09001925 unsigned int action;
Tejun Heo022bdb02006-05-15 20:58:22 +09001926 ata_reset_fn_t reset;
Tejun Heof58229f2007-08-06 18:36:23 +09001927 int rc;
Tejun Heo022bdb02006-05-15 20:58:22 +09001928
Tejun Heo13abf502006-07-10 23:18:46 +09001929 /* about to reset */
Tejun Heo955e57d2007-08-06 18:36:23 +09001930 ata_eh_about_to_do(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
Tejun Heo13abf502006-07-10 23:18:46 +09001931
Tejun Heof5914a42006-05-31 18:27:48 +09001932 /* Determine which reset to use and record in ehc->i.action.
1933 * prereset() may examine and modify it.
1934 */
1935 action = ehc->i.action;
1936 ehc->i.action &= ~ATA_EH_RESET_MASK;
Tejun Heo936fd732007-08-06 18:36:23 +09001937 if (softreset && (!hardreset || (!sata_set_spd_needed(link) &&
Tejun Heof5914a42006-05-31 18:27:48 +09001938 !(action & ATA_EH_HARDRESET))))
1939 ehc->i.action |= ATA_EH_SOFTRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001940 else
Tejun Heof5914a42006-05-31 18:27:48 +09001941 ehc->i.action |= ATA_EH_HARDRESET;
1942
1943 if (prereset) {
Tejun Heocc0680a2007-08-06 18:36:23 +09001944 rc = prereset(link, jiffies + ATA_EH_PRERESET_TIMEOUT);
Tejun Heof5914a42006-05-31 18:27:48 +09001945 if (rc) {
Alan Coxc9619222006-09-26 17:53:38 +01001946 if (rc == -ENOENT) {
Tejun Heocc0680a2007-08-06 18:36:23 +09001947 ata_link_printk(link, KERN_DEBUG,
Tejun Heo4aa9ab62007-03-12 17:24:08 +09001948 "port disabled. ignoring.\n");
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001949 ehc->i.action &= ~ATA_EH_RESET_MASK;
Tejun Heo4aa9ab62007-03-12 17:24:08 +09001950
Tejun Heo936fd732007-08-06 18:36:23 +09001951 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09001952 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heo4aa9ab62007-03-12 17:24:08 +09001953
1954 rc = 0;
Alan Coxc9619222006-09-26 17:53:38 +01001955 } else
Tejun Heocc0680a2007-08-06 18:36:23 +09001956 ata_link_printk(link, KERN_ERR,
Tejun Heof5914a42006-05-31 18:27:48 +09001957 "prereset failed (errno=%d)\n", rc);
Tejun Heofccb6ea2007-07-16 14:29:41 +09001958 goto out;
Tejun Heof5914a42006-05-31 18:27:48 +09001959 }
1960 }
1961
1962 /* prereset() might have modified ehc->i.action */
1963 if (ehc->i.action & ATA_EH_HARDRESET)
Tejun Heo022bdb02006-05-15 20:58:22 +09001964 reset = hardreset;
Tejun Heof5914a42006-05-31 18:27:48 +09001965 else if (ehc->i.action & ATA_EH_SOFTRESET)
1966 reset = softreset;
1967 else {
1968 /* prereset told us not to reset, bang classes and return */
Tejun Heo936fd732007-08-06 18:36:23 +09001969 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09001970 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heofccb6ea2007-07-16 14:29:41 +09001971 rc = 0;
1972 goto out;
Tejun Heof5914a42006-05-31 18:27:48 +09001973 }
1974
1975 /* did prereset() screw up? if so, fix up to avoid oopsing */
1976 if (!reset) {
Tejun Heof5914a42006-05-31 18:27:48 +09001977 if (softreset)
1978 reset = softreset;
1979 else
1980 reset = hardreset;
1981 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001982
1983 retry:
Tejun Heo31daabd2007-02-02 16:50:52 +09001984 deadline = jiffies + ata_eh_reset_timeouts[try++];
1985
Tejun Heo3e706392006-05-31 18:28:11 +09001986 /* shut up during boot probing */
1987 if (verbose)
Tejun Heocc0680a2007-08-06 18:36:23 +09001988 ata_link_printk(link, KERN_INFO, "%s resetting link\n",
Tejun Heo3e706392006-05-31 18:28:11 +09001989 reset == softreset ? "soft" : "hard");
Tejun Heo022bdb02006-05-15 20:58:22 +09001990
Tejun Heo13abf502006-07-10 23:18:46 +09001991 /* mark that this EH session started with reset */
Tejun Heo0d64a232007-04-23 02:41:05 +09001992 if (reset == hardreset)
1993 ehc->i.flags |= ATA_EHI_DID_HARDRESET;
1994 else
1995 ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001996
Tejun Heocc0680a2007-08-06 18:36:23 +09001997 rc = ata_do_reset(link, reset, classes, deadline);
Tejun Heo022bdb02006-05-15 20:58:22 +09001998
Tejun Heo664faf02006-05-31 18:27:50 +09001999 if (reset == hardreset &&
2000 ata_eh_followup_srst_needed(rc, classify, classes)) {
2001 /* okay, let's do follow-up softreset */
Tejun Heo664faf02006-05-31 18:27:50 +09002002 reset = softreset;
2003
2004 if (!reset) {
Tejun Heocc0680a2007-08-06 18:36:23 +09002005 ata_link_printk(link, KERN_ERR,
Tejun Heo664faf02006-05-31 18:27:50 +09002006 "follow-up softreset required "
2007 "but no softreset avaliable\n");
Tejun Heofccb6ea2007-07-16 14:29:41 +09002008 rc = -EINVAL;
2009 goto out;
Tejun Heo664faf02006-05-31 18:27:50 +09002010 }
2011
Tejun Heo955e57d2007-08-06 18:36:23 +09002012 ata_eh_about_to_do(link, NULL, ATA_EH_RESET_MASK);
Tejun Heocc0680a2007-08-06 18:36:23 +09002013 rc = ata_do_reset(link, reset, classes, deadline);
Tejun Heo664faf02006-05-31 18:27:50 +09002014
2015 if (rc == 0 && classify &&
2016 classes[0] == ATA_DEV_UNKNOWN) {
Tejun Heocc0680a2007-08-06 18:36:23 +09002017 ata_link_printk(link, KERN_ERR,
Tejun Heo664faf02006-05-31 18:27:50 +09002018 "classification failed\n");
Tejun Heofccb6ea2007-07-16 14:29:41 +09002019 rc = -EINVAL;
2020 goto out;
Tejun Heo664faf02006-05-31 18:27:50 +09002021 }
2022 }
2023
Tejun Heo31daabd2007-02-02 16:50:52 +09002024 if (rc && try < ARRAY_SIZE(ata_eh_reset_timeouts)) {
2025 unsigned long now = jiffies;
Tejun Heo664faf02006-05-31 18:27:50 +09002026
Tejun Heo31daabd2007-02-02 16:50:52 +09002027 if (time_before(now, deadline)) {
2028 unsigned long delta = deadline - jiffies;
Tejun Heo664faf02006-05-31 18:27:50 +09002029
Tejun Heocc0680a2007-08-06 18:36:23 +09002030 ata_link_printk(link, KERN_WARNING, "reset failed "
Tejun Heo31daabd2007-02-02 16:50:52 +09002031 "(errno=%d), retrying in %u secs\n",
2032 rc, (jiffies_to_msecs(delta) + 999) / 1000);
Tejun Heo022bdb02006-05-15 20:58:22 +09002033
Tejun Heo31daabd2007-02-02 16:50:52 +09002034 schedule_timeout_uninterruptible(delta);
2035 }
2036
Tejun Heof1545152007-07-16 14:29:40 +09002037 if (rc == -EPIPE ||
Tejun Heo31daabd2007-02-02 16:50:52 +09002038 try == ARRAY_SIZE(ata_eh_reset_timeouts) - 1)
Tejun Heo936fd732007-08-06 18:36:23 +09002039 sata_down_spd_limit(link);
Tejun Heo022bdb02006-05-15 20:58:22 +09002040 if (hardreset)
2041 reset = hardreset;
2042 goto retry;
2043 }
2044
2045 if (rc == 0) {
Tejun Heo008a7892007-07-16 14:29:40 +09002046 u32 sstatus;
2047
Tejun Heo20952b62006-05-31 18:27:23 +09002048 /* After the reset, the device state is PIO 0 and the
2049 * controller state is undefined. Record the mode.
2050 */
Tejun Heo936fd732007-08-06 18:36:23 +09002051 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09002052 dev->pio_mode = XFER_PIO_0;
Tejun Heo20952b62006-05-31 18:27:23 +09002053
Tejun Heo008a7892007-07-16 14:29:40 +09002054 /* record current link speed */
Tejun Heo936fd732007-08-06 18:36:23 +09002055 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2056 link->sata_spd = (sstatus >> 4) & 0xf;
Tejun Heo008a7892007-07-16 14:29:40 +09002057
Tejun Heo022bdb02006-05-15 20:58:22 +09002058 if (postreset)
Tejun Heocc0680a2007-08-06 18:36:23 +09002059 postreset(link, classes);
Tejun Heo022bdb02006-05-15 20:58:22 +09002060
2061 /* reset successful, schedule revalidation */
Tejun Heo955e57d2007-08-06 18:36:23 +09002062 ata_eh_done(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
Tejun Heo022bdb02006-05-15 20:58:22 +09002063 ehc->i.action |= ATA_EH_REVALIDATE;
2064 }
Tejun Heofccb6ea2007-07-16 14:29:41 +09002065 out:
2066 /* clear hotplug flag */
2067 ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
Tejun Heo022bdb02006-05-15 20:58:22 +09002068 return rc;
2069}
2070
Tejun Heo02607312007-08-06 18:36:23 +09002071static int ata_eh_revalidate_and_attach(struct ata_link *link,
Tejun Heo084fe632006-05-31 18:28:03 +09002072 struct ata_device **r_failed_dev)
Tejun Heo022bdb02006-05-15 20:58:22 +09002073{
Tejun Heo02607312007-08-06 18:36:23 +09002074 struct ata_port *ap = link->ap;
2075 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09002076 struct ata_device *dev;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002077 unsigned int new_mask = 0;
Tejun Heo084fe632006-05-31 18:28:03 +09002078 unsigned long flags;
Tejun Heof58229f2007-08-06 18:36:23 +09002079 int rc = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09002080
2081 DPRINTK("ENTER\n");
2082
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002083 /* For PATA drive side cable detection to work, IDENTIFY must
2084 * be done backwards such that PDIAG- is released by the slave
2085 * device before the master device is identified.
2086 */
Tejun Heo02607312007-08-06 18:36:23 +09002087 ata_link_for_each_dev_reverse(dev, link) {
Tejun Heof58229f2007-08-06 18:36:23 +09002088 unsigned int action = ata_eh_dev_action(dev);
2089 unsigned int readid_flags = 0;
Tejun Heo47005f22006-06-19 18:27:23 +09002090
Tejun Heobff04642006-11-10 18:08:10 +09002091 if (ehc->i.flags & ATA_EHI_DID_RESET)
2092 readid_flags |= ATA_READID_POSTRESET;
2093
Tejun Heo9666f402007-05-04 21:27:47 +02002094 if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
Tejun Heo02607312007-08-06 18:36:23 +09002095 if (ata_link_offline(link)) {
Tejun Heo022bdb02006-05-15 20:58:22 +09002096 rc = -EIO;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002097 goto err;
Tejun Heo022bdb02006-05-15 20:58:22 +09002098 }
2099
Tejun Heo02607312007-08-06 18:36:23 +09002100 ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
Tejun Heobff04642006-11-10 18:08:10 +09002101 rc = ata_dev_revalidate(dev, readid_flags);
Tejun Heo022bdb02006-05-15 20:58:22 +09002102 if (rc)
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002103 goto err;
Tejun Heo022bdb02006-05-15 20:58:22 +09002104
Tejun Heo02607312007-08-06 18:36:23 +09002105 ata_eh_done(link, dev, ATA_EH_REVALIDATE);
Tejun Heo47005f22006-06-19 18:27:23 +09002106
Tejun Heobaa1e782006-11-01 18:39:27 +09002107 /* Configuration may have changed, reconfigure
2108 * transfer mode.
2109 */
2110 ehc->i.flags |= ATA_EHI_SETMODE;
2111
zhao, forrest3057ac32006-06-12 12:01:34 +08002112 /* schedule the scsi_rescan_device() here */
2113 queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
Tejun Heo084fe632006-05-31 18:28:03 +09002114 } else if (dev->class == ATA_DEV_UNKNOWN &&
2115 ehc->tries[dev->devno] &&
2116 ata_class_enabled(ehc->classes[dev->devno])) {
2117 dev->class = ehc->classes[dev->devno];
2118
Tejun Heobff04642006-11-10 18:08:10 +09002119 rc = ata_dev_read_id(dev, &dev->class, readid_flags,
2120 dev->id);
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002121 switch (rc) {
2122 case 0:
Tejun Heof58229f2007-08-06 18:36:23 +09002123 new_mask |= 1 << dev->devno;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002124 break;
2125 case -ENOENT:
Tejun Heo55a8e2c2006-11-10 18:08:10 +09002126 /* IDENTIFY was issued to non-existent
2127 * device. No need to reset. Just
2128 * thaw and kill the device.
2129 */
2130 ata_eh_thaw_port(ap);
2131 dev->class = ATA_DEV_UNKNOWN;
Tejun Heo084fe632006-05-31 18:28:03 +09002132 break;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002133 default:
2134 dev->class = ATA_DEV_UNKNOWN;
2135 goto err;
Tejun Heo55a8e2c2006-11-10 18:08:10 +09002136 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002137 }
2138 }
2139
Tejun Heoc1c4e8d2007-04-23 02:05:53 +09002140 /* PDIAG- should have been released, ask cable type if post-reset */
Tejun Heo9b1e2652007-08-06 18:36:24 +09002141 if (ata_is_host_link(link) && ap->ops->cable_detect &&
2142 (ehc->i.flags & ATA_EHI_DID_RESET))
Tejun Heoc1c4e8d2007-04-23 02:05:53 +09002143 ap->cbl = ap->ops->cable_detect(ap);
2144
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002145 /* Configure new devices forward such that user doesn't see
2146 * device detection messages backwards.
2147 */
Tejun Heo02607312007-08-06 18:36:23 +09002148 ata_link_for_each_dev(dev, link) {
Tejun Heof58229f2007-08-06 18:36:23 +09002149 if (!(new_mask & (1 << dev->devno)))
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002150 continue;
2151
2152 ehc->i.flags |= ATA_EHI_PRINTINFO;
2153 rc = ata_dev_configure(dev);
2154 ehc->i.flags &= ~ATA_EHI_PRINTINFO;
2155 if (rc)
2156 goto err;
2157
2158 spin_lock_irqsave(ap->lock, flags);
2159 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
2160 spin_unlock_irqrestore(ap->lock, flags);
2161
2162 /* new device discovered, configure xfermode */
2163 ehc->i.flags |= ATA_EHI_SETMODE;
2164 }
2165
2166 return 0;
2167
2168 err:
2169 *r_failed_dev = dev;
2170 DPRINTK("EXIT rc=%d\n", rc);
Tejun Heo022bdb02006-05-15 20:58:22 +09002171 return rc;
2172}
2173
Tejun Heo02607312007-08-06 18:36:23 +09002174static int ata_link_nr_enabled(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09002175{
Tejun Heof58229f2007-08-06 18:36:23 +09002176 struct ata_device *dev;
2177 int cnt = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09002178
Tejun Heo02607312007-08-06 18:36:23 +09002179 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09002180 if (ata_dev_enabled(dev))
Tejun Heo022bdb02006-05-15 20:58:22 +09002181 cnt++;
2182 return cnt;
2183}
2184
Tejun Heo02607312007-08-06 18:36:23 +09002185static int ata_link_nr_vacant(struct ata_link *link)
Tejun Heo084fe632006-05-31 18:28:03 +09002186{
Tejun Heof58229f2007-08-06 18:36:23 +09002187 struct ata_device *dev;
2188 int cnt = 0;
Tejun Heo084fe632006-05-31 18:28:03 +09002189
Tejun Heo02607312007-08-06 18:36:23 +09002190 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09002191 if (dev->class == ATA_DEV_UNKNOWN)
Tejun Heo084fe632006-05-31 18:28:03 +09002192 cnt++;
2193 return cnt;
2194}
2195
Tejun Heo02607312007-08-06 18:36:23 +09002196static int ata_eh_skip_recovery(struct ata_link *link)
Tejun Heo084fe632006-05-31 18:28:03 +09002197{
Tejun Heo02607312007-08-06 18:36:23 +09002198 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heof58229f2007-08-06 18:36:23 +09002199 struct ata_device *dev;
Tejun Heo084fe632006-05-31 18:28:03 +09002200
Tejun Heo7c8c2cf2006-07-10 23:18:23 +09002201 /* thaw frozen port, resume link and recover failed devices */
Tejun Heo02607312007-08-06 18:36:23 +09002202 if ((link->ap->pflags & ATA_PFLAG_FROZEN) ||
2203 (ehc->i.flags & ATA_EHI_RESUME_LINK) || ata_link_nr_enabled(link))
Tejun Heo084fe632006-05-31 18:28:03 +09002204 return 0;
2205
2206 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
Tejun Heo02607312007-08-06 18:36:23 +09002207 ata_link_for_each_dev(dev, link) {
Tejun Heo084fe632006-05-31 18:28:03 +09002208 if (dev->class == ATA_DEV_UNKNOWN &&
2209 ehc->classes[dev->devno] != ATA_DEV_NONE)
2210 return 0;
2211 }
2212
2213 return 1;
2214}
2215
Tejun Heo9b1e2652007-08-06 18:36:24 +09002216static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
Tejun Heofee7ca72007-07-01 19:05:58 +09002217{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002218 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heofee7ca72007-07-01 19:05:58 +09002219
2220 ehc->tries[dev->devno]--;
2221
2222 switch (err) {
2223 case -ENODEV:
2224 /* device missing or wrong IDENTIFY data, schedule probing */
2225 ehc->i.probe_mask |= (1 << dev->devno);
2226 case -EINVAL:
2227 /* give it just one more chance */
2228 ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
2229 case -EIO:
2230 if (ehc->tries[dev->devno] == 1) {
2231 /* This is the last chance, better to slow
2232 * down than lose it.
2233 */
Tejun Heo936fd732007-08-06 18:36:23 +09002234 sata_down_spd_limit(dev->link);
Tejun Heofee7ca72007-07-01 19:05:58 +09002235 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2236 }
2237 }
2238
2239 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2240 /* disable device if it has used up all its chances */
2241 ata_dev_disable(dev);
2242
2243 /* detach if offline */
Tejun Heo936fd732007-08-06 18:36:23 +09002244 if (ata_link_offline(dev->link))
Tejun Heofee7ca72007-07-01 19:05:58 +09002245 ata_eh_detach_dev(dev);
2246
2247 /* probe if requested */
2248 if ((ehc->i.probe_mask & (1 << dev->devno)) &&
2249 !(ehc->did_probe_mask & (1 << dev->devno))) {
2250 ata_eh_detach_dev(dev);
2251 ata_dev_init(dev);
2252
2253 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2254 ehc->did_probe_mask |= (1 << dev->devno);
2255 ehc->i.action |= ATA_EH_SOFTRESET;
2256 }
Tejun Heo9b1e2652007-08-06 18:36:24 +09002257
2258 return 1;
Tejun Heofee7ca72007-07-01 19:05:58 +09002259 } else {
2260 /* soft didn't work? be haaaaard */
2261 if (ehc->i.flags & ATA_EHI_DID_RESET)
2262 ehc->i.action |= ATA_EH_HARDRESET;
2263 else
2264 ehc->i.action |= ATA_EH_SOFTRESET;
Tejun Heo9b1e2652007-08-06 18:36:24 +09002265
2266 return 0;
Tejun Heofee7ca72007-07-01 19:05:58 +09002267 }
2268}
2269
Tejun Heo022bdb02006-05-15 20:58:22 +09002270/**
2271 * ata_eh_recover - recover host port after error
2272 * @ap: host port to recover
Tejun Heof5914a42006-05-31 18:27:48 +09002273 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09002274 * @softreset: softreset method (can be NULL)
2275 * @hardreset: hardreset method (can be NULL)
2276 * @postreset: postreset method (can be NULL)
Tejun Heo9b1e2652007-08-06 18:36:24 +09002277 * @r_failed_link: out parameter for failed link
Tejun Heo022bdb02006-05-15 20:58:22 +09002278 *
2279 * This is the alpha and omega, eum and yang, heart and soul of
2280 * libata exception handling. On entry, actions required to
Tejun Heo9b1e2652007-08-06 18:36:24 +09002281 * recover each link and hotplug requests are recorded in the
2282 * link's eh_context. This function executes all the operations
2283 * with appropriate retrials and fallbacks to resurrect failed
Tejun Heo084fe632006-05-31 18:28:03 +09002284 * devices, detach goners and greet newcomers.
Tejun Heo022bdb02006-05-15 20:58:22 +09002285 *
2286 * LOCKING:
2287 * Kernel thread context (may sleep).
2288 *
2289 * RETURNS:
2290 * 0 on success, -errno on failure.
2291 */
Tejun Heof5914a42006-05-31 18:27:48 +09002292static int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
2293 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
Tejun Heo9b1e2652007-08-06 18:36:24 +09002294 ata_postreset_fn_t postreset,
2295 struct ata_link **r_failed_link)
Tejun Heo022bdb02006-05-15 20:58:22 +09002296{
Tejun Heo9b1e2652007-08-06 18:36:24 +09002297 struct ata_link *link;
Tejun Heo022bdb02006-05-15 20:58:22 +09002298 struct ata_device *dev;
Tejun Heo9b1e2652007-08-06 18:36:24 +09002299 int nr_failed_devs, nr_disabled_devs;
2300 int reset, rc;
Tejun Heo022bdb02006-05-15 20:58:22 +09002301
2302 DPRINTK("ENTER\n");
2303
2304 /* prep for recovery */
Tejun Heo9b1e2652007-08-06 18:36:24 +09002305 ata_port_for_each_link(link, ap) {
2306 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo084fe632006-05-31 18:28:03 +09002307
Tejun Heo9b1e2652007-08-06 18:36:24 +09002308 ata_link_for_each_dev(dev, link) {
2309 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
Tejun Heo79a55b72007-01-18 17:22:18 +09002310
Tejun Heo9b1e2652007-08-06 18:36:24 +09002311 /* collect port action mask recorded in dev actions */
2312 ehc->i.action |= ehc->i.dev_action[dev->devno] &
2313 ~ATA_EH_PERDEV_MASK;
2314 ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
Tejun Heo084fe632006-05-31 18:28:03 +09002315
Tejun Heo9b1e2652007-08-06 18:36:24 +09002316 /* process hotplug request */
2317 if (dev->flags & ATA_DFLAG_DETACH)
2318 ata_eh_detach_dev(dev);
2319
2320 if (!ata_dev_enabled(dev) &&
2321 ((ehc->i.probe_mask & (1 << dev->devno)) &&
2322 !(ehc->did_probe_mask & (1 << dev->devno)))) {
2323 ata_eh_detach_dev(dev);
2324 ata_dev_init(dev);
2325 ehc->did_probe_mask |= (1 << dev->devno);
2326 ehc->i.action |= ATA_EH_SOFTRESET;
2327 }
Tejun Heo084fe632006-05-31 18:28:03 +09002328 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002329 }
2330
2331 retry:
Tejun Heo022bdb02006-05-15 20:58:22 +09002332 rc = 0;
Tejun Heo9b1e2652007-08-06 18:36:24 +09002333 nr_failed_devs = 0;
2334 nr_disabled_devs = 0;
2335 reset = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09002336
Tejun Heoaeb2ecd2006-06-12 14:11:43 +09002337 /* if UNLOADING, finish immediately */
Tejun Heob51e9e52006-06-29 01:29:30 +09002338 if (ap->pflags & ATA_PFLAG_UNLOADING)
Tejun Heoaeb2ecd2006-06-12 14:11:43 +09002339 goto out;
2340
Tejun Heo9b1e2652007-08-06 18:36:24 +09002341 /* prep for EH */
2342 ata_port_for_each_link(link, ap) {
2343 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09002344
Tejun Heo9b1e2652007-08-06 18:36:24 +09002345 /* skip EH if possible. */
2346 if (ata_eh_skip_recovery(link))
2347 ehc->i.action = 0;
2348
2349 /* do we need to reset? */
2350 if (ehc->i.action & ATA_EH_RESET_MASK)
2351 reset = 1;
2352
2353 ata_link_for_each_dev(dev, link)
2354 ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
2355 }
Tejun Heo084fe632006-05-31 18:28:03 +09002356
Tejun Heo022bdb02006-05-15 20:58:22 +09002357 /* reset */
Tejun Heo9b1e2652007-08-06 18:36:24 +09002358 if (reset) {
Tejun Heo022bdb02006-05-15 20:58:22 +09002359 ata_eh_freeze_port(ap);
2360
Tejun Heo9b1e2652007-08-06 18:36:24 +09002361 ata_port_for_each_link(link, ap) {
2362 struct ata_eh_context *ehc = &link->eh_context;
2363
2364 if (!(ehc->i.action & ATA_EH_RESET_MASK))
2365 continue;
2366
2367 rc = ata_eh_reset(link, ata_link_nr_vacant(link),
2368 prereset, softreset, hardreset,
2369 postreset);
2370 if (rc) {
2371 ata_link_printk(link, KERN_ERR,
2372 "reset failed, giving up\n");
2373 goto out;
2374 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002375 }
2376
2377 ata_eh_thaw_port(ap);
2378 }
2379
Tejun Heo9b1e2652007-08-06 18:36:24 +09002380 /* the rest */
2381 ata_port_for_each_link(link, ap) {
2382 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09002383
Tejun Heo9b1e2652007-08-06 18:36:24 +09002384 /* revalidate existing devices and attach new ones */
2385 rc = ata_eh_revalidate_and_attach(link, &dev);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002386 if (rc)
Tejun Heo022bdb02006-05-15 20:58:22 +09002387 goto dev_fail;
Tejun Heo9b1e2652007-08-06 18:36:24 +09002388
2389 /* configure transfer mode if necessary */
2390 if (ehc->i.flags & ATA_EHI_SETMODE) {
2391 rc = ata_set_mode(link, &dev);
2392 if (rc)
2393 goto dev_fail;
2394 ehc->i.flags &= ~ATA_EHI_SETMODE;
2395 }
2396
2397 /* this link is okay now */
2398 ehc->i.flags = 0;
2399 continue;
2400
2401 dev_fail:
2402 nr_failed_devs++;
2403 if (ata_eh_handle_dev_fail(dev, rc))
2404 nr_disabled_devs++;
2405
2406 if (ap->pflags & ATA_PFLAG_FROZEN)
2407 break;
Tejun Heo022bdb02006-05-15 20:58:22 +09002408 }
2409
Tejun Heo9b1e2652007-08-06 18:36:24 +09002410 if (nr_failed_devs) {
2411 if (nr_failed_devs != nr_disabled_devs) {
2412 ata_port_printk(ap, KERN_WARNING, "failed to recover "
2413 "some devices, retrying in 5 secs\n");
2414 ssleep(5);
2415 } else {
2416 /* no device left to recover, repeat fast */
2417 msleep(500);
2418 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002419
Tejun Heo9b1e2652007-08-06 18:36:24 +09002420 goto retry;
Tejun Heo022bdb02006-05-15 20:58:22 +09002421 }
2422
Tejun Heo022bdb02006-05-15 20:58:22 +09002423 out:
Tejun Heo9b1e2652007-08-06 18:36:24 +09002424 if (rc && r_failed_link)
2425 *r_failed_link = link;
Tejun Heo022bdb02006-05-15 20:58:22 +09002426
2427 DPRINTK("EXIT, rc=%d\n", rc);
2428 return rc;
2429}
2430
2431/**
2432 * ata_eh_finish - finish up EH
2433 * @ap: host port to finish EH for
2434 *
2435 * Recovery is complete. Clean up EH states and retry or finish
2436 * failed qcs.
2437 *
2438 * LOCKING:
2439 * None.
2440 */
2441static void ata_eh_finish(struct ata_port *ap)
2442{
2443 int tag;
2444
2445 /* retry or finish qcs */
2446 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2447 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2448
2449 if (!(qc->flags & ATA_QCFLAG_FAILED))
2450 continue;
2451
2452 if (qc->err_mask) {
2453 /* FIXME: Once EH migration is complete,
2454 * generate sense data in this function,
2455 * considering both err_mask and tf.
2456 */
2457 if (qc->err_mask & AC_ERR_INVALID)
2458 ata_eh_qc_complete(qc);
2459 else
2460 ata_eh_qc_retry(qc);
2461 } else {
2462 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2463 ata_eh_qc_complete(qc);
2464 } else {
2465 /* feed zero TF to sense generation */
2466 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2467 ata_eh_qc_retry(qc);
2468 }
2469 }
2470 }
2471}
2472
2473/**
2474 * ata_do_eh - do standard error handling
2475 * @ap: host port to handle error for
Tejun Heof5914a42006-05-31 18:27:48 +09002476 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09002477 * @softreset: softreset method (can be NULL)
2478 * @hardreset: hardreset method (can be NULL)
2479 * @postreset: postreset method (can be NULL)
2480 *
2481 * Perform standard error handling sequence.
2482 *
2483 * LOCKING:
2484 * Kernel thread context (may sleep).
2485 */
Tejun Heof5914a42006-05-31 18:27:48 +09002486void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2487 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2488 ata_postreset_fn_t postreset)
Tejun Heo022bdb02006-05-15 20:58:22 +09002489{
Tejun Heo9b1e2652007-08-06 18:36:24 +09002490 struct ata_device *dev;
2491 int rc;
2492
2493 ata_eh_autopsy(ap);
2494 ata_eh_report(ap);
2495
2496 rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
2497 NULL);
2498 if (rc) {
2499 ata_link_for_each_dev(dev, &ap->link)
2500 ata_dev_disable(dev);
2501 }
2502
Tejun Heo022bdb02006-05-15 20:58:22 +09002503 ata_eh_finish(ap);
2504}
Tejun Heo500530f2006-07-03 16:07:27 +09002505
Tejun Heo6ffa01d2007-03-02 17:32:47 +09002506#ifdef CONFIG_PM
Tejun Heo500530f2006-07-03 16:07:27 +09002507/**
2508 * ata_eh_handle_port_suspend - perform port suspend operation
2509 * @ap: port to suspend
2510 *
2511 * Suspend @ap.
2512 *
2513 * LOCKING:
2514 * Kernel thread context (may sleep).
2515 */
2516static void ata_eh_handle_port_suspend(struct ata_port *ap)
2517{
2518 unsigned long flags;
2519 int rc = 0;
2520
2521 /* are we suspending? */
2522 spin_lock_irqsave(ap->lock, flags);
2523 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2524 ap->pm_mesg.event == PM_EVENT_ON) {
2525 spin_unlock_irqrestore(ap->lock, flags);
2526 return;
2527 }
2528 spin_unlock_irqrestore(ap->lock, flags);
2529
2530 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
2531
Tejun Heo64578a32007-05-15 03:28:16 +09002532 /* tell ACPI we're suspending */
2533 rc = ata_acpi_on_suspend(ap);
2534 if (rc)
2535 goto out;
2536
Tejun Heo500530f2006-07-03 16:07:27 +09002537 /* suspend */
2538 ata_eh_freeze_port(ap);
2539
2540 if (ap->ops->port_suspend)
2541 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
2542
Tejun Heo64578a32007-05-15 03:28:16 +09002543 out:
Tejun Heo500530f2006-07-03 16:07:27 +09002544 /* report result */
2545 spin_lock_irqsave(ap->lock, flags);
2546
2547 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
2548 if (rc == 0)
2549 ap->pflags |= ATA_PFLAG_SUSPENDED;
Tejun Heo64578a32007-05-15 03:28:16 +09002550 else if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heo500530f2006-07-03 16:07:27 +09002551 ata_port_schedule_eh(ap);
2552
2553 if (ap->pm_result) {
2554 *ap->pm_result = rc;
2555 ap->pm_result = NULL;
2556 }
2557
2558 spin_unlock_irqrestore(ap->lock, flags);
2559
2560 return;
2561}
2562
2563/**
2564 * ata_eh_handle_port_resume - perform port resume operation
2565 * @ap: port to resume
2566 *
2567 * Resume @ap.
2568 *
Tejun Heo500530f2006-07-03 16:07:27 +09002569 * LOCKING:
2570 * Kernel thread context (may sleep).
2571 */
2572static void ata_eh_handle_port_resume(struct ata_port *ap)
2573{
Tejun Heo500530f2006-07-03 16:07:27 +09002574 unsigned long flags;
Tejun Heo9666f402007-05-04 21:27:47 +02002575 int rc = 0;
Tejun Heo500530f2006-07-03 16:07:27 +09002576
2577 /* are we resuming? */
2578 spin_lock_irqsave(ap->lock, flags);
2579 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2580 ap->pm_mesg.event != PM_EVENT_ON) {
2581 spin_unlock_irqrestore(ap->lock, flags);
2582 return;
2583 }
2584 spin_unlock_irqrestore(ap->lock, flags);
2585
Tejun Heo9666f402007-05-04 21:27:47 +02002586 WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
Tejun Heo500530f2006-07-03 16:07:27 +09002587
2588 if (ap->ops->port_resume)
2589 rc = ap->ops->port_resume(ap);
2590
Tejun Heo67465442007-05-15 03:28:16 +09002591 /* tell ACPI that we're resuming */
2592 ata_acpi_on_resume(ap);
2593
Tejun Heo9666f402007-05-04 21:27:47 +02002594 /* report result */
Tejun Heo500530f2006-07-03 16:07:27 +09002595 spin_lock_irqsave(ap->lock, flags);
2596 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
2597 if (ap->pm_result) {
2598 *ap->pm_result = rc;
2599 ap->pm_result = NULL;
2600 }
2601 spin_unlock_irqrestore(ap->lock, flags);
2602}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09002603#endif /* CONFIG_PM */