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

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

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