Import 1.3.39
[davej-history.git] / drivers / block / ide.c
blob24057608bf6371c814d3852b5f6bdf931e95430d
1 /*
2 * linux/drivers/block/ide.c Version 5.17 Nov 3, 1995
4 * Copyright (C) 1994, 1995 Linus Torvalds & authors (see below)
5 */
7 /*
8 * This is the multiple IDE interface driver, as evolved from hd.c.
9 * It supports up to four IDE interfaces, on one or more IRQs (usually 14 & 15).
10 * There can be up to two drives per interface, as per the ATA-2 spec.
12 * Primary i/f: ide0: major=3; (hda) minor=0; (hdb) minor=64
13 * Secondary i/f: ide1: major=22; (hdc or hd1a) minor=0; (hdd or hd1b) minor=64
14 * Tertiary i/f: ide2: major=33; (hde) minor=0; (hdf) minor=64
15 * Quaternary i/f: ide3: major=34; (hdg) minor=0; (hdh) minor=64
17 * It is easy to extend ide.c to handle more than four interfaces:
19 * Change the MAX_HWIFS constant in ide.h.
21 * Define some new major numbers (in major.h), and insert them into
22 * the ide_hwif_to_major table in ide.c.
24 * Fill in the extra values for the new interfaces into the two tables
25 * inside ide.c: default_io_base[] and default_irqs[].
27 * Create the new request handlers by cloning "do_ide3_request()"
28 * for each new interface, and add them to the switch statement
29 * in the ide_init() function in ide.c.
31 * Recompile, create the new /dev/ entries, and it will probably work.
33 * From hd.c:
34 * |
35 * | It traverses the request-list, using interrupts to jump between functions.
36 * | As nearly all functions can be called within interrupts, we may not sleep.
37 * | Special care is recommended. Have Fun!
38 * |
39 * | modified by Drew Eckhardt to check nr of hd's from the CMOS.
40 * |
41 * | Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
42 * | in the early extended-partition checks and added DM partitions.
43 * |
44 * | Early work on error handling by Mika Liljeberg (liljeber@cs.Helsinki.FI).
45 * |
46 * | IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
47 * | and general streamlining by Mark Lord (mlord@bnr.ca).
49 * October, 1994 -- Complete line-by-line overhaul for linux 1.1.x, by:
51 * Mark Lord (mlord@bnr.ca) (IDE Perf.Pkg)
52 * Delman Lee (delman@mipg.upenn.edu) ("Mr. atdisk2")
53 * Petri Mattila (ptjmatti@kruuna.helsinki.fi) (EIDE stuff)
54 * Scott Snyder (snyder@fnald0.fnal.gov) (ATAPI IDE cd-rom)
56 * Maintained by Mark Lord (mlord@bnr.ca): ide.c, ide.h, triton.c, hd.c, ..
58 * This was a rewrite of just about everything from hd.c, though some original
59 * code is still sprinkled about. Think of it as a major evolution, with
60 * inspiration from lots of linux users, esp. hamish@zot.apana.org.au
62 * Version 1.0 ALPHA initial code, primary i/f working okay
63 * Version 1.3 BETA dual i/f on shared irq tested & working!
64 * Version 1.4 BETA added auto probing for irq(s)
65 * Version 1.5 BETA added ALPHA (untested) support for IDE cd-roms,
66 * ...
67 * Version 3.5 correct the bios_cyl field if it's too small
68 * (linux 1.1.76) (to help fdisk with brain-dead BIOSs)
69 * Version 3.6 cosmetic corrections to comments and stuff
70 * (linux 1.1.77) reorganise probing code to make it understandable
71 * added halfway retry to probing for drive identification
72 * added "hdx=noprobe" command line option
73 * allow setting multmode even when identification fails
74 * Version 3.7 move set_geometry=1 from do_identify() to ide_init()
75 * increase DRQ_WAIT to eliminate nuisance messages
76 * wait for DRQ_STAT instead of DATA_READY during probing
77 * (courtesy of Gary Thomas gary@efland.UU.NET)
78 * Version 3.8 fixed byte-swapping for confused Mitsumi cdrom drives
79 * update of ide-cd.c from Scott, allows blocksize=1024
80 * cdrom probe fixes, inspired by jprang@uni-duisburg.de
81 * Version 3.9 don't use LBA if lba_capacity looks funny
82 * correct the drive capacity calculations
83 * fix probing for old Seagates without IDE_ALTSTATUS_REG
84 * fix byte-ordering for some NEC cdrom drives
85 * Version 3.10 disable multiple mode by default; was causing trouble
86 * Version 3.11 fix mis-identification of old WD disks as cdroms
87 * Version 3,12 simplify logic for selecting initial mult_count
88 * (fixes problems with buggy WD drives)
89 * Version 3.13 remove excess "multiple mode disabled" messages
90 * Version 3.14 fix ide_error() handling of BUSY_STAT
91 * fix byte-swapped cdrom strings (again.. arghh!)
92 * ignore INDEX bit when checking the ALTSTATUS reg
93 * Version 3.15 add SINGLE_THREADED flag for use with dual-CMD i/f
94 * ignore WRERR_STAT for non-write operations
95 * added vlb_sync support for DC-2000A & others,
96 * (incl. some Promise chips), courtesy of Frank Gockel
97 * Version 3.16 convert vlb_32bit and vlb_sync into runtime flags
98 * add ioctls to get/set VLB flags (HDIO_[SG]ET_CHIPSET)
99 * rename SINGLE_THREADED to SUPPORT_SERIALIZE,
100 * add boot flag to "serialize" operation for CMD i/f
101 * add optional support for DTC2278 interfaces,
102 * courtesy of andy@cercle.cts.com (Dyan Wile).
103 * add boot flag to enable "dtc2278" probe
104 * add probe to avoid EATA (SCSI) interfaces,
105 * courtesy of neuffer@goofy.zdv.uni-mainz.de.
106 * Version 4.00 tidy up verify_area() calls - heiko@colossus.escape.de
107 * add flag to ignore WRERR_STAT for some drives
108 * courtesy of David.H.West@um.cc.umich.edu
109 * assembly syntax tweak to vlb_sync
110 * removeable drive support from scuba@cs.tu-berlin.de
111 * add transparent support for DiskManager-6.0x "Dynamic
112 * Disk Overlay" (DDO), most of this in in genhd.c
113 * eliminate "multiple mode turned off" message at boot
114 * Version 4.10 fix bug in ioctl for "hdparm -c3"
115 * fix DM6:DDO support -- now works with LILO, fdisk, ...
116 * don't treat some naughty WD drives as removeable
117 * Version 4.11 updated DM6 support using info provided by OnTrack
118 * Version 5.00 major overhaul, multmode setting fixed, vlb_sync fixed
119 * added support for 3rd/4th/alternative IDE ports
120 * created ide.h; ide-cd.c now compiles separate from ide.c
121 * hopefully fixed infinite "unexpected_intr" from cdroms
122 * zillions of other changes and restructuring
123 * somehow reduced overall memory usage by several kB
124 * probably slowed things down slightly, but worth it
125 * Version 5.01 AT LAST!! Finally understood why "unexpected_intr"
126 * was happening at various times/places: whenever the
127 * ide-interface's ctl_port was used to "mask" the irq,
128 * it also would trigger an edge in the process of masking
129 * which would result in a self-inflicted interrupt!!
130 * (such a stupid way to build a hardware interrupt mask).
131 * This is now fixed (after a year of head-scratching).
132 * Version 5.02 got rid of need for {enable,disable}_irq_list()
133 * Version 5.03 tune-ups, comments, remove "busy wait" from drive resets
134 * removed PROBE_FOR_IRQS option -- no longer needed
135 * OOOPS! fixed "bad access" bug for 2nd drive on an i/f
136 * Version 5.04 changed "ira %d" to "irq %d" in DEBUG message
137 * added more comments, cleaned up unexpected_intr()
138 * OOOPS! fixed null pointer problem in ide reset code
139 * added autodetect for Triton chipset -- no effect yet
140 * Version 5.05 OOOPS! fixed bug in revalidate_disk()
141 * OOOPS! fixed bug in ide_do_request()
142 * added ATAPI reset sequence for cdroms
143 * Version 5.10 added Bus-Mastered DMA support for Triton Chipset
144 * some (mostly) cosmetic changes
145 * Version 5.11 added ht6560b support by malafoss@snakemail.hut.fi
146 * reworked PCI scanning code
147 * added automatic RZ1000 detection/support
148 * added automatic PCI CMD640 detection/support
149 * added option for VLB CMD640 support
150 * tweaked probe to find cdrom on hdb with disks on hda,hdc
151 * Version 5.12 some performance tuning
152 * added message to alert user to bad /dev/hd[cd] entries
153 * OOOPS! fixed bug in atapi reset
154 * driver now forces "serialize" again for all cmd640 chips
155 * noticed REALLY_SLOW_IO had no effect, moved it to ide.c
156 * made do_drive_cmd() into public ide_do_drive_cmd()
157 * Version 5.13 fixed typo ('B'), thanks to houston@boyd.geog.mcgill.ca
158 * fixed ht6560b support
159 * Version 5.13b (sss) fix problem in calling ide_cdrom_setup()
160 * don't bother invalidating nonexistent partitions
161 * Version 5.14 fixes to cmd640 support.. maybe it works now(?)
162 * added & tested full EZ-DRIVE support -- don't use LILO!
163 * don't enable 2nd CMD640 PCI port during init - conflict
164 * Version 5.15 bug fix in init_cmd640_vlb()
165 * bug fix in interrupt sharing code
166 * Version 5.16 ugh.. fix "serialize" support, broken in 5.15
167 * remove "Huh?" from cmd640 code
168 * added qd6580 interface speed select from Colten Edwards
169 * Version 5.17 kludge around bug in BIOS32 on Intel triton motherboards
171 * Driver compile-time options are in ide.h
173 * To do, in likely order of completion:
174 * - add ALI M1443/1445 chipset support from derekn@vw.ece.cmu.edu
175 * - add Promise Caching controller support from peterd@pnd-pc.demon.co.uk
176 * - add ioctls to get/set interface timings on various interfaces
177 * - modify kernel to obtain BIOS geometry for drives on 2nd/3rd/4th i/f
178 * - improved CMD support: handed this off to someone else
179 * - find someone to work on IDE *tape drive* support
182 #undef REALLY_SLOW_IO /* most systems can safely undef this */
184 #include <linux/config.h>
185 #include <linux/types.h>
186 #include <linux/string.h>
187 #include <linux/kernel.h>
188 #include <linux/delay.h>
189 #include <linux/timer.h>
190 #include <linux/mm.h>
191 #include <linux/ioport.h>
192 #include <linux/interrupt.h>
193 #include <linux/major.h>
194 #include <linux/blkdev.h>
195 #include <linux/errno.h>
196 #include <linux/hdreg.h>
197 #include <linux/genhd.h>
198 #include <linux/malloc.h>
200 #include <asm/byteorder.h>
201 #include <asm/irq.h>
202 #include <asm/segment.h>
203 #include <asm/io.h>
205 #ifdef CONFIG_PCI
206 #include <linux/bios32.h>
207 #include <linux/pci.h>
208 #endif /* CONFIG_PCI */
210 #include "ide.h"
212 ide_hwif_t ide_hwifs[MAX_HWIFS]; /* hwif info */
213 static ide_hwgroup_t *irq_to_hwgroup [16];
214 static const byte ide_hwif_to_major[MAX_HWIFS] = {IDE0_MAJOR, IDE1_MAJOR, IDE2_MAJOR, IDE3_MAJOR};
216 static const unsigned short default_io_base[MAX_HWIFS] = {0x1f0, 0x170, 0x1e8, 0x168};
217 static const byte default_irqs[MAX_HWIFS] = {14, 15, 11, 10};
218 static int serialized = 0; /* "serialize" option */
219 static int disallow_unmask = 0; /* for buggy hardware */
221 #if (DISK_RECOVERY_TIME > 0)
223 * For really screwy hardware (hey, at least it *can* be used with Linux)
224 * we can enforce a minimum delay time between successive operations.
226 static unsigned long read_timer(void)
228 unsigned long t, flags;
229 int i;
231 save_flags(flags);
232 cli();
233 t = jiffies * 11932;
234 outb_p(0, 0x43);
235 i = inb_p(0x40);
236 i |= inb(0x40) << 8;
237 restore_flags(flags);
238 return (t - i);
241 void ide_set_recovery_timer (ide_hwif_t *hwif)
243 hwif->last_time = read_timer();
245 #endif /* DISK_RECOVERY_TIME */
248 * init_ide_data() sets reasonable default values into all fields
249 * of all instances of the hwifs and drives, but only on the first call.
250 * Subsequent calls have no effect (they don't wipe out anything).
252 * This routine is normally called at driver initialization time,
253 * but may also be called MUCH earlier during kernel "command-line"
254 * parameter processing. As such, we cannot depend on any other parts
255 * of the kernel (such as memory allocation) to be functioning yet.
257 * This is too bad, as otherwise we could dynamically allocate the
258 * ide_drive_t structs as needed, rather than always consuming memory
259 * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
261 #define MAGIC_COOKIE 0x12345678
262 static void init_ide_data (void)
264 byte *p;
265 unsigned int h, unit;
266 static unsigned long magic_cookie = MAGIC_COOKIE;
268 if (magic_cookie != MAGIC_COOKIE)
269 return; /* already initialized */
270 magic_cookie = 0;
272 for (h = 0; h < 16; ++h)
273 irq_to_hwgroup[h] = NULL;
275 /* bulk initialize hwif & drive info with zeros */
276 p = ((byte *) ide_hwifs) + sizeof(ide_hwifs);
277 do {
278 *--p = 0;
279 } while (p > (byte *) ide_hwifs);
281 for (h = 0; h < MAX_HWIFS; ++h) {
282 ide_hwif_t *hwif = &ide_hwifs[h];
284 /* fill in any non-zero initial values */
285 hwif->noprobe = (h > 1);
286 hwif->io_base = default_io_base[h];
287 hwif->ctl_port = hwif->io_base ? hwif->io_base+0x206 : 0x000;
288 #ifdef CONFIG_BLK_DEV_HD
289 if (hwif->io_base == HD_DATA)
290 hwif->noprobe = 1; /* may be overriden by ide_setup() */
291 #endif /* CONFIG_BLK_DEV_HD */
292 hwif->major = ide_hwif_to_major[h];
293 hwif->name[0] = 'i';
294 hwif->name[1] = 'd';
295 hwif->name[2] = 'e';
296 hwif->name[3] = '0' + h;
298 for (unit = 0; unit < MAX_DRIVES; ++unit) {
299 ide_drive_t *drive = &hwif->drives[unit];
301 /* fill in any non-zero initial values */
302 drive->select.all = (unit<<4)|0xa0;
303 drive->hwif = hwif;
304 drive->ctl = 0x08;
305 drive->ready_stat = READY_STAT;
306 drive->bad_wstat = BAD_W_STAT;
307 drive->special.b.recalibrate = 1;
308 drive->special.b.set_geometry = 1;
309 drive->name[0] = 'h';
310 drive->name[1] = 'd';
311 drive->name[2] = 'a' + (h * MAX_DRIVES) + unit;
316 #define VLB_SYNC 1
318 * Some localbus EIDE interfaces require a special access sequence
319 * when using 32-bit I/O instructions to transfer data. We call this
320 * the "vlb_sync" sequence, which consists of three successive reads
321 * of the sector count register location, with interrupts disabled
322 * to ensure that the reads all happen together.
324 static inline void do_vlb_sync (unsigned short port) {
325 (void) inb (port);
326 (void) inb (port);
327 (void) inb (port);
331 * This is used for most PIO data transfers *from* the IDE interface
333 void ide_input_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
335 unsigned short io_base = HWIF(drive)->io_base;
336 unsigned short data_reg = io_base+IDE_DATA_OFFSET;
338 if (drive->vlb_32bit) {
339 #ifdef VLB_SYNC
340 if (drive->vlb_sync) {
341 cli();
342 do_vlb_sync(io_base+IDE_NSECTOR_OFFSET);
343 insl(data_reg, buffer, wcount);
344 if (drive->unmask)
345 sti();
346 } else
347 #endif /* VLB_SYNC */
348 insl(data_reg, buffer, wcount);
349 } else
350 insw(data_reg, buffer, wcount<<1);
354 * This is used for most PIO data transfers *to* the IDE interface
356 void ide_output_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
358 unsigned short io_base = HWIF(drive)->io_base;
359 unsigned short data_reg = io_base+IDE_DATA_OFFSET;
361 if (drive->vlb_32bit) {
362 #ifdef VLB_SYNC
363 if (drive->vlb_sync) {
364 cli();
365 do_vlb_sync(io_base+IDE_NSECTOR_OFFSET);
366 outsl(data_reg, buffer, wcount);
367 if (drive->unmask)
368 sti();
369 } else
370 #endif /* VLB_SYNC */
371 outsl(data_reg, buffer, wcount);
372 } else
373 outsw(data_reg, buffer, wcount<<1);
376 #if SUPPORT_HT6560B
378 * This routine handles interface switching for the peculiar hardware design
379 * on the F.G.I./Holtek HT-6560B VLB IDE interface.
380 * The HT-6560B can only enable one IDE port at a time, and requires a
381 * silly sequence (below) whenever we switch between primary and secondary.
383 * Apparently, systems with multiple CMD640 chips may need something similar..
385 * This algorithm courtesy of malafoss@snakemail.hut.fi
388 void ide_hwif_select (ide_hwif_t *hwif)
390 static byte current_select = 0;
392 if (hwif->select != current_select) {
393 unsigned long flags;
394 save_flags (flags);
395 cli();
396 current_select = hwif->select;
397 (void) inb(0x3e6);
398 (void) inb(0x3e6);
399 (void) inb(0x3e6);
400 (void) inb(0x3e6);
401 outb(current_select,0x3e6);
402 restore_flags (flags);
405 #endif /* SUPPORT_HT6560B */
408 * This should get invoked any time we exit the driver to
409 * wait for an interrupt response from a drive. handler() points
410 * at the appropriate code to handle the next interrupt, and a
411 * timer is started to prevent us from waiting forever in case
412 * something goes wrong (see the timer_expiry() handler later on).
414 void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler)
416 ide_hwgroup_t *hwgroup = HWGROUP(drive);
417 #ifdef DEBUG
418 if (hwgroup->handler != NULL)
419 printk("%s: ide_set_handler: old handler not null\n", drive->name);
420 #endif
421 hwgroup->handler = handler;
422 hwgroup->timer.expires = jiffies + WAIT_CMD;
423 add_timer(&(hwgroup->timer));
427 * lba_capacity_is_ok() performs a sanity check on the claimed "lba_capacity"
428 * value for this drive (from its reported identification information).
430 * Returns: 1 if lba_capacity looks sensible
431 * 0 otherwise
433 static int lba_capacity_is_ok (struct hd_driveid *id)
435 unsigned long lba_sects = id->lba_capacity;
436 unsigned long chs_sects = id->cyls * id->heads * id->sectors;
437 unsigned long _10_percent = chs_sects / 10;
439 /* perform a rough sanity check on lba_sects: within 10% is "okay" */
440 if ((lba_sects - chs_sects) < _10_percent)
441 return 1; /* lba_capacity is good */
443 /* some drives have the word order reversed */
444 lba_sects = (lba_sects << 16) | (lba_sects >> 16);
445 if ((lba_sects - chs_sects) < _10_percent) {
446 id->lba_capacity = lba_sects; /* fix it */
447 return 1; /* lba_capacity is (now) good */
449 return 0; /* lba_capacity value is bad */
453 * current_capacity() returns the capacity (in sectors) of a drive
454 * according to its current geometry/LBA settings.
456 static unsigned long current_capacity (ide_drive_t *drive)
458 struct hd_driveid *id = drive->id;
459 unsigned long capacity;
461 if (!drive->present)
462 return 0;
463 if (drive->media != disk)
464 return 0x1fffff; /* cdrom */
465 /* Determine capacity, and use LBA if the drive properly supports it */
466 if (id != NULL && (id->capability & 2) && lba_capacity_is_ok(id)) {
467 drive->select.b.lba = 1;
468 capacity = id->lba_capacity;
469 } else {
470 drive->select.b.lba = 0;
471 capacity = drive->cyl * drive->head * drive->sect;
473 return (capacity - drive->sect0);
477 * ide_geninit() is called exactly *once* for each major, from genhd.c,
478 * at the beginning of the initial partition check for the drives.
480 static void ide_geninit (struct gendisk *gd)
482 unsigned int unit;
483 ide_hwif_t *hwif = gd->real_devices;
485 for (unit = 0; unit < gd->nr_real; ++unit) {
486 ide_drive_t *drive = &hwif->drives[unit];
487 #ifdef CONFIG_BLK_DEV_IDECD
488 if (drive->present && drive->media == cdrom)
489 ide_cdrom_setup(drive);
490 #endif /* CONFIG_BLK_DEV_IDECD */
491 drive->part[0].nr_sects = current_capacity(drive);
492 if (!drive->present || drive->media != disk) {
493 drive->part[0].start_sect = -1; /* skip partition check */
497 * The partition check in genhd.c needs this string to identify
498 * our minor devices by name for display purposes.
499 * Note that doing this will prevent us from working correctly
500 * if ever called a second time for this major (never happens).
502 gd->real_devices = hwif->drives[0].name; /* name of first drive */
506 * init_gendisk() (as opposed to ide_geninit) is called for each major device,
507 * after probing for drives, to allocate partition tables and other data
508 * structures needed for the routines in genhd.c. ide_geninit() gets called
509 * somewhat later, during the partition check.
511 static void init_gendisk (ide_hwif_t *hwif)
513 struct gendisk *gd;
514 unsigned int unit, units, minors;
515 int *bs;
517 /* figure out maximum drive number on the interface */
518 for (units = MAX_DRIVES; units > 0; --units) {
519 if (hwif->drives[units-1].present)
520 break;
522 minors = units * (1<<PARTN_BITS);
523 gd = kmalloc (sizeof(struct gendisk), GFP_KERNEL);
524 gd->sizes = kmalloc (minors * sizeof(int), GFP_KERNEL);
525 gd->part = kmalloc (minors * sizeof(struct hd_struct), GFP_KERNEL);
526 bs = kmalloc (minors*sizeof(int), GFP_KERNEL);
528 /* cdroms and msdos f/s are examples of non-1024 blocksizes */
529 blksize_size[hwif->major] = bs;
530 for (unit = 0; unit < minors; ++unit)
531 *bs++ = BLOCK_SIZE;
533 for (unit = 0; unit < units; ++unit)
534 hwif->drives[unit].part = &gd->part[unit << PARTN_BITS];
536 gd->major = hwif->major; /* our major device number */
537 gd->major_name = IDE_MAJOR_NAME; /* treated special in genhd.c */
538 gd->minor_shift = PARTN_BITS; /* num bits for partitions */
539 gd->max_p = 1<<PARTN_BITS; /* 1 + max partitions / drive */
540 gd->max_nr = units; /* max num real drives */
541 gd->nr_real = units; /* current num real drives */
542 gd->init = ide_geninit; /* initialization function */
543 gd->real_devices= hwif; /* ptr to internal data */
545 gd->next = gendisk_head; /* link new major into list */
546 hwif->gd = gendisk_head = gd;
549 static void unexpected_intr (int, ide_hwgroup_t *);
551 * reset_ihandler() is a dummy interrupt handler which we install during
552 * an ide interface reset operation. This prevents other devices in the
553 * same hwgroup from being serviced while we play around with shared resources.
554 * If it ever gets invoked, we call unexpected_intr(), which treats the event
555 * the same as a timer_expiry().
557 static void reset_ihandler (ide_drive_t *drive)
559 unsigned long flags;
561 save_flags(flags);
562 cli();
563 unexpected_intr (HWIF(drive)->irq, HWGROUP(drive));
564 restore_flags(flags);
568 * start_reset_timer() sets up a timer event for 50ms in the future,
569 * to poll for completion of an ide reset operation (no interrupt to help us).
571 static void start_reset_timer (ide_hwif_t *hwif)
573 ide_hwgroup_t *hwgroup = hwif->hwgroup;
575 hwgroup->reset_timeout = jiffies + WAIT_WORSTCASE; /* max waiting time */
576 hwgroup->handler = &reset_ihandler; /* dummy irq handler */
577 hwgroup->timer.expires = jiffies + (HZ/20); /* polling interval */
578 add_timer(&(hwgroup->timer));
581 #ifdef CONFIG_BLK_DEV_IDECD
583 * atapi_reset_handler() gets invoked to poll the interface for completion every 50ms
584 * during an atapi drive reset operation. If the drive has not yet responded,
585 * and we have not yet hit our maximum waiting time, then the timer is restarted
586 * for another 50ms.
588 * Returns 1 if waiting for another 50ms, returns 0 otherwise.
590 static int atapi_reset_handler (ide_hwgroup_t *hwgroup)
592 ide_hwif_t *hwif = hwgroup->hwif;
593 ide_drive_t *drive = hwgroup->drive;
594 byte stat;
596 OUT_BYTE (drive->select.all, IDE_SELECT_REG);
597 udelay (10);
599 if (!OK_STAT(stat=GET_STAT(), 0, BUSY_STAT)) {
600 if (jiffies < hwgroup->reset_timeout) {
601 start_reset_timer (hwif);
602 return 1;
604 printk("%s: ATAPI reset timed-out, status=0x%02x\n", drive->name, stat);
605 return ide_do_reset (drive); /* do it the old fashioned way */
607 hwgroup->doing_atapi_reset = 0;
608 hwgroup->handler = NULL; /* allow new requests to be processed */
609 hwgroup->reset_timeout = 0; /* signal end of ide reset operation */
610 printk("%s: ATAPI reset complete\n", drive->name);
611 return 0;
613 #endif /* CONFIG_BLK_DEV_IDECD */
616 * reset_handler() gets invoked to poll the interface for completion every 50ms
617 * during an ide reset operation. If the drives have not yet responded,
618 * and we have not yet hit our maximum waiting time, then the timer is restarted
619 * for another 50ms.
621 * Returns 1 if waiting for another 50ms, returns 0 otherwise.
623 static int reset_handler (ide_hwgroup_t *hwgroup)
625 ide_hwif_t *hwif = hwgroup->hwif;
626 ide_drive_t *drive = hwgroup->drive;
627 byte tmp;
629 #ifdef CONFIG_BLK_DEV_IDECD
630 if (hwgroup->doing_atapi_reset)
631 return atapi_reset_handler(hwgroup);
632 #endif /* CONFIG_BLK_DEV_IDECD */
634 if (!OK_STAT(tmp=GET_STAT(), 0, BUSY_STAT)) {
635 if (jiffies < hwgroup->reset_timeout) {
636 start_reset_timer (hwif);
637 return 1;
639 printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
640 } else {
641 printk("%s: reset: ", hwif->name);
642 if ((tmp = GET_ERR()) == 1)
643 printk("success\n");
644 else {
645 printk("master: ");
646 switch (tmp & 0x7f) {
647 case 1: printk("passed");
648 break;
649 case 2: printk("formatter device error");
650 break;
651 case 3: printk("sector buffer error");
652 break;
653 case 4: printk("ECC circuitry error");
654 break;
655 case 5: printk("controlling MPU error");
656 break;
657 default:printk("error (0x%02x?)", tmp);
659 if (tmp & 0x80)
660 printk("; slave: failed");
661 printk("\n");
664 hwgroup->handler = NULL; /* allow new requests to be processed */
665 hwgroup->reset_timeout = 0; /* signal end of ide reset operation */
666 return 0;
670 * ide_do_reset() attempts to recover a confused drive by resetting it.
671 * Unfortunately, resetting a disk drive actually resets all devices on
672 * the same interface, so it can really be thought of as resetting the
673 * interface rather than resetting the drive.
675 * ATAPI devices have their own reset mechanism which allows them to be
676 * individually reset without clobbering other devices on the same interface.
678 * Unfortunately, the IDE interface does not generate an interrupt to let
679 * us know when the reset operation has finished, so we must poll for this.
680 * Equally poor, though, is the fact that this may a very long time to complete,
681 * (up to 30 seconds worstcase). So, instead of busy-waiting here for it,
682 * we set a timer to poll at 50ms intervals.
684 int ide_do_reset (ide_drive_t *drive)
686 unsigned int unit;
687 unsigned long flags;
688 ide_hwif_t *hwif = HWIF(drive);
689 ide_hwgroup_t *hwgroup = HWGROUP(drive);
691 save_flags(flags);
692 cli(); /* Why ? */
694 #ifdef CONFIG_BLK_DEV_IDECD
695 /* For an ATAPI device, first try an ATAPI SRST. */
696 if (drive->media == cdrom) {
697 if (!hwgroup->doing_atapi_reset) {
698 hwgroup->doing_atapi_reset = 1;
699 if (!drive->keep_settings)
700 drive->unmask = 0;
701 OUT_BYTE (drive->select.all, IDE_SELECT_REG);
702 udelay (20);
703 OUT_BYTE (WIN_SRST, IDE_COMMAND_REG);
704 hwgroup->reset_timeout = jiffies + WAIT_WORSTCASE;
705 start_reset_timer (hwif); /* begin periodic polling */
706 restore_flags (flags);
707 return 1;
710 hwgroup->doing_atapi_reset = 0;
711 #endif /* CONFIG_BLK_DEV_IDECD */
714 * First, reset any device state data we were maintaining
715 * for any of the drives on this interface.
717 for (unit = 0; unit < MAX_DRIVES; ++unit) {
718 ide_drive_t *rdrive = &hwif->drives[unit];
719 rdrive->special.b.set_geometry = 1;
720 rdrive->special.b.recalibrate = 1;
721 rdrive->special.b.set_multmode = 0;
722 if (OK_TO_RESET_CONTROLLER)
723 rdrive->mult_count = 0;
724 if (!rdrive->keep_settings) {
725 rdrive->mult_req = 0;
726 rdrive->unmask = 0;
728 if (rdrive->mult_req != rdrive->mult_count)
729 rdrive->special.b.set_multmode = 1;
732 #if OK_TO_RESET_CONTROLLER
734 * Note that we also set nIEN while resetting the device,
735 * to mask unwanted interrupts from the interface during the reset.
736 * However, due to the design of PC hardware, this will cause an
737 * immediate interrupt due to the edge transition it produces.
738 * This single interrupt gives us a "fast poll" for drives that
739 * recover from reset very quickly, saving us the first 50ms wait time.
741 OUT_BYTE(drive->ctl|6,IDE_CONTROL_REG); /* set SRST and nIEN */
742 udelay(5); /* more than enough time */
743 OUT_BYTE(drive->ctl|2,IDE_CONTROL_REG); /* clear SRST, leave nIEN */
744 hwgroup->reset_timeout = jiffies + WAIT_WORSTCASE;
745 start_reset_timer (hwif); /* begin periodic polling */
746 #endif /* OK_TO_RESET_CONTROLLER */
748 restore_flags (flags);
749 return OK_TO_RESET_CONTROLLER; /* 1 = we are waiting, 0 = done */
753 * Clean up after success/failure of an explicit (ioctl) drive cmd
755 static void end_drive_cmd (ide_drive_t *drive, byte stat, byte err)
757 unsigned long flags;
758 struct request *rq = HWGROUP(drive)->rq;
759 byte *args = (byte *) rq->buffer;
761 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
762 if (args) {
763 args[0] = stat;
764 args[1] = err;
765 args[2] = IN_BYTE(IDE_NSECTOR_REG);
767 save_flags(flags);
768 cli();
769 up(rq->sem);
770 HWGROUP(drive)->rq = NULL;
771 restore_flags(flags);
775 * Error reporting, in human readable form (luxurious, but a memory hog).
777 byte ide_dump_status (ide_drive_t *drive, const char *msg, byte stat)
779 unsigned long flags;
780 byte err = 0;
782 save_flags (flags);
783 sti();
784 printk("%s: %s: status=0x%02x", drive->name, msg, stat);
785 #if FANCY_STATUS_DUMPS
786 if (drive->media == disk) {
787 printk(" { ");
788 if (stat & BUSY_STAT)
789 printk("Busy ");
790 else {
791 if (stat & READY_STAT) printk("DriveReady ");
792 if (stat & WRERR_STAT) printk("DeviceFault ");
793 if (stat & SEEK_STAT) printk("SeekComplete ");
794 if (stat & DRQ_STAT) printk("DataRequest ");
795 if (stat & ECC_STAT) printk("CorrectedError ");
796 if (stat & INDEX_STAT) printk("Index ");
797 if (stat & ERR_STAT) printk("Error ");
799 printk("}");
801 #endif /* FANCY_STATUS_DUMPS */
802 printk("\n");
803 if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
804 err = GET_ERR();
805 printk("%s: %s: error=0x%02x", drive->name, msg, err);
806 #if FANCY_STATUS_DUMPS
807 if (drive->media == disk) {
808 printk(" { ");
809 if (err & BBD_ERR) printk("BadSector ");
810 if (err & ECC_ERR) printk("UncorrectableError ");
811 if (err & ID_ERR) printk("SectorIdNotFound ");
812 if (err & ABRT_ERR) printk("DriveStatusError ");
813 if (err & TRK0_ERR) printk("TrackZeroNotFound ");
814 if (err & MARK_ERR) printk("AddrMarkNotFound ");
815 printk("}");
816 if (err & (BBD_ERR|ECC_ERR|ID_ERR|MARK_ERR)) {
817 byte cur = IN_BYTE(IDE_SELECT_REG);
818 if (cur & 0x40) { /* using LBA? */
819 printk(", LBAsect=%ld", (unsigned long)
820 ((cur&0xf)<<24)
821 |(IN_BYTE(IDE_HCYL_REG)<<16)
822 |(IN_BYTE(IDE_LCYL_REG)<<8)
823 | IN_BYTE(IDE_SECTOR_REG));
824 } else {
825 printk(", CHS=%d/%d/%d",
826 (IN_BYTE(IDE_HCYL_REG)<<8) +
827 IN_BYTE(IDE_LCYL_REG),
828 cur & 0xf,
829 IN_BYTE(IDE_SECTOR_REG));
831 if (HWGROUP(drive)->rq)
832 printk(", sector=%ld", HWGROUP(drive)->rq->sector);
835 #endif /* FANCY_STATUS_DUMPS */
836 printk("\n");
838 restore_flags (flags);
839 return err;
843 * try_to_flush_leftover_data() is invoked in response to a drive
844 * unexpectedly having its DRQ_STAT bit set. As an alternative to
845 * resetting the drive, this routine tries to clear the condition
846 * by read a sector's worth of data from the drive. Of course,
847 * this may not help if the drive is *waiting* for data from *us*.
849 static void try_to_flush_leftover_data (ide_drive_t *drive)
851 int i = (drive->mult_count ? drive->mult_count : 1) * SECTOR_WORDS;
853 while (i > 0) {
854 unsigned long buffer[16];
855 unsigned int wcount = (i > 16) ? 16 : i;
856 i -= wcount;
857 ide_input_data (drive, buffer, wcount);
862 * ide_error() takes action based on the error returned by the controller.
864 * Returns 1 if an ide reset operation has been initiated, in which case
865 * the caller MUST simply return from the driver (through however many levels).
866 * Returns 0 otherwise.
868 int ide_error (ide_drive_t *drive, const char *msg, byte stat)
870 struct request *rq;
871 byte err;
873 err = ide_dump_status(drive, msg, stat);
874 if ((rq = HWGROUP(drive)->rq) == NULL || drive == NULL)
875 return 0;
876 if (rq->cmd == IDE_DRIVE_CMD) { /* never retry an explicit DRIVE_CMD */
877 end_drive_cmd(drive, stat, err);
878 return 0;
880 if (stat & BUSY_STAT) { /* other bits are useless when BUSY */
881 rq->errors |= ERROR_RESET;
882 } else {
883 if (drive->media == disk && (stat & ERR_STAT)) {
884 /* err has different meaning on cdrom */
885 if (err & BBD_ERR) /* retries won't help this! */
886 rq->errors = ERROR_MAX;
887 else if (err & TRK0_ERR) /* help it find track zero */
888 rq->errors |= ERROR_RECAL;
890 if ((stat & DRQ_STAT) && rq->cmd != WRITE)
891 try_to_flush_leftover_data(drive);
893 if (GET_STAT() & (BUSY_STAT|DRQ_STAT))
894 rq->errors |= ERROR_RESET; /* Mmmm.. timing problem */
896 #ifdef CONFIG_BLK_DEV_TRITON
897 if (rq->errors > 3 && drive->using_dma) { /* DMA troubles? */
898 drive->using_dma = 0;
899 printk("%s: DMA disabled\n", drive->name);
900 --rq->errors;
901 return 0;
903 #endif /* CONFIG_BLK_DEV_TRITON */
904 if (rq->errors >= ERROR_MAX)
905 ide_end_request(0, HWGROUP(drive));
906 else {
907 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
908 ++rq->errors;
909 return ide_do_reset(drive);
910 } else if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
911 drive->special.b.recalibrate = 1;
912 ++rq->errors;
914 return 0;
918 * read_intr() is the handler for disk read/multread interrupts
920 static void read_intr (ide_drive_t *drive)
922 byte stat;
923 int i;
924 unsigned int msect, nsect;
925 struct request *rq;
927 if (!OK_STAT(stat=GET_STAT(),DATA_READY,BAD_R_STAT)) {
928 sti();
929 if (!ide_error(drive, "read_intr", stat))
930 IDE_DO_REQUEST;
931 return;
933 msect = drive->mult_count;
934 read_next:
935 rq = HWGROUP(drive)->rq;
936 if (msect) {
937 if ((nsect = rq->current_nr_sectors) > msect)
938 nsect = msect;
939 msect -= nsect;
940 } else
941 nsect = 1;
942 ide_input_data(drive, rq->buffer, nsect * SECTOR_WORDS);
943 #ifdef DEBUG
944 printk("%s: read: sectors(%ld-%ld), buffer=0x%08lx, remaining=%ld\n",
945 drive->name, rq->sector, rq->sector+nsect-1,
946 (unsigned long) rq->buffer+(nsect<<9), rq->nr_sectors-nsect);
947 #endif
948 rq->sector += nsect;
949 rq->buffer += nsect<<9;
950 rq->errors = 0;
951 i = (rq->nr_sectors -= nsect);
952 if ((rq->current_nr_sectors -= nsect) <= 0)
953 ide_end_request(1, HWGROUP(drive));
954 if (i > 0) {
955 if (msect)
956 goto read_next;
957 ide_set_handler (drive, &read_intr);
958 return;
960 IDE_DO_REQUEST;
964 * write_intr() is the handler for disk write interrupts
966 static void write_intr (ide_drive_t *drive)
968 byte stat;
969 int i;
970 struct request *rq = HWGROUP(drive)->rq;
972 if (OK_STAT(stat=GET_STAT(),DRIVE_READY,drive->bad_wstat)) {
973 #ifdef DEBUG
974 printk("%s: write: sector %ld, buffer=0x%08lx, remaining=%ld\n",
975 drive->name, rq->sector, (unsigned long) rq->buffer,
976 rq->nr_sectors-1);
977 #endif
978 if ((rq->nr_sectors == 1) ^ ((stat & DRQ_STAT) != 0)) {
979 rq->sector++;
980 rq->buffer += 512;
981 rq->errors = 0;
982 i = --rq->nr_sectors;
983 --rq->current_nr_sectors;
984 if (rq->current_nr_sectors <= 0)
985 ide_end_request(1, HWGROUP(drive));
986 if (i > 0) {
987 ide_output_data (drive, rq->buffer, SECTOR_WORDS);
988 ide_set_handler (drive, &write_intr);
989 return;
991 IDE_DO_REQUEST;
992 return;
995 sti();
996 if (!ide_error(drive, "write_intr", stat))
997 IDE_DO_REQUEST;
1001 * multwrite() transfers a block of one or more sectors of data to a drive
1002 * as part of a disk multwrite operation.
1004 static void multwrite (ide_drive_t *drive)
1006 struct request *rq = &HWGROUP(drive)->wrq;
1007 unsigned int mcount = drive->mult_count;
1009 do {
1010 unsigned int nsect = rq->current_nr_sectors;
1011 if (nsect > mcount)
1012 nsect = mcount;
1013 mcount -= nsect;
1015 ide_output_data(drive, rq->buffer, nsect<<7);
1016 #ifdef DEBUG
1017 printk("%s: multwrite: sector %ld, buffer=0x%08lx, count=%d, remaining=%ld\n",
1018 drive->name, rq->sector, (unsigned long) rq->buffer,
1019 nsect, rq->nr_sectors - nsect);
1020 #endif
1021 if ((rq->nr_sectors -= nsect) <= 0)
1022 break;
1023 if ((rq->current_nr_sectors -= nsect) == 0) {
1024 if ((rq->bh = rq->bh->b_reqnext) != NULL) {
1025 rq->current_nr_sectors = rq->bh->b_size>>9;
1026 rq->buffer = rq->bh->b_data;
1027 } else {
1028 panic("%s: buffer list corrupted\n", drive->name);
1029 break;
1031 } else {
1032 rq->buffer += nsect << 9;
1034 } while (mcount);
1038 * write_intr() is the handler for disk multwrite interrupts
1040 static void multwrite_intr (ide_drive_t *drive)
1042 byte stat;
1043 int i;
1044 struct request *rq = &HWGROUP(drive)->wrq;
1046 if (OK_STAT(stat=GET_STAT(),DRIVE_READY,drive->bad_wstat)) {
1047 if (stat & DRQ_STAT) {
1048 if (rq->nr_sectors) {
1049 multwrite(drive);
1050 ide_set_handler (drive, &multwrite_intr);
1051 return;
1053 } else {
1054 if (!rq->nr_sectors) { /* all done? */
1055 rq = HWGROUP(drive)->rq;
1056 for (i = rq->nr_sectors; i > 0;){
1057 i -= rq->current_nr_sectors;
1058 ide_end_request(1, HWGROUP(drive));
1060 IDE_DO_REQUEST;
1061 return;
1065 sti();
1066 if (!ide_error(drive, "multwrite_intr", stat))
1067 IDE_DO_REQUEST;
1071 * Issue a simple drive command
1072 * The drive must be selected beforehand.
1074 static void ide_cmd(ide_drive_t *drive, byte cmd, byte nsect, ide_handler_t *handler)
1076 ide_set_handler (drive, handler);
1077 OUT_BYTE(drive->ctl,IDE_CONTROL_REG);
1078 OUT_BYTE(nsect,IDE_NSECTOR_REG);
1079 OUT_BYTE(cmd,IDE_COMMAND_REG);
1083 * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
1085 static void set_multmode_intr (ide_drive_t *drive)
1087 byte stat = GET_STAT();
1089 sti();
1090 if (OK_STAT(stat,READY_STAT,BAD_STAT)) {
1091 drive->mult_count = drive->mult_req;
1092 } else {
1093 drive->mult_req = drive->mult_count = 0;
1094 drive->special.b.recalibrate = 1;
1095 (void) ide_dump_status(drive, "set_multmode", stat);
1097 IDE_DO_REQUEST;
1101 * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
1103 static void set_geometry_intr (ide_drive_t *drive)
1105 byte stat = GET_STAT();
1107 sti();
1108 if (!OK_STAT(stat,READY_STAT,BAD_STAT))
1109 if (ide_error(drive, "set_geometry_intr", stat))
1110 return;
1111 IDE_DO_REQUEST;
1115 * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
1117 static void recal_intr (ide_drive_t *drive)
1119 byte stat = GET_STAT();
1121 sti();
1122 if (!OK_STAT(stat,READY_STAT,BAD_STAT))
1123 if (ide_error(drive, "recal_intr", stat))
1124 return;
1125 IDE_DO_REQUEST;
1129 * drive_cmd_intr() is invoked on completion of a special DRIVE_CMD.
1131 static void drive_cmd_intr (ide_drive_t *drive)
1133 byte stat = GET_STAT();
1135 sti();
1136 if (OK_STAT(stat,READY_STAT,BAD_STAT))
1137 end_drive_cmd (drive, stat, GET_ERR());
1138 else if (ide_error(drive, "drive_cmd", stat)) /* calls end_drive_cmd */
1139 return;
1140 IDE_DO_REQUEST;
1144 * do_special() is used to issue WIN_SPECIFY, WIN_RESTORE, and WIN_SETMULT
1145 * commands to a drive. It used to do much more, but has been scaled back
1146 * in recent updates, and could be completely eliminated with a bit more effort.
1148 static inline void do_special (ide_drive_t *drive)
1150 special_t *s = &drive->special;
1151 #ifdef DEBUG
1152 printk("%s: do_special: 0x%02x\n", drive->name, s->all);
1153 #endif
1154 if (s->b.set_geometry) {
1155 s->b.set_geometry = 0;
1156 if (drive->media == disk) {
1157 OUT_BYTE(drive->sect,IDE_SECTOR_REG);
1158 OUT_BYTE(drive->cyl,IDE_LCYL_REG);
1159 OUT_BYTE(drive->cyl>>8,IDE_HCYL_REG);
1160 OUT_BYTE(((drive->head-1)|drive->select.all)&0xBF,IDE_SELECT_REG);
1161 ide_cmd(drive, WIN_SPECIFY, drive->sect, &set_geometry_intr);
1163 } else if (s->b.recalibrate) {
1164 s->b.recalibrate = 0;
1165 if (drive->media == disk) {
1166 ide_cmd(drive, WIN_RESTORE, drive->sect, &recal_intr);
1168 } else if (s->b.set_multmode) {
1169 s->b.set_multmode = 0;
1170 if (drive->media == disk) {
1171 if (drive->id && drive->mult_req > drive->id->max_multsect)
1172 drive->mult_req = drive->id->max_multsect;
1173 ide_cmd(drive, WIN_SETMULT, drive->mult_req, &set_multmode_intr);
1174 } else
1175 drive->mult_req = 0;
1176 } else if (s->all) {
1177 s->all = 0;
1178 printk("%s: bad special flag: 0x%02x\n", drive->name, s->all);
1183 * This routine busy-waits for the drive status to be not "busy".
1184 * It then checks the status for all of the "good" bits and none
1185 * of the "bad" bits, and if all is okay it returns 0. All other
1186 * cases return 1 after invoking ide_error()
1188 * This routine should get fixed to not hog the cpu during extra long waits..
1189 * That could be done by busy-waiting for the first jiffy or two, and then
1190 * setting a timer to wake up at half second intervals thereafter,
1191 * until WAIT_WORSTCASE is achieved, before timing out.
1193 int ide_wait_stat (ide_drive_t *drive, byte good, byte bad, unsigned long timeout)
1195 byte stat;
1196 unsigned long flags;
1197 test:
1198 udelay(1); /* spec allows drive 400ns to change "BUSY" */
1199 if (OK_STAT((stat = GET_STAT()), good, bad))
1200 return 0; /* fast exit for most frequent case */
1201 if (!(stat & BUSY_STAT)) {
1202 (void) ide_error(drive, "status error", stat);
1203 return 1;
1206 save_flags(flags);
1207 sti();
1208 timeout += jiffies;
1209 do {
1210 if (!((stat = GET_STAT()) & BUSY_STAT)) {
1211 restore_flags(flags);
1212 goto test;
1214 } while (jiffies <= timeout);
1216 restore_flags(flags);
1217 (void) ide_error(drive, "status timeout", GET_STAT());
1218 return 1;
1222 * do_rw_disk() issues WIN_{MULT}READ and WIN_{MULT}WRITE commands to a disk,
1223 * using LBA if supported, or CHS otherwise, to address sectors. It also takes
1224 * care of issuing special DRIVE_CMDs.
1226 static inline void do_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
1228 unsigned short io_base = HWIF(drive)->io_base;
1230 OUT_BYTE(drive->ctl,IDE_CONTROL_REG);
1231 OUT_BYTE(rq->nr_sectors,io_base+IDE_NSECTOR_OFFSET);
1232 if (drive->select.b.lba) {
1233 #ifdef DEBUG
1234 printk("%s: %sing: LBAsect=%ld, sectors=%ld, buffer=0x%08lx\n",
1235 drive->name, (rq->cmd==READ)?"read":"writ",
1236 block, rq->nr_sectors, (unsigned long) rq->buffer);
1237 #endif
1238 OUT_BYTE(block,io_base+IDE_SECTOR_OFFSET);
1239 OUT_BYTE(block>>=8,io_base+IDE_LCYL_OFFSET);
1240 OUT_BYTE(block>>=8,io_base+IDE_HCYL_OFFSET);
1241 OUT_BYTE(((block>>8)&0x0f)|drive->select.all,io_base+IDE_SELECT_OFFSET);
1242 } else {
1243 unsigned int sect,head,cyl,track;
1244 track = block / drive->sect;
1245 sect = block % drive->sect + 1;
1246 OUT_BYTE(sect,io_base+IDE_SECTOR_OFFSET);
1247 head = track % drive->head;
1248 cyl = track / drive->head;
1249 OUT_BYTE(cyl,io_base+IDE_LCYL_OFFSET);
1250 OUT_BYTE(cyl>>8,io_base+IDE_HCYL_OFFSET);
1251 OUT_BYTE(head|drive->select.all,io_base+IDE_SELECT_OFFSET);
1252 #ifdef DEBUG
1253 printk("%s: %sing: CHS=%d/%d/%d, sectors=%ld, buffer=0x%08lx\n",
1254 drive->name, (rq->cmd==READ)?"read":"writ", cyl,
1255 head, sect, rq->nr_sectors, (unsigned long) rq->buffer);
1256 #endif
1258 if (rq->cmd == READ) {
1259 #ifdef CONFIG_BLK_DEV_TRITON
1260 if (drive->using_dma && !(HWIF(drive)->dmaproc(ide_dma_read, drive)))
1261 return;
1262 #endif /* CONFIG_BLK_DEV_TRITON */
1263 ide_set_handler(drive, &read_intr);
1264 OUT_BYTE(drive->mult_count ? WIN_MULTREAD : WIN_READ, io_base+IDE_COMMAND_OFFSET);
1265 return;
1267 if (rq->cmd == WRITE) {
1268 #ifdef CONFIG_BLK_DEV_TRITON
1269 if (drive->using_dma && !(HWIF(drive)->dmaproc(ide_dma_write, drive)))
1270 return;
1271 #endif /* CONFIG_BLK_DEV_TRITON */
1272 OUT_BYTE(drive->mult_count ? WIN_MULTWRITE : WIN_WRITE, io_base+IDE_COMMAND_OFFSET);
1273 if (ide_wait_stat(drive, DATA_READY, drive->bad_wstat, WAIT_DRQ)) {
1274 printk("%s: no DRQ after issuing %s\n", drive->name,
1275 drive->mult_count ? "MULTWRITE" : "WRITE");
1276 return;
1278 if (!drive->unmask)
1279 cli();
1280 if (drive->mult_count) {
1281 HWGROUP(drive)->wrq = *rq; /* scratchpad */
1282 ide_set_handler (drive, &multwrite_intr);
1283 multwrite(drive);
1284 } else {
1285 ide_set_handler (drive, &write_intr);
1286 ide_output_data(drive, rq->buffer, SECTOR_WORDS);
1288 return;
1290 if (rq->cmd == IDE_DRIVE_CMD) {
1291 byte *args = rq->buffer;
1292 if (args) {
1293 printk("%s: DRIVE_CMD cmd=0x%02x sc=0x%02x fr=0x%02x\n",
1294 drive->name, args[0], args[1], args[2]);
1295 OUT_BYTE(args[2],io_base+IDE_FEATURE_OFFSET);
1296 ide_cmd(drive, args[0], args[1], &drive_cmd_intr);
1297 return;
1298 } else {
1300 * NULL is actually a valid way of waiting for
1301 * all current requests to be flushed from the queue.
1303 #ifdef DEBUG
1304 printk("%s: DRIVE_CMD (null)\n", drive->name);
1305 #endif
1306 end_drive_cmd(drive, GET_STAT(), GET_ERR());
1307 return;
1310 printk("%s: bad command: %d\n", drive->name, rq->cmd);
1311 ide_end_request(0, HWGROUP(drive));
1315 * do_request() initiates handling of a new I/O request
1317 static inline void do_request (ide_hwif_t *hwif, struct request *rq)
1319 unsigned int minor, unit;
1320 unsigned long block, blockend;
1321 ide_drive_t *drive;
1323 sti();
1324 #ifdef DEBUG
1325 printk("%s: ide_do_request: current=0x%08lx\n", hwif->name, (unsigned long) rq);
1326 #endif
1327 minor = MINOR(rq->rq_dev);
1328 unit = minor >> PARTN_BITS;
1329 if (MAJOR(rq->rq_dev) != hwif->major || unit >= MAX_DRIVES) {
1330 printk("%s: bad device number: %s\n",
1331 hwif->name, kdevname(rq->rq_dev));
1332 goto kill_rq;
1334 drive = &hwif->drives[unit];
1335 #ifdef DEBUG
1336 if (rq->bh && !rq->bh->b_lock) {
1337 printk("%s: block not locked\n", drive->name);
1338 goto kill_rq;
1340 #endif
1341 block = rq->sector;
1342 blockend = block + rq->nr_sectors;
1343 if ((blockend < block) || (blockend > drive->part[minor&PARTN_MASK].nr_sects)) {
1344 printk("%s%c: bad access: block=%ld, count=%ld\n", drive->name,
1345 (minor&PARTN_MASK)?'0'+(minor&PARTN_MASK):' ', block, rq->nr_sectors);
1346 goto kill_rq;
1348 block += drive->part[minor&PARTN_MASK].start_sect + drive->sect0;
1349 #if FAKE_FDISK_FOR_EZDRIVE
1350 if (block == 0 && drive->ezdrive) {
1351 block = 1;
1352 printk("%s: [EZD] accessing sector 1 instead of sector 0\n", drive->name);
1354 #endif /* FAKE_FDISK_FOR_EZDRIVE */
1355 ((ide_hwgroup_t *)hwif->hwgroup)->drive = drive;
1356 #if (DISK_RECOVERY_TIME > 0)
1357 while ((read_timer() - hwif->last_time) < DISK_RECOVERY_TIME);
1358 #endif
1359 #if SUPPORT_HT6560B
1360 if (hwif->select)
1361 ide_hwif_select (hwif);
1362 #endif
1363 OUT_BYTE(drive->select.all,IDE_SELECT_REG);
1364 if (ide_wait_stat(drive, drive->ready_stat, BUSY_STAT|DRQ_STAT, WAIT_READY)) {
1365 printk("%s: drive not ready for command\n", drive->name);
1366 return;
1369 if (!drive->special.all) {
1370 #ifdef CONFIG_BLK_DEV_IDECD
1371 switch (drive->media) {
1372 case disk:
1373 do_rw_disk (drive, rq, block);
1374 return;
1375 case cdrom:
1376 ide_do_rw_cdrom (drive, block);
1377 return;
1378 default:
1379 printk("%s: media type %d not supported\n",
1380 drive->name, drive->media);
1381 goto kill_rq;
1383 #else
1384 do_rw_disk (drive, rq, block); /* simpler and faster */
1385 return;
1386 #endif;
1388 do_special(drive);
1389 return;
1390 kill_rq:
1391 ide_end_request(0, hwif->hwgroup);
1395 * The driver enables interrupts as much as possible. In order to do this,
1396 * (a) the device-interrupt is always masked before entry, and
1397 * (b) the timeout-interrupt is always disabled before entry.
1399 * If we enter here from, say irq14, and then start a new request for irq15,
1400 * (possible with "serialize" option) then we cannot ensure that we exit
1401 * before the irq15 hits us. So, we must be careful not to let this bother us.
1403 * Interrupts are still masked (by default) whenever we are exchanging
1404 * data/cmds with a drive, because some drives seem to have very poor
1405 * tolerance for latency during I/O. For devices which don't suffer from
1406 * this problem (most don't), the unmask flag can be set using the "hdparm"
1407 * utility, to permit other interrupts during data/cmd transfers.
1409 void ide_do_request (ide_hwgroup_t *hwgroup)
1411 cli(); /* paranoia */
1412 if (hwgroup->handler != NULL) {
1413 printk("%s: EEeekk!! handler not NULL in ide_do_request()\n", hwgroup->hwif->name);
1414 return;
1416 do {
1417 ide_hwif_t *hwif = hwgroup->hwif;
1418 struct request *rq;
1419 if ((rq = hwgroup->rq) == NULL) {
1420 hwgroup->drive = NULL; /* paranoia */
1421 do {
1422 rq = blk_dev[hwif->major].current_request;
1423 if (rq != NULL && rq->rq_status != RQ_INACTIVE)
1424 goto got_rq;
1425 } while ((hwif = hwif->next) != hwgroup->hwif);
1426 return; /* no work left for this hwgroup */
1427 got_rq:
1428 blk_dev[hwif->major].current_request = rq->next;
1430 do_request(hwgroup->hwif = hwif, hwgroup->rq = rq);
1431 cli();
1432 } while (hwgroup->handler == NULL);
1436 * do_hwgroup_request() invokes ide_do_request() after first masking
1437 * all possible interrupts for the current hwgroup. This prevents race
1438 * conditions in the event that an unexpected interrupt occurs while
1439 * we are in the driver.
1441 * Note that when an interrupt is used to reenter the driver, the first level
1442 * handler will already have masked the irq that triggered, but any other ones
1443 * for the hwgroup will still be unmasked. The driver tries to be careful
1444 * about such things.
1446 static void do_hwgroup_request (ide_hwgroup_t *hwgroup)
1448 if (hwgroup->handler == NULL) {
1449 ide_hwif_t *hgif = hwgroup->hwif;
1450 ide_hwif_t *hwif = hgif;
1451 do {
1452 disable_irq(hwif->irq);
1453 } while ((hwif = hwif->next) != hgif);
1454 ide_do_request (hwgroup);
1455 do {
1456 enable_irq(hwif->irq);
1457 } while ((hwif = hwif->next) != hgif);
1461 static void do_ide0_request (void) /* invoked with cli() */
1463 do_hwgroup_request (ide_hwifs[0].hwgroup);
1466 static void do_ide1_request (void) /* invoked with cli() */
1468 do_hwgroup_request (ide_hwifs[1].hwgroup);
1471 static void do_ide2_request (void) /* invoked with cli() */
1473 do_hwgroup_request (ide_hwifs[2].hwgroup);
1476 static void do_ide3_request (void) /* invoked with cli() */
1478 do_hwgroup_request (ide_hwifs[3].hwgroup);
1481 static void timer_expiry (unsigned long data)
1483 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *) data;
1484 ide_drive_t *drive = hwgroup->drive;
1485 unsigned long flags;
1487 save_flags(flags);
1488 cli();
1490 if (hwgroup->reset_timeout != 0) { /* ide reset in progress? */
1491 if (!reset_handler(hwgroup))
1492 do_hwgroup_request (hwgroup);
1493 } else if (hwgroup->handler == NULL) { /* not waiting for anything? */
1494 sti(); /* drive must have responded just as the timer expired */
1495 printk("%s: marginal timeout\n", drive->name);
1496 } else { /* drive not responding */
1497 hwgroup->handler = NULL;
1498 if (hwgroup->hwif->dmaproc)
1499 (void) hwgroup->hwif->dmaproc (ide_dma_abort, drive);
1500 if (!ide_error(drive, "irq timeout", GET_STAT()))
1501 do_hwgroup_request (hwgroup);
1503 restore_flags(flags);
1507 * There's nothing really useful we can do with an unexpected interrupt,
1508 * other than reading the status register (to clear it), and logging it.
1509 * There should be no way that an irq can happen before we're ready for it,
1510 * so we needn't worry much about losing an "important" interrupt here.
1512 * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
1513 * drive enters "idle", "standby", or "sleep" mode, so if the status looks
1514 * "good", we just ignore the interrupt completely.
1516 * This routine assumes cli() is in effect when called.
1518 * If an unexpected interrupt happens on irq15 while we are handling irq14
1519 * and if the two interfaces are "serialized" (CMD640B), then it looks like
1520 * we could screw up by interfering with a new request being set up for irq15.
1522 * In reality, this is a non-issue. The new command is not sent unless the
1523 * drive is ready to accept one, in which case we know the drive is not
1524 * trying to interrupt us. And ide_set_handler() is always invoked before
1525 * completing the issuance of any new drive command, so we will not be
1526 * accidently invoked as a result of any valid command completion interrupt.
1529 static void unexpected_intr (int irq, ide_hwgroup_t *hwgroup)
1531 byte stat;
1532 unsigned int unit;
1533 ide_hwif_t *hwif = hwgroup->hwif;
1536 * check for ide reset in progress
1538 if (hwgroup->reset_timeout != 0) {
1539 if (!reset_handler(hwgroup))
1540 do_hwgroup_request (hwgroup);
1541 return;
1545 * handle the unexpected interrupt
1547 do {
1548 if (hwif->irq == irq) {
1549 #if SUPPORT_HT6560B
1550 if (hwif->select)
1551 ide_hwif_select (hwif);
1552 #endif
1553 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1554 ide_drive_t *drive = &hwif->drives[unit];
1555 if (!drive->present)
1556 continue;
1557 if (!OK_STAT(stat=GET_STAT(), drive->ready_stat, BAD_STAT))
1558 (void) ide_dump_status(drive, "unexpected_intr", stat);
1559 if ((stat & DRQ_STAT))
1560 try_to_flush_leftover_data(drive);
1563 } while ((hwif = hwif->next) != hwgroup->hwif);
1567 * entry point for all interrupts, caller does cli() for us
1569 static void ide_intr (int irq, struct pt_regs *regs)
1571 ide_hwgroup_t *hwgroup = irq_to_hwgroup[irq];
1572 ide_handler_t *handler;
1574 if (irq == hwgroup->hwif->irq && (handler = hwgroup->handler) != NULL) {
1575 ide_drive_t *drive = hwgroup->drive;
1576 hwgroup->handler = NULL;
1577 del_timer(&(hwgroup->timer));
1578 if (drive->unmask)
1579 sti();
1580 handler(drive);
1581 } else {
1582 unexpected_intr(irq, hwgroup);
1584 cli();
1588 * get_info_ptr() returns the (ide_drive_t *) for a given device number.
1589 * It returns NULL if the given device number does not match any present drives.
1591 static ide_drive_t *get_info_ptr (kdev_t i_rdev)
1593 int major = MAJOR(i_rdev);
1594 unsigned int h;
1596 for (h = 0; h < MAX_HWIFS; ++h) {
1597 ide_hwif_t *hwif = &ide_hwifs[h];
1598 if (hwif->present && major == hwif->major) {
1599 unsigned unit = DEVICE_NR(i_rdev);
1600 if (unit < MAX_DRIVES) {
1601 ide_drive_t *drive = &hwif->drives[unit];
1602 if (drive->present)
1603 return drive;
1604 } else if (major == IDE0_MAJOR && unit < 4) {
1605 printk("ide: probable bad entry for /dev/hd%c%d\n",
1606 'a' + unit, MINOR(i_rdev) & PARTN_MASK);
1607 printk("ide: to fix it, run: /usr/src/linux/drivers/block/MAKEDEV.ide\n");
1609 break;
1612 return NULL;
1616 * This function issues a specific IDE drive command onto the
1617 * tail of the request queue, and waits for it to be completed.
1618 * If arg is NULL, it goes through all the motions,
1619 * but without actually sending a command to the drive.
1621 int ide_do_drive_cmd(kdev_t rdev, char *args)
1623 unsigned long flags;
1624 unsigned int major = MAJOR(rdev);
1625 struct request rq, *cur_rq;
1626 struct blk_dev_struct *bdev;
1627 struct semaphore sem = MUTEX_LOCKED;
1629 /* build up a special request, and add it to the queue */
1630 rq.buffer = args;
1631 rq.cmd = IDE_DRIVE_CMD;
1632 rq.errors = 0;
1633 rq.sector = 0;
1634 rq.nr_sectors = 0;
1635 rq.current_nr_sectors = 0;
1636 rq.sem = &sem;
1637 rq.bh = NULL;
1638 rq.bhtail = NULL;
1639 rq.next = NULL;
1640 rq.rq_status = RQ_ACTIVE;
1641 rq.rq_dev = rdev;
1642 bdev = &blk_dev[major];
1644 save_flags(flags);
1645 cli();
1646 cur_rq = bdev->current_request;
1647 if (cur_rq == NULL) { /* empty request list? */
1648 bdev->current_request = &rq; /* service ours immediately */
1649 bdev->request_fn();
1650 } else {
1651 while (cur_rq->next != NULL) /* find end of request list */
1652 cur_rq = cur_rq->next;
1653 cur_rq->next = &rq; /* add rq to the end */
1656 down(&sem); /* wait for it to be serviced */
1657 restore_flags(flags);
1658 return rq.errors ? -EIO : 0; /* return -EIO if errors */
1661 static int ide_open(struct inode * inode, struct file * filp)
1663 ide_drive_t *drive;
1664 unsigned long flags;
1666 if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
1667 return -ENODEV;
1668 save_flags(flags);
1669 cli();
1670 while (drive->busy)
1671 sleep_on(&drive->wqueue);
1672 drive->usage++;
1673 restore_flags(flags);
1674 #ifdef CONFIG_BLK_DEV_IDECD
1675 if (drive->media == cdrom)
1676 return ide_cdrom_open (inode, filp, drive);
1677 #endif /* CONFIG_BLK_DEV_IDECD */
1678 if (drive->removeable) {
1679 byte door_lock[] = {WIN_DOORLOCK,0,0,0};
1680 check_disk_change(inode->i_rdev);
1681 ide_do_drive_cmd(inode->i_rdev, door_lock);
1683 return 0;
1687 * Releasing a block device means we sync() it, so that it can safely
1688 * be forgotten about...
1690 static void ide_release(struct inode * inode, struct file * file)
1692 ide_drive_t *drive;
1694 if ((drive = get_info_ptr(inode->i_rdev)) != NULL) {
1695 sync_dev(inode->i_rdev);
1696 drive->usage--;
1697 #ifdef CONFIG_BLK_DEV_IDECD
1698 if (drive->media == cdrom)
1699 ide_cdrom_release (inode, file, drive);
1700 else
1701 #endif /* CONFIG_BLK_DEV_IDECD */
1702 if (drive->removeable) {
1703 byte door_unlock[] = {WIN_DOORUNLOCK,0,0,0};
1704 invalidate_buffers(inode->i_rdev);
1705 ide_do_drive_cmd(inode->i_rdev, door_unlock);
1711 * This routine is called to flush all partitions and partition tables
1712 * for a changed disk, and then re-read the new partition table.
1713 * If we are revalidating a disk because of a media change, then we
1714 * enter with usage == 0. If we are using an ioctl, we automatically have
1715 * usage == 1 (we need an open channel to use an ioctl :-), so this
1716 * is our limit.
1718 static int revalidate_disk(kdev_t i_rdev)
1720 ide_drive_t *drive;
1721 unsigned int p, major, minor;
1722 long flags;
1724 if ((drive = get_info_ptr(i_rdev)) == NULL)
1725 return -ENODEV;
1727 major = MAJOR(i_rdev);
1728 minor = drive->select.b.unit << PARTN_BITS;
1729 save_flags(flags);
1730 cli();
1731 if (drive->busy || (drive->usage > 1)) {
1732 restore_flags(flags);
1733 return -EBUSY;
1735 drive->busy = 1;
1736 restore_flags(flags);
1738 for (p = 0; p < (1<<PARTN_BITS); ++p) {
1739 if (drive->part[p].nr_sects > 0) {
1740 kdev_t devp = MKDEV(major, minor+p);
1741 sync_dev (devp);
1742 invalidate_inodes (devp);
1743 invalidate_buffers (devp);
1745 drive->part[p].start_sect = 0;
1746 drive->part[p].nr_sects = 0;
1749 drive->part[0].nr_sects = current_capacity(drive);
1750 if (drive->media == disk)
1751 resetup_one_dev(HWIF(drive)->gd, drive->select.b.unit);
1753 drive->busy = 0;
1754 wake_up(&drive->wqueue);
1755 return 0;
1758 static int write_fs_long (unsigned long useraddr, long value)
1760 int err;
1762 if (NULL == (long *)useraddr)
1763 return -EINVAL;
1764 if ((err = verify_area(VERIFY_WRITE, (long *)useraddr, sizeof(long))))
1765 return err;
1766 put_user((unsigned)value, (long *) useraddr);
1767 return 0;
1770 static int ide_ioctl (struct inode *inode, struct file *file,
1771 unsigned int cmd, unsigned long arg)
1773 struct hd_geometry *loc = (struct hd_geometry *) arg;
1774 int err;
1775 ide_drive_t *drive;
1776 unsigned long flags;
1778 if (!inode || !(inode->i_rdev))
1779 return -EINVAL;
1780 if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
1781 return -ENODEV;
1782 switch (cmd) {
1783 case HDIO_GETGEO:
1784 if (!loc || drive->media != disk) return -EINVAL;
1785 err = verify_area(VERIFY_WRITE, loc, sizeof(*loc));
1786 if (err) return err;
1787 put_user(drive->bios_head, (byte *) &loc->heads);
1788 put_user(drive->bios_sect, (byte *) &loc->sectors);
1789 put_user(drive->bios_cyl, (unsigned short *) &loc->cylinders);
1790 put_user((unsigned)drive->part[MINOR(inode->i_rdev)&PARTN_MASK].start_sect,
1791 (unsigned long *) &loc->start);
1792 return 0;
1794 case BLKFLSBUF:
1795 if(!suser()) return -EACCES;
1796 fsync_dev(inode->i_rdev);
1797 invalidate_buffers(inode->i_rdev);
1798 return 0;
1800 case BLKRASET:
1801 if(!suser()) return -EACCES;
1802 if(arg > 0xff) return -EINVAL;
1803 read_ahead[MAJOR(inode->i_rdev)] = arg;
1804 return 0;
1806 case BLKRAGET:
1807 return write_fs_long(arg, read_ahead[MAJOR(inode->i_rdev)]);
1809 case BLKGETSIZE: /* Return device size */
1810 return write_fs_long(arg, drive->part[MINOR(inode->i_rdev)&PARTN_MASK].nr_sects);
1811 case BLKRRPART: /* Re-read partition tables */
1812 return revalidate_disk(inode->i_rdev);
1814 case HDIO_GET_KEEPSETTINGS:
1815 return write_fs_long(arg, drive->keep_settings);
1817 case HDIO_GET_UNMASKINTR:
1818 return write_fs_long(arg, drive->unmask);
1820 case HDIO_GET_DMA:
1821 return write_fs_long(arg, drive->using_dma);
1823 case HDIO_GET_CHIPSET:
1824 return write_fs_long(arg, drive->chipset);
1826 case HDIO_GET_MULTCOUNT:
1827 return write_fs_long(arg, drive->mult_count);
1829 case HDIO_GET_IDENTITY:
1830 if (!arg || (MINOR(inode->i_rdev) & PARTN_MASK))
1831 return -EINVAL;
1832 if (drive->id == NULL)
1833 return -ENOMSG;
1834 err = verify_area(VERIFY_WRITE, (char *)arg, sizeof(*drive->id));
1835 if (!err)
1836 memcpy_tofs((char *)arg, (char *)drive->id, sizeof(*drive->id));
1837 return err;
1839 case HDIO_GET_NOWERR:
1840 return write_fs_long(arg, drive->bad_wstat == BAD_R_STAT);
1842 case HDIO_SET_DMA:
1843 if (drive->media != disk)
1844 return -EPERM;
1845 if (!drive->id || !(drive->id->capability & 1) || !HWIF(drive)->dmaproc)
1846 return -EPERM;
1847 case HDIO_SET_KEEPSETTINGS:
1848 case HDIO_SET_UNMASKINTR:
1849 case HDIO_SET_NOWERR:
1850 if (arg > 1)
1851 return -EINVAL;
1852 case HDIO_SET_CHIPSET:
1853 if (!suser())
1854 return -EACCES;
1855 if ((MINOR(inode->i_rdev) & PARTN_MASK))
1856 return -EINVAL;
1857 save_flags(flags);
1858 cli();
1859 switch (cmd) {
1860 case HDIO_SET_DMA:
1861 drive->using_dma = arg;
1862 break;
1863 case HDIO_SET_KEEPSETTINGS:
1864 drive->keep_settings = arg;
1865 break;
1866 case HDIO_SET_UNMASKINTR:
1867 if (arg && disallow_unmask) {
1868 restore_flags(flags);
1869 return -EPERM;
1871 drive->unmask = arg;
1872 break;
1873 case HDIO_SET_NOWERR:
1874 drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
1875 break;
1876 case HDIO_SET_CHIPSET:
1877 drive->chipset = arg;
1878 drive->vlb_32bit = (arg & 1);
1879 drive->vlb_sync = (arg & 2) >> 1;
1880 #ifndef VLB_SYNC
1881 if (drive->vlb_sync)
1882 printk("%s: VLB_SYNC not supported by this kernel\n", drive->name);
1883 #endif
1884 break;
1886 restore_flags(flags);
1887 return 0;
1889 case HDIO_SET_MULTCOUNT:
1890 if (!suser())
1891 return -EACCES;
1892 if (MINOR(inode->i_rdev) & PARTN_MASK)
1893 return -EINVAL;
1894 if ((drive->id != NULL) && (arg > drive->id->max_multsect))
1895 return -EINVAL;
1896 save_flags(flags);
1897 cli();
1898 if (drive->special.b.set_multmode) {
1899 restore_flags(flags);
1900 return -EBUSY;
1902 drive->mult_req = arg;
1903 drive->special.b.set_multmode = 1;
1904 restore_flags(flags);
1905 ide_do_drive_cmd (inode->i_rdev, NULL);
1906 return (drive->mult_count == arg) ? 0 : -EIO;
1908 case HDIO_DRIVE_CMD:
1910 unsigned long args;
1912 if (NULL == (long *) arg)
1913 err = ide_do_drive_cmd(inode->i_rdev,NULL);
1914 else {
1915 if (!(err = verify_area(VERIFY_READ,(long *)arg,sizeof(long))))
1917 args = get_user((long *)arg);
1918 if (!(err = verify_area(VERIFY_WRITE,(long *)arg,sizeof(long)))) {
1919 err = ide_do_drive_cmd(inode->i_rdev,(char *)&args);
1920 put_user(args,(long *)arg);
1924 return err;
1927 RO_IOCTLS(inode->i_rdev, arg);
1929 default:
1930 #ifdef CONFIG_BLK_DEV_IDECD
1931 if (drive->media == cdrom)
1932 return ide_cdrom_ioctl(drive, inode, file, cmd, arg);
1933 #endif /* CONFIG_BLK_DEV_IDECD */
1934 return -EPERM;
1938 static int ide_check_media_change (kdev_t i_rdev)
1940 ide_drive_t *drive;
1942 if ((drive = get_info_ptr(i_rdev)) == NULL)
1943 return -ENODEV;
1944 #ifdef CONFIG_BLK_DEV_IDECD
1945 if (drive->media == cdrom)
1946 return ide_cdrom_check_media_change (drive);
1947 #endif /* CONFIG_BLK_DEV_IDECD */
1948 if (drive->removeable) /* for disks */
1949 return 1; /* always assume it was changed */
1950 return 0;
1953 static void fixstring (byte *s, const int bytecount, const int byteswap)
1955 byte *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */
1957 if (byteswap) {
1958 /* convert from big-endian to host byte order */
1959 for (p = end ; p != s;) {
1960 unsigned short *pp = (unsigned short *) (p -= 2);
1961 *pp = ntohs(*pp);
1965 /* strip leading blanks */
1966 while (s != end && *s == ' ')
1967 ++s;
1969 /* compress internal blanks and strip trailing blanks */
1970 while (s != end && *s) {
1971 if (*s++ != ' ' || (s != end && *s && *s != ' '))
1972 *p++ = *(s-1);
1975 /* wipe out trailing garbage */
1976 while (p != end)
1977 *p++ = '\0';
1980 static inline void do_identify (ide_drive_t *drive, byte cmd)
1982 int bswap;
1983 struct hd_driveid *id;
1984 unsigned long capacity, check;
1986 id = drive->id = kmalloc (SECTOR_WORDS*4, GFP_KERNEL);
1987 ide_input_data(drive, id, SECTOR_WORDS); /* read 512 bytes of id info */
1988 sti();
1991 * EATA SCSI controllers do a hardware ATA emulation: ignore them
1993 if ((id->model[0] == 'P' && id->model[1] == 'M')
1994 || (id->model[0] == 'S' && id->model[1] == 'K')) {
1995 printk("%s: EATA SCSI HBA %.10s\n", drive->name, id->model);
1996 drive->present = 0;
1997 return;
2001 * WIN_IDENTIFY returns little-endian info,
2002 * WIN_PIDENTIFY *usually* returns little-endian info.
2004 bswap = 1;
2005 if (cmd == WIN_PIDENTIFY) {
2006 if ((id->model[0] == 'N' && id->model[1] == 'E') /* NEC */
2007 || (id->model[0] == 'F' && id->model[1] == 'X') /* Mitsumi */
2008 || (id->model[0] == 'P' && id->model[1] == 'i'))/* Pioneer */
2009 bswap = 0; /* Vertos drives may still be weird */
2011 fixstring (id->model, sizeof(id->model), bswap);
2012 fixstring (id->fw_rev, sizeof(id->fw_rev), bswap);
2013 fixstring (id->serial_no, sizeof(id->serial_no), bswap);
2016 * Check for an ATAPI device
2018 if (cmd == WIN_PIDENTIFY) {
2019 #ifdef CONFIG_BLK_DEV_IDECD
2020 byte type = (id->config >> 8) & 0x0f;
2021 #endif /* CONFIG_BLK_DEV_IDECD */
2022 printk("%s: %s, ATAPI, ", drive->name, id->model);
2023 drive->media = cdrom;
2024 #ifdef CONFIG_BLK_DEV_IDECD
2025 if (type == 0 || type == 5)
2026 printk("CDROM drive\n");
2027 else
2028 printk("UNKNOWN device\n");
2029 drive->present = 1;
2030 drive->removeable = 1;
2031 #else
2032 printk("not supported by this kernel\n");
2033 #endif /* CONFIG_BLK_DEV_IDECD */
2034 return;
2037 /* check for removeable disks (eg. SYQUEST), ignore 'WD' drives */
2038 if (id->config & (1<<7)) { /* removeable disk ? */
2039 if (id->model[0] != 'W' || id->model[1] != 'D')
2040 drive->removeable = 1;
2043 drive->media = disk;
2044 /* Extract geometry if we did not already have one for the drive */
2045 if (!drive->present) {
2046 drive->present = 1;
2047 drive->cyl = drive->bios_cyl = id->cyls;
2048 drive->head = drive->bios_head = id->heads;
2049 drive->sect = drive->bios_sect = id->sectors;
2051 /* Handle logical geometry translation by the drive */
2052 if ((id->field_valid & 1) && id->cur_cyls && id->cur_heads
2053 && (id->cur_heads <= 16) && id->cur_sectors)
2056 * Extract the physical drive geometry for our use.
2057 * Note that we purposely do *not* update the bios info.
2058 * This way, programs that use it (like fdisk) will
2059 * still have the same logical view as the BIOS does,
2060 * which keeps the partition table from being screwed.
2062 * An exception to this is the cylinder count,
2063 * which we reexamine later on to correct for 1024 limitations.
2065 drive->cyl = id->cur_cyls;
2066 drive->head = id->cur_heads;
2067 drive->sect = id->cur_sectors;
2069 /* check for word-swapped "capacity" field in id information */
2070 capacity = drive->cyl * drive->head * drive->sect;
2071 check = (id->cur_capacity0 << 16) | id->cur_capacity1;
2072 if (check == capacity) { /* was it swapped? */
2073 /* yes, bring it into little-endian order: */
2074 id->cur_capacity0 = (capacity >> 0) & 0xffff;
2075 id->cur_capacity1 = (capacity >> 16) & 0xffff;
2078 /* Use physical geometry if what we have still makes no sense */
2079 if ((!drive->head || drive->head > 16) && id->heads && id->heads <= 16) {
2080 drive->cyl = id->cyls;
2081 drive->head = id->heads;
2082 drive->sect = id->sectors;
2084 /* Correct the number of cyls if the bios value is too small */
2085 if (drive->sect == drive->bios_sect && drive->head == drive->bios_head) {
2086 if (drive->cyl > drive->bios_cyl)
2087 drive->bios_cyl = drive->cyl;
2090 (void) current_capacity (drive); /* initialize LBA selection */
2092 printk ("%s: %.40s, %ldMB w/%dKB Cache, %sCHS=%d/%d/%d",
2093 drive->name, id->model, current_capacity(drive)/2048L, id->buf_size/2,
2094 drive->select.b.lba ? "LBA, " : "",
2095 drive->bios_cyl, drive->bios_head, drive->bios_sect);
2097 drive->mult_count = 0;
2098 if (id->max_multsect) {
2099 drive->mult_req = INITIAL_MULT_COUNT;
2100 if (drive->mult_req > id->max_multsect)
2101 drive->mult_req = id->max_multsect;
2102 if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
2103 drive->special.b.set_multmode = 1;
2105 if (HWIF(drive)->dmaproc != NULL) { /* hwif supports DMA? */
2106 if (!(HWIF(drive)->dmaproc(ide_dma_check, drive)))
2107 printk(", DMA");
2109 printk("\n");
2113 * Delay for *at least* 10ms. As we don't know how much time is left
2114 * until the next tick occurs, we wait an extra tick to be safe.
2116 static void delay_10ms (void)
2118 unsigned long timer = jiffies + (HZ + 99)/100 + 1;
2119 while (timer > jiffies);
2123 * try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
2124 * and waits for a response. It also monitors irqs while this is
2125 * happening, in hope of automatically determining which one is
2126 * being used by the interface.
2128 * Returns: 0 device was identified
2129 * 1 device timed-out (no response to identify request)
2130 * 2 device aborted the command (refused to identify itself)
2132 static int try_to_identify (ide_drive_t *drive, byte cmd)
2134 int hd_status, rc;
2135 unsigned long timeout;
2136 int irqs = 0;
2138 if (!HWIF(drive)->irq) { /* already got an IRQ? */
2139 probe_irq_off(probe_irq_on()); /* clear dangling irqs */
2140 irqs = probe_irq_on(); /* start monitoring irqs */
2141 OUT_BYTE(drive->ctl,IDE_CONTROL_REG); /* enable device irq */
2144 delay_10ms(); /* take a deep breath */
2145 if ((IN_BYTE(IDE_ALTSTATUS_REG) ^ IN_BYTE(IDE_STATUS_REG)) & ~INDEX_STAT) {
2146 printk("%s: probing with STATUS instead of ALTSTATUS\n", drive->name);
2147 hd_status = IDE_STATUS_REG; /* ancient Seagate drives */
2148 } else
2149 hd_status = IDE_ALTSTATUS_REG; /* use non-intrusive polling */
2151 OUT_BYTE(cmd,IDE_COMMAND_REG); /* ask drive for ID */
2152 timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
2153 timeout += jiffies;
2154 do {
2155 if (jiffies > timeout) {
2156 if (!HWIF(drive)->irq)
2157 (void) probe_irq_off(irqs);
2158 return 1; /* drive timed-out */
2160 delay_10ms(); /* give drive a breather */
2161 } while (IN_BYTE(hd_status) & BUSY_STAT);
2163 delay_10ms(); /* wait for IRQ and DRQ_STAT */
2164 if (OK_STAT(GET_STAT(),DRQ_STAT,BAD_R_STAT)) {
2165 cli(); /* some systems need this */
2166 do_identify(drive, cmd); /* drive returned ID */
2167 rc = 0; /* success */
2168 } else
2169 rc = 2; /* drive refused ID */
2170 if (!HWIF(drive)->irq) {
2171 irqs = probe_irq_off(irqs); /* get irq number */
2172 if (irqs > 0)
2173 HWIF(drive)->irq = irqs;
2174 else /* Mmmm.. multiple IRQs */
2175 printk("%s: IRQ probe failed (%d)\n", drive->name, irqs);
2177 return rc;
2181 * do_probe() has the difficult job of finding a drive if it exists,
2182 * without getting hung up if it doesn't exist, without trampling on
2183 * ethernet cards, and without leaving any IRQs dangling to haunt us later.
2185 * If a drive is "known" to exist (from CMOS or kernel parameters),
2186 * but does not respond right away, the probe will "hang in there"
2187 * for the maximum wait time (about 30 seconds), otherwise it will
2188 * exit much more quickly.
2190 * Returns: 0 device was identified
2191 * 1 device timed-out (no response to identify request)
2192 * 2 device aborted the command (refused to identify itself)
2193 * 3 bad status from device (possible for ATAPI drives)
2194 * 4 probe was not attempted because failure was obvious
2196 static int do_probe (ide_drive_t *drive, byte cmd)
2198 int rc;
2199 #ifdef CONFIG_BLK_DEV_IDECD
2200 if (drive->present) { /* avoid waiting for inappropriate probes */
2201 if ((drive->media == cdrom) && (cmd == WIN_IDENTIFY))
2202 return 4;
2204 #endif /* CONFIG_BLK_DEV_IDECD */
2205 #ifdef DEBUG
2206 printk("probing for %s: present=%d, type=%s, probetype=%s\n",
2207 drive->name, drive->present, drive->media ? "cdrom":"disk",
2208 (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
2209 #endif
2210 #if SUPPORT_HT6560B
2211 if (HWIF(drive)->select)
2212 ide_hwif_select (HWIF(drive));
2213 #endif
2214 OUT_BYTE(drive->select.all,IDE_SELECT_REG); /* select target drive */
2215 delay_10ms(); /* wait for BUSY_STAT */
2216 if (IN_BYTE(IDE_SELECT_REG) != drive->select.all && !drive->present) {
2217 OUT_BYTE(0xa0,IDE_SELECT_REG); /* exit with drive0 selected */
2218 return 3; /* no i/f present: avoid killing ethernet cards */
2221 if (OK_STAT(GET_STAT(),READY_STAT,BUSY_STAT)
2222 || drive->present || cmd == WIN_PIDENTIFY)
2224 if ((rc = try_to_identify(drive,cmd))) /* send cmd and wait */
2225 rc = try_to_identify(drive,cmd); /* failed: try again */
2226 if (rc == 1)
2227 printk("%s: no response (status = 0x%02x)\n", drive->name, GET_STAT());
2228 (void) GET_STAT(); /* ensure drive irq is clear */
2229 } else {
2230 rc = 3; /* not present or maybe ATAPI */
2232 if (drive->select.b.unit != 0) {
2233 OUT_BYTE(0xa0,IDE_SELECT_REG); /* exit with drive0 selected */
2234 delay_10ms();
2235 (void) GET_STAT(); /* ensure drive irq is clear */
2237 return rc;
2241 * probe_for_drive() tests for existance of a given drive using do_probe().
2243 * Returns: 0 no device was found
2244 * 1 device was found (note: drive->present might still be 0)
2246 static inline byte probe_for_drive (ide_drive_t *drive)
2248 if (drive->noprobe) /* skip probing? */
2249 return drive->present;
2250 if (do_probe(drive, WIN_IDENTIFY) >= 2) { /* if !(success||timed-out) */
2251 #ifdef CONFIG_BLK_DEV_IDECD
2252 (void) do_probe(drive, WIN_PIDENTIFY); /* look for ATAPI device */
2253 #endif /* CONFIG_BLK_DEV_IDECD */
2255 if (!drive->present)
2256 return 0; /* drive not found */
2257 if (drive->id == NULL) { /* identification failed? */
2258 if (drive->media == disk) {
2259 printk ("%s: non-IDE drive, CHS=%d/%d/%d\n",
2260 drive->name, drive->cyl, drive->head, drive->sect);
2262 #ifdef CONFIG_BLK_DEV_IDECD
2263 else if (drive->media == cdrom) {
2264 printk("%s: ATAPI cdrom (?)\n", drive->name);
2266 #endif /* CONFIG_BLK_DEV_IDECD */
2267 else {
2268 drive->present = 0; /* nuke it */
2269 return 1; /* drive was found */
2272 if (drive->media == disk && !drive->select.b.lba) {
2273 if (!drive->head || drive->head > 16) {
2274 printk("%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
2275 drive->name, drive->head);
2276 drive->present = 0;
2279 return 1; /* drive was found */
2283 * This routine only knows how to look for drive units 0 and 1
2284 * on an interface, so any setting of MAX_DRIVES > 2 won't work here.
2286 static void probe_for_drives (ide_hwif_t *hwif)
2288 unsigned int unit;
2290 if (check_region(hwif->io_base,8) || check_region(hwif->ctl_port,1)) {
2291 int msgout = 0;
2292 for (unit = 0; unit < MAX_DRIVES; ++unit) {
2293 ide_drive_t *drive = &hwif->drives[unit];
2294 if (drive->present) {
2295 drive->present = 0;
2296 printk("%s: ERROR, PORTS ALREADY IN USE\n", drive->name);
2297 msgout = 1;
2300 if (!msgout)
2301 printk("%s: ports already in use, skipping probe\n", hwif->name);
2302 } else {
2303 unsigned long flags;
2304 save_flags(flags);
2306 #if (MAX_DRIVES > 2)
2307 printk("%s: probing for first 2 of %d possible drives\n", hwif->name, MAX_DRIVES);
2308 #endif
2309 sti(); /* needed for jiffies and irq probing */
2311 * Second drive should only exist if first drive was found,
2312 * but a lot of cdrom drives seem to be configured as slave-only
2314 for (unit = 0; unit < 2; ++unit) { /* note the hardcoded '2' */
2315 ide_drive_t *drive = &hwif->drives[unit];
2316 (void) probe_for_drive (drive);
2318 for (unit = 0; unit < MAX_DRIVES; ++unit) {
2319 ide_drive_t *drive = &hwif->drives[unit];
2320 if (drive->present) {
2321 hwif->present = 1;
2322 request_region(hwif->io_base, 8, hwif->name);
2323 request_region(hwif->ctl_port, 1, hwif->name);
2324 break;
2327 restore_flags(flags);
2331 #if SUPPORT_DTC2278
2333 * From: andy@cercle.cts.com (Dyan Wile)
2335 * Below is a patch for DTC-2278 - alike software-programmable controllers
2336 * The code enables the secondary IDE controller and the PIO4 (3?) timings on
2337 * the primary (EIDE). You may probably have to enable the 32-bit support to
2338 * get the full speed. You better get the disk interrupts disabled ( hdparm -u0
2339 * /dev/hd.. ) for the drives connected to the EIDE interface. (I get my
2340 * filesystem corrupted with -u1, but under heavy disk load only :-)
2342 * From: mlord@bnr.ca -- this chipset is now forced to use the "serialize" feature,
2343 * which hopefully will make it more reliable to use.. maybe it has the same bugs
2344 * as the CMD640B and RZ1000 ??
2347 #if SET_DTC2278_MODE4
2348 static void sub22 (char b, char c)
2350 int i;
2352 for(i = 0; i < 3; ++i) {
2353 inb(0x3f6);
2354 outb_p(b,0xb0);
2355 inb(0x3f6);
2356 outb_p(c,0xb4);
2357 inb(0x3f6);
2358 if(inb(0xb4) == c) {
2359 outb_p(7,0xb0);
2360 inb(0x3f6);
2361 return; /* success */
2365 #endif /* SET_DTC2278_MODE4 */
2367 static void init_dtc2278 (void)
2369 unsigned long flags;
2371 save_flags(flags);
2372 cli();
2373 #if SET_DTC2278_MODE4
2375 * This enables PIO mode4 (3?) on the first interface
2377 sub22(1,0xc3);
2378 sub22(0,0xa0);
2379 #endif /* SET_DTC2278_MODE4 */
2381 * This enables the second interface
2383 outb_p(4,0xb0);
2384 inb(0x3f6);
2385 outb_p(0x20,0xb4);
2386 inb(0x3f6);
2387 restore_flags(flags);
2389 #endif /* SUPPORT_DTC2278 */
2391 #ifdef SUPPORT_QD6580
2393 * QDI QD6580 EIDE controller fast support by Colten Edwards.
2394 * no net access but I can be reached at pje120@cs.usask.ca
2396 * I suppose that a IOCTL could be used for this and other
2397 * cards like it to modify the speed using hdparm. Someday..
2399 static void init_qd6580 (void)
2401 unsigned long flags;
2403 /* looks like 0x4f is fast
2404 * 0x3f is medium
2405 * 0x2f is slower
2406 * 0x1f is slower yet
2407 * ports are 0xb0 0xb2 and 0xb3
2410 save_flags(flags);
2411 cli();
2412 outb_p(0x8d,0xb0);
2413 outb_p(0x0 ,0xb2);
2414 outb_p(0x4f,0xb3); /* select "fast" 0x4f */
2415 inb(0x3f6);
2416 restore_flags(flags);
2418 #endif /* SUPPORT_QD6580 */
2420 #if SUPPORT_CMD640
2422 * ??? fixme:
2424 byte read_cmd640_vlb (byte port, byte reg)
2426 byte val;
2428 unsigned long flags;
2429 save_flags(flags);
2430 cli();
2431 outw(reg, port);
2432 val = inb(port+4);
2433 restore_flags(flags);
2434 return val;
2437 void write_cmd640_vlb (byte port, byte reg, byte val)
2439 unsigned long flags;
2440 save_flags(flags);
2441 cli();
2442 outw(reg, port);
2443 outw(val, port+4);
2444 restore_flags(flags);
2447 void init_cmd640_vlb (void)
2449 byte reg;
2450 unsigned short port = 0x178;
2452 serialized = 1;
2453 printk("ide: buggy CMD640 interface: serialized, ");
2454 reg = read_cmd640_vlb(port, 0x50);
2455 if (reg == 0xff || (reg & 0x90) != 0x90) {
2456 #if TRY_CMD640_VLB_AT_0x78
2457 port = 0x78;
2458 reg = read_cmd640_vlb(port, 0x50);
2459 if (reg == 0xff || (reg & 0x90) != 0x90)
2460 #endif
2462 disallow_unmask = 1;
2463 printk("(probe failed) disabled unmasking\n");
2464 return;
2467 write_cmd640_vlb(port, 0x51, read_cmd640_vlb(port, 0x51)|0xc8);
2468 write_cmd640_vlb(port, 0x57, read_cmd640_vlb(port, 0x57)|0x0c);
2469 printk("disabled read-ahead, enabled secondary\n");
2472 #endif /* SUPPORT_CMD640 */
2475 * stridx() returns the offset of c within s,
2476 * or -1 if c is '\0' or not found within s.
2478 static int stridx (const char *s, char c)
2480 char *i = strchr(s, c);
2481 return (i && c) ? i - s : -1;
2485 * match_parm() does parsing for ide_setup():
2487 * 1. the first char of s must be '='.
2488 * 2. if the remainder matches one of the supplied keywords,
2489 * the index (1 based) of the keyword is negated and returned.
2490 * 3. if the remainder is a series of no more than max_vals numbers
2491 * separated by commas, the numbers are saved in vals[] and a
2492 * count of how many were saved is returned. Base10 is assumed,
2493 * and base16 is allowed when prefixed with "0x".
2494 * 4. otherwise, zero is returned.
2496 static int match_parm (char *s, const char *keywords[], int vals[], int max_vals)
2498 static const char *decimal = "0123456789";
2499 static const char *hex = "0123456789abcdef";
2500 int i, n;
2502 if (*s++ == '=') {
2504 * Try matching against the supplied keywords,
2505 * and return -(index+1) if we match one
2507 for (i = 0; *keywords != NULL; ++i) {
2508 if (!strcmp(s, *keywords++))
2509 return -(i+1);
2512 * Look for a series of no more than "max_vals"
2513 * numeric values separated by commas, in base10,
2514 * or base16 when prefixed with "0x".
2515 * Return a count of how many were found.
2517 for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
2518 vals[n] = i;
2519 while ((i = stridx(decimal, *++s)) >= 0)
2520 vals[n] = (vals[n] * 10) + i;
2521 if (*s == 'x' && !vals[n]) {
2522 while ((i = stridx(hex, *++s)) >= 0)
2523 vals[n] = (vals[n] * 0x10) + i;
2525 if (++n == max_vals)
2526 break;
2527 if (*s == ',')
2528 ++s;
2530 if (!*s)
2531 return n;
2533 return 0; /* zero = nothing matched */
2537 * ide_setup() gets called VERY EARLY during initialization,
2538 * to handle kernel "command line" strings beginning with "hdx="
2539 * or "ide". Here is the complete set currently supported:
2541 * "hdx=" is recognized for all "x" from "a" to "h", such as "hdc".
2542 * "idex=" is recognized for all "x" from "0" to "3", such as "ide1".
2544 * "hdx=noprobe" : drive may be present, but do not probe for it
2545 * "hdx=nowerr" : ignore the WRERR_STAT bit on this drive
2546 * "hdx=cdrom" : drive is present, and is a cdrom drive
2547 * "hdx=cyl,head,sect" : disk drive is present, with specified geometry
2549 * "idex=noprobe" : do not attempt to access/use this interface
2550 * "idex=base" : probe for an interface at the addr specified,
2551 * where "base" is usually 0x1f0 or 0x170
2552 * and "ctl" is assumed to be "base"+0x206
2553 * "idex=base,ctl" : specify both base and ctl
2554 * "idex=base,ctl,irq" : specify base, ctl, and irq number
2556 * The following two are valid ONLY on ide0 or ide1,
2557 * and the defaults for the base,ctl ports must not be altered.
2559 * "idex=serialize" : do not overlap operations on ide0 and ide1.
2560 * "idex=dtc2278" : enables use of DTC2278 secondary i/f
2561 * "idex=ht6560b" : enables use of HT6560B secondary i/f
2562 * "idex=cmd640_vlb" : required for VLB cards with the CMD640 chip
2563 * (not for PCI -- automatically detected)
2565 * This option is valid ONLY on ide0, and the defaults for the base,ctl ports
2566 * must not be altered.
2568 * "ide0=qd6580" : select "fast" interface speed on a qd6580 interface
2570 void ide_setup (char *s)
2572 int vals[3];
2573 ide_hwif_t *hwif;
2574 ide_drive_t *drive;
2575 unsigned int hw, unit;
2576 const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
2577 const char max_hwif = '0' + (MAX_HWIFS - 1);
2579 printk("ide_setup: %s", s);
2580 init_ide_data ();
2583 * Look for drive options: "hdx="
2585 if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
2586 const char *hd_words[] = {"noprobe", "nowerr", "cdrom", "serialize", NULL};
2587 unit = s[2] - 'a';
2588 hw = unit / MAX_DRIVES;
2589 unit = unit % MAX_DRIVES;
2590 hwif = &ide_hwifs[hw];
2591 drive = &hwif->drives[unit];
2592 switch (match_parm(&s[3], hd_words, vals, 3)) {
2593 case -1: /* "noprobe" */
2594 drive->noprobe = 1;
2595 goto done;
2596 case -2: /* "nowerr" */
2597 drive->bad_wstat = BAD_R_STAT;
2598 hwif->noprobe = 0;
2599 goto done;
2600 case -3: /* "cdrom" */
2601 drive->present = 1;
2602 drive->media = cdrom;
2603 hwif->noprobe = 0;
2604 goto done;
2605 case -4: /* "serialize" */
2606 printk(" -- USE \"ide%c=serialize\" INSTEAD", '0'+hw);
2607 goto do_serialize;
2608 case 3: /* cyl,head,sect */
2609 drive->media = disk;
2610 drive->cyl = drive->bios_cyl = vals[0];
2611 drive->head = drive->bios_head = vals[1];
2612 drive->sect = drive->bios_sect = vals[2];
2613 drive->present = 1;
2614 hwif->noprobe = 0;
2615 goto done;
2616 default:
2617 goto bad_option;
2621 * Look for interface options: "idex="
2623 if (s[0] == 'i' && s[1] == 'd' && s[2] == 'e' && s[3] >= '0' && s[3] <= max_hwif) {
2624 const char *ide_words[] = {"noprobe", "serialize", "dtc2278", "ht6560b", "cmd640_vlb", "qd6580", NULL};
2625 hw = s[3] - '0';
2626 hwif = &ide_hwifs[hw];
2628 switch (match_parm(&s[4], ide_words, vals, 3)) {
2629 #if SUPPORT_QD6580
2630 case -6: /* "qd6580" */
2631 if (hw != 0) goto bad_hwif;
2632 init_qd6580();
2633 goto done;
2634 #endif /* SUPPORT_QD6580 */
2635 #if SUPPORT_CMD640
2636 case -5: /* "cmd640_vlb" */
2637 if (hw > 1) goto bad_hwif;
2638 init_cmd640_vlb();
2639 goto do_serialize; /* not necessary once we implement the above */
2640 break;
2641 #endif /* SUPPORT_CMD640 */
2642 #if SUPPORT_HT6560B
2643 case -4: /* "ht6560b" */
2644 if (hw > 1) goto bad_hwif;
2646 * Using 0x1c and 0x1d apparently selects a
2647 * faster interface speed than 0x3c and 0x3d.
2649 * Need to add an ioctl to select between them.
2651 if (check_region(0x3e6,1)) {
2652 printk(" -- HT6560 PORT 0x3e6 ALREADY IN USE");
2653 goto done;
2655 request_region(0x3e6, 1, hwif->name);
2656 ide_hwifs[0].select = 0x3c;
2657 ide_hwifs[1].select = 0x3d;
2658 goto do_serialize;
2659 #endif /* SUPPORT_HT6560B */
2660 #if SUPPORT_DTC2278
2661 case -3: /* "dtc2278" */
2662 if (hw > 1) goto bad_hwif;
2663 init_dtc2278();
2664 goto do_serialize;
2665 #endif /* SUPPORT_DTC2278 */
2666 case -2: /* "serialize" */
2667 do_serialize:
2668 if (hw > 1) goto bad_hwif;
2669 serialized = 1;
2670 goto done;
2671 case -1: /* "noprobe" */
2672 hwif->noprobe = 1;
2673 goto done;
2674 case 1: /* base */
2675 vals[1] = vals[0] + 0x206; /* default ctl */
2676 case 2: /* base,ctl */
2677 vals[2] = 0; /* default irq = probe for it */
2678 case 3: /* base,ctl,irq */
2679 hwif->io_base = vals[0];
2680 hwif->ctl_port = vals[1];
2681 hwif->irq = vals[2];
2682 hwif->noprobe = 0;
2683 goto done;
2686 bad_option:
2687 printk(" -- BAD OPTION\n");
2688 return;
2689 bad_hwif:
2690 printk("-- NOT SUPPORTED ON ide%d", hw);
2691 done:
2692 printk("\n");
2696 * This routine is called from the partition-table code in genhd.c
2697 * to "convert" a drive to a logical geometry with fewer than 1024 cyls
2698 * It mimics the method used by Ontrack Disk Manager.
2700 int ide_xlate_1024 (kdev_t i_rdev, int offset, const char *msg)
2702 ide_drive_t *drive;
2703 static const byte head_vals[] = {4, 8, 16, 32, 64, 128, 255, 0};
2704 const byte *heads = head_vals;
2705 unsigned long tracks;
2707 if ((drive = get_info_ptr(i_rdev)) == NULL || drive->id == NULL)
2708 return 0;
2710 drive->cyl = drive->bios_cyl = drive->id->cyls;
2711 drive->head = drive->bios_head = drive->id->heads;
2712 drive->sect = drive->bios_sect = drive->id->sectors;
2713 drive->special.b.set_geometry = 1;
2715 tracks = drive->bios_cyl * drive->bios_head * drive->bios_sect / 63;
2716 drive->bios_sect = 63;
2717 while (drive->bios_cyl >= 1024) {
2718 drive->bios_head = *heads;
2719 drive->bios_cyl = tracks / drive->bios_head;
2720 if (0 == *++heads)
2721 break;
2723 if (offset) {
2724 #if FAKE_FDISK_FOR_EZDRIVE
2725 if (offset == -1)
2726 drive->ezdrive = 1;
2727 else
2728 #endif /* FAKE_FDISK_FOR_EZDRIVE */
2730 drive->sect0 = 63;
2731 drive->bios_cyl = (tracks - 1) / drive->bios_head;
2734 drive->part[0].nr_sects = current_capacity(drive);
2735 printk("%s [%d/%d/%d]", msg, drive->bios_cyl, drive->bios_head, drive->bios_sect);
2736 return 1;
2740 * We query CMOS about hard disks : it could be that we have a SCSI/ESDI/etc
2741 * controller that is BIOS compatible with ST-506, and thus showing up in our
2742 * BIOS table, but not register compatible, and therefore not present in CMOS.
2744 * Furthermore, we will assume that our ST-506 drives <if any> are the primary
2745 * drives in the system -- the ones reflected as drive 1 or 2. The first
2746 * drive is stored in the high nibble of CMOS byte 0x12, the second in the low
2747 * nibble. This will be either a 4 bit drive type or 0xf indicating use byte
2748 * 0x19 for an 8 bit type, drive 1, 0x1a for drive 2 in CMOS. A non-zero value
2749 * means we have an AT controller hard disk for that drive.
2751 * Of course, there is no guarantee that either drive is actually on the
2752 * "primary" IDE interface, but we don't bother trying to sort that out here.
2753 * If a drive is not actually on the primary interface, then these parameters
2754 * will be ignored. This results in the user having to supply the logical
2755 * drive geometry as a boot parameter for each drive not on the primary i/f.
2757 * The only "perfect" way to handle this would be to modify the setup.[cS] code
2758 * to do BIOS calls Int13h/Fn08h and Int13h/Fn48h to get all of the drive info
2759 * for us during initialization. I have the necessary docs -- any takers? -ml
2762 static void probe_cmos_for_drives (ide_hwif_t *hwif)
2764 #ifdef __i386__
2765 extern struct drive_info_struct drive_info;
2766 byte cmos_disks, *BIOS = (byte *) &drive_info;
2767 int unit;
2769 outb_p(0x12,0x70); /* specify CMOS address 0x12 */
2770 cmos_disks = inb_p(0x71); /* read the data from 0x12 */
2771 /* Extract drive geometry from CMOS+BIOS if not already setup */
2772 for (unit = 0; unit < MAX_DRIVES; ++unit) {
2773 ide_drive_t *drive = &hwif->drives[unit];
2774 if ((cmos_disks & (0xf0 >> (unit*4))) && !drive->present) {
2775 drive->cyl = drive->bios_cyl = *(unsigned short *)BIOS;
2776 drive->head = drive->bios_head = *(BIOS+2);
2777 drive->sect = drive->bios_sect = *(BIOS+14);
2778 drive->ctl = *(BIOS+8);
2779 drive->present = 1;
2781 BIOS += 16;
2783 #endif
2787 * This routine sets up the irq for an ide interface, and creates a new
2788 * hwgroup for the irq/hwif if none was previously assigned.
2790 * The SA_INTERRUPT in sa_flags means ide_intr() is always entered with
2791 * interrupts completely disabled. This can be bad for interrupt latency,
2792 * but anything else has led to problems on some machines. We re-enable
2793 * interrupts as much as we can safely do in most places.
2795 static int init_irq (ide_hwif_t *hwif)
2797 unsigned long flags;
2798 int irq = hwif->irq;
2799 ide_hwgroup_t *hwgroup = irq_to_hwgroup[irq];
2801 save_flags(flags);
2802 cli();
2805 * Grab the irq if we don't already have it from a previous hwif
2807 if (hwgroup == NULL) {
2808 if (request_irq(irq, ide_intr, SA_INTERRUPT|SA_SAMPLE_RANDOM, hwif->name)) {
2809 restore_flags(flags);
2810 printk(" -- FAILED!");
2811 return 1;
2815 * Check for serialization with ide1.
2816 * This code depends on us having already taken care of ide1.
2818 if (serialized && hwif->name[3] == '0' && ide_hwifs[1].present)
2819 hwgroup = ide_hwifs[1].hwgroup;
2821 * If this is the first interface in a group,
2822 * then we need to create the hwgroup structure
2824 if (hwgroup == NULL) {
2825 hwgroup = kmalloc (sizeof(ide_hwgroup_t), GFP_KERNEL);
2826 hwgroup->hwif = hwif->next = hwif;
2827 hwgroup->rq = NULL;
2828 hwgroup->handler = NULL;
2829 hwgroup->drive = NULL;
2830 hwgroup->reset_timeout = 0;
2831 #ifdef CONFIG_BLK_DEV_IDECD
2832 hwgroup->doing_atapi_reset = 0;
2833 #endif /* CONFIG_BLK_DEV_IDECD */
2834 init_timer(&hwgroup->timer);
2835 hwgroup->timer.function = &timer_expiry;
2836 hwgroup->timer.data = (unsigned long) hwgroup;
2837 } else {
2838 hwif->next = hwgroup->hwif->next;
2839 hwgroup->hwif->next = hwif;
2841 hwif->hwgroup = hwgroup;
2842 irq_to_hwgroup[irq] = hwgroup;
2844 restore_flags(flags); /* safe now that hwif->hwgroup is set up */
2846 printk("%s at 0x%03x-0x%03x,0x%03x on irq %d", hwif->name,
2847 hwif->io_base, hwif->io_base+7, hwif->ctl_port, irq);
2848 if (hwgroup->hwif != hwif)
2849 printk(" (serialized with %s)", hwgroup->hwif->name);
2850 printk("\n");
2851 return 0;
2854 static struct file_operations ide_fops = {
2855 NULL, /* lseek - default */
2856 block_read, /* read - general block-dev read */
2857 block_write, /* write - general block-dev write */
2858 NULL, /* readdir - bad */
2859 NULL, /* select */
2860 ide_ioctl, /* ioctl */
2861 NULL, /* mmap */
2862 ide_open, /* open */
2863 ide_release, /* release */
2864 block_fsync /* fsync */
2865 ,NULL, /* fasync */
2866 ide_check_media_change, /* check_media_change */
2867 revalidate_disk /* revalidate */
2870 #ifdef CONFIG_PCI
2872 void ide_pci_access_error (int rc)
2874 printk("ide: pcibios access failed - %s\n", pcibios_strerror(rc));
2877 #if SUPPORT_RZ1000 || SUPPORT_CMD640
2878 void buggy_interface_fallback (int rc)
2880 ide_pci_access_error (rc);
2881 serialized = 1;
2882 disallow_unmask = 1;
2883 printk("serialized, disabled unmasking\n");
2885 #endif /* SUPPORT_RZ1000 || SUPPORT_CMD640 */
2887 #if SUPPORT_RZ1000
2888 void init_rz1000 (byte bus, byte fn)
2890 int rc;
2891 unsigned short reg;
2893 printk("ide: buggy RZ1000 interface: ");
2894 if ((rc = pcibios_read_config_word (bus, fn, PCI_COMMAND, &reg))) {
2895 ide_pci_access_error (rc);
2896 } else if (!(reg & 1)) {
2897 printk("not enabled\n");
2898 } else {
2899 if ((rc = pcibios_read_config_word(bus, fn, 0x40, &reg))
2900 || (rc = pcibios_write_config_word(bus, fn, 0x40, reg & 0xdfff)))
2901 buggy_interface_fallback (rc);
2902 else
2903 printk("disabled read-ahead\n");
2906 #endif /* SUPPORT_RZ1000 */
2908 #if SUPPORT_CMD640
2909 void init_cmd640 (byte bus, byte fn)
2911 int rc;
2912 unsigned char reg;
2914 serialized = 1;
2915 printk("ide: buggy CMD640 interface: ");
2917 #if 0 /* funny.. the cmd640b I tried this on claimed to not be enabled.. */
2918 unsigned short sreg;
2919 if ((rc = pcibios_read_config_word (bus, fn, PCI_COMMAND, &sreg))) {
2920 ide_pci_access_error (rc);
2921 } else if (!(sreg & 1)) {
2922 printk("not enabled\n");
2923 } else {
2926 * The first part is undocumented magic from the DOS driver.
2927 * According to the datasheet, there is no port 0x5b on the cmd640.
2929 (void) pcibios_write_config_byte(bus, fn, 0x5b, 0xbd);
2930 if (pcibios_write_config_byte(bus, fn, 0x5b, 0xbd) != 0xbd)
2931 printk("init_cmd640: huh? 0x5b read back wrong\n");
2932 (void) pcibios_write_config_byte(bus, fn, 0x5b, 0);
2933 #endif /* 0 */
2935 * The rest is from the cmd640b datasheet.
2937 if ((rc = pcibios_read_config_byte(bus, fn, 0x51, &reg))
2938 || (rc = pcibios_write_config_byte(bus, fn, 0x51, reg | 0xc0)) /* 0xc8 to enable 2nd i/f */
2939 || (rc = pcibios_read_config_byte(bus, fn, 0x57, &reg))
2940 || (rc = pcibios_write_config_byte(bus, fn, 0x57, reg | 0x0c)))
2941 buggy_interface_fallback (rc);
2942 else
2943 printk("serialized, disabled read-ahead\n");
2945 #endif /* SUPPORT_CMD640 */
2947 typedef void (ide_pci_init_proc_t)(byte, byte);
2950 * ide_probe_pci() scans PCI for a specific vendor/device function,
2951 * and invokes the supplied init routine for each instance detected.
2953 static void ide_probe_pci (unsigned short vendor, unsigned short device, ide_pci_init_proc_t *init, int func_adj)
2955 unsigned long flags;
2956 unsigned index;
2957 byte fn, bus;
2959 save_flags(flags);
2960 cli();
2961 for (index = 0; !pcibios_find_device (vendor, device, index, &bus, &fn); ++index) {
2962 init (bus, fn + func_adj);
2964 restore_flags(flags);
2968 * ide_init_pci() finds/initializes "known" PCI IDE interfaces
2970 * This routine should ideally be using pcibios_find_class() to find
2971 * all IDE interfaces, but that function causes some systems to "go weird".
2973 static void ide_init_pci (void)
2975 #if SUPPORT_RZ1000
2976 ide_probe_pci (PCI_VENDOR_ID_PCTECH, PCI_DEVICE_ID_PCTECH_RZ1000, &init_rz1000, 0);
2977 #endif
2978 #if SUPPORT_CMD640
2979 ide_probe_pci (PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_CMD_640, &init_cmd640, 0);
2980 #endif
2981 #ifdef CONFIG_BLK_DEV_TRITON
2983 * Apparently the BIOS32 services on Intel motherboards are buggy,
2984 * and won't find the PCI_DEVICE_ID_INTEL_82371_1 for us.
2985 * So we instead search for PCI_DEVICE_ID_INTEL_82371_0, and then add 1.
2987 ide_probe_pci (PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371_0, &ide_init_triton, 1);
2988 #endif
2990 #endif /* CONFIG_PCI */
2993 * This is gets invoked once during initialization, to set *everything* up
2995 int ide_init (void)
2997 int h;
2999 init_ide_data ();
3001 * First, we determine what hardware is present
3004 #ifdef CONFIG_PCI
3006 * Find/initialize PCI IDE interfaces
3008 if (pcibios_present())
3009 ide_init_pci ();
3010 #endif /* CONFIG_PCI */
3013 * Probe for drives in the usual way.. CMOS/BIOS, then poke at ports
3015 for (h = 0; h < MAX_HWIFS; ++h) {
3016 ide_hwif_t *hwif = &ide_hwifs[h];
3017 if (!hwif->noprobe) {
3018 if (hwif->io_base == HD_DATA)
3019 probe_cmos_for_drives (hwif);
3020 probe_for_drives (hwif);
3022 if (hwif->present) {
3023 if (!hwif->irq) {
3024 if (!(hwif->irq = default_irqs[h])) {
3025 printk("%s: DISABLED, NO IRQ\n", hwif->name);
3026 hwif->present = 0;
3027 continue;
3030 #ifdef CONFIG_BLK_DEV_HD
3031 if (hwif->irq == HD_IRQ && hwif->io_base != HD_DATA) {
3032 printk("%s: CANNOT SHARE IRQ WITH OLD HARDDISK DRIVER (hd.c)\n", hwif->name);
3033 hwif->present = 0;
3035 #endif /* CONFIG_BLK_DEV_HD */
3040 * Now we try to set up irqs and major devices for what was found
3042 for (h = MAX_HWIFS-1; h >= 0; --h) {
3043 void (*rfn)(void);
3044 ide_hwif_t *hwif = &ide_hwifs[h];
3045 if (!hwif->present)
3046 continue;
3047 hwif->present = 0; /* we set it back to 1 if all is ok below */
3048 switch (hwif->major) {
3049 case IDE0_MAJOR: rfn = &do_ide0_request; break;
3050 case IDE1_MAJOR: rfn = &do_ide1_request; break;
3051 case IDE2_MAJOR: rfn = &do_ide2_request; break;
3052 case IDE3_MAJOR: rfn = &do_ide3_request; break;
3053 default:
3054 printk("%s: request_fn NOT DEFINED\n", hwif->name);
3055 continue;
3057 if (register_blkdev (hwif->major, hwif->name, &ide_fops)) {
3058 printk("%s: UNABLE TO GET MAJOR NUMBER %d\n", hwif->name, hwif->major);
3059 } else if (init_irq (hwif)) {
3060 printk("%s: UNABLE TO GET IRQ %d\n", hwif->name, hwif->irq);
3061 (void) unregister_blkdev (hwif->major, hwif->name);
3062 } else {
3063 init_gendisk(hwif);
3064 blk_dev[hwif->major].request_fn = rfn;
3065 read_ahead[hwif->major] = 8; /* (4kB) */
3066 hwif->present = 1; /* success */
3069 return 0;