blob: 412d6049afa360acc386892b1aa5fbd8e2cf930b [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,
59};
60
61/* The following table determines how we sequence resets. Each entry
62 * represents timeout for that try. The first try can be soft or
63 * hardreset. All others are hardreset if available. In most cases
64 * the first reset w/ 10sec timeout should succeed. Following entries
65 * are mostly for error handling, hotplug and retarded devices.
66 */
67static const unsigned long ata_eh_reset_timeouts[] = {
68 10 * HZ, /* most drives spin up by 10sec */
69 10 * HZ, /* > 99% working drives spin up before 20sec */
70 35 * HZ, /* give > 30 secs of idleness for retarded devices */
71 5 * HZ, /* and sweet one last chance */
72 /* > 1 min has elapsed, give up */
73};
74
Tejun Heoad9e2762006-05-15 20:58:12 +090075static void __ata_port_freeze(struct ata_port *ap);
Tejun Heo720ba122006-05-31 18:28:13 +090076static void ata_eh_finish(struct ata_port *ap);
Tejun Heo6ffa01d2007-03-02 17:32:47 +090077#ifdef CONFIG_PM
Tejun Heo500530f2006-07-03 16:07:27 +090078static void ata_eh_handle_port_suspend(struct ata_port *ap);
79static void ata_eh_handle_port_resume(struct ata_port *ap);
Tejun Heo6ffa01d2007-03-02 17:32:47 +090080#else /* CONFIG_PM */
81static void ata_eh_handle_port_suspend(struct ata_port *ap)
82{ }
83
84static void ata_eh_handle_port_resume(struct ata_port *ap)
85{ }
Tejun Heo6ffa01d2007-03-02 17:32:47 +090086#endif /* CONFIG_PM */
Tejun Heoad9e2762006-05-15 20:58:12 +090087
Tejun Heo0c247c52006-05-15 20:58:19 +090088static void ata_ering_record(struct ata_ering *ering, int is_io,
89 unsigned int err_mask)
90{
91 struct ata_ering_entry *ent;
92
93 WARN_ON(!err_mask);
94
95 ering->cursor++;
96 ering->cursor %= ATA_ERING_SIZE;
97
98 ent = &ering->ring[ering->cursor];
99 ent->is_io = is_io;
100 ent->err_mask = err_mask;
101 ent->timestamp = get_jiffies_64();
102}
103
Tejun Heo7d47e8d2007-02-02 16:22:31 +0900104static void ata_ering_clear(struct ata_ering *ering)
Tejun Heo0c247c52006-05-15 20:58:19 +0900105{
Tejun Heo7d47e8d2007-02-02 16:22:31 +0900106 memset(ering, 0, sizeof(*ering));
Tejun Heo0c247c52006-05-15 20:58:19 +0900107}
108
109static int ata_ering_map(struct ata_ering *ering,
110 int (*map_fn)(struct ata_ering_entry *, void *),
111 void *arg)
112{
113 int idx, rc = 0;
114 struct ata_ering_entry *ent;
115
116 idx = ering->cursor;
117 do {
118 ent = &ering->ring[idx];
119 if (!ent->err_mask)
120 break;
121 rc = map_fn(ent, arg);
122 if (rc)
123 break;
124 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
125 } while (idx != ering->cursor);
126
127 return rc;
128}
129
Tejun Heo64f65ca2006-06-24 20:30:18 +0900130static unsigned int ata_eh_dev_action(struct ata_device *dev)
131{
132 struct ata_eh_context *ehc = &dev->ap->eh_context;
133
134 return ehc->i.action | ehc->i.dev_action[dev->devno];
135}
136
Tejun Heoaf181c22006-06-24 20:30:18 +0900137static void ata_eh_clear_action(struct ata_device *dev,
138 struct ata_eh_info *ehi, unsigned int action)
139{
140 int i;
141
142 if (!dev) {
143 ehi->action &= ~action;
144 for (i = 0; i < ATA_MAX_DEVICES; i++)
145 ehi->dev_action[i] &= ~action;
146 } else {
147 /* doesn't make sense for port-wide EH actions */
148 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
149
150 /* break ehi->action into ehi->dev_action */
151 if (ehi->action & action) {
152 for (i = 0; i < ATA_MAX_DEVICES; i++)
153 ehi->dev_action[i] |= ehi->action & action;
154 ehi->action &= ~action;
155 }
156
157 /* turn off the specified per-dev action */
158 ehi->dev_action[dev->devno] &= ~action;
159 }
160}
161
Tejun Heoece1d632006-04-02 18:51:53 +0900162/**
163 * ata_scsi_timed_out - SCSI layer time out callback
164 * @cmd: timed out SCSI command
165 *
166 * Handles SCSI layer timeout. We race with normal completion of
167 * the qc for @cmd. If the qc is already gone, we lose and let
168 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
169 * timed out and EH should be invoked. Prevent ata_qc_complete()
170 * from finishing it by setting EH_SCHEDULED and return
171 * EH_NOT_HANDLED.
172 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900173 * TODO: kill this function once old EH is gone.
174 *
Tejun Heoece1d632006-04-02 18:51:53 +0900175 * LOCKING:
176 * Called from timer context
177 *
178 * RETURNS:
179 * EH_HANDLED or EH_NOT_HANDLED
180 */
181enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
182{
183 struct Scsi_Host *host = cmd->device->host;
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400184 struct ata_port *ap = ata_shost_to_port(host);
Tejun Heoece1d632006-04-02 18:51:53 +0900185 unsigned long flags;
186 struct ata_queued_cmd *qc;
Tejun Heoad9e2762006-05-15 20:58:12 +0900187 enum scsi_eh_timer_return ret;
Tejun Heoece1d632006-04-02 18:51:53 +0900188
189 DPRINTK("ENTER\n");
190
Tejun Heoad9e2762006-05-15 20:58:12 +0900191 if (ap->ops->error_handler) {
192 ret = EH_NOT_HANDLED;
193 goto out;
194 }
195
196 ret = EH_HANDLED;
Jeff Garzikba6a1302006-06-22 23:46:10 -0400197 spin_lock_irqsave(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900198 qc = ata_qc_from_tag(ap, ap->active_tag);
199 if (qc) {
200 WARN_ON(qc->scsicmd != cmd);
201 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
202 qc->err_mask |= AC_ERR_TIMEOUT;
203 ret = EH_NOT_HANDLED;
204 }
Jeff Garzikba6a1302006-06-22 23:46:10 -0400205 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900206
Tejun Heoad9e2762006-05-15 20:58:12 +0900207 out:
Tejun Heoece1d632006-04-02 18:51:53 +0900208 DPRINTK("EXIT, ret=%d\n", ret);
209 return ret;
210}
211
212/**
213 * ata_scsi_error - SCSI layer error handler callback
214 * @host: SCSI host on which error occurred
215 *
216 * Handles SCSI-layer-thrown error events.
217 *
218 * LOCKING:
219 * Inherited from SCSI layer (none, can sleep)
220 *
221 * RETURNS:
222 * Zero.
223 */
Jeff Garzik381544b2006-04-11 13:04:39 -0400224void ata_scsi_error(struct Scsi_Host *host)
Tejun Heoece1d632006-04-02 18:51:53 +0900225{
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400226 struct ata_port *ap = ata_shost_to_port(host);
Tejun Heoad9e2762006-05-15 20:58:12 +0900227 int i, repeat_cnt = ATA_EH_MAX_REPEAT;
228 unsigned long flags;
Tejun Heoece1d632006-04-02 18:51:53 +0900229
230 DPRINTK("ENTER\n");
231
Tejun Heoad9e2762006-05-15 20:58:12 +0900232 /* synchronize with port task */
Tejun Heoece1d632006-04-02 18:51:53 +0900233 ata_port_flush_task(ap);
234
Jeff Garzikcca39742006-08-24 03:19:22 -0400235 /* synchronize with host lock and sort out timeouts */
Tejun Heoece1d632006-04-02 18:51:53 +0900236
Tejun Heoad9e2762006-05-15 20:58:12 +0900237 /* For new EH, all qcs are finished in one of three ways -
238 * normal completion, error completion, and SCSI timeout.
239 * Both cmpletions can race against SCSI timeout. When normal
240 * completion wins, the qc never reaches EH. When error
241 * completion wins, the qc has ATA_QCFLAG_FAILED set.
242 *
243 * When SCSI timeout wins, things are a bit more complex.
244 * Normal or error completion can occur after the timeout but
245 * before this point. In such cases, both types of
246 * completions are honored. A scmd is determined to have
247 * timed out iff its associated qc is active and not failed.
248 */
249 if (ap->ops->error_handler) {
250 struct scsi_cmnd *scmd, *tmp;
251 int nr_timedout = 0;
Tejun Heoece1d632006-04-02 18:51:53 +0900252
Tejun Heoe30349d2006-07-03 03:02:15 +0900253 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900254
255 list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
256 struct ata_queued_cmd *qc;
257
258 for (i = 0; i < ATA_MAX_QUEUE; i++) {
259 qc = __ata_qc_from_tag(ap, i);
260 if (qc->flags & ATA_QCFLAG_ACTIVE &&
261 qc->scsicmd == scmd)
262 break;
263 }
264
265 if (i < ATA_MAX_QUEUE) {
266 /* the scmd has an associated qc */
267 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
268 /* which hasn't failed yet, timeout */
269 qc->err_mask |= AC_ERR_TIMEOUT;
270 qc->flags |= ATA_QCFLAG_FAILED;
271 nr_timedout++;
272 }
273 } else {
274 /* Normal completion occurred after
275 * SCSI timeout but before this point.
276 * Successfully complete it.
277 */
278 scmd->retries = scmd->allowed;
279 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
280 }
281 }
282
283 /* If we have timed out qcs. They belong to EH from
284 * this point but the state of the controller is
285 * unknown. Freeze the port to make sure the IRQ
286 * handler doesn't diddle with those qcs. This must
287 * be done atomically w.r.t. setting QCFLAG_FAILED.
288 */
289 if (nr_timedout)
290 __ata_port_freeze(ap);
291
Tejun Heoe30349d2006-07-03 03:02:15 +0900292 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900293 } else
Tejun Heoe30349d2006-07-03 03:02:15 +0900294 spin_unlock_wait(ap->lock);
Tejun Heoad9e2762006-05-15 20:58:12 +0900295
296 repeat:
297 /* invoke error handler */
298 if (ap->ops->error_handler) {
Tejun Heo500530f2006-07-03 16:07:27 +0900299 /* process port resume request */
300 ata_eh_handle_port_resume(ap);
301
Tejun Heof3e81b12006-05-15 20:58:21 +0900302 /* fetch & clear EH info */
Tejun Heoe30349d2006-07-03 03:02:15 +0900303 spin_lock_irqsave(ap->lock, flags);
Tejun Heof3e81b12006-05-15 20:58:21 +0900304
305 memset(&ap->eh_context, 0, sizeof(ap->eh_context));
306 ap->eh_context.i = ap->eh_info;
307 memset(&ap->eh_info, 0, sizeof(ap->eh_info));
308
Tejun Heob51e9e52006-06-29 01:29:30 +0900309 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
310 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
Tejun Heof3e81b12006-05-15 20:58:21 +0900311
Tejun Heoe30349d2006-07-03 03:02:15 +0900312 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900313
Tejun Heo500530f2006-07-03 16:07:27 +0900314 /* invoke EH, skip if unloading or suspended */
315 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
Tejun Heo720ba122006-05-31 18:28:13 +0900316 ap->ops->error_handler(ap);
317 else
318 ata_eh_finish(ap);
Tejun Heoad9e2762006-05-15 20:58:12 +0900319
Tejun Heo500530f2006-07-03 16:07:27 +0900320 /* process port suspend request */
321 ata_eh_handle_port_suspend(ap);
322
Tejun Heoad9e2762006-05-15 20:58:12 +0900323 /* Exception might have happend after ->error_handler
324 * recovered the port but before this point. Repeat
325 * EH in such case.
326 */
Tejun Heoe30349d2006-07-03 03:02:15 +0900327 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900328
Tejun Heob51e9e52006-06-29 01:29:30 +0900329 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
Tejun Heoad9e2762006-05-15 20:58:12 +0900330 if (--repeat_cnt) {
331 ata_port_printk(ap, KERN_INFO,
332 "EH pending after completion, "
333 "repeating EH (cnt=%d)\n", repeat_cnt);
Tejun Heoe30349d2006-07-03 03:02:15 +0900334 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900335 goto repeat;
336 }
337 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
338 "tries, giving up\n", ATA_EH_MAX_REPEAT);
339 }
340
Tejun Heof3e81b12006-05-15 20:58:21 +0900341 /* this run is complete, make sure EH info is clear */
342 memset(&ap->eh_info, 0, sizeof(ap->eh_info));
343
Tejun Heoe30349d2006-07-03 03:02:15 +0900344 /* Clear host_eh_scheduled while holding ap->lock such
Tejun Heoad9e2762006-05-15 20:58:12 +0900345 * that if exception occurs after this point but
346 * before EH completion, SCSI midlayer will
347 * re-initiate EH.
348 */
349 host->host_eh_scheduled = 0;
350
Tejun Heoe30349d2006-07-03 03:02:15 +0900351 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900352 } else {
353 WARN_ON(ata_qc_from_tag(ap, ap->active_tag) == NULL);
354 ap->ops->eng_timeout(ap);
355 }
356
357 /* finish or retry handled scmd's and clean up */
Tejun Heoece1d632006-04-02 18:51:53 +0900358 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
359
360 scsi_eh_flush_done_q(&ap->eh_done_q);
361
Tejun Heoad9e2762006-05-15 20:58:12 +0900362 /* clean up */
Tejun Heoe30349d2006-07-03 03:02:15 +0900363 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900364
Tejun Heo1cdaf532006-07-03 16:07:26 +0900365 if (ap->pflags & ATA_PFLAG_LOADING)
Tejun Heob51e9e52006-06-29 01:29:30 +0900366 ap->pflags &= ~ATA_PFLAG_LOADING;
Tejun Heo1cdaf532006-07-03 16:07:26 +0900367 else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
David Howells52bad642006-11-22 14:54:01 +0000368 queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0);
Tejun Heo1cdaf532006-07-03 16:07:26 +0900369
370 if (ap->pflags & ATA_PFLAG_RECOVERED)
371 ata_port_printk(ap, KERN_INFO, "EH complete\n");
Tejun Heo580b2102006-05-31 18:28:05 +0900372
Tejun Heob51e9e52006-06-29 01:29:30 +0900373 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
Tejun Heoad9e2762006-05-15 20:58:12 +0900374
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900375 /* tell wait_eh that we're done */
Tejun Heob51e9e52006-06-29 01:29:30 +0900376 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900377 wake_up_all(&ap->eh_wait_q);
378
Tejun Heoe30349d2006-07-03 03:02:15 +0900379 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900380
Tejun Heoece1d632006-04-02 18:51:53 +0900381 DPRINTK("EXIT\n");
Tejun Heoece1d632006-04-02 18:51:53 +0900382}
383
384/**
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900385 * ata_port_wait_eh - Wait for the currently pending EH to complete
386 * @ap: Port to wait EH for
387 *
388 * Wait until the currently pending EH is complete.
389 *
390 * LOCKING:
391 * Kernel thread context (may sleep).
392 */
393void ata_port_wait_eh(struct ata_port *ap)
394{
395 unsigned long flags;
396 DEFINE_WAIT(wait);
397
398 retry:
Jeff Garzikba6a1302006-06-22 23:46:10 -0400399 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900400
Tejun Heob51e9e52006-06-29 01:29:30 +0900401 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900402 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
Jeff Garzikba6a1302006-06-22 23:46:10 -0400403 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900404 schedule();
Jeff Garzikba6a1302006-06-22 23:46:10 -0400405 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900406 }
Tejun Heo0a1b6222006-06-11 11:01:38 +0900407 finish_wait(&ap->eh_wait_q, &wait);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900408
Jeff Garzikba6a1302006-06-22 23:46:10 -0400409 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900410
411 /* make sure SCSI EH is complete */
Jeff Garzikcca39742006-08-24 03:19:22 -0400412 if (scsi_host_in_recovery(ap->scsi_host)) {
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900413 msleep(10);
414 goto retry;
415 }
416}
417
418/**
Tejun Heoece1d632006-04-02 18:51:53 +0900419 * ata_qc_timeout - Handle timeout of queued command
420 * @qc: Command that timed out
421 *
422 * Some part of the kernel (currently, only the SCSI layer)
423 * has noticed that the active command on port @ap has not
424 * completed after a specified length of time. Handle this
425 * condition by disabling DMA (if necessary) and completing
426 * transactions, with error if necessary.
427 *
428 * This also handles the case of the "lost interrupt", where
429 * for some reason (possibly hardware bug, possibly driver bug)
430 * an interrupt was not delivered to the driver, even though the
431 * transaction completed successfully.
432 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900433 * TODO: kill this function once old EH is gone.
434 *
Tejun Heoece1d632006-04-02 18:51:53 +0900435 * LOCKING:
436 * Inherited from SCSI layer (none, can sleep)
437 */
438static void ata_qc_timeout(struct ata_queued_cmd *qc)
439{
440 struct ata_port *ap = qc->ap;
Tejun Heoece1d632006-04-02 18:51:53 +0900441 u8 host_stat = 0, drv_stat;
442 unsigned long flags;
443
444 DPRINTK("ENTER\n");
445
446 ap->hsm_task_state = HSM_ST_IDLE;
447
Jeff Garzikba6a1302006-06-22 23:46:10 -0400448 spin_lock_irqsave(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900449
450 switch (qc->tf.protocol) {
451
452 case ATA_PROT_DMA:
453 case ATA_PROT_ATAPI_DMA:
454 host_stat = ap->ops->bmdma_status(ap);
455
456 /* before we do anything else, clear DMA-Start bit */
457 ap->ops->bmdma_stop(qc);
458
459 /* fall through */
460
461 default:
462 ata_altstatus(ap);
463 drv_stat = ata_chk_status(ap);
464
465 /* ack bmdma irq events */
466 ap->ops->irq_clear(ap);
467
Tejun Heof15a1da2006-05-15 20:57:56 +0900468 ata_dev_printk(qc->dev, KERN_ERR, "command 0x%x timeout, "
469 "stat 0x%x host_stat 0x%x\n",
470 qc->tf.command, drv_stat, host_stat);
Tejun Heoece1d632006-04-02 18:51:53 +0900471
472 /* complete taskfile transaction */
Jeff Garzikc13b56a2006-04-02 10:34:24 -0400473 qc->err_mask |= AC_ERR_TIMEOUT;
Tejun Heoece1d632006-04-02 18:51:53 +0900474 break;
475 }
476
Jeff Garzikba6a1302006-06-22 23:46:10 -0400477 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900478
479 ata_eh_qc_complete(qc);
480
481 DPRINTK("EXIT\n");
482}
483
484/**
485 * ata_eng_timeout - Handle timeout of queued command
486 * @ap: Port on which timed-out command is active
487 *
488 * Some part of the kernel (currently, only the SCSI layer)
489 * has noticed that the active command on port @ap has not
490 * completed after a specified length of time. Handle this
491 * condition by disabling DMA (if necessary) and completing
492 * transactions, with error if necessary.
493 *
494 * This also handles the case of the "lost interrupt", where
495 * for some reason (possibly hardware bug, possibly driver bug)
496 * an interrupt was not delivered to the driver, even though the
497 * transaction completed successfully.
498 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900499 * TODO: kill this function once old EH is gone.
500 *
Tejun Heoece1d632006-04-02 18:51:53 +0900501 * LOCKING:
502 * Inherited from SCSI layer (none, can sleep)
503 */
504void ata_eng_timeout(struct ata_port *ap)
505{
506 DPRINTK("ENTER\n");
507
508 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
509
510 DPRINTK("EXIT\n");
511}
512
Tejun Heof686bcb2006-05-15 20:58:05 +0900513/**
514 * ata_qc_schedule_eh - schedule qc for error handling
515 * @qc: command to schedule error handling for
516 *
517 * Schedule error handling for @qc. EH will kick in as soon as
518 * other commands are drained.
519 *
520 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400521 * spin_lock_irqsave(host lock)
Tejun Heof686bcb2006-05-15 20:58:05 +0900522 */
523void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
524{
525 struct ata_port *ap = qc->ap;
526
527 WARN_ON(!ap->ops->error_handler);
528
529 qc->flags |= ATA_QCFLAG_FAILED;
Tejun Heob51e9e52006-06-29 01:29:30 +0900530 qc->ap->pflags |= ATA_PFLAG_EH_PENDING;
Tejun Heof686bcb2006-05-15 20:58:05 +0900531
532 /* The following will fail if timeout has already expired.
533 * ata_scsi_error() takes care of such scmds on EH entry.
534 * Note that ATA_QCFLAG_FAILED is unconditionally set after
535 * this function completes.
536 */
537 scsi_req_abort_cmd(qc->scsicmd);
538}
539
Tejun Heo7b70fc02006-05-15 20:58:07 +0900540/**
541 * ata_port_schedule_eh - schedule error handling without a qc
542 * @ap: ATA port to schedule EH for
543 *
544 * Schedule error handling for @ap. EH will kick in as soon as
545 * all commands are drained.
546 *
547 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400548 * spin_lock_irqsave(host lock)
Tejun Heo7b70fc02006-05-15 20:58:07 +0900549 */
550void ata_port_schedule_eh(struct ata_port *ap)
551{
552 WARN_ON(!ap->ops->error_handler);
553
Tejun Heob51e9e52006-06-29 01:29:30 +0900554 ap->pflags |= ATA_PFLAG_EH_PENDING;
Jeff Garzikcca39742006-08-24 03:19:22 -0400555 scsi_schedule_eh(ap->scsi_host);
Tejun Heo7b70fc02006-05-15 20:58:07 +0900556
557 DPRINTK("port EH scheduled\n");
558}
559
560/**
561 * ata_port_abort - abort all qc's on the port
562 * @ap: ATA port to abort qc's for
563 *
564 * Abort all active qc's of @ap and schedule EH.
565 *
566 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400567 * spin_lock_irqsave(host lock)
Tejun Heo7b70fc02006-05-15 20:58:07 +0900568 *
569 * RETURNS:
570 * Number of aborted qc's.
571 */
572int ata_port_abort(struct ata_port *ap)
573{
574 int tag, nr_aborted = 0;
575
576 WARN_ON(!ap->ops->error_handler);
577
578 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
579 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
580
581 if (qc) {
582 qc->flags |= ATA_QCFLAG_FAILED;
583 ata_qc_complete(qc);
584 nr_aborted++;
585 }
586 }
587
588 if (!nr_aborted)
589 ata_port_schedule_eh(ap);
590
591 return nr_aborted;
592}
593
Tejun Heoe3180492006-05-15 20:58:09 +0900594/**
595 * __ata_port_freeze - freeze port
596 * @ap: ATA port to freeze
597 *
598 * This function is called when HSM violation or some other
599 * condition disrupts normal operation of the port. Frozen port
600 * is not allowed to perform any operation until the port is
601 * thawed, which usually follows a successful reset.
602 *
603 * ap->ops->freeze() callback can be used for freezing the port
604 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
605 * port cannot be frozen hardware-wise, the interrupt handler
606 * must ack and clear interrupts unconditionally while the port
607 * is frozen.
608 *
609 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400610 * spin_lock_irqsave(host lock)
Tejun Heoe3180492006-05-15 20:58:09 +0900611 */
612static void __ata_port_freeze(struct ata_port *ap)
613{
614 WARN_ON(!ap->ops->error_handler);
615
616 if (ap->ops->freeze)
617 ap->ops->freeze(ap);
618
Tejun Heob51e9e52006-06-29 01:29:30 +0900619 ap->pflags |= ATA_PFLAG_FROZEN;
Tejun Heoe3180492006-05-15 20:58:09 +0900620
Tejun Heo44877b42007-02-21 01:06:51 +0900621 DPRINTK("ata%u port frozen\n", ap->print_id);
Tejun Heoe3180492006-05-15 20:58:09 +0900622}
623
624/**
625 * ata_port_freeze - abort & freeze port
626 * @ap: ATA port to freeze
627 *
628 * Abort and freeze @ap.
629 *
630 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400631 * spin_lock_irqsave(host lock)
Tejun Heoe3180492006-05-15 20:58:09 +0900632 *
633 * RETURNS:
634 * Number of aborted commands.
635 */
636int ata_port_freeze(struct ata_port *ap)
637{
638 int nr_aborted;
639
640 WARN_ON(!ap->ops->error_handler);
641
642 nr_aborted = ata_port_abort(ap);
643 __ata_port_freeze(ap);
644
645 return nr_aborted;
646}
647
648/**
649 * ata_eh_freeze_port - EH helper to freeze port
650 * @ap: ATA port to freeze
651 *
652 * Freeze @ap.
653 *
654 * LOCKING:
655 * None.
656 */
657void ata_eh_freeze_port(struct ata_port *ap)
658{
659 unsigned long flags;
660
661 if (!ap->ops->error_handler)
662 return;
663
Jeff Garzikba6a1302006-06-22 23:46:10 -0400664 spin_lock_irqsave(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900665 __ata_port_freeze(ap);
Jeff Garzikba6a1302006-06-22 23:46:10 -0400666 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900667}
668
669/**
670 * ata_port_thaw_port - EH helper to thaw port
671 * @ap: ATA port to thaw
672 *
673 * Thaw frozen port @ap.
674 *
675 * LOCKING:
676 * None.
677 */
678void ata_eh_thaw_port(struct ata_port *ap)
679{
680 unsigned long flags;
681
682 if (!ap->ops->error_handler)
683 return;
684
Jeff Garzikba6a1302006-06-22 23:46:10 -0400685 spin_lock_irqsave(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900686
Tejun Heob51e9e52006-06-29 01:29:30 +0900687 ap->pflags &= ~ATA_PFLAG_FROZEN;
Tejun Heoe3180492006-05-15 20:58:09 +0900688
689 if (ap->ops->thaw)
690 ap->ops->thaw(ap);
691
Jeff Garzikba6a1302006-06-22 23:46:10 -0400692 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900693
Tejun Heo44877b42007-02-21 01:06:51 +0900694 DPRINTK("ata%u port thawed\n", ap->print_id);
Tejun Heoe3180492006-05-15 20:58:09 +0900695}
696
Tejun Heoece1d632006-04-02 18:51:53 +0900697static void ata_eh_scsidone(struct scsi_cmnd *scmd)
698{
699 /* nada */
700}
701
702static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
703{
704 struct ata_port *ap = qc->ap;
705 struct scsi_cmnd *scmd = qc->scsicmd;
706 unsigned long flags;
707
Jeff Garzikba6a1302006-06-22 23:46:10 -0400708 spin_lock_irqsave(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900709 qc->scsidone = ata_eh_scsidone;
710 __ata_qc_complete(qc);
711 WARN_ON(ata_tag_valid(qc->tag));
Jeff Garzikba6a1302006-06-22 23:46:10 -0400712 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900713
714 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
715}
716
717/**
718 * ata_eh_qc_complete - Complete an active ATA command from EH
719 * @qc: Command to complete
720 *
721 * Indicate to the mid and upper layers that an ATA command has
722 * completed. To be used from EH.
723 */
724void ata_eh_qc_complete(struct ata_queued_cmd *qc)
725{
726 struct scsi_cmnd *scmd = qc->scsicmd;
727 scmd->retries = scmd->allowed;
728 __ata_eh_qc_complete(qc);
729}
730
731/**
732 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
733 * @qc: Command to retry
734 *
735 * Indicate to the mid and upper layers that an ATA command
736 * should be retried. To be used from EH.
737 *
738 * SCSI midlayer limits the number of retries to scmd->allowed.
739 * scmd->retries is decremented for commands which get retried
740 * due to unrelated failures (qc->err_mask is zero).
741 */
742void ata_eh_qc_retry(struct ata_queued_cmd *qc)
743{
744 struct scsi_cmnd *scmd = qc->scsicmd;
745 if (!qc->err_mask && scmd->retries)
746 scmd->retries--;
747 __ata_eh_qc_complete(qc);
748}
Tejun Heo022bdb02006-05-15 20:58:22 +0900749
750/**
Tejun Heo0ea035a2006-05-31 18:28:01 +0900751 * ata_eh_detach_dev - detach ATA device
752 * @dev: ATA device to detach
753 *
754 * Detach @dev.
755 *
756 * LOCKING:
757 * None.
758 */
759static void ata_eh_detach_dev(struct ata_device *dev)
760{
761 struct ata_port *ap = dev->ap;
762 unsigned long flags;
763
764 ata_dev_disable(dev);
765
Jeff Garzikba6a1302006-06-22 23:46:10 -0400766 spin_lock_irqsave(ap->lock, flags);
Tejun Heo0ea035a2006-05-31 18:28:01 +0900767
768 dev->flags &= ~ATA_DFLAG_DETACH;
769
770 if (ata_scsi_offline_dev(dev)) {
771 dev->flags |= ATA_DFLAG_DETACHED;
Tejun Heob51e9e52006-06-29 01:29:30 +0900772 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
Tejun Heo0ea035a2006-05-31 18:28:01 +0900773 }
774
Tejun Heobeb07c12006-06-24 20:30:19 +0900775 /* clear per-dev EH actions */
776 ata_eh_clear_action(dev, &ap->eh_info, ATA_EH_PERDEV_MASK);
777 ata_eh_clear_action(dev, &ap->eh_context.i, ATA_EH_PERDEV_MASK);
778
Jeff Garzikba6a1302006-06-22 23:46:10 -0400779 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo0ea035a2006-05-31 18:28:01 +0900780}
781
782/**
Tejun Heo022bdb02006-05-15 20:58:22 +0900783 * ata_eh_about_to_do - about to perform eh_action
784 * @ap: target ATA port
Tejun Heo47005f22006-06-19 18:27:23 +0900785 * @dev: target ATA dev for per-dev action (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +0900786 * @action: action about to be performed
787 *
788 * Called just before performing EH actions to clear related bits
789 * in @ap->eh_info such that eh actions are not unnecessarily
790 * repeated.
791 *
792 * LOCKING:
793 * None.
794 */
Tejun Heo47005f22006-06-19 18:27:23 +0900795static void ata_eh_about_to_do(struct ata_port *ap, struct ata_device *dev,
796 unsigned int action)
Tejun Heo022bdb02006-05-15 20:58:22 +0900797{
798 unsigned long flags;
Tejun Heo13abf502006-07-10 23:18:46 +0900799 struct ata_eh_info *ehi = &ap->eh_info;
800 struct ata_eh_context *ehc = &ap->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +0900801
Jeff Garzikba6a1302006-06-22 23:46:10 -0400802 spin_lock_irqsave(ap->lock, flags);
Tejun Heo1cdaf532006-07-03 16:07:26 +0900803
Tejun Heo13abf502006-07-10 23:18:46 +0900804 /* Reset is represented by combination of actions and EHI
805 * flags. Suck in all related bits before clearing eh_info to
806 * avoid losing requested action.
807 */
808 if (action & ATA_EH_RESET_MASK) {
809 ehc->i.action |= ehi->action & ATA_EH_RESET_MASK;
810 ehc->i.flags |= ehi->flags & ATA_EHI_RESET_MODIFIER_MASK;
Tejun Heo1cdaf532006-07-03 16:07:26 +0900811
Tejun Heo13abf502006-07-10 23:18:46 +0900812 /* make sure all reset actions are cleared & clear EHI flags */
813 action |= ATA_EH_RESET_MASK;
814 ehi->flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
815 }
816
817 ata_eh_clear_action(dev, ehi, action);
818
819 if (!(ehc->i.flags & ATA_EHI_QUIET))
Tejun Heo1cdaf532006-07-03 16:07:26 +0900820 ap->pflags |= ATA_PFLAG_RECOVERED;
821
Jeff Garzikba6a1302006-06-22 23:46:10 -0400822 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo022bdb02006-05-15 20:58:22 +0900823}
824
825/**
Tejun Heo47005f22006-06-19 18:27:23 +0900826 * ata_eh_done - EH action complete
827 * @ap: target ATA port
828 * @dev: target ATA dev for per-dev action (can be NULL)
829 * @action: action just completed
830 *
831 * Called right after performing EH actions to clear related bits
832 * in @ap->eh_context.
833 *
834 * LOCKING:
835 * None.
836 */
837static void ata_eh_done(struct ata_port *ap, struct ata_device *dev,
838 unsigned int action)
839{
Tejun Heo13abf502006-07-10 23:18:46 +0900840 /* if reset is complete, clear all reset actions & reset modifier */
841 if (action & ATA_EH_RESET_MASK) {
842 action |= ATA_EH_RESET_MASK;
843 ap->eh_context.i.flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
844 }
845
Tejun Heo47005f22006-06-19 18:27:23 +0900846 ata_eh_clear_action(dev, &ap->eh_context.i, action);
847}
848
849/**
Tejun Heo022bdb02006-05-15 20:58:22 +0900850 * ata_err_string - convert err_mask to descriptive string
851 * @err_mask: error mask to convert to string
852 *
853 * Convert @err_mask to descriptive string. Errors are
854 * prioritized according to severity and only the most severe
855 * error is reported.
856 *
857 * LOCKING:
858 * None.
859 *
860 * RETURNS:
861 * Descriptive string for @err_mask
862 */
863static const char * ata_err_string(unsigned int err_mask)
864{
865 if (err_mask & AC_ERR_HOST_BUS)
866 return "host bus error";
867 if (err_mask & AC_ERR_ATA_BUS)
868 return "ATA bus error";
869 if (err_mask & AC_ERR_TIMEOUT)
870 return "timeout";
871 if (err_mask & AC_ERR_HSM)
872 return "HSM violation";
873 if (err_mask & AC_ERR_SYSTEM)
874 return "internal error";
875 if (err_mask & AC_ERR_MEDIA)
876 return "media error";
877 if (err_mask & AC_ERR_INVALID)
878 return "invalid argument";
879 if (err_mask & AC_ERR_DEV)
880 return "device error";
881 return "unknown error";
882}
883
884/**
Tejun Heoe8ee8452006-05-15 21:03:46 +0900885 * ata_read_log_page - read a specific log page
886 * @dev: target device
887 * @page: page to read
888 * @buf: buffer to store read page
889 * @sectors: number of sectors to read
890 *
891 * Read log page using READ_LOG_EXT command.
892 *
893 * LOCKING:
894 * Kernel thread context (may sleep).
895 *
896 * RETURNS:
897 * 0 on success, AC_ERR_* mask otherwise.
898 */
899static unsigned int ata_read_log_page(struct ata_device *dev,
900 u8 page, void *buf, unsigned int sectors)
901{
902 struct ata_taskfile tf;
903 unsigned int err_mask;
904
905 DPRINTK("read log page - page %d\n", page);
906
907 ata_tf_init(dev, &tf);
908 tf.command = ATA_CMD_READ_LOG_EXT;
909 tf.lbal = page;
910 tf.nsect = sectors;
911 tf.hob_nsect = sectors >> 8;
912 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
913 tf.protocol = ATA_PROT_PIO;
914
915 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
916 buf, sectors * ATA_SECT_SIZE);
917
918 DPRINTK("EXIT, err_mask=%x\n", err_mask);
919 return err_mask;
920}
921
922/**
923 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
924 * @dev: Device to read log page 10h from
925 * @tag: Resulting tag of the failed command
926 * @tf: Resulting taskfile registers of the failed command
927 *
928 * Read log page 10h to obtain NCQ error details and clear error
929 * condition.
930 *
931 * LOCKING:
932 * Kernel thread context (may sleep).
933 *
934 * RETURNS:
935 * 0 on success, -errno otherwise.
936 */
937static int ata_eh_read_log_10h(struct ata_device *dev,
938 int *tag, struct ata_taskfile *tf)
939{
940 u8 *buf = dev->ap->sector_buf;
941 unsigned int err_mask;
942 u8 csum;
943 int i;
944
945 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
946 if (err_mask)
947 return -EIO;
948
949 csum = 0;
950 for (i = 0; i < ATA_SECT_SIZE; i++)
951 csum += buf[i];
952 if (csum)
953 ata_dev_printk(dev, KERN_WARNING,
954 "invalid checksum 0x%x on log page 10h\n", csum);
955
956 if (buf[0] & 0x80)
957 return -ENOENT;
958
959 *tag = buf[0] & 0x1f;
960
961 tf->command = buf[2];
962 tf->feature = buf[3];
963 tf->lbal = buf[4];
964 tf->lbam = buf[5];
965 tf->lbah = buf[6];
966 tf->device = buf[7];
967 tf->hob_lbal = buf[8];
968 tf->hob_lbam = buf[9];
969 tf->hob_lbah = buf[10];
970 tf->nsect = buf[12];
971 tf->hob_nsect = buf[13];
972
973 return 0;
974}
975
976/**
Tejun Heo022bdb02006-05-15 20:58:22 +0900977 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
978 * @dev: device to perform REQUEST_SENSE to
979 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
980 *
981 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
982 * SENSE. This function is EH helper.
983 *
984 * LOCKING:
985 * Kernel thread context (may sleep).
986 *
987 * RETURNS:
988 * 0 on success, AC_ERR_* mask on failure
989 */
Albert Lee56287762007-04-02 11:30:46 +0800990static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc)
Tejun Heo022bdb02006-05-15 20:58:22 +0900991{
Albert Lee56287762007-04-02 11:30:46 +0800992 struct ata_device *dev = qc->dev;
993 unsigned char *sense_buf = qc->scsicmd->sense_buffer;
Tejun Heo022bdb02006-05-15 20:58:22 +0900994 struct ata_port *ap = dev->ap;
995 struct ata_taskfile tf;
996 u8 cdb[ATAPI_CDB_LEN];
997
998 DPRINTK("ATAPI request sense\n");
999
Tejun Heo022bdb02006-05-15 20:58:22 +09001000 /* FIXME: is this needed? */
1001 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1002
Albert Lee56287762007-04-02 11:30:46 +08001003 /* initialize sense_buf with the error register,
1004 * for the case where they are -not- overwritten
1005 */
Tejun Heo022bdb02006-05-15 20:58:22 +09001006 sense_buf[0] = 0x70;
Albert Lee56287762007-04-02 11:30:46 +08001007 sense_buf[2] = qc->result_tf.feature >> 4;
1008
1009 /* some devices time out if garbage left in tf */
1010 ata_tf_init(dev, &tf);
Tejun Heo022bdb02006-05-15 20:58:22 +09001011
1012 memset(cdb, 0, ATAPI_CDB_LEN);
1013 cdb[0] = REQUEST_SENSE;
1014 cdb[4] = SCSI_SENSE_BUFFERSIZE;
1015
1016 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1017 tf.command = ATA_CMD_PACKET;
1018
1019 /* is it pointless to prefer PIO for "safety reasons"? */
1020 if (ap->flags & ATA_FLAG_PIO_DMA) {
1021 tf.protocol = ATA_PROT_ATAPI_DMA;
1022 tf.feature |= ATAPI_PKT_DMA;
1023 } else {
1024 tf.protocol = ATA_PROT_ATAPI;
1025 tf.lbam = (8 * 1024) & 0xff;
1026 tf.lbah = (8 * 1024) >> 8;
1027 }
1028
1029 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
1030 sense_buf, SCSI_SENSE_BUFFERSIZE);
1031}
1032
1033/**
1034 * ata_eh_analyze_serror - analyze SError for a failed port
1035 * @ap: ATA port to analyze SError for
1036 *
1037 * Analyze SError if available and further determine cause of
1038 * failure.
1039 *
1040 * LOCKING:
1041 * None.
1042 */
1043static void ata_eh_analyze_serror(struct ata_port *ap)
1044{
1045 struct ata_eh_context *ehc = &ap->eh_context;
1046 u32 serror = ehc->i.serror;
1047 unsigned int err_mask = 0, action = 0;
1048
1049 if (serror & SERR_PERSISTENT) {
1050 err_mask |= AC_ERR_ATA_BUS;
1051 action |= ATA_EH_HARDRESET;
1052 }
1053 if (serror &
1054 (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
1055 err_mask |= AC_ERR_ATA_BUS;
1056 action |= ATA_EH_SOFTRESET;
1057 }
1058 if (serror & SERR_PROTOCOL) {
1059 err_mask |= AC_ERR_HSM;
1060 action |= ATA_EH_SOFTRESET;
1061 }
1062 if (serror & SERR_INTERNAL) {
1063 err_mask |= AC_ERR_SYSTEM;
Tejun Heo771b8da2007-03-14 01:20:51 +09001064 action |= ATA_EH_HARDRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001065 }
Tejun Heo084fe632006-05-31 18:28:03 +09001066 if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG))
1067 ata_ehi_hotplugged(&ehc->i);
Tejun Heo022bdb02006-05-15 20:58:22 +09001068
1069 ehc->i.err_mask |= err_mask;
1070 ehc->i.action |= action;
1071}
1072
1073/**
Tejun Heoe8ee8452006-05-15 21:03:46 +09001074 * ata_eh_analyze_ncq_error - analyze NCQ error
1075 * @ap: ATA port to analyze NCQ error for
1076 *
1077 * Read log page 10h, determine the offending qc and acquire
1078 * error status TF. For NCQ device errors, all LLDDs have to do
1079 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1080 * care of the rest.
1081 *
1082 * LOCKING:
1083 * Kernel thread context (may sleep).
1084 */
1085static void ata_eh_analyze_ncq_error(struct ata_port *ap)
1086{
1087 struct ata_eh_context *ehc = &ap->eh_context;
1088 struct ata_device *dev = ap->device;
1089 struct ata_queued_cmd *qc;
1090 struct ata_taskfile tf;
1091 int tag, rc;
1092
1093 /* if frozen, we can't do much */
Tejun Heob51e9e52006-06-29 01:29:30 +09001094 if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heoe8ee8452006-05-15 21:03:46 +09001095 return;
1096
1097 /* is it NCQ device error? */
1098 if (!ap->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1099 return;
1100
1101 /* has LLDD analyzed already? */
1102 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1103 qc = __ata_qc_from_tag(ap, tag);
1104
1105 if (!(qc->flags & ATA_QCFLAG_FAILED))
1106 continue;
1107
1108 if (qc->err_mask)
1109 return;
1110 }
1111
1112 /* okay, this error is ours */
1113 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1114 if (rc) {
1115 ata_port_printk(ap, KERN_ERR, "failed to read log page 10h "
1116 "(errno=%d)\n", rc);
1117 return;
1118 }
1119
1120 if (!(ap->sactive & (1 << tag))) {
1121 ata_port_printk(ap, KERN_ERR, "log page 10h reported "
1122 "inactive tag %d\n", tag);
1123 return;
1124 }
1125
1126 /* we've got the perpetrator, condemn it */
1127 qc = __ata_qc_from_tag(ap, tag);
1128 memcpy(&qc->result_tf, &tf, sizeof(tf));
1129 qc->err_mask |= AC_ERR_DEV;
1130 ehc->i.err_mask &= ~AC_ERR_DEV;
1131}
1132
1133/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001134 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1135 * @qc: qc to analyze
1136 * @tf: Taskfile registers to analyze
1137 *
1138 * Analyze taskfile of @qc and further determine cause of
1139 * failure. This function also requests ATAPI sense data if
1140 * avaliable.
1141 *
1142 * LOCKING:
1143 * Kernel thread context (may sleep).
1144 *
1145 * RETURNS:
1146 * Determined recovery action
1147 */
1148static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1149 const struct ata_taskfile *tf)
1150{
1151 unsigned int tmp, action = 0;
1152 u8 stat = tf->command, err = tf->feature;
1153
1154 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1155 qc->err_mask |= AC_ERR_HSM;
1156 return ATA_EH_SOFTRESET;
1157 }
1158
Tejun Heoa51d6442007-03-20 15:24:11 +09001159 if (stat & (ATA_ERR | ATA_DF))
1160 qc->err_mask |= AC_ERR_DEV;
1161 else
Tejun Heo022bdb02006-05-15 20:58:22 +09001162 return 0;
1163
1164 switch (qc->dev->class) {
1165 case ATA_DEV_ATA:
1166 if (err & ATA_ICRC)
1167 qc->err_mask |= AC_ERR_ATA_BUS;
1168 if (err & ATA_UNC)
1169 qc->err_mask |= AC_ERR_MEDIA;
1170 if (err & ATA_IDNF)
1171 qc->err_mask |= AC_ERR_INVALID;
1172 break;
1173
1174 case ATA_DEV_ATAPI:
Tejun Heoa569a302006-11-21 10:40:51 +09001175 if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
Albert Lee56287762007-04-02 11:30:46 +08001176 tmp = atapi_eh_request_sense(qc);
Tejun Heoa569a302006-11-21 10:40:51 +09001177 if (!tmp) {
1178 /* ATA_QCFLAG_SENSE_VALID is used to
1179 * tell atapi_qc_complete() that sense
1180 * data is already valid.
1181 *
1182 * TODO: interpret sense data and set
1183 * appropriate err_mask.
1184 */
1185 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1186 } else
1187 qc->err_mask |= tmp;
1188 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001189 }
1190
1191 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1192 action |= ATA_EH_SOFTRESET;
1193
1194 return action;
1195}
1196
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001197static int ata_eh_categorize_error(int is_io, unsigned int err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001198{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001199 if (err_mask & AC_ERR_ATA_BUS)
Tejun Heo022bdb02006-05-15 20:58:22 +09001200 return 1;
1201
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001202 if (err_mask & AC_ERR_TIMEOUT)
1203 return 2;
1204
1205 if (is_io) {
1206 if (err_mask & AC_ERR_HSM)
Tejun Heo022bdb02006-05-15 20:58:22 +09001207 return 2;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001208 if ((err_mask &
1209 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1210 return 3;
Tejun Heo022bdb02006-05-15 20:58:22 +09001211 }
1212
1213 return 0;
1214}
1215
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001216struct speed_down_verdict_arg {
Tejun Heo022bdb02006-05-15 20:58:22 +09001217 u64 since;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001218 int nr_errors[4];
Tejun Heo022bdb02006-05-15 20:58:22 +09001219};
1220
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001221static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
Tejun Heo022bdb02006-05-15 20:58:22 +09001222{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001223 struct speed_down_verdict_arg *arg = void_arg;
1224 int cat = ata_eh_categorize_error(ent->is_io, ent->err_mask);
Tejun Heo022bdb02006-05-15 20:58:22 +09001225
1226 if (ent->timestamp < arg->since)
1227 return -1;
1228
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001229 arg->nr_errors[cat]++;
Tejun Heo022bdb02006-05-15 20:58:22 +09001230 return 0;
1231}
1232
1233/**
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001234 * ata_eh_speed_down_verdict - Determine speed down verdict
Tejun Heo022bdb02006-05-15 20:58:22 +09001235 * @dev: Device of interest
1236 *
1237 * This function examines error ring of @dev and determines
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001238 * whether NCQ needs to be turned off, transfer speed should be
1239 * stepped down, or falling back to PIO is necessary.
Tejun Heo022bdb02006-05-15 20:58:22 +09001240 *
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001241 * Cat-1 is ATA_BUS error for any command.
Tejun Heo022bdb02006-05-15 20:58:22 +09001242 *
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001243 * Cat-2 is TIMEOUT for any command or HSM violation for known
1244 * supported commands.
1245 *
1246 * Cat-3 is is unclassified DEV error for known supported
Tejun Heo022bdb02006-05-15 20:58:22 +09001247 * command.
1248 *
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001249 * NCQ needs to be turned off if there have been more than 3
1250 * Cat-2 + Cat-3 errors during last 10 minutes.
1251 *
1252 * Speed down is necessary if there have been more than 3 Cat-1 +
1253 * Cat-2 errors or 10 Cat-3 errors during last 10 minutes.
1254 *
1255 * Falling back to PIO mode is necessary if there have been more
1256 * than 10 Cat-1 + Cat-2 + Cat-3 errors during last 5 minutes.
1257 *
Tejun Heo022bdb02006-05-15 20:58:22 +09001258 * LOCKING:
1259 * Inherited from caller.
1260 *
1261 * RETURNS:
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001262 * OR of ATA_EH_SPDN_* flags.
Tejun Heo022bdb02006-05-15 20:58:22 +09001263 */
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001264static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
Tejun Heo022bdb02006-05-15 20:58:22 +09001265{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001266 const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
1267 u64 j64 = get_jiffies_64();
1268 struct speed_down_verdict_arg arg;
1269 unsigned int verdict = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09001270
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001271 /* scan past 10 mins of error history */
Tejun Heo022bdb02006-05-15 20:58:22 +09001272 memset(&arg, 0, sizeof(arg));
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001273 arg.since = j64 - min(j64, j10mins);
1274 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
Tejun Heo022bdb02006-05-15 20:58:22 +09001275
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001276 if (arg.nr_errors[2] + arg.nr_errors[3] > 3)
1277 verdict |= ATA_EH_SPDN_NCQ_OFF;
1278 if (arg.nr_errors[1] + arg.nr_errors[2] > 3 || arg.nr_errors[3] > 10)
1279 verdict |= ATA_EH_SPDN_SPEED_DOWN;
Tejun Heo022bdb02006-05-15 20:58:22 +09001280
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001281 /* scan past 3 mins of error history */
1282 memset(&arg, 0, sizeof(arg));
1283 arg.since = j64 - min(j64, j5mins);
1284 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
Tejun Heo022bdb02006-05-15 20:58:22 +09001285
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001286 if (arg.nr_errors[1] + arg.nr_errors[2] + arg.nr_errors[3] > 10)
1287 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
1288
1289 return verdict;
Tejun Heo022bdb02006-05-15 20:58:22 +09001290}
1291
1292/**
1293 * ata_eh_speed_down - record error and speed down if necessary
1294 * @dev: Failed device
1295 * @is_io: Did the device fail during normal IO?
1296 * @err_mask: err_mask of the error
1297 *
1298 * Record error and examine error history to determine whether
1299 * adjusting transmission speed is necessary. It also sets
1300 * transmission limits appropriately if such adjustment is
1301 * necessary.
1302 *
1303 * LOCKING:
1304 * Kernel thread context (may sleep).
1305 *
1306 * RETURNS:
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001307 * Determined recovery action.
Tejun Heo022bdb02006-05-15 20:58:22 +09001308 */
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001309static unsigned int ata_eh_speed_down(struct ata_device *dev, int is_io,
1310 unsigned int err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001311{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001312 unsigned int verdict;
1313 unsigned int action = 0;
1314
1315 /* don't bother if Cat-0 error */
1316 if (ata_eh_categorize_error(is_io, err_mask) == 0)
Tejun Heo022bdb02006-05-15 20:58:22 +09001317 return 0;
1318
1319 /* record error and determine whether speed down is necessary */
1320 ata_ering_record(&dev->ering, is_io, err_mask);
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001321 verdict = ata_eh_speed_down_verdict(dev);
Tejun Heo022bdb02006-05-15 20:58:22 +09001322
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001323 /* turn off NCQ? */
1324 if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
1325 (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
1326 ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
1327 dev->flags |= ATA_DFLAG_NCQ_OFF;
1328 ata_dev_printk(dev, KERN_WARNING,
1329 "NCQ disabled due to excessive errors\n");
1330 goto done;
1331 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001332
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001333 /* speed down? */
1334 if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1335 /* speed down SATA link speed if possible */
1336 if (sata_down_spd_limit(dev->ap) == 0) {
1337 action |= ATA_EH_HARDRESET;
1338 goto done;
1339 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001340
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001341 /* lower transfer mode */
1342 if (dev->spdn_cnt < 2) {
1343 static const int dma_dnxfer_sel[] =
1344 { ATA_DNXFER_DMA, ATA_DNXFER_40C };
1345 static const int pio_dnxfer_sel[] =
1346 { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
1347 int sel;
Tejun Heo022bdb02006-05-15 20:58:22 +09001348
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001349 if (dev->xfer_shift != ATA_SHIFT_PIO)
1350 sel = dma_dnxfer_sel[dev->spdn_cnt];
1351 else
1352 sel = pio_dnxfer_sel[dev->spdn_cnt];
1353
1354 dev->spdn_cnt++;
1355
1356 if (ata_down_xfermask_limit(dev, sel) == 0) {
1357 action |= ATA_EH_SOFTRESET;
1358 goto done;
1359 }
1360 }
1361 }
1362
1363 /* Fall back to PIO? Slowing down to PIO is meaningless for
1364 * SATA. Consider it only for PATA.
1365 */
1366 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
1367 (dev->ap->cbl != ATA_CBL_SATA) &&
1368 (dev->xfer_shift != ATA_SHIFT_PIO)) {
1369 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
1370 dev->spdn_cnt = 0;
1371 action |= ATA_EH_SOFTRESET;
1372 goto done;
1373 }
1374 }
1375
Tejun Heo022bdb02006-05-15 20:58:22 +09001376 return 0;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001377 done:
1378 /* device has been slowed down, blow error history */
1379 ata_ering_clear(&dev->ering);
1380 return action;
Tejun Heo022bdb02006-05-15 20:58:22 +09001381}
1382
1383/**
1384 * ata_eh_autopsy - analyze error and determine recovery action
1385 * @ap: ATA port to perform autopsy on
1386 *
1387 * Analyze why @ap failed and determine which recovery action is
1388 * needed. This function also sets more detailed AC_ERR_* values
1389 * and fills sense data for ATAPI CHECK SENSE.
1390 *
1391 * LOCKING:
1392 * Kernel thread context (may sleep).
1393 */
1394static void ata_eh_autopsy(struct ata_port *ap)
1395{
1396 struct ata_eh_context *ehc = &ap->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001397 unsigned int all_err_mask = 0;
1398 int tag, is_io = 0;
1399 u32 serror;
1400 int rc;
1401
1402 DPRINTK("ENTER\n");
1403
Tejun Heo1cdaf532006-07-03 16:07:26 +09001404 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1405 return;
1406
Tejun Heo022bdb02006-05-15 20:58:22 +09001407 /* obtain and analyze SError */
1408 rc = sata_scr_read(ap, SCR_ERROR, &serror);
1409 if (rc == 0) {
1410 ehc->i.serror |= serror;
1411 ata_eh_analyze_serror(ap);
1412 } else if (rc != -EOPNOTSUPP)
Tejun Heo4528e4d2006-07-08 20:17:26 +09001413 ehc->i.action |= ATA_EH_HARDRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001414
Tejun Heoe8ee8452006-05-15 21:03:46 +09001415 /* analyze NCQ failure */
1416 ata_eh_analyze_ncq_error(ap);
1417
Tejun Heo022bdb02006-05-15 20:58:22 +09001418 /* any real error trumps AC_ERR_OTHER */
1419 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1420 ehc->i.err_mask &= ~AC_ERR_OTHER;
1421
1422 all_err_mask |= ehc->i.err_mask;
1423
1424 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1425 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1426
1427 if (!(qc->flags & ATA_QCFLAG_FAILED))
1428 continue;
1429
1430 /* inherit upper level err_mask */
1431 qc->err_mask |= ehc->i.err_mask;
1432
Tejun Heo022bdb02006-05-15 20:58:22 +09001433 /* analyze TF */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001434 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
Tejun Heo022bdb02006-05-15 20:58:22 +09001435
1436 /* DEV errors are probably spurious in case of ATA_BUS error */
1437 if (qc->err_mask & AC_ERR_ATA_BUS)
1438 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1439 AC_ERR_INVALID);
1440
1441 /* any real error trumps unknown error */
1442 if (qc->err_mask & ~AC_ERR_OTHER)
1443 qc->err_mask &= ~AC_ERR_OTHER;
1444
1445 /* SENSE_VALID trumps dev/unknown error and revalidation */
1446 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1447 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
Tejun Heo4528e4d2006-07-08 20:17:26 +09001448 ehc->i.action &= ~ATA_EH_REVALIDATE;
Tejun Heo022bdb02006-05-15 20:58:22 +09001449 }
1450
1451 /* accumulate error info */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001452 ehc->i.dev = qc->dev;
Tejun Heo022bdb02006-05-15 20:58:22 +09001453 all_err_mask |= qc->err_mask;
1454 if (qc->flags & ATA_QCFLAG_IO)
1455 is_io = 1;
1456 }
1457
Tejun Heoa20f33f2006-05-16 12:58:24 +09001458 /* enforce default EH actions */
Tejun Heob51e9e52006-06-29 01:29:30 +09001459 if (ap->pflags & ATA_PFLAG_FROZEN ||
Tejun Heoa20f33f2006-05-16 12:58:24 +09001460 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
Tejun Heo4528e4d2006-07-08 20:17:26 +09001461 ehc->i.action |= ATA_EH_SOFTRESET;
Tejun Heoa20f33f2006-05-16 12:58:24 +09001462 else if (all_err_mask)
Tejun Heo4528e4d2006-07-08 20:17:26 +09001463 ehc->i.action |= ATA_EH_REVALIDATE;
Tejun Heo022bdb02006-05-15 20:58:22 +09001464
Tejun Heo47005f22006-06-19 18:27:23 +09001465 /* if we have offending qcs and the associated failed device */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001466 if (ehc->i.dev) {
Tejun Heo47005f22006-06-19 18:27:23 +09001467 /* speed down */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001468 ehc->i.action |= ata_eh_speed_down(ehc->i.dev, is_io,
1469 all_err_mask);
Tejun Heo47005f22006-06-19 18:27:23 +09001470
1471 /* perform per-dev EH action only on the offending device */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001472 ehc->i.dev_action[ehc->i.dev->devno] |=
1473 ehc->i.action & ATA_EH_PERDEV_MASK;
1474 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
Tejun Heo47005f22006-06-19 18:27:23 +09001475 }
1476
Tejun Heo022bdb02006-05-15 20:58:22 +09001477 DPRINTK("EXIT\n");
1478}
1479
1480/**
1481 * ata_eh_report - report error handling to user
1482 * @ap: ATA port EH is going on
1483 *
1484 * Report EH to user.
1485 *
1486 * LOCKING:
1487 * None.
1488 */
1489static void ata_eh_report(struct ata_port *ap)
1490{
1491 struct ata_eh_context *ehc = &ap->eh_context;
1492 const char *frozen, *desc;
1493 int tag, nr_failed = 0;
1494
1495 desc = NULL;
1496 if (ehc->i.desc[0] != '\0')
1497 desc = ehc->i.desc;
1498
1499 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1500 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1501
1502 if (!(qc->flags & ATA_QCFLAG_FAILED))
1503 continue;
1504 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1505 continue;
1506
1507 nr_failed++;
1508 }
1509
1510 if (!nr_failed && !ehc->i.err_mask)
1511 return;
1512
1513 frozen = "";
Tejun Heob51e9e52006-06-29 01:29:30 +09001514 if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heo022bdb02006-05-15 20:58:22 +09001515 frozen = " frozen";
1516
1517 if (ehc->i.dev) {
Tejun Heoe8ee8452006-05-15 21:03:46 +09001518 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1519 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1520 ehc->i.err_mask, ap->sactive, ehc->i.serror,
1521 ehc->i.action, frozen);
Tejun Heo022bdb02006-05-15 20:58:22 +09001522 if (desc)
1523 ata_dev_printk(ehc->i.dev, KERN_ERR, "(%s)\n", desc);
1524 } else {
Tejun Heoe8ee8452006-05-15 21:03:46 +09001525 ata_port_printk(ap, KERN_ERR, "exception Emask 0x%x "
1526 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1527 ehc->i.err_mask, ap->sactive, ehc->i.serror,
1528 ehc->i.action, frozen);
Tejun Heo022bdb02006-05-15 20:58:22 +09001529 if (desc)
1530 ata_port_printk(ap, KERN_ERR, "(%s)\n", desc);
1531 }
1532
1533 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
Tejun Heo8a937582006-11-14 22:36:12 +09001534 static const char *dma_str[] = {
1535 [DMA_BIDIRECTIONAL] = "bidi",
1536 [DMA_TO_DEVICE] = "out",
1537 [DMA_FROM_DEVICE] = "in",
1538 [DMA_NONE] = "",
1539 };
Tejun Heo022bdb02006-05-15 20:58:22 +09001540 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
Tejun Heo8a937582006-11-14 22:36:12 +09001541 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
Tejun Heo022bdb02006-05-15 20:58:22 +09001542
1543 if (!(qc->flags & ATA_QCFLAG_FAILED) || !qc->err_mask)
1544 continue;
1545
Tejun Heo8a937582006-11-14 22:36:12 +09001546 ata_dev_printk(qc->dev, KERN_ERR,
1547 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
Tejun Heo664e8502006-11-20 16:05:34 +09001548 "tag %d cdb 0x%x data %u %s\n "
Tejun Heo8a937582006-11-14 22:36:12 +09001549 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
1550 "Emask 0x%x (%s)\n",
1551 cmd->command, cmd->feature, cmd->nsect,
1552 cmd->lbal, cmd->lbam, cmd->lbah,
1553 cmd->hob_feature, cmd->hob_nsect,
1554 cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
Tejun Heo726f0782007-01-03 17:30:39 +09001555 cmd->device, qc->tag, qc->cdb[0], qc->nbytes,
Tejun Heo664e8502006-11-20 16:05:34 +09001556 dma_str[qc->dma_dir],
Tejun Heo8a937582006-11-14 22:36:12 +09001557 res->command, res->feature, res->nsect,
1558 res->lbal, res->lbam, res->lbah,
1559 res->hob_feature, res->hob_nsect,
1560 res->hob_lbal, res->hob_lbam, res->hob_lbah,
1561 res->device, qc->err_mask, ata_err_string(qc->err_mask));
Tejun Heo022bdb02006-05-15 20:58:22 +09001562 }
1563}
1564
Tejun Heod87fa382006-05-31 18:28:24 +09001565static int ata_do_reset(struct ata_port *ap, ata_reset_fn_t reset,
Tejun Heod4b2bab2007-02-02 16:50:52 +09001566 unsigned int *classes, unsigned long deadline)
Tejun Heod87fa382006-05-31 18:28:24 +09001567{
1568 int i, rc;
1569
1570 for (i = 0; i < ATA_MAX_DEVICES; i++)
1571 classes[i] = ATA_DEV_UNKNOWN;
1572
Tejun Heod4b2bab2007-02-02 16:50:52 +09001573 rc = reset(ap, classes, deadline);
Tejun Heod87fa382006-05-31 18:28:24 +09001574 if (rc)
1575 return rc;
1576
1577 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
1578 * is complete and convert all ATA_DEV_UNKNOWN to
1579 * ATA_DEV_NONE.
1580 */
1581 for (i = 0; i < ATA_MAX_DEVICES; i++)
1582 if (classes[i] != ATA_DEV_UNKNOWN)
1583 break;
1584
1585 if (i < ATA_MAX_DEVICES)
1586 for (i = 0; i < ATA_MAX_DEVICES; i++)
1587 if (classes[i] == ATA_DEV_UNKNOWN)
1588 classes[i] = ATA_DEV_NONE;
1589
1590 return 0;
1591}
1592
Tejun Heo664faf02006-05-31 18:27:50 +09001593static int ata_eh_followup_srst_needed(int rc, int classify,
1594 const unsigned int *classes)
1595{
1596 if (rc == -EAGAIN)
1597 return 1;
1598 if (rc != 0)
1599 return 0;
1600 if (classify && classes[0] == ATA_DEV_UNKNOWN)
1601 return 1;
1602 return 0;
1603}
1604
1605static int ata_eh_reset(struct ata_port *ap, int classify,
Tejun Heof5914a42006-05-31 18:27:48 +09001606 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
Tejun Heo022bdb02006-05-15 20:58:22 +09001607 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
1608{
1609 struct ata_eh_context *ehc = &ap->eh_context;
Tejun Heo664faf02006-05-31 18:27:50 +09001610 unsigned int *classes = ehc->classes;
Tejun Heo1cdaf532006-07-03 16:07:26 +09001611 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
Tejun Heo31daabd2007-02-02 16:50:52 +09001612 int try = 0;
1613 unsigned long deadline;
Tejun Heof5914a42006-05-31 18:27:48 +09001614 unsigned int action;
Tejun Heo022bdb02006-05-15 20:58:22 +09001615 ata_reset_fn_t reset;
Tejun Heo664faf02006-05-31 18:27:50 +09001616 int i, did_followup_srst, rc;
Tejun Heo022bdb02006-05-15 20:58:22 +09001617
Tejun Heo13abf502006-07-10 23:18:46 +09001618 /* about to reset */
1619 ata_eh_about_to_do(ap, NULL, ehc->i.action & ATA_EH_RESET_MASK);
1620
Tejun Heof5914a42006-05-31 18:27:48 +09001621 /* Determine which reset to use and record in ehc->i.action.
1622 * prereset() may examine and modify it.
1623 */
1624 action = ehc->i.action;
1625 ehc->i.action &= ~ATA_EH_RESET_MASK;
Tejun Heo022bdb02006-05-15 20:58:22 +09001626 if (softreset && (!hardreset || (!sata_set_spd_needed(ap) &&
Tejun Heof5914a42006-05-31 18:27:48 +09001627 !(action & ATA_EH_HARDRESET))))
1628 ehc->i.action |= ATA_EH_SOFTRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001629 else
Tejun Heof5914a42006-05-31 18:27:48 +09001630 ehc->i.action |= ATA_EH_HARDRESET;
1631
1632 if (prereset) {
Tejun Heo31daabd2007-02-02 16:50:52 +09001633 rc = prereset(ap, jiffies + ATA_EH_PRERESET_TIMEOUT);
Tejun Heof5914a42006-05-31 18:27:48 +09001634 if (rc) {
Alan Coxc9619222006-09-26 17:53:38 +01001635 if (rc == -ENOENT) {
Tejun Heo4aa9ab62007-03-12 17:24:08 +09001636 ata_port_printk(ap, KERN_DEBUG,
1637 "port disabled. ignoring.\n");
Alan Coxc9619222006-09-26 17:53:38 +01001638 ap->eh_context.i.action &= ~ATA_EH_RESET_MASK;
Tejun Heo4aa9ab62007-03-12 17:24:08 +09001639
1640 for (i = 0; i < ATA_MAX_DEVICES; i++)
1641 classes[i] = ATA_DEV_NONE;
1642
1643 rc = 0;
Alan Coxc9619222006-09-26 17:53:38 +01001644 } else
1645 ata_port_printk(ap, KERN_ERR,
Tejun Heof5914a42006-05-31 18:27:48 +09001646 "prereset failed (errno=%d)\n", rc);
1647 return rc;
1648 }
1649 }
1650
1651 /* prereset() might have modified ehc->i.action */
1652 if (ehc->i.action & ATA_EH_HARDRESET)
Tejun Heo022bdb02006-05-15 20:58:22 +09001653 reset = hardreset;
Tejun Heof5914a42006-05-31 18:27:48 +09001654 else if (ehc->i.action & ATA_EH_SOFTRESET)
1655 reset = softreset;
1656 else {
1657 /* prereset told us not to reset, bang classes and return */
1658 for (i = 0; i < ATA_MAX_DEVICES; i++)
1659 classes[i] = ATA_DEV_NONE;
1660 return 0;
1661 }
1662
1663 /* did prereset() screw up? if so, fix up to avoid oopsing */
1664 if (!reset) {
1665 ata_port_printk(ap, KERN_ERR, "BUG: prereset() requested "
1666 "invalid reset type\n");
1667 if (softreset)
1668 reset = softreset;
1669 else
1670 reset = hardreset;
1671 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001672
1673 retry:
Tejun Heo31daabd2007-02-02 16:50:52 +09001674 deadline = jiffies + ata_eh_reset_timeouts[try++];
1675
Tejun Heo3e706392006-05-31 18:28:11 +09001676 /* shut up during boot probing */
1677 if (verbose)
1678 ata_port_printk(ap, KERN_INFO, "%s resetting port\n",
1679 reset == softreset ? "soft" : "hard");
Tejun Heo022bdb02006-05-15 20:58:22 +09001680
Tejun Heo13abf502006-07-10 23:18:46 +09001681 /* mark that this EH session started with reset */
Tejun Heo0d64a232007-04-23 02:41:05 +09001682 if (reset == hardreset)
1683 ehc->i.flags |= ATA_EHI_DID_HARDRESET;
1684 else
1685 ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001686
Tejun Heo31daabd2007-02-02 16:50:52 +09001687 rc = ata_do_reset(ap, reset, classes, deadline);
Tejun Heo022bdb02006-05-15 20:58:22 +09001688
Tejun Heo664faf02006-05-31 18:27:50 +09001689 did_followup_srst = 0;
1690 if (reset == hardreset &&
1691 ata_eh_followup_srst_needed(rc, classify, classes)) {
1692 /* okay, let's do follow-up softreset */
1693 did_followup_srst = 1;
1694 reset = softreset;
1695
1696 if (!reset) {
1697 ata_port_printk(ap, KERN_ERR,
1698 "follow-up softreset required "
1699 "but no softreset avaliable\n");
1700 return -EINVAL;
1701 }
1702
Tejun Heo47005f22006-06-19 18:27:23 +09001703 ata_eh_about_to_do(ap, NULL, ATA_EH_RESET_MASK);
Tejun Heo31daabd2007-02-02 16:50:52 +09001704 rc = ata_do_reset(ap, reset, classes, deadline);
Tejun Heo664faf02006-05-31 18:27:50 +09001705
1706 if (rc == 0 && classify &&
1707 classes[0] == ATA_DEV_UNKNOWN) {
1708 ata_port_printk(ap, KERN_ERR,
1709 "classification failed\n");
1710 return -EINVAL;
1711 }
1712 }
1713
Tejun Heo31daabd2007-02-02 16:50:52 +09001714 if (rc && try < ARRAY_SIZE(ata_eh_reset_timeouts)) {
1715 unsigned long now = jiffies;
Tejun Heo664faf02006-05-31 18:27:50 +09001716
Tejun Heo31daabd2007-02-02 16:50:52 +09001717 if (time_before(now, deadline)) {
1718 unsigned long delta = deadline - jiffies;
Tejun Heo664faf02006-05-31 18:27:50 +09001719
Tejun Heo31daabd2007-02-02 16:50:52 +09001720 ata_port_printk(ap, KERN_WARNING, "reset failed "
1721 "(errno=%d), retrying in %u secs\n",
1722 rc, (jiffies_to_msecs(delta) + 999) / 1000);
Tejun Heo022bdb02006-05-15 20:58:22 +09001723
Tejun Heo31daabd2007-02-02 16:50:52 +09001724 schedule_timeout_uninterruptible(delta);
1725 }
1726
1727 if (reset == hardreset &&
1728 try == ARRAY_SIZE(ata_eh_reset_timeouts) - 1)
Tejun Heo022bdb02006-05-15 20:58:22 +09001729 sata_down_spd_limit(ap);
1730 if (hardreset)
1731 reset = hardreset;
1732 goto retry;
1733 }
1734
1735 if (rc == 0) {
Tejun Heo20952b62006-05-31 18:27:23 +09001736 /* After the reset, the device state is PIO 0 and the
1737 * controller state is undefined. Record the mode.
1738 */
1739 for (i = 0; i < ATA_MAX_DEVICES; i++)
1740 ap->device[i].pio_mode = XFER_PIO_0;
1741
Tejun Heo022bdb02006-05-15 20:58:22 +09001742 if (postreset)
1743 postreset(ap, classes);
1744
1745 /* reset successful, schedule revalidation */
Tejun Heo13abf502006-07-10 23:18:46 +09001746 ata_eh_done(ap, NULL, ehc->i.action & ATA_EH_RESET_MASK);
Tejun Heo022bdb02006-05-15 20:58:22 +09001747 ehc->i.action |= ATA_EH_REVALIDATE;
1748 }
1749
1750 return rc;
1751}
1752
Tejun Heo084fe632006-05-31 18:28:03 +09001753static int ata_eh_revalidate_and_attach(struct ata_port *ap,
1754 struct ata_device **r_failed_dev)
Tejun Heo022bdb02006-05-15 20:58:22 +09001755{
1756 struct ata_eh_context *ehc = &ap->eh_context;
1757 struct ata_device *dev;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09001758 unsigned int new_mask = 0;
Tejun Heo084fe632006-05-31 18:28:03 +09001759 unsigned long flags;
Tejun Heo022bdb02006-05-15 20:58:22 +09001760 int i, rc = 0;
1761
1762 DPRINTK("ENTER\n");
1763
Tejun Heo8c3c52a2007-03-22 22:24:19 +09001764 /* For PATA drive side cable detection to work, IDENTIFY must
1765 * be done backwards such that PDIAG- is released by the slave
1766 * device before the master device is identified.
1767 */
1768 for (i = ATA_MAX_DEVICES - 1; i >= 0; i--) {
Tejun Heobff04642006-11-10 18:08:10 +09001769 unsigned int action, readid_flags = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09001770
Tejun Heo47005f22006-06-19 18:27:23 +09001771 dev = &ap->device[i];
Tejun Heo64f65ca2006-06-24 20:30:18 +09001772 action = ata_eh_dev_action(dev);
Tejun Heo47005f22006-06-19 18:27:23 +09001773
Tejun Heobff04642006-11-10 18:08:10 +09001774 if (ehc->i.flags & ATA_EHI_DID_RESET)
1775 readid_flags |= ATA_READID_POSTRESET;
1776
Tejun Heo9666f402007-05-04 21:27:47 +02001777 if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
Tejun Heo022bdb02006-05-15 20:58:22 +09001778 if (ata_port_offline(ap)) {
1779 rc = -EIO;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09001780 goto err;
Tejun Heo022bdb02006-05-15 20:58:22 +09001781 }
1782
Tejun Heo47005f22006-06-19 18:27:23 +09001783 ata_eh_about_to_do(ap, dev, ATA_EH_REVALIDATE);
Tejun Heobff04642006-11-10 18:08:10 +09001784 rc = ata_dev_revalidate(dev, readid_flags);
Tejun Heo022bdb02006-05-15 20:58:22 +09001785 if (rc)
Tejun Heo8c3c52a2007-03-22 22:24:19 +09001786 goto err;
Tejun Heo022bdb02006-05-15 20:58:22 +09001787
Tejun Heo47005f22006-06-19 18:27:23 +09001788 ata_eh_done(ap, dev, ATA_EH_REVALIDATE);
1789
Tejun Heobaa1e782006-11-01 18:39:27 +09001790 /* Configuration may have changed, reconfigure
1791 * transfer mode.
1792 */
1793 ehc->i.flags |= ATA_EHI_SETMODE;
1794
zhao, forrest3057ac32006-06-12 12:01:34 +08001795 /* schedule the scsi_rescan_device() here */
1796 queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
Tejun Heo084fe632006-05-31 18:28:03 +09001797 } else if (dev->class == ATA_DEV_UNKNOWN &&
1798 ehc->tries[dev->devno] &&
1799 ata_class_enabled(ehc->classes[dev->devno])) {
1800 dev->class = ehc->classes[dev->devno];
1801
Tejun Heobff04642006-11-10 18:08:10 +09001802 rc = ata_dev_read_id(dev, &dev->class, readid_flags,
1803 dev->id);
Tejun Heo8c3c52a2007-03-22 22:24:19 +09001804 switch (rc) {
1805 case 0:
1806 new_mask |= 1 << i;
1807 break;
1808 case -ENOENT:
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001809 /* IDENTIFY was issued to non-existent
1810 * device. No need to reset. Just
1811 * thaw and kill the device.
1812 */
1813 ata_eh_thaw_port(ap);
1814 dev->class = ATA_DEV_UNKNOWN;
Tejun Heo084fe632006-05-31 18:28:03 +09001815 break;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09001816 default:
1817 dev->class = ATA_DEV_UNKNOWN;
1818 goto err;
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001819 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001820 }
1821 }
1822
Tejun Heoc1c4e8d2007-04-23 02:05:53 +09001823 /* PDIAG- should have been released, ask cable type if post-reset */
1824 if ((ehc->i.flags & ATA_EHI_DID_RESET) && ap->ops->cable_detect)
1825 ap->cbl = ap->ops->cable_detect(ap);
1826
Tejun Heo8c3c52a2007-03-22 22:24:19 +09001827 /* Configure new devices forward such that user doesn't see
1828 * device detection messages backwards.
1829 */
1830 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1831 dev = &ap->device[i];
Tejun Heo022bdb02006-05-15 20:58:22 +09001832
Tejun Heo8c3c52a2007-03-22 22:24:19 +09001833 if (!(new_mask & (1 << i)))
1834 continue;
1835
1836 ehc->i.flags |= ATA_EHI_PRINTINFO;
1837 rc = ata_dev_configure(dev);
1838 ehc->i.flags &= ~ATA_EHI_PRINTINFO;
1839 if (rc)
1840 goto err;
1841
1842 spin_lock_irqsave(ap->lock, flags);
1843 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
1844 spin_unlock_irqrestore(ap->lock, flags);
1845
1846 /* new device discovered, configure xfermode */
1847 ehc->i.flags |= ATA_EHI_SETMODE;
1848 }
1849
1850 return 0;
1851
1852 err:
1853 *r_failed_dev = dev;
1854 DPRINTK("EXIT rc=%d\n", rc);
Tejun Heo022bdb02006-05-15 20:58:22 +09001855 return rc;
1856}
1857
1858static int ata_port_nr_enabled(struct ata_port *ap)
1859{
1860 int i, cnt = 0;
1861
1862 for (i = 0; i < ATA_MAX_DEVICES; i++)
1863 if (ata_dev_enabled(&ap->device[i]))
1864 cnt++;
1865 return cnt;
1866}
1867
Tejun Heo084fe632006-05-31 18:28:03 +09001868static int ata_port_nr_vacant(struct ata_port *ap)
1869{
1870 int i, cnt = 0;
1871
1872 for (i = 0; i < ATA_MAX_DEVICES; i++)
1873 if (ap->device[i].class == ATA_DEV_UNKNOWN)
1874 cnt++;
1875 return cnt;
1876}
1877
1878static int ata_eh_skip_recovery(struct ata_port *ap)
1879{
1880 struct ata_eh_context *ehc = &ap->eh_context;
1881 int i;
1882
Tejun Heo7c8c2cf2006-07-10 23:18:23 +09001883 /* thaw frozen port, resume link and recover failed devices */
1884 if ((ap->pflags & ATA_PFLAG_FROZEN) ||
1885 (ehc->i.flags & ATA_EHI_RESUME_LINK) || ata_port_nr_enabled(ap))
Tejun Heo084fe632006-05-31 18:28:03 +09001886 return 0;
1887
1888 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
1889 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1890 struct ata_device *dev = &ap->device[i];
1891
1892 if (dev->class == ATA_DEV_UNKNOWN &&
1893 ehc->classes[dev->devno] != ATA_DEV_NONE)
1894 return 0;
1895 }
1896
1897 return 1;
1898}
1899
Tejun Heo022bdb02006-05-15 20:58:22 +09001900/**
1901 * ata_eh_recover - recover host port after error
1902 * @ap: host port to recover
Tejun Heof5914a42006-05-31 18:27:48 +09001903 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09001904 * @softreset: softreset method (can be NULL)
1905 * @hardreset: hardreset method (can be NULL)
1906 * @postreset: postreset method (can be NULL)
1907 *
1908 * This is the alpha and omega, eum and yang, heart and soul of
1909 * libata exception handling. On entry, actions required to
Tejun Heo084fe632006-05-31 18:28:03 +09001910 * recover the port and hotplug requests are recorded in
1911 * eh_context. This function executes all the operations with
1912 * appropriate retrials and fallbacks to resurrect failed
1913 * devices, detach goners and greet newcomers.
Tejun Heo022bdb02006-05-15 20:58:22 +09001914 *
1915 * LOCKING:
1916 * Kernel thread context (may sleep).
1917 *
1918 * RETURNS:
1919 * 0 on success, -errno on failure.
1920 */
Tejun Heof5914a42006-05-31 18:27:48 +09001921static int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
1922 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
Tejun Heo022bdb02006-05-15 20:58:22 +09001923 ata_postreset_fn_t postreset)
1924{
1925 struct ata_eh_context *ehc = &ap->eh_context;
1926 struct ata_device *dev;
Tejun Heo4ae72a12007-02-02 16:22:30 +09001927 int i, rc;
Tejun Heo022bdb02006-05-15 20:58:22 +09001928
1929 DPRINTK("ENTER\n");
1930
1931 /* prep for recovery */
1932 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1933 dev = &ap->device[i];
1934
1935 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
Tejun Heo084fe632006-05-31 18:28:03 +09001936
Tejun Heo79a55b72007-01-18 17:22:18 +09001937 /* collect port action mask recorded in dev actions */
1938 ehc->i.action |= ehc->i.dev_action[i] & ~ATA_EH_PERDEV_MASK;
1939 ehc->i.dev_action[i] &= ATA_EH_PERDEV_MASK;
1940
Tejun Heo084fe632006-05-31 18:28:03 +09001941 /* process hotplug request */
1942 if (dev->flags & ATA_DFLAG_DETACH)
1943 ata_eh_detach_dev(dev);
1944
1945 if (!ata_dev_enabled(dev) &&
1946 ((ehc->i.probe_mask & (1 << dev->devno)) &&
1947 !(ehc->did_probe_mask & (1 << dev->devno)))) {
1948 ata_eh_detach_dev(dev);
1949 ata_dev_init(dev);
1950 ehc->did_probe_mask |= (1 << dev->devno);
1951 ehc->i.action |= ATA_EH_SOFTRESET;
1952 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001953 }
1954
1955 retry:
Tejun Heo022bdb02006-05-15 20:58:22 +09001956 rc = 0;
1957
Tejun Heoaeb2ecd2006-06-12 14:11:43 +09001958 /* if UNLOADING, finish immediately */
Tejun Heob51e9e52006-06-29 01:29:30 +09001959 if (ap->pflags & ATA_PFLAG_UNLOADING)
Tejun Heoaeb2ecd2006-06-12 14:11:43 +09001960 goto out;
1961
Tejun Heo022bdb02006-05-15 20:58:22 +09001962 /* skip EH if possible. */
Tejun Heo084fe632006-05-31 18:28:03 +09001963 if (ata_eh_skip_recovery(ap))
Tejun Heo022bdb02006-05-15 20:58:22 +09001964 ehc->i.action = 0;
1965
Tejun Heo084fe632006-05-31 18:28:03 +09001966 for (i = 0; i < ATA_MAX_DEVICES; i++)
1967 ehc->classes[i] = ATA_DEV_UNKNOWN;
1968
Tejun Heo022bdb02006-05-15 20:58:22 +09001969 /* reset */
1970 if (ehc->i.action & ATA_EH_RESET_MASK) {
1971 ata_eh_freeze_port(ap);
1972
Tejun Heo084fe632006-05-31 18:28:03 +09001973 rc = ata_eh_reset(ap, ata_port_nr_vacant(ap), prereset,
1974 softreset, hardreset, postreset);
Tejun Heo022bdb02006-05-15 20:58:22 +09001975 if (rc) {
1976 ata_port_printk(ap, KERN_ERR,
1977 "reset failed, giving up\n");
1978 goto out;
1979 }
1980
1981 ata_eh_thaw_port(ap);
1982 }
1983
Tejun Heo084fe632006-05-31 18:28:03 +09001984 /* revalidate existing devices and attach new ones */
1985 rc = ata_eh_revalidate_and_attach(ap, &dev);
Tejun Heo022bdb02006-05-15 20:58:22 +09001986 if (rc)
1987 goto dev_fail;
1988
Tejun Heobaa1e782006-11-01 18:39:27 +09001989 /* configure transfer mode if necessary */
1990 if (ehc->i.flags & ATA_EHI_SETMODE) {
Tejun Heo022bdb02006-05-15 20:58:22 +09001991 rc = ata_set_mode(ap, &dev);
Tejun Heo4ae72a12007-02-02 16:22:30 +09001992 if (rc)
Tejun Heo022bdb02006-05-15 20:58:22 +09001993 goto dev_fail;
Tejun Heobaa1e782006-11-01 18:39:27 +09001994 ehc->i.flags &= ~ATA_EHI_SETMODE;
Tejun Heo022bdb02006-05-15 20:58:22 +09001995 }
1996
1997 goto out;
1998
1999 dev_fail:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002000 ehc->tries[dev->devno]--;
2001
Tejun Heo022bdb02006-05-15 20:58:22 +09002002 switch (rc) {
Tejun Heo022bdb02006-05-15 20:58:22 +09002003 case -EINVAL:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002004 /* eeek, something went very wrong, give up */
Tejun Heo022bdb02006-05-15 20:58:22 +09002005 ehc->tries[dev->devno] = 0;
2006 break;
Tejun Heo4ae72a12007-02-02 16:22:30 +09002007
2008 case -ENODEV:
2009 /* device missing or wrong IDENTIFY data, schedule probing */
2010 ehc->i.probe_mask |= (1 << dev->devno);
2011 /* give it just one more chance */
2012 ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
Tejun Heo022bdb02006-05-15 20:58:22 +09002013 case -EIO:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002014 if (ehc->tries[dev->devno] == 1) {
2015 /* This is the last chance, better to slow
2016 * down than lose it.
2017 */
2018 sata_down_spd_limit(ap);
2019 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2020 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002021 }
2022
Tejun Heo084fe632006-05-31 18:28:03 +09002023 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2024 /* disable device if it has used up all its chances */
Tejun Heo022bdb02006-05-15 20:58:22 +09002025 ata_dev_disable(dev);
2026
Tejun Heo084fe632006-05-31 18:28:03 +09002027 /* detach if offline */
2028 if (ata_port_offline(ap))
2029 ata_eh_detach_dev(dev);
2030
2031 /* probe if requested */
2032 if ((ehc->i.probe_mask & (1 << dev->devno)) &&
2033 !(ehc->did_probe_mask & (1 << dev->devno))) {
2034 ata_eh_detach_dev(dev);
2035 ata_dev_init(dev);
2036
2037 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2038 ehc->did_probe_mask |= (1 << dev->devno);
2039 ehc->i.action |= ATA_EH_SOFTRESET;
2040 }
2041 } else {
2042 /* soft didn't work? be haaaaard */
2043 if (ehc->i.flags & ATA_EHI_DID_RESET)
2044 ehc->i.action |= ATA_EH_HARDRESET;
2045 else
2046 ehc->i.action |= ATA_EH_SOFTRESET;
2047 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002048
2049 if (ata_port_nr_enabled(ap)) {
2050 ata_port_printk(ap, KERN_WARNING, "failed to recover some "
2051 "devices, retrying in 5 secs\n");
2052 ssleep(5);
2053 } else {
2054 /* no device left, repeat fast */
2055 msleep(500);
2056 }
2057
2058 goto retry;
2059
2060 out:
2061 if (rc) {
2062 for (i = 0; i < ATA_MAX_DEVICES; i++)
2063 ata_dev_disable(&ap->device[i]);
2064 }
2065
2066 DPRINTK("EXIT, rc=%d\n", rc);
2067 return rc;
2068}
2069
2070/**
2071 * ata_eh_finish - finish up EH
2072 * @ap: host port to finish EH for
2073 *
2074 * Recovery is complete. Clean up EH states and retry or finish
2075 * failed qcs.
2076 *
2077 * LOCKING:
2078 * None.
2079 */
2080static void ata_eh_finish(struct ata_port *ap)
2081{
2082 int tag;
2083
2084 /* retry or finish qcs */
2085 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2086 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2087
2088 if (!(qc->flags & ATA_QCFLAG_FAILED))
2089 continue;
2090
2091 if (qc->err_mask) {
2092 /* FIXME: Once EH migration is complete,
2093 * generate sense data in this function,
2094 * considering both err_mask and tf.
2095 */
2096 if (qc->err_mask & AC_ERR_INVALID)
2097 ata_eh_qc_complete(qc);
2098 else
2099 ata_eh_qc_retry(qc);
2100 } else {
2101 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2102 ata_eh_qc_complete(qc);
2103 } else {
2104 /* feed zero TF to sense generation */
2105 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2106 ata_eh_qc_retry(qc);
2107 }
2108 }
2109 }
2110}
2111
2112/**
2113 * ata_do_eh - do standard error handling
2114 * @ap: host port to handle error for
Tejun Heof5914a42006-05-31 18:27:48 +09002115 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09002116 * @softreset: softreset method (can be NULL)
2117 * @hardreset: hardreset method (can be NULL)
2118 * @postreset: postreset method (can be NULL)
2119 *
2120 * Perform standard error handling sequence.
2121 *
2122 * LOCKING:
2123 * Kernel thread context (may sleep).
2124 */
Tejun Heof5914a42006-05-31 18:27:48 +09002125void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2126 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2127 ata_postreset_fn_t postreset)
Tejun Heo022bdb02006-05-15 20:58:22 +09002128{
Tejun Heo1cdaf532006-07-03 16:07:26 +09002129 ata_eh_autopsy(ap);
2130 ata_eh_report(ap);
Tejun Heof5914a42006-05-31 18:27:48 +09002131 ata_eh_recover(ap, prereset, softreset, hardreset, postreset);
Tejun Heo022bdb02006-05-15 20:58:22 +09002132 ata_eh_finish(ap);
2133}
Tejun Heo500530f2006-07-03 16:07:27 +09002134
Tejun Heo6ffa01d2007-03-02 17:32:47 +09002135#ifdef CONFIG_PM
Tejun Heo500530f2006-07-03 16:07:27 +09002136/**
2137 * ata_eh_handle_port_suspend - perform port suspend operation
2138 * @ap: port to suspend
2139 *
2140 * Suspend @ap.
2141 *
2142 * LOCKING:
2143 * Kernel thread context (may sleep).
2144 */
2145static void ata_eh_handle_port_suspend(struct ata_port *ap)
2146{
2147 unsigned long flags;
2148 int rc = 0;
2149
2150 /* are we suspending? */
2151 spin_lock_irqsave(ap->lock, flags);
2152 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2153 ap->pm_mesg.event == PM_EVENT_ON) {
2154 spin_unlock_irqrestore(ap->lock, flags);
2155 return;
2156 }
2157 spin_unlock_irqrestore(ap->lock, flags);
2158
2159 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
2160
2161 /* suspend */
2162 ata_eh_freeze_port(ap);
2163
2164 if (ap->ops->port_suspend)
2165 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
2166
2167 /* report result */
2168 spin_lock_irqsave(ap->lock, flags);
2169
2170 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
2171 if (rc == 0)
2172 ap->pflags |= ATA_PFLAG_SUSPENDED;
2173 else
2174 ata_port_schedule_eh(ap);
2175
2176 if (ap->pm_result) {
2177 *ap->pm_result = rc;
2178 ap->pm_result = NULL;
2179 }
2180
2181 spin_unlock_irqrestore(ap->lock, flags);
2182
2183 return;
2184}
2185
2186/**
2187 * ata_eh_handle_port_resume - perform port resume operation
2188 * @ap: port to resume
2189 *
2190 * Resume @ap.
2191 *
Tejun Heo500530f2006-07-03 16:07:27 +09002192 * LOCKING:
2193 * Kernel thread context (may sleep).
2194 */
2195static void ata_eh_handle_port_resume(struct ata_port *ap)
2196{
Tejun Heo500530f2006-07-03 16:07:27 +09002197 unsigned long flags;
Tejun Heo9666f402007-05-04 21:27:47 +02002198 int rc = 0;
Tejun Heo500530f2006-07-03 16:07:27 +09002199
2200 /* are we resuming? */
2201 spin_lock_irqsave(ap->lock, flags);
2202 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2203 ap->pm_mesg.event != PM_EVENT_ON) {
2204 spin_unlock_irqrestore(ap->lock, flags);
2205 return;
2206 }
2207 spin_unlock_irqrestore(ap->lock, flags);
2208
Tejun Heo9666f402007-05-04 21:27:47 +02002209 WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
Tejun Heo500530f2006-07-03 16:07:27 +09002210
2211 if (ap->ops->port_resume)
2212 rc = ap->ops->port_resume(ap);
2213
Tejun Heo9666f402007-05-04 21:27:47 +02002214 /* report result */
Tejun Heo500530f2006-07-03 16:07:27 +09002215 spin_lock_irqsave(ap->lock, flags);
2216 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
2217 if (ap->pm_result) {
2218 *ap->pm_result = rc;
2219 ap->pm_result = NULL;
2220 }
2221 spin_unlock_irqrestore(ap->lock, flags);
2222}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09002223#endif /* CONFIG_PM */