blob: 733aa761f3eef94c2f628d3c3efefb417c7b7e1a [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 Heo0c247c52006-05-15 20:58:19 +0900154static void ata_ering_record(struct ata_ering *ering, int is_io,
155 unsigned int err_mask)
156{
157 struct ata_ering_entry *ent;
158
159 WARN_ON(!err_mask);
160
161 ering->cursor++;
162 ering->cursor %= ATA_ERING_SIZE;
163
164 ent = &ering->ring[ering->cursor];
165 ent->is_io = is_io;
166 ent->err_mask = err_mask;
167 ent->timestamp = get_jiffies_64();
168}
169
Tejun Heo7d47e8d2007-02-02 16:22:31 +0900170static void ata_ering_clear(struct ata_ering *ering)
Tejun Heo0c247c52006-05-15 20:58:19 +0900171{
Tejun Heo7d47e8d2007-02-02 16:22:31 +0900172 memset(ering, 0, sizeof(*ering));
Tejun Heo0c247c52006-05-15 20:58:19 +0900173}
174
175static int ata_ering_map(struct ata_ering *ering,
176 int (*map_fn)(struct ata_ering_entry *, void *),
177 void *arg)
178{
179 int idx, rc = 0;
180 struct ata_ering_entry *ent;
181
182 idx = ering->cursor;
183 do {
184 ent = &ering->ring[idx];
185 if (!ent->err_mask)
186 break;
187 rc = map_fn(ent, arg);
188 if (rc)
189 break;
190 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
191 } while (idx != ering->cursor);
192
193 return rc;
194}
195
Tejun Heo64f65ca2006-06-24 20:30:18 +0900196static unsigned int ata_eh_dev_action(struct ata_device *dev)
197{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900198 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heo64f65ca2006-06-24 20:30:18 +0900199
200 return ehc->i.action | ehc->i.dev_action[dev->devno];
201}
202
Tejun Heof58229f2007-08-06 18:36:23 +0900203static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
Tejun Heoaf181c22006-06-24 20:30:18 +0900204 struct ata_eh_info *ehi, unsigned int action)
205{
Tejun Heof58229f2007-08-06 18:36:23 +0900206 struct ata_device *tdev;
Tejun Heoaf181c22006-06-24 20:30:18 +0900207
208 if (!dev) {
209 ehi->action &= ~action;
Tejun Heof58229f2007-08-06 18:36:23 +0900210 ata_link_for_each_dev(tdev, link)
211 ehi->dev_action[tdev->devno] &= ~action;
Tejun Heoaf181c22006-06-24 20:30:18 +0900212 } else {
213 /* doesn't make sense for port-wide EH actions */
214 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
215
216 /* break ehi->action into ehi->dev_action */
217 if (ehi->action & action) {
Tejun Heof58229f2007-08-06 18:36:23 +0900218 ata_link_for_each_dev(tdev, link)
219 ehi->dev_action[tdev->devno] |=
220 ehi->action & action;
Tejun Heoaf181c22006-06-24 20:30:18 +0900221 ehi->action &= ~action;
222 }
223
224 /* turn off the specified per-dev action */
225 ehi->dev_action[dev->devno] &= ~action;
226 }
227}
228
Tejun Heoece1d632006-04-02 18:51:53 +0900229/**
230 * ata_scsi_timed_out - SCSI layer time out callback
231 * @cmd: timed out SCSI command
232 *
233 * Handles SCSI layer timeout. We race with normal completion of
234 * the qc for @cmd. If the qc is already gone, we lose and let
235 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
236 * timed out and EH should be invoked. Prevent ata_qc_complete()
237 * from finishing it by setting EH_SCHEDULED and return
238 * EH_NOT_HANDLED.
239 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900240 * TODO: kill this function once old EH is gone.
241 *
Tejun Heoece1d632006-04-02 18:51:53 +0900242 * LOCKING:
243 * Called from timer context
244 *
245 * RETURNS:
246 * EH_HANDLED or EH_NOT_HANDLED
247 */
248enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
249{
250 struct Scsi_Host *host = cmd->device->host;
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400251 struct ata_port *ap = ata_shost_to_port(host);
Tejun Heoece1d632006-04-02 18:51:53 +0900252 unsigned long flags;
253 struct ata_queued_cmd *qc;
Tejun Heoad9e2762006-05-15 20:58:12 +0900254 enum scsi_eh_timer_return ret;
Tejun Heoece1d632006-04-02 18:51:53 +0900255
256 DPRINTK("ENTER\n");
257
Tejun Heoad9e2762006-05-15 20:58:12 +0900258 if (ap->ops->error_handler) {
259 ret = EH_NOT_HANDLED;
260 goto out;
261 }
262
263 ret = EH_HANDLED;
Jeff Garzikba6a1302006-06-22 23:46:10 -0400264 spin_lock_irqsave(ap->lock, flags);
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900265 qc = ata_qc_from_tag(ap, ap->link.active_tag);
Tejun Heoece1d632006-04-02 18:51:53 +0900266 if (qc) {
267 WARN_ON(qc->scsicmd != cmd);
268 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
269 qc->err_mask |= AC_ERR_TIMEOUT;
270 ret = EH_NOT_HANDLED;
271 }
Jeff Garzikba6a1302006-06-22 23:46:10 -0400272 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900273
Tejun Heoad9e2762006-05-15 20:58:12 +0900274 out:
Tejun Heoece1d632006-04-02 18:51:53 +0900275 DPRINTK("EXIT, ret=%d\n", ret);
276 return ret;
277}
278
279/**
280 * ata_scsi_error - SCSI layer error handler callback
281 * @host: SCSI host on which error occurred
282 *
283 * Handles SCSI-layer-thrown error events.
284 *
285 * LOCKING:
286 * Inherited from SCSI layer (none, can sleep)
287 *
288 * RETURNS:
289 * Zero.
290 */
Jeff Garzik381544b2006-04-11 13:04:39 -0400291void ata_scsi_error(struct Scsi_Host *host)
Tejun Heoece1d632006-04-02 18:51:53 +0900292{
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400293 struct ata_port *ap = ata_shost_to_port(host);
Tejun Heoad9e2762006-05-15 20:58:12 +0900294 int i, repeat_cnt = ATA_EH_MAX_REPEAT;
295 unsigned long flags;
Tejun Heoece1d632006-04-02 18:51:53 +0900296
297 DPRINTK("ENTER\n");
298
Tejun Heoad9e2762006-05-15 20:58:12 +0900299 /* synchronize with port task */
Tejun Heoece1d632006-04-02 18:51:53 +0900300 ata_port_flush_task(ap);
301
Jeff Garzikcca39742006-08-24 03:19:22 -0400302 /* synchronize with host lock and sort out timeouts */
Tejun Heoece1d632006-04-02 18:51:53 +0900303
Tejun Heoad9e2762006-05-15 20:58:12 +0900304 /* For new EH, all qcs are finished in one of three ways -
305 * normal completion, error completion, and SCSI timeout.
306 * Both cmpletions can race against SCSI timeout. When normal
307 * completion wins, the qc never reaches EH. When error
308 * completion wins, the qc has ATA_QCFLAG_FAILED set.
309 *
310 * When SCSI timeout wins, things are a bit more complex.
311 * Normal or error completion can occur after the timeout but
312 * before this point. In such cases, both types of
313 * completions are honored. A scmd is determined to have
314 * timed out iff its associated qc is active and not failed.
315 */
316 if (ap->ops->error_handler) {
317 struct scsi_cmnd *scmd, *tmp;
318 int nr_timedout = 0;
Tejun Heoece1d632006-04-02 18:51:53 +0900319
Tejun Heoe30349d2006-07-03 03:02:15 +0900320 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900321
322 list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
323 struct ata_queued_cmd *qc;
324
325 for (i = 0; i < ATA_MAX_QUEUE; i++) {
326 qc = __ata_qc_from_tag(ap, i);
327 if (qc->flags & ATA_QCFLAG_ACTIVE &&
328 qc->scsicmd == scmd)
329 break;
330 }
331
332 if (i < ATA_MAX_QUEUE) {
333 /* the scmd has an associated qc */
334 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
335 /* which hasn't failed yet, timeout */
336 qc->err_mask |= AC_ERR_TIMEOUT;
337 qc->flags |= ATA_QCFLAG_FAILED;
338 nr_timedout++;
339 }
340 } else {
341 /* Normal completion occurred after
342 * SCSI timeout but before this point.
343 * Successfully complete it.
344 */
345 scmd->retries = scmd->allowed;
346 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
347 }
348 }
349
350 /* If we have timed out qcs. They belong to EH from
351 * this point but the state of the controller is
352 * unknown. Freeze the port to make sure the IRQ
353 * handler doesn't diddle with those qcs. This must
354 * be done atomically w.r.t. setting QCFLAG_FAILED.
355 */
356 if (nr_timedout)
357 __ata_port_freeze(ap);
358
Tejun Heoe30349d2006-07-03 03:02:15 +0900359 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900360 } else
Tejun Heoe30349d2006-07-03 03:02:15 +0900361 spin_unlock_wait(ap->lock);
Tejun Heoad9e2762006-05-15 20:58:12 +0900362
363 repeat:
364 /* invoke error handler */
365 if (ap->ops->error_handler) {
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900366 /* kill fast drain timer */
367 del_timer_sync(&ap->fastdrain_timer);
368
Tejun Heo500530f2006-07-03 16:07:27 +0900369 /* process port resume request */
370 ata_eh_handle_port_resume(ap);
371
Tejun Heof3e81b12006-05-15 20:58:21 +0900372 /* fetch & clear EH info */
Tejun Heoe30349d2006-07-03 03:02:15 +0900373 spin_lock_irqsave(ap->lock, flags);
Tejun Heof3e81b12006-05-15 20:58:21 +0900374
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900375 memset(&ap->link.eh_context, 0, sizeof(ap->link.eh_context));
376 ap->link.eh_context.i = ap->link.eh_info;
377 memset(&ap->link.eh_info, 0, sizeof(ap->link.eh_info));
Tejun Heof3e81b12006-05-15 20:58:21 +0900378
Tejun Heob51e9e52006-06-29 01:29:30 +0900379 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
380 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
Tejun Heof3e81b12006-05-15 20:58:21 +0900381
Tejun Heoe30349d2006-07-03 03:02:15 +0900382 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900383
Tejun Heo500530f2006-07-03 16:07:27 +0900384 /* invoke EH, skip if unloading or suspended */
385 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
Tejun Heo720ba122006-05-31 18:28:13 +0900386 ap->ops->error_handler(ap);
387 else
388 ata_eh_finish(ap);
Tejun Heoad9e2762006-05-15 20:58:12 +0900389
Tejun Heo500530f2006-07-03 16:07:27 +0900390 /* process port suspend request */
391 ata_eh_handle_port_suspend(ap);
392
Tejun Heoad9e2762006-05-15 20:58:12 +0900393 /* Exception might have happend after ->error_handler
394 * recovered the port but before this point. Repeat
395 * EH in such case.
396 */
Tejun Heoe30349d2006-07-03 03:02:15 +0900397 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900398
Tejun Heob51e9e52006-06-29 01:29:30 +0900399 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
Tejun Heoad9e2762006-05-15 20:58:12 +0900400 if (--repeat_cnt) {
401 ata_port_printk(ap, KERN_INFO,
402 "EH pending after completion, "
403 "repeating EH (cnt=%d)\n", repeat_cnt);
Tejun Heoe30349d2006-07-03 03:02:15 +0900404 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900405 goto repeat;
406 }
407 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
408 "tries, giving up\n", ATA_EH_MAX_REPEAT);
Tejun Heo914616a2007-06-25 21:47:11 +0900409 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
Tejun Heoad9e2762006-05-15 20:58:12 +0900410 }
411
Tejun Heof3e81b12006-05-15 20:58:21 +0900412 /* this run is complete, make sure EH info is clear */
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900413 memset(&ap->link.eh_info, 0, sizeof(ap->link.eh_info));
Tejun Heof3e81b12006-05-15 20:58:21 +0900414
Tejun Heoe30349d2006-07-03 03:02:15 +0900415 /* Clear host_eh_scheduled while holding ap->lock such
Tejun Heoad9e2762006-05-15 20:58:12 +0900416 * that if exception occurs after this point but
417 * before EH completion, SCSI midlayer will
418 * re-initiate EH.
419 */
420 host->host_eh_scheduled = 0;
421
Tejun Heoe30349d2006-07-03 03:02:15 +0900422 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900423 } else {
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900424 WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
Tejun Heoad9e2762006-05-15 20:58:12 +0900425 ap->ops->eng_timeout(ap);
426 }
427
428 /* finish or retry handled scmd's and clean up */
Tejun Heoece1d632006-04-02 18:51:53 +0900429 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
430
431 scsi_eh_flush_done_q(&ap->eh_done_q);
432
Tejun Heoad9e2762006-05-15 20:58:12 +0900433 /* clean up */
Tejun Heoe30349d2006-07-03 03:02:15 +0900434 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900435
Tejun Heo1cdaf532006-07-03 16:07:26 +0900436 if (ap->pflags & ATA_PFLAG_LOADING)
Tejun Heob51e9e52006-06-29 01:29:30 +0900437 ap->pflags &= ~ATA_PFLAG_LOADING;
Tejun Heo1cdaf532006-07-03 16:07:26 +0900438 else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
David Howells52bad642006-11-22 14:54:01 +0000439 queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0);
Tejun Heo1cdaf532006-07-03 16:07:26 +0900440
441 if (ap->pflags & ATA_PFLAG_RECOVERED)
442 ata_port_printk(ap, KERN_INFO, "EH complete\n");
Tejun Heo580b21022006-05-31 18:28:05 +0900443
Tejun Heob51e9e52006-06-29 01:29:30 +0900444 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
Tejun Heoad9e2762006-05-15 20:58:12 +0900445
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900446 /* tell wait_eh that we're done */
Tejun Heob51e9e52006-06-29 01:29:30 +0900447 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900448 wake_up_all(&ap->eh_wait_q);
449
Tejun Heoe30349d2006-07-03 03:02:15 +0900450 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900451
Tejun Heoece1d632006-04-02 18:51:53 +0900452 DPRINTK("EXIT\n");
Tejun Heoece1d632006-04-02 18:51:53 +0900453}
454
455/**
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900456 * ata_port_wait_eh - Wait for the currently pending EH to complete
457 * @ap: Port to wait EH for
458 *
459 * Wait until the currently pending EH is complete.
460 *
461 * LOCKING:
462 * Kernel thread context (may sleep).
463 */
464void ata_port_wait_eh(struct ata_port *ap)
465{
466 unsigned long flags;
467 DEFINE_WAIT(wait);
468
469 retry:
Jeff Garzikba6a1302006-06-22 23:46:10 -0400470 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900471
Tejun Heob51e9e52006-06-29 01:29:30 +0900472 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900473 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
Jeff Garzikba6a1302006-06-22 23:46:10 -0400474 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900475 schedule();
Jeff Garzikba6a1302006-06-22 23:46:10 -0400476 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900477 }
Tejun Heo0a1b6222006-06-11 11:01:38 +0900478 finish_wait(&ap->eh_wait_q, &wait);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900479
Jeff Garzikba6a1302006-06-22 23:46:10 -0400480 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900481
482 /* make sure SCSI EH is complete */
Jeff Garzikcca39742006-08-24 03:19:22 -0400483 if (scsi_host_in_recovery(ap->scsi_host)) {
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900484 msleep(10);
485 goto retry;
486 }
487}
488
489/**
Tejun Heoece1d632006-04-02 18:51:53 +0900490 * ata_qc_timeout - Handle timeout of queued command
491 * @qc: Command that timed out
492 *
493 * Some part of the kernel (currently, only the SCSI layer)
494 * has noticed that the active command on port @ap has not
495 * completed after a specified length of time. Handle this
496 * condition by disabling DMA (if necessary) and completing
497 * transactions, with error if necessary.
498 *
499 * This also handles the case of the "lost interrupt", where
500 * for some reason (possibly hardware bug, possibly driver bug)
501 * an interrupt was not delivered to the driver, even though the
502 * transaction completed successfully.
503 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900504 * TODO: kill this function once old EH is gone.
505 *
Tejun Heoece1d632006-04-02 18:51:53 +0900506 * LOCKING:
507 * Inherited from SCSI layer (none, can sleep)
508 */
509static void ata_qc_timeout(struct ata_queued_cmd *qc)
510{
511 struct ata_port *ap = qc->ap;
Tejun Heoece1d632006-04-02 18:51:53 +0900512 u8 host_stat = 0, drv_stat;
513 unsigned long flags;
514
515 DPRINTK("ENTER\n");
516
517 ap->hsm_task_state = HSM_ST_IDLE;
518
Jeff Garzikba6a1302006-06-22 23:46:10 -0400519 spin_lock_irqsave(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900520
521 switch (qc->tf.protocol) {
522
523 case ATA_PROT_DMA:
524 case ATA_PROT_ATAPI_DMA:
525 host_stat = ap->ops->bmdma_status(ap);
526
527 /* before we do anything else, clear DMA-Start bit */
528 ap->ops->bmdma_stop(qc);
529
530 /* fall through */
531
532 default:
533 ata_altstatus(ap);
534 drv_stat = ata_chk_status(ap);
535
536 /* ack bmdma irq events */
537 ap->ops->irq_clear(ap);
538
Tejun Heof15a1da2006-05-15 20:57:56 +0900539 ata_dev_printk(qc->dev, KERN_ERR, "command 0x%x timeout, "
540 "stat 0x%x host_stat 0x%x\n",
541 qc->tf.command, drv_stat, host_stat);
Tejun Heoece1d632006-04-02 18:51:53 +0900542
543 /* complete taskfile transaction */
Jeff Garzikc13b56a2006-04-02 10:34:24 -0400544 qc->err_mask |= AC_ERR_TIMEOUT;
Tejun Heoece1d632006-04-02 18:51:53 +0900545 break;
546 }
547
Jeff Garzikba6a1302006-06-22 23:46:10 -0400548 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900549
550 ata_eh_qc_complete(qc);
551
552 DPRINTK("EXIT\n");
553}
554
555/**
556 * ata_eng_timeout - Handle timeout of queued command
557 * @ap: Port on which timed-out command is active
558 *
559 * Some part of the kernel (currently, only the SCSI layer)
560 * has noticed that the active command on port @ap has not
561 * completed after a specified length of time. Handle this
562 * condition by disabling DMA (if necessary) and completing
563 * transactions, with error if necessary.
564 *
565 * This also handles the case of the "lost interrupt", where
566 * for some reason (possibly hardware bug, possibly driver bug)
567 * an interrupt was not delivered to the driver, even though the
568 * transaction completed successfully.
569 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900570 * TODO: kill this function once old EH is gone.
571 *
Tejun Heoece1d632006-04-02 18:51:53 +0900572 * LOCKING:
573 * Inherited from SCSI layer (none, can sleep)
574 */
575void ata_eng_timeout(struct ata_port *ap)
576{
577 DPRINTK("ENTER\n");
578
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900579 ata_qc_timeout(ata_qc_from_tag(ap, ap->link.active_tag));
Tejun Heoece1d632006-04-02 18:51:53 +0900580
581 DPRINTK("EXIT\n");
582}
583
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900584static int ata_eh_nr_in_flight(struct ata_port *ap)
585{
586 unsigned int tag;
587 int nr = 0;
588
589 /* count only non-internal commands */
590 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++)
591 if (ata_qc_from_tag(ap, tag))
592 nr++;
593
594 return nr;
595}
596
597void ata_eh_fastdrain_timerfn(unsigned long arg)
598{
599 struct ata_port *ap = (void *)arg;
600 unsigned long flags;
601 int cnt;
602
603 spin_lock_irqsave(ap->lock, flags);
604
605 cnt = ata_eh_nr_in_flight(ap);
606
607 /* are we done? */
608 if (!cnt)
609 goto out_unlock;
610
611 if (cnt == ap->fastdrain_cnt) {
612 unsigned int tag;
613
614 /* No progress during the last interval, tag all
615 * in-flight qcs as timed out and freeze the port.
616 */
617 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) {
618 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
619 if (qc)
620 qc->err_mask |= AC_ERR_TIMEOUT;
621 }
622
623 ata_port_freeze(ap);
624 } else {
625 /* some qcs have finished, give it another chance */
626 ap->fastdrain_cnt = cnt;
627 ap->fastdrain_timer.expires =
628 jiffies + ATA_EH_FASTDRAIN_INTERVAL;
629 add_timer(&ap->fastdrain_timer);
630 }
631
632 out_unlock:
633 spin_unlock_irqrestore(ap->lock, flags);
634}
635
636/**
637 * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
638 * @ap: target ATA port
639 * @fastdrain: activate fast drain
640 *
641 * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
642 * is non-zero and EH wasn't pending before. Fast drain ensures
643 * that EH kicks in in timely manner.
644 *
645 * LOCKING:
646 * spin_lock_irqsave(host lock)
647 */
648static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
649{
650 int cnt;
651
652 /* already scheduled? */
653 if (ap->pflags & ATA_PFLAG_EH_PENDING)
654 return;
655
656 ap->pflags |= ATA_PFLAG_EH_PENDING;
657
658 if (!fastdrain)
659 return;
660
661 /* do we have in-flight qcs? */
662 cnt = ata_eh_nr_in_flight(ap);
663 if (!cnt)
664 return;
665
666 /* activate fast drain */
667 ap->fastdrain_cnt = cnt;
668 ap->fastdrain_timer.expires = jiffies + ATA_EH_FASTDRAIN_INTERVAL;
669 add_timer(&ap->fastdrain_timer);
670}
671
Tejun Heof686bcb2006-05-15 20:58:05 +0900672/**
673 * ata_qc_schedule_eh - schedule qc for error handling
674 * @qc: command to schedule error handling for
675 *
676 * Schedule error handling for @qc. EH will kick in as soon as
677 * other commands are drained.
678 *
679 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400680 * spin_lock_irqsave(host lock)
Tejun Heof686bcb2006-05-15 20:58:05 +0900681 */
682void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
683{
684 struct ata_port *ap = qc->ap;
685
686 WARN_ON(!ap->ops->error_handler);
687
688 qc->flags |= ATA_QCFLAG_FAILED;
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900689 ata_eh_set_pending(ap, 1);
Tejun Heof686bcb2006-05-15 20:58:05 +0900690
691 /* The following will fail if timeout has already expired.
692 * ata_scsi_error() takes care of such scmds on EH entry.
693 * Note that ATA_QCFLAG_FAILED is unconditionally set after
694 * this function completes.
695 */
696 scsi_req_abort_cmd(qc->scsicmd);
697}
698
Tejun Heo7b70fc02006-05-15 20:58:07 +0900699/**
700 * ata_port_schedule_eh - schedule error handling without a qc
701 * @ap: ATA port to schedule EH for
702 *
703 * Schedule error handling for @ap. EH will kick in as soon as
704 * all commands are drained.
705 *
706 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400707 * spin_lock_irqsave(host lock)
Tejun Heo7b70fc02006-05-15 20:58:07 +0900708 */
709void ata_port_schedule_eh(struct ata_port *ap)
710{
711 WARN_ON(!ap->ops->error_handler);
712
Tejun Heof4d6d002007-05-01 11:50:15 +0200713 if (ap->pflags & ATA_PFLAG_INITIALIZING)
714 return;
715
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900716 ata_eh_set_pending(ap, 1);
Jeff Garzikcca39742006-08-24 03:19:22 -0400717 scsi_schedule_eh(ap->scsi_host);
Tejun Heo7b70fc02006-05-15 20:58:07 +0900718
719 DPRINTK("port EH scheduled\n");
720}
721
722/**
723 * ata_port_abort - abort all qc's on the port
724 * @ap: ATA port to abort qc's for
725 *
726 * Abort all active qc's of @ap and schedule EH.
727 *
728 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400729 * spin_lock_irqsave(host lock)
Tejun Heo7b70fc02006-05-15 20:58:07 +0900730 *
731 * RETURNS:
732 * Number of aborted qc's.
733 */
734int ata_port_abort(struct ata_port *ap)
735{
736 int tag, nr_aborted = 0;
737
738 WARN_ON(!ap->ops->error_handler);
739
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900740 /* we're gonna abort all commands, no need for fast drain */
741 ata_eh_set_pending(ap, 0);
742
Tejun Heo7b70fc02006-05-15 20:58:07 +0900743 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
744 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
745
746 if (qc) {
747 qc->flags |= ATA_QCFLAG_FAILED;
748 ata_qc_complete(qc);
749 nr_aborted++;
750 }
751 }
752
753 if (!nr_aborted)
754 ata_port_schedule_eh(ap);
755
756 return nr_aborted;
757}
758
Tejun Heoe3180492006-05-15 20:58:09 +0900759/**
760 * __ata_port_freeze - freeze port
761 * @ap: ATA port to freeze
762 *
763 * This function is called when HSM violation or some other
764 * condition disrupts normal operation of the port. Frozen port
765 * is not allowed to perform any operation until the port is
766 * thawed, which usually follows a successful reset.
767 *
768 * ap->ops->freeze() callback can be used for freezing the port
769 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
770 * port cannot be frozen hardware-wise, the interrupt handler
771 * must ack and clear interrupts unconditionally while the port
772 * is frozen.
773 *
774 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400775 * spin_lock_irqsave(host lock)
Tejun Heoe3180492006-05-15 20:58:09 +0900776 */
777static void __ata_port_freeze(struct ata_port *ap)
778{
779 WARN_ON(!ap->ops->error_handler);
780
781 if (ap->ops->freeze)
782 ap->ops->freeze(ap);
783
Tejun Heob51e9e52006-06-29 01:29:30 +0900784 ap->pflags |= ATA_PFLAG_FROZEN;
Tejun Heoe3180492006-05-15 20:58:09 +0900785
Tejun Heo44877b42007-02-21 01:06:51 +0900786 DPRINTK("ata%u port frozen\n", ap->print_id);
Tejun Heoe3180492006-05-15 20:58:09 +0900787}
788
789/**
790 * ata_port_freeze - abort & freeze port
791 * @ap: ATA port to freeze
792 *
793 * Abort and freeze @ap.
794 *
795 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400796 * spin_lock_irqsave(host lock)
Tejun Heoe3180492006-05-15 20:58:09 +0900797 *
798 * RETURNS:
799 * Number of aborted commands.
800 */
801int ata_port_freeze(struct ata_port *ap)
802{
803 int nr_aborted;
804
805 WARN_ON(!ap->ops->error_handler);
806
807 nr_aborted = ata_port_abort(ap);
808 __ata_port_freeze(ap);
809
810 return nr_aborted;
811}
812
813/**
814 * ata_eh_freeze_port - EH helper to freeze port
815 * @ap: ATA port to freeze
816 *
817 * Freeze @ap.
818 *
819 * LOCKING:
820 * None.
821 */
822void ata_eh_freeze_port(struct ata_port *ap)
823{
824 unsigned long flags;
825
826 if (!ap->ops->error_handler)
827 return;
828
Jeff Garzikba6a1302006-06-22 23:46:10 -0400829 spin_lock_irqsave(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900830 __ata_port_freeze(ap);
Jeff Garzikba6a1302006-06-22 23:46:10 -0400831 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900832}
833
834/**
835 * ata_port_thaw_port - EH helper to thaw port
836 * @ap: ATA port to thaw
837 *
838 * Thaw frozen port @ap.
839 *
840 * LOCKING:
841 * None.
842 */
843void ata_eh_thaw_port(struct ata_port *ap)
844{
845 unsigned long flags;
846
847 if (!ap->ops->error_handler)
848 return;
849
Jeff Garzikba6a1302006-06-22 23:46:10 -0400850 spin_lock_irqsave(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900851
Tejun Heob51e9e52006-06-29 01:29:30 +0900852 ap->pflags &= ~ATA_PFLAG_FROZEN;
Tejun Heoe3180492006-05-15 20:58:09 +0900853
854 if (ap->ops->thaw)
855 ap->ops->thaw(ap);
856
Jeff Garzikba6a1302006-06-22 23:46:10 -0400857 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900858
Tejun Heo44877b42007-02-21 01:06:51 +0900859 DPRINTK("ata%u port thawed\n", ap->print_id);
Tejun Heoe3180492006-05-15 20:58:09 +0900860}
861
Tejun Heoece1d632006-04-02 18:51:53 +0900862static void ata_eh_scsidone(struct scsi_cmnd *scmd)
863{
864 /* nada */
865}
866
867static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
868{
869 struct ata_port *ap = qc->ap;
870 struct scsi_cmnd *scmd = qc->scsicmd;
871 unsigned long flags;
872
Jeff Garzikba6a1302006-06-22 23:46:10 -0400873 spin_lock_irqsave(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900874 qc->scsidone = ata_eh_scsidone;
875 __ata_qc_complete(qc);
876 WARN_ON(ata_tag_valid(qc->tag));
Jeff Garzikba6a1302006-06-22 23:46:10 -0400877 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900878
879 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
880}
881
882/**
883 * ata_eh_qc_complete - Complete an active ATA command from EH
884 * @qc: Command to complete
885 *
886 * Indicate to the mid and upper layers that an ATA command has
887 * completed. To be used from EH.
888 */
889void ata_eh_qc_complete(struct ata_queued_cmd *qc)
890{
891 struct scsi_cmnd *scmd = qc->scsicmd;
892 scmd->retries = scmd->allowed;
893 __ata_eh_qc_complete(qc);
894}
895
896/**
897 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
898 * @qc: Command to retry
899 *
900 * Indicate to the mid and upper layers that an ATA command
901 * should be retried. To be used from EH.
902 *
903 * SCSI midlayer limits the number of retries to scmd->allowed.
904 * scmd->retries is decremented for commands which get retried
905 * due to unrelated failures (qc->err_mask is zero).
906 */
907void ata_eh_qc_retry(struct ata_queued_cmd *qc)
908{
909 struct scsi_cmnd *scmd = qc->scsicmd;
910 if (!qc->err_mask && scmd->retries)
911 scmd->retries--;
912 __ata_eh_qc_complete(qc);
913}
Tejun Heo022bdb02006-05-15 20:58:22 +0900914
915/**
Tejun Heo0ea035a2006-05-31 18:28:01 +0900916 * ata_eh_detach_dev - detach ATA device
917 * @dev: ATA device to detach
918 *
919 * Detach @dev.
920 *
921 * LOCKING:
922 * None.
923 */
924static void ata_eh_detach_dev(struct ata_device *dev)
925{
Tejun Heof58229f2007-08-06 18:36:23 +0900926 struct ata_link *link = dev->link;
927 struct ata_port *ap = link->ap;
Tejun Heo0ea035a2006-05-31 18:28:01 +0900928 unsigned long flags;
929
930 ata_dev_disable(dev);
931
Jeff Garzikba6a1302006-06-22 23:46:10 -0400932 spin_lock_irqsave(ap->lock, flags);
Tejun Heo0ea035a2006-05-31 18:28:01 +0900933
934 dev->flags &= ~ATA_DFLAG_DETACH;
935
936 if (ata_scsi_offline_dev(dev)) {
937 dev->flags |= ATA_DFLAG_DETACHED;
Tejun Heob51e9e52006-06-29 01:29:30 +0900938 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
Tejun Heo0ea035a2006-05-31 18:28:01 +0900939 }
940
Tejun Heobeb07c12006-06-24 20:30:19 +0900941 /* clear per-dev EH actions */
Tejun Heof58229f2007-08-06 18:36:23 +0900942 ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
943 ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
Tejun Heobeb07c12006-06-24 20:30:19 +0900944
Jeff Garzikba6a1302006-06-22 23:46:10 -0400945 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo0ea035a2006-05-31 18:28:01 +0900946}
947
948/**
Tejun Heo022bdb02006-05-15 20:58:22 +0900949 * ata_eh_about_to_do - about to perform eh_action
Tejun Heo955e57d2007-08-06 18:36:23 +0900950 * @link: target ATA link
Tejun Heo47005f22006-06-19 18:27:23 +0900951 * @dev: target ATA dev for per-dev action (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +0900952 * @action: action about to be performed
953 *
954 * Called just before performing EH actions to clear related bits
Tejun Heo955e57d2007-08-06 18:36:23 +0900955 * in @link->eh_info such that eh actions are not unnecessarily
956 * repeated.
Tejun Heo022bdb02006-05-15 20:58:22 +0900957 *
958 * LOCKING:
959 * None.
960 */
Tejun Heo955e57d2007-08-06 18:36:23 +0900961static void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
Tejun Heo47005f22006-06-19 18:27:23 +0900962 unsigned int action)
Tejun Heo022bdb02006-05-15 20:58:22 +0900963{
Tejun Heo955e57d2007-08-06 18:36:23 +0900964 struct ata_port *ap = link->ap;
965 struct ata_eh_info *ehi = &link->eh_info;
966 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +0900967 unsigned long flags;
968
Jeff Garzikba6a1302006-06-22 23:46:10 -0400969 spin_lock_irqsave(ap->lock, flags);
Tejun Heo1cdaf532006-07-03 16:07:26 +0900970
Tejun Heo13abf502006-07-10 23:18:46 +0900971 /* Reset is represented by combination of actions and EHI
972 * flags. Suck in all related bits before clearing eh_info to
973 * avoid losing requested action.
974 */
975 if (action & ATA_EH_RESET_MASK) {
976 ehc->i.action |= ehi->action & ATA_EH_RESET_MASK;
977 ehc->i.flags |= ehi->flags & ATA_EHI_RESET_MODIFIER_MASK;
Tejun Heo1cdaf532006-07-03 16:07:26 +0900978
Tejun Heo13abf502006-07-10 23:18:46 +0900979 /* make sure all reset actions are cleared & clear EHI flags */
980 action |= ATA_EH_RESET_MASK;
981 ehi->flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
982 }
983
Tejun Heo955e57d2007-08-06 18:36:23 +0900984 ata_eh_clear_action(link, dev, ehi, action);
Tejun Heo13abf502006-07-10 23:18:46 +0900985
986 if (!(ehc->i.flags & ATA_EHI_QUIET))
Tejun Heo1cdaf532006-07-03 16:07:26 +0900987 ap->pflags |= ATA_PFLAG_RECOVERED;
988
Jeff Garzikba6a1302006-06-22 23:46:10 -0400989 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo022bdb02006-05-15 20:58:22 +0900990}
991
992/**
Tejun Heo47005f22006-06-19 18:27:23 +0900993 * ata_eh_done - EH action complete
Tejun Heo955e57d2007-08-06 18:36:23 +0900994* @ap: target ATA port
Tejun Heo47005f22006-06-19 18:27:23 +0900995 * @dev: target ATA dev for per-dev action (can be NULL)
996 * @action: action just completed
997 *
998 * Called right after performing EH actions to clear related bits
Tejun Heo955e57d2007-08-06 18:36:23 +0900999 * in @link->eh_context.
Tejun Heo47005f22006-06-19 18:27:23 +09001000 *
1001 * LOCKING:
1002 * None.
1003 */
Tejun Heo955e57d2007-08-06 18:36:23 +09001004static void ata_eh_done(struct ata_link *link, struct ata_device *dev,
Tejun Heo47005f22006-06-19 18:27:23 +09001005 unsigned int action)
1006{
Tejun Heo955e57d2007-08-06 18:36:23 +09001007 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001008
Tejun Heo13abf502006-07-10 23:18:46 +09001009 /* if reset is complete, clear all reset actions & reset modifier */
1010 if (action & ATA_EH_RESET_MASK) {
1011 action |= ATA_EH_RESET_MASK;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001012 ehc->i.flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
Tejun Heo13abf502006-07-10 23:18:46 +09001013 }
1014
Tejun Heo955e57d2007-08-06 18:36:23 +09001015 ata_eh_clear_action(link, dev, &ehc->i, action);
Tejun Heo47005f22006-06-19 18:27:23 +09001016}
1017
1018/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001019 * ata_err_string - convert err_mask to descriptive string
1020 * @err_mask: error mask to convert to string
1021 *
1022 * Convert @err_mask to descriptive string. Errors are
1023 * prioritized according to severity and only the most severe
1024 * error is reported.
1025 *
1026 * LOCKING:
1027 * None.
1028 *
1029 * RETURNS:
1030 * Descriptive string for @err_mask
1031 */
1032static const char * ata_err_string(unsigned int err_mask)
1033{
1034 if (err_mask & AC_ERR_HOST_BUS)
1035 return "host bus error";
1036 if (err_mask & AC_ERR_ATA_BUS)
1037 return "ATA bus error";
1038 if (err_mask & AC_ERR_TIMEOUT)
1039 return "timeout";
1040 if (err_mask & AC_ERR_HSM)
1041 return "HSM violation";
1042 if (err_mask & AC_ERR_SYSTEM)
1043 return "internal error";
1044 if (err_mask & AC_ERR_MEDIA)
1045 return "media error";
1046 if (err_mask & AC_ERR_INVALID)
1047 return "invalid argument";
1048 if (err_mask & AC_ERR_DEV)
1049 return "device error";
1050 return "unknown error";
1051}
1052
1053/**
Tejun Heoe8ee8452006-05-15 21:03:46 +09001054 * ata_read_log_page - read a specific log page
1055 * @dev: target device
1056 * @page: page to read
1057 * @buf: buffer to store read page
1058 * @sectors: number of sectors to read
1059 *
1060 * Read log page using READ_LOG_EXT command.
1061 *
1062 * LOCKING:
1063 * Kernel thread context (may sleep).
1064 *
1065 * RETURNS:
1066 * 0 on success, AC_ERR_* mask otherwise.
1067 */
1068static unsigned int ata_read_log_page(struct ata_device *dev,
1069 u8 page, void *buf, unsigned int sectors)
1070{
1071 struct ata_taskfile tf;
1072 unsigned int err_mask;
1073
1074 DPRINTK("read log page - page %d\n", page);
1075
1076 ata_tf_init(dev, &tf);
1077 tf.command = ATA_CMD_READ_LOG_EXT;
1078 tf.lbal = page;
1079 tf.nsect = sectors;
1080 tf.hob_nsect = sectors >> 8;
1081 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1082 tf.protocol = ATA_PROT_PIO;
1083
1084 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
1085 buf, sectors * ATA_SECT_SIZE);
1086
1087 DPRINTK("EXIT, err_mask=%x\n", err_mask);
1088 return err_mask;
1089}
1090
1091/**
1092 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
1093 * @dev: Device to read log page 10h from
1094 * @tag: Resulting tag of the failed command
1095 * @tf: Resulting taskfile registers of the failed command
1096 *
1097 * Read log page 10h to obtain NCQ error details and clear error
1098 * condition.
1099 *
1100 * LOCKING:
1101 * Kernel thread context (may sleep).
1102 *
1103 * RETURNS:
1104 * 0 on success, -errno otherwise.
1105 */
1106static int ata_eh_read_log_10h(struct ata_device *dev,
1107 int *tag, struct ata_taskfile *tf)
1108{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001109 u8 *buf = dev->link->ap->sector_buf;
Tejun Heoe8ee8452006-05-15 21:03:46 +09001110 unsigned int err_mask;
1111 u8 csum;
1112 int i;
1113
1114 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
1115 if (err_mask)
1116 return -EIO;
1117
1118 csum = 0;
1119 for (i = 0; i < ATA_SECT_SIZE; i++)
1120 csum += buf[i];
1121 if (csum)
1122 ata_dev_printk(dev, KERN_WARNING,
1123 "invalid checksum 0x%x on log page 10h\n", csum);
1124
1125 if (buf[0] & 0x80)
1126 return -ENOENT;
1127
1128 *tag = buf[0] & 0x1f;
1129
1130 tf->command = buf[2];
1131 tf->feature = buf[3];
1132 tf->lbal = buf[4];
1133 tf->lbam = buf[5];
1134 tf->lbah = buf[6];
1135 tf->device = buf[7];
1136 tf->hob_lbal = buf[8];
1137 tf->hob_lbam = buf[9];
1138 tf->hob_lbah = buf[10];
1139 tf->nsect = buf[12];
1140 tf->hob_nsect = buf[13];
1141
1142 return 0;
1143}
1144
1145/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001146 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1147 * @dev: device to perform REQUEST_SENSE to
1148 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
1149 *
1150 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
1151 * SENSE. This function is EH helper.
1152 *
1153 * LOCKING:
1154 * Kernel thread context (may sleep).
1155 *
1156 * RETURNS:
1157 * 0 on success, AC_ERR_* mask on failure
1158 */
Albert Lee56287762007-04-02 11:30:46 +08001159static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc)
Tejun Heo022bdb02006-05-15 20:58:22 +09001160{
Albert Lee56287762007-04-02 11:30:46 +08001161 struct ata_device *dev = qc->dev;
1162 unsigned char *sense_buf = qc->scsicmd->sense_buffer;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001163 struct ata_port *ap = dev->link->ap;
Tejun Heo022bdb02006-05-15 20:58:22 +09001164 struct ata_taskfile tf;
1165 u8 cdb[ATAPI_CDB_LEN];
1166
1167 DPRINTK("ATAPI request sense\n");
1168
Tejun Heo022bdb02006-05-15 20:58:22 +09001169 /* FIXME: is this needed? */
1170 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1171
Albert Lee56287762007-04-02 11:30:46 +08001172 /* initialize sense_buf with the error register,
1173 * for the case where they are -not- overwritten
1174 */
Tejun Heo022bdb02006-05-15 20:58:22 +09001175 sense_buf[0] = 0x70;
Albert Lee56287762007-04-02 11:30:46 +08001176 sense_buf[2] = qc->result_tf.feature >> 4;
1177
Jeff Garzika617c092007-05-21 20:14:23 -04001178 /* some devices time out if garbage left in tf */
Albert Lee56287762007-04-02 11:30:46 +08001179 ata_tf_init(dev, &tf);
Tejun Heo022bdb02006-05-15 20:58:22 +09001180
1181 memset(cdb, 0, ATAPI_CDB_LEN);
1182 cdb[0] = REQUEST_SENSE;
1183 cdb[4] = SCSI_SENSE_BUFFERSIZE;
1184
1185 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1186 tf.command = ATA_CMD_PACKET;
1187
1188 /* is it pointless to prefer PIO for "safety reasons"? */
1189 if (ap->flags & ATA_FLAG_PIO_DMA) {
1190 tf.protocol = ATA_PROT_ATAPI_DMA;
1191 tf.feature |= ATAPI_PKT_DMA;
1192 } else {
1193 tf.protocol = ATA_PROT_ATAPI;
1194 tf.lbam = (8 * 1024) & 0xff;
1195 tf.lbah = (8 * 1024) >> 8;
1196 }
1197
1198 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
1199 sense_buf, SCSI_SENSE_BUFFERSIZE);
1200}
1201
1202/**
1203 * ata_eh_analyze_serror - analyze SError for a failed port
Tejun Heo02607312007-08-06 18:36:23 +09001204 * @link: ATA link to analyze SError for
Tejun Heo022bdb02006-05-15 20:58:22 +09001205 *
1206 * Analyze SError if available and further determine cause of
1207 * failure.
1208 *
1209 * LOCKING:
1210 * None.
1211 */
Tejun Heo02607312007-08-06 18:36:23 +09001212static void ata_eh_analyze_serror(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001213{
Tejun Heo02607312007-08-06 18:36:23 +09001214 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001215 u32 serror = ehc->i.serror;
1216 unsigned int err_mask = 0, action = 0;
1217
1218 if (serror & SERR_PERSISTENT) {
1219 err_mask |= AC_ERR_ATA_BUS;
1220 action |= ATA_EH_HARDRESET;
1221 }
1222 if (serror &
1223 (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
1224 err_mask |= AC_ERR_ATA_BUS;
1225 action |= ATA_EH_SOFTRESET;
1226 }
1227 if (serror & SERR_PROTOCOL) {
1228 err_mask |= AC_ERR_HSM;
1229 action |= ATA_EH_SOFTRESET;
1230 }
1231 if (serror & SERR_INTERNAL) {
1232 err_mask |= AC_ERR_SYSTEM;
Tejun Heo771b8da2007-03-14 01:20:51 +09001233 action |= ATA_EH_HARDRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001234 }
Tejun Heo084fe632006-05-31 18:28:03 +09001235 if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG))
1236 ata_ehi_hotplugged(&ehc->i);
Tejun Heo022bdb02006-05-15 20:58:22 +09001237
1238 ehc->i.err_mask |= err_mask;
1239 ehc->i.action |= action;
1240}
1241
1242/**
Tejun Heoe8ee8452006-05-15 21:03:46 +09001243 * ata_eh_analyze_ncq_error - analyze NCQ error
Tejun Heo02607312007-08-06 18:36:23 +09001244 * @link: ATA link to analyze NCQ error for
Tejun Heoe8ee8452006-05-15 21:03:46 +09001245 *
1246 * Read log page 10h, determine the offending qc and acquire
1247 * error status TF. For NCQ device errors, all LLDDs have to do
1248 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1249 * care of the rest.
1250 *
1251 * LOCKING:
1252 * Kernel thread context (may sleep).
1253 */
Tejun Heo02607312007-08-06 18:36:23 +09001254static void ata_eh_analyze_ncq_error(struct ata_link *link)
Tejun Heoe8ee8452006-05-15 21:03:46 +09001255{
Tejun Heo02607312007-08-06 18:36:23 +09001256 struct ata_port *ap = link->ap;
1257 struct ata_eh_context *ehc = &link->eh_context;
1258 struct ata_device *dev = link->device;
Tejun Heoe8ee8452006-05-15 21:03:46 +09001259 struct ata_queued_cmd *qc;
1260 struct ata_taskfile tf;
1261 int tag, rc;
1262
1263 /* if frozen, we can't do much */
Tejun Heob51e9e52006-06-29 01:29:30 +09001264 if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heoe8ee8452006-05-15 21:03:46 +09001265 return;
1266
1267 /* is it NCQ device error? */
Tejun Heo02607312007-08-06 18:36:23 +09001268 if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
Tejun Heoe8ee8452006-05-15 21:03:46 +09001269 return;
1270
1271 /* has LLDD analyzed already? */
1272 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1273 qc = __ata_qc_from_tag(ap, tag);
1274
1275 if (!(qc->flags & ATA_QCFLAG_FAILED))
1276 continue;
1277
1278 if (qc->err_mask)
1279 return;
1280 }
1281
1282 /* okay, this error is ours */
1283 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1284 if (rc) {
Tejun Heo02607312007-08-06 18:36:23 +09001285 ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
Tejun Heoe8ee8452006-05-15 21:03:46 +09001286 "(errno=%d)\n", rc);
1287 return;
1288 }
1289
Tejun Heo02607312007-08-06 18:36:23 +09001290 if (!(link->sactive & (1 << tag))) {
1291 ata_link_printk(link, KERN_ERR, "log page 10h reported "
Tejun Heoe8ee8452006-05-15 21:03:46 +09001292 "inactive tag %d\n", tag);
1293 return;
1294 }
1295
1296 /* we've got the perpetrator, condemn it */
1297 qc = __ata_qc_from_tag(ap, tag);
1298 memcpy(&qc->result_tf, &tf, sizeof(tf));
Tejun Heo5335b722007-07-16 14:29:40 +09001299 qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
Tejun Heoe8ee8452006-05-15 21:03:46 +09001300 ehc->i.err_mask &= ~AC_ERR_DEV;
1301}
1302
1303/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001304 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1305 * @qc: qc to analyze
1306 * @tf: Taskfile registers to analyze
1307 *
1308 * Analyze taskfile of @qc and further determine cause of
1309 * failure. This function also requests ATAPI sense data if
1310 * avaliable.
1311 *
1312 * LOCKING:
1313 * Kernel thread context (may sleep).
1314 *
1315 * RETURNS:
1316 * Determined recovery action
1317 */
1318static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1319 const struct ata_taskfile *tf)
1320{
1321 unsigned int tmp, action = 0;
1322 u8 stat = tf->command, err = tf->feature;
1323
1324 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1325 qc->err_mask |= AC_ERR_HSM;
1326 return ATA_EH_SOFTRESET;
1327 }
1328
Tejun Heoa51d6442007-03-20 15:24:11 +09001329 if (stat & (ATA_ERR | ATA_DF))
1330 qc->err_mask |= AC_ERR_DEV;
1331 else
Tejun Heo022bdb02006-05-15 20:58:22 +09001332 return 0;
1333
1334 switch (qc->dev->class) {
1335 case ATA_DEV_ATA:
1336 if (err & ATA_ICRC)
1337 qc->err_mask |= AC_ERR_ATA_BUS;
1338 if (err & ATA_UNC)
1339 qc->err_mask |= AC_ERR_MEDIA;
1340 if (err & ATA_IDNF)
1341 qc->err_mask |= AC_ERR_INVALID;
1342 break;
1343
1344 case ATA_DEV_ATAPI:
Tejun Heoa569a302006-11-21 10:40:51 +09001345 if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
Albert Lee56287762007-04-02 11:30:46 +08001346 tmp = atapi_eh_request_sense(qc);
Tejun Heoa569a302006-11-21 10:40:51 +09001347 if (!tmp) {
1348 /* ATA_QCFLAG_SENSE_VALID is used to
1349 * tell atapi_qc_complete() that sense
1350 * data is already valid.
1351 *
1352 * TODO: interpret sense data and set
1353 * appropriate err_mask.
1354 */
1355 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1356 } else
1357 qc->err_mask |= tmp;
1358 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001359 }
1360
1361 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1362 action |= ATA_EH_SOFTRESET;
1363
1364 return action;
1365}
1366
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001367static int ata_eh_categorize_error(int is_io, unsigned int err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001368{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001369 if (err_mask & AC_ERR_ATA_BUS)
Tejun Heo022bdb02006-05-15 20:58:22 +09001370 return 1;
1371
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001372 if (err_mask & AC_ERR_TIMEOUT)
1373 return 2;
1374
1375 if (is_io) {
1376 if (err_mask & AC_ERR_HSM)
Tejun Heo022bdb02006-05-15 20:58:22 +09001377 return 2;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001378 if ((err_mask &
1379 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1380 return 3;
Tejun Heo022bdb02006-05-15 20:58:22 +09001381 }
1382
1383 return 0;
1384}
1385
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001386struct speed_down_verdict_arg {
Tejun Heo022bdb02006-05-15 20:58:22 +09001387 u64 since;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001388 int nr_errors[4];
Tejun Heo022bdb02006-05-15 20:58:22 +09001389};
1390
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001391static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
Tejun Heo022bdb02006-05-15 20:58:22 +09001392{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001393 struct speed_down_verdict_arg *arg = void_arg;
1394 int cat = ata_eh_categorize_error(ent->is_io, ent->err_mask);
Tejun Heo022bdb02006-05-15 20:58:22 +09001395
1396 if (ent->timestamp < arg->since)
1397 return -1;
1398
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001399 arg->nr_errors[cat]++;
Tejun Heo022bdb02006-05-15 20:58:22 +09001400 return 0;
1401}
1402
1403/**
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001404 * ata_eh_speed_down_verdict - Determine speed down verdict
Tejun Heo022bdb02006-05-15 20:58:22 +09001405 * @dev: Device of interest
1406 *
1407 * This function examines error ring of @dev and determines
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001408 * whether NCQ needs to be turned off, transfer speed should be
1409 * stepped down, or falling back to PIO is necessary.
Tejun Heo022bdb02006-05-15 20:58:22 +09001410 *
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001411 * Cat-1 is ATA_BUS error for any command.
Tejun Heo022bdb02006-05-15 20:58:22 +09001412 *
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001413 * Cat-2 is TIMEOUT for any command or HSM violation for known
1414 * supported commands.
1415 *
1416 * Cat-3 is is unclassified DEV error for known supported
Tejun Heo022bdb02006-05-15 20:58:22 +09001417 * command.
1418 *
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001419 * NCQ needs to be turned off if there have been more than 3
1420 * Cat-2 + Cat-3 errors during last 10 minutes.
1421 *
1422 * Speed down is necessary if there have been more than 3 Cat-1 +
1423 * Cat-2 errors or 10 Cat-3 errors during last 10 minutes.
1424 *
1425 * Falling back to PIO mode is necessary if there have been more
1426 * than 10 Cat-1 + Cat-2 + Cat-3 errors during last 5 minutes.
1427 *
Tejun Heo022bdb02006-05-15 20:58:22 +09001428 * LOCKING:
1429 * Inherited from caller.
1430 *
1431 * RETURNS:
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001432 * OR of ATA_EH_SPDN_* flags.
Tejun Heo022bdb02006-05-15 20:58:22 +09001433 */
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001434static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
Tejun Heo022bdb02006-05-15 20:58:22 +09001435{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001436 const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
1437 u64 j64 = get_jiffies_64();
1438 struct speed_down_verdict_arg arg;
1439 unsigned int verdict = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09001440
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001441 /* scan past 10 mins of error history */
Tejun Heo022bdb02006-05-15 20:58:22 +09001442 memset(&arg, 0, sizeof(arg));
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001443 arg.since = j64 - min(j64, j10mins);
1444 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
Tejun Heo022bdb02006-05-15 20:58:22 +09001445
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001446 if (arg.nr_errors[2] + arg.nr_errors[3] > 3)
1447 verdict |= ATA_EH_SPDN_NCQ_OFF;
1448 if (arg.nr_errors[1] + arg.nr_errors[2] > 3 || arg.nr_errors[3] > 10)
1449 verdict |= ATA_EH_SPDN_SPEED_DOWN;
Tejun Heo022bdb02006-05-15 20:58:22 +09001450
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001451 /* scan past 3 mins of error history */
1452 memset(&arg, 0, sizeof(arg));
1453 arg.since = j64 - min(j64, j5mins);
1454 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
Tejun Heo022bdb02006-05-15 20:58:22 +09001455
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001456 if (arg.nr_errors[1] + arg.nr_errors[2] + arg.nr_errors[3] > 10)
1457 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
1458
1459 return verdict;
Tejun Heo022bdb02006-05-15 20:58:22 +09001460}
1461
1462/**
1463 * ata_eh_speed_down - record error and speed down if necessary
1464 * @dev: Failed device
1465 * @is_io: Did the device fail during normal IO?
1466 * @err_mask: err_mask of the error
1467 *
1468 * Record error and examine error history to determine whether
1469 * adjusting transmission speed is necessary. It also sets
1470 * transmission limits appropriately if such adjustment is
1471 * necessary.
1472 *
1473 * LOCKING:
1474 * Kernel thread context (may sleep).
1475 *
1476 * RETURNS:
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001477 * Determined recovery action.
Tejun Heo022bdb02006-05-15 20:58:22 +09001478 */
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001479static unsigned int ata_eh_speed_down(struct ata_device *dev, int is_io,
1480 unsigned int err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001481{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001482 unsigned int verdict;
1483 unsigned int action = 0;
1484
1485 /* don't bother if Cat-0 error */
1486 if (ata_eh_categorize_error(is_io, err_mask) == 0)
Tejun Heo022bdb02006-05-15 20:58:22 +09001487 return 0;
1488
1489 /* record error and determine whether speed down is necessary */
1490 ata_ering_record(&dev->ering, is_io, err_mask);
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001491 verdict = ata_eh_speed_down_verdict(dev);
Tejun Heo022bdb02006-05-15 20:58:22 +09001492
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001493 /* turn off NCQ? */
1494 if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
1495 (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
1496 ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
1497 dev->flags |= ATA_DFLAG_NCQ_OFF;
1498 ata_dev_printk(dev, KERN_WARNING,
1499 "NCQ disabled due to excessive errors\n");
1500 goto done;
1501 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001502
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001503 /* speed down? */
1504 if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1505 /* speed down SATA link speed if possible */
Tejun Heo936fd732007-08-06 18:36:23 +09001506 if (sata_down_spd_limit(dev->link) == 0) {
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001507 action |= ATA_EH_HARDRESET;
1508 goto done;
1509 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001510
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001511 /* lower transfer mode */
1512 if (dev->spdn_cnt < 2) {
1513 static const int dma_dnxfer_sel[] =
1514 { ATA_DNXFER_DMA, ATA_DNXFER_40C };
1515 static const int pio_dnxfer_sel[] =
1516 { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
1517 int sel;
Tejun Heo022bdb02006-05-15 20:58:22 +09001518
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001519 if (dev->xfer_shift != ATA_SHIFT_PIO)
1520 sel = dma_dnxfer_sel[dev->spdn_cnt];
1521 else
1522 sel = pio_dnxfer_sel[dev->spdn_cnt];
1523
1524 dev->spdn_cnt++;
1525
1526 if (ata_down_xfermask_limit(dev, sel) == 0) {
1527 action |= ATA_EH_SOFTRESET;
1528 goto done;
1529 }
1530 }
1531 }
1532
1533 /* Fall back to PIO? Slowing down to PIO is meaningless for
1534 * SATA. Consider it only for PATA.
1535 */
1536 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001537 (dev->link->ap->cbl != ATA_CBL_SATA) &&
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001538 (dev->xfer_shift != ATA_SHIFT_PIO)) {
1539 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
1540 dev->spdn_cnt = 0;
1541 action |= ATA_EH_SOFTRESET;
1542 goto done;
1543 }
1544 }
1545
Tejun Heo022bdb02006-05-15 20:58:22 +09001546 return 0;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001547 done:
1548 /* device has been slowed down, blow error history */
1549 ata_ering_clear(&dev->ering);
1550 return action;
Tejun Heo022bdb02006-05-15 20:58:22 +09001551}
1552
1553/**
1554 * ata_eh_autopsy - analyze error and determine recovery action
Tejun Heo02607312007-08-06 18:36:23 +09001555 * @link: ATA link to perform autopsy on
Tejun Heo022bdb02006-05-15 20:58:22 +09001556 *
Tejun Heo02607312007-08-06 18:36:23 +09001557 * Analyze why @link failed and determine which recovery actions
1558 * are needed. This function also sets more detailed AC_ERR_*
1559 * values and fills sense data for ATAPI CHECK SENSE.
Tejun Heo022bdb02006-05-15 20:58:22 +09001560 *
1561 * LOCKING:
1562 * Kernel thread context (may sleep).
1563 */
Tejun Heo02607312007-08-06 18:36:23 +09001564static void ata_eh_autopsy(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001565{
Tejun Heo02607312007-08-06 18:36:23 +09001566 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09001567 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001568 unsigned int all_err_mask = 0;
1569 int tag, is_io = 0;
1570 u32 serror;
1571 int rc;
1572
1573 DPRINTK("ENTER\n");
1574
Tejun Heo1cdaf532006-07-03 16:07:26 +09001575 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1576 return;
1577
Tejun Heo022bdb02006-05-15 20:58:22 +09001578 /* obtain and analyze SError */
Tejun Heo936fd732007-08-06 18:36:23 +09001579 rc = sata_scr_read(link, SCR_ERROR, &serror);
Tejun Heo022bdb02006-05-15 20:58:22 +09001580 if (rc == 0) {
1581 ehc->i.serror |= serror;
Tejun Heo02607312007-08-06 18:36:23 +09001582 ata_eh_analyze_serror(link);
Tejun Heo4e57c512007-07-16 14:29:41 +09001583 } else if (rc != -EOPNOTSUPP) {
1584 /* SError read failed, force hardreset and probing */
1585 ata_ehi_schedule_probe(&ehc->i);
Tejun Heo4528e4d2006-07-08 20:17:26 +09001586 ehc->i.action |= ATA_EH_HARDRESET;
Tejun Heo4e57c512007-07-16 14:29:41 +09001587 ehc->i.err_mask |= AC_ERR_OTHER;
1588 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001589
Tejun Heoe8ee8452006-05-15 21:03:46 +09001590 /* analyze NCQ failure */
Tejun Heo02607312007-08-06 18:36:23 +09001591 ata_eh_analyze_ncq_error(link);
Tejun Heoe8ee8452006-05-15 21:03:46 +09001592
Tejun Heo022bdb02006-05-15 20:58:22 +09001593 /* any real error trumps AC_ERR_OTHER */
1594 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1595 ehc->i.err_mask &= ~AC_ERR_OTHER;
1596
1597 all_err_mask |= ehc->i.err_mask;
1598
1599 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1600 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1601
Tejun Heo02607312007-08-06 18:36:23 +09001602 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001603 continue;
1604
1605 /* inherit upper level err_mask */
1606 qc->err_mask |= ehc->i.err_mask;
1607
Tejun Heo022bdb02006-05-15 20:58:22 +09001608 /* analyze TF */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001609 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
Tejun Heo022bdb02006-05-15 20:58:22 +09001610
1611 /* DEV errors are probably spurious in case of ATA_BUS error */
1612 if (qc->err_mask & AC_ERR_ATA_BUS)
1613 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1614 AC_ERR_INVALID);
1615
1616 /* any real error trumps unknown error */
1617 if (qc->err_mask & ~AC_ERR_OTHER)
1618 qc->err_mask &= ~AC_ERR_OTHER;
1619
1620 /* SENSE_VALID trumps dev/unknown error and revalidation */
1621 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1622 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
Tejun Heo4528e4d2006-07-08 20:17:26 +09001623 ehc->i.action &= ~ATA_EH_REVALIDATE;
Tejun Heo022bdb02006-05-15 20:58:22 +09001624 }
1625
1626 /* accumulate error info */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001627 ehc->i.dev = qc->dev;
Tejun Heo022bdb02006-05-15 20:58:22 +09001628 all_err_mask |= qc->err_mask;
1629 if (qc->flags & ATA_QCFLAG_IO)
1630 is_io = 1;
1631 }
1632
Tejun Heoa20f33f2006-05-16 12:58:24 +09001633 /* enforce default EH actions */
Tejun Heob51e9e52006-06-29 01:29:30 +09001634 if (ap->pflags & ATA_PFLAG_FROZEN ||
Tejun Heoa20f33f2006-05-16 12:58:24 +09001635 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
Tejun Heo4528e4d2006-07-08 20:17:26 +09001636 ehc->i.action |= ATA_EH_SOFTRESET;
Tejun Heoa20f33f2006-05-16 12:58:24 +09001637 else if (all_err_mask)
Tejun Heo4528e4d2006-07-08 20:17:26 +09001638 ehc->i.action |= ATA_EH_REVALIDATE;
Tejun Heo022bdb02006-05-15 20:58:22 +09001639
Tejun Heo47005f22006-06-19 18:27:23 +09001640 /* if we have offending qcs and the associated failed device */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001641 if (ehc->i.dev) {
Tejun Heo47005f22006-06-19 18:27:23 +09001642 /* speed down */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001643 ehc->i.action |= ata_eh_speed_down(ehc->i.dev, is_io,
1644 all_err_mask);
Tejun Heo47005f22006-06-19 18:27:23 +09001645
1646 /* perform per-dev EH action only on the offending device */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001647 ehc->i.dev_action[ehc->i.dev->devno] |=
1648 ehc->i.action & ATA_EH_PERDEV_MASK;
1649 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
Tejun Heo47005f22006-06-19 18:27:23 +09001650 }
1651
Tejun Heo022bdb02006-05-15 20:58:22 +09001652 DPRINTK("EXIT\n");
1653}
1654
1655/**
1656 * ata_eh_report - report error handling to user
Tejun Heo02607312007-08-06 18:36:23 +09001657 * @link: ATA link EH is going on
Tejun Heo022bdb02006-05-15 20:58:22 +09001658 *
1659 * Report EH to user.
1660 *
1661 * LOCKING:
1662 * None.
1663 */
Tejun Heo02607312007-08-06 18:36:23 +09001664static void ata_eh_report(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001665{
Tejun Heo02607312007-08-06 18:36:23 +09001666 struct ata_port *ap = link->ap;
1667 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001668 const char *frozen, *desc;
1669 int tag, nr_failed = 0;
1670
1671 desc = NULL;
1672 if (ehc->i.desc[0] != '\0')
1673 desc = ehc->i.desc;
1674
1675 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1676 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1677
Tejun Heo02607312007-08-06 18:36:23 +09001678 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001679 continue;
1680 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1681 continue;
1682
1683 nr_failed++;
1684 }
1685
1686 if (!nr_failed && !ehc->i.err_mask)
1687 return;
1688
1689 frozen = "";
Tejun Heob51e9e52006-06-29 01:29:30 +09001690 if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heo022bdb02006-05-15 20:58:22 +09001691 frozen = " frozen";
1692
1693 if (ehc->i.dev) {
Tejun Heoe8ee8452006-05-15 21:03:46 +09001694 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1695 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
Tejun Heo02607312007-08-06 18:36:23 +09001696 ehc->i.err_mask, link->sactive,
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001697 ehc->i.serror, ehc->i.action, frozen);
Tejun Heo022bdb02006-05-15 20:58:22 +09001698 if (desc)
Tejun Heob64bbc32007-07-16 14:29:39 +09001699 ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
Tejun Heo022bdb02006-05-15 20:58:22 +09001700 } else {
Tejun Heo02607312007-08-06 18:36:23 +09001701 ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
Tejun Heoe8ee8452006-05-15 21:03:46 +09001702 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
Tejun Heo02607312007-08-06 18:36:23 +09001703 ehc->i.err_mask, link->sactive,
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001704 ehc->i.serror, ehc->i.action, frozen);
Tejun Heo022bdb02006-05-15 20:58:22 +09001705 if (desc)
Tejun Heo02607312007-08-06 18:36:23 +09001706 ata_link_printk(link, KERN_ERR, "%s\n", desc);
Tejun Heo022bdb02006-05-15 20:58:22 +09001707 }
1708
1709 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
Tejun Heo8a937582006-11-14 22:36:12 +09001710 static const char *dma_str[] = {
1711 [DMA_BIDIRECTIONAL] = "bidi",
1712 [DMA_TO_DEVICE] = "out",
1713 [DMA_FROM_DEVICE] = "in",
1714 [DMA_NONE] = "",
1715 };
Tejun Heo022bdb02006-05-15 20:58:22 +09001716 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
Tejun Heo8a937582006-11-14 22:36:12 +09001717 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
Tejun Heo022bdb02006-05-15 20:58:22 +09001718
Tejun Heo02607312007-08-06 18:36:23 +09001719 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
1720 qc->dev->link != link || !qc->err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001721 continue;
1722
Tejun Heo8a937582006-11-14 22:36:12 +09001723 ata_dev_printk(qc->dev, KERN_ERR,
1724 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
Tejun Heo664e8502006-11-20 16:05:34 +09001725 "tag %d cdb 0x%x data %u %s\n "
Tejun Heo8a937582006-11-14 22:36:12 +09001726 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
Tejun Heo5335b722007-07-16 14:29:40 +09001727 "Emask 0x%x (%s)%s\n",
Tejun Heo8a937582006-11-14 22:36:12 +09001728 cmd->command, cmd->feature, cmd->nsect,
1729 cmd->lbal, cmd->lbam, cmd->lbah,
1730 cmd->hob_feature, cmd->hob_nsect,
1731 cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
Tejun Heo726f0782007-01-03 17:30:39 +09001732 cmd->device, qc->tag, qc->cdb[0], qc->nbytes,
Tejun Heo664e8502006-11-20 16:05:34 +09001733 dma_str[qc->dma_dir],
Tejun Heo8a937582006-11-14 22:36:12 +09001734 res->command, res->feature, res->nsect,
1735 res->lbal, res->lbam, res->lbah,
1736 res->hob_feature, res->hob_nsect,
1737 res->hob_lbal, res->hob_lbam, res->hob_lbah,
Tejun Heo5335b722007-07-16 14:29:40 +09001738 res->device, qc->err_mask, ata_err_string(qc->err_mask),
1739 qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
Tejun Heo022bdb02006-05-15 20:58:22 +09001740 }
1741}
1742
Tejun Heocc0680a2007-08-06 18:36:23 +09001743static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
Tejun Heod4b2bab2007-02-02 16:50:52 +09001744 unsigned int *classes, unsigned long deadline)
Tejun Heod87fa382006-05-31 18:28:24 +09001745{
Tejun Heof58229f2007-08-06 18:36:23 +09001746 struct ata_device *dev;
1747 int rc;
Tejun Heod87fa382006-05-31 18:28:24 +09001748
Tejun Heocc0680a2007-08-06 18:36:23 +09001749 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09001750 classes[dev->devno] = ATA_DEV_UNKNOWN;
Tejun Heod87fa382006-05-31 18:28:24 +09001751
Tejun Heocc0680a2007-08-06 18:36:23 +09001752 rc = reset(link, classes, deadline);
Tejun Heod87fa382006-05-31 18:28:24 +09001753 if (rc)
1754 return rc;
1755
1756 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
1757 * is complete and convert all ATA_DEV_UNKNOWN to
1758 * ATA_DEV_NONE.
1759 */
Tejun Heocc0680a2007-08-06 18:36:23 +09001760 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09001761 if (classes[dev->devno] != ATA_DEV_UNKNOWN)
Tejun Heod87fa382006-05-31 18:28:24 +09001762 break;
1763
Tejun Heof58229f2007-08-06 18:36:23 +09001764 if (dev) {
Tejun Heocc0680a2007-08-06 18:36:23 +09001765 ata_link_for_each_dev(dev, link) {
Tejun Heof58229f2007-08-06 18:36:23 +09001766 if (classes[dev->devno] == ATA_DEV_UNKNOWN)
1767 classes[dev->devno] = ATA_DEV_NONE;
1768 }
1769 }
Tejun Heod87fa382006-05-31 18:28:24 +09001770
1771 return 0;
1772}
1773
Tejun Heo664faf02006-05-31 18:27:50 +09001774static int ata_eh_followup_srst_needed(int rc, int classify,
1775 const unsigned int *classes)
1776{
1777 if (rc == -EAGAIN)
1778 return 1;
1779 if (rc != 0)
1780 return 0;
1781 if (classify && classes[0] == ATA_DEV_UNKNOWN)
1782 return 1;
1783 return 0;
1784}
1785
Tejun Heocc0680a2007-08-06 18:36:23 +09001786static int ata_eh_reset(struct ata_link *link, int classify,
Tejun Heof5914a42006-05-31 18:27:48 +09001787 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
Tejun Heo022bdb02006-05-15 20:58:22 +09001788 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
1789{
Tejun Heo936fd732007-08-06 18:36:23 +09001790 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo664faf02006-05-31 18:27:50 +09001791 unsigned int *classes = ehc->classes;
Tejun Heo1cdaf532006-07-03 16:07:26 +09001792 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
Tejun Heo31daabd2007-02-02 16:50:52 +09001793 int try = 0;
Tejun Heof58229f2007-08-06 18:36:23 +09001794 struct ata_device *dev;
Tejun Heo31daabd2007-02-02 16:50:52 +09001795 unsigned long deadline;
Tejun Heof5914a42006-05-31 18:27:48 +09001796 unsigned int action;
Tejun Heo022bdb02006-05-15 20:58:22 +09001797 ata_reset_fn_t reset;
Tejun Heof58229f2007-08-06 18:36:23 +09001798 int rc;
Tejun Heo022bdb02006-05-15 20:58:22 +09001799
Tejun Heo13abf502006-07-10 23:18:46 +09001800 /* about to reset */
Tejun Heo955e57d2007-08-06 18:36:23 +09001801 ata_eh_about_to_do(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
Tejun Heo13abf502006-07-10 23:18:46 +09001802
Tejun Heof5914a42006-05-31 18:27:48 +09001803 /* Determine which reset to use and record in ehc->i.action.
1804 * prereset() may examine and modify it.
1805 */
1806 action = ehc->i.action;
1807 ehc->i.action &= ~ATA_EH_RESET_MASK;
Tejun Heo936fd732007-08-06 18:36:23 +09001808 if (softreset && (!hardreset || (!sata_set_spd_needed(link) &&
Tejun Heof5914a42006-05-31 18:27:48 +09001809 !(action & ATA_EH_HARDRESET))))
1810 ehc->i.action |= ATA_EH_SOFTRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001811 else
Tejun Heof5914a42006-05-31 18:27:48 +09001812 ehc->i.action |= ATA_EH_HARDRESET;
1813
1814 if (prereset) {
Tejun Heocc0680a2007-08-06 18:36:23 +09001815 rc = prereset(link, jiffies + ATA_EH_PRERESET_TIMEOUT);
Tejun Heof5914a42006-05-31 18:27:48 +09001816 if (rc) {
Alan Coxc9619222006-09-26 17:53:38 +01001817 if (rc == -ENOENT) {
Tejun Heocc0680a2007-08-06 18:36:23 +09001818 ata_link_printk(link, KERN_DEBUG,
Tejun Heo4aa9ab62007-03-12 17:24:08 +09001819 "port disabled. ignoring.\n");
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001820 ehc->i.action &= ~ATA_EH_RESET_MASK;
Tejun Heo4aa9ab62007-03-12 17:24:08 +09001821
Tejun Heo936fd732007-08-06 18:36:23 +09001822 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09001823 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heo4aa9ab62007-03-12 17:24:08 +09001824
1825 rc = 0;
Alan Coxc9619222006-09-26 17:53:38 +01001826 } else
Tejun Heocc0680a2007-08-06 18:36:23 +09001827 ata_link_printk(link, KERN_ERR,
Tejun Heof5914a42006-05-31 18:27:48 +09001828 "prereset failed (errno=%d)\n", rc);
Tejun Heofccb6ea2007-07-16 14:29:41 +09001829 goto out;
Tejun Heof5914a42006-05-31 18:27:48 +09001830 }
1831 }
1832
1833 /* prereset() might have modified ehc->i.action */
1834 if (ehc->i.action & ATA_EH_HARDRESET)
Tejun Heo022bdb02006-05-15 20:58:22 +09001835 reset = hardreset;
Tejun Heof5914a42006-05-31 18:27:48 +09001836 else if (ehc->i.action & ATA_EH_SOFTRESET)
1837 reset = softreset;
1838 else {
1839 /* prereset told us not to reset, bang classes and return */
Tejun Heo936fd732007-08-06 18:36:23 +09001840 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09001841 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heofccb6ea2007-07-16 14:29:41 +09001842 rc = 0;
1843 goto out;
Tejun Heof5914a42006-05-31 18:27:48 +09001844 }
1845
1846 /* did prereset() screw up? if so, fix up to avoid oopsing */
1847 if (!reset) {
Tejun Heof5914a42006-05-31 18:27:48 +09001848 if (softreset)
1849 reset = softreset;
1850 else
1851 reset = hardreset;
1852 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001853
1854 retry:
Tejun Heo31daabd2007-02-02 16:50:52 +09001855 deadline = jiffies + ata_eh_reset_timeouts[try++];
1856
Tejun Heo3e706392006-05-31 18:28:11 +09001857 /* shut up during boot probing */
1858 if (verbose)
Tejun Heocc0680a2007-08-06 18:36:23 +09001859 ata_link_printk(link, KERN_INFO, "%s resetting link\n",
Tejun Heo3e706392006-05-31 18:28:11 +09001860 reset == softreset ? "soft" : "hard");
Tejun Heo022bdb02006-05-15 20:58:22 +09001861
Tejun Heo13abf502006-07-10 23:18:46 +09001862 /* mark that this EH session started with reset */
Tejun Heo0d64a232007-04-23 02:41:05 +09001863 if (reset == hardreset)
1864 ehc->i.flags |= ATA_EHI_DID_HARDRESET;
1865 else
1866 ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001867
Tejun Heocc0680a2007-08-06 18:36:23 +09001868 rc = ata_do_reset(link, reset, classes, deadline);
Tejun Heo022bdb02006-05-15 20:58:22 +09001869
Tejun Heo664faf02006-05-31 18:27:50 +09001870 if (reset == hardreset &&
1871 ata_eh_followup_srst_needed(rc, classify, classes)) {
1872 /* okay, let's do follow-up softreset */
Tejun Heo664faf02006-05-31 18:27:50 +09001873 reset = softreset;
1874
1875 if (!reset) {
Tejun Heocc0680a2007-08-06 18:36:23 +09001876 ata_link_printk(link, KERN_ERR,
Tejun Heo664faf02006-05-31 18:27:50 +09001877 "follow-up softreset required "
1878 "but no softreset avaliable\n");
Tejun Heofccb6ea2007-07-16 14:29:41 +09001879 rc = -EINVAL;
1880 goto out;
Tejun Heo664faf02006-05-31 18:27:50 +09001881 }
1882
Tejun Heo955e57d2007-08-06 18:36:23 +09001883 ata_eh_about_to_do(link, NULL, ATA_EH_RESET_MASK);
Tejun Heocc0680a2007-08-06 18:36:23 +09001884 rc = ata_do_reset(link, reset, classes, deadline);
Tejun Heo664faf02006-05-31 18:27:50 +09001885
1886 if (rc == 0 && classify &&
1887 classes[0] == ATA_DEV_UNKNOWN) {
Tejun Heocc0680a2007-08-06 18:36:23 +09001888 ata_link_printk(link, KERN_ERR,
Tejun Heo664faf02006-05-31 18:27:50 +09001889 "classification failed\n");
Tejun Heofccb6ea2007-07-16 14:29:41 +09001890 rc = -EINVAL;
1891 goto out;
Tejun Heo664faf02006-05-31 18:27:50 +09001892 }
1893 }
1894
Tejun Heo31daabd2007-02-02 16:50:52 +09001895 if (rc && try < ARRAY_SIZE(ata_eh_reset_timeouts)) {
1896 unsigned long now = jiffies;
Tejun Heo664faf02006-05-31 18:27:50 +09001897
Tejun Heo31daabd2007-02-02 16:50:52 +09001898 if (time_before(now, deadline)) {
1899 unsigned long delta = deadline - jiffies;
Tejun Heo664faf02006-05-31 18:27:50 +09001900
Tejun Heocc0680a2007-08-06 18:36:23 +09001901 ata_link_printk(link, KERN_WARNING, "reset failed "
Tejun Heo31daabd2007-02-02 16:50:52 +09001902 "(errno=%d), retrying in %u secs\n",
1903 rc, (jiffies_to_msecs(delta) + 999) / 1000);
Tejun Heo022bdb02006-05-15 20:58:22 +09001904
Tejun Heo31daabd2007-02-02 16:50:52 +09001905 schedule_timeout_uninterruptible(delta);
1906 }
1907
Tejun Heof1545152007-07-16 14:29:40 +09001908 if (rc == -EPIPE ||
Tejun Heo31daabd2007-02-02 16:50:52 +09001909 try == ARRAY_SIZE(ata_eh_reset_timeouts) - 1)
Tejun Heo936fd732007-08-06 18:36:23 +09001910 sata_down_spd_limit(link);
Tejun Heo022bdb02006-05-15 20:58:22 +09001911 if (hardreset)
1912 reset = hardreset;
1913 goto retry;
1914 }
1915
1916 if (rc == 0) {
Tejun Heo008a7892007-07-16 14:29:40 +09001917 u32 sstatus;
1918
Tejun Heo20952b62006-05-31 18:27:23 +09001919 /* After the reset, the device state is PIO 0 and the
1920 * controller state is undefined. Record the mode.
1921 */
Tejun Heo936fd732007-08-06 18:36:23 +09001922 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09001923 dev->pio_mode = XFER_PIO_0;
Tejun Heo20952b62006-05-31 18:27:23 +09001924
Tejun Heo008a7892007-07-16 14:29:40 +09001925 /* record current link speed */
Tejun Heo936fd732007-08-06 18:36:23 +09001926 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
1927 link->sata_spd = (sstatus >> 4) & 0xf;
Tejun Heo008a7892007-07-16 14:29:40 +09001928
Tejun Heo022bdb02006-05-15 20:58:22 +09001929 if (postreset)
Tejun Heocc0680a2007-08-06 18:36:23 +09001930 postreset(link, classes);
Tejun Heo022bdb02006-05-15 20:58:22 +09001931
1932 /* reset successful, schedule revalidation */
Tejun Heo955e57d2007-08-06 18:36:23 +09001933 ata_eh_done(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
Tejun Heo022bdb02006-05-15 20:58:22 +09001934 ehc->i.action |= ATA_EH_REVALIDATE;
1935 }
Tejun Heofccb6ea2007-07-16 14:29:41 +09001936 out:
1937 /* clear hotplug flag */
1938 ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
Tejun Heo022bdb02006-05-15 20:58:22 +09001939 return rc;
1940}
1941
Tejun Heo02607312007-08-06 18:36:23 +09001942static int ata_eh_revalidate_and_attach(struct ata_link *link,
Tejun Heo084fe632006-05-31 18:28:03 +09001943 struct ata_device **r_failed_dev)
Tejun Heo022bdb02006-05-15 20:58:22 +09001944{
Tejun Heo02607312007-08-06 18:36:23 +09001945 struct ata_port *ap = link->ap;
1946 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001947 struct ata_device *dev;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09001948 unsigned int new_mask = 0;
Tejun Heo084fe632006-05-31 18:28:03 +09001949 unsigned long flags;
Tejun Heof58229f2007-08-06 18:36:23 +09001950 int rc = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09001951
1952 DPRINTK("ENTER\n");
1953
Tejun Heo8c3c52a2007-03-22 22:24:19 +09001954 /* For PATA drive side cable detection to work, IDENTIFY must
1955 * be done backwards such that PDIAG- is released by the slave
1956 * device before the master device is identified.
1957 */
Tejun Heo02607312007-08-06 18:36:23 +09001958 ata_link_for_each_dev_reverse(dev, link) {
Tejun Heof58229f2007-08-06 18:36:23 +09001959 unsigned int action = ata_eh_dev_action(dev);
1960 unsigned int readid_flags = 0;
Tejun Heo47005f22006-06-19 18:27:23 +09001961
Tejun Heobff04642006-11-10 18:08:10 +09001962 if (ehc->i.flags & ATA_EHI_DID_RESET)
1963 readid_flags |= ATA_READID_POSTRESET;
1964
Tejun Heo9666f402007-05-04 21:27:47 +02001965 if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
Tejun Heo02607312007-08-06 18:36:23 +09001966 if (ata_link_offline(link)) {
Tejun Heo022bdb02006-05-15 20:58:22 +09001967 rc = -EIO;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09001968 goto err;
Tejun Heo022bdb02006-05-15 20:58:22 +09001969 }
1970
Tejun Heo02607312007-08-06 18:36:23 +09001971 ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
Tejun Heobff04642006-11-10 18:08:10 +09001972 rc = ata_dev_revalidate(dev, readid_flags);
Tejun Heo022bdb02006-05-15 20:58:22 +09001973 if (rc)
Tejun Heo8c3c52a2007-03-22 22:24:19 +09001974 goto err;
Tejun Heo022bdb02006-05-15 20:58:22 +09001975
Tejun Heo02607312007-08-06 18:36:23 +09001976 ata_eh_done(link, dev, ATA_EH_REVALIDATE);
Tejun Heo47005f22006-06-19 18:27:23 +09001977
Tejun Heobaa1e782006-11-01 18:39:27 +09001978 /* Configuration may have changed, reconfigure
1979 * transfer mode.
1980 */
1981 ehc->i.flags |= ATA_EHI_SETMODE;
1982
zhao, forrest3057ac32006-06-12 12:01:34 +08001983 /* schedule the scsi_rescan_device() here */
1984 queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
Tejun Heo084fe632006-05-31 18:28:03 +09001985 } else if (dev->class == ATA_DEV_UNKNOWN &&
1986 ehc->tries[dev->devno] &&
1987 ata_class_enabled(ehc->classes[dev->devno])) {
1988 dev->class = ehc->classes[dev->devno];
1989
Tejun Heobff04642006-11-10 18:08:10 +09001990 rc = ata_dev_read_id(dev, &dev->class, readid_flags,
1991 dev->id);
Tejun Heo8c3c52a2007-03-22 22:24:19 +09001992 switch (rc) {
1993 case 0:
Tejun Heof58229f2007-08-06 18:36:23 +09001994 new_mask |= 1 << dev->devno;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09001995 break;
1996 case -ENOENT:
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001997 /* IDENTIFY was issued to non-existent
1998 * device. No need to reset. Just
1999 * thaw and kill the device.
2000 */
2001 ata_eh_thaw_port(ap);
2002 dev->class = ATA_DEV_UNKNOWN;
Tejun Heo084fe632006-05-31 18:28:03 +09002003 break;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002004 default:
2005 dev->class = ATA_DEV_UNKNOWN;
2006 goto err;
Tejun Heo55a8e2c2006-11-10 18:08:10 +09002007 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002008 }
2009 }
2010
Tejun Heoc1c4e8d2007-04-23 02:05:53 +09002011 /* PDIAG- should have been released, ask cable type if post-reset */
2012 if ((ehc->i.flags & ATA_EHI_DID_RESET) && ap->ops->cable_detect)
2013 ap->cbl = ap->ops->cable_detect(ap);
2014
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002015 /* Configure new devices forward such that user doesn't see
2016 * device detection messages backwards.
2017 */
Tejun Heo02607312007-08-06 18:36:23 +09002018 ata_link_for_each_dev(dev, link) {
Tejun Heof58229f2007-08-06 18:36:23 +09002019 if (!(new_mask & (1 << dev->devno)))
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002020 continue;
2021
2022 ehc->i.flags |= ATA_EHI_PRINTINFO;
2023 rc = ata_dev_configure(dev);
2024 ehc->i.flags &= ~ATA_EHI_PRINTINFO;
2025 if (rc)
2026 goto err;
2027
2028 spin_lock_irqsave(ap->lock, flags);
2029 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
2030 spin_unlock_irqrestore(ap->lock, flags);
2031
2032 /* new device discovered, configure xfermode */
2033 ehc->i.flags |= ATA_EHI_SETMODE;
2034 }
2035
2036 return 0;
2037
2038 err:
2039 *r_failed_dev = dev;
2040 DPRINTK("EXIT rc=%d\n", rc);
Tejun Heo022bdb02006-05-15 20:58:22 +09002041 return rc;
2042}
2043
Tejun Heo02607312007-08-06 18:36:23 +09002044static int ata_link_nr_enabled(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09002045{
Tejun Heof58229f2007-08-06 18:36:23 +09002046 struct ata_device *dev;
2047 int cnt = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09002048
Tejun Heo02607312007-08-06 18:36:23 +09002049 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09002050 if (ata_dev_enabled(dev))
Tejun Heo022bdb02006-05-15 20:58:22 +09002051 cnt++;
2052 return cnt;
2053}
2054
Tejun Heo02607312007-08-06 18:36:23 +09002055static int ata_link_nr_vacant(struct ata_link *link)
Tejun Heo084fe632006-05-31 18:28:03 +09002056{
Tejun Heof58229f2007-08-06 18:36:23 +09002057 struct ata_device *dev;
2058 int cnt = 0;
Tejun Heo084fe632006-05-31 18:28:03 +09002059
Tejun Heo02607312007-08-06 18:36:23 +09002060 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09002061 if (dev->class == ATA_DEV_UNKNOWN)
Tejun Heo084fe632006-05-31 18:28:03 +09002062 cnt++;
2063 return cnt;
2064}
2065
Tejun Heo02607312007-08-06 18:36:23 +09002066static int ata_eh_skip_recovery(struct ata_link *link)
Tejun Heo084fe632006-05-31 18:28:03 +09002067{
Tejun Heo02607312007-08-06 18:36:23 +09002068 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heof58229f2007-08-06 18:36:23 +09002069 struct ata_device *dev;
Tejun Heo084fe632006-05-31 18:28:03 +09002070
Tejun Heo7c8c2cf2006-07-10 23:18:23 +09002071 /* thaw frozen port, resume link and recover failed devices */
Tejun Heo02607312007-08-06 18:36:23 +09002072 if ((link->ap->pflags & ATA_PFLAG_FROZEN) ||
2073 (ehc->i.flags & ATA_EHI_RESUME_LINK) || ata_link_nr_enabled(link))
Tejun Heo084fe632006-05-31 18:28:03 +09002074 return 0;
2075
2076 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
Tejun Heo02607312007-08-06 18:36:23 +09002077 ata_link_for_each_dev(dev, link) {
Tejun Heo084fe632006-05-31 18:28:03 +09002078 if (dev->class == ATA_DEV_UNKNOWN &&
2079 ehc->classes[dev->devno] != ATA_DEV_NONE)
2080 return 0;
2081 }
2082
2083 return 1;
2084}
2085
Tejun Heofee7ca72007-07-01 19:05:58 +09002086static void ata_eh_handle_dev_fail(struct ata_device *dev, int err)
2087{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002088 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heofee7ca72007-07-01 19:05:58 +09002089
2090 ehc->tries[dev->devno]--;
2091
2092 switch (err) {
2093 case -ENODEV:
2094 /* device missing or wrong IDENTIFY data, schedule probing */
2095 ehc->i.probe_mask |= (1 << dev->devno);
2096 case -EINVAL:
2097 /* give it just one more chance */
2098 ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
2099 case -EIO:
2100 if (ehc->tries[dev->devno] == 1) {
2101 /* This is the last chance, better to slow
2102 * down than lose it.
2103 */
Tejun Heo936fd732007-08-06 18:36:23 +09002104 sata_down_spd_limit(dev->link);
Tejun Heofee7ca72007-07-01 19:05:58 +09002105 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2106 }
2107 }
2108
2109 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2110 /* disable device if it has used up all its chances */
2111 ata_dev_disable(dev);
2112
2113 /* detach if offline */
Tejun Heo936fd732007-08-06 18:36:23 +09002114 if (ata_link_offline(dev->link))
Tejun Heofee7ca72007-07-01 19:05:58 +09002115 ata_eh_detach_dev(dev);
2116
2117 /* probe if requested */
2118 if ((ehc->i.probe_mask & (1 << dev->devno)) &&
2119 !(ehc->did_probe_mask & (1 << dev->devno))) {
2120 ata_eh_detach_dev(dev);
2121 ata_dev_init(dev);
2122
2123 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2124 ehc->did_probe_mask |= (1 << dev->devno);
2125 ehc->i.action |= ATA_EH_SOFTRESET;
2126 }
2127 } else {
2128 /* soft didn't work? be haaaaard */
2129 if (ehc->i.flags & ATA_EHI_DID_RESET)
2130 ehc->i.action |= ATA_EH_HARDRESET;
2131 else
2132 ehc->i.action |= ATA_EH_SOFTRESET;
2133 }
2134}
2135
Tejun Heo022bdb02006-05-15 20:58:22 +09002136/**
2137 * ata_eh_recover - recover host port after error
2138 * @ap: host port to recover
Tejun Heof5914a42006-05-31 18:27:48 +09002139 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09002140 * @softreset: softreset method (can be NULL)
2141 * @hardreset: hardreset method (can be NULL)
2142 * @postreset: postreset method (can be NULL)
2143 *
2144 * This is the alpha and omega, eum and yang, heart and soul of
2145 * libata exception handling. On entry, actions required to
Tejun Heo084fe632006-05-31 18:28:03 +09002146 * recover the port and hotplug requests are recorded in
2147 * eh_context. This function executes all the operations with
2148 * appropriate retrials and fallbacks to resurrect failed
2149 * devices, detach goners and greet newcomers.
Tejun Heo022bdb02006-05-15 20:58:22 +09002150 *
2151 * LOCKING:
2152 * Kernel thread context (may sleep).
2153 *
2154 * RETURNS:
2155 * 0 on success, -errno on failure.
2156 */
Tejun Heof5914a42006-05-31 18:27:48 +09002157static int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
2158 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
Tejun Heo022bdb02006-05-15 20:58:22 +09002159 ata_postreset_fn_t postreset)
2160{
Tejun Heo02607312007-08-06 18:36:23 +09002161 struct ata_link *link = &ap->link;
2162 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09002163 struct ata_device *dev;
Tejun Heof58229f2007-08-06 18:36:23 +09002164 int rc;
Tejun Heo022bdb02006-05-15 20:58:22 +09002165
2166 DPRINTK("ENTER\n");
2167
2168 /* prep for recovery */
Tejun Heo02607312007-08-06 18:36:23 +09002169 ata_link_for_each_dev(dev, link) {
Tejun Heo022bdb02006-05-15 20:58:22 +09002170 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
Tejun Heo084fe632006-05-31 18:28:03 +09002171
Tejun Heo79a55b72007-01-18 17:22:18 +09002172 /* collect port action mask recorded in dev actions */
Tejun Heof58229f2007-08-06 18:36:23 +09002173 ehc->i.action |=
2174 ehc->i.dev_action[dev->devno] & ~ATA_EH_PERDEV_MASK;
2175 ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
Tejun Heo79a55b72007-01-18 17:22:18 +09002176
Tejun Heo084fe632006-05-31 18:28:03 +09002177 /* process hotplug request */
2178 if (dev->flags & ATA_DFLAG_DETACH)
2179 ata_eh_detach_dev(dev);
2180
2181 if (!ata_dev_enabled(dev) &&
2182 ((ehc->i.probe_mask & (1 << dev->devno)) &&
2183 !(ehc->did_probe_mask & (1 << dev->devno)))) {
2184 ata_eh_detach_dev(dev);
2185 ata_dev_init(dev);
2186 ehc->did_probe_mask |= (1 << dev->devno);
2187 ehc->i.action |= ATA_EH_SOFTRESET;
2188 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002189 }
2190
2191 retry:
Tejun Heo022bdb02006-05-15 20:58:22 +09002192 rc = 0;
2193
Tejun Heoaeb2ecd2006-06-12 14:11:43 +09002194 /* if UNLOADING, finish immediately */
Tejun Heob51e9e52006-06-29 01:29:30 +09002195 if (ap->pflags & ATA_PFLAG_UNLOADING)
Tejun Heoaeb2ecd2006-06-12 14:11:43 +09002196 goto out;
2197
Tejun Heo022bdb02006-05-15 20:58:22 +09002198 /* skip EH if possible. */
Tejun Heo02607312007-08-06 18:36:23 +09002199 if (ata_eh_skip_recovery(link))
Tejun Heo022bdb02006-05-15 20:58:22 +09002200 ehc->i.action = 0;
2201
Tejun Heo02607312007-08-06 18:36:23 +09002202 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09002203 ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
Tejun Heo084fe632006-05-31 18:28:03 +09002204
Tejun Heo022bdb02006-05-15 20:58:22 +09002205 /* reset */
2206 if (ehc->i.action & ATA_EH_RESET_MASK) {
2207 ata_eh_freeze_port(ap);
2208
Tejun Heo02607312007-08-06 18:36:23 +09002209 rc = ata_eh_reset(link, ata_link_nr_vacant(link), prereset,
Tejun Heo084fe632006-05-31 18:28:03 +09002210 softreset, hardreset, postreset);
Tejun Heo022bdb02006-05-15 20:58:22 +09002211 if (rc) {
Tejun Heo02607312007-08-06 18:36:23 +09002212 ata_link_printk(link, KERN_ERR,
Tejun Heo022bdb02006-05-15 20:58:22 +09002213 "reset failed, giving up\n");
2214 goto out;
2215 }
2216
2217 ata_eh_thaw_port(ap);
2218 }
2219
Tejun Heo084fe632006-05-31 18:28:03 +09002220 /* revalidate existing devices and attach new ones */
Tejun Heo02607312007-08-06 18:36:23 +09002221 rc = ata_eh_revalidate_and_attach(link, &dev);
Tejun Heo022bdb02006-05-15 20:58:22 +09002222 if (rc)
2223 goto dev_fail;
2224
Tejun Heobaa1e782006-11-01 18:39:27 +09002225 /* configure transfer mode if necessary */
2226 if (ehc->i.flags & ATA_EHI_SETMODE) {
Tejun Heo02607312007-08-06 18:36:23 +09002227 rc = ata_set_mode(link, &dev);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002228 if (rc)
Tejun Heo022bdb02006-05-15 20:58:22 +09002229 goto dev_fail;
Tejun Heobaa1e782006-11-01 18:39:27 +09002230 ehc->i.flags &= ~ATA_EHI_SETMODE;
Tejun Heo022bdb02006-05-15 20:58:22 +09002231 }
2232
2233 goto out;
2234
2235 dev_fail:
Tejun Heofee7ca72007-07-01 19:05:58 +09002236 ata_eh_handle_dev_fail(dev, rc);
Tejun Heo022bdb02006-05-15 20:58:22 +09002237
Tejun Heo02607312007-08-06 18:36:23 +09002238 if (ata_link_nr_enabled(link)) {
2239 ata_link_printk(link, KERN_WARNING, "failed to recover some "
Tejun Heo022bdb02006-05-15 20:58:22 +09002240 "devices, retrying in 5 secs\n");
2241 ssleep(5);
2242 } else {
2243 /* no device left, repeat fast */
2244 msleep(500);
2245 }
2246
2247 goto retry;
2248
2249 out:
2250 if (rc) {
Tejun Heo02607312007-08-06 18:36:23 +09002251 ata_link_for_each_dev(dev, link);
Tejun Heof58229f2007-08-06 18:36:23 +09002252 ata_dev_disable(dev);
Tejun Heo022bdb02006-05-15 20:58:22 +09002253 }
2254
2255 DPRINTK("EXIT, rc=%d\n", rc);
2256 return rc;
2257}
2258
2259/**
2260 * ata_eh_finish - finish up EH
2261 * @ap: host port to finish EH for
2262 *
2263 * Recovery is complete. Clean up EH states and retry or finish
2264 * failed qcs.
2265 *
2266 * LOCKING:
2267 * None.
2268 */
2269static void ata_eh_finish(struct ata_port *ap)
2270{
2271 int tag;
2272
2273 /* retry or finish qcs */
2274 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2275 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2276
2277 if (!(qc->flags & ATA_QCFLAG_FAILED))
2278 continue;
2279
2280 if (qc->err_mask) {
2281 /* FIXME: Once EH migration is complete,
2282 * generate sense data in this function,
2283 * considering both err_mask and tf.
2284 */
2285 if (qc->err_mask & AC_ERR_INVALID)
2286 ata_eh_qc_complete(qc);
2287 else
2288 ata_eh_qc_retry(qc);
2289 } else {
2290 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2291 ata_eh_qc_complete(qc);
2292 } else {
2293 /* feed zero TF to sense generation */
2294 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2295 ata_eh_qc_retry(qc);
2296 }
2297 }
2298 }
2299}
2300
2301/**
2302 * ata_do_eh - do standard error handling
2303 * @ap: host port to handle error for
Tejun Heof5914a42006-05-31 18:27:48 +09002304 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09002305 * @softreset: softreset method (can be NULL)
2306 * @hardreset: hardreset method (can be NULL)
2307 * @postreset: postreset method (can be NULL)
2308 *
2309 * Perform standard error handling sequence.
2310 *
2311 * LOCKING:
2312 * Kernel thread context (may sleep).
2313 */
Tejun Heof5914a42006-05-31 18:27:48 +09002314void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2315 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2316 ata_postreset_fn_t postreset)
Tejun Heo022bdb02006-05-15 20:58:22 +09002317{
Tejun Heo02607312007-08-06 18:36:23 +09002318 ata_eh_autopsy(&ap->link);
2319 ata_eh_report(&ap->link);
Tejun Heof5914a42006-05-31 18:27:48 +09002320 ata_eh_recover(ap, prereset, softreset, hardreset, postreset);
Tejun Heo022bdb02006-05-15 20:58:22 +09002321 ata_eh_finish(ap);
2322}
Tejun Heo500530f2006-07-03 16:07:27 +09002323
Tejun Heo6ffa01d2007-03-02 17:32:47 +09002324#ifdef CONFIG_PM
Tejun Heo500530f2006-07-03 16:07:27 +09002325/**
2326 * ata_eh_handle_port_suspend - perform port suspend operation
2327 * @ap: port to suspend
2328 *
2329 * Suspend @ap.
2330 *
2331 * LOCKING:
2332 * Kernel thread context (may sleep).
2333 */
2334static void ata_eh_handle_port_suspend(struct ata_port *ap)
2335{
2336 unsigned long flags;
2337 int rc = 0;
2338
2339 /* are we suspending? */
2340 spin_lock_irqsave(ap->lock, flags);
2341 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2342 ap->pm_mesg.event == PM_EVENT_ON) {
2343 spin_unlock_irqrestore(ap->lock, flags);
2344 return;
2345 }
2346 spin_unlock_irqrestore(ap->lock, flags);
2347
2348 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
2349
Tejun Heo64578a32007-05-15 03:28:16 +09002350 /* tell ACPI we're suspending */
2351 rc = ata_acpi_on_suspend(ap);
2352 if (rc)
2353 goto out;
2354
Tejun Heo500530f2006-07-03 16:07:27 +09002355 /* suspend */
2356 ata_eh_freeze_port(ap);
2357
2358 if (ap->ops->port_suspend)
2359 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
2360
Tejun Heo64578a32007-05-15 03:28:16 +09002361 out:
Tejun Heo500530f2006-07-03 16:07:27 +09002362 /* report result */
2363 spin_lock_irqsave(ap->lock, flags);
2364
2365 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
2366 if (rc == 0)
2367 ap->pflags |= ATA_PFLAG_SUSPENDED;
Tejun Heo64578a32007-05-15 03:28:16 +09002368 else if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heo500530f2006-07-03 16:07:27 +09002369 ata_port_schedule_eh(ap);
2370
2371 if (ap->pm_result) {
2372 *ap->pm_result = rc;
2373 ap->pm_result = NULL;
2374 }
2375
2376 spin_unlock_irqrestore(ap->lock, flags);
2377
2378 return;
2379}
2380
2381/**
2382 * ata_eh_handle_port_resume - perform port resume operation
2383 * @ap: port to resume
2384 *
2385 * Resume @ap.
2386 *
Tejun Heo500530f2006-07-03 16:07:27 +09002387 * LOCKING:
2388 * Kernel thread context (may sleep).
2389 */
2390static void ata_eh_handle_port_resume(struct ata_port *ap)
2391{
Tejun Heo500530f2006-07-03 16:07:27 +09002392 unsigned long flags;
Tejun Heo9666f402007-05-04 21:27:47 +02002393 int rc = 0;
Tejun Heo500530f2006-07-03 16:07:27 +09002394
2395 /* are we resuming? */
2396 spin_lock_irqsave(ap->lock, flags);
2397 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2398 ap->pm_mesg.event != PM_EVENT_ON) {
2399 spin_unlock_irqrestore(ap->lock, flags);
2400 return;
2401 }
2402 spin_unlock_irqrestore(ap->lock, flags);
2403
Tejun Heo9666f402007-05-04 21:27:47 +02002404 WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
Tejun Heo500530f2006-07-03 16:07:27 +09002405
2406 if (ap->ops->port_resume)
2407 rc = ap->ops->port_resume(ap);
2408
Tejun Heo67465442007-05-15 03:28:16 +09002409 /* tell ACPI that we're resuming */
2410 ata_acpi_on_resume(ap);
2411
Tejun Heo9666f402007-05-04 21:27:47 +02002412 /* report result */
Tejun Heo500530f2006-07-03 16:07:27 +09002413 spin_lock_irqsave(ap->lock, flags);
2414 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
2415 if (ap->pm_result) {
2416 *ap->pm_result = rc;
2417 ap->pm_result = NULL;
2418 }
2419 spin_unlock_irqrestore(ap->lock, flags);
2420}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09002421#endif /* CONFIG_PM */