ide-atapi.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 & ERR_STAT))) {
  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 & DRQ_STAT) == 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 & ERR_STAT) && rq->cmd[0] == REQUEST_SENSE)
  58. stat &= ~ERR_STAT;
  59. if ((stat & ERR_STAT) || (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 & SEEK_STAT) == 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 & 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. tp_ops->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 = tp_ops->input_data;
  145. } else
  146. xferfunc = tp_ops->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. rq->cmd[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_read_ireason(ide_drive_t *drive)
  164. {
  165. ide_task_t task;
  166. memset(&task, 0, sizeof(task));
  167. task.tf_flags = IDE_TFLAG_IN_NSECT;
  168. drive->hwif->tp_ops->tf_read(drive, &task);
  169. return task.tf.nsect & 3;
  170. }
  171. static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
  172. {
  173. int retries = 100;
  174. while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
  175. printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
  176. "a packet command, retrying\n", drive->name);
  177. udelay(100);
  178. ireason = ide_read_ireason(drive);
  179. if (retries == 0) {
  180. printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
  181. "a packet command, ignoring\n",
  182. drive->name);
  183. ireason |= CD;
  184. ireason &= ~IO;
  185. }
  186. }
  187. return ireason;
  188. }
  189. ide_startstop_t ide_transfer_pc(ide_drive_t *drive, struct ide_atapi_pc *pc,
  190. ide_handler_t *handler, unsigned int timeout,
  191. ide_expiry_t *expiry)
  192. {
  193. ide_hwif_t *hwif = drive->hwif;
  194. struct request *rq = hwif->hwgroup->rq;
  195. ide_startstop_t startstop;
  196. u8 ireason;
  197. if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
  198. printk(KERN_ERR "%s: Strange, packet command initiated yet "
  199. "DRQ isn't asserted\n", drive->name);
  200. return startstop;
  201. }
  202. ireason = ide_read_ireason(drive);
  203. if (drive->media == ide_tape && !drive->scsi)
  204. ireason = ide_wait_ireason(drive, ireason);
  205. if ((ireason & CD) == 0 || (ireason & IO)) {
  206. printk(KERN_ERR "%s: (IO,CoD) != (0,1) while issuing "
  207. "a packet command\n", drive->name);
  208. return ide_do_reset(drive);
  209. }
  210. /* Set the interrupt routine */
  211. ide_set_handler(drive, handler, timeout, expiry);
  212. /* Begin DMA, if necessary */
  213. if (pc->flags & PC_FLAG_DMA_OK) {
  214. pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
  215. hwif->dma_ops->dma_start(drive);
  216. }
  217. /* Send the actual packet */
  218. if ((drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) == 0)
  219. hwif->tp_ops->output_data(drive, NULL, rq->cmd, 12);
  220. return ide_started;
  221. }
  222. EXPORT_SYMBOL_GPL(ide_transfer_pc);
  223. ide_startstop_t ide_issue_pc(ide_drive_t *drive, struct ide_atapi_pc *pc,
  224. ide_handler_t *handler, unsigned int timeout,
  225. ide_expiry_t *expiry)
  226. {
  227. ide_hwif_t *hwif = drive->hwif;
  228. u16 bcount;
  229. u8 dma = 0;
  230. /* We haven't transferred any data yet */
  231. pc->xferred = 0;
  232. pc->cur_pos = pc->buf;
  233. /* Request to transfer the entire buffer at once */
  234. if (drive->media == ide_tape && !drive->scsi)
  235. bcount = pc->req_xfer;
  236. else
  237. bcount = min(pc->req_xfer, 63 * 1024);
  238. if (pc->flags & PC_FLAG_DMA_ERROR) {
  239. pc->flags &= ~PC_FLAG_DMA_ERROR;
  240. ide_dma_off(drive);
  241. }
  242. if ((pc->flags & PC_FLAG_DMA_OK) && drive->using_dma) {
  243. if (drive->scsi)
  244. hwif->sg_mapped = 1;
  245. dma = !hwif->dma_ops->dma_setup(drive);
  246. if (drive->scsi)
  247. hwif->sg_mapped = 0;
  248. }
  249. if (!dma)
  250. pc->flags &= ~PC_FLAG_DMA_OK;
  251. ide_pktcmd_tf_load(drive, drive->scsi ? 0 : IDE_TFLAG_OUT_DEVICE,
  252. bcount, dma);
  253. /* Issue the packet command */
  254. if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
  255. ide_execute_command(drive, WIN_PACKETCMD, handler,
  256. timeout, NULL);
  257. return ide_started;
  258. } else {
  259. ide_execute_pkt_cmd(drive);
  260. return (*handler)(drive);
  261. }
  262. }
  263. EXPORT_SYMBOL_GPL(ide_issue_pc);