root/drivers/block/ide.c

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

DEFINITIONS

This source file includes following definitions.
  1. read_timer
  2. ide_set_recovery_timer
  3. init_ide_data
  4. do_vlb_sync
  5. ide_input_data
  6. ide_output_data
  7. ide_set_handler
  8. lba_capacity_is_ok
  9. current_capacity
  10. ide_geninit
  11. ide_alloc
  12. init_gendisk
  13. reset_ihandler
  14. start_reset_timer
  15. reset_handler
  16. ide_do_reset
  17. 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. write_fs_long
  45. do_drive_cmd
  46. ide_open
  47. ide_release
  48. revalidate_disk
  49. ide_ioctl
  50. ide_check_media_change
  51. fixstring
  52. do_identify
  53. delay_10ms
  54. try_to_identify
  55. do_probe
  56. probe_for_drive
  57. probe_for_drives
  58. stridx
  59. match_parm
  60. ide_setup
  61. ide_xlate_1024
  62. probe_cmos_for_drives
  63. init_irq
  64. sub22
  65. try_to_init_dtc2278
  66. ide_init

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

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