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. read_cmd640_vlb
  62. write_cmd640_vlb
  63. init_cmd640_vlb
  64. stridx
  65. match_parm
  66. ide_setup
  67. ide_xlate_1024
  68. probe_cmos_for_drives
  69. init_irq
  70. ide_pci_access_error
  71. buggy_interface_fallback
  72. init_rz1000
  73. init_cmd640
  74. ide_probe_pci
  75. ide_init_pci
  76. ide_init

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

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