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

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