[PATCH] (4/15) big struct block_device * push (first series)
[opensuse:kernel.git] / drivers / ide / ide.c
1 /*
2  *  Copyright (C) 1994-1998  Linus Torvalds & authors (see below)
3  *
4  *  Mostly written by Mark Lord  <mlord@pobox.com>
5  *                and Gadi Oxman <gadio@netvision.net.il>
6  *                and Andre Hedrick <andre@linux-ide.org>
7  *
8  *  See linux/MAINTAINERS for address of current maintainer.
9  *
10  * This is the multiple IDE interface driver, as evolved from hd.c.
11  * It supports up to MAX_HWIFS IDE interfaces, on one or more IRQs (usually 14 & 15).
12  * There can be up to two drives per interface, as per the ATA-2 spec.
13  *
14  * Primary:    ide0, port 0x1f0; major=3;  hda is minor=0; hdb is minor=64
15  * Secondary:  ide1, port 0x170; major=22; hdc is minor=0; hdd is minor=64
16  * Tertiary:   ide2, port 0x???; major=33; hde is minor=0; hdf is minor=64
17  * Quaternary: ide3, port 0x???; major=34; hdg is minor=0; hdh is minor=64
18  * ...
19  *
20  *  From hd.c:
21  *  |
22  *  | It traverses the request-list, using interrupts to jump between functions.
23  *  | As nearly all functions can be called within interrupts, we may not sleep.
24  *  | Special care is recommended.  Have Fun!
25  *  |
26  *  | modified by Drew Eckhardt to check nr of hd's from the CMOS.
27  *  |
28  *  | Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
29  *  | in the early extended-partition checks and added DM partitions.
30  *  |
31  *  | Early work on error handling by Mika Liljeberg (liljeber@cs.Helsinki.FI).
32  *  |
33  *  | IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
34  *  | and general streamlining by Mark Lord (mlord@pobox.com).
35  *
36  *  October, 1994 -- Complete line-by-line overhaul for linux 1.1.x, by:
37  *
38  *      Mark Lord       (mlord@pobox.com)               (IDE Perf.Pkg)
39  *      Delman Lee      (delman@ieee.org)               ("Mr. atdisk2")
40  *      Scott Snyder    (snyder@fnald0.fnal.gov)        (ATAPI IDE cd-rom)
41  *
42  *  This was a rewrite of just about everything from hd.c, though some original
43  *  code is still sprinkled about.  Think of it as a major evolution, with
44  *  inspiration from lots of linux users, esp.  hamish@zot.apana.org.au
45  *
46  *  Version 1.0 ALPHA   initial code, primary i/f working okay
47  *  Version 1.3 BETA    dual i/f on shared irq tested & working!
48  *  Version 1.4 BETA    added auto probing for irq(s)
49  *  Version 1.5 BETA    added ALPHA (untested) support for IDE cd-roms,
50  *  ...
51  * Version 5.50         allow values as small as 20 for idebus=
52  * Version 5.51         force non io_32bit in drive_cmd_intr()
53  *                      change delay_10ms() to delay_50ms() to fix problems
54  * Version 5.52         fix incorrect invalidation of removable devices
55  *                      add "hdx=slow" command line option
56  * Version 5.60         start to modularize the driver; the disk and ATAPI
57  *                       drivers can be compiled as loadable modules.
58  *                      move IDE probe code to ide-probe.c
59  *                      move IDE disk code to ide-disk.c
60  *                      add support for generic IDE device subdrivers
61  *                      add m68k code from Geert Uytterhoeven
62  *                      probe all interfaces by default
63  *                      add ioctl to (re)probe an interface
64  * Version 6.00         use per device request queues
65  *                      attempt to optimize shared hwgroup performance
66  *                      add ioctl to manually adjust bandwidth algorithms
67  *                      add kerneld support for the probe module
68  *                      fix bug in ide_error()
69  *                      fix bug in the first ide_get_lock() call for Atari
70  *                      don't flush leftover data for ATAPI devices
71  * Version 6.01         clear hwgroup->active while the hwgroup sleeps
72  *                      support HDIO_GETGEO for floppies
73  * Version 6.02         fix ide_ack_intr() call
74  *                      check partition table on floppies
75  * Version 6.03         handle bad status bit sequencing in ide_wait_stat()
76  * Version 6.10         deleted old entries from this list of updates
77  *                      replaced triton.c with ide-dma.c generic PCI DMA
78  *                      added support for BIOS-enabled UltraDMA
79  *                      rename all "promise" things to "pdc4030"
80  *                      fix EZ-DRIVE handling on small disks
81  * Version 6.11         fix probe error in ide_scan_devices()
82  *                      fix ancient "jiffies" polling bugs
83  *                      mask all hwgroup interrupts on each irq entry
84  * Version 6.12         integrate ioctl and proc interfaces
85  *                      fix parsing of "idex=" command line parameter
86  * Version 6.13         add support for ide4/ide5 courtesy rjones@orchestream.com
87  * Version 6.14         fixed IRQ sharing among PCI devices
88  * Version 6.15         added SMP awareness to IDE drivers
89  * Version 6.16         fixed various bugs; even more SMP friendly
90  * Version 6.17         fix for newest EZ-Drive problem
91  * Version 6.18         default unpartitioned-disk translation now "BIOS LBA"
92  * Version 6.19         Re-design for a UNIFORM driver for all platforms,
93  *                        model based on suggestions from Russell King and
94  *                        Geert Uytterhoeven
95  *                      Promise DC4030VL now supported.
96  *                      add support for ide6/ide7
97  *                      delay_50ms() changed to ide_delay_50ms() and exported.
98  * Version 6.20         Added/Fixed Generic ATA-66 support and hwif detection.
99  *                      Added hdx=flash to allow for second flash disk
100  *                        detection w/o the hang loop.
101  *                      Added support for ide8/ide9
102  *                      Added idex=ata66 for the quirky chipsets that are
103  *                        ATA-66 compliant, but have yet to determine a method
104  *                        of verification of the 80c cable presence.
105  *                        Specifically Promise's PDC20262 chipset.
106  * Version 6.21         Fixing/Fixed SMP spinlock issue with insight from an old
107  *                        hat that clarified original low level driver design.
108  * Version 6.30         Added SMP support; fixed multmode issues.  -ml
109  * Version 6.31         Debug Share INTR's and request queue streaming
110  *                      Native ATA-100 support
111  *                      Prep for Cascades Project
112  * Version 6.32         4GB highmem support for DMA, and mapping of those for
113  *                      PIO transfer (Jens Axboe)
114  *
115  *  Some additional driver compile-time options are in ./include/linux/ide.h
116  */
117
118 #define VERSION "7.0.0"
119
120 #include <linux/config.h>
121 #include <linux/module.h>
122 #include <linux/types.h>
123 #include <linux/string.h>
124 #include <linux/kernel.h>
125 #include <linux/timer.h>
126 #include <linux/mm.h>
127 #include <linux/interrupt.h>
128 #include <linux/major.h>
129 #include <linux/errno.h>
130 #include <linux/genhd.h>
131 #include <linux/blkpg.h>
132 #include <linux/slab.h>
133 #ifndef MODULE
134 # include <linux/init.h>
135 #endif
136 #include <linux/pci.h>
137 #include <linux/delay.h>
138 #include <linux/ide.h>
139 #include <linux/devfs_fs_kernel.h>
140 #include <linux/completion.h>
141 #include <linux/reboot.h>
142 #include <linux/cdrom.h>
143 #include <linux/device.h>
144 #include <linux/kmod.h>
145
146 #include <asm/byteorder.h>
147 #include <asm/irq.h>
148 #include <asm/uaccess.h>
149 #include <asm/io.h>
150 #include <asm/bitops.h>
151
152 #include "ata-timing.h"
153
154 /*
155  * Those will be moved into separate header files eventually.
156  */
157 #ifdef CONFIG_BLK_DEV_RZ1000
158 extern void ide_probe_for_rz100x(void);
159 #endif
160 #ifdef CONFIG_ETRAX_IDE
161 extern void init_e100_ide(void);
162 #endif
163 #ifdef CONFIG_BLK_DEV_CMD640
164 extern void ide_probe_for_cmd640x(void);
165 #endif
166 #ifdef CONFIG_BLK_DEV_PDC4030
167 extern int ide_probe_for_pdc4030(void);
168 #endif
169 #ifdef CONFIG_BLK_DEV_IDE_PMAC
170 extern void pmac_ide_probe(void);
171 #endif
172 #ifdef CONFIG_BLK_DEV_IDE_ICSIDE
173 extern void icside_init(void);
174 #endif
175 #ifdef CONFIG_BLK_DEV_IDE_RAPIDE
176 extern void rapide_init(void);
177 #endif
178 #ifdef CONFIG_BLK_DEV_GAYLE
179 extern void gayle_init(void);
180 #endif
181 #ifdef CONFIG_BLK_DEV_FALCON_IDE
182 extern void falconide_init(void);
183 #endif
184 #ifdef CONFIG_BLK_DEV_MAC_IDE
185 extern void macide_init(void);
186 #endif
187 #ifdef CONFIG_BLK_DEV_Q40IDE
188 extern void q40ide_init(void);
189 #endif
190 #ifdef CONFIG_BLK_DEV_BUDDHA
191 extern void buddha_init(void);
192 #endif
193 #if defined(CONFIG_BLK_DEV_ISAPNP) && defined(CONFIG_ISAPNP)
194 extern void pnpide_init(int);
195 #endif
196
197 /* default maximum number of failures */
198 #define IDE_DEFAULT_MAX_FAILURES        1
199
200 static int idebus_parameter;    /* holds the "idebus=" parameter */
201 int system_bus_speed;           /* holds what we think is VESA/PCI bus speed */
202 static int initializing;        /* set while initializing built-in drivers */
203
204 /*
205  * Protects access to global structures etc.
206  */
207 spinlock_t ide_lock __cacheline_aligned = SPIN_LOCK_UNLOCKED;
208
209 #ifdef CONFIG_BLK_DEV_IDEPCI
210 static int ide_scan_direction;  /* THIS was formerly 2.2.x pci=reverse */
211 #endif
212
213 #if defined(__mc68000__) || defined(CONFIG_APUS)
214 /*
215  * This is used by the Atari code to obtain access to the IDE interrupt,
216  * which is shared between several drivers.
217  */
218 static int      ide_intr_lock;
219 #endif
220
221 int noautodma = 0;
222
223 /*
224  * This is declared extern in ide.h, for access by other IDE modules:
225  */
226 struct ata_channel ide_hwifs[MAX_HWIFS];        /* master data repository */
227
228 #if (DISK_RECOVERY_TIME > 0)
229 /*
230  * For really screwed hardware (hey, at least it *can* be used with Linux)
231  * we can enforce a minimum delay time between successive operations.
232  */
233 static unsigned long read_timer (void)
234 {
235         unsigned long t, flags;
236         int i;
237
238         __save_flags(flags);    /* local CPU only */
239         __cli();                /* local CPU only */
240         t = jiffies * 11932;
241         outb_p(0, 0x43);
242         i = inb_p(0x40);
243         i |= inb(0x40) << 8;
244         __restore_flags(flags); /* local CPU only */
245         return (t - i);
246 }
247 #endif
248
249 static inline void set_recovery_timer(struct ata_channel *channel)
250 {
251 #if (DISK_RECOVERY_TIME > 0)
252         channel->last_time = read_timer();
253 #endif
254 }
255
256 /*
257  * Do not even *think* about calling this!
258  */
259 static void init_hwif_data(struct ata_channel *hwif, unsigned int index)
260 {
261         static const byte ide_major[] = {
262                 IDE0_MAJOR, IDE1_MAJOR, IDE2_MAJOR, IDE3_MAJOR, IDE4_MAJOR,
263                 IDE5_MAJOR, IDE6_MAJOR, IDE7_MAJOR, IDE8_MAJOR, IDE9_MAJOR
264         };
265
266         unsigned int unit;
267         hw_regs_t hw;
268
269         /* bulk initialize hwif & drive info with zeros */
270         memset(hwif, 0, sizeof(struct ata_channel));
271         memset(&hw, 0, sizeof(hw_regs_t));
272
273         /* fill in any non-zero initial values */
274         hwif->index     = index;
275         ide_init_hwif_ports(&hw, ide_default_io_base(index), 0, &hwif->irq);
276         memcpy(&hwif->hw, &hw, sizeof(hw));
277         memcpy(hwif->io_ports, hw.io_ports, sizeof(hw.io_ports));
278         hwif->noprobe   = !hwif->io_ports[IDE_DATA_OFFSET];
279 #ifdef CONFIG_BLK_DEV_HD
280         if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA)
281                 hwif->noprobe = 1; /* may be overridden by ide_setup() */
282 #endif /* CONFIG_BLK_DEV_HD */
283         hwif->major = ide_major[index];
284         sprintf(hwif->name, "ide%d", index);
285         hwif->bus_state = BUSSTATE_ON;
286         for (unit = 0; unit < MAX_DRIVES; ++unit) {
287                 ide_drive_t *drive = &hwif->drives[unit];
288
289                 drive->type                     = ATA_DISK;
290                 drive->select.all               = (unit<<4)|0xa0;
291                 drive->channel                  = hwif;
292                 drive->ctl                      = 0x08;
293                 drive->ready_stat               = READY_STAT;
294                 drive->bad_wstat                = BAD_W_STAT;
295                 drive->special.b.recalibrate    = 1;
296                 drive->special.b.set_geometry   = 1;
297                 sprintf(drive->name, "hd%c", 'a' + (index * MAX_DRIVES) + unit);
298                 drive->max_failures             = IDE_DEFAULT_MAX_FAILURES;
299                 init_waitqueue_head(&drive->wqueue);
300         }
301 }
302
303 /*
304  * init_ide_data() sets reasonable default values into all fields
305  * of all instances of the hwifs and drives, but only on the first call.
306  * Subsequent calls have no effect (they don't wipe out anything).
307  *
308  * This routine is normally called at driver initialization time,
309  * but may also be called MUCH earlier during kernel "command-line"
310  * parameter processing.  As such, we cannot depend on any other parts
311  * of the kernel (such as memory allocation) to be functioning yet.
312  *
313  * This is too bad, as otherwise we could dynamically allocate the
314  * ide_drive_t structs as needed, rather than always consuming memory
315  * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
316  */
317 #define MAGIC_COOKIE 0x12345678
318 static void __init init_ide_data (void)
319 {
320         unsigned int h;
321         static unsigned long magic_cookie = MAGIC_COOKIE;
322
323         if (magic_cookie != MAGIC_COOKIE)
324                 return;         /* already initialized */
325         magic_cookie = 0;
326
327         /* Initialize all interface structures */
328         for (h = 0; h < MAX_HWIFS; ++h)
329                 init_hwif_data(&ide_hwifs[h], h);
330
331         /* Add default hw interfaces */
332         ide_init_default_hwifs();
333
334         idebus_parameter = 0;
335 }
336
337 /*
338  * CompactFlash cards and their relatives pretend to be removable hard disks, except:
339  *      (1) they never have a slave unit, and
340  *      (2) they don't have a door lock mechanisms.
341  * This test catches them, and is invoked elsewhere when setting appropriate config bits.
342  *
343  * FIXME FIXME: Yes this is for certain applicable for all of them as time has shown.
344  *
345  * FIXME: This treatment is probably applicable for *all* PCMCIA (PC CARD) devices,
346  * so in linux 2.3.x we should change this to just treat all PCMCIA drives this way,
347  * and get rid of the model-name tests below (too big of an interface change for 2.2.x).
348  * At that time, we might also consider parameterizing the timeouts and retries,
349  * since these are MUCH faster than mechanical drives.  -M.Lord
350  */
351 int drive_is_flashcard (ide_drive_t *drive)
352 {
353         struct hd_driveid *id = drive->id;
354
355         if (drive->removable && id != NULL) {
356                 if (id->config == 0x848a)
357                         return 1;       /* CompactFlash */
358                 if (!strncmp(id->model, "KODAK ATA_FLASH", 15)  /* Kodak */
359                  || !strncmp(id->model, "Hitachi CV", 10)       /* Hitachi */
360                  || !strncmp(id->model, "SunDisk SDCFB", 13)    /* SunDisk */
361                  || !strncmp(id->model, "HAGIWARA HPC", 12)     /* Hagiwara */
362                  || !strncmp(id->model, "LEXAR ATA_FLASH", 15)  /* Lexar */
363                  || !strncmp(id->model, "ATA_FLASH", 9))        /* Simple Tech */
364                 {
365                         return 1;       /* yes, it is a flash memory card */
366                 }
367         }
368         return 0;       /* no, it is not a flash memory card */
369 }
370
371 int __ide_end_request(ide_drive_t *drive, int uptodate, int nr_secs)
372 {
373         struct request *rq;
374         unsigned long flags;
375         int ret = 1;
376
377         spin_lock_irqsave(&ide_lock, flags);
378         rq = HWGROUP(drive)->rq;
379
380         BUG_ON(!(rq->flags & REQ_STARTED));
381
382         /*
383          * small hack to eliminate locking from ide_end_request to grab
384          * the first segment number of sectors
385          */
386         if (!nr_secs)
387                 nr_secs = rq->hard_cur_sectors;
388
389         /*
390          * decide whether to reenable DMA -- 3 is a random magic for now,
391          * if we DMA timeout more than 3 times, just stay in PIO
392          */
393         if (drive->state == DMA_PIO_RETRY && drive->retry_pio <= 3) {
394                 drive->state = 0;
395                 HWGROUP(drive)->hwif->dmaproc(ide_dma_on, drive);
396         }
397
398         if (!end_that_request_first(rq, uptodate, nr_secs)) {
399                 add_blkdev_randomness(major(rq->rq_dev));
400                 blkdev_dequeue_request(rq);
401                 HWGROUP(drive)->rq = NULL;
402                 end_that_request_last(rq);
403                 ret = 0;
404         }
405
406         spin_unlock_irqrestore(&ide_lock, flags);
407         return ret;
408 }
409
410 /*
411  * This should get invoked any time we exit the driver to
412  * wait for an interrupt response from a drive.  handler() points
413  * at the appropriate code to handle the next interrupt, and a
414  * timer is started to prevent us from waiting forever in case
415  * something goes wrong (see the ide_timer_expiry() handler later on).
416  */
417 void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
418                       unsigned int timeout, ide_expiry_t *expiry)
419 {
420         unsigned long flags;
421         ide_hwgroup_t *hwgroup = HWGROUP(drive);
422
423         spin_lock_irqsave(&ide_lock, flags);
424         if (hwgroup->handler != NULL) {
425                 printk("%s: ide_set_handler: handler not null; old=%p, new=%p, from %p\n",
426                         drive->name, hwgroup->handler, handler, __builtin_return_address(0));
427         }
428         hwgroup->handler        = handler;
429         hwgroup->expiry         = expiry;
430         hwgroup->timer.expires  = jiffies + timeout;
431         add_timer(&hwgroup->timer);
432         spin_unlock_irqrestore(&ide_lock, flags);
433 }
434
435 static void ata_pre_reset(ide_drive_t *drive)
436 {
437         if (ata_ops(drive) && ata_ops(drive)->pre_reset)
438                 ata_ops(drive)->pre_reset(drive);
439
440         if (!drive->using_dma)
441             return;
442
443         /* check the DMA crc count */
444         if (drive->crc_count) {
445                 drive->channel->dmaproc(ide_dma_off_quietly, drive);
446                 if ((drive->channel->speedproc) != NULL)
447                         drive->channel->speedproc(drive, ide_auto_reduce_xfer(drive));
448                 if (drive->current_speed >= XFER_SW_DMA_0)
449                         drive->channel->dmaproc(ide_dma_on, drive);
450         } else
451                 drive->channel->dmaproc(ide_dma_off, drive);
452 }
453
454 /*
455  * The capacity of a drive according to its current geometry/LBA settings in
456  * sectors.
457  */
458 unsigned long ata_capacity(ide_drive_t *drive)
459 {
460         if (!drive->present || !drive->driver)
461                 return 0;
462
463         if (ata_ops(drive) && ata_ops(drive)->capacity)
464                 return ata_ops(drive)->capacity(drive);
465
466         /* This used to be 0x7fffffff, but since now we use the maximal drive
467          * capacity value used by other kernel subsystems as well.
468          */
469
470         return ~0UL;
471 }
472
473 /*
474  * This is used to issue WIN_SPECIFY, WIN_RESTORE, and WIN_SETMULT commands to
475  * a drive.  It used to do much more, but has been scaled back.
476  */
477 static ide_startstop_t ata_special (ide_drive_t *drive)
478 {
479         special_t *s = &drive->special;
480
481 #ifdef DEBUG
482         printk("%s: ata_special: 0x%02x\n", drive->name, s->all);
483 #endif
484         if (s->b.set_tune) {
485                 s->b.set_tune = 0;
486                 if (drive->channel->tuneproc != NULL)
487                         drive->channel->tuneproc(drive, drive->tune_req);
488         } else if (drive->driver != NULL) {
489                 if (ata_ops(drive)->special)
490                         return ata_ops(drive)->special(drive);
491                 else {
492                         drive->special.all = 0;
493                         drive->mult_req = 0;
494
495                         return ide_stopped;
496                 }
497         } else if (s->all) {
498                 printk("%s: bad special flag: 0x%02x\n", drive->name, s->all);
499                 s->all = 0;
500         }
501
502         return ide_stopped;
503 }
504
505 extern struct block_device_operations ide_fops[];
506
507 /*
508  * This is called exactly *once* for each channel.
509  */
510 void ide_geninit(struct ata_channel *hwif)
511 {
512         unsigned int unit;
513         struct gendisk *gd = hwif->gd;
514
515         for (unit = 0; unit < MAX_DRIVES; ++unit) {
516                 ide_drive_t *drive = &hwif->drives[unit];
517
518                 if (!drive->present)
519                         continue;
520                 if (drive->type != ATA_DISK && drive->type != ATA_FLOPPY)
521                         continue;
522                 register_disk(gd,mk_kdev(hwif->major,unit<<PARTN_BITS),
523 #ifdef CONFIG_BLK_DEV_ISAPNP
524                         (drive->forced_geom && drive->noprobe) ? 1 :
525 #endif
526                         1 << PARTN_BITS, ide_fops, ata_capacity(drive));
527         }
528 }
529
530 static ide_startstop_t do_reset1(ide_drive_t *, int);           /* needed below */
531
532 /*
533  * Poll the interface for completion every 50ms during an ATAPI drive reset
534  * operation. If the drive has not yet responded, and we have not yet hit our
535  * maximum waiting time, then the timer is restarted for another 50ms.
536  */
537 static ide_startstop_t atapi_reset_pollfunc (ide_drive_t *drive)
538 {
539         ide_hwgroup_t *hwgroup = HWGROUP(drive);
540         byte stat;
541
542         SELECT_DRIVE(drive->channel,drive);
543         udelay (10);
544
545         if (OK_STAT(stat=GET_STAT(), 0, BUSY_STAT)) {
546                 printk("%s: ATAPI reset complete\n", drive->name);
547         } else {
548                 if (time_before(jiffies, hwgroup->poll_timeout)) {
549                         BUG_ON(HWGROUP(drive)->handler);
550                         ide_set_handler (drive, &atapi_reset_pollfunc, HZ/20, NULL);
551                         return ide_started;     /* continue polling */
552                 }
553                 hwgroup->poll_timeout = 0;      /* end of polling */
554                 printk("%s: ATAPI reset timed-out, status=0x%02x\n", drive->name, stat);
555                 return do_reset1 (drive, 1);    /* do it the old fashioned way */
556         }
557         hwgroup->poll_timeout = 0;      /* done polling */
558         return ide_stopped;
559 }
560
561 /*
562  * Poll the interface for completion every 50ms during an ata reset operation.
563  * If the drives have not yet responded, and we have not yet hit our maximum
564  * waiting time, then the timer is restarted for another 50ms.
565  */
566 static ide_startstop_t reset_pollfunc (ide_drive_t *drive)
567 {
568         ide_hwgroup_t *hwgroup = HWGROUP(drive);
569         struct ata_channel *hwif = drive->channel;
570         byte tmp;
571
572         if (!OK_STAT(tmp=GET_STAT(), 0, BUSY_STAT)) {
573                 if (time_before(jiffies, hwgroup->poll_timeout)) {
574                         BUG_ON(HWGROUP(drive)->handler);
575                         ide_set_handler (drive, &reset_pollfunc, HZ/20, NULL);
576                         return ide_started;     /* continue polling */
577                 }
578                 printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
579                 drive->failures++;
580         } else  {
581                 printk("%s: reset: ", hwif->name);
582                 if ((tmp = GET_ERR()) == 1) {
583                         printk("success\n");
584                         drive->failures = 0;
585                 } else {
586                         drive->failures++;
587 #if FANCY_STATUS_DUMPS
588                         printk("master: ");
589                         switch (tmp & 0x7f) {
590                                 case 1: printk("passed");
591                                         break;
592                                 case 2: printk("formatter device error");
593                                         break;
594                                 case 3: printk("sector buffer error");
595                                         break;
596                                 case 4: printk("ECC circuitry error");
597                                         break;
598                                 case 5: printk("controlling MPU error");
599                                         break;
600                                 default:printk("error (0x%02x?)", tmp);
601                         }
602                         if (tmp & 0x80)
603                                 printk("; slave: failed");
604                         printk("\n");
605 #else
606                         printk("failed\n");
607 #endif
608                 }
609         }
610         hwgroup->poll_timeout = 0;      /* done polling */
611         return ide_stopped;
612 }
613
614 /*
615  * do_reset1() attempts to recover a confused drive by resetting it.
616  * Unfortunately, resetting a disk drive actually resets all devices on
617  * the same interface, so it can really be thought of as resetting the
618  * interface rather than resetting the drive.
619  *
620  * ATAPI devices have their own reset mechanism which allows them to be
621  * individually reset without clobbering other devices on the same interface.
622  *
623  * Unfortunately, the IDE interface does not generate an interrupt to let
624  * us know when the reset operation has finished, so we must poll for this.
625  * Equally poor, though, is the fact that this may a very long time to complete,
626  * (up to 30 seconds worst case).  So, instead of busy-waiting here for it,
627  * we set a timer to poll at 50ms intervals.
628  */
629 static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
630 {
631         unsigned int unit;
632         unsigned long flags;
633         struct ata_channel *hwif = drive->channel;
634         ide_hwgroup_t *hwgroup = HWGROUP(drive);
635
636         __save_flags(flags);    /* local CPU only */
637         __cli();                /* local CPU only */
638
639         /* For an ATAPI device, first try an ATAPI SRST. */
640         if (drive->type != ATA_DISK && !do_not_try_atapi) {
641                 ata_pre_reset(drive);
642                 SELECT_DRIVE(hwif,drive);
643                 udelay (20);
644                 OUT_BYTE (WIN_SRST, IDE_COMMAND_REG);
645                 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
646                 BUG_ON(HWGROUP(drive)->handler);
647                 ide_set_handler (drive, &atapi_reset_pollfunc, HZ/20, NULL);
648                 __restore_flags (flags);        /* local CPU only */
649                 return ide_started;
650         }
651
652         /*
653          * First, reset any device state data we were maintaining
654          * for any of the drives on this interface.
655          */
656         for (unit = 0; unit < MAX_DRIVES; ++unit)
657                 ata_pre_reset(&hwif->drives[unit]);
658
659 #if OK_TO_RESET_CONTROLLER
660         if (!IDE_CONTROL_REG) {
661                 __restore_flags(flags);
662                 return ide_stopped;
663         }
664         /*
665          * Note that we also set nIEN while resetting the device,
666          * to mask unwanted interrupts from the interface during the reset.
667          * However, due to the design of PC hardware, this will cause an
668          * immediate interrupt due to the edge transition it produces.
669          * This single interrupt gives us a "fast poll" for drives that
670          * recover from reset very quickly, saving us the first 50ms wait time.
671          */
672         OUT_BYTE(drive->ctl|6,IDE_CONTROL_REG); /* set SRST and nIEN */
673         udelay(10);                     /* more than enough time */
674         if (drive->quirk_list == 2) {
675                 OUT_BYTE(drive->ctl,IDE_CONTROL_REG);   /* clear SRST and nIEN */
676         } else {
677                 OUT_BYTE(drive->ctl|2,IDE_CONTROL_REG); /* clear SRST, leave nIEN */
678         }
679         udelay(10);                     /* more than enough time */
680         hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
681         BUG_ON(HWGROUP(drive)->handler);
682         ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
683
684         /*
685          * Some weird controller like resetting themselves to a strange
686          * state when the disks are reset this way. At least, the Winbond
687          * 553 documentation says that
688          */
689         if (hwif->resetproc != NULL)
690                 hwif->resetproc(drive);
691
692 #endif
693
694         __restore_flags (flags);        /* local CPU only */
695         return ide_started;
696 }
697
698 static inline u32 read_24 (ide_drive_t *drive)
699 {
700         return  (IN_BYTE(IDE_HCYL_REG)<<16) |
701                 (IN_BYTE(IDE_LCYL_REG)<<8) |
702                  IN_BYTE(IDE_SECTOR_REG);
703 }
704
705 /*
706  * Clean up after success/failure of an explicit drive cmd
707  */
708 void ide_end_drive_cmd(ide_drive_t *drive, byte stat, byte err)
709 {
710         unsigned long flags;
711         struct request *rq;
712
713         spin_lock_irqsave(&ide_lock, flags);
714         rq = HWGROUP(drive)->rq;
715
716         if (rq->flags & REQ_DRIVE_CMD) {
717                 byte *args = (byte *) rq->buffer;
718                 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
719                 if (args) {
720                         args[0] = stat;
721                         args[1] = err;
722                         args[2] = IN_BYTE(IDE_NSECTOR_REG);
723                 }
724         } else if (rq->flags & REQ_DRIVE_TASK) {
725                 byte *args = (byte *) rq->buffer;
726                 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
727                 if (args) {
728                         args[0] = stat;
729                         args[1] = err;
730                         args[2] = IN_BYTE(IDE_NSECTOR_REG);
731                         args[3] = IN_BYTE(IDE_SECTOR_REG);
732                         args[4] = IN_BYTE(IDE_LCYL_REG);
733                         args[5] = IN_BYTE(IDE_HCYL_REG);
734                         args[6] = IN_BYTE(IDE_SELECT_REG);
735                 }
736         } else if (rq->flags & REQ_DRIVE_TASKFILE) {
737                 struct ata_taskfile *args = rq->special;
738                 rq->errors = !OK_STAT(stat, READY_STAT, BAD_STAT);
739                 if (args) {
740                         args->taskfile.feature = err;
741                         args->taskfile.sector_count = IN_BYTE(IDE_NSECTOR_REG);
742                         args->taskfile.sector_number = IN_BYTE(IDE_SECTOR_REG);
743                         args->taskfile.low_cylinder = IN_BYTE(IDE_LCYL_REG);
744                         args->taskfile.high_cylinder = IN_BYTE(IDE_HCYL_REG);
745                         args->taskfile.device_head = IN_BYTE(IDE_SELECT_REG);
746                         args->taskfile.command = stat;
747                         if ((drive->id->command_set_2 & 0x0400) &&
748                             (drive->id->cfs_enable_2 & 0x0400) &&
749                             (drive->addressing == 1)) {
750                                 /* The following command goes to the hob file! */
751
752                                 OUT_BYTE(drive->ctl|0x80, IDE_CONTROL_REG);
753                                 args->hobfile.feature = IN_BYTE(IDE_FEATURE_REG);
754                                 args->hobfile.sector_count = IN_BYTE(IDE_NSECTOR_REG);
755
756                                 args->hobfile.sector_number = IN_BYTE(IDE_SECTOR_REG);
757                                 args->hobfile.low_cylinder = IN_BYTE(IDE_LCYL_REG);
758                                 args->hobfile.high_cylinder = IN_BYTE(IDE_HCYL_REG);
759                         }
760                 }
761         }
762
763         blkdev_dequeue_request(rq);
764         HWGROUP(drive)->rq = NULL;
765         end_that_request_last(rq);
766
767         spin_unlock_irqrestore(&ide_lock, flags);
768 }
769
770 /*
771  * Error reporting, in human readable form (luxurious, but a memory hog).
772  */
773 byte ide_dump_status (ide_drive_t *drive, const char *msg, byte stat)
774 {
775         unsigned long flags;
776         byte err = 0;
777
778         __save_flags (flags);   /* local CPU only */
779         ide__sti();             /* local CPU only */
780         printk("%s: %s: status=0x%02x", drive->name, msg, stat);
781 #if FANCY_STATUS_DUMPS
782         printk(" { ");
783         if (stat & BUSY_STAT)
784                 printk("Busy ");
785         else {
786                 if (stat & READY_STAT)  printk("DriveReady ");
787                 if (stat & WRERR_STAT)  printk("DeviceFault ");
788                 if (stat & SEEK_STAT)   printk("SeekComplete ");
789                 if (stat & DRQ_STAT)    printk("DataRequest ");
790                 if (stat & ECC_STAT)    printk("CorrectedError ");
791                 if (stat & INDEX_STAT)  printk("Index ");
792                 if (stat & ERR_STAT)    printk("Error ");
793         }
794         printk("}");
795 #endif  /* FANCY_STATUS_DUMPS */
796         printk("\n");
797         if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
798                 err = GET_ERR();
799                 printk("%s: %s: error=0x%02x", drive->name, msg, err);
800 #if FANCY_STATUS_DUMPS
801                 if (drive->type == ATA_DISK) {
802                         printk(" { ");
803                         if (err & ABRT_ERR)     printk("DriveStatusError ");
804                         if (err & ICRC_ERR)     printk("%s", (err & ABRT_ERR) ? "BadCRC " : "BadSector ");
805                         if (err & ECC_ERR)      printk("UncorrectableError ");
806                         if (err & ID_ERR)       printk("SectorIdNotFound ");
807                         if (err & TRK0_ERR)     printk("TrackZeroNotFound ");
808                         if (err & MARK_ERR)     printk("AddrMarkNotFound ");
809                         printk("}");
810                         if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR || (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
811                                 if ((drive->id->command_set_2 & 0x0400) &&
812                                     (drive->id->cfs_enable_2 & 0x0400) &&
813                                     (drive->addressing == 1)) {
814                                         __u64 sectors = 0;
815                                         u32 low = 0, high = 0;
816                                         low = read_24(drive);
817                                         OUT_BYTE(drive->ctl|0x80, IDE_CONTROL_REG);
818                                         high = read_24(drive);
819
820                                         sectors = ((__u64)high << 24) | low;
821                                         printk(", LBAsect=%lld, high=%d, low=%d", (long long) sectors, high, low);
822                                 } else {
823                                         byte cur = IN_BYTE(IDE_SELECT_REG);
824                                         if (cur & 0x40) {       /* using LBA? */
825                                                 printk(", LBAsect=%ld", (unsigned long)
826                                                  ((cur&0xf)<<24)
827                                                  |(IN_BYTE(IDE_HCYL_REG)<<16)
828                                                  |(IN_BYTE(IDE_LCYL_REG)<<8)
829                                                  | IN_BYTE(IDE_SECTOR_REG));
830                                         } else {
831                                                 printk(", CHS=%d/%d/%d",
832                                                  (IN_BYTE(IDE_HCYL_REG)<<8) +
833                                                   IN_BYTE(IDE_LCYL_REG),
834                                                   cur & 0xf,
835                                                   IN_BYTE(IDE_SECTOR_REG));
836                                         }
837                                 }
838                                 if (HWGROUP(drive) && HWGROUP(drive)->rq)
839                                         printk(", sector=%ld", HWGROUP(drive)->rq->sector);
840                         }
841                 }
842 #endif  /* FANCY_STATUS_DUMPS */
843                 printk("\n");
844         }
845         __restore_flags (flags);        /* local CPU only */
846         return err;
847 }
848
849 /*
850  * This gets invoked in response to a drive unexpectedly having its DRQ_STAT
851  * bit set.  As an alternative to resetting the drive, it tries to clear the
852  * condition by reading a sector's worth of data from the drive.  Of course,
853  * this may not help if the drive is *waiting* for data from *us*.
854  */
855 static void try_to_flush_leftover_data (ide_drive_t *drive)
856 {
857         int i;
858
859         if (drive->type != ATA_DISK)
860                 return;
861
862         for (i = (drive->mult_count ? drive->mult_count : 1); i > 0; --i) {
863                 u32 buffer[SECTOR_WORDS];
864
865                 ata_read(drive, buffer, SECTOR_WORDS);
866         }
867 }
868
869 /*
870  * Take action based on the error returned by the drive.
871  */
872 ide_startstop_t ide_error(ide_drive_t *drive, const char *msg, byte stat)
873 {
874         struct request *rq;
875         byte err;
876
877         err = ide_dump_status(drive, msg, stat);
878         if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
879                 return ide_stopped;
880         /* retry only "normal" I/O: */
881         if (!(rq->flags & REQ_CMD)) {
882                 rq->errors = 1;
883                 ide_end_drive_cmd(drive, stat, err);
884                 return ide_stopped;
885         }
886
887         if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) { /* other bits are useless when BUSY */
888                 rq->errors |= ERROR_RESET;
889         } else {
890                 if (drive->type == ATA_DISK && (stat & ERR_STAT)) {
891                         /* err has different meaning on cdrom and tape */
892                         if (err == ABRT_ERR) {
893                                 if (drive->select.b.lba && IN_BYTE(IDE_COMMAND_REG) == WIN_SPECIFY)
894                                         return ide_stopped; /* some newer drives don't support WIN_SPECIFY */
895                         } else if ((err & (ABRT_ERR | ICRC_ERR)) == (ABRT_ERR | ICRC_ERR)) {
896                                 drive->crc_count++; /* UDMA crc error -- just retry the operation */
897                         } else if (err & (BBD_ERR | ECC_ERR))   /* retries won't help these */
898                                 rq->errors = ERROR_MAX;
899                         else if (err & TRK0_ERR)        /* help it find track zero */
900                                 rq->errors |= ERROR_RECAL;
901                 }
902                 /* pre bio (rq->cmd != WRITE) */
903                 if ((stat & DRQ_STAT) && rq_data_dir(rq) == READ)
904                         try_to_flush_leftover_data(drive);
905         }
906         if (GET_STAT() & (BUSY_STAT|DRQ_STAT))
907                 OUT_BYTE(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);    /* force an abort */
908
909         if (rq->errors >= ERROR_MAX) {
910                 /* ATA-PATTERN */
911                 if (ata_ops(drive) && ata_ops(drive)->end_request)
912                         ata_ops(drive)->end_request(drive, 0);
913                 else
914                         ide_end_request(drive, 0);
915         } else {
916                 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
917                         ++rq->errors;
918                         return do_reset1(drive, 0);
919                 }
920                 if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
921                         drive->special.b.recalibrate = 1;
922                 ++rq->errors;
923         }
924         return ide_stopped;
925 }
926
927 /*
928  * Issue a simple drive command.  The drive must be selected beforehand.
929  */
930 void ide_cmd(ide_drive_t *drive, byte cmd, byte nsect, ide_handler_t *handler)
931 {
932         BUG_ON(HWGROUP(drive)->handler);
933         ide_set_handler (drive, handler, WAIT_CMD, NULL);
934         if (IDE_CONTROL_REG)
935                 OUT_BYTE(drive->ctl,IDE_CONTROL_REG);   /* clear nIEN */
936         SELECT_MASK(drive->channel, drive, 0);
937         OUT_BYTE(nsect,IDE_NSECTOR_REG);
938         OUT_BYTE(cmd,IDE_COMMAND_REG);
939 }
940
941 /*
942  * Invoked on completion of a special DRIVE_CMD.
943  */
944 static ide_startstop_t drive_cmd_intr(ide_drive_t *drive)
945 {
946         struct request *rq = HWGROUP(drive)->rq;
947         u8 *args = rq->buffer;
948         u8 stat = GET_STAT();
949         int retries = 10;
950
951         ide__sti();     /* local CPU only */
952         if ((stat & DRQ_STAT) && args && args[3]) {
953                 ata_read(drive, &args[4], args[3] * SECTOR_WORDS);
954
955                 while (((stat = GET_STAT()) & BUSY_STAT) && retries--)
956                         udelay(100);
957         }
958
959         if (!OK_STAT(stat, READY_STAT, BAD_STAT))
960                 return ide_error(drive, "drive_cmd", stat); /* calls ide_end_drive_cmd */
961         ide_end_drive_cmd (drive, stat, GET_ERR());
962
963         return ide_stopped;
964 }
965
966 /*
967  * Busy-wait for the drive status to be not "busy".  Check then the status for
968  * all of the "good" bits and none of the "bad" bits, and if all is okay it
969  * returns 0.  All other cases return 1 after invoking ide_error() -- caller
970  * should just return.
971  *
972  * This routine should get fixed to not hog the cpu during extra long waits..
973  * That could be done by busy-waiting for the first jiffy or two, and then
974  * setting a timer to wake up at half second intervals thereafter, until
975  * timeout is achieved, before timing out.
976  */
977 int ide_wait_stat(ide_startstop_t *startstop, ide_drive_t *drive, byte good, byte bad, unsigned long timeout) {
978         byte stat;
979         int i;
980         unsigned long flags;
981
982         /* bail early if we've exceeded max_failures */
983         if (drive->max_failures && (drive->failures > drive->max_failures)) {
984                 *startstop = ide_stopped;
985                 return 1;
986         }
987
988         udelay(1);      /* spec allows drive 400ns to assert "BUSY" */
989         if ((stat = GET_STAT()) & BUSY_STAT) {
990                 __save_flags(flags);    /* local CPU only */
991                 ide__sti();             /* local CPU only */
992                 timeout += jiffies;
993                 while ((stat = GET_STAT()) & BUSY_STAT) {
994                         if (0 < (signed long)(jiffies - timeout)) {
995                                 __restore_flags(flags); /* local CPU only */
996                                 *startstop = ide_error(drive, "status timeout", stat);
997                                 return 1;
998                         }
999                 }
1000                 __restore_flags(flags); /* local CPU only */
1001         }
1002         /*
1003          * Allow status to settle, then read it again.
1004          * A few rare drives vastly violate the 400ns spec here,
1005          * so we'll wait up to 10usec for a "good" status
1006          * rather than expensively fail things immediately.
1007          * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
1008          */
1009         for (i = 0; i < 10; i++) {
1010                 udelay(1);
1011                 if (OK_STAT((stat = GET_STAT()), good, bad))
1012                         return 0;
1013         }
1014         *startstop = ide_error(drive, "status error", stat);
1015         return 1;
1016 }
1017
1018 /*
1019  * This initiates handling of a new I/O request.
1020  */
1021 static ide_startstop_t start_request(ide_drive_t *drive, struct request *rq)
1022 {
1023         ide_startstop_t startstop;
1024         unsigned long block;
1025         unsigned int minor = minor(rq->rq_dev), unit = minor >> PARTN_BITS;
1026         struct ata_channel *hwif = drive->channel;
1027
1028         BUG_ON(!(rq->flags & REQ_STARTED));
1029
1030 #ifdef DEBUG
1031         printk("%s: start_request: current=0x%08lx\n", hwif->name, (unsigned long) rq);
1032 #endif
1033
1034         /* bail early if we've exceeded max_failures */
1035         if (drive->max_failures && (drive->failures > drive->max_failures)) {
1036                 goto kill_rq;
1037         }
1038
1039         if (unit >= MAX_DRIVES) {
1040                 printk("%s: bad device number: %s\n", hwif->name, kdevname(rq->rq_dev));
1041                 goto kill_rq;
1042         }
1043         block    = rq->sector;
1044
1045         /* Strange disk manager remap */
1046         if ((rq->flags & REQ_CMD) &&
1047             (drive->type == ATA_DISK || drive->type == ATA_FLOPPY)) {
1048                 block += drive->sect0;
1049         }
1050         /* Yecch - this will shift the entire interval,
1051            possibly killing some innocent following sector */
1052         if (block == 0 && drive->remap_0_to_1 == 1)
1053                 block = 1;  /* redirect MBR access to EZ-Drive partn table */
1054
1055 #if (DISK_RECOVERY_TIME > 0)
1056         while ((read_timer() - hwif->last_time) < DISK_RECOVERY_TIME);
1057 #endif
1058
1059         SELECT_DRIVE(hwif, drive);
1060         if (ide_wait_stat(&startstop, drive, drive->ready_stat,
1061                           BUSY_STAT|DRQ_STAT, WAIT_READY)) {
1062                 printk(KERN_WARNING "%s: drive not ready for command\n", drive->name);
1063                 return startstop;
1064         }
1065
1066         /* FIXME: We can see nicely here that all commands should be submitted
1067          * through the request queue and that the special field in drive should
1068          * go as soon as possible!
1069          */
1070
1071         if (!drive->special.all) {
1072                 if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK | REQ_DRIVE_TASKFILE)) {
1073                         /* This issues a special drive command, usually
1074                          * initiated by ioctl() from the external hdparm
1075                          * program.
1076                          */
1077
1078                         if (rq->flags & REQ_DRIVE_TASKFILE) {
1079                                 struct ata_taskfile *args = rq->special;
1080
1081                                 if (!(args))
1082                                         goto args_error;
1083
1084                                 ata_taskfile(drive, args, NULL);
1085
1086                                 if (((args->command_type == IDE_DRIVE_TASK_RAW_WRITE) ||
1087                                         (args->command_type == IDE_DRIVE_TASK_OUT)) &&
1088                                                 args->prehandler && args->handler)
1089                                         return args->prehandler(drive, rq);
1090                                 return ide_started;
1091
1092                         } else if (rq->flags & REQ_DRIVE_TASK) {
1093                                 byte *args = rq->buffer;
1094                                 byte sel;
1095
1096                                 if (!(args)) goto args_error;
1097 #ifdef DEBUG
1098                                 printk("%s: DRIVE_TASK_CMD ", drive->name);
1099                                 printk("cmd=0x%02x ", args[0]);
1100                                 printk("fr=0x%02x ", args[1]);
1101                                 printk("ns=0x%02x ", args[2]);
1102                                 printk("sc=0x%02x ", args[3]);
1103                                 printk("lcyl=0x%02x ", args[4]);
1104                                 printk("hcyl=0x%02x ", args[5]);
1105                                 printk("sel=0x%02x\n", args[6]);
1106 #endif
1107                                 OUT_BYTE(args[1], IDE_FEATURE_REG);
1108                                 OUT_BYTE(args[3], IDE_SECTOR_REG);
1109                                 OUT_BYTE(args[4], IDE_LCYL_REG);
1110                                 OUT_BYTE(args[5], IDE_HCYL_REG);
1111                                 sel = (args[6] & ~0x10);
1112                                 if (drive->select.b.unit)
1113                                         sel |= 0x10;
1114                                 OUT_BYTE(sel, IDE_SELECT_REG);
1115                                 ide_cmd(drive, args[0], args[2], &drive_cmd_intr);
1116                                 return ide_started;
1117                         } else if (rq->flags & REQ_DRIVE_CMD) {
1118                                 byte *args = rq->buffer;
1119                                 if (!(args)) goto args_error;
1120 #ifdef DEBUG
1121                                 printk("%s: DRIVE_CMD ", drive->name);
1122                                 printk("cmd=0x%02x ", args[0]);
1123                                 printk("sc=0x%02x ", args[1]);
1124                                 printk("fr=0x%02x ", args[2]);
1125                                 printk("xx=0x%02x\n", args[3]);
1126 #endif
1127                                 if (args[0] == WIN_SMART) {
1128                                         OUT_BYTE(0x4f, IDE_LCYL_REG);
1129                                         OUT_BYTE(0xc2, IDE_HCYL_REG);
1130                                         OUT_BYTE(args[2],IDE_FEATURE_REG);
1131                                         OUT_BYTE(args[1],IDE_SECTOR_REG);
1132                                         ide_cmd(drive, args[0], args[3], &drive_cmd_intr);
1133
1134                                         return ide_started;
1135                                 }
1136                                 OUT_BYTE(args[2],IDE_FEATURE_REG);
1137                                 ide_cmd(drive, args[0], args[1], &drive_cmd_intr);
1138                                 return ide_started;
1139                         }
1140
1141 args_error:
1142                         /*
1143                          * NULL is actually a valid way of waiting for all
1144                          * current requests to be flushed from the queue.
1145                          */
1146 #ifdef DEBUG
1147                         printk("%s: DRIVE_CMD (null)\n", drive->name);
1148 #endif
1149                         ide_end_drive_cmd(drive, GET_STAT(), GET_ERR());
1150                         return ide_stopped;
1151                 }
1152
1153                 /* The normal way of execution is to pass execute the request
1154                  * handler.
1155                  */
1156
1157                 if (ata_ops(drive)) {
1158                         if (ata_ops(drive)->do_request)
1159                                 return ata_ops(drive)->do_request(drive, rq, block);
1160                         else {
1161                                 ide_end_request(drive, 0);
1162                                 return ide_stopped;
1163                         }
1164                 }
1165                 printk(KERN_WARNING "%s: device type %d not supported\n",
1166                        drive->name, drive->type);
1167                 goto kill_rq;
1168         }
1169         return ata_special(drive);
1170 kill_rq:
1171         if (ata_ops(drive) && ata_ops(drive)->end_request)
1172                 ata_ops(drive)->end_request(drive, 0);
1173         else
1174                 ide_end_request(drive, 0);
1175         return ide_stopped;
1176 }
1177
1178 ide_startstop_t restart_request(ide_drive_t *drive)
1179 {
1180         ide_hwgroup_t *hwgroup = HWGROUP(drive);
1181         unsigned long flags;
1182         struct request *rq;
1183
1184         spin_lock_irqsave(&ide_lock, flags);
1185         hwgroup->handler = NULL;
1186         del_timer(&hwgroup->timer);
1187         rq = hwgroup->rq;
1188         spin_unlock_irqrestore(&ide_lock, flags);
1189
1190         return start_request(drive, rq);
1191 }
1192
1193 /*
1194  * This is used by a drive to give excess bandwidth back to the hwgroup by
1195  * sleeping for timeout jiffies.
1196  */
1197 void ide_stall_queue(ide_drive_t *drive, unsigned long timeout)
1198 {
1199         if (timeout > WAIT_WORSTCASE)
1200                 timeout = WAIT_WORSTCASE;
1201         drive->PADAM_sleep = timeout + jiffies;
1202 }
1203
1204 /*
1205  * Select the next drive which will be serviced.
1206  */
1207 static inline ide_drive_t *choose_drive(ide_hwgroup_t *hwgroup)
1208 {
1209         ide_drive_t *drive, *best;
1210
1211         best = NULL;
1212         drive = hwgroup->drive;
1213         do {
1214                 if (!list_empty(&drive->queue.queue_head)
1215                 && (!drive->PADAM_sleep || time_after_eq(drive->PADAM_sleep, jiffies))) {
1216                         if (!best
1217                          || (drive->PADAM_sleep && (!best->PADAM_sleep || time_after(best->PADAM_sleep, drive->PADAM_sleep)))
1218                          || (!best->PADAM_sleep && time_after(best->PADAM_service_start + 2 * best->PADAM_service_time, drive->PADAM_service_start + 2 * drive->PADAM_service_time)))
1219                         {
1220                                 if (!blk_queue_plugged(&drive->queue))
1221                                         best = drive;
1222                         }
1223                 }
1224         } while ((drive = drive->next) != hwgroup->drive);
1225         return best;
1226 }
1227
1228 /*
1229  * Issue a new request to a drive from hwgroup
1230  * Caller must have already done spin_lock_irqsave(&ide_lock, ...)
1231  *
1232  * A hwgroup is a serialized group of IDE interfaces.  Usually there is
1233  * exactly one hwif (interface) per hwgroup, but buggy controllers (eg. CMD640)
1234  * may have both interfaces in a single hwgroup to "serialize" access.
1235  * Or possibly multiple ISA interfaces can share a common IRQ by being grouped
1236  * together into one hwgroup for serialized access.
1237  *
1238  * Note also that several hwgroups can end up sharing a single IRQ,
1239  * possibly along with many other devices.  This is especially common in
1240  * PCI-based systems with off-board IDE controller cards.
1241  *
1242  * The IDE driver uses the queue spinlock to protect access to the request
1243  * queues.
1244  *
1245  * The first thread into the driver for a particular hwgroup sets the
1246  * hwgroup->flags IDE_BUSY flag to indicate that this hwgroup is now active,
1247  * and then initiates processing of the top request from the request queue.
1248  *
1249  * Other threads attempting entry notice the busy setting, and will simply
1250  * queue their new requests and exit immediately.  Note that hwgroup->flags
1251  * remains busy even when the driver is merely awaiting the next interrupt.
1252  * Thus, the meaning is "this hwgroup is busy processing a request".
1253  *
1254  * When processing of a request completes, the completing thread or IRQ-handler
1255  * will start the next request from the queue.  If no more work remains,
1256  * the driver will clear the hwgroup->flags IDE_BUSY flag and exit.
1257  */
1258 static void ide_do_request(ide_hwgroup_t *hwgroup, int masked_irq)
1259 {
1260         ide_drive_t *drive;
1261         struct ata_channel *hwif;
1262         ide_startstop_t startstop;
1263         struct request  *rq;
1264
1265         ide_get_lock(&ide_intr_lock, ide_intr, hwgroup);/* for atari only: POSSIBLY BROKEN HERE(?) */
1266
1267         __cli();        /* necessary paranoia: ensure IRQs are masked on local CPU */
1268
1269         while (!test_and_set_bit(IDE_BUSY, &hwgroup->flags)) {
1270                 drive = choose_drive(hwgroup);
1271                 if (drive == NULL) {
1272                         unsigned long sleep = 0;
1273                         hwgroup->rq = NULL;
1274                         drive = hwgroup->drive;
1275                         do {
1276                                 if (drive->PADAM_sleep && (!sleep || time_after(sleep, drive->PADAM_sleep)))
1277                                         sleep = drive->PADAM_sleep;
1278                         } while ((drive = drive->next) != hwgroup->drive);
1279                         if (sleep) {
1280                                 /*
1281                                  * Take a short snooze, and then wake up this hwgroup again.
1282                                  * This gives other hwgroups on the same a chance to
1283                                  * play fairly with us, just in case there are big differences
1284                                  * in relative throughputs.. don't want to hog the cpu too much.
1285                                  */
1286                                 if (0 < (signed long)(jiffies + WAIT_MIN_SLEEP - sleep))
1287                                         sleep = jiffies + WAIT_MIN_SLEEP;
1288 #if 1
1289                                 if (timer_pending(&hwgroup->timer))
1290                                         printk("ide_set_handler: timer already active\n");
1291 #endif
1292                                 set_bit(IDE_SLEEP, &hwgroup->flags);
1293                                 mod_timer(&hwgroup->timer, sleep);
1294                                 /* we purposely leave hwgroup busy while sleeping */
1295                         } else {
1296                                 /* Ugly, but how can we sleep for the lock otherwise? perhaps from tq_disk? */
1297                                 ide_release_lock(&ide_intr_lock);/* for atari only */
1298                                 clear_bit(IDE_BUSY, &hwgroup->flags);
1299                         }
1300                         return;         /* no more work for this hwgroup (for now) */
1301                 }
1302                 hwif = drive->channel;
1303                 if (hwgroup->hwif->sharing_irq && hwif != hwgroup->hwif && hwif->io_ports[IDE_CONTROL_OFFSET]) {
1304                         /* set nIEN for previous hwif */
1305
1306                         if (hwif->intrproc)
1307                                 hwif->intrproc(drive);
1308                         else
1309                                 OUT_BYTE((drive)->ctl|2, hwif->io_ports[IDE_CONTROL_OFFSET]);
1310                 }
1311                 hwgroup->hwif = hwif;
1312                 hwgroup->drive = drive;
1313                 drive->PADAM_sleep = 0;
1314                 drive->PADAM_service_start = jiffies;
1315
1316                 if (blk_queue_plugged(&drive->queue))
1317                         BUG();
1318
1319                 /*
1320                  * just continuing an interrupted request maybe
1321                  */
1322                 rq = hwgroup->rq = elv_next_request(&drive->queue);
1323
1324                 /*
1325                  * Some systems have trouble with IDE IRQs arriving while
1326                  * the driver is still setting things up.  So, here we disable
1327                  * the IRQ used by this interface while the request is being started.
1328                  * This may look bad at first, but pretty much the same thing
1329                  * happens anyway when any interrupt comes in, IDE or otherwise
1330                  *  -- the kernel masks the IRQ while it is being handled.
1331                  */
1332                 if (masked_irq && hwif->irq != masked_irq)
1333                         disable_irq_nosync(hwif->irq);
1334                 spin_unlock(&ide_lock);
1335                 ide__sti();     /* allow other IRQs while we start this request */
1336                 startstop = start_request(drive, rq);
1337                 spin_lock_irq(&ide_lock);
1338                 if (masked_irq && hwif->irq != masked_irq)
1339                         enable_irq(hwif->irq);
1340                 if (startstop == ide_stopped)
1341                         clear_bit(IDE_BUSY, &hwgroup->flags);
1342         }
1343 }
1344
1345 /*
1346  * Returns the queue which corresponds to a given device.
1347  */
1348 request_queue_t *ide_get_queue(kdev_t dev)
1349 {
1350         struct ata_channel *channel = (struct ata_channel *)blk_dev[major(dev)].data;
1351
1352         /* FIXME: ALLERT: This discriminates between master and slave! */
1353         return &channel->drives[DEVICE_NR(dev) & 1].queue;
1354 }
1355
1356 /*
1357  * Passes the stuff to ide_do_request
1358  */
1359 void do_ide_request(request_queue_t *q)
1360 {
1361         ide_do_request(q->queuedata, 0);
1362 }
1363
1364 /*
1365  * un-busy the hwgroup etc, and clear any pending DMA status. we want to
1366  * retry the current request in PIO mode instead of risking tossing it
1367  * all away
1368  */
1369 void ide_dma_timeout_retry(ide_drive_t *drive)
1370 {
1371         struct ata_channel *hwif = drive->channel;
1372         struct request *rq;
1373
1374         /*
1375          * end current dma transaction
1376          */
1377         hwif->dmaproc(ide_dma_end, drive);
1378
1379         /*
1380          * complain a little, later we might remove some of this verbosity
1381          */
1382         printk("%s: timeout waiting for DMA\n", drive->name);
1383         hwif->dmaproc(ide_dma_timeout, drive);
1384
1385         /*
1386          * Disable dma for now, but remember that we did so because of
1387          * a timeout -- we'll reenable after we finish this next request
1388          * (or rather the first chunk of it) in pio.
1389          */
1390         drive->retry_pio++;
1391         drive->state = DMA_PIO_RETRY;
1392         hwif->dmaproc(ide_dma_off_quietly, drive);
1393
1394         /*
1395          * un-busy drive etc (hwgroup->busy is cleared on return) and
1396          * make sure request is sane
1397          */
1398         rq = HWGROUP(drive)->rq;
1399         HWGROUP(drive)->rq = NULL;
1400
1401         rq->errors = 0;
1402         if (rq->bio) {
1403                 rq->sector = rq->bio->bi_sector;
1404                 rq->current_nr_sectors = bio_iovec(rq->bio)->bv_len >> 9;
1405                 rq->buffer = NULL;
1406         }
1407 }
1408
1409 /*
1410  * This is our timeout function for all drive operations.  But note that it can
1411  * also be invoked as a result of a "sleep" operation triggered by the
1412  * mod_timer() call in ide_do_request.
1413  */
1414 void ide_timer_expiry(unsigned long data)
1415 {
1416         ide_hwgroup_t   *hwgroup = (ide_hwgroup_t *) data;
1417         ide_handler_t   *handler;
1418         ide_expiry_t    *expiry;
1419         unsigned long   flags;
1420         unsigned long   wait;
1421
1422         /*
1423          * a global lock protects timers etc -- shouldn't get contention
1424          * worth mentioning
1425          */
1426         spin_lock_irqsave(&ide_lock, flags);
1427         del_timer(&hwgroup->timer);
1428
1429         if ((handler = hwgroup->handler) == NULL) {
1430                 /*
1431                  * Either a marginal timeout occurred
1432                  * (got the interrupt just as timer expired),
1433                  * or we were "sleeping" to give other devices a chance.
1434                  * Either way, we don't really want to complain about anything.
1435                  */
1436                 if (test_and_clear_bit(IDE_SLEEP, &hwgroup->flags))
1437                         clear_bit(IDE_BUSY, &hwgroup->flags);
1438         } else {
1439                 ide_drive_t *drive = hwgroup->drive;
1440                 if (!drive) {
1441                         printk("ide_timer_expiry: hwgroup->drive was NULL\n");
1442                         hwgroup->handler = NULL;
1443                 } else {
1444                         struct ata_channel *hwif;
1445                         ide_startstop_t startstop;
1446                         /* paranoia */
1447                         if (!test_and_set_bit(IDE_BUSY, &hwgroup->flags))
1448                                 printk("%s: ide_timer_expiry: hwgroup was not busy??\n", drive->name);
1449                         if ((expiry = hwgroup->expiry) != NULL) {
1450                                 /* continue */
1451                                 if ((wait = expiry(drive)) != 0) {
1452                                         /* reengage timer */
1453                                         hwgroup->timer.expires  = jiffies + wait;
1454                                         add_timer(&hwgroup->timer);
1455                                         spin_unlock_irqrestore(&ide_lock, flags);
1456                                         return;
1457                                 }
1458                         }
1459                         hwgroup->handler = NULL;
1460                         /*
1461                          * We need to simulate a real interrupt when invoking
1462                          * the handler() function, which means we need to globally
1463                          * mask the specific IRQ:
1464                          */
1465                         spin_unlock(&ide_lock);
1466                         hwif  = drive->channel;
1467 #if DISABLE_IRQ_NOSYNC
1468                         disable_irq_nosync(hwif->irq);
1469 #else
1470                         disable_irq(hwif->irq); /* disable_irq_nosync ?? */
1471 #endif
1472                         __cli();        /* local CPU only, as if we were handling an interrupt */
1473                         if (hwgroup->poll_timeout != 0) {
1474                                 startstop = handler(drive);
1475                         } else if (drive_is_ready(drive)) {
1476                                 if (drive->waiting_for_dma)
1477                                         (void) hwgroup->hwif->dmaproc(ide_dma_lostirq, drive);
1478                                 (void)ide_ack_intr(hwif);
1479                                 printk("%s: lost interrupt\n", drive->name);
1480                                 startstop = handler(drive);
1481                         } else {
1482                                 if (drive->waiting_for_dma) {
1483                                         startstop = ide_stopped;
1484                                         ide_dma_timeout_retry(drive);
1485                                 } else
1486                                         startstop = ide_error(drive, "irq timeout", GET_STAT());
1487                         }
1488                         set_recovery_timer(hwif);
1489                         drive->PADAM_service_time = jiffies - drive->PADAM_service_start;
1490                         enable_irq(hwif->irq);
1491                         spin_lock_irq(&ide_lock);
1492                         if (startstop == ide_stopped)
1493                                 clear_bit(IDE_BUSY, &hwgroup->flags);
1494                 }
1495         }
1496         ide_do_request(hwgroup, 0);
1497         spin_unlock_irqrestore(&ide_lock, flags);
1498 }
1499
1500 /*
1501  * There's nothing really useful we can do with an unexpected interrupt,
1502  * other than reading the status register (to clear it), and logging it.
1503  * There should be no way that an irq can happen before we're ready for it,
1504  * so we needn't worry much about losing an "important" interrupt here.
1505  *
1506  * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
1507  * drive enters "idle", "standby", or "sleep" mode, so if the status looks
1508  * "good", we just ignore the interrupt completely.
1509  *
1510  * This routine assumes __cli() is in effect when called.
1511  *
1512  * If an unexpected interrupt happens on irq15 while we are handling irq14
1513  * and if the two interfaces are "serialized" (CMD640), then it looks like
1514  * we could screw up by interfering with a new request being set up for irq15.
1515  *
1516  * In reality, this is a non-issue.  The new command is not sent unless the
1517  * drive is ready to accept one, in which case we know the drive is not
1518  * trying to interrupt us.  And ide_set_handler() is always invoked before
1519  * completing the issuance of any new drive command, so we will not be
1520  * accidentally invoked as a result of any valid command completion interrupt.
1521  *
1522  */
1523 static void unexpected_intr(int irq, ide_hwgroup_t *hwgroup)
1524 {
1525         u8 stat;
1526         struct ata_channel *hwif = hwgroup->hwif;
1527
1528         /*
1529          * handle the unexpected interrupt
1530          */
1531         do {
1532                 if (hwif->irq != irq)
1533                         continue;
1534
1535                 stat = IN_BYTE(hwif->io_ports[IDE_STATUS_OFFSET]);
1536                 if (!OK_STAT(stat, READY_STAT, BAD_STAT)) {
1537                         /* Try to not flood the console with msgs */
1538                         static unsigned long last_msgtime;
1539                         static int count;
1540                         ++count;
1541                         if (time_after(jiffies, last_msgtime + HZ)) {
1542                                 last_msgtime = jiffies;
1543                                 printk("%s%s: unexpected interrupt, status=0x%02x, count=%d\n",
1544                                                 hwif->name, (hwif->next == hwgroup->hwif) ? "" : "(?)", stat, count);
1545                         }
1546                 }
1547                 hwif = hwif->next;
1548         } while (hwif != hwgroup->hwif);
1549 }
1550
1551 /*
1552  * entry point for all interrupts, caller does __cli() for us
1553  */
1554 void ide_intr(int irq, void *dev_id, struct pt_regs *regs)
1555 {
1556         unsigned long flags;
1557         ide_hwgroup_t *hwgroup = (ide_hwgroup_t *)dev_id;
1558         struct ata_channel *hwif;
1559         ide_drive_t *drive;
1560         ide_handler_t *handler;
1561         ide_startstop_t startstop;
1562
1563         spin_lock_irqsave(&ide_lock, flags);
1564         hwif = hwgroup->hwif;
1565
1566         if (!ide_ack_intr(hwif))
1567                 goto out_lock;
1568
1569         if ((handler = hwgroup->handler) == NULL || hwgroup->poll_timeout != 0) {
1570                 printk(KERN_INFO "ide: unexpected interrupt %d %d\n", hwif->unit, irq);
1571
1572                 /*
1573                  * Not expecting an interrupt from this drive.
1574                  * That means this could be:
1575                  *      (1) an interrupt from another PCI device
1576                  *      sharing the same PCI INT# as us.
1577                  * or   (2) a drive just entered sleep or standby mode,
1578                  *      and is interrupting to let us know.
1579                  * or   (3) a spurious interrupt of unknown origin.
1580                  *
1581                  * For PCI, we cannot tell the difference,
1582                  * so in that case we just ignore it and hope it goes away.
1583                  */
1584 #ifdef CONFIG_BLK_DEV_IDEPCI
1585                 if (hwif->pci_dev && !hwif->pci_dev->vendor)
1586 #endif
1587                 {
1588                         /*
1589                          * Probably not a shared PCI interrupt,
1590                          * so we can safely try to do something about it:
1591                          */
1592                         unexpected_intr(irq, hwgroup);
1593 #ifdef CONFIG_BLK_DEV_IDEPCI
1594                 } else {
1595                         /*
1596                          * Whack the status register, just in case we have a leftover pending IRQ.
1597                          */
1598                         IN_BYTE(hwif->io_ports[IDE_STATUS_OFFSET]);
1599 #endif
1600                 }
1601                 goto out_lock;
1602         }
1603         drive = hwgroup->drive;
1604         if (!drive) {
1605                 /*
1606                  * This should NEVER happen, and there isn't much we could do
1607                  * about it here.
1608                  */
1609                 goto out_lock;
1610         }
1611         if (!drive_is_ready(drive)) {
1612                 /*
1613                  * This happens regularly when we share a PCI IRQ with another device.
1614                  * Unfortunately, it can also happen with some buggy drives that trigger
1615                  * the IRQ before their status register is up to date.  Hopefully we have
1616                  * enough advance overhead that the latter isn't a problem.
1617                  */
1618                 goto out_lock;
1619         }
1620         /* paranoia */
1621         if (!test_and_set_bit(IDE_BUSY, &hwgroup->flags))
1622                 printk("%s: ide_intr: hwgroup was not busy??\n", drive->name);
1623         hwgroup->handler = NULL;
1624         del_timer(&hwgroup->timer);
1625         spin_unlock(&ide_lock);
1626
1627         if (hwif->unmask)
1628                 ide__sti();     /* local CPU only */
1629         startstop = handler(drive);             /* service this interrupt, may set handler for next interrupt */
1630         spin_lock_irq(&ide_lock);
1631
1632         /*
1633          * Note that handler() may have set things up for another
1634          * interrupt to occur soon, but it cannot happen until
1635          * we exit from this routine, because it will be the
1636          * same irq as is currently being serviced here, and Linux
1637          * won't allow another of the same (on any CPU) until we return.
1638          */
1639         set_recovery_timer(drive->channel);
1640         drive->PADAM_service_time = jiffies - drive->PADAM_service_start;
1641         if (startstop == ide_stopped) {
1642                 if (hwgroup->handler == NULL) { /* paranoia */
1643                         clear_bit(IDE_BUSY, &hwgroup->flags);
1644                         ide_do_request(hwgroup, hwif->irq);
1645                 } else {
1646                         printk("%s: ide_intr: huh? expected NULL handler on exit\n", drive->name);
1647                 }
1648         }
1649
1650 out_lock:
1651         spin_unlock_irqrestore(&ide_lock, flags);
1652 }
1653
1654 /*
1655  * get_info_ptr() returns the (ide_drive_t *) for a given device number.
1656  * It returns NULL if the given device number does not match any present drives.
1657  */
1658 ide_drive_t *get_info_ptr(kdev_t i_rdev)
1659 {
1660         unsigned int major = major(i_rdev);
1661         int h;
1662
1663         for (h = 0; h < MAX_HWIFS; ++h) {
1664                 struct ata_channel *hwif = &ide_hwifs[h];
1665                 if (hwif->present && major == hwif->major) {
1666                         int unit = DEVICE_NR(i_rdev);
1667                         if (unit < MAX_DRIVES) {
1668                                 ide_drive_t *drive = &hwif->drives[unit];
1669                                 if (drive->present)
1670                                         return drive;
1671                         }
1672                         break;
1673                 }
1674         }
1675         return NULL;
1676 }
1677
1678 /*
1679  * This function is intended to be used prior to invoking ide_do_drive_cmd().
1680  */
1681 void ide_init_drive_cmd (struct request *rq)
1682 {
1683         memset(rq, 0, sizeof(*rq));
1684         rq->flags = REQ_DRIVE_CMD;
1685 }
1686
1687 /*
1688  * This function issues a special IDE device request onto the request queue.
1689  *
1690  * If action is ide_wait, then the rq is queued at the end of the request
1691  * queue, and the function sleeps until it has been processed.  This is for use
1692  * when invoked from an ioctl handler.
1693  *
1694  * If action is ide_preempt, then the rq is queued at the head of the request
1695  * queue, displacing the currently-being-processed request and this function
1696  * returns immediately without waiting for the new rq to be completed.  This is
1697  * VERY DANGEROUS, and is intended for careful use by the ATAPI tape/cdrom
1698  * driver code.
1699  *
1700  * If action is ide_next, then the rq is queued immediately after the
1701  * currently-being-processed-request (if any), and the function returns without
1702  * waiting for the new rq to be completed.  As above, This is VERY DANGEROUS,
1703  * and is intended for careful use by the ATAPI tape/cdrom driver code.
1704  *
1705  * If action is ide_end, then the rq is queued at the end of the request queue,
1706  * and the function returns immediately without waiting for the new rq to be
1707  * completed. This is again intended for careful use by the ATAPI tape/cdrom
1708  * driver code.
1709  */
1710 int ide_do_drive_cmd(ide_drive_t *drive, struct request *rq, ide_action_t action)
1711 {
1712         unsigned long flags;
1713         ide_hwgroup_t *hwgroup = HWGROUP(drive);
1714         unsigned int major = drive->channel->major;
1715         request_queue_t *q = &drive->queue;
1716         struct list_head *queue_head = &q->queue_head;
1717         DECLARE_COMPLETION(wait);
1718
1719 #ifdef CONFIG_BLK_DEV_PDC4030
1720         if (drive->channel->chipset == ide_pdc4030 && rq->buffer != NULL)
1721                 return -ENOSYS;  /* special drive cmds not supported */
1722 #endif
1723         rq->errors = 0;
1724         rq->rq_status = RQ_ACTIVE;
1725         rq->rq_dev = mk_kdev(major,(drive->select.b.unit)<<PARTN_BITS);
1726         if (action == ide_wait)
1727                 rq->waiting = &wait;
1728         spin_lock_irqsave(&ide_lock, flags);
1729         if (blk_queue_empty(&drive->queue) || action == ide_preempt) {
1730                 if (action == ide_preempt)
1731                         hwgroup->rq = NULL;
1732         } else {
1733                 if (action == ide_wait || action == ide_end)
1734                         queue_head = queue_head->prev;
1735                 else
1736                         queue_head = queue_head->next;
1737         }
1738         q->elevator.elevator_add_req_fn(q, rq, queue_head);
1739         ide_do_request(hwgroup, 0);
1740         spin_unlock_irqrestore(&ide_lock, flags);
1741         if (action == ide_wait) {
1742                 wait_for_completion(&wait);     /* wait for it to be serviced */
1743                 return rq->errors ? -EIO : 0;   /* return -EIO if errors */
1744         }
1745         return 0;
1746
1747 }
1748
1749 /*
1750  * This routine is called to flush all partitions and partition tables
1751  * for a changed disk, and then re-read the new partition table.
1752  * If we are revalidating a disk because of a media change, then we
1753  * enter with usage == 0.  If we are using an ioctl, we automatically have
1754  * usage == 1 (we need an open channel to use an ioctl :-), so this
1755  * is our limit.
1756  */
1757 int ide_revalidate_disk (kdev_t i_rdev)
1758 {
1759         ide_drive_t *drive;
1760         ide_hwgroup_t *hwgroup;
1761         unsigned long flags;
1762         int res;
1763
1764         if ((drive = get_info_ptr(i_rdev)) == NULL)
1765                 return -ENODEV;
1766         hwgroup = HWGROUP(drive);
1767         spin_lock_irqsave(&ide_lock, flags);
1768         if (drive->busy || (drive->usage > 1)) {
1769                 spin_unlock_irqrestore(&ide_lock, flags);
1770                 return -EBUSY;
1771         }
1772         drive->busy = 1;
1773         MOD_INC_USE_COUNT;
1774         spin_unlock_irqrestore(&ide_lock, flags);
1775
1776         res = wipe_partitions(i_rdev);
1777         if (!res) {
1778                 if (ata_ops(drive) && ata_ops(drive)->revalidate) {
1779                         ata_get(ata_ops(drive));
1780                         /* this is a no-op for tapes and SCSI based access */
1781                         ata_ops(drive)->revalidate(drive);
1782                         ata_put(ata_ops(drive));
1783                 } else
1784                         grok_partitions(i_rdev, ata_capacity(drive));
1785         }
1786
1787         drive->busy = 0;
1788         wake_up(&drive->wqueue);
1789         MOD_DEC_USE_COUNT;
1790         return res;
1791 }
1792
1793 /*
1794  * Look again for all drives in the system on all interfaces.  This is used
1795  * after a new driver category has been loaded as module.
1796  */
1797 void revalidate_drives(void)
1798 {
1799         struct ata_channel *hwif;
1800         ide_drive_t *drive;
1801         int h;
1802
1803         for (h = 0; h < MAX_HWIFS; ++h) {
1804                 int unit;
1805                 hwif = &ide_hwifs[h];
1806                 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1807                         drive = &ide_hwifs[h].drives[unit];
1808                         if (drive->revalidate) {
1809                                 drive->revalidate = 0;
1810                                 if (!initializing)
1811                                         ide_revalidate_disk(mk_kdev(hwif->major, unit<<PARTN_BITS));
1812                         }
1813                 }
1814         }
1815 }
1816
1817 static void ide_probe_module(void)
1818 {
1819         ideprobe_init();
1820         revalidate_drives();
1821 }
1822
1823 static void ide_driver_module (void)
1824 {
1825         int index;
1826
1827         for (index = 0; index < MAX_HWIFS; ++index)
1828                 if (ide_hwifs[index].present)
1829                         goto search;
1830         ide_probe_module();
1831 search:
1832
1833         revalidate_drives();
1834 }
1835
1836 static int ide_open(struct inode * inode, struct file * filp)
1837 {
1838         ide_drive_t *drive;
1839
1840         if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
1841                 return -ENXIO;
1842         if (drive->driver == NULL)
1843                 ide_driver_module();
1844
1845         /* Request a particular device type module.
1846          *
1847          * FIXME: The function which should rather requests the drivers in
1848          * ide_driver_module(), since it seems illogical and even a bit
1849          * dangerous to delay this until open time!
1850          */
1851
1852 #ifdef CONFIG_KMOD
1853         if (drive->driver == NULL) {
1854                 char *module = NULL;
1855
1856                 switch (drive->type) {
1857                         case ATA_DISK:
1858                                 module = "ide-disk";
1859                                 break;
1860                         case ATA_ROM:
1861                                 module = "ide-cd";
1862                                 break;
1863                         case ATA_TAPE:
1864                                 module = "ide-tape";
1865                                 break;
1866                         case ATA_FLOPPY:
1867                                 module = "ide-floppy";
1868                                 break;
1869                         case ATA_SCSI:
1870                                 module = "ide-scsi";
1871                                 break;
1872                         default:
1873                                 /* nothing we can do about it */ ;
1874                 }
1875                 if (module)
1876                         request_module(module);
1877         }
1878 #endif
1879         while (drive->busy)
1880                 sleep_on(&drive->wqueue);
1881         ++drive->usage;
1882         if (ata_ops(drive) && ata_ops(drive)->open)
1883                 return ata_ops(drive)->open(inode, filp, drive);
1884         else {
1885                 --drive->usage;
1886                 return -ENODEV;
1887         }
1888
1889         printk(KERN_INFO "%s: driver not present\n", drive->name);
1890         drive->usage--;
1891         return -ENXIO;
1892 }
1893
1894 /*
1895  * Releasing a block device means we sync() it, so that it can safely
1896  * be forgotten about...
1897  */
1898 static int ide_release(struct inode * inode, struct file * file)
1899 {
1900         ide_drive_t *drive;
1901
1902         if (!(drive = get_info_ptr(inode->i_rdev)))
1903                 return 0;
1904
1905         drive->usage--;
1906         if (ata_ops(drive) && ata_ops(drive)->release)
1907                 ata_ops(drive)->release(inode, file, drive);
1908         return 0;
1909 }
1910
1911 #ifdef CONFIG_PROC_FS
1912 ide_proc_entry_t generic_subdriver_entries[] = {
1913         { "capacity",   S_IFREG|S_IRUGO,        proc_ide_read_capacity, NULL },
1914         { NULL, 0, NULL, NULL }
1915 };
1916 #endif
1917
1918 /*
1919  * Note that we only release the standard ports, and do not even try to handle
1920  * any extra ports allocated for weird IDE interface chipsets.
1921  */
1922 static void hwif_unregister(struct ata_channel *hwif)
1923 {
1924         if (hwif->straight8) {
1925                 ide_release_region(hwif->io_ports[IDE_DATA_OFFSET], 8);
1926         } else {
1927                 if (hwif->io_ports[IDE_DATA_OFFSET])
1928                         ide_release_region(hwif->io_ports[IDE_DATA_OFFSET], 1);
1929                 if (hwif->io_ports[IDE_ERROR_OFFSET])
1930                         ide_release_region(hwif->io_ports[IDE_ERROR_OFFSET], 1);
1931                 if (hwif->io_ports[IDE_NSECTOR_OFFSET])
1932                         ide_release_region(hwif->io_ports[IDE_NSECTOR_OFFSET], 1);
1933                 if (hwif->io_ports[IDE_SECTOR_OFFSET])
1934                         ide_release_region(hwif->io_ports[IDE_SECTOR_OFFSET], 1);
1935                 if (hwif->io_ports[IDE_LCYL_OFFSET])
1936                         ide_release_region(hwif->io_ports[IDE_LCYL_OFFSET], 1);
1937                 if (hwif->io_ports[IDE_HCYL_OFFSET])
1938                         ide_release_region(hwif->io_ports[IDE_HCYL_OFFSET], 1);
1939                 if (hwif->io_ports[IDE_SELECT_OFFSET])
1940                         ide_release_region(hwif->io_ports[IDE_SELECT_OFFSET], 1);
1941                 if (hwif->io_ports[IDE_STATUS_OFFSET])
1942                         ide_release_region(hwif->io_ports[IDE_STATUS_OFFSET], 1);
1943         }
1944         if (hwif->io_ports[IDE_CONTROL_OFFSET])
1945                 ide_release_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
1946 #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
1947         if (hwif->io_ports[IDE_IRQ_OFFSET])
1948                 ide_release_region(hwif->io_ports[IDE_IRQ_OFFSET], 1);
1949 #endif
1950 }
1951
1952 void ide_unregister(struct ata_channel *channel)
1953 {
1954         struct gendisk *gd;
1955         ide_drive_t *drive, *d;
1956         ide_hwgroup_t *hwgroup;
1957         int unit, i;
1958         unsigned long flags;
1959         unsigned int p, minor;
1960         struct ata_channel old_hwif;
1961
1962         spin_lock_irqsave(&ide_lock, flags);
1963         if (!channel->present)
1964                 goto abort;
1965         put_device(&channel->dev);
1966         for (unit = 0; unit < MAX_DRIVES; ++unit) {
1967                 drive = &channel->drives[unit];
1968                 if (!drive->present)
1969                         continue;
1970                 if (drive->busy || drive->usage)
1971                         goto abort;
1972                 if (ata_ops(drive)) {
1973                         if (ata_ops(drive)->cleanup) {
1974                                 if (ata_ops(drive)->cleanup(drive))
1975                                         goto abort;
1976                         } else
1977                                 ide_unregister_subdriver(drive);
1978                 }
1979         }
1980         channel->present = 0;
1981
1982         /*
1983          * All clear?  Then blow away the buffer cache
1984          */
1985         spin_unlock_irqrestore(&ide_lock, flags);
1986         for (unit = 0; unit < MAX_DRIVES; ++unit) {
1987                 drive = &channel->drives[unit];
1988                 if (!drive->present)
1989                         continue;
1990                 minor = drive->select.b.unit << PARTN_BITS;
1991                 for (p = 0; p < (1<<PARTN_BITS); ++p) {
1992                         if (drive->part[p].nr_sects > 0) {
1993                                 kdev_t devp = mk_kdev(channel->major, minor+p);
1994                                 invalidate_device(devp, 0);
1995                         }
1996                 }
1997         }
1998 #ifdef CONFIG_PROC_FS
1999         destroy_proc_ide_drives(channel);
2000 #endif
2001         spin_lock_irqsave(&ide_lock, flags);
2002         hwgroup = channel->hwgroup;
2003
2004         /*
2005          * free the irq if we were the only hwif using it
2006          */
2007         {
2008                 struct ata_channel *g;
2009                 int irq_count = 0;
2010
2011                 g = hwgroup->hwif;
2012                 do {
2013                         if (g->irq == channel->irq)
2014                                 ++irq_count;
2015                         g = g->next;
2016                 } while (g != hwgroup->hwif);
2017                 if (irq_count == 1)
2018                         free_irq(channel->irq, hwgroup);
2019         }
2020         hwif_unregister(channel);
2021
2022         /*
2023          * Remove us from the hwgroup, and free
2024          * the hwgroup if we were the only member
2025          */
2026         d = hwgroup->drive;
2027         for (i = 0; i < MAX_DRIVES; ++i) {
2028                 drive = &channel->drives[i];
2029                 if (drive->de) {
2030                         devfs_unregister (drive->de);
2031                         drive->de = NULL;
2032                 }
2033                 if (!drive->present)
2034                         continue;
2035                 while (hwgroup->drive->next != drive)
2036                         hwgroup->drive = hwgroup->drive->next;
2037                 hwgroup->drive->next = drive->next;
2038                 if (hwgroup->drive == drive)
2039                         hwgroup->drive = NULL;
2040                 if (drive->id != NULL) {
2041                         kfree(drive->id);
2042                         drive->id = NULL;
2043                 }
2044                 drive->present = 0;
2045                 blk_cleanup_queue(&drive->queue);
2046         }
2047         if (d->present)
2048                 hwgroup->drive = d;
2049         while (hwgroup->hwif->next != channel)
2050                 hwgroup->hwif = hwgroup->hwif->next;
2051         hwgroup->hwif->next = channel->next;
2052         if (hwgroup->hwif == channel)
2053                 kfree(hwgroup);
2054         else
2055                 hwgroup->hwif = hwgroup->drive->channel;
2056
2057 #if defined(CONFIG_BLK_DEV_IDEDMA) && !defined(CONFIG_DMA_NONPCI)
2058         ide_release_dma(channel);
2059 #endif
2060
2061         /*
2062          * Remove us from the kernel's knowledge.
2063          */
2064         unregister_blkdev(channel->major, channel->name);
2065         kfree(blksize_size[channel->major]);
2066         blk_dev[channel->major].data = NULL;
2067         blk_dev[channel->major].queue = NULL;
2068         blk_clear(channel->major);
2069         gd = channel->gd;
2070         if (gd) {
2071                 del_gendisk(gd);
2072                 kfree(gd->sizes);
2073                 kfree(gd->part);
2074                 if (gd->de_arr)
2075                         kfree (gd->de_arr);
2076                 if (gd->flags)
2077                         kfree (gd->flags);
2078                 kfree(gd);
2079                 channel->gd = NULL;
2080         }
2081
2082         /*
2083          * Reinitialize the channel handler, but preserve any special methods for
2084          * it.
2085          */
2086
2087         old_hwif = *channel;
2088         init_hwif_data(channel, channel->index);
2089         channel->hwgroup = old_hwif.hwgroup;
2090         channel->tuneproc = old_hwif.tuneproc;
2091         channel->speedproc = old_hwif.speedproc;
2092         channel->selectproc = old_hwif.selectproc;
2093         channel->resetproc = old_hwif.resetproc;
2094         channel->intrproc = old_hwif.intrproc;
2095         channel->maskproc = old_hwif.maskproc;
2096         channel->quirkproc = old_hwif.quirkproc;
2097         channel->rwproc = old_hwif.rwproc;
2098         channel->ata_read = old_hwif.ata_read;
2099         channel->ata_write = old_hwif.ata_write;
2100         channel->atapi_read = old_hwif.atapi_read;
2101         channel->atapi_write = old_hwif.atapi_write;
2102         channel->dmaproc = old_hwif.dmaproc;
2103         channel->busproc = old_hwif.busproc;
2104         channel->bus_state = old_hwif.bus_state;
2105         channel->dma_base = old_hwif.dma_base;
2106         channel->dma_extra = old_hwif.dma_extra;
2107         channel->config_data = old_hwif.config_data;
2108         channel->select_data = old_hwif.select_data;
2109         channel->proc = old_hwif.proc;
2110 #ifndef CONFIG_BLK_DEV_IDECS
2111         channel->irq = old_hwif.irq;
2112 #endif
2113         channel->major = old_hwif.major;
2114         channel->chipset = old_hwif.chipset;
2115         channel->autodma = old_hwif.autodma;
2116         channel->udma_four = old_hwif.udma_four;
2117 #ifdef CONFIG_BLK_DEV_IDEPCI
2118         channel->pci_dev = old_hwif.pci_dev;
2119 #else
2120         channel->pci_dev = NULL;
2121 #endif
2122         channel->straight8 = old_hwif.straight8;
2123 abort:
2124         spin_unlock_irqrestore(&ide_lock, flags);
2125 }
2126
2127 /*
2128  * Setup hw_regs_t structure described by parameters.  You
2129  * may set up the hw structure yourself OR use this routine to
2130  * do it for you.
2131  */
2132 void ide_setup_ports (  hw_regs_t *hw,
2133                         ide_ioreg_t base, int *offsets,
2134                         ide_ioreg_t ctrl, ide_ioreg_t intr,
2135                         ide_ack_intr_t *ack_intr, int irq)
2136 {
2137         int i;
2138
2139         for (i = 0; i < IDE_NR_PORTS; i++) {
2140                 if (offsets[i] == -1) {
2141                         switch(i) {
2142                                 case IDE_CONTROL_OFFSET:
2143                                         hw->io_ports[i] = ctrl;
2144                                         break;
2145 #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
2146                                 case IDE_IRQ_OFFSET:
2147                                         hw->io_ports[i] = intr;
2148                                         break;
2149 #endif
2150                                 default:
2151                                         hw->io_ports[i] = 0;
2152                                         break;
2153                         }
2154                 } else {
2155                         hw->io_ports[i] = base + offsets[i];
2156                 }
2157         }
2158         hw->irq = irq;
2159         hw->dma = NO_DMA;
2160         hw->ack_intr = ack_intr;
2161 }
2162
2163 /*
2164  * Register an IDE interface, specifing exactly the registers etc
2165  * Set init=1 iff calling before probes have taken place.
2166  */
2167 int ide_register_hw(hw_regs_t *hw, struct ata_channel **hwifp)
2168 {
2169         int h;
2170         int retry = 1;
2171         struct ata_channel *hwif;
2172
2173         do {
2174                 for (h = 0; h < MAX_HWIFS; ++h) {
2175                         hwif = &ide_hwifs[h];
2176                         if (hwif->hw.io_ports[IDE_DATA_OFFSET] == hw->io_ports[IDE_DATA_OFFSET])
2177                                 goto found;
2178                 }
2179                 for (h = 0; h < MAX_HWIFS; ++h) {
2180                         hwif = &ide_hwifs[h];
2181                         if ((!hwif->present && (hwif->unit == ATA_PRIMARY) && !initializing) ||
2182                             (!hwif->hw.io_ports[IDE_DATA_OFFSET] && initializing))
2183                                 goto found;
2184                 }
2185                 for (h = 0; h < MAX_HWIFS; ++h)
2186                         ide_unregister(&ide_hwifs[h]);
2187         } while (retry--);
2188
2189         return -1;
2190
2191 found:
2192         ide_unregister(hwif);
2193         if (hwif->present)
2194                 return -1;
2195         memcpy(&hwif->hw, hw, sizeof(*hw));
2196         memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->hw.io_ports));
2197         hwif->irq = hw->irq;
2198         hwif->noprobe = 0;
2199         hwif->chipset = hw->chipset;
2200
2201         if (!initializing) {
2202                 ide_probe_module();
2203 #ifdef CONFIG_PROC_FS
2204                 create_proc_ide_interfaces();
2205 #endif
2206                 ide_driver_module();
2207         }
2208
2209         if (hwifp)
2210                 *hwifp = hwif;
2211
2212         return (initializing || hwif->present) ? h : -1;
2213 }
2214
2215 /*
2216  * Compatability function for existing drivers.  If you want
2217  * something different, use the function above.
2218  */
2219 int ide_register(int arg1, int arg2, int irq)
2220 {
2221         hw_regs_t hw;
2222         ide_init_hwif_ports(&hw, (ide_ioreg_t) arg1, (ide_ioreg_t) arg2, NULL);
2223         hw.irq = irq;
2224         return ide_register_hw(&hw, NULL);
2225 }
2226
2227 void ide_add_setting(ide_drive_t *drive, const char *name, int rw, int read_ioctl, int write_ioctl, int data_type, int min, int max, int mul_factor, int div_factor, void *data, ide_procset_t *set)
2228 {
2229         ide_settings_t **p = &drive->settings;
2230         ide_settings_t *setting = NULL;
2231
2232         while ((*p) && strcmp((*p)->name, name) < 0)
2233                 p = &((*p)->next);
2234         if ((setting = kmalloc(sizeof(*setting), GFP_KERNEL)) == NULL)
2235                 goto abort;
2236         memset(setting, 0, sizeof(*setting));
2237         if ((setting->name = kmalloc(strlen(name) + 1, GFP_KERNEL)) == NULL)
2238                 goto abort;
2239         strcpy(setting->name, name);            setting->rw = rw;
2240         setting->read_ioctl = read_ioctl;       setting->write_ioctl = write_ioctl;
2241         setting->data_type = data_type;         setting->min = min;
2242         setting->max = max;                     setting->mul_factor = mul_factor;
2243         setting->div_factor = div_factor;       setting->data = data;
2244         setting->set = set;                     setting->next = *p;
2245         if (drive->driver)
2246                 setting->auto_remove = 1;
2247         *p = setting;
2248         return;
2249 abort:
2250         if (setting)
2251                 kfree(setting);
2252 }
2253
2254 void ide_remove_setting (ide_drive_t *drive, char *name)
2255 {
2256         ide_settings_t **p = &drive->settings, *setting;
2257
2258         while ((*p) && strcmp((*p)->name, name))
2259                 p = &((*p)->next);
2260         if ((setting = (*p)) == NULL)
2261                 return;
2262         (*p) = setting->next;
2263         kfree(setting->name);
2264         kfree(setting);
2265 }
2266
2267 static void auto_remove_settings (ide_drive_t *drive)
2268 {
2269         ide_settings_t *setting;
2270 repeat:
2271         setting = drive->settings;
2272         while (setting) {
2273                 if (setting->auto_remove) {
2274                         ide_remove_setting(drive, setting->name);
2275                         goto repeat;
2276                 }
2277                 setting = setting->next;
2278         }
2279 }
2280
2281 int ide_read_setting (ide_drive_t *drive, ide_settings_t *setting)
2282 {
2283         int             val = -EINVAL;
2284         unsigned long   flags;
2285
2286         if ((setting->rw & SETTING_READ)) {
2287                 spin_lock_irqsave(&ide_lock, flags);
2288                 switch(setting->data_type) {
2289                         case TYPE_BYTE:
2290                                 val = *((u8 *) setting->data);
2291                                 break;
2292                         case TYPE_SHORT:
2293                                 val = *((u16 *) setting->data);
2294                                 break;
2295                         case TYPE_INT:
2296                         case TYPE_INTA:
2297                                 val = *((u32 *) setting->data);
2298                                 break;
2299                 }
2300                 spin_unlock_irqrestore(&ide_lock, flags);
2301         }
2302         return val;
2303 }
2304
2305 int ide_spin_wait_hwgroup (ide_drive_t *drive)
2306 {
2307         ide_hwgroup_t *hwgroup = HWGROUP(drive);
2308         unsigned long timeout = jiffies + (3 * HZ);
2309
2310         spin_lock_irq(&ide_lock);
2311
2312         while (test_bit(IDE_BUSY, &hwgroup->flags)) {
2313                 unsigned long lflags;
2314                 spin_unlock_irq(&ide_lock);
2315                 __save_flags(lflags);   /* local CPU only */
2316                 __sti();                /* local CPU only; needed for jiffies */
2317                 if (0 < (signed long)(jiffies - timeout)) {
2318                         __restore_flags(lflags);        /* local CPU only */
2319                         printk("%s: channel busy\n", drive->name);
2320                         return -EBUSY;
2321                 }
2322                 __restore_flags(lflags);        /* local CPU only */
2323                 spin_lock_irq(&ide_lock);
2324         }
2325         return 0;
2326 }
2327
2328 /*
2329  * FIXME:  This should be changed to enqueue a special request
2330  * to the driver to change settings, and then wait on a semaphore for completion.
2331  * The current scheme of polling is kludgey, though safe enough.
2332  */
2333 int ide_write_setting (ide_drive_t *drive, ide_settings_t *setting, int val)
2334 {
2335         int i;
2336         u32 *p;
2337
2338         if (!capable(CAP_SYS_ADMIN))
2339                 return -EACCES;
2340         if (!(setting->rw & SETTING_WRITE))
2341                 return -EPERM;
2342         if (val < setting->min || val > setting->max)
2343                 return -EINVAL;
2344         if (setting->set)
2345                 return setting->set(drive, val);
2346         if (ide_spin_wait_hwgroup(drive))
2347                 return -EBUSY;
2348         switch (setting->data_type) {
2349                 case TYPE_BYTE:
2350                         *((u8 *) setting->data) = val;
2351                         break;
2352                 case TYPE_SHORT:
2353                         *((u16 *) setting->data) = val;
2354                         break;
2355                 case TYPE_INT:
2356                         *((u32 *) setting->data) = val;
2357                         break;
2358                 case TYPE_INTA:
2359                         p = (u32 *) setting->data;
2360                         for (i = 0; i < 1 << PARTN_BITS; i++, p++)
2361                                 *p = val;
2362                         break;
2363         }
2364         spin_unlock_irq(&ide_lock);
2365         return 0;
2366 }
2367
2368 static int set_io_32bit(struct ata_device *drive, int arg)
2369 {
2370         if (drive->channel->no_io_32bit)
2371                 return -EIO;
2372
2373         drive->channel->io_32bit = arg;
2374
2375         return 0;
2376 }
2377
2378 static int set_using_dma (ide_drive_t *drive, int arg)
2379 {
2380         if (!drive->driver)
2381                 return -EPERM;
2382         if (!drive->id || !(drive->id->capability & 1) || !drive->channel->dmaproc)
2383                 return -EPERM;
2384         if (drive->channel->dmaproc(arg ? ide_dma_on : ide_dma_off, drive))
2385                 return -EIO;
2386         return 0;
2387 }
2388
2389 static int set_pio_mode (ide_drive_t *drive, int arg)
2390 {
2391         struct request rq;
2392
2393         if (!drive->channel->tuneproc)
2394                 return -ENOSYS;
2395         if (drive->special.b.set_tune)
2396                 return -EBUSY;
2397         ide_init_drive_cmd(&rq);
2398         drive->tune_req = (byte) arg;
2399         drive->special.b.set_tune = 1;
2400         ide_do_drive_cmd(drive, &rq, ide_wait);
2401         return 0;
2402 }
2403
2404 void ide_add_generic_settings (ide_drive_t *drive)
2405 {
2406 /*                      drive   setting name            read/write access                               read ioctl              write ioctl             data type       min     max                             mul_factor      div_factor      data pointer                    set function */
2407         ide_add_setting(drive,  "io_32bit",             drive->channel->no_io_32bit ? SETTING_READ : SETTING_RW,        HDIO_GET_32BIT,         HDIO_SET_32BIT,         TYPE_BYTE,      0,      1 + (SUPPORT_VLB_SYNC << 1),    1,              1,              &drive->channel->io_32bit,              set_io_32bit);
2408         ide_add_setting(drive,  "pio_mode",             SETTING_WRITE,                                  -1,                     HDIO_SET_PIO_MODE,      TYPE_BYTE,      0,      255,                            1,              1,              NULL,                           set_pio_mode);
2409         ide_add_setting(drive,  "slow",                 SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      1,                              1,              1,              &drive->channel->slow,                  NULL);
2410         ide_add_setting(drive,  "unmaskirq",            drive->channel->no_unmask ? SETTING_READ : SETTING_RW,  HDIO_GET_UNMASKINTR,    HDIO_SET_UNMASKINTR,    TYPE_BYTE,      0,      1,                              1,              1,              &drive->channel->unmask,                        NULL);
2411         ide_add_setting(drive,  "using_dma",            SETTING_RW,                                     HDIO_GET_DMA,           HDIO_SET_DMA,           TYPE_BYTE,      0,      1,                              1,              1,              &drive->using_dma,              set_using_dma);
2412         ide_add_setting(drive,  "ide_scsi",             SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      1,                              1,              1,              &drive->scsi,                   NULL);
2413         ide_add_setting(drive,  "init_speed",           SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      69,                             1,              1,              &drive->init_speed,             NULL);
2414         ide_add_setting(drive,  "current_speed",        SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      69,                             1,              1,              &drive->current_speed,          NULL);
2415         ide_add_setting(drive,  "number",               SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      3,                              1,              1,              &drive->dn,                     NULL);
2416 }
2417
2418 /*
2419  * Delay for *at least* 50ms.  As we don't know how much time is left
2420  * until the next tick occurs, we wait an extra tick to be safe.
2421  * This is used only during the probing/polling for drives at boot time.
2422  *
2423  * However, its usefullness may be needed in other places, thus we export it now.
2424  * The future may change this to a millisecond setable delay.
2425  */
2426 void ide_delay_50ms (void)
2427 {
2428 #ifndef CONFIG_BLK_DEV_IDECS
2429         mdelay(50);
2430 #else
2431         __set_current_state(TASK_UNINTERRUPTIBLE);
2432         schedule_timeout(HZ/20);
2433 #endif /* CONFIG_BLK_DEV_IDECS */
2434 }
2435
2436 static int ide_ioctl (struct inode *inode, struct file *file,
2437                         unsigned int cmd, unsigned long arg)
2438 {
2439         int err = 0, major, minor;
2440         ide_drive_t *drive;
2441         struct request rq;
2442         kdev_t dev;
2443         ide_settings_t *setting;
2444
2445         dev = inode->i_rdev;
2446         major = major(dev); minor = minor(dev);
2447         if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
2448                 return -ENODEV;
2449
2450         /* Find setting by ioctl */
2451
2452         setting = drive->settings;
2453
2454         while (setting) {
2455                 if (setting->read_ioctl == cmd || setting->write_ioctl == cmd)
2456                         break;
2457                 setting = setting->next;
2458         }
2459
2460         if (setting != NULL) {
2461                 if (cmd == setting->read_ioctl) {
2462                         err = ide_read_setting(drive, setting);
2463                         return err >= 0 ? put_user(err, (long *) arg) : err;
2464                 } else {
2465                         if ((minor(inode->i_rdev) & PARTN_MASK))
2466                                 return -EINVAL;
2467                         return ide_write_setting(drive, setting, arg);
2468                 }
2469         }
2470
2471         ide_init_drive_cmd (&rq);
2472         switch (cmd) {
2473                 case HDIO_GETGEO:
2474                 {
2475                         struct hd_geometry *loc = (struct hd_geometry *) arg;
2476                         unsigned short bios_cyl = drive->bios_cyl; /* truncate */
2477
2478                         if (!loc || (drive->type != ATA_DISK && drive->type != ATA_FLOPPY))
2479                                 return -EINVAL;
2480                         if (put_user(drive->bios_head, (byte *) &loc->heads)) return -EFAULT;
2481                         if (put_user(drive->bios_sect, (byte *) &loc->sectors)) return -EFAULT;
2482                         if (put_user(bios_cyl, (unsigned short *) &loc->cylinders)) return -EFAULT;
2483                         if (put_user((unsigned)drive->part[minor(inode->i_rdev)&PARTN_MASK].start_sect,
2484                                 (unsigned long *) &loc->start)) return -EFAULT;
2485                         return 0;
2486                 }
2487
2488                 case HDIO_GETGEO_BIG:
2489                 {
2490                         struct hd_big_geometry *loc = (struct hd_big_geometry *) arg;
2491
2492                         if (!loc || (drive->type != ATA_DISK && drive->type != ATA_FLOPPY))
2493                                 return -EINVAL;
2494
2495                         if (put_user(drive->bios_head, (byte *) &loc->heads)) return -EFAULT;
2496                         if (put_user(drive->bios_sect, (byte *) &loc->sectors)) return -EFAULT;
2497                         if (put_user(drive->bios_cyl, (unsigned int *) &loc->cylinders)) return -EFAULT;
2498                         if (put_user((unsigned)drive->part[minor(inode->i_rdev)&PARTN_MASK].start_sect,
2499                                 (unsigned long *) &loc->start)) return -EFAULT;
2500                         return 0;
2501                 }
2502
2503                 case HDIO_GETGEO_BIG_RAW:
2504                 {
2505                         struct hd_big_geometry *loc = (struct hd_big_geometry *) arg;
2506                         if (!loc || (drive->type != ATA_DISK && drive->type != ATA_FLOPPY))
2507                                 return -EINVAL;
2508                         if (put_user(drive->head, (byte *) &loc->heads)) return -EFAULT;
2509                         if (put_user(drive->sect, (byte *) &loc->sectors)) return -EFAULT;
2510                         if (put_user(drive->cyl, (unsigned int *) &loc->cylinders)) return -EFAULT;
2511                         if (put_user((unsigned)drive->part[minor(inode->i_rdev)&PARTN_MASK].start_sect,
2512                                 (unsigned long *) &loc->start)) return -EFAULT;
2513                         return 0;
2514                 }
2515
2516                 case BLKRRPART: /* Re-read partition tables */
2517                         if (!capable(CAP_SYS_ADMIN))
2518                                 return -EACCES;
2519                         return ide_revalidate_disk(inode->i_rdev);
2520
2521                 case HDIO_OBSOLETE_IDENTITY:
2522                 case HDIO_GET_IDENTITY:
2523                         if (minor(inode->i_rdev) & PARTN_MASK)
2524                                 return -EINVAL;
2525                         if (drive->id == NULL)
2526                                 return -ENOMSG;
2527                         if (copy_to_user((char *)arg, (char *)drive->id, (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
2528                                 return -EFAULT;
2529                         return 0;
2530
2531                 case HDIO_GET_NICE:
2532                         return put_user(drive->dsc_overlap      <<      IDE_NICE_DSC_OVERLAP    |
2533                                         drive->atapi_overlap    <<      IDE_NICE_ATAPI_OVERLAP,
2534                                         (long *) arg);
2535
2536                 case HDIO_DRIVE_CMD:
2537                         if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2538                                 return -EACCES;
2539                         return ide_cmd_ioctl(drive, arg);
2540
2541                 case HDIO_DRIVE_TASK:
2542                         if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2543                                 return -EACCES;
2544                         return ide_task_ioctl(drive, arg);
2545
2546                 case HDIO_SET_NICE:
2547                         if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2548                         if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP))))
2549                                 return -EPERM;
2550                         drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
2551                         /* Only CD-ROM's and tapes support DSC overlap. */
2552                         if (drive->dsc_overlap && !(drive->type == ATA_ROM || drive->type == ATA_TAPE)) {
2553                                 drive->dsc_overlap = 0;
2554                                 return -EPERM;
2555                         }
2556                         return 0;
2557                 case BLKGETSIZE:
2558                 case BLKGETSIZE64:
2559                 case BLKROSET:
2560                 case BLKROGET:
2561                 case BLKFLSBUF:
2562                 case BLKSSZGET:
2563                 case BLKPG:
2564                 case BLKELVGET:
2565                 case BLKELVSET:
2566                 case BLKBSZGET:
2567                 case BLKBSZSET:
2568                         return blk_ioctl(inode->i_bdev, cmd, arg);
2569
2570                 /*
2571                  * uniform packet command handling
2572                  */
2573                 case CDROMEJECT:
2574                 case CDROMCLOSETRAY:
2575                         return block_ioctl(inode->i_bdev, cmd, arg);
2576
2577                 case HDIO_GET_BUSSTATE:
2578                         if (!capable(CAP_SYS_ADMIN))
2579                                 return -EACCES;
2580                         if (put_user(drive->channel->bus_state, (long *)arg))
2581                                 return -EFAULT;
2582                         return 0;
2583
2584                 case HDIO_SET_BUSSTATE:
2585                         if (!capable(CAP_SYS_ADMIN))
2586                                 return -EACCES;
2587                         if (drive->channel->busproc)
2588                                 drive->channel->busproc(drive, (int)arg);
2589                         return 0;
2590
2591                 default:
2592                         if (ata_ops(drive) && ata_ops(drive)->ioctl)
2593                                 return ata_ops(drive)->ioctl(drive, inode, file, cmd, arg);
2594                         return -EINVAL;
2595         }
2596 }
2597
2598 static int ide_check_media_change (kdev_t i_rdev)
2599 {
2600         ide_drive_t *drive;
2601         int res = 0; /* not changed */
2602
2603         drive = get_info_ptr(i_rdev);
2604         if (!drive)
2605                 return -ENODEV;
2606
2607         if (ata_ops(drive)) {
2608                 ata_get(ata_ops(drive));
2609                 if (ata_ops(drive)->check_media_change)
2610                         res = ata_ops(drive)->check_media_change(drive);
2611                 else
2612                         res = 1; /* assume it was changed */
2613                 ata_put(ata_ops(drive));
2614         }
2615         return res;
2616 }
2617
2618 void ide_fixstring (byte *s, const int bytecount, const int byteswap)
2619 {
2620         byte *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */
2621
2622         if (byteswap) {
2623                 /* convert from big-endian to host byte order */
2624                 for (p = end ; p != s;) {
2625                         unsigned short *pp = (unsigned short *) (p -= 2);
2626                         *pp = ntohs(*pp);
2627                 }
2628         }
2629
2630         /* strip leading blanks */
2631         while (s != end && *s == ' ')
2632                 ++s;
2633
2634         /* compress internal blanks and strip trailing blanks */
2635         while (s != end && *s) {
2636                 if (*s++ != ' ' || (s != end && *s && *s != ' '))
2637                         *p++ = *(s-1);
2638         }
2639
2640         /* wipe out trailing garbage */
2641         while (p != end)
2642                 *p++ = '\0';
2643 }
2644
2645 /****************************************************************************
2646  * FIXME: rewrite the following crap:
2647  */
2648
2649 /*
2650  * stridx() returns the offset of c within s,
2651  * or -1 if c is '\0' or not found within s.
2652  */
2653 static int __init stridx (const char *s, char c)
2654 {
2655         char *i = strchr(s, c);
2656
2657         return (i && c) ? i - s : -1;
2658 }
2659
2660 /*
2661  * Parsing for ide_setup():
2662  *
2663  * 1. the first char of s must be '='.
2664  * 2. if the remainder matches one of the supplied keywords,
2665  *     the index (1 based) of the keyword is negated and returned.
2666  * 3. if the remainder is a series of no more than max_vals numbers
2667  *     separated by commas, the numbers are saved in vals[] and a
2668  *     count of how many were saved is returned.  Base10 is assumed,
2669  *     and base16 is allowed when prefixed with "0x".
2670  * 4. otherwise, zero is returned.
2671  */
2672 static int __init match_parm (char *s, const char *keywords[], int vals[], int max_vals)
2673 {
2674         static const char *decimal = "0123456789";
2675         static const char *hex = "0123456789abcdef";
2676         int i, n;
2677
2678         if (*s++ == '=') {
2679                 /*
2680                  * Try matching against the supplied keywords,
2681                  * and return -(index+1) if we match one
2682                  */
2683                 if (keywords != NULL) {
2684                         for (i = 0; *keywords != NULL; ++i) {
2685                                 if (!strcmp(s, *keywords++))
2686                                         return -(i+1);
2687                         }
2688                 }
2689                 /*
2690                  * Look for a series of no more than "max_vals"
2691                  * numeric values separated by commas, in base10,
2692                  * or base16 when prefixed with "0x".
2693                  * Return a count of how many were found.
2694                  */
2695                 for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
2696                         vals[n] = i;
2697                         while ((i = stridx(decimal, *++s)) >= 0)
2698                                 vals[n] = (vals[n] * 10) + i;
2699                         if (*s == 'x' && !vals[n]) {
2700                                 while ((i = stridx(hex, *++s)) >= 0)
2701                                         vals[n] = (vals[n] * 0x10) + i;
2702                         }
2703                         if (++n == max_vals)
2704                                 break;
2705                         if (*s == ',' || *s == ';')
2706                                 ++s;
2707                 }
2708                 if (!*s)
2709                         return n;
2710         }
2711         return 0;       /* zero = nothing matched */
2712 }
2713
2714 /*
2715  * This gets called VERY EARLY during initialization, to handle kernel "command
2716  * line" strings beginning with "hdx=" or "ide".It gets called even before the
2717  * actual module gets initialized.
2718  *
2719  * Here is the complete set currently supported comand line options:
2720  *
2721  * "hdx="  is recognized for all "x" from "a" to "h", such as "hdc".
2722  * "idex=" is recognized for all "x" from "0" to "3", such as "ide1".
2723  *
2724  * "hdx=noprobe"        : drive may be present, but do not probe for it
2725  * "hdx=none"           : drive is NOT present, ignore cmos and do not probe
2726  * "hdx=nowerr"         : ignore the WRERR_STAT bit on this drive
2727  * "hdx=cdrom"          : drive is present, and is a cdrom drive
2728  * "hdx=cyl,head,sect"  : disk drive is present, with specified geometry
2729  * "hdx=noremap"        : do not remap 0->1 even though EZD was detected
2730  * "hdx=autotune"       : driver will attempt to tune interface speed
2731  *                              to the fastest PIO mode supported,
2732  *                              if possible for this drive only.
2733  *                              Not fully supported by all chipset types,
2734  *                              and quite likely to cause trouble with
2735  *                              older/odd IDE drives.
2736  *
2737  * "hdx=slow"           : insert a huge pause after each access to the data
2738  *                              port. Should be used only as a last resort.
2739  *
2740  * "hdxlun=xx"          : set the drive last logical unit.
2741  * "hdx=flash"          : allows for more than one ata_flash disk to be
2742  *                              registered. In most cases, only one device
2743  *                              will be present.
2744  * "hdx=scsi"           : the return of the ide-scsi flag, this is useful for
2745  *                              allowwing ide-floppy, ide-tape, and ide-cdrom|writers
2746  *                              to use ide-scsi emulation on a device specific option.
2747  * "idebus=xx"          : inform IDE driver of VESA/PCI bus speed in MHz,
2748  *                              where "xx" is between 20 and 66 inclusive,
2749  *                              used when tuning chipset PIO modes.
2750  *                              For PCI bus, 25 is correct for a P75 system,
2751  *                              30 is correct for P90,P120,P180 systems,
2752  *                              and 33 is used for P100,P133,P166 systems.
2753  *                              If in doubt, use idebus=33 for PCI.
2754  *                              As for VLB, it is safest to not specify it.
2755  *
2756  * "idex=noprobe"       : do not attempt to access/use this interface
2757  * "idex=base"          : probe for an interface at the address specified,
2758  *                              where "base" is usually 0x1f0 or 0x170
2759  *                              and "ctl" is assumed to be "base"+0x206
2760  * "idex=base,ctl"      : specify both base and ctl
2761  * "idex=base,ctl,irq"  : specify base, ctl, and irq number
2762  * "idex=autotune"      : driver will attempt to tune interface speed
2763  *                              to the fastest PIO mode supported,
2764  *                              for all drives on this interface.
2765  *                              Not fully supported by all chipset types,
2766  *                              and quite likely to cause trouble with
2767  *                              older/odd IDE drives.
2768  * "idex=noautotune"    : driver will NOT attempt to tune interface speed
2769  *                              This is the default for most chipsets,
2770  *                              except the cmd640.
2771  * "idex=serialize"     : do not overlap operations on idex and ide(x^1)
2772  * "idex=four"          : four drives on idex and ide(x^1) share same ports
2773  * "idex=reset"         : reset interface before first use
2774  * "idex=dma"           : enable DMA by default on both drives if possible
2775  * "idex=ata66"         : informs the interface that it has an 80c cable
2776  *                              for chipsets that are ATA-66 capable, but
2777  *                              the ablity to bit test for detection is
2778  *                              currently unknown.
2779  * "ide=reverse"        : Formerly called to pci sub-system, but now local.
2780  *
2781  * The following are valid ONLY on ide0, (except dc4030)
2782  * and the defaults for the base,ctl ports must not be altered.
2783  *
2784  * "ide0=dtc2278"       : probe/support DTC2278 interface
2785  * "ide0=ht6560b"       : probe/support HT6560B interface
2786  * "ide0=cmd640_vlb"    : *REQUIRED* for VLB cards with the CMD640 chip
2787  *                        (not for PCI -- automatically detected)
2788  * "ide0=qd65xx"        : probe/support qd65xx interface
2789  * "ide0=ali14xx"       : probe/support ali14xx chipsets (ALI M1439, M1443, M1445)
2790  * "ide0=umc8672"       : probe/support umc8672 chipsets
2791  * "idex=dc4030"        : probe/support Promise DC4030VL interface
2792  * "ide=doubler"        : probe/support IDE doublers on Amiga
2793  */
2794 int __init ide_setup (char *s)
2795 {
2796         int i, vals[3];
2797         struct ata_channel *hwif;
2798         ide_drive_t *drive;
2799         unsigned int hw, unit;
2800         const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
2801         const char max_hwif  = '0' + (MAX_HWIFS - 1);
2802
2803         if (!strncmp(s, "hd=", 3))      /* hd= is for hd.c driver and not us */
2804                 return 0;
2805
2806         if (strncmp(s,"ide",3) &&
2807             strncmp(s,"idebus",6) &&
2808             strncmp(s,"hd",2))          /* hdx= & hdxlun= */
2809                 return 0;
2810
2811         printk("ide_setup: %s", s);
2812         init_ide_data ();
2813
2814 #ifdef CONFIG_BLK_DEV_IDEDOUBLER
2815         if (!strcmp(s, "ide=doubler")) {
2816                 extern int ide_doubler;
2817
2818                 printk(" : Enabled support for IDE doublers\n");
2819                 ide_doubler = 1;
2820
2821                 return 1;
2822         }
2823 #endif
2824
2825         if (!strcmp(s, "ide=nodma")) {
2826                 printk("IDE: Prevented DMA\n");
2827                 noautodma = 1;
2828
2829                 return 1;
2830         }
2831
2832 #ifdef CONFIG_BLK_DEV_IDEPCI
2833         if (!strcmp(s, "ide=reverse")) {
2834                 ide_scan_direction = 1;
2835                 printk(" : Enabled support for IDE inverse scan order.\n");
2836
2837                 return 1;
2838         }
2839 #endif
2840
2841         /*
2842          * Look for drive options:  "hdx="
2843          */
2844         if (!strncmp(s, "hd", 2) && s[2] >= 'a' && s[2] <= max_drive) {
2845                 const char *hd_words[] = {"none", "noprobe", "nowerr", "cdrom",
2846                                 "serialize", "autotune", "noautotune",
2847                                 "slow", "flash", "remap", "noremap", "scsi", NULL};
2848                 unit = s[2] - 'a';
2849                 hw   = unit / MAX_DRIVES;
2850                 unit = unit % MAX_DRIVES;
2851                 hwif = &ide_hwifs[hw];
2852                 drive = &hwif->drives[unit];
2853                 if (!strncmp(s + 4, "ide-", 4)) {
2854                         strncpy(drive->driver_req, s + 4, 9);
2855                         goto done;
2856                 }
2857                 /*
2858                  * Look for last lun option:  "hdxlun="
2859                  */
2860                 if (!strncmp(&s[3], "lun", 3)) {
2861                         if (match_parm(&s[6], NULL, vals, 1) != 1)
2862                                 goto bad_option;
2863                         if (vals[0] >= 0 && vals[0] <= 7) {
2864                                 drive->last_lun = vals[0];
2865                                 drive->forced_lun = 1;
2866                         } else
2867                                 printk(" -- BAD LAST LUN! Expected value from 0 to 7");
2868                         goto done;
2869                 }
2870                 switch (match_parm(&s[3], hd_words, vals, 3)) {
2871                         case -1: /* "none" */
2872                                 drive->nobios = 1;  /* drop into "noprobe" */
2873                         case -2: /* "noprobe" */
2874                                 drive->noprobe = 1;
2875                                 goto done;
2876                         case -3: /* "nowerr" */
2877                                 drive->bad_wstat = BAD_R_STAT;
2878                                 hwif->noprobe = 0;
2879                                 goto done;
2880                         case -4: /* "cdrom" */
2881                                 drive->present = 1;
2882                                 drive->type = ATA_ROM;
2883                                 hwif->noprobe = 0;
2884                                 goto done;
2885                         case -5: /* "serialize" */
2886                                 printk(" -- USE \"ide%d=serialize\" INSTEAD", hw);
2887                                 goto do_serialize;
2888                         case -6: /* "autotune" */
2889                                 drive->autotune = 1;
2890                                 goto done;
2891                         case -7: /* "noautotune" */
2892                                 drive->autotune = 2;
2893                                 goto done;
2894                         case -8: /* "slow" */
2895                                 hwif->slow = 1;
2896                                 goto done;
2897                         case -9: /* "flash" */
2898                                 drive->ata_flash = 1;
2899                                 goto done;
2900                         case -10: /* "remap" */
2901                                 drive->remap_0_to_1 = 1;
2902                                 goto done;
2903                         case -11: /* "noremap" */
2904                                 drive->remap_0_to_1 = 2;
2905                                 goto done;
2906                         case -12: /* "scsi" */
2907 #if defined(CONFIG_BLK_DEV_IDESCSI) && defined(CONFIG_SCSI)
2908                                 drive->scsi = 1;
2909                                 goto done;
2910 #else
2911                                 drive->scsi = 0;
2912                                 goto bad_option;
2913 #endif
2914                         case 3: /* cyl,head,sect */
2915                                 drive->type     = ATA_DISK;
2916                                 drive->cyl      = drive->bios_cyl  = vals[0];
2917                                 drive->head     = drive->bios_head = vals[1];
2918                                 drive->sect     = drive->bios_sect = vals[2];
2919                                 drive->present  = 1;
2920                                 drive->forced_geom = 1;
2921                                 hwif->noprobe = 0;
2922                                 goto done;
2923                         default:
2924                                 goto bad_option;
2925                 }
2926         }
2927
2928         /*
2929          * Look for bus speed option:  "idebus="
2930          */
2931         if (!strncmp(s, "idebus", 6)) {
2932                 if (match_parm(&s[6], NULL, vals, 1) != 1)
2933                         goto bad_option;
2934                 if (vals[0] >= 20 && vals[0] <= 66) {
2935                         idebus_parameter = vals[0];
2936                 } else
2937                         printk(" -- BAD BUS SPEED! Expected value from 20 to 66");
2938                 goto done;
2939         }
2940
2941         /*
2942          * Look for interface options:  "idex="
2943          */
2944         if (!strncmp(s, "ide", 3) && s[3] >= '0' && s[3] <= max_hwif) {
2945                 /*
2946                  * Be VERY CAREFUL changing this: note hardcoded indexes below
2947                  * -8,-9,-10. -11 : are reserved for future idex calls to ease the hardcoding.
2948                  */
2949                 const char *ide_words[] = {
2950                         "noprobe", "serialize", "autotune", "noautotune", "reset", "dma", "ata66",
2951                         "minus8", "minus9", "minus10", "minus11",
2952                         "qd65xx", "ht6560b", "cmd640_vlb", "dtc2278", "umc8672", "ali14xx", "dc4030", NULL };
2953                 hw = s[3] - '0';
2954                 hwif = &ide_hwifs[hw];
2955
2956                 i = match_parm(&s[4], ide_words, vals, 3);
2957
2958                 /*
2959        &n