root/drivers/block/ide.c

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

DEFINITIONS

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

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

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