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