Annotation of Gnu-Mach/linux/dev/drivers/block/ide.c, revision 1.1.1.2

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

unix.superglobalmegacorp.com

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