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) &&
21 !(drive->dev_flags & IDE_DFLAG_SCSI);
25 * Check whether we can support a device,
26 * based on the ATAPI IDENTIFY command results.
28 int ide_check_atapi_device(ide_drive_t *drive, const char *s)
31 u8 gcw[2], protocol, device_type, removable, drq_type, packet_size;
33 *((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;
42 /* kludge for Apple PowerBook internal zip */
43 if (drive->media == ide_floppy && device_type == 5 &&
44 !strstr((char *)&id[ATA_ID_PROD], "CD-ROM") &&
45 strstr((char *)&id[ATA_ID_PROD], "ZIP"))
50 printk(KERN_ERR "%s: %s: protocol (0x%02x) is not ATAPI\n",
51 s, drive->name, protocol);
52 else if ((drive->media == ide_floppy && device_type != 0) ||
53 (drive->media == ide_tape && device_type != 1))
54 printk(KERN_ERR "%s: %s: invalid device type (0x%02x)\n",
55 s, drive->name, device_type);
56 else if (removable == 0)
57 printk(KERN_ERR "%s: %s: the removable flag is not set\n",
59 else if (drive->media == ide_floppy && drq_type == 3)
60 printk(KERN_ERR "%s: %s: sorry, DRQ type (0x%02x) not "
61 "supported\n", s, drive->name, drq_type);
62 else if (packet_size != 0)
63 printk(KERN_ERR "%s: %s: packet size (0x%02x) is not 12 "
64 "bytes\n", s, drive->name, packet_size);
69 EXPORT_SYMBOL_GPL(ide_check_atapi_device);
71 /* PIO data transfer routine using the scatter gather table. */
72 int ide_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
73 unsigned int bcount, int write)
75 ide_hwif_t *hwif = drive->hwif;
76 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
77 xfer_func_t *xf = write ? tp_ops->output_data : tp_ops->input_data;
78 struct scatterlist *sg = pc->sg;
83 count = min(sg->length - pc->b_count, bcount);
85 if (PageHighMem(sg_page(sg))) {
88 local_irq_save(flags);
89 buf = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
90 xf(drive, NULL, buf + pc->b_count, count);
91 kunmap_atomic(buf - sg->offset, KM_IRQ0);
92 local_irq_restore(flags);
95 xf(drive, NULL, buf + pc->b_count, count);
102 if (pc->b_count == sg->length) {
105 pc->sg = sg = sg_next(sg);
111 printk(KERN_ERR "%s: %d leftover bytes, %s\n", drive->name,
112 bcount, write ? "padding with zeros"
113 : "discarding data");
114 ide_pad_transfer(drive, write, bcount);
119 EXPORT_SYMBOL_GPL(ide_io_buffers);
121 void ide_init_pc(struct ide_atapi_pc *pc)
123 memset(pc, 0, sizeof(*pc));
124 pc->buf = pc->pc_buf;
125 pc->buf_size = IDE_PC_BUFFER_SIZE;
127 EXPORT_SYMBOL_GPL(ide_init_pc);
130 * Generate a new packet command request in front of the request queue, before
131 * the current request, so that it will be processed immediately, on the next
132 * pass through the driver.
134 static void ide_queue_pc_head(ide_drive_t *drive, struct gendisk *disk,
135 struct ide_atapi_pc *pc, struct request *rq)
137 blk_rq_init(NULL, rq);
138 rq->cmd_type = REQ_TYPE_SPECIAL;
139 rq->cmd_flags |= REQ_PREEMPT;
140 rq->buffer = (char *)pc;
142 memcpy(rq->cmd, pc->c, 12);
143 if (drive->media == ide_tape)
144 rq->cmd[13] = REQ_IDETAPE_PC1;
145 ide_do_drive_cmd(drive, rq);
149 * Add a special packet command request to the tail of the request queue,
150 * and wait for it to be serviced.
152 int ide_queue_pc_tail(ide_drive_t *drive, struct gendisk *disk,
153 struct ide_atapi_pc *pc)
158 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
159 rq->cmd_type = REQ_TYPE_SPECIAL;
160 rq->buffer = (char *)pc;
161 memcpy(rq->cmd, pc->c, 12);
162 if (drive->media == ide_tape)
163 rq->cmd[13] = REQ_IDETAPE_PC1;
164 error = blk_execute_rq(drive->queue, disk, rq, 0);
169 EXPORT_SYMBOL_GPL(ide_queue_pc_tail);
171 int ide_do_test_unit_ready(ide_drive_t *drive, struct gendisk *disk)
173 struct ide_atapi_pc pc;
176 pc.c[0] = TEST_UNIT_READY;
178 return ide_queue_pc_tail(drive, disk, &pc);
180 EXPORT_SYMBOL_GPL(ide_do_test_unit_ready);
182 int ide_do_start_stop(ide_drive_t *drive, struct gendisk *disk, int start)
184 struct ide_atapi_pc pc;
187 pc.c[0] = START_STOP;
190 if (drive->media == ide_tape)
191 pc.flags |= PC_FLAG_WAIT_FOR_DSC;
193 return ide_queue_pc_tail(drive, disk, &pc);
195 EXPORT_SYMBOL_GPL(ide_do_start_stop);
197 int ide_set_media_lock(ide_drive_t *drive, struct gendisk *disk, int on)
199 struct ide_atapi_pc pc;
201 if ((drive->dev_flags & IDE_DFLAG_DOORLOCKING) == 0)
205 pc.c[0] = ALLOW_MEDIUM_REMOVAL;
208 return ide_queue_pc_tail(drive, disk, &pc);
210 EXPORT_SYMBOL_GPL(ide_set_media_lock);
212 void ide_create_request_sense_cmd(ide_drive_t *drive, struct ide_atapi_pc *pc)
215 pc->c[0] = REQUEST_SENSE;
216 if (drive->media == ide_floppy) {
224 EXPORT_SYMBOL_GPL(ide_create_request_sense_cmd);
227 * Called when an error was detected during the last packet command.
228 * We queue a request sense packet command in the head of the request list.
230 void ide_retry_pc(ide_drive_t *drive, struct gendisk *disk)
232 struct request *rq = &drive->request_sense_rq;
233 struct ide_atapi_pc *pc = &drive->request_sense_pc;
235 (void)ide_read_error(drive);
236 ide_create_request_sense_cmd(drive, pc);
237 if (drive->media == ide_tape)
238 set_bit(IDE_AFLAG_IGNORE_DSC, &drive->atapi_flags);
239 ide_queue_pc_head(drive, disk, pc, rq);
241 EXPORT_SYMBOL_GPL(ide_retry_pc);
243 int ide_scsi_expiry(ide_drive_t *drive)
245 struct ide_atapi_pc *pc = drive->pc;
247 debug_log("%s called for %lu at %lu\n", __func__,
248 pc->scsi_cmd->serial_number, jiffies);
250 pc->flags |= PC_FLAG_TIMEDOUT;
252 return 0; /* we do not want the IDE subsystem to retry */
254 EXPORT_SYMBOL_GPL(ide_scsi_expiry);
256 int ide_cd_expiry(ide_drive_t *drive)
258 struct request *rq = HWGROUP(drive)->rq;
259 unsigned long wait = 0;
261 debug_log("%s: rq->cmd[0]: 0x%x\n", __func__, rq->cmd[0]);
264 * Some commands are *slow* and normally take a long time to complete.
265 * Usually we can use the ATAPI "disconnect" to bypass this, but not all
266 * commands/drives support that. Let ide_timer_expiry keep polling us
269 switch (rq->cmd[0]) {
271 case GPCMD_FORMAT_UNIT:
272 case GPCMD_RESERVE_RZONE_TRACK:
273 case GPCMD_CLOSE_TRACK:
274 case GPCMD_FLUSH_CACHE:
275 wait = ATAPI_WAIT_PC;
278 if (!(rq->cmd_flags & REQ_QUIET))
279 printk(KERN_INFO "cmd 0x%x timed out\n",
286 EXPORT_SYMBOL_GPL(ide_cd_expiry);
288 int ide_cd_get_xferlen(struct request *rq)
290 if (blk_fs_request(rq))
292 else if (blk_sense_request(rq) || blk_pc_request(rq) ||
293 rq->cmd_type == REQ_TYPE_ATA_PC)
298 EXPORT_SYMBOL_GPL(ide_cd_get_xferlen);
301 * This is the usual interrupt handler which will be called during a packet
302 * command. We will transfer some of the data (as requested by the drive)
303 * and will re-point interrupt handler to us.
305 static ide_startstop_t ide_pc_intr(ide_drive_t *drive)
307 struct ide_atapi_pc *pc = drive->pc;
308 ide_hwif_t *hwif = drive->hwif;
309 struct request *rq = hwif->hwgroup->rq;
310 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
311 xfer_func_t *xferfunc;
312 ide_expiry_t *expiry;
313 unsigned int timeout, temp;
315 u8 stat, ireason, scsi = !!(drive->dev_flags & IDE_DFLAG_SCSI), dsc = 0;
317 debug_log("Enter %s - interrupt handler\n", __func__);
320 timeout = ide_scsi_get_timeout(pc);
321 expiry = ide_scsi_expiry;
323 timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
328 if (pc->flags & PC_FLAG_TIMEDOUT) {
329 drive->pc_callback(drive, 0);
333 /* Clear the interrupt */
334 stat = tp_ops->read_status(hwif);
336 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
337 if (hwif->dma_ops->dma_end(drive) ||
338 (drive->media == ide_tape && !scsi && (stat & ATA_ERR))) {
339 if (drive->media == ide_floppy && !scsi)
340 printk(KERN_ERR "%s: DMA %s error\n",
341 drive->name, rq_data_dir(pc->rq)
343 pc->flags |= PC_FLAG_DMA_ERROR;
345 pc->xferred = pc->req_xfer;
346 if (drive->pc_update_buffers)
347 drive->pc_update_buffers(drive, pc);
349 debug_log("%s: DMA finished\n", drive->name);
352 /* No more interrupts */
353 if ((stat & ATA_DRQ) == 0) {
354 debug_log("Packet command completed, %d bytes transferred\n",
357 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
359 local_irq_enable_in_hardirq();
361 if (drive->media == ide_tape && !scsi &&
362 (stat & ATA_ERR) && rq->cmd[0] == REQUEST_SENSE)
365 if ((stat & ATA_ERR) || (pc->flags & PC_FLAG_DMA_ERROR)) {
367 debug_log("%s: I/O error\n", drive->name);
369 if (drive->media != ide_tape || scsi) {
375 if (rq->cmd[0] == REQUEST_SENSE) {
376 printk(KERN_ERR "%s: I/O error in request sense"
377 " command\n", drive->name);
378 return ide_do_reset(drive);
381 debug_log("[cmd %x]: check condition\n", rq->cmd[0]);
383 /* Retry operation */
384 ide_retry_pc(drive, rq->rq_disk);
386 /* queued, but not started */
392 if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) && (stat & ATA_DSC) == 0)
395 /* Command finished - Call the callback function */
396 drive->pc_callback(drive, dsc);
401 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
402 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
403 printk(KERN_ERR "%s: The device wants to issue more interrupts "
404 "in DMA mode\n", drive->name);
406 return ide_do_reset(drive);
409 /* Get the number of bytes to transfer on this interrupt. */
410 ide_read_bcount_and_ireason(drive, &bcount, &ireason);
412 if (ireason & ATAPI_COD) {
413 printk(KERN_ERR "%s: CoD != 0 in %s\n", drive->name, __func__);
414 return ide_do_reset(drive);
417 if (((ireason & ATAPI_IO) == ATAPI_IO) ==
418 !!(pc->flags & PC_FLAG_WRITING)) {
419 /* Hopefully, we will never get here */
420 printk(KERN_ERR "%s: We wanted to %s, but the device wants us "
421 "to %s!\n", drive->name,
422 (ireason & ATAPI_IO) ? "Write" : "Read",
423 (ireason & ATAPI_IO) ? "Read" : "Write");
424 return ide_do_reset(drive);
427 if (!(pc->flags & PC_FLAG_WRITING)) {
428 /* Reading - Check that we have enough space */
429 temp = pc->xferred + bcount;
430 if (temp > pc->req_xfer) {
431 if (temp > pc->buf_size) {
432 printk(KERN_ERR "%s: The device wants to send "
433 "us more data than expected - "
437 temp = pc->buf_size - pc->xferred;
442 drive->pc_io_buffers(drive, pc,
445 tp_ops->input_data(drive, NULL,
447 printk(KERN_ERR "%s: transferred %d of "
454 ide_pad_transfer(drive, 0, bcount - temp);
457 debug_log("The device wants to send us more data than "
458 "expected - allowing transfer\n");
460 xferfunc = tp_ops->input_data;
462 xferfunc = tp_ops->output_data;
464 if ((drive->media == ide_floppy && !scsi && !pc->buf) ||
465 (drive->media == ide_tape && !scsi && pc->bh) ||
467 int done = drive->pc_io_buffers(drive, pc, bcount,
468 !!(pc->flags & PC_FLAG_WRITING));
470 /* FIXME: don't do partial completions */
471 if (drive->media == ide_floppy && !scsi)
472 ide_end_request(drive, 1, done >> 9);
474 xferfunc(drive, NULL, pc->cur_pos, bcount);
476 /* Update the current position */
477 pc->xferred += bcount;
478 pc->cur_pos += bcount;
480 debug_log("[cmd %x] transferred %d bytes on that intr.\n",
483 /* And set the interrupt handler again */
484 ide_set_handler(drive, ide_pc_intr, timeout, expiry);
488 static u8 ide_read_ireason(ide_drive_t *drive)
492 memset(&task, 0, sizeof(task));
493 task.tf_flags = IDE_TFLAG_IN_NSECT;
495 drive->hwif->tp_ops->tf_read(drive, &task);
497 return task.tf.nsect & 3;
500 static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
504 while (retries-- && ((ireason & ATAPI_COD) == 0 ||
505 (ireason & ATAPI_IO))) {
506 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
507 "a packet command, retrying\n", drive->name);
509 ireason = ide_read_ireason(drive);
511 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
512 "a packet command, ignoring\n",
514 ireason |= ATAPI_COD;
515 ireason &= ~ATAPI_IO;
522 static int ide_delayed_transfer_pc(ide_drive_t *drive)
524 /* Send the actual packet */
525 drive->hwif->tp_ops->output_data(drive, NULL, drive->pc->c, 12);
527 /* Timeout for the packet command */
528 return WAIT_FLOPPY_CMD;
531 static ide_startstop_t ide_transfer_pc(ide_drive_t *drive)
533 struct ide_atapi_pc *pc = drive->pc;
534 ide_hwif_t *hwif = drive->hwif;
535 struct request *rq = hwif->hwgroup->rq;
536 ide_expiry_t *expiry;
537 unsigned int timeout;
538 ide_startstop_t startstop;
541 if (ide_wait_stat(&startstop, drive, ATA_DRQ, ATA_BUSY, WAIT_READY)) {
542 printk(KERN_ERR "%s: Strange, packet command initiated yet "
543 "DRQ isn't asserted\n", drive->name);
547 if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
549 drive->waiting_for_dma = 1;
552 ireason = ide_read_ireason(drive);
553 if (drive->media == ide_tape &&
554 (drive->dev_flags & IDE_DFLAG_SCSI) == 0)
555 ireason = ide_wait_ireason(drive, ireason);
557 if ((ireason & ATAPI_COD) == 0 || (ireason & ATAPI_IO)) {
558 printk(KERN_ERR "%s: (IO,CoD) != (0,1) while issuing "
559 "a packet command\n", drive->name);
560 return ide_do_reset(drive);
564 * If necessary schedule the packet transfer to occur 'timeout'
565 * miliseconds later in ide_delayed_transfer_pc() after the device
566 * says it's ready for a packet.
568 if (drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) {
569 timeout = drive->pc_delay;
570 expiry = &ide_delayed_transfer_pc;
572 if (drive->dev_flags & IDE_DFLAG_SCSI) {
573 timeout = ide_scsi_get_timeout(pc);
574 expiry = ide_scsi_expiry;
576 timeout = (drive->media == ide_floppy) ? WAIT_FLOPPY_CMD
582 /* Set the interrupt routine */
583 ide_set_handler(drive, ide_pc_intr, timeout, expiry);
585 /* Begin DMA, if necessary */
586 if (pc->flags & PC_FLAG_DMA_OK) {
587 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
588 hwif->dma_ops->dma_start(drive);
591 /* Send the actual packet */
592 if ((drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) == 0)
593 hwif->tp_ops->output_data(drive, NULL, rq->cmd, 12);
598 ide_startstop_t ide_issue_pc(ide_drive_t *drive, unsigned int timeout)
600 struct ide_atapi_pc *pc = drive->pc;
601 ide_hwif_t *hwif = drive->hwif;
602 ide_expiry_t *expiry = NULL;
605 u8 scsi = !!(drive->dev_flags & IDE_DFLAG_SCSI);
607 /* We haven't transferred any data yet */
609 pc->cur_pos = pc->buf;
611 if (dev_is_idecd(drive)) {
612 tf_flags = IDE_TFLAG_OUT_NSECT | IDE_TFLAG_OUT_LBAL;
613 bcount = ide_cd_get_xferlen(hwif->hwgroup->rq);
614 expiry = ide_cd_expiry;
617 bcount = min(pc->req_xfer, 63 * 1024);
618 expiry = ide_scsi_expiry;
620 tf_flags = IDE_TFLAG_OUT_DEVICE;
621 bcount = ((drive->media == ide_tape) ?
623 min(pc->req_xfer, 63 * 1024));
626 if (pc->flags & PC_FLAG_DMA_ERROR) {
627 pc->flags &= ~PC_FLAG_DMA_ERROR;
631 if (((pc->flags & PC_FLAG_DMA_OK) &&
632 (drive->dev_flags & IDE_DFLAG_USING_DMA)) ||
636 drive->dma = !hwif->dma_ops->dma_setup(drive);
642 pc->flags &= ~PC_FLAG_DMA_OK;
644 ide_pktcmd_tf_load(drive, tf_flags, bcount, drive->dma);
646 /* Issue the packet command */
647 if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
649 drive->waiting_for_dma = 0;
650 ide_execute_command(drive, ATA_CMD_PACKET, ide_transfer_pc,
654 ide_execute_pkt_cmd(drive);
655 return ide_transfer_pc(drive);
658 EXPORT_SYMBOL_GPL(ide_issue_pc);