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 drive->pc_callback(drive);
38 /* Clear the interrupt */
39 stat = tp_ops->read_status(hwif);
41 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
42 if (hwif->dma_ops->dma_end(drive) ||
43 (drive->media == ide_tape && !scsi && (stat & ERR_STAT))) {
44 if (drive->media == ide_floppy && !scsi)
45 printk(KERN_ERR "%s: DMA %s error\n",
46 drive->name, rq_data_dir(pc->rq)
48 pc->flags |= PC_FLAG_DMA_ERROR;
50 pc->xferred = pc->req_xfer;
52 update_buffers(drive, pc);
54 debug_log("%s: DMA finished\n", drive->name);
57 /* No more interrupts */
58 if ((stat & DRQ_STAT) == 0) {
59 debug_log("Packet command completed, %d bytes transferred\n",
62 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
64 local_irq_enable_in_hardirq();
66 if (drive->media == ide_tape && !scsi &&
67 (stat & ERR_STAT) && pc->c[0] == REQUEST_SENSE)
69 if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) {
71 debug_log("%s: I/O error\n", drive->name);
73 if (drive->media != ide_tape || scsi) {
79 if (pc->c[0] == REQUEST_SENSE) {
80 printk(KERN_ERR "%s: I/O error in request sense"
81 " command\n", drive->name);
82 return ide_do_reset(drive);
85 debug_log("[cmd %x]: check condition\n", pc->c[0]);
89 /* queued, but not started */
94 if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) &&
95 (stat & SEEK_STAT) == 0) {
99 /* Command finished - Call the callback function */
100 drive->pc_callback(drive);
104 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
105 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
106 printk(KERN_ERR "%s: The device wants to issue more interrupts "
107 "in DMA mode\n", drive->name);
109 return ide_do_reset(drive);
112 /* Get the number of bytes to transfer on this interrupt. */
113 ide_read_bcount_and_ireason(drive, &bcount, &ireason);
116 printk(KERN_ERR "%s: CoD != 0 in %s\n", drive->name, __func__);
117 return ide_do_reset(drive);
119 if (((ireason & IO) == IO) == !!(pc->flags & PC_FLAG_WRITING)) {
120 /* Hopefully, we will never get here */
121 printk(KERN_ERR "%s: We wanted to %s, but the device wants us "
122 "to %s!\n", drive->name,
123 (ireason & IO) ? "Write" : "Read",
124 (ireason & IO) ? "Read" : "Write");
125 return ide_do_reset(drive);
127 if (!(pc->flags & PC_FLAG_WRITING)) {
128 /* Reading - Check that we have enough space */
129 temp = pc->xferred + bcount;
130 if (temp > pc->req_xfer) {
131 if (temp > pc->buf_size) {
132 printk(KERN_ERR "%s: The device wants to send "
133 "us more data than expected - "
137 temp = pc->buf_size - pc->xferred;
142 io_buffers(drive, pc, temp, 0);
144 tp_ops->input_data(drive, NULL,
146 printk(KERN_ERR "%s: transferred %d of "
153 ide_pad_transfer(drive, 0, bcount - temp);
154 ide_set_handler(drive, handler, timeout,
158 debug_log("The device wants to send us more data than "
159 "expected - allowing transfer\n");
161 xferfunc = tp_ops->input_data;
163 xferfunc = tp_ops->output_data;
165 if ((drive->media == ide_floppy && !scsi && !pc->buf) ||
166 (drive->media == ide_tape && !scsi && pc->bh) ||
168 io_buffers(drive, pc, bcount, !!(pc->flags & PC_FLAG_WRITING));
170 xferfunc(drive, NULL, pc->cur_pos, bcount);
172 /* Update the current position */
173 pc->xferred += bcount;
174 pc->cur_pos += bcount;
176 debug_log("[cmd %x] transferred %d bytes on that intr.\n",
179 /* And set the interrupt handler again */
180 ide_set_handler(drive, handler, timeout, expiry);
183 EXPORT_SYMBOL_GPL(ide_pc_intr);
185 static u8 ide_read_ireason(ide_drive_t *drive)
189 memset(&task, 0, sizeof(task));
190 task.tf_flags = IDE_TFLAG_IN_NSECT;
192 drive->hwif->tp_ops->tf_read(drive, &task);
194 return task.tf.nsect & 3;
197 static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
201 while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
202 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
203 "a packet command, retrying\n", drive->name);
205 ireason = ide_read_ireason(drive);
207 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
208 "a packet command, ignoring\n",
218 ide_startstop_t ide_transfer_pc(ide_drive_t *drive, struct ide_atapi_pc *pc,
219 ide_handler_t *handler, unsigned int timeout,
220 ide_expiry_t *expiry)
222 ide_hwif_t *hwif = drive->hwif;
223 ide_startstop_t startstop;
226 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
227 printk(KERN_ERR "%s: Strange, packet command initiated yet "
228 "DRQ isn't asserted\n", drive->name);
232 ireason = ide_read_ireason(drive);
233 if (drive->media == ide_tape && !drive->scsi)
234 ireason = ide_wait_ireason(drive, ireason);
236 if ((ireason & CD) == 0 || (ireason & IO)) {
237 printk(KERN_ERR "%s: (IO,CoD) != (0,1) while issuing "
238 "a packet command\n", drive->name);
239 return ide_do_reset(drive);
242 /* Set the interrupt routine */
243 ide_set_handler(drive, handler, timeout, expiry);
245 /* Begin DMA, if necessary */
246 if (pc->flags & PC_FLAG_DMA_OK) {
247 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
248 hwif->dma_ops->dma_start(drive);
251 /* Send the actual packet */
252 if ((pc->flags & PC_FLAG_ZIP_DRIVE) == 0)
253 hwif->tp_ops->output_data(drive, NULL, pc->c, 12);
257 EXPORT_SYMBOL_GPL(ide_transfer_pc);
259 ide_startstop_t ide_issue_pc(ide_drive_t *drive, struct ide_atapi_pc *pc,
260 ide_handler_t *handler, unsigned int timeout,
261 ide_expiry_t *expiry)
263 ide_hwif_t *hwif = drive->hwif;
267 /* We haven't transferred any data yet */
269 pc->cur_pos = pc->buf;
271 /* Request to transfer the entire buffer at once */
272 if (drive->media == ide_tape && !drive->scsi)
273 bcount = pc->req_xfer;
275 bcount = min(pc->req_xfer, 63 * 1024);
277 if (pc->flags & PC_FLAG_DMA_ERROR) {
278 pc->flags &= ~PC_FLAG_DMA_ERROR;
282 if ((pc->flags & PC_FLAG_DMA_OK) && drive->using_dma) {
285 dma = !hwif->dma_ops->dma_setup(drive);
291 pc->flags &= ~PC_FLAG_DMA_OK;
293 ide_pktcmd_tf_load(drive, drive->scsi ? 0 : IDE_TFLAG_OUT_DEVICE,
296 /* Issue the packet command */
297 if (pc->flags & PC_FLAG_DRQ_INTERRUPT) {
298 ide_execute_command(drive, WIN_PACKETCMD, handler,
302 ide_execute_pkt_cmd(drive);
303 return (*handler)(drive);
306 EXPORT_SYMBOL_GPL(ide_issue_pc);