~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/ide/ide.c

Version: ~ [ 2.2.5 ] ~ [ 2.4.1 ] ~ [ 2.4.9 ] ~ [ 2.6.17.10 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  *  linux/drivers/ide/ide.c             Version 6.31    June 9, 2000
  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 (usually 14 & 15).
 16  * There can be up to two drives per interface, as per the ATA-2 spec.
 17  *
 18  * Primary:    ide0, port 0x1f0; major=3;  hda is minor=0; hdb is minor=64
 19  * Secondary:  ide1, port 0x170; major=22; hdc is minor=0; hdd is minor=64
 20  * Tertiary:   ide2, port 0x???; major=33; hde is minor=0; hdf is minor=64
 21  * Quaternary: ide3, port 0x???; major=34; hdg is minor=0; hdh is minor=64
 22  * ...
 23  *
 24  *  From hd.c:
 25  *  |
 26  *  | It traverses the request-list, using interrupts to jump between functions.
 27  *  | As nearly all functions can be called within interrupts, we may not sleep.
 28  *  | Special care is recommended.  Have Fun!
 29  *  |
 30  *  | modified by Drew Eckhardt to check nr of hd's from the CMOS.
 31  *  |
 32  *  | Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
 33  *  | in the early extended-partition checks and added DM partitions.
 34  *  |
 35  *  | Early work on error handling by Mika Liljeberg (liljeber@cs.Helsinki.FI).
 36  *  |
 37  *  | IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
 38  *  | and general streamlining by Mark Lord (mlord@pobox.com).
 39  *
 40  *  October, 1994 -- Complete line-by-line overhaul for linux 1.1.x, by:
 41  *
 42  *      Mark Lord       (mlord@pobox.com)               (IDE Perf.Pkg)
 43  *      Delman Lee      (delman@ieee.org)               ("Mr. atdisk2")
 44  *      Scott Snyder    (snyder@fnald0.fnal.gov)        (ATAPI IDE cd-rom)
 45  *
 46  *  This was a rewrite of just about everything from hd.c, though some original
 47  *  code is still sprinkled about.  Think of it as a major evolution, with
 48  *  inspiration from lots of linux users, esp.  hamish@zot.apana.org.au
 49  *
 50  *  Version 1.0 ALPHA   initial code, primary i/f working okay
 51  *  Version 1.3 BETA    dual i/f on shared irq tested & working!
 52  *  Version 1.4 BETA    added auto probing for irq(s)
 53  *  Version 1.5 BETA    added ALPHA (untested) support for IDE cd-roms,
 54  *  ...
 55  * Version 5.50         allow values as small as 20 for idebus=
 56  * Version 5.51         force non io_32bit in drive_cmd_intr()
 57  *                      change delay_10ms() to delay_50ms() to fix problems
 58  * Version 5.52         fix incorrect invalidation of removable devices
 59  *                      add "hdx=slow" command line option
 60  * Version 5.60         start to modularize the driver; the disk and ATAPI
 61  *                       drivers can be compiled as loadable modules.
 62  *                      move IDE probe code to ide-probe.c
 63  *                      move IDE disk code to ide-disk.c
 64  *                      add support for generic IDE device subdrivers
 65  *                      add m68k code from Geert Uytterhoeven
 66  *                      probe all interfaces by default
 67  *                      add ioctl to (re)probe an interface
 68  * Version 6.00         use per device request queues
 69  *                      attempt to optimize shared hwgroup performance
 70  *                      add ioctl to manually adjust bandwidth algorithms
 71  *                      add kerneld support for the probe module
 72  *                      fix bug in ide_error()
 73  *                      fix bug in the first ide_get_lock() call for Atari
 74  *                      don't flush leftover data for ATAPI devices
 75  * Version 6.01         clear hwgroup->active while the hwgroup sleeps
 76  *                      support HDIO_GETGEO for floppies
 77  * Version 6.02         fix ide_ack_intr() call
 78  *                      check partition table on floppies
 79  * Version 6.03         handle bad status bit sequencing in ide_wait_stat()
 80  * Version 6.10         deleted old entries from this list of updates
 81  *                      replaced triton.c with ide-dma.c generic PCI DMA
 82  *                      added support for BIOS-enabled UltraDMA
 83  *                      rename all "promise" things to "pdc4030"
 84  *                      fix EZ-DRIVE handling on small disks
 85  * Version 6.11         fix probe error in ide_scan_devices()
 86  *                      fix ancient "jiffies" polling bugs
 87  *                      mask all hwgroup interrupts on each irq entry
 88  * Version 6.12         integrate ioctl and proc interfaces
 89  *                      fix parsing of "idex=" command line parameter
 90  * Version 6.13         add support for ide4/ide5 courtesy rjones@orchestream.com
 91  * Version 6.14         fixed IRQ sharing among PCI devices
 92  * Version 6.15         added SMP awareness to IDE drivers
 93  * Version 6.16         fixed various bugs; even more SMP friendly
 94  * Version 6.17         fix for newest EZ-Drive problem
 95  * Version 6.18         default unpartitioned-disk translation now "BIOS LBA"
 96  * Version 6.19         Re-design for a UNIFORM driver for all platforms,
 97  *                        model based on suggestions from Russell King and
 98  *                        Geert Uytterhoeven
 99  *                      Promise DC4030VL now supported.
100  *                      add support for ide6/ide7
101  *                      delay_50ms() changed to ide_delay_50ms() and exported.
102  * Version 6.20         Added/Fixed Generic ATA-66 support and hwif detection.
103  *                      Added hdx=flash to allow for second flash disk
104  *                        detection w/o the hang loop.
105  *                      Added support for ide8/ide9
106  *                      Added idex=ata66 for the quirky chipsets that are
107  *                        ATA-66 compliant, but have yet to determine a method
108  *                        of verification of the 80c cable presence.
109  *                        Specifically Promise's PDC20262 chipset.
110  * Version 6.21         Fixing/Fixed SMP spinlock issue with insight from an old
111  *                        hat that clarified original low level driver design.
112  * Version 6.30         Added SMP support; fixed multmode issues.  -ml
113  * Version 6.31         Debug Share INTR's and request queue streaming
114  *                      Native ATA-100 support
115  *                      Prep for Cascades Project
116  *
117  *  Some additional driver compile-time options are in ./include/linux/ide.h
118  *
119  *  To do, in likely order of completion:
120  *      - modify kernel to obtain BIOS geometry for drives on 2nd/3rd/4th i/f
121  *
122  */
123 
124 #define REVISION        "Revision: 6.31"
125 #define VERSION         "Id: ide.c 6.31 2000/06/09"
126 
127 #undef REALLY_SLOW_IO           /* most systems can safely undef this */
128 
129 #define _IDE_C                  /* Tell ide.h it's really us */
130 
131 #include <linux/config.h>
132 #include <linux/module.h>
133 #include <linux/types.h>
134 #include <linux/string.h>
135 #include <linux/kernel.h>
136 #include <linux/timer.h>
137 #include <linux/mm.h>
138 #include <linux/interrupt.h>
139 #include <linux/major.h>
140 #include <linux/errno.h>
141 #include <linux/genhd.h>
142 #include <linux/blkpg.h>
143 #include <linux/malloc.h>
144 #ifndef MODULE
145 #include <linux/init.h>
146 #endif /* MODULE */
147 #include <linux/pci.h>
148 #include <linux/delay.h>
149 #include <linux/ide.h>
150 #include <linux/devfs_fs_kernel.h>
151 
152 #include <asm/byteorder.h>
153 #include <asm/irq.h>
154 #include <asm/uaccess.h>
155 #include <asm/io.h>
156 #include <asm/bitops.h>
157 
158 #include "ide_modes.h"
159 
160 #ifdef CONFIG_KMOD
161 #include <linux/kmod.h>
162 #endif /* CONFIG_KMOD */
163 
164 static const byte ide_hwif_to_major[] = { IDE0_MAJOR, IDE1_MAJOR, IDE2_MAJOR, IDE3_MAJOR, IDE4_MAJOR, IDE5_MAJOR, IDE6_MAJOR, IDE7_MAJOR, IDE8_MAJOR, IDE9_MAJOR };
165 
166 static int      idebus_parameter; /* holds the "idebus=" parameter */
167 static int      system_bus_speed; /* holds what we think is VESA/PCI bus speed */
168 static int      initializing;     /* set while initializing built-in drivers */
169 
170 #ifdef CONFIG_BLK_DEV_IDEPCI
171 static int      ide_scan_direction;     /* THIS was formerly 2.2.x pci=reverse */
172 #endif /* CONFIG_BLK_DEV_IDEPCI */
173 
174 #if defined(__mc68000__) || defined(CONFIG_APUS)
175 /*
176  * ide_lock is used by the Atari code to obtain access to the IDE interrupt,
177  * which is shared between several drivers.
178  */
179 static int      ide_lock;
180 #endif /* __mc68000__ || CONFIG_APUS */
181 
182 /*
183  * ide_modules keeps track of the available IDE chipset/probe/driver modules.
184  */
185 ide_module_t *ide_modules;
186 ide_module_t *ide_probe;
187 
188 /*
189  * This is declared extern in ide.h, for access by other IDE modules:
190  */
191 ide_hwif_t      ide_hwifs[MAX_HWIFS];   /* master data repository */
192 
193 #if (DISK_RECOVERY_TIME > 0)
194 /*
195  * For really screwy hardware (hey, at least it *can* be used with Linux)
196  * we can enforce a minimum delay time between successive operations.
197  */
198 static unsigned long read_timer (void)
199 {
200         unsigned long t, flags;
201         int i;
202 
203         __save_flags(flags);    /* local CPU only */
204         __cli();                /* local CPU only */
205         t = jiffies * 11932;
206         outb_p(0, 0x43);
207         i = inb_p(0x40);
208         i |= inb(0x40) << 8;
209         __restore_flags(flags); /* local CPU only */
210         return (t - i);
211 }
212 #endif /* DISK_RECOVERY_TIME */
213 
214 static inline void set_recovery_timer (ide_hwif_t *hwif)
215 {
216 #if (DISK_RECOVERY_TIME > 0)
217         hwif->last_time = read_timer();
218 #endif /* DISK_RECOVERY_TIME */
219 }
220 
221 /*
222  * Do not even *think* about calling this!
223  */
224 static void init_hwif_data (unsigned int index)
225 {
226         unsigned int unit;
227         hw_regs_t hw;
228         ide_hwif_t *hwif = &ide_hwifs[index];
229 
230         /* bulk initialize hwif & drive info with zeros */
231         memset(hwif, 0, sizeof(ide_hwif_t));
232         memset(&hw, 0, sizeof(hw_regs_t));
233 
234         /* fill in any non-zero initial values */
235         hwif->index     = index;
236         ide_init_hwif_ports(&hw, ide_default_io_base(index), 0, &hwif->irq);
237         memcpy(&hwif->hw, &hw, sizeof(hw));
238         memcpy(hwif->io_ports, hw.io_ports, sizeof(hw.io_ports));
239         hwif->noprobe   = !hwif->io_ports[IDE_DATA_OFFSET];
240 #ifdef CONFIG_BLK_DEV_HD
241         if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA)
242                 hwif->noprobe = 1; /* may be overridden by ide_setup() */
243 #endif /* CONFIG_BLK_DEV_HD */
244         hwif->major     = ide_hwif_to_major[index];
245         hwif->name[0]   = 'i';
246         hwif->name[1]   = 'd';
247         hwif->name[2]   = 'e';
248         hwif->name[3]   = '' + index;
249         for (unit = 0; unit < MAX_DRIVES; ++unit) {
250                 ide_drive_t *drive = &hwif->drives[unit];
251 
252                 drive->media                    = ide_disk;
253                 drive->select.all               = (unit<<4)|0xa0;
254                 drive->hwif                     = hwif;
255                 drive->ctl                      = 0x08;
256                 drive->ready_stat               = READY_STAT;
257                 drive->bad_wstat                = BAD_W_STAT;
258                 drive->special.b.recalibrate    = 1;
259                 drive->special.b.set_geometry   = 1;
260                 drive->name[0]                  = 'h';
261                 drive->name[1]                  = 'd';
262                 drive->name[2]                  = 'a' + (index * MAX_DRIVES) + unit;
263                 init_waitqueue_head(&drive->wqueue);
264         }
265 }
266 
267 /*
268  * init_ide_data() sets reasonable default values into all fields
269  * of all instances of the hwifs and drives, but only on the first call.
270  * Subsequent calls have no effect (they don't wipe out anything).
271  *
272  * This routine is normally called at driver initialization time,
273  * but may also be called MUCH earlier during kernel "command-line"
274  * parameter processing.  As such, we cannot depend on any other parts
275  * of the kernel (such as memory allocation) to be functioning yet.
276  *
277  * This is too bad, as otherwise we could dynamically allocate the
278  * ide_drive_t structs as needed, rather than always consuming memory
279  * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
280  */
281 #define MAGIC_COOKIE 0x12345678
282 static void __init init_ide_data (void)
283 {
284         unsigned int index;
285         static unsigned long magic_cookie = MAGIC_COOKIE;
286 
287         if (magic_cookie != MAGIC_COOKIE)
288                 return;         /* already initialized */
289         magic_cookie = 0;
290 
291         /* Initialise all interface structures */
292         for (index = 0; index < MAX_HWIFS; ++index)
293                 init_hwif_data(index);
294 
295         /* Add default hw interfaces */
296         ide_init_default_hwifs();
297 
298         idebus_parameter = 0;
299         system_bus_speed = 0;
300 }
301 
302 /*
303  * CompactFlash cards and their brethern pretend to be removable hard disks, except:
304  *      (1) they never have a slave unit, and
305  *      (2) they don't have doorlock mechanisms.
306  * This test catches them, and is invoked elsewhere when setting appropriate config bits.
307  *
308  * FIXME: This treatment is probably applicable for *all* PCMCIA (PC CARD) devices,
309  * so in linux 2.3.x we should change this to just treat all PCMCIA drives this way,
310  * and get rid of the model-name tests below (too big of an interface change for 2.2.x).
311  * At that time, we might also consider parameterizing the timeouts and retries,
312  * since these are MUCH faster than mechanical drives.  -M.Lord
313  */
314 int drive_is_flashcard (ide_drive_t *drive)
315 {
316         struct hd_driveid *id = drive->id;
317 
318         if (drive->removable && id != NULL) {
319                 if (id->config == 0x848a) return 1;     /* CompactFlash */
320                 if (!strncmp(id->model, "KODAK ATA_FLASH", 15)  /* Kodak */
321                  || !strncmp(id->model, "Hitachi CV", 10)       /* Hitachi */
322                  || !strncmp(id->model, "SunDisk SDCFB", 13)    /* SunDisk */
323                  || !strncmp(id->model, "HAGIWARA HPC", 12)     /* Hagiwara */
324                  || !strncmp(id->model, "LEXAR ATA_FLASH", 15)  /* Lexar */
325                  || !strncmp(id->model, "ATA_FLASH", 9))        /* Simple Tech */
326                 {
327                         return 1;       /* yes, it is a flash memory card */
328                 }
329         }
330         return 0;       /* no, it is not a flash memory card */
331 }
332 
333 /*
334  * ide_system_bus_speed() returns what we think is the system VESA/PCI
335  * bus speed (in MHz).  This is used for calculating interface PIO timings.
336  * The default is 40 for known PCI systems, 50 otherwise.
337  * The "idebus=xx" parameter can be used to override this value.
338  * The actual value to be used is computed/displayed the first time through.
339  */
340 int ide_system_bus_speed (void)
341 {
342         if (!system_bus_speed) {
343                 if (idebus_parameter)
344                         system_bus_speed = idebus_parameter;    /* user supplied value */
345 #ifdef CONFIG_PCI
346                 else if (pci_present())
347                         system_bus_speed = 33;  /* safe default value for PCI */
348 #endif /* CONFIG_PCI */
349                 else
350                         system_bus_speed = 50;  /* safe default value for VESA and PCI */
351                 printk("ide: Assuming %dMHz system bus speed for PIO modes%s\n", system_bus_speed,
352                         idebus_parameter ? "" : "; override with idebus=xx");
353         }
354         return system_bus_speed;
355 }
356 
357 #if SUPPORT_VLB_SYNC
358 /*
359  * Some localbus EIDE interfaces require a special access sequence
360  * when using 32-bit I/O instructions to transfer data.  We call this
361  * the "vlb_sync" sequence, which consists of three successive reads
362  * of the sector count register location, with interrupts disabled
363  * to ensure that the reads all happen together.
364  */
365 static inline void do_vlb_sync (ide_ioreg_t port) {
366         (void) inb (port);
367         (void) inb (port);
368         (void) inb (port);
369 }
370 #endif /* SUPPORT_VLB_SYNC */
371 
372 /*
373  * This is used for most PIO data transfers *from* the IDE interface
374  */
375 void ide_input_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
376 {
377         byte io_32bit = drive->io_32bit;
378 
379         if (io_32bit) {
380 #if SUPPORT_VLB_SYNC
381                 if (io_32bit & 2) {
382                         unsigned long flags;
383                         __save_flags(flags);    /* local CPU only */
384                         __cli();                /* local CPU only */
385                         do_vlb_sync(IDE_NSECTOR_REG);
386                         insl(IDE_DATA_REG, buffer, wcount);
387                         __restore_flags(flags); /* local CPU only */
388                 } else
389 #endif /* SUPPORT_VLB_SYNC */
390                         insl(IDE_DATA_REG, buffer, wcount);
391         } else {
392 #if SUPPORT_SLOW_DATA_PORTS
393                 if (drive->slow) {
394                         unsigned short *ptr = (unsigned short *) buffer;
395                         while (wcount--) {
396                                 *ptr++ = inw_p(IDE_DATA_REG);
397                                 *ptr++ = inw_p(IDE_DATA_REG);
398                         }
399                 } else
400 #endif /* SUPPORT_SLOW_DATA_PORTS */
401                         insw(IDE_DATA_REG, buffer, wcount<<1);
402         }
403 }
404 
405 /*
406  * This is used for most PIO data transfers *to* the IDE interface
407  */
408 void ide_output_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
409 {
410         byte io_32bit = drive->io_32bit;
411 
412         if (io_32bit) {
413 #if SUPPORT_VLB_SYNC
414                 if (io_32bit & 2) {
415                         unsigned long flags;
416                         __save_flags(flags);    /* local CPU only */
417                         __cli();                /* local CPU only */
418                         do_vlb_sync(IDE_NSECTOR_REG);
419                         outsl(IDE_DATA_REG, buffer, wcount);
420                         __restore_flags(flags); /* local CPU only */
421                 } else
422 #endif /* SUPPORT_VLB_SYNC */
423                         outsl(IDE_DATA_REG, buffer, wcount);
424         } else {
425 #if SUPPORT_SLOW_DATA_PORTS
426                 if (drive->slow) {
427                         unsigned short *ptr = (unsigned short *) buffer;
428                         while (wcount--) {
429                                 outw_p(*ptr++, IDE_DATA_REG);
430                                 outw_p(*ptr++, IDE_DATA_REG);
431                         }
432                 } else
433 #endif /* SUPPORT_SLOW_DATA_PORTS */
434                         outsw(IDE_DATA_REG, buffer, wcount<<1);
435         }
436 }
437 
438 /*
439  * The following routines are mainly used by the ATAPI drivers.
440  *
441  * These routines will round up any request for an odd number of bytes,
442  * so if an odd bytecount is specified, be sure that there's at least one
443  * extra byte allocated for the buffer.
444  */
445 void atapi_input_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount)
446 {
447         ++bytecount;
448 #if defined(CONFIG_ATARI) || defined(CONFIG_Q40)
449         if (MACH_IS_ATARI || MACH_IS_Q40) {
450                 /* Atari has a byte-swapped IDE interface */
451                 insw_swapw(IDE_DATA_REG, buffer, bytecount / 2);
452                 return;
453         }
454 #endif /* CONFIG_ATARI */
455         ide_input_data (drive, buffer, bytecount / 4);
456         if ((bytecount & 0x03) >= 2)
457                 insw (IDE_DATA_REG, ((byte *)buffer) + (bytecount & ~0x03), 1);
458 }
459 
460 void atapi_output_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount)
461 {
462         ++bytecount;
463 #if defined(CONFIG_ATARI) || defined(CONFIG_Q40)
464         if (MACH_IS_ATARI || MACH_IS_Q40) {
465                 /* Atari has a byte-swapped IDE interface */
466                 outsw_swapw(IDE_DATA_REG, buffer, bytecount / 2);
467                 return;
468         }
469 #endif /* CONFIG_ATARI */
470         ide_output_data (drive, buffer, bytecount / 4);
471         if ((bytecount & 0x03) >= 2)
472                 outsw (IDE_DATA_REG, ((byte *)buffer) + (bytecount & ~0x03), 1);
473 }
474 
475 /*
476  * Needed for PCI irq sharing
477  */
478 static inline int drive_is_ready (ide_drive_t *drive)
479 {
480         byte stat = 0;
481         if (drive->waiting_for_dma)
482                 return HWIF(drive)->dmaproc(ide_dma_test_irq, drive);
483 #if 0
484         udelay(1);      /* need to guarantee 400ns since last command was issued */
485 #endif
486 
487 #ifdef CONFIG_IDEPCI_SHARE_IRQ
488         /*
489          * We do a passive status test under shared PCI interrupts on
490          * cards that truly share the ATA side interrupt, but may also share
491          * an interrupt with another pci card/device.  We make no assumptions
492          * about possible isa-pnp and pci-pnp issues yet.
493          */
494         if (IDE_CONTROL_REG)
495                 stat = GET_ALTSTAT();
496         else
497 #endif /* CONFIG_IDEPCI_SHARE_IRQ */
498         stat = GET_STAT();      /* Note: this may clear a pending IRQ!! */
499 
500         if (stat & BUSY_STAT)
501                 return 0;       /* drive busy:  definitely not interrupting */
502         return 1;               /* drive ready: *might* be interrupting */
503 }
504 
505 /*
506  * This is our end_request replacement function.
507  */
508 void ide_end_request (byte uptodate, ide_hwgroup_t *hwgroup)
509 {
510         struct request *rq;
511         unsigned long flags;
512 
513         spin_lock_irqsave(&io_request_lock, flags);
514         rq = hwgroup->rq;
515 
516         if (!end_that_request_first(rq, uptodate, hwgroup->drive->name)) {
517                 add_blkdev_randomness(MAJOR(rq->rq_dev));
518                 blkdev_dequeue_request(rq);
519                 hwgroup->rq = NULL;
520                 end_that_request_last(rq);
521         }
522         spin_unlock_irqrestore(&io_request_lock, flags);
523 }
524 
525 /*
526  * This should get invoked any time we exit the driver to
527  * wait for an interrupt response from a drive.  handler() points
528  * at the appropriate code to handle the next interrupt, and a
529  * timer is started to prevent us from waiting forever in case
530  * something goes wrong (see the ide_timer_expiry() handler later on).
531  */
532 void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
533                       unsigned int timeout, ide_expiry_t *expiry)
534 {
535         unsigned long flags;
536         ide_hwgroup_t *hwgroup = HWGROUP(drive);
537 
538         spin_lock_irqsave(&io_request_lock, flags);
539         if (hwgroup->handler != NULL) {
540                 printk("%s: ide_set_handler: handler not null; old=%p, new=%p\n",
541                         drive->name, hwgroup->handler, handler);
542         }
543         hwgroup->handler        = handler;
544         hwgroup->expiry         = expiry;
545         hwgroup->timer.expires  = jiffies + timeout;
546         add_timer(&hwgroup->timer);
547         spin_unlock_irqrestore(&io_request_lock, flags);
548 }
549 
550 /*
551  * current_capacity() returns the capacity (in sectors) of a drive
552  * according to its current geometry/LBA settings.
553  */
554 unsigned long current_capacity (ide_drive_t *drive)
555 {
556         if (!drive->present)
557                 return 0;
558         if (drive->driver != NULL)
559                 return DRIVER(drive)->capacity(drive);
560         return 0;
561 }
562 
563 extern struct block_device_operations ide_fops[];
564 /*
565  * ide_geninit() is called exactly *once* for each interface.
566  */
567 void ide_geninit (ide_hwif_t *hwif)
568 {
569         unsigned int unit;
570         struct gendisk *gd = hwif->gd;
571 
572         for (unit = 0; unit < MAX_DRIVES; ++unit) {
573                 ide_drive_t *drive = &hwif->drives[unit];
574 
575                 if (!drive->present)
576                         continue;
577                 if (drive->media!=ide_disk && drive->media!=ide_floppy)
578                         continue;
579                 register_disk(gd,MKDEV(hwif->major,unit<<PARTN_BITS),
580 #ifdef CONFIG_BLK_DEV_ISAPNP
581                         (drive->forced_geom && drive->noprobe) ? 1 :
582 #endif /* CONFIG_BLK_DEV_ISAPNP */
583                         1<<PARTN_BITS, ide_fops,
584                         current_capacity(drive));
585         }
586 }
587 
588 static ide_startstop_t do_reset1 (ide_drive_t *, int);          /* needed below */
589 
590 /*
591  * atapi_reset_pollfunc() gets invoked to poll the interface for completion every 50ms
592  * during an atapi drive reset operation. If the drive has not yet responded,
593  * and we have not yet hit our maximum waiting time, then the timer is restarted
594  * for another 50ms.
595  */
596 static ide_startstop_t atapi_reset_pollfunc (ide_drive_t *drive)
597 {
598         ide_hwgroup_t *hwgroup = HWGROUP(drive);
599         byte stat;
600 
601         SELECT_DRIVE(HWIF(drive),drive);
602         udelay (10);
603 
604         if (OK_STAT(stat=GET_STAT(), 0, BUSY_STAT)) {
605                 printk("%s: ATAPI reset complete\n", drive->name);
606         } else {
607                 if (0 < (signed long)(hwgroup->poll_timeout - jiffies)) {
608                         ide_set_handler (drive, &atapi_reset_pollfunc, HZ/20, NULL);
609                         return ide_started;     /* continue polling */
610                 }
611                 hwgroup->poll_timeout = 0;      /* end of polling */
612                 printk("%s: ATAPI reset timed-out, status=0x%02x\n", drive->name, stat);
613                 return do_reset1 (drive, 1);    /* do it the old fashioned way */
614         }
615         hwgroup->poll_timeout = 0;      /* done polling */
616         return ide_stopped;
617 }
618 
619 /*
620  * reset_pollfunc() gets invoked to poll the interface for completion every 50ms
621  * during an ide reset operation. If the drives have not yet responded,
622  * and we have not yet hit our maximum waiting time, then the timer is restarted
623  * for another 50ms.
624  */
625 static ide_startstop_t reset_pollfunc (ide_drive_t *drive)
626 {
627         ide_hwgroup_t *hwgroup = HWGROUP(drive);
628         ide_hwif_t *hwif = HWIF(drive);
629         byte tmp;
630 
631         if (!OK_STAT(tmp=GET_STAT(), 0, BUSY_STAT)) {
632                 if (0 < (signed long)(hwgroup->poll_timeout - jiffies)) {
633                         ide_set_handler (drive, &reset_pollfunc, HZ/20, NULL);
634                         return ide_started;     /* continue polling */
635                 }
636                 printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
637         } else  {
638                 printk("%s: reset: ", hwif->name);
639                 if ((tmp = GET_ERR()) == 1)
640                         printk("success\n");
641                 else {
642 #if FANCY_STATUS_DUMPS
643                         printk("master: ");
644                         switch (tmp & 0x7f) {
645                                 case 1: printk("passed");
646                                         break;
647                                 case 2: printk("formatter device error");
648                                         break;
649                                 case 3: printk("sector buffer error");
650                                         break;
651                                 case 4: printk("ECC circuitry error");
652                                         break;
653                                 case 5: printk("controlling MPU error");
654                                         break;
655                                 default:printk("error (0x%02x?)", tmp);
656                         }
657                         if (tmp & 0x80)
658                                 printk("; slave: failed");
659                         printk("\n");
660 #else
661                         printk("failed\n");
662 #endif /* FANCY_STATUS_DUMPS */
663                 }
664         }
665         hwgroup->poll_timeout = 0;      /* done polling */
666         return ide_stopped;
667 }
668 
669 static void check_dma_crc (ide_drive_t *drive)
670 {
671         if (drive->crc_count) {
672                 (void) HWIF(drive)->dmaproc(ide_dma_off_quietly, drive);
673                 if ((HWIF(drive)->speedproc) != NULL)
674                         HWIF(drive)->speedproc(drive, ide_auto_reduce_xfer(drive));
675                 if (drive->current_speed >= XFER_SW_DMA_0)
676                         (void) HWIF(drive)->dmaproc(ide_dma_on, drive);
677         } else {
678                 (void) HWIF(drive)->dmaproc(ide_dma_off, drive);
679         }
680 }
681 
682 static void pre_reset (ide_drive_t *drive)
683 {
684         if (drive->driver != NULL)
685                 DRIVER(drive)->pre_reset(drive);
686 
687         if (!drive->keep_settings) {
688                 if (drive->using_dma) {
689                         check_dma_crc(drive);
690                 } else {
691                         drive->unmask = 0;
692                         drive->io_32bit = 0;
693                 }
694                 return;
695         }
696         if (drive->using_dma)
697                 check_dma_crc(drive);
698 }
699 
700 /*
701  * do_reset1() attempts to recover a confused drive by resetting it.
702  * Unfortunately, resetting a disk drive actually resets all devices on
703  * the same interface, so it can really be thought of as resetting the
704  * interface rather than resetting the drive.
705  *
706  * ATAPI devices have their own reset mechanism which allows them to be
707  * individually reset without clobbering other devices on the same interface.
708  *
709  * Unfortunately, the IDE interface does not generate an interrupt to let
710  * us know when the reset operation has finished, so we must poll for this.
711  * Equally poor, though, is the fact that this may a very long time to complete,
712  * (up to 30 seconds worstcase).  So, instead of busy-waiting here for it,
713  * we set a timer to poll at 50ms intervals.
714  */
715 static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
716 {
717         unsigned int unit;
718         unsigned long flags;
719         ide_hwif_t *hwif = HWIF(drive);
720         ide_hwgroup_t *hwgroup = HWGROUP(drive);
721 
722         __save_flags(flags);    /* local CPU only */
723         __cli();                /* local CPU only */
724 
725         /* For an ATAPI device, first try an ATAPI SRST. */
726         if (drive->media != ide_disk && !do_not_try_atapi) {
727                 pre_reset(drive);
728                 SELECT_DRIVE(hwif,drive);
729                 udelay (20);
730                 OUT_BYTE (WIN_SRST, IDE_COMMAND_REG);
731                 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
732                 ide_set_handler (drive, &atapi_reset_pollfunc, HZ/20, NULL);
733                 __restore_flags (flags);        /* local CPU only */
734                 return ide_started;
735         }
736 
737         /*
738          * First, reset any device state data we were maintaining
739          * for any of the drives on this interface.
740          */
741         for (unit = 0; unit < MAX_DRIVES; ++unit)
742                 pre_reset(&hwif->drives[unit]);
743 
744 #if OK_TO_RESET_CONTROLLER
745         if (!IDE_CONTROL_REG) {
746                 __restore_flags(flags);
747                 return ide_stopped;
748         }
749         /*
750          * Note that we also set nIEN while resetting the device,
751          * to mask unwanted interrupts from the interface during the reset.
752          * However, due to the design of PC hardware, this will cause an
753          * immediate interrupt due to the edge transition it produces.
754          * This single interrupt gives us a "fast poll" for drives that
755          * recover from reset very quickly, saving us the first 50ms wait time.
756          */
757         OUT_BYTE(drive->ctl|6,IDE_CONTROL_REG); /* set SRST and nIEN */
758         udelay(10);                     /* more than enough time */
759         OUT_BYTE(drive->ctl|2,IDE_CONTROL_REG); /* clear SRST, leave nIEN */
760         udelay(10);                     /* more than enough time */
761         hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
762         ide_set_handler (drive, &reset_pollfunc, HZ/20, NULL);
763 
764         /*
765          * Some weird controller like resetting themselves to a strange
766          * state when the disks are reset this way. At least, the Winbond
767          * 553 documentation says that
768          */
769         if (hwif->resetproc != NULL)
770                 hwif->resetproc(drive);
771 
772 #endif  /* OK_TO_RESET_CONTROLLER */
773 
774         __restore_flags (flags);        /* local CPU only */
775         return ide_started;
776 }
777 
778 /*
779  * ide_do_reset() is the entry point to the drive/interface reset code.
780  */
781 ide_startstop_t ide_do_reset (ide_drive_t *drive)
782 {
783         return do_reset1 (drive, 0);
784 }
785 
786 /*
787  * Clean up after success/failure of an explicit drive cmd
788  */
789 void ide_end_drive_cmd (ide_drive_t *drive, byte stat, byte err)
790 {
791         unsigned long flags;
792         struct request *rq = HWGROUP(drive)->rq;
793 
794         if (rq->cmd == IDE_DRIVE_CMD) {
795                 byte *args = (byte *) rq->buffer;
796                 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
797                 if (args) {
798                         args[0] = stat;
799                         args[1] = err;
800                         args[2] = IN_BYTE(IDE_NSECTOR_REG);
801                 }
802         } else if (rq->cmd == IDE_DRIVE_TASK) {
803                 byte *args = (byte *) rq->buffer;
804                 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
805                 if (args) {
806                         args[0] = stat;
807                         args[1] = err;
808                         args[2] = IN_BYTE(IDE_NSECTOR_REG);
809                         args[3] = IN_BYTE(IDE_SECTOR_REG);
810                         args[4] = IN_BYTE(IDE_LCYL_REG);
811                         args[5] = IN_BYTE(IDE_HCYL_REG);
812                         args[6] = IN_BYTE(IDE_SELECT_REG);
813                 }
814         }
815         spin_lock_irqsave(&io_request_lock, flags);
816         blkdev_dequeue_request(rq);
817         HWGROUP(drive)->rq = NULL;
818         blkdev_release_request(rq);
819         spin_unlock_irqrestore(&io_request_lock, flags);
820         if (rq->sem != NULL)
821                 up(rq->sem);    /* inform originator that rq has been serviced */
822 }
823 
824 /*
825  * Error reporting, in human readable form (luxurious, but a memory hog).
826  */
827 byte ide_dump_status (ide_drive_t *drive, const char *msg, byte stat)
828 {
829         unsigned long flags;
830         byte err = 0;
831 
832         __save_flags (flags);   /* local CPU only */
833         ide__sti();             /* local CPU only */
834         printk("%s: %s: status=0x%02x", drive->name, msg, stat);
835 #if FANCY_STATUS_DUMPS
836         printk(" { ");
837         if (stat & BUSY_STAT)
838                 printk("Busy ");
839         else {
840                 if (stat & READY_STAT)  printk("DriveReady ");
841                 if (stat & WRERR_STAT)  printk("DeviceFault ");
842                 if (stat & SEEK_STAT)   printk("SeekComplete ");
843                 if (stat & DRQ_STAT)    printk("DataRequest ");
844                 if (stat & ECC_STAT)    printk("CorrectedError ");
845                 if (stat & INDEX_STAT)  printk("Index ");
846                 if (stat & ERR_STAT)    printk("Error ");
847         }
848         printk("}");
849 #endif  /* FANCY_STATUS_DUMPS */
850         printk("\n");
851         if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
852                 err = GET_ERR();
853                 printk("%s: %s: error=0x%02x", drive->name, msg, err);
854 #if FANCY_STATUS_DUMPS
855                 if (drive->media == ide_disk) {
856                         printk(" { ");
857                         if (err & ABRT_ERR)     printk("DriveStatusError ");
858                         if (err & ICRC_ERR)     printk("%s", (err & ABRT_ERR) ? "BadCRC " : "BadSector ");
859                         if (err & ECC_ERR)      printk("UncorrectableError ");
860                         if (err & ID_ERR)       printk("SectorIdNotFound ");
861                         if (err & TRK0_ERR)     printk("TrackZeroNotFound ");
862                         if (err & MARK_ERR)     printk("AddrMarkNotFound ");
863                         printk("}");
864                         if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR || (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
865                                 byte cur = IN_BYTE(IDE_SELECT_REG);
866                                 if (cur & 0x40) {       /* using LBA? */
867                                         printk(", LBAsect=%ld", (unsigned long)
868                                          ((cur&0xf)<<24)
869                                          |(IN_BYTE(IDE_HCYL_REG)<<16)
870                                          |(IN_BYTE(IDE_LCYL_REG)<<8)
871                                          | IN_BYTE(IDE_SECTOR_REG));
872                                 } else {
873                                         printk(", CHS=%d/%d/%d",
874                                          (IN_BYTE(IDE_HCYL_REG)<<8) +
875                                           IN_BYTE(IDE_LCYL_REG),
876                                           cur & 0xf,
877                                           IN_BYTE(IDE_SECTOR_REG));
878                                 }
879                                 if (HWGROUP(drive)->rq)
880                                         printk(", sector=%ld", HWGROUP(drive)->rq->sector);
881                         }
882                 }
883 #endif  /* FANCY_STATUS_DUMPS */
884                 printk("\n");
885         }
886         __restore_flags (flags);        /* local CPU only */
887         return err;
888 }
889 
890 /*
891  * try_to_flush_leftover_data() is invoked in response to a drive
892  * unexpectedly having its DRQ_STAT bit set.  As an alternative to
893  * resetting the drive, this routine tries to clear the condition
894  * by read a sector's worth of data from the drive.  Of course,
895  * this may not help if the drive is *waiting* for data from *us*.
896  */
897 static void try_to_flush_leftover_data (ide_drive_t *drive)
898 {
899         int i = (drive->mult_count ? drive->mult_count : 1) * SECTOR_WORDS;
900 
901         if (drive->media != ide_disk)
902                 return;
903         while (i > 0) {
904                 u32 buffer[16];
905                 unsigned int wcount = (i > 16) ? 16 : i;
906                 i -= wcount;
907                 ide_input_data (drive, buffer, wcount);
908         }
909 }
910 
911 /*
912  * ide_error() takes action based on the error returned by the drive.
913  */
914 ide_startstop_t ide_error (ide_drive_t *drive, const char *msg, byte stat)
915 {
916         struct request *rq;
917         byte err;
918 
919         err = ide_dump_status(drive, msg, stat);
920         if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
921                 return ide_stopped;
922         /* retry only "normal" I/O: */
923         if (rq->cmd == IDE_DRIVE_CMD || rq->cmd == IDE_DRIVE_TASK) {
924                 rq->errors = 1;
925                 ide_end_drive_cmd(drive, stat, err);
926                 return ide_stopped;
927         }
928         if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) { /* other bits are useless when BUSY */
929                 rq->errors |= ERROR_RESET;
930         } else {
931                 if (drive->media == ide_disk && (stat & ERR_STAT)) {
932                         /* err has different meaning on cdrom and tape */
933                         if (err == ABRT_ERR) {
934                                 if (drive->select.b.lba && IN_BYTE(IDE_COMMAND_REG) == WIN_SPECIFY)
935                                         return ide_stopped; /* some newer drives don't support WIN_SPECIFY */
936                         } else if ((err & (ABRT_ERR | ICRC_ERR)) == (ABRT_ERR | ICRC_ERR)) {
937                                 drive->crc_count++; /* UDMA crc error -- just retry the operation */
938                         } else if (err & (BBD_ERR | ECC_ERR))   /* retries won't help these */
939                                 rq->errors = ERROR_MAX;
940                         else if (err & TRK0_ERR)        /* help it find track zero */
941                                 rq->errors |= ERROR_RECAL;
942                 }
943                 if ((stat & DRQ_STAT) && rq->cmd != WRITE)
944                         try_to_flush_leftover_data(drive);
945         }
946         if (GET_STAT() & (BUSY_STAT|DRQ_STAT))
947                 OUT_BYTE(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);    /* force an abort */
948 
949         if (rq->errors >= ERROR_MAX) {
950                 if (drive->driver != NULL)
951                         DRIVER(drive)->end_request(0, HWGROUP(drive));
952                 else
953                         ide_end_request(0, HWGROUP(drive));
954         } else {
955                 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
956                         ++rq->errors;
957                         return ide_do_reset(drive);
958                 }
959                 if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
960                         drive->special.b.recalibrate = 1;
961                 ++rq->errors;
962         }
963         return ide_stopped;
964 }
965 
966 /*
967  * Issue a simple drive command
968  * The drive must be selected beforehand.
969  */
970 void ide_cmd (ide_drive_t *drive, byte cmd, byte nsect, ide_handler_t *handler)
971 {
972         ide_set_handler (drive, handler, WAIT_CMD, NULL);
973         if (IDE_CONTROL_REG)
974                 OUT_BYTE(drive->ctl,IDE_CONTROL_REG);   /* clear nIEN */
975         SELECT_MASK(HWIF(drive),drive,0);
976         OUT_BYTE(nsect,IDE_NSECTOR_REG);
977         OUT_BYTE(cmd,IDE_COMMAND_REG);
978 }
979 
980 /*
981  * drive_cmd_intr() is invoked on completion of a special DRIVE_CMD.
982  */
983 static ide_startstop_t drive_cmd_intr (ide_drive_t *drive)
984 {
985         struct request *rq = HWGROUP(drive)->rq;
986         byte *args = (byte *) rq->buffer;
987         byte stat = GET_STAT();
988         int retries = 10;
989 
990         ide__sti();     /* local CPU only */
991         if ((stat & DRQ_STAT) && args && args[3]) {
992                 byte io_32bit = drive->io_32bit;
993                 drive->io_32bit = 0;
994                 ide_input_data(drive, &args[4], args[3] * SECTOR_WORDS);
995                 drive->io_32bit = io_32bit;
996                 while (((stat = GET_STAT()) & BUSY_STAT) && retries--)
997                         udelay(100);
998         }
999 
1000         if (!OK_STAT(stat, READY_STAT, BAD_STAT))
1001                 return ide_error(drive, "drive_cmd", stat); /* calls ide_end_drive_cmd */
1002         ide_end_drive_cmd (drive, stat, GET_ERR());
1003         return ide_stopped;
1004 }
1005 
1006 /*
1007  * do_special() is used to issue WIN_SPECIFY, WIN_RESTORE, and WIN_SETMULT
1008  * commands to a drive.  It used to do much more, but has been scaled back.
1009  */
1010 static ide_startstop_t do_special (ide_drive_t *drive)
1011 {
1012         special_t *s = &drive->special;
1013 
1014 #ifdef DEBUG
1015         printk("%s: do_special: 0x%02x\n", drive->name, s->all);
1016 #endif
1017         if (s->b.set_tune) {
1018                 ide_tuneproc_t *tuneproc = HWIF(drive)->tuneproc;
1019                 s->b.set_tune = 0;
1020                 if (tuneproc != NULL)
1021                         tuneproc(drive, drive->tune_req);
1022         } else if (drive->driver != NULL) {
1023                 return DRIVER(drive)->special(drive);
1024         } else if (s->all) {
1025                 printk("%s: bad special flag: 0x%02x\n", drive->name, s->all);
1026                 s->all = 0;
1027         }
1028         return ide_stopped;
1029 }
1030 
1031 /*
1032  * This routine busy-waits for the drive status to be not "busy".
1033  * It then checks the status for all of the "good" bits and none
1034  * of the "bad" bits, and if all is okay it returns 0.  All other
1035  * cases return 1 after invoking ide_error() -- caller should just return.
1036  *
1037  * This routine should get fixed to not hog the cpu during extra long waits..
1038  * That could be done by busy-waiting for the first jiffy or two, and then
1039  * setting a timer to wake up at half second intervals thereafter,
1040  * until timeout is achieved, before timing out.
1041  */
1042 int ide_wait_stat (ide_startstop_t *startstop, ide_drive_t *drive, byte good, byte bad, unsigned long timeout) {
1043         byte stat;
1044         int i;
1045         unsigned long flags;
1046  
1047         udelay(1);      /* spec allows drive 400ns to assert "BUSY" */
1048         if ((stat = GET_STAT()) & BUSY_STAT) {
1049                 __save_flags(flags);    /* local CPU only */
1050                 ide__sti();             /* local CPU only */
1051                 timeout += jiffies;
1052                 while ((stat = GET_STAT()) & BUSY_STAT) {
1053                         if (0 < (signed long)(jiffies - timeout)) {
1054                                 __restore_flags(flags); /* local CPU only */
1055                                 *startstop = ide_error(drive, "status timeout", stat);
1056                                 return 1;
1057                         }
1058                 }
1059                 __restore_flags(flags); /* local CPU only */
1060         }
1061         /*
1062          * Allow status to settle, then read it again.
1063          * A few rare drives vastly violate the 400ns spec here,
1064          * so we'll wait up to 10usec for a "good" status
1065          * rather than expensively fail things immediately.
1066          * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
1067          */
1068         for (i = 0; i < 10; i++) {
1069                 udelay(1);
1070                 if (OK_STAT((stat = GET_STAT()), good, bad))
1071                         return 0;
1072         }
1073         *startstop = ide_error(drive, "status error", stat);
1074         return 1;
1075 }
1076 
1077 /*
1078  * execute_drive_cmd() issues a special drive command,
1079  * usually initiated by ioctl() from the external hdparm program.
1080  */
1081 static ide_startstop_t execute_drive_cmd (ide_drive_t *drive, struct request *rq)
1082 {
1083         byte *args = rq->buffer;
1084         if (args && rq->cmd == IDE_DRIVE_TASK) {
1085                 byte sel;
1086 #ifdef DEBUG
1087                 printk("%s: DRIVE_TASK_CMD data=x%02x cmd=0x%02x fr=0x%02x ns=0x%02x sc=0x%02x lcyl=0x%02x hcyl=0x%02x sel=0x%02x\n",
1088                         drive->name, args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
1089 #endif
1090                 OUT_BYTE(args[1], IDE_FEATURE_REG);
1091                 OUT_BYTE(args[3], IDE_SECTOR_REG);
1092                 OUT_BYTE(args[4], IDE_LCYL_REG);
1093                 OUT_BYTE(args[5], IDE_HCYL_REG);
1094                 sel = (args[6] & ~0x10);
1095                 if (drive->select.b.unit)
1096                         sel |= 0x10;
1097                 OUT_BYTE(sel, IDE_SELECT_REG);
1098                 ide_cmd(drive, args[0], args[2], &drive_cmd_intr);
1099                 return ide_started;
1100         } else if (args) {
1101 #ifdef DEBUG
1102                 printk("%s: DRIVE_CMD cmd=0x%02x sc=0x%02x fr=0x%02x xx=0x%02x\n",
1103                  drive->name, args[0], args[1], args[2], args[3]);
1104 #endif
1105                 if (args[0] == WIN_SMART) {
1106                         OUT_BYTE(0x4f, IDE_LCYL_REG);
1107                         OUT_BYTE(0xc2, IDE_HCYL_REG);
1108                         OUT_BYTE(args[2],IDE_FEATURE_REG);
1109                         OUT_BYTE(args[1],IDE_SECTOR_REG);
1110                         ide_cmd(drive, args[0], args[3], &drive_cmd_intr);
1111                         return ide_started;
1112                 }
1113                 OUT_BYTE(args[2],IDE_FEATURE_REG);
1114                 ide_cmd(drive, args[0], args[1], &drive_cmd_intr);
1115                 return ide_started;
1116         } else {
1117                 /*
1118                  * NULL is actually a valid way of waiting for
1119                  * all current requests to be flushed from the queue.
1120                  */
1121 #ifdef DEBUG
1122                 printk("%s: DRIVE_CMD (null)\n", drive->name);
1123 #endif
1124                 ide_end_drive_cmd(drive, GET_STAT(), GET_ERR());
1125                 return ide_stopped;
1126         }
1127 }
1128 
1129 /*
1130  * start_request() initiates handling of a new I/O request
1131  */
1132 static ide_startstop_t start_request (ide_drive_t *drive)
1133 {
1134         ide_startstop_t startstop;
1135         unsigned long block, blockend;
1136         struct request *rq = blkdev_entry_next_request(&drive->queue.queue_head);
1137         unsigned int minor = MINOR(rq->rq_dev), unit = minor >> PARTN_BITS;
1138         ide_hwif_t *hwif = HWIF(drive);
1139 
1140 #ifdef DEBUG
1141         printk("%s: start_request: current=0x%08lx\n", hwif->name, (unsigned long) rq);
1142 #endif
1143         if (unit >= MAX_DRIVES) {
1144                 printk("%s: bad device number: %s\n", hwif->name, kdevname(rq->rq_dev));
1145                 goto kill_rq;
1146         }
1147 #ifdef DEBUG
1148         if (rq->bh && !buffer_locked(rq->bh)) {
1149                 printk("%s: block not locked\n", drive->name);
1150                 goto kill_rq;
1151         }
1152 #endif
1153         block    = rq->sector;
1154         blockend = block + rq->nr_sectors;
1155 
1156         if ((rq->cmd == READ || rq->cmd == WRITE) &&
1157             (drive->media == ide_disk || drive->media == ide_floppy)) {
1158                 if ((blockend < block) || (blockend > drive->part[minor&PARTN_MASK].nr_sects)) {
1159                         printk("%s%c: bad access: block=%ld, count=%ld\n", drive->name,
1160                          (minor&PARTN_MASK)?''+(minor&PARTN_MASK):' ', block, rq->nr_sectors);
1161                         goto kill_rq;
1162                 }
1163                 block += drive->part[minor&PARTN_MASK].start_sect + drive->sect0;
1164         }
1165         /* Yecch - this will shift the entire interval,
1166            possibly killing some innocent following sector */
1167         if (block == 0 && drive->remap_0_to_1 == 1)
1168                 block = 1;  /* redirect MBR access to EZ-Drive partn table */
1169 
1170 #if (DISK_RECOVERY_TIME > 0)
1171         while ((read_timer() - hwif->last_time) < DISK_RECOVERY_TIME);
1172 #endif
1173 
1174         SELECT_DRIVE(hwif, drive);
1175         if (ide_wait_stat(&startstop, drive, drive->ready_stat, BUSY_STAT|DRQ_STAT, WAIT_READY)) {
1176                 printk("%s: drive not ready for command\n", drive->name);
1177                 return startstop;
1178         }
1179         if (!drive->special.all) {
1180                 if (rq->cmd == IDE_DRIVE_CMD || rq->cmd == IDE_DRIVE_TASK) {
1181                         return execute_drive_cmd(drive, rq);
1182                 }
1183                 if (drive->driver != NULL) {
1184                         return (DRIVER(drive)->do_request(drive, rq, block));
1185                 }
1186                 printk("%s: media type %d not supported\n", drive->name, drive->media);
1187                 goto kill_rq;
1188         }
1189         return do_special(drive);
1190 kill_rq:
1191         if (drive->driver != NULL)
1192                 DRIVER(drive)->end_request(0, HWGROUP(drive));
1193         else
1194                 ide_end_request(0, HWGROUP(drive));
1195         return ide_stopped;
1196 }
1197 
1198 /*
1199  * ide_stall_queue() can be used by a drive to give excess bandwidth back
1200  * to the hwgroup by sleeping for timeout jiffies.
1201  */
1202 void ide_stall_queue (ide_drive_t *drive, unsigned long timeout)
1203 {
1204         if (timeout > WAIT_WORSTCASE)
1205                 timeout = WAIT_WORSTCASE;
1206         drive->sleep = timeout + jiffies;
1207 }
1208 
1209 #define WAKEUP(drive)   ((drive)->service_start + 2 * (drive)->service_time)
1210 
1211 /*
1212  * choose_drive() selects the next drive which will be serviced.
1213  */
1214 static inline ide_drive_t *choose_drive (ide_hwgroup_t *hwgroup)
1215 {
1216         ide_drive_t *drive, *best;
1217 
1218 repeat: 
1219         best = NULL;
1220         drive = hwgroup->drive;
1221         do {
1222                 if (!list_empty(&drive->queue.queue_head) && (!drive->sleep || 0 <= (signed long)(jiffies - drive->sleep))) {
1223                         if (!best
1224                          || (drive->sleep && (!best->sleep || 0 < (signed long)(best->sleep - drive->sleep)))
1225                          || (!best->sleep && 0 < (signed long)(WAKEUP(best) - WAKEUP(drive))))
1226                         {
1227                                 if( !drive->queue.plugged )
1228                                         best = drive;
1229                         }
1230                 }
1231         } while ((drive = drive->next) != hwgroup->drive);
1232         if (best && best->nice1 && !best->sleep && best != hwgroup->drive && best->service_time > WAIT_MIN_SLEEP) {
1233                 long t = (signed long)(WAKEUP(best) - jiffies);
1234                 if (t >= WAIT_MIN_SLEEP) {
1235                         /*
1236                          * We *may* have some time to spare, but first let's see if
1237                          * someone can potentially benefit from our nice mood today..
1238                          */
1239                         drive = best->next;
1240                         do {
1241                                 if (!drive->sleep
1242                                  && 0 < (signed long)(WAKEUP(drive) - (jiffies - best->service_time))
1243                                  && 0 < (signed long)((jiffies + t) - WAKEUP(drive)))
1244                                 {
1245                                         ide_stall_queue(best, IDE_MIN(t, 10 * WAIT_MIN_SLEEP));
1246                                         goto repeat;
1247                                 }
1248                         } while ((drive = drive->next) != best);
1249                 }
1250         }
1251         return best;
1252 }
1253 
1254 /*
1255  * Issue a new request to a drive from hwgroup
1256  * Caller must have already done spin_lock_irqsave(&io_request_lock, ..);
1257  *
1258  * A hwgroup is a serialized group of IDE interfaces.  Usually there is
1259  * exactly one hwif (interface) per hwgroup, but buggy controllers (eg. CMD640)
1260  * may have both interfaces in a single hwgroup to "serialize" access.
1261  * Or possibly multiple ISA interfaces can share a common IRQ by being grouped
1262  * together into one hwgroup for serialized access.
1263  *
1264  * Note also that several hwgroups can end up sharing a single IRQ,
1265  * possibly along with many other devices.  This is especially common in
1266  * PCI-based systems with off-board IDE controller cards.
1267  *
1268  * The IDE driver uses the single global io_request_lock spinlock to protect
1269  * access to the request queues, and to protect the hwgroup->busy flag.
1270  *
1271  * The first thread into the driver for a particular hwgroup sets the
1272  * hwgroup->busy flag to indicate that this hwgroup is now active,
1273  * and then initiates processing of the top request from the request queue.
1274  *
1275  * Other threads attempting entry notice the busy setting, and will simply
1276  * queue their new requests and exit immediately.  Note that hwgroup->busy
1277  * remains set even when the driver is merely awaiting the next interrupt.
1278  * Thus, the meaning is "this hwgroup is busy processing a request".
1279  *
1280  * When processing of a request completes, the completing thread or IRQ-handler
1281  * will start the next request from the queue.  If no more work remains,
1282  * the driver will clear the hwgroup->busy flag and exit.
1283  *
1284  * The io_request_lock (spinlock) is used to protect all access to the
1285  * hwgroup->busy flag, but is otherwise not needed for most processing in
1286  * the driver.  This makes the driver much more friendlier to shared IRQs
1287  * than previous designs, while remaining 100% (?) SMP safe and capable.
1288  */
1289 static void ide_do_request(ide_hwgroup_t *hwgroup, int masked_irq)
1290 {
1291         ide_drive_t     *drive;
1292         ide_hwif_t      *hwif;
1293         ide_startstop_t startstop;
1294 
1295         ide_get_lock(&ide_lock, ide_intr, hwgroup);     /* for atari only: POSSIBLY BROKEN HERE(?) */
1296 
1297         __cli();        /* necessary paranoia: ensure IRQs are masked on local CPU */
1298 
1299         while (!hwgroup->busy) {
1300                 hwgroup->busy = 1;
1301                 drive = choose_drive(hwgroup);
1302                 if (drive == NULL) {
1303                         unsigned long sleep = 0;
1304                         hwgroup->rq = NULL;
1305                         drive = hwgroup->drive;
1306                         do {
1307                                 if (drive->sleep && (!sleep || 0 < (signed long)(sleep - drive->sleep)))
1308                                         sleep = drive->sleep;
1309                         } while ((drive = drive->next) != hwgroup->drive);
1310                         if (sleep) {
1311                                 /*
1312                                  * Take a short snooze, and then wake up this hwgroup again.
1313                                  * This gives other hwgroups on the same a chance to
1314                                  * play fairly with us, just in case there are big differences
1315                                  * in relative throughputs.. don't want to hog the cpu too much.
1316                                  */
1317                                 if (0 < (signed long)(jiffies + WAIT_MIN_SLEEP - sleep)) 
1318                                         sleep = jiffies + WAIT_MIN_SLEEP;
1319 #if 1
1320                                 if (timer_pending(&hwgroup->timer))
1321                                         printk("ide_set_handler: timer already active\n");
1322 #endif
1323                                 hwgroup->sleeping = 1;  /* so that ide_timer_expiry knows what to do */
1324                                 mod_timer(&hwgroup->timer, sleep);
1325                                 /* we purposely leave hwgroup->busy==1 while sleeping */
1326                         } else {
1327                                 /* Ugly, but how can we sleep for the lock otherwise? perhaps from tq_disk? */
1328                                 ide_release_lock(&ide_lock);    /* for atari only */
1329                                 hwgroup->busy = 0;
1330                         }
1331                         return;         /* no more work for this hwgroup (for now) */
1332                 }
1333                 hwif = HWIF(drive);
1334                 if (hwgroup->hwif->sharing_irq && hwif != hwgroup->hwif && hwif->io_ports[IDE_CONTROL_OFFSET]) {
1335                         /* set nIEN for previous hwif */
1336                         SELECT_INTERRUPT(hwif, drive);
1337                 }
1338                 hwgroup->hwif = hwif;
1339                 hwgroup->drive = drive;
1340                 drive->sleep = 0;
1341                 drive->service_start = jiffies;
1342 
1343                 if ( drive->queue.plugged )     /* paranoia */
1344                         printk("%s: Huh? nuking plugged queue\n", drive->name);
1345                 hwgroup->rq = blkdev_entry_next_request(&drive->queue.queue_head);
1346                 /*
1347                  * Some systems have trouble with IDE IRQs arriving while
1348                  * the driver is still setting things up.  So, here we disable
1349                  * the IRQ used by this interface while the request is being started.
1350                  * This may look bad at first, but pretty much the same thing
1351                  * happens anyway when any interrupt comes in, IDE or otherwise
1352                  *  -- the kernel masks the IRQ while it is being handled.
1353                  */
1354                 if (masked_irq && hwif->irq != masked_irq)
1355                         disable_irq_nosync(hwif->irq);
1356                 spin_unlock(&io_request_lock);
1357                 ide__sti();     /* allow other IRQs while we start this request */
1358                 startstop = start_request(drive);
1359                 spin_lock_irq(&io_request_lock);
1360                 if (masked_irq && hwif->irq != masked_irq)
1361                         enable_irq(hwif->irq);
1362                 if (startstop == ide_stopped)
1363                         hwgroup->busy = 0;
1364         }
1365 }
1366 
1367 /*
1368  * ide_get_queue() returns the queue which corresponds to a given device.
1369  */
1370 request_queue_t *ide_get_queue (kdev_t dev)
1371 {
1372         ide_hwif_t *hwif = (ide_hwif_t *)blk_dev[MAJOR(dev)].data;
1373 
1374         return &hwif->drives[DEVICE_NR(dev) & 1].queue;
1375 }
1376 
1377 /*
1378  * Passes the stuff to ide_do_request
1379  */
1380 void do_ide_request(request_queue_t *q)
1381 {
1382         ide_do_request(q->queuedata, 0);
1383 }
1384 
1385 /*
1386  * ide_timer_expiry() is our timeout function for all drive operations.
1387  * But note that it can also be invoked as a result of a "sleep" operation
1388  * triggered by the mod_timer() call in ide_do_request.
1389  */
1390 void ide_timer_expiry (unsigned long data)
1391 {
1392         ide_hwgroup_t   *hwgroup = (ide_hwgroup_t *) data;
1393         ide_handler_t   *handler;
1394         ide_expiry_t    *expiry;
1395         unsigned long   flags;
1396         unsigned long   wait;
1397 
1398         spin_lock_irqsave(&io_request_lock, flags);
1399         del_timer(&hwgroup->timer);
1400 
1401         if ((handler = hwgroup->handler) == NULL) {
1402                 /*
1403                  * Either a marginal timeout occured
1404                  * (got the interrupt just as timer expired),
1405                  * or we were "sleeping" to give other devices a chance.
1406                  * Either way, we don't really want to complain about anything.
1407                  */
1408                 if (hwgroup->sleeping) {
1409                         hwgroup->sleeping = 0;
1410                         hwgroup->busy = 0;
1411                 }
1412         } else {
1413                 ide_drive_t *drive = hwgroup->drive;
1414                 if (!drive) {
1415                         printk("ide_timer_expiry: hwgroup->drive was NULL\n");
1416                         hwgroup->handler = NULL;
1417                 } else {
1418                         ide_hwif_t *hwif;
1419                         ide_startstop_t startstop;
1420                         if (!hwgroup->busy) {
1421                                 hwgroup->busy = 1;      /* paranoia */
1422                                 printk("%s: ide_timer_expiry: hwgroup->busy was 0 ??\n", drive->name);
1423                         }
1424                         if ((expiry = hwgroup->expiry) != NULL) {
1425                                 /* continue */
1426                                 if ((wait = expiry(drive)) != 0) {
1427                                         /* reset timer */
1428                                         hwgroup->timer.expires  = jiffies + wait;
1429                                         add_timer(&hwgroup->timer);
1430                                         spin_unlock_irqrestore(&io_request_lock, flags);
1431                                         return;
1432                                 }
1433                         }
1434                         hwgroup->handler = NULL;
1435                         /*
1436                          * We need to simulate a real interrupt when invoking
1437                          * the handler() function, which means we need to globally
1438                          * mask the specific IRQ:
1439                          */
1440                         spin_unlock(&io_request_lock);
1441                         hwif  = HWIF(drive);
1442 #if DISABLE_IRQ_NOSYNC
1443                         disable_irq_nosync(hwif->irq);
1444 #else
1445                         disable_irq(hwif->irq); /* disable_irq_nosync ?? */
1446 #endif /* DISABLE_IRQ_NOSYNC */
1447                         __cli();        /* local CPU only, as if we were handling an interrupt */
1448                         if (hwgroup->poll_timeout != 0) {
1449                                 startstop = handler(drive);
1450                         } else if (drive_is_ready(drive)) {
1451                                 if (drive->waiting_for_dma)
1452                                         (void) hwgroup->hwif->dmaproc(ide_dma_lostirq, drive);
1453                                 (void)ide_ack_intr(hwif);
1454                                 printk("%s: lost interrupt\n", drive->name);
1455                                 startstop = handler(drive);
1456                         } else {
1457                                 if (drive->waiting_for_dma) {
1458                                         (void) hwgroup->hwif->dmaproc(ide_dma_end, drive);
1459                                         printk("%s: timeout waiting for DMA\n", drive->name);
1460                                         (void) hwgroup->hwif->dmaproc(ide_dma_timeout, drive);
1461                                 }
1462                                 startstop = ide_error(drive, "irq timeout", GET_STAT());
1463                         }
1464                         set_recovery_timer(hwif);
1465                         drive->service_time = jiffies - drive->service_start;
1466                         enable_irq(hwif->irq);
1467                         spin_lock_irq(&io_request_lock);
1468                         if (startstop == ide_stopped)
1469                                 hwgroup->busy = 0;
1470                 }
1471         }
1472         ide_do_request(hwgroup, 0);
1473         spin_unlock_irqrestore(&io_request_lock, flags);
1474 }
1475 
1476 /*
1477  * There's nothing really useful we can do with an unexpected interrupt,
1478  * other than reading the status register (to clear it), and logging it.
1479  * There should be no way that an irq can happen before we're ready for it,
1480  * so we needn't worry much about losing an "important" interrupt here.
1481  *
1482  * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
1483  * drive enters "idle", "standby", or "sleep" mode, so if the status looks
1484  * "good", we just ignore the interrupt completely.
1485  *
1486  * This routine assumes __cli() is in effect when called.
1487  *
1488  * If an unexpected interrupt happens on irq15 while we are handling irq14
1489  * and if the two interfaces are "serialized" (CMD640), then it looks like
1490  * we could screw up by interfering with a new request being set up for irq15.
1491  *
1492  * In reality, this is a non-issue.  The new command is not sent unless the
1493  * drive is ready to accept one, in which case we know the drive is not
1494  * trying to interrupt us.  And ide_set_handler() is always invoked before
1495  * completing the issuance of any new drive command, so we will not be
1496  * accidently invoked as a result of any valid command completion interrupt.
1497  *
1498  */
1499 static void unexpected_intr (int irq, ide_hwgroup_t *hwgroup)
1500 {
1501         byte stat;
1502         ide_hwif_t *hwif = hwgroup->hwif;
1503 
1504         /*
1505          * handle the unexpected interrupt
1506          */
1507         do {
1508                 if (hwif->irq == irq) {
1509                         stat = IN_BYTE(hwif->io_ports[IDE_STATUS_OFFSET]);
1510                         if (!OK_STAT(stat, READY_STAT, BAD_STAT)) {
1511                                 /* Try to not flood the console with msgs */
1512                                 static unsigned long last_msgtime, count;
1513                                 ++count;
1514                                 if (0 < (signed long)(jiffies - (last_msgtime + HZ))) {
1515                                         last_msgtime = jiffies;
1516                                         printk("%s%s: unexpected interrupt, status=0x%02x, count=%ld\n",
1517                                          hwif->name, (hwif->next == hwgroup->hwif) ? "" : "(?)", stat, count);
1518                                 }
1519                         }
1520                 }
1521         } while ((hwif = hwif->next) != hwgroup->hwif);
1522 }
1523 
1524 /*
1525  * entry point for all interrupts, caller does __cli() for us
1526  */
1527 void ide_intr (int irq, void *dev_id, struct pt_regs *regs)
1528 {
1529         unsigned long flags;
1530         ide_hwgroup_t *hwgroup = (ide_hwgroup_t *)dev_id;
1531         ide_hwif_t *hwif;
1532         ide_drive_t *drive;
1533         ide_handler_t *handler;
1534         ide_startstop_t startstop;
1535 
1536         spin_lock_irqsave(&io_request_lock, flags);
1537         hwif = hwgroup->hwif;
1538 
1539         if (!ide_ack_intr(hwif)) {
1540                 spin_unlock_irqrestore(&io_request_lock, flags);
1541                 return;
1542         }
1543 
1544         if ((handler = hwgroup->handler) == NULL || hwgroup->poll_timeout != 0) {
1545                 /*
1546                  * Not expecting an interrupt from this drive.
1547                  * That means this could be:
1548                  *      (1) an interrupt from another PCI device
1549                  *      sharing the same PCI INT# as us.
1550                  * or   (2) a drive just entered sleep or standby mode,
1551                  *      and is interrupting to let us know.
1552                  * or   (3) a spurious interrupt of unknown origin.
1553                  *
1554                  * For PCI, we cannot tell the difference,
1555                  * so in that case we just ignore it and hope it goes away.
1556                  */
1557 #ifdef CONFIG_BLK_DEV_IDEPCI
1558                 if (IDE_PCI_DEVID_EQ(hwif->pci_devid, IDE_PCI_DEVID_NULL))
1559 #endif  /* CONFIG_BLK_DEV_IDEPCI */
1560                 {
1561                         /*
1562                          * Probably not a shared PCI interrupt,
1563                          * so we can safely try to do something about it:
1564                          */
1565                         unexpected_intr(irq, hwgroup);
1566 #ifdef CONFIG_BLK_DEV_IDEPCI
1567                 } else {
1568                         /*
1569                          * Whack the status register, just in case we have a leftover pending IRQ.
1570                          */
1571                         (void) IN_BYTE(hwif->io_ports[IDE_STATUS_OFFSET]);
1572 #endif /* CONFIG_BLK_DEV_IDEPCI */
1573                 }
1574                 spin_unlock_irqrestore(&io_request_lock, flags);
1575                 return;
1576         }
1577         drive = hwgroup->drive;
1578         if (!drive) {
1579                 /*
1580                  * This should NEVER happen, and there isn't much we could do about it here.
1581                  */
1582                 spin_unlock_irqrestore(&io_request_lock, flags);
1583                 return;
1584         }
1585         if (!drive_is_ready(drive)) {
1586                 /*
1587                  * This happens regularly when we share a PCI IRQ with another device.
1588                  * Unfortunately, it can also happen with some buggy drives that trigger
1589                  * the IRQ before their status register is up to date.  Hopefully we have
1590                  * enough advance overhead that the latter isn't a problem.
1591                  */
1592                 spin_unlock_irqrestore(&io_request_lock, flags);
1593                 return;
1594         }
1595         if (!hwgroup->busy) {
1596                 hwgroup->busy = 1;      /* paranoia */
1597                 printk("%s: ide_intr: hwgroup->busy was 0 ??\n", drive->name);
1598         }
1599         hwgroup->handler = NULL;
1600         del_timer(&hwgroup->timer);
1601         spin_unlock(&io_request_lock);
1602 
1603         if (drive->unmask)
1604                 ide__sti();     /* local CPU only */
1605         startstop = handler(drive);             /* service this interrupt, may set handler for next interrupt */
1606         spin_lock_irq(&io_request_lock);
1607 
1608         /*
1609          * Note that handler() may have set things up for another
1610          * interrupt to occur soon, but it cannot happen until
1611          * we exit from this routine, because it will be the
1612          * same irq as is currently being serviced here, and Linux
1613          * won't allow another of the same (on any CPU) until we return.
1614          */
1615         set_recovery_timer(HWIF(drive));
1616         drive->service_time = jiffies - drive->service_start;
1617         if (startstop == ide_stopped) {
1618                 if (hwgroup->handler == NULL) { /* paranoia */
1619                         hwgroup->busy = 0;
1620                         ide_do_request(hwgroup, hwif->irq);
1621                 } else {
1622                         printk("%s: ide_intr: huh? expected NULL handler on exit\n", drive->name);
1623                 }
1624         }
1625         spin_unlock_irqrestore(&io_request_lock, flags);
1626 }
1627 
1628 /*
1629  * get_info_ptr() returns the (ide_drive_t *) for a given device number.
1630  * It returns NULL if the given device number does not match any present drives.
1631  */
1632 ide_drive_t *get_info_ptr (kdev_t i_rdev)
1633 {
1634         int             major = MAJOR(i_rdev);
1635 #if 0
1636         int             minor = MINOR(i_rdev) & PARTN_MASK;
1637 #endif
1638         unsigned int    h;
1639 
1640         for (h = 0; h < MAX_HWIFS; ++h) {
1641                 ide_hwif_t  *hwif = &ide_hwifs[h];
1642                 if (hwif->present && major == hwif->major) {
1643                         unsigned unit = DEVICE_NR(i_rdev);
1644                         if (unit < MAX_DRIVES) {
1645                                 ide_drive_t *drive = &hwif->drives[unit];
1646 #if 0
1647                                 if ((drive->present) && (drive->part[minor].nr_sects))
1648 #else
1649                                 if (drive->present)
1650 #endif
1651                                         return drive;
1652                         }
1653                         break;
1654                 }
1655         }
1656         return NULL;
1657 }
1658 
1659 /*
1660  * This function is intended to be used prior to invoking ide_do_drive_cmd().
1661  */
1662 void ide_init_drive_cmd (struct request *rq)
1663 {
1664         memset(rq, 0, sizeof(*rq));
1665         rq->cmd = IDE_DRIVE_CMD;
1666 }
1667 
1668 /*
1669  * This function issues a special IDE device request
1670  * onto the request queue.
1671  *
1672  * If action is ide_wait, then the rq is queued at the end of the
1673  * request queue, and the function sleeps until it has been processed.
1674  * This is for use when invoked from an ioctl handler.
1675  *
1676  * If action is ide_preempt, then the rq is queued at the head of
1677  * the request queue, displacing the currently-being-processed
1678  * request and this function returns immediately without waiting
1679  * for the new rq to be completed.  This is VERY DANGEROUS, and is
1680  * intended for careful use by the ATAPI tape/cdrom driver code.
1681  *
1682  * If action is ide_next, then the rq is queued immediately after
1683  * the currently-being-processed-request (if any), and the function
1684  * returns without waiting for the new rq to be completed.  As above,
1685  * This is VERY DANGEROUS, and is intended for careful use by the
1686  * ATAPI tape/cdrom driver code.
1687  *
1688  * If action is ide_end, then the rq is queued at the end of the
1689  * request queue, and the function returns immediately without waiting
1690  * for the new rq to be completed. This is again intended for careful
1691  * use by the ATAPI tape/cdrom driver code.
1692  */
1693 int ide_do_drive_cmd (ide_drive_t *drive, struct request *rq, ide_action_t action)
1694 {
1695         unsigned long flags;
1696         ide_hwgroup_t *hwgroup = HWGROUP(drive);
1697         unsigned int major = HWIF(drive)->major;
1698         struct list_head * queue_head;
1699         DECLARE_MUTEX_LOCKED(sem);
1700 
1701 #ifdef CONFIG_BLK_DEV_PDC4030
1702         if (HWIF(drive)->chipset == ide_pdc4030 && rq->buffer != NULL)
1703                 return -ENOSYS;  /* special drive cmds not supported */
1704 #endif
1705         rq->errors = 0;
1706         rq->rq_status = RQ_ACTIVE;
1707         rq->rq_dev = MKDEV(major,(drive->select.b.unit)<<PARTN_BITS);
1708         if (action == ide_wait)
1709                 rq->sem = &sem;
1710         spin_lock_irqsave(&io_request_lock, flags);
1711         queue_head = &drive->queue.queue_head;
1712         if (list_empty(queue_head) || action == ide_preempt) {
1713                 if (action == ide_preempt)
1714                         hwgroup->rq = NULL;
1715         } else {
1716                 if (action == ide_wait || action == ide_end) {
1717                         queue_head = queue_head->prev;
1718                 } else
1719                         queue_head = queue_head->next;
1720         }
1721         list_add(&rq->queue, queue_head);
1722         ide_do_request(hwgroup, 0);
1723         spin_unlock_irqrestore(&io_request_lock, flags);
1724         if (action == ide_wait) {
1725                 down(&sem);                     /* wait for it to be serviced */
1726                 return rq->errors ? -EIO : 0;   /* return -EIO if errors */
1727         }
1728         return 0;
1729 
1730 }
1731 
1732 /*
1733  * This routine is called to flush all partitions and partition tables
1734  * for a changed disk, and then re-read the new partition table.
1735  * If we are revalidating a disk because of a media change, then we
1736  * enter with usage == 0.  If we are using an ioctl, we automatically have
1737  * usage == 1 (we need an open channel to use an ioctl :-), so this
1738  * is our limit.
1739  */
1740 int ide_revalidate_disk (kdev_t i_rdev)
1741 {
1742         ide_drive_t *drive;
1743         ide_hwgroup_t *hwgroup;
1744         unsigned int p, major, minor;
1745         long flags;
1746 
1747         if ((drive = get_info_ptr(i_rdev)) == NULL)
1748                 return -ENODEV;
1749         major = MAJOR(i_rdev);
1750         minor = drive->select.b.unit << PARTN_BITS;
1751         hwgroup = HWGROUP(drive);
1752         spin_lock_irqsave(&io_request_lock, flags);
1753         if (drive->busy || (drive->usage > 1)) {
1754                 spin_unlock_irqrestore(&io_request_lock, flags);
1755                 return -EBUSY;
1756         };
1757         drive->busy = 1;
1758         MOD_INC_USE_COUNT;
1759         spin_unlock_irqrestore(&io_request_lock, flags);
1760 
1761         for (p = 0; p < (1<<PARTN_BITS); ++p) {
1762                 if (drive->part[p].nr_sects > 0) {
1763                         kdev_t devp = MKDEV(major, minor+p);
1764                         struct super_block * sb = get_super(devp);
1765                         fsync_dev          (devp);
1766                         if (sb)
1767                                 invalidate_inodes(sb);
1768                         invalidate_buffers (devp);
1769                         set_blocksize(devp, 1024);
1770                 }
1771                 drive->part[p].start_sect = 0;
1772                 drive->part[p].nr_sects   = 0;
1773         };
1774 
1775         if (DRIVER(drive)->revalidate)
1776                 DRIVER(drive)->revalidate(drive);
1777 
1778         drive->busy = 0;
1779         wake_up(&drive->wqueue);
1780         MOD_DEC_USE_COUNT;
1781         return 0;
1782 }
1783 
1784 static void revalidate_drives (void)
1785 {
1786         ide_hwif_t *hwif;
1787         ide_drive_t *drive;
1788         int index, unit;
1789 
1790         for (index = 0; index < MAX_HWIFS; ++index) {
1791                 hwif = &ide_hwifs[index];
1792                 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1793                         drive = &ide_hwifs[index].drives[unit];
1794                         if (drive->revalidate) {
1795                                 drive->revalidate = 0;
1796                                 if (!initializing)
1797                                         (void) ide_revalidate_disk(MKDEV(hwif->major, unit<<PARTN_BITS));
1798                         }
1799                 }
1800         }
1801 }
1802 
1803 static void ide_probe_module (void)
1804 {
1805         if (!ide_probe) {
1806 #if defined(CONFIG_KMOD) && defined(CONFIG_BLK_DEV_IDE_MODULE)
1807                 (void) request_module("ide-probe-mod");
1808 #endif /* (CONFIG_KMOD) && (CONFIG_BLK_DEV_IDE_MODULE) */
1809         } else {
1810                 (void) ide_probe->init();
1811         }
1812         revalidate_drives();
1813 }
1814 
1815 static void ide_driver_module (void)
1816 {
1817         int index;
1818         ide_module_t *module = ide_modules;
1819 
1820         for (index = 0; index < MAX_HWIFS; ++index)
1821                 if (ide_hwifs[index].present)
1822                         goto search;
1823         ide_probe_module();
1824 search:
1825         while (module) {
1826                 (void) module->init();
1827                 module = module->next;
1828         }
1829         revalidate_drives();
1830 }
1831 
1832 static int ide_open (struct inode * inode, struct file * filp)
1833 {
1834         ide_drive_t *drive;
1835         int rc;
1836 
1837         if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
1838                 return -ENXIO;
1839         MOD_INC_USE_COUNT;
1840         if (drive->driver == NULL)
1841                 ide_driver_module();
1842 #ifdef CONFIG_KMOD
1843         if (drive->driver == NULL) {
1844                 if (drive->media == ide_disk)
1845                         (void) request_module("ide-disk");
1846                 if (drive->media == ide_cdrom)
1847                         (void) request_module("ide-cd");
1848                 if (drive->media == ide_tape)
1849                         (void) request_module("ide-tape");
1850                 if (drive->media == ide_floppy)
1851                         (void) request_module("ide-floppy");
1852         }
1853 #endif /* CONFIG_KMOD */
1854         while (drive->busy)
1855                 sleep_on(&drive->wqueue);
1856         drive->usage++;
1857         if (drive->driver != NULL) {
1858                 if ((rc = DRIVER(drive)->open(inode, filp, drive)))
1859                         MOD_DEC_USE_COUNT;
1860                 return rc;
1861         }
1862         printk ("%s: driver not present\n", drive->name);
1863         drive->usage--;
1864         MOD_DEC_USE_COUNT;
1865         return -ENXIO;
1866 }
1867 
1868 /*
1869  * Releasing a block device means we sync() it, so that it can safely
1870  * be forgotten about...
1871  */
1872 static int ide_release (struct inode * inode, struct file * file)
1873 {
1874         ide_drive_t *drive;
1875 
1876         if ((drive = get_info_ptr(inode->i_rdev)) != NULL) {
1877                 drive->usage--;
1878                 if (drive->driver != NULL)
1879                         DRIVER(drive)->release(inode, file, drive);
1880                 MOD_DEC_USE_COUNT;
1881         }
1882         return 0;
1883 }
1884 
1885 int ide_replace_subdriver (ide_drive_t *drive, const char *driver)
1886 {
1887         if (!drive->present || drive->busy || drive->usage)
1888                 goto abort;
1889         if (drive->driver != NULL && DRIVER(drive)->cleanup(drive))
1890                 goto abort;
1891         strncpy(drive->driver_req, driver, 9);
1892         ide_driver_module();
1893         drive->driver_req[0] = 0;
1894         ide_driver_module();
1895         if (DRIVER(drive) && !strcmp(DRIVER(drive)->name, driver))
1896                 return 0;
1897 abort:
1898         return 1;
1899 }
1900 
1901 #ifdef CONFIG_PROC_FS
1902 ide_proc_entry_t generic_subdriver_entries[] = {
1903         { "capacity",   S_IFREG|S_IRUGO,        proc_ide_read_capacity, NULL },
1904         { NULL, 0, NULL, NULL }
1905 };
1906 #endif
1907 
1908 /*
1909  * Note that we only release the standard ports,
1910  * and do not even try to handle any extra ports
1911  * allocated for weird IDE interface chipsets.
1912  */
1913 void hwif_unregister (ide_hwif_t *hwif)
1914 {
1915         if (hwif->straight8) {
1916                 ide_release_region(hwif->io_ports[IDE_DATA_OFFSET], 8);
1917                 goto jump_eight;
1918         }
1919         if (hwif->io_ports[IDE_DATA_OFFSET])
1920                 ide_release_region(hwif->io_ports[IDE_DATA_OFFSET], 1);
1921         if (hwif->io_ports[IDE_ERROR_OFFSET])
1922                 ide_release_region(hwif->io_ports[IDE_ERROR_OFFSET], 1);
1923         if (hwif->io_ports[IDE_NSECTOR_OFFSET])
1924                 ide_release_region(hwif->io_ports[IDE_NSECTOR_OFFSET], 1);
1925         if (hwif->io_ports[IDE_SECTOR_OFFSET])
1926                 ide_release_region(hwif->io_ports[IDE_SECTOR_OFFSET], 1);
1927         if (hwif->io_ports[IDE_LCYL_OFFSET])
1928                 ide_release_region(hwif->io_ports[IDE_LCYL_OFFSET], 1);
1929         if (hwif->io_ports[IDE_HCYL_OFFSET])
1930                 ide_release_region(hwif->io_ports[IDE_HCYL_OFFSET], 1);
1931         if (hwif->io_ports[IDE_SELECT_OFFSET])
1932                 ide_release_region(hwif->io_ports[IDE_SELECT_OFFSET], 1);
1933         if (hwif->io_ports[IDE_STATUS_OFFSET])
1934                 ide_release_region(hwif->io_ports[IDE_STATUS_OFFSET], 1);
1935 jump_eight:
1936         if (hwif->io_ports[IDE_CONTROL_OFFSET])
1937                 ide_release_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
1938 #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
1939         if (hwif->io_ports[IDE_IRQ_OFFSET])
1940                 ide_release_region(hwif->io_ports[IDE_IRQ_OFFSET], 1);
1941 #endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
1942 }
1943 
1944 void ide_unregister (unsigned int index)
1945 {
1946         struct gendisk *gd, **gdp;
1947         ide_drive_t *drive, *d;
1948         ide_hwif_t *hwif, *g;
1949         ide_hwgroup_t *hwgroup;
1950         int irq_count = 0, unit, i;
1951         unsigned long flags;
1952         unsigned int p, minor;
1953         ide_hwif_t old_hwif;
1954 
1955         if (index >= MAX_HWIFS)
1956                 return;
1957         save_flags(flags);      /* all CPUs */
1958         cli();                  /* all CPUs */
1959         hwif = &ide_hwifs[index];
1960         if (!hwif->present)
1961                 goto abort;
1962         for (unit = 0; unit < MAX_DRIVES; ++unit) {
1963                 drive = &hwif->drives[unit];
1964                 if (!drive->present)
1965                         continue;
1966                 if (drive->busy || drive->usage)
1967                         goto abort;
1968                 if (drive->driver != NULL && DRIVER(drive)->cleanup(drive))
1969                         goto abort;
1970         }
1971         hwif->present = 0;
1972         
1973         /*
1974          * All clear?  Then blow away the buffer cache
1975          */
1976         sti();
1977         for (unit = 0; unit < MAX_DRIVES; ++unit) {
1978                 drive = &hwif->drives[unit];
1979                 if (!drive->present)
1980                         continue;
1981                 minor = drive->select.b.unit << PARTN_BITS;
1982                 for (p = 0; p < (1<<PARTN_BITS); ++p) {
1983                         if (drive->part[p].nr_sects > 0) {
1984                                 kdev_t devp = MKDEV(hwif->major, minor+p);
1985                                 struct super_block * sb = get_super(devp);
1986                                 if (sb) invalidate_inodes(sb);
1987                                 invalidate_buffers (devp);
1988                         }
1989                 }
1990 #ifdef CONFIG_PROC_FS
1991                 destroy_proc_ide_drives(hwif);
1992 #endif
1993         }
1994         cli();
1995         hwgroup = hwif->hwgroup;
1996 
1997         /*
1998          * free the irq if we were the only hwif using it
1999          */
2000         g = hwgroup->hwif;
2001         do {
2002                 if (g->irq == hwif->irq)
2003                         ++irq_count;
2004                 g = g->next;
2005         } while (g != hwgroup->hwif);
2006         if (irq_count == 1)
2007                 free_irq(hwif->irq, hwgroup);
2008 
2009         /*
2010          * Note that we only release the standard ports,
2011          * and do not even try to handle any extra ports
2012          * allocated for weird IDE interface chipsets.
2013          */
2014         hwif_unregister(hwif);
2015 
2016         /*
2017          * Remove us from the hwgroup, and free
2018          * the hwgroup if we were the only member
2019          */
2020         d = hwgroup->drive;
2021         for (i = 0; i < MAX_DRIVES; ++i) {
2022                 drive = &hwif->drives[i];
2023                 if (drive->de) {
2024                         devfs_unregister (drive->de);
2025                         drive->de = NULL;
2026                 }
2027                 if (!drive->present)
2028                         continue;
2029                 while (hwgroup->drive->next != drive)
2030                         hwgroup->drive = hwgroup->drive->next;
2031                 hwgroup->drive->next = drive->next;
2032                 if (hwgroup->drive == drive)
2033                         hwgroup->drive = NULL;
2034                 if (drive->id != NULL) {
2035                         kfree(drive->id);
2036                         drive->id = NULL;
2037                 }
2038                 drive->present = 0;
2039                 blk_cleanup_queue(&drive->queue);
2040         }
2041         if (d->present)
2042                 hwgroup->drive = d;
2043         while (hwgroup->hwif->next != hwif)
2044                 hwgroup->hwif = hwgroup->hwif->next;
2045         hwgroup->hwif->next = hwif->next;
2046         if (hwgroup->hwif == hwif)
2047                 kfree(hwgroup);
2048         else
2049                 hwgroup->hwif = HWIF(hwgroup->drive);
2050 
2051 #if defined(CONFIG_BLK_DEV_IDEDMA) && !defined(CONFIG_DMA_NONPCI)
2052         if (hwif->dma_base) {
2053                 (void) ide_release_dma(hwif);
2054                 hwif->dma_base = 0;
2055         }
2056 #endif /* (CONFIG_BLK_DEV_IDEDMA) && !(CONFIG_DMA_NONPCI) */
2057 
2058         /*
2059          * Remove us from the kernel's knowledge
2060          */
2061         unregister_blkdev(hwif->major, hwif->name);
2062         kfree(blksize_size[hwif->major]);
2063         kfree(max_sectors[hwif->major]);
2064         kfree(max_readahead[hwif->major]);
2065         blk_dev[hwif->major].data = NULL;
2066         blk_dev[hwif->major].queue = NULL;
2067         blksize_size[hwif->major] = NULL;
2068         for (gdp = &gendisk_head; *gdp; gdp = &((*gdp)->next))
2069                 if (*gdp == hwif->gd)
2070                         break;
2071         if (*gdp == NULL)
2072                 printk("gd not in disk chain!\n");
2073         else {
2074                 gd = *gdp; *gdp = gd->next;
2075                 kfree(gd->sizes);
2076                 kfree(gd->part);
2077                 if (gd->de_arr)
2078                         kfree (gd->de_arr);
2079                 if (gd->flags)
2080                         kfree (gd->flags);
2081                 kfree(gd);
2082         }
2083         old_hwif                = *hwif;
2084         init_hwif_data (index); /* restore hwif data to pristine status */
2085         hwif->hwgroup           = old_hwif.hwgroup;
2086         hwif->tuneproc          = old_hwif.tuneproc;
2087         hwif->speedproc         = old_hwif.speedproc;
2088         hwif->selectproc        = old_hwif.selectproc;
2089         hwif->resetproc         = old_hwif.resetproc;
2090         hwif->intrproc          = old_hwif.intrproc;
2091         hwif->maskproc          = old_hwif.maskproc;
2092         hwif->quirkproc         = old_hwif.quirkproc;
2093         hwif->rwproc            = old_hwif.rwproc;
2094         hwif->dmaproc           = old_hwif.dmaproc;
2095         hwif->dma_base          = old_hwif.dma_base;
2096         hwif->dma_extra         = old_hwif.dma_extra;
2097         hwif->config_data       = old_hwif.config_data;
2098         hwif->select_data       = old_hwif.select_data;
2099         hwif->proc              = old_hwif.proc;
2100 #ifndef CONFIG_BLK_DEV_IDECS
2101         hwif->irq               = old_hwif.irq;
2102 #endif /* CONFIG_BLK_DEV_IDECS */
2103         hwif->major             = old_hwif.major;
2104         hwif->chipset           = old_hwif.chipset;
2105         hwif->autodma           = old_hwif.autodma;
2106         hwif->udma_four         = old_hwif.udma_four;
2107 #ifdef CONFIG_BLK_DEV_IDEPCI
2108         hwif->pci_dev           = old_hwif.pci_dev;
2109         hwif->pci_devid         = old_hwif.pci_devid;
2110 #endif /* CONFIG_BLK_DEV_IDEPCI */
2111         hwif->straight8         = old_hwif.straight8;
2112         hwif->hwif_data         = old_hwif.hwif_data;
2113 abort:
2114         restore_flags(flags);   /* all CPUs */
2115 }
2116 
2117 /*
2118  * Setup hw_regs_t structure described by parameters.  You
2119  * may set up the hw structure yourself OR use this routine to
2120  * do it for you.
2121  */
2122 void ide_setup_ports (  hw_regs_t *hw,
2123                         ide_ioreg_t base, int *offsets,
2124                         ide_ioreg_t ctrl, ide_ioreg_t intr,
2125                         ide_ack_intr_t *ack_intr, int irq)
2126 {
2127         int i;
2128 
2129         for (i = 0; i < IDE_NR_PORTS; i++) {
2130                 if (offsets[i] == -1) {
2131                         switch(i) {
2132                                 case IDE_CONTROL_OFFSET:
2133                                         hw->io_ports[i] = ctrl;
2134                                         break;
2135 #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
2136                                 case IDE_IRQ_OFFSET:
2137                                         hw->io_ports[i] = intr;
2138                                         break;
2139 #endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
2140                                 default:
2141                                         hw->io_ports[i] = 0;
2142                                         break;
2143                         }
2144                 } else {
2145                         hw->io_ports[i] = base + offsets[i];
2146                 }
2147         }
2148         hw->irq = irq;
2149         hw->dma = NO_DMA;
2150         hw->ack_intr = ack_intr;
2151 }
2152 
2153 /*
2154  * Register an IDE interface, specifing exactly the registers etc
2155  * Set init=1 iff calling before probes have taken place.
2156  */
2157 int ide_register_hw (hw_regs_t *hw, ide_hwif_t **hwifp)
2158 {
2159         int index, retry = 1;
2160         ide_hwif_t *hwif;
2161 
2162         do {
2163                 for (index = 0; index < MAX_HWIFS; ++index) {
2164                         hwif = &ide_hwifs[index];
2165                         if (hwif->hw.io_ports[IDE_DATA_OFFSET] == hw->io_ports[IDE_DATA_OFFSET])
2166                                 goto found;
2167                 }
2168                 for (index = 0; index < MAX_HWIFS; ++index) {
2169                         hwif = &ide_hwifs[index];
2170                         if ((!hwif->present && !hwif->mate && !initializing) ||
2171                             (!hwif->hw.io_ports[IDE_DATA_OFFSET] && initializing))
2172                                 goto found;
2173                 }
2174                 for (index = 0; index < MAX_HWIFS; index++)
2175                         ide_unregister(index);
2176         } while (retry--);
2177         return -1;
2178 found:
2179         if (hwif->present)
2180                 ide_unregister(index);
2181         if (hwif->present)
2182                 return -1;
2183         memcpy(&hwif->hw, hw, sizeof(*hw));
2184         memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->hw.io_ports));
2185         hwif->irq = hw->irq;
2186         hwif->noprobe = 0;
2187 
2188         if (!initializing) {
2189                 ide_probe_module();
2190 #ifdef CONFIG_PROC_FS
2191                 create_proc_ide_interfaces();
2192 #endif
2193                 ide_driver_module();
2194         }
2195 
2196         if (hwifp)
2197                 *hwifp = hwif;
2198 
2199         return (initializing || hwif->present) ? index : -1;
2200 }
2201 
2202 /*
2203  * Compatability function with existing drivers.  If you want
2204  * something different, use the function above.
2205  */
2206 int ide_register (int arg1, int arg2, int irq)
2207 {
2208         hw_regs_t hw;
2209         ide_init_hwif_ports(&hw, (ide_ioreg_t) arg1, (ide_ioreg_t) arg2, NULL);
2210         hw.irq = irq;
2211         return ide_register_hw(&hw, NULL);
2212 }
2213 
2214 void 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)
2215 {
2216         ide_settings_t **p = (ide_settings_t **) &drive->settings, *setting = NULL;
2217 
2218         while ((*p) && strcmp((*p)->name, name) < 0)
2219                 p = &((*p)->next);
2220         if ((setting = kmalloc(sizeof(*setting), GFP_KERNEL)) == NULL)
2221                 goto abort;
2222         memset(setting, 0, sizeof(*setting));
2223         if ((setting->name = kmalloc(strlen(name) + 1, GFP_KERNEL)) == NULL)
2224                 goto abort;
2225         strcpy(setting->name, name);            setting->rw = rw;
2226         setting->read_ioctl = read_ioctl;       setting->write_ioctl = write_ioctl;
2227         setting->data_type = data_type;         setting->min = min;
2228         setting->max = max;                     setting->mul_factor = mul_factor;
2229         setting->div_factor = div_factor;       setting->data = data;
2230         setting->set = set;                     setting->next = *p;
2231         if (drive->driver)
2232                 setting->auto_remove = 1;
2233         *p = setting;
2234         return;
2235 abort:
2236         if (setting)
2237                 kfree(setting);
2238 }
2239 
2240 void ide_remove_setting (ide_drive_t *drive, char *name)
2241 {
2242         ide_settings_t **p = (ide_settings_t **) &drive->settings, *setting;
2243 
2244         while ((*p) && strcmp((*p)->name, name))
2245                 p = &((*p)->next);
2246         if ((setting = (*p)) == NULL)
2247                 return;
2248         (*p) = setting->next;
2249         kfree(setting->name);
2250         kfree(setting);
2251 }
2252 
2253 static ide_settings_t *ide_find_setting_by_ioctl (ide_drive_t *drive, int cmd)
2254 {
2255         ide_settings_t *setting = drive->settings;
2256 
2257         while (setting) {
2258                 if (setting->read_ioctl == cmd || setting->write_ioctl == cmd)
2259                         break;
2260                 setting = setting->next;
2261         }
2262         return setting;
2263 }
2264 
2265 ide_settings_t *ide_find_setting_by_name (ide_drive_t *drive, char *name)
2266 {
2267         ide_settings_t *setting = drive->settings;
2268 
2269         while (setting) {
2270                 if (strcmp(setting->name, name) == 0)
2271                         break;
2272                 setting = setting->next;
2273         }
2274         return setting;
2275 }
2276 
2277 static void auto_remove_settings (ide_drive_t *drive)
2278 {
2279         ide_settings_t *setting;
2280 repeat:
2281         setting = drive->settings;
2282         while (setting) {
2283                 if (setting->auto_remove) {
2284                         ide_remove_setting(drive, setting->name);
2285                         goto repeat;
2286                 }
2287                 setting = setting->next;
2288         }
2289 }
2290 
2291 int ide_read_setting (ide_drive_t *drive, ide_settings_t *setting)
2292 {
2293         int             val = -EINVAL;
2294         unsigned long   flags;
2295 
2296         if ((setting->rw & SETTING_READ)) {
2297                 spin_lock_irqsave(&io_request_lock, flags);
2298                 switch(setting->data_type) {
2299                         case TYPE_BYTE:
2300                                 val = *((u8 *) setting->data);
2301                                 break;
2302                         case TYPE_SHORT:
2303                                 val = *((u16 *) setting->data);
2304                                 break;
2305                         case TYPE_INT:
2306                         case TYPE_INTA:
2307                                 val = *((u32 *) setting->data);
2308                                 break;
2309                 }
2310                 spin_unlock_irqrestore(&io_request_lock, flags);
2311         }
2312         return val;
2313 }
2314 
2315 int ide_spin_wait_hwgroup (ide_drive_t *drive)
2316 {
2317         ide_hwgroup_t *hwgroup = HWGROUP(drive);
2318         unsigned long timeout = jiffies + (3 * HZ);
2319 
2320         spin_lock_irq(&io_request_lock);
2321 
2322         while (hwgroup->busy) {
2323                 unsigned long lflags;
2324                 spin_unlock_irq(&io_request_lock);
2325                 __save_flags(lflags);   /* local CPU only */
2326                 __sti();                /* local CPU only; needed for jiffies */
2327                 if (0 < (signed long)(jiffies - timeout)) {
2328                         __restore_flags(lflags);        /* local CPU only */
2329                         printk("%s: channel busy\n", drive->name);
2330                         return -EBUSY;
2331                 }
2332                 __restore_flags(lflags);        /* local CPU only */
2333                 spin_lock_irq(&io_request_lock);
2334         }
2335         return 0;
2336 }
2337 
2338 /*
2339  * FIXME:  This should be changed to enqueue a special request
2340  * to the driver to change settings, and then wait on a sema for completion.
2341  * The current scheme of polling is kludgey, though safe enough.
2342  */
2343 int ide_write_setting (ide_drive_t *drive, ide_settings_t *setting, int val)
2344 {
2345         int i;
2346         u32 *p;
2347 
2348         if (!capable(CAP_SYS_ADMIN))
2349                 return -EACCES;
2350         if (!(setting->rw & SETTING_WRITE))
2351                 return -EPERM;
2352         if (val < setting->min || val > setting->max)
2353                 return -EINVAL;
2354         if (setting->set)
2355                 return setting->set(drive, val);
2356         if (ide_spin_wait_hwgroup(drive))
2357                 return -EBUSY;
2358         switch (setting->data_type) {
2359                 case TYPE_BYTE:
2360                         *((u8 *) setting->data) = val;
2361                         break;
2362                 case TYPE_SHORT:
2363                         *((u16 *) setting->data) = val;
2364                         break;
2365                 case TYPE_INT:
2366                         *((u32 *) setting->data) = val;
2367                         break;
2368                 case TYPE_INTA:
2369                         p = (u32 *) setting->data;
2370                         for (i = 0; i < 1 << PARTN_BITS; i++, p++)
2371                                 *p = val;
2372                         break;
2373         }
2374         spin_unlock_irq(&io_request_lock);
2375         return 0;
2376 }
2377 
2378 static int set_io_32bit(ide_drive_t *drive, int arg)
2379 {
2380         drive->io_32bit = arg;
2381 #ifdef CONFIG_BLK_DEV_DTC2278
2382         if (HWIF(drive)->chipset == ide_dtc2278)
2383                 HWIF(drive)->drives[!drive->select.b.unit].io_32bit = arg;
2384 #endif /* CONFIG_BLK_DEV_DTC2278 */
2385         return 0;
2386 }
2387 
2388 static int set_using_dma (ide_drive_t *drive, int arg)
2389 {
2390         if (!drive->driver || !DRIVER(drive)->supports_dma)
2391                 return -EPERM;
2392         if (!drive->id || !(drive->id->capability & 1) || !HWIF(drive)->dmaproc)
2393                 return -EPERM;
2394         if (HWIF(drive)->dmaproc(arg ? ide_dma_on : ide_dma_off, drive))
2395                 return -EIO;
2396         return 0;
2397 }
2398 
2399 static int set_pio_mode (ide_drive_t *drive, int arg)
2400 {
2401         struct request rq;
2402 
2403         if (!HWIF(drive)->tuneproc)
2404                 return -ENOSYS;
2405         if (drive->special.b.set_tune)
2406                 return -EBUSY;
2407         ide_init_drive_cmd(&rq);
2408         drive->tune_req = (byte) arg;
2409         drive->special.b.set_tune = 1;
2410         (void) ide_do_drive_cmd (drive, &rq, ide_wait);
2411         return 0;
2412 }
2413 
2414 void ide_add_generic_settings (ide_drive_t *drive)
2415 {
2416 /*
2417  *                      drive   setting name            read/write access                               read ioctl              write ioctl             data type       min     max                             mul_factor      div_factor      data pointer                    set function
2418  */
2419         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);
2420         ide_add_setting(drive,  "keepsettings",         SETTING_RW,                                     HDIO_GET_KEEPSETTINGS,  HDIO_SET_KEEPSETTINGS,  TYPE_BYTE,      0,      1,                              1,              1,              &drive->keep_settings,          NULL);
2421         ide_add_setting(drive,  "nice1",                SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      1,                              1,              1,              &drive->nice1,                  NULL);
2422         ide_add_setting(drive,  "pio_mode",             SETTING_WRITE,                                  -1,                     HDIO_SET_PIO_MODE,      TYPE_BYTE,      0,      255,                            1,              1,              NULL,                           set_pio_mode);
2423         ide_add_setting(drive,  "slow",                 SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      1,                              1,              1,              &drive->slow,                   NULL);
2424         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);
2425         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);
2426         ide_add_setting(drive,  "ide_scsi",             SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      1,                              1,              1,              &drive->scsi,                   NULL);
2427         ide_add_setting(drive,  "init_speed",           SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      69,                             1,              1,              &drive->init_speed,             NULL);
2428         ide_add_setting(drive,  "current_speed",        SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      69,                             1,              1,              &drive->current_speed,          NULL);
2429         ide_add_setting(drive,  "number",               SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      3,                              1,              1,              &drive->dn,                     NULL);
2430 }
2431 
2432 int ide_wait_cmd (ide_drive_t *drive, int cmd, int nsect, int feature, int sectors, byte *buf)
2433 {
2434         struct request rq;
2435         byte buffer[4];
2436 
2437         if (!buf)
2438                 buf = buffer;
2439         memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
2440         ide_init_drive_cmd(&rq);
2441         rq.buffer = buf;
2442         *buf++ = cmd;
2443         *buf++ = nsect;
2444         *buf++ = feature;
2445         *buf++ = sectors;
2446         return ide_do_drive_cmd(drive, &rq, ide_wait);
2447 }
2448 
2449 int ide_wait_cmd_task (ide_drive_t *drive, byte *buf)
2450 {
2451         struct request rq;
2452 
2453         ide_init_drive_cmd(&rq);
2454         rq.cmd = IDE_DRIVE_TASK;
2455         rq.buffer = buf;
2456         return ide_do_drive_cmd(drive, &rq, ide_wait);
2457 }
2458 
2459 /*
2460  * Delay for *at least* 50ms.  As we don't know how much time is left
2461  * until the next tick occurs, we wait an extra tick to be safe.
2462  * This is used only during the probing/polling for drives at boot time.
2463  *
2464  * However, its usefullness may be needed in other places, thus we export it now.
2465  * The future may change this to a millisecond setable delay.
2466  */
2467 void ide_delay_50ms (void)
2468 {
2469 #ifndef CONFIG_BLK_DEV_IDECS
2470         unsigned long timeout = jiffies + ((HZ + 19)/20) + 1;
2471         while (0 < (signed long)(timeout - jiffies));
2472 #else
2473         __set_current_state(TASK_UNINTERRUPTIBLE);
2474         schedule_timeout(HZ/20);
2475 #endif /* CONFIG_BLK_DEV_IDECS */
2476 }
2477 
2478 int system_bus_clock (void)
2479 {
2480         return((int) ((!system_bus_speed) ? ide_system_bus_speed() : system_bus_speed ));
2481 }
2482 
2483 static int ide_ioctl (struct inode *inode, struct file *file,
2484                         unsigned int cmd, unsigned long arg)
2485 {
2486         int err = 0, major, minor;
2487         ide_drive_t *drive;
2488         struct request rq;
2489         kdev_t dev;
2490         ide_settings_t *setting;
2491 
2492         if (!inode || !(dev = inode->i_rdev))
2493                 return -EINVAL;
2494         major = MAJOR(dev); minor = MINOR(dev);
2495         if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
2496                 return -ENODEV;
2497 
2498         if ((setting = ide_find_setting_by_ioctl(drive, cmd)) != NULL) {
2499                 if (cmd == setting->read_ioctl) {
2500                         err = ide_read_setting(drive, setting);
2501                         return err >= 0 ? put_user(err, (long *) arg) : err;
2502                 } else {
2503                         if ((MINOR(inode->i_rdev) & PARTN_MASK))
2504                                 return -EINVAL;
2505                         return ide_write_setting(drive, setting, arg);
2506                 }
2507         }
2508 
2509         ide_init_drive_cmd (&rq);
2510         switch (cmd) {
2511                 case HDIO_GETGEO:
2512                 {
2513                         struct hd_geometry *loc = (struct hd_geometry *) arg;
2514                         unsigned short bios_cyl = drive->bios_cyl; /* truncate */
2515                         if (!loc || (drive->media != ide_disk && drive->media != ide_floppy)) return -EINVAL;
2516                         if (put_user(drive->bios_head, (byte *) &loc->heads)) return -EFAULT;
2517                         if (put_user(drive->bios_sect, (byte *) &loc->sectors)) return -EFAULT;
2518                         if (put_user(bios_cyl, (unsigned short *) &loc->cylinders)) return -EFAULT;
2519                         if (put_user((unsigned)drive->part[MINOR(inode->i_rdev)&PARTN_MASK].start_sect,
2520                                 (unsigned long *) &loc->start)) return -EFAULT;
2521                         return 0;
2522                 }
2523 
2524                 case HDIO_GETGEO_BIG:
2525                 {
2526                         struct hd_big_geometry *loc = (struct hd_big_geometry *) arg;
2527                         if (!loc || (drive->media != ide_disk && drive->media != ide_floppy)) return -EINVAL;
2528                         if (put_user(drive->bios_head, (byte *) &loc->heads)) return -EFAULT;
2529                         if (put_user(drive->bios_sect, (byte *) &loc->sectors)) return -EFAULT;
2530                         if (put_user(drive->bios_cyl, (unsigned int *) &loc->cylinders)) return -EFAULT;
2531                         if (put_user((unsigned)drive->part[MINOR(inode->i_rdev)&PARTN_MASK].start_sect,
2532                                 (unsigned long *) &loc->start)) return -EFAULT;
2533                         return 0;
2534                 }
2535 
2536                 case HDIO_GETGEO_BIG_RAW:
2537                 {
2538                         struct hd_big_geometry *loc = (struct hd_big_geometry *) arg;
2539                         if (!loc || (drive->media != ide_disk && drive->media != ide_floppy)) return -EINVAL;
2540                         if (put_user(drive->head, (byte *) &loc->heads)) return -EFAULT;
2541                         if (put_user(drive->sect, (byte *) &loc->sectors)) return -EFAULT;
2542                         if (put_user(drive->cyl, (unsigned int *) &loc->cylinders)) return -EFAULT;
2543                         if (put_user((unsigned)drive->part[MINOR(inode->i_rdev)&PARTN_MASK].start_sect,
2544                                 (unsigned long *) &loc->start)) return -EFAULT;
2545                         return 0;
2546                 }
2547 
2548                 case BLKGETSIZE:   /* Return device size */
2549                         return put_user(drive->part[MINOR(inode->i_rdev)&PARTN_MASK].nr_sects, (long *) arg);
2550 
2551                 case BLKRRPART: /* Re-read partition tables */
2552                         if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2553                         return ide_revalidate_disk(inode->i_rdev);
2554 
2555                 case HDIO_OBSOLETE_IDENTITY:
2556                 case HDIO_GET_IDENTITY:
2557                         if (MINOR(inode->i_rdev) & PARTN_MASK)
2558                                 return -EINVAL;
2559                         if (drive->id == NULL)
2560                                 return -ENOMSG;
2561                         if (copy_to_user((char *)arg, (char *)drive->id, (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
2562                                 return -EFAULT;
2563                         return 0;
2564 
2565                 case HDIO_GET_NICE:
2566                         return put_user(drive->dsc_overlap      <<      IDE_NICE_DSC_OVERLAP    |
2567                                         drive->atapi_overlap    <<      IDE_NICE_ATAPI_OVERLAP  |
2568                                         drive->nice0            <<      IDE_NICE_0              |
2569                                         drive->nice1            <<      IDE_NICE_1              |
2570                                         drive->nice2            <<      IDE_NICE_2,
2571                                         (long *) arg);
2572                 case HDIO_DRIVE_CMD:
2573                 {
2574                         byte args[4], *argbuf = args;
2575                         byte xfer_rate = 0;
2576                         int argsize = 4;
2577                         if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) return -EACCES;
2578                         if (NULL == (void *) arg)
2579                                 return ide_do_drive_cmd(drive, &rq, ide_wait);
2580                         if (copy_from_user(args, (void *)arg, 4))
2581                                 return -EFAULT;
2582                         if (args[3]) {
2583                                 argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
2584                                 argbuf = kmalloc(argsize, GFP_KERNEL);
2585                                 if (argbuf == NULL)
2586                                         return -ENOMEM;
2587                                 memcpy(argbuf, args, 4);
2588                         }
2589 
2590                         if (set_transfer(drive, args[0], args[1], args[2])) {
2591                                 xfer_rate = args[1];
2592                                 if (ide_ata66_check(drive, args[0], args[1], args[2]))
2593                                         goto abort;
2594                         }
2595 
2596                         err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
2597 
2598                         if (!err && xfer_rate) {
2599                                 /* active-retuning-calls future */
2600                                 if ((HWIF(drive)->speedproc) != NULL)
2601                                         HWIF(drive)->speedproc(drive, xfer_rate);
2602                                 ide_driveid_update(drive);
2603                         }
2604                 abort:
2605                         if (copy_to_user((void *)arg, argbuf, argsize))
2606                                 err = -EFAULT;
2607                         if (argsize > 4)
2608                                 kfree(argbuf);
2609                         return err;
2610                 }
2611                 case HDIO_DRIVE_TASK:
2612                 {
2613                         byte args[7], *argbuf = args;
2614                         int argsize = 7;
2615                         if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) return -EACCES;
2616                         if (copy_from_user(args, (void *)arg, 7))
2617                                 return -EFAULT;
2618                         err = ide_wait_cmd_task(drive, argbuf);
2619                         if (copy_to_user((void *)arg, argbuf, argsize))
2620                                 err = -EFAULT;
2621                         return err;
2622                 }
2623                 case HDIO_SCAN_HWIF:
2624                 {
2625                         int args[3];
2626                         if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2627                         if (copy_from_user(args, (void *)arg, 3 * sizeof(int)))
2628                                 return -EFAULT;
2629                         if (ide_register(args[0], args[1], args[2]) == -1)
2630                                 return -EIO;
2631                         return 0;
2632                 }
2633                 case HDIO_UNREGISTER_HWIF:
2634                         if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2635                         /* (arg > MAX_HWIFS) checked in function */
2636                         ide_unregister(arg);
2637                         return 0;
2638                 case HDIO_SET_NICE:
2639                         if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2640                         if (drive->driver == NULL)
2641                                 return -EPERM;
2642                         if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
2643                                 return -EPERM;
2644                         drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
2645                         if (drive->dsc_overlap && !DRIVER(drive)->supports_dsc_overlap) {
2646                                 drive->dsc_overlap = 0;
2647                                 return -EPERM;
2648                         }
2649                         drive->nice1 = (arg >> IDE_NICE_1) & 1;
2650                         return 0;
2651                 case HDIO_DRIVE_RESET:
2652                         if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2653                         (void) ide_do_reset(drive);
2654                         if (drive->suspend_reset) {
2655 /*
2656  *                              APM WAKE UP todo !!
2657  *                              int nogoodpower = 1;
2658  *                              while(nogoodpower) {
2659  *                                      check_power1() or check_power2()
2660  *                                      nogoodpower = 0;
2661  *                              } 
2662  *                              HWIF(drive)->multiproc(drive);
2663  */
2664                                 return ide_revalidate_disk(inode->i_rdev);
2665                         }
2666                         return 0;
2667 
2668                 case BLKROSET:
2669                 case BLKROGET:
2670                 case BLKFLSBUF:
2671                 case BLKSSZGET:
2672                 case BLKPG:
2673                 case BLKELVGET:
2674                 case BLKELVSET:
2675                         return blk_ioctl(inode->i_rdev, cmd, arg);
2676 
2677                 default:
2678                         if (drive->driver != NULL)
2679                                 return DRIVER(drive)->ioctl(drive, inode, file, cmd, arg);
2680                         return -EPERM;
2681         }
2682 }
2683 
2684 static int ide_check_media_change (kdev_t i_rdev)
2685 {
2686         ide_drive_t *drive;
2687 
2688         if ((drive = get_info_ptr(i_rdev)) == NULL)
2689                 return -ENODEV;
2690         if (drive->driver != NULL)
2691                 return DRIVER(drive)->media_change(drive);
2692         return 0;
2693 }
2694 
2695 void ide_fixstring (byte *s, const int bytecount, const int byteswap)
2696 {
2697         byte *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */
2698 
2699         if (byteswap) {
2700                 /* convert from big-endian to host byte order */
2701                 for (p = end ; p != s;) {
2702                         unsigned short *pp = (unsigned short *) (p -= 2);
2703                         *pp = ntohs(*pp);
2704                 }
2705         }
2706 
2707         /* strip leading blanks */
2708         while (s != end && *s == ' ')
2709                 ++s;
2710 
2711         /* compress internal blanks and strip trailing blanks */
2712         while (s != end && *s) {
2713                 if (*s++ != ' ' || (s != end && *s && *s != ' '))
2714                         *p++ = *(s-1);
2715         }
2716 
2717         /* wipe out trailing garbage */
2718         while (p != end)
2719                 *p++ = '\0';
2720 }
2721 
2722 /*
2723  * stridx() returns the offset of c within s,
2724  * or -1 if c is '\0' or not found within s.
2725  */
2726 static int __init stridx (const char *s, char c)
2727 {
2728         char *i = strchr(s, c);
2729         return (i && c) ? i - s : -1;
2730 }
2731 
2732 /*
2733  * match_parm() does parsing for ide_setup():
2734  *
2735  * 1. the first char of s must be '='.
2736  * 2. if the remainder matches one of the supplied keywords,
2737  *     the index (1 based) of the keyword is negated and returned.
2738  * 3. if the remainder is a series of no more than max_vals numbers
2739  *     separated by commas, the numbers are saved in vals[] and a
2740  *     count of how many were saved is returned.  Base10 is assumed,
2741  *     and base16 is allowed when prefixed with "0x".
2742  * 4. otherwise, zero is returned.
2743  */
2744 static int __init match_parm (char *s, const char *keywords[], int vals[], int max_vals)
2745 {
2746         static const char *decimal = "0123456789";
2747         static const char *hex = "0123456789abcdef";
2748         int i, n;
2749 
2750         if (*s++ == '=') {
2751                 /*
2752                  * Try matching against the supplied keywords,
2753                  * and return -(index+1) if we match one
2754                  */
2755                 if (keywords != NULL) {
2756                         for (i = 0; *keywords != NULL; ++i) {
2757                                 if (!strcmp(s, *keywords++))
2758                                         return -(i+1);
2759                         }
2760                 }
2761                 /*
2762                  * Look for a series of no more than "max_vals"
2763                  * numeric values separated by commas, in base10,
2764                  * or base16 when prefixed with "0x".
2765                  * Return a count of how many were found.
2766                  */
2767                 for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
2768                         vals[n] = i;
2769                         while ((i = stridx(decimal, *++s)) >= 0)
2770                                 vals[n] = (vals[n] * 10) + i;
2771                         if (*s == 'x' && !vals[n]) {
2772                                 while ((i = stridx(hex, *++s)) >= 0)
2773                                         vals[n] = (vals[n] * 0x10) + i;
2774                         }
2775                         if (++n == max_vals)
2776                                 break;
2777                         if (*s == ',' || *s == ';')
2778                                 ++s;
2779                 }
2780                 if (!*s)
2781                         return n;
2782         }
2783         return 0;       /* zero = nothing matched */
2784 }
2785 
2786 /*
2787  * ide_setup() gets called VERY EARLY during initialization,
2788  * to handle kernel "command line" strings beginning with "hdx="
2789  * or "ide".  Here is the complete set currently supported:
2790  *
2791  * "hdx="  is recognized for all "x" from "a" to "h", such as "hdc".
2792  * "idex=" is recognized for all "x" from "" to "3", such as "ide1".
2793  *
2794  * "hdx=noprobe"        : drive may be present, but do not probe for it
2795  * "hdx=none"           : drive is NOT present, ignore cmos and do not probe
2796  * "hdx=nowerr"         : ignore the WRERR_STAT bit on this drive
2797  * "hdx=cdrom"          : drive is present, and is a cdrom drive
2798  * "hdx=cyl,head,sect"  : disk drive is present, with specified geometry
2799  * "hdx=noremap"        : do not remap 0->1 even though EZD was detected
2800  * "hdx=autotune"       : driver will attempt to tune interface speed
2801  *                              to the fastest PIO mode supported,
2802  *                              if possible for this drive only.
2803  *                              Not fully supported by all chipset types,
2804  *                              and quite likely to cause trouble with
2805  *                              older/odd IDE drives.
2806  *
2807  * "hdx=slow"           : insert a huge pause after each access to the data
2808  *                              port. Should be used only as a last resort.
2809  *
2810  * "hdx=swapdata"       : when the drive is a disk, byte swap all data
2811  * "hdx=bswap"          : same as above..........
2812  * "hdxlun=xx"          : set the drive last logical unit.
2813  * "hdx=flash"          : allows for more than one ata_flash disk to be
2814  *                              registered. In most cases, only one device
2815  *                              will be present.
2816  * "hdx=scsi"           : the return of the ide-scsi flag, this is useful for
2817  *                              allowwing ide-floppy, ide-tape, and ide-cdrom|writers
2818  *                              to use ide-scsi emulation on a device specific option.
2819  * "idebus=xx"          : inform IDE driver of VESA/PCI bus speed in MHz,
2820  *                              where "xx" is between 20 and 66 inclusive,
2821  *                              used when tuning chipset PIO modes.
2822  *                              For PCI bus, 25 is correct for a P75 system,
2823  *                              30 is correct for P90,P120,P180 systems,
2824  *                              and 33 is used for P100,P133,P166 systems.
2825  *                              If in doubt, use idebus=33 for PCI.
2826  *                              As for VLB, it is safest to not specify it.
2827  *
2828  * "idex=noprobe"       : do not attempt to access/use this interface
2829  * "idex=base"          : probe for an interface at the addr specified,
2830  *                              where "base" is usually 0x1f0 or 0x170
2831  *                              and "ctl" is assumed to be "base"+0x206
2832  * "idex=base,ctl"      : specify both base and ctl
2833  * "idex=base,ctl,irq"  : specify base, ctl, and irq number
2834  * "idex=autotune"      : driver will attempt to tune interface speed
2835  *                              to the fastest PIO mode supported,
2836  *                              for all drives on this interface.
2837  *                              Not fully supported by all chipset types,
2838  *                              and quite likely to cause trouble with
2839  *                              older/odd IDE drives.
2840  * "idex=noautotune"    : driver will NOT attempt to tune interface speed
2841  *                              This is the default for most chipsets,
2842  *                              except the cmd640.
2843  * "idex=serialize"     : do not overlap operations on idex and ide(x^1)
2844  * "idex=four"          : four drives on idex and ide(x^1) share same ports
2845  * "idex=reset"         : reset interface before first use
2846  * "idex=dma"           : enable DMA by default on both drives if possible
2847  * "idex=ata66"         : informs the interface that it has an 80c cable
2848  *                              for chipsets that are ATA-66 capable, but
2849  *                              the ablity to bit test for detection is
2850  *                              currently unknown.
2851  * "ide=reverse"        : Formerly called to pci sub-system, but now local.
2852  *
2853  * The following are valid ONLY on ide0, (except dc4030)
2854  * and the defaults for the base,ctl ports must not be altered.
2855  *
2856  * "ide0=dtc2278"       : probe/support DTC2278 interface
2857  * "ide0=ht6560b"       : probe/support HT6560B interface
2858  * "ide0=cmd640_vlb"    : *REQUIRED* for VLB cards with the CMD640 chip
2859  *                        (not for PCI -- automatically detected)
2860  * "ide0=qd6580"        : probe/support qd6580 interface
2861  * "ide0=ali14xx"       : probe/support ali14xx chipsets (ALI M1439, M1443, M1445)
2862  * "ide0=umc8672"       : probe/support umc8672 chipsets
2863  * "idex=dc4030"        : probe/support Promise DC4030VL interface
2864  * "ide=doubler"        : probe/support IDE doublers on Amiga
2865  */
2866 int __init ide_setup (char *s)
2867 {
2868         int i, vals[3];
2869         ide_hwif_t *hwif;
2870         ide_drive_t *drive;
2871         unsigned int hw, unit;
2872         const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
2873         const char max_hwif  = '' + (MAX_HWIFS - 1);
2874 
2875         
2876         if (strncmp(s,"hd",2) == 0 && s[2] == '=')      /* hd= is for hd.c   */
2877                 return 0;                               /* driver and not us */
2878 
2879         if (strncmp(s,"ide",3) &&
2880             strncmp(s,"idebus",6) &&
2881             strncmp(s,"hd",2))          /* hdx= & hdxlun= */
2882                 return 0;
2883 
2884         printk("ide_setup: %s", s);
2885         init_ide_data ();
2886 
2887 #ifdef CONFIG_BLK_DEV_IDEDOUBLER
2888         if (!strcmp(s, "ide=doubler")) {
2889                 extern int ide_doubler;
2890 
2891                 printk(" : Enabled support for IDE doublers\n");
2892                 ide_doubler = 1;
2893                 return 1;
2894         }
2895 #endif /* CONFIG_BLK_DEV_IDEDOUBLER */
2896 
2897 #ifdef CONFIG_BLK_DEV_IDEPCI
2898         if (!strcmp(s, "ide=reverse")) {
2899                 ide_scan_direction = 1;
2900                 printk(" : Enabled support for IDE inverse scan order.\n");
2901                 return 1;
2902         }
2903 #endif /* CONFIG_BLK_DEV_IDEPCI */
2904 
2905         /*
2906          * Look for drive options:  "hdx="
2907          */
2908         if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
2909                 const char *hd_words[] = {"none", "noprobe", "nowerr", "cdrom",
2910                                 "serialize", "autotune", "noautotune",
2911                                 "slow", "swapdata", "bswap", "flash",
2912                                 "remap", "noremap", "scsi", NULL};
2913                 unit = s[2] - 'a';
2914                 hw   = unit / MAX_DRIVES;
2915                 unit = unit % MAX_DRIVES;
2916                 hwif = &ide_hwifs[hw];
2917                 drive = &hwif->drives[unit];
2918                 if (strncmp(s + 4, "ide-", 4) == 0) {
2919                         strncpy(drive->driver_req, s + 4, 9);
2920                         goto done;
2921                 }
2922                 /*
2923                  * Look for last lun option:  "hdxlun="
2924                  */
2925                 if (s[3] == 'l' && s[4] == 'u' && s[5] == 'n') {
2926                         if (match_parm(&s[6], NULL, vals, 1) != 1)
2927                                 goto bad_option;
2928                         if (vals[0] >= 0 && vals[0] <= 7) {
2929                                 drive->last_lun = vals[0];
2930                                 drive->forced_lun = 1;
2931                         } else
2932                                 printk(" -- BAD LAST LUN! Expected value from 0 to 7");
2933                         goto done;
2934                 }
2935                 switch (match_parm(&s[3], hd_words, vals, 3)) {
2936                         case -1: /* "none" */
2937                                 drive->nobios = 1;  /* drop into "noprobe" */
2938                         case -2: /* "noprobe" */
2939                                 drive->noprobe = 1;
2940                                 goto done;
2941                         case -3: /* "nowerr" */
2942                                 drive->bad_wstat = BAD_R_STAT;
2943                                 hwif->noprobe = 0;
2944                                 goto done;
2945                         case -4: /* "cdrom" */
2946                                 drive->present = 1;
2947                                 drive->media = ide_cdrom;
2948                                 hwif->noprobe = 0;
2949                                 goto done;
2950                         case -5: /* "serialize" */
2951                                 printk(" -- USE \"ide%d=serialize\" INSTEAD", hw);
2952                                 goto do_serialize;
2953                         case -6: /* "autotune" */
2954                                 drive->autotune = 1;
2955                                 goto done;
2956                         case -7: /* "noautotune" */
2957                                 drive->autotune = 2;
2958                                 goto done;
2959                         case -8: /* "slow" */
2960                                 drive->slow = 1;
2961                                 goto done;
2962                         case -9: /* "swapdata" or "bswap" */
2963                         case -10:
2964                                 drive->bswap = 1;
2965                                 goto done;
2966                         case -11: /* "flash" */
2967                                 drive->ata_flash = 1;
2968                                 goto done;
2969                         case -12: /* "remap" */
2970                                 drive->remap_0_to_1 = 1;
2971                                 goto done;
2972                         case -13: /* "noremap" */
2973                                 drive->remap_0_to_1 = 2;
2974                                 goto done;
2975                         case -14: /* "scsi" */
2976 #if defined(CONFIG_BLK_DEV_IDESCSI) && defined(CONFIG_SCSI)
2977                                 drive->scsi = 1;
2978                                 goto done;
2979 #else
2980                                 drive->scsi = 0;
2981                                 goto bad_option;
2982 #endif /* defined(CONFIG_BLK_DEV_IDESCSI) && defined(CONFIG_SCSI) */
2983                         case 3: /* cyl,head,sect */
2984                                 drive->media    = ide_disk;
2985                                 drive->cyl      = drive->bios_cyl  = vals[0];
2986                                 drive->head     = drive->bios_head = vals[1];
2987                                 drive->sect     = drive->bios_sect = vals[2];
2988                                 drive->present  = 1;
2989                                 drive->forced_geom = 1;
2990                                 hwif->noprobe = 0;
2991                                 goto done;
2992                         default:
2993                                 goto bad_option;
2994                 }
2995         }
2996 
2997         if (s[0] != 'i' || s[1] != 'd' || s[2] != 'e')
2998                 goto bad_option;
2999         /*
3000          * Look for bus speed option:  "idebus="
3001          */
3002         if (s[3] == 'b' && s[4] == 'u' && s[5] == 's') {
3003                 if (match_parm(&s[6], NULL, vals, 1) != 1)
3004                         goto bad_option;
3005                 if (vals[0] >= 20 && vals[0] <= 66) {
3006                         idebus_parameter = vals[0];
3007                 } else
3008                         printk(" -- BAD BUS SPEED! Expected value from 20 to 66");
3009                 goto done;
3010         }
3011         /*
3012          * Look for interface options:  "idex="
3013          */
3014         if (s[3] >= '' && s[3] <= max_hwif) {
3015                 /*
3016                  * Be VERY CAREFUL changing this: note hardcoded indexes below
3017                  * -8,-9,-10 : are reserved for future idex calls to ease the hardcoding.
3018                  */
3019                 const char *ide_words[] = {
3020                         "noprobe", "serialize", "autotune", "noautotune", "reset", "dma", "ata66",
3021                         "minus8", "minus9", "minus10",
3022                         "four", "qd6580", "ht6560b", "cmd640_vlb", "dtc2278", "umc8672", "ali14xx", "dc4030", NULL };
3023                 hw = s[3] - '';
3024                 hwif = &ide_hwifs[hw];
3025                 i = match_parm(&s[4], ide_words, vals, 3);
3026 
3027                 /*
3028                  * Cryptic check to ensure chipset not already set for hwif:
3029                  */
3030                 if (i > 0 || i <= -11) {                        /* is parameter a chipset name? */
3031                         if (hwif->chipset != ide_unknown)
3032                                 goto bad_option;        /* chipset already specified */
3033                         if (i <= -11 && i != -18 && hw != 0)
3034                                 goto bad_hwif;          /* chipset drivers are for "ide0=" only */
3035                         if (i <= -11 && i != -18 && ide_hwifs[hw+1].chipset != ide_unknown)
3036                                 goto bad_option;        /* chipset for 2nd port already specified */
3037                         printk("\n");
3038                 }
3039 
3040                 switch (i) {
3041 #ifdef CONFIG_BLK_DEV_PDC4030
3042                         case -18: /* "dc4030" */
3043                         {
3044                                 extern void init_pdc4030(void);
3045                                 init_pdc4030();
3046                                 goto done;
3047                         }
3048 #endif /* CONFIG_BLK_DEV_PDC4030 */
3049 #ifdef CONFIG_BLK_DEV_ALI14XX
3050                         case -17: /* "ali14xx" */
3051                         {
3052                                 extern void init_ali14xx (void);
3053                                 init_ali14xx();
3054                                 goto done;
3055                         }
3056 #endif /* CONFIG_BLK_DEV_ALI14XX */
3057 #ifdef CONFIG_BLK_DEV_UMC8672
3058                         case -16: /* "umc8672" */
3059                         {
3060                                 extern void init_umc8672 (void);
3061                                 init_umc8672();
3062                                 goto done;
3063                         }
3064 #endif /* CONFIG_BLK_DEV_UMC8672 */
3065 #ifdef CONFIG_BLK_DEV_DTC2278
3066                         case -15: /* "dtc2278" */
3067                         {
3068                                 extern void init_dtc2278 (void);
3069                                 init_dtc2278();
3070                                 goto done;
3071                         }
3072 #endif /* CONFIG_BLK_DEV_DTC2278 */
3073 #ifdef CONFIG_BLK_DEV_CMD640
3074                         case -14: /* "cmd640_vlb" */
3075                         {
3076                                 extern int cmd640_vlb; /* flag for cmd640.c */
3077                                 cmd640_vlb = 1;
3078                                 goto done;
3079                         }
3080 #endif /* CONFIG_BLK_DEV_CMD640 */
3081 #ifdef CONFIG_BLK_DEV_HT6560B
3082                         case -13: /* "ht6560b" */
3083                         {
3084                                 extern void init_ht6560b (void);
3085                                 init_ht6560b();
3086                                 goto done;
3087                         }
3088 #endif /* CONFIG_BLK_DEV_HT6560B */
3089 #if CONFIG_BLK_DEV_QD6580
3090                         case -12: /* "qd6580" */
3091                         {
3092                                 extern void init_qd6580 (void);
3093                                 init_qd6580();
3094                                 goto done;
3095                         }
3096 #endif /* CONFIG_BLK_DEV_QD6580 */
3097 #ifdef CONFIG_BLK_DEV_4DRIVES
3098                         case -11: /* "four" drives on one set of ports */
3099                         {
3100                                 ide_hwif_t *mate = &ide_hwifs[hw^1];
3101                                 mate->drives[0].select.all ^= 0x20;
3102                                 mate->drives[1].select.all ^= 0x20;
3103                                 hwif->chipset = mate->chipset = ide_4drives;
3104                                 mate->irq = hwif->irq;
3105                                 memcpy(mate->io_ports, hwif->io_ports, sizeof(hwif->io_ports));
3106                                 goto do_serialize;
3107                         }
3108 #endif /* CONFIG_BLK_DEV_4DRIVES */
3109                         case -10: /* minus10 */
3110                         case -9: /* minus9 */
3111                         case -8: /* minus8 */
3112                                 goto bad_option;
3113                         case -7: /* ata66 */
3114 #ifdef CONFIG_BLK_DEV_IDEPCI
3115                                 hwif->udma_four = 1;
3116                                 goto done;
3117 #else /* !CONFIG_BLK_DEV_IDEPCI */
3118                                 hwif->udma_four = 0;
3119                                 goto bad_hwif;
3120 #endif /* CONFIG_BLK_DEV_IDEPCI */
3121                         case -6: /* dma */
3122                                 hwif->autodma = 1;
3123                                 goto done;
3124                         case -5: /* "reset" */
3125                                 hwif->reset = 1;
3126                                 goto done;
3127                         case -4: /* "noautotune" */
3128                                 hwif->drives[0].autotune = 2;
3129                                 hwif->drives[1].autotune = 2;
3130                                 goto done;
3131                         case -3: /* "autotune" */
3132                                 hwif->drives[0].autotune = 1;
3133                                 hwif->drives[1].autotune = 1;
3134                                 goto done;
3135                         case -2: /* "serialize" */
3136                         do_serialize:
3137                                 hwif->mate = &ide_hwifs[hw^1];
3138                                 hwif->mate->mate = hwif;
3139                                 hwif->serialized = hwif->mate->serialized = 1;
3140                                 goto done;
3141 
3142                         case -1: /* "noprobe" */
3143                                 hwif->noprobe = 1;
3144                                 goto done;
3145 
3146                         case 1: /* base */
3147                                 vals[1] = vals[0] + 0x206; /* default ctl */
3148                         case 2: /* base,ctl */
3149                                 vals[2] = 0;    /* default irq = probe for it */
3150                         case 3: /* base,ctl,irq */
3151                                 hwif->hw.irq = vals[2];
3152                                 ide_init_hwif_ports(&hwif->hw, (ide_ioreg_t) vals[0], (ide_ioreg_t) vals[1], &hwif->irq);
3153                                 memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->io_ports));
3154                                 hwif->irq      = vals[2];
3155                                 hwif->noprobe  = 0;
3156                                 hwif->chipset  = ide_generic;
3157                                 goto done;
3158 
3159                         case 0: goto bad_option;
3160                         default:
3161                                 printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
3162                                 return 1;
3163                 }
3164         }
3165 bad_option:
3166         printk(" -- BAD OPTION\n");
3167         return 1;
3168 bad_hwif:
3169         printk("-- NOT SUPPORTED ON ide%d", hw);
3170 done:
3171         printk("\n");
3172         return 1;
3173 }
3174 
3175 /*
3176  * probe_for_hwifs() finds/initializes "known" IDE interfaces
3177  */
3178 static void __init probe_for_hwifs (void)
3179 {
3180 #ifdef CONFIG_PCI
3181         if (pci_present())
3182         {
3183 #ifdef CONFIG_BLK_DEV_IDEPCI
3184                 ide_scan_pcibus(ide_scan_direction);
3185 #else
3186 #ifdef CONFIG_BLK_DEV_RZ1000
3187                 {
3188                         extern void ide_probe_for_rz100x(void);
3189                         ide_probe_for_rz100x();
3190                 }
3191 #endif /* CONFIG_BLK_DEV_RZ1000 */
3192 #endif /* CONFIG_BLK_DEV_IDEPCI */
3193         }
3194 #endif /* CONFIG_PCI */
3195 
3196 #ifdef CONFIG_BLK_DEV_CMD640
3197         {
3198                 extern void ide_probe_for_cmd640x(void);
3199                 ide_probe_for_cmd640x();
3200         }
3201 #endif /* CONFIG_BLK_DEV_CMD640 */
3202 #ifdef CONFIG_BLK_DEV_PDC4030
3203         {
3204                 extern int ide_probe_for_pdc4030(void);
3205                 (void) ide_probe_for_pdc4030();
3206         }
3207 #endif /* CONFIG_BLK_DEV_PDC4030 */
3208 #ifdef CONFIG_BLK_DEV_IDE_PMAC
3209         {
3210                 extern void pmac_ide_probe(void);
3211                 pmac_ide_probe();
3212         }
3213 #endif /* CONFIG_BLK_DEV_IDE_PMAC */
3214 #ifdef CONFIG_BLK_DEV_IDE_ICSIDE
3215         {
3216                 extern void icside_init(void);
3217                 icside_init();
3218         }
3219 #endif /* CONFIG_BLK_DEV_IDE_ICSIDE */
3220 #ifdef CONFIG_BLK_DEV_IDE_RAPIDE
3221         {
3222                 extern void rapide_init(void);
3223                 rapide_init();
3224         }
3225 #endif /* CONFIG_BLK_DEV_IDE_RAPIDE */
3226 #ifdef CONFIG_BLK_DEV_GAYLE
3227         {
3228                 extern void gayle_init(void);
3229                 gayle_init();
3230         }
3231 #endif /* CONFIG_BLK_DEV_GAYLE */
3232 #ifdef CONFIG_BLK_DEV_FALCON_IDE
3233         {
3234                 extern void falconide_init(void);
3235                 falconide_init();
3236         }
3237 #endif /* CONFIG_BLK_DEV_FALCON_IDE */
3238 #ifdef CONFIG_BLK_DEV_MAC_IDE
3239         {
3240                 extern void macide_init(void);
3241                 macide_init();
3242         }
3243 #endif /* CONFIG_BLK_DEV_MAC_IDE */
3244 #ifdef CONFIG_BLK_DEV_Q40IDE
3245         {
3246                 extern void q40ide_init(void);
3247                 q40ide_init();
3248         }
3249 #endif /* CONFIG_BLK_DEV_Q40IDE */
3250 #ifdef CONFIG_BLK_DEV_BUDDHA
3251         {
3252                 extern void buddha_init(void);
3253                 buddha_init();
3254         }
3255 #endif /* CONFIG_BLK_DEV_BUDDHA */
3256 #if defined(CONFIG_BLK_DEV_ISAPNP) && defined(CONFIG_ISAPNP)
3257         {
3258                 extern void pnpide_init(int enable);
3259                 pnpide_init(1);
3260         }
3261 #endif /* CONFIG_BLK_DEV_ISAPNP */
3262 }
3263 
3264 void __init ide_init_builtin_drivers (void)
3265 {
3266         /*
3267          * Probe for special PCI and other "known" interface chipsets
3268          */
3269         probe_for_hwifs ();
3270 
3271 #ifdef CONFIG_BLK_DEV_IDE
3272 #if defined(__mc68000__) || defined(CONFIG_APUS)
3273         if (ide_hwifs[0].io_ports[IDE_DATA_OFFSET]) {
3274                 ide_get_lock(&ide_lock, NULL, NULL);    /* for atari only */
3275                 disable_irq(ide_hwifs[0].irq);  /* disable_irq_nosync ?? */
3276 //              disable_irq_nosync(ide_hwifs[0].irq);
3277         }
3278 #endif /* __mc68000__ || CONFIG_APUS */
3279 
3280         (void) ideprobe_init();
3281 
3282 #if defined(__mc68000__) || defined(CONFIG_APUS)
3283         if (ide_hwifs[0].io_ports[IDE_DATA_OFFSET]) {
3284                 enable_irq(ide_hwifs[0].irq);
3285                 ide_release_lock(&ide_lock);    /* for atari only */
3286         }
3287 #endif /* __mc68000__ || CONFIG_APUS */
3288 #endif /* CONFIG_BLK_DEV_IDE */
3289 
3290 #ifdef CONFIG_PROC_FS
3291         proc_ide_create();
3292 #endif
3293 
3294         /*
3295          * Attempt to match drivers for the available drives
3296          */
3297 #ifdef CONFIG_BLK_DEV_IDEDISK
3298         (void) idedisk_init();
3299 #endif /* CONFIG_BLK_DEV_IDEDISK */
3300 #ifdef CONFIG_BLK_DEV_IDECD
3301         (void) ide_cdrom_init();
3302 #endif /* CONFIG_BLK_DEV_IDECD */
3303 #ifdef CONFIG_BLK_DEV_IDETAPE
3304         (void) idetape_init();
3305 #endif /* CONFIG_BLK_DEV_IDETAPE */
3306 #ifdef CONFIG_BLK_DEV_IDEFLOPPY
3307         (void) idefloppy_init();
3308 #endif /* CONFIG_BLK_DEV_IDEFLOPPY */
3309 #ifdef CONFIG_BLK_DEV_IDESCSI
3310  #ifdef CONFIG_SCSI
3311         (void) idescsi_init();
3312  #else
3313     #warning ide scsi-emulation selected but no SCSI-subsystem in kernel
3314  #endif
3315 #endif /* CONFIG_BLK_DEV_IDESCSI */
3316 }
3317 
3318 static int default_cleanup (ide_drive_t *drive)
3319 {
3320         return ide_unregister_subdriver(drive);
3321 }
3322 
3323 static ide_startstop_t default_do_request(ide_drive_t *drive, struct request *rq, unsigned long block)
3324 {
3325         ide_end_request(0, HWGROUP(drive));
3326         return ide_stopped;
3327 }
3328  
3329 static void default_end_request (byte uptodate, ide_hwgroup_t *hwgroup)
3330 {
3331         ide_end_request(uptodate, hwgroup);
3332 }
3333   
3334 static int default_ioctl (ide_drive_t *drive, struct inode *inode, struct file *file,
3335                           unsigned int cmd, unsigned long arg)
3336 {
3337         return -EIO;
3338 }
3339 
3340 static int default_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
3341 {
3342         drive->usage--;
3343         return -EIO;
3344 }
3345 
3346 static void default_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
3347 {
3348 }
3349 
3350 static int default_check_media_change (ide_drive_t *drive)
3351 {
3352         return 1;
3353 }
3354 
3355 static void default_pre_reset (ide_drive_t *drive)
3356 {
3357 }
3358 
3359 static unsigned long default_capacity (ide_drive_t *drive)
3360 {
3361         return 0x7fffffff;
3362 }
3363 
3364 static ide_startstop_t default_special (ide_drive_t *drive)
3365 {
3366         special_t *s = &drive->special;
3367 
3368         s->all = 0;
3369         drive->mult_req = 0;
3370         return ide_stopped;
3371 }
3372 
3373 static void setup_driver_defaults (ide_drive_t *drive)
3374 {
3375         ide_driver_t *d = drive->driver;
3376 
3377         if (d->cleanup == NULL)         d->cleanup = default_cleanup;
3378         if (d->do_request == NULL)      d->do_request = default_do_request;
3379         if (d->end_request == NULL)     d->end_request = default_end_request;
3380         if (d->ioctl == NULL)           d->ioctl = default_ioctl;
3381         if (d->open == NULL)            d->open = default_open;
3382         if (d->release == NULL)         d->release = default_release;
3383         if (d->media_change == NULL)    d->media_change = default_check_media_change;
3384         if (d->pre_reset == NULL)       d->pre_reset = default_pre_reset;
3385         if (d->capacity == NULL)        d->capacity = default_capacity;
3386         if (d->special == NULL)         d->special = default_special;
3387 }
3388 
3389 ide_drive_t *ide_scan_devices (byte media, const char *name, ide_driver_t *driver, int n)
3390 {
3391         unsigned int unit, index, i;
3392 
3393         for (index = 0, i = 0; index < MAX_HWIFS; ++index) {
3394                 ide_hwif_t *hwif = &ide_hwifs[index];
3395                 if (!hwif->present)
3396                         continue;
3397                 for (unit = 0; unit < MAX_DRIVES; ++unit) {
3398                         ide_drive_t *drive = &hwif->drives[unit];
3399                         char *req = drive->driver_req;
3400                         if (*req && !strstr(name, req))
3401                                 continue;
3402                         if (drive->present && drive->media == media && drive->driver == driver && ++i > n)
3403                                 return drive;
3404                 }
3405         }
3406         return NULL;
3407 }
3408 
3409 int ide_register_subdriver (ide_drive_t *drive, ide_driver_t *driver, int version)
3410 {
3411         unsigned long flags;
3412         
3413         save_flags(flags);              /* all CPUs */
3414         cli();                          /* all CPUs */
3415         if (version != IDE_SUBDRIVER_VERSION || !drive->present || drive->driver != NULL || drive->busy || drive->usage) {
3416                 restore_flags(flags);   /* all CPUs */
3417                 return 1;
3418         }
3419         drive->driver = driver;
3420         setup_driver_defaults(drive);
3421         restore_flags(flags);           /* all CPUs */
3422         if (drive->autotune != 2) {
3423                 if (driver->supports_dma && HWIF(drive)->dmaproc != NULL) {
3424                         /*
3425                          * Force DMAing for the beginning of the check.
3426                          * Some chipsets appear to do interesting things,
3427                          * if not checked and cleared.
3428                          *   PARANOIA!!!
3429                          */
3430                         (void) (HWIF(drive)->dmaproc(ide_dma_off_quietly, drive));
3431                         (void) (HWIF(drive)->dmaproc(ide_dma_check, drive));
3432                 }
3433                 drive->dsc_overlap = (drive->next != drive && driver->supports_dsc_overlap);
3434                 drive->nice1 = 1;
3435         }
3436         drive->revalidate = 1;
3437         drive->suspend_reset = 0;
3438 #ifdef CONFIG_PROC_FS
3439         ide_add_proc_entries(drive->proc, generic_subdriver_entries, drive);
3440         ide_add_proc_entries(drive->proc, driver->proc, drive);
3441 #endif
3442         return 0;
3443 }
3444 
3445 int ide_unregister_subdriver (ide_drive_t *drive)
3446 {
3447         unsigned long flags;
3448         
3449         save_flags(flags);              /* all CPUs */
3450         cli();                          /* all CPUs */
3451         if (drive->usage || drive->busy || drive->driver == NULL || DRIVER(drive)->busy) {
3452                 restore_flags(flags);   /* all CPUs */
3453                 return 1;
3454         }
3455 #if defined(CONFIG_BLK_DEV_ISAPNP) && defined(CONFIG_ISAPNP) && defined(MODULE)
3456         pnpide_init(0);
3457 #endif /* CONFIG_BLK_DEV_ISAPNP */
3458 #ifdef CONFIG_PROC_FS
3459         ide_remove_proc_entries(drive->proc, DRIVER(drive)->proc);
3460         ide_remove_proc_entries(drive->proc, generic_subdriver_entries);
3461 #endif
3462         auto_remove_settings(drive);
3463         drive->driver = NULL;
3464         restore_flags(flags);           /* all CPUs */
3465         return 0;
3466 }
3467 
3468 int ide_register_module (ide_module_t *module)
3469 {
3470         ide_module_t *p = ide_modules;
3471 
3472         while (p) {
3473                 if (p == module)
3474                         return 1;
3475                 p = p->next;
3476         }
3477         module->next = ide_modules;
3478         ide_modules = module;
3479         revalidate_drives();
3480         return 0;
3481 }
3482 
3483 void ide_unregister_module (ide_module_t *module)
3484 {
3485         ide_module_t **p;
3486 
3487         for (p = &ide_modules; (*p) && (*p) != module; p = &((*p)->next));
3488         if (*p)
3489                 *p = (*p)->next;
3490 }
3491 
3492 struct block_device_operations ide_fops[] = {{
3493         open:                   ide_open,
3494         release:                ide_release,
3495         ioctl:                  ide_ioctl,
3496         check_media_change:     ide_check_media_change,
3497         revalidate:             ide_revalidate_disk
3498 }};
3499 
3500 EXPORT_SYMBOL(ide_hwifs);
3501 EXPORT_SYMBOL(ide_register_module);
3502 EXPORT_SYMBOL(ide_unregister_module);
3503 EXPORT_SYMBOL(ide_spin_wait_hwgroup);
3504 
3505 /*
3506  * Probe module
3507  */
3508 devfs_handle_t ide_devfs_handle;
3509 
3510 EXPORT_SYMBOL(ide_probe);
3511 EXPORT_SYMBOL(drive_is_flashcard);
3512 EXPORT_SYMBOL(ide_timer_expiry);
3513 EXPORT_SYMBOL(ide_intr);
3514 EXPORT_SYMBOL(ide_fops);
3515 EXPORT_SYMBOL(ide_get_queue);
3516 EXPORT_SYMBOL(ide_add_generic_settings);
3517 EXPORT_SYMBOL(ide_devfs_handle);
3518 EXPORT_SYMBOL(do_ide_request);
3519 /*
3520  * Driver module
3521  */
3522 EXPORT_SYMBOL(ide_scan_devices);
3523 EXPORT_SYMBOL(ide_register_subdriver);
3524 EXPORT_SYMBOL(ide_unregister_subdriver);
3525 EXPORT_SYMBOL(ide_replace_subdriver);
3526 EXPORT_SYMBOL(ide_input_data);
3527 EXPORT_SYMBOL(ide_output_data);
3528 EXPORT_SYMBOL(atapi_input_bytes);
3529 EXPORT_SYMBOL(atapi_output_bytes);
3530 EXPORT_SYMBOL(ide_set_handler);
3531 EXPORT_SYMBOL(ide_dump_status);
3532 EXPORT_SYMBOL(ide_error);
3533 EXPORT_SYMBOL(ide_fixstring);
3534 EXPORT_SYMBOL(ide_wait_stat);
3535 EXPORT_SYMBOL(ide_do_reset);
3536 EXPORT_SYMBOL(ide_init_drive_cmd);
3537 EXPORT_SYMBOL(ide_do_drive_cmd);
3538 EXPORT_SYMBOL(ide_end_drive_cmd);
3539 EXPORT_SYMBOL(ide_end_request);
3540 EXPORT_SYMBOL(ide_revalidate_disk);
3541 EXPORT_SYMBOL(ide_cmd);
3542 EXPORT_SYMBOL(ide_wait_cmd);
3543 EXPORT_SYMBOL(ide_wait_cmd_task);
3544 EXPORT_SYMBOL(ide_delay_50ms);
3545 EXPORT_SYMBOL(ide_stall_queue);
3546 #ifdef CONFIG_PROC_FS
3547 EXPORT_SYMBOL(ide_add_proc_entries);
3548 EXPORT_SYMBOL(ide_remove_proc_entries);
3549 EXPORT_SYMBOL(proc_ide_read_geometry);
3550 EXPORT_SYMBOL(create_proc_ide_interfaces);
3551 #endif
3552 EXPORT_SYMBOL(ide_add_setting);
3553 EXPORT_SYMBOL(ide_remove_setting);
3554 
3555 EXPORT_SYMBOL(ide_register_hw);
3556 EXPORT_SYMBOL(ide_register);
3557 EXPORT_SYMBOL(ide_unregister);
3558 EXPORT_SYMBOL(ide_setup_ports);
3559 EXPORT_SYMBOL(hwif_unregister);
3560 EXPORT_SYMBOL(get_info_ptr);
3561 EXPORT_SYMBOL(current_capacity);
3562 
3563 EXPORT_SYMBOL(system_bus_clock);
3564 
3565 /*
3566  * This is gets invoked once during initialization, to set *everything* up
3567  */
3568 int __init ide_init (void)
3569 {
3570         static char banner_printed;
3571         int i;
3572 
3573         if (!banner_printed) {
3574                 printk(KERN_INFO "Uniform Multi-Platform E-IDE driver " REVISION "\n");
3575                 ide_devfs_handle = devfs_mk_dir (NULL, "ide", NULL);
3576                 system_bus_speed = ide_system_bus_speed();
3577                 banner_printed = 1;
3578         }
3579 
3580         init_ide_data ();
3581 
3582         initializing = 1;
3583         ide_init_builtin_drivers();
3584         initializing = 0;
3585 
3586         for (i = 0; i < MAX_HWIFS; ++i) {
3587                 ide_hwif_t  *hwif = &ide_hwifs[i];
3588                 if (hwif->present)
3589                         ide_geninit(hwif);
3590         }
3591 
3592         return 0;
3593 }
3594 
3595 #ifdef MODULE
3596 char *options = NULL;
3597 MODULE_PARM(options,"s");
3598 
3599 static void __init parse_options (char *line)
3600 {
3601         char *next = line;
3602 
3603         if (line == NULL || !*line)
3604                 return;
3605         while ((line = next) != NULL) {
3606                 if ((next = strchr(line,' ')) != NULL)
3607                         *next++ = 0;
3608                 if (!ide_setup(line))
3609                         printk ("Unknown option '%s'\n", line);
3610         }
3611 }
3612 
3613 int init_module (void)
3614 {
3615         parse_options(options);
3616         return ide_init();
3617 }
3618 
3619 void cleanup_module (void)
3620 {
3621         int index;
3622 
3623         for (index = 0; index < MAX_HWIFS; ++index) {
3624                 ide_unregister(index);
3625 #if defined(CONFIG_BLK_DEV_IDEDMA) && !defined(CONFIG_DMA_NONPCI)
3626                 if (ide_hwifs[index].dma_base)
3627                         (void) ide_release_dma(&ide_hwifs[index]);
3628 #endif /* (CONFIG_BLK_DEV_IDEDMA) && !(CONFIG_DMA_NONPCI) */
3629         }
3630 
3631 #ifdef CONFIG_PROC_FS
3632         proc_ide_destroy();
3633 #endif
3634         devfs_unregister (ide_devfs_handle);
3635 }
3636 
3637 #else /* !MODULE */
3638 
3639 __setup("", ide_setup);
3640 
3641 #endif /* MODULE */
3642 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.