bsg: add a request_queue argument to scsi_cmd_ioctl()
[linux-2.6] / 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 #define _IDE_C                  /* Tell ide.h it's really us */
130
131 #include <linux/module.h>
132 #include <linux/types.h>
133 #include <linux/string.h>
134 #include <linux/kernel.h>
135 #include <linux/timer.h>
136 #include <linux/mm.h>
137 #include <linux/interrupt.h>
138 #include <linux/major.h>
139 #include <linux/errno.h>
140 #include <linux/genhd.h>
141 #include <linux/blkpg.h>
142 #include <linux/slab.h>
143 #include <linux/init.h>
144 #include <linux/pci.h>
145 #include <linux/delay.h>
146 #include <linux/ide.h>
147 #include <linux/completion.h>
148 #include <linux/reboot.h>
149 #include <linux/cdrom.h>
150 #include <linux/seq_file.h>
151 #include <linux/device.h>
152 #include <linux/bitops.h>
153
154 #include <asm/byteorder.h>
155 #include <asm/irq.h>
156 #include <asm/uaccess.h>
157 #include <asm/io.h>
158
159
160 /* default maximum number of failures */
161 #define IDE_DEFAULT_MAX_FAILURES        1
162
163 static const u8 ide_hwif_to_major[] = { IDE0_MAJOR, IDE1_MAJOR,
164                                         IDE2_MAJOR, IDE3_MAJOR,
165                                         IDE4_MAJOR, IDE5_MAJOR,
166                                         IDE6_MAJOR, IDE7_MAJOR,
167                                         IDE8_MAJOR, IDE9_MAJOR };
168
169 static int idebus_parameter;    /* holds the "idebus=" parameter */
170 static int system_bus_speed;    /* holds what we think is VESA/PCI bus speed */
171
172 DEFINE_MUTEX(ide_cfg_mtx);
173  __cacheline_aligned_in_smp DEFINE_SPINLOCK(ide_lock);
174
175 #ifdef CONFIG_IDEPCI_PCIBUS_ORDER
176 static int ide_scan_direction; /* THIS was formerly 2.2.x pci=reverse */
177 #endif
178
179 int noautodma = 0;
180
181 EXPORT_SYMBOL(noautodma);
182
183 #ifdef CONFIG_BLK_DEV_IDEACPI
184 int ide_noacpi = 0;
185 int ide_noacpitfs = 1;
186 int ide_noacpionboot = 1;
187 #endif
188
189 /*
190  * This is declared extern in ide.h, for access by other IDE modules:
191  */
192 ide_hwif_t ide_hwifs[MAX_HWIFS];        /* master data repository */
193
194 EXPORT_SYMBOL(ide_hwifs);
195
196 /*
197  * Do not even *think* about calling this!
198  */
199 static void init_hwif_data(ide_hwif_t *hwif, unsigned int index)
200 {
201         unsigned int unit;
202
203         /* bulk initialize hwif & drive info with zeros */
204         memset(hwif, 0, sizeof(ide_hwif_t));
205
206         /* fill in any non-zero initial values */
207         hwif->index     = index;
208         hwif->major     = ide_hwif_to_major[index];
209
210         hwif->name[0]   = 'i';
211         hwif->name[1]   = 'd';
212         hwif->name[2]   = 'e';
213         hwif->name[3]   = '0' + index;
214
215         hwif->bus_state = BUSSTATE_ON;
216
217         hwif->atapi_dma = 0;            /* disable all atapi dma */ 
218
219         init_completion(&hwif->gendev_rel_comp);
220
221         default_hwif_iops(hwif);
222         default_hwif_transport(hwif);
223         for (unit = 0; unit < MAX_DRIVES; ++unit) {
224                 ide_drive_t *drive = &hwif->drives[unit];
225
226                 drive->media                    = ide_disk;
227                 drive->select.all               = (unit<<4)|0xa0;
228                 drive->hwif                     = hwif;
229                 drive->ctl                      = 0x08;
230                 drive->ready_stat               = READY_STAT;
231                 drive->bad_wstat                = BAD_W_STAT;
232                 drive->special.b.recalibrate    = 1;
233                 drive->special.b.set_geometry   = 1;
234                 drive->name[0]                  = 'h';
235                 drive->name[1]                  = 'd';
236                 drive->name[2]                  = 'a' + (index * MAX_DRIVES) + unit;
237                 drive->max_failures             = IDE_DEFAULT_MAX_FAILURES;
238                 drive->using_dma                = 0;
239                 drive->vdma                     = 0;
240                 INIT_LIST_HEAD(&drive->list);
241                 init_completion(&drive->gendev_rel_comp);
242         }
243 }
244
245 static void init_hwif_default(ide_hwif_t *hwif, unsigned int index)
246 {
247         hw_regs_t hw;
248
249         memset(&hw, 0, sizeof(hw_regs_t));
250
251         ide_init_hwif_ports(&hw, ide_default_io_base(index), 0, &hwif->irq);
252
253         memcpy(&hwif->hw, &hw, sizeof(hw));
254         memcpy(hwif->io_ports, hw.io_ports, sizeof(hw.io_ports));
255
256         hwif->noprobe = !hwif->io_ports[IDE_DATA_OFFSET];
257 #ifdef CONFIG_BLK_DEV_HD
258         if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA)
259                 hwif->noprobe = 1;      /* may be overridden by ide_setup() */
260 #endif
261 }
262
263 extern void ide_arm_init(void);
264
265 /*
266  * init_ide_data() sets reasonable default values into all fields
267  * of all instances of the hwifs and drives, but only on the first call.
268  * Subsequent calls have no effect (they don't wipe out anything).
269  *
270  * This routine is normally called at driver initialization time,
271  * but may also be called MUCH earlier during kernel "command-line"
272  * parameter processing.  As such, we cannot depend on any other parts
273  * of the kernel (such as memory allocation) to be functioning yet.
274  *
275  * This is too bad, as otherwise we could dynamically allocate the
276  * ide_drive_t structs as needed, rather than always consuming memory
277  * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
278  *
279  * FIXME: We should stuff the setup data into __init and copy the
280  * relevant hwifs/allocate them properly during boot.
281  */
282 #define MAGIC_COOKIE 0x12345678
283 static void __init init_ide_data (void)
284 {
285         ide_hwif_t *hwif;
286         unsigned int index;
287         static unsigned long magic_cookie = MAGIC_COOKIE;
288
289         if (magic_cookie != MAGIC_COOKIE)
290                 return;         /* already initialized */
291         magic_cookie = 0;
292
293         /* Initialise all interface structures */
294         for (index = 0; index < MAX_HWIFS; ++index) {
295                 hwif = &ide_hwifs[index];
296                 init_hwif_data(hwif, index);
297                 init_hwif_default(hwif, index);
298 #if !defined(CONFIG_PPC32) || !defined(CONFIG_PCI)
299                 hwif->irq = hwif->hw.irq =
300                         ide_init_default_irq(hwif->io_ports[IDE_DATA_OFFSET]);
301 #endif
302         }
303 #ifdef CONFIG_IDE_ARM
304         ide_arm_init();
305 #endif
306 }
307
308 /**
309  *      ide_system_bus_speed    -       guess bus speed
310  *
311  *      ide_system_bus_speed() returns what we think is the system VESA/PCI
312  *      bus speed (in MHz). This is used for calculating interface PIO timings.
313  *      The default is 40 for known PCI systems, 50 otherwise.
314  *      The "idebus=xx" parameter can be used to override this value.
315  *      The actual value to be used is computed/displayed the first time
316  *      through. Drivers should only use this as a last resort.
317  *
318  *      Returns a guessed speed in MHz.
319  */
320
321 static int ide_system_bus_speed(void)
322 {
323 #ifdef CONFIG_PCI
324         static struct pci_device_id pci_default[] = {
325                 { PCI_DEVICE(PCI_ANY_ID, PCI_ANY_ID) },
326                 { }
327         };
328 #else
329 #define pci_default 0
330 #endif /* CONFIG_PCI */
331
332         if (!system_bus_speed) {
333                 if (idebus_parameter) {
334                         /* user supplied value */
335                         system_bus_speed = idebus_parameter;
336                 } else if (pci_dev_present(pci_default)) {
337                         /* safe default value for PCI */
338                         system_bus_speed = 33;
339                 } else {
340                         /* safe default value for VESA and PCI */
341                         system_bus_speed = 50;
342                 }
343                 printk(KERN_INFO "ide: Assuming %dMHz system bus speed "
344                         "for PIO modes%s\n", system_bus_speed,
345                         idebus_parameter ? "" : "; override with idebus=xx");
346         }
347         return system_bus_speed;
348 }
349
350 static struct resource* hwif_request_region(ide_hwif_t *hwif,
351                                             unsigned long addr, int num)
352 {
353         struct resource *res = request_region(addr, num, hwif->name);
354
355         if (!res)
356                 printk(KERN_ERR "%s: I/O resource 0x%lX-0x%lX not free.\n",
357                                 hwif->name, addr, addr+num-1);
358         return res;
359 }
360
361 /**
362  *      ide_hwif_request_regions - request resources for IDE
363  *      @hwif: interface to use
364  *
365  *      Requests all the needed resources for an interface.
366  *      Right now core IDE code does this work which is deeply wrong.
367  *      MMIO leaves it to the controller driver,
368  *      PIO will migrate this way over time.
369  */
370
371 int ide_hwif_request_regions(ide_hwif_t *hwif)
372 {
373         unsigned long addr;
374         unsigned int i;
375
376         if (hwif->mmio)
377                 return 0;
378         addr = hwif->io_ports[IDE_CONTROL_OFFSET];
379         if (addr && !hwif_request_region(hwif, addr, 1))
380                 goto control_region_busy;
381         hwif->straight8 = 0;
382         addr = hwif->io_ports[IDE_DATA_OFFSET];
383         if ((addr | 7) == hwif->io_ports[IDE_STATUS_OFFSET]) {
384                 if (!hwif_request_region(hwif, addr, 8))
385                         goto data_region_busy;
386                 hwif->straight8 = 1;
387                 return 0;
388         }
389         for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++) {
390                 addr = hwif->io_ports[i];
391                 if (!hwif_request_region(hwif, addr, 1)) {
392                         while (--i)
393                                 release_region(addr, 1);
394                         goto data_region_busy;
395                 }
396         }
397         return 0;
398
399 data_region_busy:
400         addr = hwif->io_ports[IDE_CONTROL_OFFSET];
401         if (addr)
402                 release_region(addr, 1);
403 control_region_busy:
404         /* If any errors are return, we drop the hwif interface. */
405         return -EBUSY;
406 }
407
408 /**
409  *      ide_hwif_release_regions - free IDE resources
410  *
411  *      Note that we only release the standard ports,
412  *      and do not even try to handle any extra ports
413  *      allocated for weird IDE interface chipsets.
414  *
415  *      Note also that we don't yet handle mmio resources here. More
416  *      importantly our caller should be doing this so we need to 
417  *      restructure this as a helper function for drivers.
418  */
419
420 void ide_hwif_release_regions(ide_hwif_t *hwif)
421 {
422         u32 i = 0;
423
424         if (hwif->mmio)
425                 return;
426         if (hwif->io_ports[IDE_CONTROL_OFFSET])
427                 release_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
428         if (hwif->straight8) {
429                 release_region(hwif->io_ports[IDE_DATA_OFFSET], 8);
430                 return;
431         }
432         for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++)
433                 if (hwif->io_ports[i])
434                         release_region(hwif->io_ports[i], 1);
435 }
436
437 /**
438  *      ide_hwif_restore        -       restore hwif to template
439  *      @hwif: hwif to update
440  *      @tmp_hwif: template
441  *
442  *      Restore hwif to a previous state by copying most settings
443  *      from the template.
444  */
445
446 static void ide_hwif_restore(ide_hwif_t *hwif, ide_hwif_t *tmp_hwif)
447 {
448         hwif->hwgroup                   = tmp_hwif->hwgroup;
449
450         hwif->gendev.parent             = tmp_hwif->gendev.parent;
451
452         hwif->proc                      = tmp_hwif->proc;
453
454         hwif->major                     = tmp_hwif->major;
455         hwif->straight8                 = tmp_hwif->straight8;
456         hwif->bus_state                 = tmp_hwif->bus_state;
457
458         hwif->atapi_dma                 = tmp_hwif->atapi_dma;
459         hwif->ultra_mask                = tmp_hwif->ultra_mask;
460         hwif->mwdma_mask                = tmp_hwif->mwdma_mask;
461         hwif->swdma_mask                = tmp_hwif->swdma_mask;
462
463         hwif->cbl                       = tmp_hwif->cbl;
464
465         hwif->chipset                   = tmp_hwif->chipset;
466         hwif->hold                      = tmp_hwif->hold;
467
468 #ifdef CONFIG_BLK_DEV_IDEPCI
469         hwif->pci_dev                   = tmp_hwif->pci_dev;
470         hwif->cds                       = tmp_hwif->cds;
471 #endif
472
473         hwif->tuneproc                  = tmp_hwif->tuneproc;
474         hwif->speedproc                 = tmp_hwif->speedproc;
475         hwif->udma_filter               = tmp_hwif->udma_filter;
476         hwif->selectproc                = tmp_hwif->selectproc;
477         hwif->reset_poll                = tmp_hwif->reset_poll;
478         hwif->pre_reset                 = tmp_hwif->pre_reset;
479         hwif->resetproc                 = tmp_hwif->resetproc;
480         hwif->intrproc                  = tmp_hwif->intrproc;
481         hwif->maskproc                  = tmp_hwif->maskproc;
482         hwif->quirkproc                 = tmp_hwif->quirkproc;
483         hwif->busproc                   = tmp_hwif->busproc;
484
485         hwif->ata_input_data            = tmp_hwif->ata_input_data;
486         hwif->ata_output_data           = tmp_hwif->ata_output_data;
487         hwif->atapi_input_bytes         = tmp_hwif->atapi_input_bytes;
488         hwif->atapi_output_bytes        = tmp_hwif->atapi_output_bytes;
489
490         hwif->dma_setup                 = tmp_hwif->dma_setup;
491         hwif->dma_exec_cmd              = tmp_hwif->dma_exec_cmd;
492         hwif->dma_start                 = tmp_hwif->dma_start;
493         hwif->ide_dma_end               = tmp_hwif->ide_dma_end;
494         hwif->ide_dma_check             = tmp_hwif->ide_dma_check;
495         hwif->ide_dma_on                = tmp_hwif->ide_dma_on;
496         hwif->dma_off_quietly           = tmp_hwif->dma_off_quietly;
497         hwif->ide_dma_test_irq          = tmp_hwif->ide_dma_test_irq;
498         hwif->ide_dma_clear_irq         = tmp_hwif->ide_dma_clear_irq;
499         hwif->dma_host_on               = tmp_hwif->dma_host_on;
500         hwif->dma_host_off              = tmp_hwif->dma_host_off;
501         hwif->dma_lost_irq              = tmp_hwif->dma_lost_irq;
502         hwif->dma_timeout               = tmp_hwif->dma_timeout;
503
504         hwif->OUTB                      = tmp_hwif->OUTB;
505         hwif->OUTBSYNC                  = tmp_hwif->OUTBSYNC;
506         hwif->OUTW                      = tmp_hwif->OUTW;
507         hwif->OUTSW                     = tmp_hwif->OUTSW;
508         hwif->OUTSL                     = tmp_hwif->OUTSL;
509
510         hwif->INB                       = tmp_hwif->INB;
511         hwif->INW                       = tmp_hwif->INW;
512         hwif->INSW                      = tmp_hwif->INSW;
513         hwif->INSL                      = tmp_hwif->INSL;
514
515         hwif->sg_max_nents              = tmp_hwif->sg_max_nents;
516
517         hwif->mmio                      = tmp_hwif->mmio;
518         hwif->rqsize                    = tmp_hwif->rqsize;
519         hwif->no_lba48                  = tmp_hwif->no_lba48;
520
521 #ifndef CONFIG_BLK_DEV_IDECS
522         hwif->irq                       = tmp_hwif->irq;
523 #endif
524
525         hwif->dma_base                  = tmp_hwif->dma_base;
526         hwif->dma_master                = tmp_hwif->dma_master;
527         hwif->dma_command               = tmp_hwif->dma_command;
528         hwif->dma_vendor1               = tmp_hwif->dma_vendor1;
529         hwif->dma_status                = tmp_hwif->dma_status;
530         hwif->dma_vendor3               = tmp_hwif->dma_vendor3;
531         hwif->dma_prdtable              = tmp_hwif->dma_prdtable;
532
533         hwif->config_data               = tmp_hwif->config_data;
534         hwif->select_data               = tmp_hwif->select_data;
535         hwif->extra_base                = tmp_hwif->extra_base;
536         hwif->extra_ports               = tmp_hwif->extra_ports;
537         hwif->autodma                   = tmp_hwif->autodma;
538
539         hwif->hwif_data                 = tmp_hwif->hwif_data;
540 }
541
542 /**
543  *      ide_unregister          -       free an IDE interface
544  *      @index: index of interface (will change soon to a pointer)
545  *
546  *      Perform the final unregister of an IDE interface. At the moment
547  *      we don't refcount interfaces so this will also get split up.
548  *
549  *      Locking:
550  *      The caller must not hold the IDE locks
551  *      The drive present/vanishing is not yet properly locked
552  *      Take care with the callbacks. These have been split to avoid
553  *      deadlocking the IDE layer. The shutdown callback is called
554  *      before we take the lock and free resources. It is up to the
555  *      caller to be sure there is no pending I/O here, and that
556  *      the interface will not be reopened (present/vanishing locking
557  *      isn't yet done BTW). After we commit to the final kill we
558  *      call the cleanup callback with the ide locks held.
559  *
560  *      Unregister restores the hwif structures to the default state.
561  *      This is raving bonkers.
562  */
563
564 void ide_unregister(unsigned int index)
565 {
566         ide_drive_t *drive;
567         ide_hwif_t *hwif, *g;
568         static ide_hwif_t tmp_hwif; /* protected by ide_cfg_mtx */
569         ide_hwgroup_t *hwgroup;
570         int irq_count = 0, unit;
571
572         BUG_ON(index >= MAX_HWIFS);
573
574         BUG_ON(in_interrupt());
575         BUG_ON(irqs_disabled());
576         mutex_lock(&ide_cfg_mtx);
577         spin_lock_irq(&ide_lock);
578         hwif = &ide_hwifs[index];
579         if (!hwif->present)
580                 goto abort;
581         for (unit = 0; unit < MAX_DRIVES; ++unit) {
582                 drive = &hwif->drives[unit];
583                 if (!drive->present)
584                         continue;
585                 spin_unlock_irq(&ide_lock);
586                 device_unregister(&drive->gendev);
587                 wait_for_completion(&drive->gendev_rel_comp);
588                 spin_lock_irq(&ide_lock);
589         }
590         hwif->present = 0;
591
592         spin_unlock_irq(&ide_lock);
593
594         ide_proc_unregister_port(hwif);
595
596         hwgroup = hwif->hwgroup;
597         /*
598          * free the irq if we were the only hwif using it
599          */
600         g = hwgroup->hwif;
601         do {
602                 if (g->irq == hwif->irq)
603                         ++irq_count;
604                 g = g->next;
605         } while (g != hwgroup->hwif);
606         if (irq_count == 1)
607                 free_irq(hwif->irq, hwgroup);
608
609         spin_lock_irq(&ide_lock);
610         /*
611          * Note that we only release the standard ports,
612          * and do not even try to handle any extra ports
613          * allocated for weird IDE interface chipsets.
614          */
615         ide_hwif_release_regions(hwif);
616
617         /*
618          * Remove us from the hwgroup, and free
619          * the hwgroup if we were the only member
620          */
621         if (hwif->next == hwif) {
622                 BUG_ON(hwgroup->hwif != hwif);
623                 kfree(hwgroup);
624         } else {
625                 /* There is another interface in hwgroup.
626                  * Unlink us, and set hwgroup->drive and ->hwif to
627                  * something sane.
628                  */
629                 g = hwgroup->hwif;
630                 while (g->next != hwif)
631                         g = g->next;
632                 g->next = hwif->next;
633                 if (hwgroup->hwif == hwif) {
634                         /* Chose a random hwif for hwgroup->hwif.
635                          * It's guaranteed that there are no drives
636                          * left in the hwgroup.
637                          */
638                         BUG_ON(hwgroup->drive != NULL);
639                         hwgroup->hwif = g;
640                 }
641                 BUG_ON(hwgroup->hwif == hwif);
642         }
643
644         /* More messed up locking ... */
645         spin_unlock_irq(&ide_lock);
646         device_unregister(&hwif->gendev);
647         wait_for_completion(&hwif->gendev_rel_comp);
648
649         /*
650          * Remove us from the kernel's knowledge
651          */
652         blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
653         kfree(hwif->sg_table);
654         unregister_blkdev(hwif->major, hwif->name);
655         spin_lock_irq(&ide_lock);
656
657         if (hwif->dma_base) {
658                 (void) ide_release_dma(hwif);
659
660                 hwif->dma_base = 0;
661                 hwif->dma_master = 0;
662                 hwif->dma_command = 0;
663                 hwif->dma_vendor1 = 0;
664                 hwif->dma_status = 0;
665                 hwif->dma_vendor3 = 0;
666                 hwif->dma_prdtable = 0;
667
668                 hwif->extra_base  = 0;
669                 hwif->extra_ports = 0;
670         }
671
672         /* copy original settings */
673         tmp_hwif = *hwif;
674
675         /* restore hwif data to pristine status */
676         init_hwif_data(hwif, index);
677         init_hwif_default(hwif, index);
678
679         ide_hwif_restore(hwif, &tmp_hwif);
680
681 abort:
682         spin_unlock_irq(&ide_lock);
683         mutex_unlock(&ide_cfg_mtx);
684 }
685
686 EXPORT_SYMBOL(ide_unregister);
687
688
689 /**
690  *      ide_setup_ports         -       set up IDE interface ports
691  *      @hw: register descriptions
692  *      @base: base register
693  *      @offsets: table of register offsets
694  *      @ctrl: control register
695  *      @ack_irq: IRQ ack
696  *      @irq: interrupt lie
697  *
698  *      Setup hw_regs_t structure described by parameters.  You
699  *      may set up the hw structure yourself OR use this routine to
700  *      do it for you. This is basically a helper
701  *
702  */
703  
704 void ide_setup_ports (  hw_regs_t *hw,
705                         unsigned long base, int *offsets,
706                         unsigned long ctrl, unsigned long intr,
707                         ide_ack_intr_t *ack_intr,
708 /*
709  *                      ide_io_ops_t *iops,
710  */
711                         int irq)
712 {
713         int i;
714
715         memset(hw, 0, sizeof(hw_regs_t));
716         for (i = 0; i < IDE_NR_PORTS; i++) {
717                 if (offsets[i] == -1) {
718                         switch(i) {
719                                 case IDE_CONTROL_OFFSET:
720                                         hw->io_ports[i] = ctrl;
721                                         break;
722 #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
723                                 case IDE_IRQ_OFFSET:
724                                         hw->io_ports[i] = intr;
725                                         break;
726 #endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
727                                 default:
728                                         hw->io_ports[i] = 0;
729                                         break;
730                         }
731                 } else {
732                         hw->io_ports[i] = base + offsets[i];
733                 }
734         }
735         hw->irq = irq;
736         hw->dma = NO_DMA;
737         hw->ack_intr = ack_intr;
738 /*
739  *      hw->iops = iops;
740  */
741 }
742
743 /**
744  *      ide_register_hw_with_fixup      -       register IDE interface
745  *      @hw: hardware registers
746  *      @initializing: set while initializing built-in drivers
747  *      @hwifp: pointer to returned hwif
748  *      @fixup: fixup function
749  *
750  *      Register an IDE interface, specifying exactly the registers etc.
751  *      Set init=1 iff calling before probes have taken place.
752  *
753  *      Returns -1 on error.
754  */
755
756 int ide_register_hw_with_fixup(hw_regs_t *hw, int initializing,
757                                ide_hwif_t **hwifp,
758                                void(*fixup)(ide_hwif_t *hwif))
759 {
760         int index, retry = 1;
761         ide_hwif_t *hwif;
762
763         do {
764                 for (index = 0; index < MAX_HWIFS; ++index) {
765                         hwif = &ide_hwifs[index];
766                         if (hwif->hw.io_ports[IDE_DATA_OFFSET] == hw->io_ports[IDE_DATA_OFFSET])
767                                 goto found;
768                 }
769                 for (index = 0; index < MAX_HWIFS; ++index) {
770                         hwif = &ide_hwifs[index];
771                         if (hwif->hold)
772                                 continue;
773                         if ((!hwif->present && !hwif->mate && !initializing) ||
774                             (!hwif->hw.io_ports[IDE_DATA_OFFSET] && initializing))
775                                 goto found;
776                 }
777                 for (index = 0; index < MAX_HWIFS; index++)
778                         ide_unregister(index);
779         } while (retry--);
780         return -1;
781 found:
782         if (hwif->present)
783                 ide_unregister(index);
784         else if (!hwif->hold) {
785                 init_hwif_data(hwif, index);
786                 init_hwif_default(hwif, index);
787         }
788         if (hwif->present)
789                 return -1;
790         memcpy(&hwif->hw, hw, sizeof(*hw));
791         memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->hw.io_ports));
792         hwif->irq = hw->irq;
793         hwif->noprobe = 0;
794         hwif->chipset = hw->chipset;
795         hwif->gendev.parent = hw->dev;
796
797         if (!initializing) {
798                 probe_hwif_init_with_fixup(hwif, fixup);
799                 ide_proc_register_port(hwif);
800         }
801
802         if (hwifp)
803                 *hwifp = hwif;
804
805         return (initializing || hwif->present) ? index : -1;
806 }
807
808 EXPORT_SYMBOL(ide_register_hw_with_fixup);
809
810 int ide_register_hw(hw_regs_t *hw, int initializing, ide_hwif_t **hwifp)
811 {
812         return ide_register_hw_with_fixup(hw, initializing, hwifp, NULL);
813 }
814
815 EXPORT_SYMBOL(ide_register_hw);
816
817 /*
818  *      Locks for IDE setting functionality
819  */
820
821 DEFINE_MUTEX(ide_setting_mtx);
822
823 EXPORT_SYMBOL_GPL(ide_setting_mtx);
824
825 /**
826  *      ide_spin_wait_hwgroup   -       wait for group
827  *      @drive: drive in the group
828  *
829  *      Wait for an IDE device group to go non busy and then return
830  *      holding the ide_lock which guards the hwgroup->busy status
831  *      and right to use it.
832  */
833
834 int ide_spin_wait_hwgroup (ide_drive_t *drive)
835 {
836         ide_hwgroup_t *hwgroup = HWGROUP(drive);
837         unsigned long timeout = jiffies + (3 * HZ);
838
839         spin_lock_irq(&ide_lock);
840
841         while (hwgroup->busy) {
842                 unsigned long lflags;
843                 spin_unlock_irq(&ide_lock);
844                 local_irq_set(lflags);
845                 if (time_after(jiffies, timeout)) {
846                         local_irq_restore(lflags);
847                         printk(KERN_ERR "%s: channel busy\n", drive->name);
848                         return -EBUSY;
849                 }
850                 local_irq_restore(lflags);
851                 spin_lock_irq(&ide_lock);
852         }
853         return 0;
854 }
855
856 EXPORT_SYMBOL(ide_spin_wait_hwgroup);
857
858 int set_io_32bit(ide_drive_t *drive, int arg)
859 {
860         if (drive->no_io_32bit)
861                 return -EPERM;
862
863         if (arg < 0 || arg > 1 + (SUPPORT_VLB_SYNC << 1))
864                 return -EINVAL;
865
866         drive->io_32bit = arg;
867 #ifdef CONFIG_BLK_DEV_DTC2278
868         if (HWIF(drive)->chipset == ide_dtc2278)
869                 HWIF(drive)->drives[!drive->select.b.unit].io_32bit = arg;
870 #endif /* CONFIG_BLK_DEV_DTC2278 */
871         return 0;
872 }
873
874 static int set_ksettings(ide_drive_t *drive, int arg)
875 {
876         if (arg < 0 || arg > 1)
877                 return -EINVAL;
878
879         if (ide_spin_wait_hwgroup(drive))
880                 return -EBUSY;
881         drive->keep_settings = arg;
882         spin_unlock_irq(&ide_lock);
883
884         return 0;
885 }
886
887 int set_using_dma(ide_drive_t *drive, int arg)
888 {
889 #ifdef CONFIG_BLK_DEV_IDEDMA
890         ide_hwif_t *hwif = drive->hwif;
891         int err = -EPERM;
892
893         if (arg < 0 || arg > 1)
894                 return -EINVAL;
895
896         if (!drive->id || !(drive->id->capability & 1))
897                 goto out;
898
899         if (hwif->ide_dma_check == NULL)
900                 goto out;
901
902         err = -EBUSY;
903         if (ide_spin_wait_hwgroup(drive))
904                 goto out;
905         /*
906          * set ->busy flag, unlock and let it ride
907          */
908         hwif->hwgroup->busy = 1;
909         spin_unlock_irq(&ide_lock);
910
911         err = 0;
912
913         if (arg) {
914                 hwif->dma_off_quietly(drive);
915                 if (ide_set_dma(drive) || hwif->ide_dma_on(drive))
916                         err = -EIO;
917         } else
918                 ide_dma_off(drive);
919
920         /*
921          * lock, clear ->busy flag and unlock before leaving
922          */
923         spin_lock_irq(&ide_lock);
924         hwif->hwgroup->busy = 0;
925         spin_unlock_irq(&ide_lock);
926 out:
927         return err;
928 #else
929         if (arg < 0 || arg > 1)
930                 return -EINVAL;
931
932         return -EPERM;
933 #endif
934 }
935
936 int set_pio_mode(ide_drive_t *drive, int arg)
937 {
938         struct request rq;
939
940         if (arg < 0 || arg > 255)
941                 return -EINVAL;
942
943         if (!HWIF(drive)->tuneproc)
944                 return -ENOSYS;
945         if (drive->special.b.set_tune)
946                 return -EBUSY;
947         ide_init_drive_cmd(&rq);
948         drive->tune_req = (u8) arg;
949         drive->special.b.set_tune = 1;
950         (void) ide_do_drive_cmd(drive, &rq, ide_wait);
951         return 0;
952 }
953
954 static int set_unmaskirq(ide_drive_t *drive, int arg)
955 {
956         if (drive->no_unmask)
957                 return -EPERM;
958
959         if (arg < 0 || arg > 1)
960                 return -EINVAL;
961
962         if (ide_spin_wait_hwgroup(drive))
963                 return -EBUSY;
964         drive->unmask = arg;
965         spin_unlock_irq(&ide_lock);
966
967         return 0;
968 }
969
970 /**
971  *      system_bus_clock        -       clock guess
972  *
973  *      External version of the bus clock guess used by very old IDE drivers
974  *      for things like VLB timings. Should not be used.
975  */
976
977 int system_bus_clock (void)
978 {
979         return((int) ((!system_bus_speed) ? ide_system_bus_speed() : system_bus_speed ));
980 }
981
982 EXPORT_SYMBOL(system_bus_clock);
983
984 static int generic_ide_suspend(struct device *dev, pm_message_t mesg)
985 {
986         ide_drive_t *drive = dev->driver_data;
987         ide_hwif_t *hwif = HWIF(drive);
988         struct request rq;
989         struct request_pm_state rqpm;
990         ide_task_t args;
991
992         /* Call ACPI _GTM only once */
993         if (!(drive->dn % 2))
994                 ide_acpi_get_timing(hwif);
995
996         memset(&rq, 0, sizeof(rq));
997         memset(&rqpm, 0, sizeof(rqpm));
998         memset(&args, 0, sizeof(args));
999         rq.cmd_type = REQ_TYPE_PM_SUSPEND;
1000         rq.special = &args;
1001         rq.data = &rqpm;
1002         rqpm.pm_step = ide_pm_state_start_suspend;
1003         if (mesg.event == PM_EVENT_PRETHAW)
1004                 mesg.event = PM_EVENT_FREEZE;
1005         rqpm.pm_state = mesg.event;
1006
1007         return ide_do_drive_cmd(drive, &rq, ide_wait);
1008 }
1009
1010 static int generic_ide_resume(struct device *dev)
1011 {
1012         ide_drive_t *drive = dev->driver_data;
1013         ide_hwif_t *hwif = HWIF(drive);
1014         struct request rq;
1015         struct request_pm_state rqpm;
1016         ide_task_t args;
1017         int err;
1018
1019         /* Call ACPI _STM only once */
1020         if (!(drive->dn % 2))
1021                 ide_acpi_push_timing(hwif);
1022
1023         ide_acpi_exec_tfs(drive);
1024
1025         memset(&rq, 0, sizeof(rq));
1026         memset(&rqpm, 0, sizeof(rqpm));
1027         memset(&args, 0, sizeof(args));
1028         rq.cmd_type = REQ_TYPE_PM_RESUME;
1029         rq.special = &args;
1030         rq.data = &rqpm;
1031         rqpm.pm_step = ide_pm_state_start_resume;
1032         rqpm.pm_state = PM_EVENT_ON;
1033
1034         err = ide_do_drive_cmd(drive, &rq, ide_head_wait);
1035
1036         if (err == 0 && dev->driver) {
1037                 ide_driver_t *drv = to_ide_driver(dev->driver);
1038
1039                 if (drv->resume)
1040                         drv->resume(drive);
1041         }
1042
1043         return err;
1044 }
1045
1046 int generic_ide_ioctl(ide_drive_t *drive, struct file *file, struct block_device *bdev,
1047                         unsigned int cmd, unsigned long arg)
1048 {
1049         unsigned long flags;
1050         ide_driver_t *drv;
1051         void __user *p = (void __user *)arg;
1052         int err, (*setfunc)(ide_drive_t *, int);
1053         u8 *val;
1054
1055         err = scsi_cmd_ioctl(file, bdev->bd_disk->queue, bdev->bd_disk, cmd, p);
1056         if (err != -ENOTTY)
1057                 return err;
1058
1059         switch (cmd) {
1060         case HDIO_GET_32BIT:        val = &drive->io_32bit;      goto read_val;
1061         case HDIO_GET_KEEPSETTINGS: val = &drive->keep_settings; goto read_val;
1062         case HDIO_GET_UNMASKINTR:   val = &drive->unmask;        goto read_val;
1063         case HDIO_GET_DMA:          val = &drive->using_dma;     goto read_val;
1064         case HDIO_SET_32BIT:        setfunc = set_io_32bit;      goto set_val;
1065         case HDIO_SET_KEEPSETTINGS: setfunc = set_ksettings;     goto set_val;
1066         case HDIO_SET_PIO_MODE:     setfunc = set_pio_mode;      goto set_val;
1067         case HDIO_SET_UNMASKINTR:   setfunc = set_unmaskirq;     goto set_val;
1068         case HDIO_SET_DMA:          setfunc = set_using_dma;     goto set_val;
1069         }
1070
1071         switch (cmd) {
1072                 case HDIO_OBSOLETE_IDENTITY:
1073                 case HDIO_GET_IDENTITY:
1074                         if (bdev != bdev->bd_contains)
1075                                 return -EINVAL;
1076                         if (drive->id_read == 0)
1077                                 return -ENOMSG;
1078                         if (copy_to_user(p, drive->id, (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
1079                                 return -EFAULT;
1080                         return 0;
1081
1082                 case HDIO_GET_NICE:
1083                         return put_user(drive->dsc_overlap      <<      IDE_NICE_DSC_OVERLAP    |
1084                                         drive->atapi_overlap    <<      IDE_NICE_ATAPI_OVERLAP  |
1085                                         drive->nice0            <<      IDE_NICE_0              |
1086                                         drive->nice1            <<      IDE_NICE_1              |
1087                                         drive->nice2            <<      IDE_NICE_2,
1088                                         (long __user *) arg);
1089
1090 #ifdef CONFIG_IDE_TASK_IOCTL
1091                 case HDIO_DRIVE_TASKFILE:
1092                         if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1093                                 return -EACCES;
1094                         switch(drive->media) {
1095                                 case ide_disk:
1096                                         return ide_taskfile_ioctl(drive, cmd, arg);
1097                                 default:
1098                                         return -ENOMSG;
1099                         }
1100 #endif /* CONFIG_IDE_TASK_IOCTL */
1101
1102                 case HDIO_DRIVE_CMD:
1103                         if (!capable(CAP_SYS_RAWIO))
1104                                 return -EACCES;
1105                         return ide_cmd_ioctl(drive, cmd, arg);
1106
1107                 case HDIO_DRIVE_TASK:
1108                         if (!capable(CAP_SYS_RAWIO))
1109                                 return -EACCES;
1110                         return ide_task_ioctl(drive, cmd, arg);
1111
1112                 case HDIO_SCAN_HWIF:
1113                 {
1114                         hw_regs_t hw;
1115                         int args[3];
1116                         if (!capable(CAP_SYS_RAWIO)) return -EACCES;
1117                         if (copy_from_user(args, p, 3 * sizeof(int)))
1118                                 return -EFAULT;
1119                         memset(&hw, 0, sizeof(hw));
1120                         ide_init_hwif_ports(&hw, (unsigned long) args[0],
1121                                             (unsigned long) args[1], NULL);
1122                         hw.irq = args[2];
1123                         if (ide_register_hw(&hw, 0, NULL) == -1)
1124                                 return -EIO;
1125                         return 0;
1126                 }
1127                 case HDIO_UNREGISTER_HWIF:
1128                         if (!capable(CAP_SYS_RAWIO)) return -EACCES;
1129                         /* (arg > MAX_HWIFS) checked in function */
1130                         ide_unregister(arg);
1131                         return 0;
1132                 case HDIO_SET_NICE:
1133                         if (!capable(CAP_SYS_ADMIN)) return -EACCES;
1134                         if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
1135                                 return -EPERM;
1136                         drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
1137                         drv = *(ide_driver_t **)bdev->bd_disk->private_data;
1138                         if (drive->dsc_overlap && !drv->supports_dsc_overlap) {
1139                                 drive->dsc_overlap = 0;
1140                                 return -EPERM;
1141                         }
1142                         drive->nice1 = (arg >> IDE_NICE_1) & 1;
1143                         return 0;
1144                 case HDIO_DRIVE_RESET:
1145                 {
1146                         unsigned long flags;
1147                         if (!capable(CAP_SYS_ADMIN)) return -EACCES;
1148                         
1149                         /*
1150                          *      Abort the current command on the
1151                          *      group if there is one, taking
1152                          *      care not to allow anything else
1153                          *      to be queued and to die on the
1154                          *      spot if we miss one somehow
1155                          */
1156
1157                         spin_lock_irqsave(&ide_lock, flags);
1158
1159                         if (HWGROUP(drive)->resetting) {
1160                                 spin_unlock_irqrestore(&ide_lock, flags);
1161                                 return -EBUSY;
1162                         }
1163
1164                         ide_abort(drive, "drive reset");
1165
1166                         BUG_ON(HWGROUP(drive)->handler);
1167                                 
1168                         /* Ensure nothing gets queued after we
1169                            drop the lock. Reset will clear the busy */
1170                    
1171                         HWGROUP(drive)->busy = 1;
1172                         spin_unlock_irqrestore(&ide_lock, flags);
1173                         (void) ide_do_reset(drive);
1174
1175                         return 0;
1176                 }
1177
1178                 case HDIO_GET_BUSSTATE:
1179                         if (!capable(CAP_SYS_ADMIN))
1180                                 return -EACCES;
1181                         if (put_user(HWIF(drive)->bus_state, (long __user *)arg))
1182                                 return -EFAULT;
1183                         return 0;
1184
1185                 case HDIO_SET_BUSSTATE:
1186                         if (!capable(CAP_SYS_ADMIN))
1187                                 return -EACCES;
1188                         if (HWIF(drive)->busproc)
1189                                 return HWIF(drive)->busproc(drive, (int)arg);
1190                         return -EOPNOTSUPP;
1191                 default:
1192                         return -EINVAL;
1193         }
1194
1195 read_val:
1196         mutex_lock(&ide_setting_mtx);
1197         spin_lock_irqsave(&ide_lock, flags);
1198         err = *val;
1199         spin_unlock_irqrestore(&ide_lock, flags);
1200         mutex_unlock(&ide_setting_mtx);
1201         return err >= 0 ? put_user(err, (long __user *)arg) : err;
1202
1203 set_val:
1204         if (bdev != bdev->bd_contains)
1205                 err = -EINVAL;
1206         else {
1207                 if (!capable(CAP_SYS_ADMIN))
1208                         err = -EACCES;
1209                 else {
1210                         mutex_lock(&ide_setting_mtx);
1211                         err = setfunc(drive, arg);
1212                         mutex_unlock(&ide_setting_mtx);
1213                 }
1214         }
1215         return err;
1216 }
1217
1218 EXPORT_SYMBOL(generic_ide_ioctl);
1219
1220 /*
1221  * stridx() returns the offset of c within s,
1222  * or -1 if c is '\0' or not found within s.
1223  */
1224 static int __init stridx (const char *s, char c)
1225 {
1226         char *i = strchr(s, c);
1227         return (i && c) ? i - s : -1;
1228 }
1229
1230 /*
1231  * match_parm() does parsing for ide_setup():
1232  *
1233  * 1. the first char of s must be '='.
1234  * 2. if the remainder matches one of the supplied keywords,
1235  *     the index (1 based) of the keyword is negated and returned.
1236  * 3. if the remainder is a series of no more than max_vals numbers
1237  *     separated by commas, the numbers are saved in vals[] and a
1238  *     count of how many were saved is returned.  Base10 is assumed,
1239  *     and base16 is allowed when prefixed with "0x".
1240  * 4. otherwise, zero is returned.
1241  */
1242 static int __init match_parm (char *s, const char *keywords[], int vals[], int max_vals)
1243 {
1244         static const char *decimal = "0123456789";
1245         static const char *hex = "0123456789abcdef";
1246         int i, n;
1247
1248         if (*s++ == '=') {
1249                 /*
1250                  * Try matching against the supplied keywords,
1251                  * and return -(index+1) if we match one
1252                  */
1253                 if (keywords != NULL) {
1254                         for (i = 0; *keywords != NULL; ++i) {
1255                                 if (!strcmp(s, *keywords++))
1256                                         return -(i+1);
1257                         }
1258                 }
1259                 /*
1260                  * Look for a series of no more than "max_vals"
1261                  * numeric values separated by commas, in base10,
1262                  * or base16 when prefixed with "0x".
1263                  * Return a count of how many were found.
1264                  */
1265                 for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
1266                         vals[n] = i;
1267                         while ((i = stridx(decimal, *++s)) >= 0)
1268                                 vals[n] = (vals[n] * 10) + i;
1269                         if (*s == 'x' && !vals[n]) {
1270                                 while ((i = stridx(hex, *++s)) >= 0)
1271                                         vals[n] = (vals[n] * 0x10) + i;
1272                         }
1273                         if (++n == max_vals)
1274                                 break;
1275                         if (*s == ',' || *s == ';')
1276                                 ++s;
1277                 }
1278                 if (!*s)
1279                         return n;
1280         }
1281         return 0;       /* zero = nothing matched */
1282 }
1283
1284 #ifdef CONFIG_BLK_DEV_ALI14XX
1285 extern int probe_ali14xx;
1286 extern int ali14xx_init(void);
1287 #endif
1288 #ifdef CONFIG_BLK_DEV_UMC8672
1289 extern int probe_umc8672;
1290 extern int umc8672_init(void);
1291 #endif
1292 #ifdef CONFIG_BLK_DEV_DTC2278
1293 extern int probe_dtc2278;
1294 extern int dtc2278_init(void);
1295 #endif
1296 #ifdef CONFIG_BLK_DEV_HT6560B
1297 extern int probe_ht6560b;
1298 extern int ht6560b_init(void);
1299 #endif
1300 #ifdef CONFIG_BLK_DEV_QD65XX
1301 extern int probe_qd65xx;
1302 extern int qd65xx_init(void);
1303 #endif
1304
1305 static int __initdata is_chipset_set[MAX_HWIFS];
1306
1307 /*
1308  * ide_setup() gets called VERY EARLY during initialization,
1309  * to handle kernel "command line" strings beginning with "hdx=" or "ide".
1310  *
1311  * Remember to update Documentation/ide.txt if you change something here.
1312  */
1313 static int __init ide_setup(char *s)
1314 {
1315         int i, vals[3];
1316         ide_hwif_t *hwif;
1317         ide_drive_t *drive;
1318         unsigned int hw, unit;
1319         const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
1320         const char max_hwif  = '0' + (MAX_HWIFS - 1);
1321
1322         
1323         if (strncmp(s,"hd",2) == 0 && s[2] == '=')      /* hd= is for hd.c   */
1324                 return 0;                               /* driver and not us */
1325
1326         if (strncmp(s,"ide",3) && strncmp(s,"idebus",6) && strncmp(s,"hd",2))
1327                 return 0;
1328
1329         printk(KERN_INFO "ide_setup: %s", s);
1330         init_ide_data ();
1331
1332 #ifdef CONFIG_BLK_DEV_IDEDOUBLER
1333         if (!strcmp(s, "ide=doubler")) {
1334                 extern int ide_doubler;
1335
1336                 printk(" : Enabled support for IDE doublers\n");
1337                 ide_doubler = 1;
1338                 return 1;
1339         }
1340 #endif /* CONFIG_BLK_DEV_IDEDOUBLER */
1341
1342         if (!strcmp(s, "ide=nodma")) {
1343                 printk(" : Prevented DMA\n");
1344                 noautodma = 1;
1345                 return 1;
1346         }
1347
1348 #ifdef CONFIG_IDEPCI_PCIBUS_ORDER
1349         if (!strcmp(s, "ide=reverse")) {
1350                 ide_scan_direction = 1;
1351                 printk(" : Enabled support for IDE inverse scan order.\n");
1352                 return 1;
1353         }
1354 #endif
1355
1356 #ifdef CONFIG_BLK_DEV_IDEACPI
1357         if (!strcmp(s, "ide=noacpi")) {
1358                 //printk(" : Disable IDE ACPI support.\n");
1359                 ide_noacpi = 1;
1360                 return 1;
1361         }
1362         if (!strcmp(s, "ide=acpigtf")) {
1363                 //printk(" : Enable IDE ACPI _GTF support.\n");
1364                 ide_noacpitfs = 0;
1365                 return 1;
1366         }
1367         if (!strcmp(s, "ide=acpionboot")) {
1368                 //printk(" : Call IDE ACPI methods on boot.\n");
1369                 ide_noacpionboot = 0;
1370                 return 1;
1371         }
1372 #endif /* CONFIG_BLK_DEV_IDEACPI */
1373
1374         /*
1375          * Look for drive options:  "hdx="
1376          */
1377         if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
1378                 const char *hd_words[] = {
1379                         "none", "noprobe", "nowerr", "cdrom", "minus5",
1380                         "autotune", "noautotune", "minus8", "swapdata", "bswap",
1381                         "noflush", "remap", "remap63", "scsi", NULL };
1382                 unit = s[2] - 'a';
1383                 hw   = unit / MAX_DRIVES;
1384                 unit = unit % MAX_DRIVES;
1385                 hwif = &ide_hwifs[hw];
1386                 drive = &hwif->drives[unit];
1387                 if (strncmp(s + 4, "ide-", 4) == 0) {
1388                         strlcpy(drive->driver_req, s + 4, sizeof(drive->driver_req));
1389                         goto done;
1390                 }
1391                 switch (match_parm(&s[3], hd_words, vals, 3)) {
1392                         case -1: /* "none" */
1393                         case -2: /* "noprobe" */
1394                                 drive->noprobe = 1;
1395                                 goto done;
1396                         case -3: /* "nowerr" */
1397                                 drive->bad_wstat = BAD_R_STAT;
1398                                 hwif->noprobe = 0;
1399                                 goto done;
1400                         case -4: /* "cdrom" */
1401                                 drive->present = 1;
1402                                 drive->media = ide_cdrom;
1403                                 /* an ATAPI device ignores DRDY */
1404                                 drive->ready_stat = 0;
1405                                 hwif->noprobe = 0;
1406                                 goto done;
1407                         case -6: /* "autotune" */
1408                                 drive->autotune = IDE_TUNE_AUTO;
1409                                 goto obsolete_option;
1410                         case -7: /* "noautotune" */
1411                                 drive->autotune = IDE_TUNE_NOAUTO;
1412                                 goto obsolete_option;
1413                         case -9: /* "swapdata" */
1414                         case -10: /* "bswap" */
1415                                 drive->bswap = 1;
1416                                 goto done;
1417                         case -11: /* noflush */
1418                                 drive->noflush = 1;
1419                                 goto done;
1420                         case -12: /* "remap" */
1421                                 drive->remap_0_to_1 = 1;
1422                                 goto done;
1423                         case -13: /* "remap63" */
1424                                 drive->sect0 = 63;
1425                                 goto done;
1426                         case -14: /* "scsi" */
1427                                 drive->scsi = 1;
1428                                 goto done;
1429                         case 3: /* cyl,head,sect */
1430                                 drive->media    = ide_disk;
1431                                 drive->ready_stat = READY_STAT;
1432                                 drive->cyl      = drive->bios_cyl  = vals[0];
1433                                 drive->head     = drive->bios_head = vals[1];
1434                                 drive->sect     = drive->bios_sect = vals[2];
1435                                 drive->present  = 1;
1436                                 drive->forced_geom = 1;
1437                                 hwif->noprobe = 0;
1438                                 goto done;
1439                         default:
1440                                 goto bad_option;
1441                 }
1442         }
1443
1444         if (s[0] != 'i' || s[1] != 'd' || s[2] != 'e')
1445                 goto bad_option;
1446         /*
1447          * Look for bus speed option:  "idebus="
1448          */
1449         if (s[3] == 'b' && s[4] == 'u' && s[5] == 's') {
1450                 if (match_parm(&s[6], NULL, vals, 1) != 1)
1451                         goto bad_option;
1452                 if (vals[0] >= 20 && vals[0] <= 66) {
1453                         idebus_parameter = vals[0];
1454                 } else
1455                         printk(" -- BAD BUS SPEED! Expected value from 20 to 66");
1456                 goto done;
1457         }
1458         /*
1459          * Look for interface options:  "idex="
1460          */
1461         if (s[3] >= '0' && s[3] <= max_hwif) {
1462                 /*
1463                  * Be VERY CAREFUL changing this: note hardcoded indexes below
1464                  * (-8, -9, -10) are reserved to ease the hardcoding.
1465                  */
1466                 static const char *ide_words[] = {
1467                         "noprobe", "serialize", "minus3", "minus4",
1468                         "reset", "dma", "ata66", "minus8", "minus9",
1469                         "minus10", "four", "qd65xx", "ht6560b", "cmd640_vlb",
1470                         "dtc2278", "umc8672", "ali14xx", NULL };
1471                 hw = s[3] - '0';
1472                 hwif = &ide_hwifs[hw];
1473                 i = match_parm(&s[4], ide_words, vals, 3);
1474
1475                 /*
1476                  * Cryptic check to ensure chipset not already set for hwif.
1477                  * Note: we can't depend on hwif->chipset here.
1478                  */
1479                 if ((i >= -18 && i <= -11) || (i > 0 && i <= 3)) {
1480                         /* chipset already specified */
1481                         if (is_chipset_set[hw])
1482                                 goto bad_option;
1483                         if (i > -18 && i <= -11) {
1484                                 /* these drivers are for "ide0=" only */
1485                                 if (hw != 0)
1486                                         goto bad_hwif;
1487                                 /* chipset already specified for 2nd port */
1488                                 if (is_chipset_set[hw+1])
1489                                         goto bad_option;
1490                         }
1491                         is_chipset_set[hw] = 1;
1492                         printk("\n");
1493                 }
1494
1495                 switch (i) {
1496 #ifdef CONFIG_BLK_DEV_ALI14XX
1497                         case -17: /* "ali14xx" */
1498                                 probe_ali14xx = 1;
1499                                 goto done;
1500 #endif
1501 #ifdef CONFIG_BLK_DEV_UMC8672
1502                         case -16: /* "umc8672" */
1503                                 probe_umc8672 = 1;
1504                                 goto done;
1505 #endif
1506 #ifdef CONFIG_BLK_DEV_DTC2278
1507                         case -15: /* "dtc2278" */
1508                                 probe_dtc2278 = 1;
1509                                 goto done;
1510 #endif
1511 #ifdef CONFIG_BLK_DEV_CMD640
1512                         case -14: /* "cmd640_vlb" */
1513                         {
1514                                 extern int cmd640_vlb; /* flag for cmd640.c */
1515                                 cmd640_vlb = 1;
1516                                 goto done;
1517                         }
1518 #endif
1519 #ifdef CONFIG_BLK_DEV_HT6560B
1520                         case -13: /* "ht6560b" */
1521                                 probe_ht6560b = 1;
1522                                 goto done;
1523 #endif
1524 #ifdef CONFIG_BLK_DEV_QD65XX
1525                         case -12: /* "qd65xx" */
1526                                 probe_qd65xx = 1;
1527                                 goto done;
1528 #endif
1529 #ifdef CONFIG_BLK_DEV_4DRIVES
1530                         case -11: /* "four" drives on one set of ports */
1531                         {
1532                                 ide_hwif_t *mate = &ide_hwifs[hw^1];
1533                                 mate->drives[0].select.all ^= 0x20;
1534                                 mate->drives[1].select.all ^= 0x20;
1535                                 hwif->chipset = mate->chipset = ide_4drives;
1536                                 mate->irq = hwif->irq;
1537                                 memcpy(mate->io_ports, hwif->io_ports, sizeof(hwif->io_ports));
1538                                 hwif->mate = mate;
1539                                 mate->mate = hwif;
1540                                 hwif->serialized = mate->serialized = 1;
1541                                 goto obsolete_option;
1542                         }
1543 #endif /* CONFIG_BLK_DEV_4DRIVES */
1544                         case -10: /* minus10 */
1545                         case -9: /* minus9 */
1546                         case -8: /* minus8 */
1547                         case -4:
1548                         case -3:
1549                                 goto bad_option;
1550                         case -7: /* ata66 */
1551 #ifdef CONFIG_BLK_DEV_IDEPCI
1552                                 /*
1553                                  * Use ATA_CBL_PATA40_SHORT so drive side
1554                                  * cable detection is also overriden.
1555                                  */
1556                                 hwif->cbl = ATA_CBL_PATA40_SHORT;
1557                                 goto obsolete_option;
1558 #else
1559                                 goto bad_hwif;
1560 #endif
1561                         case -6: /* dma */
1562                                 hwif->autodma = 1;
1563                                 goto obsolete_option;
1564                         case -5: /* "reset" */
1565                                 hwif->reset = 1;
1566                                 goto obsolete_option;
1567                         case -2: /* "serialize" */
1568                                 hwif->mate = &ide_hwifs[hw^1];
1569                                 hwif->mate->mate = hwif;
1570                                 hwif->serialized = hwif->mate->serialized = 1;
1571                                 goto obsolete_option;
1572
1573                         case -1: /* "noprobe" */
1574                                 hwif->noprobe = 1;
1575                                 goto done;
1576
1577                         case 1: /* base */
1578                                 vals[1] = vals[0] + 0x206; /* default ctl */
1579                         case 2: /* base,ctl */
1580                                 vals[2] = 0;    /* default irq = probe for it */
1581                         case 3: /* base,ctl,irq */
1582                                 hwif->hw.irq = vals[2];
1583                                 ide_init_hwif_ports(&hwif->hw, (unsigned long) vals[0], (unsigned long) vals[1], &hwif->irq);
1584                                 memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->io_ports));
1585                                 hwif->irq      = vals[2];
1586                                 hwif->noprobe  = 0;
1587                                 hwif->chipset  = ide_forced;
1588                                 goto obsolete_option;
1589
1590                         case 0: goto bad_option;
1591                         default:
1592                                 printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
1593                                 return 1;
1594                 }
1595         }
1596 bad_option:
1597         printk(" -- BAD OPTION\n");
1598         return 1;
1599 obsolete_option:
1600         printk(" -- OBSOLETE OPTION, WILL BE REMOVED SOON!\n");
1601         return 1;
1602 bad_hwif:
1603         printk("-- NOT SUPPORTED ON ide%d", hw);
1604 done:
1605         printk("\n");
1606         return 1;
1607 }
1608
1609 extern void __init pnpide_init(void);
1610 extern void __exit pnpide_exit(void);
1611 extern void __init h8300_ide_init(void);
1612
1613 /*
1614  * probe_for_hwifs() finds/initializes "known" IDE interfaces
1615  */
1616 static void __init probe_for_hwifs (void)
1617 {
1618 #ifdef CONFIG_IDEPCI_PCIBUS_ORDER
1619         ide_scan_pcibus(ide_scan_direction);
1620 #endif
1621
1622 #ifdef CONFIG_ETRAX_IDE
1623         {
1624                 extern void init_e100_ide(void);
1625                 init_e100_ide();
1626         }
1627 #endif /* CONFIG_ETRAX_IDE */
1628 #ifdef CONFIG_BLK_DEV_CMD640
1629         {
1630                 extern void ide_probe_for_cmd640x(void);
1631                 ide_probe_for_cmd640x();
1632         }
1633 #endif /* CONFIG_BLK_DEV_CMD640 */
1634 #ifdef CONFIG_BLK_DEV_IDE_PMAC
1635         {
1636                 extern int pmac_ide_probe(void);
1637                 (void)pmac_ide_probe();
1638         }
1639 #endif /* CONFIG_BLK_DEV_IDE_PMAC */
1640 #ifdef CONFIG_BLK_DEV_GAYLE
1641         {
1642                 extern void gayle_init(void);
1643                 gayle_init();
1644         }
1645 #endif /* CONFIG_BLK_DEV_GAYLE */
1646 #ifdef CONFIG_BLK_DEV_FALCON_IDE
1647         {
1648                 extern void falconide_init(void);
1649                 falconide_init();
1650         }
1651 #endif /* CONFIG_BLK_DEV_FALCON_IDE */
1652 #ifdef CONFIG_BLK_DEV_MAC_IDE
1653         {
1654                 extern void macide_init(void);
1655                 macide_init();
1656         }
1657 #endif /* CONFIG_BLK_DEV_MAC_IDE */
1658 #ifdef CONFIG_BLK_DEV_Q40IDE
1659         {
1660                 extern void q40ide_init(void);
1661                 q40ide_init();
1662         }
1663 #endif /* CONFIG_BLK_DEV_Q40IDE */
1664 #ifdef CONFIG_BLK_DEV_BUDDHA
1665         {
1666                 extern void buddha_init(void);
1667                 buddha_init();
1668         }
1669 #endif /* CONFIG_BLK_DEV_BUDDHA */
1670 #ifdef CONFIG_BLK_DEV_IDEPNP
1671         pnpide_init();
1672 #endif
1673 #ifdef CONFIG_H8300
1674         h8300_ide_init();
1675 #endif
1676 }
1677
1678 /*
1679  * Probe module
1680  */
1681
1682 EXPORT_SYMBOL(ide_lock);
1683
1684 static int ide_bus_match(struct device *dev, struct device_driver *drv)
1685 {
1686         return 1;
1687 }
1688
1689 static char *media_string(ide_drive_t *drive)
1690 {
1691         switch (drive->media) {
1692         case ide_disk:
1693                 return "disk";
1694         case ide_cdrom:
1695                 return "cdrom";
1696         case ide_tape:
1697                 return "tape";
1698         case ide_floppy:
1699                 return "floppy";
1700         case ide_optical:
1701                 return "optical";
1702         default:
1703                 return "UNKNOWN";
1704         }
1705 }
1706
1707 static ssize_t media_show(struct device *dev, struct device_attribute *attr, char *buf)
1708 {
1709         ide_drive_t *drive = to_ide_device(dev);
1710         return sprintf(buf, "%s\n", media_string(drive));
1711 }
1712
1713 static ssize_t drivename_show(struct device *dev, struct device_attribute *attr, char *buf)
1714 {
1715         ide_drive_t *drive = to_ide_device(dev);
1716         return sprintf(buf, "%s\n", drive->name);
1717 }
1718
1719 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
1720 {
1721         ide_drive_t *drive = to_ide_device(dev);
1722         return sprintf(buf, "ide:m-%s\n", media_string(drive));
1723 }
1724
1725 static struct device_attribute ide_dev_attrs[] = {
1726         __ATTR_RO(media),
1727         __ATTR_RO(drivename),
1728         __ATTR_RO(modalias),
1729         __ATTR_NULL
1730 };
1731
1732 static int ide_uevent(struct device *dev, char **envp, int num_envp,
1733                       char *buffer, int buffer_size)
1734 {
1735         ide_drive_t *drive = to_ide_device(dev);
1736         int i = 0;
1737         int length = 0;
1738
1739         add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
1740                        "MEDIA=%s", media_string(drive));
1741         add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
1742                        "DRIVENAME=%s", drive->name);
1743         add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
1744                        "MODALIAS=ide:m-%s", media_string(drive));
1745         envp[i] = NULL;
1746         return 0;
1747 }
1748
1749 static int generic_ide_probe(struct device *dev)
1750 {
1751         ide_drive_t *drive = to_ide_device(dev);
1752         ide_driver_t *drv = to_ide_driver(dev->driver);
1753
1754         return drv->probe ? drv->probe(drive) : -ENODEV;
1755 }
1756
1757 static int generic_ide_remove(struct device *dev)
1758 {
1759         ide_drive_t *drive = to_ide_device(dev);
1760         ide_driver_t *drv = to_ide_driver(dev->driver);
1761
1762         if (drv->remove)
1763                 drv->remove(drive);
1764
1765         return 0;
1766 }
1767
1768 static void generic_ide_shutdown(struct device *dev)
1769 {
1770         ide_drive_t *drive = to_ide_device(dev);
1771         ide_driver_t *drv = to_ide_driver(dev->driver);
1772
1773         if (dev->driver && drv->shutdown)
1774                 drv->shutdown(drive);
1775 }
1776
1777 struct bus_type ide_bus_type = {
1778         .name           = "ide",
1779         .match          = ide_bus_match,
1780         .uevent         = ide_uevent,
1781         .probe          = generic_ide_probe,
1782         .remove         = generic_ide_remove,
1783         .shutdown       = generic_ide_shutdown,
1784         .dev_attrs      = ide_dev_attrs,
1785         .suspend        = generic_ide_suspend,
1786         .resume         = generic_ide_resume,
1787 };
1788
1789 EXPORT_SYMBOL_GPL(ide_bus_type);
1790
1791 /*
1792  * This is gets invoked once during initialization, to set *everything* up
1793  */
1794 static int __init ide_init(void)
1795 {
1796         int ret;
1797
1798         printk(KERN_INFO "Uniform Multi-Platform E-IDE driver " REVISION "\n");
1799         system_bus_speed = ide_system_bus_speed();
1800
1801         ret = bus_register(&ide_bus_type);
1802         if (ret < 0) {
1803                 printk(KERN_WARNING "IDE: bus_register error: %d\n", ret);
1804                 return ret;
1805         }
1806
1807         init_ide_data();
1808
1809         proc_ide_create();
1810
1811 #ifdef CONFIG_BLK_DEV_ALI14XX
1812         if (probe_ali14xx)
1813                 (void)ali14xx_init();
1814 #endif
1815 #ifdef CONFIG_BLK_DEV_UMC8672
1816         if (probe_umc8672)
1817                 (void)umc8672_init();
1818 #endif
1819 #ifdef CONFIG_BLK_DEV_DTC2278
1820         if (probe_dtc2278)
1821                 (void)dtc2278_init();
1822 #endif
1823 #ifdef CONFIG_BLK_DEV_HT6560B
1824         if (probe_ht6560b)
1825                 (void)ht6560b_init();
1826 #endif
1827 #ifdef CONFIG_BLK_DEV_QD65XX
1828         if (probe_qd65xx)
1829                 (void)qd65xx_init();
1830 #endif
1831
1832         /* Probe for special PCI and other "known" interface chipsets. */
1833         probe_for_hwifs();
1834
1835         return 0;
1836 }
1837
1838 #ifdef MODULE
1839 static char *options = NULL;
1840 module_param(options, charp, 0);
1841 MODULE_LICENSE("GPL");
1842
1843 static void __init parse_options (char *line)
1844 {
1845         char *next = line;
1846
1847         if (line == NULL || !*line)
1848                 return;
1849         while ((line = next) != NULL) {
1850                 if ((next = strchr(line,' ')) != NULL)
1851                         *next++ = 0;
1852                 if (!ide_setup(line))
1853                         printk (KERN_INFO "Unknown option '%s'\n", line);
1854         }
1855 }
1856
1857 int __init init_module (void)
1858 {
1859         parse_options(options);
1860         return ide_init();
1861 }
1862
1863 void __exit cleanup_module (void)
1864 {
1865         int index;
1866
1867         for (index = 0; index < MAX_HWIFS; ++index)
1868                 ide_unregister(index);
1869
1870 #ifdef CONFIG_BLK_DEV_IDEPNP
1871         pnpide_exit();
1872 #endif
1873
1874         proc_ide_destroy();
1875
1876         bus_unregister(&ide_bus_type);
1877 }
1878
1879 #else /* !MODULE */
1880
1881 __setup("", ide_setup);
1882
1883 module_init(ide_init);
1884
1885 #endif /* MODULE */