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

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