Tejun Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 1 | /* |
| 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 Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 35 | #include <linux/kernel.h> |
| 36 | #include <scsi/scsi.h> |
| 37 | #include <scsi/scsi_host.h> |
| 38 | #include <scsi/scsi_eh.h> |
| 39 | #include <scsi/scsi_device.h> |
| 40 | #include <scsi/scsi_cmnd.h> |
Jeff Garzik | c6fd280 | 2006-08-10 07:31:37 -0400 | [diff] [blame] | 41 | #include "../scsi/scsi_transport_api.h" |
Tejun Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 42 | |
| 43 | #include <linux/libata.h> |
| 44 | |
| 45 | #include "libata.h" |
| 46 | |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 47 | enum { |
| 48 | ATA_EH_SPDN_NCQ_OFF = (1 << 0), |
| 49 | ATA_EH_SPDN_SPEED_DOWN = (1 << 1), |
| 50 | ATA_EH_SPDN_FALLBACK_TO_PIO = (1 << 2), |
| 51 | }; |
| 52 | |
Tejun Heo | 31daabd | 2007-02-02 16:50:52 +0900 | [diff] [blame] | 53 | /* Waiting in ->prereset can never be reliable. It's sometimes nice |
| 54 | * to wait there but it can't be depended upon; otherwise, we wouldn't |
| 55 | * be resetting. Just give it enough time for most drives to spin up. |
| 56 | */ |
| 57 | enum { |
| 58 | ATA_EH_PRERESET_TIMEOUT = 10 * HZ, |
Tejun Heo | 5ddf24c | 2007-07-16 14:29:41 +0900 | [diff] [blame] | 59 | ATA_EH_FASTDRAIN_INTERVAL = 3 * HZ, |
Tejun Heo | 31daabd | 2007-02-02 16:50:52 +0900 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | /* The following table determines how we sequence resets. Each entry |
| 63 | * represents timeout for that try. The first try can be soft or |
| 64 | * hardreset. All others are hardreset if available. In most cases |
| 65 | * the first reset w/ 10sec timeout should succeed. Following entries |
| 66 | * are mostly for error handling, hotplug and retarded devices. |
| 67 | */ |
| 68 | static const unsigned long ata_eh_reset_timeouts[] = { |
| 69 | 10 * HZ, /* most drives spin up by 10sec */ |
| 70 | 10 * HZ, /* > 99% working drives spin up before 20sec */ |
| 71 | 35 * HZ, /* give > 30 secs of idleness for retarded devices */ |
| 72 | 5 * HZ, /* and sweet one last chance */ |
| 73 | /* > 1 min has elapsed, give up */ |
| 74 | }; |
| 75 | |
Tejun Heo | ad9e276 | 2006-05-15 20:58:12 +0900 | [diff] [blame] | 76 | static void __ata_port_freeze(struct ata_port *ap); |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 77 | static void ata_eh_finish(struct ata_port *ap); |
Tejun Heo | 6ffa01d | 2007-03-02 17:32:47 +0900 | [diff] [blame] | 78 | #ifdef CONFIG_PM |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 79 | static void ata_eh_handle_port_suspend(struct ata_port *ap); |
| 80 | static void ata_eh_handle_port_resume(struct ata_port *ap); |
Tejun Heo | 6ffa01d | 2007-03-02 17:32:47 +0900 | [diff] [blame] | 81 | #else /* CONFIG_PM */ |
| 82 | static void ata_eh_handle_port_suspend(struct ata_port *ap) |
| 83 | { } |
| 84 | |
| 85 | static void ata_eh_handle_port_resume(struct ata_port *ap) |
| 86 | { } |
Tejun Heo | 6ffa01d | 2007-03-02 17:32:47 +0900 | [diff] [blame] | 87 | #endif /* CONFIG_PM */ |
Tejun Heo | ad9e276 | 2006-05-15 20:58:12 +0900 | [diff] [blame] | 88 | |
Tejun Heo | b64bbc3 | 2007-07-16 14:29:39 +0900 | [diff] [blame] | 89 | static void __ata_ehi_pushv_desc(struct ata_eh_info *ehi, const char *fmt, |
| 90 | va_list args) |
| 91 | { |
| 92 | ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len, |
| 93 | ATA_EH_DESC_LEN - ehi->desc_len, |
| 94 | fmt, args); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * __ata_ehi_push_desc - push error description without adding separator |
| 99 | * @ehi: target EHI |
| 100 | * @fmt: printf format string |
| 101 | * |
| 102 | * Format string according to @fmt and append it to @ehi->desc. |
| 103 | * |
| 104 | * LOCKING: |
| 105 | * spin_lock_irqsave(host lock) |
| 106 | */ |
| 107 | void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...) |
| 108 | { |
| 109 | va_list args; |
| 110 | |
| 111 | va_start(args, fmt); |
| 112 | __ata_ehi_pushv_desc(ehi, fmt, args); |
| 113 | va_end(args); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * ata_ehi_push_desc - push error description with separator |
| 118 | * @ehi: target EHI |
| 119 | * @fmt: printf format string |
| 120 | * |
| 121 | * Format string according to @fmt and append it to @ehi->desc. |
| 122 | * If @ehi->desc is not empty, ", " is added in-between. |
| 123 | * |
| 124 | * LOCKING: |
| 125 | * spin_lock_irqsave(host lock) |
| 126 | */ |
| 127 | void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...) |
| 128 | { |
| 129 | va_list args; |
| 130 | |
| 131 | if (ehi->desc_len) |
| 132 | __ata_ehi_push_desc(ehi, ", "); |
| 133 | |
| 134 | va_start(args, fmt); |
| 135 | __ata_ehi_pushv_desc(ehi, fmt, args); |
| 136 | va_end(args); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * ata_ehi_clear_desc - clean error description |
| 141 | * @ehi: target EHI |
| 142 | * |
| 143 | * Clear @ehi->desc. |
| 144 | * |
| 145 | * LOCKING: |
| 146 | * spin_lock_irqsave(host lock) |
| 147 | */ |
| 148 | void ata_ehi_clear_desc(struct ata_eh_info *ehi) |
| 149 | { |
| 150 | ehi->desc[0] = '\0'; |
| 151 | ehi->desc_len = 0; |
| 152 | } |
| 153 | |
Tejun Heo | 0c247c5 | 2006-05-15 20:58:19 +0900 | [diff] [blame] | 154 | static void ata_ering_record(struct ata_ering *ering, int is_io, |
| 155 | unsigned int err_mask) |
| 156 | { |
| 157 | struct ata_ering_entry *ent; |
| 158 | |
| 159 | WARN_ON(!err_mask); |
| 160 | |
| 161 | ering->cursor++; |
| 162 | ering->cursor %= ATA_ERING_SIZE; |
| 163 | |
| 164 | ent = &ering->ring[ering->cursor]; |
| 165 | ent->is_io = is_io; |
| 166 | ent->err_mask = err_mask; |
| 167 | ent->timestamp = get_jiffies_64(); |
| 168 | } |
| 169 | |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 170 | static void ata_ering_clear(struct ata_ering *ering) |
Tejun Heo | 0c247c5 | 2006-05-15 20:58:19 +0900 | [diff] [blame] | 171 | { |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 172 | memset(ering, 0, sizeof(*ering)); |
Tejun Heo | 0c247c5 | 2006-05-15 20:58:19 +0900 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | static int ata_ering_map(struct ata_ering *ering, |
| 176 | int (*map_fn)(struct ata_ering_entry *, void *), |
| 177 | void *arg) |
| 178 | { |
| 179 | int idx, rc = 0; |
| 180 | struct ata_ering_entry *ent; |
| 181 | |
| 182 | idx = ering->cursor; |
| 183 | do { |
| 184 | ent = &ering->ring[idx]; |
| 185 | if (!ent->err_mask) |
| 186 | break; |
| 187 | rc = map_fn(ent, arg); |
| 188 | if (rc) |
| 189 | break; |
| 190 | idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE; |
| 191 | } while (idx != ering->cursor); |
| 192 | |
| 193 | return rc; |
| 194 | } |
| 195 | |
Tejun Heo | 64f65ca | 2006-06-24 20:30:18 +0900 | [diff] [blame] | 196 | static unsigned int ata_eh_dev_action(struct ata_device *dev) |
| 197 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 198 | struct ata_eh_context *ehc = &dev->link->eh_context; |
Tejun Heo | 64f65ca | 2006-06-24 20:30:18 +0900 | [diff] [blame] | 199 | |
| 200 | return ehc->i.action | ehc->i.dev_action[dev->devno]; |
| 201 | } |
| 202 | |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 203 | static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev, |
Tejun Heo | af181c2 | 2006-06-24 20:30:18 +0900 | [diff] [blame] | 204 | struct ata_eh_info *ehi, unsigned int action) |
| 205 | { |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 206 | struct ata_device *tdev; |
Tejun Heo | af181c2 | 2006-06-24 20:30:18 +0900 | [diff] [blame] | 207 | |
| 208 | if (!dev) { |
| 209 | ehi->action &= ~action; |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 210 | ata_link_for_each_dev(tdev, link) |
| 211 | ehi->dev_action[tdev->devno] &= ~action; |
Tejun Heo | af181c2 | 2006-06-24 20:30:18 +0900 | [diff] [blame] | 212 | } else { |
| 213 | /* doesn't make sense for port-wide EH actions */ |
| 214 | WARN_ON(!(action & ATA_EH_PERDEV_MASK)); |
| 215 | |
| 216 | /* break ehi->action into ehi->dev_action */ |
| 217 | if (ehi->action & action) { |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 218 | ata_link_for_each_dev(tdev, link) |
| 219 | ehi->dev_action[tdev->devno] |= |
| 220 | ehi->action & action; |
Tejun Heo | af181c2 | 2006-06-24 20:30:18 +0900 | [diff] [blame] | 221 | ehi->action &= ~action; |
| 222 | } |
| 223 | |
| 224 | /* turn off the specified per-dev action */ |
| 225 | ehi->dev_action[dev->devno] &= ~action; |
| 226 | } |
| 227 | } |
| 228 | |
Tejun Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 229 | /** |
| 230 | * ata_scsi_timed_out - SCSI layer time out callback |
| 231 | * @cmd: timed out SCSI command |
| 232 | * |
| 233 | * Handles SCSI layer timeout. We race with normal completion of |
| 234 | * the qc for @cmd. If the qc is already gone, we lose and let |
| 235 | * the scsi command finish (EH_HANDLED). Otherwise, the qc has |
| 236 | * timed out and EH should be invoked. Prevent ata_qc_complete() |
| 237 | * from finishing it by setting EH_SCHEDULED and return |
| 238 | * EH_NOT_HANDLED. |
| 239 | * |
Tejun Heo | ad9e276 | 2006-05-15 20:58:12 +0900 | [diff] [blame] | 240 | * TODO: kill this function once old EH is gone. |
| 241 | * |
Tejun Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 242 | * LOCKING: |
| 243 | * Called from timer context |
| 244 | * |
| 245 | * RETURNS: |
| 246 | * EH_HANDLED or EH_NOT_HANDLED |
| 247 | */ |
| 248 | enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd) |
| 249 | { |
| 250 | struct Scsi_Host *host = cmd->device->host; |
Jeff Garzik | 35bb94b | 2006-04-11 13:12:34 -0400 | [diff] [blame] | 251 | struct ata_port *ap = ata_shost_to_port(host); |
Tejun Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 252 | unsigned long flags; |
| 253 | struct ata_queued_cmd *qc; |
Tejun Heo | ad9e276 | 2006-05-15 20:58:12 +0900 | [diff] [blame] | 254 | enum scsi_eh_timer_return ret; |
Tejun Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 255 | |
| 256 | DPRINTK("ENTER\n"); |
| 257 | |
Tejun Heo | ad9e276 | 2006-05-15 20:58:12 +0900 | [diff] [blame] | 258 | if (ap->ops->error_handler) { |
| 259 | ret = EH_NOT_HANDLED; |
| 260 | goto out; |
| 261 | } |
| 262 | |
| 263 | ret = EH_HANDLED; |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 264 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 265 | qc = ata_qc_from_tag(ap, ap->link.active_tag); |
Tejun Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 266 | if (qc) { |
| 267 | WARN_ON(qc->scsicmd != cmd); |
| 268 | qc->flags |= ATA_QCFLAG_EH_SCHEDULED; |
| 269 | qc->err_mask |= AC_ERR_TIMEOUT; |
| 270 | ret = EH_NOT_HANDLED; |
| 271 | } |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 272 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 273 | |
Tejun Heo | ad9e276 | 2006-05-15 20:58:12 +0900 | [diff] [blame] | 274 | out: |
Tejun Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 275 | DPRINTK("EXIT, ret=%d\n", ret); |
| 276 | return ret; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * ata_scsi_error - SCSI layer error handler callback |
| 281 | * @host: SCSI host on which error occurred |
| 282 | * |
| 283 | * Handles SCSI-layer-thrown error events. |
| 284 | * |
| 285 | * LOCKING: |
| 286 | * Inherited from SCSI layer (none, can sleep) |
| 287 | * |
| 288 | * RETURNS: |
| 289 | * Zero. |
| 290 | */ |
Jeff Garzik | 381544b | 2006-04-11 13:04:39 -0400 | [diff] [blame] | 291 | void ata_scsi_error(struct Scsi_Host *host) |
Tejun Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 292 | { |
Jeff Garzik | 35bb94b | 2006-04-11 13:12:34 -0400 | [diff] [blame] | 293 | struct ata_port *ap = ata_shost_to_port(host); |
Tejun Heo | ad9e276 | 2006-05-15 20:58:12 +0900 | [diff] [blame] | 294 | int i, repeat_cnt = ATA_EH_MAX_REPEAT; |
| 295 | unsigned long flags; |
Tejun Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 296 | |
| 297 | DPRINTK("ENTER\n"); |
| 298 | |
Tejun Heo | ad9e276 | 2006-05-15 20:58:12 +0900 | [diff] [blame] | 299 | /* synchronize with port task */ |
Tejun Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 300 | ata_port_flush_task(ap); |
| 301 | |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 302 | /* synchronize with host lock and sort out timeouts */ |
Tejun Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 303 | |
Tejun Heo | ad9e276 | 2006-05-15 20:58:12 +0900 | [diff] [blame] | 304 | /* For new EH, all qcs are finished in one of three ways - |
| 305 | * normal completion, error completion, and SCSI timeout. |
| 306 | * Both cmpletions can race against SCSI timeout. When normal |
| 307 | * completion wins, the qc never reaches EH. When error |
| 308 | * completion wins, the qc has ATA_QCFLAG_FAILED set. |
| 309 | * |
| 310 | * When SCSI timeout wins, things are a bit more complex. |
| 311 | * Normal or error completion can occur after the timeout but |
| 312 | * before this point. In such cases, both types of |
| 313 | * completions are honored. A scmd is determined to have |
| 314 | * timed out iff its associated qc is active and not failed. |
| 315 | */ |
| 316 | if (ap->ops->error_handler) { |
| 317 | struct scsi_cmnd *scmd, *tmp; |
| 318 | int nr_timedout = 0; |
Tejun Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 319 | |
Tejun Heo | e30349d | 2006-07-03 03:02:15 +0900 | [diff] [blame] | 320 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | ad9e276 | 2006-05-15 20:58:12 +0900 | [diff] [blame] | 321 | |
| 322 | list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) { |
| 323 | struct ata_queued_cmd *qc; |
| 324 | |
| 325 | for (i = 0; i < ATA_MAX_QUEUE; i++) { |
| 326 | qc = __ata_qc_from_tag(ap, i); |
| 327 | if (qc->flags & ATA_QCFLAG_ACTIVE && |
| 328 | qc->scsicmd == scmd) |
| 329 | break; |
| 330 | } |
| 331 | |
| 332 | if (i < ATA_MAX_QUEUE) { |
| 333 | /* the scmd has an associated qc */ |
| 334 | if (!(qc->flags & ATA_QCFLAG_FAILED)) { |
| 335 | /* which hasn't failed yet, timeout */ |
| 336 | qc->err_mask |= AC_ERR_TIMEOUT; |
| 337 | qc->flags |= ATA_QCFLAG_FAILED; |
| 338 | nr_timedout++; |
| 339 | } |
| 340 | } else { |
| 341 | /* Normal completion occurred after |
| 342 | * SCSI timeout but before this point. |
| 343 | * Successfully complete it. |
| 344 | */ |
| 345 | scmd->retries = scmd->allowed; |
| 346 | scsi_eh_finish_cmd(scmd, &ap->eh_done_q); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | /* If we have timed out qcs. They belong to EH from |
| 351 | * this point but the state of the controller is |
| 352 | * unknown. Freeze the port to make sure the IRQ |
| 353 | * handler doesn't diddle with those qcs. This must |
| 354 | * be done atomically w.r.t. setting QCFLAG_FAILED. |
| 355 | */ |
| 356 | if (nr_timedout) |
| 357 | __ata_port_freeze(ap); |
| 358 | |
Tejun Heo | e30349d | 2006-07-03 03:02:15 +0900 | [diff] [blame] | 359 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | ad9e276 | 2006-05-15 20:58:12 +0900 | [diff] [blame] | 360 | } else |
Tejun Heo | e30349d | 2006-07-03 03:02:15 +0900 | [diff] [blame] | 361 | spin_unlock_wait(ap->lock); |
Tejun Heo | ad9e276 | 2006-05-15 20:58:12 +0900 | [diff] [blame] | 362 | |
| 363 | repeat: |
| 364 | /* invoke error handler */ |
| 365 | if (ap->ops->error_handler) { |
Tejun Heo | cf1b86c | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 366 | struct ata_link *link; |
| 367 | |
Tejun Heo | 5ddf24c | 2007-07-16 14:29:41 +0900 | [diff] [blame] | 368 | /* kill fast drain timer */ |
| 369 | del_timer_sync(&ap->fastdrain_timer); |
| 370 | |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 371 | /* process port resume request */ |
| 372 | ata_eh_handle_port_resume(ap); |
| 373 | |
Tejun Heo | f3e81b1 | 2006-05-15 20:58:21 +0900 | [diff] [blame] | 374 | /* fetch & clear EH info */ |
Tejun Heo | e30349d | 2006-07-03 03:02:15 +0900 | [diff] [blame] | 375 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | f3e81b1 | 2006-05-15 20:58:21 +0900 | [diff] [blame] | 376 | |
Tejun Heo | cf1b86c | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 377 | __ata_port_for_each_link(link, ap) { |
| 378 | memset(&link->eh_context, 0, sizeof(link->eh_context)); |
| 379 | link->eh_context.i = link->eh_info; |
| 380 | memset(&link->eh_info, 0, sizeof(link->eh_info)); |
| 381 | } |
Tejun Heo | f3e81b1 | 2006-05-15 20:58:21 +0900 | [diff] [blame] | 382 | |
Tejun Heo | b51e9e5 | 2006-06-29 01:29:30 +0900 | [diff] [blame] | 383 | ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS; |
| 384 | ap->pflags &= ~ATA_PFLAG_EH_PENDING; |
Tejun Heo | f3e81b1 | 2006-05-15 20:58:21 +0900 | [diff] [blame] | 385 | |
Tejun Heo | e30349d | 2006-07-03 03:02:15 +0900 | [diff] [blame] | 386 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | ad9e276 | 2006-05-15 20:58:12 +0900 | [diff] [blame] | 387 | |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 388 | /* invoke EH, skip if unloading or suspended */ |
| 389 | if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED))) |
Tejun Heo | 720ba12 | 2006-05-31 18:28:13 +0900 | [diff] [blame] | 390 | ap->ops->error_handler(ap); |
| 391 | else |
| 392 | ata_eh_finish(ap); |
Tejun Heo | ad9e276 | 2006-05-15 20:58:12 +0900 | [diff] [blame] | 393 | |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 394 | /* process port suspend request */ |
| 395 | ata_eh_handle_port_suspend(ap); |
| 396 | |
Tejun Heo | ad9e276 | 2006-05-15 20:58:12 +0900 | [diff] [blame] | 397 | /* Exception might have happend after ->error_handler |
| 398 | * recovered the port but before this point. Repeat |
| 399 | * EH in such case. |
| 400 | */ |
Tejun Heo | e30349d | 2006-07-03 03:02:15 +0900 | [diff] [blame] | 401 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | ad9e276 | 2006-05-15 20:58:12 +0900 | [diff] [blame] | 402 | |
Tejun Heo | b51e9e5 | 2006-06-29 01:29:30 +0900 | [diff] [blame] | 403 | if (ap->pflags & ATA_PFLAG_EH_PENDING) { |
Tejun Heo | ad9e276 | 2006-05-15 20:58:12 +0900 | [diff] [blame] | 404 | if (--repeat_cnt) { |
| 405 | ata_port_printk(ap, KERN_INFO, |
| 406 | "EH pending after completion, " |
| 407 | "repeating EH (cnt=%d)\n", repeat_cnt); |
Tejun Heo | e30349d | 2006-07-03 03:02:15 +0900 | [diff] [blame] | 408 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | ad9e276 | 2006-05-15 20:58:12 +0900 | [diff] [blame] | 409 | goto repeat; |
| 410 | } |
| 411 | ata_port_printk(ap, KERN_ERR, "EH pending after %d " |
| 412 | "tries, giving up\n", ATA_EH_MAX_REPEAT); |
Tejun Heo | 914616a | 2007-06-25 21:47:11 +0900 | [diff] [blame] | 413 | ap->pflags &= ~ATA_PFLAG_EH_PENDING; |
Tejun Heo | ad9e276 | 2006-05-15 20:58:12 +0900 | [diff] [blame] | 414 | } |
| 415 | |
Tejun Heo | f3e81b1 | 2006-05-15 20:58:21 +0900 | [diff] [blame] | 416 | /* this run is complete, make sure EH info is clear */ |
Tejun Heo | cf1b86c | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 417 | __ata_port_for_each_link(link, ap) |
| 418 | memset(&link->eh_info, 0, sizeof(link->eh_info)); |
Tejun Heo | f3e81b1 | 2006-05-15 20:58:21 +0900 | [diff] [blame] | 419 | |
Tejun Heo | e30349d | 2006-07-03 03:02:15 +0900 | [diff] [blame] | 420 | /* Clear host_eh_scheduled while holding ap->lock such |
Tejun Heo | ad9e276 | 2006-05-15 20:58:12 +0900 | [diff] [blame] | 421 | * that if exception occurs after this point but |
| 422 | * before EH completion, SCSI midlayer will |
| 423 | * re-initiate EH. |
| 424 | */ |
| 425 | host->host_eh_scheduled = 0; |
| 426 | |
Tejun Heo | e30349d | 2006-07-03 03:02:15 +0900 | [diff] [blame] | 427 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | ad9e276 | 2006-05-15 20:58:12 +0900 | [diff] [blame] | 428 | } else { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 429 | WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL); |
Tejun Heo | ad9e276 | 2006-05-15 20:58:12 +0900 | [diff] [blame] | 430 | ap->ops->eng_timeout(ap); |
| 431 | } |
| 432 | |
| 433 | /* finish or retry handled scmd's and clean up */ |
Tejun Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 434 | WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q)); |
| 435 | |
| 436 | scsi_eh_flush_done_q(&ap->eh_done_q); |
| 437 | |
Tejun Heo | ad9e276 | 2006-05-15 20:58:12 +0900 | [diff] [blame] | 438 | /* clean up */ |
Tejun Heo | e30349d | 2006-07-03 03:02:15 +0900 | [diff] [blame] | 439 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | ad9e276 | 2006-05-15 20:58:12 +0900 | [diff] [blame] | 440 | |
Tejun Heo | 1cdaf53 | 2006-07-03 16:07:26 +0900 | [diff] [blame] | 441 | if (ap->pflags & ATA_PFLAG_LOADING) |
Tejun Heo | b51e9e5 | 2006-06-29 01:29:30 +0900 | [diff] [blame] | 442 | ap->pflags &= ~ATA_PFLAG_LOADING; |
Tejun Heo | 1cdaf53 | 2006-07-03 16:07:26 +0900 | [diff] [blame] | 443 | else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG) |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 444 | queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0); |
Tejun Heo | 1cdaf53 | 2006-07-03 16:07:26 +0900 | [diff] [blame] | 445 | |
| 446 | if (ap->pflags & ATA_PFLAG_RECOVERED) |
| 447 | ata_port_printk(ap, KERN_INFO, "EH complete\n"); |
Tejun Heo | 580b210 | 2006-05-31 18:28:05 +0900 | [diff] [blame] | 448 | |
Tejun Heo | b51e9e5 | 2006-06-29 01:29:30 +0900 | [diff] [blame] | 449 | ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED); |
Tejun Heo | ad9e276 | 2006-05-15 20:58:12 +0900 | [diff] [blame] | 450 | |
Tejun Heo | c6cf9e9 | 2006-05-31 18:27:27 +0900 | [diff] [blame] | 451 | /* tell wait_eh that we're done */ |
Tejun Heo | b51e9e5 | 2006-06-29 01:29:30 +0900 | [diff] [blame] | 452 | ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS; |
Tejun Heo | c6cf9e9 | 2006-05-31 18:27:27 +0900 | [diff] [blame] | 453 | wake_up_all(&ap->eh_wait_q); |
| 454 | |
Tejun Heo | e30349d | 2006-07-03 03:02:15 +0900 | [diff] [blame] | 455 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | ad9e276 | 2006-05-15 20:58:12 +0900 | [diff] [blame] | 456 | |
Tejun Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 457 | DPRINTK("EXIT\n"); |
Tejun Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | /** |
Tejun Heo | c6cf9e9 | 2006-05-31 18:27:27 +0900 | [diff] [blame] | 461 | * ata_port_wait_eh - Wait for the currently pending EH to complete |
| 462 | * @ap: Port to wait EH for |
| 463 | * |
| 464 | * Wait until the currently pending EH is complete. |
| 465 | * |
| 466 | * LOCKING: |
| 467 | * Kernel thread context (may sleep). |
| 468 | */ |
| 469 | void ata_port_wait_eh(struct ata_port *ap) |
| 470 | { |
| 471 | unsigned long flags; |
| 472 | DEFINE_WAIT(wait); |
| 473 | |
| 474 | retry: |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 475 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | c6cf9e9 | 2006-05-31 18:27:27 +0900 | [diff] [blame] | 476 | |
Tejun Heo | b51e9e5 | 2006-06-29 01:29:30 +0900 | [diff] [blame] | 477 | while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) { |
Tejun Heo | c6cf9e9 | 2006-05-31 18:27:27 +0900 | [diff] [blame] | 478 | prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE); |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 479 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | c6cf9e9 | 2006-05-31 18:27:27 +0900 | [diff] [blame] | 480 | schedule(); |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 481 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | c6cf9e9 | 2006-05-31 18:27:27 +0900 | [diff] [blame] | 482 | } |
Tejun Heo | 0a1b622 | 2006-06-11 11:01:38 +0900 | [diff] [blame] | 483 | finish_wait(&ap->eh_wait_q, &wait); |
Tejun Heo | c6cf9e9 | 2006-05-31 18:27:27 +0900 | [diff] [blame] | 484 | |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 485 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | c6cf9e9 | 2006-05-31 18:27:27 +0900 | [diff] [blame] | 486 | |
| 487 | /* make sure SCSI EH is complete */ |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 488 | if (scsi_host_in_recovery(ap->scsi_host)) { |
Tejun Heo | c6cf9e9 | 2006-05-31 18:27:27 +0900 | [diff] [blame] | 489 | msleep(10); |
| 490 | goto retry; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | /** |
Tejun Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 495 | * ata_qc_timeout - Handle timeout of queued command |
| 496 | * @qc: Command that timed out |
| 497 | * |
| 498 | * Some part of the kernel (currently, only the SCSI layer) |
| 499 | * has noticed that the active command on port @ap has not |
| 500 | * completed after a specified length of time. Handle this |
| 501 | * condition by disabling DMA (if necessary) and completing |
| 502 | * transactions, with error if necessary. |
| 503 | * |
| 504 | * This also handles the case of the "lost interrupt", where |
| 505 | * for some reason (possibly hardware bug, possibly driver bug) |
| 506 | * an interrupt was not delivered to the driver, even though the |
| 507 | * transaction completed successfully. |
| 508 | * |
Tejun Heo | ad9e276 | 2006-05-15 20:58:12 +0900 | [diff] [blame] | 509 | * TODO: kill this function once old EH is gone. |
| 510 | * |
Tejun Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 511 | * LOCKING: |
| 512 | * Inherited from SCSI layer (none, can sleep) |
| 513 | */ |
| 514 | static void ata_qc_timeout(struct ata_queued_cmd *qc) |
| 515 | { |
| 516 | struct ata_port *ap = qc->ap; |
Tejun Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 517 | u8 host_stat = 0, drv_stat; |
| 518 | unsigned long flags; |
| 519 | |
| 520 | DPRINTK("ENTER\n"); |
| 521 | |
| 522 | ap->hsm_task_state = HSM_ST_IDLE; |
| 523 | |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 524 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 525 | |
| 526 | switch (qc->tf.protocol) { |
| 527 | |
| 528 | case ATA_PROT_DMA: |
| 529 | case ATA_PROT_ATAPI_DMA: |
| 530 | host_stat = ap->ops->bmdma_status(ap); |
| 531 | |
| 532 | /* before we do anything else, clear DMA-Start bit */ |
| 533 | ap->ops->bmdma_stop(qc); |
| 534 | |
| 535 | /* fall through */ |
| 536 | |
| 537 | default: |
| 538 | ata_altstatus(ap); |
| 539 | drv_stat = ata_chk_status(ap); |
| 540 | |
| 541 | /* ack bmdma irq events */ |
| 542 | ap->ops->irq_clear(ap); |
| 543 | |
Tejun Heo | f15a1da | 2006-05-15 20:57:56 +0900 | [diff] [blame] | 544 | ata_dev_printk(qc->dev, KERN_ERR, "command 0x%x timeout, " |
| 545 | "stat 0x%x host_stat 0x%x\n", |
| 546 | qc->tf.command, drv_stat, host_stat); |
Tejun Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 547 | |
| 548 | /* complete taskfile transaction */ |
Jeff Garzik | c13b56a | 2006-04-02 10:34:24 -0400 | [diff] [blame] | 549 | qc->err_mask |= AC_ERR_TIMEOUT; |
Tejun Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 550 | break; |
| 551 | } |
| 552 | |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 553 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 554 | |
| 555 | ata_eh_qc_complete(qc); |
| 556 | |
| 557 | DPRINTK("EXIT\n"); |
| 558 | } |
| 559 | |
| 560 | /** |
| 561 | * ata_eng_timeout - Handle timeout of queued command |
| 562 | * @ap: Port on which timed-out command is active |
| 563 | * |
| 564 | * Some part of the kernel (currently, only the SCSI layer) |
| 565 | * has noticed that the active command on port @ap has not |
| 566 | * completed after a specified length of time. Handle this |
| 567 | * condition by disabling DMA (if necessary) and completing |
| 568 | * transactions, with error if necessary. |
| 569 | * |
| 570 | * This also handles the case of the "lost interrupt", where |
| 571 | * for some reason (possibly hardware bug, possibly driver bug) |
| 572 | * an interrupt was not delivered to the driver, even though the |
| 573 | * transaction completed successfully. |
| 574 | * |
Tejun Heo | ad9e276 | 2006-05-15 20:58:12 +0900 | [diff] [blame] | 575 | * TODO: kill this function once old EH is gone. |
| 576 | * |
Tejun Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 577 | * LOCKING: |
| 578 | * Inherited from SCSI layer (none, can sleep) |
| 579 | */ |
| 580 | void ata_eng_timeout(struct ata_port *ap) |
| 581 | { |
| 582 | DPRINTK("ENTER\n"); |
| 583 | |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 584 | ata_qc_timeout(ata_qc_from_tag(ap, ap->link.active_tag)); |
Tejun Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 585 | |
| 586 | DPRINTK("EXIT\n"); |
| 587 | } |
| 588 | |
Tejun Heo | 5ddf24c | 2007-07-16 14:29:41 +0900 | [diff] [blame] | 589 | static int ata_eh_nr_in_flight(struct ata_port *ap) |
| 590 | { |
| 591 | unsigned int tag; |
| 592 | int nr = 0; |
| 593 | |
| 594 | /* count only non-internal commands */ |
| 595 | for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) |
| 596 | if (ata_qc_from_tag(ap, tag)) |
| 597 | nr++; |
| 598 | |
| 599 | return nr; |
| 600 | } |
| 601 | |
| 602 | void ata_eh_fastdrain_timerfn(unsigned long arg) |
| 603 | { |
| 604 | struct ata_port *ap = (void *)arg; |
| 605 | unsigned long flags; |
| 606 | int cnt; |
| 607 | |
| 608 | spin_lock_irqsave(ap->lock, flags); |
| 609 | |
| 610 | cnt = ata_eh_nr_in_flight(ap); |
| 611 | |
| 612 | /* are we done? */ |
| 613 | if (!cnt) |
| 614 | goto out_unlock; |
| 615 | |
| 616 | if (cnt == ap->fastdrain_cnt) { |
| 617 | unsigned int tag; |
| 618 | |
| 619 | /* No progress during the last interval, tag all |
| 620 | * in-flight qcs as timed out and freeze the port. |
| 621 | */ |
| 622 | for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) { |
| 623 | struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag); |
| 624 | if (qc) |
| 625 | qc->err_mask |= AC_ERR_TIMEOUT; |
| 626 | } |
| 627 | |
| 628 | ata_port_freeze(ap); |
| 629 | } else { |
| 630 | /* some qcs have finished, give it another chance */ |
| 631 | ap->fastdrain_cnt = cnt; |
| 632 | ap->fastdrain_timer.expires = |
| 633 | jiffies + ATA_EH_FASTDRAIN_INTERVAL; |
| 634 | add_timer(&ap->fastdrain_timer); |
| 635 | } |
| 636 | |
| 637 | out_unlock: |
| 638 | spin_unlock_irqrestore(ap->lock, flags); |
| 639 | } |
| 640 | |
| 641 | /** |
| 642 | * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain |
| 643 | * @ap: target ATA port |
| 644 | * @fastdrain: activate fast drain |
| 645 | * |
| 646 | * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain |
| 647 | * is non-zero and EH wasn't pending before. Fast drain ensures |
| 648 | * that EH kicks in in timely manner. |
| 649 | * |
| 650 | * LOCKING: |
| 651 | * spin_lock_irqsave(host lock) |
| 652 | */ |
| 653 | static void ata_eh_set_pending(struct ata_port *ap, int fastdrain) |
| 654 | { |
| 655 | int cnt; |
| 656 | |
| 657 | /* already scheduled? */ |
| 658 | if (ap->pflags & ATA_PFLAG_EH_PENDING) |
| 659 | return; |
| 660 | |
| 661 | ap->pflags |= ATA_PFLAG_EH_PENDING; |
| 662 | |
| 663 | if (!fastdrain) |
| 664 | return; |
| 665 | |
| 666 | /* do we have in-flight qcs? */ |
| 667 | cnt = ata_eh_nr_in_flight(ap); |
| 668 | if (!cnt) |
| 669 | return; |
| 670 | |
| 671 | /* activate fast drain */ |
| 672 | ap->fastdrain_cnt = cnt; |
| 673 | ap->fastdrain_timer.expires = jiffies + ATA_EH_FASTDRAIN_INTERVAL; |
| 674 | add_timer(&ap->fastdrain_timer); |
| 675 | } |
| 676 | |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 677 | /** |
| 678 | * ata_qc_schedule_eh - schedule qc for error handling |
| 679 | * @qc: command to schedule error handling for |
| 680 | * |
| 681 | * Schedule error handling for @qc. EH will kick in as soon as |
| 682 | * other commands are drained. |
| 683 | * |
| 684 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 685 | * spin_lock_irqsave(host lock) |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 686 | */ |
| 687 | void ata_qc_schedule_eh(struct ata_queued_cmd *qc) |
| 688 | { |
| 689 | struct ata_port *ap = qc->ap; |
| 690 | |
| 691 | WARN_ON(!ap->ops->error_handler); |
| 692 | |
| 693 | qc->flags |= ATA_QCFLAG_FAILED; |
Tejun Heo | 5ddf24c | 2007-07-16 14:29:41 +0900 | [diff] [blame] | 694 | ata_eh_set_pending(ap, 1); |
Tejun Heo | f686bcb | 2006-05-15 20:58:05 +0900 | [diff] [blame] | 695 | |
| 696 | /* The following will fail if timeout has already expired. |
| 697 | * ata_scsi_error() takes care of such scmds on EH entry. |
| 698 | * Note that ATA_QCFLAG_FAILED is unconditionally set after |
| 699 | * this function completes. |
| 700 | */ |
| 701 | scsi_req_abort_cmd(qc->scsicmd); |
| 702 | } |
| 703 | |
Tejun Heo | 7b70fc0 | 2006-05-15 20:58:07 +0900 | [diff] [blame] | 704 | /** |
| 705 | * ata_port_schedule_eh - schedule error handling without a qc |
| 706 | * @ap: ATA port to schedule EH for |
| 707 | * |
| 708 | * Schedule error handling for @ap. EH will kick in as soon as |
| 709 | * all commands are drained. |
| 710 | * |
| 711 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 712 | * spin_lock_irqsave(host lock) |
Tejun Heo | 7b70fc0 | 2006-05-15 20:58:07 +0900 | [diff] [blame] | 713 | */ |
| 714 | void ata_port_schedule_eh(struct ata_port *ap) |
| 715 | { |
| 716 | WARN_ON(!ap->ops->error_handler); |
| 717 | |
Tejun Heo | f4d6d00 | 2007-05-01 11:50:15 +0200 | [diff] [blame] | 718 | if (ap->pflags & ATA_PFLAG_INITIALIZING) |
| 719 | return; |
| 720 | |
Tejun Heo | 5ddf24c | 2007-07-16 14:29:41 +0900 | [diff] [blame] | 721 | ata_eh_set_pending(ap, 1); |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 722 | scsi_schedule_eh(ap->scsi_host); |
Tejun Heo | 7b70fc0 | 2006-05-15 20:58:07 +0900 | [diff] [blame] | 723 | |
| 724 | DPRINTK("port EH scheduled\n"); |
| 725 | } |
| 726 | |
Tejun Heo | dbd8261 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 727 | static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link) |
Tejun Heo | 7b70fc0 | 2006-05-15 20:58:07 +0900 | [diff] [blame] | 728 | { |
| 729 | int tag, nr_aborted = 0; |
| 730 | |
| 731 | WARN_ON(!ap->ops->error_handler); |
| 732 | |
Tejun Heo | 5ddf24c | 2007-07-16 14:29:41 +0900 | [diff] [blame] | 733 | /* we're gonna abort all commands, no need for fast drain */ |
| 734 | ata_eh_set_pending(ap, 0); |
| 735 | |
Tejun Heo | 7b70fc0 | 2006-05-15 20:58:07 +0900 | [diff] [blame] | 736 | for (tag = 0; tag < ATA_MAX_QUEUE; tag++) { |
| 737 | struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag); |
| 738 | |
Tejun Heo | dbd8261 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 739 | if (qc && (!link || qc->dev->link == link)) { |
Tejun Heo | 7b70fc0 | 2006-05-15 20:58:07 +0900 | [diff] [blame] | 740 | qc->flags |= ATA_QCFLAG_FAILED; |
| 741 | ata_qc_complete(qc); |
| 742 | nr_aborted++; |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | if (!nr_aborted) |
| 747 | ata_port_schedule_eh(ap); |
| 748 | |
| 749 | return nr_aborted; |
| 750 | } |
| 751 | |
Tejun Heo | e318049 | 2006-05-15 20:58:09 +0900 | [diff] [blame] | 752 | /** |
Tejun Heo | dbd8261 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 753 | * ata_link_abort - abort all qc's on the link |
| 754 | * @link: ATA link to abort qc's for |
| 755 | * |
| 756 | * Abort all active qc's active on @link and schedule EH. |
| 757 | * |
| 758 | * LOCKING: |
| 759 | * spin_lock_irqsave(host lock) |
| 760 | * |
| 761 | * RETURNS: |
| 762 | * Number of aborted qc's. |
| 763 | */ |
| 764 | int ata_link_abort(struct ata_link *link) |
| 765 | { |
| 766 | return ata_do_link_abort(link->ap, link); |
| 767 | } |
| 768 | |
| 769 | /** |
| 770 | * ata_port_abort - abort all qc's on the port |
| 771 | * @ap: ATA port to abort qc's for |
| 772 | * |
| 773 | * Abort all active qc's of @ap and schedule EH. |
| 774 | * |
| 775 | * LOCKING: |
| 776 | * spin_lock_irqsave(host_set lock) |
| 777 | * |
| 778 | * RETURNS: |
| 779 | * Number of aborted qc's. |
| 780 | */ |
| 781 | int ata_port_abort(struct ata_port *ap) |
| 782 | { |
| 783 | return ata_do_link_abort(ap, NULL); |
| 784 | } |
| 785 | |
| 786 | /** |
Tejun Heo | e318049 | 2006-05-15 20:58:09 +0900 | [diff] [blame] | 787 | * __ata_port_freeze - freeze port |
| 788 | * @ap: ATA port to freeze |
| 789 | * |
| 790 | * This function is called when HSM violation or some other |
| 791 | * condition disrupts normal operation of the port. Frozen port |
| 792 | * is not allowed to perform any operation until the port is |
| 793 | * thawed, which usually follows a successful reset. |
| 794 | * |
| 795 | * ap->ops->freeze() callback can be used for freezing the port |
| 796 | * hardware-wise (e.g. mask interrupt and stop DMA engine). If a |
| 797 | * port cannot be frozen hardware-wise, the interrupt handler |
| 798 | * must ack and clear interrupts unconditionally while the port |
| 799 | * is frozen. |
| 800 | * |
| 801 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 802 | * spin_lock_irqsave(host lock) |
Tejun Heo | e318049 | 2006-05-15 20:58:09 +0900 | [diff] [blame] | 803 | */ |
| 804 | static void __ata_port_freeze(struct ata_port *ap) |
| 805 | { |
| 806 | WARN_ON(!ap->ops->error_handler); |
| 807 | |
| 808 | if (ap->ops->freeze) |
| 809 | ap->ops->freeze(ap); |
| 810 | |
Tejun Heo | b51e9e5 | 2006-06-29 01:29:30 +0900 | [diff] [blame] | 811 | ap->pflags |= ATA_PFLAG_FROZEN; |
Tejun Heo | e318049 | 2006-05-15 20:58:09 +0900 | [diff] [blame] | 812 | |
Tejun Heo | 44877b4 | 2007-02-21 01:06:51 +0900 | [diff] [blame] | 813 | DPRINTK("ata%u port frozen\n", ap->print_id); |
Tejun Heo | e318049 | 2006-05-15 20:58:09 +0900 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | /** |
| 817 | * ata_port_freeze - abort & freeze port |
| 818 | * @ap: ATA port to freeze |
| 819 | * |
| 820 | * Abort and freeze @ap. |
| 821 | * |
| 822 | * LOCKING: |
Jeff Garzik | cca3974 | 2006-08-24 03:19:22 -0400 | [diff] [blame] | 823 | * spin_lock_irqsave(host lock) |
Tejun Heo | e318049 | 2006-05-15 20:58:09 +0900 | [diff] [blame] | 824 | * |
| 825 | * RETURNS: |
| 826 | * Number of aborted commands. |
| 827 | */ |
| 828 | int ata_port_freeze(struct ata_port *ap) |
| 829 | { |
| 830 | int nr_aborted; |
| 831 | |
| 832 | WARN_ON(!ap->ops->error_handler); |
| 833 | |
| 834 | nr_aborted = ata_port_abort(ap); |
| 835 | __ata_port_freeze(ap); |
| 836 | |
| 837 | return nr_aborted; |
| 838 | } |
| 839 | |
| 840 | /** |
| 841 | * ata_eh_freeze_port - EH helper to freeze port |
| 842 | * @ap: ATA port to freeze |
| 843 | * |
| 844 | * Freeze @ap. |
| 845 | * |
| 846 | * LOCKING: |
| 847 | * None. |
| 848 | */ |
| 849 | void ata_eh_freeze_port(struct ata_port *ap) |
| 850 | { |
| 851 | unsigned long flags; |
| 852 | |
| 853 | if (!ap->ops->error_handler) |
| 854 | return; |
| 855 | |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 856 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | e318049 | 2006-05-15 20:58:09 +0900 | [diff] [blame] | 857 | __ata_port_freeze(ap); |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 858 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | e318049 | 2006-05-15 20:58:09 +0900 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | /** |
| 862 | * ata_port_thaw_port - EH helper to thaw port |
| 863 | * @ap: ATA port to thaw |
| 864 | * |
| 865 | * Thaw frozen port @ap. |
| 866 | * |
| 867 | * LOCKING: |
| 868 | * None. |
| 869 | */ |
| 870 | void ata_eh_thaw_port(struct ata_port *ap) |
| 871 | { |
| 872 | unsigned long flags; |
| 873 | |
| 874 | if (!ap->ops->error_handler) |
| 875 | return; |
| 876 | |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 877 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | e318049 | 2006-05-15 20:58:09 +0900 | [diff] [blame] | 878 | |
Tejun Heo | b51e9e5 | 2006-06-29 01:29:30 +0900 | [diff] [blame] | 879 | ap->pflags &= ~ATA_PFLAG_FROZEN; |
Tejun Heo | e318049 | 2006-05-15 20:58:09 +0900 | [diff] [blame] | 880 | |
| 881 | if (ap->ops->thaw) |
| 882 | ap->ops->thaw(ap); |
| 883 | |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 884 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | e318049 | 2006-05-15 20:58:09 +0900 | [diff] [blame] | 885 | |
Tejun Heo | 44877b4 | 2007-02-21 01:06:51 +0900 | [diff] [blame] | 886 | DPRINTK("ata%u port thawed\n", ap->print_id); |
Tejun Heo | e318049 | 2006-05-15 20:58:09 +0900 | [diff] [blame] | 887 | } |
| 888 | |
Tejun Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 889 | static void ata_eh_scsidone(struct scsi_cmnd *scmd) |
| 890 | { |
| 891 | /* nada */ |
| 892 | } |
| 893 | |
| 894 | static void __ata_eh_qc_complete(struct ata_queued_cmd *qc) |
| 895 | { |
| 896 | struct ata_port *ap = qc->ap; |
| 897 | struct scsi_cmnd *scmd = qc->scsicmd; |
| 898 | unsigned long flags; |
| 899 | |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 900 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 901 | qc->scsidone = ata_eh_scsidone; |
| 902 | __ata_qc_complete(qc); |
| 903 | WARN_ON(ata_tag_valid(qc->tag)); |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 904 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 905 | |
| 906 | scsi_eh_finish_cmd(scmd, &ap->eh_done_q); |
| 907 | } |
| 908 | |
| 909 | /** |
| 910 | * ata_eh_qc_complete - Complete an active ATA command from EH |
| 911 | * @qc: Command to complete |
| 912 | * |
| 913 | * Indicate to the mid and upper layers that an ATA command has |
| 914 | * completed. To be used from EH. |
| 915 | */ |
| 916 | void ata_eh_qc_complete(struct ata_queued_cmd *qc) |
| 917 | { |
| 918 | struct scsi_cmnd *scmd = qc->scsicmd; |
| 919 | scmd->retries = scmd->allowed; |
| 920 | __ata_eh_qc_complete(qc); |
| 921 | } |
| 922 | |
| 923 | /** |
| 924 | * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH |
| 925 | * @qc: Command to retry |
| 926 | * |
| 927 | * Indicate to the mid and upper layers that an ATA command |
| 928 | * should be retried. To be used from EH. |
| 929 | * |
| 930 | * SCSI midlayer limits the number of retries to scmd->allowed. |
| 931 | * scmd->retries is decremented for commands which get retried |
| 932 | * due to unrelated failures (qc->err_mask is zero). |
| 933 | */ |
| 934 | void ata_eh_qc_retry(struct ata_queued_cmd *qc) |
| 935 | { |
| 936 | struct scsi_cmnd *scmd = qc->scsicmd; |
| 937 | if (!qc->err_mask && scmd->retries) |
| 938 | scmd->retries--; |
| 939 | __ata_eh_qc_complete(qc); |
| 940 | } |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 941 | |
| 942 | /** |
Tejun Heo | 0ea035a | 2006-05-31 18:28:01 +0900 | [diff] [blame] | 943 | * ata_eh_detach_dev - detach ATA device |
| 944 | * @dev: ATA device to detach |
| 945 | * |
| 946 | * Detach @dev. |
| 947 | * |
| 948 | * LOCKING: |
| 949 | * None. |
| 950 | */ |
| 951 | static void ata_eh_detach_dev(struct ata_device *dev) |
| 952 | { |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 953 | struct ata_link *link = dev->link; |
| 954 | struct ata_port *ap = link->ap; |
Tejun Heo | 0ea035a | 2006-05-31 18:28:01 +0900 | [diff] [blame] | 955 | unsigned long flags; |
| 956 | |
| 957 | ata_dev_disable(dev); |
| 958 | |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 959 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | 0ea035a | 2006-05-31 18:28:01 +0900 | [diff] [blame] | 960 | |
| 961 | dev->flags &= ~ATA_DFLAG_DETACH; |
| 962 | |
| 963 | if (ata_scsi_offline_dev(dev)) { |
| 964 | dev->flags |= ATA_DFLAG_DETACHED; |
Tejun Heo | b51e9e5 | 2006-06-29 01:29:30 +0900 | [diff] [blame] | 965 | ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG; |
Tejun Heo | 0ea035a | 2006-05-31 18:28:01 +0900 | [diff] [blame] | 966 | } |
| 967 | |
Tejun Heo | beb07c1 | 2006-06-24 20:30:19 +0900 | [diff] [blame] | 968 | /* clear per-dev EH actions */ |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 969 | ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK); |
| 970 | ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK); |
Tejun Heo | beb07c1 | 2006-06-24 20:30:19 +0900 | [diff] [blame] | 971 | |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 972 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | 0ea035a | 2006-05-31 18:28:01 +0900 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | /** |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 976 | * ata_eh_about_to_do - about to perform eh_action |
Tejun Heo | 955e57d | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 977 | * @link: target ATA link |
Tejun Heo | 47005f2 | 2006-06-19 18:27:23 +0900 | [diff] [blame] | 978 | * @dev: target ATA dev for per-dev action (can be NULL) |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 979 | * @action: action about to be performed |
| 980 | * |
| 981 | * Called just before performing EH actions to clear related bits |
Tejun Heo | 955e57d | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 982 | * in @link->eh_info such that eh actions are not unnecessarily |
| 983 | * repeated. |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 984 | * |
| 985 | * LOCKING: |
| 986 | * None. |
| 987 | */ |
Tejun Heo | 955e57d | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 988 | static void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev, |
Tejun Heo | 47005f2 | 2006-06-19 18:27:23 +0900 | [diff] [blame] | 989 | unsigned int action) |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 990 | { |
Tejun Heo | 955e57d | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 991 | struct ata_port *ap = link->ap; |
| 992 | struct ata_eh_info *ehi = &link->eh_info; |
| 993 | struct ata_eh_context *ehc = &link->eh_context; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 994 | unsigned long flags; |
| 995 | |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 996 | spin_lock_irqsave(ap->lock, flags); |
Tejun Heo | 1cdaf53 | 2006-07-03 16:07:26 +0900 | [diff] [blame] | 997 | |
Tejun Heo | 13abf50 | 2006-07-10 23:18:46 +0900 | [diff] [blame] | 998 | /* Reset is represented by combination of actions and EHI |
| 999 | * flags. Suck in all related bits before clearing eh_info to |
| 1000 | * avoid losing requested action. |
| 1001 | */ |
| 1002 | if (action & ATA_EH_RESET_MASK) { |
| 1003 | ehc->i.action |= ehi->action & ATA_EH_RESET_MASK; |
| 1004 | ehc->i.flags |= ehi->flags & ATA_EHI_RESET_MODIFIER_MASK; |
Tejun Heo | 1cdaf53 | 2006-07-03 16:07:26 +0900 | [diff] [blame] | 1005 | |
Tejun Heo | 13abf50 | 2006-07-10 23:18:46 +0900 | [diff] [blame] | 1006 | /* make sure all reset actions are cleared & clear EHI flags */ |
| 1007 | action |= ATA_EH_RESET_MASK; |
| 1008 | ehi->flags &= ~ATA_EHI_RESET_MODIFIER_MASK; |
| 1009 | } |
| 1010 | |
Tejun Heo | 955e57d | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1011 | ata_eh_clear_action(link, dev, ehi, action); |
Tejun Heo | 13abf50 | 2006-07-10 23:18:46 +0900 | [diff] [blame] | 1012 | |
| 1013 | if (!(ehc->i.flags & ATA_EHI_QUIET)) |
Tejun Heo | 1cdaf53 | 2006-07-03 16:07:26 +0900 | [diff] [blame] | 1014 | ap->pflags |= ATA_PFLAG_RECOVERED; |
| 1015 | |
Jeff Garzik | ba6a130 | 2006-06-22 23:46:10 -0400 | [diff] [blame] | 1016 | spin_unlock_irqrestore(ap->lock, flags); |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | /** |
Tejun Heo | 47005f2 | 2006-06-19 18:27:23 +0900 | [diff] [blame] | 1020 | * ata_eh_done - EH action complete |
Tejun Heo | 955e57d | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1021 | * @ap: target ATA port |
Tejun Heo | 47005f2 | 2006-06-19 18:27:23 +0900 | [diff] [blame] | 1022 | * @dev: target ATA dev for per-dev action (can be NULL) |
| 1023 | * @action: action just completed |
| 1024 | * |
| 1025 | * Called right after performing EH actions to clear related bits |
Tejun Heo | 955e57d | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1026 | * in @link->eh_context. |
Tejun Heo | 47005f2 | 2006-06-19 18:27:23 +0900 | [diff] [blame] | 1027 | * |
| 1028 | * LOCKING: |
| 1029 | * None. |
| 1030 | */ |
Tejun Heo | 955e57d | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1031 | static void ata_eh_done(struct ata_link *link, struct ata_device *dev, |
Tejun Heo | 47005f2 | 2006-06-19 18:27:23 +0900 | [diff] [blame] | 1032 | unsigned int action) |
| 1033 | { |
Tejun Heo | 955e57d | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1034 | struct ata_eh_context *ehc = &link->eh_context; |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 1035 | |
Tejun Heo | 13abf50 | 2006-07-10 23:18:46 +0900 | [diff] [blame] | 1036 | /* if reset is complete, clear all reset actions & reset modifier */ |
| 1037 | if (action & ATA_EH_RESET_MASK) { |
| 1038 | action |= ATA_EH_RESET_MASK; |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 1039 | ehc->i.flags &= ~ATA_EHI_RESET_MODIFIER_MASK; |
Tejun Heo | 13abf50 | 2006-07-10 23:18:46 +0900 | [diff] [blame] | 1040 | } |
| 1041 | |
Tejun Heo | 955e57d | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1042 | ata_eh_clear_action(link, dev, &ehc->i, action); |
Tejun Heo | 47005f2 | 2006-06-19 18:27:23 +0900 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | /** |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1046 | * ata_err_string - convert err_mask to descriptive string |
| 1047 | * @err_mask: error mask to convert to string |
| 1048 | * |
| 1049 | * Convert @err_mask to descriptive string. Errors are |
| 1050 | * prioritized according to severity and only the most severe |
| 1051 | * error is reported. |
| 1052 | * |
| 1053 | * LOCKING: |
| 1054 | * None. |
| 1055 | * |
| 1056 | * RETURNS: |
| 1057 | * Descriptive string for @err_mask |
| 1058 | */ |
| 1059 | static const char * ata_err_string(unsigned int err_mask) |
| 1060 | { |
| 1061 | if (err_mask & AC_ERR_HOST_BUS) |
| 1062 | return "host bus error"; |
| 1063 | if (err_mask & AC_ERR_ATA_BUS) |
| 1064 | return "ATA bus error"; |
| 1065 | if (err_mask & AC_ERR_TIMEOUT) |
| 1066 | return "timeout"; |
| 1067 | if (err_mask & AC_ERR_HSM) |
| 1068 | return "HSM violation"; |
| 1069 | if (err_mask & AC_ERR_SYSTEM) |
| 1070 | return "internal error"; |
| 1071 | if (err_mask & AC_ERR_MEDIA) |
| 1072 | return "media error"; |
| 1073 | if (err_mask & AC_ERR_INVALID) |
| 1074 | return "invalid argument"; |
| 1075 | if (err_mask & AC_ERR_DEV) |
| 1076 | return "device error"; |
| 1077 | return "unknown error"; |
| 1078 | } |
| 1079 | |
| 1080 | /** |
Tejun Heo | e8ee845 | 2006-05-15 21:03:46 +0900 | [diff] [blame] | 1081 | * ata_read_log_page - read a specific log page |
| 1082 | * @dev: target device |
| 1083 | * @page: page to read |
| 1084 | * @buf: buffer to store read page |
| 1085 | * @sectors: number of sectors to read |
| 1086 | * |
| 1087 | * Read log page using READ_LOG_EXT command. |
| 1088 | * |
| 1089 | * LOCKING: |
| 1090 | * Kernel thread context (may sleep). |
| 1091 | * |
| 1092 | * RETURNS: |
| 1093 | * 0 on success, AC_ERR_* mask otherwise. |
| 1094 | */ |
| 1095 | static unsigned int ata_read_log_page(struct ata_device *dev, |
| 1096 | u8 page, void *buf, unsigned int sectors) |
| 1097 | { |
| 1098 | struct ata_taskfile tf; |
| 1099 | unsigned int err_mask; |
| 1100 | |
| 1101 | DPRINTK("read log page - page %d\n", page); |
| 1102 | |
| 1103 | ata_tf_init(dev, &tf); |
| 1104 | tf.command = ATA_CMD_READ_LOG_EXT; |
| 1105 | tf.lbal = page; |
| 1106 | tf.nsect = sectors; |
| 1107 | tf.hob_nsect = sectors >> 8; |
| 1108 | tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE; |
| 1109 | tf.protocol = ATA_PROT_PIO; |
| 1110 | |
| 1111 | err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE, |
| 1112 | buf, sectors * ATA_SECT_SIZE); |
| 1113 | |
| 1114 | DPRINTK("EXIT, err_mask=%x\n", err_mask); |
| 1115 | return err_mask; |
| 1116 | } |
| 1117 | |
| 1118 | /** |
| 1119 | * ata_eh_read_log_10h - Read log page 10h for NCQ error details |
| 1120 | * @dev: Device to read log page 10h from |
| 1121 | * @tag: Resulting tag of the failed command |
| 1122 | * @tf: Resulting taskfile registers of the failed command |
| 1123 | * |
| 1124 | * Read log page 10h to obtain NCQ error details and clear error |
| 1125 | * condition. |
| 1126 | * |
| 1127 | * LOCKING: |
| 1128 | * Kernel thread context (may sleep). |
| 1129 | * |
| 1130 | * RETURNS: |
| 1131 | * 0 on success, -errno otherwise. |
| 1132 | */ |
| 1133 | static int ata_eh_read_log_10h(struct ata_device *dev, |
| 1134 | int *tag, struct ata_taskfile *tf) |
| 1135 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 1136 | u8 *buf = dev->link->ap->sector_buf; |
Tejun Heo | e8ee845 | 2006-05-15 21:03:46 +0900 | [diff] [blame] | 1137 | unsigned int err_mask; |
| 1138 | u8 csum; |
| 1139 | int i; |
| 1140 | |
| 1141 | err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1); |
| 1142 | if (err_mask) |
| 1143 | return -EIO; |
| 1144 | |
| 1145 | csum = 0; |
| 1146 | for (i = 0; i < ATA_SECT_SIZE; i++) |
| 1147 | csum += buf[i]; |
| 1148 | if (csum) |
| 1149 | ata_dev_printk(dev, KERN_WARNING, |
| 1150 | "invalid checksum 0x%x on log page 10h\n", csum); |
| 1151 | |
| 1152 | if (buf[0] & 0x80) |
| 1153 | return -ENOENT; |
| 1154 | |
| 1155 | *tag = buf[0] & 0x1f; |
| 1156 | |
| 1157 | tf->command = buf[2]; |
| 1158 | tf->feature = buf[3]; |
| 1159 | tf->lbal = buf[4]; |
| 1160 | tf->lbam = buf[5]; |
| 1161 | tf->lbah = buf[6]; |
| 1162 | tf->device = buf[7]; |
| 1163 | tf->hob_lbal = buf[8]; |
| 1164 | tf->hob_lbam = buf[9]; |
| 1165 | tf->hob_lbah = buf[10]; |
| 1166 | tf->nsect = buf[12]; |
| 1167 | tf->hob_nsect = buf[13]; |
| 1168 | |
| 1169 | return 0; |
| 1170 | } |
| 1171 | |
| 1172 | /** |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1173 | * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE |
| 1174 | * @dev: device to perform REQUEST_SENSE to |
| 1175 | * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long) |
| 1176 | * |
| 1177 | * Perform ATAPI REQUEST_SENSE after the device reported CHECK |
| 1178 | * SENSE. This function is EH helper. |
| 1179 | * |
| 1180 | * LOCKING: |
| 1181 | * Kernel thread context (may sleep). |
| 1182 | * |
| 1183 | * RETURNS: |
| 1184 | * 0 on success, AC_ERR_* mask on failure |
| 1185 | */ |
Albert Lee | 5628776 | 2007-04-02 11:30:46 +0800 | [diff] [blame] | 1186 | static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc) |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1187 | { |
Albert Lee | 5628776 | 2007-04-02 11:30:46 +0800 | [diff] [blame] | 1188 | struct ata_device *dev = qc->dev; |
| 1189 | unsigned char *sense_buf = qc->scsicmd->sense_buffer; |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 1190 | struct ata_port *ap = dev->link->ap; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1191 | struct ata_taskfile tf; |
| 1192 | u8 cdb[ATAPI_CDB_LEN]; |
| 1193 | |
| 1194 | DPRINTK("ATAPI request sense\n"); |
| 1195 | |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1196 | /* FIXME: is this needed? */ |
| 1197 | memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE); |
| 1198 | |
Albert Lee | 5628776 | 2007-04-02 11:30:46 +0800 | [diff] [blame] | 1199 | /* initialize sense_buf with the error register, |
| 1200 | * for the case where they are -not- overwritten |
| 1201 | */ |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1202 | sense_buf[0] = 0x70; |
Albert Lee | 5628776 | 2007-04-02 11:30:46 +0800 | [diff] [blame] | 1203 | sense_buf[2] = qc->result_tf.feature >> 4; |
| 1204 | |
Jeff Garzik | a617c09 | 2007-05-21 20:14:23 -0400 | [diff] [blame] | 1205 | /* some devices time out if garbage left in tf */ |
Albert Lee | 5628776 | 2007-04-02 11:30:46 +0800 | [diff] [blame] | 1206 | ata_tf_init(dev, &tf); |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1207 | |
| 1208 | memset(cdb, 0, ATAPI_CDB_LEN); |
| 1209 | cdb[0] = REQUEST_SENSE; |
| 1210 | cdb[4] = SCSI_SENSE_BUFFERSIZE; |
| 1211 | |
| 1212 | tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
| 1213 | tf.command = ATA_CMD_PACKET; |
| 1214 | |
| 1215 | /* is it pointless to prefer PIO for "safety reasons"? */ |
| 1216 | if (ap->flags & ATA_FLAG_PIO_DMA) { |
| 1217 | tf.protocol = ATA_PROT_ATAPI_DMA; |
| 1218 | tf.feature |= ATAPI_PKT_DMA; |
| 1219 | } else { |
| 1220 | tf.protocol = ATA_PROT_ATAPI; |
| 1221 | tf.lbam = (8 * 1024) & 0xff; |
| 1222 | tf.lbah = (8 * 1024) >> 8; |
| 1223 | } |
| 1224 | |
| 1225 | return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE, |
| 1226 | sense_buf, SCSI_SENSE_BUFFERSIZE); |
| 1227 | } |
| 1228 | |
| 1229 | /** |
| 1230 | * ata_eh_analyze_serror - analyze SError for a failed port |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1231 | * @link: ATA link to analyze SError for |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1232 | * |
| 1233 | * Analyze SError if available and further determine cause of |
| 1234 | * failure. |
| 1235 | * |
| 1236 | * LOCKING: |
| 1237 | * None. |
| 1238 | */ |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1239 | static void ata_eh_analyze_serror(struct ata_link *link) |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1240 | { |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1241 | struct ata_eh_context *ehc = &link->eh_context; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1242 | u32 serror = ehc->i.serror; |
| 1243 | unsigned int err_mask = 0, action = 0; |
| 1244 | |
| 1245 | if (serror & SERR_PERSISTENT) { |
| 1246 | err_mask |= AC_ERR_ATA_BUS; |
| 1247 | action |= ATA_EH_HARDRESET; |
| 1248 | } |
| 1249 | if (serror & |
| 1250 | (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) { |
| 1251 | err_mask |= AC_ERR_ATA_BUS; |
| 1252 | action |= ATA_EH_SOFTRESET; |
| 1253 | } |
| 1254 | if (serror & SERR_PROTOCOL) { |
| 1255 | err_mask |= AC_ERR_HSM; |
| 1256 | action |= ATA_EH_SOFTRESET; |
| 1257 | } |
| 1258 | if (serror & SERR_INTERNAL) { |
| 1259 | err_mask |= AC_ERR_SYSTEM; |
Tejun Heo | 771b8da | 2007-03-14 01:20:51 +0900 | [diff] [blame] | 1260 | action |= ATA_EH_HARDRESET; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1261 | } |
Tejun Heo | 084fe63 | 2006-05-31 18:28:03 +0900 | [diff] [blame] | 1262 | if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG)) |
| 1263 | ata_ehi_hotplugged(&ehc->i); |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1264 | |
| 1265 | ehc->i.err_mask |= err_mask; |
| 1266 | ehc->i.action |= action; |
| 1267 | } |
| 1268 | |
| 1269 | /** |
Tejun Heo | e8ee845 | 2006-05-15 21:03:46 +0900 | [diff] [blame] | 1270 | * ata_eh_analyze_ncq_error - analyze NCQ error |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1271 | * @link: ATA link to analyze NCQ error for |
Tejun Heo | e8ee845 | 2006-05-15 21:03:46 +0900 | [diff] [blame] | 1272 | * |
| 1273 | * Read log page 10h, determine the offending qc and acquire |
| 1274 | * error status TF. For NCQ device errors, all LLDDs have to do |
| 1275 | * is setting AC_ERR_DEV in ehi->err_mask. This function takes |
| 1276 | * care of the rest. |
| 1277 | * |
| 1278 | * LOCKING: |
| 1279 | * Kernel thread context (may sleep). |
| 1280 | */ |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1281 | static void ata_eh_analyze_ncq_error(struct ata_link *link) |
Tejun Heo | e8ee845 | 2006-05-15 21:03:46 +0900 | [diff] [blame] | 1282 | { |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1283 | struct ata_port *ap = link->ap; |
| 1284 | struct ata_eh_context *ehc = &link->eh_context; |
| 1285 | struct ata_device *dev = link->device; |
Tejun Heo | e8ee845 | 2006-05-15 21:03:46 +0900 | [diff] [blame] | 1286 | struct ata_queued_cmd *qc; |
| 1287 | struct ata_taskfile tf; |
| 1288 | int tag, rc; |
| 1289 | |
| 1290 | /* if frozen, we can't do much */ |
Tejun Heo | b51e9e5 | 2006-06-29 01:29:30 +0900 | [diff] [blame] | 1291 | if (ap->pflags & ATA_PFLAG_FROZEN) |
Tejun Heo | e8ee845 | 2006-05-15 21:03:46 +0900 | [diff] [blame] | 1292 | return; |
| 1293 | |
| 1294 | /* is it NCQ device error? */ |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1295 | if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV)) |
Tejun Heo | e8ee845 | 2006-05-15 21:03:46 +0900 | [diff] [blame] | 1296 | return; |
| 1297 | |
| 1298 | /* has LLDD analyzed already? */ |
| 1299 | for (tag = 0; tag < ATA_MAX_QUEUE; tag++) { |
| 1300 | qc = __ata_qc_from_tag(ap, tag); |
| 1301 | |
| 1302 | if (!(qc->flags & ATA_QCFLAG_FAILED)) |
| 1303 | continue; |
| 1304 | |
| 1305 | if (qc->err_mask) |
| 1306 | return; |
| 1307 | } |
| 1308 | |
| 1309 | /* okay, this error is ours */ |
| 1310 | rc = ata_eh_read_log_10h(dev, &tag, &tf); |
| 1311 | if (rc) { |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1312 | ata_link_printk(link, KERN_ERR, "failed to read log page 10h " |
Tejun Heo | e8ee845 | 2006-05-15 21:03:46 +0900 | [diff] [blame] | 1313 | "(errno=%d)\n", rc); |
| 1314 | return; |
| 1315 | } |
| 1316 | |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1317 | if (!(link->sactive & (1 << tag))) { |
| 1318 | ata_link_printk(link, KERN_ERR, "log page 10h reported " |
Tejun Heo | e8ee845 | 2006-05-15 21:03:46 +0900 | [diff] [blame] | 1319 | "inactive tag %d\n", tag); |
| 1320 | return; |
| 1321 | } |
| 1322 | |
| 1323 | /* we've got the perpetrator, condemn it */ |
| 1324 | qc = __ata_qc_from_tag(ap, tag); |
| 1325 | memcpy(&qc->result_tf, &tf, sizeof(tf)); |
Tejun Heo | 5335b72 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 1326 | qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ; |
Tejun Heo | e8ee845 | 2006-05-15 21:03:46 +0900 | [diff] [blame] | 1327 | ehc->i.err_mask &= ~AC_ERR_DEV; |
| 1328 | } |
| 1329 | |
| 1330 | /** |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1331 | * ata_eh_analyze_tf - analyze taskfile of a failed qc |
| 1332 | * @qc: qc to analyze |
| 1333 | * @tf: Taskfile registers to analyze |
| 1334 | * |
| 1335 | * Analyze taskfile of @qc and further determine cause of |
| 1336 | * failure. This function also requests ATAPI sense data if |
| 1337 | * avaliable. |
| 1338 | * |
| 1339 | * LOCKING: |
| 1340 | * Kernel thread context (may sleep). |
| 1341 | * |
| 1342 | * RETURNS: |
| 1343 | * Determined recovery action |
| 1344 | */ |
| 1345 | static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc, |
| 1346 | const struct ata_taskfile *tf) |
| 1347 | { |
| 1348 | unsigned int tmp, action = 0; |
| 1349 | u8 stat = tf->command, err = tf->feature; |
| 1350 | |
| 1351 | if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) { |
| 1352 | qc->err_mask |= AC_ERR_HSM; |
| 1353 | return ATA_EH_SOFTRESET; |
| 1354 | } |
| 1355 | |
Tejun Heo | a51d644 | 2007-03-20 15:24:11 +0900 | [diff] [blame] | 1356 | if (stat & (ATA_ERR | ATA_DF)) |
| 1357 | qc->err_mask |= AC_ERR_DEV; |
| 1358 | else |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1359 | return 0; |
| 1360 | |
| 1361 | switch (qc->dev->class) { |
| 1362 | case ATA_DEV_ATA: |
| 1363 | if (err & ATA_ICRC) |
| 1364 | qc->err_mask |= AC_ERR_ATA_BUS; |
| 1365 | if (err & ATA_UNC) |
| 1366 | qc->err_mask |= AC_ERR_MEDIA; |
| 1367 | if (err & ATA_IDNF) |
| 1368 | qc->err_mask |= AC_ERR_INVALID; |
| 1369 | break; |
| 1370 | |
| 1371 | case ATA_DEV_ATAPI: |
Tejun Heo | a569a30 | 2006-11-21 10:40:51 +0900 | [diff] [blame] | 1372 | if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) { |
Albert Lee | 5628776 | 2007-04-02 11:30:46 +0800 | [diff] [blame] | 1373 | tmp = atapi_eh_request_sense(qc); |
Tejun Heo | a569a30 | 2006-11-21 10:40:51 +0900 | [diff] [blame] | 1374 | if (!tmp) { |
| 1375 | /* ATA_QCFLAG_SENSE_VALID is used to |
| 1376 | * tell atapi_qc_complete() that sense |
| 1377 | * data is already valid. |
| 1378 | * |
| 1379 | * TODO: interpret sense data and set |
| 1380 | * appropriate err_mask. |
| 1381 | */ |
| 1382 | qc->flags |= ATA_QCFLAG_SENSE_VALID; |
| 1383 | } else |
| 1384 | qc->err_mask |= tmp; |
| 1385 | } |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS)) |
| 1389 | action |= ATA_EH_SOFTRESET; |
| 1390 | |
| 1391 | return action; |
| 1392 | } |
| 1393 | |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1394 | static int ata_eh_categorize_error(int is_io, unsigned int err_mask) |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1395 | { |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1396 | if (err_mask & AC_ERR_ATA_BUS) |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1397 | return 1; |
| 1398 | |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1399 | if (err_mask & AC_ERR_TIMEOUT) |
| 1400 | return 2; |
| 1401 | |
| 1402 | if (is_io) { |
| 1403 | if (err_mask & AC_ERR_HSM) |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1404 | return 2; |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1405 | if ((err_mask & |
| 1406 | (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV) |
| 1407 | return 3; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | return 0; |
| 1411 | } |
| 1412 | |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1413 | struct speed_down_verdict_arg { |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1414 | u64 since; |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1415 | int nr_errors[4]; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1416 | }; |
| 1417 | |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1418 | static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg) |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1419 | { |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1420 | struct speed_down_verdict_arg *arg = void_arg; |
| 1421 | int cat = ata_eh_categorize_error(ent->is_io, ent->err_mask); |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1422 | |
| 1423 | if (ent->timestamp < arg->since) |
| 1424 | return -1; |
| 1425 | |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1426 | arg->nr_errors[cat]++; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1427 | return 0; |
| 1428 | } |
| 1429 | |
| 1430 | /** |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1431 | * ata_eh_speed_down_verdict - Determine speed down verdict |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1432 | * @dev: Device of interest |
| 1433 | * |
| 1434 | * This function examines error ring of @dev and determines |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1435 | * whether NCQ needs to be turned off, transfer speed should be |
| 1436 | * stepped down, or falling back to PIO is necessary. |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1437 | * |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1438 | * Cat-1 is ATA_BUS error for any command. |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1439 | * |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1440 | * Cat-2 is TIMEOUT for any command or HSM violation for known |
| 1441 | * supported commands. |
| 1442 | * |
| 1443 | * Cat-3 is is unclassified DEV error for known supported |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1444 | * command. |
| 1445 | * |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1446 | * NCQ needs to be turned off if there have been more than 3 |
| 1447 | * Cat-2 + Cat-3 errors during last 10 minutes. |
| 1448 | * |
| 1449 | * Speed down is necessary if there have been more than 3 Cat-1 + |
| 1450 | * Cat-2 errors or 10 Cat-3 errors during last 10 minutes. |
| 1451 | * |
| 1452 | * Falling back to PIO mode is necessary if there have been more |
| 1453 | * than 10 Cat-1 + Cat-2 + Cat-3 errors during last 5 minutes. |
| 1454 | * |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1455 | * LOCKING: |
| 1456 | * Inherited from caller. |
| 1457 | * |
| 1458 | * RETURNS: |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1459 | * OR of ATA_EH_SPDN_* flags. |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1460 | */ |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1461 | static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev) |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1462 | { |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1463 | const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ; |
| 1464 | u64 j64 = get_jiffies_64(); |
| 1465 | struct speed_down_verdict_arg arg; |
| 1466 | unsigned int verdict = 0; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1467 | |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1468 | /* scan past 10 mins of error history */ |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1469 | memset(&arg, 0, sizeof(arg)); |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1470 | arg.since = j64 - min(j64, j10mins); |
| 1471 | ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg); |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1472 | |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1473 | if (arg.nr_errors[2] + arg.nr_errors[3] > 3) |
| 1474 | verdict |= ATA_EH_SPDN_NCQ_OFF; |
| 1475 | if (arg.nr_errors[1] + arg.nr_errors[2] > 3 || arg.nr_errors[3] > 10) |
| 1476 | verdict |= ATA_EH_SPDN_SPEED_DOWN; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1477 | |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1478 | /* scan past 3 mins of error history */ |
| 1479 | memset(&arg, 0, sizeof(arg)); |
| 1480 | arg.since = j64 - min(j64, j5mins); |
| 1481 | ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg); |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1482 | |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1483 | if (arg.nr_errors[1] + arg.nr_errors[2] + arg.nr_errors[3] > 10) |
| 1484 | verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO; |
| 1485 | |
| 1486 | return verdict; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | /** |
| 1490 | * ata_eh_speed_down - record error and speed down if necessary |
| 1491 | * @dev: Failed device |
| 1492 | * @is_io: Did the device fail during normal IO? |
| 1493 | * @err_mask: err_mask of the error |
| 1494 | * |
| 1495 | * Record error and examine error history to determine whether |
| 1496 | * adjusting transmission speed is necessary. It also sets |
| 1497 | * transmission limits appropriately if such adjustment is |
| 1498 | * necessary. |
| 1499 | * |
| 1500 | * LOCKING: |
| 1501 | * Kernel thread context (may sleep). |
| 1502 | * |
| 1503 | * RETURNS: |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1504 | * Determined recovery action. |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1505 | */ |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1506 | static unsigned int ata_eh_speed_down(struct ata_device *dev, int is_io, |
| 1507 | unsigned int err_mask) |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1508 | { |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1509 | unsigned int verdict; |
| 1510 | unsigned int action = 0; |
| 1511 | |
| 1512 | /* don't bother if Cat-0 error */ |
| 1513 | if (ata_eh_categorize_error(is_io, err_mask) == 0) |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1514 | return 0; |
| 1515 | |
| 1516 | /* record error and determine whether speed down is necessary */ |
| 1517 | ata_ering_record(&dev->ering, is_io, err_mask); |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1518 | verdict = ata_eh_speed_down_verdict(dev); |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1519 | |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1520 | /* turn off NCQ? */ |
| 1521 | if ((verdict & ATA_EH_SPDN_NCQ_OFF) && |
| 1522 | (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ | |
| 1523 | ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) { |
| 1524 | dev->flags |= ATA_DFLAG_NCQ_OFF; |
| 1525 | ata_dev_printk(dev, KERN_WARNING, |
| 1526 | "NCQ disabled due to excessive errors\n"); |
| 1527 | goto done; |
| 1528 | } |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1529 | |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1530 | /* speed down? */ |
| 1531 | if (verdict & ATA_EH_SPDN_SPEED_DOWN) { |
| 1532 | /* speed down SATA link speed if possible */ |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1533 | if (sata_down_spd_limit(dev->link) == 0) { |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1534 | action |= ATA_EH_HARDRESET; |
| 1535 | goto done; |
| 1536 | } |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1537 | |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1538 | /* lower transfer mode */ |
| 1539 | if (dev->spdn_cnt < 2) { |
| 1540 | static const int dma_dnxfer_sel[] = |
| 1541 | { ATA_DNXFER_DMA, ATA_DNXFER_40C }; |
| 1542 | static const int pio_dnxfer_sel[] = |
| 1543 | { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 }; |
| 1544 | int sel; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1545 | |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1546 | if (dev->xfer_shift != ATA_SHIFT_PIO) |
| 1547 | sel = dma_dnxfer_sel[dev->spdn_cnt]; |
| 1548 | else |
| 1549 | sel = pio_dnxfer_sel[dev->spdn_cnt]; |
| 1550 | |
| 1551 | dev->spdn_cnt++; |
| 1552 | |
| 1553 | if (ata_down_xfermask_limit(dev, sel) == 0) { |
| 1554 | action |= ATA_EH_SOFTRESET; |
| 1555 | goto done; |
| 1556 | } |
| 1557 | } |
| 1558 | } |
| 1559 | |
| 1560 | /* Fall back to PIO? Slowing down to PIO is meaningless for |
| 1561 | * SATA. Consider it only for PATA. |
| 1562 | */ |
| 1563 | if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) && |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 1564 | (dev->link->ap->cbl != ATA_CBL_SATA) && |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1565 | (dev->xfer_shift != ATA_SHIFT_PIO)) { |
| 1566 | if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) { |
| 1567 | dev->spdn_cnt = 0; |
| 1568 | action |= ATA_EH_SOFTRESET; |
| 1569 | goto done; |
| 1570 | } |
| 1571 | } |
| 1572 | |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1573 | return 0; |
Tejun Heo | 7d47e8d | 2007-02-02 16:22:31 +0900 | [diff] [blame] | 1574 | done: |
| 1575 | /* device has been slowed down, blow error history */ |
| 1576 | ata_ering_clear(&dev->ering); |
| 1577 | return action; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | /** |
Tejun Heo | 9b1e265 | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 1581 | * ata_eh_link_autopsy - analyze error and determine recovery action |
| 1582 | * @link: host link to perform autopsy on |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1583 | * |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1584 | * Analyze why @link failed and determine which recovery actions |
| 1585 | * are needed. This function also sets more detailed AC_ERR_* |
| 1586 | * values and fills sense data for ATAPI CHECK SENSE. |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1587 | * |
| 1588 | * LOCKING: |
| 1589 | * Kernel thread context (may sleep). |
| 1590 | */ |
Tejun Heo | 9b1e265 | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 1591 | static void ata_eh_link_autopsy(struct ata_link *link) |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1592 | { |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1593 | struct ata_port *ap = link->ap; |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1594 | struct ata_eh_context *ehc = &link->eh_context; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1595 | unsigned int all_err_mask = 0; |
| 1596 | int tag, is_io = 0; |
| 1597 | u32 serror; |
| 1598 | int rc; |
| 1599 | |
| 1600 | DPRINTK("ENTER\n"); |
| 1601 | |
Tejun Heo | 1cdaf53 | 2006-07-03 16:07:26 +0900 | [diff] [blame] | 1602 | if (ehc->i.flags & ATA_EHI_NO_AUTOPSY) |
| 1603 | return; |
| 1604 | |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1605 | /* obtain and analyze SError */ |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1606 | rc = sata_scr_read(link, SCR_ERROR, &serror); |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1607 | if (rc == 0) { |
| 1608 | ehc->i.serror |= serror; |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1609 | ata_eh_analyze_serror(link); |
Tejun Heo | 4e57c51 | 2007-07-16 14:29:41 +0900 | [diff] [blame] | 1610 | } else if (rc != -EOPNOTSUPP) { |
| 1611 | /* SError read failed, force hardreset and probing */ |
| 1612 | ata_ehi_schedule_probe(&ehc->i); |
Tejun Heo | 4528e4d | 2006-07-08 20:17:26 +0900 | [diff] [blame] | 1613 | ehc->i.action |= ATA_EH_HARDRESET; |
Tejun Heo | 4e57c51 | 2007-07-16 14:29:41 +0900 | [diff] [blame] | 1614 | ehc->i.err_mask |= AC_ERR_OTHER; |
| 1615 | } |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1616 | |
Tejun Heo | e8ee845 | 2006-05-15 21:03:46 +0900 | [diff] [blame] | 1617 | /* analyze NCQ failure */ |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1618 | ata_eh_analyze_ncq_error(link); |
Tejun Heo | e8ee845 | 2006-05-15 21:03:46 +0900 | [diff] [blame] | 1619 | |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1620 | /* any real error trumps AC_ERR_OTHER */ |
| 1621 | if (ehc->i.err_mask & ~AC_ERR_OTHER) |
| 1622 | ehc->i.err_mask &= ~AC_ERR_OTHER; |
| 1623 | |
| 1624 | all_err_mask |= ehc->i.err_mask; |
| 1625 | |
| 1626 | for (tag = 0; tag < ATA_MAX_QUEUE; tag++) { |
| 1627 | struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag); |
| 1628 | |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1629 | if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link) |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1630 | continue; |
| 1631 | |
| 1632 | /* inherit upper level err_mask */ |
| 1633 | qc->err_mask |= ehc->i.err_mask; |
| 1634 | |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1635 | /* analyze TF */ |
Tejun Heo | 4528e4d | 2006-07-08 20:17:26 +0900 | [diff] [blame] | 1636 | ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf); |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1637 | |
| 1638 | /* DEV errors are probably spurious in case of ATA_BUS error */ |
| 1639 | if (qc->err_mask & AC_ERR_ATA_BUS) |
| 1640 | qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA | |
| 1641 | AC_ERR_INVALID); |
| 1642 | |
| 1643 | /* any real error trumps unknown error */ |
| 1644 | if (qc->err_mask & ~AC_ERR_OTHER) |
| 1645 | qc->err_mask &= ~AC_ERR_OTHER; |
| 1646 | |
| 1647 | /* SENSE_VALID trumps dev/unknown error and revalidation */ |
| 1648 | if (qc->flags & ATA_QCFLAG_SENSE_VALID) { |
| 1649 | qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER); |
Tejun Heo | 4528e4d | 2006-07-08 20:17:26 +0900 | [diff] [blame] | 1650 | ehc->i.action &= ~ATA_EH_REVALIDATE; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | /* accumulate error info */ |
Tejun Heo | 4528e4d | 2006-07-08 20:17:26 +0900 | [diff] [blame] | 1654 | ehc->i.dev = qc->dev; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1655 | all_err_mask |= qc->err_mask; |
| 1656 | if (qc->flags & ATA_QCFLAG_IO) |
| 1657 | is_io = 1; |
| 1658 | } |
| 1659 | |
Tejun Heo | a20f33f | 2006-05-16 12:58:24 +0900 | [diff] [blame] | 1660 | /* enforce default EH actions */ |
Tejun Heo | b51e9e5 | 2006-06-29 01:29:30 +0900 | [diff] [blame] | 1661 | if (ap->pflags & ATA_PFLAG_FROZEN || |
Tejun Heo | a20f33f | 2006-05-16 12:58:24 +0900 | [diff] [blame] | 1662 | all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT)) |
Tejun Heo | 4528e4d | 2006-07-08 20:17:26 +0900 | [diff] [blame] | 1663 | ehc->i.action |= ATA_EH_SOFTRESET; |
Tejun Heo | a20f33f | 2006-05-16 12:58:24 +0900 | [diff] [blame] | 1664 | else if (all_err_mask) |
Tejun Heo | 4528e4d | 2006-07-08 20:17:26 +0900 | [diff] [blame] | 1665 | ehc->i.action |= ATA_EH_REVALIDATE; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1666 | |
Tejun Heo | 47005f2 | 2006-06-19 18:27:23 +0900 | [diff] [blame] | 1667 | /* if we have offending qcs and the associated failed device */ |
Tejun Heo | 4528e4d | 2006-07-08 20:17:26 +0900 | [diff] [blame] | 1668 | if (ehc->i.dev) { |
Tejun Heo | 47005f2 | 2006-06-19 18:27:23 +0900 | [diff] [blame] | 1669 | /* speed down */ |
Tejun Heo | 4528e4d | 2006-07-08 20:17:26 +0900 | [diff] [blame] | 1670 | ehc->i.action |= ata_eh_speed_down(ehc->i.dev, is_io, |
| 1671 | all_err_mask); |
Tejun Heo | 47005f2 | 2006-06-19 18:27:23 +0900 | [diff] [blame] | 1672 | |
| 1673 | /* perform per-dev EH action only on the offending device */ |
Tejun Heo | 4528e4d | 2006-07-08 20:17:26 +0900 | [diff] [blame] | 1674 | ehc->i.dev_action[ehc->i.dev->devno] |= |
| 1675 | ehc->i.action & ATA_EH_PERDEV_MASK; |
| 1676 | ehc->i.action &= ~ATA_EH_PERDEV_MASK; |
Tejun Heo | 47005f2 | 2006-06-19 18:27:23 +0900 | [diff] [blame] | 1677 | } |
| 1678 | |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1679 | DPRINTK("EXIT\n"); |
| 1680 | } |
| 1681 | |
| 1682 | /** |
Tejun Heo | 9b1e265 | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 1683 | * ata_eh_autopsy - analyze error and determine recovery action |
| 1684 | * @ap: host port to perform autopsy on |
| 1685 | * |
| 1686 | * Analyze all links of @ap and determine why they failed and |
| 1687 | * which recovery actions are needed. |
| 1688 | * |
| 1689 | * LOCKING: |
| 1690 | * Kernel thread context (may sleep). |
| 1691 | */ |
| 1692 | static void ata_eh_autopsy(struct ata_port *ap) |
| 1693 | { |
| 1694 | struct ata_link *link; |
| 1695 | |
| 1696 | __ata_port_for_each_link(link, ap) |
| 1697 | ata_eh_link_autopsy(link); |
| 1698 | } |
| 1699 | |
| 1700 | /** |
| 1701 | * ata_eh_link_report - report error handling to user |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1702 | * @link: ATA link EH is going on |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1703 | * |
| 1704 | * Report EH to user. |
| 1705 | * |
| 1706 | * LOCKING: |
| 1707 | * None. |
| 1708 | */ |
Tejun Heo | 9b1e265 | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 1709 | static void ata_eh_link_report(struct ata_link *link) |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1710 | { |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1711 | struct ata_port *ap = link->ap; |
| 1712 | struct ata_eh_context *ehc = &link->eh_context; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1713 | const char *frozen, *desc; |
| 1714 | int tag, nr_failed = 0; |
| 1715 | |
| 1716 | desc = NULL; |
| 1717 | if (ehc->i.desc[0] != '\0') |
| 1718 | desc = ehc->i.desc; |
| 1719 | |
| 1720 | for (tag = 0; tag < ATA_MAX_QUEUE; tag++) { |
| 1721 | struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag); |
| 1722 | |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1723 | if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link) |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1724 | continue; |
| 1725 | if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask) |
| 1726 | continue; |
| 1727 | |
| 1728 | nr_failed++; |
| 1729 | } |
| 1730 | |
| 1731 | if (!nr_failed && !ehc->i.err_mask) |
| 1732 | return; |
| 1733 | |
| 1734 | frozen = ""; |
Tejun Heo | b51e9e5 | 2006-06-29 01:29:30 +0900 | [diff] [blame] | 1735 | if (ap->pflags & ATA_PFLAG_FROZEN) |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1736 | frozen = " frozen"; |
| 1737 | |
| 1738 | if (ehc->i.dev) { |
Tejun Heo | e8ee845 | 2006-05-15 21:03:46 +0900 | [diff] [blame] | 1739 | ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x " |
| 1740 | "SAct 0x%x SErr 0x%x action 0x%x%s\n", |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1741 | ehc->i.err_mask, link->sactive, |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 1742 | ehc->i.serror, ehc->i.action, frozen); |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1743 | if (desc) |
Tejun Heo | b64bbc3 | 2007-07-16 14:29:39 +0900 | [diff] [blame] | 1744 | ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc); |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1745 | } else { |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1746 | ata_link_printk(link, KERN_ERR, "exception Emask 0x%x " |
Tejun Heo | e8ee845 | 2006-05-15 21:03:46 +0900 | [diff] [blame] | 1747 | "SAct 0x%x SErr 0x%x action 0x%x%s\n", |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1748 | ehc->i.err_mask, link->sactive, |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 1749 | ehc->i.serror, ehc->i.action, frozen); |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1750 | if (desc) |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1751 | ata_link_printk(link, KERN_ERR, "%s\n", desc); |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1752 | } |
| 1753 | |
| 1754 | for (tag = 0; tag < ATA_MAX_QUEUE; tag++) { |
Tejun Heo | 8a93758 | 2006-11-14 22:36:12 +0900 | [diff] [blame] | 1755 | static const char *dma_str[] = { |
| 1756 | [DMA_BIDIRECTIONAL] = "bidi", |
| 1757 | [DMA_TO_DEVICE] = "out", |
| 1758 | [DMA_FROM_DEVICE] = "in", |
| 1759 | [DMA_NONE] = "", |
| 1760 | }; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1761 | struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag); |
Tejun Heo | 8a93758 | 2006-11-14 22:36:12 +0900 | [diff] [blame] | 1762 | struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1763 | |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1764 | if (!(qc->flags & ATA_QCFLAG_FAILED) || |
| 1765 | qc->dev->link != link || !qc->err_mask) |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1766 | continue; |
| 1767 | |
Tejun Heo | 8a93758 | 2006-11-14 22:36:12 +0900 | [diff] [blame] | 1768 | ata_dev_printk(qc->dev, KERN_ERR, |
| 1769 | "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x " |
Tejun Heo | 664e850 | 2006-11-20 16:05:34 +0900 | [diff] [blame] | 1770 | "tag %d cdb 0x%x data %u %s\n " |
Tejun Heo | 8a93758 | 2006-11-14 22:36:12 +0900 | [diff] [blame] | 1771 | "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x " |
Tejun Heo | 5335b72 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 1772 | "Emask 0x%x (%s)%s\n", |
Tejun Heo | 8a93758 | 2006-11-14 22:36:12 +0900 | [diff] [blame] | 1773 | cmd->command, cmd->feature, cmd->nsect, |
| 1774 | cmd->lbal, cmd->lbam, cmd->lbah, |
| 1775 | cmd->hob_feature, cmd->hob_nsect, |
| 1776 | cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah, |
Tejun Heo | 726f078 | 2007-01-03 17:30:39 +0900 | [diff] [blame] | 1777 | cmd->device, qc->tag, qc->cdb[0], qc->nbytes, |
Tejun Heo | 664e850 | 2006-11-20 16:05:34 +0900 | [diff] [blame] | 1778 | dma_str[qc->dma_dir], |
Tejun Heo | 8a93758 | 2006-11-14 22:36:12 +0900 | [diff] [blame] | 1779 | res->command, res->feature, res->nsect, |
| 1780 | res->lbal, res->lbam, res->lbah, |
| 1781 | res->hob_feature, res->hob_nsect, |
| 1782 | res->hob_lbal, res->hob_lbam, res->hob_lbah, |
Tejun Heo | 5335b72 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 1783 | res->device, qc->err_mask, ata_err_string(qc->err_mask), |
| 1784 | qc->err_mask & AC_ERR_NCQ ? " <F>" : ""); |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1785 | } |
| 1786 | } |
| 1787 | |
Tejun Heo | 9b1e265 | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 1788 | /** |
| 1789 | * ata_eh_report - report error handling to user |
| 1790 | * @ap: ATA port to report EH about |
| 1791 | * |
| 1792 | * Report EH to user. |
| 1793 | * |
| 1794 | * LOCKING: |
| 1795 | * None. |
| 1796 | */ |
| 1797 | static void ata_eh_report(struct ata_port *ap) |
| 1798 | { |
| 1799 | struct ata_link *link; |
| 1800 | |
| 1801 | __ata_port_for_each_link(link, ap) |
| 1802 | ata_eh_link_report(link); |
| 1803 | } |
| 1804 | |
Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1805 | static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset, |
Tejun Heo | d4b2bab | 2007-02-02 16:50:52 +0900 | [diff] [blame] | 1806 | unsigned int *classes, unsigned long deadline) |
Tejun Heo | d87fa38 | 2006-05-31 18:28:24 +0900 | [diff] [blame] | 1807 | { |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1808 | struct ata_device *dev; |
| 1809 | int rc; |
Tejun Heo | d87fa38 | 2006-05-31 18:28:24 +0900 | [diff] [blame] | 1810 | |
Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1811 | ata_link_for_each_dev(dev, link) |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1812 | classes[dev->devno] = ATA_DEV_UNKNOWN; |
Tejun Heo | d87fa38 | 2006-05-31 18:28:24 +0900 | [diff] [blame] | 1813 | |
Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1814 | rc = reset(link, classes, deadline); |
Tejun Heo | d87fa38 | 2006-05-31 18:28:24 +0900 | [diff] [blame] | 1815 | if (rc) |
| 1816 | return rc; |
| 1817 | |
| 1818 | /* If any class isn't ATA_DEV_UNKNOWN, consider classification |
| 1819 | * is complete and convert all ATA_DEV_UNKNOWN to |
| 1820 | * ATA_DEV_NONE. |
| 1821 | */ |
Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1822 | ata_link_for_each_dev(dev, link) |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1823 | if (classes[dev->devno] != ATA_DEV_UNKNOWN) |
Tejun Heo | d87fa38 | 2006-05-31 18:28:24 +0900 | [diff] [blame] | 1824 | break; |
| 1825 | |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1826 | if (dev) { |
Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1827 | ata_link_for_each_dev(dev, link) { |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1828 | if (classes[dev->devno] == ATA_DEV_UNKNOWN) |
| 1829 | classes[dev->devno] = ATA_DEV_NONE; |
| 1830 | } |
| 1831 | } |
Tejun Heo | d87fa38 | 2006-05-31 18:28:24 +0900 | [diff] [blame] | 1832 | |
| 1833 | return 0; |
| 1834 | } |
| 1835 | |
Tejun Heo | 664faf0 | 2006-05-31 18:27:50 +0900 | [diff] [blame] | 1836 | static int ata_eh_followup_srst_needed(int rc, int classify, |
| 1837 | const unsigned int *classes) |
| 1838 | { |
| 1839 | if (rc == -EAGAIN) |
| 1840 | return 1; |
| 1841 | if (rc != 0) |
| 1842 | return 0; |
| 1843 | if (classify && classes[0] == ATA_DEV_UNKNOWN) |
| 1844 | return 1; |
| 1845 | return 0; |
| 1846 | } |
| 1847 | |
Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1848 | static int ata_eh_reset(struct ata_link *link, int classify, |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 1849 | ata_prereset_fn_t prereset, ata_reset_fn_t softreset, |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1850 | ata_reset_fn_t hardreset, ata_postreset_fn_t postreset) |
| 1851 | { |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1852 | struct ata_eh_context *ehc = &link->eh_context; |
Tejun Heo | 664faf0 | 2006-05-31 18:27:50 +0900 | [diff] [blame] | 1853 | unsigned int *classes = ehc->classes; |
Tejun Heo | 1cdaf53 | 2006-07-03 16:07:26 +0900 | [diff] [blame] | 1854 | int verbose = !(ehc->i.flags & ATA_EHI_QUIET); |
Tejun Heo | 31daabd | 2007-02-02 16:50:52 +0900 | [diff] [blame] | 1855 | int try = 0; |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1856 | struct ata_device *dev; |
Tejun Heo | 31daabd | 2007-02-02 16:50:52 +0900 | [diff] [blame] | 1857 | unsigned long deadline; |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 1858 | unsigned int action; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1859 | ata_reset_fn_t reset; |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1860 | int rc; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1861 | |
Tejun Heo | 13abf50 | 2006-07-10 23:18:46 +0900 | [diff] [blame] | 1862 | /* about to reset */ |
Tejun Heo | 955e57d | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1863 | ata_eh_about_to_do(link, NULL, ehc->i.action & ATA_EH_RESET_MASK); |
Tejun Heo | 13abf50 | 2006-07-10 23:18:46 +0900 | [diff] [blame] | 1864 | |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 1865 | /* Determine which reset to use and record in ehc->i.action. |
| 1866 | * prereset() may examine and modify it. |
| 1867 | */ |
| 1868 | action = ehc->i.action; |
| 1869 | ehc->i.action &= ~ATA_EH_RESET_MASK; |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1870 | if (softreset && (!hardreset || (!sata_set_spd_needed(link) && |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 1871 | !(action & ATA_EH_HARDRESET)))) |
| 1872 | ehc->i.action |= ATA_EH_SOFTRESET; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1873 | else |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 1874 | ehc->i.action |= ATA_EH_HARDRESET; |
| 1875 | |
| 1876 | if (prereset) { |
Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1877 | rc = prereset(link, jiffies + ATA_EH_PRERESET_TIMEOUT); |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 1878 | if (rc) { |
Alan Cox | c961922 | 2006-09-26 17:53:38 +0100 | [diff] [blame] | 1879 | if (rc == -ENOENT) { |
Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1880 | ata_link_printk(link, KERN_DEBUG, |
Tejun Heo | 4aa9ab6 | 2007-03-12 17:24:08 +0900 | [diff] [blame] | 1881 | "port disabled. ignoring.\n"); |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 1882 | ehc->i.action &= ~ATA_EH_RESET_MASK; |
Tejun Heo | 4aa9ab6 | 2007-03-12 17:24:08 +0900 | [diff] [blame] | 1883 | |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1884 | ata_link_for_each_dev(dev, link) |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1885 | classes[dev->devno] = ATA_DEV_NONE; |
Tejun Heo | 4aa9ab6 | 2007-03-12 17:24:08 +0900 | [diff] [blame] | 1886 | |
| 1887 | rc = 0; |
Alan Cox | c961922 | 2006-09-26 17:53:38 +0100 | [diff] [blame] | 1888 | } else |
Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1889 | ata_link_printk(link, KERN_ERR, |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 1890 | "prereset failed (errno=%d)\n", rc); |
Tejun Heo | fccb6ea | 2007-07-16 14:29:41 +0900 | [diff] [blame] | 1891 | goto out; |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 1892 | } |
| 1893 | } |
| 1894 | |
| 1895 | /* prereset() might have modified ehc->i.action */ |
| 1896 | if (ehc->i.action & ATA_EH_HARDRESET) |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1897 | reset = hardreset; |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 1898 | else if (ehc->i.action & ATA_EH_SOFTRESET) |
| 1899 | reset = softreset; |
| 1900 | else { |
| 1901 | /* prereset told us not to reset, bang classes and return */ |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1902 | ata_link_for_each_dev(dev, link) |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1903 | classes[dev->devno] = ATA_DEV_NONE; |
Tejun Heo | fccb6ea | 2007-07-16 14:29:41 +0900 | [diff] [blame] | 1904 | rc = 0; |
| 1905 | goto out; |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 1906 | } |
| 1907 | |
| 1908 | /* did prereset() screw up? if so, fix up to avoid oopsing */ |
| 1909 | if (!reset) { |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 1910 | if (softreset) |
| 1911 | reset = softreset; |
| 1912 | else |
| 1913 | reset = hardreset; |
| 1914 | } |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1915 | |
| 1916 | retry: |
Tejun Heo | 31daabd | 2007-02-02 16:50:52 +0900 | [diff] [blame] | 1917 | deadline = jiffies + ata_eh_reset_timeouts[try++]; |
| 1918 | |
Tejun Heo | 3e70639 | 2006-05-31 18:28:11 +0900 | [diff] [blame] | 1919 | /* shut up during boot probing */ |
| 1920 | if (verbose) |
Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1921 | ata_link_printk(link, KERN_INFO, "%s resetting link\n", |
Tejun Heo | 3e70639 | 2006-05-31 18:28:11 +0900 | [diff] [blame] | 1922 | reset == softreset ? "soft" : "hard"); |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1923 | |
Tejun Heo | 13abf50 | 2006-07-10 23:18:46 +0900 | [diff] [blame] | 1924 | /* mark that this EH session started with reset */ |
Tejun Heo | 0d64a23 | 2007-04-23 02:41:05 +0900 | [diff] [blame] | 1925 | if (reset == hardreset) |
| 1926 | ehc->i.flags |= ATA_EHI_DID_HARDRESET; |
| 1927 | else |
| 1928 | ehc->i.flags |= ATA_EHI_DID_SOFTRESET; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1929 | |
Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1930 | rc = ata_do_reset(link, reset, classes, deadline); |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1931 | |
Tejun Heo | 664faf0 | 2006-05-31 18:27:50 +0900 | [diff] [blame] | 1932 | if (reset == hardreset && |
| 1933 | ata_eh_followup_srst_needed(rc, classify, classes)) { |
| 1934 | /* okay, let's do follow-up softreset */ |
Tejun Heo | 664faf0 | 2006-05-31 18:27:50 +0900 | [diff] [blame] | 1935 | reset = softreset; |
| 1936 | |
| 1937 | if (!reset) { |
Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1938 | ata_link_printk(link, KERN_ERR, |
Tejun Heo | 664faf0 | 2006-05-31 18:27:50 +0900 | [diff] [blame] | 1939 | "follow-up softreset required " |
| 1940 | "but no softreset avaliable\n"); |
Tejun Heo | fccb6ea | 2007-07-16 14:29:41 +0900 | [diff] [blame] | 1941 | rc = -EINVAL; |
| 1942 | goto out; |
Tejun Heo | 664faf0 | 2006-05-31 18:27:50 +0900 | [diff] [blame] | 1943 | } |
| 1944 | |
Tejun Heo | 955e57d | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1945 | ata_eh_about_to_do(link, NULL, ATA_EH_RESET_MASK); |
Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1946 | rc = ata_do_reset(link, reset, classes, deadline); |
Tejun Heo | 664faf0 | 2006-05-31 18:27:50 +0900 | [diff] [blame] | 1947 | |
| 1948 | if (rc == 0 && classify && |
| 1949 | classes[0] == ATA_DEV_UNKNOWN) { |
Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1950 | ata_link_printk(link, KERN_ERR, |
Tejun Heo | 664faf0 | 2006-05-31 18:27:50 +0900 | [diff] [blame] | 1951 | "classification failed\n"); |
Tejun Heo | fccb6ea | 2007-07-16 14:29:41 +0900 | [diff] [blame] | 1952 | rc = -EINVAL; |
| 1953 | goto out; |
Tejun Heo | 664faf0 | 2006-05-31 18:27:50 +0900 | [diff] [blame] | 1954 | } |
| 1955 | } |
| 1956 | |
Tejun Heo | 31daabd | 2007-02-02 16:50:52 +0900 | [diff] [blame] | 1957 | if (rc && try < ARRAY_SIZE(ata_eh_reset_timeouts)) { |
| 1958 | unsigned long now = jiffies; |
Tejun Heo | 664faf0 | 2006-05-31 18:27:50 +0900 | [diff] [blame] | 1959 | |
Tejun Heo | 31daabd | 2007-02-02 16:50:52 +0900 | [diff] [blame] | 1960 | if (time_before(now, deadline)) { |
| 1961 | unsigned long delta = deadline - jiffies; |
Tejun Heo | 664faf0 | 2006-05-31 18:27:50 +0900 | [diff] [blame] | 1962 | |
Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1963 | ata_link_printk(link, KERN_WARNING, "reset failed " |
Tejun Heo | 31daabd | 2007-02-02 16:50:52 +0900 | [diff] [blame] | 1964 | "(errno=%d), retrying in %u secs\n", |
| 1965 | rc, (jiffies_to_msecs(delta) + 999) / 1000); |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1966 | |
Tejun Heo | 31daabd | 2007-02-02 16:50:52 +0900 | [diff] [blame] | 1967 | schedule_timeout_uninterruptible(delta); |
| 1968 | } |
| 1969 | |
Tejun Heo | f154515 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 1970 | if (rc == -EPIPE || |
Tejun Heo | 31daabd | 2007-02-02 16:50:52 +0900 | [diff] [blame] | 1971 | try == ARRAY_SIZE(ata_eh_reset_timeouts) - 1) |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1972 | sata_down_spd_limit(link); |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1973 | if (hardreset) |
| 1974 | reset = hardreset; |
| 1975 | goto retry; |
| 1976 | } |
| 1977 | |
| 1978 | if (rc == 0) { |
Tejun Heo | 008a789 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 1979 | u32 sstatus; |
| 1980 | |
Tejun Heo | 20952b6 | 2006-05-31 18:27:23 +0900 | [diff] [blame] | 1981 | /* After the reset, the device state is PIO 0 and the |
| 1982 | * controller state is undefined. Record the mode. |
| 1983 | */ |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1984 | ata_link_for_each_dev(dev, link) |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1985 | dev->pio_mode = XFER_PIO_0; |
Tejun Heo | 20952b6 | 2006-05-31 18:27:23 +0900 | [diff] [blame] | 1986 | |
Tejun Heo | 008a789 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 1987 | /* record current link speed */ |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1988 | if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0) |
| 1989 | link->sata_spd = (sstatus >> 4) & 0xf; |
Tejun Heo | 008a789 | 2007-07-16 14:29:40 +0900 | [diff] [blame] | 1990 | |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1991 | if (postreset) |
Tejun Heo | cc0680a | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1992 | postreset(link, classes); |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1993 | |
| 1994 | /* reset successful, schedule revalidation */ |
Tejun Heo | 955e57d | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 1995 | ata_eh_done(link, NULL, ehc->i.action & ATA_EH_RESET_MASK); |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 1996 | ehc->i.action |= ATA_EH_REVALIDATE; |
| 1997 | } |
Tejun Heo | fccb6ea | 2007-07-16 14:29:41 +0900 | [diff] [blame] | 1998 | out: |
| 1999 | /* clear hotplug flag */ |
| 2000 | ehc->i.flags &= ~ATA_EHI_HOTPLUGGED; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2001 | return rc; |
| 2002 | } |
| 2003 | |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2004 | static int ata_eh_revalidate_and_attach(struct ata_link *link, |
Tejun Heo | 084fe63 | 2006-05-31 18:28:03 +0900 | [diff] [blame] | 2005 | struct ata_device **r_failed_dev) |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2006 | { |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2007 | struct ata_port *ap = link->ap; |
| 2008 | struct ata_eh_context *ehc = &link->eh_context; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2009 | struct ata_device *dev; |
Tejun Heo | 8c3c52a | 2007-03-22 22:24:19 +0900 | [diff] [blame] | 2010 | unsigned int new_mask = 0; |
Tejun Heo | 084fe63 | 2006-05-31 18:28:03 +0900 | [diff] [blame] | 2011 | unsigned long flags; |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2012 | int rc = 0; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2013 | |
| 2014 | DPRINTK("ENTER\n"); |
| 2015 | |
Tejun Heo | 8c3c52a | 2007-03-22 22:24:19 +0900 | [diff] [blame] | 2016 | /* For PATA drive side cable detection to work, IDENTIFY must |
| 2017 | * be done backwards such that PDIAG- is released by the slave |
| 2018 | * device before the master device is identified. |
| 2019 | */ |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2020 | ata_link_for_each_dev_reverse(dev, link) { |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2021 | unsigned int action = ata_eh_dev_action(dev); |
| 2022 | unsigned int readid_flags = 0; |
Tejun Heo | 47005f2 | 2006-06-19 18:27:23 +0900 | [diff] [blame] | 2023 | |
Tejun Heo | bff0464 | 2006-11-10 18:08:10 +0900 | [diff] [blame] | 2024 | if (ehc->i.flags & ATA_EHI_DID_RESET) |
| 2025 | readid_flags |= ATA_READID_POSTRESET; |
| 2026 | |
Tejun Heo | 9666f40 | 2007-05-04 21:27:47 +0200 | [diff] [blame] | 2027 | if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) { |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2028 | if (ata_link_offline(link)) { |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2029 | rc = -EIO; |
Tejun Heo | 8c3c52a | 2007-03-22 22:24:19 +0900 | [diff] [blame] | 2030 | goto err; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2031 | } |
| 2032 | |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2033 | ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE); |
Tejun Heo | bff0464 | 2006-11-10 18:08:10 +0900 | [diff] [blame] | 2034 | rc = ata_dev_revalidate(dev, readid_flags); |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2035 | if (rc) |
Tejun Heo | 8c3c52a | 2007-03-22 22:24:19 +0900 | [diff] [blame] | 2036 | goto err; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2037 | |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2038 | ata_eh_done(link, dev, ATA_EH_REVALIDATE); |
Tejun Heo | 47005f2 | 2006-06-19 18:27:23 +0900 | [diff] [blame] | 2039 | |
Tejun Heo | baa1e78 | 2006-11-01 18:39:27 +0900 | [diff] [blame] | 2040 | /* Configuration may have changed, reconfigure |
| 2041 | * transfer mode. |
| 2042 | */ |
| 2043 | ehc->i.flags |= ATA_EHI_SETMODE; |
| 2044 | |
zhao, forrest | 3057ac3 | 2006-06-12 12:01:34 +0800 | [diff] [blame] | 2045 | /* schedule the scsi_rescan_device() here */ |
| 2046 | queue_work(ata_aux_wq, &(ap->scsi_rescan_task)); |
Tejun Heo | 084fe63 | 2006-05-31 18:28:03 +0900 | [diff] [blame] | 2047 | } else if (dev->class == ATA_DEV_UNKNOWN && |
| 2048 | ehc->tries[dev->devno] && |
| 2049 | ata_class_enabled(ehc->classes[dev->devno])) { |
| 2050 | dev->class = ehc->classes[dev->devno]; |
| 2051 | |
Tejun Heo | bff0464 | 2006-11-10 18:08:10 +0900 | [diff] [blame] | 2052 | rc = ata_dev_read_id(dev, &dev->class, readid_flags, |
| 2053 | dev->id); |
Tejun Heo | 8c3c52a | 2007-03-22 22:24:19 +0900 | [diff] [blame] | 2054 | switch (rc) { |
| 2055 | case 0: |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2056 | new_mask |= 1 << dev->devno; |
Tejun Heo | 8c3c52a | 2007-03-22 22:24:19 +0900 | [diff] [blame] | 2057 | break; |
| 2058 | case -ENOENT: |
Tejun Heo | 55a8e2c | 2006-11-10 18:08:10 +0900 | [diff] [blame] | 2059 | /* IDENTIFY was issued to non-existent |
| 2060 | * device. No need to reset. Just |
| 2061 | * thaw and kill the device. |
| 2062 | */ |
| 2063 | ata_eh_thaw_port(ap); |
| 2064 | dev->class = ATA_DEV_UNKNOWN; |
Tejun Heo | 084fe63 | 2006-05-31 18:28:03 +0900 | [diff] [blame] | 2065 | break; |
Tejun Heo | 8c3c52a | 2007-03-22 22:24:19 +0900 | [diff] [blame] | 2066 | default: |
| 2067 | dev->class = ATA_DEV_UNKNOWN; |
| 2068 | goto err; |
Tejun Heo | 55a8e2c | 2006-11-10 18:08:10 +0900 | [diff] [blame] | 2069 | } |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2070 | } |
| 2071 | } |
| 2072 | |
Tejun Heo | c1c4e8d | 2007-04-23 02:05:53 +0900 | [diff] [blame] | 2073 | /* PDIAG- should have been released, ask cable type if post-reset */ |
Tejun Heo | 9b1e265 | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 2074 | if (ata_is_host_link(link) && ap->ops->cable_detect && |
| 2075 | (ehc->i.flags & ATA_EHI_DID_RESET)) |
Tejun Heo | c1c4e8d | 2007-04-23 02:05:53 +0900 | [diff] [blame] | 2076 | ap->cbl = ap->ops->cable_detect(ap); |
| 2077 | |
Tejun Heo | 8c3c52a | 2007-03-22 22:24:19 +0900 | [diff] [blame] | 2078 | /* Configure new devices forward such that user doesn't see |
| 2079 | * device detection messages backwards. |
| 2080 | */ |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2081 | ata_link_for_each_dev(dev, link) { |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2082 | if (!(new_mask & (1 << dev->devno))) |
Tejun Heo | 8c3c52a | 2007-03-22 22:24:19 +0900 | [diff] [blame] | 2083 | continue; |
| 2084 | |
| 2085 | ehc->i.flags |= ATA_EHI_PRINTINFO; |
| 2086 | rc = ata_dev_configure(dev); |
| 2087 | ehc->i.flags &= ~ATA_EHI_PRINTINFO; |
| 2088 | if (rc) |
| 2089 | goto err; |
| 2090 | |
| 2091 | spin_lock_irqsave(ap->lock, flags); |
| 2092 | ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG; |
| 2093 | spin_unlock_irqrestore(ap->lock, flags); |
| 2094 | |
| 2095 | /* new device discovered, configure xfermode */ |
| 2096 | ehc->i.flags |= ATA_EHI_SETMODE; |
| 2097 | } |
| 2098 | |
| 2099 | return 0; |
| 2100 | |
| 2101 | err: |
| 2102 | *r_failed_dev = dev; |
| 2103 | DPRINTK("EXIT rc=%d\n", rc); |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2104 | return rc; |
| 2105 | } |
| 2106 | |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2107 | static int ata_link_nr_enabled(struct ata_link *link) |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2108 | { |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2109 | struct ata_device *dev; |
| 2110 | int cnt = 0; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2111 | |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2112 | ata_link_for_each_dev(dev, link) |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2113 | if (ata_dev_enabled(dev)) |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2114 | cnt++; |
| 2115 | return cnt; |
| 2116 | } |
| 2117 | |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2118 | static int ata_link_nr_vacant(struct ata_link *link) |
Tejun Heo | 084fe63 | 2006-05-31 18:28:03 +0900 | [diff] [blame] | 2119 | { |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2120 | struct ata_device *dev; |
| 2121 | int cnt = 0; |
Tejun Heo | 084fe63 | 2006-05-31 18:28:03 +0900 | [diff] [blame] | 2122 | |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2123 | ata_link_for_each_dev(dev, link) |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2124 | if (dev->class == ATA_DEV_UNKNOWN) |
Tejun Heo | 084fe63 | 2006-05-31 18:28:03 +0900 | [diff] [blame] | 2125 | cnt++; |
| 2126 | return cnt; |
| 2127 | } |
| 2128 | |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2129 | static int ata_eh_skip_recovery(struct ata_link *link) |
Tejun Heo | 084fe63 | 2006-05-31 18:28:03 +0900 | [diff] [blame] | 2130 | { |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2131 | struct ata_eh_context *ehc = &link->eh_context; |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2132 | struct ata_device *dev; |
Tejun Heo | 084fe63 | 2006-05-31 18:28:03 +0900 | [diff] [blame] | 2133 | |
Tejun Heo | 7c8c2cf | 2006-07-10 23:18:23 +0900 | [diff] [blame] | 2134 | /* thaw frozen port, resume link and recover failed devices */ |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2135 | if ((link->ap->pflags & ATA_PFLAG_FROZEN) || |
| 2136 | (ehc->i.flags & ATA_EHI_RESUME_LINK) || ata_link_nr_enabled(link)) |
Tejun Heo | 084fe63 | 2006-05-31 18:28:03 +0900 | [diff] [blame] | 2137 | return 0; |
| 2138 | |
| 2139 | /* skip if class codes for all vacant slots are ATA_DEV_NONE */ |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2140 | ata_link_for_each_dev(dev, link) { |
Tejun Heo | 084fe63 | 2006-05-31 18:28:03 +0900 | [diff] [blame] | 2141 | if (dev->class == ATA_DEV_UNKNOWN && |
| 2142 | ehc->classes[dev->devno] != ATA_DEV_NONE) |
| 2143 | return 0; |
| 2144 | } |
| 2145 | |
| 2146 | return 1; |
| 2147 | } |
| 2148 | |
Tejun Heo | 9b1e265 | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 2149 | static int ata_eh_handle_dev_fail(struct ata_device *dev, int err) |
Tejun Heo | fee7ca7 | 2007-07-01 19:05:58 +0900 | [diff] [blame] | 2150 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 2151 | struct ata_eh_context *ehc = &dev->link->eh_context; |
Tejun Heo | fee7ca7 | 2007-07-01 19:05:58 +0900 | [diff] [blame] | 2152 | |
| 2153 | ehc->tries[dev->devno]--; |
| 2154 | |
| 2155 | switch (err) { |
| 2156 | case -ENODEV: |
| 2157 | /* device missing or wrong IDENTIFY data, schedule probing */ |
| 2158 | ehc->i.probe_mask |= (1 << dev->devno); |
| 2159 | case -EINVAL: |
| 2160 | /* give it just one more chance */ |
| 2161 | ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1); |
| 2162 | case -EIO: |
| 2163 | if (ehc->tries[dev->devno] == 1) { |
| 2164 | /* This is the last chance, better to slow |
| 2165 | * down than lose it. |
| 2166 | */ |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2167 | sata_down_spd_limit(dev->link); |
Tejun Heo | fee7ca7 | 2007-07-01 19:05:58 +0900 | [diff] [blame] | 2168 | ata_down_xfermask_limit(dev, ATA_DNXFER_PIO); |
| 2169 | } |
| 2170 | } |
| 2171 | |
| 2172 | if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) { |
| 2173 | /* disable device if it has used up all its chances */ |
| 2174 | ata_dev_disable(dev); |
| 2175 | |
| 2176 | /* detach if offline */ |
Tejun Heo | 936fd73 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 2177 | if (ata_link_offline(dev->link)) |
Tejun Heo | fee7ca7 | 2007-07-01 19:05:58 +0900 | [diff] [blame] | 2178 | ata_eh_detach_dev(dev); |
| 2179 | |
| 2180 | /* probe if requested */ |
| 2181 | if ((ehc->i.probe_mask & (1 << dev->devno)) && |
| 2182 | !(ehc->did_probe_mask & (1 << dev->devno))) { |
| 2183 | ata_eh_detach_dev(dev); |
| 2184 | ata_dev_init(dev); |
| 2185 | |
| 2186 | ehc->tries[dev->devno] = ATA_EH_DEV_TRIES; |
| 2187 | ehc->did_probe_mask |= (1 << dev->devno); |
| 2188 | ehc->i.action |= ATA_EH_SOFTRESET; |
| 2189 | } |
Tejun Heo | 9b1e265 | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 2190 | |
| 2191 | return 1; |
Tejun Heo | fee7ca7 | 2007-07-01 19:05:58 +0900 | [diff] [blame] | 2192 | } else { |
| 2193 | /* soft didn't work? be haaaaard */ |
| 2194 | if (ehc->i.flags & ATA_EHI_DID_RESET) |
| 2195 | ehc->i.action |= ATA_EH_HARDRESET; |
| 2196 | else |
| 2197 | ehc->i.action |= ATA_EH_SOFTRESET; |
Tejun Heo | 9b1e265 | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 2198 | |
| 2199 | return 0; |
Tejun Heo | fee7ca7 | 2007-07-01 19:05:58 +0900 | [diff] [blame] | 2200 | } |
| 2201 | } |
| 2202 | |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2203 | /** |
| 2204 | * ata_eh_recover - recover host port after error |
| 2205 | * @ap: host port to recover |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 2206 | * @prereset: prereset method (can be NULL) |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2207 | * @softreset: softreset method (can be NULL) |
| 2208 | * @hardreset: hardreset method (can be NULL) |
| 2209 | * @postreset: postreset method (can be NULL) |
Tejun Heo | 9b1e265 | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 2210 | * @r_failed_link: out parameter for failed link |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2211 | * |
| 2212 | * This is the alpha and omega, eum and yang, heart and soul of |
| 2213 | * libata exception handling. On entry, actions required to |
Tejun Heo | 9b1e265 | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 2214 | * recover each link and hotplug requests are recorded in the |
| 2215 | * link's eh_context. This function executes all the operations |
| 2216 | * with appropriate retrials and fallbacks to resurrect failed |
Tejun Heo | 084fe63 | 2006-05-31 18:28:03 +0900 | [diff] [blame] | 2217 | * devices, detach goners and greet newcomers. |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2218 | * |
| 2219 | * LOCKING: |
| 2220 | * Kernel thread context (may sleep). |
| 2221 | * |
| 2222 | * RETURNS: |
| 2223 | * 0 on success, -errno on failure. |
| 2224 | */ |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 2225 | static int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset, |
| 2226 | ata_reset_fn_t softreset, ata_reset_fn_t hardreset, |
Tejun Heo | 9b1e265 | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 2227 | ata_postreset_fn_t postreset, |
| 2228 | struct ata_link **r_failed_link) |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2229 | { |
Tejun Heo | 9b1e265 | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 2230 | struct ata_link *link; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2231 | struct ata_device *dev; |
Tejun Heo | 9b1e265 | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 2232 | int nr_failed_devs, nr_disabled_devs; |
| 2233 | int reset, rc; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2234 | |
| 2235 | DPRINTK("ENTER\n"); |
| 2236 | |
| 2237 | /* prep for recovery */ |
Tejun Heo | 9b1e265 | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 2238 | ata_port_for_each_link(link, ap) { |
| 2239 | struct ata_eh_context *ehc = &link->eh_context; |
Tejun Heo | 084fe63 | 2006-05-31 18:28:03 +0900 | [diff] [blame] | 2240 | |
Tejun Heo | 9b1e265 | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 2241 | ata_link_for_each_dev(dev, link) { |
| 2242 | ehc->tries[dev->devno] = ATA_EH_DEV_TRIES; |
Tejun Heo | 79a55b7 | 2007-01-18 17:22:18 +0900 | [diff] [blame] | 2243 | |
Tejun Heo | 9b1e265 | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 2244 | /* collect port action mask recorded in dev actions */ |
| 2245 | ehc->i.action |= ehc->i.dev_action[dev->devno] & |
| 2246 | ~ATA_EH_PERDEV_MASK; |
| 2247 | ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK; |
Tejun Heo | 084fe63 | 2006-05-31 18:28:03 +0900 | [diff] [blame] | 2248 | |
Tejun Heo | 9b1e265 | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 2249 | /* process hotplug request */ |
| 2250 | if (dev->flags & ATA_DFLAG_DETACH) |
| 2251 | ata_eh_detach_dev(dev); |
| 2252 | |
| 2253 | if (!ata_dev_enabled(dev) && |
| 2254 | ((ehc->i.probe_mask & (1 << dev->devno)) && |
| 2255 | !(ehc->did_probe_mask & (1 << dev->devno)))) { |
| 2256 | ata_eh_detach_dev(dev); |
| 2257 | ata_dev_init(dev); |
| 2258 | ehc->did_probe_mask |= (1 << dev->devno); |
| 2259 | ehc->i.action |= ATA_EH_SOFTRESET; |
| 2260 | } |
Tejun Heo | 084fe63 | 2006-05-31 18:28:03 +0900 | [diff] [blame] | 2261 | } |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2262 | } |
| 2263 | |
| 2264 | retry: |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2265 | rc = 0; |
Tejun Heo | 9b1e265 | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 2266 | nr_failed_devs = 0; |
| 2267 | nr_disabled_devs = 0; |
| 2268 | reset = 0; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2269 | |
Tejun Heo | aeb2ecd | 2006-06-12 14:11:43 +0900 | [diff] [blame] | 2270 | /* if UNLOADING, finish immediately */ |
Tejun Heo | b51e9e5 | 2006-06-29 01:29:30 +0900 | [diff] [blame] | 2271 | if (ap->pflags & ATA_PFLAG_UNLOADING) |
Tejun Heo | aeb2ecd | 2006-06-12 14:11:43 +0900 | [diff] [blame] | 2272 | goto out; |
| 2273 | |
Tejun Heo | 9b1e265 | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 2274 | /* prep for EH */ |
| 2275 | ata_port_for_each_link(link, ap) { |
| 2276 | struct ata_eh_context *ehc = &link->eh_context; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2277 | |
Tejun Heo | 9b1e265 | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 2278 | /* skip EH if possible. */ |
| 2279 | if (ata_eh_skip_recovery(link)) |
| 2280 | ehc->i.action = 0; |
| 2281 | |
| 2282 | /* do we need to reset? */ |
| 2283 | if (ehc->i.action & ATA_EH_RESET_MASK) |
| 2284 | reset = 1; |
| 2285 | |
| 2286 | ata_link_for_each_dev(dev, link) |
| 2287 | ehc->classes[dev->devno] = ATA_DEV_UNKNOWN; |
| 2288 | } |
Tejun Heo | 084fe63 | 2006-05-31 18:28:03 +0900 | [diff] [blame] | 2289 | |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2290 | /* reset */ |
Tejun Heo | 9b1e265 | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 2291 | if (reset) { |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2292 | ata_eh_freeze_port(ap); |
| 2293 | |
Tejun Heo | 9b1e265 | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 2294 | ata_port_for_each_link(link, ap) { |
| 2295 | struct ata_eh_context *ehc = &link->eh_context; |
| 2296 | |
| 2297 | if (!(ehc->i.action & ATA_EH_RESET_MASK)) |
| 2298 | continue; |
| 2299 | |
| 2300 | rc = ata_eh_reset(link, ata_link_nr_vacant(link), |
| 2301 | prereset, softreset, hardreset, |
| 2302 | postreset); |
| 2303 | if (rc) { |
| 2304 | ata_link_printk(link, KERN_ERR, |
| 2305 | "reset failed, giving up\n"); |
| 2306 | goto out; |
| 2307 | } |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2308 | } |
| 2309 | |
| 2310 | ata_eh_thaw_port(ap); |
| 2311 | } |
| 2312 | |
Tejun Heo | 9b1e265 | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 2313 | /* the rest */ |
| 2314 | ata_port_for_each_link(link, ap) { |
| 2315 | struct ata_eh_context *ehc = &link->eh_context; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2316 | |
Tejun Heo | 9b1e265 | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 2317 | /* revalidate existing devices and attach new ones */ |
| 2318 | rc = ata_eh_revalidate_and_attach(link, &dev); |
Tejun Heo | 4ae72a1 | 2007-02-02 16:22:30 +0900 | [diff] [blame] | 2319 | if (rc) |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2320 | goto dev_fail; |
Tejun Heo | 9b1e265 | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 2321 | |
| 2322 | /* configure transfer mode if necessary */ |
| 2323 | if (ehc->i.flags & ATA_EHI_SETMODE) { |
| 2324 | rc = ata_set_mode(link, &dev); |
| 2325 | if (rc) |
| 2326 | goto dev_fail; |
| 2327 | ehc->i.flags &= ~ATA_EHI_SETMODE; |
| 2328 | } |
| 2329 | |
| 2330 | /* this link is okay now */ |
| 2331 | ehc->i.flags = 0; |
| 2332 | continue; |
| 2333 | |
| 2334 | dev_fail: |
| 2335 | nr_failed_devs++; |
| 2336 | if (ata_eh_handle_dev_fail(dev, rc)) |
| 2337 | nr_disabled_devs++; |
| 2338 | |
| 2339 | if (ap->pflags & ATA_PFLAG_FROZEN) |
| 2340 | break; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2341 | } |
| 2342 | |
Tejun Heo | 9b1e265 | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 2343 | if (nr_failed_devs) { |
| 2344 | if (nr_failed_devs != nr_disabled_devs) { |
| 2345 | ata_port_printk(ap, KERN_WARNING, "failed to recover " |
| 2346 | "some devices, retrying in 5 secs\n"); |
| 2347 | ssleep(5); |
| 2348 | } else { |
| 2349 | /* no device left to recover, repeat fast */ |
| 2350 | msleep(500); |
| 2351 | } |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2352 | |
Tejun Heo | 9b1e265 | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 2353 | goto retry; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2354 | } |
| 2355 | |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2356 | out: |
Tejun Heo | 9b1e265 | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 2357 | if (rc && r_failed_link) |
| 2358 | *r_failed_link = link; |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2359 | |
| 2360 | DPRINTK("EXIT, rc=%d\n", rc); |
| 2361 | return rc; |
| 2362 | } |
| 2363 | |
| 2364 | /** |
| 2365 | * ata_eh_finish - finish up EH |
| 2366 | * @ap: host port to finish EH for |
| 2367 | * |
| 2368 | * Recovery is complete. Clean up EH states and retry or finish |
| 2369 | * failed qcs. |
| 2370 | * |
| 2371 | * LOCKING: |
| 2372 | * None. |
| 2373 | */ |
| 2374 | static void ata_eh_finish(struct ata_port *ap) |
| 2375 | { |
| 2376 | int tag; |
| 2377 | |
| 2378 | /* retry or finish qcs */ |
| 2379 | for (tag = 0; tag < ATA_MAX_QUEUE; tag++) { |
| 2380 | struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag); |
| 2381 | |
| 2382 | if (!(qc->flags & ATA_QCFLAG_FAILED)) |
| 2383 | continue; |
| 2384 | |
| 2385 | if (qc->err_mask) { |
| 2386 | /* FIXME: Once EH migration is complete, |
| 2387 | * generate sense data in this function, |
| 2388 | * considering both err_mask and tf. |
| 2389 | */ |
| 2390 | if (qc->err_mask & AC_ERR_INVALID) |
| 2391 | ata_eh_qc_complete(qc); |
| 2392 | else |
| 2393 | ata_eh_qc_retry(qc); |
| 2394 | } else { |
| 2395 | if (qc->flags & ATA_QCFLAG_SENSE_VALID) { |
| 2396 | ata_eh_qc_complete(qc); |
| 2397 | } else { |
| 2398 | /* feed zero TF to sense generation */ |
| 2399 | memset(&qc->result_tf, 0, sizeof(qc->result_tf)); |
| 2400 | ata_eh_qc_retry(qc); |
| 2401 | } |
| 2402 | } |
| 2403 | } |
| 2404 | } |
| 2405 | |
| 2406 | /** |
| 2407 | * ata_do_eh - do standard error handling |
| 2408 | * @ap: host port to handle error for |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 2409 | * @prereset: prereset method (can be NULL) |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2410 | * @softreset: softreset method (can be NULL) |
| 2411 | * @hardreset: hardreset method (can be NULL) |
| 2412 | * @postreset: postreset method (can be NULL) |
| 2413 | * |
| 2414 | * Perform standard error handling sequence. |
| 2415 | * |
| 2416 | * LOCKING: |
| 2417 | * Kernel thread context (may sleep). |
| 2418 | */ |
Tejun Heo | f5914a4 | 2006-05-31 18:27:48 +0900 | [diff] [blame] | 2419 | void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset, |
| 2420 | ata_reset_fn_t softreset, ata_reset_fn_t hardreset, |
| 2421 | ata_postreset_fn_t postreset) |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2422 | { |
Tejun Heo | 9b1e265 | 2007-08-06 18:36:24 +0900 | [diff] [blame] | 2423 | struct ata_device *dev; |
| 2424 | int rc; |
| 2425 | |
| 2426 | ata_eh_autopsy(ap); |
| 2427 | ata_eh_report(ap); |
| 2428 | |
| 2429 | rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset, |
| 2430 | NULL); |
| 2431 | if (rc) { |
| 2432 | ata_link_for_each_dev(dev, &ap->link) |
| 2433 | ata_dev_disable(dev); |
| 2434 | } |
| 2435 | |
Tejun Heo | 022bdb0 | 2006-05-15 20:58:22 +0900 | [diff] [blame] | 2436 | ata_eh_finish(ap); |
| 2437 | } |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 2438 | |
Tejun Heo | 6ffa01d | 2007-03-02 17:32:47 +0900 | [diff] [blame] | 2439 | #ifdef CONFIG_PM |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 2440 | /** |
| 2441 | * ata_eh_handle_port_suspend - perform port suspend operation |
| 2442 | * @ap: port to suspend |
| 2443 | * |
| 2444 | * Suspend @ap. |
| 2445 | * |
| 2446 | * LOCKING: |
| 2447 | * Kernel thread context (may sleep). |
| 2448 | */ |
| 2449 | static void ata_eh_handle_port_suspend(struct ata_port *ap) |
| 2450 | { |
| 2451 | unsigned long flags; |
| 2452 | int rc = 0; |
| 2453 | |
| 2454 | /* are we suspending? */ |
| 2455 | spin_lock_irqsave(ap->lock, flags); |
| 2456 | if (!(ap->pflags & ATA_PFLAG_PM_PENDING) || |
| 2457 | ap->pm_mesg.event == PM_EVENT_ON) { |
| 2458 | spin_unlock_irqrestore(ap->lock, flags); |
| 2459 | return; |
| 2460 | } |
| 2461 | spin_unlock_irqrestore(ap->lock, flags); |
| 2462 | |
| 2463 | WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED); |
| 2464 | |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 2465 | /* tell ACPI we're suspending */ |
| 2466 | rc = ata_acpi_on_suspend(ap); |
| 2467 | if (rc) |
| 2468 | goto out; |
| 2469 | |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 2470 | /* suspend */ |
| 2471 | ata_eh_freeze_port(ap); |
| 2472 | |
| 2473 | if (ap->ops->port_suspend) |
| 2474 | rc = ap->ops->port_suspend(ap, ap->pm_mesg); |
| 2475 | |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 2476 | out: |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 2477 | /* report result */ |
| 2478 | spin_lock_irqsave(ap->lock, flags); |
| 2479 | |
| 2480 | ap->pflags &= ~ATA_PFLAG_PM_PENDING; |
| 2481 | if (rc == 0) |
| 2482 | ap->pflags |= ATA_PFLAG_SUSPENDED; |
Tejun Heo | 64578a3 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 2483 | else if (ap->pflags & ATA_PFLAG_FROZEN) |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 2484 | ata_port_schedule_eh(ap); |
| 2485 | |
| 2486 | if (ap->pm_result) { |
| 2487 | *ap->pm_result = rc; |
| 2488 | ap->pm_result = NULL; |
| 2489 | } |
| 2490 | |
| 2491 | spin_unlock_irqrestore(ap->lock, flags); |
| 2492 | |
| 2493 | return; |
| 2494 | } |
| 2495 | |
| 2496 | /** |
| 2497 | * ata_eh_handle_port_resume - perform port resume operation |
| 2498 | * @ap: port to resume |
| 2499 | * |
| 2500 | * Resume @ap. |
| 2501 | * |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 2502 | * LOCKING: |
| 2503 | * Kernel thread context (may sleep). |
| 2504 | */ |
| 2505 | static void ata_eh_handle_port_resume(struct ata_port *ap) |
| 2506 | { |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 2507 | unsigned long flags; |
Tejun Heo | 9666f40 | 2007-05-04 21:27:47 +0200 | [diff] [blame] | 2508 | int rc = 0; |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 2509 | |
| 2510 | /* are we resuming? */ |
| 2511 | spin_lock_irqsave(ap->lock, flags); |
| 2512 | if (!(ap->pflags & ATA_PFLAG_PM_PENDING) || |
| 2513 | ap->pm_mesg.event != PM_EVENT_ON) { |
| 2514 | spin_unlock_irqrestore(ap->lock, flags); |
| 2515 | return; |
| 2516 | } |
| 2517 | spin_unlock_irqrestore(ap->lock, flags); |
| 2518 | |
Tejun Heo | 9666f40 | 2007-05-04 21:27:47 +0200 | [diff] [blame] | 2519 | WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED)); |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 2520 | |
| 2521 | if (ap->ops->port_resume) |
| 2522 | rc = ap->ops->port_resume(ap); |
| 2523 | |
Tejun Heo | 6746544 | 2007-05-15 03:28:16 +0900 | [diff] [blame] | 2524 | /* tell ACPI that we're resuming */ |
| 2525 | ata_acpi_on_resume(ap); |
| 2526 | |
Tejun Heo | 9666f40 | 2007-05-04 21:27:47 +0200 | [diff] [blame] | 2527 | /* report result */ |
Tejun Heo | 500530f | 2006-07-03 16:07:27 +0900 | [diff] [blame] | 2528 | spin_lock_irqsave(ap->lock, flags); |
| 2529 | ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED); |
| 2530 | if (ap->pm_result) { |
| 2531 | *ap->pm_result = rc; |
| 2532 | ap->pm_result = NULL; |
| 2533 | } |
| 2534 | spin_unlock_irqrestore(ap->lock, flags); |
| 2535 | } |
Tejun Heo | 6ffa01d | 2007-03-02 17:32:47 +0900 | [diff] [blame] | 2536 | #endif /* CONFIG_PM */ |