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

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