ide-atapi.c 16 KB

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