OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [rc203soc/] [sw/] [uClinux/] [arch/] [armnommu/] [drivers/] [block/] [ide.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.