root/drivers/block/ide.c

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

DEFINITIONS

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

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