root/drivers/block/ide.c

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

DEFINITIONS

This source file includes following definitions.
  1. read_timer
  2. ide_set_recovery_timer
  3. init_ide_data
  4. do_vlb_sync
  5. ide_input_data
  6. ide_output_data
  7. ide_hwif_select
  8. ide_set_handler
  9. lba_capacity_is_ok
  10. current_capacity
  11. ide_geninit
  12. ide_alloc
  13. init_gendisk
  14. reset_ihandler
  15. start_reset_timer
  16. atapi_reset_handler
  17. reset_handler
  18. ide_do_reset
  19. end_drive_cmd
  20. ide_dump_status
  21. try_to_flush_leftover_data
  22. ide_error
  23. read_intr
  24. write_intr
  25. multwrite
  26. multwrite_intr
  27. ide_cmd
  28. set_multmode_intr
  29. set_geometry_intr
  30. recal_intr
  31. drive_cmd_intr
  32. do_special
  33. ide_wait_stat
  34. do_rw_disk
  35. do_request
  36. ide_do_request
  37. do_hwgroup_request
  38. do_ide0_request
  39. do_ide1_request
  40. do_ide2_request
  41. do_ide3_request
  42. timer_expiry
  43. unexpected_intr
  44. ide_intr
  45. get_info_ptr
  46. do_drive_cmd
  47. ide_open
  48. ide_release
  49. revalidate_disk
  50. write_fs_long
  51. ide_ioctl
  52. ide_check_media_change
  53. fixstring
  54. do_identify
  55. delay_10ms
  56. try_to_identify
  57. do_probe
  58. probe_for_drive
  59. probe_for_drives
  60. sub22
  61. init_dtc2278
  62. read_cmd640_vlb
  63. write_cmd640_vlb
  64. init_cmd640_vlb
  65. stridx
  66. match_parm
  67. ide_setup
  68. ide_xlate_1024
  69. probe_cmos_for_drives
  70. init_irq
  71. ide_pci_access_error
  72. buggy_interface_fallback
  73. init_rz1000
  74. init_cmd640
  75. ide_probe_pci
  76. ide_init_pci
  77. ide_init

   1 /*
   2  *  linux/drivers/block/ide.c   Version 5.11  Aug 29, 1995
   3  *
   4  *  Copyright (C) 1994, 1995  Linus Torvalds & authors (see below)
   5  */
   6 
   7 /*
   8  * This is the multiple IDE interface driver, as evolved from hd.c.  
   9  * It supports up to four IDE interfaces, on one or more 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  * Tertiary i/f:   ide2: major=33; (hde)         minor=0; (hdf)         minor=64
  15  * Quaternary i/f: ide3: major=34; (hdg)         minor=0; (hdh)         minor=64
  16  * 
  17  *  From hd.c:
  18  *  |
  19  *  | It traverses the request-list, using interrupts to jump between functions.
  20  *  | As nearly all functions can be called within interrupts, we may not sleep.
  21  *  | Special care is recommended.  Have Fun!
  22  *  |
  23  *  | modified by Drew Eckhardt to check nr of hd's from the CMOS.
  24  *  |
  25  *  | Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
  26  *  | in the early extended-partition checks and added DM partitions.
  27  *  |
  28  *  | Early work on error handling by Mika Liljeberg (liljeber@cs.Helsinki.FI).
  29  *  |
  30  *  | IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
  31  *  | and general streamlining by Mark Lord (mlord@bnr.ca).
  32  *
  33  *  October, 1994 -- Complete line-by-line overhaul for linux 1.1.x, by:
  34  *
  35  *      Mark Lord       (mlord@bnr.ca)                  (IDE Perf.Pkg)
  36  *      Delman Lee      (delman@mipg.upenn.edu)         ("Mr. atdisk2")
  37  *      Petri Mattila   (ptjmatti@kruuna.helsinki.fi)   (EIDE stuff)
  38  *      Scott Snyder    (snyder@fnald0.fnal.gov)        (ATAPI IDE cd-rom)
  39  *
  40  *  This was a rewrite of just about everything from hd.c, though some original
  41  *  code is still sprinkled about.  Think of it as a major evolution, with 
  42  *  inspiration from lots of linux users, esp.  hamish@zot.apana.org.au
  43  *
  44  *  Version 1.0 ALPHA   initial code, primary i/f working okay
  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  *  ...
  49  *  Version 3.5         correct the bios_cyl field if it's too small
  50  *  (linux 1.1.76)       (to help fdisk with brain-dead BIOSs)
  51  *  Version 3.6         cosmetic corrections to comments and stuff
  52  *  (linux 1.1.77)      reorganise probing code to make it understandable
  53  *                      added halfway retry to probing for drive identification
  54  *                      added "hdx=noprobe" command line option
  55  *                      allow setting multmode even when identification fails
  56  *  Version 3.7         move set_geometry=1 from do_identify() to ide_init()
  57  *                      increase DRQ_WAIT to eliminate nuisance messages
  58  *                      wait for DRQ_STAT instead of DATA_READY during probing
  59  *                        (courtesy of Gary Thomas gary@efland.UU.NET)
  60  *  Version 3.8         fixed byte-swapping for confused Mitsumi cdrom drives
  61  *                      update of ide-cd.c from Scott, allows blocksize=1024
  62  *                      cdrom probe fixes, inspired by jprang@uni-duisburg.de
  63  *  Version 3.9         don't use LBA if lba_capacity looks funny
  64  *                      correct the drive capacity calculations
  65  *                      fix probing for old Seagates without IDE_ALTSTATUS_REG
  66  *                      fix byte-ordering for some NEC cdrom drives
  67  *  Version 3.10        disable multiple mode by default; was causing trouble
  68  *  Version 3.11        fix mis-identification of old WD disks as cdroms
  69  *  Version 3,12        simplify logic for selecting initial mult_count
  70  *                        (fixes problems with buggy WD drives)
  71  *  Version 3.13        remove excess "multiple mode disabled" messages
  72  *  Version 3.14        fix ide_error() handling of BUSY_STAT
  73  *                      fix byte-swapped cdrom strings (again.. arghh!)
  74  *                      ignore INDEX bit when checking the ALTSTATUS reg
  75  *  Version 3.15        add SINGLE_THREADED flag for use with dual-CMD i/f
  76  *                      ignore WRERR_STAT for non-write operations
  77  *                      added vlb_sync support for DC-2000A & others,
  78  *                       (incl. some Promise chips), courtesy of Frank Gockel
  79  *  Version 3.16        convert vlb_32bit and vlb_sync into runtime flags
  80  *                      add ioctls to get/set VLB flags (HDIO_[SG]ET_CHIPSET)
  81  *                      rename SINGLE_THREADED to SUPPORT_SERIALIZE,
  82  *                      add boot flag to "serialize" operation for CMD i/f
  83  *                      add optional support for DTC2278 interfaces,
  84  *                       courtesy of andy@cercle.cts.com (Dyan Wile).
  85  *                      add boot flag to enable "dtc2278" probe
  86  *                      add probe to avoid EATA (SCSI) interfaces,
  87  *                       courtesy of neuffer@goofy.zdv.uni-mainz.de.
  88  *  Version 4.00        tidy up verify_area() calls - heiko@colossus.escape.de
  89  *                      add flag to ignore WRERR_STAT for some drives
  90  *                       courtesy of David.H.West@um.cc.umich.edu
  91  *                      assembly syntax tweak to vlb_sync
  92  *                      removeable drive support from scuba@cs.tu-berlin.de
  93  *                      add transparent support for DiskManager-6.0x "Dynamic
  94  *                       Disk Overlay" (DDO), most of this in in genhd.c
  95  *                      eliminate "multiple mode turned off" message at boot
  96  *  Version 4.10        fix bug in ioctl for "hdparm -c3"
  97  *                      fix DM6:DDO support -- now works with LILO, fdisk, ...
  98  *                      don't treat some naughty WD drives as removeable
  99  *  Version 4.11        updated DM6 support using info provided by OnTrack
 100  *  Version 5.00        major overhaul, multmode setting fixed, vlb_sync fixed
 101  *                      added support for 3rd/4th/alternative IDE ports
 102  *                      created ide.h; ide-cd.c now compiles separate from ide.c
 103  *                      hopefully fixed infinite "unexpected_intr" from cdroms
 104  *                      zillions of other changes and restructuring
 105  *                      somehow reduced overall memory usage by several kB
 106  *                      probably slowed things down slightly, but worth it
 107  *  Version 5.01        AT LAST!!  Finally understood why "unexpected_intr"
 108  *                       was happening at various times/places:  whenever the
 109  *                       ide-interface's ctl_port was used to "mask" the irq,
 110  *                       it also would trigger an edge in the process of masking
 111  *                       which would result in a self-inflicted interrupt!!
 112  *                       (such a stupid way to build a hardware interrupt mask).
 113  *                       This is now fixed (after a year of head-scratching).
 114  *  Version 5.02        got rid of need for {enable,disable}_irq_list()
 115  *  Version 5.03        tune-ups, comments, remove "busy wait" from drive resets
 116  *                      removed PROBE_FOR_IRQS option -- no longer needed
 117  *                      OOOPS!  fixed "bad access" bug for 2nd drive on an i/f
 118  *  Version 5.04        changed "ira %d" to "irq %d" in DEBUG message
 119  *                      added more comments, cleaned up unexpected_intr()
 120  *                      OOOPS!  fixed null pointer problem in ide reset code
 121  *                      added autodetect for Triton chipset -- no effect yet
 122  *  Version 5.05        OOOPS!  fixed bug in revalidate_disk()
 123  *                      OOOPS!  fixed bug in ide_do_request()
 124  *                      added ATAPI reset sequence for cdroms
 125  *  Version 5.10        added Bus-Mastered DMA support for Triton Chipset
 126  *                      some (mostly) cosmetic changes
 127  *  Version 5.11        added ht6560b support by malafoss@snakemail.hut.fi
 128  *                      reworked PCI scanning code
 129  *                      added automatic RZ1000 detection/support
 130  *                      added automatic PCI CMD640 detection/support
 131  *                      added option for VLB CMD640 support
 132  *                      tweaked probe to find cdrom on hdb with disks on hda,hdc
 133  *
 134  *  Driver compile-time options are in ide.h
 135  *
 136  *  To do, in likely order of completion:
 137  *      - improved CMD support:  probably handing this off to someone else
 138  *      - modify kernel to obtain BIOS geometry for drives on 2nd/3rd/4th i/f
 139  *      - find someone to work on IDE *tape drive* support
 140  */
 141 
 142 #include <linux/config.h>
 143 #include <linux/types.h>
 144 #include <linux/string.h>
 145 #include <linux/kernel.h>
 146 #include <linux/delay.h>
 147 #include <linux/timer.h>
 148 #include <linux/mm.h>
 149 #include <linux/ioport.h>
 150 #include <linux/interrupt.h>
 151 #include <linux/major.h>
 152 #include <linux/blkdev.h>
 153 #include <linux/errno.h>
 154 #include <linux/hdreg.h>
 155 #include <linux/genhd.h>
 156 #include <asm/byteorder.h>
 157 #include <asm/irq.h>
 158 #include <asm/segment.h>
 159 
 160 #ifdef CONFIG_PCI
 161 #include <linux/bios32.h>
 162 #include <linux/pci.h>
 163 #endif /* CONFIG_PCI */
 164 
 165 #include "ide.h"
 166 
 167        ide_hwif_t       ide_hwifs[MAX_HWIFS];           /* hwif info */
 168 static ide_hwgroup_t    *irq_to_hwgroup [16];
 169 static const byte       ide_hwif_to_major[MAX_HWIFS]   = {IDE0_MAJOR, IDE1_MAJOR, IDE2_MAJOR, IDE3_MAJOR};
 170 
 171 static const unsigned short default_io_base[MAX_HWIFS] = {0x1f0, 0x170, 0x1e8, 0x168};
 172 static const byte       default_irqs[MAX_HWIFS]     = {14, 15, 11, 10};
 173 static int              single_threaded = 0;    /* "serialize" option */
 174 static int              disallow_unmask = 0;    /* for buggy hardware */
 175 
 176 #if (DISK_RECOVERY_TIME > 0)
 177 /*
 178  * For really screwy hardware (hey, at least it *can* be used with Linux)
 179  * we can enforce a minimum delay time between successive operations.
 180  */
 181 static unsigned long read_timer(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 182 {
 183         unsigned long t, flags;
 184         int i;
 185 
 186         save_flags(flags);
 187         cli();
 188         t = jiffies * 11932;
 189         outb_p(0, 0x43);
 190         i = inb_p(0x40);
 191         i |= inb(0x40) << 8;
 192         restore_flags(flags);
 193         return (t - i);
 194 }
 195 
 196 void ide_set_recovery_timer (ide_hwif_t *hwif)
     /* [previous][next][first][last][top][bottom][index][help] */
 197 {
 198         hwif->last_time = read_timer();
 199 }
 200 #endif /* DISK_RECOVERY_TIME */
 201 
 202 /*
 203  * init_ide_data() sets reasonable default values into all fields
 204  * of all instances of the hwifs and drives, but only on the first call.
 205  * Subsequent calls have no effect (they don't wipe out anything).
 206  *
 207  * This routine is normally called at driver initialization time,
 208  * but may also be called MUCH earlier during kernel "command-line"
 209  * parameter processing.  As such, we cannot depend on any other parts
 210  * of the kernel (such as memory allocation) to be functioning yet.
 211  *
 212  * This is too bad, as otherwise we could dynamically allocate the
 213  * ide_drive_t structs as needed, rather than always consuming memory
 214  * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
 215  */
 216 #define MAGIC_COOKIE 0x12345678
 217 static void init_ide_data (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 218 {
 219         byte *p;
 220         unsigned int h, unit;
 221         static unsigned long magic_cookie = MAGIC_COOKIE;
 222 
 223         if (magic_cookie != MAGIC_COOKIE)
 224                 return;         /* already initialized */
 225         magic_cookie = 0;
 226 
 227         for (h = 0; h < 16; ++h)
 228                  irq_to_hwgroup[h] = NULL;
 229 
 230         /* bulk initialize hwif & drive info with zeros */
 231         p = ((byte *) ide_hwifs) + sizeof(ide_hwifs);
 232         do {
 233                 *--p = 0;
 234         } while (p > (byte *) ide_hwifs);
 235 
 236         for (h = 0; h < MAX_HWIFS; ++h) {
 237                 ide_hwif_t *hwif = &ide_hwifs[h];
 238 
 239                 /* fill in any non-zero initial values */
 240                 hwif->noprobe   = (h > 1);
 241                 hwif->io_base   = default_io_base[h];
 242                 hwif->ctl_port  = hwif->io_base ? hwif->io_base + 0x206 : 0x000;
 243 #ifdef CONFIG_BLK_DEV_HD
 244                 if (hwif->io_base == HD_DATA)
 245                         hwif->noprobe = 1; /* may be overriden by ide_setup() */
 246 #endif /* CONFIG_BLK_DEV_HD */
 247                 hwif->major     = ide_hwif_to_major[h];
 248                 hwif->name[0]   = 'i';
 249                 hwif->name[1]   = 'd';
 250                 hwif->name[2]   = 'e';
 251                 hwif->name[3]   = '0' + h;
 252 
 253                 for (unit = 0; unit < MAX_DRIVES; ++unit) {
 254                         ide_drive_t *drive = &hwif->drives[unit];
 255 
 256                         /* fill in any non-zero initial values */
 257                         drive->select.all               = (unit<<4)|0xa0;
 258                         drive->hwif                     = hwif;
 259                         drive->ctl                      = 0x08;
 260                         drive->ready_stat               = READY_STAT;
 261                         drive->bad_wstat                = BAD_W_STAT;
 262                         drive->special.b.recalibrate    = 1;
 263                         drive->special.b.set_geometry   = 1;
 264                         drive->name[0]                  = 'h';
 265                         drive->name[1]                  = 'd';
 266                         drive->name[2]                  = 'a' + (h * MAX_DRIVES) + unit;
 267                 }
 268         }
 269 }
 270 
 271 #ifdef __i386__
 272 #define VLB_SYNC 1
 273 /*
 274  * Some localbus EIDE interfaces require a special access sequence
 275  * when using 32-bit I/O instructions to transfer data.  We call this
 276  * the "vlb_sync" sequence, which consists of three successive reads
 277  * of the sector count register location, with interrupts disabled
 278  * to ensure that the reads all happen together.
 279  */
 280 static inline void do_vlb_sync (unsigned short port) {
     /* [previous][next][first][last][top][bottom][index][help] */
 281         unsigned int _v;
 282         __asm__ __volatile__    (
 283                 "inb %w1,%b0\n\t"
 284                 "inb %w1,%b0\n\t"
 285                 "inb %w1,%b0"
 286                 : "=&a" (_v)            /* outputs */
 287                 : "d" (port)    );      /* inputs */
 288 }
 289 #endif  /* __i386__ */
 290 
 291 /*
 292  * This is used for most PIO data transfers *from* the IDE interface
 293  */
 294 void ide_input_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
     /* [previous][next][first][last][top][bottom][index][help] */
 295 {
 296         if (drive->vlb_32bit) {
 297 #ifdef VLB_SYNC
 298                 if (drive->vlb_sync) {
 299                         cli();
 300                         do_vlb_sync(IDE_NSECTOR_REG);
 301                         insl(IDE_DATA_REG, buffer, wcount);
 302                         if (drive->unmask)
 303                                 sti();
 304                 } else
 305 #endif /* VLB_SYNC */
 306                         insl(IDE_DATA_REG, buffer, wcount);
 307         } else
 308                 insw(IDE_DATA_REG, buffer, wcount<<1);
 309 }
 310 
 311 /*
 312  * This is used for most PIO data transfers *to* the IDE interface
 313  */
 314 void ide_output_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
     /* [previous][next][first][last][top][bottom][index][help] */
 315 {
 316         if (drive->vlb_32bit) {
 317 #ifdef VLB_SYNC
 318                 if (drive->vlb_sync) {
 319                         cli();
 320                         do_vlb_sync(IDE_NSECTOR_REG);
 321                         outsl(IDE_DATA_REG, buffer, wcount);
 322                         if (drive->unmask)
 323                                 sti();
 324                 } else
 325 #endif /* VLB_SYNC */
 326                         outsl(IDE_DATA_REG, buffer, wcount);
 327         } else
 328                 outsw(IDE_DATA_REG, buffer, wcount<<1);
 329 }
 330 
 331 #if SUPPORT_HT6560B
 332 /*
 333  * This routine handles interface switching for the peculiar hardware design
 334  * on the F.G.I./Holtek HT-6560B VLB IDE interface.
 335  * The HT-6560B can only enable one IDE port at a time, and requires a
 336  * silly sequence (below) whenever we switch between primary and secondary.
 337  *
 338  * Apparently, systems with multiple CMD640 chips may need something similar..
 339  *
 340  * This algorithm courtesy of malafoss@snakemail.hut.fi
 341  */
 342 
 343 void ide_hwif_select (ide_hwif_t *hwif)
     /* [previous][next][first][last][top][bottom][index][help] */
 344 {
 345         static ide_hwif_t *current_hwif = NULL;
 346 
 347         if (hwif != current_hwif) {
 348                 unsigned long flags;
 349                 save_flags (flags);
 350                 cli();
 351                 (void) inb(0x3e6);
 352                 (void) inb(0x3e6);
 353                 (void) inb(0x3e6);
 354                 (void) inb(0x3e6);
 355                 outb(0x1c,hwif->select);
 356                 current_hwif = hwif;
 357                 restore_flags (flags);
 358         }
 359 }
 360 #endif /* SUPPORT_HT6560B */
 361 
 362 /*
 363  * This should get invoked any time we exit the driver to
 364  * wait for an interrupt response from a drive.  handler() points
 365  * at the appropriate code to handle the next interrupt, and a
 366  * timer is started to prevent us from waiting forever in case
 367  * something goes wrong (see the timer_expiry() handler later on).
 368  */
 369 void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler)
     /* [previous][next][first][last][top][bottom][index][help] */
 370 {
 371         ide_hwgroup_t *hwgroup = HWGROUP(drive);
 372 #ifdef DEBUG
 373         if (hwgroup->handler != NULL)
 374                 printk("%s: ide_set_handler: old handler not null\n", drive->name);
 375 #endif
 376         hwgroup->handler       = handler;
 377         hwgroup->timer.expires = jiffies + WAIT_CMD;
 378         add_timer(&(hwgroup->timer));
 379 }
 380 
 381 /*
 382  * lba_capacity_is_ok() performs a sanity check on the claimed "lba_capacity"
 383  * value for this drive (from its reported identification information).
 384  *
 385  * Returns:     1 if lba_capacity looks sensible
 386  *              0 otherwise
 387  */
 388 static int lba_capacity_is_ok (struct hd_driveid *id)
     /* [previous][next][first][last][top][bottom][index][help] */
 389 {
 390         unsigned long lba_sects   = id->lba_capacity;
 391         unsigned long chs_sects   = id->cyls * id->heads * id->sectors;
 392         unsigned long _10_percent = chs_sects / 10;
 393 
 394         /* perform a rough sanity check on lba_sects:  within 10% is "okay" */
 395         if ((lba_sects - chs_sects) < _10_percent)
 396                 return 1;       /* lba_capacity is good */
 397 
 398         /* some drives have the word order reversed */
 399         lba_sects = (lba_sects << 16) | (lba_sects >> 16);
 400         if ((lba_sects - chs_sects) < _10_percent) {
 401                 id->lba_capacity = lba_sects;   /* fix it */
 402                 return 1;       /* lba_capacity is (now) good */
 403         }
 404         return 0;       /* lba_capacity value is bad */
 405 }
 406 
 407 /*
 408  * current_capacity() returns the capacity (in sectors) of a drive
 409  * according to its current geometry/LBA settings.
 410  */
 411 static unsigned long current_capacity (ide_drive_t  *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 412 {
 413         struct hd_driveid *id = drive->id;
 414         unsigned long capacity;
 415 
 416         if (!drive->present)
 417                 return 0;
 418         if (drive->media != disk)
 419                 return 0x1fffff;        /* cdrom */
 420         /* Determine capacity, and use LBA if the drive properly supports it */
 421         if (id != NULL && (id->capability & 2) && lba_capacity_is_ok(id)) {
 422                 drive->select.b.lba = 1;
 423                 capacity = id->lba_capacity;
 424         } else {
 425                 drive->select.b.lba = 0;
 426                 capacity = drive->cyl * drive->head * drive->sect;
 427         }
 428         return (capacity - drive->sect0);
 429 }
 430 
 431 /*
 432  * ide_geninit() is called exactly *once* for each major, from genhd.c,
 433  * at the beginning of the initial partition check for the drives.
 434  */
 435 static void ide_geninit (struct gendisk *gd)
     /* [previous][next][first][last][top][bottom][index][help] */
 436 {
 437         unsigned int unit;
 438         ide_hwif_t *hwif = gd->real_devices;
 439 
 440         for (unit = 0; unit < gd->nr_real; ++unit) {
 441                 ide_drive_t *drive = &hwif->drives[unit];
 442                 drive->part[0].nr_sects = current_capacity(drive);
 443                 if (!drive->present || drive->media != disk) {
 444                         drive->part[0].start_sect = -1; /* skip partition check */
 445                 }
 446         }
 447         /*
 448          * The partition check in genhd.c needs this string to identify
 449          * our minor devices by name for display purposes.
 450          * Note that doing this will prevent us from working correctly
 451          * if ever called a second time for this major (never happens).
 452          */
 453         gd->real_devices = hwif->drives[0].name;  /* name of first drive */
 454 }
 455 
 456 /*
 457  * ide_alloc(): memory allocation for use *only* during driver initialization.
 458  * If "within_area" is non-zero, the memory will be allocated such that
 459  * it lies entirely within a "within_area" sized area (eg. 4096).  This is
 460  * needed for DMA stuff.  "within_area" must be a power of two (not validated).
 461  * All allocations are longword aligned.
 462  */
 463 static unsigned long ide_mem_start = 0uL;       /* used by ide_alloc() */
 464 
 465 void *ide_alloc (unsigned long bytecount, unsigned long within_area)
     /* [previous][next][first][last][top][bottom][index][help] */
 466 {
 467         const unsigned long longsize_m1 = (sizeof(long) - 1);
 468         void *p;
 469 
 470         if (!ide_mem_start)
 471                 panic("ide: ide_alloc() not valid now\n");
 472         ide_mem_start = (ide_mem_start + longsize_m1) & ~longsize_m1;
 473         if (within_area) {
 474                 unsigned long fraction = within_area - (ide_mem_start & (within_area - 1));
 475                 if (fraction < bytecount)
 476                         ide_mem_start += fraction; /* realign to a new page */
 477         }
 478         p = (void *) ide_mem_start;
 479         ide_mem_start += (bytecount + longsize_m1) & ~longsize_m1;
 480         return p;
 481 }
 482 
 483 /*
 484  * init_gendisk() (as opposed to ide_geninit) is called for each major device,
 485  * after probing for drives, to allocate partition tables and other data
 486  * structures needed for the routines in genhd.c.  ide_geninit() gets called
 487  * somewhat later, during the partition check.
 488  */
 489 static void init_gendisk (ide_hwif_t *hwif)
     /* [previous][next][first][last][top][bottom][index][help] */
 490 {
 491         struct gendisk *gd;
 492         unsigned int unit, units, minors;
 493         int *bs;
 494 
 495         /* figure out maximum drive number on the interface */
 496         for (units = MAX_DRIVES; units > 0; --units) {
 497                 if (hwif->drives[units-1].present)
 498                         break;
 499         }
 500         minors    = units * (1<<PARTN_BITS);
 501         gd        = ide_alloc (sizeof(struct gendisk), 0);
 502         gd->sizes = ide_alloc (minors * sizeof(int), 0);
 503         gd->part  = ide_alloc (minors * sizeof(struct hd_struct), 0);
 504         bs        = ide_alloc (minors*sizeof(int), 0);
 505 
 506         /* cdroms and msdos f/s are examples of non-1024 blocksizes */
 507         blksize_size[hwif->major] = bs;
 508         for (unit = 0; unit < minors; ++unit)
 509                 *bs++ = BLOCK_SIZE;
 510 
 511         for (unit = 0; unit < units; ++unit)
 512                 hwif->drives[unit].part = &gd->part[unit << PARTN_BITS];
 513 
 514         gd->major       = hwif->major;          /* our major device number */
 515         gd->major_name  = IDE_MAJOR_NAME;       /* treated special in genhd.c */
 516         gd->minor_shift = PARTN_BITS;           /* num bits for partitions */
 517         gd->max_p       = 1<<PARTN_BITS;        /* 1 + max partitions / drive */
 518         gd->max_nr      = units;                /* max num real drives */
 519         gd->nr_real     = units;                /* current num real drives */
 520         gd->init        = ide_geninit;          /* initialization function */
 521         gd->real_devices= hwif;                 /* ptr to internal data */
 522 
 523         gd->next = gendisk_head;                /* link new major into list */
 524         hwif->gd = gendisk_head = gd;
 525 }
 526 
 527 static void unexpected_intr (int, ide_hwgroup_t *);
 528 /*
 529  * reset_ihandler() is a dummy interrupt handler which we install during
 530  * an ide interface reset operation.  This prevents other devices in the
 531  * same hwgroup from being serviced while we play around with shared resources.
 532  * If it ever gets invoked, we call unexpected_intr(), which treats the event
 533  * the same as a timer_expiry().
 534  */
 535 static void reset_ihandler (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 536 {
 537         unsigned long flags;
 538 
 539         save_flags(flags);
 540         cli();
 541         unexpected_intr (HWIF(drive)->irq, HWGROUP(drive));
 542         restore_flags(flags);
 543 }
 544 
 545 /*
 546  * start_reset_timer() sets up a timer event for 50ms in the future,
 547  * to poll for completion of an ide reset operation (no interrupt to help us).
 548  */
 549 static void start_reset_timer (ide_hwif_t *hwif)
     /* [previous][next][first][last][top][bottom][index][help] */
 550 {
 551         ide_hwgroup_t *hwgroup = hwif->hwgroup;
 552 
 553         hwgroup->reset_timeout = jiffies + WAIT_WORSTCASE; /* max waiting time */
 554         hwgroup->handler = &reset_ihandler;             /* dummy irq handler */
 555         hwgroup->timer.expires = jiffies + (HZ/20);     /* polling interval */
 556         add_timer(&(hwgroup->timer));
 557 }
 558 
 559 #ifdef CONFIG_BLK_DEV_IDECD
 560 /*
 561  * atapi_reset_handler() gets invoked to poll the interface for completion every 50ms
 562  * during an atapi drive reset operation. If the drive has not yet responded,
 563  * and we have not yet hit our maximum waiting time, then the timer is restarted
 564  * for another 50ms.
 565  *
 566  * Returns 1 if waiting for another 50ms, returns 0 otherwise.
 567  */
 568 static int atapi_reset_handler (ide_hwgroup_t *hwgroup)
     /* [previous][next][first][last][top][bottom][index][help] */
 569 {
 570         ide_hwif_t *hwif = hwgroup->hwif;
 571         ide_drive_t *drive = hwgroup->drive;
 572         byte stat;
 573 
 574         OUT_BYTE (drive->select.all, IDE_SELECT_REG);
 575         udelay (10);
 576 
 577         if (!OK_STAT(stat=GET_STAT(), 0, BUSY_STAT)) {
 578                 if (jiffies < hwgroup->reset_timeout) {
 579                         start_reset_timer (hwif);
 580                         return 1;
 581                 }
 582                 printk("%s: ATAPI reset timed-out, status=0x%02x\n", drive->name, stat);
 583                 return ide_do_reset (drive);    /* do it the old fashioned way */
 584         }
 585         hwgroup->doing_atapi_reset = 0;
 586         hwgroup->handler = NULL;        /* allow new requests to be processed */
 587         hwgroup->reset_timeout = 0;     /* signal end of ide reset operation */
 588         printk("%s: ATAPI reset complete\n", drive->name);
 589         return 0;
 590 }
 591 #endif /* CONFIG_BLK_DEV_IDECD */
 592 
 593 /*
 594  * reset_handler() gets invoked to poll the interface for completion every 50ms
 595  * during an ide reset operation. If the drives have not yet responded,
 596  * and we have not yet hit our maximum waiting time, then the timer is restarted
 597  * for another 50ms.
 598  *
 599  * Returns 1 if waiting for another 50ms, returns 0 otherwise.
 600  */
 601 static int reset_handler (ide_hwgroup_t *hwgroup)
     /* [previous][next][first][last][top][bottom][index][help] */
 602 {
 603         ide_hwif_t *hwif = hwgroup->hwif;
 604         ide_drive_t *drive = hwgroup->drive;
 605         byte tmp;
 606 
 607 #ifdef CONFIG_BLK_DEV_IDECD
 608         if (hwgroup->doing_atapi_reset)
 609                 return atapi_reset_handler(hwgroup);
 610 #endif /* CONFIG_BLK_DEV_IDECD */
 611 
 612         if (!OK_STAT(tmp=GET_STAT(), 0, BUSY_STAT)) {
 613                 if (jiffies < hwgroup->reset_timeout) {
 614                         start_reset_timer (hwif);
 615                         return 1;
 616                 }
 617                 printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
 618         } else  {
 619                 printk("%s: reset: ", hwif->name);
 620                 if ((tmp = GET_ERR()) == 1)
 621                         printk("success\n");
 622                 else {
 623                         printk("master: ");
 624                         switch (tmp & 0x7f) {
 625                                 case 1: printk("passed");
 626                                         break;
 627                                 case 2: printk("formatter device error");
 628                                         break;
 629                                 case 3: printk("sector buffer error");
 630                                         break;
 631                                 case 4: printk("ECC circuitry error");
 632                                         break;
 633                                 case 5: printk("controlling MPU error");
 634                                         break;
 635                                 default:printk("error (0x%02x?)", tmp);
 636                         }
 637                         if (tmp & 0x80)
 638                                 printk("; slave: failed");
 639                         printk("\n");
 640                 }
 641         }
 642         hwgroup->handler = NULL;        /* allow new requests to be processed */
 643         hwgroup->reset_timeout = 0;     /* signal end of ide reset operation */
 644         return 0;
 645 }
 646 
 647 /*
 648  * ide_do_reset() attempts to recover a confused drive by resetting it.
 649  * Unfortunately, resetting a disk drive actually resets all devices on
 650  * the same interface, so it can really be thought of as resetting the
 651  * interface rather than resetting the drive.
 652  *
 653  * ATAPI devices have their own reset mechanism which allows them to be
 654  * individually reset without clobbering other devices on the same interface.
 655  *
 656  * Unfortunately, the IDE interface does not generate an interrupt to let
 657  * us know when the reset operation has finished, so we must poll for this.
 658  * Equally poor, though, is the fact that this may a very long time to complete,
 659  * (up to 30 seconds worstcase).  So, instead of busy-waiting here for it,
 660  * we set a timer to poll at 50ms intervals.
 661  */
 662 int ide_do_reset (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 663 {
 664         unsigned int unit;
 665         unsigned long flags;
 666         ide_hwif_t *hwif = HWIF(drive);
 667         ide_hwgroup_t *hwgroup = HWGROUP(drive);
 668 
 669         save_flags(flags);
 670         cli();          /* Why ? */
 671 
 672 #ifdef CONFIG_BLK_DEV_IDECD
 673         /* For an ATAPI device, first try an ATAPI SRST. */
 674         if (drive->media == cdrom) {
 675                 if (!hwgroup->doing_atapi_reset) {
 676                         hwgroup->doing_atapi_reset = 1;
 677                         if (!drive->keep_settings)
 678                                 drive->unmask = 0;
 679                         OUT_BYTE (drive->select.all, IDE_SELECT_REG);
 680                         OUT_BYTE (WIN_SRST, IDE_COMMAND_REG);
 681                         udelay (10);
 682                         hwgroup->reset_timeout = jiffies + WAIT_WORSTCASE;
 683                         start_reset_timer (hwif); /* begin periodic polling */
 684                         restore_flags (flags);
 685                         return 1;
 686                 }
 687         }
 688         hwgroup->doing_atapi_reset = 0;
 689 #endif /* CONFIG_BLK_DEV_IDECD */
 690 
 691         /*
 692          * First, reset any device state data we were maintaining
 693          * for any of the drives on this interface.
 694          */
 695         for (unit = 0; unit < MAX_DRIVES; ++unit) {
 696                 ide_drive_t *rdrive = &hwif->drives[unit];
 697                 rdrive->special.b.set_geometry = 1;
 698                 rdrive->special.b.recalibrate  = 1;
 699                 rdrive->special.b.set_multmode = 0;
 700                 if (OK_TO_RESET_CONTROLLER)
 701                         rdrive->mult_count = 0;
 702                 if (!rdrive->keep_settings) {
 703                         rdrive->mult_req = 0;
 704                         rdrive->unmask = 0;
 705                 }
 706                 if (rdrive->mult_req != rdrive->mult_count)
 707                         rdrive->special.b.set_multmode = 1;
 708         }
 709 
 710 #if OK_TO_RESET_CONTROLLER
 711         /*
 712          * Note that we also set nIEN while resetting the device,
 713          * to mask unwanted interrupts from the interface during the reset.
 714          * However, due to the design of PC hardware, this will cause an
 715          * immediate interrupt due to the edge transition it produces.
 716          * This single interrupt gives us a "fast poll" for drives that
 717          * recover from reset very quickly, saving us the first 50ms wait time.
 718          */
 719         OUT_BYTE(drive->ctl|6,IDE_CONTROL_REG); /* set SRST and nIEN */
 720         udelay(5);                      /* more than enough time */
 721         OUT_BYTE(drive->ctl|2,IDE_CONTROL_REG); /* clear SRST, leave nIEN */
 722         hwgroup->reset_timeout = jiffies + WAIT_WORSTCASE;
 723         start_reset_timer (hwif);       /* begin periodic polling */
 724 #endif  /* OK_TO_RESET_CONTROLLER */
 725 
 726         restore_flags (flags);
 727         return OK_TO_RESET_CONTROLLER;  /* 1 = we are waiting, 0 = done */
 728 }
 729 
 730 /*
 731  * Clean up after success/failure of an explicit (ioctl) drive cmd
 732  */
 733 static void end_drive_cmd (ide_drive_t *drive, byte stat, byte err)
     /* [previous][next][first][last][top][bottom][index][help] */
 734 {
 735         unsigned long flags;
 736         struct request *rq = HWGROUP(drive)->rq;
 737         byte *args = (byte *) rq->buffer;
 738 
 739         rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
 740         if (args) {
 741                 args[0] = stat;
 742                 args[1] = err;
 743                 args[2] = IN_BYTE(IDE_NSECTOR_REG);
 744         }
 745         save_flags(flags);
 746         cli();
 747         up(rq->sem);
 748         HWGROUP(drive)->rq = NULL;
 749         restore_flags(flags);
 750 }
 751 
 752 /*
 753  * Error reporting, in human readable form (luxurious, but a memory hog).
 754  */
 755 byte ide_dump_status (ide_drive_t *drive, const char *msg, byte stat)
     /* [previous][next][first][last][top][bottom][index][help] */
 756 {
 757         unsigned long flags;
 758         byte err = 0;
 759 
 760         save_flags (flags);
 761         sti();
 762         printk("%s: %s: status=0x%02x", drive->name, msg, stat);
 763 #if FANCY_STATUS_DUMPS
 764         if (drive->media == disk) {
 765                 printk(" { ");
 766                 if (stat & BUSY_STAT)
 767                         printk("Busy ");
 768                 else {
 769                         if (stat & READY_STAT)  printk("DriveReady ");
 770                         if (stat & WRERR_STAT)  printk("DeviceFault ");
 771                         if (stat & SEEK_STAT)   printk("SeekComplete ");
 772                         if (stat & DRQ_STAT)    printk("DataRequest ");
 773                         if (stat & ECC_STAT)    printk("CorrectedError ");
 774                         if (stat & INDEX_STAT)  printk("Index ");
 775                         if (stat & ERR_STAT)    printk("Error ");
 776                 }
 777                 printk("}");
 778         }
 779 #endif  /* FANCY_STATUS_DUMPS */
 780         printk("\n");
 781         if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
 782                 err = GET_ERR();
 783                 printk("%s: %s: error=0x%02x", drive->name, msg, err);
 784 #if FANCY_STATUS_DUMPS
 785                 if (drive->media == disk) {
 786                         printk(" { ");
 787                         if (err & BBD_ERR)      printk("BadSector ");
 788                         if (err & ECC_ERR)      printk("UncorrectableError ");
 789                         if (err & ID_ERR)       printk("SectorIdNotFound ");
 790                         if (err & ABRT_ERR)     printk("DriveStatusError ");
 791                         if (err & TRK0_ERR)     printk("TrackZeroNotFound ");
 792                         if (err & MARK_ERR)     printk("AddrMarkNotFound ");
 793                         printk("}");
 794                         if (err & (BBD_ERR|ECC_ERR|ID_ERR|MARK_ERR)) {
 795                                 byte cur = IN_BYTE(IDE_SELECT_REG);
 796                                 if (cur & 0x40) {       /* using LBA? */
 797                                         printk(", LBAsect=%ld", (unsigned long)
 798                                          ((cur&0xf)<<24)
 799                                          |(IN_BYTE(IDE_HCYL_REG)<<16)
 800                                          |(IN_BYTE(IDE_LCYL_REG)<<8)
 801                                          | IN_BYTE(IDE_SECTOR_REG));
 802                                 } else {
 803                                         printk(", CHS=%d/%d/%d",
 804                                          (IN_BYTE(IDE_HCYL_REG)<<8) +
 805                                           IN_BYTE(IDE_LCYL_REG),
 806                                           cur & 0xf,
 807                                           IN_BYTE(IDE_SECTOR_REG));
 808                                 }
 809                                 if (HWGROUP(drive)->rq)
 810                                         printk(", sector=%ld", HWGROUP(drive)->rq->sector);
 811                         }
 812                 }
 813 #endif  /* FANCY_STATUS_DUMPS */
 814                 printk("\n");
 815         }
 816         restore_flags (flags);
 817         return err;
 818 }
 819 
 820 /*
 821  * try_to_flush_leftover_data() is invoked in response to a drive
 822  * unexpectedly having its DRQ_STAT bit set.  As an alternative to
 823  * resetting the drive, this routine tries to clear the condition
 824  * by read a sector's worth of data from the drive.  Of course,
 825  * this may not help if the drive is *waiting* for data from *us*.
 826  */
 827 static void try_to_flush_leftover_data (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 828 {
 829         int i = (drive->mult_count ? drive->mult_count : 1) * SECTOR_WORDS;
 830 
 831         while (i > 0) {
 832                 unsigned long buffer[16];
 833                 unsigned int wcount = (i > 16) ? 16 : i;
 834                 i -= wcount;
 835                 ide_input_data (drive, buffer, wcount);
 836         }
 837 }
 838 
 839 /*
 840  * ide_error() takes action based on the error returned by the controller.
 841  *
 842  * Returns 1 if an ide reset operation has been initiated, in which case
 843  * the caller MUST simply return from the driver (through however many levels).
 844  * Returns 0 otherwise.
 845  */
 846 int ide_error (ide_drive_t *drive, const char *msg, byte stat)
     /* [previous][next][first][last][top][bottom][index][help] */
 847 {
 848         struct request *rq;
 849         byte err;
 850 
 851         err = ide_dump_status(drive, msg, stat);
 852         if ((rq = HWGROUP(drive)->rq) == NULL || drive == NULL)
 853                 return 0;
 854 #ifdef IDE_DRIVE_CMD
 855         if (rq->cmd == IDE_DRIVE_CMD) { /* never retry an explicit DRIVE_CMD */
 856                 end_drive_cmd(drive, stat, err);
 857                 return 0;
 858         }
 859 #endif  /* IDE_DRIVE_CMD */
 860         if (stat & BUSY_STAT) {         /* other bits are useless when BUSY */
 861                 rq->errors |= ERROR_RESET;
 862         } else {
 863                 if (drive->media == disk && (stat & ERR_STAT)) {
 864                         /* err has different meaning on cdrom */
 865                         if (err & BBD_ERR)              /* retries won't help this! */
 866                                 rq->errors = ERROR_MAX;
 867                         else if (err & TRK0_ERR)        /* help it find track zero */
 868                                 rq->errors |= ERROR_RECAL;
 869                 }
 870                 if ((stat & DRQ_STAT) && rq->cmd != WRITE)
 871                         try_to_flush_leftover_data(drive);
 872         }
 873         if (GET_STAT() & (BUSY_STAT|DRQ_STAT))
 874                 rq->errors |= ERROR_RESET;      /* Mmmm.. timing problem */
 875 
 876 #ifdef CONFIG_BLK_DEV_TRITON
 877         if (rq->errors > 3 && drive->using_dma) {       /* DMA troubles? */
 878                 drive->using_dma = 0;
 879                 printk("%s: DMA disabled\n", drive->name);
 880                 --rq->errors;
 881                 return 0;
 882         }
 883 #endif /* CONFIG_BLK_DEV_TRITON */
 884         if (rq->errors >= ERROR_MAX)
 885                 ide_end_request(0, HWGROUP(drive));
 886         else {
 887                 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
 888                         ++rq->errors;
 889                         return ide_do_reset(drive);
 890                 } else if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
 891                         drive->special.b.recalibrate = 1;
 892                 ++rq->errors;
 893         }
 894         return 0;
 895 }
 896 
 897 /*
 898  * read_intr() is the handler for disk read/multread interrupts
 899  */
 900 static void read_intr (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 901 {
 902         byte stat;
 903         int i;
 904         unsigned int msect, nsect;
 905         struct request *rq;
 906 
 907         if (!OK_STAT(stat=GET_STAT(),DATA_READY,BAD_R_STAT)) {
 908                 sti();
 909                 if (!ide_error(drive, "read_intr", stat))
 910                         IDE_DO_REQUEST;
 911                 return;
 912         }
 913         msect = drive->mult_count;
 914 read_next:
 915         rq = HWGROUP(drive)->rq;
 916         if (msect) {
 917                 if ((nsect = rq->current_nr_sectors) > msect)
 918                         nsect = msect;
 919                 msect -= nsect;
 920         } else
 921                 nsect = 1;
 922         ide_input_data(drive, rq->buffer, nsect * SECTOR_WORDS);
 923 #ifdef DEBUG
 924         printk("%s:  read: sectors(%ld-%ld), buffer=0x%08lx, remaining=%ld\n",
 925                 drive->name, rq->sector, rq->sector+nsect-1,
 926                 (unsigned long) rq->buffer+(nsect<<9), rq->nr_sectors-nsect);
 927 #endif
 928         rq->sector += nsect;
 929         rq->buffer += nsect<<9;
 930         rq->errors = 0;
 931         i = (rq->nr_sectors -= nsect);
 932         if ((rq->current_nr_sectors -= nsect) <= 0)
 933                 ide_end_request(1, HWGROUP(drive));
 934         if (i > 0) {
 935                 if (msect)
 936                         goto read_next;
 937                 ide_set_handler (drive, &read_intr);
 938                 return;
 939         }
 940         IDE_DO_REQUEST;
 941 }
 942 
 943 /*
 944  * write_intr() is the handler for disk write interrupts
 945  */
 946 static void write_intr (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 947 {
 948         byte stat;
 949         int i;
 950         struct request *rq = HWGROUP(drive)->rq;
 951 
 952         if (OK_STAT(stat=GET_STAT(),DRIVE_READY,drive->bad_wstat)) {
 953 #ifdef DEBUG
 954                 printk("%s: write: sector %ld, buffer=0x%08lx, remaining=%ld\n",
 955                         drive->name, rq->sector, (unsigned long) rq->buffer,
 956                         rq->nr_sectors-1);
 957 #endif
 958                 if ((rq->nr_sectors == 1) ^ ((stat & DRQ_STAT) != 0)) {
 959                         rq->sector++;
 960                         rq->buffer += 512;
 961                         rq->errors = 0;
 962                         i = --rq->nr_sectors;
 963                         --rq->current_nr_sectors;
 964                         if (rq->current_nr_sectors <= 0)
 965                                 ide_end_request(1, HWGROUP(drive));
 966                         if (i > 0) {
 967                                 ide_output_data (drive, rq->buffer, SECTOR_WORDS);
 968                                 ide_set_handler (drive, &write_intr);
 969                                 return;
 970                         }
 971                         IDE_DO_REQUEST;
 972                         return;
 973                 }
 974         }
 975         sti();
 976         if (!ide_error(drive, "write_intr", stat))
 977                 IDE_DO_REQUEST;
 978 }
 979 
 980 /*
 981  * multwrite() transfers a block of one or more sectors of data to a drive
 982  * as part of a disk multwrite operation.  
 983  */
 984 static void multwrite (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
 985 {
 986         struct request *rq = &HWGROUP(drive)->wrq;
 987         unsigned int mcount = drive->mult_count;
 988 
 989         do {
 990                 unsigned int nsect = rq->current_nr_sectors;
 991                 if (nsect > mcount)
 992                         nsect = mcount;
 993                 mcount -= nsect;
 994 
 995                 ide_output_data(drive, rq->buffer, nsect<<7);
 996 #ifdef DEBUG
 997                 printk("%s: multwrite: sector %ld, buffer=0x%08lx, count=%d, remaining=%ld\n",
 998                         drive->name, rq->sector, (unsigned long) rq->buffer,
 999                         nsect, rq->nr_sectors - nsect);
1000 #endif
1001                 if ((rq->nr_sectors -= nsect) <= 0)
1002                         break;
1003                 if ((rq->current_nr_sectors -= nsect) == 0) {
1004                         if ((rq->bh = rq->bh->b_reqnext) != NULL) {
1005                                 rq->current_nr_sectors = rq->bh->b_size>>9;
1006                                 rq->buffer             = rq->bh->b_data;
1007                         } else {
1008                                 panic("%s: buffer list corrupted\n", drive->name);
1009                                 break;
1010                         }
1011                 } else {
1012                         rq->buffer += nsect << 9;
1013                 }
1014         } while (mcount);
1015 }
1016 
1017 /*
1018  * write_intr() is the handler for disk multwrite interrupts
1019  */
1020 static void multwrite_intr (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1021 {
1022         byte stat;
1023         int i;
1024         struct request *rq = &HWGROUP(drive)->wrq;
1025 
1026         if (OK_STAT(stat=GET_STAT(),DRIVE_READY,drive->bad_wstat)) {
1027                 if (stat & DRQ_STAT) {
1028                         if (rq->nr_sectors) {
1029                                 multwrite(drive);
1030                                 ide_set_handler (drive, &multwrite_intr);
1031                                 return;
1032                         }
1033                 } else {
1034                         if (!rq->nr_sectors) {  /* all done? */
1035                                 rq = HWGROUP(drive)->rq;
1036                                 for (i = rq->nr_sectors; i > 0;){
1037                                         i -= rq->current_nr_sectors;
1038                                         ide_end_request(1, HWGROUP(drive));
1039                                 }
1040                                 IDE_DO_REQUEST;
1041                                 return;
1042                         }
1043                 }
1044         }
1045         sti();
1046         if (!ide_error(drive, "multwrite_intr", stat))
1047                 IDE_DO_REQUEST;
1048 }
1049 
1050 /*
1051  * Issue a simple drive command
1052  * The drive must be selected beforehand.
1053  */
1054 static void ide_cmd(ide_drive_t *drive, byte cmd, byte nsect, ide_handler_t *handler)
     /* [previous][next][first][last][top][bottom][index][help] */
1055 {
1056         ide_set_handler (drive, handler);
1057         OUT_BYTE(drive->ctl,IDE_CONTROL_REG);
1058         OUT_BYTE(nsect,IDE_NSECTOR_REG);
1059         OUT_BYTE(cmd,IDE_COMMAND_REG);
1060 }
1061 
1062 /*
1063  * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
1064  */
1065 static void set_multmode_intr (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1066 {
1067         byte stat = GET_STAT();
1068 
1069         sti();
1070         if (OK_STAT(stat,READY_STAT,BAD_STAT)) {
1071                 drive->mult_count = drive->mult_req;
1072         } else {
1073                 drive->mult_req = drive->mult_count = 0;
1074                 drive->special.b.recalibrate = 1;
1075                 (void) ide_dump_status(drive, "set_multmode", stat);
1076         }
1077         IDE_DO_REQUEST;
1078 }
1079 
1080 /*
1081  * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
1082  */
1083 static void set_geometry_intr (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1084 {
1085         byte stat = GET_STAT();
1086 
1087         sti();
1088         if (!OK_STAT(stat,READY_STAT,BAD_STAT))
1089                 if (ide_error(drive, "set_geometry_intr", stat))
1090                         return;
1091         IDE_DO_REQUEST;
1092 }
1093 
1094 /*
1095  * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
1096  */
1097 static void recal_intr (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1098 {
1099         byte stat = GET_STAT();
1100 
1101         sti();
1102         if (!OK_STAT(stat,READY_STAT,BAD_STAT))
1103                 if (ide_error(drive, "recal_intr", stat))
1104                         return;
1105         IDE_DO_REQUEST;
1106 }
1107 
1108 #ifdef IDE_DRIVE_CMD
1109 /*
1110  * drive_cmd_intr() is invoked on completion of a special DRIVE_CMD.
1111  */
1112 static void drive_cmd_intr (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1113 {
1114         byte stat = GET_STAT();
1115 
1116         sti();
1117         if (OK_STAT(stat,READY_STAT,BAD_STAT))
1118                 end_drive_cmd (drive, stat, GET_ERR());
1119         else if (ide_error(drive, "drive_cmd", stat)) /* calls end_drive_cmd */
1120                 return;
1121         IDE_DO_REQUEST;
1122 }
1123 #endif  /* IDE_DRIVE_CMD */
1124 
1125 /*
1126  * do_special() is used to issue WIN_SPECIFY, WIN_RESTORE, and WIN_SETMULT
1127  * commands to a drive.  It used to do much more, but has been scaled back
1128  * in recent updates, and could be completely eliminated with a bit more effort.
1129  */
1130 static inline void do_special (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
1131 {
1132         special_t *s = &drive->special;
1133 #ifdef DEBUG
1134         printk("%s: do_special: 0x%02x\n", drive->name, s->all);
1135 #endif
1136         if (s->b.set_geometry) {
1137                 s->b.set_geometry = 0;
1138                 if (drive->media == disk) {
1139                         OUT_BYTE(drive->sect,IDE_SECTOR_REG);
1140                         OUT_BYTE(drive->cyl,IDE_LCYL_REG);
1141                         OUT_BYTE(drive->cyl>>8,IDE_HCYL_REG);
1142                         OUT_BYTE(((drive->head-1)|drive->select.all)&0xBF,IDE_SELECT_REG);
1143                         ide_cmd(drive, WIN_SPECIFY, drive->sect, &set_geometry_intr);
1144                 }
1145         } else if (s->b.recalibrate) {
1146                 s->b.recalibrate = 0;
1147                 if (drive->media == disk) {
1148                         ide_cmd(drive, WIN_RESTORE, drive->sect, &recal_intr);
1149                 }
1150         } else if (s->b.set_multmode) {
1151                 s->b.set_multmode = 0;
1152                 if (drive->media == disk) {
1153                         if (drive->id && drive->mult_req > drive->id->max_multsect)
1154                                 drive->mult_req = drive->id->max_multsect;
1155                         ide_cmd(drive, WIN_SETMULT, drive->mult_req, &set_multmode_intr);
1156                 } else
1157                         drive->mult_req = 0;
1158         } else if (s->all) {
1159                 s->all = 0;
1160                 printk("%s: bad special flag: 0x%02x\n", drive->name, s->all);
1161         }
1162 }
1163 
1164 /*
1165  * This routine busy-waits for the drive status to be not "busy".
1166  * It then checks the status for all of the "good" bits and none
1167  * of the "bad" bits, and if all is okay it returns 0.  All other
1168  * cases return 1 after invoking ide_error()
1169  *
1170  * This routine should get fixed to not hog the cpu during extra long waits..
1171  * That could be done by busy-waiting for the first jiffy or two, and then
1172  * setting a timer to wake up at half second intervals thereafter,
1173  * until WAIT_WORSTCASE is achieved, before timing out.
1174  */
1175 int ide_wait_stat (ide_drive_t *drive, byte good, byte bad, unsigned long timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
1176 {
1177         byte stat;
1178 
1179         udelay(1);      /* spec allows drive 400ns to assert "BUSY" */
1180         if (GET_STAT() & BUSY_STAT) {
1181                 unsigned long flags;
1182                 save_flags(flags);
1183                 sti();
1184                 timeout += jiffies;
1185                 while (GET_STAT() & BUSY_STAT) {
1186                         if (jiffies > timeout) {
1187                                 restore_flags(flags);
1188                                 (void) ide_error(drive, "status timeout", GET_STAT());
1189                                 return 1;
1190                         }
1191                 }
1192                 restore_flags(flags);
1193                 udelay(1);      /* spec allows 400ns for status to stabilize */
1194         }
1195         if (OK_STAT(stat=GET_STAT(), good, bad))
1196                 return 0;
1197         (void) ide_error(drive, "status error", stat);
1198         return 1;
1199 }
1200 
1201 /*
1202  * do_rw_disk() issues WIN_{MULT}READ and WIN_{MULT}WRITE commands to a disk,
1203  * using LBA if supported, or CHS otherwise, to address sectors.  It also takes
1204  * care of issuing special DRIVE_CMDs.
1205  */
1206 static inline void do_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
     /* [previous][next][first][last][top][bottom][index][help] */
1207 {
1208         OUT_BYTE(drive->ctl,IDE_CONTROL_REG);
1209         OUT_BYTE(rq->nr_sectors,IDE_NSECTOR_REG);
1210         if (drive->select.b.lba) {
1211 #ifdef DEBUG
1212                 printk("%s: %sing: LBAsect=%ld, sectors=%ld, buffer=0x%08lx\n",
1213                         drive->name, (rq->cmd==READ)?"read":"writ", 
1214                         block, rq->nr_sectors, (unsigned long) rq->buffer);
1215 #endif
1216                 OUT_BYTE(block,IDE_SECTOR_REG);
1217                 OUT_BYTE(block>>=8,IDE_LCYL_REG);
1218                 OUT_BYTE(block>>=8,IDE_HCYL_REG);
1219                 OUT_BYTE(((block>>8)&0x0f)|drive->select.all,IDE_SELECT_REG);
1220         } else {
1221                 unsigned int sect,head,cyl,track;
1222                 track = block / drive->sect;
1223                 sect  = block % drive->sect + 1;
1224                 OUT_BYTE(sect,IDE_SECTOR_REG);
1225                 head  = track % drive->head;
1226                 cyl   = track / drive->head;
1227                 OUT_BYTE(cyl,IDE_LCYL_REG);
1228                 OUT_BYTE(cyl>>8,IDE_HCYL_REG);
1229                 OUT_BYTE(head|drive->select.all,IDE_SELECT_REG);
1230 #ifdef DEBUG
1231                 printk("%s: %sing: CHS=%d/%d/%d, sectors=%ld, buffer=0x%08lx\n",
1232                         drive->name, (rq->cmd==READ)?"read":"writ", cyl,
1233                         head, sect, rq->nr_sectors, (unsigned long) rq->buffer);
1234 #endif
1235         }
1236         if (rq->cmd == READ) {
1237 #ifdef CONFIG_BLK_DEV_TRITON
1238                 if (drive->using_dma && !(HWIF(drive)->dmaproc(ide_dma_read, drive)))
1239                         return;
1240 #endif /* CONFIG_BLK_DEV_TRITON */
1241                 ide_set_handler(drive, &read_intr);
1242                 OUT_BYTE(drive->mult_count ? WIN_MULTREAD : WIN_READ, IDE_COMMAND_REG);
1243                 return;
1244         }
1245         if (rq->cmd == WRITE) {
1246 #ifdef CONFIG_BLK_DEV_TRITON
1247                 if (drive->using_dma && !(HWIF(drive)->dmaproc(ide_dma_write, drive)))
1248                         return;
1249 #endif /* CONFIG_BLK_DEV_TRITON */
1250                 OUT_BYTE(drive->mult_count ? WIN_MULTWRITE : WIN_WRITE, IDE_COMMAND_REG);
1251                 if (ide_wait_stat(drive, DATA_READY, drive->bad_wstat, WAIT_DRQ)) {
1252                         printk("%s: no DRQ after issuing %s\n", drive->name,
1253                                 drive->mult_count ? "MULTWRITE" : "WRITE");
1254                         return;
1255                 }
1256                 if (!drive->unmask)
1257                         cli();
1258                 if (drive->mult_count) {
1259                         HWGROUP(drive)->wrq = *rq; /* scratchpad */
1260                         ide_set_handler (drive, &multwrite_intr);
1261                         multwrite(drive);
1262                 } else {
1263                         ide_set_handler (drive, &write_intr);
1264                         ide_output_data(drive, rq->buffer, SECTOR_WORDS);
1265                 }
1266                 return;
1267         }
1268 #ifdef IDE_DRIVE_CMD
1269         if (rq->cmd == IDE_DRIVE_CMD) {
1270                 byte *args = rq->buffer;
1271                 if (args) {
1272                         printk("%s: DRIVE_CMD cmd=0x%02x sc=0x%02x fr=0x%02x\n",
1273                          drive->name, args[0], args[1], args[2]);
1274                         OUT_BYTE(args[2],IDE_FEATURE_REG);
1275                         ide_cmd(drive, args[0], args[1], &drive_cmd_intr);
1276                         return;
1277                 } else {
1278                         /*
1279                          * NULL is actually a valid way of waiting for
1280                          * all current requests to be flushed from the queue.
1281                          */
1282 #ifdef DEBUG
1283                         printk("%s: DRIVE_CMD (null)\n", drive->name);
1284 #endif
1285                         end_drive_cmd(drive, GET_STAT(), GET_ERR());
1286                         return;
1287                 }
1288         }
1289 #endif  /* IDE_DRIVE_CMD */
1290         printk("%s: bad command: %d\n", drive->name, rq->cmd);
1291         ide_end_request(0, HWGROUP(drive));
1292 }
1293 
1294 /*
1295  * do_request() initiates handling of a new I/O request
1296  */
1297 static inline void do_request (ide_hwif_t *hwif, struct request *rq)
     /* [previous][next][first][last][top][bottom][index][help] */
1298 {
1299         unsigned int minor, unit;
1300         unsigned long block, blockend;
1301         ide_drive_t *drive;
1302 
1303         sti();
1304 #ifdef DEBUG
1305         printk("%s: ide_do_request: current=0x%08lx\n", hwif->name, (unsigned long) rq);
1306 #endif
1307         minor = MINOR(rq->dev);
1308         unit = minor >> PARTN_BITS;
1309         if (MAJOR(rq->dev) != hwif->major || unit >= MAX_DRIVES) {
1310                 printk("%s: bad device number: 0x%04x\n", hwif->name, rq->dev);
1311                 goto kill_rq;
1312         }
1313         drive = &hwif->drives[unit];
1314 #ifdef DEBUG
1315         if (rq->bh && !rq->bh->b_lock) {
1316                 printk("%s: block not locked\n", drive->name);
1317                 goto kill_rq;
1318         }
1319 #endif
1320         block    = rq->sector;
1321         blockend = block + rq->nr_sectors;
1322         if ((blockend < block) || (blockend > drive->part[minor&PARTN_MASK].nr_sects)) {
1323                 printk("%s%c: bad access: block=%ld, count=%ld\n", drive->name,
1324                  (minor&PARTN_MASK)?'0'+(minor&PARTN_MASK):' ', block, rq->nr_sectors);
1325                 goto kill_rq;
1326         }
1327         block += drive->part[minor&PARTN_MASK].start_sect + drive->sect0;
1328         ((ide_hwgroup_t *)hwif->hwgroup)->drive = drive;
1329 #if (DISK_RECOVERY_TIME > 0)
1330         while ((read_timer() - hwif->last_time) < DISK_RECOVERY_TIME);
1331 #endif
1332 #if SUPPORT_HT6560B
1333         if (hwif->select)
1334                 ide_hwif_select (hwif);
1335 #endif
1336         OUT_BYTE(drive->select.all,IDE_SELECT_REG);
1337         if (ide_wait_stat(drive, drive->ready_stat, BUSY_STAT|DRQ_STAT, WAIT_READY)) {
1338                 printk("%s: drive not ready for command\n", drive->name);
1339                 return;
1340 
1341         }
1342         if (!drive->special.all) {
1343 #ifdef CONFIG_BLK_DEV_IDECD
1344                 switch (drive->media) {
1345                         case disk:
1346                                 do_rw_disk (drive, rq, block);
1347                                 return;
1348                         case cdrom:
1349                                 ide_do_rw_cdrom (drive, block);
1350                                 return;
1351                         default:
1352                                 printk("%s: media type %d not supported\n",
1353                                         drive->name, drive->media);
1354                                 goto kill_rq;
1355                 }
1356 #else
1357                 do_rw_disk (drive, rq, block); /* simpler and faster */
1358                 return;
1359 #endif;
1360         }
1361         do_special(drive);
1362         return;
1363 kill_rq:
1364         ide_end_request(0, hwif->hwgroup);
1365 }
1366 
1367 /*
1368  * The driver enables interrupts as much as possible.  In order to do this,
1369  * (a) the device-interrupt is always masked before entry, and
1370  * (b) the timeout-interrupt is always disabled before entry.
1371  *
1372  * If we enter here from, say irq14, and then start a new request for irq15,
1373  * (possible with "serialize" option) then we cannot ensure that we exit
1374  * before the irq15 hits us. So, we must be careful not to let this bother us.
1375  *
1376  * Interrupts are still masked (by default) whenever we are exchanging
1377  * data/cmds with a drive, because some drives seem to have very poor
1378  * tolerance for latency during I/O.  For devices which don't suffer from
1379  * this problem (most don't), the unmask flag can be set using the "hdparm"
1380  * utility, to permit other interrupts during data/cmd transfers.
1381  */
1382 void ide_do_request (ide_hwgroup_t *hwgroup)
     /* [previous][next][first][last][top][bottom][index][help] */
1383 {
1384         cli();  /* paranoia */
1385         if (hwgroup->handler != NULL) {
1386                 printk("%s: EEeekk!! handler not NULL in ide_do_request()\n", hwgroup->hwif->name);
1387                 return;
1388         }
1389         do {
1390                 ide_hwif_t *hwif = hwgroup->hwif;
1391                 struct request *rq;
1392                 if ((rq = hwgroup->rq) == NULL) {
1393                         hwgroup->drive = NULL;  /* paranoia */
1394                         do {
1395                                 rq = blk_dev[hwif->major].current_request;
1396                                 if (rq != NULL && rq->dev != -1)
1397                                         goto got_rq;
1398                         } while ((hwif = hwif->next) != hwgroup->hwif);
1399                         return;         /* no work left for this hwgroup */
1400                 got_rq:
1401                         blk_dev[hwif->major].current_request = rq->next;
1402                 }
1403                 do_request(hwgroup->hwif = hwif, hwgroup->rq = rq);
1404                 cli();
1405         } while (hwgroup->handler == NULL);
1406 }
1407 
1408 /*
1409  * do_hwgroup_request() invokes ide_do_request() after first masking
1410  * all possible interrupts for the current hwgroup.  This prevents race
1411  * conditions in the event that an unexpected interrupt occurs while
1412  * we are in the driver.
1413  *
1414  * Note that when an interrupt is used to reenter the driver, the first level
1415  * handler will already have masked the irq that triggered, but any other ones
1416  * for the hwgroup will still be unmasked.  The driver tries to be careful
1417  * about such things.
1418  */
1419 static void do_hwgroup_request (ide_hwgroup_t *hwgroup)
     /* [previous][next][first][last][top][bottom][index][help] */
1420 {
1421         if (hwgroup->handler == NULL) {
1422                 ide_hwif_t *hgif = hwgroup->hwif;
1423                 ide_hwif_t *hwif = hgif;
1424                 do {
1425                         disable_irq(hwif->irq);
1426                 } while ((hwif = hwif->next) != hgif);
1427                 ide_do_request (hwgroup);
1428                 do {
1429                         enable_irq(hwif->irq);
1430                 } while ((hwif = hwif->next) != hgif);
1431         }
1432 }
1433 
1434 static void do_ide0_request (void)      /* invoked with cli() */
     /* [previous][next][first][last][top][bottom][index][help] */
1435 {
1436         do_hwgroup_request (ide_hwifs[0].hwgroup);
1437 }
1438 
1439 static void do_ide1_request (void)      /* invoked with cli() */
     /* [previous][next][first][last][top][bottom][index][help] */
1440 {
1441         do_hwgroup_request (ide_hwifs[1].hwgroup);
1442 }
1443 
1444 static void do_ide2_request (void)      /* invoked with cli() */
     /* [previous][next][first][last][top][bottom][index][help] */
1445 {
1446         do_hwgroup_request (ide_hwifs[2].hwgroup);
1447 }
1448 
1449 static void do_ide3_request (void)      /* invoked with cli() */
     /* [previous][next][first][last][top][bottom][index][help] */
1450 {
1451         do_hwgroup_request (ide_hwifs[3].hwgroup);
1452 }
1453 
1454 static void timer_expiry (unsigned long data)
     /* [previous][next][first][last][top][bottom][index][help] */
1455 {
1456         ide_hwgroup_t *hwgroup = (ide_hwgroup_t *) data;
1457         ide_drive_t   *drive   = hwgroup->drive;
1458         unsigned long flags;
1459 
1460         save_flags(flags);
1461         cli();
1462 
1463         if (hwgroup->reset_timeout != 0) { /* ide reset in progress? */
1464                 if (!reset_handler(hwgroup))
1465                         do_hwgroup_request (hwgroup);
1466         } else if (hwgroup->handler == NULL) {   /* not waiting for anything? */
1467                 sti(); /* drive must have responded just as the timer expired */
1468                 printk("%s: marginal timeout\n", drive->name);
1469         } else {                                 /* drive not responding */
1470                 hwgroup->handler = NULL;
1471                 if (hwgroup->hwif->dmaproc) 
1472                         (void) hwgroup->hwif->dmaproc (ide_dma_abort, drive);
1473                 if (!ide_error(drive, "irq timeout", GET_STAT()))
1474                         do_hwgroup_request (hwgroup);
1475         }
1476         restore_flags(flags);
1477 }
1478 
1479 /*
1480  * There's nothing really useful we can do with an unexpected interrupt,
1481  * other than reading the status register (to clear it), and logging it.
1482  * There should be no way that an irq can happen before we're ready for it,
1483  * so we needn't worry much about losing an "important" interrupt here.
1484  *
1485  * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
1486  * drive enters "idle", "standby", or "sleep" mode, so if the status looks
1487  * "good", we just ignore the interrupt completely.
1488  *
1489  * This routine assumes cli() is in effect when called.
1490  *
1491  * If an unexpected interrupt happens on irq15 while we are handling irq14
1492  * and if the two interfaces are "serialized" (CMD640B), then it looks like
1493  * we could screw up by interfering with a new request being set up for irq15.
1494  *
1495  * In reality, this is a non-issue.  The new command is not sent unless the
1496  * drive is ready to accept one, in which case we know the drive is not
1497  * trying to interrupt us.  And ide_set_handler() is always invoked before
1498  * completing the issuance of any new drive command, so we will not be 
1499  * accidently invoked as a result of any valid command completion interrupt.
1500  *
1501  */
1502 static void unexpected_intr (int irq, ide_hwgroup_t *hwgroup)
     /* [previous][next][first][last][top][bottom][index][help] */
1503 {
1504         byte stat;
1505         unsigned int unit;
1506         ide_hwif_t *hwif = hwgroup->hwif;
1507 
1508         /*
1509          * check for ide reset in progress
1510          */
1511         if (hwgroup->reset_timeout != 0) {
1512                 if (!reset_handler(hwgroup))
1513                         do_hwgroup_request (hwgroup);
1514                 return;
1515         }
1516 
1517         /*
1518          * handle the unexpected interrupt
1519          */
1520         do {
1521                 if (hwif->irq == irq) {
1522 #if SUPPORT_HT6560B
1523                         if (hwif->select)
1524                                 ide_hwif_select (hwif);
1525 #endif
1526                         for (unit = 0; unit < MAX_DRIVES; ++unit) {
1527                                 ide_drive_t *drive = &hwif->drives[unit];
1528                                 if (!drive->present)
1529                                         continue;
1530                                 if (!OK_STAT(stat=GET_STAT(), drive->ready_stat, BAD_STAT))
1531                                         (void) ide_dump_status(drive, "unexpected_intr", stat);
1532                                 if ((stat & DRQ_STAT))
1533                                         try_to_flush_leftover_data(drive);
1534                         }
1535                 }
1536         } while ((hwif = hwif->next) != hwgroup->hwif);
1537 }
1538 
1539 /*
1540  * entry point for all interrupts, caller does cli() for us
1541  */
1542 static void ide_intr (int irq, struct pt_regs *regs)
     /* [previous][next][first][last][top][bottom][index][help] */
1543 {
1544         ide_hwgroup_t  *hwgroup = irq_to_hwgroup[irq];
1545         ide_handler_t  *handler;
1546 
1547         if (irq == hwgroup->hwif->irq && (handler = hwgroup->handler) != NULL) {
1548                 ide_drive_t *drive = hwgroup->drive;
1549                 hwgroup->handler = NULL;
1550                 del_timer(&(hwgroup->timer));
1551                 if (drive->unmask)
1552                         sti();
1553                 handler(drive);
1554         } else {
1555                 unexpected_intr(irq, hwgroup);
1556         }
1557         cli();
1558 }
1559 
1560 /*
1561  * get_info_ptr() returns the (ide_drive_t *) for a given device number.
1562  * It returns NULL if the given device number does not match any present drives.
1563  */
1564 static ide_drive_t *get_info_ptr (int i_rdev)
     /* [previous][next][first][last][top][bottom][index][help] */
1565 {
1566         int             major = MAJOR(i_rdev);
1567         unsigned int    h;
1568 
1569         for (h = 0; h < MAX_HWIFS; ++h) {
1570                 ide_hwif_t  *hwif = &ide_hwifs[h];
1571                 if (hwif->present && major == hwif->major) {
1572                         unsigned unit = DEVICE_NR(i_rdev);
1573                         if (unit < MAX_DRIVES) {
1574                                 ide_drive_t *drive = &hwif->drives[unit];
1575                                 if (drive->present)
1576                                         return drive;
1577                         }
1578                         break;
1579                 }
1580         }
1581         return NULL;
1582 }
1583 
1584 #ifdef IDE_DRIVE_CMD
1585 /*
1586  * This function issues a specific IDE drive command onto the
1587  * tail of the request queue, and waits for it to be completed.
1588  * If arg is NULL, it goes through all the motions,
1589  * but without actually sending a command to the drive.
1590  */
1591 static int do_drive_cmd(int rdev, char *args)
     /* [previous][next][first][last][top][bottom][index][help] */
1592 {
1593         unsigned long flags;
1594         unsigned int major = MAJOR(rdev);
1595         struct request rq, *cur_rq;
1596         struct blk_dev_struct *bdev;
1597         struct semaphore sem = MUTEX_LOCKED;
1598 
1599         /* build up a special request, and add it to the queue */
1600         rq.buffer = args;
1601         rq.cmd = IDE_DRIVE_CMD;
1602         rq.errors = 0;
1603         rq.sector = 0;
1604         rq.nr_sectors = 0;
1605         rq.current_nr_sectors = 0;
1606         rq.sem = &sem;
1607         rq.bh = NULL;
1608         rq.bhtail = NULL;
1609         rq.next = NULL;
1610         rq.dev = rdev;
1611         bdev = &blk_dev[major];
1612 
1613         save_flags(flags);
1614         cli();
1615         cur_rq = bdev->current_request;
1616         if (cur_rq == NULL) {                   /* empty request list? */
1617                 bdev->current_request = &rq;    /* service ours immediately */
1618                 bdev->request_fn();
1619         } else {
1620                 while (cur_rq->next != NULL)    /* find end of request list */
1621                         cur_rq = cur_rq->next;
1622                 cur_rq->next = &rq;             /* add rq to the end */
1623         }
1624 
1625         down(&sem);                             /* wait for it to be serviced */
1626         restore_flags(flags);
1627         return rq.errors ? -EIO : 0;            /* return -EIO if errors */
1628 }
1629 #endif  /* IDE_DRIVE_CMD */
1630 
1631 static int ide_open(struct inode * inode, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
1632 {
1633         ide_drive_t *drive;
1634         unsigned long flags;
1635 
1636         if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
1637                 return -ENODEV;
1638         save_flags(flags);
1639         cli();
1640         while (drive->busy)
1641                 sleep_on(&drive->wqueue);
1642         drive->usage++;
1643         restore_flags(flags);
1644 #ifdef CONFIG_BLK_DEV_IDECD
1645         if (drive->media == cdrom)
1646                 return ide_cdrom_open (inode, filp, drive);
1647 #endif  /* CONFIG_BLK_DEV_IDECD */
1648         if (drive->removeable) {
1649                 check_disk_change(inode->i_rdev);
1650 #ifdef IDE_DRIVE_CMD
1651                 {
1652                 byte door_lock[] = {WIN_DOORLOCK,0,0,0};
1653                 do_drive_cmd(inode->i_rdev, door_lock);
1654                 }
1655 #endif /* IDE_DRIVE_CMD */
1656         }
1657         return 0;
1658 }
1659 
1660 /*
1661  * Releasing a block device means we sync() it, so that it can safely
1662  * be forgotten about...
1663  */
1664 static void ide_release(struct inode * inode, struct file * file)
     /* [previous][next][first][last][top][bottom][index][help] */
1665 {
1666         ide_drive_t *drive;
1667 
1668         if ((drive = get_info_ptr(inode->i_rdev)) != NULL) {
1669                 sync_dev(inode->i_rdev);
1670                 drive->usage--;
1671 #ifdef CONFIG_BLK_DEV_IDECD
1672                 if (drive->media == cdrom)
1673                         ide_cdrom_release (inode, file, drive);
1674                 else
1675 #endif  /* CONFIG_BLK_DEV_IDECD */
1676                 if (drive->removeable) {
1677                         invalidate_buffers(inode->i_rdev);
1678 #ifdef IDE_DRIVE_CMD
1679                         {
1680                         byte door_unlock[] = {WIN_DOORUNLOCK,0,0,0};
1681                         do_drive_cmd(inode->i_rdev, door_unlock);
1682                         }
1683 #endif /* IDE_DRIVE_CMD */
1684                 }
1685         }
1686 }
1687 
1688 /*
1689  * This routine is called to flush all partitions and partition tables
1690  * for a changed disk, and then re-read the new partition table.
1691  * If we are revalidating a disk because of a media change, then we
1692  * enter with usage == 0.  If we are using an ioctl, we automatically have
1693  * usage == 1 (we need an open channel to use an ioctl :-), so this
1694  * is our limit.
1695  */
1696 static int revalidate_disk(dev_t  i_rdev)
     /* [previous][next][first][last][top][bottom][index][help] */
1697 {
1698         ide_drive_t *drive;
1699         unsigned int p, major, minor;
1700         long flags;
1701 
1702         if ((drive = get_info_ptr(i_rdev)) == NULL)
1703                 return -ENODEV;
1704 
1705         major = MAJOR(i_rdev) << 8;
1706         minor = drive->select.b.unit << PARTN_BITS;
1707         save_flags(flags);
1708         cli();
1709         if (drive->busy || (drive->usage > 1)) {
1710                 restore_flags(flags);
1711                 return -EBUSY;
1712         };
1713         drive->busy = 1;
1714         restore_flags(flags);
1715 
1716         for (p = 0; p < (1<<PARTN_BITS); ++p) {
1717                 sync_dev           (major | (minor + p));
1718                 invalidate_inodes  (major | (minor + p));
1719                 invalidate_buffers (major | (minor + p));
1720                 drive->part[p].start_sect = 0;
1721                 drive->part[p].nr_sects   = 0;
1722         };
1723 
1724         drive->part[0].nr_sects = current_capacity(drive);
1725         resetup_one_dev(HWIF(drive)->gd, drive->select.b.unit);
1726 
1727         drive->busy = 0;
1728         wake_up(&drive->wqueue);
1729         return 0;
1730 }
1731 
1732 static int write_fs_long (unsigned long useraddr, long value)
     /* [previous][next][first][last][top][bottom][index][help] */
1733 {
1734         int err;
1735 
1736         if (NULL == (long *)useraddr)
1737                 return -EINVAL;
1738         if ((err = verify_area(VERIFY_WRITE, (long *)useraddr, sizeof(long))))
1739                 return err;
1740         put_user((unsigned)value, (long *) useraddr);
1741         return 0;
1742 }
1743 
1744 static int ide_ioctl (struct inode *inode, struct file *file,
     /* [previous][next][first][last][top][bottom][index][help] */
1745                         unsigned int cmd, unsigned long arg)
1746 {
1747         struct hd_geometry *loc = (struct hd_geometry *) arg;
1748         int err;
1749         ide_drive_t *drive;
1750         unsigned long flags;
1751 
1752         if (!inode || !inode->i_rdev)
1753                 return -EINVAL;
1754         if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
1755                 return -ENODEV;
1756         switch (cmd) {
1757                 case HDIO_GETGEO:
1758                         if (!loc || drive->media != disk) return -EINVAL;
1759                         err = verify_area(VERIFY_WRITE, loc, sizeof(*loc));
1760                         if (err) return err;
1761                         put_user(drive->bios_head, (byte *) &loc->heads);
1762                         put_user(drive->bios_sect, (byte *) &loc->sectors);
1763                         put_user(drive->bios_cyl, (unsigned short *) &loc->cylinders);
1764                         put_user((unsigned)drive->part[inode->i_rdev&PARTN_MASK].start_sect,
1765                                 (unsigned long *) &loc->start);
1766                         return 0;
1767 
1768                 case BLKFLSBUF:
1769                         if(!suser()) return -EACCES;
1770                         fsync_dev(inode->i_rdev);
1771                         invalidate_buffers(inode->i_rdev);
1772                         return 0;
1773 
1774                 case BLKRASET:
1775                         if(!suser()) return -EACCES;
1776                         if(arg > 0xff) return -EINVAL;
1777                         read_ahead[MAJOR(inode->i_rdev)] = arg;
1778                         return 0;
1779 
1780                 case BLKRAGET:
1781                         return write_fs_long(arg, read_ahead[MAJOR(inode->i_rdev)]);
1782 
1783                 case BLKGETSIZE:   /* Return device size */
1784                         return write_fs_long(arg, drive->part[inode->i_rdev&PARTN_MASK].nr_sects);
1785                 case BLKRRPART: /* Re-read partition tables */
1786                         return revalidate_disk(inode->i_rdev);
1787 
1788                 case HDIO_GET_KEEPSETTINGS:
1789                         return write_fs_long(arg, drive->keep_settings);
1790 
1791                 case HDIO_GET_UNMASKINTR:
1792                         return write_fs_long(arg, drive->unmask);
1793 
1794                 case HDIO_GET_DMA:
1795                         return write_fs_long(arg, drive->using_dma);
1796 
1797                 case HDIO_GET_CHIPSET:
1798                         return write_fs_long(arg, drive->chipset);
1799 
1800                 case HDIO_GET_MULTCOUNT:
1801                         return write_fs_long(arg, drive->mult_count);
1802 
1803                 case HDIO_GET_IDENTITY:
1804                         if (!arg || (inode->i_rdev & PARTN_MASK))
1805                                 return -EINVAL;
1806                         if (drive->id == NULL)
1807                                 return -ENOMSG;
1808                         err = verify_area(VERIFY_WRITE, (char *)arg, sizeof(*drive->id));
1809                         if (!err)
1810                                 memcpy_tofs((char *)arg, (char *)drive->id, sizeof(*drive->id));
1811                         return err;
1812 
1813                         case HDIO_GET_NOWERR:
1814                         return write_fs_long(arg, drive->bad_wstat == BAD_R_STAT);
1815 
1816                 case HDIO_SET_DMA:
1817                         if (drive->media != disk)
1818                                 return -EPERM;
1819                         if (!drive->id || !(drive->id->capability & 1) || !HWIF(drive)->dmaproc)
1820                                 return -EPERM;
1821                 case HDIO_SET_KEEPSETTINGS:
1822                 case HDIO_SET_UNMASKINTR:
1823                 case HDIO_SET_NOWERR:
1824                         if (arg > 1)
1825                                 return -EINVAL;
1826                 case HDIO_SET_CHIPSET:
1827                         if (!suser())
1828                                 return -EACCES;
1829                         if ((inode->i_rdev & PARTN_MASK))
1830                                 return -EINVAL;
1831                         save_flags(flags);
1832                         cli();
1833                         switch (cmd) {
1834                                 case HDIO_SET_DMA:
1835                                         drive->using_dma = arg;
1836                                         break;
1837                                 case HDIO_SET_KEEPSETTINGS:
1838                                         drive->keep_settings = arg;
1839                                         break;
1840                                 case HDIO_SET_UNMASKINTR:
1841                                         if (arg && disallow_unmask) {
1842                                                 restore_flags(flags);
1843                                                 return -EPERM;
1844                                         }
1845                                         drive->unmask = arg;
1846                                         break;
1847                                 case HDIO_SET_NOWERR:
1848                                         drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
1849                                         break;
1850                                 case HDIO_SET_CHIPSET:
1851                                         drive->chipset   = arg;
1852                                         drive->vlb_32bit = (arg & 1);
1853                                         drive->vlb_sync  = (arg & 2) >> 1;
1854 #ifndef VLB_SYNC
1855                                         if (drive->vlb_sync)
1856                                                 printk("%s: VLB_SYNC not supported by this kernel\n", drive->name);
1857 #endif
1858                                         break;
1859                         }
1860                         restore_flags(flags);
1861                         return 0;
1862 
1863                 case HDIO_SET_MULTCOUNT:
1864                         if (!suser())
1865                                 return -EACCES;
1866                         if (inode->i_rdev & PARTN_MASK)
1867                                 return -EINVAL;
1868                         if ((drive->id != NULL) && (arg > drive->id->max_multsect))
1869                                 return -EINVAL;
1870                         save_flags(flags);
1871                         cli();
1872                         if (drive->special.b.set_multmode) {
1873                                 restore_flags(flags);
1874                                 return -EBUSY;
1875                         }
1876                         drive->mult_req = arg;
1877                         drive->special.b.set_multmode = 1;
1878                         restore_flags(flags);
1879 #ifndef IDE_DRIVE_CMD
1880                         return 0;
1881 #else
1882                         do_drive_cmd (inode->i_rdev, NULL);
1883                         return (drive->mult_count == arg) ? 0 : -EIO;
1884 
1885                 case HDIO_DRIVE_CMD:
1886                 {
1887                         unsigned long args;
1888 
1889                         if (NULL == (long *) arg)
1890                                 err = do_drive_cmd(inode->i_rdev,NULL);
1891                         else {
1892                                 if (!(err = verify_area(VERIFY_READ,(long *)arg,sizeof(long))))
1893                                 {
1894                                         args = get_user((long *)arg);
1895                                         if (!(err = verify_area(VERIFY_WRITE,(long *)arg,sizeof(long)))) {
1896                                                 err = do_drive_cmd(inode->i_rdev,(char *)&args);
1897                                                 put_user(args,(long *)arg);
1898                                         }
1899                                 }
1900                         }
1901                         return err;
1902                 }
1903 #endif /* IDE_DRIVE_CMD */
1904 
1905                 RO_IOCTLS(inode->i_rdev, arg);
1906 
1907                 default:
1908 #ifdef CONFIG_BLK_DEV_IDECD
1909                         if (drive->media == cdrom)
1910                                 return ide_cdrom_ioctl(drive, inode, file, cmd, arg);
1911 #endif /* CONFIG_BLK_DEV_IDECD */
1912                         return -EPERM;
1913         }
1914 }
1915 
1916 static int ide_check_media_change (dev_t i_rdev)
     /* [previous][next][first][last][top][bottom][index][help] */
1917 {
1918         ide_drive_t *drive;
1919 
1920         if ((drive = get_info_ptr(i_rdev)) == NULL)
1921                 return -ENODEV;
1922 #ifdef CONFIG_BLK_DEV_IDECD
1923         if (drive->media == cdrom)
1924                 return ide_cdrom_check_media_change (drive);
1925 #endif  /* CONFIG_BLK_DEV_IDECD */
1926         if (drive->removeable) /* for disks */
1927                 return 1;       /* always assume it was changed */
1928         return 0;
1929 }
1930 
1931 static void fixstring (byte *s, const int bytecount, const int byteswap)
     /* [previous][next][first][last][top][bottom][index][help] */
1932 {
1933         byte *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */
1934 
1935         if (byteswap) {
1936                 /* convert from big-endian to host byte order */
1937                 for (p = end ; p != s;) {
1938                         unsigned short *pp = (unsigned short *) (p -= 2);
1939                         *pp = ntohs(*pp);
1940                 }
1941         }
1942 
1943         /* strip leading blanks */
1944         while (s != end && *s == ' ')
1945                 ++s;
1946 
1947         /* compress internal blanks and strip trailing blanks */
1948         while (s != end && *s) {
1949                 if (*s++ != ' ' || (s != end && *s && *s != ' '))
1950                         *p++ = *(s-1);
1951         }
1952 
1953         /* wipe out trailing garbage */
1954         while (p != end)
1955                 *p++ = '\0';
1956 }
1957 
1958 static inline void do_identify (ide_drive_t *drive, byte cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
1959 {
1960         int bswap;
1961         struct hd_driveid *id;
1962         unsigned long capacity, check;
1963 
1964         id = drive->id = ide_alloc (SECTOR_WORDS*4, 0);
1965         ide_input_data(drive, id, SECTOR_WORDS);        /* read 512 bytes of id info */
1966         sti();
1967 
1968         /*
1969          * EATA SCSI controllers do a hardware ATA emulation:  ignore them
1970          */
1971         if ((id->model[0] == 'P' && id->model[1] == 'M')
1972          || (id->model[0] == 'S' && id->model[1] == 'K')) {
1973                 printk("%s: EATA SCSI HBA %.10s\n", drive->name, id->model);
1974                 drive->present = 0;
1975                 return;
1976         }
1977 
1978         /*
1979          *  WIN_IDENTIFY returns little-endian info,
1980          *  WIN_PIDENTIFY *usually* returns little-endian info.
1981          */
1982         bswap = 1;
1983         if (cmd == WIN_PIDENTIFY) {
1984                 if ((id->model[0] == 'N' && id->model[1] == 'E') /* NEC */
1985                  || (id->model[0] == 'F' && id->model[1] == 'X') /* Mitsumi */
1986                  || (id->model[0] == 'P' && id->model[1] == 'i'))/* Pioneer */
1987                         bswap = 0;      /* Vertos drives may still be weird */
1988         }
1989         fixstring (id->model,     sizeof(id->model),     bswap);
1990         fixstring (id->fw_rev,    sizeof(id->fw_rev),    bswap);
1991         fixstring (id->serial_no, sizeof(id->serial_no), bswap);
1992 
1993         /*
1994          * Check for an ATAPI device
1995          */
1996         if (cmd == WIN_PIDENTIFY) {
1997 #ifdef CONFIG_BLK_DEV_IDECD
1998                 byte type = (id->config >> 8) & 0x0f;
1999 #endif  /* CONFIG_BLK_DEV_IDECD */
2000                 printk("%s: %s, ATAPI, ", drive->name, id->model);
2001                 drive->media = cdrom;
2002 #ifdef CONFIG_BLK_DEV_IDECD
2003                 if (type == 0 || type == 5)
2004                         printk("CDROM drive\n");
2005                 else
2006                         printk("UNKNOWN device\n");
2007                 drive->present = 1;
2008                 drive->removeable = 1;
2009 #else
2010                 printk("not supported by this kernel\n");
2011 #endif  /* CONFIG_BLK_DEV_IDECD */
2012                 return;
2013         }
2014 
2015         /* check for removeable disks (eg. SYQUEST), ignore 'WD' drives */
2016         if (id->config & (1<<7)) {      /* removeable disk ? */
2017                 if (id->model[0] != 'W' || id->model[1] != 'D')
2018                         drive->removeable = 1;
2019         }
2020 
2021         drive->media = disk;
2022         /* Extract geometry if we did not already have one for the drive */
2023         if (!drive->present) {
2024                 drive->present = 1;
2025                 drive->cyl     = drive->bios_cyl  = id->cyls;
2026                 drive->head    = drive->bios_head = id->heads;
2027                 drive->sect    = drive->bios_sect = id->sectors; 
2028         }
2029         /* Handle logical geometry translation by the drive */
2030         if ((id->field_valid & 1) && id->cur_cyls && id->cur_heads
2031          && (id->cur_heads <= 16) && id->cur_sectors)
2032         {
2033                 /*
2034                  * Extract the physical drive geometry for our use.
2035                  * Note that we purposely do *not* update the bios info.
2036                  * This way, programs that use it (like fdisk) will 
2037                  * still have the same logical view as the BIOS does,
2038                  * which keeps the partition table from being screwed.
2039                  *
2040                  * An exception to this is the cylinder count,
2041                  * which we reexamine later on to correct for 1024 limitations.
2042                  */
2043                 drive->cyl  = id->cur_cyls;
2044                 drive->head = id->cur_heads;
2045                 drive->sect = id->cur_sectors;
2046 
2047                 /* check for word-swapped "capacity" field in id information */
2048                 capacity = drive->cyl * drive->head * drive->sect;
2049                 check = (id->cur_capacity0 << 16) | id->cur_capacity1;
2050                 if (check == capacity) {        /* was it swapped? */
2051                         /* yes, bring it into little-endian order: */
2052                         id->cur_capacity0 = (capacity >>  0) & 0xffff;
2053                         id->cur_capacity1 = (capacity >> 16) & 0xffff;
2054                 }
2055         }
2056         /* Use physical geometry if what we have still makes no sense */
2057         if ((!drive->head || drive->head > 16) && id->heads && id->heads <= 16) {
2058                 drive->cyl  = id->cyls;
2059                 drive->head = id->heads;
2060                 drive->sect = id->sectors; 
2061         }
2062         /* Correct the number of cyls if the bios value is too small */
2063         if (drive->sect == drive->bios_sect && drive->head == drive->bios_head) {
2064                 if (drive->cyl > drive->bios_cyl)
2065                         drive->bios_cyl = drive->cyl;
2066         }
2067 
2068         (void) current_capacity (drive); /* initialize LBA selection */
2069 
2070         printk ("%s: %.40s, %ldMB w/%dKB Cache, %sCHS=%d/%d/%d",
2071          drive->name, id->model, current_capacity(drive)/2048L, id->buf_size/2,
2072          drive->select.b.lba ? "LBA, " : "",
2073          drive->bios_cyl, drive->bios_head, drive->bios_sect);
2074 
2075         drive->mult_count = 0;
2076         if (id->max_multsect) {
2077                 drive->mult_req = INITIAL_MULT_COUNT;
2078                 if (drive->mult_req > id->max_multsect)
2079                         drive->mult_req = id->max_multsect;
2080                 if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
2081                         drive->special.b.set_multmode = 1;
2082         }
2083         if (HWIF(drive)->dmaproc != NULL) {     /* hwif supports DMA? */
2084                 if (!(HWIF(drive)->dmaproc(ide_dma_check, drive)))
2085                         printk(", DMA");
2086         }
2087         printk("\n");
2088 }
2089 
2090 /*
2091  * Delay for *at least* 10ms.  As we don't know how much time is left
2092  * until the next tick occurs, we wait an extra tick to be safe.
2093  */
2094 static void delay_10ms (void)
     /* [previous][next][first][last][top][bottom][index][help] */
2095 {
2096         unsigned long timer = jiffies + (HZ + 99)/100 + 1;
2097         while (timer > jiffies);
2098 }
2099 
2100 /*
2101  * try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
2102  * and waits for a response.  It also monitors irqs while this is
2103  * happening, in hope of automatically determining which one is
2104  * being used by the interface.
2105  *
2106  * Returns:     0  device was identified
2107  *              1  device timed-out (no response to identify request)
2108  *              2  device aborted the command (refused to identify itself)
2109  */
2110 static int try_to_identify (ide_drive_t *drive, byte cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
2111 {
2112         int hd_status, rc;
2113         unsigned long timeout;
2114         int irqs = 0;
2115 
2116         if (!HWIF(drive)->irq) {                /* already got an IRQ? */
2117                 probe_irq_off(probe_irq_on());  /* clear dangling irqs */
2118                 irqs = probe_irq_on();          /* start monitoring irqs */
2119                 OUT_BYTE(drive->ctl,IDE_CONTROL_REG);   /* enable device irq */
2120         }
2121 
2122         delay_10ms();                           /* take a deep breath */
2123         if ((IN_BYTE(IDE_ALTSTATUS_REG) ^ IN_BYTE(IDE_STATUS_REG)) & ~INDEX_STAT) {
2124                 printk("%s: probing with STATUS instead of ALTSTATUS\n", drive->name);
2125                 hd_status = IDE_STATUS_REG;     /* ancient Seagate drives */
2126         } else
2127                 hd_status = IDE_ALTSTATUS_REG;  /* use non-intrusive polling */
2128 
2129         OUT_BYTE(cmd,IDE_COMMAND_REG);          /* ask drive for ID */
2130         timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
2131         timeout += jiffies;
2132         do {
2133                 if (jiffies > timeout) {
2134                         if (!HWIF(drive)->irq)
2135                                 (void) probe_irq_off(irqs);
2136                         return 1;       /* drive timed-out */
2137                 }
2138                 delay_10ms();           /* give drive a breather */
2139         } while (IN_BYTE(hd_status) & BUSY_STAT);
2140 
2141         delay_10ms();           /* wait for IRQ and DRQ_STAT */
2142         if (OK_STAT(GET_STAT(),DRQ_STAT,BAD_R_STAT)) {
2143                 cli();                  /* some systems need this */
2144                 do_identify(drive, cmd); /* drive returned ID */
2145                 rc = 0;                 /* success */
2146         } else
2147                 rc = 2;                 /* drive refused ID */
2148         if (!HWIF(drive)->irq) {
2149                 irqs = probe_irq_off(irqs);     /* get irq number */
2150                 if (irqs > 0)
2151                         HWIF(drive)->irq = irqs;
2152                 else                            /* Mmmm.. multiple IRQs */
2153                         printk("%s: IRQ probe failed (%d)\n", drive->name, irqs);
2154         }
2155         return rc;
2156 }
2157 
2158 /*
2159  * do_probe() has the difficult job of finding a drive if it exists,
2160  * without getting hung up if it doesn't exist, without trampling on
2161  * ethernet cards, and without leaving any IRQs dangling to haunt us later.
2162  *
2163  * If a drive is "known" to exist (from CMOS or kernel parameters),
2164  * but does not respond right away, the probe will "hang in there"
2165  * for the maximum wait time (about 30 seconds), otherwise it will
2166  * exit much more quickly.
2167  *
2168  * Returns:     0  device was identified
2169  *              1  device timed-out (no response to identify request)
2170  *              2  device aborted the command (refused to identify itself)
2171  *              3  bad status from device (possible for ATAPI drives)
2172  *              4  probe was not attempted because failure was obvious
2173  */
2174 static int do_probe (ide_drive_t *drive, byte cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
2175 {
2176         int rc;
2177 #ifdef CONFIG_BLK_DEV_IDECD
2178         if (drive->present) {   /* avoid waiting for inappropriate probes */
2179                 if ((drive->media == cdrom) && (cmd == WIN_IDENTIFY))
2180                         return 4;
2181         }
2182 #endif  /* CONFIG_BLK_DEV_IDECD */
2183 #ifdef DEBUG
2184         printk("probing for %s: present=%d, type=%s, probetype=%s\n",
2185                 drive->name, drive->present, drive->media ? "cdrom":"disk",
2186                 (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
2187 #endif
2188 #if SUPPORT_HT6560B
2189         if (HWIF(drive)->select)
2190                 ide_hwif_select (HWIF(drive));
2191 #endif
2192         OUT_BYTE(drive->select.all,IDE_SELECT_REG);     /* select target drive */
2193         delay_10ms();                           /* wait for BUSY_STAT */
2194         if (IN_BYTE(IDE_SELECT_REG) != drive->select.all && !drive->present) {
2195                 OUT_BYTE(0xa0,IDE_SELECT_REG);  /* exit with drive0 selected */
2196                 return 3;    /* no i/f present: avoid killing ethernet cards */
2197         }
2198 
2199         if (OK_STAT(GET_STAT(),READY_STAT,BUSY_STAT)
2200          || drive->present || cmd == WIN_PIDENTIFY)
2201         {
2202                 if ((rc = try_to_identify(drive,cmd)))   /* send cmd and wait */
2203                         rc = try_to_identify(drive,cmd); /* failed: try again */
2204                 if (rc == 1)
2205                         printk("%s: no response (status = 0x%02x)\n", drive->name, GET_STAT());
2206                 (void) GET_STAT();              /* ensure drive irq is clear */
2207         } else {
2208                 rc = 3;                         /* not present or maybe ATAPI */
2209         }
2210         if (drive->select.b.unit != 0) {
2211                 OUT_BYTE(0xa0,IDE_SELECT_REG);  /* exit with drive0 selected */
2212                 delay_10ms();
2213                 (void) GET_STAT();              /* ensure drive irq is clear */
2214         }
2215         return rc;
2216 }
2217 
2218 /*
2219  * probe_for_drive() tests for existance of a given drive using do_probe().
2220  *
2221  * Returns:     0  no device was found
2222  *              1  device was found (note: drive->present might still be 0)
2223  */
2224 static inline byte probe_for_drive (ide_drive_t *drive)
     /* [previous][next][first][last][top][bottom][index][help] */
2225 {
2226         if (drive->noprobe)                     /* skip probing? */
2227                 return drive->present;
2228         if (do_probe(drive, WIN_IDENTIFY) >= 2) { /* if !(success||timed-out) */
2229 #ifdef CONFIG_BLK_DEV_IDECD
2230                 (void) do_probe(drive, WIN_PIDENTIFY); /* look for ATAPI device */
2231 #endif  /* CONFIG_BLK_DEV_IDECD */
2232         }
2233         if (!drive->present)
2234                 return 0;                       /* drive not found */
2235         if (drive->id == NULL) {                /* identification failed? */
2236                 if (drive->media == disk) {
2237                         printk ("%s: non-IDE drive, CHS=%d/%d/%d\n",
2238                          drive->name, drive->cyl, drive->head, drive->sect);
2239                 }
2240 #ifdef CONFIG_BLK_DEV_IDECD
2241                 else if (drive->media == cdrom) {
2242                         printk("%s: ATAPI cdrom (?)\n", drive->name);
2243                 }
2244 #endif  /* CONFIG_BLK_DEV_IDECD */
2245                 else {
2246                         drive->present = 0;     /* nuke it */
2247                         return 1;               /* drive was found */
2248                 }
2249         }
2250         if (drive->media == disk && !drive->select.b.lba) {
2251                 if (!drive->head || drive->head > 16) {
2252                         printk("%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
2253                          drive->name, drive->head);
2254                         drive->present = 0;
2255                 }
2256         }
2257         return 1;       /* drive was found */
2258 }
2259 
2260 /*
2261  *  This routine only knows how to look for drive units 0 and 1
2262  *  on an interface, so any setting of MAX_DRIVES > 2 won't work here.
2263  */
2264 static void probe_for_drives (ide_hwif_t *hwif)
     /* [previous][next][first][last][top][bottom][index][help] */
2265 {
2266         unsigned int unit;
2267 
2268         if (check_region(hwif->io_base,8) || check_region(hwif->ctl_port,1)) {
2269                 int msgout = 0;
2270                 for (unit = 0; unit < MAX_DRIVES; ++unit) {
2271                         ide_drive_t *drive = &hwif->drives[unit];
2272                         if (drive->present) {
2273                                 drive->present = 0;
2274                                 printk("%s: ERROR, PORTS ALREADY IN USE\n", drive->name);
2275                                 msgout = 1;
2276                         }
2277                 }
2278                 if (!msgout)
2279                         printk("%s: ports already in use, skipping probe\n", hwif->name);
2280         } else {
2281                 unsigned long flags;
2282                 save_flags(flags);
2283 
2284 #if (MAX_DRIVES > 2)
2285                 printk("%s: probing for first 2 of %d possible drives\n", hwif->name, MAX_DRIVES);
2286 #endif
2287                 sti();  /* needed for jiffies and irq probing */
2288                 /*
2289                  * Second drive should only exist if first drive was found,
2290                  * but a lot of cdrom drives seem to be configured as slave-only
2291                  */
2292                 for (unit = 0; unit < 2; ++unit)  /* note the hardcoded '2' */
2293                         (void) probe_for_drive(&hwif->drives[unit]);
2294                 for (unit = 0; unit < MAX_DRIVES; ++unit) {
2295                         ide_drive_t *drive = &hwif->drives[unit];
2296                         if (drive->present) {
2297                                 hwif->present = 1;
2298                                 request_region(hwif->io_base,  8, hwif->name);
2299                                 request_region(hwif->ctl_port, 1, hwif->name);
2300 #ifdef CONFIG_BLK_DEV_IDECD
2301                                 if (drive->media == cdrom)
2302                                         ide_cdrom_setup(drive);
2303 #endif  /* CONFIG_BLK_DEV_IDECD */
2304                                 break;
2305                         }
2306                 }
2307                 restore_flags(flags);
2308         }
2309 }
2310 
2311 #if SUPPORT_DTC2278
2312 /*
2313  * From: andy@cercle.cts.com (Dyan Wile)
2314  *
2315  * Below is a patch for DTC-2278 - alike software-programmable controllers
2316  * The code enables the secondary IDE controller and the PIO4 (3?) timings on
2317  * the primary (EIDE). You may probably have to enable the 32-bit support to
2318  * get the full speed. You better get the disk interrupts disabled ( hdparm -u0
2319  * /dev/hd.. ) for the drives connected to the EIDE interface. (I get my 
2320  * filesystem  corrupted with -u1, but under heavy disk load only :-)  
2321  *
2322  * From: mlord@bnr.ca -- this chipset is now forced to use the "serialize" feature,
2323  * which hopefully will make it more reliable to use.. maybe it has the same bugs
2324  * as the CMD640B and RZ1000 ??
2325  */
2326 
2327 #if SET_DTC2278_MODE4
2328 static void sub22 (char b, char c)
     /* [previous][next][first][last][top][bottom][index][help] */
2329 {
2330         int i;
2331 
2332         for(i = 0; i < 3; ++i) {
2333                 inb(0x3f6);
2334                 outb_p(b,0xb0);
2335                 inb(0x3f6);
2336                 outb_p(c,0xb4);
2337                 inb(0x3f6);
2338                 if(inb(0xb4) == c) {
2339                         outb_p(7,0xb0);
2340                         inb(0x3f6);
2341                         return; /* success */
2342                 }
2343         }
2344 }
2345 #endif /* SET_DTC2278_MODE4 */
2346 
2347 static void init_dtc2278 (void)
     /* [previous][next][first][last][top][bottom][index][help] */
2348 {
2349         cli();
2350 #if SET_DTC2278_MODE4
2351         /*
2352          * This enables PIO mode4 (3?) on the first interface
2353          */
2354         sub22(1,0xc3);
2355         sub22(0,0xa0);
2356 #endif /* SET_DTC2278_MODE4 */
2357         /*
2358          * This enables the second interface
2359          */
2360         outb_p(4,0xb0);
2361         inb(0x3f6);
2362         outb_p(0x20,0xb4);
2363         inb(0x3f6);
2364 
2365         sti();
2366 }
2367 #endif /* SUPPORT_DTC2278 */
2368 
2369 #if SUPPORT_CMD640
2370 /*
2371  * ??? fixme: 
2372  */
2373 byte read_cmd640_vlb (byte port, byte reg)
     /* [previous][next][first][last][top][bottom][index][help] */
2374 {
2375         byte val;
2376 
2377         unsigned long flags;
2378         save_flags(flags);
2379         cli();
2380         outw(reg, port);
2381         val = inb(port+4);
2382         restore_flags(flags);
2383         return val;
2384 }
2385 
2386 void write_cmd640_vlb (byte port, byte reg, byte val)
     /* [previous][next][first][last][top][bottom][index][help] */
2387 {
2388         unsigned long flags;
2389         save_flags(flags);
2390         cli();
2391         outw(reg, port);
2392         outw(val, port+4);
2393         restore_flags(flags);
2394 }
2395 
2396 void init_cmd640_vlb (void)
     /* [previous][next][first][last][top][bottom][index][help] */
2397 {
2398         byte reg;
2399         unsigned short port = 0x178;
2400 
2401         printk("ide: buggy CMD640 interface: ");
2402         reg = read_cmd640_vlb(port, 0x50);
2403         if (reg == 0xff || (reg & 0x90) != 0x90) {
2404 #if TRY_CMD640_VLB_AT_0x78
2405                 port = 0x78;
2406                 if (reg == 0xff || (reg & 0x90) != 0x90)
2407 #endif
2408                 {
2409                         single_threaded = 1;
2410                         disallow_unmask = 1;
2411                         printk("(probe failed) serialized, disabled unmasking\n");
2412                         return;
2413                 }
2414         }
2415         write_cmd640_vlb(port, 0x51, read_cmd640_vlb(port, 0x51)|0xc8);
2416         printk("disabled read-ahead, enabled secondary\n");
2417 
2418 }
2419 #endif /* SUPPORT_CMD640 */
2420 
2421 /*
2422  * stridx() returns the offset of c within s,
2423  * or -1 if c is '\0' or not found within s.
2424  */
2425 static int stridx (const char *s, char c)
     /* [previous][next][first][last][top][bottom][index][help] */
2426 {
2427         char *i = strchr(s, c);
2428         return (i && c) ? i - s : -1;
2429 }
2430 
2431 /*
2432  * match_parm() does parsing for ide_setup():
2433  *
2434  * 1. the first char of s must be '='.
2435  * 2. if the remainder matches one of the supplied keywords,
2436  *     the index (1 based) of the keyword is negated and returned.
2437  * 3. if the remainder is a series of no more than max_vals numbers
2438  *     separated by commas, the numbers are saved in vals[] and a 
2439  *     count of how many were saved is returned.  Base10 is assumed,
2440  *     and base16 is allowed when prefixed with "0x".
2441  * 4. otherwise, zero is returned.
2442  */
2443 static int match_parm (char *s, const char *keywords[], int vals[], int max_vals)
     /* [previous][next][first][last][top][bottom][index][help] */
2444 {
2445         static const char *decimal = "0123456789";
2446         static const char *hex = "0123456789abcdef";
2447         int i, n;
2448 
2449         if (*s++ == '=') {
2450                 /*
2451                  * Try matching against the supplied keywords,
2452                  * and return -(index+1) if we match one
2453                  */
2454                 for (i = 0; *keywords != NULL; ++i) {
2455                         if (!strcmp(s, *keywords++))
2456                                 return -(i+1);
2457                 }
2458                 /*
2459                  * Look for a series of no more than "max_vals"
2460                  * numeric values separated by commas, in base10,
2461                  * or base16 when prefixed with "0x".  
2462                  * Return a count of how many were found.
2463                  */
2464                 for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
2465                         vals[n] = i;
2466                         while ((i = stridx(decimal, *++s)) >= 0)
2467                                 vals[n] = (vals[n] * 10) + i;
2468                         if (*s == 'x' && !vals[n]) {
2469                                 while ((i = stridx(hex, *++s)) >= 0)
2470                                         vals[n] = (vals[n] * 0x10) + i;
2471                         }
2472                         if (++n == max_vals)
2473                                 break;
2474                         if (*s == ',')
2475                                 ++s;
2476                 }
2477                 if (!*s)
2478                         return n;
2479         }
2480         return 0;       /* zero = nothing matched */
2481 }
2482 
2483 /*
2484  * ide_setup() gets called VERY EARLY during initialization,
2485  * to handle kernel "command line" strings beginning with "hdx="
2486  * or "ide".  Here is the complete set currently supported:
2487  *
2488  * "hdx="  is recognized for all "x" from "a" to "h", such as "hdc".
2489  * "idex=" is recognized for all "x" from "0" to "3", such as "ide1".
2490  *
2491  * "hdx=noprobe"        : drive may be present, but do not probe for it
2492  * "hdx=nowerr"         : ignore the WRERR_STAT bit on this drive
2493  * "hdx=cdrom"          : drive is present, and is a cdrom drive
2494  * "hdx=cyl,head,sect"  : disk drive is present, with specified geometry
2495  *
2496  * "idex=noprobe"       : do not attempt to access/use this interface
2497  * "idex=base"          : probe for an interface at the addr specified,
2498  *                              where "base" is usually 0x1f0 or 0x170
2499  *                              and "ctl" is assumed to be "base"+0x206
2500  * "idex=base,ctl"      : specify both base and ctl
2501  * "idex=base,ctl,irq"  : specify base, ctl, and irq number
2502  *
2503  * The following two are valid ONLY on ide0 or ide1,
2504  * and the defaults for the base,ctl ports must not be altered.
2505  *
2506  * "idex=serialize"     : do not overlap operations on ide0 and ide1.
2507  * "idex=dtc2278"       : enables use of DTC2278 secondary i/f
2508  * "idex=ht6560b"       : enables use of HT6560B secondary i/f
2509  * "idex=cmd640_vlb"    : required for VLB cards with the CMD640 chip
2510  *                        (PCI version will be automatically detected)
2511  */
2512 void ide_setup (char *s)
     /* [previous][next][first][last][top][bottom][index][help] */
2513 {
2514         int vals[3];
2515         ide_hwif_t *hwif;
2516         ide_drive_t *drive;
2517         unsigned int hw, unit;
2518         const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
2519         const char max_hwif  = '0' + (MAX_HWIFS - 1);
2520 
2521         printk("ide_setup: %s", s);
2522         init_ide_data ();
2523 
2524         /*
2525          * Look for drive options:  "hdx="
2526          */
2527         if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
2528                 const char *hd_words[] = {"noprobe", "nowerr", "cdrom", "serialize", NULL};
2529                 unit = s[2] - 'a';
2530                 hw   = unit / MAX_DRIVES;
2531                 unit = unit % MAX_DRIVES;
2532                 hwif = &ide_hwifs[hw];
2533                 drive = &hwif->drives[unit];
2534                 switch (match_parm(&s[3], hd_words, vals, 3)) {
2535                         case -1: /* "noprobe" */
2536                                 drive->noprobe = 1;
2537                                 goto done;
2538                         case -2: /* "nowerr" */
2539                                 drive->bad_wstat = BAD_R_STAT;
2540                                 hwif->noprobe = 0;
2541                                 goto done;
2542                         case -3: /* "cdrom" */
2543                                 drive->present = 1;
2544                                 drive->media = cdrom;
2545                                 hwif->noprobe = 0;
2546                                 goto done;
2547                         case -4: /* "serialize" */
2548                                 printk(" -- USE ""ide%c=serialize"" INSTEAD", '0'+hw);
2549                                 goto do_serialize;
2550                         case 3: /* cyl,head,sect */
2551                                 drive->media    = disk;
2552                                 drive->cyl      = drive->bios_cyl  = vals[0];
2553                                 drive->head     = drive->bios_head = vals[1];
2554                                 drive->sect     = drive->bios_sect = vals[2];
2555                                 drive->present  = 1;
2556                                 hwif->noprobe = 0;
2557                                 goto done;
2558                         default:
2559                                 goto bad_option;
2560                 }
2561         }
2562         /*
2563          * Look for interface options:  "idex="
2564          */
2565         if (s[0] == 'i' && s[1] == 'd' && s[2] == 'e' && s[3] >= '0' && s[3] <= max_hwif) {
2566                 const char *ide_words[] = {"noprobe", "serialize", "dtc2278", "ht6560b", "cmd640_vlb", NULL};
2567                 hw = s[3] - '0';
2568                 hwif = &ide_hwifs[hw];
2569 
2570                 switch (match_parm(&s[4], ide_words, vals, 3)) {
2571 #if SUPPORT_CMD640
2572                         case -5: /* "cmd640_vlb" */
2573                                 if (hw > 1) goto bad_hwif;
2574                                 init_cmd640_vlb();
2575                                 goto do_serialize; /* not necessary once we implement the above */
2576                                 break;
2577 #endif /* SUPPORT_CMD640 */
2578 #if SUPPORT_HT6560B
2579                         case -4: /* "ht6560b" */
2580                                 if (hw > 1) goto bad_hwif;
2581                                 ide_hwifs[0].select = 0x1c;
2582                                 ide_hwifs[1].select = 0x1d;
2583                                 goto do_serialize;
2584 #endif /* SUPPORT_HT6560B */
2585 #if SUPPORT_DTC2278
2586                         case -3: /* "dtc2278" */
2587                                 if (hw > 1) goto bad_hwif;
2588                                 init_dtc2278();
2589                                 goto do_serialize;
2590 #endif /* SUPPORT_DTC2278 */
2591                         case -2: /* "serialize" */
2592                         do_serialize:
2593                                 if (hw > 1) goto bad_hwif;
2594                                 single_threaded = 1;
2595                                 goto done;
2596                         case -1: /* "noprobe" */
2597                                 hwif->noprobe = 1;
2598                                 goto done;
2599                         case 1: /* base */
2600                                 vals[1] = vals[0] + 0x206; /* default ctl */
2601                         case 2: /* base,ctl */
2602                                 vals[2] = 0;    /* default irq = probe for it */
2603                         case 3: /* base,ctl,irq */
2604                                 hwif->io_base  = vals[0];
2605                                 hwif->ctl_port = vals[1];
2606                                 hwif->irq      = vals[2];
2607                                 hwif->noprobe = 0;
2608                                 goto done;
2609                 }
2610         }
2611 bad_option:
2612         printk(" -- BAD OPTION\n");
2613         return;
2614 bad_hwif:
2615         printk("-- NOT SUPPORTED ON ide%d", hw);
2616 done:
2617         printk("\n");
2618 }
2619 
2620 /*
2621  * This routine is called from the partition-table code in genhd.c
2622  * to "convert" a drive to a logical geometry with fewer than 1024 cyls
2623  * It mimics the method used by Ontrack Disk Manager.
2624  */
2625 int ide_xlate_1024 (dev_t i_rdev, int need_offset, const char *msg)
     /* [previous][next][first][last][top][bottom][index][help] */
2626 {
2627         ide_drive_t *drive;
2628         static const byte head_vals[] = {4, 8, 16, 32, 64, 128, 255, 0};
2629         const byte *heads = head_vals;
2630         unsigned long tracks;
2631 
2632         if ((drive = get_info_ptr(i_rdev)) == NULL || drive->id == NULL)
2633                 return 0;
2634 
2635         drive->cyl  = drive->bios_cyl  = drive->id->cyls;
2636         drive->head = drive->bios_head = drive->id->heads;
2637         drive->sect = drive->bios_sect = drive->id->sectors; 
2638         drive->special.b.set_geometry = 1;
2639 
2640         tracks = drive->bios_cyl * drive->bios_head * drive->bios_sect / 63;
2641         drive->bios_sect = 63;
2642         while (drive->bios_cyl >= 1024) {
2643                 drive->bios_head = *heads;
2644                 drive->bios_cyl = tracks / drive->bios_head;
2645                 if (0 == *++heads)
2646                         break;
2647         }
2648         if (need_offset) {
2649                 drive->sect0 = 63;
2650                 drive->bios_cyl = (tracks - 1) / drive->bios_head;
2651         }
2652         drive->part[0].nr_sects = current_capacity(drive);
2653         printk("%s [+%d,%d/%d/%d]", msg, drive->sect0, drive->bios_cyl, drive->bios_head, drive->bios_sect);
2654         return 1;
2655 }
2656 
2657 /*
2658  * We query CMOS about hard disks : it could be that we have a SCSI/ESDI/etc
2659  * controller that is BIOS compatible with ST-506, and thus showing up in our
2660  * BIOS table, but not register compatible, and therefore not present in CMOS.
2661  *
2662  * Furthermore, we will assume that our ST-506 drives <if any> are the primary
2663  * drives in the system -- the ones reflected as drive 1 or 2.  The first
2664  * drive is stored in the high nibble of CMOS byte 0x12, the second in the low
2665  * nibble.  This will be either a 4 bit drive type or 0xf indicating use byte
2666  * 0x19 for an 8 bit type, drive 1, 0x1a for drive 2 in CMOS.  A non-zero value 
2667  * means we have an AT controller hard disk for that drive.
2668  *
2669  * Of course, there is no guarantee that either drive is actually on the
2670  * "primary" IDE interface, but we don't bother trying to sort that out here.
2671  * If a drive is not actually on the primary interface, then these parameters
2672  * will be ignored.  This results in the user having to supply the logical
2673  * drive geometry as a boot parameter for each drive not on the primary i/f.
2674  *
2675  * The only "perfect" way to handle this would be to modify the setup.[cS] code
2676  * to do BIOS calls Int13h/Fn08h and Int13h/Fn48h to get all of the drive info
2677  * for us during initialization.  I have the necessary docs -- any takers?  -ml
2678  */
2679 
2680 static void probe_cmos_for_drives (ide_hwif_t *hwif)
     /* [previous][next][first][last][top][bottom][index][help] */
2681 {
2682 #ifdef __i386__
2683         extern struct drive_info_struct drive_info;
2684         byte cmos_disks, *BIOS = (byte *) &drive_info;
2685         int unit;
2686 
2687         outb_p(0x12,0x70);              /* specify CMOS address 0x12 */
2688         cmos_disks = inb_p(0x71);       /* read the data from 0x12 */
2689         /* Extract drive geometry from CMOS+BIOS if not already setup */
2690         for (unit = 0; unit < MAX_DRIVES; ++unit) {
2691                 ide_drive_t *drive = &hwif->drives[unit];
2692                 if ((cmos_disks & (0xf0 >> (unit*4))) && !drive->present) {
2693                         drive->cyl   = drive->bios_cyl  = *(unsigned short *)BIOS;
2694                         drive->head  = drive->bios_head = *(BIOS+2);
2695                         drive->sect  = drive->bios_sect = *(BIOS+14);
2696                         drive->ctl   = *(BIOS+8);
2697                         drive->present = 1;
2698                 }
2699                 BIOS += 16;
2700         }
2701 #endif
2702 }
2703 
2704 /*
2705  * This routine sets up the irq for an ide interface, and creates a new
2706  * hwgroup for the irq/hwif if none was previously assigned.
2707  *
2708  * The SA_INTERRUPT in sa_flags means ide_intr() is always entered with
2709  * interrupts completely disabled.  This can be bad for interrupt latency,
2710  * but anything else has led to problems on some machines.  We re-enable
2711  * interrupts as much as we can safely do in most places.
2712  */
2713 static int init_irq (ide_hwif_t *hwif)
     /* [previous][next][first][last][top][bottom][index][help] */
2714 {
2715         unsigned long flags;
2716         ide_hwgroup_t *hwgroup;
2717 
2718         /*
2719          * First, we try to grab the irq
2720          */
2721         save_flags(flags);
2722         cli();
2723         if (request_irq(hwif->irq, ide_intr, SA_INTERRUPT, hwif->name)) {
2724                 restore_flags(flags);
2725                 printk(" -- FAILED!");
2726                 return 1;
2727         }
2728 
2729         /*
2730          * Got the irq,  now set everything else up
2731          */
2732         if ((hwgroup = irq_to_hwgroup[hwif->irq]) == NULL) {
2733                 hwgroup = ide_alloc (sizeof(ide_hwgroup_t), 0);
2734                 irq_to_hwgroup[hwif->irq] = hwgroup;
2735                 hwgroup->hwif    = hwif->next = hwif;
2736                 hwgroup->rq      = NULL;
2737                 hwgroup->handler = NULL;
2738                 hwgroup->drive   = NULL;
2739                 hwgroup->reset_timeout = 0;
2740 #ifdef CONFIG_BLK_DEV_IDECD
2741                 hwgroup->doing_atapi_reset = 0;
2742 #endif /* CONFIG_BLK_DEV_IDECD */
2743                 init_timer(&hwgroup->timer);
2744                 hwgroup->timer.function = &timer_expiry;
2745                 hwgroup->timer.data = (unsigned long) hwgroup;
2746         } else {
2747                 hwif->next = hwgroup->hwif->next;
2748                 hwgroup->hwif->next = hwif;
2749         }
2750         hwif->hwgroup = hwgroup;
2751 
2752         restore_flags(flags);   /* safe now that hwif->hwgroup is set up */
2753 
2754         printk("%s at 0x%03x-0x%03x,0x%03x on irq %d", hwif->name,
2755                 hwif->io_base, hwif->io_base + 7, hwif->ctl_port, hwif->irq);
2756         if (hwgroup->hwif != hwif) {
2757                 char *name = hwgroup->hwif->name;
2758                 if (hwgroup->hwif->irq == hwif->irq)
2759                         printk(" (shared with %s)", name);
2760                 else
2761                         printk(" (serialized with %s)", name);
2762         }
2763         printk("\n");
2764         return 0;
2765 }
2766 
2767 static struct file_operations ide_fops = {
2768         NULL,                   /* lseek - default */
2769         block_read,             /* read - general block-dev read */
2770         block_write,            /* write - general block-dev write */
2771         NULL,                   /* readdir - bad */
2772         NULL,                   /* select */
2773         ide_ioctl,              /* ioctl */
2774         NULL,                   /* mmap */
2775         ide_open,               /* open */
2776         ide_release,            /* release */
2777         block_fsync             /* fsync */
2778         ,NULL,                  /* fasync */
2779         ide_check_media_change, /* check_media_change */
2780         revalidate_disk         /* revalidate */
2781 };
2782 
2783 #ifdef CONFIG_PCI
2784 
2785 void ide_pci_access_error (int rc)
     /* [previous][next][first][last][top][bottom][index][help] */
2786 {
2787         printk("ide: pcibios access failed - %s\n", pcibios_strerror(rc));
2788 }
2789 
2790 #if SUPPORT_RZ1000 || SUPPORT_CMD640
2791 void buggy_interface_fallback (int rc)
     /* [previous][next][first][last][top][bottom][index][help] */
2792 {
2793         ide_pci_access_error (rc);
2794         single_threaded = 1;
2795         disallow_unmask = 1;
2796         printk("serialized, disabled unmasking\n");
2797 }
2798 #endif /* SUPPORT_RZ1000 || SUPPORT_CMD640 */
2799 
2800 #if SUPPORT_RZ1000
2801 void init_rz1000 (byte bus, byte fn)
     /* [previous][next][first][last][top][bottom][index][help] */
2802 {
2803         int rc;
2804         unsigned short reg;
2805 
2806         printk("ide: buggy RZ1000 interface: ");
2807         if ((rc = pcibios_read_config_word(bus, fn, 0x40, &reg))
2808          || (rc =  pcibios_write_config_word(bus, fn, 0x40, reg & 0xdfff)))
2809                 buggy_interface_fallback (rc);
2810         else
2811                 printk("disabled read-ahead\n");
2812 }
2813 #endif /* SUPPORT_RZ1000 */
2814 
2815 #if SUPPORT_CMD640
2816 void init_cmd640 (byte bus, byte fn)
     /* [previous][next][first][last][top][bottom][index][help] */
2817 {
2818         int rc;
2819         unsigned char reg;
2820 
2821         printk("ide: buggy CMD640 interface: ");
2822         if ((rc = pcibios_read_config_byte(bus, fn, 0x51, &reg))
2823          || (rc =  pcibios_write_config_byte(bus, fn, 0x51, reg | 0xc8)))
2824                 buggy_interface_fallback (rc);
2825         else
2826                 printk("disabled read-ahead, enabled secondary\n");
2827 }
2828 #endif /* SUPPORT_CMD640 */
2829 
2830 typedef void (ide_pci_init_proc_t)(byte, byte);
2831 
2832 /*
2833  * ide_probe_pci() scans PCI for a specific vendor/device function,
2834  * and invokes the supplied init routine for each instance detected.
2835  */
2836 static void ide_probe_pci (unsigned short vendor, unsigned short device, ide_pci_init_proc_t *init)
     /* [previous][next][first][last][top][bottom][index][help] */
2837 {
2838         unsigned long flags;
2839         unsigned index = 0;
2840         byte fn, bus;
2841         int rc;
2842 
2843         if (!pcibios_present())
2844                 return;
2845         save_flags(flags);
2846         cli();
2847         for (index = 0; !pcibios_find_device (vendor, device, index, &bus, &fn); ++index) {
2848                 unsigned short command;
2849                 if ((rc = pcibios_read_config_word (bus, fn, PCI_COMMAND, &command))) {
2850                         ide_pci_access_error (rc);
2851                 } else if (command & 1) {       /* is device enabled? */
2852                         init (bus, fn);
2853                 }
2854         }
2855         restore_flags(flags);
2856 }
2857 
2858 /*
2859  * ide_init_pci() finds/initializes "known" PCI IDE interfaces
2860  * 
2861  * This routine should ideally be using pcibios_find_class() to find
2862  * all IDE interfaces, but that function causes some systems to "go weird".
2863  */
2864 static void ide_init_pci (void)
     /* [previous][next][first][last][top][bottom][index][help] */
2865 {
2866 #if SUPPORT_RZ1000
2867         ide_probe_pci (PCI_VENDOR_ID_PCTECH, PCI_DEVICE_ID_PCTECH_RZ1000, &init_rz1000);
2868 #endif
2869 #if SUPPORT_CMD640
2870         ide_probe_pci (PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_CMD_640, &init_cmd640);
2871 #endif
2872 #ifdef CONFIG_BLK_DEV_TRITON
2873         ide_probe_pci (PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371, &ide_init_triton);
2874 #endif
2875 }
2876 #endif /* CONFIG_PCI */
2877 
2878 /*
2879  * This is gets invoked once during initialization, to set *everything* up
2880  */
2881 unsigned long ide_init (unsigned long mem_start, unsigned long mem_end)
     /* [previous][next][first][last][top][bottom][index][help] */
2882 {
2883         int h;
2884 
2885         ide_mem_start = mem_start;      /* for ide_alloc () */
2886         init_ide_data ();
2887         /*
2888          * First, we determine what hardware is present
2889          */
2890 
2891 #ifdef CONFIG_PCI
2892         /*
2893          * Find/initialize PCI IDE interfaces
2894          */
2895         ide_init_pci ();
2896 #endif /* CONFIG_PCI */
2897 
2898         /*
2899          * Probe for drives in the usual way.. CMOS/BIOS, then poke at ports
2900          */
2901         for (h = 0; h < MAX_HWIFS; ++h) {
2902                 ide_hwif_t *hwif = &ide_hwifs[h];
2903                 if (!hwif->noprobe) {
2904                         if (hwif->io_base == HD_DATA)
2905                                 probe_cmos_for_drives (hwif);
2906                         probe_for_drives (hwif);
2907                 }
2908                 if (hwif->present) {
2909                         if (!hwif->irq) {
2910                                 if (!(hwif->irq = default_irqs[h])) {
2911                                         printk("%s: DISABLED, NO IRQ\n", hwif->name);
2912                                         hwif->present = 0;
2913                                         continue;
2914                                 }
2915                         }
2916 #ifdef CONFIG_BLK_DEV_HD
2917                         if (hwif->irq == HD_IRQ && hwif->io_base != HD_DATA) {
2918                                 printk("%s: CANNOT SHARE IRQ WITH OLD HARDDISK DRIVER (hd.c)\n", hwif->name);
2919                                 hwif->present = 0;
2920 B
2921                         }
2922 #endif /* CONFIG_BLK_DEV_HD */
2923                 }
2924         }
2925 
2926         /*
2927          * Now we try to set up irqs and major devices for what was found
2928          */
2929         for (h = MAX_HWIFS-1; h >= 0; --h) {
2930                 void (*rfn)(void);
2931                 ide_hwif_t *hwif = &ide_hwifs[h];
2932                 if (!hwif->present)
2933                         continue;
2934                 hwif->present = 0; /* we set it back to 1 if all is ok below */
2935                 if (h == 0 && single_threaded) {
2936                         if (ide_hwifs[1].present) {
2937                                 if (irq_to_hwgroup[hwif->irq] != NULL) {
2938                                         printk("%s: SERIALIZE BUG!\n", hwif->name);
2939                                         continue;
2940                                 }
2941                                 irq_to_hwgroup[hwif->irq] = irq_to_hwgroup[ide_hwifs[1].irq];
2942                         }
2943                 }
2944                 switch (hwif->major) {
2945                         case IDE0_MAJOR: rfn = &do_ide0_request; break;
2946                         case IDE1_MAJOR: rfn = &do_ide1_request; break;
2947                         case IDE2_MAJOR: rfn = &do_ide2_request; break;
2948                         case IDE3_MAJOR: rfn = &do_ide3_request; break;
2949                         default:
2950                                 printk("%s: request_fn NOT DEFINED\n", hwif->name);
2951                                 continue;
2952                 }
2953                 if (register_blkdev (hwif->major, hwif->name, &ide_fops)) {
2954                         printk("%s: UNABLE TO GET MAJOR NUMBER %d\n", hwif->name, hwif->major);
2955                 } else if (init_irq (hwif)) {
2956                         printk("%s: UNABLE TO GET IRQ %d\n", hwif->name, hwif->irq);
2957                         (void) unregister_blkdev (hwif->major, hwif->name);
2958                 } else {
2959                         init_gendisk(hwif);
2960                         blk_dev[hwif->major].request_fn = rfn;
2961                         read_ahead[hwif->major] = 8;    /* (4kB) */
2962                         hwif->present = 1;      /* success */
2963                 }
2964         }
2965         mem_start = ide_mem_start;
2966         ide_mem_start = 0uL;    /* prevent further use of ide_alloc() */
2967         return mem_start;
2968 }

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