Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/penberg...
[linux-2.6] / drivers / ide / ide-atapi.c
1 /*
2  * ATAPI support.
3  */
4
5 #include <linux/kernel.h>
6 #include <linux/delay.h>
7 #include <linux/ide.h>
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 */
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))
23 {
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;
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) {
35                 drive->pc_callback(drive);
36                 return ide_stopped;
37         }
38
39         /* Clear the interrupt */
40         stat = tp_ops->read_status(hwif);
41
42         if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
43                 if (hwif->dma_ops->dma_end(drive) ||
44                     (drive->media == ide_tape && !scsi && (stat & ERR_STAT))) {
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 */
59         if ((stat & DRQ_STAT) == 0) {
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 &&
68                     (stat & ERR_STAT) && rq->cmd[0] == REQUEST_SENSE)
69                         stat &= ~ERR_STAT;
70
71                 if ((stat & ERR_STAT) || (pc->flags & PC_FLAG_DMA_ERROR)) {
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
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);
85                         }
86
87                         debug_log("[cmd %x]: check condition\n", rq->cmd[0]);
88
89                         /* Retry operation */
90                         retry_pc(drive);
91
92                         /* queued, but not started */
93                         return ide_stopped;
94                 }
95 cmd_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
103                 /* Command finished - Call the callback function */
104                 drive->pc_callback(drive);
105
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         }
116
117         /* Get the number of bytes to transfer on this interrupt. */
118         ide_read_bcount_and_ireason(drive, &bcount, &ireason);
119
120         if (ireason & CD) {
121                 printk(KERN_ERR "%s: CoD != 0 in %s\n", drive->name, __func__);
122                 return ide_do_reset(drive);
123         }
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
134         if (!(pc->flags & PC_FLAG_WRITING)) {
135                 /* Reading - Check that we have enough space */
136                 temp = pc->xferred + bcount;
137                 if (temp > pc->req_xfer) {
138                         if (temp > pc->buf_size) {
139                                 printk(KERN_ERR "%s: The device wants to send "
140                                                 "us more data than expected - "
141                                                 "discarding data\n",
142                                                 drive->name);
143                                 if (scsi)
144                                         temp = pc->buf_size - pc->xferred;
145                                 else
146                                         temp = 0;
147                                 if (temp) {
148                                         if (pc->sg)
149                                                 io_buffers(drive, pc, temp, 0);
150                                         else
151                                                 tp_ops->input_data(drive, NULL,
152                                                         pc->cur_pos, temp);
153                                         printk(KERN_ERR "%s: transferred %d of "
154                                                         "%d bytes\n",
155                                                         drive->name,
156                                                         temp, bcount);
157                                 }
158                                 pc->xferred += temp;
159                                 pc->cur_pos += temp;
160                                 ide_pad_transfer(drive, 0, bcount - temp);
161                                 ide_set_handler(drive, handler, timeout,
162                                                 expiry);
163                                 return ide_started;
164                         }
165                         debug_log("The device wants to send us more data than "
166                                   "expected - allowing transfer\n");
167                 }
168                 xferfunc = tp_ops->input_data;
169         } else
170                 xferfunc = tp_ops->output_data;
171
172         if ((drive->media == ide_floppy && !scsi && !pc->buf) ||
173             (drive->media == ide_tape && !scsi && pc->bh) ||
174             (scsi && pc->sg))
175                 io_buffers(drive, pc, bcount, !!(pc->flags & PC_FLAG_WRITING));
176         else
177                 xferfunc(drive, NULL, pc->cur_pos, bcount);
178
179         /* Update the current position */
180         pc->xferred += bcount;
181         pc->cur_pos += bcount;
182
183         debug_log("[cmd %x] transferred %d bytes on that intr.\n",
184                   rq->cmd[0], bcount);
185
186         /* And set the interrupt handler again */
187         ide_set_handler(drive, handler, timeout, expiry);
188         return ide_started;
189 }
190 EXPORT_SYMBOL_GPL(ide_pc_intr);
191
192 static u8 ide_read_ireason(ide_drive_t *drive)
193 {
194         ide_task_t task;
195
196         memset(&task, 0, sizeof(task));
197         task.tf_flags = IDE_TFLAG_IN_NSECT;
198
199         drive->hwif->tp_ops->tf_read(drive, &task);
200
201         return task.tf.nsect & 3;
202 }
203
204 static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
205 {
206         int retries = 100;
207
208         while (retries-- && ((ireason & CD) == 0 || (ireason & IO))) {
209                 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
210                                 "a packet command, retrying\n", drive->name);
211                 udelay(100);
212                 ireason = ide_read_ireason(drive);
213                 if (retries == 0) {
214                         printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
215                                         "a packet command, ignoring\n",
216                                         drive->name);
217                         ireason |= CD;
218                         ireason &= ~IO;
219                 }
220         }
221
222         return ireason;
223 }
224
225 ide_startstop_t ide_transfer_pc(ide_drive_t *drive, struct ide_atapi_pc *pc,
226                                 ide_handler_t *handler, unsigned int timeout,
227                                 ide_expiry_t *expiry)
228 {
229         ide_hwif_t *hwif = drive->hwif;
230         struct request *rq = hwif->hwgroup->rq;
231         ide_startstop_t startstop;
232         u8 ireason;
233
234         if (ide_wait_stat(&startstop, drive, DRQ_STAT, BUSY_STAT, WAIT_READY)) {
235                 printk(KERN_ERR "%s: Strange, packet command initiated yet "
236                                 "DRQ isn't asserted\n", drive->name);
237                 return startstop;
238         }
239
240         ireason = ide_read_ireason(drive);
241         if (drive->media == ide_tape && !drive->scsi)
242                 ireason = ide_wait_ireason(drive, ireason);
243
244         if ((ireason & CD) == 0 || (ireason & IO)) {
245                 printk(KERN_ERR "%s: (IO,CoD) != (0,1) while issuing "
246                                 "a packet command\n", drive->name);
247                 return ide_do_reset(drive);
248         }
249
250         /* Set the interrupt routine */
251         ide_set_handler(drive, handler, timeout, expiry);
252
253         /* Begin DMA, if necessary */
254         if (pc->flags & PC_FLAG_DMA_OK) {
255                 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
256                 hwif->dma_ops->dma_start(drive);
257         }
258
259         /* Send the actual packet */
260         if ((drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) == 0)
261                 hwif->tp_ops->output_data(drive, NULL, rq->cmd, 12);
262
263         return ide_started;
264 }
265 EXPORT_SYMBOL_GPL(ide_transfer_pc);
266
267 ide_startstop_t ide_issue_pc(ide_drive_t *drive, struct ide_atapi_pc *pc,
268                              ide_handler_t *handler, unsigned int timeout,
269                              ide_expiry_t *expiry)
270 {
271         ide_hwif_t *hwif = drive->hwif;
272         u16 bcount;
273         u8 dma = 0;
274
275         /* We haven't transferred any data yet */
276         pc->xferred = 0;
277         pc->cur_pos = pc->buf;
278
279         /* Request to transfer the entire buffer at once */
280         if (drive->media == ide_tape && !drive->scsi)
281                 bcount = pc->req_xfer;
282         else
283                 bcount = min(pc->req_xfer, 63 * 1024);
284
285         if (pc->flags & PC_FLAG_DMA_ERROR) {
286                 pc->flags &= ~PC_FLAG_DMA_ERROR;
287                 ide_dma_off(drive);
288         }
289
290         if ((pc->flags & PC_FLAG_DMA_OK) && drive->using_dma) {
291                 if (drive->scsi)
292                         hwif->sg_mapped = 1;
293                 dma = !hwif->dma_ops->dma_setup(drive);
294                 if (drive->scsi)
295                         hwif->sg_mapped = 0;
296         }
297
298         if (!dma)
299                 pc->flags &= ~PC_FLAG_DMA_OK;
300
301         ide_pktcmd_tf_load(drive, drive->scsi ? 0 : IDE_TFLAG_OUT_DEVICE,
302                            bcount, dma);
303
304         /* Issue the packet command */
305         if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
306                 ide_execute_command(drive, WIN_PACKETCMD, handler,
307                                     timeout, NULL);
308                 return ide_started;
309         } else {
310                 ide_execute_pkt_cmd(drive);
311                 return (*handler)(drive);
312         }
313 }
314 EXPORT_SYMBOL_GPL(ide_issue_pc);