ide-atapi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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 <scsi/scsi.h>
  9. #ifdef DEBUG
  10. #define debug_log(fmt, args...) \
  11. printk(KERN_INFO "ide: " fmt, ## args)
  12. #else
  13. #define debug_log(fmt, args...) do {} while (0)
  14. #endif
  15. #define ATAPI_MIN_CDB_BYTES 12
  16. static inline int dev_is_idecd(ide_drive_t *drive)
  17. {
  18. return drive->media == ide_cdrom || drive->media == ide_optical;
  19. }
  20. /*
  21. * Check whether we can support a device,
  22. * based on the ATAPI IDENTIFY command results.
  23. */
  24. int ide_check_atapi_device(ide_drive_t *drive, const char *s)
  25. {
  26. u16 *id = drive->id;
  27. u8 gcw[2], protocol, device_type, removable, drq_type, packet_size;
  28. *((u16 *)&gcw) = id[ATA_ID_CONFIG];
  29. protocol = (gcw[1] & 0xC0) >> 6;
  30. device_type = gcw[1] & 0x1F;
  31. removable = (gcw[0] & 0x80) >> 7;
  32. drq_type = (gcw[0] & 0x60) >> 5;
  33. packet_size = gcw[0] & 0x03;
  34. #ifdef CONFIG_PPC
  35. /* kludge for Apple PowerBook internal zip */
  36. if (drive->media == ide_floppy && device_type == 5 &&
  37. !strstr((char *)&id[ATA_ID_PROD], "CD-ROM") &&
  38. strstr((char *)&id[ATA_ID_PROD], "ZIP"))
  39. device_type = 0;
  40. #endif
  41. if (protocol != 2)
  42. printk(KERN_ERR "%s: %s: protocol (0x%02x) is not ATAPI\n",
  43. s, drive->name, protocol);
  44. else if ((drive->media == ide_floppy && device_type != 0) ||
  45. (drive->media == ide_tape && device_type != 1))
  46. printk(KERN_ERR "%s: %s: invalid device type (0x%02x)\n",
  47. s, drive->name, device_type);
  48. else if (removable == 0)
  49. printk(KERN_ERR "%s: %s: the removable flag is not set\n",
  50. s, drive->name);
  51. else if (drive->media == ide_floppy && drq_type == 3)
  52. printk(KERN_ERR "%s: %s: sorry, DRQ type (0x%02x) not "
  53. "supported\n", s, drive->name, drq_type);
  54. else if (packet_size != 0)
  55. printk(KERN_ERR "%s: %s: packet size (0x%02x) is not 12 "
  56. "bytes\n", s, drive->name, packet_size);
  57. else
  58. return 1;
  59. return 0;
  60. }
  61. EXPORT_SYMBOL_GPL(ide_check_atapi_device);
  62. /* PIO data transfer routine using the scatter gather table. */
  63. int ide_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
  64. unsigned int bcount, int write)
  65. {
  66. ide_hwif_t *hwif = drive->hwif;
  67. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  68. xfer_func_t *xf = write ? tp_ops->output_data : tp_ops->input_data;
  69. struct scatterlist *sg = pc->sg;
  70. char *buf;
  71. int count, done = 0;
  72. while (bcount) {
  73. count = min(sg->length - pc->b_count, bcount);
  74. if (PageHighMem(sg_page(sg))) {
  75. unsigned long flags;
  76. local_irq_save(flags);
  77. buf = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
  78. xf(drive, NULL, buf + pc->b_count, count);
  79. kunmap_atomic(buf - sg->offset, KM_IRQ0);
  80. local_irq_restore(flags);
  81. } else {
  82. buf = sg_virt(sg);
  83. xf(drive, NULL, buf + pc->b_count, count);
  84. }
  85. bcount -= count;
  86. pc->b_count += count;
  87. done += count;
  88. if (pc->b_count == sg->length) {
  89. if (!--pc->sg_cnt)
  90. break;
  91. pc->sg = sg = sg_next(sg);
  92. pc->b_count = 0;
  93. }
  94. }
  95. if (bcount) {
  96. printk(KERN_ERR "%s: %d leftover bytes, %s\n", drive->name,
  97. bcount, write ? "padding with zeros"
  98. : "discarding data");
  99. ide_pad_transfer(drive, write, bcount);
  100. }
  101. return done;
  102. }
  103. EXPORT_SYMBOL_GPL(ide_io_buffers);
  104. void ide_init_pc(struct ide_atapi_pc *pc)
  105. {
  106. memset(pc, 0, sizeof(*pc));
  107. pc->buf = pc->pc_buf;
  108. pc->buf_size = IDE_PC_BUFFER_SIZE;
  109. }
  110. EXPORT_SYMBOL_GPL(ide_init_pc);
  111. /*
  112. * Generate a new packet command request in front of the request queue, before
  113. * the current request, so that it will be processed immediately, on the next
  114. * pass through the driver.
  115. */
  116. static void ide_queue_pc_head(ide_drive_t *drive, struct gendisk *disk,
  117. struct ide_atapi_pc *pc, struct request *rq)
  118. {
  119. blk_rq_init(NULL, rq);
  120. rq->cmd_type = REQ_TYPE_SPECIAL;
  121. rq->cmd_flags |= REQ_PREEMPT;
  122. rq->buffer = (char *)pc;
  123. rq->rq_disk = disk;
  124. if (pc->req_xfer) {
  125. rq->data = pc->buf;
  126. rq->data_len = pc->req_xfer;
  127. }
  128. memcpy(rq->cmd, pc->c, 12);
  129. if (drive->media == ide_tape)
  130. rq->cmd[13] = REQ_IDETAPE_PC1;
  131. drive->hwif->rq = NULL;
  132. elv_add_request(drive->queue, rq, ELEVATOR_INSERT_FRONT, 0);
  133. }
  134. /*
  135. * Add a special packet command request to the tail of the request queue,
  136. * and wait for it to be serviced.
  137. */
  138. int ide_queue_pc_tail(ide_drive_t *drive, struct gendisk *disk,
  139. struct ide_atapi_pc *pc)
  140. {
  141. struct request *rq;
  142. int error;
  143. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  144. rq->cmd_type = REQ_TYPE_SPECIAL;
  145. rq->buffer = (char *)pc;
  146. if (pc->req_xfer) {
  147. rq->data = pc->buf;
  148. rq->data_len = pc->req_xfer;
  149. }
  150. memcpy(rq->cmd, pc->c, 12);
  151. if (drive->media == ide_tape)
  152. rq->cmd[13] = REQ_IDETAPE_PC1;
  153. error = blk_execute_rq(drive->queue, disk, rq, 0);
  154. blk_put_request(rq);
  155. return error;
  156. }
  157. EXPORT_SYMBOL_GPL(ide_queue_pc_tail);
  158. int ide_do_test_unit_ready(ide_drive_t *drive, struct gendisk *disk)
  159. {
  160. struct ide_atapi_pc pc;
  161. ide_init_pc(&pc);
  162. pc.c[0] = TEST_UNIT_READY;
  163. return ide_queue_pc_tail(drive, disk, &pc);
  164. }
  165. EXPORT_SYMBOL_GPL(ide_do_test_unit_ready);
  166. int ide_do_start_stop(ide_drive_t *drive, struct gendisk *disk, int start)
  167. {
  168. struct ide_atapi_pc pc;
  169. ide_init_pc(&pc);
  170. pc.c[0] = START_STOP;
  171. pc.c[4] = start;
  172. if (drive->media == ide_tape)
  173. pc.flags |= PC_FLAG_WAIT_FOR_DSC;
  174. return ide_queue_pc_tail(drive, disk, &pc);
  175. }
  176. EXPORT_SYMBOL_GPL(ide_do_start_stop);
  177. int ide_set_media_lock(ide_drive_t *drive, struct gendisk *disk, int on)
  178. {
  179. struct ide_atapi_pc pc;
  180. if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) == 0)
  181. return 0;
  182. ide_init_pc(&pc);
  183. pc.c[0] = ALLOW_MEDIUM_REMOVAL;
  184. pc.c[4] = on;
  185. return ide_queue_pc_tail(drive, disk, &pc);
  186. }
  187. EXPORT_SYMBOL_GPL(ide_set_media_lock);
  188. void ide_create_request_sense_cmd(ide_drive_t *drive, struct ide_atapi_pc *pc)
  189. {
  190. ide_init_pc(pc);
  191. pc->c[0] = REQUEST_SENSE;
  192. if (drive->media == ide_floppy) {
  193. pc->c[4] = 255;
  194. pc->req_xfer = 18;
  195. } else {
  196. pc->c[4] = 20;
  197. pc->req_xfer = 20;
  198. }
  199. }
  200. EXPORT_SYMBOL_GPL(ide_create_request_sense_cmd);
  201. /*
  202. * Called when an error was detected during the last packet command.
  203. * We queue a request sense packet command in the head of the request list.
  204. */
  205. void ide_retry_pc(ide_drive_t *drive, struct gendisk *disk)
  206. {
  207. struct request *rq = &drive->request_sense_rq;
  208. struct ide_atapi_pc *pc = &drive->request_sense_pc;
  209. (void)ide_read_error(drive);
  210. ide_create_request_sense_cmd(drive, pc);
  211. if (drive->media == ide_tape)
  212. set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
  213. ide_queue_pc_head(drive, disk, pc, rq);
  214. }
  215. EXPORT_SYMBOL_GPL(ide_retry_pc);
  216. int ide_cd_expiry(ide_drive_t *drive)
  217. {
  218. struct request *rq = drive->hwif->rq;
  219. unsigned long wait = 0;
  220. debug_log("%s: rq->cmd[0]: 0x%x\n", __func__, rq->cmd[0]);
  221. /*
  222. * Some commands are *slow* and normally take a long time to complete.
  223. * Usually we can use the ATAPI "disconnect" to bypass this, but not all
  224. * commands/drives support that. Let ide_timer_expiry keep polling us
  225. * for these.
  226. */
  227. switch (rq->cmd[0]) {
  228. case GPCMD_BLANK:
  229. case GPCMD_FORMAT_UNIT:
  230. case GPCMD_RESERVE_RZONE_TRACK:
  231. case GPCMD_CLOSE_TRACK:
  232. case GPCMD_FLUSH_CACHE:
  233. wait = ATAPI_WAIT_PC;
  234. break;
  235. default:
  236. if (!(rq->cmd_flags & REQ_QUIET))
  237. printk(KERN_INFO "cmd 0x%x timed out\n",
  238. rq->cmd[0]);
  239. wait = 0;
  240. break;
  241. }
  242. return wait;
  243. }
  244. EXPORT_SYMBOL_GPL(ide_cd_expiry);
  245. int ide_cd_get_xferlen(struct request *rq)
  246. {
  247. if (blk_fs_request(rq))
  248. return 32768;
  249. else if (blk_sense_request(rq) || blk_pc_request(rq) ||
  250. rq->cmd_type == REQ_TYPE_ATA_PC)
  251. return rq->data_len;
  252. else
  253. return 0;
  254. }
  255. EXPORT_SYMBOL_GPL(ide_cd_get_xferlen);
  256. void ide_read_bcount_and_ireason(ide_drive_t *drive, u16 *bcount, u8 *ireason)
  257. {
  258. struct ide_cmd cmd;
  259. memset(&cmd, 0, sizeof(cmd));
  260. cmd.tf_flags = IDE_TFLAG_IN_LBAH | IDE_TFLAG_IN_LBAM |
  261. IDE_TFLAG_IN_NSECT;
  262. drive->hwif->tp_ops->tf_read(drive, &cmd);
  263. *bcount = (cmd.tf.lbah << 8) | cmd.tf.lbam;
  264. *ireason = cmd.tf.nsect & 3;
  265. }
  266. EXPORT_SYMBOL_GPL(ide_read_bcount_and_ireason);
  267. /*
  268. * This is the usual interrupt handler which will be called during a packet
  269. * command. We will transfer some of the data (as requested by the drive)
  270. * and will re-point interrupt handler to us.
  271. */
  272. static ide_startstop_t ide_pc_intr(ide_drive_t *drive)
  273. {
  274. struct ide_atapi_pc *pc = drive->pc;
  275. ide_hwif_t *hwif = drive->hwif;
  276. struct request *rq = hwif->rq;
  277. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  278. xfer_func_t *xferfunc;
  279. unsigned int timeout, temp;
  280. u16 bcount;
  281. u8 stat, ireason, dsc = 0;
  282. debug_log("Enter %s - interrupt handler\n", __func__);
  283. timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
  284. : WAIT_TAPE_CMD;
  285. /* Clear the interrupt */
  286. stat = tp_ops->read_status(hwif);
  287. if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
  288. if (hwif->dma_ops->dma_end(drive) ||
  289. (drive->media == ide_tape && (stat & ATA_ERR))) {
  290. if (drive->media == ide_floppy)
  291. printk(KERN_ERR "%s: DMA %s error\n",
  292. drive->name, rq_data_dir(pc->rq)
  293. ? "write" : "read");
  294. pc->flags |= PC_FLAG_DMA_ERROR;
  295. } else {
  296. pc->xferred = pc->req_xfer;
  297. if (drive->pc_update_buffers)
  298. drive->pc_update_buffers(drive, pc);
  299. }
  300. debug_log("%s: DMA finished\n", drive->name);
  301. }
  302. /* No more interrupts */
  303. if ((stat & ATA_DRQ) == 0) {
  304. int uptodate;
  305. debug_log("Packet command completed, %d bytes transferred\n",
  306. pc->xferred);
  307. pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
  308. local_irq_enable_in_hardirq();
  309. if (drive->media == ide_tape &&
  310. (stat & ATA_ERR) && rq->cmd[0] == REQUEST_SENSE)
  311. stat &= ~ATA_ERR;
  312. if ((stat & ATA_ERR) || (pc->flags & PC_FLAG_DMA_ERROR)) {
  313. /* Error detected */
  314. debug_log("%s: I/O error\n", drive->name);
  315. if (drive->media != ide_tape)
  316. pc->rq->errors++;
  317. if (rq->cmd[0] == REQUEST_SENSE) {
  318. printk(KERN_ERR "%s: I/O error in request sense"
  319. " command\n", drive->name);
  320. return ide_do_reset(drive);
  321. }
  322. debug_log("[cmd %x]: check condition\n", rq->cmd[0]);
  323. /* Retry operation */
  324. ide_retry_pc(drive, rq->rq_disk);
  325. /* queued, but not started */
  326. return ide_stopped;
  327. }
  328. pc->error = 0;
  329. if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) && (stat & ATA_DSC) == 0)
  330. dsc = 1;
  331. /* Command finished - Call the callback function */
  332. uptodate = drive->pc_callback(drive, dsc);
  333. if (uptodate == 0)
  334. drive->failed_pc = NULL;
  335. if (blk_special_request(rq)) {
  336. rq->errors = 0;
  337. ide_complete_rq(drive, 0);
  338. } else {
  339. if (blk_fs_request(rq) == 0 && uptodate <= 0) {
  340. if (rq->errors == 0)
  341. rq->errors = -EIO;
  342. }
  343. ide_end_request(drive, uptodate, 0);
  344. }
  345. return ide_stopped;
  346. }
  347. if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
  348. pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
  349. printk(KERN_ERR "%s: The device wants to issue more interrupts "
  350. "in DMA mode\n", drive->name);
  351. ide_dma_off(drive);
  352. return ide_do_reset(drive);
  353. }
  354. /* Get the number of bytes to transfer on this interrupt. */
  355. ide_read_bcount_and_ireason(drive, &bcount, &ireason);
  356. if (ireason & ATAPI_COD) {
  357. printk(KERN_ERR "%s: CoD != 0 in %s\n", drive->name, __func__);
  358. return ide_do_reset(drive);
  359. }
  360. if (((ireason & ATAPI_IO) == ATAPI_IO) ==
  361. !!(pc->flags & PC_FLAG_WRITING)) {
  362. /* Hopefully, we will never get here */
  363. printk(KERN_ERR "%s: We wanted to %s, but the device wants us "
  364. "to %s!\n", drive->name,
  365. (ireason & ATAPI_IO) ? "Write" : "Read",
  366. (ireason & ATAPI_IO) ? "Read" : "Write");
  367. return ide_do_reset(drive);
  368. }
  369. if (!(pc->flags & PC_FLAG_WRITING)) {
  370. /* Reading - Check that we have enough space */
  371. temp = pc->xferred + bcount;
  372. if (temp > pc->req_xfer) {
  373. if (temp > pc->buf_size) {
  374. printk(KERN_ERR "%s: The device wants to send "
  375. "us more data than expected - "
  376. "discarding data\n",
  377. drive->name);
  378. ide_pad_transfer(drive, 0, bcount);
  379. goto next_irq;
  380. }
  381. debug_log("The device wants to send us more data than "
  382. "expected - allowing transfer\n");
  383. }
  384. xferfunc = tp_ops->input_data;
  385. } else
  386. xferfunc = tp_ops->output_data;
  387. if ((drive->media == ide_floppy && !pc->buf) ||
  388. (drive->media == ide_tape && pc->bh)) {
  389. int done = drive->pc_io_buffers(drive, pc, bcount,
  390. !!(pc->flags & PC_FLAG_WRITING));
  391. /* FIXME: don't do partial completions */
  392. if (drive->media == ide_floppy)
  393. ide_end_request(drive, 1, done >> 9);
  394. } else
  395. xferfunc(drive, NULL, pc->cur_pos, bcount);
  396. /* Update the current position */
  397. pc->xferred += bcount;
  398. pc->cur_pos += bcount;
  399. debug_log("[cmd %x] transferred %d bytes on that intr.\n",
  400. rq->cmd[0], bcount);
  401. next_irq:
  402. /* And set the interrupt handler again */
  403. ide_set_handler(drive, ide_pc_intr, timeout, NULL);
  404. return ide_started;
  405. }
  406. static void ide_pktcmd_tf_load(ide_drive_t *drive, u32 tf_flags, u16 bcount)
  407. {
  408. ide_hwif_t *hwif = drive->hwif;
  409. struct ide_cmd cmd;
  410. u8 dma = drive->dma;
  411. memset(&cmd, 0, sizeof(cmd));
  412. cmd.tf_flags = IDE_TFLAG_OUT_LBAH | IDE_TFLAG_OUT_LBAM |
  413. IDE_TFLAG_OUT_FEATURE | tf_flags;
  414. cmd.tf.feature = dma; /* Use PIO/DMA */
  415. cmd.tf.lbam = bcount & 0xff;
  416. cmd.tf.lbah = (bcount >> 8) & 0xff;
  417. ide_tf_dump(drive->name, &cmd.tf);
  418. hwif->tp_ops->set_irq(hwif, 1);
  419. SELECT_MASK(drive, 0);
  420. hwif->tp_ops->tf_load(drive, &cmd);
  421. }
  422. static u8 ide_read_ireason(ide_drive_t *drive)
  423. {
  424. struct ide_cmd cmd;
  425. memset(&cmd, 0, sizeof(cmd));
  426. cmd.tf_flags = IDE_TFLAG_IN_NSECT;
  427. drive->hwif->tp_ops->tf_read(drive, &cmd);
  428. return cmd.tf.nsect & 3;
  429. }
  430. static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
  431. {
  432. int retries = 100;
  433. while (retries-- && ((ireason & ATAPI_COD) == 0 ||
  434. (ireason & ATAPI_IO))) {
  435. printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
  436. "a packet command, retrying\n", drive->name);
  437. udelay(100);
  438. ireason = ide_read_ireason(drive);
  439. if (retries == 0) {
  440. printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
  441. "a packet command, ignoring\n",
  442. drive->name);
  443. ireason |= ATAPI_COD;
  444. ireason &= ~ATAPI_IO;
  445. }
  446. }
  447. return ireason;
  448. }
  449. static int ide_delayed_transfer_pc(ide_drive_t *drive)
  450. {
  451. /* Send the actual packet */
  452. drive->hwif->tp_ops->output_data(drive, NULL, drive->pc->c, 12);
  453. /* Timeout for the packet command */
  454. return WAIT_FLOPPY_CMD;
  455. }
  456. static ide_startstop_t ide_transfer_pc(ide_drive_t *drive)
  457. {
  458. struct ide_atapi_pc *uninitialized_var(pc);
  459. ide_hwif_t *hwif = drive->hwif;
  460. struct request *rq = hwif->rq;
  461. ide_expiry_t *expiry;
  462. unsigned int timeout;
  463. int cmd_len;
  464. ide_startstop_t startstop;
  465. u8 ireason;
  466. if (ide_wait_stat(&startstop, drive, ATA_DRQ, ATA_BUSY, WAIT_READY)) {
  467. printk(KERN_ERR "%s: Strange, packet command initiated yet "
  468. "DRQ isn't asserted\n", drive->name);
  469. return startstop;
  470. }
  471. if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
  472. if (drive->dma)
  473. drive->waiting_for_dma = 1;
  474. }
  475. if (dev_is_idecd(drive)) {
  476. /* ATAPI commands get padded out to 12 bytes minimum */
  477. cmd_len = COMMAND_SIZE(rq->cmd[0]);
  478. if (cmd_len < ATAPI_MIN_CDB_BYTES)
  479. cmd_len = ATAPI_MIN_CDB_BYTES;
  480. timeout = rq->timeout;
  481. expiry = ide_cd_expiry;
  482. } else {
  483. pc = drive->pc;
  484. cmd_len = ATAPI_MIN_CDB_BYTES;
  485. /*
  486. * If necessary schedule the packet transfer to occur 'timeout'
  487. * miliseconds later in ide_delayed_transfer_pc() after the
  488. * device says it's ready for a packet.
  489. */
  490. if (drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) {
  491. timeout = drive->pc_delay;
  492. expiry = &ide_delayed_transfer_pc;
  493. } else {
  494. timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
  495. : WAIT_TAPE_CMD;
  496. expiry = NULL;
  497. }
  498. ireason = ide_read_ireason(drive);
  499. if (drive->media == ide_tape)
  500. ireason = ide_wait_ireason(drive, ireason);
  501. if ((ireason & ATAPI_COD) == 0 || (ireason & ATAPI_IO)) {
  502. printk(KERN_ERR "%s: (IO,CoD) != (0,1) while issuing "
  503. "a packet command\n", drive->name);
  504. return ide_do_reset(drive);
  505. }
  506. }
  507. /* Set the interrupt routine */
  508. ide_set_handler(drive,
  509. (dev_is_idecd(drive) ? drive->irq_handler
  510. : ide_pc_intr),
  511. timeout, expiry);
  512. /* Begin DMA, if necessary */
  513. if (dev_is_idecd(drive)) {
  514. if (drive->dma)
  515. hwif->dma_ops->dma_start(drive);
  516. } else {
  517. if (pc->flags & PC_FLAG_DMA_OK) {
  518. pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
  519. hwif->dma_ops->dma_start(drive);
  520. }
  521. }
  522. /* Send the actual packet */
  523. if ((drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) == 0)
  524. hwif->tp_ops->output_data(drive, NULL, rq->cmd, cmd_len);
  525. return ide_started;
  526. }
  527. ide_startstop_t ide_issue_pc(ide_drive_t *drive)
  528. {
  529. struct ide_atapi_pc *pc;
  530. ide_hwif_t *hwif = drive->hwif;
  531. ide_expiry_t *expiry = NULL;
  532. struct request *rq = hwif->rq;
  533. unsigned int timeout;
  534. u32 tf_flags;
  535. u16 bcount;
  536. if (dev_is_idecd(drive)) {
  537. tf_flags = IDE_TFLAG_OUT_NSECT | IDE_TFLAG_OUT_LBAL;
  538. bcount = ide_cd_get_xferlen(rq);
  539. expiry = ide_cd_expiry;
  540. timeout = ATAPI_WAIT_PC;
  541. if (drive->dma) {
  542. if (ide_build_sglist(drive, rq))
  543. drive->dma = !hwif->dma_ops->dma_setup(drive);
  544. else
  545. drive->dma = 0;
  546. }
  547. } else {
  548. pc = drive->pc;
  549. /* We haven't transferred any data yet */
  550. pc->xferred = 0;
  551. pc->cur_pos = pc->buf;
  552. tf_flags = IDE_TFLAG_OUT_DEVICE;
  553. bcount = ((drive->media == ide_tape) ?
  554. pc->req_xfer :
  555. min(pc->req_xfer, 63 * 1024));
  556. if (pc->flags & PC_FLAG_DMA_ERROR) {
  557. pc->flags &= ~PC_FLAG_DMA_ERROR;
  558. ide_dma_off(drive);
  559. }
  560. if ((pc->flags & PC_FLAG_DMA_OK) &&
  561. (drive->dev_flags & IDE_DFLAG_USING_DMA)) {
  562. if (ide_build_sglist(drive, rq))
  563. drive->dma = !hwif->dma_ops->dma_setup(drive);
  564. else
  565. drive->dma = 0;
  566. }
  567. if (!drive->dma)
  568. pc->flags &= ~PC_FLAG_DMA_OK;
  569. timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
  570. : WAIT_TAPE_CMD;
  571. }
  572. ide_pktcmd_tf_load(drive, tf_flags, bcount);
  573. /* Issue the packet command */
  574. if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
  575. if (drive->dma)
  576. drive->waiting_for_dma = 0;
  577. ide_execute_command(drive, ATA_CMD_PACKET, ide_transfer_pc,
  578. timeout, expiry);
  579. return ide_started;
  580. } else {
  581. ide_execute_pkt_cmd(drive);
  582. return ide_transfer_pc(drive);
  583. }
  584. }
  585. EXPORT_SYMBOL_GPL(ide_issue_pc);