ide-atapi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /*
  2. * ATAPI support.
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/cdrom.h>
  6. #include <linux/delay.h>
  7. #include <linux/ide.h>
  8. #include <linux/scatterlist.h>
  9. #include <scsi/scsi.h>
  10. #ifdef DEBUG
  11. #define debug_log(fmt, args...) \
  12. printk(KERN_INFO "ide: " fmt, ## args)
  13. #else
  14. #define debug_log(fmt, args...) do {} while (0)
  15. #endif
  16. #define ATAPI_MIN_CDB_BYTES 12
  17. static inline int dev_is_idecd(ide_drive_t *drive)
  18. {
  19. return drive->media == ide_cdrom || drive->media == ide_optical;
  20. }
  21. /*
  22. * Check whether we can support a device,
  23. * based on the ATAPI IDENTIFY command results.
  24. */
  25. int ide_check_atapi_device(ide_drive_t *drive, const char *s)
  26. {
  27. u16 *id = drive->id;
  28. u8 gcw[2], protocol, device_type, removable, drq_type, packet_size;
  29. *((u16 *)&gcw) = id[ATA_ID_CONFIG];
  30. protocol = (gcw[1] & 0xC0) >> 6;
  31. device_type = gcw[1] & 0x1F;
  32. removable = (gcw[0] & 0x80) >> 7;
  33. drq_type = (gcw[0] & 0x60) >> 5;
  34. packet_size = gcw[0] & 0x03;
  35. #ifdef CONFIG_PPC
  36. /* kludge for Apple PowerBook internal zip */
  37. if (drive->media == ide_floppy && device_type == 5 &&
  38. !strstr((char *)&id[ATA_ID_PROD], "CD-ROM") &&
  39. strstr((char *)&id[ATA_ID_PROD], "ZIP"))
  40. device_type = 0;
  41. #endif
  42. if (protocol != 2)
  43. printk(KERN_ERR "%s: %s: protocol (0x%02x) is not ATAPI\n",
  44. s, drive->name, protocol);
  45. else if ((drive->media == ide_floppy && device_type != 0) ||
  46. (drive->media == ide_tape && device_type != 1))
  47. printk(KERN_ERR "%s: %s: invalid device type (0x%02x)\n",
  48. s, drive->name, device_type);
  49. else if (removable == 0)
  50. printk(KERN_ERR "%s: %s: the removable flag is not set\n",
  51. s, drive->name);
  52. else if (drive->media == ide_floppy && drq_type == 3)
  53. printk(KERN_ERR "%s: %s: sorry, DRQ type (0x%02x) not "
  54. "supported\n", s, drive->name, drq_type);
  55. else if (packet_size != 0)
  56. printk(KERN_ERR "%s: %s: packet size (0x%02x) is not 12 "
  57. "bytes\n", s, drive->name, packet_size);
  58. else
  59. return 1;
  60. return 0;
  61. }
  62. EXPORT_SYMBOL_GPL(ide_check_atapi_device);
  63. /* PIO data transfer routine using the scatter gather table. */
  64. int ide_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
  65. unsigned int bcount, int write)
  66. {
  67. ide_hwif_t *hwif = drive->hwif;
  68. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  69. xfer_func_t *xf = write ? tp_ops->output_data : tp_ops->input_data;
  70. struct scatterlist *sg = pc->sg;
  71. char *buf;
  72. int count, done = 0;
  73. while (bcount) {
  74. count = min(sg->length - pc->b_count, bcount);
  75. if (PageHighMem(sg_page(sg))) {
  76. unsigned long flags;
  77. local_irq_save(flags);
  78. buf = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
  79. xf(drive, NULL, buf + pc->b_count, count);
  80. kunmap_atomic(buf - sg->offset, KM_IRQ0);
  81. local_irq_restore(flags);
  82. } else {
  83. buf = sg_virt(sg);
  84. xf(drive, NULL, buf + pc->b_count, count);
  85. }
  86. bcount -= count;
  87. pc->b_count += count;
  88. done += count;
  89. if (pc->b_count == sg->length) {
  90. if (!--pc->sg_cnt)
  91. break;
  92. pc->sg = sg = sg_next(sg);
  93. pc->b_count = 0;
  94. }
  95. }
  96. if (bcount) {
  97. printk(KERN_ERR "%s: %d leftover bytes, %s\n", drive->name,
  98. bcount, write ? "padding with zeros"
  99. : "discarding data");
  100. ide_pad_transfer(drive, write, bcount);
  101. }
  102. return done;
  103. }
  104. EXPORT_SYMBOL_GPL(ide_io_buffers);
  105. void ide_init_pc(struct ide_atapi_pc *pc)
  106. {
  107. memset(pc, 0, sizeof(*pc));
  108. pc->buf = pc->pc_buf;
  109. pc->buf_size = IDE_PC_BUFFER_SIZE;
  110. }
  111. EXPORT_SYMBOL_GPL(ide_init_pc);
  112. /*
  113. * Generate a new packet command request in front of the request queue, before
  114. * the current request, so that it will be processed immediately, on the next
  115. * pass through the driver.
  116. */
  117. static void ide_queue_pc_head(ide_drive_t *drive, struct gendisk *disk,
  118. struct ide_atapi_pc *pc, struct request *rq)
  119. {
  120. blk_rq_init(NULL, rq);
  121. rq->cmd_type = REQ_TYPE_SPECIAL;
  122. rq->cmd_flags |= REQ_PREEMPT;
  123. rq->buffer = (char *)pc;
  124. rq->rq_disk = disk;
  125. if (pc->req_xfer) {
  126. rq->data = pc->buf;
  127. rq->data_len = pc->req_xfer;
  128. }
  129. memcpy(rq->cmd, pc->c, 12);
  130. if (drive->media == ide_tape)
  131. rq->cmd[13] = REQ_IDETAPE_PC1;
  132. drive->hwif->rq = NULL;
  133. elv_add_request(drive->queue, rq, ELEVATOR_INSERT_FRONT, 0);
  134. }
  135. /*
  136. * Add a special packet command request to the tail of the request queue,
  137. * and wait for it to be serviced.
  138. */
  139. int ide_queue_pc_tail(ide_drive_t *drive, struct gendisk *disk,
  140. struct ide_atapi_pc *pc)
  141. {
  142. struct request *rq;
  143. int error;
  144. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  145. rq->cmd_type = REQ_TYPE_SPECIAL;
  146. rq->buffer = (char *)pc;
  147. if (pc->req_xfer) {
  148. rq->data = pc->buf;
  149. rq->data_len = pc->req_xfer;
  150. }
  151. memcpy(rq->cmd, pc->c, 12);
  152. if (drive->media == ide_tape)
  153. rq->cmd[13] = REQ_IDETAPE_PC1;
  154. error = blk_execute_rq(drive->queue, disk, rq, 0);
  155. blk_put_request(rq);
  156. return error;
  157. }
  158. EXPORT_SYMBOL_GPL(ide_queue_pc_tail);
  159. int ide_do_test_unit_ready(ide_drive_t *drive, struct gendisk *disk)
  160. {
  161. struct ide_atapi_pc pc;
  162. ide_init_pc(&pc);
  163. pc.c[0] = TEST_UNIT_READY;
  164. return ide_queue_pc_tail(drive, disk, &pc);
  165. }
  166. EXPORT_SYMBOL_GPL(ide_do_test_unit_ready);
  167. int ide_do_start_stop(ide_drive_t *drive, struct gendisk *disk, int start)
  168. {
  169. struct ide_atapi_pc pc;
  170. ide_init_pc(&pc);
  171. pc.c[0] = START_STOP;
  172. pc.c[4] = start;
  173. if (drive->media == ide_tape)
  174. pc.flags |= PC_FLAG_WAIT_FOR_DSC;
  175. return ide_queue_pc_tail(drive, disk, &pc);
  176. }
  177. EXPORT_SYMBOL_GPL(ide_do_start_stop);
  178. int ide_set_media_lock(ide_drive_t *drive, struct gendisk *disk, int on)
  179. {
  180. struct ide_atapi_pc pc;
  181. if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) == 0)
  182. return 0;
  183. ide_init_pc(&pc);
  184. pc.c[0] = ALLOW_MEDIUM_REMOVAL;
  185. pc.c[4] = on;
  186. return ide_queue_pc_tail(drive, disk, &pc);
  187. }
  188. EXPORT_SYMBOL_GPL(ide_set_media_lock);
  189. void ide_create_request_sense_cmd(ide_drive_t *drive, struct ide_atapi_pc *pc)
  190. {
  191. ide_init_pc(pc);
  192. pc->c[0] = REQUEST_SENSE;
  193. if (drive->media == ide_floppy) {
  194. pc->c[4] = 255;
  195. pc->req_xfer = 18;
  196. } else {
  197. pc->c[4] = 20;
  198. pc->req_xfer = 20;
  199. }
  200. }
  201. EXPORT_SYMBOL_GPL(ide_create_request_sense_cmd);
  202. /*
  203. * Called when an error was detected during the last packet command.
  204. * We queue a request sense packet command in the head of the request list.
  205. */
  206. void ide_retry_pc(ide_drive_t *drive, struct gendisk *disk)
  207. {
  208. struct request *rq = &drive->request_sense_rq;
  209. struct ide_atapi_pc *pc = &drive->request_sense_pc;
  210. (void)ide_read_error(drive);
  211. ide_create_request_sense_cmd(drive, pc);
  212. if (drive->media == ide_tape)
  213. set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
  214. ide_queue_pc_head(drive, disk, pc, rq);
  215. }
  216. EXPORT_SYMBOL_GPL(ide_retry_pc);
  217. int ide_cd_expiry(ide_drive_t *drive)
  218. {
  219. struct request *rq = drive->hwif->rq;
  220. unsigned long wait = 0;
  221. debug_log("%s: rq->cmd[0]: 0x%x\n", __func__, rq->cmd[0]);
  222. /*
  223. * Some commands are *slow* and normally take a long time to complete.
  224. * Usually we can use the ATAPI "disconnect" to bypass this, but not all
  225. * commands/drives support that. Let ide_timer_expiry keep polling us
  226. * for these.
  227. */
  228. switch (rq->cmd[0]) {
  229. case GPCMD_BLANK:
  230. case GPCMD_FORMAT_UNIT:
  231. case GPCMD_RESERVE_RZONE_TRACK:
  232. case GPCMD_CLOSE_TRACK:
  233. case GPCMD_FLUSH_CACHE:
  234. wait = ATAPI_WAIT_PC;
  235. break;
  236. default:
  237. if (!(rq->cmd_flags & REQ_QUIET))
  238. printk(KERN_INFO "cmd 0x%x timed out\n",
  239. rq->cmd[0]);
  240. wait = 0;
  241. break;
  242. }
  243. return wait;
  244. }
  245. EXPORT_SYMBOL_GPL(ide_cd_expiry);
  246. int ide_cd_get_xferlen(struct request *rq)
  247. {
  248. if (blk_fs_request(rq))
  249. return 32768;
  250. else if (blk_sense_request(rq) || blk_pc_request(rq) ||
  251. rq->cmd_type == REQ_TYPE_ATA_PC)
  252. return rq->data_len;
  253. else
  254. return 0;
  255. }
  256. EXPORT_SYMBOL_GPL(ide_cd_get_xferlen);
  257. void ide_read_bcount_and_ireason(ide_drive_t *drive, u16 *bcount, u8 *ireason)
  258. {
  259. struct ide_cmd cmd;
  260. memset(&cmd, 0, sizeof(cmd));
  261. cmd.tf_flags = IDE_TFLAG_IN_LBAH | IDE_TFLAG_IN_LBAM |
  262. IDE_TFLAG_IN_NSECT;
  263. drive->hwif->tp_ops->tf_read(drive, &cmd);
  264. *bcount = (cmd.tf.lbah << 8) | cmd.tf.lbam;
  265. *ireason = cmd.tf.nsect & 3;
  266. }
  267. EXPORT_SYMBOL_GPL(ide_read_bcount_and_ireason);
  268. /*
  269. * This is the usual interrupt handler which will be called during a packet
  270. * command. We will transfer some of the data (as requested by the drive)
  271. * and will re-point interrupt handler to us.
  272. */
  273. static ide_startstop_t ide_pc_intr(ide_drive_t *drive)
  274. {
  275. struct ide_atapi_pc *pc = drive->pc;
  276. ide_hwif_t *hwif = drive->hwif;
  277. struct ide_cmd *cmd = &hwif->cmd;
  278. struct request *rq = hwif->rq;
  279. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  280. xfer_func_t *xferfunc;
  281. unsigned int timeout, temp;
  282. u16 bcount;
  283. u8 stat, ireason, dsc = 0;
  284. debug_log("Enter %s - interrupt handler\n", __func__);
  285. timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
  286. : WAIT_TAPE_CMD;
  287. /* Clear the interrupt */
  288. stat = tp_ops->read_status(hwif);
  289. if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
  290. int rc;
  291. drive->waiting_for_dma = 0;
  292. rc = hwif->dma_ops->dma_end(drive);
  293. ide_dma_unmap_sg(drive, cmd);
  294. if (rc || (drive->media == ide_tape && (stat & ATA_ERR))) {
  295. if (drive->media == ide_floppy)
  296. printk(KERN_ERR "%s: DMA %s error\n",
  297. drive->name, rq_data_dir(pc->rq)
  298. ? "write" : "read");
  299. pc->flags |= PC_FLAG_DMA_ERROR;
  300. } else {
  301. pc->xferred = pc->req_xfer;
  302. if (drive->pc_update_buffers)
  303. drive->pc_update_buffers(drive, pc);
  304. }
  305. debug_log("%s: DMA finished\n", drive->name);
  306. }
  307. /* No more interrupts */
  308. if ((stat & ATA_DRQ) == 0) {
  309. int uptodate;
  310. debug_log("Packet command completed, %d bytes transferred\n",
  311. pc->xferred);
  312. pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
  313. local_irq_enable_in_hardirq();
  314. if (drive->media == ide_tape &&
  315. (stat & ATA_ERR) && rq->cmd[0] == REQUEST_SENSE)
  316. stat &= ~ATA_ERR;
  317. if ((stat & ATA_ERR) || (pc->flags & PC_FLAG_DMA_ERROR)) {
  318. /* Error detected */
  319. debug_log("%s: I/O error\n", drive->name);
  320. if (drive->media != ide_tape)
  321. pc->rq->errors++;
  322. if (rq->cmd[0] == REQUEST_SENSE) {
  323. printk(KERN_ERR "%s: I/O error in request sense"
  324. " command\n", drive->name);
  325. return ide_do_reset(drive);
  326. }
  327. debug_log("[cmd %x]: check condition\n", rq->cmd[0]);
  328. /* Retry operation */
  329. ide_retry_pc(drive, rq->rq_disk);
  330. /* queued, but not started */
  331. return ide_stopped;
  332. }
  333. pc->error = 0;
  334. if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) && (stat & ATA_DSC) == 0)
  335. dsc = 1;
  336. /* Command finished - Call the callback function */
  337. uptodate = drive->pc_callback(drive, dsc);
  338. if (uptodate == 0)
  339. drive->failed_pc = NULL;
  340. if (blk_special_request(rq)) {
  341. rq->errors = 0;
  342. ide_complete_rq(drive, 0, blk_rq_bytes(rq));
  343. } else {
  344. if (blk_fs_request(rq) == 0 && uptodate <= 0) {
  345. if (rq->errors == 0)
  346. rq->errors = -EIO;
  347. }
  348. ide_complete_rq(drive, uptodate ? 0 : -EIO,
  349. ide_rq_bytes(rq));
  350. }
  351. return ide_stopped;
  352. }
  353. if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
  354. pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
  355. printk(KERN_ERR "%s: The device wants to issue more interrupts "
  356. "in DMA mode\n", drive->name);
  357. ide_dma_off(drive);
  358. return ide_do_reset(drive);
  359. }
  360. /* Get the number of bytes to transfer on this interrupt. */
  361. ide_read_bcount_and_ireason(drive, &bcount, &ireason);
  362. if (ireason & ATAPI_COD) {
  363. printk(KERN_ERR "%s: CoD != 0 in %s\n", drive->name, __func__);
  364. return ide_do_reset(drive);
  365. }
  366. if (((ireason & ATAPI_IO) == ATAPI_IO) ==
  367. !!(pc->flags & PC_FLAG_WRITING)) {
  368. /* Hopefully, we will never get here */
  369. printk(KERN_ERR "%s: We wanted to %s, but the device wants us "
  370. "to %s!\n", drive->name,
  371. (ireason & ATAPI_IO) ? "Write" : "Read",
  372. (ireason & ATAPI_IO) ? "Read" : "Write");
  373. return ide_do_reset(drive);
  374. }
  375. if (!(pc->flags & PC_FLAG_WRITING)) {
  376. /* Reading - Check that we have enough space */
  377. temp = pc->xferred + bcount;
  378. if (temp > pc->req_xfer) {
  379. if (temp > pc->buf_size) {
  380. printk(KERN_ERR "%s: The device wants to send "
  381. "us more data than expected - "
  382. "discarding data\n",
  383. drive->name);
  384. ide_pad_transfer(drive, 0, bcount);
  385. goto next_irq;
  386. }
  387. debug_log("The device wants to send us more data than "
  388. "expected - allowing transfer\n");
  389. }
  390. xferfunc = tp_ops->input_data;
  391. } else
  392. xferfunc = tp_ops->output_data;
  393. if ((drive->media == ide_floppy && !pc->buf) ||
  394. (drive->media == ide_tape && pc->bh)) {
  395. int done = drive->pc_io_buffers(drive, pc, bcount,
  396. !!(pc->flags & PC_FLAG_WRITING));
  397. /* FIXME: don't do partial completions */
  398. if (drive->media == ide_floppy)
  399. ide_complete_rq(drive, 0,
  400. done ? done : ide_rq_bytes(rq));
  401. } else
  402. xferfunc(drive, NULL, pc->cur_pos, bcount);
  403. /* Update the current position */
  404. pc->xferred += bcount;
  405. pc->cur_pos += bcount;
  406. debug_log("[cmd %x] transferred %d bytes on that intr.\n",
  407. rq->cmd[0], bcount);
  408. next_irq:
  409. /* And set the interrupt handler again */
  410. ide_set_handler(drive, ide_pc_intr, timeout);
  411. return ide_started;
  412. }
  413. static void ide_init_packet_cmd(struct ide_cmd *cmd, u32 tf_flags,
  414. u16 bcount, u8 dma)
  415. {
  416. cmd->protocol = dma ? ATAPI_PROT_DMA : ATAPI_PROT_PIO;
  417. cmd->tf_flags |= IDE_TFLAG_OUT_LBAH | IDE_TFLAG_OUT_LBAM |
  418. IDE_TFLAG_OUT_FEATURE | tf_flags;
  419. cmd->tf.command = ATA_CMD_PACKET;
  420. cmd->tf.feature = dma; /* Use PIO/DMA */
  421. cmd->tf.lbam = bcount & 0xff;
  422. cmd->tf.lbah = (bcount >> 8) & 0xff;
  423. }
  424. static u8 ide_read_ireason(ide_drive_t *drive)
  425. {
  426. struct ide_cmd cmd;
  427. memset(&cmd, 0, sizeof(cmd));
  428. cmd.tf_flags = IDE_TFLAG_IN_NSECT;
  429. drive->hwif->tp_ops->tf_read(drive, &cmd);
  430. return cmd.tf.nsect & 3;
  431. }
  432. static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
  433. {
  434. int retries = 100;
  435. while (retries-- && ((ireason & ATAPI_COD) == 0 ||
  436. (ireason & ATAPI_IO))) {
  437. printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
  438. "a packet command, retrying\n", drive->name);
  439. udelay(100);
  440. ireason = ide_read_ireason(drive);
  441. if (retries == 0) {
  442. printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
  443. "a packet command, ignoring\n",
  444. drive->name);
  445. ireason |= ATAPI_COD;
  446. ireason &= ~ATAPI_IO;
  447. }
  448. }
  449. return ireason;
  450. }
  451. static int ide_delayed_transfer_pc(ide_drive_t *drive)
  452. {
  453. /* Send the actual packet */
  454. drive->hwif->tp_ops->output_data(drive, NULL, drive->pc->c, 12);
  455. /* Timeout for the packet command */
  456. return WAIT_FLOPPY_CMD;
  457. }
  458. static ide_startstop_t ide_transfer_pc(ide_drive_t *drive)
  459. {
  460. struct ide_atapi_pc *uninitialized_var(pc);
  461. ide_hwif_t *hwif = drive->hwif;
  462. struct request *rq = hwif->rq;
  463. ide_expiry_t *expiry;
  464. unsigned int timeout;
  465. int cmd_len;
  466. ide_startstop_t startstop;
  467. u8 ireason;
  468. if (ide_wait_stat(&startstop, drive, ATA_DRQ, ATA_BUSY, WAIT_READY)) {
  469. printk(KERN_ERR "%s: Strange, packet command initiated yet "
  470. "DRQ isn't asserted\n", drive->name);
  471. return startstop;
  472. }
  473. if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
  474. if (drive->dma)
  475. drive->waiting_for_dma = 1;
  476. }
  477. if (dev_is_idecd(drive)) {
  478. /* ATAPI commands get padded out to 12 bytes minimum */
  479. cmd_len = COMMAND_SIZE(rq->cmd[0]);
  480. if (cmd_len < ATAPI_MIN_CDB_BYTES)
  481. cmd_len = ATAPI_MIN_CDB_BYTES;
  482. timeout = rq->timeout;
  483. expiry = ide_cd_expiry;
  484. } else {
  485. pc = drive->pc;
  486. cmd_len = ATAPI_MIN_CDB_BYTES;
  487. /*
  488. * If necessary schedule the packet transfer to occur 'timeout'
  489. * miliseconds later in ide_delayed_transfer_pc() after the
  490. * device says it's ready for a packet.
  491. */
  492. if (drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) {
  493. timeout = drive->pc_delay;
  494. expiry = &ide_delayed_transfer_pc;
  495. } else {
  496. timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
  497. : WAIT_TAPE_CMD;
  498. expiry = NULL;
  499. }
  500. ireason = ide_read_ireason(drive);
  501. if (drive->media == ide_tape)
  502. ireason = ide_wait_ireason(drive, ireason);
  503. if ((ireason & ATAPI_COD) == 0 || (ireason & ATAPI_IO)) {
  504. printk(KERN_ERR "%s: (IO,CoD) != (0,1) while issuing "
  505. "a packet command\n", drive->name);
  506. return ide_do_reset(drive);
  507. }
  508. }
  509. hwif->expiry = expiry;
  510. /* Set the interrupt routine */
  511. ide_set_handler(drive,
  512. (dev_is_idecd(drive) ? drive->irq_handler
  513. : ide_pc_intr),
  514. timeout);
  515. /* Send the actual packet */
  516. if ((drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) == 0)
  517. hwif->tp_ops->output_data(drive, NULL, rq->cmd, cmd_len);
  518. /* Begin DMA, if necessary */
  519. if (dev_is_idecd(drive)) {
  520. if (drive->dma)
  521. hwif->dma_ops->dma_start(drive);
  522. } else {
  523. if (pc->flags & PC_FLAG_DMA_OK) {
  524. pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
  525. hwif->dma_ops->dma_start(drive);
  526. }
  527. }
  528. return ide_started;
  529. }
  530. ide_startstop_t ide_issue_pc(ide_drive_t *drive, struct ide_cmd *cmd)
  531. {
  532. struct ide_atapi_pc *pc;
  533. ide_hwif_t *hwif = drive->hwif;
  534. ide_expiry_t *expiry = NULL;
  535. struct request *rq = hwif->rq;
  536. unsigned int timeout;
  537. u32 tf_flags;
  538. u16 bcount;
  539. u8 drq_int = !!(drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT);
  540. if (dev_is_idecd(drive)) {
  541. tf_flags = IDE_TFLAG_OUT_NSECT | IDE_TFLAG_OUT_LBAL;
  542. bcount = ide_cd_get_xferlen(rq);
  543. expiry = ide_cd_expiry;
  544. timeout = ATAPI_WAIT_PC;
  545. if (drive->dma)
  546. drive->dma = !ide_dma_prepare(drive, cmd);
  547. } else {
  548. pc = drive->pc;
  549. /* We haven't transferred any data yet */
  550. pc->xferred = 0;
  551. pc->cur_pos = pc->buf;
  552. tf_flags = IDE_TFLAG_OUT_DEVICE;
  553. bcount = ((drive->media == ide_tape) ?
  554. pc->req_xfer :
  555. min(pc->req_xfer, 63 * 1024));
  556. if (pc->flags & PC_FLAG_DMA_ERROR) {
  557. pc->flags &= ~PC_FLAG_DMA_ERROR;
  558. ide_dma_off(drive);
  559. }
  560. if (pc->flags & PC_FLAG_DMA_OK)
  561. drive->dma = !ide_dma_prepare(drive, cmd);
  562. if (!drive->dma)
  563. pc->flags &= ~PC_FLAG_DMA_OK;
  564. timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
  565. : WAIT_TAPE_CMD;
  566. }
  567. ide_init_packet_cmd(cmd, tf_flags, bcount, drive->dma);
  568. (void)do_rw_taskfile(drive, cmd);
  569. if (drq_int) {
  570. if (drive->dma)
  571. drive->waiting_for_dma = 0;
  572. hwif->expiry = expiry;
  573. }
  574. ide_execute_command(drive, cmd, ide_transfer_pc, timeout);
  575. return drq_int ? ide_started : ide_transfer_pc(drive);
  576. }
  577. EXPORT_SYMBOL_GPL(ide_issue_pc);