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

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