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

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