ide-atapi.c 18 KB

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