ide-atapi.c 18 KB

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