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