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

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