ide-atapi.c 19 KB

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