]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/ide/ide-atapi.c
ide-tape: use drive->pc_callback instead of pc->callback
[mirror_ubuntu-artful-kernel.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;
374e042c 25 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
646c0cb6
BZ
26 xfer_func_t *xferfunc;
27 unsigned int temp;
28 u16 bcount;
29 u8 stat, ireason, scsi = drive->scsi;
30
31 debug_log("Enter %s - interrupt handler\n", __func__);
32
33 if (pc->flags & PC_FLAG_TIMEDOUT) {
776bb027 34 if (drive->media == ide_floppy || drive->media == ide_tape)
2207fa5a
BP
35 drive->pc_callback(drive);
36 else
37 pc->callback(drive);
646c0cb6
BZ
38 return ide_stopped;
39 }
40
41 /* Clear the interrupt */
374e042c 42 stat = tp_ops->read_status(hwif);
646c0cb6
BZ
43
44 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
45 if (hwif->dma_ops->dma_end(drive) ||
46 (drive->media == ide_tape && !scsi && (stat & ERR_STAT))) {
47 if (drive->media == ide_floppy && !scsi)
48 printk(KERN_ERR "%s: DMA %s error\n",
49 drive->name, rq_data_dir(pc->rq)
50 ? "write" : "read");
51 pc->flags |= PC_FLAG_DMA_ERROR;
52 } else {
53 pc->xferred = pc->req_xfer;
54 if (update_buffers)
55 update_buffers(drive, pc);
56 }
57 debug_log("%s: DMA finished\n", drive->name);
58 }
59
60 /* No more interrupts */
61 if ((stat & DRQ_STAT) == 0) {
62 debug_log("Packet command completed, %d bytes transferred\n",
63 pc->xferred);
64
65 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
66
67 local_irq_enable_in_hardirq();
68
69 if (drive->media == ide_tape && !scsi &&
70 (stat & ERR_STAT) && pc->c[0] == REQUEST_SENSE)
71 stat &= ~ERR_STAT;
72 if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) {
73 /* Error detected */
74 debug_log("%s: I/O error\n", drive->name);
75
76 if (drive->media != ide_tape || scsi) {
77 pc->rq->errors++;
78 if (scsi)
79 goto cmd_finished;
80 }
81
82 if (pc->c[0] == REQUEST_SENSE) {
83 printk(KERN_ERR "%s: I/O error in request sense"
84 " command\n", drive->name);
85 return ide_do_reset(drive);
86 }
87
88 debug_log("[cmd %x]: check condition\n", pc->c[0]);
89
90 /* Retry operation */
91 retry_pc(drive);
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) &&
98 (stat & SEEK_STAT) == 0) {
99 dsc_handle(drive);
100 return ide_stopped;
101 }
102 /* Command finished - Call the callback function */
776bb027 103 if (drive->media == ide_floppy || drive->media == ide_tape)
2207fa5a
BP
104 drive->pc_callback(drive);
105 else
106 pc->callback(drive);
646c0cb6
BZ
107 return ide_stopped;
108 }
109
110 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
111 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
112 printk(KERN_ERR "%s: The device wants to issue more interrupts "
113 "in DMA mode\n", drive->name);
114 ide_dma_off(drive);
115 return ide_do_reset(drive);
116 }
646c0cb6 117
1823649b
BZ
118 /* Get the number of bytes to transfer on this interrupt. */
119 ide_read_bcount_and_ireason(drive, &bcount, &ireason);
646c0cb6
BZ
120
121 if (ireason & CD) {
122 printk(KERN_ERR "%s: CoD != 0 in %s\n", drive->name, __func__);
123 return ide_do_reset(drive);
124 }
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);
132 }
133 if (!(pc->flags & PC_FLAG_WRITING)) {
134 /* Reading - Check that we have enough space */
135 temp = pc->xferred + bcount;
136 if (temp > pc->req_xfer) {
137 if (temp > pc->buf_size) {
138 printk(KERN_ERR "%s: The device wants to send "
139 "us more data than expected - "
140 "discarding data\n",
141 drive->name);
142 if (scsi)
143 temp = pc->buf_size - pc->xferred;
144 else
145 temp = 0;
146 if (temp) {
147 if (pc->sg)
148 io_buffers(drive, pc, temp, 0);
149 else
374e042c 150 tp_ops->input_data(drive, NULL,
646c0cb6
BZ
151 pc->cur_pos, temp);
152 printk(KERN_ERR "%s: transferred %d of "
153 "%d bytes\n",
154 drive->name,
155 temp, bcount);
156 }
157 pc->xferred += temp;
158 pc->cur_pos += temp;
159 ide_pad_transfer(drive, 0, bcount - temp);
160 ide_set_handler(drive, handler, timeout,
161 expiry);
162 return ide_started;
163 }
164 debug_log("The device wants to send us more data than "
165 "expected - allowing transfer\n");
166 }
374e042c 167 xferfunc = tp_ops->input_data;
646c0cb6 168 } else
374e042c 169 xferfunc = tp_ops->output_data;
646c0cb6
BZ
170
171 if ((drive->media == ide_floppy && !scsi && !pc->buf) ||
172 (drive->media == ide_tape && !scsi && pc->bh) ||
173 (scsi && pc->sg))
174 io_buffers(drive, pc, bcount, !!(pc->flags & PC_FLAG_WRITING));
175 else
176 xferfunc(drive, NULL, pc->cur_pos, bcount);
177
178 /* Update the current position */
179 pc->xferred += bcount;
180 pc->cur_pos += bcount;
181
182 debug_log("[cmd %x] transferred %d bytes on that intr.\n",
183 pc->c[0], bcount);
184
185 /* And set the interrupt handler again */
186 ide_set_handler(drive, handler, timeout, expiry);
187 return ide_started;
188}
189EXPORT_SYMBOL_GPL(ide_pc_intr);
594c16d8 190
88a72109
BZ
191static u8 ide_read_ireason(ide_drive_t *drive)
192{
193 ide_task_t task;
194
195 memset(&task, 0, sizeof(task));
196 task.tf_flags = IDE_TFLAG_IN_NSECT;
197
374e042c 198 drive->hwif->tp_ops->tf_read(drive, &task);
88a72109
BZ
199
200 return task.tf.nsect & 3;
201}
202
594c16d8
BZ
203static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
204{
594c16d8
BZ
205 int retries = 100;
206
207 while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
208 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
209 "a packet command, retrying\n", drive->name);
210 udelay(100);
88a72109 211 ireason = ide_read_ireason(drive);
594c16d8
BZ
212 if (retries == 0) {
213 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
214 "a packet command, ignoring\n",
215 drive->name);
216 ireason |= CD;
217 ireason &= ~IO;
218 }
219 }
220
221 return ireason;
222}
223
224ide_startstop_t ide_transfer_pc(ide_drive_t *drive, struct ide_atapi_pc *pc,
225 ide_handler_t *handler, unsigned int timeout,
226 ide_expiry_t *expiry)
227{
228 ide_hwif_t *hwif = drive->hwif;
229 ide_startstop_t startstop;
230 u8 ireason;
231
232 if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
233 printk(KERN_ERR "%s: Strange, packet command initiated yet "
234 "DRQ isn't asserted\n", drive->name);
235 return startstop;
236 }
237
88a72109 238 ireason = ide_read_ireason(drive);
594c16d8
BZ
239 if (drive->media == ide_tape && !drive->scsi)
240 ireason = ide_wait_ireason(drive, ireason);
241
242 if ((ireason & CD) == 0 || (ireason & IO)) {
243 printk(KERN_ERR "%s: (IO,CoD) != (0,1) while issuing "
244 "a packet command\n", drive->name);
245 return ide_do_reset(drive);
246 }
247
248 /* Set the interrupt routine */
249 ide_set_handler(drive, handler, timeout, expiry);
250
251 /* Begin DMA, if necessary */
252 if (pc->flags & PC_FLAG_DMA_OK) {
253 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
254 hwif->dma_ops->dma_start(drive);
255 }
256
257 /* Send the actual packet */
258 if ((pc->flags & PC_FLAG_ZIP_DRIVE) == 0)
374e042c 259 hwif->tp_ops->output_data(drive, NULL, pc->c, 12);
594c16d8
BZ
260
261 return ide_started;
262}
263EXPORT_SYMBOL_GPL(ide_transfer_pc);
6bf1641c
BZ
264
265ide_startstop_t ide_issue_pc(ide_drive_t *drive, struct ide_atapi_pc *pc,
266 ide_handler_t *handler, unsigned int timeout,
267 ide_expiry_t *expiry)
268{
269 ide_hwif_t *hwif = drive->hwif;
270 u16 bcount;
271 u8 dma = 0;
272
273 /* We haven't transferred any data yet */
274 pc->xferred = 0;
275 pc->cur_pos = pc->buf;
276
277 /* Request to transfer the entire buffer at once */
278 if (drive->media == ide_tape && !drive->scsi)
279 bcount = pc->req_xfer;
280 else
281 bcount = min(pc->req_xfer, 63 * 1024);
282
283 if (pc->flags & PC_FLAG_DMA_ERROR) {
284 pc->flags &= ~PC_FLAG_DMA_ERROR;
285 ide_dma_off(drive);
286 }
287
288 if ((pc->flags & PC_FLAG_DMA_OK) && drive->using_dma) {
289 if (drive->scsi)
290 hwif->sg_mapped = 1;
291 dma = !hwif->dma_ops->dma_setup(drive);
292 if (drive->scsi)
293 hwif->sg_mapped = 0;
294 }
295
296 if (!dma)
297 pc->flags &= ~PC_FLAG_DMA_OK;
298
299 ide_pktcmd_tf_load(drive, drive->scsi ? 0 : IDE_TFLAG_OUT_DEVICE,
300 bcount, dma);
301
302 /* Issue the packet command */
303 if (pc->flags & PC_FLAG_DRQ_INTERRUPT) {
304 ide_execute_command(drive, WIN_PACKETCMD, handler,
305 timeout, NULL);
306 return ide_started;
307 } else {
308 ide_execute_pkt_cmd(drive);
309 return (*handler)(drive);
310 }
311}
312EXPORT_SYMBOL_GPL(ide_issue_pc);