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