Annotation of Gnu-Mach/linux/src/drivers/block/ide.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  *  linux/drivers/block/ide.c  Version 5.53  Jun  24, 1997
                      3:  *
                      4:  *  Copyright (C) 1994-1996  Linus Torvalds & authors (see below)
                      5:  */
                      6: #define _IDE_C         /* needed by <linux/blk.h> */
                      7: 
                      8: /*
                      9:  *  Maintained by Mark Lord  <[email protected]>
                     10:  *            and Gadi Oxman <[email protected]>
                     11:  *
                     12:  * This is the multiple IDE interface driver, as evolved from hd.c.
                     13:  * It supports up to four IDE interfaces, on one or more IRQs (usually 14 & 15).
                     14:  * There can be up to two drives per interface, as per the ATA-2 spec.
                     15:  *
                     16:  * Primary:    ide0, port 0x1f0; major=3;  hda is minor=0; hdb is minor=64
                     17:  * Secondary:  ide1, port 0x170; major=22; hdc is minor=0; hdd is minor=64
                     18:  * Tertiary:   ide2, port 0x???; major=33; hde is minor=0; hdf is minor=64
                     19:  * Quaternary: ide3, port 0x???; major=34; hdg is minor=0; hdh is minor=64
                     20:  *
                     21:  * It is easy to extend ide.c to handle more than four interfaces:
                     22:  *
                     23:  *     Change the MAX_HWIFS constant in ide.h.
                     24:  *
                     25:  *     Define some new major numbers (in major.h), and insert them into
                     26:  *     the ide_hwif_to_major table in ide.c.
                     27:  *
                     28:  *     Fill in the extra values for the new interfaces into the two tables
                     29:  *     inside ide.c:  default_io_base[]  and  default_irqs[].
                     30:  *
                     31:  *     Create the new request handlers by cloning "do_ide3_request()"
                     32:  *     for each new interface, and add them to the switch statement
                     33:  *     in the ide_init() function in ide.c.
                     34:  *
                     35:  *     Recompile, create the new /dev/ entries, and it will probably work.
                     36:  *
                     37:  *  From hd.c:
                     38:  *  |
                     39:  *  | It traverses the request-list, using interrupts to jump between functions.
                     40:  *  | As nearly all functions can be called within interrupts, we may not sleep.
                     41:  *  | Special care is recommended.  Have Fun!
                     42:  *  |
                     43:  *  | modified by Drew Eckhardt to check nr of hd's from the CMOS.
                     44:  *  |
                     45:  *  | Thanks to Branko Lankester, [email protected], who found a bug
                     46:  *  | in the early extended-partition checks and added DM partitions.
                     47:  *  |
                     48:  *  | Early work on error handling by Mika Liljeberg ([email protected]).
                     49:  *  |
                     50:  *  | IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
                     51:  *  | and general streamlining by Mark Lord ([email protected]).
                     52:  *
                     53:  *  October, 1994 -- Complete line-by-line overhaul for linux 1.1.x, by:
                     54:  *
                     55:  *     Mark Lord       ([email protected])               (IDE Perf.Pkg)
                     56:  *     Delman Lee      ([email protected])         ("Mr. atdisk2")
                     57:  *     Scott Snyder    ([email protected])        (ATAPI IDE cd-rom)
                     58:  *
                     59:  *  This was a rewrite of just about everything from hd.c, though some original
                     60:  *  code is still sprinkled about.  Think of it as a major evolution, with
                     61:  *  inspiration from lots of linux users, esp.  [email protected]
                     62:  *
                     63:  *  Version 1.0 ALPHA  initial code, primary i/f working okay
                     64:  *  Version 1.3 BETA   dual i/f on shared irq tested & working!
                     65:  *  Version 1.4 BETA   added auto probing for irq(s)
                     66:  *  Version 1.5 BETA   added ALPHA (untested) support for IDE cd-roms,
                     67:  *  ...
                     68:  *  Version 3.5                correct the bios_cyl field if it's too small
                     69:  *  (linux 1.1.76)      (to help fdisk with brain-dead BIOSs)
                     70:  *  Version 3.6                cosmetic corrections to comments and stuff
                     71:  *  (linux 1.1.77)     reorganise probing code to make it understandable
                     72:  *                     added halfway retry to probing for drive identification
                     73:  *                     added "hdx=noprobe" command line option
                     74:  *                     allow setting multmode even when identification fails
                     75:  *  Version 3.7                move set_geometry=1 from do_identify() to ide_init()
                     76:  *                     increase DRQ_WAIT to eliminate nuisance messages
                     77:  *                     wait for DRQ_STAT instead of DATA_READY during probing
                     78:  *                       (courtesy of Gary Thomas [email protected])
                     79:  *  Version 3.8                fixed byte-swapping for confused Mitsumi cdrom drives
                     80:  *                     update of ide-cd.c from Scott, allows blocksize=1024
                     81:  *                     cdrom probe fixes, inspired by [email protected]
                     82:  *  Version 3.9                don't use LBA if lba_capacity looks funny
                     83:  *                     correct the drive capacity calculations
                     84:  *                     fix probing for old Seagates without IDE_ALTSTATUS_REG
                     85:  *                     fix byte-ordering for some NEC cdrom drives
                     86:  *  Version 3.10       disable multiple mode by default; was causing trouble
                     87:  *  Version 3.11       fix mis-identification of old WD disks as cdroms
                     88:  *  Version 3,12       simplify logic for selecting initial mult_count
                     89:  *                       (fixes problems with buggy WD drives)
                     90:  *  Version 3.13       remove excess "multiple mode disabled" messages
                     91:  *  Version 3.14       fix ide_error() handling of BUSY_STAT
                     92:  *                     fix byte-swapped cdrom strings (again.. arghh!)
                     93:  *                     ignore INDEX bit when checking the ALTSTATUS reg
                     94:  *  Version 3.15       add SINGLE_THREADED flag for use with dual-CMD i/f
                     95:  *                     ignore WRERR_STAT for non-write operations
                     96:  *                     added vlb_sync support for DC-2000A & others,
                     97:  *                      (incl. some Promise chips), courtesy of Frank Gockel
                     98:  *  Version 3.16       convert vlb_32bit and vlb_sync into runtime flags
                     99:  *                     add ioctls to get/set VLB flags (HDIO_[SG]ET_CHIPSET)
                    100:  *                     rename SINGLE_THREADED to SUPPORT_SERIALIZE,
                    101:  *                     add boot flag to "serialize" operation for CMD i/f
                    102:  *                     add optional support for DTC2278 interfaces,
                    103:  *                      courtesy of [email protected] (Dyan Wile).
                    104:  *                     add boot flag to enable "dtc2278" probe
                    105:  *                     add probe to avoid EATA (SCSI) interfaces,
                    106:  *                      courtesy of [email protected].
                    107:  *  Version 4.00       tidy up verify_area() calls - [email protected]
                    108:  *                     add flag to ignore WRERR_STAT for some drives
                    109:  *                      courtesy of [email protected]
                    110:  *                     assembly syntax tweak to vlb_sync
                    111:  *                     removable drive support from [email protected]
                    112:  *                     add transparent support for DiskManager-6.0x "Dynamic
                    113:  *                      Disk Overlay" (DDO), most of this is in genhd.c
                    114:  *                     eliminate "multiple mode turned off" message at boot
                    115:  *  Version 4.10       fix bug in ioctl for "hdparm -c3"
                    116:  *                     fix DM6:DDO support -- now works with LILO, fdisk, ...
                    117:  *                     don't treat some naughty WD drives as removable
                    118:  *  Version 4.11       updated DM6 support using info provided by OnTrack
                    119:  *  Version 5.00       major overhaul, multmode setting fixed, vlb_sync fixed
                    120:  *                     added support for 3rd/4th/alternative IDE ports
                    121:  *                     created ide.h; ide-cd.c now compiles separate from ide.c
                    122:  *                     hopefully fixed infinite "unexpected_intr" from cdroms
                    123:  *                     zillions of other changes and restructuring
                    124:  *                     somehow reduced overall memory usage by several kB
                    125:  *                     probably slowed things down slightly, but worth it
                    126:  *  Version 5.01       AT LAST!!  Finally understood why "unexpected_intr"
                    127:  *                      was happening at various times/places:  whenever the
                    128:  *                      ide-interface's ctl_port was used to "mask" the irq,
                    129:  *                      it also would trigger an edge in the process of masking
                    130:  *                      which would result in a self-inflicted interrupt!!
                    131:  *                      (such a stupid way to build a hardware interrupt mask).
                    132:  *                      This is now fixed (after a year of head-scratching).
                    133:  *  Version 5.02       got rid of need for {enable,disable}_irq_list()
                    134:  *  Version 5.03       tune-ups, comments, remove "busy wait" from drive resets
                    135:  *                     removed PROBE_FOR_IRQS option -- no longer needed
                    136:  *                     OOOPS!  fixed "bad access" bug for 2nd drive on an i/f
                    137:  *  Version 5.04       changed "ira %d" to "irq %d" in DEBUG message
                    138:  *                     added more comments, cleaned up unexpected_intr()
                    139:  *                     OOOPS!  fixed null pointer problem in ide reset code
                    140:  *                     added autodetect for Triton chipset -- no effect yet
                    141:  *  Version 5.05       OOOPS!  fixed bug in revalidate_disk()
                    142:  *                     OOOPS!  fixed bug in ide_do_request()
                    143:  *                     added ATAPI reset sequence for cdroms
                    144:  *  Version 5.10       added Bus-Mastered DMA support for Triton Chipset
                    145:  *                     some (mostly) cosmetic changes
                    146:  *  Version 5.11       added ht6560b support by [email protected]
                    147:  *                     reworked PCI scanning code
                    148:  *                     added automatic RZ1000 detection/support
                    149:  *                     added automatic PCI CMD640 detection/support
                    150:  *                     added option for VLB CMD640 support
                    151:  *                     tweaked probe to find cdrom on hdb with disks on hda,hdc
                    152:  *  Version 5.12       some performance tuning
                    153:  *                     added message to alert user to bad /dev/hd[cd] entries
                    154:  *                     OOOPS!  fixed bug in atapi reset
                    155:  *                     driver now forces "serialize" again for all cmd640 chips
                    156:  *                     noticed REALLY_SLOW_IO had no effect, moved it to ide.c
                    157:  *                     made do_drive_cmd() into public ide_do_drive_cmd()
                    158:  *  Version 5.13       fixed typo ('B'), thanks to [email protected]
                    159:  *                     fixed ht6560b support
                    160:  *  Version 5.13b (sss)        fix problem in calling ide_cdrom_setup()
                    161:  *                     don't bother invalidating nonexistent partitions
                    162:  *  Version 5.14       fixes to cmd640 support.. maybe it works now(?)
                    163:  *                     added & tested full EZ-DRIVE support -- don't use LILO!
                    164:  *                     don't enable 2nd CMD640 PCI port during init - conflict
                    165:  *  Version 5.15       bug fix in init_cmd640_vlb()
                    166:  *                     bug fix in interrupt sharing code
                    167:  *  Version 5.16       ugh.. fix "serialize" support, broken in 5.15
                    168:  *                     remove "Huh?" from cmd640 code
                    169:  *                     added qd6580 interface speed select from Colten Edwards
                    170:  *  Version 5.17       kludge around bug in BIOS32 on Intel triton motherboards
                    171:  *  Version 5.18       new CMD640 code, moved to cmd640.c, #include'd for now
                    172:  *                     new UMC8672 code, moved to umc8672.c, #include'd for now
                    173:  *                     disallow turning on DMA when h/w not capable of DMA
                    174:  *  Version 5.19       fix potential infinite timeout on resets
                    175:  *                     extend reset poll into a general purpose polling scheme
                    176:  *                     add atapi tape drive support from Gadi Oxman
                    177:  *                     simplify exit from _intr routines -- no IDE_DO_REQUEST
                    178:  *  Version 5.20       leave current rq on blkdev request list during I/O
                    179:  *                     generalized ide_do_drive_cmd() for tape/cdrom driver use
                    180:  *  Version 5.21       fix nasty cdrom/tape bug (ide_preempt was messed up)
                    181:  *  Version 5.22       fix ide_xlate_1024() to work with/without drive->id
                    182:  *  Version 5.23       miscellaneous touch-ups
                    183:  *  Version 5.24       fix #if's for SUPPORT_CMD640
                    184:  *  Version 5.25       more touch-ups, fix cdrom resets, ...
                    185:  *                     cmd640.c now configs/compiles separate from ide.c
                    186:  *  Version 5.26       keep_settings now maintains the using_dma flag
                    187:  *                     fix [EZD] remap message to only output at boot time
                    188:  *                     fix "bad /dev/ entry" message to say hdc, not hdc0
                    189:  *                     fix ide_xlate_1024() to respect user specified CHS
                    190:  *                     use CHS from partn table if it looks translated
                    191:  *                     re-merged flags chipset,vlb_32bit,vlb_sync into io_32bit
                    192:  *                     keep track of interface chipset type, when known
                    193:  *                     add generic PIO mode "tuneproc" mechanism
                    194:  *                     fix cmd640_vlb option
                    195:  *                     fix ht6560b support (was completely broken)
                    196:  *                     umc8672.c now configures/compiles separate from ide.c
                    197:  *                     move dtc2278 support to dtc2278.c
                    198:  *                     move ht6560b support to ht6560b.c
                    199:  *                     move qd6580  support to qd6580.c
                    200:  *                     add  ali14xx support in ali14xx.c
                    201:  * Version 5.27                add [no]autotune parameters to help cmd640
                    202:  *                     move rz1000  support to rz1000.c
                    203:  * Version 5.28                #include "ide_modes.h"
                    204:  *                     fix disallow_unmask: now per-interface "no_unmask" bit
                    205:  *                     force io_32bit to be the same on drive pairs of dtc2278
                    206:  *                     improved IDE tape error handling, and tape DMA support
                    207:  *                     bugfix in ide_do_drive_cmd() for cdroms + serialize
                    208:  * Version 5.29                fixed non-IDE check for too many physical heads
                    209:  *                     don't use LBA if capacity is smaller than CHS
                    210:  * Version 5.30                remove real_devices kludge, formerly used by genhd.c
                    211:  * Version 5.32                change "KB" to "kB"
                    212:  *                     fix serialize (was broken in kernel 1.3.72)
                    213:  *                     add support for "hdparm -I"
                    214:  *                     use common code for disk/tape/cdrom IDE_DRIVE_CMDs
                    215:  *                     add support for Promise DC4030VL caching card
                    216:  *                     improved serialize support
                    217:  *                     put partition check back into alphabetical order
                    218:  *                     add config option for PCMCIA baggage
                    219:  *                     try to make PCMCIA support safer to use
                    220:  *                     improve security on ioctls(): all are suser() only
                    221:  * Version 5.33                improve handling of HDIO_DRIVE_CMDs that read data
                    222:  * Version 5.34                fix irq-sharing problem from 5.33
                    223:  *                     fix cdrom ioctl problem from 5.33
                    224:  * Version 5.35                cosmetic changes
                    225:  *                     fix cli() problem in try_to_identify()
                    226:  * Version 5.36                fixes to optional PCMCIA support
                    227:  * Version 5.37                don't use DMA when "noautotune" is specified
                    228:  * Version 5.37a (go)  fix shared irq probing (was broken in kernel 1.3.72)
                    229:  *                     call unplug_device() from ide_do_drive_cmd()
                    230:  * Version 5.38                add "hdx=none" option, courtesy of Joel Maslak
                    231:  *                     mask drive irq after use, if sharing with another hwif
                    232:  *                     add code to help debug weird cmd640 problems
                    233:  * Version 5.39                fix horrible error in earlier irq sharing "fix"
                    234:  * Version 5.40                fix serialization -- was broken in 5.39
                    235:  *                     help sharing by masking device irq after probing
                    236:  * Version 5.41                more fixes to irq sharing/serialize detection
                    237:  *                     disable io_32bit by default on drive reset
                    238:  * Version 5.42                simplify irq-masking after probe
                    239:  *                     fix NULL pointer deref in save_match()
                    240:  * Version 5.43                Ugh.. unexpected_intr is back: try to exterminate it
                    241:  * Version 5.44                Fix for "irq probe failed" on cmd640
                    242:  *                     change path on message regarding MAKEDEV.ide
                    243:  *                     add a throttle to the unexpected_intr() messages
                    244:  * Version 5.45                fix ugly parameter parsing bugs (thanks Derek)
                    245:  *                     include Gadi's magic fix for cmd640 unexpected_intr
                    246:  *                     include mc68000 patches from Geert Uytterhoeven
                    247:  *                     add Gadi's fix for PCMCIA cdroms
                    248:  * Version 5.46                remove the mc68000 #ifdefs for 2.0.x
                    249:  * Version 5.47                fix set_tune race condition
                    250:  *                     fix bug in earlier PCMCIA cdrom update
                    251:  * Version 5.48                if def'd, invoke CMD640_DUMP_REGS when irq probe fails
                    252:  *                     lengthen the do_reset1() pulse, for laptops
                    253:  *                     add idebus=xx parameter for cmd640 and ali chipsets
                    254:  *                     no_unmask flag now per-drive instead of per-hwif
                    255:  *                     fix tune_req so that it gets done immediately
                    256:  *                     fix missing restore_flags() in ide_ioctl
                    257:  *                     prevent use of io_32bit on cmd640 with no prefetch
                    258:  * Version 5.49                fix minor quirks in probing routines
                    259:  * Version 5.50                allow values as small as 20 for idebus=
                    260:  * Version 5.51                force non io_32bit in drive_cmd_intr()
                    261:  *                     change delay_10ms() to delay_50ms() to fix problems
                    262:  * Version 5.52                fix incorrect invalidation of removable devices
                    263:  *                     add "hdx=slow" command line option
                    264:  * Version 5.53                add ATAPI floppy drive support
                    265:  *                     change default media for type 0 to floppy
                    266:  *                     add support for Exabyte Nest
                    267:  *                     add missing set_blocksize() in revalidate_disk()
                    268:  *                     handle bad status bit sequencing in ide_wait_stat()
                    269:  *                     support partition table translations with 255 heads
                    270:  *                     probe all interfaces by default
                    271:  *                     add probe for the i82371AB chipset
                    272:  *                     acknowledge media change on removable drives
                    273:  *                     add work-around for BMI drives
                    274:  *                     remove "LBA" from boot messages
                    275:  * Version 5.53.1      add UDMA "CRC retry" support
                    276:  * Version 5.53.2      add Promise/33 auto-detection and DMA support
                    277:  *                     fix MC_ERR handling
                    278:  *                     fix mis-detection of NEC cdrom as floppy
                    279:  *                     issue ATAPI reset and re-probe after "no response"
                    280:  *
                    281:  *  Some additional driver compile-time options are in ide.h
                    282:  *
                    283:  *  To do, in likely order of completion:
                    284:  *     - modify kernel to obtain BIOS geometry for drives on 2nd/3rd/4th i/f
                    285: */
                    286: 
                    287: #undef REALLY_SLOW_IO          /* most systems can safely undef this */
                    288: 
                    289: #include <linux/config.h>
                    290: #include <linux/types.h>
                    291: #include <linux/string.h>
                    292: #include <linux/kernel.h>
                    293: #include <linux/delay.h>
                    294: #include <linux/timer.h>
                    295: #include <linux/mm.h>
                    296: #include <linux/ioport.h>
                    297: #include <linux/interrupt.h>
                    298: #include <linux/major.h>
                    299: #include <linux/blkdev.h>
                    300: #include <linux/errno.h>
                    301: #include <linux/hdreg.h>
                    302: #include <linux/genhd.h>
                    303: #include <linux/malloc.h>
                    304: 
                    305: #include <asm/byteorder.h>
                    306: #include <asm/irq.h>
                    307: #include <asm/segment.h>
                    308: #include <asm/io.h>
                    309: 
                    310: #ifdef CONFIG_PCI
                    311: #include <linux/bios32.h>
                    312: #include <linux/pci.h>
                    313: #endif /* CONFIG_PCI */
                    314: 
                    315: #include "ide.h"
                    316: #include "ide_modes.h"
                    317: 
                    318: #ifdef CONFIG_BLK_DEV_PROMISE
                    319: #include "promise.h"
                    320: #define IS_PROMISE_DRIVE (HWIF(drive)->chipset == ide_promise)
                    321: #else
                    322: #define IS_PROMISE_DRIVE (0)   /* auto-NULLs out Promise code */
                    323: #endif /* CONFIG_BLK_DEV_PROMISE */
                    324: 
                    325: static const byte      ide_hwif_to_major[MAX_HWIFS] = {IDE0_MAJOR, IDE1_MAJOR, IDE2_MAJOR, IDE3_MAJOR};
                    326: static const unsigned short default_io_base[MAX_HWIFS] = {0x1f0, 0x170, 0x1e8, 0x168};
                    327: static const byte      default_irqs[MAX_HWIFS]     = {14, 15, 11, 10};
                    328: static int     idebus_parameter; /* holds the "idebus=" parameter */
                    329: static int     system_bus_speed; /* holds what we think is VESA/PCI bus speed */
                    330: 
                    331: /*
                    332:  * This is declared extern in ide.h, for access by other IDE modules:
                    333:  */
                    334: ide_hwif_t     ide_hwifs[MAX_HWIFS];   /* master data repository */
                    335: 
                    336: #if (DISK_RECOVERY_TIME > 0)
                    337: /*
                    338:  * For really screwy hardware (hey, at least it *can* be used with Linux)
                    339:  * we can enforce a minimum delay time between successive operations.
                    340:  */
                    341: static unsigned long read_timer(void)
                    342: {
                    343:        unsigned long t, flags;
                    344:        int i;
                    345: 
                    346:        save_flags(flags);
                    347:        cli();
                    348:        t = jiffies * 11932;
                    349:        outb_p(0, 0x43);
                    350:        i = inb_p(0x40);
                    351:        i |= inb(0x40) << 8;
                    352:        restore_flags(flags);
                    353:        return (t - i);
                    354: }
                    355: 
                    356: static void set_recovery_timer (ide_hwif_t *hwif)
                    357: {
                    358:        hwif->last_time = read_timer();
                    359: }
                    360: #define SET_RECOVERY_TIMER(drive) set_recovery_timer (drive)
                    361: 
                    362: #else
                    363: 
                    364: #define SET_RECOVERY_TIMER(drive)
                    365: 
                    366: #endif /* DISK_RECOVERY_TIME */
                    367: 
                    368: 
                    369: /*
                    370:  * Do not even *think* about calling this!
                    371:  */
                    372: static void init_hwif_data (unsigned int index)
                    373: {
                    374:        byte *p;
                    375:        unsigned int unit;
                    376:        ide_hwif_t *hwif = &ide_hwifs[index];
                    377: 
                    378:        /* bulk initialize hwif & drive info with zeros */
                    379:        p = ((byte *) hwif) + sizeof(ide_hwif_t);
                    380:        do {
                    381:                *--p = 0;
                    382:        } while (p > (byte *) hwif);
                    383: 
                    384:        /* fill in any non-zero initial values */
                    385:        hwif->index     = index;
                    386:        hwif->io_base   = default_io_base[index];
                    387:        hwif->ctl_port  = hwif->io_base ? hwif->io_base+0x206 : 0x000;
                    388: #ifdef CONFIG_BLK_DEV_HD
                    389:        if (hwif->io_base == HD_DATA)
                    390:                hwif->noprobe = 1; /* may be overridden by ide_setup() */
                    391: #endif /* CONFIG_BLK_DEV_HD */
                    392:        hwif->major     = ide_hwif_to_major[index];
                    393:        hwif->name[0]   = 'i';
                    394:        hwif->name[1]   = 'd';
                    395:        hwif->name[2]   = 'e';
                    396:        hwif->name[3]   = '0' + index;
                    397: #ifdef CONFIG_BLK_DEV_IDETAPE
                    398:        hwif->tape_drive = NULL;
                    399: #endif /* CONFIG_BLK_DEV_IDETAPE */
                    400:        for (unit = 0; unit < MAX_DRIVES; ++unit) {
                    401:                ide_drive_t *drive = &hwif->drives[unit];
                    402: 
                    403:                drive->select.all               = (unit<<4)|0xa0;
                    404:                drive->hwif                     = hwif;
                    405:                drive->ctl                      = 0x08;
                    406:                drive->ready_stat               = READY_STAT;
                    407:                drive->bad_wstat                = BAD_W_STAT;
                    408:                drive->special.b.recalibrate    = 1;
                    409:                drive->special.b.set_geometry   = 1;
                    410:                drive->name[0]                  = 'h';
                    411:                drive->name[1]                  = 'd';
                    412:                drive->name[2]                  = 'a' + (index * MAX_DRIVES) + unit;
                    413:        }
                    414: }
                    415: 
                    416: /*
                    417:  * init_ide_data() sets reasonable default values into all fields
                    418:  * of all instances of the hwifs and drives, but only on the first call.
                    419:  * Subsequent calls have no effect (they don't wipe out anything).
                    420:  *
                    421:  * This routine is normally called at driver initialization time,
                    422:  * but may also be called MUCH earlier during kernel "command-line"
                    423:  * parameter processing.  As such, we cannot depend on any other parts
                    424:  * of the kernel (such as memory allocation) to be functioning yet.
                    425:  *
                    426:  * This is too bad, as otherwise we could dynamically allocate the
                    427:  * ide_drive_t structs as needed, rather than always consuming memory
                    428:  * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
                    429:  */
                    430: #define MAGIC_COOKIE 0x12345678
                    431: static void init_ide_data (void)
                    432: {
                    433:        unsigned int index;
                    434:        static unsigned long magic_cookie = MAGIC_COOKIE;
                    435: 
                    436:        if (magic_cookie != MAGIC_COOKIE)
                    437:                return;         /* already initialized */
                    438:        magic_cookie = 0;
                    439: 
                    440:        for (index = 0; index < MAX_HWIFS; ++index)
                    441:                init_hwif_data(index);
                    442: 
                    443:        idebus_parameter = 0;
                    444:        system_bus_speed = 0;
                    445: }
                    446: 
                    447: /*
                    448:  * ide_system_bus_speed() returns what we think is the system VESA/PCI
                    449:  * bus speed (in Mhz).  This is used for calculating interface PIO timings.
                    450:  * The default is 40 for known PCI systems, 50 otherwise.
                    451:  * The "idebus=xx" parameter can be used to override this value.
                    452:  * The actual value to be used is computed/displayed the first time through.
                    453:  */
                    454: int ide_system_bus_speed (void)
                    455: {
                    456:        if (!system_bus_speed) {
                    457:                if (idebus_parameter)
                    458:                        system_bus_speed = idebus_parameter;    /* user supplied value */
                    459: #ifdef CONFIG_PCI
                    460:                else if (pcibios_present())
                    461:                        system_bus_speed = 40;  /* safe default value for PCI */
                    462: #endif /* CONFIG_PCI */
                    463:                else
                    464:                        system_bus_speed = 50;  /* safe default value for VESA and PCI */
                    465:                printk("ide: Assuming %dMhz system bus speed for PIO modes; override with idebus=xx\n", system_bus_speed);
                    466:        }
                    467:        return system_bus_speed;
                    468: }
                    469: 
                    470: #if SUPPORT_VLB_SYNC
                    471: /*
                    472:  * Some localbus EIDE interfaces require a special access sequence
                    473:  * when using 32-bit I/O instructions to transfer data.  We call this
                    474:  * the "vlb_sync" sequence, which consists of three successive reads
                    475:  * of the sector count register location, with interrupts disabled
                    476:  * to ensure that the reads all happen together.
                    477:  */
                    478: static inline void do_vlb_sync (unsigned short port) {
                    479:        (void) inb (port);
                    480:        (void) inb (port);
                    481:        (void) inb (port);
                    482: }
                    483: #endif /* SUPPORT_VLB_SYNC */
                    484: 
                    485: /*
                    486:  * This is used for most PIO data transfers *from* the IDE interface
                    487:  */
                    488: void ide_input_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
                    489: {
                    490:        unsigned short io_base  = HWIF(drive)->io_base;
                    491:        unsigned short data_reg = io_base+IDE_DATA_OFFSET;
                    492:        byte io_32bit = drive->io_32bit;
                    493: 
                    494:        if (io_32bit) {
                    495: #if SUPPORT_VLB_SYNC
                    496:                if (io_32bit & 2) {
                    497:                        cli();
                    498:                        do_vlb_sync(io_base+IDE_NSECTOR_OFFSET);
                    499:                        insl(data_reg, buffer, wcount);
                    500:                        if (drive->unmask)
                    501:                                sti();
                    502:                } else
                    503: #endif /* SUPPORT_VLB_SYNC */
                    504:                        insl(data_reg, buffer, wcount);
                    505:        } else {
                    506: #if SUPPORT_SLOW_DATA_PORTS
                    507:                if (drive->slow) {
                    508:                        unsigned short *ptr = (unsigned short *) buffer;
                    509:                        while (wcount--) {
                    510:                                *ptr++ = inw_p(data_reg);
                    511:                                *ptr++ = inw_p(data_reg);
                    512:                        }
                    513:                } else
                    514: #endif /* SUPPORT_SLOW_DATA_PORTS */
                    515:                        insw(data_reg, buffer, wcount<<1);
                    516:        }
                    517: }
                    518: 
                    519: /*
                    520:  * This is used for most PIO data transfers *to* the IDE interface
                    521:  */
                    522: void ide_output_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
                    523: {
                    524:        unsigned short io_base  = HWIF(drive)->io_base;
                    525:        unsigned short data_reg = io_base+IDE_DATA_OFFSET;
                    526:        byte io_32bit = drive->io_32bit;
                    527: 
                    528:        if (io_32bit) {
                    529: #if SUPPORT_VLB_SYNC
                    530:                if (io_32bit & 2) {
                    531:                        cli();
                    532:                        do_vlb_sync(io_base+IDE_NSECTOR_OFFSET);
                    533:                        outsl(data_reg, buffer, wcount);
                    534:                        if (drive->unmask)
                    535:                                sti();
                    536:                } else
                    537: #endif /* SUPPORT_VLB_SYNC */
                    538:                        outsl(data_reg, buffer, wcount);
                    539:        } else {
                    540: #if SUPPORT_SLOW_DATA_PORTS
                    541:                if (drive->slow) {
                    542:                        unsigned short *ptr = (unsigned short *) buffer;
                    543:                        while (wcount--) {
                    544:                                outw_p(*ptr++, data_reg);
                    545:                                outw_p(*ptr++, data_reg);
                    546:                        }
                    547:                } else
                    548: #endif /* SUPPORT_SLOW_DATA_PORTS */
                    549:                        outsw(data_reg, buffer, wcount<<1);
                    550:        }
                    551: }
                    552: 
                    553: /*
                    554:  * The following routines are mainly used by the ATAPI drivers.
                    555:  *
                    556:  * These routines will round up any request for an odd number of bytes,
                    557:  * so if an odd bytecount is specified, be sure that there's at least one
                    558:  * extra byte allocated for the buffer.
                    559:  */
                    560: void atapi_input_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount)
                    561: {
                    562:        ++bytecount;
                    563:        ide_input_data (drive, buffer, bytecount / 4);
                    564:        if ((bytecount & 0x03) >= 2)
                    565:                insw (IDE_DATA_REG, ((byte *)buffer) + (bytecount & ~0x03), 1);
                    566: }
                    567: 
                    568: void atapi_output_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount)
                    569: {
                    570:        ++bytecount;
                    571:        ide_output_data (drive, buffer, bytecount / 4);
                    572:        if ((bytecount & 0x03) >= 2)
                    573:                outsw (IDE_DATA_REG, ((byte *)buffer) + (bytecount & ~0x03), 1);
                    574: }
                    575: 
                    576: /*
                    577:  * This should get invoked any time we exit the driver to
                    578:  * wait for an interrupt response from a drive.  handler() points
                    579:  * at the appropriate code to handle the next interrupt, and a
                    580:  * timer is started to prevent us from waiting forever in case
                    581:  * something goes wrong (see the timer_expiry() handler later on).
                    582:  */
                    583: void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler, unsigned int timeout)
                    584: {
                    585:        ide_hwgroup_t *hwgroup = HWGROUP(drive);
                    586: #ifdef DEBUG
                    587:        if (hwgroup->handler != NULL) {
                    588:                printk("%s: ide_set_handler: handler not null; old=%p, new=%p\n",
                    589:                        drive->name, hwgroup->handler, handler);
                    590:        }
                    591: #endif
                    592:        hwgroup->handler       = handler;
                    593:        hwgroup->timer.expires = jiffies + timeout;
                    594:        add_timer(&(hwgroup->timer));
                    595: }
                    596: 
                    597: /*
                    598:  * lba_capacity_is_ok() performs a sanity check on the claimed "lba_capacity"
                    599:  * value for this drive (from its reported identification information).
                    600:  *
                    601:  * Returns:    1 if lba_capacity looks sensible
                    602:  *             0 otherwise
                    603:  */
                    604: static int lba_capacity_is_ok (struct hd_driveid *id)
                    605: {
                    606:        unsigned long lba_sects   = id->lba_capacity;
                    607:        unsigned long chs_sects   = id->cyls * id->heads * id->sectors;
                    608:        unsigned long _10_percent = chs_sects / 10;
                    609: 
                    610:        /* very large drives (8GB+) may lie about the number of cylinders */
                    611:        if (id->cyls == 16383 && id->heads == 16 && id->sectors == 63 && lba_sects > chs_sects) {
                    612:                id->cyls = lba_sects / (16 * 63); /* correct cyls */
                    613:                return 1;       /* lba_capacity is our only option */
                    614:        }
                    615:        /* perform a rough sanity check on lba_sects:  within 10% is "okay" */
                    616:        if ((lba_sects - chs_sects) < _10_percent)
                    617:                return 1;       /* lba_capacity is good */
                    618: 
                    619:        /* some drives have the word order reversed */
                    620:        lba_sects = (lba_sects << 16) | (lba_sects >> 16);
                    621:        if ((lba_sects - chs_sects) < _10_percent) {
                    622:                id->lba_capacity = lba_sects;   /* fix it */
                    623:                return 1;       /* lba_capacity is (now) good */
                    624:        }
                    625:        return 0;       /* lba_capacity value is bad */
                    626: }
                    627: 
                    628: /*
                    629:  * current_capacity() returns the capacity (in sectors) of a drive
                    630:  * according to its current geometry/LBA settings.
                    631:  */
                    632: static unsigned long current_capacity (ide_drive_t  *drive)
                    633: {
                    634:        struct hd_driveid *id = drive->id;
                    635:        unsigned long capacity = drive->cyl * drive->head * drive->sect;
                    636: 
                    637:        if (!drive->present)
                    638:                return 0;
                    639: #ifdef CONFIG_BLK_DEV_IDEFLOPPY
                    640:        if (drive->media == ide_floppy)
                    641:                return idefloppy_capacity(drive);
                    642: #endif /* CONFIG_BLK_DEV_IDEFLOPPY */
                    643:        if (drive->media != ide_disk)
                    644:                return 0x7fffffff;      /* cdrom or tape */
                    645:        drive->select.b.lba = 0;
                    646:        /* Determine capacity, and use LBA if the drive properly supports it */
                    647:        if (id != NULL && (id->capability & 2) && lba_capacity_is_ok(id)) {
                    648:                if (id->lba_capacity >= capacity) {
                    649:                        drive->cyl = id->lba_capacity / (drive->head * drive->sect);
                    650:                        capacity = id->lba_capacity;
                    651:                        drive->select.b.lba = 1;
                    652:                }
                    653:        }
                    654:        return (capacity - drive->sect0);
                    655: }
                    656: 
                    657: /*
                    658:  * ide_geninit() is called exactly *once* for each major, from genhd.c,
                    659:  * at the beginning of the initial partition check for the drives.
                    660:  */
                    661: static void ide_geninit (struct gendisk *gd)
                    662: {
                    663:        unsigned int unit;
                    664:        ide_hwif_t *hwif = gd->real_devices;
                    665: 
                    666:        for (unit = 0; unit < gd->nr_real; ++unit) {
                    667:                ide_drive_t *drive = &hwif->drives[unit];
                    668: #ifdef CONFIG_BLK_DEV_IDECD
                    669:                if (drive->present && drive->media == ide_cdrom)
                    670:                        ide_cdrom_setup(drive);
                    671: #endif /* CONFIG_BLK_DEV_IDECD */
                    672: #ifdef CONFIG_BLK_DEV_IDETAPE
                    673:                if (drive->present && drive->media == ide_tape)
                    674:                        idetape_setup(drive);
                    675: #endif /* CONFIG_BLK_DEV_IDETAPE */
                    676: #ifdef CONFIG_BLK_DEV_IDEFLOPPY
                    677:                if (drive->present && drive->media == ide_floppy)
                    678:                        idefloppy_setup(drive);
                    679: #endif /* CONFIG_BLK_DEV_IDEFLOPPY */
                    680:                drive->part[0].nr_sects = current_capacity(drive);
                    681:                if (!drive->present || (drive->media != ide_disk && drive->media != ide_floppy) ||
                    682:                    !drive->part[0].nr_sects) {
                    683:                        drive->part[0].start_sect = -1; /* skip partition check */
                    684:                }
                    685:        }
                    686: }
                    687: 
                    688: /*
                    689:  * init_gendisk() (as opposed to ide_geninit) is called for each major device,
                    690:  * after probing for drives, to allocate partition tables and other data
                    691:  * structures needed for the routines in genhd.c.  ide_geninit() gets called
                    692:  * somewhat later, during the partition check.
                    693:  */
                    694: static void init_gendisk (ide_hwif_t *hwif)
                    695: {
                    696:        struct gendisk *gd, **gdp;
                    697:        unsigned int unit, units, minors;
                    698:        int *bs;
                    699: 
                    700:        /* figure out maximum drive number on the interface */
                    701:        for (units = MAX_DRIVES; units > 0; --units) {
                    702:                if (hwif->drives[units-1].present)
                    703:                        break;
                    704:        }
                    705:        minors    = units * (1<<PARTN_BITS);
                    706:        gd        = kmalloc (sizeof(struct gendisk), GFP_KERNEL);
                    707:        gd->sizes = kmalloc (minors * sizeof(int), GFP_KERNEL);
                    708:        gd->part  = kmalloc (minors * sizeof(struct hd_struct), GFP_KERNEL);
                    709:        bs        = kmalloc (minors*sizeof(int), GFP_KERNEL);
                    710: 
                    711:        memset(gd->part, 0, minors * sizeof(struct hd_struct));
                    712: 
                    713:        /* cdroms and msdos f/s are examples of non-1024 blocksizes */
                    714:        blksize_size[hwif->major] = bs;
                    715:        for (unit = 0; unit < minors; ++unit)
                    716:                *bs++ = BLOCK_SIZE;
                    717: 
                    718:        for (unit = 0; unit < units; ++unit)
                    719:                hwif->drives[unit].part = &gd->part[unit << PARTN_BITS];
                    720: 
                    721:        gd->major       = hwif->major;          /* our major device number */
                    722:        gd->major_name  = IDE_MAJOR_NAME;       /* treated special in genhd.c */
                    723:        gd->minor_shift = PARTN_BITS;           /* num bits for partitions */
                    724:        gd->max_p       = 1<<PARTN_BITS;        /* 1 + max partitions / drive */
                    725:        gd->max_nr      = units;                /* max num real drives */
                    726:        gd->nr_real     = units;                /* current num real drives */
                    727:        gd->init        = ide_geninit;          /* initialization function */
                    728:        gd->real_devices= hwif;                 /* ptr to internal data */
                    729:        gd->next        = NULL;                 /* linked list of major devs */
                    730: 
                    731:        for (gdp = &gendisk_head; *gdp; gdp = &((*gdp)->next)) ;
                    732:        hwif->gd = *gdp = gd;                   /* link onto tail of list */
                    733: }
                    734: 
                    735: static void do_reset1 (ide_drive_t *, int);            /* needed below */
                    736: 
                    737: #ifdef CONFIG_BLK_DEV_IDEATAPI
                    738: /*
                    739:  * atapi_reset_pollfunc() gets invoked to poll the interface for completion every 50ms
                    740:  * during an atapi drive reset operation. If the drive has not yet responded,
                    741:  * and we have not yet hit our maximum waiting time, then the timer is restarted
                    742:  * for another 50ms.
                    743:  */
                    744: static void atapi_reset_pollfunc (ide_drive_t *drive)
                    745: {
                    746:        ide_hwgroup_t *hwgroup = HWGROUP(drive);
                    747:        byte stat;
                    748: 
                    749:        OUT_BYTE (drive->select.all, IDE_SELECT_REG);
                    750:        udelay (10);
                    751: 
                    752:        if (OK_STAT(stat=GET_STAT(), 0, BUSY_STAT)) {
                    753:                printk("%s: ATAPI reset complete\n", drive->name);
                    754:        } else {
                    755:                if (jiffies < hwgroup->poll_timeout) {
                    756:                        ide_set_handler (drive, &atapi_reset_pollfunc, HZ/20);
                    757:                        return; /* continue polling */
                    758:                }
                    759:                hwgroup->poll_timeout = 0;      /* end of polling */
                    760:                printk("%s: ATAPI reset timed-out, status=0x%02x\n", drive->name, stat);
                    761:                do_reset1 (drive, 1);   /* do it the old fashioned way */
                    762:                return;
                    763:        }
                    764:        hwgroup->poll_timeout = 0;      /* done polling */
                    765: }
                    766: #endif /* CONFIG_BLK_DEV_IDEATAPI */
                    767: 
                    768: /*
                    769:  * reset_pollfunc() gets invoked to poll the interface for completion every 50ms
                    770:  * during an ide reset operation. If the drives have not yet responded,
                    771:  * and we have not yet hit our maximum waiting time, then the timer is restarted
                    772:  * for another 50ms.
                    773:  */
                    774: static void reset_pollfunc (ide_drive_t *drive)
                    775: {
                    776:        ide_hwgroup_t *hwgroup = HWGROUP(drive);
                    777:        ide_hwif_t *hwif = HWIF(drive);
                    778:        byte tmp;
                    779: 
                    780:        if (!OK_STAT(tmp=GET_STAT(), 0, BUSY_STAT)) {
                    781:                if (jiffies < hwgroup->poll_timeout) {
                    782:                        ide_set_handler (drive, &reset_pollfunc, HZ/20);
                    783:                        return; /* continue polling */
                    784:                }
                    785:                printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
                    786:        } else  {
                    787:                printk("%s: reset: ", hwif->name);
                    788:                if ((tmp = GET_ERR()) == 1)
                    789:                        printk("success\n");
                    790:                else {
                    791: #if FANCY_STATUS_DUMPS
                    792:                        printk("master: ");
                    793:                        switch (tmp & 0x7f) {
                    794:                                case 1: printk("passed");
                    795:                                        break;
                    796:                                case 2: printk("formatter device error");
                    797:                                        break;
                    798:                                case 3: printk("sector buffer error");
                    799:                                        break;
                    800:                                case 4: printk("ECC circuitry error");
                    801:                                        break;
                    802:                                case 5: printk("controlling MPU error");
                    803:                                        break;
                    804:                                default:printk("error (0x%02x?)", tmp);
                    805:                        }
                    806:                        if (tmp & 0x80)
                    807:                                printk("; slave: failed");
                    808:                        printk("\n");
                    809: #else
                    810:                        printk("failed\n");
                    811: #endif /* FANCY_STATUS_DUMPS */
                    812:                }
                    813:        }
                    814:        hwgroup->poll_timeout = 0;      /* done polling */
                    815: }
                    816: 
                    817: /*
                    818:  * do_reset1() attempts to recover a confused drive by resetting it.
                    819:  * Unfortunately, resetting a disk drive actually resets all devices on
                    820:  * the same interface, so it can really be thought of as resetting the
                    821:  * interface rather than resetting the drive.
                    822:  *
                    823:  * ATAPI devices have their own reset mechanism which allows them to be
                    824:  * individually reset without clobbering other devices on the same interface.
                    825:  *
                    826:  * Unfortunately, the IDE interface does not generate an interrupt to let
                    827:  * us know when the reset operation has finished, so we must poll for this.
                    828:  * Equally poor, though, is the fact that this may a very long time to complete,
                    829:  * (up to 30 seconds worstcase).  So, instead of busy-waiting here for it,
                    830:  * we set a timer to poll at 50ms intervals.
                    831:  */
                    832: static void do_reset1 (ide_drive_t *drive, int  do_not_try_atapi)
                    833: {
                    834:        unsigned int unit;
                    835:        unsigned long flags;
                    836:        ide_hwif_t *hwif = HWIF(drive);
                    837:        ide_hwgroup_t *hwgroup = HWGROUP(drive);
                    838: 
                    839:        save_flags(flags);
                    840:        cli();          /* Why ? */
                    841: 
                    842: #ifdef CONFIG_BLK_DEV_IDEATAPI
                    843:        /* For an ATAPI device, first try an ATAPI SRST. */
                    844:        if (drive->media != ide_disk) {
                    845:                if (!do_not_try_atapi) {
                    846:                        if (!drive->keep_settings) {
                    847:                                drive->unmask = 0;
                    848:                                drive->io_32bit = 0;
                    849:                        }
                    850:                        OUT_BYTE (drive->select.all, IDE_SELECT_REG);
                    851:                        udelay (20);
                    852:                        OUT_BYTE (WIN_SRST, IDE_COMMAND_REG);
                    853:                        hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
                    854:                        ide_set_handler (drive, &atapi_reset_pollfunc, HZ/20);
                    855:                        restore_flags (flags);
                    856:                        return;
                    857:                }
                    858:        }
                    859: #endif /* CONFIG_BLK_DEV_IDEATAPI */
                    860: 
                    861:        /*
                    862:         * First, reset any device state data we were maintaining
                    863:         * for any of the drives on this interface.
                    864:         */
                    865:        for (unit = 0; unit < MAX_DRIVES; ++unit) {
                    866:                ide_drive_t *rdrive = &hwif->drives[unit];
                    867: #ifdef CONFIG_BLK_DEV_IDETAPE
                    868:                if (rdrive->media == ide_tape)
                    869:                        rdrive->tape.reset_issued = 1;
                    870: #endif /* CONFIG_BLK_DEV_IDETAPE */
                    871:                rdrive->special.all = 0;
                    872:                rdrive->special.b.set_geometry = 1;
                    873:                rdrive->special.b.recalibrate  = 1;
                    874:                if (OK_TO_RESET_CONTROLLER)
                    875:                        rdrive->mult_count = 0;
                    876:                if (!rdrive->keep_settings) {
                    877:                        rdrive->mult_req = 0;
                    878:                        rdrive->unmask = 0;
                    879:                        rdrive->io_32bit = 0;
                    880:                        if (rdrive->using_dma) {
                    881:                                rdrive->using_dma = 0;
                    882:                                printk("%s: disabled DMA\n", rdrive->name);
                    883:                        }
                    884:                }
                    885:                if (rdrive->mult_req != rdrive->mult_count)
                    886:                        rdrive->special.b.set_multmode = 1;
                    887:        }
                    888: 
                    889: #if OK_TO_RESET_CONTROLLER
                    890:        /*
                    891:         * Note that we also set nIEN while resetting the device,
                    892:         * to mask unwanted interrupts from the interface during the reset.
                    893:         * However, due to the design of PC hardware, this will cause an
                    894:         * immediate interrupt due to the edge transition it produces.
                    895:         * This single interrupt gives us a "fast poll" for drives that
                    896:         * recover from reset very quickly, saving us the first 50ms wait time.
                    897:         */
                    898:        OUT_BYTE(drive->ctl|6,IDE_CONTROL_REG); /* set SRST and nIEN */
                    899:        udelay(10);                     /* more than enough time */
                    900:        OUT_BYTE(drive->ctl|2,IDE_CONTROL_REG); /* clear SRST, leave nIEN */
                    901:        udelay(10);                     /* more than enough time */
                    902:        hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
                    903:        ide_set_handler (drive, &reset_pollfunc, HZ/20);
                    904: #endif /* OK_TO_RESET_CONTROLLER */
                    905: 
                    906:        restore_flags (flags);
                    907: }
                    908: 
                    909: /*
                    910:  * ide_do_reset() is the entry point to the drive/interface reset code.
                    911:  */
                    912: void ide_do_reset (ide_drive_t *drive)
                    913: {
                    914:        do_reset1 (drive, 0);
                    915: #ifdef CONFIG_BLK_DEV_IDETAPE
                    916:        if (drive->media == ide_tape)
                    917:                drive->tape.reset_issued=1;
                    918: #endif /* CONFIG_BLK_DEV_IDETAPE */
                    919: }
                    920: 
                    921: /*
                    922:  * Clean up after success/failure of an explicit drive cmd
                    923:  */
                    924: void ide_end_drive_cmd (ide_drive_t *drive, byte stat, byte err)
                    925: {
                    926:        unsigned long flags;
                    927:        struct request *rq = HWGROUP(drive)->rq;
                    928: 
                    929:        if (rq->cmd == IDE_DRIVE_CMD) {
                    930:                byte *args = (byte *) rq->buffer;
                    931:                rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
                    932:                if (args) {
                    933:                        args[0] = stat;
                    934:                        args[1] = err;
                    935:                        args[2] = IN_BYTE(IDE_NSECTOR_REG);
                    936:                }
                    937:        }
                    938:        save_flags(flags);
                    939:        cli();
                    940:        blk_dev[MAJOR(rq->rq_dev)].current_request = rq->next;
                    941:        HWGROUP(drive)->rq = NULL;
                    942:        rq->rq_status = RQ_INACTIVE;
                    943:        if (rq->sem != NULL)
                    944:                up(rq->sem);
                    945:        restore_flags(flags);
                    946: }
                    947: 
                    948: /*
                    949:  * Error reporting, in human readable form (luxurious, but a memory hog).
                    950:  */
                    951: byte ide_dump_status (ide_drive_t *drive, const char *msg, byte stat)
                    952: {
                    953:        unsigned long flags;
                    954:        byte err = 0;
                    955: 
                    956:        save_flags (flags);
                    957:        sti();
                    958:        printk("%s: %s: status=0x%02x", drive->name, msg, stat);
                    959: #if FANCY_STATUS_DUMPS
                    960:        if (drive->media == ide_disk) {
                    961:                printk(" { ");
                    962:                if (stat & BUSY_STAT)
                    963:                        printk("Busy ");
                    964:                else {
                    965:                        if (stat & READY_STAT)  printk("DriveReady ");
                    966:                        if (stat & WRERR_STAT)  printk("DeviceFault ");
                    967:                        if (stat & SEEK_STAT)   printk("SeekComplete ");
                    968:                        if (stat & DRQ_STAT)    printk("DataRequest ");
                    969:                        if (stat & ECC_STAT)    printk("CorrectedError ");
                    970:                        if (stat & INDEX_STAT)  printk("Index ");
                    971:                        if (stat & ERR_STAT)    printk("Error ");
                    972:                }
                    973:                printk("}");
                    974:        }
                    975: #endif /* FANCY_STATUS_DUMPS */
                    976:        printk("\n");
                    977:        if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
                    978:                err = GET_ERR();
                    979:                printk("%s: %s: error=0x%02x", drive->name, msg, err);
                    980: #if FANCY_STATUS_DUMPS
                    981:                if (drive->media == ide_disk) {
                    982:                        printk(" { ");
                    983:                        if (err & ICRC_ERR)     printk((err & ABRT_ERR) ? "BadCRC " : "BadSector ");
                    984:                        if (err & ECC_ERR)      printk("UncorrectableError ");
                    985:                        if (err & ID_ERR)       printk("SectorIdNotFound ");
                    986:                        if (err & ABRT_ERR)     printk("DriveStatusError ");
                    987:                        if (err & TRK0_ERR)     printk("TrackZeroNotFound ");
                    988:                        if (err & MARK_ERR)     printk("AddrMarkNotFound ");
                    989:                        printk("}");
                    990:                        if (err & (BBD_ERR|ECC_ERR|ID_ERR|MARK_ERR)) {
                    991:                                byte cur = IN_BYTE(IDE_SELECT_REG);
                    992:                                if (cur & 0x40) {       /* using LBA? */
                    993:                                        printk(", LBAsect=%ld", (unsigned long)
                    994:                                         ((cur&0xf)<<24)
                    995:                                         |(IN_BYTE(IDE_HCYL_REG)<<16)
                    996:                                         |(IN_BYTE(IDE_LCYL_REG)<<8)
                    997:                                         | IN_BYTE(IDE_SECTOR_REG));
                    998:                                } else {
                    999:                                        printk(", CHS=%d/%d/%d",
                   1000:                                         (IN_BYTE(IDE_HCYL_REG)<<8) +
                   1001:                                          IN_BYTE(IDE_LCYL_REG),
                   1002:                                          cur & 0xf,
                   1003:                                          IN_BYTE(IDE_SECTOR_REG));
                   1004:                                }
                   1005:                                if (HWGROUP(drive)->rq)
                   1006:                                        printk(", sector=%ld", HWGROUP(drive)->rq->sector);
                   1007:                        }
                   1008:                }
                   1009: #endif /* FANCY_STATUS_DUMPS */
                   1010:                printk("\n");
                   1011:        }
                   1012:        restore_flags (flags);
                   1013:        return err;
                   1014: }
                   1015: 
                   1016: /*
                   1017:  * try_to_flush_leftover_data() is invoked in response to a drive
                   1018:  * unexpectedly having its DRQ_STAT bit set.  As an alternative to
                   1019:  * resetting the drive, this routine tries to clear the condition
                   1020:  * by read a sector's worth of data from the drive.  Of course,
                   1021:  * this may not help if the drive is *waiting* for data from *us*.
                   1022:  */
                   1023: static void try_to_flush_leftover_data (ide_drive_t *drive)
                   1024: {
                   1025:        int i = (drive->mult_count ? drive->mult_count : 1) * SECTOR_WORDS;
                   1026: 
                   1027:        while (i > 0) {
                   1028:                unsigned long buffer[16];
                   1029:                unsigned int wcount = (i > 16) ? 16 : i;
                   1030:                i -= wcount;
                   1031:                ide_input_data (drive, buffer, wcount);
                   1032:        }
                   1033: }
                   1034: 
                   1035: /*
                   1036:  * ide_error() takes action based on the error returned by the controller.
                   1037:  */
                   1038: void ide_error (ide_drive_t *drive, const char *msg, byte stat)
                   1039: {
                   1040:        struct request *rq;
                   1041:        byte err;
                   1042: 
                   1043:        err = ide_dump_status(drive, msg, stat);
                   1044:        if ((rq = HWGROUP(drive)->rq) == NULL || drive == NULL)
                   1045:                return;
                   1046:        /* retry only "normal" I/O: */
                   1047:        if (rq->cmd == IDE_DRIVE_CMD) {
                   1048:                rq->errors = 1;
                   1049:                ide_end_drive_cmd(drive, stat, err);
                   1050:                return;
                   1051:        }
                   1052:        if (stat & BUSY_STAT) {         /* other bits are useless when BUSY */
                   1053:                rq->errors |= ERROR_RESET;
                   1054:        } else {
                   1055:                if (drive->media == ide_disk && (stat & ERR_STAT)) {
                   1056:                        /* err has different meaning on cdrom and tape */
                   1057:                        if (err == ABRT_ERR) {
                   1058:                                if (drive->select.b.lba && IN_BYTE(IDE_COMMAND_REG) == WIN_SPECIFY)
                   1059:                                        return; /* some newer drives don't support WIN_SPECIFY */
                   1060:                        } else if ((err & (ABRT_ERR | ICRC_ERR)) == (ABRT_ERR | ICRC_ERR))
                   1061:                                ; /* UDMA crc error -- just retry the operation */
                   1062:                        else if (err & (BBD_ERR | ECC_ERR))     /* retries won't help these */
                   1063:                                rq->errors = ERROR_MAX;
                   1064:                        else if (err & TRK0_ERR)        /* help it find track zero */
                   1065:                                rq->errors |= ERROR_RECAL;
                   1066:                        else if (err & MC_ERR)
                   1067:                                drive->special.b.mc = 1;
                   1068:                }
                   1069:                if ((stat & DRQ_STAT) && rq->cmd != WRITE)
                   1070:                        try_to_flush_leftover_data(drive);
                   1071:        }
                   1072:        if (GET_STAT() & (BUSY_STAT|DRQ_STAT))
                   1073:                rq->errors |= ERROR_RESET;      /* Mmmm.. timing problem */
                   1074: 
                   1075:        if (rq->errors >= ERROR_MAX) {
                   1076: #ifdef CONFIG_BLK_DEV_IDETAPE
                   1077:                if (drive->media == ide_tape) {
                   1078:                        rq->errors = 0;
                   1079:                        idetape_end_request(0, HWGROUP(drive));
                   1080:                } else
                   1081: #endif /* CONFIG_BLK_DEV_IDETAPE */
                   1082: #ifdef CONFIG_BLK_DEV_IDEFLOPPY
                   1083:                if (drive->media == ide_floppy) {
                   1084:                        rq->errors = 0;
                   1085:                        idefloppy_end_request(0, HWGROUP(drive));
                   1086:                } else
                   1087: #endif /* CONFIG_BLK_DEV_IDEFLOPPY */
                   1088: #ifdef CONFIG_BLK_DEV_IDESCSI
                   1089:                if (drive->media == ide_scsi) {
                   1090:                        rq->errors = 0;
                   1091:                        idescsi_end_request(0, HWGROUP(drive));
                   1092:                } else
                   1093: #endif /* CONFIG_BLK_DEV_IDESCSI */
                   1094:                ide_end_request(0, HWGROUP(drive));
                   1095:        }
                   1096:        else {
                   1097:                if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
                   1098:                        ++rq->errors;
                   1099:                        ide_do_reset(drive);
                   1100:                        return;
                   1101:                } else if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
                   1102:                        drive->special.b.recalibrate = 1;
                   1103:                ++rq->errors;
                   1104:        }
                   1105: }
                   1106: 
                   1107: /*
                   1108:  * read_intr() is the handler for disk read/multread interrupts
                   1109:  */
                   1110: static void read_intr (ide_drive_t *drive)
                   1111: {
                   1112:        byte stat;
                   1113:        int i;
                   1114:        unsigned int msect, nsect;
                   1115:        struct request *rq;
                   1116: 
                   1117:        if (!OK_STAT(stat=GET_STAT(),DATA_READY,BAD_R_STAT)) {
                   1118:                ide_error(drive, "read_intr", stat);
                   1119:                return;
                   1120:        }
                   1121:        msect = drive->mult_count;
                   1122: read_next:
                   1123:        rq = HWGROUP(drive)->rq;
                   1124:        if (msect) {
                   1125:                if ((nsect = rq->current_nr_sectors) > msect)
                   1126:                        nsect = msect;
                   1127:                msect -= nsect;
                   1128:        } else
                   1129:                nsect = 1;
                   1130:        ide_input_data(drive, rq->buffer, nsect * SECTOR_WORDS);
                   1131: #ifdef DEBUG
                   1132:        printk("%s:  read: sectors(%ld-%ld), buffer=0x%08lx, remaining=%ld\n",
                   1133:                drive->name, rq->sector, rq->sector+nsect-1,
                   1134:                (unsigned long) rq->buffer+(nsect<<9), rq->nr_sectors-nsect);
                   1135: #endif
                   1136:        rq->sector += nsect;
                   1137:        rq->buffer += nsect<<9;
                   1138:        rq->errors = 0;
                   1139:        i = (rq->nr_sectors -= nsect);
                   1140:        if ((rq->current_nr_sectors -= nsect) <= 0)
                   1141:                ide_end_request(1, HWGROUP(drive));
                   1142:        if (i > 0) {
                   1143:                if (msect)
                   1144:                        goto read_next;
                   1145:                ide_set_handler (drive, &read_intr, WAIT_CMD);
                   1146:        }
                   1147: }
                   1148: 
                   1149: /*
                   1150:  * write_intr() is the handler for disk write interrupts
                   1151:  */
                   1152: static void write_intr (ide_drive_t *drive)
                   1153: {
                   1154:        byte stat;
                   1155:        int i;
                   1156:        ide_hwgroup_t *hwgroup = HWGROUP(drive);
                   1157:        struct request *rq = hwgroup->rq;
                   1158: 
                   1159:        if (OK_STAT(stat=GET_STAT(),DRIVE_READY,drive->bad_wstat)) {
                   1160: #ifdef DEBUG
                   1161:                printk("%s: write: sector %ld, buffer=0x%08lx, remaining=%ld\n",
                   1162:                        drive->name, rq->sector, (unsigned long) rq->buffer,
                   1163:                        rq->nr_sectors-1);
                   1164: #endif
                   1165:                if ((rq->nr_sectors == 1) ^ ((stat & DRQ_STAT) != 0)) {
                   1166:                        rq->sector++;
                   1167:                        rq->buffer += 512;
                   1168:                        rq->errors = 0;
                   1169:                        i = --rq->nr_sectors;
                   1170:                        --rq->current_nr_sectors;
                   1171:                        if (rq->current_nr_sectors <= 0)
                   1172:                                ide_end_request(1, hwgroup);
                   1173:                        if (i > 0) {
                   1174:                                ide_output_data (drive, rq->buffer, SECTOR_WORDS);
                   1175:                                ide_set_handler (drive, &write_intr, WAIT_CMD);
                   1176:                        }
                   1177:                        return;
                   1178:                }
                   1179:        }
                   1180:        ide_error(drive, "write_intr", stat);
                   1181: }
                   1182: 
                   1183: /*
                   1184:  * ide_multwrite() transfers a block of up to mcount sectors of data
                   1185:  * to a drive as part of a disk multiple-sector write operation.
                   1186:  */
                   1187: void ide_multwrite (ide_drive_t *drive, unsigned int mcount)
                   1188: {
                   1189:        struct request *rq = &HWGROUP(drive)->wrq;
                   1190: 
                   1191:        do {
                   1192:                unsigned int nsect = rq->current_nr_sectors;
                   1193:                if (nsect > mcount)
                   1194:                        nsect = mcount;
                   1195:                mcount -= nsect;
                   1196: 
                   1197:                ide_output_data(drive, rq->buffer, nsect<<7);
                   1198: #ifdef DEBUG
                   1199:                printk("%s: multwrite: sector %ld, buffer=0x%08lx, count=%d, remaining=%ld\n",
                   1200:                        drive->name, rq->sector, (unsigned long) rq->buffer,
                   1201:                        nsect, rq->nr_sectors - nsect);
                   1202: #endif
                   1203:                if ((rq->nr_sectors -= nsect) <= 0)
                   1204:                        break;
                   1205:                if ((rq->current_nr_sectors -= nsect) == 0) {
                   1206:                        if ((rq->bh = rq->bh->b_reqnext) != NULL) {
                   1207:                                rq->current_nr_sectors = rq->bh->b_size>>9;
                   1208:                                rq->buffer             = rq->bh->b_data;
                   1209:                        } else {
                   1210:                                panic("%s: buffer list corrupted\n", drive->name);
                   1211:                                break;
                   1212:                        }
                   1213:                } else {
                   1214:                        rq->buffer += nsect << 9;
                   1215:                }
                   1216:        } while (mcount);
                   1217: }
                   1218: 
                   1219: /*
                   1220:  * multwrite_intr() is the handler for disk multwrite interrupts
                   1221:  */
                   1222: static void multwrite_intr (ide_drive_t *drive)
                   1223: {
                   1224:        byte stat;
                   1225:        int i;
                   1226:        ide_hwgroup_t *hwgroup = HWGROUP(drive);
                   1227:        struct request *rq = &hwgroup->wrq;
                   1228: 
                   1229:        if (OK_STAT(stat=GET_STAT(),DRIVE_READY,drive->bad_wstat)) {
                   1230:                if (stat & DRQ_STAT) {
                   1231:                        if (rq->nr_sectors) {
                   1232:                                ide_multwrite(drive, drive->mult_count);
                   1233:                                ide_set_handler (drive, &multwrite_intr, WAIT_CMD);
                   1234:                                return;
                   1235:                        }
                   1236:                } else {
                   1237:                        if (!rq->nr_sectors) {  /* all done? */
                   1238:                                rq = hwgroup->rq;
                   1239:                                for (i = rq->nr_sectors; i > 0;){
                   1240:                                        i -= rq->current_nr_sectors;
                   1241:                                        ide_end_request(1, hwgroup);
                   1242:                                }
                   1243:                                return;
                   1244:                        }
                   1245:                }
                   1246:        }
                   1247:        ide_error(drive, "multwrite_intr", stat);
                   1248: }
                   1249: 
                   1250: /*
                   1251:  * Issue a simple drive command
                   1252:  * The drive must be selected beforehand.
                   1253:  */
                   1254: static void ide_cmd(ide_drive_t *drive, byte cmd, byte nsect, ide_handler_t *handler)
                   1255: {
                   1256:        ide_set_handler (drive, handler, WAIT_CMD);
                   1257:        OUT_BYTE(drive->ctl,IDE_CONTROL_REG);
                   1258:        OUT_BYTE(nsect,IDE_NSECTOR_REG);
                   1259:        OUT_BYTE(cmd,IDE_COMMAND_REG);
                   1260: }
                   1261: 
                   1262: /*
                   1263:  * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
                   1264:  */
                   1265: static void set_multmode_intr (ide_drive_t *drive)
                   1266: {
                   1267:        byte stat = GET_STAT();
                   1268: 
                   1269:        sti();
                   1270:        if (OK_STAT(stat,READY_STAT,BAD_STAT)) {
                   1271:                drive->mult_count = drive->mult_req;
                   1272:        } else {
                   1273:                drive->mult_req = drive->mult_count = 0;
                   1274:                drive->special.b.recalibrate = 1;
                   1275:                (void) ide_dump_status(drive, "set_multmode", stat);
                   1276:        }
                   1277: }
                   1278: 
                   1279: /*
                   1280:  * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
                   1281:  */
                   1282: static void set_geometry_intr (ide_drive_t *drive)
                   1283: {
                   1284:        byte stat = GET_STAT();
                   1285: 
                   1286:        sti();
                   1287:        if (!OK_STAT(stat,READY_STAT,BAD_STAT))
                   1288:                ide_error(drive, "set_geometry_intr", stat);
                   1289: }
                   1290: 
                   1291: /*
                   1292:  * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
                   1293:  */
                   1294: static void recal_intr (ide_drive_t *drive)
                   1295: {
                   1296:        byte stat = GET_STAT();
                   1297: 
                   1298:        sti();
                   1299:        if (!OK_STAT(stat,READY_STAT,BAD_STAT))
                   1300:                ide_error(drive, "recal_intr", stat);
                   1301: }
                   1302: 
                   1303: /*
                   1304:  * mc_intr() is invoked on completion of a WIN_ACKMC cmd.
                   1305:  */
                   1306: static void mc_intr (ide_drive_t *drive)
                   1307: {
                   1308:        byte stat = GET_STAT();
                   1309: 
                   1310:        sti();
                   1311:        if (!OK_STAT(stat,READY_STAT,BAD_STAT))
                   1312:                ide_error(drive, "mc_intr", stat);
                   1313:        drive->special.b.mc = 0;
                   1314: }
                   1315: 
                   1316: /*
                   1317:  * drive_cmd_intr() is invoked on completion of a special DRIVE_CMD.
                   1318:  */
                   1319: static void drive_cmd_intr (ide_drive_t *drive)
                   1320: {
                   1321:        struct request *rq = HWGROUP(drive)->rq;
                   1322:        byte *args = (byte *) rq->buffer;
                   1323:        byte stat = GET_STAT();
                   1324: 
                   1325:        sti();
                   1326:        if ((stat & DRQ_STAT) && args && args[3]) {
                   1327:                byte io_32bit = drive->io_32bit;
                   1328:                drive->io_32bit = 0;
                   1329:                ide_input_data(drive, &args[4], args[3] * SECTOR_WORDS);
                   1330:                drive->io_32bit = io_32bit;
                   1331:                stat = GET_STAT();
                   1332:        }
                   1333:        if (OK_STAT(stat,READY_STAT,BAD_STAT))
                   1334:                ide_end_drive_cmd (drive, stat, GET_ERR());
                   1335:        else
                   1336:                ide_error(drive, "drive_cmd", stat); /* calls ide_end_drive_cmd */
                   1337: }
                   1338: 
                   1339: /*
                   1340:  * do_special() is used to issue WIN_SPECIFY, WIN_RESTORE, and WIN_SETMULT
                   1341:  * commands to a drive.  It used to do much more, but has been scaled back.
                   1342:  */
                   1343: static inline void do_special (ide_drive_t *drive)
                   1344: {
                   1345:        special_t *s = &drive->special;
                   1346: 
                   1347: #ifdef DEBUG
                   1348:        printk("%s: do_special: 0x%02x\n", drive->name, s->all);
                   1349: #endif
                   1350:        if (s->b.set_geometry) {
                   1351:                s->b.set_geometry = 0;
                   1352:                if (drive->media == ide_disk && !drive->no_geom) {
                   1353:                        OUT_BYTE(drive->sect,IDE_SECTOR_REG);
                   1354:                        OUT_BYTE(drive->cyl,IDE_LCYL_REG);
                   1355:                        OUT_BYTE(drive->cyl>>8,IDE_HCYL_REG);
                   1356:                        OUT_BYTE(((drive->head-1)|drive->select.all)&0xBF,IDE_SELECT_REG);
                   1357:                        if (!IS_PROMISE_DRIVE)
                   1358:                                ide_cmd(drive, WIN_SPECIFY, drive->sect, &set_geometry_intr);
                   1359:                }
                   1360:        } else if (s->b.recalibrate) {
                   1361:                s->b.recalibrate = 0;
                   1362:                if (drive->media == ide_disk && !IS_PROMISE_DRIVE)
                   1363:                        ide_cmd(drive, WIN_RESTORE, drive->sect, &recal_intr);
                   1364:        } else if (s->b.set_tune) {
                   1365:                ide_tuneproc_t *tuneproc = HWIF(drive)->tuneproc;
                   1366:                s->b.set_tune = 0;
                   1367:                if (tuneproc != NULL)
                   1368:                        tuneproc(drive, drive->tune_req);
                   1369:        } else if (s->b.set_multmode) {
                   1370:                s->b.set_multmode = 0;
                   1371:                if (drive->media == ide_disk) {
                   1372:                        if (drive->id && drive->mult_req > drive->id->max_multsect)
                   1373:                                drive->mult_req = drive->id->max_multsect;
                   1374:                        if (!IS_PROMISE_DRIVE)
                   1375:                                ide_cmd(drive, WIN_SETMULT, drive->mult_req, &set_multmode_intr);
                   1376:                } else
                   1377:                        drive->mult_req = 0;
                   1378:        } else if (s->b.mc) {
                   1379:                s->b.mc = 0;
                   1380:                if (drive->media == ide_disk && !IS_PROMISE_DRIVE)
                   1381:                        ide_cmd(drive, WIN_ACKMC, drive->sect, &mc_intr);
                   1382:        } else if (s->all) {
                   1383:                int special = s->all;
                   1384:                s->all = 0;
                   1385:                printk("%s: bad special flag: 0x%02x\n", drive->name, special);
                   1386:        }
                   1387: }
                   1388: 
                   1389: /*
                   1390:  * This routine busy-waits for the drive status to be not "busy".
                   1391:  * It then checks the status for all of the "good" bits and none
                   1392:  * of the "bad" bits, and if all is okay it returns 0.  All other
                   1393:  * cases return 1 after invoking ide_error() -- caller should just return.
                   1394:  *
                   1395:  * This routine should get fixed to not hog the cpu during extra long waits..
                   1396:  * That could be done by busy-waiting for the first jiffy or two, and then
                   1397:  * setting a timer to wake up at half second intervals thereafter,
                   1398:  * until timeout is achieved, before timing out.
                   1399:  */
                   1400: int ide_wait_stat (ide_drive_t *drive, byte good, byte bad, unsigned long timeout)
                   1401: {
                   1402:        byte stat;
                   1403:        unsigned long flags;
                   1404: 
                   1405:        udelay(1);      /* spec allows drive 400ns to assert "BUSY" */
                   1406:        if ((stat = GET_STAT()) & BUSY_STAT) {
                   1407:                save_flags(flags);
                   1408:                sti();
                   1409:                timeout += jiffies;
                   1410:                while ((stat = GET_STAT()) & BUSY_STAT) {
                   1411:                        if (jiffies > timeout) {
                   1412:                                restore_flags(flags);
                   1413:                                ide_error(drive, "status timeout", stat);
                   1414:                                return 1;
                   1415:                        }
                   1416:                }
                   1417:                restore_flags(flags);
                   1418:        }
                   1419:        udelay(1);      /* allow status to settle, then read it again */
                   1420:        if (OK_STAT((stat = GET_STAT()), good, bad))
                   1421:                return 0;
                   1422:        ide_error(drive, "status error", stat);
                   1423:        return 1;
                   1424: }
                   1425: 
                   1426: /*
                   1427:  * do_rw_disk() issues READ and WRITE commands to a disk,
                   1428:  * using LBA if supported, or CHS otherwise, to address sectors.
                   1429:  * It also takes care of issuing special DRIVE_CMDs.
                   1430:  */
                   1431: static inline void do_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
                   1432: {
                   1433:        ide_hwif_t *hwif = HWIF(drive);
                   1434:        unsigned short io_base = hwif->io_base;
                   1435: #ifdef CONFIG_BLK_DEV_PROMISE
                   1436:        int use_promise_io = 0;
                   1437: #endif /* CONFIG_BLK_DEV_PROMISE */
                   1438: 
                   1439:        OUT_BYTE(drive->ctl,IDE_CONTROL_REG);
                   1440:        OUT_BYTE(rq->nr_sectors,io_base+IDE_NSECTOR_OFFSET);
                   1441: #ifdef CONFIG_BLK_DEV_PROMISE
                   1442:        if (IS_PROMISE_DRIVE) {
                   1443:                if (hwif->is_promise2 || rq->cmd == READ) {
                   1444:                        use_promise_io = 1;
                   1445:                }
                   1446:        }
                   1447:        if (drive->select.b.lba || use_promise_io) {
                   1448: #else /* !CONFIG_BLK_DEV_PROMISE */
                   1449:        if (drive->select.b.lba) {
                   1450: #endif /* CONFIG_BLK_DEV_PROMISE */
                   1451: #ifdef DEBUG
                   1452:                printk("%s: %sing: LBAsect=%ld, sectors=%ld, buffer=0x%08lx\n",
                   1453:                        drive->name, (rq->cmd==READ)?"read":"writ",
                   1454:                        block, rq->nr_sectors, (unsigned long) rq->buffer);
                   1455: #endif
                   1456:                OUT_BYTE(block,io_base+IDE_SECTOR_OFFSET);
                   1457:                OUT_BYTE(block>>=8,io_base+IDE_LCYL_OFFSET);
                   1458:                OUT_BYTE(block>>=8,io_base+IDE_HCYL_OFFSET);
                   1459:                OUT_BYTE(((block>>8)&0x0f)|drive->select.all,io_base+IDE_SELECT_OFFSET);
                   1460:        } else {
                   1461:                unsigned int sect,head,cyl,track;
                   1462:                track = block / drive->sect;
                   1463:                sect  = block % drive->sect + 1;
                   1464:                OUT_BYTE(sect,io_base+IDE_SECTOR_OFFSET);
                   1465:                head  = track % drive->head;
                   1466:                cyl   = track / drive->head;
                   1467:                OUT_BYTE(cyl,io_base+IDE_LCYL_OFFSET);
                   1468:                OUT_BYTE(cyl>>8,io_base+IDE_HCYL_OFFSET);
                   1469:                OUT_BYTE(head|drive->select.all,io_base+IDE_SELECT_OFFSET);
                   1470: #ifdef DEBUG
                   1471:                printk("%s: %sing: CHS=%d/%d/%d, sectors=%ld, buffer=0x%08lx\n",
                   1472:                        drive->name, (rq->cmd==READ)?"read":"writ", cyl,
                   1473:                        head, sect, rq->nr_sectors, (unsigned long) rq->buffer);
                   1474: #endif
                   1475:        }
                   1476: #ifdef CONFIG_BLK_DEV_PROMISE
                   1477:        if (use_promise_io) {
                   1478:                do_promise_io (drive, rq);
                   1479:                return;
                   1480:        }
                   1481: #endif /* CONFIG_BLK_DEV_PROMISE */
                   1482:        if (rq->cmd == READ) {
                   1483: #ifdef CONFIG_BLK_DEV_TRITON
                   1484:                if (drive->using_dma && !(HWIF(drive)->dmaproc(ide_dma_read, drive)))
                   1485:                        return;
                   1486: #endif /* CONFIG_BLK_DEV_TRITON */
                   1487:                ide_set_handler(drive, &read_intr, WAIT_CMD);
                   1488:                OUT_BYTE(drive->mult_count ? WIN_MULTREAD : WIN_READ, io_base+IDE_COMMAND_OFFSET);
                   1489:                return;
                   1490:        }
                   1491:        if (rq->cmd == WRITE) {
                   1492: #ifdef CONFIG_BLK_DEV_TRITON
                   1493:                if (drive->using_dma && !(HWIF(drive)->dmaproc(ide_dma_write, drive)))
                   1494:                        return;
                   1495: #endif /* CONFIG_BLK_DEV_TRITON */
                   1496:                OUT_BYTE(drive->mult_count ? WIN_MULTWRITE : WIN_WRITE, io_base+IDE_COMMAND_OFFSET);
                   1497:                if (ide_wait_stat(drive, DATA_READY, drive->bad_wstat, WAIT_DRQ)) {
                   1498:                        printk("%s: no DRQ after issuing %s\n", drive->name,
                   1499:                                drive->mult_count ? "MULTWRITE" : "WRITE");
                   1500:                        return;
                   1501:                }
                   1502:                if (!drive->unmask)
                   1503:                        cli();
                   1504:                if (drive->mult_count) {
                   1505:                        HWGROUP(drive)->wrq = *rq; /* scratchpad */
                   1506:                        ide_set_handler (drive, &multwrite_intr, WAIT_CMD);
                   1507:                        ide_multwrite(drive, drive->mult_count);
                   1508:                } else {
                   1509:                        ide_set_handler (drive, &write_intr, WAIT_CMD);
                   1510:                        ide_output_data(drive, rq->buffer, SECTOR_WORDS);
                   1511:                }
                   1512:                return;
                   1513:        }
                   1514:        printk("%s: bad command: %d\n", drive->name, rq->cmd);
                   1515:        ide_end_request(0, HWGROUP(drive));
                   1516: }
                   1517: 
                   1518: /*
                   1519:  * execute_drive_cmd() issues a special drive command,
                   1520:  * usually initiated by ioctl() from the external hdparm program.
                   1521:  */
                   1522: static void execute_drive_cmd (ide_drive_t *drive, struct request *rq)
                   1523: {
                   1524:        byte *args = rq->buffer;
                   1525:        if (args) {
                   1526: #ifdef DEBUG
                   1527:                printk("%s: DRIVE_CMD cmd=0x%02x sc=0x%02x fr=0x%02x xx=0x%02x\n",
                   1528:                 drive->name, args[0], args[1], args[2], args[3]);
                   1529: #endif
                   1530:                OUT_BYTE(args[2],IDE_FEATURE_REG);
                   1531:                ide_cmd(drive, args[0], args[1], &drive_cmd_intr);
                   1532:                return;
                   1533:        } else {
                   1534:                /*
                   1535:                 * NULL is actually a valid way of waiting for
                   1536:                 * all current requests to be flushed from the queue.
                   1537:                 */
                   1538: #ifdef DEBUG
                   1539:                printk("%s: DRIVE_CMD (null)\n", drive->name);
                   1540: #endif
                   1541:                ide_end_drive_cmd(drive, GET_STAT(), GET_ERR());
                   1542:                return;
                   1543:        }
                   1544: }
                   1545: 
                   1546: /*
                   1547:  * do_request() initiates handling of a new I/O request
                   1548:  */
                   1549: static inline void do_request (ide_hwif_t *hwif, struct request *rq)
                   1550: {
                   1551:        unsigned int minor, unit;
                   1552:        unsigned long block, blockend;
                   1553:        ide_drive_t *drive;
                   1554: 
                   1555:        sti();
                   1556: #ifdef DEBUG
                   1557:        printk("%s: do_request: current=0x%08lx\n", hwif->name, (unsigned long) rq);
                   1558: #endif
                   1559:        minor = MINOR(rq->rq_dev);
                   1560:        unit = minor >> PARTN_BITS;
                   1561:        if (MAJOR(rq->rq_dev) != hwif->major || unit >= MAX_DRIVES) {
                   1562:                printk("%s: bad device number: %s\n",
                   1563:                       hwif->name, kdevname(rq->rq_dev));
                   1564:                goto kill_rq;
                   1565:        }
                   1566:        drive = &hwif->drives[unit];
                   1567: #ifdef DEBUG
                   1568:        if (rq->bh && !buffer_locked(rq->bh)) {
                   1569:                printk("%s: block not locked\n", drive->name);
                   1570:                goto kill_rq;
                   1571:        }
                   1572: #endif
                   1573:        block    = rq->sector;
                   1574:        blockend = block + rq->nr_sectors;
                   1575:        if ((blockend < block) || (blockend > drive->part[minor&PARTN_MASK].nr_sects)) {
                   1576:                printk("%s%c: bad access: block=%ld, count=%ld\n", drive->name,
                   1577:                 (minor&PARTN_MASK)?'0'+(minor&PARTN_MASK):' ', block, rq->nr_sectors);
                   1578:                goto kill_rq;
                   1579:        }
                   1580:        block += drive->part[minor&PARTN_MASK].start_sect + drive->sect0;
                   1581: #if FAKE_FDISK_FOR_EZDRIVE
                   1582:        if (block == 0 && drive->remap_0_to_1)
                   1583:                block = 1;  /* redirect MBR access to EZ-Drive partn table */
                   1584: #endif /* FAKE_FDISK_FOR_EZDRIVE */
                   1585:        ((ide_hwgroup_t *)hwif->hwgroup)->drive = drive;
                   1586: #if (DISK_RECOVERY_TIME > 0)
                   1587:        while ((read_timer() - hwif->last_time) < DISK_RECOVERY_TIME);
                   1588: #endif
                   1589: 
                   1590: #ifdef CONFIG_BLK_DEV_IDETAPE
                   1591:        POLL_HWIF_TAPE_DRIVE;   /* macro from ide-tape.h */
                   1592: #endif /* CONFIG_BLK_DEV_IDETAPE */
                   1593: 
                   1594:        SELECT_DRIVE(hwif,drive);
                   1595:        if (ide_wait_stat(drive, drive->ready_stat, BUSY_STAT|DRQ_STAT, WAIT_READY)) {
                   1596:                printk("%s: drive not ready for command\n", drive->name);
                   1597:                return;
                   1598:        }
                   1599:        
                   1600:        if (!drive->special.all) {
                   1601:                if (rq->cmd == IDE_DRIVE_CMD) {
                   1602:                        execute_drive_cmd(drive, rq);
                   1603:                        return;
                   1604:                }
                   1605: #ifdef CONFIG_BLK_DEV_IDEATAPI
                   1606:                switch (drive->media) {
                   1607:                        case ide_disk:
                   1608:                                do_rw_disk (drive, rq, block);
                   1609:                                return;
                   1610: #ifdef CONFIG_BLK_DEV_IDECD
                   1611:                        case ide_cdrom:
                   1612:                                ide_do_rw_cdrom (drive, block);
                   1613:                                return;
                   1614: #endif /* CONFIG_BLK_DEV_IDECD */
                   1615: #ifdef CONFIG_BLK_DEV_IDETAPE
                   1616:                        case ide_tape:
                   1617:                                idetape_do_request (drive, rq, block);
                   1618:                                return;
                   1619: #endif /* CONFIG_BLK_DEV_IDETAPE */
                   1620: #ifdef CONFIG_BLK_DEV_IDEFLOPPY
                   1621:                        case ide_floppy:
                   1622:                                idefloppy_do_request (drive, rq, block);
                   1623:                                return;
                   1624: #endif /* CONFIG_BLK_DEV_IDEFLOPPY */
                   1625: #ifdef CONFIG_BLK_DEV_IDESCSI
                   1626:                        case ide_scsi:
                   1627:                                idescsi_do_request (drive, rq, block);
                   1628:                                return;
                   1629: #endif /* CONFIG_BLK_DEV_IDESCSI */
                   1630: 
                   1631:                        default:
                   1632:                                printk("%s: media type %d not supported\n",
                   1633:                                        drive->name, drive->media);
                   1634:                                goto kill_rq;
                   1635:                }
                   1636: #else
                   1637:                do_rw_disk (drive, rq, block); /* simpler and faster */
                   1638:                return;
                   1639: #endif /* CONFIG_BLK_DEV_IDEATAPI */;
                   1640:        }
                   1641:        do_special(drive);
                   1642:        return;
                   1643: kill_rq:
                   1644:        ide_end_request(0, hwif->hwgroup);
                   1645: }
                   1646: 
                   1647: /*
                   1648:  * The driver enables interrupts as much as possible.  In order to do this,
                   1649:  * (a) the device-interrupt is always masked before entry, and
                   1650:  * (b) the timeout-interrupt is always disabled before entry.
                   1651:  *
                   1652:  * If we enter here from, say irq14, and then start a new request for irq15,
                   1653:  * (possible with "serialize" option) then we cannot ensure that we exit
                   1654:  * before the irq15 hits us. So, we must be careful not to let this bother us.
                   1655:  *
                   1656:  * Interrupts are still masked (by default) whenever we are exchanging
                   1657:  * data/cmds with a drive, because some drives seem to have very poor
                   1658:  * tolerance for latency during I/O.  For devices which don't suffer from
                   1659:  * this problem (most don't), the unmask flag can be set using the "hdparm"
                   1660:  * utility, to permit other interrupts during data/cmd transfers.
                   1661:  */
                   1662: void ide_do_request (ide_hwgroup_t *hwgroup)
                   1663: {
                   1664:        cli();  /* paranoia */
                   1665:        if (hwgroup->handler != NULL) {
                   1666:                printk("%s: EEeekk!! handler not NULL in ide_do_request()\n", hwgroup->hwif->name);
                   1667:                return;
                   1668:        }
                   1669:        do {
                   1670:                ide_hwif_t *hwif = hwgroup->hwif;
                   1671:                struct request *rq;
                   1672:                if ((rq = hwgroup->rq) == NULL) {
                   1673:                        if (hwif->sharing_irq && hwgroup->drive) /* set nIEN */
                   1674:                                OUT_BYTE(hwgroup->drive->ctl|2,hwif->ctl_port);
                   1675:                        /*
                   1676:                         * hwgroup->next_hwif is different from hwgroup->hwif
                   1677:                         * only when a request is inserted using "ide_next".
                   1678:                         * This saves wear and tear on IDE tapes.
                   1679:                         */
                   1680:                        hwif = hwgroup->next_hwif;
                   1681:                        do {
                   1682:                                rq = blk_dev[hwif->major].current_request;
                   1683:                                if (rq != NULL && rq->rq_status != RQ_INACTIVE)
                   1684:                                        goto got_rq;
                   1685:                        } while ((hwif = hwif->next) != hwgroup->next_hwif);
                   1686:                        hwgroup->active = 0;
                   1687:                        return;         /* no work left for this hwgroup */
                   1688:                }
                   1689:        got_rq: 
                   1690:                do_request(hwgroup->hwif = hwgroup->next_hwif = hwif, hwgroup->rq = rq);
                   1691:                cli();
                   1692:        } while (hwgroup->handler == NULL);
                   1693: }
                   1694: 
                   1695: /*
                   1696:  * do_hwgroup_request() invokes ide_do_request() after first masking
                   1697:  * all possible interrupts for the current hwgroup.  This prevents race
                   1698:  * conditions in the event that an unexpected interrupt occurs while
                   1699:  * we are in the driver.
                   1700:  *
                   1701:  * Note that when an interrupt is used to reenter the driver, the first level
                   1702:  * handler will already have masked the irq that triggered, but any other ones
                   1703:  * for the hwgroup will still be unmasked.  The driver tries to be careful
                   1704:  * about such things.
                   1705:  */
                   1706: static void do_hwgroup_request (ide_hwgroup_t *hwgroup)
                   1707: {
                   1708:        if (hwgroup->handler == NULL) {
                   1709:                ide_hwif_t *hgif = hwgroup->hwif;
                   1710:                ide_hwif_t *hwif = hgif;
                   1711:                hwgroup->active = 1;
                   1712:                do {
                   1713:                        disable_irq(hwif->irq);
                   1714:                } while ((hwif = hwif->next) != hgif);
                   1715:                ide_do_request (hwgroup);
                   1716:                do {
                   1717:                        enable_irq(hwif->irq);
                   1718:                } while ((hwif = hwif->next) != hgif);
                   1719:        }
                   1720: }
                   1721: 
                   1722: static void do_ide0_request (void)     /* invoked with cli() */
                   1723: {
                   1724:        do_hwgroup_request (ide_hwifs[0].hwgroup);
                   1725: }
                   1726: 
                   1727: #if MAX_HWIFS > 1
                   1728: static void do_ide1_request (void)     /* invoked with cli() */
                   1729: {
                   1730:        do_hwgroup_request (ide_hwifs[1].hwgroup);
                   1731: }
                   1732: #endif
                   1733: 
                   1734: #if MAX_HWIFS > 2
                   1735: static void do_ide2_request (void)     /* invoked with cli() */
                   1736: {
                   1737:        do_hwgroup_request (ide_hwifs[2].hwgroup);
                   1738: }
                   1739: #endif
                   1740: 
                   1741: #if MAX_HWIFS > 3
                   1742: static void do_ide3_request (void)     /* invoked with cli() */
                   1743: {
                   1744:        do_hwgroup_request (ide_hwifs[3].hwgroup);
                   1745: }
                   1746: #endif
                   1747: 
                   1748: static void timer_expiry (unsigned long data)
                   1749: {
                   1750:        ide_hwgroup_t *hwgroup = (ide_hwgroup_t *) data;
                   1751:        ide_drive_t   *drive   = hwgroup->drive;
                   1752:        unsigned long flags;
                   1753: 
                   1754:        save_flags(flags);
                   1755:        cli();
                   1756: 
                   1757:        if (hwgroup->poll_timeout != 0) { /* polling in progress? */
                   1758:                ide_handler_t *handler = hwgroup->handler;
                   1759:                hwgroup->handler = NULL;
                   1760:                handler(drive);
                   1761:        } else if (hwgroup->handler == NULL) {   /* not waiting for anything? */
                   1762:                sti(); /* drive must have responded just as the timer expired */
                   1763:                printk("%s: marginal timeout\n", drive->name);
                   1764:        } else {
                   1765:                hwgroup->handler = NULL;        /* abort the operation */
                   1766:                if (hwgroup->hwif->dmaproc)
                   1767:                        (void) hwgroup->hwif->dmaproc (ide_dma_abort, drive);
                   1768:                ide_error(drive, "irq timeout", GET_STAT());
                   1769:        }
                   1770:        if (hwgroup->handler == NULL)
                   1771:                do_hwgroup_request (hwgroup);
                   1772:        restore_flags(flags);
                   1773: }
                   1774: 
                   1775: /*
                   1776:  * There's nothing really useful we can do with an unexpected interrupt,
                   1777:  * other than reading the status register (to clear it), and logging it.
                   1778:  * There should be no way that an irq can happen before we're ready for it,
                   1779:  * so we needn't worry much about losing an "important" interrupt here.
                   1780:  *
                   1781:  * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
                   1782:  * drive enters "idle", "standby", or "sleep" mode, so if the status looks
                   1783:  * "good", we just ignore the interrupt completely.
                   1784:  *
                   1785:  * This routine assumes cli() is in effect when called.
                   1786:  *
                   1787:  * If an unexpected interrupt happens on irq15 while we are handling irq14
                   1788:  * and if the two interfaces are "serialized" (CMD640), then it looks like
                   1789:  * we could screw up by interfering with a new request being set up for irq15.
                   1790:  *
                   1791:  * In reality, this is a non-issue.  The new command is not sent unless the
                   1792:  * drive is ready to accept one, in which case we know the drive is not
                   1793:  * trying to interrupt us.  And ide_set_handler() is always invoked before
                   1794:  * completing the issuance of any new drive command, so we will not be
                   1795:  * accidently invoked as a result of any valid command completion interrupt.
                   1796:  *
                   1797:  */
                   1798: static void unexpected_intr (int irq, ide_hwgroup_t *hwgroup)
                   1799: {
                   1800:        byte stat;
                   1801:        unsigned int unit;
                   1802:        ide_hwif_t *hwif = hwgroup->hwif;
                   1803: 
                   1804:        /*
                   1805:         * handle the unexpected interrupt
                   1806:         */
                   1807:        do {
                   1808:                if (hwif->irq == irq) {
                   1809:                        for (unit = 0; unit < MAX_DRIVES; ++unit) {
                   1810:                                ide_drive_t *drive = &hwif->drives[unit];
                   1811:                                if (!drive->present)
                   1812:                                        continue;
                   1813:                                SELECT_DRIVE(hwif,drive);
                   1814:                                udelay(100);  /* Ugly, but wait_stat() may not be safe here */
                   1815:                                if (!OK_STAT(stat=GET_STAT(), drive->ready_stat, BAD_STAT)) {
                   1816:                                        /* Try to not flood the console with msgs */
                   1817:                                        static unsigned long last_msgtime = 0;
                   1818:                                        if ((last_msgtime + (HZ/2)) < jiffies) {
                   1819:                                                last_msgtime = jiffies;
                   1820:                                                (void) ide_dump_status(drive, "unexpected_intr", stat);
                   1821:                                        }
                   1822:                                }
                   1823:                                if ((stat & DRQ_STAT))
                   1824:                                        try_to_flush_leftover_data(drive);
                   1825:                        }
                   1826:                }
                   1827:        } while ((hwif = hwif->next) != hwgroup->hwif);
                   1828:        SELECT_DRIVE(hwif,hwgroup->drive); /* Ugh.. probably interrupts current I/O */
                   1829:        udelay(100);  /* Ugly, but wait_stat() may not be safe here */
                   1830: }
                   1831: 
                   1832: /*
                   1833:  * entry point for all interrupts, caller does cli() for us
                   1834:  */
                   1835: void ide_intr (int irq, void *dev_id, struct pt_regs *regs)
                   1836: {
                   1837:        ide_hwgroup_t *hwgroup = dev_id;
                   1838:        ide_handler_t *handler;
                   1839: 
                   1840:        if (irq == hwgroup->hwif->irq && (handler = hwgroup->handler) != NULL) {
                   1841:                ide_drive_t *drive = hwgroup->drive;
                   1842:                hwgroup->handler = NULL;
                   1843:                del_timer(&(hwgroup->timer));
                   1844:                if (drive->unmask)
                   1845:                        sti();
                   1846:                handler(drive);
                   1847:                cli();  /* this is necessary, as next rq may be different irq */
                   1848:                if (hwgroup->handler == NULL) {
                   1849:                        SET_RECOVERY_TIMER(HWIF(drive));
                   1850:                        ide_do_request(hwgroup);
                   1851:                }
                   1852:        } else {
                   1853:                unexpected_intr(irq, hwgroup);
                   1854:        }
                   1855:        cli();
                   1856: }
                   1857: 
                   1858: /*
                   1859:  * get_info_ptr() returns the (ide_drive_t *) for a given device number.
                   1860:  * It returns NULL if the given device number does not match any present drives.
                   1861:  */
                   1862: static ide_drive_t *get_info_ptr (kdev_t i_rdev)
                   1863: {
                   1864:        int             major = MAJOR(i_rdev);
                   1865:        unsigned int    h;
                   1866: 
                   1867:        for (h = 0; h < MAX_HWIFS; ++h) {
                   1868:                ide_hwif_t  *hwif = &ide_hwifs[h];
                   1869:                if (hwif->present && major == hwif->major) {
                   1870:                        unsigned unit = DEVICE_NR(i_rdev);
                   1871:                        if (unit < MAX_DRIVES) {
                   1872:                                ide_drive_t *drive = &hwif->drives[unit];
                   1873:                                if (drive->present)
                   1874:                                        return drive;
                   1875:                        } else if (major == IDE0_MAJOR && unit < 4) {
                   1876:                                printk("ide: probable bad entry for /dev/hd%c\n", 'a'+unit);
                   1877:                                printk("ide: to fix it, run:  /usr/src/linux/scripts/MAKEDEV.ide\n");
                   1878:                        }
                   1879:                        break;
                   1880:                }
                   1881:        }
                   1882:        return NULL;
                   1883: }
                   1884: 
                   1885: /*
                   1886:  * This function is intended to be used prior to invoking ide_do_drive_cmd().
                   1887:  */
                   1888: void ide_init_drive_cmd (struct request *rq)
                   1889: {
                   1890:        rq->buffer = NULL;
                   1891:        rq->cmd = IDE_DRIVE_CMD;
                   1892:        rq->sector = 0;
                   1893:        rq->nr_sectors = 0;
                   1894:        rq->current_nr_sectors = 0;
                   1895:        rq->sem = NULL;
                   1896:        rq->bh = NULL;
                   1897:        rq->bhtail = NULL;
                   1898:        rq->next = NULL;
                   1899: 
                   1900: #if 0  /* these are done each time through ide_do_drive_cmd() */
                   1901:        rq->errors = 0;
                   1902:        rq->rq_status = RQ_ACTIVE;
                   1903:        rq->rq_dev = ????;
                   1904: #endif
                   1905: }
                   1906: 
                   1907: /*
                   1908:  * This function issues a special IDE device request
                   1909:  * onto the request queue.
                   1910:  *
                   1911:  * If action is ide_wait, then the rq is queued at the end of the
                   1912:  * request queue, and the function sleeps until it has been processed.
                   1913:  * This is for use when invoked from an ioctl handler.
                   1914:  *
                   1915:  * If action is ide_preempt, then the rq is queued at the head of
                   1916:  * the request queue, displacing the currently-being-processed
                   1917:  * request and this function returns immediately without waiting
                   1918:  * for the new rq to be completed.  This is VERY DANGEROUS, and is
                   1919:  * intended for careful use by the ATAPI tape/cdrom driver code.
                   1920:  *
                   1921:  * If action is ide_next, then the rq is queued immediately after
                   1922:  * the currently-being-processed-request (if any), and the function
                   1923:  * returns without waiting for the new rq to be completed.  As above,
                   1924:  * This is VERY DANGEROUS, and is intended for careful use by the
                   1925:  * ATAPI tape/cdrom driver code.
                   1926:  *
                   1927:  * If action is ide_end, then the rq is queued at the end of the
                   1928:  * request queue, and the function returns immediately without waiting
                   1929:  * for the new rq to be completed. This is again intended for careful
                   1930:  * use by the ATAPI tape/cdrom driver code. (Currently used by ide-tape.c,
                   1931:  * when operating in the pipelined operation mode).
                   1932:  */
                   1933: int ide_do_drive_cmd (ide_drive_t *drive, struct request *rq, ide_action_t action)
                   1934: {
                   1935:        unsigned long flags;
                   1936:        unsigned int major = HWIF(drive)->major;
                   1937:        struct request *cur_rq;
                   1938:        struct blk_dev_struct *bdev = &blk_dev[major];
                   1939:        struct semaphore sem = MUTEX_LOCKED;
                   1940: 
                   1941:        if (IS_PROMISE_DRIVE && rq->buffer != NULL)
                   1942:                return -ENOSYS;  /* special drive cmds not supported */
                   1943:        rq->errors = 0;
                   1944:        rq->rq_status = RQ_ACTIVE;
                   1945:        rq->rq_dev = MKDEV(major,(drive->select.b.unit)<<PARTN_BITS);
                   1946:        if (action == ide_wait)
                   1947:                rq->sem = &sem;
                   1948:        unplug_device(bdev);
                   1949: 
                   1950:        save_flags(flags);
                   1951:        cli();
                   1952:        if (action == ide_next)
                   1953:                HWGROUP(drive)->next_hwif = HWIF(drive);
                   1954:        cur_rq = bdev->current_request;
                   1955: 
                   1956:        if (cur_rq == NULL || action == ide_preempt) {
                   1957:                rq->next = cur_rq;
                   1958:                bdev->current_request = rq;
                   1959:                if (action == ide_preempt)
                   1960:                        HWGROUP(drive)->rq = NULL;
                   1961:        } else {
                   1962:                if (action == ide_wait || action == ide_end) {
                   1963:                        while (cur_rq->next != NULL)    /* find end of list */
                   1964:                                cur_rq = cur_rq->next;
                   1965:                }
                   1966:                rq->next = cur_rq->next;
                   1967:                cur_rq->next = rq;
                   1968:        }
                   1969:        if (!HWGROUP(drive)->active) {
                   1970:                do_hwgroup_request(HWGROUP(drive));
                   1971:                cli();
                   1972:        }
                   1973:        if (action == ide_wait  && rq->rq_status != RQ_INACTIVE)
                   1974:                down(&sem);     /* wait for it to be serviced */
                   1975:        restore_flags(flags);
                   1976:        return rq->errors ? -EIO : 0;   /* return -EIO if errors */
                   1977: }
                   1978: 
                   1979: static int ide_open(struct inode * inode, struct file * filp)
                   1980: {
                   1981:        ide_drive_t *drive;
                   1982:        unsigned long flags;
                   1983: 
                   1984:        if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
                   1985:                return -ENXIO;
                   1986:        save_flags(flags);
                   1987:        cli();
                   1988:        while (drive->busy)
                   1989:                sleep_on(&drive->wqueue);
                   1990:        drive->usage++;
                   1991:        restore_flags(flags);
                   1992: #ifdef CONFIG_BLK_DEV_IDECD
                   1993:        if (drive->media == ide_cdrom)
                   1994:                return ide_cdrom_open (inode, filp, drive);
                   1995: #endif /* CONFIG_BLK_DEV_IDECD */
                   1996: #ifdef CONFIG_BLK_DEV_IDETAPE
                   1997:        if (drive->media == ide_tape)
                   1998:                return idetape_blkdev_open (inode, filp, drive);
                   1999: #endif /* CONFIG_BLK_DEV_IDETAPE */
                   2000: #ifdef CONFIG_BLK_DEV_IDEFLOPPY
                   2001:        if (drive->media == ide_floppy)
                   2002:                return idefloppy_open (inode, filp, drive);
                   2003: #endif /* CONFIG_BLK_DEV_IDEFLOPPY */
                   2004: #ifdef CONFIG_BLK_DEV_IDESCSI
                   2005:        if (drive->media == ide_scsi)
                   2006:                return idescsi_open (inode, filp, drive);
                   2007: #endif /* CONFIG_BLK_DEV_IDESCSI */
                   2008:        if (drive->removable && drive->usage == 1) {
                   2009:                byte door_lock[] = {WIN_DOORLOCK,0,0,0};
                   2010:                struct request rq;
                   2011:                check_disk_change(inode->i_rdev);
                   2012:                ide_init_drive_cmd (&rq);
                   2013:                rq.buffer = door_lock;
                   2014:                /*
                   2015:                 * Ignore the return code from door_lock,
                   2016:                 * since the open() has already succeeded,
                   2017:                 * and the door_lock is irrelevant at this point.
                   2018:                 */
                   2019:                (void) ide_do_drive_cmd(drive, &rq, ide_wait);
                   2020:        }
                   2021:        return 0;
                   2022: }
                   2023: 
                   2024: /*
                   2025:  * Releasing a block device means we sync() it, so that it can safely
                   2026:  * be forgotten about...
                   2027:  */
                   2028: static void ide_release(struct inode * inode, struct file * file)
                   2029: {
                   2030:        ide_drive_t *drive;
                   2031: 
                   2032:        if ((drive = get_info_ptr(inode->i_rdev)) != NULL) {
                   2033:                fsync_dev(inode->i_rdev);
                   2034:                drive->usage--;
                   2035: #ifdef CONFIG_BLK_DEV_IDECD
                   2036:                if (drive->media == ide_cdrom) {
                   2037:                        ide_cdrom_release (inode, file, drive);
                   2038:                        return;
                   2039:                }
                   2040: #endif /* CONFIG_BLK_DEV_IDECD */
                   2041: #ifdef CONFIG_BLK_DEV_IDETAPE
                   2042:                if (drive->media == ide_tape) {
                   2043:                        idetape_blkdev_release (inode, file, drive);
                   2044:                        return;
                   2045:                }
                   2046: #endif /* CONFIG_BLK_DEV_IDETAPE */
                   2047: #ifdef CONFIG_BLK_DEV_IDEFLOPPY
                   2048:                if (drive->media == ide_floppy) {
                   2049:                        idefloppy_release (inode, file, drive);
                   2050:                        return;
                   2051:                }
                   2052: #endif /* CONFIG_BLK_DEV_IDEFLOPPY */
                   2053: #ifdef CONFIG_BLK_DEV_IDESCSI
                   2054:                if (drive->media == ide_scsi) {
                   2055:                        idescsi_ide_release (inode, file, drive);
                   2056:                        return;
                   2057:                }
                   2058: #endif /* CONFIG_BLK_DEV_IDESCSI */
                   2059:                if (drive->removable && !drive->usage) {
                   2060:                        byte door_unlock[] = {WIN_DOORUNLOCK,0,0,0};
                   2061:                        struct request rq;
                   2062:                        invalidate_buffers(inode->i_rdev);
                   2063:                        ide_init_drive_cmd (&rq);
                   2064:                        rq.buffer = door_unlock;
                   2065:                        (void) ide_do_drive_cmd(drive, &rq, ide_wait);
                   2066:                }
                   2067:        }
                   2068: }
                   2069: 
                   2070: /*
                   2071:  * This routine is called to flush all partitions and partition tables
                   2072:  * for a changed disk, and then re-read the new partition table.
                   2073:  * If we are revalidating a disk because of a media change, then we
                   2074:  * enter with usage == 0.  If we are using an ioctl, we automatically have
                   2075:  * usage == 1 (we need an open channel to use an ioctl :-), so this
                   2076:  * is our limit.
                   2077:  */
                   2078: static int revalidate_disk(kdev_t i_rdev)
                   2079: {
                   2080:        ide_drive_t *drive;
                   2081:        unsigned int p, major, minor;
                   2082:        long flags;
                   2083: 
                   2084:        if ((drive = get_info_ptr(i_rdev)) == NULL)
                   2085:                return -ENODEV;
                   2086: 
                   2087:        major = MAJOR(i_rdev);
                   2088:        minor = drive->select.b.unit << PARTN_BITS;
                   2089:        save_flags(flags);
                   2090:        cli();
                   2091:        if (drive->busy || (drive->usage > 1)) {
                   2092:                restore_flags(flags);
                   2093:                return -EBUSY;
                   2094:        };
                   2095:        drive->busy = 1;
                   2096:        restore_flags(flags);
                   2097: 
                   2098:        for (p = 0; p < (1<<PARTN_BITS); ++p) {
                   2099:                if (drive->part[p].nr_sects > 0) {
                   2100:                        kdev_t devp = MKDEV(major, minor+p);
                   2101:                        fsync_dev          (devp);
                   2102:                        invalidate_inodes  (devp);
                   2103:                        invalidate_buffers (devp);
                   2104:                        set_blocksize(devp, 1024);
                   2105:                }
                   2106:                drive->part[p].start_sect = 0;
                   2107:                drive->part[p].nr_sects   = 0;
                   2108:        };
                   2109: 
                   2110:        drive->part[0].nr_sects = current_capacity(drive);
                   2111:        if ((drive->media != ide_disk && drive->media != ide_floppy) || !drive->part[0].nr_sects)
                   2112:                drive->part[0].start_sect = -1;
                   2113:        resetup_one_dev(HWIF(drive)->gd, drive->select.b.unit);
                   2114: 
                   2115:        drive->busy = 0;
                   2116:        wake_up(&drive->wqueue);
                   2117:        return 0;
                   2118: }
                   2119: 
                   2120: static int write_fs_long (unsigned long useraddr, long value)
                   2121: {
                   2122:        int err;
                   2123: 
                   2124:        if (NULL == (long *)useraddr)
                   2125:                return -EINVAL;
                   2126:        if ((err = verify_area(VERIFY_WRITE, (long *)useraddr, sizeof(long))))
                   2127:                return err;
                   2128:        put_user((unsigned)value, (long *) useraddr);
                   2129:        return 0;
                   2130: }
                   2131: 
                   2132: static int ide_ioctl (struct inode *inode, struct file *file,
                   2133:                        unsigned int cmd, unsigned long arg)
                   2134: {
                   2135:        int err;
                   2136:        ide_drive_t *drive;
                   2137:        unsigned long flags;
                   2138:        struct request rq;
                   2139: 
                   2140:        if (!inode || !(inode->i_rdev))
                   2141:                return -EINVAL;
                   2142:        if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
                   2143:                return -ENODEV;
                   2144:        ide_init_drive_cmd (&rq);
                   2145:        switch (cmd) {
                   2146:                case HDIO_GETGEO:
                   2147:                {
                   2148:                        struct hd_geometry *loc = (struct hd_geometry *) arg;
                   2149:                        if (!loc || (drive->media != ide_disk && drive->media != ide_floppy)) return -EINVAL;
                   2150:                        err = verify_area(VERIFY_WRITE, loc, sizeof(*loc));
                   2151:                        if (err) return err;
                   2152:                        put_user(drive->bios_head, (byte *) &loc->heads);
                   2153:                        put_user(drive->bios_sect, (byte *) &loc->sectors);
                   2154:                        put_user(drive->bios_cyl, (unsigned short *) &loc->cylinders);
                   2155:                        put_user((unsigned)drive->part[MINOR(inode->i_rdev)&PARTN_MASK].start_sect,
                   2156:                                (unsigned long *) &loc->start);
                   2157:                        return 0;
                   2158:                }
                   2159:                case BLKFLSBUF:
                   2160:                        if (!suser()) return -EACCES;
                   2161:                        fsync_dev(inode->i_rdev);
                   2162:                        invalidate_buffers(inode->i_rdev);
                   2163:                        return 0;
                   2164: 
                   2165:                case BLKRASET:
                   2166:                        if (!suser()) return -EACCES;
                   2167:                        if(arg > 0xff) return -EINVAL;
                   2168:                        read_ahead[MAJOR(inode->i_rdev)] = arg;
                   2169:                        return 0;
                   2170: 
                   2171:                case BLKRAGET:
                   2172:                        return write_fs_long(arg, read_ahead[MAJOR(inode->i_rdev)]);
                   2173: 
                   2174:                case BLKGETSIZE:   /* Return device size */
                   2175:                        return write_fs_long(arg, drive->part[MINOR(inode->i_rdev)&PARTN_MASK].nr_sects);
                   2176:                case BLKRRPART: /* Re-read partition tables */
                   2177:                        if (!suser()) return -EACCES;
                   2178:                        return revalidate_disk(inode->i_rdev);
                   2179: 
                   2180:                case HDIO_GET_KEEPSETTINGS:
                   2181:                        return write_fs_long(arg, drive->keep_settings);
                   2182: 
                   2183:                case HDIO_GET_UNMASKINTR:
                   2184:                        return write_fs_long(arg, drive->unmask);
                   2185: 
                   2186:                case HDIO_GET_DMA:
                   2187:                        return write_fs_long(arg, drive->using_dma);
                   2188: 
                   2189:                case HDIO_GET_32BIT:
                   2190:                        return write_fs_long(arg, drive->io_32bit);
                   2191: 
                   2192:                case HDIO_GET_MULTCOUNT:
                   2193:                        return write_fs_long(arg, drive->mult_count);
                   2194: 
                   2195:                case HDIO_GET_IDENTITY:
                   2196:                        if (!arg || (MINOR(inode->i_rdev) & PARTN_MASK))
                   2197:                                return -EINVAL;
                   2198:                        if (drive->id == NULL)
                   2199:                                return -ENOMSG;
                   2200:                        err = verify_area(VERIFY_WRITE, (char *)arg, sizeof(*drive->id));
                   2201:                        if (!err)
                   2202:                                memcpy_tofs((char *)arg, (char *)drive->id, sizeof(*drive->id));
                   2203:                        return err;
                   2204: 
                   2205:                        case HDIO_GET_NOWERR:
                   2206:                        return write_fs_long(arg, drive->bad_wstat == BAD_R_STAT);
                   2207: 
                   2208:                case HDIO_SET_DMA:
                   2209:                        if (!suser()) return -EACCES;
                   2210: #ifdef CONFIG_BLK_DEV_IDECD
                   2211:                        if (drive->media == ide_cdrom)
                   2212:                                return -EPERM;
                   2213: #endif /* CONFIG_BLK_DEV_IDECD */
                   2214:                        if (!drive->id || !(drive->id->capability & 1) || !HWIF(drive)->dmaproc)
                   2215:                                return -EPERM;
                   2216:                case HDIO_SET_KEEPSETTINGS:
                   2217:                case HDIO_SET_UNMASKINTR:
                   2218:                case HDIO_SET_NOWERR:
                   2219:                        if (arg > 1)
                   2220:                                return -EINVAL;
                   2221:                case HDIO_SET_32BIT:
                   2222:                        if (!suser()) return -EACCES;
                   2223:                        if ((MINOR(inode->i_rdev) & PARTN_MASK))
                   2224:                                return -EINVAL;
                   2225:                        save_flags(flags);
                   2226:                        cli();
                   2227:                        switch (cmd) {
                   2228:                                case HDIO_SET_DMA:
                   2229:                                        if (!(HWIF(drive)->dmaproc)) {
                   2230:                                                restore_flags(flags);
                   2231:                                                return -EPERM;
                   2232:                                        }
                   2233:                                        drive->using_dma = arg;
                   2234:                                        break;
                   2235:                                case HDIO_SET_KEEPSETTINGS:
                   2236:                                        drive->keep_settings = arg;
                   2237:                                        break;
                   2238:                                case HDIO_SET_UNMASKINTR:
                   2239:                                        if (arg && drive->no_unmask) {
                   2240:                                                restore_flags(flags);
                   2241:                                                return -EPERM;
                   2242:                                        }
                   2243:                                        drive->unmask = arg;
                   2244:                                        break;
                   2245:                                case HDIO_SET_NOWERR:
                   2246:                                        drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
                   2247:                                        break;
                   2248:                                case HDIO_SET_32BIT:
                   2249:                                        if (arg > (1 + (SUPPORT_VLB_SYNC<<1))) {
                   2250:                                                restore_flags(flags);
                   2251:                                                return -EINVAL;
                   2252:                                        }
                   2253:                                        if (arg && drive->no_io_32bit) {
                   2254:                                                restore_flags(flags);
                   2255:                                                return -EPERM;
                   2256:                                        }
                   2257:                                        drive->io_32bit = arg;
                   2258: #ifdef CONFIG_BLK_DEV_DTC2278
                   2259:                                        if (HWIF(drive)->chipset == ide_dtc2278)
                   2260:                                                HWIF(drive)->drives[!drive->select.b.unit].io_32bit = arg;
                   2261: #endif /* CONFIG_BLK_DEV_DTC2278 */
                   2262:                                        break;
                   2263:                        }
                   2264:                        restore_flags(flags);
                   2265:                        return 0;
                   2266: 
                   2267:                case HDIO_SET_MULTCOUNT:
                   2268:                        if (!suser()) return -EACCES;
                   2269:                        if (MINOR(inode->i_rdev) & PARTN_MASK)
                   2270:                                return -EINVAL;
                   2271:                        if (drive->id && arg > drive->id->max_multsect)
                   2272:                                return -EINVAL;
                   2273:                        save_flags(flags);
                   2274:                        cli();
                   2275:                        if (drive->special.b.set_multmode) {
                   2276:                                restore_flags(flags);
                   2277:                                return -EBUSY;
                   2278:                        }
                   2279:                        drive->mult_req = arg;
                   2280:                        drive->special.b.set_multmode = 1;
                   2281:                        restore_flags(flags);
                   2282:                        (void) ide_do_drive_cmd (drive, &rq, ide_wait);
                   2283:                        return (drive->mult_count == arg) ? 0 : -EIO;
                   2284: 
                   2285:                case HDIO_DRIVE_CMD:
                   2286:                {
                   2287:                        byte args[4], *argbuf = args;
                   2288:                        int argsize = 4;
                   2289:                        if (!suser() || securelevel > 0) return -EACCES;
                   2290:                        if (NULL == (void *) arg) {
                   2291:                                err = ide_do_drive_cmd(drive, &rq, ide_wait);
                   2292:                        } else if (!(err = verify_area(VERIFY_READ,(void *)arg, 4))) {
                   2293:                                memcpy_fromfs(args, (void *)arg, 4);
                   2294:                                if (args[3]) {
                   2295:                                        argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
                   2296:                                        argbuf = kmalloc(argsize, GFP_KERNEL);
                   2297:                                        if (argbuf == NULL)
                   2298:                                                return -ENOMEM;
                   2299:                                        argbuf[0] = args[0];
                   2300:                                        argbuf[1] = args[1];
                   2301:                                        argbuf[2] = args[2];
                   2302:                                        argbuf[3] = args[3];
                   2303:                                }
                   2304:                                if (!(err = verify_area(VERIFY_WRITE,(void *)arg, argsize))) {
                   2305:                                        rq.buffer = argbuf;
                   2306:                                        err = ide_do_drive_cmd(drive, &rq, ide_wait);
                   2307:                                        memcpy_tofs((void *)arg, argbuf, argsize);
                   2308:                                }
                   2309:                                if (argsize > 4)
                   2310:                                        kfree(argbuf);
                   2311:                        }
                   2312:                        return err;
                   2313:                }
                   2314:                case HDIO_SET_PIO_MODE:
                   2315:                        if (!suser()) return -EACCES;
                   2316:                        if (MINOR(inode->i_rdev) & PARTN_MASK)
                   2317:                                return -EINVAL;
                   2318:                        if (!HWIF(drive)->tuneproc)
                   2319:                                return -ENOSYS;
                   2320:                        save_flags(flags);
                   2321:                        cli();
                   2322:                        if (drive->special.b.set_tune) {
                   2323:                                restore_flags(flags);
                   2324:                                return -EBUSY;
                   2325:                        }
                   2326:                        drive->tune_req = (byte) arg;
                   2327:                        drive->special.b.set_tune = 1;
                   2328:                        restore_flags(flags);
                   2329:                        (void) ide_do_drive_cmd (drive, &rq, ide_wait);
                   2330:                        return 0;
                   2331: 
                   2332:                RO_IOCTLS(inode->i_rdev, arg);
                   2333: 
                   2334:                default:
                   2335: #ifdef CONFIG_BLK_DEV_IDECD
                   2336:                        if (drive->media == ide_cdrom)
                   2337:                                return ide_cdrom_ioctl(drive, inode, file, cmd, arg);
                   2338: #endif /* CONFIG_BLK_DEV_IDECD */
                   2339: #ifdef CONFIG_BLK_DEV_IDETAPE
                   2340:                        if (drive->media == ide_tape)
                   2341:                                return idetape_blkdev_ioctl(drive, inode, file, cmd, arg);
                   2342: #endif /* CONFIG_BLK_DEV_IDETAPE */
                   2343: #ifdef CONFIG_BLK_DEV_IDEFLOPPY
                   2344:                        if (drive->media == ide_floppy)
                   2345:                                return idefloppy_ioctl(drive, inode, file, cmd, arg);
                   2346: #endif /* CONFIG_BLK_DEV_IDEFLOPPY */
                   2347: #ifdef CONFIG_BLK_DEV_IDESCSI
                   2348:                        if (drive->media == ide_scsi)
                   2349:                                return idescsi_ioctl(drive, inode, file, cmd, arg);
                   2350: #endif /* CONFIG_BLK_DEV_IDESCSI */
                   2351:                        return -EPERM;
                   2352:        }
                   2353: }
                   2354: 
                   2355: static int ide_check_media_change (kdev_t i_rdev)
                   2356: {
                   2357:        ide_drive_t *drive;
                   2358: 
                   2359:        if ((drive = get_info_ptr(i_rdev)) == NULL)
                   2360:                return -ENODEV;
                   2361: #ifdef CONFIG_BLK_DEV_IDECD
                   2362:        if (drive->media == ide_cdrom)
                   2363:                return ide_cdrom_check_media_change (drive);
                   2364: #endif /* CONFIG_BLK_DEV_IDECD */
                   2365: #ifdef CONFIG_BLK_DEV_IDEFLOPPY
                   2366:        if (drive->media == ide_floppy)
                   2367:                return idefloppy_media_change (drive);
                   2368: #endif /* CONFIG_BLK_DEV_IDEFLOPPY */
                   2369:        if (drive->removable) /* for disks */
                   2370:                return 1;       /* always assume it was changed */
                   2371:        return 0;
                   2372: }
                   2373: 
                   2374: void ide_fixstring (byte *s, const int bytecount, const int byteswap)
                   2375: {
                   2376:        byte *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */
                   2377: 
                   2378:        if (byteswap) {
                   2379:                /* convert from big-endian to host byte order */
                   2380:                for (p = end ; p != s;) {
                   2381:                        unsigned short *pp = (unsigned short *) (p -= 2);
                   2382:                        *pp = ntohs(*pp);
                   2383:                }
                   2384:        }
                   2385: 
                   2386:        /* strip leading blanks */
                   2387:        while (s != end && *s == ' ')
                   2388:                ++s;
                   2389: 
                   2390:        /* compress internal blanks and strip trailing blanks */
                   2391:        while (s != end && *s) {
                   2392:                if (*s++ != ' ' || (s != end && *s && *s != ' '))
                   2393:                        *p++ = *(s-1);
                   2394:        }
                   2395: 
                   2396:        /* wipe out trailing garbage */
                   2397:        while (p != end)
                   2398:                *p++ = '\0';
                   2399: }
                   2400: 
                   2401: static inline void do_identify (ide_drive_t *drive, byte cmd)
                   2402: {
                   2403:        int bswap;
                   2404:        struct hd_driveid *id;
                   2405:        unsigned long capacity, check;
                   2406: 
                   2407:        id = drive->id = kmalloc (SECTOR_WORDS*4, GFP_KERNEL);
                   2408:        ide_input_data(drive, id, SECTOR_WORDS);/* read 512 bytes of id info */
                   2409:        sti();
                   2410: 
                   2411: #if defined (CONFIG_SCSI_EATA_DMA) || defined (CONFIG_SCSI_EATA_PIO) || defined (CONFIG_SCSI_EATA)
                   2412:        /*
                   2413:         * EATA SCSI controllers do a hardware ATA emulation:  
                   2414:         * Ignore them if there is a driver for them available.
                   2415:         */
                   2416:        if ((id->model[0] == 'P' && id->model[1] == 'M')
                   2417:         || (id->model[0] == 'S' && id->model[1] == 'K')) {
                   2418:                printk("%s: EATA SCSI HBA %.10s\n", drive->name, id->model);
                   2419:                drive->present = 0;
                   2420:                return;
                   2421:        }
                   2422: #endif
                   2423: 
                   2424:        /*
                   2425:         *  WIN_IDENTIFY returns little-endian info,
                   2426:         *  WIN_PIDENTIFY *usually* returns little-endian info.
                   2427:         */
                   2428:        bswap = 1;
                   2429:        if (cmd == WIN_PIDENTIFY) {
                   2430:                if ((id->model[0] == 'N' && id->model[1] == 'E') /* NEC */
                   2431:                 || (id->model[0] == 'F' && id->model[1] == 'X') /* Mitsumi */
                   2432:                 || (id->model[0] == 'P' && id->model[1] == 'i'))/* Pioneer */
                   2433:                        bswap = 0;      /* Vertos drives may still be weird */
                   2434:        }
                   2435:        ide_fixstring (id->model,     sizeof(id->model),     bswap);
                   2436:        ide_fixstring (id->fw_rev,    sizeof(id->fw_rev),    bswap);
                   2437:        ide_fixstring (id->serial_no, sizeof(id->serial_no), bswap);
                   2438: 
                   2439:        if (strstr(id->model, "E X A B Y T E N E S T"))
                   2440:                return;
                   2441: 
                   2442: #ifdef CONFIG_BLK_DEV_IDEATAPI
                   2443:        /*
                   2444:         * Check for an ATAPI device
                   2445:         */
                   2446:        if (cmd == WIN_PIDENTIFY) {
                   2447:                byte type = (id->config >> 8) & 0x1f;
                   2448:                printk("%s: %s, ATAPI ", drive->name, id->model);
                   2449: #ifdef CONFIG_BLK_DEV_PROMISE
                   2450:                if (HWIF(drive)->is_promise2) {
                   2451:                        printk(" -- not supported on 2nd Promise port\n");
                   2452:                        drive->present = 0;
                   2453:                        return;
                   2454:                }
                   2455: #endif /* CONFIG_BLK_DEV_PROMISE */
                   2456:                if (!drive->ide_scsi) switch (type) {
                   2457:                        case 0:
                   2458:                                if (!strstr(id->model, "oppy") && !strstr(id->model, "poyp") && !strstr(id->model, "ZIP"))
                   2459:                                        printk("cdrom or floppy?, assuming ");
                   2460:                                if (drive->media != ide_cdrom && !strstr(id->model, "CD-ROM")) {
                   2461: #ifdef CONFIG_BLK_DEV_IDEFLOPPY
                   2462:                                        printk("FLOPPY drive\n");
                   2463:                                        drive->media = ide_floppy;
                   2464:                                        if (idefloppy_identify_device(drive, id))
                   2465:                                                drive->present = 1;
                   2466:                                        return;
                   2467: #else
                   2468:                                        printk("FLOPPY ");
                   2469:                                        break;
                   2470: #endif /* CONFIG_BLK_DEV_IDEFLOPPY */
                   2471:                                }
                   2472:                                /* Early cdrom models used zero */
                   2473:                        case 5:
                   2474: #ifdef CONFIG_BLK_DEV_IDECD
                   2475:                                printk ("CDROM drive\n");
                   2476:                                drive->media = ide_cdrom;
                   2477:                                drive->present = 1;
                   2478:                                drive->removable = 1;
                   2479:                                return;
                   2480: #else
                   2481:                                printk ("CDROM ");
                   2482:                                break;
                   2483: #endif /* CONFIG_BLK_DEV_IDECD */
                   2484:                        case 1:
                   2485: #ifdef CONFIG_BLK_DEV_IDETAPE
                   2486:                                printk ("TAPE drive");
                   2487:                                if (idetape_identify_device (drive,id)) {
                   2488:                                        drive->media = ide_tape;
                   2489:                                        drive->present = 1;
                   2490:                                        drive->removable = 1;
                   2491:                                        if (drive->autotune != 2 && HWIF(drive)->dmaproc != NULL) {
                   2492:                                                if (!HWIF(drive)->dmaproc(ide_dma_check, drive))
                   2493:                                                        printk(", DMA");
                   2494:                                        }
                   2495:                                        printk("\n");
                   2496:                                }
                   2497:                                else {
                   2498:                                        drive->present = 0;
                   2499:                                        printk ("\nide-tape: the tape is not supported by this version of the driver\n");
                   2500:                                }
                   2501:                                return;
                   2502: #else
                   2503:                                printk ("TAPE ");
                   2504:                                break;
                   2505: #endif /* CONFIG_BLK_DEV_IDETAPE */
                   2506:                        default:
                   2507:                                drive->present = 0;
                   2508:                                printk("Type %d - Unknown device\n", type);
                   2509:                                return;
                   2510:                }
                   2511: #ifdef CONFIG_BLK_DEV_IDESCSI
                   2512:                printk("drive - enabling SCSI emulation\n");
                   2513:                drive->media = ide_scsi;
                   2514:                drive->present = 1;
                   2515:                idescsi_setup(drive);
                   2516: #else
                   2517:                drive->present = 0;
                   2518:                printk("- not supported by this kernel\n");
                   2519: #endif /* CONFIG_BLK_DEV_IDESCSI */
                   2520:                return;
                   2521:        }
                   2522: #endif /* CONFIG_BLK_DEV_IDEATAPI */
                   2523: 
                   2524:        /* check for removable disks (eg. SYQUEST), ignore 'WD' drives */
                   2525:        if (id->config & (1<<7)) {      /* removable disk ? */
                   2526:                if (id->model[0] != 'W' || id->model[1] != 'D')
                   2527:                        drive->removable = 1;
                   2528:        }
                   2529: 
                   2530:        /* SunDisk drives: treat as non-removable, force one unit */
                   2531:        if (id->model[0] == 'S' && id->model[1] == 'u') {
                   2532:                drive->removable = 0;
                   2533:                if (drive->select.all & (1<<4)) {
                   2534:                    drive->present = 0;
                   2535:                    return;
                   2536:                }
                   2537:        }
                   2538: 
                   2539:        drive->media = ide_disk;
                   2540:        /* Extract geometry if we did not already have one for the drive */
                   2541:        if (!drive->present) {
                   2542:                drive->present = 1;
                   2543:                drive->cyl     = drive->bios_cyl  = id->cyls;
                   2544:                drive->head    = drive->bios_head = id->heads;
                   2545:                drive->sect    = drive->bios_sect = id->sectors;
                   2546:        }
                   2547:        /* Handle logical geometry translation by the drive */
                   2548:        if ((id->field_valid & 1) && id->cur_cyls && id->cur_heads
                   2549:         && (id->cur_heads <= 16) && id->cur_sectors)
                   2550:        {
                   2551:                /*
                   2552:                 * Extract the physical drive geometry for our use.
                   2553:                 * Note that we purposely do *not* update the bios info.
                   2554:                 * This way, programs that use it (like fdisk) will
                   2555:                 * still have the same logical view as the BIOS does,
                   2556:                 * which keeps the partition table from being screwed.
                   2557:                 *
                   2558:                 * An exception to this is the cylinder count,
                   2559:                 * which we reexamine later on to correct for 1024 limitations.
                   2560:                 */
                   2561:                drive->cyl  = id->cur_cyls;
                   2562:                drive->head = id->cur_heads;
                   2563:                drive->sect = id->cur_sectors;
                   2564: 
                   2565:                /* check for word-swapped "capacity" field in id information */
                   2566:                capacity = drive->cyl * drive->head * drive->sect;
                   2567:                check = (id->cur_capacity0 << 16) | id->cur_capacity1;
                   2568:                if (check == capacity) {        /* was it swapped? */
                   2569:                        /* yes, bring it into little-endian order: */
                   2570:                        id->cur_capacity0 = (capacity >>  0) & 0xffff;
                   2571:                        id->cur_capacity1 = (capacity >> 16) & 0xffff;
                   2572:                }
                   2573:        }
                   2574:        /* Use physical geometry if what we have still makes no sense */
                   2575:        if ((!drive->head || drive->head > 16) && id->heads && id->heads <= 16) {
                   2576:                drive->cyl  = id->cyls;
                   2577:                drive->head = id->heads;
                   2578:                drive->sect = id->sectors;
                   2579:        }
                   2580: 
                   2581:        /* calculate drive capacity, and select LBA if possible */
                   2582:        (void) current_capacity (drive);
                   2583: 
                   2584:        /* Correct the number of cyls if the bios value is too small */
                   2585:        if (drive->sect == drive->bios_sect && drive->head == drive->bios_head) {
                   2586:                if (drive->cyl > drive->bios_cyl)
                   2587:                        drive->bios_cyl = drive->cyl;
                   2588:        }
                   2589: 
                   2590:        if (!strncmp(id->model, "BMI ", 4) &&
                   2591:            strstr(id->model, " ENHANCED IDE ") &&
                   2592:            drive->select.b.lba)
                   2593:                drive->no_geom = 1;
                   2594: 
                   2595:        printk ("%s: %.40s, %ldMB w/%dkB Cache, CHS=%d/%d/%d",
                   2596:         drive->name, id->model, current_capacity(drive)/2048L, id->buf_size/2,
                   2597:         drive->bios_cyl, drive->bios_head, drive->bios_sect);
                   2598: 
                   2599:        drive->mult_count = 0;
                   2600:        if (id->max_multsect) {
                   2601:                drive->mult_req = INITIAL_MULT_COUNT;
                   2602:                if (drive->mult_req > id->max_multsect)
                   2603:                        drive->mult_req = id->max_multsect;
                   2604:                if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
                   2605:                        drive->special.b.set_multmode = 1;
                   2606:        }
                   2607:        if (drive->autotune != 2 && HWIF(drive)->dmaproc != NULL) {
                   2608:                if (!(HWIF(drive)->dmaproc(ide_dma_check, drive))) {
                   2609:                        if ((id->field_valid & 4) && (id->dma_ultra & (id->dma_ultra >> 8) & 7))
                   2610:                                printk(", UDMA");
                   2611:                        else
                   2612:                                printk(", DMA");
                   2613:                }
                   2614:        }
                   2615:        printk("\n");
                   2616: }
                   2617: 
                   2618: /*
                   2619:  * Delay for *at least* 50ms.  As we don't know how much time is left
                   2620:  * until the next tick occurs, we wait an extra tick to be safe.
                   2621:  * This is used only during the probing/polling for drives at boot time.
                   2622:  */
                   2623: static void delay_50ms (void)
                   2624: {
                   2625:        unsigned long timer = jiffies + ((HZ + 19)/20) + 1;
                   2626:        while (timer > jiffies);
                   2627: }
                   2628: 
                   2629: /*
                   2630:  * try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
                   2631:  * and waits for a response.  It also monitors irqs while this is
                   2632:  * happening, in hope of automatically determining which one is
                   2633:  * being used by the interface.
                   2634:  *
                   2635:  * Returns:    0  device was identified
                   2636:  *             1  device timed-out (no response to identify request)
                   2637:  *             2  device aborted the command (refused to identify itself)
                   2638:  */
                   2639: static int try_to_identify (ide_drive_t *drive, byte cmd)
                   2640: {
                   2641:        int hd_status, rc;
                   2642:        unsigned long timeout;
                   2643:        unsigned long irqs_on = 0;
                   2644:        int irq_off;
                   2645: 
                   2646:        if (!HWIF(drive)->irq) {                /* already got an IRQ? */
                   2647:                probe_irq_off(probe_irq_on());  /* clear dangling irqs */
                   2648:                irqs_on = probe_irq_on();       /* start monitoring irqs */
                   2649:                OUT_BYTE(drive->ctl,IDE_CONTROL_REG);   /* enable device irq */
                   2650:        }
                   2651: 
                   2652:        delay_50ms();                           /* take a deep breath */
                   2653:        if ((IN_BYTE(IDE_ALTSTATUS_REG) ^ IN_BYTE(IDE_STATUS_REG)) & ~INDEX_STAT) {
                   2654:                printk("%s: probing with STATUS instead of ALTSTATUS\n", drive->name);
                   2655:                hd_status = IDE_STATUS_REG;     /* ancient Seagate drives */
                   2656:        } else
                   2657:                hd_status = IDE_ALTSTATUS_REG;  /* use non-intrusive polling */
                   2658: 
                   2659: #if CONFIG_BLK_DEV_PROMISE
                   2660:        if (IS_PROMISE_DRIVE) {
                   2661:                if (promise_cmd(drive,PROMISE_IDENTIFY)) {
                   2662:                        if (irqs_on)
                   2663:                                (void) probe_irq_off(irqs_on);
                   2664:                        return 1;
                   2665:                }
                   2666:        } else
                   2667: #endif /* CONFIG_BLK_DEV_PROMISE */
                   2668:                OUT_BYTE(cmd,IDE_COMMAND_REG);          /* ask drive for ID */
                   2669:        timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
                   2670:        timeout += jiffies;
                   2671:        do {
                   2672:                if (jiffies > timeout) {
                   2673:                        if (irqs_on)
                   2674:                                (void) probe_irq_off(irqs_on);
                   2675:                        return 1;       /* drive timed-out */
                   2676:                }
                   2677:                delay_50ms();           /* give drive a breather */
                   2678:        } while (IN_BYTE(hd_status) & BUSY_STAT);
                   2679: 
                   2680:        delay_50ms();           /* wait for IRQ and DRQ_STAT */
                   2681:        if (OK_STAT(GET_STAT(),DRQ_STAT,BAD_R_STAT)) {
                   2682:                unsigned long flags;
                   2683:                save_flags(flags);
                   2684:                cli();                  /* some systems need this */
                   2685:                do_identify(drive, cmd); /* drive returned ID */
                   2686:                rc = 0;                 /* drive responded with ID */
                   2687:                (void) GET_STAT();      /* clear drive IRQ */
                   2688:                restore_flags(flags);
                   2689:        } else
                   2690:                rc = 2;                 /* drive refused ID */
                   2691:        if (!HWIF(drive)->irq) {
                   2692:                irq_off = probe_irq_off(irqs_on);       /* get our irq number */
                   2693:                if (irq_off > 0) {
                   2694:                        HWIF(drive)->irq = irq_off; /* save it for later */
                   2695:                        irqs_on = probe_irq_on();
                   2696:                        OUT_BYTE(drive->ctl|2,IDE_CONTROL_REG); /* mask device irq */
                   2697:                        udelay(5);
                   2698:                        (void) probe_irq_off(irqs_on);
                   2699:                        (void) probe_irq_off(probe_irq_on()); /* clear self-inflicted irq */
                   2700:                        (void) GET_STAT();      /* clear drive IRQ */
                   2701: 
                   2702:                } else {        /* Mmmm.. multiple IRQs.. don't know which was ours */
                   2703:                        printk("%s: IRQ probe failed (%d)\n", drive->name, irq_off);
                   2704: #ifdef CONFIG_BLK_DEV_CMD640
                   2705: #ifdef CMD640_DUMP_REGS
                   2706:                        if (HWIF(drive)->chipset == ide_cmd640) {
                   2707:                                printk("%s: Hmmm.. probably a driver problem.\n", drive->name);
                   2708:                                CMD640_DUMP_REGS;
                   2709:                        }
                   2710: #endif /* CMD640_DUMP_REGS */
                   2711: #endif /* CONFIG_BLK_DEV_CMD640 */
                   2712:                }
                   2713:        }
                   2714:        return rc;
                   2715: }
                   2716: 
                   2717: /*
                   2718:  * do_probe() has the difficult job of finding a drive if it exists,
                   2719:  * without getting hung up if it doesn't exist, without trampling on
                   2720:  * ethernet cards, and without leaving any IRQs dangling to haunt us later.
                   2721:  *
                   2722:  * If a drive is "known" to exist (from CMOS or kernel parameters),
                   2723:  * but does not respond right away, the probe will "hang in there"
                   2724:  * for the maximum wait time (about 30 seconds), otherwise it will
                   2725:  * exit much more quickly.
                   2726:  *
                   2727:  * Returns:    0  device was identified
                   2728:  *             1  device timed-out (no response to identify request)
                   2729:  *             2  device aborted the command (refused to identify itself)
                   2730:  *             3  bad status from device (possible for ATAPI drives)
                   2731:  *             4  probe was not attempted because failure was obvious
                   2732:  */
                   2733: static int do_probe (ide_drive_t *drive, byte cmd)
                   2734: {
                   2735:        int rc;
                   2736:        ide_hwif_t *hwif = HWIF(drive);
                   2737:        unsigned long timeout;
                   2738: #ifdef CONFIG_BLK_DEV_IDEATAPI
                   2739:        if (drive->present) {   /* avoid waiting for inappropriate probes */
                   2740:                if ((drive->media != ide_disk) && (cmd == WIN_IDENTIFY))
                   2741:                        return 4;
                   2742:        }
                   2743: #endif /* CONFIG_BLK_DEV_IDEATAPI */
                   2744: #ifdef DEBUG
                   2745:        printk("probing for %s: present=%d, media=%d, probetype=%s\n",
                   2746:                drive->name, drive->present, drive->media,
                   2747:                (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
                   2748: #endif
                   2749:        SELECT_DRIVE(hwif,drive);
                   2750:        delay_50ms();
                   2751:        if (IN_BYTE(IDE_SELECT_REG) != drive->select.all && !drive->present) {
                   2752:                OUT_BYTE(0xa0,IDE_SELECT_REG);  /* exit with drive0 selected */
                   2753:                delay_50ms();           /* allow BUSY_STAT to assert & clear */
                   2754:                return 3;    /* no i/f present: avoid killing ethernet cards */
                   2755:        }
                   2756: 
                   2757:        if (OK_STAT(GET_STAT(),READY_STAT,BUSY_STAT)
                   2758:         || drive->present || cmd == WIN_PIDENTIFY)
                   2759:        {
                   2760:                if ((rc = try_to_identify(drive,cmd)))   /* send cmd and wait */
                   2761:                        rc = try_to_identify(drive,cmd); /* failed: try again */
                   2762:                if (rc == 1 && cmd == WIN_PIDENTIFY && drive->autotune != 2) {
                   2763:                        printk("%s: no response (status = 0x%02x), resetting drive\n", drive->name, GET_STAT());
                   2764:                        delay_50ms();
                   2765:                        OUT_BYTE (drive->select.all, IDE_SELECT_REG);
                   2766:                        delay_50ms();
                   2767:                        OUT_BYTE(WIN_SRST, IDE_COMMAND_REG);
                   2768:                        timeout = jiffies;
                   2769:                        while ((GET_STAT() & BUSY_STAT) && jiffies < timeout + WAIT_WORSTCASE)
                   2770:                                delay_50ms();
                   2771:                        rc = try_to_identify(drive, cmd);
                   2772:                }
                   2773:                if (rc == 1)
                   2774:                        printk("%s: no response (status = 0x%02x)\n", drive->name, GET_STAT());
                   2775:                (void) GET_STAT();              /* ensure drive irq is clear */
                   2776:        } else {
                   2777:                rc = 3;                         /* not present or maybe ATAPI */
                   2778:        }
                   2779:        if (drive->select.b.unit != 0) {
                   2780:                OUT_BYTE(0xa0,IDE_SELECT_REG);  /* exit with drive0 selected */
                   2781:                delay_50ms();
                   2782:                (void) GET_STAT();              /* ensure drive irq is clear */
                   2783:        }
                   2784:        return rc;
                   2785: }
                   2786: 
                   2787: static void enable_nest (ide_drive_t *drive)
                   2788: {
                   2789:        unsigned long timeout;
                   2790: 
                   2791:        printk("%s: enabling %s -- ", HWIF(drive)->name, drive->id->model);
                   2792:        SELECT_DRIVE(HWIF(drive), drive);
                   2793:        delay_50ms();
                   2794:        OUT_BYTE(EXABYTE_ENABLE_NEST, IDE_COMMAND_REG);
                   2795:        timeout = jiffies + WAIT_WORSTCASE;
                   2796:        do {
                   2797:                if (jiffies > timeout) {
                   2798:                        printk("failed (timeout)\n");
                   2799:                        return;
                   2800:                }
                   2801:                delay_50ms();
                   2802:        } while (GET_STAT() & BUSY_STAT);
                   2803:        delay_50ms();
                   2804:        if (!OK_STAT(GET_STAT(), 0, BAD_STAT))
                   2805:                printk("failed (status = 0x%02x)\n", GET_STAT());
                   2806:        else
                   2807:                printk("success\n");
                   2808:        if (do_probe(drive, WIN_IDENTIFY) >= 2) { /* if !(success||timed-out) */
                   2809: #ifdef CONFIG_BLK_DEV_IDEATAPI
                   2810:                (void) do_probe(drive, WIN_PIDENTIFY); /* look for ATAPI device */
                   2811: #endif /* CONFIG_BLK_DEV_IDEATAPI */
                   2812:        }
                   2813: }
                   2814: 
                   2815: /*
                   2816:  * probe_for_drive() tests for existence of a given drive using do_probe().
                   2817:  *
                   2818:  * Returns:    0  no device was found
                   2819:  *             1  device was found (note: drive->present might still be 0)
                   2820:  */
                   2821: static inline byte probe_for_drive (ide_drive_t *drive)
                   2822: {
                   2823:        if (drive->noprobe)                     /* skip probing? */
                   2824:                return drive->present;
                   2825:        if (do_probe(drive, WIN_IDENTIFY) >= 2) { /* if !(success||timed-out) */
                   2826: #ifdef CONFIG_BLK_DEV_IDEATAPI
                   2827:                (void) do_probe(drive, WIN_PIDENTIFY); /* look for ATAPI device */
                   2828: #endif /* CONFIG_BLK_DEV_IDEATAPI */
                   2829:        }
                   2830:        if (drive->id && strstr(drive->id->model, "E X A B Y T E N E S T"))
                   2831:                enable_nest(drive);
                   2832:        if (!drive->present)
                   2833:                return 0;                       /* drive not found */
                   2834:        if (drive->id == NULL) {                /* identification failed? */
                   2835:                if (drive->media == ide_disk) {
                   2836:                        printk ("%s: non-IDE drive, CHS=%d/%d/%d\n",
                   2837:                         drive->name, drive->cyl, drive->head, drive->sect);
                   2838:                }
                   2839: #ifdef CONFIG_BLK_DEV_IDECD
                   2840:                else if (drive->media == ide_cdrom) {
                   2841:                        printk("%s: ATAPI cdrom (?)\n", drive->name);
                   2842:                }
                   2843: #endif /* CONFIG_BLK_DEV_IDECD */
                   2844:                else {
                   2845:                        drive->present = 0;     /* nuke it */
                   2846:                }
                   2847:        }
                   2848:        return 1;       /* drive was found */
                   2849: }
                   2850: 
                   2851: /*
                   2852:  * We query CMOS about hard disks : it could be that we have a SCSI/ESDI/etc
                   2853:  * controller that is BIOS compatible with ST-506, and thus showing up in our
                   2854:  * BIOS table, but not register compatible, and therefore not present in CMOS.
                   2855:  *
                   2856:  * Furthermore, we will assume that our ST-506 drives <if any> are the primary
                   2857:  * drives in the system -- the ones reflected as drive 1 or 2.  The first
                   2858:  * drive is stored in the high nibble of CMOS byte 0x12, the second in the low
                   2859:  * nibble.  This will be either a 4 bit drive type or 0xf indicating use byte
                   2860:  * 0x19 for an 8 bit type, drive 1, 0x1a for drive 2 in CMOS.  A non-zero value
                   2861:  * means we have an AT controller hard disk for that drive.
                   2862:  *
                   2863:  * Of course, there is no guarantee that either drive is actually on the
                   2864:  * "primary" IDE interface, but we don't bother trying to sort that out here.
                   2865:  * If a drive is not actually on the primary interface, then these parameters
                   2866:  * will be ignored.  This results in the user having to supply the logical
                   2867:  * drive geometry as a boot parameter for each drive not on the primary i/f.
                   2868:  *
                   2869:  * The only "perfect" way to handle this would be to modify the setup.[cS] code
                   2870:  * to do BIOS calls Int13h/Fn08h and Int13h/Fn48h to get all of the drive info
                   2871:  * for us during initialization.  I have the necessary docs -- any takers?  -ml
                   2872:  */
                   2873: static void probe_cmos_for_drives (ide_hwif_t *hwif)
                   2874: {
                   2875: #ifdef __i386__
                   2876:        extern struct drive_info_struct drive_info;
                   2877:        byte cmos_disks, *BIOS = (byte *) &drive_info;
                   2878:        int unit;
                   2879: 
                   2880: #ifdef CONFIG_BLK_DEV_PROMISE
                   2881:        if (hwif->is_promise2)
                   2882:                return;
                   2883: #endif /* CONFIG_BLK_DEV_PROMISE */
                   2884:        outb_p(0x12,0x70);              /* specify CMOS address 0x12 */
                   2885:        cmos_disks = inb_p(0x71);       /* read the data from 0x12 */
                   2886:        /* Extract drive geometry from CMOS+BIOS if not already setup */
                   2887:        for (unit = 0; unit < MAX_DRIVES; ++unit) {
                   2888:                ide_drive_t *drive = &hwif->drives[unit];
                   2889:                if ((cmos_disks & (0xf0 >> (unit*4))) && !drive->present && !drive->nobios) {
                   2890:                        drive->cyl   = drive->bios_cyl  = *(unsigned short *)BIOS;
                   2891:                        drive->head  = drive->bios_head = *(BIOS+2);
                   2892:                        drive->sect  = drive->bios_sect = *(BIOS+14);
                   2893:                        drive->ctl   = *(BIOS+8);
                   2894:                        drive->present = 1;
                   2895:                }
                   2896:                BIOS += 16;
                   2897:        }
                   2898: #endif
                   2899: }
                   2900: 
                   2901: /*
                   2902:  * This routine only knows how to look for drive units 0 and 1
                   2903:  * on an interface, so any setting of MAX_DRIVES > 2 won't work here.
                   2904:  */
                   2905: static void probe_hwif (ide_hwif_t *hwif)
                   2906: {
                   2907:        unsigned int unit;
                   2908: 
                   2909:        if (hwif->noprobe)
                   2910:                return;
                   2911:        if (hwif->io_base == HD_DATA)
                   2912:                probe_cmos_for_drives (hwif);
                   2913: #if CONFIG_BLK_DEV_PROMISE
                   2914:        if (!hwif->is_promise2 &&
                   2915:           (check_region(hwif->io_base,8) || check_region(hwif->ctl_port,1))) {
                   2916: #else
                   2917:        if (check_region(hwif->io_base,8) || check_region(hwif->ctl_port,1)) {
                   2918: #endif /* CONFIG_BLK_DEV_PROMISE */
                   2919:                int msgout = 0;
                   2920:                for (unit = 0; unit < MAX_DRIVES; ++unit) {
                   2921:                        ide_drive_t *drive = &hwif->drives[unit];
                   2922:                        if (drive->present) {
                   2923:                                drive->present = 0;
                   2924:                                printk("%s: ERROR, PORTS ALREADY IN USE\n", drive->name);
                   2925:                                msgout = 1;
                   2926:                        }
                   2927:                }
                   2928:                if (!msgout)
                   2929:                        printk("%s: ports already in use, skipping probe\n", hwif->name);
                   2930:        } else {
                   2931:                unsigned long flags;
                   2932:                save_flags(flags);
                   2933: 
                   2934:                sti();  /* needed for jiffies and irq probing */
                   2935:                /*
                   2936:                 * Second drive should only exist if first drive was found,
                   2937:                 * but a lot of cdrom drives are configured as single slaves.
                   2938:                 */
                   2939:                for (unit = 0; unit < MAX_DRIVES; ++unit) {
                   2940:                        ide_drive_t *drive = &hwif->drives[unit];
                   2941:                        (void) probe_for_drive (drive);
                   2942:                        if (drive->present && drive->media == ide_disk) {
                   2943:                                if ((!drive->head || drive->head > 16) && !drive->select.b.lba) {
                   2944:                                        printk("%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
                   2945:                                         drive->name, drive->head);
                   2946:                                        drive->present = 0;
                   2947:                                }
                   2948:                        }
                   2949:                        if (drive->present && !hwif->present) {
                   2950:                                hwif->present = 1;
                   2951:                                request_region(hwif->io_base,  8, hwif->name);
                   2952:                                request_region(hwif->ctl_port, 1, hwif->name);
                   2953:                        }
                   2954:                }
                   2955:                restore_flags(flags);
                   2956:                for (unit = 0; unit < MAX_DRIVES; ++unit) {
                   2957:                        ide_drive_t *drive = &hwif->drives[unit];
                   2958:                        if (drive->present && drive->media != ide_tape) {
                   2959:                                ide_tuneproc_t *tuneproc = HWIF(drive)->tuneproc;
                   2960:                                if (tuneproc != NULL && drive->autotune == 1)
                   2961:                                        tuneproc(drive, 255);   /* auto-tune PIO mode */
                   2962:                        }
                   2963:                }
                   2964:        }
                   2965: }
                   2966: 
                   2967: /*
                   2968:  * stridx() returns the offset of c within s,
                   2969:  * or -1 if c is '\0' or not found within s.
                   2970:  */
                   2971: static int stridx (const char *s, char c)
                   2972: {
                   2973:        char *i = strchr(s, c);
                   2974:        return (i && c) ? i - s : -1;
                   2975: }
                   2976: 
                   2977: /*
                   2978:  * match_parm() does parsing for ide_setup():
                   2979:  *
                   2980:  * 1. the first char of s must be '='.
                   2981:  * 2. if the remainder matches one of the supplied keywords,
                   2982:  *     the index (1 based) of the keyword is negated and returned.
                   2983:  * 3. if the remainder is a series of no more than max_vals numbers
                   2984:  *     separated by commas, the numbers are saved in vals[] and a
                   2985:  *     count of how many were saved is returned.  Base10 is assumed,
                   2986:  *     and base16 is allowed when prefixed with "0x".
                   2987:  * 4. otherwise, zero is returned.
                   2988:  */
                   2989: static int match_parm (char *s, const char *keywords[], int vals[], int max_vals)
                   2990: {
                   2991:        static const char *decimal = "0123456789";
                   2992:        static const char *hex = "0123456789abcdef";
                   2993:        int i, n;
                   2994: 
                   2995:        if (*s++ == '=') {
                   2996:                /*
                   2997:                 * Try matching against the supplied keywords,
                   2998:                 * and return -(index+1) if we match one
                   2999:                 */
                   3000:                if (keywords != NULL) {
                   3001:                        for (i = 0; *keywords != NULL; ++i) {
                   3002:                                if (!strcmp(s, *keywords++))
                   3003:                                        return -(i+1);
                   3004:                        }
                   3005:                }
                   3006:                /*
                   3007:                 * Look for a series of no more than "max_vals"
                   3008:                 * numeric values separated by commas, in base10,
                   3009:                 * or base16 when prefixed with "0x".
                   3010:                 * Return a count of how many were found.
                   3011:                 */
                   3012:                for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
                   3013:                        vals[n] = i;
                   3014:                        while ((i = stridx(decimal, *++s)) >= 0)
                   3015:                                vals[n] = (vals[n] * 10) + i;
                   3016:                        if (*s == 'x' && !vals[n]) {
                   3017:                                while ((i = stridx(hex, *++s)) >= 0)
                   3018:                                        vals[n] = (vals[n] * 0x10) + i;
                   3019:                        }
                   3020:                        if (++n == max_vals)
                   3021:                                break;
                   3022:                        if (*s == ',')
                   3023:                                ++s;
                   3024:                }
                   3025:                if (!*s)
                   3026:                        return n;
                   3027:        }
                   3028:        return 0;       /* zero = nothing matched */
                   3029: }
                   3030: 
                   3031: /*
                   3032:  * ide_setup() gets called VERY EARLY during initialization,
                   3033:  * to handle kernel "command line" strings beginning with "hdx="
                   3034:  * or "ide".  Here is the complete set currently supported:
                   3035:  *
                   3036:  * "hdx="  is recognized for all "x" from "a" to "h", such as "hdc".
                   3037:  * "idex=" is recognized for all "x" from "0" to "3", such as "ide1".
                   3038:  *
                   3039:  * "hdx=noprobe"       : drive may be present, but do not probe for it
                   3040:  * "hdx=none"          : drive is NOT present, ignore cmos and do not probe
                   3041:  * "hdx=nowerr"                : ignore the WRERR_STAT bit on this drive
                   3042:  * "hdx=cdrom"         : drive is present, and is a cdrom drive
                   3043:  * "hdx=cyl,head,sect" : disk drive is present, with specified geometry
                   3044:  * "hdx=autotune"      : driver will attempt to tune interface speed
                   3045:  *                             to the fastest PIO mode supported,
                   3046:  *                             if possible for this drive only.
                   3047:  *                             Not fully supported by all chipset types,
                   3048:  *                             and quite likely to cause trouble with
                   3049:  *                             older/odd IDE drives.
                   3050:  *
                   3051:  * "idebus=xx"         : inform IDE driver of VESA/PCI bus speed in Mhz,
                   3052:  *                             where "xx" is between 20 and 66 inclusive,
                   3053:  *                             used when tuning chipset PIO modes.
                   3054:  *                             For PCI bus, 25 is correct for a P75 system,
                   3055:  *                             30 is correct for P90,P120,P180 systems,
                   3056:  *                             and 33 is used for P100,P133,P166 systems.
                   3057:  *                             If in doubt, use idebus=33 for PCI.
                   3058:  *                             As for VLB, it is safest to not specify it.
                   3059:  *
                   3060:  * "idex=noprobe"      : do not attempt to access/use this interface
                   3061:  * "idex=base"         : probe for an interface at the addr specified,
                   3062:  *                             where "base" is usually 0x1f0 or 0x170
                   3063:  *                             and "ctl" is assumed to be "base"+0x206
                   3064:  * "idex=base,ctl"     : specify both base and ctl
                   3065:  * "idex=base,ctl,irq" : specify base, ctl, and irq number
                   3066:  * "idex=autotune"     : driver will attempt to tune interface speed
                   3067:  *                             to the fastest PIO mode supported,
                   3068:  *                             for all drives on this interface.
                   3069:  *                             Not fully supported by all chipset types,
                   3070:  *                             and quite likely to cause trouble with
                   3071:  *                             older/odd IDE drives.
                   3072:  * "idex=noautotune"   : driver will NOT attempt to tune interface speed
                   3073:  *                             This is the default for most chipsets,
                   3074:  *                             except the cmd640.
                   3075:  * "idex=serialize"    : do not overlap operations on idex and ide(x^1)
                   3076:  *
                   3077:  * The following are valid ONLY on ide0,
                   3078:  * and the defaults for the base,ctl ports must not be altered.
                   3079:  *
                   3080:  * "ide0=dtc2278"      : probe/support DTC2278 interface
                   3081:  * "ide0=ht6560b"      : probe/support HT6560B interface
                   3082:  * "ide0=cmd640_vlb"   : *REQUIRED* for VLB cards with the CMD640 chip
                   3083:  *                       (not for PCI -- automatically detected)
                   3084:  * "ide0=qd6580"       : probe/support qd6580 interface
                   3085:  * "ide0=ali14xx"      : probe/support ali14xx chipsets (ALI M1439, M1443, M1445)
                   3086:  * "ide0=umc8672"      : probe/support umc8672 chipsets
                   3087:  */
                   3088: void ide_setup (char *s)
                   3089: {
                   3090:        int i, vals[3];
                   3091:        ide_hwif_t *hwif;
                   3092:        ide_drive_t *drive;
                   3093:        unsigned int hw, unit;
                   3094:        const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
                   3095:        const char max_hwif  = '0' + (MAX_HWIFS - 1);
                   3096: 
                   3097:        printk("ide_setup: %s", s);
                   3098:        init_ide_data ();
                   3099: 
                   3100:        /*
                   3101:         * Look for drive options:  "hdx="
                   3102:         */
                   3103:        if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
                   3104:                const char *hd_words[] = {"none", "noprobe", "nowerr", "cdrom",
                   3105:                                "serialize", "autotune", "noautotune",
                   3106:                                "slow", "ide-scsi", NULL};
                   3107:                unit = s[2] - 'a';
                   3108:                hw   = unit / MAX_DRIVES;
                   3109:                unit = unit % MAX_DRIVES;
                   3110:                hwif = &ide_hwifs[hw];
                   3111:                drive = &hwif->drives[unit];
                   3112:                switch (match_parm(&s[3], hd_words, vals, 3)) {
                   3113:                        case -1: /* "none" */
                   3114:                                drive->nobios = 1;  /* drop into "noprobe" */
                   3115:                        case -2: /* "noprobe" */
                   3116:                                drive->noprobe = 1;
                   3117:                                goto done;
                   3118:                        case -3: /* "nowerr" */
                   3119:                                drive->bad_wstat = BAD_R_STAT;
                   3120:                                hwif->noprobe = 0;
                   3121:                                goto done;
                   3122:                        case -4: /* "cdrom" */
                   3123:                                drive->present = 1;
                   3124:                                drive->media = ide_cdrom;
                   3125:                                hwif->noprobe = 0;
                   3126:                                goto done;
                   3127:                        case -5: /* "serialize" */
                   3128:                                printk(" -- USE \"ide%d=serialize\" INSTEAD", hw);
                   3129:                                goto do_serialize;
                   3130:                        case -6: /* "autotune" */
                   3131:                                drive->autotune = 1;
                   3132:                                goto done;
                   3133:                        case -7: /* "noautotune" */
                   3134:                                drive->autotune = 2;
                   3135:                                goto done;
                   3136:                        case -8: /* "slow" */
                   3137:                                drive->slow = 1;
                   3138:                                goto done;
                   3139:                        case -9: /* "ide-scsi" */
                   3140:                                drive->ide_scsi = 1;
                   3141:                                goto done;
                   3142:                        case 3: /* cyl,head,sect */
                   3143:                                drive->media    = ide_disk;
                   3144:                                drive->cyl      = drive->bios_cyl  = vals[0];
                   3145:                                drive->head     = drive->bios_head = vals[1];
                   3146:                                drive->sect     = drive->bios_sect = vals[2];
                   3147:                                drive->present  = 1;
                   3148:                                drive->forced_geom = 1;
                   3149:                                hwif->noprobe = 0;
                   3150:                                goto done;
                   3151:                        default:
                   3152:                                goto bad_option;
                   3153:                }
                   3154:        }
                   3155: 
                   3156:        if (s[0] != 'i' || s[1] != 'd' || s[2] != 'e')
                   3157:                goto bad_option;
                   3158:        /*
                   3159:         * Look for bus speed option:  "idebus="
                   3160:         */
                   3161:        if (s[3] == 'b' && s[4] == 'u' && s[5] == 's') {
                   3162:                if (match_parm(&s[6], NULL, vals, 1) != 1)
                   3163:                        goto bad_option;
                   3164:                if (vals[0] >= 20 && vals[0] <= 66)
                   3165:                        idebus_parameter = vals[0];
                   3166:                else
                   3167:                        printk(" -- BAD BUS SPEED! Expected value from 20 to 66");
                   3168:                goto done;
                   3169:        }
                   3170:        /*
                   3171:         * Look for interface options:  "idex="
                   3172:         */
                   3173:        if (s[3] >= '0' && s[3] <= max_hwif) {
                   3174:                /*
                   3175:                 * Be VERY CAREFUL changing this: note hardcoded indexes below
                   3176:                 */
                   3177:                const char *ide_words[] = {"noprobe", "serialize", "autotune", "noautotune",
                   3178:                        "qd6580", "ht6560b", "cmd640_vlb", "dtc2278", "umc8672", "ali14xx", "dc4030", NULL};
                   3179:                hw = s[3] - '0';
                   3180:                hwif = &ide_hwifs[hw];
                   3181:                i = match_parm(&s[4], ide_words, vals, 3);
                   3182: 
                   3183:                /*
                   3184:                 * Cryptic check to ensure chipset not already set for hwif:
                   3185:                 */
                   3186:                if (i > 0 || i <= -5) {
                   3187:                        if (hwif->chipset != ide_unknown)
                   3188:                                goto bad_option;
                   3189:                        if (i <= -5) {
                   3190:                                if (ide_hwifs[1].chipset != ide_unknown)
                   3191:                                        goto bad_option;
                   3192:                                /*
                   3193:                                 * Interface keywords work only for ide0:
                   3194:                                 */
                   3195:                                if (hw != 0)
                   3196:                                        goto bad_hwif;
                   3197:                        }
                   3198:                }
                   3199: 
                   3200:                switch (i) {
                   3201: #ifdef CONFIG_BLK_DEV_PROMISE
                   3202:                        case -11: /* "dc4030" */
                   3203:                        {
                   3204:                                setup_dc4030(hwif);
                   3205:                                goto done;
                   3206:                        }
                   3207: #endif /* CONFIG_BLK_DEV_PROMISE */
                   3208: #ifdef CONFIG_BLK_DEV_ALI14XX
                   3209:                        case -10: /* "ali14xx" */
                   3210:                        {
                   3211:                                extern void init_ali14xx (void);
                   3212:                                init_ali14xx();
                   3213:                                goto done;
                   3214:                        }
                   3215: #endif /* CONFIG_BLK_DEV_ALI14XX */
                   3216: #ifdef CONFIG_BLK_DEV_UMC8672
                   3217:                        case -9: /* "umc8672" */
                   3218:                        {
                   3219:                                extern void init_umc8672 (void);
                   3220:                                init_umc8672();
                   3221:                                goto done;
                   3222:                        }
                   3223: #endif /* CONFIG_BLK_DEV_UMC8672 */
                   3224: #ifdef CONFIG_BLK_DEV_DTC2278
                   3225:                        case -8: /* "dtc2278" */
                   3226:                        {
                   3227:                                extern void init_dtc2278 (void);
                   3228:                                init_dtc2278();
                   3229:                                goto done;
                   3230:                        }
                   3231: #endif /* CONFIG_BLK_DEV_DTC2278 */
                   3232: #ifdef CONFIG_BLK_DEV_CMD640
                   3233:                        case -7: /* "cmd640_vlb" */
                   3234:                        {
                   3235:                                extern int cmd640_vlb; /* flag for cmd640.c */
                   3236:                                cmd640_vlb = 1;
                   3237:                                goto done;
                   3238:                        }
                   3239: #endif /* CONFIG_BLK_DEV_CMD640 */
                   3240: #ifdef CONFIG_BLK_DEV_HT6560B
                   3241:                        case -6: /* "ht6560b" */
                   3242:                        {
                   3243:                                extern void init_ht6560b (void);
                   3244:                                init_ht6560b();
                   3245:                                goto done;
                   3246:                        }
                   3247: #endif /* CONFIG_BLK_DEV_HT6560B */
                   3248: #if CONFIG_BLK_DEV_QD6580
                   3249:                        case -5: /* "qd6580" (has secondary i/f) */
                   3250:                        {
                   3251:                                extern void init_qd6580 (void);
                   3252:                                init_qd6580();
                   3253:                                goto done;
                   3254:                        }
                   3255: #endif /* CONFIG_BLK_DEV_QD6580 */
                   3256:                        case -4: /* "noautotune" */
                   3257:                                hwif->drives[0].autotune = 2;
                   3258:                                hwif->drives[1].autotune = 2;
                   3259:                                goto done;
                   3260:                        case -3: /* "autotune" */
                   3261:                                hwif->drives[0].autotune = 1;
                   3262:                                hwif->drives[1].autotune = 1;
                   3263:                                goto done;
                   3264:                        case -2: /* "serialize" */
                   3265:                        do_serialize:
                   3266:                                ide_hwifs[hw].serialized = 1;   /* serialize */
                   3267:                                ide_hwifs[hw^1].serialized = 1; /* with mate */
                   3268:                                goto done;
                   3269: 
                   3270:                        case -1: /* "noprobe" */
                   3271:                                hwif->noprobe = 1;
                   3272:                                goto done;
                   3273: 
                   3274:                        case 1: /* base */
                   3275:                                vals[1] = vals[0] + 0x206; /* default ctl */
                   3276:                        case 2: /* base,ctl */
                   3277:                                vals[2] = 0;    /* default irq = probe for it */
                   3278:                        case 3: /* base,ctl,irq */
                   3279:                                hwif->io_base  = vals[0];
                   3280:                                hwif->ctl_port = vals[1];
                   3281:                                hwif->irq      = vals[2];
                   3282:                                hwif->noprobe  = 0;
                   3283:                                hwif->chipset  = ide_generic;
                   3284:                                goto done;
                   3285: 
                   3286:                        case 0: goto bad_option;
                   3287:                        default:
                   3288:                                printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
                   3289:                                return;
                   3290:                }
                   3291:        }
                   3292: bad_option:
                   3293:        printk(" -- BAD OPTION\n");
                   3294:        return;
                   3295: bad_hwif:
                   3296:        printk("-- NOT SUPPORTED ON ide%d", hw);
                   3297: done:
                   3298:        printk("\n");
                   3299: }
                   3300: 
                   3301: /*
                   3302:  * This routine is called from the partition-table code in genhd.c
                   3303:  * to "convert" a drive to a logical geometry with fewer than 1024 cyls.
                   3304:  *
                   3305:  * The second parameter, "xparm", determines exactly how the translation 
                   3306:  * will be handled:
                   3307:  *              0 = convert to CHS with fewer than 1024 cyls
                   3308:  *                     using the same method as Ontrack DiskManager.
                   3309:  *              1 = same as "0", plus offset everything by 63 sectors.
                   3310:  *             -1 = similar to "0", plus redirect sector 0 to sector 1.
                   3311:  *             >1 = convert to a CHS geometry with "xparm" heads.
                   3312:  *
                   3313:  * Returns 0 if the translation was not possible, if the device was not 
                   3314:  * an IDE disk drive, or if a geometry was "forced" on the commandline.
                   3315:  * Returns 1 if the geometry translation was successful.
                   3316:  */
                   3317: int ide_xlate_1024 (kdev_t i_rdev, int xparm, const char *msg)
                   3318: {
                   3319:        ide_drive_t *drive;
                   3320:        static const byte head_vals[] = {4, 8, 16, 32, 64, 128, 255, 0};
                   3321:        const byte *heads = head_vals;
                   3322:        unsigned long tracks;
                   3323: 
                   3324:        if ((drive = get_info_ptr(i_rdev)) == NULL || drive->forced_geom)
                   3325:                return 0;
                   3326: 
                   3327:        if (xparm > 1 && xparm <= drive->bios_head && drive->bios_sect == 63)
                   3328:                return 0;               /* we already have a translation */
                   3329: 
                   3330:        printk("%s ", msg);
                   3331: 
                   3332:        if (drive->id) {
                   3333:                drive->cyl  = drive->id->cyls;
                   3334:                drive->head = drive->id->heads;
                   3335:                drive->sect = drive->id->sectors;
                   3336:        }
                   3337:        drive->bios_cyl  = drive->cyl;
                   3338:        drive->bios_head = drive->head;
                   3339:        drive->bios_sect = drive->sect;
                   3340:        drive->special.b.set_geometry = 1;
                   3341: 
                   3342:        tracks = drive->bios_cyl * drive->bios_head * drive->bios_sect / 63;
                   3343:        drive->bios_sect = 63;
                   3344:        if (xparm > 1) {
                   3345:                drive->bios_head = xparm;
                   3346:                drive->bios_cyl = tracks / drive->bios_head;
                   3347:        } else {
                   3348:                while (drive->bios_cyl >= 1024) {
                   3349:                        drive->bios_head = *heads;
                   3350:                        drive->bios_cyl = tracks / drive->bios_head;
                   3351:                        if (0 == *++heads)
                   3352:                                break;
                   3353:                }
                   3354: #if FAKE_FDISK_FOR_EZDRIVE
                   3355:                if (xparm == -1) {
                   3356:                        drive->remap_0_to_1 = 1;
                   3357:                        msg = "0->1";
                   3358:                } else
                   3359: #endif /* FAKE_FDISK_FOR_EZDRIVE */
                   3360:                if (xparm == 1) {
                   3361:                        drive->sect0 = 63;
                   3362:                        drive->bios_cyl = (tracks - 1) / drive->bios_head;
                   3363:                        msg = "+63";
                   3364:                }
                   3365:                printk("[remap %s] ", msg);
                   3366:        }
                   3367:        drive->part[0].nr_sects = current_capacity(drive);
                   3368:        printk("[%d/%d/%d]", drive->bios_cyl, drive->bios_head, drive->bios_sect);
                   3369:        return 1;
                   3370: }
                   3371: 
                   3372: #if MAX_HWIFS > 1
                   3373: /*
                   3374:  * save_match() is used to simplify logic in init_irq() below.
                   3375:  *
                   3376:  * A loophole here is that we may not know about a particular
                   3377:  * hwif's irq until after that hwif is actually probed/initialized..
                   3378:  * This could be a problem for the case where an hwif is on a
                   3379:  * dual interface that requires serialization (eg. cmd640) and another
                   3380:  * hwif using one of the same irqs is initialized beforehand.
                   3381:  *
                   3382:  * This routine detects and reports such situations, but does not fix them.
                   3383:  */
                   3384: static void save_match (ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
                   3385: {
                   3386:        ide_hwif_t *m = *match;
                   3387: 
                   3388:        if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
                   3389:                if (!new->hwgroup)
                   3390:                        return;
                   3391:                printk("%s: potential irq problem with %s and %s\n", hwif->name, new->name, m->name);
                   3392:        }
                   3393:        if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */
                   3394:                *match = new;
                   3395: }
                   3396: #endif /* MAX_HWIFS > 1 */
                   3397: 
                   3398: /*
                   3399:  * This routine sets up the irq for an ide interface, and creates a new
                   3400:  * hwgroup for the irq/hwif if none was previously assigned.
                   3401:  *
                   3402:  * Much of the code is for correctly detecting/handling irq sharing
                   3403:  * and irq serialization situations.  This is somewhat complex because
                   3404:  * it handles static as well as dynamic (PCMCIA) IDE interfaces.
                   3405:  *
                   3406:  * The SA_INTERRUPT in sa_flags means ide_intr() is always entered with
                   3407:  * interrupts completely disabled.  This can be bad for interrupt latency,
                   3408:  * but anything else has led to problems on some machines.  We re-enable
                   3409:  * interrupts as much as we can safely do in most places.
                   3410:  */
                   3411: static int init_irq (ide_hwif_t *hwif)
                   3412: {
                   3413:        unsigned long flags;
                   3414: #if MAX_HWIFS > 1
                   3415:        unsigned int index;
                   3416: #endif /* MAX_HWIFS > 1 */
                   3417:        ide_hwgroup_t *hwgroup;
                   3418:        ide_hwif_t *match = NULL;
                   3419: 
                   3420:        save_flags(flags);
                   3421:        cli();
                   3422: 
                   3423:        hwif->hwgroup = NULL;
                   3424: #if MAX_HWIFS > 1
                   3425:        /*
                   3426:         * Group up with any other hwifs that share our irq(s).
                   3427:         */
                   3428:        for (index = 0; index < MAX_HWIFS; index++) {
                   3429:                ide_hwif_t *h = &ide_hwifs[index];
                   3430:                if (h->hwgroup) {  /* scan only initialized hwif's */
                   3431:                        if (hwif->irq == h->irq) {
                   3432:                                hwif->sharing_irq = h->sharing_irq = 1;
                   3433:                                save_match(hwif, h, &match);
                   3434:                        }
                   3435:                        if (hwif->serialized) {
                   3436:                                ide_hwif_t *mate = &ide_hwifs[hwif->index^1];
                   3437:                                if (index == mate->index || h->irq == mate->irq)
                   3438:                                        save_match(hwif, h, &match);
                   3439:                        }
                   3440:                        if (h->serialized) {
                   3441:                                ide_hwif_t *mate = &ide_hwifs[h->index^1];
                   3442:                                if (hwif->irq == mate->irq)
                   3443:                                        save_match(hwif, h, &match);
                   3444:                        }
                   3445:                }
                   3446:        }
                   3447: #endif /* MAX_HWIFS > 1 */
                   3448:        /*
                   3449:         * If we are still without a hwgroup, then form a new one
                   3450:         */
                   3451:        if (match) {
                   3452:                hwgroup = match->hwgroup;
                   3453:        } else {
                   3454:                hwgroup = kmalloc(sizeof(ide_hwgroup_t), GFP_KERNEL);
                   3455:                hwgroup->hwif    = hwgroup->next_hwif = hwif->next = hwif;
                   3456:                hwgroup->rq      = NULL;
                   3457:                hwgroup->handler = NULL;
                   3458:                if (hwif->drives[0].present)
                   3459:                        hwgroup->drive = &hwif->drives[0];
                   3460:                else
                   3461:                        hwgroup->drive = &hwif->drives[1];
                   3462:                hwgroup->poll_timeout = 0;
                   3463:                hwgroup->active = 0;
                   3464:                init_timer(&hwgroup->timer);
                   3465:                hwgroup->timer.function = &timer_expiry;
                   3466:                hwgroup->timer.data = (unsigned long) hwgroup;
                   3467:        }
                   3468: 
                   3469:        /*
                   3470:         * Allocate the irq, if not already obtained for another hwif
                   3471:         */
                   3472:        if (!match || match->irq != hwif->irq) {
                   3473:                if (request_irq(hwif->irq, ide_intr, SA_INTERRUPT, hwif->name, hwgroup)) {
                   3474:                        if (!match)
                   3475:                                kfree(hwgroup);
                   3476:                        restore_flags(flags);
                   3477:                        return 1;
                   3478:                }
                   3479:        }
                   3480: 
                   3481:        /*
                   3482:         * Everything is okay, so link us into the hwgroup
                   3483:         */
                   3484:        hwif->hwgroup = hwgroup;
                   3485:        hwif->next = hwgroup->hwif->next;
                   3486:        hwgroup->hwif->next = hwif;
                   3487: 
                   3488:        restore_flags(flags);   /* safe now that hwif->hwgroup is set up */
                   3489: 
                   3490:        printk("%s at 0x%03x-0x%03x,0x%03x on irq %d", hwif->name,
                   3491:                hwif->io_base, hwif->io_base+7, hwif->ctl_port, hwif->irq);
                   3492:        if (match)
                   3493:                printk(" (%sed with %s)", hwif->sharing_irq ? "shar" : "serializ", match->name);
                   3494:        printk("\n");
                   3495:        return 0;
                   3496: }
                   3497: 
                   3498: static struct file_operations ide_fops = {
                   3499:        NULL,                   /* lseek - default */
                   3500:        block_read,             /* read - general block-dev read */
                   3501:        block_write,            /* write - general block-dev write */
                   3502:        NULL,                   /* readdir - bad */
                   3503:        NULL,                   /* select */
                   3504:        ide_ioctl,              /* ioctl */
                   3505:        NULL,                   /* mmap */
                   3506:        ide_open,               /* open */
                   3507:        ide_release,            /* release */
                   3508:        block_fsync             /* fsync */
                   3509:        ,NULL,                  /* fasync */
                   3510:        ide_check_media_change, /* check_media_change */
                   3511:        revalidate_disk         /* revalidate */
                   3512: };
                   3513: 
                   3514: #ifdef CONFIG_PCI
                   3515: #if defined(CONFIG_BLK_DEV_RZ1000) || defined(CONFIG_BLK_DEV_TRITON)
                   3516: 
                   3517: typedef void (ide_pci_init_proc_t)(byte, byte);
                   3518: 
                   3519: /*
                   3520:  * ide_probe_pci() scans PCI for a specific vendor/device function,
                   3521:  * and invokes the supplied init routine for each instance detected.
                   3522:  */
                   3523: static void ide_probe_pci (unsigned short vendor, unsigned short device, ide_pci_init_proc_t *init, int func_adj)
                   3524: {
                   3525:        unsigned long flags;
                   3526:        unsigned index;
                   3527:        byte fn, bus;
                   3528: 
                   3529:        save_flags(flags);
                   3530:        cli();
                   3531:        for (index = 0; !pcibios_find_device (vendor, device, index, &bus, &fn); ++index) {
                   3532:                init (bus, fn + func_adj);
                   3533:        }
                   3534:        restore_flags(flags);
                   3535: }
                   3536: 
                   3537: #endif /* defined(CONFIG_BLK_DEV_RZ1000) || defined(CONFIG_BLK_DEV_TRITON) */
                   3538: 
                   3539: static void ide_probe_promise_20246(void)
                   3540: {
                   3541:        byte fn, bus;
                   3542:        unsigned short io[6], count = 0;
                   3543:        unsigned int reg, tmp, i;
                   3544:        ide_hwif_t *hwif;
                   3545: 
                   3546:        memset(io, 0, 6 * sizeof(unsigned short));
                   3547:        if (pcibios_find_device(PCI_VENDOR_ID_PROMISE, PCI_DEVICE_ID_PROMISE_20246, 0, &bus, &fn))
                   3548:                return;
                   3549:        printk("ide: Promise Technology IDE Ultra-DMA 33 on PCI bus %d function %d\n", bus, fn);
                   3550:        for (reg = PCI_BASE_ADDRESS_0; reg <= PCI_BASE_ADDRESS_5; reg += 4) {
                   3551:                pcibios_read_config_dword(bus, fn, reg, &tmp);
                   3552:                if (tmp & PCI_BASE_ADDRESS_SPACE_IO)
                   3553:                        io[count++] = tmp & PCI_BASE_ADDRESS_IO_MASK;
                   3554:        }
                   3555:        for (i = 2; i < 4; i++) {
                   3556:                hwif = ide_hwifs + i;
                   3557:                if (hwif->chipset == ide_generic) {
                   3558:                        printk("ide%d: overridden with command line parameter\n", i);
                   3559:                        return;
                   3560:                }
                   3561:                tmp = (i - 2) * 2;
                   3562:                if (!io[tmp] || !io[tmp + 1]) {
                   3563:                        printk("ide%d: invalid port address %x, %x -- aborting\n", i, io[tmp], io[tmp + 1]);
                   3564:                        return;
                   3565:                }
                   3566:                hwif->io_base = io[tmp];
                   3567:                hwif->ctl_port = io[tmp + 1] + 2;
                   3568:                hwif->noprobe = 0;
                   3569:        }
                   3570: #ifdef CONFIG_BLK_DEV_TRITON
                   3571:        ide_init_promise (bus, fn, &ide_hwifs[2], &ide_hwifs[3], io[4]);
                   3572: #endif /* CONFIG_BLK_DEV_TRITON */
                   3573: }
                   3574: 
                   3575: #endif /* CONFIG_PCI */
                   3576: 
                   3577: /*
                   3578:  * ide_init_pci() finds/initializes "known" PCI IDE interfaces
                   3579:  *
                   3580:  * This routine should ideally be using pcibios_find_class() to find
                   3581:  * all IDE interfaces, but that function causes some systems to "go weird".
                   3582:  */
                   3583: static void probe_for_hwifs (void)
                   3584: {
                   3585: #ifdef CONFIG_PCI
                   3586:        /*
                   3587:         * Find/initialize PCI IDE interfaces
                   3588:         */
                   3589:        if (pcibios_present()) {
                   3590: #ifdef CONFIG_BLK_DEV_RZ1000
                   3591:                ide_pci_init_proc_t init_rz1000;
                   3592:                ide_probe_pci (PCI_VENDOR_ID_PCTECH, PCI_DEVICE_ID_PCTECH_RZ1000, &init_rz1000, 0);
                   3593:                ide_probe_pci (PCI_VENDOR_ID_PCTECH, PCI_DEVICE_ID_PCTECH_RZ1001, &init_rz1000, 0);
                   3594: #endif /* CONFIG_BLK_DEV_RZ1000 */
                   3595: #ifdef CONFIG_BLK_DEV_TRITON
                   3596:                /*
                   3597:                 * Apparently the BIOS32 services on Intel motherboards are
                   3598:                 * buggy and won't find the PCI_DEVICE_ID_INTEL_82371_1 for us.
                   3599:                 * So instead, we search for PCI_DEVICE_ID_INTEL_82371_0,
                   3600:                 * and then add 1.
                   3601:                 */
                   3602:                ide_probe_pci (PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371_0, &ide_init_triton, 1);
                   3603:                ide_probe_pci (PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371SB_1, &ide_init_triton, 0);
                   3604:                ide_probe_pci (PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB, &ide_init_triton, 0);
                   3605: #endif /* CONFIG_BLK_DEV_TRITON */
                   3606:                ide_probe_promise_20246();
                   3607:        }
                   3608: #endif /* CONFIG_PCI */
                   3609: #ifdef CONFIG_BLK_DEV_CMD640
                   3610:        {
                   3611:                extern void ide_probe_for_cmd640x (void);
                   3612:                ide_probe_for_cmd640x();
                   3613:        }
                   3614: #endif
                   3615: #ifdef CONFIG_BLK_DEV_PROMISE
                   3616:        init_dc4030();
                   3617: #endif
                   3618: }
                   3619: 
                   3620: static int hwif_init (int h)
                   3621: {
                   3622:        ide_hwif_t *hwif = &ide_hwifs[h];
                   3623:        void (*rfn)(void);
                   3624:        
                   3625:        if (!hwif->present)
                   3626:                return 0;
                   3627:        if (!hwif->irq) {
                   3628:                if (!(hwif->irq = default_irqs[h])) {
                   3629:                        printk("%s: DISABLED, NO IRQ\n", hwif->name);
                   3630:                        return (hwif->present = 0);
                   3631:                }
                   3632:        }
                   3633: #ifdef CONFIG_BLK_DEV_HD
                   3634:        if (hwif->irq == HD_IRQ && hwif->io_base != HD_DATA) {
                   3635:                printk("%s: CANNOT SHARE IRQ WITH OLD HARDDISK DRIVER (hd.c)\n", hwif->name);
                   3636:                return (hwif->present = 0);
                   3637:        }
                   3638: #endif /* CONFIG_BLK_DEV_HD */
                   3639:        
                   3640:        hwif->present = 0; /* we set it back to 1 if all is ok below */
                   3641:        switch (hwif->major) {
                   3642:        case IDE0_MAJOR: rfn = &do_ide0_request; break;
                   3643: #if MAX_HWIFS > 1
                   3644:        case IDE1_MAJOR: rfn = &do_ide1_request; break;
                   3645: #endif
                   3646: #if MAX_HWIFS > 2
                   3647:        case IDE2_MAJOR: rfn = &do_ide2_request; break;
                   3648: #endif
                   3649: #if MAX_HWIFS > 3
                   3650:        case IDE3_MAJOR: rfn = &do_ide3_request; break;
                   3651: #endif
                   3652:        default:
                   3653:                printk("%s: request_fn NOT DEFINED\n", hwif->name);
                   3654:                return (hwif->present = 0);
                   3655:        }
                   3656:        if (register_blkdev (hwif->major, hwif->name, &ide_fops)) {
                   3657:                printk("%s: UNABLE TO GET MAJOR NUMBER %d\n", hwif->name, hwif->major);
                   3658:        } else if (init_irq (hwif)) {
                   3659:                printk("%s: UNABLE TO GET IRQ %d\n", hwif->name, hwif->irq);
                   3660:                (void) unregister_blkdev (hwif->major, hwif->name);
                   3661:        } else {
                   3662:                init_gendisk(hwif);
                   3663:                blk_dev[hwif->major].request_fn = rfn;
                   3664:                read_ahead[hwif->major] = 8;    /* (4kB) */
                   3665:                hwif->present = 1;      /* success */
                   3666:        }
                   3667:        return hwif->present;
                   3668: }
                   3669: 
                   3670: /*
                   3671:  * This is gets invoked once during initialization, to set *everything* up
                   3672:  */
                   3673: int ide_init (void)
                   3674: {
                   3675:        int index;
                   3676: 
                   3677:        init_ide_data ();
                   3678:        /*
                   3679:         * Probe for special "known" interface chipsets
                   3680:         */
                   3681:        probe_for_hwifs ();
                   3682: 
                   3683:        /*
                   3684:         * Probe for drives in the usual way.. CMOS/BIOS, then poke at ports
                   3685:         */
                   3686:        for (index = 0; index < MAX_HWIFS; ++index)
                   3687:                probe_hwif (&ide_hwifs[index]);
                   3688:        for (index = 0; index < MAX_HWIFS; ++index)
                   3689:                hwif_init (index);
                   3690: 
                   3691: #ifdef CONFIG_BLK_DEV_IDETAPE
                   3692:        idetape_register_chrdev();      /* Register character device interface to the ide tape */
                   3693: #endif /* CONFIG_BLK_DEV_IDETAPE */
                   3694:        
                   3695:        return 0;
                   3696: }
                   3697: 
                   3698: #ifdef CONFIG_BLK_DEV_IDE_PCMCIA
                   3699: int ide_register(int io_base, int ctl_port, int irq)
                   3700: {
                   3701:        int index, i, rc = -1;
                   3702:        ide_hwif_t *hwif;
                   3703:        ide_drive_t *drive;
                   3704:        unsigned long flags;
                   3705: 
                   3706:        save_flags(flags);
                   3707:        cli();
                   3708:        for (index = 0; index < MAX_HWIFS; ++index) {
                   3709:                hwif = &ide_hwifs[index];
                   3710:                if (hwif->present) {
                   3711:                        if (hwif->io_base == io_base || hwif->ctl_port == ctl_port)
                   3712:                                break; /* this ide port already exists */
                   3713:                } else {
                   3714:                        hwif->io_base = io_base;
                   3715:                        hwif->ctl_port = ctl_port;
                   3716:                        hwif->irq = irq;
                   3717:                        hwif->noprobe = 0;
                   3718:                        probe_hwif(hwif);
                   3719:                        if (!hwif_init(index))
                   3720:                                break;
                   3721:                        for (i = 0; i < hwif->gd->nr_real; i++) {
                   3722:                                drive = &hwif->drives[i];
                   3723:                                revalidate_disk(MKDEV(hwif->major, i<<PARTN_BITS));
                   3724: #ifdef CONFIG_BLK_DEV_IDECD
                   3725:                                if (drive->present && drive->media == ide_cdrom)
                   3726:                                        ide_cdrom_setup(drive);
                   3727: #endif /* CONFIG_BLK_DEV_IDECD */
                   3728:                        }
                   3729:                        rc = index;
                   3730:                        break;
                   3731:                }
                   3732:        }
                   3733:        restore_flags(flags);
                   3734:        return rc;
                   3735: }
                   3736: 
                   3737: void ide_unregister (unsigned int index)
                   3738: {
                   3739:        struct gendisk *gd, **gdp;
                   3740:        ide_hwif_t *hwif, *g;
                   3741:        ide_hwgroup_t *hwgroup;
                   3742:        int irq_count = 0;
                   3743:        unsigned long flags;
                   3744: 
                   3745:        if (index >= MAX_HWIFS)
                   3746:                return;
                   3747:        save_flags(flags);
                   3748:        cli();
                   3749:        hwif = &ide_hwifs[index];
                   3750:        if (!hwif->present || hwif->drives[0].busy || hwif->drives[1].busy) {
                   3751:                restore_flags(flags);
                   3752:                return;
                   3753:        }
                   3754:        hwif->present = 0;
                   3755:        hwgroup = hwif->hwgroup;
                   3756: 
                   3757:        /*
                   3758:         * free the irq if we were the only hwif using it
                   3759:         */
                   3760:        g = hwgroup->hwif;
                   3761:        do {
                   3762:                if (g->irq == hwif->irq)
                   3763:                        ++irq_count;
                   3764:                g = g->next;
                   3765:        } while (g != hwgroup->hwif);
                   3766:        if (irq_count == 1)
                   3767:                free_irq(hwif->irq, hwgroup);
                   3768: 
                   3769:        /*
                   3770:         * Note that we only release the standard ports,
                   3771:         * and do not even try to handle any extra ports
                   3772:         * allocated for weird IDE interface chipsets.
                   3773:         */
                   3774:        release_region(hwif->io_base, 8);
                   3775:        release_region(hwif->ctl_port, 1);
                   3776: 
                   3777:        /*
                   3778:         * Remove us from the hwgroup, and free
                   3779:         * the hwgroup if we were the only member
                   3780:         */
                   3781:        while (hwgroup->hwif->next != hwif)
                   3782:                hwgroup->hwif = hwgroup->hwif->next;
                   3783:        hwgroup->hwif->next = hwif->next;
                   3784:        if (hwgroup->hwif == hwif)
                   3785:                hwgroup->hwif = hwif->next;
                   3786:        if (hwgroup->next_hwif == hwif)
                   3787:                hwgroup->next_hwif = hwif->next;
                   3788:        if (hwgroup->hwif == hwif)
                   3789:                kfree(hwgroup);
                   3790: 
                   3791:        /*
                   3792:         * Remove us from the kernel's knowledge
                   3793:         */
                   3794:        unregister_blkdev(hwif->major, hwif->name);
                   3795:        kfree(blksize_size[hwif->major]);
                   3796:        blk_dev[hwif->major].request_fn = NULL;
                   3797:        blksize_size[hwif->major] = NULL;
                   3798:        for (gdp = &gendisk_head; *gdp; gdp = &((*gdp)->next))
                   3799:                if (*gdp == hwif->gd)
                   3800:                        break;
                   3801:        if (*gdp == NULL)
                   3802:                printk("gd not in disk chain!\n");
                   3803:        else {
                   3804:                gd = *gdp; *gdp = gd->next;
                   3805:                kfree(gd->sizes);
                   3806:                kfree(gd->part);
                   3807:                kfree(gd);
                   3808:        }
                   3809:        init_hwif_data (index); /* restore hwif data to pristine status */
                   3810:        restore_flags(flags);
                   3811: }
                   3812: #endif /* CONFIG_BLK_DEV_IDE_PCMCIA */

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.