root/drivers/block/ide.c

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

DEFINITIONS

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

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

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