root/drivers/block/ide.c

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

DEFINITIONS

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

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

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