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