ide-atapi.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 = hwif->read_status(hwif);
  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. ide_read_bcount_and_ireason(drive, &bcount, &ireason);
  96. if (ireason & CD) {
  97. printk(KERN_ERR "%s: CoD != 0 in %s\n", drive->name, __func__);
  98. return ide_do_reset(drive);
  99. }
  100. if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) {
  101. /* Hopefully, we will never get here */
  102. printk(KERN_ERR "%s: We wanted to %s, but the device wants us "
  103. "to %s!\n", drive->name,
  104. (ireason & IO) ? "Write" : "Read",
  105. (ireason & IO) ? "Read" : "Write");
  106. return ide_do_reset(drive);
  107. }
  108. if (!(pc->flags & PC_FLAG_WRITING)) {
  109. /* Reading - Check that we have enough space */
  110. temp = pc->xferred + bcount;
  111. if (temp > pc->req_xfer) {
  112. if (temp > pc->buf_size) {
  113. printk(KERN_ERR "%s: The device wants to send "
  114. "us more data than expected - "
  115. "discarding data\n",
  116. drive->name);
  117. if (scsi)
  118. temp = pc->buf_size - pc->xferred;
  119. else
  120. temp = 0;
  121. if (temp) {
  122. if (pc->sg)
  123. io_buffers(drive, pc, temp, 0);
  124. else
  125. hwif->input_data(drive, NULL,
  126. pc->cur_pos, temp);
  127. printk(KERN_ERR "%s: transferred %d of "
  128. "%d bytes\n",
  129. drive->name,
  130. temp, bcount);
  131. }
  132. pc->xferred += temp;
  133. pc->cur_pos += temp;
  134. ide_pad_transfer(drive, 0, bcount - temp);
  135. ide_set_handler(drive, handler, timeout,
  136. expiry);
  137. return ide_started;
  138. }
  139. debug_log("The device wants to send us more data than "
  140. "expected - allowing transfer\n");
  141. }
  142. xferfunc = hwif->input_data;
  143. } else
  144. xferfunc = hwif->output_data;
  145. if ((drive->media == ide_floppy && !scsi && !pc->buf) ||
  146. (drive->media == ide_tape && !scsi && pc->bh) ||
  147. (scsi && pc->sg))
  148. io_buffers(drive, pc, bcount, !!(pc->flags & PC_FLAG_WRITING));
  149. else
  150. xferfunc(drive, NULL, pc->cur_pos, bcount);
  151. /* Update the current position */
  152. pc->xferred += bcount;
  153. pc->cur_pos += bcount;
  154. debug_log("[cmd %x] transferred %d bytes on that intr.\n",
  155. pc->c[0], bcount);
  156. /* And set the interrupt handler again */
  157. ide_set_handler(drive, handler, timeout, expiry);
  158. return ide_started;
  159. }
  160. EXPORT_SYMBOL_GPL(ide_pc_intr);
  161. static u8 ide_read_ireason(ide_drive_t *drive)
  162. {
  163. ide_task_t task;
  164. memset(&task, 0, sizeof(task));
  165. task.tf_flags = IDE_TFLAG_IN_NSECT;
  166. drive->hwif->tf_read(drive, &task);
  167. return task.tf.nsect & 3;
  168. }
  169. static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
  170. {
  171. int retries = 100;
  172. while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
  173. printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
  174. "a packet command, retrying\n", drive->name);
  175. udelay(100);
  176. ireason = ide_read_ireason(drive);
  177. if (retries == 0) {
  178. printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
  179. "a packet command, ignoring\n",
  180. drive->name);
  181. ireason |= CD;
  182. ireason &= ~IO;
  183. }
  184. }
  185. return ireason;
  186. }
  187. ide_startstop_t ide_transfer_pc(ide_drive_t *drive, struct ide_atapi_pc *pc,
  188. ide_handler_t *handler, unsigned int timeout,
  189. ide_expiry_t *expiry)
  190. {
  191. ide_hwif_t *hwif = drive->hwif;
  192. ide_startstop_t startstop;
  193. u8 ireason;
  194. if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
  195. printk(KERN_ERR "%s: Strange, packet command initiated yet "
  196. "DRQ isn't asserted\n", drive->name);
  197. return startstop;
  198. }
  199. ireason = ide_read_ireason(drive);
  200. if (drive->media == ide_tape && !drive->scsi)
  201. ireason = ide_wait_ireason(drive, ireason);
  202. if ((ireason & CD) == 0 || (ireason & IO)) {
  203. printk(KERN_ERR "%s: (IO,CoD) != (0,1) while issuing "
  204. "a packet command\n", drive->name);
  205. return ide_do_reset(drive);
  206. }
  207. /* Set the interrupt routine */
  208. ide_set_handler(drive, handler, timeout, expiry);
  209. /* Begin DMA, if necessary */
  210. if (pc->flags & PC_FLAG_DMA_OK) {
  211. pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
  212. hwif->dma_ops->dma_start(drive);
  213. }
  214. /* Send the actual packet */
  215. if ((pc->flags & PC_FLAG_ZIP_DRIVE) == 0)
  216. hwif->output_data(drive, NULL, pc->c, 12);
  217. return ide_started;
  218. }
  219. EXPORT_SYMBOL_GPL(ide_transfer_pc);
  220. ide_startstop_t ide_issue_pc(ide_drive_t *drive, struct ide_atapi_pc *pc,
  221. ide_handler_t *handler, unsigned int timeout,
  222. ide_expiry_t *expiry)
  223. {
  224. ide_hwif_t *hwif = drive->hwif;
  225. u16 bcount;
  226. u8 dma = 0;
  227. /* We haven't transferred any data yet */
  228. pc->xferred = 0;
  229. pc->cur_pos = pc->buf;
  230. /* Request to transfer the entire buffer at once */
  231. if (drive->media == ide_tape && !drive->scsi)
  232. bcount = pc->req_xfer;
  233. else
  234. bcount = min(pc->req_xfer, 63 * 1024);
  235. if (pc->flags & PC_FLAG_DMA_ERROR) {
  236. pc->flags &= ~PC_FLAG_DMA_ERROR;
  237. ide_dma_off(drive);
  238. }
  239. if ((pc->flags & PC_FLAG_DMA_OK) && drive->using_dma) {
  240. if (drive->scsi)
  241. hwif->sg_mapped = 1;
  242. dma = !hwif->dma_ops->dma_setup(drive);
  243. if (drive->scsi)
  244. hwif->sg_mapped = 0;
  245. }
  246. if (!dma)
  247. pc->flags &= ~PC_FLAG_DMA_OK;
  248. ide_pktcmd_tf_load(drive, drive->scsi ? 0 : IDE_TFLAG_OUT_DEVICE,
  249. bcount, dma);
  250. /* Issue the packet command */
  251. if (pc->flags & PC_FLAG_DRQ_INTERRUPT) {
  252. ide_execute_command(drive, WIN_PACKETCMD, handler,
  253. timeout, NULL);
  254. return ide_started;
  255. } else {
  256. ide_execute_pkt_cmd(drive);
  257. return (*handler)(drive);
  258. }
  259. }
  260. EXPORT_SYMBOL_GPL(ide_issue_pc);