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