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