libata-eh.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * libata-eh.c - libata error handling
  3. *
  4. * Maintained by: Jeff Garzik <jgarzik@pobox.com>
  5. * Please ALWAYS copy linux-ide@vger.kernel.org
  6. * on emails.
  7. *
  8. * Copyright 2006 Tejun Heo <htejun@gmail.com>
  9. *
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; see the file COPYING. If not, write to
  23. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
  24. * USA.
  25. *
  26. *
  27. * libata documentation is available via 'make {ps|pdf}docs',
  28. * as Documentation/DocBook/libata.*
  29. *
  30. * Hardware documentation available from http://www.t13.org/ and
  31. * http://www.sata-io.org/
  32. *
  33. */
  34. #include <linux/config.h>
  35. #include <linux/kernel.h>
  36. #include <scsi/scsi.h>
  37. #include <scsi/scsi_host.h>
  38. #include <scsi/scsi_eh.h>
  39. #include <scsi/scsi_device.h>
  40. #include <scsi/scsi_cmnd.h>
  41. #include <linux/libata.h>
  42. #include "libata.h"
  43. /**
  44. * ata_scsi_timed_out - SCSI layer time out callback
  45. * @cmd: timed out SCSI command
  46. *
  47. * Handles SCSI layer timeout. We race with normal completion of
  48. * the qc for @cmd. If the qc is already gone, we lose and let
  49. * the scsi command finish (EH_HANDLED). Otherwise, the qc has
  50. * timed out and EH should be invoked. Prevent ata_qc_complete()
  51. * from finishing it by setting EH_SCHEDULED and return
  52. * EH_NOT_HANDLED.
  53. *
  54. * LOCKING:
  55. * Called from timer context
  56. *
  57. * RETURNS:
  58. * EH_HANDLED or EH_NOT_HANDLED
  59. */
  60. enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
  61. {
  62. struct Scsi_Host *host = cmd->device->host;
  63. struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
  64. unsigned long flags;
  65. struct ata_queued_cmd *qc;
  66. enum scsi_eh_timer_return ret = EH_HANDLED;
  67. DPRINTK("ENTER\n");
  68. spin_lock_irqsave(&ap->host_set->lock, flags);
  69. qc = ata_qc_from_tag(ap, ap->active_tag);
  70. if (qc) {
  71. WARN_ON(qc->scsicmd != cmd);
  72. qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
  73. qc->err_mask |= AC_ERR_TIMEOUT;
  74. ret = EH_NOT_HANDLED;
  75. }
  76. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  77. DPRINTK("EXIT, ret=%d\n", ret);
  78. return ret;
  79. }
  80. /**
  81. * ata_scsi_error - SCSI layer error handler callback
  82. * @host: SCSI host on which error occurred
  83. *
  84. * Handles SCSI-layer-thrown error events.
  85. *
  86. * LOCKING:
  87. * Inherited from SCSI layer (none, can sleep)
  88. *
  89. * RETURNS:
  90. * Zero.
  91. */
  92. int ata_scsi_error(struct Scsi_Host *host)
  93. {
  94. struct ata_port *ap = (struct ata_port *)&host->hostdata[0];
  95. DPRINTK("ENTER\n");
  96. /* synchronize with IRQ handler and port task */
  97. spin_unlock_wait(&ap->host_set->lock);
  98. ata_port_flush_task(ap);
  99. WARN_ON(ata_qc_from_tag(ap, ap->active_tag) == NULL);
  100. ap->ops->eng_timeout(ap);
  101. WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
  102. scsi_eh_flush_done_q(&ap->eh_done_q);
  103. DPRINTK("EXIT\n");
  104. return 0;
  105. }
  106. /**
  107. * ata_qc_timeout - Handle timeout of queued command
  108. * @qc: Command that timed out
  109. *
  110. * Some part of the kernel (currently, only the SCSI layer)
  111. * has noticed that the active command on port @ap has not
  112. * completed after a specified length of time. Handle this
  113. * condition by disabling DMA (if necessary) and completing
  114. * transactions, with error if necessary.
  115. *
  116. * This also handles the case of the "lost interrupt", where
  117. * for some reason (possibly hardware bug, possibly driver bug)
  118. * an interrupt was not delivered to the driver, even though the
  119. * transaction completed successfully.
  120. *
  121. * LOCKING:
  122. * Inherited from SCSI layer (none, can sleep)
  123. */
  124. static void ata_qc_timeout(struct ata_queued_cmd *qc)
  125. {
  126. struct ata_port *ap = qc->ap;
  127. struct ata_host_set *host_set = ap->host_set;
  128. u8 host_stat = 0, drv_stat;
  129. unsigned long flags;
  130. DPRINTK("ENTER\n");
  131. ap->hsm_task_state = HSM_ST_IDLE;
  132. spin_lock_irqsave(&host_set->lock, flags);
  133. switch (qc->tf.protocol) {
  134. case ATA_PROT_DMA:
  135. case ATA_PROT_ATAPI_DMA:
  136. host_stat = ap->ops->bmdma_status(ap);
  137. /* before we do anything else, clear DMA-Start bit */
  138. ap->ops->bmdma_stop(qc);
  139. /* fall through */
  140. default:
  141. ata_altstatus(ap);
  142. drv_stat = ata_chk_status(ap);
  143. /* ack bmdma irq events */
  144. ap->ops->irq_clear(ap);
  145. printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
  146. ap->id, qc->tf.command, drv_stat, host_stat);
  147. /* complete taskfile transaction */
  148. qc->err_mask |= ac_err_mask(drv_stat);
  149. break;
  150. }
  151. spin_unlock_irqrestore(&host_set->lock, flags);
  152. ata_eh_qc_complete(qc);
  153. DPRINTK("EXIT\n");
  154. }
  155. /**
  156. * ata_eng_timeout - Handle timeout of queued command
  157. * @ap: Port on which timed-out command is active
  158. *
  159. * Some part of the kernel (currently, only the SCSI layer)
  160. * has noticed that the active command on port @ap has not
  161. * completed after a specified length of time. Handle this
  162. * condition by disabling DMA (if necessary) and completing
  163. * transactions, with error if necessary.
  164. *
  165. * This also handles the case of the "lost interrupt", where
  166. * for some reason (possibly hardware bug, possibly driver bug)
  167. * an interrupt was not delivered to the driver, even though the
  168. * transaction completed successfully.
  169. *
  170. * LOCKING:
  171. * Inherited from SCSI layer (none, can sleep)
  172. */
  173. void ata_eng_timeout(struct ata_port *ap)
  174. {
  175. DPRINTK("ENTER\n");
  176. ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
  177. DPRINTK("EXIT\n");
  178. }
  179. static void ata_eh_scsidone(struct scsi_cmnd *scmd)
  180. {
  181. /* nada */
  182. }
  183. static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
  184. {
  185. struct ata_port *ap = qc->ap;
  186. struct scsi_cmnd *scmd = qc->scsicmd;
  187. unsigned long flags;
  188. spin_lock_irqsave(&ap->host_set->lock, flags);
  189. qc->scsidone = ata_eh_scsidone;
  190. __ata_qc_complete(qc);
  191. WARN_ON(ata_tag_valid(qc->tag));
  192. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  193. scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
  194. }
  195. /**
  196. * ata_eh_qc_complete - Complete an active ATA command from EH
  197. * @qc: Command to complete
  198. *
  199. * Indicate to the mid and upper layers that an ATA command has
  200. * completed. To be used from EH.
  201. */
  202. void ata_eh_qc_complete(struct ata_queued_cmd *qc)
  203. {
  204. struct scsi_cmnd *scmd = qc->scsicmd;
  205. scmd->retries = scmd->allowed;
  206. __ata_eh_qc_complete(qc);
  207. }
  208. /**
  209. * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
  210. * @qc: Command to retry
  211. *
  212. * Indicate to the mid and upper layers that an ATA command
  213. * should be retried. To be used from EH.
  214. *
  215. * SCSI midlayer limits the number of retries to scmd->allowed.
  216. * scmd->retries is decremented for commands which get retried
  217. * due to unrelated failures (qc->err_mask is zero).
  218. */
  219. void ata_eh_qc_retry(struct ata_queued_cmd *qc)
  220. {
  221. struct scsi_cmnd *scmd = qc->scsicmd;
  222. if (!qc->err_mask && scmd->retries)
  223. scmd->retries--;
  224. __ata_eh_qc_complete(qc);
  225. }