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