blob: f848010c15a5ec275b3cb7026ce6b100c807a5d0 [file] [log] [blame]
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +02001/*
2 * ATAPI support.
3 */
4
5#include <linux/kernel.h>
6#include <linux/delay.h>
7#include <linux/ide.h>
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +02008#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;
Borislav Petkov8fccf892008-07-23 19:56:01 +020025 struct request *rq = hwif->hwgroup->rq;
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +020026 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +020027 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) {
Borislav Petkovdb9d2862008-07-23 19:55:59 +020035 drive->pc_callback(drive);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +020036 return ide_stopped;
37 }
38
39 /* Clear the interrupt */
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +020040 stat = tp_ops->read_status(hwif);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +020041
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 &&
Borislav Petkov8fccf892008-07-23 19:56:01 +020068 (stat & ERR_STAT) && rq->cmd[0] == REQUEST_SENSE)
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +020069 stat &= ~ERR_STAT;
Borislav Petkov8fccf892008-07-23 19:56:01 +020070
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +020071 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
Borislav Petkov8fccf892008-07-23 19:56:01 +020081 if (rq->cmd[0] == REQUEST_SENSE) {
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +020082 printk(KERN_ERR "%s: I/O error in request sense"
83 " command\n", drive->name);
84 return ide_do_reset(drive);
85 }
86
Borislav Petkov8fccf892008-07-23 19:56:01 +020087 debug_log("[cmd %x]: check condition\n", rq->cmd[0]);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +020088
89 /* Retry operation */
90 retry_pc(drive);
Borislav Petkov8fccf892008-07-23 19:56:01 +020091
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +020092 /* 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 }
Borislav Petkov8fccf892008-07-23 19:56:01 +0200102
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200103 /* Command finished - Call the callback function */
Borislav Petkovdb9d2862008-07-23 19:55:59 +0200104 drive->pc_callback(drive);
Borislav Petkov8fccf892008-07-23 19:56:01 +0200105
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200106 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 }
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200116
Bartlomiej Zolnierkiewicz18236492008-07-23 19:55:54 +0200117 /* Get the number of bytes to transfer on this interrupt. */
118 ide_read_bcount_and_ireason(drive, &bcount, &ireason);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200119
120 if (ireason & CD) {
121 printk(KERN_ERR "%s: CoD != 0 in %s\n", drive->name, __func__);
122 return ide_do_reset(drive);
123 }
Borislav Petkov8fccf892008-07-23 19:56:01 +0200124
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200125 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 }
Borislav Petkov8fccf892008-07-23 19:56:01 +0200133
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200134 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
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200151 tp_ops->input_data(drive, NULL,
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200152 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 }
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200168 xferfunc = tp_ops->input_data;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200169 } else
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200170 xferfunc = tp_ops->output_data;
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200171
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",
Borislav Petkov8fccf892008-07-23 19:56:01 +0200184 rq->cmd[0], bcount);
Bartlomiej Zolnierkiewicz646c0cb2008-07-15 21:22:03 +0200185
186 /* And set the interrupt handler again */
187 ide_set_handler(drive, handler, timeout, expiry);
188 return ide_started;
189}
190EXPORT_SYMBOL_GPL(ide_pc_intr);
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200191
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200192static 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
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200199 drive->hwif->tp_ops->tf_read(drive, &task);
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200200
201 return task.tf.nsect & 3;
202}
203
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200204static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
205{
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200206 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);
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200212 ireason = ide_read_ireason(drive);
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200213 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
225ide_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;
Borislav Petkov8fccf892008-07-23 19:56:01 +0200230 struct request *rq = hwif->hwgroup->rq;
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200231 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
Bartlomiej Zolnierkiewicz88a72102008-07-23 19:55:54 +0200240 ireason = ide_read_ireason(drive);
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200241 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 ((pc->flags & PC_FLAG_ZIP_DRIVE) == 0)
Borislav Petkov8fccf892008-07-23 19:56:01 +0200261 hwif->tp_ops->output_data(drive, NULL, rq->cmd, 12);
Bartlomiej Zolnierkiewicz594c16d2008-07-15 21:21:58 +0200262
263 return ide_started;
264}
265EXPORT_SYMBOL_GPL(ide_transfer_pc);
Bartlomiej Zolnierkiewicz6bf16412008-07-15 21:22:00 +0200266
267ide_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 (pc->flags & PC_FLAG_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}
314EXPORT_SYMBOL_GPL(ide_issue_pc);