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. ide_xlate_1024
  57. probe_cmos_for_drives
  58. init_ide_data
  59. setup_irq
  60. ide_geninit
  61. ide0_geninit
  62. ide1_geninit
  63. sub22
  64. try_to_init_dtc2278
  65. ide_init

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

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