blob: 419552603a1640a3ca892966a477986116d057a6 [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>
Jeff Garzik28555682007-10-11 17:12:35 -040036#include <linux/pci.h>
Tejun Heoece1d632006-04-02 18:51:53 +090037#include <scsi/scsi.h>
38#include <scsi/scsi_host.h>
39#include <scsi/scsi_eh.h>
40#include <scsi/scsi_device.h>
41#include <scsi/scsi_cmnd.h>
Jeff Garzikc6fd2802006-08-10 07:31:37 -040042#include "../scsi/scsi_transport_api.h"
Tejun Heoece1d632006-04-02 18:51:53 +090043
44#include <linux/libata.h>
45
46#include "libata.h"
47
Tejun Heo7d47e8d2007-02-02 16:22:31 +090048enum {
Tejun Heo3884f7b2007-11-27 19:28:56 +090049 /* speed down verdicts */
Tejun Heo7d47e8d2007-02-02 16:22:31 +090050 ATA_EH_SPDN_NCQ_OFF = (1 << 0),
51 ATA_EH_SPDN_SPEED_DOWN = (1 << 1),
52 ATA_EH_SPDN_FALLBACK_TO_PIO = (1 << 2),
Tejun Heo76326ac2007-11-27 19:28:59 +090053 ATA_EH_SPDN_KEEP_ERRORS = (1 << 3),
Tejun Heo3884f7b2007-11-27 19:28:56 +090054
55 /* error flags */
56 ATA_EFLAG_IS_IO = (1 << 0),
Tejun Heo76326ac2007-11-27 19:28:59 +090057 ATA_EFLAG_DUBIOUS_XFER = (1 << 1),
Tejun Heo3884f7b2007-11-27 19:28:56 +090058
59 /* error categories */
60 ATA_ECAT_NONE = 0,
61 ATA_ECAT_ATA_BUS = 1,
62 ATA_ECAT_TOUT_HSM = 2,
63 ATA_ECAT_UNK_DEV = 3,
Tejun Heo76326ac2007-11-27 19:28:59 +090064 ATA_ECAT_DUBIOUS_ATA_BUS = 4,
65 ATA_ECAT_DUBIOUS_TOUT_HSM = 5,
66 ATA_ECAT_DUBIOUS_UNK_DEV = 6,
67 ATA_ECAT_NR = 7,
68
69 ATA_ECAT_DUBIOUS_BASE = ATA_ECAT_DUBIOUS_ATA_BUS,
Tejun Heo7d47e8d2007-02-02 16:22:31 +090070};
71
Tejun Heo31daabd2007-02-02 16:50:52 +090072/* Waiting in ->prereset can never be reliable. It's sometimes nice
73 * to wait there but it can't be depended upon; otherwise, we wouldn't
74 * be resetting. Just give it enough time for most drives to spin up.
75 */
76enum {
77 ATA_EH_PRERESET_TIMEOUT = 10 * HZ,
Tejun Heo5ddf24c2007-07-16 14:29:41 +090078 ATA_EH_FASTDRAIN_INTERVAL = 3 * HZ,
Tejun Heo31daabd2007-02-02 16:50:52 +090079};
80
81/* The following table determines how we sequence resets. Each entry
82 * represents timeout for that try. The first try can be soft or
83 * hardreset. All others are hardreset if available. In most cases
84 * the first reset w/ 10sec timeout should succeed. Following entries
85 * are mostly for error handling, hotplug and retarded devices.
86 */
87static const unsigned long ata_eh_reset_timeouts[] = {
88 10 * HZ, /* most drives spin up by 10sec */
89 10 * HZ, /* > 99% working drives spin up before 20sec */
90 35 * HZ, /* give > 30 secs of idleness for retarded devices */
91 5 * HZ, /* and sweet one last chance */
92 /* > 1 min has elapsed, give up */
93};
94
Tejun Heoad9e2762006-05-15 20:58:12 +090095static void __ata_port_freeze(struct ata_port *ap);
Tejun Heo6ffa01d2007-03-02 17:32:47 +090096#ifdef CONFIG_PM
Tejun Heo500530f2006-07-03 16:07:27 +090097static void ata_eh_handle_port_suspend(struct ata_port *ap);
98static void ata_eh_handle_port_resume(struct ata_port *ap);
Tejun Heo6ffa01d2007-03-02 17:32:47 +090099#else /* CONFIG_PM */
100static void ata_eh_handle_port_suspend(struct ata_port *ap)
101{ }
102
103static void ata_eh_handle_port_resume(struct ata_port *ap)
104{ }
Tejun Heo6ffa01d2007-03-02 17:32:47 +0900105#endif /* CONFIG_PM */
Tejun Heoad9e2762006-05-15 20:58:12 +0900106
Tejun Heob64bbc32007-07-16 14:29:39 +0900107static void __ata_ehi_pushv_desc(struct ata_eh_info *ehi, const char *fmt,
108 va_list args)
109{
110 ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
111 ATA_EH_DESC_LEN - ehi->desc_len,
112 fmt, args);
113}
114
115/**
116 * __ata_ehi_push_desc - push error description without adding separator
117 * @ehi: target EHI
118 * @fmt: printf format string
119 *
120 * Format string according to @fmt and append it to @ehi->desc.
121 *
122 * LOCKING:
123 * spin_lock_irqsave(host lock)
124 */
125void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
126{
127 va_list args;
128
129 va_start(args, fmt);
130 __ata_ehi_pushv_desc(ehi, fmt, args);
131 va_end(args);
132}
133
134/**
135 * ata_ehi_push_desc - push error description with separator
136 * @ehi: target EHI
137 * @fmt: printf format string
138 *
139 * Format string according to @fmt and append it to @ehi->desc.
140 * If @ehi->desc is not empty, ", " is added in-between.
141 *
142 * LOCKING:
143 * spin_lock_irqsave(host lock)
144 */
145void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
146{
147 va_list args;
148
149 if (ehi->desc_len)
150 __ata_ehi_push_desc(ehi, ", ");
151
152 va_start(args, fmt);
153 __ata_ehi_pushv_desc(ehi, fmt, args);
154 va_end(args);
155}
156
157/**
158 * ata_ehi_clear_desc - clean error description
159 * @ehi: target EHI
160 *
161 * Clear @ehi->desc.
162 *
163 * LOCKING:
164 * spin_lock_irqsave(host lock)
165 */
166void ata_ehi_clear_desc(struct ata_eh_info *ehi)
167{
168 ehi->desc[0] = '\0';
169 ehi->desc_len = 0;
170}
171
Tejun Heocbcdd872007-08-18 13:14:55 +0900172/**
173 * ata_port_desc - append port description
174 * @ap: target ATA port
175 * @fmt: printf format string
176 *
177 * Format string according to @fmt and append it to port
178 * description. If port description is not empty, " " is added
179 * in-between. This function is to be used while initializing
180 * ata_host. The description is printed on host registration.
181 *
182 * LOCKING:
183 * None.
184 */
185void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
186{
187 va_list args;
188
189 WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
190
191 if (ap->link.eh_info.desc_len)
192 __ata_ehi_push_desc(&ap->link.eh_info, " ");
193
194 va_start(args, fmt);
195 __ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
196 va_end(args);
197}
198
199#ifdef CONFIG_PCI
200
201/**
202 * ata_port_pbar_desc - append PCI BAR description
203 * @ap: target ATA port
204 * @bar: target PCI BAR
205 * @offset: offset into PCI BAR
206 * @name: name of the area
207 *
208 * If @offset is negative, this function formats a string which
209 * contains the name, address, size and type of the BAR and
210 * appends it to the port description. If @offset is zero or
211 * positive, only name and offsetted address is appended.
212 *
213 * LOCKING:
214 * None.
215 */
216void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
217 const char *name)
218{
219 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
220 char *type = "";
221 unsigned long long start, len;
222
223 if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
224 type = "m";
225 else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
226 type = "i";
227
228 start = (unsigned long long)pci_resource_start(pdev, bar);
229 len = (unsigned long long)pci_resource_len(pdev, bar);
230
231 if (offset < 0)
232 ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
233 else
Andrew Mortone6a73ab2007-12-13 16:01:37 -0800234 ata_port_desc(ap, "%s 0x%llx", name,
235 start + (unsigned long long)offset);
Tejun Heocbcdd872007-08-18 13:14:55 +0900236}
237
238#endif /* CONFIG_PCI */
239
Tejun Heo3884f7b2007-11-27 19:28:56 +0900240static void ata_ering_record(struct ata_ering *ering, unsigned int eflags,
Tejun Heo0c247c52006-05-15 20:58:19 +0900241 unsigned int err_mask)
242{
243 struct ata_ering_entry *ent;
244
245 WARN_ON(!err_mask);
246
247 ering->cursor++;
248 ering->cursor %= ATA_ERING_SIZE;
249
250 ent = &ering->ring[ering->cursor];
Tejun Heo3884f7b2007-11-27 19:28:56 +0900251 ent->eflags = eflags;
Tejun Heo0c247c52006-05-15 20:58:19 +0900252 ent->err_mask = err_mask;
253 ent->timestamp = get_jiffies_64();
254}
255
Tejun Heo76326ac2007-11-27 19:28:59 +0900256static struct ata_ering_entry *ata_ering_top(struct ata_ering *ering)
257{
258 struct ata_ering_entry *ent = &ering->ring[ering->cursor];
259
260 if (ent->err_mask)
261 return ent;
262 return NULL;
263}
264
Tejun Heo7d47e8d2007-02-02 16:22:31 +0900265static void ata_ering_clear(struct ata_ering *ering)
Tejun Heo0c247c52006-05-15 20:58:19 +0900266{
Tejun Heo7d47e8d2007-02-02 16:22:31 +0900267 memset(ering, 0, sizeof(*ering));
Tejun Heo0c247c52006-05-15 20:58:19 +0900268}
269
270static int ata_ering_map(struct ata_ering *ering,
271 int (*map_fn)(struct ata_ering_entry *, void *),
272 void *arg)
273{
274 int idx, rc = 0;
275 struct ata_ering_entry *ent;
276
277 idx = ering->cursor;
278 do {
279 ent = &ering->ring[idx];
280 if (!ent->err_mask)
281 break;
282 rc = map_fn(ent, arg);
283 if (rc)
284 break;
285 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
286 } while (idx != ering->cursor);
287
288 return rc;
289}
290
Tejun Heo64f65ca2006-06-24 20:30:18 +0900291static unsigned int ata_eh_dev_action(struct ata_device *dev)
292{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900293 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heo64f65ca2006-06-24 20:30:18 +0900294
295 return ehc->i.action | ehc->i.dev_action[dev->devno];
296}
297
Tejun Heof58229f2007-08-06 18:36:23 +0900298static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
Tejun Heoaf181c22006-06-24 20:30:18 +0900299 struct ata_eh_info *ehi, unsigned int action)
300{
Tejun Heof58229f2007-08-06 18:36:23 +0900301 struct ata_device *tdev;
Tejun Heoaf181c22006-06-24 20:30:18 +0900302
303 if (!dev) {
304 ehi->action &= ~action;
Tejun Heof58229f2007-08-06 18:36:23 +0900305 ata_link_for_each_dev(tdev, link)
306 ehi->dev_action[tdev->devno] &= ~action;
Tejun Heoaf181c22006-06-24 20:30:18 +0900307 } else {
308 /* doesn't make sense for port-wide EH actions */
309 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
310
311 /* break ehi->action into ehi->dev_action */
312 if (ehi->action & action) {
Tejun Heof58229f2007-08-06 18:36:23 +0900313 ata_link_for_each_dev(tdev, link)
314 ehi->dev_action[tdev->devno] |=
315 ehi->action & action;
Tejun Heoaf181c22006-06-24 20:30:18 +0900316 ehi->action &= ~action;
317 }
318
319 /* turn off the specified per-dev action */
320 ehi->dev_action[dev->devno] &= ~action;
321 }
322}
323
Tejun Heoece1d632006-04-02 18:51:53 +0900324/**
325 * ata_scsi_timed_out - SCSI layer time out callback
326 * @cmd: timed out SCSI command
327 *
328 * Handles SCSI layer timeout. We race with normal completion of
329 * the qc for @cmd. If the qc is already gone, we lose and let
330 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
331 * timed out and EH should be invoked. Prevent ata_qc_complete()
332 * from finishing it by setting EH_SCHEDULED and return
333 * EH_NOT_HANDLED.
334 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900335 * TODO: kill this function once old EH is gone.
336 *
Tejun Heoece1d632006-04-02 18:51:53 +0900337 * LOCKING:
338 * Called from timer context
339 *
340 * RETURNS:
341 * EH_HANDLED or EH_NOT_HANDLED
342 */
343enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
344{
345 struct Scsi_Host *host = cmd->device->host;
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400346 struct ata_port *ap = ata_shost_to_port(host);
Tejun Heoece1d632006-04-02 18:51:53 +0900347 unsigned long flags;
348 struct ata_queued_cmd *qc;
Tejun Heoad9e2762006-05-15 20:58:12 +0900349 enum scsi_eh_timer_return ret;
Tejun Heoece1d632006-04-02 18:51:53 +0900350
351 DPRINTK("ENTER\n");
352
Tejun Heoad9e2762006-05-15 20:58:12 +0900353 if (ap->ops->error_handler) {
354 ret = EH_NOT_HANDLED;
355 goto out;
356 }
357
358 ret = EH_HANDLED;
Jeff Garzikba6a1302006-06-22 23:46:10 -0400359 spin_lock_irqsave(ap->lock, flags);
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900360 qc = ata_qc_from_tag(ap, ap->link.active_tag);
Tejun Heoece1d632006-04-02 18:51:53 +0900361 if (qc) {
362 WARN_ON(qc->scsicmd != cmd);
363 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
364 qc->err_mask |= AC_ERR_TIMEOUT;
365 ret = EH_NOT_HANDLED;
366 }
Jeff Garzikba6a1302006-06-22 23:46:10 -0400367 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900368
Tejun Heoad9e2762006-05-15 20:58:12 +0900369 out:
Tejun Heoece1d632006-04-02 18:51:53 +0900370 DPRINTK("EXIT, ret=%d\n", ret);
371 return ret;
372}
373
374/**
375 * ata_scsi_error - SCSI layer error handler callback
376 * @host: SCSI host on which error occurred
377 *
378 * Handles SCSI-layer-thrown error events.
379 *
380 * LOCKING:
381 * Inherited from SCSI layer (none, can sleep)
382 *
383 * RETURNS:
384 * Zero.
385 */
Jeff Garzik381544b2006-04-11 13:04:39 -0400386void ata_scsi_error(struct Scsi_Host *host)
Tejun Heoece1d632006-04-02 18:51:53 +0900387{
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400388 struct ata_port *ap = ata_shost_to_port(host);
Tejun Heoa1e10f72007-08-18 13:28:49 +0900389 int i;
Tejun Heoad9e2762006-05-15 20:58:12 +0900390 unsigned long flags;
Tejun Heoece1d632006-04-02 18:51:53 +0900391
392 DPRINTK("ENTER\n");
393
Tejun Heoad9e2762006-05-15 20:58:12 +0900394 /* synchronize with port task */
Tejun Heoece1d632006-04-02 18:51:53 +0900395 ata_port_flush_task(ap);
396
Jeff Garzikcca39742006-08-24 03:19:22 -0400397 /* synchronize with host lock and sort out timeouts */
Tejun Heoece1d632006-04-02 18:51:53 +0900398
Tejun Heoad9e2762006-05-15 20:58:12 +0900399 /* For new EH, all qcs are finished in one of three ways -
400 * normal completion, error completion, and SCSI timeout.
401 * Both cmpletions can race against SCSI timeout. When normal
402 * completion wins, the qc never reaches EH. When error
403 * completion wins, the qc has ATA_QCFLAG_FAILED set.
404 *
405 * When SCSI timeout wins, things are a bit more complex.
406 * Normal or error completion can occur after the timeout but
407 * before this point. In such cases, both types of
408 * completions are honored. A scmd is determined to have
409 * timed out iff its associated qc is active and not failed.
410 */
411 if (ap->ops->error_handler) {
412 struct scsi_cmnd *scmd, *tmp;
413 int nr_timedout = 0;
Tejun Heoece1d632006-04-02 18:51:53 +0900414
Tejun Heoe30349d2006-07-03 03:02:15 +0900415 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900416
417 list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
418 struct ata_queued_cmd *qc;
419
420 for (i = 0; i < ATA_MAX_QUEUE; i++) {
421 qc = __ata_qc_from_tag(ap, i);
422 if (qc->flags & ATA_QCFLAG_ACTIVE &&
423 qc->scsicmd == scmd)
424 break;
425 }
426
427 if (i < ATA_MAX_QUEUE) {
428 /* the scmd has an associated qc */
429 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
430 /* which hasn't failed yet, timeout */
431 qc->err_mask |= AC_ERR_TIMEOUT;
432 qc->flags |= ATA_QCFLAG_FAILED;
433 nr_timedout++;
434 }
435 } else {
436 /* Normal completion occurred after
437 * SCSI timeout but before this point.
438 * Successfully complete it.
439 */
440 scmd->retries = scmd->allowed;
441 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
442 }
443 }
444
445 /* If we have timed out qcs. They belong to EH from
446 * this point but the state of the controller is
447 * unknown. Freeze the port to make sure the IRQ
448 * handler doesn't diddle with those qcs. This must
449 * be done atomically w.r.t. setting QCFLAG_FAILED.
450 */
451 if (nr_timedout)
452 __ata_port_freeze(ap);
453
Tejun Heoe30349d2006-07-03 03:02:15 +0900454 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa1e10f72007-08-18 13:28:49 +0900455
456 /* initialize eh_tries */
457 ap->eh_tries = ATA_EH_MAX_TRIES;
Tejun Heoad9e2762006-05-15 20:58:12 +0900458 } else
Tejun Heoe30349d2006-07-03 03:02:15 +0900459 spin_unlock_wait(ap->lock);
Tejun Heoad9e2762006-05-15 20:58:12 +0900460
461 repeat:
462 /* invoke error handler */
463 if (ap->ops->error_handler) {
Tejun Heocf1b86c2007-08-06 18:36:23 +0900464 struct ata_link *link;
465
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900466 /* kill fast drain timer */
467 del_timer_sync(&ap->fastdrain_timer);
468
Tejun Heo500530f2006-07-03 16:07:27 +0900469 /* process port resume request */
470 ata_eh_handle_port_resume(ap);
471
Tejun Heof3e81b12006-05-15 20:58:21 +0900472 /* fetch & clear EH info */
Tejun Heoe30349d2006-07-03 03:02:15 +0900473 spin_lock_irqsave(ap->lock, flags);
Tejun Heof3e81b12006-05-15 20:58:21 +0900474
Tejun Heocf1b86c2007-08-06 18:36:23 +0900475 __ata_port_for_each_link(link, ap) {
Tejun Heo00115e02007-11-27 19:28:58 +0900476 struct ata_eh_context *ehc = &link->eh_context;
477 struct ata_device *dev;
478
Tejun Heocf1b86c2007-08-06 18:36:23 +0900479 memset(&link->eh_context, 0, sizeof(link->eh_context));
480 link->eh_context.i = link->eh_info;
481 memset(&link->eh_info, 0, sizeof(link->eh_info));
Tejun Heo00115e02007-11-27 19:28:58 +0900482
483 ata_link_for_each_dev(dev, link) {
484 int devno = dev->devno;
485
486 ehc->saved_xfer_mode[devno] = dev->xfer_mode;
487 if (ata_ncq_enabled(dev))
488 ehc->saved_ncq_enabled |= 1 << devno;
489 }
Tejun Heocf1b86c2007-08-06 18:36:23 +0900490 }
Tejun Heof3e81b12006-05-15 20:58:21 +0900491
Tejun Heob51e9e52006-06-29 01:29:30 +0900492 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
493 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
Tejun Heoda917d62007-09-23 13:14:12 +0900494 ap->excl_link = NULL; /* don't maintain exclusion over EH */
Tejun Heof3e81b12006-05-15 20:58:21 +0900495
Tejun Heoe30349d2006-07-03 03:02:15 +0900496 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900497
Tejun Heo500530f2006-07-03 16:07:27 +0900498 /* invoke EH, skip if unloading or suspended */
499 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
Tejun Heo720ba122006-05-31 18:28:13 +0900500 ap->ops->error_handler(ap);
501 else
502 ata_eh_finish(ap);
Tejun Heoad9e2762006-05-15 20:58:12 +0900503
Tejun Heo500530f2006-07-03 16:07:27 +0900504 /* process port suspend request */
505 ata_eh_handle_port_suspend(ap);
506
Tejun Heoad9e2762006-05-15 20:58:12 +0900507 /* Exception might have happend after ->error_handler
508 * recovered the port but before this point. Repeat
509 * EH in such case.
510 */
Tejun Heoe30349d2006-07-03 03:02:15 +0900511 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900512
Tejun Heob51e9e52006-06-29 01:29:30 +0900513 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
Tejun Heoa1e10f72007-08-18 13:28:49 +0900514 if (--ap->eh_tries) {
Tejun Heoe30349d2006-07-03 03:02:15 +0900515 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900516 goto repeat;
517 }
518 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
Tejun Heoa1e10f72007-08-18 13:28:49 +0900519 "tries, giving up\n", ATA_EH_MAX_TRIES);
Tejun Heo914616a2007-06-25 21:47:11 +0900520 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
Tejun Heoad9e2762006-05-15 20:58:12 +0900521 }
522
Tejun Heof3e81b12006-05-15 20:58:21 +0900523 /* this run is complete, make sure EH info is clear */
Tejun Heocf1b86c2007-08-06 18:36:23 +0900524 __ata_port_for_each_link(link, ap)
525 memset(&link->eh_info, 0, sizeof(link->eh_info));
Tejun Heof3e81b12006-05-15 20:58:21 +0900526
Tejun Heoe30349d2006-07-03 03:02:15 +0900527 /* Clear host_eh_scheduled while holding ap->lock such
Tejun Heoad9e2762006-05-15 20:58:12 +0900528 * that if exception occurs after this point but
529 * before EH completion, SCSI midlayer will
530 * re-initiate EH.
531 */
532 host->host_eh_scheduled = 0;
533
Tejun Heoe30349d2006-07-03 03:02:15 +0900534 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900535 } else {
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900536 WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
Tejun Heoad9e2762006-05-15 20:58:12 +0900537 ap->ops->eng_timeout(ap);
538 }
539
540 /* finish or retry handled scmd's and clean up */
Tejun Heoece1d632006-04-02 18:51:53 +0900541 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
542
543 scsi_eh_flush_done_q(&ap->eh_done_q);
544
Tejun Heoad9e2762006-05-15 20:58:12 +0900545 /* clean up */
Tejun Heoe30349d2006-07-03 03:02:15 +0900546 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900547
Tejun Heo1cdaf532006-07-03 16:07:26 +0900548 if (ap->pflags & ATA_PFLAG_LOADING)
Tejun Heob51e9e52006-06-29 01:29:30 +0900549 ap->pflags &= ~ATA_PFLAG_LOADING;
Tejun Heo1cdaf532006-07-03 16:07:26 +0900550 else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
David Howells52bad642006-11-22 14:54:01 +0000551 queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0);
Tejun Heo1cdaf532006-07-03 16:07:26 +0900552
553 if (ap->pflags & ATA_PFLAG_RECOVERED)
554 ata_port_printk(ap, KERN_INFO, "EH complete\n");
Tejun Heo580b2102006-05-31 18:28:05 +0900555
Tejun Heob51e9e52006-06-29 01:29:30 +0900556 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
Tejun Heoad9e2762006-05-15 20:58:12 +0900557
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900558 /* tell wait_eh that we're done */
Tejun Heob51e9e52006-06-29 01:29:30 +0900559 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900560 wake_up_all(&ap->eh_wait_q);
561
Tejun Heoe30349d2006-07-03 03:02:15 +0900562 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900563
Tejun Heoece1d632006-04-02 18:51:53 +0900564 DPRINTK("EXIT\n");
Tejun Heoece1d632006-04-02 18:51:53 +0900565}
566
567/**
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900568 * ata_port_wait_eh - Wait for the currently pending EH to complete
569 * @ap: Port to wait EH for
570 *
571 * Wait until the currently pending EH is complete.
572 *
573 * LOCKING:
574 * Kernel thread context (may sleep).
575 */
576void ata_port_wait_eh(struct ata_port *ap)
577{
578 unsigned long flags;
579 DEFINE_WAIT(wait);
580
581 retry:
Jeff Garzikba6a1302006-06-22 23:46:10 -0400582 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900583
Tejun Heob51e9e52006-06-29 01:29:30 +0900584 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900585 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
Jeff Garzikba6a1302006-06-22 23:46:10 -0400586 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900587 schedule();
Jeff Garzikba6a1302006-06-22 23:46:10 -0400588 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900589 }
Tejun Heo0a1b6222006-06-11 11:01:38 +0900590 finish_wait(&ap->eh_wait_q, &wait);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900591
Jeff Garzikba6a1302006-06-22 23:46:10 -0400592 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900593
594 /* make sure SCSI EH is complete */
Jeff Garzikcca39742006-08-24 03:19:22 -0400595 if (scsi_host_in_recovery(ap->scsi_host)) {
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900596 msleep(10);
597 goto retry;
598 }
599}
600
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900601static int ata_eh_nr_in_flight(struct ata_port *ap)
602{
603 unsigned int tag;
604 int nr = 0;
605
606 /* count only non-internal commands */
607 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++)
608 if (ata_qc_from_tag(ap, tag))
609 nr++;
610
611 return nr;
612}
613
614void ata_eh_fastdrain_timerfn(unsigned long arg)
615{
616 struct ata_port *ap = (void *)arg;
617 unsigned long flags;
618 int cnt;
619
620 spin_lock_irqsave(ap->lock, flags);
621
622 cnt = ata_eh_nr_in_flight(ap);
623
624 /* are we done? */
625 if (!cnt)
626 goto out_unlock;
627
628 if (cnt == ap->fastdrain_cnt) {
629 unsigned int tag;
630
631 /* No progress during the last interval, tag all
632 * in-flight qcs as timed out and freeze the port.
633 */
634 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) {
635 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
636 if (qc)
637 qc->err_mask |= AC_ERR_TIMEOUT;
638 }
639
640 ata_port_freeze(ap);
641 } else {
642 /* some qcs have finished, give it another chance */
643 ap->fastdrain_cnt = cnt;
644 ap->fastdrain_timer.expires =
645 jiffies + ATA_EH_FASTDRAIN_INTERVAL;
646 add_timer(&ap->fastdrain_timer);
647 }
648
649 out_unlock:
650 spin_unlock_irqrestore(ap->lock, flags);
651}
652
653/**
654 * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
655 * @ap: target ATA port
656 * @fastdrain: activate fast drain
657 *
658 * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
659 * is non-zero and EH wasn't pending before. Fast drain ensures
660 * that EH kicks in in timely manner.
661 *
662 * LOCKING:
663 * spin_lock_irqsave(host lock)
664 */
665static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
666{
667 int cnt;
668
669 /* already scheduled? */
670 if (ap->pflags & ATA_PFLAG_EH_PENDING)
671 return;
672
673 ap->pflags |= ATA_PFLAG_EH_PENDING;
674
675 if (!fastdrain)
676 return;
677
678 /* do we have in-flight qcs? */
679 cnt = ata_eh_nr_in_flight(ap);
680 if (!cnt)
681 return;
682
683 /* activate fast drain */
684 ap->fastdrain_cnt = cnt;
685 ap->fastdrain_timer.expires = jiffies + ATA_EH_FASTDRAIN_INTERVAL;
686 add_timer(&ap->fastdrain_timer);
687}
688
Tejun Heof686bcb2006-05-15 20:58:05 +0900689/**
690 * ata_qc_schedule_eh - schedule qc for error handling
691 * @qc: command to schedule error handling for
692 *
693 * Schedule error handling for @qc. EH will kick in as soon as
694 * other commands are drained.
695 *
696 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400697 * spin_lock_irqsave(host lock)
Tejun Heof686bcb2006-05-15 20:58:05 +0900698 */
699void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
700{
701 struct ata_port *ap = qc->ap;
702
703 WARN_ON(!ap->ops->error_handler);
704
705 qc->flags |= ATA_QCFLAG_FAILED;
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900706 ata_eh_set_pending(ap, 1);
Tejun Heof686bcb2006-05-15 20:58:05 +0900707
708 /* The following will fail if timeout has already expired.
709 * ata_scsi_error() takes care of such scmds on EH entry.
710 * Note that ATA_QCFLAG_FAILED is unconditionally set after
711 * this function completes.
712 */
713 scsi_req_abort_cmd(qc->scsicmd);
714}
715
Tejun Heo7b70fc02006-05-15 20:58:07 +0900716/**
717 * ata_port_schedule_eh - schedule error handling without a qc
718 * @ap: ATA port to schedule EH for
719 *
720 * Schedule error handling for @ap. EH will kick in as soon as
721 * all commands are drained.
722 *
723 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400724 * spin_lock_irqsave(host lock)
Tejun Heo7b70fc02006-05-15 20:58:07 +0900725 */
726void ata_port_schedule_eh(struct ata_port *ap)
727{
728 WARN_ON(!ap->ops->error_handler);
729
Tejun Heof4d6d002007-05-01 11:50:15 +0200730 if (ap->pflags & ATA_PFLAG_INITIALIZING)
731 return;
732
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900733 ata_eh_set_pending(ap, 1);
Jeff Garzikcca39742006-08-24 03:19:22 -0400734 scsi_schedule_eh(ap->scsi_host);
Tejun Heo7b70fc02006-05-15 20:58:07 +0900735
736 DPRINTK("port EH scheduled\n");
737}
738
Tejun Heodbd82612007-08-06 18:36:23 +0900739static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
Tejun Heo7b70fc02006-05-15 20:58:07 +0900740{
741 int tag, nr_aborted = 0;
742
743 WARN_ON(!ap->ops->error_handler);
744
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900745 /* we're gonna abort all commands, no need for fast drain */
746 ata_eh_set_pending(ap, 0);
747
Tejun Heo7b70fc02006-05-15 20:58:07 +0900748 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
749 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
750
Tejun Heodbd82612007-08-06 18:36:23 +0900751 if (qc && (!link || qc->dev->link == link)) {
Tejun Heo7b70fc02006-05-15 20:58:07 +0900752 qc->flags |= ATA_QCFLAG_FAILED;
753 ata_qc_complete(qc);
754 nr_aborted++;
755 }
756 }
757
758 if (!nr_aborted)
759 ata_port_schedule_eh(ap);
760
761 return nr_aborted;
762}
763
Tejun Heoe3180492006-05-15 20:58:09 +0900764/**
Tejun Heodbd82612007-08-06 18:36:23 +0900765 * ata_link_abort - abort all qc's on the link
766 * @link: ATA link to abort qc's for
767 *
768 * Abort all active qc's active on @link and schedule EH.
769 *
770 * LOCKING:
771 * spin_lock_irqsave(host lock)
772 *
773 * RETURNS:
774 * Number of aborted qc's.
775 */
776int ata_link_abort(struct ata_link *link)
777{
778 return ata_do_link_abort(link->ap, link);
779}
780
781/**
782 * ata_port_abort - abort all qc's on the port
783 * @ap: ATA port to abort qc's for
784 *
785 * Abort all active qc's of @ap and schedule EH.
786 *
787 * LOCKING:
788 * spin_lock_irqsave(host_set lock)
789 *
790 * RETURNS:
791 * Number of aborted qc's.
792 */
793int ata_port_abort(struct ata_port *ap)
794{
795 return ata_do_link_abort(ap, NULL);
796}
797
798/**
Tejun Heoe3180492006-05-15 20:58:09 +0900799 * __ata_port_freeze - freeze port
800 * @ap: ATA port to freeze
801 *
802 * This function is called when HSM violation or some other
803 * condition disrupts normal operation of the port. Frozen port
804 * is not allowed to perform any operation until the port is
805 * thawed, which usually follows a successful reset.
806 *
807 * ap->ops->freeze() callback can be used for freezing the port
808 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
809 * port cannot be frozen hardware-wise, the interrupt handler
810 * must ack and clear interrupts unconditionally while the port
811 * is frozen.
812 *
813 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400814 * spin_lock_irqsave(host lock)
Tejun Heoe3180492006-05-15 20:58:09 +0900815 */
816static void __ata_port_freeze(struct ata_port *ap)
817{
818 WARN_ON(!ap->ops->error_handler);
819
820 if (ap->ops->freeze)
821 ap->ops->freeze(ap);
822
Tejun Heob51e9e52006-06-29 01:29:30 +0900823 ap->pflags |= ATA_PFLAG_FROZEN;
Tejun Heoe3180492006-05-15 20:58:09 +0900824
Tejun Heo44877b42007-02-21 01:06:51 +0900825 DPRINTK("ata%u port frozen\n", ap->print_id);
Tejun Heoe3180492006-05-15 20:58:09 +0900826}
827
828/**
829 * ata_port_freeze - abort & freeze port
830 * @ap: ATA port to freeze
831 *
832 * Abort and freeze @ap.
833 *
834 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400835 * spin_lock_irqsave(host lock)
Tejun Heoe3180492006-05-15 20:58:09 +0900836 *
837 * RETURNS:
838 * Number of aborted commands.
839 */
840int ata_port_freeze(struct ata_port *ap)
841{
842 int nr_aborted;
843
844 WARN_ON(!ap->ops->error_handler);
845
846 nr_aborted = ata_port_abort(ap);
847 __ata_port_freeze(ap);
848
849 return nr_aborted;
850}
851
852/**
Tejun Heo7d77b242007-09-23 13:14:13 +0900853 * sata_async_notification - SATA async notification handler
854 * @ap: ATA port where async notification is received
855 *
856 * Handler to be called when async notification via SDB FIS is
857 * received. This function schedules EH if necessary.
858 *
859 * LOCKING:
860 * spin_lock_irqsave(host lock)
861 *
862 * RETURNS:
863 * 1 if EH is scheduled, 0 otherwise.
864 */
865int sata_async_notification(struct ata_port *ap)
866{
867 u32 sntf;
868 int rc;
869
870 if (!(ap->flags & ATA_FLAG_AN))
871 return 0;
872
873 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
874 if (rc == 0)
875 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
876
877 if (!ap->nr_pmp_links || rc) {
878 /* PMP is not attached or SNTF is not available */
879 if (!ap->nr_pmp_links) {
880 /* PMP is not attached. Check whether ATAPI
881 * AN is configured. If so, notify media
882 * change.
883 */
884 struct ata_device *dev = ap->link.device;
885
886 if ((dev->class == ATA_DEV_ATAPI) &&
887 (dev->flags & ATA_DFLAG_AN))
888 ata_scsi_media_change_notify(dev);
889 return 0;
890 } else {
891 /* PMP is attached but SNTF is not available.
892 * ATAPI async media change notification is
893 * not used. The PMP must be reporting PHY
894 * status change, schedule EH.
895 */
896 ata_port_schedule_eh(ap);
897 return 1;
898 }
899 } else {
900 /* PMP is attached and SNTF is available */
901 struct ata_link *link;
902
903 /* check and notify ATAPI AN */
904 ata_port_for_each_link(link, ap) {
905 if (!(sntf & (1 << link->pmp)))
906 continue;
907
908 if ((link->device->class == ATA_DEV_ATAPI) &&
909 (link->device->flags & ATA_DFLAG_AN))
910 ata_scsi_media_change_notify(link->device);
911 }
912
913 /* If PMP is reporting that PHY status of some
914 * downstream ports has changed, schedule EH.
915 */
916 if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
917 ata_port_schedule_eh(ap);
918 return 1;
919 }
920
921 return 0;
922 }
923}
924
925/**
Tejun Heoe3180492006-05-15 20:58:09 +0900926 * ata_eh_freeze_port - EH helper to freeze port
927 * @ap: ATA port to freeze
928 *
929 * Freeze @ap.
930 *
931 * LOCKING:
932 * None.
933 */
934void ata_eh_freeze_port(struct ata_port *ap)
935{
936 unsigned long flags;
937
938 if (!ap->ops->error_handler)
939 return;
940
Jeff Garzikba6a1302006-06-22 23:46:10 -0400941 spin_lock_irqsave(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900942 __ata_port_freeze(ap);
Jeff Garzikba6a1302006-06-22 23:46:10 -0400943 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900944}
945
946/**
947 * ata_port_thaw_port - EH helper to thaw port
948 * @ap: ATA port to thaw
949 *
950 * Thaw frozen port @ap.
951 *
952 * LOCKING:
953 * None.
954 */
955void ata_eh_thaw_port(struct ata_port *ap)
956{
957 unsigned long flags;
958
959 if (!ap->ops->error_handler)
960 return;
961
Jeff Garzikba6a1302006-06-22 23:46:10 -0400962 spin_lock_irqsave(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900963
Tejun Heob51e9e52006-06-29 01:29:30 +0900964 ap->pflags &= ~ATA_PFLAG_FROZEN;
Tejun Heoe3180492006-05-15 20:58:09 +0900965
966 if (ap->ops->thaw)
967 ap->ops->thaw(ap);
968
Jeff Garzikba6a1302006-06-22 23:46:10 -0400969 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900970
Tejun Heo44877b42007-02-21 01:06:51 +0900971 DPRINTK("ata%u port thawed\n", ap->print_id);
Tejun Heoe3180492006-05-15 20:58:09 +0900972}
973
Tejun Heoece1d632006-04-02 18:51:53 +0900974static void ata_eh_scsidone(struct scsi_cmnd *scmd)
975{
976 /* nada */
977}
978
979static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
980{
981 struct ata_port *ap = qc->ap;
982 struct scsi_cmnd *scmd = qc->scsicmd;
983 unsigned long flags;
984
Jeff Garzikba6a1302006-06-22 23:46:10 -0400985 spin_lock_irqsave(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900986 qc->scsidone = ata_eh_scsidone;
987 __ata_qc_complete(qc);
988 WARN_ON(ata_tag_valid(qc->tag));
Jeff Garzikba6a1302006-06-22 23:46:10 -0400989 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900990
991 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
992}
993
994/**
995 * ata_eh_qc_complete - Complete an active ATA command from EH
996 * @qc: Command to complete
997 *
998 * Indicate to the mid and upper layers that an ATA command has
999 * completed. To be used from EH.
1000 */
1001void ata_eh_qc_complete(struct ata_queued_cmd *qc)
1002{
1003 struct scsi_cmnd *scmd = qc->scsicmd;
1004 scmd->retries = scmd->allowed;
1005 __ata_eh_qc_complete(qc);
1006}
1007
1008/**
1009 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
1010 * @qc: Command to retry
1011 *
1012 * Indicate to the mid and upper layers that an ATA command
1013 * should be retried. To be used from EH.
1014 *
1015 * SCSI midlayer limits the number of retries to scmd->allowed.
1016 * scmd->retries is decremented for commands which get retried
1017 * due to unrelated failures (qc->err_mask is zero).
1018 */
1019void ata_eh_qc_retry(struct ata_queued_cmd *qc)
1020{
1021 struct scsi_cmnd *scmd = qc->scsicmd;
1022 if (!qc->err_mask && scmd->retries)
1023 scmd->retries--;
1024 __ata_eh_qc_complete(qc);
1025}
Tejun Heo022bdb02006-05-15 20:58:22 +09001026
1027/**
Tejun Heo0ea035a2006-05-31 18:28:01 +09001028 * ata_eh_detach_dev - detach ATA device
1029 * @dev: ATA device to detach
1030 *
1031 * Detach @dev.
1032 *
1033 * LOCKING:
1034 * None.
1035 */
Tejun Heofb7fd612007-09-23 13:14:12 +09001036void ata_eh_detach_dev(struct ata_device *dev)
Tejun Heo0ea035a2006-05-31 18:28:01 +09001037{
Tejun Heof58229f2007-08-06 18:36:23 +09001038 struct ata_link *link = dev->link;
1039 struct ata_port *ap = link->ap;
Tejun Heo0ea035a2006-05-31 18:28:01 +09001040 unsigned long flags;
1041
1042 ata_dev_disable(dev);
1043
Jeff Garzikba6a1302006-06-22 23:46:10 -04001044 spin_lock_irqsave(ap->lock, flags);
Tejun Heo0ea035a2006-05-31 18:28:01 +09001045
1046 dev->flags &= ~ATA_DFLAG_DETACH;
1047
1048 if (ata_scsi_offline_dev(dev)) {
1049 dev->flags |= ATA_DFLAG_DETACHED;
Tejun Heob51e9e52006-06-29 01:29:30 +09001050 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
Tejun Heo0ea035a2006-05-31 18:28:01 +09001051 }
1052
Tejun Heobeb07c12006-06-24 20:30:19 +09001053 /* clear per-dev EH actions */
Tejun Heof58229f2007-08-06 18:36:23 +09001054 ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
1055 ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
Tejun Heobeb07c12006-06-24 20:30:19 +09001056
Jeff Garzikba6a1302006-06-22 23:46:10 -04001057 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo0ea035a2006-05-31 18:28:01 +09001058}
1059
1060/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001061 * ata_eh_about_to_do - about to perform eh_action
Tejun Heo955e57d2007-08-06 18:36:23 +09001062 * @link: target ATA link
Tejun Heo47005f22006-06-19 18:27:23 +09001063 * @dev: target ATA dev for per-dev action (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09001064 * @action: action about to be performed
1065 *
1066 * Called just before performing EH actions to clear related bits
Tejun Heo955e57d2007-08-06 18:36:23 +09001067 * in @link->eh_info such that eh actions are not unnecessarily
1068 * repeated.
Tejun Heo022bdb02006-05-15 20:58:22 +09001069 *
1070 * LOCKING:
1071 * None.
1072 */
Tejun Heofb7fd612007-09-23 13:14:12 +09001073void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
1074 unsigned int action)
Tejun Heo022bdb02006-05-15 20:58:22 +09001075{
Tejun Heo955e57d2007-08-06 18:36:23 +09001076 struct ata_port *ap = link->ap;
1077 struct ata_eh_info *ehi = &link->eh_info;
1078 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001079 unsigned long flags;
1080
Jeff Garzikba6a1302006-06-22 23:46:10 -04001081 spin_lock_irqsave(ap->lock, flags);
Tejun Heo1cdaf532006-07-03 16:07:26 +09001082
Tejun Heo13abf502006-07-10 23:18:46 +09001083 /* Reset is represented by combination of actions and EHI
1084 * flags. Suck in all related bits before clearing eh_info to
1085 * avoid losing requested action.
1086 */
1087 if (action & ATA_EH_RESET_MASK) {
1088 ehc->i.action |= ehi->action & ATA_EH_RESET_MASK;
1089 ehc->i.flags |= ehi->flags & ATA_EHI_RESET_MODIFIER_MASK;
Tejun Heo1cdaf532006-07-03 16:07:26 +09001090
Tejun Heo13abf502006-07-10 23:18:46 +09001091 /* make sure all reset actions are cleared & clear EHI flags */
1092 action |= ATA_EH_RESET_MASK;
1093 ehi->flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
1094 }
1095
Tejun Heo955e57d2007-08-06 18:36:23 +09001096 ata_eh_clear_action(link, dev, ehi, action);
Tejun Heo13abf502006-07-10 23:18:46 +09001097
1098 if (!(ehc->i.flags & ATA_EHI_QUIET))
Tejun Heo1cdaf532006-07-03 16:07:26 +09001099 ap->pflags |= ATA_PFLAG_RECOVERED;
1100
Jeff Garzikba6a1302006-06-22 23:46:10 -04001101 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo022bdb02006-05-15 20:58:22 +09001102}
1103
1104/**
Tejun Heo47005f22006-06-19 18:27:23 +09001105 * ata_eh_done - EH action complete
Tejun Heo955e57d2007-08-06 18:36:23 +09001106* @ap: target ATA port
Tejun Heo47005f22006-06-19 18:27:23 +09001107 * @dev: target ATA dev for per-dev action (can be NULL)
1108 * @action: action just completed
1109 *
1110 * Called right after performing EH actions to clear related bits
Tejun Heo955e57d2007-08-06 18:36:23 +09001111 * in @link->eh_context.
Tejun Heo47005f22006-06-19 18:27:23 +09001112 *
1113 * LOCKING:
1114 * None.
1115 */
Tejun Heofb7fd612007-09-23 13:14:12 +09001116void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1117 unsigned int action)
Tejun Heo47005f22006-06-19 18:27:23 +09001118{
Tejun Heo955e57d2007-08-06 18:36:23 +09001119 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001120
Tejun Heo13abf502006-07-10 23:18:46 +09001121 /* if reset is complete, clear all reset actions & reset modifier */
1122 if (action & ATA_EH_RESET_MASK) {
1123 action |= ATA_EH_RESET_MASK;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001124 ehc->i.flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
Tejun Heo13abf502006-07-10 23:18:46 +09001125 }
1126
Tejun Heo955e57d2007-08-06 18:36:23 +09001127 ata_eh_clear_action(link, dev, &ehc->i, action);
Tejun Heo47005f22006-06-19 18:27:23 +09001128}
1129
1130/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001131 * ata_err_string - convert err_mask to descriptive string
1132 * @err_mask: error mask to convert to string
1133 *
1134 * Convert @err_mask to descriptive string. Errors are
1135 * prioritized according to severity and only the most severe
1136 * error is reported.
1137 *
1138 * LOCKING:
1139 * None.
1140 *
1141 * RETURNS:
1142 * Descriptive string for @err_mask
1143 */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001144static const char *ata_err_string(unsigned int err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001145{
1146 if (err_mask & AC_ERR_HOST_BUS)
1147 return "host bus error";
1148 if (err_mask & AC_ERR_ATA_BUS)
1149 return "ATA bus error";
1150 if (err_mask & AC_ERR_TIMEOUT)
1151 return "timeout";
1152 if (err_mask & AC_ERR_HSM)
1153 return "HSM violation";
1154 if (err_mask & AC_ERR_SYSTEM)
1155 return "internal error";
1156 if (err_mask & AC_ERR_MEDIA)
1157 return "media error";
1158 if (err_mask & AC_ERR_INVALID)
1159 return "invalid argument";
1160 if (err_mask & AC_ERR_DEV)
1161 return "device error";
1162 return "unknown error";
1163}
1164
1165/**
Tejun Heoe8ee8452006-05-15 21:03:46 +09001166 * ata_read_log_page - read a specific log page
1167 * @dev: target device
1168 * @page: page to read
1169 * @buf: buffer to store read page
1170 * @sectors: number of sectors to read
1171 *
1172 * Read log page using READ_LOG_EXT command.
1173 *
1174 * LOCKING:
1175 * Kernel thread context (may sleep).
1176 *
1177 * RETURNS:
1178 * 0 on success, AC_ERR_* mask otherwise.
1179 */
1180static unsigned int ata_read_log_page(struct ata_device *dev,
1181 u8 page, void *buf, unsigned int sectors)
1182{
1183 struct ata_taskfile tf;
1184 unsigned int err_mask;
1185
1186 DPRINTK("read log page - page %d\n", page);
1187
1188 ata_tf_init(dev, &tf);
1189 tf.command = ATA_CMD_READ_LOG_EXT;
1190 tf.lbal = page;
1191 tf.nsect = sectors;
1192 tf.hob_nsect = sectors >> 8;
1193 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1194 tf.protocol = ATA_PROT_PIO;
1195
1196 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
Tejun Heo2b789102007-10-09 15:05:44 +09001197 buf, sectors * ATA_SECT_SIZE, 0);
Tejun Heoe8ee8452006-05-15 21:03:46 +09001198
1199 DPRINTK("EXIT, err_mask=%x\n", err_mask);
1200 return err_mask;
1201}
1202
1203/**
1204 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
1205 * @dev: Device to read log page 10h from
1206 * @tag: Resulting tag of the failed command
1207 * @tf: Resulting taskfile registers of the failed command
1208 *
1209 * Read log page 10h to obtain NCQ error details and clear error
1210 * condition.
1211 *
1212 * LOCKING:
1213 * Kernel thread context (may sleep).
1214 *
1215 * RETURNS:
1216 * 0 on success, -errno otherwise.
1217 */
1218static int ata_eh_read_log_10h(struct ata_device *dev,
1219 int *tag, struct ata_taskfile *tf)
1220{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001221 u8 *buf = dev->link->ap->sector_buf;
Tejun Heoe8ee8452006-05-15 21:03:46 +09001222 unsigned int err_mask;
1223 u8 csum;
1224 int i;
1225
1226 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
1227 if (err_mask)
1228 return -EIO;
1229
1230 csum = 0;
1231 for (i = 0; i < ATA_SECT_SIZE; i++)
1232 csum += buf[i];
1233 if (csum)
1234 ata_dev_printk(dev, KERN_WARNING,
1235 "invalid checksum 0x%x on log page 10h\n", csum);
1236
1237 if (buf[0] & 0x80)
1238 return -ENOENT;
1239
1240 *tag = buf[0] & 0x1f;
1241
1242 tf->command = buf[2];
1243 tf->feature = buf[3];
1244 tf->lbal = buf[4];
1245 tf->lbam = buf[5];
1246 tf->lbah = buf[6];
1247 tf->device = buf[7];
1248 tf->hob_lbal = buf[8];
1249 tf->hob_lbam = buf[9];
1250 tf->hob_lbah = buf[10];
1251 tf->nsect = buf[12];
1252 tf->hob_nsect = buf[13];
1253
1254 return 0;
1255}
1256
1257/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001258 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1259 * @dev: device to perform REQUEST_SENSE to
1260 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
1261 *
1262 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
1263 * SENSE. This function is EH helper.
1264 *
1265 * LOCKING:
1266 * Kernel thread context (may sleep).
1267 *
1268 * RETURNS:
1269 * 0 on success, AC_ERR_* mask on failure
1270 */
Albert Lee56287762007-04-02 11:30:46 +08001271static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc)
Tejun Heo022bdb02006-05-15 20:58:22 +09001272{
Albert Lee56287762007-04-02 11:30:46 +08001273 struct ata_device *dev = qc->dev;
1274 unsigned char *sense_buf = qc->scsicmd->sense_buffer;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001275 struct ata_port *ap = dev->link->ap;
Tejun Heo022bdb02006-05-15 20:58:22 +09001276 struct ata_taskfile tf;
1277 u8 cdb[ATAPI_CDB_LEN];
1278
1279 DPRINTK("ATAPI request sense\n");
1280
Tejun Heo022bdb02006-05-15 20:58:22 +09001281 /* FIXME: is this needed? */
1282 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1283
Albert Lee56287762007-04-02 11:30:46 +08001284 /* initialize sense_buf with the error register,
1285 * for the case where they are -not- overwritten
1286 */
Tejun Heo022bdb02006-05-15 20:58:22 +09001287 sense_buf[0] = 0x70;
Albert Lee56287762007-04-02 11:30:46 +08001288 sense_buf[2] = qc->result_tf.feature >> 4;
1289
Jeff Garzika617c092007-05-21 20:14:23 -04001290 /* some devices time out if garbage left in tf */
Albert Lee56287762007-04-02 11:30:46 +08001291 ata_tf_init(dev, &tf);
Tejun Heo022bdb02006-05-15 20:58:22 +09001292
1293 memset(cdb, 0, ATAPI_CDB_LEN);
1294 cdb[0] = REQUEST_SENSE;
1295 cdb[4] = SCSI_SENSE_BUFFERSIZE;
1296
1297 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1298 tf.command = ATA_CMD_PACKET;
1299
1300 /* is it pointless to prefer PIO for "safety reasons"? */
1301 if (ap->flags & ATA_FLAG_PIO_DMA) {
Tejun Heo0dc36882007-12-18 16:34:43 -05001302 tf.protocol = ATAPI_PROT_DMA;
Tejun Heo022bdb02006-05-15 20:58:22 +09001303 tf.feature |= ATAPI_PKT_DMA;
1304 } else {
Tejun Heo0dc36882007-12-18 16:34:43 -05001305 tf.protocol = ATAPI_PROT_PIO;
Tejun Heof2dfc1a2007-12-12 12:12:46 +09001306 tf.lbam = SCSI_SENSE_BUFFERSIZE;
1307 tf.lbah = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09001308 }
1309
1310 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
Tejun Heo2b789102007-10-09 15:05:44 +09001311 sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
Tejun Heo022bdb02006-05-15 20:58:22 +09001312}
1313
1314/**
1315 * ata_eh_analyze_serror - analyze SError for a failed port
Tejun Heo02607312007-08-06 18:36:23 +09001316 * @link: ATA link to analyze SError for
Tejun Heo022bdb02006-05-15 20:58:22 +09001317 *
1318 * Analyze SError if available and further determine cause of
1319 * failure.
1320 *
1321 * LOCKING:
1322 * None.
1323 */
Tejun Heo02607312007-08-06 18:36:23 +09001324static void ata_eh_analyze_serror(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001325{
Tejun Heo02607312007-08-06 18:36:23 +09001326 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001327 u32 serror = ehc->i.serror;
1328 unsigned int err_mask = 0, action = 0;
Tejun Heof9df58c2007-09-23 13:14:13 +09001329 u32 hotplug_mask;
Tejun Heo022bdb02006-05-15 20:58:22 +09001330
1331 if (serror & SERR_PERSISTENT) {
1332 err_mask |= AC_ERR_ATA_BUS;
1333 action |= ATA_EH_HARDRESET;
1334 }
1335 if (serror &
1336 (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
1337 err_mask |= AC_ERR_ATA_BUS;
1338 action |= ATA_EH_SOFTRESET;
1339 }
1340 if (serror & SERR_PROTOCOL) {
1341 err_mask |= AC_ERR_HSM;
1342 action |= ATA_EH_SOFTRESET;
1343 }
1344 if (serror & SERR_INTERNAL) {
1345 err_mask |= AC_ERR_SYSTEM;
Tejun Heo771b8da2007-03-14 01:20:51 +09001346 action |= ATA_EH_HARDRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001347 }
Tejun Heof9df58c2007-09-23 13:14:13 +09001348
1349 /* Determine whether a hotplug event has occurred. Both
1350 * SError.N/X are considered hotplug events for enabled or
1351 * host links. For disabled PMP links, only N bit is
1352 * considered as X bit is left at 1 for link plugging.
1353 */
1354 hotplug_mask = 0;
1355
1356 if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
1357 hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
1358 else
1359 hotplug_mask = SERR_PHYRDY_CHG;
1360
1361 if (serror & hotplug_mask)
Tejun Heo084fe632006-05-31 18:28:03 +09001362 ata_ehi_hotplugged(&ehc->i);
Tejun Heo022bdb02006-05-15 20:58:22 +09001363
1364 ehc->i.err_mask |= err_mask;
1365 ehc->i.action |= action;
1366}
1367
1368/**
Tejun Heoe8ee8452006-05-15 21:03:46 +09001369 * ata_eh_analyze_ncq_error - analyze NCQ error
Tejun Heo02607312007-08-06 18:36:23 +09001370 * @link: ATA link to analyze NCQ error for
Tejun Heoe8ee8452006-05-15 21:03:46 +09001371 *
1372 * Read log page 10h, determine the offending qc and acquire
1373 * error status TF. For NCQ device errors, all LLDDs have to do
1374 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1375 * care of the rest.
1376 *
1377 * LOCKING:
1378 * Kernel thread context (may sleep).
1379 */
Tejun Heo02607312007-08-06 18:36:23 +09001380static void ata_eh_analyze_ncq_error(struct ata_link *link)
Tejun Heoe8ee8452006-05-15 21:03:46 +09001381{
Tejun Heo02607312007-08-06 18:36:23 +09001382 struct ata_port *ap = link->ap;
1383 struct ata_eh_context *ehc = &link->eh_context;
1384 struct ata_device *dev = link->device;
Tejun Heoe8ee8452006-05-15 21:03:46 +09001385 struct ata_queued_cmd *qc;
1386 struct ata_taskfile tf;
1387 int tag, rc;
1388
1389 /* if frozen, we can't do much */
Tejun Heob51e9e52006-06-29 01:29:30 +09001390 if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heoe8ee8452006-05-15 21:03:46 +09001391 return;
1392
1393 /* is it NCQ device error? */
Tejun Heo02607312007-08-06 18:36:23 +09001394 if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
Tejun Heoe8ee8452006-05-15 21:03:46 +09001395 return;
1396
1397 /* has LLDD analyzed already? */
1398 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1399 qc = __ata_qc_from_tag(ap, tag);
1400
1401 if (!(qc->flags & ATA_QCFLAG_FAILED))
1402 continue;
1403
1404 if (qc->err_mask)
1405 return;
1406 }
1407
1408 /* okay, this error is ours */
1409 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1410 if (rc) {
Tejun Heo02607312007-08-06 18:36:23 +09001411 ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
Tejun Heoe8ee8452006-05-15 21:03:46 +09001412 "(errno=%d)\n", rc);
1413 return;
1414 }
1415
Tejun Heo02607312007-08-06 18:36:23 +09001416 if (!(link->sactive & (1 << tag))) {
1417 ata_link_printk(link, KERN_ERR, "log page 10h reported "
Tejun Heoe8ee8452006-05-15 21:03:46 +09001418 "inactive tag %d\n", tag);
1419 return;
1420 }
1421
1422 /* we've got the perpetrator, condemn it */
1423 qc = __ata_qc_from_tag(ap, tag);
1424 memcpy(&qc->result_tf, &tf, sizeof(tf));
Tejun Heo5335b722007-07-16 14:29:40 +09001425 qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
Tejun Heoe8ee8452006-05-15 21:03:46 +09001426 ehc->i.err_mask &= ~AC_ERR_DEV;
1427}
1428
1429/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001430 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1431 * @qc: qc to analyze
1432 * @tf: Taskfile registers to analyze
1433 *
1434 * Analyze taskfile of @qc and further determine cause of
1435 * failure. This function also requests ATAPI sense data if
1436 * avaliable.
1437 *
1438 * LOCKING:
1439 * Kernel thread context (may sleep).
1440 *
1441 * RETURNS:
1442 * Determined recovery action
1443 */
1444static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1445 const struct ata_taskfile *tf)
1446{
1447 unsigned int tmp, action = 0;
1448 u8 stat = tf->command, err = tf->feature;
1449
1450 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1451 qc->err_mask |= AC_ERR_HSM;
1452 return ATA_EH_SOFTRESET;
1453 }
1454
Tejun Heoa51d6442007-03-20 15:24:11 +09001455 if (stat & (ATA_ERR | ATA_DF))
1456 qc->err_mask |= AC_ERR_DEV;
1457 else
Tejun Heo022bdb02006-05-15 20:58:22 +09001458 return 0;
1459
1460 switch (qc->dev->class) {
1461 case ATA_DEV_ATA:
1462 if (err & ATA_ICRC)
1463 qc->err_mask |= AC_ERR_ATA_BUS;
1464 if (err & ATA_UNC)
1465 qc->err_mask |= AC_ERR_MEDIA;
1466 if (err & ATA_IDNF)
1467 qc->err_mask |= AC_ERR_INVALID;
1468 break;
1469
1470 case ATA_DEV_ATAPI:
Tejun Heoa569a302006-11-21 10:40:51 +09001471 if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
Albert Lee56287762007-04-02 11:30:46 +08001472 tmp = atapi_eh_request_sense(qc);
Tejun Heoa569a302006-11-21 10:40:51 +09001473 if (!tmp) {
1474 /* ATA_QCFLAG_SENSE_VALID is used to
1475 * tell atapi_qc_complete() that sense
1476 * data is already valid.
1477 *
1478 * TODO: interpret sense data and set
1479 * appropriate err_mask.
1480 */
1481 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1482 } else
1483 qc->err_mask |= tmp;
1484 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001485 }
1486
1487 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1488 action |= ATA_EH_SOFTRESET;
1489
1490 return action;
1491}
1492
Tejun Heo76326ac2007-11-27 19:28:59 +09001493static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask,
1494 int *xfer_ok)
Tejun Heo022bdb02006-05-15 20:58:22 +09001495{
Tejun Heo76326ac2007-11-27 19:28:59 +09001496 int base = 0;
1497
1498 if (!(eflags & ATA_EFLAG_DUBIOUS_XFER))
1499 *xfer_ok = 1;
1500
1501 if (!*xfer_ok)
1502 base = ATA_ECAT_DUBIOUS_BASE;
1503
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001504 if (err_mask & AC_ERR_ATA_BUS)
Tejun Heo76326ac2007-11-27 19:28:59 +09001505 return base + ATA_ECAT_ATA_BUS;
Tejun Heo022bdb02006-05-15 20:58:22 +09001506
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001507 if (err_mask & AC_ERR_TIMEOUT)
Tejun Heo76326ac2007-11-27 19:28:59 +09001508 return base + ATA_ECAT_TOUT_HSM;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001509
Tejun Heo3884f7b2007-11-27 19:28:56 +09001510 if (eflags & ATA_EFLAG_IS_IO) {
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001511 if (err_mask & AC_ERR_HSM)
Tejun Heo76326ac2007-11-27 19:28:59 +09001512 return base + ATA_ECAT_TOUT_HSM;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001513 if ((err_mask &
1514 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
Tejun Heo76326ac2007-11-27 19:28:59 +09001515 return base + ATA_ECAT_UNK_DEV;
Tejun Heo022bdb02006-05-15 20:58:22 +09001516 }
1517
1518 return 0;
1519}
1520
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001521struct speed_down_verdict_arg {
Tejun Heo022bdb02006-05-15 20:58:22 +09001522 u64 since;
Tejun Heo76326ac2007-11-27 19:28:59 +09001523 int xfer_ok;
Tejun Heo3884f7b2007-11-27 19:28:56 +09001524 int nr_errors[ATA_ECAT_NR];
Tejun Heo022bdb02006-05-15 20:58:22 +09001525};
1526
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001527static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
Tejun Heo022bdb02006-05-15 20:58:22 +09001528{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001529 struct speed_down_verdict_arg *arg = void_arg;
Tejun Heo76326ac2007-11-27 19:28:59 +09001530 int cat;
Tejun Heo022bdb02006-05-15 20:58:22 +09001531
1532 if (ent->timestamp < arg->since)
1533 return -1;
1534
Tejun Heo76326ac2007-11-27 19:28:59 +09001535 cat = ata_eh_categorize_error(ent->eflags, ent->err_mask,
1536 &arg->xfer_ok);
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001537 arg->nr_errors[cat]++;
Tejun Heo76326ac2007-11-27 19:28:59 +09001538
Tejun Heo022bdb02006-05-15 20:58:22 +09001539 return 0;
1540}
1541
1542/**
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001543 * ata_eh_speed_down_verdict - Determine speed down verdict
Tejun Heo022bdb02006-05-15 20:58:22 +09001544 * @dev: Device of interest
1545 *
1546 * This function examines error ring of @dev and determines
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001547 * whether NCQ needs to be turned off, transfer speed should be
1548 * stepped down, or falling back to PIO is necessary.
Tejun Heo022bdb02006-05-15 20:58:22 +09001549 *
Tejun Heo3884f7b2007-11-27 19:28:56 +09001550 * ECAT_ATA_BUS : ATA_BUS error for any command
Tejun Heo022bdb02006-05-15 20:58:22 +09001551 *
Tejun Heo3884f7b2007-11-27 19:28:56 +09001552 * ECAT_TOUT_HSM : TIMEOUT for any command or HSM violation for
1553 * IO commands
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001554 *
Tejun Heo3884f7b2007-11-27 19:28:56 +09001555 * ECAT_UNK_DEV : Unknown DEV error for IO commands
Tejun Heo022bdb02006-05-15 20:58:22 +09001556 *
Tejun Heo76326ac2007-11-27 19:28:59 +09001557 * ECAT_DUBIOUS_* : Identical to above three but occurred while
1558 * data transfer hasn't been verified.
1559 *
Tejun Heo3884f7b2007-11-27 19:28:56 +09001560 * Verdicts are
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001561 *
Tejun Heo3884f7b2007-11-27 19:28:56 +09001562 * NCQ_OFF : Turn off NCQ.
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001563 *
Tejun Heo3884f7b2007-11-27 19:28:56 +09001564 * SPEED_DOWN : Speed down transfer speed but don't fall back
1565 * to PIO.
1566 *
1567 * FALLBACK_TO_PIO : Fall back to PIO.
1568 *
1569 * Even if multiple verdicts are returned, only one action is
Tejun Heo76326ac2007-11-27 19:28:59 +09001570 * taken per error. An action triggered by non-DUBIOUS errors
1571 * clears ering, while one triggered by DUBIOUS_* errors doesn't.
1572 * This is to expedite speed down decisions right after device is
1573 * initially configured.
Tejun Heo3884f7b2007-11-27 19:28:56 +09001574 *
Tejun Heo76326ac2007-11-27 19:28:59 +09001575 * The followings are speed down rules. #1 and #2 deal with
1576 * DUBIOUS errors.
1577 *
1578 * 1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors
1579 * occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO.
1580 *
1581 * 2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors
1582 * occurred during last 5 mins, NCQ_OFF.
1583 *
1584 * 3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors
Tejun Heo3884f7b2007-11-27 19:28:56 +09001585 * ocurred during last 5 mins, FALLBACK_TO_PIO
1586 *
Tejun Heo76326ac2007-11-27 19:28:59 +09001587 * 4. If more than 3 TOUT_HSM or UNK_DEV errors occurred
Tejun Heo3884f7b2007-11-27 19:28:56 +09001588 * during last 10 mins, NCQ_OFF.
1589 *
Tejun Heo76326ac2007-11-27 19:28:59 +09001590 * 5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6
Tejun Heo3884f7b2007-11-27 19:28:56 +09001591 * UNK_DEV errors occurred during last 10 mins, SPEED_DOWN.
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001592 *
Tejun Heo022bdb02006-05-15 20:58:22 +09001593 * LOCKING:
1594 * Inherited from caller.
1595 *
1596 * RETURNS:
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001597 * OR of ATA_EH_SPDN_* flags.
Tejun Heo022bdb02006-05-15 20:58:22 +09001598 */
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001599static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
Tejun Heo022bdb02006-05-15 20:58:22 +09001600{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001601 const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
1602 u64 j64 = get_jiffies_64();
1603 struct speed_down_verdict_arg arg;
1604 unsigned int verdict = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09001605
Tejun Heo3884f7b2007-11-27 19:28:56 +09001606 /* scan past 5 mins of error history */
1607 memset(&arg, 0, sizeof(arg));
1608 arg.since = j64 - min(j64, j5mins);
1609 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1610
Tejun Heo76326ac2007-11-27 19:28:59 +09001611 if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] +
1612 arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1)
1613 verdict |= ATA_EH_SPDN_SPEED_DOWN |
1614 ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS;
1615
1616 if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] +
1617 arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1)
1618 verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS;
1619
Tejun Heo3884f7b2007-11-27 19:28:56 +09001620 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1621 arg.nr_errors[ATA_ECAT_TOUT_HSM] +
Tejun Heo663f99b2007-11-27 19:28:57 +09001622 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
Tejun Heo3884f7b2007-11-27 19:28:56 +09001623 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
1624
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001625 /* scan past 10 mins of error history */
Tejun Heo022bdb02006-05-15 20:58:22 +09001626 memset(&arg, 0, sizeof(arg));
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001627 arg.since = j64 - min(j64, j10mins);
1628 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
Tejun Heo022bdb02006-05-15 20:58:22 +09001629
Tejun Heo3884f7b2007-11-27 19:28:56 +09001630 if (arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1631 arg.nr_errors[ATA_ECAT_UNK_DEV] > 3)
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001632 verdict |= ATA_EH_SPDN_NCQ_OFF;
Tejun Heo3884f7b2007-11-27 19:28:56 +09001633
1634 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1635 arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 ||
Tejun Heo663f99b2007-11-27 19:28:57 +09001636 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001637 verdict |= ATA_EH_SPDN_SPEED_DOWN;
Tejun Heo022bdb02006-05-15 20:58:22 +09001638
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001639 return verdict;
Tejun Heo022bdb02006-05-15 20:58:22 +09001640}
1641
1642/**
1643 * ata_eh_speed_down - record error and speed down if necessary
1644 * @dev: Failed device
Tejun Heo3884f7b2007-11-27 19:28:56 +09001645 * @eflags: mask of ATA_EFLAG_* flags
Tejun Heo022bdb02006-05-15 20:58:22 +09001646 * @err_mask: err_mask of the error
1647 *
1648 * Record error and examine error history to determine whether
1649 * adjusting transmission speed is necessary. It also sets
1650 * transmission limits appropriately if such adjustment is
1651 * necessary.
1652 *
1653 * LOCKING:
1654 * Kernel thread context (may sleep).
1655 *
1656 * RETURNS:
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001657 * Determined recovery action.
Tejun Heo022bdb02006-05-15 20:58:22 +09001658 */
Tejun Heo3884f7b2007-11-27 19:28:56 +09001659static unsigned int ata_eh_speed_down(struct ata_device *dev,
1660 unsigned int eflags, unsigned int err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001661{
Tejun Heo3884f7b2007-11-27 19:28:56 +09001662 struct ata_link *link = dev->link;
Tejun Heo76326ac2007-11-27 19:28:59 +09001663 int xfer_ok = 0;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001664 unsigned int verdict;
1665 unsigned int action = 0;
1666
1667 /* don't bother if Cat-0 error */
Tejun Heo76326ac2007-11-27 19:28:59 +09001668 if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0)
Tejun Heo022bdb02006-05-15 20:58:22 +09001669 return 0;
1670
1671 /* record error and determine whether speed down is necessary */
Tejun Heo3884f7b2007-11-27 19:28:56 +09001672 ata_ering_record(&dev->ering, eflags, err_mask);
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001673 verdict = ata_eh_speed_down_verdict(dev);
Tejun Heo022bdb02006-05-15 20:58:22 +09001674
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001675 /* turn off NCQ? */
1676 if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
1677 (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
1678 ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
1679 dev->flags |= ATA_DFLAG_NCQ_OFF;
1680 ata_dev_printk(dev, KERN_WARNING,
1681 "NCQ disabled due to excessive errors\n");
1682 goto done;
1683 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001684
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001685 /* speed down? */
1686 if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1687 /* speed down SATA link speed if possible */
Tejun Heo3884f7b2007-11-27 19:28:56 +09001688 if (sata_down_spd_limit(link) == 0) {
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001689 action |= ATA_EH_HARDRESET;
1690 goto done;
1691 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001692
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001693 /* lower transfer mode */
1694 if (dev->spdn_cnt < 2) {
1695 static const int dma_dnxfer_sel[] =
1696 { ATA_DNXFER_DMA, ATA_DNXFER_40C };
1697 static const int pio_dnxfer_sel[] =
1698 { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
1699 int sel;
Tejun Heo022bdb02006-05-15 20:58:22 +09001700
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001701 if (dev->xfer_shift != ATA_SHIFT_PIO)
1702 sel = dma_dnxfer_sel[dev->spdn_cnt];
1703 else
1704 sel = pio_dnxfer_sel[dev->spdn_cnt];
1705
1706 dev->spdn_cnt++;
1707
1708 if (ata_down_xfermask_limit(dev, sel) == 0) {
1709 action |= ATA_EH_SOFTRESET;
1710 goto done;
1711 }
1712 }
1713 }
1714
1715 /* Fall back to PIO? Slowing down to PIO is meaningless for
Tejun Heo663f99b2007-11-27 19:28:57 +09001716 * SATA ATA devices. Consider it only for PATA and SATAPI.
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001717 */
1718 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
Tejun Heo663f99b2007-11-27 19:28:57 +09001719 (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) &&
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001720 (dev->xfer_shift != ATA_SHIFT_PIO)) {
1721 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
1722 dev->spdn_cnt = 0;
1723 action |= ATA_EH_SOFTRESET;
1724 goto done;
1725 }
1726 }
1727
Tejun Heo022bdb02006-05-15 20:58:22 +09001728 return 0;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001729 done:
1730 /* device has been slowed down, blow error history */
Tejun Heo76326ac2007-11-27 19:28:59 +09001731 if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS))
1732 ata_ering_clear(&dev->ering);
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001733 return action;
Tejun Heo022bdb02006-05-15 20:58:22 +09001734}
1735
1736/**
Tejun Heo9b1e2652007-08-06 18:36:24 +09001737 * ata_eh_link_autopsy - analyze error and determine recovery action
1738 * @link: host link to perform autopsy on
Tejun Heo022bdb02006-05-15 20:58:22 +09001739 *
Tejun Heo02607312007-08-06 18:36:23 +09001740 * Analyze why @link failed and determine which recovery actions
1741 * are needed. This function also sets more detailed AC_ERR_*
1742 * values and fills sense data for ATAPI CHECK SENSE.
Tejun Heo022bdb02006-05-15 20:58:22 +09001743 *
1744 * LOCKING:
1745 * Kernel thread context (may sleep).
1746 */
Tejun Heo9b1e2652007-08-06 18:36:24 +09001747static void ata_eh_link_autopsy(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001748{
Tejun Heo02607312007-08-06 18:36:23 +09001749 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09001750 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heodfcc1732007-10-31 10:17:05 +09001751 struct ata_device *dev;
Tejun Heo3884f7b2007-11-27 19:28:56 +09001752 unsigned int all_err_mask = 0, eflags = 0;
1753 int tag;
Tejun Heo022bdb02006-05-15 20:58:22 +09001754 u32 serror;
1755 int rc;
1756
1757 DPRINTK("ENTER\n");
1758
Tejun Heo1cdaf532006-07-03 16:07:26 +09001759 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1760 return;
1761
Tejun Heo022bdb02006-05-15 20:58:22 +09001762 /* obtain and analyze SError */
Tejun Heo936fd732007-08-06 18:36:23 +09001763 rc = sata_scr_read(link, SCR_ERROR, &serror);
Tejun Heo022bdb02006-05-15 20:58:22 +09001764 if (rc == 0) {
1765 ehc->i.serror |= serror;
Tejun Heo02607312007-08-06 18:36:23 +09001766 ata_eh_analyze_serror(link);
Tejun Heo4e57c512007-07-16 14:29:41 +09001767 } else if (rc != -EOPNOTSUPP) {
1768 /* SError read failed, force hardreset and probing */
1769 ata_ehi_schedule_probe(&ehc->i);
Tejun Heo4528e4d2006-07-08 20:17:26 +09001770 ehc->i.action |= ATA_EH_HARDRESET;
Tejun Heo4e57c512007-07-16 14:29:41 +09001771 ehc->i.err_mask |= AC_ERR_OTHER;
1772 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001773
Tejun Heoe8ee8452006-05-15 21:03:46 +09001774 /* analyze NCQ failure */
Tejun Heo02607312007-08-06 18:36:23 +09001775 ata_eh_analyze_ncq_error(link);
Tejun Heoe8ee8452006-05-15 21:03:46 +09001776
Tejun Heo022bdb02006-05-15 20:58:22 +09001777 /* any real error trumps AC_ERR_OTHER */
1778 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1779 ehc->i.err_mask &= ~AC_ERR_OTHER;
1780
1781 all_err_mask |= ehc->i.err_mask;
1782
1783 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1784 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1785
Tejun Heo02607312007-08-06 18:36:23 +09001786 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001787 continue;
1788
1789 /* inherit upper level err_mask */
1790 qc->err_mask |= ehc->i.err_mask;
1791
Tejun Heo022bdb02006-05-15 20:58:22 +09001792 /* analyze TF */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001793 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
Tejun Heo022bdb02006-05-15 20:58:22 +09001794
1795 /* DEV errors are probably spurious in case of ATA_BUS error */
1796 if (qc->err_mask & AC_ERR_ATA_BUS)
1797 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1798 AC_ERR_INVALID);
1799
1800 /* any real error trumps unknown error */
1801 if (qc->err_mask & ~AC_ERR_OTHER)
1802 qc->err_mask &= ~AC_ERR_OTHER;
1803
1804 /* SENSE_VALID trumps dev/unknown error and revalidation */
Tejun Heof90f0822007-10-26 16:12:41 +09001805 if (qc->flags & ATA_QCFLAG_SENSE_VALID)
Tejun Heo022bdb02006-05-15 20:58:22 +09001806 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
Tejun Heo022bdb02006-05-15 20:58:22 +09001807
1808 /* accumulate error info */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001809 ehc->i.dev = qc->dev;
Tejun Heo022bdb02006-05-15 20:58:22 +09001810 all_err_mask |= qc->err_mask;
1811 if (qc->flags & ATA_QCFLAG_IO)
Tejun Heo3884f7b2007-11-27 19:28:56 +09001812 eflags |= ATA_EFLAG_IS_IO;
Tejun Heo022bdb02006-05-15 20:58:22 +09001813 }
1814
Tejun Heoa20f33f2006-05-16 12:58:24 +09001815 /* enforce default EH actions */
Tejun Heob51e9e52006-06-29 01:29:30 +09001816 if (ap->pflags & ATA_PFLAG_FROZEN ||
Tejun Heoa20f33f2006-05-16 12:58:24 +09001817 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
Tejun Heo4528e4d2006-07-08 20:17:26 +09001818 ehc->i.action |= ATA_EH_SOFTRESET;
Tejun Heo3884f7b2007-11-27 19:28:56 +09001819 else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) ||
1820 (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV)))
Tejun Heo4528e4d2006-07-08 20:17:26 +09001821 ehc->i.action |= ATA_EH_REVALIDATE;
Tejun Heo022bdb02006-05-15 20:58:22 +09001822
Tejun Heodfcc1732007-10-31 10:17:05 +09001823 /* If we have offending qcs and the associated failed device,
1824 * perform per-dev EH action only on the offending device.
1825 */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001826 if (ehc->i.dev) {
Tejun Heo4528e4d2006-07-08 20:17:26 +09001827 ehc->i.dev_action[ehc->i.dev->devno] |=
1828 ehc->i.action & ATA_EH_PERDEV_MASK;
1829 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
Tejun Heo47005f22006-06-19 18:27:23 +09001830 }
1831
Tejun Heo2695e362008-01-10 13:41:23 +09001832 /* propagate timeout to host link */
1833 if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link))
1834 ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT;
1835
1836 /* record error and consider speeding down */
Tejun Heodfcc1732007-10-31 10:17:05 +09001837 dev = ehc->i.dev;
Tejun Heo2695e362008-01-10 13:41:23 +09001838 if (!dev && ((ata_link_max_devices(link) == 1 &&
1839 ata_dev_enabled(link->device))))
1840 dev = link->device;
Tejun Heodfcc1732007-10-31 10:17:05 +09001841
Tejun Heo76326ac2007-11-27 19:28:59 +09001842 if (dev) {
1843 if (dev->flags & ATA_DFLAG_DUBIOUS_XFER)
1844 eflags |= ATA_EFLAG_DUBIOUS_XFER;
Tejun Heo3884f7b2007-11-27 19:28:56 +09001845 ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask);
Tejun Heo76326ac2007-11-27 19:28:59 +09001846 }
Tejun Heodfcc1732007-10-31 10:17:05 +09001847
Tejun Heo022bdb02006-05-15 20:58:22 +09001848 DPRINTK("EXIT\n");
1849}
1850
1851/**
Tejun Heo9b1e2652007-08-06 18:36:24 +09001852 * ata_eh_autopsy - analyze error and determine recovery action
1853 * @ap: host port to perform autopsy on
1854 *
1855 * Analyze all links of @ap and determine why they failed and
1856 * which recovery actions are needed.
1857 *
1858 * LOCKING:
1859 * Kernel thread context (may sleep).
1860 */
Tejun Heofb7fd612007-09-23 13:14:12 +09001861void ata_eh_autopsy(struct ata_port *ap)
Tejun Heo9b1e2652007-08-06 18:36:24 +09001862{
1863 struct ata_link *link;
1864
Tejun Heo2695e362008-01-10 13:41:23 +09001865 ata_port_for_each_link(link, ap)
Tejun Heo9b1e2652007-08-06 18:36:24 +09001866 ata_eh_link_autopsy(link);
Tejun Heo2695e362008-01-10 13:41:23 +09001867
1868 /* Autopsy of fanout ports can affect host link autopsy.
1869 * Perform host link autopsy last.
1870 */
1871 if (ap->nr_pmp_links)
1872 ata_eh_link_autopsy(&ap->link);
Tejun Heo9b1e2652007-08-06 18:36:24 +09001873}
1874
1875/**
1876 * ata_eh_link_report - report error handling to user
Tejun Heo02607312007-08-06 18:36:23 +09001877 * @link: ATA link EH is going on
Tejun Heo022bdb02006-05-15 20:58:22 +09001878 *
1879 * Report EH to user.
1880 *
1881 * LOCKING:
1882 * None.
1883 */
Tejun Heo9b1e2652007-08-06 18:36:24 +09001884static void ata_eh_link_report(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001885{
Tejun Heo02607312007-08-06 18:36:23 +09001886 struct ata_port *ap = link->ap;
1887 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001888 const char *frozen, *desc;
Tejun Heoa1e10f72007-08-18 13:28:49 +09001889 char tries_buf[6];
Tejun Heo022bdb02006-05-15 20:58:22 +09001890 int tag, nr_failed = 0;
1891
Tejun Heo94ff3d52007-10-09 14:57:56 +09001892 if (ehc->i.flags & ATA_EHI_QUIET)
1893 return;
1894
Tejun Heo022bdb02006-05-15 20:58:22 +09001895 desc = NULL;
1896 if (ehc->i.desc[0] != '\0')
1897 desc = ehc->i.desc;
1898
1899 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1900 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1901
Tejun Heoe027bd32007-10-26 16:19:26 +09001902 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link ||
1903 ((qc->flags & ATA_QCFLAG_QUIET) &&
1904 qc->err_mask == AC_ERR_DEV))
Tejun Heo022bdb02006-05-15 20:58:22 +09001905 continue;
1906 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1907 continue;
1908
1909 nr_failed++;
1910 }
1911
1912 if (!nr_failed && !ehc->i.err_mask)
1913 return;
1914
1915 frozen = "";
Tejun Heob51e9e52006-06-29 01:29:30 +09001916 if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heo022bdb02006-05-15 20:58:22 +09001917 frozen = " frozen";
1918
Tejun Heoa1e10f72007-08-18 13:28:49 +09001919 memset(tries_buf, 0, sizeof(tries_buf));
1920 if (ap->eh_tries < ATA_EH_MAX_TRIES)
1921 snprintf(tries_buf, sizeof(tries_buf) - 1, " t%d",
1922 ap->eh_tries);
1923
Tejun Heo022bdb02006-05-15 20:58:22 +09001924 if (ehc->i.dev) {
Tejun Heoe8ee8452006-05-15 21:03:46 +09001925 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
Tejun Heoa1e10f72007-08-18 13:28:49 +09001926 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1927 ehc->i.err_mask, link->sactive, ehc->i.serror,
1928 ehc->i.action, frozen, tries_buf);
Tejun Heo022bdb02006-05-15 20:58:22 +09001929 if (desc)
Tejun Heob64bbc32007-07-16 14:29:39 +09001930 ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
Tejun Heo022bdb02006-05-15 20:58:22 +09001931 } else {
Tejun Heo02607312007-08-06 18:36:23 +09001932 ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
Tejun Heoa1e10f72007-08-18 13:28:49 +09001933 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1934 ehc->i.err_mask, link->sactive, ehc->i.serror,
1935 ehc->i.action, frozen, tries_buf);
Tejun Heo022bdb02006-05-15 20:58:22 +09001936 if (desc)
Tejun Heo02607312007-08-06 18:36:23 +09001937 ata_link_printk(link, KERN_ERR, "%s\n", desc);
Tejun Heo022bdb02006-05-15 20:58:22 +09001938 }
1939
Robert Hancock1333e192007-10-02 11:22:02 -04001940 if (ehc->i.serror)
1941 ata_port_printk(ap, KERN_ERR,
1942 "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
1943 ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
1944 ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
1945 ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
1946 ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
1947 ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
1948 ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
1949 ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
1950 ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
1951 ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
1952 ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
1953 ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
1954 ehc->i.serror & SERR_CRC ? "BadCRC " : "",
1955 ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
1956 ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
1957 ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
1958 ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001959 ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
Robert Hancock1333e192007-10-02 11:22:02 -04001960
Tejun Heo022bdb02006-05-15 20:58:22 +09001961 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1962 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
Tejun Heo8a937582006-11-14 22:36:12 +09001963 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
Tejun Heoabb6a882007-11-28 23:16:09 +09001964 const u8 *cdb = qc->cdb;
1965 char data_buf[20] = "";
1966 char cdb_buf[70] = "";
Tejun Heo022bdb02006-05-15 20:58:22 +09001967
Tejun Heo02607312007-08-06 18:36:23 +09001968 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
1969 qc->dev->link != link || !qc->err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001970 continue;
1971
Tejun Heoabb6a882007-11-28 23:16:09 +09001972 if (qc->dma_dir != DMA_NONE) {
1973 static const char *dma_str[] = {
1974 [DMA_BIDIRECTIONAL] = "bidi",
1975 [DMA_TO_DEVICE] = "out",
1976 [DMA_FROM_DEVICE] = "in",
1977 };
1978 static const char *prot_str[] = {
1979 [ATA_PROT_PIO] = "pio",
1980 [ATA_PROT_DMA] = "dma",
1981 [ATA_PROT_NCQ] = "ncq",
Tejun Heo0dc36882007-12-18 16:34:43 -05001982 [ATAPI_PROT_PIO] = "pio",
1983 [ATAPI_PROT_DMA] = "dma",
Tejun Heoabb6a882007-11-28 23:16:09 +09001984 };
1985
1986 snprintf(data_buf, sizeof(data_buf), " %s %u %s",
1987 prot_str[qc->tf.protocol], qc->nbytes,
1988 dma_str[qc->dma_dir]);
1989 }
1990
Jeff Garzike39eec12007-12-01 18:05:39 -05001991 if (ata_is_atapi(qc->tf.protocol))
Tejun Heoabb6a882007-11-28 23:16:09 +09001992 snprintf(cdb_buf, sizeof(cdb_buf),
1993 "cdb %02x %02x %02x %02x %02x %02x %02x %02x "
1994 "%02x %02x %02x %02x %02x %02x %02x %02x\n ",
1995 cdb[0], cdb[1], cdb[2], cdb[3],
1996 cdb[4], cdb[5], cdb[6], cdb[7],
1997 cdb[8], cdb[9], cdb[10], cdb[11],
1998 cdb[12], cdb[13], cdb[14], cdb[15]);
1999
Tejun Heo8a937582006-11-14 22:36:12 +09002000 ata_dev_printk(qc->dev, KERN_ERR,
2001 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
Tejun Heoabb6a882007-11-28 23:16:09 +09002002 "tag %d%s\n %s"
Tejun Heo8a937582006-11-14 22:36:12 +09002003 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
Tejun Heo5335b722007-07-16 14:29:40 +09002004 "Emask 0x%x (%s)%s\n",
Tejun Heo8a937582006-11-14 22:36:12 +09002005 cmd->command, cmd->feature, cmd->nsect,
2006 cmd->lbal, cmd->lbam, cmd->lbah,
2007 cmd->hob_feature, cmd->hob_nsect,
2008 cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
Tejun Heoabb6a882007-11-28 23:16:09 +09002009 cmd->device, qc->tag, data_buf, cdb_buf,
Tejun Heo8a937582006-11-14 22:36:12 +09002010 res->command, res->feature, res->nsect,
2011 res->lbal, res->lbam, res->lbah,
2012 res->hob_feature, res->hob_nsect,
2013 res->hob_lbal, res->hob_lbam, res->hob_lbah,
Tejun Heo5335b722007-07-16 14:29:40 +09002014 res->device, qc->err_mask, ata_err_string(qc->err_mask),
2015 qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
Robert Hancock1333e192007-10-02 11:22:02 -04002016
2017 if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002018 ATA_ERR)) {
Robert Hancock1333e192007-10-02 11:22:02 -04002019 if (res->command & ATA_BUSY)
2020 ata_dev_printk(qc->dev, KERN_ERR,
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002021 "status: { Busy }\n");
Robert Hancock1333e192007-10-02 11:22:02 -04002022 else
2023 ata_dev_printk(qc->dev, KERN_ERR,
2024 "status: { %s%s%s%s}\n",
2025 res->command & ATA_DRDY ? "DRDY " : "",
2026 res->command & ATA_DF ? "DF " : "",
2027 res->command & ATA_DRQ ? "DRQ " : "",
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002028 res->command & ATA_ERR ? "ERR " : "");
Robert Hancock1333e192007-10-02 11:22:02 -04002029 }
2030
2031 if (cmd->command != ATA_CMD_PACKET &&
2032 (res->feature & (ATA_ICRC | ATA_UNC | ATA_IDNF |
2033 ATA_ABORTED)))
2034 ata_dev_printk(qc->dev, KERN_ERR,
2035 "error: { %s%s%s%s}\n",
2036 res->feature & ATA_ICRC ? "ICRC " : "",
2037 res->feature & ATA_UNC ? "UNC " : "",
2038 res->feature & ATA_IDNF ? "IDNF " : "",
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002039 res->feature & ATA_ABORTED ? "ABRT " : "");
Tejun Heo022bdb02006-05-15 20:58:22 +09002040 }
2041}
2042
Tejun Heo9b1e2652007-08-06 18:36:24 +09002043/**
2044 * ata_eh_report - report error handling to user
2045 * @ap: ATA port to report EH about
2046 *
2047 * Report EH to user.
2048 *
2049 * LOCKING:
2050 * None.
2051 */
Tejun Heofb7fd612007-09-23 13:14:12 +09002052void ata_eh_report(struct ata_port *ap)
Tejun Heo9b1e2652007-08-06 18:36:24 +09002053{
2054 struct ata_link *link;
2055
2056 __ata_port_for_each_link(link, ap)
2057 ata_eh_link_report(link);
2058}
2059
Tejun Heocc0680a2007-08-06 18:36:23 +09002060static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
Tejun Heod4b2bab2007-02-02 16:50:52 +09002061 unsigned int *classes, unsigned long deadline)
Tejun Heod87fa382006-05-31 18:28:24 +09002062{
Tejun Heof58229f2007-08-06 18:36:23 +09002063 struct ata_device *dev;
2064 int rc;
Tejun Heod87fa382006-05-31 18:28:24 +09002065
Tejun Heocc0680a2007-08-06 18:36:23 +09002066 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09002067 classes[dev->devno] = ATA_DEV_UNKNOWN;
Tejun Heod87fa382006-05-31 18:28:24 +09002068
Tejun Heocc0680a2007-08-06 18:36:23 +09002069 rc = reset(link, classes, deadline);
Tejun Heod87fa382006-05-31 18:28:24 +09002070 if (rc)
2071 return rc;
2072
2073 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2074 * is complete and convert all ATA_DEV_UNKNOWN to
2075 * ATA_DEV_NONE.
2076 */
Tejun Heocc0680a2007-08-06 18:36:23 +09002077 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09002078 if (classes[dev->devno] != ATA_DEV_UNKNOWN)
Tejun Heod87fa382006-05-31 18:28:24 +09002079 break;
2080
Tejun Heof58229f2007-08-06 18:36:23 +09002081 if (dev) {
Tejun Heocc0680a2007-08-06 18:36:23 +09002082 ata_link_for_each_dev(dev, link) {
Tejun Heof58229f2007-08-06 18:36:23 +09002083 if (classes[dev->devno] == ATA_DEV_UNKNOWN)
2084 classes[dev->devno] = ATA_DEV_NONE;
2085 }
2086 }
Tejun Heod87fa382006-05-31 18:28:24 +09002087
2088 return 0;
2089}
2090
Tejun Heoae791c02007-09-23 13:14:12 +09002091static int ata_eh_followup_srst_needed(struct ata_link *link,
2092 int rc, int classify,
Tejun Heo664faf02006-05-31 18:27:50 +09002093 const unsigned int *classes)
2094{
Tejun Heoae791c02007-09-23 13:14:12 +09002095 if (link->flags & ATA_LFLAG_NO_SRST)
2096 return 0;
Tejun Heo664faf02006-05-31 18:27:50 +09002097 if (rc == -EAGAIN)
2098 return 1;
2099 if (rc != 0)
2100 return 0;
Tejun Heo3495de72007-09-23 13:19:53 +09002101 if ((link->ap->flags & ATA_FLAG_PMP) && ata_is_host_link(link))
2102 return 1;
Tejun Heoae791c02007-09-23 13:14:12 +09002103 if (classify && !(link->flags & ATA_LFLAG_ASSUME_CLASS) &&
2104 classes[0] == ATA_DEV_UNKNOWN)
Tejun Heo664faf02006-05-31 18:27:50 +09002105 return 1;
2106 return 0;
2107}
2108
Tejun Heofb7fd612007-09-23 13:14:12 +09002109int ata_eh_reset(struct ata_link *link, int classify,
2110 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
2111 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
Tejun Heo022bdb02006-05-15 20:58:22 +09002112{
Tejun Heo416dc9e2007-10-31 10:17:03 +09002113 const int max_tries = ARRAY_SIZE(ata_eh_reset_timeouts);
Tejun Heoafaa5c32007-10-09 15:06:10 +09002114 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09002115 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo664faf02006-05-31 18:27:50 +09002116 unsigned int *classes = ehc->classes;
Tejun Heo416dc9e2007-10-31 10:17:03 +09002117 unsigned int lflags = link->flags;
Tejun Heo1cdaf532006-07-03 16:07:26 +09002118 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
Tejun Heo31daabd2007-02-02 16:50:52 +09002119 int try = 0;
Tejun Heof58229f2007-08-06 18:36:23 +09002120 struct ata_device *dev;
Tejun Heo416dc9e2007-10-31 10:17:03 +09002121 unsigned long deadline, now;
Tejun Heo0e06d9c2007-10-24 15:21:26 +09002122 unsigned int tmp_action;
Tejun Heo022bdb02006-05-15 20:58:22 +09002123 ata_reset_fn_t reset;
Tejun Heoafaa5c32007-10-09 15:06:10 +09002124 unsigned long flags;
Tejun Heo416dc9e2007-10-31 10:17:03 +09002125 u32 sstatus;
Tejun Heof58229f2007-08-06 18:36:23 +09002126 int rc;
Tejun Heo022bdb02006-05-15 20:58:22 +09002127
Tejun Heo13abf502006-07-10 23:18:46 +09002128 /* about to reset */
Tejun Heoafaa5c32007-10-09 15:06:10 +09002129 spin_lock_irqsave(ap->lock, flags);
2130 ap->pflags |= ATA_PFLAG_RESETTING;
2131 spin_unlock_irqrestore(ap->lock, flags);
2132
Tejun Heo955e57d2007-08-06 18:36:23 +09002133 ata_eh_about_to_do(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
Tejun Heo13abf502006-07-10 23:18:46 +09002134
Tejun Heocdeab112007-10-29 16:41:09 +09002135 ata_link_for_each_dev(dev, link) {
2136 /* If we issue an SRST then an ATA drive (not ATAPI)
2137 * may change configuration and be in PIO0 timing. If
2138 * we do a hard reset (or are coming from power on)
2139 * this is true for ATA or ATAPI. Until we've set a
2140 * suitable controller mode we should not touch the
2141 * bus as we may be talking too fast.
2142 */
2143 dev->pio_mode = XFER_PIO_0;
2144
2145 /* If the controller has a pio mode setup function
2146 * then use it to set the chipset to rights. Don't
2147 * touch the DMA setup as that will be dealt with when
2148 * configuring devices.
2149 */
2150 if (ap->ops->set_piomode)
2151 ap->ops->set_piomode(ap, dev);
2152 }
2153
Tejun Heof5914a42006-05-31 18:27:48 +09002154 /* Determine which reset to use and record in ehc->i.action.
2155 * prereset() may examine and modify it.
2156 */
Tejun Heo416dc9e2007-10-31 10:17:03 +09002157 if (softreset && (!hardreset || (!(lflags & ATA_LFLAG_NO_SRST) &&
Tejun Heoae791c02007-09-23 13:14:12 +09002158 !sata_set_spd_needed(link) &&
Tejun Heo0e06d9c2007-10-24 15:21:26 +09002159 !(ehc->i.action & ATA_EH_HARDRESET))))
2160 tmp_action = ATA_EH_SOFTRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09002161 else
Tejun Heo0e06d9c2007-10-24 15:21:26 +09002162 tmp_action = ATA_EH_HARDRESET;
2163
2164 ehc->i.action = (ehc->i.action & ~ATA_EH_RESET_MASK) | tmp_action;
Tejun Heof5914a42006-05-31 18:27:48 +09002165
2166 if (prereset) {
Tejun Heocc0680a2007-08-06 18:36:23 +09002167 rc = prereset(link, jiffies + ATA_EH_PRERESET_TIMEOUT);
Tejun Heof5914a42006-05-31 18:27:48 +09002168 if (rc) {
Alan Coxc9619222006-09-26 17:53:38 +01002169 if (rc == -ENOENT) {
Tejun Heocc0680a2007-08-06 18:36:23 +09002170 ata_link_printk(link, KERN_DEBUG,
Tejun Heo4aa9ab62007-03-12 17:24:08 +09002171 "port disabled. ignoring.\n");
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002172 ehc->i.action &= ~ATA_EH_RESET_MASK;
Tejun Heo4aa9ab62007-03-12 17:24:08 +09002173
Tejun Heo936fd732007-08-06 18:36:23 +09002174 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09002175 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heo4aa9ab62007-03-12 17:24:08 +09002176
2177 rc = 0;
Alan Coxc9619222006-09-26 17:53:38 +01002178 } else
Tejun Heocc0680a2007-08-06 18:36:23 +09002179 ata_link_printk(link, KERN_ERR,
Tejun Heof5914a42006-05-31 18:27:48 +09002180 "prereset failed (errno=%d)\n", rc);
Tejun Heofccb6ea2007-07-16 14:29:41 +09002181 goto out;
Tejun Heof5914a42006-05-31 18:27:48 +09002182 }
2183 }
2184
2185 /* prereset() might have modified ehc->i.action */
2186 if (ehc->i.action & ATA_EH_HARDRESET)
Tejun Heo022bdb02006-05-15 20:58:22 +09002187 reset = hardreset;
Tejun Heof5914a42006-05-31 18:27:48 +09002188 else if (ehc->i.action & ATA_EH_SOFTRESET)
2189 reset = softreset;
2190 else {
2191 /* prereset told us not to reset, bang classes and return */
Tejun Heo936fd732007-08-06 18:36:23 +09002192 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09002193 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heofccb6ea2007-07-16 14:29:41 +09002194 rc = 0;
2195 goto out;
Tejun Heof5914a42006-05-31 18:27:48 +09002196 }
2197
2198 /* did prereset() screw up? if so, fix up to avoid oopsing */
2199 if (!reset) {
Tejun Heof5914a42006-05-31 18:27:48 +09002200 if (softreset)
2201 reset = softreset;
2202 else
2203 reset = hardreset;
2204 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002205
2206 retry:
Tejun Heo31daabd2007-02-02 16:50:52 +09002207 deadline = jiffies + ata_eh_reset_timeouts[try++];
2208
Tejun Heo3e706392006-05-31 18:28:11 +09002209 /* shut up during boot probing */
2210 if (verbose)
Tejun Heocc0680a2007-08-06 18:36:23 +09002211 ata_link_printk(link, KERN_INFO, "%s resetting link\n",
Tejun Heo3e706392006-05-31 18:28:11 +09002212 reset == softreset ? "soft" : "hard");
Tejun Heo022bdb02006-05-15 20:58:22 +09002213
Tejun Heo13abf502006-07-10 23:18:46 +09002214 /* mark that this EH session started with reset */
Tejun Heo0d64a232007-04-23 02:41:05 +09002215 if (reset == hardreset)
2216 ehc->i.flags |= ATA_EHI_DID_HARDRESET;
2217 else
2218 ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09002219
Tejun Heocc0680a2007-08-06 18:36:23 +09002220 rc = ata_do_reset(link, reset, classes, deadline);
Tejun Heo022bdb02006-05-15 20:58:22 +09002221
Tejun Heo664faf02006-05-31 18:27:50 +09002222 if (reset == hardreset &&
Tejun Heoae791c02007-09-23 13:14:12 +09002223 ata_eh_followup_srst_needed(link, rc, classify, classes)) {
Tejun Heo664faf02006-05-31 18:27:50 +09002224 /* okay, let's do follow-up softreset */
Tejun Heo664faf02006-05-31 18:27:50 +09002225 reset = softreset;
2226
2227 if (!reset) {
Tejun Heocc0680a2007-08-06 18:36:23 +09002228 ata_link_printk(link, KERN_ERR,
Tejun Heo664faf02006-05-31 18:27:50 +09002229 "follow-up softreset required "
2230 "but no softreset avaliable\n");
Tejun Heofccb6ea2007-07-16 14:29:41 +09002231 rc = -EINVAL;
Tejun Heo08cf69d2007-10-31 10:17:04 +09002232 goto fail;
Tejun Heo664faf02006-05-31 18:27:50 +09002233 }
2234
Tejun Heo955e57d2007-08-06 18:36:23 +09002235 ata_eh_about_to_do(link, NULL, ATA_EH_RESET_MASK);
Tejun Heocc0680a2007-08-06 18:36:23 +09002236 rc = ata_do_reset(link, reset, classes, deadline);
Tejun Heo664faf02006-05-31 18:27:50 +09002237 }
2238
Tejun Heo416dc9e2007-10-31 10:17:03 +09002239 /* -EAGAIN can happen if we skipped followup SRST */
2240 if (rc && rc != -EAGAIN)
2241 goto fail;
Tejun Heoae791c02007-09-23 13:14:12 +09002242
Tejun Heo08cf69d2007-10-31 10:17:04 +09002243 /* was classification successful? */
2244 if (classify && classes[0] == ATA_DEV_UNKNOWN &&
2245 !(lflags & ATA_LFLAG_ASSUME_CLASS)) {
2246 if (try < max_tries) {
2247 ata_link_printk(link, KERN_WARNING,
2248 "classification failed\n");
2249 rc = -EINVAL;
2250 goto fail;
2251 }
2252
2253 ata_link_printk(link, KERN_WARNING,
2254 "classfication failed, assuming ATA\n");
2255 lflags |= ATA_LFLAG_ASSUME_ATA;
2256 }
2257
Tejun Heo416dc9e2007-10-31 10:17:03 +09002258 ata_link_for_each_dev(dev, link) {
2259 /* After the reset, the device state is PIO 0 and the
2260 * controller state is undefined. Reset also wakes up
2261 * drives from sleeping mode.
2262 */
2263 dev->pio_mode = XFER_PIO_0;
2264 dev->flags &= ~ATA_DFLAG_SLEEPING;
Tejun Heo664faf02006-05-31 18:27:50 +09002265
Tejun Heo416dc9e2007-10-31 10:17:03 +09002266 if (ata_link_offline(link))
2267 continue;
Tejun Heo664faf02006-05-31 18:27:50 +09002268
Tejun Heo4ccd3322008-01-08 20:26:12 +09002269 /* apply class override */
Tejun Heo416dc9e2007-10-31 10:17:03 +09002270 if (lflags & ATA_LFLAG_ASSUME_ATA)
2271 classes[dev->devno] = ATA_DEV_ATA;
2272 else if (lflags & ATA_LFLAG_ASSUME_SEMB)
2273 classes[dev->devno] = ATA_DEV_SEMB_UNSUP; /* not yet */
Tejun Heo022bdb02006-05-15 20:58:22 +09002274 }
2275
Tejun Heo416dc9e2007-10-31 10:17:03 +09002276 /* record current link speed */
2277 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2278 link->sata_spd = (sstatus >> 4) & 0xf;
Tejun Heo008a7892007-07-16 14:29:40 +09002279
Tejun Heo416dc9e2007-10-31 10:17:03 +09002280 if (postreset)
2281 postreset(link, classes);
Tejun Heo20952b62006-05-31 18:27:23 +09002282
Tejun Heo416dc9e2007-10-31 10:17:03 +09002283 /* reset successful, schedule revalidation */
2284 ata_eh_done(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
2285 ehc->i.action |= ATA_EH_REVALIDATE;
Tejun Heoae791c02007-09-23 13:14:12 +09002286
Tejun Heo416dc9e2007-10-31 10:17:03 +09002287 rc = 0;
Tejun Heofccb6ea2007-07-16 14:29:41 +09002288 out:
2289 /* clear hotplug flag */
2290 ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
Tejun Heoafaa5c32007-10-09 15:06:10 +09002291
2292 spin_lock_irqsave(ap->lock, flags);
2293 ap->pflags &= ~ATA_PFLAG_RESETTING;
2294 spin_unlock_irqrestore(ap->lock, flags);
2295
Tejun Heo022bdb02006-05-15 20:58:22 +09002296 return rc;
Tejun Heo416dc9e2007-10-31 10:17:03 +09002297
2298 fail:
2299 if (rc == -ERESTART || try >= max_tries)
2300 goto out;
2301
2302 now = jiffies;
2303 if (time_before(now, deadline)) {
2304 unsigned long delta = deadline - now;
2305
2306 ata_link_printk(link, KERN_WARNING, "reset failed "
2307 "(errno=%d), retrying in %u secs\n",
2308 rc, (jiffies_to_msecs(delta) + 999) / 1000);
2309
2310 while (delta)
2311 delta = schedule_timeout_uninterruptible(delta);
2312 }
2313
2314 if (rc == -EPIPE || try == max_tries - 1)
2315 sata_down_spd_limit(link);
2316 if (hardreset)
2317 reset = hardreset;
2318 goto retry;
Tejun Heo022bdb02006-05-15 20:58:22 +09002319}
2320
Tejun Heo02607312007-08-06 18:36:23 +09002321static int ata_eh_revalidate_and_attach(struct ata_link *link,
Tejun Heo084fe632006-05-31 18:28:03 +09002322 struct ata_device **r_failed_dev)
Tejun Heo022bdb02006-05-15 20:58:22 +09002323{
Tejun Heo02607312007-08-06 18:36:23 +09002324 struct ata_port *ap = link->ap;
2325 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09002326 struct ata_device *dev;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002327 unsigned int new_mask = 0;
Tejun Heo084fe632006-05-31 18:28:03 +09002328 unsigned long flags;
Tejun Heof58229f2007-08-06 18:36:23 +09002329 int rc = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09002330
2331 DPRINTK("ENTER\n");
2332
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002333 /* For PATA drive side cable detection to work, IDENTIFY must
2334 * be done backwards such that PDIAG- is released by the slave
2335 * device before the master device is identified.
2336 */
Tejun Heo02607312007-08-06 18:36:23 +09002337 ata_link_for_each_dev_reverse(dev, link) {
Tejun Heof58229f2007-08-06 18:36:23 +09002338 unsigned int action = ata_eh_dev_action(dev);
2339 unsigned int readid_flags = 0;
Tejun Heo47005f22006-06-19 18:27:23 +09002340
Tejun Heobff04642006-11-10 18:08:10 +09002341 if (ehc->i.flags & ATA_EHI_DID_RESET)
2342 readid_flags |= ATA_READID_POSTRESET;
2343
Tejun Heo9666f402007-05-04 21:27:47 +02002344 if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
Tejun Heo633273a2007-09-23 13:19:54 +09002345 WARN_ON(dev->class == ATA_DEV_PMP);
2346
Tejun Heo02607312007-08-06 18:36:23 +09002347 if (ata_link_offline(link)) {
Tejun Heo022bdb02006-05-15 20:58:22 +09002348 rc = -EIO;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002349 goto err;
Tejun Heo022bdb02006-05-15 20:58:22 +09002350 }
2351
Tejun Heo02607312007-08-06 18:36:23 +09002352 ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
Tejun Heo422c9da2007-09-23 13:14:12 +09002353 rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
2354 readid_flags);
Tejun Heo022bdb02006-05-15 20:58:22 +09002355 if (rc)
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002356 goto err;
Tejun Heo022bdb02006-05-15 20:58:22 +09002357
Tejun Heo02607312007-08-06 18:36:23 +09002358 ata_eh_done(link, dev, ATA_EH_REVALIDATE);
Tejun Heo47005f22006-06-19 18:27:23 +09002359
Tejun Heobaa1e782006-11-01 18:39:27 +09002360 /* Configuration may have changed, reconfigure
2361 * transfer mode.
2362 */
2363 ehc->i.flags |= ATA_EHI_SETMODE;
2364
zhao, forrest3057ac32006-06-12 12:01:34 +08002365 /* schedule the scsi_rescan_device() here */
2366 queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
Tejun Heo084fe632006-05-31 18:28:03 +09002367 } else if (dev->class == ATA_DEV_UNKNOWN &&
2368 ehc->tries[dev->devno] &&
2369 ata_class_enabled(ehc->classes[dev->devno])) {
2370 dev->class = ehc->classes[dev->devno];
2371
Tejun Heo633273a2007-09-23 13:19:54 +09002372 if (dev->class == ATA_DEV_PMP)
2373 rc = sata_pmp_attach(dev);
2374 else
2375 rc = ata_dev_read_id(dev, &dev->class,
2376 readid_flags, dev->id);
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002377 switch (rc) {
2378 case 0:
Tejun Heof58229f2007-08-06 18:36:23 +09002379 new_mask |= 1 << dev->devno;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002380 break;
2381 case -ENOENT:
Tejun Heo55a8e2c2006-11-10 18:08:10 +09002382 /* IDENTIFY was issued to non-existent
2383 * device. No need to reset. Just
2384 * thaw and kill the device.
2385 */
2386 ata_eh_thaw_port(ap);
2387 dev->class = ATA_DEV_UNKNOWN;
Tejun Heo084fe632006-05-31 18:28:03 +09002388 break;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002389 default:
2390 dev->class = ATA_DEV_UNKNOWN;
2391 goto err;
Tejun Heo55a8e2c2006-11-10 18:08:10 +09002392 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002393 }
2394 }
2395
Tejun Heoc1c4e8d2007-04-23 02:05:53 +09002396 /* PDIAG- should have been released, ask cable type if post-reset */
Tejun Heo9b1e2652007-08-06 18:36:24 +09002397 if (ata_is_host_link(link) && ap->ops->cable_detect &&
2398 (ehc->i.flags & ATA_EHI_DID_RESET))
Tejun Heoc1c4e8d2007-04-23 02:05:53 +09002399 ap->cbl = ap->ops->cable_detect(ap);
2400
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002401 /* Configure new devices forward such that user doesn't see
2402 * device detection messages backwards.
2403 */
Tejun Heo02607312007-08-06 18:36:23 +09002404 ata_link_for_each_dev(dev, link) {
Tejun Heo633273a2007-09-23 13:19:54 +09002405 if (!(new_mask & (1 << dev->devno)) ||
2406 dev->class == ATA_DEV_PMP)
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002407 continue;
2408
2409 ehc->i.flags |= ATA_EHI_PRINTINFO;
2410 rc = ata_dev_configure(dev);
2411 ehc->i.flags &= ~ATA_EHI_PRINTINFO;
2412 if (rc)
2413 goto err;
2414
2415 spin_lock_irqsave(ap->lock, flags);
2416 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
2417 spin_unlock_irqrestore(ap->lock, flags);
2418
2419 /* new device discovered, configure xfermode */
2420 ehc->i.flags |= ATA_EHI_SETMODE;
2421 }
2422
2423 return 0;
2424
2425 err:
2426 *r_failed_dev = dev;
2427 DPRINTK("EXIT rc=%d\n", rc);
Tejun Heo022bdb02006-05-15 20:58:22 +09002428 return rc;
2429}
2430
Tejun Heo6f1d1e32007-11-27 19:28:55 +09002431/**
2432 * ata_set_mode - Program timings and issue SET FEATURES - XFER
2433 * @link: link on which timings will be programmed
2434 * @r_failed_dev: out paramter for failed device
2435 *
2436 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
2437 * ata_set_mode() fails, pointer to the failing device is
2438 * returned in @r_failed_dev.
2439 *
2440 * LOCKING:
2441 * PCI/etc. bus probe sem.
2442 *
2443 * RETURNS:
2444 * 0 on success, negative errno otherwise
2445 */
2446int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
2447{
2448 struct ata_port *ap = link->ap;
Tejun Heo00115e02007-11-27 19:28:58 +09002449 struct ata_device *dev;
2450 int rc;
Tejun Heo6f1d1e32007-11-27 19:28:55 +09002451
Tejun Heo76326ac2007-11-27 19:28:59 +09002452 /* if data transfer is verified, clear DUBIOUS_XFER on ering top */
2453 ata_link_for_each_dev(dev, link) {
2454 if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
2455 struct ata_ering_entry *ent;
2456
2457 ent = ata_ering_top(&dev->ering);
2458 if (ent)
2459 ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER;
2460 }
2461 }
2462
Tejun Heo6f1d1e32007-11-27 19:28:55 +09002463 /* has private set_mode? */
2464 if (ap->ops->set_mode)
Tejun Heo00115e02007-11-27 19:28:58 +09002465 rc = ap->ops->set_mode(link, r_failed_dev);
2466 else
2467 rc = ata_do_set_mode(link, r_failed_dev);
2468
2469 /* if transfer mode has changed, set DUBIOUS_XFER on device */
2470 ata_link_for_each_dev(dev, link) {
2471 struct ata_eh_context *ehc = &link->eh_context;
2472 u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
2473 u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
2474
2475 if (dev->xfer_mode != saved_xfer_mode ||
2476 ata_ncq_enabled(dev) != saved_ncq)
2477 dev->flags |= ATA_DFLAG_DUBIOUS_XFER;
2478 }
2479
2480 return rc;
Tejun Heo6f1d1e32007-11-27 19:28:55 +09002481}
2482
Tejun Heo02607312007-08-06 18:36:23 +09002483static int ata_link_nr_enabled(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09002484{
Tejun Heof58229f2007-08-06 18:36:23 +09002485 struct ata_device *dev;
2486 int cnt = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09002487
Tejun Heo02607312007-08-06 18:36:23 +09002488 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09002489 if (ata_dev_enabled(dev))
Tejun Heo022bdb02006-05-15 20:58:22 +09002490 cnt++;
2491 return cnt;
2492}
2493
Tejun Heo02607312007-08-06 18:36:23 +09002494static int ata_link_nr_vacant(struct ata_link *link)
Tejun Heo084fe632006-05-31 18:28:03 +09002495{
Tejun Heof58229f2007-08-06 18:36:23 +09002496 struct ata_device *dev;
2497 int cnt = 0;
Tejun Heo084fe632006-05-31 18:28:03 +09002498
Tejun Heo02607312007-08-06 18:36:23 +09002499 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09002500 if (dev->class == ATA_DEV_UNKNOWN)
Tejun Heo084fe632006-05-31 18:28:03 +09002501 cnt++;
2502 return cnt;
2503}
2504
Tejun Heo02607312007-08-06 18:36:23 +09002505static int ata_eh_skip_recovery(struct ata_link *link)
Tejun Heo084fe632006-05-31 18:28:03 +09002506{
Tejun Heo02607312007-08-06 18:36:23 +09002507 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heof58229f2007-08-06 18:36:23 +09002508 struct ata_device *dev;
Tejun Heo084fe632006-05-31 18:28:03 +09002509
Tejun Heof9df58c2007-09-23 13:14:13 +09002510 /* skip disabled links */
2511 if (link->flags & ATA_LFLAG_DISABLED)
2512 return 1;
2513
Tejun Heo7c8c2cf2006-07-10 23:18:23 +09002514 /* thaw frozen port, resume link and recover failed devices */
Tejun Heo02607312007-08-06 18:36:23 +09002515 if ((link->ap->pflags & ATA_PFLAG_FROZEN) ||
2516 (ehc->i.flags & ATA_EHI_RESUME_LINK) || ata_link_nr_enabled(link))
Tejun Heo084fe632006-05-31 18:28:03 +09002517 return 0;
2518
2519 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
Tejun Heo02607312007-08-06 18:36:23 +09002520 ata_link_for_each_dev(dev, link) {
Tejun Heo084fe632006-05-31 18:28:03 +09002521 if (dev->class == ATA_DEV_UNKNOWN &&
2522 ehc->classes[dev->devno] != ATA_DEV_NONE)
2523 return 0;
2524 }
2525
2526 return 1;
2527}
2528
Tejun Heo02c05a22007-11-27 19:28:54 +09002529static int ata_eh_schedule_probe(struct ata_device *dev)
2530{
2531 struct ata_eh_context *ehc = &dev->link->eh_context;
2532
2533 if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
2534 (ehc->did_probe_mask & (1 << dev->devno)))
2535 return 0;
2536
2537 ata_eh_detach_dev(dev);
2538 ata_dev_init(dev);
2539 ehc->did_probe_mask |= (1 << dev->devno);
2540 ehc->i.action |= ATA_EH_SOFTRESET;
Tejun Heo00115e02007-11-27 19:28:58 +09002541 ehc->saved_xfer_mode[dev->devno] = 0;
2542 ehc->saved_ncq_enabled &= ~(1 << dev->devno);
Tejun Heo02c05a22007-11-27 19:28:54 +09002543
2544 return 1;
2545}
2546
Tejun Heo9b1e2652007-08-06 18:36:24 +09002547static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
Tejun Heofee7ca72007-07-01 19:05:58 +09002548{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002549 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heofee7ca72007-07-01 19:05:58 +09002550
2551 ehc->tries[dev->devno]--;
2552
2553 switch (err) {
2554 case -ENODEV:
2555 /* device missing or wrong IDENTIFY data, schedule probing */
2556 ehc->i.probe_mask |= (1 << dev->devno);
2557 case -EINVAL:
2558 /* give it just one more chance */
2559 ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
2560 case -EIO:
Tejun Heo4fb46152007-10-29 16:45:05 +09002561 if (ehc->tries[dev->devno] == 1 && dev->pio_mode > XFER_PIO_0) {
Tejun Heofee7ca72007-07-01 19:05:58 +09002562 /* This is the last chance, better to slow
2563 * down than lose it.
2564 */
Tejun Heo936fd732007-08-06 18:36:23 +09002565 sata_down_spd_limit(dev->link);
Tejun Heofee7ca72007-07-01 19:05:58 +09002566 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2567 }
2568 }
2569
2570 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2571 /* disable device if it has used up all its chances */
2572 ata_dev_disable(dev);
2573
2574 /* detach if offline */
Tejun Heo936fd732007-08-06 18:36:23 +09002575 if (ata_link_offline(dev->link))
Tejun Heofee7ca72007-07-01 19:05:58 +09002576 ata_eh_detach_dev(dev);
2577
Tejun Heo02c05a22007-11-27 19:28:54 +09002578 /* schedule probe if necessary */
2579 if (ata_eh_schedule_probe(dev))
Tejun Heofee7ca72007-07-01 19:05:58 +09002580 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
Tejun Heo9b1e2652007-08-06 18:36:24 +09002581
2582 return 1;
Tejun Heofee7ca72007-07-01 19:05:58 +09002583 } else {
2584 /* soft didn't work? be haaaaard */
2585 if (ehc->i.flags & ATA_EHI_DID_RESET)
2586 ehc->i.action |= ATA_EH_HARDRESET;
2587 else
2588 ehc->i.action |= ATA_EH_SOFTRESET;
Tejun Heo9b1e2652007-08-06 18:36:24 +09002589
2590 return 0;
Tejun Heofee7ca72007-07-01 19:05:58 +09002591 }
2592}
2593
Tejun Heo022bdb02006-05-15 20:58:22 +09002594/**
2595 * ata_eh_recover - recover host port after error
2596 * @ap: host port to recover
Tejun Heof5914a42006-05-31 18:27:48 +09002597 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09002598 * @softreset: softreset method (can be NULL)
2599 * @hardreset: hardreset method (can be NULL)
2600 * @postreset: postreset method (can be NULL)
Tejun Heo9b1e2652007-08-06 18:36:24 +09002601 * @r_failed_link: out parameter for failed link
Tejun Heo022bdb02006-05-15 20:58:22 +09002602 *
2603 * This is the alpha and omega, eum and yang, heart and soul of
2604 * libata exception handling. On entry, actions required to
Tejun Heo9b1e2652007-08-06 18:36:24 +09002605 * recover each link and hotplug requests are recorded in the
2606 * link's eh_context. This function executes all the operations
2607 * with appropriate retrials and fallbacks to resurrect failed
Tejun Heo084fe632006-05-31 18:28:03 +09002608 * devices, detach goners and greet newcomers.
Tejun Heo022bdb02006-05-15 20:58:22 +09002609 *
2610 * LOCKING:
2611 * Kernel thread context (may sleep).
2612 *
2613 * RETURNS:
2614 * 0 on success, -errno on failure.
2615 */
Tejun Heofb7fd612007-09-23 13:14:12 +09002616int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
2617 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2618 ata_postreset_fn_t postreset,
2619 struct ata_link **r_failed_link)
Tejun Heo022bdb02006-05-15 20:58:22 +09002620{
Tejun Heo9b1e2652007-08-06 18:36:24 +09002621 struct ata_link *link;
Tejun Heo022bdb02006-05-15 20:58:22 +09002622 struct ata_device *dev;
Tejun Heo9b1e2652007-08-06 18:36:24 +09002623 int nr_failed_devs, nr_disabled_devs;
2624 int reset, rc;
Tejun Heof9df58c2007-09-23 13:14:13 +09002625 unsigned long flags;
Tejun Heo022bdb02006-05-15 20:58:22 +09002626
2627 DPRINTK("ENTER\n");
2628
2629 /* prep for recovery */
Tejun Heo9b1e2652007-08-06 18:36:24 +09002630 ata_port_for_each_link(link, ap) {
2631 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo084fe632006-05-31 18:28:03 +09002632
Tejun Heof9df58c2007-09-23 13:14:13 +09002633 /* re-enable link? */
2634 if (ehc->i.action & ATA_EH_ENABLE_LINK) {
2635 ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
2636 spin_lock_irqsave(ap->lock, flags);
2637 link->flags &= ~ATA_LFLAG_DISABLED;
2638 spin_unlock_irqrestore(ap->lock, flags);
2639 ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
2640 }
2641
Tejun Heo9b1e2652007-08-06 18:36:24 +09002642 ata_link_for_each_dev(dev, link) {
Tejun Heofd995f72007-09-23 13:14:12 +09002643 if (link->flags & ATA_LFLAG_NO_RETRY)
2644 ehc->tries[dev->devno] = 1;
2645 else
2646 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
Tejun Heo79a55b72007-01-18 17:22:18 +09002647
Tejun Heo9b1e2652007-08-06 18:36:24 +09002648 /* collect port action mask recorded in dev actions */
2649 ehc->i.action |= ehc->i.dev_action[dev->devno] &
2650 ~ATA_EH_PERDEV_MASK;
2651 ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
Tejun Heo084fe632006-05-31 18:28:03 +09002652
Tejun Heo9b1e2652007-08-06 18:36:24 +09002653 /* process hotplug request */
2654 if (dev->flags & ATA_DFLAG_DETACH)
2655 ata_eh_detach_dev(dev);
2656
Tejun Heo02c05a22007-11-27 19:28:54 +09002657 /* schedule probe if necessary */
2658 if (!ata_dev_enabled(dev))
2659 ata_eh_schedule_probe(dev);
Tejun Heo084fe632006-05-31 18:28:03 +09002660 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002661 }
2662
2663 retry:
Tejun Heo022bdb02006-05-15 20:58:22 +09002664 rc = 0;
Tejun Heo9b1e2652007-08-06 18:36:24 +09002665 nr_failed_devs = 0;
2666 nr_disabled_devs = 0;
2667 reset = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09002668
Tejun Heoaeb2ecd2006-06-12 14:11:43 +09002669 /* if UNLOADING, finish immediately */
Tejun Heob51e9e52006-06-29 01:29:30 +09002670 if (ap->pflags & ATA_PFLAG_UNLOADING)
Tejun Heoaeb2ecd2006-06-12 14:11:43 +09002671 goto out;
2672
Tejun Heo9b1e2652007-08-06 18:36:24 +09002673 /* prep for EH */
2674 ata_port_for_each_link(link, ap) {
2675 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09002676
Tejun Heo9b1e2652007-08-06 18:36:24 +09002677 /* skip EH if possible. */
2678 if (ata_eh_skip_recovery(link))
2679 ehc->i.action = 0;
2680
2681 /* do we need to reset? */
2682 if (ehc->i.action & ATA_EH_RESET_MASK)
2683 reset = 1;
2684
2685 ata_link_for_each_dev(dev, link)
2686 ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
2687 }
Tejun Heo084fe632006-05-31 18:28:03 +09002688
Tejun Heo022bdb02006-05-15 20:58:22 +09002689 /* reset */
Tejun Heo9b1e2652007-08-06 18:36:24 +09002690 if (reset) {
Tejun Heob06ce3e2007-10-09 15:06:48 +09002691 /* if PMP is attached, this function only deals with
2692 * downstream links, port should stay thawed.
2693 */
2694 if (!ap->nr_pmp_links)
2695 ata_eh_freeze_port(ap);
Tejun Heo022bdb02006-05-15 20:58:22 +09002696
Tejun Heo9b1e2652007-08-06 18:36:24 +09002697 ata_port_for_each_link(link, ap) {
2698 struct ata_eh_context *ehc = &link->eh_context;
2699
2700 if (!(ehc->i.action & ATA_EH_RESET_MASK))
2701 continue;
2702
2703 rc = ata_eh_reset(link, ata_link_nr_vacant(link),
2704 prereset, softreset, hardreset,
2705 postreset);
2706 if (rc) {
2707 ata_link_printk(link, KERN_ERR,
2708 "reset failed, giving up\n");
2709 goto out;
2710 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002711 }
2712
Tejun Heob06ce3e2007-10-09 15:06:48 +09002713 if (!ap->nr_pmp_links)
2714 ata_eh_thaw_port(ap);
Tejun Heo022bdb02006-05-15 20:58:22 +09002715 }
2716
Tejun Heo9b1e2652007-08-06 18:36:24 +09002717 /* the rest */
2718 ata_port_for_each_link(link, ap) {
2719 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09002720
Tejun Heo9b1e2652007-08-06 18:36:24 +09002721 /* revalidate existing devices and attach new ones */
2722 rc = ata_eh_revalidate_and_attach(link, &dev);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002723 if (rc)
Tejun Heo022bdb02006-05-15 20:58:22 +09002724 goto dev_fail;
Tejun Heo9b1e2652007-08-06 18:36:24 +09002725
Tejun Heo633273a2007-09-23 13:19:54 +09002726 /* if PMP got attached, return, pmp EH will take care of it */
2727 if (link->device->class == ATA_DEV_PMP) {
2728 ehc->i.action = 0;
2729 return 0;
2730 }
2731
Tejun Heo9b1e2652007-08-06 18:36:24 +09002732 /* configure transfer mode if necessary */
2733 if (ehc->i.flags & ATA_EHI_SETMODE) {
2734 rc = ata_set_mode(link, &dev);
2735 if (rc)
2736 goto dev_fail;
2737 ehc->i.flags &= ~ATA_EHI_SETMODE;
2738 }
2739
Kristen Carlson Accardica773292007-10-25 00:58:59 -04002740 if (ehc->i.action & ATA_EHI_LPM)
2741 ata_link_for_each_dev(dev, link)
2742 ata_dev_enable_pm(dev, ap->pm_policy);
2743
Tejun Heo9b1e2652007-08-06 18:36:24 +09002744 /* this link is okay now */
2745 ehc->i.flags = 0;
2746 continue;
2747
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002748dev_fail:
Tejun Heo9b1e2652007-08-06 18:36:24 +09002749 nr_failed_devs++;
2750 if (ata_eh_handle_dev_fail(dev, rc))
2751 nr_disabled_devs++;
2752
Tejun Heob06ce3e2007-10-09 15:06:48 +09002753 if (ap->pflags & ATA_PFLAG_FROZEN) {
2754 /* PMP reset requires working host port.
2755 * Can't retry if it's frozen.
2756 */
2757 if (ap->nr_pmp_links)
2758 goto out;
Tejun Heo9b1e2652007-08-06 18:36:24 +09002759 break;
Tejun Heob06ce3e2007-10-09 15:06:48 +09002760 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002761 }
2762
Tejun Heo9b1e2652007-08-06 18:36:24 +09002763 if (nr_failed_devs) {
2764 if (nr_failed_devs != nr_disabled_devs) {
2765 ata_port_printk(ap, KERN_WARNING, "failed to recover "
2766 "some devices, retrying in 5 secs\n");
2767 ssleep(5);
2768 } else {
2769 /* no device left to recover, repeat fast */
2770 msleep(500);
2771 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002772
Tejun Heo9b1e2652007-08-06 18:36:24 +09002773 goto retry;
Tejun Heo022bdb02006-05-15 20:58:22 +09002774 }
2775
Tejun Heo022bdb02006-05-15 20:58:22 +09002776 out:
Tejun Heo9b1e2652007-08-06 18:36:24 +09002777 if (rc && r_failed_link)
2778 *r_failed_link = link;
Tejun Heo022bdb02006-05-15 20:58:22 +09002779
2780 DPRINTK("EXIT, rc=%d\n", rc);
2781 return rc;
2782}
2783
2784/**
2785 * ata_eh_finish - finish up EH
2786 * @ap: host port to finish EH for
2787 *
2788 * Recovery is complete. Clean up EH states and retry or finish
2789 * failed qcs.
2790 *
2791 * LOCKING:
2792 * None.
2793 */
Tejun Heofb7fd612007-09-23 13:14:12 +09002794void ata_eh_finish(struct ata_port *ap)
Tejun Heo022bdb02006-05-15 20:58:22 +09002795{
2796 int tag;
2797
2798 /* retry or finish qcs */
2799 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2800 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2801
2802 if (!(qc->flags & ATA_QCFLAG_FAILED))
2803 continue;
2804
2805 if (qc->err_mask) {
2806 /* FIXME: Once EH migration is complete,
2807 * generate sense data in this function,
2808 * considering both err_mask and tf.
Tejun Heof90f0822007-10-26 16:12:41 +09002809 *
2810 * There's no point in retrying invalid
2811 * (detected by libata) and non-IO device
2812 * errors (rejected by device). Finish them
2813 * immediately.
Tejun Heo022bdb02006-05-15 20:58:22 +09002814 */
Tejun Heof90f0822007-10-26 16:12:41 +09002815 if ((qc->err_mask & AC_ERR_INVALID) ||
2816 (!(qc->flags & ATA_QCFLAG_IO) &&
2817 qc->err_mask == AC_ERR_DEV))
Tejun Heo022bdb02006-05-15 20:58:22 +09002818 ata_eh_qc_complete(qc);
2819 else
2820 ata_eh_qc_retry(qc);
2821 } else {
2822 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2823 ata_eh_qc_complete(qc);
2824 } else {
2825 /* feed zero TF to sense generation */
2826 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2827 ata_eh_qc_retry(qc);
2828 }
2829 }
2830 }
Tejun Heoda917d62007-09-23 13:14:12 +09002831
2832 /* make sure nr_active_links is zero after EH */
2833 WARN_ON(ap->nr_active_links);
2834 ap->nr_active_links = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09002835}
2836
2837/**
2838 * ata_do_eh - do standard error handling
2839 * @ap: host port to handle error for
Tejun Heof5914a42006-05-31 18:27:48 +09002840 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09002841 * @softreset: softreset method (can be NULL)
2842 * @hardreset: hardreset method (can be NULL)
2843 * @postreset: postreset method (can be NULL)
2844 *
2845 * Perform standard error handling sequence.
2846 *
2847 * LOCKING:
2848 * Kernel thread context (may sleep).
2849 */
Tejun Heof5914a42006-05-31 18:27:48 +09002850void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2851 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2852 ata_postreset_fn_t postreset)
Tejun Heo022bdb02006-05-15 20:58:22 +09002853{
Tejun Heo9b1e2652007-08-06 18:36:24 +09002854 struct ata_device *dev;
2855 int rc;
2856
2857 ata_eh_autopsy(ap);
2858 ata_eh_report(ap);
2859
2860 rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
2861 NULL);
2862 if (rc) {
2863 ata_link_for_each_dev(dev, &ap->link)
2864 ata_dev_disable(dev);
2865 }
2866
Tejun Heo022bdb02006-05-15 20:58:22 +09002867 ata_eh_finish(ap);
2868}
Tejun Heo500530f2006-07-03 16:07:27 +09002869
Tejun Heo6ffa01d2007-03-02 17:32:47 +09002870#ifdef CONFIG_PM
Tejun Heo500530f2006-07-03 16:07:27 +09002871/**
2872 * ata_eh_handle_port_suspend - perform port suspend operation
2873 * @ap: port to suspend
2874 *
2875 * Suspend @ap.
2876 *
2877 * LOCKING:
2878 * Kernel thread context (may sleep).
2879 */
2880static void ata_eh_handle_port_suspend(struct ata_port *ap)
2881{
2882 unsigned long flags;
2883 int rc = 0;
2884
2885 /* are we suspending? */
2886 spin_lock_irqsave(ap->lock, flags);
2887 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2888 ap->pm_mesg.event == PM_EVENT_ON) {
2889 spin_unlock_irqrestore(ap->lock, flags);
2890 return;
2891 }
2892 spin_unlock_irqrestore(ap->lock, flags);
2893
2894 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
2895
Tejun Heo64578a32007-05-15 03:28:16 +09002896 /* tell ACPI we're suspending */
2897 rc = ata_acpi_on_suspend(ap);
2898 if (rc)
2899 goto out;
2900
Tejun Heo500530f2006-07-03 16:07:27 +09002901 /* suspend */
2902 ata_eh_freeze_port(ap);
2903
2904 if (ap->ops->port_suspend)
2905 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
2906
Shaohua Libd3adca2007-11-02 09:32:38 +08002907 ata_acpi_set_state(ap, PMSG_SUSPEND);
Tejun Heo64578a32007-05-15 03:28:16 +09002908 out:
Tejun Heo500530f2006-07-03 16:07:27 +09002909 /* report result */
2910 spin_lock_irqsave(ap->lock, flags);
2911
2912 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
2913 if (rc == 0)
2914 ap->pflags |= ATA_PFLAG_SUSPENDED;
Tejun Heo64578a32007-05-15 03:28:16 +09002915 else if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heo500530f2006-07-03 16:07:27 +09002916 ata_port_schedule_eh(ap);
2917
2918 if (ap->pm_result) {
2919 *ap->pm_result = rc;
2920 ap->pm_result = NULL;
2921 }
2922
2923 spin_unlock_irqrestore(ap->lock, flags);
2924
2925 return;
2926}
2927
2928/**
2929 * ata_eh_handle_port_resume - perform port resume operation
2930 * @ap: port to resume
2931 *
2932 * Resume @ap.
2933 *
Tejun Heo500530f2006-07-03 16:07:27 +09002934 * LOCKING:
2935 * Kernel thread context (may sleep).
2936 */
2937static void ata_eh_handle_port_resume(struct ata_port *ap)
2938{
Tejun Heo500530f2006-07-03 16:07:27 +09002939 unsigned long flags;
Tejun Heo9666f402007-05-04 21:27:47 +02002940 int rc = 0;
Tejun Heo500530f2006-07-03 16:07:27 +09002941
2942 /* are we resuming? */
2943 spin_lock_irqsave(ap->lock, flags);
2944 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2945 ap->pm_mesg.event != PM_EVENT_ON) {
2946 spin_unlock_irqrestore(ap->lock, flags);
2947 return;
2948 }
2949 spin_unlock_irqrestore(ap->lock, flags);
2950
Tejun Heo9666f402007-05-04 21:27:47 +02002951 WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
Tejun Heo500530f2006-07-03 16:07:27 +09002952
Shaohua Libd3adca2007-11-02 09:32:38 +08002953 ata_acpi_set_state(ap, PMSG_ON);
2954
Tejun Heo500530f2006-07-03 16:07:27 +09002955 if (ap->ops->port_resume)
2956 rc = ap->ops->port_resume(ap);
2957
Tejun Heo67465442007-05-15 03:28:16 +09002958 /* tell ACPI that we're resuming */
2959 ata_acpi_on_resume(ap);
2960
Tejun Heo9666f402007-05-04 21:27:47 +02002961 /* report result */
Tejun Heo500530f2006-07-03 16:07:27 +09002962 spin_lock_irqsave(ap->lock, flags);
2963 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
2964 if (ap->pm_result) {
2965 *ap->pm_result = rc;
2966 ap->pm_result = NULL;
2967 }
2968 spin_unlock_irqrestore(ap->lock, flags);
2969}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09002970#endif /* CONFIG_PM */