root/drivers/block/ide.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. read_timer
  2. set_recovery_timer
  3. init_hwif_data
  4. init_ide_data
  5. do_vlb_sync
  6. ide_input_data
  7. ide_output_data
  8. ide_set_handler
  9. lba_capacity_is_ok
  10. current_capacity
  11. ide_geninit
  12. init_gendisk
  13. atapi_reset_pollfunc
  14. reset_pollfunc
  15. do_reset1
  16. ide_do_reset
  17. ide_end_drive_cmd
  18. ide_dump_status
  19. try_to_flush_leftover_data
  20. ide_error
  21. read_intr
  22. write_intr
  23. ide_multwrite
  24. multwrite_intr
  25. ide_cmd
  26. set_multmode_intr
  27. set_geometry_intr
  28. recal_intr
  29. drive_cmd_intr
  30. do_special
  31. ide_wait_stat
  32. do_rw_disk
  33. execute_drive_cmd
  34. do_request
  35. ide_do_request
  36. do_hwgroup_request
  37. do_ide0_request
  38. do_ide1_request
  39. do_ide2_request
  40. do_ide3_request
  41. timer_expiry
  42. unexpected_intr
  43. ide_intr
  44. get_info_ptr
  45. ide_init_drive_cmd
  46. ide_do_drive_cmd
  47. ide_open
  48. ide_release
  49. revalidate_disk
  50. write_fs_long
  51. ide_ioctl
  52. ide_check_media_change
  53. ide_fixstring
  54. do_identify
  55. delay_10ms
  56. try_to_identify
  57. do_probe
  58. probe_for_drive
  59. probe_cmos_for_drives
  60. probe_hwif
  61. stridx
  62. match_parm
  63. ide_setup
  64. ide_xlate_1024
  65. init_irq
  66. ide_probe_pci
  67. probe_for_hwifs
  68. hwif_init
  69. ide_init
  70. ide_register
  71. ide_unregister

   1 /*
   2  *  linux/drivers/block/ide.c   Version 5.39  May 3, 1996
   3  *
   4  *  Copyright (C) 1994-1996  Linus Torvalds & authors (see below)
   5  */
   6 #define _IDE_C          /* needed by <linux/blk.h> */
   7 
   8 /*
   9  * This is the multiple IDE interface driver, as evolved from hd.c.
  10  * It supports up to four IDE interfaces, on one or more IRQs (usually 14 & 15).
  11  * There can be up to two drives per interface, as per the ATA-2 spec.
  12  *
  13  * Primary i/f:    ide0: major=3;  (hda)         minor=0; (hdb)         minor=64
  14  * Secondary i/f:  ide1: major=22; (hdc or hd1a) minor=0; (hdd or hd1b) minor=64
  15  * Tertiary i/f:   ide2: major=33; (hde)         minor=0; (hdf)         minor=64
  16  * Quaternary i/f: ide3: major=34; (hdg)         minor=0; (hdh)         minor=64
  17  *
  18  * It is easy to extend ide.c to handle more than four interfaces:
  19  *
  20  *      Change the MAX_HWIFS constant in ide.h.
  21  *
  22  *      Define some new major numbers (in major.h), and insert them into
  23  *      the ide_hwif_to_major table in ide.c.
  24  *
  25  *      Fill in the extra values for the new interfaces into the two tables
  26  *      inside ide.c:  default_io_base[]  and  default_irqs[].
  27  *
  28  *      Create the new request handlers by cloning "do_ide3_request()"
  29  *      for each new interface, and add them to the switch statement
  30  *      in the ide_init() function in ide.c.
  31  *
  32  *      Recompile, create the new /dev/ entries, and it will probably work.
  33  *
  34  *  From hd.c:
  35  *  |
  36  *  | It traverses the request-list, using interrupts to jump between functions.
  37  *  | As nearly all functions can be called within interrupts, we may not sleep.
  38  *  | Special care is recommended.  Have Fun!
  39  *  |
  40  *  | modified by Drew Eckhardt to check nr of hd's from the CMOS.
  41  *  |
  42  *  | Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
  43  *  | in the early extended-partition checks and added DM partitions.
  44  *  |
  45  *  | Early work on error handling by Mika Liljeberg (liljeber@cs.Helsinki.FI).
  46  *  |
  47  *  | IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
  48  *  | and general streamlining by Mark Lord (mlord@pobox.com).
  49  *
  50  *  October, 1994 -- Complete line-by-line overhaul for linux 1.1.x, by:
  51  *
  52  *      Mark Lord       (mlord@pobox.com)               (IDE Perf.Pkg)
  53  *      Delman Lee      (delman@mipg.upenn.edu)         ("Mr. atdisk2")
  54  *      Petri Mattila   (ptjmatti@kruuna.helsinki.fi)   (EIDE stuff)
  55  *      Scott Snyder    (snyder@fnald0.fnal.gov)        (ATAPI IDE cd-rom)
  56  *
  57  *  Maintained by Mark Lord (mlord@pobox.com):  ide.c, ide.h, triton.c, hd.c, ..
  58  *
  59  *  This was a rewrite of just about everything from hd.c, though some original
  60  *  code is still sprinkled about.  Think of it as a major evolution, with
  61  *  inspiration from lots of linux users, esp.  hamish@zot.apana.org.au
  62  *
  63  *  Version 1.0 ALPHA   initial code, primary i/f working okay
  64  *  Version 1.3 BETA    dual i/f on shared irq tested & working!
  65  *  Version 1.4 BETA    added auto probing for irq(s)
  66  *  Version 1.5 BETA    added ALPHA (untested) support for IDE cd-roms,
  67  *  ...
  68  *  Version 3.5         correct the bios_cyl field if it's too small
  69  *  (linux 1.1.76)       (to help fdisk with brain-dead BIOSs)
  70  *  Version 3.6         cosmetic corrections to comments and stuff
  71  *  (linux 1.1.77)      reorganise probing code to make it understandable
  72  *                      added halfway retry to probing for drive identification
  73  *                      added "hdx=noprobe" command line option
  74  *                      allow setting multmode even when identification fails
  75  *  Version 3.7         move set_geometry=1 from do_identify() to ide_init()
  76  *                      increase DRQ_WAIT to eliminate nuisance messages
  77  *                      wait for DRQ_STAT instead of DATA_READY during probing
  78  *                        (courtesy of Gary Thomas gary@efland.UU.NET)
  79  *  Version 3.8         fixed byte-swapping for confused Mitsumi cdrom drives
  80  *                      update of ide-cd.c from Scott, allows blocksize=1024
  81  *                      cdrom probe fixes, inspired by jprang@uni-duisburg.de
  82  *  Version 3.9         don't use LBA if lba_capacity looks funny
  83  *                      correct the drive capacity calculations
  84  *                      fix probing for old Seagates without IDE_ALTSTATUS_REG
  85  *                      fix byte-ordering for some NEC cdrom drives
  86  *  Version 3.10        disable multiple mode by default; was causing trouble
  87  *  Version 3.11        fix mis-identification of old WD disks as cdroms
  88  *  Version 3,12        simplify logic for selecting initial mult_count
  89  *                        (fixes problems with buggy WD drives)
  90  *  Version 3.13        remove excess "multiple mode disabled" messages
  91  *  Version 3.14        fix ide_error() handling of BUSY_STAT
  92  *                      fix byte-swapped cdrom strings (again.. arghh!)
  93  *                      ignore INDEX bit when checking the ALTSTATUS reg
  94  *  Version 3.15        add SINGLE_THREADED flag for use with dual-CMD i/f
  95  *                      ignore WRERR_STAT for non-write operations
  96  *                      added vlb_sync support for DC-2000A & others,
  97  *                       (incl. some Promise chips), courtesy of Frank Gockel
  98  *  Version 3.16        convert vlb_32bit and vlb_sync into runtime flags
  99  *                      add ioctls to get/set VLB flags (HDIO_[SG]ET_CHIPSET)
 100  *                      rename SINGLE_THREADED to SUPPORT_SERIALIZE,
 101  *                      add boot flag to "serialize" operation for CMD i/f
 102  *                      add optional support for DTC2278 interfaces,
 103  *                       courtesy of andy@cercle.cts.com (Dyan Wile).
 104  *                      add boot flag to enable "dtc2278" probe
 105  *                      add probe to avoid EATA (SCSI) interfaces,
 106  *                       courtesy of neuffer@goofy.zdv.uni-mainz.de.
 107  *  Version 4.00        tidy up verify_area() calls - heiko@colossus.escape.de
 108  *                      add flag to ignore WRERR_STAT for some drives
 109  *                       courtesy of David.H.West@um.cc.umich.edu
 110  *                      assembly syntax tweak to vlb_sync
 111  *                      removable drive support from scuba@cs.tu-berlin.de
 112  *                      add transparent support for DiskManager-6.0x "Dynamic
 113  *                       Disk Overlay" (DDO), most of this is in genhd.c
 114  *                      eliminate "multiple mode turned off" message at boot
 115  *  Version 4.10        fix bug in ioctl for "hdparm -c3"
 116  *                      fix DM6:DDO support -- now works with LILO, fdisk, ...
 117  *                      don't treat some naughty WD drives as removable
 118  *  Version 4.11        updated DM6 support using info provided by OnTrack
 119  *  Version 5.00        major overhaul, multmode setting fixed, vlb_sync fixed
 120  *                      added support for 3rd/4th/alternative IDE ports
 121  *                      created ide.h; ide-cd.c now compiles separate from ide.c
 122  *                      hopefully fixed infinite "unexpected_intr" from cdroms
 123  *                      zillions of other changes and restructuring
 124  *                      somehow reduced overall memory usage by several kB
 125  *                      probably slowed things down slightly, but worth it
 126  *  Version 5.01        AT LAST!!  Finally understood why "unexpected_intr"
 127  *                       was happening at various times/places:  whenever the
 128  *                       ide-interface's ctl_port was used to "mask" the irq,
 129  *                       it also would trigger an edge in the process of masking
 130  *                       which would result in a self-inflicted interrupt!!
 131  *                       (such a stupid way to build a hardware interrupt mask).
 132  *                       This is now fixed (after a year of head-scratching).
 133  *  Version 5.02        got rid of need for {enable,disable}_irq_list()
 134  *  Version 5.03        tune-ups, comments, remove "busy wait" from drive resets
 135  *                      removed PROBE_FOR_IRQS option -- no longer needed
 136  *                      OOOPS!  fixed "bad access" bug for 2nd drive on an i/f
 137  *  Version 5.04        changed "ira %d" to "irq %d" in DEBUG message
 138  *                      added more comments, cleaned up unexpected_intr()
 139  *                      OOOPS!  fixed null pointer problem in ide reset code
 140  *                      added autodetect for Triton chipset -- no effect yet
 141  *  Version 5.05        OOOPS!  fixed bug in revalidate_disk()
 142  *                      OOOPS!  fixed bug in ide_do_request()
 143  *                      added ATAPI reset sequence for cdroms
 144  *  Version 5.10        added Bus-Mastered DMA support for Triton Chipset
 145  *                      some (mostly) cosmetic changes
 146  *  Version 5.11        added ht6560b support by malafoss@snakemail.hut.fi
 147  *                      reworked PCI scanning code
 148  *                      added automatic RZ1000 detection/support
 149  *                      added automatic PCI CMD640 detection/support
 150  *                      added option for VLB CMD640 support
 151  *                      tweaked probe to find cdrom on hdb with disks on hda,hdc
 152  *  Version 5.12        some performance tuning
 153  *                      added message to alert user to bad /dev/hd[cd] entries
 154  *                      OOOPS!  fixed bug in atapi reset
 155  *                      driver now forces "serialize" again for all cmd640 chips
 156  *                      noticed REALLY_SLOW_IO had no effect, moved it to ide.c
 157  *                      made do_drive_cmd() into public ide_do_drive_cmd()
 158  *  Version 5.13        fixed typo ('B'), thanks to houston@boyd.geog.mcgill.ca
 159  *                      fixed ht6560b support
 160  *  Version 5.13b (sss) fix problem in calling ide_cdrom_setup()
 161  *                      don't bother invalidating nonexistent partitions
 162  *  Version 5.14        fixes to cmd640 support.. maybe it works now(?)
 163  *                      added & tested full EZ-DRIVE support -- don't use LILO!
 164  *                      don't enable 2nd CMD640 PCI port during init - conflict
 165  *  Version 5.15        bug fix in init_cmd640_vlb()
 166  *                      bug fix in interrupt sharing code
 167  *  Version 5.16        ugh.. fix "serialize" support, broken in 5.15
 168  *                      remove "Huh?" from cmd640 code
 169  *                      added qd6580 interface speed select from Colten Edwards
 170  *  Version 5.17        kludge around bug in BIOS32 on Intel triton motherboards
 171  *  Version 5.18        new CMD640 code, moved to cmd640.c, #include'd for now
 172  *                      new UMC8672 code, moved to umc8672.c, #include'd for now
 173  *                      disallow turning on DMA when h/w not capable of DMA
 174  *  Version 5.19        fix potential infinite timeout on resets
 175  *                      extend reset poll into a general purpose polling scheme
 176  *                      add atapi tape drive support from Gadi Oxman
 177  *                      simplify exit from _intr routines -- no IDE_DO_REQUEST
 178  *  Version 5.20        leave current rq on blkdev request list during I/O
 179  *                      generalized ide_do_drive_cmd() for tape/cdrom driver use
 180  *  Version 5.21        fix nasty cdrom/tape bug (ide_preempt was messed up)
 181  *  Version 5.22        fix ide_xlate_1024() to work with/without drive->id
 182  *  Version 5.23        miscellaneous touch-ups
 183  *  Version 5.24        fix #if's for SUPPORT_CMD640
 184  *  Version 5.25        more touch-ups, fix cdrom resets, ...
 185  *                      cmd640.c now configs/compiles separate from ide.c
 186  *  Version 5.26        keep_settings now maintains the using_dma flag
 187  *                      fix [EZD] remap message to only output at boot time
 188  *                      fix "bad /dev/ entry" message to say hdc, not hdc0
 189  *                      fix ide_xlate_1024() to respect user specified CHS
 190  *                      use CHS from partn table if it looks translated
 191  *                      re-merged flags chipset,vlb_32bit,vlb_sync into io_32bit
 192  *                      keep track of interface chipset type, when known
 193  *                      add generic PIO mode "tuneproc" mechanism
 194  *                      fix cmd640_vlb option
 195  *                      fix ht6560b support (was completely broken)
 196  *                      umc8672.c now configures/compiles separate from ide.c
 197  *                      move dtc2278 support to dtc2278.c
 198  *                      move ht6560b support to ht6560b.c
 199  *                      move qd6580  support to qd6580.c
 200  *                      add  ali14xx support in ali14xx.c
 201  * Version 5.27         add [no]autotune parameters to help cmd640
 202  *                      move rz1000  support to rz1000.c
 203  * Version 5.28         #include "ide_modes.h"
 204  *                      fix disallow_unmask: now per-interface "no_unmask" bit
 205  *                      force io_32bit to be the same on drive pairs of dtc2278
 206  *                      improved IDE tape error handling, and tape DMA support
 207  *                      bugfix in ide_do_drive_cmd() for cdroms + serialize
 208  * Version 5.29         fixed non-IDE check for too many physical heads
 209  *                      don't use LBA if capacity is smaller than CHS
 210  * Version 5.30         remove real_devices kludge, formerly used by genhd.c
 211  * Version 5.32         change "KB" to "kB"
 212  *                      fix serialize (was broken in kernel 1.3.72)
 213  *                      add support for "hdparm -I"
 214  *                      use common code for disk/tape/cdrom IDE_DRIVE_CMDs
 215  *                      add support for Promise DC4030VL caching card
 216  *                      improved serialize support
 217  *                      put partition check back into alphabetical order
 218  *                      add config option for PCMCIA baggage
 219  *                      try to make PCMCIA support safer to use
 220  *                      improve security on ioctls(): all are suser() only
 221  * Version 5.33         improve handling of HDIO_DRIVE_CMDs that read data
 222  * Version 5.34         fix irq-sharing problem from 5.33
 223  *                      fix cdrom ioctl problem from 5.33
 224  * Version 5.35         cosmetic changes
 225  *                      fix cli() problem in try_to_identify()
 226  * Version 5.36         fixes to optional PCMCIA support
 227  * Version 5.37         don't use DMA when "noautotune" is specified
 228  * Version 5.37a (go)   fix shared irq probing (was broken in kernel 1.3.72)
 229  *                      call unplug_device() from ide_do_drive_cmd()
 230  * Version 5.38         add "hdx=none" option, courtesy of Joel Maslak
 231  *                      mask drive irq after use, if sharing with another hwif
 232  *                      add code to help debug weird cmd640 problems
 233  * Version 5.39         fix horrible error in earlier irq sharing "fix"
 234  *
 235  *  Some additional driver compile-time options are in ide.h
 236  *
 237  *  To do, in likely order of completion:
 238  *      - modify kernel to obtain BIOS geometry for drives on 2nd/3rd/4th i/f
 239 */
 240 
 241 #undef REALLY_SLOW_IO           /* most systems can safely undef this */
 242 
 243 #include <linux/config.h>
 244 #include <linux/types.h>
 245 #include <linux/string.h>
 246 #include <linux/kernel.h>
 247 #include <linux/delay.h>
 248 #include <linux/timer.h>
 249 #include <linux/mm.h>
 250 #include <linux/ioport.h>
 251 #include <linux/interrupt.h>
 252 #include <linux/major.h>
 253 #include <linux/blkdev.h>
 254 #include <linux/errno.h>
 255 #include <linux/hdreg.h>
 256 #include <linux/genhd.h>
 257 #include <linux/malloc.h>
 258 
 259 #include <asm/byteorder.h>
 260 #include <asm/irq.h>
 261 #include <asm/segment.h>
 262 #include <asm/io.h>
 263 
 264 #ifdef CONFIG_PCI
 265 #include <linux/bios32.h>
 266 #include <linux/pci.h>
 267 #endif /* CONFIG_PCI */
 268 
 269 #include "ide.h"
 270 #include "ide_modes.h"
 271 
 272 #ifdef CONFIG_BLK_DEV_PROMISE
 273 #include "promise.h"
 274 #define IS_PROMISE_DRIVE (HWIF(drive)->chipset == ide_promise)
 275 #else
 276 #define IS_PROMISE_DRIVE (0)    /* auto-NULLs out Promise code */
 277 #endif /* CONFIG_BLK_DEV_PROMISE */
 278 
 279 static const byte       ide_hwif_to_major[MAX_HWIFS] = {IDE0_MAJOR, IDE1_MAJOR, IDE2_MAJOR, IDE3_MAJOR};
 280 
 281 static const unsigned short default_io_base[MAX_HWIFS] = {0x1f0, 0x170, 0x1e8, 0x168};
 282 static const byte       default_irqs[MAX_HWIFS]     = {14, 15, 11, 10};
 283 
 284 #if (DISK_RECOVERY_TIME > 0)
 285 /*
 286  * For really screwy hardware (hey, at least it *can* be used with Linux)
 287  * we can enforce a minimum delay time between successive operations.
 288  */
 289 static unsigned long read_timer(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 290 {
 291         unsigned long t, flags;
 292         int i;
 293 
 294         save_flags(flags);
 295         cli();
 296         t = jiffies * 11932;
 297         outb_p(0, 0x43);
 298         i = inb_p(0x40);
 299         i |= inb(0x40) << 8;
 300         restore_flags(flags);
 301         return (t - i);
 302 }
 303 
 304 static void set_recovery_timer (ide_hwif_t *hwif)
     /* [previous][next][first][last][top][bottom][index][help] */
 305 {
 306         hwif->last_time = read_timer();
 307 }
 308 #define SET_RECOVERY_TIMER(drive) set_recovery_timer (drive)
 309 
 310 #else
 311 
 312 #define SET_RECOVERY_TIMER(drive)
 313 
 314 #endif /* DISK_RECOVERY_TIME */
 315 
 316 
 317 /*
 318  * Do not even *think* about calling this!
 319  */
 320 static void init_hwif_data (unsigned int index)
     /* [previous][next][first][last][top][bottom][index][help] */
 321 {
 322         byte *p;
 323         unsigned int unit;
 324         ide_hwif_t *hwif = &ide_hwifs[index];
 325 
 326         /* bulk initialize hwif & drive info with zeros */
 327         p = ((byte *) hwif) + sizeof(ide_hwif_t);
 328         do {
 329                 *--p = 0;
 330         } while (p > (byte *) hwif);
 331 
 332         /* fill in any non-zero initial values */
 333         hwif->index     = index;
 334         hwif->noprobe   = (index > 1);
 335         hwif->io_base   = default_io_base[index];
 336         hwif->ctl_port  = hwif->io_base ? hwif->io_base+0x206 : 0x000;
 337 #ifdef CONFIG_BLK_DEV_HD
 338         if (hwif->io_base == HD_DATA)
 339                 hwif->noprobe = 1; /* may be overridden by ide_setup() */
 340 #endif /* CONFIG_BLK_DEV_HD */
 341         hwif->major     = ide_hwif_to_major[index];
 342         hwif->name[0]   = 'i';
 343         hwif->name[1]   = 'd';
 344         hwif->name[2]   = 'e';
 345         hwif->name[3]   = '0' + index;
 346 #ifdef CONFIG_BLK_DEV_IDETAPE
 347         hwif->tape_drive = NULL;
 348 #endif /* CONFIG_BLK_DEV_IDETAPE */
 349         for (unit = 0; unit < MAX_DRIVES; ++unit) {
 350                 ide_drive_t *drive = &hwif->drives[unit];
 351 
 352                 drive->select.all               = (unit<<4)|0xa0;
 353                 drive->hwif                     = hwif;
 354                 drive->ctl                      = 0x08;
 355                 drive->ready_stat               = READY_STAT;
 356                 drive->bad_wstat                = BAD_W_STAT;
 357                 drive->special.b.recalibrate    = 1;
 358                 drive->special.b.set_geometry   = 1;
 359                 drive->name[0]                  = 'h';
 360                 drive->name[1]                  = 'd';
 361                 drive->name[2]                  = 'a' + (index * MAX_DRIVES) + unit;
 362         }
 363 }
 364 
 365 /*
 366  * init_ide_data() sets reasonable default values into all fields
 367  * of all instances of the hwifs and drives, but only on the first call.
 368  * Subsequent calls have no effect (they don't wipe out anything).
 369  *
 370  * This routine is normally called at driver initialization time,
 371  * but may also be called MUCH earlier during kernel "command-line"
 372  * parameter processing.  As such, we cannot depend on any other parts
 373  * of the kernel (such as memory allocation) to be functioning yet.
 374  *
 375  * This is too bad, as otherwise we could dynamically allocate the
 376  * ide_drive_t structs as needed, rather than always consuming memory
 377  * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
 378  */
 379 #define MAGIC_COOKIE 0x12345678
 380 static void init_ide_data (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 381 {
 382         unsigned int index;
 383         static unsigned long magic_cookie = MAGIC_COOKIE;
 384 
 385         if (magic_cookie != MAGIC_COOKIE)
 386                 return;         /* already initialized */
 387         magic_cookie = 0;
 388 
 389         for (index = 0; index < MAX_HWIFS; ++index)
 390                 init_hwif_data(index);
 391 }
 392 
 393 #if SUPPORT_VLB_SYNC
 394 /*
 395  * Some localbus EIDE interfaces require a special access sequence
 396  * when using 32-bit I/O instructions to transfer data.  We call this
 397  * the "vlb_sync" sequence, which consists of three successive reads
 398  * of the sector count register location, with interrupts disabled
 399  * to ensure that the reads all happen together.
 400  */
 401 static inline void do_vlb_sync (unsigned short port) {
     /* [previous][next][first][last][top][bottom][index][help] */
 402         (void) inb (port);
 403         (void) inb (port);
 404         (void) inb (port);
 405 }
 406 #endif /* SUPPORT_VLB_SYNC */
 407 
 408 /*
 409  * This is used for most PIO data transfers *from* the IDE interface
 410  */
 411 void ide_input_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
     /* [previous][next][first][last][top][bottom][index][help] */
 412 {
 413         unsigned short io_base  = HWIF(drive)->io_base;
 414         unsigned short data_reg = io_base+IDE_DATA_OFFSET;
 415         byte io_32bit = drive->io_32bit;
 416 
 417         if (io_32bit) {
 418 #if SUPPORT_VLB_SYNC
 419                 if (io_32bit & 2) {
 420                         cli();
 421                         do_vlb_sync(io_base+IDE_NSECTOR_OFFSET);
 422                         insl(data_reg, buffer, wcount);
 423                         if (drive->unmask)
 424                                 sti();
 425                 } else
 426 #endif /* SUPPORT_VLB_SYNC */
 427                         insl(data_reg, buffer, wcount);
 428         } else
 429                 insw(data_reg, buffer, wcount<<1);
 430 }
 431 
 432 /*
 433  * This is used for most PIO data transfers *to* the IDE interface
 434  */
 435 void ide_output_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
     /* [previous][next][first][last][top][bottom][index][help] */
 436 {
 437         unsigned short io_base  = HWIF(drive)->io_base;
 438         unsigned short data_reg = io_base+IDE_DATA_OFFSET;
 439         byte io_32bit = drive->io_32bit;
 440 
 441         if (io_32bit) {
 442 #if SUPPORT_VLB_SYNC
 443                 if (io_32bit & 2) {
 444                         cli();
 445                         do_vlb_sync(io_base+IDE_NSECTOR_OFFSET);
 446                         outsl(data_reg, buffer, wcount);
 447                         if (drive->unmask)
 448                                 sti();
 449                 } else
 450 #endif /* SUPPORT_VLB_SYNC */
 451                         outsl(data_reg, buffer, wcount);
 452         } else
 453                 outsw(data_reg, buffer, wcount<<1);
 454 }
 455 
 456 /*
 457  * This should get invoked any time we exit the driver to
 458  * wait for an interrupt response from a drive.  handler() points
 459  * at the appropriate code to handle the next interrupt, and a
 460  * timer is started to prevent us from waiting forever in case
 461  * something goes wrong (see the timer_expiry() handler later on).
 462  */
 463 void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler, unsigned int timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
 464 {
 465         ide_hwgroup_t *hwgroup = HWGROUP(drive);
 466 #ifdef DEBUG
 467         if (hwgroup->handler != NULL) {
 468                 printk("%s: ide_set_handler: handler not null; old=%p, new=%p\n",
 469                         drive->name, hwgroup->handler, handler);
 470         }
 471 #endif
 472         hwgroup->handler       = handler;
 473         hwgroup->timer.expires = jiffies + timeout;
 474         add_timer(&(hwgroup->timer));
 475 }
 476 
 477 /*
 478  * lba_capacity_is_ok() performs a sanity check on the claimed "lba_capacity"
 479  * value for this drive (from its reported identification information).
 480  *
 481  * Returns:     1 if lba_capacity looks sensible
 482  *              0 otherwise
 483  */
 484 static int lba_capacity_is_ok (struct hd_driveid *id)
     /* [previous][next][first][last][top][bottom][index][help] */
 485 {
 486         unsigned long lba_sects   = id->lba_capacity;
 487         unsigned long chs_sects   = id->cyls * id->heads * id->sectors;
 488         unsigned long _10_percent = chs_sects / 10;
 489 
 490         /* perform a rough sanity check on lba_sects:  within 10% is "okay" */
 491         if ((lba_sects - chs_sects) < _10_percent)
 492                 return 1;       /* lba_capacity is good */
 493 
 494         /* some drives have the word order reversed */
 495         lba_sects = (lba_sects << 16) | (lba_sects >> 16);
 496         if ((lba_sects - chs_sects) < _10_percent) {
 497                 id->lba_capacity = lba_sects;   /* fix it */
 498                 return 1;       /* lba_capacity is (now) good */
 499         }
 500         return 0;       /* lba_capacity value is bad */
 501 }
 502 
 503 /*
 504  * current_capacity() returns the capacity (in sectors) of a drive
 505  * according to its current geometry/LBA settings.
 506  */
 507 static unsigned long current_capacity (ide_drive_t  *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 508 {
 509         struct hd_driveid *id = drive->id;
 510         unsigned long capacity = drive->cyl * drive->head * drive->sect;
 511 
 512         if (!drive->present)
 513                 return 0;
 514         if (drive->media != ide_disk)
 515                 return 0x7fffffff;      /* cdrom or tape */
 516         drive->select.b.lba = 0;
 517         /* Determine capacity, and use LBA if the drive properly supports it */
 518         if (id != NULL && (id->capability & 2) && lba_capacity_is_ok(id)) {
 519                 if (id->lba_capacity >= capacity) {
 520                         capacity = id->lba_capacity;
 521                         drive->select.b.lba = 1;
 522                 }
 523         }
 524         return (capacity - drive->sect0);
 525 }
 526 
 527 /*
 528  * ide_geninit() is called exactly *once* for each major, from genhd.c,
 529  * at the beginning of the initial partition check for the drives.
 530  */
 531 static void ide_geninit (struct gendisk *gd)
     /* [previous][next][first][last][top][bottom][index][help] */
 532 {
 533         unsigned int unit;
 534         ide_hwif_t *hwif = gd->real_devices;
 535 
 536         for (unit = 0; unit < gd->nr_real; ++unit) {
 537                 ide_drive_t *drive = &hwif->drives[unit];
 538 #ifdef CONFIG_BLK_DEV_IDECD
 539                 if (drive->present && drive->media == ide_cdrom)
 540                         ide_cdrom_setup(drive);
 541 #endif /* CONFIG_BLK_DEV_IDECD */
 542 #ifdef CONFIG_BLK_DEV_IDETAPE
 543                 if (drive->present && drive->media == ide_tape)
 544                         idetape_setup(drive);
 545 #endif /* CONFIG_BLK_DEV_IDETAPE */
 546                 drive->part[0].nr_sects = current_capacity(drive);
 547                 if (!drive->present || drive->media != ide_disk) {
 548                         drive->part[0].start_sect = -1; /* skip partition check */
 549                 }
 550         }
 551 }
 552 
 553 /*
 554  * init_gendisk() (as opposed to ide_geninit) is called for each major device,
 555  * after probing for drives, to allocate partition tables and other data
 556  * structures needed for the routines in genhd.c.  ide_geninit() gets called
 557  * somewhat later, during the partition check.
 558  */
 559 static void init_gendisk (ide_hwif_t *hwif)
     /* [previous][next][first][last][top][bottom][index][help] */
 560 {
 561         struct gendisk *gd, **gdp;
 562         unsigned int unit, units, minors;
 563         int *bs;
 564 
 565         /* figure out maximum drive number on the interface */
 566         for (units = MAX_DRIVES; units > 0; --units) {
 567                 if (hwif->drives[units-1].present)
 568                         break;
 569         }
 570         minors    = units * (1<<PARTN_BITS);
 571         gd        = kmalloc (sizeof(struct gendisk), GFP_KERNEL);
 572         gd->sizes = kmalloc (minors * sizeof(int), GFP_KERNEL);
 573         gd->part  = kmalloc (minors * sizeof(struct hd_struct), GFP_KERNEL);
 574         bs        = kmalloc (minors*sizeof(int), GFP_KERNEL);
 575 
 576         /* cdroms and msdos f/s are examples of non-1024 blocksizes */
 577         blksize_size[hwif->major] = bs;
 578         for (unit = 0; unit < minors; ++unit)
 579                 *bs++ = BLOCK_SIZE;
 580 
 581         for (unit = 0; unit < units; ++unit)
 582                 hwif->drives[unit].part = &gd->part[unit << PARTN_BITS];
 583 
 584         gd->major       = hwif->major;          /* our major device number */
 585         gd->major_name  = IDE_MAJOR_NAME;       /* treated special in genhd.c */
 586         gd->minor_shift = PARTN_BITS;           /* num bits for partitions */
 587         gd->max_p       = 1<<PARTN_BITS;        /* 1 + max partitions / drive */
 588         gd->max_nr      = units;                /* max num real drives */
 589         gd->nr_real     = units;                /* current num real drives */
 590         gd->init        = ide_geninit;          /* initialization function */
 591         gd->real_devices= hwif;                 /* ptr to internal data */
 592         gd->next        = NULL;                 /* linked list of major devs */
 593 
 594         for (gdp = &gendisk_head; *gdp; gdp = &((*gdp)->next)) ;
 595         hwif->gd = *gdp = gd;                   /* link onto tail of list */
 596 }
 597 
 598 static void do_reset1 (ide_drive_t *, int);             /* needed below */
 599 
 600 #ifdef CONFIG_BLK_DEV_IDEATAPI
 601 /*
 602  * atapi_reset_pollfunc() gets invoked to poll the interface for completion every 50ms
 603  * during an atapi drive reset operation. If the drive has not yet responded,
 604  * and we have not yet hit our maximum waiting time, then the timer is restarted
 605  * for another 50ms.
 606  */
 607 static void atapi_reset_pollfunc (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 608 {
 609         ide_hwgroup_t *hwgroup = HWGROUP(drive);
 610         byte stat;
 611 
 612         OUT_BYTE (drive->select.all, IDE_SELECT_REG);
 613         udelay (10);
 614 
 615         if (OK_STAT(stat=GET_STAT(), 0, BUSY_STAT)) {
 616                 printk("%s: ATAPI reset complete\n", drive->name);
 617         } else {
 618                 if (jiffies < hwgroup->poll_timeout) {
 619                         ide_set_handler (drive, &atapi_reset_pollfunc, HZ/20);
 620                         return; /* continue polling */
 621                 }
 622                 hwgroup->poll_timeout = 0;      /* end of polling */
 623                 printk("%s: ATAPI reset timed-out, status=0x%02x\n", drive->name, stat);
 624                 do_reset1 (drive, 1);   /* do it the old fashioned way */
 625         }
 626         hwgroup->poll_timeout = 0;      /* done polling */
 627 }
 628 #endif /* CONFIG_BLK_DEV_IDEATAPI */
 629 
 630 /*
 631  * reset_pollfunc() gets invoked to poll the interface for completion every 50ms
 632  * during an ide reset operation. If the drives have not yet responded,
 633  * and we have not yet hit our maximum waiting time, then the timer is restarted
 634  * for another 50ms.
 635  */
 636 static void reset_pollfunc (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 637 {
 638         ide_hwgroup_t *hwgroup = HWGROUP(drive);
 639         ide_hwif_t *hwif = HWIF(drive);
 640         byte tmp;
 641 
 642         if (!OK_STAT(tmp=GET_STAT(), 0, BUSY_STAT)) {
 643                 if (jiffies < hwgroup->poll_timeout) {
 644                         ide_set_handler (drive, &reset_pollfunc, HZ/20);
 645                         return; /* continue polling */
 646                 }
 647                 printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
 648         } else  {
 649                 printk("%s: reset: ", hwif->name);
 650                 if ((tmp = GET_ERR()) == 1)
 651                         printk("success\n");
 652                 else {
 653 #if FANCY_STATUS_DUMPS
 654                         printk("master: ");
 655                         switch (tmp & 0x7f) {
 656                                 case 1: printk("passed");
 657                                         break;
 658                                 case 2: printk("formatter device error");
 659                                         break;
 660                                 case 3: printk("sector buffer error");
 661                                         break;
 662                                 case 4: printk("ECC circuitry error");
 663                                         break;
 664                                 case 5: printk("controlling MPU error");
 665                                         break;
 666                                 default:printk("error (0x%02x?)", tmp);
 667                         }
 668                         if (tmp & 0x80)
 669                                 printk("; slave: failed");
 670                         printk("\n");
 671 #else
 672                         printk("failed\n");
 673 #endif /* FANCY_STATUS_DUMPS */
 674                 }
 675         }
 676         hwgroup->poll_timeout = 0;      /* done polling */
 677 }
 678 
 679 /*
 680  * do_reset1() attempts to recover a confused drive by resetting it.
 681  * Unfortunately, resetting a disk drive actually resets all devices on
 682  * the same interface, so it can really be thought of as resetting the
 683  * interface rather than resetting the drive.
 684  *
 685  * ATAPI devices have their own reset mechanism which allows them to be
 686  * individually reset without clobbering other devices on the same interface.
 687  *
 688  * Unfortunately, the IDE interface does not generate an interrupt to let
 689  * us know when the reset operation has finished, so we must poll for this.
 690  * Equally poor, though, is the fact that this may a very long time to complete,
 691  * (up to 30 seconds worstcase).  So, instead of busy-waiting here for it,
 692  * we set a timer to poll at 50ms intervals.
 693  */
 694 static void do_reset1 (ide_drive_t *drive, int  do_not_try_atapi)
     /* [previous][next][first][last][top][bottom][index][help] */
 695 {
 696         unsigned int unit;
 697         unsigned long flags;
 698         ide_hwif_t *hwif = HWIF(drive);
 699         ide_hwgroup_t *hwgroup = HWGROUP(drive);
 700 
 701         save_flags(flags);
 702         cli();          /* Why ? */
 703 
 704 #ifdef CONFIG_BLK_DEV_IDEATAPI
 705         /* For an ATAPI device, first try an ATAPI SRST. */
 706         if (drive->media != ide_disk) {
 707                 if (!do_not_try_atapi) {
 708                         if (!drive->keep_settings)
 709                                 drive->unmask = 0;
 710                         OUT_BYTE (drive->select.all, IDE_SELECT_REG);
 711                         udelay (20);
 712                         OUT_BYTE (WIN_SRST, IDE_COMMAND_REG);
 713                         hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
 714                         ide_set_handler (drive, &atapi_reset_pollfunc, HZ/20);
 715                         restore_flags (flags);
 716                         return;
 717                 }
 718         }
 719 #endif /* CONFIG_BLK_DEV_IDEATAPI */
 720 
 721         /*
 722          * First, reset any device state data we were maintaining
 723          * for any of the drives on this interface.
 724          */
 725         for (unit = 0; unit < MAX_DRIVES; ++unit) {
 726                 ide_drive_t *rdrive = &hwif->drives[unit];
 727                 rdrive->special.all = 0;
 728                 rdrive->special.b.set_geometry = 1;
 729                 rdrive->special.b.recalibrate  = 1;
 730                 if (OK_TO_RESET_CONTROLLER)
 731                         rdrive->mult_count = 0;
 732                 if (!rdrive->keep_settings) {
 733                         rdrive->mult_req = 0;
 734                         rdrive->unmask = 0;
 735                         if (rdrive->using_dma) {
 736                                 rdrive->using_dma = 0;
 737                                 printk("%s: disabled DMA\n", rdrive->name);
 738                         }
 739                 }
 740                 if (rdrive->mult_req != rdrive->mult_count)
 741                         rdrive->special.b.set_multmode = 1;
 742         }
 743 
 744 #if OK_TO_RESET_CONTROLLER
 745         /*
 746          * Note that we also set nIEN while resetting the device,
 747          * to mask unwanted interrupts from the interface during the reset.
 748          * However, due to the design of PC hardware, this will cause an
 749          * immediate interrupt due to the edge transition it produces.
 750          * This single interrupt gives us a "fast poll" for drives that
 751          * recover from reset very quickly, saving us the first 50ms wait time.
 752          */
 753         OUT_BYTE(drive->ctl|6,IDE_CONTROL_REG); /* set SRST and nIEN */
 754         udelay(5);                      /* more than enough time */
 755         OUT_BYTE(drive->ctl|2,IDE_CONTROL_REG); /* clear SRST, leave nIEN */
 756         udelay(5);                      /* more than enough time */
 757         hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
 758         ide_set_handler (drive, &reset_pollfunc, HZ/20);
 759 #endif  /* OK_TO_RESET_CONTROLLER */
 760 
 761         restore_flags (flags);
 762 }
 763 
 764 /*
 765  * ide_do_reset() is the entry point to the drive/interface reset code.
 766  */
 767 void ide_do_reset (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 768 {
 769         do_reset1 (drive, 0);
 770 #ifdef CONFIG_BLK_DEV_IDETAPE
 771         if (drive->media == ide_tape)
 772                 drive->tape.reset_issued=1;
 773 #endif /* CONFIG_BLK_DEV_IDETAPE */
 774 }
 775 
 776 /*
 777  * Clean up after success/failure of an explicit drive cmd
 778  */
 779 void ide_end_drive_cmd (ide_drive_t *drive, byte stat, byte err)
     /* [previous][next][first][last][top][bottom][index][help] */
 780 {
 781         unsigned long flags;
 782         struct request *rq = HWGROUP(drive)->rq;
 783 
 784         if (rq->cmd == IDE_DRIVE_CMD) {
 785                 byte *args = (byte *) rq->buffer;
 786                 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
 787                 if (args) {
 788                         args[0] = stat;
 789                         args[1] = err;
 790                         args[2] = IN_BYTE(IDE_NSECTOR_REG);
 791                 }
 792         }
 793         save_flags(flags);
 794         cli();
 795         blk_dev[MAJOR(rq->rq_dev)].current_request = rq->next;
 796         HWGROUP(drive)->rq = NULL;
 797         rq->rq_status = RQ_INACTIVE;
 798         if (rq->sem != NULL)
 799                 up(rq->sem);
 800         restore_flags(flags);
 801 }
 802 
 803 /*
 804  * Error reporting, in human readable form (luxurious, but a memory hog).
 805  */
 806 byte ide_dump_status (ide_drive_t *drive, const char *msg, byte stat)
     /* [previous][next][first][last][top][bottom][index][help] */
 807 {
 808         unsigned long flags;
 809         byte err = 0;
 810 
 811         save_flags (flags);
 812         sti();
 813         printk("%s: %s: status=0x%02x", drive->name, msg, stat);
 814 #if FANCY_STATUS_DUMPS
 815         if (drive->media == ide_disk) {
 816                 printk(" { ");
 817                 if (stat & BUSY_STAT)
 818                         printk("Busy ");
 819                 else {
 820                         if (stat & READY_STAT)  printk("DriveReady ");
 821                         if (stat & WRERR_STAT)  printk("DeviceFault ");
 822                         if (stat & SEEK_STAT)   printk("SeekComplete ");
 823                         if (stat & DRQ_STAT)    printk("DataRequest ");
 824                         if (stat & ECC_STAT)    printk("CorrectedError ");
 825                         if (stat & INDEX_STAT)  printk("Index ");
 826                         if (stat & ERR_STAT)    printk("Error ");
 827                 }
 828                 printk("}");
 829         }
 830 #endif  /* FANCY_STATUS_DUMPS */
 831         printk("\n");
 832         if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
 833                 err = GET_ERR();
 834                 printk("%s: %s: error=0x%02x", drive->name, msg, err);
 835 #if FANCY_STATUS_DUMPS
 836                 if (drive->media == ide_disk) {
 837                         printk(" { ");
 838                         if (err & BBD_ERR)      printk("BadSector ");
 839                         if (err & ECC_ERR)      printk("UncorrectableError ");
 840                         if (err & ID_ERR)       printk("SectorIdNotFound ");
 841                         if (err & ABRT_ERR)     printk("DriveStatusError ");
 842                         if (err & TRK0_ERR)     printk("TrackZeroNotFound ");
 843                         if (err & MARK_ERR)     printk("AddrMarkNotFound ");
 844                         printk("}");
 845                         if (err & (BBD_ERR|ECC_ERR|ID_ERR|MARK_ERR)) {
 846                                 byte cur = IN_BYTE(IDE_SELECT_REG);
 847                                 if (cur & 0x40) {       /* using LBA? */
 848                                         printk(", LBAsect=%ld", (unsigned long)
 849                                          ((cur&0xf)<<24)
 850                                          |(IN_BYTE(IDE_HCYL_REG)<<16)
 851                                          |(IN_BYTE(IDE_LCYL_REG)<<8)
 852                                          | IN_BYTE(IDE_SECTOR_REG));
 853                                 } else {
 854                                         printk(", CHS=%d/%d/%d",
 855                                          (IN_BYTE(IDE_HCYL_REG)<<8) +
 856                                           IN_BYTE(IDE_LCYL_REG),
 857                                           cur & 0xf,
 858                                           IN_BYTE(IDE_SECTOR_REG));
 859                                 }
 860                                 if (HWGROUP(drive)->rq)
 861                                         printk(", sector=%ld", HWGROUP(drive)->rq->sector);
 862                         }
 863                 }
 864 #endif  /* FANCY_STATUS_DUMPS */
 865                 printk("\n");
 866         }
 867         restore_flags (flags);
 868         return err;
 869 }
 870 
 871 /*
 872  * try_to_flush_leftover_data() is invoked in response to a drive
 873  * unexpectedly having its DRQ_STAT bit set.  As an alternative to
 874  * resetting the drive, this routine tries to clear the condition
 875  * by read a sector's worth of data from the drive.  Of course,
 876  * this may not help if the drive is *waiting* for data from *us*.
 877  */
 878 static void try_to_flush_leftover_data (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 879 {
 880         int i = (drive->mult_count ? drive->mult_count : 1) * SECTOR_WORDS;
 881 
 882         while (i > 0) {
 883                 unsigned long buffer[16];
 884                 unsigned int wcount = (i > 16) ? 16 : i;
 885                 i -= wcount;
 886                 ide_input_data (drive, buffer, wcount);
 887         }
 888 }
 889 
 890 /*
 891  * ide_error() takes action based on the error returned by the controller.
 892  */
 893 void ide_error (ide_drive_t *drive, const char *msg, byte stat)
     /* [previous][next][first][last][top][bottom][index][help] */
 894 {
 895         struct request *rq;
 896         byte err;
 897 
 898         err = ide_dump_status(drive, msg, stat);
 899         if ((rq = HWGROUP(drive)->rq) == NULL || drive == NULL)
 900                 return;
 901         /* retry only "normal" I/O: */
 902         if (rq->cmd == IDE_DRIVE_CMD) {
 903                 rq->errors = 1;
 904                 ide_end_drive_cmd(drive, stat, err);
 905                 return;
 906         }
 907         if (stat & BUSY_STAT) {         /* other bits are useless when BUSY */
 908                 rq->errors |= ERROR_RESET;
 909         } else {
 910                 if (drive->media == ide_disk && (stat & ERR_STAT)) {
 911                         /* err has different meaning on cdrom and tape */
 912                         if (err & (BBD_ERR | ECC_ERR))  /* retries won't help these */
 913                                 rq->errors = ERROR_MAX;
 914                         else if (err & TRK0_ERR)        /* help it find track zero */
 915                                 rq->errors |= ERROR_RECAL;
 916                 }
 917                 if ((stat & DRQ_STAT) && rq->cmd != WRITE)
 918                         try_to_flush_leftover_data(drive);
 919         }
 920         if (GET_STAT() & (BUSY_STAT|DRQ_STAT))
 921                 rq->errors |= ERROR_RESET;      /* Mmmm.. timing problem */
 922 
 923         if (rq->errors >= ERROR_MAX) {
 924 #ifdef CONFIG_BLK_DEV_IDETAPE
 925                 if (drive->media == ide_tape) {
 926                         rq->errors = 0;
 927                         idetape_end_request(0, HWGROUP(drive));
 928                 }
 929                 else
 930 #endif /* CONFIG_BLK_DEV_IDETAPE */
 931                 ide_end_request(0, HWGROUP(drive));
 932         }
 933         else {
 934                 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
 935                         ++rq->errors;
 936                         ide_do_reset(drive);
 937                         return;
 938                 } else if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
 939                         drive->special.b.recalibrate = 1;
 940                 ++rq->errors;
 941         }
 942 }
 943 
 944 /*
 945  * read_intr() is the handler for disk read/multread interrupts
 946  */
 947 static void read_intr (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 948 {
 949         byte stat;
 950         int i;
 951         unsigned int msect, nsect;
 952         struct request *rq;
 953 
 954         if (!OK_STAT(stat=GET_STAT(),DATA_READY,BAD_R_STAT)) {
 955                 ide_error(drive, "read_intr", stat);
 956                 return;
 957         }
 958         msect = drive->mult_count;
 959 read_next:
 960         rq = HWGROUP(drive)->rq;
 961         if (msect) {
 962                 if ((nsect = rq->current_nr_sectors) > msect)
 963                         nsect = msect;
 964                 msect -= nsect;
 965         } else
 966                 nsect = 1;
 967         ide_input_data(drive, rq->buffer, nsect * SECTOR_WORDS);
 968 #ifdef DEBUG
 969         printk("%s:  read: sectors(%ld-%ld), buffer=0x%08lx, remaining=%ld\n",
 970                 drive->name, rq->sector, rq->sector+nsect-1,
 971                 (unsigned long) rq->buffer+(nsect<<9), rq->nr_sectors-nsect);
 972 #endif
 973         rq->sector += nsect;
 974         rq->buffer += nsect<<9;
 975         rq->errors = 0;
 976         i = (rq->nr_sectors -= nsect);
 977         if ((rq->current_nr_sectors -= nsect) <= 0)
 978                 ide_end_request(1, HWGROUP(drive));
 979         if (i > 0) {
 980                 if (msect)
 981                         goto read_next;
 982                 ide_set_handler (drive, &read_intr, WAIT_CMD);
 983         }
 984 }
 985 
 986 /*
 987  * write_intr() is the handler for disk write interrupts
 988  */
 989 static void write_intr (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 990 {
 991         byte stat;
 992         int i;
 993         ide_hwgroup_t *hwgroup = HWGROUP(drive);
 994         struct request *rq = hwgroup->rq;
 995 
 996         if (OK_STAT(stat=GET_STAT(),DRIVE_READY,drive->bad_wstat)) {
 997 #ifdef DEBUG
 998                 printk("%s: write: sector %ld, buffer=0x%08lx, remaining=%ld\n",
 999                         drive->name, rq->sector, (unsigned long) rq->buffer,
1000                         rq->nr_sectors-1);
1001 #endif
1002                 if ((rq->nr_sectors == 1) ^ ((stat & DRQ_STAT) != 0)) {
1003                         rq->sector++;
1004                         rq->buffer += 512;
1005                         rq->errors = 0;
1006                         i = --rq->nr_sectors;
1007                         --rq->current_nr_sectors;
1008                         if (rq->current_nr_sectors <= 0)
1009                                 ide_end_request(1, hwgroup);
1010                         if (i > 0) {
1011                                 ide_output_data (drive, rq->buffer, SECTOR_WORDS);
1012                                 ide_set_handler (drive, &write_intr, WAIT_CMD);
1013                         }
1014                         return;
1015                 }
1016         }
1017         ide_error(drive, "write_intr", stat);
1018 }
1019 
1020 /*
1021  * ide_multwrite() transfers a block of up to mcount sectors of data
1022  * to a drive as part of a disk multiple-sector write operation.
1023  */
1024 void ide_multwrite (ide_drive_t *drive, unsigned int mcount)
     /* [previous][next][first][last][top][bottom][index][help] */
1025 {
1026         struct request *rq = &HWGROUP(drive)->wrq;
1027 
1028         do {
1029                 unsigned int nsect = rq->current_nr_sectors;
1030                 if (nsect > mcount)
1031                         nsect = mcount;
1032                 mcount -= nsect;
1033 
1034                 ide_output_data(drive, rq->buffer, nsect<<7);
1035 #ifdef DEBUG
1036                 printk("%s: multwrite: sector %ld, buffer=0x%08lx, count=%d, remaining=%ld\n",
1037                         drive->name, rq->sector, (unsigned long) rq->buffer,
1038                         nsect, rq->nr_sectors - nsect);
1039 #endif
1040                 if ((rq->nr_sectors -= nsect) <= 0)
1041                         break;
1042                 if ((rq->current_nr_sectors -= nsect) == 0) {
1043                         if ((rq->bh = rq->bh->b_reqnext) != NULL) {
1044                                 rq->current_nr_sectors = rq->bh->b_size>>9;
1045                                 rq->buffer             = rq->bh->b_data;
1046                         } else {
1047                                 panic("%s: buffer list corrupted\n", drive->name);
1048                                 break;
1049                         }
1050                 } else {
1051                         rq->buffer += nsect << 9;
1052                 }
1053         } while (mcount);
1054 }
1055 
1056 /*
1057  * multwrite_intr() is the handler for disk multwrite interrupts
1058  */
1059 static void multwrite_intr (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1060 {
1061         byte stat;
1062         int i;
1063         ide_hwgroup_t *hwgroup = HWGROUP(drive);
1064         struct request *rq = &hwgroup->wrq;
1065 
1066         if (OK_STAT(stat=GET_STAT(),DRIVE_READY,drive->bad_wstat)) {
1067                 if (stat & DRQ_STAT) {
1068                         if (rq->nr_sectors) {
1069                                 ide_multwrite(drive, drive->mult_count);
1070                                 ide_set_handler (drive, &multwrite_intr, WAIT_CMD);
1071                                 return;
1072                         }
1073                 } else {
1074                         if (!rq->nr_sectors) {  /* all done? */
1075                                 rq = hwgroup->rq;
1076                                 for (i = rq->nr_sectors; i > 0;){
1077                                         i -= rq->current_nr_sectors;
1078                                         ide_end_request(1, hwgroup);
1079                                 }
1080                                 return;
1081                         }
1082                 }
1083         }
1084         ide_error(drive, "multwrite_intr", stat);
1085 }
1086 
1087 /*
1088  * Issue a simple drive command
1089  * The drive must be selected beforehand.
1090  */
1091 static void ide_cmd(ide_drive_t *drive, byte cmd, byte nsect, ide_handler_t *handler)
     /* [previous][next][first][last][top][bottom][index][help] */
1092 {
1093         ide_set_handler (drive, handler, WAIT_CMD);
1094         OUT_BYTE(drive->ctl,IDE_CONTROL_REG);
1095         OUT_BYTE(nsect,IDE_NSECTOR_REG);
1096         OUT_BYTE(cmd,IDE_COMMAND_REG);
1097 }
1098 
1099 /*
1100  * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
1101  */
1102 static void set_multmode_intr (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1103 {
1104         byte stat = GET_STAT();
1105 
1106         sti();
1107         if (OK_STAT(stat,READY_STAT,BAD_STAT)) {
1108                 drive->mult_count = drive->mult_req;
1109         } else {
1110                 drive->mult_req = drive->mult_count = 0;
1111                 drive->special.b.recalibrate = 1;
1112                 (void) ide_dump_status(drive, "set_multmode", stat);
1113         }
1114 }
1115 
1116 /*
1117  * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
1118  */
1119 static void set_geometry_intr (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1120 {
1121         byte stat = GET_STAT();
1122 
1123         sti();
1124         if (!OK_STAT(stat,READY_STAT,BAD_STAT))
1125                 ide_error(drive, "set_geometry_intr", stat);
1126 }
1127 
1128 /*
1129  * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
1130  */
1131 static void recal_intr (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1132 {
1133         byte stat = GET_STAT();
1134 
1135         sti();
1136         if (!OK_STAT(stat,READY_STAT,BAD_STAT))
1137                 ide_error(drive, "recal_intr", stat);
1138 }
1139 
1140 /*
1141  * drive_cmd_intr() is invoked on completion of a special DRIVE_CMD.
1142  */
1143 static void drive_cmd_intr (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1144 {
1145         struct request *rq = HWGROUP(drive)->rq;
1146         byte *args = (byte *) rq->buffer;
1147         byte stat = GET_STAT();
1148 
1149         sti();
1150         if ((stat & DRQ_STAT) && args && args[3]) {
1151                 ide_input_data(drive, &args[4], args[3] * SECTOR_WORDS);
1152                 stat = GET_STAT();
1153         }
1154         if (OK_STAT(stat,READY_STAT,BAD_STAT))
1155                 ide_end_drive_cmd (drive, stat, GET_ERR());
1156         else
1157                 ide_error(drive, "drive_cmd", stat); /* calls ide_end_drive_cmd */
1158 }
1159 
1160 /*
1161  * do_special() is used to issue WIN_SPECIFY, WIN_RESTORE, and WIN_SETMULT
1162  * commands to a drive.  It used to do much more, but has been scaled back.
1163  */
1164 static inline void do_special (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1165 {
1166         special_t *s = &drive->special;
1167 next:
1168 #ifdef DEBUG
1169         printk("%s: do_special: 0x%02x\n", drive->name, s->all);
1170 #endif
1171         if (s->b.set_geometry) {
1172                 s->b.set_geometry = 0;
1173                 if (drive->media == ide_disk) {
1174                         OUT_BYTE(drive->sect,IDE_SECTOR_REG);
1175                         OUT_BYTE(drive->cyl,IDE_LCYL_REG);
1176                         OUT_BYTE(drive->cyl>>8,IDE_HCYL_REG);
1177                         OUT_BYTE(((drive->head-1)|drive->select.all)&0xBF,IDE_SELECT_REG);
1178                         if (!IS_PROMISE_DRIVE)
1179                                 ide_cmd(drive, WIN_SPECIFY, drive->sect, &set_geometry_intr);
1180                 }
1181         } else if (s->b.recalibrate) {
1182                 s->b.recalibrate = 0;
1183                 if (drive->media == ide_disk && !IS_PROMISE_DRIVE)
1184                         ide_cmd(drive, WIN_RESTORE, drive->sect, &recal_intr);
1185         } else if (s->b.set_pio) {
1186                 ide_tuneproc_t *tuneproc = HWIF(drive)->tuneproc;
1187                 s->b.set_pio = 0;
1188                 if (tuneproc != NULL)
1189                         tuneproc(drive, drive->pio_req);
1190                 goto next;
1191         } else if (s->b.set_multmode) {
1192                 s->b.set_multmode = 0;
1193                 if (drive->media == ide_disk) {
1194                         if (drive->id && drive->mult_req > drive->id->max_multsect)
1195                                 drive->mult_req = drive->id->max_multsect;
1196                         if (!IS_PROMISE_DRIVE)
1197                                 ide_cmd(drive, WIN_SETMULT, drive->mult_req, &set_multmode_intr);
1198                 } else
1199                         drive->mult_req = 0;
1200         } else if (s->all) {
1201                 int special = s->all;
1202                 s->all = 0;
1203                 printk("%s: bad special flag: 0x%02x\n", drive->name, special);
1204         }
1205 }
1206 
1207 /*
1208  * This routine busy-waits for the drive status to be not "busy".
1209  * It then checks the status for all of the "good" bits and none
1210  * of the "bad" bits, and if all is okay it returns 0.  All other
1211  * cases return 1 after invoking ide_error() -- caller should just return.
1212  *
1213  * This routine should get fixed to not hog the cpu during extra long waits..
1214  * That could be done by busy-waiting for the first jiffy or two, and then
1215  * setting a timer to wake up at half second intervals thereafter,
1216  * until timeout is achieved, before timing out.
1217  */
1218 int ide_wait_stat (ide_drive_t *drive, byte good, byte bad, unsigned long timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
1219 {
1220         byte stat;
1221         unsigned long flags;
1222 
1223 test:
1224         udelay(1);      /* spec allows drive 400ns to change "BUSY" */
1225         if (OK_STAT((stat = GET_STAT()), good, bad))
1226                 return 0;       /* fast exit for most frequent case */
1227         if (!(stat & BUSY_STAT)) {
1228                 ide_error(drive, "status error", stat);
1229                 return 1;
1230         }
1231 
1232         save_flags(flags);
1233         sti();
1234         timeout += jiffies;
1235         do {
1236                 if (!((stat = GET_STAT()) & BUSY_STAT)) {
1237                         restore_flags(flags);
1238                         goto test;
1239                 }
1240         } while (jiffies <= timeout);
1241 
1242         restore_flags(flags);
1243         ide_error(drive, "status timeout", GET_STAT());
1244         return 1;
1245 }
1246 
1247 /*
1248  * do_rw_disk() issues READ and WRITE commands to a disk,
1249  * using LBA if supported, or CHS otherwise, to address sectors.
1250  * It also takes care of issuing special DRIVE_CMDs.
1251  */
1252 static inline void do_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
     /* [previous][next][first][last][top][bottom][index][help] */
1253 {
1254         ide_hwif_t *hwif = HWIF(drive);
1255         unsigned short io_base = hwif->io_base;
1256 #ifdef CONFIG_BLK_DEV_PROMISE
1257         int use_promise_io = 0;
1258 #endif /* CONFIG_BLK_DEV_PROMISE */
1259 
1260         OUT_BYTE(drive->ctl,IDE_CONTROL_REG);
1261         OUT_BYTE(rq->nr_sectors,io_base+IDE_NSECTOR_OFFSET);
1262 #ifdef CONFIG_BLK_DEV_PROMISE
1263         if (IS_PROMISE_DRIVE) {
1264                 if (hwif->is_promise2 || rq->cmd == READ) {
1265                         use_promise_io = 1;
1266                 }
1267         }
1268         if (drive->select.b.lba || use_promise_io) {
1269 #else /* !CONFIG_BLK_DEV_PROMISE */
1270         if (drive->select.b.lba) {
1271 #endif /* CONFIG_BLK_DEV_PROMISE */
1272 #ifdef DEBUG
1273                 printk("%s: %sing: LBAsect=%ld, sectors=%ld, buffer=0x%08lx\n",
1274                         drive->name, (rq->cmd==READ)?"read":"writ",
1275                         block, rq->nr_sectors, (unsigned long) rq->buffer);
1276 #endif
1277                 OUT_BYTE(block,io_base+IDE_SECTOR_OFFSET);
1278                 OUT_BYTE(block>>=8,io_base+IDE_LCYL_OFFSET);
1279                 OUT_BYTE(block>>=8,io_base+IDE_HCYL_OFFSET);
1280                 OUT_BYTE(((block>>8)&0x0f)|drive->select.all,io_base+IDE_SELECT_OFFSET);
1281         } else {
1282                 unsigned int sect,head,cyl,track;
1283                 track = block / drive->sect;
1284                 sect  = block % drive->sect + 1;
1285                 OUT_BYTE(sect,io_base+IDE_SECTOR_OFFSET);
1286                 head  = track % drive->head;
1287                 cyl   = track / drive->head;
1288                 OUT_BYTE(cyl,io_base+IDE_LCYL_OFFSET);
1289                 OUT_BYTE(cyl>>8,io_base+IDE_HCYL_OFFSET);
1290                 OUT_BYTE(head|drive->select.all,io_base+IDE_SELECT_OFFSET);
1291 #ifdef DEBUG
1292                 printk("%s: %sing: CHS=%d/%d/%d, sectors=%ld, buffer=0x%08lx\n",
1293                         drive->name, (rq->cmd==READ)?"read":"writ", cyl,
1294                         head, sect, rq->nr_sectors, (unsigned long) rq->buffer);
1295 #endif
1296         }
1297 #ifdef CONFIG_BLK_DEV_PROMISE
1298         if (use_promise_io) {
1299                 do_promise_io (drive, rq);
1300                 return;
1301         }
1302 #endif /* CONFIG_BLK_DEV_PROMISE */
1303         if (rq->cmd == READ) {
1304 #ifdef CONFIG_BLK_DEV_TRITON
1305                 if (drive->using_dma && !(HWIF(drive)->dmaproc(ide_dma_read, drive)))
1306                         return;
1307 #endif /* CONFIG_BLK_DEV_TRITON */
1308                 ide_set_handler(drive, &read_intr, WAIT_CMD);
1309                 OUT_BYTE(drive->mult_count ? WIN_MULTREAD : WIN_READ, io_base+IDE_COMMAND_OFFSET);
1310                 return;
1311         }
1312         if (rq->cmd == WRITE) {
1313 #ifdef CONFIG_BLK_DEV_TRITON
1314                 if (drive->using_dma && !(HWIF(drive)->dmaproc(ide_dma_write, drive)))
1315                         return;
1316 #endif /* CONFIG_BLK_DEV_TRITON */
1317                 OUT_BYTE(drive->mult_count ? WIN_MULTWRITE : WIN_WRITE, io_base+IDE_COMMAND_OFFSET);
1318                 if (ide_wait_stat(drive, DATA_READY, drive->bad_wstat, WAIT_DRQ)) {
1319                         printk("%s: no DRQ after issuing %s\n", drive->name,
1320                                 drive->mult_count ? "MULTWRITE" : "WRITE");
1321                         return;
1322                 }
1323                 if (!drive->unmask)
1324                         cli();
1325                 if (drive->mult_count) {
1326                         HWGROUP(drive)->wrq = *rq; /* scratchpad */
1327                         ide_set_handler (drive, &multwrite_intr, WAIT_CMD);
1328                         ide_multwrite(drive, drive->mult_count);
1329                 } else {
1330                         ide_set_handler (drive, &write_intr, WAIT_CMD);
1331                         ide_output_data(drive, rq->buffer, SECTOR_WORDS);
1332                 }
1333                 return;
1334         }
1335         printk("%s: bad command: %d\n", drive->name, rq->cmd);
1336         ide_end_request(0, HWGROUP(drive));
1337 }
1338 
1339 /*
1340  * execute_drive_cmd() issues a special drive command,
1341  * usually initiated by ioctl() from the external hdparm program.
1342  */
1343 static void execute_drive_cmd (ide_drive_t *drive, struct request *rq)
     /* [previous][next][first][last][top][bottom][index][help] */
1344 {
1345         byte *args = rq->buffer;
1346         if (args) {
1347 #ifdef DEBUG
1348                 printk("%s: DRIVE_CMD cmd=0x%02x sc=0x%02x fr=0x%02x xx=0x%02x\n",
1349                  drive->name, args[0], args[1], args[2], args[3]);
1350 #endif
1351                 OUT_BYTE(args[2],IDE_FEATURE_REG);
1352                 ide_cmd(drive, args[0], args[1], &drive_cmd_intr);
1353                 return;
1354         } else {
1355                 /*
1356                  * NULL is actually a valid way of waiting for
1357                  * all current requests to be flushed from the queue.
1358                  */
1359 #ifdef DEBUG
1360                 printk("%s: DRIVE_CMD (null)\n", drive->name);
1361 #endif
1362                 ide_end_drive_cmd(drive, GET_STAT(), GET_ERR());
1363                 return;
1364         }
1365 }
1366 
1367 /*
1368  * do_request() initiates handling of a new I/O request
1369  */
1370 static inline void do_request (ide_hwif_t *hwif, struct request *rq)
     /* [previous][next][first][last][top][bottom][index][help] */
1371 {
1372         unsigned int minor, unit;
1373         unsigned long block, blockend;
1374         ide_drive_t *drive;
1375 
1376         sti();
1377 #ifdef DEBUG
1378         printk("%s: do_request: current=0x%08lx\n", hwif->name, (unsigned long) rq);
1379 #endif
1380         minor = MINOR(rq->rq_dev);
1381         unit = minor >> PARTN_BITS;
1382         if (MAJOR(rq->rq_dev) != hwif->major || unit >= MAX_DRIVES) {
1383                 printk("%s: bad device number: %s\n",
1384                        hwif->name, kdevname(rq->rq_dev));
1385                 goto kill_rq;
1386         }
1387         drive = &hwif->drives[unit];
1388 #ifdef DEBUG
1389         if (rq->bh && !buffer_locked(rq->bh)) {
1390                 printk("%s: block not locked\n", drive->name);
1391                 goto kill_rq;
1392         }
1393 #endif
1394         block    = rq->sector;
1395         blockend = block + rq->nr_sectors;
1396         if ((blockend < block) || (blockend > drive->part[minor&PARTN_MASK].nr_sects)) {
1397                 printk("%s%c: bad access: block=%ld, count=%ld\n", drive->name,
1398                  (minor&PARTN_MASK)?'0'+(minor&PARTN_MASK):' ', block, rq->nr_sectors);
1399                 goto kill_rq;
1400         }
1401         block += drive->part[minor&PARTN_MASK].start_sect + drive->sect0;
1402 #if FAKE_FDISK_FOR_EZDRIVE
1403         if (block == 0 && drive->remap_0_to_1)
1404                 block = 1;  /* redirect MBR access to EZ-Drive partn table */
1405 #endif /* FAKE_FDISK_FOR_EZDRIVE */
1406         ((ide_hwgroup_t *)hwif->hwgroup)->drive = drive;
1407 #if (DISK_RECOVERY_TIME > 0)
1408         while ((read_timer() - hwif->last_time) < DISK_RECOVERY_TIME);
1409 #endif
1410 
1411 #ifdef CONFIG_BLK_DEV_IDETAPE
1412         POLL_HWIF_TAPE_DRIVE;   /* macro from ide-tape.h */
1413 #endif /* CONFIG_BLK_DEV_IDETAPE */
1414 
1415         SELECT_DRIVE(hwif,drive);
1416         if (ide_wait_stat(drive, drive->ready_stat, BUSY_STAT|DRQ_STAT, WAIT_READY)) {
1417                 printk("%s: drive not ready for command\n", drive->name);
1418                 return;
1419         }
1420         
1421         if (!drive->special.all) {
1422                 if (rq->cmd == IDE_DRIVE_CMD) {
1423                         execute_drive_cmd(drive, rq);
1424                         return;
1425                 }
1426 #ifdef CONFIG_BLK_DEV_IDEATAPI
1427                 switch (drive->media) {
1428                         case ide_disk:
1429                                 do_rw_disk (drive, rq, block);
1430                                 return;
1431 #ifdef CONFIG_BLK_DEV_IDECD
1432                         case ide_cdrom:
1433                                 ide_do_rw_cdrom (drive, block);
1434                                 return;
1435 #endif /* CONFIG_BLK_DEV_IDECD */
1436 #ifdef CONFIG_BLK_DEV_IDETAPE
1437                         case ide_tape:
1438                                 idetape_do_request (drive, rq, block);
1439                                 return;
1440 #endif /* CONFIG_BLK_DEV_IDETAPE */
1441 
1442                         default:
1443                                 printk("%s: media type %d not supported\n",
1444                                         drive->name, drive->media);
1445                                 goto kill_rq;
1446                 }
1447 #else
1448                 do_rw_disk (drive, rq, block); /* simpler and faster */
1449                 return;
1450 #endif /* CONFIG_BLK_DEV_IDEATAPI */;
1451         }
1452         do_special(drive);
1453         return;
1454 kill_rq:
1455         ide_end_request(0, hwif->hwgroup);
1456 }
1457 
1458 /*
1459  * The driver enables interrupts as much as possible.  In order to do this,
1460  * (a) the device-interrupt is always masked before entry, and
1461  * (b) the timeout-interrupt is always disabled before entry.
1462  *
1463  * If we enter here from, say irq14, and then start a new request for irq15,
1464  * (possible with "serialize" option) then we cannot ensure that we exit
1465  * before the irq15 hits us. So, we must be careful not to let this bother us.
1466  *
1467  * Interrupts are still masked (by default) whenever we are exchanging
1468  * data/cmds with a drive, because some drives seem to have very poor
1469  * tolerance for latency during I/O.  For devices which don't suffer from
1470  * this problem (most don't), the unmask flag can be set using the "hdparm"
1471  * utility, to permit other interrupts during data/cmd transfers.
1472  */
1473 void ide_do_request (ide_hwgroup_t *hwgroup)
     /* [previous][next][first][last][top][bottom][index][help] */
1474 {
1475         cli();  /* paranoia */
1476         if (hwgroup->handler != NULL) {
1477                 printk("%s: EEeekk!! handler not NULL in ide_do_request()\n", hwgroup->hwif->name);
1478                 return;
1479         }
1480         do {
1481                 ide_hwif_t *hwif = hwgroup->hwif;
1482                 struct request *rq;
1483                 if ((rq = hwgroup->rq) == NULL) {
1484                         if (hwif->sharing_irq && hwgroup->drive) /* set nIEN */
1485                                 OUT_BYTE(hwgroup->drive->ctl|2,hwif->ctl_port);
1486                         /*
1487                          * hwgroup->next_hwif is different from hwgroup->hwif
1488                          * only when a request is inserted using "ide_next".
1489                          * This saves wear and tear on IDE tapes.
1490                          */
1491                         hwif = hwgroup->next_hwif;
1492                         do {
1493                                 rq = blk_dev[hwif->major].current_request;
1494                                 if (rq != NULL && rq->rq_status != RQ_INACTIVE)
1495                                         goto got_rq;
1496                         } while ((hwif = hwif->next) != hwgroup->next_hwif);
1497                         return;         /* no work left for this hwgroup */
1498                 }
1499         got_rq: 
1500                 do_request(hwgroup->hwif = hwgroup->next_hwif = hwif, hwgroup->rq = rq);
1501                 cli();
1502         } while (hwgroup->handler == NULL);
1503 }
1504 
1505 /*
1506  * do_hwgroup_request() invokes ide_do_request() after first masking
1507  * all possible interrupts for the current hwgroup.  This prevents race
1508  * conditions in the event that an unexpected interrupt occurs while
1509  * we are in the driver.
1510  *
1511  * Note that when an interrupt is used to reenter the driver, the first level
1512  * handler will already have masked the irq that triggered, but any other ones
1513  * for the hwgroup will still be unmasked.  The driver tries to be careful
1514  * about such things.
1515  */
1516 static void do_hwgroup_request (ide_hwgroup_t *hwgroup)
     /* [previous][next][first][last][top][bottom][index][help] */
1517 {
1518         if (hwgroup->handler == NULL) {
1519                 ide_hwif_t *hgif = hwgroup->hwif;
1520                 ide_hwif_t *hwif = hgif;
1521                 do {
1522                         disable_irq(hwif->irq);
1523                 } while ((hwif = hwif->next) != hgif);
1524                 ide_do_request (hwgroup);
1525                 do {
1526                         enable_irq(hwif->irq);
1527                 } while ((hwif = hwif->next) != hgif);
1528         }
1529 }
1530 
1531 static void do_ide0_request (void)      /* invoked with cli() */
     /* [previous][next][first][last][top][bottom][index][help] */
1532 {
1533         do_hwgroup_request (ide_hwifs[0].hwgroup);
1534 }
1535 
1536 #if MAX_HWIFS > 1
1537 static void do_ide1_request (void)      /* invoked with cli() */
     /* [previous][next][first][last][top][bottom][index][help] */
1538 {
1539         do_hwgroup_request (ide_hwifs[1].hwgroup);
1540 }
1541 #endif
1542 
1543 #if MAX_HWIFS > 2
1544 static void do_ide2_request (void)      /* invoked with cli() */
     /* [previous][next][first][last][top][bottom][index][help] */
1545 {
1546         do_hwgroup_request (ide_hwifs[2].hwgroup);
1547 }
1548 #endif
1549 
1550 #if MAX_HWIFS > 3
1551 static void do_ide3_request (void)      /* invoked with cli() */
     /* [previous][next][first][last][top][bottom][index][help] */
1552 {
1553         do_hwgroup_request (ide_hwifs[3].hwgroup);
1554 }
1555 #endif
1556 
1557 static void timer_expiry (unsigned long data)
     /* [previous][next][first][last][top][bottom][index][help] */
1558 {
1559         ide_hwgroup_t *hwgroup = (ide_hwgroup_t *) data;
1560         ide_drive_t   *drive   = hwgroup->drive;
1561         unsigned long flags;
1562 
1563         save_flags(flags);
1564         cli();
1565 
1566         if (hwgroup->poll_timeout != 0) { /* polling in progress? */
1567                 ide_handler_t *handler = hwgroup->handler;
1568                 hwgroup->handler = NULL;
1569                 handler(drive);
1570         } else if (hwgroup->handler == NULL) {   /* not waiting for anything? */
1571                 sti(); /* drive must have responded just as the timer expired */
1572                 printk("%s: marginal timeout\n", drive->name);
1573         } else {
1574                 hwgroup->handler = NULL;        /* abort the operation */
1575                 if (hwgroup->hwif->dmaproc)
1576                         (void) hwgroup->hwif->dmaproc (ide_dma_abort, drive);
1577                 ide_error(drive, "irq timeout", GET_STAT());
1578         }
1579         if (hwgroup->handler == NULL)
1580                 do_hwgroup_request (hwgroup);
1581         restore_flags(flags);
1582 }
1583 
1584 /*
1585  * There's nothing really useful we can do with an unexpected interrupt,
1586  * other than reading the status register (to clear it), and logging it.
1587  * There should be no way that an irq can happen before we're ready for it,
1588  * so we needn't worry much about losing an "important" interrupt here.
1589  *
1590  * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
1591  * drive enters "idle", "standby", or "sleep" mode, so if the status looks
1592  * "good", we just ignore the interrupt completely.
1593  *
1594  * This routine assumes cli() is in effect when called.
1595  *
1596  * If an unexpected interrupt happens on irq15 while we are handling irq14
1597  * and if the two interfaces are "serialized" (CMD640), then it looks like
1598  * we could screw up by interfering with a new request being set up for irq15.
1599  *
1600  * In reality, this is a non-issue.  The new command is not sent unless the
1601  * drive is ready to accept one, in which case we know the drive is not
1602  * trying to interrupt us.  And ide_set_handler() is always invoked before
1603  * completing the issuance of any new drive command, so we will not be
1604  * accidently invoked as a result of any valid command completion interrupt.
1605  *
1606  */
1607 static void unexpected_intr (int irq, ide_hwgroup_t *hwgroup)
     /* [previous][next][first][last][top][bottom][index][help] */
1608 {
1609         byte stat;
1610         unsigned int unit;
1611         ide_hwif_t *hwif = hwgroup->hwif;
1612 
1613         /*
1614          * handle the unexpected interrupt
1615          */
1616         do {
1617                 if (hwif->irq == irq) {
1618                         for (unit = 0; unit < MAX_DRIVES; ++unit) {
1619                                 ide_drive_t *drive = &hwif->drives[unit];
1620                                 if (!drive->present)
1621                                         continue;
1622                                 SELECT_DRIVE(hwif,drive);
1623                                 if (!OK_STAT(stat=GET_STAT(), drive->ready_stat, BAD_STAT))
1624                                         (void) ide_dump_status(drive, "unexpected_intr", stat);
1625                                 if ((stat & DRQ_STAT))
1626                                         try_to_flush_leftover_data(drive);
1627                         }
1628                 }
1629         } while ((hwif = hwif->next) != hwgroup->hwif);
1630         SELECT_DRIVE(hwif,hwgroup->drive); /* Ugh.. probably interrupts current I/O */
1631 }
1632 
1633 /*
1634  * entry point for all interrupts, caller does cli() for us
1635  */
1636 void ide_intr (int irq, void *dev_id, struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
1637 {
1638         ide_hwgroup_t  *hwgroup = dev_id;
1639         ide_handler_t  *handler;
1640 
1641         if (irq == hwgroup->hwif->irq && (handler = hwgroup->handler) != NULL) {
1642                 ide_drive_t *drive = hwgroup->drive;
1643                 hwgroup->handler = NULL;
1644                 del_timer(&(hwgroup->timer));
1645                 if (drive->unmask)
1646                         sti();
1647                 handler(drive);
1648                 cli();  /* this is necessary, as next rq may be different irq */
1649                 if (hwgroup->handler == NULL) {
1650                         SET_RECOVERY_TIMER(HWIF(drive));
1651                         ide_do_request(hwgroup);
1652                 }
1653         } else {
1654                 unexpected_intr(irq, hwgroup);
1655         }
1656         cli();
1657 }
1658 
1659 /*
1660  * get_info_ptr() returns the (ide_drive_t *) for a given device number.
1661  * It returns NULL if the given device number does not match any present drives.
1662  */
1663 static ide_drive_t *get_info_ptr (kdev_t i_rdev)
     /* [previous][next][first][last][top][bottom][index][help] */
1664 {
1665         int             major = MAJOR(i_rdev);
1666         unsigned int    h;
1667 
1668         for (h = 0; h < MAX_HWIFS; ++h) {
1669                 ide_hwif_t  *hwif = &ide_hwifs[h];
1670                 if (hwif->present && major == hwif->major) {
1671                         unsigned unit = DEVICE_NR(i_rdev);
1672                         if (unit < MAX_DRIVES) {
1673                                 ide_drive_t *drive = &hwif->drives[unit];
1674                                 if (drive->present)
1675                                         return drive;
1676                         } else if (major == IDE0_MAJOR && unit < 4) {
1677                                 printk("ide: probable bad entry for /dev/hd%c\n", 'a'+unit);
1678                                 printk("ide: to fix it, run:  /usr/src/linux/drivers/block/MAKEDEV.ide\n");
1679                         }
1680                         break;
1681                 }
1682         }
1683         return NULL;
1684 }
1685 
1686 /*
1687  * This function is intended to be used prior to invoking ide_do_drive_cmd().
1688  */
1689 void ide_init_drive_cmd (struct request *rq)
     /* [previous][next][first][last][top][bottom][index][help] */
1690 {
1691         rq->buffer = NULL;
1692         rq->cmd = IDE_DRIVE_CMD;
1693         rq->sector = 0;
1694         rq->nr_sectors = 0;
1695         rq->current_nr_sectors = 0;
1696         rq->sem = NULL;
1697         rq->bh = NULL;
1698         rq->bhtail = NULL;
1699         rq->next = NULL;
1700 
1701 #if 0   /* these are done each time through ide_do_drive_cmd() */
1702         rq->errors = 0;
1703         rq->rq_status = RQ_ACTIVE;
1704         rq->rq_dev = ????;
1705 #endif
1706 }
1707 
1708 /*
1709  * This function issues a special IDE device request
1710  * onto the request queue.
1711  *
1712  * If action is ide_wait, then then rq is queued at the end of
1713  * the request queue, and the function sleeps until it has been
1714  * processed.  This is for use when invoked from an ioctl handler.
1715  *
1716  * If action is ide_preempt, then the rq is queued at the head of
1717  * the request queue, displacing the currently-being-processed
1718  * request and this function returns immediately without waiting
1719  * for the new rq to be completed.  This is VERY DANGEROUS, and is
1720  * intended for careful use by the ATAPI tape/cdrom driver code.
1721  *
1722  * If action is ide_next, then the rq is queued immediately after
1723  * the currently-being-processed-request (if any), and the function
1724  * returns without waiting for the new rq to be completed.  As above,
1725  * This is VERY DANGEROUS, and is intended for careful use by the
1726  * ATAPI tape/cdrom driver code.
1727  *
1728  * If action is ide_end, then the rq is queued at the end of the
1729  * request queue, and the function returns immediately without waiting
1730  * for the new rq to be completed. This is again intended for careful
1731  * use by the ATAPI tape/cdrom driver code. (Currently used by ide-tape.c,
1732  * when operating in the pipelined operation mode).
1733  */
1734 int ide_do_drive_cmd (ide_drive_t *drive, struct request *rq, ide_action_t action)
     /* [previous][next][first][last][top][bottom][index][help] */
1735 {
1736         unsigned long flags;
1737         unsigned int major = HWIF(drive)->major;
1738         struct request *cur_rq;
1739         struct blk_dev_struct *bdev = &blk_dev[major];
1740         struct semaphore sem = MUTEX_LOCKED;
1741 
1742         if (IS_PROMISE_DRIVE && rq->buffer != NULL)
1743                 return -ENOSYS;  /* special drive cmds not supported */
1744         rq->errors = 0;
1745         rq->rq_status = RQ_ACTIVE;
1746         rq->rq_dev = MKDEV(major,(drive->select.b.unit)<<PARTN_BITS);
1747         if (action == ide_wait)
1748                 rq->sem = &sem;
1749         unplug_device(bdev);
1750 
1751         save_flags(flags);
1752         cli();
1753         if (action == ide_next)
1754                 HWGROUP(drive)->next_hwif = HWIF(drive);
1755         cur_rq = bdev->current_request;
1756 
1757         if (cur_rq == NULL || action == ide_preempt) {
1758                 rq->next = cur_rq;
1759                 bdev->current_request = rq;
1760                 if (action == ide_preempt) {
1761                         HWGROUP(drive)->rq = NULL;
1762                 } else
1763                 if (HWGROUP(drive)->rq == NULL) {  /* is this necessary (?) */
1764                         bdev->request_fn();
1765                         cli();
1766                 }
1767         } else {
1768                 if (action == ide_wait || action == ide_end) {
1769                         while (cur_rq->next != NULL)    /* find end of list */
1770                                 cur_rq = cur_rq->next;
1771                 }
1772                 rq->next = cur_rq->next;
1773                 cur_rq->next = rq;
1774         }
1775         if (action == ide_wait  && rq->rq_status != RQ_INACTIVE)
1776                 down(&sem);     /* wait for it to be serviced */
1777         restore_flags(flags);
1778         return rq->errors ? -EIO : 0;   /* return -EIO if errors */
1779 }
1780 
1781 static int ide_open(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
1782 {
1783         ide_drive_t *drive;
1784         unsigned long flags;
1785 
1786         if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
1787                 return -ENODEV;
1788         save_flags(flags);
1789         cli();
1790         while (drive->busy)
1791                 sleep_on(&drive->wqueue);
1792         drive->usage++;
1793         restore_flags(flags);
1794 #ifdef CONFIG_BLK_DEV_IDECD
1795         if (drive->media == ide_cdrom)
1796                 return ide_cdrom_open (inode, filp, drive);
1797 #endif  /* CONFIG_BLK_DEV_IDECD */
1798 #ifdef CONFIG_BLK_DEV_IDETAPE
1799         if (drive->media == ide_tape)
1800                 return idetape_blkdev_open (inode, filp, drive);
1801 #endif  /* CONFIG_BLK_DEV_IDETAPE */
1802         if (drive->removable) {
1803                 byte door_lock[] = {WIN_DOORLOCK,0,0,0};
1804                 struct request rq;
1805                 check_disk_change(inode->i_rdev);
1806                 ide_init_drive_cmd (&rq);
1807                 rq.buffer = door_lock;
1808                 /*
1809                  * Ignore the return code from door_lock,
1810                  * since the open() has already succeeded,
1811                  * and the door_lock is irrelevant at this point.
1812                  */
1813                 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
1814         }
1815         return 0;
1816 }
1817 
1818 /*
1819  * Releasing a block device means we sync() it, so that it can safely
1820  * be forgotten about...
1821  */
1822 static void ide_release(struct inode * inode, struct file * file)
     /* [previous][next][first][last][top][bottom][index][help] */
1823 {
1824         ide_drive_t *drive;
1825 
1826         if ((drive = get_info_ptr(inode->i_rdev)) != NULL) {
1827                 fsync_dev(inode->i_rdev);
1828                 drive->usage--;
1829 #ifdef CONFIG_BLK_DEV_IDECD
1830                 if (drive->media == ide_cdrom) {
1831                         ide_cdrom_release (inode, file, drive);
1832                         return;
1833                 }
1834 #endif  /* CONFIG_BLK_DEV_IDECD */
1835 #ifdef CONFIG_BLK_DEV_IDETAPE
1836                 if (drive->media == ide_tape) {
1837                         idetape_blkdev_release (inode, file, drive);
1838                         return;
1839                 }
1840 #endif  /* CONFIG_BLK_DEV_IDETAPE */
1841                 if (drive->removable && !drive->usage) {
1842                         byte door_unlock[] = {WIN_DOORUNLOCK,0,0,0};
1843                         struct request rq;
1844                         invalidate_buffers(inode->i_rdev);
1845                         ide_init_drive_cmd (&rq);
1846                         rq.buffer = door_unlock;
1847                         (void) ide_do_drive_cmd(drive, &rq, ide_wait);
1848                 }
1849         }
1850 }
1851 
1852 /*
1853  * This routine is called to flush all partitions and partition tables
1854  * for a changed disk, and then re-read the new partition table.
1855  * If we are revalidating a disk because of a media change, then we
1856  * enter with usage == 0.  If we are using an ioctl, we automatically have
1857  * usage == 1 (we need an open channel to use an ioctl :-), so this
1858  * is our limit.
1859  */
1860 static int revalidate_disk(kdev_t i_rdev)
     /* [previous][next][first][last][top][bottom][index][help] */
1861 {
1862         ide_drive_t *drive;
1863         unsigned int p, major, minor;
1864         long flags;
1865 
1866         if ((drive = get_info_ptr(i_rdev)) == NULL)
1867                 return -ENODEV;
1868 
1869         major = MAJOR(i_rdev);
1870         minor = drive->select.b.unit << PARTN_BITS;
1871         save_flags(flags);
1872         cli();
1873         if (drive->busy || (drive->usage > 1)) {
1874                 restore_flags(flags);
1875                 return -EBUSY;
1876         };
1877         drive->busy = 1;
1878         restore_flags(flags);
1879 
1880         for (p = 0; p < (1<<PARTN_BITS); ++p) {
1881                 if (drive->part[p].nr_sects > 0) {
1882                         kdev_t devp = MKDEV(major, minor+p);
1883                         sync_dev           (devp);
1884                         invalidate_inodes  (devp);
1885                         invalidate_buffers (devp);
1886                 }
1887                 drive->part[p].start_sect = 0;
1888                 drive->part[p].nr_sects   = 0;
1889         };
1890 
1891         drive->part[0].nr_sects = current_capacity(drive);
1892         if (drive->media == ide_disk)
1893                 resetup_one_dev(HWIF(drive)->gd, drive->select.b.unit);
1894 
1895         drive->busy = 0;
1896         wake_up(&drive->wqueue);
1897         return 0;
1898 }
1899 
1900 static int write_fs_long (unsigned long useraddr, long value)
     /* [previous][next][first][last][top][bottom][index][help] */
1901 {
1902         int err;
1903 
1904         if (NULL == (long *)useraddr)
1905                 return -EINVAL;
1906         if ((err = verify_area(VERIFY_WRITE, (long *)useraddr, sizeof(long))))
1907                 return err;
1908         put_user((unsigned)value, (long *) useraddr);
1909         return 0;
1910 }
1911 
1912 static int ide_ioctl (struct inode *inode, struct file *file,
     /* [previous][next][first][last][top][bottom][index][help] */
1913                         unsigned int cmd, unsigned long arg)
1914 {
1915         int err;
1916         ide_drive_t *drive;
1917         unsigned long flags;
1918         struct request rq;
1919 
1920         if (!inode || !(inode->i_rdev))
1921                 return -EINVAL;
1922         if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
1923                 return -ENODEV;
1924         ide_init_drive_cmd (&rq);
1925         switch (cmd) {
1926                 case HDIO_GETGEO:
1927                 {
1928                         struct hd_geometry *loc = (struct hd_geometry *) arg;
1929                         if (!loc || drive->media != ide_disk) return -EINVAL;
1930                         err = verify_area(VERIFY_WRITE, loc, sizeof(*loc));
1931                         if (err) return err;
1932                         put_user(drive->bios_head, (byte *) &loc->heads);
1933                         put_user(drive->bios_sect, (byte *) &loc->sectors);
1934                         put_user(drive->bios_cyl, (unsigned short *) &loc->cylinders);
1935                         put_user((unsigned)drive->part[MINOR(inode->i_rdev)&PARTN_MASK].start_sect,
1936                                 (unsigned long *) &loc->start);
1937                         return 0;
1938                 }
1939                 case BLKFLSBUF:
1940                         if (!suser()) return -EACCES;
1941                         fsync_dev(inode->i_rdev);
1942                         invalidate_buffers(inode->i_rdev);
1943                         return 0;
1944 
1945                 case BLKRASET:
1946                         if (!suser()) return -EACCES;
1947                         if(arg > 0xff) return -EINVAL;
1948                         read_ahead[MAJOR(inode->i_rdev)] = arg;
1949                         return 0;
1950 
1951                 case BLKRAGET:
1952                         return write_fs_long(arg, read_ahead[MAJOR(inode->i_rdev)]);
1953 
1954                 case BLKGETSIZE:   /* Return device size */
1955                         return write_fs_long(arg, drive->part[MINOR(inode->i_rdev)&PARTN_MASK].nr_sects);
1956                 case BLKRRPART: /* Re-read partition tables */
1957                         if (!suser()) return -EACCES;
1958                         return revalidate_disk(inode->i_rdev);
1959 
1960                 case HDIO_GET_KEEPSETTINGS:
1961                         return write_fs_long(arg, drive->keep_settings);
1962 
1963                 case HDIO_GET_UNMASKINTR:
1964                         return write_fs_long(arg, drive->unmask);
1965 
1966                 case HDIO_GET_DMA:
1967                         return write_fs_long(arg, drive->using_dma);
1968 
1969                 case HDIO_GET_32BIT:
1970                         return write_fs_long(arg, drive->io_32bit);
1971 
1972                 case HDIO_GET_MULTCOUNT:
1973                         return write_fs_long(arg, drive->mult_count);
1974 
1975                 case HDIO_GET_IDENTITY:
1976                         if (!arg || (MINOR(inode->i_rdev) & PARTN_MASK))
1977                                 return -EINVAL;
1978                         if (drive->id == NULL)
1979                                 return -ENOMSG;
1980                         err = verify_area(VERIFY_WRITE, (char *)arg, sizeof(*drive->id));
1981                         if (!err)
1982                                 memcpy_tofs((char *)arg, (char *)drive->id, sizeof(*drive->id));
1983                         return err;
1984 
1985                         case HDIO_GET_NOWERR:
1986                         return write_fs_long(arg, drive->bad_wstat == BAD_R_STAT);
1987 
1988                 case HDIO_SET_DMA:
1989                         if (!suser()) return -EACCES;
1990 #ifdef CONFIG_BLK_DEV_IDECD
1991                         if (drive->media == ide_cdrom)
1992                                 return -EPERM;
1993 #endif /* CONFIG_BLK_DEV_IDECD */
1994                         if (!drive->id || !(drive->id->capability & 1) || !HWIF(drive)->dmaproc)
1995                                 return -EPERM;
1996                 case HDIO_SET_KEEPSETTINGS:
1997                 case HDIO_SET_UNMASKINTR:
1998                 case HDIO_SET_NOWERR:
1999                         if (arg > 1)
2000                                 return -EINVAL;
2001                 case HDIO_SET_32BIT:
2002                         if (!suser()) return -EACCES;
2003                         if ((MINOR(inode->i_rdev) & PARTN_MASK))
2004                                 return -EINVAL;
2005                         save_flags(flags);
2006                         cli();
2007                         switch (cmd) {
2008                                 case HDIO_SET_DMA:
2009                                         if (!(HWIF(drive)->dmaproc)) {
2010                                                 restore_flags(flags);
2011                                                 return -EPERM;
2012                                         }
2013                                         drive->using_dma = arg;
2014                                         break;
2015                                 case HDIO_SET_KEEPSETTINGS:
2016                                         drive->keep_settings = arg;
2017                                         break;
2018                                 case HDIO_SET_UNMASKINTR:
2019                                         if (arg && HWIF(drive)->no_unmask) {
2020                                                 restore_flags(flags);
2021                                                 return -EPERM;
2022                                         }
2023                                         drive->unmask = arg;
2024                                         break;
2025                                 case HDIO_SET_NOWERR:
2026                                         drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
2027                                         break;
2028                                 case HDIO_SET_32BIT:
2029                                         if (arg > (1 + (SUPPORT_VLB_SYNC<<1)))
2030                                                 return -EINVAL;
2031                                         drive->io_32bit = arg;
2032 #ifdef CONFIG_BLK_DEV_DTC2278
2033                                         if (HWIF(drive)->chipset == ide_dtc2278)
2034                                                 HWIF(drive)->drives[!drive->select.b.unit].io_32bit = arg;
2035 #endif /* CONFIG_BLK_DEV_DTC2278 */
2036                                         break;
2037                         }
2038                         restore_flags(flags);
2039                         return 0;
2040 
2041                 case HDIO_SET_MULTCOUNT:
2042                         if (!suser()) return -EACCES;
2043                         if (MINOR(inode->i_rdev) & PARTN_MASK)
2044                                 return -EINVAL;
2045                         if (drive->id && arg > drive->id->max_multsect)
2046                                 return -EINVAL;
2047                         save_flags(flags);
2048                         cli();
2049                         if (drive->special.b.set_multmode) {
2050                                 restore_flags(flags);
2051                                 return -EBUSY;
2052                         }
2053                         drive->mult_req = arg;
2054                         drive->special.b.set_multmode = 1;
2055                         restore_flags(flags);
2056                         (void) ide_do_drive_cmd (drive, &rq, ide_wait);
2057                         return (drive->mult_count == arg) ? 0 : -EIO;
2058 
2059                 case HDIO_DRIVE_CMD:
2060                 {
2061                         byte args[4], *argbuf = args;
2062                         int argsize = 4;
2063                         if (!suser()) return -EACCES;
2064                         if (NULL == (void *) arg) {
2065                                 err = ide_do_drive_cmd(drive, &rq, ide_wait);
2066                         } else if (!(err = verify_area(VERIFY_READ,(void *)arg, 4))) {
2067                                 memcpy_fromfs(args, (void *)arg, 4);
2068                                 if (args[3]) {
2069                                         argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
2070                                         argbuf = kmalloc(argsize, GFP_KERNEL);
2071                                         if (argbuf == NULL)
2072                                                 return -ENOMEM;
2073                                         argbuf[0] = args[0];
2074                                         argbuf[1] = args[1];
2075                                         argbuf[2] = args[2];
2076                                         argbuf[3] = args[3];
2077                                 }
2078                                 if (!(err = verify_area(VERIFY_WRITE,(void *)arg, argsize))) {
2079                                         rq.buffer = argbuf;
2080                                         err = ide_do_drive_cmd(drive, &rq, ide_wait);
2081                                         memcpy_tofs((void *)arg, argbuf, argsize);
2082                                 }
2083                                 if (argsize > 4)
2084                                         kfree(argbuf);
2085                         }
2086                         return err;
2087                 }
2088                 case HDIO_SET_PIO_MODE:
2089                         if (!suser()) return -EACCES;
2090                         if (MINOR(inode->i_rdev) & PARTN_MASK)
2091                                 return -EINVAL;
2092                         if (!HWIF(drive)->tuneproc)
2093                                 return -ENOSYS;
2094                         save_flags(flags);
2095                         cli();
2096                         drive->pio_req = (int) arg;
2097                         drive->special.b.set_pio = 1;
2098                         restore_flags(flags);
2099                         return 0;
2100 
2101                 RO_IOCTLS(inode->i_rdev, arg);
2102 
2103                 default:
2104 #ifdef CONFIG_BLK_DEV_IDECD
2105                         if (drive->media == ide_cdrom)
2106                                 return ide_cdrom_ioctl(drive, inode, file, cmd, arg);
2107 #endif /* CONFIG_BLK_DEV_IDECD */
2108 #ifdef CONFIG_BLK_DEV_IDETAPE
2109                         if (drive->media == ide_tape)
2110                                 return idetape_blkdev_ioctl(drive, inode, file, cmd, arg);
2111 #endif /* CONFIG_BLK_DEV_IDETAPE */
2112                         return -EPERM;
2113         }
2114 }
2115 
2116 static int ide_check_media_change (kdev_t i_rdev)
     /* [previous][next][first][last][top][bottom][index][help] */
2117 {
2118         ide_drive_t *drive;
2119 
2120         if ((drive = get_info_ptr(i_rdev)) == NULL)
2121                 return -ENODEV;
2122 #ifdef CONFIG_BLK_DEV_IDECD
2123         if (drive->media == ide_cdrom)
2124                 return ide_cdrom_check_media_change (drive);
2125 #endif  /* CONFIG_BLK_DEV_IDECD */
2126         if (drive->removable) /* for disks */
2127                 return 1;       /* always assume it was changed */
2128         return 0;
2129 }
2130 
2131 void ide_fixstring (byte *s, const int bytecount, const int byteswap)
     /* [previous][next][first][last][top][bottom][index][help] */
2132 {
2133         byte *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */
2134 
2135         if (byteswap) {
2136                 /* convert from big-endian to host byte order */
2137                 for (p = end ; p != s;) {
2138                         unsigned short *pp = (unsigned short *) (p -= 2);
2139                         *pp = ntohs(*pp);
2140                 }
2141         }
2142 
2143         /* strip leading blanks */
2144         while (s != end && *s == ' ')
2145                 ++s;
2146 
2147         /* compress internal blanks and strip trailing blanks */
2148         while (s != end && *s) {
2149                 if (*s++ != ' ' || (s != end && *s && *s != ' '))
2150                         *p++ = *(s-1);
2151         }
2152 
2153         /* wipe out trailing garbage */
2154         while (p != end)
2155                 *p++ = '\0';
2156 }
2157 
2158 static inline void do_identify (ide_drive_t *drive, byte cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
2159 {
2160         int bswap;
2161         struct hd_driveid *id;
2162         unsigned long capacity, check;
2163 
2164         id = drive->id = kmalloc (SECTOR_WORDS*4, GFP_KERNEL);
2165         ide_input_data(drive, id, SECTOR_WORDS);        /* read 512 bytes of id info */
2166         sti();
2167 
2168         /*
2169          * EATA SCSI controllers do a hardware ATA emulation:  ignore them
2170          */
2171         if ((id->model[0] == 'P' && id->model[1] == 'M')
2172          || (id->model[0] == 'S' && id->model[1] == 'K')) {
2173                 printk("%s: EATA SCSI HBA %.10s\n", drive->name, id->model);
2174                 drive->present = 0;
2175                 return;
2176         }
2177 
2178         /*
2179          *  WIN_IDENTIFY returns little-endian info,
2180          *  WIN_PIDENTIFY *usually* returns little-endian info.
2181          */
2182         bswap = 1;
2183         if (cmd == WIN_PIDENTIFY) {
2184                 if ((id->model[0] == 'N' && id->model[1] == 'E') /* NEC */
2185                  || (id->model[0] == 'F' && id->model[1] == 'X') /* Mitsumi */
2186                  || (id->model[0] == 'P' && id->model[1] == 'i'))/* Pioneer */
2187                         bswap = 0;      /* Vertos drives may still be weird */
2188         }
2189         ide_fixstring (id->model,     sizeof(id->model),     bswap);
2190         ide_fixstring (id->fw_rev,    sizeof(id->fw_rev),    bswap);
2191         ide_fixstring (id->serial_no, sizeof(id->serial_no), bswap);
2192 
2193 #ifdef CONFIG_BLK_DEV_IDEATAPI
2194         /*
2195          * Check for an ATAPI device
2196          */
2197         if (cmd == WIN_PIDENTIFY) {
2198                 byte type = (id->config >> 8) & 0x1f;
2199                 printk("%s: %s, ATAPI ", drive->name, id->model);
2200 #ifdef CONFIG_BLK_DEV_PROMISE
2201                 if (HWIF(drive)->is_promise2) {
2202                         printk(" -- not supported on 2nd Promise port\n");
2203                         drive->present = 0;
2204                         return;
2205                 }
2206 #endif /* CONFIG_BLK_DEV_PROMISE */
2207                 switch (type) {
2208                         case 0:         /* Early cdrom models used zero */
2209                         case 5:
2210 #ifdef CONFIG_BLK_DEV_IDECD
2211                                 printk ("CDROM drive\n");
2212                                 drive->media = ide_cdrom;
2213                                 drive->present = 1;
2214                                 drive->removable = 1;
2215                                 return;
2216 #else
2217                                 printk ("CDROM ");
2218                                 break;
2219 #endif /* CONFIG_BLK_DEV_IDECD */
2220                         case 1:
2221 #ifdef CONFIG_BLK_DEV_IDETAPE
2222                                 printk ("TAPE drive");
2223                                 if (idetape_identify_device (drive,id)) {
2224                                         drive->media = ide_tape;
2225                                         drive->present = 1;
2226                                         drive->removable = 1;
2227                                         if (drive->autotune != 2 && HWIF(drive)->dmaproc != NULL) {
2228                                                 if (!HWIF(drive)->dmaproc(ide_dma_check, drive))
2229                                                         printk(", DMA");
2230                                         }
2231                                         printk("\n");
2232                                 }
2233                                 else {
2234                                         drive->present = 0;
2235                                         printk ("\nide-tape: the tape is not supported by this version of the driver\n");
2236                                 }
2237                                 return;
2238 #else
2239                                 printk ("TAPE ");
2240                                 break;
2241 #endif /* CONFIG_BLK_DEV_IDETAPE */
2242                         default:
2243                                 drive->present = 0;
2244                                 printk("Type %d - Unknown device\n", type);
2245                                 return;
2246                 }
2247                 drive->present = 0;
2248                 printk("- not supported by this kernel\n");
2249                 return;
2250         }
2251 #endif /* CONFIG_BLK_DEV_IDEATAPI */
2252 
2253         /* check for removable disks (eg. SYQUEST), ignore 'WD' drives */
2254         if (id->config & (1<<7)) {      /* removable disk ? */
2255                 if (id->model[0] != 'W' || id->model[1] != 'D')
2256                         drive->removable = 1;
2257         }
2258 
2259         /* SunDisk drives: treat as non-removable, force one unit */
2260         if (id->model[0] == 'S' && id->model[1] == 'u') {
2261                 drive->removable = 0;
2262                 if (drive->select.all & (1<<4)) {
2263                     drive->present = 0;
2264                     return;
2265                 }
2266         }
2267         
2268         drive->media = ide_disk;
2269         /* Extract geometry if we did not already have one for the drive */
2270         if (!drive->present) {
2271                 drive->present = 1;
2272                 drive->cyl     = drive->bios_cyl  = id->cyls;
2273                 drive->head    = drive->bios_head = id->heads;
2274                 drive->sect    = drive->bios_sect = id->sectors;
2275         }
2276         /* Handle logical geometry translation by the drive */
2277         if ((id->field_valid & 1) && id->cur_cyls && id->cur_heads
2278          && (id->cur_heads <= 16) && id->cur_sectors)
2279         {
2280                 /*
2281                  * Extract the physical drive geometry for our use.
2282                  * Note that we purposely do *not* update the bios info.
2283                  * This way, programs that use it (like fdisk) will
2284                  * still have the same logical view as the BIOS does,
2285                  * which keeps the partition table from being screwed.
2286                  *
2287                  * An exception to this is the cylinder count,
2288                  * which we reexamine later on to correct for 1024 limitations.
2289                  */
2290                 drive->cyl  = id->cur_cyls;
2291                 drive->head = id->cur_heads;
2292                 drive->sect = id->cur_sectors;
2293 
2294                 /* check for word-swapped "capacity" field in id information */
2295                 capacity = drive->cyl * drive->head * drive->sect;
2296                 check = (id->cur_capacity0 << 16) | id->cur_capacity1;
2297                 if (check == capacity) {        /* was it swapped? */
2298                         /* yes, bring it into little-endian order: */
2299                         id->cur_capacity0 = (capacity >>  0) & 0xffff;
2300                         id->cur_capacity1 = (capacity >> 16) & 0xffff;
2301                 }
2302         }
2303         /* Use physical geometry if what we have still makes no sense */
2304         if ((!drive->head || drive->head > 16) && id->heads && id->heads <= 16) {
2305                 drive->cyl  = id->cyls;
2306                 drive->head = id->heads;
2307                 drive->sect = id->sectors;
2308         }
2309         /* Correct the number of cyls if the bios value is too small */
2310         if (drive->sect == drive->bios_sect && drive->head == drive->bios_head) {
2311                 if (drive->cyl > drive->bios_cyl)
2312                         drive->bios_cyl = drive->cyl;
2313         }
2314 
2315         (void) current_capacity (drive); /* initialize LBA selection */
2316 
2317         printk ("%s: %.40s, %ldMB w/%dkB Cache, %sCHS=%d/%d/%d",
2318          drive->name, id->model, current_capacity(drive)/2048L, id->buf_size/2,
2319          drive->select.b.lba ? "LBA, " : "",
2320          drive->bios_cyl, drive->bios_head, drive->bios_sect);
2321 
2322         drive->mult_count = 0;
2323         if (id->max_multsect) {
2324                 drive->mult_req = INITIAL_MULT_COUNT;
2325                 if (drive->mult_req > id->max_multsect)
2326                         drive->mult_req = id->max_multsect;
2327                 if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
2328                         drive->special.b.set_multmode = 1;
2329         }
2330         if (drive->autotune != 2 && HWIF(drive)->dmaproc != NULL) {
2331                 if (!(HWIF(drive)->dmaproc(ide_dma_check, drive)))
2332                         printk(", DMA");
2333         }
2334         printk("\n");
2335 }
2336 
2337 /*
2338  * Delay for *at least* 10ms.  As we don't know how much time is left
2339  * until the next tick occurs, we wait an extra tick to be safe.
2340  * This is used only during the probing/polling for drives at boot time.
2341  */
2342 static void delay_10ms (void)
     /* [previous][next][first][last][top][bottom][index][help] */
2343 {
2344         unsigned long timer = jiffies + (HZ + 99)/100 + 1;
2345         while (timer > jiffies);
2346 }
2347 
2348 /*
2349  * try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
2350  * and waits for a response.  It also monitors irqs while this is
2351  * happening, in hope of automatically determining which one is
2352  * being used by the interface.
2353  *
2354  * Returns:     0  device was identified
2355  *              1  device timed-out (no response to identify request)
2356  *              2  device aborted the command (refused to identify itself)
2357  */
2358 static int try_to_identify (ide_drive_t *drive, byte cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
2359 {
2360         int hd_status, rc;
2361         unsigned long timeout;
2362         int irqs = 0;
2363 
2364         if (!HWIF(drive)->irq) {                /* already got an IRQ? */
2365                 probe_irq_off(probe_irq_on());  /* clear dangling irqs */
2366                 irqs = probe_irq_on();          /* start monitoring irqs */
2367                 OUT_BYTE(drive->ctl,IDE_CONTROL_REG);   /* enable device irq */
2368         }
2369 
2370         delay_10ms();                           /* take a deep breath */
2371         if ((IN_BYTE(IDE_ALTSTATUS_REG) ^ IN_BYTE(IDE_STATUS_REG)) & ~INDEX_STAT) {
2372                 printk("%s: probing with STATUS instead of ALTSTATUS\n", drive->name);
2373                 hd_status = IDE_STATUS_REG;     /* ancient Seagate drives */
2374         } else
2375                 hd_status = IDE_ALTSTATUS_REG;  /* use non-intrusive polling */
2376 
2377 #if CONFIG_BLK_DEV_PROMISE
2378         if (IS_PROMISE_DRIVE) {
2379                 if(promise_cmd(drive,PROMISE_IDENTIFY))
2380                         return 1;
2381         } else
2382 #endif /* CONFIG_BLK_DEV_PROMISE */
2383                 OUT_BYTE(cmd,IDE_COMMAND_REG);          /* ask drive for ID */
2384         timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
2385         timeout += jiffies;
2386         do {
2387                 if (jiffies > timeout) {
2388                         if (!HWIF(drive)->irq)
2389                                 (void) probe_irq_off(irqs);
2390                         return 1;       /* drive timed-out */
2391                 }
2392                 delay_10ms();           /* give drive a breather */
2393         } while (IN_BYTE(hd_status) & BUSY_STAT);
2394 
2395         delay_10ms();           /* wait for IRQ and DRQ_STAT */
2396         if (OK_STAT(GET_STAT(),DRQ_STAT,BAD_R_STAT)) {
2397                 unsigned long flags;
2398                 save_flags(flags);
2399                 cli();                  /* some systems need this */
2400                 do_identify(drive, cmd); /* drive returned ID */
2401                 if (drive->present && drive->media != ide_tape) {
2402                         ide_tuneproc_t *tuneproc = HWIF(drive)->tuneproc;
2403                         if (tuneproc != NULL && drive->autotune == 1)
2404                                 tuneproc(drive, 255);   /* auto-tune PIO mode */
2405                 }
2406                 rc = 0;                 /* drive responded with ID */
2407                 (void) GET_STAT();      /* clear drive IRQ */
2408                 restore_flags(flags);
2409         } else
2410                 rc = 2;                 /* drive refused ID */
2411         if (!HWIF(drive)->irq) {
2412                 irqs = probe_irq_off(irqs);     /* get irq number */
2413                 if (irqs > 0)
2414                         HWIF(drive)->irq = irqs;
2415                 else {                          /* Mmmm.. multiple IRQs */
2416                         printk("%s: IRQ probe failed (%d)\n", drive->name, irqs);
2417 #ifdef CONFIG_BLK_DEV_CMD640
2418                         if (HWIF(drive)->chipset == ide_cmd640) {
2419                                 extern byte (*get_cmd640_reg)(int);
2420                                 printk("%s: Hmmm.. probably a driver problem.\n", drive->name);
2421                                 printk("%s: cmd640 reg 09h == 0x%02x\n", drive->name, get_cmd640_reg(9));
2422                                 printk("%s: cmd640 reg 51h == 0x%02x\n", drive->name, get_cmd640_reg(0x51));
2423                         }
2424 #endif /* CONFIG_BLK_DEV_CMD640 */
2425                 }
2426         }
2427         return rc;
2428 }
2429 
2430 /*
2431  * do_probe() has the difficult job of finding a drive if it exists,
2432  * without getting hung up if it doesn't exist, without trampling on
2433  * ethernet cards, and without leaving any IRQs dangling to haunt us later.
2434  *
2435  * If a drive is "known" to exist (from CMOS or kernel parameters),
2436  * but does not respond right away, the probe will "hang in there"
2437  * for the maximum wait time (about 30 seconds), otherwise it will
2438  * exit much more quickly.
2439  *
2440  * Returns:     0  device was identified
2441  *              1  device timed-out (no response to identify request)
2442  *              2  device aborted the command (refused to identify itself)
2443  *              3  bad status from device (possible for ATAPI drives)
2444  *              4  probe was not attempted because failure was obvious
2445  */
2446 static int do_probe (ide_drive_t *drive, byte cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
2447 {
2448         int rc;
2449         ide_hwif_t *hwif;
2450 #ifdef CONFIG_BLK_DEV_IDEATAPI
2451         if (drive->present) {   /* avoid waiting for inappropriate probes */
2452                 if ((drive->media != ide_disk) && (cmd == WIN_IDENTIFY))
2453                         return 4;
2454         }
2455 #endif  /* CONFIG_BLK_DEV_IDEATAPI */
2456 #ifdef DEBUG
2457         printk("probing for %s: present=%d, media=%d, probetype=%s\n",
2458                 drive->name, drive->present, drive->media,
2459                 (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
2460 #endif
2461         hwif = HWIF(drive);
2462         SELECT_DRIVE(hwif,drive);
2463         OUT_BYTE(drive->select.all,IDE_SELECT_REG);     /* select target drive */
2464         delay_10ms();                           /* wait for BUSY_STAT */
2465         if (IN_BYTE(IDE_SELECT_REG) != drive->select.all && !drive->present) {
2466                 OUT_BYTE(0xa0,IDE_SELECT_REG);  /* exit with drive0 selected */
2467                 return 3;    /* no i/f present: avoid killing ethernet cards */
2468         }
2469 
2470         if (OK_STAT(GET_STAT(),READY_STAT,BUSY_STAT)
2471          || drive->present || cmd == WIN_PIDENTIFY)
2472         {
2473                 if ((rc = try_to_identify(drive,cmd)))   /* send cmd and wait */
2474                         rc = try_to_identify(drive,cmd); /* failed: try again */
2475                 if (rc == 1)
2476                         printk("%s: no response (status = 0x%02x)\n", drive->name, GET_STAT());
2477                 (void) GET_STAT();              /* ensure drive irq is clear */
2478         } else {
2479                 rc = 3;                         /* not present or maybe ATAPI */
2480         }
2481         if (drive->select.b.unit != 0) {
2482                 OUT_BYTE(0xa0,IDE_SELECT_REG);  /* exit with drive0 selected */
2483                 delay_10ms();
2484                 (void) GET_STAT();              /* ensure drive irq is clear */
2485         }
2486         return rc;
2487 }
2488 
2489 /*
2490  * probe_for_drive() tests for existence of a given drive using do_probe().
2491  *
2492  * Returns:     0  no device was found
2493  *              1  device was found (note: drive->present might still be 0)
2494  */
2495 static inline byte probe_for_drive (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
2496 {
2497         if (drive->noprobe)                     /* skip probing? */
2498                 return drive->present;
2499         if (do_probe(drive, WIN_IDENTIFY) >= 2) { /* if !(success||timed-out) */
2500 #ifdef CONFIG_BLK_DEV_IDEATAPI
2501                 (void) do_probe(drive, WIN_PIDENTIFY); /* look for ATAPI device */
2502 #endif  /* CONFIG_BLK_DEV_IDEATAPI */
2503         }
2504         if (!drive->present)
2505                 return 0;                       /* drive not found */
2506         if (drive->id == NULL) {                /* identification failed? */
2507                 if (drive->media == ide_disk) {
2508                         printk ("%s: non-IDE drive, CHS=%d/%d/%d\n",
2509                          drive->name, drive->cyl, drive->head, drive->sect);
2510                 }
2511 #ifdef CONFIG_BLK_DEV_IDECD
2512                 else if (drive->media == ide_cdrom) {
2513                         printk("%s: ATAPI cdrom (?)\n", drive->name);
2514                 }
2515 #endif  /* CONFIG_BLK_DEV_IDECD */
2516                 else {
2517                         drive->present = 0;     /* nuke it */
2518                 }
2519         }
2520         return 1;       /* drive was found */
2521 }
2522 
2523 /*
2524  * We query CMOS about hard disks : it could be that we have a SCSI/ESDI/etc
2525  * controller that is BIOS compatible with ST-506, and thus showing up in our
2526  * BIOS table, but not register compatible, and therefore not present in CMOS.
2527  *
2528  * Furthermore, we will assume that our ST-506 drives <if any> are the primary
2529  * drives in the system -- the ones reflected as drive 1 or 2.  The first
2530  * drive is stored in the high nibble of CMOS byte 0x12, the second in the low
2531  * nibble.  This will be either a 4 bit drive type or 0xf indicating use byte
2532  * 0x19 for an 8 bit type, drive 1, 0x1a for drive 2 in CMOS.  A non-zero value
2533  * means we have an AT controller hard disk for that drive.
2534  *
2535  * Of course, there is no guarantee that either drive is actually on the
2536  * "primary" IDE interface, but we don't bother trying to sort that out here.
2537  * If a drive is not actually on the primary interface, then these parameters
2538  * will be ignored.  This results in the user having to supply the logical
2539  * drive geometry as a boot parameter for each drive not on the primary i/f.
2540  *
2541  * The only "perfect" way to handle this would be to modify the setup.[cS] code
2542  * to do BIOS calls Int13h/Fn08h and Int13h/Fn48h to get all of the drive info
2543  * for us during initialization.  I have the necessary docs -- any takers?  -ml
2544  */
2545 static void probe_cmos_for_drives (ide_hwif_t *hwif)
     /* [previous][next][first][last][top][bottom][index][help] */
2546 {
2547 #ifdef __i386__
2548         extern struct drive_info_struct drive_info;
2549         byte cmos_disks, *BIOS = (byte *) &drive_info;
2550         int unit;
2551 
2552 #ifdef CONFIG_BLK_DEV_PROMISE
2553         if (hwif->is_promise2)
2554                 return;
2555 #endif /* CONFIG_BLK_DEV_PROMISE */
2556         outb_p(0x12,0x70);              /* specify CMOS address 0x12 */
2557         cmos_disks = inb_p(0x71);       /* read the data from 0x12 */
2558         /* Extract drive geometry from CMOS+BIOS if not already setup */
2559         for (unit = 0; unit < MAX_DRIVES; ++unit) {
2560                 ide_drive_t *drive = &hwif->drives[unit];
2561                 if ((cmos_disks & (0xf0 >> (unit*4))) && !drive->present && !drive->nobios) {
2562                         drive->cyl   = drive->bios_cyl  = *(unsigned short *)BIOS;
2563                         drive->head  = drive->bios_head = *(BIOS+2);
2564                         drive->sect  = drive->bios_sect = *(BIOS+14);
2565                         drive->ctl   = *(BIOS+8);
2566                         drive->present = 1;
2567                 }
2568                 BIOS += 16;
2569         }
2570 #endif
2571 }
2572 
2573 /*
2574  * This routine only knows how to look for drive units 0 and 1
2575  * on an interface, so any setting of MAX_DRIVES > 2 won't work here.
2576  */
2577 static void probe_hwif (ide_hwif_t *hwif)
     /* [previous][next][first][last][top][bottom][index][help] */
2578 {
2579         unsigned int unit;
2580 
2581         if (hwif->noprobe)
2582                 return;
2583         if (hwif->io_base == HD_DATA)
2584                 probe_cmos_for_drives (hwif);
2585 #if CONFIG_BLK_DEV_PROMISE
2586         if (!hwif->is_promise2 &&
2587            (check_region(hwif->io_base,8) || check_region(hwif->ctl_port,1))) {
2588 #else
2589         if (check_region(hwif->io_base,8) || check_region(hwif->ctl_port,1)) {
2590 #endif /* CONFIG_BLK_DEV_PROMISE */
2591                 int msgout = 0;
2592                 for (unit = 0; unit < MAX_DRIVES; ++unit) {
2593                         ide_drive_t *drive = &hwif->drives[unit];
2594                         if (drive->present) {
2595                                 drive->present = 0;
2596                                 printk("%s: ERROR, PORTS ALREADY IN USE\n", drive->name);
2597                                 msgout = 1;
2598                         }
2599                 }
2600                 if (!msgout)
2601                         printk("%s: ports already in use, skipping probe\n", hwif->name);
2602         } else {
2603                 unsigned long flags;
2604                 save_flags(flags);
2605 
2606                 sti();  /* needed for jiffies and irq probing */
2607                 /*
2608                  * Second drive should only exist if first drive was found,
2609                  * but a lot of cdrom drives are configured as single slaves.
2610                  */
2611                 for (unit = 0; unit < MAX_DRIVES; ++unit) {
2612                         ide_drive_t *drive = &hwif->drives[unit];
2613                         (void) probe_for_drive (drive);
2614                         if (drive->present && drive->media == ide_disk) {
2615                                 if ((!drive->head || drive->head > 16) && !drive->select.b.lba) {
2616                                         printk("%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
2617                                          drive->name, drive->head);
2618                                         drive->present = 0;
2619                                 }
2620                         }
2621                         if (drive->present && !hwif->present) {
2622                                 hwif->present = 1;
2623                                 request_region(hwif->io_base,  8, hwif->name);
2624                                 request_region(hwif->ctl_port, 1, hwif->name);
2625                         }
2626                 }
2627                 restore_flags(flags);
2628         }
2629 }
2630 
2631 /*
2632  * stridx() returns the offset of c within s,
2633  * or -1 if c is '\0' or not found within s.
2634  */
2635 static int stridx (const char *s, char c)
     /* [previous][next][first][last][top][bottom][index][help] */
2636 {
2637         char *i = strchr(s, c);
2638         return (i && c) ? i - s : -1;
2639 }
2640 
2641 /*
2642  * match_parm() does parsing for ide_setup():
2643  *
2644  * 1. the first char of s must be '='.
2645  * 2. if the remainder matches one of the supplied keywords,
2646  *     the index (1 based) of the keyword is negated and returned.
2647  * 3. if the remainder is a series of no more than max_vals numbers
2648  *     separated by commas, the numbers are saved in vals[] and a
2649  *     count of how many were saved is returned.  Base10 is assumed,
2650  *     and base16 is allowed when prefixed with "0x".
2651  * 4. otherwise, zero is returned.
2652  */
2653 static int match_parm (char *s, const char *keywords[], int vals[], int max_vals)
     /* [previous][next][first][last][top][bottom][index][help] */
2654 {
2655         static const char *decimal = "0123456789";
2656         static const char *hex = "0123456789abcdef";
2657         int i, n;
2658 
2659         if (*s++ == '=') {
2660                 /*
2661                  * Try matching against the supplied keywords,
2662                  * and return -(index+1) if we match one
2663                  */
2664                 for (i = 0; *keywords != NULL; ++i) {
2665                         if (!strcmp(s, *keywords++))
2666                                 return -(i+1);
2667                 }
2668                 /*
2669                  * Look for a series of no more than "max_vals"
2670                  * numeric values separated by commas, in base10,
2671                  * or base16 when prefixed with "0x".
2672                  * Return a count of how many were found.
2673                  */
2674                 for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
2675                         vals[n] = i;
2676                         while ((i = stridx(decimal, *++s)) >= 0)
2677                                 vals[n] = (vals[n] * 10) + i;
2678                         if (*s == 'x' && !vals[n]) {
2679                                 while ((i = stridx(hex, *++s)) >= 0)
2680                                         vals[n] = (vals[n] * 0x10) + i;
2681                         }
2682                         if (++n == max_vals)
2683                                 break;
2684                         if (*s == ',')
2685                                 ++s;
2686                 }
2687                 if (!*s)
2688                         return n;
2689         }
2690         return 0;       /* zero = nothing matched */
2691 }
2692 
2693 /*
2694  * ide_setup() gets called VERY EARLY during initialization,
2695  * to handle kernel "command line" strings beginning with "hdx="
2696  * or "ide".  Here is the complete set currently supported:
2697  *
2698  * "hdx="  is recognized for all "x" from "a" to "h", such as "hdc".
2699  * "idex=" is recognized for all "x" from "0" to "3", such as "ide1".
2700  *
2701  * "hdx=noprobe"        : drive may be present, but do not probe for it
2702  * "hdx=none"           : drive is NOT present, ignore cmos and do not probe
2703  * "hdx=nowerr"         : ignore the WRERR_STAT bit on this drive
2704  * "hdx=cdrom"          : drive is present, and is a cdrom drive
2705  * "hdx=cyl,head,sect"  : disk drive is present, with specified geometry
2706  * "hdx=autotune"       : driver will attempt to tune interface speed
2707  *                              to the fastest PIO mode supported,
2708  *                              if possible for this drive only.
2709  *                              Not fully supported by all chipset types,
2710  *                              and quite likely to cause trouble with
2711  *                              older/odd IDE drives.
2712  *
2713  * "idex=noprobe"       : do not attempt to access/use this interface
2714  * "idex=base"          : probe for an interface at the addr specified,
2715  *                              where "base" is usually 0x1f0 or 0x170
2716  *                              and "ctl" is assumed to be "base"+0x206
2717  * "idex=base,ctl"      : specify both base and ctl
2718  * "idex=base,ctl,irq"  : specify base, ctl, and irq number
2719  * "idex=autotune"      : driver will attempt to tune interface speed
2720  *                              to the fastest PIO mode supported,
2721  *                              for all drives on this interface.
2722  *                              Not fully supported by all chipset types,
2723  *                              and quite likely to cause trouble with
2724  *                              older/odd IDE drives.
2725  * "idex=noautotune"    : driver will NOT attempt to tune interface speed
2726  *                              This is the default for most chipsets,
2727  *                              except the cmd640.
2728  * "idex=serialize"     : do not overlap operations on idex and ide(x^1)
2729  *
2730  * The following are valid ONLY on ide0,
2731  * and the defaults for the base,ctl ports must not be altered.
2732  *
2733  * "ide0=dtc2278"       : probe/support DTC2278 interface
2734  * "ide0=ht6560b"       : probe/support HT6560B interface
2735  * "ide0=cmd640_vlb"    : *REQUIRED* for VLB cards with the CMD640 chip
2736  *                        (not for PCI -- automatically detected)
2737  * "ide0=qd6580"        : probe/support qd6580 interface
2738  * "ide0=ali14xx"       : probe/support ali14xx chipsets (ALI M1439, M1443, M1445)
2739  * "ide0=umc8672"       : probe/support umc8672 chipsets
2740  */
2741 void ide_setup (char *s)
     /* [previous][next][first][last][top][bottom][index][help] */
2742 {
2743         int i, vals[3];
2744         ide_hwif_t *hwif;
2745         ide_drive_t *drive;
2746         unsigned int hw, unit;
2747         const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
2748         const char max_hwif  = '0' + (MAX_HWIFS - 1);
2749 
2750         printk("ide_setup: %s", s);
2751         init_ide_data ();
2752 
2753         /*
2754          * Look for drive options:  "hdx="
2755          */
2756         if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
2757                 const char *hd_words[] = {"none", "noprobe", "nowerr", "cdrom",
2758                                 "serialize", "autotune", "noautotune", NULL};
2759                 unit = s[2] - 'a';
2760                 hw   = unit / MAX_DRIVES;
2761                 unit = unit % MAX_DRIVES;
2762                 hwif = &ide_hwifs[hw];
2763                 drive = &hwif->drives[unit];
2764                 switch (match_parm(&s[3], hd_words, vals, 3)) {
2765                         case -1: /* "none" */
2766                                 drive->nobios = 1;  /* drop into "noprobe" */
2767                         case -2: /* "noprobe" */
2768                                 drive->noprobe = 1;
2769                                 goto done;
2770                         case -3: /* "nowerr" */
2771                                 drive->bad_wstat = BAD_R_STAT;
2772                                 hwif->noprobe = 0;
2773                                 goto done;
2774                         case -4: /* "cdrom" */
2775                                 drive->present = 1;
2776                                 drive->media = ide_cdrom;
2777                                 hwif->noprobe = 0;
2778                                 goto done;
2779                         case -5: /* "serialize" */
2780                                 printk(" -- USE \"ide%d=serialize\" INSTEAD", hw);
2781                                 goto do_serialize;
2782                         case -6: /* "autotune" */
2783                                 drive->autotune = 1;
2784                                 goto done;
2785                         case -7: /* "noautotune" */
2786                                 drive->autotune = 2;
2787                                 goto done;
2788                         case 3: /* cyl,head,sect */
2789                                 drive->media    = ide_disk;
2790                                 drive->cyl      = drive->bios_cyl  = vals[0];
2791                                 drive->head     = drive->bios_head = vals[1];
2792                                 drive->sect     = drive->bios_sect = vals[2];
2793                                 drive->present  = 1;
2794                                 drive->forced_geom = 1;
2795                                 hwif->noprobe = 0;
2796                                 goto done;
2797                         default:
2798                                 goto bad_option;
2799                 }
2800         }
2801         /*
2802          * Look for interface options:  "idex="
2803          */
2804         if (s[0] == 'i' && s[1] == 'd' && s[2] == 'e' && s[3] >= '0' && s[3] <= max_hwif) {
2805                 /*
2806                  * Be VERY CAREFUL changing this: note hardcoded indexes below
2807                  */
2808                 const char *ide_words[] = {"noprobe", "serialize", "autotune", "noautotune",
2809                         "qd6580", "ht6560b", "cmd640_vlb", "dtc2278", "umc8672", "ali14xx", "dc4030", NULL};
2810                 hw = s[3] - '0';
2811                 hwif = &ide_hwifs[hw];
2812                 i = match_parm(&s[4], ide_words, vals, 3);
2813 
2814                 /*
2815                  * Cryptic check to ensure chipset not already set for hwif:
2816                  */
2817                 if (i != -1 && i != -2) {
2818                         if (hwif->chipset != ide_unknown)
2819                                 goto bad_option;
2820                         if (i < 0 && ide_hwifs[1].chipset != ide_unknown)
2821                                 goto bad_option;
2822                 }
2823                 /*
2824                  * Interface keywords work only for ide0:
2825                  */
2826                 if (i <= -6 && hw != 0)
2827                         goto bad_hwif;
2828 
2829                 switch (i) {
2830 #ifdef CONFIG_BLK_DEV_PROMISE
2831                         case -11: /* "dc4030" */
2832                         {
2833                                 setup_dc4030(hwif);
2834                                 goto done;
2835                         }
2836 #endif /* CONFIG_BLK_DEV_PROMISE */
2837 #ifdef CONFIG_BLK_DEV_ALI14XX
2838                         case -10: /* "ali14xx" */
2839                         {
2840                                 extern void init_ali14xx (void);
2841                                 init_ali14xx();
2842                                 goto done;
2843                         }
2844 #endif /* CONFIG_BLK_DEV_ALI14XX */
2845 #ifdef CONFIG_BLK_DEV_UMC8672
2846                         case -9: /* "umc8672" */
2847                         {
2848                                 extern void init_umc8672 (void);
2849                                 init_umc8672();
2850                                 goto done;
2851                         }
2852 #endif /* CONFIG_BLK_DEV_UMC8672 */
2853 #ifdef CONFIG_BLK_DEV_DTC2278
2854                         case -8: /* "dtc2278" */
2855                         {
2856                                 extern void init_dtc2278 (void);
2857                                 init_dtc2278();
2858                                 goto done;
2859                         }
2860 #endif /* CONFIG_BLK_DEV_DTC2278 */
2861 #ifdef CONFIG_BLK_DEV_CMD640
2862                         case -7: /* "cmd640_vlb" */
2863                         {
2864                                 extern int cmd640_vlb; /* flag for cmd640.c */
2865                                 cmd640_vlb = 1;
2866                                 goto done;
2867                         }
2868 #endif /* CONFIG_BLK_DEV_CMD640 */
2869 #ifdef CONFIG_BLK_DEV_HT6560B
2870                         case -6: /* "ht6560b" */
2871                         {
2872                                 extern void init_ht6560b (void);
2873                                 init_ht6560b();
2874                                 goto done;
2875                         }
2876 #endif /* CONFIG_BLK_DEV_HT6560B */
2877 #if CONFIG_BLK_DEV_QD6580
2878                         case -5: /* "qd6580" (has secondary i/f) */
2879                         {
2880                                 extern void init_qd6580 (void);
2881                                 init_qd6580();
2882                                 goto done;
2883                         }
2884 #endif /* CONFIG_BLK_DEV_QD6580 */
2885                         case -4: /* "noautotune" */
2886                                 hwif->drives[0].autotune = 2;
2887                                 hwif->drives[1].autotune = 2;
2888                                 goto done;
2889                         case -3: /* "autotune" */
2890                                 hwif->drives[0].autotune = 1;
2891                                 hwif->drives[1].autotune = 1;
2892                                 goto done;
2893                         case -2: /* "serialize" */
2894                         do_serialize:
2895                                 ide_hwifs[hw].serialized = 1;   /* serialize */
2896                                 ide_hwifs[hw^1].serialized = 1; /* with mate */
2897                                 goto done;
2898 
2899                         case -1: /* "noprobe" */
2900                                 hwif->noprobe = 1;
2901                                 goto done;
2902 
2903                         case 1: /* base */
2904                                 vals[1] = vals[0] + 0x206; /* default ctl */
2905                         case 2: /* base,ctl */
2906                                 vals[2] = 0;    /* default irq = probe for it */
2907                         case 3: /* base,ctl,irq */
2908                                 hwif->io_base  = vals[0];
2909                                 hwif->ctl_port = vals[1];
2910                                 hwif->irq      = vals[2];
2911                                 hwif->noprobe  = 0;
2912                                 hwif->chipset  = ide_generic;
2913                                 goto done;
2914 
2915                         case 0: goto bad_option;
2916                         default:
2917                                 printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
2918                                 return;
2919                 }
2920         }
2921 bad_option:
2922         printk(" -- BAD OPTION\n");
2923         return;
2924 bad_hwif:
2925         printk("-- NOT SUPPORTED ON ide%d", hw);
2926 done:
2927         printk("\n");
2928 }
2929 
2930 /*
2931  * This routine is called from the partition-table code in genhd.c
2932  * to "convert" a drive to a logical geometry with fewer than 1024 cyls.
2933  *
2934  * The second parameter, "xparm", determines exactly how the translation 
2935  * will be handled:
2936  *               0 = convert to CHS with fewer than 1024 cyls
2937  *                      using the same method as Ontrack DiskManager.
2938  *               1 = same as "0", plus offset everything by 63 sectors.
2939  *              -1 = similar to "0", plus redirect sector 0 to sector 1.
2940  *              >1 = convert to a CHS geometry with "xparm" heads.
2941  *
2942  * Returns 0 if the translation was not possible, if the device was not 
2943  * an IDE disk drive, or if a geometry was "forced" on the commandline.
2944  * Returns 1 if the geometry translation was successful.
2945  */
2946 int ide_xlate_1024 (kdev_t i_rdev, int xparm, const char *msg)
     /* [previous][next][first][last][top][bottom][index][help] */
2947 {
2948         ide_drive_t *drive;
2949         static const byte head_vals[] = {4, 8, 16, 32, 64, 128, 255, 0};
2950         const byte *heads = head_vals;
2951         unsigned long tracks;
2952 
2953         if ((drive = get_info_ptr(i_rdev)) == NULL || drive->forced_geom)
2954                 return 0;
2955 
2956         if (xparm > 1 && xparm <= drive->bios_head && drive->bios_sect == 63)
2957                 return 0;               /* we already have a translation */
2958 
2959         printk("%s ", msg);
2960 
2961         if (drive->id) {
2962                 drive->cyl  = drive->id->cyls;
2963                 drive->head = drive->id->heads;
2964                 drive->sect = drive->id->sectors;
2965         }
2966         drive->bios_cyl  = drive->cyl;
2967         drive->bios_head = drive->head;
2968         drive->bios_sect = drive->sect;
2969         drive->special.b.set_geometry = 1;
2970 
2971         tracks = drive->bios_cyl * drive->bios_head * drive->bios_sect / 63;
2972         drive->bios_sect = 63;
2973         if (xparm > 1) {
2974                 drive->bios_head = xparm;
2975                 drive->bios_cyl = tracks / drive->bios_head;
2976         } else {
2977                 while (drive->bios_cyl >= 1024) {
2978                         drive->bios_head = *heads;
2979                         drive->bios_cyl = tracks / drive->bios_head;
2980                         if (0 == *++heads)
2981                                 break;
2982                 }
2983 #if FAKE_FDISK_FOR_EZDRIVE
2984                 if (xparm == -1) {
2985                         drive->remap_0_to_1 = 1;
2986                         msg = "0->1";
2987                 } else
2988 #endif /* FAKE_FDISK_FOR_EZDRIVE */
2989                 if (xparm == 1) {
2990                         drive->sect0 = 63;
2991                         drive->bios_cyl = (tracks - 1) / drive->bios_head;
2992                         msg = "+63";
2993                 }
2994                 printk("[remap %s] ", msg);
2995         }
2996         drive->part[0].nr_sects = current_capacity(drive);
2997         printk("[%d/%d/%d]", drive->bios_cyl, drive->bios_head, drive->bios_sect);
2998         return 1;
2999 }
3000 
3001 
3002 /*
3003  * This routine sets up the irq for an ide interface, and creates a new
3004  * hwgroup for the irq/hwif if none was previously assigned.
3005  *
3006  * The SA_INTERRUPT in sa_flags means ide_intr() is always entered with
3007  * interrupts completely disabled.  This can be bad for interrupt latency,
3008  * but anything else has led to problems on some machines.  We re-enable
3009  * interrupts as much as we can safely do in most places.
3010  */
3011 static int init_irq (ide_hwif_t *hwif)
     /* [previous][next][first][last][top][bottom][index][help] */
3012 {
3013         unsigned long flags;
3014         ide_hwgroup_t *hwgroup = hwif->hwgroup;
3015         ide_hwif_t *mate_hwif;
3016         unsigned int index, mate_irq = hwif->irq;
3017 
3018         save_flags(flags);
3019         cli();
3020 
3021         /*
3022          * Handle serialization, regardless of init sequence
3023          */
3024         mate_hwif = &ide_hwifs[hwif->index ^ 1];
3025         if (hwif->serialized && mate_hwif->present)
3026                 mate_irq = mate_hwif->irq;
3027 
3028         /*
3029          * Group up with any other hwifs that share our irq(s)
3030          */
3031         for (index = 0; index < MAX_HWIFS; index++) {
3032                 if (index != hwif->index) {
3033                         ide_hwif_t *h = &ide_hwifs[index];
3034                         if (h->irq == hwif->irq || h->irq == mate_irq) {
3035                                 hwif->sharing_irq = h->sharing_irq = 1;
3036                                 if (hwgroup && !h->hwgroup)
3037                                         h->hwgroup = hwgroup;
3038                                 else if (!hwgroup)
3039                                         hwgroup = h->hwgroup;
3040                         }
3041                 }
3042         }
3043 
3044         /*
3045          * If we are still without a hwgroup, then form a new one
3046          */
3047         if (hwgroup == NULL) {
3048                 hwgroup = kmalloc (sizeof(ide_hwgroup_t), GFP_KERNEL);
3049                 hwgroup->hwif    = hwgroup->next_hwif = hwif->next = hwif;
3050                 hwgroup->rq      = NULL;
3051                 hwgroup->handler = NULL;
3052                 if (hwif->drives[0].present)
3053                         hwgroup->drive   = &hwif->drives[0];
3054                 else
3055                         hwgroup->drive   = &hwif->drives[1];
3056                 hwgroup->poll_timeout = 0;
3057                 init_timer(&hwgroup->timer);
3058                 hwgroup->timer.function = &timer_expiry;
3059                 hwgroup->timer.data = (unsigned long) hwgroup;
3060         }
3061 
3062         /*
3063          * Allocate the irq, if not already obtained for another hwif
3064          */
3065         if (!hwif->got_irq) {
3066                 if (request_irq(hwif->irq, ide_intr, SA_INTERRUPT|SA_SAMPLE_RANDOM, hwif->name, hwgroup)) {
3067                         restore_flags(flags);
3068                         return 1;
3069                 }
3070                 for (index = 0; index < MAX_HWIFS; index++) {
3071                         ide_hwif_t *g = &ide_hwifs[index];
3072                         if (g->irq == hwif->irq)
3073                                 g->got_irq = 1;
3074                 }
3075         }
3076 
3077         /*
3078          * Everything is okay, so link us into the hwgroup
3079          */
3080         hwif->hwgroup = hwgroup;
3081         hwif->next = hwgroup->hwif->next;
3082         hwgroup->hwif->next = hwif;
3083 
3084         restore_flags(flags);   /* safe now that hwif->hwgroup is set up */
3085 
3086         printk("%s at 0x%03x-0x%03x,0x%03x on irq %d", hwif->name,
3087                 hwif->io_base, hwif->io_base+7, hwif->ctl_port, hwif->irq);
3088         if (hwgroup->hwif != hwif)
3089                 printk(" (serialized with %s)", hwgroup->hwif->name);
3090         printk("\n");
3091         return 0;
3092 }
3093 
3094 static struct file_operations ide_fops = {
3095         NULL,                   /* lseek - default */
3096         block_read,             /* read - general block-dev read */
3097         block_write,            /* write - general block-dev write */
3098         NULL,                   /* readdir - bad */
3099         NULL,                   /* select */
3100         ide_ioctl,              /* ioctl */
3101         NULL,                   /* mmap */
3102         ide_open,               /* open */
3103         ide_release,            /* release */
3104         block_fsync             /* fsync */
3105         ,NULL,                  /* fasync */
3106         ide_check_media_change, /* check_media_change */
3107         revalidate_disk         /* revalidate */
3108 };
3109 
3110 #ifdef CONFIG_PCI
3111 #if defined(CONFIG_BLK_DEV_RZ1000) || defined(CONFIG_BLK_DEV_TRITON)
3112 
3113 typedef void (ide_pci_init_proc_t)(byte, byte);
3114 
3115 /*
3116  * ide_probe_pci() scans PCI for a specific vendor/device function,
3117  * and invokes the supplied init routine for each instance detected.
3118  */
3119 static void ide_probe_pci (unsigned short vendor, unsigned short device, ide_pci_init_proc_t *init, int func_adj)
     /* [previous][next][first][last][top][bottom][index][help] */
3120 {
3121         unsigned long flags;
3122         unsigned index;
3123         byte fn, bus;
3124 
3125         save_flags(flags);
3126         cli();
3127         for (index = 0; !pcibios_find_device (vendor, device, index, &bus, &fn); ++index) {
3128                 init (bus, fn + func_adj);
3129         }
3130         restore_flags(flags);
3131 }
3132 
3133 #endif /* defined(CONFIG_BLK_DEV_RZ1000) || defined(CONFIG_BLK_DEV_TRITON) */
3134 #endif /* CONFIG_PCI */
3135 
3136 /*
3137  * ide_init_pci() finds/initializes "known" PCI IDE interfaces
3138  *
3139  * This routine should ideally be using pcibios_find_class() to find
3140  * all IDE interfaces, but that function causes some systems to "go weird".
3141  */
3142 static void probe_for_hwifs (void)
     /* [previous][next][first][last][top][bottom][index][help] */
3143 {
3144 #ifdef CONFIG_PCI
3145         /*
3146          * Find/initialize PCI IDE interfaces
3147          */
3148         if (pcibios_present()) {
3149 #ifdef CONFIG_BLK_DEV_RZ1000
3150                 ide_pci_init_proc_t init_rz1000;
3151                 ide_probe_pci (PCI_VENDOR_ID_PCTECH, PCI_DEVICE_ID_PCTECH_RZ1000, &init_rz1000, 0);
3152                 ide_probe_pci (PCI_VENDOR_ID_PCTECH, PCI_DEVICE_ID_PCTECH_RZ1001, &init_rz1000, 0);
3153 #endif /* CONFIG_BLK_DEV_RZ1000 */
3154 #ifdef CONFIG_BLK_DEV_TRITON
3155                 /*
3156                  * Apparently the BIOS32 services on Intel motherboards are
3157                  * buggy and won't find the PCI_DEVICE_ID_INTEL_82371_1 for us.
3158                  * So instead, we search for PCI_DEVICE_ID_INTEL_82371_0,
3159                  * and then add 1.
3160                  */
3161                 ide_probe_pci (PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371_0, &ide_init_triton, 1);
3162 #endif /* CONFIG_BLK_DEV_TRITON */
3163         }
3164 #endif /* CONFIG_PCI */
3165 #ifdef CONFIG_BLK_DEV_CMD640
3166         {
3167                 extern void ide_probe_for_cmd640x (void);
3168                 ide_probe_for_cmd640x();
3169         }
3170 #endif
3171 #ifdef CONFIG_BLK_DEV_PROMISE
3172         init_dc4030();
3173 #endif
3174 }
3175 
3176 static int hwif_init (int h)
     /* [previous][next][first][last][top][bottom][index][help] */
3177 {
3178         ide_hwif_t *hwif = &ide_hwifs[h];
3179         void (*rfn)(void);
3180         
3181         if (!hwif->present)
3182                 return 0;
3183         if (!hwif->irq) {
3184                 if (!(hwif->irq = default_irqs[h])) {
3185                         printk("%s: DISABLED, NO IRQ\n", hwif->name);
3186                         return (hwif->present = 0);
3187                 }
3188         }
3189 #ifdef CONFIG_BLK_DEV_HD
3190         if (hwif->irq == HD_IRQ && hwif->io_base != HD_DATA) {
3191                 printk("%s: CANNOT SHARE IRQ WITH OLD HARDDISK DRIVER (hd.c)\n", hwif->name);
3192                 return (hwif->present = 0);
3193         }
3194 #endif /* CONFIG_BLK_DEV_HD */
3195         
3196         hwif->present = 0; /* we set it back to 1 if all is ok below */
3197         switch (hwif->major) {
3198         case IDE0_MAJOR: rfn = &do_ide0_request; break;
3199 #if MAX_HWIFS > 1
3200         case IDE1_MAJOR: rfn = &do_ide1_request; break;
3201 #endif
3202 #if MAX_HWIFS > 2
3203         case IDE2_MAJOR: rfn = &do_ide2_request; break;
3204 #endif
3205 #if MAX_HWIFS > 3
3206         case IDE3_MAJOR: rfn = &do_ide3_request; break;
3207 #endif
3208         default:
3209                 printk("%s: request_fn NOT DEFINED\n", hwif->name);
3210                 return (hwif->present = 0);
3211         }
3212         if (register_blkdev (hwif->major, hwif->name, &ide_fops)) {
3213                 printk("%s: UNABLE TO GET MAJOR NUMBER %d\n", hwif->name, hwif->major);
3214         } else if (init_irq (hwif)) {
3215                 printk("%s: UNABLE TO GET IRQ %d\n", hwif->name, hwif->irq);
3216                 (void) unregister_blkdev (hwif->major, hwif->name);
3217         } else {
3218                 init_gendisk(hwif);
3219                 blk_dev[hwif->major].request_fn = rfn;
3220                 read_ahead[hwif->major] = 8;    /* (4kB) */
3221                 hwif->present = 1;      /* success */
3222         }
3223         return hwif->present;
3224 }
3225 
3226 /*
3227  * This is gets invoked once during initialization, to set *everything* up
3228  */
3229 int ide_init (void)
     /* [previous][next][first][last][top][bottom][index][help] */
3230 {
3231         int index;
3232 
3233         init_ide_data ();
3234         /*
3235          * Probe for special "known" interface chipsets
3236          */
3237         probe_for_hwifs ();
3238 
3239         /*
3240          * Probe for drives in the usual way.. CMOS/BIOS, then poke at ports
3241          */
3242         for (index = 0; index < MAX_HWIFS; ++index)
3243                 probe_hwif (&ide_hwifs[index]);
3244         for (index = 0; index < MAX_HWIFS; ++index)
3245                 hwif_init (index);
3246 
3247 #ifdef CONFIG_BLK_DEV_IDETAPE
3248         idetape_register_chrdev();      /* Register character device interface to the ide tape */
3249 #endif /* CONFIG_BLK_DEV_IDETAPE */
3250         
3251         return 0;
3252 }
3253 
3254 #ifdef CONFIG_BLK_DEV_IDE_PCMCIA
3255 int ide_register(int io_base, int ctl_port, int irq)
     /* [previous][next][first][last][top][bottom][index][help] */
3256 {
3257         int index, i, rc = -1;
3258         ide_hwif_t *hwif;
3259         unsigned long flags;
3260 
3261         save_flags(flags);
3262         cli();
3263         for (index = 0; index < MAX_HWIFS; ++index) {
3264                 hwif = &ide_hwifs[index];
3265                 if (hwif->present) {
3266                         if (hwif->io_base == io_base || hwif->ctl_port == ctl_port)
3267                                 break; /* this ide port already exists */
3268                 } else {
3269                         hwif->io_base = io_base;
3270                         hwif->ctl_port = ctl_port;
3271                         hwif->irq = irq;
3272                         hwif->noprobe = 0;
3273                         probe_hwif(hwif);
3274                         if (!hwif_init(index))
3275                                 break;
3276                         for (i = 0; i < hwif->gd->nr_real; i++)
3277                                 revalidate_disk(MKDEV(hwif->major, i<<PARTN_BITS));
3278                         rc = index;
3279                         break;
3280                 }
3281         }
3282         restore_flags(flags);
3283         return rc;
3284 }
3285 
3286 void ide_unregister (unsigned int index)
     /* [previous][next][first][last][top][bottom][index][help] */
3287 {
3288         struct gendisk *gd, **gdp;
3289         ide_hwif_t *hwif, *g;
3290         ide_hwgroup_t *hwgroup;
3291         int irq_count = 0;
3292         unsigned long flags;
3293 
3294         if (index >= MAX_HWIFS)
3295                 return;
3296         save_flags(flags);
3297         cli();
3298         hwif = &ide_hwifs[index];
3299         if (!hwif->present || hwif->drives[0].busy || hwif->drives[1].busy) {
3300                 restore_flags(flags);
3301                 return;
3302         }
3303         hwif->present = 0;
3304         hwgroup = hwif->hwgroup;
3305 
3306         /*
3307          * free the irq if we were the only hwif using it
3308          */
3309         g = hwgroup->hwif;
3310         do {
3311                 if (g->irq == hwif->irq)
3312                         ++irq_count;
3313                 g = g->next;
3314         } while (g != hwgroup->hwif);
3315         if (irq_count == 1)
3316                 free_irq(hwif->irq, hwgroup);
3317 
3318         /*
3319          * Note that we only release the standard ports,
3320          * and do not even try to handle any extra ports
3321          * allocated for weird IDE interface chipsets.
3322          */
3323         release_region(hwif->io_base, 8);
3324         release_region(hwif->ctl_port, 1);
3325 
3326         /*
3327          * Remove us from the hwgroup, and free
3328          * the hwgroup if we were the only member
3329          */
3330         while (hwgroup->hwif->next != hwif)
3331                 hwgroup->hwif = hwgroup->hwif->next;
3332         hwgroup->hwif->next = hwif->next;
3333         if (hwgroup->hwif == hwif)
3334                 hwgroup->hwif = hwif->next;
3335         if (hwgroup->next_hwif == hwif)
3336                 hwgroup->next_hwif = hwif->next;
3337         if (hwgroup->hwif == hwif)
3338                 kfree(hwgroup);
3339 
3340         /*
3341          * Remove us from the kernel's knowledge
3342          */
3343         unregister_blkdev(hwif->major, hwif->name);
3344         kfree(blksize_size[hwif->major]);
3345         blk_dev[hwif->major].request_fn = NULL;
3346         blksize_size[hwif->major] = NULL;
3347         for (gdp = &gendisk_head; *gdp; gdp = &((*gdp)->next))
3348                 if (*gdp == hwif->gd)
3349                         break;
3350         if (*gdp == NULL)
3351                 printk("gd not in disk chain!\n");
3352         else {
3353                 gd = *gdp; *gdp = gd->next;
3354                 kfree(gd->sizes);
3355                 kfree(gd->part);
3356                 kfree(gd);
3357         }
3358         init_hwif_data (index); /* restore hwif data to pristine status */
3359         restore_flags(flags);
3360 }
3361 #endif /* CONFIG_BLK_DEV_IDE_PCMCIA */

/* [previous][next][first][last][top][bottom][index][help] */