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