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

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