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 xfer_func_t
*xferfunc
;
28 u8 stat
, ireason
, scsi
= drive
->scsi
;
30 debug_log("Enter %s - interrupt handler\n", __func__
);
32 if (pc
->flags
& PC_FLAG_TIMEDOUT
) {
37 /* Clear the interrupt */
38 stat
= ide_read_status(drive
);
40 if (pc
->flags
& PC_FLAG_DMA_IN_PROGRESS
) {
41 if (hwif
->dma_ops
->dma_end(drive
) ||
42 (drive
->media
== ide_tape
&& !scsi
&& (stat
& ERR_STAT
))) {
43 if (drive
->media
== ide_floppy
&& !scsi
)
44 printk(KERN_ERR
"%s: DMA %s error\n",
45 drive
->name
, rq_data_dir(pc
->rq
)
47 pc
->flags
|= PC_FLAG_DMA_ERROR
;
49 pc
->xferred
= pc
->req_xfer
;
51 update_buffers(drive
, pc
);
53 debug_log("%s: DMA finished\n", drive
->name
);
56 /* No more interrupts */
57 if ((stat
& DRQ_STAT
) == 0) {
58 debug_log("Packet command completed, %d bytes transferred\n",
61 pc
->flags
&= ~PC_FLAG_DMA_IN_PROGRESS
;
63 local_irq_enable_in_hardirq();
65 if (drive
->media
== ide_tape
&& !scsi
&&
66 (stat
& ERR_STAT
) && pc
->c
[0] == REQUEST_SENSE
)
68 if ((stat
& ERR_STAT
) || (pc
->flags
& PC_FLAG_DMA_ERROR
)) {
70 debug_log("%s: I/O error\n", drive
->name
);
72 if (drive
->media
!= ide_tape
|| scsi
) {
78 if (pc
->c
[0] == REQUEST_SENSE
) {
79 printk(KERN_ERR
"%s: I/O error in request sense"
80 " command\n", drive
->name
);
81 return ide_do_reset(drive
);
84 debug_log("[cmd %x]: check condition\n", pc
->c
[0]);
88 /* queued, but not started */
93 if ((pc
->flags
& PC_FLAG_WAIT_FOR_DSC
) &&
94 (stat
& SEEK_STAT
) == 0) {
98 /* Command finished - Call the callback function */
103 if (pc
->flags
& PC_FLAG_DMA_IN_PROGRESS
) {
104 pc
->flags
&= ~PC_FLAG_DMA_IN_PROGRESS
;
105 printk(KERN_ERR
"%s: The device wants to issue more interrupts "
106 "in DMA mode\n", drive
->name
);
108 return ide_do_reset(drive
);
110 /* Get the number of bytes to transfer on this interrupt. */
111 bcount
= (hwif
->INB(hwif
->io_ports
.lbah_addr
) << 8) |
112 hwif
->INB(hwif
->io_ports
.lbam_addr
);
114 ireason
= hwif
->INB(hwif
->io_ports
.nsect_addr
);
117 printk(KERN_ERR
"%s: CoD != 0 in %s\n", drive
->name
, __func__
);
118 return ide_do_reset(drive
);
120 if (((ireason
& IO
) == IO
) == !!(pc
->flags
& PC_FLAG_WRITING
)) {
121 /* Hopefully, we will never get here */
122 printk(KERN_ERR
"%s: We wanted to %s, but the device wants us "
123 "to %s!\n", drive
->name
,
124 (ireason
& IO
) ? "Write" : "Read",
125 (ireason
& IO
) ? "Read" : "Write");
126 return ide_do_reset(drive
);
128 if (!(pc
->flags
& PC_FLAG_WRITING
)) {
129 /* Reading - Check that we have enough space */
130 temp
= pc
->xferred
+ bcount
;
131 if (temp
> pc
->req_xfer
) {
132 if (temp
> pc
->buf_size
) {
133 printk(KERN_ERR
"%s: The device wants to send "
134 "us more data than expected - "
138 temp
= pc
->buf_size
- pc
->xferred
;
143 io_buffers(drive
, pc
, temp
, 0);
145 hwif
->input_data(drive
, NULL
,
147 printk(KERN_ERR
"%s: transferred %d of "
154 ide_pad_transfer(drive
, 0, bcount
- temp
);
155 ide_set_handler(drive
, handler
, timeout
,
159 debug_log("The device wants to send us more data than "
160 "expected - allowing transfer\n");
162 xferfunc
= hwif
->input_data
;
164 xferfunc
= hwif
->output_data
;
166 if ((drive
->media
== ide_floppy
&& !scsi
&& !pc
->buf
) ||
167 (drive
->media
== ide_tape
&& !scsi
&& pc
->bh
) ||
169 io_buffers(drive
, pc
, bcount
, !!(pc
->flags
& PC_FLAG_WRITING
));
171 xferfunc(drive
, NULL
, pc
->cur_pos
, bcount
);
173 /* Update the current position */
174 pc
->xferred
+= bcount
;
175 pc
->cur_pos
+= bcount
;
177 debug_log("[cmd %x] transferred %d bytes on that intr.\n",
180 /* And set the interrupt handler again */
181 ide_set_handler(drive
, handler
, timeout
, expiry
);
184 EXPORT_SYMBOL_GPL(ide_pc_intr
);
186 static u8
ide_wait_ireason(ide_drive_t
*drive
, u8 ireason
)
188 ide_hwif_t
*hwif
= drive
->hwif
;
191 while (retries
-- && ((ireason
& CD
) == 0 || (ireason
& IO
))) {
192 printk(KERN_ERR
"%s: (IO,CoD != (0,1) while issuing "
193 "a packet command, retrying\n", drive
->name
);
195 ireason
= hwif
->INB(hwif
->io_ports
.nsect_addr
);
197 printk(KERN_ERR
"%s: (IO,CoD != (0,1) while issuing "
198 "a packet command, ignoring\n",
208 ide_startstop_t
ide_transfer_pc(ide_drive_t
*drive
, struct ide_atapi_pc
*pc
,
209 ide_handler_t
*handler
, unsigned int timeout
,
210 ide_expiry_t
*expiry
)
212 ide_hwif_t
*hwif
= drive
->hwif
;
213 ide_startstop_t startstop
;
216 if (ide_wait_stat(&startstop
, drive
, DRQ_STAT
, BUSY_STAT
, WAIT_READY
)) {
217 printk(KERN_ERR
"%s: Strange, packet command initiated yet "
218 "DRQ isn't asserted\n", drive
->name
);
222 ireason
= hwif
->INB(hwif
->io_ports
.nsect_addr
);
223 if (drive
->media
== ide_tape
&& !drive
->scsi
)
224 ireason
= ide_wait_ireason(drive
, ireason
);
226 if ((ireason
& CD
) == 0 || (ireason
& IO
)) {
227 printk(KERN_ERR
"%s: (IO,CoD) != (0,1) while issuing "
228 "a packet command\n", drive
->name
);
229 return ide_do_reset(drive
);
232 /* Set the interrupt routine */
233 ide_set_handler(drive
, handler
, timeout
, expiry
);
235 /* Begin DMA, if necessary */
236 if (pc
->flags
& PC_FLAG_DMA_OK
) {
237 pc
->flags
|= PC_FLAG_DMA_IN_PROGRESS
;
238 hwif
->dma_ops
->dma_start(drive
);
241 /* Send the actual packet */
242 if ((pc
->flags
& PC_FLAG_ZIP_DRIVE
) == 0)
243 hwif
->output_data(drive
, NULL
, pc
->c
, 12);
247 EXPORT_SYMBOL_GPL(ide_transfer_pc
);
249 ide_startstop_t
ide_issue_pc(ide_drive_t
*drive
, struct ide_atapi_pc
*pc
,
250 ide_handler_t
*handler
, unsigned int timeout
,
251 ide_expiry_t
*expiry
)
253 ide_hwif_t
*hwif
= drive
->hwif
;
257 /* We haven't transferred any data yet */
259 pc
->cur_pos
= pc
->buf
;
261 /* Request to transfer the entire buffer at once */
262 if (drive
->media
== ide_tape
&& !drive
->scsi
)
263 bcount
= pc
->req_xfer
;
265 bcount
= min(pc
->req_xfer
, 63 * 1024);
267 if (pc
->flags
& PC_FLAG_DMA_ERROR
) {
268 pc
->flags
&= ~PC_FLAG_DMA_ERROR
;
272 if ((pc
->flags
& PC_FLAG_DMA_OK
) && drive
->using_dma
) {
275 dma
= !hwif
->dma_ops
->dma_setup(drive
);
281 pc
->flags
&= ~PC_FLAG_DMA_OK
;
283 ide_pktcmd_tf_load(drive
, drive
->scsi
? 0 : IDE_TFLAG_OUT_DEVICE
,
286 /* Issue the packet command */
287 if (pc
->flags
& PC_FLAG_DRQ_INTERRUPT
) {
288 ide_execute_command(drive
, WIN_PACKETCMD
, handler
,
292 ide_execute_pkt_cmd(drive
);
293 return (*handler
)(drive
);
296 EXPORT_SYMBOL_GPL(ide_issue_pc
);