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

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