pre-2.3.4..
[davej-history.git] / drivers / block / ide.c
blob5263e2b65946d49b30f2f3889394caff085fbe21
1 /*
2 * linux/drivers/block/ide.c Version 6.19 January 29, 1999
4 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
5 */
7 /*
8 * Mostly written by Mark Lord <mlord@pobox.com>
9 * and Gadi Oxman <gadio@netvision.net.il>
11 * See linux/MAINTAINERS for address of current maintainer.
13 * This is the multiple IDE interface driver, as evolved from hd.c.
14 * It supports up to MAX_HWIFS IDE interfaces, on one or more IRQs (usually 14 & 15).
15 * There can be up to two drives per interface, as per the ATA-2 spec.
17 * Primary: ide0, port 0x1f0; major=3; hda is minor=0; hdb is minor=64
18 * Secondary: ide1, port 0x170; major=22; hdc is minor=0; hdd is minor=64
19 * Tertiary: ide2, port 0x???; major=33; hde is minor=0; hdf is minor=64
20 * Quaternary: ide3, port 0x???; major=34; hdg is minor=0; hdh is minor=64
21 * ...
23 * From hd.c:
24 * |
25 * | It traverses the request-list, using interrupts to jump between functions.
26 * | As nearly all functions can be called within interrupts, we may not sleep.
27 * | Special care is recommended. Have Fun!
28 * |
29 * | modified by Drew Eckhardt to check nr of hd's from the CMOS.
30 * |
31 * | Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
32 * | in the early extended-partition checks and added DM partitions.
33 * |
34 * | Early work on error handling by Mika Liljeberg (liljeber@cs.Helsinki.FI).
35 * |
36 * | IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
37 * | and general streamlining by Mark Lord (mlord@pobox.com).
39 * October, 1994 -- Complete line-by-line overhaul for linux 1.1.x, by:
41 * Mark Lord (mlord@pobox.com) (IDE Perf.Pkg)
42 * Delman Lee (delman@mipg.upenn.edu) ("Mr. atdisk2")
43 * Scott Snyder (snyder@fnald0.fnal.gov) (ATAPI IDE cd-rom)
45 * This was a rewrite of just about everything from hd.c, though some original
46 * code is still sprinkled about. Think of it as a major evolution, with
47 * inspiration from lots of linux users, esp. hamish@zot.apana.org.au
49 * Version 1.0 ALPHA initial code, primary i/f working okay
50 * Version 1.3 BETA dual i/f on shared irq tested & working!
51 * Version 1.4 BETA added auto probing for irq(s)
52 * Version 1.5 BETA added ALPHA (untested) support for IDE cd-roms,
53 * ...
54 * Version 5.50 allow values as small as 20 for idebus=
55 * Version 5.51 force non io_32bit in drive_cmd_intr()
56 * change delay_10ms() to delay_50ms() to fix problems
57 * Version 5.52 fix incorrect invalidation of removable devices
58 * add "hdx=slow" command line option
59 * Version 5.60 start to modularize the driver; the disk and ATAPI
60 * drivers can be compiled as loadable modules.
61 * move IDE probe code to ide-probe.c
62 * move IDE disk code to ide-disk.c
63 * add support for generic IDE device subdrivers
64 * add m68k code from Geert Uytterhoeven
65 * probe all interfaces by default
66 * add ioctl to (re)probe an interface
67 * Version 6.00 use per device request queues
68 * attempt to optimize shared hwgroup performance
69 * add ioctl to manually adjust bandwidth algorithms
70 * add kerneld support for the probe module
71 * fix bug in ide_error()
72 * fix bug in the first ide_get_lock() call for Atari
73 * don't flush leftover data for ATAPI devices
74 * Version 6.01 clear hwgroup->active while the hwgroup sleeps
75 * support HDIO_GETGEO for floppies
76 * Version 6.02 fix ide_ack_intr() call
77 * check partition table on floppies
78 * Version 6.03 handle bad status bit sequencing in ide_wait_stat()
79 * Version 6.10 deleted old entries from this list of updates
80 * replaced triton.c with ide-dma.c generic PCI DMA
81 * added support for BIOS-enabled UltraDMA
82 * rename all "promise" things to "pdc4030"
83 * fix EZ-DRIVE handling on small disks
84 * Version 6.11 fix probe error in ide_scan_devices()
85 * fix ancient "jiffies" polling bugs
86 * mask all hwgroup interrupts on each irq entry
87 * Version 6.12 integrate ioctl and proc interfaces
88 * fix parsing of "idex=" command line parameter
89 * Version 6.13 add support for ide4/ide5 courtesy rjones@orchestream.com
90 * Version 6.14 fixed IRQ sharing among PCI devices
91 * Version 6.15 added SMP awareness to IDE drivers
92 * Version 6.16 fixed various bugs; even more SMP friendly
93 * Version 6.17 fix for newest EZ-Drive problem
94 * Version 6.18 default unpartitioned-disk translation now "BIOS LBA"
95 * Version 6.19 Re-design for a UNIFORM driver for all platforms,
96 * model based on suggestions from Russell King and
97 * Geert Uytterhoeven
98 * Promise DC4030VL now supported.
99 * delay_50ms() changed to ide_delay_50ms() and exported.
101 * Some additional driver compile-time options are in ide.h
103 * To do, in likely order of completion:
104 * - modify kernel to obtain BIOS geometry for drives on 2nd/3rd/4th i/f
107 #define REVISION "Revision: 6.19"
108 #define VERSION "Id: ide.c 6.19 1999/01/29"
110 #undef REALLY_SLOW_IO /* most systems can safely undef this */
112 #define _IDE_C /* Tell ide.h it's really us */
114 #include <linux/config.h>
115 #include <linux/module.h>
116 #include <linux/types.h>
117 #include <linux/string.h>
118 #include <linux/kernel.h>
119 #include <linux/timer.h>
120 #include <linux/mm.h>
121 #include <linux/interrupt.h>
122 #include <linux/major.h>
123 #include <linux/errno.h>
124 #include <linux/genhd.h>
125 #include <linux/blkpg.h>
126 #include <linux/malloc.h>
127 #include <linux/pci.h>
128 #include <linux/delay.h>
129 #include <linux/ide.h>
131 #include <asm/byteorder.h>
132 #include <asm/irq.h>
133 #include <asm/uaccess.h>
134 #include <asm/io.h>
135 #include <asm/bitops.h>
137 #include "ide_modes.h"
139 #ifdef CONFIG_KMOD
140 #include <linux/kmod.h>
141 #endif /* CONFIG_KMOD */
143 #ifdef CONFIG_BLK_DEV_VIA82C586
144 extern byte fifoconfig; /* defined in via82c586.c used by ide_setup()*/
145 #endif
147 static const byte ide_hwif_to_major[] = {IDE0_MAJOR, IDE1_MAJOR, IDE2_MAJOR, IDE3_MAJOR, IDE4_MAJOR, IDE5_MAJOR, IDE6_MAJOR, IDE7_MAJOR };
149 static int idebus_parameter; /* holds the "idebus=" parameter */
150 static int system_bus_speed; /* holds what we think is VESA/PCI bus speed */
151 static int initializing; /* set while initializing built-in drivers */
153 #if defined(__mc68000__) || defined(CONFIG_APUS)
155 * ide_lock is used by the Atari code to obtain access to the IDE interrupt,
156 * which is shared between several drivers.
158 static int ide_lock = 0;
159 #endif /* __mc68000__ || CONFIG_APUS */
162 * ide_modules keeps track of the available IDE chipset/probe/driver modules.
164 ide_module_t *ide_modules = NULL;
167 * This is declared extern in ide.h, for access by other IDE modules:
169 ide_hwif_t ide_hwifs[MAX_HWIFS]; /* master data repository */
171 #if (DISK_RECOVERY_TIME > 0)
173 * For really screwy hardware (hey, at least it *can* be used with Linux)
174 * we can enforce a minimum delay time between successive operations.
176 static unsigned long read_timer(void)
178 unsigned long t, flags;
179 int i;
181 __save_flags(flags); /* local CPU only */
182 __cli(); /* local CPU only */
183 t = jiffies * 11932;
184 outb_p(0, 0x43);
185 i = inb_p(0x40);
186 i |= inb(0x40) << 8;
187 __restore_flags(flags); /* local CPU only */
188 return (t - i);
190 #endif /* DISK_RECOVERY_TIME */
192 static inline void set_recovery_timer (ide_hwif_t *hwif)
194 #if (DISK_RECOVERY_TIME > 0)
195 hwif->last_time = read_timer();
196 #endif /* DISK_RECOVERY_TIME */
200 * Do not even *think* about calling this!
202 static void init_hwif_data (unsigned int index)
204 unsigned int unit;
205 hw_regs_t hw;
206 ide_hwif_t *hwif = &ide_hwifs[index];
208 /* bulk initialize hwif & drive info with zeros */
209 memset(hwif, 0, sizeof(ide_hwif_t));
210 memset(&hw, 0, sizeof(hw_regs_t));
212 /* fill in any non-zero initial values */
213 hwif->index = index;
214 ide_init_hwif_ports(&hw, ide_default_io_base(index), 0, &hwif->irq);
215 memcpy(&hwif->hw, &hw, sizeof(hw));
216 memcpy(hwif->io_ports, hw.io_ports, sizeof(hw.io_ports));
217 hwif->noprobe = !hwif->io_ports[IDE_DATA_OFFSET];
218 #ifdef CONFIG_BLK_DEV_HD
219 if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA)
220 hwif->noprobe = 1; /* may be overridden by ide_setup() */
221 #endif /* CONFIG_BLK_DEV_HD */
222 hwif->major = ide_hwif_to_major[index];
223 hwif->name[0] = 'i';
224 hwif->name[1] = 'd';
225 hwif->name[2] = 'e';
226 hwif->name[3] = '0' + index;
227 for (unit = 0; unit < MAX_DRIVES; ++unit) {
228 ide_drive_t *drive = &hwif->drives[unit];
230 drive->media = ide_disk;
231 drive->select.all = (unit<<4)|0xa0;
232 drive->hwif = hwif;
233 drive->ctl = 0x08;
234 drive->ready_stat = READY_STAT;
235 drive->bad_wstat = BAD_W_STAT;
236 drive->special.b.recalibrate = 1;
237 drive->special.b.set_geometry = 1;
238 drive->name[0] = 'h';
239 drive->name[1] = 'd';
240 drive->name[2] = 'a' + (index * MAX_DRIVES) + unit;
241 init_waitqueue_head(&drive->wqueue);
246 * init_ide_data() sets reasonable default values into all fields
247 * of all instances of the hwifs and drives, but only on the first call.
248 * Subsequent calls have no effect (they don't wipe out anything).
250 * This routine is normally called at driver initialization time,
251 * but may also be called MUCH earlier during kernel "command-line"
252 * parameter processing. As such, we cannot depend on any other parts
253 * of the kernel (such as memory allocation) to be functioning yet.
255 * This is too bad, as otherwise we could dynamically allocate the
256 * ide_drive_t structs as needed, rather than always consuming memory
257 * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
259 #define MAGIC_COOKIE 0x12345678
260 static void init_ide_data (void)
262 unsigned int index;
263 static unsigned long magic_cookie = MAGIC_COOKIE;
265 if (magic_cookie != MAGIC_COOKIE)
266 return; /* already initialized */
267 magic_cookie = 0;
269 /* Initialise all interface structures */
270 for (index = 0; index < MAX_HWIFS; ++index)
271 init_hwif_data(index);
273 /* Add default hw interfaces */
274 ide_init_default_hwifs();
276 idebus_parameter = 0;
277 system_bus_speed = 0;
281 * CompactFlash cards and their brethern pretend to be removable hard disks, except:
282 * (1) they never have a slave unit, and
283 * (2) they don't have doorlock mechanisms.
284 * This test catches them, and is invoked elsewhere when setting appropriate config bits.
286 * FIXME: This treatment is probably applicable for *all* PCMCIA (PC CARD) devices,
287 * so in linux 2.3.x we should change this to just treat all PCMCIA drives this way,
288 * and get rid of the model-name tests below (too big of an interface change for 2.2.x).
289 * At that time, we might also consider parameterizing the timeouts and retries,
290 * since these are MUCH faster than mechanical drives. -M.Lord
292 int drive_is_flashcard (ide_drive_t *drive)
294 struct hd_driveid *id = drive->id;
296 if (drive->removable && id != NULL) {
297 if (!strncmp(id->model, "KODAK ATA_FLASH", 15) /* Kodak */
298 || !strncmp(id->model, "Hitachi CV", 10) /* Hitachi */
299 || !strncmp(id->model, "SunDisk SDCFB", 13)) /* SunDisk */
301 return 1; /* yes, it is a flash memory card */
304 return 0; /* no, it is not a flash memory card */
308 * ide_system_bus_speed() returns what we think is the system VESA/PCI
309 * bus speed (in MHz). This is used for calculating interface PIO timings.
310 * The default is 40 for known PCI systems, 50 otherwise.
311 * The "idebus=xx" parameter can be used to override this value.
312 * The actual value to be used is computed/displayed the first time through.
314 int ide_system_bus_speed (void)
316 if (!system_bus_speed) {
317 if (idebus_parameter)
318 system_bus_speed = idebus_parameter; /* user supplied value */
319 #ifdef CONFIG_PCI
320 else if (pci_present())
321 system_bus_speed = 40; /* safe default value for PCI */
322 #endif /* CONFIG_PCI */
323 else
324 system_bus_speed = 50; /* safe default value for VESA and PCI */
325 printk("ide: Assuming %dMHz system bus speed for PIO modes%s\n", system_bus_speed,
326 idebus_parameter ? "" : "; override with idebus=xx");
328 return system_bus_speed;
331 #if SUPPORT_VLB_SYNC
333 * Some localbus EIDE interfaces require a special access sequence
334 * when using 32-bit I/O instructions to transfer data. We call this
335 * the "vlb_sync" sequence, which consists of three successive reads
336 * of the sector count register location, with interrupts disabled
337 * to ensure that the reads all happen together.
339 static inline void do_vlb_sync (ide_ioreg_t port) {
340 (void) inb (port);
341 (void) inb (port);
342 (void) inb (port);
344 #endif /* SUPPORT_VLB_SYNC */
347 * This is used for most PIO data transfers *from* the IDE interface
349 void ide_input_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
351 byte io_32bit = drive->io_32bit;
353 if (io_32bit) {
354 #if SUPPORT_VLB_SYNC
355 if (io_32bit & 2) {
356 unsigned long flags;
357 __save_flags(flags); /* local CPU only */
358 __cli(); /* local CPU only */
359 do_vlb_sync(IDE_NSECTOR_REG);
360 insl(IDE_DATA_REG, buffer, wcount);
361 __restore_flags(flags); /* local CPU only */
362 } else
363 #endif /* SUPPORT_VLB_SYNC */
364 insl(IDE_DATA_REG, buffer, wcount);
365 } else {
366 #if SUPPORT_SLOW_DATA_PORTS
367 if (drive->slow) {
368 unsigned short *ptr = (unsigned short *) buffer;
369 while (wcount--) {
370 *ptr++ = inw_p(IDE_DATA_REG);
371 *ptr++ = inw_p(IDE_DATA_REG);
373 } else
374 #endif /* SUPPORT_SLOW_DATA_PORTS */
375 insw(IDE_DATA_REG, buffer, wcount<<1);
380 * This is used for most PIO data transfers *to* the IDE interface
382 void ide_output_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
384 byte io_32bit = drive->io_32bit;
386 if (io_32bit) {
387 #if SUPPORT_VLB_SYNC
388 if (io_32bit & 2) {
389 unsigned long flags;
390 __save_flags(flags); /* local CPU only */
391 __cli(); /* local CPU only */
392 do_vlb_sync(IDE_NSECTOR_REG);
393 outsl(IDE_DATA_REG, buffer, wcount);
394 __restore_flags(flags); /* local CPU only */
395 } else
396 #endif /* SUPPORT_VLB_SYNC */
397 outsl(IDE_DATA_REG, buffer, wcount);
398 } else {
399 #if SUPPORT_SLOW_DATA_PORTS
400 if (drive->slow) {
401 unsigned short *ptr = (unsigned short *) buffer;
402 while (wcount--) {
403 outw_p(*ptr++, IDE_DATA_REG);
404 outw_p(*ptr++, IDE_DATA_REG);
406 } else
407 #endif /* SUPPORT_SLOW_DATA_PORTS */
408 outsw(IDE_DATA_REG, buffer, wcount<<1);
413 * The following routines are mainly used by the ATAPI drivers.
415 * These routines will round up any request for an odd number of bytes,
416 * so if an odd bytecount is specified, be sure that there's at least one
417 * extra byte allocated for the buffer.
419 void atapi_input_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount)
421 ++bytecount;
422 #ifdef CONFIG_ATARI
423 if (MACH_IS_ATARI) {
424 /* Atari has a byte-swapped IDE interface */
425 insw_swapw(IDE_DATA_REG, buffer, bytecount / 2);
426 return;
428 #endif /* CONFIG_ATARI */
429 ide_input_data (drive, buffer, bytecount / 4);
430 if ((bytecount & 0x03) >= 2)
431 insw (IDE_DATA_REG, ((byte *)buffer) + (bytecount & ~0x03), 1);
434 void atapi_output_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount)
436 ++bytecount;
437 #ifdef CONFIG_ATARI
438 if (MACH_IS_ATARI) {
439 /* Atari has a byte-swapped IDE interface */
440 outsw_swapw(IDE_DATA_REG, buffer, bytecount / 2);
441 return;
443 #endif /* CONFIG_ATARI */
444 ide_output_data (drive, buffer, bytecount / 4);
445 if ((bytecount & 0x03) >= 2)
446 outsw (IDE_DATA_REG, ((byte *)buffer) + (bytecount & ~0x03), 1);
450 * Needed for PCI irq sharing
452 static inline int drive_is_ready (ide_drive_t *drive)
454 if (drive->waiting_for_dma)
455 return HWIF(drive)->dmaproc(ide_dma_test_irq, drive);
456 #if 0
457 udelay(1); /* need to guarantee 400ns since last command was issued */
458 #endif
459 if (GET_STAT() & BUSY_STAT)
460 return 0; /* drive busy: definitely not interrupting */
461 return 1; /* drive ready: *might* be interrupting */
465 * This is our end_request replacement function.
467 void ide_end_request(byte uptodate, ide_hwgroup_t *hwgroup)
469 struct request *rq;
470 unsigned long flags;
472 spin_lock_irqsave(&io_request_lock, flags);
473 rq = hwgroup->rq;
475 if (!end_that_request_first(rq, uptodate, hwgroup->drive->name)) {
476 add_blkdev_randomness(MAJOR(rq->rq_dev));
477 hwgroup->drive->queue = rq->next;
478 blk_dev[MAJOR(rq->rq_dev)].current_request = NULL;
479 hwgroup->rq = NULL;
480 end_that_request_last(rq);
482 spin_unlock_irqrestore(&io_request_lock, flags);
486 * This should get invoked any time we exit the driver to
487 * wait for an interrupt response from a drive. handler() points
488 * at the appropriate code to handle the next interrupt, and a
489 * timer is started to prevent us from waiting forever in case
490 * something goes wrong (see the ide_timer_expiry() handler later on).
492 void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler, unsigned int timeout)
494 unsigned long flags;
495 ide_hwgroup_t *hwgroup = HWGROUP(drive);
497 spin_lock_irqsave(&hwgroup->spinlock, flags);
498 #ifdef DEBUG
499 if (hwgroup->handler != NULL) {
500 printk("%s: ide_set_handler: handler not null; old=%p, new=%p\n",
501 drive->name, hwgroup->handler, handler);
503 #endif
504 hwgroup->handler = handler;
505 hwgroup->timer.expires = jiffies + timeout;
506 add_timer(&(hwgroup->timer));
507 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
511 * current_capacity() returns the capacity (in sectors) of a drive
512 * according to its current geometry/LBA settings.
514 static unsigned long current_capacity (ide_drive_t *drive)
516 if (!drive->present)
517 return 0;
518 if (drive->driver != NULL)
519 return DRIVER(drive)->capacity(drive);
520 return 0;
524 * ide_geninit() is called exactly *once* for each major, from genhd.c,
525 * at the beginning of the initial partition check for the drives.
527 void ide_geninit (struct gendisk *gd)
529 unsigned int unit;
530 ide_hwif_t *hwif = gd->real_devices;
532 for (unit = 0; unit < gd->nr_real; ++unit) {
533 ide_drive_t *drive = &hwif->drives[unit];
535 drive->part[0].nr_sects = current_capacity(drive);
536 if (!drive->present || (drive->media != ide_disk && drive->media != ide_floppy) ||
537 drive->driver == NULL || !drive->part[0].nr_sects)
538 drive->part[0].start_sect = -1; /* skip partition check */
542 static void do_reset1 (ide_drive_t *, int); /* needed below */
545 * atapi_reset_pollfunc() gets invoked to poll the interface for completion every 50ms
546 * during an atapi drive reset operation. If the drive has not yet responded,
547 * and we have not yet hit our maximum waiting time, then the timer is restarted
548 * for another 50ms.
550 static void atapi_reset_pollfunc (ide_drive_t *drive)
552 ide_hwgroup_t *hwgroup = HWGROUP(drive);
553 byte stat;
555 SELECT_DRIVE(HWIF(drive),drive);
556 udelay (10);
558 if (OK_STAT(stat=GET_STAT(), 0, BUSY_STAT)) {
559 printk("%s: ATAPI reset complete\n", drive->name);
560 } else {
561 if (0 < (signed long)(hwgroup->poll_timeout - jiffies)) {
562 ide_set_handler (drive, &atapi_reset_pollfunc, HZ/20);
563 return; /* continue polling */
565 hwgroup->poll_timeout = 0; /* end of polling */
566 printk("%s: ATAPI reset timed-out, status=0x%02x\n", drive->name, stat);
567 do_reset1 (drive, 1); /* do it the old fashioned way */
568 return;
570 hwgroup->poll_timeout = 0; /* done polling */
574 * reset_pollfunc() gets invoked to poll the interface for completion every 50ms
575 * during an ide reset operation. If the drives have not yet responded,
576 * and we have not yet hit our maximum waiting time, then the timer is restarted
577 * for another 50ms.
579 static void reset_pollfunc (ide_drive_t *drive)
581 ide_hwgroup_t *hwgroup = HWGROUP(drive);
582 ide_hwif_t *hwif = HWIF(drive);
583 byte tmp;
585 if (!OK_STAT(tmp=GET_STAT(), 0, BUSY_STAT)) {
586 if (0 < (signed long)(hwgroup->poll_timeout - jiffies)) {
587 ide_set_handler (drive, &reset_pollfunc, HZ/20);
588 return; /* continue polling */
590 printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
591 } else {
592 printk("%s: reset: ", hwif->name);
593 if ((tmp = GET_ERR()) == 1)
594 printk("success\n");
595 else {
596 #if FANCY_STATUS_DUMPS
597 printk("master: ");
598 switch (tmp & 0x7f) {
599 case 1: printk("passed");
600 break;
601 case 2: printk("formatter device error");
602 break;
603 case 3: printk("sector buffer error");
604 break;
605 case 4: printk("ECC circuitry error");
606 break;
607 case 5: printk("controlling MPU error");
608 break;
609 default:printk("error (0x%02x?)", tmp);
611 if (tmp & 0x80)
612 printk("; slave: failed");
613 printk("\n");
614 #else
615 printk("failed\n");
616 #endif /* FANCY_STATUS_DUMPS */
619 hwgroup->poll_timeout = 0; /* done polling */
622 static void pre_reset (ide_drive_t *drive)
624 if (!drive->keep_settings) {
625 drive->unmask = 0;
626 drive->io_32bit = 0;
627 if (drive->using_dma)
628 (void) HWIF(drive)->dmaproc(ide_dma_off, drive);
630 if (drive->driver != NULL)
631 DRIVER(drive)->pre_reset(drive);
635 * do_reset1() attempts to recover a confused drive by resetting it.
636 * Unfortunately, resetting a disk drive actually resets all devices on
637 * the same interface, so it can really be thought of as resetting the
638 * interface rather than resetting the drive.
640 * ATAPI devices have their own reset mechanism which allows them to be
641 * individually reset without clobbering other devices on the same interface.
643 * Unfortunately, the IDE interface does not generate an interrupt to let
644 * us know when the reset operation has finished, so we must poll for this.
645 * Equally poor, though, is the fact that this may a very long time to complete,
646 * (up to 30 seconds worstcase). So, instead of busy-waiting here for it,
647 * we set a timer to poll at 50ms intervals.
649 static void do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
651 unsigned int unit;
652 unsigned long flags;
653 ide_hwif_t *hwif = HWIF(drive);
654 ide_hwgroup_t *hwgroup = HWGROUP(drive);
656 __save_flags(flags); /* local CPU only */
657 __cli(); /* local CPU only */
659 /* For an ATAPI device, first try an ATAPI SRST. */
660 if (drive->media != ide_disk && !do_not_try_atapi) {
661 pre_reset(drive);
662 SELECT_DRIVE(hwif,drive);
663 udelay (20);
664 OUT_BYTE (WIN_SRST, IDE_COMMAND_REG);
665 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
666 ide_set_handler (drive, &atapi_reset_pollfunc, HZ/20);
667 __restore_flags (flags); /* local CPU only */
668 return;
672 * First, reset any device state data we were maintaining
673 * for any of the drives on this interface.
675 for (unit = 0; unit < MAX_DRIVES; ++unit)
676 pre_reset(&hwif->drives[unit]);
678 #if OK_TO_RESET_CONTROLLER
679 if (!IDE_CONTROL_REG) {
680 __restore_flags(flags);
681 return;
684 * Note that we also set nIEN while resetting the device,
685 * to mask unwanted interrupts from the interface during the reset.
686 * However, due to the design of PC hardware, this will cause an
687 * immediate interrupt due to the edge transition it produces.
688 * This single interrupt gives us a "fast poll" for drives that
689 * recover from reset very quickly, saving us the first 50ms wait time.
691 OUT_BYTE(drive->ctl|6,IDE_CONTROL_REG); /* set SRST and nIEN */
692 udelay(10); /* more than enough time */
693 OUT_BYTE(drive->ctl|2,IDE_CONTROL_REG); /* clear SRST, leave nIEN */
694 udelay(10); /* more than enough time */
695 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
696 ide_set_handler (drive, &reset_pollfunc, HZ/20);
697 #endif /* OK_TO_RESET_CONTROLLER */
699 __restore_flags (flags); /* local CPU only */
703 * ide_do_reset() is the entry point to the drive/interface reset code.
705 void ide_do_reset (ide_drive_t *drive)
707 do_reset1 (drive, 0);
711 * Clean up after success/failure of an explicit drive cmd
713 void ide_end_drive_cmd (ide_drive_t *drive, byte stat, byte err)
715 unsigned long flags;
716 struct request *rq = HWGROUP(drive)->rq;
718 if (rq->cmd == IDE_DRIVE_CMD) {
719 byte *args = (byte *) rq->buffer;
720 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
721 if (args) {
722 args[0] = stat;
723 args[1] = err;
724 args[2] = IN_BYTE(IDE_NSECTOR_REG);
727 spin_lock_irqsave(&io_request_lock, flags);
728 drive->queue = rq->next;
729 blk_dev[MAJOR(rq->rq_dev)].current_request = NULL;
730 HWGROUP(drive)->rq = NULL;
731 rq->rq_status = RQ_INACTIVE;
732 spin_unlock_irqrestore(&io_request_lock, flags);
733 save_flags(flags); /* all CPUs; overkill? */
734 cli(); /* all CPUs; overkill? */
735 if (rq->sem != NULL)
736 up(rq->sem); /* inform originator that rq has been serviced */
737 restore_flags(flags); /* all CPUs; overkill? */
741 * Error reporting, in human readable form (luxurious, but a memory hog).
743 byte ide_dump_status (ide_drive_t *drive, const char *msg, byte stat)
745 unsigned long flags;
746 byte err = 0;
748 __save_flags (flags); /* local CPU only */
749 ide__sti(); /* local CPU only */
750 printk("%s: %s: status=0x%02x", drive->name, msg, stat);
751 #if FANCY_STATUS_DUMPS
752 printk(" { ");
753 if (stat & BUSY_STAT)
754 printk("Busy ");
755 else {
756 if (stat & READY_STAT) printk("DriveReady ");
757 if (stat & WRERR_STAT) printk("DeviceFault ");
758 if (stat & SEEK_STAT) printk("SeekComplete ");
759 if (stat & DRQ_STAT) printk("DataRequest ");
760 if (stat & ECC_STAT) printk("CorrectedError ");
761 if (stat & INDEX_STAT) printk("Index ");
762 if (stat & ERR_STAT) printk("Error ");
764 printk("}");
765 #endif /* FANCY_STATUS_DUMPS */
766 printk("\n");
767 if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
768 err = GET_ERR();
769 printk("%s: %s: error=0x%02x", drive->name, msg, err);
770 #if FANCY_STATUS_DUMPS
771 if (drive->media == ide_disk) {
772 printk(" { ");
773 if (err & ABRT_ERR) printk("DriveStatusError ");
774 if (err & ICRC_ERR) printk((err & ABRT_ERR) ? "BadCRC " : "BadSector ");
775 if (err & ECC_ERR) printk("UncorrectableError ");
776 if (err & ID_ERR) printk("SectorIdNotFound ");
777 if (err & TRK0_ERR) printk("TrackZeroNotFound ");
778 if (err & MARK_ERR) printk("AddrMarkNotFound ");
779 printk("}");
780 if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR || (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
781 byte cur = IN_BYTE(IDE_SELECT_REG);
782 if (cur & 0x40) { /* using LBA? */
783 printk(", LBAsect=%ld", (unsigned long)
784 ((cur&0xf)<<24)
785 |(IN_BYTE(IDE_HCYL_REG)<<16)
786 |(IN_BYTE(IDE_LCYL_REG)<<8)
787 | IN_BYTE(IDE_SECTOR_REG));
788 } else {
789 printk(", CHS=%d/%d/%d",
790 (IN_BYTE(IDE_HCYL_REG)<<8) +
791 IN_BYTE(IDE_LCYL_REG),
792 cur & 0xf,
793 IN_BYTE(IDE_SECTOR_REG));
795 if (HWGROUP(drive)->rq)
796 printk(", sector=%ld", HWGROUP(drive)->rq->sector);
799 #endif /* FANCY_STATUS_DUMPS */
800 printk("\n");
802 __restore_flags (flags); /* local CPU only */
803 return err;
807 * try_to_flush_leftover_data() is invoked in response to a drive
808 * unexpectedly having its DRQ_STAT bit set. As an alternative to
809 * resetting the drive, this routine tries to clear the condition
810 * by read a sector's worth of data from the drive. Of course,
811 * this may not help if the drive is *waiting* for data from *us*.
813 static void try_to_flush_leftover_data (ide_drive_t *drive)
815 int i = (drive->mult_count ? drive->mult_count : 1) * SECTOR_WORDS;
817 if (drive->media != ide_disk)
818 return;
819 while (i > 0) {
820 unsigned long buffer[16];
821 unsigned int wcount = (i > 16) ? 16 : i;
822 i -= wcount;
823 ide_input_data (drive, buffer, wcount);
828 * ide_error() takes action based on the error returned by the drive.
830 void ide_error (ide_drive_t *drive, const char *msg, byte stat)
832 struct request *rq;
833 byte err;
835 err = ide_dump_status(drive, msg, stat);
836 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
837 return;
838 /* retry only "normal" I/O: */
839 if (rq->cmd == IDE_DRIVE_CMD) {
840 rq->errors = 1;
841 ide_end_drive_cmd(drive, stat, err);
842 return;
844 if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) { /* other bits are useless when BUSY */
845 rq->errors |= ERROR_RESET;
846 } else {
847 if (drive->media == ide_disk && (stat & ERR_STAT)) {
848 /* err has different meaning on cdrom and tape */
849 if (err == ABRT_ERR) {
850 if (drive->select.b.lba && IN_BYTE(IDE_COMMAND_REG) == WIN_SPECIFY)
851 return; /* some newer drives don't support WIN_SPECIFY */
852 } else if ((err & (ABRT_ERR | ICRC_ERR)) == (ABRT_ERR | ICRC_ERR))
853 ; /* UDMA crc error -- just retry the operation */
854 else if (err & (BBD_ERR | ECC_ERR)) /* retries won't help these */
855 rq->errors = ERROR_MAX;
856 else if (err & TRK0_ERR) /* help it find track zero */
857 rq->errors |= ERROR_RECAL;
859 if ((stat & DRQ_STAT) && rq->cmd != WRITE)
860 try_to_flush_leftover_data(drive);
862 if (GET_STAT() & (BUSY_STAT|DRQ_STAT))
863 rq->errors |= ERROR_RESET; /* Mmmm.. timing problem */
865 if (rq->errors >= ERROR_MAX) {
866 if (drive->driver != NULL)
867 DRIVER(drive)->end_request(0, HWGROUP(drive));
868 else
869 ide_end_request(0, HWGROUP(drive));
870 } else {
871 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
872 ++rq->errors;
873 ide_do_reset(drive);
874 return;
875 } else if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
876 drive->special.b.recalibrate = 1;
877 ++rq->errors;
882 * Issue a simple drive command
883 * The drive must be selected beforehand.
885 void ide_cmd(ide_drive_t *drive, byte cmd, byte nsect, ide_handler_t *handler)
887 ide_set_handler (drive, handler, WAIT_CMD);
888 if (IDE_CONTROL_REG)
889 OUT_BYTE(drive->ctl,IDE_CONTROL_REG); /* clear nIEN */
890 OUT_BYTE(nsect,IDE_NSECTOR_REG);
891 OUT_BYTE(cmd,IDE_COMMAND_REG);
895 * drive_cmd_intr() is invoked on completion of a special DRIVE_CMD.
897 static void drive_cmd_intr (ide_drive_t *drive)
899 struct request *rq = HWGROUP(drive)->rq;
900 byte *args = (byte *) rq->buffer;
901 byte stat = GET_STAT();
902 int retries = 10;
904 ide__sti(); /* local CPU only */
905 if ((stat & DRQ_STAT) && args && args[3]) {
906 byte io_32bit = drive->io_32bit;
907 drive->io_32bit = 0;
908 ide_input_data(drive, &args[4], args[3] * SECTOR_WORDS);
909 drive->io_32bit = io_32bit;
910 while (((stat = GET_STAT()) & BUSY_STAT) && retries--)
911 udelay(100);
914 if (OK_STAT(stat, READY_STAT, BAD_STAT))
915 ide_end_drive_cmd (drive, stat, GET_ERR());
916 else
917 ide_error(drive, "drive_cmd", stat); /* calls ide_end_drive_cmd */
921 * do_special() is used to issue WIN_SPECIFY, WIN_RESTORE, and WIN_SETMULT
922 * commands to a drive. It used to do much more, but has been scaled back.
924 static inline void do_special (ide_drive_t *drive)
926 special_t *s = &drive->special;
928 #ifdef DEBUG
929 printk("%s: do_special: 0x%02x\n", drive->name, s->all);
930 #endif
931 if (s->b.set_tune) {
932 ide_tuneproc_t *tuneproc = HWIF(drive)->tuneproc;
933 s->b.set_tune = 0;
934 if (tuneproc != NULL)
935 tuneproc(drive, drive->tune_req);
936 } else if (drive->driver != NULL) {
937 DRIVER(drive)->special(drive);
938 } else if (s->all) {
939 printk("%s: bad special flag: 0x%02x\n", drive->name, s->all);
940 s->all = 0;
945 * This routine busy-waits for the drive status to be not "busy".
946 * It then checks the status for all of the "good" bits and none
947 * of the "bad" bits, and if all is okay it returns 0. All other
948 * cases return 1 after invoking ide_error() -- caller should just return.
950 * This routine should get fixed to not hog the cpu during extra long waits..
951 * That could be done by busy-waiting for the first jiffy or two, and then
952 * setting a timer to wake up at half second intervals thereafter,
953 * until timeout is achieved, before timing out.
955 int ide_wait_stat (ide_drive_t *drive, byte good, byte bad, unsigned long timeout)
957 byte stat;
958 int i;
959 unsigned long flags;
961 udelay(1); /* spec allows drive 400ns to assert "BUSY" */
962 if ((stat = GET_STAT()) & BUSY_STAT) {
963 __save_flags(flags); /* local CPU only */
964 ide__sti(); /* local CPU only */
965 timeout += jiffies;
966 while ((stat = GET_STAT()) & BUSY_STAT) {
967 if (0 < (signed long)(jiffies - timeout)) {
968 __restore_flags(flags); /* local CPU only */
969 ide_error(drive, "status timeout", stat);
970 return 1;
973 __restore_flags(flags); /* local CPU only */
976 * Allow status to settle, then read it again.
977 * A few rare drives vastly violate the 400ns spec here,
978 * so we'll wait up to 10usec for a "good" status
979 * rather than expensively fail things immediately.
980 * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
982 for (i = 0; i < 10; i++) {
983 udelay(1);
984 if (OK_STAT((stat = GET_STAT()), good, bad))
985 return 0;
987 ide_error(drive, "status error", stat);
988 return 1;
992 * execute_drive_cmd() issues a special drive command,
993 * usually initiated by ioctl() from the external hdparm program.
995 static void execute_drive_cmd (ide_drive_t *drive, struct request *rq)
997 byte *args = rq->buffer;
998 if (args) {
999 #ifdef DEBUG
1000 printk("%s: DRIVE_CMD cmd=0x%02x sc=0x%02x fr=0x%02x xx=0x%02x\n",
1001 drive->name, args[0], args[1], args[2], args[3]);
1002 #endif
1003 if (args[0] == WIN_SMART) {
1004 OUT_BYTE(0x4f, IDE_LCYL_REG);
1005 OUT_BYTE(0xc2, IDE_HCYL_REG);
1007 OUT_BYTE(args[2],IDE_FEATURE_REG);
1008 ide_cmd(drive, args[0], args[1], &drive_cmd_intr);
1009 return;
1010 } else {
1012 * NULL is actually a valid way of waiting for
1013 * all current requests to be flushed from the queue.
1015 #ifdef DEBUG
1016 printk("%s: DRIVE_CMD (null)\n", drive->name);
1017 #endif
1018 ide_end_drive_cmd(drive, GET_STAT(), GET_ERR());
1019 return;
1024 * start_request() initiates handling of a new I/O request
1026 static inline void start_request (ide_drive_t *drive)
1028 unsigned long block, blockend;
1029 struct request *rq = drive->queue;
1030 unsigned int minor = MINOR(rq->rq_dev), unit = minor >> PARTN_BITS;
1031 ide_hwif_t *hwif = HWIF(drive);
1033 ide__sti(); /* local CPU only */
1034 #ifdef DEBUG
1035 printk("%s: start_request: current=0x%08lx\n", hwif->name, (unsigned long) rq);
1036 #endif
1037 if (unit >= MAX_DRIVES) {
1038 printk("%s: bad device number: %s\n", hwif->name, kdevname(rq->rq_dev));
1039 goto kill_rq;
1041 #ifdef DEBUG
1042 if (rq->bh && !buffer_locked(rq->bh)) {
1043 printk("%s: block not locked\n", drive->name);
1044 goto kill_rq;
1046 #endif
1047 block = rq->sector;
1048 blockend = block + rq->nr_sectors;
1049 if ((blockend < block) || (blockend > drive->part[minor&PARTN_MASK].nr_sects)) {
1050 printk("%s%c: bad access: block=%ld, count=%ld\n", drive->name,
1051 (minor&PARTN_MASK)?'0'+(minor&PARTN_MASK):' ', block, rq->nr_sectors);
1052 goto kill_rq;
1054 block += drive->part[minor&PARTN_MASK].start_sect + drive->sect0;
1055 #if FAKE_FDISK_FOR_EZDRIVE
1056 if (block == 0 && drive->remap_0_to_1)
1057 block = 1; /* redirect MBR access to EZ-Drive partn table */
1058 #endif /* FAKE_FDISK_FOR_EZDRIVE */
1059 #if (DISK_RECOVERY_TIME > 0)
1060 while ((read_timer() - hwif->last_time) < DISK_RECOVERY_TIME);
1061 #endif
1062 SELECT_DRIVE(hwif, drive);
1063 if (ide_wait_stat(drive, drive->ready_stat, BUSY_STAT|DRQ_STAT, WAIT_READY)) {
1064 printk("%s: drive not ready for command\n", drive->name);
1065 return;
1067 if (!drive->special.all) {
1068 if (rq->cmd == IDE_DRIVE_CMD) {
1069 execute_drive_cmd(drive, rq);
1070 return;
1072 if (drive->driver != NULL) {
1073 DRIVER(drive)->do_request(drive, rq, block);
1074 return;
1076 printk("%s: media type %d not supported\n", drive->name, drive->media);
1077 goto kill_rq;
1079 do_special(drive);
1080 return;
1081 kill_rq:
1082 if (drive->driver != NULL)
1083 DRIVER(drive)->end_request(0, HWGROUP(drive));
1084 else
1085 ide_end_request(0, HWGROUP(drive));
1089 * ide_stall_queue() can be used by a drive to give excess bandwidth back
1090 * to the hwgroup by sleeping for timeout jiffies.
1092 void ide_stall_queue (ide_drive_t *drive, unsigned long timeout)
1094 if (timeout > WAIT_WORSTCASE)
1095 timeout = WAIT_WORSTCASE;
1096 drive->sleep = timeout + jiffies;
1099 #define WAKEUP(drive) ((drive)->service_start + 2 * (drive)->service_time)
1102 * choose_drive() selects the next drive which will be serviced.
1104 static inline ide_drive_t *choose_drive (ide_hwgroup_t *hwgroup)
1106 ide_drive_t *drive, *best;
1108 repeat:
1109 best = NULL;
1110 drive = hwgroup->drive;
1111 do {
1112 if (drive->queue && (!drive->sleep || 0 <= (signed long)(jiffies - drive->sleep))) {
1113 if (!best
1114 || (drive->sleep && (!best->sleep || 0 < (signed long)(best->sleep - drive->sleep)))
1115 || (!best->sleep && 0 < (signed long)(WAKEUP(best) - WAKEUP(drive))))
1117 struct blk_dev_struct *bdev = &blk_dev[HWIF(drive)->major];
1118 if (bdev->current_request != &bdev->plug)
1119 best = drive;
1122 } while ((drive = drive->next) != hwgroup->drive);
1123 if (best && best->nice1 && !best->sleep && best != hwgroup->drive && best->service_time > WAIT_MIN_SLEEP) {
1124 long t = (signed long)(WAKEUP(best) - jiffies);
1125 if (t >= WAIT_MIN_SLEEP) {
1127 * We *may* have some time to spare, but first let's see if
1128 * someone can potentially benefit from our nice mood today..
1130 drive = best->next;
1131 do {
1132 if (!drive->sleep
1133 && 0 < (signed long)(WAKEUP(drive) - (jiffies - best->service_time))
1134 && 0 < (signed long)((jiffies + t) - WAKEUP(drive)))
1136 ide_stall_queue(best, IDE_MIN(t, 10 * WAIT_MIN_SLEEP));
1137 goto repeat;
1139 } while ((drive = drive->next) != best);
1142 return best;
1146 * Caller must have already acquired spinlock using *spinflags
1148 static void ide_do_request (ide_hwgroup_t *hwgroup, unsigned long *hwgroup_flags, int masked_irq)
1150 struct blk_dev_struct *bdev;
1151 ide_drive_t *drive;
1152 ide_hwif_t *hwif;
1153 unsigned long io_flags;
1155 hwgroup->busy = 1;
1156 while (hwgroup->handler == NULL) {
1157 spin_lock_irqsave(&io_request_lock, io_flags);
1158 drive = choose_drive(hwgroup);
1159 if (drive == NULL) {
1160 unsigned long sleep = 0;
1162 hwgroup->rq = NULL;
1163 drive = hwgroup->drive;
1164 do {
1165 bdev = &blk_dev[HWIF(drive)->major];
1166 if (bdev->current_request != &bdev->plug) /* FIXME: this will do for now */
1167 bdev->current_request = NULL; /* (broken since patch-2.1.15) */
1168 if (drive->sleep && (!sleep || 0 < (signed long)(sleep - drive->sleep)))
1169 sleep = drive->sleep;
1170 } while ((drive = drive->next) != hwgroup->drive);
1171 spin_unlock_irqrestore(&io_request_lock, io_flags);
1172 if (sleep) {
1173 if (0 < (signed long)(jiffies + WAIT_MIN_SLEEP - sleep))
1174 sleep = jiffies + WAIT_MIN_SLEEP;
1175 #if 1
1176 if (hwgroup->timer.next || hwgroup->timer.prev)
1177 printk("ide_set_handler: timer already active\n");
1178 #endif
1179 mod_timer(&hwgroup->timer, sleep);
1180 } else {
1181 /* Ugly, but how can we sleep for the lock otherwise? perhaps from tq_scheduler? */
1182 ide_release_lock(&ide_lock); /* for atari only */
1184 hwgroup->busy = 0;
1185 return;
1187 hwif = HWIF(drive);
1188 if (hwgroup->hwif->sharing_irq && hwif != hwgroup->hwif) /* set nIEN for previous hwif */
1189 OUT_BYTE(hwgroup->drive->ctl|2, hwgroup->hwif->io_ports[IDE_CONTROL_OFFSET]);
1190 hwgroup->hwif = hwif;
1191 hwgroup->drive = drive;
1192 drive->sleep = 0;
1193 drive->service_start = jiffies;
1195 bdev = &blk_dev[hwif->major];
1196 if (bdev->current_request == &bdev->plug) /* FIXME: paranoia */
1197 printk("%s: Huh? nuking plugged queue\n", drive->name);
1198 bdev->current_request = hwgroup->rq = drive->queue;
1199 spin_unlock_irqrestore(&io_request_lock, io_flags);
1201 if (hwif->irq != masked_irq)
1202 disable_irq(hwif->irq);
1203 spin_unlock_irqrestore(&hwgroup->spinlock, *hwgroup_flags);
1204 start_request(drive);
1205 spin_lock_irqsave(&hwgroup->spinlock, *hwgroup_flags);
1206 if (hwif->irq != masked_irq)
1207 enable_irq(hwif->irq);
1212 * ide_get_queue() returns the queue which corresponds to a given device.
1214 struct request **ide_get_queue (kdev_t dev)
1216 ide_hwif_t *hwif = (ide_hwif_t *)blk_dev[MAJOR(dev)].data;
1218 return &hwif->drives[DEVICE_NR(dev) & 1].queue;
1222 * do_hwgroup_request() invokes ide_do_request() after claiming hwgroup->busy.
1224 static void do_hwgroup_request (ide_hwgroup_t *hwgroup)
1226 unsigned long flags;
1228 spin_lock_irqsave(&hwgroup->spinlock, flags);
1229 if (hwgroup->busy) {
1230 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1231 return;
1233 del_timer(&hwgroup->timer);
1234 ide_get_lock(&ide_lock, ide_intr, hwgroup); /* for atari only */
1235 ide_do_request(hwgroup, &flags, 0);
1236 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1240 * ll_rw_blk.c invokes our do_idex_request() function
1241 * with the io_request_spinlock already grabbed.
1242 * Since we need to do our own spinlock's internally,
1243 * on paths that don't necessarily originate through the
1244 * do_idex_request() path, we have to undo the spinlock on entry,
1245 * and restore it again on exit.
1246 * Fortunately, this is mostly a nop for non-SMP kernels.
1248 static inline void unlock_do_hwgroup_request (ide_hwgroup_t *hwgroup)
1250 spin_unlock(&io_request_lock);
1251 do_hwgroup_request (hwgroup);
1252 spin_lock_irq(&io_request_lock);
1255 void do_ide0_request (void)
1257 unlock_do_hwgroup_request (ide_hwifs[0].hwgroup);
1260 #if MAX_HWIFS > 1
1261 void do_ide1_request (void)
1263 unlock_do_hwgroup_request (ide_hwifs[1].hwgroup);
1265 #endif /* MAX_HWIFS > 1 */
1267 #if MAX_HWIFS > 2
1268 void do_ide2_request (void)
1270 unlock_do_hwgroup_request (ide_hwifs[2].hwgroup);
1272 #endif /* MAX_HWIFS > 2 */
1274 #if MAX_HWIFS > 3
1275 void do_ide3_request (void)
1277 unlock_do_hwgroup_request (ide_hwifs[3].hwgroup);
1279 #endif /* MAX_HWIFS > 3 */
1281 #if MAX_HWIFS > 4
1282 void do_ide4_request (void)
1284 unlock_do_hwgroup_request (ide_hwifs[4].hwgroup);
1286 #endif /* MAX_HWIFS > 4 */
1288 #if MAX_HWIFS > 5
1289 void do_ide5_request (void)
1291 unlock_do_hwgroup_request (ide_hwifs[5].hwgroup);
1293 #endif /* MAX_HWIFS > 5 */
1295 #if MAX_HWIFS > 6
1296 void do_ide6_request (void)
1298 unlock_do_hwgroup_request (ide_hwifs[6].hwgroup);
1300 #endif /* MAX_HWIFS > 6 */
1302 #if MAX_HWIFS > 7
1303 void do_ide7_request (void)
1305 unlock_do_hwgroup_request (ide_hwifs[7].hwgroup);
1307 #endif /* MAX_HWIFS > 7 */
1309 static void start_next_request (ide_hwgroup_t *hwgroup, int masked_irq)
1311 unsigned long flags;
1312 ide_drive_t *drive;
1314 spin_lock_irqsave(&hwgroup->spinlock, flags);
1315 if (hwgroup->handler != NULL) {
1316 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1317 return;
1319 drive = hwgroup->drive;
1320 set_recovery_timer(HWIF(drive));
1321 drive->service_time = jiffies - drive->service_start;
1322 ide_do_request(hwgroup, &flags, masked_irq);
1323 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1326 void ide_timer_expiry (unsigned long data)
1328 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *) data;
1329 ide_drive_t *drive;
1330 ide_handler_t *handler;
1331 unsigned long flags;
1333 spin_lock_irqsave(&hwgroup->spinlock, flags);
1334 drive = hwgroup->drive;
1335 if ((handler = hwgroup->handler) == NULL) {
1336 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1337 do_hwgroup_request(hwgroup);
1338 return;
1340 hwgroup->busy = 1; /* should already be "1" */
1341 hwgroup->handler = NULL;
1342 del_timer(&hwgroup->timer); /* Is this needed?? */
1343 if (hwgroup->poll_timeout != 0) { /* polling in progress? */
1344 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1345 handler(drive);
1346 } else if (drive_is_ready(drive)) {
1347 printk("%s: lost interrupt\n", drive->name);
1348 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1349 handler(drive);
1350 } else {
1351 if (drive->waiting_for_dma) {
1352 (void) hwgroup->hwif->dmaproc(ide_dma_end, drive);
1353 printk("%s: timeout waiting for DMA\n", drive->name);
1355 * need something here for HX PIIX3 UDMA and HPT343.......AMH
1356 * irq timeout: status=0x58 { DriveReady SeekComplete DataRequest }
1359 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1360 ide_error(drive, "irq timeout", GET_STAT());
1362 start_next_request(hwgroup, 0);
1366 * There's nothing really useful we can do with an unexpected interrupt,
1367 * other than reading the status register (to clear it), and logging it.
1368 * There should be no way that an irq can happen before we're ready for it,
1369 * so we needn't worry much about losing an "important" interrupt here.
1371 * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
1372 * drive enters "idle", "standby", or "sleep" mode, so if the status looks
1373 * "good", we just ignore the interrupt completely.
1375 * This routine assumes __cli() is in effect when called.
1377 * If an unexpected interrupt happens on irq15 while we are handling irq14
1378 * and if the two interfaces are "serialized" (CMD640), then it looks like
1379 * we could screw up by interfering with a new request being set up for irq15.
1381 * In reality, this is a non-issue. The new command is not sent unless the
1382 * drive is ready to accept one, in which case we know the drive is not
1383 * trying to interrupt us. And ide_set_handler() is always invoked before
1384 * completing the issuance of any new drive command, so we will not be
1385 * accidently invoked as a result of any valid command completion interrupt.
1388 static void unexpected_intr (int irq, ide_hwgroup_t *hwgroup)
1390 byte stat;
1391 ide_hwif_t *hwif = hwgroup->hwif;
1394 * handle the unexpected interrupt
1396 do {
1397 if (hwif->irq == irq) {
1398 stat = IN_BYTE(hwif->io_ports[IDE_STATUS_OFFSET]);
1399 if (!OK_STAT(stat, READY_STAT, BAD_STAT)) {
1400 /* Try to not flood the console with msgs */
1401 static unsigned long last_msgtime = 0, count = 0;
1402 ++count;
1403 if (0 < (signed long)(jiffies - (last_msgtime + HZ))) {
1404 last_msgtime = jiffies;
1405 printk("%s%s: unexpected interrupt, status=0x%02x, count=%ld\n",
1406 hwif->name, (hwif->next == hwgroup->hwif) ? "" : "(?)", stat, count);
1410 } while ((hwif = hwif->next) != hwgroup->hwif);
1413 * entry point for all interrupts, caller does __cli() for us
1415 void ide_intr (int irq, void *dev_id, struct pt_regs *regs)
1417 unsigned long flags;
1418 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *)dev_id;
1419 ide_hwif_t *hwif;
1420 ide_drive_t *drive;
1421 ide_handler_t *handler;
1423 __cli(); /* local CPU only */
1424 spin_lock_irqsave(&hwgroup->spinlock, flags);
1425 hwif = hwgroup->hwif;
1427 if (!ide_ack_intr(hwif)) {
1428 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1429 return;
1432 if ((handler = hwgroup->handler) == NULL || hwgroup->poll_timeout != 0) {
1434 * Not expecting an interrupt from this drive.
1435 * That means this could be:
1436 * (1) an interrupt from another PCI device
1437 * sharing the same PCI INT# as us.
1438 * or (2) a drive just entered sleep or standby mode,
1439 * and is interrupting to let us know.
1440 * or (3) a spurious interrupt of unknown origin.
1442 * For PCI, we cannot tell the difference,
1443 * so in that case we just ignore it and hope it goes away.
1445 #ifdef CONFIG_BLK_DEV_IDEPCI
1446 if (IDE_PCI_DEVID_EQ(hwif->pci_devid, IDE_PCI_DEVID_NULL))
1447 #endif /* CONFIG_BLK_DEV_IDEPCI */
1450 * Probably not a shared PCI interrupt,
1451 * so we can safely try to do something about it:
1453 unexpected_intr(irq, hwgroup);
1455 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1456 return;
1458 drive = hwgroup->drive;
1459 if (!drive || !drive_is_ready(drive)) {
1460 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1461 return;
1463 hwgroup->handler = NULL;
1464 del_timer(&(hwgroup->timer));
1465 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1466 if (drive->unmask)
1467 ide__sti(); /* local CPU only */
1468 handler(drive); /* service this interrupt, may set handler for next interrupt */
1470 * Note that handler() may have set things up for another
1471 * interrupt to occur soon, but it cannot happen until
1472 * we exit from this routine, because it will be the
1473 * same irq as is currently being serviced here,
1474 * and Linux won't allow another (on any CPU) until we return.
1476 start_next_request(hwgroup, hwif->irq);
1480 * get_info_ptr() returns the (ide_drive_t *) for a given device number.
1481 * It returns NULL if the given device number does not match any present drives.
1483 static ide_drive_t *get_info_ptr (kdev_t i_rdev)
1485 int major = MAJOR(i_rdev);
1486 unsigned int h;
1488 for (h = 0; h < MAX_HWIFS; ++h) {
1489 ide_hwif_t *hwif = &ide_hwifs[h];
1490 if (hwif->present && major == hwif->major) {
1491 unsigned unit = DEVICE_NR(i_rdev);
1492 if (unit < MAX_DRIVES) {
1493 ide_drive_t *drive = &hwif->drives[unit];
1494 if (drive->present)
1495 return drive;
1497 break;
1500 return NULL;
1504 * This function is intended to be used prior to invoking ide_do_drive_cmd().
1506 void ide_init_drive_cmd (struct request *rq)
1508 rq->buffer = NULL;
1509 rq->cmd = IDE_DRIVE_CMD;
1510 rq->sector = 0;
1511 rq->nr_sectors = 0;
1512 rq->current_nr_sectors = 0;
1513 rq->sem = NULL;
1514 rq->bh = NULL;
1515 rq->bhtail = NULL;
1516 rq->next = NULL;
1520 * This function issues a special IDE device request
1521 * onto the request queue.
1523 * If action is ide_wait, then the rq is queued at the end of the
1524 * request queue, and the function sleeps until it has been processed.
1525 * This is for use when invoked from an ioctl handler.
1527 * If action is ide_preempt, then the rq is queued at the head of
1528 * the request queue, displacing the currently-being-processed
1529 * request and this function returns immediately without waiting
1530 * for the new rq to be completed. This is VERY DANGEROUS, and is
1531 * intended for careful use by the ATAPI tape/cdrom driver code.
1533 * If action is ide_next, then the rq is queued immediately after
1534 * the currently-being-processed-request (if any), and the function
1535 * returns without waiting for the new rq to be completed. As above,
1536 * This is VERY DANGEROUS, and is intended for careful use by the
1537 * ATAPI tape/cdrom driver code.
1539 * If action is ide_end, then the rq is queued at the end of the
1540 * request queue, and the function returns immediately without waiting
1541 * for the new rq to be completed. This is again intended for careful
1542 * use by the ATAPI tape/cdrom driver code.
1544 int ide_do_drive_cmd (ide_drive_t *drive, struct request *rq, ide_action_t action)
1546 unsigned long flags;
1547 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1548 unsigned int major = HWIF(drive)->major;
1549 struct request *cur_rq;
1550 DECLARE_MUTEX_LOCKED(sem);
1552 if (IS_PDC4030_DRIVE && rq->buffer != NULL)
1553 return -ENOSYS; /* special drive cmds not supported */
1554 rq->errors = 0;
1555 rq->rq_status = RQ_ACTIVE;
1556 rq->rq_dev = MKDEV(major,(drive->select.b.unit)<<PARTN_BITS);
1557 if (action == ide_wait)
1558 rq->sem = &sem;
1560 spin_lock_irqsave(&io_request_lock, flags);
1561 cur_rq = drive->queue;
1562 if (cur_rq == NULL || action == ide_preempt) {
1563 rq->next = cur_rq;
1564 drive->queue = rq;
1565 if (action == ide_preempt)
1566 hwgroup->rq = NULL;
1567 } else {
1568 if (action == ide_wait || action == ide_end) {
1569 while (cur_rq->next != NULL) /* find end of list */
1570 cur_rq = cur_rq->next;
1572 rq->next = cur_rq->next;
1573 cur_rq->next = rq;
1575 spin_unlock_irqrestore(&io_request_lock, flags);
1576 do_hwgroup_request(hwgroup);
1577 save_flags(flags); /* all CPUs; overkill? */
1578 cli(); /* all CPUs; overkill? */
1579 if (action == ide_wait && rq->rq_status != RQ_INACTIVE)
1580 down(&sem); /* wait for it to be serviced */
1581 restore_flags(flags); /* all CPUs; overkill? */
1582 return rq->errors ? -EIO : 0; /* return -EIO if errors */
1586 * This routine is called to flush all partitions and partition tables
1587 * for a changed disk, and then re-read the new partition table.
1588 * If we are revalidating a disk because of a media change, then we
1589 * enter with usage == 0. If we are using an ioctl, we automatically have
1590 * usage == 1 (we need an open channel to use an ioctl :-), so this
1591 * is our limit.
1593 int ide_revalidate_disk(kdev_t i_rdev)
1595 ide_drive_t *drive;
1596 ide_hwgroup_t *hwgroup;
1597 unsigned int p, major, minor;
1598 long flags;
1600 if ((drive = get_info_ptr(i_rdev)) == NULL)
1601 return -ENODEV;
1602 major = MAJOR(i_rdev);
1603 minor = drive->select.b.unit << PARTN_BITS;
1604 hwgroup = HWGROUP(drive);
1605 spin_lock_irqsave(&hwgroup->spinlock, flags);
1606 if (drive->busy || (drive->usage > 1)) {
1607 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1608 return -EBUSY;
1610 drive->busy = 1;
1611 MOD_INC_USE_COUNT;
1612 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1614 for (p = 0; p < (1<<PARTN_BITS); ++p) {
1615 if (drive->part[p].nr_sects > 0) {
1616 kdev_t devp = MKDEV(major, minor+p);
1617 struct super_block * sb = get_super(devp);
1618 fsync_dev (devp);
1619 if (sb)
1620 invalidate_inodes(sb);
1621 invalidate_buffers (devp);
1622 set_blocksize(devp, 1024);
1624 drive->part[p].start_sect = 0;
1625 drive->part[p].nr_sects = 0;
1628 drive->part[0].nr_sects = current_capacity(drive);
1629 if ((drive->media != ide_disk && drive->media != ide_floppy) ||
1630 drive->driver == NULL || !drive->part[0].nr_sects)
1631 drive->part[0].start_sect = -1;
1632 resetup_one_dev(HWIF(drive)->gd, drive->select.b.unit);
1634 drive->busy = 0;
1635 wake_up(&drive->wqueue);
1636 MOD_DEC_USE_COUNT;
1637 return 0;
1640 static void revalidate_drives (void)
1642 ide_hwif_t *hwif;
1643 ide_drive_t *drive;
1644 int index, unit;
1646 for (index = 0; index < MAX_HWIFS; ++index) {
1647 hwif = &ide_hwifs[index];
1648 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1649 drive = &ide_hwifs[index].drives[unit];
1650 if (drive->revalidate) {
1651 drive->revalidate = 0;
1652 if (!initializing)
1653 (void) ide_revalidate_disk(MKDEV(hwif->major, unit<<PARTN_BITS));
1659 static void ide_init_module (int type)
1661 int found = 0;
1662 ide_module_t *module = ide_modules;
1664 while (module) {
1665 if (module->type == type) {
1666 found = 1;
1667 (void) module->init();
1669 module = module->next;
1671 revalidate_drives();
1672 #ifdef CONFIG_KMOD
1673 if (!found && type == IDE_PROBE_MODULE)
1674 (void) request_module("ide-probe");
1675 #endif /* CONFIG_KMOD */
1678 static int ide_open(struct inode * inode, struct file * filp)
1680 ide_drive_t *drive;
1681 int rc;
1683 if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
1684 return -ENXIO;
1685 MOD_INC_USE_COUNT;
1686 if (drive->driver == NULL)
1687 ide_init_module(IDE_DRIVER_MODULE);
1688 #ifdef CONFIG_KMOD
1689 if (drive->driver == NULL) {
1690 if (drive->media == ide_disk)
1691 (void) request_module("ide-disk");
1692 if (drive->media == ide_cdrom)
1693 (void) request_module("ide-cd");
1694 if (drive->media == ide_tape)
1695 (void) request_module("ide-tape");
1696 if (drive->media == ide_floppy)
1697 (void) request_module("ide-floppy");
1699 #endif /* CONFIG_KMOD */
1700 while (drive->busy)
1701 sleep_on(&drive->wqueue);
1702 drive->usage++;
1703 if (drive->driver != NULL) {
1704 if ((rc = DRIVER(drive)->open(inode, filp, drive)))
1705 MOD_DEC_USE_COUNT;
1706 return rc;
1708 printk ("%s: driver not present\n", drive->name);
1709 drive->usage--;
1710 MOD_DEC_USE_COUNT;
1711 return -ENXIO;
1715 * Releasing a block device means we sync() it, so that it can safely
1716 * be forgotten about...
1718 static int ide_release(struct inode * inode, struct file * file)
1720 ide_drive_t *drive;
1722 if ((drive = get_info_ptr(inode->i_rdev)) != NULL) {
1723 fsync_dev(inode->i_rdev);
1724 drive->usage--;
1725 if (drive->driver != NULL)
1726 DRIVER(drive)->release(inode, file, drive);
1727 MOD_DEC_USE_COUNT;
1729 return 0;
1732 int ide_replace_subdriver(ide_drive_t *drive, const char *driver)
1734 if (!drive->present || drive->busy || drive->usage)
1735 goto abort;
1736 if (drive->driver != NULL && DRIVER(drive)->cleanup(drive))
1737 goto abort;
1738 strncpy(drive->driver_req, driver, 9);
1739 ide_init_module(IDE_DRIVER_MODULE);
1740 drive->driver_req[0] = 0;
1741 ide_init_module(IDE_DRIVER_MODULE);
1742 if (DRIVER(drive) && !strcmp(DRIVER(drive)->name, driver))
1743 return 0;
1744 abort:
1745 return 1;
1748 void ide_unregister (unsigned int index)
1750 struct gendisk *gd, **gdp;
1751 ide_drive_t *drive, *d;
1752 ide_hwif_t *hwif, *g;
1753 ide_hwgroup_t *hwgroup;
1754 int irq_count = 0, unit, i;
1755 unsigned long flags;
1756 unsigned int p, minor;
1758 if (index >= MAX_HWIFS)
1759 return;
1760 save_flags(flags); /* all CPUs */
1761 cli(); /* all CPUs */
1762 hwif = &ide_hwifs[index];
1763 if (!hwif->present)
1764 goto abort;
1765 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1766 drive = &hwif->drives[unit];
1767 if (!drive->present)
1768 continue;
1769 if (drive->busy || drive->usage)
1770 goto abort;
1771 if (drive->driver != NULL && DRIVER(drive)->cleanup(drive))
1772 goto abort;
1774 hwif->present = 0;
1777 * All clear? Then blow away the buffer cache
1779 sti();
1780 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1781 drive = &hwif->drives[unit];
1782 minor = drive->select.b.unit << PARTN_BITS;
1783 for (p = 0; p < (1<<PARTN_BITS); ++p) {
1784 if (drive->part[p].nr_sects > 0) {
1785 kdev_t devp = MKDEV(hwif->major, minor+p);
1786 struct super_block * sb = get_super(devp);
1787 if (sb) invalidate_inodes(sb);
1788 invalidate_buffers (devp);
1792 cli();
1793 hwgroup = hwif->hwgroup;
1796 * free the irq if we were the only hwif using it
1798 g = hwgroup->hwif;
1799 do {
1800 if (g->irq == hwif->irq)
1801 ++irq_count;
1802 g = g->next;
1803 } while (g != hwgroup->hwif);
1804 if (irq_count == 1)
1805 free_irq(hwif->irq, hwgroup);
1808 * Note that we only release the standard ports,
1809 * and do not even try to handle any extra ports
1810 * allocated for weird IDE interface chipsets.
1812 ide_release_region(hwif->io_ports[IDE_DATA_OFFSET], 8);
1813 if (hwif->io_ports[IDE_CONTROL_OFFSET])
1814 ide_release_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
1817 * Remove us from the hwgroup, and free
1818 * the hwgroup if we were the only member
1820 d = hwgroup->drive;
1821 for (i = 0; i < MAX_DRIVES; ++i) {
1822 drive = &hwif->drives[i];
1823 if (!drive->present)
1824 continue;
1825 while (hwgroup->drive->next != drive)
1826 hwgroup->drive = hwgroup->drive->next;
1827 hwgroup->drive->next = drive->next;
1828 if (hwgroup->drive == drive)
1829 hwgroup->drive = NULL;
1830 if (drive->id != NULL) {
1831 kfree(drive->id);
1832 drive->id = NULL;
1834 drive->present = 0;
1836 if (d->present)
1837 hwgroup->drive = d;
1838 while (hwgroup->hwif->next != hwif)
1839 hwgroup->hwif = hwgroup->hwif->next;
1840 hwgroup->hwif->next = hwif->next;
1841 if (hwgroup->hwif == hwif)
1842 kfree(hwgroup);
1843 else
1844 hwgroup->hwif = HWIF(hwgroup->drive);
1846 #ifdef CONFIG_BLK_DEV_IDEDMA
1847 if (hwif->dma_base)
1848 (void) ide_release_dma(hwif);
1849 #endif /* CONFIG_BLK_DEV_IDEDMA */
1852 * Remove us from the kernel's knowledge
1854 unregister_blkdev(hwif->major, hwif->name);
1855 kfree(blksize_size[hwif->major]);
1856 kfree(max_sectors[hwif->major]);
1857 kfree(max_readahead[hwif->major]);
1858 blk_dev[hwif->major].request_fn = NULL;
1859 blk_dev[hwif->major].data = NULL;
1860 blk_dev[hwif->major].queue = NULL;
1861 blksize_size[hwif->major] = NULL;
1862 for (gdp = &gendisk_head; *gdp; gdp = &((*gdp)->next))
1863 if (*gdp == hwif->gd)
1864 break;
1865 if (*gdp == NULL)
1866 printk("gd not in disk chain!\n");
1867 else {
1868 gd = *gdp; *gdp = gd->next;
1869 kfree(gd->sizes);
1870 kfree(gd->part);
1871 kfree(gd);
1873 init_hwif_data (index); /* restore hwif data to pristine status */
1874 abort:
1875 restore_flags(flags); /* all CPUs */
1879 * Setup hw_regs_t structure described by parameters. You
1880 * may set up the hw structure yourself OR use this routine to
1881 * do it for you.
1883 void ide_setup_ports ( hw_regs_t *hw,
1884 ide_ioreg_t base, int *offsets,
1885 ide_ioreg_t ctrl, ide_ioreg_t intr,
1886 ide_ack_intr_t *ack_intr, int irq)
1888 int i;
1890 for (i = 0; i < IDE_NR_PORTS; i++) {
1891 if (offsets[i] == -1) {
1892 switch(i) {
1893 case IDE_CONTROL_OFFSET:
1894 hw->io_ports[i] = ctrl;
1895 break;
1896 case IDE_IRQ_OFFSET:
1897 hw->io_ports[i] = intr;
1898 break;
1899 default:
1900 hw->io_ports[i] = 0;
1901 break;
1903 } else {
1904 hw->io_ports[i] = base + offsets[i];
1907 hw->irq = irq;
1908 hw->ack_intr = ack_intr;
1912 * Register an IDE interface, specifing exactly the registers etc
1913 * Set init=1 iff calling before probes have taken place.
1915 int ide_register_hw (hw_regs_t *hw, ide_hwif_t **hwifp)
1917 int index, retry = 1;
1918 ide_hwif_t *hwif;
1920 do {
1921 for (index = 0; index < MAX_HWIFS; ++index) {
1922 hwif = &ide_hwifs[index];
1923 if (hwif->hw.io_ports[IDE_DATA_OFFSET] == hw->io_ports[IDE_DATA_OFFSET])
1924 goto found;
1926 for (index = 0; index < MAX_HWIFS; ++index) {
1927 hwif = &ide_hwifs[index];
1928 if ((!hwif->present && !initializing) ||
1929 (!hwif->hw.io_ports[IDE_DATA_OFFSET] && initializing))
1930 goto found;
1932 for (index = 0; index < MAX_HWIFS; index++)
1933 ide_unregister(index);
1934 } while (retry--);
1935 return -1;
1936 found:
1937 if (hwif->present)
1938 ide_unregister(index);
1939 if (hwif->present)
1940 return -1;
1941 memcpy(&hwif->hw, hw, sizeof(*hw));
1942 memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->hw.io_ports));
1943 hwif->irq = hw->irq;
1944 hwif->noprobe = 0;
1946 if (!initializing) {
1947 ide_init_module(IDE_PROBE_MODULE);
1948 ide_init_module(IDE_DRIVER_MODULE);
1951 if (hwifp)
1952 *hwifp = hwif;
1954 return (initializing || hwif->present) ? index : -1;
1958 * Compatability function with existing drivers. If you want
1959 * something different, use the function above.
1961 int ide_register (int arg1, int arg2, int irq)
1963 hw_regs_t hw;
1965 ide_init_hwif_ports(&hw, (ide_ioreg_t) arg1, (ide_ioreg_t) arg2, NULL);
1966 hw.irq = irq;
1967 return ide_register_hw(&hw, NULL);
1970 void ide_add_setting(ide_drive_t *drive, const char *name, int rw, int read_ioctl, int write_ioctl, int data_type, int min, int max, int mul_factor, int div_factor, void *data, ide_procset_t *set)
1972 ide_settings_t **p = (ide_settings_t **) &drive->settings, *setting = NULL;
1974 while ((*p) && strcmp((*p)->name, name) < 0)
1975 p = &((*p)->next);
1976 if ((setting = kmalloc(sizeof(*setting), GFP_KERNEL)) == NULL)
1977 goto abort;
1978 memset(setting, 0, sizeof(*setting));
1979 if ((setting->name = kmalloc(strlen(name) + 1, GFP_KERNEL)) == NULL)
1980 goto abort;
1981 strcpy(setting->name, name); setting->rw = rw;
1982 setting->read_ioctl = read_ioctl; setting->write_ioctl = write_ioctl;
1983 setting->data_type = data_type; setting->min = min;
1984 setting->max = max; setting->mul_factor = mul_factor;
1985 setting->div_factor = div_factor; setting->data = data;
1986 setting->set = set; setting->next = *p;
1987 if (drive->driver)
1988 setting->auto_remove = 1;
1989 *p = setting;
1990 return;
1991 abort:
1992 if (setting)
1993 kfree(setting);
1996 void ide_remove_setting(ide_drive_t *drive, char *name)
1998 ide_settings_t **p = (ide_settings_t **) &drive->settings, *setting;
2000 while ((*p) && strcmp((*p)->name, name))
2001 p = &((*p)->next);
2002 if ((setting = (*p)) == NULL)
2003 return;
2004 (*p) = setting->next;
2005 kfree(setting->name);
2006 kfree(setting);
2009 static ide_settings_t *ide_find_setting_by_ioctl(ide_drive_t *drive, int cmd)
2011 ide_settings_t *setting = drive->settings;
2013 while (setting) {
2014 if (setting->read_ioctl == cmd || setting->write_ioctl == cmd)
2015 break;
2016 setting = setting->next;
2018 return setting;
2021 ide_settings_t *ide_find_setting_by_name(ide_drive_t *drive, char *name)
2023 ide_settings_t *setting = drive->settings;
2025 while (setting) {
2026 if (strcmp(setting->name, name) == 0)
2027 break;
2028 setting = setting->next;
2030 return setting;
2033 static void auto_remove_settings(ide_drive_t *drive)
2035 ide_settings_t *setting;
2036 repeat:
2037 setting = drive->settings;
2038 while (setting) {
2039 if (setting->auto_remove) {
2040 ide_remove_setting(drive, setting->name);
2041 goto repeat;
2043 setting = setting->next;
2047 int ide_read_setting(ide_drive_t *drive, ide_settings_t *setting)
2049 int val = -EINVAL;
2050 unsigned long flags;
2052 if ((setting->rw & SETTING_READ)) {
2053 spin_lock_irqsave(&HWGROUP(drive)->spinlock, flags);
2054 switch(setting->data_type) {
2055 case TYPE_BYTE:
2056 val = *((u8 *) setting->data);
2057 break;
2058 case TYPE_SHORT:
2059 val = *((u16 *) setting->data);
2060 break;
2061 case TYPE_INT:
2062 case TYPE_INTA:
2063 val = *((u32 *) setting->data);
2064 break;
2066 spin_unlock_irqrestore(&HWGROUP(drive)->spinlock, flags);
2068 return val;
2071 int ide_spin_wait_hwgroup (ide_drive_t *drive, unsigned long *flags)
2073 ide_hwgroup_t *hwgroup = HWGROUP(drive);
2074 unsigned long timeout = jiffies + (3 * HZ);
2076 spin_lock_irqsave(&hwgroup->spinlock, *flags);
2077 while (hwgroup->busy) {
2078 spin_unlock_irqrestore(&hwgroup->spinlock, *flags);
2079 __sti(); /* local CPU only; needed for jiffies */
2080 if (0 < (signed long)(jiffies - timeout)) {
2081 printk("%s: channel busy\n", drive->name);
2082 return -EBUSY;
2084 spin_lock_irqsave(&hwgroup->spinlock, *flags);
2086 return 0;
2089 int ide_write_setting(ide_drive_t *drive, ide_settings_t *setting, int val)
2091 unsigned long flags;
2092 int i;
2093 u32 *p;
2095 if (!capable(CAP_SYS_ADMIN))
2096 return -EACCES;
2097 if (!(setting->rw & SETTING_WRITE))
2098 return -EPERM;
2099 if (val < setting->min || val > setting->max)
2100 return -EINVAL;
2101 if (setting->set)
2102 return setting->set(drive, val);
2103 if (ide_spin_wait_hwgroup(drive, &flags))
2104 return -EBUSY;
2105 switch (setting->data_type) {
2106 case TYPE_BYTE:
2107 *((u8 *) setting->data) = val;
2108 break;
2109 case TYPE_SHORT:
2110 *((u16 *) setting->data) = val;
2111 break;
2112 case TYPE_INT:
2113 *((u32 *) setting->data) = val;
2114 break;
2115 case TYPE_INTA:
2116 p = (u32 *) setting->data;
2117 for (i = 0; i < 1 << PARTN_BITS; i++, p++)
2118 *p = val;
2119 break;
2121 spin_unlock_irqrestore(&HWGROUP(drive)->spinlock, flags);
2122 return 0;
2125 static int set_io_32bit(ide_drive_t *drive, int arg)
2127 drive->io_32bit = arg;
2128 #ifdef CONFIG_BLK_DEV_DTC2278
2129 if (HWIF(drive)->chipset == ide_dtc2278)
2130 HWIF(drive)->drives[!drive->select.b.unit].io_32bit = arg;
2131 #endif /* CONFIG_BLK_DEV_DTC2278 */
2132 return 0;
2135 static int set_using_dma(ide_drive_t *drive, int arg)
2137 if (!drive->driver || !DRIVER(drive)->supports_dma)
2138 return -EPERM;
2139 if (!drive->id || !(drive->id->capability & 1) || !HWIF(drive)->dmaproc)
2140 return -EPERM;
2141 if (HWIF(drive)->dmaproc(arg ? ide_dma_on : ide_dma_off, drive))
2142 return -EIO;
2143 return 0;
2146 static int set_pio_mode(ide_drive_t *drive, int arg)
2148 struct request rq;
2150 if (!HWIF(drive)->tuneproc)
2151 return -ENOSYS;
2152 if (drive->special.b.set_tune)
2153 return -EBUSY;
2154 ide_init_drive_cmd(&rq);
2155 drive->tune_req = (byte) arg;
2156 drive->special.b.set_tune = 1;
2157 (void) ide_do_drive_cmd (drive, &rq, ide_wait);
2158 return 0;
2161 void ide_add_generic_settings(ide_drive_t *drive)
2164 * drive setting name read/write access read ioctl write ioctl data type min max mul_factor div_factor data pointer set function
2166 ide_add_setting(drive, "io_32bit", drive->no_io_32bit ? SETTING_READ : SETTING_RW, HDIO_GET_32BIT, HDIO_SET_32BIT, TYPE_BYTE, 0, 1 + (SUPPORT_VLB_SYNC << 1), 1, 1, &drive->io_32bit, set_io_32bit);
2167 ide_add_setting(drive, "keepsettings", SETTING_RW, HDIO_GET_KEEPSETTINGS, HDIO_SET_KEEPSETTINGS, TYPE_BYTE, 0, 1, 1, 1, &drive->keep_settings, NULL);
2168 ide_add_setting(drive, "nice1", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->nice1, NULL);
2169 ide_add_setting(drive, "pio_mode", SETTING_WRITE, -1, HDIO_SET_PIO_MODE, TYPE_BYTE, 0, 255, 1, 1, NULL, set_pio_mode);
2170 ide_add_setting(drive, "slow", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->slow, NULL);
2171 ide_add_setting(drive, "unmaskirq", drive->no_unmask ? SETTING_READ : SETTING_RW, HDIO_GET_UNMASKINTR, HDIO_SET_UNMASKINTR, TYPE_BYTE, 0, 1, 1, 1, &drive->unmask, NULL);
2172 ide_add_setting(drive, "using_dma", SETTING_RW, HDIO_GET_DMA, HDIO_SET_DMA, TYPE_BYTE, 0, 1, 1, 1, &drive->using_dma, set_using_dma);
2175 int ide_wait_cmd (ide_drive_t *drive, int cmd, int nsect, int feature, int sectors, byte *buf)
2177 struct request rq;
2178 byte buffer[4];
2180 if (!buf)
2181 buf = buffer;
2182 memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
2183 ide_init_drive_cmd(&rq);
2184 rq.buffer = buf;
2185 *buf++ = cmd;
2186 *buf++ = nsect;
2187 *buf++ = feature;
2188 *buf++ = sectors;
2189 return ide_do_drive_cmd(drive, &rq, ide_wait);
2193 * Delay for *at least* 50ms. As we don't know how much time is left
2194 * until the next tick occurs, we wait an extra tick to be safe.
2195 * This is used only during the probing/polling for drives at boot time.
2197 * However, its usefullness may be needed in other places, thus we export it now.
2198 * The future may change this to a millisecond setable delay.
2200 void ide_delay_50ms (void)
2202 unsigned long timeout = jiffies + ((HZ + 19)/20) + 1;
2203 while (0 < (signed long)(timeout - jiffies));
2206 static int ide_ioctl (struct inode *inode, struct file *file,
2207 unsigned int cmd, unsigned long arg)
2209 int err = 0, major, minor;
2210 ide_drive_t *drive;
2211 struct request rq;
2212 kdev_t dev;
2213 ide_settings_t *setting;
2215 if (!inode || !(dev = inode->i_rdev))
2216 return -EINVAL;
2217 major = MAJOR(dev); minor = MINOR(dev);
2218 if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
2219 return -ENODEV;
2221 if ((setting = ide_find_setting_by_ioctl(drive, cmd)) != NULL) {
2222 if (cmd == setting->read_ioctl) {
2223 err = ide_read_setting(drive, setting);
2224 return err >= 0 ? put_user(err, (long *) arg) : err;
2225 } else {
2226 if ((MINOR(inode->i_rdev) & PARTN_MASK))
2227 return -EINVAL;
2228 return ide_write_setting(drive, setting, arg);
2232 ide_init_drive_cmd (&rq);
2233 switch (cmd) {
2234 case HDIO_GETGEO:
2236 struct hd_geometry *loc = (struct hd_geometry *) arg;
2237 if (!loc || (drive->media != ide_disk && drive->media != ide_floppy)) return -EINVAL;
2238 if (put_user(drive->bios_head, (byte *) &loc->heads)) return -EFAULT;
2239 if (put_user(drive->bios_sect, (byte *) &loc->sectors)) return -EFAULT;
2240 if (put_user(drive->bios_cyl, (unsigned short *) &loc->cylinders)) return -EFAULT;
2241 if (put_user((unsigned)drive->part[MINOR(inode->i_rdev)&PARTN_MASK].start_sect,
2242 (unsigned long *) &loc->start)) return -EFAULT;
2243 return 0;
2246 case BLKGETSIZE: /* Return device size */
2247 return put_user(drive->part[MINOR(inode->i_rdev)&PARTN_MASK].nr_sects, (long *) arg);
2249 case BLKRRPART: /* Re-read partition tables */
2250 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2251 return ide_revalidate_disk(inode->i_rdev);
2253 case HDIO_OBSOLETE_IDENTITY:
2254 case HDIO_GET_IDENTITY:
2255 if (MINOR(inode->i_rdev) & PARTN_MASK)
2256 return -EINVAL;
2257 if (drive->id == NULL)
2258 return -ENOMSG;
2259 if (copy_to_user((char *)arg, (char *)drive->id, (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
2260 return -EFAULT;
2261 return 0;
2263 case HDIO_GET_NICE:
2264 return put_user(drive->dsc_overlap << IDE_NICE_DSC_OVERLAP |
2265 drive->atapi_overlap << IDE_NICE_ATAPI_OVERLAP |
2266 drive->nice0 << IDE_NICE_0 |
2267 drive->nice1 << IDE_NICE_1 |
2268 drive->nice2 << IDE_NICE_2,
2269 (long *) arg);
2270 case HDIO_DRIVE_CMD:
2272 byte args[4], *argbuf = args;
2273 int argsize = 4;
2274 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2275 if (NULL == (void *) arg)
2276 return ide_do_drive_cmd(drive, &rq, ide_wait);
2277 if (copy_from_user(args, (void *)arg, 4))
2278 return -EFAULT;
2279 if (args[3]) {
2280 argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
2281 argbuf = kmalloc(argsize, GFP_KERNEL);
2282 if (argbuf == NULL)
2283 return -ENOMEM;
2284 memcpy(argbuf, args, 4);
2286 if ((((byte *)arg)[0] == WIN_SETFEATURES) &&
2287 (((byte *)arg)[1] > 66) &&
2288 (((byte *)arg)[2] == 3) &&
2289 ((drive->id->word93 & 0x2000) == 0)) {
2290 printk("%s: Speed warnings UDMA 3/4 is not functional.\n", drive->name);
2291 goto abort;
2293 err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
2294 if (!err &&
2295 (((byte *)arg)[0] == WIN_SETFEATURES) &&
2296 (((byte *)arg)[1] >= 16) &&
2297 (((byte *)arg)[2] == 3) &&
2298 (drive->id->dma_ultra ||
2299 drive->id->dma_mword ||
2300 drive->id->dma_1word)) {
2303 * Re-read drive->id for possible DMA mode
2304 * change (copied from ide-probe.c)
2306 struct hd_driveid *id;
2307 unsigned long timeout, irqs, flags;
2309 probe_irq_off(probe_irq_on());
2310 irqs = probe_irq_on();
2311 if (IDE_CONTROL_REG)
2312 OUT_BYTE(drive->ctl,IDE_CONTROL_REG);
2313 ide_delay_50ms();
2314 OUT_BYTE(WIN_IDENTIFY, IDE_COMMAND_REG);
2315 timeout = jiffies + WAIT_WORSTCASE;
2316 do {
2317 if (0 < (signed long)(jiffies - timeout)) {
2318 if (irqs)
2319 (void) probe_irq_off(irqs);
2320 goto abort; /* drive timed-out */
2322 ide_delay_50ms(); /* give drive a breather */
2323 } while (IN_BYTE(IDE_ALTSTATUS_REG) & BUSY_STAT);
2324 ide_delay_50ms(); /* wait for IRQ and DRQ_STAT */
2325 if (!OK_STAT(GET_STAT(),DRQ_STAT,BAD_R_STAT))
2326 goto abort;
2327 __save_flags(flags); /* local CPU only */
2328 __cli(); /* local CPU only; some systems need this */
2329 id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC);
2330 ide_input_data(drive, id, SECTOR_WORDS);
2331 (void) GET_STAT(); /* clear drive IRQ */
2332 ide__sti(); /* local CPU only */
2333 __restore_flags(flags); /* local CPU only */
2334 ide_fix_driveid(id);
2335 if (id && id->cyls) {
2336 drive->id->dma_ultra = id->dma_ultra;
2337 drive->id->dma_mword = id->dma_mword;
2338 drive->id->dma_1word = id->dma_1word;
2339 /* anything more ? */
2340 #ifdef DEBUG
2341 printk("%s: dma_ultra=%04X, dma_mword=%04X, dma_1word=%04X\n",
2342 drive->name, id->dma_ultra, id->dma_mword, id->dma_1word);
2343 #endif
2344 kfree(id);
2347 abort:
2348 if (copy_to_user((void *)arg, argbuf, argsize))
2349 err = -EFAULT;
2350 if (argsize > 4)
2351 kfree(argbuf);
2352 return err;
2355 case HDIO_SCAN_HWIF:
2357 int args[3];
2358 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2359 if (copy_from_user(args, (void *)arg, 3 * sizeof(int)))
2360 return -EFAULT;
2361 if (ide_register(args[0], args[1], args[2]) == -1)
2362 return -EIO;
2363 return 0;
2365 case HDIO_UNREGISTER_HWIF:
2366 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2367 /* should I check here for arg > MAX_HWIFS, or
2368 just let ide_unregister fail silently? -- shaver */
2369 ide_unregister(arg);
2370 return 0;
2371 case HDIO_SET_NICE:
2372 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2373 if (drive->driver == NULL)
2374 return -EPERM;
2375 if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
2376 return -EPERM;
2377 drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
2378 if (drive->dsc_overlap && !DRIVER(drive)->supports_dsc_overlap) {
2379 drive->dsc_overlap = 0;
2380 return -EPERM;
2382 drive->nice1 = (arg >> IDE_NICE_1) & 1;
2383 return 0;
2385 case BLKROSET:
2386 case BLKROGET:
2387 case BLKFLSBUF:
2388 case BLKSSZGET:
2389 case BLKPG:
2390 return blk_ioctl(inode->i_rdev, cmd, arg);
2392 default:
2393 if (drive->driver != NULL)
2394 return DRIVER(drive)->ioctl(drive, inode, file, cmd, arg);
2395 return -EPERM;
2399 static int ide_check_media_change (kdev_t i_rdev)
2401 ide_drive_t *drive;
2403 if ((drive = get_info_ptr(i_rdev)) == NULL)
2404 return -ENODEV;
2405 if (drive->driver != NULL)
2406 return DRIVER(drive)->media_change(drive);
2407 return 0;
2410 void ide_fixstring (byte *s, const int bytecount, const int byteswap)
2412 byte *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */
2414 if (byteswap) {
2415 /* convert from big-endian to host byte order */
2416 for (p = end ; p != s;) {
2417 unsigned short *pp = (unsigned short *) (p -= 2);
2418 *pp = ntohs(*pp);
2422 /* strip leading blanks */
2423 while (s != end && *s == ' ')
2424 ++s;
2426 /* compress internal blanks and strip trailing blanks */
2427 while (s != end && *s) {
2428 if (*s++ != ' ' || (s != end && *s && *s != ' '))
2429 *p++ = *(s-1);
2432 /* wipe out trailing garbage */
2433 while (p != end)
2434 *p++ = '\0';
2440 char *ide_xfer_verbose (byte xfer_rate) {
2441 switch(xfer_rate) {
2442 case XFER_UDMA_4: return("UDMA 4");
2443 case XFER_UDMA_3: return("UDMA 3");
2444 case XFER_UDMA_2: return("UDMA 2");
2445 case XFER_UDMA_1: return("UDMA 1");
2446 case XFER_UDMA_0: return("UDMA 0");
2447 case XFER_MW_DMA_2: return("MW DMA 2");
2448 case XFER_MW_DMA_1: return("MW DMA 1");
2449 case XFER_MW_DMA_0: return("MW DMA 0");
2450 case XFER_SW_DMA_2: return("SW DMA 2");
2451 case XFER_SW_DMA_1: return("SW DMA 1");
2452 case XFER_SW_DMA_0: return("SW DMA 0");
2453 case XFER_PIO_4: return("PIO 4");
2454 case XFER_PIO_3: return("PIO 3");
2455 case XFER_PIO_2: return("PIO 2");
2456 case XFER_PIO_1: return("PIO 1");
2457 case XFER_PIO_0: return("PIO 0");
2458 case XFER_PIO_SLOW: return("PIO SLOW");
2459 default: return("XFER ERROR");
2464 * stridx() returns the offset of c within s,
2465 * or -1 if c is '\0' or not found within s.
2467 __initfunc(static int stridx (const char *s, char c))
2469 char *i = strchr(s, c);
2470 return (i && c) ? i - s : -1;
2474 * match_parm() does parsing for ide_setup():
2476 * 1. the first char of s must be '='.
2477 * 2. if the remainder matches one of the supplied keywords,
2478 * the index (1 based) of the keyword is negated and returned.
2479 * 3. if the remainder is a series of no more than max_vals numbers
2480 * separated by commas, the numbers are saved in vals[] and a
2481 * count of how many were saved is returned. Base10 is assumed,
2482 * and base16 is allowed when prefixed with "0x".
2483 * 4. otherwise, zero is returned.
2485 __initfunc(static int match_parm (char *s, const char *keywords[], int vals[], int max_vals))
2487 static const char *decimal = "0123456789";
2488 static const char *hex = "0123456789abcdef";
2489 int i, n;
2491 if (*s++ == '=') {
2493 * Try matching against the supplied keywords,
2494 * and return -(index+1) if we match one
2496 if (keywords != NULL) {
2497 for (i = 0; *keywords != NULL; ++i) {
2498 if (!strcmp(s, *keywords++))
2499 return -(i+1);
2503 * Look for a series of no more than "max_vals"
2504 * numeric values separated by commas, in base10,
2505 * or base16 when prefixed with "0x".
2506 * Return a count of how many were found.
2508 for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
2509 vals[n] = i;
2510 while ((i = stridx(decimal, *++s)) >= 0)
2511 vals[n] = (vals[n] * 10) + i;
2512 if (*s == 'x' && !vals[n]) {
2513 while ((i = stridx(hex, *++s)) >= 0)
2514 vals[n] = (vals[n] * 0x10) + i;
2516 if (++n == max_vals)
2517 break;
2518 if (*s == ',' || *s == ';')
2519 ++s;
2521 if (!*s)
2522 return n;
2524 return 0; /* zero = nothing matched */
2528 * ide_setup() gets called VERY EARLY during initialization,
2529 * to handle kernel "command line" strings beginning with "hdx="
2530 * or "ide". Here is the complete set currently supported:
2532 * "hdx=" is recognized for all "x" from "a" to "h", such as "hdc".
2533 * "idex=" is recognized for all "x" from "0" to "3", such as "ide1".
2535 * "hdx=noprobe" : drive may be present, but do not probe for it
2536 * "hdx=none" : drive is NOT present, ignore cmos and do not probe
2537 * "hdx=nowerr" : ignore the WRERR_STAT bit on this drive
2538 * "hdx=cdrom" : drive is present, and is a cdrom drive
2539 * "hdx=cyl,head,sect" : disk drive is present, with specified geometry
2540 * "hdx=autotune" : driver will attempt to tune interface speed
2541 * to the fastest PIO mode supported,
2542 * if possible for this drive only.
2543 * Not fully supported by all chipset types,
2544 * and quite likely to cause trouble with
2545 * older/odd IDE drives.
2547 * "hdx=slow" : insert a huge pause after each access to the data
2548 * port. Should be used only as a last resort.
2550 * "hdx=swapdata" : when the drive is a disk, byte swap all data
2551 * "hdx=bswap" : same as above..........
2553 * "idebus=xx" : inform IDE driver of VESA/PCI bus speed in MHz,
2554 * where "xx" is between 20 and 66 inclusive,
2555 * used when tuning chipset PIO modes.
2556 * For PCI bus, 25 is correct for a P75 system,
2557 * 30 is correct for P90,P120,P180 systems,
2558 * and 33 is used for P100,P133,P166 systems.
2559 * If in doubt, use idebus=33 for PCI.
2560 * As for VLB, it is safest to not specify it.
2562 * "idex=noprobe" : do not attempt to access/use this interface
2563 * "idex=base" : probe for an interface at the addr specified,
2564 * where "base" is usually 0x1f0 or 0x170
2565 * and "ctl" is assumed to be "base"+0x206
2566 * "idex=base,ctl" : specify both base and ctl
2567 * "idex=base,ctl,irq" : specify base, ctl, and irq number
2568 * "idex=autotune" : driver will attempt to tune interface speed
2569 * to the fastest PIO mode supported,
2570 * for all drives on this interface.
2571 * Not fully supported by all chipset types,
2572 * and quite likely to cause trouble with
2573 * older/odd IDE drives.
2574 * "idex=noautotune" : driver will NOT attempt to tune interface speed
2575 * This is the default for most chipsets,
2576 * except the cmd640.
2577 * "idex=serialize" : do not overlap operations on idex and ide(x^1)
2578 * "idex=four" : four drives on idex and ide(x^1) share same ports
2579 * "idex=reset" : reset interface before first use
2580 * "idex=dma" : enable DMA by default on both drives if possible
2582 * "splitfifo=betweenChan"
2583 * : FIFO Configuration of VIA 82c586(<nothing>,"A"or"B").
2584 * --see what follows...
2585 * "splitfifo=betweenChan,thresholdprim,thresholdsec"
2586 * : FIFO Configuration of VIA 82c586(<nothing>,"A" or "B").
2587 * betweenChan = 1(all FIFO's to primary channel)
2588 * , 2(all FIFO's to secondary channel)
2589 * , 3 or 4(evenly shared between them).
2590 * note: without FIFO, a channel is (u)dma disabled!
2591 * thresholdprim = 4, 3, 2 or 1
2592 * (standing for 1, 3/4, 1/2, 1/4).
2593 * Sets the threshold of FIFO to begin dma
2594 * transfer on the primary channel.
2595 * thresholdsec = cf upper, but for secondary channel.
2597 * The following are valid ONLY on ide0, (except dc4030)
2598 * and the defaults for the base,ctl ports must not be altered.
2600 * "ide0=dtc2278" : probe/support DTC2278 interface
2601 * "ide0=ht6560b" : probe/support HT6560B interface
2602 * "ide0=cmd640_vlb" : *REQUIRED* for VLB cards with the CMD640 chip
2603 * (not for PCI -- automatically detected)
2604 * "ide0=qd6580" : probe/support qd6580 interface
2605 * "ide0=ali14xx" : probe/support ali14xx chipsets (ALI M1439, M1443, M1445)
2606 * "ide0=umc8672" : probe/support umc8672 chipsets
2607 * "idex=dc4030" : probe/support Promise DC4030VL interface
2609 __initfunc(void ide_setup (char *s))
2611 int i, vals[3];
2612 ide_hwif_t *hwif;
2613 ide_drive_t *drive;
2614 unsigned int hw, unit;
2615 const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
2616 const char max_hwif = '0' + (MAX_HWIFS - 1);
2618 printk("ide_setup: %s", s);
2620 #ifdef CONFIG_BLK_DEV_IDEDOUBLER
2621 if (!strcmp(s, "ide=doubler")) {
2622 extern int ide_doubler;
2624 printk("ide: Enabled support for IDE doublers\n");
2625 ide_doubler = 1;
2626 return;
2628 #endif /* CONFIG_BLK_DEV_IDEDOUBLER */
2630 init_ide_data ();
2633 * Look for drive options: "hdx="
2635 if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
2636 const char *hd_words[] = {"none", "noprobe", "nowerr", "cdrom",
2637 "serialize", "autotune", "noautotune",
2638 "slow", "swapdata", "bswap", NULL};
2639 unit = s[2] - 'a';
2640 hw = unit / MAX_DRIVES;
2641 unit = unit % MAX_DRIVES;
2642 hwif = &ide_hwifs[hw];
2643 drive = &hwif->drives[unit];
2644 if (strncmp(s + 4, "ide-", 4) == 0) {
2645 strncpy(drive->driver_req, s + 4, 9);
2646 goto done;
2648 switch (match_parm(&s[3], hd_words, vals, 3)) {
2649 case -1: /* "none" */
2650 drive->nobios = 1; /* drop into "noprobe" */
2651 case -2: /* "noprobe" */
2652 drive->noprobe = 1;
2653 goto done;
2654 case -3: /* "nowerr" */
2655 drive->bad_wstat = BAD_R_STAT;
2656 hwif->noprobe = 0;
2657 goto done;
2658 case -4: /* "cdrom" */
2659 drive->present = 1;
2660 drive->media = ide_cdrom;
2661 hwif->noprobe = 0;
2662 goto done;
2663 case -5: /* "serialize" */
2664 printk(" -- USE \"ide%d=serialize\" INSTEAD", hw);
2665 goto do_serialize;
2666 case -6: /* "autotune" */
2667 drive->autotune = 1;
2668 goto done;
2669 case -7: /* "noautotune" */
2670 drive->autotune = 2;
2671 goto done;
2672 case -8: /* "slow" */
2673 drive->slow = 1;
2674 goto done;
2675 case -9: /* swapdata or bswap */
2676 case -10:
2677 drive->bswap = 1;
2678 goto done;
2679 case 3: /* cyl,head,sect */
2680 drive->media = ide_disk;
2681 drive->cyl = drive->bios_cyl = vals[0];
2682 drive->head = drive->bios_head = vals[1];
2683 drive->sect = drive->bios_sect = vals[2];
2684 drive->present = 1;
2685 drive->forced_geom = 1;
2686 hwif->noprobe = 0;
2687 goto done;
2688 default:
2689 goto bad_option;
2693 #if defined(CONFIG_BLK_DEV_VIA82C586)
2695 * Look for drive option "splitfifo=..."
2698 if (s[0] == 's' && s[1] == 'p' && s[2] == 'l' &&
2699 s[3] == 'i' && s[4] == 't' && s[5] == 'f' &&
2700 s[6] == 'i' && s[7] == 'f' && s[8] == 'o') {
2701 byte tmp = 0x3a; /* default config byte */
2703 i = match_parm(&s[9], NULL, vals, 3);
2704 switch(i) {
2705 case 3:
2706 tmp &= 0xf0;
2707 if ((vals[1] > 0) && (vals[1] < 5)) {
2708 /* sets threshold for primary Channel: */
2709 byte x = 4 - vals[1];
2710 tmp |= (x << 2);
2712 else
2713 goto bad_option;
2714 if ((vals[2] > 0) && (vals[2] < 5)) {
2715 /* sets threshold for secondary Channel: */
2716 byte x = 4 - vals[2];
2717 tmp |= x;
2719 else
2720 goto bad_option;
2721 case 1:
2722 /* set the FIFO config between channels to 0: */
2723 tmp &= 0x9f;
2724 /* set the needed FIFO config between channels: */
2725 if (vals[0] == 1) /* primary fifo only */
2726 tmp |= 0x10;
2727 else if (vals[0] == 2) /* secondary fifo only */
2728 tmp |= 0x70;
2729 else if (vals[0] == 4) /* other shared fifo config */
2730 tmp |= 0x50;
2731 else if (vals[0] == 3) /* default config */
2732 tmp |= 0x30;
2733 else
2734 goto bad_option;
2735 break;
2736 default:
2737 goto bad_option;
2739 /* set the found option in fifoconfig */
2740 fifoconfig = tmp;
2741 goto done;
2743 #endif /* defined(CONFIG_BLK_DEV_VIA82C586) */
2745 if (s[0] != 'i' || s[1] != 'd' || s[2] != 'e')
2746 goto bad_option;
2748 * Look for bus speed option: "idebus="
2750 if (s[3] == 'b' && s[4] == 'u' && s[5] == 's') {
2751 if (match_parm(&s[6], NULL, vals, 1) != 1)
2752 goto bad_option;
2753 if (vals[0] >= 20 && vals[0] <= 66)
2754 idebus_parameter = vals[0];
2755 else
2756 printk(" -- BAD BUS SPEED! Expected value from 20 to 66");
2757 goto done;
2760 * Look for interface options: "idex="
2762 if (s[3] >= '0' && s[3] <= max_hwif) {
2764 * Be VERY CAREFUL changing this: note hardcoded indexes below
2766 const char *ide_words[] = {"noprobe", "serialize", "autotune", "noautotune", "reset", "dma", "four",
2767 "qd6580", "ht6560b", "cmd640_vlb", "dtc2278", "umc8672", "ali14xx", "dc4030", NULL};
2768 hw = s[3] - '0';
2769 hwif = &ide_hwifs[hw];
2770 i = match_parm(&s[4], ide_words, vals, 3);
2773 * Cryptic check to ensure chipset not already set for hwif:
2775 if (i > 0 || i <= -7) { /* is parameter a chipset name? */
2776 if (hwif->chipset != ide_unknown)
2777 goto bad_option; /* chipset already specified */
2778 if (i <= -7 && i != -14 && hw != 0)
2779 goto bad_hwif; /* chipset drivers are for "ide0=" only */
2780 if (i <= -7 && i != -14 && ide_hwifs[hw+1].chipset != ide_unknown)
2781 goto bad_option; /* chipset for 2nd port already specified */
2782 printk("\n");
2785 switch (i) {
2786 #ifdef CONFIG_BLK_DEV_PDC4030
2787 case -14: /* "dc4030" */
2789 extern void setup_pdc4030(ide_hwif_t *);
2790 setup_pdc4030(hwif);
2791 goto done;
2793 #endif /* CONFIG_BLK_DEV_PDC4030 */
2794 #ifdef CONFIG_BLK_DEV_ALI14XX
2795 case -13: /* "ali14xx" */
2797 extern void init_ali14xx (void);
2798 init_ali14xx();
2799 goto done;
2801 #endif /* CONFIG_BLK_DEV_ALI14XX */
2802 #ifdef CONFIG_BLK_DEV_UMC8672
2803 case -12: /* "umc8672" */
2805 extern void init_umc8672 (void);
2806 init_umc8672();
2807 goto done;
2809 #endif /* CONFIG_BLK_DEV_UMC8672 */
2810 #ifdef CONFIG_BLK_DEV_DTC2278
2811 case -11: /* "dtc2278" */
2813 extern void init_dtc2278 (void);
2814 init_dtc2278();
2815 goto done;
2817 #endif /* CONFIG_BLK_DEV_DTC2278 */
2818 #ifdef CONFIG_BLK_DEV_CMD640
2819 case -10: /* "cmd640_vlb" */
2821 extern int cmd640_vlb; /* flag for cmd640.c */
2822 cmd640_vlb = 1;
2823 goto done;
2825 #endif /* CONFIG_BLK_DEV_CMD640 */
2826 #ifdef CONFIG_BLK_DEV_HT6560B
2827 case -9: /* "ht6560b" */
2829 extern void init_ht6560b (void);
2830 init_ht6560b();
2831 goto done;
2833 #endif /* CONFIG_BLK_DEV_HT6560B */
2834 #if CONFIG_BLK_DEV_QD6580
2835 case -8: /* "qd6580" */
2837 extern void init_qd6580 (void);
2838 init_qd6580();
2839 goto done;
2841 #endif /* CONFIG_BLK_DEV_QD6580 */
2842 #ifdef CONFIG_BLK_DEV_4DRIVES
2843 case -7: /* "four" drives on one set of ports */
2845 ide_hwif_t *mate = &ide_hwifs[hw^1];
2846 mate->drives[0].select.all ^= 0x20;
2847 mate->drives[1].select.all ^= 0x20;
2848 hwif->chipset = mate->chipset = ide_4drives;
2849 mate->irq = hwif->irq;
2850 memcpy(mate->io_ports, hwif->io_ports, sizeof(hwif->io_ports));
2851 goto do_serialize;
2853 #endif /* CONFIG_BLK_DEV_4DRIVES */
2854 case -6: /* dma */
2855 hwif->autodma = 1;
2856 goto done;
2857 case -5: /* "reset" */
2858 hwif->reset = 1;
2859 goto done;
2860 case -4: /* "noautotune" */
2861 hwif->drives[0].autotune = 2;
2862 hwif->drives[1].autotune = 2;
2863 goto done;
2864 case -3: /* "autotune" */
2865 hwif->drives[0].autotune = 1;
2866 hwif->drives[1].autotune = 1;
2867 goto done;
2868 case -2: /* "serialize" */
2869 do_serialize:
2870 hwif->mate = &ide_hwifs[hw^1];
2871 hwif->mate->mate = hwif;
2872 hwif->serialized = hwif->mate->serialized = 1;
2873 goto done;
2875 case -1: /* "noprobe" */
2876 hwif->noprobe = 1;
2877 goto done;
2879 case 1: /* base */
2880 vals[1] = vals[0] + 0x206; /* default ctl */
2881 case 2: /* base,ctl */
2882 vals[2] = 0; /* default irq = probe for it */
2883 case 3: /* base,ctl,irq */
2884 hwif->hw.irq = vals[2];
2885 ide_init_hwif_ports(&hwif->hw, (ide_ioreg_t) vals[0], (ide_ioreg_t) vals[1], &hwif->irq);
2886 memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->io_ports));
2887 hwif->irq = vals[2];
2888 hwif->noprobe = 0;
2889 hwif->chipset = ide_generic;
2890 goto done;
2892 case 0: goto bad_option;
2893 default:
2894 printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
2895 return;
2898 bad_option:
2899 printk(" -- BAD OPTION\n");
2900 return;
2901 bad_hwif:
2902 printk("-- NOT SUPPORTED ON ide%d", hw);
2903 done:
2904 printk("\n");
2908 * This routine is called from the partition-table code in genhd.c
2909 * to "convert" a drive to a logical geometry with fewer than 1024 cyls.
2911 * The second parameter, "xparm", determines exactly how the translation
2912 * will be handled:
2913 * 0 = convert to CHS with fewer than 1024 cyls
2914 * using the same method as Ontrack DiskManager.
2915 * 1 = same as "0", plus offset everything by 63 sectors.
2916 * -1 = similar to "0", plus redirect sector 0 to sector 1.
2917 * >1 = convert to a CHS geometry with "xparm" heads.
2919 * Returns 0 if the translation was not possible, if the device was not
2920 * an IDE disk drive, or if a geometry was "forced" on the commandline.
2921 * Returns 1 if the geometry translation was successful.
2924 int ide_xlate_1024 (kdev_t i_rdev, int xparm, const char *msg)
2926 ide_drive_t *drive;
2928 static const byte head_vals[] = {4, 8, 16, 32, 64, 128, 255, 0};
2929 const byte *heads = head_vals;
2930 unsigned long tracks;
2932 drive = get_info_ptr(i_rdev);
2933 if (!drive)
2934 return 0;
2936 if (drive->forced_geom) {
2938 * Update the current 3D drive values.
2940 drive->id->cur_cyls = drive->bios_cyl;
2941 drive->id->cur_heads = drive->bios_head;
2942 drive->id->cur_sectors = drive->bios_sect;
2943 return 0;
2946 if (xparm > 1 && xparm <= drive->bios_head && drive->bios_sect == 63) {
2948 * Update the current 3D drive values.
2950 drive->id->cur_cyls = drive->bios_cyl;
2951 drive->id->cur_heads = drive->bios_head;
2952 drive->id->cur_sectors = drive->bios_sect;
2953 return 0; /* we already have a translation */
2956 printk("%s ", msg);
2958 if (xparm < 0 && (drive->bios_cyl * drive->bios_head * drive->bios_sect) < (1024 * 16 * 63)) {
2960 * Update the current 3D drive values.
2962 drive->id->cur_cyls = drive->bios_cyl;
2963 drive->id->cur_heads = drive->bios_head;
2964 drive->id->cur_sectors = drive->bios_sect;
2965 return 0; /* small disk: no translation needed */
2968 if (drive->id) {
2969 drive->cyl = drive->id->cyls;
2970 drive->head = drive->id->heads;
2971 drive->sect = drive->id->sectors;
2973 drive->bios_cyl = drive->cyl;
2974 drive->bios_head = drive->head;
2975 drive->bios_sect = drive->sect;
2976 drive->special.b.set_geometry = 1;
2978 tracks = drive->bios_cyl * drive->bios_head * drive->bios_sect / 63;
2979 drive->bios_sect = 63;
2980 if (xparm > 1) {
2981 drive->bios_head = xparm;
2982 drive->bios_cyl = tracks / drive->bios_head;
2983 } else {
2984 while (drive->bios_cyl >= 1024) {
2985 drive->bios_head = *heads;
2986 drive->bios_cyl = tracks / drive->bios_head;
2987 if (0 == *++heads)
2988 break;
2990 #if FAKE_FDISK_FOR_EZDRIVE
2991 if (xparm == -1) {
2992 drive->remap_0_to_1 = 1;
2993 printk("[remap 0->1] ");
2994 } else
2995 #endif /* FAKE_FDISK_FOR_EZDRIVE */
2996 if (xparm == 1) {
2997 drive->sect0 = 63;
2998 drive->bios_cyl = (tracks - 1) / drive->bios_head;
2999 printk("[remap +63] ");
3003 drive->part[0].nr_sects = current_capacity(drive);
3004 printk("[%d/%d/%d]", drive->bios_cyl, drive->bios_head, drive->bios_sect);
3006 * Update the current 3D drive values.
3008 drive->id->cur_cyls = drive->bios_cyl;
3009 drive->id->cur_heads = drive->bios_head;
3010 drive->id->cur_sectors = drive->bios_sect;
3011 return 1;
3015 * probe_for_hwifs() finds/initializes "known" IDE interfaces
3017 __initfunc(static void probe_for_hwifs (void))
3019 #ifdef CONFIG_PCI
3020 if (pci_present())
3022 #ifdef CONFIG_BLK_DEV_IDEPCI
3023 ide_scan_pcibus();
3024 #else
3025 #ifdef CONFIG_BLK_DEV_RZ1000
3027 extern void ide_probe_for_rz100x(void);
3028 ide_probe_for_rz100x();
3030 #endif /* CONFIG_BLK_DEV_RZ1000 */
3031 #endif /* CONFIG_BLK_DEV_IDEPCI */
3033 #endif /* CONFIG_PCI */
3035 #ifdef CONFIG_BLK_DEV_CMD640
3037 extern void ide_probe_for_cmd640x(void);
3038 ide_probe_for_cmd640x();
3040 #endif /* CONFIG_BLK_DEV_CMD640 */
3041 #ifdef CONFIG_BLK_DEV_PDC4030
3043 extern int init_pdc4030(void);
3044 (void) init_pdc4030();
3046 #endif /* CONFIG_BLK_DEV_PDC4030 */
3047 #ifdef CONFIG_BLK_DEV_IDE_PMAC
3049 extern void pmac_ide_probe(void);
3050 pmac_ide_probe();
3052 #endif /* CONFIG_BLK_DEV_IDE_PMAC */
3053 #ifdef CONFIG_BLK_DEV_IDE_ICSIDE
3055 extern void icside_init(void);
3056 icside_init();
3058 #endif /* CONFIG_BLK_DEV_IDE_ICSIDE */
3059 #ifdef CONFIG_BLK_DEV_IDE_RAPIDE
3061 extern void rapide_init(void);
3062 rapide_init();
3064 #endif /* CONFIG_BLK_DEV_IDE_RAPIDE */
3065 #ifdef CONFIG_BLK_DEV_GAYLE
3067 extern void gayle_init(void);
3068 gayle_init();
3070 #endif /* CONFIG_BLK_DEV_GAYLE */
3071 #ifdef CONFIG_BLK_DEV_FALCON_IDE
3073 extern void falconide_init(void);
3074 falconide_init();
3076 #endif /* CONFIG_BLK_DEV_FALCON_IDE */
3077 #ifdef CONFIG_BLK_DEV_MAC_IDE
3079 extern void macide_init(void);
3080 macide_init();
3082 #endif /* CONFIG_BLK_DEV_MAC_IDE */
3083 #ifdef CONFIG_BLK_DEV_BUDDHA
3085 extern void buddha_init(void);
3086 buddha_init();
3088 #endif /* CONFIG_BLK_DEV_BUDDHA */
3091 __initfunc(void ide_init_builtin_drivers (void))
3094 * Probe for special PCI and other "known" interface chipsets
3096 probe_for_hwifs ();
3098 #ifdef CONFIG_BLK_DEV_IDE
3099 #if defined(__mc68000__) || defined(CONFIG_APUS)
3100 if (ide_hwifs[0].io_ports[IDE_DATA_OFFSET]) {
3101 ide_get_lock(&ide_lock, NULL, NULL); /* for atari only */
3102 disable_irq(ide_hwifs[0].irq);
3104 #endif /* __mc68000__ || CONFIG_APUS */
3106 (void) ideprobe_init();
3108 #if defined(__mc68000__) || defined(CONFIG_APUS)
3109 if (ide_hwifs[0].io_ports[IDE_DATA_OFFSET]) {
3110 enable_irq(ide_hwifs[0].irq);
3111 ide_release_lock(&ide_lock); /* for atari only */
3113 #endif /* __mc68000__ || CONFIG_APUS */
3114 #endif /* CONFIG_BLK_DEV_IDE */
3116 #ifdef CONFIG_PROC_FS
3117 proc_ide_create();
3118 #endif
3121 * Attempt to match drivers for the available drives
3123 #ifdef CONFIG_BLK_DEV_IDEDISK
3124 (void) idedisk_init();
3125 #endif /* CONFIG_BLK_DEV_IDEDISK */
3126 #ifdef CONFIG_BLK_DEV_IDECD
3127 (void) ide_cdrom_init();
3128 #endif /* CONFIG_BLK_DEV_IDECD */
3129 #ifdef CONFIG_BLK_DEV_IDETAPE
3130 (void) idetape_init();
3131 #endif /* CONFIG_BLK_DEV_IDETAPE */
3132 #ifdef CONFIG_BLK_DEV_IDEFLOPPY
3133 (void) idefloppy_init();
3134 #endif /* CONFIG_BLK_DEV_IDEFLOPPY */
3135 #ifdef CONFIG_BLK_DEV_IDESCSI
3136 #ifdef CONFIG_SCSI
3137 (void) idescsi_init();
3138 #else
3139 #warning ide scsi-emulation selected but no SCSI-subsystem in kernel
3140 #endif
3141 #endif /* CONFIG_BLK_DEV_IDESCSI */
3144 static int default_cleanup (ide_drive_t *drive)
3146 return ide_unregister_subdriver(drive);
3149 static void default_do_request(ide_drive_t *drive, struct request *rq, unsigned long block)
3151 ide_end_request(0, HWGROUP(drive));
3154 static void default_end_request (byte uptodate, ide_hwgroup_t *hwgroup)
3156 ide_end_request(uptodate, hwgroup);
3159 static int default_ioctl (ide_drive_t *drive, struct inode *inode, struct file *file,
3160 unsigned int cmd, unsigned long arg)
3162 return -EIO;
3165 static int default_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
3167 drive->usage--;
3168 return -EIO;
3171 static void default_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
3175 static int default_check_media_change (ide_drive_t *drive)
3177 return 1;
3180 static void default_pre_reset (ide_drive_t *drive)
3184 static unsigned long default_capacity (ide_drive_t *drive)
3186 return 0x7fffffff; /* cdrom or tape */
3189 static void default_special (ide_drive_t *drive)
3191 special_t *s = &drive->special;
3193 s->all = 0;
3194 drive->mult_req = 0;
3197 static void setup_driver_defaults (ide_drive_t *drive)
3199 ide_driver_t *d = drive->driver;
3201 if (d->cleanup == NULL) d->cleanup = default_cleanup;
3202 if (d->do_request == NULL) d->do_request = default_do_request;
3203 if (d->end_request == NULL) d->end_request = default_end_request;
3204 if (d->ioctl == NULL) d->ioctl = default_ioctl;
3205 if (d->open == NULL) d->open = default_open;
3206 if (d->release == NULL) d->release = default_release;
3207 if (d->media_change == NULL) d->media_change = default_check_media_change;
3208 if (d->pre_reset == NULL) d->pre_reset = default_pre_reset;
3209 if (d->capacity == NULL) d->capacity = default_capacity;
3210 if (d->special == NULL) d->special = default_special;
3213 ide_drive_t *ide_scan_devices (byte media, const char *name, ide_driver_t *driver, int n)
3215 unsigned int unit, index, i;
3217 for (index = 0; index < MAX_HWIFS; ++index)
3218 if (ide_hwifs[index].present) goto search;
3219 ide_init_module(IDE_PROBE_MODULE);
3220 search:
3221 for (index = 0, i = 0; index < MAX_HWIFS; ++index) {
3222 ide_hwif_t *hwif = &ide_hwifs[index];
3223 if (!hwif->present)
3224 continue;
3225 for (unit = 0; unit < MAX_DRIVES; ++unit) {
3226 ide_drive_t *drive = &hwif->drives[unit];
3227 char *req = drive->driver_req;
3228 if (*req && !strstr(name, req))
3229 continue;
3230 if (drive->present && drive->media == media && drive->driver == driver && ++i > n)
3231 return drive;
3234 return NULL;
3237 #ifdef CONFIG_PROC_FS
3238 static ide_proc_entry_t generic_subdriver_entries[] = {
3239 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
3240 { NULL, 0, NULL, NULL }
3242 #endif
3244 int ide_register_subdriver (ide_drive_t *drive, ide_driver_t *driver, int version)
3246 unsigned long flags;
3248 save_flags(flags); /* all CPUs */
3249 cli(); /* all CPUs */
3250 if (version != IDE_SUBDRIVER_VERSION || !drive->present || drive->driver != NULL || drive->busy || drive->usage) {
3251 restore_flags(flags); /* all CPUs */
3252 return 1;
3254 drive->driver = driver;
3255 setup_driver_defaults(drive);
3256 restore_flags(flags); /* all CPUs */
3257 if (drive->autotune != 2) {
3258 if (driver->supports_dma && HWIF(drive)->dmaproc != NULL)
3259 (void) (HWIF(drive)->dmaproc(ide_dma_check, drive));
3260 drive->dsc_overlap = (drive->next != drive && driver->supports_dsc_overlap);
3261 drive->nice1 = 1;
3263 drive->revalidate = 1;
3264 #ifdef CONFIG_PROC_FS
3265 ide_add_proc_entries(drive->proc, generic_subdriver_entries, drive);
3266 ide_add_proc_entries(drive->proc, driver->proc, drive);
3267 #endif
3268 return 0;
3271 int ide_unregister_subdriver (ide_drive_t *drive)
3273 unsigned long flags;
3275 save_flags(flags); /* all CPUs */
3276 cli(); /* all CPUs */
3277 if (drive->usage || drive->busy || drive->driver == NULL || DRIVER(drive)->busy) {
3278 restore_flags(flags); /* all CPUs */
3279 return 1;
3281 #ifdef CONFIG_PROC_FS
3282 ide_remove_proc_entries(drive->proc, DRIVER(drive)->proc);
3283 ide_remove_proc_entries(drive->proc, generic_subdriver_entries);
3284 #endif
3285 auto_remove_settings(drive);
3286 drive->driver = NULL;
3287 restore_flags(flags); /* all CPUs */
3288 return 0;
3291 int ide_register_module (ide_module_t *module)
3293 ide_module_t *p = ide_modules;
3295 while (p) {
3296 if (p == module)
3297 return 1;
3298 p = p->next;
3300 module->next = ide_modules;
3301 ide_modules = module;
3302 revalidate_drives();
3303 return 0;
3306 void ide_unregister_module (ide_module_t *module)
3308 ide_module_t **p;
3310 for (p = &ide_modules; (*p) && (*p) != module; p = &((*p)->next));
3311 if (*p)
3312 *p = (*p)->next;
3315 struct file_operations ide_fops[] = {{
3316 NULL, /* lseek - default */
3317 block_read, /* read - general block-dev read */
3318 block_write, /* write - general block-dev write */
3319 NULL, /* readdir - bad */
3320 NULL, /* poll */
3321 ide_ioctl, /* ioctl */
3322 NULL, /* mmap */
3323 ide_open, /* open */
3324 NULL, /* flush */
3325 ide_release, /* release */
3326 block_fsync, /* fsync */
3327 NULL, /* fasync */
3328 ide_check_media_change, /* check_media_change */
3329 ide_revalidate_disk /* revalidate */
3332 EXPORT_SYMBOL(ide_hwifs);
3333 EXPORT_SYMBOL(ide_register_module);
3334 EXPORT_SYMBOL(ide_unregister_module);
3335 EXPORT_SYMBOL(ide_spin_wait_hwgroup);
3338 * Probe module
3340 EXPORT_SYMBOL(drive_is_flashcard);
3341 EXPORT_SYMBOL(ide_timer_expiry);
3342 EXPORT_SYMBOL(ide_intr);
3343 EXPORT_SYMBOL(ide_geninit);
3344 EXPORT_SYMBOL(ide_fops);
3345 EXPORT_SYMBOL(ide_get_queue);
3346 EXPORT_SYMBOL(do_ide0_request);
3347 EXPORT_SYMBOL(ide_add_generic_settings);
3348 #if MAX_HWIFS > 1
3349 EXPORT_SYMBOL(do_ide1_request);
3350 #endif /* MAX_HWIFS > 1 */
3351 #if MAX_HWIFS > 2
3352 EXPORT_SYMBOL(do_ide2_request);
3353 #endif /* MAX_HWIFS > 2 */
3354 #if MAX_HWIFS > 3
3355 EXPORT_SYMBOL(do_ide3_request);
3356 #endif /* MAX_HWIFS > 3 */
3357 #if MAX_HWIFS > 4
3358 EXPORT_SYMBOL(do_ide4_request);
3359 #endif /* MAX_HWIFS > 4 */
3360 #if MAX_HWIFS > 5
3361 EXPORT_SYMBOL(do_ide5_request);
3362 #endif /* MAX_HWIFS > 5 */
3363 #if MAX_HWIFS > 6
3364 EXPORT_SYMBOL(do_ide6_request);
3365 #endif /* MAX_HWIFS > 6 */
3366 #if MAX_HWIFS > 7
3367 EXPORT_SYMBOL(do_ide7_request);
3368 #endif /* MAX_HWIFS > 7 */
3371 * Driver module
3373 EXPORT_SYMBOL(ide_scan_devices);
3374 EXPORT_SYMBOL(ide_register_subdriver);
3375 EXPORT_SYMBOL(ide_unregister_subdriver);
3376 EXPORT_SYMBOL(ide_input_data);
3377 EXPORT_SYMBOL(ide_output_data);
3378 EXPORT_SYMBOL(atapi_input_bytes);
3379 EXPORT_SYMBOL(atapi_output_bytes);
3380 EXPORT_SYMBOL(ide_set_handler);
3381 EXPORT_SYMBOL(ide_dump_status);
3382 EXPORT_SYMBOL(ide_error);
3383 EXPORT_SYMBOL(ide_fixstring);
3384 EXPORT_SYMBOL(ide_wait_stat);
3385 EXPORT_SYMBOL(ide_do_reset);
3386 EXPORT_SYMBOL(ide_init_drive_cmd);
3387 EXPORT_SYMBOL(ide_do_drive_cmd);
3388 EXPORT_SYMBOL(ide_end_drive_cmd);
3389 EXPORT_SYMBOL(ide_end_request);
3390 EXPORT_SYMBOL(ide_revalidate_disk);
3391 EXPORT_SYMBOL(ide_cmd);
3392 EXPORT_SYMBOL(ide_wait_cmd);
3393 EXPORT_SYMBOL(ide_delay_50ms);
3394 EXPORT_SYMBOL(ide_stall_queue);
3395 #ifdef CONFIG_PROC_FS
3396 EXPORT_SYMBOL(ide_add_proc_entries);
3397 EXPORT_SYMBOL(ide_remove_proc_entries);
3398 EXPORT_SYMBOL(proc_ide_read_geometry);
3399 #endif
3400 EXPORT_SYMBOL(ide_add_setting);
3401 EXPORT_SYMBOL(ide_remove_setting);
3403 EXPORT_SYMBOL(ide_register_hw);
3404 EXPORT_SYMBOL(ide_register);
3405 EXPORT_SYMBOL(ide_unregister);
3406 EXPORT_SYMBOL(ide_setup_ports);
3409 * This is gets invoked once during initialization, to set *everything* up
3411 __initfunc(int ide_init (void))
3413 static char banner_printed = 0;
3415 if (!banner_printed) {
3416 printk(KERN_INFO "Uniform Multi-Platform E-IDE driver " REVISION "\n");
3417 banner_printed = 1;
3420 init_ide_data ();
3422 initializing = 1;
3423 ide_init_builtin_drivers();
3424 initializing = 0;
3426 return 0;
3429 #ifdef MODULE
3430 char *options = NULL;
3431 MODULE_PARM(options,"s");
3433 __initfunc(static void parse_options (char *line))
3435 char *next = line;
3437 if (line == NULL || !*line)
3438 return;
3439 while ((line = next) != NULL) {
3440 if ((next = strchr(line,' ')) != NULL)
3441 *next++ = 0;
3442 if (!strncmp(line,"ide",3) || (!strncmp(line,"hd",2) && line[2] != '='))
3443 ide_setup(line);
3447 int init_module (void)
3449 parse_options(options);
3450 return ide_init();
3453 void cleanup_module (void)
3455 int index;
3457 for (index = 0; index < MAX_HWIFS; ++index) {
3458 ide_unregister(index);
3459 #ifdef CONFIG_BLK_DEV_IDEDMA
3460 if (ide_hwifs[index].dma_base)
3461 (void) ide_release_dma(&ide_hwifs[index]);
3462 #endif /* CONFIG_BLK_DEV_IDEDMA */
3464 #ifdef CONFIG_PROC_FS
3465 proc_ide_destroy();
3466 #endif
3468 #endif /* MODULE */