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

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