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

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