ide-atapi.c 7.8 KB

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