root/drivers/block/ide.c

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

DEFINITIONS

This source file includes following definitions.
  1. read_timer
  2. ide_set_recovery_timer
  3. init_ide_data
  4. do_vlb_sync
  5. ide_input_data
  6. ide_output_data
  7. ide_hwif_select
  8. ide_set_handler
  9. lba_capacity_is_ok
  10. current_capacity
  11. ide_geninit
  12. ide_alloc
  13. init_gendisk
  14. reset_ihandler
  15. start_reset_timer
  16. atapi_reset_handler
  17. reset_handler
  18. ide_do_reset
  19. end_drive_cmd
  20. ide_dump_status
  21. try_to_flush_leftover_data
  22. ide_error
  23. read_intr
  24. write_intr
  25. multwrite
  26. multwrite_intr
  27. ide_cmd
  28. set_multmode_intr
  29. set_geometry_intr
  30. recal_intr
  31. drive_cmd_intr
  32. do_special
  33. ide_wait_stat
  34. do_rw_disk
  35. do_request
  36. ide_do_request
  37. do_hwgroup_request
  38. do_ide0_request
  39. do_ide1_request
  40. do_ide2_request
  41. do_ide3_request
  42. timer_expiry
  43. unexpected_intr
  44. ide_intr
  45. get_info_ptr
  46. ide_do_drive_cmd
  47. ide_open
  48. ide_release
  49. revalidate_disk
  50. write_fs_long
  51. ide_ioctl
  52. ide_check_media_change
  53. fixstring
  54. do_identify
  55. delay_10ms
  56. try_to_identify
  57. do_probe
  58. probe_for_drive
  59. probe_for_drives
  60. sub22
  61. init_dtc2278
  62. read_cmd640_vlb
  63. write_cmd640_vlb
  64. init_cmd640_vlb
  65. stridx
  66. match_parm
  67. ide_setup
  68. ide_xlate_1024
  69. probe_cmos_for_drives
  70. init_irq
  71. ide_pci_access_error
  72. buggy_interface_fallback
  73. init_rz1000
  74. init_cmd640
  75. ide_probe_pci
  76. ide_init_pci
  77. ide_init

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

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