5 #include <linux/kernel.h>
6 #include <linux/cdrom.h>
7 #include <linux/delay.h>
12 #define debug_log(fmt, args...) \
13 printk(KERN_INFO "ide: " fmt, ## args)
15 #define debug_log(fmt, args...) do {} while (0)
18 static inline int dev_is_idecd(ide_drive_t *drive)
20 return drive->media == ide_cdrom || drive->media == ide_optical;
24 * Check whether we can support a device,
25 * based on the ATAPI IDENTIFY command results.
27 int ide_check_atapi_device(ide_drive_t *drive, const char *s)
30 u8 gcw[2], protocol, device_type, removable, drq_type, packet_size;
32 *((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;
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"))
49 printk(KERN_ERR "%s: %s: protocol (0x%02x) is not ATAPI\n",
50 s, drive->name, protocol);
51 else if ((drive->media == ide_floppy && device_type != 0) ||
52 (drive->media == ide_tape && device_type != 1))
53 printk(KERN_ERR "%s: %s: invalid device type (0x%02x)\n",
54 s, drive->name, device_type);
55 else if (removable == 0)
56 printk(KERN_ERR "%s: %s: the removable flag is not set\n",
58 else if (drive->media == ide_floppy && drq_type == 3)
59 printk(KERN_ERR "%s: %s: sorry, DRQ type (0x%02x) not "
60 "supported\n", s, drive->name, drq_type);
61 else if (packet_size != 0)
62 printk(KERN_ERR "%s: %s: packet size (0x%02x) is not 12 "
63 "bytes\n", s, drive->name, packet_size);
68 EXPORT_SYMBOL_GPL(ide_check_atapi_device);
70 /* PIO data transfer routine using the scatter gather table. */
71 int ide_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
72 unsigned int bcount, int write)
74 ide_hwif_t *hwif = drive->hwif;
75 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
76 xfer_func_t *xf = write ? tp_ops->output_data : tp_ops->input_data;
77 struct scatterlist *sg = pc->sg;
82 count = min(sg->length - pc->b_count, bcount);
84 if (PageHighMem(sg_page(sg))) {
87 local_irq_save(flags);
88 buf = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
89 xf(drive, NULL, buf + pc->b_count, count);
90 kunmap_atomic(buf - sg->offset, KM_IRQ0);
91 local_irq_restore(flags);
94 xf(drive, NULL, buf + pc->b_count, count);
101 if (pc->b_count == sg->length) {
104 pc->sg = sg = sg_next(sg);
110 printk(KERN_ERR "%s: %d leftover bytes, %s\n", drive->name,
111 bcount, write ? "padding with zeros"
112 : "discarding data");
113 ide_pad_transfer(drive, write, bcount);
118 EXPORT_SYMBOL_GPL(ide_io_buffers);
120 void ide_init_pc(struct ide_atapi_pc *pc)
122 memset(pc, 0, sizeof(*pc));
123 pc->buf = pc->pc_buf;
124 pc->buf_size = IDE_PC_BUFFER_SIZE;
126 EXPORT_SYMBOL_GPL(ide_init_pc);
129 * Generate a new packet command request in front of the request queue, before
130 * the current request, so that it will be processed immediately, on the next
131 * pass through the driver.
133 static void ide_queue_pc_head(ide_drive_t *drive, struct gendisk *disk,
134 struct ide_atapi_pc *pc, struct request *rq)
136 blk_rq_init(NULL, rq);
137 rq->cmd_type = REQ_TYPE_SPECIAL;
138 rq->cmd_flags |= REQ_PREEMPT;
139 rq->buffer = (char *)pc;
141 memcpy(rq->cmd, pc->c, 12);
142 if (drive->media == ide_tape)
143 rq->cmd[13] = REQ_IDETAPE_PC1;
144 ide_do_drive_cmd(drive, rq);
148 * Add a special packet command request to the tail of the request queue,
149 * and wait for it to be serviced.
151 int ide_queue_pc_tail(ide_drive_t *drive, struct gendisk *disk,
152 struct ide_atapi_pc *pc)
157 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
158 rq->cmd_type = REQ_TYPE_SPECIAL;
159 rq->buffer = (char *)pc;
160 memcpy(rq->cmd, pc->c, 12);
161 if (drive->media == ide_tape)
162 rq->cmd[13] = REQ_IDETAPE_PC1;
163 error = blk_execute_rq(drive->queue, disk, rq, 0);
168 EXPORT_SYMBOL_GPL(ide_queue_pc_tail);
170 int ide_do_test_unit_ready(ide_drive_t *drive, struct gendisk *disk)
172 struct ide_atapi_pc pc;
175 pc.c[0] = TEST_UNIT_READY;
177 return ide_queue_pc_tail(drive, disk, &pc);
179 EXPORT_SYMBOL_GPL(ide_do_test_unit_ready);
181 int ide_do_start_stop(ide_drive_t *drive, struct gendisk *disk, int start)
183 struct ide_atapi_pc pc;
186 pc.c[0] = START_STOP;
189 if (drive->media == ide_tape)
190 pc.flags |= PC_FLAG_WAIT_FOR_DSC;
192 return ide_queue_pc_tail(drive, disk, &pc);
194 EXPORT_SYMBOL_GPL(ide_do_start_stop);
196 int ide_set_media_lock(ide_drive_t *drive, struct gendisk *disk, int on)
198 struct ide_atapi_pc pc;
200 if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) == 0)
204 pc.c[0] = ALLOW_MEDIUM_REMOVAL;
207 return ide_queue_pc_tail(drive, disk, &pc);
209 EXPORT_SYMBOL_GPL(ide_set_media_lock);
211 void ide_create_request_sense_cmd(ide_drive_t *drive, struct ide_atapi_pc *pc)
214 pc->c[0] = REQUEST_SENSE;
215 if (drive->media == ide_floppy) {
223 EXPORT_SYMBOL_GPL(ide_create_request_sense_cmd);
226 * Called when an error was detected during the last packet command.
227 * We queue a request sense packet command in the head of the request list.
229 void ide_retry_pc(ide_drive_t *drive, struct gendisk *disk)
231 struct request *rq = &drive->request_sense_rq;
232 struct ide_atapi_pc *pc = &drive->request_sense_pc;
234 (void)ide_read_error(drive);
235 ide_create_request_sense_cmd(drive, pc);
236 if (drive->media == ide_tape)
237 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
238 ide_queue_pc_head(drive, disk, pc, rq);
240 EXPORT_SYMBOL_GPL(ide_retry_pc);
242 int ide_cd_expiry(ide_drive_t *drive)
244 struct request *rq = HWGROUP(drive)->rq;
245 unsigned long wait = 0;
247 debug_log("%s: rq->cmd[0]: 0x%x\n", __func__, rq->cmd[0]);
250 * Some commands are *slow* and normally take a long time to complete.
251 * Usually we can use the ATAPI "disconnect" to bypass this, but not all
252 * commands/drives support that. Let ide_timer_expiry keep polling us
255 switch (rq->cmd[0]) {
257 case GPCMD_FORMAT_UNIT:
258 case GPCMD_RESERVE_RZONE_TRACK:
259 case GPCMD_CLOSE_TRACK:
260 case GPCMD_FLUSH_CACHE:
261 wait = ATAPI_WAIT_PC;
264 if (!(rq->cmd_flags & REQ_QUIET))
265 printk(KERN_INFO "cmd 0x%x timed out\n",
272 EXPORT_SYMBOL_GPL(ide_cd_expiry);
274 int ide_cd_get_xferlen(struct request *rq)
276 if (blk_fs_request(rq))
278 else if (blk_sense_request(rq) || blk_pc_request(rq) ||
279 rq->cmd_type == REQ_TYPE_ATA_PC)
284 EXPORT_SYMBOL_GPL(ide_cd_get_xferlen);
287 * This is the usual interrupt handler which will be called during a packet
288 * command. We will transfer some of the data (as requested by the drive)
289 * and will re-point interrupt handler to us.
291 static ide_startstop_t ide_pc_intr(ide_drive_t *drive)
293 struct ide_atapi_pc *pc = drive->pc;
294 ide_hwif_t *hwif = drive->hwif;
295 struct request *rq = hwif->hwgroup->rq;
296 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
297 xfer_func_t *xferfunc;
298 unsigned int timeout, temp;
300 u8 stat, ireason, dsc = 0;
302 debug_log("Enter %s - interrupt handler\n", __func__);
304 timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
307 if (pc->flags & PC_FLAG_TIMEDOUT) {
308 drive->pc_callback(drive, 0);
312 /* Clear the interrupt */
313 stat = tp_ops->read_status(hwif);
315 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
316 if (hwif->dma_ops->dma_end(drive) ||
317 (drive->media == ide_tape && (stat & ATA_ERR))) {
318 if (drive->media == ide_floppy)
319 printk(KERN_ERR "%s: DMA %s error\n",
320 drive->name, rq_data_dir(pc->rq)
322 pc->flags |= PC_FLAG_DMA_ERROR;
324 pc->xferred = pc->req_xfer;
325 if (drive->pc_update_buffers)
326 drive->pc_update_buffers(drive, pc);
328 debug_log("%s: DMA finished\n", drive->name);
331 /* No more interrupts */
332 if ((stat & ATA_DRQ) == 0) {
333 debug_log("Packet command completed, %d bytes transferred\n",
336 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
338 local_irq_enable_in_hardirq();
340 if (drive->media == ide_tape &&
341 (stat & ATA_ERR) && rq->cmd[0] == REQUEST_SENSE)
344 if ((stat & ATA_ERR) || (pc->flags & PC_FLAG_DMA_ERROR)) {
346 debug_log("%s: I/O error\n", drive->name);
348 if (drive->media != ide_tape)
351 if (rq->cmd[0] == REQUEST_SENSE) {
352 printk(KERN_ERR "%s: I/O error in request sense"
353 " command\n", drive->name);
354 return ide_do_reset(drive);
357 debug_log("[cmd %x]: check condition\n", rq->cmd[0]);
359 /* Retry operation */
360 ide_retry_pc(drive, rq->rq_disk);
362 /* queued, but not started */
367 if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) && (stat & ATA_DSC) == 0)
370 /* Command finished - Call the callback function */
371 drive->pc_callback(drive, dsc);
376 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
377 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
378 printk(KERN_ERR "%s: The device wants to issue more interrupts "
379 "in DMA mode\n", drive->name);
381 return ide_do_reset(drive);
384 /* Get the number of bytes to transfer on this interrupt. */
385 ide_read_bcount_and_ireason(drive, &bcount, &ireason);
387 if (ireason & ATAPI_COD) {
388 printk(KERN_ERR "%s: CoD != 0 in %s\n", drive->name, __func__);
389 return ide_do_reset(drive);
392 if (((ireason & ATAPI_IO) == ATAPI_IO) ==
393 !!(pc->flags & PC_FLAG_WRITING)) {
394 /* Hopefully, we will never get here */
395 printk(KERN_ERR "%s: We wanted to %s, but the device wants us "
396 "to %s!\n", drive->name,
397 (ireason & ATAPI_IO) ? "Write" : "Read",
398 (ireason & ATAPI_IO) ? "Read" : "Write");
399 return ide_do_reset(drive);
402 if (!(pc->flags & PC_FLAG_WRITING)) {
403 /* Reading - Check that we have enough space */
404 temp = pc->xferred + bcount;
405 if (temp > pc->req_xfer) {
406 if (temp > pc->buf_size) {
407 printk(KERN_ERR "%s: The device wants to send "
408 "us more data than expected - "
412 ide_pad_transfer(drive, 0, bcount);
415 debug_log("The device wants to send us more data than "
416 "expected - allowing transfer\n");
418 xferfunc = tp_ops->input_data;
420 xferfunc = tp_ops->output_data;
422 if ((drive->media == ide_floppy && !pc->buf) ||
423 (drive->media == ide_tape && pc->bh)) {
424 int done = drive->pc_io_buffers(drive, pc, bcount,
425 !!(pc->flags & PC_FLAG_WRITING));
427 /* FIXME: don't do partial completions */
428 if (drive->media == ide_floppy)
429 ide_end_request(drive, 1, done >> 9);
431 xferfunc(drive, NULL, pc->cur_pos, bcount);
433 /* Update the current position */
434 pc->xferred += bcount;
435 pc->cur_pos += bcount;
437 debug_log("[cmd %x] transferred %d bytes on that intr.\n",
440 /* And set the interrupt handler again */
441 ide_set_handler(drive, ide_pc_intr, timeout, NULL);
445 static u8 ide_read_ireason(ide_drive_t *drive)
449 memset(&task, 0, sizeof(task));
450 task.tf_flags = IDE_TFLAG_IN_NSECT;
452 drive->hwif->tp_ops->tf_read(drive, &task);
454 return task.tf.nsect & 3;
457 static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
461 while (retries-- && ((ireason & ATAPI_COD) == 0 ||
462 (ireason & ATAPI_IO))) {
463 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
464 "a packet command, retrying\n", drive->name);
466 ireason = ide_read_ireason(drive);
468 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
469 "a packet command, ignoring\n",
471 ireason |= ATAPI_COD;
472 ireason &= ~ATAPI_IO;
479 static int ide_delayed_transfer_pc(ide_drive_t *drive)
481 /* Send the actual packet */
482 drive->hwif->tp_ops->output_data(drive, NULL, drive->pc->c, 12);
484 /* Timeout for the packet command */
485 return WAIT_FLOPPY_CMD;
488 static ide_startstop_t ide_transfer_pc(ide_drive_t *drive)
490 struct ide_atapi_pc *pc = drive->pc;
491 ide_hwif_t *hwif = drive->hwif;
492 struct request *rq = hwif->hwgroup->rq;
493 ide_expiry_t *expiry;
494 unsigned int timeout;
495 ide_startstop_t startstop;
498 if (ide_wait_stat(&startstop, drive, ATA_DRQ, ATA_BUSY, WAIT_READY)) {
499 printk(KERN_ERR "%s: Strange, packet command initiated yet "
500 "DRQ isn't asserted\n", drive->name);
504 if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
506 drive->waiting_for_dma = 1;
509 ireason = ide_read_ireason(drive);
510 if (drive->media == ide_tape)
511 ireason = ide_wait_ireason(drive, ireason);
513 if ((ireason & ATAPI_COD) == 0 || (ireason & ATAPI_IO)) {
514 printk(KERN_ERR "%s: (IO,CoD) != (0,1) while issuing "
515 "a packet command\n", drive->name);
516 return ide_do_reset(drive);
520 * If necessary schedule the packet transfer to occur 'timeout'
521 * miliseconds later in ide_delayed_transfer_pc() after the device
522 * says it's ready for a packet.
524 if (drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) {
525 timeout = drive->pc_delay;
526 expiry = &ide_delayed_transfer_pc;
528 timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
533 /* Set the interrupt routine */
534 ide_set_handler(drive, ide_pc_intr, timeout, expiry);
536 /* Begin DMA, if necessary */
537 if (pc->flags & PC_FLAG_DMA_OK) {
538 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
539 hwif->dma_ops->dma_start(drive);
542 /* Send the actual packet */
543 if ((drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) == 0)
544 hwif->tp_ops->output_data(drive, NULL, rq->cmd, 12);
549 ide_startstop_t ide_issue_pc(ide_drive_t *drive, unsigned int timeout)
551 struct ide_atapi_pc *pc = drive->pc;
552 ide_hwif_t *hwif = drive->hwif;
553 ide_expiry_t *expiry = NULL;
557 /* We haven't transferred any data yet */
559 pc->cur_pos = pc->buf;
561 if (dev_is_idecd(drive)) {
562 tf_flags = IDE_TFLAG_OUT_NSECT | IDE_TFLAG_OUT_LBAL;
563 bcount = ide_cd_get_xferlen(hwif->hwgroup->rq);
564 expiry = ide_cd_expiry;
566 tf_flags = IDE_TFLAG_OUT_DEVICE;
567 bcount = ((drive->media == ide_tape) ?
569 min(pc->req_xfer, 63 * 1024));
572 if (pc->flags & PC_FLAG_DMA_ERROR) {
573 pc->flags &= ~PC_FLAG_DMA_ERROR;
577 if (((pc->flags & PC_FLAG_DMA_OK) &&
578 (drive->dev_flags & IDE_DFLAG_USING_DMA)) ||
580 drive->dma = !hwif->dma_ops->dma_setup(drive);
583 pc->flags &= ~PC_FLAG_DMA_OK;
585 ide_pktcmd_tf_load(drive, tf_flags, bcount, drive->dma);
587 /* Issue the packet command */
588 if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
590 drive->waiting_for_dma = 0;
591 ide_execute_command(drive, ATA_CMD_PACKET, ide_transfer_pc,
595 ide_execute_pkt_cmd(drive);
596 return ide_transfer_pc(drive);
599 EXPORT_SYMBOL_GPL(ide_issue_pc);