ide-atapi.c 18 KB

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