ide-atapi.c 16 KB

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