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

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