bc60dfb78c0a9c475d7a40792d28f76f777aeee6
[linux-2.6.git] / drivers / ide / ide.c
1 /*
2  *  linux/drivers/ide/ide.c             Version 7.00beta2       Mar 05 2003
3  *
4  *  Copyright (C) 1994-1998  Linus Torvalds & authors (see below)
5  */
6
7 /*
8  *  Mostly written by Mark Lord  <mlord@pobox.com>
9  *                and Gadi Oxman <gadio@netvision.net.il>
10  *                and Andre Hedrick <andre@linux-ide.org>
11  *
12  *  See linux/MAINTAINERS for address of current maintainer.
13  *
14  * This is the multiple IDE interface driver, as evolved from hd.c.
15  * It supports up to MAX_HWIFS IDE interfaces, on one or more IRQs
16  *   (usually 14 & 15).
17  * There can be up to two drives per interface, as per the ATA-2 spec.
18  *
19  * Primary:    ide0, port 0x1f0; major=3;  hda is minor=0; hdb is minor=64
20  * Secondary:  ide1, port 0x170; major=22; hdc is minor=0; hdd is minor=64
21  * Tertiary:   ide2, port 0x???; major=33; hde is minor=0; hdf is minor=64
22  * Quaternary: ide3, port 0x???; major=34; hdg is minor=0; hdh is minor=64
23  * ...
24  *
25  *  From hd.c:
26  *  |
27  *  | It traverses the request-list, using interrupts to jump between functions.
28  *  | As nearly all functions can be called within interrupts, we may not sleep.
29  *  | Special care is recommended.  Have Fun!
30  *  |
31  *  | modified by Drew Eckhardt to check nr of hd's from the CMOS.
32  *  |
33  *  | Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
34  *  | in the early extended-partition checks and added DM partitions.
35  *  |
36  *  | Early work on error handling by Mika Liljeberg (liljeber@cs.Helsinki.FI).
37  *  |
38  *  | IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
39  *  | and general streamlining by Mark Lord (mlord@pobox.com).
40  *
41  *  October, 1994 -- Complete line-by-line overhaul for linux 1.1.x, by:
42  *
43  *      Mark Lord       (mlord@pobox.com)               (IDE Perf.Pkg)
44  *      Delman Lee      (delman@ieee.org)               ("Mr. atdisk2")
45  *      Scott Snyder    (snyder@fnald0.fnal.gov)        (ATAPI IDE cd-rom)
46  *
47  *  This was a rewrite of just about everything from hd.c, though some original
48  *  code is still sprinkled about.  Think of it as a major evolution, with
49  *  inspiration from lots of linux users, esp.  hamish@zot.apana.org.au
50  *
51  *  Version 1.0 ALPHA   initial code, primary i/f working okay
52  *  Version 1.3 BETA    dual i/f on shared irq tested & working!
53  *  Version 1.4 BETA    added auto probing for irq(s)
54  *  Version 1.5 BETA    added ALPHA (untested) support for IDE cd-roms,
55  *  ...
56  * Version 5.50         allow values as small as 20 for idebus=
57  * Version 5.51         force non io_32bit in drive_cmd_intr()
58  *                      change delay_10ms() to delay_50ms() to fix problems
59  * Version 5.52         fix incorrect invalidation of removable devices
60  *                      add "hdx=slow" command line option
61  * Version 5.60         start to modularize the driver; the disk and ATAPI
62  *                       drivers can be compiled as loadable modules.
63  *                      move IDE probe code to ide-probe.c
64  *                      move IDE disk code to ide-disk.c
65  *                      add support for generic IDE device subdrivers
66  *                      add m68k code from Geert Uytterhoeven
67  *                      probe all interfaces by default
68  *                      add ioctl to (re)probe an interface
69  * Version 6.00         use per device request queues
70  *                      attempt to optimize shared hwgroup performance
71  *                      add ioctl to manually adjust bandwidth algorithms
72  *                      add kerneld support for the probe module
73  *                      fix bug in ide_error()
74  *                      fix bug in the first ide_get_lock() call for Atari
75  *                      don't flush leftover data for ATAPI devices
76  * Version 6.01         clear hwgroup->active while the hwgroup sleeps
77  *                      support HDIO_GETGEO for floppies
78  * Version 6.02         fix ide_ack_intr() call
79  *                      check partition table on floppies
80  * Version 6.03         handle bad status bit sequencing in ide_wait_stat()
81  * Version 6.10         deleted old entries from this list of updates
82  *                      replaced triton.c with ide-dma.c generic PCI DMA
83  *                      added support for BIOS-enabled UltraDMA
84  *                      rename all "promise" things to "pdc4030"
85  *                      fix EZ-DRIVE handling on small disks
86  * Version 6.11         fix probe error in ide_scan_devices()
87  *                      fix ancient "jiffies" polling bugs
88  *                      mask all hwgroup interrupts on each irq entry
89  * Version 6.12         integrate ioctl and proc interfaces
90  *                      fix parsing of "idex=" command line parameter
91  * Version 6.13         add support for ide4/ide5 courtesy rjones@orchestream.com
92  * Version 6.14         fixed IRQ sharing among PCI devices
93  * Version 6.15         added SMP awareness to IDE drivers
94  * Version 6.16         fixed various bugs; even more SMP friendly
95  * Version 6.17         fix for newest EZ-Drive problem
96  * Version 6.18         default unpartitioned-disk translation now "BIOS LBA"
97  * Version 6.19         Re-design for a UNIFORM driver for all platforms,
98  *                        model based on suggestions from Russell King and
99  *                        Geert Uytterhoeven
100  *                      Promise DC4030VL now supported.
101  *                      add support for ide6/ide7
102  *                      delay_50ms() changed to ide_delay_50ms() and exported.
103  * Version 6.20         Added/Fixed Generic ATA-66 support and hwif detection.
104  *                      Added hdx=flash to allow for second flash disk
105  *                        detection w/o the hang loop.
106  *                      Added support for ide8/ide9
107  *                      Added idex=ata66 for the quirky chipsets that are
108  *                        ATA-66 compliant, but have yet to determine a method
109  *                        of verification of the 80c cable presence.
110  *                        Specifically Promise's PDC20262 chipset.
111  * Version 6.21         Fixing/Fixed SMP spinlock issue with insight from an old
112  *                        hat that clarified original low level driver design.
113  * Version 6.30         Added SMP support; fixed multmode issues.  -ml
114  * Version 6.31         Debug Share INTR's and request queue streaming
115  *                      Native ATA-100 support
116  *                      Prep for Cascades Project
117  * Version 7.00alpha    First named revision of ide rearrange
118  *
119  *  Some additional driver compile-time options are in ./include/linux/ide.h
120  *
121  *  To do, in likely order of completion:
122  *      - modify kernel to obtain BIOS geometry for drives on 2nd/3rd/4th i/f
123  *
124  */
125
126 #define REVISION        "Revision: 7.00alpha2"
127 #define VERSION         "Id: ide.c 7.00a2 20020906"
128
129 #undef REALLY_SLOW_IO           /* most systems can safely undef this */
130
131 #define _IDE_C                  /* Tell ide.h it's really us */
132
133 #include <linux/config.h>
134 #include <linux/module.h>
135 #include <linux/types.h>
136 #include <linux/string.h>
137 #include <linux/kernel.h>
138 #include <linux/timer.h>
139 #include <linux/mm.h>
140 #include <linux/interrupt.h>
141 #include <linux/major.h>
142 #include <linux/errno.h>
143 #include <linux/genhd.h>
144 #include <linux/blkpg.h>
145 #include <linux/slab.h>
146 #include <linux/init.h>
147 #include <linux/pci.h>
148 #include <linux/delay.h>
149 #include <linux/ide.h>
150 #include <linux/devfs_fs_kernel.h>
151 #include <linux/completion.h>
152 #include <linux/reboot.h>
153 #include <linux/cdrom.h>
154 #include <linux/seq_file.h>
155 #include <linux/device.h>
156
157 #include <asm/byteorder.h>
158 #include <asm/irq.h>
159 #include <asm/uaccess.h>
160 #include <asm/io.h>
161 #include <asm/bitops.h>
162
163
164 /* default maximum number of failures */
165 #define IDE_DEFAULT_MAX_FAILURES        1
166
167 static const u8 ide_hwif_to_major[] = { IDE0_MAJOR, IDE1_MAJOR,
168                                         IDE2_MAJOR, IDE3_MAJOR,
169                                         IDE4_MAJOR, IDE5_MAJOR,
170                                         IDE6_MAJOR, IDE7_MAJOR,
171                                         IDE8_MAJOR, IDE9_MAJOR };
172
173 static int idebus_parameter;    /* holds the "idebus=" parameter */
174 static int system_bus_speed;    /* holds what we think is VESA/PCI bus speed */
175 static int initializing;        /* set while initializing built-in drivers */
176
177 DECLARE_MUTEX(ide_cfg_sem);
178 spinlock_t ide_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
179
180 #ifdef CONFIG_BLK_DEV_IDEPCI
181 static int ide_scan_direction; /* THIS was formerly 2.2.x pci=reverse */
182 #endif
183
184 #ifdef CONFIG_IDEDMA_AUTO
185 int noautodma = 0;
186 #else
187 int noautodma = 1;
188 #endif
189
190 EXPORT_SYMBOL(noautodma);
191 EXPORT_SYMBOL(ide_bus_type);
192
193 /*
194  * This is declared extern in ide.h, for access by other IDE modules:
195  */
196 ide_hwif_t ide_hwifs[MAX_HWIFS];        /* master data repository */
197
198 EXPORT_SYMBOL(ide_hwifs);
199
200 extern ide_driver_t idedefault_driver;
201 static void setup_driver_defaults(ide_driver_t *driver);
202
203 /*
204  * Do not even *think* about calling this!
205  */
206 static void init_hwif_data(ide_hwif_t *hwif, unsigned int index)
207 {
208         unsigned int unit;
209
210         /* bulk initialize hwif & drive info with zeros */
211         memset(hwif, 0, sizeof(ide_hwif_t));
212
213         /* fill in any non-zero initial values */
214         hwif->index     = index;
215         hwif->major     = ide_hwif_to_major[index];
216
217         hwif->name[0]   = 'i';
218         hwif->name[1]   = 'd';
219         hwif->name[2]   = 'e';
220         hwif->name[3]   = '0' + index;
221
222         hwif->bus_state = BUSSTATE_ON;
223
224         hwif->atapi_dma = 0;            /* disable all atapi dma */ 
225         hwif->ultra_mask = 0x80;        /* disable all ultra */
226         hwif->mwdma_mask = 0x80;        /* disable all mwdma */
227         hwif->swdma_mask = 0x80;        /* disable all swdma */
228
229         sema_init(&hwif->gendev_rel_sem, 0);
230
231         default_hwif_iops(hwif);
232         default_hwif_transport(hwif);
233         for (unit = 0; unit < MAX_DRIVES; ++unit) {
234                 ide_drive_t *drive = &hwif->drives[unit];
235
236                 drive->media                    = ide_disk;
237                 drive->select.all               = (unit<<4)|0xa0;
238                 drive->hwif                     = hwif;
239                 drive->ctl                      = 0x08;
240                 drive->ready_stat               = READY_STAT;
241                 drive->bad_wstat                = BAD_W_STAT;
242                 drive->special.b.recalibrate    = 1;
243                 drive->special.b.set_geometry   = 1;
244                 drive->name[0]                  = 'h';
245                 drive->name[1]                  = 'd';
246                 drive->name[2]                  = 'a' + (index * MAX_DRIVES) + unit;
247                 drive->max_failures             = IDE_DEFAULT_MAX_FAILURES;
248                 drive->using_dma                = 0;
249                 drive->is_flash                 = 0;
250                 drive->driver                   = &idedefault_driver;
251                 drive->vdma                     = 0;
252                 INIT_LIST_HEAD(&drive->list);
253                 sema_init(&drive->gendev_rel_sem, 0);
254         }
255 }
256
257 static void init_hwif_default(ide_hwif_t *hwif, unsigned int index)
258 {
259         hw_regs_t hw;
260
261         memset(&hw, 0, sizeof(hw_regs_t));
262
263         ide_init_hwif_ports(&hw, ide_default_io_base(index), 0, &hwif->irq);
264
265         memcpy(&hwif->hw, &hw, sizeof(hw));
266         memcpy(hwif->io_ports, hw.io_ports, sizeof(hw.io_ports));
267
268         hwif->noprobe = !hwif->io_ports[IDE_DATA_OFFSET];
269 #ifdef CONFIG_BLK_DEV_HD
270         if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA)
271                 hwif->noprobe = 1;      /* may be overridden by ide_setup() */
272 #endif
273 }
274
275 extern void ide_arm_init(void);
276
277 /*
278  * init_ide_data() sets reasonable default values into all fields
279  * of all instances of the hwifs and drives, but only on the first call.
280  * Subsequent calls have no effect (they don't wipe out anything).
281  *
282  * This routine is normally called at driver initialization time,
283  * but may also be called MUCH earlier during kernel "command-line"
284  * parameter processing.  As such, we cannot depend on any other parts
285  * of the kernel (such as memory allocation) to be functioning yet.
286  *
287  * This is too bad, as otherwise we could dynamically allocate the
288  * ide_drive_t structs as needed, rather than always consuming memory
289  * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
290  *
291  * FIXME: We should stuff the setup data into __init and copy the
292  * relevant hwifs/allocate them properly during boot.
293  */
294 #define MAGIC_COOKIE 0x12345678
295 static void __init init_ide_data (void)
296 {
297         ide_hwif_t *hwif;
298         unsigned int index;
299         static unsigned long magic_cookie = MAGIC_COOKIE;
300
301         if (magic_cookie != MAGIC_COOKIE)
302                 return;         /* already initialized */
303         magic_cookie = 0;
304
305         setup_driver_defaults(&idedefault_driver);
306
307         /* Initialise all interface structures */
308         for (index = 0; index < MAX_HWIFS; ++index) {
309                 hwif = &ide_hwifs[index];
310                 init_hwif_data(hwif, index);
311                 init_hwif_default(hwif, index);
312 #if !defined(CONFIG_PPC32) || !defined(CONFIG_PCI)
313                 hwif->irq = hwif->hw.irq =
314                         ide_init_default_irq(hwif->io_ports[IDE_DATA_OFFSET]);
315 #endif
316         }
317 #ifdef CONFIG_IDE_ARM
318         initializing = 1;
319         ide_arm_init();
320         initializing = 0;
321 #endif
322 }
323
324 /*
325  * ide_system_bus_speed() returns what we think is the system VESA/PCI
326  * bus speed (in MHz).  This is used for calculating interface PIO timings.
327  * The default is 40 for known PCI systems, 50 otherwise.
328  * The "idebus=xx" parameter can be used to override this value.
329  * The actual value to be used is computed/displayed the first time through.
330  */
331 int ide_system_bus_speed (void)
332 {
333         if (!system_bus_speed) {
334                 if (idebus_parameter) {
335                         /* user supplied value */
336                         system_bus_speed = idebus_parameter;
337                 } else if (pci_find_device(PCI_ANY_ID, PCI_ANY_ID, NULL) != NULL) {
338                         /* safe default value for PCI */
339                         system_bus_speed = 33;
340                 } else {
341                         /* safe default value for VESA and PCI */
342                         system_bus_speed = 50;
343                 }
344                 printk(KERN_INFO "ide: Assuming %dMHz system bus speed "
345                         "for PIO modes%s\n", system_bus_speed,
346                         idebus_parameter ? "" : "; override with idebus=xx");
347         }
348         return system_bus_speed;
349 }
350
351 /*
352  * current_capacity() returns the capacity (in sectors) of a drive
353  * according to its current geometry/LBA settings.
354  */
355 sector_t current_capacity (ide_drive_t *drive)
356 {
357         if (!drive->present)
358                 return 0;
359         return DRIVER(drive)->capacity(drive);
360 }
361
362 EXPORT_SYMBOL(current_capacity);
363
364 /*
365  * Error reporting, in human readable form (luxurious, but a memory hog).
366  */
367 u8 ide_dump_status (ide_drive_t *drive, const char *msg, u8 stat)
368 {
369         ide_hwif_t *hwif = HWIF(drive);
370         unsigned long flags;
371         u8 err = 0;
372
373         local_irq_set(flags);
374         printk(KERN_WARNING "%s: %s: status=0x%02x", drive->name, msg, stat);
375 #if FANCY_STATUS_DUMPS
376         printk(" { ");
377         if (stat & BUSY_STAT) {
378                 printk("Busy ");
379         } else {
380                 if (stat & READY_STAT)  printk("DriveReady ");
381                 if (stat & WRERR_STAT)  printk("DeviceFault ");
382                 if (stat & SEEK_STAT)   printk("SeekComplete ");
383                 if (stat & DRQ_STAT)    printk("DataRequest ");
384                 if (stat & ECC_STAT)    printk("CorrectedError ");
385                 if (stat & INDEX_STAT)  printk("Index ");
386                 if (stat & ERR_STAT)    printk("Error ");
387         }
388         printk("}");
389 #endif  /* FANCY_STATUS_DUMPS */
390         printk("\n");
391         if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
392                 err = hwif->INB(IDE_ERROR_REG);
393                 printk("%s: %s: error=0x%02x", drive->name, msg, err);
394 #if FANCY_STATUS_DUMPS
395                 if (drive->media == ide_disk) {
396                         printk(" { ");
397                         if (err & ABRT_ERR)     printk("DriveStatusError ");
398                         if (err & ICRC_ERR)     printk("Bad%s ", (err & ABRT_ERR) ? "CRC" : "Sector");
399                         if (err & ECC_ERR)      printk("UncorrectableError ");
400                         if (err & ID_ERR)       printk("SectorIdNotFound ");
401                         if (err & TRK0_ERR)     printk("TrackZeroNotFound ");
402                         if (err & MARK_ERR)     printk("AddrMarkNotFound ");
403                         printk("}");
404                         if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR || (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
405                                 if ((drive->id->command_set_2 & 0x0400) &&
406                                     (drive->id->cfs_enable_2 & 0x0400) &&
407                                     (drive->addressing == 1)) {
408                                         u64 sectors = 0;
409                                         u32 high = 0;
410                                         u32 low = ide_read_24(drive);
411                                         hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
412                                         high = ide_read_24(drive);
413
414                                         sectors = ((u64)high << 24) | low;
415                                         printk(", LBAsect=%llu, high=%d, low=%d",
416                                                (long long) sectors,
417                                                high, low);
418                                 } else {
419                                         u8 cur = hwif->INB(IDE_SELECT_REG);
420                                         if (cur & 0x40) {       /* using LBA? */
421                                                 printk(", LBAsect=%ld", (unsigned long)
422                                                  ((cur&0xf)<<24)
423                                                  |(hwif->INB(IDE_HCYL_REG)<<16)
424                                                  |(hwif->INB(IDE_LCYL_REG)<<8)
425                                                  | hwif->INB(IDE_SECTOR_REG));
426                                         } else {
427                                                 printk(", CHS=%d/%d/%d",
428                                                  (hwif->INB(IDE_HCYL_REG)<<8) +
429                                                   hwif->INB(IDE_LCYL_REG),
430                                                   cur & 0xf,
431                                                   hwif->INB(IDE_SECTOR_REG));
432                                         }
433                                 }
434                                 if (HWGROUP(drive) && HWGROUP(drive)->rq)
435                                         printk(", sector=%llu", (unsigned long long)HWGROUP(drive)->rq->sector);
436                         }
437                 }
438 #endif  /* FANCY_STATUS_DUMPS */
439                 printk("\n");
440         }
441         local_irq_restore(flags);
442         return err;
443 }
444
445 EXPORT_SYMBOL(ide_dump_status);
446
447 static int ide_open (struct inode * inode, struct file * filp)
448 {
449         return -ENXIO;
450 }
451
452 static spinlock_t drives_lock = SPIN_LOCK_UNLOCKED;
453 static spinlock_t drivers_lock = SPIN_LOCK_UNLOCKED;
454 static LIST_HEAD(drivers);
455
456 /* Iterator */
457 static void *m_start(struct seq_file *m, loff_t *pos)
458 {
459         struct list_head *p;
460         loff_t l = *pos;
461         spin_lock(&drivers_lock);
462         list_for_each(p, &drivers)
463                 if (!l--)
464                         return list_entry(p, ide_driver_t, drivers);
465         return NULL;
466 }
467 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
468 {
469         struct list_head *p = ((ide_driver_t *)v)->drivers.next;
470         (*pos)++;
471         return p==&drivers ? NULL : list_entry(p, ide_driver_t, drivers);
472 }
473 static void m_stop(struct seq_file *m, void *v)
474 {
475         spin_unlock(&drivers_lock);
476 }
477 static int show_driver(struct seq_file *m, void *v)
478 {
479         ide_driver_t *driver = v;
480         seq_printf(m, "%s version %s\n", driver->name, driver->version);
481         return 0;
482 }
483 struct seq_operations ide_drivers_op = {
484         .start  = m_start,
485         .next   = m_next,
486         .stop   = m_stop,
487         .show   = show_driver
488 };
489
490 #ifdef CONFIG_PROC_FS
491 struct proc_dir_entry *proc_ide_root;
492
493 ide_proc_entry_t generic_subdriver_entries[] = {
494         { "capacity",   S_IFREG|S_IRUGO,        proc_ide_read_capacity, NULL },
495         { NULL, 0, NULL, NULL }
496 };
497 #endif
498
499 static struct resource* hwif_request_region(ide_hwif_t *hwif,
500                                             unsigned long addr, int num)
501 {
502         struct resource *res = request_region(addr, num, hwif->name);
503
504         if (!res)
505                 printk(KERN_ERR "%s: I/O resource 0x%lX-0x%lX not free.\n",
506                                 hwif->name, addr, addr+num-1);
507         return res;
508 }
509
510 /**
511  *      ide_hwif_request_regions - request resources for IDE
512  *      @hwif: interface to use
513  *
514  *      Requests all the needed resources for an interface.
515  *      Right now core IDE code does this work which is deeply wrong.
516  *      MMIO leaves it to the controller driver,
517  *      PIO will migrate this way over time.
518  */
519 int ide_hwif_request_regions(ide_hwif_t *hwif)
520 {
521         unsigned long addr;
522         unsigned int i;
523
524         if (hwif->mmio == 2)
525                 return 0;
526         BUG_ON(hwif->mmio == 1);
527         addr = hwif->io_ports[IDE_CONTROL_OFFSET];
528         if (addr && !hwif_request_region(hwif, addr, 1))
529                 goto control_region_busy;
530         hwif->straight8 = 0;
531         addr = hwif->io_ports[IDE_DATA_OFFSET];
532         if ((addr | 7) == hwif->io_ports[IDE_STATUS_OFFSET]) {
533                 if (!hwif_request_region(hwif, addr, 8))
534                         goto data_region_busy;
535                 hwif->straight8 = 1;
536                 return 0;
537         }
538         for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++) {
539                 addr = hwif->io_ports[i];
540                 if (!hwif_request_region(hwif, addr, 1)) {
541                         while (--i)
542                                 release_region(addr, 1);
543                         goto data_region_busy;
544                 }
545         }
546         return 0;
547
548 data_region_busy:
549         addr = hwif->io_ports[IDE_CONTROL_OFFSET];
550         if (addr)
551                 release_region(addr, 1);
552 control_region_busy:
553         /* If any errors are return, we drop the hwif interface. */
554         return -EBUSY;
555 }
556
557 EXPORT_SYMBOL(ide_hwif_request_regions);
558
559 /**
560  *      ide_hwif_release_regions - free IDE resources
561  *
562  *      Note that we only release the standard ports,
563  *      and do not even try to handle any extra ports
564  *      allocated for weird IDE interface chipsets.
565  *
566  *      Note also that we don't yet handle mmio resources here. More
567  *      importantly our caller should be doing this so we need to 
568  *      restructure this as a helper function for drivers.
569  */
570 void ide_hwif_release_regions(ide_hwif_t *hwif)
571 {
572         u32 i = 0;
573
574         if (hwif->mmio == 2)
575                 return;
576         if (hwif->io_ports[IDE_CONTROL_OFFSET])
577                 release_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
578         if (hwif->straight8) {
579                 release_region(hwif->io_ports[IDE_DATA_OFFSET], 8);
580                 return;
581         }
582         for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++)
583                 if (hwif->io_ports[i])
584                         release_region(hwif->io_ports[i], 1);
585 }
586
587 EXPORT_SYMBOL(ide_hwif_release_regions);
588
589 /* restore hwif to a sane state */
590 static void ide_hwif_restore(ide_hwif_t *hwif, ide_hwif_t *tmp_hwif)
591 {
592         hwif->hwgroup                   = tmp_hwif->hwgroup;
593
594         hwif->gendev.parent             = tmp_hwif->gendev.parent;
595
596         hwif->proc                      = tmp_hwif->proc;
597
598         hwif->major                     = tmp_hwif->major;
599         hwif->straight8                 = tmp_hwif->straight8;
600         hwif->bus_state                 = tmp_hwif->bus_state;
601
602         hwif->atapi_dma                 = tmp_hwif->atapi_dma;
603         hwif->ultra_mask                = tmp_hwif->ultra_mask;
604         hwif->mwdma_mask                = tmp_hwif->mwdma_mask;
605         hwif->swdma_mask                = tmp_hwif->swdma_mask;
606
607         hwif->chipset                   = tmp_hwif->chipset;
608         hwif->hold                      = tmp_hwif->hold;
609
610 #ifdef CONFIG_BLK_DEV_IDEPCI
611         hwif->pci_dev                   = tmp_hwif->pci_dev;
612         hwif->cds                       = tmp_hwif->cds;
613 #endif
614
615         hwif->identify                  = tmp_hwif->identify;
616         hwif->tuneproc                  = tmp_hwif->tuneproc;
617         hwif->speedproc                 = tmp_hwif->speedproc;
618         hwif->selectproc                = tmp_hwif->selectproc;
619         hwif->reset_poll                = tmp_hwif->reset_poll;
620         hwif->pre_reset                 = tmp_hwif->pre_reset;
621         hwif->resetproc                 = tmp_hwif->resetproc;
622         hwif->intrproc                  = tmp_hwif->intrproc;
623         hwif->maskproc                  = tmp_hwif->maskproc;
624         hwif->quirkproc                 = tmp_hwif->quirkproc;
625         hwif->busproc                   = tmp_hwif->busproc;
626
627         hwif->ata_input_data            = tmp_hwif->ata_input_data;
628         hwif->ata_output_data           = tmp_hwif->ata_output_data;
629         hwif->atapi_input_bytes         = tmp_hwif->atapi_input_bytes;
630         hwif->atapi_output_bytes        = tmp_hwif->atapi_output_bytes;
631
632         hwif->ide_dma_read              = tmp_hwif->ide_dma_read;
633         hwif->ide_dma_write             = tmp_hwif->ide_dma_write;
634         hwif->ide_dma_begin             = tmp_hwif->ide_dma_begin;
635         hwif->ide_dma_end               = tmp_hwif->ide_dma_end;
636         hwif->ide_dma_check             = tmp_hwif->ide_dma_check;
637         hwif->ide_dma_on                = tmp_hwif->ide_dma_on;
638         hwif->ide_dma_off_quietly       = tmp_hwif->ide_dma_off_quietly;
639         hwif->ide_dma_test_irq          = tmp_hwif->ide_dma_test_irq;
640         hwif->ide_dma_host_on           = tmp_hwif->ide_dma_host_on;
641         hwif->ide_dma_host_off          = tmp_hwif->ide_dma_host_off;
642         hwif->ide_dma_verbose           = tmp_hwif->ide_dma_verbose;
643         hwif->ide_dma_lostirq           = tmp_hwif->ide_dma_lostirq;
644         hwif->ide_dma_timeout           = tmp_hwif->ide_dma_timeout;
645
646         hwif->OUTB                      = tmp_hwif->OUTB;
647         hwif->OUTBSYNC                  = tmp_hwif->OUTBSYNC;
648         hwif->OUTW                      = tmp_hwif->OUTW;
649         hwif->OUTL                      = tmp_hwif->OUTL;
650         hwif->OUTSW                     = tmp_hwif->OUTSW;
651         hwif->OUTSL                     = tmp_hwif->OUTSL;
652
653         hwif->INB                       = tmp_hwif->INB;
654         hwif->INW                       = tmp_hwif->INW;
655         hwif->INL                       = tmp_hwif->INL;
656         hwif->INSW                      = tmp_hwif->INSW;
657         hwif->INSL                      = tmp_hwif->INSL;
658
659         hwif->mmio                      = tmp_hwif->mmio;
660         hwif->rqsize                    = tmp_hwif->rqsize;
661         hwif->no_lba48                  = tmp_hwif->no_lba48;
662
663 #ifndef CONFIG_BLK_DEV_IDECS
664         hwif->irq                       = tmp_hwif->irq;
665 #endif
666
667         hwif->dma_base                  = tmp_hwif->dma_base;
668         hwif->dma_master                = tmp_hwif->dma_master;
669         hwif->dma_command               = tmp_hwif->dma_command;
670         hwif->dma_vendor1               = tmp_hwif->dma_vendor1;
671         hwif->dma_status                = tmp_hwif->dma_status;
672         hwif->dma_vendor3               = tmp_hwif->dma_vendor3;
673         hwif->dma_prdtable              = tmp_hwif->dma_prdtable;
674
675         hwif->dma_extra                 = tmp_hwif->dma_extra;
676         hwif->config_data               = tmp_hwif->config_data;
677         hwif->select_data               = tmp_hwif->select_data;
678         hwif->autodma                   = tmp_hwif->autodma;
679         hwif->udma_four                 = tmp_hwif->udma_four;
680         hwif->no_dsc                    = tmp_hwif->no_dsc;
681
682         hwif->hwif_data                 = tmp_hwif->hwif_data;
683 }
684
685 /**
686  *      ide_unregister          -       free an ide interface
687  *      @index: index of interface (will change soon to a pointer)
688  *
689  *      Perform the final unregister of an IDE interface. At the moment
690  *      we don't refcount interfaces so this will also get split up.
691  *
692  *      Locking:
693  *      The caller must not hold the IDE locks
694  *      The drive present/vanishing is not yet properly locked
695  *      Take care with the callbacks. These have been split to avoid
696  *      deadlocking the IDE layer. The shutdown callback is called
697  *      before we take the lock and free resources. It is up to the
698  *      caller to be sure there is no pending I/O here, and that
699  *      the interfce will not be reopened (present/vanishing locking
700  *      isnt yet done btw). After we commit to the final kill we
701  *      call the cleanup callback with the ide locks held.
702  *
703  *      Unregister restores the hwif structures to the default state.
704  *      This is raving bonkers.
705  */
706
707 void ide_unregister(unsigned int index)
708 {
709         ide_drive_t *drive;
710         ide_hwif_t *hwif, *g, *tmp_hwif;
711         ide_hwgroup_t *hwgroup;
712         int irq_count = 0, unit, i;
713
714         BUG_ON(index >= MAX_HWIFS);
715
716         tmp_hwif = kmalloc(sizeof(*tmp_hwif), GFP_KERNEL|__GFP_NOFAIL);
717         if (!tmp_hwif) {
718                 printk(KERN_ERR "%s: unable to allocate memory\n", __FUNCTION__);
719                 return;
720         }
721
722         BUG_ON(in_interrupt());
723         BUG_ON(irqs_disabled());
724         down(&ide_cfg_sem);
725         spin_lock_irq(&ide_lock);
726         hwif = &ide_hwifs[index];
727         if (!hwif->present)
728                 goto abort;
729         for (unit = 0; unit < MAX_DRIVES; ++unit) {
730                 drive = &hwif->drives[unit];
731                 if (!drive->present)
732                         continue;
733                 if (drive->usage || DRIVER(drive)->busy)
734                         goto abort;
735                 drive->dead = 1;
736         }
737         hwif->present = 0;
738
739         spin_unlock_irq(&ide_lock);
740
741         for (unit = 0; unit < MAX_DRIVES; ++unit) {
742                 drive = &hwif->drives[unit];
743                 if (!drive->present)
744                         continue;
745                 DRIVER(drive)->cleanup(drive);
746         }
747
748 #ifdef CONFIG_PROC_FS
749         destroy_proc_ide_drives(hwif);
750 #endif
751
752         hwgroup = hwif->hwgroup;
753         /*
754          * free the irq if we were the only hwif using it
755          */
756         g = hwgroup->hwif;
757         do {
758                 if (g->irq == hwif->irq)
759                         ++irq_count;
760                 g = g->next;
761         } while (g != hwgroup->hwif);
762         if (irq_count == 1)
763                 free_irq(hwif->irq, hwgroup);
764
765         spin_lock_irq(&ide_lock);
766         /*
767          * Note that we only release the standard ports,
768          * and do not even try to handle any extra ports
769          * allocated for weird IDE interface chipsets.
770          */
771         ide_hwif_release_regions(hwif);
772
773         /*
774          * Remove us from the hwgroup, and free
775          * the hwgroup if we were the only member
776          */
777         for (i = 0; i < MAX_DRIVES; ++i) {
778                 drive = &hwif->drives[i];
779                 if (drive->devfs_name[0] != '\0') {
780                         devfs_remove(drive->devfs_name);
781                         drive->devfs_name[0] = '\0';
782                 }
783                 if (!drive->present)
784                         continue;
785                 if (drive == drive->next) {
786                         /* special case: last drive from hwgroup. */
787                         BUG_ON(hwgroup->drive != drive);
788                         hwgroup->drive = NULL;
789                 } else {
790                         ide_drive_t *walk;
791
792                         walk = hwgroup->drive;
793                         while (walk->next != drive)
794                                 walk = walk->next;
795                         walk->next = drive->next;
796                         if (hwgroup->drive == drive) {
797                                 hwgroup->drive = drive->next;
798                                 hwgroup->hwif = HWIF(hwgroup->drive);
799                         }
800                 }
801                 BUG_ON(hwgroup->drive == drive);
802                 if (drive->id != NULL) {
803                         kfree(drive->id);
804                         drive->id = NULL;
805                 }
806                 drive->present = 0;
807                 /* Messed up locking ... */
808                 spin_unlock_irq(&ide_lock);
809                 blk_cleanup_queue(drive->queue);
810                 device_unregister(&drive->gendev);
811                 down(&drive->gendev_rel_sem);
812                 spin_lock_irq(&ide_lock);
813                 drive->queue = NULL;
814         }
815         if (hwif->next == hwif) {
816                 BUG_ON(hwgroup->hwif != hwif);
817                 kfree(hwgroup);
818         } else {
819                 /* There is another interface in hwgroup.
820                  * Unlink us, and set hwgroup->drive and ->hwif to
821                  * something sane.
822                  */
823                 g = hwgroup->hwif;
824                 while (g->next != hwif)
825                         g = g->next;
826                 g->next = hwif->next;
827                 if (hwgroup->hwif == hwif) {
828                         /* Chose a random hwif for hwgroup->hwif.
829                          * It's guaranteed that there are no drives
830                          * left in the hwgroup.
831                          */
832                         BUG_ON(hwgroup->drive != NULL);
833                         hwgroup->hwif = g;
834                 }
835                 BUG_ON(hwgroup->hwif == hwif);
836         }
837
838         /* More messed up locking ... */
839         spin_unlock_irq(&ide_lock);
840         device_unregister(&hwif->gendev);
841         down(&hwif->gendev_rel_sem);
842
843         /*
844          * Remove us from the kernel's knowledge
845          */
846         blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
847         for (i = 0; i < MAX_DRIVES; i++) {
848                 struct gendisk *disk = hwif->drives[i].disk;
849                 hwif->drives[i].disk = NULL;
850                 put_disk(disk);
851         }
852         unregister_blkdev(hwif->major, hwif->name);
853         spin_lock_irq(&ide_lock);
854
855         if (hwif->dma_base) {
856                 (void) ide_release_dma(hwif);
857
858                 hwif->dma_base = 0;
859                 hwif->dma_master = 0;
860                 hwif->dma_command = 0;
861                 hwif->dma_vendor1 = 0;
862                 hwif->dma_status = 0;
863                 hwif->dma_vendor3 = 0;
864                 hwif->dma_prdtable = 0;
865         }
866
867         /* copy original settings */
868         *tmp_hwif = *hwif;
869
870         /* restore hwif data to pristine status */
871         init_hwif_data(hwif, index);
872         init_hwif_default(hwif, index);
873
874         ide_hwif_restore(hwif, tmp_hwif);
875
876 abort:
877         spin_unlock_irq(&ide_lock);
878         up(&ide_cfg_sem);
879
880         kfree(tmp_hwif);
881 }
882
883 EXPORT_SYMBOL(ide_unregister);
884
885
886 /**
887  *      ide_setup_ports         -       set up IDE interface ports
888  *      @hw: register descriptions
889  *      @base: base register
890  *      @offsets: table of register offsets
891  *      @ctrl: control register
892  *      @ack_irq: IRQ ack
893  *      @irq: interrupt lie
894  *
895  *      Setup hw_regs_t structure described by parameters.  You
896  *      may set up the hw structure yourself OR use this routine to
897  *      do it for you. This is basically a helper
898  *
899  */
900  
901 void ide_setup_ports (  hw_regs_t *hw,
902                         unsigned long base, int *offsets,
903                         unsigned long ctrl, unsigned long intr,
904                         ide_ack_intr_t *ack_intr,
905 /*
906  *                      ide_io_ops_t *iops,
907  */
908                         int irq)
909 {
910         int i;
911
912         for (i = 0; i < IDE_NR_PORTS; i++) {
913                 if (offsets[i] == -1) {
914                         switch(i) {
915                                 case IDE_CONTROL_OFFSET:
916                                         hw->io_ports[i] = ctrl;
917                                         break;
918 #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
919                                 case IDE_IRQ_OFFSET:
920                                         hw->io_ports[i] = intr;
921                                         break;
922 #endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
923                                 default:
924                                         hw->io_ports[i] = 0;
925                                         break;
926                         }
927                 } else {
928                         hw->io_ports[i] = base + offsets[i];
929                 }
930         }
931         hw->irq = irq;
932         hw->dma = NO_DMA;
933         hw->ack_intr = ack_intr;
934 /*
935  *      hw->iops = iops;
936  */
937 }
938
939 EXPORT_SYMBOL(ide_setup_ports);
940
941 /*
942  * Register an IDE interface, specifying exactly the registers etc
943  * Set init=1 iff calling before probes have taken place.
944  */
945 int ide_register_hw (hw_regs_t *hw, ide_hwif_t **hwifp)
946 {
947         int index, retry = 1;
948         ide_hwif_t *hwif;
949
950         do {
951                 for (index = 0; index < MAX_HWIFS; ++index) {
952                         hwif = &ide_hwifs[index];
953                         if (hwif->hw.io_ports[IDE_DATA_OFFSET] == hw->io_ports[IDE_DATA_OFFSET])
954                                 goto found;
955                 }
956                 for (index = 0; index < MAX_HWIFS; ++index) {
957                         hwif = &ide_hwifs[index];
958                         if (hwif->hold)
959                                 continue;
960                         if ((!hwif->present && !hwif->mate && !initializing) ||
961                             (!hwif->hw.io_ports[IDE_DATA_OFFSET] && initializing))
962                                 goto found;
963                 }
964                 for (index = 0; index < MAX_HWIFS; index++)
965                         ide_unregister(index);
966         } while (retry--);
967         return -1;
968 found:
969         if (hwif->present)
970                 ide_unregister(index);
971         else if (!hwif->hold) {
972                 init_hwif_data(hwif, index);
973                 init_hwif_default(hwif, index);
974         }
975         if (hwif->present)
976                 return -1;
977         memcpy(&hwif->hw, hw, sizeof(*hw));
978         memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->hw.io_ports));
979         hwif->irq = hw->irq;
980         hwif->noprobe = 0;
981         hwif->chipset = hw->chipset;
982
983         if (!initializing) {
984                 probe_hwif_init(hwif);
985                 create_proc_ide_interfaces();
986         }
987
988         if (hwifp)
989                 *hwifp = hwif;
990
991         return (initializing || hwif->present) ? index : -1;
992 }
993
994 EXPORT_SYMBOL(ide_register_hw);
995
996 /*
997  *      Locks for IDE setting functionality
998  */
999
1000 DECLARE_MUTEX(ide_setting_sem);
1001 EXPORT_SYMBOL(ide_setting_sem);
1002
1003 /**
1004  *      ide_add_setting -       add an ide setting option
1005  *      @drive: drive to use
1006  *      @name: setting name
1007  *      @rw: true if the function is read write
1008  *      @read_ioctl: function to call on read
1009  *      @write_ioctl: function to call on write
1010  *      @data_type: type of data
1011  *      @min: range minimum
1012  *      @max: range maximum
1013  *      @mul_factor: multiplication scale
1014  *      @div_factor: divison scale
1015  *      @data: private data field
1016  *      @set: setting
1017  *
1018  *      Removes the setting named from the device if it is present.
1019  *      The function takes the settings_lock to protect against 
1020  *      parallel changes. This function must not be called from IRQ
1021  *      context. Returns 0 on success or -1 on failure.
1022  *
1023  *      BUGS: This code is seriously over-engineered. There is also
1024  *      magic about how the driver specific features are setup. If
1025  *      a driver is attached we assume the driver settings are auto
1026  *      remove.
1027  */
1028  
1029 int 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)
1030 {
1031         ide_settings_t **p = (ide_settings_t **) &drive->settings, *setting = NULL;
1032
1033         down(&ide_setting_sem);
1034         while ((*p) && strcmp((*p)->name, name) < 0)
1035                 p = &((*p)->next);
1036         if ((setting = kmalloc(sizeof(*setting), GFP_KERNEL)) == NULL)
1037                 goto abort;
1038         memset(setting, 0, sizeof(*setting));
1039         if ((setting->name = kmalloc(strlen(name) + 1, GFP_KERNEL)) == NULL)
1040                 goto abort;
1041         strcpy(setting->name, name);
1042         setting->rw = rw;
1043         setting->read_ioctl = read_ioctl;
1044         setting->write_ioctl = write_ioctl;
1045         setting->data_type = data_type;
1046         setting->min = min;
1047         setting->max = max;
1048         setting->mul_factor = mul_factor;
1049         setting->div_factor = div_factor;
1050         setting->data = data;
1051         setting->set = set;
1052         
1053         setting->next = *p;
1054         if (drive->driver != &idedefault_driver)
1055                 setting->auto_remove = 1;
1056         *p = setting;
1057         up(&ide_setting_sem);
1058         return 0;
1059 abort:
1060         up(&ide_setting_sem);
1061         if (setting)
1062                 kfree(setting);
1063         return -1;
1064 }
1065
1066 EXPORT_SYMBOL(ide_add_setting);
1067
1068 /**
1069  *      __ide_remove_setting    -       remove an ide setting option
1070  *      @drive: drive to use
1071  *      @name: setting name
1072  *
1073  *      Removes the setting named from the device if it is present.
1074  *      The caller must hold the setting semaphore.
1075  */
1076  
1077 static void __ide_remove_setting (ide_drive_t *drive, char *name)
1078 {
1079         ide_settings_t **p, *setting;
1080
1081         p = (ide_settings_t **) &drive->settings;
1082
1083         while ((*p) && strcmp((*p)->name, name))
1084                 p = &((*p)->next);
1085         if ((setting = (*p)) == NULL)
1086                 return;
1087
1088         (*p) = setting->next;
1089         
1090         kfree(setting->name);
1091         kfree(setting);
1092 }
1093
1094 /**
1095  *      ide_remove_setting      -       remove an ide setting option
1096  *      @drive: drive to use
1097  *      @name: setting name
1098  *
1099  *      Removes the setting named from the device if it is present.
1100  *      The function takes the settings_lock to protect against 
1101  *      parallel changes. This function must not be called from IRQ
1102  *      context.
1103  */
1104  
1105 void ide_remove_setting (ide_drive_t *drive, char *name)
1106 {
1107         down(&ide_setting_sem);
1108         __ide_remove_setting(drive, name);
1109         up(&ide_setting_sem);
1110 }
1111
1112 EXPORT_SYMBOL(ide_remove_setting);
1113
1114 /**
1115  *      ide_find_setting_by_ioctl       -       find a drive specific ioctl
1116  *      @drive: drive to scan
1117  *      @cmd: ioctl command to handle
1118  *
1119  *      Scan's the device setting table for a matching entry and returns
1120  *      this or NULL if no entry is found. The caller must hold the
1121  *      setting semaphore
1122  */
1123  
1124 static ide_settings_t *ide_find_setting_by_ioctl (ide_drive_t *drive, int cmd)
1125 {
1126         ide_settings_t *setting = drive->settings;
1127
1128         while (setting) {
1129                 if (setting->read_ioctl == cmd || setting->write_ioctl == cmd)
1130                         break;
1131                 setting = setting->next;
1132         }
1133         
1134         return setting;
1135 }
1136
1137 /**
1138  *      ide_find_setting_by_name        -       find a drive specific setting
1139  *      @drive: drive to scan
1140  *      @name: setting name
1141  *
1142  *      Scan's the device setting table for a matching entry and returns
1143  *      this or NULL if no entry is found. The caller must hold the
1144  *      setting semaphore
1145  */
1146  
1147 ide_settings_t *ide_find_setting_by_name (ide_drive_t *drive, char *name)
1148 {
1149         ide_settings_t *setting = drive->settings;
1150
1151         while (setting) {
1152                 if (strcmp(setting->name, name) == 0)
1153                         break;
1154                 setting = setting->next;
1155         }
1156         return setting;
1157 }
1158
1159 /**
1160  *      auto_remove_settings    -       remove driver specific settings
1161  *      @drive: drive
1162  *
1163  *      Automatically remove all the driver specific settings for this
1164  *      drive. This function may sleep and must not be called from IRQ
1165  *      context. The caller must hold ide_setting_sem.
1166  */
1167  
1168 static void auto_remove_settings (ide_drive_t *drive)
1169 {
1170         ide_settings_t *setting;
1171 repeat:
1172         setting = drive->settings;
1173         while (setting) {
1174                 if (setting->auto_remove) {
1175                         __ide_remove_setting(drive, setting->name);
1176                         goto repeat;
1177                 }
1178                 setting = setting->next;
1179         }
1180 }
1181
1182 /**
1183  *      ide_read_setting        -       read an IDE setting
1184  *      @drive: drive to read from
1185  *      @setting: drive setting
1186  *
1187  *      Read a drive setting and return the value. The caller
1188  *      must hold the ide_setting_sem when making this call.
1189  *
1190  *      BUGS: the data return and error are the same return value
1191  *      so an error -EINVAL and true return of the same value cannot
1192  *      be told apart
1193  */
1194  
1195 int ide_read_setting (ide_drive_t *drive, ide_settings_t *setting)
1196 {
1197         int             val = -EINVAL;
1198         unsigned long   flags;
1199
1200         if ((setting->rw & SETTING_READ)) {
1201                 spin_lock_irqsave(&ide_lock, flags);
1202                 switch(setting->data_type) {
1203                         case TYPE_BYTE:
1204                                 val = *((u8 *) setting->data);
1205                                 break;
1206                         case TYPE_SHORT:
1207                                 val = *((u16 *) setting->data);
1208                                 break;
1209                         case TYPE_INT:
1210                         case TYPE_INTA:
1211                                 val = *((u32 *) setting->data);
1212                                 break;
1213                 }
1214                 spin_unlock_irqrestore(&ide_lock, flags);
1215         }
1216         return val;
1217 }
1218
1219 int ide_spin_wait_hwgroup (ide_drive_t *drive)
1220 {
1221         ide_hwgroup_t *hwgroup = HWGROUP(drive);
1222         unsigned long timeout = jiffies + (3 * HZ);
1223
1224         spin_lock_irq(&ide_lock);
1225
1226         while (hwgroup->busy) {
1227                 unsigned long lflags;
1228                 spin_unlock_irq(&ide_lock);
1229                 local_irq_set(lflags);
1230                 if (time_after(jiffies, timeout)) {
1231                         local_irq_restore(lflags);
1232                         printk(KERN_ERR "%s: channel busy\n", drive->name);
1233                         return -EBUSY;
1234                 }
1235                 local_irq_restore(lflags);
1236                 spin_lock_irq(&ide_lock);
1237         }
1238         return 0;
1239 }
1240
1241 EXPORT_SYMBOL(ide_spin_wait_hwgroup);
1242
1243 /**
1244  *      ide_write_setting       -       read an IDE setting
1245  *      @drive: drive to read from
1246  *      @setting: drive setting
1247  *      @val: value
1248  *
1249  *      Write a drive setting if it is possible. The caller
1250  *      must hold the ide_setting_sem when making this call.
1251  *
1252  *      BUGS: the data return and error are the same return value
1253  *      so an error -EINVAL and true return of the same value cannot
1254  *      be told apart
1255  *
1256  *      FIXME:  This should be changed to enqueue a special request
1257  *      to the driver to change settings, and then wait on a sema for completion.
1258  *      The current scheme of polling is kludgy, though safe enough.
1259  */
1260 int ide_write_setting (ide_drive_t *drive, ide_settings_t *setting, int val)
1261 {
1262         int i;
1263         u32 *p;
1264
1265         if (!capable(CAP_SYS_ADMIN))
1266                 return -EACCES;
1267         if (!(setting->rw & SETTING_WRITE))
1268                 return -EPERM;
1269         if (val < setting->min || val > setting->max)
1270                 return -EINVAL;
1271         if (setting->set)
1272                 return setting->set(drive, val);
1273         if (ide_spin_wait_hwgroup(drive))
1274                 return -EBUSY;
1275         switch (setting->data_type) {
1276                 case TYPE_BYTE:
1277                         *((u8 *) setting->data) = val;
1278                         break;
1279                 case TYPE_SHORT:
1280                         *((u16 *) setting->data) = val;
1281                         break;
1282                 case TYPE_INT:
1283                         *((u32 *) setting->data) = val;
1284                         break;
1285                 case TYPE_INTA:
1286                         p = (u32 *) setting->data;
1287                         for (i = 0; i < 1 << PARTN_BITS; i++, p++)
1288                                 *p = val;
1289                         break;
1290         }
1291         spin_unlock_irq(&ide_lock);
1292         return 0;
1293 }
1294
1295 EXPORT_SYMBOL(ide_write_setting);
1296
1297 static int set_io_32bit(ide_drive_t *drive, int arg)
1298 {
1299         drive->io_32bit = arg;
1300 #ifdef CONFIG_BLK_DEV_DTC2278
1301         if (HWIF(drive)->chipset == ide_dtc2278)
1302                 HWIF(drive)->drives[!drive->select.b.unit].io_32bit = arg;
1303 #endif /* CONFIG_BLK_DEV_DTC2278 */
1304         return 0;
1305 }
1306
1307 static int set_using_dma (ide_drive_t *drive, int arg)
1308 {
1309 #ifdef CONFIG_BLK_DEV_IDEDMA
1310         if (!drive->id || !(drive->id->capability & 1))
1311                 return -EPERM;
1312         if (HWIF(drive)->ide_dma_check == NULL)
1313                 return -EPERM;
1314         if (arg) {
1315                 if (HWIF(drive)->ide_dma_check(drive)) return -EIO;
1316                 if (HWIF(drive)->ide_dma_on(drive)) return -EIO;
1317         } else {
1318                 if (__ide_dma_off(drive))
1319                         return -EIO;
1320         }
1321         return 0;
1322 #else
1323         return -EPERM;
1324 #endif
1325 }
1326
1327 static int set_pio_mode (ide_drive_t *drive, int arg)
1328 {
1329         struct request rq;
1330
1331         if (!HWIF(drive)->tuneproc)
1332                 return -ENOSYS;
1333         if (drive->special.b.set_tune)
1334                 return -EBUSY;
1335         ide_init_drive_cmd(&rq);
1336         drive->tune_req = (u8) arg;
1337         drive->special.b.set_tune = 1;
1338         (void) ide_do_drive_cmd(drive, &rq, ide_wait);
1339         return 0;
1340 }
1341
1342 static int set_xfer_rate (ide_drive_t *drive, int arg)
1343 {
1344         int err = ide_wait_cmd(drive,
1345                         WIN_SETFEATURES, (u8) arg,
1346                         SETFEATURES_XFER, 0, NULL);
1347
1348         if (!err && arg) {
1349                 ide_set_xfer_rate(drive, (u8) arg);
1350                 ide_driveid_update(drive);
1351         }
1352         return err;
1353 }
1354
1355 int ide_atapi_to_scsi (ide_drive_t *drive, int arg)
1356 {
1357         if (drive->media == ide_disk) {
1358                 drive->scsi = 0;
1359                 return 0;
1360         }
1361
1362         if (DRIVER(drive)->cleanup(drive)) {
1363                 drive->scsi = 0;
1364                 return 0;
1365         }
1366
1367         drive->scsi = (u8) arg;
1368         ata_attach(drive);
1369         return 0;
1370 }
1371
1372 void ide_add_generic_settings (ide_drive_t *drive)
1373 {
1374 /*
1375  *                      drive   setting name            read/write access                               read ioctl              write ioctl             data type       min     max                             mul_factor      div_factor      data pointer                    set function
1376  */
1377         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);
1378         ide_add_setting(drive,  "keepsettings",         SETTING_RW,                                     HDIO_GET_KEEPSETTINGS,  HDIO_SET_KEEPSETTINGS,  TYPE_BYTE,      0,      1,                              1,              1,              &drive->keep_settings,          NULL);
1379         ide_add_setting(drive,  "nice1",                SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      1,                              1,              1,              &drive->nice1,                  NULL);
1380         ide_add_setting(drive,  "pio_mode",             SETTING_WRITE,                                  -1,                     HDIO_SET_PIO_MODE,      TYPE_BYTE,      0,      255,                            1,              1,              NULL,                           set_pio_mode);
1381         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);
1382         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);
1383         ide_add_setting(drive,  "init_speed",           SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      70,                             1,              1,              &drive->init_speed,             NULL);
1384         ide_add_setting(drive,  "current_speed",        SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      70,                             1,              1,              &drive->current_speed,          set_xfer_rate);
1385         ide_add_setting(drive,  "number",               SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      3,                              1,              1,              &drive->dn,                     NULL);
1386         if (drive->media != ide_disk)
1387                 ide_add_setting(drive,  "ide-scsi",             SETTING_RW,                                     -1,             HDIO_SET_IDE_SCSI,              TYPE_BYTE,      0,      1,                              1,              1,              &drive->scsi,                   ide_atapi_to_scsi);
1388 }
1389
1390 int system_bus_clock (void)
1391 {
1392         return((int) ((!system_bus_speed) ? ide_system_bus_speed() : system_bus_speed ));
1393 }
1394
1395 EXPORT_SYMBOL(system_bus_clock);
1396
1397 /*
1398  *      Locking is badly broken here - since way back.  That sucker is
1399  * root-only, but that's not an excuse...  The real question is what
1400  * exclusion rules do we want here.
1401  */
1402 int ide_replace_subdriver (ide_drive_t *drive, const char *driver)
1403 {
1404         if (!drive->present || drive->usage || drive->dead)
1405                 goto abort;
1406         if (DRIVER(drive)->cleanup(drive))
1407                 goto abort;
1408         strlcpy(drive->driver_req, driver, sizeof(drive->driver_req));
1409         if (ata_attach(drive)) {
1410                 spin_lock(&drives_lock);
1411                 list_del_init(&drive->list);
1412                 spin_unlock(&drives_lock);
1413                 drive->driver_req[0] = 0;
1414                 ata_attach(drive);
1415         } else {
1416                 drive->driver_req[0] = 0;
1417         }
1418         if (DRIVER(drive)!= &idedefault_driver && !strcmp(DRIVER(drive)->name, driver))
1419                 return 0;
1420 abort:
1421         return 1;
1422 }
1423
1424 EXPORT_SYMBOL(ide_replace_subdriver);
1425
1426 int ata_attach(ide_drive_t *drive)
1427 {
1428         struct list_head *p;
1429         spin_lock(&drivers_lock);
1430         list_for_each(p, &drivers) {
1431                 ide_driver_t *driver = list_entry(p, ide_driver_t, drivers);
1432                 if (!try_module_get(driver->owner))
1433                         continue;
1434                 spin_unlock(&drivers_lock);
1435                 if (driver->attach(drive) == 0) {
1436                         module_put(driver->owner);
1437                         drive->gendev.driver = &driver->gen_driver;
1438                         return 0;
1439                 }
1440                 spin_lock(&drivers_lock);
1441                 module_put(driver->owner);
1442         }
1443         drive->gendev.driver = &idedefault_driver.gen_driver;
1444         spin_unlock(&drivers_lock);
1445         if(idedefault_driver.attach(drive) != 0)
1446                 panic("ide: default attach failed");
1447         return 1;
1448 }
1449
1450 EXPORT_SYMBOL(ata_attach);
1451
1452 static int generic_ide_suspend(struct device *dev, u32 state)
1453 {
1454         ide_drive_t *drive = dev->driver_data;
1455         struct request rq;
1456         struct request_pm_state rqpm;
1457         ide_task_t args;
1458
1459         memset(&rq, 0, sizeof(rq));
1460         memset(&rqpm, 0, sizeof(rqpm));
1461         memset(&args, 0, sizeof(args));
1462         rq.flags = REQ_PM_SUSPEND;
1463         rq.special = &args;
1464         rq.pm = &rqpm;
1465         rqpm.pm_step = ide_pm_state_start_suspend;
1466         rqpm.pm_state = state;
1467
1468         return ide_do_drive_cmd(drive, &rq, ide_wait);
1469 }
1470
1471 static int generic_ide_resume(struct device *dev)
1472 {
1473         ide_drive_t *drive = dev->driver_data;
1474         struct request rq;
1475         struct request_pm_state rqpm;
1476         ide_task_t args;
1477
1478         memset(&rq, 0, sizeof(rq));
1479         memset(&rqpm, 0, sizeof(rqpm));
1480         memset(&args, 0, sizeof(args));
1481         rq.flags = REQ_PM_RESUME;
1482         rq.special = &args;
1483         rq.pm = &rqpm;
1484         rqpm.pm_step = ide_pm_state_start_resume;
1485         rqpm.pm_state = 0;
1486
1487         return ide_do_drive_cmd(drive, &rq, ide_head_wait);
1488 }
1489
1490 int generic_ide_ioctl(struct block_device *bdev, unsigned int cmd,
1491                         unsigned long arg)
1492 {
1493         ide_drive_t *drive = bdev->bd_disk->private_data;
1494         ide_settings_t *setting;
1495         int err = 0;
1496         void __user *p = (void __user *)arg;
1497
1498         down(&ide_setting_sem);
1499         if ((setting = ide_find_setting_by_ioctl(drive, cmd)) != NULL) {
1500                 if (cmd == setting->read_ioctl) {
1501                         err = ide_read_setting(drive, setting);
1502                         up(&ide_setting_sem);
1503                         return err >= 0 ? put_user(err, (long __user *)arg) : err;
1504                 } else {
1505                         if (bdev != bdev->bd_contains)
1506                                 err = -EINVAL;
1507                         else
1508                                 err = ide_write_setting(drive, setting, arg);
1509                         up(&ide_setting_sem);
1510                         return err;
1511                 }
1512         }
1513         up(&ide_setting_sem);
1514
1515         switch (cmd) {
1516                 case HDIO_GETGEO:
1517                 {
1518                         struct hd_geometry geom;
1519                         if (!p || (drive->media != ide_disk && drive->media != ide_floppy)) return -EINVAL;
1520                         geom.heads = drive->bios_head;
1521                         geom.sectors = drive->bios_sect;
1522                         geom.cylinders = (u16)drive->bios_cyl; /* truncate */
1523                         geom.start = get_start_sect(bdev);
1524                         if (copy_to_user(p, &geom, sizeof(struct hd_geometry)))
1525                                 return -EFAULT;
1526                         return 0;
1527                 }
1528
1529                 case HDIO_OBSOLETE_IDENTITY:
1530                 case HDIO_GET_IDENTITY:
1531                         if (bdev != bdev->bd_contains)
1532                                 return -EINVAL;
1533                         if (drive->id_read == 0)
1534                                 return -ENOMSG;
1535                         if (copy_to_user(p, drive->id, (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
1536                                 return -EFAULT;
1537                         return 0;
1538
1539                 case HDIO_GET_NICE:
1540                         return put_user(drive->dsc_overlap      <<      IDE_NICE_DSC_OVERLAP    |
1541                                         drive->atapi_overlap    <<      IDE_NICE_ATAPI_OVERLAP  |
1542                                         drive->nice0            <<      IDE_NICE_0              |
1543                                         drive->nice1            <<      IDE_NICE_1              |
1544                                         drive->nice2            <<      IDE_NICE_2,
1545                                         (long __user *) arg);
1546
1547 #ifdef CONFIG_IDE_TASK_IOCTL
1548                 case HDIO_DRIVE_TASKFILE:
1549                         if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1550                                 return -EACCES;
1551                         switch(drive->media) {
1552                                 case ide_disk:
1553                                         return ide_taskfile_ioctl(drive, cmd, arg);
1554                                 default:
1555                                         return -ENOMSG;
1556                         }
1557 #endif /* CONFIG_IDE_TASK_IOCTL */
1558
1559                 case HDIO_DRIVE_CMD:
1560                         if (!capable(CAP_SYS_RAWIO))
1561                                 return -EACCES;
1562                         return ide_cmd_ioctl(drive, cmd, arg);
1563
1564                 case HDIO_DRIVE_TASK:
1565                         if (!capable(CAP_SYS_RAWIO))
1566                                 return -EACCES;
1567                         return ide_task_ioctl(drive, cmd, arg);
1568
1569                 case HDIO_SCAN_HWIF:
1570                 {
1571                         hw_regs_t hw;
1572                         int args[3];
1573                         if (!capable(CAP_SYS_RAWIO)) return -EACCES;
1574                         if (copy_from_user(args, p, 3 * sizeof(int)))
1575                                 return -EFAULT;
1576                         memset(&hw, 0, sizeof(hw));
1577                         ide_init_hwif_ports(&hw, (unsigned long) args[0],
1578                                             (unsigned long) args[1], NULL);
1579                         hw.irq = args[2];
1580                         if (ide_register_hw(&hw, NULL) == -1)
1581                                 return -EIO;
1582                         return 0;
1583                 }
1584                 case HDIO_UNREGISTER_HWIF:
1585                         if (!capable(CAP_SYS_RAWIO)) return -EACCES;
1586                         /* (arg > MAX_HWIFS) checked in function */
1587                         ide_unregister(arg);
1588                         return 0;
1589                 case HDIO_SET_NICE:
1590                         if (!capable(CAP_SYS_ADMIN)) return -EACCES;
1591                         if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
1592                                 return -EPERM;
1593                         drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
1594                         if (drive->dsc_overlap && !DRIVER(drive)->supports_dsc_overlap) {
1595                                 drive->dsc_overlap = 0;
1596                                 return -EPERM;
1597                         }
1598                         drive->nice1 = (arg >> IDE_NICE_1) & 1;
1599                         return 0;
1600                 case HDIO_DRIVE_RESET:
1601                 {
1602                         unsigned long flags;
1603                         if (!capable(CAP_SYS_ADMIN)) return -EACCES;
1604                         
1605                         /*
1606                          *      Abort the current command on the
1607                          *      group if there is one, taking
1608                          *      care not to allow anything else
1609                          *      to be queued and to die on the
1610                          *      spot if we miss one somehow
1611                          */
1612
1613                         spin_lock_irqsave(&ide_lock, flags);
1614                         
1615                         DRIVER(drive)->abort(drive, "drive reset");
1616                         if(HWGROUP(drive)->handler)
1617                                 BUG();
1618                                 
1619                         /* Ensure nothing gets queued after we
1620                            drop the lock. Reset will clear the busy */
1621                    
1622                         HWGROUP(drive)->busy = 1;
1623                         spin_unlock_irqrestore(&ide_lock, flags);
1624                         (void) ide_do_reset(drive);
1625                         if (drive->suspend_reset) {
1626 /*
1627  *                              APM WAKE UP todo !!
1628  *                              int nogoodpower = 1;
1629  *                              while(nogoodpower) {
1630  *                                      check_power1() or check_power2()
1631  *                                      nogoodpower = 0;
1632  *                              } 
1633  *                              HWIF(drive)->multiproc(drive);
1634  */
1635                                 return ioctl_by_bdev(bdev, BLKRRPART, 0);
1636                         }
1637                         return 0;
1638                 }
1639
1640                 case CDROMEJECT:
1641                 case CDROMCLOSETRAY:
1642                         return scsi_cmd_ioctl(bdev->bd_disk, cmd, p);
1643
1644                 case HDIO_GET_BUSSTATE:
1645                         if (!capable(CAP_SYS_ADMIN))
1646                                 return -EACCES;
1647                         if (put_user(HWIF(drive)->bus_state, (long __user *)arg))
1648                                 return -EFAULT;
1649                         return 0;
1650
1651                 case HDIO_SET_BUSSTATE:
1652                         if (!capable(CAP_SYS_ADMIN))
1653                                 return -EACCES;
1654                         if (HWIF(drive)->busproc)
1655                                 return HWIF(drive)->busproc(drive, (int)arg);
1656                         return -EOPNOTSUPP;
1657                 default:
1658                         return -EINVAL;
1659         }
1660 }
1661
1662 EXPORT_SYMBOL(generic_ide_ioctl);
1663
1664 /*
1665  * stridx() returns the offset of c within s,
1666  * or -1 if c is '\0' or not found within s.
1667  */
1668 static int __init stridx (const char *s, char c)
1669 {
1670         char *i = strchr(s, c);
1671         return (i && c) ? i - s : -1;
1672 }
1673
1674 /*
1675  * match_parm() does parsing for ide_setup():
1676  *
1677  * 1. the first char of s must be '='.
1678  * 2. if the remainder matches one of the supplied keywords,
1679  *     the index (1 based) of the keyword is negated and returned.
1680  * 3. if the remainder is a series of no more than max_vals numbers
1681  *     separated by commas, the numbers are saved in vals[] and a
1682  *     count of how many were saved is returned.  Base10 is assumed,
1683  *     and base16 is allowed when prefixed with "0x".
1684  * 4. otherwise, zero is returned.
1685  */
1686 static int __init match_parm (char *s, const char *keywords[], int vals[], int max_vals)
1687 {
1688         static const char *decimal = "0123456789";
1689         static const char *hex = "0123456789abcdef";
1690         int i, n;
1691
1692         if (*s++ == '=') {
1693                 /*
1694                  * Try matching against the supplied keywords,
1695                  * and return -(index+1) if we match one
1696                  */
1697                 if (keywords != NULL) {
1698                         for (i = 0; *keywords != NULL; ++i) {
1699                                 if (!strcmp(s, *keywords++))
1700                                         return -(i+1);
1701                         }
1702                 }
1703                 /*
1704                  * Look for a series of no more than "max_vals"
1705                  * numeric values separated by commas, in base10,
1706                  * or base16 when prefixed with "0x".
1707                  * Return a count of how many were found.
1708                  */
1709                 for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
1710                         vals[n] = i;
1711                         while ((i = stridx(decimal, *++s)) >= 0)
1712                                 vals[n] = (vals[n] * 10) + i;
1713                         if (*s == 'x' && !vals[n]) {
1714                                 while ((i = stridx(hex, *++s)) >= 0)
1715                                         vals[n] = (vals[n] * 0x10) + i;
1716                         }
1717                         if (++n == max_vals)
1718                                 break;
1719                         if (*s == ',' || *s == ';')
1720                                 ++s;
1721                 }
1722                 if (!*s)
1723                         return n;
1724         }
1725         return 0;       /* zero = nothing matched */
1726 }
1727
1728 #ifdef CONFIG_BLK_DEV_PDC4030
1729 static int __initdata probe_pdc4030;
1730 #endif
1731 #ifdef CONFIG_BLK_DEV_ALI14XX
1732 static int __initdata probe_ali14xx;
1733 extern int ali14xx_init(void);
1734 #endif
1735 #ifdef CONFIG_BLK_DEV_UMC8672
1736 static int __initdata probe_umc8672;
1737 extern int umc8672_init(void);
1738 #endif
1739 #ifdef CONFIG_BLK_DEV_DTC2278
1740 static int __initdata probe_dtc2278;
1741 extern int dtc2278_init(void);
1742 #endif
1743 #ifdef CONFIG_BLK_DEV_HT6560B
1744 static int __initdata probe_ht6560b;
1745 extern int ht6560b_init(void);
1746 #endif
1747 #ifdef CONFIG_BLK_DEV_QD65XX
1748 static int __initdata probe_qd65xx;
1749 extern int qd65xx_init(void);
1750 #endif
1751
1752 static int __initdata is_chipset_set[MAX_HWIFS];
1753
1754 /*
1755  * ide_setup() gets called VERY EARLY during initialization,
1756  * to handle kernel "command line" strings beginning with "hdx=" or "ide".
1757  *
1758  * Remember to update Documentation/ide.txt if you change something here.
1759  */
1760 int __init ide_setup (char *s)
1761 {
1762         int i, vals[3];
1763         ide_hwif_t *hwif;
1764         ide_drive_t *drive;
1765         unsigned int hw, unit;
1766         const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
1767         const char max_hwif  = '0' + (MAX_HWIFS - 1);
1768
1769         
1770         if (strncmp(s,"hd",2) == 0 && s[2] == '=')      /* hd= is for hd.c   */
1771                 return 0;                               /* driver and not us */
1772
1773         if (strncmp(s,"ide",3) && strncmp(s,"idebus",6) && strncmp(s,"hd",2))
1774                 return 0;
1775
1776         printk(KERN_INFO "ide_setup: %s", s);
1777         init_ide_data ();
1778
1779 #ifdef CONFIG_BLK_DEV_IDEDOUBLER
1780         if (!strcmp(s, "ide=doubler")) {
1781                 extern int ide_doubler;
1782
1783                 printk(" : Enabled support for IDE doublers\n");
1784                 ide_doubler = 1;
1785                 return 1;
1786         }
1787 #endif /* CONFIG_BLK_DEV_IDEDOUBLER */
1788
1789         if (!strcmp(s, "ide=nodma")) {
1790                 printk("IDE: Prevented DMA\n");
1791                 noautodma = 1;
1792                 return 1;
1793         }
1794
1795 #ifdef CONFIG_BLK_DEV_IDEPCI
1796         if (!strcmp(s, "ide=reverse")) {
1797                 ide_scan_direction = 1;
1798                 printk(" : Enabled support for IDE inverse scan order.\n");
1799                 return 1;
1800         }
1801 #endif /* CONFIG_BLK_DEV_IDEPCI */
1802
1803         /*
1804          * Look for drive options:  "hdx="
1805          */
1806         if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
1807                 const char *hd_words[] = {
1808                         "none", "noprobe", "nowerr", "cdrom", "serialize",
1809                         "autotune", "noautotune", "minus8", "swapdata", "bswap",
1810                         "minus11", "remap", "remap63", "scsi", NULL };
1811                 unit = s[2] - 'a';
1812                 hw   = unit / MAX_DRIVES;
1813                 unit = unit % MAX_DRIVES;
1814                 hwif = &ide_hwifs[hw];
1815                 drive = &hwif->drives[unit];
1816                 if (strncmp(s + 4, "ide-", 4) == 0) {
1817                         strlcpy(drive->driver_req, s + 4, sizeof(drive->driver_req));
1818                         goto done;
1819                 }
1820                 switch (match_parm(&s[3], hd_words, vals, 3)) {
1821                         case -1: /* "none" */
1822                         case -2: /* "noprobe" */
1823                                 drive->noprobe = 1;
1824                                 goto done;
1825                         case -3: /* "nowerr" */
1826                                 drive->bad_wstat = BAD_R_STAT;
1827                                 hwif->noprobe = 0;
1828                                 goto done;
1829                         case -4: /* "cdrom" */
1830                                 drive->present = 1;
1831                                 drive->media = ide_cdrom;
1832                                 hwif->noprobe = 0;
1833                                 goto done;
1834                         case -5: /* "serialize" */
1835                                 printk(" -- USE \"ide%d=serialize\" INSTEAD", hw);
1836                                 goto do_serialize;
1837                         case -6: /* "autotune" */
1838                                 drive->autotune = IDE_TUNE_AUTO;
1839                                 goto done;
1840                         case -7: /* "noautotune" */
1841                                 drive->autotune = IDE_TUNE_NOAUTO;
1842                                 goto done;
1843                         case -9: /* "swapdata" */
1844                         case -10: /* "bswap" */
1845                                 drive->bswap = 1;
1846                                 goto done;
1847                         case -12: /* "remap" */
1848                                 drive->remap_0_to_1 = 1;
1849                                 goto done;
1850                         case -13: /* "remap63" */
1851                                 drive->sect0 = 63;
1852                                 goto done;
1853                         case -14: /* "scsi" */
1854                                 drive->scsi = 1;
1855                                 goto done;
1856                         case 3: /* cyl,head,sect */
1857                                 drive->media    = ide_disk;
1858                                 drive->cyl      = drive->bios_cyl  = vals[0];
1859                                 drive->head     = drive->bios_head = vals[1];
1860                                 drive->sect     = drive->bios_sect = vals[2];
1861                                 drive->present  = 1;
1862                                 drive->forced_geom = 1;
1863                                 hwif->noprobe = 0;
1864                                 goto done;
1865                         default:
1866                                 goto bad_option;
1867                 }
1868         }
1869
1870         if (s[0] != 'i' || s[1] != 'd' || s[2] != 'e')
1871                 goto bad_option;
1872         /*
1873          * Look for bus speed option:  "idebus="
1874          */
1875         if (s[3] == 'b' && s[4] == 'u' && s[5] == 's') {
1876                 if (match_parm(&s[6], NULL, vals, 1) != 1)
1877                         goto bad_option;
1878                 if (vals[0] >= 20 && vals[0] <= 66) {
1879                         idebus_parameter = vals[0];
1880                 } else
1881                         printk(" -- BAD BUS SPEED! Expected value from 20 to 66");
1882                 goto done;
1883         }
1884         /*
1885          * Look for interface options:  "idex="
1886          */
1887         if (s[3] >= '0' && s[3] <= max_hwif) {
1888                 /*
1889                  * Be VERY CAREFUL changing this: note hardcoded indexes below
1890                  * (-8, -9, -10) are reserved to ease the hardcoding.
1891                  */
1892                 static const char *ide_words[] = {
1893                         "noprobe", "serialize", "autotune", "noautotune", 
1894                         "reset", "dma", "ata66", "minus8", "minus9",
1895                         "minus10", "four", "qd65xx", "ht6560b", "cmd640_vlb",
1896                         "dtc2278", "umc8672", "ali14xx", "dc4030", NULL };
1897                 hw = s[3] - '0';
1898                 hwif = &ide_hwifs[hw];
1899                 i = match_parm(&s[4], ide_words, vals, 3);
1900
1901                 /*
1902                  * Cryptic check to ensure chipset not already set for hwif.
1903                  * Note: we can't depend on hwif->chipset here.
1904                  */
1905                 if ((i >= -18 && i <= -11) || (i > 0 && i <= 3)) {
1906                         /* chipset already specified */
1907                         if (is_chipset_set[hw])
1908                                 goto bad_option;
1909                         if (i > -18 && i <= -11) {
1910                                 /* these drivers are for "ide0=" only */
1911                                 if (hw != 0)
1912                                         goto bad_hwif;
1913                                 /* chipset already specified for 2nd port */
1914                                 if (is_chipset_set[hw+1])
1915                                         goto bad_option;
1916                         }
1917                         is_chipset_set[hw] = 1;
1918                         printk("\n");
1919                 }
1920
1921                 switch (i) {
1922 #ifdef CONFIG_BLK_DEV_PDC4030
1923                         case -18: /* "dc4030" */
1924                                 probe_pdc4030 = 1;
1925                                 goto done;
1926 #endif
1927 #ifdef CONFIG_BLK_DEV_ALI14XX
1928                         case -17: /* "ali14xx" */
1929                                 probe_ali14xx = 1;
1930                                 goto done;
1931 #endif
1932 #ifdef CONFIG_BLK_DEV_UMC8672
1933                         case -16: /* "umc8672" */
1934                                 probe_umc8672 = 1;
1935                                 goto done;
1936 #endif
1937 #ifdef CONFIG_BLK_DEV_DTC2278
1938                         case -15: /* "dtc2278" */
1939                                 probe_dtc2278 = 1;
1940                                 goto done;
1941 #endif
1942 #ifdef CONFIG_BLK_DEV_CMD640
1943                         case -14: /* "cmd640_vlb" */
1944                         {
1945                                 extern int cmd640_vlb; /* flag for cmd640.c */
1946                                 cmd640_vlb = 1;
1947                                 goto done;
1948                         }
1949 #endif
1950 #ifdef CONFIG_BLK_DEV_HT6560B
1951                         case -13: /* "ht6560b" */
1952                                 probe_ht6560b = 1;
1953                                 goto done;
1954 #endif
1955 #ifdef CONFIG_BLK_DEV_QD65XX
1956                         case -12: /* "qd65xx" */
1957                                 probe_qd65xx = 1;
1958                                 goto done;
1959 #endif
1960 #ifdef CONFIG_BLK_DEV_4DRIVES
1961                         case -11: /* "four" drives on one set of ports */
1962                         {
1963                                 ide_hwif_t *mate = &ide_hwifs[hw^1];
1964                                 mate->drives[0].select.all ^= 0x20;
1965                                 mate->drives[1].select.all ^= 0x20;
1966                                 hwif->chipset = mate->chipset = ide_4drives;
1967                                 mate->irq = hwif->irq;
1968                                 memcpy(mate->io_ports, hwif->io_ports, sizeof(hwif->io_ports));
1969                                 goto do_serialize;
1970                         }
1971 #endif /* CONFIG_BLK_DEV_4DRIVES */
1972                         case -10: /* minus10 */
1973                         case -9: /* minus9 */
1974                         case -8: /* minus8 */
1975                                 goto bad_option;
1976                         case -7: /* ata66 */
1977 #ifdef CONFIG_BLK_DEV_IDEPCI
1978                                 hwif->udma_four = 1;
1979                                 goto done;
1980 #else
1981                                 goto bad_hwif;
1982 #endif
1983                         case -6: /* dma */
1984                                 hwif->autodma = 1;
1985                                 goto done;
1986                         case -5: /* "reset" */
1987                                 hwif->reset = 1;
1988                                 goto done;
1989                         case -4: /* "noautotune" */
1990                                 hwif->drives[0].autotune = IDE_TUNE_NOAUTO;
1991                                 hwif->drives[1].autotune = IDE_TUNE_NOAUTO;
1992                                 goto done;
1993                         case -3: /* "autotune" */
1994                                 hwif->drives[0].autotune = IDE_TUNE_AUTO;
1995                                 hwif->drives[1].autotune = IDE_TUNE_AUTO;
1996                                 goto done;
1997                         case -2: /* "serialize" */
1998                         do_serialize:
1999                                 hwif->mate = &ide_hwifs[hw^1];
2000                                 hwif->mate->mate = hwif;
2001                                 hwif->serialized = hwif->mate->serialized = 1;
2002                                 goto done;
2003
2004                         case -1: /* "noprobe" */
2005                                 hwif->noprobe = 1;
2006                                 goto done;
2007
2008                         case 1: /* base */
2009                                 vals[1] = vals[0] + 0x206; /* default ctl */
2010                         case 2: /* base,ctl */
2011                                 vals[2] = 0;    /* default irq = probe for it */
2012                         case 3: /* base,ctl,irq */
2013                                 hwif->hw.irq = vals[2];
2014                                 ide_init_hwif_ports(&hwif->hw, (unsigned long) vals[0], (unsigned long) vals[1], &hwif->irq);
2015                                 memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->io_ports));
2016                                 hwif->irq      = vals[2];
2017                                 hwif->noprobe  = 0;
2018                                 hwif->chipset  = ide_forced;
2019                                 goto done;
2020
2021                         case 0: goto bad_option;
2022                         default:
2023                                 printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
2024                                 return 1;
2025                 }
2026         }
2027 bad_option:
2028         printk(" -- BAD OPTION\n");
2029         return 1;
2030 bad_hwif:
2031         printk("-- NOT SUPPORTED ON ide%d", hw);
2032 done:
2033         printk("\n");
2034         return 1;
2035 }
2036
2037 extern void h8300_ide_init(void);
2038
2039 /*
2040  * probe_for_hwifs() finds/initializes "known" IDE interfaces
2041  */
2042 static void __init probe_for_hwifs (void)
2043 {
2044 #ifdef CONFIG_BLK_DEV_IDEPCI
2045         ide_scan_pcibus(ide_scan_direction);
2046 #endif /* CONFIG_BLK_DEV_IDEPCI */
2047
2048 #ifdef CONFIG_ETRAX_IDE
2049         {
2050                 extern void init_e100_ide(void);
2051                 init_e100_ide();
2052         }
2053 #endif /* CONFIG_ETRAX_IDE */
2054 #ifdef CONFIG_BLK_DEV_CMD640
2055         {
2056                 extern void ide_probe_for_cmd640x(void);
2057                 ide_probe_for_cmd640x();
2058         }
2059 #endif /* CONFIG_BLK_DEV_CMD640 */
2060 #ifdef CONFIG_BLK_DEV_PDC4030
2061         {
2062                 extern int pdc4030_init(void);
2063                 if (probe_pdc4030)
2064                         (void)pdc4030_init();
2065         }
2066 #endif /* CONFIG_BLK_DEV_PDC4030 */
2067 #ifdef CONFIG_BLK_DEV_IDE_PMAC
2068         {
2069                 extern void pmac_ide_probe(void);
2070                 pmac_ide_probe();
2071         }
2072 #endif /* CONFIG_BLK_DEV_IDE_PMAC */
2073 #ifdef CONFIG_BLK_DEV_GAYLE
2074         {
2075                 extern void gayle_init(void);
2076                 gayle_init();
2077         }
2078 #endif /* CONFIG_BLK_DEV_GAYLE */
2079 #ifdef CONFIG_BLK_DEV_FALCON_IDE
2080         {
2081                 extern void falconide_init(void);
2082                 falconide_init();
2083         }
2084 #endif /* CONFIG_BLK_DEV_FALCON_IDE */
2085 #ifdef CONFIG_BLK_DEV_MAC_IDE
2086         {
2087                 extern void macide_init(void);
2088                 macide_init();
2089         }
2090 #endif /* CONFIG_BLK_DEV_MAC_IDE */
2091 #ifdef CONFIG_BLK_DEV_Q40IDE
2092         {
2093                 extern void q40ide_init(void);
2094                 q40ide_init();
2095         }
2096 #endif /* CONFIG_BLK_DEV_Q40IDE */
2097 #ifdef CONFIG_BLK_DEV_BUDDHA
2098         {
2099                 extern void buddha_init(void);
2100                 buddha_init();
2101         }
2102 #endif /* CONFIG_BLK_DEV_BUDDHA */
2103 #if defined(CONFIG_BLK_DEV_IDEPNP) && defined(CONFIG_PNP)
2104         {
2105                 extern void pnpide_init(int enable);
2106                 pnpide_init(1);
2107         }
2108 #endif /* CONFIG_BLK_DEV_IDEPNP */
2109 #ifdef CONFIG_H8300
2110         h8300_ide_init();
2111 #endif
2112 }
2113
2114 /*
2115  *      Actually unregister the subdriver. Called with the
2116  *      request lock dropped.
2117  */
2118  
2119 static int default_cleanup (ide_drive_t *drive)
2120 {
2121         return ide_unregister_subdriver(drive);
2122 }
2123
2124 static ide_startstop_t default_do_request (ide_drive_t *drive, struct request *rq, sector_t block)
2125 {
2126         ide_end_request(drive, 0, 0);
2127         return ide_stopped;
2128 }
2129
2130 static int default_end_request (ide_drive_t *drive, int uptodate, int nr_sects)
2131 {
2132         return ide_end_request(drive, uptodate, nr_sects);
2133 }
2134
2135 static u8 default_sense (ide_drive_t *drive, const char *msg, u8 stat)
2136 {
2137         return ide_dump_status(drive, msg, stat);
2138 }
2139
2140 static ide_startstop_t default_error (ide_drive_t *drive, const char *msg, u8 stat)
2141 {
2142         return ide_error(drive, msg, stat);
2143 }
2144
2145 static void default_pre_reset (ide_drive_t *drive)
2146 {
2147 }
2148
2149 static sector_t default_capacity (ide_drive_t *drive)
2150 {
2151         return 0x7fffffff;
2152 }
2153
2154 static ide_startstop_t default_special (ide_drive_t *drive)
2155 {
2156         special_t *s = &drive->special;
2157
2158         s->all = 0;
2159         drive->mult_req = 0;
2160         return ide_stopped;
2161 }
2162
2163 static int default_attach (ide_drive_t *drive)
2164 {
2165         printk(KERN_ERR "%s: does not support hotswap of device class !\n",
2166                 drive->name);
2167
2168         return 0;
2169 }
2170
2171 static ide_startstop_t default_abort (ide_drive_t *drive, const char *msg)
2172 {
2173         return ide_abort(drive, msg);
2174 }
2175
2176 static ide_startstop_t default_start_power_step(ide_drive_t *drive,
2177                                                 struct request *rq)
2178 {
2179         rq->pm->pm_step = ide_pm_state_completed;
2180         return ide_stopped;
2181 }
2182
2183 static void setup_driver_defaults (ide_driver_t *d)
2184 {
2185         if (d->cleanup == NULL)         d->cleanup = default_cleanup;
2186         if (d->do_request == NULL)      d->do_request = default_do_request;
2187         if (d->end_request == NULL)     d->end_request = default_end_request;
2188         if (d->sense == NULL)           d->sense = default_sense;
2189         if (d->error == NULL)           d->error = default_error;
2190         if (d->abort == NULL)           d->abort = default_abort;
2191         if (d->pre_reset == NULL)       d->pre_reset = default_pre_reset;
2192         if (d->capacity == NULL)        d->capacity = default_capacity;
2193         if (d->special == NULL)         d->special = default_special;
2194         if (d->attach == NULL)          d->attach = default_attach;
2195         if (d->start_power_step == NULL)
2196                 d->start_power_step = default_start_power_step;
2197 }
2198
2199 int ide_register_subdriver(ide_drive_t *drive, ide_driver_t *driver)
2200 {
2201         unsigned long flags;
2202
2203         BUG_ON(!drive->driver);
2204
2205         spin_lock_irqsave(&ide_lock, flags);
2206         if (!drive->present || drive->driver != &idedefault_driver ||
2207             drive->usage || drive->dead) {
2208                 spin_unlock_irqrestore(&ide_lock, flags);
2209                 return 1;
2210         }
2211         drive->driver = driver;
2212         spin_unlock_irqrestore(&ide_lock, flags);
2213         spin_lock(&drives_lock);
2214         list_add_tail(&drive->list, &driver->drives);
2215         spin_unlock(&drives_lock);
2216 //      printk(KERN_INFO "%s: attached %s driver.\n", drive->name, driver->name);
2217         if ((drive->autotune == IDE_TUNE_DEFAULT) ||
2218                 (drive->autotune == IDE_TUNE_AUTO)) {
2219                 /* DMA timings and setup moved to ide-probe.c */
2220                 drive->dsc_overlap = (drive->next != drive && driver->supports_dsc_overlap);
2221                 drive->nice1 = 1;
2222         }
2223         drive->suspend_reset = 0;
2224 #ifdef CONFIG_PROC_FS
2225         if (drive->driver != &idedefault_driver) {
2226                 ide_add_proc_entries(drive->proc, generic_subdriver_entries, drive);
2227                 ide_add_proc_entries(drive->proc, driver->proc, drive);
2228         }
2229 #endif
2230         return 0;
2231 }
2232
2233 EXPORT_SYMBOL(ide_register_subdriver);
2234
2235 int ide_unregister_subdriver (ide_drive_t *drive)
2236 {
2237         unsigned long flags;
2238         
2239         down(&ide_setting_sem);
2240         spin_lock_irqsave(&ide_lock, flags);
2241         if (drive->usage || drive->driver == &idedefault_driver || DRIVER(drive)->busy) {
2242                 spin_unlock_irqrestore(&ide_lock, flags);
2243                 up(&ide_setting_sem);
2244                 return 1;
2245         }
2246 #if defined(CONFIG_BLK_DEV_IDEPNP) && defined(CONFIG_PNP) && defined(MODULE)
2247         pnpide_init(0);
2248 #endif /* CONFIG_BLK_DEV_IDEPNP */
2249 #ifdef CONFIG_PROC_FS
2250         ide_remove_proc_entries(drive->proc, DRIVER(drive)->proc);
2251         ide_remove_proc_entries(drive->proc, generic_subdriver_entries);
2252 #endif
2253         auto_remove_settings(drive);
2254         drive->driver = &idedefault_driver;
2255         spin_unlock_irqrestore(&ide_lock, flags);
2256         up(&ide_setting_sem);
2257         spin_lock(&drives_lock);
2258         list_del_init(&drive->list);
2259         spin_unlock(&drives_lock);
2260         /* drive will be added to &idedefault_driver->drives in ata_attach() */
2261         return 0;
2262 }
2263
2264 EXPORT_SYMBOL(ide_unregister_subdriver);
2265
2266 static int ide_drive_remove(struct device * dev)
2267 {
2268         ide_drive_t * drive = container_of(dev,ide_drive_t,gendev);
2269         DRIVER(drive)->cleanup(drive);
2270         return 0;
2271 }
2272
2273 int ide_register_driver(ide_driver_t *driver)
2274 {
2275         struct list_head list;
2276         struct list_head *list_loop;
2277         struct list_head *tmp_storage;
2278
2279         setup_driver_defaults(driver);
2280
2281         spin_lock(&drivers_lock);
2282         list_add(&driver->drivers, &drivers);
2283         spin_unlock(&drivers_lock);
2284
2285         INIT_LIST_HEAD(&list);
2286         spin_lock(&drives_lock);
2287         list_splice_init(&idedefault_driver.drives, &list);
2288         spin_unlock(&drives_lock);
2289
2290         list_for_each_safe(list_loop, tmp_storage, &list) {
2291                 ide_drive_t *drive = container_of(list_loop, ide_drive_t, list);
2292                 list_del_init(&drive->list);
2293                 if (drive->present)
2294                         ata_attach(drive);
2295         }
2296         driver->gen_driver.name = (char *) driver->name;
2297         driver->gen_driver.bus = &ide_bus_type;
2298         driver->gen_driver.remove = ide_drive_remove;
2299         return driver_register(&driver->gen_driver);
2300 }
2301
2302 EXPORT_SYMBOL(ide_register_driver);
2303
2304 void ide_unregister_driver(ide_driver_t *driver)
2305 {
2306         ide_drive_t *drive;
2307
2308         spin_lock(&drivers_lock);
2309         list_del(&driver->drivers);
2310         spin_unlock(&drivers_lock);
2311
2312         driver_unregister(&driver->gen_driver);
2313
2314         while(!list_empty(&driver->drives)) {
2315                 drive = list_entry(driver->drives.next, ide_drive_t, list);
2316                 if (driver->cleanup(drive)) {
2317                         printk(KERN_ERR "%s: cleanup_module() called while still busy\n", drive->name);
2318                         BUG();
2319                 }
2320                 ata_attach(drive);
2321         }
2322 }
2323
2324 EXPORT_SYMBOL(ide_unregister_driver);
2325
2326 struct block_device_operations ide_fops[] = {{
2327         .owner          = THIS_MODULE,
2328         .open           = ide_open,
2329 }};
2330
2331 EXPORT_SYMBOL(ide_fops);
2332
2333 /*
2334  * Probe module
2335  */
2336
2337 EXPORT_SYMBOL(ide_lock);
2338
2339 struct bus_type ide_bus_type = {
2340         .name           = "ide",
2341         .suspend        = generic_ide_suspend,
2342         .resume         = generic_ide_resume,
2343 };
2344
2345 /*
2346  * This is gets invoked once during initialization, to set *everything* up
2347  */
2348 int __init ide_init (void)
2349 {
2350         printk(KERN_INFO "Uniform Multi-Platform E-IDE driver " REVISION "\n");
2351         devfs_mk_dir("ide");
2352         system_bus_speed = ide_system_bus_speed();
2353
2354         bus_register(&ide_bus_type);
2355
2356         init_ide_data();
2357
2358 #ifdef CONFIG_PROC_FS
2359         proc_ide_root = proc_mkdir("ide", 0);
2360 #endif
2361
2362 #ifdef CONFIG_BLK_DEV_ALI14XX
2363         if (probe_ali14xx)
2364                 (void)ali14xx_init();
2365 #endif
2366 #ifdef CONFIG_BLK_DEV_UMC8672
2367         if (probe_umc8672)
2368                 (void)umc8672_init();
2369 #endif
2370 #ifdef CONFIG_BLK_DEV_DTC2278
2371         if (probe_dtc2278)
2372                 (void)dtc2278_init();
2373 #endif
2374 #ifdef CONFIG_BLK_DEV_HT6560B
2375         if (probe_ht6560b)
2376                 (void)ht6560b_init();
2377 #endif
2378 #ifdef CONFIG_BLK_DEV_QD65XX
2379         if (probe_qd65xx)
2380                 (void)qd65xx_init();
2381 #endif
2382
2383         initializing = 1;
2384         /* Probe for special PCI and other "known" interface chipsets. */
2385         probe_for_hwifs();
2386         initializing = 0;
2387
2388 #ifdef CONFIG_PROC_FS
2389         proc_ide_create();
2390 #endif
2391         return 0;
2392 }
2393
2394 #ifdef MODULE
2395 char *options = NULL;
2396 MODULE_PARM(options,"s");
2397 MODULE_LICENSE("GPL");
2398
2399 static void __init parse_options (char *line)
2400 {
2401         char *next = line;
2402
2403         if (line == NULL || !*line)
2404                 return;
2405         while ((line = next) != NULL) {
2406                 if ((next = strchr(line,' ')) != NULL)
2407                         *next++ = 0;
2408                 if (!ide_setup(line))
2409                         printk (KERN_INFO "Unknown option '%s'\n", line);
2410         }
2411 }
2412
2413 int init_module (void)
2414 {
2415         parse_options(options);
2416         return ide_init();
2417 }
2418
2419 void cleanup_module (void)
2420 {
2421         int index;
2422
2423         for (index = 0; index < MAX_HWIFS; ++index) {
2424                 ide_unregister(index);
2425                 if (ide_hwifs[index].dma_base)
2426                         (void) ide_release_dma(&ide_hwifs[index]);
2427         }
2428
2429 #ifdef CONFIG_PROC_FS
2430         proc_ide_destroy();
2431 #endif
2432         devfs_remove("ide");
2433
2434         bus_unregister(&ide_bus_type);
2435 }
2436
2437 #else /* !MODULE */
2438
2439 __setup("", ide_setup);
2440
2441 module_init(ide_init);
2442
2443 #endif /* MODULE */