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