ide: move ioctls handling to ide-ioctls.c
[deliverable/linux.git] / drivers / ide / ide-atapi.c
CommitLineData
594c16d8
BZ
1/*
2 * ATAPI support.
3 */
4
5#include <linux/kernel.h>
6#include <linux/delay.h>
7#include <linux/ide.h>
646c0cb6
BZ
8#include <scsi/scsi.h>
9
10#ifdef DEBUG
11#define debug_log(fmt, args...) \
12 printk(KERN_INFO "ide: " fmt, ## args)
13#else
14#define debug_log(fmt, args...) do {} while (0)
15#endif
16
17/* TODO: unify the code thus making some arguments go away */
18ide_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))
23{
24 ide_hwif_t *hwif = drive->hwif;
8fccf899 25 struct request *rq = hwif->hwgroup->rq;
374e042c 26 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
646c0cb6
BZ
27 xfer_func_t *xferfunc;
28 unsigned int temp;
29 u16 bcount;
30 u8 stat, ireason, scsi = drive->scsi;
31
32 debug_log("Enter %s - interrupt handler\n", __func__);
33
34 if (pc->flags & PC_FLAG_TIMEDOUT) {
db9d2869 35 drive->pc_callback(drive);
646c0cb6
BZ
36 return ide_stopped;
37 }
38
39 /* Clear the interrupt */
374e042c 40 stat = tp_ops->read_status(hwif);
646c0cb6
BZ
41
42 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
43 if (hwif->dma_ops->dma_end(drive) ||
3a7d2484 44 (drive->media == ide_tape && !scsi && (stat & ATA_ERR))) {
646c0cb6
BZ
45 if (drive->media == ide_floppy && !scsi)
46 printk(KERN_ERR "%s: DMA %s error\n",
47 drive->name, rq_data_dir(pc->rq)
48 ? "write" : "read");
49 pc->flags |= PC_FLAG_DMA_ERROR;
50 } else {
51 pc->xferred = pc->req_xfer;
52 if (update_buffers)
53 update_buffers(drive, pc);
54 }
55 debug_log("%s: DMA finished\n", drive->name);
56 }
57
58 /* No more interrupts */
3a7d2484 59 if ((stat & ATA_DRQ) == 0) {
646c0cb6
BZ
60 debug_log("Packet command completed, %d bytes transferred\n",
61 pc->xferred);
62
63 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
64
65 local_irq_enable_in_hardirq();
66
67 if (drive->media == ide_tape && !scsi &&
3a7d2484
BZ
68 (stat & ATA_ERR) && rq->cmd[0] == REQUEST_SENSE)
69 stat &= ~ATA_ERR;
8fccf899 70
3a7d2484 71 if ((stat & ATA_ERR) || (pc->flags & PC_FLAG_DMA_ERROR)) {
646c0cb6
BZ
72 /* Error detected */
73 debug_log("%s: I/O error\n", drive->name);
74
75 if (drive->media != ide_tape || scsi) {
76 pc->rq->errors++;
77 if (scsi)
78 goto cmd_finished;
79 }
80
8fccf899 81 if (rq->cmd[0] == REQUEST_SENSE) {
646c0cb6
BZ
82 printk(KERN_ERR "%s: I/O error in request sense"
83 " command\n", drive->name);
84 return ide_do_reset(drive);
85 }
86
8fccf899 87 debug_log("[cmd %x]: check condition\n", rq->cmd[0]);
646c0cb6
BZ
88
89 /* Retry operation */
90 retry_pc(drive);
8fccf899 91
646c0cb6
BZ
92 /* queued, but not started */
93 return ide_stopped;
94 }
95cmd_finished:
96 pc->error = 0;
97 if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) &&
3a7d2484 98 (stat & ATA_DSC) == 0) {
646c0cb6
BZ
99 dsc_handle(drive);
100 return ide_stopped;
101 }
8fccf899 102
646c0cb6 103 /* Command finished - Call the callback function */
db9d2869 104 drive->pc_callback(drive);
8fccf899 105
646c0cb6
BZ
106 return ide_stopped;
107 }
108
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);
113 ide_dma_off(drive);
114 return ide_do_reset(drive);
115 }
646c0cb6 116
1823649b
BZ
117 /* Get the number of bytes to transfer on this interrupt. */
118 ide_read_bcount_and_ireason(drive, &bcount, &ireason);
646c0cb6 119
3a7d2484 120 if (ireason & ATAPI_COD) {
646c0cb6
BZ
121 printk(KERN_ERR "%s: CoD != 0 in %s\n", drive->name, __func__);
122 return ide_do_reset(drive);
123 }
8fccf899 124
3a7d2484
BZ
125 if (((ireason & ATAPI_IO) == ATAPI_IO) ==
126 !!(pc->flags & PC_FLAG_WRITING)) {
646c0cb6
BZ
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,
3a7d2484
BZ
130 (ireason & ATAPI_IO) ? "Write" : "Read",
131 (ireason & ATAPI_IO) ? "Read" : "Write");
646c0cb6
BZ
132 return ide_do_reset(drive);
133 }
8fccf899 134
646c0cb6
BZ
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 - "
142 "discarding data\n",
143 drive->name);
144 if (scsi)
145 temp = pc->buf_size - pc->xferred;
146 else
147 temp = 0;
148 if (temp) {
149 if (pc->sg)
150 io_buffers(drive, pc, temp, 0);
151 else
374e042c 152 tp_ops->input_data(drive, NULL,
646c0cb6
BZ
153 pc->cur_pos, temp);
154 printk(KERN_ERR "%s: transferred %d of "
155 "%d bytes\n",
156 drive->name,
157 temp, bcount);
158 }
159 pc->xferred += temp;
160 pc->cur_pos += temp;
161 ide_pad_transfer(drive, 0, bcount - temp);
162 ide_set_handler(drive, handler, timeout,
163 expiry);
164 return ide_started;
165 }
166 debug_log("The device wants to send us more data than "
167 "expected - allowing transfer\n");
168 }
374e042c 169 xferfunc = tp_ops->input_data;
646c0cb6 170 } else
374e042c 171 xferfunc = tp_ops->output_data;
646c0cb6
BZ
172
173 if ((drive->media == ide_floppy && !scsi && !pc->buf) ||
174 (drive->media == ide_tape && !scsi && pc->bh) ||
175 (scsi && pc->sg))
176 io_buffers(drive, pc, bcount, !!(pc->flags & PC_FLAG_WRITING));
177 else
178 xferfunc(drive, NULL, pc->cur_pos, bcount);
179
180 /* Update the current position */
181 pc->xferred += bcount;
182 pc->cur_pos += bcount;
183
184 debug_log("[cmd %x] transferred %d bytes on that intr.\n",
8fccf899 185 rq->cmd[0], bcount);
646c0cb6
BZ
186
187 /* And set the interrupt handler again */
188 ide_set_handler(drive, handler, timeout, expiry);
189 return ide_started;
190}
191EXPORT_SYMBOL_GPL(ide_pc_intr);
594c16d8 192
88a72109
BZ
193static u8 ide_read_ireason(ide_drive_t *drive)
194{
195 ide_task_t task;
196
197 memset(&task, 0, sizeof(task));
198 task.tf_flags = IDE_TFLAG_IN_NSECT;
199
374e042c 200 drive->hwif->tp_ops->tf_read(drive, &task);
88a72109
BZ
201
202 return task.tf.nsect & 3;
203}
204
594c16d8
BZ
205static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
206{
594c16d8
BZ
207 int retries = 100;
208
3a7d2484
BZ
209 while (retries-- && ((ireason & ATAPI_COD) == 0 ||
210 (ireason & ATAPI_IO))) {
594c16d8
BZ
211 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
212 "a packet command, retrying\n", drive->name);
213 udelay(100);
88a72109 214 ireason = ide_read_ireason(drive);
594c16d8
BZ
215 if (retries == 0) {
216 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
217 "a packet command, ignoring\n",
218 drive->name);
3a7d2484
BZ
219 ireason |= ATAPI_COD;
220 ireason &= ~ATAPI_IO;
594c16d8
BZ
221 }
222 }
223
224 return ireason;
225}
226
227ide_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)
230{
231 ide_hwif_t *hwif = drive->hwif;
8fccf899 232 struct request *rq = hwif->hwgroup->rq;
594c16d8
BZ
233 ide_startstop_t startstop;
234 u8 ireason;
235
3a7d2484 236 if (ide_wait_stat(&startstop, drive, ATA_DRQ, ATA_BUSY, WAIT_READY)) {
594c16d8
BZ
237 printk(KERN_ERR "%s: Strange, packet command initiated yet "
238 "DRQ isn't asserted\n", drive->name);
239 return startstop;
240 }
241
88a72109 242 ireason = ide_read_ireason(drive);
594c16d8
BZ
243 if (drive->media == ide_tape && !drive->scsi)
244 ireason = ide_wait_ireason(drive, ireason);
245
3a7d2484 246 if ((ireason & ATAPI_COD) == 0 || (ireason & ATAPI_IO)) {
594c16d8
BZ
247 printk(KERN_ERR "%s: (IO,CoD) != (0,1) while issuing "
248 "a packet command\n", drive->name);
249 return ide_do_reset(drive);
250 }
251
252 /* Set the interrupt routine */
253 ide_set_handler(drive, handler, timeout, expiry);
254
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);
259 }
260
261 /* Send the actual packet */
ea68d270 262 if ((drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) == 0)
8fccf899 263 hwif->tp_ops->output_data(drive, NULL, rq->cmd, 12);
594c16d8
BZ
264
265 return ide_started;
266}
267EXPORT_SYMBOL_GPL(ide_transfer_pc);
6bf1641c
BZ
268
269ide_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)
272{
273 ide_hwif_t *hwif = drive->hwif;
274 u16 bcount;
275 u8 dma = 0;
276
277 /* We haven't transferred any data yet */
278 pc->xferred = 0;
279 pc->cur_pos = pc->buf;
280
281 /* Request to transfer the entire buffer at once */
282 if (drive->media == ide_tape && !drive->scsi)
283 bcount = pc->req_xfer;
284 else
285 bcount = min(pc->req_xfer, 63 * 1024);
286
287 if (pc->flags & PC_FLAG_DMA_ERROR) {
288 pc->flags &= ~PC_FLAG_DMA_ERROR;
289 ide_dma_off(drive);
290 }
291
292 if ((pc->flags & PC_FLAG_DMA_OK) && drive->using_dma) {
293 if (drive->scsi)
294 hwif->sg_mapped = 1;
295 dma = !hwif->dma_ops->dma_setup(drive);
296 if (drive->scsi)
297 hwif->sg_mapped = 0;
298 }
299
300 if (!dma)
301 pc->flags &= ~PC_FLAG_DMA_OK;
302
303 ide_pktcmd_tf_load(drive, drive->scsi ? 0 : IDE_TFLAG_OUT_DEVICE,
304 bcount, dma);
305
306 /* Issue the packet command */
ac77ef8b 307 if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
aaaade3f 308 ide_execute_command(drive, ATA_CMD_PACKET, handler,
6bf1641c
BZ
309 timeout, NULL);
310 return ide_started;
311 } else {
312 ide_execute_pkt_cmd(drive);
313 return (*handler)(drive);
314 }
315}
316EXPORT_SYMBOL_GPL(ide_issue_pc);
This page took 0.075736 seconds and 5 git commands to generate.