initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / ide / ide-io.c
blobd20cd1eb537a985f75db000427b247ec0142e2f1
1 /*
2 * IDE I/O functions
4 * Basic PIO and command management functionality.
6 * This code was split off from ide.c. See ide.c for history and original
7 * copyrights.
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2, or (at your option) any
12 * later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * For the avoidance of doubt the "preferred form" of this code is one which
20 * is in an open non patent encumbered format. Where cryptographic key signing
21 * forms part of the process of creating an executable the information
22 * including keys needed to generate an equivalently functional executable
23 * are deemed to be part of the source code.
27 #include <linux/config.h>
28 #include <linux/module.h>
29 #include <linux/types.h>
30 #include <linux/string.h>
31 #include <linux/kernel.h>
32 #include <linux/timer.h>
33 #include <linux/mm.h>
34 #include <linux/interrupt.h>
35 #include <linux/major.h>
36 #include <linux/errno.h>
37 #include <linux/genhd.h>
38 #include <linux/blkpg.h>
39 #include <linux/slab.h>
40 #include <linux/init.h>
41 #include <linux/pci.h>
42 #include <linux/delay.h>
43 #include <linux/ide.h>
44 #include <linux/completion.h>
45 #include <linux/reboot.h>
46 #include <linux/cdrom.h>
47 #include <linux/seq_file.h>
48 #include <linux/device.h>
49 #include <linux/kmod.h>
51 #include <asm/byteorder.h>
52 #include <asm/irq.h>
53 #include <asm/uaccess.h>
54 #include <asm/io.h>
55 #include <asm/bitops.h>
57 static void ide_fill_flush_cmd(ide_drive_t *drive, struct request *rq)
59 char *buf = rq->cmd;
62 * reuse cdb space for ata command
64 memset(buf, 0, sizeof(rq->cmd));
66 rq->flags |= REQ_DRIVE_TASK | REQ_STARTED;
67 rq->buffer = buf;
68 rq->buffer[0] = WIN_FLUSH_CACHE;
70 if (ide_id_has_flush_cache_ext(drive->id) &&
71 (drive->capacity64 >= (1UL << 28)))
72 rq->buffer[0] = WIN_FLUSH_CACHE_EXT;
76 * preempt pending requests, and store this cache flush for immediate
77 * execution
79 static struct request *ide_queue_flush_cmd(ide_drive_t *drive,
80 struct request *rq, int post)
82 struct request *flush_rq = &HWGROUP(drive)->wrq;
85 * write cache disabled, clear the barrier bit and treat it like
86 * an ordinary write
88 if (!drive->wcache) {
89 rq->flags |= REQ_BAR_PREFLUSH;
90 return rq;
93 ide_init_drive_cmd(flush_rq);
94 ide_fill_flush_cmd(drive, flush_rq);
96 flush_rq->special = rq;
97 flush_rq->nr_sectors = rq->nr_sectors;
99 if (!post) {
100 drive->doing_barrier = 1;
101 flush_rq->flags |= REQ_BAR_PREFLUSH;
102 blkdev_dequeue_request(rq);
103 } else
104 flush_rq->flags |= REQ_BAR_POSTFLUSH;
106 __elv_add_request(drive->queue, flush_rq, ELEVATOR_INSERT_FRONT, 0);
107 HWGROUP(drive)->rq = NULL;
108 return flush_rq;
111 static int __ide_end_request(ide_drive_t *drive, struct request *rq,
112 int uptodate, int nr_sectors)
114 int ret = 1;
116 BUG_ON(!(rq->flags & REQ_STARTED));
119 * if failfast is set on a request, override number of sectors and
120 * complete the whole request right now
122 if (blk_noretry_request(rq) && end_io_error(uptodate))
123 nr_sectors = rq->hard_nr_sectors;
125 if (!blk_fs_request(rq) && end_io_error(uptodate) && !rq->errors)
126 rq->errors = -EIO;
129 * decide whether to reenable DMA -- 3 is a random magic for now,
130 * if we DMA timeout more than 3 times, just stay in PIO
132 if (drive->state == DMA_PIO_RETRY && drive->retry_pio <= 3) {
133 drive->state = 0;
134 HWGROUP(drive)->hwif->ide_dma_on(drive);
137 if (!end_that_request_first(rq, uptodate, nr_sectors)) {
138 add_disk_randomness(rq->rq_disk);
140 if (blk_rq_tagged(rq))
141 blk_queue_end_tag(drive->queue, rq);
143 blkdev_dequeue_request(rq);
144 HWGROUP(drive)->rq = NULL;
145 end_that_request_last(rq);
146 ret = 0;
148 return ret;
152 * ide_end_request - complete an IDE I/O
153 * @drive: IDE device for the I/O
154 * @uptodate:
155 * @nr_sectors: number of sectors completed
157 * This is our end_request wrapper function. We complete the I/O
158 * update random number input and dequeue the request, which if
159 * it was tagged may be out of order.
162 int ide_end_request (ide_drive_t *drive, int uptodate, int nr_sectors)
164 struct request *rq;
165 unsigned long flags;
166 int ret = 1;
168 spin_lock_irqsave(&ide_lock, flags);
169 rq = HWGROUP(drive)->rq;
171 if (!nr_sectors)
172 nr_sectors = rq->hard_cur_sectors;
174 if (!blk_barrier_rq(rq) || !drive->wcache)
175 ret = __ide_end_request(drive, rq, uptodate, nr_sectors);
176 else {
177 struct request *flush_rq = &HWGROUP(drive)->wrq;
179 flush_rq->nr_sectors -= nr_sectors;
180 if (!flush_rq->nr_sectors) {
181 ide_queue_flush_cmd(drive, rq, 1);
182 ret = 0;
186 spin_unlock_irqrestore(&ide_lock, flags);
187 return ret;
189 EXPORT_SYMBOL(ide_end_request);
192 * ide_complete_pm_request - end the current Power Management request
193 * @drive: target drive
194 * @rq: request
196 * This function cleans up the current PM request and stops the queue
197 * if necessary.
199 static void ide_complete_pm_request (ide_drive_t *drive, struct request *rq)
201 unsigned long flags;
203 #ifdef DEBUG_PM
204 printk("%s: completing PM request, %s\n", drive->name,
205 blk_pm_suspend_request(rq) ? "suspend" : "resume");
206 #endif
207 spin_lock_irqsave(&ide_lock, flags);
208 if (blk_pm_suspend_request(rq)) {
209 blk_stop_queue(drive->queue);
210 } else {
211 drive->blocked = 0;
212 blk_start_queue(drive->queue);
214 blkdev_dequeue_request(rq);
215 HWGROUP(drive)->rq = NULL;
216 end_that_request_last(rq);
217 spin_unlock_irqrestore(&ide_lock, flags);
221 * FIXME: probably move this somewhere else, name is bad too :)
223 u64 ide_get_error_location(ide_drive_t *drive, char *args)
225 u32 high, low;
226 u8 hcyl, lcyl, sect;
227 u64 sector;
229 high = 0;
230 hcyl = args[5];
231 lcyl = args[4];
232 sect = args[3];
234 if (ide_id_has_flush_cache_ext(drive->id)) {
235 low = (hcyl << 16) | (lcyl << 8) | sect;
236 HWIF(drive)->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
237 high = ide_read_24(drive);
238 } else {
239 u8 cur = HWIF(drive)->INB(IDE_SELECT_REG);
240 if (cur & 0x40)
241 low = (hcyl << 16) | (lcyl << 8) | sect;
242 else {
243 low = hcyl * drive->head * drive->sect;
244 low += lcyl * drive->sect;
245 low += sect - 1;
249 sector = ((u64) high << 24) | low;
250 return sector;
252 EXPORT_SYMBOL(ide_get_error_location);
254 static void ide_complete_barrier(ide_drive_t *drive, struct request *rq,
255 int error)
257 struct request *real_rq = rq->special;
258 int good_sectors, bad_sectors;
259 sector_t sector;
261 if (!error) {
262 if (blk_barrier_postflush(rq)) {
264 * this completes the barrier write
266 __ide_end_request(drive, real_rq, 1, real_rq->hard_nr_sectors);
267 drive->doing_barrier = 0;
268 } else {
270 * just indicate that we did the pre flush
272 real_rq->flags |= REQ_BAR_PREFLUSH;
273 elv_requeue_request(drive->queue, real_rq);
276 * all is fine, return
278 return;
282 * we need to end real_rq, but it's not on the queue currently.
283 * put it back on the queue, so we don't have to special case
284 * anything else for completing it
286 if (!blk_barrier_postflush(rq))
287 elv_requeue_request(drive->queue, real_rq);
290 * drive aborted flush command, assume FLUSH_CACHE_* doesn't
291 * work and disable barrier support
293 if (error & ABRT_ERR) {
294 printk(KERN_ERR "%s: barrier support doesn't work\n", drive->name);
295 __ide_end_request(drive, real_rq, -EOPNOTSUPP, real_rq->hard_nr_sectors);
296 blk_queue_ordered(drive->queue, 0);
297 blk_queue_issue_flush_fn(drive->queue, NULL);
298 } else {
300 * find out what part of the request failed
302 good_sectors = 0;
303 if (blk_barrier_postflush(rq)) {
304 sector = ide_get_error_location(drive, rq->buffer);
306 if ((sector >= real_rq->hard_sector) &&
307 (sector < real_rq->hard_sector + real_rq->hard_nr_sectors))
308 good_sectors = sector - real_rq->hard_sector;
309 } else
310 sector = real_rq->hard_sector;
312 bad_sectors = real_rq->hard_nr_sectors - good_sectors;
313 if (good_sectors)
314 __ide_end_request(drive, real_rq, 1, good_sectors);
315 if (bad_sectors)
316 __ide_end_request(drive, real_rq, 0, bad_sectors);
318 printk(KERN_ERR "%s: failed barrier write: "
319 "sector=%Lx(good=%d/bad=%d)\n",
320 drive->name, (unsigned long long)sector,
321 good_sectors, bad_sectors);
324 drive->doing_barrier = 0;
328 * ide_end_drive_cmd - end an explicit drive command
329 * @drive: command
330 * @stat: status bits
331 * @err: error bits
333 * Clean up after success/failure of an explicit drive command.
334 * These get thrown onto the queue so they are synchronized with
335 * real I/O operations on the drive.
337 * In LBA48 mode we have to read the register set twice to get
338 * all the extra information out.
341 void ide_end_drive_cmd (ide_drive_t *drive, u8 stat, u8 err)
343 ide_hwif_t *hwif = HWIF(drive);
344 unsigned long flags;
345 struct request *rq;
347 spin_lock_irqsave(&ide_lock, flags);
348 rq = HWGROUP(drive)->rq;
349 spin_unlock_irqrestore(&ide_lock, flags);
351 if (rq->flags & REQ_DRIVE_CMD) {
352 u8 *args = (u8 *) rq->buffer;
353 if (rq->errors == 0)
354 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
356 if (args) {
357 args[0] = stat;
358 args[1] = err;
359 args[2] = hwif->INB(IDE_NSECTOR_REG);
361 } else if (rq->flags & REQ_DRIVE_TASK) {
362 u8 *args = (u8 *) rq->buffer;
363 if (rq->errors == 0)
364 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
366 if (args) {
367 args[0] = stat;
368 args[1] = err;
369 args[2] = hwif->INB(IDE_NSECTOR_REG);
370 args[3] = hwif->INB(IDE_SECTOR_REG);
371 args[4] = hwif->INB(IDE_LCYL_REG);
372 args[5] = hwif->INB(IDE_HCYL_REG);
373 args[6] = hwif->INB(IDE_SELECT_REG);
375 } else if (rq->flags & REQ_DRIVE_TASKFILE) {
376 ide_task_t *args = (ide_task_t *) rq->special;
377 if (rq->errors == 0)
378 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
380 if (args) {
381 if (args->tf_in_flags.b.data) {
382 u16 data = hwif->INW(IDE_DATA_REG);
383 args->tfRegister[IDE_DATA_OFFSET] = (data) & 0xFF;
384 args->hobRegister[IDE_DATA_OFFSET] = (data >> 8) & 0xFF;
386 args->tfRegister[IDE_ERROR_OFFSET] = err;
387 /* be sure we're looking at the low order bits */
388 hwif->OUTB(drive->ctl & ~0x80, IDE_CONTROL_REG);
389 args->tfRegister[IDE_NSECTOR_OFFSET] = hwif->INB(IDE_NSECTOR_REG);
390 args->tfRegister[IDE_SECTOR_OFFSET] = hwif->INB(IDE_SECTOR_REG);
391 args->tfRegister[IDE_LCYL_OFFSET] = hwif->INB(IDE_LCYL_REG);
392 args->tfRegister[IDE_HCYL_OFFSET] = hwif->INB(IDE_HCYL_REG);
393 args->tfRegister[IDE_SELECT_OFFSET] = hwif->INB(IDE_SELECT_REG);
394 args->tfRegister[IDE_STATUS_OFFSET] = stat;
396 if (drive->addressing == 1) {
397 hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
398 args->hobRegister[IDE_FEATURE_OFFSET] = hwif->INB(IDE_FEATURE_REG);
399 args->hobRegister[IDE_NSECTOR_OFFSET] = hwif->INB(IDE_NSECTOR_REG);
400 args->hobRegister[IDE_SECTOR_OFFSET] = hwif->INB(IDE_SECTOR_REG);
401 args->hobRegister[IDE_LCYL_OFFSET] = hwif->INB(IDE_LCYL_REG);
402 args->hobRegister[IDE_HCYL_OFFSET] = hwif->INB(IDE_HCYL_REG);
405 } else if (blk_pm_request(rq)) {
406 #ifdef DEBUG_PM
407 printk("%s: complete_power_step(step: %d, stat: %x, err: %x)\n",
408 drive->name, rq->pm->pm_step, stat, err);
409 #endif
410 DRIVER(drive)->complete_power_step(drive, rq, stat, err);
411 if (rq->pm->pm_step == ide_pm_state_completed)
412 ide_complete_pm_request(drive, rq);
413 return;
416 spin_lock_irqsave(&ide_lock, flags);
417 blkdev_dequeue_request(rq);
419 if (blk_barrier_preflush(rq) || blk_barrier_postflush(rq))
420 ide_complete_barrier(drive, rq, err);
422 HWGROUP(drive)->rq = NULL;
423 end_that_request_last(rq);
424 spin_unlock_irqrestore(&ide_lock, flags);
427 EXPORT_SYMBOL(ide_end_drive_cmd);
430 * try_to_flush_leftover_data - flush junk
431 * @drive: drive to flush
433 * try_to_flush_leftover_data() is invoked in response to a drive
434 * unexpectedly having its DRQ_STAT bit set. As an alternative to
435 * resetting the drive, this routine tries to clear the condition
436 * by read a sector's worth of data from the drive. Of course,
437 * this may not help if the drive is *waiting* for data from *us*.
439 void try_to_flush_leftover_data (ide_drive_t *drive)
441 int i = (drive->mult_count ? drive->mult_count : 1) * SECTOR_WORDS;
443 if (drive->media != ide_disk)
444 return;
445 while (i > 0) {
446 u32 buffer[16];
447 u32 wcount = (i > 16) ? 16 : i;
449 i -= wcount;
450 HWIF(drive)->ata_input_data(drive, buffer, wcount);
454 EXPORT_SYMBOL(try_to_flush_leftover_data);
457 * FIXME Add an ATAPI error
461 * ide_error - handle an error on the IDE
462 * @drive: drive the error occurred on
463 * @msg: message to report
464 * @stat: status bits
466 * ide_error() takes action based on the error returned by the drive.
467 * For normal I/O that may well include retries. We deal with
468 * both new-style (taskfile) and old style command handling here.
469 * In the case of taskfile command handling there is work left to
470 * do
473 ide_startstop_t ide_error (ide_drive_t *drive, const char *msg, u8 stat)
475 ide_hwif_t *hwif;
476 struct request *rq;
477 u8 err;
479 err = ide_dump_status(drive, msg, stat);
480 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
481 return ide_stopped;
483 hwif = HWIF(drive);
484 /* retry only "normal" I/O: */
485 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK)) {
486 rq->errors = 1;
487 ide_end_drive_cmd(drive, stat, err);
488 return ide_stopped;
490 if (rq->flags & REQ_DRIVE_TASKFILE) {
491 rq->errors = 1;
492 ide_end_drive_cmd(drive, stat, err);
493 return ide_stopped;
496 if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) {
497 /* other bits are useless when BUSY */
498 rq->errors |= ERROR_RESET;
499 } else {
500 if (drive->media != ide_disk)
501 goto media_out;
503 if (stat & ERR_STAT) {
504 /* err has different meaning on cdrom and tape */
505 if (err == ABRT_ERR) {
506 if (drive->select.b.lba &&
507 (hwif->INB(IDE_COMMAND_REG) == WIN_SPECIFY))
508 /* some newer drives don't
509 * support WIN_SPECIFY
511 return ide_stopped;
512 } else if ((err & BAD_CRC) == BAD_CRC) {
513 drive->crc_count++;
514 /* UDMA crc error -- just retry the operation */
515 } else if (err & (BBD_ERR | ECC_ERR)) {
516 /* retries won't help these */
517 rq->errors = ERROR_MAX;
518 } else if (err & TRK0_ERR) {
519 /* help it find track zero */
520 rq->errors |= ERROR_RECAL;
523 media_out:
524 if ((stat & DRQ_STAT) && rq_data_dir(rq) != WRITE)
525 try_to_flush_leftover_data(drive);
527 if (hwif->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT)) {
528 /* force an abort */
529 hwif->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
531 if (rq->errors >= ERROR_MAX) {
532 DRIVER(drive)->end_request(drive, 0, 0);
533 } else {
534 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
535 ++rq->errors;
536 return ide_do_reset(drive);
538 if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
539 drive->special.b.recalibrate = 1;
540 ++rq->errors;
542 return ide_stopped;
545 EXPORT_SYMBOL(ide_error);
548 * ide_abort - abort pending IDE operatins
549 * @drive: drive the error occurred on
550 * @msg: message to report
552 * ide_abort kills and cleans up when we are about to do a
553 * host initiated reset on active commands. Longer term we
554 * want handlers to have sensible abort handling themselves
556 * This differs fundamentally from ide_error because in
557 * this case the command is doing just fine when we
558 * blow it away.
561 ide_startstop_t ide_abort(ide_drive_t *drive, const char *msg)
563 ide_hwif_t *hwif;
564 struct request *rq;
566 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
567 return ide_stopped;
569 hwif = HWIF(drive);
570 /* retry only "normal" I/O: */
571 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK)) {
572 rq->errors = 1;
573 ide_end_drive_cmd(drive, BUSY_STAT, 0);
574 return ide_stopped;
576 if (rq->flags & REQ_DRIVE_TASKFILE) {
577 rq->errors = 1;
578 ide_end_drive_cmd(drive, BUSY_STAT, 0);
579 return ide_stopped;
582 rq->errors |= ERROR_RESET;
583 DRIVER(drive)->end_request(drive, 0, 0);
584 return ide_stopped;
587 EXPORT_SYMBOL(ide_abort);
590 * ide_cmd - issue a simple drive command
591 * @drive: drive the command is for
592 * @cmd: command byte
593 * @nsect: sector byte
594 * @handler: handler for the command completion
596 * Issue a simple drive command with interrupts.
597 * The drive must be selected beforehand.
600 void ide_cmd (ide_drive_t *drive, u8 cmd, u8 nsect, ide_handler_t *handler)
602 ide_hwif_t *hwif = HWIF(drive);
603 if (IDE_CONTROL_REG)
604 hwif->OUTB(drive->ctl,IDE_CONTROL_REG); /* clear nIEN */
605 SELECT_MASK(drive,0);
606 hwif->OUTB(nsect,IDE_NSECTOR_REG);
607 ide_execute_command(drive, cmd, handler, WAIT_CMD, NULL);
610 EXPORT_SYMBOL(ide_cmd);
613 * drive_cmd_intr - drive command completion interrupt
614 * @drive: drive the completion interrupt occurred on
616 * drive_cmd_intr() is invoked on completion of a special DRIVE_CMD.
617 * We do any necessary daya reading and then wait for the drive to
618 * go non busy. At that point we may read the error data and complete
619 * the request
622 ide_startstop_t drive_cmd_intr (ide_drive_t *drive)
624 struct request *rq = HWGROUP(drive)->rq;
625 ide_hwif_t *hwif = HWIF(drive);
626 u8 *args = (u8 *) rq->buffer;
627 u8 stat = hwif->INB(IDE_STATUS_REG);
628 int retries = 10;
630 local_irq_enable();
631 if ((stat & DRQ_STAT) && args && args[3]) {
632 u8 io_32bit = drive->io_32bit;
633 drive->io_32bit = 0;
634 hwif->ata_input_data(drive, &args[4], args[3] * SECTOR_WORDS);
635 drive->io_32bit = io_32bit;
636 while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
637 udelay(100);
640 if (!OK_STAT(stat, READY_STAT, BAD_STAT) && DRIVER(drive) != NULL)
641 return DRIVER(drive)->error(drive, "drive_cmd", stat);
642 /* calls ide_end_drive_cmd */
643 ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
644 return ide_stopped;
647 EXPORT_SYMBOL(drive_cmd_intr);
650 * do_special - issue some special commands
651 * @drive: drive the command is for
653 * do_special() is used to issue WIN_SPECIFY, WIN_RESTORE, and WIN_SETMULT
654 * commands to a drive. It used to do much more, but has been scaled
655 * back.
658 ide_startstop_t do_special (ide_drive_t *drive)
660 special_t *s = &drive->special;
662 #ifdef DEBUG
663 printk("%s: do_special: 0x%02x\n", drive->name, s->all);
664 #endif
665 if (s->b.set_tune) {
666 s->b.set_tune = 0;
667 if (HWIF(drive)->tuneproc != NULL)
668 HWIF(drive)->tuneproc(drive, drive->tune_req);
669 return ide_stopped;
671 else
672 return DRIVER(drive)->special(drive);
675 EXPORT_SYMBOL(do_special);
678 * execute_drive_command - issue special drive command
679 * @drive: the drive to issue th command on
680 * @rq: the request structure holding the command
682 * execute_drive_cmd() issues a special drive command, usually
683 * initiated by ioctl() from the external hdparm program. The
684 * command can be a drive command, drive task or taskfile
685 * operation. Weirdly you can call it with NULL to wait for
686 * all commands to finish. Don't do this as that is due to change
689 ide_startstop_t execute_drive_cmd (ide_drive_t *drive, struct request *rq)
691 ide_hwif_t *hwif = HWIF(drive);
692 if (rq->flags & REQ_DRIVE_TASKFILE) {
693 ide_task_t *args = rq->special;
695 if (!args)
696 goto done;
698 hwif->data_phase = args->data_phase;
700 if (args->tf_out_flags.all != 0)
701 return flagged_taskfile(drive, args);
702 return do_rw_taskfile(drive, args);
703 } else if (rq->flags & REQ_DRIVE_TASK) {
704 u8 *args = rq->buffer;
705 u8 sel;
707 if (!args)
708 goto done;
709 #ifdef DEBUG
710 printk("%s: DRIVE_TASK_CMD ", drive->name);
711 printk("cmd=0x%02x ", args[0]);
712 printk("fr=0x%02x ", args[1]);
713 printk("ns=0x%02x ", args[2]);
714 printk("sc=0x%02x ", args[3]);
715 printk("lcyl=0x%02x ", args[4]);
716 printk("hcyl=0x%02x ", args[5]);
717 printk("sel=0x%02x\n", args[6]);
718 #endif
719 hwif->OUTB(args[1], IDE_FEATURE_REG);
720 hwif->OUTB(args[3], IDE_SECTOR_REG);
721 hwif->OUTB(args[4], IDE_LCYL_REG);
722 hwif->OUTB(args[5], IDE_HCYL_REG);
723 sel = (args[6] & ~0x10);
724 if (drive->select.b.unit)
725 sel |= 0x10;
726 hwif->OUTB(sel, IDE_SELECT_REG);
727 ide_cmd(drive, args[0], args[2], &drive_cmd_intr);
728 return ide_started;
729 } else if (rq->flags & REQ_DRIVE_CMD) {
730 u8 *args = rq->buffer;
732 if (!args)
733 goto done;
734 #ifdef DEBUG
735 printk("%s: DRIVE_CMD ", drive->name);
736 printk("cmd=0x%02x ", args[0]);
737 printk("sc=0x%02x ", args[1]);
738 printk("fr=0x%02x ", args[2]);
739 printk("xx=0x%02x\n", args[3]);
740 #endif
741 if (args[0] == WIN_SMART) {
742 hwif->OUTB(0x4f, IDE_LCYL_REG);
743 hwif->OUTB(0xc2, IDE_HCYL_REG);
744 hwif->OUTB(args[2],IDE_FEATURE_REG);
745 hwif->OUTB(args[1],IDE_SECTOR_REG);
746 ide_cmd(drive, args[0], args[3], &drive_cmd_intr);
747 return ide_started;
749 hwif->OUTB(args[2],IDE_FEATURE_REG);
750 ide_cmd(drive, args[0], args[1], &drive_cmd_intr);
751 return ide_started;
754 done:
756 * NULL is actually a valid way of waiting for
757 * all current requests to be flushed from the queue.
759 #ifdef DEBUG
760 printk("%s: DRIVE_CMD (null)\n", drive->name);
761 #endif
762 ide_end_drive_cmd(drive,
763 hwif->INB(IDE_STATUS_REG),
764 hwif->INB(IDE_ERROR_REG));
765 return ide_stopped;
768 EXPORT_SYMBOL(execute_drive_cmd);
771 * start_request - start of I/O and command issuing for IDE
773 * start_request() initiates handling of a new I/O request. It
774 * accepts commands and I/O (read/write) requests. It also does
775 * the final remapping for weird stuff like EZDrive. Once
776 * device mapper can work sector level the EZDrive stuff can go away
778 * FIXME: this function needs a rename
781 ide_startstop_t start_request (ide_drive_t *drive, struct request *rq)
783 ide_startstop_t startstop;
784 sector_t block;
786 BUG_ON(!(rq->flags & REQ_STARTED));
788 #ifdef DEBUG
789 printk("%s: start_request: current=0x%08lx\n",
790 HWIF(drive)->name, (unsigned long) rq);
791 #endif
793 /* bail early if we've exceeded max_failures */
794 if (drive->max_failures && (drive->failures > drive->max_failures)) {
795 goto kill_rq;
799 * bail early if we've sent a device to sleep, however how to wake
800 * this needs to be a masked flag. FIXME for proper operations.
802 if (drive->suspend_reset)
803 goto kill_rq;
805 block = rq->sector;
806 if (blk_fs_request(rq) &&
807 (drive->media == ide_disk || drive->media == ide_floppy)) {
808 block += drive->sect0;
810 /* Yecch - this will shift the entire interval,
811 possibly killing some innocent following sector */
812 if (block == 0 && drive->remap_0_to_1 == 1)
813 block = 1; /* redirect MBR access to EZ-Drive partn table */
815 if (blk_pm_suspend_request(rq) &&
816 rq->pm->pm_step == ide_pm_state_start_suspend)
817 /* Mark drive blocked when starting the suspend sequence. */
818 drive->blocked = 1;
819 else if (blk_pm_resume_request(rq) &&
820 rq->pm->pm_step == ide_pm_state_start_resume) {
822 * The first thing we do on wakeup is to wait for BSY bit to
823 * go away (with a looong timeout) as a drive on this hwif may
824 * just be POSTing itself.
825 * We do that before even selecting as the "other" device on
826 * the bus may be broken enough to walk on our toes at this
827 * point.
829 int rc;
830 #ifdef DEBUG_PM
831 printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name);
832 #endif
833 rc = ide_wait_not_busy(HWIF(drive), 35000);
834 if (rc)
835 printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name);
836 SELECT_DRIVE(drive);
837 HWIF(drive)->OUTB(8, HWIF(drive)->io_ports[IDE_CONTROL_OFFSET]);
838 rc = ide_wait_not_busy(HWIF(drive), 10000);
839 if (rc)
840 printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);
843 SELECT_DRIVE(drive);
844 if (ide_wait_stat(&startstop, drive, drive->ready_stat, BUSY_STAT|DRQ_STAT, WAIT_READY)) {
845 printk(KERN_ERR "%s: drive not ready for command\n", drive->name);
846 return startstop;
848 if (!drive->special.all) {
849 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK))
850 return execute_drive_cmd(drive, rq);
851 else if (rq->flags & REQ_DRIVE_TASKFILE)
852 return execute_drive_cmd(drive, rq);
853 else if (blk_pm_request(rq)) {
854 #ifdef DEBUG_PM
855 printk("%s: start_power_step(step: %d)\n",
856 drive->name, rq->pm->pm_step);
857 #endif
858 startstop = DRIVER(drive)->start_power_step(drive, rq);
859 if (startstop == ide_stopped &&
860 rq->pm->pm_step == ide_pm_state_completed)
861 ide_complete_pm_request(drive, rq);
862 return startstop;
864 return (DRIVER(drive)->do_request(drive, rq, block));
866 return do_special(drive);
867 kill_rq:
868 DRIVER(drive)->end_request(drive, 0, 0);
869 return ide_stopped;
872 EXPORT_SYMBOL(start_request);
875 * ide_stall_queue - pause an IDE device
876 * @drive: drive to stall
877 * @timeout: time to stall for (jiffies)
879 * ide_stall_queue() can be used by a drive to give excess bandwidth back
880 * to the hwgroup by sleeping for timeout jiffies.
883 void ide_stall_queue (ide_drive_t *drive, unsigned long timeout)
885 if (timeout > WAIT_WORSTCASE)
886 timeout = WAIT_WORSTCASE;
887 drive->sleep = timeout + jiffies;
890 EXPORT_SYMBOL(ide_stall_queue);
892 #define WAKEUP(drive) ((drive)->service_start + 2 * (drive)->service_time)
895 * choose_drive - select a drive to service
896 * @hwgroup: hardware group to select on
898 * choose_drive() selects the next drive which will be serviced.
899 * This is necessary because the IDE layer can't issue commands
900 * to both drives on the same cable, unlike SCSI.
903 static inline ide_drive_t *choose_drive (ide_hwgroup_t *hwgroup)
905 ide_drive_t *drive, *best;
907 repeat:
908 best = NULL;
909 drive = hwgroup->drive;
912 * drive is doing pre-flush, ordered write, post-flush sequence. even
913 * though that is 3 requests, it must be seen as a single transaction.
914 * we must not preempt this drive until that is complete
916 if (drive->doing_barrier) {
918 * small race where queue could get replugged during
919 * the 3-request flush cycle, just yank the plug since
920 * we want it to finish asap
922 blk_remove_plug(drive->queue);
923 return drive;
926 do {
927 if ((!drive->sleep || time_after_eq(jiffies, drive->sleep))
928 && !elv_queue_empty(drive->queue)) {
929 if (!best
930 || (drive->sleep && (!best->sleep || 0 < (signed long)(best->sleep - drive->sleep)))
931 || (!best->sleep && 0 < (signed long)(WAKEUP(best) - WAKEUP(drive))))
933 if (!blk_queue_plugged(drive->queue))
934 best = drive;
937 } while ((drive = drive->next) != hwgroup->drive);
938 if (best && best->nice1 && !best->sleep && best != hwgroup->drive && best->service_time > WAIT_MIN_SLEEP) {
939 long t = (signed long)(WAKEUP(best) - jiffies);
940 if (t >= WAIT_MIN_SLEEP) {
942 * We *may* have some time to spare, but first let's see if
943 * someone can potentially benefit from our nice mood today..
945 drive = best->next;
946 do {
947 if (!drive->sleep
948 /* FIXME: use time_before */
949 && 0 < (signed long)(WAKEUP(drive) - (jiffies - best->service_time))
950 && 0 < (signed long)((jiffies + t) - WAKEUP(drive)))
952 ide_stall_queue(best, min_t(long, t, 10 * WAIT_MIN_SLEEP));
953 goto repeat;
955 } while ((drive = drive->next) != best);
958 return best;
962 * Issue a new request to a drive from hwgroup
963 * Caller must have already done spin_lock_irqsave(&ide_lock, ..);
965 * A hwgroup is a serialized group of IDE interfaces. Usually there is
966 * exactly one hwif (interface) per hwgroup, but buggy controllers (eg. CMD640)
967 * may have both interfaces in a single hwgroup to "serialize" access.
968 * Or possibly multiple ISA interfaces can share a common IRQ by being grouped
969 * together into one hwgroup for serialized access.
971 * Note also that several hwgroups can end up sharing a single IRQ,
972 * possibly along with many other devices. This is especially common in
973 * PCI-based systems with off-board IDE controller cards.
975 * The IDE driver uses the single global ide_lock spinlock to protect
976 * access to the request queues, and to protect the hwgroup->busy flag.
978 * The first thread into the driver for a particular hwgroup sets the
979 * hwgroup->busy flag to indicate that this hwgroup is now active,
980 * and then initiates processing of the top request from the request queue.
982 * Other threads attempting entry notice the busy setting, and will simply
983 * queue their new requests and exit immediately. Note that hwgroup->busy
984 * remains set even when the driver is merely awaiting the next interrupt.
985 * Thus, the meaning is "this hwgroup is busy processing a request".
987 * When processing of a request completes, the completing thread or IRQ-handler
988 * will start the next request from the queue. If no more work remains,
989 * the driver will clear the hwgroup->busy flag and exit.
991 * The ide_lock (spinlock) is used to protect all access to the
992 * hwgroup->busy flag, but is otherwise not needed for most processing in
993 * the driver. This makes the driver much more friendlier to shared IRQs
994 * than previous designs, while remaining 100% (?) SMP safe and capable.
996 /* --BenH: made non-static as ide-pmac.c uses it to kick the hwgroup back
997 * into life on wakeup from machine sleep.
999 void ide_do_request (ide_hwgroup_t *hwgroup, int masked_irq)
1001 ide_drive_t *drive;
1002 ide_hwif_t *hwif;
1003 struct request *rq;
1004 ide_startstop_t startstop;
1006 /* for atari only: POSSIBLY BROKEN HERE(?) */
1007 ide_get_lock(ide_intr, hwgroup);
1009 /* caller must own ide_lock */
1010 BUG_ON(!irqs_disabled());
1012 while (!hwgroup->busy) {
1013 hwgroup->busy = 1;
1014 drive = choose_drive(hwgroup);
1015 if (drive == NULL) {
1016 unsigned long sleep = 0;
1017 hwgroup->rq = NULL;
1018 drive = hwgroup->drive;
1019 do {
1020 if (drive->sleep && (!sleep || 0 < (signed long)(sleep - drive->sleep)))
1021 sleep = drive->sleep;
1022 } while ((drive = drive->next) != hwgroup->drive);
1023 if (sleep) {
1025 * Take a short snooze, and then wake up this hwgroup again.
1026 * This gives other hwgroups on the same a chance to
1027 * play fairly with us, just in case there are big differences
1028 * in relative throughputs.. don't want to hog the cpu too much.
1030 if (time_before(sleep, jiffies + WAIT_MIN_SLEEP))
1031 sleep = jiffies + WAIT_MIN_SLEEP;
1032 #if 1
1033 if (timer_pending(&hwgroup->timer))
1034 printk(KERN_CRIT "ide_set_handler: timer already active\n");
1035 #endif
1036 /* so that ide_timer_expiry knows what to do */
1037 hwgroup->sleeping = 1;
1038 mod_timer(&hwgroup->timer, sleep);
1039 /* we purposely leave hwgroup->busy==1
1040 * while sleeping */
1041 } else {
1042 /* Ugly, but how can we sleep for the lock
1043 * otherwise? perhaps from tq_disk?
1046 /* for atari only */
1047 ide_release_lock();
1048 hwgroup->busy = 0;
1051 /* no more work for this hwgroup (for now) */
1052 return;
1054 hwif = HWIF(drive);
1055 if (hwgroup->hwif->sharing_irq &&
1056 hwif != hwgroup->hwif &&
1057 hwif->io_ports[IDE_CONTROL_OFFSET]) {
1058 /* set nIEN for previous hwif */
1059 SELECT_INTERRUPT(drive);
1061 hwgroup->hwif = hwif;
1062 hwgroup->drive = drive;
1063 drive->sleep = 0;
1064 drive->service_start = jiffies;
1066 if (blk_queue_plugged(drive->queue)) {
1067 printk(KERN_ERR "ide: huh? queue was plugged!\n");
1068 break;
1072 * we know that the queue isn't empty, but this can happen
1073 * if the q->prep_rq_fn() decides to kill a request
1075 rq = elv_next_request(drive->queue);
1076 if (!rq) {
1077 hwgroup->busy = 0;
1078 break;
1082 * if rq is a barrier write, issue pre cache flush if not
1083 * already done
1085 if (blk_barrier_rq(rq) && !blk_barrier_preflush(rq))
1086 rq = ide_queue_flush_cmd(drive, rq, 0);
1089 * Sanity: don't accept a request that isn't a PM request
1090 * if we are currently power managed. This is very important as
1091 * blk_stop_queue() doesn't prevent the elv_next_request()
1092 * above to return us whatever is in the queue. Since we call
1093 * ide_do_request() ourselves, we end up taking requests while
1094 * the queue is blocked...
1096 * We let requests forced at head of queue with ide-preempt
1097 * though. I hope that doesn't happen too much, hopefully not
1098 * unless the subdriver triggers such a thing in its own PM
1099 * state machine.
1101 if (drive->blocked && !blk_pm_request(rq) && !(rq->flags & REQ_PREEMPT)) {
1102 /* We clear busy, there should be no pending ATA command at this point. */
1103 hwgroup->busy = 0;
1104 break;
1107 hwgroup->rq = rq;
1110 * Some systems have trouble with IDE IRQs arriving while
1111 * the driver is still setting things up. So, here we disable
1112 * the IRQ used by this interface while the request is being started.
1113 * This may look bad at first, but pretty much the same thing
1114 * happens anyway when any interrupt comes in, IDE or otherwise
1115 * -- the kernel masks the IRQ while it is being handled.
1117 if (hwif->irq != masked_irq)
1118 disable_irq_nosync(hwif->irq);
1119 spin_unlock(&ide_lock);
1120 local_irq_enable();
1121 /* allow other IRQs while we start this request */
1122 startstop = start_request(drive, rq);
1123 spin_lock_irq(&ide_lock);
1124 if (hwif->irq != masked_irq)
1125 enable_irq(hwif->irq);
1126 if (startstop == ide_stopped)
1127 hwgroup->busy = 0;
1131 EXPORT_SYMBOL(ide_do_request);
1134 * Passes the stuff to ide_do_request
1136 void do_ide_request(request_queue_t *q)
1138 ide_drive_t *drive = q->queuedata;
1140 ide_do_request(HWGROUP(drive), IDE_NO_IRQ);
1144 * un-busy the hwgroup etc, and clear any pending DMA status. we want to
1145 * retry the current request in pio mode instead of risking tossing it
1146 * all away
1148 static ide_startstop_t ide_dma_timeout_retry(ide_drive_t *drive, int error)
1150 ide_hwif_t *hwif = HWIF(drive);
1151 struct request *rq;
1152 ide_startstop_t ret = ide_stopped;
1155 * end current dma transaction
1158 if (error < 0) {
1159 printk(KERN_WARNING "%s: DMA timeout error\n", drive->name);
1160 (void)HWIF(drive)->ide_dma_end(drive);
1161 ret = DRIVER(drive)->error(drive, "dma timeout error",
1162 hwif->INB(IDE_STATUS_REG));
1163 } else {
1164 printk(KERN_WARNING "%s: DMA timeout retry\n", drive->name);
1165 (void) hwif->ide_dma_timeout(drive);
1169 * disable dma for now, but remember that we did so because of
1170 * a timeout -- we'll reenable after we finish this next request
1171 * (or rather the first chunk of it) in pio.
1173 drive->retry_pio++;
1174 drive->state = DMA_PIO_RETRY;
1175 (void) hwif->ide_dma_off_quietly(drive);
1178 * un-busy drive etc (hwgroup->busy is cleared on return) and
1179 * make sure request is sane
1181 rq = HWGROUP(drive)->rq;
1182 HWGROUP(drive)->rq = NULL;
1184 rq->errors = 0;
1185 rq->sector = rq->bio->bi_sector;
1186 rq->current_nr_sectors = bio_iovec(rq->bio)->bv_len >> 9;
1187 rq->hard_cur_sectors = rq->current_nr_sectors;
1188 if (rq->bio)
1189 rq->buffer = NULL;
1191 return ret;
1195 * ide_timer_expiry - handle lack of an IDE interrupt
1196 * @data: timer callback magic (hwgroup)
1198 * An IDE command has timed out before the expected drive return
1199 * occurred. At this point we attempt to clean up the current
1200 * mess. If the current handler includes an expiry handler then
1201 * we invoke the expiry handler, and providing it is happy the
1202 * work is done. If that fails we apply generic recovery rules
1203 * invoking the handler and checking the drive DMA status. We
1204 * have an excessively incestuous relationship with the DMA
1205 * logic that wants cleaning up.
1208 void ide_timer_expiry (unsigned long data)
1210 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *) data;
1211 ide_handler_t *handler;
1212 ide_expiry_t *expiry;
1213 unsigned long flags;
1214 unsigned long wait = -1;
1216 spin_lock_irqsave(&ide_lock, flags);
1218 if ((handler = hwgroup->handler) == NULL) {
1220 * Either a marginal timeout occurred
1221 * (got the interrupt just as timer expired),
1222 * or we were "sleeping" to give other devices a chance.
1223 * Either way, we don't really want to complain about anything.
1225 if (hwgroup->sleeping) {
1226 hwgroup->sleeping = 0;
1227 hwgroup->busy = 0;
1229 } else {
1230 ide_drive_t *drive = hwgroup->drive;
1231 if (!drive) {
1232 printk(KERN_ERR "ide_timer_expiry: hwgroup->drive was NULL\n");
1233 hwgroup->handler = NULL;
1234 } else {
1235 ide_hwif_t *hwif;
1236 ide_startstop_t startstop = ide_stopped;
1237 if (!hwgroup->busy) {
1238 hwgroup->busy = 1; /* paranoia */
1239 printk(KERN_ERR "%s: ide_timer_expiry: hwgroup->busy was 0 ??\n", drive->name);
1241 if ((expiry = hwgroup->expiry) != NULL) {
1242 /* continue */
1243 if ((wait = expiry(drive)) > 0) {
1244 /* reset timer */
1245 hwgroup->timer.expires = jiffies + wait;
1246 add_timer(&hwgroup->timer);
1247 spin_unlock_irqrestore(&ide_lock, flags);
1248 return;
1251 hwgroup->handler = NULL;
1253 * We need to simulate a real interrupt when invoking
1254 * the handler() function, which means we need to
1255 * globally mask the specific IRQ:
1257 spin_unlock(&ide_lock);
1258 hwif = HWIF(drive);
1259 #if DISABLE_IRQ_NOSYNC
1260 disable_irq_nosync(hwif->irq);
1261 #else
1262 /* disable_irq_nosync ?? */
1263 disable_irq(hwif->irq);
1264 #endif /* DISABLE_IRQ_NOSYNC */
1265 /* local CPU only,
1266 * as if we were handling an interrupt */
1267 local_irq_disable();
1268 if (hwgroup->poll_timeout != 0) {
1269 startstop = handler(drive);
1270 } else if (drive_is_ready(drive)) {
1271 if (drive->waiting_for_dma)
1272 (void) hwgroup->hwif->ide_dma_lostirq(drive);
1273 (void)ide_ack_intr(hwif);
1274 printk(KERN_WARNING "%s: lost interrupt\n", drive->name);
1275 startstop = handler(drive);
1276 } else {
1277 if (drive->waiting_for_dma) {
1278 startstop = ide_dma_timeout_retry(drive, wait);
1279 } else
1280 startstop =
1281 DRIVER(drive)->error(drive, "irq timeout", hwif->INB(IDE_STATUS_REG));
1283 drive->service_time = jiffies - drive->service_start;
1284 spin_lock_irq(&ide_lock);
1285 enable_irq(hwif->irq);
1286 if (startstop == ide_stopped)
1287 hwgroup->busy = 0;
1290 ide_do_request(hwgroup, IDE_NO_IRQ);
1291 spin_unlock_irqrestore(&ide_lock, flags);
1294 EXPORT_SYMBOL(ide_timer_expiry);
1297 * unexpected_intr - handle an unexpected IDE interrupt
1298 * @irq: interrupt line
1299 * @hwgroup: hwgroup being processed
1301 * There's nothing really useful we can do with an unexpected interrupt,
1302 * other than reading the status register (to clear it), and logging it.
1303 * There should be no way that an irq can happen before we're ready for it,
1304 * so we needn't worry much about losing an "important" interrupt here.
1306 * On laptops (and "green" PCs), an unexpected interrupt occurs whenever
1307 * the drive enters "idle", "standby", or "sleep" mode, so if the status
1308 * looks "good", we just ignore the interrupt completely.
1310 * This routine assumes __cli() is in effect when called.
1312 * If an unexpected interrupt happens on irq15 while we are handling irq14
1313 * and if the two interfaces are "serialized" (CMD640), then it looks like
1314 * we could screw up by interfering with a new request being set up for
1315 * irq15.
1317 * In reality, this is a non-issue. The new command is not sent unless
1318 * the drive is ready to accept one, in which case we know the drive is
1319 * not trying to interrupt us. And ide_set_handler() is always invoked
1320 * before completing the issuance of any new drive command, so we will not
1321 * be accidentally invoked as a result of any valid command completion
1322 * interrupt.
1324 * Note that we must walk the entire hwgroup here. We know which hwif
1325 * is doing the current command, but we don't know which hwif burped
1326 * mysteriously.
1329 static void unexpected_intr (int irq, ide_hwgroup_t *hwgroup)
1331 u8 stat;
1332 ide_hwif_t *hwif = hwgroup->hwif;
1335 * handle the unexpected interrupt
1337 do {
1338 if (hwif->irq == irq) {
1339 stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
1340 if (!OK_STAT(stat, READY_STAT, BAD_STAT)) {
1341 /* Try to not flood the console with msgs */
1342 static unsigned long last_msgtime, count;
1343 ++count;
1344 if (time_after(jiffies, last_msgtime + HZ)) {
1345 last_msgtime = jiffies;
1346 printk(KERN_ERR "%s%s: unexpected interrupt, "
1347 "status=0x%02x, count=%ld\n",
1348 hwif->name,
1349 (hwif->next==hwgroup->hwif) ? "" : "(?)", stat, count);
1353 } while ((hwif = hwif->next) != hwgroup->hwif);
1357 * ide_intr - default IDE interrupt handler
1358 * @irq: interrupt number
1359 * @dev_id: hwif group
1360 * @regs: unused weirdness from the kernel irq layer
1362 * This is the default IRQ handler for the IDE layer. You should
1363 * not need to override it. If you do be aware it is subtle in
1364 * places
1366 * hwgroup->hwif is the interface in the group currently performing
1367 * a command. hwgroup->drive is the drive and hwgroup->handler is
1368 * the IRQ handler to call. As we issue a command the handlers
1369 * step through multiple states, reassigning the handler to the
1370 * next step in the process. Unlike a smart SCSI controller IDE
1371 * expects the main processor to sequence the various transfer
1372 * stages. We also manage a poll timer to catch up with most
1373 * timeout situations. There are still a few where the handlers
1374 * don't ever decide to give up.
1376 * The handler eventually returns ide_stopped to indicate the
1377 * request completed. At this point we issue the next request
1378 * on the hwgroup and the process begins again.
1381 irqreturn_t ide_intr (int irq, void *dev_id, struct pt_regs *regs)
1383 unsigned long flags;
1384 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *)dev_id;
1385 ide_hwif_t *hwif;
1386 ide_drive_t *drive;
1387 ide_handler_t *handler;
1388 ide_startstop_t startstop;
1390 spin_lock_irqsave(&ide_lock, flags);
1391 hwif = hwgroup->hwif;
1393 if (!ide_ack_intr(hwif)) {
1394 spin_unlock_irqrestore(&ide_lock, flags);
1395 return IRQ_NONE;
1398 if ((handler = hwgroup->handler) == NULL ||
1399 hwgroup->poll_timeout != 0) {
1401 * Not expecting an interrupt from this drive.
1402 * That means this could be:
1403 * (1) an interrupt from another PCI device
1404 * sharing the same PCI INT# as us.
1405 * or (2) a drive just entered sleep or standby mode,
1406 * and is interrupting to let us know.
1407 * or (3) a spurious interrupt of unknown origin.
1409 * For PCI, we cannot tell the difference,
1410 * so in that case we just ignore it and hope it goes away.
1412 * FIXME: unexpected_intr should be hwif-> then we can
1413 * remove all the ifdef PCI crap
1415 #ifdef CONFIG_BLK_DEV_IDEPCI
1416 if (hwif->pci_dev && !hwif->pci_dev->vendor)
1417 #endif /* CONFIG_BLK_DEV_IDEPCI */
1420 * Probably not a shared PCI interrupt,
1421 * so we can safely try to do something about it:
1423 unexpected_intr(irq, hwgroup);
1424 #ifdef CONFIG_BLK_DEV_IDEPCI
1425 } else {
1427 * Whack the status register, just in case
1428 * we have a leftover pending IRQ.
1430 (void) hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
1431 #endif /* CONFIG_BLK_DEV_IDEPCI */
1433 spin_unlock_irqrestore(&ide_lock, flags);
1434 return IRQ_NONE;
1436 drive = hwgroup->drive;
1437 if (!drive) {
1439 * This should NEVER happen, and there isn't much
1440 * we could do about it here.
1442 * [Note - this can occur if the drive is hot unplugged]
1444 spin_unlock_irqrestore(&ide_lock, flags);
1445 return IRQ_HANDLED;
1447 if (!drive_is_ready(drive)) {
1449 * This happens regularly when we share a PCI IRQ with
1450 * another device. Unfortunately, it can also happen
1451 * with some buggy drives that trigger the IRQ before
1452 * their status register is up to date. Hopefully we have
1453 * enough advance overhead that the latter isn't a problem.
1455 spin_unlock_irqrestore(&ide_lock, flags);
1456 return IRQ_NONE;
1458 if (!hwgroup->busy) {
1459 hwgroup->busy = 1; /* paranoia */
1460 printk(KERN_ERR "%s: ide_intr: hwgroup->busy was 0 ??\n", drive->name);
1462 hwgroup->handler = NULL;
1463 del_timer(&hwgroup->timer);
1464 spin_unlock(&ide_lock);
1466 if (drive->unmask)
1467 local_irq_enable();
1468 /* service this interrupt, may set handler for next interrupt */
1469 startstop = handler(drive);
1470 spin_lock_irq(&ide_lock);
1473 * Note that handler() may have set things up for another
1474 * interrupt to occur soon, but it cannot happen until
1475 * we exit from this routine, because it will be the
1476 * same irq as is currently being serviced here, and Linux
1477 * won't allow another of the same (on any CPU) until we return.
1479 drive->service_time = jiffies - drive->service_start;
1480 if (startstop == ide_stopped) {
1481 if (hwgroup->handler == NULL) { /* paranoia */
1482 hwgroup->busy = 0;
1483 ide_do_request(hwgroup, hwif->irq);
1484 } else {
1485 printk(KERN_ERR "%s: ide_intr: huh? expected NULL handler "
1486 "on exit\n", drive->name);
1489 spin_unlock_irqrestore(&ide_lock, flags);
1490 return IRQ_HANDLED;
1493 EXPORT_SYMBOL(ide_intr);
1496 * ide_init_drive_cmd - initialize a drive command request
1497 * @rq: request object
1499 * Initialize a request before we fill it in and send it down to
1500 * ide_do_drive_cmd. Commands must be set up by this function. Right
1501 * now it doesn't do a lot, but if that changes abusers will have a
1502 * nasty suprise.
1505 void ide_init_drive_cmd (struct request *rq)
1507 memset(rq, 0, sizeof(*rq));
1508 rq->flags = REQ_DRIVE_CMD;
1509 rq->ref_count = 1;
1512 EXPORT_SYMBOL(ide_init_drive_cmd);
1515 * ide_do_drive_cmd - issue IDE special command
1516 * @drive: device to issue command
1517 * @rq: request to issue
1518 * @action: action for processing
1520 * This function issues a special IDE device request
1521 * onto the request queue.
1523 * If action is ide_wait, then the rq is queued at the end of the
1524 * request queue, and the function sleeps until it has been processed.
1525 * This is for use when invoked from an ioctl handler.
1527 * If action is ide_preempt, then the rq is queued at the head of
1528 * the request queue, displacing the currently-being-processed
1529 * request and this function returns immediately without waiting
1530 * for the new rq to be completed. This is VERY DANGEROUS, and is
1531 * intended for careful use by the ATAPI tape/cdrom driver code.
1533 * If action is ide_next, then the rq is queued immediately after
1534 * the currently-being-processed-request (if any), and the function
1535 * returns without waiting for the new rq to be completed. As above,
1536 * This is VERY DANGEROUS, and is intended for careful use by the
1537 * ATAPI tape/cdrom driver code.
1539 * If action is ide_end, then the rq is queued at the end of the
1540 * request queue, and the function returns immediately without waiting
1541 * for the new rq to be completed. This is again intended for careful
1542 * use by the ATAPI tape/cdrom driver code.
1545 int ide_do_drive_cmd (ide_drive_t *drive, struct request *rq, ide_action_t action)
1547 unsigned long flags;
1548 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1549 DECLARE_COMPLETION(wait);
1550 int where = ELEVATOR_INSERT_BACK, err;
1551 int must_wait = (action == ide_wait || action == ide_head_wait);
1553 #ifdef CONFIG_BLK_DEV_PDC4030
1555 * FIXME: there should be a drive or hwif->special
1556 * handler that points here by default, not hacks
1557 * in the ide-io.c code
1559 * FIXME2: That code breaks power management if used with
1560 * this chipset, that really doesn't belong here !
1562 if (HWIF(drive)->chipset == ide_pdc4030 && rq->buffer != NULL)
1563 return -ENOSYS; /* special drive cmds not supported */
1564 #endif
1565 rq->errors = 0;
1566 rq->rq_status = RQ_ACTIVE;
1568 rq->rq_disk = drive->disk;
1571 * we need to hold an extra reference to request for safe inspection
1572 * after completion
1574 if (must_wait) {
1575 rq->ref_count++;
1576 rq->waiting = &wait;
1579 spin_lock_irqsave(&ide_lock, flags);
1580 if (action == ide_preempt)
1581 hwgroup->rq = NULL;
1582 if (action == ide_preempt || action == ide_head_wait) {
1583 where = ELEVATOR_INSERT_FRONT;
1584 rq->flags |= REQ_PREEMPT;
1586 __elv_add_request(drive->queue, rq, where, 0);
1587 ide_do_request(hwgroup, IDE_NO_IRQ);
1588 spin_unlock_irqrestore(&ide_lock, flags);
1590 err = 0;
1591 if (must_wait) {
1592 wait_for_completion(&wait);
1593 rq->waiting = NULL;
1594 if (rq->errors)
1595 err = -EIO;
1597 blk_put_request(rq);
1600 return err;
1603 EXPORT_SYMBOL(ide_do_drive_cmd);