root/drivers/block/ide.c

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

DEFINITIONS

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

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

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