5 #include <linux/kernel.h>
6 #include <linux/cdrom.h>
7 #include <linux/delay.h>
9 #include <linux/scatterlist.h>
11 #include <scsi/scsi.h>
13 #define DRV_NAME "ide-atapi"
14 #define PFX DRV_NAME ": "
17 #define debug_log(fmt, args...) \
18 printk(KERN_INFO "ide: " fmt, ## args)
20 #define debug_log(fmt, args...) do {} while (0)
23 #define ATAPI_MIN_CDB_BYTES 12
25 static inline int dev_is_idecd(ide_drive_t
*drive
)
27 return drive
->media
== ide_cdrom
|| drive
->media
== ide_optical
;
31 * Check whether we can support a device,
32 * based on the ATAPI IDENTIFY command results.
34 int ide_check_atapi_device(ide_drive_t
*drive
, const char *s
)
37 u8 gcw
[2], protocol
, device_type
, removable
, drq_type
, packet_size
;
39 *((u16
*)&gcw
) = id
[ATA_ID_CONFIG
];
41 protocol
= (gcw
[1] & 0xC0) >> 6;
42 device_type
= gcw
[1] & 0x1F;
43 removable
= (gcw
[0] & 0x80) >> 7;
44 drq_type
= (gcw
[0] & 0x60) >> 5;
45 packet_size
= gcw
[0] & 0x03;
48 /* kludge for Apple PowerBook internal zip */
49 if (drive
->media
== ide_floppy
&& device_type
== 5 &&
50 !strstr((char *)&id
[ATA_ID_PROD
], "CD-ROM") &&
51 strstr((char *)&id
[ATA_ID_PROD
], "ZIP"))
56 printk(KERN_ERR
"%s: %s: protocol (0x%02x) is not ATAPI\n",
57 s
, drive
->name
, protocol
);
58 else if ((drive
->media
== ide_floppy
&& device_type
!= 0) ||
59 (drive
->media
== ide_tape
&& device_type
!= 1))
60 printk(KERN_ERR
"%s: %s: invalid device type (0x%02x)\n",
61 s
, drive
->name
, device_type
);
62 else if (removable
== 0)
63 printk(KERN_ERR
"%s: %s: the removable flag is not set\n",
65 else if (drive
->media
== ide_floppy
&& drq_type
== 3)
66 printk(KERN_ERR
"%s: %s: sorry, DRQ type (0x%02x) not "
67 "supported\n", s
, drive
->name
, drq_type
);
68 else if (packet_size
!= 0)
69 printk(KERN_ERR
"%s: %s: packet size (0x%02x) is not 12 "
70 "bytes\n", s
, drive
->name
, packet_size
);
75 EXPORT_SYMBOL_GPL(ide_check_atapi_device
);
77 void ide_init_pc(struct ide_atapi_pc
*pc
)
79 memset(pc
, 0, sizeof(*pc
));
81 EXPORT_SYMBOL_GPL(ide_init_pc
);
84 * Add a special packet command request to the tail of the request queue,
85 * and wait for it to be serviced.
87 int ide_queue_pc_tail(ide_drive_t
*drive
, struct gendisk
*disk
,
88 struct ide_atapi_pc
*pc
, void *buf
, unsigned int bufflen
)
93 rq
= blk_get_request(drive
->queue
, READ
, __GFP_WAIT
);
94 rq
->cmd_type
= REQ_TYPE_SPECIAL
;
95 rq
->special
= (char *)pc
;
98 error
= blk_rq_map_kern(drive
->queue
, rq
, buf
, bufflen
,
104 memcpy(rq
->cmd
, pc
->c
, 12);
105 if (drive
->media
== ide_tape
)
106 rq
->cmd
[13] = REQ_IDETAPE_PC1
;
107 error
= blk_execute_rq(drive
->queue
, disk
, rq
, 0);
112 EXPORT_SYMBOL_GPL(ide_queue_pc_tail
);
114 int ide_do_test_unit_ready(ide_drive_t
*drive
, struct gendisk
*disk
)
116 struct ide_atapi_pc pc
;
119 pc
.c
[0] = TEST_UNIT_READY
;
121 return ide_queue_pc_tail(drive
, disk
, &pc
, NULL
, 0);
123 EXPORT_SYMBOL_GPL(ide_do_test_unit_ready
);
125 int ide_do_start_stop(ide_drive_t
*drive
, struct gendisk
*disk
, int start
)
127 struct ide_atapi_pc pc
;
130 pc
.c
[0] = START_STOP
;
133 if (drive
->media
== ide_tape
)
134 pc
.flags
|= PC_FLAG_WAIT_FOR_DSC
;
136 return ide_queue_pc_tail(drive
, disk
, &pc
, NULL
, 0);
138 EXPORT_SYMBOL_GPL(ide_do_start_stop
);
140 int ide_set_media_lock(ide_drive_t
*drive
, struct gendisk
*disk
, int on
)
142 struct ide_atapi_pc pc
;
144 if ((drive
->dev_flags
& IDE_DFLAG_DOORLOCKING
) == 0)
148 pc
.c
[0] = ALLOW_MEDIUM_REMOVAL
;
151 return ide_queue_pc_tail(drive
, disk
, &pc
, NULL
, 0);
153 EXPORT_SYMBOL_GPL(ide_set_media_lock
);
155 void ide_create_request_sense_cmd(ide_drive_t
*drive
, struct ide_atapi_pc
*pc
)
158 pc
->c
[0] = REQUEST_SENSE
;
159 if (drive
->media
== ide_floppy
) {
167 EXPORT_SYMBOL_GPL(ide_create_request_sense_cmd
);
169 void ide_prep_sense(ide_drive_t
*drive
, struct request
*rq
)
171 struct request_sense
*sense
= &drive
->sense_data
;
172 struct request
*sense_rq
= &drive
->sense_rq
;
173 unsigned int cmd_len
, sense_len
;
176 switch (drive
->media
) {
190 BUG_ON(sense_len
> sizeof(*sense
));
192 if (blk_sense_request(rq
) || drive
->sense_rq_armed
)
195 memset(sense
, 0, sizeof(*sense
));
197 blk_rq_init(rq
->q
, sense_rq
);
199 err
= blk_rq_map_kern(drive
->queue
, sense_rq
, sense
, sense_len
,
202 if (printk_ratelimit())
203 printk(KERN_WARNING PFX
"%s: failed to map sense "
204 "buffer\n", drive
->name
);
208 sense_rq
->rq_disk
= rq
->rq_disk
;
209 sense_rq
->cmd
[0] = GPCMD_REQUEST_SENSE
;
210 sense_rq
->cmd
[4] = cmd_len
;
211 sense_rq
->cmd_type
= REQ_TYPE_SENSE
;
212 sense_rq
->cmd_flags
|= REQ_PREEMPT
;
214 if (drive
->media
== ide_tape
)
215 sense_rq
->cmd
[13] = REQ_IDETAPE_PC1
;
217 drive
->sense_rq_armed
= true;
219 EXPORT_SYMBOL_GPL(ide_prep_sense
);
221 int ide_queue_sense_rq(ide_drive_t
*drive
, void *special
)
223 /* deferred failure from ide_prep_sense() */
224 if (!drive
->sense_rq_armed
) {
225 printk(KERN_WARNING PFX
"%s: error queuing a sense request\n",
230 drive
->sense_rq
.special
= special
;
231 drive
->sense_rq_armed
= false;
233 drive
->hwif
->rq
= NULL
;
235 elv_add_request(drive
->queue
, &drive
->sense_rq
,
236 ELEVATOR_INSERT_FRONT
, 0);
239 EXPORT_SYMBOL_GPL(ide_queue_sense_rq
);
242 * Called when an error was detected during the last packet command.
243 * We queue a request sense packet command at the head of the request
246 void ide_retry_pc(ide_drive_t
*drive
)
248 struct request
*failed_rq
= drive
->hwif
->rq
;
249 struct request
*sense_rq
= &drive
->sense_rq
;
250 struct ide_atapi_pc
*pc
= &drive
->request_sense_pc
;
252 (void)ide_read_error(drive
);
254 /* init pc from sense_rq */
256 memcpy(pc
->c
, sense_rq
->cmd
, 12);
258 if (drive
->media
== ide_tape
)
259 drive
->atapi_flags
|= IDE_AFLAG_IGNORE_DSC
;
262 * Push back the failed request and put request sense on top
263 * of it. The failed command will be retried after sense data
266 blk_requeue_request(failed_rq
->q
, failed_rq
);
267 drive
->hwif
->rq
= NULL
;
268 if (ide_queue_sense_rq(drive
, pc
)) {
269 blk_start_request(failed_rq
);
270 ide_complete_rq(drive
, -EIO
, blk_rq_bytes(failed_rq
));
273 EXPORT_SYMBOL_GPL(ide_retry_pc
);
275 int ide_cd_expiry(ide_drive_t
*drive
)
277 struct request
*rq
= drive
->hwif
->rq
;
278 unsigned long wait
= 0;
280 debug_log("%s: rq->cmd[0]: 0x%x\n", __func__
, rq
->cmd
[0]);
283 * Some commands are *slow* and normally take a long time to complete.
284 * Usually we can use the ATAPI "disconnect" to bypass this, but not all
285 * commands/drives support that. Let ide_timer_expiry keep polling us
288 switch (rq
->cmd
[0]) {
290 case GPCMD_FORMAT_UNIT
:
291 case GPCMD_RESERVE_RZONE_TRACK
:
292 case GPCMD_CLOSE_TRACK
:
293 case GPCMD_FLUSH_CACHE
:
294 wait
= ATAPI_WAIT_PC
;
297 if (!(rq
->cmd_flags
& REQ_QUIET
))
298 printk(KERN_INFO PFX
"cmd 0x%x timed out\n",
305 EXPORT_SYMBOL_GPL(ide_cd_expiry
);
307 int ide_cd_get_xferlen(struct request
*rq
)
309 if (blk_fs_request(rq
))
311 else if (blk_sense_request(rq
) || blk_pc_request(rq
) ||
312 rq
->cmd_type
== REQ_TYPE_ATA_PC
)
313 return blk_rq_bytes(rq
);
317 EXPORT_SYMBOL_GPL(ide_cd_get_xferlen
);
319 void ide_read_bcount_and_ireason(ide_drive_t
*drive
, u16
*bcount
, u8
*ireason
)
321 struct ide_taskfile tf
;
323 drive
->hwif
->tp_ops
->tf_read(drive
, &tf
, IDE_VALID_NSECT
|
324 IDE_VALID_LBAM
| IDE_VALID_LBAH
);
326 *bcount
= (tf
.lbah
<< 8) | tf
.lbam
;
327 *ireason
= tf
.nsect
& 3;
329 EXPORT_SYMBOL_GPL(ide_read_bcount_and_ireason
);
332 * Check the contents of the interrupt reason register and attempt to recover if
333 * there are problems.
336 * - 0 if everything's ok
337 * - 1 if the request has to be terminated.
339 int ide_check_ireason(ide_drive_t
*drive
, struct request
*rq
, int len
,
342 ide_hwif_t
*hwif
= drive
->hwif
;
344 debug_log("ireason: 0x%x, rw: 0x%x\n", ireason
, rw
);
346 if (ireason
== (!rw
<< 1))
348 else if (ireason
== (rw
<< 1)) {
349 printk(KERN_ERR PFX
"%s: %s: wrong transfer direction!\n",
350 drive
->name
, __func__
);
352 if (dev_is_idecd(drive
))
353 ide_pad_transfer(drive
, rw
, len
);
354 } else if (!rw
&& ireason
== ATAPI_COD
) {
355 if (dev_is_idecd(drive
)) {
357 * Some drives (ASUS) seem to tell us that status info
358 * is available. Just get it and ignore.
360 (void)hwif
->tp_ops
->read_status(hwif
);
364 if (ireason
& ATAPI_COD
)
365 printk(KERN_ERR PFX
"%s: CoD != 0 in %s\n", drive
->name
,
368 /* drive wants a command packet, or invalid ireason... */
369 printk(KERN_ERR PFX
"%s: %s: bad interrupt reason 0x%02x\n",
370 drive
->name
, __func__
, ireason
);
373 if (dev_is_idecd(drive
) && rq
->cmd_type
== REQ_TYPE_ATA_PC
)
374 rq
->cmd_flags
|= REQ_FAILED
;
378 EXPORT_SYMBOL_GPL(ide_check_ireason
);
381 * This is the usual interrupt handler which will be called during a packet
382 * command. We will transfer some of the data (as requested by the drive)
383 * and will re-point interrupt handler to us.
385 static ide_startstop_t
ide_pc_intr(ide_drive_t
*drive
)
387 struct ide_atapi_pc
*pc
= drive
->pc
;
388 ide_hwif_t
*hwif
= drive
->hwif
;
389 struct ide_cmd
*cmd
= &hwif
->cmd
;
390 struct request
*rq
= hwif
->rq
;
391 const struct ide_tp_ops
*tp_ops
= hwif
->tp_ops
;
392 unsigned int timeout
, done
;
394 u8 stat
, ireason
, dsc
= 0;
395 u8 write
= !!(pc
->flags
& PC_FLAG_WRITING
);
397 debug_log("Enter %s - interrupt handler\n", __func__
);
399 timeout
= (drive
->media
== ide_floppy
) ? WAIT_FLOPPY_CMD
402 /* Clear the interrupt */
403 stat
= tp_ops
->read_status(hwif
);
405 if (pc
->flags
& PC_FLAG_DMA_IN_PROGRESS
) {
408 drive
->waiting_for_dma
= 0;
409 rc
= hwif
->dma_ops
->dma_end(drive
);
410 ide_dma_unmap_sg(drive
, cmd
);
412 if (rc
|| (drive
->media
== ide_tape
&& (stat
& ATA_ERR
))) {
413 if (drive
->media
== ide_floppy
)
414 printk(KERN_ERR PFX
"%s: DMA %s error\n",
415 drive
->name
, rq_data_dir(pc
->rq
)
417 pc
->flags
|= PC_FLAG_DMA_ERROR
;
420 debug_log("%s: DMA finished\n", drive
->name
);
423 /* No more interrupts */
424 if ((stat
& ATA_DRQ
) == 0) {
427 debug_log("Packet command completed, %d bytes transferred\n",
430 pc
->flags
&= ~PC_FLAG_DMA_IN_PROGRESS
;
432 local_irq_enable_in_hardirq();
434 if (drive
->media
== ide_tape
&&
435 (stat
& ATA_ERR
) && rq
->cmd
[0] == REQUEST_SENSE
)
438 if ((stat
& ATA_ERR
) || (pc
->flags
& PC_FLAG_DMA_ERROR
)) {
440 debug_log("%s: I/O error\n", drive
->name
);
442 if (drive
->media
!= ide_tape
)
445 if (rq
->cmd
[0] == REQUEST_SENSE
) {
446 printk(KERN_ERR PFX
"%s: I/O error in request "
447 "sense command\n", drive
->name
);
448 return ide_do_reset(drive
);
451 debug_log("[cmd %x]: check condition\n", rq
->cmd
[0]);
453 /* Retry operation */
456 /* queued, but not started */
461 if ((pc
->flags
& PC_FLAG_WAIT_FOR_DSC
) && (stat
& ATA_DSC
) == 0)
465 * ->pc_callback() might change rq->data_len for
466 * residual count, cache total length.
468 done
= blk_rq_bytes(rq
);
470 /* Command finished - Call the callback function */
471 uptodate
= drive
->pc_callback(drive
, dsc
);
474 drive
->failed_pc
= NULL
;
476 if (blk_special_request(rq
)) {
481 if (blk_fs_request(rq
) == 0 && uptodate
<= 0) {
486 error
= uptodate
? 0 : -EIO
;
489 ide_complete_rq(drive
, error
, blk_rq_bytes(rq
));
493 if (pc
->flags
& PC_FLAG_DMA_IN_PROGRESS
) {
494 pc
->flags
&= ~PC_FLAG_DMA_IN_PROGRESS
;
495 printk(KERN_ERR PFX
"%s: The device wants to issue more "
496 "interrupts in DMA mode\n", drive
->name
);
498 return ide_do_reset(drive
);
501 /* Get the number of bytes to transfer on this interrupt. */
502 ide_read_bcount_and_ireason(drive
, &bcount
, &ireason
);
504 if (ide_check_ireason(drive
, rq
, bcount
, ireason
, write
))
505 return ide_do_reset(drive
);
507 done
= min_t(unsigned int, bcount
, cmd
->nleft
);
508 ide_pio_bytes(drive
, cmd
, write
, done
);
510 /* Update transferred byte count */
511 rq
->resid_len
-= done
;
516 ide_pad_transfer(drive
, write
, bcount
);
518 debug_log("[cmd %x] transferred %d bytes, padded %d bytes, resid: %u\n",
519 rq
->cmd
[0], done
, bcount
, rq
->resid_len
);
521 /* And set the interrupt handler again */
522 ide_set_handler(drive
, ide_pc_intr
, timeout
);
526 static void ide_init_packet_cmd(struct ide_cmd
*cmd
, u8 valid_tf
,
529 cmd
->protocol
= dma
? ATAPI_PROT_DMA
: ATAPI_PROT_PIO
;
530 cmd
->valid
.out
.tf
= IDE_VALID_LBAH
| IDE_VALID_LBAM
|
531 IDE_VALID_FEATURE
| valid_tf
;
532 cmd
->tf
.command
= ATA_CMD_PACKET
;
533 cmd
->tf
.feature
= dma
; /* Use PIO/DMA */
534 cmd
->tf
.lbam
= bcount
& 0xff;
535 cmd
->tf
.lbah
= (bcount
>> 8) & 0xff;
538 static u8
ide_read_ireason(ide_drive_t
*drive
)
540 struct ide_taskfile tf
;
542 drive
->hwif
->tp_ops
->tf_read(drive
, &tf
, IDE_VALID_NSECT
);
547 static u8
ide_wait_ireason(ide_drive_t
*drive
, u8 ireason
)
551 while (retries
-- && ((ireason
& ATAPI_COD
) == 0 ||
552 (ireason
& ATAPI_IO
))) {
553 printk(KERN_ERR PFX
"%s: (IO,CoD != (0,1) while issuing "
554 "a packet command, retrying\n", drive
->name
);
556 ireason
= ide_read_ireason(drive
);
558 printk(KERN_ERR PFX
"%s: (IO,CoD != (0,1) while issuing"
559 " a packet command, ignoring\n",
561 ireason
|= ATAPI_COD
;
562 ireason
&= ~ATAPI_IO
;
569 static int ide_delayed_transfer_pc(ide_drive_t
*drive
)
571 /* Send the actual packet */
572 drive
->hwif
->tp_ops
->output_data(drive
, NULL
, drive
->pc
->c
, 12);
574 /* Timeout for the packet command */
575 return WAIT_FLOPPY_CMD
;
578 static ide_startstop_t
ide_transfer_pc(ide_drive_t
*drive
)
580 struct ide_atapi_pc
*uninitialized_var(pc
);
581 ide_hwif_t
*hwif
= drive
->hwif
;
582 struct request
*rq
= hwif
->rq
;
583 ide_expiry_t
*expiry
;
584 unsigned int timeout
;
586 ide_startstop_t startstop
;
589 if (ide_wait_stat(&startstop
, drive
, ATA_DRQ
, ATA_BUSY
, WAIT_READY
)) {
590 printk(KERN_ERR PFX
"%s: Strange, packet command initiated yet "
591 "DRQ isn't asserted\n", drive
->name
);
595 if (drive
->atapi_flags
& IDE_AFLAG_DRQ_INTERRUPT
) {
597 drive
->waiting_for_dma
= 1;
600 if (dev_is_idecd(drive
)) {
601 /* ATAPI commands get padded out to 12 bytes minimum */
602 cmd_len
= COMMAND_SIZE(rq
->cmd
[0]);
603 if (cmd_len
< ATAPI_MIN_CDB_BYTES
)
604 cmd_len
= ATAPI_MIN_CDB_BYTES
;
606 timeout
= rq
->timeout
;
607 expiry
= ide_cd_expiry
;
611 cmd_len
= ATAPI_MIN_CDB_BYTES
;
614 * If necessary schedule the packet transfer to occur 'timeout'
615 * milliseconds later in ide_delayed_transfer_pc() after the
616 * device says it's ready for a packet.
618 if (drive
->atapi_flags
& IDE_AFLAG_ZIP_DRIVE
) {
619 timeout
= drive
->pc_delay
;
620 expiry
= &ide_delayed_transfer_pc
;
622 timeout
= (drive
->media
== ide_floppy
) ? WAIT_FLOPPY_CMD
627 ireason
= ide_read_ireason(drive
);
628 if (drive
->media
== ide_tape
)
629 ireason
= ide_wait_ireason(drive
, ireason
);
631 if ((ireason
& ATAPI_COD
) == 0 || (ireason
& ATAPI_IO
)) {
632 printk(KERN_ERR PFX
"%s: (IO,CoD) != (0,1) while "
633 "issuing a packet command\n", drive
->name
);
635 return ide_do_reset(drive
);
639 hwif
->expiry
= expiry
;
641 /* Set the interrupt routine */
642 ide_set_handler(drive
,
643 (dev_is_idecd(drive
) ? drive
->irq_handler
647 /* Send the actual packet */
648 if ((drive
->atapi_flags
& IDE_AFLAG_ZIP_DRIVE
) == 0)
649 hwif
->tp_ops
->output_data(drive
, NULL
, rq
->cmd
, cmd_len
);
651 /* Begin DMA, if necessary */
652 if (dev_is_idecd(drive
)) {
654 hwif
->dma_ops
->dma_start(drive
);
656 if (pc
->flags
& PC_FLAG_DMA_OK
) {
657 pc
->flags
|= PC_FLAG_DMA_IN_PROGRESS
;
658 hwif
->dma_ops
->dma_start(drive
);
665 ide_startstop_t
ide_issue_pc(ide_drive_t
*drive
, struct ide_cmd
*cmd
)
667 struct ide_atapi_pc
*pc
;
668 ide_hwif_t
*hwif
= drive
->hwif
;
669 ide_expiry_t
*expiry
= NULL
;
670 struct request
*rq
= hwif
->rq
;
671 unsigned int timeout
, bytes
;
674 u8 drq_int
= !!(drive
->atapi_flags
& IDE_AFLAG_DRQ_INTERRUPT
);
676 if (dev_is_idecd(drive
)) {
677 valid_tf
= IDE_VALID_NSECT
| IDE_VALID_LBAL
;
678 bcount
= ide_cd_get_xferlen(rq
);
679 expiry
= ide_cd_expiry
;
680 timeout
= ATAPI_WAIT_PC
;
683 drive
->dma
= !ide_dma_prepare(drive
, cmd
);
687 valid_tf
= IDE_VALID_DEVICE
;
688 bytes
= blk_rq_bytes(rq
);
689 bcount
= ((drive
->media
== ide_tape
) ? bytes
690 : min_t(unsigned int,
693 /* We haven't transferred any data yet */
694 rq
->resid_len
= bcount
;
696 if (pc
->flags
& PC_FLAG_DMA_ERROR
) {
697 pc
->flags
&= ~PC_FLAG_DMA_ERROR
;
701 if (pc
->flags
& PC_FLAG_DMA_OK
)
702 drive
->dma
= !ide_dma_prepare(drive
, cmd
);
705 pc
->flags
&= ~PC_FLAG_DMA_OK
;
707 timeout
= (drive
->media
== ide_floppy
) ? WAIT_FLOPPY_CMD
711 ide_init_packet_cmd(cmd
, valid_tf
, bcount
, drive
->dma
);
713 (void)do_rw_taskfile(drive
, cmd
);
717 drive
->waiting_for_dma
= 0;
718 hwif
->expiry
= expiry
;
721 ide_execute_command(drive
, cmd
, ide_transfer_pc
, timeout
);
723 return drq_int
? ide_started
: ide_transfer_pc(drive
);
725 EXPORT_SYMBOL_GPL(ide_issue_pc
);