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

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