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)
17 /* TODO: unify the code thus making some arguments go away */
18 ide_startstop_t
ide_pc_intr(ide_drive_t
*drive
, struct ide_atapi_pc
*pc
,
19 ide_handler_t
*handler
, unsigned int timeout
, ide_expiry_t
*expiry
,
20 void (*update_buffers
)(ide_drive_t
*, struct ide_atapi_pc
*),
21 void (*retry_pc
)(ide_drive_t
*), void (*dsc_handle
)(ide_drive_t
*),
22 void (*io_buffers
)(ide_drive_t
*, struct ide_atapi_pc
*, unsigned, int))
24 ide_hwif_t
*hwif
= drive
->hwif
;
25 const struct ide_tp_ops
*tp_ops
= hwif
->tp_ops
;
26 xfer_func_t
*xferfunc
;
29 u8 stat
, ireason
, scsi
= drive
->scsi
;
31 debug_log("Enter %s - interrupt handler\n", __func__
);
33 if (pc
->flags
& PC_FLAG_TIMEDOUT
) {
34 if (drive
->media
== ide_floppy
|| drive
->media
== ide_tape
)
35 drive
->pc_callback(drive
);
41 /* Clear the interrupt */
42 stat
= tp_ops
->read_status(hwif
);
44 if (pc
->flags
& PC_FLAG_DMA_IN_PROGRESS
) {
45 if (hwif
->dma_ops
->dma_end(drive
) ||
46 (drive
->media
== ide_tape
&& !scsi
&& (stat
& ERR_STAT
))) {
47 if (drive
->media
== ide_floppy
&& !scsi
)
48 printk(KERN_ERR
"%s: DMA %s error\n",
49 drive
->name
, rq_data_dir(pc
->rq
)
51 pc
->flags
|= PC_FLAG_DMA_ERROR
;
53 pc
->xferred
= pc
->req_xfer
;
55 update_buffers(drive
, pc
);
57 debug_log("%s: DMA finished\n", drive
->name
);
60 /* No more interrupts */
61 if ((stat
& DRQ_STAT
) == 0) {
62 debug_log("Packet command completed, %d bytes transferred\n",
65 pc
->flags
&= ~PC_FLAG_DMA_IN_PROGRESS
;
67 local_irq_enable_in_hardirq();
69 if (drive
->media
== ide_tape
&& !scsi
&&
70 (stat
& ERR_STAT
) && pc
->c
[0] == REQUEST_SENSE
)
72 if ((stat
& ERR_STAT
) || (pc
->flags
& PC_FLAG_DMA_ERROR
)) {
74 debug_log("%s: I/O error\n", drive
->name
);
76 if (drive
->media
!= ide_tape
|| scsi
) {
82 if (pc
->c
[0] == REQUEST_SENSE
) {
83 printk(KERN_ERR
"%s: I/O error in request sense"
84 " command\n", drive
->name
);
85 return ide_do_reset(drive
);
88 debug_log("[cmd %x]: check condition\n", pc
->c
[0]);
92 /* queued, but not started */
97 if ((pc
->flags
& PC_FLAG_WAIT_FOR_DSC
) &&
98 (stat
& SEEK_STAT
) == 0) {
102 /* Command finished - Call the callback function */
103 if (drive
->media
== ide_floppy
|| drive
->media
== ide_tape
)
104 drive
->pc_callback(drive
);
110 if (pc
->flags
& PC_FLAG_DMA_IN_PROGRESS
) {
111 pc
->flags
&= ~PC_FLAG_DMA_IN_PROGRESS
;
112 printk(KERN_ERR
"%s: The device wants to issue more interrupts "
113 "in DMA mode\n", drive
->name
);
115 return ide_do_reset(drive
);
118 /* Get the number of bytes to transfer on this interrupt. */
119 ide_read_bcount_and_ireason(drive
, &bcount
, &ireason
);
122 printk(KERN_ERR
"%s: CoD != 0 in %s\n", drive
->name
, __func__
);
123 return ide_do_reset(drive
);
125 if (((ireason
& IO
) == IO
) == !!(pc
->flags
& PC_FLAG_WRITING
)) {
126 /* Hopefully, we will never get here */
127 printk(KERN_ERR
"%s: We wanted to %s, but the device wants us "
128 "to %s!\n", drive
->name
,
129 (ireason
& IO
) ? "Write" : "Read",
130 (ireason
& IO
) ? "Read" : "Write");
131 return ide_do_reset(drive
);
133 if (!(pc
->flags
& PC_FLAG_WRITING
)) {
134 /* Reading - Check that we have enough space */
135 temp
= pc
->xferred
+ bcount
;
136 if (temp
> pc
->req_xfer
) {
137 if (temp
> pc
->buf_size
) {
138 printk(KERN_ERR
"%s: The device wants to send "
139 "us more data than expected - "
143 temp
= pc
->buf_size
- pc
->xferred
;
148 io_buffers(drive
, pc
, temp
, 0);
150 tp_ops
->input_data(drive
, NULL
,
152 printk(KERN_ERR
"%s: transferred %d of "
159 ide_pad_transfer(drive
, 0, bcount
- temp
);
160 ide_set_handler(drive
, handler
, timeout
,
164 debug_log("The device wants to send us more data than "
165 "expected - allowing transfer\n");
167 xferfunc
= tp_ops
->input_data
;
169 xferfunc
= tp_ops
->output_data
;
171 if ((drive
->media
== ide_floppy
&& !scsi
&& !pc
->buf
) ||
172 (drive
->media
== ide_tape
&& !scsi
&& pc
->bh
) ||
174 io_buffers(drive
, pc
, bcount
, !!(pc
->flags
& PC_FLAG_WRITING
));
176 xferfunc(drive
, NULL
, pc
->cur_pos
, bcount
);
178 /* Update the current position */
179 pc
->xferred
+= bcount
;
180 pc
->cur_pos
+= bcount
;
182 debug_log("[cmd %x] transferred %d bytes on that intr.\n",
185 /* And set the interrupt handler again */
186 ide_set_handler(drive
, handler
, timeout
, expiry
);
189 EXPORT_SYMBOL_GPL(ide_pc_intr
);
191 static u8
ide_read_ireason(ide_drive_t
*drive
)
195 memset(&task
, 0, sizeof(task
));
196 task
.tf_flags
= IDE_TFLAG_IN_NSECT
;
198 drive
->hwif
->tp_ops
->tf_read(drive
, &task
);
200 return task
.tf
.nsect
& 3;
203 static u8
ide_wait_ireason(ide_drive_t
*drive
, u8 ireason
)
207 while (retries
-- && ((ireason
& CD
) == 0 || (ireason
& IO
))) {
208 printk(KERN_ERR
"%s: (IO,CoD != (0,1) while issuing "
209 "a packet command, retrying\n", drive
->name
);
211 ireason
= ide_read_ireason(drive
);
213 printk(KERN_ERR
"%s: (IO,CoD != (0,1) while issuing "
214 "a packet command, ignoring\n",
224 ide_startstop_t
ide_transfer_pc(ide_drive_t
*drive
, struct ide_atapi_pc
*pc
,
225 ide_handler_t
*handler
, unsigned int timeout
,
226 ide_expiry_t
*expiry
)
228 ide_hwif_t
*hwif
= drive
->hwif
;
229 ide_startstop_t startstop
;
232 if (ide_wait_stat(&startstop
, drive
, DRQ_STAT
, BUSY_STAT
, WAIT_READY
)) {
233 printk(KERN_ERR
"%s: Strange, packet command initiated yet "
234 "DRQ isn't asserted\n", drive
->name
);
238 ireason
= ide_read_ireason(drive
);
239 if (drive
->media
== ide_tape
&& !drive
->scsi
)
240 ireason
= ide_wait_ireason(drive
, ireason
);
242 if ((ireason
& CD
) == 0 || (ireason
& IO
)) {
243 printk(KERN_ERR
"%s: (IO,CoD) != (0,1) while issuing "
244 "a packet command\n", drive
->name
);
245 return ide_do_reset(drive
);
248 /* Set the interrupt routine */
249 ide_set_handler(drive
, handler
, timeout
, expiry
);
251 /* Begin DMA, if necessary */
252 if (pc
->flags
& PC_FLAG_DMA_OK
) {
253 pc
->flags
|= PC_FLAG_DMA_IN_PROGRESS
;
254 hwif
->dma_ops
->dma_start(drive
);
257 /* Send the actual packet */
258 if ((pc
->flags
& PC_FLAG_ZIP_DRIVE
) == 0)
259 hwif
->tp_ops
->output_data(drive
, NULL
, pc
->c
, 12);
263 EXPORT_SYMBOL_GPL(ide_transfer_pc
);
265 ide_startstop_t
ide_issue_pc(ide_drive_t
*drive
, struct ide_atapi_pc
*pc
,
266 ide_handler_t
*handler
, unsigned int timeout
,
267 ide_expiry_t
*expiry
)
269 ide_hwif_t
*hwif
= drive
->hwif
;
273 /* We haven't transferred any data yet */
275 pc
->cur_pos
= pc
->buf
;
277 /* Request to transfer the entire buffer at once */
278 if (drive
->media
== ide_tape
&& !drive
->scsi
)
279 bcount
= pc
->req_xfer
;
281 bcount
= min(pc
->req_xfer
, 63 * 1024);
283 if (pc
->flags
& PC_FLAG_DMA_ERROR
) {
284 pc
->flags
&= ~PC_FLAG_DMA_ERROR
;
288 if ((pc
->flags
& PC_FLAG_DMA_OK
) && drive
->using_dma
) {
291 dma
= !hwif
->dma_ops
->dma_setup(drive
);
297 pc
->flags
&= ~PC_FLAG_DMA_OK
;
299 ide_pktcmd_tf_load(drive
, drive
->scsi
? 0 : IDE_TFLAG_OUT_DEVICE
,
302 /* Issue the packet command */
303 if (pc
->flags
& PC_FLAG_DRQ_INTERRUPT
) {
304 ide_execute_command(drive
, WIN_PACKETCMD
, handler
,
308 ide_execute_pkt_cmd(drive
);
309 return (*handler
)(drive
);
312 EXPORT_SYMBOL_GPL(ide_issue_pc
);