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_hwif
  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. hwif_init
  67. ide_init
  68. ide_register
  69. ide_unregister

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

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