ide-atapi.c 8.0 KB

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