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_hwif_data
  4. init_ide_data
  5. do_vlb_sync
  6. ide_input_data
  7. ide_output_data
  8. ide_set_handler
  9. lba_capacity_is_ok
  10. current_capacity
  11. ide_geninit
  12. init_gendisk
  13. atapi_reset_pollfunc
  14. reset_pollfunc
  15. do_reset1
  16. ide_do_reset
  17. ide_end_drive_cmd
  18. ide_dump_status
  19. try_to_flush_leftover_data
  20. ide_error
  21. read_intr
  22. write_intr
  23. ide_multwrite
  24. multwrite_intr
  25. ide_cmd
  26. set_multmode_intr
  27. set_geometry_intr
  28. recal_intr
  29. drive_cmd_intr
  30. do_special
  31. ide_wait_stat
  32. do_rw_disk
  33. execute_drive_cmd
  34. do_request
  35. ide_do_request
  36. do_hwgroup_request
  37. do_ide0_request
  38. do_ide1_request
  39. do_ide2_request
  40. do_ide3_request
  41. timer_expiry
  42. unexpected_intr
  43. ide_intr
  44. get_info_ptr
  45. ide_init_drive_cmd
  46. ide_do_drive_cmd
  47. ide_open
  48. ide_release
  49. revalidate_disk
  50. write_fs_long
  51. ide_ioctl
  52. ide_check_media_change
  53. ide_fixstring
  54. do_identify
  55. delay_10ms
  56. try_to_identify
  57. do_probe
  58. probe_for_drive
  59. probe_hwif
  60. stridx
  61. match_parm
  62. ide_setup
  63. ide_xlate_1024
  64. probe_cmos_for_drives
  65. init_irq
  66. ide_probe_pci
  67. probe_for_hwifs
  68. hwif_init
  69. ide_init
  70. ide_register
  71. ide_unregister

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

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