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 #define ATAPI_MIN_CDB_BYTES 12
20 static inline int dev_is_idecd(ide_drive_t
*drive
)
22 return drive
->media
== ide_cdrom
|| drive
->media
== ide_optical
;
26 * Check whether we can support a device,
27 * based on the ATAPI IDENTIFY command results.
29 int ide_check_atapi_device(ide_drive_t
*drive
, const char *s
)
32 u8 gcw
[2], protocol
, device_type
, removable
, drq_type
, packet_size
;
34 *((u16
*)&gcw
) = id
[ATA_ID_CONFIG
];
36 protocol
= (gcw
[1] & 0xC0) >> 6;
37 device_type
= gcw
[1] & 0x1F;
38 removable
= (gcw
[0] & 0x80) >> 7;
39 drq_type
= (gcw
[0] & 0x60) >> 5;
40 packet_size
= gcw
[0] & 0x03;
43 /* kludge for Apple PowerBook internal zip */
44 if (drive
->media
== ide_floppy
&& device_type
== 5 &&
45 !strstr((char *)&id
[ATA_ID_PROD
], "CD-ROM") &&
46 strstr((char *)&id
[ATA_ID_PROD
], "ZIP"))
51 printk(KERN_ERR
"%s: %s: protocol (0x%02x) is not ATAPI\n",
52 s
, drive
->name
, protocol
);
53 else if ((drive
->media
== ide_floppy
&& device_type
!= 0) ||
54 (drive
->media
== ide_tape
&& device_type
!= 1))
55 printk(KERN_ERR
"%s: %s: invalid device type (0x%02x)\n",
56 s
, drive
->name
, device_type
);
57 else if (removable
== 0)
58 printk(KERN_ERR
"%s: %s: the removable flag is not set\n",
60 else if (drive
->media
== ide_floppy
&& drq_type
== 3)
61 printk(KERN_ERR
"%s: %s: sorry, DRQ type (0x%02x) not "
62 "supported\n", s
, drive
->name
, drq_type
);
63 else if (packet_size
!= 0)
64 printk(KERN_ERR
"%s: %s: packet size (0x%02x) is not 12 "
65 "bytes\n", s
, drive
->name
, packet_size
);
70 EXPORT_SYMBOL_GPL(ide_check_atapi_device
);
72 /* PIO data transfer routine using the scatter gather table. */
73 int ide_io_buffers(ide_drive_t
*drive
, struct ide_atapi_pc
*pc
,
74 unsigned int bcount
, int write
)
76 ide_hwif_t
*hwif
= drive
->hwif
;
77 const struct ide_tp_ops
*tp_ops
= hwif
->tp_ops
;
78 xfer_func_t
*xf
= write
? tp_ops
->output_data
: tp_ops
->input_data
;
79 struct scatterlist
*sg
= pc
->sg
;
84 count
= min(sg
->length
- pc
->b_count
, bcount
);
86 if (PageHighMem(sg_page(sg
))) {
89 local_irq_save(flags
);
90 buf
= kmap_atomic(sg_page(sg
), KM_IRQ0
) + sg
->offset
;
91 xf(drive
, NULL
, buf
+ pc
->b_count
, count
);
92 kunmap_atomic(buf
- sg
->offset
, KM_IRQ0
);
93 local_irq_restore(flags
);
96 xf(drive
, NULL
, buf
+ pc
->b_count
, count
);
100 pc
->b_count
+= count
;
103 if (pc
->b_count
== sg
->length
) {
106 pc
->sg
= sg
= sg_next(sg
);
112 printk(KERN_ERR
"%s: %d leftover bytes, %s\n", drive
->name
,
113 bcount
, write
? "padding with zeros"
114 : "discarding data");
115 ide_pad_transfer(drive
, write
, bcount
);
120 EXPORT_SYMBOL_GPL(ide_io_buffers
);
122 void ide_init_pc(struct ide_atapi_pc
*pc
)
124 memset(pc
, 0, sizeof(*pc
));
125 pc
->buf
= pc
->pc_buf
;
126 pc
->buf_size
= IDE_PC_BUFFER_SIZE
;
128 EXPORT_SYMBOL_GPL(ide_init_pc
);
131 * Generate a new packet command request in front of the request queue, before
132 * the current request, so that it will be processed immediately, on the next
133 * pass through the driver.
135 static void ide_queue_pc_head(ide_drive_t
*drive
, struct gendisk
*disk
,
136 struct ide_atapi_pc
*pc
, struct request
*rq
)
138 blk_rq_init(NULL
, rq
);
139 rq
->cmd_type
= REQ_TYPE_SPECIAL
;
140 rq
->cmd_flags
|= REQ_PREEMPT
;
141 rq
->buffer
= (char *)pc
;
146 rq
->data_len
= pc
->req_xfer
;
149 memcpy(rq
->cmd
, pc
->c
, 12);
150 if (drive
->media
== ide_tape
)
151 rq
->cmd
[13] = REQ_IDETAPE_PC1
;
153 drive
->hwif
->rq
= NULL
;
155 elv_add_request(drive
->queue
, rq
, ELEVATOR_INSERT_FRONT
, 0);
159 * Add a special packet command request to the tail of the request queue,
160 * and wait for it to be serviced.
162 int ide_queue_pc_tail(ide_drive_t
*drive
, struct gendisk
*disk
,
163 struct ide_atapi_pc
*pc
)
168 rq
= blk_get_request(drive
->queue
, READ
, __GFP_WAIT
);
169 rq
->cmd_type
= REQ_TYPE_SPECIAL
;
170 rq
->buffer
= (char *)pc
;
174 rq
->data_len
= pc
->req_xfer
;
177 memcpy(rq
->cmd
, pc
->c
, 12);
178 if (drive
->media
== ide_tape
)
179 rq
->cmd
[13] = REQ_IDETAPE_PC1
;
180 error
= blk_execute_rq(drive
->queue
, disk
, rq
, 0);
185 EXPORT_SYMBOL_GPL(ide_queue_pc_tail
);
187 int ide_do_test_unit_ready(ide_drive_t
*drive
, struct gendisk
*disk
)
189 struct ide_atapi_pc pc
;
192 pc
.c
[0] = TEST_UNIT_READY
;
194 return ide_queue_pc_tail(drive
, disk
, &pc
);
196 EXPORT_SYMBOL_GPL(ide_do_test_unit_ready
);
198 int ide_do_start_stop(ide_drive_t
*drive
, struct gendisk
*disk
, int start
)
200 struct ide_atapi_pc pc
;
203 pc
.c
[0] = START_STOP
;
206 if (drive
->media
== ide_tape
)
207 pc
.flags
|= PC_FLAG_WAIT_FOR_DSC
;
209 return ide_queue_pc_tail(drive
, disk
, &pc
);
211 EXPORT_SYMBOL_GPL(ide_do_start_stop
);
213 int ide_set_media_lock(ide_drive_t
*drive
, struct gendisk
*disk
, int on
)
215 struct ide_atapi_pc pc
;
217 if ((drive
->dev_flags
& IDE_DFLAG_DOORLOCKING
) == 0)
221 pc
.c
[0] = ALLOW_MEDIUM_REMOVAL
;
224 return ide_queue_pc_tail(drive
, disk
, &pc
);
226 EXPORT_SYMBOL_GPL(ide_set_media_lock
);
228 void ide_create_request_sense_cmd(ide_drive_t
*drive
, struct ide_atapi_pc
*pc
)
231 pc
->c
[0] = REQUEST_SENSE
;
232 if (drive
->media
== ide_floppy
) {
240 EXPORT_SYMBOL_GPL(ide_create_request_sense_cmd
);
243 * Called when an error was detected during the last packet command.
244 * We queue a request sense packet command in the head of the request list.
246 void ide_retry_pc(ide_drive_t
*drive
, struct gendisk
*disk
)
248 struct request
*rq
= &drive
->request_sense_rq
;
249 struct ide_atapi_pc
*pc
= &drive
->request_sense_pc
;
251 (void)ide_read_error(drive
);
252 ide_create_request_sense_cmd(drive
, pc
);
253 if (drive
->media
== ide_tape
)
254 set_bit(IDE_AFLAG_IGNORE_DSC
, &drive
->atapi_flags
);
255 ide_queue_pc_head(drive
, disk
, pc
, rq
);
257 EXPORT_SYMBOL_GPL(ide_retry_pc
);
259 int ide_cd_expiry(ide_drive_t
*drive
)
261 struct request
*rq
= drive
->hwif
->rq
;
262 unsigned long wait
= 0;
264 debug_log("%s: rq->cmd[0]: 0x%x\n", __func__
, rq
->cmd
[0]);
267 * Some commands are *slow* and normally take a long time to complete.
268 * Usually we can use the ATAPI "disconnect" to bypass this, but not all
269 * commands/drives support that. Let ide_timer_expiry keep polling us
272 switch (rq
->cmd
[0]) {
274 case GPCMD_FORMAT_UNIT
:
275 case GPCMD_RESERVE_RZONE_TRACK
:
276 case GPCMD_CLOSE_TRACK
:
277 case GPCMD_FLUSH_CACHE
:
278 wait
= ATAPI_WAIT_PC
;
281 if (!(rq
->cmd_flags
& REQ_QUIET
))
282 printk(KERN_INFO
"cmd 0x%x timed out\n",
289 EXPORT_SYMBOL_GPL(ide_cd_expiry
);
291 int ide_cd_get_xferlen(struct request
*rq
)
293 if (blk_fs_request(rq
))
295 else if (blk_sense_request(rq
) || blk_pc_request(rq
) ||
296 rq
->cmd_type
== REQ_TYPE_ATA_PC
)
301 EXPORT_SYMBOL_GPL(ide_cd_get_xferlen
);
303 void ide_read_bcount_and_ireason(ide_drive_t
*drive
, u16
*bcount
, u8
*ireason
)
307 memset(&task
, 0, sizeof(task
));
308 task
.tf_flags
= IDE_TFLAG_IN_LBAH
| IDE_TFLAG_IN_LBAM
|
311 drive
->hwif
->tp_ops
->tf_read(drive
, &task
);
313 *bcount
= (task
.tf
.lbah
<< 8) | task
.tf
.lbam
;
314 *ireason
= task
.tf
.nsect
& 3;
316 EXPORT_SYMBOL_GPL(ide_read_bcount_and_ireason
);
319 * This is the usual interrupt handler which will be called during a packet
320 * command. We will transfer some of the data (as requested by the drive)
321 * and will re-point interrupt handler to us.
323 static ide_startstop_t
ide_pc_intr(ide_drive_t
*drive
)
325 struct ide_atapi_pc
*pc
= drive
->pc
;
326 ide_hwif_t
*hwif
= drive
->hwif
;
327 struct request
*rq
= hwif
->rq
;
328 const struct ide_tp_ops
*tp_ops
= hwif
->tp_ops
;
329 xfer_func_t
*xferfunc
;
330 unsigned int timeout
, temp
;
332 u8 stat
, ireason
, dsc
= 0;
334 debug_log("Enter %s - interrupt handler\n", __func__
);
336 timeout
= (drive
->media
== ide_floppy
) ? WAIT_FLOPPY_CMD
339 if (pc
->flags
& PC_FLAG_TIMEDOUT
) {
340 drive
->pc_callback(drive
, 0);
344 /* Clear the interrupt */
345 stat
= tp_ops
->read_status(hwif
);
347 if (pc
->flags
& PC_FLAG_DMA_IN_PROGRESS
) {
348 if (hwif
->dma_ops
->dma_end(drive
) ||
349 (drive
->media
== ide_tape
&& (stat
& ATA_ERR
))) {
350 if (drive
->media
== ide_floppy
)
351 printk(KERN_ERR
"%s: DMA %s error\n",
352 drive
->name
, rq_data_dir(pc
->rq
)
354 pc
->flags
|= PC_FLAG_DMA_ERROR
;
356 pc
->xferred
= pc
->req_xfer
;
357 if (drive
->pc_update_buffers
)
358 drive
->pc_update_buffers(drive
, pc
);
360 debug_log("%s: DMA finished\n", drive
->name
);
363 /* No more interrupts */
364 if ((stat
& ATA_DRQ
) == 0) {
365 debug_log("Packet command completed, %d bytes transferred\n",
368 pc
->flags
&= ~PC_FLAG_DMA_IN_PROGRESS
;
370 local_irq_enable_in_hardirq();
372 if (drive
->media
== ide_tape
&&
373 (stat
& ATA_ERR
) && rq
->cmd
[0] == REQUEST_SENSE
)
376 if ((stat
& ATA_ERR
) || (pc
->flags
& PC_FLAG_DMA_ERROR
)) {
378 debug_log("%s: I/O error\n", drive
->name
);
380 if (drive
->media
!= ide_tape
)
383 if (rq
->cmd
[0] == REQUEST_SENSE
) {
384 printk(KERN_ERR
"%s: I/O error in request sense"
385 " command\n", drive
->name
);
386 return ide_do_reset(drive
);
389 debug_log("[cmd %x]: check condition\n", rq
->cmd
[0]);
391 /* Retry operation */
392 ide_retry_pc(drive
, rq
->rq_disk
);
394 /* queued, but not started */
399 if ((pc
->flags
& PC_FLAG_WAIT_FOR_DSC
) && (stat
& ATA_DSC
) == 0)
402 /* Command finished - Call the callback function */
403 drive
->pc_callback(drive
, dsc
);
408 if (pc
->flags
& PC_FLAG_DMA_IN_PROGRESS
) {
409 pc
->flags
&= ~PC_FLAG_DMA_IN_PROGRESS
;
410 printk(KERN_ERR
"%s: The device wants to issue more interrupts "
411 "in DMA mode\n", drive
->name
);
413 return ide_do_reset(drive
);
416 /* Get the number of bytes to transfer on this interrupt. */
417 ide_read_bcount_and_ireason(drive
, &bcount
, &ireason
);
419 if (ireason
& ATAPI_COD
) {
420 printk(KERN_ERR
"%s: CoD != 0 in %s\n", drive
->name
, __func__
);
421 return ide_do_reset(drive
);
424 if (((ireason
& ATAPI_IO
) == ATAPI_IO
) ==
425 !!(pc
->flags
& PC_FLAG_WRITING
)) {
426 /* Hopefully, we will never get here */
427 printk(KERN_ERR
"%s: We wanted to %s, but the device wants us "
428 "to %s!\n", drive
->name
,
429 (ireason
& ATAPI_IO
) ? "Write" : "Read",
430 (ireason
& ATAPI_IO
) ? "Read" : "Write");
431 return ide_do_reset(drive
);
434 if (!(pc
->flags
& PC_FLAG_WRITING
)) {
435 /* Reading - Check that we have enough space */
436 temp
= pc
->xferred
+ bcount
;
437 if (temp
> pc
->req_xfer
) {
438 if (temp
> pc
->buf_size
) {
439 printk(KERN_ERR
"%s: The device wants to send "
440 "us more data than expected - "
444 ide_pad_transfer(drive
, 0, bcount
);
447 debug_log("The device wants to send us more data than "
448 "expected - allowing transfer\n");
450 xferfunc
= tp_ops
->input_data
;
452 xferfunc
= tp_ops
->output_data
;
454 if ((drive
->media
== ide_floppy
&& !pc
->buf
) ||
455 (drive
->media
== ide_tape
&& pc
->bh
)) {
456 int done
= drive
->pc_io_buffers(drive
, pc
, bcount
,
457 !!(pc
->flags
& PC_FLAG_WRITING
));
459 /* FIXME: don't do partial completions */
460 if (drive
->media
== ide_floppy
)
461 ide_end_request(drive
, 1, done
>> 9);
463 xferfunc(drive
, NULL
, pc
->cur_pos
, bcount
);
465 /* Update the current position */
466 pc
->xferred
+= bcount
;
467 pc
->cur_pos
+= bcount
;
469 debug_log("[cmd %x] transferred %d bytes on that intr.\n",
472 /* And set the interrupt handler again */
473 ide_set_handler(drive
, ide_pc_intr
, timeout
, NULL
);
477 static void ide_pktcmd_tf_load(ide_drive_t
*drive
, u32 tf_flags
, u16 bcount
)
479 ide_hwif_t
*hwif
= drive
->hwif
;
483 memset(&task
, 0, sizeof(task
));
484 task
.tf_flags
= IDE_TFLAG_OUT_LBAH
| IDE_TFLAG_OUT_LBAM
|
485 IDE_TFLAG_OUT_FEATURE
| tf_flags
;
486 task
.tf
.feature
= dma
; /* Use PIO/DMA */
487 task
.tf
.lbam
= bcount
& 0xff;
488 task
.tf
.lbah
= (bcount
>> 8) & 0xff;
490 ide_tf_dump(drive
->name
, &task
.tf
);
491 hwif
->tp_ops
->set_irq(hwif
, 1);
492 SELECT_MASK(drive
, 0);
493 hwif
->tp_ops
->tf_load(drive
, &task
);
496 static u8
ide_read_ireason(ide_drive_t
*drive
)
500 memset(&task
, 0, sizeof(task
));
501 task
.tf_flags
= IDE_TFLAG_IN_NSECT
;
503 drive
->hwif
->tp_ops
->tf_read(drive
, &task
);
505 return task
.tf
.nsect
& 3;
508 static u8
ide_wait_ireason(ide_drive_t
*drive
, u8 ireason
)
512 while (retries
-- && ((ireason
& ATAPI_COD
) == 0 ||
513 (ireason
& ATAPI_IO
))) {
514 printk(KERN_ERR
"%s: (IO,CoD != (0,1) while issuing "
515 "a packet command, retrying\n", drive
->name
);
517 ireason
= ide_read_ireason(drive
);
519 printk(KERN_ERR
"%s: (IO,CoD != (0,1) while issuing "
520 "a packet command, ignoring\n",
522 ireason
|= ATAPI_COD
;
523 ireason
&= ~ATAPI_IO
;
530 static int ide_delayed_transfer_pc(ide_drive_t
*drive
)
532 /* Send the actual packet */
533 drive
->hwif
->tp_ops
->output_data(drive
, NULL
, drive
->pc
->c
, 12);
535 /* Timeout for the packet command */
536 return WAIT_FLOPPY_CMD
;
539 static ide_startstop_t
ide_transfer_pc(ide_drive_t
*drive
)
541 struct ide_atapi_pc
*uninitialized_var(pc
);
542 ide_hwif_t
*hwif
= drive
->hwif
;
543 struct request
*rq
= hwif
->rq
;
544 ide_expiry_t
*expiry
;
545 unsigned int timeout
;
547 ide_startstop_t startstop
;
550 if (ide_wait_stat(&startstop
, drive
, ATA_DRQ
, ATA_BUSY
, WAIT_READY
)) {
551 printk(KERN_ERR
"%s: Strange, packet command initiated yet "
552 "DRQ isn't asserted\n", drive
->name
);
556 if (drive
->atapi_flags
& IDE_AFLAG_DRQ_INTERRUPT
) {
558 drive
->waiting_for_dma
= 1;
561 if (dev_is_idecd(drive
)) {
562 /* ATAPI commands get padded out to 12 bytes minimum */
563 cmd_len
= COMMAND_SIZE(rq
->cmd
[0]);
564 if (cmd_len
< ATAPI_MIN_CDB_BYTES
)
565 cmd_len
= ATAPI_MIN_CDB_BYTES
;
567 timeout
= rq
->timeout
;
568 expiry
= ide_cd_expiry
;
572 cmd_len
= ATAPI_MIN_CDB_BYTES
;
575 * If necessary schedule the packet transfer to occur 'timeout'
576 * miliseconds later in ide_delayed_transfer_pc() after the
577 * device says it's ready for a packet.
579 if (drive
->atapi_flags
& IDE_AFLAG_ZIP_DRIVE
) {
580 timeout
= drive
->pc_delay
;
581 expiry
= &ide_delayed_transfer_pc
;
583 timeout
= (drive
->media
== ide_floppy
) ? WAIT_FLOPPY_CMD
588 ireason
= ide_read_ireason(drive
);
589 if (drive
->media
== ide_tape
)
590 ireason
= ide_wait_ireason(drive
, ireason
);
592 if ((ireason
& ATAPI_COD
) == 0 || (ireason
& ATAPI_IO
)) {
593 printk(KERN_ERR
"%s: (IO,CoD) != (0,1) while issuing "
594 "a packet command\n", drive
->name
);
596 return ide_do_reset(drive
);
600 /* Set the interrupt routine */
601 ide_set_handler(drive
,
602 (dev_is_idecd(drive
) ? drive
->irq_handler
606 /* Begin DMA, if necessary */
607 if (dev_is_idecd(drive
)) {
609 hwif
->dma_ops
->dma_start(drive
);
611 if (pc
->flags
& PC_FLAG_DMA_OK
) {
612 pc
->flags
|= PC_FLAG_DMA_IN_PROGRESS
;
613 hwif
->dma_ops
->dma_start(drive
);
617 /* Send the actual packet */
618 if ((drive
->atapi_flags
& IDE_AFLAG_ZIP_DRIVE
) == 0)
619 hwif
->tp_ops
->output_data(drive
, NULL
, rq
->cmd
, cmd_len
);
624 ide_startstop_t
ide_issue_pc(ide_drive_t
*drive
)
626 struct ide_atapi_pc
*pc
;
627 ide_hwif_t
*hwif
= drive
->hwif
;
628 ide_expiry_t
*expiry
= NULL
;
629 unsigned int timeout
;
633 if (dev_is_idecd(drive
)) {
634 tf_flags
= IDE_TFLAG_OUT_NSECT
| IDE_TFLAG_OUT_LBAL
;
635 bcount
= ide_cd_get_xferlen(hwif
->rq
);
636 expiry
= ide_cd_expiry
;
637 timeout
= ATAPI_WAIT_PC
;
640 drive
->dma
= !hwif
->dma_ops
->dma_setup(drive
);
644 /* We haven't transferred any data yet */
646 pc
->cur_pos
= pc
->buf
;
648 tf_flags
= IDE_TFLAG_OUT_DEVICE
;
649 bcount
= ((drive
->media
== ide_tape
) ?
651 min(pc
->req_xfer
, 63 * 1024));
653 if (pc
->flags
& PC_FLAG_DMA_ERROR
) {
654 pc
->flags
&= ~PC_FLAG_DMA_ERROR
;
658 if ((pc
->flags
& PC_FLAG_DMA_OK
) &&
659 (drive
->dev_flags
& IDE_DFLAG_USING_DMA
))
660 drive
->dma
= !hwif
->dma_ops
->dma_setup(drive
);
663 pc
->flags
&= ~PC_FLAG_DMA_OK
;
665 timeout
= (drive
->media
== ide_floppy
) ? WAIT_FLOPPY_CMD
669 ide_pktcmd_tf_load(drive
, tf_flags
, bcount
);
671 /* Issue the packet command */
672 if (drive
->atapi_flags
& IDE_AFLAG_DRQ_INTERRUPT
) {
674 drive
->waiting_for_dma
= 0;
675 ide_execute_command(drive
, ATA_CMD_PACKET
, ide_transfer_pc
,
679 ide_execute_pkt_cmd(drive
);
680 return ide_transfer_pc(drive
);
683 EXPORT_SYMBOL_GPL(ide_issue_pc
);