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 struct request
*rq
= hwif
->hwgroup
->rq
;
26 const struct ide_tp_ops
*tp_ops
= hwif
->tp_ops
;
27 xfer_func_t
*xferfunc
;
30 u8 stat
, ireason
, scsi
= drive
->scsi
;
32 debug_log("Enter %s - interrupt handler\n", __func__
);
34 if (pc
->flags
& PC_FLAG_TIMEDOUT
) {
35 drive
->pc_callback(drive
);
39 /* Clear the interrupt */
40 stat
= tp_ops
->read_status(hwif
);
42 if (pc
->flags
& PC_FLAG_DMA_IN_PROGRESS
) {
43 if (hwif
->dma_ops
->dma_end(drive
) ||
44 (drive
->media
== ide_tape
&& !scsi
&& (stat
& ERR_STAT
))) {
45 if (drive
->media
== ide_floppy
&& !scsi
)
46 printk(KERN_ERR
"%s: DMA %s error\n",
47 drive
->name
, rq_data_dir(pc
->rq
)
49 pc
->flags
|= PC_FLAG_DMA_ERROR
;
51 pc
->xferred
= pc
->req_xfer
;
53 update_buffers(drive
, pc
);
55 debug_log("%s: DMA finished\n", drive
->name
);
58 /* No more interrupts */
59 if ((stat
& DRQ_STAT
) == 0) {
60 debug_log("Packet command completed, %d bytes transferred\n",
63 pc
->flags
&= ~PC_FLAG_DMA_IN_PROGRESS
;
65 local_irq_enable_in_hardirq();
67 if (drive
->media
== ide_tape
&& !scsi
&&
68 (stat
& ERR_STAT
) && rq
->cmd
[0] == REQUEST_SENSE
)
71 if ((stat
& ERR_STAT
) || (pc
->flags
& PC_FLAG_DMA_ERROR
)) {
73 debug_log("%s: I/O error\n", drive
->name
);
75 if (drive
->media
!= ide_tape
|| scsi
) {
81 if (rq
->cmd
[0] == REQUEST_SENSE
) {
82 printk(KERN_ERR
"%s: I/O error in request sense"
83 " command\n", drive
->name
);
84 return ide_do_reset(drive
);
87 debug_log("[cmd %x]: check condition\n", rq
->cmd
[0]);
92 /* queued, but not started */
97 if ((pc
->flags
& PC_FLAG_WAIT_FOR_DSC
) &&
98 (stat
& SEEK_STAT
) == 0) {
103 /* Command finished - Call the callback function */
104 drive
->pc_callback(drive
);
109 if (pc
->flags
& PC_FLAG_DMA_IN_PROGRESS
) {
110 pc
->flags
&= ~PC_FLAG_DMA_IN_PROGRESS
;
111 printk(KERN_ERR
"%s: The device wants to issue more interrupts "
112 "in DMA mode\n", drive
->name
);
114 return ide_do_reset(drive
);
117 /* Get the number of bytes to transfer on this interrupt. */
118 ide_read_bcount_and_ireason(drive
, &bcount
, &ireason
);
121 printk(KERN_ERR
"%s: CoD != 0 in %s\n", drive
->name
, __func__
);
122 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
);
134 if (!(pc
->flags
& PC_FLAG_WRITING
)) {
135 /* Reading - Check that we have enough space */
136 temp
= pc
->xferred
+ bcount
;
137 if (temp
> pc
->req_xfer
) {
138 if (temp
> pc
->buf_size
) {
139 printk(KERN_ERR
"%s: The device wants to send "
140 "us more data than expected - "
144 temp
= pc
->buf_size
- pc
->xferred
;
149 io_buffers(drive
, pc
, temp
, 0);
151 tp_ops
->input_data(drive
, NULL
,
153 printk(KERN_ERR
"%s: transferred %d of "
160 ide_pad_transfer(drive
, 0, bcount
- temp
);
161 ide_set_handler(drive
, handler
, timeout
,
165 debug_log("The device wants to send us more data than "
166 "expected - allowing transfer\n");
168 xferfunc
= tp_ops
->input_data
;
170 xferfunc
= tp_ops
->output_data
;
172 if ((drive
->media
== ide_floppy
&& !scsi
&& !pc
->buf
) ||
173 (drive
->media
== ide_tape
&& !scsi
&& pc
->bh
) ||
175 io_buffers(drive
, pc
, bcount
, !!(pc
->flags
& PC_FLAG_WRITING
));
177 xferfunc(drive
, NULL
, pc
->cur_pos
, bcount
);
179 /* Update the current position */
180 pc
->xferred
+= bcount
;
181 pc
->cur_pos
+= bcount
;
183 debug_log("[cmd %x] transferred %d bytes on that intr.\n",
186 /* And set the interrupt handler again */
187 ide_set_handler(drive
, handler
, timeout
, expiry
);
190 EXPORT_SYMBOL_GPL(ide_pc_intr
);
192 static u8
ide_read_ireason(ide_drive_t
*drive
)
196 memset(&task
, 0, sizeof(task
));
197 task
.tf_flags
= IDE_TFLAG_IN_NSECT
;
199 drive
->hwif
->tp_ops
->tf_read(drive
, &task
);
201 return task
.tf
.nsect
& 3;
204 static u8
ide_wait_ireason(ide_drive_t
*drive
, u8 ireason
)
208 while (retries
-- && ((ireason
& CD
) == 0 || (ireason
& IO
))) {
209 printk(KERN_ERR
"%s: (IO,CoD != (0,1) while issuing "
210 "a packet command, retrying\n", drive
->name
);
212 ireason
= ide_read_ireason(drive
);
214 printk(KERN_ERR
"%s: (IO,CoD != (0,1) while issuing "
215 "a packet command, ignoring\n",
225 ide_startstop_t
ide_transfer_pc(ide_drive_t
*drive
, struct ide_atapi_pc
*pc
,
226 ide_handler_t
*handler
, unsigned int timeout
,
227 ide_expiry_t
*expiry
)
229 ide_hwif_t
*hwif
= drive
->hwif
;
230 struct request
*rq
= hwif
->hwgroup
->rq
;
231 ide_startstop_t startstop
;
234 if (ide_wait_stat(&startstop
, drive
, DRQ_STAT
, BUSY_STAT
, WAIT_READY
)) {
235 printk(KERN_ERR
"%s: Strange, packet command initiated yet "
236 "DRQ isn't asserted\n", drive
->name
);
240 ireason
= ide_read_ireason(drive
);
241 if (drive
->media
== ide_tape
&& !drive
->scsi
)
242 ireason
= ide_wait_ireason(drive
, ireason
);
244 if ((ireason
& CD
) == 0 || (ireason
& IO
)) {
245 printk(KERN_ERR
"%s: (IO,CoD) != (0,1) while issuing "
246 "a packet command\n", drive
->name
);
247 return ide_do_reset(drive
);
250 /* Set the interrupt routine */
251 ide_set_handler(drive
, handler
, timeout
, expiry
);
253 /* Begin DMA, if necessary */
254 if (pc
->flags
& PC_FLAG_DMA_OK
) {
255 pc
->flags
|= PC_FLAG_DMA_IN_PROGRESS
;
256 hwif
->dma_ops
->dma_start(drive
);
259 /* Send the actual packet */
260 if ((drive
->atapi_flags
& IDE_AFLAG_ZIP_DRIVE
) == 0)
261 hwif
->tp_ops
->output_data(drive
, NULL
, rq
->cmd
, 12);
265 EXPORT_SYMBOL_GPL(ide_transfer_pc
);
267 ide_startstop_t
ide_issue_pc(ide_drive_t
*drive
, struct ide_atapi_pc
*pc
,
268 ide_handler_t
*handler
, unsigned int timeout
,
269 ide_expiry_t
*expiry
)
271 ide_hwif_t
*hwif
= drive
->hwif
;
275 /* We haven't transferred any data yet */
277 pc
->cur_pos
= pc
->buf
;
279 /* Request to transfer the entire buffer at once */
280 if (drive
->media
== ide_tape
&& !drive
->scsi
)
281 bcount
= pc
->req_xfer
;
283 bcount
= min(pc
->req_xfer
, 63 * 1024);
285 if (pc
->flags
& PC_FLAG_DMA_ERROR
) {
286 pc
->flags
&= ~PC_FLAG_DMA_ERROR
;
290 if ((pc
->flags
& PC_FLAG_DMA_OK
) && drive
->using_dma
) {
293 dma
= !hwif
->dma_ops
->dma_setup(drive
);
299 pc
->flags
&= ~PC_FLAG_DMA_OK
;
301 ide_pktcmd_tf_load(drive
, drive
->scsi
? 0 : IDE_TFLAG_OUT_DEVICE
,
304 /* Issue the packet command */
305 if (drive
->atapi_flags
& IDE_AFLAG_DRQ_INTERRUPT
) {
306 ide_execute_command(drive
, WIN_PACKETCMD
, handler
,
310 ide_execute_pkt_cmd(drive
);
311 return (*handler
)(drive
);
314 EXPORT_SYMBOL_GPL(ide_issue_pc
);