ide-atapi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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 (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 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,
  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 *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. if (blk_fs_request(rq))
  260. return 32768;
  261. else if (blk_sense_request(rq) || blk_pc_request(rq) ||
  262. rq->cmd_type == REQ_TYPE_ATA_PC)
  263. return blk_rq_bytes(rq);
  264. else
  265. return 0;
  266. }
  267. EXPORT_SYMBOL_GPL(ide_cd_get_xferlen);
  268. void ide_read_bcount_and_ireason(ide_drive_t *drive, u16 *bcount, u8 *ireason)
  269. {
  270. struct ide_taskfile tf;
  271. drive->hwif->tp_ops->tf_read(drive, &tf, IDE_VALID_NSECT |
  272. IDE_VALID_LBAM | IDE_VALID_LBAH);
  273. *bcount = (tf.lbah << 8) | tf.lbam;
  274. *ireason = tf.nsect & 3;
  275. }
  276. EXPORT_SYMBOL_GPL(ide_read_bcount_and_ireason);
  277. /*
  278. * Check the contents of the interrupt reason register and attempt to recover if
  279. * there are problems.
  280. *
  281. * Returns:
  282. * - 0 if everything's ok
  283. * - 1 if the request has to be terminated.
  284. */
  285. int ide_check_ireason(ide_drive_t *drive, struct request *rq, int len,
  286. int ireason, int rw)
  287. {
  288. ide_hwif_t *hwif = drive->hwif;
  289. debug_log("ireason: 0x%x, rw: 0x%x\n", ireason, rw);
  290. if (ireason == (!rw << 1))
  291. return 0;
  292. else if (ireason == (rw << 1)) {
  293. printk(KERN_ERR PFX "%s: %s: wrong transfer direction!\n",
  294. drive->name, __func__);
  295. if (dev_is_idecd(drive))
  296. ide_pad_transfer(drive, rw, len);
  297. } else if (!rw && ireason == ATAPI_COD) {
  298. if (dev_is_idecd(drive)) {
  299. /*
  300. * Some drives (ASUS) seem to tell us that status info
  301. * is available. Just get it and ignore.
  302. */
  303. (void)hwif->tp_ops->read_status(hwif);
  304. return 0;
  305. }
  306. } else {
  307. if (ireason & ATAPI_COD)
  308. printk(KERN_ERR PFX "%s: CoD != 0 in %s\n", drive->name,
  309. __func__);
  310. /* drive wants a command packet, or invalid ireason... */
  311. printk(KERN_ERR PFX "%s: %s: bad interrupt reason 0x%02x\n",
  312. drive->name, __func__, ireason);
  313. }
  314. if (dev_is_idecd(drive) && rq->cmd_type == REQ_TYPE_ATA_PC)
  315. rq->cmd_flags |= REQ_FAILED;
  316. return 1;
  317. }
  318. EXPORT_SYMBOL_GPL(ide_check_ireason);
  319. /*
  320. * This is the usual interrupt handler which will be called during a packet
  321. * command. We will transfer some of the data (as requested by the drive)
  322. * and will re-point interrupt handler to us.
  323. */
  324. static ide_startstop_t ide_pc_intr(ide_drive_t *drive)
  325. {
  326. struct ide_atapi_pc *pc = drive->pc;
  327. ide_hwif_t *hwif = drive->hwif;
  328. struct ide_cmd *cmd = &hwif->cmd;
  329. struct request *rq = hwif->rq;
  330. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  331. unsigned int timeout, done;
  332. u16 bcount;
  333. u8 stat, ireason, dsc = 0;
  334. u8 write = !!(pc->flags & PC_FLAG_WRITING);
  335. debug_log("Enter %s - interrupt handler\n", __func__);
  336. timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
  337. : WAIT_TAPE_CMD;
  338. /* Clear the interrupt */
  339. stat = tp_ops->read_status(hwif);
  340. if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
  341. int rc;
  342. drive->waiting_for_dma = 0;
  343. rc = hwif->dma_ops->dma_end(drive);
  344. ide_dma_unmap_sg(drive, cmd);
  345. if (rc || (drive->media == ide_tape && (stat & ATA_ERR))) {
  346. if (drive->media == ide_floppy)
  347. printk(KERN_ERR PFX "%s: DMA %s error\n",
  348. drive->name, rq_data_dir(pc->rq)
  349. ? "write" : "read");
  350. pc->flags |= PC_FLAG_DMA_ERROR;
  351. } else
  352. rq->resid_len = 0;
  353. debug_log("%s: DMA finished\n", drive->name);
  354. }
  355. /* No more interrupts */
  356. if ((stat & ATA_DRQ) == 0) {
  357. int uptodate, error;
  358. debug_log("Packet command completed, %d bytes transferred\n",
  359. blk_rq_bytes(rq));
  360. pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
  361. local_irq_enable_in_hardirq();
  362. if (drive->media == ide_tape &&
  363. (stat & ATA_ERR) && rq->cmd[0] == REQUEST_SENSE)
  364. stat &= ~ATA_ERR;
  365. if ((stat & ATA_ERR) || (pc->flags & PC_FLAG_DMA_ERROR)) {
  366. /* Error detected */
  367. debug_log("%s: I/O error\n", drive->name);
  368. if (drive->media != ide_tape)
  369. pc->rq->errors++;
  370. if (rq->cmd[0] == REQUEST_SENSE) {
  371. printk(KERN_ERR PFX "%s: I/O error in request "
  372. "sense command\n", drive->name);
  373. return ide_do_reset(drive);
  374. }
  375. debug_log("[cmd %x]: check condition\n", rq->cmd[0]);
  376. /* Retry operation */
  377. ide_retry_pc(drive);
  378. /* queued, but not started */
  379. return ide_stopped;
  380. }
  381. pc->error = 0;
  382. if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) && (stat & ATA_DSC) == 0)
  383. dsc = 1;
  384. /*
  385. * ->pc_callback() might change rq->data_len for
  386. * residual count, cache total length.
  387. */
  388. done = blk_rq_bytes(rq);
  389. /* Command finished - Call the callback function */
  390. uptodate = drive->pc_callback(drive, dsc);
  391. if (uptodate == 0)
  392. drive->failed_pc = NULL;
  393. if (blk_special_request(rq)) {
  394. rq->errors = 0;
  395. error = 0;
  396. } else {
  397. if (blk_fs_request(rq) == 0 && uptodate <= 0) {
  398. if (rq->errors == 0)
  399. rq->errors = -EIO;
  400. }
  401. error = uptodate ? 0 : -EIO;
  402. }
  403. ide_complete_rq(drive, error, blk_rq_bytes(rq));
  404. return ide_stopped;
  405. }
  406. if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
  407. pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
  408. printk(KERN_ERR PFX "%s: The device wants to issue more "
  409. "interrupts in DMA mode\n", drive->name);
  410. ide_dma_off(drive);
  411. return ide_do_reset(drive);
  412. }
  413. /* Get the number of bytes to transfer on this interrupt. */
  414. ide_read_bcount_and_ireason(drive, &bcount, &ireason);
  415. if (ide_check_ireason(drive, rq, bcount, ireason, write))
  416. return ide_do_reset(drive);
  417. done = min_t(unsigned int, bcount, cmd->nleft);
  418. ide_pio_bytes(drive, cmd, write, done);
  419. /* Update transferred byte count */
  420. rq->resid_len -= done;
  421. bcount -= done;
  422. if (bcount)
  423. ide_pad_transfer(drive, write, bcount);
  424. debug_log("[cmd %x] transferred %d bytes, padded %d bytes, resid: %u\n",
  425. rq->cmd[0], done, bcount, rq->resid_len);
  426. /* And set the interrupt handler again */
  427. ide_set_handler(drive, ide_pc_intr, timeout);
  428. return ide_started;
  429. }
  430. static void ide_init_packet_cmd(struct ide_cmd *cmd, u8 valid_tf,
  431. u16 bcount, u8 dma)
  432. {
  433. cmd->protocol = dma ? ATAPI_PROT_DMA : ATAPI_PROT_PIO;
  434. cmd->valid.out.tf = IDE_VALID_LBAH | IDE_VALID_LBAM |
  435. IDE_VALID_FEATURE | valid_tf;
  436. cmd->tf.command = ATA_CMD_PACKET;
  437. cmd->tf.feature = dma; /* Use PIO/DMA */
  438. cmd->tf.lbam = bcount & 0xff;
  439. cmd->tf.lbah = (bcount >> 8) & 0xff;
  440. }
  441. static u8 ide_read_ireason(ide_drive_t *drive)
  442. {
  443. struct ide_taskfile tf;
  444. drive->hwif->tp_ops->tf_read(drive, &tf, IDE_VALID_NSECT);
  445. return tf.nsect & 3;
  446. }
  447. static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
  448. {
  449. int retries = 100;
  450. while (retries-- && ((ireason & ATAPI_COD) == 0 ||
  451. (ireason & ATAPI_IO))) {
  452. printk(KERN_ERR PFX "%s: (IO,CoD != (0,1) while issuing "
  453. "a packet command, retrying\n", drive->name);
  454. udelay(100);
  455. ireason = ide_read_ireason(drive);
  456. if (retries == 0) {
  457. printk(KERN_ERR PFX "%s: (IO,CoD != (0,1) while issuing"
  458. " a packet command, ignoring\n",
  459. drive->name);
  460. ireason |= ATAPI_COD;
  461. ireason &= ~ATAPI_IO;
  462. }
  463. }
  464. return ireason;
  465. }
  466. static int ide_delayed_transfer_pc(ide_drive_t *drive)
  467. {
  468. /* Send the actual packet */
  469. drive->hwif->tp_ops->output_data(drive, NULL, drive->pc->c, 12);
  470. /* Timeout for the packet command */
  471. return WAIT_FLOPPY_CMD;
  472. }
  473. static ide_startstop_t ide_transfer_pc(ide_drive_t *drive)
  474. {
  475. struct ide_atapi_pc *uninitialized_var(pc);
  476. ide_hwif_t *hwif = drive->hwif;
  477. struct request *rq = hwif->rq;
  478. ide_expiry_t *expiry;
  479. unsigned int timeout;
  480. int cmd_len;
  481. ide_startstop_t startstop;
  482. u8 ireason;
  483. if (ide_wait_stat(&startstop, drive, ATA_DRQ, ATA_BUSY, WAIT_READY)) {
  484. printk(KERN_ERR PFX "%s: Strange, packet command initiated yet "
  485. "DRQ isn't asserted\n", drive->name);
  486. return startstop;
  487. }
  488. if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
  489. if (drive->dma)
  490. drive->waiting_for_dma = 1;
  491. }
  492. if (dev_is_idecd(drive)) {
  493. /* ATAPI commands get padded out to 12 bytes minimum */
  494. cmd_len = COMMAND_SIZE(rq->cmd[0]);
  495. if (cmd_len < ATAPI_MIN_CDB_BYTES)
  496. cmd_len = ATAPI_MIN_CDB_BYTES;
  497. timeout = rq->timeout;
  498. expiry = ide_cd_expiry;
  499. } else {
  500. pc = drive->pc;
  501. cmd_len = ATAPI_MIN_CDB_BYTES;
  502. /*
  503. * If necessary schedule the packet transfer to occur 'timeout'
  504. * milliseconds later in ide_delayed_transfer_pc() after the
  505. * device says it's ready for a packet.
  506. */
  507. if (drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) {
  508. timeout = drive->pc_delay;
  509. expiry = &ide_delayed_transfer_pc;
  510. } else {
  511. timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
  512. : WAIT_TAPE_CMD;
  513. expiry = NULL;
  514. }
  515. ireason = ide_read_ireason(drive);
  516. if (drive->media == ide_tape)
  517. ireason = ide_wait_ireason(drive, ireason);
  518. if ((ireason & ATAPI_COD) == 0 || (ireason & ATAPI_IO)) {
  519. printk(KERN_ERR PFX "%s: (IO,CoD) != (0,1) while "
  520. "issuing a packet command\n", drive->name);
  521. return ide_do_reset(drive);
  522. }
  523. }
  524. hwif->expiry = expiry;
  525. /* Set the interrupt routine */
  526. ide_set_handler(drive,
  527. (dev_is_idecd(drive) ? drive->irq_handler
  528. : ide_pc_intr),
  529. timeout);
  530. /* Send the actual packet */
  531. if ((drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) == 0)
  532. hwif->tp_ops->output_data(drive, NULL, rq->cmd, cmd_len);
  533. /* Begin DMA, if necessary */
  534. if (dev_is_idecd(drive)) {
  535. if (drive->dma)
  536. hwif->dma_ops->dma_start(drive);
  537. } else {
  538. if (pc->flags & PC_FLAG_DMA_OK) {
  539. pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
  540. hwif->dma_ops->dma_start(drive);
  541. }
  542. }
  543. return ide_started;
  544. }
  545. ide_startstop_t ide_issue_pc(ide_drive_t *drive, struct ide_cmd *cmd)
  546. {
  547. struct ide_atapi_pc *pc;
  548. ide_hwif_t *hwif = drive->hwif;
  549. ide_expiry_t *expiry = NULL;
  550. struct request *rq = hwif->rq;
  551. unsigned int timeout, bytes;
  552. u16 bcount;
  553. u8 valid_tf;
  554. u8 drq_int = !!(drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT);
  555. if (dev_is_idecd(drive)) {
  556. valid_tf = IDE_VALID_NSECT | IDE_VALID_LBAL;
  557. bcount = ide_cd_get_xferlen(rq);
  558. expiry = ide_cd_expiry;
  559. timeout = ATAPI_WAIT_PC;
  560. if (drive->dma)
  561. drive->dma = !ide_dma_prepare(drive, cmd);
  562. } else {
  563. pc = drive->pc;
  564. valid_tf = IDE_VALID_DEVICE;
  565. bytes = blk_rq_bytes(rq);
  566. bcount = ((drive->media == ide_tape) ? bytes
  567. : min_t(unsigned int,
  568. bytes, 63 * 1024));
  569. /* We haven't transferred any data yet */
  570. rq->resid_len = bcount;
  571. if (pc->flags & PC_FLAG_DMA_ERROR) {
  572. pc->flags &= ~PC_FLAG_DMA_ERROR;
  573. ide_dma_off(drive);
  574. }
  575. if (pc->flags & PC_FLAG_DMA_OK)
  576. drive->dma = !ide_dma_prepare(drive, cmd);
  577. if (!drive->dma)
  578. pc->flags &= ~PC_FLAG_DMA_OK;
  579. timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
  580. : WAIT_TAPE_CMD;
  581. }
  582. ide_init_packet_cmd(cmd, valid_tf, bcount, drive->dma);
  583. (void)do_rw_taskfile(drive, cmd);
  584. if (drq_int) {
  585. if (drive->dma)
  586. drive->waiting_for_dma = 0;
  587. hwif->expiry = expiry;
  588. }
  589. ide_execute_command(drive, cmd, ide_transfer_pc, timeout);
  590. return drq_int ? ide_started : ide_transfer_pc(drive);
  591. }
  592. EXPORT_SYMBOL_GPL(ide_issue_pc);