blob: 99037a4860d9c38858c4805cfdf71ad54a0b1b21 [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 Heo75f9caf2008-01-03 01:21:14 +090064 ATA_ECAT_DUBIOUS_NONE = 4,
65 ATA_ECAT_DUBIOUS_ATA_BUS = 5,
66 ATA_ECAT_DUBIOUS_TOUT_HSM = 6,
67 ATA_ECAT_DUBIOUS_UNK_DEV = 7,
68 ATA_ECAT_NR = 8,
Tejun Heo7d47e8d2007-02-02 16:22:31 +090069
Tejun Heo87fbc5a2008-05-20 02:17:54 +090070 ATA_EH_CMD_DFL_TIMEOUT = 5000,
71
Tejun Heo0a2c0f52008-05-20 02:17:52 +090072 /* always put at least this amount of time between resets */
73 ATA_EH_RESET_COOL_DOWN = 5000,
74
Tejun Heo341c2c92008-05-20 02:17:51 +090075 /* Waiting in ->prereset can never be reliable. It's
76 * sometimes nice to wait there but it can't be depended upon;
77 * otherwise, we wouldn't be resetting. Just give it enough
78 * time for most drives to spin up.
79 */
80 ATA_EH_PRERESET_TIMEOUT = 10000,
81 ATA_EH_FASTDRAIN_INTERVAL = 3000,
Tejun Heo31daabd2007-02-02 16:50:52 +090082};
83
84/* The following table determines how we sequence resets. Each entry
85 * represents timeout for that try. The first try can be soft or
86 * hardreset. All others are hardreset if available. In most cases
87 * the first reset w/ 10sec timeout should succeed. Following entries
88 * are mostly for error handling, hotplug and retarded devices.
89 */
90static const unsigned long ata_eh_reset_timeouts[] = {
Tejun Heo341c2c92008-05-20 02:17:51 +090091 10000, /* most drives spin up by 10sec */
92 10000, /* > 99% working drives spin up before 20sec */
93 35000, /* give > 30 secs of idleness for retarded devices */
94 5000, /* and sweet one last chance */
Tejun Heod8af0eb2008-05-20 02:17:53 +090095 ULONG_MAX, /* > 1 min has elapsed, give up */
Tejun Heo31daabd2007-02-02 16:50:52 +090096};
97
Tejun Heo87fbc5a2008-05-20 02:17:54 +090098static const unsigned long ata_eh_identify_timeouts[] = {
99 5000, /* covers > 99% of successes and not too boring on failures */
100 10000, /* combined time till here is enough even for media access */
101 30000, /* for true idiots */
102 ULONG_MAX,
103};
104
105static const unsigned long ata_eh_other_timeouts[] = {
106 5000, /* same rationale as identify timeout */
107 10000, /* ditto */
108 /* but no merciful 30sec for other commands, it just isn't worth it */
109 ULONG_MAX,
110};
111
112struct ata_eh_cmd_timeout_ent {
113 const u8 *commands;
114 const unsigned long *timeouts;
115};
116
117/* The following table determines timeouts to use for EH internal
118 * commands. Each table entry is a command class and matches the
119 * commands the entry applies to and the timeout table to use.
120 *
121 * On the retry after a command timed out, the next timeout value from
122 * the table is used. If the table doesn't contain further entries,
123 * the last value is used.
124 *
125 * ehc->cmd_timeout_idx keeps track of which timeout to use per
126 * command class, so if SET_FEATURES times out on the first try, the
127 * next try will use the second timeout value only for that class.
128 */
129#define CMDS(cmds...) (const u8 []){ cmds, 0 }
130static const struct ata_eh_cmd_timeout_ent
131ata_eh_cmd_timeout_table[ATA_EH_CMD_TIMEOUT_TABLE_SIZE] = {
132 { .commands = CMDS(ATA_CMD_ID_ATA, ATA_CMD_ID_ATAPI),
133 .timeouts = ata_eh_identify_timeouts, },
134 { .commands = CMDS(ATA_CMD_READ_NATIVE_MAX, ATA_CMD_READ_NATIVE_MAX_EXT),
135 .timeouts = ata_eh_other_timeouts, },
136 { .commands = CMDS(ATA_CMD_SET_MAX, ATA_CMD_SET_MAX_EXT),
137 .timeouts = ata_eh_other_timeouts, },
138 { .commands = CMDS(ATA_CMD_SET_FEATURES),
139 .timeouts = ata_eh_other_timeouts, },
140 { .commands = CMDS(ATA_CMD_INIT_DEV_PARAMS),
141 .timeouts = ata_eh_other_timeouts, },
142};
143#undef CMDS
144
Tejun Heoad9e2762006-05-15 20:58:12 +0900145static void __ata_port_freeze(struct ata_port *ap);
Tejun Heo6ffa01d2007-03-02 17:32:47 +0900146#ifdef CONFIG_PM
Tejun Heo500530f2006-07-03 16:07:27 +0900147static void ata_eh_handle_port_suspend(struct ata_port *ap);
148static void ata_eh_handle_port_resume(struct ata_port *ap);
Tejun Heo6ffa01d2007-03-02 17:32:47 +0900149#else /* CONFIG_PM */
150static void ata_eh_handle_port_suspend(struct ata_port *ap)
151{ }
152
153static void ata_eh_handle_port_resume(struct ata_port *ap)
154{ }
Tejun Heo6ffa01d2007-03-02 17:32:47 +0900155#endif /* CONFIG_PM */
Tejun Heoad9e2762006-05-15 20:58:12 +0900156
Tejun Heob64bbc32007-07-16 14:29:39 +0900157static void __ata_ehi_pushv_desc(struct ata_eh_info *ehi, const char *fmt,
158 va_list args)
159{
160 ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
161 ATA_EH_DESC_LEN - ehi->desc_len,
162 fmt, args);
163}
164
165/**
166 * __ata_ehi_push_desc - push error description without adding separator
167 * @ehi: target EHI
168 * @fmt: printf format string
169 *
170 * Format string according to @fmt and append it to @ehi->desc.
171 *
172 * LOCKING:
173 * spin_lock_irqsave(host lock)
174 */
175void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
176{
177 va_list args;
178
179 va_start(args, fmt);
180 __ata_ehi_pushv_desc(ehi, fmt, args);
181 va_end(args);
182}
183
184/**
185 * ata_ehi_push_desc - push error description with separator
186 * @ehi: target EHI
187 * @fmt: printf format string
188 *
189 * Format string according to @fmt and append it to @ehi->desc.
190 * If @ehi->desc is not empty, ", " is added in-between.
191 *
192 * LOCKING:
193 * spin_lock_irqsave(host lock)
194 */
195void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
196{
197 va_list args;
198
199 if (ehi->desc_len)
200 __ata_ehi_push_desc(ehi, ", ");
201
202 va_start(args, fmt);
203 __ata_ehi_pushv_desc(ehi, fmt, args);
204 va_end(args);
205}
206
207/**
208 * ata_ehi_clear_desc - clean error description
209 * @ehi: target EHI
210 *
211 * Clear @ehi->desc.
212 *
213 * LOCKING:
214 * spin_lock_irqsave(host lock)
215 */
216void ata_ehi_clear_desc(struct ata_eh_info *ehi)
217{
218 ehi->desc[0] = '\0';
219 ehi->desc_len = 0;
220}
221
Tejun Heocbcdd872007-08-18 13:14:55 +0900222/**
223 * ata_port_desc - append port description
224 * @ap: target ATA port
225 * @fmt: printf format string
226 *
227 * Format string according to @fmt and append it to port
228 * description. If port description is not empty, " " is added
229 * in-between. This function is to be used while initializing
230 * ata_host. The description is printed on host registration.
231 *
232 * LOCKING:
233 * None.
234 */
235void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
236{
237 va_list args;
238
239 WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
240
241 if (ap->link.eh_info.desc_len)
242 __ata_ehi_push_desc(&ap->link.eh_info, " ");
243
244 va_start(args, fmt);
245 __ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
246 va_end(args);
247}
248
249#ifdef CONFIG_PCI
250
251/**
252 * ata_port_pbar_desc - append PCI BAR description
253 * @ap: target ATA port
254 * @bar: target PCI BAR
255 * @offset: offset into PCI BAR
256 * @name: name of the area
257 *
258 * If @offset is negative, this function formats a string which
259 * contains the name, address, size and type of the BAR and
260 * appends it to the port description. If @offset is zero or
261 * positive, only name and offsetted address is appended.
262 *
263 * LOCKING:
264 * None.
265 */
266void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
267 const char *name)
268{
269 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
270 char *type = "";
271 unsigned long long start, len;
272
273 if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
274 type = "m";
275 else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
276 type = "i";
277
278 start = (unsigned long long)pci_resource_start(pdev, bar);
279 len = (unsigned long long)pci_resource_len(pdev, bar);
280
281 if (offset < 0)
282 ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
283 else
Andrew Mortone6a73ab2007-12-13 16:01:37 -0800284 ata_port_desc(ap, "%s 0x%llx", name,
285 start + (unsigned long long)offset);
Tejun Heocbcdd872007-08-18 13:14:55 +0900286}
287
288#endif /* CONFIG_PCI */
289
Tejun Heo87fbc5a2008-05-20 02:17:54 +0900290static int ata_lookup_timeout_table(u8 cmd)
291{
292 int i;
293
294 for (i = 0; i < ATA_EH_CMD_TIMEOUT_TABLE_SIZE; i++) {
295 const u8 *cur;
296
297 for (cur = ata_eh_cmd_timeout_table[i].commands; *cur; cur++)
298 if (*cur == cmd)
299 return i;
300 }
301
302 return -1;
303}
304
305/**
306 * ata_internal_cmd_timeout - determine timeout for an internal command
307 * @dev: target device
308 * @cmd: internal command to be issued
309 *
310 * Determine timeout for internal command @cmd for @dev.
311 *
312 * LOCKING:
313 * EH context.
314 *
315 * RETURNS:
316 * Determined timeout.
317 */
318unsigned long ata_internal_cmd_timeout(struct ata_device *dev, u8 cmd)
319{
320 struct ata_eh_context *ehc = &dev->link->eh_context;
321 int ent = ata_lookup_timeout_table(cmd);
322 int idx;
323
324 if (ent < 0)
325 return ATA_EH_CMD_DFL_TIMEOUT;
326
327 idx = ehc->cmd_timeout_idx[dev->devno][ent];
328 return ata_eh_cmd_timeout_table[ent].timeouts[idx];
329}
330
331/**
332 * ata_internal_cmd_timed_out - notification for internal command timeout
333 * @dev: target device
334 * @cmd: internal command which timed out
335 *
336 * Notify EH that internal command @cmd for @dev timed out. This
337 * function should be called only for commands whose timeouts are
338 * determined using ata_internal_cmd_timeout().
339 *
340 * LOCKING:
341 * EH context.
342 */
343void ata_internal_cmd_timed_out(struct ata_device *dev, u8 cmd)
344{
345 struct ata_eh_context *ehc = &dev->link->eh_context;
346 int ent = ata_lookup_timeout_table(cmd);
347 int idx;
348
349 if (ent < 0)
350 return;
351
352 idx = ehc->cmd_timeout_idx[dev->devno][ent];
353 if (ata_eh_cmd_timeout_table[ent].timeouts[idx + 1] != ULONG_MAX)
354 ehc->cmd_timeout_idx[dev->devno][ent]++;
355}
356
Tejun Heo3884f7b2007-11-27 19:28:56 +0900357static void ata_ering_record(struct ata_ering *ering, unsigned int eflags,
Tejun Heo0c247c52006-05-15 20:58:19 +0900358 unsigned int err_mask)
359{
360 struct ata_ering_entry *ent;
361
362 WARN_ON(!err_mask);
363
364 ering->cursor++;
365 ering->cursor %= ATA_ERING_SIZE;
366
367 ent = &ering->ring[ering->cursor];
Tejun Heo3884f7b2007-11-27 19:28:56 +0900368 ent->eflags = eflags;
Tejun Heo0c247c52006-05-15 20:58:19 +0900369 ent->err_mask = err_mask;
370 ent->timestamp = get_jiffies_64();
371}
372
Tejun Heo76326ac2007-11-27 19:28:59 +0900373static struct ata_ering_entry *ata_ering_top(struct ata_ering *ering)
374{
375 struct ata_ering_entry *ent = &ering->ring[ering->cursor];
376
377 if (ent->err_mask)
378 return ent;
379 return NULL;
380}
381
Tejun Heo7d47e8d2007-02-02 16:22:31 +0900382static void ata_ering_clear(struct ata_ering *ering)
Tejun Heo0c247c52006-05-15 20:58:19 +0900383{
Tejun Heo7d47e8d2007-02-02 16:22:31 +0900384 memset(ering, 0, sizeof(*ering));
Tejun Heo0c247c52006-05-15 20:58:19 +0900385}
386
387static int ata_ering_map(struct ata_ering *ering,
388 int (*map_fn)(struct ata_ering_entry *, void *),
389 void *arg)
390{
391 int idx, rc = 0;
392 struct ata_ering_entry *ent;
393
394 idx = ering->cursor;
395 do {
396 ent = &ering->ring[idx];
397 if (!ent->err_mask)
398 break;
399 rc = map_fn(ent, arg);
400 if (rc)
401 break;
402 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
403 } while (idx != ering->cursor);
404
405 return rc;
406}
407
Tejun Heo64f65ca2006-06-24 20:30:18 +0900408static unsigned int ata_eh_dev_action(struct ata_device *dev)
409{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900410 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heo64f65ca2006-06-24 20:30:18 +0900411
412 return ehc->i.action | ehc->i.dev_action[dev->devno];
413}
414
Tejun Heof58229f2007-08-06 18:36:23 +0900415static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
Tejun Heoaf181c22006-06-24 20:30:18 +0900416 struct ata_eh_info *ehi, unsigned int action)
417{
Tejun Heof58229f2007-08-06 18:36:23 +0900418 struct ata_device *tdev;
Tejun Heoaf181c22006-06-24 20:30:18 +0900419
420 if (!dev) {
421 ehi->action &= ~action;
Tejun Heof58229f2007-08-06 18:36:23 +0900422 ata_link_for_each_dev(tdev, link)
423 ehi->dev_action[tdev->devno] &= ~action;
Tejun Heoaf181c22006-06-24 20:30:18 +0900424 } else {
425 /* doesn't make sense for port-wide EH actions */
426 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
427
428 /* break ehi->action into ehi->dev_action */
429 if (ehi->action & action) {
Tejun Heof58229f2007-08-06 18:36:23 +0900430 ata_link_for_each_dev(tdev, link)
431 ehi->dev_action[tdev->devno] |=
432 ehi->action & action;
Tejun Heoaf181c22006-06-24 20:30:18 +0900433 ehi->action &= ~action;
434 }
435
436 /* turn off the specified per-dev action */
437 ehi->dev_action[dev->devno] &= ~action;
438 }
439}
440
Tejun Heoece1d632006-04-02 18:51:53 +0900441/**
442 * ata_scsi_timed_out - SCSI layer time out callback
443 * @cmd: timed out SCSI command
444 *
445 * Handles SCSI layer timeout. We race with normal completion of
446 * the qc for @cmd. If the qc is already gone, we lose and let
447 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
448 * timed out and EH should be invoked. Prevent ata_qc_complete()
449 * from finishing it by setting EH_SCHEDULED and return
450 * EH_NOT_HANDLED.
451 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900452 * TODO: kill this function once old EH is gone.
453 *
Tejun Heoece1d632006-04-02 18:51:53 +0900454 * LOCKING:
455 * Called from timer context
456 *
457 * RETURNS:
458 * EH_HANDLED or EH_NOT_HANDLED
459 */
460enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
461{
462 struct Scsi_Host *host = cmd->device->host;
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400463 struct ata_port *ap = ata_shost_to_port(host);
Tejun Heoece1d632006-04-02 18:51:53 +0900464 unsigned long flags;
465 struct ata_queued_cmd *qc;
Tejun Heoad9e2762006-05-15 20:58:12 +0900466 enum scsi_eh_timer_return ret;
Tejun Heoece1d632006-04-02 18:51:53 +0900467
468 DPRINTK("ENTER\n");
469
Tejun Heoad9e2762006-05-15 20:58:12 +0900470 if (ap->ops->error_handler) {
471 ret = EH_NOT_HANDLED;
472 goto out;
473 }
474
475 ret = EH_HANDLED;
Jeff Garzikba6a1302006-06-22 23:46:10 -0400476 spin_lock_irqsave(ap->lock, flags);
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900477 qc = ata_qc_from_tag(ap, ap->link.active_tag);
Tejun Heoece1d632006-04-02 18:51:53 +0900478 if (qc) {
479 WARN_ON(qc->scsicmd != cmd);
480 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
481 qc->err_mask |= AC_ERR_TIMEOUT;
482 ret = EH_NOT_HANDLED;
483 }
Jeff Garzikba6a1302006-06-22 23:46:10 -0400484 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900485
Tejun Heoad9e2762006-05-15 20:58:12 +0900486 out:
Tejun Heoece1d632006-04-02 18:51:53 +0900487 DPRINTK("EXIT, ret=%d\n", ret);
488 return ret;
489}
490
491/**
492 * ata_scsi_error - SCSI layer error handler callback
493 * @host: SCSI host on which error occurred
494 *
495 * Handles SCSI-layer-thrown error events.
496 *
497 * LOCKING:
498 * Inherited from SCSI layer (none, can sleep)
499 *
500 * RETURNS:
501 * Zero.
502 */
Jeff Garzik381544b2006-04-11 13:04:39 -0400503void ata_scsi_error(struct Scsi_Host *host)
Tejun Heoece1d632006-04-02 18:51:53 +0900504{
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400505 struct ata_port *ap = ata_shost_to_port(host);
Tejun Heoa1e10f72007-08-18 13:28:49 +0900506 int i;
Tejun Heoad9e2762006-05-15 20:58:12 +0900507 unsigned long flags;
Tejun Heoece1d632006-04-02 18:51:53 +0900508
509 DPRINTK("ENTER\n");
510
Tejun Heoad9e2762006-05-15 20:58:12 +0900511 /* synchronize with port task */
Tejun Heoece1d632006-04-02 18:51:53 +0900512 ata_port_flush_task(ap);
513
Jeff Garzikcca39742006-08-24 03:19:22 -0400514 /* synchronize with host lock and sort out timeouts */
Tejun Heoece1d632006-04-02 18:51:53 +0900515
Tejun Heoad9e2762006-05-15 20:58:12 +0900516 /* For new EH, all qcs are finished in one of three ways -
517 * normal completion, error completion, and SCSI timeout.
518 * Both cmpletions can race against SCSI timeout. When normal
519 * completion wins, the qc never reaches EH. When error
520 * completion wins, the qc has ATA_QCFLAG_FAILED set.
521 *
522 * When SCSI timeout wins, things are a bit more complex.
523 * Normal or error completion can occur after the timeout but
524 * before this point. In such cases, both types of
525 * completions are honored. A scmd is determined to have
526 * timed out iff its associated qc is active and not failed.
527 */
528 if (ap->ops->error_handler) {
529 struct scsi_cmnd *scmd, *tmp;
530 int nr_timedout = 0;
Tejun Heoece1d632006-04-02 18:51:53 +0900531
Tejun Heoe30349d2006-07-03 03:02:15 +0900532 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900533
534 list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
535 struct ata_queued_cmd *qc;
536
537 for (i = 0; i < ATA_MAX_QUEUE; i++) {
538 qc = __ata_qc_from_tag(ap, i);
539 if (qc->flags & ATA_QCFLAG_ACTIVE &&
540 qc->scsicmd == scmd)
541 break;
542 }
543
544 if (i < ATA_MAX_QUEUE) {
545 /* the scmd has an associated qc */
546 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
547 /* which hasn't failed yet, timeout */
548 qc->err_mask |= AC_ERR_TIMEOUT;
549 qc->flags |= ATA_QCFLAG_FAILED;
550 nr_timedout++;
551 }
552 } else {
553 /* Normal completion occurred after
554 * SCSI timeout but before this point.
555 * Successfully complete it.
556 */
557 scmd->retries = scmd->allowed;
558 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
559 }
560 }
561
562 /* If we have timed out qcs. They belong to EH from
563 * this point but the state of the controller is
564 * unknown. Freeze the port to make sure the IRQ
565 * handler doesn't diddle with those qcs. This must
566 * be done atomically w.r.t. setting QCFLAG_FAILED.
567 */
568 if (nr_timedout)
569 __ata_port_freeze(ap);
570
Tejun Heoe30349d2006-07-03 03:02:15 +0900571 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa1e10f72007-08-18 13:28:49 +0900572
573 /* initialize eh_tries */
574 ap->eh_tries = ATA_EH_MAX_TRIES;
Tejun Heoad9e2762006-05-15 20:58:12 +0900575 } else
Tejun Heoe30349d2006-07-03 03:02:15 +0900576 spin_unlock_wait(ap->lock);
Tejun Heoad9e2762006-05-15 20:58:12 +0900577
578 repeat:
579 /* invoke error handler */
580 if (ap->ops->error_handler) {
Tejun Heocf1b86c2007-08-06 18:36:23 +0900581 struct ata_link *link;
582
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900583 /* kill fast drain timer */
584 del_timer_sync(&ap->fastdrain_timer);
585
Tejun Heo500530f2006-07-03 16:07:27 +0900586 /* process port resume request */
587 ata_eh_handle_port_resume(ap);
588
Tejun Heof3e81b12006-05-15 20:58:21 +0900589 /* fetch & clear EH info */
Tejun Heoe30349d2006-07-03 03:02:15 +0900590 spin_lock_irqsave(ap->lock, flags);
Tejun Heof3e81b12006-05-15 20:58:21 +0900591
Tejun Heocf1b86c2007-08-06 18:36:23 +0900592 __ata_port_for_each_link(link, ap) {
Tejun Heo00115e02007-11-27 19:28:58 +0900593 struct ata_eh_context *ehc = &link->eh_context;
594 struct ata_device *dev;
595
Tejun Heocf1b86c2007-08-06 18:36:23 +0900596 memset(&link->eh_context, 0, sizeof(link->eh_context));
597 link->eh_context.i = link->eh_info;
598 memset(&link->eh_info, 0, sizeof(link->eh_info));
Tejun Heo00115e02007-11-27 19:28:58 +0900599
600 ata_link_for_each_dev(dev, link) {
601 int devno = dev->devno;
602
603 ehc->saved_xfer_mode[devno] = dev->xfer_mode;
604 if (ata_ncq_enabled(dev))
605 ehc->saved_ncq_enabled |= 1 << devno;
606 }
Tejun Heo0a2c0f52008-05-20 02:17:52 +0900607
608 /* set last reset timestamp to some time in the past */
609 ehc->last_reset = jiffies - 60 * HZ;
Tejun Heocf1b86c2007-08-06 18:36:23 +0900610 }
Tejun Heof3e81b12006-05-15 20:58:21 +0900611
Tejun Heob51e9e52006-06-29 01:29:30 +0900612 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
613 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
Tejun Heoda917d62007-09-23 13:14:12 +0900614 ap->excl_link = NULL; /* don't maintain exclusion over EH */
Tejun Heof3e81b12006-05-15 20:58:21 +0900615
Tejun Heoe30349d2006-07-03 03:02:15 +0900616 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900617
Tejun Heo500530f2006-07-03 16:07:27 +0900618 /* invoke EH, skip if unloading or suspended */
619 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
Tejun Heo720ba122006-05-31 18:28:13 +0900620 ap->ops->error_handler(ap);
621 else
622 ata_eh_finish(ap);
Tejun Heoad9e2762006-05-15 20:58:12 +0900623
Tejun Heo500530f2006-07-03 16:07:27 +0900624 /* process port suspend request */
625 ata_eh_handle_port_suspend(ap);
626
Tejun Heoad9e2762006-05-15 20:58:12 +0900627 /* Exception might have happend after ->error_handler
628 * recovered the port but before this point. Repeat
629 * EH in such case.
630 */
Tejun Heoe30349d2006-07-03 03:02:15 +0900631 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900632
Tejun Heob51e9e52006-06-29 01:29:30 +0900633 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
Tejun Heoa1e10f72007-08-18 13:28:49 +0900634 if (--ap->eh_tries) {
Tejun Heoe30349d2006-07-03 03:02:15 +0900635 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900636 goto repeat;
637 }
638 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
Tejun Heoa1e10f72007-08-18 13:28:49 +0900639 "tries, giving up\n", ATA_EH_MAX_TRIES);
Tejun Heo914616a2007-06-25 21:47:11 +0900640 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
Tejun Heoad9e2762006-05-15 20:58:12 +0900641 }
642
Tejun Heof3e81b12006-05-15 20:58:21 +0900643 /* this run is complete, make sure EH info is clear */
Tejun Heocf1b86c2007-08-06 18:36:23 +0900644 __ata_port_for_each_link(link, ap)
645 memset(&link->eh_info, 0, sizeof(link->eh_info));
Tejun Heof3e81b12006-05-15 20:58:21 +0900646
Tejun Heoe30349d2006-07-03 03:02:15 +0900647 /* Clear host_eh_scheduled while holding ap->lock such
Tejun Heoad9e2762006-05-15 20:58:12 +0900648 * that if exception occurs after this point but
649 * before EH completion, SCSI midlayer will
650 * re-initiate EH.
651 */
652 host->host_eh_scheduled = 0;
653
Tejun Heoe30349d2006-07-03 03:02:15 +0900654 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900655 } else {
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900656 WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
Tejun Heoad9e2762006-05-15 20:58:12 +0900657 ap->ops->eng_timeout(ap);
658 }
659
660 /* finish or retry handled scmd's and clean up */
Tejun Heoece1d632006-04-02 18:51:53 +0900661 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
662
663 scsi_eh_flush_done_q(&ap->eh_done_q);
664
Tejun Heoad9e2762006-05-15 20:58:12 +0900665 /* clean up */
Tejun Heoe30349d2006-07-03 03:02:15 +0900666 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900667
Tejun Heo1cdaf532006-07-03 16:07:26 +0900668 if (ap->pflags & ATA_PFLAG_LOADING)
Tejun Heob51e9e52006-06-29 01:29:30 +0900669 ap->pflags &= ~ATA_PFLAG_LOADING;
Tejun Heo1cdaf532006-07-03 16:07:26 +0900670 else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
David Howells52bad642006-11-22 14:54:01 +0000671 queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0);
Tejun Heo1cdaf532006-07-03 16:07:26 +0900672
673 if (ap->pflags & ATA_PFLAG_RECOVERED)
674 ata_port_printk(ap, KERN_INFO, "EH complete\n");
Tejun Heo580b21022006-05-31 18:28:05 +0900675
Tejun Heob51e9e52006-06-29 01:29:30 +0900676 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
Tejun Heoad9e2762006-05-15 20:58:12 +0900677
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900678 /* tell wait_eh that we're done */
Tejun Heob51e9e52006-06-29 01:29:30 +0900679 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900680 wake_up_all(&ap->eh_wait_q);
681
Tejun Heoe30349d2006-07-03 03:02:15 +0900682 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900683
Tejun Heoece1d632006-04-02 18:51:53 +0900684 DPRINTK("EXIT\n");
Tejun Heoece1d632006-04-02 18:51:53 +0900685}
686
687/**
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900688 * ata_port_wait_eh - Wait for the currently pending EH to complete
689 * @ap: Port to wait EH for
690 *
691 * Wait until the currently pending EH is complete.
692 *
693 * LOCKING:
694 * Kernel thread context (may sleep).
695 */
696void ata_port_wait_eh(struct ata_port *ap)
697{
698 unsigned long flags;
699 DEFINE_WAIT(wait);
700
701 retry:
Jeff Garzikba6a1302006-06-22 23:46:10 -0400702 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900703
Tejun Heob51e9e52006-06-29 01:29:30 +0900704 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900705 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
Jeff Garzikba6a1302006-06-22 23:46:10 -0400706 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900707 schedule();
Jeff Garzikba6a1302006-06-22 23:46:10 -0400708 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900709 }
Tejun Heo0a1b6222006-06-11 11:01:38 +0900710 finish_wait(&ap->eh_wait_q, &wait);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900711
Jeff Garzikba6a1302006-06-22 23:46:10 -0400712 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900713
714 /* make sure SCSI EH is complete */
Jeff Garzikcca39742006-08-24 03:19:22 -0400715 if (scsi_host_in_recovery(ap->scsi_host)) {
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900716 msleep(10);
717 goto retry;
718 }
719}
720
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900721static int ata_eh_nr_in_flight(struct ata_port *ap)
722{
723 unsigned int tag;
724 int nr = 0;
725
726 /* count only non-internal commands */
727 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++)
728 if (ata_qc_from_tag(ap, tag))
729 nr++;
730
731 return nr;
732}
733
734void ata_eh_fastdrain_timerfn(unsigned long arg)
735{
736 struct ata_port *ap = (void *)arg;
737 unsigned long flags;
738 int cnt;
739
740 spin_lock_irqsave(ap->lock, flags);
741
742 cnt = ata_eh_nr_in_flight(ap);
743
744 /* are we done? */
745 if (!cnt)
746 goto out_unlock;
747
748 if (cnt == ap->fastdrain_cnt) {
749 unsigned int tag;
750
751 /* No progress during the last interval, tag all
752 * in-flight qcs as timed out and freeze the port.
753 */
754 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) {
755 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
756 if (qc)
757 qc->err_mask |= AC_ERR_TIMEOUT;
758 }
759
760 ata_port_freeze(ap);
761 } else {
762 /* some qcs have finished, give it another chance */
763 ap->fastdrain_cnt = cnt;
764 ap->fastdrain_timer.expires =
Tejun Heo341c2c92008-05-20 02:17:51 +0900765 ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900766 add_timer(&ap->fastdrain_timer);
767 }
768
769 out_unlock:
770 spin_unlock_irqrestore(ap->lock, flags);
771}
772
773/**
774 * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
775 * @ap: target ATA port
776 * @fastdrain: activate fast drain
777 *
778 * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
779 * is non-zero and EH wasn't pending before. Fast drain ensures
780 * that EH kicks in in timely manner.
781 *
782 * LOCKING:
783 * spin_lock_irqsave(host lock)
784 */
785static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
786{
787 int cnt;
788
789 /* already scheduled? */
790 if (ap->pflags & ATA_PFLAG_EH_PENDING)
791 return;
792
793 ap->pflags |= ATA_PFLAG_EH_PENDING;
794
795 if (!fastdrain)
796 return;
797
798 /* do we have in-flight qcs? */
799 cnt = ata_eh_nr_in_flight(ap);
800 if (!cnt)
801 return;
802
803 /* activate fast drain */
804 ap->fastdrain_cnt = cnt;
Tejun Heo341c2c92008-05-20 02:17:51 +0900805 ap->fastdrain_timer.expires =
806 ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900807 add_timer(&ap->fastdrain_timer);
808}
809
Tejun Heof686bcb2006-05-15 20:58:05 +0900810/**
811 * ata_qc_schedule_eh - schedule qc for error handling
812 * @qc: command to schedule error handling for
813 *
814 * Schedule error handling for @qc. EH will kick in as soon as
815 * other commands are drained.
816 *
817 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400818 * spin_lock_irqsave(host lock)
Tejun Heof686bcb2006-05-15 20:58:05 +0900819 */
820void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
821{
822 struct ata_port *ap = qc->ap;
823
824 WARN_ON(!ap->ops->error_handler);
825
826 qc->flags |= ATA_QCFLAG_FAILED;
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900827 ata_eh_set_pending(ap, 1);
Tejun Heof686bcb2006-05-15 20:58:05 +0900828
829 /* The following will fail if timeout has already expired.
830 * ata_scsi_error() takes care of such scmds on EH entry.
831 * Note that ATA_QCFLAG_FAILED is unconditionally set after
832 * this function completes.
833 */
834 scsi_req_abort_cmd(qc->scsicmd);
835}
836
Tejun Heo7b70fc02006-05-15 20:58:07 +0900837/**
838 * ata_port_schedule_eh - schedule error handling without a qc
839 * @ap: ATA port to schedule EH for
840 *
841 * Schedule error handling for @ap. EH will kick in as soon as
842 * all commands are drained.
843 *
844 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400845 * spin_lock_irqsave(host lock)
Tejun Heo7b70fc02006-05-15 20:58:07 +0900846 */
847void ata_port_schedule_eh(struct ata_port *ap)
848{
849 WARN_ON(!ap->ops->error_handler);
850
Tejun Heof4d6d002007-05-01 11:50:15 +0200851 if (ap->pflags & ATA_PFLAG_INITIALIZING)
852 return;
853
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900854 ata_eh_set_pending(ap, 1);
Jeff Garzikcca39742006-08-24 03:19:22 -0400855 scsi_schedule_eh(ap->scsi_host);
Tejun Heo7b70fc02006-05-15 20:58:07 +0900856
857 DPRINTK("port EH scheduled\n");
858}
859
Tejun Heodbd82612007-08-06 18:36:23 +0900860static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
Tejun Heo7b70fc02006-05-15 20:58:07 +0900861{
862 int tag, nr_aborted = 0;
863
864 WARN_ON(!ap->ops->error_handler);
865
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900866 /* we're gonna abort all commands, no need for fast drain */
867 ata_eh_set_pending(ap, 0);
868
Tejun Heo7b70fc02006-05-15 20:58:07 +0900869 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
870 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
871
Tejun Heodbd82612007-08-06 18:36:23 +0900872 if (qc && (!link || qc->dev->link == link)) {
Tejun Heo7b70fc02006-05-15 20:58:07 +0900873 qc->flags |= ATA_QCFLAG_FAILED;
874 ata_qc_complete(qc);
875 nr_aborted++;
876 }
877 }
878
879 if (!nr_aborted)
880 ata_port_schedule_eh(ap);
881
882 return nr_aborted;
883}
884
Tejun Heoe3180492006-05-15 20:58:09 +0900885/**
Tejun Heodbd82612007-08-06 18:36:23 +0900886 * ata_link_abort - abort all qc's on the link
887 * @link: ATA link to abort qc's for
888 *
889 * Abort all active qc's active on @link and schedule EH.
890 *
891 * LOCKING:
892 * spin_lock_irqsave(host lock)
893 *
894 * RETURNS:
895 * Number of aborted qc's.
896 */
897int ata_link_abort(struct ata_link *link)
898{
899 return ata_do_link_abort(link->ap, link);
900}
901
902/**
903 * ata_port_abort - abort all qc's on the port
904 * @ap: ATA port to abort qc's for
905 *
906 * Abort all active qc's of @ap and schedule EH.
907 *
908 * LOCKING:
909 * spin_lock_irqsave(host_set lock)
910 *
911 * RETURNS:
912 * Number of aborted qc's.
913 */
914int ata_port_abort(struct ata_port *ap)
915{
916 return ata_do_link_abort(ap, NULL);
917}
918
919/**
Tejun Heoe3180492006-05-15 20:58:09 +0900920 * __ata_port_freeze - freeze port
921 * @ap: ATA port to freeze
922 *
923 * This function is called when HSM violation or some other
924 * condition disrupts normal operation of the port. Frozen port
925 * is not allowed to perform any operation until the port is
926 * thawed, which usually follows a successful reset.
927 *
928 * ap->ops->freeze() callback can be used for freezing the port
929 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
930 * port cannot be frozen hardware-wise, the interrupt handler
931 * must ack and clear interrupts unconditionally while the port
932 * is frozen.
933 *
934 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400935 * spin_lock_irqsave(host lock)
Tejun Heoe3180492006-05-15 20:58:09 +0900936 */
937static void __ata_port_freeze(struct ata_port *ap)
938{
939 WARN_ON(!ap->ops->error_handler);
940
941 if (ap->ops->freeze)
942 ap->ops->freeze(ap);
943
Tejun Heob51e9e52006-06-29 01:29:30 +0900944 ap->pflags |= ATA_PFLAG_FROZEN;
Tejun Heoe3180492006-05-15 20:58:09 +0900945
Tejun Heo44877b42007-02-21 01:06:51 +0900946 DPRINTK("ata%u port frozen\n", ap->print_id);
Tejun Heoe3180492006-05-15 20:58:09 +0900947}
948
949/**
950 * ata_port_freeze - abort & freeze port
951 * @ap: ATA port to freeze
952 *
953 * Abort and freeze @ap.
954 *
955 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400956 * spin_lock_irqsave(host lock)
Tejun Heoe3180492006-05-15 20:58:09 +0900957 *
958 * RETURNS:
959 * Number of aborted commands.
960 */
961int ata_port_freeze(struct ata_port *ap)
962{
963 int nr_aborted;
964
965 WARN_ON(!ap->ops->error_handler);
966
967 nr_aborted = ata_port_abort(ap);
968 __ata_port_freeze(ap);
969
970 return nr_aborted;
971}
972
973/**
Tejun Heo7d77b242007-09-23 13:14:13 +0900974 * sata_async_notification - SATA async notification handler
975 * @ap: ATA port where async notification is received
976 *
977 * Handler to be called when async notification via SDB FIS is
978 * received. This function schedules EH if necessary.
979 *
980 * LOCKING:
981 * spin_lock_irqsave(host lock)
982 *
983 * RETURNS:
984 * 1 if EH is scheduled, 0 otherwise.
985 */
986int sata_async_notification(struct ata_port *ap)
987{
988 u32 sntf;
989 int rc;
990
991 if (!(ap->flags & ATA_FLAG_AN))
992 return 0;
993
994 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
995 if (rc == 0)
996 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
997
Tejun Heo071f44b2008-04-07 22:47:22 +0900998 if (!sata_pmp_attached(ap) || rc) {
Tejun Heo7d77b242007-09-23 13:14:13 +0900999 /* PMP is not attached or SNTF is not available */
Tejun Heo071f44b2008-04-07 22:47:22 +09001000 if (!sata_pmp_attached(ap)) {
Tejun Heo7d77b242007-09-23 13:14:13 +09001001 /* PMP is not attached. Check whether ATAPI
1002 * AN is configured. If so, notify media
1003 * change.
1004 */
1005 struct ata_device *dev = ap->link.device;
1006
1007 if ((dev->class == ATA_DEV_ATAPI) &&
1008 (dev->flags & ATA_DFLAG_AN))
1009 ata_scsi_media_change_notify(dev);
1010 return 0;
1011 } else {
1012 /* PMP is attached but SNTF is not available.
1013 * ATAPI async media change notification is
1014 * not used. The PMP must be reporting PHY
1015 * status change, schedule EH.
1016 */
1017 ata_port_schedule_eh(ap);
1018 return 1;
1019 }
1020 } else {
1021 /* PMP is attached and SNTF is available */
1022 struct ata_link *link;
1023
1024 /* check and notify ATAPI AN */
1025 ata_port_for_each_link(link, ap) {
1026 if (!(sntf & (1 << link->pmp)))
1027 continue;
1028
1029 if ((link->device->class == ATA_DEV_ATAPI) &&
1030 (link->device->flags & ATA_DFLAG_AN))
1031 ata_scsi_media_change_notify(link->device);
1032 }
1033
1034 /* If PMP is reporting that PHY status of some
1035 * downstream ports has changed, schedule EH.
1036 */
1037 if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
1038 ata_port_schedule_eh(ap);
1039 return 1;
1040 }
1041
1042 return 0;
1043 }
1044}
1045
1046/**
Tejun Heoe3180492006-05-15 20:58:09 +09001047 * ata_eh_freeze_port - EH helper to freeze port
1048 * @ap: ATA port to freeze
1049 *
1050 * Freeze @ap.
1051 *
1052 * LOCKING:
1053 * None.
1054 */
1055void ata_eh_freeze_port(struct ata_port *ap)
1056{
1057 unsigned long flags;
1058
1059 if (!ap->ops->error_handler)
1060 return;
1061
Jeff Garzikba6a1302006-06-22 23:46:10 -04001062 spin_lock_irqsave(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001063 __ata_port_freeze(ap);
Jeff Garzikba6a1302006-06-22 23:46:10 -04001064 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001065}
1066
1067/**
1068 * ata_port_thaw_port - EH helper to thaw port
1069 * @ap: ATA port to thaw
1070 *
1071 * Thaw frozen port @ap.
1072 *
1073 * LOCKING:
1074 * None.
1075 */
1076void ata_eh_thaw_port(struct ata_port *ap)
1077{
1078 unsigned long flags;
1079
1080 if (!ap->ops->error_handler)
1081 return;
1082
Jeff Garzikba6a1302006-06-22 23:46:10 -04001083 spin_lock_irqsave(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001084
Tejun Heob51e9e52006-06-29 01:29:30 +09001085 ap->pflags &= ~ATA_PFLAG_FROZEN;
Tejun Heoe3180492006-05-15 20:58:09 +09001086
1087 if (ap->ops->thaw)
1088 ap->ops->thaw(ap);
1089
Jeff Garzikba6a1302006-06-22 23:46:10 -04001090 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001091
Tejun Heo44877b42007-02-21 01:06:51 +09001092 DPRINTK("ata%u port thawed\n", ap->print_id);
Tejun Heoe3180492006-05-15 20:58:09 +09001093}
1094
Tejun Heoece1d632006-04-02 18:51:53 +09001095static void ata_eh_scsidone(struct scsi_cmnd *scmd)
1096{
1097 /* nada */
1098}
1099
1100static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
1101{
1102 struct ata_port *ap = qc->ap;
1103 struct scsi_cmnd *scmd = qc->scsicmd;
1104 unsigned long flags;
1105
Jeff Garzikba6a1302006-06-22 23:46:10 -04001106 spin_lock_irqsave(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +09001107 qc->scsidone = ata_eh_scsidone;
1108 __ata_qc_complete(qc);
1109 WARN_ON(ata_tag_valid(qc->tag));
Jeff Garzikba6a1302006-06-22 23:46:10 -04001110 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +09001111
1112 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
1113}
1114
1115/**
1116 * ata_eh_qc_complete - Complete an active ATA command from EH
1117 * @qc: Command to complete
1118 *
1119 * Indicate to the mid and upper layers that an ATA command has
1120 * completed. To be used from EH.
1121 */
1122void ata_eh_qc_complete(struct ata_queued_cmd *qc)
1123{
1124 struct scsi_cmnd *scmd = qc->scsicmd;
1125 scmd->retries = scmd->allowed;
1126 __ata_eh_qc_complete(qc);
1127}
1128
1129/**
1130 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
1131 * @qc: Command to retry
1132 *
1133 * Indicate to the mid and upper layers that an ATA command
1134 * should be retried. To be used from EH.
1135 *
1136 * SCSI midlayer limits the number of retries to scmd->allowed.
1137 * scmd->retries is decremented for commands which get retried
1138 * due to unrelated failures (qc->err_mask is zero).
1139 */
1140void ata_eh_qc_retry(struct ata_queued_cmd *qc)
1141{
1142 struct scsi_cmnd *scmd = qc->scsicmd;
1143 if (!qc->err_mask && scmd->retries)
1144 scmd->retries--;
1145 __ata_eh_qc_complete(qc);
1146}
Tejun Heo022bdb02006-05-15 20:58:22 +09001147
1148/**
Tejun Heo0ea035a2006-05-31 18:28:01 +09001149 * ata_eh_detach_dev - detach ATA device
1150 * @dev: ATA device to detach
1151 *
1152 * Detach @dev.
1153 *
1154 * LOCKING:
1155 * None.
1156 */
Tejun Heofb7fd612007-09-23 13:14:12 +09001157void ata_eh_detach_dev(struct ata_device *dev)
Tejun Heo0ea035a2006-05-31 18:28:01 +09001158{
Tejun Heof58229f2007-08-06 18:36:23 +09001159 struct ata_link *link = dev->link;
1160 struct ata_port *ap = link->ap;
Tejun Heo0ea035a2006-05-31 18:28:01 +09001161 unsigned long flags;
1162
1163 ata_dev_disable(dev);
1164
Jeff Garzikba6a1302006-06-22 23:46:10 -04001165 spin_lock_irqsave(ap->lock, flags);
Tejun Heo0ea035a2006-05-31 18:28:01 +09001166
1167 dev->flags &= ~ATA_DFLAG_DETACH;
1168
1169 if (ata_scsi_offline_dev(dev)) {
1170 dev->flags |= ATA_DFLAG_DETACHED;
Tejun Heob51e9e52006-06-29 01:29:30 +09001171 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
Tejun Heo0ea035a2006-05-31 18:28:01 +09001172 }
1173
Tejun Heobeb07c12006-06-24 20:30:19 +09001174 /* clear per-dev EH actions */
Tejun Heof58229f2007-08-06 18:36:23 +09001175 ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
1176 ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
Tejun Heobeb07c12006-06-24 20:30:19 +09001177
Jeff Garzikba6a1302006-06-22 23:46:10 -04001178 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo0ea035a2006-05-31 18:28:01 +09001179}
1180
1181/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001182 * ata_eh_about_to_do - about to perform eh_action
Tejun Heo955e57d2007-08-06 18:36:23 +09001183 * @link: target ATA link
Tejun Heo47005f22006-06-19 18:27:23 +09001184 * @dev: target ATA dev for per-dev action (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09001185 * @action: action about to be performed
1186 *
1187 * Called just before performing EH actions to clear related bits
Tejun Heo955e57d2007-08-06 18:36:23 +09001188 * in @link->eh_info such that eh actions are not unnecessarily
1189 * repeated.
Tejun Heo022bdb02006-05-15 20:58:22 +09001190 *
1191 * LOCKING:
1192 * None.
1193 */
Tejun Heofb7fd612007-09-23 13:14:12 +09001194void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
1195 unsigned int action)
Tejun Heo022bdb02006-05-15 20:58:22 +09001196{
Tejun Heo955e57d2007-08-06 18:36:23 +09001197 struct ata_port *ap = link->ap;
1198 struct ata_eh_info *ehi = &link->eh_info;
1199 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001200 unsigned long flags;
1201
Jeff Garzikba6a1302006-06-22 23:46:10 -04001202 spin_lock_irqsave(ap->lock, flags);
Tejun Heo1cdaf532006-07-03 16:07:26 +09001203
Tejun Heo955e57d2007-08-06 18:36:23 +09001204 ata_eh_clear_action(link, dev, ehi, action);
Tejun Heo13abf502006-07-10 23:18:46 +09001205
1206 if (!(ehc->i.flags & ATA_EHI_QUIET))
Tejun Heo1cdaf532006-07-03 16:07:26 +09001207 ap->pflags |= ATA_PFLAG_RECOVERED;
1208
Jeff Garzikba6a1302006-06-22 23:46:10 -04001209 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo022bdb02006-05-15 20:58:22 +09001210}
1211
1212/**
Tejun Heo47005f22006-06-19 18:27:23 +09001213 * ata_eh_done - EH action complete
Tejun Heo955e57d2007-08-06 18:36:23 +09001214* @ap: target ATA port
Tejun Heo47005f22006-06-19 18:27:23 +09001215 * @dev: target ATA dev for per-dev action (can be NULL)
1216 * @action: action just completed
1217 *
1218 * Called right after performing EH actions to clear related bits
Tejun Heo955e57d2007-08-06 18:36:23 +09001219 * in @link->eh_context.
Tejun Heo47005f22006-06-19 18:27:23 +09001220 *
1221 * LOCKING:
1222 * None.
1223 */
Tejun Heofb7fd612007-09-23 13:14:12 +09001224void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1225 unsigned int action)
Tejun Heo47005f22006-06-19 18:27:23 +09001226{
Tejun Heo955e57d2007-08-06 18:36:23 +09001227 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001228
Tejun Heo955e57d2007-08-06 18:36:23 +09001229 ata_eh_clear_action(link, dev, &ehc->i, action);
Tejun Heo47005f22006-06-19 18:27:23 +09001230}
1231
1232/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001233 * ata_err_string - convert err_mask to descriptive string
1234 * @err_mask: error mask to convert to string
1235 *
1236 * Convert @err_mask to descriptive string. Errors are
1237 * prioritized according to severity and only the most severe
1238 * error is reported.
1239 *
1240 * LOCKING:
1241 * None.
1242 *
1243 * RETURNS:
1244 * Descriptive string for @err_mask
1245 */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001246static const char *ata_err_string(unsigned int err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001247{
1248 if (err_mask & AC_ERR_HOST_BUS)
1249 return "host bus error";
1250 if (err_mask & AC_ERR_ATA_BUS)
1251 return "ATA bus error";
1252 if (err_mask & AC_ERR_TIMEOUT)
1253 return "timeout";
1254 if (err_mask & AC_ERR_HSM)
1255 return "HSM violation";
1256 if (err_mask & AC_ERR_SYSTEM)
1257 return "internal error";
1258 if (err_mask & AC_ERR_MEDIA)
1259 return "media error";
1260 if (err_mask & AC_ERR_INVALID)
1261 return "invalid argument";
1262 if (err_mask & AC_ERR_DEV)
1263 return "device error";
1264 return "unknown error";
1265}
1266
1267/**
Tejun Heoe8ee8452006-05-15 21:03:46 +09001268 * ata_read_log_page - read a specific log page
1269 * @dev: target device
1270 * @page: page to read
1271 * @buf: buffer to store read page
1272 * @sectors: number of sectors to read
1273 *
1274 * Read log page using READ_LOG_EXT command.
1275 *
1276 * LOCKING:
1277 * Kernel thread context (may sleep).
1278 *
1279 * RETURNS:
1280 * 0 on success, AC_ERR_* mask otherwise.
1281 */
1282static unsigned int ata_read_log_page(struct ata_device *dev,
1283 u8 page, void *buf, unsigned int sectors)
1284{
1285 struct ata_taskfile tf;
1286 unsigned int err_mask;
1287
1288 DPRINTK("read log page - page %d\n", page);
1289
1290 ata_tf_init(dev, &tf);
1291 tf.command = ATA_CMD_READ_LOG_EXT;
1292 tf.lbal = page;
1293 tf.nsect = sectors;
1294 tf.hob_nsect = sectors >> 8;
1295 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1296 tf.protocol = ATA_PROT_PIO;
1297
1298 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
Tejun Heo2b789102007-10-09 15:05:44 +09001299 buf, sectors * ATA_SECT_SIZE, 0);
Tejun Heoe8ee8452006-05-15 21:03:46 +09001300
1301 DPRINTK("EXIT, err_mask=%x\n", err_mask);
1302 return err_mask;
1303}
1304
1305/**
1306 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
1307 * @dev: Device to read log page 10h from
1308 * @tag: Resulting tag of the failed command
1309 * @tf: Resulting taskfile registers of the failed command
1310 *
1311 * Read log page 10h to obtain NCQ error details and clear error
1312 * condition.
1313 *
1314 * LOCKING:
1315 * Kernel thread context (may sleep).
1316 *
1317 * RETURNS:
1318 * 0 on success, -errno otherwise.
1319 */
1320static int ata_eh_read_log_10h(struct ata_device *dev,
1321 int *tag, struct ata_taskfile *tf)
1322{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001323 u8 *buf = dev->link->ap->sector_buf;
Tejun Heoe8ee8452006-05-15 21:03:46 +09001324 unsigned int err_mask;
1325 u8 csum;
1326 int i;
1327
1328 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
1329 if (err_mask)
1330 return -EIO;
1331
1332 csum = 0;
1333 for (i = 0; i < ATA_SECT_SIZE; i++)
1334 csum += buf[i];
1335 if (csum)
1336 ata_dev_printk(dev, KERN_WARNING,
1337 "invalid checksum 0x%x on log page 10h\n", csum);
1338
1339 if (buf[0] & 0x80)
1340 return -ENOENT;
1341
1342 *tag = buf[0] & 0x1f;
1343
1344 tf->command = buf[2];
1345 tf->feature = buf[3];
1346 tf->lbal = buf[4];
1347 tf->lbam = buf[5];
1348 tf->lbah = buf[6];
1349 tf->device = buf[7];
1350 tf->hob_lbal = buf[8];
1351 tf->hob_lbam = buf[9];
1352 tf->hob_lbah = buf[10];
1353 tf->nsect = buf[12];
1354 tf->hob_nsect = buf[13];
1355
1356 return 0;
1357}
1358
1359/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001360 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1361 * @dev: device to perform REQUEST_SENSE to
1362 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
Tejun Heo3eabddb2008-06-10 18:28:05 +09001363 * @dfl_sense_key: default sense key to use
Tejun Heo022bdb02006-05-15 20:58:22 +09001364 *
1365 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
1366 * SENSE. This function is EH helper.
1367 *
1368 * LOCKING:
1369 * Kernel thread context (may sleep).
1370 *
1371 * RETURNS:
1372 * 0 on success, AC_ERR_* mask on failure
1373 */
Tejun Heo3eabddb2008-06-10 18:28:05 +09001374static unsigned int atapi_eh_request_sense(struct ata_device *dev,
1375 u8 *sense_buf, u8 dfl_sense_key)
Tejun Heo022bdb02006-05-15 20:58:22 +09001376{
Tejun Heo3eabddb2008-06-10 18:28:05 +09001377 u8 cdb[ATAPI_CDB_LEN] =
1378 { REQUEST_SENSE, 0, 0, 0, SCSI_SENSE_BUFFERSIZE, 0 };
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001379 struct ata_port *ap = dev->link->ap;
Tejun Heo022bdb02006-05-15 20:58:22 +09001380 struct ata_taskfile tf;
Tejun Heo022bdb02006-05-15 20:58:22 +09001381
1382 DPRINTK("ATAPI request sense\n");
1383
Tejun Heo022bdb02006-05-15 20:58:22 +09001384 /* FIXME: is this needed? */
1385 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1386
Albert Lee56287762007-04-02 11:30:46 +08001387 /* initialize sense_buf with the error register,
1388 * for the case where they are -not- overwritten
1389 */
Tejun Heo022bdb02006-05-15 20:58:22 +09001390 sense_buf[0] = 0x70;
Tejun Heo3eabddb2008-06-10 18:28:05 +09001391 sense_buf[2] = dfl_sense_key;
Albert Lee56287762007-04-02 11:30:46 +08001392
Jeff Garzika617c092007-05-21 20:14:23 -04001393 /* some devices time out if garbage left in tf */
Albert Lee56287762007-04-02 11:30:46 +08001394 ata_tf_init(dev, &tf);
Tejun Heo022bdb02006-05-15 20:58:22 +09001395
Tejun Heo022bdb02006-05-15 20:58:22 +09001396 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1397 tf.command = ATA_CMD_PACKET;
1398
1399 /* is it pointless to prefer PIO for "safety reasons"? */
1400 if (ap->flags & ATA_FLAG_PIO_DMA) {
Tejun Heo0dc36882007-12-18 16:34:43 -05001401 tf.protocol = ATAPI_PROT_DMA;
Tejun Heo022bdb02006-05-15 20:58:22 +09001402 tf.feature |= ATAPI_PKT_DMA;
1403 } else {
Tejun Heo0dc36882007-12-18 16:34:43 -05001404 tf.protocol = ATAPI_PROT_PIO;
Tejun Heof2dfc1a2007-12-12 12:12:46 +09001405 tf.lbam = SCSI_SENSE_BUFFERSIZE;
1406 tf.lbah = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09001407 }
1408
1409 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
Tejun Heo2b789102007-10-09 15:05:44 +09001410 sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
Tejun Heo022bdb02006-05-15 20:58:22 +09001411}
1412
1413/**
1414 * ata_eh_analyze_serror - analyze SError for a failed port
Tejun Heo02607312007-08-06 18:36:23 +09001415 * @link: ATA link to analyze SError for
Tejun Heo022bdb02006-05-15 20:58:22 +09001416 *
1417 * Analyze SError if available and further determine cause of
1418 * failure.
1419 *
1420 * LOCKING:
1421 * None.
1422 */
Tejun Heo02607312007-08-06 18:36:23 +09001423static void ata_eh_analyze_serror(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001424{
Tejun Heo02607312007-08-06 18:36:23 +09001425 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001426 u32 serror = ehc->i.serror;
1427 unsigned int err_mask = 0, action = 0;
Tejun Heof9df58c2007-09-23 13:14:13 +09001428 u32 hotplug_mask;
Tejun Heo022bdb02006-05-15 20:58:22 +09001429
Tejun Heoe0614db2008-05-19 01:15:09 +09001430 if (serror & (SERR_PERSISTENT | SERR_DATA)) {
Tejun Heo022bdb02006-05-15 20:58:22 +09001431 err_mask |= AC_ERR_ATA_BUS;
Tejun Heocf480622008-01-24 00:05:14 +09001432 action |= ATA_EH_RESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001433 }
1434 if (serror & SERR_PROTOCOL) {
1435 err_mask |= AC_ERR_HSM;
Tejun Heocf480622008-01-24 00:05:14 +09001436 action |= ATA_EH_RESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001437 }
1438 if (serror & SERR_INTERNAL) {
1439 err_mask |= AC_ERR_SYSTEM;
Tejun Heocf480622008-01-24 00:05:14 +09001440 action |= ATA_EH_RESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001441 }
Tejun Heof9df58c2007-09-23 13:14:13 +09001442
1443 /* Determine whether a hotplug event has occurred. Both
1444 * SError.N/X are considered hotplug events for enabled or
1445 * host links. For disabled PMP links, only N bit is
1446 * considered as X bit is left at 1 for link plugging.
1447 */
1448 hotplug_mask = 0;
1449
1450 if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
1451 hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
1452 else
1453 hotplug_mask = SERR_PHYRDY_CHG;
1454
1455 if (serror & hotplug_mask)
Tejun Heo084fe632006-05-31 18:28:03 +09001456 ata_ehi_hotplugged(&ehc->i);
Tejun Heo022bdb02006-05-15 20:58:22 +09001457
1458 ehc->i.err_mask |= err_mask;
1459 ehc->i.action |= action;
1460}
1461
1462/**
Tejun Heoe8ee8452006-05-15 21:03:46 +09001463 * ata_eh_analyze_ncq_error - analyze NCQ error
Tejun Heo02607312007-08-06 18:36:23 +09001464 * @link: ATA link to analyze NCQ error for
Tejun Heoe8ee8452006-05-15 21:03:46 +09001465 *
1466 * Read log page 10h, determine the offending qc and acquire
1467 * error status TF. For NCQ device errors, all LLDDs have to do
1468 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1469 * care of the rest.
1470 *
1471 * LOCKING:
1472 * Kernel thread context (may sleep).
1473 */
Mark Lord10acf3b2008-05-02 02:14:53 -04001474void ata_eh_analyze_ncq_error(struct ata_link *link)
Tejun Heoe8ee8452006-05-15 21:03:46 +09001475{
Tejun Heo02607312007-08-06 18:36:23 +09001476 struct ata_port *ap = link->ap;
1477 struct ata_eh_context *ehc = &link->eh_context;
1478 struct ata_device *dev = link->device;
Tejun Heoe8ee8452006-05-15 21:03:46 +09001479 struct ata_queued_cmd *qc;
1480 struct ata_taskfile tf;
1481 int tag, rc;
1482
1483 /* if frozen, we can't do much */
Tejun Heob51e9e52006-06-29 01:29:30 +09001484 if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heoe8ee8452006-05-15 21:03:46 +09001485 return;
1486
1487 /* is it NCQ device error? */
Tejun Heo02607312007-08-06 18:36:23 +09001488 if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
Tejun Heoe8ee8452006-05-15 21:03:46 +09001489 return;
1490
1491 /* has LLDD analyzed already? */
1492 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1493 qc = __ata_qc_from_tag(ap, tag);
1494
1495 if (!(qc->flags & ATA_QCFLAG_FAILED))
1496 continue;
1497
1498 if (qc->err_mask)
1499 return;
1500 }
1501
1502 /* okay, this error is ours */
1503 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1504 if (rc) {
Tejun Heo02607312007-08-06 18:36:23 +09001505 ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
Tejun Heoe8ee8452006-05-15 21:03:46 +09001506 "(errno=%d)\n", rc);
1507 return;
1508 }
1509
Tejun Heo02607312007-08-06 18:36:23 +09001510 if (!(link->sactive & (1 << tag))) {
1511 ata_link_printk(link, KERN_ERR, "log page 10h reported "
Tejun Heoe8ee8452006-05-15 21:03:46 +09001512 "inactive tag %d\n", tag);
1513 return;
1514 }
1515
1516 /* we've got the perpetrator, condemn it */
1517 qc = __ata_qc_from_tag(ap, tag);
1518 memcpy(&qc->result_tf, &tf, sizeof(tf));
Mark Lorda6116c92008-04-23 22:36:25 -04001519 qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
Tejun Heo5335b722007-07-16 14:29:40 +09001520 qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
Tejun Heoe8ee8452006-05-15 21:03:46 +09001521 ehc->i.err_mask &= ~AC_ERR_DEV;
1522}
1523
1524/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001525 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1526 * @qc: qc to analyze
1527 * @tf: Taskfile registers to analyze
1528 *
1529 * Analyze taskfile of @qc and further determine cause of
1530 * failure. This function also requests ATAPI sense data if
1531 * avaliable.
1532 *
1533 * LOCKING:
1534 * Kernel thread context (may sleep).
1535 *
1536 * RETURNS:
1537 * Determined recovery action
1538 */
1539static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1540 const struct ata_taskfile *tf)
1541{
1542 unsigned int tmp, action = 0;
1543 u8 stat = tf->command, err = tf->feature;
1544
1545 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1546 qc->err_mask |= AC_ERR_HSM;
Tejun Heocf480622008-01-24 00:05:14 +09001547 return ATA_EH_RESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001548 }
1549
Tejun Heoa51d6442007-03-20 15:24:11 +09001550 if (stat & (ATA_ERR | ATA_DF))
1551 qc->err_mask |= AC_ERR_DEV;
1552 else
Tejun Heo022bdb02006-05-15 20:58:22 +09001553 return 0;
1554
1555 switch (qc->dev->class) {
1556 case ATA_DEV_ATA:
1557 if (err & ATA_ICRC)
1558 qc->err_mask |= AC_ERR_ATA_BUS;
1559 if (err & ATA_UNC)
1560 qc->err_mask |= AC_ERR_MEDIA;
1561 if (err & ATA_IDNF)
1562 qc->err_mask |= AC_ERR_INVALID;
1563 break;
1564
1565 case ATA_DEV_ATAPI:
Tejun Heoa569a302006-11-21 10:40:51 +09001566 if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
Tejun Heo3eabddb2008-06-10 18:28:05 +09001567 tmp = atapi_eh_request_sense(qc->dev,
1568 qc->scsicmd->sense_buffer,
1569 qc->result_tf.feature >> 4);
Tejun Heoa569a302006-11-21 10:40:51 +09001570 if (!tmp) {
1571 /* ATA_QCFLAG_SENSE_VALID is used to
1572 * tell atapi_qc_complete() that sense
1573 * data is already valid.
1574 *
1575 * TODO: interpret sense data and set
1576 * appropriate err_mask.
1577 */
1578 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1579 } else
1580 qc->err_mask |= tmp;
1581 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001582 }
1583
1584 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
Tejun Heocf480622008-01-24 00:05:14 +09001585 action |= ATA_EH_RESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001586
1587 return action;
1588}
1589
Tejun Heo76326ac2007-11-27 19:28:59 +09001590static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask,
1591 int *xfer_ok)
Tejun Heo022bdb02006-05-15 20:58:22 +09001592{
Tejun Heo76326ac2007-11-27 19:28:59 +09001593 int base = 0;
1594
1595 if (!(eflags & ATA_EFLAG_DUBIOUS_XFER))
1596 *xfer_ok = 1;
1597
1598 if (!*xfer_ok)
Tejun Heo75f9caf2008-01-03 01:21:14 +09001599 base = ATA_ECAT_DUBIOUS_NONE;
Tejun Heo76326ac2007-11-27 19:28:59 +09001600
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001601 if (err_mask & AC_ERR_ATA_BUS)
Tejun Heo76326ac2007-11-27 19:28:59 +09001602 return base + ATA_ECAT_ATA_BUS;
Tejun Heo022bdb02006-05-15 20:58:22 +09001603
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001604 if (err_mask & AC_ERR_TIMEOUT)
Tejun Heo76326ac2007-11-27 19:28:59 +09001605 return base + ATA_ECAT_TOUT_HSM;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001606
Tejun Heo3884f7b2007-11-27 19:28:56 +09001607 if (eflags & ATA_EFLAG_IS_IO) {
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001608 if (err_mask & AC_ERR_HSM)
Tejun Heo76326ac2007-11-27 19:28:59 +09001609 return base + ATA_ECAT_TOUT_HSM;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001610 if ((err_mask &
1611 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
Tejun Heo76326ac2007-11-27 19:28:59 +09001612 return base + ATA_ECAT_UNK_DEV;
Tejun Heo022bdb02006-05-15 20:58:22 +09001613 }
1614
1615 return 0;
1616}
1617
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001618struct speed_down_verdict_arg {
Tejun Heo022bdb02006-05-15 20:58:22 +09001619 u64 since;
Tejun Heo76326ac2007-11-27 19:28:59 +09001620 int xfer_ok;
Tejun Heo3884f7b2007-11-27 19:28:56 +09001621 int nr_errors[ATA_ECAT_NR];
Tejun Heo022bdb02006-05-15 20:58:22 +09001622};
1623
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001624static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
Tejun Heo022bdb02006-05-15 20:58:22 +09001625{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001626 struct speed_down_verdict_arg *arg = void_arg;
Tejun Heo76326ac2007-11-27 19:28:59 +09001627 int cat;
Tejun Heo022bdb02006-05-15 20:58:22 +09001628
1629 if (ent->timestamp < arg->since)
1630 return -1;
1631
Tejun Heo76326ac2007-11-27 19:28:59 +09001632 cat = ata_eh_categorize_error(ent->eflags, ent->err_mask,
1633 &arg->xfer_ok);
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001634 arg->nr_errors[cat]++;
Tejun Heo76326ac2007-11-27 19:28:59 +09001635
Tejun Heo022bdb02006-05-15 20:58:22 +09001636 return 0;
1637}
1638
1639/**
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001640 * ata_eh_speed_down_verdict - Determine speed down verdict
Tejun Heo022bdb02006-05-15 20:58:22 +09001641 * @dev: Device of interest
1642 *
1643 * This function examines error ring of @dev and determines
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001644 * whether NCQ needs to be turned off, transfer speed should be
1645 * stepped down, or falling back to PIO is necessary.
Tejun Heo022bdb02006-05-15 20:58:22 +09001646 *
Tejun Heo3884f7b2007-11-27 19:28:56 +09001647 * ECAT_ATA_BUS : ATA_BUS error for any command
Tejun Heo022bdb02006-05-15 20:58:22 +09001648 *
Tejun Heo3884f7b2007-11-27 19:28:56 +09001649 * ECAT_TOUT_HSM : TIMEOUT for any command or HSM violation for
1650 * IO commands
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001651 *
Tejun Heo3884f7b2007-11-27 19:28:56 +09001652 * ECAT_UNK_DEV : Unknown DEV error for IO commands
Tejun Heo022bdb02006-05-15 20:58:22 +09001653 *
Tejun Heo76326ac2007-11-27 19:28:59 +09001654 * ECAT_DUBIOUS_* : Identical to above three but occurred while
1655 * data transfer hasn't been verified.
1656 *
Tejun Heo3884f7b2007-11-27 19:28:56 +09001657 * Verdicts are
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001658 *
Tejun Heo3884f7b2007-11-27 19:28:56 +09001659 * NCQ_OFF : Turn off NCQ.
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001660 *
Tejun Heo3884f7b2007-11-27 19:28:56 +09001661 * SPEED_DOWN : Speed down transfer speed but don't fall back
1662 * to PIO.
1663 *
1664 * FALLBACK_TO_PIO : Fall back to PIO.
1665 *
1666 * Even if multiple verdicts are returned, only one action is
Tejun Heo76326ac2007-11-27 19:28:59 +09001667 * taken per error. An action triggered by non-DUBIOUS errors
1668 * clears ering, while one triggered by DUBIOUS_* errors doesn't.
1669 * This is to expedite speed down decisions right after device is
1670 * initially configured.
Tejun Heo3884f7b2007-11-27 19:28:56 +09001671 *
Tejun Heo76326ac2007-11-27 19:28:59 +09001672 * The followings are speed down rules. #1 and #2 deal with
1673 * DUBIOUS errors.
1674 *
1675 * 1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors
1676 * occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO.
1677 *
1678 * 2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors
1679 * occurred during last 5 mins, NCQ_OFF.
1680 *
1681 * 3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors
Tejun Heo3884f7b2007-11-27 19:28:56 +09001682 * ocurred during last 5 mins, FALLBACK_TO_PIO
1683 *
Tejun Heo76326ac2007-11-27 19:28:59 +09001684 * 4. If more than 3 TOUT_HSM or UNK_DEV errors occurred
Tejun Heo3884f7b2007-11-27 19:28:56 +09001685 * during last 10 mins, NCQ_OFF.
1686 *
Tejun Heo76326ac2007-11-27 19:28:59 +09001687 * 5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6
Tejun Heo3884f7b2007-11-27 19:28:56 +09001688 * UNK_DEV errors occurred during last 10 mins, SPEED_DOWN.
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001689 *
Tejun Heo022bdb02006-05-15 20:58:22 +09001690 * LOCKING:
1691 * Inherited from caller.
1692 *
1693 * RETURNS:
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001694 * OR of ATA_EH_SPDN_* flags.
Tejun Heo022bdb02006-05-15 20:58:22 +09001695 */
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001696static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
Tejun Heo022bdb02006-05-15 20:58:22 +09001697{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001698 const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
1699 u64 j64 = get_jiffies_64();
1700 struct speed_down_verdict_arg arg;
1701 unsigned int verdict = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09001702
Tejun Heo3884f7b2007-11-27 19:28:56 +09001703 /* scan past 5 mins of error history */
1704 memset(&arg, 0, sizeof(arg));
1705 arg.since = j64 - min(j64, j5mins);
1706 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1707
Tejun Heo76326ac2007-11-27 19:28:59 +09001708 if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] +
1709 arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1)
1710 verdict |= ATA_EH_SPDN_SPEED_DOWN |
1711 ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS;
1712
1713 if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] +
1714 arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1)
1715 verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS;
1716
Tejun Heo3884f7b2007-11-27 19:28:56 +09001717 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1718 arg.nr_errors[ATA_ECAT_TOUT_HSM] +
Tejun Heo663f99b2007-11-27 19:28:57 +09001719 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
Tejun Heo3884f7b2007-11-27 19:28:56 +09001720 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
1721
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001722 /* scan past 10 mins of error history */
Tejun Heo022bdb02006-05-15 20:58:22 +09001723 memset(&arg, 0, sizeof(arg));
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001724 arg.since = j64 - min(j64, j10mins);
1725 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
Tejun Heo022bdb02006-05-15 20:58:22 +09001726
Tejun Heo3884f7b2007-11-27 19:28:56 +09001727 if (arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1728 arg.nr_errors[ATA_ECAT_UNK_DEV] > 3)
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001729 verdict |= ATA_EH_SPDN_NCQ_OFF;
Tejun Heo3884f7b2007-11-27 19:28:56 +09001730
1731 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1732 arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 ||
Tejun Heo663f99b2007-11-27 19:28:57 +09001733 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001734 verdict |= ATA_EH_SPDN_SPEED_DOWN;
Tejun Heo022bdb02006-05-15 20:58:22 +09001735
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001736 return verdict;
Tejun Heo022bdb02006-05-15 20:58:22 +09001737}
1738
1739/**
1740 * ata_eh_speed_down - record error and speed down if necessary
1741 * @dev: Failed device
Tejun Heo3884f7b2007-11-27 19:28:56 +09001742 * @eflags: mask of ATA_EFLAG_* flags
Tejun Heo022bdb02006-05-15 20:58:22 +09001743 * @err_mask: err_mask of the error
1744 *
1745 * Record error and examine error history to determine whether
1746 * adjusting transmission speed is necessary. It also sets
1747 * transmission limits appropriately if such adjustment is
1748 * necessary.
1749 *
1750 * LOCKING:
1751 * Kernel thread context (may sleep).
1752 *
1753 * RETURNS:
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001754 * Determined recovery action.
Tejun Heo022bdb02006-05-15 20:58:22 +09001755 */
Tejun Heo3884f7b2007-11-27 19:28:56 +09001756static unsigned int ata_eh_speed_down(struct ata_device *dev,
1757 unsigned int eflags, unsigned int err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001758{
Tejun Heob1c72912008-07-31 17:02:43 +09001759 struct ata_link *link = ata_dev_phys_link(dev);
Tejun Heo76326ac2007-11-27 19:28:59 +09001760 int xfer_ok = 0;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001761 unsigned int verdict;
1762 unsigned int action = 0;
1763
1764 /* don't bother if Cat-0 error */
Tejun Heo76326ac2007-11-27 19:28:59 +09001765 if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0)
Tejun Heo022bdb02006-05-15 20:58:22 +09001766 return 0;
1767
1768 /* record error and determine whether speed down is necessary */
Tejun Heo3884f7b2007-11-27 19:28:56 +09001769 ata_ering_record(&dev->ering, eflags, err_mask);
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001770 verdict = ata_eh_speed_down_verdict(dev);
Tejun Heo022bdb02006-05-15 20:58:22 +09001771
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001772 /* turn off NCQ? */
1773 if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
1774 (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
1775 ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
1776 dev->flags |= ATA_DFLAG_NCQ_OFF;
1777 ata_dev_printk(dev, KERN_WARNING,
1778 "NCQ disabled due to excessive errors\n");
1779 goto done;
1780 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001781
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001782 /* speed down? */
1783 if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1784 /* speed down SATA link speed if possible */
Tejun Heo3884f7b2007-11-27 19:28:56 +09001785 if (sata_down_spd_limit(link) == 0) {
Tejun Heocf480622008-01-24 00:05:14 +09001786 action |= ATA_EH_RESET;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001787 goto done;
1788 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001789
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001790 /* lower transfer mode */
1791 if (dev->spdn_cnt < 2) {
1792 static const int dma_dnxfer_sel[] =
1793 { ATA_DNXFER_DMA, ATA_DNXFER_40C };
1794 static const int pio_dnxfer_sel[] =
1795 { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
1796 int sel;
Tejun Heo022bdb02006-05-15 20:58:22 +09001797
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001798 if (dev->xfer_shift != ATA_SHIFT_PIO)
1799 sel = dma_dnxfer_sel[dev->spdn_cnt];
1800 else
1801 sel = pio_dnxfer_sel[dev->spdn_cnt];
1802
1803 dev->spdn_cnt++;
1804
1805 if (ata_down_xfermask_limit(dev, sel) == 0) {
Tejun Heocf480622008-01-24 00:05:14 +09001806 action |= ATA_EH_RESET;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001807 goto done;
1808 }
1809 }
1810 }
1811
1812 /* Fall back to PIO? Slowing down to PIO is meaningless for
Tejun Heo663f99b2007-11-27 19:28:57 +09001813 * SATA ATA devices. Consider it only for PATA and SATAPI.
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001814 */
1815 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
Tejun Heo663f99b2007-11-27 19:28:57 +09001816 (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) &&
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001817 (dev->xfer_shift != ATA_SHIFT_PIO)) {
1818 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
1819 dev->spdn_cnt = 0;
Tejun Heocf480622008-01-24 00:05:14 +09001820 action |= ATA_EH_RESET;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001821 goto done;
1822 }
1823 }
1824
Tejun Heo022bdb02006-05-15 20:58:22 +09001825 return 0;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001826 done:
1827 /* device has been slowed down, blow error history */
Tejun Heo76326ac2007-11-27 19:28:59 +09001828 if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS))
1829 ata_ering_clear(&dev->ering);
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001830 return action;
Tejun Heo022bdb02006-05-15 20:58:22 +09001831}
1832
1833/**
Tejun Heo9b1e2652007-08-06 18:36:24 +09001834 * ata_eh_link_autopsy - analyze error and determine recovery action
1835 * @link: host link to perform autopsy on
Tejun Heo022bdb02006-05-15 20:58:22 +09001836 *
Tejun Heo02607312007-08-06 18:36:23 +09001837 * Analyze why @link failed and determine which recovery actions
1838 * are needed. This function also sets more detailed AC_ERR_*
1839 * values and fills sense data for ATAPI CHECK SENSE.
Tejun Heo022bdb02006-05-15 20:58:22 +09001840 *
1841 * LOCKING:
1842 * Kernel thread context (may sleep).
1843 */
Tejun Heo9b1e2652007-08-06 18:36:24 +09001844static void ata_eh_link_autopsy(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001845{
Tejun Heo02607312007-08-06 18:36:23 +09001846 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09001847 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heodfcc1732007-10-31 10:17:05 +09001848 struct ata_device *dev;
Tejun Heo3884f7b2007-11-27 19:28:56 +09001849 unsigned int all_err_mask = 0, eflags = 0;
1850 int tag;
Tejun Heo022bdb02006-05-15 20:58:22 +09001851 u32 serror;
1852 int rc;
1853
1854 DPRINTK("ENTER\n");
1855
Tejun Heo1cdaf532006-07-03 16:07:26 +09001856 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1857 return;
1858
Tejun Heo022bdb02006-05-15 20:58:22 +09001859 /* obtain and analyze SError */
Tejun Heo936fd732007-08-06 18:36:23 +09001860 rc = sata_scr_read(link, SCR_ERROR, &serror);
Tejun Heo022bdb02006-05-15 20:58:22 +09001861 if (rc == 0) {
1862 ehc->i.serror |= serror;
Tejun Heo02607312007-08-06 18:36:23 +09001863 ata_eh_analyze_serror(link);
Tejun Heo4e57c512007-07-16 14:29:41 +09001864 } else if (rc != -EOPNOTSUPP) {
Tejun Heocf480622008-01-24 00:05:14 +09001865 /* SError read failed, force reset and probing */
Tejun Heob558edd2008-01-24 00:05:14 +09001866 ehc->i.probe_mask |= ATA_ALL_DEVICES;
Tejun Heocf480622008-01-24 00:05:14 +09001867 ehc->i.action |= ATA_EH_RESET;
Tejun Heo4e57c512007-07-16 14:29:41 +09001868 ehc->i.err_mask |= AC_ERR_OTHER;
1869 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001870
Tejun Heoe8ee8452006-05-15 21:03:46 +09001871 /* analyze NCQ failure */
Tejun Heo02607312007-08-06 18:36:23 +09001872 ata_eh_analyze_ncq_error(link);
Tejun Heoe8ee8452006-05-15 21:03:46 +09001873
Tejun Heo022bdb02006-05-15 20:58:22 +09001874 /* any real error trumps AC_ERR_OTHER */
1875 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1876 ehc->i.err_mask &= ~AC_ERR_OTHER;
1877
1878 all_err_mask |= ehc->i.err_mask;
1879
1880 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1881 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1882
Tejun Heob1c72912008-07-31 17:02:43 +09001883 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
1884 ata_dev_phys_link(qc->dev) != link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001885 continue;
1886
1887 /* inherit upper level err_mask */
1888 qc->err_mask |= ehc->i.err_mask;
1889
Tejun Heo022bdb02006-05-15 20:58:22 +09001890 /* analyze TF */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001891 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
Tejun Heo022bdb02006-05-15 20:58:22 +09001892
1893 /* DEV errors are probably spurious in case of ATA_BUS error */
1894 if (qc->err_mask & AC_ERR_ATA_BUS)
1895 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1896 AC_ERR_INVALID);
1897
1898 /* any real error trumps unknown error */
1899 if (qc->err_mask & ~AC_ERR_OTHER)
1900 qc->err_mask &= ~AC_ERR_OTHER;
1901
1902 /* SENSE_VALID trumps dev/unknown error and revalidation */
Tejun Heof90f0822007-10-26 16:12:41 +09001903 if (qc->flags & ATA_QCFLAG_SENSE_VALID)
Tejun Heo022bdb02006-05-15 20:58:22 +09001904 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
Tejun Heo022bdb02006-05-15 20:58:22 +09001905
Tejun Heo03faab72008-03-27 19:14:24 +09001906 /* determine whether the command is worth retrying */
1907 if (!(qc->err_mask & AC_ERR_INVALID) &&
1908 ((qc->flags & ATA_QCFLAG_IO) || qc->err_mask != AC_ERR_DEV))
1909 qc->flags |= ATA_QCFLAG_RETRY;
1910
Tejun Heo022bdb02006-05-15 20:58:22 +09001911 /* accumulate error info */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001912 ehc->i.dev = qc->dev;
Tejun Heo022bdb02006-05-15 20:58:22 +09001913 all_err_mask |= qc->err_mask;
1914 if (qc->flags & ATA_QCFLAG_IO)
Tejun Heo3884f7b2007-11-27 19:28:56 +09001915 eflags |= ATA_EFLAG_IS_IO;
Tejun Heo022bdb02006-05-15 20:58:22 +09001916 }
1917
Tejun Heoa20f33f2006-05-16 12:58:24 +09001918 /* enforce default EH actions */
Tejun Heob51e9e52006-06-29 01:29:30 +09001919 if (ap->pflags & ATA_PFLAG_FROZEN ||
Tejun Heoa20f33f2006-05-16 12:58:24 +09001920 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
Tejun Heocf480622008-01-24 00:05:14 +09001921 ehc->i.action |= ATA_EH_RESET;
Tejun Heo3884f7b2007-11-27 19:28:56 +09001922 else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) ||
1923 (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV)))
Tejun Heo4528e4d2006-07-08 20:17:26 +09001924 ehc->i.action |= ATA_EH_REVALIDATE;
Tejun Heo022bdb02006-05-15 20:58:22 +09001925
Tejun Heodfcc1732007-10-31 10:17:05 +09001926 /* If we have offending qcs and the associated failed device,
1927 * perform per-dev EH action only on the offending device.
1928 */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001929 if (ehc->i.dev) {
Tejun Heo4528e4d2006-07-08 20:17:26 +09001930 ehc->i.dev_action[ehc->i.dev->devno] |=
1931 ehc->i.action & ATA_EH_PERDEV_MASK;
1932 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
Tejun Heo47005f22006-06-19 18:27:23 +09001933 }
1934
Tejun Heo2695e362008-01-10 13:41:23 +09001935 /* propagate timeout to host link */
1936 if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link))
1937 ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT;
1938
1939 /* record error and consider speeding down */
Tejun Heodfcc1732007-10-31 10:17:05 +09001940 dev = ehc->i.dev;
Tejun Heo2695e362008-01-10 13:41:23 +09001941 if (!dev && ((ata_link_max_devices(link) == 1 &&
1942 ata_dev_enabled(link->device))))
1943 dev = link->device;
Tejun Heodfcc1732007-10-31 10:17:05 +09001944
Tejun Heo76326ac2007-11-27 19:28:59 +09001945 if (dev) {
1946 if (dev->flags & ATA_DFLAG_DUBIOUS_XFER)
1947 eflags |= ATA_EFLAG_DUBIOUS_XFER;
Tejun Heo3884f7b2007-11-27 19:28:56 +09001948 ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask);
Tejun Heo76326ac2007-11-27 19:28:59 +09001949 }
Tejun Heodfcc1732007-10-31 10:17:05 +09001950
Tejun Heo022bdb02006-05-15 20:58:22 +09001951 DPRINTK("EXIT\n");
1952}
1953
1954/**
Tejun Heo9b1e2652007-08-06 18:36:24 +09001955 * ata_eh_autopsy - analyze error and determine recovery action
1956 * @ap: host port to perform autopsy on
1957 *
1958 * Analyze all links of @ap and determine why they failed and
1959 * which recovery actions are needed.
1960 *
1961 * LOCKING:
1962 * Kernel thread context (may sleep).
1963 */
Tejun Heofb7fd612007-09-23 13:14:12 +09001964void ata_eh_autopsy(struct ata_port *ap)
Tejun Heo9b1e2652007-08-06 18:36:24 +09001965{
1966 struct ata_link *link;
1967
Tejun Heo2695e362008-01-10 13:41:23 +09001968 ata_port_for_each_link(link, ap)
Tejun Heo9b1e2652007-08-06 18:36:24 +09001969 ata_eh_link_autopsy(link);
Tejun Heo2695e362008-01-10 13:41:23 +09001970
Tejun Heob1c72912008-07-31 17:02:43 +09001971 /* Handle the frigging slave link. Autopsy is done similarly
1972 * but actions and flags are transferred over to the master
1973 * link and handled from there.
1974 */
1975 if (ap->slave_link) {
1976 struct ata_eh_context *mehc = &ap->link.eh_context;
1977 struct ata_eh_context *sehc = &ap->slave_link->eh_context;
1978
1979 ata_eh_link_autopsy(ap->slave_link);
1980
1981 ata_eh_about_to_do(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
1982 mehc->i.action |= sehc->i.action;
1983 mehc->i.dev_action[1] |= sehc->i.dev_action[1];
1984 mehc->i.flags |= sehc->i.flags;
1985 ata_eh_done(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
1986 }
1987
Tejun Heo2695e362008-01-10 13:41:23 +09001988 /* Autopsy of fanout ports can affect host link autopsy.
1989 * Perform host link autopsy last.
1990 */
Tejun Heo071f44b2008-04-07 22:47:22 +09001991 if (sata_pmp_attached(ap))
Tejun Heo2695e362008-01-10 13:41:23 +09001992 ata_eh_link_autopsy(&ap->link);
Tejun Heo9b1e2652007-08-06 18:36:24 +09001993}
1994
1995/**
1996 * ata_eh_link_report - report error handling to user
Tejun Heo02607312007-08-06 18:36:23 +09001997 * @link: ATA link EH is going on
Tejun Heo022bdb02006-05-15 20:58:22 +09001998 *
1999 * Report EH to user.
2000 *
2001 * LOCKING:
2002 * None.
2003 */
Tejun Heo9b1e2652007-08-06 18:36:24 +09002004static void ata_eh_link_report(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09002005{
Tejun Heo02607312007-08-06 18:36:23 +09002006 struct ata_port *ap = link->ap;
2007 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09002008 const char *frozen, *desc;
Tejun Heoa1e10f72007-08-18 13:28:49 +09002009 char tries_buf[6];
Tejun Heo022bdb02006-05-15 20:58:22 +09002010 int tag, nr_failed = 0;
2011
Tejun Heo94ff3d52007-10-09 14:57:56 +09002012 if (ehc->i.flags & ATA_EHI_QUIET)
2013 return;
2014
Tejun Heo022bdb02006-05-15 20:58:22 +09002015 desc = NULL;
2016 if (ehc->i.desc[0] != '\0')
2017 desc = ehc->i.desc;
2018
2019 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2020 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2021
Tejun Heob1c72912008-07-31 17:02:43 +09002022 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
2023 ata_dev_phys_link(qc->dev) != link ||
Tejun Heoe027bd32007-10-26 16:19:26 +09002024 ((qc->flags & ATA_QCFLAG_QUIET) &&
2025 qc->err_mask == AC_ERR_DEV))
Tejun Heo022bdb02006-05-15 20:58:22 +09002026 continue;
2027 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
2028 continue;
2029
2030 nr_failed++;
2031 }
2032
2033 if (!nr_failed && !ehc->i.err_mask)
2034 return;
2035
2036 frozen = "";
Tejun Heob51e9e52006-06-29 01:29:30 +09002037 if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heo022bdb02006-05-15 20:58:22 +09002038 frozen = " frozen";
2039
Tejun Heoa1e10f72007-08-18 13:28:49 +09002040 memset(tries_buf, 0, sizeof(tries_buf));
2041 if (ap->eh_tries < ATA_EH_MAX_TRIES)
2042 snprintf(tries_buf, sizeof(tries_buf) - 1, " t%d",
2043 ap->eh_tries);
2044
Tejun Heo022bdb02006-05-15 20:58:22 +09002045 if (ehc->i.dev) {
Tejun Heoe8ee8452006-05-15 21:03:46 +09002046 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
Tejun Heoa1e10f72007-08-18 13:28:49 +09002047 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2048 ehc->i.err_mask, link->sactive, ehc->i.serror,
2049 ehc->i.action, frozen, tries_buf);
Tejun Heo022bdb02006-05-15 20:58:22 +09002050 if (desc)
Tejun Heob64bbc32007-07-16 14:29:39 +09002051 ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
Tejun Heo022bdb02006-05-15 20:58:22 +09002052 } else {
Tejun Heo02607312007-08-06 18:36:23 +09002053 ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
Tejun Heoa1e10f72007-08-18 13:28:49 +09002054 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2055 ehc->i.err_mask, link->sactive, ehc->i.serror,
2056 ehc->i.action, frozen, tries_buf);
Tejun Heo022bdb02006-05-15 20:58:22 +09002057 if (desc)
Tejun Heo02607312007-08-06 18:36:23 +09002058 ata_link_printk(link, KERN_ERR, "%s\n", desc);
Tejun Heo022bdb02006-05-15 20:58:22 +09002059 }
2060
Robert Hancock1333e192007-10-02 11:22:02 -04002061 if (ehc->i.serror)
Tejun Heoda0e21d2008-07-31 16:08:38 +09002062 ata_link_printk(link, KERN_ERR,
Robert Hancock1333e192007-10-02 11:22:02 -04002063 "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
2064 ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
2065 ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
2066 ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
2067 ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
2068 ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
2069 ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
2070 ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
2071 ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
2072 ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
2073 ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
2074 ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
2075 ehc->i.serror & SERR_CRC ? "BadCRC " : "",
2076 ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
2077 ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
2078 ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
2079 ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002080 ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
Robert Hancock1333e192007-10-02 11:22:02 -04002081
Tejun Heo022bdb02006-05-15 20:58:22 +09002082 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2083 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
Tejun Heo8a937582006-11-14 22:36:12 +09002084 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
Tejun Heoabb6a882007-11-28 23:16:09 +09002085 const u8 *cdb = qc->cdb;
2086 char data_buf[20] = "";
2087 char cdb_buf[70] = "";
Tejun Heo022bdb02006-05-15 20:58:22 +09002088
Tejun Heo02607312007-08-06 18:36:23 +09002089 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
Tejun Heob1c72912008-07-31 17:02:43 +09002090 ata_dev_phys_link(qc->dev) != link || !qc->err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09002091 continue;
2092
Tejun Heoabb6a882007-11-28 23:16:09 +09002093 if (qc->dma_dir != DMA_NONE) {
2094 static const char *dma_str[] = {
2095 [DMA_BIDIRECTIONAL] = "bidi",
2096 [DMA_TO_DEVICE] = "out",
2097 [DMA_FROM_DEVICE] = "in",
2098 };
2099 static const char *prot_str[] = {
2100 [ATA_PROT_PIO] = "pio",
2101 [ATA_PROT_DMA] = "dma",
2102 [ATA_PROT_NCQ] = "ncq",
Tejun Heo0dc36882007-12-18 16:34:43 -05002103 [ATAPI_PROT_PIO] = "pio",
2104 [ATAPI_PROT_DMA] = "dma",
Tejun Heoabb6a882007-11-28 23:16:09 +09002105 };
2106
2107 snprintf(data_buf, sizeof(data_buf), " %s %u %s",
2108 prot_str[qc->tf.protocol], qc->nbytes,
2109 dma_str[qc->dma_dir]);
2110 }
2111
Jeff Garzike39eec12007-12-01 18:05:39 -05002112 if (ata_is_atapi(qc->tf.protocol))
Tejun Heoabb6a882007-11-28 23:16:09 +09002113 snprintf(cdb_buf, sizeof(cdb_buf),
2114 "cdb %02x %02x %02x %02x %02x %02x %02x %02x "
2115 "%02x %02x %02x %02x %02x %02x %02x %02x\n ",
2116 cdb[0], cdb[1], cdb[2], cdb[3],
2117 cdb[4], cdb[5], cdb[6], cdb[7],
2118 cdb[8], cdb[9], cdb[10], cdb[11],
2119 cdb[12], cdb[13], cdb[14], cdb[15]);
2120
Tejun Heo8a937582006-11-14 22:36:12 +09002121 ata_dev_printk(qc->dev, KERN_ERR,
2122 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
Tejun Heoabb6a882007-11-28 23:16:09 +09002123 "tag %d%s\n %s"
Tejun Heo8a937582006-11-14 22:36:12 +09002124 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
Tejun Heo5335b722007-07-16 14:29:40 +09002125 "Emask 0x%x (%s)%s\n",
Tejun Heo8a937582006-11-14 22:36:12 +09002126 cmd->command, cmd->feature, cmd->nsect,
2127 cmd->lbal, cmd->lbam, cmd->lbah,
2128 cmd->hob_feature, cmd->hob_nsect,
2129 cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
Tejun Heoabb6a882007-11-28 23:16:09 +09002130 cmd->device, qc->tag, data_buf, cdb_buf,
Tejun Heo8a937582006-11-14 22:36:12 +09002131 res->command, res->feature, res->nsect,
2132 res->lbal, res->lbam, res->lbah,
2133 res->hob_feature, res->hob_nsect,
2134 res->hob_lbal, res->hob_lbam, res->hob_lbah,
Tejun Heo5335b722007-07-16 14:29:40 +09002135 res->device, qc->err_mask, ata_err_string(qc->err_mask),
2136 qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
Robert Hancock1333e192007-10-02 11:22:02 -04002137
2138 if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002139 ATA_ERR)) {
Robert Hancock1333e192007-10-02 11:22:02 -04002140 if (res->command & ATA_BUSY)
2141 ata_dev_printk(qc->dev, KERN_ERR,
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002142 "status: { Busy }\n");
Robert Hancock1333e192007-10-02 11:22:02 -04002143 else
2144 ata_dev_printk(qc->dev, KERN_ERR,
2145 "status: { %s%s%s%s}\n",
2146 res->command & ATA_DRDY ? "DRDY " : "",
2147 res->command & ATA_DF ? "DF " : "",
2148 res->command & ATA_DRQ ? "DRQ " : "",
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002149 res->command & ATA_ERR ? "ERR " : "");
Robert Hancock1333e192007-10-02 11:22:02 -04002150 }
2151
2152 if (cmd->command != ATA_CMD_PACKET &&
2153 (res->feature & (ATA_ICRC | ATA_UNC | ATA_IDNF |
2154 ATA_ABORTED)))
2155 ata_dev_printk(qc->dev, KERN_ERR,
2156 "error: { %s%s%s%s}\n",
2157 res->feature & ATA_ICRC ? "ICRC " : "",
2158 res->feature & ATA_UNC ? "UNC " : "",
2159 res->feature & ATA_IDNF ? "IDNF " : "",
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002160 res->feature & ATA_ABORTED ? "ABRT " : "");
Tejun Heo022bdb02006-05-15 20:58:22 +09002161 }
2162}
2163
Tejun Heo9b1e2652007-08-06 18:36:24 +09002164/**
2165 * ata_eh_report - report error handling to user
2166 * @ap: ATA port to report EH about
2167 *
2168 * Report EH to user.
2169 *
2170 * LOCKING:
2171 * None.
2172 */
Tejun Heofb7fd612007-09-23 13:14:12 +09002173void ata_eh_report(struct ata_port *ap)
Tejun Heo9b1e2652007-08-06 18:36:24 +09002174{
2175 struct ata_link *link;
2176
2177 __ata_port_for_each_link(link, ap)
2178 ata_eh_link_report(link);
2179}
2180
Tejun Heocc0680a2007-08-06 18:36:23 +09002181static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
Tejun Heob1c72912008-07-31 17:02:43 +09002182 unsigned int *classes, unsigned long deadline,
2183 bool clear_classes)
Tejun Heod87fa382006-05-31 18:28:24 +09002184{
Tejun Heof58229f2007-08-06 18:36:23 +09002185 struct ata_device *dev;
Tejun Heod87fa382006-05-31 18:28:24 +09002186
Tejun Heob1c72912008-07-31 17:02:43 +09002187 if (clear_classes)
2188 ata_link_for_each_dev(dev, link)
2189 classes[dev->devno] = ATA_DEV_UNKNOWN;
Tejun Heod87fa382006-05-31 18:28:24 +09002190
Tejun Heof0465192008-05-19 01:15:08 +09002191 return reset(link, classes, deadline);
Tejun Heod87fa382006-05-31 18:28:24 +09002192}
2193
Tejun Heoae791c02007-09-23 13:14:12 +09002194static int ata_eh_followup_srst_needed(struct ata_link *link,
Tejun Heo5dbfc9c2008-07-31 16:08:02 +09002195 int rc, const unsigned int *classes)
Tejun Heo664faf02006-05-31 18:27:50 +09002196{
Tejun Heo45db2f62008-04-08 01:46:56 +09002197 if ((link->flags & ATA_LFLAG_NO_SRST) || ata_link_offline(link))
Tejun Heoae791c02007-09-23 13:14:12 +09002198 return 0;
Tejun Heo5dbfc9c2008-07-31 16:08:02 +09002199 if (rc == -EAGAIN)
2200 return 1;
Tejun Heo071f44b2008-04-07 22:47:22 +09002201 if (sata_pmp_supported(link->ap) && ata_is_host_link(link))
Tejun Heo3495de72007-09-23 13:19:53 +09002202 return 1;
Tejun Heo664faf02006-05-31 18:27:50 +09002203 return 0;
2204}
2205
Tejun Heofb7fd612007-09-23 13:14:12 +09002206int ata_eh_reset(struct ata_link *link, int classify,
2207 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
2208 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
Tejun Heo022bdb02006-05-15 20:58:22 +09002209{
Tejun Heoafaa5c32007-10-09 15:06:10 +09002210 struct ata_port *ap = link->ap;
Tejun Heob1c72912008-07-31 17:02:43 +09002211 struct ata_link *slave = ap->slave_link;
Tejun Heo936fd732007-08-06 18:36:23 +09002212 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heob1c72912008-07-31 17:02:43 +09002213 struct ata_eh_context *sehc = &slave->eh_context;
Tejun Heo664faf02006-05-31 18:27:50 +09002214 unsigned int *classes = ehc->classes;
Tejun Heo416dc9e2007-10-31 10:17:03 +09002215 unsigned int lflags = link->flags;
Tejun Heo1cdaf532006-07-03 16:07:26 +09002216 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
Tejun Heod8af0eb2008-05-20 02:17:53 +09002217 int max_tries = 0, try = 0;
Tejun Heob1c72912008-07-31 17:02:43 +09002218 struct ata_link *failed_link;
Tejun Heof58229f2007-08-06 18:36:23 +09002219 struct ata_device *dev;
Tejun Heo416dc9e2007-10-31 10:17:03 +09002220 unsigned long deadline, now;
Tejun Heo022bdb02006-05-15 20:58:22 +09002221 ata_reset_fn_t reset;
Tejun Heoafaa5c32007-10-09 15:06:10 +09002222 unsigned long flags;
Tejun Heo416dc9e2007-10-31 10:17:03 +09002223 u32 sstatus;
Tejun Heob1c72912008-07-31 17:02:43 +09002224 int nr_unknown, rc;
Tejun Heo022bdb02006-05-15 20:58:22 +09002225
Tejun Heo932648b2008-05-19 01:15:06 +09002226 /*
2227 * Prepare to reset
2228 */
Tejun Heod8af0eb2008-05-20 02:17:53 +09002229 while (ata_eh_reset_timeouts[max_tries] != ULONG_MAX)
2230 max_tries++;
Tejun Heo05944bd2008-08-13 20:19:09 +09002231 if (link->flags & ATA_LFLAG_NO_HRST)
2232 hardreset = NULL;
2233 if (link->flags & ATA_LFLAG_NO_SRST)
2234 softreset = NULL;
Tejun Heod8af0eb2008-05-20 02:17:53 +09002235
Tejun Heo0a2c0f52008-05-20 02:17:52 +09002236 now = jiffies;
2237 deadline = ata_deadline(ehc->last_reset, ATA_EH_RESET_COOL_DOWN);
2238 if (time_before(now, deadline))
2239 schedule_timeout_uninterruptible(deadline - now);
2240
Tejun Heoafaa5c32007-10-09 15:06:10 +09002241 spin_lock_irqsave(ap->lock, flags);
2242 ap->pflags |= ATA_PFLAG_RESETTING;
2243 spin_unlock_irqrestore(ap->lock, flags);
2244
Tejun Heocf480622008-01-24 00:05:14 +09002245 ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
Tejun Heo0a2c0f52008-05-20 02:17:52 +09002246 ehc->last_reset = jiffies;
Tejun Heo13abf502006-07-10 23:18:46 +09002247
Tejun Heocdeab112007-10-29 16:41:09 +09002248 ata_link_for_each_dev(dev, link) {
2249 /* If we issue an SRST then an ATA drive (not ATAPI)
2250 * may change configuration and be in PIO0 timing. If
2251 * we do a hard reset (or are coming from power on)
2252 * this is true for ATA or ATAPI. Until we've set a
2253 * suitable controller mode we should not touch the
2254 * bus as we may be talking too fast.
2255 */
2256 dev->pio_mode = XFER_PIO_0;
2257
2258 /* If the controller has a pio mode setup function
2259 * then use it to set the chipset to rights. Don't
2260 * touch the DMA setup as that will be dealt with when
2261 * configuring devices.
2262 */
2263 if (ap->ops->set_piomode)
2264 ap->ops->set_piomode(ap, dev);
2265 }
2266
Tejun Heocf480622008-01-24 00:05:14 +09002267 /* prefer hardreset */
Tejun Heo932648b2008-05-19 01:15:06 +09002268 reset = NULL;
Tejun Heocf480622008-01-24 00:05:14 +09002269 ehc->i.action &= ~ATA_EH_RESET;
2270 if (hardreset) {
2271 reset = hardreset;
Tejun Heoa6740502008-07-31 16:07:04 +09002272 ehc->i.action |= ATA_EH_HARDRESET;
Tejun Heo4f7faa32008-01-30 18:18:26 +09002273 } else if (softreset) {
Tejun Heocf480622008-01-24 00:05:14 +09002274 reset = softreset;
Tejun Heoa6740502008-07-31 16:07:04 +09002275 ehc->i.action |= ATA_EH_SOFTRESET;
Tejun Heocf480622008-01-24 00:05:14 +09002276 }
Tejun Heof5914a42006-05-31 18:27:48 +09002277
2278 if (prereset) {
Tejun Heob1c72912008-07-31 17:02:43 +09002279 unsigned long deadline = ata_deadline(jiffies,
2280 ATA_EH_PRERESET_TIMEOUT);
2281
2282 if (slave) {
2283 sehc->i.action &= ~ATA_EH_RESET;
2284 sehc->i.action |= ehc->i.action;
2285 }
2286
2287 rc = prereset(link, deadline);
2288
2289 /* If present, do prereset on slave link too. Reset
2290 * is skipped iff both master and slave links report
2291 * -ENOENT or clear ATA_EH_RESET.
2292 */
2293 if (slave && (rc == 0 || rc == -ENOENT)) {
2294 int tmp;
2295
2296 tmp = prereset(slave, deadline);
2297 if (tmp != -ENOENT)
2298 rc = tmp;
2299
2300 ehc->i.action |= sehc->i.action;
2301 }
2302
Tejun Heof5914a42006-05-31 18:27:48 +09002303 if (rc) {
Alan Coxc9619222006-09-26 17:53:38 +01002304 if (rc == -ENOENT) {
Tejun Heocc0680a2007-08-06 18:36:23 +09002305 ata_link_printk(link, KERN_DEBUG,
Tejun Heo4aa9ab62007-03-12 17:24:08 +09002306 "port disabled. ignoring.\n");
Tejun Heocf480622008-01-24 00:05:14 +09002307 ehc->i.action &= ~ATA_EH_RESET;
Tejun Heo4aa9ab62007-03-12 17:24:08 +09002308
Tejun Heo936fd732007-08-06 18:36:23 +09002309 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09002310 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heo4aa9ab62007-03-12 17:24:08 +09002311
2312 rc = 0;
Alan Coxc9619222006-09-26 17:53:38 +01002313 } else
Tejun Heocc0680a2007-08-06 18:36:23 +09002314 ata_link_printk(link, KERN_ERR,
Tejun Heof5914a42006-05-31 18:27:48 +09002315 "prereset failed (errno=%d)\n", rc);
Tejun Heofccb6ea2007-07-16 14:29:41 +09002316 goto out;
Tejun Heof5914a42006-05-31 18:27:48 +09002317 }
Tejun Heof5914a42006-05-31 18:27:48 +09002318
Tejun Heo932648b2008-05-19 01:15:06 +09002319 /* prereset() might have cleared ATA_EH_RESET. If so,
2320 * bang classes and return.
2321 */
2322 if (reset && !(ehc->i.action & ATA_EH_RESET)) {
2323 ata_link_for_each_dev(dev, link)
2324 classes[dev->devno] = ATA_DEV_NONE;
2325 rc = 0;
2326 goto out;
2327 }
Tejun Heof5914a42006-05-31 18:27:48 +09002328 }
2329
Tejun Heo022bdb02006-05-15 20:58:22 +09002330 retry:
Tejun Heo932648b2008-05-19 01:15:06 +09002331 /*
2332 * Perform reset
2333 */
Tejun Heo0a2c0f52008-05-20 02:17:52 +09002334 ehc->last_reset = jiffies;
Tejun Heodc98c322008-05-19 01:15:07 +09002335 if (ata_is_host_link(link))
2336 ata_eh_freeze_port(ap);
2337
Tejun Heo341c2c92008-05-20 02:17:51 +09002338 deadline = ata_deadline(jiffies, ata_eh_reset_timeouts[try++]);
Tejun Heo31daabd2007-02-02 16:50:52 +09002339
Tejun Heo932648b2008-05-19 01:15:06 +09002340 if (reset) {
2341 if (verbose)
2342 ata_link_printk(link, KERN_INFO, "%s resetting link\n",
2343 reset == softreset ? "soft" : "hard");
Tejun Heo022bdb02006-05-15 20:58:22 +09002344
Tejun Heo932648b2008-05-19 01:15:06 +09002345 /* mark that this EH session started with reset */
2346 if (reset == hardreset)
2347 ehc->i.flags |= ATA_EHI_DID_HARDRESET;
2348 else
2349 ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09002350
Tejun Heob1c72912008-07-31 17:02:43 +09002351 rc = ata_do_reset(link, reset, classes, deadline, true);
2352 if (rc && rc != -EAGAIN) {
2353 failed_link = link;
Tejun Heo5dbfc9c2008-07-31 16:08:02 +09002354 goto fail;
Tejun Heob1c72912008-07-31 17:02:43 +09002355 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002356
Tejun Heob1c72912008-07-31 17:02:43 +09002357 /* hardreset slave link if existent */
2358 if (slave && reset == hardreset) {
2359 int tmp;
2360
2361 if (verbose)
2362 ata_link_printk(slave, KERN_INFO,
2363 "hard resetting link\n");
2364
2365 ata_eh_about_to_do(slave, NULL, ATA_EH_RESET);
2366 tmp = ata_do_reset(slave, reset, classes, deadline,
2367 false);
2368 switch (tmp) {
2369 case -EAGAIN:
2370 rc = -EAGAIN;
2371 case 0:
2372 break;
2373 default:
2374 failed_link = slave;
2375 rc = tmp;
2376 goto fail;
2377 }
2378 }
2379
2380 /* perform follow-up SRST if necessary */
Tejun Heo932648b2008-05-19 01:15:06 +09002381 if (reset == hardreset &&
Tejun Heo5dbfc9c2008-07-31 16:08:02 +09002382 ata_eh_followup_srst_needed(link, rc, classes)) {
Tejun Heo932648b2008-05-19 01:15:06 +09002383 reset = softreset;
Tejun Heo664faf02006-05-31 18:27:50 +09002384
Tejun Heo932648b2008-05-19 01:15:06 +09002385 if (!reset) {
2386 ata_link_printk(link, KERN_ERR,
2387 "follow-up softreset required "
2388 "but no softreset avaliable\n");
Tejun Heob1c72912008-07-31 17:02:43 +09002389 failed_link = link;
Tejun Heo932648b2008-05-19 01:15:06 +09002390 rc = -EINVAL;
2391 goto fail;
2392 }
2393
2394 ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
Tejun Heob1c72912008-07-31 17:02:43 +09002395 rc = ata_do_reset(link, reset, classes, deadline, true);
Tejun Heo664faf02006-05-31 18:27:50 +09002396 }
Tejun Heo932648b2008-05-19 01:15:06 +09002397 } else {
2398 if (verbose)
2399 ata_link_printk(link, KERN_INFO, "no reset method "
2400 "available, skipping reset\n");
2401 if (!(lflags & ATA_LFLAG_ASSUME_CLASS))
2402 lflags |= ATA_LFLAG_ASSUME_ATA;
Tejun Heo664faf02006-05-31 18:27:50 +09002403 }
2404
Tejun Heo932648b2008-05-19 01:15:06 +09002405 /*
2406 * Post-reset processing
2407 */
Tejun Heo416dc9e2007-10-31 10:17:03 +09002408 ata_link_for_each_dev(dev, link) {
2409 /* After the reset, the device state is PIO 0 and the
2410 * controller state is undefined. Reset also wakes up
2411 * drives from sleeping mode.
2412 */
2413 dev->pio_mode = XFER_PIO_0;
2414 dev->flags &= ~ATA_DFLAG_SLEEPING;
Tejun Heo664faf02006-05-31 18:27:50 +09002415
Tejun Heob1c72912008-07-31 17:02:43 +09002416 if (ata_phys_link_offline(ata_dev_phys_link(dev)))
Tejun Heo416dc9e2007-10-31 10:17:03 +09002417 continue;
Tejun Heo664faf02006-05-31 18:27:50 +09002418
Tejun Heo4ccd3322008-01-08 20:26:12 +09002419 /* apply class override */
Tejun Heo416dc9e2007-10-31 10:17:03 +09002420 if (lflags & ATA_LFLAG_ASSUME_ATA)
2421 classes[dev->devno] = ATA_DEV_ATA;
2422 else if (lflags & ATA_LFLAG_ASSUME_SEMB)
2423 classes[dev->devno] = ATA_DEV_SEMB_UNSUP; /* not yet */
Tejun Heo022bdb02006-05-15 20:58:22 +09002424 }
2425
Tejun Heo416dc9e2007-10-31 10:17:03 +09002426 /* record current link speed */
2427 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2428 link->sata_spd = (sstatus >> 4) & 0xf;
Tejun Heob1c72912008-07-31 17:02:43 +09002429 if (slave && sata_scr_read(slave, SCR_STATUS, &sstatus) == 0)
2430 slave->sata_spd = (sstatus >> 4) & 0xf;
Tejun Heo008a7892007-07-16 14:29:40 +09002431
Tejun Heodc98c322008-05-19 01:15:07 +09002432 /* thaw the port */
2433 if (ata_is_host_link(link))
2434 ata_eh_thaw_port(ap);
2435
Tejun Heof0465192008-05-19 01:15:08 +09002436 /* postreset() should clear hardware SError. Although SError
2437 * is cleared during link resume, clearing SError here is
2438 * necessary as some PHYs raise hotplug events after SRST.
2439 * This introduces race condition where hotplug occurs between
2440 * reset and here. This race is mediated by cross checking
2441 * link onlineness and classification result later.
2442 */
Tejun Heob1c72912008-07-31 17:02:43 +09002443 if (postreset) {
Tejun Heo416dc9e2007-10-31 10:17:03 +09002444 postreset(link, classes);
Tejun Heob1c72912008-07-31 17:02:43 +09002445 if (slave)
2446 postreset(slave, classes);
2447 }
Tejun Heo20952b62006-05-31 18:27:23 +09002448
Tejun Heof0465192008-05-19 01:15:08 +09002449 /* clear cached SError */
2450 spin_lock_irqsave(link->ap->lock, flags);
2451 link->eh_info.serror = 0;
Tejun Heob1c72912008-07-31 17:02:43 +09002452 if (slave)
2453 slave->eh_info.serror = 0;
Tejun Heof0465192008-05-19 01:15:08 +09002454 spin_unlock_irqrestore(link->ap->lock, flags);
2455
2456 /* Make sure onlineness and classification result correspond.
2457 * Hotplug could have happened during reset and some
2458 * controllers fail to wait while a drive is spinning up after
2459 * being hotplugged causing misdetection. By cross checking
2460 * link onlineness and classification result, those conditions
2461 * can be reliably detected and retried.
2462 */
Tejun Heob1c72912008-07-31 17:02:43 +09002463 nr_unknown = 0;
Tejun Heof0465192008-05-19 01:15:08 +09002464 ata_link_for_each_dev(dev, link) {
2465 /* convert all ATA_DEV_UNKNOWN to ATA_DEV_NONE */
Tejun Heob1c72912008-07-31 17:02:43 +09002466 if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
Tejun Heof0465192008-05-19 01:15:08 +09002467 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heob1c72912008-07-31 17:02:43 +09002468 if (ata_phys_link_online(ata_dev_phys_link(dev)))
2469 nr_unknown++;
2470 }
Tejun Heof0465192008-05-19 01:15:08 +09002471 }
2472
Tejun Heob1c72912008-07-31 17:02:43 +09002473 if (classify && nr_unknown) {
Tejun Heof0465192008-05-19 01:15:08 +09002474 if (try < max_tries) {
2475 ata_link_printk(link, KERN_WARNING, "link online but "
2476 "device misclassified, retrying\n");
Tejun Heob1c72912008-07-31 17:02:43 +09002477 failed_link = link;
Tejun Heof0465192008-05-19 01:15:08 +09002478 rc = -EAGAIN;
2479 goto fail;
2480 }
2481 ata_link_printk(link, KERN_WARNING,
2482 "link online but device misclassified, "
2483 "device detection might fail\n");
2484 }
2485
Tejun Heo416dc9e2007-10-31 10:17:03 +09002486 /* reset successful, schedule revalidation */
Tejun Heocf480622008-01-24 00:05:14 +09002487 ata_eh_done(link, NULL, ATA_EH_RESET);
Tejun Heob1c72912008-07-31 17:02:43 +09002488 if (slave)
2489 ata_eh_done(slave, NULL, ATA_EH_RESET);
Tejun Heo0a2c0f52008-05-20 02:17:52 +09002490 ehc->last_reset = jiffies;
Tejun Heo416dc9e2007-10-31 10:17:03 +09002491 ehc->i.action |= ATA_EH_REVALIDATE;
Tejun Heoae791c02007-09-23 13:14:12 +09002492
Tejun Heo416dc9e2007-10-31 10:17:03 +09002493 rc = 0;
Tejun Heofccb6ea2007-07-16 14:29:41 +09002494 out:
2495 /* clear hotplug flag */
2496 ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
Tejun Heob1c72912008-07-31 17:02:43 +09002497 if (slave)
2498 sehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
Tejun Heoafaa5c32007-10-09 15:06:10 +09002499
2500 spin_lock_irqsave(ap->lock, flags);
2501 ap->pflags &= ~ATA_PFLAG_RESETTING;
2502 spin_unlock_irqrestore(ap->lock, flags);
2503
Tejun Heo022bdb02006-05-15 20:58:22 +09002504 return rc;
Tejun Heo416dc9e2007-10-31 10:17:03 +09002505
2506 fail:
Tejun Heo5958e302008-04-07 22:47:20 +09002507 /* if SCR isn't accessible on a fan-out port, PMP needs to be reset */
2508 if (!ata_is_host_link(link) &&
2509 sata_scr_read(link, SCR_STATUS, &sstatus))
2510 rc = -ERESTART;
2511
Tejun Heo416dc9e2007-10-31 10:17:03 +09002512 if (rc == -ERESTART || try >= max_tries)
2513 goto out;
2514
2515 now = jiffies;
2516 if (time_before(now, deadline)) {
2517 unsigned long delta = deadline - now;
2518
Tejun Heob1c72912008-07-31 17:02:43 +09002519 ata_link_printk(failed_link, KERN_WARNING,
Tejun Heo0a2c0f52008-05-20 02:17:52 +09002520 "reset failed (errno=%d), retrying in %u secs\n",
2521 rc, DIV_ROUND_UP(jiffies_to_msecs(delta), 1000));
Tejun Heo416dc9e2007-10-31 10:17:03 +09002522
2523 while (delta)
2524 delta = schedule_timeout_uninterruptible(delta);
2525 }
2526
Tejun Heob1c72912008-07-31 17:02:43 +09002527 if (try == max_tries - 1) {
Tejun Heo416dc9e2007-10-31 10:17:03 +09002528 sata_down_spd_limit(link);
Tejun Heob1c72912008-07-31 17:02:43 +09002529 if (slave)
2530 sata_down_spd_limit(slave);
2531 } else if (rc == -EPIPE)
2532 sata_down_spd_limit(failed_link);
2533
Tejun Heo416dc9e2007-10-31 10:17:03 +09002534 if (hardreset)
2535 reset = hardreset;
2536 goto retry;
Tejun Heo022bdb02006-05-15 20:58:22 +09002537}
2538
Tejun Heo02607312007-08-06 18:36:23 +09002539static int ata_eh_revalidate_and_attach(struct ata_link *link,
Tejun Heo084fe632006-05-31 18:28:03 +09002540 struct ata_device **r_failed_dev)
Tejun Heo022bdb02006-05-15 20:58:22 +09002541{
Tejun Heo02607312007-08-06 18:36:23 +09002542 struct ata_port *ap = link->ap;
2543 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09002544 struct ata_device *dev;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002545 unsigned int new_mask = 0;
Tejun Heo084fe632006-05-31 18:28:03 +09002546 unsigned long flags;
Tejun Heof58229f2007-08-06 18:36:23 +09002547 int rc = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09002548
2549 DPRINTK("ENTER\n");
2550
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002551 /* For PATA drive side cable detection to work, IDENTIFY must
2552 * be done backwards such that PDIAG- is released by the slave
2553 * device before the master device is identified.
2554 */
Tejun Heo02607312007-08-06 18:36:23 +09002555 ata_link_for_each_dev_reverse(dev, link) {
Tejun Heof58229f2007-08-06 18:36:23 +09002556 unsigned int action = ata_eh_dev_action(dev);
2557 unsigned int readid_flags = 0;
Tejun Heo47005f22006-06-19 18:27:23 +09002558
Tejun Heobff04642006-11-10 18:08:10 +09002559 if (ehc->i.flags & ATA_EHI_DID_RESET)
2560 readid_flags |= ATA_READID_POSTRESET;
2561
Tejun Heo9666f402007-05-04 21:27:47 +02002562 if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
Tejun Heo633273a32007-09-23 13:19:54 +09002563 WARN_ON(dev->class == ATA_DEV_PMP);
2564
Tejun Heob1c72912008-07-31 17:02:43 +09002565 if (ata_phys_link_offline(ata_dev_phys_link(dev))) {
Tejun Heo022bdb02006-05-15 20:58:22 +09002566 rc = -EIO;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002567 goto err;
Tejun Heo022bdb02006-05-15 20:58:22 +09002568 }
2569
Tejun Heo02607312007-08-06 18:36:23 +09002570 ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
Tejun Heo422c9da2007-09-23 13:14:12 +09002571 rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
2572 readid_flags);
Tejun Heo022bdb02006-05-15 20:58:22 +09002573 if (rc)
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002574 goto err;
Tejun Heo022bdb02006-05-15 20:58:22 +09002575
Tejun Heo02607312007-08-06 18:36:23 +09002576 ata_eh_done(link, dev, ATA_EH_REVALIDATE);
Tejun Heo47005f22006-06-19 18:27:23 +09002577
Tejun Heobaa1e782006-11-01 18:39:27 +09002578 /* Configuration may have changed, reconfigure
2579 * transfer mode.
2580 */
2581 ehc->i.flags |= ATA_EHI_SETMODE;
2582
zhao, forrest3057ac32006-06-12 12:01:34 +08002583 /* schedule the scsi_rescan_device() here */
2584 queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
Tejun Heo084fe632006-05-31 18:28:03 +09002585 } else if (dev->class == ATA_DEV_UNKNOWN &&
2586 ehc->tries[dev->devno] &&
2587 ata_class_enabled(ehc->classes[dev->devno])) {
2588 dev->class = ehc->classes[dev->devno];
2589
Tejun Heo633273a32007-09-23 13:19:54 +09002590 if (dev->class == ATA_DEV_PMP)
2591 rc = sata_pmp_attach(dev);
2592 else
2593 rc = ata_dev_read_id(dev, &dev->class,
2594 readid_flags, dev->id);
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002595 switch (rc) {
2596 case 0:
Tejun Heof58229f2007-08-06 18:36:23 +09002597 new_mask |= 1 << dev->devno;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002598 break;
2599 case -ENOENT:
Tejun Heo55a8e2c2006-11-10 18:08:10 +09002600 /* IDENTIFY was issued to non-existent
2601 * device. No need to reset. Just
2602 * thaw and kill the device.
2603 */
2604 ata_eh_thaw_port(ap);
2605 dev->class = ATA_DEV_UNKNOWN;
Tejun Heo084fe632006-05-31 18:28:03 +09002606 break;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002607 default:
2608 dev->class = ATA_DEV_UNKNOWN;
2609 goto err;
Tejun Heo55a8e2c2006-11-10 18:08:10 +09002610 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002611 }
2612 }
2613
Tejun Heoc1c4e8d2007-04-23 02:05:53 +09002614 /* PDIAG- should have been released, ask cable type if post-reset */
Tejun Heo33267322008-02-13 09:15:09 +09002615 if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) {
2616 if (ap->ops->cable_detect)
2617 ap->cbl = ap->ops->cable_detect(ap);
2618 ata_force_cbl(ap);
2619 }
Tejun Heoc1c4e8d2007-04-23 02:05:53 +09002620
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002621 /* Configure new devices forward such that user doesn't see
2622 * device detection messages backwards.
2623 */
Tejun Heo02607312007-08-06 18:36:23 +09002624 ata_link_for_each_dev(dev, link) {
Tejun Heo633273a32007-09-23 13:19:54 +09002625 if (!(new_mask & (1 << dev->devno)) ||
2626 dev->class == ATA_DEV_PMP)
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002627 continue;
2628
2629 ehc->i.flags |= ATA_EHI_PRINTINFO;
2630 rc = ata_dev_configure(dev);
2631 ehc->i.flags &= ~ATA_EHI_PRINTINFO;
2632 if (rc)
2633 goto err;
2634
2635 spin_lock_irqsave(ap->lock, flags);
2636 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
2637 spin_unlock_irqrestore(ap->lock, flags);
2638
2639 /* new device discovered, configure xfermode */
2640 ehc->i.flags |= ATA_EHI_SETMODE;
2641 }
2642
2643 return 0;
2644
2645 err:
2646 *r_failed_dev = dev;
2647 DPRINTK("EXIT rc=%d\n", rc);
Tejun Heo022bdb02006-05-15 20:58:22 +09002648 return rc;
2649}
2650
Tejun Heo6f1d1e32007-11-27 19:28:55 +09002651/**
2652 * ata_set_mode - Program timings and issue SET FEATURES - XFER
2653 * @link: link on which timings will be programmed
2654 * @r_failed_dev: out paramter for failed device
2655 *
2656 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
2657 * ata_set_mode() fails, pointer to the failing device is
2658 * returned in @r_failed_dev.
2659 *
2660 * LOCKING:
2661 * PCI/etc. bus probe sem.
2662 *
2663 * RETURNS:
2664 * 0 on success, negative errno otherwise
2665 */
2666int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
2667{
2668 struct ata_port *ap = link->ap;
Tejun Heo00115e02007-11-27 19:28:58 +09002669 struct ata_device *dev;
2670 int rc;
Tejun Heo6f1d1e32007-11-27 19:28:55 +09002671
Tejun Heo76326ac2007-11-27 19:28:59 +09002672 /* if data transfer is verified, clear DUBIOUS_XFER on ering top */
2673 ata_link_for_each_dev(dev, link) {
2674 if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
2675 struct ata_ering_entry *ent;
2676
2677 ent = ata_ering_top(&dev->ering);
2678 if (ent)
2679 ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER;
2680 }
2681 }
2682
Tejun Heo6f1d1e32007-11-27 19:28:55 +09002683 /* has private set_mode? */
2684 if (ap->ops->set_mode)
Tejun Heo00115e02007-11-27 19:28:58 +09002685 rc = ap->ops->set_mode(link, r_failed_dev);
2686 else
2687 rc = ata_do_set_mode(link, r_failed_dev);
2688
2689 /* if transfer mode has changed, set DUBIOUS_XFER on device */
2690 ata_link_for_each_dev(dev, link) {
2691 struct ata_eh_context *ehc = &link->eh_context;
2692 u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
2693 u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
2694
2695 if (dev->xfer_mode != saved_xfer_mode ||
2696 ata_ncq_enabled(dev) != saved_ncq)
2697 dev->flags |= ATA_DFLAG_DUBIOUS_XFER;
2698 }
2699
2700 return rc;
Tejun Heo6f1d1e32007-11-27 19:28:55 +09002701}
2702
Tejun Heo02607312007-08-06 18:36:23 +09002703static int ata_link_nr_enabled(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09002704{
Tejun Heof58229f2007-08-06 18:36:23 +09002705 struct ata_device *dev;
2706 int cnt = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09002707
Tejun Heo02607312007-08-06 18:36:23 +09002708 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09002709 if (ata_dev_enabled(dev))
Tejun Heo022bdb02006-05-15 20:58:22 +09002710 cnt++;
2711 return cnt;
2712}
2713
Tejun Heo02607312007-08-06 18:36:23 +09002714static int ata_link_nr_vacant(struct ata_link *link)
Tejun Heo084fe632006-05-31 18:28:03 +09002715{
Tejun Heof58229f2007-08-06 18:36:23 +09002716 struct ata_device *dev;
2717 int cnt = 0;
Tejun Heo084fe632006-05-31 18:28:03 +09002718
Tejun Heo02607312007-08-06 18:36:23 +09002719 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09002720 if (dev->class == ATA_DEV_UNKNOWN)
Tejun Heo084fe632006-05-31 18:28:03 +09002721 cnt++;
2722 return cnt;
2723}
2724
Tejun Heo02607312007-08-06 18:36:23 +09002725static int ata_eh_skip_recovery(struct ata_link *link)
Tejun Heo084fe632006-05-31 18:28:03 +09002726{
Tejun Heo672b2d62008-01-24 00:05:14 +09002727 struct ata_port *ap = link->ap;
Tejun Heo02607312007-08-06 18:36:23 +09002728 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heof58229f2007-08-06 18:36:23 +09002729 struct ata_device *dev;
Tejun Heo084fe632006-05-31 18:28:03 +09002730
Tejun Heof9df58c2007-09-23 13:14:13 +09002731 /* skip disabled links */
2732 if (link->flags & ATA_LFLAG_DISABLED)
2733 return 1;
2734
Tejun Heo672b2d62008-01-24 00:05:14 +09002735 /* thaw frozen port and recover failed devices */
2736 if ((ap->pflags & ATA_PFLAG_FROZEN) || ata_link_nr_enabled(link))
2737 return 0;
2738
2739 /* reset at least once if reset is requested */
2740 if ((ehc->i.action & ATA_EH_RESET) &&
2741 !(ehc->i.flags & ATA_EHI_DID_RESET))
Tejun Heo084fe632006-05-31 18:28:03 +09002742 return 0;
2743
2744 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
Tejun Heo02607312007-08-06 18:36:23 +09002745 ata_link_for_each_dev(dev, link) {
Tejun Heo084fe632006-05-31 18:28:03 +09002746 if (dev->class == ATA_DEV_UNKNOWN &&
2747 ehc->classes[dev->devno] != ATA_DEV_NONE)
2748 return 0;
2749 }
2750
2751 return 1;
2752}
2753
Tejun Heo02c05a22007-11-27 19:28:54 +09002754static int ata_eh_schedule_probe(struct ata_device *dev)
2755{
2756 struct ata_eh_context *ehc = &dev->link->eh_context;
2757
2758 if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
2759 (ehc->did_probe_mask & (1 << dev->devno)))
2760 return 0;
2761
2762 ata_eh_detach_dev(dev);
2763 ata_dev_init(dev);
2764 ehc->did_probe_mask |= (1 << dev->devno);
Tejun Heocf480622008-01-24 00:05:14 +09002765 ehc->i.action |= ATA_EH_RESET;
Tejun Heo00115e02007-11-27 19:28:58 +09002766 ehc->saved_xfer_mode[dev->devno] = 0;
2767 ehc->saved_ncq_enabled &= ~(1 << dev->devno);
Tejun Heo02c05a22007-11-27 19:28:54 +09002768
2769 return 1;
2770}
2771
Tejun Heo9b1e2652007-08-06 18:36:24 +09002772static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
Tejun Heofee7ca72007-07-01 19:05:58 +09002773{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002774 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heofee7ca72007-07-01 19:05:58 +09002775
2776 ehc->tries[dev->devno]--;
2777
2778 switch (err) {
2779 case -ENODEV:
2780 /* device missing or wrong IDENTIFY data, schedule probing */
2781 ehc->i.probe_mask |= (1 << dev->devno);
2782 case -EINVAL:
2783 /* give it just one more chance */
2784 ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
2785 case -EIO:
Tejun Heo4fb46152007-10-29 16:45:05 +09002786 if (ehc->tries[dev->devno] == 1 && dev->pio_mode > XFER_PIO_0) {
Tejun Heofee7ca72007-07-01 19:05:58 +09002787 /* This is the last chance, better to slow
2788 * down than lose it.
2789 */
Tejun Heob1c72912008-07-31 17:02:43 +09002790 sata_down_spd_limit(ata_dev_phys_link(dev));
Tejun Heofee7ca72007-07-01 19:05:58 +09002791 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2792 }
2793 }
2794
2795 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2796 /* disable device if it has used up all its chances */
2797 ata_dev_disable(dev);
2798
2799 /* detach if offline */
Tejun Heob1c72912008-07-31 17:02:43 +09002800 if (ata_phys_link_offline(ata_dev_phys_link(dev)))
Tejun Heofee7ca72007-07-01 19:05:58 +09002801 ata_eh_detach_dev(dev);
2802
Tejun Heo02c05a22007-11-27 19:28:54 +09002803 /* schedule probe if necessary */
Tejun Heo87fbc5a2008-05-20 02:17:54 +09002804 if (ata_eh_schedule_probe(dev)) {
Tejun Heofee7ca72007-07-01 19:05:58 +09002805 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
Tejun Heo87fbc5a2008-05-20 02:17:54 +09002806 memset(ehc->cmd_timeout_idx[dev->devno], 0,
2807 sizeof(ehc->cmd_timeout_idx[dev->devno]));
2808 }
Tejun Heo9b1e2652007-08-06 18:36:24 +09002809
2810 return 1;
Tejun Heofee7ca72007-07-01 19:05:58 +09002811 } else {
Tejun Heocf480622008-01-24 00:05:14 +09002812 ehc->i.action |= ATA_EH_RESET;
Tejun Heo9b1e2652007-08-06 18:36:24 +09002813 return 0;
Tejun Heofee7ca72007-07-01 19:05:58 +09002814 }
2815}
2816
Tejun Heo022bdb02006-05-15 20:58:22 +09002817/**
2818 * ata_eh_recover - recover host port after error
2819 * @ap: host port to recover
Tejun Heof5914a42006-05-31 18:27:48 +09002820 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09002821 * @softreset: softreset method (can be NULL)
2822 * @hardreset: hardreset method (can be NULL)
2823 * @postreset: postreset method (can be NULL)
Tejun Heo9b1e2652007-08-06 18:36:24 +09002824 * @r_failed_link: out parameter for failed link
Tejun Heo022bdb02006-05-15 20:58:22 +09002825 *
2826 * This is the alpha and omega, eum and yang, heart and soul of
2827 * libata exception handling. On entry, actions required to
Tejun Heo9b1e2652007-08-06 18:36:24 +09002828 * recover each link and hotplug requests are recorded in the
2829 * link's eh_context. This function executes all the operations
2830 * with appropriate retrials and fallbacks to resurrect failed
Tejun Heo084fe632006-05-31 18:28:03 +09002831 * devices, detach goners and greet newcomers.
Tejun Heo022bdb02006-05-15 20:58:22 +09002832 *
2833 * LOCKING:
2834 * Kernel thread context (may sleep).
2835 *
2836 * RETURNS:
2837 * 0 on success, -errno on failure.
2838 */
Tejun Heofb7fd612007-09-23 13:14:12 +09002839int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
2840 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2841 ata_postreset_fn_t postreset,
2842 struct ata_link **r_failed_link)
Tejun Heo022bdb02006-05-15 20:58:22 +09002843{
Tejun Heo9b1e2652007-08-06 18:36:24 +09002844 struct ata_link *link;
Tejun Heo022bdb02006-05-15 20:58:22 +09002845 struct ata_device *dev;
Tejun Heo0a2c0f52008-05-20 02:17:52 +09002846 int nr_failed_devs;
Tejun Heodc98c322008-05-19 01:15:07 +09002847 int rc;
Tejun Heof9df58c2007-09-23 13:14:13 +09002848 unsigned long flags;
Tejun Heo022bdb02006-05-15 20:58:22 +09002849
2850 DPRINTK("ENTER\n");
2851
2852 /* prep for recovery */
Tejun Heo9b1e2652007-08-06 18:36:24 +09002853 ata_port_for_each_link(link, ap) {
2854 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo084fe632006-05-31 18:28:03 +09002855
Tejun Heof9df58c2007-09-23 13:14:13 +09002856 /* re-enable link? */
2857 if (ehc->i.action & ATA_EH_ENABLE_LINK) {
2858 ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
2859 spin_lock_irqsave(ap->lock, flags);
2860 link->flags &= ~ATA_LFLAG_DISABLED;
2861 spin_unlock_irqrestore(ap->lock, flags);
2862 ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
2863 }
2864
Tejun Heo9b1e2652007-08-06 18:36:24 +09002865 ata_link_for_each_dev(dev, link) {
Tejun Heofd995f72007-09-23 13:14:12 +09002866 if (link->flags & ATA_LFLAG_NO_RETRY)
2867 ehc->tries[dev->devno] = 1;
2868 else
2869 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
Tejun Heo79a55b72007-01-18 17:22:18 +09002870
Tejun Heo9b1e2652007-08-06 18:36:24 +09002871 /* collect port action mask recorded in dev actions */
2872 ehc->i.action |= ehc->i.dev_action[dev->devno] &
2873 ~ATA_EH_PERDEV_MASK;
2874 ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
Tejun Heo084fe632006-05-31 18:28:03 +09002875
Tejun Heo9b1e2652007-08-06 18:36:24 +09002876 /* process hotplug request */
2877 if (dev->flags & ATA_DFLAG_DETACH)
2878 ata_eh_detach_dev(dev);
2879
Tejun Heo02c05a22007-11-27 19:28:54 +09002880 /* schedule probe if necessary */
2881 if (!ata_dev_enabled(dev))
2882 ata_eh_schedule_probe(dev);
Tejun Heo084fe632006-05-31 18:28:03 +09002883 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002884 }
2885
2886 retry:
Tejun Heo022bdb02006-05-15 20:58:22 +09002887 rc = 0;
Tejun Heo9b1e2652007-08-06 18:36:24 +09002888 nr_failed_devs = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09002889
Tejun Heoaeb2ecd2006-06-12 14:11:43 +09002890 /* if UNLOADING, finish immediately */
Tejun Heob51e9e52006-06-29 01:29:30 +09002891 if (ap->pflags & ATA_PFLAG_UNLOADING)
Tejun Heoaeb2ecd2006-06-12 14:11:43 +09002892 goto out;
2893
Tejun Heo9b1e2652007-08-06 18:36:24 +09002894 /* prep for EH */
2895 ata_port_for_each_link(link, ap) {
2896 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09002897
Tejun Heo9b1e2652007-08-06 18:36:24 +09002898 /* skip EH if possible. */
2899 if (ata_eh_skip_recovery(link))
2900 ehc->i.action = 0;
2901
Tejun Heo9b1e2652007-08-06 18:36:24 +09002902 ata_link_for_each_dev(dev, link)
2903 ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
2904 }
Tejun Heo084fe632006-05-31 18:28:03 +09002905
Tejun Heo022bdb02006-05-15 20:58:22 +09002906 /* reset */
Tejun Heodc98c322008-05-19 01:15:07 +09002907 ata_port_for_each_link(link, ap) {
2908 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09002909
Tejun Heodc98c322008-05-19 01:15:07 +09002910 if (!(ehc->i.action & ATA_EH_RESET))
2911 continue;
Tejun Heo9b1e2652007-08-06 18:36:24 +09002912
Tejun Heodc98c322008-05-19 01:15:07 +09002913 rc = ata_eh_reset(link, ata_link_nr_vacant(link),
2914 prereset, softreset, hardreset, postreset);
2915 if (rc) {
2916 ata_link_printk(link, KERN_ERR,
2917 "reset failed, giving up\n");
2918 goto out;
Tejun Heo022bdb02006-05-15 20:58:22 +09002919 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002920 }
2921
Tejun Heo9b1e2652007-08-06 18:36:24 +09002922 /* the rest */
2923 ata_port_for_each_link(link, ap) {
2924 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09002925
Tejun Heo9b1e2652007-08-06 18:36:24 +09002926 /* revalidate existing devices and attach new ones */
2927 rc = ata_eh_revalidate_and_attach(link, &dev);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002928 if (rc)
Tejun Heo022bdb02006-05-15 20:58:22 +09002929 goto dev_fail;
Tejun Heo9b1e2652007-08-06 18:36:24 +09002930
Tejun Heo633273a32007-09-23 13:19:54 +09002931 /* if PMP got attached, return, pmp EH will take care of it */
2932 if (link->device->class == ATA_DEV_PMP) {
2933 ehc->i.action = 0;
2934 return 0;
2935 }
2936
Tejun Heo9b1e2652007-08-06 18:36:24 +09002937 /* configure transfer mode if necessary */
2938 if (ehc->i.flags & ATA_EHI_SETMODE) {
2939 rc = ata_set_mode(link, &dev);
2940 if (rc)
2941 goto dev_fail;
2942 ehc->i.flags &= ~ATA_EHI_SETMODE;
2943 }
2944
Tejun Heo3ec25eb2008-03-27 18:37:14 +09002945 if (ehc->i.action & ATA_EH_LPM)
Kristen Carlson Accardica773292007-10-25 00:58:59 -04002946 ata_link_for_each_dev(dev, link)
2947 ata_dev_enable_pm(dev, ap->pm_policy);
2948
Tejun Heo9b1e2652007-08-06 18:36:24 +09002949 /* this link is okay now */
2950 ehc->i.flags = 0;
2951 continue;
2952
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002953dev_fail:
Tejun Heo9b1e2652007-08-06 18:36:24 +09002954 nr_failed_devs++;
Tejun Heo0a2c0f52008-05-20 02:17:52 +09002955 ata_eh_handle_dev_fail(dev, rc);
Tejun Heo9b1e2652007-08-06 18:36:24 +09002956
Tejun Heob06ce3e2007-10-09 15:06:48 +09002957 if (ap->pflags & ATA_PFLAG_FROZEN) {
2958 /* PMP reset requires working host port.
2959 * Can't retry if it's frozen.
2960 */
Tejun Heo071f44b2008-04-07 22:47:22 +09002961 if (sata_pmp_attached(ap))
Tejun Heob06ce3e2007-10-09 15:06:48 +09002962 goto out;
Tejun Heo9b1e2652007-08-06 18:36:24 +09002963 break;
Tejun Heob06ce3e2007-10-09 15:06:48 +09002964 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002965 }
2966
Tejun Heo0a2c0f52008-05-20 02:17:52 +09002967 if (nr_failed_devs)
Tejun Heo9b1e2652007-08-06 18:36:24 +09002968 goto retry;
Tejun Heo022bdb02006-05-15 20:58:22 +09002969
Tejun Heo022bdb02006-05-15 20:58:22 +09002970 out:
Tejun Heo9b1e2652007-08-06 18:36:24 +09002971 if (rc && r_failed_link)
2972 *r_failed_link = link;
Tejun Heo022bdb02006-05-15 20:58:22 +09002973
2974 DPRINTK("EXIT, rc=%d\n", rc);
2975 return rc;
2976}
2977
2978/**
2979 * ata_eh_finish - finish up EH
2980 * @ap: host port to finish EH for
2981 *
2982 * Recovery is complete. Clean up EH states and retry or finish
2983 * failed qcs.
2984 *
2985 * LOCKING:
2986 * None.
2987 */
Tejun Heofb7fd612007-09-23 13:14:12 +09002988void ata_eh_finish(struct ata_port *ap)
Tejun Heo022bdb02006-05-15 20:58:22 +09002989{
2990 int tag;
2991
2992 /* retry or finish qcs */
2993 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2994 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2995
2996 if (!(qc->flags & ATA_QCFLAG_FAILED))
2997 continue;
2998
2999 if (qc->err_mask) {
3000 /* FIXME: Once EH migration is complete,
3001 * generate sense data in this function,
3002 * considering both err_mask and tf.
3003 */
Tejun Heo03faab72008-03-27 19:14:24 +09003004 if (qc->flags & ATA_QCFLAG_RETRY)
Tejun Heo022bdb02006-05-15 20:58:22 +09003005 ata_eh_qc_retry(qc);
Tejun Heo03faab72008-03-27 19:14:24 +09003006 else
3007 ata_eh_qc_complete(qc);
Tejun Heo022bdb02006-05-15 20:58:22 +09003008 } else {
3009 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
3010 ata_eh_qc_complete(qc);
3011 } else {
3012 /* feed zero TF to sense generation */
3013 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
3014 ata_eh_qc_retry(qc);
3015 }
3016 }
3017 }
Tejun Heoda917d62007-09-23 13:14:12 +09003018
3019 /* make sure nr_active_links is zero after EH */
3020 WARN_ON(ap->nr_active_links);
3021 ap->nr_active_links = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09003022}
3023
3024/**
3025 * ata_do_eh - do standard error handling
3026 * @ap: host port to handle error for
Tejun Heoa1efdab2008-03-25 12:22:50 +09003027 *
Tejun Heof5914a42006-05-31 18:27:48 +09003028 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09003029 * @softreset: softreset method (can be NULL)
3030 * @hardreset: hardreset method (can be NULL)
3031 * @postreset: postreset method (can be NULL)
3032 *
3033 * Perform standard error handling sequence.
3034 *
3035 * LOCKING:
3036 * Kernel thread context (may sleep).
3037 */
Tejun Heof5914a42006-05-31 18:27:48 +09003038void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
3039 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
3040 ata_postreset_fn_t postreset)
Tejun Heo022bdb02006-05-15 20:58:22 +09003041{
Tejun Heo9b1e2652007-08-06 18:36:24 +09003042 struct ata_device *dev;
3043 int rc;
3044
3045 ata_eh_autopsy(ap);
3046 ata_eh_report(ap);
3047
3048 rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
3049 NULL);
3050 if (rc) {
3051 ata_link_for_each_dev(dev, &ap->link)
3052 ata_dev_disable(dev);
3053 }
3054
Tejun Heo022bdb02006-05-15 20:58:22 +09003055 ata_eh_finish(ap);
3056}
Tejun Heo500530f2006-07-03 16:07:27 +09003057
Tejun Heoa1efdab2008-03-25 12:22:50 +09003058/**
3059 * ata_std_error_handler - standard error handler
3060 * @ap: host port to handle error for
3061 *
3062 * Standard error handler
3063 *
3064 * LOCKING:
3065 * Kernel thread context (may sleep).
3066 */
3067void ata_std_error_handler(struct ata_port *ap)
3068{
3069 struct ata_port_operations *ops = ap->ops;
3070 ata_reset_fn_t hardreset = ops->hardreset;
3071
Tejun Heo57c9efd2008-04-07 22:47:19 +09003072 /* ignore built-in hardreset if SCR access is not available */
3073 if (ata_is_builtin_hardreset(hardreset) && !sata_scr_valid(&ap->link))
Tejun Heoa1efdab2008-03-25 12:22:50 +09003074 hardreset = NULL;
3075
3076 ata_do_eh(ap, ops->prereset, ops->softreset, hardreset, ops->postreset);
3077}
3078
Tejun Heo6ffa01d2007-03-02 17:32:47 +09003079#ifdef CONFIG_PM
Tejun Heo500530f2006-07-03 16:07:27 +09003080/**
3081 * ata_eh_handle_port_suspend - perform port suspend operation
3082 * @ap: port to suspend
3083 *
3084 * Suspend @ap.
3085 *
3086 * LOCKING:
3087 * Kernel thread context (may sleep).
3088 */
3089static void ata_eh_handle_port_suspend(struct ata_port *ap)
3090{
3091 unsigned long flags;
3092 int rc = 0;
3093
3094 /* are we suspending? */
3095 spin_lock_irqsave(ap->lock, flags);
3096 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
3097 ap->pm_mesg.event == PM_EVENT_ON) {
3098 spin_unlock_irqrestore(ap->lock, flags);
3099 return;
3100 }
3101 spin_unlock_irqrestore(ap->lock, flags);
3102
3103 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
3104
Tejun Heo64578a32007-05-15 03:28:16 +09003105 /* tell ACPI we're suspending */
3106 rc = ata_acpi_on_suspend(ap);
3107 if (rc)
3108 goto out;
3109
Tejun Heo500530f2006-07-03 16:07:27 +09003110 /* suspend */
3111 ata_eh_freeze_port(ap);
3112
3113 if (ap->ops->port_suspend)
3114 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
3115
Shaohua Libd3adca2007-11-02 09:32:38 +08003116 ata_acpi_set_state(ap, PMSG_SUSPEND);
Tejun Heo64578a32007-05-15 03:28:16 +09003117 out:
Tejun Heo500530f2006-07-03 16:07:27 +09003118 /* report result */
3119 spin_lock_irqsave(ap->lock, flags);
3120
3121 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
3122 if (rc == 0)
3123 ap->pflags |= ATA_PFLAG_SUSPENDED;
Tejun Heo64578a32007-05-15 03:28:16 +09003124 else if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heo500530f2006-07-03 16:07:27 +09003125 ata_port_schedule_eh(ap);
3126
3127 if (ap->pm_result) {
3128 *ap->pm_result = rc;
3129 ap->pm_result = NULL;
3130 }
3131
3132 spin_unlock_irqrestore(ap->lock, flags);
3133
3134 return;
3135}
3136
3137/**
3138 * ata_eh_handle_port_resume - perform port resume operation
3139 * @ap: port to resume
3140 *
3141 * Resume @ap.
3142 *
Tejun Heo500530f2006-07-03 16:07:27 +09003143 * LOCKING:
3144 * Kernel thread context (may sleep).
3145 */
3146static void ata_eh_handle_port_resume(struct ata_port *ap)
3147{
Tejun Heo500530f2006-07-03 16:07:27 +09003148 unsigned long flags;
Tejun Heo9666f402007-05-04 21:27:47 +02003149 int rc = 0;
Tejun Heo500530f2006-07-03 16:07:27 +09003150
3151 /* are we resuming? */
3152 spin_lock_irqsave(ap->lock, flags);
3153 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
3154 ap->pm_mesg.event != PM_EVENT_ON) {
3155 spin_unlock_irqrestore(ap->lock, flags);
3156 return;
3157 }
3158 spin_unlock_irqrestore(ap->lock, flags);
3159
Tejun Heo9666f402007-05-04 21:27:47 +02003160 WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
Tejun Heo500530f2006-07-03 16:07:27 +09003161
Shaohua Libd3adca2007-11-02 09:32:38 +08003162 ata_acpi_set_state(ap, PMSG_ON);
3163
Tejun Heo500530f2006-07-03 16:07:27 +09003164 if (ap->ops->port_resume)
3165 rc = ap->ops->port_resume(ap);
3166
Tejun Heo67465442007-05-15 03:28:16 +09003167 /* tell ACPI that we're resuming */
3168 ata_acpi_on_resume(ap);
3169
Tejun Heo9666f402007-05-04 21:27:47 +02003170 /* report result */
Tejun Heo500530f2006-07-03 16:07:27 +09003171 spin_lock_irqsave(ap->lock, flags);
3172 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
3173 if (ap->pm_result) {
3174 *ap->pm_result = rc;
3175 ap->pm_result = NULL;
3176 }
3177 spin_unlock_irqrestore(ap->lock, flags);
3178}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09003179#endif /* CONFIG_PM */