ide-atapi.c 18 KB

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