root/drivers/block/ide.c

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

DEFINITIONS

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

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

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