blob: bd0b2bc76f1077761fb1aa56d4d238305468abb6 [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>
Jens Axboe242f9dc2008-09-14 05:55:09 -070036#include <linux/blkdev.h>
Jeff Garzik28555682007-10-11 17:12:35 -040037#include <linux/pci.h>
Tejun Heoece1d632006-04-02 18:51:53 +090038#include <scsi/scsi.h>
39#include <scsi/scsi_host.h>
40#include <scsi/scsi_eh.h>
41#include <scsi/scsi_device.h>
42#include <scsi/scsi_cmnd.h>
Jeff Garzikc6fd2802006-08-10 07:31:37 -040043#include "../scsi/scsi_transport_api.h"
Tejun Heoece1d632006-04-02 18:51:53 +090044
45#include <linux/libata.h>
46
47#include "libata.h"
48
Tejun Heo7d47e8d2007-02-02 16:22:31 +090049enum {
Tejun Heo3884f7b2007-11-27 19:28:56 +090050 /* speed down verdicts */
Tejun Heo7d47e8d2007-02-02 16:22:31 +090051 ATA_EH_SPDN_NCQ_OFF = (1 << 0),
52 ATA_EH_SPDN_SPEED_DOWN = (1 << 1),
53 ATA_EH_SPDN_FALLBACK_TO_PIO = (1 << 2),
Tejun Heo76326ac2007-11-27 19:28:59 +090054 ATA_EH_SPDN_KEEP_ERRORS = (1 << 3),
Tejun Heo3884f7b2007-11-27 19:28:56 +090055
56 /* error flags */
57 ATA_EFLAG_IS_IO = (1 << 0),
Tejun Heo76326ac2007-11-27 19:28:59 +090058 ATA_EFLAG_DUBIOUS_XFER = (1 << 1),
Tejun Heo3884f7b2007-11-27 19:28:56 +090059
60 /* error categories */
61 ATA_ECAT_NONE = 0,
62 ATA_ECAT_ATA_BUS = 1,
63 ATA_ECAT_TOUT_HSM = 2,
64 ATA_ECAT_UNK_DEV = 3,
Tejun Heo75f9caf2008-01-03 01:21:14 +090065 ATA_ECAT_DUBIOUS_NONE = 4,
66 ATA_ECAT_DUBIOUS_ATA_BUS = 5,
67 ATA_ECAT_DUBIOUS_TOUT_HSM = 6,
68 ATA_ECAT_DUBIOUS_UNK_DEV = 7,
69 ATA_ECAT_NR = 8,
Tejun Heo7d47e8d2007-02-02 16:22:31 +090070
Tejun Heo87fbc5a2008-05-20 02:17:54 +090071 ATA_EH_CMD_DFL_TIMEOUT = 5000,
72
Tejun Heo0a2c0f52008-05-20 02:17:52 +090073 /* always put at least this amount of time between resets */
74 ATA_EH_RESET_COOL_DOWN = 5000,
75
Tejun Heo341c2c92008-05-20 02:17:51 +090076 /* Waiting in ->prereset can never be reliable. It's
77 * sometimes nice to wait there but it can't be depended upon;
78 * otherwise, we wouldn't be resetting. Just give it enough
79 * time for most drives to spin up.
80 */
81 ATA_EH_PRERESET_TIMEOUT = 10000,
82 ATA_EH_FASTDRAIN_INTERVAL = 3000,
Tejun Heo31daabd2007-02-02 16:50:52 +090083};
84
85/* The following table determines how we sequence resets. Each entry
86 * represents timeout for that try. The first try can be soft or
87 * hardreset. All others are hardreset if available. In most cases
88 * the first reset w/ 10sec timeout should succeed. Following entries
89 * are mostly for error handling, hotplug and retarded devices.
90 */
91static const unsigned long ata_eh_reset_timeouts[] = {
Tejun Heo341c2c92008-05-20 02:17:51 +090092 10000, /* most drives spin up by 10sec */
93 10000, /* > 99% working drives spin up before 20sec */
94 35000, /* give > 30 secs of idleness for retarded devices */
95 5000, /* and sweet one last chance */
Tejun Heod8af0eb2008-05-20 02:17:53 +090096 ULONG_MAX, /* > 1 min has elapsed, give up */
Tejun Heo31daabd2007-02-02 16:50:52 +090097};
98
Tejun Heo87fbc5a2008-05-20 02:17:54 +090099static const unsigned long ata_eh_identify_timeouts[] = {
100 5000, /* covers > 99% of successes and not too boring on failures */
101 10000, /* combined time till here is enough even for media access */
102 30000, /* for true idiots */
103 ULONG_MAX,
104};
105
106static const unsigned long ata_eh_other_timeouts[] = {
107 5000, /* same rationale as identify timeout */
108 10000, /* ditto */
109 /* but no merciful 30sec for other commands, it just isn't worth it */
110 ULONG_MAX,
111};
112
113struct ata_eh_cmd_timeout_ent {
114 const u8 *commands;
115 const unsigned long *timeouts;
116};
117
118/* The following table determines timeouts to use for EH internal
119 * commands. Each table entry is a command class and matches the
120 * commands the entry applies to and the timeout table to use.
121 *
122 * On the retry after a command timed out, the next timeout value from
123 * the table is used. If the table doesn't contain further entries,
124 * the last value is used.
125 *
126 * ehc->cmd_timeout_idx keeps track of which timeout to use per
127 * command class, so if SET_FEATURES times out on the first try, the
128 * next try will use the second timeout value only for that class.
129 */
130#define CMDS(cmds...) (const u8 []){ cmds, 0 }
131static const struct ata_eh_cmd_timeout_ent
132ata_eh_cmd_timeout_table[ATA_EH_CMD_TIMEOUT_TABLE_SIZE] = {
133 { .commands = CMDS(ATA_CMD_ID_ATA, ATA_CMD_ID_ATAPI),
134 .timeouts = ata_eh_identify_timeouts, },
135 { .commands = CMDS(ATA_CMD_READ_NATIVE_MAX, ATA_CMD_READ_NATIVE_MAX_EXT),
136 .timeouts = ata_eh_other_timeouts, },
137 { .commands = CMDS(ATA_CMD_SET_MAX, ATA_CMD_SET_MAX_EXT),
138 .timeouts = ata_eh_other_timeouts, },
139 { .commands = CMDS(ATA_CMD_SET_FEATURES),
140 .timeouts = ata_eh_other_timeouts, },
141 { .commands = CMDS(ATA_CMD_INIT_DEV_PARAMS),
142 .timeouts = ata_eh_other_timeouts, },
143};
144#undef CMDS
145
Tejun Heoad9e2762006-05-15 20:58:12 +0900146static void __ata_port_freeze(struct ata_port *ap);
Tejun Heo6ffa01d2007-03-02 17:32:47 +0900147#ifdef CONFIG_PM
Tejun Heo500530f2006-07-03 16:07:27 +0900148static void ata_eh_handle_port_suspend(struct ata_port *ap);
149static void ata_eh_handle_port_resume(struct ata_port *ap);
Tejun Heo6ffa01d2007-03-02 17:32:47 +0900150#else /* CONFIG_PM */
151static void ata_eh_handle_port_suspend(struct ata_port *ap)
152{ }
153
154static void ata_eh_handle_port_resume(struct ata_port *ap)
155{ }
Tejun Heo6ffa01d2007-03-02 17:32:47 +0900156#endif /* CONFIG_PM */
Tejun Heoad9e2762006-05-15 20:58:12 +0900157
Tejun Heob64bbc32007-07-16 14:29:39 +0900158static void __ata_ehi_pushv_desc(struct ata_eh_info *ehi, const char *fmt,
159 va_list args)
160{
161 ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
162 ATA_EH_DESC_LEN - ehi->desc_len,
163 fmt, args);
164}
165
166/**
167 * __ata_ehi_push_desc - push error description without adding separator
168 * @ehi: target EHI
169 * @fmt: printf format string
170 *
171 * Format string according to @fmt and append it to @ehi->desc.
172 *
173 * LOCKING:
174 * spin_lock_irqsave(host lock)
175 */
176void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
177{
178 va_list args;
179
180 va_start(args, fmt);
181 __ata_ehi_pushv_desc(ehi, fmt, args);
182 va_end(args);
183}
184
185/**
186 * ata_ehi_push_desc - push error description with separator
187 * @ehi: target EHI
188 * @fmt: printf format string
189 *
190 * Format string according to @fmt and append it to @ehi->desc.
191 * If @ehi->desc is not empty, ", " is added in-between.
192 *
193 * LOCKING:
194 * spin_lock_irqsave(host lock)
195 */
196void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
197{
198 va_list args;
199
200 if (ehi->desc_len)
201 __ata_ehi_push_desc(ehi, ", ");
202
203 va_start(args, fmt);
204 __ata_ehi_pushv_desc(ehi, fmt, args);
205 va_end(args);
206}
207
208/**
209 * ata_ehi_clear_desc - clean error description
210 * @ehi: target EHI
211 *
212 * Clear @ehi->desc.
213 *
214 * LOCKING:
215 * spin_lock_irqsave(host lock)
216 */
217void ata_ehi_clear_desc(struct ata_eh_info *ehi)
218{
219 ehi->desc[0] = '\0';
220 ehi->desc_len = 0;
221}
222
Tejun Heocbcdd872007-08-18 13:14:55 +0900223/**
224 * ata_port_desc - append port description
225 * @ap: target ATA port
226 * @fmt: printf format string
227 *
228 * Format string according to @fmt and append it to port
229 * description. If port description is not empty, " " is added
230 * in-between. This function is to be used while initializing
231 * ata_host. The description is printed on host registration.
232 *
233 * LOCKING:
234 * None.
235 */
236void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
237{
238 va_list args;
239
240 WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
241
242 if (ap->link.eh_info.desc_len)
243 __ata_ehi_push_desc(&ap->link.eh_info, " ");
244
245 va_start(args, fmt);
246 __ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
247 va_end(args);
248}
249
250#ifdef CONFIG_PCI
251
252/**
253 * ata_port_pbar_desc - append PCI BAR description
254 * @ap: target ATA port
255 * @bar: target PCI BAR
256 * @offset: offset into PCI BAR
257 * @name: name of the area
258 *
259 * If @offset is negative, this function formats a string which
260 * contains the name, address, size and type of the BAR and
261 * appends it to the port description. If @offset is zero or
262 * positive, only name and offsetted address is appended.
263 *
264 * LOCKING:
265 * None.
266 */
267void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
268 const char *name)
269{
270 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
271 char *type = "";
272 unsigned long long start, len;
273
274 if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
275 type = "m";
276 else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
277 type = "i";
278
279 start = (unsigned long long)pci_resource_start(pdev, bar);
280 len = (unsigned long long)pci_resource_len(pdev, bar);
281
282 if (offset < 0)
283 ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
284 else
Andrew Mortone6a73ab2007-12-13 16:01:37 -0800285 ata_port_desc(ap, "%s 0x%llx", name,
286 start + (unsigned long long)offset);
Tejun Heocbcdd872007-08-18 13:14:55 +0900287}
288
289#endif /* CONFIG_PCI */
290
Tejun Heo87fbc5a2008-05-20 02:17:54 +0900291static int ata_lookup_timeout_table(u8 cmd)
292{
293 int i;
294
295 for (i = 0; i < ATA_EH_CMD_TIMEOUT_TABLE_SIZE; i++) {
296 const u8 *cur;
297
298 for (cur = ata_eh_cmd_timeout_table[i].commands; *cur; cur++)
299 if (*cur == cmd)
300 return i;
301 }
302
303 return -1;
304}
305
306/**
307 * ata_internal_cmd_timeout - determine timeout for an internal command
308 * @dev: target device
309 * @cmd: internal command to be issued
310 *
311 * Determine timeout for internal command @cmd for @dev.
312 *
313 * LOCKING:
314 * EH context.
315 *
316 * RETURNS:
317 * Determined timeout.
318 */
319unsigned long ata_internal_cmd_timeout(struct ata_device *dev, u8 cmd)
320{
321 struct ata_eh_context *ehc = &dev->link->eh_context;
322 int ent = ata_lookup_timeout_table(cmd);
323 int idx;
324
325 if (ent < 0)
326 return ATA_EH_CMD_DFL_TIMEOUT;
327
328 idx = ehc->cmd_timeout_idx[dev->devno][ent];
329 return ata_eh_cmd_timeout_table[ent].timeouts[idx];
330}
331
332/**
333 * ata_internal_cmd_timed_out - notification for internal command timeout
334 * @dev: target device
335 * @cmd: internal command which timed out
336 *
337 * Notify EH that internal command @cmd for @dev timed out. This
338 * function should be called only for commands whose timeouts are
339 * determined using ata_internal_cmd_timeout().
340 *
341 * LOCKING:
342 * EH context.
343 */
344void ata_internal_cmd_timed_out(struct ata_device *dev, u8 cmd)
345{
346 struct ata_eh_context *ehc = &dev->link->eh_context;
347 int ent = ata_lookup_timeout_table(cmd);
348 int idx;
349
350 if (ent < 0)
351 return;
352
353 idx = ehc->cmd_timeout_idx[dev->devno][ent];
354 if (ata_eh_cmd_timeout_table[ent].timeouts[idx + 1] != ULONG_MAX)
355 ehc->cmd_timeout_idx[dev->devno][ent]++;
356}
357
Tejun Heo3884f7b2007-11-27 19:28:56 +0900358static void ata_ering_record(struct ata_ering *ering, unsigned int eflags,
Tejun Heo0c247c52006-05-15 20:58:19 +0900359 unsigned int err_mask)
360{
361 struct ata_ering_entry *ent;
362
363 WARN_ON(!err_mask);
364
365 ering->cursor++;
366 ering->cursor %= ATA_ERING_SIZE;
367
368 ent = &ering->ring[ering->cursor];
Tejun Heo3884f7b2007-11-27 19:28:56 +0900369 ent->eflags = eflags;
Tejun Heo0c247c52006-05-15 20:58:19 +0900370 ent->err_mask = err_mask;
371 ent->timestamp = get_jiffies_64();
372}
373
Tejun Heo76326ac2007-11-27 19:28:59 +0900374static struct ata_ering_entry *ata_ering_top(struct ata_ering *ering)
375{
376 struct ata_ering_entry *ent = &ering->ring[ering->cursor];
377
378 if (ent->err_mask)
379 return ent;
380 return NULL;
381}
382
Tejun Heo7d47e8d2007-02-02 16:22:31 +0900383static void ata_ering_clear(struct ata_ering *ering)
Tejun Heo0c247c52006-05-15 20:58:19 +0900384{
Tejun Heo7d47e8d2007-02-02 16:22:31 +0900385 memset(ering, 0, sizeof(*ering));
Tejun Heo0c247c52006-05-15 20:58:19 +0900386}
387
388static int ata_ering_map(struct ata_ering *ering,
389 int (*map_fn)(struct ata_ering_entry *, void *),
390 void *arg)
391{
392 int idx, rc = 0;
393 struct ata_ering_entry *ent;
394
395 idx = ering->cursor;
396 do {
397 ent = &ering->ring[idx];
398 if (!ent->err_mask)
399 break;
400 rc = map_fn(ent, arg);
401 if (rc)
402 break;
403 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
404 } while (idx != ering->cursor);
405
406 return rc;
407}
408
Tejun Heo64f65ca2006-06-24 20:30:18 +0900409static unsigned int ata_eh_dev_action(struct ata_device *dev)
410{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900411 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heo64f65ca2006-06-24 20:30:18 +0900412
413 return ehc->i.action | ehc->i.dev_action[dev->devno];
414}
415
Tejun Heof58229f2007-08-06 18:36:23 +0900416static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
Tejun Heoaf181c22006-06-24 20:30:18 +0900417 struct ata_eh_info *ehi, unsigned int action)
418{
Tejun Heof58229f2007-08-06 18:36:23 +0900419 struct ata_device *tdev;
Tejun Heoaf181c22006-06-24 20:30:18 +0900420
421 if (!dev) {
422 ehi->action &= ~action;
Tejun Heof58229f2007-08-06 18:36:23 +0900423 ata_link_for_each_dev(tdev, link)
424 ehi->dev_action[tdev->devno] &= ~action;
Tejun Heoaf181c22006-06-24 20:30:18 +0900425 } else {
426 /* doesn't make sense for port-wide EH actions */
427 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
428
429 /* break ehi->action into ehi->dev_action */
430 if (ehi->action & action) {
Tejun Heof58229f2007-08-06 18:36:23 +0900431 ata_link_for_each_dev(tdev, link)
432 ehi->dev_action[tdev->devno] |=
433 ehi->action & action;
Tejun Heoaf181c22006-06-24 20:30:18 +0900434 ehi->action &= ~action;
435 }
436
437 /* turn off the specified per-dev action */
438 ehi->dev_action[dev->devno] &= ~action;
439 }
440}
441
Tejun Heoece1d632006-04-02 18:51:53 +0900442/**
443 * ata_scsi_timed_out - SCSI layer time out callback
444 * @cmd: timed out SCSI command
445 *
446 * Handles SCSI layer timeout. We race with normal completion of
447 * the qc for @cmd. If the qc is already gone, we lose and let
448 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
449 * timed out and EH should be invoked. Prevent ata_qc_complete()
450 * from finishing it by setting EH_SCHEDULED and return
451 * EH_NOT_HANDLED.
452 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900453 * TODO: kill this function once old EH is gone.
454 *
Tejun Heoece1d632006-04-02 18:51:53 +0900455 * LOCKING:
456 * Called from timer context
457 *
458 * RETURNS:
459 * EH_HANDLED or EH_NOT_HANDLED
460 */
Jens Axboe242f9dc2008-09-14 05:55:09 -0700461enum blk_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
Tejun Heoece1d632006-04-02 18:51:53 +0900462{
463 struct Scsi_Host *host = cmd->device->host;
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400464 struct ata_port *ap = ata_shost_to_port(host);
Tejun Heoece1d632006-04-02 18:51:53 +0900465 unsigned long flags;
466 struct ata_queued_cmd *qc;
Jens Axboe242f9dc2008-09-14 05:55:09 -0700467 enum blk_eh_timer_return ret;
Tejun Heoece1d632006-04-02 18:51:53 +0900468
469 DPRINTK("ENTER\n");
470
Tejun Heoad9e2762006-05-15 20:58:12 +0900471 if (ap->ops->error_handler) {
Jens Axboe242f9dc2008-09-14 05:55:09 -0700472 ret = BLK_EH_NOT_HANDLED;
Tejun Heoad9e2762006-05-15 20:58:12 +0900473 goto out;
474 }
475
Jens Axboe242f9dc2008-09-14 05:55:09 -0700476 ret = BLK_EH_HANDLED;
Jeff Garzikba6a1302006-06-22 23:46:10 -0400477 spin_lock_irqsave(ap->lock, flags);
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900478 qc = ata_qc_from_tag(ap, ap->link.active_tag);
Tejun Heoece1d632006-04-02 18:51:53 +0900479 if (qc) {
480 WARN_ON(qc->scsicmd != cmd);
481 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
482 qc->err_mask |= AC_ERR_TIMEOUT;
Jens Axboe242f9dc2008-09-14 05:55:09 -0700483 ret = BLK_EH_NOT_HANDLED;
Tejun Heoece1d632006-04-02 18:51:53 +0900484 }
Jeff Garzikba6a1302006-06-22 23:46:10 -0400485 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900486
Tejun Heoad9e2762006-05-15 20:58:12 +0900487 out:
Tejun Heoece1d632006-04-02 18:51:53 +0900488 DPRINTK("EXIT, ret=%d\n", ret);
489 return ret;
490}
491
492/**
493 * ata_scsi_error - SCSI layer error handler callback
494 * @host: SCSI host on which error occurred
495 *
496 * Handles SCSI-layer-thrown error events.
497 *
498 * LOCKING:
499 * Inherited from SCSI layer (none, can sleep)
500 *
501 * RETURNS:
502 * Zero.
503 */
Jeff Garzik381544b2006-04-11 13:04:39 -0400504void ata_scsi_error(struct Scsi_Host *host)
Tejun Heoece1d632006-04-02 18:51:53 +0900505{
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400506 struct ata_port *ap = ata_shost_to_port(host);
Tejun Heoa1e10f72007-08-18 13:28:49 +0900507 int i;
Tejun Heoad9e2762006-05-15 20:58:12 +0900508 unsigned long flags;
Tejun Heoece1d632006-04-02 18:51:53 +0900509
510 DPRINTK("ENTER\n");
511
Tejun Heoad9e2762006-05-15 20:58:12 +0900512 /* synchronize with port task */
Tejun Heoece1d632006-04-02 18:51:53 +0900513 ata_port_flush_task(ap);
514
Jeff Garzikcca39742006-08-24 03:19:22 -0400515 /* synchronize with host lock and sort out timeouts */
Tejun Heoece1d632006-04-02 18:51:53 +0900516
Tejun Heoad9e2762006-05-15 20:58:12 +0900517 /* For new EH, all qcs are finished in one of three ways -
518 * normal completion, error completion, and SCSI timeout.
519 * Both cmpletions can race against SCSI timeout. When normal
520 * completion wins, the qc never reaches EH. When error
521 * completion wins, the qc has ATA_QCFLAG_FAILED set.
522 *
523 * When SCSI timeout wins, things are a bit more complex.
524 * Normal or error completion can occur after the timeout but
525 * before this point. In such cases, both types of
526 * completions are honored. A scmd is determined to have
527 * timed out iff its associated qc is active and not failed.
528 */
529 if (ap->ops->error_handler) {
530 struct scsi_cmnd *scmd, *tmp;
531 int nr_timedout = 0;
Tejun Heoece1d632006-04-02 18:51:53 +0900532
Tejun Heoe30349d2006-07-03 03:02:15 +0900533 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900534
535 list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
536 struct ata_queued_cmd *qc;
537
538 for (i = 0; i < ATA_MAX_QUEUE; i++) {
539 qc = __ata_qc_from_tag(ap, i);
540 if (qc->flags & ATA_QCFLAG_ACTIVE &&
541 qc->scsicmd == scmd)
542 break;
543 }
544
545 if (i < ATA_MAX_QUEUE) {
546 /* the scmd has an associated qc */
547 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
548 /* which hasn't failed yet, timeout */
549 qc->err_mask |= AC_ERR_TIMEOUT;
550 qc->flags |= ATA_QCFLAG_FAILED;
551 nr_timedout++;
552 }
553 } else {
554 /* Normal completion occurred after
555 * SCSI timeout but before this point.
556 * Successfully complete it.
557 */
558 scmd->retries = scmd->allowed;
559 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
560 }
561 }
562
563 /* If we have timed out qcs. They belong to EH from
564 * this point but the state of the controller is
565 * unknown. Freeze the port to make sure the IRQ
566 * handler doesn't diddle with those qcs. This must
567 * be done atomically w.r.t. setting QCFLAG_FAILED.
568 */
569 if (nr_timedout)
570 __ata_port_freeze(ap);
571
Tejun Heoe30349d2006-07-03 03:02:15 +0900572 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa1e10f72007-08-18 13:28:49 +0900573
574 /* initialize eh_tries */
575 ap->eh_tries = ATA_EH_MAX_TRIES;
Tejun Heoad9e2762006-05-15 20:58:12 +0900576 } else
Tejun Heoe30349d2006-07-03 03:02:15 +0900577 spin_unlock_wait(ap->lock);
Tejun Heoad9e2762006-05-15 20:58:12 +0900578
579 repeat:
580 /* invoke error handler */
581 if (ap->ops->error_handler) {
Tejun Heocf1b86c2007-08-06 18:36:23 +0900582 struct ata_link *link;
583
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900584 /* kill fast drain timer */
585 del_timer_sync(&ap->fastdrain_timer);
586
Tejun Heo500530f2006-07-03 16:07:27 +0900587 /* process port resume request */
588 ata_eh_handle_port_resume(ap);
589
Tejun Heof3e81b12006-05-15 20:58:21 +0900590 /* fetch & clear EH info */
Tejun Heoe30349d2006-07-03 03:02:15 +0900591 spin_lock_irqsave(ap->lock, flags);
Tejun Heof3e81b12006-05-15 20:58:21 +0900592
Tejun Heocf1b86c2007-08-06 18:36:23 +0900593 __ata_port_for_each_link(link, ap) {
Tejun Heo00115e02007-11-27 19:28:58 +0900594 struct ata_eh_context *ehc = &link->eh_context;
595 struct ata_device *dev;
596
Tejun Heocf1b86c2007-08-06 18:36:23 +0900597 memset(&link->eh_context, 0, sizeof(link->eh_context));
598 link->eh_context.i = link->eh_info;
599 memset(&link->eh_info, 0, sizeof(link->eh_info));
Tejun Heo00115e02007-11-27 19:28:58 +0900600
601 ata_link_for_each_dev(dev, link) {
602 int devno = dev->devno;
603
604 ehc->saved_xfer_mode[devno] = dev->xfer_mode;
605 if (ata_ncq_enabled(dev))
606 ehc->saved_ncq_enabled |= 1 << devno;
607 }
Tejun Heo0a2c0f52008-05-20 02:17:52 +0900608
609 /* set last reset timestamp to some time in the past */
610 ehc->last_reset = jiffies - 60 * HZ;
Tejun Heocf1b86c2007-08-06 18:36:23 +0900611 }
Tejun Heof3e81b12006-05-15 20:58:21 +0900612
Tejun Heob51e9e52006-06-29 01:29:30 +0900613 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
614 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
Tejun Heoda917d62007-09-23 13:14:12 +0900615 ap->excl_link = NULL; /* don't maintain exclusion over EH */
Tejun Heof3e81b12006-05-15 20:58:21 +0900616
Tejun Heoe30349d2006-07-03 03:02:15 +0900617 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900618
Tejun Heo500530f2006-07-03 16:07:27 +0900619 /* invoke EH, skip if unloading or suspended */
620 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
Tejun Heo720ba122006-05-31 18:28:13 +0900621 ap->ops->error_handler(ap);
622 else
623 ata_eh_finish(ap);
Tejun Heoad9e2762006-05-15 20:58:12 +0900624
Tejun Heo500530f2006-07-03 16:07:27 +0900625 /* process port suspend request */
626 ata_eh_handle_port_suspend(ap);
627
Tejun Heoad9e2762006-05-15 20:58:12 +0900628 /* Exception might have happend after ->error_handler
629 * recovered the port but before this point. Repeat
630 * EH in such case.
631 */
Tejun Heoe30349d2006-07-03 03:02:15 +0900632 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900633
Tejun Heob51e9e52006-06-29 01:29:30 +0900634 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
Tejun Heoa1e10f72007-08-18 13:28:49 +0900635 if (--ap->eh_tries) {
Tejun Heoe30349d2006-07-03 03:02:15 +0900636 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900637 goto repeat;
638 }
639 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
Tejun Heoa1e10f72007-08-18 13:28:49 +0900640 "tries, giving up\n", ATA_EH_MAX_TRIES);
Tejun Heo914616a2007-06-25 21:47:11 +0900641 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
Tejun Heoad9e2762006-05-15 20:58:12 +0900642 }
643
Tejun Heof3e81b12006-05-15 20:58:21 +0900644 /* this run is complete, make sure EH info is clear */
Tejun Heocf1b86c2007-08-06 18:36:23 +0900645 __ata_port_for_each_link(link, ap)
646 memset(&link->eh_info, 0, sizeof(link->eh_info));
Tejun Heof3e81b12006-05-15 20:58:21 +0900647
Tejun Heoe30349d2006-07-03 03:02:15 +0900648 /* Clear host_eh_scheduled while holding ap->lock such
Tejun Heoad9e2762006-05-15 20:58:12 +0900649 * that if exception occurs after this point but
650 * before EH completion, SCSI midlayer will
651 * re-initiate EH.
652 */
653 host->host_eh_scheduled = 0;
654
Tejun Heoe30349d2006-07-03 03:02:15 +0900655 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900656 } else {
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900657 WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
Tejun Heoad9e2762006-05-15 20:58:12 +0900658 ap->ops->eng_timeout(ap);
659 }
660
661 /* finish or retry handled scmd's and clean up */
Tejun Heoece1d632006-04-02 18:51:53 +0900662 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
663
664 scsi_eh_flush_done_q(&ap->eh_done_q);
665
Tejun Heoad9e2762006-05-15 20:58:12 +0900666 /* clean up */
Tejun Heoe30349d2006-07-03 03:02:15 +0900667 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900668
Tejun Heo1cdaf532006-07-03 16:07:26 +0900669 if (ap->pflags & ATA_PFLAG_LOADING)
Tejun Heob51e9e52006-06-29 01:29:30 +0900670 ap->pflags &= ~ATA_PFLAG_LOADING;
Tejun Heo1cdaf532006-07-03 16:07:26 +0900671 else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
David Howells52bad642006-11-22 14:54:01 +0000672 queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0);
Tejun Heo1cdaf532006-07-03 16:07:26 +0900673
674 if (ap->pflags & ATA_PFLAG_RECOVERED)
675 ata_port_printk(ap, KERN_INFO, "EH complete\n");
Tejun Heo580b2102006-05-31 18:28:05 +0900676
Tejun Heob51e9e52006-06-29 01:29:30 +0900677 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
Tejun Heoad9e2762006-05-15 20:58:12 +0900678
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900679 /* tell wait_eh that we're done */
Tejun Heob51e9e52006-06-29 01:29:30 +0900680 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900681 wake_up_all(&ap->eh_wait_q);
682
Tejun Heoe30349d2006-07-03 03:02:15 +0900683 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900684
Tejun Heoece1d632006-04-02 18:51:53 +0900685 DPRINTK("EXIT\n");
Tejun Heoece1d632006-04-02 18:51:53 +0900686}
687
688/**
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900689 * ata_port_wait_eh - Wait for the currently pending EH to complete
690 * @ap: Port to wait EH for
691 *
692 * Wait until the currently pending EH is complete.
693 *
694 * LOCKING:
695 * Kernel thread context (may sleep).
696 */
697void ata_port_wait_eh(struct ata_port *ap)
698{
699 unsigned long flags;
700 DEFINE_WAIT(wait);
701
702 retry:
Jeff Garzikba6a1302006-06-22 23:46:10 -0400703 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900704
Tejun Heob51e9e52006-06-29 01:29:30 +0900705 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900706 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
Jeff Garzikba6a1302006-06-22 23:46:10 -0400707 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900708 schedule();
Jeff Garzikba6a1302006-06-22 23:46:10 -0400709 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900710 }
Tejun Heo0a1b6222006-06-11 11:01:38 +0900711 finish_wait(&ap->eh_wait_q, &wait);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900712
Jeff Garzikba6a1302006-06-22 23:46:10 -0400713 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900714
715 /* make sure SCSI EH is complete */
Jeff Garzikcca39742006-08-24 03:19:22 -0400716 if (scsi_host_in_recovery(ap->scsi_host)) {
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900717 msleep(10);
718 goto retry;
719 }
720}
721
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900722static int ata_eh_nr_in_flight(struct ata_port *ap)
723{
724 unsigned int tag;
725 int nr = 0;
726
727 /* count only non-internal commands */
728 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++)
729 if (ata_qc_from_tag(ap, tag))
730 nr++;
731
732 return nr;
733}
734
735void ata_eh_fastdrain_timerfn(unsigned long arg)
736{
737 struct ata_port *ap = (void *)arg;
738 unsigned long flags;
739 int cnt;
740
741 spin_lock_irqsave(ap->lock, flags);
742
743 cnt = ata_eh_nr_in_flight(ap);
744
745 /* are we done? */
746 if (!cnt)
747 goto out_unlock;
748
749 if (cnt == ap->fastdrain_cnt) {
750 unsigned int tag;
751
752 /* No progress during the last interval, tag all
753 * in-flight qcs as timed out and freeze the port.
754 */
755 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) {
756 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
757 if (qc)
758 qc->err_mask |= AC_ERR_TIMEOUT;
759 }
760
761 ata_port_freeze(ap);
762 } else {
763 /* some qcs have finished, give it another chance */
764 ap->fastdrain_cnt = cnt;
765 ap->fastdrain_timer.expires =
Tejun Heo341c2c92008-05-20 02:17:51 +0900766 ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900767 add_timer(&ap->fastdrain_timer);
768 }
769
770 out_unlock:
771 spin_unlock_irqrestore(ap->lock, flags);
772}
773
774/**
775 * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
776 * @ap: target ATA port
777 * @fastdrain: activate fast drain
778 *
779 * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
780 * is non-zero and EH wasn't pending before. Fast drain ensures
781 * that EH kicks in in timely manner.
782 *
783 * LOCKING:
784 * spin_lock_irqsave(host lock)
785 */
786static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
787{
788 int cnt;
789
790 /* already scheduled? */
791 if (ap->pflags & ATA_PFLAG_EH_PENDING)
792 return;
793
794 ap->pflags |= ATA_PFLAG_EH_PENDING;
795
796 if (!fastdrain)
797 return;
798
799 /* do we have in-flight qcs? */
800 cnt = ata_eh_nr_in_flight(ap);
801 if (!cnt)
802 return;
803
804 /* activate fast drain */
805 ap->fastdrain_cnt = cnt;
Tejun Heo341c2c92008-05-20 02:17:51 +0900806 ap->fastdrain_timer.expires =
807 ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900808 add_timer(&ap->fastdrain_timer);
809}
810
Tejun Heof686bcb2006-05-15 20:58:05 +0900811/**
812 * ata_qc_schedule_eh - schedule qc for error handling
813 * @qc: command to schedule error handling for
814 *
815 * Schedule error handling for @qc. EH will kick in as soon as
816 * other commands are drained.
817 *
818 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400819 * spin_lock_irqsave(host lock)
Tejun Heof686bcb2006-05-15 20:58:05 +0900820 */
821void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
822{
823 struct ata_port *ap = qc->ap;
824
825 WARN_ON(!ap->ops->error_handler);
826
827 qc->flags |= ATA_QCFLAG_FAILED;
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900828 ata_eh_set_pending(ap, 1);
Tejun Heof686bcb2006-05-15 20:58:05 +0900829
830 /* The following will fail if timeout has already expired.
831 * ata_scsi_error() takes care of such scmds on EH entry.
832 * Note that ATA_QCFLAG_FAILED is unconditionally set after
833 * this function completes.
834 */
Jens Axboe242f9dc2008-09-14 05:55:09 -0700835 blk_abort_request(qc->scsicmd->request);
Tejun Heof686bcb2006-05-15 20:58:05 +0900836}
837
Tejun Heo7b70fc02006-05-15 20:58:07 +0900838/**
839 * ata_port_schedule_eh - schedule error handling without a qc
840 * @ap: ATA port to schedule EH for
841 *
842 * Schedule error handling for @ap. EH will kick in as soon as
843 * all commands are drained.
844 *
845 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400846 * spin_lock_irqsave(host lock)
Tejun Heo7b70fc02006-05-15 20:58:07 +0900847 */
848void ata_port_schedule_eh(struct ata_port *ap)
849{
850 WARN_ON(!ap->ops->error_handler);
851
Tejun Heof4d6d002007-05-01 11:50:15 +0200852 if (ap->pflags & ATA_PFLAG_INITIALIZING)
853 return;
854
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900855 ata_eh_set_pending(ap, 1);
Jeff Garzikcca39742006-08-24 03:19:22 -0400856 scsi_schedule_eh(ap->scsi_host);
Tejun Heo7b70fc02006-05-15 20:58:07 +0900857
858 DPRINTK("port EH scheduled\n");
859}
860
Tejun Heodbd82612007-08-06 18:36:23 +0900861static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
Tejun Heo7b70fc02006-05-15 20:58:07 +0900862{
863 int tag, nr_aborted = 0;
864
865 WARN_ON(!ap->ops->error_handler);
866
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900867 /* we're gonna abort all commands, no need for fast drain */
868 ata_eh_set_pending(ap, 0);
869
Tejun Heo7b70fc02006-05-15 20:58:07 +0900870 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
871 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
872
Tejun Heodbd82612007-08-06 18:36:23 +0900873 if (qc && (!link || qc->dev->link == link)) {
Tejun Heo7b70fc02006-05-15 20:58:07 +0900874 qc->flags |= ATA_QCFLAG_FAILED;
875 ata_qc_complete(qc);
876 nr_aborted++;
877 }
878 }
879
880 if (!nr_aborted)
881 ata_port_schedule_eh(ap);
882
883 return nr_aborted;
884}
885
Tejun Heoe3180492006-05-15 20:58:09 +0900886/**
Tejun Heodbd82612007-08-06 18:36:23 +0900887 * ata_link_abort - abort all qc's on the link
888 * @link: ATA link to abort qc's for
889 *
890 * Abort all active qc's active on @link and schedule EH.
891 *
892 * LOCKING:
893 * spin_lock_irqsave(host lock)
894 *
895 * RETURNS:
896 * Number of aborted qc's.
897 */
898int ata_link_abort(struct ata_link *link)
899{
900 return ata_do_link_abort(link->ap, link);
901}
902
903/**
904 * ata_port_abort - abort all qc's on the port
905 * @ap: ATA port to abort qc's for
906 *
907 * Abort all active qc's of @ap and schedule EH.
908 *
909 * LOCKING:
910 * spin_lock_irqsave(host_set lock)
911 *
912 * RETURNS:
913 * Number of aborted qc's.
914 */
915int ata_port_abort(struct ata_port *ap)
916{
917 return ata_do_link_abort(ap, NULL);
918}
919
920/**
Tejun Heoe3180492006-05-15 20:58:09 +0900921 * __ata_port_freeze - freeze port
922 * @ap: ATA port to freeze
923 *
924 * This function is called when HSM violation or some other
925 * condition disrupts normal operation of the port. Frozen port
926 * is not allowed to perform any operation until the port is
927 * thawed, which usually follows a successful reset.
928 *
929 * ap->ops->freeze() callback can be used for freezing the port
930 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
931 * port cannot be frozen hardware-wise, the interrupt handler
932 * must ack and clear interrupts unconditionally while the port
933 * is frozen.
934 *
935 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400936 * spin_lock_irqsave(host lock)
Tejun Heoe3180492006-05-15 20:58:09 +0900937 */
938static void __ata_port_freeze(struct ata_port *ap)
939{
940 WARN_ON(!ap->ops->error_handler);
941
942 if (ap->ops->freeze)
943 ap->ops->freeze(ap);
944
Tejun Heob51e9e52006-06-29 01:29:30 +0900945 ap->pflags |= ATA_PFLAG_FROZEN;
Tejun Heoe3180492006-05-15 20:58:09 +0900946
Tejun Heo44877b42007-02-21 01:06:51 +0900947 DPRINTK("ata%u port frozen\n", ap->print_id);
Tejun Heoe3180492006-05-15 20:58:09 +0900948}
949
950/**
951 * ata_port_freeze - abort & freeze port
952 * @ap: ATA port to freeze
953 *
954 * Abort and freeze @ap.
955 *
956 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400957 * spin_lock_irqsave(host lock)
Tejun Heoe3180492006-05-15 20:58:09 +0900958 *
959 * RETURNS:
960 * Number of aborted commands.
961 */
962int ata_port_freeze(struct ata_port *ap)
963{
964 int nr_aborted;
965
966 WARN_ON(!ap->ops->error_handler);
967
968 nr_aborted = ata_port_abort(ap);
969 __ata_port_freeze(ap);
970
971 return nr_aborted;
972}
973
974/**
Tejun Heo7d77b242007-09-23 13:14:13 +0900975 * sata_async_notification - SATA async notification handler
976 * @ap: ATA port where async notification is received
977 *
978 * Handler to be called when async notification via SDB FIS is
979 * received. This function schedules EH if necessary.
980 *
981 * LOCKING:
982 * spin_lock_irqsave(host lock)
983 *
984 * RETURNS:
985 * 1 if EH is scheduled, 0 otherwise.
986 */
987int sata_async_notification(struct ata_port *ap)
988{
989 u32 sntf;
990 int rc;
991
992 if (!(ap->flags & ATA_FLAG_AN))
993 return 0;
994
995 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
996 if (rc == 0)
997 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
998
Tejun Heo071f44b2008-04-07 22:47:22 +0900999 if (!sata_pmp_attached(ap) || rc) {
Tejun Heo7d77b242007-09-23 13:14:13 +09001000 /* PMP is not attached or SNTF is not available */
Tejun Heo071f44b2008-04-07 22:47:22 +09001001 if (!sata_pmp_attached(ap)) {
Tejun Heo7d77b242007-09-23 13:14:13 +09001002 /* PMP is not attached. Check whether ATAPI
1003 * AN is configured. If so, notify media
1004 * change.
1005 */
1006 struct ata_device *dev = ap->link.device;
1007
1008 if ((dev->class == ATA_DEV_ATAPI) &&
1009 (dev->flags & ATA_DFLAG_AN))
1010 ata_scsi_media_change_notify(dev);
1011 return 0;
1012 } else {
1013 /* PMP is attached but SNTF is not available.
1014 * ATAPI async media change notification is
1015 * not used. The PMP must be reporting PHY
1016 * status change, schedule EH.
1017 */
1018 ata_port_schedule_eh(ap);
1019 return 1;
1020 }
1021 } else {
1022 /* PMP is attached and SNTF is available */
1023 struct ata_link *link;
1024
1025 /* check and notify ATAPI AN */
1026 ata_port_for_each_link(link, ap) {
1027 if (!(sntf & (1 << link->pmp)))
1028 continue;
1029
1030 if ((link->device->class == ATA_DEV_ATAPI) &&
1031 (link->device->flags & ATA_DFLAG_AN))
1032 ata_scsi_media_change_notify(link->device);
1033 }
1034
1035 /* If PMP is reporting that PHY status of some
1036 * downstream ports has changed, schedule EH.
1037 */
1038 if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
1039 ata_port_schedule_eh(ap);
1040 return 1;
1041 }
1042
1043 return 0;
1044 }
1045}
1046
1047/**
Tejun Heoe3180492006-05-15 20:58:09 +09001048 * ata_eh_freeze_port - EH helper to freeze port
1049 * @ap: ATA port to freeze
1050 *
1051 * Freeze @ap.
1052 *
1053 * LOCKING:
1054 * None.
1055 */
1056void ata_eh_freeze_port(struct ata_port *ap)
1057{
1058 unsigned long flags;
1059
1060 if (!ap->ops->error_handler)
1061 return;
1062
Jeff Garzikba6a1302006-06-22 23:46:10 -04001063 spin_lock_irqsave(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001064 __ata_port_freeze(ap);
Jeff Garzikba6a1302006-06-22 23:46:10 -04001065 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001066}
1067
1068/**
1069 * ata_port_thaw_port - EH helper to thaw port
1070 * @ap: ATA port to thaw
1071 *
1072 * Thaw frozen port @ap.
1073 *
1074 * LOCKING:
1075 * None.
1076 */
1077void ata_eh_thaw_port(struct ata_port *ap)
1078{
1079 unsigned long flags;
1080
1081 if (!ap->ops->error_handler)
1082 return;
1083
Jeff Garzikba6a1302006-06-22 23:46:10 -04001084 spin_lock_irqsave(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001085
Tejun Heob51e9e52006-06-29 01:29:30 +09001086 ap->pflags &= ~ATA_PFLAG_FROZEN;
Tejun Heoe3180492006-05-15 20:58:09 +09001087
1088 if (ap->ops->thaw)
1089 ap->ops->thaw(ap);
1090
Jeff Garzikba6a1302006-06-22 23:46:10 -04001091 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001092
Tejun Heo44877b42007-02-21 01:06:51 +09001093 DPRINTK("ata%u port thawed\n", ap->print_id);
Tejun Heoe3180492006-05-15 20:58:09 +09001094}
1095
Tejun Heoece1d632006-04-02 18:51:53 +09001096static void ata_eh_scsidone(struct scsi_cmnd *scmd)
1097{
1098 /* nada */
1099}
1100
1101static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
1102{
1103 struct ata_port *ap = qc->ap;
1104 struct scsi_cmnd *scmd = qc->scsicmd;
1105 unsigned long flags;
1106
Jeff Garzikba6a1302006-06-22 23:46:10 -04001107 spin_lock_irqsave(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +09001108 qc->scsidone = ata_eh_scsidone;
1109 __ata_qc_complete(qc);
1110 WARN_ON(ata_tag_valid(qc->tag));
Jeff Garzikba6a1302006-06-22 23:46:10 -04001111 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +09001112
1113 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
1114}
1115
1116/**
1117 * ata_eh_qc_complete - Complete an active ATA command from EH
1118 * @qc: Command to complete
1119 *
1120 * Indicate to the mid and upper layers that an ATA command has
1121 * completed. To be used from EH.
1122 */
1123void ata_eh_qc_complete(struct ata_queued_cmd *qc)
1124{
1125 struct scsi_cmnd *scmd = qc->scsicmd;
1126 scmd->retries = scmd->allowed;
1127 __ata_eh_qc_complete(qc);
1128}
1129
1130/**
1131 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
1132 * @qc: Command to retry
1133 *
1134 * Indicate to the mid and upper layers that an ATA command
1135 * should be retried. To be used from EH.
1136 *
1137 * SCSI midlayer limits the number of retries to scmd->allowed.
1138 * scmd->retries is decremented for commands which get retried
1139 * due to unrelated failures (qc->err_mask is zero).
1140 */
1141void ata_eh_qc_retry(struct ata_queued_cmd *qc)
1142{
1143 struct scsi_cmnd *scmd = qc->scsicmd;
1144 if (!qc->err_mask && scmd->retries)
1145 scmd->retries--;
1146 __ata_eh_qc_complete(qc);
1147}
Tejun Heo022bdb02006-05-15 20:58:22 +09001148
1149/**
Tejun Heo0ea035a2006-05-31 18:28:01 +09001150 * ata_eh_detach_dev - detach ATA device
1151 * @dev: ATA device to detach
1152 *
1153 * Detach @dev.
1154 *
1155 * LOCKING:
1156 * None.
1157 */
Tejun Heofb7fd612007-09-23 13:14:12 +09001158void ata_eh_detach_dev(struct ata_device *dev)
Tejun Heo0ea035a2006-05-31 18:28:01 +09001159{
Tejun Heof58229f2007-08-06 18:36:23 +09001160 struct ata_link *link = dev->link;
1161 struct ata_port *ap = link->ap;
Tejun Heo0ea035a2006-05-31 18:28:01 +09001162 unsigned long flags;
1163
1164 ata_dev_disable(dev);
1165
Jeff Garzikba6a1302006-06-22 23:46:10 -04001166 spin_lock_irqsave(ap->lock, flags);
Tejun Heo0ea035a2006-05-31 18:28:01 +09001167
1168 dev->flags &= ~ATA_DFLAG_DETACH;
1169
1170 if (ata_scsi_offline_dev(dev)) {
1171 dev->flags |= ATA_DFLAG_DETACHED;
Tejun Heob51e9e52006-06-29 01:29:30 +09001172 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
Tejun Heo0ea035a2006-05-31 18:28:01 +09001173 }
1174
Tejun Heobeb07c12006-06-24 20:30:19 +09001175 /* clear per-dev EH actions */
Tejun Heof58229f2007-08-06 18:36:23 +09001176 ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
1177 ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
Tejun Heobeb07c12006-06-24 20:30:19 +09001178
Jeff Garzikba6a1302006-06-22 23:46:10 -04001179 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo0ea035a2006-05-31 18:28:01 +09001180}
1181
1182/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001183 * ata_eh_about_to_do - about to perform eh_action
Tejun Heo955e57d2007-08-06 18:36:23 +09001184 * @link: target ATA link
Tejun Heo47005f22006-06-19 18:27:23 +09001185 * @dev: target ATA dev for per-dev action (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09001186 * @action: action about to be performed
1187 *
1188 * Called just before performing EH actions to clear related bits
Tejun Heo955e57d2007-08-06 18:36:23 +09001189 * in @link->eh_info such that eh actions are not unnecessarily
1190 * repeated.
Tejun Heo022bdb02006-05-15 20:58:22 +09001191 *
1192 * LOCKING:
1193 * None.
1194 */
Tejun Heofb7fd612007-09-23 13:14:12 +09001195void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
1196 unsigned int action)
Tejun Heo022bdb02006-05-15 20:58:22 +09001197{
Tejun Heo955e57d2007-08-06 18:36:23 +09001198 struct ata_port *ap = link->ap;
1199 struct ata_eh_info *ehi = &link->eh_info;
1200 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001201 unsigned long flags;
1202
Jeff Garzikba6a1302006-06-22 23:46:10 -04001203 spin_lock_irqsave(ap->lock, flags);
Tejun Heo1cdaf532006-07-03 16:07:26 +09001204
Tejun Heo955e57d2007-08-06 18:36:23 +09001205 ata_eh_clear_action(link, dev, ehi, action);
Tejun Heo13abf502006-07-10 23:18:46 +09001206
1207 if (!(ehc->i.flags & ATA_EHI_QUIET))
Tejun Heo1cdaf532006-07-03 16:07:26 +09001208 ap->pflags |= ATA_PFLAG_RECOVERED;
1209
Jeff Garzikba6a1302006-06-22 23:46:10 -04001210 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo022bdb02006-05-15 20:58:22 +09001211}
1212
1213/**
Tejun Heo47005f22006-06-19 18:27:23 +09001214 * ata_eh_done - EH action complete
Tejun Heo955e57d2007-08-06 18:36:23 +09001215* @ap: target ATA port
Tejun Heo47005f22006-06-19 18:27:23 +09001216 * @dev: target ATA dev for per-dev action (can be NULL)
1217 * @action: action just completed
1218 *
1219 * Called right after performing EH actions to clear related bits
Tejun Heo955e57d2007-08-06 18:36:23 +09001220 * in @link->eh_context.
Tejun Heo47005f22006-06-19 18:27:23 +09001221 *
1222 * LOCKING:
1223 * None.
1224 */
Tejun Heofb7fd612007-09-23 13:14:12 +09001225void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1226 unsigned int action)
Tejun Heo47005f22006-06-19 18:27:23 +09001227{
Tejun Heo955e57d2007-08-06 18:36:23 +09001228 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001229
Tejun Heo955e57d2007-08-06 18:36:23 +09001230 ata_eh_clear_action(link, dev, &ehc->i, action);
Tejun Heo47005f22006-06-19 18:27:23 +09001231}
1232
1233/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001234 * ata_err_string - convert err_mask to descriptive string
1235 * @err_mask: error mask to convert to string
1236 *
1237 * Convert @err_mask to descriptive string. Errors are
1238 * prioritized according to severity and only the most severe
1239 * error is reported.
1240 *
1241 * LOCKING:
1242 * None.
1243 *
1244 * RETURNS:
1245 * Descriptive string for @err_mask
1246 */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001247static const char *ata_err_string(unsigned int err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001248{
1249 if (err_mask & AC_ERR_HOST_BUS)
1250 return "host bus error";
1251 if (err_mask & AC_ERR_ATA_BUS)
1252 return "ATA bus error";
1253 if (err_mask & AC_ERR_TIMEOUT)
1254 return "timeout";
1255 if (err_mask & AC_ERR_HSM)
1256 return "HSM violation";
1257 if (err_mask & AC_ERR_SYSTEM)
1258 return "internal error";
1259 if (err_mask & AC_ERR_MEDIA)
1260 return "media error";
1261 if (err_mask & AC_ERR_INVALID)
1262 return "invalid argument";
1263 if (err_mask & AC_ERR_DEV)
1264 return "device error";
1265 return "unknown error";
1266}
1267
1268/**
Tejun Heoe8ee8452006-05-15 21:03:46 +09001269 * ata_read_log_page - read a specific log page
1270 * @dev: target device
1271 * @page: page to read
1272 * @buf: buffer to store read page
1273 * @sectors: number of sectors to read
1274 *
1275 * Read log page using READ_LOG_EXT command.
1276 *
1277 * LOCKING:
1278 * Kernel thread context (may sleep).
1279 *
1280 * RETURNS:
1281 * 0 on success, AC_ERR_* mask otherwise.
1282 */
1283static unsigned int ata_read_log_page(struct ata_device *dev,
1284 u8 page, void *buf, unsigned int sectors)
1285{
1286 struct ata_taskfile tf;
1287 unsigned int err_mask;
1288
1289 DPRINTK("read log page - page %d\n", page);
1290
1291 ata_tf_init(dev, &tf);
1292 tf.command = ATA_CMD_READ_LOG_EXT;
1293 tf.lbal = page;
1294 tf.nsect = sectors;
1295 tf.hob_nsect = sectors >> 8;
1296 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1297 tf.protocol = ATA_PROT_PIO;
1298
1299 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
Tejun Heo2b789102007-10-09 15:05:44 +09001300 buf, sectors * ATA_SECT_SIZE, 0);
Tejun Heoe8ee8452006-05-15 21:03:46 +09001301
1302 DPRINTK("EXIT, err_mask=%x\n", err_mask);
1303 return err_mask;
1304}
1305
1306/**
1307 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
1308 * @dev: Device to read log page 10h from
1309 * @tag: Resulting tag of the failed command
1310 * @tf: Resulting taskfile registers of the failed command
1311 *
1312 * Read log page 10h to obtain NCQ error details and clear error
1313 * condition.
1314 *
1315 * LOCKING:
1316 * Kernel thread context (may sleep).
1317 *
1318 * RETURNS:
1319 * 0 on success, -errno otherwise.
1320 */
1321static int ata_eh_read_log_10h(struct ata_device *dev,
1322 int *tag, struct ata_taskfile *tf)
1323{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001324 u8 *buf = dev->link->ap->sector_buf;
Tejun Heoe8ee8452006-05-15 21:03:46 +09001325 unsigned int err_mask;
1326 u8 csum;
1327 int i;
1328
1329 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
1330 if (err_mask)
1331 return -EIO;
1332
1333 csum = 0;
1334 for (i = 0; i < ATA_SECT_SIZE; i++)
1335 csum += buf[i];
1336 if (csum)
1337 ata_dev_printk(dev, KERN_WARNING,
1338 "invalid checksum 0x%x on log page 10h\n", csum);
1339
1340 if (buf[0] & 0x80)
1341 return -ENOENT;
1342
1343 *tag = buf[0] & 0x1f;
1344
1345 tf->command = buf[2];
1346 tf->feature = buf[3];
1347 tf->lbal = buf[4];
1348 tf->lbam = buf[5];
1349 tf->lbah = buf[6];
1350 tf->device = buf[7];
1351 tf->hob_lbal = buf[8];
1352 tf->hob_lbam = buf[9];
1353 tf->hob_lbah = buf[10];
1354 tf->nsect = buf[12];
1355 tf->hob_nsect = buf[13];
1356
1357 return 0;
1358}
1359
1360/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001361 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1362 * @dev: device to perform REQUEST_SENSE to
1363 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
Tejun Heo3eabddb2008-06-10 18:28:05 +09001364 * @dfl_sense_key: default sense key to use
Tejun Heo022bdb02006-05-15 20:58:22 +09001365 *
1366 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
1367 * SENSE. This function is EH helper.
1368 *
1369 * LOCKING:
1370 * Kernel thread context (may sleep).
1371 *
1372 * RETURNS:
1373 * 0 on success, AC_ERR_* mask on failure
1374 */
Tejun Heo3eabddb2008-06-10 18:28:05 +09001375static unsigned int atapi_eh_request_sense(struct ata_device *dev,
1376 u8 *sense_buf, u8 dfl_sense_key)
Tejun Heo022bdb02006-05-15 20:58:22 +09001377{
Tejun Heo3eabddb2008-06-10 18:28:05 +09001378 u8 cdb[ATAPI_CDB_LEN] =
1379 { REQUEST_SENSE, 0, 0, 0, SCSI_SENSE_BUFFERSIZE, 0 };
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001380 struct ata_port *ap = dev->link->ap;
Tejun Heo022bdb02006-05-15 20:58:22 +09001381 struct ata_taskfile tf;
Tejun Heo022bdb02006-05-15 20:58:22 +09001382
1383 DPRINTK("ATAPI request sense\n");
1384
Tejun Heo022bdb02006-05-15 20:58:22 +09001385 /* FIXME: is this needed? */
1386 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1387
Albert Lee56287762007-04-02 11:30:46 +08001388 /* initialize sense_buf with the error register,
1389 * for the case where they are -not- overwritten
1390 */
Tejun Heo022bdb02006-05-15 20:58:22 +09001391 sense_buf[0] = 0x70;
Tejun Heo3eabddb2008-06-10 18:28:05 +09001392 sense_buf[2] = dfl_sense_key;
Albert Lee56287762007-04-02 11:30:46 +08001393
Jeff Garzika617c092007-05-21 20:14:23 -04001394 /* some devices time out if garbage left in tf */
Albert Lee56287762007-04-02 11:30:46 +08001395 ata_tf_init(dev, &tf);
Tejun Heo022bdb02006-05-15 20:58:22 +09001396
Tejun Heo022bdb02006-05-15 20:58:22 +09001397 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1398 tf.command = ATA_CMD_PACKET;
1399
1400 /* is it pointless to prefer PIO for "safety reasons"? */
1401 if (ap->flags & ATA_FLAG_PIO_DMA) {
Tejun Heo0dc36882007-12-18 16:34:43 -05001402 tf.protocol = ATAPI_PROT_DMA;
Tejun Heo022bdb02006-05-15 20:58:22 +09001403 tf.feature |= ATAPI_PKT_DMA;
1404 } else {
Tejun Heo0dc36882007-12-18 16:34:43 -05001405 tf.protocol = ATAPI_PROT_PIO;
Tejun Heof2dfc1a2007-12-12 12:12:46 +09001406 tf.lbam = SCSI_SENSE_BUFFERSIZE;
1407 tf.lbah = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09001408 }
1409
1410 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
Tejun Heo2b789102007-10-09 15:05:44 +09001411 sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
Tejun Heo022bdb02006-05-15 20:58:22 +09001412}
1413
1414/**
1415 * ata_eh_analyze_serror - analyze SError for a failed port
Tejun Heo02607312007-08-06 18:36:23 +09001416 * @link: ATA link to analyze SError for
Tejun Heo022bdb02006-05-15 20:58:22 +09001417 *
1418 * Analyze SError if available and further determine cause of
1419 * failure.
1420 *
1421 * LOCKING:
1422 * None.
1423 */
Tejun Heo02607312007-08-06 18:36:23 +09001424static void ata_eh_analyze_serror(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001425{
Tejun Heo02607312007-08-06 18:36:23 +09001426 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001427 u32 serror = ehc->i.serror;
1428 unsigned int err_mask = 0, action = 0;
Tejun Heof9df58c2007-09-23 13:14:13 +09001429 u32 hotplug_mask;
Tejun Heo022bdb02006-05-15 20:58:22 +09001430
Tejun Heoe0614db2008-05-19 01:15:09 +09001431 if (serror & (SERR_PERSISTENT | SERR_DATA)) {
Tejun Heo022bdb02006-05-15 20:58:22 +09001432 err_mask |= AC_ERR_ATA_BUS;
Tejun Heocf480622008-01-24 00:05:14 +09001433 action |= ATA_EH_RESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001434 }
1435 if (serror & SERR_PROTOCOL) {
1436 err_mask |= AC_ERR_HSM;
Tejun Heocf480622008-01-24 00:05:14 +09001437 action |= ATA_EH_RESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001438 }
1439 if (serror & SERR_INTERNAL) {
1440 err_mask |= AC_ERR_SYSTEM;
Tejun Heocf480622008-01-24 00:05:14 +09001441 action |= ATA_EH_RESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001442 }
Tejun Heof9df58c2007-09-23 13:14:13 +09001443
1444 /* Determine whether a hotplug event has occurred. Both
1445 * SError.N/X are considered hotplug events for enabled or
1446 * host links. For disabled PMP links, only N bit is
1447 * considered as X bit is left at 1 for link plugging.
1448 */
1449 hotplug_mask = 0;
1450
1451 if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
1452 hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
1453 else
1454 hotplug_mask = SERR_PHYRDY_CHG;
1455
1456 if (serror & hotplug_mask)
Tejun Heo084fe632006-05-31 18:28:03 +09001457 ata_ehi_hotplugged(&ehc->i);
Tejun Heo022bdb02006-05-15 20:58:22 +09001458
1459 ehc->i.err_mask |= err_mask;
1460 ehc->i.action |= action;
1461}
1462
1463/**
Tejun Heoe8ee8452006-05-15 21:03:46 +09001464 * ata_eh_analyze_ncq_error - analyze NCQ error
Tejun Heo02607312007-08-06 18:36:23 +09001465 * @link: ATA link to analyze NCQ error for
Tejun Heoe8ee8452006-05-15 21:03:46 +09001466 *
1467 * Read log page 10h, determine the offending qc and acquire
1468 * error status TF. For NCQ device errors, all LLDDs have to do
1469 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1470 * care of the rest.
1471 *
1472 * LOCKING:
1473 * Kernel thread context (may sleep).
1474 */
Mark Lord10acf3b2008-05-02 02:14:53 -04001475void ata_eh_analyze_ncq_error(struct ata_link *link)
Tejun Heoe8ee8452006-05-15 21:03:46 +09001476{
Tejun Heo02607312007-08-06 18:36:23 +09001477 struct ata_port *ap = link->ap;
1478 struct ata_eh_context *ehc = &link->eh_context;
1479 struct ata_device *dev = link->device;
Tejun Heoe8ee8452006-05-15 21:03:46 +09001480 struct ata_queued_cmd *qc;
1481 struct ata_taskfile tf;
1482 int tag, rc;
1483
1484 /* if frozen, we can't do much */
Tejun Heob51e9e52006-06-29 01:29:30 +09001485 if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heoe8ee8452006-05-15 21:03:46 +09001486 return;
1487
1488 /* is it NCQ device error? */
Tejun Heo02607312007-08-06 18:36:23 +09001489 if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
Tejun Heoe8ee8452006-05-15 21:03:46 +09001490 return;
1491
1492 /* has LLDD analyzed already? */
1493 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1494 qc = __ata_qc_from_tag(ap, tag);
1495
1496 if (!(qc->flags & ATA_QCFLAG_FAILED))
1497 continue;
1498
1499 if (qc->err_mask)
1500 return;
1501 }
1502
1503 /* okay, this error is ours */
1504 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1505 if (rc) {
Tejun Heo02607312007-08-06 18:36:23 +09001506 ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
Tejun Heoe8ee8452006-05-15 21:03:46 +09001507 "(errno=%d)\n", rc);
1508 return;
1509 }
1510
Tejun Heo02607312007-08-06 18:36:23 +09001511 if (!(link->sactive & (1 << tag))) {
1512 ata_link_printk(link, KERN_ERR, "log page 10h reported "
Tejun Heoe8ee8452006-05-15 21:03:46 +09001513 "inactive tag %d\n", tag);
1514 return;
1515 }
1516
1517 /* we've got the perpetrator, condemn it */
1518 qc = __ata_qc_from_tag(ap, tag);
1519 memcpy(&qc->result_tf, &tf, sizeof(tf));
Mark Lorda6116c92008-04-23 22:36:25 -04001520 qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
Tejun Heo5335b722007-07-16 14:29:40 +09001521 qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
Tejun Heoe8ee8452006-05-15 21:03:46 +09001522 ehc->i.err_mask &= ~AC_ERR_DEV;
1523}
1524
1525/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001526 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1527 * @qc: qc to analyze
1528 * @tf: Taskfile registers to analyze
1529 *
1530 * Analyze taskfile of @qc and further determine cause of
1531 * failure. This function also requests ATAPI sense data if
1532 * avaliable.
1533 *
1534 * LOCKING:
1535 * Kernel thread context (may sleep).
1536 *
1537 * RETURNS:
1538 * Determined recovery action
1539 */
1540static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1541 const struct ata_taskfile *tf)
1542{
1543 unsigned int tmp, action = 0;
1544 u8 stat = tf->command, err = tf->feature;
1545
1546 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1547 qc->err_mask |= AC_ERR_HSM;
Tejun Heocf480622008-01-24 00:05:14 +09001548 return ATA_EH_RESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001549 }
1550
Tejun Heoa51d6442007-03-20 15:24:11 +09001551 if (stat & (ATA_ERR | ATA_DF))
1552 qc->err_mask |= AC_ERR_DEV;
1553 else
Tejun Heo022bdb02006-05-15 20:58:22 +09001554 return 0;
1555
1556 switch (qc->dev->class) {
1557 case ATA_DEV_ATA:
1558 if (err & ATA_ICRC)
1559 qc->err_mask |= AC_ERR_ATA_BUS;
1560 if (err & ATA_UNC)
1561 qc->err_mask |= AC_ERR_MEDIA;
1562 if (err & ATA_IDNF)
1563 qc->err_mask |= AC_ERR_INVALID;
1564 break;
1565
1566 case ATA_DEV_ATAPI:
Tejun Heoa569a302006-11-21 10:40:51 +09001567 if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
Tejun Heo3eabddb2008-06-10 18:28:05 +09001568 tmp = atapi_eh_request_sense(qc->dev,
1569 qc->scsicmd->sense_buffer,
1570 qc->result_tf.feature >> 4);
Tejun Heoa569a302006-11-21 10:40:51 +09001571 if (!tmp) {
1572 /* ATA_QCFLAG_SENSE_VALID is used to
1573 * tell atapi_qc_complete() that sense
1574 * data is already valid.
1575 *
1576 * TODO: interpret sense data and set
1577 * appropriate err_mask.
1578 */
1579 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1580 } else
1581 qc->err_mask |= tmp;
1582 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001583 }
1584
1585 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
Tejun Heocf480622008-01-24 00:05:14 +09001586 action |= ATA_EH_RESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001587
1588 return action;
1589}
1590
Tejun Heo76326ac2007-11-27 19:28:59 +09001591static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask,
1592 int *xfer_ok)
Tejun Heo022bdb02006-05-15 20:58:22 +09001593{
Tejun Heo76326ac2007-11-27 19:28:59 +09001594 int base = 0;
1595
1596 if (!(eflags & ATA_EFLAG_DUBIOUS_XFER))
1597 *xfer_ok = 1;
1598
1599 if (!*xfer_ok)
Tejun Heo75f9caf2008-01-03 01:21:14 +09001600 base = ATA_ECAT_DUBIOUS_NONE;
Tejun Heo76326ac2007-11-27 19:28:59 +09001601
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001602 if (err_mask & AC_ERR_ATA_BUS)
Tejun Heo76326ac2007-11-27 19:28:59 +09001603 return base + ATA_ECAT_ATA_BUS;
Tejun Heo022bdb02006-05-15 20:58:22 +09001604
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001605 if (err_mask & AC_ERR_TIMEOUT)
Tejun Heo76326ac2007-11-27 19:28:59 +09001606 return base + ATA_ECAT_TOUT_HSM;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001607
Tejun Heo3884f7b2007-11-27 19:28:56 +09001608 if (eflags & ATA_EFLAG_IS_IO) {
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001609 if (err_mask & AC_ERR_HSM)
Tejun Heo76326ac2007-11-27 19:28:59 +09001610 return base + ATA_ECAT_TOUT_HSM;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001611 if ((err_mask &
1612 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
Tejun Heo76326ac2007-11-27 19:28:59 +09001613 return base + ATA_ECAT_UNK_DEV;
Tejun Heo022bdb02006-05-15 20:58:22 +09001614 }
1615
1616 return 0;
1617}
1618
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001619struct speed_down_verdict_arg {
Tejun Heo022bdb02006-05-15 20:58:22 +09001620 u64 since;
Tejun Heo76326ac2007-11-27 19:28:59 +09001621 int xfer_ok;
Tejun Heo3884f7b2007-11-27 19:28:56 +09001622 int nr_errors[ATA_ECAT_NR];
Tejun Heo022bdb02006-05-15 20:58:22 +09001623};
1624
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001625static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
Tejun Heo022bdb02006-05-15 20:58:22 +09001626{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001627 struct speed_down_verdict_arg *arg = void_arg;
Tejun Heo76326ac2007-11-27 19:28:59 +09001628 int cat;
Tejun Heo022bdb02006-05-15 20:58:22 +09001629
1630 if (ent->timestamp < arg->since)
1631 return -1;
1632
Tejun Heo76326ac2007-11-27 19:28:59 +09001633 cat = ata_eh_categorize_error(ent->eflags, ent->err_mask,
1634 &arg->xfer_ok);
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001635 arg->nr_errors[cat]++;
Tejun Heo76326ac2007-11-27 19:28:59 +09001636
Tejun Heo022bdb02006-05-15 20:58:22 +09001637 return 0;
1638}
1639
1640/**
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001641 * ata_eh_speed_down_verdict - Determine speed down verdict
Tejun Heo022bdb02006-05-15 20:58:22 +09001642 * @dev: Device of interest
1643 *
1644 * This function examines error ring of @dev and determines
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001645 * whether NCQ needs to be turned off, transfer speed should be
1646 * stepped down, or falling back to PIO is necessary.
Tejun Heo022bdb02006-05-15 20:58:22 +09001647 *
Tejun Heo3884f7b2007-11-27 19:28:56 +09001648 * ECAT_ATA_BUS : ATA_BUS error for any command
Tejun Heo022bdb02006-05-15 20:58:22 +09001649 *
Tejun Heo3884f7b2007-11-27 19:28:56 +09001650 * ECAT_TOUT_HSM : TIMEOUT for any command or HSM violation for
1651 * IO commands
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001652 *
Tejun Heo3884f7b2007-11-27 19:28:56 +09001653 * ECAT_UNK_DEV : Unknown DEV error for IO commands
Tejun Heo022bdb02006-05-15 20:58:22 +09001654 *
Tejun Heo76326ac2007-11-27 19:28:59 +09001655 * ECAT_DUBIOUS_* : Identical to above three but occurred while
1656 * data transfer hasn't been verified.
1657 *
Tejun Heo3884f7b2007-11-27 19:28:56 +09001658 * Verdicts are
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001659 *
Tejun Heo3884f7b2007-11-27 19:28:56 +09001660 * NCQ_OFF : Turn off NCQ.
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001661 *
Tejun Heo3884f7b2007-11-27 19:28:56 +09001662 * SPEED_DOWN : Speed down transfer speed but don't fall back
1663 * to PIO.
1664 *
1665 * FALLBACK_TO_PIO : Fall back to PIO.
1666 *
1667 * Even if multiple verdicts are returned, only one action is
Tejun Heo76326ac2007-11-27 19:28:59 +09001668 * taken per error. An action triggered by non-DUBIOUS errors
1669 * clears ering, while one triggered by DUBIOUS_* errors doesn't.
1670 * This is to expedite speed down decisions right after device is
1671 * initially configured.
Tejun Heo3884f7b2007-11-27 19:28:56 +09001672 *
Tejun Heo76326ac2007-11-27 19:28:59 +09001673 * The followings are speed down rules. #1 and #2 deal with
1674 * DUBIOUS errors.
1675 *
1676 * 1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors
1677 * occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO.
1678 *
1679 * 2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors
1680 * occurred during last 5 mins, NCQ_OFF.
1681 *
1682 * 3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors
Tejun Heo3884f7b2007-11-27 19:28:56 +09001683 * ocurred during last 5 mins, FALLBACK_TO_PIO
1684 *
Tejun Heo76326ac2007-11-27 19:28:59 +09001685 * 4. If more than 3 TOUT_HSM or UNK_DEV errors occurred
Tejun Heo3884f7b2007-11-27 19:28:56 +09001686 * during last 10 mins, NCQ_OFF.
1687 *
Tejun Heo76326ac2007-11-27 19:28:59 +09001688 * 5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6
Tejun Heo3884f7b2007-11-27 19:28:56 +09001689 * UNK_DEV errors occurred during last 10 mins, SPEED_DOWN.
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001690 *
Tejun Heo022bdb02006-05-15 20:58:22 +09001691 * LOCKING:
1692 * Inherited from caller.
1693 *
1694 * RETURNS:
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001695 * OR of ATA_EH_SPDN_* flags.
Tejun Heo022bdb02006-05-15 20:58:22 +09001696 */
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001697static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
Tejun Heo022bdb02006-05-15 20:58:22 +09001698{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001699 const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
1700 u64 j64 = get_jiffies_64();
1701 struct speed_down_verdict_arg arg;
1702 unsigned int verdict = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09001703
Tejun Heo3884f7b2007-11-27 19:28:56 +09001704 /* scan past 5 mins of error history */
1705 memset(&arg, 0, sizeof(arg));
1706 arg.since = j64 - min(j64, j5mins);
1707 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1708
Tejun Heo76326ac2007-11-27 19:28:59 +09001709 if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] +
1710 arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1)
1711 verdict |= ATA_EH_SPDN_SPEED_DOWN |
1712 ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS;
1713
1714 if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] +
1715 arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1)
1716 verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS;
1717
Tejun Heo3884f7b2007-11-27 19:28:56 +09001718 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1719 arg.nr_errors[ATA_ECAT_TOUT_HSM] +
Tejun Heo663f99b2007-11-27 19:28:57 +09001720 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
Tejun Heo3884f7b2007-11-27 19:28:56 +09001721 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
1722
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001723 /* scan past 10 mins of error history */
Tejun Heo022bdb02006-05-15 20:58:22 +09001724 memset(&arg, 0, sizeof(arg));
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001725 arg.since = j64 - min(j64, j10mins);
1726 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
Tejun Heo022bdb02006-05-15 20:58:22 +09001727
Tejun Heo3884f7b2007-11-27 19:28:56 +09001728 if (arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1729 arg.nr_errors[ATA_ECAT_UNK_DEV] > 3)
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001730 verdict |= ATA_EH_SPDN_NCQ_OFF;
Tejun Heo3884f7b2007-11-27 19:28:56 +09001731
1732 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1733 arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 ||
Tejun Heo663f99b2007-11-27 19:28:57 +09001734 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001735 verdict |= ATA_EH_SPDN_SPEED_DOWN;
Tejun Heo022bdb02006-05-15 20:58:22 +09001736
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001737 return verdict;
Tejun Heo022bdb02006-05-15 20:58:22 +09001738}
1739
1740/**
1741 * ata_eh_speed_down - record error and speed down if necessary
1742 * @dev: Failed device
Tejun Heo3884f7b2007-11-27 19:28:56 +09001743 * @eflags: mask of ATA_EFLAG_* flags
Tejun Heo022bdb02006-05-15 20:58:22 +09001744 * @err_mask: err_mask of the error
1745 *
1746 * Record error and examine error history to determine whether
1747 * adjusting transmission speed is necessary. It also sets
1748 * transmission limits appropriately if such adjustment is
1749 * necessary.
1750 *
1751 * LOCKING:
1752 * Kernel thread context (may sleep).
1753 *
1754 * RETURNS:
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001755 * Determined recovery action.
Tejun Heo022bdb02006-05-15 20:58:22 +09001756 */
Tejun Heo3884f7b2007-11-27 19:28:56 +09001757static unsigned int ata_eh_speed_down(struct ata_device *dev,
1758 unsigned int eflags, unsigned int err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001759{
Tejun Heo3884f7b2007-11-27 19:28:56 +09001760 struct ata_link *link = dev->link;
Tejun Heo76326ac2007-11-27 19:28:59 +09001761 int xfer_ok = 0;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001762 unsigned int verdict;
1763 unsigned int action = 0;
1764
1765 /* don't bother if Cat-0 error */
Tejun Heo76326ac2007-11-27 19:28:59 +09001766 if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0)
Tejun Heo022bdb02006-05-15 20:58:22 +09001767 return 0;
1768
1769 /* record error and determine whether speed down is necessary */
Tejun Heo3884f7b2007-11-27 19:28:56 +09001770 ata_ering_record(&dev->ering, eflags, err_mask);
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001771 verdict = ata_eh_speed_down_verdict(dev);
Tejun Heo022bdb02006-05-15 20:58:22 +09001772
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001773 /* turn off NCQ? */
1774 if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
1775 (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
1776 ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
1777 dev->flags |= ATA_DFLAG_NCQ_OFF;
1778 ata_dev_printk(dev, KERN_WARNING,
1779 "NCQ disabled due to excessive errors\n");
1780 goto done;
1781 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001782
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001783 /* speed down? */
1784 if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1785 /* speed down SATA link speed if possible */
Tejun Heo3884f7b2007-11-27 19:28:56 +09001786 if (sata_down_spd_limit(link) == 0) {
Tejun Heocf480622008-01-24 00:05:14 +09001787 action |= ATA_EH_RESET;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001788 goto done;
1789 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001790
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001791 /* lower transfer mode */
1792 if (dev->spdn_cnt < 2) {
1793 static const int dma_dnxfer_sel[] =
1794 { ATA_DNXFER_DMA, ATA_DNXFER_40C };
1795 static const int pio_dnxfer_sel[] =
1796 { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
1797 int sel;
Tejun Heo022bdb02006-05-15 20:58:22 +09001798
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001799 if (dev->xfer_shift != ATA_SHIFT_PIO)
1800 sel = dma_dnxfer_sel[dev->spdn_cnt];
1801 else
1802 sel = pio_dnxfer_sel[dev->spdn_cnt];
1803
1804 dev->spdn_cnt++;
1805
1806 if (ata_down_xfermask_limit(dev, sel) == 0) {
Tejun Heocf480622008-01-24 00:05:14 +09001807 action |= ATA_EH_RESET;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001808 goto done;
1809 }
1810 }
1811 }
1812
1813 /* Fall back to PIO? Slowing down to PIO is meaningless for
Tejun Heo663f99b2007-11-27 19:28:57 +09001814 * SATA ATA devices. Consider it only for PATA and SATAPI.
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001815 */
1816 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
Tejun Heo663f99b2007-11-27 19:28:57 +09001817 (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) &&
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001818 (dev->xfer_shift != ATA_SHIFT_PIO)) {
1819 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
1820 dev->spdn_cnt = 0;
Tejun Heocf480622008-01-24 00:05:14 +09001821 action |= ATA_EH_RESET;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001822 goto done;
1823 }
1824 }
1825
Tejun Heo022bdb02006-05-15 20:58:22 +09001826 return 0;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001827 done:
1828 /* device has been slowed down, blow error history */
Tejun Heo76326ac2007-11-27 19:28:59 +09001829 if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS))
1830 ata_ering_clear(&dev->ering);
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001831 return action;
Tejun Heo022bdb02006-05-15 20:58:22 +09001832}
1833
1834/**
Tejun Heo9b1e2652007-08-06 18:36:24 +09001835 * ata_eh_link_autopsy - analyze error and determine recovery action
1836 * @link: host link to perform autopsy on
Tejun Heo022bdb02006-05-15 20:58:22 +09001837 *
Tejun Heo02607312007-08-06 18:36:23 +09001838 * Analyze why @link failed and determine which recovery actions
1839 * are needed. This function also sets more detailed AC_ERR_*
1840 * values and fills sense data for ATAPI CHECK SENSE.
Tejun Heo022bdb02006-05-15 20:58:22 +09001841 *
1842 * LOCKING:
1843 * Kernel thread context (may sleep).
1844 */
Tejun Heo9b1e2652007-08-06 18:36:24 +09001845static void ata_eh_link_autopsy(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001846{
Tejun Heo02607312007-08-06 18:36:23 +09001847 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09001848 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heodfcc1732007-10-31 10:17:05 +09001849 struct ata_device *dev;
Tejun Heo3884f7b2007-11-27 19:28:56 +09001850 unsigned int all_err_mask = 0, eflags = 0;
1851 int tag;
Tejun Heo022bdb02006-05-15 20:58:22 +09001852 u32 serror;
1853 int rc;
1854
1855 DPRINTK("ENTER\n");
1856
Tejun Heo1cdaf532006-07-03 16:07:26 +09001857 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1858 return;
1859
Tejun Heo022bdb02006-05-15 20:58:22 +09001860 /* obtain and analyze SError */
Tejun Heo936fd732007-08-06 18:36:23 +09001861 rc = sata_scr_read(link, SCR_ERROR, &serror);
Tejun Heo022bdb02006-05-15 20:58:22 +09001862 if (rc == 0) {
1863 ehc->i.serror |= serror;
Tejun Heo02607312007-08-06 18:36:23 +09001864 ata_eh_analyze_serror(link);
Tejun Heo4e57c512007-07-16 14:29:41 +09001865 } else if (rc != -EOPNOTSUPP) {
Tejun Heocf480622008-01-24 00:05:14 +09001866 /* SError read failed, force reset and probing */
Tejun Heob558edd2008-01-24 00:05:14 +09001867 ehc->i.probe_mask |= ATA_ALL_DEVICES;
Tejun Heocf480622008-01-24 00:05:14 +09001868 ehc->i.action |= ATA_EH_RESET;
Tejun Heo4e57c512007-07-16 14:29:41 +09001869 ehc->i.err_mask |= AC_ERR_OTHER;
1870 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001871
Tejun Heoe8ee8452006-05-15 21:03:46 +09001872 /* analyze NCQ failure */
Tejun Heo02607312007-08-06 18:36:23 +09001873 ata_eh_analyze_ncq_error(link);
Tejun Heoe8ee8452006-05-15 21:03:46 +09001874
Tejun Heo022bdb02006-05-15 20:58:22 +09001875 /* any real error trumps AC_ERR_OTHER */
1876 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1877 ehc->i.err_mask &= ~AC_ERR_OTHER;
1878
1879 all_err_mask |= ehc->i.err_mask;
1880
1881 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1882 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1883
Tejun Heo02607312007-08-06 18:36:23 +09001884 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != 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
1971 /* Autopsy of fanout ports can affect host link autopsy.
1972 * Perform host link autopsy last.
1973 */
Tejun Heo071f44b2008-04-07 22:47:22 +09001974 if (sata_pmp_attached(ap))
Tejun Heo2695e362008-01-10 13:41:23 +09001975 ata_eh_link_autopsy(&ap->link);
Tejun Heo9b1e2652007-08-06 18:36:24 +09001976}
1977
1978/**
1979 * ata_eh_link_report - report error handling to user
Tejun Heo02607312007-08-06 18:36:23 +09001980 * @link: ATA link EH is going on
Tejun Heo022bdb02006-05-15 20:58:22 +09001981 *
1982 * Report EH to user.
1983 *
1984 * LOCKING:
1985 * None.
1986 */
Tejun Heo9b1e2652007-08-06 18:36:24 +09001987static void ata_eh_link_report(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001988{
Tejun Heo02607312007-08-06 18:36:23 +09001989 struct ata_port *ap = link->ap;
1990 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001991 const char *frozen, *desc;
Tejun Heoa1e10f72007-08-18 13:28:49 +09001992 char tries_buf[6];
Tejun Heo022bdb02006-05-15 20:58:22 +09001993 int tag, nr_failed = 0;
1994
Tejun Heo94ff3d52007-10-09 14:57:56 +09001995 if (ehc->i.flags & ATA_EHI_QUIET)
1996 return;
1997
Tejun Heo022bdb02006-05-15 20:58:22 +09001998 desc = NULL;
1999 if (ehc->i.desc[0] != '\0')
2000 desc = ehc->i.desc;
2001
2002 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2003 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2004
Tejun Heoe027bd32007-10-26 16:19:26 +09002005 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link ||
2006 ((qc->flags & ATA_QCFLAG_QUIET) &&
2007 qc->err_mask == AC_ERR_DEV))
Tejun Heo022bdb02006-05-15 20:58:22 +09002008 continue;
2009 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
2010 continue;
2011
2012 nr_failed++;
2013 }
2014
2015 if (!nr_failed && !ehc->i.err_mask)
2016 return;
2017
2018 frozen = "";
Tejun Heob51e9e52006-06-29 01:29:30 +09002019 if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heo022bdb02006-05-15 20:58:22 +09002020 frozen = " frozen";
2021
Tejun Heoa1e10f72007-08-18 13:28:49 +09002022 memset(tries_buf, 0, sizeof(tries_buf));
2023 if (ap->eh_tries < ATA_EH_MAX_TRIES)
2024 snprintf(tries_buf, sizeof(tries_buf) - 1, " t%d",
2025 ap->eh_tries);
2026
Tejun Heo022bdb02006-05-15 20:58:22 +09002027 if (ehc->i.dev) {
Tejun Heoe8ee8452006-05-15 21:03:46 +09002028 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
Tejun Heoa1e10f72007-08-18 13:28:49 +09002029 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2030 ehc->i.err_mask, link->sactive, ehc->i.serror,
2031 ehc->i.action, frozen, tries_buf);
Tejun Heo022bdb02006-05-15 20:58:22 +09002032 if (desc)
Tejun Heob64bbc32007-07-16 14:29:39 +09002033 ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
Tejun Heo022bdb02006-05-15 20:58:22 +09002034 } else {
Tejun Heo02607312007-08-06 18:36:23 +09002035 ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
Tejun Heoa1e10f72007-08-18 13:28:49 +09002036 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2037 ehc->i.err_mask, link->sactive, ehc->i.serror,
2038 ehc->i.action, frozen, tries_buf);
Tejun Heo022bdb02006-05-15 20:58:22 +09002039 if (desc)
Tejun Heo02607312007-08-06 18:36:23 +09002040 ata_link_printk(link, KERN_ERR, "%s\n", desc);
Tejun Heo022bdb02006-05-15 20:58:22 +09002041 }
2042
Robert Hancock1333e192007-10-02 11:22:02 -04002043 if (ehc->i.serror)
Tejun Heoda0e21d2008-07-31 16:08:38 +09002044 ata_link_printk(link, KERN_ERR,
Robert Hancock1333e192007-10-02 11:22:02 -04002045 "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
2046 ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
2047 ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
2048 ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
2049 ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
2050 ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
2051 ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
2052 ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
2053 ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
2054 ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
2055 ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
2056 ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
2057 ehc->i.serror & SERR_CRC ? "BadCRC " : "",
2058 ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
2059 ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
2060 ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
2061 ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002062 ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
Robert Hancock1333e192007-10-02 11:22:02 -04002063
Tejun Heo022bdb02006-05-15 20:58:22 +09002064 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2065 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
Tejun Heo8a937582006-11-14 22:36:12 +09002066 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
Tejun Heoabb6a882007-11-28 23:16:09 +09002067 const u8 *cdb = qc->cdb;
2068 char data_buf[20] = "";
2069 char cdb_buf[70] = "";
Tejun Heo022bdb02006-05-15 20:58:22 +09002070
Tejun Heo02607312007-08-06 18:36:23 +09002071 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
2072 qc->dev->link != link || !qc->err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09002073 continue;
2074
Tejun Heoabb6a882007-11-28 23:16:09 +09002075 if (qc->dma_dir != DMA_NONE) {
2076 static const char *dma_str[] = {
2077 [DMA_BIDIRECTIONAL] = "bidi",
2078 [DMA_TO_DEVICE] = "out",
2079 [DMA_FROM_DEVICE] = "in",
2080 };
2081 static const char *prot_str[] = {
2082 [ATA_PROT_PIO] = "pio",
2083 [ATA_PROT_DMA] = "dma",
2084 [ATA_PROT_NCQ] = "ncq",
Tejun Heo0dc36882007-12-18 16:34:43 -05002085 [ATAPI_PROT_PIO] = "pio",
2086 [ATAPI_PROT_DMA] = "dma",
Tejun Heoabb6a882007-11-28 23:16:09 +09002087 };
2088
2089 snprintf(data_buf, sizeof(data_buf), " %s %u %s",
2090 prot_str[qc->tf.protocol], qc->nbytes,
2091 dma_str[qc->dma_dir]);
2092 }
2093
Jeff Garzike39eec12007-12-01 18:05:39 -05002094 if (ata_is_atapi(qc->tf.protocol))
Tejun Heoabb6a882007-11-28 23:16:09 +09002095 snprintf(cdb_buf, sizeof(cdb_buf),
2096 "cdb %02x %02x %02x %02x %02x %02x %02x %02x "
2097 "%02x %02x %02x %02x %02x %02x %02x %02x\n ",
2098 cdb[0], cdb[1], cdb[2], cdb[3],
2099 cdb[4], cdb[5], cdb[6], cdb[7],
2100 cdb[8], cdb[9], cdb[10], cdb[11],
2101 cdb[12], cdb[13], cdb[14], cdb[15]);
2102
Tejun Heo8a937582006-11-14 22:36:12 +09002103 ata_dev_printk(qc->dev, KERN_ERR,
2104 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
Tejun Heoabb6a882007-11-28 23:16:09 +09002105 "tag %d%s\n %s"
Tejun Heo8a937582006-11-14 22:36:12 +09002106 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
Tejun Heo5335b722007-07-16 14:29:40 +09002107 "Emask 0x%x (%s)%s\n",
Tejun Heo8a937582006-11-14 22:36:12 +09002108 cmd->command, cmd->feature, cmd->nsect,
2109 cmd->lbal, cmd->lbam, cmd->lbah,
2110 cmd->hob_feature, cmd->hob_nsect,
2111 cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
Tejun Heoabb6a882007-11-28 23:16:09 +09002112 cmd->device, qc->tag, data_buf, cdb_buf,
Tejun Heo8a937582006-11-14 22:36:12 +09002113 res->command, res->feature, res->nsect,
2114 res->lbal, res->lbam, res->lbah,
2115 res->hob_feature, res->hob_nsect,
2116 res->hob_lbal, res->hob_lbam, res->hob_lbah,
Tejun Heo5335b722007-07-16 14:29:40 +09002117 res->device, qc->err_mask, ata_err_string(qc->err_mask),
2118 qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
Robert Hancock1333e192007-10-02 11:22:02 -04002119
2120 if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002121 ATA_ERR)) {
Robert Hancock1333e192007-10-02 11:22:02 -04002122 if (res->command & ATA_BUSY)
2123 ata_dev_printk(qc->dev, KERN_ERR,
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002124 "status: { Busy }\n");
Robert Hancock1333e192007-10-02 11:22:02 -04002125 else
2126 ata_dev_printk(qc->dev, KERN_ERR,
2127 "status: { %s%s%s%s}\n",
2128 res->command & ATA_DRDY ? "DRDY " : "",
2129 res->command & ATA_DF ? "DF " : "",
2130 res->command & ATA_DRQ ? "DRQ " : "",
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002131 res->command & ATA_ERR ? "ERR " : "");
Robert Hancock1333e192007-10-02 11:22:02 -04002132 }
2133
2134 if (cmd->command != ATA_CMD_PACKET &&
2135 (res->feature & (ATA_ICRC | ATA_UNC | ATA_IDNF |
2136 ATA_ABORTED)))
2137 ata_dev_printk(qc->dev, KERN_ERR,
2138 "error: { %s%s%s%s}\n",
2139 res->feature & ATA_ICRC ? "ICRC " : "",
2140 res->feature & ATA_UNC ? "UNC " : "",
2141 res->feature & ATA_IDNF ? "IDNF " : "",
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002142 res->feature & ATA_ABORTED ? "ABRT " : "");
Tejun Heo022bdb02006-05-15 20:58:22 +09002143 }
2144}
2145
Tejun Heo9b1e2652007-08-06 18:36:24 +09002146/**
2147 * ata_eh_report - report error handling to user
2148 * @ap: ATA port to report EH about
2149 *
2150 * Report EH to user.
2151 *
2152 * LOCKING:
2153 * None.
2154 */
Tejun Heofb7fd612007-09-23 13:14:12 +09002155void ata_eh_report(struct ata_port *ap)
Tejun Heo9b1e2652007-08-06 18:36:24 +09002156{
2157 struct ata_link *link;
2158
2159 __ata_port_for_each_link(link, ap)
2160 ata_eh_link_report(link);
2161}
2162
Tejun Heocc0680a2007-08-06 18:36:23 +09002163static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
Tejun Heod4b2bab2007-02-02 16:50:52 +09002164 unsigned int *classes, unsigned long deadline)
Tejun Heod87fa382006-05-31 18:28:24 +09002165{
Tejun Heof58229f2007-08-06 18:36:23 +09002166 struct ata_device *dev;
Tejun Heod87fa382006-05-31 18:28:24 +09002167
Tejun Heocc0680a2007-08-06 18:36:23 +09002168 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09002169 classes[dev->devno] = ATA_DEV_UNKNOWN;
Tejun Heod87fa382006-05-31 18:28:24 +09002170
Tejun Heof0465192008-05-19 01:15:08 +09002171 return reset(link, classes, deadline);
Tejun Heod87fa382006-05-31 18:28:24 +09002172}
2173
Tejun Heoae791c02007-09-23 13:14:12 +09002174static int ata_eh_followup_srst_needed(struct ata_link *link,
Tejun Heo5dbfc9c2008-07-31 16:08:02 +09002175 int rc, const unsigned int *classes)
Tejun Heo664faf02006-05-31 18:27:50 +09002176{
Tejun Heo45db2f62008-04-08 01:46:56 +09002177 if ((link->flags & ATA_LFLAG_NO_SRST) || ata_link_offline(link))
Tejun Heoae791c02007-09-23 13:14:12 +09002178 return 0;
Tejun Heo5dbfc9c2008-07-31 16:08:02 +09002179 if (rc == -EAGAIN)
2180 return 1;
Tejun Heo071f44b2008-04-07 22:47:22 +09002181 if (sata_pmp_supported(link->ap) && ata_is_host_link(link))
Tejun Heo3495de72007-09-23 13:19:53 +09002182 return 1;
Tejun Heo664faf02006-05-31 18:27:50 +09002183 return 0;
2184}
2185
Tejun Heofb7fd612007-09-23 13:14:12 +09002186int ata_eh_reset(struct ata_link *link, int classify,
2187 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
2188 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
Tejun Heo022bdb02006-05-15 20:58:22 +09002189{
Tejun Heoafaa5c32007-10-09 15:06:10 +09002190 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09002191 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo664faf02006-05-31 18:27:50 +09002192 unsigned int *classes = ehc->classes;
Tejun Heo416dc9e2007-10-31 10:17:03 +09002193 unsigned int lflags = link->flags;
Tejun Heo1cdaf532006-07-03 16:07:26 +09002194 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
Tejun Heod8af0eb2008-05-20 02:17:53 +09002195 int max_tries = 0, try = 0;
Tejun Heof58229f2007-08-06 18:36:23 +09002196 struct ata_device *dev;
Tejun Heo416dc9e2007-10-31 10:17:03 +09002197 unsigned long deadline, now;
Tejun Heo022bdb02006-05-15 20:58:22 +09002198 ata_reset_fn_t reset;
Tejun Heoafaa5c32007-10-09 15:06:10 +09002199 unsigned long flags;
Tejun Heo416dc9e2007-10-31 10:17:03 +09002200 u32 sstatus;
Tejun Heof0465192008-05-19 01:15:08 +09002201 int nr_known, rc;
Tejun Heo022bdb02006-05-15 20:58:22 +09002202
Tejun Heo932648b2008-05-19 01:15:06 +09002203 /*
2204 * Prepare to reset
2205 */
Tejun Heod8af0eb2008-05-20 02:17:53 +09002206 while (ata_eh_reset_timeouts[max_tries] != ULONG_MAX)
2207 max_tries++;
Tejun Heo05944bd2008-08-13 20:19:09 +09002208 if (link->flags & ATA_LFLAG_NO_HRST)
2209 hardreset = NULL;
2210 if (link->flags & ATA_LFLAG_NO_SRST)
2211 softreset = NULL;
Tejun Heod8af0eb2008-05-20 02:17:53 +09002212
Tejun Heo0a2c0f52008-05-20 02:17:52 +09002213 now = jiffies;
2214 deadline = ata_deadline(ehc->last_reset, ATA_EH_RESET_COOL_DOWN);
2215 if (time_before(now, deadline))
2216 schedule_timeout_uninterruptible(deadline - now);
2217
Tejun Heoafaa5c32007-10-09 15:06:10 +09002218 spin_lock_irqsave(ap->lock, flags);
2219 ap->pflags |= ATA_PFLAG_RESETTING;
2220 spin_unlock_irqrestore(ap->lock, flags);
2221
Tejun Heocf480622008-01-24 00:05:14 +09002222 ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
Tejun Heo0a2c0f52008-05-20 02:17:52 +09002223 ehc->last_reset = jiffies;
Tejun Heo13abf502006-07-10 23:18:46 +09002224
Tejun Heocdeab112007-10-29 16:41:09 +09002225 ata_link_for_each_dev(dev, link) {
2226 /* If we issue an SRST then an ATA drive (not ATAPI)
2227 * may change configuration and be in PIO0 timing. If
2228 * we do a hard reset (or are coming from power on)
2229 * this is true for ATA or ATAPI. Until we've set a
2230 * suitable controller mode we should not touch the
2231 * bus as we may be talking too fast.
2232 */
2233 dev->pio_mode = XFER_PIO_0;
2234
2235 /* If the controller has a pio mode setup function
2236 * then use it to set the chipset to rights. Don't
2237 * touch the DMA setup as that will be dealt with when
2238 * configuring devices.
2239 */
2240 if (ap->ops->set_piomode)
2241 ap->ops->set_piomode(ap, dev);
2242 }
2243
Tejun Heocf480622008-01-24 00:05:14 +09002244 /* prefer hardreset */
Tejun Heo932648b2008-05-19 01:15:06 +09002245 reset = NULL;
Tejun Heocf480622008-01-24 00:05:14 +09002246 ehc->i.action &= ~ATA_EH_RESET;
2247 if (hardreset) {
2248 reset = hardreset;
Tejun Heoa6740502008-07-31 16:07:04 +09002249 ehc->i.action |= ATA_EH_HARDRESET;
Tejun Heo4f7faa32008-01-30 18:18:26 +09002250 } else if (softreset) {
Tejun Heocf480622008-01-24 00:05:14 +09002251 reset = softreset;
Tejun Heoa6740502008-07-31 16:07:04 +09002252 ehc->i.action |= ATA_EH_SOFTRESET;
Tejun Heocf480622008-01-24 00:05:14 +09002253 }
Tejun Heof5914a42006-05-31 18:27:48 +09002254
2255 if (prereset) {
Tejun Heo341c2c92008-05-20 02:17:51 +09002256 rc = prereset(link,
2257 ata_deadline(jiffies, ATA_EH_PRERESET_TIMEOUT));
Tejun Heof5914a42006-05-31 18:27:48 +09002258 if (rc) {
Alan Coxc9619222006-09-26 17:53:38 +01002259 if (rc == -ENOENT) {
Tejun Heocc0680a2007-08-06 18:36:23 +09002260 ata_link_printk(link, KERN_DEBUG,
Tejun Heo4aa9ab62007-03-12 17:24:08 +09002261 "port disabled. ignoring.\n");
Tejun Heocf480622008-01-24 00:05:14 +09002262 ehc->i.action &= ~ATA_EH_RESET;
Tejun Heo4aa9ab62007-03-12 17:24:08 +09002263
Tejun Heo936fd732007-08-06 18:36:23 +09002264 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09002265 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heo4aa9ab62007-03-12 17:24:08 +09002266
2267 rc = 0;
Alan Coxc9619222006-09-26 17:53:38 +01002268 } else
Tejun Heocc0680a2007-08-06 18:36:23 +09002269 ata_link_printk(link, KERN_ERR,
Tejun Heof5914a42006-05-31 18:27:48 +09002270 "prereset failed (errno=%d)\n", rc);
Tejun Heofccb6ea2007-07-16 14:29:41 +09002271 goto out;
Tejun Heof5914a42006-05-31 18:27:48 +09002272 }
Tejun Heof5914a42006-05-31 18:27:48 +09002273
Tejun Heo932648b2008-05-19 01:15:06 +09002274 /* prereset() might have cleared ATA_EH_RESET. If so,
2275 * bang classes and return.
2276 */
2277 if (reset && !(ehc->i.action & ATA_EH_RESET)) {
2278 ata_link_for_each_dev(dev, link)
2279 classes[dev->devno] = ATA_DEV_NONE;
2280 rc = 0;
2281 goto out;
2282 }
Tejun Heof5914a42006-05-31 18:27:48 +09002283 }
2284
Tejun Heo022bdb02006-05-15 20:58:22 +09002285 retry:
Tejun Heo932648b2008-05-19 01:15:06 +09002286 /*
2287 * Perform reset
2288 */
Tejun Heo0a2c0f52008-05-20 02:17:52 +09002289 ehc->last_reset = jiffies;
Tejun Heodc98c322008-05-19 01:15:07 +09002290 if (ata_is_host_link(link))
2291 ata_eh_freeze_port(ap);
2292
Tejun Heo341c2c92008-05-20 02:17:51 +09002293 deadline = ata_deadline(jiffies, ata_eh_reset_timeouts[try++]);
Tejun Heo31daabd2007-02-02 16:50:52 +09002294
Tejun Heo932648b2008-05-19 01:15:06 +09002295 if (reset) {
2296 if (verbose)
2297 ata_link_printk(link, KERN_INFO, "%s resetting link\n",
2298 reset == softreset ? "soft" : "hard");
Tejun Heo022bdb02006-05-15 20:58:22 +09002299
Tejun Heo932648b2008-05-19 01:15:06 +09002300 /* mark that this EH session started with reset */
2301 if (reset == hardreset)
2302 ehc->i.flags |= ATA_EHI_DID_HARDRESET;
2303 else
2304 ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09002305
Tejun Heo932648b2008-05-19 01:15:06 +09002306 rc = ata_do_reset(link, reset, classes, deadline);
Tejun Heo5dbfc9c2008-07-31 16:08:02 +09002307 if (rc && rc != -EAGAIN)
2308 goto fail;
Tejun Heo022bdb02006-05-15 20:58:22 +09002309
Tejun Heo932648b2008-05-19 01:15:06 +09002310 if (reset == hardreset &&
Tejun Heo5dbfc9c2008-07-31 16:08:02 +09002311 ata_eh_followup_srst_needed(link, rc, classes)) {
Tejun Heo932648b2008-05-19 01:15:06 +09002312 /* okay, let's do follow-up softreset */
2313 reset = softreset;
Tejun Heo664faf02006-05-31 18:27:50 +09002314
Tejun Heo932648b2008-05-19 01:15:06 +09002315 if (!reset) {
2316 ata_link_printk(link, KERN_ERR,
2317 "follow-up softreset required "
2318 "but no softreset avaliable\n");
2319 rc = -EINVAL;
2320 goto fail;
2321 }
2322
2323 ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
2324 rc = ata_do_reset(link, reset, classes, deadline);
Tejun Heo664faf02006-05-31 18:27:50 +09002325 }
Tejun Heo932648b2008-05-19 01:15:06 +09002326 } else {
2327 if (verbose)
2328 ata_link_printk(link, KERN_INFO, "no reset method "
2329 "available, skipping reset\n");
2330 if (!(lflags & ATA_LFLAG_ASSUME_CLASS))
2331 lflags |= ATA_LFLAG_ASSUME_ATA;
Tejun Heo664faf02006-05-31 18:27:50 +09002332 }
2333
Tejun Heo932648b2008-05-19 01:15:06 +09002334 /*
2335 * Post-reset processing
2336 */
Tejun Heo416dc9e2007-10-31 10:17:03 +09002337 ata_link_for_each_dev(dev, link) {
2338 /* After the reset, the device state is PIO 0 and the
2339 * controller state is undefined. Reset also wakes up
2340 * drives from sleeping mode.
2341 */
2342 dev->pio_mode = XFER_PIO_0;
2343 dev->flags &= ~ATA_DFLAG_SLEEPING;
Tejun Heo664faf02006-05-31 18:27:50 +09002344
Tejun Heo416dc9e2007-10-31 10:17:03 +09002345 if (ata_link_offline(link))
2346 continue;
Tejun Heo664faf02006-05-31 18:27:50 +09002347
Tejun Heo4ccd3322008-01-08 20:26:12 +09002348 /* apply class override */
Tejun Heo416dc9e2007-10-31 10:17:03 +09002349 if (lflags & ATA_LFLAG_ASSUME_ATA)
2350 classes[dev->devno] = ATA_DEV_ATA;
2351 else if (lflags & ATA_LFLAG_ASSUME_SEMB)
2352 classes[dev->devno] = ATA_DEV_SEMB_UNSUP; /* not yet */
Tejun Heo022bdb02006-05-15 20:58:22 +09002353 }
2354
Tejun Heo416dc9e2007-10-31 10:17:03 +09002355 /* record current link speed */
2356 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2357 link->sata_spd = (sstatus >> 4) & 0xf;
Tejun Heo008a7892007-07-16 14:29:40 +09002358
Tejun Heodc98c322008-05-19 01:15:07 +09002359 /* thaw the port */
2360 if (ata_is_host_link(link))
2361 ata_eh_thaw_port(ap);
2362
Tejun Heof0465192008-05-19 01:15:08 +09002363 /* postreset() should clear hardware SError. Although SError
2364 * is cleared during link resume, clearing SError here is
2365 * necessary as some PHYs raise hotplug events after SRST.
2366 * This introduces race condition where hotplug occurs between
2367 * reset and here. This race is mediated by cross checking
2368 * link onlineness and classification result later.
2369 */
Tejun Heo416dc9e2007-10-31 10:17:03 +09002370 if (postreset)
2371 postreset(link, classes);
Tejun Heo20952b62006-05-31 18:27:23 +09002372
Tejun Heof0465192008-05-19 01:15:08 +09002373 /* clear cached SError */
2374 spin_lock_irqsave(link->ap->lock, flags);
2375 link->eh_info.serror = 0;
2376 spin_unlock_irqrestore(link->ap->lock, flags);
2377
2378 /* Make sure onlineness and classification result correspond.
2379 * Hotplug could have happened during reset and some
2380 * controllers fail to wait while a drive is spinning up after
2381 * being hotplugged causing misdetection. By cross checking
2382 * link onlineness and classification result, those conditions
2383 * can be reliably detected and retried.
2384 */
2385 nr_known = 0;
2386 ata_link_for_each_dev(dev, link) {
2387 /* convert all ATA_DEV_UNKNOWN to ATA_DEV_NONE */
2388 if (classes[dev->devno] == ATA_DEV_UNKNOWN)
2389 classes[dev->devno] = ATA_DEV_NONE;
2390 else
2391 nr_known++;
2392 }
2393
2394 if (classify && !nr_known && ata_link_online(link)) {
2395 if (try < max_tries) {
2396 ata_link_printk(link, KERN_WARNING, "link online but "
2397 "device misclassified, retrying\n");
2398 rc = -EAGAIN;
2399 goto fail;
2400 }
2401 ata_link_printk(link, KERN_WARNING,
2402 "link online but device misclassified, "
2403 "device detection might fail\n");
2404 }
2405
Tejun Heo416dc9e2007-10-31 10:17:03 +09002406 /* reset successful, schedule revalidation */
Tejun Heocf480622008-01-24 00:05:14 +09002407 ata_eh_done(link, NULL, ATA_EH_RESET);
Tejun Heo0a2c0f52008-05-20 02:17:52 +09002408 ehc->last_reset = jiffies;
Tejun Heo416dc9e2007-10-31 10:17:03 +09002409 ehc->i.action |= ATA_EH_REVALIDATE;
Tejun Heoae791c02007-09-23 13:14:12 +09002410
Tejun Heo416dc9e2007-10-31 10:17:03 +09002411 rc = 0;
Tejun Heofccb6ea2007-07-16 14:29:41 +09002412 out:
2413 /* clear hotplug flag */
2414 ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
Tejun Heoafaa5c32007-10-09 15:06:10 +09002415
2416 spin_lock_irqsave(ap->lock, flags);
2417 ap->pflags &= ~ATA_PFLAG_RESETTING;
2418 spin_unlock_irqrestore(ap->lock, flags);
2419
Tejun Heo022bdb02006-05-15 20:58:22 +09002420 return rc;
Tejun Heo416dc9e2007-10-31 10:17:03 +09002421
2422 fail:
Tejun Heo5958e302008-04-07 22:47:20 +09002423 /* if SCR isn't accessible on a fan-out port, PMP needs to be reset */
2424 if (!ata_is_host_link(link) &&
2425 sata_scr_read(link, SCR_STATUS, &sstatus))
2426 rc = -ERESTART;
2427
Tejun Heo416dc9e2007-10-31 10:17:03 +09002428 if (rc == -ERESTART || try >= max_tries)
2429 goto out;
2430
2431 now = jiffies;
2432 if (time_before(now, deadline)) {
2433 unsigned long delta = deadline - now;
2434
Tejun Heo0a2c0f52008-05-20 02:17:52 +09002435 ata_link_printk(link, KERN_WARNING,
2436 "reset failed (errno=%d), retrying in %u secs\n",
2437 rc, DIV_ROUND_UP(jiffies_to_msecs(delta), 1000));
Tejun Heo416dc9e2007-10-31 10:17:03 +09002438
2439 while (delta)
2440 delta = schedule_timeout_uninterruptible(delta);
2441 }
2442
2443 if (rc == -EPIPE || try == max_tries - 1)
2444 sata_down_spd_limit(link);
2445 if (hardreset)
2446 reset = hardreset;
2447 goto retry;
Tejun Heo022bdb02006-05-15 20:58:22 +09002448}
2449
Tejun Heo02607312007-08-06 18:36:23 +09002450static int ata_eh_revalidate_and_attach(struct ata_link *link,
Tejun Heo084fe632006-05-31 18:28:03 +09002451 struct ata_device **r_failed_dev)
Tejun Heo022bdb02006-05-15 20:58:22 +09002452{
Tejun Heo02607312007-08-06 18:36:23 +09002453 struct ata_port *ap = link->ap;
2454 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09002455 struct ata_device *dev;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002456 unsigned int new_mask = 0;
Tejun Heo084fe632006-05-31 18:28:03 +09002457 unsigned long flags;
Tejun Heof58229f2007-08-06 18:36:23 +09002458 int rc = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09002459
2460 DPRINTK("ENTER\n");
2461
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002462 /* For PATA drive side cable detection to work, IDENTIFY must
2463 * be done backwards such that PDIAG- is released by the slave
2464 * device before the master device is identified.
2465 */
Tejun Heo02607312007-08-06 18:36:23 +09002466 ata_link_for_each_dev_reverse(dev, link) {
Tejun Heof58229f2007-08-06 18:36:23 +09002467 unsigned int action = ata_eh_dev_action(dev);
2468 unsigned int readid_flags = 0;
Tejun Heo47005f22006-06-19 18:27:23 +09002469
Tejun Heobff04642006-11-10 18:08:10 +09002470 if (ehc->i.flags & ATA_EHI_DID_RESET)
2471 readid_flags |= ATA_READID_POSTRESET;
2472
Tejun Heo9666f402007-05-04 21:27:47 +02002473 if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
Tejun Heo633273a2007-09-23 13:19:54 +09002474 WARN_ON(dev->class == ATA_DEV_PMP);
2475
Tejun Heo02607312007-08-06 18:36:23 +09002476 if (ata_link_offline(link)) {
Tejun Heo022bdb02006-05-15 20:58:22 +09002477 rc = -EIO;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002478 goto err;
Tejun Heo022bdb02006-05-15 20:58:22 +09002479 }
2480
Tejun Heo02607312007-08-06 18:36:23 +09002481 ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
Tejun Heo422c9da2007-09-23 13:14:12 +09002482 rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
2483 readid_flags);
Tejun Heo022bdb02006-05-15 20:58:22 +09002484 if (rc)
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002485 goto err;
Tejun Heo022bdb02006-05-15 20:58:22 +09002486
Tejun Heo02607312007-08-06 18:36:23 +09002487 ata_eh_done(link, dev, ATA_EH_REVALIDATE);
Tejun Heo47005f22006-06-19 18:27:23 +09002488
Tejun Heobaa1e782006-11-01 18:39:27 +09002489 /* Configuration may have changed, reconfigure
2490 * transfer mode.
2491 */
2492 ehc->i.flags |= ATA_EHI_SETMODE;
2493
zhao, forrest3057ac32006-06-12 12:01:34 +08002494 /* schedule the scsi_rescan_device() here */
2495 queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
Tejun Heo084fe632006-05-31 18:28:03 +09002496 } else if (dev->class == ATA_DEV_UNKNOWN &&
2497 ehc->tries[dev->devno] &&
2498 ata_class_enabled(ehc->classes[dev->devno])) {
2499 dev->class = ehc->classes[dev->devno];
2500
Tejun Heo633273a2007-09-23 13:19:54 +09002501 if (dev->class == ATA_DEV_PMP)
2502 rc = sata_pmp_attach(dev);
2503 else
2504 rc = ata_dev_read_id(dev, &dev->class,
2505 readid_flags, dev->id);
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002506 switch (rc) {
2507 case 0:
Tejun Heof58229f2007-08-06 18:36:23 +09002508 new_mask |= 1 << dev->devno;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002509 break;
2510 case -ENOENT:
Tejun Heo55a8e2c2006-11-10 18:08:10 +09002511 /* IDENTIFY was issued to non-existent
2512 * device. No need to reset. Just
2513 * thaw and kill the device.
2514 */
2515 ata_eh_thaw_port(ap);
2516 dev->class = ATA_DEV_UNKNOWN;
Tejun Heo084fe632006-05-31 18:28:03 +09002517 break;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002518 default:
2519 dev->class = ATA_DEV_UNKNOWN;
2520 goto err;
Tejun Heo55a8e2c2006-11-10 18:08:10 +09002521 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002522 }
2523 }
2524
Tejun Heoc1c4e8d2007-04-23 02:05:53 +09002525 /* PDIAG- should have been released, ask cable type if post-reset */
Tejun Heo33267322008-02-13 09:15:09 +09002526 if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) {
2527 if (ap->ops->cable_detect)
2528 ap->cbl = ap->ops->cable_detect(ap);
2529 ata_force_cbl(ap);
2530 }
Tejun Heoc1c4e8d2007-04-23 02:05:53 +09002531
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002532 /* Configure new devices forward such that user doesn't see
2533 * device detection messages backwards.
2534 */
Tejun Heo02607312007-08-06 18:36:23 +09002535 ata_link_for_each_dev(dev, link) {
Tejun Heo633273a2007-09-23 13:19:54 +09002536 if (!(new_mask & (1 << dev->devno)) ||
2537 dev->class == ATA_DEV_PMP)
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002538 continue;
2539
2540 ehc->i.flags |= ATA_EHI_PRINTINFO;
2541 rc = ata_dev_configure(dev);
2542 ehc->i.flags &= ~ATA_EHI_PRINTINFO;
2543 if (rc)
2544 goto err;
2545
2546 spin_lock_irqsave(ap->lock, flags);
2547 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
2548 spin_unlock_irqrestore(ap->lock, flags);
2549
2550 /* new device discovered, configure xfermode */
2551 ehc->i.flags |= ATA_EHI_SETMODE;
2552 }
2553
2554 return 0;
2555
2556 err:
2557 *r_failed_dev = dev;
2558 DPRINTK("EXIT rc=%d\n", rc);
Tejun Heo022bdb02006-05-15 20:58:22 +09002559 return rc;
2560}
2561
Tejun Heo6f1d1e32007-11-27 19:28:55 +09002562/**
2563 * ata_set_mode - Program timings and issue SET FEATURES - XFER
2564 * @link: link on which timings will be programmed
2565 * @r_failed_dev: out paramter for failed device
2566 *
2567 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
2568 * ata_set_mode() fails, pointer to the failing device is
2569 * returned in @r_failed_dev.
2570 *
2571 * LOCKING:
2572 * PCI/etc. bus probe sem.
2573 *
2574 * RETURNS:
2575 * 0 on success, negative errno otherwise
2576 */
2577int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
2578{
2579 struct ata_port *ap = link->ap;
Tejun Heo00115e02007-11-27 19:28:58 +09002580 struct ata_device *dev;
2581 int rc;
Tejun Heo6f1d1e32007-11-27 19:28:55 +09002582
Tejun Heo76326ac2007-11-27 19:28:59 +09002583 /* if data transfer is verified, clear DUBIOUS_XFER on ering top */
2584 ata_link_for_each_dev(dev, link) {
2585 if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
2586 struct ata_ering_entry *ent;
2587
2588 ent = ata_ering_top(&dev->ering);
2589 if (ent)
2590 ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER;
2591 }
2592 }
2593
Tejun Heo6f1d1e32007-11-27 19:28:55 +09002594 /* has private set_mode? */
2595 if (ap->ops->set_mode)
Tejun Heo00115e02007-11-27 19:28:58 +09002596 rc = ap->ops->set_mode(link, r_failed_dev);
2597 else
2598 rc = ata_do_set_mode(link, r_failed_dev);
2599
2600 /* if transfer mode has changed, set DUBIOUS_XFER on device */
2601 ata_link_for_each_dev(dev, link) {
2602 struct ata_eh_context *ehc = &link->eh_context;
2603 u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
2604 u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
2605
2606 if (dev->xfer_mode != saved_xfer_mode ||
2607 ata_ncq_enabled(dev) != saved_ncq)
2608 dev->flags |= ATA_DFLAG_DUBIOUS_XFER;
2609 }
2610
2611 return rc;
Tejun Heo6f1d1e32007-11-27 19:28:55 +09002612}
2613
Tejun Heo02607312007-08-06 18:36:23 +09002614static int ata_link_nr_enabled(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09002615{
Tejun Heof58229f2007-08-06 18:36:23 +09002616 struct ata_device *dev;
2617 int cnt = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09002618
Tejun Heo02607312007-08-06 18:36:23 +09002619 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09002620 if (ata_dev_enabled(dev))
Tejun Heo022bdb02006-05-15 20:58:22 +09002621 cnt++;
2622 return cnt;
2623}
2624
Tejun Heo02607312007-08-06 18:36:23 +09002625static int ata_link_nr_vacant(struct ata_link *link)
Tejun Heo084fe632006-05-31 18:28:03 +09002626{
Tejun Heof58229f2007-08-06 18:36:23 +09002627 struct ata_device *dev;
2628 int cnt = 0;
Tejun Heo084fe632006-05-31 18:28:03 +09002629
Tejun Heo02607312007-08-06 18:36:23 +09002630 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09002631 if (dev->class == ATA_DEV_UNKNOWN)
Tejun Heo084fe632006-05-31 18:28:03 +09002632 cnt++;
2633 return cnt;
2634}
2635
Tejun Heo02607312007-08-06 18:36:23 +09002636static int ata_eh_skip_recovery(struct ata_link *link)
Tejun Heo084fe632006-05-31 18:28:03 +09002637{
Tejun Heo672b2d62008-01-24 00:05:14 +09002638 struct ata_port *ap = link->ap;
Tejun Heo02607312007-08-06 18:36:23 +09002639 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heof58229f2007-08-06 18:36:23 +09002640 struct ata_device *dev;
Tejun Heo084fe632006-05-31 18:28:03 +09002641
Tejun Heof9df58c2007-09-23 13:14:13 +09002642 /* skip disabled links */
2643 if (link->flags & ATA_LFLAG_DISABLED)
2644 return 1;
2645
Tejun Heo672b2d62008-01-24 00:05:14 +09002646 /* thaw frozen port and recover failed devices */
2647 if ((ap->pflags & ATA_PFLAG_FROZEN) || ata_link_nr_enabled(link))
2648 return 0;
2649
2650 /* reset at least once if reset is requested */
2651 if ((ehc->i.action & ATA_EH_RESET) &&
2652 !(ehc->i.flags & ATA_EHI_DID_RESET))
Tejun Heo084fe632006-05-31 18:28:03 +09002653 return 0;
2654
2655 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
Tejun Heo02607312007-08-06 18:36:23 +09002656 ata_link_for_each_dev(dev, link) {
Tejun Heo084fe632006-05-31 18:28:03 +09002657 if (dev->class == ATA_DEV_UNKNOWN &&
2658 ehc->classes[dev->devno] != ATA_DEV_NONE)
2659 return 0;
2660 }
2661
2662 return 1;
2663}
2664
Tejun Heo02c05a22007-11-27 19:28:54 +09002665static int ata_eh_schedule_probe(struct ata_device *dev)
2666{
2667 struct ata_eh_context *ehc = &dev->link->eh_context;
2668
2669 if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
2670 (ehc->did_probe_mask & (1 << dev->devno)))
2671 return 0;
2672
2673 ata_eh_detach_dev(dev);
2674 ata_dev_init(dev);
2675 ehc->did_probe_mask |= (1 << dev->devno);
Tejun Heocf480622008-01-24 00:05:14 +09002676 ehc->i.action |= ATA_EH_RESET;
Tejun Heo00115e02007-11-27 19:28:58 +09002677 ehc->saved_xfer_mode[dev->devno] = 0;
2678 ehc->saved_ncq_enabled &= ~(1 << dev->devno);
Tejun Heo02c05a22007-11-27 19:28:54 +09002679
2680 return 1;
2681}
2682
Tejun Heo9b1e2652007-08-06 18:36:24 +09002683static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
Tejun Heofee7ca72007-07-01 19:05:58 +09002684{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002685 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heofee7ca72007-07-01 19:05:58 +09002686
2687 ehc->tries[dev->devno]--;
2688
2689 switch (err) {
2690 case -ENODEV:
2691 /* device missing or wrong IDENTIFY data, schedule probing */
2692 ehc->i.probe_mask |= (1 << dev->devno);
2693 case -EINVAL:
2694 /* give it just one more chance */
2695 ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
2696 case -EIO:
Tejun Heo4fb46152007-10-29 16:45:05 +09002697 if (ehc->tries[dev->devno] == 1 && dev->pio_mode > XFER_PIO_0) {
Tejun Heofee7ca72007-07-01 19:05:58 +09002698 /* This is the last chance, better to slow
2699 * down than lose it.
2700 */
Tejun Heo936fd732007-08-06 18:36:23 +09002701 sata_down_spd_limit(dev->link);
Tejun Heofee7ca72007-07-01 19:05:58 +09002702 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2703 }
2704 }
2705
2706 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2707 /* disable device if it has used up all its chances */
2708 ata_dev_disable(dev);
2709
2710 /* detach if offline */
Tejun Heo936fd732007-08-06 18:36:23 +09002711 if (ata_link_offline(dev->link))
Tejun Heofee7ca72007-07-01 19:05:58 +09002712 ata_eh_detach_dev(dev);
2713
Tejun Heo02c05a22007-11-27 19:28:54 +09002714 /* schedule probe if necessary */
Tejun Heo87fbc5a2008-05-20 02:17:54 +09002715 if (ata_eh_schedule_probe(dev)) {
Tejun Heofee7ca72007-07-01 19:05:58 +09002716 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
Tejun Heo87fbc5a2008-05-20 02:17:54 +09002717 memset(ehc->cmd_timeout_idx[dev->devno], 0,
2718 sizeof(ehc->cmd_timeout_idx[dev->devno]));
2719 }
Tejun Heo9b1e2652007-08-06 18:36:24 +09002720
2721 return 1;
Tejun Heofee7ca72007-07-01 19:05:58 +09002722 } else {
Tejun Heocf480622008-01-24 00:05:14 +09002723 ehc->i.action |= ATA_EH_RESET;
Tejun Heo9b1e2652007-08-06 18:36:24 +09002724 return 0;
Tejun Heofee7ca72007-07-01 19:05:58 +09002725 }
2726}
2727
Tejun Heo022bdb02006-05-15 20:58:22 +09002728/**
2729 * ata_eh_recover - recover host port after error
2730 * @ap: host port to recover
Tejun Heof5914a42006-05-31 18:27:48 +09002731 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09002732 * @softreset: softreset method (can be NULL)
2733 * @hardreset: hardreset method (can be NULL)
2734 * @postreset: postreset method (can be NULL)
Tejun Heo9b1e2652007-08-06 18:36:24 +09002735 * @r_failed_link: out parameter for failed link
Tejun Heo022bdb02006-05-15 20:58:22 +09002736 *
2737 * This is the alpha and omega, eum and yang, heart and soul of
2738 * libata exception handling. On entry, actions required to
Tejun Heo9b1e2652007-08-06 18:36:24 +09002739 * recover each link and hotplug requests are recorded in the
2740 * link's eh_context. This function executes all the operations
2741 * with appropriate retrials and fallbacks to resurrect failed
Tejun Heo084fe632006-05-31 18:28:03 +09002742 * devices, detach goners and greet newcomers.
Tejun Heo022bdb02006-05-15 20:58:22 +09002743 *
2744 * LOCKING:
2745 * Kernel thread context (may sleep).
2746 *
2747 * RETURNS:
2748 * 0 on success, -errno on failure.
2749 */
Tejun Heofb7fd612007-09-23 13:14:12 +09002750int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
2751 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2752 ata_postreset_fn_t postreset,
2753 struct ata_link **r_failed_link)
Tejun Heo022bdb02006-05-15 20:58:22 +09002754{
Tejun Heo9b1e2652007-08-06 18:36:24 +09002755 struct ata_link *link;
Tejun Heo022bdb02006-05-15 20:58:22 +09002756 struct ata_device *dev;
Tejun Heo0a2c0f52008-05-20 02:17:52 +09002757 int nr_failed_devs;
Tejun Heodc98c322008-05-19 01:15:07 +09002758 int rc;
Tejun Heof9df58c2007-09-23 13:14:13 +09002759 unsigned long flags;
Tejun Heo022bdb02006-05-15 20:58:22 +09002760
2761 DPRINTK("ENTER\n");
2762
2763 /* prep for recovery */
Tejun Heo9b1e2652007-08-06 18:36:24 +09002764 ata_port_for_each_link(link, ap) {
2765 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo084fe632006-05-31 18:28:03 +09002766
Tejun Heof9df58c2007-09-23 13:14:13 +09002767 /* re-enable link? */
2768 if (ehc->i.action & ATA_EH_ENABLE_LINK) {
2769 ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
2770 spin_lock_irqsave(ap->lock, flags);
2771 link->flags &= ~ATA_LFLAG_DISABLED;
2772 spin_unlock_irqrestore(ap->lock, flags);
2773 ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
2774 }
2775
Tejun Heo9b1e2652007-08-06 18:36:24 +09002776 ata_link_for_each_dev(dev, link) {
Tejun Heofd995f72007-09-23 13:14:12 +09002777 if (link->flags & ATA_LFLAG_NO_RETRY)
2778 ehc->tries[dev->devno] = 1;
2779 else
2780 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
Tejun Heo79a55b72007-01-18 17:22:18 +09002781
Tejun Heo9b1e2652007-08-06 18:36:24 +09002782 /* collect port action mask recorded in dev actions */
2783 ehc->i.action |= ehc->i.dev_action[dev->devno] &
2784 ~ATA_EH_PERDEV_MASK;
2785 ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
Tejun Heo084fe632006-05-31 18:28:03 +09002786
Tejun Heo9b1e2652007-08-06 18:36:24 +09002787 /* process hotplug request */
2788 if (dev->flags & ATA_DFLAG_DETACH)
2789 ata_eh_detach_dev(dev);
2790
Tejun Heo02c05a22007-11-27 19:28:54 +09002791 /* schedule probe if necessary */
2792 if (!ata_dev_enabled(dev))
2793 ata_eh_schedule_probe(dev);
Tejun Heo084fe632006-05-31 18:28:03 +09002794 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002795 }
2796
2797 retry:
Tejun Heo022bdb02006-05-15 20:58:22 +09002798 rc = 0;
Tejun Heo9b1e2652007-08-06 18:36:24 +09002799 nr_failed_devs = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09002800
Tejun Heoaeb2ecd2006-06-12 14:11:43 +09002801 /* if UNLOADING, finish immediately */
Tejun Heob51e9e52006-06-29 01:29:30 +09002802 if (ap->pflags & ATA_PFLAG_UNLOADING)
Tejun Heoaeb2ecd2006-06-12 14:11:43 +09002803 goto out;
2804
Tejun Heo9b1e2652007-08-06 18:36:24 +09002805 /* prep for EH */
2806 ata_port_for_each_link(link, ap) {
2807 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09002808
Tejun Heo9b1e2652007-08-06 18:36:24 +09002809 /* skip EH if possible. */
2810 if (ata_eh_skip_recovery(link))
2811 ehc->i.action = 0;
2812
Tejun Heo9b1e2652007-08-06 18:36:24 +09002813 ata_link_for_each_dev(dev, link)
2814 ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
2815 }
Tejun Heo084fe632006-05-31 18:28:03 +09002816
Tejun Heo022bdb02006-05-15 20:58:22 +09002817 /* reset */
Tejun Heodc98c322008-05-19 01:15:07 +09002818 ata_port_for_each_link(link, ap) {
2819 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09002820
Tejun Heodc98c322008-05-19 01:15:07 +09002821 if (!(ehc->i.action & ATA_EH_RESET))
2822 continue;
Tejun Heo9b1e2652007-08-06 18:36:24 +09002823
Tejun Heodc98c322008-05-19 01:15:07 +09002824 rc = ata_eh_reset(link, ata_link_nr_vacant(link),
2825 prereset, softreset, hardreset, postreset);
2826 if (rc) {
2827 ata_link_printk(link, KERN_ERR,
2828 "reset failed, giving up\n");
2829 goto out;
Tejun Heo022bdb02006-05-15 20:58:22 +09002830 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002831 }
2832
Tejun Heo9b1e2652007-08-06 18:36:24 +09002833 /* the rest */
2834 ata_port_for_each_link(link, ap) {
2835 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09002836
Tejun Heo9b1e2652007-08-06 18:36:24 +09002837 /* revalidate existing devices and attach new ones */
2838 rc = ata_eh_revalidate_and_attach(link, &dev);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002839 if (rc)
Tejun Heo022bdb02006-05-15 20:58:22 +09002840 goto dev_fail;
Tejun Heo9b1e2652007-08-06 18:36:24 +09002841
Tejun Heo633273a2007-09-23 13:19:54 +09002842 /* if PMP got attached, return, pmp EH will take care of it */
2843 if (link->device->class == ATA_DEV_PMP) {
2844 ehc->i.action = 0;
2845 return 0;
2846 }
2847
Tejun Heo9b1e2652007-08-06 18:36:24 +09002848 /* configure transfer mode if necessary */
2849 if (ehc->i.flags & ATA_EHI_SETMODE) {
2850 rc = ata_set_mode(link, &dev);
2851 if (rc)
2852 goto dev_fail;
2853 ehc->i.flags &= ~ATA_EHI_SETMODE;
2854 }
2855
Tejun Heo3ec25eb2008-03-27 18:37:14 +09002856 if (ehc->i.action & ATA_EH_LPM)
Kristen Carlson Accardica773292007-10-25 00:58:59 -04002857 ata_link_for_each_dev(dev, link)
2858 ata_dev_enable_pm(dev, ap->pm_policy);
2859
Tejun Heo9b1e2652007-08-06 18:36:24 +09002860 /* this link is okay now */
2861 ehc->i.flags = 0;
2862 continue;
2863
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002864dev_fail:
Tejun Heo9b1e2652007-08-06 18:36:24 +09002865 nr_failed_devs++;
Tejun Heo0a2c0f52008-05-20 02:17:52 +09002866 ata_eh_handle_dev_fail(dev, rc);
Tejun Heo9b1e2652007-08-06 18:36:24 +09002867
Tejun Heob06ce3e2007-10-09 15:06:48 +09002868 if (ap->pflags & ATA_PFLAG_FROZEN) {
2869 /* PMP reset requires working host port.
2870 * Can't retry if it's frozen.
2871 */
Tejun Heo071f44b2008-04-07 22:47:22 +09002872 if (sata_pmp_attached(ap))
Tejun Heob06ce3e2007-10-09 15:06:48 +09002873 goto out;
Tejun Heo9b1e2652007-08-06 18:36:24 +09002874 break;
Tejun Heob06ce3e2007-10-09 15:06:48 +09002875 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002876 }
2877
Tejun Heo0a2c0f52008-05-20 02:17:52 +09002878 if (nr_failed_devs)
Tejun Heo9b1e2652007-08-06 18:36:24 +09002879 goto retry;
Tejun Heo022bdb02006-05-15 20:58:22 +09002880
Tejun Heo022bdb02006-05-15 20:58:22 +09002881 out:
Tejun Heo9b1e2652007-08-06 18:36:24 +09002882 if (rc && r_failed_link)
2883 *r_failed_link = link;
Tejun Heo022bdb02006-05-15 20:58:22 +09002884
2885 DPRINTK("EXIT, rc=%d\n", rc);
2886 return rc;
2887}
2888
2889/**
2890 * ata_eh_finish - finish up EH
2891 * @ap: host port to finish EH for
2892 *
2893 * Recovery is complete. Clean up EH states and retry or finish
2894 * failed qcs.
2895 *
2896 * LOCKING:
2897 * None.
2898 */
Tejun Heofb7fd612007-09-23 13:14:12 +09002899void ata_eh_finish(struct ata_port *ap)
Tejun Heo022bdb02006-05-15 20:58:22 +09002900{
2901 int tag;
2902
2903 /* retry or finish qcs */
2904 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2905 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2906
2907 if (!(qc->flags & ATA_QCFLAG_FAILED))
2908 continue;
2909
2910 if (qc->err_mask) {
2911 /* FIXME: Once EH migration is complete,
2912 * generate sense data in this function,
2913 * considering both err_mask and tf.
2914 */
Tejun Heo03faab72008-03-27 19:14:24 +09002915 if (qc->flags & ATA_QCFLAG_RETRY)
Tejun Heo022bdb02006-05-15 20:58:22 +09002916 ata_eh_qc_retry(qc);
Tejun Heo03faab72008-03-27 19:14:24 +09002917 else
2918 ata_eh_qc_complete(qc);
Tejun Heo022bdb02006-05-15 20:58:22 +09002919 } else {
2920 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2921 ata_eh_qc_complete(qc);
2922 } else {
2923 /* feed zero TF to sense generation */
2924 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2925 ata_eh_qc_retry(qc);
2926 }
2927 }
2928 }
Tejun Heoda917d62007-09-23 13:14:12 +09002929
2930 /* make sure nr_active_links is zero after EH */
2931 WARN_ON(ap->nr_active_links);
2932 ap->nr_active_links = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09002933}
2934
2935/**
2936 * ata_do_eh - do standard error handling
2937 * @ap: host port to handle error for
Tejun Heoa1efdab2008-03-25 12:22:50 +09002938 *
Tejun Heof5914a42006-05-31 18:27:48 +09002939 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09002940 * @softreset: softreset method (can be NULL)
2941 * @hardreset: hardreset method (can be NULL)
2942 * @postreset: postreset method (can be NULL)
2943 *
2944 * Perform standard error handling sequence.
2945 *
2946 * LOCKING:
2947 * Kernel thread context (may sleep).
2948 */
Tejun Heof5914a42006-05-31 18:27:48 +09002949void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2950 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2951 ata_postreset_fn_t postreset)
Tejun Heo022bdb02006-05-15 20:58:22 +09002952{
Tejun Heo9b1e2652007-08-06 18:36:24 +09002953 struct ata_device *dev;
2954 int rc;
2955
2956 ata_eh_autopsy(ap);
2957 ata_eh_report(ap);
2958
2959 rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
2960 NULL);
2961 if (rc) {
2962 ata_link_for_each_dev(dev, &ap->link)
2963 ata_dev_disable(dev);
2964 }
2965
Tejun Heo022bdb02006-05-15 20:58:22 +09002966 ata_eh_finish(ap);
2967}
Tejun Heo500530f2006-07-03 16:07:27 +09002968
Tejun Heoa1efdab2008-03-25 12:22:50 +09002969/**
2970 * ata_std_error_handler - standard error handler
2971 * @ap: host port to handle error for
2972 *
2973 * Standard error handler
2974 *
2975 * LOCKING:
2976 * Kernel thread context (may sleep).
2977 */
2978void ata_std_error_handler(struct ata_port *ap)
2979{
2980 struct ata_port_operations *ops = ap->ops;
2981 ata_reset_fn_t hardreset = ops->hardreset;
2982
Tejun Heo57c9efd2008-04-07 22:47:19 +09002983 /* ignore built-in hardreset if SCR access is not available */
2984 if (ata_is_builtin_hardreset(hardreset) && !sata_scr_valid(&ap->link))
Tejun Heoa1efdab2008-03-25 12:22:50 +09002985 hardreset = NULL;
2986
2987 ata_do_eh(ap, ops->prereset, ops->softreset, hardreset, ops->postreset);
2988}
2989
Tejun Heo6ffa01d2007-03-02 17:32:47 +09002990#ifdef CONFIG_PM
Tejun Heo500530f2006-07-03 16:07:27 +09002991/**
2992 * ata_eh_handle_port_suspend - perform port suspend operation
2993 * @ap: port to suspend
2994 *
2995 * Suspend @ap.
2996 *
2997 * LOCKING:
2998 * Kernel thread context (may sleep).
2999 */
3000static void ata_eh_handle_port_suspend(struct ata_port *ap)
3001{
3002 unsigned long flags;
3003 int rc = 0;
3004
3005 /* are we suspending? */
3006 spin_lock_irqsave(ap->lock, flags);
3007 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
3008 ap->pm_mesg.event == PM_EVENT_ON) {
3009 spin_unlock_irqrestore(ap->lock, flags);
3010 return;
3011 }
3012 spin_unlock_irqrestore(ap->lock, flags);
3013
3014 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
3015
Tejun Heo64578a32007-05-15 03:28:16 +09003016 /* tell ACPI we're suspending */
3017 rc = ata_acpi_on_suspend(ap);
3018 if (rc)
3019 goto out;
3020
Tejun Heo500530f2006-07-03 16:07:27 +09003021 /* suspend */
3022 ata_eh_freeze_port(ap);
3023
3024 if (ap->ops->port_suspend)
3025 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
3026
Shaohua Libd3adca2007-11-02 09:32:38 +08003027 ata_acpi_set_state(ap, PMSG_SUSPEND);
Tejun Heo64578a32007-05-15 03:28:16 +09003028 out:
Tejun Heo500530f2006-07-03 16:07:27 +09003029 /* report result */
3030 spin_lock_irqsave(ap->lock, flags);
3031
3032 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
3033 if (rc == 0)
3034 ap->pflags |= ATA_PFLAG_SUSPENDED;
Tejun Heo64578a32007-05-15 03:28:16 +09003035 else if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heo500530f2006-07-03 16:07:27 +09003036 ata_port_schedule_eh(ap);
3037
3038 if (ap->pm_result) {
3039 *ap->pm_result = rc;
3040 ap->pm_result = NULL;
3041 }
3042
3043 spin_unlock_irqrestore(ap->lock, flags);
3044
3045 return;
3046}
3047
3048/**
3049 * ata_eh_handle_port_resume - perform port resume operation
3050 * @ap: port to resume
3051 *
3052 * Resume @ap.
3053 *
Tejun Heo500530f2006-07-03 16:07:27 +09003054 * LOCKING:
3055 * Kernel thread context (may sleep).
3056 */
3057static void ata_eh_handle_port_resume(struct ata_port *ap)
3058{
Tejun Heo500530f2006-07-03 16:07:27 +09003059 unsigned long flags;
Tejun Heo9666f402007-05-04 21:27:47 +02003060 int rc = 0;
Tejun Heo500530f2006-07-03 16:07:27 +09003061
3062 /* are we resuming? */
3063 spin_lock_irqsave(ap->lock, flags);
3064 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
3065 ap->pm_mesg.event != PM_EVENT_ON) {
3066 spin_unlock_irqrestore(ap->lock, flags);
3067 return;
3068 }
3069 spin_unlock_irqrestore(ap->lock, flags);
3070
Tejun Heo9666f402007-05-04 21:27:47 +02003071 WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
Tejun Heo500530f2006-07-03 16:07:27 +09003072
Shaohua Libd3adca2007-11-02 09:32:38 +08003073 ata_acpi_set_state(ap, PMSG_ON);
3074
Tejun Heo500530f2006-07-03 16:07:27 +09003075 if (ap->ops->port_resume)
3076 rc = ap->ops->port_resume(ap);
3077
Tejun Heo67465442007-05-15 03:28:16 +09003078 /* tell ACPI that we're resuming */
3079 ata_acpi_on_resume(ap);
3080
Tejun Heo9666f402007-05-04 21:27:47 +02003081 /* report result */
Tejun Heo500530f2006-07-03 16:07:27 +09003082 spin_lock_irqsave(ap->lock, flags);
3083 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
3084 if (ap->pm_result) {
3085 *ap->pm_result = rc;
3086 ap->pm_result = NULL;
3087 }
3088 spin_unlock_irqrestore(ap->lock, flags);
3089}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09003090#endif /* CONFIG_PM */