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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [drivers/] [block/] [ide.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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