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