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

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