5 #include <linux/kernel.h>
6 #include <linux/delay.h>
11 #define debug_log(fmt, args...) \
12 printk(KERN_INFO "ide: " fmt, ## args)
14 #define debug_log(fmt, args...) do {} while (0)
18 * Check whether we can support a device,
19 * based on the ATAPI IDENTIFY command results.
21 int ide_check_atapi_device(ide_drive_t
*drive
, const char *s
)
24 u8 gcw
[2], protocol
, device_type
, removable
, drq_type
, packet_size
;
26 *((u16
*)&gcw
) = id
[ATA_ID_CONFIG
];
28 protocol
= (gcw
[1] & 0xC0) >> 6;
29 device_type
= gcw
[1] & 0x1F;
30 removable
= (gcw
[0] & 0x80) >> 7;
31 drq_type
= (gcw
[0] & 0x60) >> 5;
32 packet_size
= gcw
[0] & 0x03;
35 /* kludge for Apple PowerBook internal zip */
36 if (drive
->media
== ide_floppy
&& device_type
== 5 &&
37 !strstr((char *)&id
[ATA_ID_PROD
], "CD-ROM") &&
38 strstr((char *)&id
[ATA_ID_PROD
], "ZIP"))
43 printk(KERN_ERR
"%s: %s: protocol (0x%02x) is not ATAPI\n",
44 s
, drive
->name
, protocol
);
45 else if ((drive
->media
== ide_floppy
&& device_type
!= 0) ||
46 (drive
->media
== ide_tape
&& device_type
!= 1))
47 printk(KERN_ERR
"%s: %s: invalid device type (0x%02x)\n",
48 s
, drive
->name
, device_type
);
49 else if (removable
== 0)
50 printk(KERN_ERR
"%s: %s: the removable flag is not set\n",
52 else if (drive
->media
== ide_floppy
&& drq_type
== 3)
53 printk(KERN_ERR
"%s: %s: sorry, DRQ type (0x%02x) not "
54 "supported\n", s
, drive
->name
, drq_type
);
55 else if (packet_size
!= 0)
56 printk(KERN_ERR
"%s: %s: packet size (0x%02x) is not 12 "
57 "bytes\n", s
, drive
->name
, packet_size
);
62 EXPORT_SYMBOL_GPL(ide_check_atapi_device
);
64 /* PIO data transfer routine using the scatter gather table. */
65 int ide_io_buffers(ide_drive_t
*drive
, struct ide_atapi_pc
*pc
,
66 unsigned int bcount
, int write
)
68 ide_hwif_t
*hwif
= drive
->hwif
;
69 const struct ide_tp_ops
*tp_ops
= hwif
->tp_ops
;
70 xfer_func_t
*xf
= write
? tp_ops
->output_data
: tp_ops
->input_data
;
71 struct scatterlist
*sg
= pc
->sg
;
76 count
= min(sg
->length
- pc
->b_count
, bcount
);
78 if (PageHighMem(sg_page(sg
))) {
81 local_irq_save(flags
);
82 buf
= kmap_atomic(sg_page(sg
), KM_IRQ0
) + sg
->offset
;
83 xf(drive
, NULL
, buf
+ pc
->b_count
, count
);
84 kunmap_atomic(buf
- sg
->offset
, KM_IRQ0
);
85 local_irq_restore(flags
);
88 xf(drive
, NULL
, buf
+ pc
->b_count
, count
);
95 if (pc
->b_count
== sg
->length
) {
98 pc
->sg
= sg
= sg_next(sg
);
104 printk(KERN_ERR
"%s: %d leftover bytes, %s\n", drive
->name
,
105 bcount
, write
? "padding with zeros"
106 : "discarding data");
107 ide_pad_transfer(drive
, write
, bcount
);
112 EXPORT_SYMBOL_GPL(ide_io_buffers
);
114 void ide_init_pc(struct ide_atapi_pc
*pc
)
116 memset(pc
, 0, sizeof(*pc
));
117 pc
->buf
= pc
->pc_buf
;
118 pc
->buf_size
= IDE_PC_BUFFER_SIZE
;
120 EXPORT_SYMBOL_GPL(ide_init_pc
);
123 * Generate a new packet command request in front of the request queue, before
124 * the current request, so that it will be processed immediately, on the next
125 * pass through the driver.
127 static void ide_queue_pc_head(ide_drive_t
*drive
, struct gendisk
*disk
,
128 struct ide_atapi_pc
*pc
, struct request
*rq
)
130 blk_rq_init(NULL
, rq
);
131 rq
->cmd_type
= REQ_TYPE_SPECIAL
;
132 rq
->cmd_flags
|= REQ_PREEMPT
;
133 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 ide_do_drive_cmd(drive
, rq
);
142 * Add a special packet command request to the tail of the request queue,
143 * and wait for it to be serviced.
145 int ide_queue_pc_tail(ide_drive_t
*drive
, struct gendisk
*disk
,
146 struct ide_atapi_pc
*pc
)
151 rq
= blk_get_request(drive
->queue
, READ
, __GFP_WAIT
);
152 rq
->cmd_type
= REQ_TYPE_SPECIAL
;
153 rq
->buffer
= (char *)pc
;
154 memcpy(rq
->cmd
, pc
->c
, 12);
155 if (drive
->media
== ide_tape
)
156 rq
->cmd
[13] = REQ_IDETAPE_PC1
;
157 error
= blk_execute_rq(drive
->queue
, disk
, rq
, 0);
162 EXPORT_SYMBOL_GPL(ide_queue_pc_tail
);
164 int ide_do_test_unit_ready(ide_drive_t
*drive
, struct gendisk
*disk
)
166 struct ide_atapi_pc pc
;
169 pc
.c
[0] = TEST_UNIT_READY
;
171 return ide_queue_pc_tail(drive
, disk
, &pc
);
173 EXPORT_SYMBOL_GPL(ide_do_test_unit_ready
);
175 int ide_do_start_stop(ide_drive_t
*drive
, struct gendisk
*disk
, int start
)
177 struct ide_atapi_pc pc
;
180 pc
.c
[0] = START_STOP
;
183 if (drive
->media
== ide_tape
)
184 pc
.flags
|= PC_FLAG_WAIT_FOR_DSC
;
186 return ide_queue_pc_tail(drive
, disk
, &pc
);
188 EXPORT_SYMBOL_GPL(ide_do_start_stop
);
190 int ide_set_media_lock(ide_drive_t
*drive
, struct gendisk
*disk
, int on
)
192 struct ide_atapi_pc pc
;
194 if (drive
->atapi_flags
& IDE_AFLAG_NO_DOORLOCK
)
198 pc
.c
[0] = ALLOW_MEDIUM_REMOVAL
;
201 return ide_queue_pc_tail(drive
, disk
, &pc
);
203 EXPORT_SYMBOL_GPL(ide_set_media_lock
);
205 void ide_create_request_sense_cmd(ide_drive_t
*drive
, struct ide_atapi_pc
*pc
)
208 pc
->c
[0] = REQUEST_SENSE
;
209 if (drive
->media
== ide_floppy
) {
217 EXPORT_SYMBOL_GPL(ide_create_request_sense_cmd
);
220 * Called when an error was detected during the last packet command.
221 * We queue a request sense packet command in the head of the request list.
223 void ide_retry_pc(ide_drive_t
*drive
, struct gendisk
*disk
)
225 struct request
*rq
= &drive
->request_sense_rq
;
226 struct ide_atapi_pc
*pc
= &drive
->request_sense_pc
;
228 (void)ide_read_error(drive
);
229 ide_create_request_sense_cmd(drive
, pc
);
230 if (drive
->media
== ide_tape
)
231 set_bit(IDE_AFLAG_IGNORE_DSC
, &drive
->atapi_flags
);
232 ide_queue_pc_head(drive
, disk
, pc
, rq
);
234 EXPORT_SYMBOL_GPL(ide_retry_pc
);
236 int ide_scsi_expiry(ide_drive_t
*drive
)
238 struct ide_atapi_pc
*pc
= drive
->pc
;
240 debug_log("%s called for %lu at %lu\n", __func__
,
241 pc
->scsi_cmd
->serial_number
, jiffies
);
243 pc
->flags
|= PC_FLAG_TIMEDOUT
;
245 return 0; /* we do not want the IDE subsystem to retry */
247 EXPORT_SYMBOL_GPL(ide_scsi_expiry
);
249 /* TODO: unify the code thus making some arguments go away */
250 ide_startstop_t
ide_pc_intr(ide_drive_t
*drive
, ide_handler_t
*handler
,
251 void (*update_buffers
)(ide_drive_t
*, struct ide_atapi_pc
*),
252 int (*io_buffers
)(ide_drive_t
*, struct ide_atapi_pc
*, unsigned, int))
254 struct ide_atapi_pc
*pc
= drive
->pc
;
255 ide_hwif_t
*hwif
= drive
->hwif
;
256 struct request
*rq
= hwif
->hwgroup
->rq
;
257 const struct ide_tp_ops
*tp_ops
= hwif
->tp_ops
;
258 xfer_func_t
*xferfunc
;
259 ide_expiry_t
*expiry
;
260 unsigned int timeout
, temp
;
262 u8 stat
, ireason
, scsi
= drive
->scsi
, dsc
= 0;
264 debug_log("Enter %s - interrupt handler\n", __func__
);
267 timeout
= ide_scsi_get_timeout(pc
);
268 expiry
= ide_scsi_expiry
;
270 timeout
= (drive
->media
== ide_floppy
) ? WAIT_FLOPPY_CMD
275 if (pc
->flags
& PC_FLAG_TIMEDOUT
) {
276 drive
->pc_callback(drive
, 0);
280 /* Clear the interrupt */
281 stat
= tp_ops
->read_status(hwif
);
283 if (pc
->flags
& PC_FLAG_DMA_IN_PROGRESS
) {
284 if (hwif
->dma_ops
->dma_end(drive
) ||
285 (drive
->media
== ide_tape
&& !scsi
&& (stat
& ATA_ERR
))) {
286 if (drive
->media
== ide_floppy
&& !scsi
)
287 printk(KERN_ERR
"%s: DMA %s error\n",
288 drive
->name
, rq_data_dir(pc
->rq
)
290 pc
->flags
|= PC_FLAG_DMA_ERROR
;
292 pc
->xferred
= pc
->req_xfer
;
294 update_buffers(drive
, pc
);
296 debug_log("%s: DMA finished\n", drive
->name
);
299 /* No more interrupts */
300 if ((stat
& ATA_DRQ
) == 0) {
301 debug_log("Packet command completed, %d bytes transferred\n",
304 pc
->flags
&= ~PC_FLAG_DMA_IN_PROGRESS
;
306 local_irq_enable_in_hardirq();
308 if (drive
->media
== ide_tape
&& !scsi
&&
309 (stat
& ATA_ERR
) && rq
->cmd
[0] == REQUEST_SENSE
)
312 if ((stat
& ATA_ERR
) || (pc
->flags
& PC_FLAG_DMA_ERROR
)) {
314 debug_log("%s: I/O error\n", drive
->name
);
316 if (drive
->media
!= ide_tape
|| scsi
) {
322 if (rq
->cmd
[0] == REQUEST_SENSE
) {
323 printk(KERN_ERR
"%s: I/O error in request sense"
324 " command\n", drive
->name
);
325 return ide_do_reset(drive
);
328 debug_log("[cmd %x]: check condition\n", rq
->cmd
[0]);
330 /* Retry operation */
331 ide_retry_pc(drive
, rq
->rq_disk
);
333 /* queued, but not started */
339 if ((pc
->flags
& PC_FLAG_WAIT_FOR_DSC
) && (stat
& ATA_DSC
) == 0)
342 /* Command finished - Call the callback function */
343 drive
->pc_callback(drive
, dsc
);
348 if (pc
->flags
& PC_FLAG_DMA_IN_PROGRESS
) {
349 pc
->flags
&= ~PC_FLAG_DMA_IN_PROGRESS
;
350 printk(KERN_ERR
"%s: The device wants to issue more interrupts "
351 "in DMA mode\n", drive
->name
);
353 return ide_do_reset(drive
);
356 /* Get the number of bytes to transfer on this interrupt. */
357 ide_read_bcount_and_ireason(drive
, &bcount
, &ireason
);
359 if (ireason
& ATAPI_COD
) {
360 printk(KERN_ERR
"%s: CoD != 0 in %s\n", drive
->name
, __func__
);
361 return ide_do_reset(drive
);
364 if (((ireason
& ATAPI_IO
) == ATAPI_IO
) ==
365 !!(pc
->flags
& PC_FLAG_WRITING
)) {
366 /* Hopefully, we will never get here */
367 printk(KERN_ERR
"%s: We wanted to %s, but the device wants us "
368 "to %s!\n", drive
->name
,
369 (ireason
& ATAPI_IO
) ? "Write" : "Read",
370 (ireason
& ATAPI_IO
) ? "Read" : "Write");
371 return ide_do_reset(drive
);
374 if (!(pc
->flags
& PC_FLAG_WRITING
)) {
375 /* Reading - Check that we have enough space */
376 temp
= pc
->xferred
+ bcount
;
377 if (temp
> pc
->req_xfer
) {
378 if (temp
> pc
->buf_size
) {
379 printk(KERN_ERR
"%s: The device wants to send "
380 "us more data than expected - "
384 temp
= pc
->buf_size
- pc
->xferred
;
389 io_buffers(drive
, pc
, temp
, 0);
391 tp_ops
->input_data(drive
, NULL
,
393 printk(KERN_ERR
"%s: transferred %d of "
400 ide_pad_transfer(drive
, 0, bcount
- temp
);
403 debug_log("The device wants to send us more data than "
404 "expected - allowing transfer\n");
406 xferfunc
= tp_ops
->input_data
;
408 xferfunc
= tp_ops
->output_data
;
410 if ((drive
->media
== ide_floppy
&& !scsi
&& !pc
->buf
) ||
411 (drive
->media
== ide_tape
&& !scsi
&& pc
->bh
) ||
413 int done
= io_buffers(drive
, pc
, bcount
,
414 !!(pc
->flags
& PC_FLAG_WRITING
));
416 /* FIXME: don't do partial completions */
417 if (drive
->media
== ide_floppy
&& !scsi
)
418 ide_end_request(drive
, 1, done
>> 9);
420 xferfunc(drive
, NULL
, pc
->cur_pos
, bcount
);
422 /* Update the current position */
423 pc
->xferred
+= bcount
;
424 pc
->cur_pos
+= bcount
;
426 debug_log("[cmd %x] transferred %d bytes on that intr.\n",
429 /* And set the interrupt handler again */
430 ide_set_handler(drive
, handler
, timeout
, expiry
);
433 EXPORT_SYMBOL_GPL(ide_pc_intr
);
435 static u8
ide_read_ireason(ide_drive_t
*drive
)
439 memset(&task
, 0, sizeof(task
));
440 task
.tf_flags
= IDE_TFLAG_IN_NSECT
;
442 drive
->hwif
->tp_ops
->tf_read(drive
, &task
);
444 return task
.tf
.nsect
& 3;
447 static u8
ide_wait_ireason(ide_drive_t
*drive
, u8 ireason
)
451 while (retries
-- && ((ireason
& ATAPI_COD
) == 0 ||
452 (ireason
& ATAPI_IO
))) {
453 printk(KERN_ERR
"%s: (IO,CoD != (0,1) while issuing "
454 "a packet command, retrying\n", drive
->name
);
456 ireason
= ide_read_ireason(drive
);
458 printk(KERN_ERR
"%s: (IO,CoD != (0,1) while issuing "
459 "a packet command, ignoring\n",
461 ireason
|= ATAPI_COD
;
462 ireason
&= ~ATAPI_IO
;
469 ide_startstop_t
ide_transfer_pc(ide_drive_t
*drive
,
470 ide_handler_t
*handler
, unsigned int timeout
,
471 ide_expiry_t
*expiry
)
473 struct ide_atapi_pc
*pc
= drive
->pc
;
474 ide_hwif_t
*hwif
= drive
->hwif
;
475 struct request
*rq
= hwif
->hwgroup
->rq
;
476 ide_startstop_t startstop
;
479 if (ide_wait_stat(&startstop
, drive
, ATA_DRQ
, ATA_BUSY
, WAIT_READY
)) {
480 printk(KERN_ERR
"%s: Strange, packet command initiated yet "
481 "DRQ isn't asserted\n", drive
->name
);
485 ireason
= ide_read_ireason(drive
);
486 if (drive
->media
== ide_tape
&& !drive
->scsi
)
487 ireason
= ide_wait_ireason(drive
, ireason
);
489 if ((ireason
& ATAPI_COD
) == 0 || (ireason
& ATAPI_IO
)) {
490 printk(KERN_ERR
"%s: (IO,CoD) != (0,1) while issuing "
491 "a packet command\n", drive
->name
);
492 return ide_do_reset(drive
);
495 /* Set the interrupt routine */
496 ide_set_handler(drive
, handler
, timeout
, expiry
);
498 /* Begin DMA, if necessary */
499 if (pc
->flags
& PC_FLAG_DMA_OK
) {
500 pc
->flags
|= PC_FLAG_DMA_IN_PROGRESS
;
501 hwif
->dma_ops
->dma_start(drive
);
504 /* Send the actual packet */
505 if ((drive
->atapi_flags
& IDE_AFLAG_ZIP_DRIVE
) == 0)
506 hwif
->tp_ops
->output_data(drive
, NULL
, rq
->cmd
, 12);
510 EXPORT_SYMBOL_GPL(ide_transfer_pc
);
512 ide_startstop_t
ide_issue_pc(ide_drive_t
*drive
,
513 ide_handler_t
*handler
, unsigned int timeout
,
514 ide_expiry_t
*expiry
)
516 struct ide_atapi_pc
*pc
= drive
->pc
;
517 ide_hwif_t
*hwif
= drive
->hwif
;
521 /* We haven't transferred any data yet */
523 pc
->cur_pos
= pc
->buf
;
525 /* Request to transfer the entire buffer at once */
526 if (drive
->media
== ide_tape
&& !drive
->scsi
)
527 bcount
= pc
->req_xfer
;
529 bcount
= min(pc
->req_xfer
, 63 * 1024);
531 if (pc
->flags
& PC_FLAG_DMA_ERROR
) {
532 pc
->flags
&= ~PC_FLAG_DMA_ERROR
;
536 if ((pc
->flags
& PC_FLAG_DMA_OK
) && drive
->using_dma
) {
539 dma
= !hwif
->dma_ops
->dma_setup(drive
);
545 pc
->flags
&= ~PC_FLAG_DMA_OK
;
547 ide_pktcmd_tf_load(drive
, drive
->scsi
? 0 : IDE_TFLAG_OUT_DEVICE
,
550 /* Issue the packet command */
551 if (drive
->atapi_flags
& IDE_AFLAG_DRQ_INTERRUPT
) {
552 ide_execute_command(drive
, ATA_CMD_PACKET
, handler
,
556 ide_execute_pkt_cmd(drive
);
557 return (*handler
)(drive
);
560 EXPORT_SYMBOL_GPL(ide_issue_pc
);