ide-atapi.c 9.6 KB

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