root/drivers/block/ide.c

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

DEFINITIONS

This source file includes following definitions.
  1. read_timer
  2. input_ide_data
  3. output_ide_data
  4. start_ide_timer
  5. do_ide_reset
  6. end_drive_cmd
  7. dump_status
  8. ide_error
  9. read_intr
  10. write_intr
  11. multwrite
  12. multwrite_intr
  13. ide_cmd
  14. set_multmode_intr
  15. set_geometry_intr
  16. recal_intr
  17. drive_cmd_intr
  18. timer_expiry
  19. ide0_timer_expiry
  20. ide1_timer_expiry
  21. do_special
  22. wait_stat
  23. do_rw_disk
  24. do_request
  25. do_ide0_request
  26. do_ide1_request
  27. do_ide0_request
  28. do_shared_request
  29. unexpected_intr
  30. ide_seq_intr
  31. ide0_intr
  32. ide1_intr
  33. ide_intr
  34. ide_shared_intr
  35. get_info_ptr
  36. ide_open
  37. ide_release
  38. revalidate_disk
  39. do_drive_cmd
  40. write_fs_long
  41. ide_ioctl
  42. ide_check_media_change
  43. fixstring
  44. lba_capacity_is_ok
  45. do_identify
  46. delay_10ms
  47. try_to_identify
  48. do_probe
  49. probe_for_drive
  50. probe_for_drives
  51. ide_setup
  52. hda_setup
  53. hdb_setup
  54. hdc_setup
  55. hdd_setup
  56. probe_cmos_for_drives
  57. init_ide_data
  58. setup_irq
  59. ide_geninit
  60. ide0_geninit
  61. ide1_geninit
  62. sub22
  63. try_to_init_dtc2278
  64. ide_init

   1 /*
   2  *  linux/drivers/block/ide.c   Version 3.16  May 30, 1995
   3  *
   4  *  Copyright (C) 1994, 1995  Linus Torvalds & authors (see below)
   5  */
   6 
   7 /*
   8  * This is the dual IDE interface driver, as evolved from hd.c.  
   9  * It supports up to two IDE interfaces, on one or two IRQs (usually 14 & 15).
  10  * There can be up to two drives per interface, as per the ATA-2 spec.
  11  *
  12  * Primary   i/f: ide0: major=3;  (hda)         minor=0, (hdb)         minor=64
  13  * Secondary i/f: ide1: major=22; (hdc or hd1a) minor=0, (hdd or hd1b) minor=64
  14  *
  15  *  From hd.c:
  16  *  |
  17  *  | It traverses the request-list, using interrupts to jump between functions.
  18  *  | As nearly all functions can be called within interrupts, we may not sleep.
  19  *  | Special care is recommended.  Have Fun!
  20  *  |
  21  *  | modified by Drew Eckhardt to check nr of hd's from the CMOS.
  22  *  |
  23  *  | Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
  24  *  | in the early extended-partition checks and added DM partitions.
  25  *  |
  26  *  | Early work on error handling by Mika Liljeberg (liljeber@cs.Helsinki.FI).
  27  *  |
  28  *  | IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
  29  *  | and general streamlining by Mark Lord (mlord@bnr.ca).
  30  *
  31  *  October, 1994 -- Complete line-by-line overhaul for linux 1.1.x, by:
  32  *
  33  *      Mark Lord       (mlord@bnr.ca)                  (IDE Perf.Pkg)
  34  *      Delman Lee      (delman@mipg.upenn.edu)         ("Mr. atdisk2")
  35  *      Petri Mattila   (ptjmatti@kruuna.helsinki.fi)   (EIDE stuff)
  36  *      Scott Snyder    (snyder@fnald0.fnal.gov)        (ATAPI IDE cd-rom)
  37  *
  38  *  This was a rewrite of just about everything from hd.c, though some original
  39  *  code is still sprinkled about.  Think of it as a major evolution, with 
  40  *  inspiration from lots of linux users, esp.  hamish@zot.apana.org.au
  41  *
  42  *  Version 1.0 ALPHA   initial code, primary i/f working okay
  43  *  Version 1.1 ALPHA   fixes for dual i/f
  44  *  Version 1.2 ALPHA   first serious attempt at sharing irqs
  45  *  Version 1.3 BETA    dual i/f on shared irq tested & working!
  46  *  Version 1.4 BETA    added auto probing for irq(s)
  47  *  Version 1.5 BETA    added ALPHA (untested) support for IDE cd-roms,
  48  *                      fixed hd.c coexistence bug, other minor stuff
  49  *  Version 1.6 BETA    fix link error when cd-rom not configured
  50  *  Version 2.0 BETA    lots of minor fixes; remove annoying messages; ...
  51  *  Version 2.2 BETA    fixed reset_drives; major overhaul of autoprobing
  52  *  Version 2.3 BETA    set DEFAULT_UNMASK_INTR to 0 again; cosmetic changes
  53  *  Version 2.4 BETA    added debounce on reading of drive status reg,
  54  *                      added config flags to remove unwanted features
  55  *  Version 2.5 BETA    fixed problem with leftover phantom IRQ after probe,
  56  *                      allow "set_geometry" even when in LBA (as per spec(?)),
  57  *                      assorted miscellaneous tweaks.
  58  *  Version 2.6 BETA    more config flag stuff, another probing tweak,
  59  *  (not released)      multmode now defaults to status quo from boot time,
  60  *                      moved >16heads check to init time, rearranged reset code
  61  *                      added HDIO_DRIVE_CMD, removed standby/xfermode stuff
  62  *                      hopefully fixed ATAPI probing code, added hdx=cdrom
  63  *  Version 2.7 BETA    fixed invocation of cdrom_setup()
  64  *  Version 2.8 BETA    fixed compile error for DISK_RECOVERY_TIME>0
  65  *                      fixed incorrect drive selection in DO_DRIVE_CMD (Bug!)
  66  *  Version 2.9 BETA    more work on ATAPI CDROM recognition
  67  *  (not released)      changed init order so partition checks go in sequence
  68  *  Version 3.0 BETA    included ide-cd.c update from Steve with Mitsumi fixes
  69  *                      attempt to fix byte-swap problem with Mitsumi id_info
  70  *                      ensure drives on second i/f get initialized on boot
  71  *                      preliminary compile-time support for 32bit IDE i/f chips
  72  *                      added check_region() and snarf_region() to probes
  73  *  Version 3.1 BETA    ensure drives on *both* i/f get initialized on boot
  74  *                      fix byte-swap problem with Mitsumi id_info
  75  *                      changed ide_timermask into ide_timerbit
  76  *                      get rid of unexpected interrupts after probing
  77  *                      don't wait for READY_STAT on cdrom drives
  78  *  Version 3.2 BETA    Ooops.. mistakenly left VLB_32BIT_IDE on by default
  79  *                      new ide-cd.c from Scott
  80  *  Version 3.3 BETA    fix compiling with PROBE_FOR_IRQS==0
  81  *  (sent to Linus)     tweak in do_probe() to fix Delman's DRDY problem
  82  *  Version 3.4 BETA    removed "444" debug message
  83  *  (sent to Linus)
  84  *  Version 3.5         correct the bios_cyl field if it's too small
  85  *  (linux 1.1.76)       (to help fdisk with brain-dead BIOSs)
  86  *  Version 3.6         cosmetic corrections to comments and stuff
  87  *  (linux 1.1.77)      reorganise probing code to make it understandable
  88  *                      added halfway retry to probing for drive identification
  89  *                      added "hdx=noprobe" command line option
  90  *                      allow setting multmode even when identification fails
  91  *  Version 3.7         move set_geometry=1 from do_identify() to ide_init()
  92  *                      increase DRQ_WAIT to eliminate nuisance messages
  93  *                      wait for DRQ_STAT instead of DATA_READY during probing
  94  *                        (courtesy of Gary Thomas gary@efland.UU.NET)
  95  *  Version 3.8         fixed byte-swapping for confused Mitsumi cdrom drives
  96  *                      update of ide-cd.c from Scott, allows blocksize=1024
  97  *                      cdrom probe fixes, inspired by jprang@uni-duisburg.de
  98  *  Version 3.9         don't use LBA if lba_capacity looks funny
  99  *                      correct the drive capacity calculations
 100  *                      fix probing for old Seagates without HD_ALTSTATUS
 101  *                      fix byte-ordering for some NEC cdrom drives
 102  *  Version 3.10        disable multiple mode by default; was causing trouble
 103  *  Version 3.11        fix mis-identification of old WD disks as cdroms
 104  *  Version 3,12        simplify logic for selecting initial mult_count
 105  *                        (fixes problems with buggy WD drives)
 106  *  Version 3.13        remove excess "multiple mode disabled" messages
 107  *  Version 3.14        fix ide_error() handling of BUSY_STAT
 108  *                      fix byte-swapped cdrom strings (again.. arghh!)
 109  *                      ignore INDEX bit when checking the ALTSTATUS reg
 110  *  Version 3.15        add SINGLE_THREADED flag for use with dual-CMD i/f
 111  *                      ignore WRERR_STAT for non-write operations
 112  *                      added VLB_SYNC support for DC-2000A & others,
 113  *                       (incl. some Promise chips), courtesy of Frank Gockel
 114  *  Version 3.16        convert VLB_32BIT and VLB_SYNC into runtime flags
 115  *                      add ioctls to get/set VLB flags (HDIO_[SG]ET_CHIPSET)
 116  *                      rename SINGLE_THREADED to SUPPORT_SERIALIZE,
 117  *                      add boot flag to "serialize" operation for CMD i/f
 118  *                      add optional support for DTC2278 interfaces,
 119  *                       courtesy of andy@cercle.cts.com (Dyan Wile).
 120  *                      add boot flag to enable "dtc2278" probe
 121  *                      add probe to avoid EATA (SCSI) interfaces,
 122  *                       courtesy of neuffer@goofy.zdv.uni-mainz.de.
 123  *
 124  *  To do:
 125  *      - improved CMD support:  tech info is supposedly "in the mail"
 126  *      - special 32-bit controller-type detection & support
 127  *      - figure out how to support oddball "intelligent" caching cards
 128  *      - reverse-engineer 3/4 drive support on fancy "Promise" cards
 129  */
 130 
 131 #include <linux/config.h>
 132 #include <linux/errno.h>
 133 #include <linux/signal.h>
 134 #include <linux/sched.h>
 135 #include <linux/timer.h>
 136 #include <linux/fs.h>
 137 #include <linux/kernel.h>
 138 #include <linux/hdreg.h>
 139 #include <linux/genhd.h>
 140 #include <linux/malloc.h>
 141 #include <linux/string.h>
 142 #include <linux/delay.h>
 143 #include <linux/major.h>
 144 #include <linux/ioport.h>
 145 #include <linux/interrupt.h>
 146 #include <linux/blkdev.h>
 147 #include <asm/bitops.h>
 148 #include <asm/irq.h>
 149 #include <asm/segment.h>
 150 #include <asm/system.h>
 151 
 152 /*****************************************************************************
 153  * IDE driver configuration options (play with these as desired):
 154  */
 155 #define REALLY_SLOW_IO                  /* most systems can safely undef this */
 156 #include <asm/io.h>
 157 
 158 #undef  REALLY_FAST_IO                  /* define if ide ports are perfect */
 159 #define INITIAL_MULT_COUNT      0       /* off=0; on=2,4,8,16,32, etc.. */
 160 #ifndef SUPPORT_VLB_32BIT               /* 1 to support 32bit I/O on VLB */
 161 #define SUPPORT_VLB_32BIT       1       /* 0 to reduce kernel size */
 162 #endif
 163 #ifndef SUPPORT_VLB_SYNC                /* 1 to support weird 32-bit chips */
 164 #define SUPPORT_VLB_SYNC        1       /* 0 to reduce kernel size */
 165 #endif
 166 #ifndef DISK_RECOVERY_TIME              /* off=0; on=access_delay_time */
 167 #define DISK_RECOVERY_TIME      0       /*  for hardware that needs it */
 168 #endif
 169 #ifndef OK_TO_RESET_CONTROLLER          /* 1 needed for good error recovery */
 170 #define OK_TO_RESET_CONTROLLER  1       /* 0 for use with AH2372A/B interface */
 171 #endif
 172 #ifndef SUPPORT_TWO_INTERFACES          /* 1 to support one/two interfaces */
 173 #define SUPPORT_TWO_INTERFACES  1       /* 0 for a smaller, faster kernel */
 174 #endif
 175 #ifndef OPTIMIZE_IRQS                   /* 1 for slightly faster code */
 176 #define OPTIMIZE_IRQS           1       /* 0 to reduce kernel size */
 177 #endif
 178 #ifndef SUPPORT_SERIALIZE               /* 1 to support CMD dual interfaces */
 179 #define SUPPORT_SERIALIZE       1       /* 0 to reduce kernel size */
 180 #endif
 181 #ifndef SUPPORT_SHARING_IRQ             /* 1 to allow two IDE i/f on one IRQ */
 182 #define SUPPORT_SHARING_IRQ     1       /* 0 to reduce kernel size */
 183 #endif
 184 #ifndef SUPPORT_DTC2278                 /* 1 to support DTC2278 chipset */
 185 #define SUPPORT_DTC2278         1       /* 0 to reduce kernel size */
 186 #endif
 187 #ifndef FANCY_STATUS_DUMPS              /* 1 for human-readable drive errors */
 188 #define FANCY_STATUS_DUMPS      1       /* 0 to reduce kernel size */
 189 #endif
 190 #define PROBE_FOR_IRQS          1       /* 0 to force use of defaults below */
 191 #define DEFAULT_IDE0_IRQ        14      /* in case irq-probe fails */
 192 #define DEFAULT_IDE1_IRQ        15      /* in case irq-probe fails */
 193 
 194 /* IDE_DRIVE_CMD is used to implement many features of the hdparm utility */
 195 #define IDE_DRIVE_CMD           99      /* (magic) undef to reduce kernel size*/
 196 
 197 /*
 198  *  "No user-serviceable parts" beyond this point  :)
 199  ******************************************************************************
 200  */
 201 
 202 /*
 203  * Need to change these elsewhere in the kernel (someday)
 204  */
 205 #ifndef IDE0_TIMER
 206 #define IDE0_TIMER              HD_TIMER
 207 #define IDE1_TIMER              HD_TIMER2
 208 #endif
 209 
 210 /*
 211  * Ensure that various configuration flags have compatible settings
 212  */
 213 #ifdef REALLY_SLOW_IO
 214 #undef REALLY_FAST_IO
 215 #endif
 216 #ifdef CONFIG_BLK_DEV_HD
 217 #undef  SUPPORT_TWO_INTERFACES
 218 #define SUPPORT_TWO_INTERFACES  0
 219 #endif  /* CONFIG_BLK_DEV_HD */
 220 
 221 #if SUPPORT_TWO_INTERFACES
 222 #define HWIF                    hwif
 223 #define DEV_HWIF                (dev->hwif)
 224 #if SUPPORT_SERIALIZE
 225 #undef  SUPPORT_SHARING_IRQ
 226 #define SUPPORT_SHARING_IRQ     1
 227 #endif
 228 #else
 229 #undef  SUPPORT_SERIALIZE
 230 #define SUPPORT_SERIALIZE               0
 231 #undef  OPTIMIZE_IRQS
 232 #define OPTIMIZE_IRQS           0
 233 #undef  SUPPORT_SHARING_IRQ
 234 #define SUPPORT_SHARING_IRQ     0
 235 #ifdef CONFIG_BLK_DEV_HD
 236 #define HWIF                    1
 237 #else
 238 #define HWIF                    0
 239 #endif  /* CONFIG_BLK_DEV_HD */
 240 #define DEV_HWIF                HWIF
 241 #endif  /* SUPPORT_TWO_INTERFACES */
 242 
 243 /*
 244  * Definitions for accessing IDE controller registers
 245  */
 246 typedef unsigned char           byte;   /* used everywhere */
 247 #define IDE_PORT(p,hwif) ((p)^((hwif)<<7)) /* IDE0: p^0x00 , IDE1: p^0x80 */
 248 
 249 #ifdef REALLY_FAST_IO
 250 #define OUT_BYTE(b,p)           outb((b),IDE_PORT(p,DEV_HWIF))
 251 #define IN_BYTE(p,hwif)         (byte)inb(IDE_PORT(p,hwif))
 252 #else
 253 #define OUT_BYTE(b,p)           outb_p((b),IDE_PORT(p,DEV_HWIF))
 254 #define IN_BYTE(p,hwif)         (byte)inb_p(IDE_PORT(p,hwif))
 255 #endif /* REALLY_FAST_IO */
 256 
 257 #if SUPPORT_VLB_32BIT
 258 #if SUPPORT_VLB_SYNC
 259 #define VLB_SYNC __asm__ __volatile__ ("pusha\n movl $0x01f2,%edx\n inb (%dx),%al\n inb (%dx),%al\n inb (%dx),%al\n popa\n")
 260 #endif  /* SUPPORT_VLB_SYNC */
 261 #endif  /* SUPPORT_VLB_32BIT */
 262 
 263 #if SUPPORT_DTC2278
 264 static uint probe_dtc2278 = 0;
 265 #endif
 266 
 267 #define GET_ERR(hwif)           IN_BYTE(HD_ERROR,hwif)
 268 #define GET_STAT(hwif)          IN_BYTE(HD_STATUS,hwif)
 269 #define OK_STAT(stat,good,bad)  (((stat)&((good)|(bad)))==(good))
 270 #define BAD_R_STAT              (BUSY_STAT   | ERR_STAT)
 271 #define BAD_W_STAT              (BUSY_STAT   | ERR_STAT | WRERR_STAT)
 272 #define BAD_STAT                (BAD_R_STAT  | DRQ_STAT)
 273 #define DRIVE_READY             (READY_STAT  | SEEK_STAT)
 274 #define DATA_READY              (DRIVE_READY | DRQ_STAT)
 275 
 276 /*
 277  * Some more useful definitions
 278  */
 279 #define BIOS_SECTORS(dev)       (dev->bios_head*dev->bios_sect*dev->bios_cyl)
 280 #define HD_NAME         "hd"    /* the same for both i/f;  see also genhd.c */
 281 #define PARTN_BITS      6       /* number of minor dev bits for partitions */
 282 #define PARTN_MASK      ((1<<PARTN_BITS)-1)     /* a useful bit mask */
 283 #define MAX_DRIVES      2       /* per interface; 2 assumed by lots of code */
 284 #define SECTOR_WORDS    (512 / 4)       /* number of 32bit words per sector */
 285 
 286 /*
 287  * Timeouts for various operations:
 288  */
 289 #define WAIT_DRQ        5       /* 50msec - spec allows up to 20ms */
 290 #define WAIT_READY      3       /* 30msec - should be instantaneous */
 291 #define WAIT_PIDENTIFY  100     /* 1sec   - should be less than 3ms (?) */
 292 #define WAIT_WORSTCASE  3000    /* 30sec  - worst case when spinning up */
 293 #define WAIT_CMD        1000    /* 10sec  - maximum wait for an IRQ to happen */
 294 
 295 /*
 296  * Now for the data we need to maintain per-device:  ide_dev_t
 297  *
 298  * For fast indexing, sizeof(ide_dev_t) = 32 = power_of_2;
 299  * Everything is carefully aligned on appropriate boundaries,
 300  *  and several fields are placed for optimal (gcc) access.
 301  */
 302 typedef enum {disk, cdrom} dev_type;
 303 
 304 typedef union {
 305         unsigned all                    : 8;    /* all of the bits together */
 306         struct {
 307                 unsigned set_geometry   : 1;    /* respecify drive geometry */
 308                 unsigned recalibrate    : 1;    /* seek to cyl 0      */
 309                 unsigned set_multmode   : 1;    /* set multmode count */
 310                 unsigned reserved       : 5;    /* unused */
 311                 } b;
 312         } special_t;
 313 
 314 typedef union {
 315         unsigned all                    : 8;    /* all of the bits together */
 316         struct {
 317                 unsigned head           : 4;    /* always zeros here */
 318                 unsigned drive          : 1;    /* drive number */
 319                 unsigned bit5           : 1;    /* always 1 */
 320                 unsigned lba            : 1;    /* LBA instead of CHS */
 321                 unsigned bit7           : 1;    /* always 1 */
 322         } b;
 323         } select_t;
 324 
 325 typedef struct {
 326         byte     hwif;                  /* first field gets very fast access */
 327         byte     unmask;                /* pretty quick access to this also */
 328         dev_type type           : 1;    /* disk or cdrom (or tape, floppy..) */
 329         unsigned present        : 1;    /* drive is physically present */
 330         unsigned dont_probe     : 1;    /* from:  hdx=noprobe */
 331         unsigned keep_settings  : 1;    /* restore settings after drive reset */
 332         unsigned busy           : 1;    /* mutex for ide_open, revalidate_.. */
 333         unsigned vlb_32bit      : 1;    /* use 32bit in/out for data */
 334         unsigned vlb_sync       : 1;    /* needed for some 32bit chip sets */
 335         unsigned reserved0      : 1;    /* unused */
 336         special_t special;              /* special action flags */
 337         select_t  select;               /* basic drive/head select reg value */
 338         byte mult_count, chipset, reserved2;
 339         byte usage, mult_req, wpcom, ctl;
 340         byte head, sect, bios_head, bios_sect;
 341         unsigned short cyl, bios_cyl;
 342         const char *name;
 343         struct hd_driveid *id;
 344         struct wait_queue *wqueue;
 345         } ide_dev_t;
 346 
 347 /*
 348  * Stuff prefixed by "ide_" is indexed by the IDE interface number: 0 or 1
 349  */
 350 static const byte       ide_major    [2] = {IDE0_MAJOR,IDE1_MAJOR};
 351 static byte             ide_irq      [2] = {DEFAULT_IDE0_IRQ,DEFAULT_IDE1_IRQ};
 352 static struct hd_struct ide_hd       [2][MAX_DRIVES<<PARTN_BITS] = {{{0,0},},};
 353 static int              ide_sizes    [2][MAX_DRIVES<<PARTN_BITS] = {{0,},};
 354 static int              ide_blksizes [2][MAX_DRIVES<<PARTN_BITS] = {{0,},};
 355 static unsigned long    ide_capacity [2][MAX_DRIVES] = {{0,},};
 356 static ide_dev_t        ide_dev      [2][MAX_DRIVES] = {{{0,},},};
 357 static ide_dev_t        *ide_cur_dev [2] = {NULL,NULL};
 358 static void             (*ide_handler[2])(ide_dev_t *) = {NULL,NULL};
 359 static struct request   *ide_cur_rq  [2] = {NULL,NULL}; /* current request */
 360 static struct request   ide_write_rq [2];  /* copy of *ide_cur_rq for WRITEs */
 361 static const int        ide_timer    [2] = {IDE0_TIMER,IDE1_TIMER};
 362 static const int        ide_timerbit[2] = {(1<<IDE0_TIMER),(1<<IDE1_TIMER)};
 363 static const char       *ide_name    [2] = {"ide0", "ide1"};
 364 static const char       *ide_devname [2][MAX_DRIVES] = /* for printk()'s */
 365         {{HD_NAME "a", HD_NAME "b"}, {HD_NAME "c", HD_NAME "d"}};
 366 static const char       *unsupported = " not supported by this kernel\n";
 367 
 368 static byte             single_threaded    = 0;
 369 #if SUPPORT_SHARING_IRQ
 370 static byte             sharing_single_irq = 0; /* for two i/f on one IRQ */
 371 static volatile byte    current_hwif = 0;       /* for single_threaded==1 */
 372 #endif /* SUPPORT_SHARING_IRQ */
 373 
 374 /*
 375  * This structure is used to register our block device(s) with the kernel:
 376  */
 377 static void ide0_geninit(void), ide1_geninit(void);
 378 static struct gendisk   ide_gendisk  [2] =
 379         {{
 380                 IDE0_MAJOR,     /* major number */      
 381                 HD_NAME,        /* same as below; see genhd.c before changing */
 382                 PARTN_BITS,     /* minor_shift (to extract minor number) */
 383                 1 << PARTN_BITS,/* max_p (number of partitions per real) */
 384                 MAX_DRIVES,     /* maximum number of real drives */
 385                 ide0_geninit,   /* init function */
 386                 ide_hd[0],      /* hd_struct */
 387                 ide_sizes[0],   /* block sizes */
 388                 0,              /* nr_real (number of drives present) */
 389                 ide_dev[0],     /* ptr to internal data structure */
 390                 NULL            /* next */
 391         },{
 392                 IDE1_MAJOR,     /* major number */      
 393                 HD_NAME,        /* same as above; see genhd.c before changing */
 394                 PARTN_BITS,     /* minor_shift (to extract minor number) */
 395                 1 << PARTN_BITS,/* max_p (number of partitions per real) */
 396                 MAX_DRIVES,     /* maximum number of real drives */
 397                 ide1_geninit,   /* init function */
 398                 ide_hd[1],      /* hd_struct */
 399                 ide_sizes[1],   /* block sizes */
 400                 0,              /* nr_real (number of drives present) */
 401                 ide_dev[1],     /* ptr to internal data structure */
 402                 NULL            /* next */
 403         }};
 404 
 405 /*
 406  * One final include file, which references some of the data/defns from above
 407  */
 408 #define IDE_DRIVER      /* "parameter" for blk.h */
 409 #include "blk.h"
 410 
 411 /*
 412  * For really screwy hardware (hey, at least it *can* be used with Linux!
 413  */
 414 #if (DISK_RECOVERY_TIME > 0)
 415 static unsigned long    ide_lastreq[] = {0,0}; /* completion time of last I/O */
 416 #define SET_DISK_RECOVERY_TIMER  ide_lastreq[DEV_HWIF] = read_timer();
 417 static unsigned long read_timer(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 418 {
 419         unsigned long t, flags;
 420         int i;
 421 
 422         save_flags(flags);
 423         cli();
 424         t = jiffies * 11932;
 425         outb_p(0, 0x43);
 426         i = inb_p(0x40);
 427         i |= inb(0x40) << 8;
 428         restore_flags(flags);
 429         return (t - i);
 430 }
 431 #else
 432 #define SET_DISK_RECOVERY_TIMER /* nothing */
 433 #endif /* DISK_RECOVERY_TIME */
 434 
 435 /*
 436  * The heart of the driver, referenced from lots of other routines:
 437  */
 438 static void do_request (byte hwif);
 439 #define DO_REQUEST {SET_DISK_RECOVERY_TIMER do_request(DEV_HWIF);}
 440 
 441 /*
 442  * This is a macro rather than an inline to permit better gcc code.
 443  * Caller MUST do sti() before invoking WAIT_STAT() (for jiffies to work).
 444  *
 445  * This routine should get fixed to not hog the cpu during extra long waits..
 446  * That could be done by busy-waiting for the first jiffy or two, and then
 447  * setting a timer to wake up at half second intervals thereafter,
 448  * until WAIT_WORSTCASE is achieved, before timing out.
 449  */
 450 #define WAIT_STAT(dev,good,bad,timeout,msg,label)                       \
 451 {                                                                       \
 452         byte stat;                                                      \
 453         udelay(1);      /* spec allows drive 400ns to assert "BUSY" */  \
 454         if (GET_STAT(DEV_HWIF) & BUSY_STAT) {                           \
 455                 unsigned long timer = jiffies + timeout;                \
 456                 do {                                                    \
 457                         if ((GET_STAT(DEV_HWIF) & BUSY_STAT) == 0)      \
 458                                 break;                                  \
 459                 } while (timer > jiffies);                              \
 460         }                                                               \
 461         udelay(1);      /* spec allows 400ns for status to stabilize */ \
 462         if (!OK_STAT(stat=GET_STAT(DEV_HWIF), good, bad)) {             \
 463                 ide_error(dev, msg " error", stat);                     \
 464                 goto label;                                             \
 465         }                                                               \
 466 }
 467 
 468 /*
 469  * This is used for all data transfers *from* the IDE interface
 470  */
 471 void input_ide_data (ide_dev_t *dev, void *buffer, uint wcount)
     /* [previous][next][first][last][top][bottom][index][help] */
 472 {
 473 #if SUPPORT_VLB_32BIT
 474         if (dev->vlb_32bit) {
 475 #if SUPPORT_VLB_SYNC
 476                 if (dev->vlb_sync) {
 477                         cli();
 478                         VLB_SYNC;
 479                         insl(IDE_PORT(HD_DATA,DEV_HWIF), buffer, wcount);
 480                         if (dev->unmask)
 481                                 sti();
 482                 } else
 483 #endif /* SUPPORT_VLB_SYNC */
 484                         insl(IDE_PORT(HD_DATA,DEV_HWIF), buffer, wcount);
 485         } else
 486 #endif /* SUPPORT_VLB_32BIT */
 487                 insw(IDE_PORT(HD_DATA,DEV_HWIF), buffer, wcount<<1);
 488 }
 489 
 490 /*
 491  * This is used for all data transfers *to* the IDE interface
 492  */
 493 void output_ide_data (ide_dev_t *dev, void *buffer, uint wcount)
     /* [previous][next][first][last][top][bottom][index][help] */
 494 {
 495 #if SUPPORT_VLB_32BIT
 496         if (dev->vlb_32bit) {
 497 #if SUPPORT_VLB_SYNC
 498                 if (dev->vlb_sync) {
 499                         cli();
 500                         VLB_SYNC;
 501                         outsl(IDE_PORT(HD_DATA,DEV_HWIF), buffer, wcount);
 502                         if (dev->unmask)
 503                                 sti();
 504                 } else
 505                         outsl(IDE_PORT(HD_DATA,DEV_HWIF), buffer, wcount);
 506 #endif /* SUPPORT_VLB_SYNC */
 507         } else
 508 #endif /* SUPPORT_VLB_32BIT */
 509                 outsw(IDE_PORT(HD_DATA,DEV_HWIF), buffer, wcount<<1);
 510 }
 511 
 512 /*
 513  * This should get invoked on every exit path from the driver.
 514  */
 515 static inline void start_ide_timer (byte hwif)
     /* [previous][next][first][last][top][bottom][index][help] */
 516 {
 517         if (ide_handler[HWIF] != NULL) {        /* waiting for an irq? */
 518                 timer_table[ide_timer[HWIF]].expires = jiffies + WAIT_CMD;
 519                 timer_active |= ide_timerbit[HWIF];
 520         }
 521 }
 522 
 523 static void do_ide_reset (ide_dev_t *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 524 {
 525         byte tmp;
 526         unsigned long timer, flags;
 527 
 528         save_flags(flags);
 529         sti();
 530         for (tmp = 0; tmp < MAX_DRIVES; tmp++) {
 531                 ide_dev_t *rdev = &ide_dev[DEV_HWIF][tmp];
 532                 rdev->special.b.set_geometry = 1;
 533                 rdev->special.b.recalibrate  = 1;
 534                 rdev->special.b.set_multmode = 0;
 535                 if (OK_TO_RESET_CONTROLLER)
 536                         rdev->mult_count = 0;
 537                 if (!rdev->keep_settings) {
 538                         rdev->mult_req = 0;
 539                         rdev->unmask = 0;
 540                 }
 541                 if (rdev->mult_req != rdev->mult_count)
 542                         rdev->special.b.set_multmode = 1;
 543         }
 544 
 545 #if OK_TO_RESET_CONTROLLER
 546         cli();
 547         OUT_BYTE(dev->ctl|6,HD_CMD);    /* set nIEN, set SRST */
 548         udelay(10);                     /* more than enough time */
 549         OUT_BYTE(dev->ctl|2,HD_CMD);    /* clear SRST */
 550         udelay(10);                     /* more than enough time */
 551         sti();                          /* needed for jiffies */
 552         for (timer = jiffies + WAIT_WORSTCASE; timer > jiffies;) {
 553                 if ((GET_STAT(DEV_HWIF) & BUSY_STAT) == 0)
 554                         break;
 555         }
 556         printk("%s: do_ide_reset: ", ide_name[DEV_HWIF]);
 557         /* ATAPI devices usually do *not* assert READY after a reset */
 558         if (!OK_STAT(tmp=GET_STAT(DEV_HWIF), 0, BUSY_STAT)) {
 559                 printk("timed-out, status=0x%02x\n", tmp);
 560         } else  {
 561                 if ((tmp = GET_ERR(DEV_HWIF)) == 1)
 562                         printk("success\n");
 563                 else {
 564                         printk("%s: ", ide_devname[DEV_HWIF][0]);
 565                         switch (tmp & 0x7f) {
 566                                 case 1: printk("passed");
 567                                         break;
 568                                 case 2: printk("formatter device error");
 569                                         break;
 570                                 case 3: printk("sector buffer error");
 571                                         break;
 572                                 case 4: printk("ECC circuitry error");
 573                                         break;
 574                                 case 5: printk("controlling MPU error");
 575                                         break;
 576                                 default:printk("error (0x%02x?)", tmp);
 577                         }
 578                         if (tmp & 0x80)
 579                                 printk("; %s: error", ide_devname[DEV_HWIF][1]);
 580                         printk("\n");
 581                 }
 582         }
 583 #endif  /* OK_TO_RESET_CONTROLLER */
 584         restore_flags(flags);
 585 }
 586 
 587 /*
 588  * Clean up after success/failure of an explicit (ioctl) drive cmd
 589  */
 590 static void end_drive_cmd (ide_dev_t *dev, byte stat, byte err)
     /* [previous][next][first][last][top][bottom][index][help] */
 591 {
 592         unsigned long flags;
 593         struct request *rq = ide_cur_rq[DEV_HWIF];
 594         byte *args = (byte *) rq->buffer;
 595 
 596         rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
 597         if (args) {
 598                 args[0] = stat;
 599                 args[1] = err;
 600                 args[2] = IN_BYTE(HD_NSECTOR,DEV_HWIF);
 601         }
 602         save_flags(flags);
 603         cli();
 604         up(rq->sem);
 605         ide_cur_rq[DEV_HWIF] = NULL;
 606         restore_flags(flags);
 607 }
 608 
 609 /*
 610  * Error reporting, in human readable form (luxurious, but a memory hog).
 611  */
 612 static byte dump_status (byte hwif, const char *msg, byte stat)
     /* [previous][next][first][last][top][bottom][index][help] */
 613 {
 614         unsigned long flags;
 615         byte err = 0;
 616         ide_dev_t *dev = ide_cur_dev[HWIF];
 617         const char *name = dev ? dev->name : ide_name[HWIF];
 618 
 619         save_flags (flags);
 620         sti();
 621         printk("%s: %s: status=0x%02x", name, msg, stat);
 622 #if FANCY_STATUS_DUMPS
 623         if (dev && dev->type == disk) {
 624                 printk(" { ");
 625                 if (stat & BUSY_STAT)
 626                         printk("Busy ");
 627                 else {
 628                         if (stat & READY_STAT)  printk("DriveReady ");
 629                         if (stat & WRERR_STAT)  printk("WriteFault ");
 630                         if (stat & SEEK_STAT)   printk("SeekComplete ");
 631                         if (stat & DRQ_STAT)    printk("DataRequest ");
 632                         if (stat & ECC_STAT)    printk("CorrectedError ");
 633                         if (stat & INDEX_STAT)  printk("Index ");
 634                         if (stat & ERR_STAT)    printk("Error ");
 635                 }
 636                 printk("}");
 637         }
 638 #endif  /* FANCY_STATUS_DUMPS */
 639         printk("\n");
 640         if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
 641                 err = GET_ERR(HWIF);
 642                 printk("%s: %s: error=0x%02x", name, msg, err);
 643 #if FANCY_STATUS_DUMPS
 644                 if (dev && dev->type == disk) {
 645                         printk(" { ");
 646                         if (err & BBD_ERR)      printk("BadSector ");
 647                         if (err & ECC_ERR)      printk("UncorrectableError ");
 648                         if (err & ID_ERR)       printk("SectorIdNotFound ");
 649                         if (err & ABRT_ERR)     printk("DriveStatusError ");
 650                         if (err & TRK0_ERR)     printk("TrackZeroNotFound ");
 651                         if (err & MARK_ERR)     printk("AddrMarkNotFound ");
 652                         printk("}");
 653                         if (err & (BBD_ERR|ECC_ERR|ID_ERR|MARK_ERR)) {
 654                                 byte cur = IN_BYTE(HD_CURRENT,HWIF);
 655                                 if (cur & 0x40) {       /* using LBA? */
 656                                         printk(", LBAsect=%ld", (unsigned long)
 657                                          ((cur&0xf)<<24)
 658                                          |(IN_BYTE(HD_HCYL,HWIF)<<16)
 659                                          |(IN_BYTE(HD_LCYL,HWIF)<<8)
 660                                          | IN_BYTE(HD_SECTOR,HWIF));
 661                                 } else {
 662                                         printk(", CHS=%d/%d/%d",
 663                                          (IN_BYTE(HD_HCYL,HWIF)<<8) +
 664                                           IN_BYTE(HD_LCYL,HWIF),
 665                                           cur & 0xf,
 666                                           IN_BYTE(HD_SECTOR,HWIF));
 667                                 }
 668                                 if (ide_cur_rq[HWIF])
 669                                         printk(", sector=%ld", ide_cur_rq[HWIF]->sector);
 670                         }
 671                 }
 672 #endif  /* FANCY_STATUS_DUMPS */
 673                 printk("\n");
 674         }
 675         restore_flags (flags);
 676         return err;
 677 }
 678 
 679 /*
 680  * ide_error() takes action based on the error returned by the controller.
 681  */
 682 #define ERROR_MAX       8       /* Max read/write errors per sector */
 683 #define ERROR_RESET     3       /* Reset controller every 4th retry */
 684 #define ERROR_RECAL     1       /* Recalibrate every 2nd retry */
 685 static void ide_error (ide_dev_t *dev, const char *msg, byte stat)
     /* [previous][next][first][last][top][bottom][index][help] */
 686 {
 687         struct request *rq;
 688         byte err;
 689 
 690         err = dump_status(DEV_HWIF, msg, stat);
 691         if ((rq = ide_cur_rq[DEV_HWIF]) == NULL || dev == NULL)
 692                 return;
 693 #ifdef IDE_DRIVE_CMD
 694         if (rq->cmd == IDE_DRIVE_CMD) { /* never retry an explicit DRIVE_CMD */
 695                 end_drive_cmd(dev, stat, err);
 696                 return;
 697         }
 698 #endif  /* IDE_DRIVE_CMD */
 699         if (stat & BUSY_STAT) {         /* other bits are useless when BUSY */
 700                 rq->errors |= ERROR_RESET;
 701         } else {
 702                 if (dev->type == disk && (stat & ERR_STAT)) {
 703                         /* err has different meaning on cdrom */
 704                         if (err & BBD_ERR)              /* retries won't help this! */
 705                                 rq->errors = ERROR_MAX;
 706                         else if (err & TRK0_ERR)        /* help it find track zero */
 707                                 rq->errors |= ERROR_RECAL;
 708                 }
 709                 if ((stat & DRQ_STAT) && rq->cmd == READ) {
 710                         int i = dev->mult_count ? dev->mult_count<<8 : 1<<8;
 711                         while (i-- > 0)                 /* try to flush data */
 712                                 (void) IN_BYTE(HD_DATA, dev->hwif);
 713                 }
 714         }
 715         if (GET_STAT(dev->hwif) & (BUSY_STAT|DRQ_STAT))
 716                 rq->errors |= ERROR_RESET;      /* Mmmm.. timing problem */
 717 
 718         if (rq->errors >= ERROR_MAX)
 719                 end_request(0, DEV_HWIF);
 720         else {
 721                 if ((rq->errors & ERROR_RESET) == ERROR_RESET)
 722                         do_ide_reset(dev);
 723                 else if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
 724                         dev->special.b.recalibrate = 1;
 725                 ++rq->errors;
 726         }
 727 }
 728 
 729 static void read_intr (ide_dev_t *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 730 {
 731         byte stat;
 732         int i;
 733         unsigned int msect, nsect;
 734         struct request *rq;
 735 
 736         if (!OK_STAT(stat=GET_STAT(DEV_HWIF),DATA_READY,BAD_R_STAT)) {
 737                 sti();
 738                 ide_error(dev, "read_intr", stat);
 739                 DO_REQUEST;
 740                 return;
 741         }
 742         msect = dev->mult_count;
 743 read_next:
 744         rq = ide_cur_rq[DEV_HWIF];
 745         if (msect) {
 746                 if ((nsect = rq->current_nr_sectors) > msect)
 747                         nsect = msect;
 748                 msect -= nsect;
 749         } else
 750                 nsect = 1;
 751         input_ide_data(dev, rq->buffer, nsect * SECTOR_WORDS);
 752 #ifdef DEBUG
 753         printk("%s:  read: sectors(%ld-%ld), buffer=0x%08lx, remaining=%ld\n",
 754                 dev->name, rq->sector, rq->sector+nsect-1,
 755                 (unsigned long) rq->buffer+(nsect<<9), rq->nr_sectors-nsect);
 756 #endif
 757         rq->sector += nsect;
 758         rq->buffer += nsect<<9;
 759         rq->errors = 0;
 760         i = (rq->nr_sectors -= nsect);
 761         if ((rq->current_nr_sectors -= nsect) <= 0)
 762                 end_request(1, DEV_HWIF);
 763         if (i > 0) {
 764                 if (msect)
 765                         goto read_next;
 766                 ide_handler[DEV_HWIF] = &read_intr;
 767                 return;
 768         }
 769         /* (void) GET_STAT(DEV_HWIF); */        /* hd.c did this */
 770         DO_REQUEST;
 771 }
 772 
 773 static void write_intr (ide_dev_t *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 774 {
 775         byte stat;
 776         int i;
 777         struct request *rq = ide_cur_rq[DEV_HWIF];
 778 
 779         if (OK_STAT(stat=GET_STAT(DEV_HWIF),DRIVE_READY,BAD_W_STAT)) {
 780 #ifdef DEBUG
 781                 printk("%s: write: sector %ld, buffer=0x%08lx, remaining=%ld\n",
 782                         dev->name, rq->sector, (unsigned long) rq->buffer,
 783                         rq->nr_sectors-1);
 784 #endif
 785                 if ((rq->nr_sectors == 1) ^ ((stat & DRQ_STAT) != 0)) {
 786                         rq->sector++;
 787                         rq->buffer += 512;
 788                         rq->errors = 0;
 789                         i = --rq->nr_sectors;
 790                         --rq->current_nr_sectors;
 791                         if (rq->current_nr_sectors <= 0)
 792                                 end_request(1, DEV_HWIF);
 793                         if (i > 0) {
 794                                 ide_handler[DEV_HWIF] = &write_intr;
 795                                 output_ide_data(dev, rq->buffer, SECTOR_WORDS);
 796                                 return;
 797                         }
 798                         DO_REQUEST;
 799                         return;
 800                 }
 801         }
 802         sti();
 803         ide_error(dev, "write_intr", stat);
 804         DO_REQUEST;
 805 }
 806 
 807 static void multwrite (ide_dev_t *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 808 {
 809         struct request *rq = &ide_write_rq[DEV_HWIF];
 810         unsigned int mcount = dev->mult_count;
 811 
 812         do {
 813                 unsigned int nsect = rq->current_nr_sectors;
 814                 if (nsect > mcount)
 815                         nsect = mcount;
 816                 mcount -= nsect;
 817 
 818                 output_ide_data(dev, rq->buffer, nsect<<7);
 819 #ifdef DEBUG
 820                 printk("%s: multwrite: sector %ld, buffer=0x%08lx, count=%d, remaining=%ld\n",
 821                         dev->name, rq->sector, (unsigned long) rq->buffer,
 822                         nsect, rq->nr_sectors - nsect);
 823 #endif
 824                 if ((rq->nr_sectors -= nsect) <= 0)
 825                         break;
 826                 if ((rq->current_nr_sectors -= nsect) == 0) {
 827                         if ((rq->bh = rq->bh->b_reqnext) != NULL) {
 828                                 rq->current_nr_sectors = rq->bh->b_size>>9;
 829                                 rq->buffer             = rq->bh->b_data;
 830                         } else {
 831                                 panic("%s: buffer list corrupted\n", dev->name);
 832                                 break;
 833                         }
 834                 } else {
 835                         rq->buffer += nsect << 9;
 836                 }
 837         } while (mcount);
 838 }
 839 
 840 static void multwrite_intr (ide_dev_t *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 841 {
 842         byte stat;
 843         int i;
 844         struct request *rq = &ide_write_rq[DEV_HWIF];
 845 
 846         if (OK_STAT(stat=GET_STAT(DEV_HWIF),DRIVE_READY,BAD_W_STAT)) {
 847                 if (stat & DRQ_STAT) {
 848                         if (rq->nr_sectors) {
 849                                 if (dev->mult_count)
 850                                         multwrite(dev);
 851                                 ide_handler[DEV_HWIF] = &multwrite_intr;
 852                                 return;
 853                         }
 854                 } else {
 855                         if (!rq->nr_sectors) {  /* all done? */
 856                                 rq = ide_cur_rq[DEV_HWIF];
 857                                 for (i = rq->nr_sectors; i > 0;){
 858                                         i -= rq->current_nr_sectors;
 859                                         end_request(1, DEV_HWIF);
 860                                 }
 861                                 DO_REQUEST;
 862                                 return;
 863                         }
 864                 }
 865         }
 866         sti();
 867         ide_error(dev, "multwrite_intr", stat);
 868         DO_REQUEST;
 869 }
 870 
 871 /*
 872  * Issue a simple drive command
 873  * The drive must be selected beforehand.
 874  */
 875 static inline void ide_cmd(ide_dev_t *dev, byte cmd, byte nsect,
     /* [previous][next][first][last][top][bottom][index][help] */
 876                                 void (*handler)(ide_dev_t *dev))
 877 {
 878         OUT_BYTE(dev->ctl,HD_CMD);
 879         OUT_BYTE(nsect,HD_NSECTOR);
 880         OUT_BYTE(cmd,HD_COMMAND);
 881         ide_handler[DEV_HWIF] = handler;
 882 }
 883 
 884 static void set_multmode_intr (ide_dev_t *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 885 {
 886         byte stat = GET_STAT(DEV_HWIF);
 887 
 888         sti();
 889         if (!OK_STAT(stat,READY_STAT,BAD_STAT)) {
 890                 dev->mult_req = dev->mult_count = 0;
 891                 dev->special.b.recalibrate = 1;
 892                 (void) dump_status(DEV_HWIF, "set_multmode", stat);
 893         } else {
 894                 if ((dev->mult_count = dev->mult_req))
 895                         printk ("  %s: enabled %d-sector multiple mode\n",
 896                                 dev->name, dev->mult_count);
 897                 else
 898                         printk ("  %s: multiple mode turned off\n", dev->name);
 899         }
 900         DO_REQUEST;
 901 }
 902 
 903 static void set_geometry_intr (ide_dev_t *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 904 {
 905         byte stat = GET_STAT(DEV_HWIF);
 906 
 907         sti();
 908         if (!OK_STAT(stat,READY_STAT,BAD_STAT))
 909                 ide_error(dev, "set_geometry_intr", stat);
 910         DO_REQUEST;
 911 }
 912 
 913 static void recal_intr (ide_dev_t *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 914 {
 915         byte stat = GET_STAT(DEV_HWIF);
 916 
 917         sti();
 918         if (!OK_STAT(stat,READY_STAT,BAD_STAT))
 919                 ide_error(dev, "recal_intr", stat);
 920         DO_REQUEST;
 921 }
 922 
 923 static void drive_cmd_intr (ide_dev_t *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 924 {
 925         byte stat = GET_STAT(DEV_HWIF);
 926 
 927         sti();
 928         if (!OK_STAT(stat,READY_STAT,BAD_STAT))
 929                 ide_error(dev, "drive_cmd", stat); /* calls end_drive_cmd() */
 930         else
 931                 end_drive_cmd (dev, stat, GET_ERR(DEV_HWIF));
 932         DO_REQUEST;
 933 }
 934 
 935 static void timer_expiry (byte hwif)
     /* [previous][next][first][last][top][bottom][index][help] */
 936 {
 937         unsigned long flags;
 938 
 939         save_flags(flags);
 940         cli();
 941 
 942         if (ide_handler[HWIF] == NULL || (timer_active & ide_timerbit[HWIF])) {
 943                 /* The drive must have responded just as the timer expired */
 944                 sti();
 945                 printk("%s: marginal timeout\n", ide_name[HWIF]);
 946         } else {
 947                 ide_handler[HWIF] = NULL;
 948                 disable_irq(ide_irq[HWIF]);
 949 #if SUPPORT_SERIALIZE
 950                 if (single_threaded && ide_irq[HWIF] != ide_irq[HWIF^1])
 951                         disable_irq(ide_irq[HWIF^1]);
 952 #endif /* SUPPORT_SERIALIZE */
 953                 sti();
 954                 ide_error(ide_cur_dev[HWIF], "timeout", GET_STAT(HWIF));
 955                 do_request(HWIF);
 956 #if SUPPORT_SHARING_IRQ
 957                 if (single_threaded)    /* this line is indeed necessary */
 958                         hwif = current_hwif;
 959 #endif /* SUPPORT_SHARING_IRQ */
 960                 cli();
 961                 start_ide_timer(HWIF);
 962                 enable_irq(ide_irq[HWIF]);
 963 #if SUPPORT_SERIALIZE
 964                 if (single_threaded && ide_irq[HWIF] != ide_irq[HWIF^1])
 965                         enable_irq(ide_irq[HWIF^1]);
 966 #endif /* SUPPORT_SERIALIZE */
 967         }
 968         restore_flags(flags);
 969 }
 970 
 971 static void ide0_timer_expiry (void)            /* invoked from sched.c */
     /* [previous][next][first][last][top][bottom][index][help] */
 972 {
 973         timer_expiry (0);
 974 }
 975 
 976 static void ide1_timer_expiry (void)            /* invoked from sched.c */
     /* [previous][next][first][last][top][bottom][index][help] */
 977 {
 978         timer_expiry (1);
 979 }
 980 
 981 static int do_special (ide_dev_t *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 982 {
 983         special_t *s = &dev->special;
 984 #ifdef DEBUG
 985         printk("%s: do_special: 0x%02x\n", dev->name, s->all);
 986 #endif
 987         if (s->b.set_geometry) {
 988                 s->b.set_geometry = 0;
 989                 if (dev->type == disk) {
 990                         OUT_BYTE(dev->sect,HD_SECTOR);
 991                         OUT_BYTE(dev->cyl,HD_LCYL);
 992                         OUT_BYTE(dev->cyl>>8,HD_HCYL);
 993                         OUT_BYTE(((dev->head-1)|dev->select.all)&0xBF,HD_CURRENT);
 994                         ide_cmd(dev, WIN_SPECIFY, dev->sect, &set_geometry_intr);
 995                 }
 996         } else if (s->b.recalibrate) {
 997                 s->b.recalibrate = 0;
 998                 if (dev->type == disk)
 999                         ide_cmd(dev,WIN_RESTORE,dev->sect,&recal_intr);
1000         } else if (s->b.set_multmode) {
1001                 if (dev->type == disk) {
1002                         if (dev->id && dev->mult_req > dev->id->max_multsect)
1003                                 dev->mult_req = dev->id->max_multsect;
1004                         ide_cmd(dev,WIN_SETMULT,dev->mult_req,&set_multmode_intr);
1005                 } else {
1006                         dev->mult_req = 0;
1007                         printk("%s: multmode not supported by this device\n", dev->name);
1008                 }
1009                 s->b.set_multmode = 0;
1010         } else {
1011                 if (s->all) {
1012                         printk("%s: bad special flag: 0x%02x\n", dev->name, s->all);
1013                         s->all = 0;
1014                 }
1015         }
1016         return (ide_handler[DEV_HWIF] == NULL) ? 1 : 0;
1017 }
1018 
1019 #ifdef CONFIG_BLK_DEV_IDECD
1020 static byte wait_stat (ide_dev_t *dev, byte good, byte bad, unsigned long timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
1021 {
1022         unsigned long flags;
1023 
1024         save_flags(flags);
1025         sti();
1026         WAIT_STAT(dev, good, bad, timeout, "status", error);
1027         restore_flags(flags);
1028         return 0;
1029 error:
1030         restore_flags(flags);
1031         return 1;
1032 }
1033 
1034 #include "ide-cd.c"
1035 #endif  /* CONFIG_BLK_DEV_IDECD */
1036 
1037 static inline int do_rw_disk (ide_dev_t *dev, struct request *rq, unsigned long block)
     /* [previous][next][first][last][top][bottom][index][help] */
1038 {
1039         OUT_BYTE(dev->ctl,HD_CMD);
1040         OUT_BYTE(rq->nr_sectors,HD_NSECTOR);
1041         if (dev->select.b.lba) {
1042 #ifdef DEBUG
1043                 printk("%s: %sing: LBAsect=%ld, sectors=%ld, buffer=0x%08lx\n",
1044                         dev->name, (rq->cmd==READ)?"read":"writ", 
1045                         block, rq->nr_sectors, (unsigned long) rq->buffer);
1046 #endif
1047                 OUT_BYTE(block,HD_SECTOR);
1048                 OUT_BYTE(block>>=8,HD_LCYL);
1049                 OUT_BYTE(block>>=8,HD_HCYL);
1050                 OUT_BYTE(((block>>8)&0x0f)|dev->select.all,HD_CURRENT);
1051         } else {
1052                 unsigned int sect,head,cyl,track;
1053                 track = block / dev->sect;
1054                 sect  = block % dev->sect + 1;
1055                 OUT_BYTE(sect,HD_SECTOR);
1056                 head  = track % dev->head;
1057                 cyl   = track / dev->head;
1058                 OUT_BYTE(cyl,HD_LCYL);
1059                 OUT_BYTE(cyl>>8,HD_HCYL);
1060                 OUT_BYTE(head|dev->select.all,HD_CURRENT);
1061 #ifdef DEBUG
1062                 printk("%s: %sing: CHS=%d/%d/%d, sectors=%ld, buffer=0x%08lx\n",
1063                         dev->name, (rq->cmd==READ)?"read":"writ", cyl,
1064                         head, sect, rq->nr_sectors, (unsigned long) rq->buffer);
1065 #endif
1066         }
1067         if (rq->cmd == READ) {
1068                 OUT_BYTE(dev->mult_count ? WIN_MULTREAD : WIN_READ, HD_COMMAND);
1069                 ide_handler[DEV_HWIF] = &read_intr;
1070                 return 0;
1071         }
1072         if (rq->cmd == WRITE) {
1073                 OUT_BYTE(dev->wpcom,HD_PRECOMP);        /* for ancient drives */
1074                 OUT_BYTE(dev->mult_count ? WIN_MULTWRITE : WIN_WRITE, HD_COMMAND);
1075                 WAIT_STAT(dev, DATA_READY, BAD_W_STAT, WAIT_DRQ, "DRQ", error);
1076                 if (!dev->unmask)
1077                         cli();
1078                 if (dev->mult_count) {
1079                         ide_write_rq[DEV_HWIF] = *rq; /* scratchpad */
1080                         multwrite(dev);
1081                         ide_handler[DEV_HWIF] = &multwrite_intr;
1082                 } else {
1083                         output_ide_data(dev, rq->buffer, SECTOR_WORDS);
1084                         ide_handler[DEV_HWIF] = &write_intr;
1085                 }
1086                 return 0;
1087         }
1088 #ifdef IDE_DRIVE_CMD
1089         if (rq->cmd == IDE_DRIVE_CMD) {
1090                 byte *args = rq->buffer;
1091                 if (args) {
1092                         OUT_BYTE(args[2],HD_FEATURE);
1093                         ide_cmd(dev, args[0], args[1], &drive_cmd_intr);
1094                         printk("%s: DRIVE_CMD cmd=0x%02x sc=0x%02x fr=0x%02x\n",
1095                          dev->name, args[0], args[1], args[2]);
1096                         return 0;
1097                 } else {
1098 #ifdef DEBUG
1099                         printk("%s: DRIVE_CMD (null)\n", dev->name);
1100 #endif
1101                         end_drive_cmd(dev,GET_STAT(DEV_HWIF),GET_ERR(DEV_HWIF));
1102                         return 1;
1103                 }
1104         }
1105 #endif  /* IDE_DRIVE_CMD */
1106         printk("%s: bad command: %d\n", dev->name, rq->cmd);
1107         end_request(0, DEV_HWIF);
1108 error:
1109         return 1;
1110 }
1111 
1112 /*
1113  * The driver enables interrupts as much as possible.  In order to do this,
1114  * (a) the device-interrupt is always masked before entry, and
1115  * (b) the timeout-interrupt is always disabled before entry.
1116  *
1117  * Interrupts are still masked (by default) whenever we are exchanging
1118  * data/cmds with a drive, because some drives seem to have very poor
1119  * tolerance for latency during I/O.  For devices which don't suffer from
1120  * this problem (most don't), the ide_dev[][].unmask flag can be set to permit
1121  * other interrupts during data/cmd transfers by using the "hdparm" utility.
1122  */
1123 static void do_request (byte hwif)
     /* [previous][next][first][last][top][bottom][index][help] */
1124 {
1125         unsigned int minor, drive;
1126         unsigned long block, blockend;
1127         struct request *rq;
1128         ide_dev_t *dev;
1129 repeat:
1130         sti();
1131 #if SUPPORT_SHARING_IRQ
1132         current_hwif = hwif;    /* used *only* when single_threaded==1 */
1133 #endif /* SUPPORT_SHARING_IRQ */
1134         if ((rq = ide_cur_rq[HWIF]) == NULL) {
1135                 rq = blk_dev[ide_major[HWIF]].current_request;
1136                 if ((rq == NULL) || (rq->dev < 0)) {
1137 #if SUPPORT_SHARING_IRQ
1138                         if (single_threaded) {
1139                                 if (sharing_single_irq && (dev = ide_cur_dev[hwif])) /* disable irq */
1140                                         OUT_BYTE(dev->ctl|2,HD_CMD);
1141                                 rq = blk_dev[ide_major[hwif^=1]].current_request;
1142                                 if ((rq != NULL) && (rq->dev >= 0))
1143                                         goto repeat;
1144                         }
1145 #endif /* SUPPORT_SHARING_IRQ */
1146                         return;
1147                 }
1148                 blk_dev[ide_major[HWIF]].current_request = rq->next;
1149                 ide_cur_rq[HWIF] = rq;
1150         }
1151 #ifdef DEBUG
1152         printk("%s: do_request: current=0x%08lx\n",ide_name[HWIF],(unsigned long)rq);
1153 #endif
1154         minor = MINOR(rq->dev);
1155         drive = minor >> PARTN_BITS;
1156         ide_cur_dev[HWIF] = dev = &ide_dev[HWIF][drive];
1157         if ((MAJOR(rq->dev) != ide_major[HWIF]) || (drive >= MAX_DRIVES)) {
1158                 printk("%s: bad device number: 0x%04x\n", ide_name[HWIF], rq->dev);
1159                 end_request(0, HWIF);
1160                 goto repeat;
1161         }
1162         if (rq->bh && !rq->bh->b_lock) {
1163                 printk("%s: block not locked\n", ide_name[HWIF]);
1164                 end_request(0, HWIF);
1165                 goto repeat;
1166         }
1167         block    = rq->sector;
1168         blockend = block + rq->nr_sectors;
1169         if ((blockend < block) || (blockend > ide_hd[HWIF][minor].nr_sects)) {
1170                 printk("%s: bad access: block=%ld, count=%ld\n",
1171                         dev->name, block, rq->nr_sectors);
1172                 end_request(0, HWIF);
1173                 goto repeat;
1174         }
1175         block += ide_hd[HWIF][minor].start_sect;
1176 #if (DISK_RECOVERY_TIME > 0)
1177         while ((read_timer() - ide_lastreq[HWIF]) < DISK_RECOVERY_TIME);
1178 #endif
1179         OUT_BYTE(dev->select.all,HD_CURRENT);
1180 #ifdef CONFIG_BLK_DEV_IDECD
1181         WAIT_STAT(dev, (dev->type == cdrom) ? 0 : READY_STAT,
1182                 BUSY_STAT|DRQ_STAT, WAIT_READY, "DRDY", repeat);
1183 #else
1184         WAIT_STAT(dev, READY_STAT, BUSY_STAT|DRQ_STAT, WAIT_READY, "DRDY", repeat);
1185 #endif  /* CONFIG_BLK_DEV_IDECD */
1186         if (!dev->special.all) {
1187 #ifdef CONFIG_BLK_DEV_IDECD
1188                 if (dev->type == disk) {
1189 #endif  /* CONFIG_BLK_DEV_IDECD */
1190                         if (do_rw_disk(dev, rq, block))
1191                                 goto repeat;
1192 #ifdef CONFIG_BLK_DEV_IDECD
1193                 } else {
1194                         if (do_rw_cdrom(dev, block))
1195                                 goto repeat;
1196                 }
1197 #endif  /* CONFIG_BLK_DEV_IDECD */
1198         } else {
1199                 if (do_special(dev))
1200                         goto repeat;
1201         }
1202 }
1203 
1204 /*
1205  * This is a macro rather than an inline function to
1206  * prevent gcc from over-optimizing accesses to current_hwif,
1207  * which may have a different value on exit from do_request().
1208  */
1209 #define DO_IDE_REQUEST(hwif)                    \
1210 {                                               \
1211         if (ide_handler[hwif] == NULL) {        \
1212                 disable_irq(ide_irq[hwif]);     \
1213                 if (single_threaded && ide_irq[hwif] != ide_irq[hwif^1]) \
1214                         disable_irq(ide_irq[hwif^1]); \
1215                 do_request(hwif);               \
1216                 cli();                          \
1217                 start_ide_timer(hwif);          \
1218                 enable_irq(ide_irq[hwif]);      \
1219                 if (single_threaded && ide_irq[hwif] != ide_irq[hwif^1]) \
1220                         enable_irq(ide_irq[hwif^1]); \
1221         }                                       \
1222 }
1223 
1224 #if SUPPORT_TWO_INTERFACES
1225 static void do_ide0_request (void)      /* invoked with cli() */
     /* [previous][next][first][last][top][bottom][index][help] */
1226 {
1227         DO_IDE_REQUEST(0);
1228 }
1229 
1230 static void do_ide1_request (void)      /* invoked with cli() */
     /* [previous][next][first][last][top][bottom][index][help] */
1231 {
1232         DO_IDE_REQUEST(1);
1233 }
1234 #else
1235 #define do_ide1_request do_ide0_request
1236 static void do_ide0_request (void)      /* invoked with cli() */
     /* [previous][next][first][last][top][bottom][index][help] */
1237 {
1238         DO_IDE_REQUEST(HWIF);
1239 }
1240 #endif  /* SUPPORT_TWO_INTERFACES */
1241 
1242 #if SUPPORT_SHARING_IRQ
1243 static void do_shared_request (void)    /* invoked with cli() */
     /* [previous][next][first][last][top][bottom][index][help] */
1244 {
1245         DO_IDE_REQUEST(current_hwif);
1246 }
1247 #endif /* SUPPORT_SHARING_IRQ */
1248 
1249 /*
1250  * There's nothing really useful we can do with an unexpected interrupt,
1251  * other than reading the status register (to clear it), and logging it.
1252  * There should be no way that an irq can happen before we're ready for it,
1253  * so we needn't worry much about losing an "important" interrupt here.
1254  *
1255  * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
1256  * drive enters "idle", "standby", or "sleep" mode, so if the status looks
1257  * "good", we just ignore the interrupt completely.
1258  */
1259 static void unexpected_intr (byte hwif)
     /* [previous][next][first][last][top][bottom][index][help] */
1260 {
1261         byte stat;
1262 
1263         if (!OK_STAT(stat=GET_STAT(HWIF), DRIVE_READY, BAD_STAT))
1264                 (void) dump_status(HWIF, "unexpected_intr", stat);
1265         outb_p(2,IDE_PORT(HD_CMD,hwif));        /* disable device irq */
1266 #if SUPPORT_SHARING_IRQ
1267         if (single_threaded && ide_irq[hwif] == ide_irq[hwif^1]) {
1268                 if (!OK_STAT(stat=GET_STAT(hwif^1), DRIVE_READY, BAD_STAT))
1269                         (void) dump_status(hwif^1, "unexpected_intr", stat);
1270                 outb_p(2,IDE_PORT(HD_CMD,hwif^1));      /* disable device irq */
1271         }
1272 #endif /* SUPPORT_SHARING_IRQ */
1273 }
1274 
1275 /*
1276  * This is a macro rather than an inline function to
1277  * prevent gcc from over-optimizing accesses to current_hwif,
1278  * which may have a different value on exit from handler().
1279  */
1280 #define IDE_INTR(hwif)                                  \
1281 {                                                       \
1282         ide_dev_t *dev;                                 \
1283         void (*handler)(ide_dev_t *);                   \
1284                                                         \
1285         timer_active &= ~ide_timerbit[hwif];            \
1286         if ((handler = ide_handler[hwif]) != NULL) {    \
1287                 ide_handler[hwif] = NULL;               \
1288                 dev = ide_cur_dev[hwif];                \
1289                 if (dev->unmask)                        \
1290                         sti();                          \
1291                 handler(dev);                           \
1292         } else                                          \
1293                 unexpected_intr(hwif);                  \
1294         cli();                                          \
1295 }
1296 
1297 #if SUPPORT_SERIALIZE
1298 /* entry point for all interrupts when single_threaded==1 */
1299 static void ide_seq_intr (int irq, struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
1300 {
1301         byte hwif = (irq != ide_irq[0]);
1302         IDE_INTR(HWIF);
1303         start_ide_timer(current_hwif);
1304 }
1305 #endif /* SUPPORT_SERIALIZE */
1306 
1307 #if OPTIMIZE_IRQS
1308 
1309 /* entry point for all interrupts on ide0 when single_threaded==0 */
1310 static void ide0_intr (int irq, struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
1311 {
1312         IDE_INTR(0);
1313         start_ide_timer(0);
1314 }
1315 
1316 /* entry point for all interrupts on ide1 when single_threaded==0 */
1317 static void ide1_intr (int irq, struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
1318 {
1319         IDE_INTR(1);
1320         start_ide_timer(1);
1321 }
1322 
1323 #else   /* OPTIMIZE_IRQS */
1324 
1325 #define ide0_intr       ide_intr
1326 #define ide1_intr       ide_intr
1327 
1328 /* entry point for all interrupts when single_threaded==0 */
1329 static void ide_intr (int irq, struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
1330 {
1331 #if SUPPORT_TWO_INTERFACES
1332         byte hwif = (irq != ide_irq[0]);
1333 #endif  /* SUPPORT_TWO_INTERFACES */
1334         IDE_INTR(HWIF);
1335         start_ide_timer(HWIF);
1336 }
1337 
1338 #endif  /* OPTIMIZE_IRQS */
1339 
1340 #if SUPPORT_SHARING_IRQ
1341 /* entry point for all interrupts on ide0/ide1 when sharing_single_irq==1 */
1342 static void ide_shared_intr (int irq, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
1343 {
1344         IDE_INTR(current_hwif);
1345         start_ide_timer(current_hwif);
1346 }
1347 #endif /* SUPPORT_SHARING_IRQ */
1348 
1349 static ide_dev_t *get_info_ptr (int i_rdev)
     /* [previous][next][first][last][top][bottom][index][help] */
1350 {
1351         unsigned int drive = DEVICE_NR(i_rdev);
1352         ide_dev_t *dev;
1353 
1354         if (drive < MAX_DRIVES) {
1355                 switch (MAJOR(i_rdev)) {
1356                         case IDE0_MAJOR:        dev = &ide_dev[0][drive];
1357                                                 if (dev->present) return dev;
1358                                                 break;
1359                         case IDE1_MAJOR:        dev = &ide_dev[1][drive];
1360                                                 if (dev->present) return dev;
1361                                                 break;
1362                 }
1363         }
1364         return NULL;
1365 }
1366 
1367 static int ide_open(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
1368 {
1369         ide_dev_t *dev;
1370         unsigned long flags;
1371 
1372         if ((dev = get_info_ptr(inode->i_rdev)) == NULL)
1373                 return -ENODEV;
1374         save_flags(flags);
1375         cli();
1376         while (dev->busy)
1377                 sleep_on(&dev->wqueue);
1378         dev->usage++;
1379         restore_flags(flags);
1380 #ifdef CONFIG_BLK_DEV_IDECD
1381         if (dev->type == cdrom)
1382                 return cdrom_open (inode, filp, dev);
1383 #endif  /* CONFIG_BLK_DEV_IDECD */
1384         return 0;
1385 }
1386 
1387 /*
1388  * Releasing a block device means we sync() it, so that it can safely
1389  * be forgotten about...
1390  */
1391 static void ide_release(struct inode * inode, struct file * file)
     /* [previous][next][first][last][top][bottom][index][help] */
1392 {
1393         ide_dev_t *dev;
1394 
1395         if ((dev = get_info_ptr(inode->i_rdev)) != NULL) {
1396                 sync_dev(inode->i_rdev);
1397                 dev->usage--;
1398 #ifdef CONFIG_BLK_DEV_IDECD
1399                 if (dev->type == cdrom)
1400                         cdrom_release (inode, file, dev);
1401 #endif  /* CONFIG_BLK_DEV_IDECD */
1402         }
1403 }
1404 
1405 /*
1406  * This routine is called to flush all partitions and partition tables
1407  * for a changed disk, and then re-read the new partition table.
1408  * If we are revalidating a disk because of a media change, then we
1409  * enter with usage == 0.  If we are using an ioctl, we automatically have
1410  * usage == 1 (we need an open channel to use an ioctl :-), so this
1411  * is our limit.
1412  */
1413 static int revalidate_disk(int i_rdev)
     /* [previous][next][first][last][top][bottom][index][help] */
1414 {
1415         unsigned int i, major, start, drive = DEVICE_NR(i_rdev);
1416         ide_dev_t *dev;
1417         struct gendisk *gd;
1418         long flags;
1419 
1420         if ((dev = get_info_ptr(i_rdev)) == NULL)
1421                 return -ENODEV;
1422 
1423         save_flags(flags);
1424         cli();
1425         if (dev->busy || (dev->usage > 1)) {
1426                 restore_flags(flags);
1427                 return -EBUSY;
1428         };
1429         dev->busy = 1;
1430         restore_flags(flags);
1431 
1432         gd    = &ide_gendisk[DEV_HWIF];
1433         major = ide_major[DEV_HWIF] << 8;
1434         start = drive << PARTN_BITS;
1435 
1436         for (i = 0; i < (1<<PARTN_BITS); i++) {
1437                 unsigned int minor = start + i;
1438                 sync_dev           (major | minor);
1439                 invalidate_inodes  (major | minor);
1440                 invalidate_buffers (major | minor);
1441                 gd->part[minor].start_sect = 0;
1442                 gd->part[minor].nr_sects   = 0;
1443         };
1444 
1445         gd->part[start].nr_sects = ide_capacity[DEV_HWIF][drive];
1446         resetup_one_dev(gd, drive);
1447 
1448         dev->busy = 0;
1449         wake_up(&dev->wqueue);
1450         return 0;
1451 }
1452 
1453 #ifdef IDE_DRIVE_CMD
1454 /*
1455  * This function issues a specific IDE drive command onto the
1456  * tail of the request queue, and waits for it to be completed.
1457  * If arg is NULL, it goes through all the motions,
1458  * but without actually sending a command to the drive.
1459  */
1460 static int do_drive_cmd(int dev, char *args)
     /* [previous][next][first][last][top][bottom][index][help] */
1461 {
1462         unsigned long flags;
1463         unsigned int major = MAJOR(dev);
1464         struct request rq, *cur_rq;
1465         struct blk_dev_struct *bdev;
1466         struct semaphore sem = MUTEX_LOCKED;
1467 
1468         /* build up a special request, and add it to the queue */
1469         rq.buffer = args;
1470         rq.cmd = IDE_DRIVE_CMD;
1471         rq.errors = 0;
1472         rq.sector = 0;
1473         rq.nr_sectors = 0;
1474         rq.current_nr_sectors = 0;
1475         rq.sem = &sem;
1476         rq.bh = NULL;
1477         rq.bhtail = NULL;
1478         rq.next = NULL;
1479         rq.dev = dev;
1480         bdev = &blk_dev[major];
1481 
1482         save_flags(flags);
1483         cli();
1484         cur_rq = bdev->current_request;
1485         if (cur_rq == NULL) {                   /* empty request list? */
1486                 bdev->current_request = &rq;    /* service ours immediately */
1487                 bdev->request_fn();
1488         } else {
1489                 while (cur_rq->next != NULL)    /* find end of request list */
1490                         cur_rq = cur_rq->next;
1491                 cur_rq->next = &rq;             /* add rq to the end */
1492         }
1493 
1494         down(&sem);                             /* wait for it to be serviced */
1495         restore_flags(flags);
1496         return rq.errors ? -EIO : 0;            /* return -EIO if errors */
1497 }
1498 #endif  /* IDE_DRIVE_CMD */
1499 
1500 static int write_fs_long (unsigned long useraddr, long value)
     /* [previous][next][first][last][top][bottom][index][help] */
1501 {
1502         int err;
1503 
1504         if (NULL == (long *)useraddr)
1505                 return -EINVAL;
1506         if ((err = verify_area(VERIFY_WRITE, (long *)useraddr, sizeof(long))))
1507                 return err;
1508         put_fs_long((unsigned)value, (long *) useraddr);
1509         return 0;
1510 }
1511 
1512 static int ide_ioctl (struct inode *inode, struct file *file,
     /* [previous][next][first][last][top][bottom][index][help] */
1513                         unsigned int cmd, unsigned long arg)
1514 {
1515         struct hd_geometry *loc = (struct hd_geometry *) arg;
1516         int err;
1517         ide_dev_t *dev;
1518         unsigned long flags;
1519 
1520         if (!inode || !inode->i_rdev)
1521                 return -EINVAL;
1522         if ((dev = get_info_ptr(inode->i_rdev)) == NULL)
1523                 return -ENODEV;
1524         switch (cmd) {
1525                 case HDIO_GETGEO:
1526                         if (!loc || dev->type != disk) return -EINVAL;
1527                         err = verify_area(VERIFY_WRITE, loc, sizeof(*loc));
1528                         if (err) return err;
1529                         put_fs_byte(dev->bios_head,
1530                                 (char *) &loc->heads);
1531                         put_fs_byte(dev->bios_sect,
1532                                 (char *) &loc->sectors);
1533                         put_fs_word(dev->bios_cyl,
1534                                 (short *) &loc->cylinders);
1535                         put_fs_long((unsigned)ide_hd[DEV_HWIF][MINOR(inode->i_rdev)].start_sect,
1536                                 (long *) &loc->start);
1537                         return 0;
1538 
1539                 case BLKFLSBUF:
1540                         if(!suser()) return -EACCES;
1541                         fsync_dev(inode->i_rdev);
1542                         invalidate_buffers(inode->i_rdev);
1543                         return 0;
1544 
1545                 case BLKRASET:
1546                         if(!suser()) return -EACCES;
1547                         if(arg > 0xff) return -EINVAL;
1548                         read_ahead[MAJOR(inode->i_rdev)] = arg;
1549                         return 0;
1550 
1551                 case BLKRAGET:
1552                         return write_fs_long(arg, read_ahead[MAJOR(inode->i_rdev)]);
1553 
1554                 case BLKGETSIZE:   /* Return device size */
1555                         return write_fs_long(arg, ide_hd[DEV_HWIF][MINOR(inode->i_rdev)].nr_sects);
1556                 case BLKRRPART: /* Re-read partition tables */
1557                         return revalidate_disk(inode->i_rdev);
1558 
1559                 case HDIO_GET_KEEPSETTINGS:
1560                         return write_fs_long(arg, dev->keep_settings);
1561 
1562                 case HDIO_GET_UNMASKINTR:
1563                         return write_fs_long(arg, dev->unmask);
1564 
1565                 case HDIO_GET_CHIPSET:
1566                         return write_fs_long(arg, dev->chipset);
1567 
1568                 case HDIO_GET_MULTCOUNT:
1569                         return write_fs_long(arg, dev->mult_count);
1570 
1571                 case HDIO_GET_IDENTITY:
1572                         if (!arg || (MINOR(inode->i_rdev) & PARTN_MASK))
1573                                 return -EINVAL;
1574                         if (dev->id == NULL)
1575                                 return -ENOMSG;
1576                         err = verify_area(VERIFY_WRITE, (char *)arg, sizeof(*dev->id));
1577                         if (err) return err;
1578                         memcpy_tofs((char *)arg, (char *)dev->id, sizeof(*dev->id));
1579                         return 0;
1580 
1581                 case HDIO_SET_KEEPSETTINGS:
1582                 case HDIO_SET_UNMASKINTR:
1583                         if (!suser()) return -EACCES;
1584                         if ((arg > 1) || (MINOR(inode->i_rdev) & PARTN_MASK))
1585                                 return -EINVAL;
1586                         save_flags(flags);
1587                         cli();
1588                         if (cmd == HDIO_SET_KEEPSETTINGS)
1589                                 dev->keep_settings = arg;
1590                         else
1591                                 dev->unmask = arg;
1592                         restore_flags(flags);
1593                         return 0;
1594 
1595                 case HDIO_SET_CHIPSET:
1596                         if (!suser()) return -EACCES;
1597                         if ((arg > 3) || (MINOR(inode->i_rdev) & PARTN_MASK))
1598                                 return -EINVAL;
1599                         save_flags(flags);
1600                         cli();
1601                         dev->chipset   = arg;
1602                         dev->vlb_sync  = (arg & 2) >> 1;
1603                         dev->vlb_32bit = (arg & 1);
1604                         restore_flags(flags);
1605                         return 0;
1606 
1607                 case HDIO_SET_MULTCOUNT:
1608                         if (!suser()) return -EACCES;
1609                         if (MINOR(inode->i_rdev) & PARTN_MASK)
1610                                 return -EINVAL;
1611                         if ((dev->id != NULL) && (arg > dev->id->max_multsect))
1612                                 return -EINVAL;
1613                         save_flags(flags);
1614                         cli();
1615                         if (dev->special.b.set_multmode) {
1616                                 restore_flags(flags);
1617                                 return -EBUSY;
1618                         }
1619                         dev->mult_req = arg;
1620                         dev->special.b.set_multmode = 1;
1621                         restore_flags(flags);
1622 #ifdef IDE_DRIVE_CMD
1623                         do_drive_cmd (inode->i_rdev, NULL);
1624                         return (dev->mult_count == arg) ? 0 : -EIO;
1625 #else
1626                         return 0;
1627 #endif  /* IDE_DRIVE_CMD */
1628 
1629 #ifdef IDE_DRIVE_CMD
1630                 case HDIO_DRIVE_CMD:
1631                 {
1632                         unsigned long args;
1633 
1634                         if (NULL == (long *) arg)
1635                                 err = do_drive_cmd(inode->i_rdev,NULL);
1636                         else {
1637                                 if (!(err = verify_area(VERIFY_WRITE,(long *)arg,sizeof(long))))
1638                                 {
1639                                         args = get_fs_long((long *)arg);
1640                                         err = do_drive_cmd(inode->i_rdev,(char *)&args);
1641                                         put_fs_long(args,(long *)arg);
1642                                 }
1643                         }
1644                         return err;
1645                 }
1646 #endif /* IDE_DRIVE_CMD */
1647 
1648                 RO_IOCTLS(inode->i_rdev, arg);
1649 
1650                 default:
1651 #ifdef CONFIG_BLK_DEV_IDECD
1652                         if (dev->type == cdrom)
1653                                 return ide_cdrom_ioctl(dev, inode, file, cmd, arg);
1654 #endif /* CONFIG_BLK_DEV_IDECD */
1655                         return -EPERM;
1656         }
1657 }
1658 
1659 #ifdef CONFIG_BLK_DEV_IDECD
1660 static int ide_check_media_change (dev_t full_dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1661 {
1662         ide_dev_t *dev;
1663 
1664         if ((dev = get_info_ptr(full_dev)) == NULL)
1665                 return -ENODEV;
1666         if (dev->type != cdrom)
1667                 return 0;
1668         return cdrom_check_media_change (dev);
1669 }
1670 #endif  /* CONFIG_BLK_DEV_IDECD */
1671 
1672 
1673 static void fixstring (byte *s, int bytecount, int byteswap)
     /* [previous][next][first][last][top][bottom][index][help] */
1674 {
1675         byte *p, *end = &s[bytecount &= ~1];    /* bytecount must be even */
1676 
1677         if (byteswap) {
1678                 /* convert from big-endian to little-endian */
1679                 for (p = end ; p != s;) {
1680                         unsigned short *pp = (unsigned short *) (p -= 2);
1681                         *pp = (*pp >> 8) | (*pp << 8);
1682                 }
1683         }
1684         p = s;
1685 
1686         /* strip leading blanks */
1687         while (s != end && *s == ' ')
1688                 ++s;
1689 
1690         /* compress internal blanks and strip trailing blanks */
1691         while (s != end && *s) {
1692                 if (*s++ != ' ' || (s != end && *s && *s != ' '))
1693                         *p++ = *(s-1);
1694         }
1695 
1696         /* wipe out trailing garbage */
1697         while (p != end)
1698                 *p++ = '\0';
1699 }
1700 
1701 static int lba_capacity_is_ok (struct hd_driveid *id)
     /* [previous][next][first][last][top][bottom][index][help] */
1702 /*
1703  * Returns:     1 if lba_capacity looks sensible
1704  *              0 otherwise
1705  */
1706 {
1707         unsigned long lba_sects   = id->lba_capacity;
1708         unsigned long chs_sects   = id->cyls * id->heads * id->sectors;
1709         unsigned long _10_percent = chs_sects / 10;
1710 
1711         /* perform a rough sanity check on lba_sects:  within 10% is "okay" */
1712         if ((lba_sects - chs_sects) < _10_percent)
1713                 return 1;       /* lba_capacity is good */
1714 
1715         /* some drives have the word order reversed */
1716         lba_sects = (lba_sects << 16) | (lba_sects >> 16);
1717         if ((lba_sects - chs_sects) < _10_percent) {
1718                 id->lba_capacity = lba_sects;   /* fix it */
1719                 return 1;       /* lba_capacity is (now) good */
1720         }
1721         return 0;       /* lba_capacity value is bad */
1722 }
1723 
1724 static unsigned long probe_mem_start;   /* used by drive/irq probing routines */
1725 
1726 static void do_identify (ide_dev_t *dev, byte cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
1727 {
1728         int bswap;
1729         struct hd_driveid *id;
1730         unsigned long capacity, check;
1731 
1732         id = dev->id = (struct hd_driveid *) probe_mem_start; /* kmalloc() */
1733         probe_mem_start += 512;
1734         input_ide_data(dev, id, SECTOR_WORDS);  /* read 512 bytes of id info */
1735         sti();
1736 
1737         /*
1738          * EATA SCSI controllers do a hardware ATA emulation:  ignore them
1739          */
1740         if ((id->model[0] == 'P' && id->model[1] == 'M')
1741          || (id->model[0] == 'S' && id->model[1] == 'K')) {
1742                 printk("%s: EATA SCSI HBA %.10s\n", dev->name, id->model);
1743                 dev->present = 0;
1744                 return;
1745         }
1746 
1747         /*
1748          *  WIN_IDENTIFY returns little-endian info,
1749          *  WIN_PIDENTIFY *usually* returns little-endian info.
1750          */
1751         bswap = 1;
1752         if (cmd == WIN_PIDENTIFY) {
1753                 if ((id->model[0] == 'N' && id->model[1] == 'E')
1754                  || (id->model[0] == 'F' && id->model[1] == 'X'))
1755                         bswap = 0;      /* NEC and *some* Mitsumi units */
1756         }                               /* Vertos drives may still be weird */
1757         fixstring (id->model,     sizeof(id->model),     bswap);
1758         fixstring (id->fw_rev,    sizeof(id->fw_rev),    bswap);
1759         fixstring (id->serial_no, sizeof(id->serial_no), bswap);
1760 
1761         /*
1762          * Check for an ATAPI device
1763          */
1764         if (cmd == WIN_PIDENTIFY) {
1765 #ifdef CONFIG_BLK_DEV_IDECD
1766                 byte type = (id->config >> 8) & 0x0f;
1767 #endif  /* CONFIG_BLK_DEV_IDECD */
1768                 printk("%s: %s, ATAPI,", dev->name, id->model);
1769 #ifdef CONFIG_BLK_DEV_IDECD
1770                 if (type == 0 || type == 5)
1771                         printk(" CDROM drive\n");
1772                 else
1773                         printk(" UNKNOWN device\n");
1774                 dev->type = cdrom;      /* until we do it "correctly" above */
1775                 dev->present = 1;
1776 #else
1777                 printk(unsupported);
1778 #endif  /* CONFIG_BLK_DEV_IDECD */
1779                 return;
1780         }
1781 
1782         dev->type = disk;
1783         /* Extract geometry if we did not already have one for the drive */
1784         if (!dev->present) {
1785                 dev->present = 1;
1786                 dev->cyl     = dev->bios_cyl  = id->cyls;
1787                 dev->head    = dev->bios_head = id->heads;
1788                 dev->sect    = dev->bios_sect = id->sectors; 
1789         }
1790         /* Handle logical geometry translation by the drive */
1791         if ((id->field_valid & 1) && id->cur_cyls && id->cur_heads
1792          && (id->cur_heads <= 16) && id->cur_sectors)
1793         {
1794                 /*
1795                  * Extract the physical drive geometry for our use.
1796                  * Note that we purposely do *not* update the bios info.
1797                  * This way, programs that use it (like fdisk) will 
1798                  * still have the same logical view as the BIOS does,
1799                  * which keeps the partition table from being screwed.
1800                  *
1801                  * An exception to this is the cylinder count,
1802                  * which we reexamine later on to correct for 1024 limitations.
1803                  */
1804                 dev->cyl  = id->cur_cyls;
1805                 dev->head = id->cur_heads;
1806                 dev->sect = id->cur_sectors;
1807                 capacity  = dev->cyl * dev->head * dev->sect;
1808 
1809                 /* check for word-swapped "capacity" field in id information */
1810                 check = (id->cur_capacity0 << 16) | id->cur_capacity1;
1811                 if (check == capacity)          /* was it swapped? */
1812                         *((int *)&id->cur_capacity0) = capacity; /* fix it */
1813         }
1814         /* Use physical geometry if what we have still makes no sense */
1815         if ((!dev->head || dev->head > 16) && id->heads && id->heads <= 16) {
1816                 dev->cyl  = id->cyls;
1817                 dev->head = id->heads;
1818                 dev->sect = id->sectors; 
1819         }
1820         /* Correct the number of cyls if the bios value is too small */
1821         if (dev->sect == dev->bios_sect && dev->head == dev->bios_head) {
1822                 if (dev->cyl > dev->bios_cyl)
1823                         dev->bios_cyl = dev->cyl;
1824         }
1825         /* Determine capacity, and use LBA if the drive properly supports it */
1826         if ((id->capability & 2) && lba_capacity_is_ok(id)) {
1827                 dev->select.b.lba = 1;
1828                 capacity = id->lba_capacity;
1829         } else {
1830                 capacity = dev->cyl * dev->head * dev->sect;
1831         }
1832 
1833         ide_capacity[DEV_HWIF][dev->select.b.drive] = capacity;
1834         printk ("%s: %.40s, %ldMB w/%dKB Cache, %sCHS=%d/%d/%d",
1835          dev->name, id->model, capacity/2048L, id->buf_size/2,
1836          dev->select.b.lba ? "LBA, " : "",
1837          dev->bios_cyl, dev->bios_head, dev->bios_sect);
1838 
1839         dev->mult_count = 0;
1840         if (id->max_multsect) {
1841                 dev->mult_req = INITIAL_MULT_COUNT;
1842                 if (dev->mult_req > id->max_multsect)
1843                         dev->mult_req = id->max_multsect;
1844                 if (dev->mult_req || ((id->multsect_valid & 1) && id->multsect))
1845                         dev->special.b.set_multmode = 1;
1846                 printk(", MaxMult=%d", id->max_multsect);
1847         }
1848         printk("\n");
1849 }
1850 
1851 static void delay_10ms (void)
     /* [previous][next][first][last][top][bottom][index][help] */
1852 {
1853         unsigned long timer = jiffies + 2;
1854         while (timer > jiffies);
1855 }
1856 
1857 
1858 static int try_to_identify (ide_dev_t *dev, byte cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
1859 /*
1860  * Returns:     0  device was identified
1861  *              1  device timed-out (no response to identify request)
1862  *              2  device aborted the command (refused to identify itself)
1863  */
1864 {
1865         int hd_status, rc;
1866         unsigned long timeout;
1867 #if PROBE_FOR_IRQS
1868         int irqs = 0;
1869         static byte irq_probed[2] = {0,0};
1870 #endif  /* PROBE_FOR_IRQS */
1871 
1872         OUT_BYTE(dev->ctl|2,HD_CMD);            /* disable device irq */
1873 #if PROBE_FOR_IRQS
1874         if (!irq_probed[DEV_HWIF]) {            /* already probed for IRQ? */
1875                 irqs = probe_irq_on();          /* start monitoring irqs */
1876                 OUT_BYTE(dev->ctl,HD_CMD);      /* enable device irq */
1877         }
1878 #endif  /* PROBE_FOR_IRQS */
1879         delay_10ms();                           /* take a deep breath */
1880         if ((IN_BYTE(HD_ALTSTATUS,DEV_HWIF) ^ IN_BYTE(HD_STATUS,DEV_HWIF)) & ~INDEX_STAT) {
1881                 hd_status = HD_STATUS;          /* an ancient Seagate drive */
1882                 printk("%s: probing with STATUS instead of ALTSTATUS\n", dev->name);
1883         } else
1884                 hd_status = HD_ALTSTATUS;       /* use non-intrusive polling */
1885         OUT_BYTE(cmd,HD_COMMAND);               /* ask drive for ID */
1886         timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
1887         timeout += jiffies;
1888         do {
1889                 if (jiffies > timeout) {
1890 #if PROBE_FOR_IRQS
1891                         if (!irq_probed[DEV_HWIF])
1892                                 (void) probe_irq_off(irqs);
1893 #endif  /* PROBE_FOR_IRQS */
1894                         return 1;       /* drive timed-out */
1895                 }
1896                 delay_10ms();           /* give drive a breather */
1897         } while (IN_BYTE(hd_status,DEV_HWIF) & BUSY_STAT);
1898         delay_10ms();           /* wait for IRQ and DRQ_STAT */
1899         if (OK_STAT(GET_STAT(DEV_HWIF),DRQ_STAT,BAD_R_STAT)) {
1900                 cli();                  /* some systems need this */
1901                 do_identify(dev, cmd);  /* drive returned ID */
1902                 rc = 0;                 /* success */
1903         } else
1904                 rc = 2;                 /* drive refused ID */
1905 #if PROBE_FOR_IRQS
1906         if (!irq_probed[DEV_HWIF]) {
1907                 irqs = probe_irq_off(irqs);     /* get irq number */
1908                 if (irqs > 0) {
1909                         irq_probed[DEV_HWIF] = 1;
1910                         ide_irq[DEV_HWIF] = irqs;
1911                 } else                          /* Mmmm.. multiple IRQs */
1912                         printk("%s: IRQ probe failed (%d)\n", dev->name, irqs);
1913         }
1914 #endif  /* PROBE_FOR_IRQS */
1915         return rc;
1916 }
1917 
1918 /*
1919  * This routine has the difficult job of finding a drive if it exists,
1920  * without getting hung up if it doesn't exist, without trampling on
1921  * ethernet cards, and without leaving any IRQs dangling to haunt us later.
1922  *
1923  * If a drive is "known" to exist (from CMOS or kernel parameters),
1924  * but does not respond right away, the probe will "hang in there"
1925  * for the maximum wait time (about 30 seconds), otherwise it will
1926  * exit much more quickly.
1927  */
1928 static int do_probe (ide_dev_t *dev, byte cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
1929 /*
1930  * Returns:     0  device was identified
1931  *              1  device timed-out (no response to identify request)
1932  *              2  device aborted the command (refused to identify itself)
1933  *              3  bad status from device (possible for ATAPI drives)
1934  *              4  probe was not attempted
1935  */
1936 {
1937         int rc;
1938 
1939 #ifdef CONFIG_BLK_DEV_IDECD
1940         if (dev->present) {     /* avoid waiting for inappropriate probes */
1941                 if ((dev->type == disk) ^ (cmd == WIN_IDENTIFY))
1942                         return 4;
1943         }
1944 #endif  /* CONFIG_BLK_DEV_IDECD */
1945 #if DEBUG
1946         printk("probing for %s: present=%d, type=%s, probetype=%s\n",
1947                 dev->name, dev->present, dev->type ? "cdrom":"disk",
1948                 (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
1949 #endif
1950         OUT_BYTE(dev->select.all,HD_CURRENT);   /* select target drive */
1951         delay_10ms();                           /* wait for BUSY_STAT */
1952         if (IN_BYTE(HD_CURRENT,DEV_HWIF) != dev->select.all && !dev->present) {
1953                 OUT_BYTE(0xa0,HD_CURRENT);      /* exit with drive0 selected */
1954                 return 3;    /* no i/f present: avoid killing ethernet cards */
1955         }
1956 
1957         if (OK_STAT(GET_STAT(DEV_HWIF),READY_STAT,BUSY_STAT)
1958          || dev->present || cmd == WIN_PIDENTIFY)
1959         {
1960                 if ((rc = try_to_identify(dev, cmd)))  /* send cmd and wait */
1961                         rc = try_to_identify(dev, cmd); /* failed: try again */
1962                 if (rc == 1)
1963                         printk("%s: no response (status = 0x%02x)\n",
1964                          dev->name, GET_STAT(DEV_HWIF));
1965                 OUT_BYTE(dev->ctl|2,HD_CMD);    /* disable device irq */
1966                 delay_10ms();
1967                 (void) GET_STAT(DEV_HWIF);      /* ensure drive irq is clear */
1968         } else {
1969                 rc = 3;                         /* not present or maybe ATAPI */
1970         }
1971         if (dev->select.b.drive == 1) {
1972                 OUT_BYTE(0xa0,HD_CURRENT);      /* exit with drive0 selected */
1973                 delay_10ms();
1974                 OUT_BYTE(dev->ctl|2,HD_CMD);    /* disable device irq */
1975                 delay_10ms();
1976                 (void) GET_STAT(DEV_HWIF);      /* ensure drive irq is clear */
1977         }
1978         return rc;
1979 }
1980 
1981 static byte probe_for_drive (ide_dev_t *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1982 /*
1983  * Returns:     0  no device was found
1984  *              1  device was found (note: dev->present might still be 0)
1985  */
1986 {
1987         if (dev->dont_probe)                    /* skip probing? */
1988                 return dev->present;
1989         if (do_probe(dev, WIN_IDENTIFY) >= 2) { /* if !(success || timed-out) */
1990 #ifdef CONFIG_BLK_DEV_IDECD
1991                 (void) do_probe(dev, WIN_PIDENTIFY); /* look for ATAPI device */
1992 #endif  /* CONFIG_BLK_DEV_IDECD */
1993         }
1994         if (!dev->present)
1995                 return 0;                       /* drive not found */
1996         if (dev->id == NULL) {                  /* identification failed? */
1997                 if (dev->type == disk) {
1998                         printk ("%s: non-IDE device, CHS=%d/%d/%d\n",
1999                          dev->name, dev->cyl, dev->head, dev->sect);
2000                 }
2001 #ifdef CONFIG_BLK_DEV_IDECD
2002                 else if (dev->type == cdrom) {
2003                         printk("%s: ATAPI cdrom (?)\n", dev->name);
2004                 }
2005 #endif  /* CONFIG_BLK_DEV_IDECD */
2006                 else {
2007                         dev->present = 0;       /* nuke it */
2008                         return 1;               /* drive was found */
2009                 }
2010         }
2011 #ifdef CONFIG_BLK_DEV_IDECD
2012         if (dev->type == cdrom)
2013                 cdrom_setup(dev);
2014 #endif  /* CONFIG_BLK_DEV_IDECD */
2015         if (dev->type == disk && !dev->select.b.lba) {
2016                 if (!dev->head || dev->head > 16) {
2017                         printk("%s: cannot handle disk with %d physical heads\n",
2018                          dev->name, dev->head);
2019                         dev->present = 0;
2020                 }
2021         }
2022         return 1;       /* drive was found */
2023 }
2024 
2025 static void probe_for_drives (byte hwif)
     /* [previous][next][first][last][top][bottom][index][help] */
2026 {
2027         ide_dev_t *devs = &ide_dev[HWIF][0];    /* for convenience */
2028 
2029         if (check_region(IDE_PORT(HD_DATA,HWIF),8)
2030          || check_region(IDE_PORT(HD_CMD,HWIF),1))
2031         {
2032                 if (devs[0].present || devs[1].present)
2033                         printk("ERROR: ");
2034                 printk("%s: port(s) already in use\n", ide_name[HWIF]);
2035                 devs[0].present = 0;
2036                 devs[1].present = 0;
2037         } else {
2038                 unsigned long flags;
2039                 save_flags(flags);
2040                 sti();  /* needed for jiffies and irq probing */
2041 
2042                 /* second drive should only exist if first drive was found */
2043                 if (probe_for_drive(&devs[0]) || devs[1].present)
2044                         (void) probe_for_drive(&devs[1]);
2045 #if PROBE_FOR_IRQS
2046                 (void) probe_irq_off(probe_irq_on()); /* clear dangling irqs */
2047 #endif  /* PROBE_FOR_IRQS */
2048                 if (devs[0].present || devs[1].present) {
2049                         request_region(IDE_PORT(HD_DATA,HWIF),8,ide_name[HWIF]);
2050                         request_region(IDE_PORT(HD_CMD,HWIF),1,ide_name[HWIF]);
2051                 }
2052                 restore_flags(flags);
2053         }
2054 }
2055 
2056 static int next_drive = 0;      /* used by the ide_setup() routines below */
2057 
2058 void ide_setup(char *str, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
2059 {
2060         ide_dev_t *dev;
2061         const char *p[] = {"cyls","heads","sects","wpcom","irq"};
2062         int i, hwif, drive = next_drive++;
2063 #ifdef CONFIG_BLK_DEV_HD
2064         extern void hd_setup(char *, int *);
2065 
2066         if (drive < 2) {
2067                 hd_setup (str, ints);
2068                 return;
2069         }
2070 #endif /* CONFIG_BLK_DEV_HD */
2071         hwif = (drive > 1);
2072         printk("%s: ", ide_name[hwif]);
2073         if (drive > 3) {
2074                 printk("too many drives defined\n");
2075                 return;
2076         }
2077         drive = drive & 1;
2078         printk("%s: ", ide_devname[hwif][drive]);
2079         if (!SUPPORT_TWO_INTERFACES && hwif != HWIF) {
2080                 printk(unsupported);
2081                 return;
2082         }
2083         dev = &ide_dev[hwif][drive];
2084         if (dev->present)
2085                 printk("(redefined) ");
2086         if (ints[0] == 0) {
2087 #if SUPPORT_DTC2278
2088                 if (!strcmp(str,"dtc2278")) {
2089                         printk("%s\n",str);
2090                         probe_dtc2278 = 1;      /* try to init DTC-2278 at boot */
2091                         return;
2092                 }
2093 #endif /* SUPPORT_DTC2278 */
2094 #if SUPPORT_SERIALIZE
2095                 if (!strcmp(str,"serialize") || !strcmp(str,"cmd")) {
2096                         printk("%s\n",str);
2097                         single_threaded = 1;    /* serialize all drive access */
2098                         return;
2099                 }
2100 #endif /* SUPPORT_SERIALIZE */
2101                 if (!strcmp(str,"noprobe")) {
2102                         printk("%s\n",str);
2103                         dev->dont_probe = 1;    /* don't probe for this drive */
2104                         return;
2105                 }
2106 #ifdef CONFIG_BLK_DEV_IDECD
2107                 if (!strcmp(str,"cdrom")) {
2108                         printk("cdrom\n");
2109                         dev->present = 1;       /* force autoprobe to find it */
2110                         dev->type = cdrom;
2111                         return;
2112                 }
2113 #endif  /* CONFIG_BLK_DEV_IDECD */
2114         }
2115         if (ints[0] < 3 || ints[0] > 5) {
2116                 printk("bad parms, expected: cyls,heads,sects[,wpcom[,irq]]\n");
2117         } else {
2118                 for (i=0; i++ < ints[0];)
2119                         printk("%s=%d%c",p[i-1],ints[i],i<ints[0]?',':'\n');
2120                 dev->type    = disk;
2121                 dev->cyl     = dev->bios_cyl  = ints[1];
2122                 dev->head    = dev->bios_head = ints[2];
2123                 dev->ctl     = (ints[2] > 8 ? 8 : 0);
2124                 dev->sect    = dev->bios_sect = ints[3];
2125                 dev->wpcom   = (ints[0] >= 4) ? ints[4] : 0;
2126                 if (ints[0] >= 5)
2127                         ide_irq[HWIF] = ints[5];
2128                 ide_capacity[HWIF][drive] = BIOS_SECTORS(dev);
2129                 dev->present = 1;
2130         }
2131 }
2132 
2133 void hda_setup(char *str, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
2134 {
2135         next_drive = 0;
2136         ide_setup (str, ints);
2137 }
2138 
2139 void hdb_setup(char *str, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
2140 {
2141         next_drive = 1;
2142         ide_setup (str, ints);
2143 }
2144 
2145 void hdc_setup(char *str, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
2146 {
2147         next_drive = 2;
2148         ide_setup (str, ints);
2149 }
2150 
2151 void hdd_setup(char *str, int *ints)
     /* [previous][next][first][last][top][bottom][index][help] */
2152 {
2153         next_drive = 3;
2154         ide_setup (str, ints);
2155 }
2156 
2157 #ifndef CONFIG_BLK_DEV_HD
2158 /*
2159  * We query CMOS about hard disks : it could be that we have a SCSI/ESDI/etc
2160  * controller that is BIOS compatible with ST-506, and thus showing up in our
2161  * BIOS table, but not register compatible, and therefore not present in CMOS.
2162  *
2163  * Furthermore, we will assume that our ST-506 drives <if any> are the primary
2164  * drives in the system -- the ones reflected as drive 1 or 2.  The first
2165  * drive is stored in the high nibble of CMOS byte 0x12, the second in the low
2166  * nibble.  This will be either a 4 bit drive type or 0xf indicating use byte
2167  * 0x19 for an 8 bit type, drive 1, 0x1a for drive 2 in CMOS.  A non-zero value 
2168  * means we have an AT controller hard disk for that drive.
2169  */
2170 extern struct drive_info_struct drive_info;
2171 static void probe_cmos_for_drives (void)
     /* [previous][next][first][last][top][bottom][index][help] */
2172 {
2173         byte drive, cmos_disks, *BIOS = (byte *) &drive_info;
2174 
2175         outb_p(0x12,0x70);              /* specify CMOS address 0x12 */
2176         cmos_disks = inb_p(0x71);       /* read the data from 0x12 */
2177         /* Extract drive geometry from CMOS+BIOS if not already setup */
2178         for (drive = 0; drive < MAX_DRIVES; drive++) {
2179                 ide_dev_t *dev = &ide_dev[0][drive];
2180                 if ((cmos_disks & (0xf0 >> (drive*4))) && !dev->present) {
2181                         dev->cyl     = dev->bios_cyl  = *(unsigned short *)BIOS;
2182                         dev->head    = dev->bios_head = * (BIOS+2);
2183                         dev->sect    = dev->bios_sect = * (BIOS+14);
2184                         dev->wpcom   = (*(unsigned short *)(BIOS+5))>>2;
2185                         dev->ctl     = *(BIOS+8);
2186                         dev->wpcom   = 0;
2187                         dev->type    = disk;
2188                         dev->present = 1;
2189                         ide_capacity[0][drive] = BIOS_SECTORS(dev);
2190                 }
2191                 BIOS += 16;
2192         }
2193 }
2194 #endif  /* CONFIG_BLK_DEV_HD */
2195 
2196 static void init_ide_data (byte hwif)
     /* [previous][next][first][last][top][bottom][index][help] */
2197 {
2198         int drive;
2199 
2200         for (drive = 0; drive < (MAX_DRIVES<<PARTN_BITS); drive++)
2201                 ide_blksizes[hwif][drive] = 1024;
2202         blksize_size[ide_major[hwif]] = ide_blksizes[hwif];
2203 
2204         /* Initialize non-geometry fields -- ide_setup() runs before we do */
2205         for (drive = 0; drive < MAX_DRIVES; drive++) {
2206                 ide_dev_t *dev = &ide_dev[hwif][drive];
2207                 dev->select.all                 = (drive<<4)|0xa0;
2208                 dev->hwif                       = hwif;
2209                 dev->unmask                     = 0;
2210                 dev->busy                       = 0;
2211                 dev->mult_count                 = 0; /* set by do_identify() */
2212                 dev->mult_req                   = 0; /* set by do_identify() */
2213                 dev->usage                      = 0;
2214                 dev->vlb_32bit                  = 0;
2215                 dev->vlb_sync                   = 0;
2216                 dev->id                         = NULL;
2217                 dev->ctl                        = 0x08;
2218                 dev->wqueue                     = NULL;
2219                 dev->special.all                = 0;
2220                 dev->special.b.recalibrate      = 1;
2221                 dev->special.b.set_geometry     = 1;
2222                 dev->keep_settings              = 0;
2223                 ide_hd[hwif][drive<<PARTN_BITS].start_sect = 0;
2224                 dev->name = ide_devname[hwif][drive];
2225         }
2226 }
2227 
2228 /*
2229  * This is the harddisk IRQ description. The SA_INTERRUPT in sa_flags
2230  * means we enter the IRQ-handler with interrupts disabled: this is bad for
2231  * interrupt latency, but anything else has led to problems on some
2232  * machines.  We enable interrupts as much as we can safely do in most places.
2233  */
2234 static byte setup_irq (byte hwif)
     /* [previous][next][first][last][top][bottom][index][help] */
2235 {
2236         static byte rc = 0;
2237         unsigned long flags;
2238         const char *msg = "", *primary_secondary[] = {"primary", "secondary"};
2239         void (*handler)(int, struct pt_regs *) = HWIF ? &ide1_intr : &ide0_intr;
2240 
2241 #if SUPPORT_SHARING_IRQ
2242         if (sharing_single_irq) {
2243                 if (HWIF != 0 && !rc) { /* IRQ already allocated? */
2244                         msg = " (shared with ide0)";
2245                         goto done;
2246                 }
2247                 handler = &ide_shared_intr;
2248         }
2249 #if SUPPORT_SERIALIZE
2250         else if (single_threaded) {
2251                 handler = &ide_seq_intr;
2252                 if (HWIF != 0)
2253                         msg = " (single-threaded with ide0)";
2254         }
2255 #endif /* SUPPORT_SERIALIZE */
2256 #endif /* SUPPORT_SHARING_IRQ */
2257         save_flags(flags);
2258         cli();
2259         if ((rc = request_irq(ide_irq[HWIF],handler,SA_INTERRUPT,ide_name[HWIF])))
2260                 msg = ":  FAILED! unable to allocate IRQ";
2261         restore_flags(flags);
2262 #if SUPPORT_SHARING_IRQ
2263 done:
2264 #endif /* SUPPORT_SHARING_IRQ */
2265         printk("%s: %s interface on irq %d%s\n",
2266          ide_name[HWIF], primary_secondary[HWIF], ide_irq[HWIF], msg);
2267         return rc;
2268 }
2269 
2270 static void ide_geninit(byte hwif)
     /* [previous][next][first][last][top][bottom][index][help] */
2271 {
2272         static int drive;
2273         
2274         for (drive = 0; drive < MAX_DRIVES; drive++) {
2275                 ide_dev_t *dev = &ide_dev[HWIF][drive];
2276                 if (dev->present) {
2277                         ide_hd[HWIF][drive<<PARTN_BITS].nr_sects = ide_capacity[HWIF][drive];
2278                         /* Skip partition check for cdroms. */
2279                         if (dev->type == cdrom)
2280                                 ide_hd[HWIF][drive<<PARTN_BITS].start_sect = -1;
2281                 }
2282         }
2283 }
2284 
2285 static void ide0_geninit(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2286 {
2287         ide_geninit(0);
2288 }
2289 
2290 static void ide1_geninit(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2291 {
2292         ide_geninit(1);
2293 }
2294 
2295 static struct file_operations ide_fops = {
2296         NULL,                   /* lseek - default */
2297         block_read,             /* read - general block-dev read */
2298         block_write,            /* write - general block-dev write */
2299         NULL,                   /* readdir - bad */
2300         NULL,                   /* select */
2301         ide_ioctl,              /* ioctl */
2302         NULL,                   /* mmap */
2303         ide_open,               /* open */
2304         ide_release,            /* release */
2305         block_fsync             /* fsync */
2306 #ifdef CONFIG_BLK_DEV_IDECD
2307         ,NULL,                  /* fasync */
2308         ide_check_media_change, /* check_media_change */
2309         NULL                    /* revalidate */
2310 #endif CONFIG_BLK_DEV_IDECD
2311 };
2312 
2313 
2314 #if SUPPORT_DTC2278
2315 /*
2316  * From: andy@cercle.cts.com (Dyan Wile)
2317  *
2318  * Below is a patch for DTC-2278 - alike software-programmable controllers
2319  * The code enables the secondary IDE controller and the PIO4 (3?) timings on
2320  * the primary (EIDE). You may probably have to enable the 32-bit support to
2321  * get the full speed. You better get the disk interrupts disabled ( hdparm -u0 
2322  * /dev/hd.. ) for the drives connected to the EIDE interface. (I get my 
2323  * filesystem  corrupted with -u 1, but under heavy disk load only :-)  
2324  */
2325 
2326 static void sub22 (char b, char c)
     /* [previous][next][first][last][top][bottom][index][help] */
2327 {
2328         int i;
2329 
2330         for(i = 0; i < 3; i++) {
2331                 __inb(0x3f6);
2332                 outb_p(b,0xb0);
2333                 __inb(0x3f6);
2334                 outb_p(c,0xb4);
2335                 __inb(0x3f6);
2336                 if(__inb(0xb4) == c) {
2337                         outb_p(7,0xb0);
2338                         __inb(0x3f6);
2339                         return; /* success */
2340                 }
2341         }
2342 }
2343 
2344 static void try_to_init_dtc2278 (void)
     /* [previous][next][first][last][top][bottom][index][help] */
2345 {
2346 /* This (presumably) enables PIO mode4 (3?) on the first interface */
2347         cli();
2348         sub22(1,0xc3);
2349         sub22(0,0xa0);
2350         sti();
2351 
2352 /* This enables the second interface */
2353 
2354         outb_p(4,0xb0);
2355         __inb(0x3f6);
2356         outb_p(0x20,0xb4);
2357         __inb(0x3f6);
2358 }
2359 #endif /* SUPPORT_DTC2278 */
2360 
2361 /*
2362  * This is gets invoked once during initialization, to set *everything* up
2363  */
2364 unsigned long ide_init (unsigned long mem_start, unsigned long mem_end)
     /* [previous][next][first][last][top][bottom][index][help] */
2365 {
2366         byte hwif;
2367 
2368 #if SUPPORT_DTC2278
2369         if (probe_dtc2278)
2370                 try_to_init_dtc2278();
2371 #endif /* SUPPORT_DTC2278 */
2372         /* single_threaded = 0; */      /* zero by default, override at boot */
2373         for (hwif = 0; hwif < 2; hwif++) {
2374                 init_ide_data (hwif);
2375                 if (SUPPORT_TWO_INTERFACES || hwif == HWIF) {
2376                         if (hwif == 0)
2377 #ifdef CONFIG_BLK_DEV_HD
2378                                 continue;
2379 #else
2380                                 probe_cmos_for_drives ();
2381 #endif /* CONFIG_BLJ_DEV_HD */
2382                         probe_mem_start = (mem_start + 3uL) & ~3uL;
2383                         probe_for_drives (hwif);
2384                         mem_start = probe_mem_start;
2385                 }
2386         }
2387 
2388         /* At this point, all methods of drive detection have completed */
2389         ide_gendisk[0].nr_real = ide_dev[0][0].present + ide_dev[0][1].present;
2390         ide_gendisk[1].nr_real = ide_dev[1][0].present + ide_dev[1][1].present;
2391         if (ide_gendisk[1].nr_real && (ide_irq[0] == ide_irq[1])) {
2392                 if (!ide_gendisk[0].nr_real) {
2393                         ide_irq[0] = 0; /* needed by ide_intr() */
2394                 } else {
2395 #if SUPPORT_SHARING_IRQ
2396                         sharing_single_irq = 1;
2397                         single_threaded = 1;
2398 #else /* SUPPORT_SHARING_IRQ */
2399                         printk("%s: ide irq-sharing%s", ide_name[1], unsupported);
2400                         return mem_start;
2401 #endif /* SUPPORT_SHARING_IRQ */
2402                 }
2403         }
2404 #ifdef CONFIG_BLK_DEV_HD
2405 #if SUPPORT_SHARING_IRQ
2406         if (ide_irq[1] == 14 || sharing_single_irq) {
2407 #else
2408         if (ide_irq[1] == 14) {
2409 #endif /* SUPPORT_SHARING_IRQ */
2410                 printk("%s: irq-sharing not possible with old harddisk driver (hd.c)\n", ide_name[1]);
2411                 return mem_start;
2412         }
2413 #endif /* CONFIG_BLK_DEV_HD */
2414 
2415         for (hwif = 2; hwif-- > 0;) {
2416                 if (ide_gendisk[hwif].nr_real != 0 && !setup_irq(hwif)) {
2417                         const char *name = ide_name[HWIF];
2418                         unsigned int major = ide_major[HWIF];
2419                         if (register_blkdev(major, name, &ide_fops)) {
2420                                 printk("%s: unable to get major number %d\n", name, major);
2421                         } else {
2422                                 timer_table[ide_timer[HWIF]].fn
2423                                         = HWIF ? ide1_timer_expiry : ide0_timer_expiry;
2424 #if SUPPORT_SHARING_IRQ
2425                                 if (single_threaded)
2426                                         blk_dev[major].request_fn = &do_shared_request;
2427                                 else
2428 #endif /* SUPPORT_SHARING_IRQ */
2429                                 blk_dev[major].request_fn =
2430                                  HWIF ? &do_ide1_request : &do_ide0_request;
2431                                 read_ahead[major] = 8;  /* (4kB) */
2432                                 ide_gendisk[HWIF].next = gendisk_head;
2433                                 gendisk_head = &ide_gendisk[HWIF];
2434                         }
2435                 }
2436         }
2437         return mem_start;
2438 }

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