i386: convert hardware exception 19 to an interrupt gate
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / ide / ide-atapi.c
blob608c5bade92939476d15a1c1f053584fde419e95
1 /*
2 * ATAPI support.
3 */
5 #include <linux/kernel.h>
6 #include <linux/delay.h>
7 #include <linux/ide.h>
8 #include <scsi/scsi.h>
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
18 * Check whether we can support a device,
19 * based on the ATAPI IDENTIFY command results.
21 int ide_check_atapi_device(ide_drive_t *drive, const char *s)
23 u16 *id = drive->id;
24 u8 gcw[2], protocol, device_type, removable, drq_type, packet_size;
26 *((u16 *)&gcw) = id[ATA_ID_CONFIG];
28 protocol = (gcw[1] & 0xC0) >> 6;
29 device_type = gcw[1] & 0x1F;
30 removable = (gcw[0] & 0x80) >> 7;
31 drq_type = (gcw[0] & 0x60) >> 5;
32 packet_size = gcw[0] & 0x03;
34 #ifdef CONFIG_PPC
35 /* kludge for Apple PowerBook internal zip */
36 if (drive->media == ide_floppy && device_type == 5 &&
37 !strstr((char *)&id[ATA_ID_PROD], "CD-ROM") &&
38 strstr((char *)&id[ATA_ID_PROD], "ZIP"))
39 device_type = 0;
40 #endif
42 if (protocol != 2)
43 printk(KERN_ERR "%s: %s: protocol (0x%02x) is not ATAPI\n",
44 s, drive->name, protocol);
45 else if ((drive->media == ide_floppy && device_type != 0) ||
46 (drive->media == ide_tape && device_type != 1))
47 printk(KERN_ERR "%s: %s: invalid device type (0x%02x)\n",
48 s, drive->name, device_type);
49 else if (removable == 0)
50 printk(KERN_ERR "%s: %s: the removable flag is not set\n",
51 s, drive->name);
52 else if (drive->media == ide_floppy && drq_type == 3)
53 printk(KERN_ERR "%s: %s: sorry, DRQ type (0x%02x) not "
54 "supported\n", s, drive->name, drq_type);
55 else if (packet_size != 0)
56 printk(KERN_ERR "%s: %s: packet size (0x%02x) is not 12 "
57 "bytes\n", s, drive->name, packet_size);
58 else
59 return 1;
60 return 0;
62 EXPORT_SYMBOL_GPL(ide_check_atapi_device);
64 /* PIO data transfer routine using the scatter gather table. */
65 int ide_io_buffers(ide_drive_t *drive, struct ide_atapi_pc *pc,
66 unsigned int bcount, int write)
68 ide_hwif_t *hwif = drive->hwif;
69 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
70 xfer_func_t *xf = write ? tp_ops->output_data : tp_ops->input_data;
71 struct scatterlist *sg = pc->sg;
72 char *buf;
73 int count, done = 0;
75 while (bcount) {
76 count = min(sg->length - pc->b_count, bcount);
78 if (PageHighMem(sg_page(sg))) {
79 unsigned long flags;
81 local_irq_save(flags);
82 buf = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
83 xf(drive, NULL, buf + pc->b_count, count);
84 kunmap_atomic(buf - sg->offset, KM_IRQ0);
85 local_irq_restore(flags);
86 } else {
87 buf = sg_virt(sg);
88 xf(drive, NULL, buf + pc->b_count, count);
91 bcount -= count;
92 pc->b_count += count;
93 done += count;
95 if (pc->b_count == sg->length) {
96 if (!--pc->sg_cnt)
97 break;
98 pc->sg = sg = sg_next(sg);
99 pc->b_count = 0;
103 if (bcount) {
104 printk(KERN_ERR "%s: %d leftover bytes, %s\n", drive->name,
105 bcount, write ? "padding with zeros"
106 : "discarding data");
107 ide_pad_transfer(drive, write, bcount);
110 return done;
112 EXPORT_SYMBOL_GPL(ide_io_buffers);
114 void ide_init_pc(struct ide_atapi_pc *pc)
116 memset(pc, 0, sizeof(*pc));
117 pc->buf = pc->pc_buf;
118 pc->buf_size = IDE_PC_BUFFER_SIZE;
120 EXPORT_SYMBOL_GPL(ide_init_pc);
123 * Generate a new packet command request in front of the request queue, before
124 * the current request, so that it will be processed immediately, on the next
125 * pass through the driver.
127 void ide_queue_pc_head(ide_drive_t *drive, struct gendisk *disk,
128 struct ide_atapi_pc *pc, struct request *rq)
130 blk_rq_init(NULL, rq);
131 rq->cmd_type = REQ_TYPE_SPECIAL;
132 rq->cmd_flags |= REQ_PREEMPT;
133 rq->buffer = (char *)pc;
134 rq->rq_disk = disk;
135 memcpy(rq->cmd, pc->c, 12);
136 if (drive->media == ide_tape)
137 rq->cmd[13] = REQ_IDETAPE_PC1;
138 ide_do_drive_cmd(drive, rq);
140 EXPORT_SYMBOL_GPL(ide_queue_pc_head);
143 * Add a special packet command request to the tail of the request queue,
144 * and wait for it to be serviced.
146 int ide_queue_pc_tail(ide_drive_t *drive, struct gendisk *disk,
147 struct ide_atapi_pc *pc)
149 struct request *rq;
150 int error;
152 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
153 rq->cmd_type = REQ_TYPE_SPECIAL;
154 rq->buffer = (char *)pc;
155 memcpy(rq->cmd, pc->c, 12);
156 if (drive->media == ide_tape)
157 rq->cmd[13] = REQ_IDETAPE_PC1;
158 error = blk_execute_rq(drive->queue, disk, rq, 0);
159 blk_put_request(rq);
161 return error;
163 EXPORT_SYMBOL_GPL(ide_queue_pc_tail);
165 int ide_do_test_unit_ready(ide_drive_t *drive, struct gendisk *disk)
167 struct ide_atapi_pc pc;
169 ide_init_pc(&pc);
170 pc.c[0] = TEST_UNIT_READY;
172 return ide_queue_pc_tail(drive, disk, &pc);
174 EXPORT_SYMBOL_GPL(ide_do_test_unit_ready);
176 int ide_do_start_stop(ide_drive_t *drive, struct gendisk *disk, int start)
178 struct ide_atapi_pc pc;
180 ide_init_pc(&pc);
181 pc.c[0] = START_STOP;
182 pc.c[4] = start;
184 if (drive->media == ide_tape)
185 pc.flags |= PC_FLAG_WAIT_FOR_DSC;
187 return ide_queue_pc_tail(drive, disk, &pc);
189 EXPORT_SYMBOL_GPL(ide_do_start_stop);
191 int ide_set_media_lock(ide_drive_t *drive, struct gendisk *disk, int on)
193 struct ide_atapi_pc pc;
195 if (drive->atapi_flags & IDE_AFLAG_NO_DOORLOCK)
196 return 0;
198 ide_init_pc(&pc);
199 pc.c[0] = ALLOW_MEDIUM_REMOVAL;
200 pc.c[4] = on;
202 return ide_queue_pc_tail(drive, disk, &pc);
204 EXPORT_SYMBOL_GPL(ide_set_media_lock);
206 /* TODO: unify the code thus making some arguments go away */
207 ide_startstop_t ide_pc_intr(ide_drive_t *drive, struct ide_atapi_pc *pc,
208 ide_handler_t *handler, unsigned int timeout, ide_expiry_t *expiry,
209 void (*update_buffers)(ide_drive_t *, struct ide_atapi_pc *),
210 void (*retry_pc)(ide_drive_t *), void (*dsc_handle)(ide_drive_t *),
211 int (*io_buffers)(ide_drive_t *, struct ide_atapi_pc *, unsigned, int))
213 ide_hwif_t *hwif = drive->hwif;
214 struct request *rq = hwif->hwgroup->rq;
215 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
216 xfer_func_t *xferfunc;
217 unsigned int temp;
218 u16 bcount;
219 u8 stat, ireason, scsi = drive->scsi;
221 debug_log("Enter %s - interrupt handler\n", __func__);
223 if (pc->flags & PC_FLAG_TIMEDOUT) {
224 drive->pc_callback(drive);
225 return ide_stopped;
228 /* Clear the interrupt */
229 stat = tp_ops->read_status(hwif);
231 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
232 if (hwif->dma_ops->dma_end(drive) ||
233 (drive->media == ide_tape && !scsi && (stat & ATA_ERR))) {
234 if (drive->media == ide_floppy && !scsi)
235 printk(KERN_ERR "%s: DMA %s error\n",
236 drive->name, rq_data_dir(pc->rq)
237 ? "write" : "read");
238 pc->flags |= PC_FLAG_DMA_ERROR;
239 } else {
240 pc->xferred = pc->req_xfer;
241 if (update_buffers)
242 update_buffers(drive, pc);
244 debug_log("%s: DMA finished\n", drive->name);
247 /* No more interrupts */
248 if ((stat & ATA_DRQ) == 0) {
249 debug_log("Packet command completed, %d bytes transferred\n",
250 pc->xferred);
252 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
254 local_irq_enable_in_hardirq();
256 if (drive->media == ide_tape && !scsi &&
257 (stat & ATA_ERR) && rq->cmd[0] == REQUEST_SENSE)
258 stat &= ~ATA_ERR;
260 if ((stat & ATA_ERR) || (pc->flags & PC_FLAG_DMA_ERROR)) {
261 /* Error detected */
262 debug_log("%s: I/O error\n", drive->name);
264 if (drive->media != ide_tape || scsi) {
265 pc->rq->errors++;
266 if (scsi)
267 goto cmd_finished;
270 if (rq->cmd[0] == REQUEST_SENSE) {
271 printk(KERN_ERR "%s: I/O error in request sense"
272 " command\n", drive->name);
273 return ide_do_reset(drive);
276 debug_log("[cmd %x]: check condition\n", rq->cmd[0]);
278 /* Retry operation */
279 retry_pc(drive);
281 /* queued, but not started */
282 return ide_stopped;
284 cmd_finished:
285 pc->error = 0;
286 if ((pc->flags & PC_FLAG_WAIT_FOR_DSC) &&
287 (stat & ATA_DSC) == 0) {
288 dsc_handle(drive);
289 return ide_stopped;
292 /* Command finished - Call the callback function */
293 drive->pc_callback(drive);
295 return ide_stopped;
298 if (pc->flags & PC_FLAG_DMA_IN_PROGRESS) {
299 pc->flags &= ~PC_FLAG_DMA_IN_PROGRESS;
300 printk(KERN_ERR "%s: The device wants to issue more interrupts "
301 "in DMA mode\n", drive->name);
302 ide_dma_off(drive);
303 return ide_do_reset(drive);
306 /* Get the number of bytes to transfer on this interrupt. */
307 ide_read_bcount_and_ireason(drive, &bcount, &ireason);
309 if (ireason & ATAPI_COD) {
310 printk(KERN_ERR "%s: CoD != 0 in %s\n", drive->name, __func__);
311 return ide_do_reset(drive);
314 if (((ireason & ATAPI_IO) == ATAPI_IO) ==
315 !!(pc->flags & PC_FLAG_WRITING)) {
316 /* Hopefully, we will never get here */
317 printk(KERN_ERR "%s: We wanted to %s, but the device wants us "
318 "to %s!\n", drive->name,
319 (ireason & ATAPI_IO) ? "Write" : "Read",
320 (ireason & ATAPI_IO) ? "Read" : "Write");
321 return ide_do_reset(drive);
324 if (!(pc->flags & PC_FLAG_WRITING)) {
325 /* Reading - Check that we have enough space */
326 temp = pc->xferred + bcount;
327 if (temp > pc->req_xfer) {
328 if (temp > pc->buf_size) {
329 printk(KERN_ERR "%s: The device wants to send "
330 "us more data than expected - "
331 "discarding data\n",
332 drive->name);
333 if (scsi)
334 temp = pc->buf_size - pc->xferred;
335 else
336 temp = 0;
337 if (temp) {
338 if (pc->sg)
339 io_buffers(drive, pc, temp, 0);
340 else
341 tp_ops->input_data(drive, NULL,
342 pc->cur_pos, temp);
343 printk(KERN_ERR "%s: transferred %d of "
344 "%d bytes\n",
345 drive->name,
346 temp, bcount);
348 pc->xferred += temp;
349 pc->cur_pos += temp;
350 ide_pad_transfer(drive, 0, bcount - temp);
351 ide_set_handler(drive, handler, timeout,
352 expiry);
353 return ide_started;
355 debug_log("The device wants to send us more data than "
356 "expected - allowing transfer\n");
358 xferfunc = tp_ops->input_data;
359 } else
360 xferfunc = tp_ops->output_data;
362 if ((drive->media == ide_floppy && !scsi && !pc->buf) ||
363 (drive->media == ide_tape && !scsi && pc->bh) ||
364 (scsi && pc->sg)) {
365 int done = io_buffers(drive, pc, bcount,
366 !!(pc->flags & PC_FLAG_WRITING));
368 /* FIXME: don't do partial completions */
369 if (drive->media == ide_floppy && !scsi)
370 ide_end_request(drive, 1, done >> 9);
371 } else
372 xferfunc(drive, NULL, pc->cur_pos, bcount);
374 /* Update the current position */
375 pc->xferred += bcount;
376 pc->cur_pos += bcount;
378 debug_log("[cmd %x] transferred %d bytes on that intr.\n",
379 rq->cmd[0], bcount);
381 /* And set the interrupt handler again */
382 ide_set_handler(drive, handler, timeout, expiry);
383 return ide_started;
385 EXPORT_SYMBOL_GPL(ide_pc_intr);
387 static u8 ide_read_ireason(ide_drive_t *drive)
389 ide_task_t task;
391 memset(&task, 0, sizeof(task));
392 task.tf_flags = IDE_TFLAG_IN_NSECT;
394 drive->hwif->tp_ops->tf_read(drive, &task);
396 return task.tf.nsect & 3;
399 static u8 ide_wait_ireason(ide_drive_t *drive, u8 ireason)
401 int retries = 100;
403 while (retries-- && ((ireason & ATAPI_COD) == 0 ||
404 (ireason & ATAPI_IO))) {
405 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
406 "a packet command, retrying\n", drive->name);
407 udelay(100);
408 ireason = ide_read_ireason(drive);
409 if (retries == 0) {
410 printk(KERN_ERR "%s: (IO,CoD != (0,1) while issuing "
411 "a packet command, ignoring\n",
412 drive->name);
413 ireason |= ATAPI_COD;
414 ireason &= ~ATAPI_IO;
418 return ireason;
421 ide_startstop_t ide_transfer_pc(ide_drive_t *drive, struct ide_atapi_pc *pc,
422 ide_handler_t *handler, unsigned int timeout,
423 ide_expiry_t *expiry)
425 ide_hwif_t *hwif = drive->hwif;
426 struct request *rq = hwif->hwgroup->rq;
427 ide_startstop_t startstop;
428 u8 ireason;
430 if (ide_wait_stat(&startstop, drive, ATA_DRQ, ATA_BUSY, WAIT_READY)) {
431 printk(KERN_ERR "%s: Strange, packet command initiated yet "
432 "DRQ isn't asserted\n", drive->name);
433 return startstop;
436 ireason = ide_read_ireason(drive);
437 if (drive->media == ide_tape && !drive->scsi)
438 ireason = ide_wait_ireason(drive, ireason);
440 if ((ireason & ATAPI_COD) == 0 || (ireason & ATAPI_IO)) {
441 printk(KERN_ERR "%s: (IO,CoD) != (0,1) while issuing "
442 "a packet command\n", drive->name);
443 return ide_do_reset(drive);
446 /* Set the interrupt routine */
447 ide_set_handler(drive, handler, timeout, expiry);
449 /* Begin DMA, if necessary */
450 if (pc->flags & PC_FLAG_DMA_OK) {
451 pc->flags |= PC_FLAG_DMA_IN_PROGRESS;
452 hwif->dma_ops->dma_start(drive);
455 /* Send the actual packet */
456 if ((drive->atapi_flags & IDE_AFLAG_ZIP_DRIVE) == 0)
457 hwif->tp_ops->output_data(drive, NULL, rq->cmd, 12);
459 return ide_started;
461 EXPORT_SYMBOL_GPL(ide_transfer_pc);
463 ide_startstop_t ide_issue_pc(ide_drive_t *drive, struct ide_atapi_pc *pc,
464 ide_handler_t *handler, unsigned int timeout,
465 ide_expiry_t *expiry)
467 ide_hwif_t *hwif = drive->hwif;
468 u16 bcount;
469 u8 dma = 0;
471 /* We haven't transferred any data yet */
472 pc->xferred = 0;
473 pc->cur_pos = pc->buf;
475 /* Request to transfer the entire buffer at once */
476 if (drive->media == ide_tape && !drive->scsi)
477 bcount = pc->req_xfer;
478 else
479 bcount = min(pc->req_xfer, 63 * 1024);
481 if (pc->flags & PC_FLAG_DMA_ERROR) {
482 pc->flags &= ~PC_FLAG_DMA_ERROR;
483 ide_dma_off(drive);
486 if ((pc->flags & PC_FLAG_DMA_OK) && drive->using_dma) {
487 if (drive->scsi)
488 hwif->sg_mapped = 1;
489 dma = !hwif->dma_ops->dma_setup(drive);
490 if (drive->scsi)
491 hwif->sg_mapped = 0;
494 if (!dma)
495 pc->flags &= ~PC_FLAG_DMA_OK;
497 ide_pktcmd_tf_load(drive, drive->scsi ? 0 : IDE_TFLAG_OUT_DEVICE,
498 bcount, dma);
500 /* Issue the packet command */
501 if (drive->atapi_flags & IDE_AFLAG_DRQ_INTERRUPT) {
502 ide_execute_command(drive, ATA_CMD_PACKET, handler,
503 timeout, NULL);
504 return ide_started;
505 } else {
506 ide_execute_pkt_cmd(drive);
507 return (*handler)(drive);
510 EXPORT_SYMBOL_GPL(ide_issue_pc);