blob: e6605f0386477f7f41fc4821debb11b0191fb9b6 [file] [log] [blame]
Tejun Heoece1d632006-04-02 18:51:53 +09001/*
2 * libata-eh.c - libata error handling
3 *
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
7 *
8 * Copyright 2006 Tejun Heo <htejun@gmail.com>
9 *
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; see the file COPYING. If not, write to
23 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
24 * USA.
25 *
26 *
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
29 *
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
32 *
33 */
34
Tejun Heoece1d632006-04-02 18:51:53 +090035#include <linux/kernel.h>
Jeff Garzik28555682007-10-11 17:12:35 -040036#include <linux/pci.h>
Tejun Heoece1d632006-04-02 18:51:53 +090037#include <scsi/scsi.h>
38#include <scsi/scsi_host.h>
39#include <scsi/scsi_eh.h>
40#include <scsi/scsi_device.h>
41#include <scsi/scsi_cmnd.h>
Jeff Garzikc6fd2802006-08-10 07:31:37 -040042#include "../scsi/scsi_transport_api.h"
Tejun Heoece1d632006-04-02 18:51:53 +090043
44#include <linux/libata.h>
45
46#include "libata.h"
47
Tejun Heo7d47e8d2007-02-02 16:22:31 +090048enum {
49 ATA_EH_SPDN_NCQ_OFF = (1 << 0),
50 ATA_EH_SPDN_SPEED_DOWN = (1 << 1),
51 ATA_EH_SPDN_FALLBACK_TO_PIO = (1 << 2),
52};
53
Tejun Heo31daabd2007-02-02 16:50:52 +090054/* Waiting in ->prereset can never be reliable. It's sometimes nice
55 * to wait there but it can't be depended upon; otherwise, we wouldn't
56 * be resetting. Just give it enough time for most drives to spin up.
57 */
58enum {
59 ATA_EH_PRERESET_TIMEOUT = 10 * HZ,
Tejun Heo5ddf24c2007-07-16 14:29:41 +090060 ATA_EH_FASTDRAIN_INTERVAL = 3 * HZ,
Tejun Heo31daabd2007-02-02 16:50:52 +090061};
62
63/* The following table determines how we sequence resets. Each entry
64 * represents timeout for that try. The first try can be soft or
65 * hardreset. All others are hardreset if available. In most cases
66 * the first reset w/ 10sec timeout should succeed. Following entries
67 * are mostly for error handling, hotplug and retarded devices.
68 */
69static const unsigned long ata_eh_reset_timeouts[] = {
70 10 * HZ, /* most drives spin up by 10sec */
71 10 * HZ, /* > 99% working drives spin up before 20sec */
72 35 * HZ, /* give > 30 secs of idleness for retarded devices */
73 5 * HZ, /* and sweet one last chance */
74 /* > 1 min has elapsed, give up */
75};
76
Tejun Heoad9e2762006-05-15 20:58:12 +090077static void __ata_port_freeze(struct ata_port *ap);
Tejun Heo6ffa01d2007-03-02 17:32:47 +090078#ifdef CONFIG_PM
Tejun Heo500530f2006-07-03 16:07:27 +090079static void ata_eh_handle_port_suspend(struct ata_port *ap);
80static void ata_eh_handle_port_resume(struct ata_port *ap);
Tejun Heo6ffa01d2007-03-02 17:32:47 +090081#else /* CONFIG_PM */
82static void ata_eh_handle_port_suspend(struct ata_port *ap)
83{ }
84
85static void ata_eh_handle_port_resume(struct ata_port *ap)
86{ }
Tejun Heo6ffa01d2007-03-02 17:32:47 +090087#endif /* CONFIG_PM */
Tejun Heoad9e2762006-05-15 20:58:12 +090088
Tejun Heob64bbc32007-07-16 14:29:39 +090089static void __ata_ehi_pushv_desc(struct ata_eh_info *ehi, const char *fmt,
90 va_list args)
91{
92 ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
93 ATA_EH_DESC_LEN - ehi->desc_len,
94 fmt, args);
95}
96
97/**
98 * __ata_ehi_push_desc - push error description without adding separator
99 * @ehi: target EHI
100 * @fmt: printf format string
101 *
102 * Format string according to @fmt and append it to @ehi->desc.
103 *
104 * LOCKING:
105 * spin_lock_irqsave(host lock)
106 */
107void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
108{
109 va_list args;
110
111 va_start(args, fmt);
112 __ata_ehi_pushv_desc(ehi, fmt, args);
113 va_end(args);
114}
115
116/**
117 * ata_ehi_push_desc - push error description with separator
118 * @ehi: target EHI
119 * @fmt: printf format string
120 *
121 * Format string according to @fmt and append it to @ehi->desc.
122 * If @ehi->desc is not empty, ", " is added in-between.
123 *
124 * LOCKING:
125 * spin_lock_irqsave(host lock)
126 */
127void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
128{
129 va_list args;
130
131 if (ehi->desc_len)
132 __ata_ehi_push_desc(ehi, ", ");
133
134 va_start(args, fmt);
135 __ata_ehi_pushv_desc(ehi, fmt, args);
136 va_end(args);
137}
138
139/**
140 * ata_ehi_clear_desc - clean error description
141 * @ehi: target EHI
142 *
143 * Clear @ehi->desc.
144 *
145 * LOCKING:
146 * spin_lock_irqsave(host lock)
147 */
148void ata_ehi_clear_desc(struct ata_eh_info *ehi)
149{
150 ehi->desc[0] = '\0';
151 ehi->desc_len = 0;
152}
153
Tejun Heocbcdd872007-08-18 13:14:55 +0900154/**
155 * ata_port_desc - append port description
156 * @ap: target ATA port
157 * @fmt: printf format string
158 *
159 * Format string according to @fmt and append it to port
160 * description. If port description is not empty, " " is added
161 * in-between. This function is to be used while initializing
162 * ata_host. The description is printed on host registration.
163 *
164 * LOCKING:
165 * None.
166 */
167void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
168{
169 va_list args;
170
171 WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
172
173 if (ap->link.eh_info.desc_len)
174 __ata_ehi_push_desc(&ap->link.eh_info, " ");
175
176 va_start(args, fmt);
177 __ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
178 va_end(args);
179}
180
181#ifdef CONFIG_PCI
182
183/**
184 * ata_port_pbar_desc - append PCI BAR description
185 * @ap: target ATA port
186 * @bar: target PCI BAR
187 * @offset: offset into PCI BAR
188 * @name: name of the area
189 *
190 * If @offset is negative, this function formats a string which
191 * contains the name, address, size and type of the BAR and
192 * appends it to the port description. If @offset is zero or
193 * positive, only name and offsetted address is appended.
194 *
195 * LOCKING:
196 * None.
197 */
198void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
199 const char *name)
200{
201 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
202 char *type = "";
203 unsigned long long start, len;
204
205 if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
206 type = "m";
207 else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
208 type = "i";
209
210 start = (unsigned long long)pci_resource_start(pdev, bar);
211 len = (unsigned long long)pci_resource_len(pdev, bar);
212
213 if (offset < 0)
214 ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
215 else
216 ata_port_desc(ap, "%s 0x%llx", name, start + offset);
217}
218
219#endif /* CONFIG_PCI */
220
Tejun Heo0c247c52006-05-15 20:58:19 +0900221static void ata_ering_record(struct ata_ering *ering, int is_io,
222 unsigned int err_mask)
223{
224 struct ata_ering_entry *ent;
225
226 WARN_ON(!err_mask);
227
228 ering->cursor++;
229 ering->cursor %= ATA_ERING_SIZE;
230
231 ent = &ering->ring[ering->cursor];
232 ent->is_io = is_io;
233 ent->err_mask = err_mask;
234 ent->timestamp = get_jiffies_64();
235}
236
Tejun Heo7d47e8d2007-02-02 16:22:31 +0900237static void ata_ering_clear(struct ata_ering *ering)
Tejun Heo0c247c52006-05-15 20:58:19 +0900238{
Tejun Heo7d47e8d2007-02-02 16:22:31 +0900239 memset(ering, 0, sizeof(*ering));
Tejun Heo0c247c52006-05-15 20:58:19 +0900240}
241
242static int ata_ering_map(struct ata_ering *ering,
243 int (*map_fn)(struct ata_ering_entry *, void *),
244 void *arg)
245{
246 int idx, rc = 0;
247 struct ata_ering_entry *ent;
248
249 idx = ering->cursor;
250 do {
251 ent = &ering->ring[idx];
252 if (!ent->err_mask)
253 break;
254 rc = map_fn(ent, arg);
255 if (rc)
256 break;
257 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
258 } while (idx != ering->cursor);
259
260 return rc;
261}
262
Tejun Heo64f65ca2006-06-24 20:30:18 +0900263static unsigned int ata_eh_dev_action(struct ata_device *dev)
264{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900265 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heo64f65ca2006-06-24 20:30:18 +0900266
267 return ehc->i.action | ehc->i.dev_action[dev->devno];
268}
269
Tejun Heof58229f2007-08-06 18:36:23 +0900270static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
Tejun Heoaf181c22006-06-24 20:30:18 +0900271 struct ata_eh_info *ehi, unsigned int action)
272{
Tejun Heof58229f2007-08-06 18:36:23 +0900273 struct ata_device *tdev;
Tejun Heoaf181c22006-06-24 20:30:18 +0900274
275 if (!dev) {
276 ehi->action &= ~action;
Tejun Heof58229f2007-08-06 18:36:23 +0900277 ata_link_for_each_dev(tdev, link)
278 ehi->dev_action[tdev->devno] &= ~action;
Tejun Heoaf181c22006-06-24 20:30:18 +0900279 } else {
280 /* doesn't make sense for port-wide EH actions */
281 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
282
283 /* break ehi->action into ehi->dev_action */
284 if (ehi->action & action) {
Tejun Heof58229f2007-08-06 18:36:23 +0900285 ata_link_for_each_dev(tdev, link)
286 ehi->dev_action[tdev->devno] |=
287 ehi->action & action;
Tejun Heoaf181c22006-06-24 20:30:18 +0900288 ehi->action &= ~action;
289 }
290
291 /* turn off the specified per-dev action */
292 ehi->dev_action[dev->devno] &= ~action;
293 }
294}
295
Tejun Heoece1d632006-04-02 18:51:53 +0900296/**
297 * ata_scsi_timed_out - SCSI layer time out callback
298 * @cmd: timed out SCSI command
299 *
300 * Handles SCSI layer timeout. We race with normal completion of
301 * the qc for @cmd. If the qc is already gone, we lose and let
302 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
303 * timed out and EH should be invoked. Prevent ata_qc_complete()
304 * from finishing it by setting EH_SCHEDULED and return
305 * EH_NOT_HANDLED.
306 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900307 * TODO: kill this function once old EH is gone.
308 *
Tejun Heoece1d632006-04-02 18:51:53 +0900309 * LOCKING:
310 * Called from timer context
311 *
312 * RETURNS:
313 * EH_HANDLED or EH_NOT_HANDLED
314 */
315enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
316{
317 struct Scsi_Host *host = cmd->device->host;
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400318 struct ata_port *ap = ata_shost_to_port(host);
Tejun Heoece1d632006-04-02 18:51:53 +0900319 unsigned long flags;
320 struct ata_queued_cmd *qc;
Tejun Heoad9e2762006-05-15 20:58:12 +0900321 enum scsi_eh_timer_return ret;
Tejun Heoece1d632006-04-02 18:51:53 +0900322
323 DPRINTK("ENTER\n");
324
Tejun Heoad9e2762006-05-15 20:58:12 +0900325 if (ap->ops->error_handler) {
326 ret = EH_NOT_HANDLED;
327 goto out;
328 }
329
330 ret = EH_HANDLED;
Jeff Garzikba6a1302006-06-22 23:46:10 -0400331 spin_lock_irqsave(ap->lock, flags);
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900332 qc = ata_qc_from_tag(ap, ap->link.active_tag);
Tejun Heoece1d632006-04-02 18:51:53 +0900333 if (qc) {
334 WARN_ON(qc->scsicmd != cmd);
335 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
336 qc->err_mask |= AC_ERR_TIMEOUT;
337 ret = EH_NOT_HANDLED;
338 }
Jeff Garzikba6a1302006-06-22 23:46:10 -0400339 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900340
Tejun Heoad9e2762006-05-15 20:58:12 +0900341 out:
Tejun Heoece1d632006-04-02 18:51:53 +0900342 DPRINTK("EXIT, ret=%d\n", ret);
343 return ret;
344}
345
346/**
347 * ata_scsi_error - SCSI layer error handler callback
348 * @host: SCSI host on which error occurred
349 *
350 * Handles SCSI-layer-thrown error events.
351 *
352 * LOCKING:
353 * Inherited from SCSI layer (none, can sleep)
354 *
355 * RETURNS:
356 * Zero.
357 */
Jeff Garzik381544b2006-04-11 13:04:39 -0400358void ata_scsi_error(struct Scsi_Host *host)
Tejun Heoece1d632006-04-02 18:51:53 +0900359{
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400360 struct ata_port *ap = ata_shost_to_port(host);
Tejun Heoa1e10f72007-08-18 13:28:49 +0900361 int i;
Tejun Heoad9e2762006-05-15 20:58:12 +0900362 unsigned long flags;
Tejun Heoece1d632006-04-02 18:51:53 +0900363
364 DPRINTK("ENTER\n");
365
Tejun Heoad9e2762006-05-15 20:58:12 +0900366 /* synchronize with port task */
Tejun Heoece1d632006-04-02 18:51:53 +0900367 ata_port_flush_task(ap);
368
Jeff Garzikcca39742006-08-24 03:19:22 -0400369 /* synchronize with host lock and sort out timeouts */
Tejun Heoece1d632006-04-02 18:51:53 +0900370
Tejun Heoad9e2762006-05-15 20:58:12 +0900371 /* For new EH, all qcs are finished in one of three ways -
372 * normal completion, error completion, and SCSI timeout.
373 * Both cmpletions can race against SCSI timeout. When normal
374 * completion wins, the qc never reaches EH. When error
375 * completion wins, the qc has ATA_QCFLAG_FAILED set.
376 *
377 * When SCSI timeout wins, things are a bit more complex.
378 * Normal or error completion can occur after the timeout but
379 * before this point. In such cases, both types of
380 * completions are honored. A scmd is determined to have
381 * timed out iff its associated qc is active and not failed.
382 */
383 if (ap->ops->error_handler) {
384 struct scsi_cmnd *scmd, *tmp;
385 int nr_timedout = 0;
Tejun Heoece1d632006-04-02 18:51:53 +0900386
Tejun Heoe30349d2006-07-03 03:02:15 +0900387 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900388
389 list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
390 struct ata_queued_cmd *qc;
391
392 for (i = 0; i < ATA_MAX_QUEUE; i++) {
393 qc = __ata_qc_from_tag(ap, i);
394 if (qc->flags & ATA_QCFLAG_ACTIVE &&
395 qc->scsicmd == scmd)
396 break;
397 }
398
399 if (i < ATA_MAX_QUEUE) {
400 /* the scmd has an associated qc */
401 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
402 /* which hasn't failed yet, timeout */
403 qc->err_mask |= AC_ERR_TIMEOUT;
404 qc->flags |= ATA_QCFLAG_FAILED;
405 nr_timedout++;
406 }
407 } else {
408 /* Normal completion occurred after
409 * SCSI timeout but before this point.
410 * Successfully complete it.
411 */
412 scmd->retries = scmd->allowed;
413 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
414 }
415 }
416
417 /* If we have timed out qcs. They belong to EH from
418 * this point but the state of the controller is
419 * unknown. Freeze the port to make sure the IRQ
420 * handler doesn't diddle with those qcs. This must
421 * be done atomically w.r.t. setting QCFLAG_FAILED.
422 */
423 if (nr_timedout)
424 __ata_port_freeze(ap);
425
Tejun Heoe30349d2006-07-03 03:02:15 +0900426 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa1e10f72007-08-18 13:28:49 +0900427
428 /* initialize eh_tries */
429 ap->eh_tries = ATA_EH_MAX_TRIES;
Tejun Heoad9e2762006-05-15 20:58:12 +0900430 } else
Tejun Heoe30349d2006-07-03 03:02:15 +0900431 spin_unlock_wait(ap->lock);
Tejun Heoad9e2762006-05-15 20:58:12 +0900432
433 repeat:
434 /* invoke error handler */
435 if (ap->ops->error_handler) {
Tejun Heocf1b86c2007-08-06 18:36:23 +0900436 struct ata_link *link;
437
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900438 /* kill fast drain timer */
439 del_timer_sync(&ap->fastdrain_timer);
440
Tejun Heo500530f2006-07-03 16:07:27 +0900441 /* process port resume request */
442 ata_eh_handle_port_resume(ap);
443
Tejun Heof3e81b12006-05-15 20:58:21 +0900444 /* fetch & clear EH info */
Tejun Heoe30349d2006-07-03 03:02:15 +0900445 spin_lock_irqsave(ap->lock, flags);
Tejun Heof3e81b12006-05-15 20:58:21 +0900446
Tejun Heocf1b86c2007-08-06 18:36:23 +0900447 __ata_port_for_each_link(link, ap) {
448 memset(&link->eh_context, 0, sizeof(link->eh_context));
449 link->eh_context.i = link->eh_info;
450 memset(&link->eh_info, 0, sizeof(link->eh_info));
451 }
Tejun Heof3e81b12006-05-15 20:58:21 +0900452
Tejun Heob51e9e52006-06-29 01:29:30 +0900453 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
454 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
Tejun Heoda917d62007-09-23 13:14:12 +0900455 ap->excl_link = NULL; /* don't maintain exclusion over EH */
Tejun Heof3e81b12006-05-15 20:58:21 +0900456
Tejun Heoe30349d2006-07-03 03:02:15 +0900457 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900458
Tejun Heo500530f2006-07-03 16:07:27 +0900459 /* invoke EH, skip if unloading or suspended */
460 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
Tejun Heo720ba122006-05-31 18:28:13 +0900461 ap->ops->error_handler(ap);
462 else
463 ata_eh_finish(ap);
Tejun Heoad9e2762006-05-15 20:58:12 +0900464
Tejun Heo500530f2006-07-03 16:07:27 +0900465 /* process port suspend request */
466 ata_eh_handle_port_suspend(ap);
467
Tejun Heoad9e2762006-05-15 20:58:12 +0900468 /* Exception might have happend after ->error_handler
469 * recovered the port but before this point. Repeat
470 * EH in such case.
471 */
Tejun Heoe30349d2006-07-03 03:02:15 +0900472 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900473
Tejun Heob51e9e52006-06-29 01:29:30 +0900474 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
Tejun Heoa1e10f72007-08-18 13:28:49 +0900475 if (--ap->eh_tries) {
Tejun Heoe30349d2006-07-03 03:02:15 +0900476 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900477 goto repeat;
478 }
479 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
Tejun Heoa1e10f72007-08-18 13:28:49 +0900480 "tries, giving up\n", ATA_EH_MAX_TRIES);
Tejun Heo914616a2007-06-25 21:47:11 +0900481 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
Tejun Heoad9e2762006-05-15 20:58:12 +0900482 }
483
Tejun Heof3e81b12006-05-15 20:58:21 +0900484 /* this run is complete, make sure EH info is clear */
Tejun Heocf1b86c2007-08-06 18:36:23 +0900485 __ata_port_for_each_link(link, ap)
486 memset(&link->eh_info, 0, sizeof(link->eh_info));
Tejun Heof3e81b12006-05-15 20:58:21 +0900487
Tejun Heoe30349d2006-07-03 03:02:15 +0900488 /* Clear host_eh_scheduled while holding ap->lock such
Tejun Heoad9e2762006-05-15 20:58:12 +0900489 * that if exception occurs after this point but
490 * before EH completion, SCSI midlayer will
491 * re-initiate EH.
492 */
493 host->host_eh_scheduled = 0;
494
Tejun Heoe30349d2006-07-03 03:02:15 +0900495 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900496 } else {
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900497 WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
Tejun Heoad9e2762006-05-15 20:58:12 +0900498 ap->ops->eng_timeout(ap);
499 }
500
501 /* finish or retry handled scmd's and clean up */
Tejun Heoece1d632006-04-02 18:51:53 +0900502 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
503
504 scsi_eh_flush_done_q(&ap->eh_done_q);
505
Tejun Heoad9e2762006-05-15 20:58:12 +0900506 /* clean up */
Tejun Heoe30349d2006-07-03 03:02:15 +0900507 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900508
Tejun Heo1cdaf532006-07-03 16:07:26 +0900509 if (ap->pflags & ATA_PFLAG_LOADING)
Tejun Heob51e9e52006-06-29 01:29:30 +0900510 ap->pflags &= ~ATA_PFLAG_LOADING;
Tejun Heo1cdaf532006-07-03 16:07:26 +0900511 else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
David Howells52bad642006-11-22 14:54:01 +0000512 queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0);
Tejun Heo1cdaf532006-07-03 16:07:26 +0900513
514 if (ap->pflags & ATA_PFLAG_RECOVERED)
515 ata_port_printk(ap, KERN_INFO, "EH complete\n");
Tejun Heo580b21022006-05-31 18:28:05 +0900516
Tejun Heob51e9e52006-06-29 01:29:30 +0900517 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
Tejun Heoad9e2762006-05-15 20:58:12 +0900518
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900519 /* tell wait_eh that we're done */
Tejun Heob51e9e52006-06-29 01:29:30 +0900520 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900521 wake_up_all(&ap->eh_wait_q);
522
Tejun Heoe30349d2006-07-03 03:02:15 +0900523 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900524
Tejun Heoece1d632006-04-02 18:51:53 +0900525 DPRINTK("EXIT\n");
Tejun Heoece1d632006-04-02 18:51:53 +0900526}
527
528/**
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900529 * ata_port_wait_eh - Wait for the currently pending EH to complete
530 * @ap: Port to wait EH for
531 *
532 * Wait until the currently pending EH is complete.
533 *
534 * LOCKING:
535 * Kernel thread context (may sleep).
536 */
537void ata_port_wait_eh(struct ata_port *ap)
538{
539 unsigned long flags;
540 DEFINE_WAIT(wait);
541
542 retry:
Jeff Garzikba6a1302006-06-22 23:46:10 -0400543 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900544
Tejun Heob51e9e52006-06-29 01:29:30 +0900545 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900546 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
Jeff Garzikba6a1302006-06-22 23:46:10 -0400547 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900548 schedule();
Jeff Garzikba6a1302006-06-22 23:46:10 -0400549 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900550 }
Tejun Heo0a1b6222006-06-11 11:01:38 +0900551 finish_wait(&ap->eh_wait_q, &wait);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900552
Jeff Garzikba6a1302006-06-22 23:46:10 -0400553 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900554
555 /* make sure SCSI EH is complete */
Jeff Garzikcca39742006-08-24 03:19:22 -0400556 if (scsi_host_in_recovery(ap->scsi_host)) {
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900557 msleep(10);
558 goto retry;
559 }
560}
561
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900562static int ata_eh_nr_in_flight(struct ata_port *ap)
563{
564 unsigned int tag;
565 int nr = 0;
566
567 /* count only non-internal commands */
568 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++)
569 if (ata_qc_from_tag(ap, tag))
570 nr++;
571
572 return nr;
573}
574
575void ata_eh_fastdrain_timerfn(unsigned long arg)
576{
577 struct ata_port *ap = (void *)arg;
578 unsigned long flags;
579 int cnt;
580
581 spin_lock_irqsave(ap->lock, flags);
582
583 cnt = ata_eh_nr_in_flight(ap);
584
585 /* are we done? */
586 if (!cnt)
587 goto out_unlock;
588
589 if (cnt == ap->fastdrain_cnt) {
590 unsigned int tag;
591
592 /* No progress during the last interval, tag all
593 * in-flight qcs as timed out and freeze the port.
594 */
595 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) {
596 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
597 if (qc)
598 qc->err_mask |= AC_ERR_TIMEOUT;
599 }
600
601 ata_port_freeze(ap);
602 } else {
603 /* some qcs have finished, give it another chance */
604 ap->fastdrain_cnt = cnt;
605 ap->fastdrain_timer.expires =
606 jiffies + ATA_EH_FASTDRAIN_INTERVAL;
607 add_timer(&ap->fastdrain_timer);
608 }
609
610 out_unlock:
611 spin_unlock_irqrestore(ap->lock, flags);
612}
613
614/**
615 * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
616 * @ap: target ATA port
617 * @fastdrain: activate fast drain
618 *
619 * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
620 * is non-zero and EH wasn't pending before. Fast drain ensures
621 * that EH kicks in in timely manner.
622 *
623 * LOCKING:
624 * spin_lock_irqsave(host lock)
625 */
626static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
627{
628 int cnt;
629
630 /* already scheduled? */
631 if (ap->pflags & ATA_PFLAG_EH_PENDING)
632 return;
633
634 ap->pflags |= ATA_PFLAG_EH_PENDING;
635
636 if (!fastdrain)
637 return;
638
639 /* do we have in-flight qcs? */
640 cnt = ata_eh_nr_in_flight(ap);
641 if (!cnt)
642 return;
643
644 /* activate fast drain */
645 ap->fastdrain_cnt = cnt;
646 ap->fastdrain_timer.expires = jiffies + ATA_EH_FASTDRAIN_INTERVAL;
647 add_timer(&ap->fastdrain_timer);
648}
649
Tejun Heof686bcb2006-05-15 20:58:05 +0900650/**
651 * ata_qc_schedule_eh - schedule qc for error handling
652 * @qc: command to schedule error handling for
653 *
654 * Schedule error handling for @qc. EH will kick in as soon as
655 * other commands are drained.
656 *
657 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400658 * spin_lock_irqsave(host lock)
Tejun Heof686bcb2006-05-15 20:58:05 +0900659 */
660void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
661{
662 struct ata_port *ap = qc->ap;
663
664 WARN_ON(!ap->ops->error_handler);
665
666 qc->flags |= ATA_QCFLAG_FAILED;
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900667 ata_eh_set_pending(ap, 1);
Tejun Heof686bcb2006-05-15 20:58:05 +0900668
669 /* The following will fail if timeout has already expired.
670 * ata_scsi_error() takes care of such scmds on EH entry.
671 * Note that ATA_QCFLAG_FAILED is unconditionally set after
672 * this function completes.
673 */
674 scsi_req_abort_cmd(qc->scsicmd);
675}
676
Tejun Heo7b70fc02006-05-15 20:58:07 +0900677/**
678 * ata_port_schedule_eh - schedule error handling without a qc
679 * @ap: ATA port to schedule EH for
680 *
681 * Schedule error handling for @ap. EH will kick in as soon as
682 * all commands are drained.
683 *
684 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400685 * spin_lock_irqsave(host lock)
Tejun Heo7b70fc02006-05-15 20:58:07 +0900686 */
687void ata_port_schedule_eh(struct ata_port *ap)
688{
689 WARN_ON(!ap->ops->error_handler);
690
Tejun Heof4d6d002007-05-01 11:50:15 +0200691 if (ap->pflags & ATA_PFLAG_INITIALIZING)
692 return;
693
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900694 ata_eh_set_pending(ap, 1);
Jeff Garzikcca39742006-08-24 03:19:22 -0400695 scsi_schedule_eh(ap->scsi_host);
Tejun Heo7b70fc02006-05-15 20:58:07 +0900696
697 DPRINTK("port EH scheduled\n");
698}
699
Tejun Heodbd82612007-08-06 18:36:23 +0900700static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
Tejun Heo7b70fc02006-05-15 20:58:07 +0900701{
702 int tag, nr_aborted = 0;
703
704 WARN_ON(!ap->ops->error_handler);
705
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900706 /* we're gonna abort all commands, no need for fast drain */
707 ata_eh_set_pending(ap, 0);
708
Tejun Heo7b70fc02006-05-15 20:58:07 +0900709 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
710 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
711
Tejun Heodbd82612007-08-06 18:36:23 +0900712 if (qc && (!link || qc->dev->link == link)) {
Tejun Heo7b70fc02006-05-15 20:58:07 +0900713 qc->flags |= ATA_QCFLAG_FAILED;
714 ata_qc_complete(qc);
715 nr_aborted++;
716 }
717 }
718
719 if (!nr_aborted)
720 ata_port_schedule_eh(ap);
721
722 return nr_aborted;
723}
724
Tejun Heoe3180492006-05-15 20:58:09 +0900725/**
Tejun Heodbd82612007-08-06 18:36:23 +0900726 * ata_link_abort - abort all qc's on the link
727 * @link: ATA link to abort qc's for
728 *
729 * Abort all active qc's active on @link and schedule EH.
730 *
731 * LOCKING:
732 * spin_lock_irqsave(host lock)
733 *
734 * RETURNS:
735 * Number of aborted qc's.
736 */
737int ata_link_abort(struct ata_link *link)
738{
739 return ata_do_link_abort(link->ap, link);
740}
741
742/**
743 * ata_port_abort - abort all qc's on the port
744 * @ap: ATA port to abort qc's for
745 *
746 * Abort all active qc's of @ap and schedule EH.
747 *
748 * LOCKING:
749 * spin_lock_irqsave(host_set lock)
750 *
751 * RETURNS:
752 * Number of aborted qc's.
753 */
754int ata_port_abort(struct ata_port *ap)
755{
756 return ata_do_link_abort(ap, NULL);
757}
758
759/**
Tejun Heoe3180492006-05-15 20:58:09 +0900760 * __ata_port_freeze - freeze port
761 * @ap: ATA port to freeze
762 *
763 * This function is called when HSM violation or some other
764 * condition disrupts normal operation of the port. Frozen port
765 * is not allowed to perform any operation until the port is
766 * thawed, which usually follows a successful reset.
767 *
768 * ap->ops->freeze() callback can be used for freezing the port
769 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
770 * port cannot be frozen hardware-wise, the interrupt handler
771 * must ack and clear interrupts unconditionally while the port
772 * is frozen.
773 *
774 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400775 * spin_lock_irqsave(host lock)
Tejun Heoe3180492006-05-15 20:58:09 +0900776 */
777static void __ata_port_freeze(struct ata_port *ap)
778{
779 WARN_ON(!ap->ops->error_handler);
780
781 if (ap->ops->freeze)
782 ap->ops->freeze(ap);
783
Tejun Heob51e9e52006-06-29 01:29:30 +0900784 ap->pflags |= ATA_PFLAG_FROZEN;
Tejun Heoe3180492006-05-15 20:58:09 +0900785
Tejun Heo44877b42007-02-21 01:06:51 +0900786 DPRINTK("ata%u port frozen\n", ap->print_id);
Tejun Heoe3180492006-05-15 20:58:09 +0900787}
788
789/**
790 * ata_port_freeze - abort & freeze port
791 * @ap: ATA port to freeze
792 *
793 * Abort and freeze @ap.
794 *
795 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400796 * spin_lock_irqsave(host lock)
Tejun Heoe3180492006-05-15 20:58:09 +0900797 *
798 * RETURNS:
799 * Number of aborted commands.
800 */
801int ata_port_freeze(struct ata_port *ap)
802{
803 int nr_aborted;
804
805 WARN_ON(!ap->ops->error_handler);
806
807 nr_aborted = ata_port_abort(ap);
808 __ata_port_freeze(ap);
809
810 return nr_aborted;
811}
812
813/**
Tejun Heo7d77b242007-09-23 13:14:13 +0900814 * sata_async_notification - SATA async notification handler
815 * @ap: ATA port where async notification is received
816 *
817 * Handler to be called when async notification via SDB FIS is
818 * received. This function schedules EH if necessary.
819 *
820 * LOCKING:
821 * spin_lock_irqsave(host lock)
822 *
823 * RETURNS:
824 * 1 if EH is scheduled, 0 otherwise.
825 */
826int sata_async_notification(struct ata_port *ap)
827{
828 u32 sntf;
829 int rc;
830
831 if (!(ap->flags & ATA_FLAG_AN))
832 return 0;
833
834 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
835 if (rc == 0)
836 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
837
838 if (!ap->nr_pmp_links || rc) {
839 /* PMP is not attached or SNTF is not available */
840 if (!ap->nr_pmp_links) {
841 /* PMP is not attached. Check whether ATAPI
842 * AN is configured. If so, notify media
843 * change.
844 */
845 struct ata_device *dev = ap->link.device;
846
847 if ((dev->class == ATA_DEV_ATAPI) &&
848 (dev->flags & ATA_DFLAG_AN))
849 ata_scsi_media_change_notify(dev);
850 return 0;
851 } else {
852 /* PMP is attached but SNTF is not available.
853 * ATAPI async media change notification is
854 * not used. The PMP must be reporting PHY
855 * status change, schedule EH.
856 */
857 ata_port_schedule_eh(ap);
858 return 1;
859 }
860 } else {
861 /* PMP is attached and SNTF is available */
862 struct ata_link *link;
863
864 /* check and notify ATAPI AN */
865 ata_port_for_each_link(link, ap) {
866 if (!(sntf & (1 << link->pmp)))
867 continue;
868
869 if ((link->device->class == ATA_DEV_ATAPI) &&
870 (link->device->flags & ATA_DFLAG_AN))
871 ata_scsi_media_change_notify(link->device);
872 }
873
874 /* If PMP is reporting that PHY status of some
875 * downstream ports has changed, schedule EH.
876 */
877 if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
878 ata_port_schedule_eh(ap);
879 return 1;
880 }
881
882 return 0;
883 }
884}
885
886/**
Tejun Heoe3180492006-05-15 20:58:09 +0900887 * ata_eh_freeze_port - EH helper to freeze port
888 * @ap: ATA port to freeze
889 *
890 * Freeze @ap.
891 *
892 * LOCKING:
893 * None.
894 */
895void ata_eh_freeze_port(struct ata_port *ap)
896{
897 unsigned long flags;
898
899 if (!ap->ops->error_handler)
900 return;
901
Jeff Garzikba6a1302006-06-22 23:46:10 -0400902 spin_lock_irqsave(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900903 __ata_port_freeze(ap);
Jeff Garzikba6a1302006-06-22 23:46:10 -0400904 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900905}
906
907/**
908 * ata_port_thaw_port - EH helper to thaw port
909 * @ap: ATA port to thaw
910 *
911 * Thaw frozen port @ap.
912 *
913 * LOCKING:
914 * None.
915 */
916void ata_eh_thaw_port(struct ata_port *ap)
917{
918 unsigned long flags;
919
920 if (!ap->ops->error_handler)
921 return;
922
Jeff Garzikba6a1302006-06-22 23:46:10 -0400923 spin_lock_irqsave(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900924
Tejun Heob51e9e52006-06-29 01:29:30 +0900925 ap->pflags &= ~ATA_PFLAG_FROZEN;
Tejun Heoe3180492006-05-15 20:58:09 +0900926
927 if (ap->ops->thaw)
928 ap->ops->thaw(ap);
929
Jeff Garzikba6a1302006-06-22 23:46:10 -0400930 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +0900931
Tejun Heo44877b42007-02-21 01:06:51 +0900932 DPRINTK("ata%u port thawed\n", ap->print_id);
Tejun Heoe3180492006-05-15 20:58:09 +0900933}
934
Tejun Heoece1d632006-04-02 18:51:53 +0900935static void ata_eh_scsidone(struct scsi_cmnd *scmd)
936{
937 /* nada */
938}
939
940static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
941{
942 struct ata_port *ap = qc->ap;
943 struct scsi_cmnd *scmd = qc->scsicmd;
944 unsigned long flags;
945
Jeff Garzikba6a1302006-06-22 23:46:10 -0400946 spin_lock_irqsave(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900947 qc->scsidone = ata_eh_scsidone;
948 __ata_qc_complete(qc);
949 WARN_ON(ata_tag_valid(qc->tag));
Jeff Garzikba6a1302006-06-22 23:46:10 -0400950 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900951
952 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
953}
954
955/**
956 * ata_eh_qc_complete - Complete an active ATA command from EH
957 * @qc: Command to complete
958 *
959 * Indicate to the mid and upper layers that an ATA command has
960 * completed. To be used from EH.
961 */
962void ata_eh_qc_complete(struct ata_queued_cmd *qc)
963{
964 struct scsi_cmnd *scmd = qc->scsicmd;
965 scmd->retries = scmd->allowed;
966 __ata_eh_qc_complete(qc);
967}
968
969/**
970 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
971 * @qc: Command to retry
972 *
973 * Indicate to the mid and upper layers that an ATA command
974 * should be retried. To be used from EH.
975 *
976 * SCSI midlayer limits the number of retries to scmd->allowed.
977 * scmd->retries is decremented for commands which get retried
978 * due to unrelated failures (qc->err_mask is zero).
979 */
980void ata_eh_qc_retry(struct ata_queued_cmd *qc)
981{
982 struct scsi_cmnd *scmd = qc->scsicmd;
983 if (!qc->err_mask && scmd->retries)
984 scmd->retries--;
985 __ata_eh_qc_complete(qc);
986}
Tejun Heo022bdb02006-05-15 20:58:22 +0900987
988/**
Tejun Heo0ea035a2006-05-31 18:28:01 +0900989 * ata_eh_detach_dev - detach ATA device
990 * @dev: ATA device to detach
991 *
992 * Detach @dev.
993 *
994 * LOCKING:
995 * None.
996 */
Tejun Heofb7fd612007-09-23 13:14:12 +0900997void ata_eh_detach_dev(struct ata_device *dev)
Tejun Heo0ea035a2006-05-31 18:28:01 +0900998{
Tejun Heof58229f2007-08-06 18:36:23 +0900999 struct ata_link *link = dev->link;
1000 struct ata_port *ap = link->ap;
Tejun Heo0ea035a2006-05-31 18:28:01 +09001001 unsigned long flags;
1002
1003 ata_dev_disable(dev);
1004
Jeff Garzikba6a1302006-06-22 23:46:10 -04001005 spin_lock_irqsave(ap->lock, flags);
Tejun Heo0ea035a2006-05-31 18:28:01 +09001006
1007 dev->flags &= ~ATA_DFLAG_DETACH;
1008
1009 if (ata_scsi_offline_dev(dev)) {
1010 dev->flags |= ATA_DFLAG_DETACHED;
Tejun Heob51e9e52006-06-29 01:29:30 +09001011 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
Tejun Heo0ea035a2006-05-31 18:28:01 +09001012 }
1013
Tejun Heobeb07c12006-06-24 20:30:19 +09001014 /* clear per-dev EH actions */
Tejun Heof58229f2007-08-06 18:36:23 +09001015 ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
1016 ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
Tejun Heobeb07c12006-06-24 20:30:19 +09001017
Jeff Garzikba6a1302006-06-22 23:46:10 -04001018 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo0ea035a2006-05-31 18:28:01 +09001019}
1020
1021/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001022 * ata_eh_about_to_do - about to perform eh_action
Tejun Heo955e57d2007-08-06 18:36:23 +09001023 * @link: target ATA link
Tejun Heo47005f22006-06-19 18:27:23 +09001024 * @dev: target ATA dev for per-dev action (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09001025 * @action: action about to be performed
1026 *
1027 * Called just before performing EH actions to clear related bits
Tejun Heo955e57d2007-08-06 18:36:23 +09001028 * in @link->eh_info such that eh actions are not unnecessarily
1029 * repeated.
Tejun Heo022bdb02006-05-15 20:58:22 +09001030 *
1031 * LOCKING:
1032 * None.
1033 */
Tejun Heofb7fd612007-09-23 13:14:12 +09001034void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
1035 unsigned int action)
Tejun Heo022bdb02006-05-15 20:58:22 +09001036{
Tejun Heo955e57d2007-08-06 18:36:23 +09001037 struct ata_port *ap = link->ap;
1038 struct ata_eh_info *ehi = &link->eh_info;
1039 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001040 unsigned long flags;
1041
Jeff Garzikba6a1302006-06-22 23:46:10 -04001042 spin_lock_irqsave(ap->lock, flags);
Tejun Heo1cdaf532006-07-03 16:07:26 +09001043
Tejun Heo13abf502006-07-10 23:18:46 +09001044 /* Reset is represented by combination of actions and EHI
1045 * flags. Suck in all related bits before clearing eh_info to
1046 * avoid losing requested action.
1047 */
1048 if (action & ATA_EH_RESET_MASK) {
1049 ehc->i.action |= ehi->action & ATA_EH_RESET_MASK;
1050 ehc->i.flags |= ehi->flags & ATA_EHI_RESET_MODIFIER_MASK;
Tejun Heo1cdaf532006-07-03 16:07:26 +09001051
Tejun Heo13abf502006-07-10 23:18:46 +09001052 /* make sure all reset actions are cleared & clear EHI flags */
1053 action |= ATA_EH_RESET_MASK;
1054 ehi->flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
1055 }
1056
Tejun Heo955e57d2007-08-06 18:36:23 +09001057 ata_eh_clear_action(link, dev, ehi, action);
Tejun Heo13abf502006-07-10 23:18:46 +09001058
1059 if (!(ehc->i.flags & ATA_EHI_QUIET))
Tejun Heo1cdaf532006-07-03 16:07:26 +09001060 ap->pflags |= ATA_PFLAG_RECOVERED;
1061
Jeff Garzikba6a1302006-06-22 23:46:10 -04001062 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo022bdb02006-05-15 20:58:22 +09001063}
1064
1065/**
Tejun Heo47005f22006-06-19 18:27:23 +09001066 * ata_eh_done - EH action complete
Tejun Heo955e57d2007-08-06 18:36:23 +09001067* @ap: target ATA port
Tejun Heo47005f22006-06-19 18:27:23 +09001068 * @dev: target ATA dev for per-dev action (can be NULL)
1069 * @action: action just completed
1070 *
1071 * Called right after performing EH actions to clear related bits
Tejun Heo955e57d2007-08-06 18:36:23 +09001072 * in @link->eh_context.
Tejun Heo47005f22006-06-19 18:27:23 +09001073 *
1074 * LOCKING:
1075 * None.
1076 */
Tejun Heofb7fd612007-09-23 13:14:12 +09001077void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1078 unsigned int action)
Tejun Heo47005f22006-06-19 18:27:23 +09001079{
Tejun Heo955e57d2007-08-06 18:36:23 +09001080 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001081
Tejun Heo13abf502006-07-10 23:18:46 +09001082 /* if reset is complete, clear all reset actions & reset modifier */
1083 if (action & ATA_EH_RESET_MASK) {
1084 action |= ATA_EH_RESET_MASK;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001085 ehc->i.flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
Tejun Heo13abf502006-07-10 23:18:46 +09001086 }
1087
Tejun Heo955e57d2007-08-06 18:36:23 +09001088 ata_eh_clear_action(link, dev, &ehc->i, action);
Tejun Heo47005f22006-06-19 18:27:23 +09001089}
1090
1091/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001092 * ata_err_string - convert err_mask to descriptive string
1093 * @err_mask: error mask to convert to string
1094 *
1095 * Convert @err_mask to descriptive string. Errors are
1096 * prioritized according to severity and only the most severe
1097 * error is reported.
1098 *
1099 * LOCKING:
1100 * None.
1101 *
1102 * RETURNS:
1103 * Descriptive string for @err_mask
1104 */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001105static const char *ata_err_string(unsigned int err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001106{
1107 if (err_mask & AC_ERR_HOST_BUS)
1108 return "host bus error";
1109 if (err_mask & AC_ERR_ATA_BUS)
1110 return "ATA bus error";
1111 if (err_mask & AC_ERR_TIMEOUT)
1112 return "timeout";
1113 if (err_mask & AC_ERR_HSM)
1114 return "HSM violation";
1115 if (err_mask & AC_ERR_SYSTEM)
1116 return "internal error";
1117 if (err_mask & AC_ERR_MEDIA)
1118 return "media error";
1119 if (err_mask & AC_ERR_INVALID)
1120 return "invalid argument";
1121 if (err_mask & AC_ERR_DEV)
1122 return "device error";
1123 return "unknown error";
1124}
1125
1126/**
Tejun Heoe8ee8452006-05-15 21:03:46 +09001127 * ata_read_log_page - read a specific log page
1128 * @dev: target device
1129 * @page: page to read
1130 * @buf: buffer to store read page
1131 * @sectors: number of sectors to read
1132 *
1133 * Read log page using READ_LOG_EXT command.
1134 *
1135 * LOCKING:
1136 * Kernel thread context (may sleep).
1137 *
1138 * RETURNS:
1139 * 0 on success, AC_ERR_* mask otherwise.
1140 */
1141static unsigned int ata_read_log_page(struct ata_device *dev,
1142 u8 page, void *buf, unsigned int sectors)
1143{
1144 struct ata_taskfile tf;
1145 unsigned int err_mask;
1146
1147 DPRINTK("read log page - page %d\n", page);
1148
1149 ata_tf_init(dev, &tf);
1150 tf.command = ATA_CMD_READ_LOG_EXT;
1151 tf.lbal = page;
1152 tf.nsect = sectors;
1153 tf.hob_nsect = sectors >> 8;
1154 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1155 tf.protocol = ATA_PROT_PIO;
1156
1157 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
Tejun Heo2b789102007-10-09 15:05:44 +09001158 buf, sectors * ATA_SECT_SIZE, 0);
Tejun Heoe8ee8452006-05-15 21:03:46 +09001159
1160 DPRINTK("EXIT, err_mask=%x\n", err_mask);
1161 return err_mask;
1162}
1163
1164/**
1165 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
1166 * @dev: Device to read log page 10h from
1167 * @tag: Resulting tag of the failed command
1168 * @tf: Resulting taskfile registers of the failed command
1169 *
1170 * Read log page 10h to obtain NCQ error details and clear error
1171 * condition.
1172 *
1173 * LOCKING:
1174 * Kernel thread context (may sleep).
1175 *
1176 * RETURNS:
1177 * 0 on success, -errno otherwise.
1178 */
1179static int ata_eh_read_log_10h(struct ata_device *dev,
1180 int *tag, struct ata_taskfile *tf)
1181{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001182 u8 *buf = dev->link->ap->sector_buf;
Tejun Heoe8ee8452006-05-15 21:03:46 +09001183 unsigned int err_mask;
1184 u8 csum;
1185 int i;
1186
1187 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
1188 if (err_mask)
1189 return -EIO;
1190
1191 csum = 0;
1192 for (i = 0; i < ATA_SECT_SIZE; i++)
1193 csum += buf[i];
1194 if (csum)
1195 ata_dev_printk(dev, KERN_WARNING,
1196 "invalid checksum 0x%x on log page 10h\n", csum);
1197
1198 if (buf[0] & 0x80)
1199 return -ENOENT;
1200
1201 *tag = buf[0] & 0x1f;
1202
1203 tf->command = buf[2];
1204 tf->feature = buf[3];
1205 tf->lbal = buf[4];
1206 tf->lbam = buf[5];
1207 tf->lbah = buf[6];
1208 tf->device = buf[7];
1209 tf->hob_lbal = buf[8];
1210 tf->hob_lbam = buf[9];
1211 tf->hob_lbah = buf[10];
1212 tf->nsect = buf[12];
1213 tf->hob_nsect = buf[13];
1214
1215 return 0;
1216}
1217
1218/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001219 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1220 * @dev: device to perform REQUEST_SENSE to
1221 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
1222 *
1223 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
1224 * SENSE. This function is EH helper.
1225 *
1226 * LOCKING:
1227 * Kernel thread context (may sleep).
1228 *
1229 * RETURNS:
1230 * 0 on success, AC_ERR_* mask on failure
1231 */
Albert Lee56287762007-04-02 11:30:46 +08001232static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc)
Tejun Heo022bdb02006-05-15 20:58:22 +09001233{
Albert Lee56287762007-04-02 11:30:46 +08001234 struct ata_device *dev = qc->dev;
1235 unsigned char *sense_buf = qc->scsicmd->sense_buffer;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001236 struct ata_port *ap = dev->link->ap;
Tejun Heo022bdb02006-05-15 20:58:22 +09001237 struct ata_taskfile tf;
1238 u8 cdb[ATAPI_CDB_LEN];
1239
1240 DPRINTK("ATAPI request sense\n");
1241
Tejun Heo022bdb02006-05-15 20:58:22 +09001242 /* FIXME: is this needed? */
1243 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1244
Albert Lee56287762007-04-02 11:30:46 +08001245 /* initialize sense_buf with the error register,
1246 * for the case where they are -not- overwritten
1247 */
Tejun Heo022bdb02006-05-15 20:58:22 +09001248 sense_buf[0] = 0x70;
Albert Lee56287762007-04-02 11:30:46 +08001249 sense_buf[2] = qc->result_tf.feature >> 4;
1250
Jeff Garzika617c092007-05-21 20:14:23 -04001251 /* some devices time out if garbage left in tf */
Albert Lee56287762007-04-02 11:30:46 +08001252 ata_tf_init(dev, &tf);
Tejun Heo022bdb02006-05-15 20:58:22 +09001253
1254 memset(cdb, 0, ATAPI_CDB_LEN);
1255 cdb[0] = REQUEST_SENSE;
1256 cdb[4] = SCSI_SENSE_BUFFERSIZE;
1257
1258 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1259 tf.command = ATA_CMD_PACKET;
1260
1261 /* is it pointless to prefer PIO for "safety reasons"? */
1262 if (ap->flags & ATA_FLAG_PIO_DMA) {
1263 tf.protocol = ATA_PROT_ATAPI_DMA;
1264 tf.feature |= ATAPI_PKT_DMA;
1265 } else {
1266 tf.protocol = ATA_PROT_ATAPI;
1267 tf.lbam = (8 * 1024) & 0xff;
1268 tf.lbah = (8 * 1024) >> 8;
1269 }
1270
1271 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
Tejun Heo2b789102007-10-09 15:05:44 +09001272 sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
Tejun Heo022bdb02006-05-15 20:58:22 +09001273}
1274
1275/**
1276 * ata_eh_analyze_serror - analyze SError for a failed port
Tejun Heo02607312007-08-06 18:36:23 +09001277 * @link: ATA link to analyze SError for
Tejun Heo022bdb02006-05-15 20:58:22 +09001278 *
1279 * Analyze SError if available and further determine cause of
1280 * failure.
1281 *
1282 * LOCKING:
1283 * None.
1284 */
Tejun Heo02607312007-08-06 18:36:23 +09001285static void ata_eh_analyze_serror(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001286{
Tejun Heo02607312007-08-06 18:36:23 +09001287 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001288 u32 serror = ehc->i.serror;
1289 unsigned int err_mask = 0, action = 0;
Tejun Heof9df58c2007-09-23 13:14:13 +09001290 u32 hotplug_mask;
Tejun Heo022bdb02006-05-15 20:58:22 +09001291
1292 if (serror & SERR_PERSISTENT) {
1293 err_mask |= AC_ERR_ATA_BUS;
1294 action |= ATA_EH_HARDRESET;
1295 }
1296 if (serror &
1297 (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
1298 err_mask |= AC_ERR_ATA_BUS;
1299 action |= ATA_EH_SOFTRESET;
1300 }
1301 if (serror & SERR_PROTOCOL) {
1302 err_mask |= AC_ERR_HSM;
1303 action |= ATA_EH_SOFTRESET;
1304 }
1305 if (serror & SERR_INTERNAL) {
1306 err_mask |= AC_ERR_SYSTEM;
Tejun Heo771b8da2007-03-14 01:20:51 +09001307 action |= ATA_EH_HARDRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001308 }
Tejun Heof9df58c2007-09-23 13:14:13 +09001309
1310 /* Determine whether a hotplug event has occurred. Both
1311 * SError.N/X are considered hotplug events for enabled or
1312 * host links. For disabled PMP links, only N bit is
1313 * considered as X bit is left at 1 for link plugging.
1314 */
1315 hotplug_mask = 0;
1316
1317 if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
1318 hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
1319 else
1320 hotplug_mask = SERR_PHYRDY_CHG;
1321
1322 if (serror & hotplug_mask)
Tejun Heo084fe632006-05-31 18:28:03 +09001323 ata_ehi_hotplugged(&ehc->i);
Tejun Heo022bdb02006-05-15 20:58:22 +09001324
1325 ehc->i.err_mask |= err_mask;
1326 ehc->i.action |= action;
1327}
1328
1329/**
Tejun Heoe8ee8452006-05-15 21:03:46 +09001330 * ata_eh_analyze_ncq_error - analyze NCQ error
Tejun Heo02607312007-08-06 18:36:23 +09001331 * @link: ATA link to analyze NCQ error for
Tejun Heoe8ee8452006-05-15 21:03:46 +09001332 *
1333 * Read log page 10h, determine the offending qc and acquire
1334 * error status TF. For NCQ device errors, all LLDDs have to do
1335 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1336 * care of the rest.
1337 *
1338 * LOCKING:
1339 * Kernel thread context (may sleep).
1340 */
Tejun Heo02607312007-08-06 18:36:23 +09001341static void ata_eh_analyze_ncq_error(struct ata_link *link)
Tejun Heoe8ee8452006-05-15 21:03:46 +09001342{
Tejun Heo02607312007-08-06 18:36:23 +09001343 struct ata_port *ap = link->ap;
1344 struct ata_eh_context *ehc = &link->eh_context;
1345 struct ata_device *dev = link->device;
Tejun Heoe8ee8452006-05-15 21:03:46 +09001346 struct ata_queued_cmd *qc;
1347 struct ata_taskfile tf;
1348 int tag, rc;
1349
1350 /* if frozen, we can't do much */
Tejun Heob51e9e52006-06-29 01:29:30 +09001351 if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heoe8ee8452006-05-15 21:03:46 +09001352 return;
1353
1354 /* is it NCQ device error? */
Tejun Heo02607312007-08-06 18:36:23 +09001355 if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
Tejun Heoe8ee8452006-05-15 21:03:46 +09001356 return;
1357
1358 /* has LLDD analyzed already? */
1359 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1360 qc = __ata_qc_from_tag(ap, tag);
1361
1362 if (!(qc->flags & ATA_QCFLAG_FAILED))
1363 continue;
1364
1365 if (qc->err_mask)
1366 return;
1367 }
1368
1369 /* okay, this error is ours */
1370 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1371 if (rc) {
Tejun Heo02607312007-08-06 18:36:23 +09001372 ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
Tejun Heoe8ee8452006-05-15 21:03:46 +09001373 "(errno=%d)\n", rc);
1374 return;
1375 }
1376
Tejun Heo02607312007-08-06 18:36:23 +09001377 if (!(link->sactive & (1 << tag))) {
1378 ata_link_printk(link, KERN_ERR, "log page 10h reported "
Tejun Heoe8ee8452006-05-15 21:03:46 +09001379 "inactive tag %d\n", tag);
1380 return;
1381 }
1382
1383 /* we've got the perpetrator, condemn it */
1384 qc = __ata_qc_from_tag(ap, tag);
1385 memcpy(&qc->result_tf, &tf, sizeof(tf));
Tejun Heo5335b722007-07-16 14:29:40 +09001386 qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
Tejun Heoe8ee8452006-05-15 21:03:46 +09001387 ehc->i.err_mask &= ~AC_ERR_DEV;
1388}
1389
1390/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001391 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1392 * @qc: qc to analyze
1393 * @tf: Taskfile registers to analyze
1394 *
1395 * Analyze taskfile of @qc and further determine cause of
1396 * failure. This function also requests ATAPI sense data if
1397 * avaliable.
1398 *
1399 * LOCKING:
1400 * Kernel thread context (may sleep).
1401 *
1402 * RETURNS:
1403 * Determined recovery action
1404 */
1405static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1406 const struct ata_taskfile *tf)
1407{
1408 unsigned int tmp, action = 0;
1409 u8 stat = tf->command, err = tf->feature;
1410
1411 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1412 qc->err_mask |= AC_ERR_HSM;
1413 return ATA_EH_SOFTRESET;
1414 }
1415
Tejun Heoa51d6442007-03-20 15:24:11 +09001416 if (stat & (ATA_ERR | ATA_DF))
1417 qc->err_mask |= AC_ERR_DEV;
1418 else
Tejun Heo022bdb02006-05-15 20:58:22 +09001419 return 0;
1420
1421 switch (qc->dev->class) {
1422 case ATA_DEV_ATA:
1423 if (err & ATA_ICRC)
1424 qc->err_mask |= AC_ERR_ATA_BUS;
1425 if (err & ATA_UNC)
1426 qc->err_mask |= AC_ERR_MEDIA;
1427 if (err & ATA_IDNF)
1428 qc->err_mask |= AC_ERR_INVALID;
1429 break;
1430
1431 case ATA_DEV_ATAPI:
Tejun Heoa569a302006-11-21 10:40:51 +09001432 if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
Albert Lee56287762007-04-02 11:30:46 +08001433 tmp = atapi_eh_request_sense(qc);
Tejun Heoa569a302006-11-21 10:40:51 +09001434 if (!tmp) {
1435 /* ATA_QCFLAG_SENSE_VALID is used to
1436 * tell atapi_qc_complete() that sense
1437 * data is already valid.
1438 *
1439 * TODO: interpret sense data and set
1440 * appropriate err_mask.
1441 */
1442 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1443 } else
1444 qc->err_mask |= tmp;
1445 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001446 }
1447
1448 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
1449 action |= ATA_EH_SOFTRESET;
1450
1451 return action;
1452}
1453
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001454static int ata_eh_categorize_error(int is_io, unsigned int err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001455{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001456 if (err_mask & AC_ERR_ATA_BUS)
Tejun Heo022bdb02006-05-15 20:58:22 +09001457 return 1;
1458
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001459 if (err_mask & AC_ERR_TIMEOUT)
1460 return 2;
1461
1462 if (is_io) {
1463 if (err_mask & AC_ERR_HSM)
Tejun Heo022bdb02006-05-15 20:58:22 +09001464 return 2;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001465 if ((err_mask &
1466 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
1467 return 3;
Tejun Heo022bdb02006-05-15 20:58:22 +09001468 }
1469
1470 return 0;
1471}
1472
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001473struct speed_down_verdict_arg {
Tejun Heo022bdb02006-05-15 20:58:22 +09001474 u64 since;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001475 int nr_errors[4];
Tejun Heo022bdb02006-05-15 20:58:22 +09001476};
1477
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001478static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
Tejun Heo022bdb02006-05-15 20:58:22 +09001479{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001480 struct speed_down_verdict_arg *arg = void_arg;
1481 int cat = ata_eh_categorize_error(ent->is_io, ent->err_mask);
Tejun Heo022bdb02006-05-15 20:58:22 +09001482
1483 if (ent->timestamp < arg->since)
1484 return -1;
1485
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001486 arg->nr_errors[cat]++;
Tejun Heo022bdb02006-05-15 20:58:22 +09001487 return 0;
1488}
1489
1490/**
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001491 * ata_eh_speed_down_verdict - Determine speed down verdict
Tejun Heo022bdb02006-05-15 20:58:22 +09001492 * @dev: Device of interest
1493 *
1494 * This function examines error ring of @dev and determines
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001495 * whether NCQ needs to be turned off, transfer speed should be
1496 * stepped down, or falling back to PIO is necessary.
Tejun Heo022bdb02006-05-15 20:58:22 +09001497 *
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001498 * Cat-1 is ATA_BUS error for any command.
Tejun Heo022bdb02006-05-15 20:58:22 +09001499 *
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001500 * Cat-2 is TIMEOUT for any command or HSM violation for known
1501 * supported commands.
1502 *
1503 * Cat-3 is is unclassified DEV error for known supported
Tejun Heo022bdb02006-05-15 20:58:22 +09001504 * command.
1505 *
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001506 * NCQ needs to be turned off if there have been more than 3
1507 * Cat-2 + Cat-3 errors during last 10 minutes.
1508 *
1509 * Speed down is necessary if there have been more than 3 Cat-1 +
1510 * Cat-2 errors or 10 Cat-3 errors during last 10 minutes.
1511 *
1512 * Falling back to PIO mode is necessary if there have been more
1513 * than 10 Cat-1 + Cat-2 + Cat-3 errors during last 5 minutes.
1514 *
Tejun Heo022bdb02006-05-15 20:58:22 +09001515 * LOCKING:
1516 * Inherited from caller.
1517 *
1518 * RETURNS:
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001519 * OR of ATA_EH_SPDN_* flags.
Tejun Heo022bdb02006-05-15 20:58:22 +09001520 */
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001521static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
Tejun Heo022bdb02006-05-15 20:58:22 +09001522{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001523 const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
1524 u64 j64 = get_jiffies_64();
1525 struct speed_down_verdict_arg arg;
1526 unsigned int verdict = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09001527
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001528 /* scan past 10 mins of error history */
Tejun Heo022bdb02006-05-15 20:58:22 +09001529 memset(&arg, 0, sizeof(arg));
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001530 arg.since = j64 - min(j64, j10mins);
1531 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
Tejun Heo022bdb02006-05-15 20:58:22 +09001532
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001533 if (arg.nr_errors[2] + arg.nr_errors[3] > 3)
1534 verdict |= ATA_EH_SPDN_NCQ_OFF;
1535 if (arg.nr_errors[1] + arg.nr_errors[2] > 3 || arg.nr_errors[3] > 10)
1536 verdict |= ATA_EH_SPDN_SPEED_DOWN;
Tejun Heo022bdb02006-05-15 20:58:22 +09001537
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001538 /* scan past 3 mins of error history */
1539 memset(&arg, 0, sizeof(arg));
1540 arg.since = j64 - min(j64, j5mins);
1541 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
Tejun Heo022bdb02006-05-15 20:58:22 +09001542
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001543 if (arg.nr_errors[1] + arg.nr_errors[2] + arg.nr_errors[3] > 10)
1544 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
1545
1546 return verdict;
Tejun Heo022bdb02006-05-15 20:58:22 +09001547}
1548
1549/**
1550 * ata_eh_speed_down - record error and speed down if necessary
1551 * @dev: Failed device
1552 * @is_io: Did the device fail during normal IO?
1553 * @err_mask: err_mask of the error
1554 *
1555 * Record error and examine error history to determine whether
1556 * adjusting transmission speed is necessary. It also sets
1557 * transmission limits appropriately if such adjustment is
1558 * necessary.
1559 *
1560 * LOCKING:
1561 * Kernel thread context (may sleep).
1562 *
1563 * RETURNS:
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001564 * Determined recovery action.
Tejun Heo022bdb02006-05-15 20:58:22 +09001565 */
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001566static unsigned int ata_eh_speed_down(struct ata_device *dev, int is_io,
1567 unsigned int err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001568{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001569 unsigned int verdict;
1570 unsigned int action = 0;
1571
1572 /* don't bother if Cat-0 error */
1573 if (ata_eh_categorize_error(is_io, err_mask) == 0)
Tejun Heo022bdb02006-05-15 20:58:22 +09001574 return 0;
1575
1576 /* record error and determine whether speed down is necessary */
1577 ata_ering_record(&dev->ering, is_io, err_mask);
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001578 verdict = ata_eh_speed_down_verdict(dev);
Tejun Heo022bdb02006-05-15 20:58:22 +09001579
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001580 /* turn off NCQ? */
1581 if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
1582 (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
1583 ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
1584 dev->flags |= ATA_DFLAG_NCQ_OFF;
1585 ata_dev_printk(dev, KERN_WARNING,
1586 "NCQ disabled due to excessive errors\n");
1587 goto done;
1588 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001589
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001590 /* speed down? */
1591 if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1592 /* speed down SATA link speed if possible */
Tejun Heo936fd732007-08-06 18:36:23 +09001593 if (sata_down_spd_limit(dev->link) == 0) {
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001594 action |= ATA_EH_HARDRESET;
1595 goto done;
1596 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001597
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001598 /* lower transfer mode */
1599 if (dev->spdn_cnt < 2) {
1600 static const int dma_dnxfer_sel[] =
1601 { ATA_DNXFER_DMA, ATA_DNXFER_40C };
1602 static const int pio_dnxfer_sel[] =
1603 { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
1604 int sel;
Tejun Heo022bdb02006-05-15 20:58:22 +09001605
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001606 if (dev->xfer_shift != ATA_SHIFT_PIO)
1607 sel = dma_dnxfer_sel[dev->spdn_cnt];
1608 else
1609 sel = pio_dnxfer_sel[dev->spdn_cnt];
1610
1611 dev->spdn_cnt++;
1612
1613 if (ata_down_xfermask_limit(dev, sel) == 0) {
1614 action |= ATA_EH_SOFTRESET;
1615 goto done;
1616 }
1617 }
1618 }
1619
1620 /* Fall back to PIO? Slowing down to PIO is meaningless for
1621 * SATA. Consider it only for PATA.
1622 */
1623 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001624 (dev->link->ap->cbl != ATA_CBL_SATA) &&
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001625 (dev->xfer_shift != ATA_SHIFT_PIO)) {
1626 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
1627 dev->spdn_cnt = 0;
1628 action |= ATA_EH_SOFTRESET;
1629 goto done;
1630 }
1631 }
1632
Tejun Heo022bdb02006-05-15 20:58:22 +09001633 return 0;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001634 done:
1635 /* device has been slowed down, blow error history */
1636 ata_ering_clear(&dev->ering);
1637 return action;
Tejun Heo022bdb02006-05-15 20:58:22 +09001638}
1639
1640/**
Tejun Heo9b1e2652007-08-06 18:36:24 +09001641 * ata_eh_link_autopsy - analyze error and determine recovery action
1642 * @link: host link to perform autopsy on
Tejun Heo022bdb02006-05-15 20:58:22 +09001643 *
Tejun Heo02607312007-08-06 18:36:23 +09001644 * Analyze why @link failed and determine which recovery actions
1645 * are needed. This function also sets more detailed AC_ERR_*
1646 * values and fills sense data for ATAPI CHECK SENSE.
Tejun Heo022bdb02006-05-15 20:58:22 +09001647 *
1648 * LOCKING:
1649 * Kernel thread context (may sleep).
1650 */
Tejun Heo9b1e2652007-08-06 18:36:24 +09001651static void ata_eh_link_autopsy(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001652{
Tejun Heo02607312007-08-06 18:36:23 +09001653 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09001654 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heodfcc1732007-10-31 10:17:05 +09001655 struct ata_device *dev;
Tejun Heo022bdb02006-05-15 20:58:22 +09001656 unsigned int all_err_mask = 0;
1657 int tag, is_io = 0;
1658 u32 serror;
1659 int rc;
1660
1661 DPRINTK("ENTER\n");
1662
Tejun Heo1cdaf532006-07-03 16:07:26 +09001663 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
1664 return;
1665
Tejun Heo022bdb02006-05-15 20:58:22 +09001666 /* obtain and analyze SError */
Tejun Heo936fd732007-08-06 18:36:23 +09001667 rc = sata_scr_read(link, SCR_ERROR, &serror);
Tejun Heo022bdb02006-05-15 20:58:22 +09001668 if (rc == 0) {
1669 ehc->i.serror |= serror;
Tejun Heo02607312007-08-06 18:36:23 +09001670 ata_eh_analyze_serror(link);
Tejun Heo4e57c512007-07-16 14:29:41 +09001671 } else if (rc != -EOPNOTSUPP) {
1672 /* SError read failed, force hardreset and probing */
1673 ata_ehi_schedule_probe(&ehc->i);
Tejun Heo4528e4d2006-07-08 20:17:26 +09001674 ehc->i.action |= ATA_EH_HARDRESET;
Tejun Heo4e57c512007-07-16 14:29:41 +09001675 ehc->i.err_mask |= AC_ERR_OTHER;
1676 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001677
Tejun Heoe8ee8452006-05-15 21:03:46 +09001678 /* analyze NCQ failure */
Tejun Heo02607312007-08-06 18:36:23 +09001679 ata_eh_analyze_ncq_error(link);
Tejun Heoe8ee8452006-05-15 21:03:46 +09001680
Tejun Heo022bdb02006-05-15 20:58:22 +09001681 /* any real error trumps AC_ERR_OTHER */
1682 if (ehc->i.err_mask & ~AC_ERR_OTHER)
1683 ehc->i.err_mask &= ~AC_ERR_OTHER;
1684
1685 all_err_mask |= ehc->i.err_mask;
1686
1687 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1688 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1689
Tejun Heo02607312007-08-06 18:36:23 +09001690 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001691 continue;
1692
1693 /* inherit upper level err_mask */
1694 qc->err_mask |= ehc->i.err_mask;
1695
Tejun Heo022bdb02006-05-15 20:58:22 +09001696 /* analyze TF */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001697 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
Tejun Heo022bdb02006-05-15 20:58:22 +09001698
1699 /* DEV errors are probably spurious in case of ATA_BUS error */
1700 if (qc->err_mask & AC_ERR_ATA_BUS)
1701 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
1702 AC_ERR_INVALID);
1703
1704 /* any real error trumps unknown error */
1705 if (qc->err_mask & ~AC_ERR_OTHER)
1706 qc->err_mask &= ~AC_ERR_OTHER;
1707
1708 /* SENSE_VALID trumps dev/unknown error and revalidation */
Tejun Heof90f0822007-10-26 16:12:41 +09001709 if (qc->flags & ATA_QCFLAG_SENSE_VALID)
Tejun Heo022bdb02006-05-15 20:58:22 +09001710 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
Tejun Heo022bdb02006-05-15 20:58:22 +09001711
1712 /* accumulate error info */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001713 ehc->i.dev = qc->dev;
Tejun Heo022bdb02006-05-15 20:58:22 +09001714 all_err_mask |= qc->err_mask;
1715 if (qc->flags & ATA_QCFLAG_IO)
1716 is_io = 1;
1717 }
1718
Tejun Heoa20f33f2006-05-16 12:58:24 +09001719 /* enforce default EH actions */
Tejun Heob51e9e52006-06-29 01:29:30 +09001720 if (ap->pflags & ATA_PFLAG_FROZEN ||
Tejun Heoa20f33f2006-05-16 12:58:24 +09001721 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
Tejun Heo4528e4d2006-07-08 20:17:26 +09001722 ehc->i.action |= ATA_EH_SOFTRESET;
Tejun Heof90f0822007-10-26 16:12:41 +09001723 else if ((is_io && all_err_mask) ||
1724 (!is_io && (all_err_mask & ~AC_ERR_DEV)))
Tejun Heo4528e4d2006-07-08 20:17:26 +09001725 ehc->i.action |= ATA_EH_REVALIDATE;
Tejun Heo022bdb02006-05-15 20:58:22 +09001726
Tejun Heodfcc1732007-10-31 10:17:05 +09001727 /* If we have offending qcs and the associated failed device,
1728 * perform per-dev EH action only on the offending device.
1729 */
Tejun Heo4528e4d2006-07-08 20:17:26 +09001730 if (ehc->i.dev) {
Tejun Heo4528e4d2006-07-08 20:17:26 +09001731 ehc->i.dev_action[ehc->i.dev->devno] |=
1732 ehc->i.action & ATA_EH_PERDEV_MASK;
1733 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
Tejun Heo47005f22006-06-19 18:27:23 +09001734 }
1735
Tejun Heodfcc1732007-10-31 10:17:05 +09001736 /* consider speeding down */
1737 dev = ehc->i.dev;
1738 if (!dev && ata_link_max_devices(link) == 1 &&
1739 ata_dev_enabled(link->device))
1740 dev = link->device;
1741
1742 if (dev)
1743 ehc->i.action |= ata_eh_speed_down(dev, is_io, all_err_mask);
1744
Tejun Heo022bdb02006-05-15 20:58:22 +09001745 DPRINTK("EXIT\n");
1746}
1747
1748/**
Tejun Heo9b1e2652007-08-06 18:36:24 +09001749 * ata_eh_autopsy - analyze error and determine recovery action
1750 * @ap: host port to perform autopsy on
1751 *
1752 * Analyze all links of @ap and determine why they failed and
1753 * which recovery actions are needed.
1754 *
1755 * LOCKING:
1756 * Kernel thread context (may sleep).
1757 */
Tejun Heofb7fd612007-09-23 13:14:12 +09001758void ata_eh_autopsy(struct ata_port *ap)
Tejun Heo9b1e2652007-08-06 18:36:24 +09001759{
1760 struct ata_link *link;
1761
1762 __ata_port_for_each_link(link, ap)
1763 ata_eh_link_autopsy(link);
1764}
1765
1766/**
1767 * ata_eh_link_report - report error handling to user
Tejun Heo02607312007-08-06 18:36:23 +09001768 * @link: ATA link EH is going on
Tejun Heo022bdb02006-05-15 20:58:22 +09001769 *
1770 * Report EH to user.
1771 *
1772 * LOCKING:
1773 * None.
1774 */
Tejun Heo9b1e2652007-08-06 18:36:24 +09001775static void ata_eh_link_report(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001776{
Tejun Heo02607312007-08-06 18:36:23 +09001777 struct ata_port *ap = link->ap;
1778 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001779 const char *frozen, *desc;
Tejun Heoa1e10f72007-08-18 13:28:49 +09001780 char tries_buf[6];
Tejun Heo022bdb02006-05-15 20:58:22 +09001781 int tag, nr_failed = 0;
1782
Tejun Heo94ff3d52007-10-09 14:57:56 +09001783 if (ehc->i.flags & ATA_EHI_QUIET)
1784 return;
1785
Tejun Heo022bdb02006-05-15 20:58:22 +09001786 desc = NULL;
1787 if (ehc->i.desc[0] != '\0')
1788 desc = ehc->i.desc;
1789
1790 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1791 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
1792
Tejun Heoe027bd32007-10-26 16:19:26 +09001793 if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link ||
1794 ((qc->flags & ATA_QCFLAG_QUIET) &&
1795 qc->err_mask == AC_ERR_DEV))
Tejun Heo022bdb02006-05-15 20:58:22 +09001796 continue;
1797 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
1798 continue;
1799
1800 nr_failed++;
1801 }
1802
1803 if (!nr_failed && !ehc->i.err_mask)
1804 return;
1805
1806 frozen = "";
Tejun Heob51e9e52006-06-29 01:29:30 +09001807 if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heo022bdb02006-05-15 20:58:22 +09001808 frozen = " frozen";
1809
Tejun Heoa1e10f72007-08-18 13:28:49 +09001810 memset(tries_buf, 0, sizeof(tries_buf));
1811 if (ap->eh_tries < ATA_EH_MAX_TRIES)
1812 snprintf(tries_buf, sizeof(tries_buf) - 1, " t%d",
1813 ap->eh_tries);
1814
Tejun Heo022bdb02006-05-15 20:58:22 +09001815 if (ehc->i.dev) {
Tejun Heoe8ee8452006-05-15 21:03:46 +09001816 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
Tejun Heoa1e10f72007-08-18 13:28:49 +09001817 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1818 ehc->i.err_mask, link->sactive, ehc->i.serror,
1819 ehc->i.action, frozen, tries_buf);
Tejun Heo022bdb02006-05-15 20:58:22 +09001820 if (desc)
Tejun Heob64bbc32007-07-16 14:29:39 +09001821 ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
Tejun Heo022bdb02006-05-15 20:58:22 +09001822 } else {
Tejun Heo02607312007-08-06 18:36:23 +09001823 ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
Tejun Heoa1e10f72007-08-18 13:28:49 +09001824 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
1825 ehc->i.err_mask, link->sactive, ehc->i.serror,
1826 ehc->i.action, frozen, tries_buf);
Tejun Heo022bdb02006-05-15 20:58:22 +09001827 if (desc)
Tejun Heo02607312007-08-06 18:36:23 +09001828 ata_link_printk(link, KERN_ERR, "%s\n", desc);
Tejun Heo022bdb02006-05-15 20:58:22 +09001829 }
1830
Robert Hancock1333e192007-10-02 11:22:02 -04001831 if (ehc->i.serror)
1832 ata_port_printk(ap, KERN_ERR,
1833 "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
1834 ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
1835 ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
1836 ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
1837 ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
1838 ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
1839 ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
1840 ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
1841 ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
1842 ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
1843 ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
1844 ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
1845 ehc->i.serror & SERR_CRC ? "BadCRC " : "",
1846 ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
1847 ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
1848 ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
1849 ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001850 ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
Robert Hancock1333e192007-10-02 11:22:02 -04001851
Tejun Heo022bdb02006-05-15 20:58:22 +09001852 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1853 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
Tejun Heo8a937582006-11-14 22:36:12 +09001854 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
Tejun Heoabb6a882007-11-28 23:16:09 +09001855 const u8 *cdb = qc->cdb;
1856 char data_buf[20] = "";
1857 char cdb_buf[70] = "";
Tejun Heo022bdb02006-05-15 20:58:22 +09001858
Tejun Heo02607312007-08-06 18:36:23 +09001859 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
1860 qc->dev->link != link || !qc->err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001861 continue;
1862
Tejun Heoabb6a882007-11-28 23:16:09 +09001863 if (qc->dma_dir != DMA_NONE) {
1864 static const char *dma_str[] = {
1865 [DMA_BIDIRECTIONAL] = "bidi",
1866 [DMA_TO_DEVICE] = "out",
1867 [DMA_FROM_DEVICE] = "in",
1868 };
1869 static const char *prot_str[] = {
1870 [ATA_PROT_PIO] = "pio",
1871 [ATA_PROT_DMA] = "dma",
1872 [ATA_PROT_NCQ] = "ncq",
1873 [ATA_PROT_ATAPI] = "pio",
1874 [ATA_PROT_ATAPI_DMA] = "dma",
1875 };
1876
1877 snprintf(data_buf, sizeof(data_buf), " %s %u %s",
1878 prot_str[qc->tf.protocol], qc->nbytes,
1879 dma_str[qc->dma_dir]);
1880 }
1881
1882 if (is_atapi_taskfile(&qc->tf))
1883 snprintf(cdb_buf, sizeof(cdb_buf),
1884 "cdb %02x %02x %02x %02x %02x %02x %02x %02x "
1885 "%02x %02x %02x %02x %02x %02x %02x %02x\n ",
1886 cdb[0], cdb[1], cdb[2], cdb[3],
1887 cdb[4], cdb[5], cdb[6], cdb[7],
1888 cdb[8], cdb[9], cdb[10], cdb[11],
1889 cdb[12], cdb[13], cdb[14], cdb[15]);
1890
Tejun Heo8a937582006-11-14 22:36:12 +09001891 ata_dev_printk(qc->dev, KERN_ERR,
1892 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
Tejun Heoabb6a882007-11-28 23:16:09 +09001893 "tag %d%s\n %s"
Tejun Heo8a937582006-11-14 22:36:12 +09001894 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
Tejun Heo5335b722007-07-16 14:29:40 +09001895 "Emask 0x%x (%s)%s\n",
Tejun Heo8a937582006-11-14 22:36:12 +09001896 cmd->command, cmd->feature, cmd->nsect,
1897 cmd->lbal, cmd->lbam, cmd->lbah,
1898 cmd->hob_feature, cmd->hob_nsect,
1899 cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
Tejun Heoabb6a882007-11-28 23:16:09 +09001900 cmd->device, qc->tag, data_buf, cdb_buf,
Tejun Heo8a937582006-11-14 22:36:12 +09001901 res->command, res->feature, res->nsect,
1902 res->lbal, res->lbam, res->lbah,
1903 res->hob_feature, res->hob_nsect,
1904 res->hob_lbal, res->hob_lbam, res->hob_lbah,
Tejun Heo5335b722007-07-16 14:29:40 +09001905 res->device, qc->err_mask, ata_err_string(qc->err_mask),
1906 qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
Robert Hancock1333e192007-10-02 11:22:02 -04001907
1908 if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001909 ATA_ERR)) {
Robert Hancock1333e192007-10-02 11:22:02 -04001910 if (res->command & ATA_BUSY)
1911 ata_dev_printk(qc->dev, KERN_ERR,
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001912 "status: { Busy }\n");
Robert Hancock1333e192007-10-02 11:22:02 -04001913 else
1914 ata_dev_printk(qc->dev, KERN_ERR,
1915 "status: { %s%s%s%s}\n",
1916 res->command & ATA_DRDY ? "DRDY " : "",
1917 res->command & ATA_DF ? "DF " : "",
1918 res->command & ATA_DRQ ? "DRQ " : "",
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001919 res->command & ATA_ERR ? "ERR " : "");
Robert Hancock1333e192007-10-02 11:22:02 -04001920 }
1921
1922 if (cmd->command != ATA_CMD_PACKET &&
1923 (res->feature & (ATA_ICRC | ATA_UNC | ATA_IDNF |
1924 ATA_ABORTED)))
1925 ata_dev_printk(qc->dev, KERN_ERR,
1926 "error: { %s%s%s%s}\n",
1927 res->feature & ATA_ICRC ? "ICRC " : "",
1928 res->feature & ATA_UNC ? "UNC " : "",
1929 res->feature & ATA_IDNF ? "IDNF " : "",
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001930 res->feature & ATA_ABORTED ? "ABRT " : "");
Tejun Heo022bdb02006-05-15 20:58:22 +09001931 }
1932}
1933
Tejun Heo9b1e2652007-08-06 18:36:24 +09001934/**
1935 * ata_eh_report - report error handling to user
1936 * @ap: ATA port to report EH about
1937 *
1938 * Report EH to user.
1939 *
1940 * LOCKING:
1941 * None.
1942 */
Tejun Heofb7fd612007-09-23 13:14:12 +09001943void ata_eh_report(struct ata_port *ap)
Tejun Heo9b1e2652007-08-06 18:36:24 +09001944{
1945 struct ata_link *link;
1946
1947 __ata_port_for_each_link(link, ap)
1948 ata_eh_link_report(link);
1949}
1950
Tejun Heocc0680a2007-08-06 18:36:23 +09001951static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
Tejun Heod4b2bab2007-02-02 16:50:52 +09001952 unsigned int *classes, unsigned long deadline)
Tejun Heod87fa382006-05-31 18:28:24 +09001953{
Tejun Heof58229f2007-08-06 18:36:23 +09001954 struct ata_device *dev;
1955 int rc;
Tejun Heod87fa382006-05-31 18:28:24 +09001956
Tejun Heocc0680a2007-08-06 18:36:23 +09001957 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09001958 classes[dev->devno] = ATA_DEV_UNKNOWN;
Tejun Heod87fa382006-05-31 18:28:24 +09001959
Tejun Heocc0680a2007-08-06 18:36:23 +09001960 rc = reset(link, classes, deadline);
Tejun Heod87fa382006-05-31 18:28:24 +09001961 if (rc)
1962 return rc;
1963
1964 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
1965 * is complete and convert all ATA_DEV_UNKNOWN to
1966 * ATA_DEV_NONE.
1967 */
Tejun Heocc0680a2007-08-06 18:36:23 +09001968 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09001969 if (classes[dev->devno] != ATA_DEV_UNKNOWN)
Tejun Heod87fa382006-05-31 18:28:24 +09001970 break;
1971
Tejun Heof58229f2007-08-06 18:36:23 +09001972 if (dev) {
Tejun Heocc0680a2007-08-06 18:36:23 +09001973 ata_link_for_each_dev(dev, link) {
Tejun Heof58229f2007-08-06 18:36:23 +09001974 if (classes[dev->devno] == ATA_DEV_UNKNOWN)
1975 classes[dev->devno] = ATA_DEV_NONE;
1976 }
1977 }
Tejun Heod87fa382006-05-31 18:28:24 +09001978
1979 return 0;
1980}
1981
Tejun Heoae791c02007-09-23 13:14:12 +09001982static int ata_eh_followup_srst_needed(struct ata_link *link,
1983 int rc, int classify,
Tejun Heo664faf02006-05-31 18:27:50 +09001984 const unsigned int *classes)
1985{
Tejun Heoae791c02007-09-23 13:14:12 +09001986 if (link->flags & ATA_LFLAG_NO_SRST)
1987 return 0;
Tejun Heo664faf02006-05-31 18:27:50 +09001988 if (rc == -EAGAIN)
1989 return 1;
1990 if (rc != 0)
1991 return 0;
Tejun Heo3495de72007-09-23 13:19:53 +09001992 if ((link->ap->flags & ATA_FLAG_PMP) && ata_is_host_link(link))
1993 return 1;
Tejun Heoae791c02007-09-23 13:14:12 +09001994 if (classify && !(link->flags & ATA_LFLAG_ASSUME_CLASS) &&
1995 classes[0] == ATA_DEV_UNKNOWN)
Tejun Heo664faf02006-05-31 18:27:50 +09001996 return 1;
1997 return 0;
1998}
1999
Tejun Heofb7fd612007-09-23 13:14:12 +09002000int ata_eh_reset(struct ata_link *link, int classify,
2001 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
2002 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
Tejun Heo022bdb02006-05-15 20:58:22 +09002003{
Tejun Heo416dc9e2007-10-31 10:17:03 +09002004 const int max_tries = ARRAY_SIZE(ata_eh_reset_timeouts);
Tejun Heoafaa5c32007-10-09 15:06:10 +09002005 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09002006 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo664faf02006-05-31 18:27:50 +09002007 unsigned int *classes = ehc->classes;
Tejun Heo416dc9e2007-10-31 10:17:03 +09002008 unsigned int lflags = link->flags;
Tejun Heo1cdaf532006-07-03 16:07:26 +09002009 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
Tejun Heo31daabd2007-02-02 16:50:52 +09002010 int try = 0;
Tejun Heof58229f2007-08-06 18:36:23 +09002011 struct ata_device *dev;
Tejun Heo416dc9e2007-10-31 10:17:03 +09002012 unsigned long deadline, now;
Tejun Heo0e06d9c2007-10-24 15:21:26 +09002013 unsigned int tmp_action;
Tejun Heo022bdb02006-05-15 20:58:22 +09002014 ata_reset_fn_t reset;
Tejun Heoafaa5c32007-10-09 15:06:10 +09002015 unsigned long flags;
Tejun Heo416dc9e2007-10-31 10:17:03 +09002016 u32 sstatus;
Tejun Heof58229f2007-08-06 18:36:23 +09002017 int rc;
Tejun Heo022bdb02006-05-15 20:58:22 +09002018
Tejun Heo13abf502006-07-10 23:18:46 +09002019 /* about to reset */
Tejun Heoafaa5c32007-10-09 15:06:10 +09002020 spin_lock_irqsave(ap->lock, flags);
2021 ap->pflags |= ATA_PFLAG_RESETTING;
2022 spin_unlock_irqrestore(ap->lock, flags);
2023
Tejun Heo955e57d2007-08-06 18:36:23 +09002024 ata_eh_about_to_do(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
Tejun Heo13abf502006-07-10 23:18:46 +09002025
Tejun Heocdeab112007-10-29 16:41:09 +09002026 ata_link_for_each_dev(dev, link) {
2027 /* If we issue an SRST then an ATA drive (not ATAPI)
2028 * may change configuration and be in PIO0 timing. If
2029 * we do a hard reset (or are coming from power on)
2030 * this is true for ATA or ATAPI. Until we've set a
2031 * suitable controller mode we should not touch the
2032 * bus as we may be talking too fast.
2033 */
2034 dev->pio_mode = XFER_PIO_0;
2035
2036 /* If the controller has a pio mode setup function
2037 * then use it to set the chipset to rights. Don't
2038 * touch the DMA setup as that will be dealt with when
2039 * configuring devices.
2040 */
2041 if (ap->ops->set_piomode)
2042 ap->ops->set_piomode(ap, dev);
2043 }
2044
Tejun Heof5914a42006-05-31 18:27:48 +09002045 /* Determine which reset to use and record in ehc->i.action.
2046 * prereset() may examine and modify it.
2047 */
Tejun Heo416dc9e2007-10-31 10:17:03 +09002048 if (softreset && (!hardreset || (!(lflags & ATA_LFLAG_NO_SRST) &&
Tejun Heoae791c02007-09-23 13:14:12 +09002049 !sata_set_spd_needed(link) &&
Tejun Heo0e06d9c2007-10-24 15:21:26 +09002050 !(ehc->i.action & ATA_EH_HARDRESET))))
2051 tmp_action = ATA_EH_SOFTRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09002052 else
Tejun Heo0e06d9c2007-10-24 15:21:26 +09002053 tmp_action = ATA_EH_HARDRESET;
2054
2055 ehc->i.action = (ehc->i.action & ~ATA_EH_RESET_MASK) | tmp_action;
Tejun Heof5914a42006-05-31 18:27:48 +09002056
2057 if (prereset) {
Tejun Heocc0680a2007-08-06 18:36:23 +09002058 rc = prereset(link, jiffies + ATA_EH_PRERESET_TIMEOUT);
Tejun Heof5914a42006-05-31 18:27:48 +09002059 if (rc) {
Alan Coxc9619222006-09-26 17:53:38 +01002060 if (rc == -ENOENT) {
Tejun Heocc0680a2007-08-06 18:36:23 +09002061 ata_link_printk(link, KERN_DEBUG,
Tejun Heo4aa9ab62007-03-12 17:24:08 +09002062 "port disabled. ignoring.\n");
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002063 ehc->i.action &= ~ATA_EH_RESET_MASK;
Tejun Heo4aa9ab62007-03-12 17:24:08 +09002064
Tejun Heo936fd732007-08-06 18:36:23 +09002065 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09002066 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heo4aa9ab62007-03-12 17:24:08 +09002067
2068 rc = 0;
Alan Coxc9619222006-09-26 17:53:38 +01002069 } else
Tejun Heocc0680a2007-08-06 18:36:23 +09002070 ata_link_printk(link, KERN_ERR,
Tejun Heof5914a42006-05-31 18:27:48 +09002071 "prereset failed (errno=%d)\n", rc);
Tejun Heofccb6ea2007-07-16 14:29:41 +09002072 goto out;
Tejun Heof5914a42006-05-31 18:27:48 +09002073 }
2074 }
2075
2076 /* prereset() might have modified ehc->i.action */
2077 if (ehc->i.action & ATA_EH_HARDRESET)
Tejun Heo022bdb02006-05-15 20:58:22 +09002078 reset = hardreset;
Tejun Heof5914a42006-05-31 18:27:48 +09002079 else if (ehc->i.action & ATA_EH_SOFTRESET)
2080 reset = softreset;
2081 else {
2082 /* prereset told us not to reset, bang classes and return */
Tejun Heo936fd732007-08-06 18:36:23 +09002083 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09002084 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heofccb6ea2007-07-16 14:29:41 +09002085 rc = 0;
2086 goto out;
Tejun Heof5914a42006-05-31 18:27:48 +09002087 }
2088
2089 /* did prereset() screw up? if so, fix up to avoid oopsing */
2090 if (!reset) {
Tejun Heof5914a42006-05-31 18:27:48 +09002091 if (softreset)
2092 reset = softreset;
2093 else
2094 reset = hardreset;
2095 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002096
2097 retry:
Tejun Heo31daabd2007-02-02 16:50:52 +09002098 deadline = jiffies + ata_eh_reset_timeouts[try++];
2099
Tejun Heo3e706392006-05-31 18:28:11 +09002100 /* shut up during boot probing */
2101 if (verbose)
Tejun Heocc0680a2007-08-06 18:36:23 +09002102 ata_link_printk(link, KERN_INFO, "%s resetting link\n",
Tejun Heo3e706392006-05-31 18:28:11 +09002103 reset == softreset ? "soft" : "hard");
Tejun Heo022bdb02006-05-15 20:58:22 +09002104
Tejun Heo13abf502006-07-10 23:18:46 +09002105 /* mark that this EH session started with reset */
Tejun Heo0d64a232007-04-23 02:41:05 +09002106 if (reset == hardreset)
2107 ehc->i.flags |= ATA_EHI_DID_HARDRESET;
2108 else
2109 ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09002110
Tejun Heocc0680a2007-08-06 18:36:23 +09002111 rc = ata_do_reset(link, reset, classes, deadline);
Tejun Heo022bdb02006-05-15 20:58:22 +09002112
Tejun Heo664faf02006-05-31 18:27:50 +09002113 if (reset == hardreset &&
Tejun Heoae791c02007-09-23 13:14:12 +09002114 ata_eh_followup_srst_needed(link, rc, classify, classes)) {
Tejun Heo664faf02006-05-31 18:27:50 +09002115 /* okay, let's do follow-up softreset */
Tejun Heo664faf02006-05-31 18:27:50 +09002116 reset = softreset;
2117
2118 if (!reset) {
Tejun Heocc0680a2007-08-06 18:36:23 +09002119 ata_link_printk(link, KERN_ERR,
Tejun Heo664faf02006-05-31 18:27:50 +09002120 "follow-up softreset required "
2121 "but no softreset avaliable\n");
Tejun Heofccb6ea2007-07-16 14:29:41 +09002122 rc = -EINVAL;
Tejun Heo08cf69d2007-10-31 10:17:04 +09002123 goto fail;
Tejun Heo664faf02006-05-31 18:27:50 +09002124 }
2125
Tejun Heo955e57d2007-08-06 18:36:23 +09002126 ata_eh_about_to_do(link, NULL, ATA_EH_RESET_MASK);
Tejun Heocc0680a2007-08-06 18:36:23 +09002127 rc = ata_do_reset(link, reset, classes, deadline);
Tejun Heo664faf02006-05-31 18:27:50 +09002128 }
2129
Tejun Heo416dc9e2007-10-31 10:17:03 +09002130 /* -EAGAIN can happen if we skipped followup SRST */
2131 if (rc && rc != -EAGAIN)
2132 goto fail;
Tejun Heoae791c02007-09-23 13:14:12 +09002133
Tejun Heo08cf69d2007-10-31 10:17:04 +09002134 /* was classification successful? */
2135 if (classify && classes[0] == ATA_DEV_UNKNOWN &&
2136 !(lflags & ATA_LFLAG_ASSUME_CLASS)) {
2137 if (try < max_tries) {
2138 ata_link_printk(link, KERN_WARNING,
2139 "classification failed\n");
2140 rc = -EINVAL;
2141 goto fail;
2142 }
2143
2144 ata_link_printk(link, KERN_WARNING,
2145 "classfication failed, assuming ATA\n");
2146 lflags |= ATA_LFLAG_ASSUME_ATA;
2147 }
2148
Tejun Heo416dc9e2007-10-31 10:17:03 +09002149 ata_link_for_each_dev(dev, link) {
2150 /* After the reset, the device state is PIO 0 and the
2151 * controller state is undefined. Reset also wakes up
2152 * drives from sleeping mode.
2153 */
2154 dev->pio_mode = XFER_PIO_0;
2155 dev->flags &= ~ATA_DFLAG_SLEEPING;
Tejun Heo664faf02006-05-31 18:27:50 +09002156
Tejun Heo416dc9e2007-10-31 10:17:03 +09002157 if (ata_link_offline(link))
2158 continue;
Tejun Heo664faf02006-05-31 18:27:50 +09002159
Tejun Heo416dc9e2007-10-31 10:17:03 +09002160 /* apply class override and convert UNKNOWN to NONE */
2161 if (lflags & ATA_LFLAG_ASSUME_ATA)
2162 classes[dev->devno] = ATA_DEV_ATA;
2163 else if (lflags & ATA_LFLAG_ASSUME_SEMB)
2164 classes[dev->devno] = ATA_DEV_SEMB_UNSUP; /* not yet */
2165 else if (classes[dev->devno] == ATA_DEV_UNKNOWN)
2166 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heo022bdb02006-05-15 20:58:22 +09002167 }
2168
Tejun Heo416dc9e2007-10-31 10:17:03 +09002169 /* record current link speed */
2170 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2171 link->sata_spd = (sstatus >> 4) & 0xf;
Tejun Heo008a7892007-07-16 14:29:40 +09002172
Tejun Heo416dc9e2007-10-31 10:17:03 +09002173 if (postreset)
2174 postreset(link, classes);
Tejun Heo20952b62006-05-31 18:27:23 +09002175
Tejun Heo416dc9e2007-10-31 10:17:03 +09002176 /* reset successful, schedule revalidation */
2177 ata_eh_done(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
2178 ehc->i.action |= ATA_EH_REVALIDATE;
Tejun Heoae791c02007-09-23 13:14:12 +09002179
Tejun Heo416dc9e2007-10-31 10:17:03 +09002180 rc = 0;
Tejun Heofccb6ea2007-07-16 14:29:41 +09002181 out:
2182 /* clear hotplug flag */
2183 ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
Tejun Heoafaa5c32007-10-09 15:06:10 +09002184
2185 spin_lock_irqsave(ap->lock, flags);
2186 ap->pflags &= ~ATA_PFLAG_RESETTING;
2187 spin_unlock_irqrestore(ap->lock, flags);
2188
Tejun Heo022bdb02006-05-15 20:58:22 +09002189 return rc;
Tejun Heo416dc9e2007-10-31 10:17:03 +09002190
2191 fail:
2192 if (rc == -ERESTART || try >= max_tries)
2193 goto out;
2194
2195 now = jiffies;
2196 if (time_before(now, deadline)) {
2197 unsigned long delta = deadline - now;
2198
2199 ata_link_printk(link, KERN_WARNING, "reset failed "
2200 "(errno=%d), retrying in %u secs\n",
2201 rc, (jiffies_to_msecs(delta) + 999) / 1000);
2202
2203 while (delta)
2204 delta = schedule_timeout_uninterruptible(delta);
2205 }
2206
2207 if (rc == -EPIPE || try == max_tries - 1)
2208 sata_down_spd_limit(link);
2209 if (hardreset)
2210 reset = hardreset;
2211 goto retry;
Tejun Heo022bdb02006-05-15 20:58:22 +09002212}
2213
Tejun Heo02607312007-08-06 18:36:23 +09002214static int ata_eh_revalidate_and_attach(struct ata_link *link,
Tejun Heo084fe632006-05-31 18:28:03 +09002215 struct ata_device **r_failed_dev)
Tejun Heo022bdb02006-05-15 20:58:22 +09002216{
Tejun Heo02607312007-08-06 18:36:23 +09002217 struct ata_port *ap = link->ap;
2218 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09002219 struct ata_device *dev;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002220 unsigned int new_mask = 0;
Tejun Heo084fe632006-05-31 18:28:03 +09002221 unsigned long flags;
Tejun Heof58229f2007-08-06 18:36:23 +09002222 int rc = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09002223
2224 DPRINTK("ENTER\n");
2225
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002226 /* For PATA drive side cable detection to work, IDENTIFY must
2227 * be done backwards such that PDIAG- is released by the slave
2228 * device before the master device is identified.
2229 */
Tejun Heo02607312007-08-06 18:36:23 +09002230 ata_link_for_each_dev_reverse(dev, link) {
Tejun Heof58229f2007-08-06 18:36:23 +09002231 unsigned int action = ata_eh_dev_action(dev);
2232 unsigned int readid_flags = 0;
Tejun Heo47005f22006-06-19 18:27:23 +09002233
Tejun Heobff04642006-11-10 18:08:10 +09002234 if (ehc->i.flags & ATA_EHI_DID_RESET)
2235 readid_flags |= ATA_READID_POSTRESET;
2236
Tejun Heo9666f402007-05-04 21:27:47 +02002237 if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
Tejun Heo633273a2007-09-23 13:19:54 +09002238 WARN_ON(dev->class == ATA_DEV_PMP);
2239
Tejun Heo02607312007-08-06 18:36:23 +09002240 if (ata_link_offline(link)) {
Tejun Heo022bdb02006-05-15 20:58:22 +09002241 rc = -EIO;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002242 goto err;
Tejun Heo022bdb02006-05-15 20:58:22 +09002243 }
2244
Tejun Heo02607312007-08-06 18:36:23 +09002245 ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
Tejun Heo422c9da2007-09-23 13:14:12 +09002246 rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
2247 readid_flags);
Tejun Heo022bdb02006-05-15 20:58:22 +09002248 if (rc)
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002249 goto err;
Tejun Heo022bdb02006-05-15 20:58:22 +09002250
Tejun Heo02607312007-08-06 18:36:23 +09002251 ata_eh_done(link, dev, ATA_EH_REVALIDATE);
Tejun Heo47005f22006-06-19 18:27:23 +09002252
Tejun Heobaa1e782006-11-01 18:39:27 +09002253 /* Configuration may have changed, reconfigure
2254 * transfer mode.
2255 */
2256 ehc->i.flags |= ATA_EHI_SETMODE;
2257
zhao, forrest3057ac32006-06-12 12:01:34 +08002258 /* schedule the scsi_rescan_device() here */
2259 queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
Tejun Heo084fe632006-05-31 18:28:03 +09002260 } else if (dev->class == ATA_DEV_UNKNOWN &&
2261 ehc->tries[dev->devno] &&
2262 ata_class_enabled(ehc->classes[dev->devno])) {
2263 dev->class = ehc->classes[dev->devno];
2264
Tejun Heo633273a2007-09-23 13:19:54 +09002265 if (dev->class == ATA_DEV_PMP)
2266 rc = sata_pmp_attach(dev);
2267 else
2268 rc = ata_dev_read_id(dev, &dev->class,
2269 readid_flags, dev->id);
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002270 switch (rc) {
2271 case 0:
Tejun Heof58229f2007-08-06 18:36:23 +09002272 new_mask |= 1 << dev->devno;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002273 break;
2274 case -ENOENT:
Tejun Heo55a8e2c2006-11-10 18:08:10 +09002275 /* IDENTIFY was issued to non-existent
2276 * device. No need to reset. Just
2277 * thaw and kill the device.
2278 */
2279 ata_eh_thaw_port(ap);
2280 dev->class = ATA_DEV_UNKNOWN;
Tejun Heo084fe632006-05-31 18:28:03 +09002281 break;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002282 default:
2283 dev->class = ATA_DEV_UNKNOWN;
2284 goto err;
Tejun Heo55a8e2c2006-11-10 18:08:10 +09002285 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002286 }
2287 }
2288
Tejun Heoc1c4e8d2007-04-23 02:05:53 +09002289 /* PDIAG- should have been released, ask cable type if post-reset */
Tejun Heo9b1e2652007-08-06 18:36:24 +09002290 if (ata_is_host_link(link) && ap->ops->cable_detect &&
2291 (ehc->i.flags & ATA_EHI_DID_RESET))
Tejun Heoc1c4e8d2007-04-23 02:05:53 +09002292 ap->cbl = ap->ops->cable_detect(ap);
2293
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002294 /* Configure new devices forward such that user doesn't see
2295 * device detection messages backwards.
2296 */
Tejun Heo02607312007-08-06 18:36:23 +09002297 ata_link_for_each_dev(dev, link) {
Tejun Heo633273a2007-09-23 13:19:54 +09002298 if (!(new_mask & (1 << dev->devno)) ||
2299 dev->class == ATA_DEV_PMP)
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002300 continue;
2301
2302 ehc->i.flags |= ATA_EHI_PRINTINFO;
2303 rc = ata_dev_configure(dev);
2304 ehc->i.flags &= ~ATA_EHI_PRINTINFO;
2305 if (rc)
2306 goto err;
2307
2308 spin_lock_irqsave(ap->lock, flags);
2309 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
2310 spin_unlock_irqrestore(ap->lock, flags);
2311
2312 /* new device discovered, configure xfermode */
2313 ehc->i.flags |= ATA_EHI_SETMODE;
2314 }
2315
2316 return 0;
2317
2318 err:
2319 *r_failed_dev = dev;
2320 DPRINTK("EXIT rc=%d\n", rc);
Tejun Heo022bdb02006-05-15 20:58:22 +09002321 return rc;
2322}
2323
Tejun Heo02607312007-08-06 18:36:23 +09002324static int ata_link_nr_enabled(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09002325{
Tejun Heof58229f2007-08-06 18:36:23 +09002326 struct ata_device *dev;
2327 int cnt = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09002328
Tejun Heo02607312007-08-06 18:36:23 +09002329 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09002330 if (ata_dev_enabled(dev))
Tejun Heo022bdb02006-05-15 20:58:22 +09002331 cnt++;
2332 return cnt;
2333}
2334
Tejun Heo02607312007-08-06 18:36:23 +09002335static int ata_link_nr_vacant(struct ata_link *link)
Tejun Heo084fe632006-05-31 18:28:03 +09002336{
Tejun Heof58229f2007-08-06 18:36:23 +09002337 struct ata_device *dev;
2338 int cnt = 0;
Tejun Heo084fe632006-05-31 18:28:03 +09002339
Tejun Heo02607312007-08-06 18:36:23 +09002340 ata_link_for_each_dev(dev, link)
Tejun Heof58229f2007-08-06 18:36:23 +09002341 if (dev->class == ATA_DEV_UNKNOWN)
Tejun Heo084fe632006-05-31 18:28:03 +09002342 cnt++;
2343 return cnt;
2344}
2345
Tejun Heo02607312007-08-06 18:36:23 +09002346static int ata_eh_skip_recovery(struct ata_link *link)
Tejun Heo084fe632006-05-31 18:28:03 +09002347{
Tejun Heo02607312007-08-06 18:36:23 +09002348 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heof58229f2007-08-06 18:36:23 +09002349 struct ata_device *dev;
Tejun Heo084fe632006-05-31 18:28:03 +09002350
Tejun Heof9df58c2007-09-23 13:14:13 +09002351 /* skip disabled links */
2352 if (link->flags & ATA_LFLAG_DISABLED)
2353 return 1;
2354
Tejun Heo7c8c2cf2006-07-10 23:18:23 +09002355 /* thaw frozen port, resume link and recover failed devices */
Tejun Heo02607312007-08-06 18:36:23 +09002356 if ((link->ap->pflags & ATA_PFLAG_FROZEN) ||
2357 (ehc->i.flags & ATA_EHI_RESUME_LINK) || ata_link_nr_enabled(link))
Tejun Heo084fe632006-05-31 18:28:03 +09002358 return 0;
2359
2360 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
Tejun Heo02607312007-08-06 18:36:23 +09002361 ata_link_for_each_dev(dev, link) {
Tejun Heo084fe632006-05-31 18:28:03 +09002362 if (dev->class == ATA_DEV_UNKNOWN &&
2363 ehc->classes[dev->devno] != ATA_DEV_NONE)
2364 return 0;
2365 }
2366
2367 return 1;
2368}
2369
Tejun Heo9b1e2652007-08-06 18:36:24 +09002370static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
Tejun Heofee7ca72007-07-01 19:05:58 +09002371{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002372 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heofee7ca72007-07-01 19:05:58 +09002373
2374 ehc->tries[dev->devno]--;
2375
2376 switch (err) {
2377 case -ENODEV:
2378 /* device missing or wrong IDENTIFY data, schedule probing */
2379 ehc->i.probe_mask |= (1 << dev->devno);
2380 case -EINVAL:
2381 /* give it just one more chance */
2382 ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
2383 case -EIO:
Tejun Heo4fb46152007-10-29 16:45:05 +09002384 if (ehc->tries[dev->devno] == 1 && dev->pio_mode > XFER_PIO_0) {
Tejun Heofee7ca72007-07-01 19:05:58 +09002385 /* This is the last chance, better to slow
2386 * down than lose it.
2387 */
Tejun Heo936fd732007-08-06 18:36:23 +09002388 sata_down_spd_limit(dev->link);
Tejun Heofee7ca72007-07-01 19:05:58 +09002389 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2390 }
2391 }
2392
2393 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
2394 /* disable device if it has used up all its chances */
2395 ata_dev_disable(dev);
2396
2397 /* detach if offline */
Tejun Heo936fd732007-08-06 18:36:23 +09002398 if (ata_link_offline(dev->link))
Tejun Heofee7ca72007-07-01 19:05:58 +09002399 ata_eh_detach_dev(dev);
2400
2401 /* probe if requested */
2402 if ((ehc->i.probe_mask & (1 << dev->devno)) &&
2403 !(ehc->did_probe_mask & (1 << dev->devno))) {
2404 ata_eh_detach_dev(dev);
2405 ata_dev_init(dev);
2406
2407 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
2408 ehc->did_probe_mask |= (1 << dev->devno);
2409 ehc->i.action |= ATA_EH_SOFTRESET;
2410 }
Tejun Heo9b1e2652007-08-06 18:36:24 +09002411
2412 return 1;
Tejun Heofee7ca72007-07-01 19:05:58 +09002413 } else {
2414 /* soft didn't work? be haaaaard */
2415 if (ehc->i.flags & ATA_EHI_DID_RESET)
2416 ehc->i.action |= ATA_EH_HARDRESET;
2417 else
2418 ehc->i.action |= ATA_EH_SOFTRESET;
Tejun Heo9b1e2652007-08-06 18:36:24 +09002419
2420 return 0;
Tejun Heofee7ca72007-07-01 19:05:58 +09002421 }
2422}
2423
Tejun Heo022bdb02006-05-15 20:58:22 +09002424/**
2425 * ata_eh_recover - recover host port after error
2426 * @ap: host port to recover
Tejun Heof5914a42006-05-31 18:27:48 +09002427 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09002428 * @softreset: softreset method (can be NULL)
2429 * @hardreset: hardreset method (can be NULL)
2430 * @postreset: postreset method (can be NULL)
Tejun Heo9b1e2652007-08-06 18:36:24 +09002431 * @r_failed_link: out parameter for failed link
Tejun Heo022bdb02006-05-15 20:58:22 +09002432 *
2433 * This is the alpha and omega, eum and yang, heart and soul of
2434 * libata exception handling. On entry, actions required to
Tejun Heo9b1e2652007-08-06 18:36:24 +09002435 * recover each link and hotplug requests are recorded in the
2436 * link's eh_context. This function executes all the operations
2437 * with appropriate retrials and fallbacks to resurrect failed
Tejun Heo084fe632006-05-31 18:28:03 +09002438 * devices, detach goners and greet newcomers.
Tejun Heo022bdb02006-05-15 20:58:22 +09002439 *
2440 * LOCKING:
2441 * Kernel thread context (may sleep).
2442 *
2443 * RETURNS:
2444 * 0 on success, -errno on failure.
2445 */
Tejun Heofb7fd612007-09-23 13:14:12 +09002446int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
2447 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2448 ata_postreset_fn_t postreset,
2449 struct ata_link **r_failed_link)
Tejun Heo022bdb02006-05-15 20:58:22 +09002450{
Tejun Heo9b1e2652007-08-06 18:36:24 +09002451 struct ata_link *link;
Tejun Heo022bdb02006-05-15 20:58:22 +09002452 struct ata_device *dev;
Tejun Heo9b1e2652007-08-06 18:36:24 +09002453 int nr_failed_devs, nr_disabled_devs;
2454 int reset, rc;
Tejun Heof9df58c2007-09-23 13:14:13 +09002455 unsigned long flags;
Tejun Heo022bdb02006-05-15 20:58:22 +09002456
2457 DPRINTK("ENTER\n");
2458
2459 /* prep for recovery */
Tejun Heo9b1e2652007-08-06 18:36:24 +09002460 ata_port_for_each_link(link, ap) {
2461 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo084fe632006-05-31 18:28:03 +09002462
Tejun Heof9df58c2007-09-23 13:14:13 +09002463 /* re-enable link? */
2464 if (ehc->i.action & ATA_EH_ENABLE_LINK) {
2465 ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
2466 spin_lock_irqsave(ap->lock, flags);
2467 link->flags &= ~ATA_LFLAG_DISABLED;
2468 spin_unlock_irqrestore(ap->lock, flags);
2469 ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
2470 }
2471
Tejun Heo9b1e2652007-08-06 18:36:24 +09002472 ata_link_for_each_dev(dev, link) {
Tejun Heofd995f72007-09-23 13:14:12 +09002473 if (link->flags & ATA_LFLAG_NO_RETRY)
2474 ehc->tries[dev->devno] = 1;
2475 else
2476 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
Tejun Heo79a55b72007-01-18 17:22:18 +09002477
Tejun Heo9b1e2652007-08-06 18:36:24 +09002478 /* collect port action mask recorded in dev actions */
2479 ehc->i.action |= ehc->i.dev_action[dev->devno] &
2480 ~ATA_EH_PERDEV_MASK;
2481 ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
Tejun Heo084fe632006-05-31 18:28:03 +09002482
Tejun Heo9b1e2652007-08-06 18:36:24 +09002483 /* process hotplug request */
2484 if (dev->flags & ATA_DFLAG_DETACH)
2485 ata_eh_detach_dev(dev);
2486
2487 if (!ata_dev_enabled(dev) &&
2488 ((ehc->i.probe_mask & (1 << dev->devno)) &&
2489 !(ehc->did_probe_mask & (1 << dev->devno)))) {
2490 ata_eh_detach_dev(dev);
2491 ata_dev_init(dev);
2492 ehc->did_probe_mask |= (1 << dev->devno);
2493 ehc->i.action |= ATA_EH_SOFTRESET;
2494 }
Tejun Heo084fe632006-05-31 18:28:03 +09002495 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002496 }
2497
2498 retry:
Tejun Heo022bdb02006-05-15 20:58:22 +09002499 rc = 0;
Tejun Heo9b1e2652007-08-06 18:36:24 +09002500 nr_failed_devs = 0;
2501 nr_disabled_devs = 0;
2502 reset = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09002503
Tejun Heoaeb2ecd2006-06-12 14:11:43 +09002504 /* if UNLOADING, finish immediately */
Tejun Heob51e9e52006-06-29 01:29:30 +09002505 if (ap->pflags & ATA_PFLAG_UNLOADING)
Tejun Heoaeb2ecd2006-06-12 14:11:43 +09002506 goto out;
2507
Tejun Heo9b1e2652007-08-06 18:36:24 +09002508 /* prep for EH */
2509 ata_port_for_each_link(link, ap) {
2510 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09002511
Tejun Heo9b1e2652007-08-06 18:36:24 +09002512 /* skip EH if possible. */
2513 if (ata_eh_skip_recovery(link))
2514 ehc->i.action = 0;
2515
2516 /* do we need to reset? */
2517 if (ehc->i.action & ATA_EH_RESET_MASK)
2518 reset = 1;
2519
2520 ata_link_for_each_dev(dev, link)
2521 ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
2522 }
Tejun Heo084fe632006-05-31 18:28:03 +09002523
Tejun Heo022bdb02006-05-15 20:58:22 +09002524 /* reset */
Tejun Heo9b1e2652007-08-06 18:36:24 +09002525 if (reset) {
Tejun Heob06ce3e2007-10-09 15:06:48 +09002526 /* if PMP is attached, this function only deals with
2527 * downstream links, port should stay thawed.
2528 */
2529 if (!ap->nr_pmp_links)
2530 ata_eh_freeze_port(ap);
Tejun Heo022bdb02006-05-15 20:58:22 +09002531
Tejun Heo9b1e2652007-08-06 18:36:24 +09002532 ata_port_for_each_link(link, ap) {
2533 struct ata_eh_context *ehc = &link->eh_context;
2534
2535 if (!(ehc->i.action & ATA_EH_RESET_MASK))
2536 continue;
2537
2538 rc = ata_eh_reset(link, ata_link_nr_vacant(link),
2539 prereset, softreset, hardreset,
2540 postreset);
2541 if (rc) {
2542 ata_link_printk(link, KERN_ERR,
2543 "reset failed, giving up\n");
2544 goto out;
2545 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002546 }
2547
Tejun Heob06ce3e2007-10-09 15:06:48 +09002548 if (!ap->nr_pmp_links)
2549 ata_eh_thaw_port(ap);
Tejun Heo022bdb02006-05-15 20:58:22 +09002550 }
2551
Tejun Heo9b1e2652007-08-06 18:36:24 +09002552 /* the rest */
2553 ata_port_for_each_link(link, ap) {
2554 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09002555
Tejun Heo9b1e2652007-08-06 18:36:24 +09002556 /* revalidate existing devices and attach new ones */
2557 rc = ata_eh_revalidate_and_attach(link, &dev);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002558 if (rc)
Tejun Heo022bdb02006-05-15 20:58:22 +09002559 goto dev_fail;
Tejun Heo9b1e2652007-08-06 18:36:24 +09002560
Tejun Heo633273a2007-09-23 13:19:54 +09002561 /* if PMP got attached, return, pmp EH will take care of it */
2562 if (link->device->class == ATA_DEV_PMP) {
2563 ehc->i.action = 0;
2564 return 0;
2565 }
2566
Tejun Heo9b1e2652007-08-06 18:36:24 +09002567 /* configure transfer mode if necessary */
2568 if (ehc->i.flags & ATA_EHI_SETMODE) {
2569 rc = ata_set_mode(link, &dev);
2570 if (rc)
2571 goto dev_fail;
2572 ehc->i.flags &= ~ATA_EHI_SETMODE;
2573 }
2574
Kristen Carlson Accardica773292007-10-25 00:58:59 -04002575 if (ehc->i.action & ATA_EHI_LPM)
2576 ata_link_for_each_dev(dev, link)
2577 ata_dev_enable_pm(dev, ap->pm_policy);
2578
Tejun Heo9b1e2652007-08-06 18:36:24 +09002579 /* this link is okay now */
2580 ehc->i.flags = 0;
2581 continue;
2582
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002583dev_fail:
Tejun Heo9b1e2652007-08-06 18:36:24 +09002584 nr_failed_devs++;
2585 if (ata_eh_handle_dev_fail(dev, rc))
2586 nr_disabled_devs++;
2587
Tejun Heob06ce3e2007-10-09 15:06:48 +09002588 if (ap->pflags & ATA_PFLAG_FROZEN) {
2589 /* PMP reset requires working host port.
2590 * Can't retry if it's frozen.
2591 */
2592 if (ap->nr_pmp_links)
2593 goto out;
Tejun Heo9b1e2652007-08-06 18:36:24 +09002594 break;
Tejun Heob06ce3e2007-10-09 15:06:48 +09002595 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002596 }
2597
Tejun Heo9b1e2652007-08-06 18:36:24 +09002598 if (nr_failed_devs) {
2599 if (nr_failed_devs != nr_disabled_devs) {
2600 ata_port_printk(ap, KERN_WARNING, "failed to recover "
2601 "some devices, retrying in 5 secs\n");
2602 ssleep(5);
2603 } else {
2604 /* no device left to recover, repeat fast */
2605 msleep(500);
2606 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002607
Tejun Heo9b1e2652007-08-06 18:36:24 +09002608 goto retry;
Tejun Heo022bdb02006-05-15 20:58:22 +09002609 }
2610
Tejun Heo022bdb02006-05-15 20:58:22 +09002611 out:
Tejun Heo9b1e2652007-08-06 18:36:24 +09002612 if (rc && r_failed_link)
2613 *r_failed_link = link;
Tejun Heo022bdb02006-05-15 20:58:22 +09002614
2615 DPRINTK("EXIT, rc=%d\n", rc);
2616 return rc;
2617}
2618
2619/**
2620 * ata_eh_finish - finish up EH
2621 * @ap: host port to finish EH for
2622 *
2623 * Recovery is complete. Clean up EH states and retry or finish
2624 * failed qcs.
2625 *
2626 * LOCKING:
2627 * None.
2628 */
Tejun Heofb7fd612007-09-23 13:14:12 +09002629void ata_eh_finish(struct ata_port *ap)
Tejun Heo022bdb02006-05-15 20:58:22 +09002630{
2631 int tag;
2632
2633 /* retry or finish qcs */
2634 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2635 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2636
2637 if (!(qc->flags & ATA_QCFLAG_FAILED))
2638 continue;
2639
2640 if (qc->err_mask) {
2641 /* FIXME: Once EH migration is complete,
2642 * generate sense data in this function,
2643 * considering both err_mask and tf.
Tejun Heof90f0822007-10-26 16:12:41 +09002644 *
2645 * There's no point in retrying invalid
2646 * (detected by libata) and non-IO device
2647 * errors (rejected by device). Finish them
2648 * immediately.
Tejun Heo022bdb02006-05-15 20:58:22 +09002649 */
Tejun Heof90f0822007-10-26 16:12:41 +09002650 if ((qc->err_mask & AC_ERR_INVALID) ||
2651 (!(qc->flags & ATA_QCFLAG_IO) &&
2652 qc->err_mask == AC_ERR_DEV))
Tejun Heo022bdb02006-05-15 20:58:22 +09002653 ata_eh_qc_complete(qc);
2654 else
2655 ata_eh_qc_retry(qc);
2656 } else {
2657 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
2658 ata_eh_qc_complete(qc);
2659 } else {
2660 /* feed zero TF to sense generation */
2661 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
2662 ata_eh_qc_retry(qc);
2663 }
2664 }
2665 }
Tejun Heoda917d62007-09-23 13:14:12 +09002666
2667 /* make sure nr_active_links is zero after EH */
2668 WARN_ON(ap->nr_active_links);
2669 ap->nr_active_links = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09002670}
2671
2672/**
2673 * ata_do_eh - do standard error handling
2674 * @ap: host port to handle error for
Tejun Heof5914a42006-05-31 18:27:48 +09002675 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09002676 * @softreset: softreset method (can be NULL)
2677 * @hardreset: hardreset method (can be NULL)
2678 * @postreset: postreset method (can be NULL)
2679 *
2680 * Perform standard error handling sequence.
2681 *
2682 * LOCKING:
2683 * Kernel thread context (may sleep).
2684 */
Tejun Heof5914a42006-05-31 18:27:48 +09002685void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
2686 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2687 ata_postreset_fn_t postreset)
Tejun Heo022bdb02006-05-15 20:58:22 +09002688{
Tejun Heo9b1e2652007-08-06 18:36:24 +09002689 struct ata_device *dev;
2690 int rc;
2691
2692 ata_eh_autopsy(ap);
2693 ata_eh_report(ap);
2694
2695 rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
2696 NULL);
2697 if (rc) {
2698 ata_link_for_each_dev(dev, &ap->link)
2699 ata_dev_disable(dev);
2700 }
2701
Tejun Heo022bdb02006-05-15 20:58:22 +09002702 ata_eh_finish(ap);
2703}
Tejun Heo500530f2006-07-03 16:07:27 +09002704
Tejun Heo6ffa01d2007-03-02 17:32:47 +09002705#ifdef CONFIG_PM
Tejun Heo500530f2006-07-03 16:07:27 +09002706/**
2707 * ata_eh_handle_port_suspend - perform port suspend operation
2708 * @ap: port to suspend
2709 *
2710 * Suspend @ap.
2711 *
2712 * LOCKING:
2713 * Kernel thread context (may sleep).
2714 */
2715static void ata_eh_handle_port_suspend(struct ata_port *ap)
2716{
2717 unsigned long flags;
2718 int rc = 0;
2719
2720 /* are we suspending? */
2721 spin_lock_irqsave(ap->lock, flags);
2722 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2723 ap->pm_mesg.event == PM_EVENT_ON) {
2724 spin_unlock_irqrestore(ap->lock, flags);
2725 return;
2726 }
2727 spin_unlock_irqrestore(ap->lock, flags);
2728
2729 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
2730
Tejun Heo64578a3de2007-05-15 03:28:16 +09002731 /* tell ACPI we're suspending */
2732 rc = ata_acpi_on_suspend(ap);
2733 if (rc)
2734 goto out;
2735
Tejun Heo500530f2006-07-03 16:07:27 +09002736 /* suspend */
2737 ata_eh_freeze_port(ap);
2738
2739 if (ap->ops->port_suspend)
2740 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
2741
Tejun Heo64578a3de2007-05-15 03:28:16 +09002742 out:
Tejun Heo500530f2006-07-03 16:07:27 +09002743 /* report result */
2744 spin_lock_irqsave(ap->lock, flags);
2745
2746 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
2747 if (rc == 0)
2748 ap->pflags |= ATA_PFLAG_SUSPENDED;
Tejun Heo64578a3de2007-05-15 03:28:16 +09002749 else if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heo500530f2006-07-03 16:07:27 +09002750 ata_port_schedule_eh(ap);
2751
2752 if (ap->pm_result) {
2753 *ap->pm_result = rc;
2754 ap->pm_result = NULL;
2755 }
2756
2757 spin_unlock_irqrestore(ap->lock, flags);
2758
2759 return;
2760}
2761
2762/**
2763 * ata_eh_handle_port_resume - perform port resume operation
2764 * @ap: port to resume
2765 *
2766 * Resume @ap.
2767 *
Tejun Heo500530f2006-07-03 16:07:27 +09002768 * LOCKING:
2769 * Kernel thread context (may sleep).
2770 */
2771static void ata_eh_handle_port_resume(struct ata_port *ap)
2772{
Tejun Heo500530f2006-07-03 16:07:27 +09002773 unsigned long flags;
Tejun Heo9666f402007-05-04 21:27:47 +02002774 int rc = 0;
Tejun Heo500530f2006-07-03 16:07:27 +09002775
2776 /* are we resuming? */
2777 spin_lock_irqsave(ap->lock, flags);
2778 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
2779 ap->pm_mesg.event != PM_EVENT_ON) {
2780 spin_unlock_irqrestore(ap->lock, flags);
2781 return;
2782 }
2783 spin_unlock_irqrestore(ap->lock, flags);
2784
Tejun Heo9666f402007-05-04 21:27:47 +02002785 WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
Tejun Heo500530f2006-07-03 16:07:27 +09002786
2787 if (ap->ops->port_resume)
2788 rc = ap->ops->port_resume(ap);
2789
Tejun Heo67465442007-05-15 03:28:16 +09002790 /* tell ACPI that we're resuming */
2791 ata_acpi_on_resume(ap);
2792
Tejun Heo9666f402007-05-04 21:27:47 +02002793 /* report result */
Tejun Heo500530f2006-07-03 16:07:27 +09002794 spin_lock_irqsave(ap->lock, flags);
2795 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
2796 if (ap->pm_result) {
2797 *ap->pm_result = rc;
2798 ap->pm_result = NULL;
2799 }
2800 spin_unlock_irqrestore(ap->lock, flags);
2801}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09002802#endif /* CONFIG_PM */