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

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