ide-atapi.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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. void ide_init_pc(struct ide_atapi_pc *pc)
  64. {
  65. memset(pc, 0, sizeof(*pc));
  66. pc->buf = pc->pc_buf;
  67. pc->buf_size = IDE_PC_BUFFER_SIZE;
  68. }
  69. EXPORT_SYMBOL_GPL(ide_init_pc);
  70. /*
  71. * Add a special packet command request to the tail of the request queue,
  72. * and wait for it to be serviced.
  73. */
  74. int ide_queue_pc_tail(ide_drive_t *drive, struct gendisk *disk,
  75. struct ide_atapi_pc *pc)
  76. {
  77. struct request *rq;
  78. int error;
  79. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  80. rq->cmd_type = REQ_TYPE_SPECIAL;
  81. rq->special = (char *)pc;
  82. if (pc->req_xfer) {
  83. rq->data = pc->buf;
  84. rq->data_len = pc->req_xfer;
  85. }
  86. memcpy(rq->cmd, pc->c, 12);
  87. if (drive->media == ide_tape)
  88. rq->cmd[13] = REQ_IDETAPE_PC1;
  89. error = blk_execute_rq(drive->queue, disk, rq, 0);
  90. blk_put_request(rq);
  91. return error;
  92. }
  93. EXPORT_SYMBOL_GPL(ide_queue_pc_tail);
  94. int ide_do_test_unit_ready(ide_drive_t *drive, struct gendisk *disk)
  95. {
  96. struct ide_atapi_pc pc;
  97. ide_init_pc(&pc);
  98. pc.c[0] = TEST_UNIT_READY;
  99. return ide_queue_pc_tail(drive, disk, &pc);
  100. }
  101. EXPORT_SYMBOL_GPL(ide_do_test_unit_ready);
  102. int ide_do_start_stop(ide_drive_t *drive, struct gendisk *disk, int start)
  103. {
  104. struct ide_atapi_pc pc;
  105. ide_init_pc(&pc);
  106. pc.c[0] = START_STOP;
  107. pc.c[4] = start;
  108. if (drive->media == ide_tape)
  109. pc.flags |= PC_FLAG_WAIT_FOR_DSC;
  110. return ide_queue_pc_tail(drive, disk, &pc);
  111. }
  112. EXPORT_SYMBOL_GPL(ide_do_start_stop);
  113. int ide_set_media_lock(ide_drive_t *drive, struct gendisk *disk, int on)
  114. {
  115. struct ide_atapi_pc pc;
  116. if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) == 0)
  117. return 0;
  118. ide_init_pc(&pc);
  119. pc.c[0] = ALLOW_MEDIUM_REMOVAL;
  120. pc.c[4] = on;
  121. return ide_queue_pc_tail(drive, disk, &pc);
  122. }
  123. EXPORT_SYMBOL_GPL(ide_set_media_lock);
  124. void ide_create_request_sense_cmd(ide_drive_t *drive, struct ide_atapi_pc *pc)
  125. {
  126. ide_init_pc(pc);
  127. pc->c[0] = REQUEST_SENSE;
  128. if (drive->media == ide_floppy) {
  129. pc->c[4] = 255;
  130. pc->req_xfer = 18;
  131. } else {
  132. pc->c[4] = 20;
  133. pc->req_xfer = 20;
  134. }
  135. }
  136. EXPORT_SYMBOL_GPL(ide_create_request_sense_cmd);
  137. void ide_prep_sense(ide_drive_t *drive, struct request *rq)
  138. {
  139. struct request_sense *sense = &drive->sense_data;
  140. struct request *sense_rq = &drive->sense_rq;
  141. unsigned int cmd_len, sense_len;
  142. debug_log("%s: enter\n", __func__);
  143. switch (drive->media) {
  144. case ide_floppy:
  145. cmd_len = 255;
  146. sense_len = 18;
  147. break;
  148. case ide_tape:
  149. cmd_len = 20;
  150. sense_len = 20;
  151. break;
  152. default:
  153. cmd_len = 18;
  154. sense_len = 18;
  155. }
  156. BUG_ON(sense_len > sizeof(*sense));
  157. if (blk_sense_request(rq) || drive->sense_rq_armed)
  158. return;
  159. memset(sense, 0, sizeof(*sense));
  160. blk_rq_init(rq->q, sense_rq);
  161. sense_rq->rq_disk = rq->rq_disk;
  162. sense_rq->data = sense;
  163. sense_rq->cmd[0] = GPCMD_REQUEST_SENSE;
  164. sense_rq->cmd[4] = cmd_len;
  165. sense_rq->data_len = sense_len;
  166. sense_rq->cmd_type = REQ_TYPE_SENSE;
  167. sense_rq->cmd_flags |= REQ_PREEMPT;
  168. if (drive->media == ide_tape)
  169. sense_rq->cmd[13] = REQ_IDETAPE_PC1;
  170. drive->sense_rq_armed = true;
  171. }
  172. EXPORT_SYMBOL_GPL(ide_prep_sense);
  173. void ide_queue_sense_rq(ide_drive_t *drive, void *special)
  174. {
  175. BUG_ON(!drive->sense_rq_armed);
  176. drive->sense_rq.special = special;
  177. drive->sense_rq_armed = false;
  178. drive->hwif->rq = NULL;
  179. elv_add_request(drive->queue, &drive->sense_rq,
  180. ELEVATOR_INSERT_FRONT, 0);
  181. }
  182. EXPORT_SYMBOL_GPL(ide_queue_sense_rq);
  183. /*
  184. * Called when an error was detected during the last packet command.
  185. * We queue a request sense packet command at the head of the request
  186. * queue.
  187. */
  188. void ide_retry_pc(ide_drive_t *drive)
  189. {
  190. struct request *sense_rq = &drive->sense_rq;
  191. struct ide_atapi_pc *pc = &drive->request_sense_pc;
  192. (void)ide_read_error(drive);
  193. /* init pc from sense_rq */
  194. ide_init_pc(pc);
  195. memcpy(pc->c, sense_rq->cmd, 12);
  196. pc->buf = sense_rq->data;
  197. pc->req_xfer = sense_rq->data_len;
  198. if (drive->media == ide_tape)
  199. set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
  200. ide_queue_sense_rq(drive, pc);
  201. }
  202. EXPORT_SYMBOL_GPL(ide_retry_pc);
  203. int ide_cd_expiry(ide_drive_t *drive)
  204. {
  205. struct request *rq = drive->hwif->rq;
  206. unsigned long wait = 0;
  207. debug_log("%s: rq->cmd[0]: 0x%x\n", __func__, rq->cmd[0]);
  208. /*
  209. * Some commands are *slow* and normally take a long time to complete.
  210. * Usually we can use the ATAPI "disconnect" to bypass this, but not all
  211. * commands/drives support that. Let ide_timer_expiry keep polling us
  212. * for these.
  213. */
  214. switch (rq->cmd[0]) {
  215. case GPCMD_BLANK:
  216. case GPCMD_FORMAT_UNIT:
  217. case GPCMD_RESERVE_RZONE_TRACK:
  218. case GPCMD_CLOSE_TRACK:
  219. case GPCMD_FLUSH_CACHE:
  220. wait = ATAPI_WAIT_PC;
  221. break;
  222. default:
  223. if (!(rq->cmd_flags & REQ_QUIET))
  224. printk(KERN_INFO "cmd 0x%x timed out\n",
  225. rq->cmd[0]);
  226. wait = 0;
  227. break;
  228. }
  229. return wait;
  230. }
  231. EXPORT_SYMBOL_GPL(ide_cd_expiry);
  232. int ide_cd_get_xferlen(struct request *rq)
  233. {
  234. if (blk_fs_request(rq))
  235. return 32768;
  236. else if (blk_sense_request(rq) || blk_pc_request(rq) ||
  237. rq->cmd_type == REQ_TYPE_ATA_PC)
  238. return rq->data_len;
  239. else
  240. return 0;
  241. }
  242. EXPORT_SYMBOL_GPL(ide_cd_get_xferlen);
  243. void ide_read_bcount_and_ireason(ide_drive_t *drive, u16 *bcount, u8 *ireason)
  244. {
  245. struct ide_taskfile tf;
  246. drive->hwif->tp_ops->tf_read(drive, &tf, IDE_VALID_NSECT |
  247. IDE_VALID_LBAM | IDE_VALID_LBAH);
  248. *bcount = (tf.lbah << 8) | tf.lbam;
  249. *ireason = tf.nsect & 3;
  250. }
  251. EXPORT_SYMBOL_GPL(ide_read_bcount_and_ireason);
  252. /*
  253. * This is the usual interrupt handler which will be called during a packet
  254. * command. We will transfer some of the data (as requested by the drive)
  255. * and will re-point interrupt handler to us.
  256. */
  257. static ide_startstop_t ide_pc_intr(ide_drive_t *drive)
  258. {
  259. struct ide_atapi_pc *pc = drive->pc;
  260. ide_hwif_t *hwif = drive->hwif;
  261. struct ide_cmd *cmd = &hwif->cmd;
  262. struct request *rq = hwif->rq;
  263. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  264. xfer_func_t *xferfunc;
  265. unsigned int timeout, done;
  266. u16 bcount;
  267. u8 stat, ireason, dsc = 0;
  268. u8 write = !!(pc->flags & PC_FLAG_WRITING);
  269. debug_log("Enter %s - interrupt handler\n", __func__);
  270. timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
  271. : WAIT_TAPE_CMD;
  272. /* Clear the interrupt */
  273. stat = tp_ops->read_status(hwif);
  274. if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
  275. int rc;
  276. drive->waiting_for_dma = 0;
  277. rc = hwif->dma_ops->dma_end(drive);
  278. ide_dma_unmap_sg(drive, cmd);
  279. if (rc || (drive->media == ide_tape && (stat & ATA_ERR))) {
  280. if (drive->media == ide_floppy)
  281. printk(KERN_ERR "%s: DMA %s error\n",
  282. drive->name, rq_data_dir(pc->rq)
  283. ? "write" : "read");
  284. pc->flags |= PC_FLAG_DMA_ERROR;
  285. } else {
  286. pc->xferred = pc->req_xfer;
  287. if (drive->pc_update_buffers)
  288. drive->pc_update_buffers(drive, pc);
  289. }
  290. debug_log("%s: DMA finished\n", drive->name);
  291. }
  292. /* No more interrupts */
  293. if ((stat & ATA_DRQ) == 0) {
  294. int uptodate, error;
  295. unsigned int done;
  296. debug_log("Packet command completed, %d bytes transferred\n",
  297. pc->xferred);
  298. pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
  299. local_irq_enable_in_hardirq();
  300. if (drive->media == ide_tape &&
  301. (stat & ATA_ERR) && rq->cmd[0] == REQUEST_SENSE)
  302. stat &= ~ATA_ERR;
  303. if ((stat & ATA_ERR) || (pc->flags & PC_FLAG_DMA_ERROR)) {
  304. /* Error detected */
  305. debug_log("%s: I/O error\n", drive->name);
  306. if (drive->media != ide_tape)
  307. pc->rq->errors++;
  308. if (rq->cmd[0] == REQUEST_SENSE) {
  309. printk(KERN_ERR "%s: I/O error in request sense"
  310. " command\n", drive->name);
  311. return ide_do_reset(drive);
  312. }
  313. debug_log("[cmd %x]: check condition\n", rq->cmd[0]);
  314. /* Retry operation */
  315. ide_retry_pc(drive);
  316. /* queued, but not started */
  317. return ide_stopped;
  318. }
  319. pc->error = 0;
  320. if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) && (stat & ATA_DSC) == 0)
  321. dsc = 1;
  322. /* Command finished - Call the callback function */
  323. uptodate = drive->pc_callback(drive, dsc);
  324. if (uptodate == 0)
  325. drive->failed_pc = NULL;
  326. if (blk_special_request(rq)) {
  327. rq->errors = 0;
  328. done = blk_rq_bytes(rq);
  329. error = 0;
  330. } else {
  331. if (blk_fs_request(rq) == 0 && uptodate <= 0) {
  332. if (rq->errors == 0)
  333. rq->errors = -EIO;
  334. }
  335. if (drive->media == ide_tape)
  336. done = ide_rq_bytes(rq); /* FIXME */
  337. else
  338. done = blk_rq_bytes(rq);
  339. error = uptodate ? 0 : -EIO;
  340. }
  341. ide_complete_rq(drive, error, done);
  342. return ide_stopped;
  343. }
  344. if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
  345. pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
  346. printk(KERN_ERR "%s: The device wants to issue more interrupts "
  347. "in DMA mode\n", drive->name);
  348. ide_dma_off(drive);
  349. return ide_do_reset(drive);
  350. }
  351. /* Get the number of bytes to transfer on this interrupt. */
  352. ide_read_bcount_and_ireason(drive, &bcount, &ireason);
  353. if (ireason & ATAPI_COD) {
  354. printk(KERN_ERR "%s: CoD != 0 in %s\n", drive->name, __func__);
  355. return ide_do_reset(drive);
  356. }
  357. if (((ireason & ATAPI_IO) == ATAPI_IO) == write) {
  358. /* Hopefully, we will never get here */
  359. printk(KERN_ERR "%s: We wanted to %s, but the device wants us "
  360. "to %s!\n", drive->name,
  361. (ireason & ATAPI_IO) ? "Write" : "Read",
  362. (ireason & ATAPI_IO) ? "Read" : "Write");
  363. return ide_do_reset(drive);
  364. }
  365. xferfunc = write ? tp_ops->output_data : tp_ops->input_data;
  366. if (drive->media == ide_floppy && pc->buf == NULL) {
  367. done = min_t(unsigned int, bcount, cmd->nleft);
  368. ide_pio_bytes(drive, cmd, write, done);
  369. } else if (drive->media == ide_tape && pc->bh) {
  370. done = drive->pc_io_buffers(drive, pc, bcount, write);
  371. } else {
  372. done = min_t(unsigned int, bcount, pc->req_xfer - pc->xferred);
  373. xferfunc(drive, NULL, pc->cur_pos, done);
  374. }
  375. /* Update the current position */
  376. pc->xferred += done;
  377. pc->cur_pos += done;
  378. bcount -= done;
  379. if (bcount)
  380. ide_pad_transfer(drive, write, bcount);
  381. debug_log("[cmd %x] transferred %d bytes, padded %d bytes\n",
  382. rq->cmd[0], done, bcount);
  383. /* And set the interrupt handler again */
  384. ide_set_handler(drive, ide_pc_intr, timeout);
  385. return ide_started;
  386. }
  387. static void ide_init_packet_cmd(struct ide_cmd *cmd, u8 valid_tf,
  388. u16 bcount, u8 dma)
  389. {
  390. cmd->protocol = dma ? ATAPI_PROT_DMA : ATAPI_PROT_PIO;
  391. cmd->valid.out.tf = IDE_VALID_LBAH | IDE_VALID_LBAM |
  392. IDE_VALID_FEATURE | valid_tf;
  393. cmd->tf.command = ATA_CMD_PACKET;
  394. cmd->tf.feature = dma; /* Use PIO/DMA */
  395. cmd->tf.lbam = bcount & 0xff;
  396. cmd->tf.lbah = (bcount >> 8) & 0xff;
  397. }
  398. static u8 ide_read_ireason(ide_drive_t *drive)
  399. {
  400. struct ide_taskfile tf;
  401. drive->hwif->tp_ops->tf_read(drive, &tf, IDE_VALID_NSECT);
  402. return tf.nsect & 3;
  403. }
  404. static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
  405. {
  406. int retries = 100;
  407. while (retries-- && ((ireason & ATAPI_COD) == 0 ||
  408. (ireason & ATAPI_IO))) {
  409. printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
  410. "a packet command, retrying\n", drive->name);
  411. udelay(100);
  412. ireason = ide_read_ireason(drive);
  413. if (retries == 0) {
  414. printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
  415. "a packet command, ignoring\n",
  416. drive->name);
  417. ireason |= ATAPI_COD;
  418. ireason &= ~ATAPI_IO;
  419. }
  420. }
  421. return ireason;
  422. }
  423. static int ide_delayed_transfer_pc(ide_drive_t *drive)
  424. {
  425. /* Send the actual packet */
  426. drive->hwif->tp_ops->output_data(drive, NULL, drive->pc->c, 12);
  427. /* Timeout for the packet command */
  428. return WAIT_FLOPPY_CMD;
  429. }
  430. static ide_startstop_t ide_transfer_pc(ide_drive_t *drive)
  431. {
  432. struct ide_atapi_pc *uninitialized_var(pc);
  433. ide_hwif_t *hwif = drive->hwif;
  434. struct request *rq = hwif->rq;
  435. ide_expiry_t *expiry;
  436. unsigned int timeout;
  437. int cmd_len;
  438. ide_startstop_t startstop;
  439. u8 ireason;
  440. if (ide_wait_stat(&startstop, drive, ATA_DRQ, ATA_BUSY, WAIT_READY)) {
  441. printk(KERN_ERR "%s: Strange, packet command initiated yet "
  442. "DRQ isn't asserted\n", drive->name);
  443. return startstop;
  444. }
  445. if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
  446. if (drive->dma)
  447. drive->waiting_for_dma = 1;
  448. }
  449. if (dev_is_idecd(drive)) {
  450. /* ATAPI commands get padded out to 12 bytes minimum */
  451. cmd_len = COMMAND_SIZE(rq->cmd[0]);
  452. if (cmd_len < ATAPI_MIN_CDB_BYTES)
  453. cmd_len = ATAPI_MIN_CDB_BYTES;
  454. timeout = rq->timeout;
  455. expiry = ide_cd_expiry;
  456. } else {
  457. pc = drive->pc;
  458. cmd_len = ATAPI_MIN_CDB_BYTES;
  459. /*
  460. * If necessary schedule the packet transfer to occur 'timeout'
  461. * miliseconds later in ide_delayed_transfer_pc() after the
  462. * device says it's ready for a packet.
  463. */
  464. if (drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) {
  465. timeout = drive->pc_delay;
  466. expiry = &ide_delayed_transfer_pc;
  467. } else {
  468. timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
  469. : WAIT_TAPE_CMD;
  470. expiry = NULL;
  471. }
  472. ireason = ide_read_ireason(drive);
  473. if (drive->media == ide_tape)
  474. ireason = ide_wait_ireason(drive, ireason);
  475. if ((ireason & ATAPI_COD) == 0 || (ireason & ATAPI_IO)) {
  476. printk(KERN_ERR "%s: (IO,CoD) != (0,1) while issuing "
  477. "a packet command\n", drive->name);
  478. return ide_do_reset(drive);
  479. }
  480. }
  481. hwif->expiry = expiry;
  482. /* Set the interrupt routine */
  483. ide_set_handler(drive,
  484. (dev_is_idecd(drive) ? drive->irq_handler
  485. : ide_pc_intr),
  486. timeout);
  487. /* Send the actual packet */
  488. if ((drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) == 0)
  489. hwif->tp_ops->output_data(drive, NULL, rq->cmd, cmd_len);
  490. /* Begin DMA, if necessary */
  491. if (dev_is_idecd(drive)) {
  492. if (drive->dma)
  493. hwif->dma_ops->dma_start(drive);
  494. } else {
  495. if (pc->flags & PC_FLAG_DMA_OK) {
  496. pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
  497. hwif->dma_ops->dma_start(drive);
  498. }
  499. }
  500. return ide_started;
  501. }
  502. ide_startstop_t ide_issue_pc(ide_drive_t *drive, struct ide_cmd *cmd)
  503. {
  504. struct ide_atapi_pc *pc;
  505. ide_hwif_t *hwif = drive->hwif;
  506. ide_expiry_t *expiry = NULL;
  507. struct request *rq = hwif->rq;
  508. unsigned int timeout;
  509. u16 bcount;
  510. u8 valid_tf;
  511. u8 drq_int = !!(drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT);
  512. if (dev_is_idecd(drive)) {
  513. valid_tf = IDE_VALID_NSECT | IDE_VALID_LBAL;
  514. bcount = ide_cd_get_xferlen(rq);
  515. expiry = ide_cd_expiry;
  516. timeout = ATAPI_WAIT_PC;
  517. if (drive->dma)
  518. drive->dma = !ide_dma_prepare(drive, cmd);
  519. } else {
  520. pc = drive->pc;
  521. /* We haven't transferred any data yet */
  522. pc->xferred = 0;
  523. pc->cur_pos = pc->buf;
  524. valid_tf = IDE_VALID_DEVICE;
  525. bcount = ((drive->media == ide_tape) ?
  526. pc->req_xfer :
  527. min(pc->req_xfer, 63 * 1024));
  528. if (pc->flags & PC_FLAG_DMA_ERROR) {
  529. pc->flags &= ~PC_FLAG_DMA_ERROR;
  530. ide_dma_off(drive);
  531. }
  532. if (pc->flags & PC_FLAG_DMA_OK)
  533. drive->dma = !ide_dma_prepare(drive, cmd);
  534. if (!drive->dma)
  535. pc->flags &= ~PC_FLAG_DMA_OK;
  536. timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
  537. : WAIT_TAPE_CMD;
  538. }
  539. ide_init_packet_cmd(cmd, valid_tf, bcount, drive->dma);
  540. (void)do_rw_taskfile(drive, cmd);
  541. if (drq_int) {
  542. if (drive->dma)
  543. drive->waiting_for_dma = 0;
  544. hwif->expiry = expiry;
  545. }
  546. ide_execute_command(drive, cmd, ide_transfer_pc, timeout);
  547. return drq_int ? ide_started : ide_transfer_pc(drive);
  548. }
  549. EXPORT_SYMBOL_GPL(ide_issue_pc);