blob: e69f3df2ea3936b6f8babb6053309f04f7add43a [file] [log] [blame]
Tejun Heoece1d632006-04-02 18:51:53 +09001/*
2 * libata-eh.c - libata error handling
3 *
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
7 *
8 * Copyright 2006 Tejun Heo <htejun@gmail.com>
9 *
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; see the file COPYING. If not, write to
23 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
24 * USA.
25 *
26 *
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
29 *
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
32 *
33 */
34
Tejun Heoece1d632006-04-02 18:51:53 +090035#include <linux/kernel.h>
36#include <scsi/scsi.h>
37#include <scsi/scsi_host.h>
38#include <scsi/scsi_eh.h>
39#include <scsi/scsi_device.h>
40#include <scsi/scsi_cmnd.h>
Jeff Garzikc6fd2802006-08-10 07:31:37 -040041#include "../scsi/scsi_transport_api.h"
Tejun Heoece1d632006-04-02 18:51:53 +090042
43#include <linux/libata.h>
44
45#include "libata.h"
46
Tejun Heoad9e2762006-05-15 20:58:12 +090047static void __ata_port_freeze(struct ata_port *ap);
Tejun Heo720ba122006-05-31 18:28:13 +090048static void ata_eh_finish(struct ata_port *ap);
Tejun Heo500530f2006-07-03 16:07:27 +090049static void ata_eh_handle_port_suspend(struct ata_port *ap);
50static void ata_eh_handle_port_resume(struct ata_port *ap);
Tejun Heoad9e2762006-05-15 20:58:12 +090051
Tejun Heo0c247c52006-05-15 20:58:19 +090052static void ata_ering_record(struct ata_ering *ering, int is_io,
53 unsigned int err_mask)
54{
55 struct ata_ering_entry *ent;
56
57 WARN_ON(!err_mask);
58
59 ering->cursor++;
60 ering->cursor %= ATA_ERING_SIZE;
61
62 ent = &ering->ring[ering->cursor];
63 ent->is_io = is_io;
64 ent->err_mask = err_mask;
65 ent->timestamp = get_jiffies_64();
66}
67
68static struct ata_ering_entry * ata_ering_top(struct ata_ering *ering)
69{
70 struct ata_ering_entry *ent = &ering->ring[ering->cursor];
71 if (!ent->err_mask)
72 return NULL;
73 return ent;
74}
75
76static int ata_ering_map(struct ata_ering *ering,
77 int (*map_fn)(struct ata_ering_entry *, void *),
78 void *arg)
79{
80 int idx, rc = 0;
81 struct ata_ering_entry *ent;
82
83 idx = ering->cursor;
84 do {
85 ent = &ering->ring[idx];
86 if (!ent->err_mask)
87 break;
88 rc = map_fn(ent, arg);
89 if (rc)
90 break;
91 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
92 } while (idx != ering->cursor);
93
94 return rc;
95}
96
Tejun Heo64f65ca2006-06-24 20:30:18 +090097static unsigned int ata_eh_dev_action(struct ata_device *dev)
98{
99 struct ata_eh_context *ehc = &dev->ap->eh_context;
100
101 return ehc->i.action | ehc->i.dev_action[dev->devno];
102}
103
Tejun Heoaf181c22006-06-24 20:30:18 +0900104static void ata_eh_clear_action(struct ata_device *dev,
105 struct ata_eh_info *ehi, unsigned int action)
106{
107 int i;
108
109 if (!dev) {
110 ehi->action &= ~action;
111 for (i = 0; i < ATA_MAX_DEVICES; i++)
112 ehi->dev_action[i] &= ~action;
113 } else {
114 /* doesn't make sense for port-wide EH actions */
115 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
116
117 /* break ehi->action into ehi->dev_action */
118 if (ehi->action & action) {
119 for (i = 0; i < ATA_MAX_DEVICES; i++)
120 ehi->dev_action[i] |= ehi->action & action;
121 ehi->action &= ~action;
122 }
123
124 /* turn off the specified per-dev action */
125 ehi->dev_action[dev->devno] &= ~action;
126 }
127}
128
Tejun Heoece1d632006-04-02 18:51:53 +0900129/**
130 * ata_scsi_timed_out - SCSI layer time out callback
131 * @cmd: timed out SCSI command
132 *
133 * Handles SCSI layer timeout. We race with normal completion of
134 * the qc for @cmd. If the qc is already gone, we lose and let
135 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
136 * timed out and EH should be invoked. Prevent ata_qc_complete()
137 * from finishing it by setting EH_SCHEDULED and return
138 * EH_NOT_HANDLED.
139 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900140 * TODO: kill this function once old EH is gone.
141 *
Tejun Heoece1d632006-04-02 18:51:53 +0900142 * LOCKING:
143 * Called from timer context
144 *
145 * RETURNS:
146 * EH_HANDLED or EH_NOT_HANDLED
147 */
148enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
149{
150 struct Scsi_Host *host = cmd->device->host;
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400151 struct ata_port *ap = ata_shost_to_port(host);
Tejun Heoece1d632006-04-02 18:51:53 +0900152 unsigned long flags;
153 struct ata_queued_cmd *qc;
Tejun Heoad9e2762006-05-15 20:58:12 +0900154 enum scsi_eh_timer_return ret;
Tejun Heoece1d632006-04-02 18:51:53 +0900155
156 DPRINTK("ENTER\n");
157
Tejun Heoad9e2762006-05-15 20:58:12 +0900158 if (ap->ops->error_handler) {
159 ret = EH_NOT_HANDLED;
160 goto out;
161 }
162
163 ret = EH_HANDLED;
Jeff Garzikba6a1302006-06-22 23:46:10 -0400164 spin_lock_irqsave(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900165 qc = ata_qc_from_tag(ap, ap->active_tag);
166 if (qc) {
167 WARN_ON(qc->scsicmd != cmd);
168 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
169 qc->err_mask |= AC_ERR_TIMEOUT;
170 ret = EH_NOT_HANDLED;
171 }
Jeff Garzikba6a1302006-06-22 23:46:10 -0400172 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900173
Tejun Heoad9e2762006-05-15 20:58:12 +0900174 out:
Tejun Heoece1d632006-04-02 18:51:53 +0900175 DPRINTK("EXIT, ret=%d\n", ret);
176 return ret;
177}
178
179/**
180 * ata_scsi_error - SCSI layer error handler callback
181 * @host: SCSI host on which error occurred
182 *
183 * Handles SCSI-layer-thrown error events.
184 *
185 * LOCKING:
186 * Inherited from SCSI layer (none, can sleep)
187 *
188 * RETURNS:
189 * Zero.
190 */
Jeff Garzik381544b2006-04-11 13:04:39 -0400191void ata_scsi_error(struct Scsi_Host *host)
Tejun Heoece1d632006-04-02 18:51:53 +0900192{
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400193 struct ata_port *ap = ata_shost_to_port(host);
Tejun Heoad9e2762006-05-15 20:58:12 +0900194 int i, repeat_cnt = ATA_EH_MAX_REPEAT;
195 unsigned long flags;
Tejun Heoece1d632006-04-02 18:51:53 +0900196
197 DPRINTK("ENTER\n");
198
Tejun Heoad9e2762006-05-15 20:58:12 +0900199 /* synchronize with port task */
Tejun Heoece1d632006-04-02 18:51:53 +0900200 ata_port_flush_task(ap);
201
Jeff Garzikcca39742006-08-24 03:19:22 -0400202 /* synchronize with host lock and sort out timeouts */
Tejun Heoece1d632006-04-02 18:51:53 +0900203
Tejun Heoad9e2762006-05-15 20:58:12 +0900204 /* For new EH, all qcs are finished in one of three ways -
205 * normal completion, error completion, and SCSI timeout.
206 * Both cmpletions can race against SCSI timeout. When normal
207 * completion wins, the qc never reaches EH. When error
208 * completion wins, the qc has ATA_QCFLAG_FAILED set.
209 *
210 * When SCSI timeout wins, things are a bit more complex.
211 * Normal or error completion can occur after the timeout but
212 * before this point. In such cases, both types of
213 * completions are honored. A scmd is determined to have
214 * timed out iff its associated qc is active and not failed.
215 */
216 if (ap->ops->error_handler) {
217 struct scsi_cmnd *scmd, *tmp;
218 int nr_timedout = 0;
Tejun Heoece1d632006-04-02 18:51:53 +0900219
Tejun Heoe30349d2006-07-03 03:02:15 +0900220 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900221
222 list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
223 struct ata_queued_cmd *qc;
224
225 for (i = 0; i < ATA_MAX_QUEUE; i++) {
226 qc = __ata_qc_from_tag(ap, i);
227 if (qc->flags & ATA_QCFLAG_ACTIVE &&
228 qc->scsicmd == scmd)
229 break;
230 }
231
232 if (i < ATA_MAX_QUEUE) {
233 /* the scmd has an associated qc */
234 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
235 /* which hasn't failed yet, timeout */
236 qc->err_mask |= AC_ERR_TIMEOUT;
237 qc->flags |= ATA_QCFLAG_FAILED;
238 nr_timedout++;
239 }
240 } else {
241 /* Normal completion occurred after
242 * SCSI timeout but before this point.
243 * Successfully complete it.
244 */
245 scmd->retries = scmd->allowed;
246 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
247 }
248 }
249
250 /* If we have timed out qcs. They belong to EH from
251 * this point but the state of the controller is
252 * unknown. Freeze the port to make sure the IRQ
253 * handler doesn't diddle with those qcs. This must
254 * be done atomically w.r.t. setting QCFLAG_FAILED.
255 */
256 if (nr_timedout)
257 __ata_port_freeze(ap);
258
Tejun Heoe30349d2006-07-03 03:02:15 +0900259 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900260 } else
Tejun Heoe30349d2006-07-03 03:02:15 +0900261 spin_unlock_wait(ap->lock);
Tejun Heoad9e2762006-05-15 20:58:12 +0900262
263 repeat:
264 /* invoke error handler */
265 if (ap->ops->error_handler) {
Tejun Heo500530f2006-07-03 16:07:27 +0900266 /* process port resume request */
267 ata_eh_handle_port_resume(ap);
268
Tejun Heof3e81b12006-05-15 20:58:21 +0900269 /* fetch & clear EH info */
Tejun Heoe30349d2006-07-03 03:02:15 +0900270 spin_lock_irqsave(ap->lock, flags);
Tejun Heof3e81b12006-05-15 20:58:21 +0900271
272 memset(&ap->eh_context, 0, sizeof(ap->eh_context));
273 ap->eh_context.i = ap->eh_info;
274 memset(&ap->eh_info, 0, sizeof(ap->eh_info));
275
Tejun Heob51e9e52006-06-29 01:29:30 +0900276 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
277 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
Tejun Heof3e81b12006-05-15 20:58:21 +0900278
Tejun Heoe30349d2006-07-03 03:02:15 +0900279 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900280
Tejun Heo500530f2006-07-03 16:07:27 +0900281 /* invoke EH, skip if unloading or suspended */
282 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
Tejun Heo720ba122006-05-31 18:28:13 +0900283 ap->ops->error_handler(ap);
284 else
285 ata_eh_finish(ap);
Tejun Heoad9e2762006-05-15 20:58:12 +0900286
Tejun Heo500530f2006-07-03 16:07:27 +0900287 /* process port suspend request */
288 ata_eh_handle_port_suspend(ap);
289
Tejun Heoad9e2762006-05-15 20:58:12 +0900290 /* Exception might have happend after ->error_handler
291 * recovered the port but before this point. Repeat
292 * EH in such case.
293 */
Tejun Heoe30349d2006-07-03 03:02:15 +0900294 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900295
Tejun Heob51e9e52006-06-29 01:29:30 +0900296 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
Tejun Heoad9e2762006-05-15 20:58:12 +0900297 if (--repeat_cnt) {
298 ata_port_printk(ap, KERN_INFO,
299 "EH pending after completion, "
300 "repeating EH (cnt=%d)\n", repeat_cnt);
Tejun Heoe30349d2006-07-03 03:02:15 +0900301 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900302 goto repeat;
303 }
304 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
305 "tries, giving up\n", ATA_EH_MAX_REPEAT);
306 }
307
Tejun Heof3e81b12006-05-15 20:58:21 +0900308 /* this run is complete, make sure EH info is clear */
309 memset(&ap->eh_info, 0, sizeof(ap->eh_info));
310
Tejun Heoe30349d2006-07-03 03:02:15 +0900311 /* Clear host_eh_scheduled while holding ap->lock such
Tejun Heoad9e2762006-05-15 20:58:12 +0900312 * that if exception occurs after this point but
313 * before EH completion, SCSI midlayer will
314 * re-initiate EH.
315 */
316 host->host_eh_scheduled = 0;
317
Tejun Heoe30349d2006-07-03 03:02:15 +0900318 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900319 } else {
320 WARN_ON(ata_qc_from_tag(ap, ap->active_tag) == NULL);
321 ap->ops->eng_timeout(ap);
322 }
323
324 /* finish or retry handled scmd's and clean up */
Tejun Heoece1d632006-04-02 18:51:53 +0900325 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
326
327 scsi_eh_flush_done_q(&ap->eh_done_q);
328
Tejun Heoad9e2762006-05-15 20:58:12 +0900329 /* clean up */
Tejun Heoe30349d2006-07-03 03:02:15 +0900330 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900331
Tejun Heo1cdaf532006-07-03 16:07:26 +0900332 if (ap->pflags & ATA_PFLAG_LOADING)
Tejun Heob51e9e52006-06-29 01:29:30 +0900333 ap->pflags &= ~ATA_PFLAG_LOADING;
Tejun Heo1cdaf532006-07-03 16:07:26 +0900334 else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
335 queue_work(ata_aux_wq, &ap->hotplug_task);
336
337 if (ap->pflags & ATA_PFLAG_RECOVERED)
338 ata_port_printk(ap, KERN_INFO, "EH complete\n");
Tejun Heo580b21022006-05-31 18:28:05 +0900339
Tejun Heob51e9e52006-06-29 01:29:30 +0900340 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
Tejun Heoad9e2762006-05-15 20:58:12 +0900341
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900342 /* tell wait_eh that we're done */
Tejun Heob51e9e52006-06-29 01:29:30 +0900343 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900344 wake_up_all(&ap->eh_wait_q);
345
Tejun Heoe30349d2006-07-03 03:02:15 +0900346 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900347
Tejun Heoece1d632006-04-02 18:51:53 +0900348 DPRINTK("EXIT\n");
Tejun Heoece1d632006-04-02 18:51:53 +0900349}
350
351/**
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900352 * ata_port_wait_eh - Wait for the currently pending EH to complete
353 * @ap: Port to wait EH for
354 *
355 * Wait until the currently pending EH is complete.
356 *
357 * LOCKING:
358 * Kernel thread context (may sleep).
359 */
360void ata_port_wait_eh(struct ata_port *ap)
361{
362 unsigned long flags;
363 DEFINE_WAIT(wait);
364
365 retry:
Jeff Garzikba6a1302006-06-22 23:46:10 -0400366 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900367
Tejun Heob51e9e52006-06-29 01:29:30 +0900368 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900369 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
Jeff Garzikba6a1302006-06-22 23:46:10 -0400370 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900371 schedule();
Jeff Garzikba6a1302006-06-22 23:46:10 -0400372 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900373 }
Tejun Heo0a1b6222006-06-11 11:01:38 +0900374 finish_wait(&ap->eh_wait_q, &wait);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900375
Jeff Garzikba6a1302006-06-22 23:46:10 -0400376 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900377
378 /* make sure SCSI EH is complete */
Jeff Garzikcca39742006-08-24 03:19:22 -0400379 if (scsi_host_in_recovery(ap->scsi_host)) {
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900380 msleep(10);
381 goto retry;
382 }
383}
384
385/**
Tejun Heoece1d632006-04-02 18:51:53 +0900386 * ata_qc_timeout - Handle timeout of queued command
387 * @qc: Command that timed out
388 *
389 * Some part of the kernel (currently, only the SCSI layer)
390 * has noticed that the active command on port @ap has not
391 * completed after a specified length of time. Handle this
392 * condition by disabling DMA (if necessary) and completing
393 * transactions, with error if necessary.
394 *
395 * This also handles the case of the "lost interrupt", where
396 * for some reason (possibly hardware bug, possibly driver bug)
397 * an interrupt was not delivered to the driver, even though the
398 * transaction completed successfully.
399 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900400 * TODO: kill this function once old EH is gone.
401 *
Tejun Heoece1d632006-04-02 18:51:53 +0900402 * LOCKING:
403 * Inherited from SCSI layer (none, can sleep)
404 */
405static void ata_qc_timeout(struct ata_queued_cmd *qc)
406{
407 struct ata_port *ap = qc->ap;
Tejun Heoece1d632006-04-02 18:51:53 +0900408 u8 host_stat = 0, drv_stat;
409 unsigned long flags;
410
411 DPRINTK("ENTER\n");
412
413 ap->hsm_task_state = HSM_ST_IDLE;
414
Jeff Garzikba6a1302006-06-22 23:46:10 -0400415 spin_lock_irqsave(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900416
417 switch (qc->tf.protocol) {
418
419 case ATA_PROT_DMA:
420 case ATA_PROT_ATAPI_DMA:
421 host_stat = ap->ops->bmdma_status(ap);
422
423 /* before we do anything else, clear DMA-Start bit */
424 ap->ops->bmdma_stop(qc);
425
426 /* fall through */
427
428 default:
429 ata_altstatus(ap);
430 drv_stat = ata_chk_status(ap);
431
432 /* ack bmdma irq events */
433 ap->ops->irq_clear(ap);
434
Tejun Heof15a1da2006-05-15 20:57:56 +0900435 ata_dev_printk(qc->dev, KERN_ERR, "command 0x%x timeout, "
436 "stat 0x%x host_stat 0x%x\n",
437 qc->tf.command, drv_stat, host_stat);
Tejun Heoece1d632006-04-02 18:51:53 +0900438
439 /* complete taskfile transaction */
Jeff Garzikc13b56a2006-04-02 10:34:24 -0400440 qc->err_mask |= AC_ERR_TIMEOUT;
Tejun Heoece1d632006-04-02 18:51:53 +0900441 break;
442 }
443
Jeff Garzikba6a1302006-06-22 23:46:10 -0400444 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900445
446 ata_eh_qc_complete(qc);
447
448 DPRINTK("EXIT\n");
449}
450
451/**
452 * ata_eng_timeout - Handle timeout of queued command
453 * @ap: Port on which timed-out command is active
454 *
455 * Some part of the kernel (currently, only the SCSI layer)
456 * has noticed that the active command on port @ap has not
457 * completed after a specified length of time. Handle this
458 * condition by disabling DMA (if necessary) and completing
459 * transactions, with error if necessary.
460 *
461 * This also handles the case of the "lost interrupt", where
462 * for some reason (possibly hardware bug, possibly driver bug)
463 * an interrupt was not delivered to the driver, even though the
464 * transaction completed successfully.
465 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900466 * TODO: kill this function once old EH is gone.
467 *
Tejun Heoece1d632006-04-02 18:51:53 +0900468 * LOCKING:
469 * Inherited from SCSI layer (none, can sleep)
470 */
471void ata_eng_timeout(struct ata_port *ap)
472{
473 DPRINTK("ENTER\n");
474
475 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
476
477 DPRINTK("EXIT\n");
478}
479
Tejun Heof686bcb2006-05-15 20:58:05 +0900480/**
481 * ata_qc_schedule_eh - schedule qc for error handling
482 * @qc: command to schedule error handling for
483 *
484 * Schedule error handling for @qc. EH will kick in as soon as
485 * other commands are drained.
486 *
487 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400488 * spin_lock_irqsave(host lock)
Tejun Heof686bcb2006-05-15 20:58:05 +0900489 */
490void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
491{
492 struct ata_port *ap = qc->ap;
493
494 WARN_ON(!ap->ops->error_handler);
495
496 qc->flags |= ATA_QCFLAG_FAILED;
Tejun Heob51e9e52006-06-29 01:29:30 +0900497 qc->ap->pflags |= ATA_PFLAG_EH_PENDING;
Tejun Heof686bcb2006-05-15 20:58:05 +0900498
499 /* The following will fail if timeout has already expired.
500 * ata_scsi_error() takes care of such scmds on EH entry.
501 * Note that ATA_QCFLAG_FAILED is unconditionally set after
502 * this function completes.
503 */
504 scsi_req_abort_cmd(qc->scsicmd);
505}
506
Tejun Heo7b70fc02006-05-15 20:58:07 +0900507/**
508 * ata_port_schedule_eh - schedule error handling without a qc
509 * @ap: ATA port to schedule EH for
510 *
511 * Schedule error handling for @ap. EH will kick in as soon as
512 * all commands are drained.
513 *
514 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400515 * spin_lock_irqsave(host lock)
Tejun Heo7b70fc02006-05-15 20:58:07 +0900516 */
517void ata_port_schedule_eh(struct ata_port *ap)
518{
519 WARN_ON(!ap->ops->error_handler);
520
Tejun Heob51e9e52006-06-29 01:29:30 +0900521 ap->pflags |= ATA_PFLAG_EH_PENDING;
Jeff Garzikcca39742006-08-24 03:19:22 -0400522 scsi_schedule_eh(ap->scsi_host);
Tejun Heo7b70fc02006-05-15 20:58:07 +0900523
524 DPRINTK("port EH scheduled\n");
525}
526
527/**
528 * ata_port_abort - abort all qc's on the port
529 * @ap: ATA port to abort qc's for
530 *
531 * Abort all active qc's of @ap and schedule EH.
532 *
533 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400534 * spin_lock_irqsave(host lock)
Tejun Heo7b70fc02006-05-15 20:58:07 +0900535 *
536 * RETURNS:
537 * Number of aborted qc's.
538 */
539int ata_port_abort(struct ata_port *ap)
540{
541 int tag, nr_aborted = 0;
542
543 WARN_ON(!ap->ops->error_handler);
544
545 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
546 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
547
548 if (qc) {
549 qc->flags |= ATA_QCFLAG_FAILED;
550 ata_qc_complete(qc);
551 nr_aborted++;
552 }
553 }
554
555 if (!nr_aborted)
556 ata_port_schedule_eh(ap);
557
558 return nr_aborted;
559}
560
Tejun Heoe3180492006-05-15 20:58:09 +0900561/**
562 * __ata_port_freeze - freeze port
563 * @ap: ATA port to freeze
564 *
565 * This function is called when HSM violation or some other
566 * condition disrupts normal operation of the port. Frozen port
567 * is not allowed to perform any operation until the port is
568 * thawed, which usually follows a successful reset.
569 *
570 * ap->ops->freeze() callback can be used for freezing the port
571 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
572 * port cannot be frozen hardware-wise, the interrupt handler
573 * must ack and clear interrupts unconditionally while the port
574 * is frozen.
575 *
576 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400577 * spin_lock_irqsave(host lock)
Tejun Heoe3180492006-05-15 20:58:09 +0900578 */
579static void __ata_port_freeze(struct ata_port *ap)
580{
581 WARN_ON(!ap->ops->error_handler);
582
583 if (ap->ops->freeze)
584 ap->ops->freeze(ap);
585
Tejun Heob51e9e52006-06-29 01:29:30 +0900586 ap->pflags |= ATA_PFLAG_FROZEN;
Tejun Heoe3180492006-05-15 20:58:09 +0900587
588 DPRINTK("ata%u port frozen\n", ap->id);
589}
590
591/**
592 * ata_port_freeze - abort & freeze port
593 * @ap: ATA port to freeze
594 *
595 * Abort and freeze @ap.
596 *
597 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400598 * spin_lock_irqsave(host lock)
Tejun Heoe3180492006-05-15 20:58:09 +0900599 *
600 * RETURNS:
601 * Number of aborted commands.
602 */
603int ata_port_freeze(struct ata_port *ap)
604{
605 int nr_aborted;
606
607 WARN_ON(!ap->ops->error_handler);
608
609 nr_aborted = ata_port_abort(ap);
610 __ata_port_freeze(ap);
611
612 return nr_aborted;
613}
614
615/**
616 * ata_eh_freeze_port - EH helper to freeze port
617 * @ap: ATA port to freeze
618 *
619 * Freeze @ap.
620 *
621 * LOCKING:
622 * None.
623 */
624void ata_eh_freeze_port(struct ata_port *ap)
625{
626 unsigned long flags;
627
628 if (!ap->ops->error_handler)
629 return;
630
Jeff Garzikba6a1302006-06-22 23:46:10 -0400631 spin_lock_irqsave(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900632 __ata_port_freeze(ap);
Jeff Garzikba6a1302006-06-22 23:46:10 -0400633 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900634}
635
636/**
637 * ata_port_thaw_port - EH helper to thaw port
638 * @ap: ATA port to thaw
639 *
640 * Thaw frozen port @ap.
641 *
642 * LOCKING:
643 * None.
644 */
645void ata_eh_thaw_port(struct ata_port *ap)
646{
647 unsigned long flags;
648
649 if (!ap->ops->error_handler)
650 return;
651
Jeff Garzikba6a1302006-06-22 23:46:10 -0400652 spin_lock_irqsave(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900653
Tejun Heob51e9e52006-06-29 01:29:30 +0900654 ap->pflags &= ~ATA_PFLAG_FROZEN;
Tejun Heoe3180492006-05-15 20:58:09 +0900655
656 if (ap->ops->thaw)
657 ap->ops->thaw(ap);
658
Jeff Garzikba6a1302006-06-22 23:46:10 -0400659 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900660
661 DPRINTK("ata%u port thawed\n", ap->id);
662}
663
Tejun Heoece1d632006-04-02 18:51:53 +0900664static void ata_eh_scsidone(struct scsi_cmnd *scmd)
665{
666 /* nada */
667}
668
669static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
670{
671 struct ata_port *ap = qc->ap;
672 struct scsi_cmnd *scmd = qc->scsicmd;
673 unsigned long flags;
674
Jeff Garzikba6a1302006-06-22 23:46:10 -0400675 spin_lock_irqsave(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900676 qc->scsidone = ata_eh_scsidone;
677 __ata_qc_complete(qc);
678 WARN_ON(ata_tag_valid(qc->tag));
Jeff Garzikba6a1302006-06-22 23:46:10 -0400679 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900680
681 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
682}
683
684/**
685 * ata_eh_qc_complete - Complete an active ATA command from EH
686 * @qc: Command to complete
687 *
688 * Indicate to the mid and upper layers that an ATA command has
689 * completed. To be used from EH.
690 */
691void ata_eh_qc_complete(struct ata_queued_cmd *qc)
692{
693 struct scsi_cmnd *scmd = qc->scsicmd;
694 scmd->retries = scmd->allowed;
695 __ata_eh_qc_complete(qc);
696}
697
698/**
699 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
700 * @qc: Command to retry
701 *
702 * Indicate to the mid and upper layers that an ATA command
703 * should be retried. To be used from EH.
704 *
705 * SCSI midlayer limits the number of retries to scmd->allowed.
706 * scmd->retries is decremented for commands which get retried
707 * due to unrelated failures (qc->err_mask is zero).
708 */
709void ata_eh_qc_retry(struct ata_queued_cmd *qc)
710{
711 struct scsi_cmnd *scmd = qc->scsicmd;
712 if (!qc->err_mask && scmd->retries)
713 scmd->retries--;
714 __ata_eh_qc_complete(qc);
715}
Tejun Heo022bdb02006-05-15 20:58:22 +0900716
717/**
Tejun Heo0ea035a2006-05-31 18:28:01 +0900718 * ata_eh_detach_dev - detach ATA device
719 * @dev: ATA device to detach
720 *
721 * Detach @dev.
722 *
723 * LOCKING:
724 * None.
725 */
726static void ata_eh_detach_dev(struct ata_device *dev)
727{
728 struct ata_port *ap = dev->ap;
729 unsigned long flags;
730
731 ata_dev_disable(dev);
732
Jeff Garzikba6a1302006-06-22 23:46:10 -0400733 spin_lock_irqsave(ap->lock, flags);
Tejun Heo0ea035a2006-05-31 18:28:01 +0900734
735 dev->flags &= ~ATA_DFLAG_DETACH;
736
737 if (ata_scsi_offline_dev(dev)) {
738 dev->flags |= ATA_DFLAG_DETACHED;
Tejun Heob51e9e52006-06-29 01:29:30 +0900739 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
Tejun Heo0ea035a2006-05-31 18:28:01 +0900740 }
741
Tejun Heobeb07c12006-06-24 20:30:19 +0900742 /* clear per-dev EH actions */
743 ata_eh_clear_action(dev, &ap->eh_info, ATA_EH_PERDEV_MASK);
744 ata_eh_clear_action(dev, &ap->eh_context.i, ATA_EH_PERDEV_MASK);
745
Jeff Garzikba6a1302006-06-22 23:46:10 -0400746 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo0ea035a2006-05-31 18:28:01 +0900747}
748
749/**
Tejun Heo022bdb02006-05-15 20:58:22 +0900750 * ata_eh_about_to_do - about to perform eh_action
751 * @ap: target ATA port
Tejun Heo47005f22006-06-19 18:27:23 +0900752 * @dev: target ATA dev for per-dev action (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +0900753 * @action: action about to be performed
754 *
755 * Called just before performing EH actions to clear related bits
756 * in @ap->eh_info such that eh actions are not unnecessarily
757 * repeated.
758 *
759 * LOCKING:
760 * None.
761 */
Tejun Heo47005f22006-06-19 18:27:23 +0900762static void ata_eh_about_to_do(struct ata_port *ap, struct ata_device *dev,
763 unsigned int action)
Tejun Heo022bdb02006-05-15 20:58:22 +0900764{
765 unsigned long flags;
Tejun Heo13abf502006-07-10 23:18:46 +0900766 struct ata_eh_info *ehi = &ap->eh_info;
767 struct ata_eh_context *ehc = &ap->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +0900768
Jeff Garzikba6a1302006-06-22 23:46:10 -0400769 spin_lock_irqsave(ap->lock, flags);
Tejun Heo1cdaf532006-07-03 16:07:26 +0900770
Tejun Heo13abf502006-07-10 23:18:46 +0900771 /* Reset is represented by combination of actions and EHI
772 * flags. Suck in all related bits before clearing eh_info to
773 * avoid losing requested action.
774 */
775 if (action & ATA_EH_RESET_MASK) {
776 ehc->i.action |= ehi->action & ATA_EH_RESET_MASK;
777 ehc->i.flags |= ehi->flags & ATA_EHI_RESET_MODIFIER_MASK;
Tejun Heo1cdaf532006-07-03 16:07:26 +0900778
Tejun Heo13abf502006-07-10 23:18:46 +0900779 /* make sure all reset actions are cleared & clear EHI flags */
780 action |= ATA_EH_RESET_MASK;
781 ehi->flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
782 }
783
784 ata_eh_clear_action(dev, ehi, action);
785
786 if (!(ehc->i.flags & ATA_EHI_QUIET))
Tejun Heo1cdaf532006-07-03 16:07:26 +0900787 ap->pflags |= ATA_PFLAG_RECOVERED;
788
Jeff Garzikba6a1302006-06-22 23:46:10 -0400789 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo022bdb02006-05-15 20:58:22 +0900790}
791
792/**
Tejun Heo47005f22006-06-19 18:27:23 +0900793 * ata_eh_done - EH action complete
794 * @ap: target ATA port
795 * @dev: target ATA dev for per-dev action (can be NULL)
796 * @action: action just completed
797 *
798 * Called right after performing EH actions to clear related bits
799 * in @ap->eh_context.
800 *
801 * LOCKING:
802 * None.
803 */
804static void ata_eh_done(struct ata_port *ap, struct ata_device *dev,
805 unsigned int action)
806{
Tejun Heo13abf502006-07-10 23:18:46 +0900807 /* if reset is complete, clear all reset actions & reset modifier */
808 if (action & ATA_EH_RESET_MASK) {
809 action |= ATA_EH_RESET_MASK;
810 ap->eh_context.i.flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
811 }
812
Tejun Heo47005f22006-06-19 18:27:23 +0900813 ata_eh_clear_action(dev, &ap->eh_context.i, action);
814}
815
816/**
Tejun Heo022bdb02006-05-15 20:58:22 +0900817 * ata_err_string - convert err_mask to descriptive string
818 * @err_mask: error mask to convert to string
819 *
820 * Convert @err_mask to descriptive string. Errors are
821 * prioritized according to severity and only the most severe
822 * error is reported.
823 *
824 * LOCKING:
825 * None.
826 *
827 * RETURNS:
828 * Descriptive string for @err_mask
829 */
830static const char * ata_err_string(unsigned int err_mask)
831{
832 if (err_mask & AC_ERR_HOST_BUS)
833 return "host bus error";
834 if (err_mask & AC_ERR_ATA_BUS)
835 return "ATA bus error";
836 if (err_mask & AC_ERR_TIMEOUT)
837 return "timeout";
838 if (err_mask & AC_ERR_HSM)
839 return "HSM violation";
840 if (err_mask & AC_ERR_SYSTEM)
841 return "internal error";
842 if (err_mask & AC_ERR_MEDIA)
843 return "media error";
844 if (err_mask & AC_ERR_INVALID)
845 return "invalid argument";
846 if (err_mask & AC_ERR_DEV)
847 return "device error";
848 return "unknown error";
849}
850
851/**
Tejun Heoe8ee8452006-05-15 21:03:46 +0900852 * ata_read_log_page - read a specific log page
853 * @dev: target device
854 * @page: page to read
855 * @buf: buffer to store read page
856 * @sectors: number of sectors to read
857 *
858 * Read log page using READ_LOG_EXT command.
859 *
860 * LOCKING:
861 * Kernel thread context (may sleep).
862 *
863 * RETURNS:
864 * 0 on success, AC_ERR_* mask otherwise.
865 */
866static unsigned int ata_read_log_page(struct ata_device *dev,
867 u8 page, void *buf, unsigned int sectors)
868{
869 struct ata_taskfile tf;
870 unsigned int err_mask;
871
872 DPRINTK("read log page - page %d\n", page);
873
874 ata_tf_init(dev, &tf);
875 tf.command = ATA_CMD_READ_LOG_EXT;
876 tf.lbal = page;
877 tf.nsect = sectors;
878 tf.hob_nsect = sectors >> 8;
879 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
880 tf.protocol = ATA_PROT_PIO;
881
882 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
883 buf, sectors * ATA_SECT_SIZE);
884
885 DPRINTK("EXIT, err_mask=%x\n", err_mask);
886 return err_mask;
887}
888
889/**
890 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
891 * @dev: Device to read log page 10h from
892 * @tag: Resulting tag of the failed command
893 * @tf: Resulting taskfile registers of the failed command
894 *
895 * Read log page 10h to obtain NCQ error details and clear error
896 * condition.
897 *
898 * LOCKING:
899 * Kernel thread context (may sleep).
900 *
901 * RETURNS:
902 * 0 on success, -errno otherwise.
903 */
904static int ata_eh_read_log_10h(struct ata_device *dev,
905 int *tag, struct ata_taskfile *tf)
906{
907 u8 *buf = dev->ap->sector_buf;
908 unsigned int err_mask;
909 u8 csum;
910 int i;
911
912 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
913 if (err_mask)
914 return -EIO;
915
916 csum = 0;
917 for (i = 0; i < ATA_SECT_SIZE; i++)
918 csum += buf[i];
919 if (csum)
920 ata_dev_printk(dev, KERN_WARNING,
921 "invalid checksum 0x%x on log page 10h\n", csum);
922
923 if (buf[0] & 0x80)
924 return -ENOENT;
925
926 *tag = buf[0] & 0x1f;
927
928 tf->command = buf[2];
929 tf->feature = buf[3];
930 tf->lbal = buf[4];
931 tf->lbam = buf[5];
932 tf->lbah = buf[6];
933 tf->device = buf[7];
934 tf->hob_lbal = buf[8];
935 tf->hob_lbam = buf[9];
936 tf->hob_lbah = buf[10];
937 tf->nsect = buf[12];
938 tf->hob_nsect = buf[13];
939
940 return 0;
941}
942
943/**
Tejun Heo022bdb02006-05-15 20:58:22 +0900944 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
945 * @dev: device to perform REQUEST_SENSE to
946 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
947 *
948 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
949 * SENSE. This function is EH helper.
950 *
951 * LOCKING:
952 * Kernel thread context (may sleep).
953 *
954 * RETURNS:
955 * 0 on success, AC_ERR_* mask on failure
956 */
957static unsigned int atapi_eh_request_sense(struct ata_device *dev,
958 unsigned char *sense_buf)
959{
960 struct ata_port *ap = dev->ap;
961 struct ata_taskfile tf;
962 u8 cdb[ATAPI_CDB_LEN];
963
964 DPRINTK("ATAPI request sense\n");
965
966 ata_tf_init(dev, &tf);
967
968 /* FIXME: is this needed? */
969 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
970
971 /* XXX: why tf_read here? */
972 ap->ops->tf_read(ap, &tf);
973
974 /* fill these in, for the case where they are -not- overwritten */
975 sense_buf[0] = 0x70;
976 sense_buf[2] = tf.feature >> 4;
977
978 memset(cdb, 0, ATAPI_CDB_LEN);
979 cdb[0] = REQUEST_SENSE;
980 cdb[4] = SCSI_SENSE_BUFFERSIZE;
981
982 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
983 tf.command = ATA_CMD_PACKET;
984
985 /* is it pointless to prefer PIO for "safety reasons"? */
986 if (ap->flags & ATA_FLAG_PIO_DMA) {
987 tf.protocol = ATA_PROT_ATAPI_DMA;
988 tf.feature |= ATAPI_PKT_DMA;
989 } else {
990 tf.protocol = ATA_PROT_ATAPI;
991 tf.lbam = (8 * 1024) & 0xff;
992 tf.lbah = (8 * 1024) >> 8;
993 }
994
995 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
996 sense_buf, SCSI_SENSE_BUFFERSIZE);
997}
998
999/**
1000 * ata_eh_analyze_serror - analyze SError for a failed port
1001 * @ap: ATA port to analyze SError for
1002 *
1003 * Analyze SError if available and further determine cause of
1004 * failure.
1005 *
1006 * LOCKING:
1007 * None.
1008 */
1009static void ata_eh_analyze_serror(struct ata_port *ap)
1010{
1011 struct ata_eh_context *ehc = &ap->eh_context;
1012 u32 serror = ehc->i.serror;
1013 unsigned int err_mask = 0, action = 0;
1014
1015 if (serror & SERR_PERSISTENT) {
1016 err_mask |= AC_ERR_ATA_BUS;
1017 action |= ATA_EH_HARDRESET;
1018 }
1019 if (serror &
1020 (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
1021 err_mask |= AC_ERR_ATA_BUS;
1022 action |= ATA_EH_SOFTRESET;
1023 }
1024 if (serror & SERR_PROTOCOL) {
1025 err_mask |= AC_ERR_HSM;
1026 action |= ATA_EH_SOFTRESET;
1027 }
1028 if (serror & SERR_INTERNAL) {
1029 err_mask |= AC_ERR_SYSTEM;
1030 action |= ATA_EH_SOFTRESET;
1031 }
Tejun Heo084fe632006-05-31 18:28:03 +09001032 if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG))
1033 ata_ehi_hotplugged(&ehc->i);
Tejun Heo022bdb02006-05-15 20:58:22 +09001034
1035 ehc->i.err_mask |= err_mask;
1036 ehc->i.action |= action;
1037}
1038
1039/**
Tejun Heoe8ee8452006-05-15 21:03:46 +09001040 * ata_eh_analyze_ncq_error - analyze NCQ error
1041 * @ap: ATA port to analyze NCQ error for
1042 *
1043 * Read log page 10h, determine the offending qc and acquire
1044 * error status TF. For NCQ device errors, all LLDDs have to do
1045 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1046 * care of the rest.
1047 *
1048 * LOCKING:
1049 * Kernel thread context (may sleep).
1050 */
1051static void ata_eh_analyze_ncq_error(struct ata_port *ap)
1052{
1053 struct ata_eh_context *ehc = &ap->eh_context;
1054 struct ata_device *dev = ap->device;
1055 struct ata_queued_cmd *qc;
1056 struct ata_taskfile tf;
1057 int tag, rc;
1058
1059 /* if frozen, we can't do much */
Tejun Heob51e9e52006-06-29 01:29:30 +09001060 if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heoe8ee8452006-05-15 21:03:46 +09001061 return;
1062
1063 /* is it NCQ device error? */
1064 if (!ap->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1065 return;
1066
1067 /* has LLDD analyzed already? */
1068 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1069 qc = __ata_qc_from_tag(ap, tag);
1070
1071 if (!(qc->flags & ATA_QCFLAG_FAILED))
1072 continue;
1073
1074 if (qc->err_mask)
1075 return;
1076 }
1077
1078 /* okay, this error is ours */
1079 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1080 if (rc) {
1081 ata_port_printk(ap, KERN_ERR, "failed to read log page 10h "
1082 "(errno=%d)\n", rc);
1083 return;
1084 }
1085
1086 if (!(ap->sactive & (1 << tag))) {
1087 ata_port_printk(ap, KERN_ERR, "log page 10h reported "
1088 "inactive tag %d\n", tag);
1089 return;
1090 }
1091
1092 /* we've got the perpetrator, condemn it */
1093 qc = __ata_qc_from_tag(ap, tag);
1094 memcpy(&qc->result_tf, &tf, sizeof(tf));
1095 qc->err_mask |= AC_ERR_DEV;
1096 ehc->i.err_mask &= ~AC_ERR_DEV;
1097}
1098
1099/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001100 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1101 * @qc: qc to analyze
1102 * @tf: Taskfile registers to analyze
1103 *
1104 * Analyze taskfile of @qc and further determine cause of
1105 * failure. This function also requests ATAPI sense data if
1106 * avaliable.
1107 *
1108 * LOCKING:
1109 * Kernel thread context (may sleep).
1110 *
1111 * RETURNS:
1112 * Determined recovery action
1113 */
1114static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1115 const struct ata_taskfile *tf)
1116{
1117 unsigned int tmp, action = 0;
1118 u8 stat = tf->command, err = tf->feature;
1119
1120 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1121 qc->err_mask |= AC_ERR_HSM;
1122 return ATA_EH_SOFTRESET;
1123 }
1124
1125 if (!(qc->err_mask & AC_ERR_DEV))
1126 return 0;
1127
1128 switch (qc->dev->class) {
1129 case ATA_DEV_ATA:
1130 if (err & ATA_ICRC)
1131 qc->err_mask |= AC_ERR_ATA_BUS;
1132 if (err & ATA_UNC)
1133 qc->err_mask |= AC_ERR_MEDIA;
1134 if (err & ATA_IDNF)
1135 qc->err_mask |= AC_ERR_INVALID;
1136 break;
1137
1138 case ATA_DEV_ATAPI:
1139 tmp = atapi_eh_request_sense(qc->dev,
1140 qc->scsicmd->sense_buffer);
1141 if (!tmp) {
1142 /* ATA_QCFLAG_SENSE_VALID is used to tell
1143 * atapi_qc_complete() that sense data is
1144 * already valid.
1145 *
1146 * TODO: interpret sense data and set
1147 * appropriate err_mask.
1148 */
1149 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1150 } else
1151 qc->err_mask |= tmp;
1152 }
1153
1154 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1155 action |= ATA_EH_SOFTRESET;
1156
1157 return action;
1158}
1159
1160static int ata_eh_categorize_ering_entry(struct ata_ering_entry *ent)
1161{
1162 if (ent->err_mask & (AC_ERR_ATA_BUS | AC_ERR_TIMEOUT))
1163 return 1;
1164
1165 if (ent->is_io) {
1166 if (ent->err_mask & AC_ERR_HSM)
1167 return 1;
1168 if ((ent->err_mask &
1169 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1170 return 2;
1171 }
1172
1173 return 0;
1174}
1175
1176struct speed_down_needed_arg {
1177 u64 since;
1178 int nr_errors[3];
1179};
1180
1181static int speed_down_needed_cb(struct ata_ering_entry *ent, void *void_arg)
1182{
1183 struct speed_down_needed_arg *arg = void_arg;
1184
1185 if (ent->timestamp < arg->since)
1186 return -1;
1187
1188 arg->nr_errors[ata_eh_categorize_ering_entry(ent)]++;
1189 return 0;
1190}
1191
1192/**
1193 * ata_eh_speed_down_needed - Determine wheter speed down is necessary
1194 * @dev: Device of interest
1195 *
1196 * This function examines error ring of @dev and determines
1197 * whether speed down is necessary. Speed down is necessary if
1198 * there have been more than 3 of Cat-1 errors or 10 of Cat-2
1199 * errors during last 15 minutes.
1200 *
1201 * Cat-1 errors are ATA_BUS, TIMEOUT for any command and HSM
1202 * violation for known supported commands.
1203 *
1204 * Cat-2 errors are unclassified DEV error for known supported
1205 * command.
1206 *
1207 * LOCKING:
1208 * Inherited from caller.
1209 *
1210 * RETURNS:
1211 * 1 if speed down is necessary, 0 otherwise
1212 */
1213static int ata_eh_speed_down_needed(struct ata_device *dev)
1214{
1215 const u64 interval = 15LLU * 60 * HZ;
1216 static const int err_limits[3] = { -1, 3, 10 };
1217 struct speed_down_needed_arg arg;
1218 struct ata_ering_entry *ent;
1219 int err_cat;
1220 u64 j64;
1221
1222 ent = ata_ering_top(&dev->ering);
1223 if (!ent)
1224 return 0;
1225
1226 err_cat = ata_eh_categorize_ering_entry(ent);
1227 if (err_cat == 0)
1228 return 0;
1229
1230 memset(&arg, 0, sizeof(arg));
1231
1232 j64 = get_jiffies_64();
1233 if (j64 >= interval)
1234 arg.since = j64 - interval;
1235 else
1236 arg.since = 0;
1237
1238 ata_ering_map(&dev->ering, speed_down_needed_cb, &arg);
1239
1240 return arg.nr_errors[err_cat] > err_limits[err_cat];
1241}
1242
1243/**
1244 * ata_eh_speed_down - record error and speed down if necessary
1245 * @dev: Failed device
1246 * @is_io: Did the device fail during normal IO?
1247 * @err_mask: err_mask of the error
1248 *
1249 * Record error and examine error history to determine whether
1250 * adjusting transmission speed is necessary. It also sets
1251 * transmission limits appropriately if such adjustment is
1252 * necessary.
1253 *
1254 * LOCKING:
1255 * Kernel thread context (may sleep).
1256 *
1257 * RETURNS:
1258 * 0 on success, -errno otherwise
1259 */
1260static int ata_eh_speed_down(struct ata_device *dev, int is_io,
1261 unsigned int err_mask)
1262{
1263 if (!err_mask)
1264 return 0;
1265
1266 /* record error and determine whether speed down is necessary */
1267 ata_ering_record(&dev->ering, is_io, err_mask);
1268
1269 if (!ata_eh_speed_down_needed(dev))
1270 return 0;
1271
1272 /* speed down SATA link speed if possible */
1273 if (sata_down_spd_limit(dev->ap) == 0)
1274 return ATA_EH_HARDRESET;
1275
1276 /* lower transfer mode */
1277 if (ata_down_xfermask_limit(dev, 0) == 0)
1278 return ATA_EH_SOFTRESET;
1279
1280 ata_dev_printk(dev, KERN_ERR,
1281 "speed down requested but no transfer mode left\n");
1282 return 0;
1283}
1284
1285/**
1286 * ata_eh_autopsy - analyze error and determine recovery action
1287 * @ap: ATA port to perform autopsy on
1288 *
1289 * Analyze why @ap failed and determine which recovery action is
1290 * needed. This function also sets more detailed AC_ERR_* values
1291 * and fills sense data for ATAPI CHECK SENSE.
1292 *
1293 * LOCKING:
1294 * Kernel thread context (may sleep).
1295 */
1296static void ata_eh_autopsy(struct ata_port *ap)
1297{
1298 struct ata_eh_context *ehc = &ap->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001299 unsigned int all_err_mask = 0;
1300 int tag, is_io = 0;
1301 u32 serror;
1302 int rc;
1303
1304 DPRINTK("ENTER\n");
1305
Tejun Heo1cdaf532006-07-03 16:07:26 +09001306 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1307 return;
1308
Tejun Heo022bdb02006-05-15 20:58:22 +09001309 /* obtain and analyze SError */
1310 rc = sata_scr_read(ap, SCR_ERROR, &serror);
1311 if (rc == 0) {
1312 ehc->i.serror |= serror;
1313 ata_eh_analyze_serror(ap);
1314 } else if (rc != -EOPNOTSUPP)
Tejun Heo4528e4d2006-07-08 20:17:26 +09001315 ehc->i.action |= ATA_EH_HARDRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001316
Tejun Heoe8ee8452006-05-15 21:03:46 +09001317 /* analyze NCQ failure */
1318 ata_eh_analyze_ncq_error(ap);
1319
Tejun Heo022bdb02006-05-15 20:58:22 +09001320 /* any real error trumps AC_ERR_OTHER */
1321 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1322 ehc->i.err_mask &= ~AC_ERR_OTHER;
1323
1324 all_err_mask |= ehc->i.err_mask;
1325
1326 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1327 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1328
1329 if (!(qc->flags & ATA_QCFLAG_FAILED))
1330 continue;
1331
1332 /* inherit upper level err_mask */
1333 qc->err_mask |= ehc->i.err_mask;
1334
Tejun Heo022bdb02006-05-15 20:58:22 +09001335 /* analyze TF */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001336 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
Tejun Heo022bdb02006-05-15 20:58:22 +09001337
1338 /* DEV errors are probably spurious in case of ATA_BUS error */
1339 if (qc->err_mask & AC_ERR_ATA_BUS)
1340 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1341 AC_ERR_INVALID);
1342
1343 /* any real error trumps unknown error */
1344 if (qc->err_mask & ~AC_ERR_OTHER)
1345 qc->err_mask &= ~AC_ERR_OTHER;
1346
1347 /* SENSE_VALID trumps dev/unknown error and revalidation */
1348 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1349 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
Tejun Heo4528e4d2006-07-08 20:17:26 +09001350 ehc->i.action &= ~ATA_EH_REVALIDATE;
Tejun Heo022bdb02006-05-15 20:58:22 +09001351 }
1352
1353 /* accumulate error info */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001354 ehc->i.dev = qc->dev;
Tejun Heo022bdb02006-05-15 20:58:22 +09001355 all_err_mask |= qc->err_mask;
1356 if (qc->flags & ATA_QCFLAG_IO)
1357 is_io = 1;
1358 }
1359
Tejun Heoa20f33f2006-05-16 12:58:24 +09001360 /* enforce default EH actions */
Tejun Heob51e9e52006-06-29 01:29:30 +09001361 if (ap->pflags & ATA_PFLAG_FROZEN ||
Tejun Heoa20f33f2006-05-16 12:58:24 +09001362 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
Tejun Heo4528e4d2006-07-08 20:17:26 +09001363 ehc->i.action |= ATA_EH_SOFTRESET;
Tejun Heoa20f33f2006-05-16 12:58:24 +09001364 else if (all_err_mask)
Tejun Heo4528e4d2006-07-08 20:17:26 +09001365 ehc->i.action |= ATA_EH_REVALIDATE;
Tejun Heo022bdb02006-05-15 20:58:22 +09001366
Tejun Heo47005f22006-06-19 18:27:23 +09001367 /* if we have offending qcs and the associated failed device */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001368 if (ehc->i.dev) {
Tejun Heo47005f22006-06-19 18:27:23 +09001369 /* speed down */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001370 ehc->i.action |= ata_eh_speed_down(ehc->i.dev, is_io,
1371 all_err_mask);
Tejun Heo47005f22006-06-19 18:27:23 +09001372
1373 /* perform per-dev EH action only on the offending device */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001374 ehc->i.dev_action[ehc->i.dev->devno] |=
1375 ehc->i.action & ATA_EH_PERDEV_MASK;
1376 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
Tejun Heo47005f22006-06-19 18:27:23 +09001377 }
1378
Tejun Heo022bdb02006-05-15 20:58:22 +09001379 DPRINTK("EXIT\n");
1380}
1381
1382/**
1383 * ata_eh_report - report error handling to user
1384 * @ap: ATA port EH is going on
1385 *
1386 * Report EH to user.
1387 *
1388 * LOCKING:
1389 * None.
1390 */
1391static void ata_eh_report(struct ata_port *ap)
1392{
1393 struct ata_eh_context *ehc = &ap->eh_context;
1394 const char *frozen, *desc;
1395 int tag, nr_failed = 0;
1396
1397 desc = NULL;
1398 if (ehc->i.desc[0] != '\0')
1399 desc = ehc->i.desc;
1400
1401 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1402 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1403
1404 if (!(qc->flags & ATA_QCFLAG_FAILED))
1405 continue;
1406 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1407 continue;
1408
1409 nr_failed++;
1410 }
1411
1412 if (!nr_failed && !ehc->i.err_mask)
1413 return;
1414
1415 frozen = "";
Tejun Heob51e9e52006-06-29 01:29:30 +09001416 if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heo022bdb02006-05-15 20:58:22 +09001417 frozen = " frozen";
1418
1419 if (ehc->i.dev) {
Tejun Heoe8ee8452006-05-15 21:03:46 +09001420 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
1421 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1422 ehc->i.err_mask, ap->sactive, ehc->i.serror,
1423 ehc->i.action, frozen);
Tejun Heo022bdb02006-05-15 20:58:22 +09001424 if (desc)
1425 ata_dev_printk(ehc->i.dev, KERN_ERR, "(%s)\n", desc);
1426 } else {
Tejun Heoe8ee8452006-05-15 21:03:46 +09001427 ata_port_printk(ap, KERN_ERR, "exception Emask 0x%x "
1428 "SAct 0x%x SErr 0x%x action 0x%x%s\n",
1429 ehc->i.err_mask, ap->sactive, ehc->i.serror,
1430 ehc->i.action, frozen);
Tejun Heo022bdb02006-05-15 20:58:22 +09001431 if (desc)
1432 ata_port_printk(ap, KERN_ERR, "(%s)\n", desc);
1433 }
1434
1435 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1436 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1437
1438 if (!(qc->flags & ATA_QCFLAG_FAILED) || !qc->err_mask)
1439 continue;
1440
1441 ata_dev_printk(qc->dev, KERN_ERR, "tag %d cmd 0x%x "
1442 "Emask 0x%x stat 0x%x err 0x%x (%s)\n",
1443 qc->tag, qc->tf.command, qc->err_mask,
1444 qc->result_tf.command, qc->result_tf.feature,
1445 ata_err_string(qc->err_mask));
1446 }
1447}
1448
Tejun Heod87fa382006-05-31 18:28:24 +09001449static int ata_do_reset(struct ata_port *ap, ata_reset_fn_t reset,
1450 unsigned int *classes)
1451{
1452 int i, rc;
1453
1454 for (i = 0; i < ATA_MAX_DEVICES; i++)
1455 classes[i] = ATA_DEV_UNKNOWN;
1456
1457 rc = reset(ap, classes);
1458 if (rc)
1459 return rc;
1460
1461 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
1462 * is complete and convert all ATA_DEV_UNKNOWN to
1463 * ATA_DEV_NONE.
1464 */
1465 for (i = 0; i < ATA_MAX_DEVICES; i++)
1466 if (classes[i] != ATA_DEV_UNKNOWN)
1467 break;
1468
1469 if (i < ATA_MAX_DEVICES)
1470 for (i = 0; i < ATA_MAX_DEVICES; i++)
1471 if (classes[i] == ATA_DEV_UNKNOWN)
1472 classes[i] = ATA_DEV_NONE;
1473
1474 return 0;
1475}
1476
Tejun Heo664faf02006-05-31 18:27:50 +09001477static int ata_eh_followup_srst_needed(int rc, int classify,
1478 const unsigned int *classes)
1479{
1480 if (rc == -EAGAIN)
1481 return 1;
1482 if (rc != 0)
1483 return 0;
1484 if (classify && classes[0] == ATA_DEV_UNKNOWN)
1485 return 1;
1486 return 0;
1487}
1488
1489static int ata_eh_reset(struct ata_port *ap, int classify,
Tejun Heof5914a42006-05-31 18:27:48 +09001490 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
Tejun Heo022bdb02006-05-15 20:58:22 +09001491 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
1492{
1493 struct ata_eh_context *ehc = &ap->eh_context;
Tejun Heo664faf02006-05-31 18:27:50 +09001494 unsigned int *classes = ehc->classes;
Tejun Heo022bdb02006-05-15 20:58:22 +09001495 int tries = ATA_EH_RESET_TRIES;
Tejun Heo1cdaf532006-07-03 16:07:26 +09001496 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
Tejun Heof5914a42006-05-31 18:27:48 +09001497 unsigned int action;
Tejun Heo022bdb02006-05-15 20:58:22 +09001498 ata_reset_fn_t reset;
Tejun Heo664faf02006-05-31 18:27:50 +09001499 int i, did_followup_srst, rc;
Tejun Heo022bdb02006-05-15 20:58:22 +09001500
Tejun Heo13abf502006-07-10 23:18:46 +09001501 /* about to reset */
1502 ata_eh_about_to_do(ap, NULL, ehc->i.action & ATA_EH_RESET_MASK);
1503
Tejun Heof5914a42006-05-31 18:27:48 +09001504 /* Determine which reset to use and record in ehc->i.action.
1505 * prereset() may examine and modify it.
1506 */
1507 action = ehc->i.action;
1508 ehc->i.action &= ~ATA_EH_RESET_MASK;
Tejun Heo022bdb02006-05-15 20:58:22 +09001509 if (softreset && (!hardreset || (!sata_set_spd_needed(ap) &&
Tejun Heof5914a42006-05-31 18:27:48 +09001510 !(action & ATA_EH_HARDRESET))))
1511 ehc->i.action |= ATA_EH_SOFTRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001512 else
Tejun Heof5914a42006-05-31 18:27:48 +09001513 ehc->i.action |= ATA_EH_HARDRESET;
1514
1515 if (prereset) {
1516 rc = prereset(ap);
1517 if (rc) {
Alan Coxc9619222006-09-26 17:53:38 +01001518 if (rc == -ENOENT) {
1519 ata_port_printk(ap, KERN_DEBUG, "port disabled. ignoring.\n");
1520 ap->eh_context.i.action &= ~ATA_EH_RESET_MASK;
1521 } else
1522 ata_port_printk(ap, KERN_ERR,
Tejun Heof5914a42006-05-31 18:27:48 +09001523 "prereset failed (errno=%d)\n", rc);
1524 return rc;
1525 }
1526 }
1527
1528 /* prereset() might have modified ehc->i.action */
1529 if (ehc->i.action & ATA_EH_HARDRESET)
Tejun Heo022bdb02006-05-15 20:58:22 +09001530 reset = hardreset;
Tejun Heof5914a42006-05-31 18:27:48 +09001531 else if (ehc->i.action & ATA_EH_SOFTRESET)
1532 reset = softreset;
1533 else {
1534 /* prereset told us not to reset, bang classes and return */
1535 for (i = 0; i < ATA_MAX_DEVICES; i++)
1536 classes[i] = ATA_DEV_NONE;
1537 return 0;
1538 }
1539
1540 /* did prereset() screw up? if so, fix up to avoid oopsing */
1541 if (!reset) {
1542 ata_port_printk(ap, KERN_ERR, "BUG: prereset() requested "
1543 "invalid reset type\n");
1544 if (softreset)
1545 reset = softreset;
1546 else
1547 reset = hardreset;
1548 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001549
1550 retry:
Tejun Heo3e706392006-05-31 18:28:11 +09001551 /* shut up during boot probing */
1552 if (verbose)
1553 ata_port_printk(ap, KERN_INFO, "%s resetting port\n",
1554 reset == softreset ? "soft" : "hard");
Tejun Heo022bdb02006-05-15 20:58:22 +09001555
Tejun Heo13abf502006-07-10 23:18:46 +09001556 /* mark that this EH session started with reset */
Tejun Heo022bdb02006-05-15 20:58:22 +09001557 ehc->i.flags |= ATA_EHI_DID_RESET;
1558
1559 rc = ata_do_reset(ap, reset, classes);
1560
Tejun Heo664faf02006-05-31 18:27:50 +09001561 did_followup_srst = 0;
1562 if (reset == hardreset &&
1563 ata_eh_followup_srst_needed(rc, classify, classes)) {
1564 /* okay, let's do follow-up softreset */
1565 did_followup_srst = 1;
1566 reset = softreset;
1567
1568 if (!reset) {
1569 ata_port_printk(ap, KERN_ERR,
1570 "follow-up softreset required "
1571 "but no softreset avaliable\n");
1572 return -EINVAL;
1573 }
1574
Tejun Heo47005f22006-06-19 18:27:23 +09001575 ata_eh_about_to_do(ap, NULL, ATA_EH_RESET_MASK);
Tejun Heo664faf02006-05-31 18:27:50 +09001576 rc = ata_do_reset(ap, reset, classes);
1577
1578 if (rc == 0 && classify &&
1579 classes[0] == ATA_DEV_UNKNOWN) {
1580 ata_port_printk(ap, KERN_ERR,
1581 "classification failed\n");
1582 return -EINVAL;
1583 }
1584 }
1585
Tejun Heo022bdb02006-05-15 20:58:22 +09001586 if (rc && --tries) {
Tejun Heo664faf02006-05-31 18:27:50 +09001587 const char *type;
1588
1589 if (reset == softreset) {
1590 if (did_followup_srst)
1591 type = "follow-up soft";
1592 else
1593 type = "soft";
1594 } else
1595 type = "hard";
1596
Tejun Heo022bdb02006-05-15 20:58:22 +09001597 ata_port_printk(ap, KERN_WARNING,
Tejun Heo664faf02006-05-31 18:27:50 +09001598 "%sreset failed, retrying in 5 secs\n", type);
Tejun Heo022bdb02006-05-15 20:58:22 +09001599 ssleep(5);
1600
1601 if (reset == hardreset)
1602 sata_down_spd_limit(ap);
1603 if (hardreset)
1604 reset = hardreset;
1605 goto retry;
1606 }
1607
1608 if (rc == 0) {
Tejun Heo20952b62006-05-31 18:27:23 +09001609 /* After the reset, the device state is PIO 0 and the
1610 * controller state is undefined. Record the mode.
1611 */
1612 for (i = 0; i < ATA_MAX_DEVICES; i++)
1613 ap->device[i].pio_mode = XFER_PIO_0;
1614
Tejun Heo022bdb02006-05-15 20:58:22 +09001615 if (postreset)
1616 postreset(ap, classes);
1617
1618 /* reset successful, schedule revalidation */
Tejun Heo13abf502006-07-10 23:18:46 +09001619 ata_eh_done(ap, NULL, ehc->i.action & ATA_EH_RESET_MASK);
Tejun Heo022bdb02006-05-15 20:58:22 +09001620 ehc->i.action |= ATA_EH_REVALIDATE;
1621 }
1622
1623 return rc;
1624}
1625
Tejun Heo084fe632006-05-31 18:28:03 +09001626static int ata_eh_revalidate_and_attach(struct ata_port *ap,
1627 struct ata_device **r_failed_dev)
Tejun Heo022bdb02006-05-15 20:58:22 +09001628{
1629 struct ata_eh_context *ehc = &ap->eh_context;
1630 struct ata_device *dev;
Tejun Heo084fe632006-05-31 18:28:03 +09001631 unsigned long flags;
Tejun Heo022bdb02006-05-15 20:58:22 +09001632 int i, rc = 0;
1633
1634 DPRINTK("ENTER\n");
1635
1636 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heobff04642006-11-10 18:08:10 +09001637 unsigned int action, readid_flags = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09001638
Tejun Heo47005f22006-06-19 18:27:23 +09001639 dev = &ap->device[i];
Tejun Heo64f65ca2006-06-24 20:30:18 +09001640 action = ata_eh_dev_action(dev);
Tejun Heo47005f22006-06-19 18:27:23 +09001641
Tejun Heobff04642006-11-10 18:08:10 +09001642 if (ehc->i.flags & ATA_EHI_DID_RESET)
1643 readid_flags |= ATA_READID_POSTRESET;
1644
Tejun Heo02670bf2006-07-03 16:07:26 +09001645 if (action & ATA_EH_REVALIDATE && ata_dev_ready(dev)) {
Tejun Heo022bdb02006-05-15 20:58:22 +09001646 if (ata_port_offline(ap)) {
1647 rc = -EIO;
1648 break;
1649 }
1650
Tejun Heo47005f22006-06-19 18:27:23 +09001651 ata_eh_about_to_do(ap, dev, ATA_EH_REVALIDATE);
Tejun Heobff04642006-11-10 18:08:10 +09001652 rc = ata_dev_revalidate(dev, readid_flags);
Tejun Heo022bdb02006-05-15 20:58:22 +09001653 if (rc)
1654 break;
1655
Tejun Heo47005f22006-06-19 18:27:23 +09001656 ata_eh_done(ap, dev, ATA_EH_REVALIDATE);
1657
Tejun Heobaa1e782006-11-01 18:39:27 +09001658 /* Configuration may have changed, reconfigure
1659 * transfer mode.
1660 */
1661 ehc->i.flags |= ATA_EHI_SETMODE;
1662
zhao, forrest3057ac32006-06-12 12:01:34 +08001663 /* schedule the scsi_rescan_device() here */
1664 queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
Tejun Heo084fe632006-05-31 18:28:03 +09001665 } else if (dev->class == ATA_DEV_UNKNOWN &&
1666 ehc->tries[dev->devno] &&
1667 ata_class_enabled(ehc->classes[dev->devno])) {
1668 dev->class = ehc->classes[dev->devno];
1669
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001670 if (ap->flags & ATA_FLAG_DETECT_POLLING)
1671 readid_flags |= ATA_READID_DETECT;
1672
Tejun Heobff04642006-11-10 18:08:10 +09001673 rc = ata_dev_read_id(dev, &dev->class, readid_flags,
1674 dev->id);
Tejun Heoefdaedc2006-11-01 18:38:52 +09001675 if (rc == 0) {
1676 ehc->i.flags |= ATA_EHI_PRINTINFO;
1677 rc = ata_dev_configure(dev);
1678 ehc->i.flags &= ~ATA_EHI_PRINTINFO;
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001679 } else if (rc == -ENOENT) {
1680 /* IDENTIFY was issued to non-existent
1681 * device. No need to reset. Just
1682 * thaw and kill the device.
1683 */
1684 ata_eh_thaw_port(ap);
1685 dev->class = ATA_DEV_UNKNOWN;
1686 rc = 0;
Tejun Heoefdaedc2006-11-01 18:38:52 +09001687 }
Tejun Heo084fe632006-05-31 18:28:03 +09001688
1689 if (rc) {
1690 dev->class = ATA_DEV_UNKNOWN;
1691 break;
1692 }
1693
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001694 if (ata_dev_enabled(dev)) {
1695 spin_lock_irqsave(ap->lock, flags);
1696 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
1697 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heobaa1e782006-11-01 18:39:27 +09001698
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001699 /* new device discovered, configure xfermode */
1700 ehc->i.flags |= ATA_EHI_SETMODE;
1701 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001702 }
1703 }
1704
Tejun Heo47005f22006-06-19 18:27:23 +09001705 if (rc)
Tejun Heo022bdb02006-05-15 20:58:22 +09001706 *r_failed_dev = dev;
1707
1708 DPRINTK("EXIT\n");
1709 return rc;
1710}
1711
Tejun Heo02670bf2006-07-03 16:07:26 +09001712/**
1713 * ata_eh_suspend - handle suspend EH action
1714 * @ap: target host port
1715 * @r_failed_dev: result parameter to indicate failing device
1716 *
1717 * Handle suspend EH action. Disk devices are spinned down and
1718 * other types of devices are just marked suspended. Once
1719 * suspended, no EH action to the device is allowed until it is
1720 * resumed.
1721 *
1722 * LOCKING:
1723 * Kernel thread context (may sleep).
1724 *
1725 * RETURNS:
1726 * 0 on success, -errno otherwise
1727 */
1728static int ata_eh_suspend(struct ata_port *ap, struct ata_device **r_failed_dev)
1729{
1730 struct ata_device *dev;
1731 int i, rc = 0;
1732
1733 DPRINTK("ENTER\n");
1734
1735 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1736 unsigned long flags;
1737 unsigned int action, err_mask;
1738
1739 dev = &ap->device[i];
1740 action = ata_eh_dev_action(dev);
1741
1742 if (!ata_dev_enabled(dev) || !(action & ATA_EH_SUSPEND))
1743 continue;
1744
1745 WARN_ON(dev->flags & ATA_DFLAG_SUSPENDED);
1746
1747 ata_eh_about_to_do(ap, dev, ATA_EH_SUSPEND);
1748
1749 if (dev->class == ATA_DEV_ATA && !(action & ATA_EH_PM_FREEZE)) {
1750 /* flush cache */
1751 rc = ata_flush_cache(dev);
1752 if (rc)
1753 break;
1754
1755 /* spin down */
1756 err_mask = ata_do_simple_cmd(dev, ATA_CMD_STANDBYNOW1);
1757 if (err_mask) {
1758 ata_dev_printk(dev, KERN_ERR, "failed to "
1759 "spin down (err_mask=0x%x)\n",
1760 err_mask);
1761 rc = -EIO;
1762 break;
1763 }
1764 }
1765
1766 spin_lock_irqsave(ap->lock, flags);
1767 dev->flags |= ATA_DFLAG_SUSPENDED;
1768 spin_unlock_irqrestore(ap->lock, flags);
1769
1770 ata_eh_done(ap, dev, ATA_EH_SUSPEND);
1771 }
1772
1773 if (rc)
1774 *r_failed_dev = dev;
1775
1776 DPRINTK("EXIT\n");
1777 return 0;
1778}
1779
1780/**
1781 * ata_eh_prep_resume - prep for resume EH action
1782 * @ap: target host port
1783 *
1784 * Clear SUSPENDED in preparation for scheduled resume actions.
1785 * This allows other parts of EH to access the devices being
1786 * resumed.
1787 *
1788 * LOCKING:
1789 * Kernel thread context (may sleep).
1790 */
1791static void ata_eh_prep_resume(struct ata_port *ap)
1792{
1793 struct ata_device *dev;
1794 unsigned long flags;
1795 int i;
1796
1797 DPRINTK("ENTER\n");
1798
1799 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1800 unsigned int action;
1801
1802 dev = &ap->device[i];
1803 action = ata_eh_dev_action(dev);
1804
1805 if (!ata_dev_enabled(dev) || !(action & ATA_EH_RESUME))
1806 continue;
1807
1808 spin_lock_irqsave(ap->lock, flags);
1809 dev->flags &= ~ATA_DFLAG_SUSPENDED;
1810 spin_unlock_irqrestore(ap->lock, flags);
1811 }
1812
1813 DPRINTK("EXIT\n");
1814}
1815
1816/**
1817 * ata_eh_resume - handle resume EH action
1818 * @ap: target host port
1819 * @r_failed_dev: result parameter to indicate failing device
1820 *
1821 * Handle resume EH action. Target devices are already reset and
1822 * revalidated. Spinning up is the only operation left.
1823 *
1824 * LOCKING:
1825 * Kernel thread context (may sleep).
1826 *
1827 * RETURNS:
1828 * 0 on success, -errno otherwise
1829 */
1830static int ata_eh_resume(struct ata_port *ap, struct ata_device **r_failed_dev)
1831{
1832 struct ata_device *dev;
1833 int i, rc = 0;
1834
1835 DPRINTK("ENTER\n");
1836
1837 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1838 unsigned int action, err_mask;
1839
1840 dev = &ap->device[i];
1841 action = ata_eh_dev_action(dev);
1842
1843 if (!ata_dev_enabled(dev) || !(action & ATA_EH_RESUME))
1844 continue;
1845
1846 ata_eh_about_to_do(ap, dev, ATA_EH_RESUME);
1847
1848 if (dev->class == ATA_DEV_ATA && !(action & ATA_EH_PM_FREEZE)) {
1849 err_mask = ata_do_simple_cmd(dev,
1850 ATA_CMD_IDLEIMMEDIATE);
1851 if (err_mask) {
1852 ata_dev_printk(dev, KERN_ERR, "failed to "
1853 "spin up (err_mask=0x%x)\n",
1854 err_mask);
1855 rc = -EIO;
1856 break;
1857 }
1858 }
1859
1860 ata_eh_done(ap, dev, ATA_EH_RESUME);
1861 }
1862
1863 if (rc)
1864 *r_failed_dev = dev;
1865
1866 DPRINTK("EXIT\n");
1867 return 0;
1868}
1869
Tejun Heo022bdb02006-05-15 20:58:22 +09001870static int ata_port_nr_enabled(struct ata_port *ap)
1871{
1872 int i, cnt = 0;
1873
1874 for (i = 0; i < ATA_MAX_DEVICES; i++)
1875 if (ata_dev_enabled(&ap->device[i]))
1876 cnt++;
1877 return cnt;
1878}
1879
Tejun Heo084fe632006-05-31 18:28:03 +09001880static int ata_port_nr_vacant(struct ata_port *ap)
1881{
1882 int i, cnt = 0;
1883
1884 for (i = 0; i < ATA_MAX_DEVICES; i++)
1885 if (ap->device[i].class == ATA_DEV_UNKNOWN)
1886 cnt++;
1887 return cnt;
1888}
1889
1890static int ata_eh_skip_recovery(struct ata_port *ap)
1891{
1892 struct ata_eh_context *ehc = &ap->eh_context;
1893 int i;
1894
Tejun Heo02670bf2006-07-03 16:07:26 +09001895 /* skip if all possible devices are suspended */
1896 for (i = 0; i < ata_port_max_devices(ap); i++) {
1897 struct ata_device *dev = &ap->device[i];
1898
Tejun Heo7c8c2cf2006-07-10 23:18:23 +09001899 if (!(dev->flags & ATA_DFLAG_SUSPENDED))
Tejun Heo02670bf2006-07-03 16:07:26 +09001900 break;
1901 }
1902
1903 if (i == ata_port_max_devices(ap))
1904 return 1;
1905
Tejun Heo7c8c2cf2006-07-10 23:18:23 +09001906 /* thaw frozen port, resume link and recover failed devices */
1907 if ((ap->pflags & ATA_PFLAG_FROZEN) ||
1908 (ehc->i.flags & ATA_EHI_RESUME_LINK) || ata_port_nr_enabled(ap))
Tejun Heo084fe632006-05-31 18:28:03 +09001909 return 0;
1910
1911 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
1912 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1913 struct ata_device *dev = &ap->device[i];
1914
1915 if (dev->class == ATA_DEV_UNKNOWN &&
1916 ehc->classes[dev->devno] != ATA_DEV_NONE)
1917 return 0;
1918 }
1919
1920 return 1;
1921}
1922
Tejun Heo022bdb02006-05-15 20:58:22 +09001923/**
1924 * ata_eh_recover - recover host port after error
1925 * @ap: host port to recover
Tejun Heof5914a42006-05-31 18:27:48 +09001926 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09001927 * @softreset: softreset method (can be NULL)
1928 * @hardreset: hardreset method (can be NULL)
1929 * @postreset: postreset method (can be NULL)
1930 *
1931 * This is the alpha and omega, eum and yang, heart and soul of
1932 * libata exception handling. On entry, actions required to
Tejun Heo084fe632006-05-31 18:28:03 +09001933 * recover the port and hotplug requests are recorded in
1934 * eh_context. This function executes all the operations with
1935 * appropriate retrials and fallbacks to resurrect failed
1936 * devices, detach goners and greet newcomers.
Tejun Heo022bdb02006-05-15 20:58:22 +09001937 *
1938 * LOCKING:
1939 * Kernel thread context (may sleep).
1940 *
1941 * RETURNS:
1942 * 0 on success, -errno on failure.
1943 */
Tejun Heof5914a42006-05-31 18:27:48 +09001944static int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
1945 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
Tejun Heo022bdb02006-05-15 20:58:22 +09001946 ata_postreset_fn_t postreset)
1947{
1948 struct ata_eh_context *ehc = &ap->eh_context;
1949 struct ata_device *dev;
1950 int down_xfermask, i, rc;
1951
1952 DPRINTK("ENTER\n");
1953
1954 /* prep for recovery */
1955 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1956 dev = &ap->device[i];
1957
1958 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
Tejun Heo084fe632006-05-31 18:28:03 +09001959
1960 /* process hotplug request */
1961 if (dev->flags & ATA_DFLAG_DETACH)
1962 ata_eh_detach_dev(dev);
1963
1964 if (!ata_dev_enabled(dev) &&
1965 ((ehc->i.probe_mask & (1 << dev->devno)) &&
1966 !(ehc->did_probe_mask & (1 << dev->devno)))) {
1967 ata_eh_detach_dev(dev);
1968 ata_dev_init(dev);
1969 ehc->did_probe_mask |= (1 << dev->devno);
1970 ehc->i.action |= ATA_EH_SOFTRESET;
1971 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001972 }
1973
1974 retry:
1975 down_xfermask = 0;
1976 rc = 0;
1977
Tejun Heoaeb2ecd2006-06-12 14:11:43 +09001978 /* if UNLOADING, finish immediately */
Tejun Heob51e9e52006-06-29 01:29:30 +09001979 if (ap->pflags & ATA_PFLAG_UNLOADING)
Tejun Heoaeb2ecd2006-06-12 14:11:43 +09001980 goto out;
1981
Tejun Heo02670bf2006-07-03 16:07:26 +09001982 /* prep for resume */
1983 ata_eh_prep_resume(ap);
1984
Tejun Heo022bdb02006-05-15 20:58:22 +09001985 /* skip EH if possible. */
Tejun Heo084fe632006-05-31 18:28:03 +09001986 if (ata_eh_skip_recovery(ap))
Tejun Heo022bdb02006-05-15 20:58:22 +09001987 ehc->i.action = 0;
1988
Tejun Heo084fe632006-05-31 18:28:03 +09001989 for (i = 0; i < ATA_MAX_DEVICES; i++)
1990 ehc->classes[i] = ATA_DEV_UNKNOWN;
1991
Tejun Heo022bdb02006-05-15 20:58:22 +09001992 /* reset */
1993 if (ehc->i.action & ATA_EH_RESET_MASK) {
1994 ata_eh_freeze_port(ap);
1995
Tejun Heo084fe632006-05-31 18:28:03 +09001996 rc = ata_eh_reset(ap, ata_port_nr_vacant(ap), prereset,
1997 softreset, hardreset, postreset);
Tejun Heo022bdb02006-05-15 20:58:22 +09001998 if (rc) {
1999 ata_port_printk(ap, KERN_ERR,
2000 "reset failed, giving up\n");
2001 goto out;
2002 }
2003
2004 ata_eh_thaw_port(ap);
2005 }
2006
Tejun Heo084fe632006-05-31 18:28:03 +09002007 /* revalidate existing devices and attach new ones */
2008 rc = ata_eh_revalidate_and_attach(ap, &dev);
Tejun Heo022bdb02006-05-15 20:58:22 +09002009 if (rc)
2010 goto dev_fail;
2011
Tejun Heo02670bf2006-07-03 16:07:26 +09002012 /* resume devices */
2013 rc = ata_eh_resume(ap, &dev);
2014 if (rc)
2015 goto dev_fail;
2016
Tejun Heobaa1e782006-11-01 18:39:27 +09002017 /* configure transfer mode if necessary */
2018 if (ehc->i.flags & ATA_EHI_SETMODE) {
Tejun Heo022bdb02006-05-15 20:58:22 +09002019 rc = ata_set_mode(ap, &dev);
2020 if (rc) {
2021 down_xfermask = 1;
2022 goto dev_fail;
2023 }
Tejun Heobaa1e782006-11-01 18:39:27 +09002024 ehc->i.flags &= ~ATA_EHI_SETMODE;
Tejun Heo022bdb02006-05-15 20:58:22 +09002025 }
2026
Tejun Heo02670bf2006-07-03 16:07:26 +09002027 /* suspend devices */
2028 rc = ata_eh_suspend(ap, &dev);
2029 if (rc)
2030 goto dev_fail;
2031
Tejun Heo022bdb02006-05-15 20:58:22 +09002032 goto out;
2033
2034 dev_fail:
2035 switch (rc) {
2036 case -ENODEV:
Tejun Heo084fe632006-05-31 18:28:03 +09002037 /* device missing, schedule probing */
2038 ehc->i.probe_mask |= (1 << dev->devno);
Tejun Heo022bdb02006-05-15 20:58:22 +09002039 case -EINVAL:
2040 ehc->tries[dev->devno] = 0;
2041 break;
2042 case -EIO:
2043 sata_down_spd_limit(ap);
2044 default:
2045 ehc->tries[dev->devno]--;
2046 if (down_xfermask &&
2047 ata_down_xfermask_limit(dev, ehc->tries[dev->devno] == 1))
2048 ehc->tries[dev->devno] = 0;
2049 }
2050
Tejun Heo084fe632006-05-31 18:28:03 +09002051 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2052 /* disable device if it has used up all its chances */
Tejun Heo022bdb02006-05-15 20:58:22 +09002053 ata_dev_disable(dev);
2054
Tejun Heo084fe632006-05-31 18:28:03 +09002055 /* detach if offline */
2056 if (ata_port_offline(ap))
2057 ata_eh_detach_dev(dev);
2058
2059 /* probe if requested */
2060 if ((ehc->i.probe_mask & (1 << dev->devno)) &&
2061 !(ehc->did_probe_mask & (1 << dev->devno))) {
2062 ata_eh_detach_dev(dev);
2063 ata_dev_init(dev);
2064
2065 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2066 ehc->did_probe_mask |= (1 << dev->devno);
2067 ehc->i.action |= ATA_EH_SOFTRESET;
2068 }
2069 } else {
2070 /* soft didn't work? be haaaaard */
2071 if (ehc->i.flags & ATA_EHI_DID_RESET)
2072 ehc->i.action |= ATA_EH_HARDRESET;
2073 else
2074 ehc->i.action |= ATA_EH_SOFTRESET;
2075 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002076
2077 if (ata_port_nr_enabled(ap)) {
2078 ata_port_printk(ap, KERN_WARNING, "failed to recover some "
2079 "devices, retrying in 5 secs\n");
2080 ssleep(5);
2081 } else {
2082 /* no device left, repeat fast */
2083 msleep(500);
2084 }
2085
2086 goto retry;
2087
2088 out:
2089 if (rc) {
2090 for (i = 0; i < ATA_MAX_DEVICES; i++)
2091 ata_dev_disable(&ap->device[i]);
2092 }
2093
2094 DPRINTK("EXIT, rc=%d\n", rc);
2095 return rc;
2096}
2097
2098/**
2099 * ata_eh_finish - finish up EH
2100 * @ap: host port to finish EH for
2101 *
2102 * Recovery is complete. Clean up EH states and retry or finish
2103 * failed qcs.
2104 *
2105 * LOCKING:
2106 * None.
2107 */
2108static void ata_eh_finish(struct ata_port *ap)
2109{
2110 int tag;
2111
2112 /* retry or finish qcs */
2113 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2114 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2115
2116 if (!(qc->flags & ATA_QCFLAG_FAILED))
2117 continue;
2118
2119 if (qc->err_mask) {
2120 /* FIXME: Once EH migration is complete,
2121 * generate sense data in this function,
2122 * considering both err_mask and tf.
2123 */
2124 if (qc->err_mask & AC_ERR_INVALID)
2125 ata_eh_qc_complete(qc);
2126 else
2127 ata_eh_qc_retry(qc);
2128 } else {
2129 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2130 ata_eh_qc_complete(qc);
2131 } else {
2132 /* feed zero TF to sense generation */
2133 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2134 ata_eh_qc_retry(qc);
2135 }
2136 }
2137 }
2138}
2139
2140/**
2141 * ata_do_eh - do standard error handling
2142 * @ap: host port to handle error for
Tejun Heof5914a42006-05-31 18:27:48 +09002143 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09002144 * @softreset: softreset method (can be NULL)
2145 * @hardreset: hardreset method (can be NULL)
2146 * @postreset: postreset method (can be NULL)
2147 *
2148 * Perform standard error handling sequence.
2149 *
2150 * LOCKING:
2151 * Kernel thread context (may sleep).
2152 */
Tejun Heof5914a42006-05-31 18:27:48 +09002153void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2154 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2155 ata_postreset_fn_t postreset)
Tejun Heo022bdb02006-05-15 20:58:22 +09002156{
Tejun Heo1cdaf532006-07-03 16:07:26 +09002157 ata_eh_autopsy(ap);
2158 ata_eh_report(ap);
Tejun Heof5914a42006-05-31 18:27:48 +09002159 ata_eh_recover(ap, prereset, softreset, hardreset, postreset);
Tejun Heo022bdb02006-05-15 20:58:22 +09002160 ata_eh_finish(ap);
2161}
Tejun Heo500530f2006-07-03 16:07:27 +09002162
2163/**
2164 * ata_eh_handle_port_suspend - perform port suspend operation
2165 * @ap: port to suspend
2166 *
2167 * Suspend @ap.
2168 *
2169 * LOCKING:
2170 * Kernel thread context (may sleep).
2171 */
2172static void ata_eh_handle_port_suspend(struct ata_port *ap)
2173{
2174 unsigned long flags;
2175 int rc = 0;
2176
2177 /* are we suspending? */
2178 spin_lock_irqsave(ap->lock, flags);
2179 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2180 ap->pm_mesg.event == PM_EVENT_ON) {
2181 spin_unlock_irqrestore(ap->lock, flags);
2182 return;
2183 }
2184 spin_unlock_irqrestore(ap->lock, flags);
2185
2186 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
2187
2188 /* suspend */
2189 ata_eh_freeze_port(ap);
2190
2191 if (ap->ops->port_suspend)
2192 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
2193
2194 /* report result */
2195 spin_lock_irqsave(ap->lock, flags);
2196
2197 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
2198 if (rc == 0)
2199 ap->pflags |= ATA_PFLAG_SUSPENDED;
2200 else
2201 ata_port_schedule_eh(ap);
2202
2203 if (ap->pm_result) {
2204 *ap->pm_result = rc;
2205 ap->pm_result = NULL;
2206 }
2207
2208 spin_unlock_irqrestore(ap->lock, flags);
2209
2210 return;
2211}
2212
2213/**
2214 * ata_eh_handle_port_resume - perform port resume operation
2215 * @ap: port to resume
2216 *
2217 * Resume @ap.
2218 *
2219 * This function also waits upto one second until all devices
2220 * hanging off this port requests resume EH action. This is to
2221 * prevent invoking EH and thus reset multiple times on resume.
2222 *
2223 * On DPM resume, where some of devices might not be resumed
2224 * together, this may delay port resume upto one second, but such
2225 * DPM resumes are rare and 1 sec delay isn't too bad.
2226 *
2227 * LOCKING:
2228 * Kernel thread context (may sleep).
2229 */
2230static void ata_eh_handle_port_resume(struct ata_port *ap)
2231{
2232 unsigned long timeout;
2233 unsigned long flags;
2234 int i, rc = 0;
2235
2236 /* are we resuming? */
2237 spin_lock_irqsave(ap->lock, flags);
2238 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2239 ap->pm_mesg.event != PM_EVENT_ON) {
2240 spin_unlock_irqrestore(ap->lock, flags);
2241 return;
2242 }
2243 spin_unlock_irqrestore(ap->lock, flags);
2244
2245 /* spurious? */
2246 if (!(ap->pflags & ATA_PFLAG_SUSPENDED))
2247 goto done;
2248
2249 if (ap->ops->port_resume)
2250 rc = ap->ops->port_resume(ap);
2251
2252 /* give devices time to request EH */
2253 timeout = jiffies + HZ; /* 1s max */
2254 while (1) {
2255 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2256 struct ata_device *dev = &ap->device[i];
2257 unsigned int action = ata_eh_dev_action(dev);
2258
2259 if ((dev->flags & ATA_DFLAG_SUSPENDED) &&
2260 !(action & ATA_EH_RESUME))
2261 break;
2262 }
2263
2264 if (i == ATA_MAX_DEVICES || time_after(jiffies, timeout))
2265 break;
2266 msleep(10);
2267 }
2268
2269 done:
2270 spin_lock_irqsave(ap->lock, flags);
2271 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
2272 if (ap->pm_result) {
2273 *ap->pm_result = rc;
2274 ap->pm_result = NULL;
2275 }
2276 spin_unlock_irqrestore(ap->lock, flags);
2277}