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. 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. buggy_interface_fallback
  70. init_rz1000
  71. ide_probe_pci
  72. ide_init_pci
  73. ide_init

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

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