blob: 1d3b0dccfb0a5821ac0854d670c4331f681a6fbc [file] [log] [blame]
Tejun Heoece1d632006-04-02 18:51:53 +09001/*
2 * libata-eh.c - libata error handling
3 *
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
7 *
8 * Copyright 2006 Tejun Heo <htejun@gmail.com>
9 *
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; see the file COPYING. If not, write to
23 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
24 * USA.
25 *
26 *
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
29 *
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
32 *
33 */
34
Tejun Heoece1d632006-04-02 18:51:53 +090035#include <linux/kernel.h>
36#include <scsi/scsi.h>
37#include <scsi/scsi_host.h>
38#include <scsi/scsi_eh.h>
39#include <scsi/scsi_device.h>
40#include <scsi/scsi_cmnd.h>
Jeff Garzikc6fd2802006-08-10 07:31:37 -040041#include "../scsi/scsi_transport_api.h"
Tejun Heoece1d632006-04-02 18:51:53 +090042
43#include <linux/libata.h>
44
45#include "libata.h"
46
Tejun Heo7d47e8d2007-02-02 16:22:31 +090047enum {
48 ATA_EH_SPDN_NCQ_OFF = (1 << 0),
49 ATA_EH_SPDN_SPEED_DOWN = (1 << 1),
50 ATA_EH_SPDN_FALLBACK_TO_PIO = (1 << 2),
51};
52
Tejun Heo31daabd2007-02-02 16:50:52 +090053/* Waiting in ->prereset can never be reliable. It's sometimes nice
54 * to wait there but it can't be depended upon; otherwise, we wouldn't
55 * be resetting. Just give it enough time for most drives to spin up.
56 */
57enum {
58 ATA_EH_PRERESET_TIMEOUT = 10 * HZ,
Tejun Heo5ddf24c2007-07-16 14:29:41 +090059 ATA_EH_FASTDRAIN_INTERVAL = 3 * HZ,
Tejun Heo31daabd2007-02-02 16:50:52 +090060};
61
62/* The following table determines how we sequence resets. Each entry
63 * represents timeout for that try. The first try can be soft or
64 * hardreset. All others are hardreset if available. In most cases
65 * the first reset w/ 10sec timeout should succeed. Following entries
66 * are mostly for error handling, hotplug and retarded devices.
67 */
68static const unsigned long ata_eh_reset_timeouts[] = {
69 10 * HZ, /* most drives spin up by 10sec */
70 10 * HZ, /* > 99% working drives spin up before 20sec */
71 35 * HZ, /* give > 30 secs of idleness for retarded devices */
72 5 * HZ, /* and sweet one last chance */
73 /* > 1 min has elapsed, give up */
74};
75
Tejun Heoad9e2762006-05-15 20:58:12 +090076static void __ata_port_freeze(struct ata_port *ap);
Tejun Heo6ffa01d2007-03-02 17:32:47 +090077#ifdef CONFIG_PM
Tejun Heo500530f2006-07-03 16:07:27 +090078static void ata_eh_handle_port_suspend(struct ata_port *ap);
79static void ata_eh_handle_port_resume(struct ata_port *ap);
Tejun Heo6ffa01d2007-03-02 17:32:47 +090080#else /* CONFIG_PM */
81static void ata_eh_handle_port_suspend(struct ata_port *ap)
82{ }
83
84static void ata_eh_handle_port_resume(struct ata_port *ap)
85{ }
Tejun Heo6ffa01d2007-03-02 17:32:47 +090086#endif /* CONFIG_PM */
Tejun Heoad9e2762006-05-15 20:58:12 +090087
Tejun Heob64bbc32007-07-16 14:29:39 +090088static void __ata_ehi_pushv_desc(struct ata_eh_info *ehi, const char *fmt,
89 va_list args)
90{
91 ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
92 ATA_EH_DESC_LEN - ehi->desc_len,
93 fmt, args);
94}
95
96/**
97 * __ata_ehi_push_desc - push error description without adding separator
98 * @ehi: target EHI
99 * @fmt: printf format string
100 *
101 * Format string according to @fmt and append it to @ehi->desc.
102 *
103 * LOCKING:
104 * spin_lock_irqsave(host lock)
105 */
106void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
107{
108 va_list args;
109
110 va_start(args, fmt);
111 __ata_ehi_pushv_desc(ehi, fmt, args);
112 va_end(args);
113}
114
115/**
116 * ata_ehi_push_desc - push error description with separator
117 * @ehi: target EHI
118 * @fmt: printf format string
119 *
120 * Format string according to @fmt and append it to @ehi->desc.
121 * If @ehi->desc is not empty, ", " is added in-between.
122 *
123 * LOCKING:
124 * spin_lock_irqsave(host lock)
125 */
126void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
127{
128 va_list args;
129
130 if (ehi->desc_len)
131 __ata_ehi_push_desc(ehi, ", ");
132
133 va_start(args, fmt);
134 __ata_ehi_pushv_desc(ehi, fmt, args);
135 va_end(args);
136}
137
138/**
139 * ata_ehi_clear_desc - clean error description
140 * @ehi: target EHI
141 *
142 * Clear @ehi->desc.
143 *
144 * LOCKING:
145 * spin_lock_irqsave(host lock)
146 */
147void ata_ehi_clear_desc(struct ata_eh_info *ehi)
148{
149 ehi->desc[0] = '\0';
150 ehi->desc_len = 0;
151}
152
Tejun Heocbcdd872007-08-18 13:14:55 +0900153/**
154 * ata_port_desc - append port description
155 * @ap: target ATA port
156 * @fmt: printf format string
157 *
158 * Format string according to @fmt and append it to port
159 * description. If port description is not empty, " " is added
160 * in-between. This function is to be used while initializing
161 * ata_host. The description is printed on host registration.
162 *
163 * LOCKING:
164 * None.
165 */
166void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
167{
168 va_list args;
169
170 WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
171
172 if (ap->link.eh_info.desc_len)
173 __ata_ehi_push_desc(&ap->link.eh_info, " ");
174
175 va_start(args, fmt);
176 __ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
177 va_end(args);
178}
179
180#ifdef CONFIG_PCI
181
182/**
183 * ata_port_pbar_desc - append PCI BAR description
184 * @ap: target ATA port
185 * @bar: target PCI BAR
186 * @offset: offset into PCI BAR
187 * @name: name of the area
188 *
189 * If @offset is negative, this function formats a string which
190 * contains the name, address, size and type of the BAR and
191 * appends it to the port description. If @offset is zero or
192 * positive, only name and offsetted address is appended.
193 *
194 * LOCKING:
195 * None.
196 */
197void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
198 const char *name)
199{
200 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
201 char *type = "";
202 unsigned long long start, len;
203
204 if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
205 type = "m";
206 else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
207 type = "i";
208
209 start = (unsigned long long)pci_resource_start(pdev, bar);
210 len = (unsigned long long)pci_resource_len(pdev, bar);
211
212 if (offset < 0)
213 ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
214 else
215 ata_port_desc(ap, "%s 0x%llx", name, start + offset);
216}
217
218#endif /* CONFIG_PCI */
219
Tejun Heo0c247c52006-05-15 20:58:19 +0900220static void ata_ering_record(struct ata_ering *ering, int is_io,
221 unsigned int err_mask)
222{
223 struct ata_ering_entry *ent;
224
225 WARN_ON(!err_mask);
226
227 ering->cursor++;
228 ering->cursor %= ATA_ERING_SIZE;
229
230 ent = &ering->ring[ering->cursor];
231 ent->is_io = is_io;
232 ent->err_mask = err_mask;
233 ent->timestamp = get_jiffies_64();
234}
235
Tejun Heo7d47e8d2007-02-02 16:22:31 +0900236static void ata_ering_clear(struct ata_ering *ering)
Tejun Heo0c247c52006-05-15 20:58:19 +0900237{
Tejun Heo7d47e8d2007-02-02 16:22:31 +0900238 memset(ering, 0, sizeof(*ering));
Tejun Heo0c247c52006-05-15 20:58:19 +0900239}
240
241static int ata_ering_map(struct ata_ering *ering,
242 int (*map_fn)(struct ata_ering_entry *, void *),
243 void *arg)
244{
245 int idx, rc = 0;
246 struct ata_ering_entry *ent;
247
248 idx = ering->cursor;
249 do {
250 ent = &ering->ring[idx];
251 if (!ent->err_mask)
252 break;
253 rc = map_fn(ent, arg);
254 if (rc)
255 break;
256 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
257 } while (idx != ering->cursor);
258
259 return rc;
260}
261
Tejun Heo64f65ca2006-06-24 20:30:18 +0900262static unsigned int ata_eh_dev_action(struct ata_device *dev)
263{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900264 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heo64f65ca2006-06-24 20:30:18 +0900265
266 return ehc->i.action | ehc->i.dev_action[dev->devno];
267}
268
Tejun Heof58229f2007-08-06 18:36:23 +0900269static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
Tejun Heoaf181c22006-06-24 20:30:18 +0900270 struct ata_eh_info *ehi, unsigned int action)
271{
Tejun Heof58229f2007-08-06 18:36:23 +0900272 struct ata_device *tdev;
Tejun Heoaf181c22006-06-24 20:30:18 +0900273
274 if (!dev) {
275 ehi->action &= ~action;
Tejun Heof58229f2007-08-06 18:36:23 +0900276 ata_link_for_each_dev(tdev, link)
277 ehi->dev_action[tdev->devno] &= ~action;
Tejun Heoaf181c22006-06-24 20:30:18 +0900278 } else {
279 /* doesn't make sense for port-wide EH actions */
280 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
281
282 /* break ehi->action into ehi->dev_action */
283 if (ehi->action & action) {
Tejun Heof58229f2007-08-06 18:36:23 +0900284 ata_link_for_each_dev(tdev, link)
285 ehi->dev_action[tdev->devno] |=
286 ehi->action & action;
Tejun Heoaf181c22006-06-24 20:30:18 +0900287 ehi->action &= ~action;
288 }
289
290 /* turn off the specified per-dev action */
291 ehi->dev_action[dev->devno] &= ~action;
292 }
293}
294
Tejun Heoece1d632006-04-02 18:51:53 +0900295/**
296 * ata_scsi_timed_out - SCSI layer time out callback
297 * @cmd: timed out SCSI command
298 *
299 * Handles SCSI layer timeout. We race with normal completion of
300 * the qc for @cmd. If the qc is already gone, we lose and let
301 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
302 * timed out and EH should be invoked. Prevent ata_qc_complete()
303 * from finishing it by setting EH_SCHEDULED and return
304 * EH_NOT_HANDLED.
305 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900306 * TODO: kill this function once old EH is gone.
307 *
Tejun Heoece1d632006-04-02 18:51:53 +0900308 * LOCKING:
309 * Called from timer context
310 *
311 * RETURNS:
312 * EH_HANDLED or EH_NOT_HANDLED
313 */
314enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
315{
316 struct Scsi_Host *host = cmd->device->host;
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400317 struct ata_port *ap = ata_shost_to_port(host);
Tejun Heoece1d632006-04-02 18:51:53 +0900318 unsigned long flags;
319 struct ata_queued_cmd *qc;
Tejun Heoad9e2762006-05-15 20:58:12 +0900320 enum scsi_eh_timer_return ret;
Tejun Heoece1d632006-04-02 18:51:53 +0900321
322 DPRINTK("ENTER\n");
323
Tejun Heoad9e2762006-05-15 20:58:12 +0900324 if (ap->ops->error_handler) {
325 ret = EH_NOT_HANDLED;
326 goto out;
327 }
328
329 ret = EH_HANDLED;
Jeff Garzikba6a1302006-06-22 23:46:10 -0400330 spin_lock_irqsave(ap->lock, flags);
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900331 qc = ata_qc_from_tag(ap, ap->link.active_tag);
Tejun Heoece1d632006-04-02 18:51:53 +0900332 if (qc) {
333 WARN_ON(qc->scsicmd != cmd);
334 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
335 qc->err_mask |= AC_ERR_TIMEOUT;
336 ret = EH_NOT_HANDLED;
337 }
Jeff Garzikba6a1302006-06-22 23:46:10 -0400338 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900339
Tejun Heoad9e2762006-05-15 20:58:12 +0900340 out:
Tejun Heoece1d632006-04-02 18:51:53 +0900341 DPRINTK("EXIT, ret=%d\n", ret);
342 return ret;
343}
344
345/**
346 * ata_scsi_error - SCSI layer error handler callback
347 * @host: SCSI host on which error occurred
348 *
349 * Handles SCSI-layer-thrown error events.
350 *
351 * LOCKING:
352 * Inherited from SCSI layer (none, can sleep)
353 *
354 * RETURNS:
355 * Zero.
356 */
Jeff Garzik381544b2006-04-11 13:04:39 -0400357void ata_scsi_error(struct Scsi_Host *host)
Tejun Heoece1d632006-04-02 18:51:53 +0900358{
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400359 struct ata_port *ap = ata_shost_to_port(host);
Tejun Heoa1e10f72007-08-18 13:28:49 +0900360 int i;
Tejun Heoad9e2762006-05-15 20:58:12 +0900361 unsigned long flags;
Tejun Heoece1d632006-04-02 18:51:53 +0900362
363 DPRINTK("ENTER\n");
364
Tejun Heoad9e2762006-05-15 20:58:12 +0900365 /* synchronize with port task */
Tejun Heoece1d632006-04-02 18:51:53 +0900366 ata_port_flush_task(ap);
367
Jeff Garzikcca39742006-08-24 03:19:22 -0400368 /* synchronize with host lock and sort out timeouts */
Tejun Heoece1d632006-04-02 18:51:53 +0900369
Tejun Heoad9e2762006-05-15 20:58:12 +0900370 /* For new EH, all qcs are finished in one of three ways -
371 * normal completion, error completion, and SCSI timeout.
372 * Both cmpletions can race against SCSI timeout. When normal
373 * completion wins, the qc never reaches EH. When error
374 * completion wins, the qc has ATA_QCFLAG_FAILED set.
375 *
376 * When SCSI timeout wins, things are a bit more complex.
377 * Normal or error completion can occur after the timeout but
378 * before this point. In such cases, both types of
379 * completions are honored. A scmd is determined to have
380 * timed out iff its associated qc is active and not failed.
381 */
382 if (ap->ops->error_handler) {
383 struct scsi_cmnd *scmd, *tmp;
384 int nr_timedout = 0;
Tejun Heoece1d632006-04-02 18:51:53 +0900385
Tejun Heoe30349d2006-07-03 03:02:15 +0900386 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900387
388 list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
389 struct ata_queued_cmd *qc;
390
391 for (i = 0; i < ATA_MAX_QUEUE; i++) {
392 qc = __ata_qc_from_tag(ap, i);
393 if (qc->flags & ATA_QCFLAG_ACTIVE &&
394 qc->scsicmd == scmd)
395 break;
396 }
397
398 if (i < ATA_MAX_QUEUE) {
399 /* the scmd has an associated qc */
400 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
401 /* which hasn't failed yet, timeout */
402 qc->err_mask |= AC_ERR_TIMEOUT;
403 qc->flags |= ATA_QCFLAG_FAILED;
404 nr_timedout++;
405 }
406 } else {
407 /* Normal completion occurred after
408 * SCSI timeout but before this point.
409 * Successfully complete it.
410 */
411 scmd->retries = scmd->allowed;
412 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
413 }
414 }
415
416 /* If we have timed out qcs. They belong to EH from
417 * this point but the state of the controller is
418 * unknown. Freeze the port to make sure the IRQ
419 * handler doesn't diddle with those qcs. This must
420 * be done atomically w.r.t. setting QCFLAG_FAILED.
421 */
422 if (nr_timedout)
423 __ata_port_freeze(ap);
424
Tejun Heoe30349d2006-07-03 03:02:15 +0900425 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa1e10f72007-08-18 13:28:49 +0900426
427 /* initialize eh_tries */
428 ap->eh_tries = ATA_EH_MAX_TRIES;
Tejun Heoad9e2762006-05-15 20:58:12 +0900429 } else
Tejun Heoe30349d2006-07-03 03:02:15 +0900430 spin_unlock_wait(ap->lock);
Tejun Heoad9e2762006-05-15 20:58:12 +0900431
432 repeat:
433 /* invoke error handler */
434 if (ap->ops->error_handler) {
Tejun Heocf1b86c2007-08-06 18:36:23 +0900435 struct ata_link *link;
436
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900437 /* kill fast drain timer */
438 del_timer_sync(&ap->fastdrain_timer);
439
Tejun Heo500530f2006-07-03 16:07:27 +0900440 /* process port resume request */
441 ata_eh_handle_port_resume(ap);
442
Tejun Heof3e81b12006-05-15 20:58:21 +0900443 /* fetch & clear EH info */
Tejun Heoe30349d2006-07-03 03:02:15 +0900444 spin_lock_irqsave(ap->lock, flags);
Tejun Heof3e81b12006-05-15 20:58:21 +0900445
Tejun Heocf1b86c2007-08-06 18:36:23 +0900446 __ata_port_for_each_link(link, ap) {
447 memset(&link->eh_context, 0, sizeof(link->eh_context));
448 link->eh_context.i = link->eh_info;
449 memset(&link->eh_info, 0, sizeof(link->eh_info));
450 }
Tejun Heof3e81b12006-05-15 20:58:21 +0900451
Tejun Heob51e9e52006-06-29 01:29:30 +0900452 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
453 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
Tejun Heof3e81b12006-05-15 20:58:21 +0900454
Tejun Heoe30349d2006-07-03 03:02:15 +0900455 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900456
Tejun Heo500530f2006-07-03 16:07:27 +0900457 /* invoke EH, skip if unloading or suspended */
458 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
Tejun Heo720ba122006-05-31 18:28:13 +0900459 ap->ops->error_handler(ap);
460 else
461 ata_eh_finish(ap);
Tejun Heoad9e2762006-05-15 20:58:12 +0900462
Tejun Heo500530f2006-07-03 16:07:27 +0900463 /* process port suspend request */
464 ata_eh_handle_port_suspend(ap);
465
Tejun Heoad9e2762006-05-15 20:58:12 +0900466 /* Exception might have happend after ->error_handler
467 * recovered the port but before this point. Repeat
468 * EH in such case.
469 */
Tejun Heoe30349d2006-07-03 03:02:15 +0900470 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900471
Tejun Heob51e9e52006-06-29 01:29:30 +0900472 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
Tejun Heoa1e10f72007-08-18 13:28:49 +0900473 if (--ap->eh_tries) {
Tejun Heoe30349d2006-07-03 03:02:15 +0900474 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900475 goto repeat;
476 }
477 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
Tejun Heoa1e10f72007-08-18 13:28:49 +0900478 "tries, giving up\n", ATA_EH_MAX_TRIES);
Tejun Heo914616a2007-06-25 21:47:11 +0900479 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
Tejun Heoad9e2762006-05-15 20:58:12 +0900480 }
481
Tejun Heof3e81b12006-05-15 20:58:21 +0900482 /* this run is complete, make sure EH info is clear */
Tejun Heocf1b86c2007-08-06 18:36:23 +0900483 __ata_port_for_each_link(link, ap)
484 memset(&link->eh_info, 0, sizeof(link->eh_info));
Tejun Heof3e81b12006-05-15 20:58:21 +0900485
Tejun Heoe30349d2006-07-03 03:02:15 +0900486 /* Clear host_eh_scheduled while holding ap->lock such
Tejun Heoad9e2762006-05-15 20:58:12 +0900487 * that if exception occurs after this point but
488 * before EH completion, SCSI midlayer will
489 * re-initiate EH.
490 */
491 host->host_eh_scheduled = 0;
492
Tejun Heoe30349d2006-07-03 03:02:15 +0900493 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900494 } else {
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900495 WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
Tejun Heoad9e2762006-05-15 20:58:12 +0900496 ap->ops->eng_timeout(ap);
497 }
498
499 /* finish or retry handled scmd's and clean up */
Tejun Heoece1d632006-04-02 18:51:53 +0900500 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
501
502 scsi_eh_flush_done_q(&ap->eh_done_q);
503
Tejun Heoad9e2762006-05-15 20:58:12 +0900504 /* clean up */
Tejun Heoe30349d2006-07-03 03:02:15 +0900505 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900506
Tejun Heo1cdaf532006-07-03 16:07:26 +0900507 if (ap->pflags & ATA_PFLAG_LOADING)
Tejun Heob51e9e52006-06-29 01:29:30 +0900508 ap->pflags &= ~ATA_PFLAG_LOADING;
Tejun Heo1cdaf532006-07-03 16:07:26 +0900509 else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
David Howells52bad642006-11-22 14:54:01 +0000510 queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0);
Tejun Heo1cdaf532006-07-03 16:07:26 +0900511
512 if (ap->pflags & ATA_PFLAG_RECOVERED)
513 ata_port_printk(ap, KERN_INFO, "EH complete\n");
Tejun Heo580b21022006-05-31 18:28:05 +0900514
Tejun Heob51e9e52006-06-29 01:29:30 +0900515 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
Tejun Heoad9e2762006-05-15 20:58:12 +0900516
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900517 /* tell wait_eh that we're done */
Tejun Heob51e9e52006-06-29 01:29:30 +0900518 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900519 wake_up_all(&ap->eh_wait_q);
520
Tejun Heoe30349d2006-07-03 03:02:15 +0900521 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900522
Tejun Heoece1d632006-04-02 18:51:53 +0900523 DPRINTK("EXIT\n");
Tejun Heoece1d632006-04-02 18:51:53 +0900524}
525
526/**
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900527 * ata_port_wait_eh - Wait for the currently pending EH to complete
528 * @ap: Port to wait EH for
529 *
530 * Wait until the currently pending EH is complete.
531 *
532 * LOCKING:
533 * Kernel thread context (may sleep).
534 */
535void ata_port_wait_eh(struct ata_port *ap)
536{
537 unsigned long flags;
538 DEFINE_WAIT(wait);
539
540 retry:
Jeff Garzikba6a1302006-06-22 23:46:10 -0400541 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900542
Tejun Heob51e9e52006-06-29 01:29:30 +0900543 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900544 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
Jeff Garzikba6a1302006-06-22 23:46:10 -0400545 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900546 schedule();
Jeff Garzikba6a1302006-06-22 23:46:10 -0400547 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900548 }
Tejun Heo0a1b6222006-06-11 11:01:38 +0900549 finish_wait(&ap->eh_wait_q, &wait);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900550
Jeff Garzikba6a1302006-06-22 23:46:10 -0400551 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900552
553 /* make sure SCSI EH is complete */
Jeff Garzikcca39742006-08-24 03:19:22 -0400554 if (scsi_host_in_recovery(ap->scsi_host)) {
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900555 msleep(10);
556 goto retry;
557 }
558}
559
560/**
Tejun Heoece1d632006-04-02 18:51:53 +0900561 * ata_qc_timeout - Handle timeout of queued command
562 * @qc: Command that timed out
563 *
564 * Some part of the kernel (currently, only the SCSI layer)
565 * has noticed that the active command on port @ap has not
566 * completed after a specified length of time. Handle this
567 * condition by disabling DMA (if necessary) and completing
568 * transactions, with error if necessary.
569 *
570 * This also handles the case of the "lost interrupt", where
571 * for some reason (possibly hardware bug, possibly driver bug)
572 * an interrupt was not delivered to the driver, even though the
573 * transaction completed successfully.
574 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900575 * TODO: kill this function once old EH is gone.
576 *
Tejun Heoece1d632006-04-02 18:51:53 +0900577 * LOCKING:
578 * Inherited from SCSI layer (none, can sleep)
579 */
580static void ata_qc_timeout(struct ata_queued_cmd *qc)
581{
582 struct ata_port *ap = qc->ap;
Tejun Heoece1d632006-04-02 18:51:53 +0900583 u8 host_stat = 0, drv_stat;
584 unsigned long flags;
585
586 DPRINTK("ENTER\n");
587
588 ap->hsm_task_state = HSM_ST_IDLE;
589
Jeff Garzikba6a1302006-06-22 23:46:10 -0400590 spin_lock_irqsave(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900591
592 switch (qc->tf.protocol) {
593
594 case ATA_PROT_DMA:
595 case ATA_PROT_ATAPI_DMA:
596 host_stat = ap->ops->bmdma_status(ap);
597
598 /* before we do anything else, clear DMA-Start bit */
599 ap->ops->bmdma_stop(qc);
600
601 /* fall through */
602
603 default:
604 ata_altstatus(ap);
605 drv_stat = ata_chk_status(ap);
606
607 /* ack bmdma irq events */
608 ap->ops->irq_clear(ap);
609
Tejun Heof15a1da2006-05-15 20:57:56 +0900610 ata_dev_printk(qc->dev, KERN_ERR, "command 0x%x timeout, "
611 "stat 0x%x host_stat 0x%x\n",
612 qc->tf.command, drv_stat, host_stat);
Tejun Heoece1d632006-04-02 18:51:53 +0900613
614 /* complete taskfile transaction */
Jeff Garzikc13b56a2006-04-02 10:34:24 -0400615 qc->err_mask |= AC_ERR_TIMEOUT;
Tejun Heoece1d632006-04-02 18:51:53 +0900616 break;
617 }
618
Jeff Garzikba6a1302006-06-22 23:46:10 -0400619 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900620
621 ata_eh_qc_complete(qc);
622
623 DPRINTK("EXIT\n");
624}
625
626/**
627 * ata_eng_timeout - Handle timeout of queued command
628 * @ap: Port on which timed-out command is active
629 *
630 * Some part of the kernel (currently, only the SCSI layer)
631 * has noticed that the active command on port @ap has not
632 * completed after a specified length of time. Handle this
633 * condition by disabling DMA (if necessary) and completing
634 * transactions, with error if necessary.
635 *
636 * This also handles the case of the "lost interrupt", where
637 * for some reason (possibly hardware bug, possibly driver bug)
638 * an interrupt was not delivered to the driver, even though the
639 * transaction completed successfully.
640 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900641 * TODO: kill this function once old EH is gone.
642 *
Tejun Heoece1d632006-04-02 18:51:53 +0900643 * LOCKING:
644 * Inherited from SCSI layer (none, can sleep)
645 */
646void ata_eng_timeout(struct ata_port *ap)
647{
648 DPRINTK("ENTER\n");
649
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900650 ata_qc_timeout(ata_qc_from_tag(ap, ap->link.active_tag));
Tejun Heoece1d632006-04-02 18:51:53 +0900651
652 DPRINTK("EXIT\n");
653}
654
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900655static int ata_eh_nr_in_flight(struct ata_port *ap)
656{
657 unsigned int tag;
658 int nr = 0;
659
660 /* count only non-internal commands */
661 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++)
662 if (ata_qc_from_tag(ap, tag))
663 nr++;
664
665 return nr;
666}
667
668void ata_eh_fastdrain_timerfn(unsigned long arg)
669{
670 struct ata_port *ap = (void *)arg;
671 unsigned long flags;
672 int cnt;
673
674 spin_lock_irqsave(ap->lock, flags);
675
676 cnt = ata_eh_nr_in_flight(ap);
677
678 /* are we done? */
679 if (!cnt)
680 goto out_unlock;
681
682 if (cnt == ap->fastdrain_cnt) {
683 unsigned int tag;
684
685 /* No progress during the last interval, tag all
686 * in-flight qcs as timed out and freeze the port.
687 */
688 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) {
689 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
690 if (qc)
691 qc->err_mask |= AC_ERR_TIMEOUT;
692 }
693
694 ata_port_freeze(ap);
695 } else {
696 /* some qcs have finished, give it another chance */
697 ap->fastdrain_cnt = cnt;
698 ap->fastdrain_timer.expires =
699 jiffies + ATA_EH_FASTDRAIN_INTERVAL;
700 add_timer(&ap->fastdrain_timer);
701 }
702
703 out_unlock:
704 spin_unlock_irqrestore(ap->lock, flags);
705}
706
707/**
708 * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
709 * @ap: target ATA port
710 * @fastdrain: activate fast drain
711 *
712 * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
713 * is non-zero and EH wasn't pending before. Fast drain ensures
714 * that EH kicks in in timely manner.
715 *
716 * LOCKING:
717 * spin_lock_irqsave(host lock)
718 */
719static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
720{
721 int cnt;
722
723 /* already scheduled? */
724 if (ap->pflags & ATA_PFLAG_EH_PENDING)
725 return;
726
727 ap->pflags |= ATA_PFLAG_EH_PENDING;
728
729 if (!fastdrain)
730 return;
731
732 /* do we have in-flight qcs? */
733 cnt = ata_eh_nr_in_flight(ap);
734 if (!cnt)
735 return;
736
737 /* activate fast drain */
738 ap->fastdrain_cnt = cnt;
739 ap->fastdrain_timer.expires = jiffies + ATA_EH_FASTDRAIN_INTERVAL;
740 add_timer(&ap->fastdrain_timer);
741}
742
Tejun Heof686bcb2006-05-15 20:58:05 +0900743/**
744 * ata_qc_schedule_eh - schedule qc for error handling
745 * @qc: command to schedule error handling for
746 *
747 * Schedule error handling for @qc. EH will kick in as soon as
748 * other commands are drained.
749 *
750 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400751 * spin_lock_irqsave(host lock)
Tejun Heof686bcb2006-05-15 20:58:05 +0900752 */
753void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
754{
755 struct ata_port *ap = qc->ap;
756
757 WARN_ON(!ap->ops->error_handler);
758
759 qc->flags |= ATA_QCFLAG_FAILED;
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900760 ata_eh_set_pending(ap, 1);
Tejun Heof686bcb2006-05-15 20:58:05 +0900761
762 /* The following will fail if timeout has already expired.
763 * ata_scsi_error() takes care of such scmds on EH entry.
764 * Note that ATA_QCFLAG_FAILED is unconditionally set after
765 * this function completes.
766 */
767 scsi_req_abort_cmd(qc->scsicmd);
768}
769
Tejun Heo7b70fc02006-05-15 20:58:07 +0900770/**
771 * ata_port_schedule_eh - schedule error handling without a qc
772 * @ap: ATA port to schedule EH for
773 *
774 * Schedule error handling for @ap. EH will kick in as soon as
775 * all commands are drained.
776 *
777 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400778 * spin_lock_irqsave(host lock)
Tejun Heo7b70fc02006-05-15 20:58:07 +0900779 */
780void ata_port_schedule_eh(struct ata_port *ap)
781{
782 WARN_ON(!ap->ops->error_handler);
783
Tejun Heof4d6d002007-05-01 11:50:15 +0200784 if (ap->pflags & ATA_PFLAG_INITIALIZING)
785 return;
786
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900787 ata_eh_set_pending(ap, 1);
Jeff Garzikcca39742006-08-24 03:19:22 -0400788 scsi_schedule_eh(ap->scsi_host);
Tejun Heo7b70fc02006-05-15 20:58:07 +0900789
790 DPRINTK("port EH scheduled\n");
791}
792
Tejun Heodbd82612007-08-06 18:36:23 +0900793static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
Tejun Heo7b70fc02006-05-15 20:58:07 +0900794{
795 int tag, nr_aborted = 0;
796
797 WARN_ON(!ap->ops->error_handler);
798
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900799 /* we're gonna abort all commands, no need for fast drain */
800 ata_eh_set_pending(ap, 0);
801
Tejun Heo7b70fc02006-05-15 20:58:07 +0900802 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
803 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
804
Tejun Heodbd82612007-08-06 18:36:23 +0900805 if (qc && (!link || qc->dev->link == link)) {
Tejun Heo7b70fc02006-05-15 20:58:07 +0900806 qc->flags |= ATA_QCFLAG_FAILED;
807 ata_qc_complete(qc);
808 nr_aborted++;
809 }
810 }
811
812 if (!nr_aborted)
813 ata_port_schedule_eh(ap);
814
815 return nr_aborted;
816}
817
Tejun Heoe3180492006-05-15 20:58:09 +0900818/**
Tejun Heodbd82612007-08-06 18:36:23 +0900819 * ata_link_abort - abort all qc's on the link
820 * @link: ATA link to abort qc's for
821 *
822 * Abort all active qc's active on @link and schedule EH.
823 *
824 * LOCKING:
825 * spin_lock_irqsave(host lock)
826 *
827 * RETURNS:
828 * Number of aborted qc's.
829 */
830int ata_link_abort(struct ata_link *link)
831{
832 return ata_do_link_abort(link->ap, link);
833}
834
835/**
836 * ata_port_abort - abort all qc's on the port
837 * @ap: ATA port to abort qc's for
838 *
839 * Abort all active qc's of @ap and schedule EH.
840 *
841 * LOCKING:
842 * spin_lock_irqsave(host_set lock)
843 *
844 * RETURNS:
845 * Number of aborted qc's.
846 */
847int ata_port_abort(struct ata_port *ap)
848{
849 return ata_do_link_abort(ap, NULL);
850}
851
852/**
Tejun Heoe3180492006-05-15 20:58:09 +0900853 * __ata_port_freeze - freeze port
854 * @ap: ATA port to freeze
855 *
856 * This function is called when HSM violation or some other
857 * condition disrupts normal operation of the port. Frozen port
858 * is not allowed to perform any operation until the port is
859 * thawed, which usually follows a successful reset.
860 *
861 * ap->ops->freeze() callback can be used for freezing the port
862 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
863 * port cannot be frozen hardware-wise, the interrupt handler
864 * must ack and clear interrupts unconditionally while the port
865 * is frozen.
866 *
867 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400868 * spin_lock_irqsave(host lock)
Tejun Heoe3180492006-05-15 20:58:09 +0900869 */
870static void __ata_port_freeze(struct ata_port *ap)
871{
872 WARN_ON(!ap->ops->error_handler);
873
874 if (ap->ops->freeze)
875 ap->ops->freeze(ap);
876
Tejun Heob51e9e52006-06-29 01:29:30 +0900877 ap->pflags |= ATA_PFLAG_FROZEN;
Tejun Heoe3180492006-05-15 20:58:09 +0900878
Tejun Heo44877b42007-02-21 01:06:51 +0900879 DPRINTK("ata%u port frozen\n", ap->print_id);
Tejun Heoe3180492006-05-15 20:58:09 +0900880}
881
882/**
883 * ata_port_freeze - abort & freeze port
884 * @ap: ATA port to freeze
885 *
886 * Abort and freeze @ap.
887 *
888 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400889 * spin_lock_irqsave(host lock)
Tejun Heoe3180492006-05-15 20:58:09 +0900890 *
891 * RETURNS:
892 * Number of aborted commands.
893 */
894int ata_port_freeze(struct ata_port *ap)
895{
896 int nr_aborted;
897
898 WARN_ON(!ap->ops->error_handler);
899
900 nr_aborted = ata_port_abort(ap);
901 __ata_port_freeze(ap);
902
903 return nr_aborted;
904}
905
906/**
907 * ata_eh_freeze_port - EH helper to freeze port
908 * @ap: ATA port to freeze
909 *
910 * Freeze @ap.
911 *
912 * LOCKING:
913 * None.
914 */
915void ata_eh_freeze_port(struct ata_port *ap)
916{
917 unsigned long flags;
918
919 if (!ap->ops->error_handler)
920 return;
921
Jeff Garzikba6a1302006-06-22 23:46:10 -0400922 spin_lock_irqsave(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900923 __ata_port_freeze(ap);
Jeff Garzikba6a1302006-06-22 23:46:10 -0400924 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900925}
926
927/**
928 * ata_port_thaw_port - EH helper to thaw port
929 * @ap: ATA port to thaw
930 *
931 * Thaw frozen port @ap.
932 *
933 * LOCKING:
934 * None.
935 */
936void ata_eh_thaw_port(struct ata_port *ap)
937{
938 unsigned long flags;
939
940 if (!ap->ops->error_handler)
941 return;
942
Jeff Garzikba6a1302006-06-22 23:46:10 -0400943 spin_lock_irqsave(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900944
Tejun Heob51e9e52006-06-29 01:29:30 +0900945 ap->pflags &= ~ATA_PFLAG_FROZEN;
Tejun Heoe3180492006-05-15 20:58:09 +0900946
947 if (ap->ops->thaw)
948 ap->ops->thaw(ap);
949
Jeff Garzikba6a1302006-06-22 23:46:10 -0400950 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900951
Tejun Heo44877b42007-02-21 01:06:51 +0900952 DPRINTK("ata%u port thawed\n", ap->print_id);
Tejun Heoe3180492006-05-15 20:58:09 +0900953}
954
Tejun Heoece1d632006-04-02 18:51:53 +0900955static void ata_eh_scsidone(struct scsi_cmnd *scmd)
956{
957 /* nada */
958}
959
960static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
961{
962 struct ata_port *ap = qc->ap;
963 struct scsi_cmnd *scmd = qc->scsicmd;
964 unsigned long flags;
965
Jeff Garzikba6a1302006-06-22 23:46:10 -0400966 spin_lock_irqsave(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900967 qc->scsidone = ata_eh_scsidone;
968 __ata_qc_complete(qc);
969 WARN_ON(ata_tag_valid(qc->tag));
Jeff Garzikba6a1302006-06-22 23:46:10 -0400970 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900971
972 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
973}
974
975/**
976 * ata_eh_qc_complete - Complete an active ATA command from EH
977 * @qc: Command to complete
978 *
979 * Indicate to the mid and upper layers that an ATA command has
980 * completed. To be used from EH.
981 */
982void ata_eh_qc_complete(struct ata_queued_cmd *qc)
983{
984 struct scsi_cmnd *scmd = qc->scsicmd;
985 scmd->retries = scmd->allowed;
986 __ata_eh_qc_complete(qc);
987}
988
989/**
990 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
991 * @qc: Command to retry
992 *
993 * Indicate to the mid and upper layers that an ATA command
994 * should be retried. To be used from EH.
995 *
996 * SCSI midlayer limits the number of retries to scmd->allowed.
997 * scmd->retries is decremented for commands which get retried
998 * due to unrelated failures (qc->err_mask is zero).
999 */
1000void ata_eh_qc_retry(struct ata_queued_cmd *qc)
1001{
1002 struct scsi_cmnd *scmd = qc->scsicmd;
1003 if (!qc->err_mask && scmd->retries)
1004 scmd->retries--;
1005 __ata_eh_qc_complete(qc);
1006}
Tejun Heo022bdb02006-05-15 20:58:22 +09001007
1008/**
Tejun Heo0ea035a2006-05-31 18:28:01 +09001009 * ata_eh_detach_dev - detach ATA device
1010 * @dev: ATA device to detach
1011 *
1012 * Detach @dev.
1013 *
1014 * LOCKING:
1015 * None.
1016 */
Tejun Heofb7fd612007-09-23 13:14:12 +09001017void ata_eh_detach_dev(struct ata_device *dev)
Tejun Heo0ea035a2006-05-31 18:28:01 +09001018{
Tejun Heof58229f2007-08-06 18:36:23 +09001019 struct ata_link *link = dev->link;
1020 struct ata_port *ap = link->ap;
Tejun Heo0ea035a2006-05-31 18:28:01 +09001021 unsigned long flags;
1022
1023 ata_dev_disable(dev);
1024
Jeff Garzikba6a1302006-06-22 23:46:10 -04001025 spin_lock_irqsave(ap->lock, flags);
Tejun Heo0ea035a2006-05-31 18:28:01 +09001026
1027 dev->flags &= ~ATA_DFLAG_DETACH;
1028
1029 if (ata_scsi_offline_dev(dev)) {
1030 dev->flags |= ATA_DFLAG_DETACHED;
Tejun Heob51e9e52006-06-29 01:29:30 +09001031 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
Tejun Heo0ea035a2006-05-31 18:28:01 +09001032 }
1033
Tejun Heobeb07c12006-06-24 20:30:19 +09001034 /* clear per-dev EH actions */
Tejun Heof58229f2007-08-06 18:36:23 +09001035 ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
1036 ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
Tejun Heobeb07c12006-06-24 20:30:19 +09001037
Jeff Garzikba6a1302006-06-22 23:46:10 -04001038 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo0ea035a2006-05-31 18:28:01 +09001039}
1040
1041/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001042 * ata_eh_about_to_do - about to perform eh_action
Tejun Heo955e57d2007-08-06 18:36:23 +09001043 * @link: target ATA link
Tejun Heo47005f22006-06-19 18:27:23 +09001044 * @dev: target ATA dev for per-dev action (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09001045 * @action: action about to be performed
1046 *
1047 * Called just before performing EH actions to clear related bits
Tejun Heo955e57d2007-08-06 18:36:23 +09001048 * in @link->eh_info such that eh actions are not unnecessarily
1049 * repeated.
Tejun Heo022bdb02006-05-15 20:58:22 +09001050 *
1051 * LOCKING:
1052 * None.
1053 */
Tejun Heofb7fd612007-09-23 13:14:12 +09001054void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
1055 unsigned int action)
Tejun Heo022bdb02006-05-15 20:58:22 +09001056{
Tejun Heo955e57d2007-08-06 18:36:23 +09001057 struct ata_port *ap = link->ap;
1058 struct ata_eh_info *ehi = &link->eh_info;
1059 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001060 unsigned long flags;
1061
Jeff Garzikba6a1302006-06-22 23:46:10 -04001062 spin_lock_irqsave(ap->lock, flags);
Tejun Heo1cdaf532006-07-03 16:07:26 +09001063
Tejun Heo13abf502006-07-10 23:18:46 +09001064 /* Reset is represented by combination of actions and EHI
1065 * flags. Suck in all related bits before clearing eh_info to
1066 * avoid losing requested action.
1067 */
1068 if (action & ATA_EH_RESET_MASK) {
1069 ehc->i.action |= ehi->action & ATA_EH_RESET_MASK;
1070 ehc->i.flags |= ehi->flags & ATA_EHI_RESET_MODIFIER_MASK;
Tejun Heo1cdaf532006-07-03 16:07:26 +09001071
Tejun Heo13abf502006-07-10 23:18:46 +09001072 /* make sure all reset actions are cleared & clear EHI flags */
1073 action |= ATA_EH_RESET_MASK;
1074 ehi->flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
1075 }
1076
Tejun Heo955e57d2007-08-06 18:36:23 +09001077 ata_eh_clear_action(link, dev, ehi, action);
Tejun Heo13abf502006-07-10 23:18:46 +09001078
1079 if (!(ehc->i.flags & ATA_EHI_QUIET))
Tejun Heo1cdaf532006-07-03 16:07:26 +09001080 ap->pflags |= ATA_PFLAG_RECOVERED;
1081
Jeff Garzikba6a1302006-06-22 23:46:10 -04001082 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo022bdb02006-05-15 20:58:22 +09001083}
1084
1085/**
Tejun Heo47005f22006-06-19 18:27:23 +09001086 * ata_eh_done - EH action complete
Tejun Heo955e57d2007-08-06 18:36:23 +09001087* @ap: target ATA port
Tejun Heo47005f22006-06-19 18:27:23 +09001088 * @dev: target ATA dev for per-dev action (can be NULL)
1089 * @action: action just completed
1090 *
1091 * Called right after performing EH actions to clear related bits
Tejun Heo955e57d2007-08-06 18:36:23 +09001092 * in @link->eh_context.
Tejun Heo47005f22006-06-19 18:27:23 +09001093 *
1094 * LOCKING:
1095 * None.
1096 */
Tejun Heofb7fd612007-09-23 13:14:12 +09001097void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1098 unsigned int action)
Tejun Heo47005f22006-06-19 18:27:23 +09001099{
Tejun Heo955e57d2007-08-06 18:36:23 +09001100 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001101
Tejun Heo13abf502006-07-10 23:18:46 +09001102 /* if reset is complete, clear all reset actions & reset modifier */
1103 if (action & ATA_EH_RESET_MASK) {
1104 action |= ATA_EH_RESET_MASK;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001105 ehc->i.flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
Tejun Heo13abf502006-07-10 23:18:46 +09001106 }
1107
Tejun Heo955e57d2007-08-06 18:36:23 +09001108 ata_eh_clear_action(link, dev, &ehc->i, action);
Tejun Heo47005f22006-06-19 18:27:23 +09001109}
1110
1111/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001112 * ata_err_string - convert err_mask to descriptive string
1113 * @err_mask: error mask to convert to string
1114 *
1115 * Convert @err_mask to descriptive string. Errors are
1116 * prioritized according to severity and only the most severe
1117 * error is reported.
1118 *
1119 * LOCKING:
1120 * None.
1121 *
1122 * RETURNS:
1123 * Descriptive string for @err_mask
1124 */
1125static const char * ata_err_string(unsigned int err_mask)
1126{
1127 if (err_mask & AC_ERR_HOST_BUS)
1128 return "host bus error";
1129 if (err_mask & AC_ERR_ATA_BUS)
1130 return "ATA bus error";
1131 if (err_mask & AC_ERR_TIMEOUT)
1132 return "timeout";
1133 if (err_mask & AC_ERR_HSM)
1134 return "HSM violation";
1135 if (err_mask & AC_ERR_SYSTEM)
1136 return "internal error";
1137 if (err_mask & AC_ERR_MEDIA)
1138 return "media error";
1139 if (err_mask & AC_ERR_INVALID)
1140 return "invalid argument";
1141 if (err_mask & AC_ERR_DEV)
1142 return "device error";
1143 return "unknown error";
1144}
1145
1146/**
Tejun Heoe8ee8452006-05-15 21:03:46 +09001147 * ata_read_log_page - read a specific log page
1148 * @dev: target device
1149 * @page: page to read
1150 * @buf: buffer to store read page
1151 * @sectors: number of sectors to read
1152 *
1153 * Read log page using READ_LOG_EXT command.
1154 *
1155 * LOCKING:
1156 * Kernel thread context (may sleep).
1157 *
1158 * RETURNS:
1159 * 0 on success, AC_ERR_* mask otherwise.
1160 */
1161static unsigned int ata_read_log_page(struct ata_device *dev,
1162 u8 page, void *buf, unsigned int sectors)
1163{
1164 struct ata_taskfile tf;
1165 unsigned int err_mask;
1166
1167 DPRINTK("read log page - page %d\n", page);
1168
1169 ata_tf_init(dev, &tf);
1170 tf.command = ATA_CMD_READ_LOG_EXT;
1171 tf.lbal = page;
1172 tf.nsect = sectors;
1173 tf.hob_nsect = sectors >> 8;
1174 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1175 tf.protocol = ATA_PROT_PIO;
1176
1177 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
1178 buf, sectors * ATA_SECT_SIZE);
1179
1180 DPRINTK("EXIT, err_mask=%x\n", err_mask);
1181 return err_mask;
1182}
1183
1184/**
1185 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
1186 * @dev: Device to read log page 10h from
1187 * @tag: Resulting tag of the failed command
1188 * @tf: Resulting taskfile registers of the failed command
1189 *
1190 * Read log page 10h to obtain NCQ error details and clear error
1191 * condition.
1192 *
1193 * LOCKING:
1194 * Kernel thread context (may sleep).
1195 *
1196 * RETURNS:
1197 * 0 on success, -errno otherwise.
1198 */
1199static int ata_eh_read_log_10h(struct ata_device *dev,
1200 int *tag, struct ata_taskfile *tf)
1201{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001202 u8 *buf = dev->link->ap->sector_buf;
Tejun Heoe8ee8452006-05-15 21:03:46 +09001203 unsigned int err_mask;
1204 u8 csum;
1205 int i;
1206
1207 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
1208 if (err_mask)
1209 return -EIO;
1210
1211 csum = 0;
1212 for (i = 0; i < ATA_SECT_SIZE; i++)
1213 csum += buf[i];
1214 if (csum)
1215 ata_dev_printk(dev, KERN_WARNING,
1216 "invalid checksum 0x%x on log page 10h\n", csum);
1217
1218 if (buf[0] & 0x80)
1219 return -ENOENT;
1220
1221 *tag = buf[0] & 0x1f;
1222
1223 tf->command = buf[2];
1224 tf->feature = buf[3];
1225 tf->lbal = buf[4];
1226 tf->lbam = buf[5];
1227 tf->lbah = buf[6];
1228 tf->device = buf[7];
1229 tf->hob_lbal = buf[8];
1230 tf->hob_lbam = buf[9];
1231 tf->hob_lbah = buf[10];
1232 tf->nsect = buf[12];
1233 tf->hob_nsect = buf[13];
1234
1235 return 0;
1236}
1237
1238/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001239 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1240 * @dev: device to perform REQUEST_SENSE to
1241 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
1242 *
1243 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
1244 * SENSE. This function is EH helper.
1245 *
1246 * LOCKING:
1247 * Kernel thread context (may sleep).
1248 *
1249 * RETURNS:
1250 * 0 on success, AC_ERR_* mask on failure
1251 */
Albert Lee56287762007-04-02 11:30:46 +08001252static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc)
Tejun Heo022bdb02006-05-15 20:58:22 +09001253{
Albert Lee56287762007-04-02 11:30:46 +08001254 struct ata_device *dev = qc->dev;
1255 unsigned char *sense_buf = qc->scsicmd->sense_buffer;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001256 struct ata_port *ap = dev->link->ap;
Tejun Heo022bdb02006-05-15 20:58:22 +09001257 struct ata_taskfile tf;
1258 u8 cdb[ATAPI_CDB_LEN];
1259
1260 DPRINTK("ATAPI request sense\n");
1261
Tejun Heo022bdb02006-05-15 20:58:22 +09001262 /* FIXME: is this needed? */
1263 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1264
Albert Lee56287762007-04-02 11:30:46 +08001265 /* initialize sense_buf with the error register,
1266 * for the case where they are -not- overwritten
1267 */
Tejun Heo022bdb02006-05-15 20:58:22 +09001268 sense_buf[0] = 0x70;
Albert Lee56287762007-04-02 11:30:46 +08001269 sense_buf[2] = qc->result_tf.feature >> 4;
1270
Jeff Garzika617c092007-05-21 20:14:23 -04001271 /* some devices time out if garbage left in tf */
Albert Lee56287762007-04-02 11:30:46 +08001272 ata_tf_init(dev, &tf);
Tejun Heo022bdb02006-05-15 20:58:22 +09001273
1274 memset(cdb, 0, ATAPI_CDB_LEN);
1275 cdb[0] = REQUEST_SENSE;
1276 cdb[4] = SCSI_SENSE_BUFFERSIZE;
1277
1278 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1279 tf.command = ATA_CMD_PACKET;
1280
1281 /* is it pointless to prefer PIO for "safety reasons"? */
1282 if (ap->flags & ATA_FLAG_PIO_DMA) {
1283 tf.protocol = ATA_PROT_ATAPI_DMA;
1284 tf.feature |= ATAPI_PKT_DMA;
1285 } else {
1286 tf.protocol = ATA_PROT_ATAPI;
1287 tf.lbam = (8 * 1024) & 0xff;
1288 tf.lbah = (8 * 1024) >> 8;
1289 }
1290
1291 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
1292 sense_buf, SCSI_SENSE_BUFFERSIZE);
1293}
1294
1295/**
1296 * ata_eh_analyze_serror - analyze SError for a failed port
Tejun Heo02607312007-08-06 18:36:23 +09001297 * @link: ATA link to analyze SError for
Tejun Heo022bdb02006-05-15 20:58:22 +09001298 *
1299 * Analyze SError if available and further determine cause of
1300 * failure.
1301 *
1302 * LOCKING:
1303 * None.
1304 */
Tejun Heo02607312007-08-06 18:36:23 +09001305static void ata_eh_analyze_serror(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001306{
Tejun Heo02607312007-08-06 18:36:23 +09001307 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001308 u32 serror = ehc->i.serror;
1309 unsigned int err_mask = 0, action = 0;
1310
1311 if (serror & SERR_PERSISTENT) {
1312 err_mask |= AC_ERR_ATA_BUS;
1313 action |= ATA_EH_HARDRESET;
1314 }
1315 if (serror &
1316 (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
1317 err_mask |= AC_ERR_ATA_BUS;
1318 action |= ATA_EH_SOFTRESET;
1319 }
1320 if (serror & SERR_PROTOCOL) {
1321 err_mask |= AC_ERR_HSM;
1322 action |= ATA_EH_SOFTRESET;
1323 }
1324 if (serror & SERR_INTERNAL) {
1325 err_mask |= AC_ERR_SYSTEM;
Tejun Heo771b8da2007-03-14 01:20:51 +09001326 action |= ATA_EH_HARDRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001327 }
Tejun Heo084fe632006-05-31 18:28:03 +09001328 if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG))
1329 ata_ehi_hotplugged(&ehc->i);
Tejun Heo022bdb02006-05-15 20:58:22 +09001330
1331 ehc->i.err_mask |= err_mask;
1332 ehc->i.action |= action;
1333}
1334
1335/**
Tejun Heoe8ee8452006-05-15 21:03:46 +09001336 * ata_eh_analyze_ncq_error - analyze NCQ error
Tejun Heo02607312007-08-06 18:36:23 +09001337 * @link: ATA link to analyze NCQ error for
Tejun Heoe8ee8452006-05-15 21:03:46 +09001338 *
1339 * Read log page 10h, determine the offending qc and acquire
1340 * error status TF. For NCQ device errors, all LLDDs have to do
1341 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1342 * care of the rest.
1343 *
1344 * LOCKING:
1345 * Kernel thread context (may sleep).
1346 */
Tejun Heo02607312007-08-06 18:36:23 +09001347static void ata_eh_analyze_ncq_error(struct ata_link *link)
Tejun Heoe8ee8452006-05-15 21:03:46 +09001348{
Tejun Heo02607312007-08-06 18:36:23 +09001349 struct ata_port *ap = link->ap;
1350 struct ata_eh_context *ehc = &link->eh_context;
1351 struct ata_device *dev = link->device;
Tejun Heoe8ee8452006-05-15 21:03:46 +09001352 struct ata_queued_cmd *qc;
1353 struct ata_taskfile tf;
1354 int tag, rc;
1355
1356 /* if frozen, we can't do much */
Tejun Heob51e9e52006-06-29 01:29:30 +09001357 if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heoe8ee8452006-05-15 21:03:46 +09001358 return;
1359
1360 /* is it NCQ device error? */
Tejun Heo02607312007-08-06 18:36:23 +09001361 if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
Tejun Heoe8ee8452006-05-15 21:03:46 +09001362 return;
1363
1364 /* has LLDD analyzed already? */
1365 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1366 qc = __ata_qc_from_tag(ap, tag);
1367
1368 if (!(qc->flags & ATA_QCFLAG_FAILED))
1369 continue;
1370
1371 if (qc->err_mask)
1372 return;
1373 }
1374
1375 /* okay, this error is ours */
1376 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1377 if (rc) {
Tejun Heo02607312007-08-06 18:36:23 +09001378 ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
Tejun Heoe8ee8452006-05-15 21:03:46 +09001379 "(errno=%d)\n", rc);
1380 return;
1381 }
1382
Tejun Heo02607312007-08-06 18:36:23 +09001383 if (!(link->sactive & (1 << tag))) {
1384 ata_link_printk(link, KERN_ERR, "log page 10h reported "
Tejun Heoe8ee8452006-05-15 21:03:46 +09001385 "inactive tag %d\n", tag);
1386 return;
1387 }
1388
1389 /* we've got the perpetrator, condemn it */
1390 qc = __ata_qc_from_tag(ap, tag);
1391 memcpy(&qc->result_tf, &tf, sizeof(tf));
Tejun Heo5335b722007-07-16 14:29:40 +09001392 qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
Tejun Heoe8ee8452006-05-15 21:03:46 +09001393 ehc->i.err_mask &= ~AC_ERR_DEV;
1394}
1395
1396/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001397 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1398 * @qc: qc to analyze
1399 * @tf: Taskfile registers to analyze
1400 *
1401 * Analyze taskfile of @qc and further determine cause of
1402 * failure. This function also requests ATAPI sense data if
1403 * avaliable.
1404 *
1405 * LOCKING:
1406 * Kernel thread context (may sleep).
1407 *
1408 * RETURNS:
1409 * Determined recovery action
1410 */
1411static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1412 const struct ata_taskfile *tf)
1413{
1414 unsigned int tmp, action = 0;
1415 u8 stat = tf->command, err = tf->feature;
1416
1417 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1418 qc->err_mask |= AC_ERR_HSM;
1419 return ATA_EH_SOFTRESET;
1420 }
1421
Tejun Heoa51d6442007-03-20 15:24:11 +09001422 if (stat & (ATA_ERR | ATA_DF))
1423 qc->err_mask |= AC_ERR_DEV;
1424 else
Tejun Heo022bdb02006-05-15 20:58:22 +09001425 return 0;
1426
1427 switch (qc->dev->class) {
1428 case ATA_DEV_ATA:
1429 if (err & ATA_ICRC)
1430 qc->err_mask |= AC_ERR_ATA_BUS;
1431 if (err & ATA_UNC)
1432 qc->err_mask |= AC_ERR_MEDIA;
1433 if (err & ATA_IDNF)
1434 qc->err_mask |= AC_ERR_INVALID;
1435 break;
1436
1437 case ATA_DEV_ATAPI:
Tejun Heoa569a302006-11-21 10:40:51 +09001438 if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
Albert Lee56287762007-04-02 11:30:46 +08001439 tmp = atapi_eh_request_sense(qc);
Tejun Heoa569a302006-11-21 10:40:51 +09001440 if (!tmp) {
1441 /* ATA_QCFLAG_SENSE_VALID is used to
1442 * tell atapi_qc_complete() that sense
1443 * data is already valid.
1444 *
1445 * TODO: interpret sense data and set
1446 * appropriate err_mask.
1447 */
1448 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1449 } else
1450 qc->err_mask |= tmp;
1451 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001452 }
1453
1454 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1455 action |= ATA_EH_SOFTRESET;
1456
1457 return action;
1458}
1459
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001460static int ata_eh_categorize_error(int is_io, unsigned int err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001461{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001462 if (err_mask & AC_ERR_ATA_BUS)
Tejun Heo022bdb02006-05-15 20:58:22 +09001463 return 1;
1464
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001465 if (err_mask & AC_ERR_TIMEOUT)
1466 return 2;
1467
1468 if (is_io) {
1469 if (err_mask & AC_ERR_HSM)
Tejun Heo022bdb02006-05-15 20:58:22 +09001470 return 2;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001471 if ((err_mask &
1472 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1473 return 3;
Tejun Heo022bdb02006-05-15 20:58:22 +09001474 }
1475
1476 return 0;
1477}
1478
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001479struct speed_down_verdict_arg {
Tejun Heo022bdb02006-05-15 20:58:22 +09001480 u64 since;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001481 int nr_errors[4];
Tejun Heo022bdb02006-05-15 20:58:22 +09001482};
1483
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001484static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
Tejun Heo022bdb02006-05-15 20:58:22 +09001485{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001486 struct speed_down_verdict_arg *arg = void_arg;
1487 int cat = ata_eh_categorize_error(ent->is_io, ent->err_mask);
Tejun Heo022bdb02006-05-15 20:58:22 +09001488
1489 if (ent->timestamp < arg->since)
1490 return -1;
1491
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001492 arg->nr_errors[cat]++;
Tejun Heo022bdb02006-05-15 20:58:22 +09001493 return 0;
1494}
1495
1496/**
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001497 * ata_eh_speed_down_verdict - Determine speed down verdict
Tejun Heo022bdb02006-05-15 20:58:22 +09001498 * @dev: Device of interest
1499 *
1500 * This function examines error ring of @dev and determines
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001501 * whether NCQ needs to be turned off, transfer speed should be
1502 * stepped down, or falling back to PIO is necessary.
Tejun Heo022bdb02006-05-15 20:58:22 +09001503 *
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001504 * Cat-1 is ATA_BUS error for any command.
Tejun Heo022bdb02006-05-15 20:58:22 +09001505 *
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001506 * Cat-2 is TIMEOUT for any command or HSM violation for known
1507 * supported commands.
1508 *
1509 * Cat-3 is is unclassified DEV error for known supported
Tejun Heo022bdb02006-05-15 20:58:22 +09001510 * command.
1511 *
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001512 * NCQ needs to be turned off if there have been more than 3
1513 * Cat-2 + Cat-3 errors during last 10 minutes.
1514 *
1515 * Speed down is necessary if there have been more than 3 Cat-1 +
1516 * Cat-2 errors or 10 Cat-3 errors during last 10 minutes.
1517 *
1518 * Falling back to PIO mode is necessary if there have been more
1519 * than 10 Cat-1 + Cat-2 + Cat-3 errors during last 5 minutes.
1520 *
Tejun Heo022bdb02006-05-15 20:58:22 +09001521 * LOCKING:
1522 * Inherited from caller.
1523 *
1524 * RETURNS:
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001525 * OR of ATA_EH_SPDN_* flags.
Tejun Heo022bdb02006-05-15 20:58:22 +09001526 */
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001527static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
Tejun Heo022bdb02006-05-15 20:58:22 +09001528{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001529 const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
1530 u64 j64 = get_jiffies_64();
1531 struct speed_down_verdict_arg arg;
1532 unsigned int verdict = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09001533
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001534 /* scan past 10 mins of error history */
Tejun Heo022bdb02006-05-15 20:58:22 +09001535 memset(&arg, 0, sizeof(arg));
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001536 arg.since = j64 - min(j64, j10mins);
1537 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
Tejun Heo022bdb02006-05-15 20:58:22 +09001538
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001539 if (arg.nr_errors[2] + arg.nr_errors[3] > 3)
1540 verdict |= ATA_EH_SPDN_NCQ_OFF;
1541 if (arg.nr_errors[1] + arg.nr_errors[2] > 3 || arg.nr_errors[3] > 10)
1542 verdict |= ATA_EH_SPDN_SPEED_DOWN;
Tejun Heo022bdb02006-05-15 20:58:22 +09001543
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001544 /* scan past 3 mins of error history */
1545 memset(&arg, 0, sizeof(arg));
1546 arg.since = j64 - min(j64, j5mins);
1547 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
Tejun Heo022bdb02006-05-15 20:58:22 +09001548
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001549 if (arg.nr_errors[1] + arg.nr_errors[2] + arg.nr_errors[3] > 10)
1550 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
1551
1552 return verdict;
Tejun Heo022bdb02006-05-15 20:58:22 +09001553}
1554
1555/**
1556 * ata_eh_speed_down - record error and speed down if necessary
1557 * @dev: Failed device
1558 * @is_io: Did the device fail during normal IO?
1559 * @err_mask: err_mask of the error
1560 *
1561 * Record error and examine error history to determine whether
1562 * adjusting transmission speed is necessary. It also sets
1563 * transmission limits appropriately if such adjustment is
1564 * necessary.
1565 *
1566 * LOCKING:
1567 * Kernel thread context (may sleep).
1568 *
1569 * RETURNS:
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001570 * Determined recovery action.
Tejun Heo022bdb02006-05-15 20:58:22 +09001571 */
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001572static unsigned int ata_eh_speed_down(struct ata_device *dev, int is_io,
1573 unsigned int err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001574{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001575 unsigned int verdict;
1576 unsigned int action = 0;
1577
1578 /* don't bother if Cat-0 error */
1579 if (ata_eh_categorize_error(is_io, err_mask) == 0)
Tejun Heo022bdb02006-05-15 20:58:22 +09001580 return 0;
1581
1582 /* record error and determine whether speed down is necessary */
1583 ata_ering_record(&dev->ering, is_io, err_mask);
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001584 verdict = ata_eh_speed_down_verdict(dev);
Tejun Heo022bdb02006-05-15 20:58:22 +09001585
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001586 /* turn off NCQ? */
1587 if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
1588 (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
1589 ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
1590 dev->flags |= ATA_DFLAG_NCQ_OFF;
1591 ata_dev_printk(dev, KERN_WARNING,
1592 "NCQ disabled due to excessive errors\n");
1593 goto done;
1594 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001595
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001596 /* speed down? */
1597 if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1598 /* speed down SATA link speed if possible */
Tejun Heo936fd732007-08-06 18:36:23 +09001599 if (sata_down_spd_limit(dev->link) == 0) {
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001600 action |= ATA_EH_HARDRESET;
1601 goto done;
1602 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001603
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001604 /* lower transfer mode */
1605 if (dev->spdn_cnt < 2) {
1606 static const int dma_dnxfer_sel[] =
1607 { ATA_DNXFER_DMA, ATA_DNXFER_40C };
1608 static const int pio_dnxfer_sel[] =
1609 { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
1610 int sel;
Tejun Heo022bdb02006-05-15 20:58:22 +09001611
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001612 if (dev->xfer_shift != ATA_SHIFT_PIO)
1613 sel = dma_dnxfer_sel[dev->spdn_cnt];
1614 else
1615 sel = pio_dnxfer_sel[dev->spdn_cnt];
1616
1617 dev->spdn_cnt++;
1618
1619 if (ata_down_xfermask_limit(dev, sel) == 0) {
1620 action |= ATA_EH_SOFTRESET;
1621 goto done;
1622 }
1623 }
1624 }
1625
1626 /* Fall back to PIO? Slowing down to PIO is meaningless for
1627 * SATA. Consider it only for PATA.
1628 */
1629 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001630 (dev->link->ap->cbl != ATA_CBL_SATA) &&
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001631 (dev->xfer_shift != ATA_SHIFT_PIO)) {
1632 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
1633 dev->spdn_cnt = 0;
1634 action |= ATA_EH_SOFTRESET;
1635 goto done;
1636 }
1637 }
1638
Tejun Heo022bdb02006-05-15 20:58:22 +09001639 return 0;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001640 done:
1641 /* device has been slowed down, blow error history */
1642 ata_ering_clear(&dev->ering);
1643 return action;
Tejun Heo022bdb02006-05-15 20:58:22 +09001644}
1645
1646/**
Tejun Heo9b1e2652007-08-06 18:36:24 +09001647 * ata_eh_link_autopsy - analyze error and determine recovery action
1648 * @link: host link to perform autopsy on
Tejun Heo022bdb02006-05-15 20:58:22 +09001649 *
Tejun Heo02607312007-08-06 18:36:23 +09001650 * Analyze why @link failed and determine which recovery actions
1651 * are needed. This function also sets more detailed AC_ERR_*
1652 * values and fills sense data for ATAPI CHECK SENSE.
Tejun Heo022bdb02006-05-15 20:58:22 +09001653 *
1654 * LOCKING:
1655 * Kernel thread context (may sleep).
1656 */
Tejun Heo9b1e2652007-08-06 18:36:24 +09001657static void ata_eh_link_autopsy(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001658{
Tejun Heo02607312007-08-06 18:36:23 +09001659 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09001660 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001661 unsigned int all_err_mask = 0;
1662 int tag, is_io = 0;
1663 u32 serror;
1664 int rc;
1665
1666 DPRINTK("ENTER\n");
1667
Tejun Heo1cdaf532006-07-03 16:07:26 +09001668 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1669 return;
1670
Tejun Heo022bdb02006-05-15 20:58:22 +09001671 /* obtain and analyze SError */
Tejun Heo936fd732007-08-06 18:36:23 +09001672 rc = sata_scr_read(link, SCR_ERROR, &serror);
Tejun Heo022bdb02006-05-15 20:58:22 +09001673 if (rc == 0) {
1674 ehc->i.serror |= serror;
Tejun Heo02607312007-08-06 18:36:23 +09001675 ata_eh_analyze_serror(link);
Tejun Heo4e57c512007-07-16 14:29:41 +09001676 } else if (rc != -EOPNOTSUPP) {
1677 /* SError read failed, force hardreset and probing */
1678 ata_ehi_schedule_probe(&ehc->i);
Tejun Heo4528e4d2006-07-08 20:17:26 +09001679 ehc->i.action |= ATA_EH_HARDRESET;
Tejun Heo4e57c512007-07-16 14:29:41 +09001680 ehc->i.err_mask |= AC_ERR_OTHER;
1681 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001682
Tejun Heoe8ee8452006-05-15 21:03:46 +09001683 /* analyze NCQ failure */
Tejun Heo02607312007-08-06 18:36:23 +09001684 ata_eh_analyze_ncq_error(link);
Tejun Heoe8ee8452006-05-15 21:03:46 +09001685
Tejun Heo022bdb02006-05-15 20:58:22 +09001686 /* any real error trumps AC_ERR_OTHER */
1687 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1688 ehc->i.err_mask &= ~AC_ERR_OTHER;
1689
1690 all_err_mask |= ehc->i.err_mask;
1691
1692 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1693 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1694
Tejun Heo02607312007-08-06 18:36:23 +09001695 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001696 continue;
1697
1698 /* inherit upper level err_mask */
1699 qc->err_mask |= ehc->i.err_mask;
1700
Tejun Heo022bdb02006-05-15 20:58:22 +09001701 /* analyze TF */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001702 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
Tejun Heo022bdb02006-05-15 20:58:22 +09001703
1704 /* DEV errors are probably spurious in case of ATA_BUS error */
1705 if (qc->err_mask & AC_ERR_ATA_BUS)
1706 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1707 AC_ERR_INVALID);
1708
1709 /* any real error trumps unknown error */
1710 if (qc->err_mask & ~AC_ERR_OTHER)
1711 qc->err_mask &= ~AC_ERR_OTHER;
1712
1713 /* SENSE_VALID trumps dev/unknown error and revalidation */
1714 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
1715 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
Tejun Heo4528e4d2006-07-08 20:17:26 +09001716 ehc->i.action &= ~ATA_EH_REVALIDATE;
Tejun Heo022bdb02006-05-15 20:58:22 +09001717 }
1718
1719 /* accumulate error info */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001720 ehc->i.dev = qc->dev;
Tejun Heo022bdb02006-05-15 20:58:22 +09001721 all_err_mask |= qc->err_mask;
1722 if (qc->flags & ATA_QCFLAG_IO)
1723 is_io = 1;
1724 }
1725
Tejun Heoa20f33f2006-05-16 12:58:24 +09001726 /* enforce default EH actions */
Tejun Heob51e9e52006-06-29 01:29:30 +09001727 if (ap->pflags & ATA_PFLAG_FROZEN ||
Tejun Heoa20f33f2006-05-16 12:58:24 +09001728 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
Tejun Heo4528e4d2006-07-08 20:17:26 +09001729 ehc->i.action |= ATA_EH_SOFTRESET;
Tejun Heoa20f33f2006-05-16 12:58:24 +09001730 else if (all_err_mask)
Tejun Heo4528e4d2006-07-08 20:17:26 +09001731 ehc->i.action |= ATA_EH_REVALIDATE;
Tejun Heo022bdb02006-05-15 20:58:22 +09001732
Tejun Heo47005f22006-06-19 18:27:23 +09001733 /* if we have offending qcs and the associated failed device */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001734 if (ehc->i.dev) {
Tejun Heo47005f22006-06-19 18:27:23 +09001735 /* speed down */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001736 ehc->i.action |= ata_eh_speed_down(ehc->i.dev, is_io,
1737 all_err_mask);
Tejun Heo47005f22006-06-19 18:27:23 +09001738
1739 /* perform per-dev EH action only on the offending device */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001740 ehc->i.dev_action[ehc->i.dev->devno] |=
1741 ehc->i.action & ATA_EH_PERDEV_MASK;
1742 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
Tejun Heo47005f22006-06-19 18:27:23 +09001743 }
1744
Tejun Heo022bdb02006-05-15 20:58:22 +09001745 DPRINTK("EXIT\n");
1746}
1747
1748/**
Tejun Heo9b1e2652007-08-06 18:36:24 +09001749 * ata_eh_autopsy - analyze error and determine recovery action
1750 * @ap: host port to perform autopsy on
1751 *
1752 * Analyze all links of @ap and determine why they failed and
1753 * which recovery actions are needed.
1754 *
1755 * LOCKING:
1756 * Kernel thread context (may sleep).
1757 */
Tejun Heofb7fd612007-09-23 13:14:12 +09001758void ata_eh_autopsy(struct ata_port *ap)
Tejun Heo9b1e2652007-08-06 18:36:24 +09001759{
1760 struct ata_link *link;
1761
1762 __ata_port_for_each_link(link, ap)
1763 ata_eh_link_autopsy(link);
1764}
1765
1766/**
1767 * ata_eh_link_report - report error handling to user
Tejun Heo02607312007-08-06 18:36:23 +09001768 * @link: ATA link EH is going on
Tejun Heo022bdb02006-05-15 20:58:22 +09001769 *
1770 * Report EH to user.
1771 *
1772 * LOCKING:
1773 * None.
1774 */
Tejun Heo9b1e2652007-08-06 18:36:24 +09001775static void ata_eh_link_report(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001776{
Tejun Heo02607312007-08-06 18:36:23 +09001777 struct ata_port *ap = link->ap;
1778 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001779 const char *frozen, *desc;
Tejun Heoa1e10f72007-08-18 13:28:49 +09001780 char tries_buf[6];
Tejun Heo022bdb02006-05-15 20:58:22 +09001781 int tag, nr_failed = 0;
1782
1783 desc = NULL;
1784 if (ehc->i.desc[0] != '\0')
1785 desc = ehc->i.desc;
1786
1787 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1788 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1789
Tejun Heo02607312007-08-06 18:36:23 +09001790 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001791 continue;
1792 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1793 continue;
1794
1795 nr_failed++;
1796 }
1797
1798 if (!nr_failed && !ehc->i.err_mask)
1799 return;
1800
1801 frozen = "";
Tejun Heob51e9e52006-06-29 01:29:30 +09001802 if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heo022bdb02006-05-15 20:58:22 +09001803 frozen = " frozen";
1804
Tejun Heoa1e10f72007-08-18 13:28:49 +09001805 memset(tries_buf, 0, sizeof(tries_buf));
1806 if (ap->eh_tries < ATA_EH_MAX_TRIES)
1807 snprintf(tries_buf, sizeof(tries_buf) - 1, " t%d",
1808 ap->eh_tries);
1809
Tejun Heo022bdb02006-05-15 20:58:22 +09001810 if (ehc->i.dev) {
Tejun Heoe8ee8452006-05-15 21:03:46 +09001811 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
Tejun Heoa1e10f72007-08-18 13:28:49 +09001812 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1813 ehc->i.err_mask, link->sactive, ehc->i.serror,
1814 ehc->i.action, frozen, tries_buf);
Tejun Heo022bdb02006-05-15 20:58:22 +09001815 if (desc)
Tejun Heob64bbc32007-07-16 14:29:39 +09001816 ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
Tejun Heo022bdb02006-05-15 20:58:22 +09001817 } else {
Tejun Heo02607312007-08-06 18:36:23 +09001818 ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
Tejun Heoa1e10f72007-08-18 13:28:49 +09001819 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1820 ehc->i.err_mask, link->sactive, ehc->i.serror,
1821 ehc->i.action, frozen, tries_buf);
Tejun Heo022bdb02006-05-15 20:58:22 +09001822 if (desc)
Tejun Heo02607312007-08-06 18:36:23 +09001823 ata_link_printk(link, KERN_ERR, "%s\n", desc);
Tejun Heo022bdb02006-05-15 20:58:22 +09001824 }
1825
1826 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
Tejun Heo8a937582006-11-14 22:36:12 +09001827 static const char *dma_str[] = {
1828 [DMA_BIDIRECTIONAL] = "bidi",
1829 [DMA_TO_DEVICE] = "out",
1830 [DMA_FROM_DEVICE] = "in",
1831 [DMA_NONE] = "",
1832 };
Tejun Heo022bdb02006-05-15 20:58:22 +09001833 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
Tejun Heo8a937582006-11-14 22:36:12 +09001834 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
Tejun Heo022bdb02006-05-15 20:58:22 +09001835
Tejun Heo02607312007-08-06 18:36:23 +09001836 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
1837 qc->dev->link != link || !qc->err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001838 continue;
1839
Tejun Heo8a937582006-11-14 22:36:12 +09001840 ata_dev_printk(qc->dev, KERN_ERR,
1841 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
Tejun Heo664e8502006-11-20 16:05:34 +09001842 "tag %d cdb 0x%x data %u %s\n "
Tejun Heo8a937582006-11-14 22:36:12 +09001843 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
Tejun Heo5335b722007-07-16 14:29:40 +09001844 "Emask 0x%x (%s)%s\n",
Tejun Heo8a937582006-11-14 22:36:12 +09001845 cmd->command, cmd->feature, cmd->nsect,
1846 cmd->lbal, cmd->lbam, cmd->lbah,
1847 cmd->hob_feature, cmd->hob_nsect,
1848 cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
Tejun Heo726f0782007-01-03 17:30:39 +09001849 cmd->device, qc->tag, qc->cdb[0], qc->nbytes,
Tejun Heo664e8502006-11-20 16:05:34 +09001850 dma_str[qc->dma_dir],
Tejun Heo8a937582006-11-14 22:36:12 +09001851 res->command, res->feature, res->nsect,
1852 res->lbal, res->lbam, res->lbah,
1853 res->hob_feature, res->hob_nsect,
1854 res->hob_lbal, res->hob_lbam, res->hob_lbah,
Tejun Heo5335b722007-07-16 14:29:40 +09001855 res->device, qc->err_mask, ata_err_string(qc->err_mask),
1856 qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
Tejun Heo022bdb02006-05-15 20:58:22 +09001857 }
1858}
1859
Tejun Heo9b1e2652007-08-06 18:36:24 +09001860/**
1861 * ata_eh_report - report error handling to user
1862 * @ap: ATA port to report EH about
1863 *
1864 * Report EH to user.
1865 *
1866 * LOCKING:
1867 * None.
1868 */
Tejun Heofb7fd612007-09-23 13:14:12 +09001869void ata_eh_report(struct ata_port *ap)
Tejun Heo9b1e2652007-08-06 18:36:24 +09001870{
1871 struct ata_link *link;
1872
1873 __ata_port_for_each_link(link, ap)
1874 ata_eh_link_report(link);
1875}
1876
Tejun Heocc0680a2007-08-06 18:36:23 +09001877static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
Tejun Heod4b2bab2007-02-02 16:50:52 +09001878 unsigned int *classes, unsigned long deadline)
Tejun Heod87fa382006-05-31 18:28:24 +09001879{
Tejun Heof58229f2007-08-06 18:36:23 +09001880 struct ata_device *dev;
1881 int rc;
Tejun Heod87fa382006-05-31 18:28:24 +09001882
Tejun Heocc0680a2007-08-06 18:36:23 +09001883 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09001884 classes[dev->devno] = ATA_DEV_UNKNOWN;
Tejun Heod87fa382006-05-31 18:28:24 +09001885
Tejun Heocc0680a2007-08-06 18:36:23 +09001886 rc = reset(link, classes, deadline);
Tejun Heod87fa382006-05-31 18:28:24 +09001887 if (rc)
1888 return rc;
1889
1890 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
1891 * is complete and convert all ATA_DEV_UNKNOWN to
1892 * ATA_DEV_NONE.
1893 */
Tejun Heocc0680a2007-08-06 18:36:23 +09001894 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09001895 if (classes[dev->devno] != ATA_DEV_UNKNOWN)
Tejun Heod87fa382006-05-31 18:28:24 +09001896 break;
1897
Tejun Heof58229f2007-08-06 18:36:23 +09001898 if (dev) {
Tejun Heocc0680a2007-08-06 18:36:23 +09001899 ata_link_for_each_dev(dev, link) {
Tejun Heof58229f2007-08-06 18:36:23 +09001900 if (classes[dev->devno] == ATA_DEV_UNKNOWN)
1901 classes[dev->devno] = ATA_DEV_NONE;
1902 }
1903 }
Tejun Heod87fa382006-05-31 18:28:24 +09001904
1905 return 0;
1906}
1907
Tejun Heo664faf02006-05-31 18:27:50 +09001908static int ata_eh_followup_srst_needed(int rc, int classify,
1909 const unsigned int *classes)
1910{
1911 if (rc == -EAGAIN)
1912 return 1;
1913 if (rc != 0)
1914 return 0;
1915 if (classify && classes[0] == ATA_DEV_UNKNOWN)
1916 return 1;
1917 return 0;
1918}
1919
Tejun Heofb7fd612007-09-23 13:14:12 +09001920int ata_eh_reset(struct ata_link *link, int classify,
1921 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
1922 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
Tejun Heo022bdb02006-05-15 20:58:22 +09001923{
Tejun Heo936fd732007-08-06 18:36:23 +09001924 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo664faf02006-05-31 18:27:50 +09001925 unsigned int *classes = ehc->classes;
Tejun Heo1cdaf532006-07-03 16:07:26 +09001926 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
Tejun Heo31daabd2007-02-02 16:50:52 +09001927 int try = 0;
Tejun Heof58229f2007-08-06 18:36:23 +09001928 struct ata_device *dev;
Tejun Heo31daabd2007-02-02 16:50:52 +09001929 unsigned long deadline;
Tejun Heof5914a42006-05-31 18:27:48 +09001930 unsigned int action;
Tejun Heo022bdb02006-05-15 20:58:22 +09001931 ata_reset_fn_t reset;
Tejun Heof58229f2007-08-06 18:36:23 +09001932 int rc;
Tejun Heo022bdb02006-05-15 20:58:22 +09001933
Tejun Heo13abf502006-07-10 23:18:46 +09001934 /* about to reset */
Tejun Heo955e57d2007-08-06 18:36:23 +09001935 ata_eh_about_to_do(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
Tejun Heo13abf502006-07-10 23:18:46 +09001936
Tejun Heof5914a42006-05-31 18:27:48 +09001937 /* Determine which reset to use and record in ehc->i.action.
1938 * prereset() may examine and modify it.
1939 */
1940 action = ehc->i.action;
1941 ehc->i.action &= ~ATA_EH_RESET_MASK;
Tejun Heo936fd732007-08-06 18:36:23 +09001942 if (softreset && (!hardreset || (!sata_set_spd_needed(link) &&
Tejun Heof5914a42006-05-31 18:27:48 +09001943 !(action & ATA_EH_HARDRESET))))
1944 ehc->i.action |= ATA_EH_SOFTRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001945 else
Tejun Heof5914a42006-05-31 18:27:48 +09001946 ehc->i.action |= ATA_EH_HARDRESET;
1947
1948 if (prereset) {
Tejun Heocc0680a2007-08-06 18:36:23 +09001949 rc = prereset(link, jiffies + ATA_EH_PRERESET_TIMEOUT);
Tejun Heof5914a42006-05-31 18:27:48 +09001950 if (rc) {
Alan Coxc9619222006-09-26 17:53:38 +01001951 if (rc == -ENOENT) {
Tejun Heocc0680a2007-08-06 18:36:23 +09001952 ata_link_printk(link, KERN_DEBUG,
Tejun Heo4aa9ab62007-03-12 17:24:08 +09001953 "port disabled. ignoring.\n");
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001954 ehc->i.action &= ~ATA_EH_RESET_MASK;
Tejun Heo4aa9ab62007-03-12 17:24:08 +09001955
Tejun Heo936fd732007-08-06 18:36:23 +09001956 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09001957 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heo4aa9ab62007-03-12 17:24:08 +09001958
1959 rc = 0;
Alan Coxc9619222006-09-26 17:53:38 +01001960 } else
Tejun Heocc0680a2007-08-06 18:36:23 +09001961 ata_link_printk(link, KERN_ERR,
Tejun Heof5914a42006-05-31 18:27:48 +09001962 "prereset failed (errno=%d)\n", rc);
Tejun Heofccb6ea2007-07-16 14:29:41 +09001963 goto out;
Tejun Heof5914a42006-05-31 18:27:48 +09001964 }
1965 }
1966
1967 /* prereset() might have modified ehc->i.action */
1968 if (ehc->i.action & ATA_EH_HARDRESET)
Tejun Heo022bdb02006-05-15 20:58:22 +09001969 reset = hardreset;
Tejun Heof5914a42006-05-31 18:27:48 +09001970 else if (ehc->i.action & ATA_EH_SOFTRESET)
1971 reset = softreset;
1972 else {
1973 /* prereset told us not to reset, bang classes and return */
Tejun Heo936fd732007-08-06 18:36:23 +09001974 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09001975 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heofccb6ea2007-07-16 14:29:41 +09001976 rc = 0;
1977 goto out;
Tejun Heof5914a42006-05-31 18:27:48 +09001978 }
1979
1980 /* did prereset() screw up? if so, fix up to avoid oopsing */
1981 if (!reset) {
Tejun Heof5914a42006-05-31 18:27:48 +09001982 if (softreset)
1983 reset = softreset;
1984 else
1985 reset = hardreset;
1986 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001987
1988 retry:
Tejun Heo31daabd2007-02-02 16:50:52 +09001989 deadline = jiffies + ata_eh_reset_timeouts[try++];
1990
Tejun Heo3e706392006-05-31 18:28:11 +09001991 /* shut up during boot probing */
1992 if (verbose)
Tejun Heocc0680a2007-08-06 18:36:23 +09001993 ata_link_printk(link, KERN_INFO, "%s resetting link\n",
Tejun Heo3e706392006-05-31 18:28:11 +09001994 reset == softreset ? "soft" : "hard");
Tejun Heo022bdb02006-05-15 20:58:22 +09001995
Tejun Heo13abf502006-07-10 23:18:46 +09001996 /* mark that this EH session started with reset */
Tejun Heo0d64a232007-04-23 02:41:05 +09001997 if (reset == hardreset)
1998 ehc->i.flags |= ATA_EHI_DID_HARDRESET;
1999 else
2000 ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09002001
Tejun Heocc0680a2007-08-06 18:36:23 +09002002 rc = ata_do_reset(link, reset, classes, deadline);
Tejun Heo022bdb02006-05-15 20:58:22 +09002003
Tejun Heo664faf02006-05-31 18:27:50 +09002004 if (reset == hardreset &&
2005 ata_eh_followup_srst_needed(rc, classify, classes)) {
2006 /* okay, let's do follow-up softreset */
Tejun Heo664faf02006-05-31 18:27:50 +09002007 reset = softreset;
2008
2009 if (!reset) {
Tejun Heocc0680a2007-08-06 18:36:23 +09002010 ata_link_printk(link, KERN_ERR,
Tejun Heo664faf02006-05-31 18:27:50 +09002011 "follow-up softreset required "
2012 "but no softreset avaliable\n");
Tejun Heofccb6ea2007-07-16 14:29:41 +09002013 rc = -EINVAL;
2014 goto out;
Tejun Heo664faf02006-05-31 18:27:50 +09002015 }
2016
Tejun Heo955e57d2007-08-06 18:36:23 +09002017 ata_eh_about_to_do(link, NULL, ATA_EH_RESET_MASK);
Tejun Heocc0680a2007-08-06 18:36:23 +09002018 rc = ata_do_reset(link, reset, classes, deadline);
Tejun Heo664faf02006-05-31 18:27:50 +09002019
2020 if (rc == 0 && classify &&
2021 classes[0] == ATA_DEV_UNKNOWN) {
Tejun Heocc0680a2007-08-06 18:36:23 +09002022 ata_link_printk(link, KERN_ERR,
Tejun Heo664faf02006-05-31 18:27:50 +09002023 "classification failed\n");
Tejun Heofccb6ea2007-07-16 14:29:41 +09002024 rc = -EINVAL;
2025 goto out;
Tejun Heo664faf02006-05-31 18:27:50 +09002026 }
2027 }
2028
Tejun Heo31daabd2007-02-02 16:50:52 +09002029 if (rc && try < ARRAY_SIZE(ata_eh_reset_timeouts)) {
2030 unsigned long now = jiffies;
Tejun Heo664faf02006-05-31 18:27:50 +09002031
Tejun Heo31daabd2007-02-02 16:50:52 +09002032 if (time_before(now, deadline)) {
2033 unsigned long delta = deadline - jiffies;
Tejun Heo664faf02006-05-31 18:27:50 +09002034
Tejun Heocc0680a2007-08-06 18:36:23 +09002035 ata_link_printk(link, KERN_WARNING, "reset failed "
Tejun Heo31daabd2007-02-02 16:50:52 +09002036 "(errno=%d), retrying in %u secs\n",
2037 rc, (jiffies_to_msecs(delta) + 999) / 1000);
Tejun Heo022bdb02006-05-15 20:58:22 +09002038
Tejun Heo31daabd2007-02-02 16:50:52 +09002039 schedule_timeout_uninterruptible(delta);
2040 }
2041
Tejun Heof1545152007-07-16 14:29:40 +09002042 if (rc == -EPIPE ||
Tejun Heo31daabd2007-02-02 16:50:52 +09002043 try == ARRAY_SIZE(ata_eh_reset_timeouts) - 1)
Tejun Heo936fd732007-08-06 18:36:23 +09002044 sata_down_spd_limit(link);
Tejun Heo022bdb02006-05-15 20:58:22 +09002045 if (hardreset)
2046 reset = hardreset;
2047 goto retry;
2048 }
2049
2050 if (rc == 0) {
Tejun Heo008a7892007-07-16 14:29:40 +09002051 u32 sstatus;
2052
Tejun Heo20952b62006-05-31 18:27:23 +09002053 /* After the reset, the device state is PIO 0 and the
2054 * controller state is undefined. Record the mode.
2055 */
Tejun Heo936fd732007-08-06 18:36:23 +09002056 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09002057 dev->pio_mode = XFER_PIO_0;
Tejun Heo20952b62006-05-31 18:27:23 +09002058
Tejun Heo008a7892007-07-16 14:29:40 +09002059 /* record current link speed */
Tejun Heo936fd732007-08-06 18:36:23 +09002060 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2061 link->sata_spd = (sstatus >> 4) & 0xf;
Tejun Heo008a7892007-07-16 14:29:40 +09002062
Tejun Heo022bdb02006-05-15 20:58:22 +09002063 if (postreset)
Tejun Heocc0680a2007-08-06 18:36:23 +09002064 postreset(link, classes);
Tejun Heo022bdb02006-05-15 20:58:22 +09002065
2066 /* reset successful, schedule revalidation */
Tejun Heo955e57d2007-08-06 18:36:23 +09002067 ata_eh_done(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
Tejun Heo022bdb02006-05-15 20:58:22 +09002068 ehc->i.action |= ATA_EH_REVALIDATE;
2069 }
Tejun Heofccb6ea2007-07-16 14:29:41 +09002070 out:
2071 /* clear hotplug flag */
2072 ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
Tejun Heo022bdb02006-05-15 20:58:22 +09002073 return rc;
2074}
2075
Tejun Heo02607312007-08-06 18:36:23 +09002076static int ata_eh_revalidate_and_attach(struct ata_link *link,
Tejun Heo084fe632006-05-31 18:28:03 +09002077 struct ata_device **r_failed_dev)
Tejun Heo022bdb02006-05-15 20:58:22 +09002078{
Tejun Heo02607312007-08-06 18:36:23 +09002079 struct ata_port *ap = link->ap;
2080 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09002081 struct ata_device *dev;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002082 unsigned int new_mask = 0;
Tejun Heo084fe632006-05-31 18:28:03 +09002083 unsigned long flags;
Tejun Heof58229f2007-08-06 18:36:23 +09002084 int rc = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09002085
2086 DPRINTK("ENTER\n");
2087
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002088 /* For PATA drive side cable detection to work, IDENTIFY must
2089 * be done backwards such that PDIAG- is released by the slave
2090 * device before the master device is identified.
2091 */
Tejun Heo02607312007-08-06 18:36:23 +09002092 ata_link_for_each_dev_reverse(dev, link) {
Tejun Heof58229f2007-08-06 18:36:23 +09002093 unsigned int action = ata_eh_dev_action(dev);
2094 unsigned int readid_flags = 0;
Tejun Heo47005f22006-06-19 18:27:23 +09002095
Tejun Heobff04642006-11-10 18:08:10 +09002096 if (ehc->i.flags & ATA_EHI_DID_RESET)
2097 readid_flags |= ATA_READID_POSTRESET;
2098
Tejun Heo9666f402007-05-04 21:27:47 +02002099 if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
Tejun Heo02607312007-08-06 18:36:23 +09002100 if (ata_link_offline(link)) {
Tejun Heo022bdb02006-05-15 20:58:22 +09002101 rc = -EIO;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002102 goto err;
Tejun Heo022bdb02006-05-15 20:58:22 +09002103 }
2104
Tejun Heo02607312007-08-06 18:36:23 +09002105 ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
Tejun Heo422c9da2007-09-23 13:14:12 +09002106 rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
2107 readid_flags);
Tejun Heo022bdb02006-05-15 20:58:22 +09002108 if (rc)
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002109 goto err;
Tejun Heo022bdb02006-05-15 20:58:22 +09002110
Tejun Heo02607312007-08-06 18:36:23 +09002111 ata_eh_done(link, dev, ATA_EH_REVALIDATE);
Tejun Heo47005f22006-06-19 18:27:23 +09002112
Tejun Heobaa1e782006-11-01 18:39:27 +09002113 /* Configuration may have changed, reconfigure
2114 * transfer mode.
2115 */
2116 ehc->i.flags |= ATA_EHI_SETMODE;
2117
zhao, forrest3057ac32006-06-12 12:01:34 +08002118 /* schedule the scsi_rescan_device() here */
2119 queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
Tejun Heo084fe632006-05-31 18:28:03 +09002120 } else if (dev->class == ATA_DEV_UNKNOWN &&
2121 ehc->tries[dev->devno] &&
2122 ata_class_enabled(ehc->classes[dev->devno])) {
2123 dev->class = ehc->classes[dev->devno];
2124
Tejun Heobff04642006-11-10 18:08:10 +09002125 rc = ata_dev_read_id(dev, &dev->class, readid_flags,
2126 dev->id);
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002127 switch (rc) {
2128 case 0:
Tejun Heof58229f2007-08-06 18:36:23 +09002129 new_mask |= 1 << dev->devno;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002130 break;
2131 case -ENOENT:
Tejun Heo55a8e2c2006-11-10 18:08:10 +09002132 /* IDENTIFY was issued to non-existent
2133 * device. No need to reset. Just
2134 * thaw and kill the device.
2135 */
2136 ata_eh_thaw_port(ap);
2137 dev->class = ATA_DEV_UNKNOWN;
Tejun Heo084fe632006-05-31 18:28:03 +09002138 break;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002139 default:
2140 dev->class = ATA_DEV_UNKNOWN;
2141 goto err;
Tejun Heo55a8e2c2006-11-10 18:08:10 +09002142 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002143 }
2144 }
2145
Tejun Heoc1c4e8d2007-04-23 02:05:53 +09002146 /* PDIAG- should have been released, ask cable type if post-reset */
Tejun Heo9b1e2652007-08-06 18:36:24 +09002147 if (ata_is_host_link(link) && ap->ops->cable_detect &&
2148 (ehc->i.flags & ATA_EHI_DID_RESET))
Tejun Heoc1c4e8d2007-04-23 02:05:53 +09002149 ap->cbl = ap->ops->cable_detect(ap);
2150
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002151 /* Configure new devices forward such that user doesn't see
2152 * device detection messages backwards.
2153 */
Tejun Heo02607312007-08-06 18:36:23 +09002154 ata_link_for_each_dev(dev, link) {
Tejun Heof58229f2007-08-06 18:36:23 +09002155 if (!(new_mask & (1 << dev->devno)))
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002156 continue;
2157
2158 ehc->i.flags |= ATA_EHI_PRINTINFO;
2159 rc = ata_dev_configure(dev);
2160 ehc->i.flags &= ~ATA_EHI_PRINTINFO;
2161 if (rc)
2162 goto err;
2163
2164 spin_lock_irqsave(ap->lock, flags);
2165 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
2166 spin_unlock_irqrestore(ap->lock, flags);
2167
2168 /* new device discovered, configure xfermode */
2169 ehc->i.flags |= ATA_EHI_SETMODE;
2170 }
2171
2172 return 0;
2173
2174 err:
2175 *r_failed_dev = dev;
2176 DPRINTK("EXIT rc=%d\n", rc);
Tejun Heo022bdb02006-05-15 20:58:22 +09002177 return rc;
2178}
2179
Tejun Heo02607312007-08-06 18:36:23 +09002180static int ata_link_nr_enabled(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09002181{
Tejun Heof58229f2007-08-06 18:36:23 +09002182 struct ata_device *dev;
2183 int cnt = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09002184
Tejun Heo02607312007-08-06 18:36:23 +09002185 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09002186 if (ata_dev_enabled(dev))
Tejun Heo022bdb02006-05-15 20:58:22 +09002187 cnt++;
2188 return cnt;
2189}
2190
Tejun Heo02607312007-08-06 18:36:23 +09002191static int ata_link_nr_vacant(struct ata_link *link)
Tejun Heo084fe632006-05-31 18:28:03 +09002192{
Tejun Heof58229f2007-08-06 18:36:23 +09002193 struct ata_device *dev;
2194 int cnt = 0;
Tejun Heo084fe632006-05-31 18:28:03 +09002195
Tejun Heo02607312007-08-06 18:36:23 +09002196 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09002197 if (dev->class == ATA_DEV_UNKNOWN)
Tejun Heo084fe632006-05-31 18:28:03 +09002198 cnt++;
2199 return cnt;
2200}
2201
Tejun Heo02607312007-08-06 18:36:23 +09002202static int ata_eh_skip_recovery(struct ata_link *link)
Tejun Heo084fe632006-05-31 18:28:03 +09002203{
Tejun Heo02607312007-08-06 18:36:23 +09002204 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heof58229f2007-08-06 18:36:23 +09002205 struct ata_device *dev;
Tejun Heo084fe632006-05-31 18:28:03 +09002206
Tejun Heo7c8c2cf2006-07-10 23:18:23 +09002207 /* thaw frozen port, resume link and recover failed devices */
Tejun Heo02607312007-08-06 18:36:23 +09002208 if ((link->ap->pflags & ATA_PFLAG_FROZEN) ||
2209 (ehc->i.flags & ATA_EHI_RESUME_LINK) || ata_link_nr_enabled(link))
Tejun Heo084fe632006-05-31 18:28:03 +09002210 return 0;
2211
2212 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
Tejun Heo02607312007-08-06 18:36:23 +09002213 ata_link_for_each_dev(dev, link) {
Tejun Heo084fe632006-05-31 18:28:03 +09002214 if (dev->class == ATA_DEV_UNKNOWN &&
2215 ehc->classes[dev->devno] != ATA_DEV_NONE)
2216 return 0;
2217 }
2218
2219 return 1;
2220}
2221
Tejun Heo9b1e2652007-08-06 18:36:24 +09002222static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
Tejun Heofee7ca72007-07-01 19:05:58 +09002223{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002224 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heofee7ca72007-07-01 19:05:58 +09002225
2226 ehc->tries[dev->devno]--;
2227
2228 switch (err) {
2229 case -ENODEV:
2230 /* device missing or wrong IDENTIFY data, schedule probing */
2231 ehc->i.probe_mask |= (1 << dev->devno);
2232 case -EINVAL:
2233 /* give it just one more chance */
2234 ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
2235 case -EIO:
2236 if (ehc->tries[dev->devno] == 1) {
2237 /* This is the last chance, better to slow
2238 * down than lose it.
2239 */
Tejun Heo936fd732007-08-06 18:36:23 +09002240 sata_down_spd_limit(dev->link);
Tejun Heofee7ca72007-07-01 19:05:58 +09002241 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2242 }
2243 }
2244
2245 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2246 /* disable device if it has used up all its chances */
2247 ata_dev_disable(dev);
2248
2249 /* detach if offline */
Tejun Heo936fd732007-08-06 18:36:23 +09002250 if (ata_link_offline(dev->link))
Tejun Heofee7ca72007-07-01 19:05:58 +09002251 ata_eh_detach_dev(dev);
2252
2253 /* probe if requested */
2254 if ((ehc->i.probe_mask & (1 << dev->devno)) &&
2255 !(ehc->did_probe_mask & (1 << dev->devno))) {
2256 ata_eh_detach_dev(dev);
2257 ata_dev_init(dev);
2258
2259 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2260 ehc->did_probe_mask |= (1 << dev->devno);
2261 ehc->i.action |= ATA_EH_SOFTRESET;
2262 }
Tejun Heo9b1e2652007-08-06 18:36:24 +09002263
2264 return 1;
Tejun Heofee7ca72007-07-01 19:05:58 +09002265 } else {
2266 /* soft didn't work? be haaaaard */
2267 if (ehc->i.flags & ATA_EHI_DID_RESET)
2268 ehc->i.action |= ATA_EH_HARDRESET;
2269 else
2270 ehc->i.action |= ATA_EH_SOFTRESET;
Tejun Heo9b1e2652007-08-06 18:36:24 +09002271
2272 return 0;
Tejun Heofee7ca72007-07-01 19:05:58 +09002273 }
2274}
2275
Tejun Heo022bdb02006-05-15 20:58:22 +09002276/**
2277 * ata_eh_recover - recover host port after error
2278 * @ap: host port to recover
Tejun Heof5914a42006-05-31 18:27:48 +09002279 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09002280 * @softreset: softreset method (can be NULL)
2281 * @hardreset: hardreset method (can be NULL)
2282 * @postreset: postreset method (can be NULL)
Tejun Heo9b1e2652007-08-06 18:36:24 +09002283 * @r_failed_link: out parameter for failed link
Tejun Heo022bdb02006-05-15 20:58:22 +09002284 *
2285 * This is the alpha and omega, eum and yang, heart and soul of
2286 * libata exception handling. On entry, actions required to
Tejun Heo9b1e2652007-08-06 18:36:24 +09002287 * recover each link and hotplug requests are recorded in the
2288 * link's eh_context. This function executes all the operations
2289 * with appropriate retrials and fallbacks to resurrect failed
Tejun Heo084fe632006-05-31 18:28:03 +09002290 * devices, detach goners and greet newcomers.
Tejun Heo022bdb02006-05-15 20:58:22 +09002291 *
2292 * LOCKING:
2293 * Kernel thread context (may sleep).
2294 *
2295 * RETURNS:
2296 * 0 on success, -errno on failure.
2297 */
Tejun Heofb7fd612007-09-23 13:14:12 +09002298int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
2299 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2300 ata_postreset_fn_t postreset,
2301 struct ata_link **r_failed_link)
Tejun Heo022bdb02006-05-15 20:58:22 +09002302{
Tejun Heo9b1e2652007-08-06 18:36:24 +09002303 struct ata_link *link;
Tejun Heo022bdb02006-05-15 20:58:22 +09002304 struct ata_device *dev;
Tejun Heo9b1e2652007-08-06 18:36:24 +09002305 int nr_failed_devs, nr_disabled_devs;
2306 int reset, rc;
Tejun Heo022bdb02006-05-15 20:58:22 +09002307
2308 DPRINTK("ENTER\n");
2309
2310 /* prep for recovery */
Tejun Heo9b1e2652007-08-06 18:36:24 +09002311 ata_port_for_each_link(link, ap) {
2312 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo084fe632006-05-31 18:28:03 +09002313
Tejun Heo9b1e2652007-08-06 18:36:24 +09002314 ata_link_for_each_dev(dev, link) {
2315 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
Tejun Heo79a55b72007-01-18 17:22:18 +09002316
Tejun Heo9b1e2652007-08-06 18:36:24 +09002317 /* collect port action mask recorded in dev actions */
2318 ehc->i.action |= ehc->i.dev_action[dev->devno] &
2319 ~ATA_EH_PERDEV_MASK;
2320 ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
Tejun Heo084fe632006-05-31 18:28:03 +09002321
Tejun Heo9b1e2652007-08-06 18:36:24 +09002322 /* process hotplug request */
2323 if (dev->flags & ATA_DFLAG_DETACH)
2324 ata_eh_detach_dev(dev);
2325
2326 if (!ata_dev_enabled(dev) &&
2327 ((ehc->i.probe_mask & (1 << dev->devno)) &&
2328 !(ehc->did_probe_mask & (1 << dev->devno)))) {
2329 ata_eh_detach_dev(dev);
2330 ata_dev_init(dev);
2331 ehc->did_probe_mask |= (1 << dev->devno);
2332 ehc->i.action |= ATA_EH_SOFTRESET;
2333 }
Tejun Heo084fe632006-05-31 18:28:03 +09002334 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002335 }
2336
2337 retry:
Tejun Heo022bdb02006-05-15 20:58:22 +09002338 rc = 0;
Tejun Heo9b1e2652007-08-06 18:36:24 +09002339 nr_failed_devs = 0;
2340 nr_disabled_devs = 0;
2341 reset = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09002342
Tejun Heoaeb2ecd2006-06-12 14:11:43 +09002343 /* if UNLOADING, finish immediately */
Tejun Heob51e9e52006-06-29 01:29:30 +09002344 if (ap->pflags & ATA_PFLAG_UNLOADING)
Tejun Heoaeb2ecd2006-06-12 14:11:43 +09002345 goto out;
2346
Tejun Heo9b1e2652007-08-06 18:36:24 +09002347 /* prep for EH */
2348 ata_port_for_each_link(link, ap) {
2349 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09002350
Tejun Heo9b1e2652007-08-06 18:36:24 +09002351 /* skip EH if possible. */
2352 if (ata_eh_skip_recovery(link))
2353 ehc->i.action = 0;
2354
2355 /* do we need to reset? */
2356 if (ehc->i.action & ATA_EH_RESET_MASK)
2357 reset = 1;
2358
2359 ata_link_for_each_dev(dev, link)
2360 ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
2361 }
Tejun Heo084fe632006-05-31 18:28:03 +09002362
Tejun Heo022bdb02006-05-15 20:58:22 +09002363 /* reset */
Tejun Heo9b1e2652007-08-06 18:36:24 +09002364 if (reset) {
Tejun Heo022bdb02006-05-15 20:58:22 +09002365 ata_eh_freeze_port(ap);
2366
Tejun Heo9b1e2652007-08-06 18:36:24 +09002367 ata_port_for_each_link(link, ap) {
2368 struct ata_eh_context *ehc = &link->eh_context;
2369
2370 if (!(ehc->i.action & ATA_EH_RESET_MASK))
2371 continue;
2372
2373 rc = ata_eh_reset(link, ata_link_nr_vacant(link),
2374 prereset, softreset, hardreset,
2375 postreset);
2376 if (rc) {
2377 ata_link_printk(link, KERN_ERR,
2378 "reset failed, giving up\n");
2379 goto out;
2380 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002381 }
2382
2383 ata_eh_thaw_port(ap);
2384 }
2385
Tejun Heo9b1e2652007-08-06 18:36:24 +09002386 /* the rest */
2387 ata_port_for_each_link(link, ap) {
2388 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09002389
Tejun Heo9b1e2652007-08-06 18:36:24 +09002390 /* revalidate existing devices and attach new ones */
2391 rc = ata_eh_revalidate_and_attach(link, &dev);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002392 if (rc)
Tejun Heo022bdb02006-05-15 20:58:22 +09002393 goto dev_fail;
Tejun Heo9b1e2652007-08-06 18:36:24 +09002394
2395 /* configure transfer mode if necessary */
2396 if (ehc->i.flags & ATA_EHI_SETMODE) {
2397 rc = ata_set_mode(link, &dev);
2398 if (rc)
2399 goto dev_fail;
2400 ehc->i.flags &= ~ATA_EHI_SETMODE;
2401 }
2402
2403 /* this link is okay now */
2404 ehc->i.flags = 0;
2405 continue;
2406
2407 dev_fail:
2408 nr_failed_devs++;
2409 if (ata_eh_handle_dev_fail(dev, rc))
2410 nr_disabled_devs++;
2411
2412 if (ap->pflags & ATA_PFLAG_FROZEN)
2413 break;
Tejun Heo022bdb02006-05-15 20:58:22 +09002414 }
2415
Tejun Heo9b1e2652007-08-06 18:36:24 +09002416 if (nr_failed_devs) {
2417 if (nr_failed_devs != nr_disabled_devs) {
2418 ata_port_printk(ap, KERN_WARNING, "failed to recover "
2419 "some devices, retrying in 5 secs\n");
2420 ssleep(5);
2421 } else {
2422 /* no device left to recover, repeat fast */
2423 msleep(500);
2424 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002425
Tejun Heo9b1e2652007-08-06 18:36:24 +09002426 goto retry;
Tejun Heo022bdb02006-05-15 20:58:22 +09002427 }
2428
Tejun Heo022bdb02006-05-15 20:58:22 +09002429 out:
Tejun Heo9b1e2652007-08-06 18:36:24 +09002430 if (rc && r_failed_link)
2431 *r_failed_link = link;
Tejun Heo022bdb02006-05-15 20:58:22 +09002432
2433 DPRINTK("EXIT, rc=%d\n", rc);
2434 return rc;
2435}
2436
2437/**
2438 * ata_eh_finish - finish up EH
2439 * @ap: host port to finish EH for
2440 *
2441 * Recovery is complete. Clean up EH states and retry or finish
2442 * failed qcs.
2443 *
2444 * LOCKING:
2445 * None.
2446 */
Tejun Heofb7fd612007-09-23 13:14:12 +09002447void ata_eh_finish(struct ata_port *ap)
Tejun Heo022bdb02006-05-15 20:58:22 +09002448{
2449 int tag;
2450
2451 /* retry or finish qcs */
2452 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2453 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2454
2455 if (!(qc->flags & ATA_QCFLAG_FAILED))
2456 continue;
2457
2458 if (qc->err_mask) {
2459 /* FIXME: Once EH migration is complete,
2460 * generate sense data in this function,
2461 * considering both err_mask and tf.
2462 */
2463 if (qc->err_mask & AC_ERR_INVALID)
2464 ata_eh_qc_complete(qc);
2465 else
2466 ata_eh_qc_retry(qc);
2467 } else {
2468 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2469 ata_eh_qc_complete(qc);
2470 } else {
2471 /* feed zero TF to sense generation */
2472 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2473 ata_eh_qc_retry(qc);
2474 }
2475 }
2476 }
2477}
2478
2479/**
2480 * ata_do_eh - do standard error handling
2481 * @ap: host port to handle error for
Tejun Heof5914a42006-05-31 18:27:48 +09002482 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09002483 * @softreset: softreset method (can be NULL)
2484 * @hardreset: hardreset method (can be NULL)
2485 * @postreset: postreset method (can be NULL)
2486 *
2487 * Perform standard error handling sequence.
2488 *
2489 * LOCKING:
2490 * Kernel thread context (may sleep).
2491 */
Tejun Heof5914a42006-05-31 18:27:48 +09002492void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2493 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2494 ata_postreset_fn_t postreset)
Tejun Heo022bdb02006-05-15 20:58:22 +09002495{
Tejun Heo9b1e2652007-08-06 18:36:24 +09002496 struct ata_device *dev;
2497 int rc;
2498
2499 ata_eh_autopsy(ap);
2500 ata_eh_report(ap);
2501
2502 rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
2503 NULL);
2504 if (rc) {
2505 ata_link_for_each_dev(dev, &ap->link)
2506 ata_dev_disable(dev);
2507 }
2508
Tejun Heo022bdb02006-05-15 20:58:22 +09002509 ata_eh_finish(ap);
2510}
Tejun Heo500530f2006-07-03 16:07:27 +09002511
Tejun Heo6ffa01d2007-03-02 17:32:47 +09002512#ifdef CONFIG_PM
Tejun Heo500530f2006-07-03 16:07:27 +09002513/**
2514 * ata_eh_handle_port_suspend - perform port suspend operation
2515 * @ap: port to suspend
2516 *
2517 * Suspend @ap.
2518 *
2519 * LOCKING:
2520 * Kernel thread context (may sleep).
2521 */
2522static void ata_eh_handle_port_suspend(struct ata_port *ap)
2523{
2524 unsigned long flags;
2525 int rc = 0;
2526
2527 /* are we suspending? */
2528 spin_lock_irqsave(ap->lock, flags);
2529 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2530 ap->pm_mesg.event == PM_EVENT_ON) {
2531 spin_unlock_irqrestore(ap->lock, flags);
2532 return;
2533 }
2534 spin_unlock_irqrestore(ap->lock, flags);
2535
2536 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
2537
Tejun Heo64578a32007-05-15 03:28:16 +09002538 /* tell ACPI we're suspending */
2539 rc = ata_acpi_on_suspend(ap);
2540 if (rc)
2541 goto out;
2542
Tejun Heo500530f2006-07-03 16:07:27 +09002543 /* suspend */
2544 ata_eh_freeze_port(ap);
2545
2546 if (ap->ops->port_suspend)
2547 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
2548
Tejun Heo64578a32007-05-15 03:28:16 +09002549 out:
Tejun Heo500530f2006-07-03 16:07:27 +09002550 /* report result */
2551 spin_lock_irqsave(ap->lock, flags);
2552
2553 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
2554 if (rc == 0)
2555 ap->pflags |= ATA_PFLAG_SUSPENDED;
Tejun Heo64578a32007-05-15 03:28:16 +09002556 else if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heo500530f2006-07-03 16:07:27 +09002557 ata_port_schedule_eh(ap);
2558
2559 if (ap->pm_result) {
2560 *ap->pm_result = rc;
2561 ap->pm_result = NULL;
2562 }
2563
2564 spin_unlock_irqrestore(ap->lock, flags);
2565
2566 return;
2567}
2568
2569/**
2570 * ata_eh_handle_port_resume - perform port resume operation
2571 * @ap: port to resume
2572 *
2573 * Resume @ap.
2574 *
Tejun Heo500530f2006-07-03 16:07:27 +09002575 * LOCKING:
2576 * Kernel thread context (may sleep).
2577 */
2578static void ata_eh_handle_port_resume(struct ata_port *ap)
2579{
Tejun Heo500530f2006-07-03 16:07:27 +09002580 unsigned long flags;
Tejun Heo9666f402007-05-04 21:27:47 +02002581 int rc = 0;
Tejun Heo500530f2006-07-03 16:07:27 +09002582
2583 /* are we resuming? */
2584 spin_lock_irqsave(ap->lock, flags);
2585 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2586 ap->pm_mesg.event != PM_EVENT_ON) {
2587 spin_unlock_irqrestore(ap->lock, flags);
2588 return;
2589 }
2590 spin_unlock_irqrestore(ap->lock, flags);
2591
Tejun Heo9666f402007-05-04 21:27:47 +02002592 WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
Tejun Heo500530f2006-07-03 16:07:27 +09002593
2594 if (ap->ops->port_resume)
2595 rc = ap->ops->port_resume(ap);
2596
Tejun Heo67465442007-05-15 03:28:16 +09002597 /* tell ACPI that we're resuming */
2598 ata_acpi_on_resume(ap);
2599
Tejun Heo9666f402007-05-04 21:27:47 +02002600 /* report result */
Tejun Heo500530f2006-07-03 16:07:27 +09002601 spin_lock_irqsave(ap->lock, flags);
2602 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
2603 if (ap->pm_result) {
2604 *ap->pm_result = rc;
2605 ap->pm_result = NULL;
2606 }
2607 spin_unlock_irqrestore(ap->lock, flags);
2608}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09002609#endif /* CONFIG_PM */