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

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