Annotation of Gnu-Mach/i386/i386at/gpl/linux/block/ide.c, revision 1.1.1.1

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

unix.superglobalmegacorp.com

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