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 & ATA_ERR))) {
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 & ATA_DRQ) == 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 & ATA_ERR) && rq->cmd[0] == REQUEST_SENSE)
71 if ((stat & ATA_ERR) || (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 & ATA_DSC) == 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);
120 if (ireason & ATAPI_COD) {
121 printk(KERN_ERR "%s: CoD != 0 in %s\n", drive->name, __func__);
122 return ide_do_reset(drive);
125 if (((ireason & ATAPI_IO) == ATAPI_IO) ==
126 !!(pc->flags & PC_FLAG_WRITING)) {
127 /* Hopefully, we will never get here */
128 printk(KERN_ERR "%s: We wanted to %s, but the device wants us "
129 "to %s!\n", drive->name,
130 (ireason & ATAPI_IO) ? "Write" : "Read",
131 (ireason & ATAPI_IO) ? "Read" : "Write");
132 return ide_do_reset(drive);
135 if (!(pc->flags & PC_FLAG_WRITING)) {
136 /* Reading - Check that we have enough space */
137 temp = pc->xferred + bcount;
138 if (temp > pc->req_xfer) {
139 if (temp > pc->buf_size) {
140 printk(KERN_ERR "%s: The device wants to send "
141 "us more data than expected - "
145 temp = pc->buf_size - pc->xferred;
150 io_buffers(drive, pc, temp, 0);
152 tp_ops->input_data(drive, NULL,
154 printk(KERN_ERR "%s: transferred %d of "
161 ide_pad_transfer(drive, 0, bcount - temp);
162 ide_set_handler(drive, handler, timeout,
166 debug_log("The device wants to send us more data than "
167 "expected - allowing transfer\n");
169 xferfunc = tp_ops->input_data;
171 xferfunc = tp_ops->output_data;
173 if ((drive->media == ide_floppy && !scsi && !pc->buf) ||
174 (drive->media == ide_tape && !scsi && pc->bh) ||
176 io_buffers(drive, pc, bcount, !!(pc->flags & PC_FLAG_WRITING));
178 xferfunc(drive, NULL, pc->cur_pos, bcount);
180 /* Update the current position */
181 pc->xferred += bcount;
182 pc->cur_pos += bcount;
184 debug_log("[cmd %x] transferred %d bytes on that intr.\n",
187 /* And set the interrupt handler again */
188 ide_set_handler(drive, handler, timeout, expiry);
191 EXPORT_SYMBOL_GPL(ide_pc_intr);
193 static u8 ide_read_ireason(ide_drive_t *drive)
197 memset(&task, 0, sizeof(task));
198 task.tf_flags = IDE_TFLAG_IN_NSECT;
200 drive->hwif->tp_ops->tf_read(drive, &task);
202 return task.tf.nsect & 3;
205 static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
209 while (retries-- && ((ireason & ATAPI_COD) == 0 ||
210 (ireason & ATAPI_IO))) {
211 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
212 "a packet command, retrying\n", drive->name);
214 ireason = ide_read_ireason(drive);
216 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
217 "a packet command, ignoring\n",
219 ireason |= ATAPI_COD;
220 ireason &= ~ATAPI_IO;
227 ide_startstop_t ide_transfer_pc(ide_drive_t *drive, struct ide_atapi_pc *pc,
228 ide_handler_t *handler, unsigned int timeout,
229 ide_expiry_t *expiry)
231 ide_hwif_t *hwif = drive->hwif;
232 struct request *rq = hwif->hwgroup->rq;
233 ide_startstop_t startstop;
236 if (ide_wait_stat(&startstop, drive, ATA_DRQ, ATA_BUSY, WAIT_READY)) {
237 printk(KERN_ERR "%s: Strange, packet command initiated yet "
238 "DRQ isn't asserted\n", drive->name);
242 ireason = ide_read_ireason(drive);
243 if (drive->media == ide_tape && !drive->scsi)
244 ireason = ide_wait_ireason(drive, ireason);
246 if ((ireason & ATAPI_COD) == 0 || (ireason & ATAPI_IO)) {
247 printk(KERN_ERR "%s: (IO,CoD) != (0,1) while issuing "
248 "a packet command\n", drive->name);
249 return ide_do_reset(drive);
252 /* Set the interrupt routine */
253 ide_set_handler(drive, handler, timeout, expiry);
255 /* Begin DMA, if necessary */
256 if (pc->flags & PC_FLAG_DMA_OK) {
257 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
258 hwif->dma_ops->dma_start(drive);
261 /* Send the actual packet */
262 if ((drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) == 0)
263 hwif->tp_ops->output_data(drive, NULL, rq->cmd, 12);
267 EXPORT_SYMBOL_GPL(ide_transfer_pc);
269 ide_startstop_t ide_issue_pc(ide_drive_t *drive, struct ide_atapi_pc *pc,
270 ide_handler_t *handler, unsigned int timeout,
271 ide_expiry_t *expiry)
273 ide_hwif_t *hwif = drive->hwif;
277 /* We haven't transferred any data yet */
279 pc->cur_pos = pc->buf;
281 /* Request to transfer the entire buffer at once */
282 if (drive->media == ide_tape && !drive->scsi)
283 bcount = pc->req_xfer;
285 bcount = min(pc->req_xfer, 63 * 1024);
287 if (pc->flags & PC_FLAG_DMA_ERROR) {
288 pc->flags &= ~PC_FLAG_DMA_ERROR;
292 if ((pc->flags & PC_FLAG_DMA_OK) && drive->using_dma) {
295 dma = !hwif->dma_ops->dma_setup(drive);
301 pc->flags &= ~PC_FLAG_DMA_OK;
303 ide_pktcmd_tf_load(drive, drive->scsi ? 0 : IDE_TFLAG_OUT_DEVICE,
306 /* Issue the packet command */
307 if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
308 ide_execute_command(drive, ATA_CMD_PACKET, handler,
312 ide_execute_pkt_cmd(drive);
313 return (*handler)(drive);
316 EXPORT_SYMBOL_GPL(ide_issue_pc);