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

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