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

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