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