blob: d19666c376ad3412efbbfdac2fa59e0338ec8a81 [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
35#include <linux/config.h>
36#include <linux/kernel.h>
37#include <scsi/scsi.h>
38#include <scsi/scsi_host.h>
39#include <scsi/scsi_eh.h>
40#include <scsi/scsi_device.h>
41#include <scsi/scsi_cmnd.h>
Tejun Heof8bbfc22006-05-19 21:07:05 +090042#include "scsi_transport_api.h"
Tejun Heoece1d632006-04-02 18:51:53 +090043
44#include <linux/libata.h>
45
46#include "libata.h"
47
Tejun Heoad9e2762006-05-15 20:58:12 +090048static void __ata_port_freeze(struct ata_port *ap);
Tejun Heo720ba122006-05-31 18:28:13 +090049static void ata_eh_finish(struct ata_port *ap);
Tejun Heoad9e2762006-05-15 20:58:12 +090050
Tejun Heo0c247c52006-05-15 20:58:19 +090051static void ata_ering_record(struct ata_ering *ering, int is_io,
52 unsigned int err_mask)
53{
54 struct ata_ering_entry *ent;
55
56 WARN_ON(!err_mask);
57
58 ering->cursor++;
59 ering->cursor %= ATA_ERING_SIZE;
60
61 ent = &ering->ring[ering->cursor];
62 ent->is_io = is_io;
63 ent->err_mask = err_mask;
64 ent->timestamp = get_jiffies_64();
65}
66
67static struct ata_ering_entry * ata_ering_top(struct ata_ering *ering)
68{
69 struct ata_ering_entry *ent = &ering->ring[ering->cursor];
70 if (!ent->err_mask)
71 return NULL;
72 return ent;
73}
74
75static int ata_ering_map(struct ata_ering *ering,
76 int (*map_fn)(struct ata_ering_entry *, void *),
77 void *arg)
78{
79 int idx, rc = 0;
80 struct ata_ering_entry *ent;
81
82 idx = ering->cursor;
83 do {
84 ent = &ering->ring[idx];
85 if (!ent->err_mask)
86 break;
87 rc = map_fn(ent, arg);
88 if (rc)
89 break;
90 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
91 } while (idx != ering->cursor);
92
93 return rc;
94}
95
Tejun Heo64f65ca2006-06-24 20:30:18 +090096static unsigned int ata_eh_dev_action(struct ata_device *dev)
97{
98 struct ata_eh_context *ehc = &dev->ap->eh_context;
99
100 return ehc->i.action | ehc->i.dev_action[dev->devno];
101}
102
Tejun Heoaf181c22006-06-24 20:30:18 +0900103static void ata_eh_clear_action(struct ata_device *dev,
104 struct ata_eh_info *ehi, unsigned int action)
105{
106 int i;
107
108 if (!dev) {
109 ehi->action &= ~action;
110 for (i = 0; i < ATA_MAX_DEVICES; i++)
111 ehi->dev_action[i] &= ~action;
112 } else {
113 /* doesn't make sense for port-wide EH actions */
114 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
115
116 /* break ehi->action into ehi->dev_action */
117 if (ehi->action & action) {
118 for (i = 0; i < ATA_MAX_DEVICES; i++)
119 ehi->dev_action[i] |= ehi->action & action;
120 ehi->action &= ~action;
121 }
122
123 /* turn off the specified per-dev action */
124 ehi->dev_action[dev->devno] &= ~action;
125 }
126}
127
Tejun Heoece1d632006-04-02 18:51:53 +0900128/**
129 * ata_scsi_timed_out - SCSI layer time out callback
130 * @cmd: timed out SCSI command
131 *
132 * Handles SCSI layer timeout. We race with normal completion of
133 * the qc for @cmd. If the qc is already gone, we lose and let
134 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
135 * timed out and EH should be invoked. Prevent ata_qc_complete()
136 * from finishing it by setting EH_SCHEDULED and return
137 * EH_NOT_HANDLED.
138 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900139 * TODO: kill this function once old EH is gone.
140 *
Tejun Heoece1d632006-04-02 18:51:53 +0900141 * LOCKING:
142 * Called from timer context
143 *
144 * RETURNS:
145 * EH_HANDLED or EH_NOT_HANDLED
146 */
147enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
148{
149 struct Scsi_Host *host = cmd->device->host;
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400150 struct ata_port *ap = ata_shost_to_port(host);
Tejun Heoece1d632006-04-02 18:51:53 +0900151 unsigned long flags;
152 struct ata_queued_cmd *qc;
Tejun Heoad9e2762006-05-15 20:58:12 +0900153 enum scsi_eh_timer_return ret;
Tejun Heoece1d632006-04-02 18:51:53 +0900154
155 DPRINTK("ENTER\n");
156
Tejun Heoad9e2762006-05-15 20:58:12 +0900157 if (ap->ops->error_handler) {
158 ret = EH_NOT_HANDLED;
159 goto out;
160 }
161
162 ret = EH_HANDLED;
Jeff Garzikba6a1302006-06-22 23:46:10 -0400163 spin_lock_irqsave(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900164 qc = ata_qc_from_tag(ap, ap->active_tag);
165 if (qc) {
166 WARN_ON(qc->scsicmd != cmd);
167 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
168 qc->err_mask |= AC_ERR_TIMEOUT;
169 ret = EH_NOT_HANDLED;
170 }
Jeff Garzikba6a1302006-06-22 23:46:10 -0400171 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900172
Tejun Heoad9e2762006-05-15 20:58:12 +0900173 out:
Tejun Heoece1d632006-04-02 18:51:53 +0900174 DPRINTK("EXIT, ret=%d\n", ret);
175 return ret;
176}
177
178/**
179 * ata_scsi_error - SCSI layer error handler callback
180 * @host: SCSI host on which error occurred
181 *
182 * Handles SCSI-layer-thrown error events.
183 *
184 * LOCKING:
185 * Inherited from SCSI layer (none, can sleep)
186 *
187 * RETURNS:
188 * Zero.
189 */
Jeff Garzik381544b2006-04-11 13:04:39 -0400190void ata_scsi_error(struct Scsi_Host *host)
Tejun Heoece1d632006-04-02 18:51:53 +0900191{
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400192 struct ata_port *ap = ata_shost_to_port(host);
Tejun Heoad9e2762006-05-15 20:58:12 +0900193 int i, repeat_cnt = ATA_EH_MAX_REPEAT;
194 unsigned long flags;
Tejun Heoece1d632006-04-02 18:51:53 +0900195
196 DPRINTK("ENTER\n");
197
Tejun Heoad9e2762006-05-15 20:58:12 +0900198 /* synchronize with port task */
Tejun Heoece1d632006-04-02 18:51:53 +0900199 ata_port_flush_task(ap);
200
Tejun Heoad9e2762006-05-15 20:58:12 +0900201 /* synchronize with host_set lock and sort out timeouts */
Tejun Heoece1d632006-04-02 18:51:53 +0900202
Tejun Heoad9e2762006-05-15 20:58:12 +0900203 /* For new EH, all qcs are finished in one of three ways -
204 * normal completion, error completion, and SCSI timeout.
205 * Both cmpletions can race against SCSI timeout. When normal
206 * completion wins, the qc never reaches EH. When error
207 * completion wins, the qc has ATA_QCFLAG_FAILED set.
208 *
209 * When SCSI timeout wins, things are a bit more complex.
210 * Normal or error completion can occur after the timeout but
211 * before this point. In such cases, both types of
212 * completions are honored. A scmd is determined to have
213 * timed out iff its associated qc is active and not failed.
214 */
215 if (ap->ops->error_handler) {
216 struct scsi_cmnd *scmd, *tmp;
217 int nr_timedout = 0;
Tejun Heoece1d632006-04-02 18:51:53 +0900218
Tejun Heoe30349d2006-07-03 03:02:15 +0900219 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900220
221 list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
222 struct ata_queued_cmd *qc;
223
224 for (i = 0; i < ATA_MAX_QUEUE; i++) {
225 qc = __ata_qc_from_tag(ap, i);
226 if (qc->flags & ATA_QCFLAG_ACTIVE &&
227 qc->scsicmd == scmd)
228 break;
229 }
230
231 if (i < ATA_MAX_QUEUE) {
232 /* the scmd has an associated qc */
233 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
234 /* which hasn't failed yet, timeout */
235 qc->err_mask |= AC_ERR_TIMEOUT;
236 qc->flags |= ATA_QCFLAG_FAILED;
237 nr_timedout++;
238 }
239 } else {
240 /* Normal completion occurred after
241 * SCSI timeout but before this point.
242 * Successfully complete it.
243 */
244 scmd->retries = scmd->allowed;
245 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
246 }
247 }
248
249 /* If we have timed out qcs. They belong to EH from
250 * this point but the state of the controller is
251 * unknown. Freeze the port to make sure the IRQ
252 * handler doesn't diddle with those qcs. This must
253 * be done atomically w.r.t. setting QCFLAG_FAILED.
254 */
255 if (nr_timedout)
256 __ata_port_freeze(ap);
257
Tejun Heoe30349d2006-07-03 03:02:15 +0900258 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900259 } else
Tejun Heoe30349d2006-07-03 03:02:15 +0900260 spin_unlock_wait(ap->lock);
Tejun Heoad9e2762006-05-15 20:58:12 +0900261
262 repeat:
263 /* invoke error handler */
264 if (ap->ops->error_handler) {
Tejun Heof3e81b12006-05-15 20:58:21 +0900265 /* fetch & clear EH info */
Tejun Heoe30349d2006-07-03 03:02:15 +0900266 spin_lock_irqsave(ap->lock, flags);
Tejun Heof3e81b12006-05-15 20:58:21 +0900267
268 memset(&ap->eh_context, 0, sizeof(ap->eh_context));
269 ap->eh_context.i = ap->eh_info;
270 memset(&ap->eh_info, 0, sizeof(ap->eh_info));
271
Tejun Heob51e9e52006-06-29 01:29:30 +0900272 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
273 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
Tejun Heof3e81b12006-05-15 20:58:21 +0900274
Tejun Heoe30349d2006-07-03 03:02:15 +0900275 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900276
Tejun Heo720ba122006-05-31 18:28:13 +0900277 /* invoke EH. if unloading, just finish failed qcs */
Tejun Heob51e9e52006-06-29 01:29:30 +0900278 if (!(ap->pflags & ATA_PFLAG_UNLOADING))
Tejun Heo720ba122006-05-31 18:28:13 +0900279 ap->ops->error_handler(ap);
280 else
281 ata_eh_finish(ap);
Tejun Heoad9e2762006-05-15 20:58:12 +0900282
283 /* Exception might have happend after ->error_handler
284 * recovered the port but before this point. Repeat
285 * EH in such case.
286 */
Tejun Heoe30349d2006-07-03 03:02:15 +0900287 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900288
Tejun Heob51e9e52006-06-29 01:29:30 +0900289 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
Tejun Heoad9e2762006-05-15 20:58:12 +0900290 if (--repeat_cnt) {
291 ata_port_printk(ap, KERN_INFO,
292 "EH pending after completion, "
293 "repeating EH (cnt=%d)\n", repeat_cnt);
Tejun Heoe30349d2006-07-03 03:02:15 +0900294 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900295 goto repeat;
296 }
297 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
298 "tries, giving up\n", ATA_EH_MAX_REPEAT);
299 }
300
Tejun Heof3e81b12006-05-15 20:58:21 +0900301 /* this run is complete, make sure EH info is clear */
302 memset(&ap->eh_info, 0, sizeof(ap->eh_info));
303
Tejun Heoe30349d2006-07-03 03:02:15 +0900304 /* Clear host_eh_scheduled while holding ap->lock such
Tejun Heoad9e2762006-05-15 20:58:12 +0900305 * that if exception occurs after this point but
306 * before EH completion, SCSI midlayer will
307 * re-initiate EH.
308 */
309 host->host_eh_scheduled = 0;
310
Tejun Heoe30349d2006-07-03 03:02:15 +0900311 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900312 } else {
313 WARN_ON(ata_qc_from_tag(ap, ap->active_tag) == NULL);
314 ap->ops->eng_timeout(ap);
315 }
316
317 /* finish or retry handled scmd's and clean up */
Tejun Heoece1d632006-04-02 18:51:53 +0900318 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
319
320 scsi_eh_flush_done_q(&ap->eh_done_q);
321
Tejun Heoad9e2762006-05-15 20:58:12 +0900322 /* clean up */
Tejun Heoe30349d2006-07-03 03:02:15 +0900323 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900324
Tejun Heob51e9e52006-06-29 01:29:30 +0900325 if (ap->pflags & ATA_PFLAG_LOADING) {
326 ap->pflags &= ~ATA_PFLAG_LOADING;
Tejun Heo3e706392006-05-31 18:28:11 +0900327 } else {
Tejun Heob51e9e52006-06-29 01:29:30 +0900328 if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
Tejun Heo3e706392006-05-31 18:28:11 +0900329 queue_work(ata_aux_wq, &ap->hotplug_task);
Tejun Heob51e9e52006-06-29 01:29:30 +0900330 if (ap->pflags & ATA_PFLAG_RECOVERED)
Tejun Heo3e706392006-05-31 18:28:11 +0900331 ata_port_printk(ap, KERN_INFO, "EH complete\n");
332 }
Tejun Heo580b21022006-05-31 18:28:05 +0900333
Tejun Heob51e9e52006-06-29 01:29:30 +0900334 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
Tejun Heoad9e2762006-05-15 20:58:12 +0900335
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900336 /* tell wait_eh that we're done */
Tejun Heob51e9e52006-06-29 01:29:30 +0900337 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900338 wake_up_all(&ap->eh_wait_q);
339
Tejun Heoe30349d2006-07-03 03:02:15 +0900340 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900341
Tejun Heoece1d632006-04-02 18:51:53 +0900342 DPRINTK("EXIT\n");
Tejun Heoece1d632006-04-02 18:51:53 +0900343}
344
345/**
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900346 * ata_port_wait_eh - Wait for the currently pending EH to complete
347 * @ap: Port to wait EH for
348 *
349 * Wait until the currently pending EH is complete.
350 *
351 * LOCKING:
352 * Kernel thread context (may sleep).
353 */
354void ata_port_wait_eh(struct ata_port *ap)
355{
356 unsigned long flags;
357 DEFINE_WAIT(wait);
358
359 retry:
Jeff Garzikba6a1302006-06-22 23:46:10 -0400360 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900361
Tejun Heob51e9e52006-06-29 01:29:30 +0900362 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900363 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
Jeff Garzikba6a1302006-06-22 23:46:10 -0400364 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900365 schedule();
Jeff Garzikba6a1302006-06-22 23:46:10 -0400366 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900367 }
Tejun Heo0a1b6222006-06-11 11:01:38 +0900368 finish_wait(&ap->eh_wait_q, &wait);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900369
Jeff Garzikba6a1302006-06-22 23:46:10 -0400370 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900371
372 /* make sure SCSI EH is complete */
373 if (scsi_host_in_recovery(ap->host)) {
374 msleep(10);
375 goto retry;
376 }
377}
378
379/**
Tejun Heoece1d632006-04-02 18:51:53 +0900380 * ata_qc_timeout - Handle timeout of queued command
381 * @qc: Command that timed out
382 *
383 * Some part of the kernel (currently, only the SCSI layer)
384 * has noticed that the active command on port @ap has not
385 * completed after a specified length of time. Handle this
386 * condition by disabling DMA (if necessary) and completing
387 * transactions, with error if necessary.
388 *
389 * This also handles the case of the "lost interrupt", where
390 * for some reason (possibly hardware bug, possibly driver bug)
391 * an interrupt was not delivered to the driver, even though the
392 * transaction completed successfully.
393 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900394 * TODO: kill this function once old EH is gone.
395 *
Tejun Heoece1d632006-04-02 18:51:53 +0900396 * LOCKING:
397 * Inherited from SCSI layer (none, can sleep)
398 */
399static void ata_qc_timeout(struct ata_queued_cmd *qc)
400{
401 struct ata_port *ap = qc->ap;
Tejun Heoece1d632006-04-02 18:51:53 +0900402 u8 host_stat = 0, drv_stat;
403 unsigned long flags;
404
405 DPRINTK("ENTER\n");
406
407 ap->hsm_task_state = HSM_ST_IDLE;
408
Jeff Garzikba6a1302006-06-22 23:46:10 -0400409 spin_lock_irqsave(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900410
411 switch (qc->tf.protocol) {
412
413 case ATA_PROT_DMA:
414 case ATA_PROT_ATAPI_DMA:
415 host_stat = ap->ops->bmdma_status(ap);
416
417 /* before we do anything else, clear DMA-Start bit */
418 ap->ops->bmdma_stop(qc);
419
420 /* fall through */
421
422 default:
423 ata_altstatus(ap);
424 drv_stat = ata_chk_status(ap);
425
426 /* ack bmdma irq events */
427 ap->ops->irq_clear(ap);
428
Tejun Heof15a1da2006-05-15 20:57:56 +0900429 ata_dev_printk(qc->dev, KERN_ERR, "command 0x%x timeout, "
430 "stat 0x%x host_stat 0x%x\n",
431 qc->tf.command, drv_stat, host_stat);
Tejun Heoece1d632006-04-02 18:51:53 +0900432
433 /* complete taskfile transaction */
Jeff Garzikc13b56a2006-04-02 10:34:24 -0400434 qc->err_mask |= AC_ERR_TIMEOUT;
Tejun Heoece1d632006-04-02 18:51:53 +0900435 break;
436 }
437
Jeff Garzikba6a1302006-06-22 23:46:10 -0400438 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900439
440 ata_eh_qc_complete(qc);
441
442 DPRINTK("EXIT\n");
443}
444
445/**
446 * ata_eng_timeout - Handle timeout of queued command
447 * @ap: Port on which timed-out command is active
448 *
449 * Some part of the kernel (currently, only the SCSI layer)
450 * has noticed that the active command on port @ap has not
451 * completed after a specified length of time. Handle this
452 * condition by disabling DMA (if necessary) and completing
453 * transactions, with error if necessary.
454 *
455 * This also handles the case of the "lost interrupt", where
456 * for some reason (possibly hardware bug, possibly driver bug)
457 * an interrupt was not delivered to the driver, even though the
458 * transaction completed successfully.
459 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900460 * TODO: kill this function once old EH is gone.
461 *
Tejun Heoece1d632006-04-02 18:51:53 +0900462 * LOCKING:
463 * Inherited from SCSI layer (none, can sleep)
464 */
465void ata_eng_timeout(struct ata_port *ap)
466{
467 DPRINTK("ENTER\n");
468
469 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
470
471 DPRINTK("EXIT\n");
472}
473
Tejun Heof686bcb2006-05-15 20:58:05 +0900474/**
475 * ata_qc_schedule_eh - schedule qc for error handling
476 * @qc: command to schedule error handling for
477 *
478 * Schedule error handling for @qc. EH will kick in as soon as
479 * other commands are drained.
480 *
481 * LOCKING:
482 * spin_lock_irqsave(host_set lock)
483 */
484void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
485{
486 struct ata_port *ap = qc->ap;
487
488 WARN_ON(!ap->ops->error_handler);
489
490 qc->flags |= ATA_QCFLAG_FAILED;
Tejun Heob51e9e52006-06-29 01:29:30 +0900491 qc->ap->pflags |= ATA_PFLAG_EH_PENDING;
Tejun Heof686bcb2006-05-15 20:58:05 +0900492
493 /* The following will fail if timeout has already expired.
494 * ata_scsi_error() takes care of such scmds on EH entry.
495 * Note that ATA_QCFLAG_FAILED is unconditionally set after
496 * this function completes.
497 */
498 scsi_req_abort_cmd(qc->scsicmd);
499}
500
Tejun Heo7b70fc02006-05-15 20:58:07 +0900501/**
502 * ata_port_schedule_eh - schedule error handling without a qc
503 * @ap: ATA port to schedule EH for
504 *
505 * Schedule error handling for @ap. EH will kick in as soon as
506 * all commands are drained.
507 *
508 * LOCKING:
509 * spin_lock_irqsave(host_set lock)
510 */
511void ata_port_schedule_eh(struct ata_port *ap)
512{
513 WARN_ON(!ap->ops->error_handler);
514
Tejun Heob51e9e52006-06-29 01:29:30 +0900515 ap->pflags |= ATA_PFLAG_EH_PENDING;
Tejun Heof8bbfc22006-05-19 21:07:05 +0900516 scsi_schedule_eh(ap->host);
Tejun Heo7b70fc02006-05-15 20:58:07 +0900517
518 DPRINTK("port EH scheduled\n");
519}
520
521/**
522 * ata_port_abort - abort all qc's on the port
523 * @ap: ATA port to abort qc's for
524 *
525 * Abort all active qc's of @ap and schedule EH.
526 *
527 * LOCKING:
528 * spin_lock_irqsave(host_set lock)
529 *
530 * RETURNS:
531 * Number of aborted qc's.
532 */
533int ata_port_abort(struct ata_port *ap)
534{
535 int tag, nr_aborted = 0;
536
537 WARN_ON(!ap->ops->error_handler);
538
539 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
540 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
541
542 if (qc) {
543 qc->flags |= ATA_QCFLAG_FAILED;
544 ata_qc_complete(qc);
545 nr_aborted++;
546 }
547 }
548
549 if (!nr_aborted)
550 ata_port_schedule_eh(ap);
551
552 return nr_aborted;
553}
554
Tejun Heoe3180492006-05-15 20:58:09 +0900555/**
556 * __ata_port_freeze - freeze port
557 * @ap: ATA port to freeze
558 *
559 * This function is called when HSM violation or some other
560 * condition disrupts normal operation of the port. Frozen port
561 * is not allowed to perform any operation until the port is
562 * thawed, which usually follows a successful reset.
563 *
564 * ap->ops->freeze() callback can be used for freezing the port
565 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
566 * port cannot be frozen hardware-wise, the interrupt handler
567 * must ack and clear interrupts unconditionally while the port
568 * is frozen.
569 *
570 * LOCKING:
571 * spin_lock_irqsave(host_set lock)
572 */
573static void __ata_port_freeze(struct ata_port *ap)
574{
575 WARN_ON(!ap->ops->error_handler);
576
577 if (ap->ops->freeze)
578 ap->ops->freeze(ap);
579
Tejun Heob51e9e52006-06-29 01:29:30 +0900580 ap->pflags |= ATA_PFLAG_FROZEN;
Tejun Heoe3180492006-05-15 20:58:09 +0900581
582 DPRINTK("ata%u port frozen\n", ap->id);
583}
584
585/**
586 * ata_port_freeze - abort & freeze port
587 * @ap: ATA port to freeze
588 *
589 * Abort and freeze @ap.
590 *
591 * LOCKING:
592 * spin_lock_irqsave(host_set lock)
593 *
594 * RETURNS:
595 * Number of aborted commands.
596 */
597int ata_port_freeze(struct ata_port *ap)
598{
599 int nr_aborted;
600
601 WARN_ON(!ap->ops->error_handler);
602
603 nr_aborted = ata_port_abort(ap);
604 __ata_port_freeze(ap);
605
606 return nr_aborted;
607}
608
609/**
610 * ata_eh_freeze_port - EH helper to freeze port
611 * @ap: ATA port to freeze
612 *
613 * Freeze @ap.
614 *
615 * LOCKING:
616 * None.
617 */
618void ata_eh_freeze_port(struct ata_port *ap)
619{
620 unsigned long flags;
621
622 if (!ap->ops->error_handler)
623 return;
624
Jeff Garzikba6a1302006-06-22 23:46:10 -0400625 spin_lock_irqsave(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900626 __ata_port_freeze(ap);
Jeff Garzikba6a1302006-06-22 23:46:10 -0400627 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900628}
629
630/**
631 * ata_port_thaw_port - EH helper to thaw port
632 * @ap: ATA port to thaw
633 *
634 * Thaw frozen port @ap.
635 *
636 * LOCKING:
637 * None.
638 */
639void ata_eh_thaw_port(struct ata_port *ap)
640{
641 unsigned long flags;
642
643 if (!ap->ops->error_handler)
644 return;
645
Jeff Garzikba6a1302006-06-22 23:46:10 -0400646 spin_lock_irqsave(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900647
Tejun Heob51e9e52006-06-29 01:29:30 +0900648 ap->pflags &= ~ATA_PFLAG_FROZEN;
Tejun Heoe3180492006-05-15 20:58:09 +0900649
650 if (ap->ops->thaw)
651 ap->ops->thaw(ap);
652
Jeff Garzikba6a1302006-06-22 23:46:10 -0400653 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900654
655 DPRINTK("ata%u port thawed\n", ap->id);
656}
657
Tejun Heoece1d632006-04-02 18:51:53 +0900658static void ata_eh_scsidone(struct scsi_cmnd *scmd)
659{
660 /* nada */
661}
662
663static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
664{
665 struct ata_port *ap = qc->ap;
666 struct scsi_cmnd *scmd = qc->scsicmd;
667 unsigned long flags;
668
Jeff Garzikba6a1302006-06-22 23:46:10 -0400669 spin_lock_irqsave(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900670 qc->scsidone = ata_eh_scsidone;
671 __ata_qc_complete(qc);
672 WARN_ON(ata_tag_valid(qc->tag));
Jeff Garzikba6a1302006-06-22 23:46:10 -0400673 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900674
675 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
676}
677
678/**
679 * ata_eh_qc_complete - Complete an active ATA command from EH
680 * @qc: Command to complete
681 *
682 * Indicate to the mid and upper layers that an ATA command has
683 * completed. To be used from EH.
684 */
685void ata_eh_qc_complete(struct ata_queued_cmd *qc)
686{
687 struct scsi_cmnd *scmd = qc->scsicmd;
688 scmd->retries = scmd->allowed;
689 __ata_eh_qc_complete(qc);
690}
691
692/**
693 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
694 * @qc: Command to retry
695 *
696 * Indicate to the mid and upper layers that an ATA command
697 * should be retried. To be used from EH.
698 *
699 * SCSI midlayer limits the number of retries to scmd->allowed.
700 * scmd->retries is decremented for commands which get retried
701 * due to unrelated failures (qc->err_mask is zero).
702 */
703void ata_eh_qc_retry(struct ata_queued_cmd *qc)
704{
705 struct scsi_cmnd *scmd = qc->scsicmd;
706 if (!qc->err_mask && scmd->retries)
707 scmd->retries--;
708 __ata_eh_qc_complete(qc);
709}
Tejun Heo022bdb02006-05-15 20:58:22 +0900710
711/**
Tejun Heo0ea035a2006-05-31 18:28:01 +0900712 * ata_eh_detach_dev - detach ATA device
713 * @dev: ATA device to detach
714 *
715 * Detach @dev.
716 *
717 * LOCKING:
718 * None.
719 */
720static void ata_eh_detach_dev(struct ata_device *dev)
721{
722 struct ata_port *ap = dev->ap;
723 unsigned long flags;
724
725 ata_dev_disable(dev);
726
Jeff Garzikba6a1302006-06-22 23:46:10 -0400727 spin_lock_irqsave(ap->lock, flags);
Tejun Heo0ea035a2006-05-31 18:28:01 +0900728
729 dev->flags &= ~ATA_DFLAG_DETACH;
730
731 if (ata_scsi_offline_dev(dev)) {
732 dev->flags |= ATA_DFLAG_DETACHED;
Tejun Heob51e9e52006-06-29 01:29:30 +0900733 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
Tejun Heo0ea035a2006-05-31 18:28:01 +0900734 }
735
Tejun Heobeb07c12006-06-24 20:30:19 +0900736 /* clear per-dev EH actions */
737 ata_eh_clear_action(dev, &ap->eh_info, ATA_EH_PERDEV_MASK);
738 ata_eh_clear_action(dev, &ap->eh_context.i, ATA_EH_PERDEV_MASK);
739
Jeff Garzikba6a1302006-06-22 23:46:10 -0400740 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo0ea035a2006-05-31 18:28:01 +0900741}
742
743/**
Tejun Heo022bdb02006-05-15 20:58:22 +0900744 * ata_eh_about_to_do - about to perform eh_action
745 * @ap: target ATA port
Tejun Heo47005f22006-06-19 18:27:23 +0900746 * @dev: target ATA dev for per-dev action (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +0900747 * @action: action about to be performed
748 *
749 * Called just before performing EH actions to clear related bits
750 * in @ap->eh_info such that eh actions are not unnecessarily
751 * repeated.
752 *
753 * LOCKING:
754 * None.
755 */
Tejun Heo47005f22006-06-19 18:27:23 +0900756static void ata_eh_about_to_do(struct ata_port *ap, struct ata_device *dev,
757 unsigned int action)
Tejun Heo022bdb02006-05-15 20:58:22 +0900758{
759 unsigned long flags;
760
Jeff Garzikba6a1302006-06-22 23:46:10 -0400761 spin_lock_irqsave(ap->lock, flags);
Tejun Heo47005f22006-06-19 18:27:23 +0900762 ata_eh_clear_action(dev, &ap->eh_info, action);
Tejun Heob51e9e52006-06-29 01:29:30 +0900763 ap->pflags |= ATA_PFLAG_RECOVERED;
Jeff Garzikba6a1302006-06-22 23:46:10 -0400764 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo022bdb02006-05-15 20:58:22 +0900765}
766
767/**
Tejun Heo47005f22006-06-19 18:27:23 +0900768 * ata_eh_done - EH action complete
769 * @ap: target ATA port
770 * @dev: target ATA dev for per-dev action (can be NULL)
771 * @action: action just completed
772 *
773 * Called right after performing EH actions to clear related bits
774 * in @ap->eh_context.
775 *
776 * LOCKING:
777 * None.
778 */
779static void ata_eh_done(struct ata_port *ap, struct ata_device *dev,
780 unsigned int action)
781{
782 ata_eh_clear_action(dev, &ap->eh_context.i, action);
783}
784
785/**
Tejun Heo022bdb02006-05-15 20:58:22 +0900786 * ata_err_string - convert err_mask to descriptive string
787 * @err_mask: error mask to convert to string
788 *
789 * Convert @err_mask to descriptive string. Errors are
790 * prioritized according to severity and only the most severe
791 * error is reported.
792 *
793 * LOCKING:
794 * None.
795 *
796 * RETURNS:
797 * Descriptive string for @err_mask
798 */
799static const char * ata_err_string(unsigned int err_mask)
800{
801 if (err_mask & AC_ERR_HOST_BUS)
802 return "host bus error";
803 if (err_mask & AC_ERR_ATA_BUS)
804 return "ATA bus error";
805 if (err_mask & AC_ERR_TIMEOUT)
806 return "timeout";
807 if (err_mask & AC_ERR_HSM)
808 return "HSM violation";
809 if (err_mask & AC_ERR_SYSTEM)
810 return "internal error";
811 if (err_mask & AC_ERR_MEDIA)
812 return "media error";
813 if (err_mask & AC_ERR_INVALID)
814 return "invalid argument";
815 if (err_mask & AC_ERR_DEV)
816 return "device error";
817 return "unknown error";
818}
819
820/**
Tejun Heoe8ee8452006-05-15 21:03:46 +0900821 * ata_read_log_page - read a specific log page
822 * @dev: target device
823 * @page: page to read
824 * @buf: buffer to store read page
825 * @sectors: number of sectors to read
826 *
827 * Read log page using READ_LOG_EXT command.
828 *
829 * LOCKING:
830 * Kernel thread context (may sleep).
831 *
832 * RETURNS:
833 * 0 on success, AC_ERR_* mask otherwise.
834 */
835static unsigned int ata_read_log_page(struct ata_device *dev,
836 u8 page, void *buf, unsigned int sectors)
837{
838 struct ata_taskfile tf;
839 unsigned int err_mask;
840
841 DPRINTK("read log page - page %d\n", page);
842
843 ata_tf_init(dev, &tf);
844 tf.command = ATA_CMD_READ_LOG_EXT;
845 tf.lbal = page;
846 tf.nsect = sectors;
847 tf.hob_nsect = sectors >> 8;
848 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
849 tf.protocol = ATA_PROT_PIO;
850
851 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
852 buf, sectors * ATA_SECT_SIZE);
853
854 DPRINTK("EXIT, err_mask=%x\n", err_mask);
855 return err_mask;
856}
857
858/**
859 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
860 * @dev: Device to read log page 10h from
861 * @tag: Resulting tag of the failed command
862 * @tf: Resulting taskfile registers of the failed command
863 *
864 * Read log page 10h to obtain NCQ error details and clear error
865 * condition.
866 *
867 * LOCKING:
868 * Kernel thread context (may sleep).
869 *
870 * RETURNS:
871 * 0 on success, -errno otherwise.
872 */
873static int ata_eh_read_log_10h(struct ata_device *dev,
874 int *tag, struct ata_taskfile *tf)
875{
876 u8 *buf = dev->ap->sector_buf;
877 unsigned int err_mask;
878 u8 csum;
879 int i;
880
881 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
882 if (err_mask)
883 return -EIO;
884
885 csum = 0;
886 for (i = 0; i < ATA_SECT_SIZE; i++)
887 csum += buf[i];
888 if (csum)
889 ata_dev_printk(dev, KERN_WARNING,
890 "invalid checksum 0x%x on log page 10h\n", csum);
891
892 if (buf[0] & 0x80)
893 return -ENOENT;
894
895 *tag = buf[0] & 0x1f;
896
897 tf->command = buf[2];
898 tf->feature = buf[3];
899 tf->lbal = buf[4];
900 tf->lbam = buf[5];
901 tf->lbah = buf[6];
902 tf->device = buf[7];
903 tf->hob_lbal = buf[8];
904 tf->hob_lbam = buf[9];
905 tf->hob_lbah = buf[10];
906 tf->nsect = buf[12];
907 tf->hob_nsect = buf[13];
908
909 return 0;
910}
911
912/**
Tejun Heo022bdb02006-05-15 20:58:22 +0900913 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
914 * @dev: device to perform REQUEST_SENSE to
915 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
916 *
917 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
918 * SENSE. This function is EH helper.
919 *
920 * LOCKING:
921 * Kernel thread context (may sleep).
922 *
923 * RETURNS:
924 * 0 on success, AC_ERR_* mask on failure
925 */
926static unsigned int atapi_eh_request_sense(struct ata_device *dev,
927 unsigned char *sense_buf)
928{
929 struct ata_port *ap = dev->ap;
930 struct ata_taskfile tf;
931 u8 cdb[ATAPI_CDB_LEN];
932
933 DPRINTK("ATAPI request sense\n");
934
935 ata_tf_init(dev, &tf);
936
937 /* FIXME: is this needed? */
938 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
939
940 /* XXX: why tf_read here? */
941 ap->ops->tf_read(ap, &tf);
942
943 /* fill these in, for the case where they are -not- overwritten */
944 sense_buf[0] = 0x70;
945 sense_buf[2] = tf.feature >> 4;
946
947 memset(cdb, 0, ATAPI_CDB_LEN);
948 cdb[0] = REQUEST_SENSE;
949 cdb[4] = SCSI_SENSE_BUFFERSIZE;
950
951 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
952 tf.command = ATA_CMD_PACKET;
953
954 /* is it pointless to prefer PIO for "safety reasons"? */
955 if (ap->flags & ATA_FLAG_PIO_DMA) {
956 tf.protocol = ATA_PROT_ATAPI_DMA;
957 tf.feature |= ATAPI_PKT_DMA;
958 } else {
959 tf.protocol = ATA_PROT_ATAPI;
960 tf.lbam = (8 * 1024) & 0xff;
961 tf.lbah = (8 * 1024) >> 8;
962 }
963
964 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
965 sense_buf, SCSI_SENSE_BUFFERSIZE);
966}
967
968/**
969 * ata_eh_analyze_serror - analyze SError for a failed port
970 * @ap: ATA port to analyze SError for
971 *
972 * Analyze SError if available and further determine cause of
973 * failure.
974 *
975 * LOCKING:
976 * None.
977 */
978static void ata_eh_analyze_serror(struct ata_port *ap)
979{
980 struct ata_eh_context *ehc = &ap->eh_context;
981 u32 serror = ehc->i.serror;
982 unsigned int err_mask = 0, action = 0;
983
984 if (serror & SERR_PERSISTENT) {
985 err_mask |= AC_ERR_ATA_BUS;
986 action |= ATA_EH_HARDRESET;
987 }
988 if (serror &
989 (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
990 err_mask |= AC_ERR_ATA_BUS;
991 action |= ATA_EH_SOFTRESET;
992 }
993 if (serror & SERR_PROTOCOL) {
994 err_mask |= AC_ERR_HSM;
995 action |= ATA_EH_SOFTRESET;
996 }
997 if (serror & SERR_INTERNAL) {
998 err_mask |= AC_ERR_SYSTEM;
999 action |= ATA_EH_SOFTRESET;
1000 }
Tejun Heo084fe632006-05-31 18:28:03 +09001001 if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG))
1002 ata_ehi_hotplugged(&ehc->i);
Tejun Heo022bdb02006-05-15 20:58:22 +09001003
1004 ehc->i.err_mask |= err_mask;
1005 ehc->i.action |= action;
1006}
1007
1008/**
Tejun Heoe8ee8452006-05-15 21:03:46 +09001009 * ata_eh_analyze_ncq_error - analyze NCQ error
1010 * @ap: ATA port to analyze NCQ error for
1011 *
1012 * Read log page 10h, determine the offending qc and acquire
1013 * error status TF. For NCQ device errors, all LLDDs have to do
1014 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1015 * care of the rest.
1016 *
1017 * LOCKING:
1018 * Kernel thread context (may sleep).
1019 */
1020static void ata_eh_analyze_ncq_error(struct ata_port *ap)
1021{
1022 struct ata_eh_context *ehc = &ap->eh_context;
1023 struct ata_device *dev = ap->device;
1024 struct ata_queued_cmd *qc;
1025 struct ata_taskfile tf;
1026 int tag, rc;
1027
1028 /* if frozen, we can't do much */
Tejun Heob51e9e52006-06-29 01:29:30 +09001029 if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heoe8ee8452006-05-15 21:03:46 +09001030 return;
1031
1032 /* is it NCQ device error? */
1033 if (!ap->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1034 return;
1035
1036 /* has LLDD analyzed already? */
1037 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1038 qc = __ata_qc_from_tag(ap, tag);
1039
1040 if (!(qc->flags & ATA_QCFLAG_FAILED))
1041 continue;
1042
1043 if (qc->err_mask)
1044 return;
1045 }
1046
1047 /* okay, this error is ours */
1048 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1049 if (rc) {
1050 ata_port_printk(ap, KERN_ERR, "failed to read log page 10h "
1051 "(errno=%d)\n", rc);
1052 return;
1053 }
1054
1055 if (!(ap->sactive & (1 << tag))) {
1056 ata_port_printk(ap, KERN_ERR, "log page 10h reported "
1057 "inactive tag %d\n", tag);
1058 return;
1059 }
1060
1061 /* we've got the perpetrator, condemn it */
1062 qc = __ata_qc_from_tag(ap, tag);
1063 memcpy(&qc->result_tf, &tf, sizeof(tf));
1064 qc->err_mask |= AC_ERR_DEV;
1065 ehc->i.err_mask &= ~AC_ERR_DEV;
1066}
1067
1068/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001069 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1070 * @qc: qc to analyze
1071 * @tf: Taskfile registers to analyze
1072 *
1073 * Analyze taskfile of @qc and further determine cause of
1074 * failure. This function also requests ATAPI sense data if
1075 * avaliable.
1076 *
1077 * LOCKING:
1078 * Kernel thread context (may sleep).
1079 *
1080 * RETURNS:
1081 * Determined recovery action
1082 */
1083static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1084 const struct ata_taskfile *tf)
1085{
1086 unsigned int tmp, action = 0;
1087 u8 stat = tf->command, err = tf->feature;
1088
1089 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1090 qc->err_mask |= AC_ERR_HSM;
1091 return ATA_EH_SOFTRESET;
1092 }
1093
1094 if (!(qc->err_mask & AC_ERR_DEV))
1095 return 0;
1096
1097 switch (qc->dev->class) {
1098 case ATA_DEV_ATA:
1099 if (err & ATA_ICRC)
1100 qc->err_mask |= AC_ERR_ATA_BUS;
1101 if (err & ATA_UNC)
1102 qc->err_mask |= AC_ERR_MEDIA;
1103 if (err & ATA_IDNF)
1104 qc->err_mask |= AC_ERR_INVALID;
1105 break;
1106
1107 case ATA_DEV_ATAPI:
1108 tmp = atapi_eh_request_sense(qc->dev,
1109 qc->scsicmd->sense_buffer);
1110 if (!tmp) {
1111 /* ATA_QCFLAG_SENSE_VALID is used to tell
1112 * atapi_qc_complete() that sense data is
1113 * already valid.
1114 *
1115 * TODO: interpret sense data and set
1116 * appropriate err_mask.
1117 */
1118 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1119 } else
1120 qc->err_mask |= tmp;
1121 }
1122
1123 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1124 action |= ATA_EH_SOFTRESET;
1125
1126 return action;
1127}
1128
1129static int ata_eh_categorize_ering_entry(struct ata_ering_entry *ent)
1130{
1131 if (ent->err_mask & (AC_ERR_ATA_BUS | AC_ERR_TIMEOUT))
1132 return 1;
1133
1134 if (ent->is_io) {
1135 if (ent->err_mask & AC_ERR_HSM)
1136 return 1;
1137 if ((ent->err_mask &
1138 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1139 return 2;
1140 }
1141
1142 return 0;
1143}
1144
1145struct speed_down_needed_arg {
1146 u64 since;
1147 int nr_errors[3];
1148};
1149
1150static int speed_down_needed_cb(struct ata_ering_entry *ent, void *void_arg)
1151{
1152 struct speed_down_needed_arg *arg = void_arg;
1153
1154 if (ent->timestamp < arg->since)
1155 return -1;
1156
1157 arg->nr_errors[ata_eh_categorize_ering_entry(ent)]++;
1158 return 0;
1159}
1160
1161/**
1162 * ata_eh_speed_down_needed - Determine wheter speed down is necessary
1163 * @dev: Device of interest
1164 *
1165 * This function examines error ring of @dev and determines
1166 * whether speed down is necessary. Speed down is necessary if
1167 * there have been more than 3 of Cat-1 errors or 10 of Cat-2
1168 * errors during last 15 minutes.
1169 *
1170 * Cat-1 errors are ATA_BUS, TIMEOUT for any command and HSM
1171 * violation for known supported commands.
1172 *
1173 * Cat-2 errors are unclassified DEV error for known supported
1174 * command.
1175 *
1176 * LOCKING:
1177 * Inherited from caller.
1178 *
1179 * RETURNS:
1180 * 1 if speed down is necessary, 0 otherwise
1181 */
1182static int ata_eh_speed_down_needed(struct ata_device *dev)
1183{
1184 const u64 interval = 15LLU * 60 * HZ;
1185 static const int err_limits[3] = { -1, 3, 10 };
1186 struct speed_down_needed_arg arg;
1187 struct ata_ering_entry *ent;
1188 int err_cat;
1189 u64 j64;
1190
1191 ent = ata_ering_top(&dev->ering);
1192 if (!ent)
1193 return 0;
1194
1195 err_cat = ata_eh_categorize_ering_entry(ent);
1196 if (err_cat == 0)
1197 return 0;
1198
1199 memset(&arg, 0, sizeof(arg));
1200
1201 j64 = get_jiffies_64();
1202 if (j64 >= interval)
1203 arg.since = j64 - interval;
1204 else
1205 arg.since = 0;
1206
1207 ata_ering_map(&dev->ering, speed_down_needed_cb, &arg);
1208
1209 return arg.nr_errors[err_cat] > err_limits[err_cat];
1210}
1211
1212/**
1213 * ata_eh_speed_down - record error and speed down if necessary
1214 * @dev: Failed device
1215 * @is_io: Did the device fail during normal IO?
1216 * @err_mask: err_mask of the error
1217 *
1218 * Record error and examine error history to determine whether
1219 * adjusting transmission speed is necessary. It also sets
1220 * transmission limits appropriately if such adjustment is
1221 * necessary.
1222 *
1223 * LOCKING:
1224 * Kernel thread context (may sleep).
1225 *
1226 * RETURNS:
1227 * 0 on success, -errno otherwise
1228 */
1229static int ata_eh_speed_down(struct ata_device *dev, int is_io,
1230 unsigned int err_mask)
1231{
1232 if (!err_mask)
1233 return 0;
1234
1235 /* record error and determine whether speed down is necessary */
1236 ata_ering_record(&dev->ering, is_io, err_mask);
1237
1238 if (!ata_eh_speed_down_needed(dev))
1239 return 0;
1240
1241 /* speed down SATA link speed if possible */
1242 if (sata_down_spd_limit(dev->ap) == 0)
1243 return ATA_EH_HARDRESET;
1244
1245 /* lower transfer mode */
1246 if (ata_down_xfermask_limit(dev, 0) == 0)
1247 return ATA_EH_SOFTRESET;
1248
1249 ata_dev_printk(dev, KERN_ERR,
1250 "speed down requested but no transfer mode left\n");
1251 return 0;
1252}
1253
1254/**
1255 * ata_eh_autopsy - analyze error and determine recovery action
1256 * @ap: ATA port to perform autopsy on
1257 *
1258 * Analyze why @ap failed and determine which recovery action is
1259 * needed. This function also sets more detailed AC_ERR_* values
1260 * and fills sense data for ATAPI CHECK SENSE.
1261 *
1262 * LOCKING:
1263 * Kernel thread context (may sleep).
1264 */
1265static void ata_eh_autopsy(struct ata_port *ap)
1266{
1267 struct ata_eh_context *ehc = &ap->eh_context;
1268 unsigned int action = ehc->i.action;
1269 struct ata_device *failed_dev = NULL;
1270 unsigned int all_err_mask = 0;
1271 int tag, is_io = 0;
1272 u32 serror;
1273 int rc;
1274
1275 DPRINTK("ENTER\n");
1276
1277 /* obtain and analyze SError */
1278 rc = sata_scr_read(ap, SCR_ERROR, &serror);
1279 if (rc == 0) {
1280 ehc->i.serror |= serror;
1281 ata_eh_analyze_serror(ap);
1282 } else if (rc != -EOPNOTSUPP)
1283 action |= ATA_EH_HARDRESET;
1284
Tejun Heoe8ee8452006-05-15 21:03:46 +09001285 /* analyze NCQ failure */
1286 ata_eh_analyze_ncq_error(ap);
1287
Tejun Heo022bdb02006-05-15 20:58:22 +09001288 /* any real error trumps AC_ERR_OTHER */
1289 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1290 ehc->i.err_mask &= ~AC_ERR_OTHER;
1291
1292 all_err_mask |= ehc->i.err_mask;
1293
1294 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1295 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1296
1297 if (!(qc->flags & ATA_QCFLAG_FAILED))
1298 continue;
1299
1300 /* inherit upper level err_mask */
1301 qc->err_mask |= ehc->i.err_mask;
1302
Tejun Heo022bdb02006-05-15 20:58:22 +09001303 /* analyze TF */
1304 action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1305
1306 /* DEV errors are probably spurious in case of ATA_BUS error */
1307 if (qc->err_mask & AC_ERR_ATA_BUS)
1308 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1309 AC_ERR_INVALID);
1310
1311 /* any real error trumps unknown error */
1312 if (qc->err_mask & ~AC_ERR_OTHER)
1313 qc->err_mask &= ~AC_ERR_OTHER;
1314
1315 /* SENSE_VALID trumps dev/unknown error and revalidation */
1316 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1317 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1318 action &= ~ATA_EH_REVALIDATE;
1319 }
1320
1321 /* accumulate error info */
1322 failed_dev = qc->dev;
1323 all_err_mask |= qc->err_mask;
1324 if (qc->flags & ATA_QCFLAG_IO)
1325 is_io = 1;
1326 }
1327
Tejun Heoa20f33f2006-05-16 12:58:24 +09001328 /* enforce default EH actions */
Tejun Heob51e9e52006-06-29 01:29:30 +09001329 if (ap->pflags & ATA_PFLAG_FROZEN ||
Tejun Heoa20f33f2006-05-16 12:58:24 +09001330 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
1331 action |= ATA_EH_SOFTRESET;
1332 else if (all_err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001333 action |= ATA_EH_REVALIDATE;
1334
Tejun Heo47005f22006-06-19 18:27:23 +09001335 /* if we have offending qcs and the associated failed device */
1336 if (failed_dev) {
1337 /* speed down */
1338 action |= ata_eh_speed_down(failed_dev, is_io, all_err_mask);
1339
1340 /* perform per-dev EH action only on the offending device */
1341 ehc->i.dev_action[failed_dev->devno] |=
1342 action & ATA_EH_PERDEV_MASK;
1343 action &= ~ATA_EH_PERDEV_MASK;
1344 }
1345
Tejun Heoa20f33f2006-05-16 12:58:24 +09001346 /* record autopsy result */
Tejun Heo022bdb02006-05-15 20:58:22 +09001347 ehc->i.dev = failed_dev;
Tejun Heo0662c582006-07-03 02:54:58 +09001348 ehc->i.action |= action;
Tejun Heo022bdb02006-05-15 20:58:22 +09001349
1350 DPRINTK("EXIT\n");
1351}
1352
1353/**
1354 * ata_eh_report - report error handling to user
1355 * @ap: ATA port EH is going on
1356 *
1357 * Report EH to user.
1358 *
1359 * LOCKING:
1360 * None.
1361 */
1362static void ata_eh_report(struct ata_port *ap)
1363{
1364 struct ata_eh_context *ehc = &ap->eh_context;
1365 const char *frozen, *desc;
1366 int tag, nr_failed = 0;
1367
1368 desc = NULL;
1369 if (ehc->i.desc[0] != '\0')
1370 desc = ehc->i.desc;
1371
1372 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1373 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1374
1375 if (!(qc->flags & ATA_QCFLAG_FAILED))
1376 continue;
1377 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1378 continue;
1379
1380 nr_failed++;
1381 }
1382
1383 if (!nr_failed && !ehc->i.err_mask)
1384 return;
1385
1386 frozen = "";
Tejun Heob51e9e52006-06-29 01:29:30 +09001387 if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heo022bdb02006-05-15 20:58:22 +09001388 frozen = " frozen";
1389
1390 if (ehc->i.dev) {
Tejun Heoe8ee8452006-05-15 21:03:46 +09001391 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1392 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1393 ehc->i.err_mask, ap->sactive, ehc->i.serror,
1394 ehc->i.action, frozen);
Tejun Heo022bdb02006-05-15 20:58:22 +09001395 if (desc)
1396 ata_dev_printk(ehc->i.dev, KERN_ERR, "(%s)\n", desc);
1397 } else {
Tejun Heoe8ee8452006-05-15 21:03:46 +09001398 ata_port_printk(ap, KERN_ERR, "exception Emask 0x%x "
1399 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1400 ehc->i.err_mask, ap->sactive, ehc->i.serror,
1401 ehc->i.action, frozen);
Tejun Heo022bdb02006-05-15 20:58:22 +09001402 if (desc)
1403 ata_port_printk(ap, KERN_ERR, "(%s)\n", desc);
1404 }
1405
1406 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1407 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1408
1409 if (!(qc->flags & ATA_QCFLAG_FAILED) || !qc->err_mask)
1410 continue;
1411
1412 ata_dev_printk(qc->dev, KERN_ERR, "tag %d cmd 0x%x "
1413 "Emask 0x%x stat 0x%x err 0x%x (%s)\n",
1414 qc->tag, qc->tf.command, qc->err_mask,
1415 qc->result_tf.command, qc->result_tf.feature,
1416 ata_err_string(qc->err_mask));
1417 }
1418}
1419
Tejun Heod87fa382006-05-31 18:28:24 +09001420static int ata_do_reset(struct ata_port *ap, ata_reset_fn_t reset,
1421 unsigned int *classes)
1422{
1423 int i, rc;
1424
1425 for (i = 0; i < ATA_MAX_DEVICES; i++)
1426 classes[i] = ATA_DEV_UNKNOWN;
1427
1428 rc = reset(ap, classes);
1429 if (rc)
1430 return rc;
1431
1432 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
1433 * is complete and convert all ATA_DEV_UNKNOWN to
1434 * ATA_DEV_NONE.
1435 */
1436 for (i = 0; i < ATA_MAX_DEVICES; i++)
1437 if (classes[i] != ATA_DEV_UNKNOWN)
1438 break;
1439
1440 if (i < ATA_MAX_DEVICES)
1441 for (i = 0; i < ATA_MAX_DEVICES; i++)
1442 if (classes[i] == ATA_DEV_UNKNOWN)
1443 classes[i] = ATA_DEV_NONE;
1444
1445 return 0;
1446}
1447
Tejun Heo664faf02006-05-31 18:27:50 +09001448static int ata_eh_followup_srst_needed(int rc, int classify,
1449 const unsigned int *classes)
1450{
1451 if (rc == -EAGAIN)
1452 return 1;
1453 if (rc != 0)
1454 return 0;
1455 if (classify && classes[0] == ATA_DEV_UNKNOWN)
1456 return 1;
1457 return 0;
1458}
1459
1460static int ata_eh_reset(struct ata_port *ap, int classify,
Tejun Heof5914a42006-05-31 18:27:48 +09001461 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
Tejun Heo022bdb02006-05-15 20:58:22 +09001462 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
1463{
1464 struct ata_eh_context *ehc = &ap->eh_context;
Tejun Heo664faf02006-05-31 18:27:50 +09001465 unsigned int *classes = ehc->classes;
Tejun Heo022bdb02006-05-15 20:58:22 +09001466 int tries = ATA_EH_RESET_TRIES;
Tejun Heob51e9e52006-06-29 01:29:30 +09001467 int verbose = !(ap->pflags & ATA_PFLAG_LOADING);
Tejun Heof5914a42006-05-31 18:27:48 +09001468 unsigned int action;
Tejun Heo022bdb02006-05-15 20:58:22 +09001469 ata_reset_fn_t reset;
Tejun Heo664faf02006-05-31 18:27:50 +09001470 int i, did_followup_srst, rc;
Tejun Heo022bdb02006-05-15 20:58:22 +09001471
Tejun Heof5914a42006-05-31 18:27:48 +09001472 /* Determine which reset to use and record in ehc->i.action.
1473 * prereset() may examine and modify it.
1474 */
1475 action = ehc->i.action;
1476 ehc->i.action &= ~ATA_EH_RESET_MASK;
Tejun Heo022bdb02006-05-15 20:58:22 +09001477 if (softreset && (!hardreset || (!sata_set_spd_needed(ap) &&
Tejun Heof5914a42006-05-31 18:27:48 +09001478 !(action & ATA_EH_HARDRESET))))
1479 ehc->i.action |= ATA_EH_SOFTRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001480 else
Tejun Heof5914a42006-05-31 18:27:48 +09001481 ehc->i.action |= ATA_EH_HARDRESET;
1482
1483 if (prereset) {
1484 rc = prereset(ap);
1485 if (rc) {
1486 ata_port_printk(ap, KERN_ERR,
1487 "prereset failed (errno=%d)\n", rc);
1488 return rc;
1489 }
1490 }
1491
1492 /* prereset() might have modified ehc->i.action */
1493 if (ehc->i.action & ATA_EH_HARDRESET)
Tejun Heo022bdb02006-05-15 20:58:22 +09001494 reset = hardreset;
Tejun Heof5914a42006-05-31 18:27:48 +09001495 else if (ehc->i.action & ATA_EH_SOFTRESET)
1496 reset = softreset;
1497 else {
1498 /* prereset told us not to reset, bang classes and return */
1499 for (i = 0; i < ATA_MAX_DEVICES; i++)
1500 classes[i] = ATA_DEV_NONE;
1501 return 0;
1502 }
1503
1504 /* did prereset() screw up? if so, fix up to avoid oopsing */
1505 if (!reset) {
1506 ata_port_printk(ap, KERN_ERR, "BUG: prereset() requested "
1507 "invalid reset type\n");
1508 if (softreset)
1509 reset = softreset;
1510 else
1511 reset = hardreset;
1512 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001513
1514 retry:
Tejun Heo3e706392006-05-31 18:28:11 +09001515 /* shut up during boot probing */
1516 if (verbose)
1517 ata_port_printk(ap, KERN_INFO, "%s resetting port\n",
1518 reset == softreset ? "soft" : "hard");
Tejun Heo022bdb02006-05-15 20:58:22 +09001519
1520 /* reset */
Tejun Heo47005f22006-06-19 18:27:23 +09001521 ata_eh_about_to_do(ap, NULL, ATA_EH_RESET_MASK);
Tejun Heo022bdb02006-05-15 20:58:22 +09001522 ehc->i.flags |= ATA_EHI_DID_RESET;
1523
1524 rc = ata_do_reset(ap, reset, classes);
1525
Tejun Heo664faf02006-05-31 18:27:50 +09001526 did_followup_srst = 0;
1527 if (reset == hardreset &&
1528 ata_eh_followup_srst_needed(rc, classify, classes)) {
1529 /* okay, let's do follow-up softreset */
1530 did_followup_srst = 1;
1531 reset = softreset;
1532
1533 if (!reset) {
1534 ata_port_printk(ap, KERN_ERR,
1535 "follow-up softreset required "
1536 "but no softreset avaliable\n");
1537 return -EINVAL;
1538 }
1539
Tejun Heo47005f22006-06-19 18:27:23 +09001540 ata_eh_about_to_do(ap, NULL, ATA_EH_RESET_MASK);
Tejun Heo664faf02006-05-31 18:27:50 +09001541 rc = ata_do_reset(ap, reset, classes);
1542
1543 if (rc == 0 && classify &&
1544 classes[0] == ATA_DEV_UNKNOWN) {
1545 ata_port_printk(ap, KERN_ERR,
1546 "classification failed\n");
1547 return -EINVAL;
1548 }
1549 }
1550
Tejun Heo022bdb02006-05-15 20:58:22 +09001551 if (rc && --tries) {
Tejun Heo664faf02006-05-31 18:27:50 +09001552 const char *type;
1553
1554 if (reset == softreset) {
1555 if (did_followup_srst)
1556 type = "follow-up soft";
1557 else
1558 type = "soft";
1559 } else
1560 type = "hard";
1561
Tejun Heo022bdb02006-05-15 20:58:22 +09001562 ata_port_printk(ap, KERN_WARNING,
Tejun Heo664faf02006-05-31 18:27:50 +09001563 "%sreset failed, retrying in 5 secs\n", type);
Tejun Heo022bdb02006-05-15 20:58:22 +09001564 ssleep(5);
1565
1566 if (reset == hardreset)
1567 sata_down_spd_limit(ap);
1568 if (hardreset)
1569 reset = hardreset;
1570 goto retry;
1571 }
1572
1573 if (rc == 0) {
Tejun Heo20952b62006-05-31 18:27:23 +09001574 /* After the reset, the device state is PIO 0 and the
1575 * controller state is undefined. Record the mode.
1576 */
1577 for (i = 0; i < ATA_MAX_DEVICES; i++)
1578 ap->device[i].pio_mode = XFER_PIO_0;
1579
Tejun Heo022bdb02006-05-15 20:58:22 +09001580 if (postreset)
1581 postreset(ap, classes);
1582
1583 /* reset successful, schedule revalidation */
Tejun Heo47005f22006-06-19 18:27:23 +09001584 ata_eh_done(ap, NULL, ATA_EH_RESET_MASK);
Tejun Heo022bdb02006-05-15 20:58:22 +09001585 ehc->i.action |= ATA_EH_REVALIDATE;
1586 }
1587
1588 return rc;
1589}
1590
Tejun Heo084fe632006-05-31 18:28:03 +09001591static int ata_eh_revalidate_and_attach(struct ata_port *ap,
1592 struct ata_device **r_failed_dev)
Tejun Heo022bdb02006-05-15 20:58:22 +09001593{
1594 struct ata_eh_context *ehc = &ap->eh_context;
1595 struct ata_device *dev;
Tejun Heo084fe632006-05-31 18:28:03 +09001596 unsigned long flags;
Tejun Heo022bdb02006-05-15 20:58:22 +09001597 int i, rc = 0;
1598
1599 DPRINTK("ENTER\n");
1600
1601 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heo47005f22006-06-19 18:27:23 +09001602 unsigned int action;
Tejun Heo022bdb02006-05-15 20:58:22 +09001603
Tejun Heo47005f22006-06-19 18:27:23 +09001604 dev = &ap->device[i];
Tejun Heo64f65ca2006-06-24 20:30:18 +09001605 action = ata_eh_dev_action(dev);
Tejun Heo47005f22006-06-19 18:27:23 +09001606
1607 if (action & ATA_EH_REVALIDATE && ata_dev_enabled(dev)) {
Tejun Heo022bdb02006-05-15 20:58:22 +09001608 if (ata_port_offline(ap)) {
1609 rc = -EIO;
1610 break;
1611 }
1612
Tejun Heo47005f22006-06-19 18:27:23 +09001613 ata_eh_about_to_do(ap, dev, ATA_EH_REVALIDATE);
Tejun Heo022bdb02006-05-15 20:58:22 +09001614 rc = ata_dev_revalidate(dev,
1615 ehc->i.flags & ATA_EHI_DID_RESET);
1616 if (rc)
1617 break;
1618
Tejun Heo47005f22006-06-19 18:27:23 +09001619 ata_eh_done(ap, dev, ATA_EH_REVALIDATE);
1620
zhao, forrest3057ac32006-06-12 12:01:34 +08001621 /* schedule the scsi_rescan_device() here */
1622 queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
Tejun Heo084fe632006-05-31 18:28:03 +09001623 } else if (dev->class == ATA_DEV_UNKNOWN &&
1624 ehc->tries[dev->devno] &&
1625 ata_class_enabled(ehc->classes[dev->devno])) {
1626 dev->class = ehc->classes[dev->devno];
1627
1628 rc = ata_dev_read_id(dev, &dev->class, 1, dev->id);
1629 if (rc == 0)
1630 rc = ata_dev_configure(dev, 1);
1631
1632 if (rc) {
1633 dev->class = ATA_DEV_UNKNOWN;
1634 break;
1635 }
1636
Jeff Garzikba6a1302006-06-22 23:46:10 -04001637 spin_lock_irqsave(ap->lock, flags);
Tejun Heob51e9e52006-06-29 01:29:30 +09001638 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
Jeff Garzikba6a1302006-06-22 23:46:10 -04001639 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo022bdb02006-05-15 20:58:22 +09001640 }
1641 }
1642
Tejun Heo47005f22006-06-19 18:27:23 +09001643 if (rc)
Tejun Heo022bdb02006-05-15 20:58:22 +09001644 *r_failed_dev = dev;
1645
1646 DPRINTK("EXIT\n");
1647 return rc;
1648}
1649
1650static int ata_port_nr_enabled(struct ata_port *ap)
1651{
1652 int i, cnt = 0;
1653
1654 for (i = 0; i < ATA_MAX_DEVICES; i++)
1655 if (ata_dev_enabled(&ap->device[i]))
1656 cnt++;
1657 return cnt;
1658}
1659
Tejun Heo084fe632006-05-31 18:28:03 +09001660static int ata_port_nr_vacant(struct ata_port *ap)
1661{
1662 int i, cnt = 0;
1663
1664 for (i = 0; i < ATA_MAX_DEVICES; i++)
1665 if (ap->device[i].class == ATA_DEV_UNKNOWN)
1666 cnt++;
1667 return cnt;
1668}
1669
1670static int ata_eh_skip_recovery(struct ata_port *ap)
1671{
1672 struct ata_eh_context *ehc = &ap->eh_context;
1673 int i;
1674
Tejun Heob51e9e52006-06-29 01:29:30 +09001675 if (ap->pflags & ATA_PFLAG_FROZEN || ata_port_nr_enabled(ap))
Tejun Heo084fe632006-05-31 18:28:03 +09001676 return 0;
1677
1678 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
1679 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1680 struct ata_device *dev = &ap->device[i];
1681
1682 if (dev->class == ATA_DEV_UNKNOWN &&
1683 ehc->classes[dev->devno] != ATA_DEV_NONE)
1684 return 0;
1685 }
1686
1687 return 1;
1688}
1689
Tejun Heo022bdb02006-05-15 20:58:22 +09001690/**
1691 * ata_eh_recover - recover host port after error
1692 * @ap: host port to recover
Tejun Heof5914a42006-05-31 18:27:48 +09001693 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09001694 * @softreset: softreset method (can be NULL)
1695 * @hardreset: hardreset method (can be NULL)
1696 * @postreset: postreset method (can be NULL)
1697 *
1698 * This is the alpha and omega, eum and yang, heart and soul of
1699 * libata exception handling. On entry, actions required to
Tejun Heo084fe632006-05-31 18:28:03 +09001700 * recover the port and hotplug requests are recorded in
1701 * eh_context. This function executes all the operations with
1702 * appropriate retrials and fallbacks to resurrect failed
1703 * devices, detach goners and greet newcomers.
Tejun Heo022bdb02006-05-15 20:58:22 +09001704 *
1705 * LOCKING:
1706 * Kernel thread context (may sleep).
1707 *
1708 * RETURNS:
1709 * 0 on success, -errno on failure.
1710 */
Tejun Heof5914a42006-05-31 18:27:48 +09001711static int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
1712 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
Tejun Heo022bdb02006-05-15 20:58:22 +09001713 ata_postreset_fn_t postreset)
1714{
1715 struct ata_eh_context *ehc = &ap->eh_context;
1716 struct ata_device *dev;
1717 int down_xfermask, i, rc;
1718
1719 DPRINTK("ENTER\n");
1720
1721 /* prep for recovery */
1722 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1723 dev = &ap->device[i];
1724
1725 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
Tejun Heo084fe632006-05-31 18:28:03 +09001726
1727 /* process hotplug request */
1728 if (dev->flags & ATA_DFLAG_DETACH)
1729 ata_eh_detach_dev(dev);
1730
1731 if (!ata_dev_enabled(dev) &&
1732 ((ehc->i.probe_mask & (1 << dev->devno)) &&
1733 !(ehc->did_probe_mask & (1 << dev->devno)))) {
1734 ata_eh_detach_dev(dev);
1735 ata_dev_init(dev);
1736 ehc->did_probe_mask |= (1 << dev->devno);
1737 ehc->i.action |= ATA_EH_SOFTRESET;
1738 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001739 }
1740
1741 retry:
1742 down_xfermask = 0;
1743 rc = 0;
1744
Tejun Heoaeb2ecd2006-06-12 14:11:43 +09001745 /* if UNLOADING, finish immediately */
Tejun Heob51e9e52006-06-29 01:29:30 +09001746 if (ap->pflags & ATA_PFLAG_UNLOADING)
Tejun Heoaeb2ecd2006-06-12 14:11:43 +09001747 goto out;
1748
Tejun Heo022bdb02006-05-15 20:58:22 +09001749 /* skip EH if possible. */
Tejun Heo084fe632006-05-31 18:28:03 +09001750 if (ata_eh_skip_recovery(ap))
Tejun Heo022bdb02006-05-15 20:58:22 +09001751 ehc->i.action = 0;
1752
Tejun Heo084fe632006-05-31 18:28:03 +09001753 for (i = 0; i < ATA_MAX_DEVICES; i++)
1754 ehc->classes[i] = ATA_DEV_UNKNOWN;
1755
Tejun Heo022bdb02006-05-15 20:58:22 +09001756 /* reset */
1757 if (ehc->i.action & ATA_EH_RESET_MASK) {
1758 ata_eh_freeze_port(ap);
1759
Tejun Heo084fe632006-05-31 18:28:03 +09001760 rc = ata_eh_reset(ap, ata_port_nr_vacant(ap), prereset,
1761 softreset, hardreset, postreset);
Tejun Heo022bdb02006-05-15 20:58:22 +09001762 if (rc) {
1763 ata_port_printk(ap, KERN_ERR,
1764 "reset failed, giving up\n");
1765 goto out;
1766 }
1767
1768 ata_eh_thaw_port(ap);
1769 }
1770
Tejun Heo084fe632006-05-31 18:28:03 +09001771 /* revalidate existing devices and attach new ones */
1772 rc = ata_eh_revalidate_and_attach(ap, &dev);
Tejun Heo022bdb02006-05-15 20:58:22 +09001773 if (rc)
1774 goto dev_fail;
1775
1776 /* configure transfer mode if the port has been reset */
1777 if (ehc->i.flags & ATA_EHI_DID_RESET) {
1778 rc = ata_set_mode(ap, &dev);
1779 if (rc) {
1780 down_xfermask = 1;
1781 goto dev_fail;
1782 }
1783 }
1784
1785 goto out;
1786
1787 dev_fail:
1788 switch (rc) {
1789 case -ENODEV:
Tejun Heo084fe632006-05-31 18:28:03 +09001790 /* device missing, schedule probing */
1791 ehc->i.probe_mask |= (1 << dev->devno);
Tejun Heo022bdb02006-05-15 20:58:22 +09001792 case -EINVAL:
1793 ehc->tries[dev->devno] = 0;
1794 break;
1795 case -EIO:
1796 sata_down_spd_limit(ap);
1797 default:
1798 ehc->tries[dev->devno]--;
1799 if (down_xfermask &&
1800 ata_down_xfermask_limit(dev, ehc->tries[dev->devno] == 1))
1801 ehc->tries[dev->devno] = 0;
1802 }
1803
Tejun Heo084fe632006-05-31 18:28:03 +09001804 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
1805 /* disable device if it has used up all its chances */
Tejun Heo022bdb02006-05-15 20:58:22 +09001806 ata_dev_disable(dev);
1807
Tejun Heo084fe632006-05-31 18:28:03 +09001808 /* detach if offline */
1809 if (ata_port_offline(ap))
1810 ata_eh_detach_dev(dev);
1811
1812 /* probe if requested */
1813 if ((ehc->i.probe_mask & (1 << dev->devno)) &&
1814 !(ehc->did_probe_mask & (1 << dev->devno))) {
1815 ata_eh_detach_dev(dev);
1816 ata_dev_init(dev);
1817
1818 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
1819 ehc->did_probe_mask |= (1 << dev->devno);
1820 ehc->i.action |= ATA_EH_SOFTRESET;
1821 }
1822 } else {
1823 /* soft didn't work? be haaaaard */
1824 if (ehc->i.flags & ATA_EHI_DID_RESET)
1825 ehc->i.action |= ATA_EH_HARDRESET;
1826 else
1827 ehc->i.action |= ATA_EH_SOFTRESET;
1828 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001829
1830 if (ata_port_nr_enabled(ap)) {
1831 ata_port_printk(ap, KERN_WARNING, "failed to recover some "
1832 "devices, retrying in 5 secs\n");
1833 ssleep(5);
1834 } else {
1835 /* no device left, repeat fast */
1836 msleep(500);
1837 }
1838
1839 goto retry;
1840
1841 out:
1842 if (rc) {
1843 for (i = 0; i < ATA_MAX_DEVICES; i++)
1844 ata_dev_disable(&ap->device[i]);
1845 }
1846
1847 DPRINTK("EXIT, rc=%d\n", rc);
1848 return rc;
1849}
1850
1851/**
1852 * ata_eh_finish - finish up EH
1853 * @ap: host port to finish EH for
1854 *
1855 * Recovery is complete. Clean up EH states and retry or finish
1856 * failed qcs.
1857 *
1858 * LOCKING:
1859 * None.
1860 */
1861static void ata_eh_finish(struct ata_port *ap)
1862{
1863 int tag;
1864
1865 /* retry or finish qcs */
1866 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1867 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1868
1869 if (!(qc->flags & ATA_QCFLAG_FAILED))
1870 continue;
1871
1872 if (qc->err_mask) {
1873 /* FIXME: Once EH migration is complete,
1874 * generate sense data in this function,
1875 * considering both err_mask and tf.
1876 */
1877 if (qc->err_mask & AC_ERR_INVALID)
1878 ata_eh_qc_complete(qc);
1879 else
1880 ata_eh_qc_retry(qc);
1881 } else {
1882 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1883 ata_eh_qc_complete(qc);
1884 } else {
1885 /* feed zero TF to sense generation */
1886 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
1887 ata_eh_qc_retry(qc);
1888 }
1889 }
1890 }
1891}
1892
1893/**
1894 * ata_do_eh - do standard error handling
1895 * @ap: host port to handle error for
Tejun Heof5914a42006-05-31 18:27:48 +09001896 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09001897 * @softreset: softreset method (can be NULL)
1898 * @hardreset: hardreset method (can be NULL)
1899 * @postreset: postreset method (can be NULL)
1900 *
1901 * Perform standard error handling sequence.
1902 *
1903 * LOCKING:
1904 * Kernel thread context (may sleep).
1905 */
Tejun Heof5914a42006-05-31 18:27:48 +09001906void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
1907 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
1908 ata_postreset_fn_t postreset)
Tejun Heo022bdb02006-05-15 20:58:22 +09001909{
Tejun Heob51e9e52006-06-29 01:29:30 +09001910 if (!(ap->pflags & ATA_PFLAG_LOADING)) {
Tejun Heo3e706392006-05-31 18:28:11 +09001911 ata_eh_autopsy(ap);
1912 ata_eh_report(ap);
1913 }
1914
Tejun Heof5914a42006-05-31 18:27:48 +09001915 ata_eh_recover(ap, prereset, softreset, hardreset, postreset);
Tejun Heo022bdb02006-05-15 20:58:22 +09001916 ata_eh_finish(ap);
1917}