blob: 073b88156b3cb4e04f9d06dfebc3032804918ca9 [file] [log] [blame]
Tejun Heoece1d632006-04-02 18:51:53 +09001/*
2 * libata-eh.c - libata error handling
3 *
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
7 *
8 * Copyright 2006 Tejun Heo <htejun@gmail.com>
9 *
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; see the file COPYING. If not, write to
23 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
24 * USA.
25 *
26 *
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
29 *
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
32 *
33 */
34
Tejun Heoece1d632006-04-02 18:51:53 +090035#include <linux/kernel.h>
Jens Axboe242f9dc2008-09-14 05:55:09 -070036#include <linux/blkdev.h>
Jeff Garzik28555682007-10-11 17:12:35 -040037#include <linux/pci.h>
Tejun Heoece1d632006-04-02 18:51:53 +090038#include <scsi/scsi.h>
39#include <scsi/scsi_host.h>
40#include <scsi/scsi_eh.h>
41#include <scsi/scsi_device.h>
42#include <scsi/scsi_cmnd.h>
Robert Hancock65211482009-07-14 20:43:39 -060043#include <scsi/scsi_dbg.h>
Jeff Garzikc6fd2802006-08-10 07:31:37 -040044#include "../scsi/scsi_transport_api.h"
Tejun Heoece1d632006-04-02 18:51:53 +090045
46#include <linux/libata.h>
47
48#include "libata.h"
49
Tejun Heo7d47e8d2007-02-02 16:22:31 +090050enum {
Tejun Heo3884f7b2007-11-27 19:28:56 +090051 /* speed down verdicts */
Tejun Heo7d47e8d2007-02-02 16:22:31 +090052 ATA_EH_SPDN_NCQ_OFF = (1 << 0),
53 ATA_EH_SPDN_SPEED_DOWN = (1 << 1),
54 ATA_EH_SPDN_FALLBACK_TO_PIO = (1 << 2),
Tejun Heo76326ac2007-11-27 19:28:59 +090055 ATA_EH_SPDN_KEEP_ERRORS = (1 << 3),
Tejun Heo3884f7b2007-11-27 19:28:56 +090056
57 /* error flags */
58 ATA_EFLAG_IS_IO = (1 << 0),
Tejun Heo76326ac2007-11-27 19:28:59 +090059 ATA_EFLAG_DUBIOUS_XFER = (1 << 1),
Gwendal Grignoud9027472010-05-25 12:31:38 -070060 ATA_EFLAG_OLD_ER = (1 << 31),
Tejun Heo3884f7b2007-11-27 19:28:56 +090061
62 /* error categories */
63 ATA_ECAT_NONE = 0,
64 ATA_ECAT_ATA_BUS = 1,
65 ATA_ECAT_TOUT_HSM = 2,
66 ATA_ECAT_UNK_DEV = 3,
Tejun Heo75f9caf2008-01-03 01:21:14 +090067 ATA_ECAT_DUBIOUS_NONE = 4,
68 ATA_ECAT_DUBIOUS_ATA_BUS = 5,
69 ATA_ECAT_DUBIOUS_TOUT_HSM = 6,
70 ATA_ECAT_DUBIOUS_UNK_DEV = 7,
71 ATA_ECAT_NR = 8,
Tejun Heo7d47e8d2007-02-02 16:22:31 +090072
Tejun Heo87fbc5a2008-05-20 02:17:54 +090073 ATA_EH_CMD_DFL_TIMEOUT = 5000,
74
Tejun Heo0a2c0f52008-05-20 02:17:52 +090075 /* always put at least this amount of time between resets */
76 ATA_EH_RESET_COOL_DOWN = 5000,
77
Tejun Heo341c2c92008-05-20 02:17:51 +090078 /* Waiting in ->prereset can never be reliable. It's
79 * sometimes nice to wait there but it can't be depended upon;
80 * otherwise, we wouldn't be resetting. Just give it enough
81 * time for most drives to spin up.
82 */
83 ATA_EH_PRERESET_TIMEOUT = 10000,
84 ATA_EH_FASTDRAIN_INTERVAL = 3000,
Tejun Heo11fc33d2008-08-30 14:20:01 +020085
86 ATA_EH_UA_TRIES = 5,
Tejun Heoc2c7a892009-01-29 20:31:34 +090087
88 /* probe speed down parameters, see ata_eh_schedule_probe() */
89 ATA_EH_PROBE_TRIAL_INTERVAL = 60000, /* 1 min */
90 ATA_EH_PROBE_TRIALS = 2,
Tejun Heo31daabd2007-02-02 16:50:52 +090091};
92
93/* The following table determines how we sequence resets. Each entry
94 * represents timeout for that try. The first try can be soft or
95 * hardreset. All others are hardreset if available. In most cases
96 * the first reset w/ 10sec timeout should succeed. Following entries
97 * are mostly for error handling, hotplug and retarded devices.
98 */
99static const unsigned long ata_eh_reset_timeouts[] = {
Tejun Heo341c2c92008-05-20 02:17:51 +0900100 10000, /* most drives spin up by 10sec */
101 10000, /* > 99% working drives spin up before 20sec */
102 35000, /* give > 30 secs of idleness for retarded devices */
103 5000, /* and sweet one last chance */
Tejun Heod8af0eb2008-05-20 02:17:53 +0900104 ULONG_MAX, /* > 1 min has elapsed, give up */
Tejun Heo31daabd2007-02-02 16:50:52 +0900105};
106
Tejun Heo87fbc5a2008-05-20 02:17:54 +0900107static const unsigned long ata_eh_identify_timeouts[] = {
108 5000, /* covers > 99% of successes and not too boring on failures */
109 10000, /* combined time till here is enough even for media access */
110 30000, /* for true idiots */
111 ULONG_MAX,
112};
113
Tejun Heo6013efd2009-11-19 15:36:45 +0900114static const unsigned long ata_eh_flush_timeouts[] = {
115 15000, /* be generous with flush */
116 15000, /* ditto */
117 30000, /* and even more generous */
118 ULONG_MAX,
119};
120
Tejun Heo87fbc5a2008-05-20 02:17:54 +0900121static const unsigned long ata_eh_other_timeouts[] = {
122 5000, /* same rationale as identify timeout */
123 10000, /* ditto */
124 /* but no merciful 30sec for other commands, it just isn't worth it */
125 ULONG_MAX,
126};
127
128struct ata_eh_cmd_timeout_ent {
129 const u8 *commands;
130 const unsigned long *timeouts;
131};
132
133/* The following table determines timeouts to use for EH internal
134 * commands. Each table entry is a command class and matches the
135 * commands the entry applies to and the timeout table to use.
136 *
137 * On the retry after a command timed out, the next timeout value from
138 * the table is used. If the table doesn't contain further entries,
139 * the last value is used.
140 *
141 * ehc->cmd_timeout_idx keeps track of which timeout to use per
142 * command class, so if SET_FEATURES times out on the first try, the
143 * next try will use the second timeout value only for that class.
144 */
145#define CMDS(cmds...) (const u8 []){ cmds, 0 }
146static const struct ata_eh_cmd_timeout_ent
147ata_eh_cmd_timeout_table[ATA_EH_CMD_TIMEOUT_TABLE_SIZE] = {
148 { .commands = CMDS(ATA_CMD_ID_ATA, ATA_CMD_ID_ATAPI),
149 .timeouts = ata_eh_identify_timeouts, },
150 { .commands = CMDS(ATA_CMD_READ_NATIVE_MAX, ATA_CMD_READ_NATIVE_MAX_EXT),
151 .timeouts = ata_eh_other_timeouts, },
152 { .commands = CMDS(ATA_CMD_SET_MAX, ATA_CMD_SET_MAX_EXT),
153 .timeouts = ata_eh_other_timeouts, },
154 { .commands = CMDS(ATA_CMD_SET_FEATURES),
155 .timeouts = ata_eh_other_timeouts, },
156 { .commands = CMDS(ATA_CMD_INIT_DEV_PARAMS),
157 .timeouts = ata_eh_other_timeouts, },
Tejun Heo6013efd2009-11-19 15:36:45 +0900158 { .commands = CMDS(ATA_CMD_FLUSH, ATA_CMD_FLUSH_EXT),
159 .timeouts = ata_eh_flush_timeouts },
Tejun Heo87fbc5a2008-05-20 02:17:54 +0900160};
161#undef CMDS
162
Tejun Heoad9e2762006-05-15 20:58:12 +0900163static void __ata_port_freeze(struct ata_port *ap);
Tejun Heo6ffa01d2007-03-02 17:32:47 +0900164#ifdef CONFIG_PM
Tejun Heo500530f2006-07-03 16:07:27 +0900165static void ata_eh_handle_port_suspend(struct ata_port *ap);
166static void ata_eh_handle_port_resume(struct ata_port *ap);
Tejun Heo6ffa01d2007-03-02 17:32:47 +0900167#else /* CONFIG_PM */
168static void ata_eh_handle_port_suspend(struct ata_port *ap)
169{ }
170
171static void ata_eh_handle_port_resume(struct ata_port *ap)
172{ }
Tejun Heo6ffa01d2007-03-02 17:32:47 +0900173#endif /* CONFIG_PM */
Tejun Heoad9e2762006-05-15 20:58:12 +0900174
Tejun Heob64bbc32007-07-16 14:29:39 +0900175static void __ata_ehi_pushv_desc(struct ata_eh_info *ehi, const char *fmt,
176 va_list args)
177{
178 ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
179 ATA_EH_DESC_LEN - ehi->desc_len,
180 fmt, args);
181}
182
183/**
184 * __ata_ehi_push_desc - push error description without adding separator
185 * @ehi: target EHI
186 * @fmt: printf format string
187 *
188 * Format string according to @fmt and append it to @ehi->desc.
189 *
190 * LOCKING:
191 * spin_lock_irqsave(host lock)
192 */
193void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
194{
195 va_list args;
196
197 va_start(args, fmt);
198 __ata_ehi_pushv_desc(ehi, fmt, args);
199 va_end(args);
200}
201
202/**
203 * ata_ehi_push_desc - push error description with separator
204 * @ehi: target EHI
205 * @fmt: printf format string
206 *
207 * Format string according to @fmt and append it to @ehi->desc.
208 * If @ehi->desc is not empty, ", " is added in-between.
209 *
210 * LOCKING:
211 * spin_lock_irqsave(host lock)
212 */
213void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
214{
215 va_list args;
216
217 if (ehi->desc_len)
218 __ata_ehi_push_desc(ehi, ", ");
219
220 va_start(args, fmt);
221 __ata_ehi_pushv_desc(ehi, fmt, args);
222 va_end(args);
223}
224
225/**
226 * ata_ehi_clear_desc - clean error description
227 * @ehi: target EHI
228 *
229 * Clear @ehi->desc.
230 *
231 * LOCKING:
232 * spin_lock_irqsave(host lock)
233 */
234void ata_ehi_clear_desc(struct ata_eh_info *ehi)
235{
236 ehi->desc[0] = '\0';
237 ehi->desc_len = 0;
238}
239
Tejun Heocbcdd872007-08-18 13:14:55 +0900240/**
241 * ata_port_desc - append port description
242 * @ap: target ATA port
243 * @fmt: printf format string
244 *
245 * Format string according to @fmt and append it to port
246 * description. If port description is not empty, " " is added
247 * in-between. This function is to be used while initializing
248 * ata_host. The description is printed on host registration.
249 *
250 * LOCKING:
251 * None.
252 */
253void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
254{
255 va_list args;
256
257 WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
258
259 if (ap->link.eh_info.desc_len)
260 __ata_ehi_push_desc(&ap->link.eh_info, " ");
261
262 va_start(args, fmt);
263 __ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
264 va_end(args);
265}
266
267#ifdef CONFIG_PCI
268
269/**
270 * ata_port_pbar_desc - append PCI BAR description
271 * @ap: target ATA port
272 * @bar: target PCI BAR
273 * @offset: offset into PCI BAR
274 * @name: name of the area
275 *
276 * If @offset is negative, this function formats a string which
277 * contains the name, address, size and type of the BAR and
278 * appends it to the port description. If @offset is zero or
279 * positive, only name and offsetted address is appended.
280 *
281 * LOCKING:
282 * None.
283 */
284void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
285 const char *name)
286{
287 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
288 char *type = "";
289 unsigned long long start, len;
290
291 if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
292 type = "m";
293 else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
294 type = "i";
295
296 start = (unsigned long long)pci_resource_start(pdev, bar);
297 len = (unsigned long long)pci_resource_len(pdev, bar);
298
299 if (offset < 0)
300 ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
301 else
Andrew Mortone6a73ab2007-12-13 16:01:37 -0800302 ata_port_desc(ap, "%s 0x%llx", name,
303 start + (unsigned long long)offset);
Tejun Heocbcdd872007-08-18 13:14:55 +0900304}
305
306#endif /* CONFIG_PCI */
307
Tejun Heo87fbc5a2008-05-20 02:17:54 +0900308static int ata_lookup_timeout_table(u8 cmd)
309{
310 int i;
311
312 for (i = 0; i < ATA_EH_CMD_TIMEOUT_TABLE_SIZE; i++) {
313 const u8 *cur;
314
315 for (cur = ata_eh_cmd_timeout_table[i].commands; *cur; cur++)
316 if (*cur == cmd)
317 return i;
318 }
319
320 return -1;
321}
322
323/**
324 * ata_internal_cmd_timeout - determine timeout for an internal command
325 * @dev: target device
326 * @cmd: internal command to be issued
327 *
328 * Determine timeout for internal command @cmd for @dev.
329 *
330 * LOCKING:
331 * EH context.
332 *
333 * RETURNS:
334 * Determined timeout.
335 */
336unsigned long ata_internal_cmd_timeout(struct ata_device *dev, u8 cmd)
337{
338 struct ata_eh_context *ehc = &dev->link->eh_context;
339 int ent = ata_lookup_timeout_table(cmd);
340 int idx;
341
342 if (ent < 0)
343 return ATA_EH_CMD_DFL_TIMEOUT;
344
345 idx = ehc->cmd_timeout_idx[dev->devno][ent];
346 return ata_eh_cmd_timeout_table[ent].timeouts[idx];
347}
348
349/**
350 * ata_internal_cmd_timed_out - notification for internal command timeout
351 * @dev: target device
352 * @cmd: internal command which timed out
353 *
354 * Notify EH that internal command @cmd for @dev timed out. This
355 * function should be called only for commands whose timeouts are
356 * determined using ata_internal_cmd_timeout().
357 *
358 * LOCKING:
359 * EH context.
360 */
361void ata_internal_cmd_timed_out(struct ata_device *dev, u8 cmd)
362{
363 struct ata_eh_context *ehc = &dev->link->eh_context;
364 int ent = ata_lookup_timeout_table(cmd);
365 int idx;
366
367 if (ent < 0)
368 return;
369
370 idx = ehc->cmd_timeout_idx[dev->devno][ent];
371 if (ata_eh_cmd_timeout_table[ent].timeouts[idx + 1] != ULONG_MAX)
372 ehc->cmd_timeout_idx[dev->devno][ent]++;
373}
374
Tejun Heo3884f7b2007-11-27 19:28:56 +0900375static void ata_ering_record(struct ata_ering *ering, unsigned int eflags,
Tejun Heo0c247c52006-05-15 20:58:19 +0900376 unsigned int err_mask)
377{
378 struct ata_ering_entry *ent;
379
380 WARN_ON(!err_mask);
381
382 ering->cursor++;
383 ering->cursor %= ATA_ERING_SIZE;
384
385 ent = &ering->ring[ering->cursor];
Tejun Heo3884f7b2007-11-27 19:28:56 +0900386 ent->eflags = eflags;
Tejun Heo0c247c52006-05-15 20:58:19 +0900387 ent->err_mask = err_mask;
388 ent->timestamp = get_jiffies_64();
389}
390
Tejun Heo76326ac2007-11-27 19:28:59 +0900391static struct ata_ering_entry *ata_ering_top(struct ata_ering *ering)
392{
393 struct ata_ering_entry *ent = &ering->ring[ering->cursor];
394
395 if (ent->err_mask)
396 return ent;
397 return NULL;
398}
399
Gwendal Grignoud9027472010-05-25 12:31:38 -0700400int ata_ering_map(struct ata_ering *ering,
401 int (*map_fn)(struct ata_ering_entry *, void *),
402 void *arg)
Tejun Heo0c247c52006-05-15 20:58:19 +0900403{
404 int idx, rc = 0;
405 struct ata_ering_entry *ent;
406
407 idx = ering->cursor;
408 do {
409 ent = &ering->ring[idx];
410 if (!ent->err_mask)
411 break;
412 rc = map_fn(ent, arg);
413 if (rc)
414 break;
415 idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
416 } while (idx != ering->cursor);
417
418 return rc;
419}
420
Gwendal Grignoud9027472010-05-25 12:31:38 -0700421int ata_ering_clear_cb(struct ata_ering_entry *ent, void *void_arg)
422{
423 ent->eflags |= ATA_EFLAG_OLD_ER;
424 return 0;
425}
426
427static void ata_ering_clear(struct ata_ering *ering)
428{
429 ata_ering_map(ering, ata_ering_clear_cb, NULL);
430}
431
Tejun Heo64f65ca2006-06-24 20:30:18 +0900432static unsigned int ata_eh_dev_action(struct ata_device *dev)
433{
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900434 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heo64f65ca2006-06-24 20:30:18 +0900435
436 return ehc->i.action | ehc->i.dev_action[dev->devno];
437}
438
Tejun Heof58229f2007-08-06 18:36:23 +0900439static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
Tejun Heoaf181c22006-06-24 20:30:18 +0900440 struct ata_eh_info *ehi, unsigned int action)
441{
Tejun Heof58229f2007-08-06 18:36:23 +0900442 struct ata_device *tdev;
Tejun Heoaf181c22006-06-24 20:30:18 +0900443
444 if (!dev) {
445 ehi->action &= ~action;
Tejun Heo1eca4362008-11-03 20:03:17 +0900446 ata_for_each_dev(tdev, link, ALL)
Tejun Heof58229f2007-08-06 18:36:23 +0900447 ehi->dev_action[tdev->devno] &= ~action;
Tejun Heoaf181c22006-06-24 20:30:18 +0900448 } else {
449 /* doesn't make sense for port-wide EH actions */
450 WARN_ON(!(action & ATA_EH_PERDEV_MASK));
451
452 /* break ehi->action into ehi->dev_action */
453 if (ehi->action & action) {
Tejun Heo1eca4362008-11-03 20:03:17 +0900454 ata_for_each_dev(tdev, link, ALL)
Tejun Heof58229f2007-08-06 18:36:23 +0900455 ehi->dev_action[tdev->devno] |=
456 ehi->action & action;
Tejun Heoaf181c22006-06-24 20:30:18 +0900457 ehi->action &= ~action;
458 }
459
460 /* turn off the specified per-dev action */
461 ehi->dev_action[dev->devno] &= ~action;
462 }
463}
464
Tejun Heoece1d632006-04-02 18:51:53 +0900465/**
Tejun Heoc0c362b2010-09-06 17:57:14 +0200466 * ata_eh_acquire - acquire EH ownership
467 * @ap: ATA port to acquire EH ownership for
468 *
469 * Acquire EH ownership for @ap. This is the basic exclusion
470 * mechanism for ports sharing a host. Only one port hanging off
471 * the same host can claim the ownership of EH.
472 *
473 * LOCKING:
474 * EH context.
475 */
476void ata_eh_acquire(struct ata_port *ap)
477{
478 mutex_lock(&ap->host->eh_mutex);
479 WARN_ON_ONCE(ap->host->eh_owner);
480 ap->host->eh_owner = current;
481}
482
483/**
484 * ata_eh_release - release EH ownership
485 * @ap: ATA port to release EH ownership for
486 *
487 * Release EH ownership for @ap if the caller. The caller must
488 * have acquired EH ownership using ata_eh_acquire() previously.
489 *
490 * LOCKING:
491 * EH context.
492 */
493void ata_eh_release(struct ata_port *ap)
494{
495 WARN_ON_ONCE(ap->host->eh_owner != current);
496 ap->host->eh_owner = NULL;
497 mutex_unlock(&ap->host->eh_mutex);
498}
499
500/**
Tejun Heoece1d632006-04-02 18:51:53 +0900501 * ata_scsi_timed_out - SCSI layer time out callback
502 * @cmd: timed out SCSI command
503 *
504 * Handles SCSI layer timeout. We race with normal completion of
505 * the qc for @cmd. If the qc is already gone, we lose and let
506 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
507 * timed out and EH should be invoked. Prevent ata_qc_complete()
508 * from finishing it by setting EH_SCHEDULED and return
509 * EH_NOT_HANDLED.
510 *
Tejun Heoad9e2762006-05-15 20:58:12 +0900511 * TODO: kill this function once old EH is gone.
512 *
Tejun Heoece1d632006-04-02 18:51:53 +0900513 * LOCKING:
514 * Called from timer context
515 *
516 * RETURNS:
517 * EH_HANDLED or EH_NOT_HANDLED
518 */
Jens Axboe242f9dc2008-09-14 05:55:09 -0700519enum blk_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
Tejun Heoece1d632006-04-02 18:51:53 +0900520{
521 struct Scsi_Host *host = cmd->device->host;
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400522 struct ata_port *ap = ata_shost_to_port(host);
Tejun Heoece1d632006-04-02 18:51:53 +0900523 unsigned long flags;
524 struct ata_queued_cmd *qc;
Jens Axboe242f9dc2008-09-14 05:55:09 -0700525 enum blk_eh_timer_return ret;
Tejun Heoece1d632006-04-02 18:51:53 +0900526
527 DPRINTK("ENTER\n");
528
Tejun Heoad9e2762006-05-15 20:58:12 +0900529 if (ap->ops->error_handler) {
Jens Axboe242f9dc2008-09-14 05:55:09 -0700530 ret = BLK_EH_NOT_HANDLED;
Tejun Heoad9e2762006-05-15 20:58:12 +0900531 goto out;
532 }
533
Jens Axboe242f9dc2008-09-14 05:55:09 -0700534 ret = BLK_EH_HANDLED;
Jeff Garzikba6a1302006-06-22 23:46:10 -0400535 spin_lock_irqsave(ap->lock, flags);
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900536 qc = ata_qc_from_tag(ap, ap->link.active_tag);
Tejun Heoece1d632006-04-02 18:51:53 +0900537 if (qc) {
538 WARN_ON(qc->scsicmd != cmd);
539 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
540 qc->err_mask |= AC_ERR_TIMEOUT;
Jens Axboe242f9dc2008-09-14 05:55:09 -0700541 ret = BLK_EH_NOT_HANDLED;
Tejun Heoece1d632006-04-02 18:51:53 +0900542 }
Jeff Garzikba6a1302006-06-22 23:46:10 -0400543 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +0900544
Tejun Heoad9e2762006-05-15 20:58:12 +0900545 out:
Tejun Heoece1d632006-04-02 18:51:53 +0900546 DPRINTK("EXIT, ret=%d\n", ret);
547 return ret;
548}
549
Tejun Heoece180d2008-11-03 20:04:37 +0900550static void ata_eh_unload(struct ata_port *ap)
551{
552 struct ata_link *link;
553 struct ata_device *dev;
554 unsigned long flags;
555
556 /* Restore SControl IPM and SPD for the next driver and
557 * disable attached devices.
558 */
559 ata_for_each_link(link, ap, PMP_FIRST) {
560 sata_scr_write(link, SCR_CONTROL, link->saved_scontrol & 0xff0);
561 ata_for_each_dev(dev, link, ALL)
562 ata_dev_disable(dev);
563 }
564
565 /* freeze and set UNLOADED */
566 spin_lock_irqsave(ap->lock, flags);
567
568 ata_port_freeze(ap); /* won't be thawed */
569 ap->pflags &= ~ATA_PFLAG_EH_PENDING; /* clear pending from freeze */
570 ap->pflags |= ATA_PFLAG_UNLOADED;
571
572 spin_unlock_irqrestore(ap->lock, flags);
573}
574
Tejun Heoece1d632006-04-02 18:51:53 +0900575/**
576 * ata_scsi_error - SCSI layer error handler callback
577 * @host: SCSI host on which error occurred
578 *
579 * Handles SCSI-layer-thrown error events.
580 *
581 * LOCKING:
582 * Inherited from SCSI layer (none, can sleep)
583 *
584 * RETURNS:
585 * Zero.
586 */
Jeff Garzik381544b2006-04-11 13:04:39 -0400587void ata_scsi_error(struct Scsi_Host *host)
Tejun Heoece1d632006-04-02 18:51:53 +0900588{
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400589 struct ata_port *ap = ata_shost_to_port(host);
Tejun Heoa1e10f72007-08-18 13:28:49 +0900590 int i;
Tejun Heoad9e2762006-05-15 20:58:12 +0900591 unsigned long flags;
James Bottomleyc34aeeb2011-01-23 08:31:14 -0600592 LIST_HEAD(eh_work_q);
Tejun Heoece1d632006-04-02 18:51:53 +0900593
594 DPRINTK("ENTER\n");
595
James Bottomleyc34aeeb2011-01-23 08:31:14 -0600596 spin_lock_irqsave(host->host_lock, flags);
597 list_splice_init(&host->eh_cmd_q, &eh_work_q);
598 spin_unlock_irqrestore(host->host_lock, flags);
599
Tejun Heoc4291372010-05-10 21:41:38 +0200600 /* make sure sff pio task is not running */
601 ata_sff_flush_pio_task(ap);
Tejun Heoece1d632006-04-02 18:51:53 +0900602
Jeff Garzikcca39742006-08-24 03:19:22 -0400603 /* synchronize with host lock and sort out timeouts */
Tejun Heoece1d632006-04-02 18:51:53 +0900604
Tejun Heoad9e2762006-05-15 20:58:12 +0900605 /* For new EH, all qcs are finished in one of three ways -
606 * normal completion, error completion, and SCSI timeout.
Alan Coxc96f1732009-03-24 10:23:46 +0000607 * Both completions can race against SCSI timeout. When normal
Tejun Heoad9e2762006-05-15 20:58:12 +0900608 * completion wins, the qc never reaches EH. When error
609 * completion wins, the qc has ATA_QCFLAG_FAILED set.
610 *
611 * When SCSI timeout wins, things are a bit more complex.
612 * Normal or error completion can occur after the timeout but
613 * before this point. In such cases, both types of
614 * completions are honored. A scmd is determined to have
615 * timed out iff its associated qc is active and not failed.
616 */
617 if (ap->ops->error_handler) {
618 struct scsi_cmnd *scmd, *tmp;
619 int nr_timedout = 0;
Tejun Heoece1d632006-04-02 18:51:53 +0900620
Tejun Heoe30349d2006-07-03 03:02:15 +0900621 spin_lock_irqsave(ap->lock, flags);
Gwendal Grignoud9027472010-05-25 12:31:38 -0700622
Alan Coxc96f1732009-03-24 10:23:46 +0000623 /* This must occur under the ap->lock as we don't want
624 a polled recovery to race the real interrupt handler
Gwendal Grignoud9027472010-05-25 12:31:38 -0700625
Alan Coxc96f1732009-03-24 10:23:46 +0000626 The lost_interrupt handler checks for any completed but
627 non-notified command and completes much like an IRQ handler.
Gwendal Grignoud9027472010-05-25 12:31:38 -0700628
Alan Coxc96f1732009-03-24 10:23:46 +0000629 We then fall into the error recovery code which will treat
630 this as if normal completion won the race */
Tejun Heoad9e2762006-05-15 20:58:12 +0900631
Alan Coxc96f1732009-03-24 10:23:46 +0000632 if (ap->ops->lost_interrupt)
633 ap->ops->lost_interrupt(ap);
Gwendal Grignoud9027472010-05-25 12:31:38 -0700634
James Bottomleyc34aeeb2011-01-23 08:31:14 -0600635 list_for_each_entry_safe(scmd, tmp, &eh_work_q, eh_entry) {
Tejun Heoad9e2762006-05-15 20:58:12 +0900636 struct ata_queued_cmd *qc;
637
638 for (i = 0; i < ATA_MAX_QUEUE; i++) {
639 qc = __ata_qc_from_tag(ap, i);
640 if (qc->flags & ATA_QCFLAG_ACTIVE &&
641 qc->scsicmd == scmd)
642 break;
643 }
644
645 if (i < ATA_MAX_QUEUE) {
646 /* the scmd has an associated qc */
647 if (!(qc->flags & ATA_QCFLAG_FAILED)) {
648 /* which hasn't failed yet, timeout */
649 qc->err_mask |= AC_ERR_TIMEOUT;
650 qc->flags |= ATA_QCFLAG_FAILED;
651 nr_timedout++;
652 }
653 } else {
654 /* Normal completion occurred after
655 * SCSI timeout but before this point.
656 * Successfully complete it.
657 */
658 scmd->retries = scmd->allowed;
659 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
660 }
661 }
662
663 /* If we have timed out qcs. They belong to EH from
664 * this point but the state of the controller is
665 * unknown. Freeze the port to make sure the IRQ
666 * handler doesn't diddle with those qcs. This must
667 * be done atomically w.r.t. setting QCFLAG_FAILED.
668 */
669 if (nr_timedout)
670 __ata_port_freeze(ap);
671
Tejun Heoe30349d2006-07-03 03:02:15 +0900672 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa1e10f72007-08-18 13:28:49 +0900673
674 /* initialize eh_tries */
675 ap->eh_tries = ATA_EH_MAX_TRIES;
Tejun Heoad9e2762006-05-15 20:58:12 +0900676 } else
Tejun Heoe30349d2006-07-03 03:02:15 +0900677 spin_unlock_wait(ap->lock);
Gwendal Grignoud9027472010-05-25 12:31:38 -0700678
Alan Coxc96f1732009-03-24 10:23:46 +0000679 /* If we timed raced normal completion and there is nothing to
680 recover nr_timedout == 0 why exactly are we doing error recovery ? */
Tejun Heoad9e2762006-05-15 20:58:12 +0900681
Tejun Heoad9e2762006-05-15 20:58:12 +0900682 /* invoke error handler */
683 if (ap->ops->error_handler) {
Tejun Heocf1b86c2007-08-06 18:36:23 +0900684 struct ata_link *link;
685
Tejun Heoc0c362b2010-09-06 17:57:14 +0200686 /* acquire EH ownership */
687 ata_eh_acquire(ap);
688 repeat:
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900689 /* kill fast drain timer */
690 del_timer_sync(&ap->fastdrain_timer);
691
Tejun Heo500530f2006-07-03 16:07:27 +0900692 /* process port resume request */
693 ata_eh_handle_port_resume(ap);
694
Tejun Heof3e81b12006-05-15 20:58:21 +0900695 /* fetch & clear EH info */
Tejun Heoe30349d2006-07-03 03:02:15 +0900696 spin_lock_irqsave(ap->lock, flags);
Tejun Heof3e81b12006-05-15 20:58:21 +0900697
Tejun Heo1eca4362008-11-03 20:03:17 +0900698 ata_for_each_link(link, ap, HOST_FIRST) {
Tejun Heo00115e02007-11-27 19:28:58 +0900699 struct ata_eh_context *ehc = &link->eh_context;
700 struct ata_device *dev;
701
Tejun Heocf1b86c2007-08-06 18:36:23 +0900702 memset(&link->eh_context, 0, sizeof(link->eh_context));
703 link->eh_context.i = link->eh_info;
704 memset(&link->eh_info, 0, sizeof(link->eh_info));
Tejun Heo00115e02007-11-27 19:28:58 +0900705
Tejun Heo1eca4362008-11-03 20:03:17 +0900706 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo00115e02007-11-27 19:28:58 +0900707 int devno = dev->devno;
708
709 ehc->saved_xfer_mode[devno] = dev->xfer_mode;
710 if (ata_ncq_enabled(dev))
711 ehc->saved_ncq_enabled |= 1 << devno;
712 }
Tejun Heocf1b86c2007-08-06 18:36:23 +0900713 }
Tejun Heof3e81b12006-05-15 20:58:21 +0900714
Tejun Heob51e9e52006-06-29 01:29:30 +0900715 ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
716 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
Tejun Heoda917d62007-09-23 13:14:12 +0900717 ap->excl_link = NULL; /* don't maintain exclusion over EH */
Tejun Heof3e81b12006-05-15 20:58:21 +0900718
Tejun Heoe30349d2006-07-03 03:02:15 +0900719 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900720
Tejun Heo500530f2006-07-03 16:07:27 +0900721 /* invoke EH, skip if unloading or suspended */
722 if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
Tejun Heo720ba122006-05-31 18:28:13 +0900723 ap->ops->error_handler(ap);
Tejun Heoece180d2008-11-03 20:04:37 +0900724 else {
725 /* if unloading, commence suicide */
726 if ((ap->pflags & ATA_PFLAG_UNLOADING) &&
727 !(ap->pflags & ATA_PFLAG_UNLOADED))
728 ata_eh_unload(ap);
Tejun Heo720ba122006-05-31 18:28:13 +0900729 ata_eh_finish(ap);
Tejun Heoece180d2008-11-03 20:04:37 +0900730 }
Tejun Heoad9e2762006-05-15 20:58:12 +0900731
Tejun Heo500530f2006-07-03 16:07:27 +0900732 /* process port suspend request */
733 ata_eh_handle_port_suspend(ap);
734
Tejun Heoad9e2762006-05-15 20:58:12 +0900735 /* Exception might have happend after ->error_handler
736 * recovered the port but before this point. Repeat
737 * EH in such case.
738 */
Tejun Heoe30349d2006-07-03 03:02:15 +0900739 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900740
Tejun Heob51e9e52006-06-29 01:29:30 +0900741 if (ap->pflags & ATA_PFLAG_EH_PENDING) {
Tejun Heoa1e10f72007-08-18 13:28:49 +0900742 if (--ap->eh_tries) {
Tejun Heoe30349d2006-07-03 03:02:15 +0900743 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900744 goto repeat;
745 }
746 ata_port_printk(ap, KERN_ERR, "EH pending after %d "
Tejun Heoa1e10f72007-08-18 13:28:49 +0900747 "tries, giving up\n", ATA_EH_MAX_TRIES);
Tejun Heo914616a2007-06-25 21:47:11 +0900748 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
Tejun Heoad9e2762006-05-15 20:58:12 +0900749 }
750
Tejun Heof3e81b12006-05-15 20:58:21 +0900751 /* this run is complete, make sure EH info is clear */
Tejun Heo1eca4362008-11-03 20:03:17 +0900752 ata_for_each_link(link, ap, HOST_FIRST)
Tejun Heocf1b86c2007-08-06 18:36:23 +0900753 memset(&link->eh_info, 0, sizeof(link->eh_info));
Tejun Heof3e81b12006-05-15 20:58:21 +0900754
Tejun Heoe30349d2006-07-03 03:02:15 +0900755 /* Clear host_eh_scheduled while holding ap->lock such
Tejun Heoad9e2762006-05-15 20:58:12 +0900756 * that if exception occurs after this point but
757 * before EH completion, SCSI midlayer will
758 * re-initiate EH.
759 */
760 host->host_eh_scheduled = 0;
761
Tejun Heoe30349d2006-07-03 03:02:15 +0900762 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc0c362b2010-09-06 17:57:14 +0200763 ata_eh_release(ap);
Tejun Heoad9e2762006-05-15 20:58:12 +0900764 } else {
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900765 WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
Tejun Heoad9e2762006-05-15 20:58:12 +0900766 ap->ops->eng_timeout(ap);
767 }
768
769 /* finish or retry handled scmd's and clean up */
James Bottomleyc34aeeb2011-01-23 08:31:14 -0600770 WARN_ON(host->host_failed || !list_empty(&eh_work_q));
Tejun Heoece1d632006-04-02 18:51:53 +0900771
772 scsi_eh_flush_done_q(&ap->eh_done_q);
773
Tejun Heoad9e2762006-05-15 20:58:12 +0900774 /* clean up */
Tejun Heoe30349d2006-07-03 03:02:15 +0900775 spin_lock_irqsave(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900776
Tejun Heo1cdaf532006-07-03 16:07:26 +0900777 if (ap->pflags & ATA_PFLAG_LOADING)
Tejun Heob51e9e52006-06-29 01:29:30 +0900778 ap->pflags &= ~ATA_PFLAG_LOADING;
Tejun Heo1cdaf532006-07-03 16:07:26 +0900779 else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
Tejun Heoad72cf92010-07-02 10:03:52 +0200780 schedule_delayed_work(&ap->hotplug_task, 0);
Tejun Heo1cdaf532006-07-03 16:07:26 +0900781
782 if (ap->pflags & ATA_PFLAG_RECOVERED)
783 ata_port_printk(ap, KERN_INFO, "EH complete\n");
Tejun Heo580b2102006-05-31 18:28:05 +0900784
Tejun Heob51e9e52006-06-29 01:29:30 +0900785 ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
Tejun Heoad9e2762006-05-15 20:58:12 +0900786
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900787 /* tell wait_eh that we're done */
Tejun Heob51e9e52006-06-29 01:29:30 +0900788 ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900789 wake_up_all(&ap->eh_wait_q);
790
Tejun Heoe30349d2006-07-03 03:02:15 +0900791 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoad9e2762006-05-15 20:58:12 +0900792
Tejun Heoece1d632006-04-02 18:51:53 +0900793 DPRINTK("EXIT\n");
Tejun Heoece1d632006-04-02 18:51:53 +0900794}
795
796/**
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900797 * ata_port_wait_eh - Wait for the currently pending EH to complete
798 * @ap: Port to wait EH for
799 *
800 * Wait until the currently pending EH is complete.
801 *
802 * LOCKING:
803 * Kernel thread context (may sleep).
804 */
805void ata_port_wait_eh(struct ata_port *ap)
806{
807 unsigned long flags;
808 DEFINE_WAIT(wait);
809
810 retry:
Jeff Garzikba6a1302006-06-22 23:46:10 -0400811 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900812
Tejun Heob51e9e52006-06-29 01:29:30 +0900813 while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900814 prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
Jeff Garzikba6a1302006-06-22 23:46:10 -0400815 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900816 schedule();
Jeff Garzikba6a1302006-06-22 23:46:10 -0400817 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900818 }
Tejun Heo0a1b6222006-06-11 11:01:38 +0900819 finish_wait(&ap->eh_wait_q, &wait);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900820
Jeff Garzikba6a1302006-06-22 23:46:10 -0400821 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900822
823 /* make sure SCSI EH is complete */
Jeff Garzikcca39742006-08-24 03:19:22 -0400824 if (scsi_host_in_recovery(ap->scsi_host)) {
Tejun Heo97750ce2010-09-06 17:56:29 +0200825 ata_msleep(ap, 10);
Tejun Heoc6cf9e92006-05-31 18:27:27 +0900826 goto retry;
827 }
828}
829
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900830static int ata_eh_nr_in_flight(struct ata_port *ap)
831{
832 unsigned int tag;
833 int nr = 0;
834
835 /* count only non-internal commands */
836 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++)
837 if (ata_qc_from_tag(ap, tag))
838 nr++;
839
840 return nr;
841}
842
843void ata_eh_fastdrain_timerfn(unsigned long arg)
844{
845 struct ata_port *ap = (void *)arg;
846 unsigned long flags;
847 int cnt;
848
849 spin_lock_irqsave(ap->lock, flags);
850
851 cnt = ata_eh_nr_in_flight(ap);
852
853 /* are we done? */
854 if (!cnt)
855 goto out_unlock;
856
857 if (cnt == ap->fastdrain_cnt) {
858 unsigned int tag;
859
860 /* No progress during the last interval, tag all
861 * in-flight qcs as timed out and freeze the port.
862 */
863 for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) {
864 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
865 if (qc)
866 qc->err_mask |= AC_ERR_TIMEOUT;
867 }
868
869 ata_port_freeze(ap);
870 } else {
871 /* some qcs have finished, give it another chance */
872 ap->fastdrain_cnt = cnt;
873 ap->fastdrain_timer.expires =
Tejun Heo341c2c92008-05-20 02:17:51 +0900874 ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900875 add_timer(&ap->fastdrain_timer);
876 }
877
878 out_unlock:
879 spin_unlock_irqrestore(ap->lock, flags);
880}
881
882/**
883 * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
884 * @ap: target ATA port
885 * @fastdrain: activate fast drain
886 *
887 * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
888 * is non-zero and EH wasn't pending before. Fast drain ensures
889 * that EH kicks in in timely manner.
890 *
891 * LOCKING:
892 * spin_lock_irqsave(host lock)
893 */
894static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
895{
896 int cnt;
897
898 /* already scheduled? */
899 if (ap->pflags & ATA_PFLAG_EH_PENDING)
900 return;
901
902 ap->pflags |= ATA_PFLAG_EH_PENDING;
903
904 if (!fastdrain)
905 return;
906
907 /* do we have in-flight qcs? */
908 cnt = ata_eh_nr_in_flight(ap);
909 if (!cnt)
910 return;
911
912 /* activate fast drain */
913 ap->fastdrain_cnt = cnt;
Tejun Heo341c2c92008-05-20 02:17:51 +0900914 ap->fastdrain_timer.expires =
915 ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900916 add_timer(&ap->fastdrain_timer);
917}
918
Tejun Heof686bcb2006-05-15 20:58:05 +0900919/**
920 * ata_qc_schedule_eh - schedule qc for error handling
921 * @qc: command to schedule error handling for
922 *
923 * Schedule error handling for @qc. EH will kick in as soon as
924 * other commands are drained.
925 *
926 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400927 * spin_lock_irqsave(host lock)
Tejun Heof686bcb2006-05-15 20:58:05 +0900928 */
929void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
930{
931 struct ata_port *ap = qc->ap;
Tejun Heofa41efd2010-04-15 08:57:37 +0900932 struct request_queue *q = qc->scsicmd->device->request_queue;
933 unsigned long flags;
Tejun Heof686bcb2006-05-15 20:58:05 +0900934
935 WARN_ON(!ap->ops->error_handler);
936
937 qc->flags |= ATA_QCFLAG_FAILED;
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900938 ata_eh_set_pending(ap, 1);
Tejun Heof686bcb2006-05-15 20:58:05 +0900939
940 /* The following will fail if timeout has already expired.
941 * ata_scsi_error() takes care of such scmds on EH entry.
942 * Note that ATA_QCFLAG_FAILED is unconditionally set after
943 * this function completes.
944 */
Tejun Heofa41efd2010-04-15 08:57:37 +0900945 spin_lock_irqsave(q->queue_lock, flags);
Jens Axboe242f9dc2008-09-14 05:55:09 -0700946 blk_abort_request(qc->scsicmd->request);
Tejun Heofa41efd2010-04-15 08:57:37 +0900947 spin_unlock_irqrestore(q->queue_lock, flags);
Tejun Heof686bcb2006-05-15 20:58:05 +0900948}
949
Tejun Heo7b70fc02006-05-15 20:58:07 +0900950/**
951 * ata_port_schedule_eh - schedule error handling without a qc
952 * @ap: ATA port to schedule EH for
953 *
954 * Schedule error handling for @ap. EH will kick in as soon as
955 * all commands are drained.
956 *
957 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400958 * spin_lock_irqsave(host lock)
Tejun Heo7b70fc02006-05-15 20:58:07 +0900959 */
960void ata_port_schedule_eh(struct ata_port *ap)
961{
962 WARN_ON(!ap->ops->error_handler);
963
Tejun Heof4d6d002007-05-01 11:50:15 +0200964 if (ap->pflags & ATA_PFLAG_INITIALIZING)
965 return;
966
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900967 ata_eh_set_pending(ap, 1);
Jeff Garzikcca39742006-08-24 03:19:22 -0400968 scsi_schedule_eh(ap->scsi_host);
Tejun Heo7b70fc02006-05-15 20:58:07 +0900969
970 DPRINTK("port EH scheduled\n");
971}
972
Tejun Heodbd82612007-08-06 18:36:23 +0900973static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
Tejun Heo7b70fc02006-05-15 20:58:07 +0900974{
975 int tag, nr_aborted = 0;
976
977 WARN_ON(!ap->ops->error_handler);
978
Tejun Heo5ddf24c2007-07-16 14:29:41 +0900979 /* we're gonna abort all commands, no need for fast drain */
980 ata_eh_set_pending(ap, 0);
981
Tejun Heo7b70fc02006-05-15 20:58:07 +0900982 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
983 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
984
Tejun Heodbd82612007-08-06 18:36:23 +0900985 if (qc && (!link || qc->dev->link == link)) {
Tejun Heo7b70fc02006-05-15 20:58:07 +0900986 qc->flags |= ATA_QCFLAG_FAILED;
987 ata_qc_complete(qc);
988 nr_aborted++;
989 }
990 }
991
992 if (!nr_aborted)
993 ata_port_schedule_eh(ap);
994
995 return nr_aborted;
996}
997
Tejun Heoe3180492006-05-15 20:58:09 +0900998/**
Tejun Heodbd82612007-08-06 18:36:23 +0900999 * ata_link_abort - abort all qc's on the link
1000 * @link: ATA link to abort qc's for
1001 *
1002 * Abort all active qc's active on @link and schedule EH.
1003 *
1004 * LOCKING:
1005 * spin_lock_irqsave(host lock)
1006 *
1007 * RETURNS:
1008 * Number of aborted qc's.
1009 */
1010int ata_link_abort(struct ata_link *link)
1011{
1012 return ata_do_link_abort(link->ap, link);
1013}
1014
1015/**
1016 * ata_port_abort - abort all qc's on the port
1017 * @ap: ATA port to abort qc's for
1018 *
1019 * Abort all active qc's of @ap and schedule EH.
1020 *
1021 * LOCKING:
1022 * spin_lock_irqsave(host_set lock)
1023 *
1024 * RETURNS:
1025 * Number of aborted qc's.
1026 */
1027int ata_port_abort(struct ata_port *ap)
1028{
1029 return ata_do_link_abort(ap, NULL);
1030}
1031
1032/**
Tejun Heoe3180492006-05-15 20:58:09 +09001033 * __ata_port_freeze - freeze port
1034 * @ap: ATA port to freeze
1035 *
1036 * This function is called when HSM violation or some other
1037 * condition disrupts normal operation of the port. Frozen port
1038 * is not allowed to perform any operation until the port is
1039 * thawed, which usually follows a successful reset.
1040 *
1041 * ap->ops->freeze() callback can be used for freezing the port
1042 * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
1043 * port cannot be frozen hardware-wise, the interrupt handler
1044 * must ack and clear interrupts unconditionally while the port
1045 * is frozen.
1046 *
1047 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001048 * spin_lock_irqsave(host lock)
Tejun Heoe3180492006-05-15 20:58:09 +09001049 */
1050static void __ata_port_freeze(struct ata_port *ap)
1051{
1052 WARN_ON(!ap->ops->error_handler);
1053
1054 if (ap->ops->freeze)
1055 ap->ops->freeze(ap);
1056
Tejun Heob51e9e52006-06-29 01:29:30 +09001057 ap->pflags |= ATA_PFLAG_FROZEN;
Tejun Heoe3180492006-05-15 20:58:09 +09001058
Tejun Heo44877b42007-02-21 01:06:51 +09001059 DPRINTK("ata%u port frozen\n", ap->print_id);
Tejun Heoe3180492006-05-15 20:58:09 +09001060}
1061
1062/**
1063 * ata_port_freeze - abort & freeze port
1064 * @ap: ATA port to freeze
1065 *
Jeff Garzik54c38442009-04-07 19:13:15 -04001066 * Abort and freeze @ap. The freeze operation must be called
1067 * first, because some hardware requires special operations
1068 * before the taskfile registers are accessible.
Tejun Heoe3180492006-05-15 20:58:09 +09001069 *
1070 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001071 * spin_lock_irqsave(host lock)
Tejun Heoe3180492006-05-15 20:58:09 +09001072 *
1073 * RETURNS:
1074 * Number of aborted commands.
1075 */
1076int ata_port_freeze(struct ata_port *ap)
1077{
1078 int nr_aborted;
1079
1080 WARN_ON(!ap->ops->error_handler);
1081
Tejun Heoe3180492006-05-15 20:58:09 +09001082 __ata_port_freeze(ap);
Jeff Garzik54c38442009-04-07 19:13:15 -04001083 nr_aborted = ata_port_abort(ap);
Tejun Heoe3180492006-05-15 20:58:09 +09001084
1085 return nr_aborted;
1086}
1087
1088/**
Tejun Heo7d77b242007-09-23 13:14:13 +09001089 * sata_async_notification - SATA async notification handler
1090 * @ap: ATA port where async notification is received
1091 *
1092 * Handler to be called when async notification via SDB FIS is
1093 * received. This function schedules EH if necessary.
1094 *
1095 * LOCKING:
1096 * spin_lock_irqsave(host lock)
1097 *
1098 * RETURNS:
1099 * 1 if EH is scheduled, 0 otherwise.
1100 */
1101int sata_async_notification(struct ata_port *ap)
1102{
1103 u32 sntf;
1104 int rc;
1105
1106 if (!(ap->flags & ATA_FLAG_AN))
1107 return 0;
1108
1109 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
1110 if (rc == 0)
1111 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
1112
Tejun Heo071f44b2008-04-07 22:47:22 +09001113 if (!sata_pmp_attached(ap) || rc) {
Tejun Heo7d77b242007-09-23 13:14:13 +09001114 /* PMP is not attached or SNTF is not available */
Tejun Heo071f44b2008-04-07 22:47:22 +09001115 if (!sata_pmp_attached(ap)) {
Tejun Heo7d77b242007-09-23 13:14:13 +09001116 /* PMP is not attached. Check whether ATAPI
1117 * AN is configured. If so, notify media
1118 * change.
1119 */
1120 struct ata_device *dev = ap->link.device;
1121
1122 if ((dev->class == ATA_DEV_ATAPI) &&
1123 (dev->flags & ATA_DFLAG_AN))
1124 ata_scsi_media_change_notify(dev);
1125 return 0;
1126 } else {
1127 /* PMP is attached but SNTF is not available.
1128 * ATAPI async media change notification is
1129 * not used. The PMP must be reporting PHY
1130 * status change, schedule EH.
1131 */
1132 ata_port_schedule_eh(ap);
1133 return 1;
1134 }
1135 } else {
1136 /* PMP is attached and SNTF is available */
1137 struct ata_link *link;
1138
1139 /* check and notify ATAPI AN */
Tejun Heo1eca4362008-11-03 20:03:17 +09001140 ata_for_each_link(link, ap, EDGE) {
Tejun Heo7d77b242007-09-23 13:14:13 +09001141 if (!(sntf & (1 << link->pmp)))
1142 continue;
1143
1144 if ((link->device->class == ATA_DEV_ATAPI) &&
1145 (link->device->flags & ATA_DFLAG_AN))
1146 ata_scsi_media_change_notify(link->device);
1147 }
1148
1149 /* If PMP is reporting that PHY status of some
1150 * downstream ports has changed, schedule EH.
1151 */
1152 if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
1153 ata_port_schedule_eh(ap);
1154 return 1;
1155 }
1156
1157 return 0;
1158 }
1159}
1160
1161/**
Tejun Heoe3180492006-05-15 20:58:09 +09001162 * ata_eh_freeze_port - EH helper to freeze port
1163 * @ap: ATA port to freeze
1164 *
1165 * Freeze @ap.
1166 *
1167 * LOCKING:
1168 * None.
1169 */
1170void ata_eh_freeze_port(struct ata_port *ap)
1171{
1172 unsigned long flags;
1173
1174 if (!ap->ops->error_handler)
1175 return;
1176
Jeff Garzikba6a1302006-06-22 23:46:10 -04001177 spin_lock_irqsave(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001178 __ata_port_freeze(ap);
Jeff Garzikba6a1302006-06-22 23:46:10 -04001179 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001180}
1181
1182/**
1183 * ata_port_thaw_port - EH helper to thaw port
1184 * @ap: ATA port to thaw
1185 *
1186 * Thaw frozen port @ap.
1187 *
1188 * LOCKING:
1189 * None.
1190 */
1191void ata_eh_thaw_port(struct ata_port *ap)
1192{
1193 unsigned long flags;
1194
1195 if (!ap->ops->error_handler)
1196 return;
1197
Jeff Garzikba6a1302006-06-22 23:46:10 -04001198 spin_lock_irqsave(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001199
Tejun Heob51e9e52006-06-29 01:29:30 +09001200 ap->pflags &= ~ATA_PFLAG_FROZEN;
Tejun Heoe3180492006-05-15 20:58:09 +09001201
1202 if (ap->ops->thaw)
1203 ap->ops->thaw(ap);
1204
Jeff Garzikba6a1302006-06-22 23:46:10 -04001205 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001206
Tejun Heo44877b42007-02-21 01:06:51 +09001207 DPRINTK("ata%u port thawed\n", ap->print_id);
Tejun Heoe3180492006-05-15 20:58:09 +09001208}
1209
Tejun Heoece1d632006-04-02 18:51:53 +09001210static void ata_eh_scsidone(struct scsi_cmnd *scmd)
1211{
1212 /* nada */
1213}
1214
1215static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
1216{
1217 struct ata_port *ap = qc->ap;
1218 struct scsi_cmnd *scmd = qc->scsicmd;
1219 unsigned long flags;
1220
Jeff Garzikba6a1302006-06-22 23:46:10 -04001221 spin_lock_irqsave(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +09001222 qc->scsidone = ata_eh_scsidone;
1223 __ata_qc_complete(qc);
1224 WARN_ON(ata_tag_valid(qc->tag));
Jeff Garzikba6a1302006-06-22 23:46:10 -04001225 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoece1d632006-04-02 18:51:53 +09001226
1227 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
1228}
1229
1230/**
1231 * ata_eh_qc_complete - Complete an active ATA command from EH
1232 * @qc: Command to complete
1233 *
1234 * Indicate to the mid and upper layers that an ATA command has
1235 * completed. To be used from EH.
1236 */
1237void ata_eh_qc_complete(struct ata_queued_cmd *qc)
1238{
1239 struct scsi_cmnd *scmd = qc->scsicmd;
1240 scmd->retries = scmd->allowed;
1241 __ata_eh_qc_complete(qc);
1242}
1243
1244/**
1245 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
1246 * @qc: Command to retry
1247 *
1248 * Indicate to the mid and upper layers that an ATA command
1249 * should be retried. To be used from EH.
1250 *
1251 * SCSI midlayer limits the number of retries to scmd->allowed.
1252 * scmd->retries is decremented for commands which get retried
1253 * due to unrelated failures (qc->err_mask is zero).
1254 */
1255void ata_eh_qc_retry(struct ata_queued_cmd *qc)
1256{
1257 struct scsi_cmnd *scmd = qc->scsicmd;
1258 if (!qc->err_mask && scmd->retries)
1259 scmd->retries--;
1260 __ata_eh_qc_complete(qc);
1261}
Tejun Heo022bdb02006-05-15 20:58:22 +09001262
1263/**
Tejun Heo678afac2009-01-29 20:31:30 +09001264 * ata_dev_disable - disable ATA device
1265 * @dev: ATA device to disable
1266 *
1267 * Disable @dev.
1268 *
1269 * Locking:
1270 * EH context.
1271 */
1272void ata_dev_disable(struct ata_device *dev)
1273{
1274 if (!ata_dev_enabled(dev))
1275 return;
1276
1277 if (ata_msg_drv(dev->link->ap))
1278 ata_dev_printk(dev, KERN_WARNING, "disabled\n");
1279 ata_acpi_on_disable(dev);
1280 ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 | ATA_DNXFER_QUIET);
1281 dev->class++;
Tejun Heo99cf6102009-01-29 20:31:32 +09001282
1283 /* From now till the next successful probe, ering is used to
1284 * track probe failures. Clear accumulated device error info.
1285 */
1286 ata_ering_clear(&dev->ering);
Tejun Heo678afac2009-01-29 20:31:30 +09001287}
1288
1289/**
Tejun Heo0ea035a2006-05-31 18:28:01 +09001290 * ata_eh_detach_dev - detach ATA device
1291 * @dev: ATA device to detach
1292 *
1293 * Detach @dev.
1294 *
1295 * LOCKING:
1296 * None.
1297 */
Tejun Heofb7fd612007-09-23 13:14:12 +09001298void ata_eh_detach_dev(struct ata_device *dev)
Tejun Heo0ea035a2006-05-31 18:28:01 +09001299{
Tejun Heof58229f2007-08-06 18:36:23 +09001300 struct ata_link *link = dev->link;
1301 struct ata_port *ap = link->ap;
Tejun Heo90484eb2008-10-26 15:43:03 +09001302 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo0ea035a2006-05-31 18:28:01 +09001303 unsigned long flags;
1304
1305 ata_dev_disable(dev);
1306
Jeff Garzikba6a1302006-06-22 23:46:10 -04001307 spin_lock_irqsave(ap->lock, flags);
Tejun Heo0ea035a2006-05-31 18:28:01 +09001308
1309 dev->flags &= ~ATA_DFLAG_DETACH;
1310
1311 if (ata_scsi_offline_dev(dev)) {
1312 dev->flags |= ATA_DFLAG_DETACHED;
Tejun Heob51e9e52006-06-29 01:29:30 +09001313 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
Tejun Heo0ea035a2006-05-31 18:28:01 +09001314 }
1315
Tejun Heo90484eb2008-10-26 15:43:03 +09001316 /* clear per-dev EH info */
Tejun Heof58229f2007-08-06 18:36:23 +09001317 ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
1318 ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
Tejun Heo90484eb2008-10-26 15:43:03 +09001319 ehc->saved_xfer_mode[dev->devno] = 0;
1320 ehc->saved_ncq_enabled &= ~(1 << dev->devno);
Tejun Heobeb07c12006-06-24 20:30:19 +09001321
Jeff Garzikba6a1302006-06-22 23:46:10 -04001322 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo0ea035a2006-05-31 18:28:01 +09001323}
1324
1325/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001326 * ata_eh_about_to_do - about to perform eh_action
Tejun Heo955e57d2007-08-06 18:36:23 +09001327 * @link: target ATA link
Tejun Heo47005f22006-06-19 18:27:23 +09001328 * @dev: target ATA dev for per-dev action (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09001329 * @action: action about to be performed
1330 *
1331 * Called just before performing EH actions to clear related bits
Tejun Heo955e57d2007-08-06 18:36:23 +09001332 * in @link->eh_info such that eh actions are not unnecessarily
1333 * repeated.
Tejun Heo022bdb02006-05-15 20:58:22 +09001334 *
1335 * LOCKING:
1336 * None.
1337 */
Tejun Heofb7fd612007-09-23 13:14:12 +09001338void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
1339 unsigned int action)
Tejun Heo022bdb02006-05-15 20:58:22 +09001340{
Tejun Heo955e57d2007-08-06 18:36:23 +09001341 struct ata_port *ap = link->ap;
1342 struct ata_eh_info *ehi = &link->eh_info;
1343 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001344 unsigned long flags;
1345
Jeff Garzikba6a1302006-06-22 23:46:10 -04001346 spin_lock_irqsave(ap->lock, flags);
Tejun Heo1cdaf532006-07-03 16:07:26 +09001347
Tejun Heo955e57d2007-08-06 18:36:23 +09001348 ata_eh_clear_action(link, dev, ehi, action);
Tejun Heo13abf502006-07-10 23:18:46 +09001349
Tejun Heoa568d1d2008-10-21 20:37:21 +09001350 /* About to take EH action, set RECOVERED. Ignore actions on
1351 * slave links as master will do them again.
1352 */
1353 if (!(ehc->i.flags & ATA_EHI_QUIET) && link != ap->slave_link)
Tejun Heo1cdaf532006-07-03 16:07:26 +09001354 ap->pflags |= ATA_PFLAG_RECOVERED;
1355
Jeff Garzikba6a1302006-06-22 23:46:10 -04001356 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo022bdb02006-05-15 20:58:22 +09001357}
1358
1359/**
Tejun Heo47005f22006-06-19 18:27:23 +09001360 * ata_eh_done - EH action complete
Tejun Heo955e57d2007-08-06 18:36:23 +09001361* @ap: target ATA port
Tejun Heo47005f22006-06-19 18:27:23 +09001362 * @dev: target ATA dev for per-dev action (can be NULL)
1363 * @action: action just completed
1364 *
1365 * Called right after performing EH actions to clear related bits
Tejun Heo955e57d2007-08-06 18:36:23 +09001366 * in @link->eh_context.
Tejun Heo47005f22006-06-19 18:27:23 +09001367 *
1368 * LOCKING:
1369 * None.
1370 */
Tejun Heofb7fd612007-09-23 13:14:12 +09001371void ata_eh_done(struct ata_link *link, struct ata_device *dev,
1372 unsigned int action)
Tejun Heo47005f22006-06-19 18:27:23 +09001373{
Tejun Heo955e57d2007-08-06 18:36:23 +09001374 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001375
Tejun Heo955e57d2007-08-06 18:36:23 +09001376 ata_eh_clear_action(link, dev, &ehc->i, action);
Tejun Heo47005f22006-06-19 18:27:23 +09001377}
1378
1379/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001380 * ata_err_string - convert err_mask to descriptive string
1381 * @err_mask: error mask to convert to string
1382 *
1383 * Convert @err_mask to descriptive string. Errors are
1384 * prioritized according to severity and only the most severe
1385 * error is reported.
1386 *
1387 * LOCKING:
1388 * None.
1389 *
1390 * RETURNS:
1391 * Descriptive string for @err_mask
1392 */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001393static const char *ata_err_string(unsigned int err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001394{
1395 if (err_mask & AC_ERR_HOST_BUS)
1396 return "host bus error";
1397 if (err_mask & AC_ERR_ATA_BUS)
1398 return "ATA bus error";
1399 if (err_mask & AC_ERR_TIMEOUT)
1400 return "timeout";
1401 if (err_mask & AC_ERR_HSM)
1402 return "HSM violation";
1403 if (err_mask & AC_ERR_SYSTEM)
1404 return "internal error";
1405 if (err_mask & AC_ERR_MEDIA)
1406 return "media error";
1407 if (err_mask & AC_ERR_INVALID)
1408 return "invalid argument";
1409 if (err_mask & AC_ERR_DEV)
1410 return "device error";
1411 return "unknown error";
1412}
1413
1414/**
Tejun Heoe8ee8452006-05-15 21:03:46 +09001415 * ata_read_log_page - read a specific log page
1416 * @dev: target device
1417 * @page: page to read
1418 * @buf: buffer to store read page
1419 * @sectors: number of sectors to read
1420 *
1421 * Read log page using READ_LOG_EXT command.
1422 *
1423 * LOCKING:
1424 * Kernel thread context (may sleep).
1425 *
1426 * RETURNS:
1427 * 0 on success, AC_ERR_* mask otherwise.
1428 */
1429static unsigned int ata_read_log_page(struct ata_device *dev,
1430 u8 page, void *buf, unsigned int sectors)
1431{
1432 struct ata_taskfile tf;
1433 unsigned int err_mask;
1434
1435 DPRINTK("read log page - page %d\n", page);
1436
1437 ata_tf_init(dev, &tf);
1438 tf.command = ATA_CMD_READ_LOG_EXT;
1439 tf.lbal = page;
1440 tf.nsect = sectors;
1441 tf.hob_nsect = sectors >> 8;
1442 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
1443 tf.protocol = ATA_PROT_PIO;
1444
1445 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
Tejun Heo2b789102007-10-09 15:05:44 +09001446 buf, sectors * ATA_SECT_SIZE, 0);
Tejun Heoe8ee8452006-05-15 21:03:46 +09001447
1448 DPRINTK("EXIT, err_mask=%x\n", err_mask);
1449 return err_mask;
1450}
1451
1452/**
1453 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
1454 * @dev: Device to read log page 10h from
1455 * @tag: Resulting tag of the failed command
1456 * @tf: Resulting taskfile registers of the failed command
1457 *
1458 * Read log page 10h to obtain NCQ error details and clear error
1459 * condition.
1460 *
1461 * LOCKING:
1462 * Kernel thread context (may sleep).
1463 *
1464 * RETURNS:
1465 * 0 on success, -errno otherwise.
1466 */
1467static int ata_eh_read_log_10h(struct ata_device *dev,
1468 int *tag, struct ata_taskfile *tf)
1469{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001470 u8 *buf = dev->link->ap->sector_buf;
Tejun Heoe8ee8452006-05-15 21:03:46 +09001471 unsigned int err_mask;
1472 u8 csum;
1473 int i;
1474
1475 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
1476 if (err_mask)
1477 return -EIO;
1478
1479 csum = 0;
1480 for (i = 0; i < ATA_SECT_SIZE; i++)
1481 csum += buf[i];
1482 if (csum)
1483 ata_dev_printk(dev, KERN_WARNING,
1484 "invalid checksum 0x%x on log page 10h\n", csum);
1485
1486 if (buf[0] & 0x80)
1487 return -ENOENT;
1488
1489 *tag = buf[0] & 0x1f;
1490
1491 tf->command = buf[2];
1492 tf->feature = buf[3];
1493 tf->lbal = buf[4];
1494 tf->lbam = buf[5];
1495 tf->lbah = buf[6];
1496 tf->device = buf[7];
1497 tf->hob_lbal = buf[8];
1498 tf->hob_lbam = buf[9];
1499 tf->hob_lbah = buf[10];
1500 tf->nsect = buf[12];
1501 tf->hob_nsect = buf[13];
1502
1503 return 0;
1504}
1505
1506/**
Tejun Heo11fc33d2008-08-30 14:20:01 +02001507 * atapi_eh_tur - perform ATAPI TEST_UNIT_READY
1508 * @dev: target ATAPI device
1509 * @r_sense_key: out parameter for sense_key
1510 *
1511 * Perform ATAPI TEST_UNIT_READY.
1512 *
1513 * LOCKING:
1514 * EH context (may sleep).
1515 *
1516 * RETURNS:
1517 * 0 on success, AC_ERR_* mask on failure.
1518 */
1519static unsigned int atapi_eh_tur(struct ata_device *dev, u8 *r_sense_key)
1520{
1521 u8 cdb[ATAPI_CDB_LEN] = { TEST_UNIT_READY, 0, 0, 0, 0, 0 };
1522 struct ata_taskfile tf;
1523 unsigned int err_mask;
1524
1525 ata_tf_init(dev, &tf);
1526
1527 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1528 tf.command = ATA_CMD_PACKET;
1529 tf.protocol = ATAPI_PROT_NODATA;
1530
1531 err_mask = ata_exec_internal(dev, &tf, cdb, DMA_NONE, NULL, 0, 0);
1532 if (err_mask == AC_ERR_DEV)
1533 *r_sense_key = tf.feature >> 4;
1534 return err_mask;
1535}
1536
1537/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001538 * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
1539 * @dev: device to perform REQUEST_SENSE to
1540 * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
Tejun Heo3eabddb2008-06-10 18:28:05 +09001541 * @dfl_sense_key: default sense key to use
Tejun Heo022bdb02006-05-15 20:58:22 +09001542 *
1543 * Perform ATAPI REQUEST_SENSE after the device reported CHECK
1544 * SENSE. This function is EH helper.
1545 *
1546 * LOCKING:
1547 * Kernel thread context (may sleep).
1548 *
1549 * RETURNS:
1550 * 0 on success, AC_ERR_* mask on failure
1551 */
Tejun Heo3eabddb2008-06-10 18:28:05 +09001552static unsigned int atapi_eh_request_sense(struct ata_device *dev,
1553 u8 *sense_buf, u8 dfl_sense_key)
Tejun Heo022bdb02006-05-15 20:58:22 +09001554{
Tejun Heo3eabddb2008-06-10 18:28:05 +09001555 u8 cdb[ATAPI_CDB_LEN] =
1556 { REQUEST_SENSE, 0, 0, 0, SCSI_SENSE_BUFFERSIZE, 0 };
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001557 struct ata_port *ap = dev->link->ap;
Tejun Heo022bdb02006-05-15 20:58:22 +09001558 struct ata_taskfile tf;
Tejun Heo022bdb02006-05-15 20:58:22 +09001559
1560 DPRINTK("ATAPI request sense\n");
1561
Tejun Heo022bdb02006-05-15 20:58:22 +09001562 /* FIXME: is this needed? */
1563 memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
1564
Albert Lee56287762007-04-02 11:30:46 +08001565 /* initialize sense_buf with the error register,
1566 * for the case where they are -not- overwritten
1567 */
Tejun Heo022bdb02006-05-15 20:58:22 +09001568 sense_buf[0] = 0x70;
Tejun Heo3eabddb2008-06-10 18:28:05 +09001569 sense_buf[2] = dfl_sense_key;
Albert Lee56287762007-04-02 11:30:46 +08001570
Jeff Garzika617c092007-05-21 20:14:23 -04001571 /* some devices time out if garbage left in tf */
Albert Lee56287762007-04-02 11:30:46 +08001572 ata_tf_init(dev, &tf);
Tejun Heo022bdb02006-05-15 20:58:22 +09001573
Tejun Heo022bdb02006-05-15 20:58:22 +09001574 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1575 tf.command = ATA_CMD_PACKET;
1576
1577 /* is it pointless to prefer PIO for "safety reasons"? */
1578 if (ap->flags & ATA_FLAG_PIO_DMA) {
Tejun Heo0dc36882007-12-18 16:34:43 -05001579 tf.protocol = ATAPI_PROT_DMA;
Tejun Heo022bdb02006-05-15 20:58:22 +09001580 tf.feature |= ATAPI_PKT_DMA;
1581 } else {
Tejun Heo0dc36882007-12-18 16:34:43 -05001582 tf.protocol = ATAPI_PROT_PIO;
Tejun Heof2dfc1a2007-12-12 12:12:46 +09001583 tf.lbam = SCSI_SENSE_BUFFERSIZE;
1584 tf.lbah = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09001585 }
1586
1587 return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
Tejun Heo2b789102007-10-09 15:05:44 +09001588 sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
Tejun Heo022bdb02006-05-15 20:58:22 +09001589}
1590
1591/**
1592 * ata_eh_analyze_serror - analyze SError for a failed port
Tejun Heo02607312007-08-06 18:36:23 +09001593 * @link: ATA link to analyze SError for
Tejun Heo022bdb02006-05-15 20:58:22 +09001594 *
1595 * Analyze SError if available and further determine cause of
1596 * failure.
1597 *
1598 * LOCKING:
1599 * None.
1600 */
Tejun Heo02607312007-08-06 18:36:23 +09001601static void ata_eh_analyze_serror(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09001602{
Tejun Heo02607312007-08-06 18:36:23 +09001603 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09001604 u32 serror = ehc->i.serror;
1605 unsigned int err_mask = 0, action = 0;
Tejun Heof9df58c2007-09-23 13:14:13 +09001606 u32 hotplug_mask;
Tejun Heo022bdb02006-05-15 20:58:22 +09001607
Tejun Heoe0614db2008-05-19 01:15:09 +09001608 if (serror & (SERR_PERSISTENT | SERR_DATA)) {
Tejun Heo022bdb02006-05-15 20:58:22 +09001609 err_mask |= AC_ERR_ATA_BUS;
Tejun Heocf480622008-01-24 00:05:14 +09001610 action |= ATA_EH_RESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001611 }
1612 if (serror & SERR_PROTOCOL) {
1613 err_mask |= AC_ERR_HSM;
Tejun Heocf480622008-01-24 00:05:14 +09001614 action |= ATA_EH_RESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001615 }
1616 if (serror & SERR_INTERNAL) {
1617 err_mask |= AC_ERR_SYSTEM;
Tejun Heocf480622008-01-24 00:05:14 +09001618 action |= ATA_EH_RESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001619 }
Tejun Heof9df58c2007-09-23 13:14:13 +09001620
1621 /* Determine whether a hotplug event has occurred. Both
1622 * SError.N/X are considered hotplug events for enabled or
1623 * host links. For disabled PMP links, only N bit is
1624 * considered as X bit is left at 1 for link plugging.
1625 */
Tejun Heoeb0e85e2011-02-24 19:30:37 +01001626 if (link->lpm_policy > ATA_LPM_MAX_POWER)
Tejun Heo6b7ae952010-09-01 17:50:06 +02001627 hotplug_mask = 0; /* hotplug doesn't work w/ LPM */
1628 else if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
Tejun Heof9df58c2007-09-23 13:14:13 +09001629 hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
1630 else
1631 hotplug_mask = SERR_PHYRDY_CHG;
1632
1633 if (serror & hotplug_mask)
Tejun Heo084fe632006-05-31 18:28:03 +09001634 ata_ehi_hotplugged(&ehc->i);
Tejun Heo022bdb02006-05-15 20:58:22 +09001635
1636 ehc->i.err_mask |= err_mask;
1637 ehc->i.action |= action;
1638}
1639
1640/**
Tejun Heoe8ee8452006-05-15 21:03:46 +09001641 * ata_eh_analyze_ncq_error - analyze NCQ error
Tejun Heo02607312007-08-06 18:36:23 +09001642 * @link: ATA link to analyze NCQ error for
Tejun Heoe8ee8452006-05-15 21:03:46 +09001643 *
1644 * Read log page 10h, determine the offending qc and acquire
1645 * error status TF. For NCQ device errors, all LLDDs have to do
1646 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1647 * care of the rest.
1648 *
1649 * LOCKING:
1650 * Kernel thread context (may sleep).
1651 */
Mark Lord10acf3b2008-05-02 02:14:53 -04001652void ata_eh_analyze_ncq_error(struct ata_link *link)
Tejun Heoe8ee8452006-05-15 21:03:46 +09001653{
Tejun Heo02607312007-08-06 18:36:23 +09001654 struct ata_port *ap = link->ap;
1655 struct ata_eh_context *ehc = &link->eh_context;
1656 struct ata_device *dev = link->device;
Tejun Heoe8ee8452006-05-15 21:03:46 +09001657 struct ata_queued_cmd *qc;
1658 struct ata_taskfile tf;
1659 int tag, rc;
1660
1661 /* if frozen, we can't do much */
Tejun Heob51e9e52006-06-29 01:29:30 +09001662 if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heoe8ee8452006-05-15 21:03:46 +09001663 return;
1664
1665 /* is it NCQ device error? */
Tejun Heo02607312007-08-06 18:36:23 +09001666 if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
Tejun Heoe8ee8452006-05-15 21:03:46 +09001667 return;
1668
1669 /* has LLDD analyzed already? */
1670 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
1671 qc = __ata_qc_from_tag(ap, tag);
1672
1673 if (!(qc->flags & ATA_QCFLAG_FAILED))
1674 continue;
1675
1676 if (qc->err_mask)
1677 return;
1678 }
1679
1680 /* okay, this error is ours */
Jeff Garzika09bf4c2010-04-22 21:59:13 -04001681 memset(&tf, 0, sizeof(tf));
Tejun Heoe8ee8452006-05-15 21:03:46 +09001682 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1683 if (rc) {
Tejun Heo02607312007-08-06 18:36:23 +09001684 ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
Tejun Heoe8ee8452006-05-15 21:03:46 +09001685 "(errno=%d)\n", rc);
1686 return;
1687 }
1688
Tejun Heo02607312007-08-06 18:36:23 +09001689 if (!(link->sactive & (1 << tag))) {
1690 ata_link_printk(link, KERN_ERR, "log page 10h reported "
Tejun Heoe8ee8452006-05-15 21:03:46 +09001691 "inactive tag %d\n", tag);
1692 return;
1693 }
1694
1695 /* we've got the perpetrator, condemn it */
1696 qc = __ata_qc_from_tag(ap, tag);
1697 memcpy(&qc->result_tf, &tf, sizeof(tf));
Mark Lorda6116c92008-04-23 22:36:25 -04001698 qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
Tejun Heo5335b722007-07-16 14:29:40 +09001699 qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
Tejun Heoe8ee8452006-05-15 21:03:46 +09001700 ehc->i.err_mask &= ~AC_ERR_DEV;
1701}
1702
1703/**
Tejun Heo022bdb02006-05-15 20:58:22 +09001704 * ata_eh_analyze_tf - analyze taskfile of a failed qc
1705 * @qc: qc to analyze
1706 * @tf: Taskfile registers to analyze
1707 *
1708 * Analyze taskfile of @qc and further determine cause of
1709 * failure. This function also requests ATAPI sense data if
1710 * avaliable.
1711 *
1712 * LOCKING:
1713 * Kernel thread context (may sleep).
1714 *
1715 * RETURNS:
1716 * Determined recovery action
1717 */
1718static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
1719 const struct ata_taskfile *tf)
1720{
1721 unsigned int tmp, action = 0;
1722 u8 stat = tf->command, err = tf->feature;
1723
1724 if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
1725 qc->err_mask |= AC_ERR_HSM;
Tejun Heocf480622008-01-24 00:05:14 +09001726 return ATA_EH_RESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001727 }
1728
Tejun Heoa51d6442007-03-20 15:24:11 +09001729 if (stat & (ATA_ERR | ATA_DF))
1730 qc->err_mask |= AC_ERR_DEV;
1731 else
Tejun Heo022bdb02006-05-15 20:58:22 +09001732 return 0;
1733
1734 switch (qc->dev->class) {
1735 case ATA_DEV_ATA:
1736 if (err & ATA_ICRC)
1737 qc->err_mask |= AC_ERR_ATA_BUS;
1738 if (err & ATA_UNC)
1739 qc->err_mask |= AC_ERR_MEDIA;
1740 if (err & ATA_IDNF)
1741 qc->err_mask |= AC_ERR_INVALID;
1742 break;
1743
1744 case ATA_DEV_ATAPI:
Tejun Heoa569a302006-11-21 10:40:51 +09001745 if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
Tejun Heo3eabddb2008-06-10 18:28:05 +09001746 tmp = atapi_eh_request_sense(qc->dev,
1747 qc->scsicmd->sense_buffer,
1748 qc->result_tf.feature >> 4);
Tejun Heoa569a302006-11-21 10:40:51 +09001749 if (!tmp) {
1750 /* ATA_QCFLAG_SENSE_VALID is used to
1751 * tell atapi_qc_complete() that sense
1752 * data is already valid.
1753 *
1754 * TODO: interpret sense data and set
1755 * appropriate err_mask.
1756 */
1757 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1758 } else
1759 qc->err_mask |= tmp;
1760 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001761 }
1762
1763 if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
Tejun Heocf480622008-01-24 00:05:14 +09001764 action |= ATA_EH_RESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09001765
1766 return action;
1767}
1768
Tejun Heo76326ac2007-11-27 19:28:59 +09001769static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask,
1770 int *xfer_ok)
Tejun Heo022bdb02006-05-15 20:58:22 +09001771{
Tejun Heo76326ac2007-11-27 19:28:59 +09001772 int base = 0;
1773
1774 if (!(eflags & ATA_EFLAG_DUBIOUS_XFER))
1775 *xfer_ok = 1;
1776
1777 if (!*xfer_ok)
Tejun Heo75f9caf2008-01-03 01:21:14 +09001778 base = ATA_ECAT_DUBIOUS_NONE;
Tejun Heo76326ac2007-11-27 19:28:59 +09001779
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001780 if (err_mask & AC_ERR_ATA_BUS)
Tejun Heo76326ac2007-11-27 19:28:59 +09001781 return base + ATA_ECAT_ATA_BUS;
Tejun Heo022bdb02006-05-15 20:58:22 +09001782
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001783 if (err_mask & AC_ERR_TIMEOUT)
Tejun Heo76326ac2007-11-27 19:28:59 +09001784 return base + ATA_ECAT_TOUT_HSM;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001785
Tejun Heo3884f7b2007-11-27 19:28:56 +09001786 if (eflags & ATA_EFLAG_IS_IO) {
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001787 if (err_mask & AC_ERR_HSM)
Tejun Heo76326ac2007-11-27 19:28:59 +09001788 return base + ATA_ECAT_TOUT_HSM;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001789 if ((err_mask &
1790 (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
Tejun Heo76326ac2007-11-27 19:28:59 +09001791 return base + ATA_ECAT_UNK_DEV;
Tejun Heo022bdb02006-05-15 20:58:22 +09001792 }
1793
1794 return 0;
1795}
1796
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001797struct speed_down_verdict_arg {
Tejun Heo022bdb02006-05-15 20:58:22 +09001798 u64 since;
Tejun Heo76326ac2007-11-27 19:28:59 +09001799 int xfer_ok;
Tejun Heo3884f7b2007-11-27 19:28:56 +09001800 int nr_errors[ATA_ECAT_NR];
Tejun Heo022bdb02006-05-15 20:58:22 +09001801};
1802
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001803static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
Tejun Heo022bdb02006-05-15 20:58:22 +09001804{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001805 struct speed_down_verdict_arg *arg = void_arg;
Tejun Heo76326ac2007-11-27 19:28:59 +09001806 int cat;
Tejun Heo022bdb02006-05-15 20:58:22 +09001807
Gwendal Grignoud9027472010-05-25 12:31:38 -07001808 if ((ent->eflags & ATA_EFLAG_OLD_ER) || (ent->timestamp < arg->since))
Tejun Heo022bdb02006-05-15 20:58:22 +09001809 return -1;
1810
Tejun Heo76326ac2007-11-27 19:28:59 +09001811 cat = ata_eh_categorize_error(ent->eflags, ent->err_mask,
1812 &arg->xfer_ok);
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001813 arg->nr_errors[cat]++;
Tejun Heo76326ac2007-11-27 19:28:59 +09001814
Tejun Heo022bdb02006-05-15 20:58:22 +09001815 return 0;
1816}
1817
1818/**
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001819 * ata_eh_speed_down_verdict - Determine speed down verdict
Tejun Heo022bdb02006-05-15 20:58:22 +09001820 * @dev: Device of interest
1821 *
1822 * This function examines error ring of @dev and determines
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001823 * whether NCQ needs to be turned off, transfer speed should be
1824 * stepped down, or falling back to PIO is necessary.
Tejun Heo022bdb02006-05-15 20:58:22 +09001825 *
Tejun Heo3884f7b2007-11-27 19:28:56 +09001826 * ECAT_ATA_BUS : ATA_BUS error for any command
Tejun Heo022bdb02006-05-15 20:58:22 +09001827 *
Tejun Heo3884f7b2007-11-27 19:28:56 +09001828 * ECAT_TOUT_HSM : TIMEOUT for any command or HSM violation for
1829 * IO commands
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001830 *
Tejun Heo3884f7b2007-11-27 19:28:56 +09001831 * ECAT_UNK_DEV : Unknown DEV error for IO commands
Tejun Heo022bdb02006-05-15 20:58:22 +09001832 *
Tejun Heo76326ac2007-11-27 19:28:59 +09001833 * ECAT_DUBIOUS_* : Identical to above three but occurred while
1834 * data transfer hasn't been verified.
1835 *
Tejun Heo3884f7b2007-11-27 19:28:56 +09001836 * Verdicts are
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001837 *
Tejun Heo3884f7b2007-11-27 19:28:56 +09001838 * NCQ_OFF : Turn off NCQ.
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001839 *
Tejun Heo3884f7b2007-11-27 19:28:56 +09001840 * SPEED_DOWN : Speed down transfer speed but don't fall back
1841 * to PIO.
1842 *
1843 * FALLBACK_TO_PIO : Fall back to PIO.
1844 *
1845 * Even if multiple verdicts are returned, only one action is
Tejun Heo76326ac2007-11-27 19:28:59 +09001846 * taken per error. An action triggered by non-DUBIOUS errors
1847 * clears ering, while one triggered by DUBIOUS_* errors doesn't.
1848 * This is to expedite speed down decisions right after device is
1849 * initially configured.
Tejun Heo3884f7b2007-11-27 19:28:56 +09001850 *
Tejun Heo76326ac2007-11-27 19:28:59 +09001851 * The followings are speed down rules. #1 and #2 deal with
1852 * DUBIOUS errors.
1853 *
1854 * 1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors
1855 * occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO.
1856 *
1857 * 2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors
1858 * occurred during last 5 mins, NCQ_OFF.
1859 *
1860 * 3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors
Tejun Heo3884f7b2007-11-27 19:28:56 +09001861 * ocurred during last 5 mins, FALLBACK_TO_PIO
1862 *
Tejun Heo76326ac2007-11-27 19:28:59 +09001863 * 4. If more than 3 TOUT_HSM or UNK_DEV errors occurred
Tejun Heo3884f7b2007-11-27 19:28:56 +09001864 * during last 10 mins, NCQ_OFF.
1865 *
Tejun Heo76326ac2007-11-27 19:28:59 +09001866 * 5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6
Tejun Heo3884f7b2007-11-27 19:28:56 +09001867 * UNK_DEV errors occurred during last 10 mins, SPEED_DOWN.
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001868 *
Tejun Heo022bdb02006-05-15 20:58:22 +09001869 * LOCKING:
1870 * Inherited from caller.
1871 *
1872 * RETURNS:
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001873 * OR of ATA_EH_SPDN_* flags.
Tejun Heo022bdb02006-05-15 20:58:22 +09001874 */
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001875static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
Tejun Heo022bdb02006-05-15 20:58:22 +09001876{
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001877 const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
1878 u64 j64 = get_jiffies_64();
1879 struct speed_down_verdict_arg arg;
1880 unsigned int verdict = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09001881
Tejun Heo3884f7b2007-11-27 19:28:56 +09001882 /* scan past 5 mins of error history */
1883 memset(&arg, 0, sizeof(arg));
1884 arg.since = j64 - min(j64, j5mins);
1885 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
1886
Tejun Heo76326ac2007-11-27 19:28:59 +09001887 if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] +
1888 arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1)
1889 verdict |= ATA_EH_SPDN_SPEED_DOWN |
1890 ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS;
1891
1892 if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] +
1893 arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1)
1894 verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS;
1895
Tejun Heo3884f7b2007-11-27 19:28:56 +09001896 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1897 arg.nr_errors[ATA_ECAT_TOUT_HSM] +
Tejun Heo663f99b2007-11-27 19:28:57 +09001898 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
Tejun Heo3884f7b2007-11-27 19:28:56 +09001899 verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
1900
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001901 /* scan past 10 mins of error history */
Tejun Heo022bdb02006-05-15 20:58:22 +09001902 memset(&arg, 0, sizeof(arg));
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001903 arg.since = j64 - min(j64, j10mins);
1904 ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
Tejun Heo022bdb02006-05-15 20:58:22 +09001905
Tejun Heo3884f7b2007-11-27 19:28:56 +09001906 if (arg.nr_errors[ATA_ECAT_TOUT_HSM] +
1907 arg.nr_errors[ATA_ECAT_UNK_DEV] > 3)
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001908 verdict |= ATA_EH_SPDN_NCQ_OFF;
Tejun Heo3884f7b2007-11-27 19:28:56 +09001909
1910 if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
1911 arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 ||
Tejun Heo663f99b2007-11-27 19:28:57 +09001912 arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001913 verdict |= ATA_EH_SPDN_SPEED_DOWN;
Tejun Heo022bdb02006-05-15 20:58:22 +09001914
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001915 return verdict;
Tejun Heo022bdb02006-05-15 20:58:22 +09001916}
1917
1918/**
1919 * ata_eh_speed_down - record error and speed down if necessary
1920 * @dev: Failed device
Tejun Heo3884f7b2007-11-27 19:28:56 +09001921 * @eflags: mask of ATA_EFLAG_* flags
Tejun Heo022bdb02006-05-15 20:58:22 +09001922 * @err_mask: err_mask of the error
1923 *
1924 * Record error and examine error history to determine whether
1925 * adjusting transmission speed is necessary. It also sets
1926 * transmission limits appropriately if such adjustment is
1927 * necessary.
1928 *
1929 * LOCKING:
1930 * Kernel thread context (may sleep).
1931 *
1932 * RETURNS:
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001933 * Determined recovery action.
Tejun Heo022bdb02006-05-15 20:58:22 +09001934 */
Tejun Heo3884f7b2007-11-27 19:28:56 +09001935static unsigned int ata_eh_speed_down(struct ata_device *dev,
1936 unsigned int eflags, unsigned int err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09001937{
Tejun Heob1c72912008-07-31 17:02:43 +09001938 struct ata_link *link = ata_dev_phys_link(dev);
Tejun Heo76326ac2007-11-27 19:28:59 +09001939 int xfer_ok = 0;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001940 unsigned int verdict;
1941 unsigned int action = 0;
1942
1943 /* don't bother if Cat-0 error */
Tejun Heo76326ac2007-11-27 19:28:59 +09001944 if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0)
Tejun Heo022bdb02006-05-15 20:58:22 +09001945 return 0;
1946
1947 /* record error and determine whether speed down is necessary */
Tejun Heo3884f7b2007-11-27 19:28:56 +09001948 ata_ering_record(&dev->ering, eflags, err_mask);
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001949 verdict = ata_eh_speed_down_verdict(dev);
Tejun Heo022bdb02006-05-15 20:58:22 +09001950
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001951 /* turn off NCQ? */
1952 if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
1953 (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
1954 ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
1955 dev->flags |= ATA_DFLAG_NCQ_OFF;
1956 ata_dev_printk(dev, KERN_WARNING,
1957 "NCQ disabled due to excessive errors\n");
1958 goto done;
1959 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001960
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001961 /* speed down? */
1962 if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
1963 /* speed down SATA link speed if possible */
Tejun Heoa07d4992009-01-29 20:31:33 +09001964 if (sata_down_spd_limit(link, 0) == 0) {
Tejun Heocf480622008-01-24 00:05:14 +09001965 action |= ATA_EH_RESET;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001966 goto done;
1967 }
Tejun Heo022bdb02006-05-15 20:58:22 +09001968
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001969 /* lower transfer mode */
1970 if (dev->spdn_cnt < 2) {
1971 static const int dma_dnxfer_sel[] =
1972 { ATA_DNXFER_DMA, ATA_DNXFER_40C };
1973 static const int pio_dnxfer_sel[] =
1974 { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
1975 int sel;
Tejun Heo022bdb02006-05-15 20:58:22 +09001976
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001977 if (dev->xfer_shift != ATA_SHIFT_PIO)
1978 sel = dma_dnxfer_sel[dev->spdn_cnt];
1979 else
1980 sel = pio_dnxfer_sel[dev->spdn_cnt];
1981
1982 dev->spdn_cnt++;
1983
1984 if (ata_down_xfermask_limit(dev, sel) == 0) {
Tejun Heocf480622008-01-24 00:05:14 +09001985 action |= ATA_EH_RESET;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001986 goto done;
1987 }
1988 }
1989 }
1990
1991 /* Fall back to PIO? Slowing down to PIO is meaningless for
Tejun Heo663f99b2007-11-27 19:28:57 +09001992 * SATA ATA devices. Consider it only for PATA and SATAPI.
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001993 */
1994 if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
Tejun Heo663f99b2007-11-27 19:28:57 +09001995 (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) &&
Tejun Heo7d47e8d2007-02-02 16:22:31 +09001996 (dev->xfer_shift != ATA_SHIFT_PIO)) {
1997 if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
1998 dev->spdn_cnt = 0;
Tejun Heocf480622008-01-24 00:05:14 +09001999 action |= ATA_EH_RESET;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09002000 goto done;
2001 }
2002 }
2003
Tejun Heo022bdb02006-05-15 20:58:22 +09002004 return 0;
Tejun Heo7d47e8d2007-02-02 16:22:31 +09002005 done:
2006 /* device has been slowed down, blow error history */
Tejun Heo76326ac2007-11-27 19:28:59 +09002007 if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS))
2008 ata_ering_clear(&dev->ering);
Tejun Heo7d47e8d2007-02-02 16:22:31 +09002009 return action;
Tejun Heo022bdb02006-05-15 20:58:22 +09002010}
2011
2012/**
Tejun Heo9b1e2652007-08-06 18:36:24 +09002013 * ata_eh_link_autopsy - analyze error and determine recovery action
2014 * @link: host link to perform autopsy on
Tejun Heo022bdb02006-05-15 20:58:22 +09002015 *
Tejun Heo02607312007-08-06 18:36:23 +09002016 * Analyze why @link failed and determine which recovery actions
2017 * are needed. This function also sets more detailed AC_ERR_*
2018 * values and fills sense data for ATAPI CHECK SENSE.
Tejun Heo022bdb02006-05-15 20:58:22 +09002019 *
2020 * LOCKING:
2021 * Kernel thread context (may sleep).
2022 */
Tejun Heo9b1e2652007-08-06 18:36:24 +09002023static void ata_eh_link_autopsy(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09002024{
Tejun Heo02607312007-08-06 18:36:23 +09002025 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09002026 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heodfcc1732007-10-31 10:17:05 +09002027 struct ata_device *dev;
Tejun Heo3884f7b2007-11-27 19:28:56 +09002028 unsigned int all_err_mask = 0, eflags = 0;
2029 int tag;
Tejun Heo022bdb02006-05-15 20:58:22 +09002030 u32 serror;
2031 int rc;
2032
2033 DPRINTK("ENTER\n");
2034
Tejun Heo1cdaf532006-07-03 16:07:26 +09002035 if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
2036 return;
2037
Tejun Heo022bdb02006-05-15 20:58:22 +09002038 /* obtain and analyze SError */
Tejun Heo936fd732007-08-06 18:36:23 +09002039 rc = sata_scr_read(link, SCR_ERROR, &serror);
Tejun Heo022bdb02006-05-15 20:58:22 +09002040 if (rc == 0) {
2041 ehc->i.serror |= serror;
Tejun Heo02607312007-08-06 18:36:23 +09002042 ata_eh_analyze_serror(link);
Tejun Heo4e57c512007-07-16 14:29:41 +09002043 } else if (rc != -EOPNOTSUPP) {
Tejun Heocf480622008-01-24 00:05:14 +09002044 /* SError read failed, force reset and probing */
Tejun Heob558edd2008-01-24 00:05:14 +09002045 ehc->i.probe_mask |= ATA_ALL_DEVICES;
Tejun Heocf480622008-01-24 00:05:14 +09002046 ehc->i.action |= ATA_EH_RESET;
Tejun Heo4e57c512007-07-16 14:29:41 +09002047 ehc->i.err_mask |= AC_ERR_OTHER;
2048 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002049
Tejun Heoe8ee8452006-05-15 21:03:46 +09002050 /* analyze NCQ failure */
Tejun Heo02607312007-08-06 18:36:23 +09002051 ata_eh_analyze_ncq_error(link);
Tejun Heoe8ee8452006-05-15 21:03:46 +09002052
Tejun Heo022bdb02006-05-15 20:58:22 +09002053 /* any real error trumps AC_ERR_OTHER */
2054 if (ehc->i.err_mask & ~AC_ERR_OTHER)
2055 ehc->i.err_mask &= ~AC_ERR_OTHER;
2056
2057 all_err_mask |= ehc->i.err_mask;
2058
2059 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2060 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2061
Tejun Heob1c72912008-07-31 17:02:43 +09002062 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
2063 ata_dev_phys_link(qc->dev) != link)
Tejun Heo022bdb02006-05-15 20:58:22 +09002064 continue;
2065
2066 /* inherit upper level err_mask */
2067 qc->err_mask |= ehc->i.err_mask;
2068
Tejun Heo022bdb02006-05-15 20:58:22 +09002069 /* analyze TF */
Tejun Heo4528e4d2006-07-08 20:17:26 +09002070 ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
Tejun Heo022bdb02006-05-15 20:58:22 +09002071
2072 /* DEV errors are probably spurious in case of ATA_BUS error */
2073 if (qc->err_mask & AC_ERR_ATA_BUS)
2074 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
2075 AC_ERR_INVALID);
2076
2077 /* any real error trumps unknown error */
2078 if (qc->err_mask & ~AC_ERR_OTHER)
2079 qc->err_mask &= ~AC_ERR_OTHER;
2080
2081 /* SENSE_VALID trumps dev/unknown error and revalidation */
Tejun Heof90f0822007-10-26 16:12:41 +09002082 if (qc->flags & ATA_QCFLAG_SENSE_VALID)
Tejun Heo022bdb02006-05-15 20:58:22 +09002083 qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
Tejun Heo022bdb02006-05-15 20:58:22 +09002084
Tejun Heo03faab72008-03-27 19:14:24 +09002085 /* determine whether the command is worth retrying */
Tejun Heo534ead72010-01-14 16:18:09 +09002086 if (qc->flags & ATA_QCFLAG_IO ||
2087 (!(qc->err_mask & AC_ERR_INVALID) &&
2088 qc->err_mask != AC_ERR_DEV))
Tejun Heo03faab72008-03-27 19:14:24 +09002089 qc->flags |= ATA_QCFLAG_RETRY;
2090
Tejun Heo022bdb02006-05-15 20:58:22 +09002091 /* accumulate error info */
Tejun Heo4528e4d2006-07-08 20:17:26 +09002092 ehc->i.dev = qc->dev;
Tejun Heo022bdb02006-05-15 20:58:22 +09002093 all_err_mask |= qc->err_mask;
2094 if (qc->flags & ATA_QCFLAG_IO)
Tejun Heo3884f7b2007-11-27 19:28:56 +09002095 eflags |= ATA_EFLAG_IS_IO;
Tejun Heo022bdb02006-05-15 20:58:22 +09002096 }
2097
Tejun Heoa20f33f2006-05-16 12:58:24 +09002098 /* enforce default EH actions */
Tejun Heob51e9e52006-06-29 01:29:30 +09002099 if (ap->pflags & ATA_PFLAG_FROZEN ||
Tejun Heoa20f33f2006-05-16 12:58:24 +09002100 all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
Tejun Heocf480622008-01-24 00:05:14 +09002101 ehc->i.action |= ATA_EH_RESET;
Tejun Heo3884f7b2007-11-27 19:28:56 +09002102 else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) ||
2103 (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV)))
Tejun Heo4528e4d2006-07-08 20:17:26 +09002104 ehc->i.action |= ATA_EH_REVALIDATE;
Tejun Heo022bdb02006-05-15 20:58:22 +09002105
Tejun Heodfcc1732007-10-31 10:17:05 +09002106 /* If we have offending qcs and the associated failed device,
2107 * perform per-dev EH action only on the offending device.
2108 */
Tejun Heo4528e4d2006-07-08 20:17:26 +09002109 if (ehc->i.dev) {
Tejun Heo4528e4d2006-07-08 20:17:26 +09002110 ehc->i.dev_action[ehc->i.dev->devno] |=
2111 ehc->i.action & ATA_EH_PERDEV_MASK;
2112 ehc->i.action &= ~ATA_EH_PERDEV_MASK;
Tejun Heo47005f22006-06-19 18:27:23 +09002113 }
2114
Tejun Heo2695e362008-01-10 13:41:23 +09002115 /* propagate timeout to host link */
2116 if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link))
2117 ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT;
2118
2119 /* record error and consider speeding down */
Tejun Heodfcc1732007-10-31 10:17:05 +09002120 dev = ehc->i.dev;
Tejun Heo2695e362008-01-10 13:41:23 +09002121 if (!dev && ((ata_link_max_devices(link) == 1 &&
2122 ata_dev_enabled(link->device))))
2123 dev = link->device;
Tejun Heodfcc1732007-10-31 10:17:05 +09002124
Tejun Heo76326ac2007-11-27 19:28:59 +09002125 if (dev) {
2126 if (dev->flags & ATA_DFLAG_DUBIOUS_XFER)
2127 eflags |= ATA_EFLAG_DUBIOUS_XFER;
Tejun Heo3884f7b2007-11-27 19:28:56 +09002128 ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask);
Tejun Heo76326ac2007-11-27 19:28:59 +09002129 }
Tejun Heodfcc1732007-10-31 10:17:05 +09002130
Tejun Heo022bdb02006-05-15 20:58:22 +09002131 DPRINTK("EXIT\n");
2132}
2133
2134/**
Tejun Heo9b1e2652007-08-06 18:36:24 +09002135 * ata_eh_autopsy - analyze error and determine recovery action
2136 * @ap: host port to perform autopsy on
2137 *
2138 * Analyze all links of @ap and determine why they failed and
2139 * which recovery actions are needed.
2140 *
2141 * LOCKING:
2142 * Kernel thread context (may sleep).
2143 */
Tejun Heofb7fd612007-09-23 13:14:12 +09002144void ata_eh_autopsy(struct ata_port *ap)
Tejun Heo9b1e2652007-08-06 18:36:24 +09002145{
2146 struct ata_link *link;
2147
Tejun Heo1eca4362008-11-03 20:03:17 +09002148 ata_for_each_link(link, ap, EDGE)
Tejun Heo9b1e2652007-08-06 18:36:24 +09002149 ata_eh_link_autopsy(link);
Tejun Heo2695e362008-01-10 13:41:23 +09002150
Tejun Heob1c72912008-07-31 17:02:43 +09002151 /* Handle the frigging slave link. Autopsy is done similarly
2152 * but actions and flags are transferred over to the master
2153 * link and handled from there.
2154 */
2155 if (ap->slave_link) {
2156 struct ata_eh_context *mehc = &ap->link.eh_context;
2157 struct ata_eh_context *sehc = &ap->slave_link->eh_context;
2158
Tejun Heo848e4c62008-10-21 14:26:39 +09002159 /* transfer control flags from master to slave */
2160 sehc->i.flags |= mehc->i.flags & ATA_EHI_TO_SLAVE_MASK;
2161
2162 /* perform autopsy on the slave link */
Tejun Heob1c72912008-07-31 17:02:43 +09002163 ata_eh_link_autopsy(ap->slave_link);
2164
Tejun Heo848e4c62008-10-21 14:26:39 +09002165 /* transfer actions from slave to master and clear slave */
Tejun Heob1c72912008-07-31 17:02:43 +09002166 ata_eh_about_to_do(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
2167 mehc->i.action |= sehc->i.action;
2168 mehc->i.dev_action[1] |= sehc->i.dev_action[1];
2169 mehc->i.flags |= sehc->i.flags;
2170 ata_eh_done(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
2171 }
2172
Tejun Heo2695e362008-01-10 13:41:23 +09002173 /* Autopsy of fanout ports can affect host link autopsy.
2174 * Perform host link autopsy last.
2175 */
Tejun Heo071f44b2008-04-07 22:47:22 +09002176 if (sata_pmp_attached(ap))
Tejun Heo2695e362008-01-10 13:41:23 +09002177 ata_eh_link_autopsy(&ap->link);
Tejun Heo9b1e2652007-08-06 18:36:24 +09002178}
2179
2180/**
Robert Hancock65211482009-07-14 20:43:39 -06002181 * ata_get_cmd_descript - get description for ATA command
2182 * @command: ATA command code to get description for
2183 *
2184 * Return a textual description of the given command, or NULL if the
2185 * command is not known.
2186 *
2187 * LOCKING:
2188 * None
2189 */
2190const char *ata_get_cmd_descript(u8 command)
2191{
2192#ifdef CONFIG_ATA_VERBOSE_ERROR
2193 static const struct
2194 {
2195 u8 command;
2196 const char *text;
2197 } cmd_descr[] = {
2198 { ATA_CMD_DEV_RESET, "DEVICE RESET" },
2199 { ATA_CMD_CHK_POWER, "CHECK POWER MODE" },
2200 { ATA_CMD_STANDBY, "STANDBY" },
2201 { ATA_CMD_IDLE, "IDLE" },
2202 { ATA_CMD_EDD, "EXECUTE DEVICE DIAGNOSTIC" },
2203 { ATA_CMD_DOWNLOAD_MICRO, "DOWNLOAD MICROCODE" },
2204 { ATA_CMD_NOP, "NOP" },
2205 { ATA_CMD_FLUSH, "FLUSH CACHE" },
2206 { ATA_CMD_FLUSH_EXT, "FLUSH CACHE EXT" },
2207 { ATA_CMD_ID_ATA, "IDENTIFY DEVICE" },
2208 { ATA_CMD_ID_ATAPI, "IDENTIFY PACKET DEVICE" },
2209 { ATA_CMD_SERVICE, "SERVICE" },
2210 { ATA_CMD_READ, "READ DMA" },
2211 { ATA_CMD_READ_EXT, "READ DMA EXT" },
2212 { ATA_CMD_READ_QUEUED, "READ DMA QUEUED" },
2213 { ATA_CMD_READ_STREAM_EXT, "READ STREAM EXT" },
2214 { ATA_CMD_READ_STREAM_DMA_EXT, "READ STREAM DMA EXT" },
2215 { ATA_CMD_WRITE, "WRITE DMA" },
2216 { ATA_CMD_WRITE_EXT, "WRITE DMA EXT" },
2217 { ATA_CMD_WRITE_QUEUED, "WRITE DMA QUEUED EXT" },
2218 { ATA_CMD_WRITE_STREAM_EXT, "WRITE STREAM EXT" },
2219 { ATA_CMD_WRITE_STREAM_DMA_EXT, "WRITE STREAM DMA EXT" },
2220 { ATA_CMD_WRITE_FUA_EXT, "WRITE DMA FUA EXT" },
2221 { ATA_CMD_WRITE_QUEUED_FUA_EXT, "WRITE DMA QUEUED FUA EXT" },
2222 { ATA_CMD_FPDMA_READ, "READ FPDMA QUEUED" },
2223 { ATA_CMD_FPDMA_WRITE, "WRITE FPDMA QUEUED" },
2224 { ATA_CMD_PIO_READ, "READ SECTOR(S)" },
2225 { ATA_CMD_PIO_READ_EXT, "READ SECTOR(S) EXT" },
2226 { ATA_CMD_PIO_WRITE, "WRITE SECTOR(S)" },
2227 { ATA_CMD_PIO_WRITE_EXT, "WRITE SECTOR(S) EXT" },
2228 { ATA_CMD_READ_MULTI, "READ MULTIPLE" },
2229 { ATA_CMD_READ_MULTI_EXT, "READ MULTIPLE EXT" },
2230 { ATA_CMD_WRITE_MULTI, "WRITE MULTIPLE" },
2231 { ATA_CMD_WRITE_MULTI_EXT, "WRITE MULTIPLE EXT" },
2232 { ATA_CMD_WRITE_MULTI_FUA_EXT, "WRITE MULTIPLE FUA EXT" },
2233 { ATA_CMD_SET_FEATURES, "SET FEATURES" },
2234 { ATA_CMD_SET_MULTI, "SET MULTIPLE MODE" },
2235 { ATA_CMD_VERIFY, "READ VERIFY SECTOR(S)" },
2236 { ATA_CMD_VERIFY_EXT, "READ VERIFY SECTOR(S) EXT" },
2237 { ATA_CMD_WRITE_UNCORR_EXT, "WRITE UNCORRECTABLE EXT" },
2238 { ATA_CMD_STANDBYNOW1, "STANDBY IMMEDIATE" },
2239 { ATA_CMD_IDLEIMMEDIATE, "IDLE IMMEDIATE" },
2240 { ATA_CMD_SLEEP, "SLEEP" },
2241 { ATA_CMD_INIT_DEV_PARAMS, "INITIALIZE DEVICE PARAMETERS" },
2242 { ATA_CMD_READ_NATIVE_MAX, "READ NATIVE MAX ADDRESS" },
2243 { ATA_CMD_READ_NATIVE_MAX_EXT, "READ NATIVE MAX ADDRESS EXT" },
2244 { ATA_CMD_SET_MAX, "SET MAX ADDRESS" },
2245 { ATA_CMD_SET_MAX_EXT, "SET MAX ADDRESS EXT" },
2246 { ATA_CMD_READ_LOG_EXT, "READ LOG EXT" },
2247 { ATA_CMD_WRITE_LOG_EXT, "WRITE LOG EXT" },
2248 { ATA_CMD_READ_LOG_DMA_EXT, "READ LOG DMA EXT" },
2249 { ATA_CMD_WRITE_LOG_DMA_EXT, "WRITE LOG DMA EXT" },
2250 { ATA_CMD_TRUSTED_RCV, "TRUSTED RECEIVE" },
2251 { ATA_CMD_TRUSTED_RCV_DMA, "TRUSTED RECEIVE DMA" },
2252 { ATA_CMD_TRUSTED_SND, "TRUSTED SEND" },
2253 { ATA_CMD_TRUSTED_SND_DMA, "TRUSTED SEND DMA" },
2254 { ATA_CMD_PMP_READ, "READ BUFFER" },
2255 { ATA_CMD_PMP_WRITE, "WRITE BUFFER" },
2256 { ATA_CMD_CONF_OVERLAY, "DEVICE CONFIGURATION OVERLAY" },
2257 { ATA_CMD_SEC_SET_PASS, "SECURITY SET PASSWORD" },
2258 { ATA_CMD_SEC_UNLOCK, "SECURITY UNLOCK" },
2259 { ATA_CMD_SEC_ERASE_PREP, "SECURITY ERASE PREPARE" },
2260 { ATA_CMD_SEC_ERASE_UNIT, "SECURITY ERASE UNIT" },
2261 { ATA_CMD_SEC_FREEZE_LOCK, "SECURITY FREEZE LOCK" },
2262 { ATA_CMD_SEC_DISABLE_PASS, "SECURITY DISABLE PASSWORD" },
2263 { ATA_CMD_CONFIG_STREAM, "CONFIGURE STREAM" },
2264 { ATA_CMD_SMART, "SMART" },
2265 { ATA_CMD_MEDIA_LOCK, "DOOR LOCK" },
2266 { ATA_CMD_MEDIA_UNLOCK, "DOOR UNLOCK" },
FUJITA Tomonoriacad762722010-07-05 15:45:20 +09002267 { ATA_CMD_DSM, "DATA SET MANAGEMENT" },
Robert Hancock65211482009-07-14 20:43:39 -06002268 { ATA_CMD_CHK_MED_CRD_TYP, "CHECK MEDIA CARD TYPE" },
2269 { ATA_CMD_CFA_REQ_EXT_ERR, "CFA REQUEST EXTENDED ERROR" },
2270 { ATA_CMD_CFA_WRITE_NE, "CFA WRITE SECTORS WITHOUT ERASE" },
2271 { ATA_CMD_CFA_TRANS_SECT, "CFA TRANSLATE SECTOR" },
2272 { ATA_CMD_CFA_ERASE, "CFA ERASE SECTORS" },
2273 { ATA_CMD_CFA_WRITE_MULT_NE, "CFA WRITE MULTIPLE WITHOUT ERASE" },
2274 { ATA_CMD_READ_LONG, "READ LONG (with retries)" },
2275 { ATA_CMD_READ_LONG_ONCE, "READ LONG (without retries)" },
2276 { ATA_CMD_WRITE_LONG, "WRITE LONG (with retries)" },
2277 { ATA_CMD_WRITE_LONG_ONCE, "WRITE LONG (without retries)" },
2278 { ATA_CMD_RESTORE, "RECALIBRATE" },
2279 { 0, NULL } /* terminate list */
2280 };
2281
2282 unsigned int i;
2283 for (i = 0; cmd_descr[i].text; i++)
2284 if (cmd_descr[i].command == command)
2285 return cmd_descr[i].text;
2286#endif
2287
2288 return NULL;
2289}
2290
2291/**
Tejun Heo9b1e2652007-08-06 18:36:24 +09002292 * ata_eh_link_report - report error handling to user
Tejun Heo02607312007-08-06 18:36:23 +09002293 * @link: ATA link EH is going on
Tejun Heo022bdb02006-05-15 20:58:22 +09002294 *
2295 * Report EH to user.
2296 *
2297 * LOCKING:
2298 * None.
2299 */
Tejun Heo9b1e2652007-08-06 18:36:24 +09002300static void ata_eh_link_report(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09002301{
Tejun Heo02607312007-08-06 18:36:23 +09002302 struct ata_port *ap = link->ap;
2303 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09002304 const char *frozen, *desc;
Tejun Heoa1e10f72007-08-18 13:28:49 +09002305 char tries_buf[6];
Tejun Heo022bdb02006-05-15 20:58:22 +09002306 int tag, nr_failed = 0;
2307
Tejun Heo94ff3d52007-10-09 14:57:56 +09002308 if (ehc->i.flags & ATA_EHI_QUIET)
2309 return;
2310
Tejun Heo022bdb02006-05-15 20:58:22 +09002311 desc = NULL;
2312 if (ehc->i.desc[0] != '\0')
2313 desc = ehc->i.desc;
2314
2315 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2316 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
2317
Tejun Heob1c72912008-07-31 17:02:43 +09002318 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
2319 ata_dev_phys_link(qc->dev) != link ||
Tejun Heoe027bd32007-10-26 16:19:26 +09002320 ((qc->flags & ATA_QCFLAG_QUIET) &&
2321 qc->err_mask == AC_ERR_DEV))
Tejun Heo022bdb02006-05-15 20:58:22 +09002322 continue;
2323 if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
2324 continue;
2325
2326 nr_failed++;
2327 }
2328
2329 if (!nr_failed && !ehc->i.err_mask)
2330 return;
2331
2332 frozen = "";
Tejun Heob51e9e52006-06-29 01:29:30 +09002333 if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heo022bdb02006-05-15 20:58:22 +09002334 frozen = " frozen";
2335
Tejun Heoa1e10f72007-08-18 13:28:49 +09002336 memset(tries_buf, 0, sizeof(tries_buf));
2337 if (ap->eh_tries < ATA_EH_MAX_TRIES)
2338 snprintf(tries_buf, sizeof(tries_buf) - 1, " t%d",
2339 ap->eh_tries);
2340
Tejun Heo022bdb02006-05-15 20:58:22 +09002341 if (ehc->i.dev) {
Tejun Heoe8ee8452006-05-15 21:03:46 +09002342 ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
Tejun Heoa1e10f72007-08-18 13:28:49 +09002343 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2344 ehc->i.err_mask, link->sactive, ehc->i.serror,
2345 ehc->i.action, frozen, tries_buf);
Tejun Heo022bdb02006-05-15 20:58:22 +09002346 if (desc)
Tejun Heob64bbc32007-07-16 14:29:39 +09002347 ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
Tejun Heo022bdb02006-05-15 20:58:22 +09002348 } else {
Tejun Heo02607312007-08-06 18:36:23 +09002349 ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
Tejun Heoa1e10f72007-08-18 13:28:49 +09002350 "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
2351 ehc->i.err_mask, link->sactive, ehc->i.serror,
2352 ehc->i.action, frozen, tries_buf);
Tejun Heo022bdb02006-05-15 20:58:22 +09002353 if (desc)
Tejun Heo02607312007-08-06 18:36:23 +09002354 ata_link_printk(link, KERN_ERR, "%s\n", desc);
Tejun Heo022bdb02006-05-15 20:58:22 +09002355 }
2356
Robert Hancock65211482009-07-14 20:43:39 -06002357#ifdef CONFIG_ATA_VERBOSE_ERROR
Robert Hancock1333e192007-10-02 11:22:02 -04002358 if (ehc->i.serror)
Tejun Heoda0e21d2008-07-31 16:08:38 +09002359 ata_link_printk(link, KERN_ERR,
Robert Hancock1333e192007-10-02 11:22:02 -04002360 "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
2361 ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
2362 ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
2363 ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
2364 ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
2365 ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
2366 ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
2367 ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
2368 ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
2369 ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
2370 ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
2371 ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
2372 ehc->i.serror & SERR_CRC ? "BadCRC " : "",
2373 ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
2374 ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
2375 ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
2376 ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002377 ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
Robert Hancock65211482009-07-14 20:43:39 -06002378#endif
Robert Hancock1333e192007-10-02 11:22:02 -04002379
Tejun Heo022bdb02006-05-15 20:58:22 +09002380 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
2381 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
Tejun Heo8a937582006-11-14 22:36:12 +09002382 struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
Tejun Heoabb6a882007-11-28 23:16:09 +09002383 const u8 *cdb = qc->cdb;
2384 char data_buf[20] = "";
2385 char cdb_buf[70] = "";
Tejun Heo022bdb02006-05-15 20:58:22 +09002386
Tejun Heo02607312007-08-06 18:36:23 +09002387 if (!(qc->flags & ATA_QCFLAG_FAILED) ||
Tejun Heob1c72912008-07-31 17:02:43 +09002388 ata_dev_phys_link(qc->dev) != link || !qc->err_mask)
Tejun Heo022bdb02006-05-15 20:58:22 +09002389 continue;
2390
Tejun Heoabb6a882007-11-28 23:16:09 +09002391 if (qc->dma_dir != DMA_NONE) {
2392 static const char *dma_str[] = {
2393 [DMA_BIDIRECTIONAL] = "bidi",
2394 [DMA_TO_DEVICE] = "out",
2395 [DMA_FROM_DEVICE] = "in",
2396 };
2397 static const char *prot_str[] = {
2398 [ATA_PROT_PIO] = "pio",
2399 [ATA_PROT_DMA] = "dma",
2400 [ATA_PROT_NCQ] = "ncq",
Tejun Heo0dc36882007-12-18 16:34:43 -05002401 [ATAPI_PROT_PIO] = "pio",
2402 [ATAPI_PROT_DMA] = "dma",
Tejun Heoabb6a882007-11-28 23:16:09 +09002403 };
2404
2405 snprintf(data_buf, sizeof(data_buf), " %s %u %s",
2406 prot_str[qc->tf.protocol], qc->nbytes,
2407 dma_str[qc->dma_dir]);
2408 }
2409
Robert Hancock65211482009-07-14 20:43:39 -06002410 if (ata_is_atapi(qc->tf.protocol)) {
2411 if (qc->scsicmd)
2412 scsi_print_command(qc->scsicmd);
2413 else
2414 snprintf(cdb_buf, sizeof(cdb_buf),
Tejun Heoabb6a882007-11-28 23:16:09 +09002415 "cdb %02x %02x %02x %02x %02x %02x %02x %02x "
2416 "%02x %02x %02x %02x %02x %02x %02x %02x\n ",
2417 cdb[0], cdb[1], cdb[2], cdb[3],
2418 cdb[4], cdb[5], cdb[6], cdb[7],
2419 cdb[8], cdb[9], cdb[10], cdb[11],
2420 cdb[12], cdb[13], cdb[14], cdb[15]);
Robert Hancock65211482009-07-14 20:43:39 -06002421 } else {
2422 const char *descr = ata_get_cmd_descript(cmd->command);
2423 if (descr)
2424 ata_dev_printk(qc->dev, KERN_ERR,
2425 "failed command: %s\n", descr);
2426 }
Tejun Heoabb6a882007-11-28 23:16:09 +09002427
Tejun Heo8a937582006-11-14 22:36:12 +09002428 ata_dev_printk(qc->dev, KERN_ERR,
2429 "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
Tejun Heoabb6a882007-11-28 23:16:09 +09002430 "tag %d%s\n %s"
Tejun Heo8a937582006-11-14 22:36:12 +09002431 "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
Tejun Heo5335b722007-07-16 14:29:40 +09002432 "Emask 0x%x (%s)%s\n",
Tejun Heo8a937582006-11-14 22:36:12 +09002433 cmd->command, cmd->feature, cmd->nsect,
2434 cmd->lbal, cmd->lbam, cmd->lbah,
2435 cmd->hob_feature, cmd->hob_nsect,
2436 cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
Tejun Heoabb6a882007-11-28 23:16:09 +09002437 cmd->device, qc->tag, data_buf, cdb_buf,
Tejun Heo8a937582006-11-14 22:36:12 +09002438 res->command, res->feature, res->nsect,
2439 res->lbal, res->lbam, res->lbah,
2440 res->hob_feature, res->hob_nsect,
2441 res->hob_lbal, res->hob_lbam, res->hob_lbah,
Tejun Heo5335b722007-07-16 14:29:40 +09002442 res->device, qc->err_mask, ata_err_string(qc->err_mask),
2443 qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
Robert Hancock1333e192007-10-02 11:22:02 -04002444
Robert Hancock65211482009-07-14 20:43:39 -06002445#ifdef CONFIG_ATA_VERBOSE_ERROR
Robert Hancock1333e192007-10-02 11:22:02 -04002446 if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002447 ATA_ERR)) {
Robert Hancock1333e192007-10-02 11:22:02 -04002448 if (res->command & ATA_BUSY)
2449 ata_dev_printk(qc->dev, KERN_ERR,
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002450 "status: { Busy }\n");
Robert Hancock1333e192007-10-02 11:22:02 -04002451 else
2452 ata_dev_printk(qc->dev, KERN_ERR,
2453 "status: { %s%s%s%s}\n",
2454 res->command & ATA_DRDY ? "DRDY " : "",
2455 res->command & ATA_DF ? "DF " : "",
2456 res->command & ATA_DRQ ? "DRQ " : "",
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002457 res->command & ATA_ERR ? "ERR " : "");
Robert Hancock1333e192007-10-02 11:22:02 -04002458 }
2459
2460 if (cmd->command != ATA_CMD_PACKET &&
2461 (res->feature & (ATA_ICRC | ATA_UNC | ATA_IDNF |
2462 ATA_ABORTED)))
2463 ata_dev_printk(qc->dev, KERN_ERR,
2464 "error: { %s%s%s%s}\n",
2465 res->feature & ATA_ICRC ? "ICRC " : "",
2466 res->feature & ATA_UNC ? "UNC " : "",
2467 res->feature & ATA_IDNF ? "IDNF " : "",
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002468 res->feature & ATA_ABORTED ? "ABRT " : "");
Robert Hancock65211482009-07-14 20:43:39 -06002469#endif
Tejun Heo022bdb02006-05-15 20:58:22 +09002470 }
2471}
2472
Tejun Heo9b1e2652007-08-06 18:36:24 +09002473/**
2474 * ata_eh_report - report error handling to user
2475 * @ap: ATA port to report EH about
2476 *
2477 * Report EH to user.
2478 *
2479 * LOCKING:
2480 * None.
2481 */
Tejun Heofb7fd612007-09-23 13:14:12 +09002482void ata_eh_report(struct ata_port *ap)
Tejun Heo9b1e2652007-08-06 18:36:24 +09002483{
2484 struct ata_link *link;
2485
Tejun Heo1eca4362008-11-03 20:03:17 +09002486 ata_for_each_link(link, ap, HOST_FIRST)
Tejun Heo9b1e2652007-08-06 18:36:24 +09002487 ata_eh_link_report(link);
2488}
2489
Tejun Heocc0680a2007-08-06 18:36:23 +09002490static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
Tejun Heob1c72912008-07-31 17:02:43 +09002491 unsigned int *classes, unsigned long deadline,
2492 bool clear_classes)
Tejun Heod87fa382006-05-31 18:28:24 +09002493{
Tejun Heof58229f2007-08-06 18:36:23 +09002494 struct ata_device *dev;
Tejun Heod87fa382006-05-31 18:28:24 +09002495
Tejun Heob1c72912008-07-31 17:02:43 +09002496 if (clear_classes)
Tejun Heo1eca4362008-11-03 20:03:17 +09002497 ata_for_each_dev(dev, link, ALL)
Tejun Heob1c72912008-07-31 17:02:43 +09002498 classes[dev->devno] = ATA_DEV_UNKNOWN;
Tejun Heod87fa382006-05-31 18:28:24 +09002499
Tejun Heof0465192008-05-19 01:15:08 +09002500 return reset(link, classes, deadline);
Tejun Heod87fa382006-05-31 18:28:24 +09002501}
2502
Tejun Heoae791c02007-09-23 13:14:12 +09002503static int ata_eh_followup_srst_needed(struct ata_link *link,
Tejun Heo5dbfc9c2008-07-31 16:08:02 +09002504 int rc, const unsigned int *classes)
Tejun Heo664faf02006-05-31 18:27:50 +09002505{
Tejun Heo45db2f62008-04-08 01:46:56 +09002506 if ((link->flags & ATA_LFLAG_NO_SRST) || ata_link_offline(link))
Tejun Heoae791c02007-09-23 13:14:12 +09002507 return 0;
Tejun Heo5dbfc9c2008-07-31 16:08:02 +09002508 if (rc == -EAGAIN)
2509 return 1;
Tejun Heo071f44b2008-04-07 22:47:22 +09002510 if (sata_pmp_supported(link->ap) && ata_is_host_link(link))
Tejun Heo3495de72007-09-23 13:19:53 +09002511 return 1;
Tejun Heo664faf02006-05-31 18:27:50 +09002512 return 0;
2513}
2514
Tejun Heofb7fd612007-09-23 13:14:12 +09002515int ata_eh_reset(struct ata_link *link, int classify,
2516 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
2517 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
Tejun Heo022bdb02006-05-15 20:58:22 +09002518{
Tejun Heoafaa5c32007-10-09 15:06:10 +09002519 struct ata_port *ap = link->ap;
Tejun Heob1c72912008-07-31 17:02:43 +09002520 struct ata_link *slave = ap->slave_link;
Tejun Heo936fd732007-08-06 18:36:23 +09002521 struct ata_eh_context *ehc = &link->eh_context;
Bartlomiej Zolnierkiewicz705d2012009-07-26 16:21:01 +02002522 struct ata_eh_context *sehc = slave ? &slave->eh_context : NULL;
Tejun Heo664faf02006-05-31 18:27:50 +09002523 unsigned int *classes = ehc->classes;
Tejun Heo416dc9e2007-10-31 10:17:03 +09002524 unsigned int lflags = link->flags;
Tejun Heo1cdaf532006-07-03 16:07:26 +09002525 int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
Tejun Heod8af0eb2008-05-20 02:17:53 +09002526 int max_tries = 0, try = 0;
Tejun Heob1c72912008-07-31 17:02:43 +09002527 struct ata_link *failed_link;
Tejun Heof58229f2007-08-06 18:36:23 +09002528 struct ata_device *dev;
Tejun Heo416dc9e2007-10-31 10:17:03 +09002529 unsigned long deadline, now;
Tejun Heo022bdb02006-05-15 20:58:22 +09002530 ata_reset_fn_t reset;
Tejun Heoafaa5c32007-10-09 15:06:10 +09002531 unsigned long flags;
Tejun Heo416dc9e2007-10-31 10:17:03 +09002532 u32 sstatus;
Tejun Heob1c72912008-07-31 17:02:43 +09002533 int nr_unknown, rc;
Tejun Heo022bdb02006-05-15 20:58:22 +09002534
Tejun Heo932648b2008-05-19 01:15:06 +09002535 /*
2536 * Prepare to reset
2537 */
Tejun Heod8af0eb2008-05-20 02:17:53 +09002538 while (ata_eh_reset_timeouts[max_tries] != ULONG_MAX)
2539 max_tries++;
Tejun Heo05944bd2008-08-13 20:19:09 +09002540 if (link->flags & ATA_LFLAG_NO_HRST)
2541 hardreset = NULL;
2542 if (link->flags & ATA_LFLAG_NO_SRST)
2543 softreset = NULL;
Tejun Heod8af0eb2008-05-20 02:17:53 +09002544
Tejun Heo19b72322008-11-04 17:08:40 +09002545 /* make sure each reset attemp is at least COOL_DOWN apart */
2546 if (ehc->i.flags & ATA_EHI_DID_RESET) {
2547 now = jiffies;
2548 WARN_ON(time_after(ehc->last_reset, now));
2549 deadline = ata_deadline(ehc->last_reset,
2550 ATA_EH_RESET_COOL_DOWN);
2551 if (time_before(now, deadline))
2552 schedule_timeout_uninterruptible(deadline - now);
2553 }
Tejun Heo0a2c0f52008-05-20 02:17:52 +09002554
Tejun Heoafaa5c32007-10-09 15:06:10 +09002555 spin_lock_irqsave(ap->lock, flags);
2556 ap->pflags |= ATA_PFLAG_RESETTING;
2557 spin_unlock_irqrestore(ap->lock, flags);
2558
Tejun Heocf480622008-01-24 00:05:14 +09002559 ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
Tejun Heo13abf502006-07-10 23:18:46 +09002560
Tejun Heo1eca4362008-11-03 20:03:17 +09002561 ata_for_each_dev(dev, link, ALL) {
Tejun Heocdeab112007-10-29 16:41:09 +09002562 /* If we issue an SRST then an ATA drive (not ATAPI)
2563 * may change configuration and be in PIO0 timing. If
2564 * we do a hard reset (or are coming from power on)
2565 * this is true for ATA or ATAPI. Until we've set a
2566 * suitable controller mode we should not touch the
2567 * bus as we may be talking too fast.
2568 */
2569 dev->pio_mode = XFER_PIO_0;
2570
2571 /* If the controller has a pio mode setup function
2572 * then use it to set the chipset to rights. Don't
2573 * touch the DMA setup as that will be dealt with when
2574 * configuring devices.
2575 */
2576 if (ap->ops->set_piomode)
2577 ap->ops->set_piomode(ap, dev);
2578 }
2579
Tejun Heocf480622008-01-24 00:05:14 +09002580 /* prefer hardreset */
Tejun Heo932648b2008-05-19 01:15:06 +09002581 reset = NULL;
Tejun Heocf480622008-01-24 00:05:14 +09002582 ehc->i.action &= ~ATA_EH_RESET;
2583 if (hardreset) {
2584 reset = hardreset;
Tejun Heoa6740502008-07-31 16:07:04 +09002585 ehc->i.action |= ATA_EH_HARDRESET;
Tejun Heo4f7faa32008-01-30 18:18:26 +09002586 } else if (softreset) {
Tejun Heocf480622008-01-24 00:05:14 +09002587 reset = softreset;
Tejun Heoa6740502008-07-31 16:07:04 +09002588 ehc->i.action |= ATA_EH_SOFTRESET;
Tejun Heocf480622008-01-24 00:05:14 +09002589 }
Tejun Heof5914a42006-05-31 18:27:48 +09002590
2591 if (prereset) {
Tejun Heob1c72912008-07-31 17:02:43 +09002592 unsigned long deadline = ata_deadline(jiffies,
2593 ATA_EH_PRERESET_TIMEOUT);
2594
2595 if (slave) {
2596 sehc->i.action &= ~ATA_EH_RESET;
2597 sehc->i.action |= ehc->i.action;
2598 }
2599
2600 rc = prereset(link, deadline);
2601
2602 /* If present, do prereset on slave link too. Reset
2603 * is skipped iff both master and slave links report
2604 * -ENOENT or clear ATA_EH_RESET.
2605 */
2606 if (slave && (rc == 0 || rc == -ENOENT)) {
2607 int tmp;
2608
2609 tmp = prereset(slave, deadline);
2610 if (tmp != -ENOENT)
2611 rc = tmp;
2612
2613 ehc->i.action |= sehc->i.action;
2614 }
2615
Tejun Heof5914a42006-05-31 18:27:48 +09002616 if (rc) {
Alan Coxc9619222006-09-26 17:53:38 +01002617 if (rc == -ENOENT) {
Tejun Heocc0680a2007-08-06 18:36:23 +09002618 ata_link_printk(link, KERN_DEBUG,
Tejun Heo4aa9ab62007-03-12 17:24:08 +09002619 "port disabled. ignoring.\n");
Tejun Heocf480622008-01-24 00:05:14 +09002620 ehc->i.action &= ~ATA_EH_RESET;
Tejun Heo4aa9ab62007-03-12 17:24:08 +09002621
Tejun Heo1eca4362008-11-03 20:03:17 +09002622 ata_for_each_dev(dev, link, ALL)
Tejun Heof58229f2007-08-06 18:36:23 +09002623 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heo4aa9ab62007-03-12 17:24:08 +09002624
2625 rc = 0;
Alan Coxc9619222006-09-26 17:53:38 +01002626 } else
Tejun Heocc0680a2007-08-06 18:36:23 +09002627 ata_link_printk(link, KERN_ERR,
Tejun Heof5914a42006-05-31 18:27:48 +09002628 "prereset failed (errno=%d)\n", rc);
Tejun Heofccb6ea2007-07-16 14:29:41 +09002629 goto out;
Tejun Heof5914a42006-05-31 18:27:48 +09002630 }
Tejun Heof5914a42006-05-31 18:27:48 +09002631
Tejun Heo932648b2008-05-19 01:15:06 +09002632 /* prereset() might have cleared ATA_EH_RESET. If so,
Tejun Heod6515e62009-03-04 15:59:30 +09002633 * bang classes, thaw and return.
Tejun Heo932648b2008-05-19 01:15:06 +09002634 */
2635 if (reset && !(ehc->i.action & ATA_EH_RESET)) {
Tejun Heo1eca4362008-11-03 20:03:17 +09002636 ata_for_each_dev(dev, link, ALL)
Tejun Heo932648b2008-05-19 01:15:06 +09002637 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heod6515e62009-03-04 15:59:30 +09002638 if ((ap->pflags & ATA_PFLAG_FROZEN) &&
2639 ata_is_host_link(link))
2640 ata_eh_thaw_port(ap);
Tejun Heo932648b2008-05-19 01:15:06 +09002641 rc = 0;
2642 goto out;
2643 }
Tejun Heof5914a42006-05-31 18:27:48 +09002644 }
2645
Tejun Heo022bdb02006-05-15 20:58:22 +09002646 retry:
Tejun Heo932648b2008-05-19 01:15:06 +09002647 /*
2648 * Perform reset
2649 */
Tejun Heodc98c322008-05-19 01:15:07 +09002650 if (ata_is_host_link(link))
2651 ata_eh_freeze_port(ap);
2652
Tejun Heo341c2c92008-05-20 02:17:51 +09002653 deadline = ata_deadline(jiffies, ata_eh_reset_timeouts[try++]);
Tejun Heo31daabd2007-02-02 16:50:52 +09002654
Tejun Heo932648b2008-05-19 01:15:06 +09002655 if (reset) {
2656 if (verbose)
2657 ata_link_printk(link, KERN_INFO, "%s resetting link\n",
2658 reset == softreset ? "soft" : "hard");
Tejun Heo022bdb02006-05-15 20:58:22 +09002659
Tejun Heo932648b2008-05-19 01:15:06 +09002660 /* mark that this EH session started with reset */
Tejun Heo19b72322008-11-04 17:08:40 +09002661 ehc->last_reset = jiffies;
Tejun Heo932648b2008-05-19 01:15:06 +09002662 if (reset == hardreset)
2663 ehc->i.flags |= ATA_EHI_DID_HARDRESET;
2664 else
2665 ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
Tejun Heo022bdb02006-05-15 20:58:22 +09002666
Tejun Heob1c72912008-07-31 17:02:43 +09002667 rc = ata_do_reset(link, reset, classes, deadline, true);
2668 if (rc && rc != -EAGAIN) {
2669 failed_link = link;
Tejun Heo5dbfc9c2008-07-31 16:08:02 +09002670 goto fail;
Tejun Heob1c72912008-07-31 17:02:43 +09002671 }
Tejun Heo022bdb02006-05-15 20:58:22 +09002672
Tejun Heob1c72912008-07-31 17:02:43 +09002673 /* hardreset slave link if existent */
2674 if (slave && reset == hardreset) {
2675 int tmp;
2676
2677 if (verbose)
2678 ata_link_printk(slave, KERN_INFO,
2679 "hard resetting link\n");
2680
2681 ata_eh_about_to_do(slave, NULL, ATA_EH_RESET);
2682 tmp = ata_do_reset(slave, reset, classes, deadline,
2683 false);
2684 switch (tmp) {
2685 case -EAGAIN:
2686 rc = -EAGAIN;
2687 case 0:
2688 break;
2689 default:
2690 failed_link = slave;
2691 rc = tmp;
2692 goto fail;
2693 }
2694 }
2695
2696 /* perform follow-up SRST if necessary */
Tejun Heo932648b2008-05-19 01:15:06 +09002697 if (reset == hardreset &&
Tejun Heo5dbfc9c2008-07-31 16:08:02 +09002698 ata_eh_followup_srst_needed(link, rc, classes)) {
Tejun Heo932648b2008-05-19 01:15:06 +09002699 reset = softreset;
Tejun Heo664faf02006-05-31 18:27:50 +09002700
Tejun Heo932648b2008-05-19 01:15:06 +09002701 if (!reset) {
2702 ata_link_printk(link, KERN_ERR,
2703 "follow-up softreset required "
2704 "but no softreset avaliable\n");
Tejun Heob1c72912008-07-31 17:02:43 +09002705 failed_link = link;
Tejun Heo932648b2008-05-19 01:15:06 +09002706 rc = -EINVAL;
2707 goto fail;
2708 }
2709
2710 ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
Tejun Heob1c72912008-07-31 17:02:43 +09002711 rc = ata_do_reset(link, reset, classes, deadline, true);
Tejun Heofe2c4d02009-07-08 12:16:37 +09002712 if (rc) {
2713 failed_link = link;
2714 goto fail;
2715 }
Tejun Heo664faf02006-05-31 18:27:50 +09002716 }
Tejun Heo932648b2008-05-19 01:15:06 +09002717 } else {
2718 if (verbose)
2719 ata_link_printk(link, KERN_INFO, "no reset method "
2720 "available, skipping reset\n");
2721 if (!(lflags & ATA_LFLAG_ASSUME_CLASS))
2722 lflags |= ATA_LFLAG_ASSUME_ATA;
Tejun Heo664faf02006-05-31 18:27:50 +09002723 }
2724
Tejun Heo932648b2008-05-19 01:15:06 +09002725 /*
2726 * Post-reset processing
2727 */
Tejun Heo1eca4362008-11-03 20:03:17 +09002728 ata_for_each_dev(dev, link, ALL) {
Tejun Heo416dc9e2007-10-31 10:17:03 +09002729 /* After the reset, the device state is PIO 0 and the
2730 * controller state is undefined. Reset also wakes up
2731 * drives from sleeping mode.
2732 */
2733 dev->pio_mode = XFER_PIO_0;
2734 dev->flags &= ~ATA_DFLAG_SLEEPING;
Tejun Heo664faf02006-05-31 18:27:50 +09002735
Tejun Heo3b761d32009-10-06 17:08:40 +09002736 if (ata_phys_link_offline(ata_dev_phys_link(dev)))
2737 continue;
2738
2739 /* apply class override */
2740 if (lflags & ATA_LFLAG_ASSUME_ATA)
2741 classes[dev->devno] = ATA_DEV_ATA;
2742 else if (lflags & ATA_LFLAG_ASSUME_SEMB)
2743 classes[dev->devno] = ATA_DEV_SEMB_UNSUP;
Tejun Heo022bdb02006-05-15 20:58:22 +09002744 }
2745
Tejun Heo416dc9e2007-10-31 10:17:03 +09002746 /* record current link speed */
2747 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
2748 link->sata_spd = (sstatus >> 4) & 0xf;
Tejun Heob1c72912008-07-31 17:02:43 +09002749 if (slave && sata_scr_read(slave, SCR_STATUS, &sstatus) == 0)
2750 slave->sata_spd = (sstatus >> 4) & 0xf;
Tejun Heo008a7892007-07-16 14:29:40 +09002751
Tejun Heodc98c322008-05-19 01:15:07 +09002752 /* thaw the port */
2753 if (ata_is_host_link(link))
2754 ata_eh_thaw_port(ap);
2755
Tejun Heof0465192008-05-19 01:15:08 +09002756 /* postreset() should clear hardware SError. Although SError
2757 * is cleared during link resume, clearing SError here is
2758 * necessary as some PHYs raise hotplug events after SRST.
2759 * This introduces race condition where hotplug occurs between
2760 * reset and here. This race is mediated by cross checking
2761 * link onlineness and classification result later.
2762 */
Tejun Heob1c72912008-07-31 17:02:43 +09002763 if (postreset) {
Tejun Heo416dc9e2007-10-31 10:17:03 +09002764 postreset(link, classes);
Tejun Heob1c72912008-07-31 17:02:43 +09002765 if (slave)
2766 postreset(slave, classes);
2767 }
Tejun Heo20952b62006-05-31 18:27:23 +09002768
Tejun Heo1e641062009-07-17 11:58:33 +09002769 /*
2770 * Some controllers can't be frozen very well and may set
2771 * spuruious error conditions during reset. Clear accumulated
2772 * error information. As reset is the final recovery action,
2773 * nothing is lost by doing this.
2774 */
Tejun Heof0465192008-05-19 01:15:08 +09002775 spin_lock_irqsave(link->ap->lock, flags);
Tejun Heo1e641062009-07-17 11:58:33 +09002776 memset(&link->eh_info, 0, sizeof(link->eh_info));
Tejun Heob1c72912008-07-31 17:02:43 +09002777 if (slave)
Tejun Heo1e641062009-07-17 11:58:33 +09002778 memset(&slave->eh_info, 0, sizeof(link->eh_info));
2779 ap->pflags &= ~ATA_PFLAG_EH_PENDING;
Tejun Heof0465192008-05-19 01:15:08 +09002780 spin_unlock_irqrestore(link->ap->lock, flags);
2781
Tejun Heo3b761d32009-10-06 17:08:40 +09002782 /*
2783 * Make sure onlineness and classification result correspond.
Tejun Heof0465192008-05-19 01:15:08 +09002784 * Hotplug could have happened during reset and some
2785 * controllers fail to wait while a drive is spinning up after
2786 * being hotplugged causing misdetection. By cross checking
Tejun Heo3b761d32009-10-06 17:08:40 +09002787 * link on/offlineness and classification result, those
2788 * conditions can be reliably detected and retried.
Tejun Heof0465192008-05-19 01:15:08 +09002789 */
Tejun Heob1c72912008-07-31 17:02:43 +09002790 nr_unknown = 0;
Tejun Heo1eca4362008-11-03 20:03:17 +09002791 ata_for_each_dev(dev, link, ALL) {
Tejun Heo3b761d32009-10-06 17:08:40 +09002792 if (ata_phys_link_online(ata_dev_phys_link(dev))) {
2793 if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
2794 ata_dev_printk(dev, KERN_DEBUG, "link online "
2795 "but device misclassifed\n");
2796 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heob1c72912008-07-31 17:02:43 +09002797 nr_unknown++;
Tejun Heo3b761d32009-10-06 17:08:40 +09002798 }
2799 } else if (ata_phys_link_offline(ata_dev_phys_link(dev))) {
2800 if (ata_class_enabled(classes[dev->devno]))
2801 ata_dev_printk(dev, KERN_DEBUG, "link offline, "
2802 "clearing class %d to NONE\n",
2803 classes[dev->devno]);
2804 classes[dev->devno] = ATA_DEV_NONE;
2805 } else if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
2806 ata_dev_printk(dev, KERN_DEBUG, "link status unknown, "
2807 "clearing UNKNOWN to NONE\n");
2808 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heob1c72912008-07-31 17:02:43 +09002809 }
Tejun Heof0465192008-05-19 01:15:08 +09002810 }
2811
Tejun Heob1c72912008-07-31 17:02:43 +09002812 if (classify && nr_unknown) {
Tejun Heof0465192008-05-19 01:15:08 +09002813 if (try < max_tries) {
2814 ata_link_printk(link, KERN_WARNING, "link online but "
Tejun Heo3b761d32009-10-06 17:08:40 +09002815 "%d devices misclassified, retrying\n",
2816 nr_unknown);
Tejun Heob1c72912008-07-31 17:02:43 +09002817 failed_link = link;
Tejun Heof0465192008-05-19 01:15:08 +09002818 rc = -EAGAIN;
2819 goto fail;
2820 }
2821 ata_link_printk(link, KERN_WARNING,
Tejun Heo3b761d32009-10-06 17:08:40 +09002822 "link online but %d devices misclassified, "
2823 "device detection might fail\n", nr_unknown);
Tejun Heof0465192008-05-19 01:15:08 +09002824 }
2825
Tejun Heo416dc9e2007-10-31 10:17:03 +09002826 /* reset successful, schedule revalidation */
Tejun Heocf480622008-01-24 00:05:14 +09002827 ata_eh_done(link, NULL, ATA_EH_RESET);
Tejun Heob1c72912008-07-31 17:02:43 +09002828 if (slave)
2829 ata_eh_done(slave, NULL, ATA_EH_RESET);
Tejun Heo6b7ae952010-09-01 17:50:06 +02002830 ehc->last_reset = jiffies; /* update to completion time */
Tejun Heo416dc9e2007-10-31 10:17:03 +09002831 ehc->i.action |= ATA_EH_REVALIDATE;
Tejun Heo6b7ae952010-09-01 17:50:06 +02002832 link->lpm_policy = ATA_LPM_UNKNOWN; /* reset LPM state */
Tejun Heoae791c02007-09-23 13:14:12 +09002833
Tejun Heo416dc9e2007-10-31 10:17:03 +09002834 rc = 0;
Tejun Heofccb6ea2007-07-16 14:29:41 +09002835 out:
2836 /* clear hotplug flag */
2837 ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
Tejun Heob1c72912008-07-31 17:02:43 +09002838 if (slave)
2839 sehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
Tejun Heoafaa5c32007-10-09 15:06:10 +09002840
2841 spin_lock_irqsave(ap->lock, flags);
2842 ap->pflags &= ~ATA_PFLAG_RESETTING;
2843 spin_unlock_irqrestore(ap->lock, flags);
2844
Tejun Heo022bdb02006-05-15 20:58:22 +09002845 return rc;
Tejun Heo416dc9e2007-10-31 10:17:03 +09002846
2847 fail:
Tejun Heo5958e302008-04-07 22:47:20 +09002848 /* if SCR isn't accessible on a fan-out port, PMP needs to be reset */
2849 if (!ata_is_host_link(link) &&
2850 sata_scr_read(link, SCR_STATUS, &sstatus))
2851 rc = -ERESTART;
2852
Tejun Heo416dc9e2007-10-31 10:17:03 +09002853 if (rc == -ERESTART || try >= max_tries)
2854 goto out;
2855
2856 now = jiffies;
2857 if (time_before(now, deadline)) {
2858 unsigned long delta = deadline - now;
2859
Tejun Heob1c72912008-07-31 17:02:43 +09002860 ata_link_printk(failed_link, KERN_WARNING,
Tejun Heo0a2c0f52008-05-20 02:17:52 +09002861 "reset failed (errno=%d), retrying in %u secs\n",
2862 rc, DIV_ROUND_UP(jiffies_to_msecs(delta), 1000));
Tejun Heo416dc9e2007-10-31 10:17:03 +09002863
Tejun Heoc0c362b2010-09-06 17:57:14 +02002864 ata_eh_release(ap);
Tejun Heo416dc9e2007-10-31 10:17:03 +09002865 while (delta)
2866 delta = schedule_timeout_uninterruptible(delta);
Tejun Heoc0c362b2010-09-06 17:57:14 +02002867 ata_eh_acquire(ap);
Tejun Heo416dc9e2007-10-31 10:17:03 +09002868 }
2869
Tejun Heob1c72912008-07-31 17:02:43 +09002870 if (try == max_tries - 1) {
Tejun Heoa07d4992009-01-29 20:31:33 +09002871 sata_down_spd_limit(link, 0);
Tejun Heob1c72912008-07-31 17:02:43 +09002872 if (slave)
Tejun Heoa07d4992009-01-29 20:31:33 +09002873 sata_down_spd_limit(slave, 0);
Tejun Heob1c72912008-07-31 17:02:43 +09002874 } else if (rc == -EPIPE)
Tejun Heoa07d4992009-01-29 20:31:33 +09002875 sata_down_spd_limit(failed_link, 0);
Tejun Heob1c72912008-07-31 17:02:43 +09002876
Tejun Heo416dc9e2007-10-31 10:17:03 +09002877 if (hardreset)
2878 reset = hardreset;
2879 goto retry;
Tejun Heo022bdb02006-05-15 20:58:22 +09002880}
2881
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02002882static inline void ata_eh_pull_park_action(struct ata_port *ap)
2883{
2884 struct ata_link *link;
2885 struct ata_device *dev;
2886 unsigned long flags;
2887
2888 /*
2889 * This function can be thought of as an extended version of
2890 * ata_eh_about_to_do() specially crafted to accommodate the
2891 * requirements of ATA_EH_PARK handling. Since the EH thread
2892 * does not leave the do {} while () loop in ata_eh_recover as
2893 * long as the timeout for a park request to *one* device on
2894 * the port has not expired, and since we still want to pick
2895 * up park requests to other devices on the same port or
2896 * timeout updates for the same device, we have to pull
2897 * ATA_EH_PARK actions from eh_info into eh_context.i
2898 * ourselves at the beginning of each pass over the loop.
2899 *
2900 * Additionally, all write accesses to &ap->park_req_pending
2901 * through INIT_COMPLETION() (see below) or complete_all()
2902 * (see ata_scsi_park_store()) are protected by the host lock.
2903 * As a result we have that park_req_pending.done is zero on
2904 * exit from this function, i.e. when ATA_EH_PARK actions for
2905 * *all* devices on port ap have been pulled into the
2906 * respective eh_context structs. If, and only if,
2907 * park_req_pending.done is non-zero by the time we reach
2908 * wait_for_completion_timeout(), another ATA_EH_PARK action
2909 * has been scheduled for at least one of the devices on port
2910 * ap and we have to cycle over the do {} while () loop in
2911 * ata_eh_recover() again.
2912 */
2913
2914 spin_lock_irqsave(ap->lock, flags);
2915 INIT_COMPLETION(ap->park_req_pending);
Tejun Heo1eca4362008-11-03 20:03:17 +09002916 ata_for_each_link(link, ap, EDGE) {
2917 ata_for_each_dev(dev, link, ALL) {
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02002918 struct ata_eh_info *ehi = &link->eh_info;
2919
2920 link->eh_context.i.dev_action[dev->devno] |=
2921 ehi->dev_action[dev->devno] & ATA_EH_PARK;
2922 ata_eh_clear_action(link, dev, ehi, ATA_EH_PARK);
2923 }
2924 }
2925 spin_unlock_irqrestore(ap->lock, flags);
2926}
2927
2928static void ata_eh_park_issue_cmd(struct ata_device *dev, int park)
2929{
2930 struct ata_eh_context *ehc = &dev->link->eh_context;
2931 struct ata_taskfile tf;
2932 unsigned int err_mask;
2933
2934 ata_tf_init(dev, &tf);
2935 if (park) {
2936 ehc->unloaded_mask |= 1 << dev->devno;
2937 tf.command = ATA_CMD_IDLEIMMEDIATE;
2938 tf.feature = 0x44;
2939 tf.lbal = 0x4c;
2940 tf.lbam = 0x4e;
2941 tf.lbah = 0x55;
2942 } else {
2943 ehc->unloaded_mask &= ~(1 << dev->devno);
2944 tf.command = ATA_CMD_CHK_POWER;
2945 }
2946
2947 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
2948 tf.protocol |= ATA_PROT_NODATA;
2949 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
2950 if (park && (err_mask || tf.lbal != 0xc4)) {
2951 ata_dev_printk(dev, KERN_ERR, "head unload failed!\n");
2952 ehc->unloaded_mask &= ~(1 << dev->devno);
2953 }
2954}
2955
Tejun Heo02607312007-08-06 18:36:23 +09002956static int ata_eh_revalidate_and_attach(struct ata_link *link,
Tejun Heo084fe632006-05-31 18:28:03 +09002957 struct ata_device **r_failed_dev)
Tejun Heo022bdb02006-05-15 20:58:22 +09002958{
Tejun Heo02607312007-08-06 18:36:23 +09002959 struct ata_port *ap = link->ap;
2960 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09002961 struct ata_device *dev;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002962 unsigned int new_mask = 0;
Tejun Heo084fe632006-05-31 18:28:03 +09002963 unsigned long flags;
Tejun Heof58229f2007-08-06 18:36:23 +09002964 int rc = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09002965
2966 DPRINTK("ENTER\n");
2967
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002968 /* For PATA drive side cable detection to work, IDENTIFY must
2969 * be done backwards such that PDIAG- is released by the slave
2970 * device before the master device is identified.
2971 */
Tejun Heo1eca4362008-11-03 20:03:17 +09002972 ata_for_each_dev(dev, link, ALL_REVERSE) {
Tejun Heof58229f2007-08-06 18:36:23 +09002973 unsigned int action = ata_eh_dev_action(dev);
2974 unsigned int readid_flags = 0;
Tejun Heo47005f22006-06-19 18:27:23 +09002975
Tejun Heobff04642006-11-10 18:08:10 +09002976 if (ehc->i.flags & ATA_EHI_DID_RESET)
2977 readid_flags |= ATA_READID_POSTRESET;
2978
Tejun Heo9666f402007-05-04 21:27:47 +02002979 if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
Tejun Heo633273a2007-09-23 13:19:54 +09002980 WARN_ON(dev->class == ATA_DEV_PMP);
2981
Tejun Heob1c72912008-07-31 17:02:43 +09002982 if (ata_phys_link_offline(ata_dev_phys_link(dev))) {
Tejun Heo022bdb02006-05-15 20:58:22 +09002983 rc = -EIO;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002984 goto err;
Tejun Heo022bdb02006-05-15 20:58:22 +09002985 }
2986
Tejun Heo02607312007-08-06 18:36:23 +09002987 ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
Tejun Heo422c9da2007-09-23 13:14:12 +09002988 rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
2989 readid_flags);
Tejun Heo022bdb02006-05-15 20:58:22 +09002990 if (rc)
Tejun Heo8c3c52a2007-03-22 22:24:19 +09002991 goto err;
Tejun Heo022bdb02006-05-15 20:58:22 +09002992
Tejun Heo02607312007-08-06 18:36:23 +09002993 ata_eh_done(link, dev, ATA_EH_REVALIDATE);
Tejun Heo47005f22006-06-19 18:27:23 +09002994
Tejun Heobaa1e782006-11-01 18:39:27 +09002995 /* Configuration may have changed, reconfigure
2996 * transfer mode.
2997 */
2998 ehc->i.flags |= ATA_EHI_SETMODE;
2999
zhao, forrest3057ac32006-06-12 12:01:34 +08003000 /* schedule the scsi_rescan_device() here */
Tejun Heoad72cf92010-07-02 10:03:52 +02003001 schedule_work(&(ap->scsi_rescan_task));
Tejun Heo084fe632006-05-31 18:28:03 +09003002 } else if (dev->class == ATA_DEV_UNKNOWN &&
3003 ehc->tries[dev->devno] &&
3004 ata_class_enabled(ehc->classes[dev->devno])) {
Tejun Heo842faa62009-05-10 01:06:54 +09003005 /* Temporarily set dev->class, it will be
3006 * permanently set once all configurations are
3007 * complete. This is necessary because new
3008 * device configuration is done in two
3009 * separate loops.
3010 */
Tejun Heo084fe632006-05-31 18:28:03 +09003011 dev->class = ehc->classes[dev->devno];
3012
Tejun Heo633273a2007-09-23 13:19:54 +09003013 if (dev->class == ATA_DEV_PMP)
3014 rc = sata_pmp_attach(dev);
3015 else
3016 rc = ata_dev_read_id(dev, &dev->class,
3017 readid_flags, dev->id);
Tejun Heo842faa62009-05-10 01:06:54 +09003018
3019 /* read_id might have changed class, store and reset */
3020 ehc->classes[dev->devno] = dev->class;
3021 dev->class = ATA_DEV_UNKNOWN;
3022
Tejun Heo8c3c52a2007-03-22 22:24:19 +09003023 switch (rc) {
3024 case 0:
Tejun Heo99cf6102009-01-29 20:31:32 +09003025 /* clear error info accumulated during probe */
3026 ata_ering_clear(&dev->ering);
Tejun Heof58229f2007-08-06 18:36:23 +09003027 new_mask |= 1 << dev->devno;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09003028 break;
3029 case -ENOENT:
Tejun Heo55a8e2c2006-11-10 18:08:10 +09003030 /* IDENTIFY was issued to non-existent
3031 * device. No need to reset. Just
Tejun Heo842faa62009-05-10 01:06:54 +09003032 * thaw and ignore the device.
Tejun Heo55a8e2c2006-11-10 18:08:10 +09003033 */
3034 ata_eh_thaw_port(ap);
Tejun Heo084fe632006-05-31 18:28:03 +09003035 break;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09003036 default:
Tejun Heo8c3c52a2007-03-22 22:24:19 +09003037 goto err;
Tejun Heo55a8e2c2006-11-10 18:08:10 +09003038 }
Tejun Heo022bdb02006-05-15 20:58:22 +09003039 }
3040 }
3041
Tejun Heoc1c4e8d2007-04-23 02:05:53 +09003042 /* PDIAG- should have been released, ask cable type if post-reset */
Tejun Heo33267322008-02-13 09:15:09 +09003043 if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) {
3044 if (ap->ops->cable_detect)
3045 ap->cbl = ap->ops->cable_detect(ap);
3046 ata_force_cbl(ap);
3047 }
Tejun Heoc1c4e8d2007-04-23 02:05:53 +09003048
Tejun Heo8c3c52a2007-03-22 22:24:19 +09003049 /* Configure new devices forward such that user doesn't see
3050 * device detection messages backwards.
3051 */
Tejun Heo1eca4362008-11-03 20:03:17 +09003052 ata_for_each_dev(dev, link, ALL) {
Tejun Heo4f7c2872009-10-15 23:37:32 +09003053 if (!(new_mask & (1 << dev->devno)))
Tejun Heo8c3c52a2007-03-22 22:24:19 +09003054 continue;
3055
Tejun Heo842faa62009-05-10 01:06:54 +09003056 dev->class = ehc->classes[dev->devno];
3057
Tejun Heo4f7c2872009-10-15 23:37:32 +09003058 if (dev->class == ATA_DEV_PMP)
3059 continue;
3060
Tejun Heo8c3c52a2007-03-22 22:24:19 +09003061 ehc->i.flags |= ATA_EHI_PRINTINFO;
3062 rc = ata_dev_configure(dev);
3063 ehc->i.flags &= ~ATA_EHI_PRINTINFO;
Tejun Heo842faa62009-05-10 01:06:54 +09003064 if (rc) {
3065 dev->class = ATA_DEV_UNKNOWN;
Tejun Heo8c3c52a2007-03-22 22:24:19 +09003066 goto err;
Tejun Heo842faa62009-05-10 01:06:54 +09003067 }
Tejun Heo8c3c52a2007-03-22 22:24:19 +09003068
3069 spin_lock_irqsave(ap->lock, flags);
3070 ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
3071 spin_unlock_irqrestore(ap->lock, flags);
3072
3073 /* new device discovered, configure xfermode */
3074 ehc->i.flags |= ATA_EHI_SETMODE;
3075 }
3076
3077 return 0;
3078
3079 err:
3080 *r_failed_dev = dev;
3081 DPRINTK("EXIT rc=%d\n", rc);
Tejun Heo022bdb02006-05-15 20:58:22 +09003082 return rc;
3083}
3084
Tejun Heo6f1d1e32007-11-27 19:28:55 +09003085/**
3086 * ata_set_mode - Program timings and issue SET FEATURES - XFER
3087 * @link: link on which timings will be programmed
Martin Olsson98a17082009-04-22 18:21:29 +02003088 * @r_failed_dev: out parameter for failed device
Tejun Heo6f1d1e32007-11-27 19:28:55 +09003089 *
3090 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3091 * ata_set_mode() fails, pointer to the failing device is
3092 * returned in @r_failed_dev.
3093 *
3094 * LOCKING:
3095 * PCI/etc. bus probe sem.
3096 *
3097 * RETURNS:
3098 * 0 on success, negative errno otherwise
3099 */
3100int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3101{
3102 struct ata_port *ap = link->ap;
Tejun Heo00115e02007-11-27 19:28:58 +09003103 struct ata_device *dev;
3104 int rc;
Tejun Heo6f1d1e32007-11-27 19:28:55 +09003105
Tejun Heo76326ac2007-11-27 19:28:59 +09003106 /* if data transfer is verified, clear DUBIOUS_XFER on ering top */
Tejun Heo1eca4362008-11-03 20:03:17 +09003107 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo76326ac2007-11-27 19:28:59 +09003108 if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
3109 struct ata_ering_entry *ent;
3110
3111 ent = ata_ering_top(&dev->ering);
3112 if (ent)
3113 ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER;
3114 }
3115 }
3116
Tejun Heo6f1d1e32007-11-27 19:28:55 +09003117 /* has private set_mode? */
3118 if (ap->ops->set_mode)
Tejun Heo00115e02007-11-27 19:28:58 +09003119 rc = ap->ops->set_mode(link, r_failed_dev);
3120 else
3121 rc = ata_do_set_mode(link, r_failed_dev);
3122
3123 /* if transfer mode has changed, set DUBIOUS_XFER on device */
Tejun Heo1eca4362008-11-03 20:03:17 +09003124 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo00115e02007-11-27 19:28:58 +09003125 struct ata_eh_context *ehc = &link->eh_context;
3126 u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
3127 u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
3128
3129 if (dev->xfer_mode != saved_xfer_mode ||
3130 ata_ncq_enabled(dev) != saved_ncq)
3131 dev->flags |= ATA_DFLAG_DUBIOUS_XFER;
3132 }
3133
3134 return rc;
Tejun Heo6f1d1e32007-11-27 19:28:55 +09003135}
3136
Tejun Heo11fc33d2008-08-30 14:20:01 +02003137/**
3138 * atapi_eh_clear_ua - Clear ATAPI UNIT ATTENTION after reset
3139 * @dev: ATAPI device to clear UA for
3140 *
3141 * Resets and other operations can make an ATAPI device raise
3142 * UNIT ATTENTION which causes the next operation to fail. This
3143 * function clears UA.
3144 *
3145 * LOCKING:
3146 * EH context (may sleep).
3147 *
3148 * RETURNS:
3149 * 0 on success, -errno on failure.
3150 */
3151static int atapi_eh_clear_ua(struct ata_device *dev)
3152{
3153 int i;
3154
3155 for (i = 0; i < ATA_EH_UA_TRIES; i++) {
Tejun Heob5357082009-03-02 18:55:16 +09003156 u8 *sense_buffer = dev->link->ap->sector_buf;
Tejun Heo11fc33d2008-08-30 14:20:01 +02003157 u8 sense_key = 0;
3158 unsigned int err_mask;
3159
3160 err_mask = atapi_eh_tur(dev, &sense_key);
3161 if (err_mask != 0 && err_mask != AC_ERR_DEV) {
3162 ata_dev_printk(dev, KERN_WARNING, "TEST_UNIT_READY "
3163 "failed (err_mask=0x%x)\n", err_mask);
3164 return -EIO;
3165 }
3166
3167 if (!err_mask || sense_key != UNIT_ATTENTION)
3168 return 0;
3169
3170 err_mask = atapi_eh_request_sense(dev, sense_buffer, sense_key);
3171 if (err_mask) {
3172 ata_dev_printk(dev, KERN_WARNING, "failed to clear "
3173 "UNIT ATTENTION (err_mask=0x%x)\n", err_mask);
3174 return -EIO;
3175 }
3176 }
3177
3178 ata_dev_printk(dev, KERN_WARNING,
3179 "UNIT ATTENTION persists after %d tries\n", ATA_EH_UA_TRIES);
3180
3181 return 0;
3182}
3183
Tejun Heo6013efd2009-11-19 15:36:45 +09003184/**
3185 * ata_eh_maybe_retry_flush - Retry FLUSH if necessary
3186 * @dev: ATA device which may need FLUSH retry
3187 *
3188 * If @dev failed FLUSH, it needs to be reported upper layer
3189 * immediately as it means that @dev failed to remap and already
3190 * lost at least a sector and further FLUSH retrials won't make
3191 * any difference to the lost sector. However, if FLUSH failed
3192 * for other reasons, for example transmission error, FLUSH needs
3193 * to be retried.
3194 *
3195 * This function determines whether FLUSH failure retry is
3196 * necessary and performs it if so.
3197 *
3198 * RETURNS:
3199 * 0 if EH can continue, -errno if EH needs to be repeated.
3200 */
3201static int ata_eh_maybe_retry_flush(struct ata_device *dev)
3202{
3203 struct ata_link *link = dev->link;
3204 struct ata_port *ap = link->ap;
3205 struct ata_queued_cmd *qc;
3206 struct ata_taskfile tf;
3207 unsigned int err_mask;
3208 int rc = 0;
3209
3210 /* did flush fail for this device? */
3211 if (!ata_tag_valid(link->active_tag))
3212 return 0;
3213
3214 qc = __ata_qc_from_tag(ap, link->active_tag);
3215 if (qc->dev != dev || (qc->tf.command != ATA_CMD_FLUSH_EXT &&
3216 qc->tf.command != ATA_CMD_FLUSH))
3217 return 0;
3218
3219 /* if the device failed it, it should be reported to upper layers */
3220 if (qc->err_mask & AC_ERR_DEV)
3221 return 0;
3222
3223 /* flush failed for some other reason, give it another shot */
3224 ata_tf_init(dev, &tf);
3225
3226 tf.command = qc->tf.command;
3227 tf.flags |= ATA_TFLAG_DEVICE;
3228 tf.protocol = ATA_PROT_NODATA;
3229
3230 ata_dev_printk(dev, KERN_WARNING, "retrying FLUSH 0x%x Emask 0x%x\n",
3231 tf.command, qc->err_mask);
3232
3233 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
3234 if (!err_mask) {
3235 /*
3236 * FLUSH is complete but there's no way to
3237 * successfully complete a failed command from EH.
3238 * Making sure retry is allowed at least once and
3239 * retrying it should do the trick - whatever was in
3240 * the cache is already on the platter and this won't
3241 * cause infinite loop.
3242 */
3243 qc->scsicmd->allowed = max(qc->scsicmd->allowed, 1);
3244 } else {
3245 ata_dev_printk(dev, KERN_WARNING, "FLUSH failed Emask 0x%x\n",
3246 err_mask);
3247 rc = -EIO;
3248
3249 /* if device failed it, report it to upper layers */
3250 if (err_mask & AC_ERR_DEV) {
3251 qc->err_mask |= AC_ERR_DEV;
3252 qc->result_tf = tf;
3253 if (!(ap->pflags & ATA_PFLAG_FROZEN))
3254 rc = 0;
3255 }
3256 }
3257 return rc;
3258}
3259
Tejun Heo6b7ae952010-09-01 17:50:06 +02003260/**
3261 * ata_eh_set_lpm - configure SATA interface power management
3262 * @link: link to configure power management
3263 * @policy: the link power management policy
3264 * @r_failed_dev: out parameter for failed device
3265 *
3266 * Enable SATA Interface power management. This will enable
3267 * Device Interface Power Management (DIPM) for min_power
3268 * policy, and then call driver specific callbacks for
3269 * enabling Host Initiated Power management.
3270 *
3271 * LOCKING:
3272 * EH context.
3273 *
3274 * RETURNS:
3275 * 0 on success, -errno on failure.
3276 */
3277static int ata_eh_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
3278 struct ata_device **r_failed_dev)
3279{
Tejun Heo6c8ea892010-09-01 17:50:07 +02003280 struct ata_port *ap = ata_is_host_link(link) ? link->ap : NULL;
Tejun Heo6b7ae952010-09-01 17:50:06 +02003281 struct ata_eh_context *ehc = &link->eh_context;
3282 struct ata_device *dev, *link_dev = NULL, *lpm_dev = NULL;
Tejun Heoe5005b12010-12-09 16:13:15 +01003283 enum ata_lpm_policy old_policy = link->lpm_policy;
Tejun Heo6b7ae952010-09-01 17:50:06 +02003284 unsigned int hints = ATA_LPM_EMPTY | ATA_LPM_HIPM;
3285 unsigned int err_mask;
3286 int rc;
3287
3288 /* if the link or host doesn't do LPM, noop */
3289 if ((link->flags & ATA_LFLAG_NO_LPM) || (ap && !ap->ops->set_lpm))
3290 return 0;
3291
3292 /*
3293 * DIPM is enabled only for MIN_POWER as some devices
3294 * misbehave when the host NACKs transition to SLUMBER. Order
3295 * device and link configurations such that the host always
3296 * allows DIPM requests.
3297 */
3298 ata_for_each_dev(dev, link, ENABLED) {
3299 bool hipm = ata_id_has_hipm(dev->id);
3300 bool dipm = ata_id_has_dipm(dev->id);
3301
3302 /* find the first enabled and LPM enabled devices */
3303 if (!link_dev)
3304 link_dev = dev;
3305
3306 if (!lpm_dev && (hipm || dipm))
3307 lpm_dev = dev;
3308
3309 hints &= ~ATA_LPM_EMPTY;
3310 if (!hipm)
3311 hints &= ~ATA_LPM_HIPM;
3312
3313 /* disable DIPM before changing link config */
3314 if (policy != ATA_LPM_MIN_POWER && dipm) {
3315 err_mask = ata_dev_set_feature(dev,
3316 SETFEATURES_SATA_DISABLE, SATA_DIPM);
3317 if (err_mask && err_mask != AC_ERR_DEV) {
3318 ata_dev_printk(dev, KERN_WARNING,
3319 "failed to disable DIPM, Emask 0x%x\n",
3320 err_mask);
3321 rc = -EIO;
3322 goto fail;
3323 }
3324 }
3325 }
3326
Tejun Heo6c8ea892010-09-01 17:50:07 +02003327 if (ap) {
3328 rc = ap->ops->set_lpm(link, policy, hints);
3329 if (!rc && ap->slave_link)
3330 rc = ap->ops->set_lpm(ap->slave_link, policy, hints);
3331 } else
3332 rc = sata_pmp_set_lpm(link, policy, hints);
Tejun Heo6b7ae952010-09-01 17:50:06 +02003333
3334 /*
3335 * Attribute link config failure to the first (LPM) enabled
3336 * device on the link.
3337 */
3338 if (rc) {
3339 if (rc == -EOPNOTSUPP) {
3340 link->flags |= ATA_LFLAG_NO_LPM;
3341 return 0;
3342 }
3343 dev = lpm_dev ? lpm_dev : link_dev;
3344 goto fail;
3345 }
3346
Tejun Heoe5005b12010-12-09 16:13:15 +01003347 /*
3348 * Low level driver acked the transition. Issue DIPM command
3349 * with the new policy set.
3350 */
3351 link->lpm_policy = policy;
3352 if (ap && ap->slave_link)
3353 ap->slave_link->lpm_policy = policy;
3354
Tejun Heo6b7ae952010-09-01 17:50:06 +02003355 /* host config updated, enable DIPM if transitioning to MIN_POWER */
3356 ata_for_each_dev(dev, link, ENABLED) {
3357 if (policy == ATA_LPM_MIN_POWER && ata_id_has_dipm(dev->id)) {
3358 err_mask = ata_dev_set_feature(dev,
3359 SETFEATURES_SATA_ENABLE, SATA_DIPM);
3360 if (err_mask && err_mask != AC_ERR_DEV) {
3361 ata_dev_printk(dev, KERN_WARNING,
3362 "failed to enable DIPM, Emask 0x%x\n",
3363 err_mask);
3364 rc = -EIO;
3365 goto fail;
3366 }
3367 }
3368 }
3369
Tejun Heo6b7ae952010-09-01 17:50:06 +02003370 return 0;
3371
3372fail:
Tejun Heoe5005b12010-12-09 16:13:15 +01003373 /* restore the old policy */
3374 link->lpm_policy = old_policy;
3375 if (ap && ap->slave_link)
3376 ap->slave_link->lpm_policy = old_policy;
3377
Tejun Heo6b7ae952010-09-01 17:50:06 +02003378 /* if no device or only one more chance is left, disable LPM */
3379 if (!dev || ehc->tries[dev->devno] <= 2) {
3380 ata_link_printk(link, KERN_WARNING,
3381 "disabling LPM on the link\n");
3382 link->flags |= ATA_LFLAG_NO_LPM;
3383 }
3384 if (r_failed_dev)
3385 *r_failed_dev = dev;
3386 return rc;
3387}
3388
Tejun Heo02607312007-08-06 18:36:23 +09003389static int ata_link_nr_enabled(struct ata_link *link)
Tejun Heo022bdb02006-05-15 20:58:22 +09003390{
Tejun Heof58229f2007-08-06 18:36:23 +09003391 struct ata_device *dev;
3392 int cnt = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09003393
Tejun Heo1eca4362008-11-03 20:03:17 +09003394 ata_for_each_dev(dev, link, ENABLED)
3395 cnt++;
Tejun Heo022bdb02006-05-15 20:58:22 +09003396 return cnt;
3397}
3398
Tejun Heo02607312007-08-06 18:36:23 +09003399static int ata_link_nr_vacant(struct ata_link *link)
Tejun Heo084fe632006-05-31 18:28:03 +09003400{
Tejun Heof58229f2007-08-06 18:36:23 +09003401 struct ata_device *dev;
3402 int cnt = 0;
Tejun Heo084fe632006-05-31 18:28:03 +09003403
Tejun Heo1eca4362008-11-03 20:03:17 +09003404 ata_for_each_dev(dev, link, ALL)
Tejun Heof58229f2007-08-06 18:36:23 +09003405 if (dev->class == ATA_DEV_UNKNOWN)
Tejun Heo084fe632006-05-31 18:28:03 +09003406 cnt++;
3407 return cnt;
3408}
3409
Tejun Heo02607312007-08-06 18:36:23 +09003410static int ata_eh_skip_recovery(struct ata_link *link)
Tejun Heo084fe632006-05-31 18:28:03 +09003411{
Tejun Heo672b2d62008-01-24 00:05:14 +09003412 struct ata_port *ap = link->ap;
Tejun Heo02607312007-08-06 18:36:23 +09003413 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heof58229f2007-08-06 18:36:23 +09003414 struct ata_device *dev;
Tejun Heo084fe632006-05-31 18:28:03 +09003415
Tejun Heof9df58c2007-09-23 13:14:13 +09003416 /* skip disabled links */
3417 if (link->flags & ATA_LFLAG_DISABLED)
3418 return 1;
3419
Tejun Heoe2f3d752010-09-07 14:05:31 +02003420 /* skip if explicitly requested */
3421 if (ehc->i.flags & ATA_EHI_NO_RECOVERY)
3422 return 1;
3423
Tejun Heo672b2d62008-01-24 00:05:14 +09003424 /* thaw frozen port and recover failed devices */
3425 if ((ap->pflags & ATA_PFLAG_FROZEN) || ata_link_nr_enabled(link))
3426 return 0;
3427
3428 /* reset at least once if reset is requested */
3429 if ((ehc->i.action & ATA_EH_RESET) &&
3430 !(ehc->i.flags & ATA_EHI_DID_RESET))
Tejun Heo084fe632006-05-31 18:28:03 +09003431 return 0;
3432
3433 /* skip if class codes for all vacant slots are ATA_DEV_NONE */
Tejun Heo1eca4362008-11-03 20:03:17 +09003434 ata_for_each_dev(dev, link, ALL) {
Tejun Heo084fe632006-05-31 18:28:03 +09003435 if (dev->class == ATA_DEV_UNKNOWN &&
3436 ehc->classes[dev->devno] != ATA_DEV_NONE)
3437 return 0;
3438 }
3439
3440 return 1;
3441}
3442
Tejun Heoc2c7a892009-01-29 20:31:34 +09003443static int ata_count_probe_trials_cb(struct ata_ering_entry *ent, void *void_arg)
3444{
3445 u64 interval = msecs_to_jiffies(ATA_EH_PROBE_TRIAL_INTERVAL);
3446 u64 now = get_jiffies_64();
3447 int *trials = void_arg;
3448
3449 if (ent->timestamp < now - min(now, interval))
3450 return -1;
3451
3452 (*trials)++;
3453 return 0;
3454}
3455
Tejun Heo02c05a22007-11-27 19:28:54 +09003456static int ata_eh_schedule_probe(struct ata_device *dev)
3457{
3458 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heoc2c7a892009-01-29 20:31:34 +09003459 struct ata_link *link = ata_dev_phys_link(dev);
3460 int trials = 0;
Tejun Heo02c05a22007-11-27 19:28:54 +09003461
3462 if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
3463 (ehc->did_probe_mask & (1 << dev->devno)))
3464 return 0;
3465
3466 ata_eh_detach_dev(dev);
3467 ata_dev_init(dev);
3468 ehc->did_probe_mask |= (1 << dev->devno);
Tejun Heocf480622008-01-24 00:05:14 +09003469 ehc->i.action |= ATA_EH_RESET;
Tejun Heo00115e02007-11-27 19:28:58 +09003470 ehc->saved_xfer_mode[dev->devno] = 0;
3471 ehc->saved_ncq_enabled &= ~(1 << dev->devno);
Tejun Heo02c05a22007-11-27 19:28:54 +09003472
Tejun Heo6b7ae952010-09-01 17:50:06 +02003473 /* the link maybe in a deep sleep, wake it up */
Tejun Heo6c8ea892010-09-01 17:50:07 +02003474 if (link->lpm_policy > ATA_LPM_MAX_POWER) {
3475 if (ata_is_host_link(link))
3476 link->ap->ops->set_lpm(link, ATA_LPM_MAX_POWER,
3477 ATA_LPM_EMPTY);
3478 else
3479 sata_pmp_set_lpm(link, ATA_LPM_MAX_POWER,
3480 ATA_LPM_EMPTY);
3481 }
Tejun Heo6b7ae952010-09-01 17:50:06 +02003482
Tejun Heoc2c7a892009-01-29 20:31:34 +09003483 /* Record and count probe trials on the ering. The specific
3484 * error mask used is irrelevant. Because a successful device
3485 * detection clears the ering, this count accumulates only if
3486 * there are consecutive failed probes.
3487 *
3488 * If the count is equal to or higher than ATA_EH_PROBE_TRIALS
3489 * in the last ATA_EH_PROBE_TRIAL_INTERVAL, link speed is
3490 * forced to 1.5Gbps.
3491 *
3492 * This is to work around cases where failed link speed
3493 * negotiation results in device misdetection leading to
3494 * infinite DEVXCHG or PHRDY CHG events.
3495 */
3496 ata_ering_record(&dev->ering, 0, AC_ERR_OTHER);
3497 ata_ering_map(&dev->ering, ata_count_probe_trials_cb, &trials);
3498
3499 if (trials > ATA_EH_PROBE_TRIALS)
3500 sata_down_spd_limit(link, 1);
3501
Tejun Heo02c05a22007-11-27 19:28:54 +09003502 return 1;
3503}
3504
Tejun Heo9b1e2652007-08-06 18:36:24 +09003505static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
Tejun Heofee7ca72007-07-01 19:05:58 +09003506{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003507 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heofee7ca72007-07-01 19:05:58 +09003508
Tejun Heocf9a5902009-01-29 20:31:35 +09003509 /* -EAGAIN from EH routine indicates retry without prejudice.
3510 * The requester is responsible for ensuring forward progress.
3511 */
3512 if (err != -EAGAIN)
3513 ehc->tries[dev->devno]--;
Tejun Heofee7ca72007-07-01 19:05:58 +09003514
3515 switch (err) {
3516 case -ENODEV:
3517 /* device missing or wrong IDENTIFY data, schedule probing */
3518 ehc->i.probe_mask |= (1 << dev->devno);
3519 case -EINVAL:
3520 /* give it just one more chance */
3521 ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
3522 case -EIO:
Tejun Heod89293a2009-01-29 20:31:29 +09003523 if (ehc->tries[dev->devno] == 1) {
Tejun Heofee7ca72007-07-01 19:05:58 +09003524 /* This is the last chance, better to slow
3525 * down than lose it.
3526 */
Tejun Heoa07d4992009-01-29 20:31:33 +09003527 sata_down_spd_limit(ata_dev_phys_link(dev), 0);
Tejun Heod89293a2009-01-29 20:31:29 +09003528 if (dev->pio_mode > XFER_PIO_0)
3529 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
Tejun Heofee7ca72007-07-01 19:05:58 +09003530 }
3531 }
3532
3533 if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
3534 /* disable device if it has used up all its chances */
3535 ata_dev_disable(dev);
3536
3537 /* detach if offline */
Tejun Heob1c72912008-07-31 17:02:43 +09003538 if (ata_phys_link_offline(ata_dev_phys_link(dev)))
Tejun Heofee7ca72007-07-01 19:05:58 +09003539 ata_eh_detach_dev(dev);
3540
Tejun Heo02c05a22007-11-27 19:28:54 +09003541 /* schedule probe if necessary */
Tejun Heo87fbc5a2008-05-20 02:17:54 +09003542 if (ata_eh_schedule_probe(dev)) {
Tejun Heofee7ca72007-07-01 19:05:58 +09003543 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
Tejun Heo87fbc5a2008-05-20 02:17:54 +09003544 memset(ehc->cmd_timeout_idx[dev->devno], 0,
3545 sizeof(ehc->cmd_timeout_idx[dev->devno]));
3546 }
Tejun Heo9b1e2652007-08-06 18:36:24 +09003547
3548 return 1;
Tejun Heofee7ca72007-07-01 19:05:58 +09003549 } else {
Tejun Heocf480622008-01-24 00:05:14 +09003550 ehc->i.action |= ATA_EH_RESET;
Tejun Heo9b1e2652007-08-06 18:36:24 +09003551 return 0;
Tejun Heofee7ca72007-07-01 19:05:58 +09003552 }
3553}
3554
Tejun Heo022bdb02006-05-15 20:58:22 +09003555/**
3556 * ata_eh_recover - recover host port after error
3557 * @ap: host port to recover
Tejun Heof5914a42006-05-31 18:27:48 +09003558 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09003559 * @softreset: softreset method (can be NULL)
3560 * @hardreset: hardreset method (can be NULL)
3561 * @postreset: postreset method (can be NULL)
Tejun Heo9b1e2652007-08-06 18:36:24 +09003562 * @r_failed_link: out parameter for failed link
Tejun Heo022bdb02006-05-15 20:58:22 +09003563 *
3564 * This is the alpha and omega, eum and yang, heart and soul of
3565 * libata exception handling. On entry, actions required to
Tejun Heo9b1e2652007-08-06 18:36:24 +09003566 * recover each link and hotplug requests are recorded in the
3567 * link's eh_context. This function executes all the operations
3568 * with appropriate retrials and fallbacks to resurrect failed
Tejun Heo084fe632006-05-31 18:28:03 +09003569 * devices, detach goners and greet newcomers.
Tejun Heo022bdb02006-05-15 20:58:22 +09003570 *
3571 * LOCKING:
3572 * Kernel thread context (may sleep).
3573 *
3574 * RETURNS:
3575 * 0 on success, -errno on failure.
3576 */
Tejun Heofb7fd612007-09-23 13:14:12 +09003577int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
3578 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
3579 ata_postreset_fn_t postreset,
3580 struct ata_link **r_failed_link)
Tejun Heo022bdb02006-05-15 20:58:22 +09003581{
Tejun Heo9b1e2652007-08-06 18:36:24 +09003582 struct ata_link *link;
Tejun Heo022bdb02006-05-15 20:58:22 +09003583 struct ata_device *dev;
Tejun Heo6b7ae952010-09-01 17:50:06 +02003584 int rc, nr_fails;
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02003585 unsigned long flags, deadline;
Tejun Heo022bdb02006-05-15 20:58:22 +09003586
3587 DPRINTK("ENTER\n");
3588
3589 /* prep for recovery */
Tejun Heo1eca4362008-11-03 20:03:17 +09003590 ata_for_each_link(link, ap, EDGE) {
Tejun Heo9b1e2652007-08-06 18:36:24 +09003591 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo084fe632006-05-31 18:28:03 +09003592
Tejun Heof9df58c2007-09-23 13:14:13 +09003593 /* re-enable link? */
3594 if (ehc->i.action & ATA_EH_ENABLE_LINK) {
3595 ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
3596 spin_lock_irqsave(ap->lock, flags);
3597 link->flags &= ~ATA_LFLAG_DISABLED;
3598 spin_unlock_irqrestore(ap->lock, flags);
3599 ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
3600 }
3601
Tejun Heo1eca4362008-11-03 20:03:17 +09003602 ata_for_each_dev(dev, link, ALL) {
Tejun Heofd995f72007-09-23 13:14:12 +09003603 if (link->flags & ATA_LFLAG_NO_RETRY)
3604 ehc->tries[dev->devno] = 1;
3605 else
3606 ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
Tejun Heo79a55b72007-01-18 17:22:18 +09003607
Tejun Heo9b1e2652007-08-06 18:36:24 +09003608 /* collect port action mask recorded in dev actions */
3609 ehc->i.action |= ehc->i.dev_action[dev->devno] &
3610 ~ATA_EH_PERDEV_MASK;
3611 ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
Tejun Heo084fe632006-05-31 18:28:03 +09003612
Tejun Heo9b1e2652007-08-06 18:36:24 +09003613 /* process hotplug request */
3614 if (dev->flags & ATA_DFLAG_DETACH)
3615 ata_eh_detach_dev(dev);
3616
Tejun Heo02c05a22007-11-27 19:28:54 +09003617 /* schedule probe if necessary */
3618 if (!ata_dev_enabled(dev))
3619 ata_eh_schedule_probe(dev);
Tejun Heo084fe632006-05-31 18:28:03 +09003620 }
Tejun Heo022bdb02006-05-15 20:58:22 +09003621 }
3622
3623 retry:
Tejun Heo022bdb02006-05-15 20:58:22 +09003624 rc = 0;
3625
Tejun Heoaeb2ecd2006-06-12 14:11:43 +09003626 /* if UNLOADING, finish immediately */
Tejun Heob51e9e52006-06-29 01:29:30 +09003627 if (ap->pflags & ATA_PFLAG_UNLOADING)
Tejun Heoaeb2ecd2006-06-12 14:11:43 +09003628 goto out;
3629
Tejun Heo9b1e2652007-08-06 18:36:24 +09003630 /* prep for EH */
Tejun Heo1eca4362008-11-03 20:03:17 +09003631 ata_for_each_link(link, ap, EDGE) {
Tejun Heo9b1e2652007-08-06 18:36:24 +09003632 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09003633
Tejun Heo9b1e2652007-08-06 18:36:24 +09003634 /* skip EH if possible. */
3635 if (ata_eh_skip_recovery(link))
3636 ehc->i.action = 0;
3637
Tejun Heo1eca4362008-11-03 20:03:17 +09003638 ata_for_each_dev(dev, link, ALL)
Tejun Heo9b1e2652007-08-06 18:36:24 +09003639 ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
3640 }
Tejun Heo084fe632006-05-31 18:28:03 +09003641
Tejun Heo022bdb02006-05-15 20:58:22 +09003642 /* reset */
Tejun Heo1eca4362008-11-03 20:03:17 +09003643 ata_for_each_link(link, ap, EDGE) {
Tejun Heodc98c322008-05-19 01:15:07 +09003644 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09003645
Tejun Heodc98c322008-05-19 01:15:07 +09003646 if (!(ehc->i.action & ATA_EH_RESET))
3647 continue;
Tejun Heo9b1e2652007-08-06 18:36:24 +09003648
Tejun Heodc98c322008-05-19 01:15:07 +09003649 rc = ata_eh_reset(link, ata_link_nr_vacant(link),
3650 prereset, softreset, hardreset, postreset);
3651 if (rc) {
3652 ata_link_printk(link, KERN_ERR,
3653 "reset failed, giving up\n");
3654 goto out;
Tejun Heo022bdb02006-05-15 20:58:22 +09003655 }
Tejun Heo022bdb02006-05-15 20:58:22 +09003656 }
3657
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02003658 do {
3659 unsigned long now;
3660
3661 /*
3662 * clears ATA_EH_PARK in eh_info and resets
3663 * ap->park_req_pending
3664 */
3665 ata_eh_pull_park_action(ap);
3666
3667 deadline = jiffies;
Tejun Heo1eca4362008-11-03 20:03:17 +09003668 ata_for_each_link(link, ap, EDGE) {
3669 ata_for_each_dev(dev, link, ALL) {
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02003670 struct ata_eh_context *ehc = &link->eh_context;
3671 unsigned long tmp;
3672
3673 if (dev->class != ATA_DEV_ATA)
3674 continue;
3675 if (!(ehc->i.dev_action[dev->devno] &
3676 ATA_EH_PARK))
3677 continue;
3678 tmp = dev->unpark_deadline;
3679 if (time_before(deadline, tmp))
3680 deadline = tmp;
3681 else if (time_before_eq(tmp, jiffies))
3682 continue;
3683 if (ehc->unloaded_mask & (1 << dev->devno))
3684 continue;
3685
3686 ata_eh_park_issue_cmd(dev, 1);
3687 }
3688 }
3689
3690 now = jiffies;
3691 if (time_before_eq(deadline, now))
3692 break;
3693
Tejun Heoc0c362b2010-09-06 17:57:14 +02003694 ata_eh_release(ap);
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02003695 deadline = wait_for_completion_timeout(&ap->park_req_pending,
3696 deadline - now);
Tejun Heoc0c362b2010-09-06 17:57:14 +02003697 ata_eh_acquire(ap);
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02003698 } while (deadline);
Tejun Heo1eca4362008-11-03 20:03:17 +09003699 ata_for_each_link(link, ap, EDGE) {
3700 ata_for_each_dev(dev, link, ALL) {
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02003701 if (!(link->eh_context.unloaded_mask &
3702 (1 << dev->devno)))
3703 continue;
3704
3705 ata_eh_park_issue_cmd(dev, 0);
3706 ata_eh_done(link, dev, ATA_EH_PARK);
3707 }
3708 }
3709
Tejun Heo9b1e2652007-08-06 18:36:24 +09003710 /* the rest */
Tejun Heo6b7ae952010-09-01 17:50:06 +02003711 nr_fails = 0;
3712 ata_for_each_link(link, ap, PMP_FIRST) {
Tejun Heo9b1e2652007-08-06 18:36:24 +09003713 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heo022bdb02006-05-15 20:58:22 +09003714
Tejun Heo6b7ae952010-09-01 17:50:06 +02003715 if (sata_pmp_attached(ap) && ata_is_host_link(link))
3716 goto config_lpm;
3717
Tejun Heo9b1e2652007-08-06 18:36:24 +09003718 /* revalidate existing devices and attach new ones */
3719 rc = ata_eh_revalidate_and_attach(link, &dev);
Tejun Heo4ae72a12007-02-02 16:22:30 +09003720 if (rc)
Tejun Heo6b7ae952010-09-01 17:50:06 +02003721 goto rest_fail;
Tejun Heo9b1e2652007-08-06 18:36:24 +09003722
Tejun Heo633273a2007-09-23 13:19:54 +09003723 /* if PMP got attached, return, pmp EH will take care of it */
3724 if (link->device->class == ATA_DEV_PMP) {
3725 ehc->i.action = 0;
3726 return 0;
3727 }
3728
Tejun Heo9b1e2652007-08-06 18:36:24 +09003729 /* configure transfer mode if necessary */
3730 if (ehc->i.flags & ATA_EHI_SETMODE) {
3731 rc = ata_set_mode(link, &dev);
3732 if (rc)
Tejun Heo6b7ae952010-09-01 17:50:06 +02003733 goto rest_fail;
Tejun Heo9b1e2652007-08-06 18:36:24 +09003734 ehc->i.flags &= ~ATA_EHI_SETMODE;
3735 }
3736
Tejun Heo11fc33d2008-08-30 14:20:01 +02003737 /* If reset has been issued, clear UA to avoid
3738 * disrupting the current users of the device.
3739 */
3740 if (ehc->i.flags & ATA_EHI_DID_RESET) {
Tejun Heo1eca4362008-11-03 20:03:17 +09003741 ata_for_each_dev(dev, link, ALL) {
Tejun Heo11fc33d2008-08-30 14:20:01 +02003742 if (dev->class != ATA_DEV_ATAPI)
3743 continue;
3744 rc = atapi_eh_clear_ua(dev);
3745 if (rc)
Tejun Heo6b7ae952010-09-01 17:50:06 +02003746 goto rest_fail;
Tejun Heo11fc33d2008-08-30 14:20:01 +02003747 }
3748 }
3749
Tejun Heo6013efd2009-11-19 15:36:45 +09003750 /* retry flush if necessary */
3751 ata_for_each_dev(dev, link, ALL) {
3752 if (dev->class != ATA_DEV_ATA)
3753 continue;
3754 rc = ata_eh_maybe_retry_flush(dev);
3755 if (rc)
Tejun Heo6b7ae952010-09-01 17:50:06 +02003756 goto rest_fail;
Tejun Heo6013efd2009-11-19 15:36:45 +09003757 }
3758
Tejun Heo6b7ae952010-09-01 17:50:06 +02003759 config_lpm:
Tejun Heo11fc33d2008-08-30 14:20:01 +02003760 /* configure link power saving */
Tejun Heo6b7ae952010-09-01 17:50:06 +02003761 if (link->lpm_policy != ap->target_lpm_policy) {
3762 rc = ata_eh_set_lpm(link, ap->target_lpm_policy, &dev);
3763 if (rc)
3764 goto rest_fail;
3765 }
Kristen Carlson Accardica773292007-10-25 00:58:59 -04003766
Tejun Heo9b1e2652007-08-06 18:36:24 +09003767 /* this link is okay now */
3768 ehc->i.flags = 0;
3769 continue;
3770
Tejun Heo6b7ae952010-09-01 17:50:06 +02003771 rest_fail:
3772 nr_fails++;
3773 if (dev)
3774 ata_eh_handle_dev_fail(dev, rc);
Tejun Heo9b1e2652007-08-06 18:36:24 +09003775
Tejun Heob06ce3e2007-10-09 15:06:48 +09003776 if (ap->pflags & ATA_PFLAG_FROZEN) {
3777 /* PMP reset requires working host port.
3778 * Can't retry if it's frozen.
3779 */
Tejun Heo071f44b2008-04-07 22:47:22 +09003780 if (sata_pmp_attached(ap))
Tejun Heob06ce3e2007-10-09 15:06:48 +09003781 goto out;
Tejun Heo9b1e2652007-08-06 18:36:24 +09003782 break;
Tejun Heob06ce3e2007-10-09 15:06:48 +09003783 }
Tejun Heo022bdb02006-05-15 20:58:22 +09003784 }
3785
Tejun Heo6b7ae952010-09-01 17:50:06 +02003786 if (nr_fails)
Tejun Heo9b1e2652007-08-06 18:36:24 +09003787 goto retry;
Tejun Heo022bdb02006-05-15 20:58:22 +09003788
Tejun Heo022bdb02006-05-15 20:58:22 +09003789 out:
Tejun Heo9b1e2652007-08-06 18:36:24 +09003790 if (rc && r_failed_link)
3791 *r_failed_link = link;
Tejun Heo022bdb02006-05-15 20:58:22 +09003792
3793 DPRINTK("EXIT, rc=%d\n", rc);
3794 return rc;
3795}
3796
3797/**
3798 * ata_eh_finish - finish up EH
3799 * @ap: host port to finish EH for
3800 *
3801 * Recovery is complete. Clean up EH states and retry or finish
3802 * failed qcs.
3803 *
3804 * LOCKING:
3805 * None.
3806 */
Tejun Heofb7fd612007-09-23 13:14:12 +09003807void ata_eh_finish(struct ata_port *ap)
Tejun Heo022bdb02006-05-15 20:58:22 +09003808{
3809 int tag;
3810
3811 /* retry or finish qcs */
3812 for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
3813 struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
3814
3815 if (!(qc->flags & ATA_QCFLAG_FAILED))
3816 continue;
3817
3818 if (qc->err_mask) {
3819 /* FIXME: Once EH migration is complete,
3820 * generate sense data in this function,
3821 * considering both err_mask and tf.
3822 */
Tejun Heo03faab72008-03-27 19:14:24 +09003823 if (qc->flags & ATA_QCFLAG_RETRY)
Tejun Heo022bdb02006-05-15 20:58:22 +09003824 ata_eh_qc_retry(qc);
Tejun Heo03faab72008-03-27 19:14:24 +09003825 else
3826 ata_eh_qc_complete(qc);
Tejun Heo022bdb02006-05-15 20:58:22 +09003827 } else {
3828 if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
3829 ata_eh_qc_complete(qc);
3830 } else {
3831 /* feed zero TF to sense generation */
3832 memset(&qc->result_tf, 0, sizeof(qc->result_tf));
3833 ata_eh_qc_retry(qc);
3834 }
3835 }
3836 }
Tejun Heoda917d62007-09-23 13:14:12 +09003837
3838 /* make sure nr_active_links is zero after EH */
3839 WARN_ON(ap->nr_active_links);
3840 ap->nr_active_links = 0;
Tejun Heo022bdb02006-05-15 20:58:22 +09003841}
3842
3843/**
3844 * ata_do_eh - do standard error handling
3845 * @ap: host port to handle error for
Tejun Heoa1efdab2008-03-25 12:22:50 +09003846 *
Tejun Heof5914a42006-05-31 18:27:48 +09003847 * @prereset: prereset method (can be NULL)
Tejun Heo022bdb02006-05-15 20:58:22 +09003848 * @softreset: softreset method (can be NULL)
3849 * @hardreset: hardreset method (can be NULL)
3850 * @postreset: postreset method (can be NULL)
3851 *
3852 * Perform standard error handling sequence.
3853 *
3854 * LOCKING:
3855 * Kernel thread context (may sleep).
3856 */
Tejun Heof5914a42006-05-31 18:27:48 +09003857void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
3858 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
3859 ata_postreset_fn_t postreset)
Tejun Heo022bdb02006-05-15 20:58:22 +09003860{
Tejun Heo9b1e2652007-08-06 18:36:24 +09003861 struct ata_device *dev;
3862 int rc;
3863
3864 ata_eh_autopsy(ap);
3865 ata_eh_report(ap);
3866
3867 rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
3868 NULL);
3869 if (rc) {
Tejun Heo1eca4362008-11-03 20:03:17 +09003870 ata_for_each_dev(dev, &ap->link, ALL)
Tejun Heo9b1e2652007-08-06 18:36:24 +09003871 ata_dev_disable(dev);
3872 }
3873
Tejun Heo022bdb02006-05-15 20:58:22 +09003874 ata_eh_finish(ap);
3875}
Tejun Heo500530f2006-07-03 16:07:27 +09003876
Tejun Heoa1efdab2008-03-25 12:22:50 +09003877/**
3878 * ata_std_error_handler - standard error handler
3879 * @ap: host port to handle error for
3880 *
3881 * Standard error handler
3882 *
3883 * LOCKING:
3884 * Kernel thread context (may sleep).
3885 */
3886void ata_std_error_handler(struct ata_port *ap)
3887{
3888 struct ata_port_operations *ops = ap->ops;
3889 ata_reset_fn_t hardreset = ops->hardreset;
3890
Tejun Heo57c9efd2008-04-07 22:47:19 +09003891 /* ignore built-in hardreset if SCR access is not available */
Tejun Heofe06e5f2010-05-10 21:41:39 +02003892 if (hardreset == sata_std_hardreset && !sata_scr_valid(&ap->link))
Tejun Heoa1efdab2008-03-25 12:22:50 +09003893 hardreset = NULL;
3894
3895 ata_do_eh(ap, ops->prereset, ops->softreset, hardreset, ops->postreset);
3896}
3897
Tejun Heo6ffa01d2007-03-02 17:32:47 +09003898#ifdef CONFIG_PM
Tejun Heo500530f2006-07-03 16:07:27 +09003899/**
3900 * ata_eh_handle_port_suspend - perform port suspend operation
3901 * @ap: port to suspend
3902 *
3903 * Suspend @ap.
3904 *
3905 * LOCKING:
3906 * Kernel thread context (may sleep).
3907 */
3908static void ata_eh_handle_port_suspend(struct ata_port *ap)
3909{
3910 unsigned long flags;
3911 int rc = 0;
3912
3913 /* are we suspending? */
3914 spin_lock_irqsave(ap->lock, flags);
3915 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
3916 ap->pm_mesg.event == PM_EVENT_ON) {
3917 spin_unlock_irqrestore(ap->lock, flags);
3918 return;
3919 }
3920 spin_unlock_irqrestore(ap->lock, flags);
3921
3922 WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
3923
Tejun Heo64578a32007-05-15 03:28:16 +09003924 /* tell ACPI we're suspending */
3925 rc = ata_acpi_on_suspend(ap);
3926 if (rc)
3927 goto out;
3928
Tejun Heo500530f2006-07-03 16:07:27 +09003929 /* suspend */
3930 ata_eh_freeze_port(ap);
3931
3932 if (ap->ops->port_suspend)
3933 rc = ap->ops->port_suspend(ap, ap->pm_mesg);
3934
Shaohua Libd3adca2007-11-02 09:32:38 +08003935 ata_acpi_set_state(ap, PMSG_SUSPEND);
Tejun Heo64578a32007-05-15 03:28:16 +09003936 out:
Tejun Heo500530f2006-07-03 16:07:27 +09003937 /* report result */
3938 spin_lock_irqsave(ap->lock, flags);
3939
3940 ap->pflags &= ~ATA_PFLAG_PM_PENDING;
3941 if (rc == 0)
3942 ap->pflags |= ATA_PFLAG_SUSPENDED;
Tejun Heo64578a32007-05-15 03:28:16 +09003943 else if (ap->pflags & ATA_PFLAG_FROZEN)
Tejun Heo500530f2006-07-03 16:07:27 +09003944 ata_port_schedule_eh(ap);
3945
3946 if (ap->pm_result) {
3947 *ap->pm_result = rc;
3948 ap->pm_result = NULL;
3949 }
3950
3951 spin_unlock_irqrestore(ap->lock, flags);
3952
3953 return;
3954}
3955
3956/**
3957 * ata_eh_handle_port_resume - perform port resume operation
3958 * @ap: port to resume
3959 *
3960 * Resume @ap.
3961 *
Tejun Heo500530f2006-07-03 16:07:27 +09003962 * LOCKING:
3963 * Kernel thread context (may sleep).
3964 */
3965static void ata_eh_handle_port_resume(struct ata_port *ap)
3966{
Tejun Heo6f9c1ea2009-04-23 09:55:28 +09003967 struct ata_link *link;
3968 struct ata_device *dev;
Tejun Heo500530f2006-07-03 16:07:27 +09003969 unsigned long flags;
Tejun Heo9666f402007-05-04 21:27:47 +02003970 int rc = 0;
Tejun Heo500530f2006-07-03 16:07:27 +09003971
3972 /* are we resuming? */
3973 spin_lock_irqsave(ap->lock, flags);
3974 if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
3975 ap->pm_mesg.event != PM_EVENT_ON) {
3976 spin_unlock_irqrestore(ap->lock, flags);
3977 return;
3978 }
3979 spin_unlock_irqrestore(ap->lock, flags);
3980
Tejun Heo9666f402007-05-04 21:27:47 +02003981 WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
Tejun Heo500530f2006-07-03 16:07:27 +09003982
Tejun Heo6f9c1ea2009-04-23 09:55:28 +09003983 /*
3984 * Error timestamps are in jiffies which doesn't run while
3985 * suspended and PHY events during resume isn't too uncommon.
3986 * When the two are combined, it can lead to unnecessary speed
3987 * downs if the machine is suspended and resumed repeatedly.
3988 * Clear error history.
3989 */
3990 ata_for_each_link(link, ap, HOST_FIRST)
3991 ata_for_each_dev(dev, link, ALL)
3992 ata_ering_clear(&dev->ering);
3993
Shaohua Libd3adca2007-11-02 09:32:38 +08003994 ata_acpi_set_state(ap, PMSG_ON);
3995
Tejun Heo500530f2006-07-03 16:07:27 +09003996 if (ap->ops->port_resume)
3997 rc = ap->ops->port_resume(ap);
3998
Tejun Heo67465442007-05-15 03:28:16 +09003999 /* tell ACPI that we're resuming */
4000 ata_acpi_on_resume(ap);
4001
Tejun Heo9666f402007-05-04 21:27:47 +02004002 /* report result */
Tejun Heo500530f2006-07-03 16:07:27 +09004003 spin_lock_irqsave(ap->lock, flags);
4004 ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
4005 if (ap->pm_result) {
4006 *ap->pm_result = rc;
4007 ap->pm_result = NULL;
4008 }
4009 spin_unlock_irqrestore(ap->lock, flags);
4010}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09004011#endif /* CONFIG_PM */