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