ide-atapi.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * ATAPI support.
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/delay.h>
  6. #include <linux/ide.h>
  7. #include <scsi/scsi.h>
  8. #ifdef DEBUG
  9. #define debug_log(fmt, args...) \
  10. printk(KERN_INFO "ide: " fmt, ## args)
  11. #else
  12. #define debug_log(fmt, args...) do {} while (0)
  13. #endif
  14. /* TODO: unify the code thus making some arguments go away */
  15. ide_startstop_t ide_pc_intr(ide_drive_t *drive, struct ide_atapi_pc *pc,
  16. ide_handler_t *handler, unsigned int timeout, ide_expiry_t *expiry,
  17. void (*update_buffers)(ide_drive_t *, struct ide_atapi_pc *),
  18. void (*retry_pc)(ide_drive_t *), void (*dsc_handle)(ide_drive_t *),
  19. void (*io_buffers)(ide_drive_t *, struct ide_atapi_pc *, unsigned, int))
  20. {
  21. ide_hwif_t *hwif = drive->hwif;
  22. struct request *rq = hwif->hwgroup->rq;
  23. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  24. xfer_func_t *xferfunc;
  25. unsigned int temp;
  26. u16 bcount;
  27. u8 stat, ireason, scsi = drive->scsi;
  28. debug_log("Enter %s - interrupt handler\n", __func__);
  29. if (pc->flags & PC_FLAG_TIMEDOUT) {
  30. drive->pc_callback(drive);
  31. return ide_stopped;
  32. }
  33. /* Clear the interrupt */
  34. stat = tp_ops->read_status(hwif);
  35. if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
  36. if (hwif->dma_ops->dma_end(drive) ||
  37. (drive->media == ide_tape && !scsi && (stat & ATA_ERR))) {
  38. if (drive->media == ide_floppy && !scsi)
  39. printk(KERN_ERR "%s: DMA %s error\n",
  40. drive->name, rq_data_dir(pc->rq)
  41. ? "write" : "read");
  42. pc->flags |= PC_FLAG_DMA_ERROR;
  43. } else {
  44. pc->xferred = pc->req_xfer;
  45. if (update_buffers)
  46. update_buffers(drive, pc);
  47. }
  48. debug_log("%s: DMA finished\n", drive->name);
  49. }
  50. /* No more interrupts */
  51. if ((stat & ATA_DRQ) == 0) {
  52. debug_log("Packet command completed, %d bytes transferred\n",
  53. pc->xferred);
  54. pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
  55. local_irq_enable_in_hardirq();
  56. if (drive->media == ide_tape && !scsi &&
  57. (stat & ATA_ERR) && rq->cmd[0] == REQUEST_SENSE)
  58. stat &= ~ATA_ERR;
  59. if ((stat & ATA_ERR) || (pc->flags & PC_FLAG_DMA_ERROR)) {
  60. /* Error detected */
  61. debug_log("%s: I/O error\n", drive->name);
  62. if (drive->media != ide_tape || scsi) {
  63. pc->rq->errors++;
  64. if (scsi)
  65. goto cmd_finished;
  66. }
  67. if (rq->cmd[0] == REQUEST_SENSE) {
  68. printk(KERN_ERR "%s: I/O error in request sense"
  69. " command\n", drive->name);
  70. return ide_do_reset(drive);
  71. }
  72. debug_log("[cmd %x]: check condition\n", rq->cmd[0]);
  73. /* Retry operation */
  74. retry_pc(drive);
  75. /* queued, but not started */
  76. return ide_stopped;
  77. }
  78. cmd_finished:
  79. pc->error = 0;
  80. if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) &&
  81. (stat & ATA_DSC) == 0) {
  82. dsc_handle(drive);
  83. return ide_stopped;
  84. }
  85. /* Command finished - Call the callback function */
  86. drive->pc_callback(drive);
  87. return ide_stopped;
  88. }
  89. if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
  90. pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
  91. printk(KERN_ERR "%s: The device wants to issue more interrupts "
  92. "in DMA mode\n", drive->name);
  93. ide_dma_off(drive);
  94. return ide_do_reset(drive);
  95. }
  96. /* Get the number of bytes to transfer on this interrupt. */
  97. ide_read_bcount_and_ireason(drive, &bcount, &ireason);
  98. if (ireason & ATAPI_COD) {
  99. printk(KERN_ERR "%s: CoD != 0 in %s\n", drive->name, __func__);
  100. return ide_do_reset(drive);
  101. }
  102. if (((ireason & ATAPI_IO) == ATAPI_IO) ==
  103. !!(pc->flags & PC_FLAG_WRITING)) {
  104. /* Hopefully, we will never get here */
  105. printk(KERN_ERR "%s: We wanted to %s, but the device wants us "
  106. "to %s!\n", drive->name,
  107. (ireason & ATAPI_IO) ? "Write" : "Read",
  108. (ireason & ATAPI_IO) ? "Read" : "Write");
  109. return ide_do_reset(drive);
  110. }
  111. if (!(pc->flags & PC_FLAG_WRITING)) {
  112. /* Reading - Check that we have enough space */
  113. temp = pc->xferred + bcount;
  114. if (temp > pc->req_xfer) {
  115. if (temp > pc->buf_size) {
  116. printk(KERN_ERR "%s: The device wants to send "
  117. "us more data than expected - "
  118. "discarding data\n",
  119. drive->name);
  120. if (scsi)
  121. temp = pc->buf_size - pc->xferred;
  122. else
  123. temp = 0;
  124. if (temp) {
  125. if (pc->sg)
  126. io_buffers(drive, pc, temp, 0);
  127. else
  128. tp_ops->input_data(drive, NULL,
  129. pc->cur_pos, temp);
  130. printk(KERN_ERR "%s: transferred %d of "
  131. "%d bytes\n",
  132. drive->name,
  133. temp, bcount);
  134. }
  135. pc->xferred += temp;
  136. pc->cur_pos += temp;
  137. ide_pad_transfer(drive, 0, bcount - temp);
  138. ide_set_handler(drive, handler, timeout,
  139. expiry);
  140. return ide_started;
  141. }
  142. debug_log("The device wants to send us more data than "
  143. "expected - allowing transfer\n");
  144. }
  145. xferfunc = tp_ops->input_data;
  146. } else
  147. xferfunc = tp_ops->output_data;
  148. if ((drive->media == ide_floppy && !scsi && !pc->buf) ||
  149. (drive->media == ide_tape && !scsi && pc->bh) ||
  150. (scsi && pc->sg))
  151. io_buffers(drive, pc, bcount, !!(pc->flags & PC_FLAG_WRITING));
  152. else
  153. xferfunc(drive, NULL, pc->cur_pos, bcount);
  154. /* Update the current position */
  155. pc->xferred += bcount;
  156. pc->cur_pos += bcount;
  157. debug_log("[cmd %x] transferred %d bytes on that intr.\n",
  158. rq->cmd[0], bcount);
  159. /* And set the interrupt handler again */
  160. ide_set_handler(drive, handler, timeout, expiry);
  161. return ide_started;
  162. }
  163. EXPORT_SYMBOL_GPL(ide_pc_intr);
  164. static u8 ide_read_ireason(ide_drive_t *drive)
  165. {
  166. ide_task_t task;
  167. memset(&task, 0, sizeof(task));
  168. task.tf_flags = IDE_TFLAG_IN_NSECT;
  169. drive->hwif->tp_ops->tf_read(drive, &task);
  170. return task.tf.nsect & 3;
  171. }
  172. static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
  173. {
  174. int retries = 100;
  175. while (retries-- && ((ireason & ATAPI_COD) == 0 ||
  176. (ireason & ATAPI_IO))) {
  177. printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
  178. "a packet command, retrying\n", drive->name);
  179. udelay(100);
  180. ireason = ide_read_ireason(drive);
  181. if (retries == 0) {
  182. printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
  183. "a packet command, ignoring\n",
  184. drive->name);
  185. ireason |= ATAPI_COD;
  186. ireason &= ~ATAPI_IO;
  187. }
  188. }
  189. return ireason;
  190. }
  191. ide_startstop_t ide_transfer_pc(ide_drive_t *drive, struct ide_atapi_pc *pc,
  192. ide_handler_t *handler, unsigned int timeout,
  193. ide_expiry_t *expiry)
  194. {
  195. ide_hwif_t *hwif = drive->hwif;
  196. struct request *rq = hwif->hwgroup->rq;
  197. ide_startstop_t startstop;
  198. u8 ireason;
  199. if (ide_wait_stat(&startstop, drive, ATA_DRQ, ATA_BUSY, WAIT_READY)) {
  200. printk(KERN_ERR "%s: Strange, packet command initiated yet "
  201. "DRQ isn't asserted\n", drive->name);
  202. return startstop;
  203. }
  204. ireason = ide_read_ireason(drive);
  205. if (drive->media == ide_tape && !drive->scsi)
  206. ireason = ide_wait_ireason(drive, ireason);
  207. if ((ireason & ATAPI_COD) == 0 || (ireason & ATAPI_IO)) {
  208. printk(KERN_ERR "%s: (IO,CoD) != (0,1) while issuing "
  209. "a packet command\n", drive->name);
  210. return ide_do_reset(drive);
  211. }
  212. /* Set the interrupt routine */
  213. ide_set_handler(drive, handler, timeout, expiry);
  214. /* Begin DMA, if necessary */
  215. if (pc->flags & PC_FLAG_DMA_OK) {
  216. pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
  217. hwif->dma_ops->dma_start(drive);
  218. }
  219. /* Send the actual packet */
  220. if ((drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) == 0)
  221. hwif->tp_ops->output_data(drive, NULL, rq->cmd, 12);
  222. return ide_started;
  223. }
  224. EXPORT_SYMBOL_GPL(ide_transfer_pc);
  225. ide_startstop_t ide_issue_pc(ide_drive_t *drive, struct ide_atapi_pc *pc,
  226. ide_handler_t *handler, unsigned int timeout,
  227. ide_expiry_t *expiry)
  228. {
  229. ide_hwif_t *hwif = drive->hwif;
  230. u16 bcount;
  231. u8 dma = 0;
  232. /* We haven't transferred any data yet */
  233. pc->xferred = 0;
  234. pc->cur_pos = pc->buf;
  235. /* Request to transfer the entire buffer at once */
  236. if (drive->media == ide_tape && !drive->scsi)
  237. bcount = pc->req_xfer;
  238. else
  239. bcount = min(pc->req_xfer, 63 * 1024);
  240. if (pc->flags & PC_FLAG_DMA_ERROR) {
  241. pc->flags &= ~PC_FLAG_DMA_ERROR;
  242. ide_dma_off(drive);
  243. }
  244. if ((pc->flags & PC_FLAG_DMA_OK) && drive->using_dma) {
  245. if (drive->scsi)
  246. hwif->sg_mapped = 1;
  247. dma = !hwif->dma_ops->dma_setup(drive);
  248. if (drive->scsi)
  249. hwif->sg_mapped = 0;
  250. }
  251. if (!dma)
  252. pc->flags &= ~PC_FLAG_DMA_OK;
  253. ide_pktcmd_tf_load(drive, drive->scsi ? 0 : IDE_TFLAG_OUT_DEVICE,
  254. bcount, dma);
  255. /* Issue the packet command */
  256. if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
  257. ide_execute_command(drive, ATA_CMD_PACKET, handler,
  258. timeout, NULL);
  259. return ide_started;
  260. } else {
  261. ide_execute_pkt_cmd(drive);
  262. return (*handler)(drive);
  263. }
  264. }
  265. EXPORT_SYMBOL_GPL(ide_issue_pc);