root/drivers/block/ide.c

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

DEFINITIONS

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

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

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