blob: e2681f56ed443025afc61b7b54ddec302eecd5f5 [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 Heoaf181c22006-06-24 20:30:18 +0900203static void ata_eh_clear_action(struct ata_device *dev,
204 struct ata_eh_info *ehi, unsigned int action)
205{
206 int i;
207
208 if (!dev) {
209 ehi->action &= ~action;
210 for (i = 0; i < ATA_MAX_DEVICES; i++)
211 ehi->dev_action[i] &= ~action;
212 } 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) {
218 for (i = 0; i < ATA_MAX_DEVICES; i++)
219 ehi->dev_action[i] |= ehi->action & action;
220 ehi->action &= ~action;
221 }
222
223 /* turn off the specified per-dev action */
224 ehi->dev_action[dev->devno] &= ~action;
225 }
226}
227
Tejun Heoece1d632006-04-02 18:51:53 +0900228/**
229 * ata_scsi_timed_out - SCSI layer time out callback
230 * @cmd: timed out SCSI command
231 *
232 * Handles SCSI layer timeout. We race with normal completion of
233 * the qc for @cmd. If the qc is already gone, we lose and let
234 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
235 * timed out and EH should be invoked. Prevent ata_qc_complete()
236 * from finishing it by setting EH_SCHEDULED and return
237 * EH_NOT_HANDLED.
238 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900239 * TODO: kill this function once old EH is gone.
240 *
Tejun Heoece1d632006-04-02 18:51:53 +0900241 * LOCKING:
242 * Called from timer context
243 *
244 * RETURNS:
245 * EH_HANDLED or EH_NOT_HANDLED
246 */
247enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
248{
249 struct Scsi_Host *host = cmd->device->host;
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400250 struct ata_port *ap = ata_shost_to_port(host);
Tejun Heoece1d632006-04-02 18:51:53 +0900251 unsigned long flags;
252 struct ata_queued_cmd *qc;
Tejun Heoad9e2762006-05-15 20:58:12 +0900253 enum scsi_eh_timer_return ret;
Tejun Heoece1d632006-04-02 18:51:53 +0900254
255 DPRINTK("ENTER\n");
256
Tejun Heoad9e2762006-05-15 20:58:12 +0900257 if (ap->ops->error_handler) {
258 ret = EH_NOT_HANDLED;
259 goto out;
260 }
261
262 ret = EH_HANDLED;
Jeff Garzikba6a1302006-06-22 23:46:10 -0400263 spin_lock_irqsave(ap->lock, flags);
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900264 qc = ata_qc_from_tag(ap, ap->link.active_tag);
Tejun Heoece1d632006-04-02 18:51:53 +0900265 if (qc) {
266 WARN_ON(qc->scsicmd != cmd);
267 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
268 qc->err_mask |= AC_ERR_TIMEOUT;
269 ret = EH_NOT_HANDLED;
270 }
Jeff Garzikba6a1302006-06-22 23:46:10 -0400271 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900272
Tejun Heoad9e2762006-05-15 20:58:12 +0900273 out:
Tejun Heoece1d632006-04-02 18:51:53 +0900274 DPRINTK("EXIT, ret=%d\n", ret);
275 return ret;
276}
277
278/**
279 * ata_scsi_error - SCSI layer error handler callback
280 * @host: SCSI host on which error occurred
281 *
282 * Handles SCSI-layer-thrown error events.
283 *
284 * LOCKING:
285 * Inherited from SCSI layer (none, can sleep)
286 *
287 * RETURNS:
288 * Zero.
289 */
Jeff Garzik381544b2006-04-11 13:04:39 -0400290void ata_scsi_error(struct Scsi_Host *host)
Tejun Heoece1d632006-04-02 18:51:53 +0900291{
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400292 struct ata_port *ap = ata_shost_to_port(host);
Tejun Heoad9e2762006-05-15 20:58:12 +0900293 int i, repeat_cnt = ATA_EH_MAX_REPEAT;
294 unsigned long flags;
Tejun Heoece1d632006-04-02 18:51:53 +0900295
296 DPRINTK("ENTER\n");
297
Tejun Heoad9e2762006-05-15 20:58:12 +0900298 /* synchronize with port task */
Tejun Heoece1d632006-04-02 18:51:53 +0900299 ata_port_flush_task(ap);
300
Jeff Garzikcca39742006-08-24 03:19:22 -0400301 /* synchronize with host lock and sort out timeouts */
Tejun Heoece1d632006-04-02 18:51:53 +0900302
Tejun Heoad9e2762006-05-15 20:58:12 +0900303 /* For new EH, all qcs are finished in one of three ways -
304 * normal completion, error completion, and SCSI timeout.
305 * Both cmpletions can race against SCSI timeout. When normal
306 * completion wins, the qc never reaches EH. When error
307 * completion wins, the qc has ATA_QCFLAG_FAILED set.
308 *
309 * When SCSI timeout wins, things are a bit more complex.
310 * Normal or error completion can occur after the timeout but
311 * before this point. In such cases, both types of
312 * completions are honored. A scmd is determined to have
313 * timed out iff its associated qc is active and not failed.
314 */
315 if (ap->ops->error_handler) {
316 struct scsi_cmnd *scmd, *tmp;
317 int nr_timedout = 0;
Tejun Heoece1d632006-04-02 18:51:53 +0900318
Tejun Heoe30349d2006-07-03 03:02:15 +0900319 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900320
321 list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
322 struct ata_queued_cmd *qc;
323
324 for (i = 0; i < ATA_MAX_QUEUE; i++) {
325 qc = __ata_qc_from_tag(ap, i);
326 if (qc->flags & ATA_QCFLAG_ACTIVE &&
327 qc->scsicmd == scmd)
328 break;
329 }
330
331 if (i < ATA_MAX_QUEUE) {
332 /* the scmd has an associated qc */
333 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
334 /* which hasn't failed yet, timeout */
335 qc->err_mask |= AC_ERR_TIMEOUT;
336 qc->flags |= ATA_QCFLAG_FAILED;
337 nr_timedout++;
338 }
339 } else {
340 /* Normal completion occurred after
341 * SCSI timeout but before this point.
342 * Successfully complete it.
343 */
344 scmd->retries = scmd->allowed;
345 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
346 }
347 }
348
349 /* If we have timed out qcs. They belong to EH from
350 * this point but the state of the controller is
351 * unknown. Freeze the port to make sure the IRQ
352 * handler doesn't diddle with those qcs. This must
353 * be done atomically w.r.t. setting QCFLAG_FAILED.
354 */
355 if (nr_timedout)
356 __ata_port_freeze(ap);
357
Tejun Heoe30349d2006-07-03 03:02:15 +0900358 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900359 } else
Tejun Heoe30349d2006-07-03 03:02:15 +0900360 spin_unlock_wait(ap->lock);
Tejun Heoad9e2762006-05-15 20:58:12 +0900361
362 repeat:
363 /* invoke error handler */
364 if (ap->ops->error_handler) {
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900365 /* kill fast drain timer */
366 del_timer_sync(&ap->fastdrain_timer);
367
Tejun Heo500530f2006-07-03 16:07:27 +0900368 /* process port resume request */
369 ata_eh_handle_port_resume(ap);
370
Tejun Heof3e81b12006-05-15 20:58:21 +0900371 /* fetch & clear EH info */
Tejun Heoe30349d2006-07-03 03:02:15 +0900372 spin_lock_irqsave(ap->lock, flags);
Tejun Heof3e81b12006-05-15 20:58:21 +0900373
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900374 memset(&ap->link.eh_context, 0, sizeof(ap->link.eh_context));
375 ap->link.eh_context.i = ap->link.eh_info;
376 memset(&ap->link.eh_info, 0, sizeof(ap->link.eh_info));
Tejun Heof3e81b12006-05-15 20:58:21 +0900377
Tejun Heob51e9e52006-06-29 01:29:30 +0900378 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
379 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
Tejun Heof3e81b12006-05-15 20:58:21 +0900380
Tejun Heoe30349d2006-07-03 03:02:15 +0900381 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900382
Tejun Heo500530f2006-07-03 16:07:27 +0900383 /* invoke EH, skip if unloading or suspended */
384 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
Tejun Heo720ba122006-05-31 18:28:13 +0900385 ap->ops->error_handler(ap);
386 else
387 ata_eh_finish(ap);
Tejun Heoad9e2762006-05-15 20:58:12 +0900388
Tejun Heo500530f2006-07-03 16:07:27 +0900389 /* process port suspend request */
390 ata_eh_handle_port_suspend(ap);
391
Tejun Heoad9e2762006-05-15 20:58:12 +0900392 /* Exception might have happend after ->error_handler
393 * recovered the port but before this point. Repeat
394 * EH in such case.
395 */
Tejun Heoe30349d2006-07-03 03:02:15 +0900396 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900397
Tejun Heob51e9e52006-06-29 01:29:30 +0900398 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
Tejun Heoad9e2762006-05-15 20:58:12 +0900399 if (--repeat_cnt) {
400 ata_port_printk(ap, KERN_INFO,
401 "EH pending after completion, "
402 "repeating EH (cnt=%d)\n", repeat_cnt);
Tejun Heoe30349d2006-07-03 03:02:15 +0900403 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900404 goto repeat;
405 }
406 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
407 "tries, giving up\n", ATA_EH_MAX_REPEAT);
Tejun Heo914616a2007-06-25 21:47:11 +0900408 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
Tejun Heoad9e2762006-05-15 20:58:12 +0900409 }
410
Tejun Heof3e81b12006-05-15 20:58:21 +0900411 /* this run is complete, make sure EH info is clear */
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900412 memset(&ap->link.eh_info, 0, sizeof(ap->link.eh_info));
Tejun Heof3e81b12006-05-15 20:58:21 +0900413
Tejun Heoe30349d2006-07-03 03:02:15 +0900414 /* Clear host_eh_scheduled while holding ap->lock such
Tejun Heoad9e2762006-05-15 20:58:12 +0900415 * that if exception occurs after this point but
416 * before EH completion, SCSI midlayer will
417 * re-initiate EH.
418 */
419 host->host_eh_scheduled = 0;
420
Tejun Heoe30349d2006-07-03 03:02:15 +0900421 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900422 } else {
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900423 WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
Tejun Heoad9e2762006-05-15 20:58:12 +0900424 ap->ops->eng_timeout(ap);
425 }
426
427 /* finish or retry handled scmd's and clean up */
Tejun Heoece1d632006-04-02 18:51:53 +0900428 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
429
430 scsi_eh_flush_done_q(&ap->eh_done_q);
431
Tejun Heoad9e2762006-05-15 20:58:12 +0900432 /* clean up */
Tejun Heoe30349d2006-07-03 03:02:15 +0900433 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900434
Tejun Heo1cdaf532006-07-03 16:07:26 +0900435 if (ap->pflags & ATA_PFLAG_LOADING)
Tejun Heob51e9e52006-06-29 01:29:30 +0900436 ap->pflags &= ~ATA_PFLAG_LOADING;
Tejun Heo1cdaf532006-07-03 16:07:26 +0900437 else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
David Howells52bad642006-11-22 14:54:01 +0000438 queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0);
Tejun Heo1cdaf532006-07-03 16:07:26 +0900439
440 if (ap->pflags & ATA_PFLAG_RECOVERED)
441 ata_port_printk(ap, KERN_INFO, "EH complete\n");
Tejun Heo580b21022006-05-31 18:28:05 +0900442
Tejun Heob51e9e52006-06-29 01:29:30 +0900443 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
Tejun Heoad9e2762006-05-15 20:58:12 +0900444
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900445 /* tell wait_eh that we're done */
Tejun Heob51e9e52006-06-29 01:29:30 +0900446 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900447 wake_up_all(&ap->eh_wait_q);
448
Tejun Heoe30349d2006-07-03 03:02:15 +0900449 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900450
Tejun Heoece1d632006-04-02 18:51:53 +0900451 DPRINTK("EXIT\n");
Tejun Heoece1d632006-04-02 18:51:53 +0900452}
453
454/**
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900455 * ata_port_wait_eh - Wait for the currently pending EH to complete
456 * @ap: Port to wait EH for
457 *
458 * Wait until the currently pending EH is complete.
459 *
460 * LOCKING:
461 * Kernel thread context (may sleep).
462 */
463void ata_port_wait_eh(struct ata_port *ap)
464{
465 unsigned long flags;
466 DEFINE_WAIT(wait);
467
468 retry:
Jeff Garzikba6a1302006-06-22 23:46:10 -0400469 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900470
Tejun Heob51e9e52006-06-29 01:29:30 +0900471 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900472 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
Jeff Garzikba6a1302006-06-22 23:46:10 -0400473 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900474 schedule();
Jeff Garzikba6a1302006-06-22 23:46:10 -0400475 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900476 }
Tejun Heo0a1b6222006-06-11 11:01:38 +0900477 finish_wait(&ap->eh_wait_q, &wait);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900478
Jeff Garzikba6a1302006-06-22 23:46:10 -0400479 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900480
481 /* make sure SCSI EH is complete */
Jeff Garzikcca39742006-08-24 03:19:22 -0400482 if (scsi_host_in_recovery(ap->scsi_host)) {
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900483 msleep(10);
484 goto retry;
485 }
486}
487
488/**
Tejun Heoece1d632006-04-02 18:51:53 +0900489 * ata_qc_timeout - Handle timeout of queued command
490 * @qc: Command that timed out
491 *
492 * Some part of the kernel (currently, only the SCSI layer)
493 * has noticed that the active command on port @ap has not
494 * completed after a specified length of time. Handle this
495 * condition by disabling DMA (if necessary) and completing
496 * transactions, with error if necessary.
497 *
498 * This also handles the case of the "lost interrupt", where
499 * for some reason (possibly hardware bug, possibly driver bug)
500 * an interrupt was not delivered to the driver, even though the
501 * transaction completed successfully.
502 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900503 * TODO: kill this function once old EH is gone.
504 *
Tejun Heoece1d632006-04-02 18:51:53 +0900505 * LOCKING:
506 * Inherited from SCSI layer (none, can sleep)
507 */
508static void ata_qc_timeout(struct ata_queued_cmd *qc)
509{
510 struct ata_port *ap = qc->ap;
Tejun Heoece1d632006-04-02 18:51:53 +0900511 u8 host_stat = 0, drv_stat;
512 unsigned long flags;
513
514 DPRINTK("ENTER\n");
515
516 ap->hsm_task_state = HSM_ST_IDLE;
517
Jeff Garzikba6a1302006-06-22 23:46:10 -0400518 spin_lock_irqsave(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900519
520 switch (qc->tf.protocol) {
521
522 case ATA_PROT_DMA:
523 case ATA_PROT_ATAPI_DMA:
524 host_stat = ap->ops->bmdma_status(ap);
525
526 /* before we do anything else, clear DMA-Start bit */
527 ap->ops->bmdma_stop(qc);
528
529 /* fall through */
530
531 default:
532 ata_altstatus(ap);
533 drv_stat = ata_chk_status(ap);
534
535 /* ack bmdma irq events */
536 ap->ops->irq_clear(ap);
537
Tejun Heof15a1da2006-05-15 20:57:56 +0900538 ata_dev_printk(qc->dev, KERN_ERR, "command 0x%x timeout, "
539 "stat 0x%x host_stat 0x%x\n",
540 qc->tf.command, drv_stat, host_stat);
Tejun Heoece1d632006-04-02 18:51:53 +0900541
542 /* complete taskfile transaction */
Jeff Garzikc13b56a2006-04-02 10:34:24 -0400543 qc->err_mask |= AC_ERR_TIMEOUT;
Tejun Heoece1d632006-04-02 18:51:53 +0900544 break;
545 }
546
Jeff Garzikba6a1302006-06-22 23:46:10 -0400547 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900548
549 ata_eh_qc_complete(qc);
550
551 DPRINTK("EXIT\n");
552}
553
554/**
555 * ata_eng_timeout - Handle timeout of queued command
556 * @ap: Port on which timed-out command is active
557 *
558 * Some part of the kernel (currently, only the SCSI layer)
559 * has noticed that the active command on port @ap has not
560 * completed after a specified length of time. Handle this
561 * condition by disabling DMA (if necessary) and completing
562 * transactions, with error if necessary.
563 *
564 * This also handles the case of the "lost interrupt", where
565 * for some reason (possibly hardware bug, possibly driver bug)
566 * an interrupt was not delivered to the driver, even though the
567 * transaction completed successfully.
568 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900569 * TODO: kill this function once old EH is gone.
570 *
Tejun Heoece1d632006-04-02 18:51:53 +0900571 * LOCKING:
572 * Inherited from SCSI layer (none, can sleep)
573 */
574void ata_eng_timeout(struct ata_port *ap)
575{
576 DPRINTK("ENTER\n");
577
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900578 ata_qc_timeout(ata_qc_from_tag(ap, ap->link.active_tag));
Tejun Heoece1d632006-04-02 18:51:53 +0900579
580 DPRINTK("EXIT\n");
581}
582
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900583static int ata_eh_nr_in_flight(struct ata_port *ap)
584{
585 unsigned int tag;
586 int nr = 0;
587
588 /* count only non-internal commands */
589 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++)
590 if (ata_qc_from_tag(ap, tag))
591 nr++;
592
593 return nr;
594}
595
596void ata_eh_fastdrain_timerfn(unsigned long arg)
597{
598 struct ata_port *ap = (void *)arg;
599 unsigned long flags;
600 int cnt;
601
602 spin_lock_irqsave(ap->lock, flags);
603
604 cnt = ata_eh_nr_in_flight(ap);
605
606 /* are we done? */
607 if (!cnt)
608 goto out_unlock;
609
610 if (cnt == ap->fastdrain_cnt) {
611 unsigned int tag;
612
613 /* No progress during the last interval, tag all
614 * in-flight qcs as timed out and freeze the port.
615 */
616 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) {
617 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
618 if (qc)
619 qc->err_mask |= AC_ERR_TIMEOUT;
620 }
621
622 ata_port_freeze(ap);
623 } else {
624 /* some qcs have finished, give it another chance */
625 ap->fastdrain_cnt = cnt;
626 ap->fastdrain_timer.expires =
627 jiffies + ATA_EH_FASTDRAIN_INTERVAL;
628 add_timer(&ap->fastdrain_timer);
629 }
630
631 out_unlock:
632 spin_unlock_irqrestore(ap->lock, flags);
633}
634
635/**
636 * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
637 * @ap: target ATA port
638 * @fastdrain: activate fast drain
639 *
640 * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
641 * is non-zero and EH wasn't pending before. Fast drain ensures
642 * that EH kicks in in timely manner.
643 *
644 * LOCKING:
645 * spin_lock_irqsave(host lock)
646 */
647static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
648{
649 int cnt;
650
651 /* already scheduled? */
652 if (ap->pflags & ATA_PFLAG_EH_PENDING)
653 return;
654
655 ap->pflags |= ATA_PFLAG_EH_PENDING;
656
657 if (!fastdrain)
658 return;
659
660 /* do we have in-flight qcs? */
661 cnt = ata_eh_nr_in_flight(ap);
662 if (!cnt)
663 return;
664
665 /* activate fast drain */
666 ap->fastdrain_cnt = cnt;
667 ap->fastdrain_timer.expires = jiffies + ATA_EH_FASTDRAIN_INTERVAL;
668 add_timer(&ap->fastdrain_timer);
669}
670
Tejun Heof686bcb2006-05-15 20:58:05 +0900671/**
672 * ata_qc_schedule_eh - schedule qc for error handling
673 * @qc: command to schedule error handling for
674 *
675 * Schedule error handling for @qc. EH will kick in as soon as
676 * other commands are drained.
677 *
678 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400679 * spin_lock_irqsave(host lock)
Tejun Heof686bcb2006-05-15 20:58:05 +0900680 */
681void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
682{
683 struct ata_port *ap = qc->ap;
684
685 WARN_ON(!ap->ops->error_handler);
686
687 qc->flags |= ATA_QCFLAG_FAILED;
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900688 ata_eh_set_pending(ap, 1);
Tejun Heof686bcb2006-05-15 20:58:05 +0900689
690 /* The following will fail if timeout has already expired.
691 * ata_scsi_error() takes care of such scmds on EH entry.
692 * Note that ATA_QCFLAG_FAILED is unconditionally set after
693 * this function completes.
694 */
695 scsi_req_abort_cmd(qc->scsicmd);
696}
697
Tejun Heo7b70fc02006-05-15 20:58:07 +0900698/**
699 * ata_port_schedule_eh - schedule error handling without a qc
700 * @ap: ATA port to schedule EH for
701 *
702 * Schedule error handling for @ap. EH will kick in as soon as
703 * all commands are drained.
704 *
705 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400706 * spin_lock_irqsave(host lock)
Tejun Heo7b70fc02006-05-15 20:58:07 +0900707 */
708void ata_port_schedule_eh(struct ata_port *ap)
709{
710 WARN_ON(!ap->ops->error_handler);
711
Tejun Heof4d6d002007-05-01 11:50:15 +0200712 if (ap->pflags & ATA_PFLAG_INITIALIZING)
713 return;
714
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900715 ata_eh_set_pending(ap, 1);
Jeff Garzikcca39742006-08-24 03:19:22 -0400716 scsi_schedule_eh(ap->scsi_host);
Tejun Heo7b70fc02006-05-15 20:58:07 +0900717
718 DPRINTK("port EH scheduled\n");
719}
720
721/**
722 * ata_port_abort - abort all qc's on the port
723 * @ap: ATA port to abort qc's for
724 *
725 * Abort all active qc's of @ap and schedule EH.
726 *
727 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400728 * spin_lock_irqsave(host lock)
Tejun Heo7b70fc02006-05-15 20:58:07 +0900729 *
730 * RETURNS:
731 * Number of aborted qc's.
732 */
733int ata_port_abort(struct ata_port *ap)
734{
735 int tag, nr_aborted = 0;
736
737 WARN_ON(!ap->ops->error_handler);
738
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900739 /* we're gonna abort all commands, no need for fast drain */
740 ata_eh_set_pending(ap, 0);
741
Tejun Heo7b70fc02006-05-15 20:58:07 +0900742 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
743 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
744
745 if (qc) {
746 qc->flags |= ATA_QCFLAG_FAILED;
747 ata_qc_complete(qc);
748 nr_aborted++;
749 }
750 }
751
752 if (!nr_aborted)
753 ata_port_schedule_eh(ap);
754
755 return nr_aborted;
756}
757
Tejun Heoe3180492006-05-15 20:58:09 +0900758/**
759 * __ata_port_freeze - freeze port
760 * @ap: ATA port to freeze
761 *
762 * This function is called when HSM violation or some other
763 * condition disrupts normal operation of the port. Frozen port
764 * is not allowed to perform any operation until the port is
765 * thawed, which usually follows a successful reset.
766 *
767 * ap->ops->freeze() callback can be used for freezing the port
768 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
769 * port cannot be frozen hardware-wise, the interrupt handler
770 * must ack and clear interrupts unconditionally while the port
771 * is frozen.
772 *
773 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400774 * spin_lock_irqsave(host lock)
Tejun Heoe3180492006-05-15 20:58:09 +0900775 */
776static void __ata_port_freeze(struct ata_port *ap)
777{
778 WARN_ON(!ap->ops->error_handler);
779
780 if (ap->ops->freeze)
781 ap->ops->freeze(ap);
782
Tejun Heob51e9e52006-06-29 01:29:30 +0900783 ap->pflags |= ATA_PFLAG_FROZEN;
Tejun Heoe3180492006-05-15 20:58:09 +0900784
Tejun Heo44877b42007-02-21 01:06:51 +0900785 DPRINTK("ata%u port frozen\n", ap->print_id);
Tejun Heoe3180492006-05-15 20:58:09 +0900786}
787
788/**
789 * ata_port_freeze - abort & freeze port
790 * @ap: ATA port to freeze
791 *
792 * Abort and freeze @ap.
793 *
794 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400795 * spin_lock_irqsave(host lock)
Tejun Heoe3180492006-05-15 20:58:09 +0900796 *
797 * RETURNS:
798 * Number of aborted commands.
799 */
800int ata_port_freeze(struct ata_port *ap)
801{
802 int nr_aborted;
803
804 WARN_ON(!ap->ops->error_handler);
805
806 nr_aborted = ata_port_abort(ap);
807 __ata_port_freeze(ap);
808
809 return nr_aborted;
810}
811
812/**
813 * ata_eh_freeze_port - EH helper to freeze port
814 * @ap: ATA port to freeze
815 *
816 * Freeze @ap.
817 *
818 * LOCKING:
819 * None.
820 */
821void ata_eh_freeze_port(struct ata_port *ap)
822{
823 unsigned long flags;
824
825 if (!ap->ops->error_handler)
826 return;
827
Jeff Garzikba6a1302006-06-22 23:46:10 -0400828 spin_lock_irqsave(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900829 __ata_port_freeze(ap);
Jeff Garzikba6a1302006-06-22 23:46:10 -0400830 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900831}
832
833/**
834 * ata_port_thaw_port - EH helper to thaw port
835 * @ap: ATA port to thaw
836 *
837 * Thaw frozen port @ap.
838 *
839 * LOCKING:
840 * None.
841 */
842void ata_eh_thaw_port(struct ata_port *ap)
843{
844 unsigned long flags;
845
846 if (!ap->ops->error_handler)
847 return;
848
Jeff Garzikba6a1302006-06-22 23:46:10 -0400849 spin_lock_irqsave(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900850
Tejun Heob51e9e52006-06-29 01:29:30 +0900851 ap->pflags &= ~ATA_PFLAG_FROZEN;
Tejun Heoe3180492006-05-15 20:58:09 +0900852
853 if (ap->ops->thaw)
854 ap->ops->thaw(ap);
855
Jeff Garzikba6a1302006-06-22 23:46:10 -0400856 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900857
Tejun Heo44877b42007-02-21 01:06:51 +0900858 DPRINTK("ata%u port thawed\n", ap->print_id);
Tejun Heoe3180492006-05-15 20:58:09 +0900859}
860
Tejun Heoece1d632006-04-02 18:51:53 +0900861static void ata_eh_scsidone(struct scsi_cmnd *scmd)
862{
863 /* nada */
864}
865
866static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
867{
868 struct ata_port *ap = qc->ap;
869 struct scsi_cmnd *scmd = qc->scsicmd;
870 unsigned long flags;
871
Jeff Garzikba6a1302006-06-22 23:46:10 -0400872 spin_lock_irqsave(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900873 qc->scsidone = ata_eh_scsidone;
874 __ata_qc_complete(qc);
875 WARN_ON(ata_tag_valid(qc->tag));
Jeff Garzikba6a1302006-06-22 23:46:10 -0400876 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900877
878 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
879}
880
881/**
882 * ata_eh_qc_complete - Complete an active ATA command from EH
883 * @qc: Command to complete
884 *
885 * Indicate to the mid and upper layers that an ATA command has
886 * completed. To be used from EH.
887 */
888void ata_eh_qc_complete(struct ata_queued_cmd *qc)
889{
890 struct scsi_cmnd *scmd = qc->scsicmd;
891 scmd->retries = scmd->allowed;
892 __ata_eh_qc_complete(qc);
893}
894
895/**
896 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
897 * @qc: Command to retry
898 *
899 * Indicate to the mid and upper layers that an ATA command
900 * should be retried. To be used from EH.
901 *
902 * SCSI midlayer limits the number of retries to scmd->allowed.
903 * scmd->retries is decremented for commands which get retried
904 * due to unrelated failures (qc->err_mask is zero).
905 */
906void ata_eh_qc_retry(struct ata_queued_cmd *qc)
907{
908 struct scsi_cmnd *scmd = qc->scsicmd;
909 if (!qc->err_mask && scmd->retries)
910 scmd->retries--;
911 __ata_eh_qc_complete(qc);
912}
Tejun Heo022bdb02006-05-15 20:58:22 +0900913
914/**
Tejun Heo0ea035a2006-05-31 18:28:01 +0900915 * ata_eh_detach_dev - detach ATA device
916 * @dev: ATA device to detach
917 *
918 * Detach @dev.
919 *
920 * LOCKING:
921 * None.
922 */
923static void ata_eh_detach_dev(struct ata_device *dev)
924{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900925 struct ata_port *ap = dev->link->ap;
Tejun Heo0ea035a2006-05-31 18:28:01 +0900926 unsigned long flags;
927
928 ata_dev_disable(dev);
929
Jeff Garzikba6a1302006-06-22 23:46:10 -0400930 spin_lock_irqsave(ap->lock, flags);
Tejun Heo0ea035a2006-05-31 18:28:01 +0900931
932 dev->flags &= ~ATA_DFLAG_DETACH;
933
934 if (ata_scsi_offline_dev(dev)) {
935 dev->flags |= ATA_DFLAG_DETACHED;
Tejun Heob51e9e52006-06-29 01:29:30 +0900936 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
Tejun Heo0ea035a2006-05-31 18:28:01 +0900937 }
938
Tejun Heobeb07c12006-06-24 20:30:19 +0900939 /* clear per-dev EH actions */
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900940 ata_eh_clear_action(dev, &dev->link->eh_info, ATA_EH_PERDEV_MASK);
941 ata_eh_clear_action(dev, &dev->link->eh_context.i, ATA_EH_PERDEV_MASK);
Tejun Heobeb07c12006-06-24 20:30:19 +0900942
Jeff Garzikba6a1302006-06-22 23:46:10 -0400943 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo0ea035a2006-05-31 18:28:01 +0900944}
945
946/**
Tejun Heo022bdb02006-05-15 20:58:22 +0900947 * ata_eh_about_to_do - about to perform eh_action
948 * @ap: target ATA port
Tejun Heo47005f22006-06-19 18:27:23 +0900949 * @dev: target ATA dev for per-dev action (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +0900950 * @action: action about to be performed
951 *
952 * Called just before performing EH actions to clear related bits
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900953 * in @ap->link.eh_info such that eh actions are not
954 * unnecessarily repeated.
Tejun Heo022bdb02006-05-15 20:58:22 +0900955 *
956 * LOCKING:
957 * None.
958 */
Tejun Heo47005f22006-06-19 18:27:23 +0900959static void ata_eh_about_to_do(struct ata_port *ap, struct ata_device *dev,
960 unsigned int action)
Tejun Heo022bdb02006-05-15 20:58:22 +0900961{
962 unsigned long flags;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900963 struct ata_eh_info *ehi = &ap->link.eh_info;
964 struct ata_eh_context *ehc = &ap->link.eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +0900965
Jeff Garzikba6a1302006-06-22 23:46:10 -0400966 spin_lock_irqsave(ap->lock, flags);
Tejun Heo1cdaf532006-07-03 16:07:26 +0900967
Tejun Heo13abf502006-07-10 23:18:46 +0900968 /* Reset is represented by combination of actions and EHI
969 * flags. Suck in all related bits before clearing eh_info to
970 * avoid losing requested action.
971 */
972 if (action & ATA_EH_RESET_MASK) {
973 ehc->i.action |= ehi->action & ATA_EH_RESET_MASK;
974 ehc->i.flags |= ehi->flags & ATA_EHI_RESET_MODIFIER_MASK;
Tejun Heo1cdaf532006-07-03 16:07:26 +0900975
Tejun Heo13abf502006-07-10 23:18:46 +0900976 /* make sure all reset actions are cleared & clear EHI flags */
977 action |= ATA_EH_RESET_MASK;
978 ehi->flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
979 }
980
981 ata_eh_clear_action(dev, ehi, action);
982
983 if (!(ehc->i.flags & ATA_EHI_QUIET))
Tejun Heo1cdaf532006-07-03 16:07:26 +0900984 ap->pflags |= ATA_PFLAG_RECOVERED;
985
Jeff Garzikba6a1302006-06-22 23:46:10 -0400986 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo022bdb02006-05-15 20:58:22 +0900987}
988
989/**
Tejun Heo47005f22006-06-19 18:27:23 +0900990 * ata_eh_done - EH action complete
991 * @ap: target ATA port
992 * @dev: target ATA dev for per-dev action (can be NULL)
993 * @action: action just completed
994 *
995 * Called right after performing EH actions to clear related bits
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900996 * in @ap->link.eh_context.
Tejun Heo47005f22006-06-19 18:27:23 +0900997 *
998 * LOCKING:
999 * None.
1000 */
1001static void ata_eh_done(struct ata_port *ap, struct ata_device *dev,
1002 unsigned int action)
1003{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001004 struct ata_eh_context *ehc = &ap->link.eh_context;
1005
Tejun Heo13abf502006-07-10 23:18:46 +09001006 /* if reset is complete, clear all reset actions & reset modifier */
1007 if (action & ATA_EH_RESET_MASK) {
1008 action |= ATA_EH_RESET_MASK;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001009 ehc->i.flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
Tejun Heo13abf502006-07-10 23:18:46 +09001010 }
1011
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001012 ata_eh_clear_action(dev, &ehc->i, action);
Tejun Heo47005f22006-06-19 18:27:23 +09001013}
1014
1015/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001016 * ata_err_string - convert err_mask to descriptive string
1017 * @err_mask: error mask to convert to string
1018 *
1019 * Convert @err_mask to descriptive string. Errors are
1020 * prioritized according to severity and only the most severe
1021 * error is reported.
1022 *
1023 * LOCKING:
1024 * None.
1025 *
1026 * RETURNS:
1027 * Descriptive string for @err_mask
1028 */
1029static const char * ata_err_string(unsigned int err_mask)
1030{
1031 if (err_mask & AC_ERR_HOST_BUS)
1032 return "host bus error";
1033 if (err_mask & AC_ERR_ATA_BUS)
1034 return "ATA bus error";
1035 if (err_mask & AC_ERR_TIMEOUT)
1036 return "timeout";
1037 if (err_mask & AC_ERR_HSM)
1038 return "HSM violation";
1039 if (err_mask & AC_ERR_SYSTEM)
1040 return "internal error";
1041 if (err_mask & AC_ERR_MEDIA)
1042 return "media error";
1043 if (err_mask & AC_ERR_INVALID)
1044 return "invalid argument";
1045 if (err_mask & AC_ERR_DEV)
1046 return "device error";
1047 return "unknown error";
1048}
1049
1050/**
Tejun Heoe8ee8452006-05-15 21:03:46 +09001051 * ata_read_log_page - read a specific log page
1052 * @dev: target device
1053 * @page: page to read
1054 * @buf: buffer to store read page
1055 * @sectors: number of sectors to read
1056 *
1057 * Read log page using READ_LOG_EXT command.
1058 *
1059 * LOCKING:
1060 * Kernel thread context (may sleep).
1061 *
1062 * RETURNS:
1063 * 0 on success, AC_ERR_* mask otherwise.
1064 */
1065static unsigned int ata_read_log_page(struct ata_device *dev,
1066 u8 page, void *buf, unsigned int sectors)
1067{
1068 struct ata_taskfile tf;
1069 unsigned int err_mask;
1070
1071 DPRINTK("read log page - page %d\n", page);
1072
1073 ata_tf_init(dev, &tf);
1074 tf.command = ATA_CMD_READ_LOG_EXT;
1075 tf.lbal = page;
1076 tf.nsect = sectors;
1077 tf.hob_nsect = sectors >> 8;
1078 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1079 tf.protocol = ATA_PROT_PIO;
1080
1081 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
1082 buf, sectors * ATA_SECT_SIZE);
1083
1084 DPRINTK("EXIT, err_mask=%x\n", err_mask);
1085 return err_mask;
1086}
1087
1088/**
1089 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
1090 * @dev: Device to read log page 10h from
1091 * @tag: Resulting tag of the failed command
1092 * @tf: Resulting taskfile registers of the failed command
1093 *
1094 * Read log page 10h to obtain NCQ error details and clear error
1095 * condition.
1096 *
1097 * LOCKING:
1098 * Kernel thread context (may sleep).
1099 *
1100 * RETURNS:
1101 * 0 on success, -errno otherwise.
1102 */
1103static int ata_eh_read_log_10h(struct ata_device *dev,
1104 int *tag, struct ata_taskfile *tf)
1105{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001106 u8 *buf = dev->link->ap->sector_buf;
Tejun Heoe8ee8452006-05-15 21:03:46 +09001107 unsigned int err_mask;
1108 u8 csum;
1109 int i;
1110
1111 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
1112 if (err_mask)
1113 return -EIO;
1114
1115 csum = 0;
1116 for (i = 0; i < ATA_SECT_SIZE; i++)
1117 csum += buf[i];
1118 if (csum)
1119 ata_dev_printk(dev, KERN_WARNING,
1120 "invalid checksum 0x%x on log page 10h\n", csum);
1121
1122 if (buf[0] & 0x80)
1123 return -ENOENT;
1124
1125 *tag = buf[0] & 0x1f;
1126
1127 tf->command = buf[2];
1128 tf->feature = buf[3];
1129 tf->lbal = buf[4];
1130 tf->lbam = buf[5];
1131 tf->lbah = buf[6];
1132 tf->device = buf[7];
1133 tf->hob_lbal = buf[8];
1134 tf->hob_lbam = buf[9];
1135 tf->hob_lbah = buf[10];
1136 tf->nsect = buf[12];
1137 tf->hob_nsect = buf[13];
1138
1139 return 0;
1140}
1141
1142/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001143 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1144 * @dev: device to perform REQUEST_SENSE to
1145 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
1146 *
1147 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
1148 * SENSE. This function is EH helper.
1149 *
1150 * LOCKING:
1151 * Kernel thread context (may sleep).
1152 *
1153 * RETURNS:
1154 * 0 on success, AC_ERR_* mask on failure
1155 */
Albert Lee56287762007-04-02 11:30:46 +08001156static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc)
Tejun Heo022bdb02006-05-15 20:58:22 +09001157{
Albert Lee56287762007-04-02 11:30:46 +08001158 struct ata_device *dev = qc->dev;
1159 unsigned char *sense_buf = qc->scsicmd->sense_buffer;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001160 struct ata_port *ap = dev->link->ap;
Tejun Heo022bdb02006-05-15 20:58:22 +09001161 struct ata_taskfile tf;
1162 u8 cdb[ATAPI_CDB_LEN];
1163
1164 DPRINTK("ATAPI request sense\n");
1165
Tejun Heo022bdb02006-05-15 20:58:22 +09001166 /* FIXME: is this needed? */
1167 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1168
Albert Lee56287762007-04-02 11:30:46 +08001169 /* initialize sense_buf with the error register,
1170 * for the case where they are -not- overwritten
1171 */
Tejun Heo022bdb02006-05-15 20:58:22 +09001172 sense_buf[0] = 0x70;
Albert Lee56287762007-04-02 11:30:46 +08001173 sense_buf[2] = qc->result_tf.feature >> 4;
1174
Jeff Garzika617c092007-05-21 20:14:23 -04001175 /* some devices time out if garbage left in tf */
Albert Lee56287762007-04-02 11:30:46 +08001176 ata_tf_init(dev, &tf);
Tejun Heo022bdb02006-05-15 20:58:22 +09001177
1178 memset(cdb, 0, ATAPI_CDB_LEN);
1179 cdb[0] = REQUEST_SENSE;
1180 cdb[4] = SCSI_SENSE_BUFFERSIZE;
1181
1182 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1183 tf.command = ATA_CMD_PACKET;
1184
1185 /* is it pointless to prefer PIO for "safety reasons"? */
1186 if (ap->flags & ATA_FLAG_PIO_DMA) {
1187 tf.protocol = ATA_PROT_ATAPI_DMA;
1188 tf.feature |= ATAPI_PKT_DMA;
1189 } else {
1190 tf.protocol = ATA_PROT_ATAPI;
1191 tf.lbam = (8 * 1024) & 0xff;
1192 tf.lbah = (8 * 1024) >> 8;
1193 }
1194
1195 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
1196 sense_buf, SCSI_SENSE_BUFFERSIZE);
1197}
1198
1199/**
1200 * ata_eh_analyze_serror - analyze SError for a failed port
1201 * @ap: ATA port to analyze SError for
1202 *
1203 * Analyze SError if available and further determine cause of
1204 * failure.
1205 *
1206 * LOCKING:
1207 * None.
1208 */
1209static void ata_eh_analyze_serror(struct ata_port *ap)
1210{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001211 struct ata_eh_context *ehc = &ap->link.eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001212 u32 serror = ehc->i.serror;
1213 unsigned int err_mask = 0, action = 0;
1214
1215 if (serror & SERR_PERSISTENT) {
1216 err_mask |= AC_ERR_ATA_BUS;
1217 action |= ATA_EH_HARDRESET;
1218 }
1219 if (serror &
1220 (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
1221 err_mask |= AC_ERR_ATA_BUS;
1222 action |= ATA_EH_SOFTRESET;
1223 }
1224 if (serror & SERR_PROTOCOL) {
1225 err_mask |= AC_ERR_HSM;
1226 action |= ATA_EH_SOFTRESET;
1227 }
1228 if (serror & SERR_INTERNAL) {
1229 err_mask |= AC_ERR_SYSTEM;
Tejun Heo771b8da2007-03-14 01:20:51 +09001230 action |= ATA_EH_HARDRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001231 }
Tejun Heo084fe632006-05-31 18:28:03 +09001232 if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG))
1233 ata_ehi_hotplugged(&ehc->i);
Tejun Heo022bdb02006-05-15 20:58:22 +09001234
1235 ehc->i.err_mask |= err_mask;
1236 ehc->i.action |= action;
1237}
1238
1239/**
Tejun Heoe8ee8452006-05-15 21:03:46 +09001240 * ata_eh_analyze_ncq_error - analyze NCQ error
1241 * @ap: ATA port to analyze NCQ error for
1242 *
1243 * Read log page 10h, determine the offending qc and acquire
1244 * error status TF. For NCQ device errors, all LLDDs have to do
1245 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1246 * care of the rest.
1247 *
1248 * LOCKING:
1249 * Kernel thread context (may sleep).
1250 */
1251static void ata_eh_analyze_ncq_error(struct ata_port *ap)
1252{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001253 struct ata_eh_context *ehc = &ap->link.eh_context;
1254 struct ata_device *dev = ap->link.device;
Tejun Heoe8ee8452006-05-15 21:03:46 +09001255 struct ata_queued_cmd *qc;
1256 struct ata_taskfile tf;
1257 int tag, rc;
1258
1259 /* if frozen, we can't do much */
Tejun Heob51e9e52006-06-29 01:29:30 +09001260 if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heoe8ee8452006-05-15 21:03:46 +09001261 return;
1262
1263 /* is it NCQ device error? */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001264 if (!ap->link.sactive || !(ehc->i.err_mask & AC_ERR_DEV))
Tejun Heoe8ee8452006-05-15 21:03:46 +09001265 return;
1266
1267 /* has LLDD analyzed already? */
1268 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1269 qc = __ata_qc_from_tag(ap, tag);
1270
1271 if (!(qc->flags & ATA_QCFLAG_FAILED))
1272 continue;
1273
1274 if (qc->err_mask)
1275 return;
1276 }
1277
1278 /* okay, this error is ours */
1279 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1280 if (rc) {
1281 ata_port_printk(ap, KERN_ERR, "failed to read log page 10h "
1282 "(errno=%d)\n", rc);
1283 return;
1284 }
1285
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001286 if (!(ap->link.sactive & (1 << tag))) {
Tejun Heoe8ee8452006-05-15 21:03:46 +09001287 ata_port_printk(ap, KERN_ERR, "log page 10h reported "
1288 "inactive tag %d\n", tag);
1289 return;
1290 }
1291
1292 /* we've got the perpetrator, condemn it */
1293 qc = __ata_qc_from_tag(ap, tag);
1294 memcpy(&qc->result_tf, &tf, sizeof(tf));
Tejun Heo5335b722007-07-16 14:29:40 +09001295 qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
Tejun Heoe8ee8452006-05-15 21:03:46 +09001296 ehc->i.err_mask &= ~AC_ERR_DEV;
1297}
1298
1299/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001300 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1301 * @qc: qc to analyze
1302 * @tf: Taskfile registers to analyze
1303 *
1304 * Analyze taskfile of @qc and further determine cause of
1305 * failure. This function also requests ATAPI sense data if
1306 * avaliable.
1307 *
1308 * LOCKING:
1309 * Kernel thread context (may sleep).
1310 *
1311 * RETURNS:
1312 * Determined recovery action
1313 */
1314static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1315 const struct ata_taskfile *tf)
1316{
1317 unsigned int tmp, action = 0;
1318 u8 stat = tf->command, err = tf->feature;
1319
1320 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1321 qc->err_mask |= AC_ERR_HSM;
1322 return ATA_EH_SOFTRESET;
1323 }
1324
Tejun Heoa51d6442007-03-20 15:24:11 +09001325 if (stat & (ATA_ERR | ATA_DF))
1326 qc->err_mask |= AC_ERR_DEV;
1327 else
Tejun Heo022bdb02006-05-15 20:58:22 +09001328 return 0;
1329
1330 switch (qc->dev->class) {
1331 case ATA_DEV_ATA:
1332 if (err & ATA_ICRC)
1333 qc->err_mask |= AC_ERR_ATA_BUS;
1334 if (err & ATA_UNC)
1335 qc->err_mask |= AC_ERR_MEDIA;
1336 if (err & ATA_IDNF)
1337 qc->err_mask |= AC_ERR_INVALID;
1338 break;
1339
1340 case ATA_DEV_ATAPI:
Tejun Heoa569a302006-11-21 10:40:51 +09001341 if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
Albert Lee56287762007-04-02 11:30:46 +08001342 tmp = atapi_eh_request_sense(qc);
Tejun Heoa569a302006-11-21 10:40:51 +09001343 if (!tmp) {
1344 /* ATA_QCFLAG_SENSE_VALID is used to
1345 * tell atapi_qc_complete() that sense
1346 * data is already valid.
1347 *
1348 * TODO: interpret sense data and set
1349 * appropriate err_mask.
1350 */
1351 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1352 } else
1353 qc->err_mask |= tmp;
1354 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001355 }
1356
1357 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1358 action |= ATA_EH_SOFTRESET;
1359
1360 return action;
1361}
1362
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001363static int ata_eh_categorize_error(int is_io, unsigned int err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001364{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001365 if (err_mask & AC_ERR_ATA_BUS)
Tejun Heo022bdb02006-05-15 20:58:22 +09001366 return 1;
1367
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001368 if (err_mask & AC_ERR_TIMEOUT)
1369 return 2;
1370
1371 if (is_io) {
1372 if (err_mask & AC_ERR_HSM)
Tejun Heo022bdb02006-05-15 20:58:22 +09001373 return 2;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001374 if ((err_mask &
1375 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1376 return 3;
Tejun Heo022bdb02006-05-15 20:58:22 +09001377 }
1378
1379 return 0;
1380}
1381
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001382struct speed_down_verdict_arg {
Tejun Heo022bdb02006-05-15 20:58:22 +09001383 u64 since;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001384 int nr_errors[4];
Tejun Heo022bdb02006-05-15 20:58:22 +09001385};
1386
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001387static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
Tejun Heo022bdb02006-05-15 20:58:22 +09001388{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001389 struct speed_down_verdict_arg *arg = void_arg;
1390 int cat = ata_eh_categorize_error(ent->is_io, ent->err_mask);
Tejun Heo022bdb02006-05-15 20:58:22 +09001391
1392 if (ent->timestamp < arg->since)
1393 return -1;
1394
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001395 arg->nr_errors[cat]++;
Tejun Heo022bdb02006-05-15 20:58:22 +09001396 return 0;
1397}
1398
1399/**
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001400 * ata_eh_speed_down_verdict - Determine speed down verdict
Tejun Heo022bdb02006-05-15 20:58:22 +09001401 * @dev: Device of interest
1402 *
1403 * This function examines error ring of @dev and determines
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001404 * whether NCQ needs to be turned off, transfer speed should be
1405 * stepped down, or falling back to PIO is necessary.
Tejun Heo022bdb02006-05-15 20:58:22 +09001406 *
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001407 * Cat-1 is ATA_BUS error for any command.
Tejun Heo022bdb02006-05-15 20:58:22 +09001408 *
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001409 * Cat-2 is TIMEOUT for any command or HSM violation for known
1410 * supported commands.
1411 *
1412 * Cat-3 is is unclassified DEV error for known supported
Tejun Heo022bdb02006-05-15 20:58:22 +09001413 * command.
1414 *
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001415 * NCQ needs to be turned off if there have been more than 3
1416 * Cat-2 + Cat-3 errors during last 10 minutes.
1417 *
1418 * Speed down is necessary if there have been more than 3 Cat-1 +
1419 * Cat-2 errors or 10 Cat-3 errors during last 10 minutes.
1420 *
1421 * Falling back to PIO mode is necessary if there have been more
1422 * than 10 Cat-1 + Cat-2 + Cat-3 errors during last 5 minutes.
1423 *
Tejun Heo022bdb02006-05-15 20:58:22 +09001424 * LOCKING:
1425 * Inherited from caller.
1426 *
1427 * RETURNS:
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001428 * OR of ATA_EH_SPDN_* flags.
Tejun Heo022bdb02006-05-15 20:58:22 +09001429 */
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001430static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
Tejun Heo022bdb02006-05-15 20:58:22 +09001431{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001432 const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
1433 u64 j64 = get_jiffies_64();
1434 struct speed_down_verdict_arg arg;
1435 unsigned int verdict = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09001436
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001437 /* scan past 10 mins of error history */
Tejun Heo022bdb02006-05-15 20:58:22 +09001438 memset(&arg, 0, sizeof(arg));
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001439 arg.since = j64 - min(j64, j10mins);
1440 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
Tejun Heo022bdb02006-05-15 20:58:22 +09001441
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001442 if (arg.nr_errors[2] + arg.nr_errors[3] > 3)
1443 verdict |= ATA_EH_SPDN_NCQ_OFF;
1444 if (arg.nr_errors[1] + arg.nr_errors[2] > 3 || arg.nr_errors[3] > 10)
1445 verdict |= ATA_EH_SPDN_SPEED_DOWN;
Tejun Heo022bdb02006-05-15 20:58:22 +09001446
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001447 /* scan past 3 mins of error history */
1448 memset(&arg, 0, sizeof(arg));
1449 arg.since = j64 - min(j64, j5mins);
1450 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
Tejun Heo022bdb02006-05-15 20:58:22 +09001451
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001452 if (arg.nr_errors[1] + arg.nr_errors[2] + arg.nr_errors[3] > 10)
1453 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
1454
1455 return verdict;
Tejun Heo022bdb02006-05-15 20:58:22 +09001456}
1457
1458/**
1459 * ata_eh_speed_down - record error and speed down if necessary
1460 * @dev: Failed device
1461 * @is_io: Did the device fail during normal IO?
1462 * @err_mask: err_mask of the error
1463 *
1464 * Record error and examine error history to determine whether
1465 * adjusting transmission speed is necessary. It also sets
1466 * transmission limits appropriately if such adjustment is
1467 * necessary.
1468 *
1469 * LOCKING:
1470 * Kernel thread context (may sleep).
1471 *
1472 * RETURNS:
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001473 * Determined recovery action.
Tejun Heo022bdb02006-05-15 20:58:22 +09001474 */
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001475static unsigned int ata_eh_speed_down(struct ata_device *dev, int is_io,
1476 unsigned int err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001477{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001478 unsigned int verdict;
1479 unsigned int action = 0;
1480
1481 /* don't bother if Cat-0 error */
1482 if (ata_eh_categorize_error(is_io, err_mask) == 0)
Tejun Heo022bdb02006-05-15 20:58:22 +09001483 return 0;
1484
1485 /* record error and determine whether speed down is necessary */
1486 ata_ering_record(&dev->ering, is_io, err_mask);
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001487 verdict = ata_eh_speed_down_verdict(dev);
Tejun Heo022bdb02006-05-15 20:58:22 +09001488
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001489 /* turn off NCQ? */
1490 if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
1491 (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
1492 ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
1493 dev->flags |= ATA_DFLAG_NCQ_OFF;
1494 ata_dev_printk(dev, KERN_WARNING,
1495 "NCQ disabled due to excessive errors\n");
1496 goto done;
1497 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001498
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001499 /* speed down? */
1500 if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1501 /* speed down SATA link speed if possible */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001502 if (sata_down_spd_limit(dev->link->ap) == 0) {
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001503 action |= ATA_EH_HARDRESET;
1504 goto done;
1505 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001506
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001507 /* lower transfer mode */
1508 if (dev->spdn_cnt < 2) {
1509 static const int dma_dnxfer_sel[] =
1510 { ATA_DNXFER_DMA, ATA_DNXFER_40C };
1511 static const int pio_dnxfer_sel[] =
1512 { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
1513 int sel;
Tejun Heo022bdb02006-05-15 20:58:22 +09001514
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001515 if (dev->xfer_shift != ATA_SHIFT_PIO)
1516 sel = dma_dnxfer_sel[dev->spdn_cnt];
1517 else
1518 sel = pio_dnxfer_sel[dev->spdn_cnt];
1519
1520 dev->spdn_cnt++;
1521
1522 if (ata_down_xfermask_limit(dev, sel) == 0) {
1523 action |= ATA_EH_SOFTRESET;
1524 goto done;
1525 }
1526 }
1527 }
1528
1529 /* Fall back to PIO? Slowing down to PIO is meaningless for
1530 * SATA. Consider it only for PATA.
1531 */
1532 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001533 (dev->link->ap->cbl != ATA_CBL_SATA) &&
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001534 (dev->xfer_shift != ATA_SHIFT_PIO)) {
1535 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
1536 dev->spdn_cnt = 0;
1537 action |= ATA_EH_SOFTRESET;
1538 goto done;
1539 }
1540 }
1541
Tejun Heo022bdb02006-05-15 20:58:22 +09001542 return 0;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001543 done:
1544 /* device has been slowed down, blow error history */
1545 ata_ering_clear(&dev->ering);
1546 return action;
Tejun Heo022bdb02006-05-15 20:58:22 +09001547}
1548
1549/**
1550 * ata_eh_autopsy - analyze error and determine recovery action
1551 * @ap: ATA port to perform autopsy on
1552 *
1553 * Analyze why @ap failed and determine which recovery action is
1554 * needed. This function also sets more detailed AC_ERR_* values
1555 * and fills sense data for ATAPI CHECK SENSE.
1556 *
1557 * LOCKING:
1558 * Kernel thread context (may sleep).
1559 */
1560static void ata_eh_autopsy(struct ata_port *ap)
1561{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001562 struct ata_eh_context *ehc = &ap->link.eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001563 unsigned int all_err_mask = 0;
1564 int tag, is_io = 0;
1565 u32 serror;
1566 int rc;
1567
1568 DPRINTK("ENTER\n");
1569
Tejun Heo1cdaf532006-07-03 16:07:26 +09001570 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1571 return;
1572
Tejun Heo022bdb02006-05-15 20:58:22 +09001573 /* obtain and analyze SError */
1574 rc = sata_scr_read(ap, SCR_ERROR, &serror);
1575 if (rc == 0) {
1576 ehc->i.serror |= serror;
1577 ata_eh_analyze_serror(ap);
Tejun Heo4e57c512007-07-16 14:29:41 +09001578 } else if (rc != -EOPNOTSUPP) {
1579 /* SError read failed, force hardreset and probing */
1580 ata_ehi_schedule_probe(&ehc->i);
Tejun Heo4528e4d2006-07-08 20:17:26 +09001581 ehc->i.action |= ATA_EH_HARDRESET;
Tejun Heo4e57c512007-07-16 14:29:41 +09001582 ehc->i.err_mask |= AC_ERR_OTHER;
1583 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001584
Tejun Heoe8ee8452006-05-15 21:03:46 +09001585 /* analyze NCQ failure */
1586 ata_eh_analyze_ncq_error(ap);
1587
Tejun Heo022bdb02006-05-15 20:58:22 +09001588 /* any real error trumps AC_ERR_OTHER */
1589 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1590 ehc->i.err_mask &= ~AC_ERR_OTHER;
1591
1592 all_err_mask |= ehc->i.err_mask;
1593
1594 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1595 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1596
1597 if (!(qc->flags & ATA_QCFLAG_FAILED))
1598 continue;
1599
1600 /* inherit upper level err_mask */
1601 qc->err_mask |= ehc->i.err_mask;
1602
Tejun Heo022bdb02006-05-15 20:58:22 +09001603 /* analyze TF */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001604 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
Tejun Heo022bdb02006-05-15 20:58:22 +09001605
1606 /* DEV errors are probably spurious in case of ATA_BUS error */
1607 if (qc->err_mask & AC_ERR_ATA_BUS)
1608 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1609 AC_ERR_INVALID);
1610
1611 /* any real error trumps unknown error */
1612 if (qc->err_mask & ~AC_ERR_OTHER)
1613 qc->err_mask &= ~AC_ERR_OTHER;
1614
1615 /* SENSE_VALID trumps dev/unknown error and revalidation */
1616 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1617 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
Tejun Heo4528e4d2006-07-08 20:17:26 +09001618 ehc->i.action &= ~ATA_EH_REVALIDATE;
Tejun Heo022bdb02006-05-15 20:58:22 +09001619 }
1620
1621 /* accumulate error info */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001622 ehc->i.dev = qc->dev;
Tejun Heo022bdb02006-05-15 20:58:22 +09001623 all_err_mask |= qc->err_mask;
1624 if (qc->flags & ATA_QCFLAG_IO)
1625 is_io = 1;
1626 }
1627
Tejun Heoa20f33f2006-05-16 12:58:24 +09001628 /* enforce default EH actions */
Tejun Heob51e9e52006-06-29 01:29:30 +09001629 if (ap->pflags & ATA_PFLAG_FROZEN ||
Tejun Heoa20f33f2006-05-16 12:58:24 +09001630 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
Tejun Heo4528e4d2006-07-08 20:17:26 +09001631 ehc->i.action |= ATA_EH_SOFTRESET;
Tejun Heoa20f33f2006-05-16 12:58:24 +09001632 else if (all_err_mask)
Tejun Heo4528e4d2006-07-08 20:17:26 +09001633 ehc->i.action |= ATA_EH_REVALIDATE;
Tejun Heo022bdb02006-05-15 20:58:22 +09001634
Tejun Heo47005f22006-06-19 18:27:23 +09001635 /* if we have offending qcs and the associated failed device */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001636 if (ehc->i.dev) {
Tejun Heo47005f22006-06-19 18:27:23 +09001637 /* speed down */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001638 ehc->i.action |= ata_eh_speed_down(ehc->i.dev, is_io,
1639 all_err_mask);
Tejun Heo47005f22006-06-19 18:27:23 +09001640
1641 /* perform per-dev EH action only on the offending device */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001642 ehc->i.dev_action[ehc->i.dev->devno] |=
1643 ehc->i.action & ATA_EH_PERDEV_MASK;
1644 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
Tejun Heo47005f22006-06-19 18:27:23 +09001645 }
1646
Tejun Heo022bdb02006-05-15 20:58:22 +09001647 DPRINTK("EXIT\n");
1648}
1649
1650/**
1651 * ata_eh_report - report error handling to user
1652 * @ap: ATA port EH is going on
1653 *
1654 * Report EH to user.
1655 *
1656 * LOCKING:
1657 * None.
1658 */
1659static void ata_eh_report(struct ata_port *ap)
1660{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001661 struct ata_eh_context *ehc = &ap->link.eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001662 const char *frozen, *desc;
1663 int tag, nr_failed = 0;
1664
1665 desc = NULL;
1666 if (ehc->i.desc[0] != '\0')
1667 desc = ehc->i.desc;
1668
1669 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1670 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1671
1672 if (!(qc->flags & ATA_QCFLAG_FAILED))
1673 continue;
1674 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1675 continue;
1676
1677 nr_failed++;
1678 }
1679
1680 if (!nr_failed && !ehc->i.err_mask)
1681 return;
1682
1683 frozen = "";
Tejun Heob51e9e52006-06-29 01:29:30 +09001684 if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heo022bdb02006-05-15 20:58:22 +09001685 frozen = " frozen";
1686
1687 if (ehc->i.dev) {
Tejun Heoe8ee8452006-05-15 21:03:46 +09001688 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1689 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001690 ehc->i.err_mask, ap->link.sactive,
1691 ehc->i.serror, ehc->i.action, frozen);
Tejun Heo022bdb02006-05-15 20:58:22 +09001692 if (desc)
Tejun Heob64bbc32007-07-16 14:29:39 +09001693 ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
Tejun Heo022bdb02006-05-15 20:58:22 +09001694 } else {
Tejun Heoe8ee8452006-05-15 21:03:46 +09001695 ata_port_printk(ap, KERN_ERR, "exception Emask 0x%x "
1696 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001697 ehc->i.err_mask, ap->link.sactive,
1698 ehc->i.serror, ehc->i.action, frozen);
Tejun Heo022bdb02006-05-15 20:58:22 +09001699 if (desc)
Tejun Heob64bbc32007-07-16 14:29:39 +09001700 ata_port_printk(ap, KERN_ERR, "%s\n", desc);
Tejun Heo022bdb02006-05-15 20:58:22 +09001701 }
1702
1703 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
Tejun Heo8a937582006-11-14 22:36:12 +09001704 static const char *dma_str[] = {
1705 [DMA_BIDIRECTIONAL] = "bidi",
1706 [DMA_TO_DEVICE] = "out",
1707 [DMA_FROM_DEVICE] = "in",
1708 [DMA_NONE] = "",
1709 };
Tejun Heo022bdb02006-05-15 20:58:22 +09001710 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
Tejun Heo8a937582006-11-14 22:36:12 +09001711 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
Tejun Heo022bdb02006-05-15 20:58:22 +09001712
1713 if (!(qc->flags & ATA_QCFLAG_FAILED) || !qc->err_mask)
1714 continue;
1715
Tejun Heo8a937582006-11-14 22:36:12 +09001716 ata_dev_printk(qc->dev, KERN_ERR,
1717 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
Tejun Heo664e8502006-11-20 16:05:34 +09001718 "tag %d cdb 0x%x data %u %s\n "
Tejun Heo8a937582006-11-14 22:36:12 +09001719 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
Tejun Heo5335b722007-07-16 14:29:40 +09001720 "Emask 0x%x (%s)%s\n",
Tejun Heo8a937582006-11-14 22:36:12 +09001721 cmd->command, cmd->feature, cmd->nsect,
1722 cmd->lbal, cmd->lbam, cmd->lbah,
1723 cmd->hob_feature, cmd->hob_nsect,
1724 cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
Tejun Heo726f0782007-01-03 17:30:39 +09001725 cmd->device, qc->tag, qc->cdb[0], qc->nbytes,
Tejun Heo664e8502006-11-20 16:05:34 +09001726 dma_str[qc->dma_dir],
Tejun Heo8a937582006-11-14 22:36:12 +09001727 res->command, res->feature, res->nsect,
1728 res->lbal, res->lbam, res->lbah,
1729 res->hob_feature, res->hob_nsect,
1730 res->hob_lbal, res->hob_lbam, res->hob_lbah,
Tejun Heo5335b722007-07-16 14:29:40 +09001731 res->device, qc->err_mask, ata_err_string(qc->err_mask),
1732 qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
Tejun Heo022bdb02006-05-15 20:58:22 +09001733 }
1734}
1735
Tejun Heod87fa382006-05-31 18:28:24 +09001736static int ata_do_reset(struct ata_port *ap, ata_reset_fn_t reset,
Tejun Heod4b2bab2007-02-02 16:50:52 +09001737 unsigned int *classes, unsigned long deadline)
Tejun Heod87fa382006-05-31 18:28:24 +09001738{
1739 int i, rc;
1740
1741 for (i = 0; i < ATA_MAX_DEVICES; i++)
1742 classes[i] = ATA_DEV_UNKNOWN;
1743
Tejun Heod4b2bab2007-02-02 16:50:52 +09001744 rc = reset(ap, classes, deadline);
Tejun Heod87fa382006-05-31 18:28:24 +09001745 if (rc)
1746 return rc;
1747
1748 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
1749 * is complete and convert all ATA_DEV_UNKNOWN to
1750 * ATA_DEV_NONE.
1751 */
1752 for (i = 0; i < ATA_MAX_DEVICES; i++)
1753 if (classes[i] != ATA_DEV_UNKNOWN)
1754 break;
1755
1756 if (i < ATA_MAX_DEVICES)
1757 for (i = 0; i < ATA_MAX_DEVICES; i++)
1758 if (classes[i] == ATA_DEV_UNKNOWN)
1759 classes[i] = ATA_DEV_NONE;
1760
1761 return 0;
1762}
1763
Tejun Heo664faf02006-05-31 18:27:50 +09001764static int ata_eh_followup_srst_needed(int rc, int classify,
1765 const unsigned int *classes)
1766{
1767 if (rc == -EAGAIN)
1768 return 1;
1769 if (rc != 0)
1770 return 0;
1771 if (classify && classes[0] == ATA_DEV_UNKNOWN)
1772 return 1;
1773 return 0;
1774}
1775
1776static int ata_eh_reset(struct ata_port *ap, int classify,
Tejun Heof5914a42006-05-31 18:27:48 +09001777 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
Tejun Heo022bdb02006-05-15 20:58:22 +09001778 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
1779{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001780 struct ata_eh_context *ehc = &ap->link.eh_context;
Tejun Heo664faf02006-05-31 18:27:50 +09001781 unsigned int *classes = ehc->classes;
Tejun Heo1cdaf532006-07-03 16:07:26 +09001782 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
Tejun Heo31daabd2007-02-02 16:50:52 +09001783 int try = 0;
1784 unsigned long deadline;
Tejun Heof5914a42006-05-31 18:27:48 +09001785 unsigned int action;
Tejun Heo022bdb02006-05-15 20:58:22 +09001786 ata_reset_fn_t reset;
Tejun Heo8b5bb2f2007-06-25 21:43:04 +09001787 int i, rc;
Tejun Heo022bdb02006-05-15 20:58:22 +09001788
Tejun Heo13abf502006-07-10 23:18:46 +09001789 /* about to reset */
1790 ata_eh_about_to_do(ap, NULL, ehc->i.action & ATA_EH_RESET_MASK);
1791
Tejun Heof5914a42006-05-31 18:27:48 +09001792 /* Determine which reset to use and record in ehc->i.action.
1793 * prereset() may examine and modify it.
1794 */
1795 action = ehc->i.action;
1796 ehc->i.action &= ~ATA_EH_RESET_MASK;
Tejun Heo022bdb02006-05-15 20:58:22 +09001797 if (softreset && (!hardreset || (!sata_set_spd_needed(ap) &&
Tejun Heof5914a42006-05-31 18:27:48 +09001798 !(action & ATA_EH_HARDRESET))))
1799 ehc->i.action |= ATA_EH_SOFTRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001800 else
Tejun Heof5914a42006-05-31 18:27:48 +09001801 ehc->i.action |= ATA_EH_HARDRESET;
1802
1803 if (prereset) {
Tejun Heo31daabd2007-02-02 16:50:52 +09001804 rc = prereset(ap, jiffies + ATA_EH_PRERESET_TIMEOUT);
Tejun Heof5914a42006-05-31 18:27:48 +09001805 if (rc) {
Alan Coxc9619222006-09-26 17:53:38 +01001806 if (rc == -ENOENT) {
Tejun Heo4aa9ab62007-03-12 17:24:08 +09001807 ata_port_printk(ap, KERN_DEBUG,
1808 "port disabled. ignoring.\n");
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001809 ehc->i.action &= ~ATA_EH_RESET_MASK;
Tejun Heo4aa9ab62007-03-12 17:24:08 +09001810
1811 for (i = 0; i < ATA_MAX_DEVICES; i++)
1812 classes[i] = ATA_DEV_NONE;
1813
1814 rc = 0;
Alan Coxc9619222006-09-26 17:53:38 +01001815 } else
1816 ata_port_printk(ap, KERN_ERR,
Tejun Heof5914a42006-05-31 18:27:48 +09001817 "prereset failed (errno=%d)\n", rc);
Tejun Heofccb6ea2007-07-16 14:29:41 +09001818 goto out;
Tejun Heof5914a42006-05-31 18:27:48 +09001819 }
1820 }
1821
1822 /* prereset() might have modified ehc->i.action */
1823 if (ehc->i.action & ATA_EH_HARDRESET)
Tejun Heo022bdb02006-05-15 20:58:22 +09001824 reset = hardreset;
Tejun Heof5914a42006-05-31 18:27:48 +09001825 else if (ehc->i.action & ATA_EH_SOFTRESET)
1826 reset = softreset;
1827 else {
1828 /* prereset told us not to reset, bang classes and return */
1829 for (i = 0; i < ATA_MAX_DEVICES; i++)
1830 classes[i] = ATA_DEV_NONE;
Tejun Heofccb6ea2007-07-16 14:29:41 +09001831 rc = 0;
1832 goto out;
Tejun Heof5914a42006-05-31 18:27:48 +09001833 }
1834
1835 /* did prereset() screw up? if so, fix up to avoid oopsing */
1836 if (!reset) {
Tejun Heof5914a42006-05-31 18:27:48 +09001837 if (softreset)
1838 reset = softreset;
1839 else
1840 reset = hardreset;
1841 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001842
1843 retry:
Tejun Heo31daabd2007-02-02 16:50:52 +09001844 deadline = jiffies + ata_eh_reset_timeouts[try++];
1845
Tejun Heo3e706392006-05-31 18:28:11 +09001846 /* shut up during boot probing */
1847 if (verbose)
1848 ata_port_printk(ap, KERN_INFO, "%s resetting port\n",
1849 reset == softreset ? "soft" : "hard");
Tejun Heo022bdb02006-05-15 20:58:22 +09001850
Tejun Heo13abf502006-07-10 23:18:46 +09001851 /* mark that this EH session started with reset */
Tejun Heo0d64a232007-04-23 02:41:05 +09001852 if (reset == hardreset)
1853 ehc->i.flags |= ATA_EHI_DID_HARDRESET;
1854 else
1855 ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001856
Tejun Heo31daabd2007-02-02 16:50:52 +09001857 rc = ata_do_reset(ap, reset, classes, deadline);
Tejun Heo022bdb02006-05-15 20:58:22 +09001858
Tejun Heo664faf02006-05-31 18:27:50 +09001859 if (reset == hardreset &&
1860 ata_eh_followup_srst_needed(rc, classify, classes)) {
1861 /* okay, let's do follow-up softreset */
Tejun Heo664faf02006-05-31 18:27:50 +09001862 reset = softreset;
1863
1864 if (!reset) {
1865 ata_port_printk(ap, KERN_ERR,
1866 "follow-up softreset required "
1867 "but no softreset avaliable\n");
Tejun Heofccb6ea2007-07-16 14:29:41 +09001868 rc = -EINVAL;
1869 goto out;
Tejun Heo664faf02006-05-31 18:27:50 +09001870 }
1871
Tejun Heo47005f22006-06-19 18:27:23 +09001872 ata_eh_about_to_do(ap, NULL, ATA_EH_RESET_MASK);
Tejun Heo31daabd2007-02-02 16:50:52 +09001873 rc = ata_do_reset(ap, reset, classes, deadline);
Tejun Heo664faf02006-05-31 18:27:50 +09001874
1875 if (rc == 0 && classify &&
1876 classes[0] == ATA_DEV_UNKNOWN) {
1877 ata_port_printk(ap, KERN_ERR,
1878 "classification failed\n");
Tejun Heofccb6ea2007-07-16 14:29:41 +09001879 rc = -EINVAL;
1880 goto out;
Tejun Heo664faf02006-05-31 18:27:50 +09001881 }
1882 }
1883
Tejun Heo31daabd2007-02-02 16:50:52 +09001884 if (rc && try < ARRAY_SIZE(ata_eh_reset_timeouts)) {
1885 unsigned long now = jiffies;
Tejun Heo664faf02006-05-31 18:27:50 +09001886
Tejun Heo31daabd2007-02-02 16:50:52 +09001887 if (time_before(now, deadline)) {
1888 unsigned long delta = deadline - jiffies;
Tejun Heo664faf02006-05-31 18:27:50 +09001889
Tejun Heo31daabd2007-02-02 16:50:52 +09001890 ata_port_printk(ap, KERN_WARNING, "reset failed "
1891 "(errno=%d), retrying in %u secs\n",
1892 rc, (jiffies_to_msecs(delta) + 999) / 1000);
Tejun Heo022bdb02006-05-15 20:58:22 +09001893
Tejun Heo31daabd2007-02-02 16:50:52 +09001894 schedule_timeout_uninterruptible(delta);
1895 }
1896
Tejun Heof1545152007-07-16 14:29:40 +09001897 if (rc == -EPIPE ||
Tejun Heo31daabd2007-02-02 16:50:52 +09001898 try == ARRAY_SIZE(ata_eh_reset_timeouts) - 1)
Tejun Heo022bdb02006-05-15 20:58:22 +09001899 sata_down_spd_limit(ap);
1900 if (hardreset)
1901 reset = hardreset;
1902 goto retry;
1903 }
1904
1905 if (rc == 0) {
Tejun Heo008a7892007-07-16 14:29:40 +09001906 u32 sstatus;
1907
Tejun Heo20952b62006-05-31 18:27:23 +09001908 /* After the reset, the device state is PIO 0 and the
1909 * controller state is undefined. Record the mode.
1910 */
1911 for (i = 0; i < ATA_MAX_DEVICES; i++)
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001912 ap->link.device[i].pio_mode = XFER_PIO_0;
Tejun Heo20952b62006-05-31 18:27:23 +09001913
Tejun Heo008a7892007-07-16 14:29:40 +09001914 /* record current link speed */
1915 if (sata_scr_read(ap, SCR_STATUS, &sstatus) == 0)
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001916 ap->link.sata_spd = (sstatus >> 4) & 0xf;
Tejun Heo008a7892007-07-16 14:29:40 +09001917
Tejun Heo022bdb02006-05-15 20:58:22 +09001918 if (postreset)
1919 postreset(ap, classes);
1920
1921 /* reset successful, schedule revalidation */
Tejun Heo13abf502006-07-10 23:18:46 +09001922 ata_eh_done(ap, NULL, ehc->i.action & ATA_EH_RESET_MASK);
Tejun Heo022bdb02006-05-15 20:58:22 +09001923 ehc->i.action |= ATA_EH_REVALIDATE;
1924 }
Tejun Heofccb6ea2007-07-16 14:29:41 +09001925 out:
1926 /* clear hotplug flag */
1927 ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
Tejun Heo022bdb02006-05-15 20:58:22 +09001928 return rc;
1929}
1930
Tejun Heo084fe632006-05-31 18:28:03 +09001931static int ata_eh_revalidate_and_attach(struct ata_port *ap,
1932 struct ata_device **r_failed_dev)
Tejun Heo022bdb02006-05-15 20:58:22 +09001933{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001934 struct ata_eh_context *ehc = &ap->link.eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001935 struct ata_device *dev;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09001936 unsigned int new_mask = 0;
Tejun Heo084fe632006-05-31 18:28:03 +09001937 unsigned long flags;
Tejun Heo022bdb02006-05-15 20:58:22 +09001938 int i, rc = 0;
1939
1940 DPRINTK("ENTER\n");
1941
Tejun Heo8c3c52a2007-03-22 22:24:19 +09001942 /* For PATA drive side cable detection to work, IDENTIFY must
1943 * be done backwards such that PDIAG- is released by the slave
1944 * device before the master device is identified.
1945 */
1946 for (i = ATA_MAX_DEVICES - 1; i >= 0; i--) {
Tejun Heobff04642006-11-10 18:08:10 +09001947 unsigned int action, readid_flags = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09001948
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001949 dev = &ap->link.device[i];
Tejun Heo64f65ca2006-06-24 20:30:18 +09001950 action = ata_eh_dev_action(dev);
Tejun Heo47005f22006-06-19 18:27:23 +09001951
Tejun Heobff04642006-11-10 18:08:10 +09001952 if (ehc->i.flags & ATA_EHI_DID_RESET)
1953 readid_flags |= ATA_READID_POSTRESET;
1954
Tejun Heo9666f402007-05-04 21:27:47 +02001955 if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
Tejun Heo022bdb02006-05-15 20:58:22 +09001956 if (ata_port_offline(ap)) {
1957 rc = -EIO;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09001958 goto err;
Tejun Heo022bdb02006-05-15 20:58:22 +09001959 }
1960
Tejun Heo47005f22006-06-19 18:27:23 +09001961 ata_eh_about_to_do(ap, dev, ATA_EH_REVALIDATE);
Tejun Heobff04642006-11-10 18:08:10 +09001962 rc = ata_dev_revalidate(dev, readid_flags);
Tejun Heo022bdb02006-05-15 20:58:22 +09001963 if (rc)
Tejun Heo8c3c52a2007-03-22 22:24:19 +09001964 goto err;
Tejun Heo022bdb02006-05-15 20:58:22 +09001965
Tejun Heo47005f22006-06-19 18:27:23 +09001966 ata_eh_done(ap, dev, ATA_EH_REVALIDATE);
1967
Tejun Heobaa1e782006-11-01 18:39:27 +09001968 /* Configuration may have changed, reconfigure
1969 * transfer mode.
1970 */
1971 ehc->i.flags |= ATA_EHI_SETMODE;
1972
zhao, forrest3057ac32006-06-12 12:01:34 +08001973 /* schedule the scsi_rescan_device() here */
1974 queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
Tejun Heo084fe632006-05-31 18:28:03 +09001975 } else if (dev->class == ATA_DEV_UNKNOWN &&
1976 ehc->tries[dev->devno] &&
1977 ata_class_enabled(ehc->classes[dev->devno])) {
1978 dev->class = ehc->classes[dev->devno];
1979
Tejun Heobff04642006-11-10 18:08:10 +09001980 rc = ata_dev_read_id(dev, &dev->class, readid_flags,
1981 dev->id);
Tejun Heo8c3c52a2007-03-22 22:24:19 +09001982 switch (rc) {
1983 case 0:
1984 new_mask |= 1 << i;
1985 break;
1986 case -ENOENT:
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001987 /* IDENTIFY was issued to non-existent
1988 * device. No need to reset. Just
1989 * thaw and kill the device.
1990 */
1991 ata_eh_thaw_port(ap);
1992 dev->class = ATA_DEV_UNKNOWN;
Tejun Heo084fe632006-05-31 18:28:03 +09001993 break;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09001994 default:
1995 dev->class = ATA_DEV_UNKNOWN;
1996 goto err;
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001997 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001998 }
1999 }
2000
Tejun Heoc1c4e8d2007-04-23 02:05:53 +09002001 /* PDIAG- should have been released, ask cable type if post-reset */
2002 if ((ehc->i.flags & ATA_EHI_DID_RESET) && ap->ops->cable_detect)
2003 ap->cbl = ap->ops->cable_detect(ap);
2004
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002005 /* Configure new devices forward such that user doesn't see
2006 * device detection messages backwards.
2007 */
2008 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002009 dev = &ap->link.device[i];
Tejun Heo022bdb02006-05-15 20:58:22 +09002010
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002011 if (!(new_mask & (1 << i)))
2012 continue;
2013
2014 ehc->i.flags |= ATA_EHI_PRINTINFO;
2015 rc = ata_dev_configure(dev);
2016 ehc->i.flags &= ~ATA_EHI_PRINTINFO;
2017 if (rc)
2018 goto err;
2019
2020 spin_lock_irqsave(ap->lock, flags);
2021 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
2022 spin_unlock_irqrestore(ap->lock, flags);
2023
2024 /* new device discovered, configure xfermode */
2025 ehc->i.flags |= ATA_EHI_SETMODE;
2026 }
2027
2028 return 0;
2029
2030 err:
2031 *r_failed_dev = dev;
2032 DPRINTK("EXIT rc=%d\n", rc);
Tejun Heo022bdb02006-05-15 20:58:22 +09002033 return rc;
2034}
2035
2036static int ata_port_nr_enabled(struct ata_port *ap)
2037{
2038 int i, cnt = 0;
2039
2040 for (i = 0; i < ATA_MAX_DEVICES; i++)
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002041 if (ata_dev_enabled(&ap->link.device[i]))
Tejun Heo022bdb02006-05-15 20:58:22 +09002042 cnt++;
2043 return cnt;
2044}
2045
Tejun Heo084fe632006-05-31 18:28:03 +09002046static int ata_port_nr_vacant(struct ata_port *ap)
2047{
2048 int i, cnt = 0;
2049
2050 for (i = 0; i < ATA_MAX_DEVICES; i++)
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002051 if (ap->link.device[i].class == ATA_DEV_UNKNOWN)
Tejun Heo084fe632006-05-31 18:28:03 +09002052 cnt++;
2053 return cnt;
2054}
2055
2056static int ata_eh_skip_recovery(struct ata_port *ap)
2057{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002058 struct ata_eh_context *ehc = &ap->link.eh_context;
Tejun Heo084fe632006-05-31 18:28:03 +09002059 int i;
2060
Tejun Heo7c8c2cf2006-07-10 23:18:23 +09002061 /* thaw frozen port, resume link and recover failed devices */
2062 if ((ap->pflags & ATA_PFLAG_FROZEN) ||
2063 (ehc->i.flags & ATA_EHI_RESUME_LINK) || ata_port_nr_enabled(ap))
Tejun Heo084fe632006-05-31 18:28:03 +09002064 return 0;
2065
2066 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
2067 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002068 struct ata_device *dev = &ap->link.device[i];
Tejun Heo084fe632006-05-31 18:28:03 +09002069
2070 if (dev->class == ATA_DEV_UNKNOWN &&
2071 ehc->classes[dev->devno] != ATA_DEV_NONE)
2072 return 0;
2073 }
2074
2075 return 1;
2076}
2077
Tejun Heofee7ca72007-07-01 19:05:58 +09002078static void ata_eh_handle_dev_fail(struct ata_device *dev, int err)
2079{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002080 struct ata_port *ap = dev->link->ap;
2081 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heofee7ca72007-07-01 19:05:58 +09002082
2083 ehc->tries[dev->devno]--;
2084
2085 switch (err) {
2086 case -ENODEV:
2087 /* device missing or wrong IDENTIFY data, schedule probing */
2088 ehc->i.probe_mask |= (1 << dev->devno);
2089 case -EINVAL:
2090 /* give it just one more chance */
2091 ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
2092 case -EIO:
2093 if (ehc->tries[dev->devno] == 1) {
2094 /* This is the last chance, better to slow
2095 * down than lose it.
2096 */
2097 sata_down_spd_limit(ap);
2098 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2099 }
2100 }
2101
2102 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2103 /* disable device if it has used up all its chances */
2104 ata_dev_disable(dev);
2105
2106 /* detach if offline */
2107 if (ata_port_offline(ap))
2108 ata_eh_detach_dev(dev);
2109
2110 /* probe if requested */
2111 if ((ehc->i.probe_mask & (1 << dev->devno)) &&
2112 !(ehc->did_probe_mask & (1 << dev->devno))) {
2113 ata_eh_detach_dev(dev);
2114 ata_dev_init(dev);
2115
2116 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2117 ehc->did_probe_mask |= (1 << dev->devno);
2118 ehc->i.action |= ATA_EH_SOFTRESET;
2119 }
2120 } else {
2121 /* soft didn't work? be haaaaard */
2122 if (ehc->i.flags & ATA_EHI_DID_RESET)
2123 ehc->i.action |= ATA_EH_HARDRESET;
2124 else
2125 ehc->i.action |= ATA_EH_SOFTRESET;
2126 }
2127}
2128
Tejun Heo022bdb02006-05-15 20:58:22 +09002129/**
2130 * ata_eh_recover - recover host port after error
2131 * @ap: host port to recover
Tejun Heof5914a42006-05-31 18:27:48 +09002132 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09002133 * @softreset: softreset method (can be NULL)
2134 * @hardreset: hardreset method (can be NULL)
2135 * @postreset: postreset method (can be NULL)
2136 *
2137 * This is the alpha and omega, eum and yang, heart and soul of
2138 * libata exception handling. On entry, actions required to
Tejun Heo084fe632006-05-31 18:28:03 +09002139 * recover the port and hotplug requests are recorded in
2140 * eh_context. This function executes all the operations with
2141 * appropriate retrials and fallbacks to resurrect failed
2142 * devices, detach goners and greet newcomers.
Tejun Heo022bdb02006-05-15 20:58:22 +09002143 *
2144 * LOCKING:
2145 * Kernel thread context (may sleep).
2146 *
2147 * RETURNS:
2148 * 0 on success, -errno on failure.
2149 */
Tejun Heof5914a42006-05-31 18:27:48 +09002150static int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
2151 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
Tejun Heo022bdb02006-05-15 20:58:22 +09002152 ata_postreset_fn_t postreset)
2153{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002154 struct ata_eh_context *ehc = &ap->link.eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09002155 struct ata_device *dev;
Tejun Heo4ae72a12007-02-02 16:22:30 +09002156 int i, rc;
Tejun Heo022bdb02006-05-15 20:58:22 +09002157
2158 DPRINTK("ENTER\n");
2159
2160 /* prep for recovery */
2161 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002162 dev = &ap->link.device[i];
Tejun Heo022bdb02006-05-15 20:58:22 +09002163
2164 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
Tejun Heo084fe632006-05-31 18:28:03 +09002165
Tejun Heo79a55b72007-01-18 17:22:18 +09002166 /* collect port action mask recorded in dev actions */
2167 ehc->i.action |= ehc->i.dev_action[i] & ~ATA_EH_PERDEV_MASK;
2168 ehc->i.dev_action[i] &= ATA_EH_PERDEV_MASK;
2169
Tejun Heo084fe632006-05-31 18:28:03 +09002170 /* process hotplug request */
2171 if (dev->flags & ATA_DFLAG_DETACH)
2172 ata_eh_detach_dev(dev);
2173
2174 if (!ata_dev_enabled(dev) &&
2175 ((ehc->i.probe_mask & (1 << dev->devno)) &&
2176 !(ehc->did_probe_mask & (1 << dev->devno)))) {
2177 ata_eh_detach_dev(dev);
2178 ata_dev_init(dev);
2179 ehc->did_probe_mask |= (1 << dev->devno);
2180 ehc->i.action |= ATA_EH_SOFTRESET;
2181 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002182 }
2183
2184 retry:
Tejun Heo022bdb02006-05-15 20:58:22 +09002185 rc = 0;
2186
Tejun Heoaeb2ecd2006-06-12 14:11:43 +09002187 /* if UNLOADING, finish immediately */
Tejun Heob51e9e52006-06-29 01:29:30 +09002188 if (ap->pflags & ATA_PFLAG_UNLOADING)
Tejun Heoaeb2ecd2006-06-12 14:11:43 +09002189 goto out;
2190
Tejun Heo022bdb02006-05-15 20:58:22 +09002191 /* skip EH if possible. */
Tejun Heo084fe632006-05-31 18:28:03 +09002192 if (ata_eh_skip_recovery(ap))
Tejun Heo022bdb02006-05-15 20:58:22 +09002193 ehc->i.action = 0;
2194
Tejun Heo084fe632006-05-31 18:28:03 +09002195 for (i = 0; i < ATA_MAX_DEVICES; i++)
2196 ehc->classes[i] = ATA_DEV_UNKNOWN;
2197
Tejun Heo022bdb02006-05-15 20:58:22 +09002198 /* reset */
2199 if (ehc->i.action & ATA_EH_RESET_MASK) {
2200 ata_eh_freeze_port(ap);
2201
Tejun Heo084fe632006-05-31 18:28:03 +09002202 rc = ata_eh_reset(ap, ata_port_nr_vacant(ap), prereset,
2203 softreset, hardreset, postreset);
Tejun Heo022bdb02006-05-15 20:58:22 +09002204 if (rc) {
2205 ata_port_printk(ap, KERN_ERR,
2206 "reset failed, giving up\n");
2207 goto out;
2208 }
2209
2210 ata_eh_thaw_port(ap);
2211 }
2212
Tejun Heo084fe632006-05-31 18:28:03 +09002213 /* revalidate existing devices and attach new ones */
2214 rc = ata_eh_revalidate_and_attach(ap, &dev);
Tejun Heo022bdb02006-05-15 20:58:22 +09002215 if (rc)
2216 goto dev_fail;
2217
Tejun Heobaa1e782006-11-01 18:39:27 +09002218 /* configure transfer mode if necessary */
2219 if (ehc->i.flags & ATA_EHI_SETMODE) {
Tejun Heo022bdb02006-05-15 20:58:22 +09002220 rc = ata_set_mode(ap, &dev);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002221 if (rc)
Tejun Heo022bdb02006-05-15 20:58:22 +09002222 goto dev_fail;
Tejun Heobaa1e782006-11-01 18:39:27 +09002223 ehc->i.flags &= ~ATA_EHI_SETMODE;
Tejun Heo022bdb02006-05-15 20:58:22 +09002224 }
2225
2226 goto out;
2227
2228 dev_fail:
Tejun Heofee7ca72007-07-01 19:05:58 +09002229 ata_eh_handle_dev_fail(dev, rc);
Tejun Heo022bdb02006-05-15 20:58:22 +09002230
2231 if (ata_port_nr_enabled(ap)) {
2232 ata_port_printk(ap, KERN_WARNING, "failed to recover some "
2233 "devices, retrying in 5 secs\n");
2234 ssleep(5);
2235 } else {
2236 /* no device left, repeat fast */
2237 msleep(500);
2238 }
2239
2240 goto retry;
2241
2242 out:
2243 if (rc) {
2244 for (i = 0; i < ATA_MAX_DEVICES; i++)
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002245 ata_dev_disable(&ap->link.device[i]);
Tejun Heo022bdb02006-05-15 20:58:22 +09002246 }
2247
2248 DPRINTK("EXIT, rc=%d\n", rc);
2249 return rc;
2250}
2251
2252/**
2253 * ata_eh_finish - finish up EH
2254 * @ap: host port to finish EH for
2255 *
2256 * Recovery is complete. Clean up EH states and retry or finish
2257 * failed qcs.
2258 *
2259 * LOCKING:
2260 * None.
2261 */
2262static void ata_eh_finish(struct ata_port *ap)
2263{
2264 int tag;
2265
2266 /* retry or finish qcs */
2267 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2268 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2269
2270 if (!(qc->flags & ATA_QCFLAG_FAILED))
2271 continue;
2272
2273 if (qc->err_mask) {
2274 /* FIXME: Once EH migration is complete,
2275 * generate sense data in this function,
2276 * considering both err_mask and tf.
2277 */
2278 if (qc->err_mask & AC_ERR_INVALID)
2279 ata_eh_qc_complete(qc);
2280 else
2281 ata_eh_qc_retry(qc);
2282 } else {
2283 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2284 ata_eh_qc_complete(qc);
2285 } else {
2286 /* feed zero TF to sense generation */
2287 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2288 ata_eh_qc_retry(qc);
2289 }
2290 }
2291 }
2292}
2293
2294/**
2295 * ata_do_eh - do standard error handling
2296 * @ap: host port to handle error for
Tejun Heof5914a42006-05-31 18:27:48 +09002297 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09002298 * @softreset: softreset method (can be NULL)
2299 * @hardreset: hardreset method (can be NULL)
2300 * @postreset: postreset method (can be NULL)
2301 *
2302 * Perform standard error handling sequence.
2303 *
2304 * LOCKING:
2305 * Kernel thread context (may sleep).
2306 */
Tejun Heof5914a42006-05-31 18:27:48 +09002307void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2308 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2309 ata_postreset_fn_t postreset)
Tejun Heo022bdb02006-05-15 20:58:22 +09002310{
Tejun Heo1cdaf532006-07-03 16:07:26 +09002311 ata_eh_autopsy(ap);
2312 ata_eh_report(ap);
Tejun Heof5914a42006-05-31 18:27:48 +09002313 ata_eh_recover(ap, prereset, softreset, hardreset, postreset);
Tejun Heo022bdb02006-05-15 20:58:22 +09002314 ata_eh_finish(ap);
2315}
Tejun Heo500530f2006-07-03 16:07:27 +09002316
Tejun Heo6ffa01d2007-03-02 17:32:47 +09002317#ifdef CONFIG_PM
Tejun Heo500530f2006-07-03 16:07:27 +09002318/**
2319 * ata_eh_handle_port_suspend - perform port suspend operation
2320 * @ap: port to suspend
2321 *
2322 * Suspend @ap.
2323 *
2324 * LOCKING:
2325 * Kernel thread context (may sleep).
2326 */
2327static void ata_eh_handle_port_suspend(struct ata_port *ap)
2328{
2329 unsigned long flags;
2330 int rc = 0;
2331
2332 /* are we suspending? */
2333 spin_lock_irqsave(ap->lock, flags);
2334 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2335 ap->pm_mesg.event == PM_EVENT_ON) {
2336 spin_unlock_irqrestore(ap->lock, flags);
2337 return;
2338 }
2339 spin_unlock_irqrestore(ap->lock, flags);
2340
2341 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
2342
Tejun Heo64578a3de2007-05-15 03:28:16 +09002343 /* tell ACPI we're suspending */
2344 rc = ata_acpi_on_suspend(ap);
2345 if (rc)
2346 goto out;
2347
Tejun Heo500530f2006-07-03 16:07:27 +09002348 /* suspend */
2349 ata_eh_freeze_port(ap);
2350
2351 if (ap->ops->port_suspend)
2352 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
2353
Tejun Heo64578a3de2007-05-15 03:28:16 +09002354 out:
Tejun Heo500530f2006-07-03 16:07:27 +09002355 /* report result */
2356 spin_lock_irqsave(ap->lock, flags);
2357
2358 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
2359 if (rc == 0)
2360 ap->pflags |= ATA_PFLAG_SUSPENDED;
Tejun Heo64578a3de2007-05-15 03:28:16 +09002361 else if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heo500530f2006-07-03 16:07:27 +09002362 ata_port_schedule_eh(ap);
2363
2364 if (ap->pm_result) {
2365 *ap->pm_result = rc;
2366 ap->pm_result = NULL;
2367 }
2368
2369 spin_unlock_irqrestore(ap->lock, flags);
2370
2371 return;
2372}
2373
2374/**
2375 * ata_eh_handle_port_resume - perform port resume operation
2376 * @ap: port to resume
2377 *
2378 * Resume @ap.
2379 *
Tejun Heo500530f2006-07-03 16:07:27 +09002380 * LOCKING:
2381 * Kernel thread context (may sleep).
2382 */
2383static void ata_eh_handle_port_resume(struct ata_port *ap)
2384{
Tejun Heo500530f2006-07-03 16:07:27 +09002385 unsigned long flags;
Tejun Heo9666f402007-05-04 21:27:47 +02002386 int rc = 0;
Tejun Heo500530f2006-07-03 16:07:27 +09002387
2388 /* are we resuming? */
2389 spin_lock_irqsave(ap->lock, flags);
2390 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2391 ap->pm_mesg.event != PM_EVENT_ON) {
2392 spin_unlock_irqrestore(ap->lock, flags);
2393 return;
2394 }
2395 spin_unlock_irqrestore(ap->lock, flags);
2396
Tejun Heo9666f402007-05-04 21:27:47 +02002397 WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
Tejun Heo500530f2006-07-03 16:07:27 +09002398
2399 if (ap->ops->port_resume)
2400 rc = ap->ops->port_resume(ap);
2401
Tejun Heo67465442007-05-15 03:28:16 +09002402 /* tell ACPI that we're resuming */
2403 ata_acpi_on_resume(ap);
2404
Tejun Heo9666f402007-05-04 21:27:47 +02002405 /* report result */
Tejun Heo500530f2006-07-03 16:07:27 +09002406 spin_lock_irqsave(ap->lock, flags);
2407 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
2408 if (ap->pm_result) {
2409 *ap->pm_result = rc;
2410 ap->pm_result = NULL;
2411 }
2412 spin_unlock_irqrestore(ap->lock, flags);
2413}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09002414#endif /* CONFIG_PM */