ide-atapi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. * ATAPI support.
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/delay.h>
  6. #include <linux/ide.h>
  7. #include <scsi/scsi.h>
  8. #ifdef DEBUG
  9. #define debug_log(fmt, args...) \
  10. printk(KERN_INFO "ide: " fmt, ## args)
  11. #else
  12. #define debug_log(fmt, args...) do {} while (0)
  13. #endif
  14. /*
  15. * Check whether we can support a device,
  16. * based on the ATAPI IDENTIFY command results.
  17. */
  18. int ide_check_atapi_device(ide_drive_t *drive, const char *s)
  19. {
  20. u16 *id = drive->id;
  21. u8 gcw[2], protocol, device_type, removable, drq_type, packet_size;
  22. *((u16 *)&gcw) = id[ATA_ID_CONFIG];
  23. protocol = (gcw[1] & 0xC0) >> 6;
  24. device_type = gcw[1] & 0x1F;
  25. removable = (gcw[0] & 0x80) >> 7;
  26. drq_type = (gcw[0] & 0x60) >> 5;
  27. packet_size = gcw[0] & 0x03;
  28. #ifdef CONFIG_PPC
  29. /* kludge for Apple PowerBook internal zip */
  30. if (drive->media == ide_floppy && device_type == 5 &&
  31. !strstr((char *)&id[ATA_ID_PROD], "CD-ROM") &&
  32. strstr((char *)&id[ATA_ID_PROD], "ZIP"))
  33. device_type = 0;
  34. #endif
  35. if (protocol != 2)
  36. printk(KERN_ERR "%s: %s: protocol (0x%02x) is not ATAPI\n",
  37. s, drive->name, protocol);
  38. else if ((drive->media == ide_floppy && device_type != 0) ||
  39. (drive->media == ide_tape && device_type != 1))
  40. printk(KERN_ERR "%s: %s: invalid device type (0x%02x)\n",
  41. s, drive->name, device_type);
  42. else if (removable == 0)
  43. printk(KERN_ERR "%s: %s: the removable flag is not set\n",
  44. s, drive->name);
  45. else if (drive->media == ide_floppy && drq_type == 3)
  46. printk(KERN_ERR "%s: %s: sorry, DRQ type (0x%02x) not "
  47. "supported\n", s, drive->name, drq_type);
  48. else if (packet_size != 0)
  49. printk(KERN_ERR "%s: %s: packet size (0x%02x) is not 12 "
  50. "bytes\n", s, drive->name, packet_size);
  51. else
  52. return 1;
  53. return 0;
  54. }
  55. EXPORT_SYMBOL_GPL(ide_check_atapi_device);
  56. /* PIO data transfer routine using the scatter gather table. */
  57. int ide_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
  58. unsigned int bcount, int write)
  59. {
  60. ide_hwif_t *hwif = drive->hwif;
  61. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  62. xfer_func_t *xf = write ? tp_ops->output_data : tp_ops->input_data;
  63. struct scatterlist *sg = pc->sg;
  64. char *buf;
  65. int count, done = 0;
  66. while (bcount) {
  67. count = min(sg->length - pc->b_count, bcount);
  68. if (PageHighMem(sg_page(sg))) {
  69. unsigned long flags;
  70. local_irq_save(flags);
  71. buf = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
  72. xf(drive, NULL, buf + pc->b_count, count);
  73. kunmap_atomic(buf - sg->offset, KM_IRQ0);
  74. local_irq_restore(flags);
  75. } else {
  76. buf = sg_virt(sg);
  77. xf(drive, NULL, buf + pc->b_count, count);
  78. }
  79. bcount -= count;
  80. pc->b_count += count;
  81. done += count;
  82. if (pc->b_count == sg->length) {
  83. if (!--pc->sg_cnt)
  84. break;
  85. pc->sg = sg = sg_next(sg);
  86. pc->b_count = 0;
  87. }
  88. }
  89. if (bcount) {
  90. printk(KERN_ERR "%s: %d leftover bytes, %s\n", drive->name,
  91. bcount, write ? "padding with zeros"
  92. : "discarding data");
  93. ide_pad_transfer(drive, write, bcount);
  94. }
  95. return done;
  96. }
  97. EXPORT_SYMBOL_GPL(ide_io_buffers);
  98. void ide_init_pc(struct ide_atapi_pc *pc)
  99. {
  100. memset(pc, 0, sizeof(*pc));
  101. pc->buf = pc->pc_buf;
  102. pc->buf_size = IDE_PC_BUFFER_SIZE;
  103. }
  104. EXPORT_SYMBOL_GPL(ide_init_pc);
  105. /*
  106. * Generate a new packet command request in front of the request queue, before
  107. * the current request, so that it will be processed immediately, on the next
  108. * pass through the driver.
  109. */
  110. static void ide_queue_pc_head(ide_drive_t *drive, struct gendisk *disk,
  111. struct ide_atapi_pc *pc, struct request *rq)
  112. {
  113. blk_rq_init(NULL, rq);
  114. rq->cmd_type = REQ_TYPE_SPECIAL;
  115. rq->cmd_flags |= REQ_PREEMPT;
  116. rq->buffer = (char *)pc;
  117. rq->rq_disk = disk;
  118. memcpy(rq->cmd, pc->c, 12);
  119. if (drive->media == ide_tape)
  120. rq->cmd[13] = REQ_IDETAPE_PC1;
  121. ide_do_drive_cmd(drive, rq);
  122. }
  123. /*
  124. * Add a special packet command request to the tail of the request queue,
  125. * and wait for it to be serviced.
  126. */
  127. int ide_queue_pc_tail(ide_drive_t *drive, struct gendisk *disk,
  128. struct ide_atapi_pc *pc)
  129. {
  130. struct request *rq;
  131. int error;
  132. rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
  133. rq->cmd_type = REQ_TYPE_SPECIAL;
  134. rq->buffer = (char *)pc;
  135. memcpy(rq->cmd, pc->c, 12);
  136. if (drive->media == ide_tape)
  137. rq->cmd[13] = REQ_IDETAPE_PC1;
  138. error = blk_execute_rq(drive->queue, disk, rq, 0);
  139. blk_put_request(rq);
  140. return error;
  141. }
  142. EXPORT_SYMBOL_GPL(ide_queue_pc_tail);
  143. int ide_do_test_unit_ready(ide_drive_t *drive, struct gendisk *disk)
  144. {
  145. struct ide_atapi_pc pc;
  146. ide_init_pc(&pc);
  147. pc.c[0] = TEST_UNIT_READY;
  148. return ide_queue_pc_tail(drive, disk, &pc);
  149. }
  150. EXPORT_SYMBOL_GPL(ide_do_test_unit_ready);
  151. int ide_do_start_stop(ide_drive_t *drive, struct gendisk *disk, int start)
  152. {
  153. struct ide_atapi_pc pc;
  154. ide_init_pc(&pc);
  155. pc.c[0] = START_STOP;
  156. pc.c[4] = start;
  157. if (drive->media == ide_tape)
  158. pc.flags |= PC_FLAG_WAIT_FOR_DSC;
  159. return ide_queue_pc_tail(drive, disk, &pc);
  160. }
  161. EXPORT_SYMBOL_GPL(ide_do_start_stop);
  162. int ide_set_media_lock(ide_drive_t *drive, struct gendisk *disk, int on)
  163. {
  164. struct ide_atapi_pc pc;
  165. if (drive->atapi_flags & IDE_AFLAG_NO_DOORLOCK)
  166. return 0;
  167. ide_init_pc(&pc);
  168. pc.c[0] = ALLOW_MEDIUM_REMOVAL;
  169. pc.c[4] = on;
  170. return ide_queue_pc_tail(drive, disk, &pc);
  171. }
  172. EXPORT_SYMBOL_GPL(ide_set_media_lock);
  173. void ide_create_request_sense_cmd(ide_drive_t *drive, struct ide_atapi_pc *pc)
  174. {
  175. ide_init_pc(pc);
  176. pc->c[0] = REQUEST_SENSE;
  177. if (drive->media == ide_floppy) {
  178. pc->c[4] = 255;
  179. pc->req_xfer = 18;
  180. } else {
  181. pc->c[4] = 20;
  182. pc->req_xfer = 20;
  183. }
  184. }
  185. EXPORT_SYMBOL_GPL(ide_create_request_sense_cmd);
  186. /*
  187. * Called when an error was detected during the last packet command.
  188. * We queue a request sense packet command in the head of the request list.
  189. */
  190. void ide_retry_pc(ide_drive_t *drive, struct gendisk *disk)
  191. {
  192. struct request *rq = &drive->request_sense_rq;
  193. struct ide_atapi_pc *pc = &drive->request_sense_pc;
  194. (void)ide_read_error(drive);
  195. ide_create_request_sense_cmd(drive, pc);
  196. if (drive->media == ide_tape)
  197. set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
  198. ide_queue_pc_head(drive, disk, pc, rq);
  199. }
  200. EXPORT_SYMBOL_GPL(ide_retry_pc);
  201. int ide_scsi_expiry(ide_drive_t *drive)
  202. {
  203. struct ide_atapi_pc *pc = drive->pc;
  204. debug_log("%s called for %lu at %lu\n", __func__,
  205. pc->scsi_cmd->serial_number, jiffies);
  206. pc->flags |= PC_FLAG_TIMEDOUT;
  207. return 0; /* we do not want the IDE subsystem to retry */
  208. }
  209. EXPORT_SYMBOL_GPL(ide_scsi_expiry);
  210. /*
  211. * This is the usual interrupt handler which will be called during a packet
  212. * command. We will transfer some of the data (as requested by the drive)
  213. * and will re-point interrupt handler to us.
  214. */
  215. static ide_startstop_t ide_pc_intr(ide_drive_t *drive)
  216. {
  217. struct ide_atapi_pc *pc = drive->pc;
  218. ide_hwif_t *hwif = drive->hwif;
  219. struct request *rq = hwif->hwgroup->rq;
  220. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  221. xfer_func_t *xferfunc;
  222. ide_expiry_t *expiry;
  223. unsigned int timeout, temp;
  224. u16 bcount;
  225. u8 stat, ireason, scsi = drive->scsi, dsc = 0;
  226. debug_log("Enter %s - interrupt handler\n", __func__);
  227. if (scsi) {
  228. timeout = ide_scsi_get_timeout(pc);
  229. expiry = ide_scsi_expiry;
  230. } else {
  231. timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
  232. : WAIT_TAPE_CMD;
  233. expiry = NULL;
  234. }
  235. if (pc->flags & PC_FLAG_TIMEDOUT) {
  236. drive->pc_callback(drive, 0);
  237. return ide_stopped;
  238. }
  239. /* Clear the interrupt */
  240. stat = tp_ops->read_status(hwif);
  241. if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
  242. if (hwif->dma_ops->dma_end(drive) ||
  243. (drive->media == ide_tape && !scsi && (stat & ATA_ERR))) {
  244. if (drive->media == ide_floppy && !scsi)
  245. printk(KERN_ERR "%s: DMA %s error\n",
  246. drive->name, rq_data_dir(pc->rq)
  247. ? "write" : "read");
  248. pc->flags |= PC_FLAG_DMA_ERROR;
  249. } else {
  250. pc->xferred = pc->req_xfer;
  251. if (drive->pc_update_buffers)
  252. drive->pc_update_buffers(drive, pc);
  253. }
  254. debug_log("%s: DMA finished\n", drive->name);
  255. }
  256. /* No more interrupts */
  257. if ((stat & ATA_DRQ) == 0) {
  258. debug_log("Packet command completed, %d bytes transferred\n",
  259. pc->xferred);
  260. pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
  261. local_irq_enable_in_hardirq();
  262. if (drive->media == ide_tape && !scsi &&
  263. (stat & ATA_ERR) && rq->cmd[0] == REQUEST_SENSE)
  264. stat &= ~ATA_ERR;
  265. if ((stat & ATA_ERR) || (pc->flags & PC_FLAG_DMA_ERROR)) {
  266. /* Error detected */
  267. debug_log("%s: I/O error\n", drive->name);
  268. if (drive->media != ide_tape || scsi) {
  269. pc->rq->errors++;
  270. if (scsi)
  271. goto cmd_finished;
  272. }
  273. if (rq->cmd[0] == REQUEST_SENSE) {
  274. printk(KERN_ERR "%s: I/O error in request sense"
  275. " command\n", drive->name);
  276. return ide_do_reset(drive);
  277. }
  278. debug_log("[cmd %x]: check condition\n", rq->cmd[0]);
  279. /* Retry operation */
  280. ide_retry_pc(drive, rq->rq_disk);
  281. /* queued, but not started */
  282. return ide_stopped;
  283. }
  284. cmd_finished:
  285. pc->error = 0;
  286. if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) && (stat & ATA_DSC) == 0)
  287. dsc = 1;
  288. /* Command finished - Call the callback function */
  289. drive->pc_callback(drive, dsc);
  290. return ide_stopped;
  291. }
  292. if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
  293. pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
  294. printk(KERN_ERR "%s: The device wants to issue more interrupts "
  295. "in DMA mode\n", drive->name);
  296. ide_dma_off(drive);
  297. return ide_do_reset(drive);
  298. }
  299. /* Get the number of bytes to transfer on this interrupt. */
  300. ide_read_bcount_and_ireason(drive, &bcount, &ireason);
  301. if (ireason & ATAPI_COD) {
  302. printk(KERN_ERR "%s: CoD != 0 in %s\n", drive->name, __func__);
  303. return ide_do_reset(drive);
  304. }
  305. if (((ireason & ATAPI_IO) == ATAPI_IO) ==
  306. !!(pc->flags & PC_FLAG_WRITING)) {
  307. /* Hopefully, we will never get here */
  308. printk(KERN_ERR "%s: We wanted to %s, but the device wants us "
  309. "to %s!\n", drive->name,
  310. (ireason & ATAPI_IO) ? "Write" : "Read",
  311. (ireason & ATAPI_IO) ? "Read" : "Write");
  312. return ide_do_reset(drive);
  313. }
  314. if (!(pc->flags & PC_FLAG_WRITING)) {
  315. /* Reading - Check that we have enough space */
  316. temp = pc->xferred + bcount;
  317. if (temp > pc->req_xfer) {
  318. if (temp > pc->buf_size) {
  319. printk(KERN_ERR "%s: The device wants to send "
  320. "us more data than expected - "
  321. "discarding data\n",
  322. drive->name);
  323. if (scsi)
  324. temp = pc->buf_size - pc->xferred;
  325. else
  326. temp = 0;
  327. if (temp) {
  328. if (pc->sg)
  329. drive->pc_io_buffers(drive, pc,
  330. temp, 0);
  331. else
  332. tp_ops->input_data(drive, NULL,
  333. pc->cur_pos, temp);
  334. printk(KERN_ERR "%s: transferred %d of "
  335. "%d bytes\n",
  336. drive->name,
  337. temp, bcount);
  338. }
  339. pc->xferred += temp;
  340. pc->cur_pos += temp;
  341. ide_pad_transfer(drive, 0, bcount - temp);
  342. goto next_irq;
  343. }
  344. debug_log("The device wants to send us more data than "
  345. "expected - allowing transfer\n");
  346. }
  347. xferfunc = tp_ops->input_data;
  348. } else
  349. xferfunc = tp_ops->output_data;
  350. if ((drive->media == ide_floppy && !scsi && !pc->buf) ||
  351. (drive->media == ide_tape && !scsi && pc->bh) ||
  352. (scsi && pc->sg)) {
  353. int done = drive->pc_io_buffers(drive, pc, bcount,
  354. !!(pc->flags & PC_FLAG_WRITING));
  355. /* FIXME: don't do partial completions */
  356. if (drive->media == ide_floppy && !scsi)
  357. ide_end_request(drive, 1, done >> 9);
  358. } else
  359. xferfunc(drive, NULL, pc->cur_pos, bcount);
  360. /* Update the current position */
  361. pc->xferred += bcount;
  362. pc->cur_pos += bcount;
  363. debug_log("[cmd %x] transferred %d bytes on that intr.\n",
  364. rq->cmd[0], bcount);
  365. next_irq:
  366. /* And set the interrupt handler again */
  367. ide_set_handler(drive, ide_pc_intr, timeout, expiry);
  368. return ide_started;
  369. }
  370. static u8 ide_read_ireason(ide_drive_t *drive)
  371. {
  372. ide_task_t task;
  373. memset(&task, 0, sizeof(task));
  374. task.tf_flags = IDE_TFLAG_IN_NSECT;
  375. drive->hwif->tp_ops->tf_read(drive, &task);
  376. return task.tf.nsect & 3;
  377. }
  378. static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
  379. {
  380. int retries = 100;
  381. while (retries-- && ((ireason & ATAPI_COD) == 0 ||
  382. (ireason & ATAPI_IO))) {
  383. printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
  384. "a packet command, retrying\n", drive->name);
  385. udelay(100);
  386. ireason = ide_read_ireason(drive);
  387. if (retries == 0) {
  388. printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
  389. "a packet command, ignoring\n",
  390. drive->name);
  391. ireason |= ATAPI_COD;
  392. ireason &= ~ATAPI_IO;
  393. }
  394. }
  395. return ireason;
  396. }
  397. ide_startstop_t ide_transfer_pc(ide_drive_t *drive, unsigned int timeout,
  398. ide_expiry_t *expiry)
  399. {
  400. struct ide_atapi_pc *pc = drive->pc;
  401. ide_hwif_t *hwif = drive->hwif;
  402. struct request *rq = hwif->hwgroup->rq;
  403. ide_startstop_t startstop;
  404. u8 ireason;
  405. if (ide_wait_stat(&startstop, drive, ATA_DRQ, ATA_BUSY, WAIT_READY)) {
  406. printk(KERN_ERR "%s: Strange, packet command initiated yet "
  407. "DRQ isn't asserted\n", drive->name);
  408. return startstop;
  409. }
  410. ireason = ide_read_ireason(drive);
  411. if (drive->media == ide_tape && !drive->scsi)
  412. ireason = ide_wait_ireason(drive, ireason);
  413. if ((ireason & ATAPI_COD) == 0 || (ireason & ATAPI_IO)) {
  414. printk(KERN_ERR "%s: (IO,CoD) != (0,1) while issuing "
  415. "a packet command\n", drive->name);
  416. return ide_do_reset(drive);
  417. }
  418. /* Set the interrupt routine */
  419. ide_set_handler(drive, ide_pc_intr, timeout, expiry);
  420. /* Begin DMA, if necessary */
  421. if (pc->flags & PC_FLAG_DMA_OK) {
  422. pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
  423. hwif->dma_ops->dma_start(drive);
  424. }
  425. /* Send the actual packet */
  426. if ((drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) == 0)
  427. hwif->tp_ops->output_data(drive, NULL, rq->cmd, 12);
  428. return ide_started;
  429. }
  430. EXPORT_SYMBOL_GPL(ide_transfer_pc);
  431. ide_startstop_t ide_issue_pc(ide_drive_t *drive,
  432. ide_handler_t *handler, unsigned int timeout,
  433. ide_expiry_t *expiry)
  434. {
  435. struct ide_atapi_pc *pc = drive->pc;
  436. ide_hwif_t *hwif = drive->hwif;
  437. u16 bcount;
  438. u8 dma = 0;
  439. /* We haven't transferred any data yet */
  440. pc->xferred = 0;
  441. pc->cur_pos = pc->buf;
  442. /* Request to transfer the entire buffer at once */
  443. if (drive->media == ide_tape && !drive->scsi)
  444. bcount = pc->req_xfer;
  445. else
  446. bcount = min(pc->req_xfer, 63 * 1024);
  447. if (pc->flags & PC_FLAG_DMA_ERROR) {
  448. pc->flags &= ~PC_FLAG_DMA_ERROR;
  449. ide_dma_off(drive);
  450. }
  451. if ((pc->flags & PC_FLAG_DMA_OK) && drive->using_dma) {
  452. if (drive->scsi)
  453. hwif->sg_mapped = 1;
  454. dma = !hwif->dma_ops->dma_setup(drive);
  455. if (drive->scsi)
  456. hwif->sg_mapped = 0;
  457. }
  458. if (!dma)
  459. pc->flags &= ~PC_FLAG_DMA_OK;
  460. ide_pktcmd_tf_load(drive, drive->scsi ? 0 : IDE_TFLAG_OUT_DEVICE,
  461. bcount, dma);
  462. /* Issue the packet command */
  463. if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
  464. ide_execute_command(drive, ATA_CMD_PACKET, handler,
  465. timeout, NULL);
  466. return ide_started;
  467. } else {
  468. ide_execute_pkt_cmd(drive);
  469. return (*handler)(drive);
  470. }
  471. }
  472. EXPORT_SYMBOL_GPL(ide_issue_pc);