added a lot of printk output to ease writing of emulator
[linux-2.4.21-pre4.git] / drivers / ide / ide-io.c
1 /*
2  *      IDE I/O functions
3  *
4  *      Basic PIO and command management functionality.
5  *
6  * This code was split off from ide.c. See ide.c for history and original
7  * copyrights.
8  *
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.
13  *
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.
18  *
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.
24  */
25  
26  
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/devfs_fs_kernel.h>
45 #include <linux/completion.h>
46 #include <linux/reboot.h>
47 #include <linux/cdrom.h>
48 #include <linux/seq_file.h>
49 #include <linux/kmod.h>
50
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>
56
57 #include "ide_modes.h"
58
59 #if (DISK_RECOVERY_TIME > 0)
60
61 Error So the User Has To Fix the Compilation And Stop Hacking Port 0x43
62 Does anyone ever use this anyway ??
63
64 /*
65  * For really screwy hardware (hey, at least it *can* be used with Linux)
66  * we can enforce a minimum delay time between successive operations.
67  */
68 static unsigned long read_timer (ide_hwif_t *hwif)
69 {
70         unsigned long t, flags;
71         int i;
72         
73         /* FIXME this is completely unsafe! */
74         local_irq_save(flags);
75         t = jiffies * 11932;
76         outb_p(0, 0x43);
77         i = inb_p(0x40);
78         i |= inb_p(0x40) << 8;
79         local_irq_restore(flags);
80         return (t - i);
81 }
82 #endif /* DISK_RECOVERY_TIME */
83
84 static inline void set_recovery_timer (ide_hwif_t *hwif)
85 {
86 #if (DISK_RECOVERY_TIME > 0)
87         hwif->last_time = read_timer(hwif);
88 #endif /* DISK_RECOVERY_TIME */
89 }
90
91 /*
92  *      ide_end_request         -       complete an IDE I/O
93  *      @drive: IDE device for the I/O
94  *      @uptodate: 
95  *
96  *      This is our end_request wrapper function. We complete the I/O
97  *      update random number input and dequeue the request.
98  */
99  
100 int ide_end_request (ide_drive_t *drive, int uptodate)
101 {
102         struct request *rq;
103         unsigned long flags;
104         int ret = 1;
105
106         spin_lock_irqsave(&io_request_lock, flags);
107         rq = HWGROUP(drive)->rq;
108
109         /*
110          * decide whether to reenable DMA -- 3 is a random magic for now,
111          * if we DMA timeout more than 3 times, just stay in PIO
112          */
113         if (drive->state == DMA_PIO_RETRY && drive->retry_pio <= 3) {
114                 drive->state = 0;
115                 HWGROUP(drive)->hwif->ide_dma_on(drive);
116         }
117
118         if (!end_that_request_first(rq, uptodate, drive->name)) {
119                 add_blkdev_randomness(MAJOR(rq->rq_dev));
120                 blkdev_dequeue_request(rq);
121                 HWGROUP(drive)->rq = NULL;
122                 end_that_request_last(rq);
123                 ret = 0;
124         }
125
126         spin_unlock_irqrestore(&io_request_lock, flags);
127         return ret;
128 }
129
130 EXPORT_SYMBOL(ide_end_request);
131
132 /**
133  *      ide_end_drive_cmd       -       end an explicit drive command
134  *      @drive: command 
135  *      @stat: status bits
136  *      @err: error bits
137  *
138  *      Clean up after success/failure of an explicit drive command.
139  *      These get thrown onto the queue so they are synchronized with
140  *      real I/O operations on the drive.
141  *
142  *      In LBA48 mode we have to read the register set twice to get
143  *      all the extra information out.
144  */
145  
146 void ide_end_drive_cmd (ide_drive_t *drive, u8 stat, u8 err)
147 {
148         ide_hwif_t *hwif = HWIF(drive);
149         unsigned long flags;
150         struct request *rq;
151
152         spin_lock_irqsave(&io_request_lock, flags);
153         rq = HWGROUP(drive)->rq;
154         spin_unlock_irqrestore(&io_request_lock, flags);
155
156         switch(rq->cmd) {
157                 case IDE_DRIVE_CMD:
158                 {
159                         u8 *args = (u8 *) rq->buffer;
160                         if (rq->errors == 0)
161                                 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
162
163                         if (args) {
164                                 args[0] = stat;
165                                 args[1] = err;
166                                 args[2] = hwif->INB(IDE_NSECTOR_REG);
167                         }
168                         break;
169                 }
170                 case IDE_DRIVE_TASK:
171                 {
172                         u8 *args = (u8 *) rq->buffer;
173                         if (rq->errors == 0)
174                                 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
175
176                         if (args) {
177                                 args[0] = stat;
178                                 args[1] = err;
179                                 args[2] = hwif->INB(IDE_NSECTOR_REG);
180                                 args[3] = hwif->INB(IDE_SECTOR_REG);
181                                 args[4] = hwif->INB(IDE_LCYL_REG);
182                                 args[5] = hwif->INB(IDE_HCYL_REG);
183                                 args[6] = hwif->INB(IDE_SELECT_REG);
184                         }
185                         break;
186                 }
187                 case IDE_DRIVE_TASKFILE:
188                 {
189                         ide_task_t *args = (ide_task_t *) rq->special;
190                         if (rq->errors == 0)
191                                 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
192                                 
193                         if (args) {
194                                 if (args->tf_in_flags.b.data) {
195                                         u16 data                        = hwif->INW(IDE_DATA_REG);
196                                         args->tfRegister[IDE_DATA_OFFSET]       = (data) & 0xFF;
197                                         args->hobRegister[IDE_DATA_OFFSET_HOB]  = (data >> 8) & 0xFF;
198                                 }
199                                 args->tfRegister[IDE_ERROR_OFFSET]   = err;
200                                 args->tfRegister[IDE_NSECTOR_OFFSET] = hwif->INB(IDE_NSECTOR_REG);
201                                 args->tfRegister[IDE_SECTOR_OFFSET]  = hwif->INB(IDE_SECTOR_REG);
202                                 args->tfRegister[IDE_LCYL_OFFSET]    = hwif->INB(IDE_LCYL_REG);
203                                 args->tfRegister[IDE_HCYL_OFFSET]    = hwif->INB(IDE_HCYL_REG);
204                                 args->tfRegister[IDE_SELECT_OFFSET]  = hwif->INB(IDE_SELECT_REG);
205                                 args->tfRegister[IDE_STATUS_OFFSET]  = stat;
206
207                                 if (drive->addressing == 1) {
208                                         hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG_HOB);
209                                         args->hobRegister[IDE_FEATURE_OFFSET_HOB] = hwif->INB(IDE_FEATURE_REG);
210                                         args->hobRegister[IDE_NSECTOR_OFFSET_HOB] = hwif->INB(IDE_NSECTOR_REG);
211                                         args->hobRegister[IDE_SECTOR_OFFSET_HOB]  = hwif->INB(IDE_SECTOR_REG);
212                                         args->hobRegister[IDE_LCYL_OFFSET_HOB]    = hwif->INB(IDE_LCYL_REG);
213                                         args->hobRegister[IDE_HCYL_OFFSET_HOB]    = hwif->INB(IDE_HCYL_REG);
214                                 }
215                         }
216                         break;
217                 }
218                 default:
219                         break;
220         }
221         spin_lock_irqsave(&io_request_lock, flags);
222         blkdev_dequeue_request(rq);
223         HWGROUP(drive)->rq = NULL;
224         end_that_request_last(rq);
225         spin_unlock_irqrestore(&io_request_lock, flags);
226 }
227
228 EXPORT_SYMBOL(ide_end_drive_cmd);
229
230 /**
231  *      try_to_flush_leftover_data      -       flush junk
232  *      @drive: drive to flush
233  *
234  *      try_to_flush_leftover_data() is invoked in response to a drive
235  *      unexpectedly having its DRQ_STAT bit set.  As an alternative to
236  *      resetting the drive, this routine tries to clear the condition
237  *      by read a sector's worth of data from the drive.  Of course,
238  *      this may not help if the drive is *waiting* for data from *us*.
239  */
240 void try_to_flush_leftover_data (ide_drive_t *drive)
241 {
242         int i = (drive->mult_count ? drive->mult_count : 1) * SECTOR_WORDS;
243
244         if (drive->media != ide_disk)
245                 return;
246         while (i > 0) {
247                 u32 buffer[16];
248                 u32 wcount = (i > 16) ? 16 : i;
249
250                 i -= wcount;
251                 HWIF(drive)->ata_input_data(drive, buffer, wcount);
252         }
253 }
254
255 EXPORT_SYMBOL(try_to_flush_leftover_data);
256
257 /*
258  * FIXME Add an ATAPI error
259  */
260
261 /**
262  *      ide_error       -       handle an error on the IDE
263  *      @drive: drive the error occurred on
264  *      @msg: message to report
265  *      @stat: status bits
266  *
267  *      ide_error() takes action based on the error returned by the drive.
268  *      For normal I/O that may well include retries. We deal with
269  *      both new-style (taskfile) and old style command handling here.
270  *      In the case of taskfile command handling there is work left to
271  *      do
272  */
273  
274 ide_startstop_t ide_error (ide_drive_t *drive, const char *msg, u8 stat)
275 {
276         ide_hwif_t *hwif;
277         struct request *rq;
278         u8 err;
279
280         err = ide_dump_status(drive, msg, stat);
281         if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
282                 return ide_stopped;
283
284         hwif = HWIF(drive);
285         /* retry only "normal" I/O: */
286         if (rq->cmd == IDE_DRIVE_CMD || rq->cmd == IDE_DRIVE_TASK) {
287                 rq->errors = 1;
288                 ide_end_drive_cmd(drive, stat, err);
289                 return ide_stopped;
290         }
291         if (rq->cmd == IDE_DRIVE_TASKFILE) {
292                 rq->errors = 1;
293                 ide_end_drive_cmd(drive, stat, err);
294 //              ide_end_taskfile(drive, stat, err);
295                 return ide_stopped;
296         }
297
298         if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) {
299                  /* other bits are useless when BUSY */
300                 rq->errors |= ERROR_RESET;
301         } else {
302                 if (drive->media != ide_disk)
303                         goto media_out;
304
305                 if (stat & ERR_STAT) {
306                         /* err has different meaning on cdrom and tape */
307                         if (err == ABRT_ERR) {
308                                 if (drive->select.b.lba &&
309                                     (hwif->INB(IDE_COMMAND_REG) == WIN_SPECIFY))
310                                         /* some newer drives don't
311                                          * support WIN_SPECIFY
312                                          */
313                                         return ide_stopped;
314                         } else if ((err & BAD_CRC) == BAD_CRC) {
315                                 drive->crc_count++;
316                                 /* UDMA crc error -- just retry the operation */
317                         } else if (err & (BBD_ERR | ECC_ERR)) {
318                                 /* retries won't help these */
319                                 rq->errors = ERROR_MAX;
320                         } else if (err & TRK0_ERR) {
321                                 /* help it find track zero */
322                                 rq->errors |= ERROR_RECAL;
323                         }
324                 }
325 media_out:
326                 if ((stat & DRQ_STAT) && rq->cmd != WRITE)
327                         try_to_flush_leftover_data(drive);
328         }
329         if (hwif->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT)) {
330                 /* force an abort */
331                 hwif->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
332         }
333         if (rq->errors >= ERROR_MAX) {
334                 if (drive->driver != NULL)
335                         DRIVER(drive)->end_request(drive, 0);
336                 else
337                         ide_end_request(drive, 0);
338         } else {
339                 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
340                         ++rq->errors;
341                         return ide_do_reset(drive);
342                 }
343                 if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
344                         drive->special.b.recalibrate = 1;
345                 ++rq->errors;
346         }
347         return ide_stopped;
348 }
349
350 EXPORT_SYMBOL(ide_error);
351
352 /**
353  *      ide_cmd         -       issue a simple drive command
354  *      @drive: drive the command is for
355  *      @cmd: command byte
356  *      @nsect: sector byte
357  *      @handler: handler for the command completion
358  *
359  *      Issue a simple drive command with interrupts.
360  *      The drive must be selected beforehand.
361  */
362
363 void ide_cmd (ide_drive_t *drive, u8 cmd, u8 nsect, ide_handler_t *handler)
364 {
365         ide_hwif_t *hwif = HWIF(drive);
366         if (HWGROUP(drive)->handler != NULL)
367                 BUG();
368         ide_set_handler(drive, handler, WAIT_CMD, NULL);
369         if (IDE_CONTROL_REG)
370                 hwif->OUTB(drive->ctl,IDE_CONTROL_REG); /* clear nIEN */
371         SELECT_MASK(drive,0);
372         hwif->OUTB(nsect,IDE_NSECTOR_REG);
373         hwif->OUTB(cmd,IDE_COMMAND_REG);
374 }
375
376 EXPORT_SYMBOL(ide_cmd);
377
378 /**
379  *      drive_cmd_intr          -       drive command completion interrupt
380  *      @drive: drive the completion interrupt occurred on
381  *
382  *      drive_cmd_intr() is invoked on completion of a special DRIVE_CMD.
383  *      We do any neccessary daya reading and then wait for the drive to
384  *      go non busy. At that point we may read the error data and complete
385  *      the request
386  */
387  
388 ide_startstop_t drive_cmd_intr (ide_drive_t *drive)
389 {
390         struct request *rq = HWGROUP(drive)->rq;
391         ide_hwif_t *hwif = HWIF(drive);
392         u8 *args = (u8 *) rq->buffer;
393         u8 stat = hwif->INB(IDE_STATUS_REG);
394         int retries = 10;
395
396         local_irq_enable();
397         if ((stat & DRQ_STAT) && args && args[3]) {
398                 u8 io_32bit = drive->io_32bit;
399                 drive->io_32bit = 0;
400                 hwif->ata_input_data(drive, &args[4], args[3] * SECTOR_WORDS);
401                 drive->io_32bit = io_32bit;
402                 while (((stat = hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && retries--)
403                         udelay(100);
404         }
405
406         if (!OK_STAT(stat, READY_STAT, BAD_STAT))
407                 return DRIVER(drive)->error(drive, "drive_cmd", stat);
408                 /* calls ide_end_drive_cmd */
409         ide_end_drive_cmd(drive, stat, hwif->INB(IDE_ERROR_REG));
410         return ide_stopped;
411 }
412
413 EXPORT_SYMBOL(drive_cmd_intr);
414
415 /**
416  *      do_special              -       issue some special commands
417  *      @drive: drive the command is for
418  *
419  *      do_special() is used to issue WIN_SPECIFY, WIN_RESTORE, and WIN_SETMULT
420  *      commands to a drive.  It used to do much more, but has been scaled
421  *      back.
422  */
423
424 ide_startstop_t do_special (ide_drive_t *drive)
425 {
426         special_t *s = &drive->special;
427
428 #ifdef DEBUG
429         printk("%s: do_special: 0x%02x\n", drive->name, s->all);
430 #endif
431         if (s->b.set_tune) {
432                 s->b.set_tune = 0;
433                 if (HWIF(drive)->tuneproc != NULL)
434                         HWIF(drive)->tuneproc(drive, drive->tune_req);
435         } else if (drive->driver != NULL) {
436                 return DRIVER(drive)->special(drive);
437         } else if (s->all) {
438                 printk(KERN_ERR "%s: bad special flag: 0x%02x\n", drive->name, s->all);
439                 s->all = 0;
440         }
441         return ide_stopped;
442 }
443
444 EXPORT_SYMBOL(do_special);
445
446 /**
447  *      execute_drive_command   -       issue special drive command
448  *      @drive: the drive to issue th command on
449  *      @rq: the request structure holding the command
450  *
451  *      execute_drive_cmd() issues a special drive command,  usually 
452  *      initiated by ioctl() from the external hdparm program. The
453  *      command can be a drive command, drive task or taskfile 
454  *      operation. Weirdly you can call it with NULL to wait for
455  *      all commands to finish. Don't do this as that is due to change
456  */
457
458 ide_startstop_t execute_drive_cmd (ide_drive_t *drive, struct request *rq)
459 {
460         ide_hwif_t *hwif = HWIF(drive);
461         switch(rq->cmd) {
462                 case IDE_DRIVE_TASKFILE:
463                 {
464                         ide_task_t *args = rq->special;
465  
466                         if (!(args)) break;
467  
468                         if (args->tf_out_flags.all != 0) 
469                                 return flagged_taskfile(drive, args);
470                         return do_rw_taskfile(drive, args);
471                 }
472                 case IDE_DRIVE_TASK:
473                 {
474                         u8 *args = rq->buffer;
475                         u8 sel;
476  
477                         if (!(args)) break;
478 #ifdef DEBUG
479                         printk("%s: DRIVE_TASK_CMD ", drive->name);
480                         printk("cmd=0x%02x ", args[0]);
481                         printk("fr=0x%02x ", args[1]);
482                         printk("ns=0x%02x ", args[2]);
483                         printk("sc=0x%02x ", args[3]);
484                         printk("lcyl=0x%02x ", args[4]);
485                         printk("hcyl=0x%02x ", args[5]);
486                         printk("sel=0x%02x\n", args[6]);
487 #endif
488                         hwif->OUTB(args[1], IDE_FEATURE_REG);
489                         hwif->OUTB(args[3], IDE_SECTOR_REG);
490                         hwif->OUTB(args[4], IDE_LCYL_REG);
491                         hwif->OUTB(args[5], IDE_HCYL_REG);
492                         sel = (args[6] & ~0x10);
493                         if (drive->select.b.unit)
494                                 sel |= 0x10;
495                         hwif->OUTB(sel, IDE_SELECT_REG);
496                         ide_cmd(drive, args[0], args[2], &drive_cmd_intr);
497                         return ide_started;
498                 }
499                 case IDE_DRIVE_CMD:
500                 {
501                         u8 *args = rq->buffer;
502  
503                         if (!(args)) break;
504 #ifdef DEBUG
505                         printk("%s: DRIVE_CMD ", drive->name);
506                         printk("cmd=0x%02x ", args[0]);
507                         printk("sc=0x%02x ", args[1]);
508                         printk("fr=0x%02x ", args[2]);
509                         printk("xx=0x%02x\n", args[3]);
510 #endif
511                         if (args[0] == WIN_SMART) {
512                                 hwif->OUTB(0x4f, IDE_LCYL_REG);
513                                 hwif->OUTB(0xc2, IDE_HCYL_REG);
514                                 hwif->OUTB(args[2],IDE_FEATURE_REG);
515                                 hwif->OUTB(args[1],IDE_SECTOR_REG);
516                                 ide_cmd(drive, args[0], args[3], &drive_cmd_intr);
517                                 return ide_started;
518                         }
519                         hwif->OUTB(args[2],IDE_FEATURE_REG);
520                         ide_cmd(drive, args[0], args[1], &drive_cmd_intr);
521                         return ide_started;
522                 }
523                 default:
524                         break;
525         }
526         /*
527          * NULL is actually a valid way of waiting for
528          * all current requests to be flushed from the queue.
529          */
530 #ifdef DEBUG
531         printk("%s: DRIVE_CMD (null)\n", drive->name);
532 #endif
533         ide_end_drive_cmd(drive,
534                         hwif->INB(IDE_STATUS_REG),
535                         hwif->INB(IDE_ERROR_REG));
536         return ide_stopped;
537 }
538
539 EXPORT_SYMBOL(execute_drive_cmd);
540
541 /**
542  *      start_request   -       start of I/O and command issuing for IDE
543  *
544  *      start_request() initiates handling of a new I/O request. It
545  *      accepts commands and I/O (read/write) requests. It also does
546  *      the final remapping for weird stuff like EZDrive. Once 
547  *      device mapper can work sector level the EZDrive stuff can go away
548  *
549  *      FIXME: this function needs a rename
550  */
551  
552 ide_startstop_t start_request (ide_drive_t *drive, struct request *rq)
553 {
554         ide_startstop_t startstop;
555         unsigned long block, blockend;
556         unsigned int minor = MINOR(rq->rq_dev), unit = minor >> PARTN_BITS;
557         ide_hwif_t *hwif = HWIF(drive);
558
559 #ifdef DEBUG
560         printk("%s: start_request: current=0x%08lx\n",
561                 hwif->name, (unsigned long) rq);
562 #endif
563
564         /* bail early if we've exceeded max_failures */
565         if (drive->max_failures && (drive->failures > drive->max_failures)) {
566                 goto kill_rq;
567         }
568
569         /*
570          * bail early if we've sent a device to sleep, however how to wake
571          * this needs to be a masked flag.  FIXME for proper operations.
572          */
573         if (drive->suspend_reset) {
574                 goto kill_rq;
575         }
576
577         if (unit >= MAX_DRIVES) {
578                 printk(KERN_ERR "%s: bad device number: %s\n",
579                         hwif->name, kdevname(rq->rq_dev));
580                 goto kill_rq;
581         }
582 #ifdef DEBUG
583         if (rq->bh && !buffer_locked(rq->bh)) {
584                 printk(KERN_ERR "%s: block not locked\n", drive->name);
585                 goto kill_rq;
586         }
587 #endif
588         block    = rq->sector;
589         blockend = block + rq->nr_sectors;
590
591         if (blk_fs_request(rq) &&
592             (drive->media == ide_disk || drive->media == ide_floppy)) {
593                 if ((blockend < block) || (blockend > drive->part[minor&PARTN_MASK].nr_sects)) {
594                         printk(KERN_ERR "%s%c: bad access: block=%ld, count=%ld\n", drive->name,
595                          (minor&PARTN_MASK)?'0'+(minor&PARTN_MASK):' ', block, rq->nr_sectors);
596                         goto kill_rq;
597                 }
598                 block += drive->part[minor&PARTN_MASK].start_sect + drive->sect0;
599         }
600         /* Yecch - this will shift the entire interval,
601            possibly killing some innocent following sector */
602         if (block == 0 && drive->remap_0_to_1 == 1)
603                 block = 1;  /* redirect MBR access to EZ-Drive partn table */
604
605 #if (DISK_RECOVERY_TIME > 0)
606         while ((read_timer() - hwif->last_time) < DISK_RECOVERY_TIME);
607 #endif
608
609         SELECT_DRIVE(drive);
610         if (ide_wait_stat(&startstop, drive, drive->ready_stat, BUSY_STAT|DRQ_STAT, WAIT_READY)) {
611                 printk(KERN_ERR "%s: drive not ready for command\n", drive->name);
612                 return startstop;
613         }
614         if (!drive->special.all) {
615                 switch(rq->cmd) {
616                         case IDE_DRIVE_CMD:
617                         case IDE_DRIVE_TASK:
618                                 return execute_drive_cmd(drive, rq);
619                         case IDE_DRIVE_TASKFILE:
620                                 return execute_drive_cmd(drive, rq);
621                         default:
622                                 break;
623                 }
624                 if (drive->driver != NULL) {
625                         return (DRIVER(drive)->do_request(drive, rq, block));
626                 }
627                 printk(KERN_WARNING "%s: media type %d not supported\n", drive->name, drive->media);
628                 goto kill_rq;
629         }
630         return do_special(drive);
631 kill_rq:
632         if (drive->driver != NULL)
633                 DRIVER(drive)->end_request(drive, 0);
634         else
635                 ide_end_request(drive, 0);
636         return ide_stopped;
637 }
638
639 EXPORT_SYMBOL(start_request);
640
641 int restart_request (ide_drive_t *drive, struct request *rq)
642 {
643         (void) start_request(drive, rq);
644         return 0;
645 }
646
647 EXPORT_SYMBOL(restart_request);
648
649 /**
650  *      ide_stall_queue         -       pause an IDE device
651  *      @drive: drive to stall
652  *      @timeout: time to stall for (jiffies)
653  *
654  *      ide_stall_queue() can be used by a drive to give excess bandwidth back
655  *      to the hwgroup by sleeping for timeout jiffies.
656  */
657  
658 void ide_stall_queue (ide_drive_t *drive, unsigned long timeout)
659 {
660         if (timeout > WAIT_WORSTCASE)
661                 timeout = WAIT_WORSTCASE;
662         drive->sleep = timeout + jiffies;
663 }
664
665 EXPORT_SYMBOL(ide_stall_queue);
666
667 #define WAKEUP(drive)   ((drive)->service_start + 2 * (drive)->service_time)
668
669 /**
670  *      choose_drive            -       select a drive to service
671  *      @hwgroup: hardware group to select on
672  *
673  *      choose_drive() selects the next drive which will be serviced.
674  *      This is neccessary because the IDE layer can't issue commands
675  *      to both drives on the same cable, unlike SCSI.
676  */
677  
678 static inline ide_drive_t *choose_drive (ide_hwgroup_t *hwgroup)
679 {
680         ide_drive_t *drive, *best;
681
682 repeat: 
683         best = NULL;
684         drive = hwgroup->drive;
685         do {
686                 if (!blk_queue_empty(&drive->queue) && (!drive->sleep || time_after_eq(jiffies, drive->sleep))) {
687                         if (!best
688                          || (drive->sleep && (!best->sleep || 0 < (signed long)(best->sleep - drive->sleep)))
689                          || (!best->sleep && 0 < (signed long)(WAKEUP(best) - WAKEUP(drive))))
690                         {
691                                 if (!blk_queue_plugged(&drive->queue))
692                                         best = drive;
693                         }
694                 }
695         } while ((drive = drive->next) != hwgroup->drive);
696         if (best && best->nice1 && !best->sleep && best != hwgroup->drive && best->service_time > WAIT_MIN_SLEEP) {
697                 long t = (signed long)(WAKEUP(best) - jiffies);
698                 if (t >= WAIT_MIN_SLEEP) {
699                 /*
700                  * We *may* have some time to spare, but first let's see if
701                  * someone can potentially benefit from our nice mood today..
702                  */
703                         drive = best->next;
704                         do {
705                                 if (!drive->sleep
706                                  && 0 < (signed long)(WAKEUP(drive) - (jiffies - best->service_time))
707                                  && 0 < (signed long)((jiffies + t) - WAKEUP(drive)))
708                                 {
709                                         ide_stall_queue(best, IDE_MIN(t, 10 * WAIT_MIN_SLEEP));
710                                         goto repeat;
711                                 }
712                         } while ((drive = drive->next) != best);
713                 }
714         }
715         return best;
716 }
717
718 /*
719  * Issue a new request to a drive from hwgroup
720  * Caller must have already done spin_lock_irqsave(&io_request_lock, ..);
721  *
722  * A hwgroup is a serialized group of IDE interfaces.  Usually there is
723  * exactly one hwif (interface) per hwgroup, but buggy controllers (eg. CMD640)
724  * may have both interfaces in a single hwgroup to "serialize" access.
725  * Or possibly multiple ISA interfaces can share a common IRQ by being grouped
726  * together into one hwgroup for serialized access.
727  *
728  * Note also that several hwgroups can end up sharing a single IRQ,
729  * possibly along with many other devices.  This is especially common in
730  * PCI-based systems with off-board IDE controller cards.
731  *
732  * The IDE driver uses the single global io_request_lock spinlock to protect
733  * access to the request queues, and to protect the hwgroup->busy flag.
734  *
735  * The first thread into the driver for a particular hwgroup sets the
736  * hwgroup->busy flag to indicate that this hwgroup is now active,
737  * and then initiates processing of the top request from the request queue.
738  *
739  * Other threads attempting entry notice the busy setting, and will simply
740  * queue their new requests and exit immediately.  Note that hwgroup->busy
741  * remains set even when the driver is merely awaiting the next interrupt.
742  * Thus, the meaning is "this hwgroup is busy processing a request".
743  *
744  * When processing of a request completes, the completing thread or IRQ-handler
745  * will start the next request from the queue.  If no more work remains,
746  * the driver will clear the hwgroup->busy flag and exit.
747  *
748  * The io_request_lock (spinlock) is used to protect all access to the
749  * hwgroup->busy flag, but is otherwise not needed for most processing in
750  * the driver.  This makes the driver much more friendlier to shared IRQs
751  * than previous designs, while remaining 100% (?) SMP safe and capable.
752  */
753 /* --BenH: made non-static as ide-pmac.c uses it to kick the hwgroup back
754  *         into life on wakeup from machine sleep.
755  */ 
756 void ide_do_request (ide_hwgroup_t *hwgroup, int masked_irq)
757 {
758         ide_drive_t     *drive;
759         ide_hwif_t      *hwif;
760         struct request  *rq;
761         ide_startstop_t startstop;
762
763         /* for atari only: POSSIBLY BROKEN HERE(?) */
764         ide_get_lock(&ide_intr_lock, ide_intr, hwgroup);
765
766         /* necessary paranoia: ensure IRQs are masked on local CPU */
767         local_irq_disable();
768
769         while (!hwgroup->busy) {
770                 hwgroup->busy = 1;
771                 drive = choose_drive(hwgroup);
772                 if (drive == NULL) {
773                         unsigned long sleep = 0;
774                         hwgroup->rq = NULL;
775                         drive = hwgroup->drive;
776                         do {
777                                 if (drive->sleep && (!sleep || 0 < (signed long)(sleep - drive->sleep)))
778                                         sleep = drive->sleep;
779                         } while ((drive = drive->next) != hwgroup->drive);
780                         if (sleep) {
781                 /*
782                  * Take a short snooze, and then wake up this hwgroup again.
783                  * This gives other hwgroups on the same a chance to
784                  * play fairly with us, just in case there are big differences
785                  * in relative throughputs.. don't want to hog the cpu too much.
786                  */
787                                 if (time_before(sleep, jiffies + WAIT_MIN_SLEEP))
788                                         sleep = jiffies + WAIT_MIN_SLEEP;
789 #if 1
790                                 if (timer_pending(&hwgroup->timer))
791                                         printk(KERN_ERR "ide_set_handler: timer already active\n");
792 #endif
793                                 /* so that ide_timer_expiry knows what to do */
794                                 hwgroup->sleeping = 1;
795                                 mod_timer(&hwgroup->timer, sleep);
796                                 /* we purposely leave hwgroup->busy==1
797                                  * while sleeping */
798                         } else {
799                                 /* Ugly, but how can we sleep for the lock
800                                  * otherwise? perhaps from tq_disk?
801                                  */
802
803                                 /* for atari only */
804                                 ide_release_lock(&ide_intr_lock);
805                                 hwgroup->busy = 0;
806                         }
807                         /* no more work for this hwgroup (for now) */
808                         return;
809                 }
810                 hwif = HWIF(drive);
811                 if (hwgroup->hwif->sharing_irq &&
812                     hwif != hwgroup->hwif &&
813                     hwif->io_ports[IDE_CONTROL_OFFSET]) {
814                         /* set nIEN for previous hwif */
815                         SELECT_INTERRUPT(drive);
816                 }
817                 hwgroup->hwif = hwif;
818                 hwgroup->drive = drive;
819                 drive->sleep = 0;
820                 drive->service_start = jiffies;
821
822                 /* paranoia */
823                 if (blk_queue_plugged(&drive->queue))
824                         printk(KERN_ERR "%s: Huh? nuking plugged queue\n", drive->name);
825
826                 rq = blkdev_entry_next_request(&drive->queue.queue_head);
827                 hwgroup->rq = rq;
828                 /*
829                  * Some systems have trouble with IDE IRQs arriving while
830                  * the driver is still setting things up.  So, here we disable
831                  * the IRQ used by this interface while the request is being started.
832                  * This may look bad at first, but pretty much the same thing
833                  * happens anyway when any interrupt comes in, IDE or otherwise
834                  *  -- the kernel masks the IRQ while it is being handled.
835                  */
836                 if (masked_irq && hwif->irq != masked_irq)
837                         disable_irq_nosync(hwif->irq);
838                 spin_unlock(&io_request_lock);
839                 local_irq_enable();
840                         /* allow other IRQs while we start this request */
841                 startstop = start_request(drive, rq);
842                 spin_lock_irq(&io_request_lock);
843                 if (masked_irq && hwif->irq != masked_irq)
844                         enable_irq(hwif->irq);
845                 if (startstop == ide_stopped)
846                         hwgroup->busy = 0;
847         }
848 }
849
850 EXPORT_SYMBOL(ide_do_request);
851
852 /*
853  * ide_get_queue() returns the queue which corresponds to a given device.
854  */
855 request_queue_t *ide_get_queue (kdev_t dev)
856 {
857         ide_hwif_t *hwif = (ide_hwif_t *)blk_dev[MAJOR(dev)].data;
858
859         return &hwif->drives[DEVICE_NR(dev) & 1].queue;
860 }
861
862 EXPORT_SYMBOL(ide_get_queue);
863
864 /*
865  * Passes the stuff to ide_do_request
866  */
867 void do_ide_request(request_queue_t *q)
868 {
869         ide_do_request(q->queuedata, 0);
870 }
871
872 /*
873  * un-busy the hwgroup etc, and clear any pending DMA status. we want to
874  * retry the current request in pio mode instead of risking tossing it
875  * all away
876  */
877 void ide_dma_timeout_retry(ide_drive_t *drive)
878 {
879         ide_hwif_t *hwif = HWIF(drive);
880         struct request *rq;
881
882         /*
883          * end current dma transaction
884          */
885         (void) hwif->ide_dma_end(drive);
886
887         /*
888          * complain a little, later we might remove some of this verbosity
889          */
890         printk(KERN_ERR "%s: timeout waiting for DMA\n", drive->name);
891         (void) hwif->ide_dma_timeout(drive);
892
893         /*
894          * disable dma for now, but remember that we did so because of
895          * a timeout -- we'll reenable after we finish this next request
896          * (or rather the first chunk of it) in pio.
897          */
898         drive->retry_pio++;
899         drive->state = DMA_PIO_RETRY;
900         (void) hwif->ide_dma_off_quietly(drive);
901
902         /*
903          * un-busy drive etc (hwgroup->busy is cleared on return) and
904          * make sure request is sane
905          */
906         rq = HWGROUP(drive)->rq;
907         HWGROUP(drive)->rq = NULL;
908
909         rq->errors = 0;
910         rq->sector = rq->bh->b_rsector;
911         rq->current_nr_sectors = rq->bh->b_size >> 9;
912         rq->hard_cur_sectors = rq->current_nr_sectors;
913         rq->buffer = rq->bh->b_data;
914 }
915
916 EXPORT_SYMBOL(ide_dma_timeout_retry);
917
918 /**
919  *      ide_timer_expiry        -       handle lack of an IDE interrupt
920  *      @data: timer callback magic (hwgroup)
921  *
922  *      An IDE command has timed out before the expected drive return
923  *      occurred. At this point we attempt to clean up the current
924  *      mess. If the current handler includes an expiry handler then
925  *      we invoke the expiry handler, and providing it is happy the
926  *      work is done. If that fails we apply generic recovery rules
927  *      invoking the handler and checking the drive DMA status. We
928  *      have an excessively incestuous relationship with the DMA
929  *      logic that wants cleaning up.
930  */
931  
932 void ide_timer_expiry (unsigned long data)
933 {
934         ide_hwgroup_t   *hwgroup = (ide_hwgroup_t *) data;
935         ide_handler_t   *handler;
936         ide_expiry_t    *expiry;
937         unsigned long   flags;
938         unsigned long   wait;
939
940         spin_lock_irqsave(&io_request_lock, flags);
941         del_timer(&hwgroup->timer);
942
943         if ((handler = hwgroup->handler) == NULL) {
944                 /*
945                  * Either a marginal timeout occurred
946                  * (got the interrupt just as timer expired),
947                  * or we were "sleeping" to give other devices a chance.
948                  * Either way, we don't really want to complain about anything.
949                  */
950                 if (hwgroup->sleeping) {
951                         hwgroup->sleeping = 0;
952                         hwgroup->busy = 0;
953                 }
954         } else {
955                 ide_drive_t *drive = hwgroup->drive;
956                 if (!drive) {
957                         printk(KERN_ERR "ide_timer_expiry: hwgroup->drive was NULL\n");
958                         hwgroup->handler = NULL;
959                 } else {
960                         ide_hwif_t *hwif;
961                         ide_startstop_t startstop = ide_stopped;
962                         if (!hwgroup->busy) {
963                                 hwgroup->busy = 1;      /* paranoia */
964                                 printk(KERN_ERR "%s: ide_timer_expiry: hwgroup->busy was 0 ??\n", drive->name);
965                         }
966                         if ((expiry = hwgroup->expiry) != NULL) {
967                                 /* continue */
968                                 if ((wait = expiry(drive)) != 0) {
969                                         /* reset timer */
970                                         hwgroup->timer.expires  = jiffies + wait;
971                                         add_timer(&hwgroup->timer);
972                                         spin_unlock_irqrestore(&io_request_lock, flags);
973                                         return;
974                                 }
975                         }
976                         hwgroup->handler = NULL;
977                         /*
978                          * We need to simulate a real interrupt when invoking
979                          * the handler() function, which means we need to
980                          * globally mask the specific IRQ:
981                          */
982                         spin_unlock(&io_request_lock);
983                         hwif  = HWIF(drive);
984 #if DISABLE_IRQ_NOSYNC
985                         disable_irq_nosync(hwif->irq);
986 #else
987                         /* disable_irq_nosync ?? */
988                         disable_irq(hwif->irq);
989 #endif /* DISABLE_IRQ_NOSYNC */
990
991                         /* local CPU only,
992                          * as if we were handling an interrupt */
993                         local_irq_disable();
994                         if (hwgroup->poll_timeout != 0) {
995                                 startstop = handler(drive);
996                         } else if (drive_is_ready(drive)) {
997                                 if (drive->waiting_for_dma)
998                                         (void) hwgroup->hwif->ide_dma_lostirq(drive);
999                                 (void)ide_ack_intr(hwif);
1000                                 printk(KERN_ERR "%s: lost interrupt\n", drive->name);
1001                                 startstop = handler(drive);
1002                         } else {
1003                                 if (drive->waiting_for_dma) {
1004                                         startstop = ide_stopped;
1005                                         ide_dma_timeout_retry(drive);
1006                                 } else
1007                                         startstop = DRIVER(drive)->error(drive, "irq timeout", hwif->INB(IDE_STATUS_REG));
1008                         }
1009                         set_recovery_timer(hwif);
1010                         drive->service_time = jiffies - drive->service_start;
1011                         enable_irq(hwif->irq);
1012                         spin_lock_irq(&io_request_lock);
1013                         if (startstop == ide_stopped)
1014                                 hwgroup->busy = 0;
1015                 }
1016         }
1017         ide_do_request(hwgroup, 0);
1018         spin_unlock_irqrestore(&io_request_lock, flags);
1019 }
1020
1021 EXPORT_SYMBOL(ide_timer_expiry);
1022
1023 /**
1024  *      unexpected_intr         -       handle an unexpected IDE interrupt
1025  *      @irq: interrupt line
1026  *      @hwgroup: hwgroup being processed
1027  *
1028  *      There's nothing really useful we can do with an unexpected interrupt,
1029  *      other than reading the status register (to clear it), and logging it.
1030  *      There should be no way that an irq can happen before we're ready for it,
1031  *      so we needn't worry much about losing an "important" interrupt here.
1032  *
1033  *      On laptops (and "green" PCs), an unexpected interrupt occurs whenever
1034  *      the drive enters "idle", "standby", or "sleep" mode, so if the status
1035  *      looks "good", we just ignore the interrupt completely.
1036  *
1037  *      This routine assumes __cli() is in effect when called.
1038  *
1039  *      If an unexpected interrupt happens on irq15 while we are handling irq14
1040  *      and if the two interfaces are "serialized" (CMD640), then it looks like
1041  *      we could screw up by interfering with a new request being set up for 
1042  *      irq15.
1043  *
1044  *      In reality, this is a non-issue.  The new command is not sent unless 
1045  *      the drive is ready to accept one, in which case we know the drive is
1046  *      not trying to interrupt us.  And ide_set_handler() is always invoked
1047  *      before completing the issuance of any new drive command, so we will not
1048  *      be accidentally invoked as a result of any valid command completion
1049  *      interrupt.
1050  *
1051  *      Note that we must walk the entire hwgroup here. We know which hwif
1052  *      is doing the current command, but we don't know which hwif burped
1053  *      mysteriously.
1054  */
1055  
1056 static void unexpected_intr (int irq, ide_hwgroup_t *hwgroup)
1057 {
1058         u8 stat;
1059         ide_hwif_t *hwif = hwgroup->hwif;
1060
1061         /*
1062          * handle the unexpected interrupt
1063          */
1064         do {
1065                 if (hwif->irq == irq) {
1066                         stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
1067                         if (!OK_STAT(stat, READY_STAT, BAD_STAT)) {
1068                                 /* Try to not flood the console with msgs */
1069                                 static unsigned long last_msgtime, count;
1070                                 ++count;
1071                                 if (time_after(jiffies, last_msgtime + HZ)) {
1072                                         last_msgtime = jiffies;
1073                                         printk(KERN_ERR "%s%s: unexpected interrupt, "
1074                                                 "status=0x%02x, count=%ld\n",
1075                                                 hwif->name,
1076                                                 (hwif->next==hwgroup->hwif) ? "" : "(?)", stat, count);
1077                                 }
1078                         }
1079                 }
1080         } while ((hwif = hwif->next) != hwgroup->hwif);
1081 }
1082
1083 /**
1084  *      ide_intr        -       default IDE interrupt handler
1085  *      @irq: interrupt number
1086  *      @dev_id: hwif group
1087  *      @regs: unused weirdness from the kernel irq layer
1088  *
1089  *      This is the default IRQ handler for the IDE layer. You should
1090  *      not need to override it. If you do be aware it is subtle in
1091  *      places
1092  *
1093  *      hwgroup->hwif is the interface in the group currently performing
1094  *      a command. hwgroup->drive is the drive and hwgroup->handler is
1095  *      the IRQ handler to call. As we issue a command the handlers
1096  *      step through multiple states, reassigning the handler to the
1097  *      next step in the process. Unlike a smart SCSI controller IDE
1098  *      expects the main processor to sequence the various transfer
1099  *      stages. We also manage a poll timer to catch up with most
1100  *      timeout situations. There are still a few where the handlers
1101  *      don't ever decide to give up.
1102  *
1103  *      The handler eventually returns ide_stopped to indicate the
1104  *      request completed. At this point we issue the next request
1105  *      on the hwgroup and the process begins again.
1106  */
1107  
1108 void ide_intr (int irq, void *dev_id, struct pt_regs *regs)
1109 {
1110         unsigned long flags;
1111         ide_hwgroup_t *hwgroup = (ide_hwgroup_t *)dev_id;
1112         ide_hwif_t *hwif;
1113         ide_drive_t *drive;
1114         ide_handler_t *handler;
1115         ide_startstop_t startstop;
1116
1117         spin_lock_irqsave(&io_request_lock, flags);
1118         hwif = hwgroup->hwif;
1119
1120         if (!ide_ack_intr(hwif)) {
1121                 spin_unlock_irqrestore(&io_request_lock, flags);
1122                 return;
1123         }
1124
1125         if ((handler = hwgroup->handler) == NULL ||
1126             hwgroup->poll_timeout != 0) {
1127                 /*
1128                  * Not expecting an interrupt from this drive.
1129                  * That means this could be:
1130                  *      (1) an interrupt from another PCI device
1131                  *      sharing the same PCI INT# as us.
1132                  * or   (2) a drive just entered sleep or standby mode,
1133                  *      and is interrupting to let us know.
1134                  * or   (3) a spurious interrupt of unknown origin.
1135                  *
1136                  * For PCI, we cannot tell the difference,
1137                  * so in that case we just ignore it and hope it goes away.
1138                  */
1139 #ifdef CONFIG_BLK_DEV_IDEPCI
1140                 if (hwif->pci_dev && !hwif->pci_dev->vendor)
1141 #endif  /* CONFIG_BLK_DEV_IDEPCI */
1142                 {
1143                         /*
1144                          * Probably not a shared PCI interrupt,
1145                          * so we can safely try to do something about it:
1146                          */
1147                         unexpected_intr(irq, hwgroup);
1148 #ifdef CONFIG_BLK_DEV_IDEPCI
1149                 } else {
1150                         /*
1151                          * Whack the status register, just in case
1152                          * we have a leftover pending IRQ.
1153                          */
1154                         (void) hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
1155 #endif /* CONFIG_BLK_DEV_IDEPCI */
1156                 }
1157                 spin_unlock_irqrestore(&io_request_lock, flags);
1158                 return;
1159         }
1160         drive = hwgroup->drive;
1161         if (!drive) {
1162                 /*
1163                  * This should NEVER happen, and there isn't much
1164                  * we could do about it here.
1165                  */
1166                 spin_unlock_irqrestore(&io_request_lock, flags);
1167                 return;
1168         }
1169         if (!drive_is_ready(drive)) {
1170                 /*
1171                  * This happens regularly when we share a PCI IRQ with
1172                  * another device.  Unfortunately, it can also happen
1173                  * with some buggy drives that trigger the IRQ before
1174                  * their status register is up to date.  Hopefully we have
1175                  * enough advance overhead that the latter isn't a problem.
1176                  */
1177                 spin_unlock_irqrestore(&io_request_lock, flags);
1178                 return;
1179         }
1180         if (!hwgroup->busy) {
1181                 hwgroup->busy = 1;      /* paranoia */
1182                 printk(KERN_ERR "%s: ide_intr: hwgroup->busy was 0 ??\n", drive->name);
1183         }
1184         hwgroup->handler = NULL;
1185         del_timer(&hwgroup->timer);
1186         spin_unlock(&io_request_lock);
1187
1188         if (drive->unmask)
1189                 local_irq_enable();
1190
1191         /* service this interrupt, may set handler for next interrupt */
1192         startstop = handler(drive);
1193         spin_lock_irq(&io_request_lock);
1194
1195         /*
1196          * Note that handler() may have set things up for another
1197          * interrupt to occur soon, but it cannot happen until
1198          * we exit from this routine, because it will be the
1199          * same irq as is currently being serviced here, and Linux
1200          * won't allow another of the same (on any CPU) until we return.
1201          */
1202         set_recovery_timer(HWIF(drive));
1203         drive->service_time = jiffies - drive->service_start;
1204         if (startstop == ide_stopped) {
1205                 if (hwgroup->handler == NULL) { /* paranoia */
1206                         hwgroup->busy = 0;
1207                         ide_do_request(hwgroup, hwif->irq);
1208                 } else {
1209                         printk(KERN_ERR "%s: ide_intr: huh? expected NULL handler "
1210                                 "on exit\n", drive->name);
1211                 }
1212         }
1213         spin_unlock_irqrestore(&io_request_lock, flags);
1214 }
1215
1216 EXPORT_SYMBOL(ide_intr);
1217
1218 /*
1219  * get_info_ptr() returns the (ide_drive_t *) for a given device number.
1220  * It returns NULL if the given device number does not match any present drives.
1221  */
1222 ide_drive_t *get_info_ptr (kdev_t i_rdev)
1223 {
1224         int             major = MAJOR(i_rdev);
1225         unsigned int    h;
1226
1227         for (h = 0; h < MAX_HWIFS; ++h) {
1228                 ide_hwif_t  *hwif = &ide_hwifs[h];
1229                 if (hwif->present && major == hwif->major) {
1230                         unsigned unit = DEVICE_NR(i_rdev);
1231                         if (unit < MAX_DRIVES) {
1232                                 ide_drive_t *drive = &hwif->drives[unit];
1233                                 if (drive->present)
1234                                         return drive;
1235                         }
1236                         break;
1237                 }
1238         }
1239         return NULL;
1240 }
1241
1242 EXPORT_SYMBOL(get_info_ptr);
1243
1244 /**
1245  *      ide_init_drive_cmd      -       initialize a drive command request
1246  *      @rq: request object
1247  *
1248  *      Initialize a request before we fill it in and send it down to
1249  *      ide_do_drive_cmd. Commands must be set up by this function. Right
1250  *      now it doesn't do a lot, but if that changes abusers will have a
1251  *      nasty suprise.
1252  */
1253
1254 void ide_init_drive_cmd (struct request *rq)
1255 {
1256         memset(rq, 0, sizeof(*rq));
1257         rq->cmd = IDE_DRIVE_CMD;
1258 }
1259
1260 EXPORT_SYMBOL(ide_init_drive_cmd);
1261
1262 /**
1263  *      ide_do_drive_cmd        -       issue IDE special command
1264  *      @drive: device to issue command
1265  *      @rq: request to issue
1266  *      @action: action for processing
1267  *
1268  *      This function issues a special IDE device request
1269  *      onto the request queue.
1270  *
1271  *      If action is ide_wait, then the rq is queued at the end of the
1272  *      request queue, and the function sleeps until it has been processed.
1273  *      This is for use when invoked from an ioctl handler.
1274  *
1275  *      If action is ide_preempt, then the rq is queued at the head of
1276  *      the request queue, displacing the currently-being-processed
1277  *      request and this function returns immediately without waiting
1278  *      for the new rq to be completed.  This is VERY DANGEROUS, and is
1279  *      intended for careful use by the ATAPI tape/cdrom driver code.
1280  *
1281  *      If action is ide_next, then the rq is queued immediately after
1282  *      the currently-being-processed-request (if any), and the function
1283  *      returns without waiting for the new rq to be completed.  As above,
1284  *      This is VERY DANGEROUS, and is intended for careful use by the
1285  *      ATAPI tape/cdrom driver code.
1286  *
1287  *      If action is ide_end, then the rq is queued at the end of the
1288  *      request queue, and the function returns immediately without waiting
1289  *      for the new rq to be completed. This is again intended for careful
1290  *      use by the ATAPI tape/cdrom driver code.
1291  */
1292  
1293 int ide_do_drive_cmd (ide_drive_t *drive, struct request *rq, ide_action_t action)
1294 {
1295         unsigned long flags;
1296         ide_hwgroup_t *hwgroup = HWGROUP(drive);
1297         unsigned int major = HWIF(drive)->major;
1298         request_queue_t *q = &drive->queue;
1299         struct list_head *queue_head = &q->queue_head;
1300         DECLARE_COMPLETION(wait);
1301
1302 #ifdef CONFIG_BLK_DEV_PDC4030
1303         if (HWIF(drive)->chipset == ide_pdc4030 && rq->buffer != NULL)
1304                 return -ENOSYS;  /* special drive cmds not supported */
1305 #endif
1306         rq->errors = 0;
1307         rq->rq_status = RQ_ACTIVE;
1308         rq->rq_dev = MKDEV(major,(drive->select.b.unit)<<PARTN_BITS);
1309         if (action == ide_wait)
1310                 rq->waiting = &wait;
1311         spin_lock_irqsave(&io_request_lock, flags);
1312         if (blk_queue_empty(q) || action == ide_preempt) {
1313                 if (action == ide_preempt)
1314                         hwgroup->rq = NULL;
1315         } else {
1316                 if (action == ide_wait || action == ide_end) {
1317                         queue_head = queue_head->prev;
1318                 } else
1319                         queue_head = queue_head->next;
1320         }
1321         list_add(&rq->queue, queue_head);
1322         ide_do_request(hwgroup, 0);
1323         spin_unlock_irqrestore(&io_request_lock, flags);
1324         if (action == ide_wait) {
1325                 /* wait for it to be serviced */
1326                 wait_for_completion(&wait);
1327                 /* return -EIO if errors */
1328                 return rq->errors ? -EIO : 0;
1329         }
1330         return 0;
1331
1332 }
1333
1334 EXPORT_SYMBOL(ide_do_drive_cmd);