Import 2.3.10pre1
[davej-history.git] / drivers / block / ide.c
blob6d324bea84895e4a532fe677ea504378d7aef930
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 */
300 || !strncmp(id->model, "HAGIWARA HPC", 12)) /* Hagiwara */
302 return 1; /* yes, it is a flash memory card */
305 return 0; /* no, it is not a flash memory card */
309 * ide_system_bus_speed() returns what we think is the system VESA/PCI
310 * bus speed (in MHz). This is used for calculating interface PIO timings.
311 * The default is 40 for known PCI systems, 50 otherwise.
312 * The "idebus=xx" parameter can be used to override this value.
313 * The actual value to be used is computed/displayed the first time through.
315 int ide_system_bus_speed (void)
317 if (!system_bus_speed) {
318 if (idebus_parameter)
319 system_bus_speed = idebus_parameter; /* user supplied value */
320 #ifdef CONFIG_PCI
321 else if (pci_present())
322 system_bus_speed = 40; /* safe default value for PCI */
323 #endif /* CONFIG_PCI */
324 else
325 system_bus_speed = 50; /* safe default value for VESA and PCI */
326 printk("ide: Assuming %dMHz system bus speed for PIO modes%s\n", system_bus_speed,
327 idebus_parameter ? "" : "; override with idebus=xx");
329 return system_bus_speed;
332 #if SUPPORT_VLB_SYNC
334 * Some localbus EIDE interfaces require a special access sequence
335 * when using 32-bit I/O instructions to transfer data. We call this
336 * the "vlb_sync" sequence, which consists of three successive reads
337 * of the sector count register location, with interrupts disabled
338 * to ensure that the reads all happen together.
340 static inline void do_vlb_sync (ide_ioreg_t port) {
341 (void) inb (port);
342 (void) inb (port);
343 (void) inb (port);
345 #endif /* SUPPORT_VLB_SYNC */
348 * This is used for most PIO data transfers *from* the IDE interface
350 void ide_input_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
352 byte io_32bit = drive->io_32bit;
354 if (io_32bit) {
355 #if SUPPORT_VLB_SYNC
356 if (io_32bit & 2) {
357 unsigned long flags;
358 __save_flags(flags); /* local CPU only */
359 __cli(); /* local CPU only */
360 do_vlb_sync(IDE_NSECTOR_REG);
361 insl(IDE_DATA_REG, buffer, wcount);
362 __restore_flags(flags); /* local CPU only */
363 } else
364 #endif /* SUPPORT_VLB_SYNC */
365 insl(IDE_DATA_REG, buffer, wcount);
366 } else {
367 #if SUPPORT_SLOW_DATA_PORTS
368 if (drive->slow) {
369 unsigned short *ptr = (unsigned short *) buffer;
370 while (wcount--) {
371 *ptr++ = inw_p(IDE_DATA_REG);
372 *ptr++ = inw_p(IDE_DATA_REG);
374 } else
375 #endif /* SUPPORT_SLOW_DATA_PORTS */
376 insw(IDE_DATA_REG, buffer, wcount<<1);
381 * This is used for most PIO data transfers *to* the IDE interface
383 void ide_output_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
385 byte io_32bit = drive->io_32bit;
387 if (io_32bit) {
388 #if SUPPORT_VLB_SYNC
389 if (io_32bit & 2) {
390 unsigned long flags;
391 __save_flags(flags); /* local CPU only */
392 __cli(); /* local CPU only */
393 do_vlb_sync(IDE_NSECTOR_REG);
394 outsl(IDE_DATA_REG, buffer, wcount);
395 __restore_flags(flags); /* local CPU only */
396 } else
397 #endif /* SUPPORT_VLB_SYNC */
398 outsl(IDE_DATA_REG, buffer, wcount);
399 } else {
400 #if SUPPORT_SLOW_DATA_PORTS
401 if (drive->slow) {
402 unsigned short *ptr = (unsigned short *) buffer;
403 while (wcount--) {
404 outw_p(*ptr++, IDE_DATA_REG);
405 outw_p(*ptr++, IDE_DATA_REG);
407 } else
408 #endif /* SUPPORT_SLOW_DATA_PORTS */
409 outsw(IDE_DATA_REG, buffer, wcount<<1);
414 * The following routines are mainly used by the ATAPI drivers.
416 * These routines will round up any request for an odd number of bytes,
417 * so if an odd bytecount is specified, be sure that there's at least one
418 * extra byte allocated for the buffer.
420 void atapi_input_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount)
422 ++bytecount;
423 #ifdef CONFIG_ATARI
424 if (MACH_IS_ATARI) {
425 /* Atari has a byte-swapped IDE interface */
426 insw_swapw(IDE_DATA_REG, buffer, bytecount / 2);
427 return;
429 #endif /* CONFIG_ATARI */
430 ide_input_data (drive, buffer, bytecount / 4);
431 if ((bytecount & 0x03) >= 2)
432 insw (IDE_DATA_REG, ((byte *)buffer) + (bytecount & ~0x03), 1);
435 void atapi_output_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount)
437 ++bytecount;
438 #ifdef CONFIG_ATARI
439 if (MACH_IS_ATARI) {
440 /* Atari has a byte-swapped IDE interface */
441 outsw_swapw(IDE_DATA_REG, buffer, bytecount / 2);
442 return;
444 #endif /* CONFIG_ATARI */
445 ide_output_data (drive, buffer, bytecount / 4);
446 if ((bytecount & 0x03) >= 2)
447 outsw (IDE_DATA_REG, ((byte *)buffer) + (bytecount & ~0x03), 1);
451 * Needed for PCI irq sharing
453 static inline int drive_is_ready (ide_drive_t *drive)
455 if (drive->waiting_for_dma)
456 return HWIF(drive)->dmaproc(ide_dma_test_irq, drive);
457 #if 0
458 udelay(1); /* need to guarantee 400ns since last command was issued */
459 #endif
460 if (GET_STAT() & BUSY_STAT)
461 return 0; /* drive busy: definitely not interrupting */
462 return 1; /* drive ready: *might* be interrupting */
466 * This is our end_request replacement function.
468 void ide_end_request(byte uptodate, ide_hwgroup_t *hwgroup)
470 struct request *rq;
471 unsigned long flags;
473 spin_lock_irqsave(&io_request_lock, flags);
474 rq = hwgroup->rq;
476 if (!end_that_request_first(rq, uptodate, hwgroup->drive->name)) {
477 add_blkdev_randomness(MAJOR(rq->rq_dev));
478 hwgroup->drive->queue = rq->next;
479 blk_dev[MAJOR(rq->rq_dev)].current_request = NULL;
480 hwgroup->rq = NULL;
481 end_that_request_last(rq);
483 spin_unlock_irqrestore(&io_request_lock, flags);
487 * This should get invoked any time we exit the driver to
488 * wait for an interrupt response from a drive. handler() points
489 * at the appropriate code to handle the next interrupt, and a
490 * timer is started to prevent us from waiting forever in case
491 * something goes wrong (see the ide_timer_expiry() handler later on).
493 void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler, unsigned int timeout)
495 unsigned long flags;
496 ide_hwgroup_t *hwgroup = HWGROUP(drive);
498 spin_lock_irqsave(&hwgroup->spinlock, flags);
499 #ifdef DEBUG
500 if (hwgroup->handler != NULL) {
501 printk("%s: ide_set_handler: handler not null; old=%p, new=%p\n",
502 drive->name, hwgroup->handler, handler);
504 #endif
505 hwgroup->handler = handler;
506 hwgroup->timer.expires = jiffies + timeout;
507 add_timer(&(hwgroup->timer));
508 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
512 * current_capacity() returns the capacity (in sectors) of a drive
513 * according to its current geometry/LBA settings.
515 static unsigned long current_capacity (ide_drive_t *drive)
517 if (!drive->present)
518 return 0;
519 if (drive->driver != NULL)
520 return DRIVER(drive)->capacity(drive);
521 return 0;
525 * ide_geninit() is called exactly *once* for each major, from genhd.c,
526 * at the beginning of the initial partition check for the drives.
528 void ide_geninit (struct gendisk *gd)
530 unsigned int unit;
531 ide_hwif_t *hwif = gd->real_devices;
533 for (unit = 0; unit < gd->nr_real; ++unit) {
534 ide_drive_t *drive = &hwif->drives[unit];
536 drive->part[0].nr_sects = current_capacity(drive);
537 if (!drive->present || (drive->media != ide_disk && drive->media != ide_floppy) ||
538 drive->driver == NULL || !drive->part[0].nr_sects)
539 drive->part[0].start_sect = -1; /* skip partition check */
543 static void do_reset1 (ide_drive_t *, int); /* needed below */
546 * atapi_reset_pollfunc() gets invoked to poll the interface for completion every 50ms
547 * during an atapi drive reset operation. If the drive has not yet responded,
548 * and we have not yet hit our maximum waiting time, then the timer is restarted
549 * for another 50ms.
551 static void atapi_reset_pollfunc (ide_drive_t *drive)
553 ide_hwgroup_t *hwgroup = HWGROUP(drive);
554 byte stat;
556 SELECT_DRIVE(HWIF(drive),drive);
557 udelay (10);
559 if (OK_STAT(stat=GET_STAT(), 0, BUSY_STAT)) {
560 printk("%s: ATAPI reset complete\n", drive->name);
561 } else {
562 if (0 < (signed long)(hwgroup->poll_timeout - jiffies)) {
563 ide_set_handler (drive, &atapi_reset_pollfunc, HZ/20);
564 return; /* continue polling */
566 hwgroup->poll_timeout = 0; /* end of polling */
567 printk("%s: ATAPI reset timed-out, status=0x%02x\n", drive->name, stat);
568 do_reset1 (drive, 1); /* do it the old fashioned way */
569 return;
571 hwgroup->poll_timeout = 0; /* done polling */
575 * reset_pollfunc() gets invoked to poll the interface for completion every 50ms
576 * during an ide reset operation. If the drives have not yet responded,
577 * and we have not yet hit our maximum waiting time, then the timer is restarted
578 * for another 50ms.
580 static void reset_pollfunc (ide_drive_t *drive)
582 ide_hwgroup_t *hwgroup = HWGROUP(drive);
583 ide_hwif_t *hwif = HWIF(drive);
584 byte tmp;
586 if (!OK_STAT(tmp=GET_STAT(), 0, BUSY_STAT)) {
587 if (0 < (signed long)(hwgroup->poll_timeout - jiffies)) {
588 ide_set_handler (drive, &reset_pollfunc, HZ/20);
589 return; /* continue polling */
591 printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
592 } else {
593 printk("%s: reset: ", hwif->name);
594 if ((tmp = GET_ERR()) == 1)
595 printk("success\n");
596 else {
597 #if FANCY_STATUS_DUMPS
598 printk("master: ");
599 switch (tmp & 0x7f) {
600 case 1: printk("passed");
601 break;
602 case 2: printk("formatter device error");
603 break;
604 case 3: printk("sector buffer error");
605 break;
606 case 4: printk("ECC circuitry error");
607 break;
608 case 5: printk("controlling MPU error");
609 break;
610 default:printk("error (0x%02x?)", tmp);
612 if (tmp & 0x80)
613 printk("; slave: failed");
614 printk("\n");
615 #else
616 printk("failed\n");
617 #endif /* FANCY_STATUS_DUMPS */
620 hwgroup->poll_timeout = 0; /* done polling */
623 static void pre_reset (ide_drive_t *drive)
625 if (!drive->keep_settings) {
626 drive->unmask = 0;
627 drive->io_32bit = 0;
628 if (drive->using_dma)
629 (void) HWIF(drive)->dmaproc(ide_dma_off, drive);
631 if (drive->driver != NULL)
632 DRIVER(drive)->pre_reset(drive);
636 * do_reset1() attempts to recover a confused drive by resetting it.
637 * Unfortunately, resetting a disk drive actually resets all devices on
638 * the same interface, so it can really be thought of as resetting the
639 * interface rather than resetting the drive.
641 * ATAPI devices have their own reset mechanism which allows them to be
642 * individually reset without clobbering other devices on the same interface.
644 * Unfortunately, the IDE interface does not generate an interrupt to let
645 * us know when the reset operation has finished, so we must poll for this.
646 * Equally poor, though, is the fact that this may a very long time to complete,
647 * (up to 30 seconds worstcase). So, instead of busy-waiting here for it,
648 * we set a timer to poll at 50ms intervals.
650 static void do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
652 unsigned int unit;
653 unsigned long flags;
654 ide_hwif_t *hwif = HWIF(drive);
655 ide_hwgroup_t *hwgroup = HWGROUP(drive);
657 __save_flags(flags); /* local CPU only */
658 __cli(); /* local CPU only */
660 /* For an ATAPI device, first try an ATAPI SRST. */
661 if (drive->media != ide_disk && !do_not_try_atapi) {
662 pre_reset(drive);
663 SELECT_DRIVE(hwif,drive);
664 udelay (20);
665 OUT_BYTE (WIN_SRST, IDE_COMMAND_REG);
666 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
667 ide_set_handler (drive, &atapi_reset_pollfunc, HZ/20);
668 __restore_flags (flags); /* local CPU only */
669 return;
673 * First, reset any device state data we were maintaining
674 * for any of the drives on this interface.
676 for (unit = 0; unit < MAX_DRIVES; ++unit)
677 pre_reset(&hwif->drives[unit]);
679 #if OK_TO_RESET_CONTROLLER
680 if (!IDE_CONTROL_REG) {
681 __restore_flags(flags);
682 return;
685 * Note that we also set nIEN while resetting the device,
686 * to mask unwanted interrupts from the interface during the reset.
687 * However, due to the design of PC hardware, this will cause an
688 * immediate interrupt due to the edge transition it produces.
689 * This single interrupt gives us a "fast poll" for drives that
690 * recover from reset very quickly, saving us the first 50ms wait time.
692 OUT_BYTE(drive->ctl|6,IDE_CONTROL_REG); /* set SRST and nIEN */
693 udelay(10); /* more than enough time */
694 OUT_BYTE(drive->ctl|2,IDE_CONTROL_REG); /* clear SRST, leave nIEN */
695 udelay(10); /* more than enough time */
696 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
697 ide_set_handler (drive, &reset_pollfunc, HZ/20);
698 #endif /* OK_TO_RESET_CONTROLLER */
700 __restore_flags (flags); /* local CPU only */
704 * ide_do_reset() is the entry point to the drive/interface reset code.
706 void ide_do_reset (ide_drive_t *drive)
708 do_reset1 (drive, 0);
712 * Clean up after success/failure of an explicit drive cmd
714 void ide_end_drive_cmd (ide_drive_t *drive, byte stat, byte err)
716 unsigned long flags;
717 struct request *rq = HWGROUP(drive)->rq;
719 if (rq->cmd == IDE_DRIVE_CMD) {
720 byte *args = (byte *) rq->buffer;
721 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
722 if (args) {
723 args[0] = stat;
724 args[1] = err;
725 args[2] = IN_BYTE(IDE_NSECTOR_REG);
728 spin_lock_irqsave(&io_request_lock, flags);
729 drive->queue = rq->next;
730 blk_dev[MAJOR(rq->rq_dev)].current_request = NULL;
731 HWGROUP(drive)->rq = NULL;
732 rq->rq_status = RQ_INACTIVE;
733 spin_unlock_irqrestore(&io_request_lock, flags);
734 save_flags(flags); /* all CPUs; overkill? */
735 cli(); /* all CPUs; overkill? */
736 if (rq->sem != NULL)
737 up(rq->sem); /* inform originator that rq has been serviced */
738 restore_flags(flags); /* all CPUs; overkill? */
742 * Error reporting, in human readable form (luxurious, but a memory hog).
744 byte ide_dump_status (ide_drive_t *drive, const char *msg, byte stat)
746 unsigned long flags;
747 byte err = 0;
749 __save_flags (flags); /* local CPU only */
750 ide__sti(); /* local CPU only */
751 printk("%s: %s: status=0x%02x", drive->name, msg, stat);
752 #if FANCY_STATUS_DUMPS
753 printk(" { ");
754 if (stat & BUSY_STAT)
755 printk("Busy ");
756 else {
757 if (stat & READY_STAT) printk("DriveReady ");
758 if (stat & WRERR_STAT) printk("DeviceFault ");
759 if (stat & SEEK_STAT) printk("SeekComplete ");
760 if (stat & DRQ_STAT) printk("DataRequest ");
761 if (stat & ECC_STAT) printk("CorrectedError ");
762 if (stat & INDEX_STAT) printk("Index ");
763 if (stat & ERR_STAT) printk("Error ");
765 printk("}");
766 #endif /* FANCY_STATUS_DUMPS */
767 printk("\n");
768 if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
769 err = GET_ERR();
770 printk("%s: %s: error=0x%02x", drive->name, msg, err);
771 #if FANCY_STATUS_DUMPS
772 if (drive->media == ide_disk) {
773 printk(" { ");
774 if (err & ABRT_ERR) printk("DriveStatusError ");
775 if (err & ICRC_ERR) printk((err & ABRT_ERR) ? "BadCRC " : "BadSector ");
776 if (err & ECC_ERR) printk("UncorrectableError ");
777 if (err & ID_ERR) printk("SectorIdNotFound ");
778 if (err & TRK0_ERR) printk("TrackZeroNotFound ");
779 if (err & MARK_ERR) printk("AddrMarkNotFound ");
780 printk("}");
781 if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR || (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
782 byte cur = IN_BYTE(IDE_SELECT_REG);
783 if (cur & 0x40) { /* using LBA? */
784 printk(", LBAsect=%ld", (unsigned long)
785 ((cur&0xf)<<24)
786 |(IN_BYTE(IDE_HCYL_REG)<<16)
787 |(IN_BYTE(IDE_LCYL_REG)<<8)
788 | IN_BYTE(IDE_SECTOR_REG));
789 } else {
790 printk(", CHS=%d/%d/%d",
791 (IN_BYTE(IDE_HCYL_REG)<<8) +
792 IN_BYTE(IDE_LCYL_REG),
793 cur & 0xf,
794 IN_BYTE(IDE_SECTOR_REG));
796 if (HWGROUP(drive)->rq)
797 printk(", sector=%ld", HWGROUP(drive)->rq->sector);
800 #endif /* FANCY_STATUS_DUMPS */
801 printk("\n");
803 __restore_flags (flags); /* local CPU only */
804 return err;
808 * try_to_flush_leftover_data() is invoked in response to a drive
809 * unexpectedly having its DRQ_STAT bit set. As an alternative to
810 * resetting the drive, this routine tries to clear the condition
811 * by read a sector's worth of data from the drive. Of course,
812 * this may not help if the drive is *waiting* for data from *us*.
814 static void try_to_flush_leftover_data (ide_drive_t *drive)
816 int i = (drive->mult_count ? drive->mult_count : 1) * SECTOR_WORDS;
818 if (drive->media != ide_disk)
819 return;
820 while (i > 0) {
821 unsigned long buffer[16];
822 unsigned int wcount = (i > 16) ? 16 : i;
823 i -= wcount;
824 ide_input_data (drive, buffer, wcount);
829 * ide_error() takes action based on the error returned by the drive.
831 void ide_error (ide_drive_t *drive, const char *msg, byte stat)
833 struct request *rq;
834 byte err;
836 err = ide_dump_status(drive, msg, stat);
837 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
838 return;
839 /* retry only "normal" I/O: */
840 if (rq->cmd == IDE_DRIVE_CMD) {
841 rq->errors = 1;
842 ide_end_drive_cmd(drive, stat, err);
843 return;
845 if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) { /* other bits are useless when BUSY */
846 rq->errors |= ERROR_RESET;
847 } else {
848 if (drive->media == ide_disk && (stat & ERR_STAT)) {
849 /* err has different meaning on cdrom and tape */
850 if (err == ABRT_ERR) {
851 if (drive->select.b.lba && IN_BYTE(IDE_COMMAND_REG) == WIN_SPECIFY)
852 return; /* some newer drives don't support WIN_SPECIFY */
853 } else if ((err & (ABRT_ERR | ICRC_ERR)) == (ABRT_ERR | ICRC_ERR))
854 ; /* UDMA crc error -- just retry the operation */
855 else if (err & (BBD_ERR | ECC_ERR)) /* retries won't help these */
856 rq->errors = ERROR_MAX;
857 else if (err & TRK0_ERR) /* help it find track zero */
858 rq->errors |= ERROR_RECAL;
860 if ((stat & DRQ_STAT) && rq->cmd != WRITE)
861 try_to_flush_leftover_data(drive);
863 if (GET_STAT() & (BUSY_STAT|DRQ_STAT))
864 rq->errors |= ERROR_RESET; /* Mmmm.. timing problem */
866 if (rq->errors >= ERROR_MAX) {
867 if (drive->driver != NULL)
868 DRIVER(drive)->end_request(0, HWGROUP(drive));
869 else
870 ide_end_request(0, HWGROUP(drive));
871 } else {
872 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
873 ++rq->errors;
874 ide_do_reset(drive);
875 return;
876 } else if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
877 drive->special.b.recalibrate = 1;
878 ++rq->errors;
883 * Issue a simple drive command
884 * The drive must be selected beforehand.
886 void ide_cmd(ide_drive_t *drive, byte cmd, byte nsect, ide_handler_t *handler)
888 ide_set_handler (drive, handler, WAIT_CMD);
889 if (IDE_CONTROL_REG)
890 OUT_BYTE(drive->ctl,IDE_CONTROL_REG); /* clear nIEN */
891 OUT_BYTE(nsect,IDE_NSECTOR_REG);
892 OUT_BYTE(cmd,IDE_COMMAND_REG);
896 * drive_cmd_intr() is invoked on completion of a special DRIVE_CMD.
898 static void drive_cmd_intr (ide_drive_t *drive)
900 struct request *rq = HWGROUP(drive)->rq;
901 byte *args = (byte *) rq->buffer;
902 byte stat = GET_STAT();
903 int retries = 10;
905 ide__sti(); /* local CPU only */
906 if ((stat & DRQ_STAT) && args && args[3]) {
907 byte io_32bit = drive->io_32bit;
908 drive->io_32bit = 0;
909 ide_input_data(drive, &args[4], args[3] * SECTOR_WORDS);
910 drive->io_32bit = io_32bit;
911 while (((stat = GET_STAT()) & BUSY_STAT) && retries--)
912 udelay(100);
915 if (OK_STAT(stat, READY_STAT, BAD_STAT))
916 ide_end_drive_cmd (drive, stat, GET_ERR());
917 else
918 ide_error(drive, "drive_cmd", stat); /* calls ide_end_drive_cmd */
922 * do_special() is used to issue WIN_SPECIFY, WIN_RESTORE, and WIN_SETMULT
923 * commands to a drive. It used to do much more, but has been scaled back.
925 static inline void do_special (ide_drive_t *drive)
927 special_t *s = &drive->special;
929 #ifdef DEBUG
930 printk("%s: do_special: 0x%02x\n", drive->name, s->all);
931 #endif
932 if (s->b.set_tune) {
933 ide_tuneproc_t *tuneproc = HWIF(drive)->tuneproc;
934 s->b.set_tune = 0;
935 if (tuneproc != NULL)
936 tuneproc(drive, drive->tune_req);
937 } else if (drive->driver != NULL) {
938 DRIVER(drive)->special(drive);
939 } else if (s->all) {
940 printk("%s: bad special flag: 0x%02x\n", drive->name, s->all);
941 s->all = 0;
946 * This routine busy-waits for the drive status to be not "busy".
947 * It then checks the status for all of the "good" bits and none
948 * of the "bad" bits, and if all is okay it returns 0. All other
949 * cases return 1 after invoking ide_error() -- caller should just return.
951 * This routine should get fixed to not hog the cpu during extra long waits..
952 * That could be done by busy-waiting for the first jiffy or two, and then
953 * setting a timer to wake up at half second intervals thereafter,
954 * until timeout is achieved, before timing out.
956 int ide_wait_stat (ide_drive_t *drive, byte good, byte bad, unsigned long timeout)
958 byte stat;
959 int i;
960 unsigned long flags;
962 udelay(1); /* spec allows drive 400ns to assert "BUSY" */
963 if ((stat = GET_STAT()) & BUSY_STAT) {
964 __save_flags(flags); /* local CPU only */
965 ide__sti(); /* local CPU only */
966 timeout += jiffies;
967 while ((stat = GET_STAT()) & BUSY_STAT) {
968 if (0 < (signed long)(jiffies - timeout)) {
969 __restore_flags(flags); /* local CPU only */
970 ide_error(drive, "status timeout", stat);
971 return 1;
974 __restore_flags(flags); /* local CPU only */
977 * Allow status to settle, then read it again.
978 * A few rare drives vastly violate the 400ns spec here,
979 * so we'll wait up to 10usec for a "good" status
980 * rather than expensively fail things immediately.
981 * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
983 for (i = 0; i < 10; i++) {
984 udelay(1);
985 if (OK_STAT((stat = GET_STAT()), good, bad))
986 return 0;
988 ide_error(drive, "status error", stat);
989 return 1;
993 * execute_drive_cmd() issues a special drive command,
994 * usually initiated by ioctl() from the external hdparm program.
996 static void execute_drive_cmd (ide_drive_t *drive, struct request *rq)
998 byte *args = rq->buffer;
999 if (args) {
1000 #ifdef DEBUG
1001 printk("%s: DRIVE_CMD cmd=0x%02x sc=0x%02x fr=0x%02x xx=0x%02x\n",
1002 drive->name, args[0], args[1], args[2], args[3]);
1003 #endif
1004 if (args[0] == WIN_SMART) {
1005 OUT_BYTE(0x4f, IDE_LCYL_REG);
1006 OUT_BYTE(0xc2, IDE_HCYL_REG);
1008 OUT_BYTE(args[2],IDE_FEATURE_REG);
1009 ide_cmd(drive, args[0], args[1], &drive_cmd_intr);
1010 return;
1011 } else {
1013 * NULL is actually a valid way of waiting for
1014 * all current requests to be flushed from the queue.
1016 #ifdef DEBUG
1017 printk("%s: DRIVE_CMD (null)\n", drive->name);
1018 #endif
1019 ide_end_drive_cmd(drive, GET_STAT(), GET_ERR());
1020 return;
1025 * start_request() initiates handling of a new I/O request
1027 static inline void start_request (ide_drive_t *drive)
1029 unsigned long block, blockend;
1030 struct request *rq = drive->queue;
1031 unsigned int minor = MINOR(rq->rq_dev), unit = minor >> PARTN_BITS;
1032 ide_hwif_t *hwif = HWIF(drive);
1034 ide__sti(); /* local CPU only */
1035 #ifdef DEBUG
1036 printk("%s: start_request: current=0x%08lx\n", hwif->name, (unsigned long) rq);
1037 #endif
1038 if (unit >= MAX_DRIVES) {
1039 printk("%s: bad device number: %s\n", hwif->name, kdevname(rq->rq_dev));
1040 goto kill_rq;
1042 #ifdef DEBUG
1043 if (rq->bh && !buffer_locked(rq->bh)) {
1044 printk("%s: block not locked\n", drive->name);
1045 goto kill_rq;
1047 #endif
1048 block = rq->sector;
1049 blockend = block + rq->nr_sectors;
1050 if ((blockend < block) || (blockend > drive->part[minor&PARTN_MASK].nr_sects)) {
1051 printk("%s%c: bad access: block=%ld, count=%ld\n", drive->name,
1052 (minor&PARTN_MASK)?'0'+(minor&PARTN_MASK):' ', block, rq->nr_sectors);
1053 goto kill_rq;
1055 block += drive->part[minor&PARTN_MASK].start_sect + drive->sect0;
1056 #if FAKE_FDISK_FOR_EZDRIVE
1057 if (block == 0 && drive->remap_0_to_1)
1058 block = 1; /* redirect MBR access to EZ-Drive partn table */
1059 #endif /* FAKE_FDISK_FOR_EZDRIVE */
1060 #if (DISK_RECOVERY_TIME > 0)
1061 while ((read_timer() - hwif->last_time) < DISK_RECOVERY_TIME);
1062 #endif
1063 SELECT_DRIVE(hwif, drive);
1064 if (ide_wait_stat(drive, drive->ready_stat, BUSY_STAT|DRQ_STAT, WAIT_READY)) {
1065 printk("%s: drive not ready for command\n", drive->name);
1066 return;
1068 if (!drive->special.all) {
1069 if (rq->cmd == IDE_DRIVE_CMD) {
1070 execute_drive_cmd(drive, rq);
1071 return;
1073 if (drive->driver != NULL) {
1074 DRIVER(drive)->do_request(drive, rq, block);
1075 return;
1077 printk("%s: media type %d not supported\n", drive->name, drive->media);
1078 goto kill_rq;
1080 do_special(drive);
1081 return;
1082 kill_rq:
1083 if (drive->driver != NULL)
1084 DRIVER(drive)->end_request(0, HWGROUP(drive));
1085 else
1086 ide_end_request(0, HWGROUP(drive));
1090 * ide_stall_queue() can be used by a drive to give excess bandwidth back
1091 * to the hwgroup by sleeping for timeout jiffies.
1093 void ide_stall_queue (ide_drive_t *drive, unsigned long timeout)
1095 if (timeout > WAIT_WORSTCASE)
1096 timeout = WAIT_WORSTCASE;
1097 drive->sleep = timeout + jiffies;
1100 #define WAKEUP(drive) ((drive)->service_start + 2 * (drive)->service_time)
1103 * choose_drive() selects the next drive which will be serviced.
1105 static inline ide_drive_t *choose_drive (ide_hwgroup_t *hwgroup)
1107 ide_drive_t *drive, *best;
1109 repeat:
1110 best = NULL;
1111 drive = hwgroup->drive;
1112 do {
1113 if (drive->queue && (!drive->sleep || 0 <= (signed long)(jiffies - drive->sleep))) {
1114 if (!best
1115 || (drive->sleep && (!best->sleep || 0 < (signed long)(best->sleep - drive->sleep)))
1116 || (!best->sleep && 0 < (signed long)(WAKEUP(best) - WAKEUP(drive))))
1118 struct blk_dev_struct *bdev = &blk_dev[HWIF(drive)->major];
1119 if (bdev->current_request != &bdev->plug)
1120 best = drive;
1123 } while ((drive = drive->next) != hwgroup->drive);
1124 if (best && best->nice1 && !best->sleep && best != hwgroup->drive && best->service_time > WAIT_MIN_SLEEP) {
1125 long t = (signed long)(WAKEUP(best) - jiffies);
1126 if (t >= WAIT_MIN_SLEEP) {
1128 * We *may* have some time to spare, but first let's see if
1129 * someone can potentially benefit from our nice mood today..
1131 drive = best->next;
1132 do {
1133 if (!drive->sleep
1134 && 0 < (signed long)(WAKEUP(drive) - (jiffies - best->service_time))
1135 && 0 < (signed long)((jiffies + t) - WAKEUP(drive)))
1137 ide_stall_queue(best, IDE_MIN(t, 10 * WAIT_MIN_SLEEP));
1138 goto repeat;
1140 } while ((drive = drive->next) != best);
1143 return best;
1147 * Caller must have already acquired spinlock using *spinflags
1149 static void ide_do_request (ide_hwgroup_t *hwgroup, unsigned long *hwgroup_flags, int masked_irq)
1151 struct blk_dev_struct *bdev;
1152 ide_drive_t *drive;
1153 ide_hwif_t *hwif;
1154 unsigned long io_flags;
1156 hwgroup->busy = 1;
1157 while (hwgroup->handler == NULL) {
1158 spin_lock_irqsave(&io_request_lock, io_flags);
1159 drive = choose_drive(hwgroup);
1160 if (drive == NULL) {
1161 unsigned long sleep = 0;
1163 hwgroup->rq = NULL;
1164 drive = hwgroup->drive;
1165 do {
1166 bdev = &blk_dev[HWIF(drive)->major];
1167 if (bdev->current_request != &bdev->plug) /* FIXME: this will do for now */
1168 bdev->current_request = NULL; /* (broken since patch-2.1.15) */
1169 if (drive->sleep && (!sleep || 0 < (signed long)(sleep - drive->sleep)))
1170 sleep = drive->sleep;
1171 } while ((drive = drive->next) != hwgroup->drive);
1172 spin_unlock_irqrestore(&io_request_lock, io_flags);
1173 if (sleep) {
1174 if (0 < (signed long)(jiffies + WAIT_MIN_SLEEP - sleep))
1175 sleep = jiffies + WAIT_MIN_SLEEP;
1176 #if 1
1177 if (hwgroup->timer.next || hwgroup->timer.prev)
1178 printk("ide_set_handler: timer already active\n");
1179 #endif
1180 mod_timer(&hwgroup->timer, sleep);
1181 } else {
1182 /* Ugly, but how can we sleep for the lock otherwise? perhaps from tq_scheduler? */
1183 ide_release_lock(&ide_lock); /* for atari only */
1185 hwgroup->busy = 0;
1186 return;
1188 hwif = HWIF(drive);
1189 if (hwgroup->hwif->sharing_irq && hwif != hwgroup->hwif) /* set nIEN for previous hwif */
1190 OUT_BYTE(hwgroup->drive->ctl|2, hwgroup->hwif->io_ports[IDE_CONTROL_OFFSET]);
1191 hwgroup->hwif = hwif;
1192 hwgroup->drive = drive;
1193 drive->sleep = 0;
1194 drive->service_start = jiffies;
1196 bdev = &blk_dev[hwif->major];
1197 if (bdev->current_request == &bdev->plug) /* FIXME: paranoia */
1198 printk("%s: Huh? nuking plugged queue\n", drive->name);
1199 bdev->current_request = hwgroup->rq = drive->queue;
1200 spin_unlock_irqrestore(&io_request_lock, io_flags);
1202 if (hwif->irq != masked_irq)
1203 disable_irq(hwif->irq);
1204 spin_unlock_irqrestore(&hwgroup->spinlock, *hwgroup_flags);
1205 start_request(drive);
1206 spin_lock_irqsave(&hwgroup->spinlock, *hwgroup_flags);
1207 if (hwif->irq != masked_irq)
1208 enable_irq(hwif->irq);
1213 * ide_get_queue() returns the queue which corresponds to a given device.
1215 struct request **ide_get_queue (kdev_t dev)
1217 ide_hwif_t *hwif = (ide_hwif_t *)blk_dev[MAJOR(dev)].data;
1219 return &hwif->drives[DEVICE_NR(dev) & 1].queue;
1223 * do_hwgroup_request() invokes ide_do_request() after claiming hwgroup->busy.
1225 static void do_hwgroup_request (ide_hwgroup_t *hwgroup)
1227 unsigned long flags;
1229 spin_lock_irqsave(&hwgroup->spinlock, flags);
1230 if (hwgroup->busy) {
1231 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1232 return;
1234 del_timer(&hwgroup->timer);
1235 ide_get_lock(&ide_lock, ide_intr, hwgroup); /* for atari only */
1236 ide_do_request(hwgroup, &flags, 0);
1237 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1241 * ll_rw_blk.c invokes our do_idex_request() function
1242 * with the io_request_spinlock already grabbed.
1243 * Since we need to do our own spinlock's internally,
1244 * on paths that don't necessarily originate through the
1245 * do_idex_request() path, we have to undo the spinlock on entry,
1246 * and restore it again on exit.
1247 * Fortunately, this is mostly a nop for non-SMP kernels.
1249 static inline void unlock_do_hwgroup_request (ide_hwgroup_t *hwgroup)
1251 spin_unlock(&io_request_lock);
1252 do_hwgroup_request (hwgroup);
1253 spin_lock_irq(&io_request_lock);
1256 void do_ide0_request (void)
1258 unlock_do_hwgroup_request (ide_hwifs[0].hwgroup);
1261 #if MAX_HWIFS > 1
1262 void do_ide1_request (void)
1264 unlock_do_hwgroup_request (ide_hwifs[1].hwgroup);
1266 #endif /* MAX_HWIFS > 1 */
1268 #if MAX_HWIFS > 2
1269 void do_ide2_request (void)
1271 unlock_do_hwgroup_request (ide_hwifs[2].hwgroup);
1273 #endif /* MAX_HWIFS > 2 */
1275 #if MAX_HWIFS > 3
1276 void do_ide3_request (void)
1278 unlock_do_hwgroup_request (ide_hwifs[3].hwgroup);
1280 #endif /* MAX_HWIFS > 3 */
1282 #if MAX_HWIFS > 4
1283 void do_ide4_request (void)
1285 unlock_do_hwgroup_request (ide_hwifs[4].hwgroup);
1287 #endif /* MAX_HWIFS > 4 */
1289 #if MAX_HWIFS > 5
1290 void do_ide5_request (void)
1292 unlock_do_hwgroup_request (ide_hwifs[5].hwgroup);
1294 #endif /* MAX_HWIFS > 5 */
1296 #if MAX_HWIFS > 6
1297 void do_ide6_request (void)
1299 unlock_do_hwgroup_request (ide_hwifs[6].hwgroup);
1301 #endif /* MAX_HWIFS > 6 */
1303 #if MAX_HWIFS > 7
1304 void do_ide7_request (void)
1306 unlock_do_hwgroup_request (ide_hwifs[7].hwgroup);
1308 #endif /* MAX_HWIFS > 7 */
1310 static void start_next_request (ide_hwgroup_t *hwgroup, int masked_irq)
1312 unsigned long flags;
1313 ide_drive_t *drive;
1315 spin_lock_irqsave(&hwgroup->spinlock, flags);
1316 if (hwgroup->handler != NULL) {
1317 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1318 return;
1320 drive = hwgroup->drive;
1321 set_recovery_timer(HWIF(drive));
1322 drive->service_time = jiffies - drive->service_start;
1323 ide_do_request(hwgroup, &flags, masked_irq);
1324 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1327 void ide_timer_expiry (unsigned long data)
1329 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *) data;
1330 ide_drive_t *drive;
1331 ide_handler_t *handler;
1332 unsigned long flags;
1334 spin_lock_irqsave(&hwgroup->spinlock, flags);
1335 drive = hwgroup->drive;
1336 if ((handler = hwgroup->handler) == NULL) {
1337 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1338 do_hwgroup_request(hwgroup);
1339 return;
1341 hwgroup->busy = 1; /* should already be "1" */
1342 hwgroup->handler = NULL;
1343 del_timer(&hwgroup->timer); /* Is this needed?? */
1344 if (hwgroup->poll_timeout != 0) { /* polling in progress? */
1345 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1346 handler(drive);
1347 } else if (drive_is_ready(drive)) {
1348 printk("%s: lost interrupt\n", drive->name);
1349 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1350 handler(drive);
1351 } else {
1352 if (drive->waiting_for_dma) {
1353 (void) hwgroup->hwif->dmaproc(ide_dma_end, drive);
1354 printk("%s: timeout waiting for DMA\n", drive->name);
1356 * need something here for HX PIIX3 UDMA and HPT343.......AMH
1357 * irq timeout: status=0x58 { DriveReady SeekComplete DataRequest }
1360 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1361 ide_error(drive, "irq timeout", GET_STAT());
1363 start_next_request(hwgroup, 0);
1367 * There's nothing really useful we can do with an unexpected interrupt,
1368 * other than reading the status register (to clear it), and logging it.
1369 * There should be no way that an irq can happen before we're ready for it,
1370 * so we needn't worry much about losing an "important" interrupt here.
1372 * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
1373 * drive enters "idle", "standby", or "sleep" mode, so if the status looks
1374 * "good", we just ignore the interrupt completely.
1376 * This routine assumes __cli() is in effect when called.
1378 * If an unexpected interrupt happens on irq15 while we are handling irq14
1379 * and if the two interfaces are "serialized" (CMD640), then it looks like
1380 * we could screw up by interfering with a new request being set up for irq15.
1382 * In reality, this is a non-issue. The new command is not sent unless the
1383 * drive is ready to accept one, in which case we know the drive is not
1384 * trying to interrupt us. And ide_set_handler() is always invoked before
1385 * completing the issuance of any new drive command, so we will not be
1386 * accidently invoked as a result of any valid command completion interrupt.
1389 static void unexpected_intr (int irq, ide_hwgroup_t *hwgroup)
1391 byte stat;
1392 ide_hwif_t *hwif = hwgroup->hwif;
1395 * handle the unexpected interrupt
1397 do {
1398 if (hwif->irq == irq) {
1399 stat = IN_BYTE(hwif->io_ports[IDE_STATUS_OFFSET]);
1400 if (!OK_STAT(stat, READY_STAT, BAD_STAT)) {
1401 /* Try to not flood the console with msgs */
1402 static unsigned long last_msgtime = 0, count = 0;
1403 ++count;
1404 if (0 < (signed long)(jiffies - (last_msgtime + HZ))) {
1405 last_msgtime = jiffies;
1406 printk("%s%s: unexpected interrupt, status=0x%02x, count=%ld\n",
1407 hwif->name, (hwif->next == hwgroup->hwif) ? "" : "(?)", stat, count);
1411 } while ((hwif = hwif->next) != hwgroup->hwif);
1414 * entry point for all interrupts, caller does __cli() for us
1416 void ide_intr (int irq, void *dev_id, struct pt_regs *regs)
1418 unsigned long flags;
1419 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *)dev_id;
1420 ide_hwif_t *hwif;
1421 ide_drive_t *drive;
1422 ide_handler_t *handler;
1424 __cli(); /* local CPU only */
1425 spin_lock_irqsave(&hwgroup->spinlock, flags);
1426 hwif = hwgroup->hwif;
1428 if (!ide_ack_intr(hwif)) {
1429 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1430 return;
1433 if ((handler = hwgroup->handler) == NULL || hwgroup->poll_timeout != 0) {
1435 * Not expecting an interrupt from this drive.
1436 * That means this could be:
1437 * (1) an interrupt from another PCI device
1438 * sharing the same PCI INT# as us.
1439 * or (2) a drive just entered sleep or standby mode,
1440 * and is interrupting to let us know.
1441 * or (3) a spurious interrupt of unknown origin.
1443 * For PCI, we cannot tell the difference,
1444 * so in that case we just ignore it and hope it goes away.
1446 #ifdef CONFIG_BLK_DEV_IDEPCI
1447 if (IDE_PCI_DEVID_EQ(hwif->pci_devid, IDE_PCI_DEVID_NULL))
1448 #endif /* CONFIG_BLK_DEV_IDEPCI */
1451 * Probably not a shared PCI interrupt,
1452 * so we can safely try to do something about it:
1454 unexpected_intr(irq, hwgroup);
1456 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1457 return;
1459 drive = hwgroup->drive;
1460 if (!drive || !drive_is_ready(drive)) {
1461 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1462 return;
1464 hwgroup->handler = NULL;
1465 del_timer(&(hwgroup->timer));
1466 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1467 if (drive->unmask)
1468 ide__sti(); /* local CPU only */
1469 handler(drive); /* service this interrupt, may set handler for next interrupt */
1471 * Note that handler() may have set things up for another
1472 * interrupt to occur soon, but it cannot happen until
1473 * we exit from this routine, because it will be the
1474 * same irq as is currently being serviced here,
1475 * and Linux won't allow another (on any CPU) until we return.
1477 start_next_request(hwgroup, hwif->irq);
1481 * get_info_ptr() returns the (ide_drive_t *) for a given device number.
1482 * It returns NULL if the given device number does not match any present drives.
1484 static ide_drive_t *get_info_ptr (kdev_t i_rdev)
1486 int major = MAJOR(i_rdev);
1487 unsigned int h;
1489 for (h = 0; h < MAX_HWIFS; ++h) {
1490 ide_hwif_t *hwif = &ide_hwifs[h];
1491 if (hwif->present && major == hwif->major) {
1492 unsigned unit = DEVICE_NR(i_rdev);
1493 if (unit < MAX_DRIVES) {
1494 ide_drive_t *drive = &hwif->drives[unit];
1495 if (drive->present)
1496 return drive;
1498 break;
1501 return NULL;
1505 * This function is intended to be used prior to invoking ide_do_drive_cmd().
1507 void ide_init_drive_cmd (struct request *rq)
1509 rq->buffer = NULL;
1510 rq->cmd = IDE_DRIVE_CMD;
1511 rq->sector = 0;
1512 rq->nr_sectors = 0;
1513 rq->current_nr_sectors = 0;
1514 rq->sem = NULL;
1515 rq->bh = NULL;
1516 rq->bhtail = NULL;
1517 rq->next = NULL;
1521 * This function issues a special IDE device request
1522 * onto the request queue.
1524 * If action is ide_wait, then the rq is queued at the end of the
1525 * request queue, and the function sleeps until it has been processed.
1526 * This is for use when invoked from an ioctl handler.
1528 * If action is ide_preempt, then the rq is queued at the head of
1529 * the request queue, displacing the currently-being-processed
1530 * request and this function returns immediately without waiting
1531 * for the new rq to be completed. This is VERY DANGEROUS, and is
1532 * intended for careful use by the ATAPI tape/cdrom driver code.
1534 * If action is ide_next, then the rq is queued immediately after
1535 * the currently-being-processed-request (if any), and the function
1536 * returns without waiting for the new rq to be completed. As above,
1537 * This is VERY DANGEROUS, and is intended for careful use by the
1538 * ATAPI tape/cdrom driver code.
1540 * If action is ide_end, then the rq is queued at the end of the
1541 * request queue, and the function returns immediately without waiting
1542 * for the new rq to be completed. This is again intended for careful
1543 * use by the ATAPI tape/cdrom driver code.
1545 int ide_do_drive_cmd (ide_drive_t *drive, struct request *rq, ide_action_t action)
1547 unsigned long flags;
1548 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1549 unsigned int major = HWIF(drive)->major;
1550 struct request *cur_rq;
1551 DECLARE_MUTEX_LOCKED(sem);
1553 #ifdef CONFIG_BLK_DEV_PDC4030
1554 if (HWIF(drive)->chipset == ide_pdc4030 && rq->buffer != NULL)
1555 return -ENOSYS; /* special drive cmds not supported */
1556 #endif
1557 rq->errors = 0;
1558 rq->rq_status = RQ_ACTIVE;
1559 rq->rq_dev = MKDEV(major,(drive->select.b.unit)<<PARTN_BITS);
1560 if (action == ide_wait)
1561 rq->sem = &sem;
1563 spin_lock_irqsave(&io_request_lock, flags);
1564 cur_rq = drive->queue;
1565 if (cur_rq == NULL || action == ide_preempt) {
1566 rq->next = cur_rq;
1567 drive->queue = rq;
1568 if (action == ide_preempt)
1569 hwgroup->rq = NULL;
1570 } else {
1571 if (action == ide_wait || action == ide_end) {
1572 while (cur_rq->next != NULL) /* find end of list */
1573 cur_rq = cur_rq->next;
1575 rq->next = cur_rq->next;
1576 cur_rq->next = rq;
1578 spin_unlock_irqrestore(&io_request_lock, flags);
1579 do_hwgroup_request(hwgroup);
1580 save_flags(flags); /* all CPUs; overkill? */
1581 cli(); /* all CPUs; overkill? */
1582 if (action == ide_wait && rq->rq_status != RQ_INACTIVE)
1583 down(&sem); /* wait for it to be serviced */
1584 restore_flags(flags); /* all CPUs; overkill? */
1585 return rq->errors ? -EIO : 0; /* return -EIO if errors */
1589 * This routine is called to flush all partitions and partition tables
1590 * for a changed disk, and then re-read the new partition table.
1591 * If we are revalidating a disk because of a media change, then we
1592 * enter with usage == 0. If we are using an ioctl, we automatically have
1593 * usage == 1 (we need an open channel to use an ioctl :-), so this
1594 * is our limit.
1596 int ide_revalidate_disk(kdev_t i_rdev)
1598 ide_drive_t *drive;
1599 ide_hwgroup_t *hwgroup;
1600 unsigned int p, major, minor;
1601 long flags;
1603 if ((drive = get_info_ptr(i_rdev)) == NULL)
1604 return -ENODEV;
1605 major = MAJOR(i_rdev);
1606 minor = drive->select.b.unit << PARTN_BITS;
1607 hwgroup = HWGROUP(drive);
1608 spin_lock_irqsave(&hwgroup->spinlock, flags);
1609 if (drive->busy || (drive->usage > 1)) {
1610 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1611 return -EBUSY;
1613 drive->busy = 1;
1614 MOD_INC_USE_COUNT;
1615 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1617 for (p = 0; p < (1<<PARTN_BITS); ++p) {
1618 if (drive->part[p].nr_sects > 0) {
1619 kdev_t devp = MKDEV(major, minor+p);
1620 struct super_block * sb = get_super(devp);
1621 fsync_dev (devp);
1622 if (sb)
1623 invalidate_inodes(sb);
1624 invalidate_buffers (devp);
1625 set_blocksize(devp, 1024);
1627 drive->part[p].start_sect = 0;
1628 drive->part[p].nr_sects = 0;
1631 drive->part[0].nr_sects = current_capacity(drive);
1632 if ((drive->media != ide_disk && drive->media != ide_floppy) ||
1633 drive->driver == NULL || !drive->part[0].nr_sects)
1634 drive->part[0].start_sect = -1;
1635 resetup_one_dev(HWIF(drive)->gd, drive->select.b.unit);
1637 drive->busy = 0;
1638 wake_up(&drive->wqueue);
1639 MOD_DEC_USE_COUNT;
1640 return 0;
1643 static void revalidate_drives (void)
1645 ide_hwif_t *hwif;
1646 ide_drive_t *drive;
1647 int index, unit;
1649 for (index = 0; index < MAX_HWIFS; ++index) {
1650 hwif = &ide_hwifs[index];
1651 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1652 drive = &ide_hwifs[index].drives[unit];
1653 if (drive->revalidate) {
1654 drive->revalidate = 0;
1655 if (!initializing)
1656 (void) ide_revalidate_disk(MKDEV(hwif->major, unit<<PARTN_BITS));
1662 static void ide_init_module (int type)
1664 int found = 0;
1665 ide_module_t *module = ide_modules;
1667 while (module) {
1668 if (module->type == type) {
1669 found = 1;
1670 (void) module->init();
1672 module = module->next;
1674 revalidate_drives();
1675 #ifdef CONFIG_KMOD
1676 if (!found && type == IDE_PROBE_MODULE)
1677 (void) request_module("ide-probe");
1678 #endif /* CONFIG_KMOD */
1681 static int ide_open(struct inode * inode, struct file * filp)
1683 ide_drive_t *drive;
1684 int rc;
1686 if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
1687 return -ENXIO;
1688 MOD_INC_USE_COUNT;
1689 if (drive->driver == NULL)
1690 ide_init_module(IDE_DRIVER_MODULE);
1691 #ifdef CONFIG_KMOD
1692 if (drive->driver == NULL) {
1693 if (drive->media == ide_disk)
1694 (void) request_module("ide-disk");
1695 if (drive->media == ide_cdrom)
1696 (void) request_module("ide-cd");
1697 if (drive->media == ide_tape)
1698 (void) request_module("ide-tape");
1699 if (drive->media == ide_floppy)
1700 (void) request_module("ide-floppy");
1702 #endif /* CONFIG_KMOD */
1703 while (drive->busy)
1704 sleep_on(&drive->wqueue);
1705 drive->usage++;
1706 if (drive->driver != NULL) {
1707 if ((rc = DRIVER(drive)->open(inode, filp, drive)))
1708 MOD_DEC_USE_COUNT;
1709 return rc;
1711 printk ("%s: driver not present\n", drive->name);
1712 drive->usage--;
1713 MOD_DEC_USE_COUNT;
1714 return -ENXIO;
1718 * Releasing a block device means we sync() it, so that it can safely
1719 * be forgotten about...
1721 static int ide_release(struct inode * inode, struct file * file)
1723 ide_drive_t *drive;
1725 if ((drive = get_info_ptr(inode->i_rdev)) != NULL) {
1726 fsync_dev(inode->i_rdev);
1727 drive->usage--;
1728 if (drive->driver != NULL)
1729 DRIVER(drive)->release(inode, file, drive);
1730 MOD_DEC_USE_COUNT;
1732 return 0;
1735 int ide_replace_subdriver(ide_drive_t *drive, const char *driver)
1737 if (!drive->present || drive->busy || drive->usage)
1738 goto abort;
1739 if (drive->driver != NULL && DRIVER(drive)->cleanup(drive))
1740 goto abort;
1741 strncpy(drive->driver_req, driver, 9);
1742 ide_init_module(IDE_DRIVER_MODULE);
1743 drive->driver_req[0] = 0;
1744 ide_init_module(IDE_DRIVER_MODULE);
1745 if (DRIVER(drive) && !strcmp(DRIVER(drive)->name, driver))
1746 return 0;
1747 abort:
1748 return 1;
1751 #ifdef CONFIG_PROC_FS
1752 ide_proc_entry_t generic_subdriver_entries[] = {
1753 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
1754 { NULL, 0, NULL, NULL }
1756 #endif
1758 void ide_unregister (unsigned int index)
1760 struct gendisk *gd, **gdp;
1761 ide_drive_t *drive, *d;
1762 ide_hwif_t *hwif, *g;
1763 ide_hwgroup_t *hwgroup;
1764 int irq_count = 0, unit, i;
1765 unsigned long flags;
1766 unsigned int p, minor;
1767 ide_hwif_t old_hwif;
1769 if (index >= MAX_HWIFS)
1770 return;
1771 save_flags(flags); /* all CPUs */
1772 cli(); /* all CPUs */
1773 hwif = &ide_hwifs[index];
1774 if (!hwif->present)
1775 goto abort;
1776 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1777 drive = &hwif->drives[unit];
1778 if (!drive->present)
1779 continue;
1780 if (drive->busy || drive->usage)
1781 goto abort;
1782 if (drive->driver != NULL && DRIVER(drive)->cleanup(drive))
1783 goto abort;
1785 hwif->present = 0;
1788 * All clear? Then blow away the buffer cache
1790 sti();
1791 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1792 drive = &hwif->drives[unit];
1793 if (!drive->present)
1794 continue;
1795 minor = drive->select.b.unit << PARTN_BITS;
1796 for (p = 0; p < (1<<PARTN_BITS); ++p) {
1797 if (drive->part[p].nr_sects > 0) {
1798 kdev_t devp = MKDEV(hwif->major, minor+p);
1799 struct super_block * sb = get_super(devp);
1800 if (sb) invalidate_inodes(sb);
1801 invalidate_buffers (devp);
1804 #ifdef CONFIG_PROC_FS
1805 destroy_proc_ide_drives(hwif);
1806 #endif
1808 cli();
1809 hwgroup = hwif->hwgroup;
1812 * free the irq if we were the only hwif using it
1814 g = hwgroup->hwif;
1815 do {
1816 if (g->irq == hwif->irq)
1817 ++irq_count;
1818 g = g->next;
1819 } while (g != hwgroup->hwif);
1820 if (irq_count == 1)
1821 free_irq(hwif->irq, hwgroup);
1824 * Note that we only release the standard ports,
1825 * and do not even try to handle any extra ports
1826 * allocated for weird IDE interface chipsets.
1828 ide_release_region(hwif->io_ports[IDE_DATA_OFFSET], 8);
1829 if (hwif->io_ports[IDE_CONTROL_OFFSET])
1830 ide_release_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
1833 * Remove us from the hwgroup, and free
1834 * the hwgroup if we were the only member
1836 d = hwgroup->drive;
1837 for (i = 0; i < MAX_DRIVES; ++i) {
1838 drive = &hwif->drives[i];
1839 if (!drive->present)
1840 continue;
1841 while (hwgroup->drive->next != drive)
1842 hwgroup->drive = hwgroup->drive->next;
1843 hwgroup->drive->next = drive->next;
1844 if (hwgroup->drive == drive)
1845 hwgroup->drive = NULL;
1846 if (drive->id != NULL) {
1847 kfree(drive->id);
1848 drive->id = NULL;
1850 drive->present = 0;
1852 if (d->present)
1853 hwgroup->drive = d;
1854 while (hwgroup->hwif->next != hwif)
1855 hwgroup->hwif = hwgroup->hwif->next;
1856 hwgroup->hwif->next = hwif->next;
1857 if (hwgroup->hwif == hwif)
1858 kfree(hwgroup);
1859 else
1860 hwgroup->hwif = HWIF(hwgroup->drive);
1862 #ifdef CONFIG_BLK_DEV_IDEDMA
1863 if (hwif->dma_base)
1864 (void) ide_release_dma(hwif);
1865 #endif /* CONFIG_BLK_DEV_IDEDMA */
1868 * Remove us from the kernel's knowledge
1870 unregister_blkdev(hwif->major, hwif->name);
1871 kfree(blksize_size[hwif->major]);
1872 kfree(max_sectors[hwif->major]);
1873 kfree(max_readahead[hwif->major]);
1874 blk_dev[hwif->major].request_fn = NULL;
1875 blk_dev[hwif->major].data = NULL;
1876 blk_dev[hwif->major].queue = NULL;
1877 blksize_size[hwif->major] = NULL;
1878 for (gdp = &gendisk_head; *gdp; gdp = &((*gdp)->next))
1879 if (*gdp == hwif->gd)
1880 break;
1881 if (*gdp == NULL)
1882 printk("gd not in disk chain!\n");
1883 else {
1884 gd = *gdp; *gdp = gd->next;
1885 kfree(gd->sizes);
1886 kfree(gd->part);
1887 kfree(gd);
1889 old_hwif = *hwif;
1890 init_hwif_data (index); /* restore hwif data to pristine status */
1891 hwif->hwgroup = old_hwif.hwgroup;
1892 hwif->tuneproc = old_hwif.tuneproc;
1893 hwif->dmaproc = old_hwif.dmaproc;
1894 hwif->dma_base = old_hwif.dma_base;
1895 hwif->dma_extra = old_hwif.dma_extra;
1896 hwif->config_data = old_hwif.config_data;
1897 hwif->select_data = old_hwif.select_data;
1898 hwif->irq = old_hwif.irq;
1899 hwif->major = old_hwif.major;
1900 hwif->proc = old_hwif.proc;
1901 hwif->chipset = old_hwif.chipset;
1902 hwif->pci_dev = old_hwif.pci_dev;
1903 hwif->pci_devid = old_hwif.pci_devid;
1904 abort:
1905 restore_flags(flags); /* all CPUs */
1909 * Setup hw_regs_t structure described by parameters. You
1910 * may set up the hw structure yourself OR use this routine to
1911 * do it for you.
1913 void ide_setup_ports ( hw_regs_t *hw,
1914 ide_ioreg_t base, int *offsets,
1915 ide_ioreg_t ctrl, ide_ioreg_t intr,
1916 ide_ack_intr_t *ack_intr, int irq)
1918 int i;
1920 for (i = 0; i < IDE_NR_PORTS; i++) {
1921 if (offsets[i] == -1) {
1922 switch(i) {
1923 case IDE_CONTROL_OFFSET:
1924 hw->io_ports[i] = ctrl;
1925 break;
1926 case IDE_IRQ_OFFSET:
1927 hw->io_ports[i] = intr;
1928 break;
1929 default:
1930 hw->io_ports[i] = 0;
1931 break;
1933 } else {
1934 hw->io_ports[i] = base + offsets[i];
1937 hw->irq = irq;
1938 hw->ack_intr = ack_intr;
1942 * Register an IDE interface, specifing exactly the registers etc
1943 * Set init=1 iff calling before probes have taken place.
1945 int ide_register_hw (hw_regs_t *hw, ide_hwif_t **hwifp)
1947 int index, retry = 1;
1948 ide_hwif_t *hwif;
1950 do {
1951 for (index = 0; index < MAX_HWIFS; ++index) {
1952 hwif = &ide_hwifs[index];
1953 if (hwif->hw.io_ports[IDE_DATA_OFFSET] == hw->io_ports[IDE_DATA_OFFSET])
1954 goto found;
1956 for (index = 0; index < MAX_HWIFS; ++index) {
1957 hwif = &ide_hwifs[index];
1958 if ((!hwif->present && !initializing) ||
1959 (!hwif->hw.io_ports[IDE_DATA_OFFSET] && initializing))
1960 goto found;
1962 for (index = 0; index < MAX_HWIFS; index++)
1963 ide_unregister(index);
1964 } while (retry--);
1965 return -1;
1966 found:
1967 if (hwif->present)
1968 ide_unregister(index);
1969 if (hwif->present)
1970 return -1;
1971 memcpy(&hwif->hw, hw, sizeof(*hw));
1972 memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->hw.io_ports));
1973 hwif->irq = hw->irq;
1974 hwif->noprobe = 0;
1976 if (!initializing) {
1977 ide_init_module(IDE_PROBE_MODULE);
1978 #ifdef CONFIG_PROC_FS
1979 create_proc_ide_interfaces();
1980 #endif
1981 ide_init_module(IDE_DRIVER_MODULE);
1984 if (hwifp)
1985 *hwifp = hwif;
1987 return (initializing || hwif->present) ? index : -1;
1991 * Compatability function with existing drivers. If you want
1992 * something different, use the function above.
1994 int ide_register (int arg1, int arg2, int irq)
1996 hw_regs_t hw;
1997 ide_init_hwif_ports(&hw, (ide_ioreg_t) arg1, (ide_ioreg_t) arg2, NULL);
1998 hw.irq = irq;
1999 return ide_register_hw(&hw, NULL);
2002 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)
2004 ide_settings_t **p = (ide_settings_t **) &drive->settings, *setting = NULL;
2006 while ((*p) && strcmp((*p)->name, name) < 0)
2007 p = &((*p)->next);
2008 if ((setting = kmalloc(sizeof(*setting), GFP_KERNEL)) == NULL)
2009 goto abort;
2010 memset(setting, 0, sizeof(*setting));
2011 if ((setting->name = kmalloc(strlen(name) + 1, GFP_KERNEL)) == NULL)
2012 goto abort;
2013 strcpy(setting->name, name); setting->rw = rw;
2014 setting->read_ioctl = read_ioctl; setting->write_ioctl = write_ioctl;
2015 setting->data_type = data_type; setting->min = min;
2016 setting->max = max; setting->mul_factor = mul_factor;
2017 setting->div_factor = div_factor; setting->data = data;
2018 setting->set = set; setting->next = *p;
2019 if (drive->driver)
2020 setting->auto_remove = 1;
2021 *p = setting;
2022 return;
2023 abort:
2024 if (setting)
2025 kfree(setting);
2028 void ide_remove_setting(ide_drive_t *drive, char *name)
2030 ide_settings_t **p = (ide_settings_t **) &drive->settings, *setting;
2032 while ((*p) && strcmp((*p)->name, name))
2033 p = &((*p)->next);
2034 if ((setting = (*p)) == NULL)
2035 return;
2036 (*p) = setting->next;
2037 kfree(setting->name);
2038 kfree(setting);
2041 static ide_settings_t *ide_find_setting_by_ioctl(ide_drive_t *drive, int cmd)
2043 ide_settings_t *setting = drive->settings;
2045 while (setting) {
2046 if (setting->read_ioctl == cmd || setting->write_ioctl == cmd)
2047 break;
2048 setting = setting->next;
2050 return setting;
2053 ide_settings_t *ide_find_setting_by_name(ide_drive_t *drive, char *name)
2055 ide_settings_t *setting = drive->settings;
2057 while (setting) {
2058 if (strcmp(setting->name, name) == 0)
2059 break;
2060 setting = setting->next;
2062 return setting;
2065 static void auto_remove_settings(ide_drive_t *drive)
2067 ide_settings_t *setting;
2068 repeat:
2069 setting = drive->settings;
2070 while (setting) {
2071 if (setting->auto_remove) {
2072 ide_remove_setting(drive, setting->name);
2073 goto repeat;
2075 setting = setting->next;
2079 int ide_read_setting(ide_drive_t *drive, ide_settings_t *setting)
2081 int val = -EINVAL;
2082 unsigned long flags;
2084 if ((setting->rw & SETTING_READ)) {
2085 spin_lock_irqsave(&HWGROUP(drive)->spinlock, flags);
2086 switch(setting->data_type) {
2087 case TYPE_BYTE:
2088 val = *((u8 *) setting->data);
2089 break;
2090 case TYPE_SHORT:
2091 val = *((u16 *) setting->data);
2092 break;
2093 case TYPE_INT:
2094 case TYPE_INTA:
2095 val = *((u32 *) setting->data);
2096 break;
2098 spin_unlock_irqrestore(&HWGROUP(drive)->spinlock, flags);
2100 return val;
2103 int ide_spin_wait_hwgroup (ide_drive_t *drive, unsigned long *flags)
2105 ide_hwgroup_t *hwgroup = HWGROUP(drive);
2106 unsigned long timeout = jiffies + (3 * HZ);
2108 spin_lock_irqsave(&hwgroup->spinlock, *flags);
2109 while (hwgroup->busy) {
2110 spin_unlock_irqrestore(&hwgroup->spinlock, *flags);
2111 __sti(); /* local CPU only; needed for jiffies */
2112 if (0 < (signed long)(jiffies - timeout)) {
2113 printk("%s: channel busy\n", drive->name);
2114 return -EBUSY;
2116 spin_lock_irqsave(&hwgroup->spinlock, *flags);
2118 return 0;
2121 int ide_write_setting(ide_drive_t *drive, ide_settings_t *setting, int val)
2123 unsigned long flags;
2124 int i;
2125 u32 *p;
2127 if (!capable(CAP_SYS_ADMIN))
2128 return -EACCES;
2129 if (!(setting->rw & SETTING_WRITE))
2130 return -EPERM;
2131 if (val < setting->min || val > setting->max)
2132 return -EINVAL;
2133 if (setting->set)
2134 return setting->set(drive, val);
2135 if (ide_spin_wait_hwgroup(drive, &flags))
2136 return -EBUSY;
2137 switch (setting->data_type) {
2138 case TYPE_BYTE:
2139 *((u8 *) setting->data) = val;
2140 break;
2141 case TYPE_SHORT:
2142 *((u16 *) setting->data) = val;
2143 break;
2144 case TYPE_INT:
2145 *((u32 *) setting->data) = val;
2146 break;
2147 case TYPE_INTA:
2148 p = (u32 *) setting->data;
2149 for (i = 0; i < 1 << PARTN_BITS; i++, p++)
2150 *p = val;
2151 break;
2153 spin_unlock_irqrestore(&HWGROUP(drive)->spinlock, flags);
2154 return 0;
2157 static int set_io_32bit(ide_drive_t *drive, int arg)
2159 drive->io_32bit = arg;
2160 #ifdef CONFIG_BLK_DEV_DTC2278
2161 if (HWIF(drive)->chipset == ide_dtc2278)
2162 HWIF(drive)->drives[!drive->select.b.unit].io_32bit = arg;
2163 #endif /* CONFIG_BLK_DEV_DTC2278 */
2164 return 0;
2167 static int set_using_dma(ide_drive_t *drive, int arg)
2169 if (!drive->driver || !DRIVER(drive)->supports_dma)
2170 return -EPERM;
2171 if (!drive->id || !(drive->id->capability & 1) || !HWIF(drive)->dmaproc)
2172 return -EPERM;
2173 if (HWIF(drive)->dmaproc(arg ? ide_dma_on : ide_dma_off, drive))
2174 return -EIO;
2175 return 0;
2178 static int set_pio_mode(ide_drive_t *drive, int arg)
2180 struct request rq;
2182 if (!HWIF(drive)->tuneproc)
2183 return -ENOSYS;
2184 if (drive->special.b.set_tune)
2185 return -EBUSY;
2186 ide_init_drive_cmd(&rq);
2187 drive->tune_req = (byte) arg;
2188 drive->special.b.set_tune = 1;
2189 (void) ide_do_drive_cmd (drive, &rq, ide_wait);
2190 return 0;
2193 void ide_add_generic_settings(ide_drive_t *drive)
2196 * drive setting name read/write access read ioctl write ioctl data type min max mul_factor div_factor data pointer set function
2198 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);
2199 ide_add_setting(drive, "keepsettings", SETTING_RW, HDIO_GET_KEEPSETTINGS, HDIO_SET_KEEPSETTINGS, TYPE_BYTE, 0, 1, 1, 1, &drive->keep_settings, NULL);
2200 ide_add_setting(drive, "nice1", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->nice1, NULL);
2201 ide_add_setting(drive, "pio_mode", SETTING_WRITE, -1, HDIO_SET_PIO_MODE, TYPE_BYTE, 0, 255, 1, 1, NULL, set_pio_mode);
2202 ide_add_setting(drive, "slow", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->slow, NULL);
2203 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);
2204 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);
2207 int ide_wait_cmd (ide_drive_t *drive, int cmd, int nsect, int feature, int sectors, byte *buf)
2209 struct request rq;
2210 byte buffer[4];
2212 if (!buf)
2213 buf = buffer;
2214 memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
2215 ide_init_drive_cmd(&rq);
2216 rq.buffer = buf;
2217 *buf++ = cmd;
2218 *buf++ = nsect;
2219 *buf++ = feature;
2220 *buf++ = sectors;
2221 return ide_do_drive_cmd(drive, &rq, ide_wait);
2225 * Delay for *at least* 50ms. As we don't know how much time is left
2226 * until the next tick occurs, we wait an extra tick to be safe.
2227 * This is used only during the probing/polling for drives at boot time.
2229 * However, its usefullness may be needed in other places, thus we export it now.
2230 * The future may change this to a millisecond setable delay.
2232 void ide_delay_50ms (void)
2234 unsigned long timeout = jiffies + ((HZ + 19)/20) + 1;
2235 while (0 < (signed long)(timeout - jiffies));
2238 int ide_config_drive_speed (ide_drive_t *drive, byte speed)
2240 int err;
2243 * Don't use ide_wait_cmd here - it will
2244 * attempt to set_geometry and recalibrate,
2245 * but for some reason these don't work at
2246 * this point (lost interrupt).
2248 SELECT_DRIVE(HWIF(drive), drive);
2249 OUT_BYTE(drive->ctl | 2, IDE_CONTROL_REG);
2250 OUT_BYTE(speed, IDE_NSECTOR_REG);
2251 OUT_BYTE(SETFEATURES_XFER, IDE_FEATURE_REG);
2252 OUT_BYTE(WIN_SETFEATURES, IDE_COMMAND_REG);
2254 err = ide_wait_stat(drive, DRIVE_READY, BUSY_STAT|DRQ_STAT|ERR_STAT, WAIT_CMD);
2256 return(err);
2259 static int ide_ioctl (struct inode *inode, struct file *file,
2260 unsigned int cmd, unsigned long arg)
2262 int err = 0, major, minor;
2263 ide_drive_t *drive;
2264 struct request rq;
2265 kdev_t dev;
2266 ide_settings_t *setting;
2268 if (!inode || !(dev = inode->i_rdev))
2269 return -EINVAL;
2270 major = MAJOR(dev); minor = MINOR(dev);
2271 if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
2272 return -ENODEV;
2274 if ((setting = ide_find_setting_by_ioctl(drive, cmd)) != NULL) {
2275 if (cmd == setting->read_ioctl) {
2276 err = ide_read_setting(drive, setting);
2277 return err >= 0 ? put_user(err, (long *) arg) : err;
2278 } else {
2279 if ((MINOR(inode->i_rdev) & PARTN_MASK))
2280 return -EINVAL;
2281 return ide_write_setting(drive, setting, arg);
2285 ide_init_drive_cmd (&rq);
2286 switch (cmd) {
2287 case HDIO_GETGEO:
2289 struct hd_geometry *loc = (struct hd_geometry *) arg;
2290 if (!loc || (drive->media != ide_disk && drive->media != ide_floppy)) return -EINVAL;
2291 if (put_user(drive->bios_head, (byte *) &loc->heads)) return -EFAULT;
2292 if (put_user(drive->bios_sect, (byte *) &loc->sectors)) return -EFAULT;
2293 if (put_user(drive->bios_cyl, (unsigned short *) &loc->cylinders)) return -EFAULT;
2294 if (put_user((unsigned)drive->part[MINOR(inode->i_rdev)&PARTN_MASK].start_sect,
2295 (unsigned long *) &loc->start)) return -EFAULT;
2296 return 0;
2299 case BLKGETSIZE: /* Return device size */
2300 return put_user(drive->part[MINOR(inode->i_rdev)&PARTN_MASK].nr_sects, (long *) arg);
2302 case BLKRRPART: /* Re-read partition tables */
2303 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2304 return ide_revalidate_disk(inode->i_rdev);
2306 case HDIO_OBSOLETE_IDENTITY:
2307 case HDIO_GET_IDENTITY:
2308 if (MINOR(inode->i_rdev) & PARTN_MASK)
2309 return -EINVAL;
2310 if (drive->id == NULL)
2311 return -ENOMSG;
2312 if (copy_to_user((char *)arg, (char *)drive->id, (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
2313 return -EFAULT;
2314 return 0;
2316 case HDIO_GET_NICE:
2317 return put_user(drive->dsc_overlap << IDE_NICE_DSC_OVERLAP |
2318 drive->atapi_overlap << IDE_NICE_ATAPI_OVERLAP |
2319 drive->nice0 << IDE_NICE_0 |
2320 drive->nice1 << IDE_NICE_1 |
2321 drive->nice2 << IDE_NICE_2,
2322 (long *) arg);
2323 case HDIO_DRIVE_CMD:
2325 byte args[4], *argbuf = args;
2326 int argsize = 4;
2327 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2328 if (NULL == (void *) arg)
2329 return ide_do_drive_cmd(drive, &rq, ide_wait);
2330 if (copy_from_user(args, (void *)arg, 4))
2331 return -EFAULT;
2332 if (args[3]) {
2333 argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
2334 argbuf = kmalloc(argsize, GFP_KERNEL);
2335 if (argbuf == NULL)
2336 return -ENOMEM;
2337 memcpy(argbuf, args, 4);
2339 if ((((byte *)arg)[0] == WIN_SETFEATURES) &&
2340 (((byte *)arg)[1] > 66) &&
2341 (((byte *)arg)[2] == 3) &&
2342 ((drive->id->word93 & 0x2000) == 0)) {
2343 printk("%s: Speed warnings UDMA 3/4 is not functional.\n", drive->name);
2344 goto abort;
2346 err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
2347 if (!err &&
2348 (((byte *)arg)[0] == WIN_SETFEATURES) &&
2349 (((byte *)arg)[1] >= 16) &&
2350 (((byte *)arg)[2] == 3) &&
2351 (drive->id->dma_ultra ||
2352 drive->id->dma_mword ||
2353 drive->id->dma_1word)) {
2356 * Re-read drive->id for possible DMA mode
2357 * change (copied from ide-probe.c)
2359 struct hd_driveid *id;
2360 unsigned long timeout, irqs, flags;
2362 probe_irq_off(probe_irq_on());
2363 irqs = probe_irq_on();
2364 if (IDE_CONTROL_REG)
2365 OUT_BYTE(drive->ctl,IDE_CONTROL_REG);
2366 ide_delay_50ms();
2367 OUT_BYTE(WIN_IDENTIFY, IDE_COMMAND_REG);
2368 timeout = jiffies + WAIT_WORSTCASE;
2369 do {
2370 if (0 < (signed long)(jiffies - timeout)) {
2371 if (irqs)
2372 (void) probe_irq_off(irqs);
2373 goto abort; /* drive timed-out */
2375 ide_delay_50ms(); /* give drive a breather */
2376 } while (IN_BYTE(IDE_ALTSTATUS_REG) & BUSY_STAT);
2377 ide_delay_50ms(); /* wait for IRQ and DRQ_STAT */
2378 if (!OK_STAT(GET_STAT(),DRQ_STAT,BAD_R_STAT))
2379 goto abort;
2380 __save_flags(flags); /* local CPU only */
2381 __cli(); /* local CPU only; some systems need this */
2382 id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC);
2383 ide_input_data(drive, id, SECTOR_WORDS);
2384 (void) GET_STAT(); /* clear drive IRQ */
2385 ide__sti(); /* local CPU only */
2386 __restore_flags(flags); /* local CPU only */
2387 ide_fix_driveid(id);
2388 if (id && id->cyls) {
2389 drive->id->dma_ultra = id->dma_ultra;
2390 drive->id->dma_mword = id->dma_mword;
2391 drive->id->dma_1word = id->dma_1word;
2392 /* anything more ? */
2393 #ifdef DEBUG
2394 printk("%s: dma_ultra=%04X, dma_mword=%04X, dma_1word=%04X\n",
2395 drive->name, id->dma_ultra, id->dma_mword, id->dma_1word);
2396 #endif
2397 kfree(id);
2400 abort:
2401 if (copy_to_user((void *)arg, argbuf, argsize))
2402 err = -EFAULT;
2403 if (argsize > 4)
2404 kfree(argbuf);
2405 return err;
2408 case HDIO_SCAN_HWIF:
2410 int args[3];
2411 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2412 if (copy_from_user(args, (void *)arg, 3 * sizeof(int)))
2413 return -EFAULT;
2414 if (ide_register(args[0], args[1], args[2]) == -1)
2415 return -EIO;
2416 return 0;
2418 case HDIO_UNREGISTER_HWIF:
2419 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2420 /* should I check here for arg > MAX_HWIFS, or
2421 just let ide_unregister fail silently? -- shaver */
2422 ide_unregister(arg);
2423 return 0;
2424 case HDIO_SET_NICE:
2425 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2426 if (drive->driver == NULL)
2427 return -EPERM;
2428 if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
2429 return -EPERM;
2430 drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
2431 if (drive->dsc_overlap && !DRIVER(drive)->supports_dsc_overlap) {
2432 drive->dsc_overlap = 0;
2433 return -EPERM;
2435 drive->nice1 = (arg >> IDE_NICE_1) & 1;
2436 return 0;
2438 case BLKROSET:
2439 case BLKROGET:
2440 case BLKFLSBUF:
2441 case BLKSSZGET:
2442 case BLKPG:
2443 return blk_ioctl(inode->i_rdev, cmd, arg);
2445 default:
2446 if (drive->driver != NULL)
2447 return DRIVER(drive)->ioctl(drive, inode, file, cmd, arg);
2448 return -EPERM;
2452 static int ide_check_media_change (kdev_t i_rdev)
2454 ide_drive_t *drive;
2456 if ((drive = get_info_ptr(i_rdev)) == NULL)
2457 return -ENODEV;
2458 if (drive->driver != NULL)
2459 return DRIVER(drive)->media_change(drive);
2460 return 0;
2463 void ide_fixstring (byte *s, const int bytecount, const int byteswap)
2465 byte *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */
2467 if (byteswap) {
2468 /* convert from big-endian to host byte order */
2469 for (p = end ; p != s;) {
2470 unsigned short *pp = (unsigned short *) (p -= 2);
2471 *pp = ntohs(*pp);
2475 /* strip leading blanks */
2476 while (s != end && *s == ' ')
2477 ++s;
2479 /* compress internal blanks and strip trailing blanks */
2480 while (s != end && *s) {
2481 if (*s++ != ' ' || (s != end && *s && *s != ' '))
2482 *p++ = *(s-1);
2485 /* wipe out trailing garbage */
2486 while (p != end)
2487 *p++ = '\0';
2493 char *ide_xfer_verbose (byte xfer_rate) {
2494 switch(xfer_rate) {
2495 case XFER_UDMA_4: return("UDMA 4");
2496 case XFER_UDMA_3: return("UDMA 3");
2497 case XFER_UDMA_2: return("UDMA 2");
2498 case XFER_UDMA_1: return("UDMA 1");
2499 case XFER_UDMA_0: return("UDMA 0");
2500 case XFER_MW_DMA_2: return("MW DMA 2");
2501 case XFER_MW_DMA_1: return("MW DMA 1");
2502 case XFER_MW_DMA_0: return("MW DMA 0");
2503 case XFER_SW_DMA_2: return("SW DMA 2");
2504 case XFER_SW_DMA_1: return("SW DMA 1");
2505 case XFER_SW_DMA_0: return("SW DMA 0");
2506 case XFER_PIO_4: return("PIO 4");
2507 case XFER_PIO_3: return("PIO 3");
2508 case XFER_PIO_2: return("PIO 2");
2509 case XFER_PIO_1: return("PIO 1");
2510 case XFER_PIO_0: return("PIO 0");
2511 case XFER_PIO_SLOW: return("PIO SLOW");
2512 default: return("XFER ERROR");
2517 * stridx() returns the offset of c within s,
2518 * or -1 if c is '\0' or not found within s.
2520 __initfunc(static int stridx (const char *s, char c))
2522 char *i = strchr(s, c);
2523 return (i && c) ? i - s : -1;
2527 * match_parm() does parsing for ide_setup():
2529 * 1. the first char of s must be '='.
2530 * 2. if the remainder matches one of the supplied keywords,
2531 * the index (1 based) of the keyword is negated and returned.
2532 * 3. if the remainder is a series of no more than max_vals numbers
2533 * separated by commas, the numbers are saved in vals[] and a
2534 * count of how many were saved is returned. Base10 is assumed,
2535 * and base16 is allowed when prefixed with "0x".
2536 * 4. otherwise, zero is returned.
2538 __initfunc(static int match_parm (char *s, const char *keywords[], int vals[], int max_vals))
2540 static const char *decimal = "0123456789";
2541 static const char *hex = "0123456789abcdef";
2542 int i, n;
2544 if (*s++ == '=') {
2546 * Try matching against the supplied keywords,
2547 * and return -(index+1) if we match one
2549 if (keywords != NULL) {
2550 for (i = 0; *keywords != NULL; ++i) {
2551 if (!strcmp(s, *keywords++))
2552 return -(i+1);
2556 * Look for a series of no more than "max_vals"
2557 * numeric values separated by commas, in base10,
2558 * or base16 when prefixed with "0x".
2559 * Return a count of how many were found.
2561 for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
2562 vals[n] = i;
2563 while ((i = stridx(decimal, *++s)) >= 0)
2564 vals[n] = (vals[n] * 10) + i;
2565 if (*s == 'x' && !vals[n]) {
2566 while ((i = stridx(hex, *++s)) >= 0)
2567 vals[n] = (vals[n] * 0x10) + i;
2569 if (++n == max_vals)
2570 break;
2571 if (*s == ',' || *s == ';')
2572 ++s;
2574 if (!*s)
2575 return n;
2577 return 0; /* zero = nothing matched */
2581 * ide_setup() gets called VERY EARLY during initialization,
2582 * to handle kernel "command line" strings beginning with "hdx="
2583 * or "ide". Here is the complete set currently supported:
2585 * "hdx=" is recognized for all "x" from "a" to "h", such as "hdc".
2586 * "idex=" is recognized for all "x" from "0" to "3", such as "ide1".
2588 * "hdx=noprobe" : drive may be present, but do not probe for it
2589 * "hdx=none" : drive is NOT present, ignore cmos and do not probe
2590 * "hdx=nowerr" : ignore the WRERR_STAT bit on this drive
2591 * "hdx=cdrom" : drive is present, and is a cdrom drive
2592 * "hdx=cyl,head,sect" : disk drive is present, with specified geometry
2593 * "hdx=autotune" : driver will attempt to tune interface speed
2594 * to the fastest PIO mode supported,
2595 * if possible for this drive only.
2596 * Not fully supported by all chipset types,
2597 * and quite likely to cause trouble with
2598 * older/odd IDE drives.
2600 * "hdx=slow" : insert a huge pause after each access to the data
2601 * port. Should be used only as a last resort.
2603 * "hdx=swapdata" : when the drive is a disk, byte swap all data
2604 * "hdx=bswap" : same as above..........
2606 * "idebus=xx" : inform IDE driver of VESA/PCI bus speed in MHz,
2607 * where "xx" is between 20 and 66 inclusive,
2608 * used when tuning chipset PIO modes.
2609 * For PCI bus, 25 is correct for a P75 system,
2610 * 30 is correct for P90,P120,P180 systems,
2611 * and 33 is used for P100,P133,P166 systems.
2612 * If in doubt, use idebus=33 for PCI.
2613 * As for VLB, it is safest to not specify it.
2615 * "idex=noprobe" : do not attempt to access/use this interface
2616 * "idex=base" : probe for an interface at the addr specified,
2617 * where "base" is usually 0x1f0 or 0x170
2618 * and "ctl" is assumed to be "base"+0x206
2619 * "idex=base,ctl" : specify both base and ctl
2620 * "idex=base,ctl,irq" : specify base, ctl, and irq number
2621 * "idex=autotune" : driver will attempt to tune interface speed
2622 * to the fastest PIO mode supported,
2623 * for all drives on this interface.
2624 * Not fully supported by all chipset types,
2625 * and quite likely to cause trouble with
2626 * older/odd IDE drives.
2627 * "idex=noautotune" : driver will NOT attempt to tune interface speed
2628 * This is the default for most chipsets,
2629 * except the cmd640.
2630 * "idex=serialize" : do not overlap operations on idex and ide(x^1)
2631 * "idex=four" : four drives on idex and ide(x^1) share same ports
2632 * "idex=reset" : reset interface before first use
2633 * "idex=dma" : enable DMA by default on both drives if possible
2635 * "splitfifo=betweenChan"
2636 * : FIFO Configuration of VIA 82c586(<nothing>,"A"or"B").
2637 * --see what follows...
2638 * "splitfifo=betweenChan,thresholdprim,thresholdsec"
2639 * : FIFO Configuration of VIA 82c586(<nothing>,"A" or "B").
2640 * betweenChan = 1(all FIFO's to primary channel)
2641 * , 2(all FIFO's to secondary channel)
2642 * , 3 or 4(evenly shared between them).
2643 * note: without FIFO, a channel is (u)dma disabled!
2644 * thresholdprim = 4, 3, 2 or 1
2645 * (standing for 1, 3/4, 1/2, 1/4).
2646 * Sets the threshold of FIFO to begin dma
2647 * transfer on the primary channel.
2648 * thresholdsec = cf upper, but for secondary channel.
2650 * The following are valid ONLY on ide0, (except dc4030)
2651 * and the defaults for the base,ctl ports must not be altered.
2653 * "ide0=dtc2278" : probe/support DTC2278 interface
2654 * "ide0=ht6560b" : probe/support HT6560B interface
2655 * "ide0=cmd640_vlb" : *REQUIRED* for VLB cards with the CMD640 chip
2656 * (not for PCI -- automatically detected)
2657 * "ide0=qd6580" : probe/support qd6580 interface
2658 * "ide0=ali14xx" : probe/support ali14xx chipsets (ALI M1439, M1443, M1445)
2659 * "ide0=umc8672" : probe/support umc8672 chipsets
2660 * "idex=dc4030" : probe/support Promise DC4030VL interface
2662 __initfunc(void ide_setup (char *s))
2664 int i, vals[3];
2665 ide_hwif_t *hwif;
2666 ide_drive_t *drive;
2667 unsigned int hw, unit;
2668 const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
2669 const char max_hwif = '0' + (MAX_HWIFS - 1);
2671 printk("ide_setup: %s", s);
2673 #ifdef CONFIG_BLK_DEV_IDEDOUBLER
2674 if (!strcmp(s, "ide=doubler")) {
2675 extern int ide_doubler;
2677 printk("ide: Enabled support for IDE doublers\n");
2678 ide_doubler = 1;
2679 return;
2681 #endif /* CONFIG_BLK_DEV_IDEDOUBLER */
2683 init_ide_data ();
2686 * Look for drive options: "hdx="
2688 if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
2689 const char *hd_words[] = {"none", "noprobe", "nowerr", "cdrom",
2690 "serialize", "autotune", "noautotune",
2691 "slow", "swapdata", "bswap", NULL};
2692 unit = s[2] - 'a';
2693 hw = unit / MAX_DRIVES;
2694 unit = unit % MAX_DRIVES;
2695 hwif = &ide_hwifs[hw];
2696 drive = &hwif->drives[unit];
2697 if (strncmp(s + 4, "ide-", 4) == 0) {
2698 strncpy(drive->driver_req, s + 4, 9);
2699 goto done;
2701 switch (match_parm(&s[3], hd_words, vals, 3)) {
2702 case -1: /* "none" */
2703 drive->nobios = 1; /* drop into "noprobe" */
2704 case -2: /* "noprobe" */
2705 drive->noprobe = 1;
2706 goto done;
2707 case -3: /* "nowerr" */
2708 drive->bad_wstat = BAD_R_STAT;
2709 hwif->noprobe = 0;
2710 goto done;
2711 case -4: /* "cdrom" */
2712 drive->present = 1;
2713 drive->media = ide_cdrom;
2714 hwif->noprobe = 0;
2715 goto done;
2716 case -5: /* "serialize" */
2717 printk(" -- USE \"ide%d=serialize\" INSTEAD", hw);
2718 goto do_serialize;
2719 case -6: /* "autotune" */
2720 drive->autotune = 1;
2721 goto done;
2722 case -7: /* "noautotune" */
2723 drive->autotune = 2;
2724 goto done;
2725 case -8: /* "slow" */
2726 drive->slow = 1;
2727 goto done;
2728 case -9: /* swapdata or bswap */
2729 case -10:
2730 drive->bswap = 1;
2731 goto done;
2732 case 3: /* cyl,head,sect */
2733 drive->media = ide_disk;
2734 drive->cyl = drive->bios_cyl = vals[0];
2735 drive->head = drive->bios_head = vals[1];
2736 drive->sect = drive->bios_sect = vals[2];
2737 drive->present = 1;
2738 drive->forced_geom = 1;
2739 hwif->noprobe = 0;
2740 goto done;
2741 default:
2742 goto bad_option;
2746 #if defined(CONFIG_BLK_DEV_VIA82C586)
2748 * Look for drive option "splitfifo=..."
2751 if (s[0] == 's' && s[1] == 'p' && s[2] == 'l' &&
2752 s[3] == 'i' && s[4] == 't' && s[5] == 'f' &&
2753 s[6] == 'i' && s[7] == 'f' && s[8] == 'o') {
2754 byte tmp = 0x3a; /* default config byte */
2756 i = match_parm(&s[9], NULL, vals, 3);
2757 switch(i) {
2758 case 3:
2759 tmp &= 0xf0;
2760 if ((vals[1] > 0) && (vals[1] < 5)) {
2761 /* sets threshold for primary Channel: */
2762 byte x = 4 - vals[1];
2763 tmp |= (x << 2);
2765 else
2766 goto bad_option;
2767 if ((vals[2] > 0) && (vals[2] < 5)) {
2768 /* sets threshold for secondary Channel: */
2769 byte x = 4 - vals[2];
2770 tmp |= x;
2772 else
2773 goto bad_option;
2774 case 1:
2775 /* set the FIFO config between channels to 0: */
2776 tmp &= 0x9f;
2777 /* set the needed FIFO config between channels: */
2778 if (vals[0] == 1) /* primary fifo only */
2779 tmp |= 0x10;
2780 else if (vals[0] == 2) /* secondary fifo only */
2781 tmp |= 0x70;
2782 else if (vals[0] == 4) /* other shared fifo config */
2783 tmp |= 0x50;
2784 else if (vals[0] == 3) /* default config */
2785 tmp |= 0x30;
2786 else
2787 goto bad_option;
2788 break;
2789 default:
2790 goto bad_option;
2792 /* set the found option in fifoconfig */
2793 fifoconfig = tmp;
2794 goto done;
2796 #endif /* defined(CONFIG_BLK_DEV_VIA82C586) */
2798 if (s[0] != 'i' || s[1] != 'd' || s[2] != 'e')
2799 goto bad_option;
2801 * Look for bus speed option: "idebus="
2803 if (s[3] == 'b' && s[4] == 'u' && s[5] == 's') {
2804 if (match_parm(&s[6], NULL, vals, 1) != 1)
2805 goto bad_option;
2806 if (vals[0] >= 20 && vals[0] <= 66)
2807 idebus_parameter = vals[0];
2808 else
2809 printk(" -- BAD BUS SPEED! Expected value from 20 to 66");
2810 goto done;
2813 * Look for interface options: "idex="
2815 if (s[3] >= '0' && s[3] <= max_hwif) {
2817 * Be VERY CAREFUL changing this: note hardcoded indexes below
2819 const char *ide_words[] = {"noprobe", "serialize", "autotune", "noautotune", "reset", "dma", "four",
2820 "qd6580", "ht6560b", "cmd640_vlb", "dtc2278", "umc8672", "ali14xx", "dc4030", NULL};
2821 hw = s[3] - '0';
2822 hwif = &ide_hwifs[hw];
2823 i = match_parm(&s[4], ide_words, vals, 3);
2826 * Cryptic check to ensure chipset not already set for hwif:
2828 if (i > 0 || i <= -7) { /* is parameter a chipset name? */
2829 if (hwif->chipset != ide_unknown)
2830 goto bad_option; /* chipset already specified */
2831 if (i <= -7 && i != -14 && hw != 0)
2832 goto bad_hwif; /* chipset drivers are for "ide0=" only */
2833 if (i <= -7 && i != -14 && ide_hwifs[hw+1].chipset != ide_unknown)
2834 goto bad_option; /* chipset for 2nd port already specified */
2835 printk("\n");
2838 switch (i) {
2839 #ifdef CONFIG_BLK_DEV_PDC4030
2840 case -14: /* "dc4030" */
2842 extern void init_pdc4030(void);
2843 init_pdc4030();
2844 goto done;
2846 #endif /* CONFIG_BLK_DEV_PDC4030 */
2847 #ifdef CONFIG_BLK_DEV_ALI14XX
2848 case -13: /* "ali14xx" */
2850 extern void init_ali14xx (void);
2851 init_ali14xx();
2852 goto done;
2854 #endif /* CONFIG_BLK_DEV_ALI14XX */
2855 #ifdef CONFIG_BLK_DEV_UMC8672
2856 case -12: /* "umc8672" */
2858 extern void init_umc8672 (void);
2859 init_umc8672();
2860 goto done;
2862 #endif /* CONFIG_BLK_DEV_UMC8672 */
2863 #ifdef CONFIG_BLK_DEV_DTC2278
2864 case -11: /* "dtc2278" */
2866 extern void init_dtc2278 (void);
2867 init_dtc2278();
2868 goto done;
2870 #endif /* CONFIG_BLK_DEV_DTC2278 */
2871 #ifdef CONFIG_BLK_DEV_CMD640
2872 case -10: /* "cmd640_vlb" */
2874 extern int cmd640_vlb; /* flag for cmd640.c */
2875 cmd640_vlb = 1;
2876 goto done;
2878 #endif /* CONFIG_BLK_DEV_CMD640 */
2879 #ifdef CONFIG_BLK_DEV_HT6560B
2880 case -9: /* "ht6560b" */
2882 extern void init_ht6560b (void);
2883 init_ht6560b();
2884 goto done;
2886 #endif /* CONFIG_BLK_DEV_HT6560B */
2887 #if CONFIG_BLK_DEV_QD6580
2888 case -8: /* "qd6580" */
2890 extern void init_qd6580 (void);
2891 init_qd6580();
2892 goto done;
2894 #endif /* CONFIG_BLK_DEV_QD6580 */
2895 #ifdef CONFIG_BLK_DEV_4DRIVES
2896 case -7: /* "four" drives on one set of ports */
2898 ide_hwif_t *mate = &ide_hwifs[hw^1];
2899 mate->drives[0].select.all ^= 0x20;
2900 mate->drives[1].select.all ^= 0x20;
2901 hwif->chipset = mate->chipset = ide_4drives;
2902 mate->irq = hwif->irq;
2903 memcpy(mate->io_ports, hwif->io_ports, sizeof(hwif->io_ports));
2904 goto do_serialize;
2906 #endif /* CONFIG_BLK_DEV_4DRIVES */
2907 case -6: /* dma */
2908 hwif->autodma = 1;
2909 goto done;
2910 case -5: /* "reset" */
2911 hwif->reset = 1;
2912 goto done;
2913 case -4: /* "noautotune" */
2914 hwif->drives[0].autotune = 2;
2915 hwif->drives[1].autotune = 2;
2916 goto done;
2917 case -3: /* "autotune" */
2918 hwif->drives[0].autotune = 1;
2919 hwif->drives[1].autotune = 1;
2920 goto done;
2921 case -2: /* "serialize" */
2922 do_serialize:
2923 hwif->mate = &ide_hwifs[hw^1];
2924 hwif->mate->mate = hwif;
2925 hwif->serialized = hwif->mate->serialized = 1;
2926 goto done;
2928 case -1: /* "noprobe" */
2929 hwif->noprobe = 1;
2930 goto done;
2932 case 1: /* base */
2933 vals[1] = vals[0] + 0x206; /* default ctl */
2934 case 2: /* base,ctl */
2935 vals[2] = 0; /* default irq = probe for it */
2936 case 3: /* base,ctl,irq */
2937 hwif->hw.irq = vals[2];
2938 ide_init_hwif_ports(&hwif->hw, (ide_ioreg_t) vals[0], (ide_ioreg_t) vals[1], &hwif->irq);
2939 memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->io_ports));
2940 hwif->irq = vals[2];
2941 hwif->noprobe = 0;
2942 hwif->chipset = ide_generic;
2943 goto done;
2945 case 0: goto bad_option;
2946 default:
2947 printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
2948 return;
2951 bad_option:
2952 printk(" -- BAD OPTION\n");
2953 return;
2954 bad_hwif:
2955 printk("-- NOT SUPPORTED ON ide%d", hw);
2956 done:
2957 printk("\n");
2961 * This routine is called from the partition-table code in genhd.c
2962 * to "convert" a drive to a logical geometry with fewer than 1024 cyls.
2964 * The second parameter, "xparm", determines exactly how the translation
2965 * will be handled:
2966 * 0 = convert to CHS with fewer than 1024 cyls
2967 * using the same method as Ontrack DiskManager.
2968 * 1 = same as "0", plus offset everything by 63 sectors.
2969 * -1 = similar to "0", plus redirect sector 0 to sector 1.
2970 * >1 = convert to a CHS geometry with "xparm" heads.
2972 * Returns 0 if the translation was not possible, if the device was not
2973 * an IDE disk drive, or if a geometry was "forced" on the commandline.
2974 * Returns 1 if the geometry translation was successful.
2977 int ide_xlate_1024 (kdev_t i_rdev, int xparm, const char *msg)
2979 ide_drive_t *drive;
2981 static const byte head_vals[] = {4, 8, 16, 32, 64, 128, 255, 0};
2982 const byte *heads = head_vals;
2983 unsigned long tracks;
2985 drive = get_info_ptr(i_rdev);
2986 if (!drive)
2987 return 0;
2989 if (drive->forced_geom) {
2991 * Update the current 3D drive values.
2993 drive->id->cur_cyls = drive->bios_cyl;
2994 drive->id->cur_heads = drive->bios_head;
2995 drive->id->cur_sectors = drive->bios_sect;
2996 return 0;
2999 if (xparm > 1 && xparm <= drive->bios_head && drive->bios_sect == 63) {
3001 * Update the current 3D drive values.
3003 drive->id->cur_cyls = drive->bios_cyl;
3004 drive->id->cur_heads = drive->bios_head;
3005 drive->id->cur_sectors = drive->bios_sect;
3006 return 0; /* we already have a translation */
3009 printk("%s ", msg);
3011 if (xparm < 0 && (drive->bios_cyl * drive->bios_head * drive->bios_sect) < (1024 * 16 * 63)) {
3013 * Update the current 3D drive values.
3015 drive->id->cur_cyls = drive->bios_cyl;
3016 drive->id->cur_heads = drive->bios_head;
3017 drive->id->cur_sectors = drive->bios_sect;
3018 return 0; /* small disk: no translation needed */
3021 if (drive->id) {
3022 drive->cyl = drive->id->cyls;
3023 drive->head = drive->id->heads;
3024 drive->sect = drive->id->sectors;
3026 drive->bios_cyl = drive->cyl;
3027 drive->bios_head = drive->head;
3028 drive->bios_sect = drive->sect;
3029 drive->special.b.set_geometry = 1;
3031 tracks = drive->bios_cyl * drive->bios_head * drive->bios_sect / 63;
3032 drive->bios_sect = 63;
3033 if (xparm > 1) {
3034 drive->bios_head = xparm;
3035 drive->bios_cyl = tracks / drive->bios_head;
3036 } else {
3037 while (drive->bios_cyl >= 1024) {
3038 drive->bios_head = *heads;
3039 drive->bios_cyl = tracks / drive->bios_head;
3040 if (0 == *++heads)
3041 break;
3043 #if FAKE_FDISK_FOR_EZDRIVE
3044 if (xparm == -1) {
3045 drive->remap_0_to_1 = 1;
3046 printk("[remap 0->1] ");
3047 } else
3048 #endif /* FAKE_FDISK_FOR_EZDRIVE */
3049 if (xparm == 1) {
3050 drive->sect0 = 63;
3051 drive->bios_cyl = (tracks - 1) / drive->bios_head;
3052 printk("[remap +63] ");
3056 drive->part[0].nr_sects = current_capacity(drive);
3057 printk("[%d/%d/%d]", drive->bios_cyl, drive->bios_head, drive->bios_sect);
3059 * Update the current 3D drive values.
3061 drive->id->cur_cyls = drive->bios_cyl;
3062 drive->id->cur_heads = drive->bios_head;
3063 drive->id->cur_sectors = drive->bios_sect;
3064 return 1;
3068 * probe_for_hwifs() finds/initializes "known" IDE interfaces
3070 __initfunc(static void probe_for_hwifs (void))
3072 #ifdef CONFIG_PCI
3073 if (pci_present())
3075 #ifdef CONFIG_BLK_DEV_IDEPCI
3076 ide_scan_pcibus();
3077 #else
3078 #ifdef CONFIG_BLK_DEV_RZ1000
3080 extern void ide_probe_for_rz100x(void);
3081 ide_probe_for_rz100x();
3083 #endif /* CONFIG_BLK_DEV_RZ1000 */
3084 #endif /* CONFIG_BLK_DEV_IDEPCI */
3086 #endif /* CONFIG_PCI */
3088 #ifdef CONFIG_BLK_DEV_CMD640
3090 extern void ide_probe_for_cmd640x(void);
3091 ide_probe_for_cmd640x();
3093 #endif /* CONFIG_BLK_DEV_CMD640 */
3094 #ifdef CONFIG_BLK_DEV_PDC4030
3096 extern int ide_probe_for_pdc4030(void);
3097 (void) ide_probe_for_pdc4030();
3099 #endif /* CONFIG_BLK_DEV_PDC4030 */
3100 #ifdef CONFIG_BLK_DEV_IDE_PMAC
3102 extern void pmac_ide_probe(void);
3103 pmac_ide_probe();
3105 #endif /* CONFIG_BLK_DEV_IDE_PMAC */
3106 #ifdef CONFIG_BLK_DEV_IDE_ICSIDE
3108 extern void icside_init(void);
3109 icside_init();
3111 #endif /* CONFIG_BLK_DEV_IDE_ICSIDE */
3112 #ifdef CONFIG_BLK_DEV_IDE_RAPIDE
3114 extern void rapide_init(void);
3115 rapide_init();
3117 #endif /* CONFIG_BLK_DEV_IDE_RAPIDE */
3118 #ifdef CONFIG_BLK_DEV_GAYLE
3120 extern void gayle_init(void);
3121 gayle_init();
3123 #endif /* CONFIG_BLK_DEV_GAYLE */
3124 #ifdef CONFIG_BLK_DEV_FALCON_IDE
3126 extern void falconide_init(void);
3127 falconide_init();
3129 #endif /* CONFIG_BLK_DEV_FALCON_IDE */
3130 #ifdef CONFIG_BLK_DEV_MAC_IDE
3132 extern void macide_init(void);
3133 macide_init();
3135 #endif /* CONFIG_BLK_DEV_MAC_IDE */
3136 #ifdef CONFIG_BLK_DEV_BUDDHA
3138 extern void buddha_init(void);
3139 buddha_init();
3141 #endif /* CONFIG_BLK_DEV_BUDDHA */
3144 __initfunc(void ide_init_builtin_drivers (void))
3147 * Probe for special PCI and other "known" interface chipsets
3149 probe_for_hwifs ();
3151 #ifdef CONFIG_BLK_DEV_IDE
3152 #if defined(__mc68000__) || defined(CONFIG_APUS)
3153 if (ide_hwifs[0].io_ports[IDE_DATA_OFFSET]) {
3154 ide_get_lock(&ide_lock, NULL, NULL); /* for atari only */
3155 disable_irq(ide_hwifs[0].irq);
3157 #endif /* __mc68000__ || CONFIG_APUS */
3159 (void) ideprobe_init();
3161 #if defined(__mc68000__) || defined(CONFIG_APUS)
3162 if (ide_hwifs[0].io_ports[IDE_DATA_OFFSET]) {
3163 enable_irq(ide_hwifs[0].irq);
3164 ide_release_lock(&ide_lock); /* for atari only */
3166 #endif /* __mc68000__ || CONFIG_APUS */
3167 #endif /* CONFIG_BLK_DEV_IDE */
3169 #ifdef CONFIG_PROC_FS
3170 proc_ide_create();
3171 #endif
3174 * Attempt to match drivers for the available drives
3176 #ifdef CONFIG_BLK_DEV_IDEDISK
3177 (void) idedisk_init();
3178 #endif /* CONFIG_BLK_DEV_IDEDISK */
3179 #ifdef CONFIG_BLK_DEV_IDECD
3180 (void) ide_cdrom_init();
3181 #endif /* CONFIG_BLK_DEV_IDECD */
3182 #ifdef CONFIG_BLK_DEV_IDETAPE
3183 (void) idetape_init();
3184 #endif /* CONFIG_BLK_DEV_IDETAPE */
3185 #ifdef CONFIG_BLK_DEV_IDEFLOPPY
3186 (void) idefloppy_init();
3187 #endif /* CONFIG_BLK_DEV_IDEFLOPPY */
3188 #ifdef CONFIG_BLK_DEV_IDESCSI
3189 #ifdef CONFIG_SCSI
3190 (void) idescsi_init();
3191 #else
3192 #warning ide scsi-emulation selected but no SCSI-subsystem in kernel
3193 #endif
3194 #endif /* CONFIG_BLK_DEV_IDESCSI */
3197 static int default_cleanup (ide_drive_t *drive)
3199 return ide_unregister_subdriver(drive);
3202 static void default_do_request(ide_drive_t *drive, struct request *rq, unsigned long block)
3204 ide_end_request(0, HWGROUP(drive));
3207 static void default_end_request (byte uptodate, ide_hwgroup_t *hwgroup)
3209 ide_end_request(uptodate, hwgroup);
3212 static int default_ioctl (ide_drive_t *drive, struct inode *inode, struct file *file,
3213 unsigned int cmd, unsigned long arg)
3215 return -EIO;
3218 static int default_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
3220 drive->usage--;
3221 return -EIO;
3224 static void default_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
3228 static int default_check_media_change (ide_drive_t *drive)
3230 return 1;
3233 static void default_pre_reset (ide_drive_t *drive)
3237 static unsigned long default_capacity (ide_drive_t *drive)
3239 return 0x7fffffff; /* cdrom or tape */
3242 static void default_special (ide_drive_t *drive)
3244 special_t *s = &drive->special;
3246 s->all = 0;
3247 drive->mult_req = 0;
3250 static void setup_driver_defaults (ide_drive_t *drive)
3252 ide_driver_t *d = drive->driver;
3254 if (d->cleanup == NULL) d->cleanup = default_cleanup;
3255 if (d->do_request == NULL) d->do_request = default_do_request;
3256 if (d->end_request == NULL) d->end_request = default_end_request;
3257 if (d->ioctl == NULL) d->ioctl = default_ioctl;
3258 if (d->open == NULL) d->open = default_open;
3259 if (d->release == NULL) d->release = default_release;
3260 if (d->media_change == NULL) d->media_change = default_check_media_change;
3261 if (d->pre_reset == NULL) d->pre_reset = default_pre_reset;
3262 if (d->capacity == NULL) d->capacity = default_capacity;
3263 if (d->special == NULL) d->special = default_special;
3266 ide_drive_t *ide_scan_devices (byte media, const char *name, ide_driver_t *driver, int n)
3268 unsigned int unit, index, i;
3270 for (index = 0; index < MAX_HWIFS; ++index)
3271 if (ide_hwifs[index].present) goto search;
3272 ide_init_module(IDE_PROBE_MODULE);
3273 search:
3274 for (index = 0, i = 0; index < MAX_HWIFS; ++index) {
3275 ide_hwif_t *hwif = &ide_hwifs[index];
3276 if (!hwif->present)
3277 continue;
3278 for (unit = 0; unit < MAX_DRIVES; ++unit) {
3279 ide_drive_t *drive = &hwif->drives[unit];
3280 char *req = drive->driver_req;
3281 if (*req && !strstr(name, req))
3282 continue;
3283 if (drive->present && drive->media == media && drive->driver == driver && ++i > n)
3284 return drive;
3287 return NULL;
3290 int ide_register_subdriver (ide_drive_t *drive, ide_driver_t *driver, int version)
3292 unsigned long flags;
3294 save_flags(flags); /* all CPUs */
3295 cli(); /* all CPUs */
3296 if (version != IDE_SUBDRIVER_VERSION || !drive->present || drive->driver != NULL || drive->busy || drive->usage) {
3297 restore_flags(flags); /* all CPUs */
3298 return 1;
3300 drive->driver = driver;
3301 setup_driver_defaults(drive);
3302 restore_flags(flags); /* all CPUs */
3303 if (drive->autotune != 2) {
3304 if (driver->supports_dma && HWIF(drive)->dmaproc != NULL)
3305 (void) (HWIF(drive)->dmaproc(ide_dma_check, drive));
3306 drive->dsc_overlap = (drive->next != drive && driver->supports_dsc_overlap);
3307 drive->nice1 = 1;
3309 drive->revalidate = 1;
3310 #ifdef CONFIG_PROC_FS
3311 ide_add_proc_entries(drive->proc, generic_subdriver_entries, drive);
3312 ide_add_proc_entries(drive->proc, driver->proc, drive);
3313 #endif
3314 return 0;
3317 int ide_unregister_subdriver (ide_drive_t *drive)
3319 unsigned long flags;
3321 save_flags(flags); /* all CPUs */
3322 cli(); /* all CPUs */
3323 if (drive->usage || drive->busy || drive->driver == NULL || DRIVER(drive)->busy) {
3324 restore_flags(flags); /* all CPUs */
3325 return 1;
3327 #ifdef CONFIG_PROC_FS
3328 ide_remove_proc_entries(drive->proc, DRIVER(drive)->proc);
3329 ide_remove_proc_entries(drive->proc, generic_subdriver_entries);
3330 #endif
3331 auto_remove_settings(drive);
3332 drive->driver = NULL;
3333 restore_flags(flags); /* all CPUs */
3334 return 0;
3337 int ide_register_module (ide_module_t *module)
3339 ide_module_t *p = ide_modules;
3341 while (p) {
3342 if (p == module)
3343 return 1;
3344 p = p->next;
3346 module->next = ide_modules;
3347 ide_modules = module;
3348 revalidate_drives();
3349 return 0;
3352 void ide_unregister_module (ide_module_t *module)
3354 ide_module_t **p;
3356 for (p = &ide_modules; (*p) && (*p) != module; p = &((*p)->next));
3357 if (*p)
3358 *p = (*p)->next;
3361 struct file_operations ide_fops[] = {{
3362 NULL, /* lseek - default */
3363 block_read, /* read - general block-dev read */
3364 block_write, /* write - general block-dev write */
3365 NULL, /* readdir - bad */
3366 NULL, /* poll */
3367 ide_ioctl, /* ioctl */
3368 NULL, /* mmap */
3369 ide_open, /* open */
3370 NULL, /* flush */
3371 ide_release, /* release */
3372 block_fsync, /* fsync */
3373 NULL, /* fasync */
3374 ide_check_media_change, /* check_media_change */
3375 ide_revalidate_disk /* revalidate */
3378 EXPORT_SYMBOL(ide_hwifs);
3379 EXPORT_SYMBOL(ide_register_module);
3380 EXPORT_SYMBOL(ide_unregister_module);
3381 EXPORT_SYMBOL(ide_spin_wait_hwgroup);
3384 * Probe module
3386 EXPORT_SYMBOL(drive_is_flashcard);
3387 EXPORT_SYMBOL(ide_timer_expiry);
3388 EXPORT_SYMBOL(ide_intr);
3389 EXPORT_SYMBOL(ide_geninit);
3390 EXPORT_SYMBOL(ide_fops);
3391 EXPORT_SYMBOL(ide_get_queue);
3392 EXPORT_SYMBOL(do_ide0_request);
3393 EXPORT_SYMBOL(ide_add_generic_settings);
3394 #if MAX_HWIFS > 1
3395 EXPORT_SYMBOL(do_ide1_request);
3396 #endif /* MAX_HWIFS > 1 */
3397 #if MAX_HWIFS > 2
3398 EXPORT_SYMBOL(do_ide2_request);
3399 #endif /* MAX_HWIFS > 2 */
3400 #if MAX_HWIFS > 3
3401 EXPORT_SYMBOL(do_ide3_request);
3402 #endif /* MAX_HWIFS > 3 */
3403 #if MAX_HWIFS > 4
3404 EXPORT_SYMBOL(do_ide4_request);
3405 #endif /* MAX_HWIFS > 4 */
3406 #if MAX_HWIFS > 5
3407 EXPORT_SYMBOL(do_ide5_request);
3408 #endif /* MAX_HWIFS > 5 */
3409 #if MAX_HWIFS > 6
3410 EXPORT_SYMBOL(do_ide6_request);
3411 #endif /* MAX_HWIFS > 6 */
3412 #if MAX_HWIFS > 7
3413 EXPORT_SYMBOL(do_ide7_request);
3414 #endif /* MAX_HWIFS > 7 */
3417 * Driver module
3419 EXPORT_SYMBOL(ide_scan_devices);
3420 EXPORT_SYMBOL(ide_register_subdriver);
3421 EXPORT_SYMBOL(ide_unregister_subdriver);
3422 EXPORT_SYMBOL(ide_input_data);
3423 EXPORT_SYMBOL(ide_output_data);
3424 EXPORT_SYMBOL(atapi_input_bytes);
3425 EXPORT_SYMBOL(atapi_output_bytes);
3426 EXPORT_SYMBOL(ide_set_handler);
3427 EXPORT_SYMBOL(ide_dump_status);
3428 EXPORT_SYMBOL(ide_error);
3429 EXPORT_SYMBOL(ide_fixstring);
3430 EXPORT_SYMBOL(ide_wait_stat);
3431 EXPORT_SYMBOL(ide_do_reset);
3432 EXPORT_SYMBOL(ide_init_drive_cmd);
3433 EXPORT_SYMBOL(ide_do_drive_cmd);
3434 EXPORT_SYMBOL(ide_end_drive_cmd);
3435 EXPORT_SYMBOL(ide_end_request);
3436 EXPORT_SYMBOL(ide_revalidate_disk);
3437 EXPORT_SYMBOL(ide_cmd);
3438 EXPORT_SYMBOL(ide_wait_cmd);
3439 EXPORT_SYMBOL(ide_delay_50ms);
3440 EXPORT_SYMBOL(ide_config_drive_speed);
3441 EXPORT_SYMBOL(ide_stall_queue);
3442 #ifdef CONFIG_PROC_FS
3443 EXPORT_SYMBOL(ide_add_proc_entries);
3444 EXPORT_SYMBOL(ide_remove_proc_entries);
3445 EXPORT_SYMBOL(proc_ide_read_geometry);
3446 EXPORT_SYMBOL(create_proc_ide_interfaces);
3447 #endif
3448 EXPORT_SYMBOL(ide_add_setting);
3449 EXPORT_SYMBOL(ide_remove_setting);
3451 EXPORT_SYMBOL(ide_register_hw);
3452 EXPORT_SYMBOL(ide_register);
3453 EXPORT_SYMBOL(ide_unregister);
3454 EXPORT_SYMBOL(ide_setup_ports);
3457 * This is gets invoked once during initialization, to set *everything* up
3459 __initfunc(int ide_init (void))
3461 static char banner_printed = 0;
3463 if (!banner_printed) {
3464 printk(KERN_INFO "Uniform Multi-Platform E-IDE driver " REVISION "\n");
3465 banner_printed = 1;
3468 init_ide_data ();
3470 initializing = 1;
3471 ide_init_builtin_drivers();
3472 initializing = 0;
3474 return 0;
3477 #ifdef MODULE
3478 char *options = NULL;
3479 MODULE_PARM(options,"s");
3481 __initfunc(static void parse_options (char *line))
3483 char *next = line;
3485 if (line == NULL || !*line)
3486 return;
3487 while ((line = next) != NULL) {
3488 if ((next = strchr(line,' ')) != NULL)
3489 *next++ = 0;
3490 if (!strncmp(line,"ide",3) || (!strncmp(line,"hd",2) && line[2] != '='))
3491 ide_setup(line);
3495 int init_module (void)
3497 parse_options(options);
3498 return ide_init();
3501 void cleanup_module (void)
3503 int index;
3505 for (index = 0; index < MAX_HWIFS; ++index) {
3506 ide_unregister(index);
3507 #ifdef CONFIG_BLK_DEV_IDEDMA
3508 if (ide_hwifs[index].dma_base)
3509 (void) ide_release_dma(&ide_hwifs[index]);
3510 #endif /* CONFIG_BLK_DEV_IDEDMA */
3512 #ifdef CONFIG_PROC_FS
3513 proc_ide_destroy();
3514 #endif
3516 #endif /* MODULE */