blob: 126be36289a77816edac83f5551d961cd23a5601 [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 Heoaf181c22006-06-24 20:30:18 +090096static void ata_eh_clear_action(struct ata_device *dev,
97 struct ata_eh_info *ehi, unsigned int action)
98{
99 int i;
100
101 if (!dev) {
102 ehi->action &= ~action;
103 for (i = 0; i < ATA_MAX_DEVICES; i++)
104 ehi->dev_action[i] &= ~action;
105 } else {
106 /* doesn't make sense for port-wide EH actions */
107 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
108
109 /* break ehi->action into ehi->dev_action */
110 if (ehi->action & action) {
111 for (i = 0; i < ATA_MAX_DEVICES; i++)
112 ehi->dev_action[i] |= ehi->action & action;
113 ehi->action &= ~action;
114 }
115
116 /* turn off the specified per-dev action */
117 ehi->dev_action[dev->devno] &= ~action;
118 }
119}
120
Tejun Heoece1d632006-04-02 18:51:53 +0900121/**
122 * ata_scsi_timed_out - SCSI layer time out callback
123 * @cmd: timed out SCSI command
124 *
125 * Handles SCSI layer timeout. We race with normal completion of
126 * the qc for @cmd. If the qc is already gone, we lose and let
127 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
128 * timed out and EH should be invoked. Prevent ata_qc_complete()
129 * from finishing it by setting EH_SCHEDULED and return
130 * EH_NOT_HANDLED.
131 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900132 * TODO: kill this function once old EH is gone.
133 *
Tejun Heoece1d632006-04-02 18:51:53 +0900134 * LOCKING:
135 * Called from timer context
136 *
137 * RETURNS:
138 * EH_HANDLED or EH_NOT_HANDLED
139 */
140enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
141{
142 struct Scsi_Host *host = cmd->device->host;
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400143 struct ata_port *ap = ata_shost_to_port(host);
Tejun Heoece1d632006-04-02 18:51:53 +0900144 unsigned long flags;
145 struct ata_queued_cmd *qc;
Tejun Heoad9e2762006-05-15 20:58:12 +0900146 enum scsi_eh_timer_return ret;
Tejun Heoece1d632006-04-02 18:51:53 +0900147
148 DPRINTK("ENTER\n");
149
Tejun Heoad9e2762006-05-15 20:58:12 +0900150 if (ap->ops->error_handler) {
151 ret = EH_NOT_HANDLED;
152 goto out;
153 }
154
155 ret = EH_HANDLED;
Jeff Garzikba6a1302006-06-22 23:46:10 -0400156 spin_lock_irqsave(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900157 qc = ata_qc_from_tag(ap, ap->active_tag);
158 if (qc) {
159 WARN_ON(qc->scsicmd != cmd);
160 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
161 qc->err_mask |= AC_ERR_TIMEOUT;
162 ret = EH_NOT_HANDLED;
163 }
Jeff Garzikba6a1302006-06-22 23:46:10 -0400164 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900165
Tejun Heoad9e2762006-05-15 20:58:12 +0900166 out:
Tejun Heoece1d632006-04-02 18:51:53 +0900167 DPRINTK("EXIT, ret=%d\n", ret);
168 return ret;
169}
170
171/**
172 * ata_scsi_error - SCSI layer error handler callback
173 * @host: SCSI host on which error occurred
174 *
175 * Handles SCSI-layer-thrown error events.
176 *
177 * LOCKING:
178 * Inherited from SCSI layer (none, can sleep)
179 *
180 * RETURNS:
181 * Zero.
182 */
Jeff Garzik381544b2006-04-11 13:04:39 -0400183void ata_scsi_error(struct Scsi_Host *host)
Tejun Heoece1d632006-04-02 18:51:53 +0900184{
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400185 struct ata_port *ap = ata_shost_to_port(host);
Jeff Garzikba6a1302006-06-22 23:46:10 -0400186 spinlock_t *ap_lock = ap->lock;
Tejun Heoad9e2762006-05-15 20:58:12 +0900187 int i, repeat_cnt = ATA_EH_MAX_REPEAT;
188 unsigned long flags;
Tejun Heoece1d632006-04-02 18:51:53 +0900189
190 DPRINTK("ENTER\n");
191
Tejun Heoad9e2762006-05-15 20:58:12 +0900192 /* synchronize with port task */
Tejun Heoece1d632006-04-02 18:51:53 +0900193 ata_port_flush_task(ap);
194
Tejun Heoad9e2762006-05-15 20:58:12 +0900195 /* synchronize with host_set lock and sort out timeouts */
Tejun Heoece1d632006-04-02 18:51:53 +0900196
Tejun Heoad9e2762006-05-15 20:58:12 +0900197 /* For new EH, all qcs are finished in one of three ways -
198 * normal completion, error completion, and SCSI timeout.
199 * Both cmpletions can race against SCSI timeout. When normal
200 * completion wins, the qc never reaches EH. When error
201 * completion wins, the qc has ATA_QCFLAG_FAILED set.
202 *
203 * When SCSI timeout wins, things are a bit more complex.
204 * Normal or error completion can occur after the timeout but
205 * before this point. In such cases, both types of
206 * completions are honored. A scmd is determined to have
207 * timed out iff its associated qc is active and not failed.
208 */
209 if (ap->ops->error_handler) {
210 struct scsi_cmnd *scmd, *tmp;
211 int nr_timedout = 0;
Tejun Heoece1d632006-04-02 18:51:53 +0900212
Jeff Garzikba6a1302006-06-22 23:46:10 -0400213 spin_lock_irqsave(ap_lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900214
215 list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
216 struct ata_queued_cmd *qc;
217
218 for (i = 0; i < ATA_MAX_QUEUE; i++) {
219 qc = __ata_qc_from_tag(ap, i);
220 if (qc->flags & ATA_QCFLAG_ACTIVE &&
221 qc->scsicmd == scmd)
222 break;
223 }
224
225 if (i < ATA_MAX_QUEUE) {
226 /* the scmd has an associated qc */
227 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
228 /* which hasn't failed yet, timeout */
229 qc->err_mask |= AC_ERR_TIMEOUT;
230 qc->flags |= ATA_QCFLAG_FAILED;
231 nr_timedout++;
232 }
233 } else {
234 /* Normal completion occurred after
235 * SCSI timeout but before this point.
236 * Successfully complete it.
237 */
238 scmd->retries = scmd->allowed;
239 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
240 }
241 }
242
243 /* If we have timed out qcs. They belong to EH from
244 * this point but the state of the controller is
245 * unknown. Freeze the port to make sure the IRQ
246 * handler doesn't diddle with those qcs. This must
247 * be done atomically w.r.t. setting QCFLAG_FAILED.
248 */
249 if (nr_timedout)
250 __ata_port_freeze(ap);
251
Jeff Garzikba6a1302006-06-22 23:46:10 -0400252 spin_unlock_irqrestore(ap_lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900253 } else
Jeff Garzikba6a1302006-06-22 23:46:10 -0400254 spin_unlock_wait(ap_lock);
Tejun Heoad9e2762006-05-15 20:58:12 +0900255
256 repeat:
257 /* invoke error handler */
258 if (ap->ops->error_handler) {
Tejun Heof3e81b12006-05-15 20:58:21 +0900259 /* fetch & clear EH info */
Jeff Garzikba6a1302006-06-22 23:46:10 -0400260 spin_lock_irqsave(ap_lock, flags);
Tejun Heof3e81b12006-05-15 20:58:21 +0900261
262 memset(&ap->eh_context, 0, sizeof(ap->eh_context));
263 ap->eh_context.i = ap->eh_info;
264 memset(&ap->eh_info, 0, sizeof(ap->eh_info));
265
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900266 ap->flags |= ATA_FLAG_EH_IN_PROGRESS;
Tejun Heoad9e2762006-05-15 20:58:12 +0900267 ap->flags &= ~ATA_FLAG_EH_PENDING;
Tejun Heof3e81b12006-05-15 20:58:21 +0900268
Jeff Garzikba6a1302006-06-22 23:46:10 -0400269 spin_unlock_irqrestore(ap_lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900270
Tejun Heo720ba122006-05-31 18:28:13 +0900271 /* invoke EH. if unloading, just finish failed qcs */
272 if (!(ap->flags & ATA_FLAG_UNLOADING))
273 ap->ops->error_handler(ap);
274 else
275 ata_eh_finish(ap);
Tejun Heoad9e2762006-05-15 20:58:12 +0900276
277 /* Exception might have happend after ->error_handler
278 * recovered the port but before this point. Repeat
279 * EH in such case.
280 */
Jeff Garzikba6a1302006-06-22 23:46:10 -0400281 spin_lock_irqsave(ap_lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900282
283 if (ap->flags & ATA_FLAG_EH_PENDING) {
284 if (--repeat_cnt) {
285 ata_port_printk(ap, KERN_INFO,
286 "EH pending after completion, "
287 "repeating EH (cnt=%d)\n", repeat_cnt);
Jeff Garzikba6a1302006-06-22 23:46:10 -0400288 spin_unlock_irqrestore(ap_lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900289 goto repeat;
290 }
291 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
292 "tries, giving up\n", ATA_EH_MAX_REPEAT);
293 }
294
Tejun Heof3e81b12006-05-15 20:58:21 +0900295 /* this run is complete, make sure EH info is clear */
296 memset(&ap->eh_info, 0, sizeof(ap->eh_info));
297
Jeff Garzikba6a1302006-06-22 23:46:10 -0400298 /* Clear host_eh_scheduled while holding ap_lock such
Tejun Heoad9e2762006-05-15 20:58:12 +0900299 * that if exception occurs after this point but
300 * before EH completion, SCSI midlayer will
301 * re-initiate EH.
302 */
303 host->host_eh_scheduled = 0;
304
Jeff Garzikba6a1302006-06-22 23:46:10 -0400305 spin_unlock_irqrestore(ap_lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900306 } else {
307 WARN_ON(ata_qc_from_tag(ap, ap->active_tag) == NULL);
308 ap->ops->eng_timeout(ap);
309 }
310
311 /* finish or retry handled scmd's and clean up */
Tejun Heoece1d632006-04-02 18:51:53 +0900312 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
313
314 scsi_eh_flush_done_q(&ap->eh_done_q);
315
Tejun Heoad9e2762006-05-15 20:58:12 +0900316 /* clean up */
Jeff Garzikba6a1302006-06-22 23:46:10 -0400317 spin_lock_irqsave(ap_lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900318
Tejun Heo3e706392006-05-31 18:28:11 +0900319 if (ap->flags & ATA_FLAG_LOADING) {
320 ap->flags &= ~ATA_FLAG_LOADING;
321 } else {
322 if (ap->flags & ATA_FLAG_SCSI_HOTPLUG)
323 queue_work(ata_aux_wq, &ap->hotplug_task);
324 if (ap->flags & ATA_FLAG_RECOVERED)
325 ata_port_printk(ap, KERN_INFO, "EH complete\n");
326 }
Tejun Heo580b21022006-05-31 18:28:05 +0900327
328 ap->flags &= ~(ATA_FLAG_SCSI_HOTPLUG | ATA_FLAG_RECOVERED);
Tejun Heoad9e2762006-05-15 20:58:12 +0900329
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900330 /* tell wait_eh that we're done */
331 ap->flags &= ~ATA_FLAG_EH_IN_PROGRESS;
332 wake_up_all(&ap->eh_wait_q);
333
Jeff Garzikba6a1302006-06-22 23:46:10 -0400334 spin_unlock_irqrestore(ap_lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900335
Tejun Heoece1d632006-04-02 18:51:53 +0900336 DPRINTK("EXIT\n");
Tejun Heoece1d632006-04-02 18:51:53 +0900337}
338
339/**
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900340 * ata_port_wait_eh - Wait for the currently pending EH to complete
341 * @ap: Port to wait EH for
342 *
343 * Wait until the currently pending EH is complete.
344 *
345 * LOCKING:
346 * Kernel thread context (may sleep).
347 */
348void ata_port_wait_eh(struct ata_port *ap)
349{
350 unsigned long flags;
351 DEFINE_WAIT(wait);
352
353 retry:
Jeff Garzikba6a1302006-06-22 23:46:10 -0400354 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900355
356 while (ap->flags & (ATA_FLAG_EH_PENDING | ATA_FLAG_EH_IN_PROGRESS)) {
357 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
Jeff Garzikba6a1302006-06-22 23:46:10 -0400358 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900359 schedule();
Jeff Garzikba6a1302006-06-22 23:46:10 -0400360 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900361 }
Tejun Heo0a1b6222006-06-11 11:01:38 +0900362 finish_wait(&ap->eh_wait_q, &wait);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900363
Jeff Garzikba6a1302006-06-22 23:46:10 -0400364 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900365
366 /* make sure SCSI EH is complete */
367 if (scsi_host_in_recovery(ap->host)) {
368 msleep(10);
369 goto retry;
370 }
371}
372
373/**
Tejun Heoece1d632006-04-02 18:51:53 +0900374 * ata_qc_timeout - Handle timeout of queued command
375 * @qc: Command that timed out
376 *
377 * Some part of the kernel (currently, only the SCSI layer)
378 * has noticed that the active command on port @ap has not
379 * completed after a specified length of time. Handle this
380 * condition by disabling DMA (if necessary) and completing
381 * transactions, with error if necessary.
382 *
383 * This also handles the case of the "lost interrupt", where
384 * for some reason (possibly hardware bug, possibly driver bug)
385 * an interrupt was not delivered to the driver, even though the
386 * transaction completed successfully.
387 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900388 * TODO: kill this function once old EH is gone.
389 *
Tejun Heoece1d632006-04-02 18:51:53 +0900390 * LOCKING:
391 * Inherited from SCSI layer (none, can sleep)
392 */
393static void ata_qc_timeout(struct ata_queued_cmd *qc)
394{
395 struct ata_port *ap = qc->ap;
Tejun Heoece1d632006-04-02 18:51:53 +0900396 u8 host_stat = 0, drv_stat;
397 unsigned long flags;
398
399 DPRINTK("ENTER\n");
400
401 ap->hsm_task_state = HSM_ST_IDLE;
402
Jeff Garzikba6a1302006-06-22 23:46:10 -0400403 spin_lock_irqsave(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900404
405 switch (qc->tf.protocol) {
406
407 case ATA_PROT_DMA:
408 case ATA_PROT_ATAPI_DMA:
409 host_stat = ap->ops->bmdma_status(ap);
410
411 /* before we do anything else, clear DMA-Start bit */
412 ap->ops->bmdma_stop(qc);
413
414 /* fall through */
415
416 default:
417 ata_altstatus(ap);
418 drv_stat = ata_chk_status(ap);
419
420 /* ack bmdma irq events */
421 ap->ops->irq_clear(ap);
422
Tejun Heof15a1da2006-05-15 20:57:56 +0900423 ata_dev_printk(qc->dev, KERN_ERR, "command 0x%x timeout, "
424 "stat 0x%x host_stat 0x%x\n",
425 qc->tf.command, drv_stat, host_stat);
Tejun Heoece1d632006-04-02 18:51:53 +0900426
427 /* complete taskfile transaction */
Jeff Garzikc13b56a2006-04-02 10:34:24 -0400428 qc->err_mask |= AC_ERR_TIMEOUT;
Tejun Heoece1d632006-04-02 18:51:53 +0900429 break;
430 }
431
Jeff Garzikba6a1302006-06-22 23:46:10 -0400432 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900433
434 ata_eh_qc_complete(qc);
435
436 DPRINTK("EXIT\n");
437}
438
439/**
440 * ata_eng_timeout - Handle timeout of queued command
441 * @ap: Port on which timed-out command is active
442 *
443 * Some part of the kernel (currently, only the SCSI layer)
444 * has noticed that the active command on port @ap has not
445 * completed after a specified length of time. Handle this
446 * condition by disabling DMA (if necessary) and completing
447 * transactions, with error if necessary.
448 *
449 * This also handles the case of the "lost interrupt", where
450 * for some reason (possibly hardware bug, possibly driver bug)
451 * an interrupt was not delivered to the driver, even though the
452 * transaction completed successfully.
453 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900454 * TODO: kill this function once old EH is gone.
455 *
Tejun Heoece1d632006-04-02 18:51:53 +0900456 * LOCKING:
457 * Inherited from SCSI layer (none, can sleep)
458 */
459void ata_eng_timeout(struct ata_port *ap)
460{
461 DPRINTK("ENTER\n");
462
463 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
464
465 DPRINTK("EXIT\n");
466}
467
Tejun Heof686bcb2006-05-15 20:58:05 +0900468/**
469 * ata_qc_schedule_eh - schedule qc for error handling
470 * @qc: command to schedule error handling for
471 *
472 * Schedule error handling for @qc. EH will kick in as soon as
473 * other commands are drained.
474 *
475 * LOCKING:
476 * spin_lock_irqsave(host_set lock)
477 */
478void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
479{
480 struct ata_port *ap = qc->ap;
481
482 WARN_ON(!ap->ops->error_handler);
483
484 qc->flags |= ATA_QCFLAG_FAILED;
485 qc->ap->flags |= ATA_FLAG_EH_PENDING;
486
487 /* The following will fail if timeout has already expired.
488 * ata_scsi_error() takes care of such scmds on EH entry.
489 * Note that ATA_QCFLAG_FAILED is unconditionally set after
490 * this function completes.
491 */
492 scsi_req_abort_cmd(qc->scsicmd);
493}
494
Tejun Heo7b70fc02006-05-15 20:58:07 +0900495/**
496 * ata_port_schedule_eh - schedule error handling without a qc
497 * @ap: ATA port to schedule EH for
498 *
499 * Schedule error handling for @ap. EH will kick in as soon as
500 * all commands are drained.
501 *
502 * LOCKING:
503 * spin_lock_irqsave(host_set lock)
504 */
505void ata_port_schedule_eh(struct ata_port *ap)
506{
507 WARN_ON(!ap->ops->error_handler);
508
509 ap->flags |= ATA_FLAG_EH_PENDING;
Tejun Heof8bbfc22006-05-19 21:07:05 +0900510 scsi_schedule_eh(ap->host);
Tejun Heo7b70fc02006-05-15 20:58:07 +0900511
512 DPRINTK("port EH scheduled\n");
513}
514
515/**
516 * ata_port_abort - abort all qc's on the port
517 * @ap: ATA port to abort qc's for
518 *
519 * Abort all active qc's of @ap and schedule EH.
520 *
521 * LOCKING:
522 * spin_lock_irqsave(host_set lock)
523 *
524 * RETURNS:
525 * Number of aborted qc's.
526 */
527int ata_port_abort(struct ata_port *ap)
528{
529 int tag, nr_aborted = 0;
530
531 WARN_ON(!ap->ops->error_handler);
532
533 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
534 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
535
536 if (qc) {
537 qc->flags |= ATA_QCFLAG_FAILED;
538 ata_qc_complete(qc);
539 nr_aborted++;
540 }
541 }
542
543 if (!nr_aborted)
544 ata_port_schedule_eh(ap);
545
546 return nr_aborted;
547}
548
Tejun Heoe3180492006-05-15 20:58:09 +0900549/**
550 * __ata_port_freeze - freeze port
551 * @ap: ATA port to freeze
552 *
553 * This function is called when HSM violation or some other
554 * condition disrupts normal operation of the port. Frozen port
555 * is not allowed to perform any operation until the port is
556 * thawed, which usually follows a successful reset.
557 *
558 * ap->ops->freeze() callback can be used for freezing the port
559 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
560 * port cannot be frozen hardware-wise, the interrupt handler
561 * must ack and clear interrupts unconditionally while the port
562 * is frozen.
563 *
564 * LOCKING:
565 * spin_lock_irqsave(host_set lock)
566 */
567static void __ata_port_freeze(struct ata_port *ap)
568{
569 WARN_ON(!ap->ops->error_handler);
570
571 if (ap->ops->freeze)
572 ap->ops->freeze(ap);
573
574 ap->flags |= ATA_FLAG_FROZEN;
575
576 DPRINTK("ata%u port frozen\n", ap->id);
577}
578
579/**
580 * ata_port_freeze - abort & freeze port
581 * @ap: ATA port to freeze
582 *
583 * Abort and freeze @ap.
584 *
585 * LOCKING:
586 * spin_lock_irqsave(host_set lock)
587 *
588 * RETURNS:
589 * Number of aborted commands.
590 */
591int ata_port_freeze(struct ata_port *ap)
592{
593 int nr_aborted;
594
595 WARN_ON(!ap->ops->error_handler);
596
597 nr_aborted = ata_port_abort(ap);
598 __ata_port_freeze(ap);
599
600 return nr_aborted;
601}
602
603/**
604 * ata_eh_freeze_port - EH helper to freeze port
605 * @ap: ATA port to freeze
606 *
607 * Freeze @ap.
608 *
609 * LOCKING:
610 * None.
611 */
612void ata_eh_freeze_port(struct ata_port *ap)
613{
614 unsigned long flags;
615
616 if (!ap->ops->error_handler)
617 return;
618
Jeff Garzikba6a1302006-06-22 23:46:10 -0400619 spin_lock_irqsave(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900620 __ata_port_freeze(ap);
Jeff Garzikba6a1302006-06-22 23:46:10 -0400621 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900622}
623
624/**
625 * ata_port_thaw_port - EH helper to thaw port
626 * @ap: ATA port to thaw
627 *
628 * Thaw frozen port @ap.
629 *
630 * LOCKING:
631 * None.
632 */
633void ata_eh_thaw_port(struct ata_port *ap)
634{
635 unsigned long flags;
636
637 if (!ap->ops->error_handler)
638 return;
639
Jeff Garzikba6a1302006-06-22 23:46:10 -0400640 spin_lock_irqsave(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900641
642 ap->flags &= ~ATA_FLAG_FROZEN;
643
644 if (ap->ops->thaw)
645 ap->ops->thaw(ap);
646
Jeff Garzikba6a1302006-06-22 23:46:10 -0400647 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900648
649 DPRINTK("ata%u port thawed\n", ap->id);
650}
651
Tejun Heoece1d632006-04-02 18:51:53 +0900652static void ata_eh_scsidone(struct scsi_cmnd *scmd)
653{
654 /* nada */
655}
656
657static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
658{
659 struct ata_port *ap = qc->ap;
660 struct scsi_cmnd *scmd = qc->scsicmd;
661 unsigned long flags;
662
Jeff Garzikba6a1302006-06-22 23:46:10 -0400663 spin_lock_irqsave(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900664 qc->scsidone = ata_eh_scsidone;
665 __ata_qc_complete(qc);
666 WARN_ON(ata_tag_valid(qc->tag));
Jeff Garzikba6a1302006-06-22 23:46:10 -0400667 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900668
669 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
670}
671
672/**
673 * ata_eh_qc_complete - Complete an active ATA command from EH
674 * @qc: Command to complete
675 *
676 * Indicate to the mid and upper layers that an ATA command has
677 * completed. To be used from EH.
678 */
679void ata_eh_qc_complete(struct ata_queued_cmd *qc)
680{
681 struct scsi_cmnd *scmd = qc->scsicmd;
682 scmd->retries = scmd->allowed;
683 __ata_eh_qc_complete(qc);
684}
685
686/**
687 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
688 * @qc: Command to retry
689 *
690 * Indicate to the mid and upper layers that an ATA command
691 * should be retried. To be used from EH.
692 *
693 * SCSI midlayer limits the number of retries to scmd->allowed.
694 * scmd->retries is decremented for commands which get retried
695 * due to unrelated failures (qc->err_mask is zero).
696 */
697void ata_eh_qc_retry(struct ata_queued_cmd *qc)
698{
699 struct scsi_cmnd *scmd = qc->scsicmd;
700 if (!qc->err_mask && scmd->retries)
701 scmd->retries--;
702 __ata_eh_qc_complete(qc);
703}
Tejun Heo022bdb02006-05-15 20:58:22 +0900704
705/**
Tejun Heo0ea035a2006-05-31 18:28:01 +0900706 * ata_eh_detach_dev - detach ATA device
707 * @dev: ATA device to detach
708 *
709 * Detach @dev.
710 *
711 * LOCKING:
712 * None.
713 */
714static void ata_eh_detach_dev(struct ata_device *dev)
715{
716 struct ata_port *ap = dev->ap;
717 unsigned long flags;
718
719 ata_dev_disable(dev);
720
Jeff Garzikba6a1302006-06-22 23:46:10 -0400721 spin_lock_irqsave(ap->lock, flags);
Tejun Heo0ea035a2006-05-31 18:28:01 +0900722
723 dev->flags &= ~ATA_DFLAG_DETACH;
724
725 if (ata_scsi_offline_dev(dev)) {
726 dev->flags |= ATA_DFLAG_DETACHED;
727 ap->flags |= ATA_FLAG_SCSI_HOTPLUG;
728 }
729
Jeff Garzikba6a1302006-06-22 23:46:10 -0400730 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo0ea035a2006-05-31 18:28:01 +0900731}
732
733/**
Tejun Heo022bdb02006-05-15 20:58:22 +0900734 * ata_eh_about_to_do - about to perform eh_action
735 * @ap: target ATA port
Tejun Heo47005f22006-06-19 18:27:23 +0900736 * @dev: target ATA dev for per-dev action (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +0900737 * @action: action about to be performed
738 *
739 * Called just before performing EH actions to clear related bits
740 * in @ap->eh_info such that eh actions are not unnecessarily
741 * repeated.
742 *
743 * LOCKING:
744 * None.
745 */
Tejun Heo47005f22006-06-19 18:27:23 +0900746static void ata_eh_about_to_do(struct ata_port *ap, struct ata_device *dev,
747 unsigned int action)
Tejun Heo022bdb02006-05-15 20:58:22 +0900748{
749 unsigned long flags;
750
Jeff Garzikba6a1302006-06-22 23:46:10 -0400751 spin_lock_irqsave(ap->lock, flags);
Tejun Heo47005f22006-06-19 18:27:23 +0900752 ata_eh_clear_action(dev, &ap->eh_info, action);
Tejun Heo022bdb02006-05-15 20:58:22 +0900753 ap->flags |= ATA_FLAG_RECOVERED;
Jeff Garzikba6a1302006-06-22 23:46:10 -0400754 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo022bdb02006-05-15 20:58:22 +0900755}
756
757/**
Tejun Heo47005f22006-06-19 18:27:23 +0900758 * ata_eh_done - EH action complete
759 * @ap: target ATA port
760 * @dev: target ATA dev for per-dev action (can be NULL)
761 * @action: action just completed
762 *
763 * Called right after performing EH actions to clear related bits
764 * in @ap->eh_context.
765 *
766 * LOCKING:
767 * None.
768 */
769static void ata_eh_done(struct ata_port *ap, struct ata_device *dev,
770 unsigned int action)
771{
772 ata_eh_clear_action(dev, &ap->eh_context.i, action);
773}
774
775/**
Tejun Heo022bdb02006-05-15 20:58:22 +0900776 * ata_err_string - convert err_mask to descriptive string
777 * @err_mask: error mask to convert to string
778 *
779 * Convert @err_mask to descriptive string. Errors are
780 * prioritized according to severity and only the most severe
781 * error is reported.
782 *
783 * LOCKING:
784 * None.
785 *
786 * RETURNS:
787 * Descriptive string for @err_mask
788 */
789static const char * ata_err_string(unsigned int err_mask)
790{
791 if (err_mask & AC_ERR_HOST_BUS)
792 return "host bus error";
793 if (err_mask & AC_ERR_ATA_BUS)
794 return "ATA bus error";
795 if (err_mask & AC_ERR_TIMEOUT)
796 return "timeout";
797 if (err_mask & AC_ERR_HSM)
798 return "HSM violation";
799 if (err_mask & AC_ERR_SYSTEM)
800 return "internal error";
801 if (err_mask & AC_ERR_MEDIA)
802 return "media error";
803 if (err_mask & AC_ERR_INVALID)
804 return "invalid argument";
805 if (err_mask & AC_ERR_DEV)
806 return "device error";
807 return "unknown error";
808}
809
810/**
Tejun Heoe8ee8452006-05-15 21:03:46 +0900811 * ata_read_log_page - read a specific log page
812 * @dev: target device
813 * @page: page to read
814 * @buf: buffer to store read page
815 * @sectors: number of sectors to read
816 *
817 * Read log page using READ_LOG_EXT command.
818 *
819 * LOCKING:
820 * Kernel thread context (may sleep).
821 *
822 * RETURNS:
823 * 0 on success, AC_ERR_* mask otherwise.
824 */
825static unsigned int ata_read_log_page(struct ata_device *dev,
826 u8 page, void *buf, unsigned int sectors)
827{
828 struct ata_taskfile tf;
829 unsigned int err_mask;
830
831 DPRINTK("read log page - page %d\n", page);
832
833 ata_tf_init(dev, &tf);
834 tf.command = ATA_CMD_READ_LOG_EXT;
835 tf.lbal = page;
836 tf.nsect = sectors;
837 tf.hob_nsect = sectors >> 8;
838 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
839 tf.protocol = ATA_PROT_PIO;
840
841 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
842 buf, sectors * ATA_SECT_SIZE);
843
844 DPRINTK("EXIT, err_mask=%x\n", err_mask);
845 return err_mask;
846}
847
848/**
849 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
850 * @dev: Device to read log page 10h from
851 * @tag: Resulting tag of the failed command
852 * @tf: Resulting taskfile registers of the failed command
853 *
854 * Read log page 10h to obtain NCQ error details and clear error
855 * condition.
856 *
857 * LOCKING:
858 * Kernel thread context (may sleep).
859 *
860 * RETURNS:
861 * 0 on success, -errno otherwise.
862 */
863static int ata_eh_read_log_10h(struct ata_device *dev,
864 int *tag, struct ata_taskfile *tf)
865{
866 u8 *buf = dev->ap->sector_buf;
867 unsigned int err_mask;
868 u8 csum;
869 int i;
870
871 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
872 if (err_mask)
873 return -EIO;
874
875 csum = 0;
876 for (i = 0; i < ATA_SECT_SIZE; i++)
877 csum += buf[i];
878 if (csum)
879 ata_dev_printk(dev, KERN_WARNING,
880 "invalid checksum 0x%x on log page 10h\n", csum);
881
882 if (buf[0] & 0x80)
883 return -ENOENT;
884
885 *tag = buf[0] & 0x1f;
886
887 tf->command = buf[2];
888 tf->feature = buf[3];
889 tf->lbal = buf[4];
890 tf->lbam = buf[5];
891 tf->lbah = buf[6];
892 tf->device = buf[7];
893 tf->hob_lbal = buf[8];
894 tf->hob_lbam = buf[9];
895 tf->hob_lbah = buf[10];
896 tf->nsect = buf[12];
897 tf->hob_nsect = buf[13];
898
899 return 0;
900}
901
902/**
Tejun Heo022bdb02006-05-15 20:58:22 +0900903 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
904 * @dev: device to perform REQUEST_SENSE to
905 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
906 *
907 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
908 * SENSE. This function is EH helper.
909 *
910 * LOCKING:
911 * Kernel thread context (may sleep).
912 *
913 * RETURNS:
914 * 0 on success, AC_ERR_* mask on failure
915 */
916static unsigned int atapi_eh_request_sense(struct ata_device *dev,
917 unsigned char *sense_buf)
918{
919 struct ata_port *ap = dev->ap;
920 struct ata_taskfile tf;
921 u8 cdb[ATAPI_CDB_LEN];
922
923 DPRINTK("ATAPI request sense\n");
924
925 ata_tf_init(dev, &tf);
926
927 /* FIXME: is this needed? */
928 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
929
930 /* XXX: why tf_read here? */
931 ap->ops->tf_read(ap, &tf);
932
933 /* fill these in, for the case where they are -not- overwritten */
934 sense_buf[0] = 0x70;
935 sense_buf[2] = tf.feature >> 4;
936
937 memset(cdb, 0, ATAPI_CDB_LEN);
938 cdb[0] = REQUEST_SENSE;
939 cdb[4] = SCSI_SENSE_BUFFERSIZE;
940
941 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
942 tf.command = ATA_CMD_PACKET;
943
944 /* is it pointless to prefer PIO for "safety reasons"? */
945 if (ap->flags & ATA_FLAG_PIO_DMA) {
946 tf.protocol = ATA_PROT_ATAPI_DMA;
947 tf.feature |= ATAPI_PKT_DMA;
948 } else {
949 tf.protocol = ATA_PROT_ATAPI;
950 tf.lbam = (8 * 1024) & 0xff;
951 tf.lbah = (8 * 1024) >> 8;
952 }
953
954 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
955 sense_buf, SCSI_SENSE_BUFFERSIZE);
956}
957
958/**
959 * ata_eh_analyze_serror - analyze SError for a failed port
960 * @ap: ATA port to analyze SError for
961 *
962 * Analyze SError if available and further determine cause of
963 * failure.
964 *
965 * LOCKING:
966 * None.
967 */
968static void ata_eh_analyze_serror(struct ata_port *ap)
969{
970 struct ata_eh_context *ehc = &ap->eh_context;
971 u32 serror = ehc->i.serror;
972 unsigned int err_mask = 0, action = 0;
973
974 if (serror & SERR_PERSISTENT) {
975 err_mask |= AC_ERR_ATA_BUS;
976 action |= ATA_EH_HARDRESET;
977 }
978 if (serror &
979 (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
980 err_mask |= AC_ERR_ATA_BUS;
981 action |= ATA_EH_SOFTRESET;
982 }
983 if (serror & SERR_PROTOCOL) {
984 err_mask |= AC_ERR_HSM;
985 action |= ATA_EH_SOFTRESET;
986 }
987 if (serror & SERR_INTERNAL) {
988 err_mask |= AC_ERR_SYSTEM;
989 action |= ATA_EH_SOFTRESET;
990 }
Tejun Heo084fe632006-05-31 18:28:03 +0900991 if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG))
992 ata_ehi_hotplugged(&ehc->i);
Tejun Heo022bdb02006-05-15 20:58:22 +0900993
994 ehc->i.err_mask |= err_mask;
995 ehc->i.action |= action;
996}
997
998/**
Tejun Heoe8ee8452006-05-15 21:03:46 +0900999 * ata_eh_analyze_ncq_error - analyze NCQ error
1000 * @ap: ATA port to analyze NCQ error for
1001 *
1002 * Read log page 10h, determine the offending qc and acquire
1003 * error status TF. For NCQ device errors, all LLDDs have to do
1004 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1005 * care of the rest.
1006 *
1007 * LOCKING:
1008 * Kernel thread context (may sleep).
1009 */
1010static void ata_eh_analyze_ncq_error(struct ata_port *ap)
1011{
1012 struct ata_eh_context *ehc = &ap->eh_context;
1013 struct ata_device *dev = ap->device;
1014 struct ata_queued_cmd *qc;
1015 struct ata_taskfile tf;
1016 int tag, rc;
1017
1018 /* if frozen, we can't do much */
1019 if (ap->flags & ATA_FLAG_FROZEN)
1020 return;
1021
1022 /* is it NCQ device error? */
1023 if (!ap->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1024 return;
1025
1026 /* has LLDD analyzed already? */
1027 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1028 qc = __ata_qc_from_tag(ap, tag);
1029
1030 if (!(qc->flags & ATA_QCFLAG_FAILED))
1031 continue;
1032
1033 if (qc->err_mask)
1034 return;
1035 }
1036
1037 /* okay, this error is ours */
1038 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1039 if (rc) {
1040 ata_port_printk(ap, KERN_ERR, "failed to read log page 10h "
1041 "(errno=%d)\n", rc);
1042 return;
1043 }
1044
1045 if (!(ap->sactive & (1 << tag))) {
1046 ata_port_printk(ap, KERN_ERR, "log page 10h reported "
1047 "inactive tag %d\n", tag);
1048 return;
1049 }
1050
1051 /* we've got the perpetrator, condemn it */
1052 qc = __ata_qc_from_tag(ap, tag);
1053 memcpy(&qc->result_tf, &tf, sizeof(tf));
1054 qc->err_mask |= AC_ERR_DEV;
1055 ehc->i.err_mask &= ~AC_ERR_DEV;
1056}
1057
1058/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001059 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1060 * @qc: qc to analyze
1061 * @tf: Taskfile registers to analyze
1062 *
1063 * Analyze taskfile of @qc and further determine cause of
1064 * failure. This function also requests ATAPI sense data if
1065 * avaliable.
1066 *
1067 * LOCKING:
1068 * Kernel thread context (may sleep).
1069 *
1070 * RETURNS:
1071 * Determined recovery action
1072 */
1073static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1074 const struct ata_taskfile *tf)
1075{
1076 unsigned int tmp, action = 0;
1077 u8 stat = tf->command, err = tf->feature;
1078
1079 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1080 qc->err_mask |= AC_ERR_HSM;
1081 return ATA_EH_SOFTRESET;
1082 }
1083
1084 if (!(qc->err_mask & AC_ERR_DEV))
1085 return 0;
1086
1087 switch (qc->dev->class) {
1088 case ATA_DEV_ATA:
1089 if (err & ATA_ICRC)
1090 qc->err_mask |= AC_ERR_ATA_BUS;
1091 if (err & ATA_UNC)
1092 qc->err_mask |= AC_ERR_MEDIA;
1093 if (err & ATA_IDNF)
1094 qc->err_mask |= AC_ERR_INVALID;
1095 break;
1096
1097 case ATA_DEV_ATAPI:
1098 tmp = atapi_eh_request_sense(qc->dev,
1099 qc->scsicmd->sense_buffer);
1100 if (!tmp) {
1101 /* ATA_QCFLAG_SENSE_VALID is used to tell
1102 * atapi_qc_complete() that sense data is
1103 * already valid.
1104 *
1105 * TODO: interpret sense data and set
1106 * appropriate err_mask.
1107 */
1108 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1109 } else
1110 qc->err_mask |= tmp;
1111 }
1112
1113 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1114 action |= ATA_EH_SOFTRESET;
1115
1116 return action;
1117}
1118
1119static int ata_eh_categorize_ering_entry(struct ata_ering_entry *ent)
1120{
1121 if (ent->err_mask & (AC_ERR_ATA_BUS | AC_ERR_TIMEOUT))
1122 return 1;
1123
1124 if (ent->is_io) {
1125 if (ent->err_mask & AC_ERR_HSM)
1126 return 1;
1127 if ((ent->err_mask &
1128 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1129 return 2;
1130 }
1131
1132 return 0;
1133}
1134
1135struct speed_down_needed_arg {
1136 u64 since;
1137 int nr_errors[3];
1138};
1139
1140static int speed_down_needed_cb(struct ata_ering_entry *ent, void *void_arg)
1141{
1142 struct speed_down_needed_arg *arg = void_arg;
1143
1144 if (ent->timestamp < arg->since)
1145 return -1;
1146
1147 arg->nr_errors[ata_eh_categorize_ering_entry(ent)]++;
1148 return 0;
1149}
1150
1151/**
1152 * ata_eh_speed_down_needed - Determine wheter speed down is necessary
1153 * @dev: Device of interest
1154 *
1155 * This function examines error ring of @dev and determines
1156 * whether speed down is necessary. Speed down is necessary if
1157 * there have been more than 3 of Cat-1 errors or 10 of Cat-2
1158 * errors during last 15 minutes.
1159 *
1160 * Cat-1 errors are ATA_BUS, TIMEOUT for any command and HSM
1161 * violation for known supported commands.
1162 *
1163 * Cat-2 errors are unclassified DEV error for known supported
1164 * command.
1165 *
1166 * LOCKING:
1167 * Inherited from caller.
1168 *
1169 * RETURNS:
1170 * 1 if speed down is necessary, 0 otherwise
1171 */
1172static int ata_eh_speed_down_needed(struct ata_device *dev)
1173{
1174 const u64 interval = 15LLU * 60 * HZ;
1175 static const int err_limits[3] = { -1, 3, 10 };
1176 struct speed_down_needed_arg arg;
1177 struct ata_ering_entry *ent;
1178 int err_cat;
1179 u64 j64;
1180
1181 ent = ata_ering_top(&dev->ering);
1182 if (!ent)
1183 return 0;
1184
1185 err_cat = ata_eh_categorize_ering_entry(ent);
1186 if (err_cat == 0)
1187 return 0;
1188
1189 memset(&arg, 0, sizeof(arg));
1190
1191 j64 = get_jiffies_64();
1192 if (j64 >= interval)
1193 arg.since = j64 - interval;
1194 else
1195 arg.since = 0;
1196
1197 ata_ering_map(&dev->ering, speed_down_needed_cb, &arg);
1198
1199 return arg.nr_errors[err_cat] > err_limits[err_cat];
1200}
1201
1202/**
1203 * ata_eh_speed_down - record error and speed down if necessary
1204 * @dev: Failed device
1205 * @is_io: Did the device fail during normal IO?
1206 * @err_mask: err_mask of the error
1207 *
1208 * Record error and examine error history to determine whether
1209 * adjusting transmission speed is necessary. It also sets
1210 * transmission limits appropriately if such adjustment is
1211 * necessary.
1212 *
1213 * LOCKING:
1214 * Kernel thread context (may sleep).
1215 *
1216 * RETURNS:
1217 * 0 on success, -errno otherwise
1218 */
1219static int ata_eh_speed_down(struct ata_device *dev, int is_io,
1220 unsigned int err_mask)
1221{
1222 if (!err_mask)
1223 return 0;
1224
1225 /* record error and determine whether speed down is necessary */
1226 ata_ering_record(&dev->ering, is_io, err_mask);
1227
1228 if (!ata_eh_speed_down_needed(dev))
1229 return 0;
1230
1231 /* speed down SATA link speed if possible */
1232 if (sata_down_spd_limit(dev->ap) == 0)
1233 return ATA_EH_HARDRESET;
1234
1235 /* lower transfer mode */
1236 if (ata_down_xfermask_limit(dev, 0) == 0)
1237 return ATA_EH_SOFTRESET;
1238
1239 ata_dev_printk(dev, KERN_ERR,
1240 "speed down requested but no transfer mode left\n");
1241 return 0;
1242}
1243
1244/**
1245 * ata_eh_autopsy - analyze error and determine recovery action
1246 * @ap: ATA port to perform autopsy on
1247 *
1248 * Analyze why @ap failed and determine which recovery action is
1249 * needed. This function also sets more detailed AC_ERR_* values
1250 * and fills sense data for ATAPI CHECK SENSE.
1251 *
1252 * LOCKING:
1253 * Kernel thread context (may sleep).
1254 */
1255static void ata_eh_autopsy(struct ata_port *ap)
1256{
1257 struct ata_eh_context *ehc = &ap->eh_context;
1258 unsigned int action = ehc->i.action;
1259 struct ata_device *failed_dev = NULL;
1260 unsigned int all_err_mask = 0;
1261 int tag, is_io = 0;
1262 u32 serror;
1263 int rc;
1264
1265 DPRINTK("ENTER\n");
1266
1267 /* obtain and analyze SError */
1268 rc = sata_scr_read(ap, SCR_ERROR, &serror);
1269 if (rc == 0) {
1270 ehc->i.serror |= serror;
1271 ata_eh_analyze_serror(ap);
1272 } else if (rc != -EOPNOTSUPP)
1273 action |= ATA_EH_HARDRESET;
1274
Tejun Heoe8ee8452006-05-15 21:03:46 +09001275 /* analyze NCQ failure */
1276 ata_eh_analyze_ncq_error(ap);
1277
Tejun Heo022bdb02006-05-15 20:58:22 +09001278 /* any real error trumps AC_ERR_OTHER */
1279 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1280 ehc->i.err_mask &= ~AC_ERR_OTHER;
1281
1282 all_err_mask |= ehc->i.err_mask;
1283
1284 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1285 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1286
1287 if (!(qc->flags & ATA_QCFLAG_FAILED))
1288 continue;
1289
1290 /* inherit upper level err_mask */
1291 qc->err_mask |= ehc->i.err_mask;
1292
Tejun Heo022bdb02006-05-15 20:58:22 +09001293 /* analyze TF */
1294 action |= ata_eh_analyze_tf(qc, &qc->result_tf);
1295
1296 /* DEV errors are probably spurious in case of ATA_BUS error */
1297 if (qc->err_mask & AC_ERR_ATA_BUS)
1298 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1299 AC_ERR_INVALID);
1300
1301 /* any real error trumps unknown error */
1302 if (qc->err_mask & ~AC_ERR_OTHER)
1303 qc->err_mask &= ~AC_ERR_OTHER;
1304
1305 /* SENSE_VALID trumps dev/unknown error and revalidation */
1306 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1307 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
1308 action &= ~ATA_EH_REVALIDATE;
1309 }
1310
1311 /* accumulate error info */
1312 failed_dev = qc->dev;
1313 all_err_mask |= qc->err_mask;
1314 if (qc->flags & ATA_QCFLAG_IO)
1315 is_io = 1;
1316 }
1317
Tejun Heoa20f33f2006-05-16 12:58:24 +09001318 /* enforce default EH actions */
1319 if (ap->flags & ATA_FLAG_FROZEN ||
1320 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
1321 action |= ATA_EH_SOFTRESET;
1322 else if (all_err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001323 action |= ATA_EH_REVALIDATE;
1324
Tejun Heo47005f22006-06-19 18:27:23 +09001325 /* if we have offending qcs and the associated failed device */
1326 if (failed_dev) {
1327 /* speed down */
1328 action |= ata_eh_speed_down(failed_dev, is_io, all_err_mask);
1329
1330 /* perform per-dev EH action only on the offending device */
1331 ehc->i.dev_action[failed_dev->devno] |=
1332 action & ATA_EH_PERDEV_MASK;
1333 action &= ~ATA_EH_PERDEV_MASK;
1334 }
1335
Tejun Heoa20f33f2006-05-16 12:58:24 +09001336 /* record autopsy result */
Tejun Heo022bdb02006-05-15 20:58:22 +09001337 ehc->i.dev = failed_dev;
1338 ehc->i.action = action;
1339
1340 DPRINTK("EXIT\n");
1341}
1342
1343/**
1344 * ata_eh_report - report error handling to user
1345 * @ap: ATA port EH is going on
1346 *
1347 * Report EH to user.
1348 *
1349 * LOCKING:
1350 * None.
1351 */
1352static void ata_eh_report(struct ata_port *ap)
1353{
1354 struct ata_eh_context *ehc = &ap->eh_context;
1355 const char *frozen, *desc;
1356 int tag, nr_failed = 0;
1357
1358 desc = NULL;
1359 if (ehc->i.desc[0] != '\0')
1360 desc = ehc->i.desc;
1361
1362 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1363 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1364
1365 if (!(qc->flags & ATA_QCFLAG_FAILED))
1366 continue;
1367 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1368 continue;
1369
1370 nr_failed++;
1371 }
1372
1373 if (!nr_failed && !ehc->i.err_mask)
1374 return;
1375
1376 frozen = "";
1377 if (ap->flags & ATA_FLAG_FROZEN)
1378 frozen = " frozen";
1379
1380 if (ehc->i.dev) {
Tejun Heoe8ee8452006-05-15 21:03:46 +09001381 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1382 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1383 ehc->i.err_mask, ap->sactive, ehc->i.serror,
1384 ehc->i.action, frozen);
Tejun Heo022bdb02006-05-15 20:58:22 +09001385 if (desc)
1386 ata_dev_printk(ehc->i.dev, KERN_ERR, "(%s)\n", desc);
1387 } else {
Tejun Heoe8ee8452006-05-15 21:03:46 +09001388 ata_port_printk(ap, KERN_ERR, "exception Emask 0x%x "
1389 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1390 ehc->i.err_mask, ap->sactive, ehc->i.serror,
1391 ehc->i.action, frozen);
Tejun Heo022bdb02006-05-15 20:58:22 +09001392 if (desc)
1393 ata_port_printk(ap, KERN_ERR, "(%s)\n", desc);
1394 }
1395
1396 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1397 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1398
1399 if (!(qc->flags & ATA_QCFLAG_FAILED) || !qc->err_mask)
1400 continue;
1401
1402 ata_dev_printk(qc->dev, KERN_ERR, "tag %d cmd 0x%x "
1403 "Emask 0x%x stat 0x%x err 0x%x (%s)\n",
1404 qc->tag, qc->tf.command, qc->err_mask,
1405 qc->result_tf.command, qc->result_tf.feature,
1406 ata_err_string(qc->err_mask));
1407 }
1408}
1409
Tejun Heod87fa382006-05-31 18:28:24 +09001410static int ata_do_reset(struct ata_port *ap, ata_reset_fn_t reset,
1411 unsigned int *classes)
1412{
1413 int i, rc;
1414
1415 for (i = 0; i < ATA_MAX_DEVICES; i++)
1416 classes[i] = ATA_DEV_UNKNOWN;
1417
1418 rc = reset(ap, classes);
1419 if (rc)
1420 return rc;
1421
1422 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
1423 * is complete and convert all ATA_DEV_UNKNOWN to
1424 * ATA_DEV_NONE.
1425 */
1426 for (i = 0; i < ATA_MAX_DEVICES; i++)
1427 if (classes[i] != ATA_DEV_UNKNOWN)
1428 break;
1429
1430 if (i < ATA_MAX_DEVICES)
1431 for (i = 0; i < ATA_MAX_DEVICES; i++)
1432 if (classes[i] == ATA_DEV_UNKNOWN)
1433 classes[i] = ATA_DEV_NONE;
1434
1435 return 0;
1436}
1437
Tejun Heo664faf02006-05-31 18:27:50 +09001438static int ata_eh_followup_srst_needed(int rc, int classify,
1439 const unsigned int *classes)
1440{
1441 if (rc == -EAGAIN)
1442 return 1;
1443 if (rc != 0)
1444 return 0;
1445 if (classify && classes[0] == ATA_DEV_UNKNOWN)
1446 return 1;
1447 return 0;
1448}
1449
1450static int ata_eh_reset(struct ata_port *ap, int classify,
Tejun Heof5914a42006-05-31 18:27:48 +09001451 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
Tejun Heo022bdb02006-05-15 20:58:22 +09001452 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
1453{
1454 struct ata_eh_context *ehc = &ap->eh_context;
Tejun Heo664faf02006-05-31 18:27:50 +09001455 unsigned int *classes = ehc->classes;
Tejun Heo022bdb02006-05-15 20:58:22 +09001456 int tries = ATA_EH_RESET_TRIES;
Tejun Heo3e706392006-05-31 18:28:11 +09001457 int verbose = !(ap->flags & ATA_FLAG_LOADING);
Tejun Heof5914a42006-05-31 18:27:48 +09001458 unsigned int action;
Tejun Heo022bdb02006-05-15 20:58:22 +09001459 ata_reset_fn_t reset;
Tejun Heo664faf02006-05-31 18:27:50 +09001460 int i, did_followup_srst, rc;
Tejun Heo022bdb02006-05-15 20:58:22 +09001461
Tejun Heof5914a42006-05-31 18:27:48 +09001462 /* Determine which reset to use and record in ehc->i.action.
1463 * prereset() may examine and modify it.
1464 */
1465 action = ehc->i.action;
1466 ehc->i.action &= ~ATA_EH_RESET_MASK;
Tejun Heo022bdb02006-05-15 20:58:22 +09001467 if (softreset && (!hardreset || (!sata_set_spd_needed(ap) &&
Tejun Heof5914a42006-05-31 18:27:48 +09001468 !(action & ATA_EH_HARDRESET))))
1469 ehc->i.action |= ATA_EH_SOFTRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001470 else
Tejun Heof5914a42006-05-31 18:27:48 +09001471 ehc->i.action |= ATA_EH_HARDRESET;
1472
1473 if (prereset) {
1474 rc = prereset(ap);
1475 if (rc) {
1476 ata_port_printk(ap, KERN_ERR,
1477 "prereset failed (errno=%d)\n", rc);
1478 return rc;
1479 }
1480 }
1481
1482 /* prereset() might have modified ehc->i.action */
1483 if (ehc->i.action & ATA_EH_HARDRESET)
Tejun Heo022bdb02006-05-15 20:58:22 +09001484 reset = hardreset;
Tejun Heof5914a42006-05-31 18:27:48 +09001485 else if (ehc->i.action & ATA_EH_SOFTRESET)
1486 reset = softreset;
1487 else {
1488 /* prereset told us not to reset, bang classes and return */
1489 for (i = 0; i < ATA_MAX_DEVICES; i++)
1490 classes[i] = ATA_DEV_NONE;
1491 return 0;
1492 }
1493
1494 /* did prereset() screw up? if so, fix up to avoid oopsing */
1495 if (!reset) {
1496 ata_port_printk(ap, KERN_ERR, "BUG: prereset() requested "
1497 "invalid reset type\n");
1498 if (softreset)
1499 reset = softreset;
1500 else
1501 reset = hardreset;
1502 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001503
1504 retry:
Tejun Heo3e706392006-05-31 18:28:11 +09001505 /* shut up during boot probing */
1506 if (verbose)
1507 ata_port_printk(ap, KERN_INFO, "%s resetting port\n",
1508 reset == softreset ? "soft" : "hard");
Tejun Heo022bdb02006-05-15 20:58:22 +09001509
1510 /* reset */
Tejun Heo47005f22006-06-19 18:27:23 +09001511 ata_eh_about_to_do(ap, NULL, ATA_EH_RESET_MASK);
Tejun Heo022bdb02006-05-15 20:58:22 +09001512 ehc->i.flags |= ATA_EHI_DID_RESET;
1513
1514 rc = ata_do_reset(ap, reset, classes);
1515
Tejun Heo664faf02006-05-31 18:27:50 +09001516 did_followup_srst = 0;
1517 if (reset == hardreset &&
1518 ata_eh_followup_srst_needed(rc, classify, classes)) {
1519 /* okay, let's do follow-up softreset */
1520 did_followup_srst = 1;
1521 reset = softreset;
1522
1523 if (!reset) {
1524 ata_port_printk(ap, KERN_ERR,
1525 "follow-up softreset required "
1526 "but no softreset avaliable\n");
1527 return -EINVAL;
1528 }
1529
Tejun Heo47005f22006-06-19 18:27:23 +09001530 ata_eh_about_to_do(ap, NULL, ATA_EH_RESET_MASK);
Tejun Heo664faf02006-05-31 18:27:50 +09001531 rc = ata_do_reset(ap, reset, classes);
1532
1533 if (rc == 0 && classify &&
1534 classes[0] == ATA_DEV_UNKNOWN) {
1535 ata_port_printk(ap, KERN_ERR,
1536 "classification failed\n");
1537 return -EINVAL;
1538 }
1539 }
1540
Tejun Heo022bdb02006-05-15 20:58:22 +09001541 if (rc && --tries) {
Tejun Heo664faf02006-05-31 18:27:50 +09001542 const char *type;
1543
1544 if (reset == softreset) {
1545 if (did_followup_srst)
1546 type = "follow-up soft";
1547 else
1548 type = "soft";
1549 } else
1550 type = "hard";
1551
Tejun Heo022bdb02006-05-15 20:58:22 +09001552 ata_port_printk(ap, KERN_WARNING,
Tejun Heo664faf02006-05-31 18:27:50 +09001553 "%sreset failed, retrying in 5 secs\n", type);
Tejun Heo022bdb02006-05-15 20:58:22 +09001554 ssleep(5);
1555
1556 if (reset == hardreset)
1557 sata_down_spd_limit(ap);
1558 if (hardreset)
1559 reset = hardreset;
1560 goto retry;
1561 }
1562
1563 if (rc == 0) {
Tejun Heo20952b62006-05-31 18:27:23 +09001564 /* After the reset, the device state is PIO 0 and the
1565 * controller state is undefined. Record the mode.
1566 */
1567 for (i = 0; i < ATA_MAX_DEVICES; i++)
1568 ap->device[i].pio_mode = XFER_PIO_0;
1569
Tejun Heo022bdb02006-05-15 20:58:22 +09001570 if (postreset)
1571 postreset(ap, classes);
1572
1573 /* reset successful, schedule revalidation */
Tejun Heo47005f22006-06-19 18:27:23 +09001574 ata_eh_done(ap, NULL, ATA_EH_RESET_MASK);
Tejun Heo022bdb02006-05-15 20:58:22 +09001575 ehc->i.action |= ATA_EH_REVALIDATE;
1576 }
1577
1578 return rc;
1579}
1580
Tejun Heo084fe632006-05-31 18:28:03 +09001581static int ata_eh_revalidate_and_attach(struct ata_port *ap,
1582 struct ata_device **r_failed_dev)
Tejun Heo022bdb02006-05-15 20:58:22 +09001583{
1584 struct ata_eh_context *ehc = &ap->eh_context;
1585 struct ata_device *dev;
Tejun Heo084fe632006-05-31 18:28:03 +09001586 unsigned long flags;
Tejun Heo022bdb02006-05-15 20:58:22 +09001587 int i, rc = 0;
1588
1589 DPRINTK("ENTER\n");
1590
1591 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heo47005f22006-06-19 18:27:23 +09001592 unsigned int action;
Tejun Heo022bdb02006-05-15 20:58:22 +09001593
Tejun Heo47005f22006-06-19 18:27:23 +09001594 dev = &ap->device[i];
1595 action = ehc->i.action | ehc->i.dev_action[dev->devno];
1596
1597 if (action & ATA_EH_REVALIDATE && ata_dev_enabled(dev)) {
Tejun Heo022bdb02006-05-15 20:58:22 +09001598 if (ata_port_offline(ap)) {
1599 rc = -EIO;
1600 break;
1601 }
1602
Tejun Heo47005f22006-06-19 18:27:23 +09001603 ata_eh_about_to_do(ap, dev, ATA_EH_REVALIDATE);
Tejun Heo022bdb02006-05-15 20:58:22 +09001604 rc = ata_dev_revalidate(dev,
1605 ehc->i.flags & ATA_EHI_DID_RESET);
1606 if (rc)
1607 break;
1608
Tejun Heo47005f22006-06-19 18:27:23 +09001609 ata_eh_done(ap, dev, ATA_EH_REVALIDATE);
1610
zhao, forrest3057ac32006-06-12 12:01:34 +08001611 /* schedule the scsi_rescan_device() here */
1612 queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
Tejun Heo084fe632006-05-31 18:28:03 +09001613 } else if (dev->class == ATA_DEV_UNKNOWN &&
1614 ehc->tries[dev->devno] &&
1615 ata_class_enabled(ehc->classes[dev->devno])) {
1616 dev->class = ehc->classes[dev->devno];
1617
1618 rc = ata_dev_read_id(dev, &dev->class, 1, dev->id);
1619 if (rc == 0)
1620 rc = ata_dev_configure(dev, 1);
1621
1622 if (rc) {
1623 dev->class = ATA_DEV_UNKNOWN;
1624 break;
1625 }
1626
Jeff Garzikba6a1302006-06-22 23:46:10 -04001627 spin_lock_irqsave(ap->lock, flags);
Tejun Heo084fe632006-05-31 18:28:03 +09001628 ap->flags |= ATA_FLAG_SCSI_HOTPLUG;
Jeff Garzikba6a1302006-06-22 23:46:10 -04001629 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo022bdb02006-05-15 20:58:22 +09001630 }
1631 }
1632
Tejun Heo47005f22006-06-19 18:27:23 +09001633 if (rc)
Tejun Heo022bdb02006-05-15 20:58:22 +09001634 *r_failed_dev = dev;
1635
1636 DPRINTK("EXIT\n");
1637 return rc;
1638}
1639
1640static int ata_port_nr_enabled(struct ata_port *ap)
1641{
1642 int i, cnt = 0;
1643
1644 for (i = 0; i < ATA_MAX_DEVICES; i++)
1645 if (ata_dev_enabled(&ap->device[i]))
1646 cnt++;
1647 return cnt;
1648}
1649
Tejun Heo084fe632006-05-31 18:28:03 +09001650static int ata_port_nr_vacant(struct ata_port *ap)
1651{
1652 int i, cnt = 0;
1653
1654 for (i = 0; i < ATA_MAX_DEVICES; i++)
1655 if (ap->device[i].class == ATA_DEV_UNKNOWN)
1656 cnt++;
1657 return cnt;
1658}
1659
1660static int ata_eh_skip_recovery(struct ata_port *ap)
1661{
1662 struct ata_eh_context *ehc = &ap->eh_context;
1663 int i;
1664
1665 if (ap->flags & ATA_FLAG_FROZEN || ata_port_nr_enabled(ap))
1666 return 0;
1667
1668 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
1669 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1670 struct ata_device *dev = &ap->device[i];
1671
1672 if (dev->class == ATA_DEV_UNKNOWN &&
1673 ehc->classes[dev->devno] != ATA_DEV_NONE)
1674 return 0;
1675 }
1676
1677 return 1;
1678}
1679
Tejun Heo022bdb02006-05-15 20:58:22 +09001680/**
1681 * ata_eh_recover - recover host port after error
1682 * @ap: host port to recover
Tejun Heof5914a42006-05-31 18:27:48 +09001683 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09001684 * @softreset: softreset method (can be NULL)
1685 * @hardreset: hardreset method (can be NULL)
1686 * @postreset: postreset method (can be NULL)
1687 *
1688 * This is the alpha and omega, eum and yang, heart and soul of
1689 * libata exception handling. On entry, actions required to
Tejun Heo084fe632006-05-31 18:28:03 +09001690 * recover the port and hotplug requests are recorded in
1691 * eh_context. This function executes all the operations with
1692 * appropriate retrials and fallbacks to resurrect failed
1693 * devices, detach goners and greet newcomers.
Tejun Heo022bdb02006-05-15 20:58:22 +09001694 *
1695 * LOCKING:
1696 * Kernel thread context (may sleep).
1697 *
1698 * RETURNS:
1699 * 0 on success, -errno on failure.
1700 */
Tejun Heof5914a42006-05-31 18:27:48 +09001701static int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
1702 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
Tejun Heo022bdb02006-05-15 20:58:22 +09001703 ata_postreset_fn_t postreset)
1704{
1705 struct ata_eh_context *ehc = &ap->eh_context;
1706 struct ata_device *dev;
1707 int down_xfermask, i, rc;
1708
1709 DPRINTK("ENTER\n");
1710
1711 /* prep for recovery */
1712 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1713 dev = &ap->device[i];
1714
1715 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
Tejun Heo084fe632006-05-31 18:28:03 +09001716
1717 /* process hotplug request */
1718 if (dev->flags & ATA_DFLAG_DETACH)
1719 ata_eh_detach_dev(dev);
1720
1721 if (!ata_dev_enabled(dev) &&
1722 ((ehc->i.probe_mask & (1 << dev->devno)) &&
1723 !(ehc->did_probe_mask & (1 << dev->devno)))) {
1724 ata_eh_detach_dev(dev);
1725 ata_dev_init(dev);
1726 ehc->did_probe_mask |= (1 << dev->devno);
1727 ehc->i.action |= ATA_EH_SOFTRESET;
1728 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001729 }
1730
1731 retry:
1732 down_xfermask = 0;
1733 rc = 0;
1734
Tejun Heoaeb2ecd2006-06-12 14:11:43 +09001735 /* if UNLOADING, finish immediately */
1736 if (ap->flags & ATA_FLAG_UNLOADING)
1737 goto out;
1738
Tejun Heo022bdb02006-05-15 20:58:22 +09001739 /* skip EH if possible. */
Tejun Heo084fe632006-05-31 18:28:03 +09001740 if (ata_eh_skip_recovery(ap))
Tejun Heo022bdb02006-05-15 20:58:22 +09001741 ehc->i.action = 0;
1742
Tejun Heo084fe632006-05-31 18:28:03 +09001743 for (i = 0; i < ATA_MAX_DEVICES; i++)
1744 ehc->classes[i] = ATA_DEV_UNKNOWN;
1745
Tejun Heo022bdb02006-05-15 20:58:22 +09001746 /* reset */
1747 if (ehc->i.action & ATA_EH_RESET_MASK) {
1748 ata_eh_freeze_port(ap);
1749
Tejun Heo084fe632006-05-31 18:28:03 +09001750 rc = ata_eh_reset(ap, ata_port_nr_vacant(ap), prereset,
1751 softreset, hardreset, postreset);
Tejun Heo022bdb02006-05-15 20:58:22 +09001752 if (rc) {
1753 ata_port_printk(ap, KERN_ERR,
1754 "reset failed, giving up\n");
1755 goto out;
1756 }
1757
1758 ata_eh_thaw_port(ap);
1759 }
1760
Tejun Heo084fe632006-05-31 18:28:03 +09001761 /* revalidate existing devices and attach new ones */
1762 rc = ata_eh_revalidate_and_attach(ap, &dev);
Tejun Heo022bdb02006-05-15 20:58:22 +09001763 if (rc)
1764 goto dev_fail;
1765
1766 /* configure transfer mode if the port has been reset */
1767 if (ehc->i.flags & ATA_EHI_DID_RESET) {
1768 rc = ata_set_mode(ap, &dev);
1769 if (rc) {
1770 down_xfermask = 1;
1771 goto dev_fail;
1772 }
1773 }
1774
1775 goto out;
1776
1777 dev_fail:
1778 switch (rc) {
1779 case -ENODEV:
Tejun Heo084fe632006-05-31 18:28:03 +09001780 /* device missing, schedule probing */
1781 ehc->i.probe_mask |= (1 << dev->devno);
Tejun Heo022bdb02006-05-15 20:58:22 +09001782 case -EINVAL:
1783 ehc->tries[dev->devno] = 0;
1784 break;
1785 case -EIO:
1786 sata_down_spd_limit(ap);
1787 default:
1788 ehc->tries[dev->devno]--;
1789 if (down_xfermask &&
1790 ata_down_xfermask_limit(dev, ehc->tries[dev->devno] == 1))
1791 ehc->tries[dev->devno] = 0;
1792 }
1793
Tejun Heo084fe632006-05-31 18:28:03 +09001794 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
1795 /* disable device if it has used up all its chances */
Tejun Heo022bdb02006-05-15 20:58:22 +09001796 ata_dev_disable(dev);
1797
Tejun Heo084fe632006-05-31 18:28:03 +09001798 /* detach if offline */
1799 if (ata_port_offline(ap))
1800 ata_eh_detach_dev(dev);
1801
1802 /* probe if requested */
1803 if ((ehc->i.probe_mask & (1 << dev->devno)) &&
1804 !(ehc->did_probe_mask & (1 << dev->devno))) {
1805 ata_eh_detach_dev(dev);
1806 ata_dev_init(dev);
1807
1808 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
1809 ehc->did_probe_mask |= (1 << dev->devno);
1810 ehc->i.action |= ATA_EH_SOFTRESET;
1811 }
1812 } else {
1813 /* soft didn't work? be haaaaard */
1814 if (ehc->i.flags & ATA_EHI_DID_RESET)
1815 ehc->i.action |= ATA_EH_HARDRESET;
1816 else
1817 ehc->i.action |= ATA_EH_SOFTRESET;
1818 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001819
1820 if (ata_port_nr_enabled(ap)) {
1821 ata_port_printk(ap, KERN_WARNING, "failed to recover some "
1822 "devices, retrying in 5 secs\n");
1823 ssleep(5);
1824 } else {
1825 /* no device left, repeat fast */
1826 msleep(500);
1827 }
1828
1829 goto retry;
1830
1831 out:
1832 if (rc) {
1833 for (i = 0; i < ATA_MAX_DEVICES; i++)
1834 ata_dev_disable(&ap->device[i]);
1835 }
1836
1837 DPRINTK("EXIT, rc=%d\n", rc);
1838 return rc;
1839}
1840
1841/**
1842 * ata_eh_finish - finish up EH
1843 * @ap: host port to finish EH for
1844 *
1845 * Recovery is complete. Clean up EH states and retry or finish
1846 * failed qcs.
1847 *
1848 * LOCKING:
1849 * None.
1850 */
1851static void ata_eh_finish(struct ata_port *ap)
1852{
1853 int tag;
1854
1855 /* retry or finish qcs */
1856 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1857 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1858
1859 if (!(qc->flags & ATA_QCFLAG_FAILED))
1860 continue;
1861
1862 if (qc->err_mask) {
1863 /* FIXME: Once EH migration is complete,
1864 * generate sense data in this function,
1865 * considering both err_mask and tf.
1866 */
1867 if (qc->err_mask & AC_ERR_INVALID)
1868 ata_eh_qc_complete(qc);
1869 else
1870 ata_eh_qc_retry(qc);
1871 } else {
1872 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1873 ata_eh_qc_complete(qc);
1874 } else {
1875 /* feed zero TF to sense generation */
1876 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
1877 ata_eh_qc_retry(qc);
1878 }
1879 }
1880 }
1881}
1882
1883/**
1884 * ata_do_eh - do standard error handling
1885 * @ap: host port to handle error for
Tejun Heof5914a42006-05-31 18:27:48 +09001886 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09001887 * @softreset: softreset method (can be NULL)
1888 * @hardreset: hardreset method (can be NULL)
1889 * @postreset: postreset method (can be NULL)
1890 *
1891 * Perform standard error handling sequence.
1892 *
1893 * LOCKING:
1894 * Kernel thread context (may sleep).
1895 */
Tejun Heof5914a42006-05-31 18:27:48 +09001896void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
1897 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
1898 ata_postreset_fn_t postreset)
Tejun Heo022bdb02006-05-15 20:58:22 +09001899{
Tejun Heo3e706392006-05-31 18:28:11 +09001900 if (!(ap->flags & ATA_FLAG_LOADING)) {
1901 ata_eh_autopsy(ap);
1902 ata_eh_report(ap);
1903 }
1904
Tejun Heof5914a42006-05-31 18:27:48 +09001905 ata_eh_recover(ap, prereset, softreset, hardreset, postreset);
Tejun Heo022bdb02006-05-15 20:58:22 +09001906 ata_eh_finish(ap);
1907}