root/drivers/block/ide.c

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

DEFINITIONS

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

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

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