blob: 1dcb2c13ffa98b050813f2beda7e94cba7adf669 [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 Heo500530f2006-07-03 16:07:27 +090050static void ata_eh_handle_port_suspend(struct ata_port *ap);
51static void ata_eh_handle_port_resume(struct ata_port *ap);
Tejun Heoad9e2762006-05-15 20:58:12 +090052
Tejun Heo0c247c52006-05-15 20:58:19 +090053static void ata_ering_record(struct ata_ering *ering, int is_io,
54 unsigned int err_mask)
55{
56 struct ata_ering_entry *ent;
57
58 WARN_ON(!err_mask);
59
60 ering->cursor++;
61 ering->cursor %= ATA_ERING_SIZE;
62
63 ent = &ering->ring[ering->cursor];
64 ent->is_io = is_io;
65 ent->err_mask = err_mask;
66 ent->timestamp = get_jiffies_64();
67}
68
69static struct ata_ering_entry * ata_ering_top(struct ata_ering *ering)
70{
71 struct ata_ering_entry *ent = &ering->ring[ering->cursor];
72 if (!ent->err_mask)
73 return NULL;
74 return ent;
75}
76
77static int ata_ering_map(struct ata_ering *ering,
78 int (*map_fn)(struct ata_ering_entry *, void *),
79 void *arg)
80{
81 int idx, rc = 0;
82 struct ata_ering_entry *ent;
83
84 idx = ering->cursor;
85 do {
86 ent = &ering->ring[idx];
87 if (!ent->err_mask)
88 break;
89 rc = map_fn(ent, arg);
90 if (rc)
91 break;
92 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
93 } while (idx != ering->cursor);
94
95 return rc;
96}
97
Tejun Heo64f65ca2006-06-24 20:30:18 +090098static unsigned int ata_eh_dev_action(struct ata_device *dev)
99{
100 struct ata_eh_context *ehc = &dev->ap->eh_context;
101
102 return ehc->i.action | ehc->i.dev_action[dev->devno];
103}
104
Tejun Heoaf181c22006-06-24 20:30:18 +0900105static void ata_eh_clear_action(struct ata_device *dev,
106 struct ata_eh_info *ehi, unsigned int action)
107{
108 int i;
109
110 if (!dev) {
111 ehi->action &= ~action;
112 for (i = 0; i < ATA_MAX_DEVICES; i++)
113 ehi->dev_action[i] &= ~action;
114 } else {
115 /* doesn't make sense for port-wide EH actions */
116 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
117
118 /* break ehi->action into ehi->dev_action */
119 if (ehi->action & action) {
120 for (i = 0; i < ATA_MAX_DEVICES; i++)
121 ehi->dev_action[i] |= ehi->action & action;
122 ehi->action &= ~action;
123 }
124
125 /* turn off the specified per-dev action */
126 ehi->dev_action[dev->devno] &= ~action;
127 }
128}
129
Tejun Heoece1d632006-04-02 18:51:53 +0900130/**
131 * ata_scsi_timed_out - SCSI layer time out callback
132 * @cmd: timed out SCSI command
133 *
134 * Handles SCSI layer timeout. We race with normal completion of
135 * the qc for @cmd. If the qc is already gone, we lose and let
136 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
137 * timed out and EH should be invoked. Prevent ata_qc_complete()
138 * from finishing it by setting EH_SCHEDULED and return
139 * EH_NOT_HANDLED.
140 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900141 * TODO: kill this function once old EH is gone.
142 *
Tejun Heoece1d632006-04-02 18:51:53 +0900143 * LOCKING:
144 * Called from timer context
145 *
146 * RETURNS:
147 * EH_HANDLED or EH_NOT_HANDLED
148 */
149enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
150{
151 struct Scsi_Host *host = cmd->device->host;
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400152 struct ata_port *ap = ata_shost_to_port(host);
Tejun Heoece1d632006-04-02 18:51:53 +0900153 unsigned long flags;
154 struct ata_queued_cmd *qc;
Tejun Heoad9e2762006-05-15 20:58:12 +0900155 enum scsi_eh_timer_return ret;
Tejun Heoece1d632006-04-02 18:51:53 +0900156
157 DPRINTK("ENTER\n");
158
Tejun Heoad9e2762006-05-15 20:58:12 +0900159 if (ap->ops->error_handler) {
160 ret = EH_NOT_HANDLED;
161 goto out;
162 }
163
164 ret = EH_HANDLED;
Jeff Garzikba6a1302006-06-22 23:46:10 -0400165 spin_lock_irqsave(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900166 qc = ata_qc_from_tag(ap, ap->active_tag);
167 if (qc) {
168 WARN_ON(qc->scsicmd != cmd);
169 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
170 qc->err_mask |= AC_ERR_TIMEOUT;
171 ret = EH_NOT_HANDLED;
172 }
Jeff Garzikba6a1302006-06-22 23:46:10 -0400173 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900174
Tejun Heoad9e2762006-05-15 20:58:12 +0900175 out:
Tejun Heoece1d632006-04-02 18:51:53 +0900176 DPRINTK("EXIT, ret=%d\n", ret);
177 return ret;
178}
179
180/**
181 * ata_scsi_error - SCSI layer error handler callback
182 * @host: SCSI host on which error occurred
183 *
184 * Handles SCSI-layer-thrown error events.
185 *
186 * LOCKING:
187 * Inherited from SCSI layer (none, can sleep)
188 *
189 * RETURNS:
190 * Zero.
191 */
Jeff Garzik381544b2006-04-11 13:04:39 -0400192void ata_scsi_error(struct Scsi_Host *host)
Tejun Heoece1d632006-04-02 18:51:53 +0900193{
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400194 struct ata_port *ap = ata_shost_to_port(host);
Tejun Heoad9e2762006-05-15 20:58:12 +0900195 int i, repeat_cnt = ATA_EH_MAX_REPEAT;
196 unsigned long flags;
Tejun Heoece1d632006-04-02 18:51:53 +0900197
198 DPRINTK("ENTER\n");
199
Tejun Heoad9e2762006-05-15 20:58:12 +0900200 /* synchronize with port task */
Tejun Heoece1d632006-04-02 18:51:53 +0900201 ata_port_flush_task(ap);
202
Tejun Heoad9e2762006-05-15 20:58:12 +0900203 /* synchronize with host_set lock and sort out timeouts */
Tejun Heoece1d632006-04-02 18:51:53 +0900204
Tejun Heoad9e2762006-05-15 20:58:12 +0900205 /* For new EH, all qcs are finished in one of three ways -
206 * normal completion, error completion, and SCSI timeout.
207 * Both cmpletions can race against SCSI timeout. When normal
208 * completion wins, the qc never reaches EH. When error
209 * completion wins, the qc has ATA_QCFLAG_FAILED set.
210 *
211 * When SCSI timeout wins, things are a bit more complex.
212 * Normal or error completion can occur after the timeout but
213 * before this point. In such cases, both types of
214 * completions are honored. A scmd is determined to have
215 * timed out iff its associated qc is active and not failed.
216 */
217 if (ap->ops->error_handler) {
218 struct scsi_cmnd *scmd, *tmp;
219 int nr_timedout = 0;
Tejun Heoece1d632006-04-02 18:51:53 +0900220
Tejun Heoe30349d2006-07-03 03:02:15 +0900221 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900222
223 list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
224 struct ata_queued_cmd *qc;
225
226 for (i = 0; i < ATA_MAX_QUEUE; i++) {
227 qc = __ata_qc_from_tag(ap, i);
228 if (qc->flags & ATA_QCFLAG_ACTIVE &&
229 qc->scsicmd == scmd)
230 break;
231 }
232
233 if (i < ATA_MAX_QUEUE) {
234 /* the scmd has an associated qc */
235 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
236 /* which hasn't failed yet, timeout */
237 qc->err_mask |= AC_ERR_TIMEOUT;
238 qc->flags |= ATA_QCFLAG_FAILED;
239 nr_timedout++;
240 }
241 } else {
242 /* Normal completion occurred after
243 * SCSI timeout but before this point.
244 * Successfully complete it.
245 */
246 scmd->retries = scmd->allowed;
247 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
248 }
249 }
250
251 /* If we have timed out qcs. They belong to EH from
252 * this point but the state of the controller is
253 * unknown. Freeze the port to make sure the IRQ
254 * handler doesn't diddle with those qcs. This must
255 * be done atomically w.r.t. setting QCFLAG_FAILED.
256 */
257 if (nr_timedout)
258 __ata_port_freeze(ap);
259
Tejun Heoe30349d2006-07-03 03:02:15 +0900260 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900261 } else
Tejun Heoe30349d2006-07-03 03:02:15 +0900262 spin_unlock_wait(ap->lock);
Tejun Heoad9e2762006-05-15 20:58:12 +0900263
264 repeat:
265 /* invoke error handler */
266 if (ap->ops->error_handler) {
Tejun Heo500530f2006-07-03 16:07:27 +0900267 /* process port resume request */
268 ata_eh_handle_port_resume(ap);
269
Tejun Heof3e81b12006-05-15 20:58:21 +0900270 /* fetch & clear EH info */
Tejun Heoe30349d2006-07-03 03:02:15 +0900271 spin_lock_irqsave(ap->lock, flags);
Tejun Heof3e81b12006-05-15 20:58:21 +0900272
273 memset(&ap->eh_context, 0, sizeof(ap->eh_context));
274 ap->eh_context.i = ap->eh_info;
275 memset(&ap->eh_info, 0, sizeof(ap->eh_info));
276
Tejun Heob51e9e52006-06-29 01:29:30 +0900277 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
278 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
Tejun Heof3e81b12006-05-15 20:58:21 +0900279
Tejun Heoe30349d2006-07-03 03:02:15 +0900280 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900281
Tejun Heo500530f2006-07-03 16:07:27 +0900282 /* invoke EH, skip if unloading or suspended */
283 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
Tejun Heo720ba122006-05-31 18:28:13 +0900284 ap->ops->error_handler(ap);
285 else
286 ata_eh_finish(ap);
Tejun Heoad9e2762006-05-15 20:58:12 +0900287
Tejun Heo500530f2006-07-03 16:07:27 +0900288 /* process port suspend request */
289 ata_eh_handle_port_suspend(ap);
290
Tejun Heoad9e2762006-05-15 20:58:12 +0900291 /* Exception might have happend after ->error_handler
292 * recovered the port but before this point. Repeat
293 * EH in such case.
294 */
Tejun Heoe30349d2006-07-03 03:02:15 +0900295 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900296
Tejun Heob51e9e52006-06-29 01:29:30 +0900297 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
Tejun Heoad9e2762006-05-15 20:58:12 +0900298 if (--repeat_cnt) {
299 ata_port_printk(ap, KERN_INFO,
300 "EH pending after completion, "
301 "repeating EH (cnt=%d)\n", repeat_cnt);
Tejun Heoe30349d2006-07-03 03:02:15 +0900302 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900303 goto repeat;
304 }
305 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
306 "tries, giving up\n", ATA_EH_MAX_REPEAT);
307 }
308
Tejun Heof3e81b12006-05-15 20:58:21 +0900309 /* this run is complete, make sure EH info is clear */
310 memset(&ap->eh_info, 0, sizeof(ap->eh_info));
311
Tejun Heoe30349d2006-07-03 03:02:15 +0900312 /* Clear host_eh_scheduled while holding ap->lock such
Tejun Heoad9e2762006-05-15 20:58:12 +0900313 * that if exception occurs after this point but
314 * before EH completion, SCSI midlayer will
315 * re-initiate EH.
316 */
317 host->host_eh_scheduled = 0;
318
Tejun Heoe30349d2006-07-03 03:02:15 +0900319 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900320 } else {
321 WARN_ON(ata_qc_from_tag(ap, ap->active_tag) == NULL);
322 ap->ops->eng_timeout(ap);
323 }
324
325 /* finish or retry handled scmd's and clean up */
Tejun Heoece1d632006-04-02 18:51:53 +0900326 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
327
328 scsi_eh_flush_done_q(&ap->eh_done_q);
329
Tejun Heoad9e2762006-05-15 20:58:12 +0900330 /* clean up */
Tejun Heoe30349d2006-07-03 03:02:15 +0900331 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900332
Tejun Heo1cdaf532006-07-03 16:07:26 +0900333 if (ap->pflags & ATA_PFLAG_LOADING)
Tejun Heob51e9e52006-06-29 01:29:30 +0900334 ap->pflags &= ~ATA_PFLAG_LOADING;
Tejun Heo1cdaf532006-07-03 16:07:26 +0900335 else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
336 queue_work(ata_aux_wq, &ap->hotplug_task);
337
338 if (ap->pflags & ATA_PFLAG_RECOVERED)
339 ata_port_printk(ap, KERN_INFO, "EH complete\n");
Tejun Heo580b21022006-05-31 18:28:05 +0900340
Tejun Heob51e9e52006-06-29 01:29:30 +0900341 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
Tejun Heoad9e2762006-05-15 20:58:12 +0900342
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900343 /* tell wait_eh that we're done */
Tejun Heob51e9e52006-06-29 01:29:30 +0900344 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900345 wake_up_all(&ap->eh_wait_q);
346
Tejun Heoe30349d2006-07-03 03:02:15 +0900347 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900348
Tejun Heoece1d632006-04-02 18:51:53 +0900349 DPRINTK("EXIT\n");
Tejun Heoece1d632006-04-02 18:51:53 +0900350}
351
352/**
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900353 * ata_port_wait_eh - Wait for the currently pending EH to complete
354 * @ap: Port to wait EH for
355 *
356 * Wait until the currently pending EH is complete.
357 *
358 * LOCKING:
359 * Kernel thread context (may sleep).
360 */
361void ata_port_wait_eh(struct ata_port *ap)
362{
363 unsigned long flags;
364 DEFINE_WAIT(wait);
365
366 retry:
Jeff Garzikba6a1302006-06-22 23:46:10 -0400367 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900368
Tejun Heob51e9e52006-06-29 01:29:30 +0900369 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900370 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
Jeff Garzikba6a1302006-06-22 23:46:10 -0400371 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900372 schedule();
Jeff Garzikba6a1302006-06-22 23:46:10 -0400373 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900374 }
Tejun Heo0a1b6222006-06-11 11:01:38 +0900375 finish_wait(&ap->eh_wait_q, &wait);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900376
Jeff Garzikba6a1302006-06-22 23:46:10 -0400377 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900378
379 /* make sure SCSI EH is complete */
380 if (scsi_host_in_recovery(ap->host)) {
381 msleep(10);
382 goto retry;
383 }
384}
385
386/**
Tejun Heoece1d632006-04-02 18:51:53 +0900387 * ata_qc_timeout - Handle timeout of queued command
388 * @qc: Command that timed out
389 *
390 * Some part of the kernel (currently, only the SCSI layer)
391 * has noticed that the active command on port @ap has not
392 * completed after a specified length of time. Handle this
393 * condition by disabling DMA (if necessary) and completing
394 * transactions, with error if necessary.
395 *
396 * This also handles the case of the "lost interrupt", where
397 * for some reason (possibly hardware bug, possibly driver bug)
398 * an interrupt was not delivered to the driver, even though the
399 * transaction completed successfully.
400 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900401 * TODO: kill this function once old EH is gone.
402 *
Tejun Heoece1d632006-04-02 18:51:53 +0900403 * LOCKING:
404 * Inherited from SCSI layer (none, can sleep)
405 */
406static void ata_qc_timeout(struct ata_queued_cmd *qc)
407{
408 struct ata_port *ap = qc->ap;
Tejun Heoece1d632006-04-02 18:51:53 +0900409 u8 host_stat = 0, drv_stat;
410 unsigned long flags;
411
412 DPRINTK("ENTER\n");
413
414 ap->hsm_task_state = HSM_ST_IDLE;
415
Jeff Garzikba6a1302006-06-22 23:46:10 -0400416 spin_lock_irqsave(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900417
418 switch (qc->tf.protocol) {
419
420 case ATA_PROT_DMA:
421 case ATA_PROT_ATAPI_DMA:
422 host_stat = ap->ops->bmdma_status(ap);
423
424 /* before we do anything else, clear DMA-Start bit */
425 ap->ops->bmdma_stop(qc);
426
427 /* fall through */
428
429 default:
430 ata_altstatus(ap);
431 drv_stat = ata_chk_status(ap);
432
433 /* ack bmdma irq events */
434 ap->ops->irq_clear(ap);
435
Tejun Heof15a1da2006-05-15 20:57:56 +0900436 ata_dev_printk(qc->dev, KERN_ERR, "command 0x%x timeout, "
437 "stat 0x%x host_stat 0x%x\n",
438 qc->tf.command, drv_stat, host_stat);
Tejun Heoece1d632006-04-02 18:51:53 +0900439
440 /* complete taskfile transaction */
Jeff Garzikc13b56a2006-04-02 10:34:24 -0400441 qc->err_mask |= AC_ERR_TIMEOUT;
Tejun Heoece1d632006-04-02 18:51:53 +0900442 break;
443 }
444
Jeff Garzikba6a1302006-06-22 23:46:10 -0400445 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900446
447 ata_eh_qc_complete(qc);
448
449 DPRINTK("EXIT\n");
450}
451
452/**
453 * ata_eng_timeout - Handle timeout of queued command
454 * @ap: Port on which timed-out command is active
455 *
456 * Some part of the kernel (currently, only the SCSI layer)
457 * has noticed that the active command on port @ap has not
458 * completed after a specified length of time. Handle this
459 * condition by disabling DMA (if necessary) and completing
460 * transactions, with error if necessary.
461 *
462 * This also handles the case of the "lost interrupt", where
463 * for some reason (possibly hardware bug, possibly driver bug)
464 * an interrupt was not delivered to the driver, even though the
465 * transaction completed successfully.
466 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900467 * TODO: kill this function once old EH is gone.
468 *
Tejun Heoece1d632006-04-02 18:51:53 +0900469 * LOCKING:
470 * Inherited from SCSI layer (none, can sleep)
471 */
472void ata_eng_timeout(struct ata_port *ap)
473{
474 DPRINTK("ENTER\n");
475
476 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
477
478 DPRINTK("EXIT\n");
479}
480
Tejun Heof686bcb2006-05-15 20:58:05 +0900481/**
482 * ata_qc_schedule_eh - schedule qc for error handling
483 * @qc: command to schedule error handling for
484 *
485 * Schedule error handling for @qc. EH will kick in as soon as
486 * other commands are drained.
487 *
488 * LOCKING:
489 * spin_lock_irqsave(host_set lock)
490 */
491void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
492{
493 struct ata_port *ap = qc->ap;
494
495 WARN_ON(!ap->ops->error_handler);
496
497 qc->flags |= ATA_QCFLAG_FAILED;
Tejun Heob51e9e52006-06-29 01:29:30 +0900498 qc->ap->pflags |= ATA_PFLAG_EH_PENDING;
Tejun Heof686bcb2006-05-15 20:58:05 +0900499
500 /* The following will fail if timeout has already expired.
501 * ata_scsi_error() takes care of such scmds on EH entry.
502 * Note that ATA_QCFLAG_FAILED is unconditionally set after
503 * this function completes.
504 */
505 scsi_req_abort_cmd(qc->scsicmd);
506}
507
Tejun Heo7b70fc02006-05-15 20:58:07 +0900508/**
509 * ata_port_schedule_eh - schedule error handling without a qc
510 * @ap: ATA port to schedule EH for
511 *
512 * Schedule error handling for @ap. EH will kick in as soon as
513 * all commands are drained.
514 *
515 * LOCKING:
516 * spin_lock_irqsave(host_set lock)
517 */
518void ata_port_schedule_eh(struct ata_port *ap)
519{
520 WARN_ON(!ap->ops->error_handler);
521
Tejun Heob51e9e52006-06-29 01:29:30 +0900522 ap->pflags |= ATA_PFLAG_EH_PENDING;
Tejun Heof8bbfc22006-05-19 21:07:05 +0900523 scsi_schedule_eh(ap->host);
Tejun Heo7b70fc02006-05-15 20:58:07 +0900524
525 DPRINTK("port EH scheduled\n");
526}
527
528/**
529 * ata_port_abort - abort all qc's on the port
530 * @ap: ATA port to abort qc's for
531 *
532 * Abort all active qc's of @ap and schedule EH.
533 *
534 * LOCKING:
535 * spin_lock_irqsave(host_set lock)
536 *
537 * RETURNS:
538 * Number of aborted qc's.
539 */
540int ata_port_abort(struct ata_port *ap)
541{
542 int tag, nr_aborted = 0;
543
544 WARN_ON(!ap->ops->error_handler);
545
546 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
547 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
548
549 if (qc) {
550 qc->flags |= ATA_QCFLAG_FAILED;
551 ata_qc_complete(qc);
552 nr_aborted++;
553 }
554 }
555
556 if (!nr_aborted)
557 ata_port_schedule_eh(ap);
558
559 return nr_aborted;
560}
561
Tejun Heoe3180492006-05-15 20:58:09 +0900562/**
563 * __ata_port_freeze - freeze port
564 * @ap: ATA port to freeze
565 *
566 * This function is called when HSM violation or some other
567 * condition disrupts normal operation of the port. Frozen port
568 * is not allowed to perform any operation until the port is
569 * thawed, which usually follows a successful reset.
570 *
571 * ap->ops->freeze() callback can be used for freezing the port
572 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
573 * port cannot be frozen hardware-wise, the interrupt handler
574 * must ack and clear interrupts unconditionally while the port
575 * is frozen.
576 *
577 * LOCKING:
578 * spin_lock_irqsave(host_set lock)
579 */
580static void __ata_port_freeze(struct ata_port *ap)
581{
582 WARN_ON(!ap->ops->error_handler);
583
584 if (ap->ops->freeze)
585 ap->ops->freeze(ap);
586
Tejun Heob51e9e52006-06-29 01:29:30 +0900587 ap->pflags |= ATA_PFLAG_FROZEN;
Tejun Heoe3180492006-05-15 20:58:09 +0900588
589 DPRINTK("ata%u port frozen\n", ap->id);
590}
591
592/**
593 * ata_port_freeze - abort & freeze port
594 * @ap: ATA port to freeze
595 *
596 * Abort and freeze @ap.
597 *
598 * LOCKING:
599 * spin_lock_irqsave(host_set lock)
600 *
601 * RETURNS:
602 * Number of aborted commands.
603 */
604int ata_port_freeze(struct ata_port *ap)
605{
606 int nr_aborted;
607
608 WARN_ON(!ap->ops->error_handler);
609
610 nr_aborted = ata_port_abort(ap);
611 __ata_port_freeze(ap);
612
613 return nr_aborted;
614}
615
616/**
617 * ata_eh_freeze_port - EH helper to freeze port
618 * @ap: ATA port to freeze
619 *
620 * Freeze @ap.
621 *
622 * LOCKING:
623 * None.
624 */
625void ata_eh_freeze_port(struct ata_port *ap)
626{
627 unsigned long flags;
628
629 if (!ap->ops->error_handler)
630 return;
631
Jeff Garzikba6a1302006-06-22 23:46:10 -0400632 spin_lock_irqsave(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900633 __ata_port_freeze(ap);
Jeff Garzikba6a1302006-06-22 23:46:10 -0400634 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900635}
636
637/**
638 * ata_port_thaw_port - EH helper to thaw port
639 * @ap: ATA port to thaw
640 *
641 * Thaw frozen port @ap.
642 *
643 * LOCKING:
644 * None.
645 */
646void ata_eh_thaw_port(struct ata_port *ap)
647{
648 unsigned long flags;
649
650 if (!ap->ops->error_handler)
651 return;
652
Jeff Garzikba6a1302006-06-22 23:46:10 -0400653 spin_lock_irqsave(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900654
Tejun Heob51e9e52006-06-29 01:29:30 +0900655 ap->pflags &= ~ATA_PFLAG_FROZEN;
Tejun Heoe3180492006-05-15 20:58:09 +0900656
657 if (ap->ops->thaw)
658 ap->ops->thaw(ap);
659
Jeff Garzikba6a1302006-06-22 23:46:10 -0400660 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900661
662 DPRINTK("ata%u port thawed\n", ap->id);
663}
664
Tejun Heoece1d632006-04-02 18:51:53 +0900665static void ata_eh_scsidone(struct scsi_cmnd *scmd)
666{
667 /* nada */
668}
669
670static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
671{
672 struct ata_port *ap = qc->ap;
673 struct scsi_cmnd *scmd = qc->scsicmd;
674 unsigned long flags;
675
Jeff Garzikba6a1302006-06-22 23:46:10 -0400676 spin_lock_irqsave(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900677 qc->scsidone = ata_eh_scsidone;
678 __ata_qc_complete(qc);
679 WARN_ON(ata_tag_valid(qc->tag));
Jeff Garzikba6a1302006-06-22 23:46:10 -0400680 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900681
682 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
683}
684
685/**
686 * ata_eh_qc_complete - Complete an active ATA command from EH
687 * @qc: Command to complete
688 *
689 * Indicate to the mid and upper layers that an ATA command has
690 * completed. To be used from EH.
691 */
692void ata_eh_qc_complete(struct ata_queued_cmd *qc)
693{
694 struct scsi_cmnd *scmd = qc->scsicmd;
695 scmd->retries = scmd->allowed;
696 __ata_eh_qc_complete(qc);
697}
698
699/**
700 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
701 * @qc: Command to retry
702 *
703 * Indicate to the mid and upper layers that an ATA command
704 * should be retried. To be used from EH.
705 *
706 * SCSI midlayer limits the number of retries to scmd->allowed.
707 * scmd->retries is decremented for commands which get retried
708 * due to unrelated failures (qc->err_mask is zero).
709 */
710void ata_eh_qc_retry(struct ata_queued_cmd *qc)
711{
712 struct scsi_cmnd *scmd = qc->scsicmd;
713 if (!qc->err_mask && scmd->retries)
714 scmd->retries--;
715 __ata_eh_qc_complete(qc);
716}
Tejun Heo022bdb02006-05-15 20:58:22 +0900717
718/**
Tejun Heo0ea035a2006-05-31 18:28:01 +0900719 * ata_eh_detach_dev - detach ATA device
720 * @dev: ATA device to detach
721 *
722 * Detach @dev.
723 *
724 * LOCKING:
725 * None.
726 */
727static void ata_eh_detach_dev(struct ata_device *dev)
728{
729 struct ata_port *ap = dev->ap;
730 unsigned long flags;
731
732 ata_dev_disable(dev);
733
Jeff Garzikba6a1302006-06-22 23:46:10 -0400734 spin_lock_irqsave(ap->lock, flags);
Tejun Heo0ea035a2006-05-31 18:28:01 +0900735
736 dev->flags &= ~ATA_DFLAG_DETACH;
737
738 if (ata_scsi_offline_dev(dev)) {
739 dev->flags |= ATA_DFLAG_DETACHED;
Tejun Heob51e9e52006-06-29 01:29:30 +0900740 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
Tejun Heo0ea035a2006-05-31 18:28:01 +0900741 }
742
Tejun Heobeb07c12006-06-24 20:30:19 +0900743 /* clear per-dev EH actions */
744 ata_eh_clear_action(dev, &ap->eh_info, ATA_EH_PERDEV_MASK);
745 ata_eh_clear_action(dev, &ap->eh_context.i, ATA_EH_PERDEV_MASK);
746
Jeff Garzikba6a1302006-06-22 23:46:10 -0400747 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo0ea035a2006-05-31 18:28:01 +0900748}
749
750/**
Tejun Heo022bdb02006-05-15 20:58:22 +0900751 * ata_eh_about_to_do - about to perform eh_action
752 * @ap: target ATA port
Tejun Heo47005f22006-06-19 18:27:23 +0900753 * @dev: target ATA dev for per-dev action (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +0900754 * @action: action about to be performed
755 *
756 * Called just before performing EH actions to clear related bits
757 * in @ap->eh_info such that eh actions are not unnecessarily
758 * repeated.
759 *
760 * LOCKING:
761 * None.
762 */
Tejun Heo47005f22006-06-19 18:27:23 +0900763static void ata_eh_about_to_do(struct ata_port *ap, struct ata_device *dev,
764 unsigned int action)
Tejun Heo022bdb02006-05-15 20:58:22 +0900765{
766 unsigned long flags;
767
Jeff Garzikba6a1302006-06-22 23:46:10 -0400768 spin_lock_irqsave(ap->lock, flags);
Tejun Heo1cdaf532006-07-03 16:07:26 +0900769
Tejun Heo47005f22006-06-19 18:27:23 +0900770 ata_eh_clear_action(dev, &ap->eh_info, action);
Tejun Heo1cdaf532006-07-03 16:07:26 +0900771
772 if (!(ap->eh_context.i.flags & ATA_EHI_QUIET))
773 ap->pflags |= ATA_PFLAG_RECOVERED;
774
Jeff Garzikba6a1302006-06-22 23:46:10 -0400775 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo022bdb02006-05-15 20:58:22 +0900776}
777
778/**
Tejun Heo47005f22006-06-19 18:27:23 +0900779 * ata_eh_done - EH action complete
780 * @ap: target ATA port
781 * @dev: target ATA dev for per-dev action (can be NULL)
782 * @action: action just completed
783 *
784 * Called right after performing EH actions to clear related bits
785 * in @ap->eh_context.
786 *
787 * LOCKING:
788 * None.
789 */
790static void ata_eh_done(struct ata_port *ap, struct ata_device *dev,
791 unsigned int action)
792{
793 ata_eh_clear_action(dev, &ap->eh_context.i, action);
794}
795
796/**
Tejun Heo022bdb02006-05-15 20:58:22 +0900797 * ata_err_string - convert err_mask to descriptive string
798 * @err_mask: error mask to convert to string
799 *
800 * Convert @err_mask to descriptive string. Errors are
801 * prioritized according to severity and only the most severe
802 * error is reported.
803 *
804 * LOCKING:
805 * None.
806 *
807 * RETURNS:
808 * Descriptive string for @err_mask
809 */
810static const char * ata_err_string(unsigned int err_mask)
811{
812 if (err_mask & AC_ERR_HOST_BUS)
813 return "host bus error";
814 if (err_mask & AC_ERR_ATA_BUS)
815 return "ATA bus error";
816 if (err_mask & AC_ERR_TIMEOUT)
817 return "timeout";
818 if (err_mask & AC_ERR_HSM)
819 return "HSM violation";
820 if (err_mask & AC_ERR_SYSTEM)
821 return "internal error";
822 if (err_mask & AC_ERR_MEDIA)
823 return "media error";
824 if (err_mask & AC_ERR_INVALID)
825 return "invalid argument";
826 if (err_mask & AC_ERR_DEV)
827 return "device error";
828 return "unknown error";
829}
830
831/**
Tejun Heoe8ee8452006-05-15 21:03:46 +0900832 * ata_read_log_page - read a specific log page
833 * @dev: target device
834 * @page: page to read
835 * @buf: buffer to store read page
836 * @sectors: number of sectors to read
837 *
838 * Read log page using READ_LOG_EXT command.
839 *
840 * LOCKING:
841 * Kernel thread context (may sleep).
842 *
843 * RETURNS:
844 * 0 on success, AC_ERR_* mask otherwise.
845 */
846static unsigned int ata_read_log_page(struct ata_device *dev,
847 u8 page, void *buf, unsigned int sectors)
848{
849 struct ata_taskfile tf;
850 unsigned int err_mask;
851
852 DPRINTK("read log page - page %d\n", page);
853
854 ata_tf_init(dev, &tf);
855 tf.command = ATA_CMD_READ_LOG_EXT;
856 tf.lbal = page;
857 tf.nsect = sectors;
858 tf.hob_nsect = sectors >> 8;
859 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
860 tf.protocol = ATA_PROT_PIO;
861
862 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
863 buf, sectors * ATA_SECT_SIZE);
864
865 DPRINTK("EXIT, err_mask=%x\n", err_mask);
866 return err_mask;
867}
868
869/**
870 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
871 * @dev: Device to read log page 10h from
872 * @tag: Resulting tag of the failed command
873 * @tf: Resulting taskfile registers of the failed command
874 *
875 * Read log page 10h to obtain NCQ error details and clear error
876 * condition.
877 *
878 * LOCKING:
879 * Kernel thread context (may sleep).
880 *
881 * RETURNS:
882 * 0 on success, -errno otherwise.
883 */
884static int ata_eh_read_log_10h(struct ata_device *dev,
885 int *tag, struct ata_taskfile *tf)
886{
887 u8 *buf = dev->ap->sector_buf;
888 unsigned int err_mask;
889 u8 csum;
890 int i;
891
892 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
893 if (err_mask)
894 return -EIO;
895
896 csum = 0;
897 for (i = 0; i < ATA_SECT_SIZE; i++)
898 csum += buf[i];
899 if (csum)
900 ata_dev_printk(dev, KERN_WARNING,
901 "invalid checksum 0x%x on log page 10h\n", csum);
902
903 if (buf[0] & 0x80)
904 return -ENOENT;
905
906 *tag = buf[0] & 0x1f;
907
908 tf->command = buf[2];
909 tf->feature = buf[3];
910 tf->lbal = buf[4];
911 tf->lbam = buf[5];
912 tf->lbah = buf[6];
913 tf->device = buf[7];
914 tf->hob_lbal = buf[8];
915 tf->hob_lbam = buf[9];
916 tf->hob_lbah = buf[10];
917 tf->nsect = buf[12];
918 tf->hob_nsect = buf[13];
919
920 return 0;
921}
922
923/**
Tejun Heo022bdb02006-05-15 20:58:22 +0900924 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
925 * @dev: device to perform REQUEST_SENSE to
926 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
927 *
928 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
929 * SENSE. This function is EH helper.
930 *
931 * LOCKING:
932 * Kernel thread context (may sleep).
933 *
934 * RETURNS:
935 * 0 on success, AC_ERR_* mask on failure
936 */
937static unsigned int atapi_eh_request_sense(struct ata_device *dev,
938 unsigned char *sense_buf)
939{
940 struct ata_port *ap = dev->ap;
941 struct ata_taskfile tf;
942 u8 cdb[ATAPI_CDB_LEN];
943
944 DPRINTK("ATAPI request sense\n");
945
946 ata_tf_init(dev, &tf);
947
948 /* FIXME: is this needed? */
949 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
950
951 /* XXX: why tf_read here? */
952 ap->ops->tf_read(ap, &tf);
953
954 /* fill these in, for the case where they are -not- overwritten */
955 sense_buf[0] = 0x70;
956 sense_buf[2] = tf.feature >> 4;
957
958 memset(cdb, 0, ATAPI_CDB_LEN);
959 cdb[0] = REQUEST_SENSE;
960 cdb[4] = SCSI_SENSE_BUFFERSIZE;
961
962 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
963 tf.command = ATA_CMD_PACKET;
964
965 /* is it pointless to prefer PIO for "safety reasons"? */
966 if (ap->flags & ATA_FLAG_PIO_DMA) {
967 tf.protocol = ATA_PROT_ATAPI_DMA;
968 tf.feature |= ATAPI_PKT_DMA;
969 } else {
970 tf.protocol = ATA_PROT_ATAPI;
971 tf.lbam = (8 * 1024) & 0xff;
972 tf.lbah = (8 * 1024) >> 8;
973 }
974
975 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
976 sense_buf, SCSI_SENSE_BUFFERSIZE);
977}
978
979/**
980 * ata_eh_analyze_serror - analyze SError for a failed port
981 * @ap: ATA port to analyze SError for
982 *
983 * Analyze SError if available and further determine cause of
984 * failure.
985 *
986 * LOCKING:
987 * None.
988 */
989static void ata_eh_analyze_serror(struct ata_port *ap)
990{
991 struct ata_eh_context *ehc = &ap->eh_context;
992 u32 serror = ehc->i.serror;
993 unsigned int err_mask = 0, action = 0;
994
995 if (serror & SERR_PERSISTENT) {
996 err_mask |= AC_ERR_ATA_BUS;
997 action |= ATA_EH_HARDRESET;
998 }
999 if (serror &
1000 (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
1001 err_mask |= AC_ERR_ATA_BUS;
1002 action |= ATA_EH_SOFTRESET;
1003 }
1004 if (serror & SERR_PROTOCOL) {
1005 err_mask |= AC_ERR_HSM;
1006 action |= ATA_EH_SOFTRESET;
1007 }
1008 if (serror & SERR_INTERNAL) {
1009 err_mask |= AC_ERR_SYSTEM;
1010 action |= ATA_EH_SOFTRESET;
1011 }
Tejun Heo084fe632006-05-31 18:28:03 +09001012 if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG))
1013 ata_ehi_hotplugged(&ehc->i);
Tejun Heo022bdb02006-05-15 20:58:22 +09001014
1015 ehc->i.err_mask |= err_mask;
1016 ehc->i.action |= action;
1017}
1018
1019/**
Tejun Heoe8ee8452006-05-15 21:03:46 +09001020 * ata_eh_analyze_ncq_error - analyze NCQ error
1021 * @ap: ATA port to analyze NCQ error for
1022 *
1023 * Read log page 10h, determine the offending qc and acquire
1024 * error status TF. For NCQ device errors, all LLDDs have to do
1025 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1026 * care of the rest.
1027 *
1028 * LOCKING:
1029 * Kernel thread context (may sleep).
1030 */
1031static void ata_eh_analyze_ncq_error(struct ata_port *ap)
1032{
1033 struct ata_eh_context *ehc = &ap->eh_context;
1034 struct ata_device *dev = ap->device;
1035 struct ata_queued_cmd *qc;
1036 struct ata_taskfile tf;
1037 int tag, rc;
1038
1039 /* if frozen, we can't do much */
Tejun Heob51e9e52006-06-29 01:29:30 +09001040 if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heoe8ee8452006-05-15 21:03:46 +09001041 return;
1042
1043 /* is it NCQ device error? */
1044 if (!ap->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1045 return;
1046
1047 /* has LLDD analyzed already? */
1048 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1049 qc = __ata_qc_from_tag(ap, tag);
1050
1051 if (!(qc->flags & ATA_QCFLAG_FAILED))
1052 continue;
1053
1054 if (qc->err_mask)
1055 return;
1056 }
1057
1058 /* okay, this error is ours */
1059 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1060 if (rc) {
1061 ata_port_printk(ap, KERN_ERR, "failed to read log page 10h "
1062 "(errno=%d)\n", rc);
1063 return;
1064 }
1065
1066 if (!(ap->sactive & (1 << tag))) {
1067 ata_port_printk(ap, KERN_ERR, "log page 10h reported "
1068 "inactive tag %d\n", tag);
1069 return;
1070 }
1071
1072 /* we've got the perpetrator, condemn it */
1073 qc = __ata_qc_from_tag(ap, tag);
1074 memcpy(&qc->result_tf, &tf, sizeof(tf));
1075 qc->err_mask |= AC_ERR_DEV;
1076 ehc->i.err_mask &= ~AC_ERR_DEV;
1077}
1078
1079/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001080 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1081 * @qc: qc to analyze
1082 * @tf: Taskfile registers to analyze
1083 *
1084 * Analyze taskfile of @qc and further determine cause of
1085 * failure. This function also requests ATAPI sense data if
1086 * avaliable.
1087 *
1088 * LOCKING:
1089 * Kernel thread context (may sleep).
1090 *
1091 * RETURNS:
1092 * Determined recovery action
1093 */
1094static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1095 const struct ata_taskfile *tf)
1096{
1097 unsigned int tmp, action = 0;
1098 u8 stat = tf->command, err = tf->feature;
1099
1100 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1101 qc->err_mask |= AC_ERR_HSM;
1102 return ATA_EH_SOFTRESET;
1103 }
1104
1105 if (!(qc->err_mask & AC_ERR_DEV))
1106 return 0;
1107
1108 switch (qc->dev->class) {
1109 case ATA_DEV_ATA:
1110 if (err & ATA_ICRC)
1111 qc->err_mask |= AC_ERR_ATA_BUS;
1112 if (err & ATA_UNC)
1113 qc->err_mask |= AC_ERR_MEDIA;
1114 if (err & ATA_IDNF)
1115 qc->err_mask |= AC_ERR_INVALID;
1116 break;
1117
1118 case ATA_DEV_ATAPI:
1119 tmp = atapi_eh_request_sense(qc->dev,
1120 qc->scsicmd->sense_buffer);
1121 if (!tmp) {
1122 /* ATA_QCFLAG_SENSE_VALID is used to tell
1123 * atapi_qc_complete() that sense data is
1124 * already valid.
1125 *
1126 * TODO: interpret sense data and set
1127 * appropriate err_mask.
1128 */
1129 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1130 } else
1131 qc->err_mask |= tmp;
1132 }
1133
1134 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1135 action |= ATA_EH_SOFTRESET;
1136
1137 return action;
1138}
1139
1140static int ata_eh_categorize_ering_entry(struct ata_ering_entry *ent)
1141{
1142 if (ent->err_mask & (AC_ERR_ATA_BUS | AC_ERR_TIMEOUT))
1143 return 1;
1144
1145 if (ent->is_io) {
1146 if (ent->err_mask & AC_ERR_HSM)
1147 return 1;
1148 if ((ent->err_mask &
1149 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1150 return 2;
1151 }
1152
1153 return 0;
1154}
1155
1156struct speed_down_needed_arg {
1157 u64 since;
1158 int nr_errors[3];
1159};
1160
1161static int speed_down_needed_cb(struct ata_ering_entry *ent, void *void_arg)
1162{
1163 struct speed_down_needed_arg *arg = void_arg;
1164
1165 if (ent->timestamp < arg->since)
1166 return -1;
1167
1168 arg->nr_errors[ata_eh_categorize_ering_entry(ent)]++;
1169 return 0;
1170}
1171
1172/**
1173 * ata_eh_speed_down_needed - Determine wheter speed down is necessary
1174 * @dev: Device of interest
1175 *
1176 * This function examines error ring of @dev and determines
1177 * whether speed down is necessary. Speed down is necessary if
1178 * there have been more than 3 of Cat-1 errors or 10 of Cat-2
1179 * errors during last 15 minutes.
1180 *
1181 * Cat-1 errors are ATA_BUS, TIMEOUT for any command and HSM
1182 * violation for known supported commands.
1183 *
1184 * Cat-2 errors are unclassified DEV error for known supported
1185 * command.
1186 *
1187 * LOCKING:
1188 * Inherited from caller.
1189 *
1190 * RETURNS:
1191 * 1 if speed down is necessary, 0 otherwise
1192 */
1193static int ata_eh_speed_down_needed(struct ata_device *dev)
1194{
1195 const u64 interval = 15LLU * 60 * HZ;
1196 static const int err_limits[3] = { -1, 3, 10 };
1197 struct speed_down_needed_arg arg;
1198 struct ata_ering_entry *ent;
1199 int err_cat;
1200 u64 j64;
1201
1202 ent = ata_ering_top(&dev->ering);
1203 if (!ent)
1204 return 0;
1205
1206 err_cat = ata_eh_categorize_ering_entry(ent);
1207 if (err_cat == 0)
1208 return 0;
1209
1210 memset(&arg, 0, sizeof(arg));
1211
1212 j64 = get_jiffies_64();
1213 if (j64 >= interval)
1214 arg.since = j64 - interval;
1215 else
1216 arg.since = 0;
1217
1218 ata_ering_map(&dev->ering, speed_down_needed_cb, &arg);
1219
1220 return arg.nr_errors[err_cat] > err_limits[err_cat];
1221}
1222
1223/**
1224 * ata_eh_speed_down - record error and speed down if necessary
1225 * @dev: Failed device
1226 * @is_io: Did the device fail during normal IO?
1227 * @err_mask: err_mask of the error
1228 *
1229 * Record error and examine error history to determine whether
1230 * adjusting transmission speed is necessary. It also sets
1231 * transmission limits appropriately if such adjustment is
1232 * necessary.
1233 *
1234 * LOCKING:
1235 * Kernel thread context (may sleep).
1236 *
1237 * RETURNS:
1238 * 0 on success, -errno otherwise
1239 */
1240static int ata_eh_speed_down(struct ata_device *dev, int is_io,
1241 unsigned int err_mask)
1242{
1243 if (!err_mask)
1244 return 0;
1245
1246 /* record error and determine whether speed down is necessary */
1247 ata_ering_record(&dev->ering, is_io, err_mask);
1248
1249 if (!ata_eh_speed_down_needed(dev))
1250 return 0;
1251
1252 /* speed down SATA link speed if possible */
1253 if (sata_down_spd_limit(dev->ap) == 0)
1254 return ATA_EH_HARDRESET;
1255
1256 /* lower transfer mode */
1257 if (ata_down_xfermask_limit(dev, 0) == 0)
1258 return ATA_EH_SOFTRESET;
1259
1260 ata_dev_printk(dev, KERN_ERR,
1261 "speed down requested but no transfer mode left\n");
1262 return 0;
1263}
1264
1265/**
1266 * ata_eh_autopsy - analyze error and determine recovery action
1267 * @ap: ATA port to perform autopsy on
1268 *
1269 * Analyze why @ap failed and determine which recovery action is
1270 * needed. This function also sets more detailed AC_ERR_* values
1271 * and fills sense data for ATAPI CHECK SENSE.
1272 *
1273 * LOCKING:
1274 * Kernel thread context (may sleep).
1275 */
1276static void ata_eh_autopsy(struct ata_port *ap)
1277{
1278 struct ata_eh_context *ehc = &ap->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001279 unsigned int all_err_mask = 0;
1280 int tag, is_io = 0;
1281 u32 serror;
1282 int rc;
1283
1284 DPRINTK("ENTER\n");
1285
Tejun Heo1cdaf532006-07-03 16:07:26 +09001286 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1287 return;
1288
Tejun Heo022bdb02006-05-15 20:58:22 +09001289 /* obtain and analyze SError */
1290 rc = sata_scr_read(ap, SCR_ERROR, &serror);
1291 if (rc == 0) {
1292 ehc->i.serror |= serror;
1293 ata_eh_analyze_serror(ap);
1294 } else if (rc != -EOPNOTSUPP)
Tejun Heo4528e4d2006-07-08 20:17:26 +09001295 ehc->i.action |= ATA_EH_HARDRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001296
Tejun Heoe8ee8452006-05-15 21:03:46 +09001297 /* analyze NCQ failure */
1298 ata_eh_analyze_ncq_error(ap);
1299
Tejun Heo022bdb02006-05-15 20:58:22 +09001300 /* any real error trumps AC_ERR_OTHER */
1301 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1302 ehc->i.err_mask &= ~AC_ERR_OTHER;
1303
1304 all_err_mask |= ehc->i.err_mask;
1305
1306 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1307 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1308
1309 if (!(qc->flags & ATA_QCFLAG_FAILED))
1310 continue;
1311
1312 /* inherit upper level err_mask */
1313 qc->err_mask |= ehc->i.err_mask;
1314
Tejun Heo022bdb02006-05-15 20:58:22 +09001315 /* analyze TF */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001316 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
Tejun Heo022bdb02006-05-15 20:58:22 +09001317
1318 /* DEV errors are probably spurious in case of ATA_BUS error */
1319 if (qc->err_mask & AC_ERR_ATA_BUS)
1320 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1321 AC_ERR_INVALID);
1322
1323 /* any real error trumps unknown error */
1324 if (qc->err_mask & ~AC_ERR_OTHER)
1325 qc->err_mask &= ~AC_ERR_OTHER;
1326
1327 /* SENSE_VALID trumps dev/unknown error and revalidation */
1328 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1329 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
Tejun Heo4528e4d2006-07-08 20:17:26 +09001330 ehc->i.action &= ~ATA_EH_REVALIDATE;
Tejun Heo022bdb02006-05-15 20:58:22 +09001331 }
1332
1333 /* accumulate error info */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001334 ehc->i.dev = qc->dev;
Tejun Heo022bdb02006-05-15 20:58:22 +09001335 all_err_mask |= qc->err_mask;
1336 if (qc->flags & ATA_QCFLAG_IO)
1337 is_io = 1;
1338 }
1339
Tejun Heoa20f33f2006-05-16 12:58:24 +09001340 /* enforce default EH actions */
Tejun Heob51e9e52006-06-29 01:29:30 +09001341 if (ap->pflags & ATA_PFLAG_FROZEN ||
Tejun Heoa20f33f2006-05-16 12:58:24 +09001342 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
Tejun Heo4528e4d2006-07-08 20:17:26 +09001343 ehc->i.action |= ATA_EH_SOFTRESET;
Tejun Heoa20f33f2006-05-16 12:58:24 +09001344 else if (all_err_mask)
Tejun Heo4528e4d2006-07-08 20:17:26 +09001345 ehc->i.action |= ATA_EH_REVALIDATE;
Tejun Heo022bdb02006-05-15 20:58:22 +09001346
Tejun Heo47005f22006-06-19 18:27:23 +09001347 /* if we have offending qcs and the associated failed device */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001348 if (ehc->i.dev) {
Tejun Heo47005f22006-06-19 18:27:23 +09001349 /* speed down */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001350 ehc->i.action |= ata_eh_speed_down(ehc->i.dev, is_io,
1351 all_err_mask);
Tejun Heo47005f22006-06-19 18:27:23 +09001352
1353 /* perform per-dev EH action only on the offending device */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001354 ehc->i.dev_action[ehc->i.dev->devno] |=
1355 ehc->i.action & ATA_EH_PERDEV_MASK;
1356 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
Tejun Heo47005f22006-06-19 18:27:23 +09001357 }
1358
Tejun Heo022bdb02006-05-15 20:58:22 +09001359 DPRINTK("EXIT\n");
1360}
1361
1362/**
1363 * ata_eh_report - report error handling to user
1364 * @ap: ATA port EH is going on
1365 *
1366 * Report EH to user.
1367 *
1368 * LOCKING:
1369 * None.
1370 */
1371static void ata_eh_report(struct ata_port *ap)
1372{
1373 struct ata_eh_context *ehc = &ap->eh_context;
1374 const char *frozen, *desc;
1375 int tag, nr_failed = 0;
1376
1377 desc = NULL;
1378 if (ehc->i.desc[0] != '\0')
1379 desc = ehc->i.desc;
1380
1381 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1382 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1383
1384 if (!(qc->flags & ATA_QCFLAG_FAILED))
1385 continue;
1386 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1387 continue;
1388
1389 nr_failed++;
1390 }
1391
1392 if (!nr_failed && !ehc->i.err_mask)
1393 return;
1394
1395 frozen = "";
Tejun Heob51e9e52006-06-29 01:29:30 +09001396 if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heo022bdb02006-05-15 20:58:22 +09001397 frozen = " frozen";
1398
1399 if (ehc->i.dev) {
Tejun Heoe8ee8452006-05-15 21:03:46 +09001400 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1401 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1402 ehc->i.err_mask, ap->sactive, ehc->i.serror,
1403 ehc->i.action, frozen);
Tejun Heo022bdb02006-05-15 20:58:22 +09001404 if (desc)
1405 ata_dev_printk(ehc->i.dev, KERN_ERR, "(%s)\n", desc);
1406 } else {
Tejun Heoe8ee8452006-05-15 21:03:46 +09001407 ata_port_printk(ap, KERN_ERR, "exception Emask 0x%x "
1408 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1409 ehc->i.err_mask, ap->sactive, ehc->i.serror,
1410 ehc->i.action, frozen);
Tejun Heo022bdb02006-05-15 20:58:22 +09001411 if (desc)
1412 ata_port_printk(ap, KERN_ERR, "(%s)\n", desc);
1413 }
1414
1415 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1416 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1417
1418 if (!(qc->flags & ATA_QCFLAG_FAILED) || !qc->err_mask)
1419 continue;
1420
1421 ata_dev_printk(qc->dev, KERN_ERR, "tag %d cmd 0x%x "
1422 "Emask 0x%x stat 0x%x err 0x%x (%s)\n",
1423 qc->tag, qc->tf.command, qc->err_mask,
1424 qc->result_tf.command, qc->result_tf.feature,
1425 ata_err_string(qc->err_mask));
1426 }
1427}
1428
Tejun Heod87fa382006-05-31 18:28:24 +09001429static int ata_do_reset(struct ata_port *ap, ata_reset_fn_t reset,
1430 unsigned int *classes)
1431{
1432 int i, rc;
1433
1434 for (i = 0; i < ATA_MAX_DEVICES; i++)
1435 classes[i] = ATA_DEV_UNKNOWN;
1436
1437 rc = reset(ap, classes);
1438 if (rc)
1439 return rc;
1440
1441 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
1442 * is complete and convert all ATA_DEV_UNKNOWN to
1443 * ATA_DEV_NONE.
1444 */
1445 for (i = 0; i < ATA_MAX_DEVICES; i++)
1446 if (classes[i] != ATA_DEV_UNKNOWN)
1447 break;
1448
1449 if (i < ATA_MAX_DEVICES)
1450 for (i = 0; i < ATA_MAX_DEVICES; i++)
1451 if (classes[i] == ATA_DEV_UNKNOWN)
1452 classes[i] = ATA_DEV_NONE;
1453
1454 return 0;
1455}
1456
Tejun Heo664faf02006-05-31 18:27:50 +09001457static int ata_eh_followup_srst_needed(int rc, int classify,
1458 const unsigned int *classes)
1459{
1460 if (rc == -EAGAIN)
1461 return 1;
1462 if (rc != 0)
1463 return 0;
1464 if (classify && classes[0] == ATA_DEV_UNKNOWN)
1465 return 1;
1466 return 0;
1467}
1468
1469static int ata_eh_reset(struct ata_port *ap, int classify,
Tejun Heof5914a42006-05-31 18:27:48 +09001470 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
Tejun Heo022bdb02006-05-15 20:58:22 +09001471 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
1472{
1473 struct ata_eh_context *ehc = &ap->eh_context;
Tejun Heo664faf02006-05-31 18:27:50 +09001474 unsigned int *classes = ehc->classes;
Tejun Heo022bdb02006-05-15 20:58:22 +09001475 int tries = ATA_EH_RESET_TRIES;
Tejun Heo1cdaf532006-07-03 16:07:26 +09001476 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
Tejun Heof5914a42006-05-31 18:27:48 +09001477 unsigned int action;
Tejun Heo022bdb02006-05-15 20:58:22 +09001478 ata_reset_fn_t reset;
Tejun Heo664faf02006-05-31 18:27:50 +09001479 int i, did_followup_srst, rc;
Tejun Heo022bdb02006-05-15 20:58:22 +09001480
Tejun Heof5914a42006-05-31 18:27:48 +09001481 /* Determine which reset to use and record in ehc->i.action.
1482 * prereset() may examine and modify it.
1483 */
1484 action = ehc->i.action;
1485 ehc->i.action &= ~ATA_EH_RESET_MASK;
Tejun Heo022bdb02006-05-15 20:58:22 +09001486 if (softreset && (!hardreset || (!sata_set_spd_needed(ap) &&
Tejun Heof5914a42006-05-31 18:27:48 +09001487 !(action & ATA_EH_HARDRESET))))
1488 ehc->i.action |= ATA_EH_SOFTRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001489 else
Tejun Heof5914a42006-05-31 18:27:48 +09001490 ehc->i.action |= ATA_EH_HARDRESET;
1491
1492 if (prereset) {
1493 rc = prereset(ap);
1494 if (rc) {
1495 ata_port_printk(ap, KERN_ERR,
1496 "prereset failed (errno=%d)\n", rc);
1497 return rc;
1498 }
1499 }
1500
1501 /* prereset() might have modified ehc->i.action */
1502 if (ehc->i.action & ATA_EH_HARDRESET)
Tejun Heo022bdb02006-05-15 20:58:22 +09001503 reset = hardreset;
Tejun Heof5914a42006-05-31 18:27:48 +09001504 else if (ehc->i.action & ATA_EH_SOFTRESET)
1505 reset = softreset;
1506 else {
1507 /* prereset told us not to reset, bang classes and return */
1508 for (i = 0; i < ATA_MAX_DEVICES; i++)
1509 classes[i] = ATA_DEV_NONE;
1510 return 0;
1511 }
1512
1513 /* did prereset() screw up? if so, fix up to avoid oopsing */
1514 if (!reset) {
1515 ata_port_printk(ap, KERN_ERR, "BUG: prereset() requested "
1516 "invalid reset type\n");
1517 if (softreset)
1518 reset = softreset;
1519 else
1520 reset = hardreset;
1521 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001522
1523 retry:
Tejun Heo3e706392006-05-31 18:28:11 +09001524 /* shut up during boot probing */
1525 if (verbose)
1526 ata_port_printk(ap, KERN_INFO, "%s resetting port\n",
1527 reset == softreset ? "soft" : "hard");
Tejun Heo022bdb02006-05-15 20:58:22 +09001528
1529 /* reset */
Tejun Heo47005f22006-06-19 18:27:23 +09001530 ata_eh_about_to_do(ap, NULL, ATA_EH_RESET_MASK);
Tejun Heo022bdb02006-05-15 20:58:22 +09001531 ehc->i.flags |= ATA_EHI_DID_RESET;
1532
1533 rc = ata_do_reset(ap, reset, classes);
1534
Tejun Heo664faf02006-05-31 18:27:50 +09001535 did_followup_srst = 0;
1536 if (reset == hardreset &&
1537 ata_eh_followup_srst_needed(rc, classify, classes)) {
1538 /* okay, let's do follow-up softreset */
1539 did_followup_srst = 1;
1540 reset = softreset;
1541
1542 if (!reset) {
1543 ata_port_printk(ap, KERN_ERR,
1544 "follow-up softreset required "
1545 "but no softreset avaliable\n");
1546 return -EINVAL;
1547 }
1548
Tejun Heo47005f22006-06-19 18:27:23 +09001549 ata_eh_about_to_do(ap, NULL, ATA_EH_RESET_MASK);
Tejun Heo664faf02006-05-31 18:27:50 +09001550 rc = ata_do_reset(ap, reset, classes);
1551
1552 if (rc == 0 && classify &&
1553 classes[0] == ATA_DEV_UNKNOWN) {
1554 ata_port_printk(ap, KERN_ERR,
1555 "classification failed\n");
1556 return -EINVAL;
1557 }
1558 }
1559
Tejun Heo022bdb02006-05-15 20:58:22 +09001560 if (rc && --tries) {
Tejun Heo664faf02006-05-31 18:27:50 +09001561 const char *type;
1562
1563 if (reset == softreset) {
1564 if (did_followup_srst)
1565 type = "follow-up soft";
1566 else
1567 type = "soft";
1568 } else
1569 type = "hard";
1570
Tejun Heo022bdb02006-05-15 20:58:22 +09001571 ata_port_printk(ap, KERN_WARNING,
Tejun Heo664faf02006-05-31 18:27:50 +09001572 "%sreset failed, retrying in 5 secs\n", type);
Tejun Heo022bdb02006-05-15 20:58:22 +09001573 ssleep(5);
1574
1575 if (reset == hardreset)
1576 sata_down_spd_limit(ap);
1577 if (hardreset)
1578 reset = hardreset;
1579 goto retry;
1580 }
1581
1582 if (rc == 0) {
Tejun Heo20952b62006-05-31 18:27:23 +09001583 /* After the reset, the device state is PIO 0 and the
1584 * controller state is undefined. Record the mode.
1585 */
1586 for (i = 0; i < ATA_MAX_DEVICES; i++)
1587 ap->device[i].pio_mode = XFER_PIO_0;
1588
Tejun Heo022bdb02006-05-15 20:58:22 +09001589 if (postreset)
1590 postreset(ap, classes);
1591
1592 /* reset successful, schedule revalidation */
Tejun Heo47005f22006-06-19 18:27:23 +09001593 ata_eh_done(ap, NULL, ATA_EH_RESET_MASK);
Tejun Heo022bdb02006-05-15 20:58:22 +09001594 ehc->i.action |= ATA_EH_REVALIDATE;
1595 }
1596
1597 return rc;
1598}
1599
Tejun Heo084fe632006-05-31 18:28:03 +09001600static int ata_eh_revalidate_and_attach(struct ata_port *ap,
1601 struct ata_device **r_failed_dev)
Tejun Heo022bdb02006-05-15 20:58:22 +09001602{
1603 struct ata_eh_context *ehc = &ap->eh_context;
1604 struct ata_device *dev;
Tejun Heo084fe632006-05-31 18:28:03 +09001605 unsigned long flags;
Tejun Heo022bdb02006-05-15 20:58:22 +09001606 int i, rc = 0;
1607
1608 DPRINTK("ENTER\n");
1609
1610 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heo47005f22006-06-19 18:27:23 +09001611 unsigned int action;
Tejun Heo022bdb02006-05-15 20:58:22 +09001612
Tejun Heo47005f22006-06-19 18:27:23 +09001613 dev = &ap->device[i];
Tejun Heo64f65ca2006-06-24 20:30:18 +09001614 action = ata_eh_dev_action(dev);
Tejun Heo47005f22006-06-19 18:27:23 +09001615
Tejun Heo02670bf2006-07-03 16:07:26 +09001616 if (action & ATA_EH_REVALIDATE && ata_dev_ready(dev)) {
Tejun Heo022bdb02006-05-15 20:58:22 +09001617 if (ata_port_offline(ap)) {
1618 rc = -EIO;
1619 break;
1620 }
1621
Tejun Heo47005f22006-06-19 18:27:23 +09001622 ata_eh_about_to_do(ap, dev, ATA_EH_REVALIDATE);
Tejun Heo022bdb02006-05-15 20:58:22 +09001623 rc = ata_dev_revalidate(dev,
1624 ehc->i.flags & ATA_EHI_DID_RESET);
1625 if (rc)
1626 break;
1627
Tejun Heo47005f22006-06-19 18:27:23 +09001628 ata_eh_done(ap, dev, ATA_EH_REVALIDATE);
1629
zhao, forrest3057ac32006-06-12 12:01:34 +08001630 /* schedule the scsi_rescan_device() here */
1631 queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
Tejun Heo084fe632006-05-31 18:28:03 +09001632 } else if (dev->class == ATA_DEV_UNKNOWN &&
1633 ehc->tries[dev->devno] &&
1634 ata_class_enabled(ehc->classes[dev->devno])) {
1635 dev->class = ehc->classes[dev->devno];
1636
1637 rc = ata_dev_read_id(dev, &dev->class, 1, dev->id);
1638 if (rc == 0)
1639 rc = ata_dev_configure(dev, 1);
1640
1641 if (rc) {
1642 dev->class = ATA_DEV_UNKNOWN;
1643 break;
1644 }
1645
Jeff Garzikba6a1302006-06-22 23:46:10 -04001646 spin_lock_irqsave(ap->lock, flags);
Tejun Heob51e9e52006-06-29 01:29:30 +09001647 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
Jeff Garzikba6a1302006-06-22 23:46:10 -04001648 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo022bdb02006-05-15 20:58:22 +09001649 }
1650 }
1651
Tejun Heo47005f22006-06-19 18:27:23 +09001652 if (rc)
Tejun Heo022bdb02006-05-15 20:58:22 +09001653 *r_failed_dev = dev;
1654
1655 DPRINTK("EXIT\n");
1656 return rc;
1657}
1658
Tejun Heo02670bf2006-07-03 16:07:26 +09001659/**
1660 * ata_eh_suspend - handle suspend EH action
1661 * @ap: target host port
1662 * @r_failed_dev: result parameter to indicate failing device
1663 *
1664 * Handle suspend EH action. Disk devices are spinned down and
1665 * other types of devices are just marked suspended. Once
1666 * suspended, no EH action to the device is allowed until it is
1667 * resumed.
1668 *
1669 * LOCKING:
1670 * Kernel thread context (may sleep).
1671 *
1672 * RETURNS:
1673 * 0 on success, -errno otherwise
1674 */
1675static int ata_eh_suspend(struct ata_port *ap, struct ata_device **r_failed_dev)
1676{
1677 struct ata_device *dev;
1678 int i, rc = 0;
1679
1680 DPRINTK("ENTER\n");
1681
1682 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1683 unsigned long flags;
1684 unsigned int action, err_mask;
1685
1686 dev = &ap->device[i];
1687 action = ata_eh_dev_action(dev);
1688
1689 if (!ata_dev_enabled(dev) || !(action & ATA_EH_SUSPEND))
1690 continue;
1691
1692 WARN_ON(dev->flags & ATA_DFLAG_SUSPENDED);
1693
1694 ata_eh_about_to_do(ap, dev, ATA_EH_SUSPEND);
1695
1696 if (dev->class == ATA_DEV_ATA && !(action & ATA_EH_PM_FREEZE)) {
1697 /* flush cache */
1698 rc = ata_flush_cache(dev);
1699 if (rc)
1700 break;
1701
1702 /* spin down */
1703 err_mask = ata_do_simple_cmd(dev, ATA_CMD_STANDBYNOW1);
1704 if (err_mask) {
1705 ata_dev_printk(dev, KERN_ERR, "failed to "
1706 "spin down (err_mask=0x%x)\n",
1707 err_mask);
1708 rc = -EIO;
1709 break;
1710 }
1711 }
1712
1713 spin_lock_irqsave(ap->lock, flags);
1714 dev->flags |= ATA_DFLAG_SUSPENDED;
1715 spin_unlock_irqrestore(ap->lock, flags);
1716
1717 ata_eh_done(ap, dev, ATA_EH_SUSPEND);
1718 }
1719
1720 if (rc)
1721 *r_failed_dev = dev;
1722
1723 DPRINTK("EXIT\n");
1724 return 0;
1725}
1726
1727/**
1728 * ata_eh_prep_resume - prep for resume EH action
1729 * @ap: target host port
1730 *
1731 * Clear SUSPENDED in preparation for scheduled resume actions.
1732 * This allows other parts of EH to access the devices being
1733 * resumed.
1734 *
1735 * LOCKING:
1736 * Kernel thread context (may sleep).
1737 */
1738static void ata_eh_prep_resume(struct ata_port *ap)
1739{
1740 struct ata_device *dev;
1741 unsigned long flags;
1742 int i;
1743
1744 DPRINTK("ENTER\n");
1745
1746 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1747 unsigned int action;
1748
1749 dev = &ap->device[i];
1750 action = ata_eh_dev_action(dev);
1751
1752 if (!ata_dev_enabled(dev) || !(action & ATA_EH_RESUME))
1753 continue;
1754
1755 spin_lock_irqsave(ap->lock, flags);
1756 dev->flags &= ~ATA_DFLAG_SUSPENDED;
1757 spin_unlock_irqrestore(ap->lock, flags);
1758 }
1759
1760 DPRINTK("EXIT\n");
1761}
1762
1763/**
1764 * ata_eh_resume - handle resume EH action
1765 * @ap: target host port
1766 * @r_failed_dev: result parameter to indicate failing device
1767 *
1768 * Handle resume EH action. Target devices are already reset and
1769 * revalidated. Spinning up is the only operation left.
1770 *
1771 * LOCKING:
1772 * Kernel thread context (may sleep).
1773 *
1774 * RETURNS:
1775 * 0 on success, -errno otherwise
1776 */
1777static int ata_eh_resume(struct ata_port *ap, struct ata_device **r_failed_dev)
1778{
1779 struct ata_device *dev;
1780 int i, rc = 0;
1781
1782 DPRINTK("ENTER\n");
1783
1784 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1785 unsigned int action, err_mask;
1786
1787 dev = &ap->device[i];
1788 action = ata_eh_dev_action(dev);
1789
1790 if (!ata_dev_enabled(dev) || !(action & ATA_EH_RESUME))
1791 continue;
1792
1793 ata_eh_about_to_do(ap, dev, ATA_EH_RESUME);
1794
1795 if (dev->class == ATA_DEV_ATA && !(action & ATA_EH_PM_FREEZE)) {
1796 err_mask = ata_do_simple_cmd(dev,
1797 ATA_CMD_IDLEIMMEDIATE);
1798 if (err_mask) {
1799 ata_dev_printk(dev, KERN_ERR, "failed to "
1800 "spin up (err_mask=0x%x)\n",
1801 err_mask);
1802 rc = -EIO;
1803 break;
1804 }
1805 }
1806
1807 ata_eh_done(ap, dev, ATA_EH_RESUME);
1808 }
1809
1810 if (rc)
1811 *r_failed_dev = dev;
1812
1813 DPRINTK("EXIT\n");
1814 return 0;
1815}
1816
Tejun Heo022bdb02006-05-15 20:58:22 +09001817static int ata_port_nr_enabled(struct ata_port *ap)
1818{
1819 int i, cnt = 0;
1820
1821 for (i = 0; i < ATA_MAX_DEVICES; i++)
1822 if (ata_dev_enabled(&ap->device[i]))
1823 cnt++;
1824 return cnt;
1825}
1826
Tejun Heo084fe632006-05-31 18:28:03 +09001827static int ata_port_nr_vacant(struct ata_port *ap)
1828{
1829 int i, cnt = 0;
1830
1831 for (i = 0; i < ATA_MAX_DEVICES; i++)
1832 if (ap->device[i].class == ATA_DEV_UNKNOWN)
1833 cnt++;
1834 return cnt;
1835}
1836
1837static int ata_eh_skip_recovery(struct ata_port *ap)
1838{
1839 struct ata_eh_context *ehc = &ap->eh_context;
1840 int i;
1841
Tejun Heo02670bf2006-07-03 16:07:26 +09001842 /* skip if all possible devices are suspended */
1843 for (i = 0; i < ata_port_max_devices(ap); i++) {
1844 struct ata_device *dev = &ap->device[i];
1845
Tejun Heo7c8c2cf2006-07-10 23:18:23 +09001846 if (!(dev->flags & ATA_DFLAG_SUSPENDED))
Tejun Heo02670bf2006-07-03 16:07:26 +09001847 break;
1848 }
1849
1850 if (i == ata_port_max_devices(ap))
1851 return 1;
1852
Tejun Heo7c8c2cf2006-07-10 23:18:23 +09001853 /* thaw frozen port, resume link and recover failed devices */
1854 if ((ap->pflags & ATA_PFLAG_FROZEN) ||
1855 (ehc->i.flags & ATA_EHI_RESUME_LINK) || ata_port_nr_enabled(ap))
Tejun Heo084fe632006-05-31 18:28:03 +09001856 return 0;
1857
1858 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
1859 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1860 struct ata_device *dev = &ap->device[i];
1861
1862 if (dev->class == ATA_DEV_UNKNOWN &&
1863 ehc->classes[dev->devno] != ATA_DEV_NONE)
1864 return 0;
1865 }
1866
1867 return 1;
1868}
1869
Tejun Heo022bdb02006-05-15 20:58:22 +09001870/**
1871 * ata_eh_recover - recover host port after error
1872 * @ap: host port to recover
Tejun Heof5914a42006-05-31 18:27:48 +09001873 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09001874 * @softreset: softreset method (can be NULL)
1875 * @hardreset: hardreset method (can be NULL)
1876 * @postreset: postreset method (can be NULL)
1877 *
1878 * This is the alpha and omega, eum and yang, heart and soul of
1879 * libata exception handling. On entry, actions required to
Tejun Heo084fe632006-05-31 18:28:03 +09001880 * recover the port and hotplug requests are recorded in
1881 * eh_context. This function executes all the operations with
1882 * appropriate retrials and fallbacks to resurrect failed
1883 * devices, detach goners and greet newcomers.
Tejun Heo022bdb02006-05-15 20:58:22 +09001884 *
1885 * LOCKING:
1886 * Kernel thread context (may sleep).
1887 *
1888 * RETURNS:
1889 * 0 on success, -errno on failure.
1890 */
Tejun Heof5914a42006-05-31 18:27:48 +09001891static int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
1892 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
Tejun Heo022bdb02006-05-15 20:58:22 +09001893 ata_postreset_fn_t postreset)
1894{
1895 struct ata_eh_context *ehc = &ap->eh_context;
1896 struct ata_device *dev;
1897 int down_xfermask, i, rc;
1898
1899 DPRINTK("ENTER\n");
1900
1901 /* prep for recovery */
1902 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1903 dev = &ap->device[i];
1904
1905 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
Tejun Heo084fe632006-05-31 18:28:03 +09001906
1907 /* process hotplug request */
1908 if (dev->flags & ATA_DFLAG_DETACH)
1909 ata_eh_detach_dev(dev);
1910
1911 if (!ata_dev_enabled(dev) &&
1912 ((ehc->i.probe_mask & (1 << dev->devno)) &&
1913 !(ehc->did_probe_mask & (1 << dev->devno)))) {
1914 ata_eh_detach_dev(dev);
1915 ata_dev_init(dev);
1916 ehc->did_probe_mask |= (1 << dev->devno);
1917 ehc->i.action |= ATA_EH_SOFTRESET;
1918 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001919 }
1920
1921 retry:
1922 down_xfermask = 0;
1923 rc = 0;
1924
Tejun Heoaeb2ecd2006-06-12 14:11:43 +09001925 /* if UNLOADING, finish immediately */
Tejun Heob51e9e52006-06-29 01:29:30 +09001926 if (ap->pflags & ATA_PFLAG_UNLOADING)
Tejun Heoaeb2ecd2006-06-12 14:11:43 +09001927 goto out;
1928
Tejun Heo02670bf2006-07-03 16:07:26 +09001929 /* prep for resume */
1930 ata_eh_prep_resume(ap);
1931
Tejun Heo022bdb02006-05-15 20:58:22 +09001932 /* skip EH if possible. */
Tejun Heo084fe632006-05-31 18:28:03 +09001933 if (ata_eh_skip_recovery(ap))
Tejun Heo022bdb02006-05-15 20:58:22 +09001934 ehc->i.action = 0;
1935
Tejun Heo084fe632006-05-31 18:28:03 +09001936 for (i = 0; i < ATA_MAX_DEVICES; i++)
1937 ehc->classes[i] = ATA_DEV_UNKNOWN;
1938
Tejun Heo022bdb02006-05-15 20:58:22 +09001939 /* reset */
1940 if (ehc->i.action & ATA_EH_RESET_MASK) {
1941 ata_eh_freeze_port(ap);
1942
Tejun Heo084fe632006-05-31 18:28:03 +09001943 rc = ata_eh_reset(ap, ata_port_nr_vacant(ap), prereset,
1944 softreset, hardreset, postreset);
Tejun Heo022bdb02006-05-15 20:58:22 +09001945 if (rc) {
1946 ata_port_printk(ap, KERN_ERR,
1947 "reset failed, giving up\n");
1948 goto out;
1949 }
1950
1951 ata_eh_thaw_port(ap);
1952 }
1953
Tejun Heo084fe632006-05-31 18:28:03 +09001954 /* revalidate existing devices and attach new ones */
1955 rc = ata_eh_revalidate_and_attach(ap, &dev);
Tejun Heo022bdb02006-05-15 20:58:22 +09001956 if (rc)
1957 goto dev_fail;
1958
Tejun Heo02670bf2006-07-03 16:07:26 +09001959 /* resume devices */
1960 rc = ata_eh_resume(ap, &dev);
1961 if (rc)
1962 goto dev_fail;
1963
Tejun Heo022bdb02006-05-15 20:58:22 +09001964 /* configure transfer mode if the port has been reset */
1965 if (ehc->i.flags & ATA_EHI_DID_RESET) {
1966 rc = ata_set_mode(ap, &dev);
1967 if (rc) {
1968 down_xfermask = 1;
1969 goto dev_fail;
1970 }
1971 }
1972
Tejun Heo02670bf2006-07-03 16:07:26 +09001973 /* suspend devices */
1974 rc = ata_eh_suspend(ap, &dev);
1975 if (rc)
1976 goto dev_fail;
1977
Tejun Heo022bdb02006-05-15 20:58:22 +09001978 goto out;
1979
1980 dev_fail:
1981 switch (rc) {
1982 case -ENODEV:
Tejun Heo084fe632006-05-31 18:28:03 +09001983 /* device missing, schedule probing */
1984 ehc->i.probe_mask |= (1 << dev->devno);
Tejun Heo022bdb02006-05-15 20:58:22 +09001985 case -EINVAL:
1986 ehc->tries[dev->devno] = 0;
1987 break;
1988 case -EIO:
1989 sata_down_spd_limit(ap);
1990 default:
1991 ehc->tries[dev->devno]--;
1992 if (down_xfermask &&
1993 ata_down_xfermask_limit(dev, ehc->tries[dev->devno] == 1))
1994 ehc->tries[dev->devno] = 0;
1995 }
1996
Tejun Heo084fe632006-05-31 18:28:03 +09001997 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
1998 /* disable device if it has used up all its chances */
Tejun Heo022bdb02006-05-15 20:58:22 +09001999 ata_dev_disable(dev);
2000
Tejun Heo084fe632006-05-31 18:28:03 +09002001 /* detach if offline */
2002 if (ata_port_offline(ap))
2003 ata_eh_detach_dev(dev);
2004
2005 /* probe if requested */
2006 if ((ehc->i.probe_mask & (1 << dev->devno)) &&
2007 !(ehc->did_probe_mask & (1 << dev->devno))) {
2008 ata_eh_detach_dev(dev);
2009 ata_dev_init(dev);
2010
2011 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2012 ehc->did_probe_mask |= (1 << dev->devno);
2013 ehc->i.action |= ATA_EH_SOFTRESET;
2014 }
2015 } else {
2016 /* soft didn't work? be haaaaard */
2017 if (ehc->i.flags & ATA_EHI_DID_RESET)
2018 ehc->i.action |= ATA_EH_HARDRESET;
2019 else
2020 ehc->i.action |= ATA_EH_SOFTRESET;
2021 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002022
2023 if (ata_port_nr_enabled(ap)) {
2024 ata_port_printk(ap, KERN_WARNING, "failed to recover some "
2025 "devices, retrying in 5 secs\n");
2026 ssleep(5);
2027 } else {
2028 /* no device left, repeat fast */
2029 msleep(500);
2030 }
2031
2032 goto retry;
2033
2034 out:
2035 if (rc) {
2036 for (i = 0; i < ATA_MAX_DEVICES; i++)
2037 ata_dev_disable(&ap->device[i]);
2038 }
2039
2040 DPRINTK("EXIT, rc=%d\n", rc);
2041 return rc;
2042}
2043
2044/**
2045 * ata_eh_finish - finish up EH
2046 * @ap: host port to finish EH for
2047 *
2048 * Recovery is complete. Clean up EH states and retry or finish
2049 * failed qcs.
2050 *
2051 * LOCKING:
2052 * None.
2053 */
2054static void ata_eh_finish(struct ata_port *ap)
2055{
2056 int tag;
2057
2058 /* retry or finish qcs */
2059 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2060 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2061
2062 if (!(qc->flags & ATA_QCFLAG_FAILED))
2063 continue;
2064
2065 if (qc->err_mask) {
2066 /* FIXME: Once EH migration is complete,
2067 * generate sense data in this function,
2068 * considering both err_mask and tf.
2069 */
2070 if (qc->err_mask & AC_ERR_INVALID)
2071 ata_eh_qc_complete(qc);
2072 else
2073 ata_eh_qc_retry(qc);
2074 } else {
2075 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2076 ata_eh_qc_complete(qc);
2077 } else {
2078 /* feed zero TF to sense generation */
2079 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2080 ata_eh_qc_retry(qc);
2081 }
2082 }
2083 }
2084}
2085
2086/**
2087 * ata_do_eh - do standard error handling
2088 * @ap: host port to handle error for
Tejun Heof5914a42006-05-31 18:27:48 +09002089 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09002090 * @softreset: softreset method (can be NULL)
2091 * @hardreset: hardreset method (can be NULL)
2092 * @postreset: postreset method (can be NULL)
2093 *
2094 * Perform standard error handling sequence.
2095 *
2096 * LOCKING:
2097 * Kernel thread context (may sleep).
2098 */
Tejun Heof5914a42006-05-31 18:27:48 +09002099void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2100 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2101 ata_postreset_fn_t postreset)
Tejun Heo022bdb02006-05-15 20:58:22 +09002102{
Tejun Heo1cdaf532006-07-03 16:07:26 +09002103 ata_eh_autopsy(ap);
2104 ata_eh_report(ap);
Tejun Heof5914a42006-05-31 18:27:48 +09002105 ata_eh_recover(ap, prereset, softreset, hardreset, postreset);
Tejun Heo022bdb02006-05-15 20:58:22 +09002106 ata_eh_finish(ap);
2107}
Tejun Heo500530f2006-07-03 16:07:27 +09002108
2109/**
2110 * ata_eh_handle_port_suspend - perform port suspend operation
2111 * @ap: port to suspend
2112 *
2113 * Suspend @ap.
2114 *
2115 * LOCKING:
2116 * Kernel thread context (may sleep).
2117 */
2118static void ata_eh_handle_port_suspend(struct ata_port *ap)
2119{
2120 unsigned long flags;
2121 int rc = 0;
2122
2123 /* are we suspending? */
2124 spin_lock_irqsave(ap->lock, flags);
2125 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2126 ap->pm_mesg.event == PM_EVENT_ON) {
2127 spin_unlock_irqrestore(ap->lock, flags);
2128 return;
2129 }
2130 spin_unlock_irqrestore(ap->lock, flags);
2131
2132 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
2133
2134 /* suspend */
2135 ata_eh_freeze_port(ap);
2136
2137 if (ap->ops->port_suspend)
2138 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
2139
2140 /* report result */
2141 spin_lock_irqsave(ap->lock, flags);
2142
2143 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
2144 if (rc == 0)
2145 ap->pflags |= ATA_PFLAG_SUSPENDED;
2146 else
2147 ata_port_schedule_eh(ap);
2148
2149 if (ap->pm_result) {
2150 *ap->pm_result = rc;
2151 ap->pm_result = NULL;
2152 }
2153
2154 spin_unlock_irqrestore(ap->lock, flags);
2155
2156 return;
2157}
2158
2159/**
2160 * ata_eh_handle_port_resume - perform port resume operation
2161 * @ap: port to resume
2162 *
2163 * Resume @ap.
2164 *
2165 * This function also waits upto one second until all devices
2166 * hanging off this port requests resume EH action. This is to
2167 * prevent invoking EH and thus reset multiple times on resume.
2168 *
2169 * On DPM resume, where some of devices might not be resumed
2170 * together, this may delay port resume upto one second, but such
2171 * DPM resumes are rare and 1 sec delay isn't too bad.
2172 *
2173 * LOCKING:
2174 * Kernel thread context (may sleep).
2175 */
2176static void ata_eh_handle_port_resume(struct ata_port *ap)
2177{
2178 unsigned long timeout;
2179 unsigned long flags;
2180 int i, rc = 0;
2181
2182 /* are we resuming? */
2183 spin_lock_irqsave(ap->lock, flags);
2184 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2185 ap->pm_mesg.event != PM_EVENT_ON) {
2186 spin_unlock_irqrestore(ap->lock, flags);
2187 return;
2188 }
2189 spin_unlock_irqrestore(ap->lock, flags);
2190
2191 /* spurious? */
2192 if (!(ap->pflags & ATA_PFLAG_SUSPENDED))
2193 goto done;
2194
2195 if (ap->ops->port_resume)
2196 rc = ap->ops->port_resume(ap);
2197
2198 /* give devices time to request EH */
2199 timeout = jiffies + HZ; /* 1s max */
2200 while (1) {
2201 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2202 struct ata_device *dev = &ap->device[i];
2203 unsigned int action = ata_eh_dev_action(dev);
2204
2205 if ((dev->flags & ATA_DFLAG_SUSPENDED) &&
2206 !(action & ATA_EH_RESUME))
2207 break;
2208 }
2209
2210 if (i == ATA_MAX_DEVICES || time_after(jiffies, timeout))
2211 break;
2212 msleep(10);
2213 }
2214
2215 done:
2216 spin_lock_irqsave(ap->lock, flags);
2217 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
2218 if (ap->pm_result) {
2219 *ap->pm_result = rc;
2220 ap->pm_result = NULL;
2221 }
2222 spin_unlock_irqrestore(ap->lock, flags);
2223}