Import 2.3.9pre5
[davej-history.git] / drivers / block / ide.c
blob17b10593d2875d61f6317a45dd8ccb9920404677
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 static int ide_ioctl (struct inode *inode, struct file *file,
2239 unsigned int cmd, unsigned long arg)
2241 int err = 0, major, minor;
2242 ide_drive_t *drive;
2243 struct request rq;
2244 kdev_t dev;
2245 ide_settings_t *setting;
2247 if (!inode || !(dev = inode->i_rdev))
2248 return -EINVAL;
2249 major = MAJOR(dev); minor = MINOR(dev);
2250 if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
2251 return -ENODEV;
2253 if ((setting = ide_find_setting_by_ioctl(drive, cmd)) != NULL) {
2254 if (cmd == setting->read_ioctl) {
2255 err = ide_read_setting(drive, setting);
2256 return err >= 0 ? put_user(err, (long *) arg) : err;
2257 } else {
2258 if ((MINOR(inode->i_rdev) & PARTN_MASK))
2259 return -EINVAL;
2260 return ide_write_setting(drive, setting, arg);
2264 ide_init_drive_cmd (&rq);
2265 switch (cmd) {
2266 case HDIO_GETGEO:
2268 struct hd_geometry *loc = (struct hd_geometry *) arg;
2269 if (!loc || (drive->media != ide_disk && drive->media != ide_floppy)) return -EINVAL;
2270 if (put_user(drive->bios_head, (byte *) &loc->heads)) return -EFAULT;
2271 if (put_user(drive->bios_sect, (byte *) &loc->sectors)) return -EFAULT;
2272 if (put_user(drive->bios_cyl, (unsigned short *) &loc->cylinders)) return -EFAULT;
2273 if (put_user((unsigned)drive->part[MINOR(inode->i_rdev)&PARTN_MASK].start_sect,
2274 (unsigned long *) &loc->start)) return -EFAULT;
2275 return 0;
2278 case BLKGETSIZE: /* Return device size */
2279 return put_user(drive->part[MINOR(inode->i_rdev)&PARTN_MASK].nr_sects, (long *) arg);
2281 case BLKRRPART: /* Re-read partition tables */
2282 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2283 return ide_revalidate_disk(inode->i_rdev);
2285 case HDIO_OBSOLETE_IDENTITY:
2286 case HDIO_GET_IDENTITY:
2287 if (MINOR(inode->i_rdev) & PARTN_MASK)
2288 return -EINVAL;
2289 if (drive->id == NULL)
2290 return -ENOMSG;
2291 if (copy_to_user((char *)arg, (char *)drive->id, (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
2292 return -EFAULT;
2293 return 0;
2295 case HDIO_GET_NICE:
2296 return put_user(drive->dsc_overlap << IDE_NICE_DSC_OVERLAP |
2297 drive->atapi_overlap << IDE_NICE_ATAPI_OVERLAP |
2298 drive->nice0 << IDE_NICE_0 |
2299 drive->nice1 << IDE_NICE_1 |
2300 drive->nice2 << IDE_NICE_2,
2301 (long *) arg);
2302 case HDIO_DRIVE_CMD:
2304 byte args[4], *argbuf = args;
2305 int argsize = 4;
2306 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2307 if (NULL == (void *) arg)
2308 return ide_do_drive_cmd(drive, &rq, ide_wait);
2309 if (copy_from_user(args, (void *)arg, 4))
2310 return -EFAULT;
2311 if (args[3]) {
2312 argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
2313 argbuf = kmalloc(argsize, GFP_KERNEL);
2314 if (argbuf == NULL)
2315 return -ENOMEM;
2316 memcpy(argbuf, args, 4);
2318 if ((((byte *)arg)[0] == WIN_SETFEATURES) &&
2319 (((byte *)arg)[1] > 66) &&
2320 (((byte *)arg)[2] == 3) &&
2321 ((drive->id->word93 & 0x2000) == 0)) {
2322 printk("%s: Speed warnings UDMA 3/4 is not functional.\n", drive->name);
2323 goto abort;
2325 err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
2326 if (!err &&
2327 (((byte *)arg)[0] == WIN_SETFEATURES) &&
2328 (((byte *)arg)[1] >= 16) &&
2329 (((byte *)arg)[2] == 3) &&
2330 (drive->id->dma_ultra ||
2331 drive->id->dma_mword ||
2332 drive->id->dma_1word)) {
2335 * Re-read drive->id for possible DMA mode
2336 * change (copied from ide-probe.c)
2338 struct hd_driveid *id;
2339 unsigned long timeout, irqs, flags;
2341 probe_irq_off(probe_irq_on());
2342 irqs = probe_irq_on();
2343 if (IDE_CONTROL_REG)
2344 OUT_BYTE(drive->ctl,IDE_CONTROL_REG);
2345 ide_delay_50ms();
2346 OUT_BYTE(WIN_IDENTIFY, IDE_COMMAND_REG);
2347 timeout = jiffies + WAIT_WORSTCASE;
2348 do {
2349 if (0 < (signed long)(jiffies - timeout)) {
2350 if (irqs)
2351 (void) probe_irq_off(irqs);
2352 goto abort; /* drive timed-out */
2354 ide_delay_50ms(); /* give drive a breather */
2355 } while (IN_BYTE(IDE_ALTSTATUS_REG) & BUSY_STAT);
2356 ide_delay_50ms(); /* wait for IRQ and DRQ_STAT */
2357 if (!OK_STAT(GET_STAT(),DRQ_STAT,BAD_R_STAT))
2358 goto abort;
2359 __save_flags(flags); /* local CPU only */
2360 __cli(); /* local CPU only; some systems need this */
2361 id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC);
2362 ide_input_data(drive, id, SECTOR_WORDS);
2363 (void) GET_STAT(); /* clear drive IRQ */
2364 ide__sti(); /* local CPU only */
2365 __restore_flags(flags); /* local CPU only */
2366 ide_fix_driveid(id);
2367 if (id && id->cyls) {
2368 drive->id->dma_ultra = id->dma_ultra;
2369 drive->id->dma_mword = id->dma_mword;
2370 drive->id->dma_1word = id->dma_1word;
2371 /* anything more ? */
2372 #ifdef DEBUG
2373 printk("%s: dma_ultra=%04X, dma_mword=%04X, dma_1word=%04X\n",
2374 drive->name, id->dma_ultra, id->dma_mword, id->dma_1word);
2375 #endif
2376 kfree(id);
2379 abort:
2380 if (copy_to_user((void *)arg, argbuf, argsize))
2381 err = -EFAULT;
2382 if (argsize > 4)
2383 kfree(argbuf);
2384 return err;
2387 case HDIO_SCAN_HWIF:
2389 int args[3];
2390 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2391 if (copy_from_user(args, (void *)arg, 3 * sizeof(int)))
2392 return -EFAULT;
2393 if (ide_register(args[0], args[1], args[2]) == -1)
2394 return -EIO;
2395 return 0;
2397 case HDIO_UNREGISTER_HWIF:
2398 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2399 /* should I check here for arg > MAX_HWIFS, or
2400 just let ide_unregister fail silently? -- shaver */
2401 ide_unregister(arg);
2402 return 0;
2403 case HDIO_SET_NICE:
2404 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2405 if (drive->driver == NULL)
2406 return -EPERM;
2407 if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
2408 return -EPERM;
2409 drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
2410 if (drive->dsc_overlap && !DRIVER(drive)->supports_dsc_overlap) {
2411 drive->dsc_overlap = 0;
2412 return -EPERM;
2414 drive->nice1 = (arg >> IDE_NICE_1) & 1;
2415 return 0;
2417 case BLKROSET:
2418 case BLKROGET:
2419 case BLKFLSBUF:
2420 case BLKSSZGET:
2421 case BLKPG:
2422 return blk_ioctl(inode->i_rdev, cmd, arg);
2424 default:
2425 if (drive->driver != NULL)
2426 return DRIVER(drive)->ioctl(drive, inode, file, cmd, arg);
2427 return -EPERM;
2431 static int ide_check_media_change (kdev_t i_rdev)
2433 ide_drive_t *drive;
2435 if ((drive = get_info_ptr(i_rdev)) == NULL)
2436 return -ENODEV;
2437 if (drive->driver != NULL)
2438 return DRIVER(drive)->media_change(drive);
2439 return 0;
2442 void ide_fixstring (byte *s, const int bytecount, const int byteswap)
2444 byte *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */
2446 if (byteswap) {
2447 /* convert from big-endian to host byte order */
2448 for (p = end ; p != s;) {
2449 unsigned short *pp = (unsigned short *) (p -= 2);
2450 *pp = ntohs(*pp);
2454 /* strip leading blanks */
2455 while (s != end && *s == ' ')
2456 ++s;
2458 /* compress internal blanks and strip trailing blanks */
2459 while (s != end && *s) {
2460 if (*s++ != ' ' || (s != end && *s && *s != ' '))
2461 *p++ = *(s-1);
2464 /* wipe out trailing garbage */
2465 while (p != end)
2466 *p++ = '\0';
2472 char *ide_xfer_verbose (byte xfer_rate) {
2473 switch(xfer_rate) {
2474 case XFER_UDMA_4: return("UDMA 4");
2475 case XFER_UDMA_3: return("UDMA 3");
2476 case XFER_UDMA_2: return("UDMA 2");
2477 case XFER_UDMA_1: return("UDMA 1");
2478 case XFER_UDMA_0: return("UDMA 0");
2479 case XFER_MW_DMA_2: return("MW DMA 2");
2480 case XFER_MW_DMA_1: return("MW DMA 1");
2481 case XFER_MW_DMA_0: return("MW DMA 0");
2482 case XFER_SW_DMA_2: return("SW DMA 2");
2483 case XFER_SW_DMA_1: return("SW DMA 1");
2484 case XFER_SW_DMA_0: return("SW DMA 0");
2485 case XFER_PIO_4: return("PIO 4");
2486 case XFER_PIO_3: return("PIO 3");
2487 case XFER_PIO_2: return("PIO 2");
2488 case XFER_PIO_1: return("PIO 1");
2489 case XFER_PIO_0: return("PIO 0");
2490 case XFER_PIO_SLOW: return("PIO SLOW");
2491 default: return("XFER ERROR");
2496 * stridx() returns the offset of c within s,
2497 * or -1 if c is '\0' or not found within s.
2499 __initfunc(static int stridx (const char *s, char c))
2501 char *i = strchr(s, c);
2502 return (i && c) ? i - s : -1;
2506 * match_parm() does parsing for ide_setup():
2508 * 1. the first char of s must be '='.
2509 * 2. if the remainder matches one of the supplied keywords,
2510 * the index (1 based) of the keyword is negated and returned.
2511 * 3. if the remainder is a series of no more than max_vals numbers
2512 * separated by commas, the numbers are saved in vals[] and a
2513 * count of how many were saved is returned. Base10 is assumed,
2514 * and base16 is allowed when prefixed with "0x".
2515 * 4. otherwise, zero is returned.
2517 __initfunc(static int match_parm (char *s, const char *keywords[], int vals[], int max_vals))
2519 static const char *decimal = "0123456789";
2520 static const char *hex = "0123456789abcdef";
2521 int i, n;
2523 if (*s++ == '=') {
2525 * Try matching against the supplied keywords,
2526 * and return -(index+1) if we match one
2528 if (keywords != NULL) {
2529 for (i = 0; *keywords != NULL; ++i) {
2530 if (!strcmp(s, *keywords++))
2531 return -(i+1);
2535 * Look for a series of no more than "max_vals"
2536 * numeric values separated by commas, in base10,
2537 * or base16 when prefixed with "0x".
2538 * Return a count of how many were found.
2540 for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
2541 vals[n] = i;
2542 while ((i = stridx(decimal, *++s)) >= 0)
2543 vals[n] = (vals[n] * 10) + i;
2544 if (*s == 'x' && !vals[n]) {
2545 while ((i = stridx(hex, *++s)) >= 0)
2546 vals[n] = (vals[n] * 0x10) + i;
2548 if (++n == max_vals)
2549 break;
2550 if (*s == ',' || *s == ';')
2551 ++s;
2553 if (!*s)
2554 return n;
2556 return 0; /* zero = nothing matched */
2560 * ide_setup() gets called VERY EARLY during initialization,
2561 * to handle kernel "command line" strings beginning with "hdx="
2562 * or "ide". Here is the complete set currently supported:
2564 * "hdx=" is recognized for all "x" from "a" to "h", such as "hdc".
2565 * "idex=" is recognized for all "x" from "0" to "3", such as "ide1".
2567 * "hdx=noprobe" : drive may be present, but do not probe for it
2568 * "hdx=none" : drive is NOT present, ignore cmos and do not probe
2569 * "hdx=nowerr" : ignore the WRERR_STAT bit on this drive
2570 * "hdx=cdrom" : drive is present, and is a cdrom drive
2571 * "hdx=cyl,head,sect" : disk drive is present, with specified geometry
2572 * "hdx=autotune" : driver will attempt to tune interface speed
2573 * to the fastest PIO mode supported,
2574 * if possible for this drive only.
2575 * Not fully supported by all chipset types,
2576 * and quite likely to cause trouble with
2577 * older/odd IDE drives.
2579 * "hdx=slow" : insert a huge pause after each access to the data
2580 * port. Should be used only as a last resort.
2582 * "hdx=swapdata" : when the drive is a disk, byte swap all data
2583 * "hdx=bswap" : same as above..........
2585 * "idebus=xx" : inform IDE driver of VESA/PCI bus speed in MHz,
2586 * where "xx" is between 20 and 66 inclusive,
2587 * used when tuning chipset PIO modes.
2588 * For PCI bus, 25 is correct for a P75 system,
2589 * 30 is correct for P90,P120,P180 systems,
2590 * and 33 is used for P100,P133,P166 systems.
2591 * If in doubt, use idebus=33 for PCI.
2592 * As for VLB, it is safest to not specify it.
2594 * "idex=noprobe" : do not attempt to access/use this interface
2595 * "idex=base" : probe for an interface at the addr specified,
2596 * where "base" is usually 0x1f0 or 0x170
2597 * and "ctl" is assumed to be "base"+0x206
2598 * "idex=base,ctl" : specify both base and ctl
2599 * "idex=base,ctl,irq" : specify base, ctl, and irq number
2600 * "idex=autotune" : driver will attempt to tune interface speed
2601 * to the fastest PIO mode supported,
2602 * for all drives on this interface.
2603 * Not fully supported by all chipset types,
2604 * and quite likely to cause trouble with
2605 * older/odd IDE drives.
2606 * "idex=noautotune" : driver will NOT attempt to tune interface speed
2607 * This is the default for most chipsets,
2608 * except the cmd640.
2609 * "idex=serialize" : do not overlap operations on idex and ide(x^1)
2610 * "idex=four" : four drives on idex and ide(x^1) share same ports
2611 * "idex=reset" : reset interface before first use
2612 * "idex=dma" : enable DMA by default on both drives if possible
2614 * "splitfifo=betweenChan"
2615 * : FIFO Configuration of VIA 82c586(<nothing>,"A"or"B").
2616 * --see what follows...
2617 * "splitfifo=betweenChan,thresholdprim,thresholdsec"
2618 * : FIFO Configuration of VIA 82c586(<nothing>,"A" or "B").
2619 * betweenChan = 1(all FIFO's to primary channel)
2620 * , 2(all FIFO's to secondary channel)
2621 * , 3 or 4(evenly shared between them).
2622 * note: without FIFO, a channel is (u)dma disabled!
2623 * thresholdprim = 4, 3, 2 or 1
2624 * (standing for 1, 3/4, 1/2, 1/4).
2625 * Sets the threshold of FIFO to begin dma
2626 * transfer on the primary channel.
2627 * thresholdsec = cf upper, but for secondary channel.
2629 * The following are valid ONLY on ide0, (except dc4030)
2630 * and the defaults for the base,ctl ports must not be altered.
2632 * "ide0=dtc2278" : probe/support DTC2278 interface
2633 * "ide0=ht6560b" : probe/support HT6560B interface
2634 * "ide0=cmd640_vlb" : *REQUIRED* for VLB cards with the CMD640 chip
2635 * (not for PCI -- automatically detected)
2636 * "ide0=qd6580" : probe/support qd6580 interface
2637 * "ide0=ali14xx" : probe/support ali14xx chipsets (ALI M1439, M1443, M1445)
2638 * "ide0=umc8672" : probe/support umc8672 chipsets
2639 * "idex=dc4030" : probe/support Promise DC4030VL interface
2641 __initfunc(void ide_setup (char *s))
2643 int i, vals[3];
2644 ide_hwif_t *hwif;
2645 ide_drive_t *drive;
2646 unsigned int hw, unit;
2647 const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
2648 const char max_hwif = '0' + (MAX_HWIFS - 1);
2650 printk("ide_setup: %s", s);
2652 #ifdef CONFIG_BLK_DEV_IDEDOUBLER
2653 if (!strcmp(s, "ide=doubler")) {
2654 extern int ide_doubler;
2656 printk("ide: Enabled support for IDE doublers\n");
2657 ide_doubler = 1;
2658 return;
2660 #endif /* CONFIG_BLK_DEV_IDEDOUBLER */
2662 init_ide_data ();
2665 * Look for drive options: "hdx="
2667 if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
2668 const char *hd_words[] = {"none", "noprobe", "nowerr", "cdrom",
2669 "serialize", "autotune", "noautotune",
2670 "slow", "swapdata", "bswap", NULL};
2671 unit = s[2] - 'a';
2672 hw = unit / MAX_DRIVES;
2673 unit = unit % MAX_DRIVES;
2674 hwif = &ide_hwifs[hw];
2675 drive = &hwif->drives[unit];
2676 if (strncmp(s + 4, "ide-", 4) == 0) {
2677 strncpy(drive->driver_req, s + 4, 9);
2678 goto done;
2680 switch (match_parm(&s[3], hd_words, vals, 3)) {
2681 case -1: /* "none" */
2682 drive->nobios = 1; /* drop into "noprobe" */
2683 case -2: /* "noprobe" */
2684 drive->noprobe = 1;
2685 goto done;
2686 case -3: /* "nowerr" */
2687 drive->bad_wstat = BAD_R_STAT;
2688 hwif->noprobe = 0;
2689 goto done;
2690 case -4: /* "cdrom" */
2691 drive->present = 1;
2692 drive->media = ide_cdrom;
2693 hwif->noprobe = 0;
2694 goto done;
2695 case -5: /* "serialize" */
2696 printk(" -- USE \"ide%d=serialize\" INSTEAD", hw);
2697 goto do_serialize;
2698 case -6: /* "autotune" */
2699 drive->autotune = 1;
2700 goto done;
2701 case -7: /* "noautotune" */
2702 drive->autotune = 2;
2703 goto done;
2704 case -8: /* "slow" */
2705 drive->slow = 1;
2706 goto done;
2707 case -9: /* swapdata or bswap */
2708 case -10:
2709 drive->bswap = 1;
2710 goto done;
2711 case 3: /* cyl,head,sect */
2712 drive->media = ide_disk;
2713 drive->cyl = drive->bios_cyl = vals[0];
2714 drive->head = drive->bios_head = vals[1];
2715 drive->sect = drive->bios_sect = vals[2];
2716 drive->present = 1;
2717 drive->forced_geom = 1;
2718 hwif->noprobe = 0;
2719 goto done;
2720 default:
2721 goto bad_option;
2725 #if defined(CONFIG_BLK_DEV_VIA82C586)
2727 * Look for drive option "splitfifo=..."
2730 if (s[0] == 's' && s[1] == 'p' && s[2] == 'l' &&
2731 s[3] == 'i' && s[4] == 't' && s[5] == 'f' &&
2732 s[6] == 'i' && s[7] == 'f' && s[8] == 'o') {
2733 byte tmp = 0x3a; /* default config byte */
2735 i = match_parm(&s[9], NULL, vals, 3);
2736 switch(i) {
2737 case 3:
2738 tmp &= 0xf0;
2739 if ((vals[1] > 0) && (vals[1] < 5)) {
2740 /* sets threshold for primary Channel: */
2741 byte x = 4 - vals[1];
2742 tmp |= (x << 2);
2744 else
2745 goto bad_option;
2746 if ((vals[2] > 0) && (vals[2] < 5)) {
2747 /* sets threshold for secondary Channel: */
2748 byte x = 4 - vals[2];
2749 tmp |= x;
2751 else
2752 goto bad_option;
2753 case 1:
2754 /* set the FIFO config between channels to 0: */
2755 tmp &= 0x9f;
2756 /* set the needed FIFO config between channels: */
2757 if (vals[0] == 1) /* primary fifo only */
2758 tmp |= 0x10;
2759 else if (vals[0] == 2) /* secondary fifo only */
2760 tmp |= 0x70;
2761 else if (vals[0] == 4) /* other shared fifo config */
2762 tmp |= 0x50;
2763 else if (vals[0] == 3) /* default config */
2764 tmp |= 0x30;
2765 else
2766 goto bad_option;
2767 break;
2768 default:
2769 goto bad_option;
2771 /* set the found option in fifoconfig */
2772 fifoconfig = tmp;
2773 goto done;
2775 #endif /* defined(CONFIG_BLK_DEV_VIA82C586) */
2777 if (s[0] != 'i' || s[1] != 'd' || s[2] != 'e')
2778 goto bad_option;
2780 * Look for bus speed option: "idebus="
2782 if (s[3] == 'b' && s[4] == 'u' && s[5] == 's') {
2783 if (match_parm(&s[6], NULL, vals, 1) != 1)
2784 goto bad_option;
2785 if (vals[0] >= 20 && vals[0] <= 66)
2786 idebus_parameter = vals[0];
2787 else
2788 printk(" -- BAD BUS SPEED! Expected value from 20 to 66");
2789 goto done;
2792 * Look for interface options: "idex="
2794 if (s[3] >= '0' && s[3] <= max_hwif) {
2796 * Be VERY CAREFUL changing this: note hardcoded indexes below
2798 const char *ide_words[] = {"noprobe", "serialize", "autotune", "noautotune", "reset", "dma", "four",
2799 "qd6580", "ht6560b", "cmd640_vlb", "dtc2278", "umc8672", "ali14xx", "dc4030", NULL};
2800 hw = s[3] - '0';
2801 hwif = &ide_hwifs[hw];
2802 i = match_parm(&s[4], ide_words, vals, 3);
2805 * Cryptic check to ensure chipset not already set for hwif:
2807 if (i > 0 || i <= -7) { /* is parameter a chipset name? */
2808 if (hwif->chipset != ide_unknown)
2809 goto bad_option; /* chipset already specified */
2810 if (i <= -7 && i != -14 && hw != 0)
2811 goto bad_hwif; /* chipset drivers are for "ide0=" only */
2812 if (i <= -7 && i != -14 && ide_hwifs[hw+1].chipset != ide_unknown)
2813 goto bad_option; /* chipset for 2nd port already specified */
2814 printk("\n");
2817 switch (i) {
2818 #ifdef CONFIG_BLK_DEV_PDC4030
2819 case -14: /* "dc4030" */
2821 extern void init_pdc4030(void);
2822 init_pdc4030();
2823 goto done;
2825 #endif /* CONFIG_BLK_DEV_PDC4030 */
2826 #ifdef CONFIG_BLK_DEV_ALI14XX
2827 case -13: /* "ali14xx" */
2829 extern void init_ali14xx (void);
2830 init_ali14xx();
2831 goto done;
2833 #endif /* CONFIG_BLK_DEV_ALI14XX */
2834 #ifdef CONFIG_BLK_DEV_UMC8672
2835 case -12: /* "umc8672" */
2837 extern void init_umc8672 (void);
2838 init_umc8672();
2839 goto done;
2841 #endif /* CONFIG_BLK_DEV_UMC8672 */
2842 #ifdef CONFIG_BLK_DEV_DTC2278
2843 case -11: /* "dtc2278" */
2845 extern void init_dtc2278 (void);
2846 init_dtc2278();
2847 goto done;
2849 #endif /* CONFIG_BLK_DEV_DTC2278 */
2850 #ifdef CONFIG_BLK_DEV_CMD640
2851 case -10: /* "cmd640_vlb" */
2853 extern int cmd640_vlb; /* flag for cmd640.c */
2854 cmd640_vlb = 1;
2855 goto done;
2857 #endif /* CONFIG_BLK_DEV_CMD640 */
2858 #ifdef CONFIG_BLK_DEV_HT6560B
2859 case -9: /* "ht6560b" */
2861 extern void init_ht6560b (void);
2862 init_ht6560b();
2863 goto done;
2865 #endif /* CONFIG_BLK_DEV_HT6560B */
2866 #if CONFIG_BLK_DEV_QD6580
2867 case -8: /* "qd6580" */
2869 extern void init_qd6580 (void);
2870 init_qd6580();
2871 goto done;
2873 #endif /* CONFIG_BLK_DEV_QD6580 */
2874 #ifdef CONFIG_BLK_DEV_4DRIVES
2875 case -7: /* "four" drives on one set of ports */
2877 ide_hwif_t *mate = &ide_hwifs[hw^1];
2878 mate->drives[0].select.all ^= 0x20;
2879 mate->drives[1].select.all ^= 0x20;
2880 hwif->chipset = mate->chipset = ide_4drives;
2881 mate->irq = hwif->irq;
2882 memcpy(mate->io_ports, hwif->io_ports, sizeof(hwif->io_ports));
2883 goto do_serialize;
2885 #endif /* CONFIG_BLK_DEV_4DRIVES */
2886 case -6: /* dma */
2887 hwif->autodma = 1;
2888 goto done;
2889 case -5: /* "reset" */
2890 hwif->reset = 1;
2891 goto done;
2892 case -4: /* "noautotune" */
2893 hwif->drives[0].autotune = 2;
2894 hwif->drives[1].autotune = 2;
2895 goto done;
2896 case -3: /* "autotune" */
2897 hwif->drives[0].autotune = 1;
2898 hwif->drives[1].autotune = 1;
2899 goto done;
2900 case -2: /* "serialize" */
2901 do_serialize:
2902 hwif->mate = &ide_hwifs[hw^1];
2903 hwif->mate->mate = hwif;
2904 hwif->serialized = hwif->mate->serialized = 1;
2905 goto done;
2907 case -1: /* "noprobe" */
2908 hwif->noprobe = 1;
2909 goto done;
2911 case 1: /* base */
2912 vals[1] = vals[0] + 0x206; /* default ctl */
2913 case 2: /* base,ctl */
2914 vals[2] = 0; /* default irq = probe for it */
2915 case 3: /* base,ctl,irq */
2916 hwif->hw.irq = vals[2];
2917 ide_init_hwif_ports(&hwif->hw, (ide_ioreg_t) vals[0], (ide_ioreg_t) vals[1], &hwif->irq);
2918 memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->io_ports));
2919 hwif->irq = vals[2];
2920 hwif->noprobe = 0;
2921 hwif->chipset = ide_generic;
2922 goto done;
2924 case 0: goto bad_option;
2925 default:
2926 printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
2927 return;
2930 bad_option:
2931 printk(" -- BAD OPTION\n");
2932 return;
2933 bad_hwif:
2934 printk("-- NOT SUPPORTED ON ide%d", hw);
2935 done:
2936 printk("\n");
2940 * This routine is called from the partition-table code in genhd.c
2941 * to "convert" a drive to a logical geometry with fewer than 1024 cyls.
2943 * The second parameter, "xparm", determines exactly how the translation
2944 * will be handled:
2945 * 0 = convert to CHS with fewer than 1024 cyls
2946 * using the same method as Ontrack DiskManager.
2947 * 1 = same as "0", plus offset everything by 63 sectors.
2948 * -1 = similar to "0", plus redirect sector 0 to sector 1.
2949 * >1 = convert to a CHS geometry with "xparm" heads.
2951 * Returns 0 if the translation was not possible, if the device was not
2952 * an IDE disk drive, or if a geometry was "forced" on the commandline.
2953 * Returns 1 if the geometry translation was successful.
2956 int ide_xlate_1024 (kdev_t i_rdev, int xparm, const char *msg)
2958 ide_drive_t *drive;
2960 static const byte head_vals[] = {4, 8, 16, 32, 64, 128, 255, 0};
2961 const byte *heads = head_vals;
2962 unsigned long tracks;
2964 drive = get_info_ptr(i_rdev);
2965 if (!drive)
2966 return 0;
2968 if (drive->forced_geom) {
2970 * Update the current 3D drive values.
2972 drive->id->cur_cyls = drive->bios_cyl;
2973 drive->id->cur_heads = drive->bios_head;
2974 drive->id->cur_sectors = drive->bios_sect;
2975 return 0;
2978 if (xparm > 1 && xparm <= drive->bios_head && drive->bios_sect == 63) {
2980 * Update the current 3D drive values.
2982 drive->id->cur_cyls = drive->bios_cyl;
2983 drive->id->cur_heads = drive->bios_head;
2984 drive->id->cur_sectors = drive->bios_sect;
2985 return 0; /* we already have a translation */
2988 printk("%s ", msg);
2990 if (xparm < 0 && (drive->bios_cyl * drive->bios_head * drive->bios_sect) < (1024 * 16 * 63)) {
2992 * Update the current 3D drive values.
2994 drive->id->cur_cyls = drive->bios_cyl;
2995 drive->id->cur_heads = drive->bios_head;
2996 drive->id->cur_sectors = drive->bios_sect;
2997 return 0; /* small disk: no translation needed */
3000 if (drive->id) {
3001 drive->cyl = drive->id->cyls;
3002 drive->head = drive->id->heads;
3003 drive->sect = drive->id->sectors;
3005 drive->bios_cyl = drive->cyl;
3006 drive->bios_head = drive->head;
3007 drive->bios_sect = drive->sect;
3008 drive->special.b.set_geometry = 1;
3010 tracks = drive->bios_cyl * drive->bios_head * drive->bios_sect / 63;
3011 drive->bios_sect = 63;
3012 if (xparm > 1) {
3013 drive->bios_head = xparm;
3014 drive->bios_cyl = tracks / drive->bios_head;
3015 } else {
3016 while (drive->bios_cyl >= 1024) {
3017 drive->bios_head = *heads;
3018 drive->bios_cyl = tracks / drive->bios_head;
3019 if (0 == *++heads)
3020 break;
3022 #if FAKE_FDISK_FOR_EZDRIVE
3023 if (xparm == -1) {
3024 drive->remap_0_to_1 = 1;
3025 printk("[remap 0->1] ");
3026 } else
3027 #endif /* FAKE_FDISK_FOR_EZDRIVE */
3028 if (xparm == 1) {
3029 drive->sect0 = 63;
3030 drive->bios_cyl = (tracks - 1) / drive->bios_head;
3031 printk("[remap +63] ");
3035 drive->part[0].nr_sects = current_capacity(drive);
3036 printk("[%d/%d/%d]", drive->bios_cyl, drive->bios_head, drive->bios_sect);
3038 * Update the current 3D drive values.
3040 drive->id->cur_cyls = drive->bios_cyl;
3041 drive->id->cur_heads = drive->bios_head;
3042 drive->id->cur_sectors = drive->bios_sect;
3043 return 1;
3047 * probe_for_hwifs() finds/initializes "known" IDE interfaces
3049 __initfunc(static void probe_for_hwifs (void))
3051 #ifdef CONFIG_PCI
3052 if (pci_present())
3054 #ifdef CONFIG_BLK_DEV_IDEPCI
3055 ide_scan_pcibus();
3056 #else
3057 #ifdef CONFIG_BLK_DEV_RZ1000
3059 extern void ide_probe_for_rz100x(void);
3060 ide_probe_for_rz100x();
3062 #endif /* CONFIG_BLK_DEV_RZ1000 */
3063 #endif /* CONFIG_BLK_DEV_IDEPCI */
3065 #endif /* CONFIG_PCI */
3067 #ifdef CONFIG_BLK_DEV_CMD640
3069 extern void ide_probe_for_cmd640x(void);
3070 ide_probe_for_cmd640x();
3072 #endif /* CONFIG_BLK_DEV_CMD640 */
3073 #ifdef CONFIG_BLK_DEV_PDC4030
3075 extern int ide_probe_for_pdc4030(void);
3076 (void) ide_probe_for_pdc4030();
3078 #endif /* CONFIG_BLK_DEV_PDC4030 */
3079 #ifdef CONFIG_BLK_DEV_IDE_PMAC
3081 extern void pmac_ide_probe(void);
3082 pmac_ide_probe();
3084 #endif /* CONFIG_BLK_DEV_IDE_PMAC */
3085 #ifdef CONFIG_BLK_DEV_IDE_ICSIDE
3087 extern void icside_init(void);
3088 icside_init();
3090 #endif /* CONFIG_BLK_DEV_IDE_ICSIDE */
3091 #ifdef CONFIG_BLK_DEV_IDE_RAPIDE
3093 extern void rapide_init(void);
3094 rapide_init();
3096 #endif /* CONFIG_BLK_DEV_IDE_RAPIDE */
3097 #ifdef CONFIG_BLK_DEV_GAYLE
3099 extern void gayle_init(void);
3100 gayle_init();
3102 #endif /* CONFIG_BLK_DEV_GAYLE */
3103 #ifdef CONFIG_BLK_DEV_FALCON_IDE
3105 extern void falconide_init(void);
3106 falconide_init();
3108 #endif /* CONFIG_BLK_DEV_FALCON_IDE */
3109 #ifdef CONFIG_BLK_DEV_MAC_IDE
3111 extern void macide_init(void);
3112 macide_init();
3114 #endif /* CONFIG_BLK_DEV_MAC_IDE */
3115 #ifdef CONFIG_BLK_DEV_BUDDHA
3117 extern void buddha_init(void);
3118 buddha_init();
3120 #endif /* CONFIG_BLK_DEV_BUDDHA */
3123 __initfunc(void ide_init_builtin_drivers (void))
3126 * Probe for special PCI and other "known" interface chipsets
3128 probe_for_hwifs ();
3130 #ifdef CONFIG_BLK_DEV_IDE
3131 #if defined(__mc68000__) || defined(CONFIG_APUS)
3132 if (ide_hwifs[0].io_ports[IDE_DATA_OFFSET]) {
3133 ide_get_lock(&ide_lock, NULL, NULL); /* for atari only */
3134 disable_irq(ide_hwifs[0].irq);
3136 #endif /* __mc68000__ || CONFIG_APUS */
3138 (void) ideprobe_init();
3140 #if defined(__mc68000__) || defined(CONFIG_APUS)
3141 if (ide_hwifs[0].io_ports[IDE_DATA_OFFSET]) {
3142 enable_irq(ide_hwifs[0].irq);
3143 ide_release_lock(&ide_lock); /* for atari only */
3145 #endif /* __mc68000__ || CONFIG_APUS */
3146 #endif /* CONFIG_BLK_DEV_IDE */
3148 #ifdef CONFIG_PROC_FS
3149 proc_ide_create();
3150 #endif
3153 * Attempt to match drivers for the available drives
3155 #ifdef CONFIG_BLK_DEV_IDEDISK
3156 (void) idedisk_init();
3157 #endif /* CONFIG_BLK_DEV_IDEDISK */
3158 #ifdef CONFIG_BLK_DEV_IDECD
3159 (void) ide_cdrom_init();
3160 #endif /* CONFIG_BLK_DEV_IDECD */
3161 #ifdef CONFIG_BLK_DEV_IDETAPE
3162 (void) idetape_init();
3163 #endif /* CONFIG_BLK_DEV_IDETAPE */
3164 #ifdef CONFIG_BLK_DEV_IDEFLOPPY
3165 (void) idefloppy_init();
3166 #endif /* CONFIG_BLK_DEV_IDEFLOPPY */
3167 #ifdef CONFIG_BLK_DEV_IDESCSI
3168 #ifdef CONFIG_SCSI
3169 (void) idescsi_init();
3170 #else
3171 #warning ide scsi-emulation selected but no SCSI-subsystem in kernel
3172 #endif
3173 #endif /* CONFIG_BLK_DEV_IDESCSI */
3176 static int default_cleanup (ide_drive_t *drive)
3178 return ide_unregister_subdriver(drive);
3181 static void default_do_request(ide_drive_t *drive, struct request *rq, unsigned long block)
3183 ide_end_request(0, HWGROUP(drive));
3186 static void default_end_request (byte uptodate, ide_hwgroup_t *hwgroup)
3188 ide_end_request(uptodate, hwgroup);
3191 static int default_ioctl (ide_drive_t *drive, struct inode *inode, struct file *file,
3192 unsigned int cmd, unsigned long arg)
3194 return -EIO;
3197 static int default_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
3199 drive->usage--;
3200 return -EIO;
3203 static void default_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
3207 static int default_check_media_change (ide_drive_t *drive)
3209 return 1;
3212 static void default_pre_reset (ide_drive_t *drive)
3216 static unsigned long default_capacity (ide_drive_t *drive)
3218 return 0x7fffffff; /* cdrom or tape */
3221 static void default_special (ide_drive_t *drive)
3223 special_t *s = &drive->special;
3225 s->all = 0;
3226 drive->mult_req = 0;
3229 static void setup_driver_defaults (ide_drive_t *drive)
3231 ide_driver_t *d = drive->driver;
3233 if (d->cleanup == NULL) d->cleanup = default_cleanup;
3234 if (d->do_request == NULL) d->do_request = default_do_request;
3235 if (d->end_request == NULL) d->end_request = default_end_request;
3236 if (d->ioctl == NULL) d->ioctl = default_ioctl;
3237 if (d->open == NULL) d->open = default_open;
3238 if (d->release == NULL) d->release = default_release;
3239 if (d->media_change == NULL) d->media_change = default_check_media_change;
3240 if (d->pre_reset == NULL) d->pre_reset = default_pre_reset;
3241 if (d->capacity == NULL) d->capacity = default_capacity;
3242 if (d->special == NULL) d->special = default_special;
3245 ide_drive_t *ide_scan_devices (byte media, const char *name, ide_driver_t *driver, int n)
3247 unsigned int unit, index, i;
3249 for (index = 0; index < MAX_HWIFS; ++index)
3250 if (ide_hwifs[index].present) goto search;
3251 ide_init_module(IDE_PROBE_MODULE);
3252 search:
3253 for (index = 0, i = 0; index < MAX_HWIFS; ++index) {
3254 ide_hwif_t *hwif = &ide_hwifs[index];
3255 if (!hwif->present)
3256 continue;
3257 for (unit = 0; unit < MAX_DRIVES; ++unit) {
3258 ide_drive_t *drive = &hwif->drives[unit];
3259 char *req = drive->driver_req;
3260 if (*req && !strstr(name, req))
3261 continue;
3262 if (drive->present && drive->media == media && drive->driver == driver && ++i > n)
3263 return drive;
3266 return NULL;
3269 int ide_register_subdriver (ide_drive_t *drive, ide_driver_t *driver, int version)
3271 unsigned long flags;
3273 save_flags(flags); /* all CPUs */
3274 cli(); /* all CPUs */
3275 if (version != IDE_SUBDRIVER_VERSION || !drive->present || drive->driver != NULL || drive->busy || drive->usage) {
3276 restore_flags(flags); /* all CPUs */
3277 return 1;
3279 drive->driver = driver;
3280 setup_driver_defaults(drive);
3281 restore_flags(flags); /* all CPUs */
3282 if (drive->autotune != 2) {
3283 if (driver->supports_dma && HWIF(drive)->dmaproc != NULL)
3284 (void) (HWIF(drive)->dmaproc(ide_dma_check, drive));
3285 drive->dsc_overlap = (drive->next != drive && driver->supports_dsc_overlap);
3286 drive->nice1 = 1;
3288 drive->revalidate = 1;
3289 #ifdef CONFIG_PROC_FS
3290 ide_add_proc_entries(drive->proc, generic_subdriver_entries, drive);
3291 ide_add_proc_entries(drive->proc, driver->proc, drive);
3292 #endif
3293 return 0;
3296 int ide_unregister_subdriver (ide_drive_t *drive)
3298 unsigned long flags;
3300 save_flags(flags); /* all CPUs */
3301 cli(); /* all CPUs */
3302 if (drive->usage || drive->busy || drive->driver == NULL || DRIVER(drive)->busy) {
3303 restore_flags(flags); /* all CPUs */
3304 return 1;
3306 #ifdef CONFIG_PROC_FS
3307 ide_remove_proc_entries(drive->proc, DRIVER(drive)->proc);
3308 ide_remove_proc_entries(drive->proc, generic_subdriver_entries);
3309 #endif
3310 auto_remove_settings(drive);
3311 drive->driver = NULL;
3312 restore_flags(flags); /* all CPUs */
3313 return 0;
3316 int ide_register_module (ide_module_t *module)
3318 ide_module_t *p = ide_modules;
3320 while (p) {
3321 if (p == module)
3322 return 1;
3323 p = p->next;
3325 module->next = ide_modules;
3326 ide_modules = module;
3327 revalidate_drives();
3328 return 0;
3331 void ide_unregister_module (ide_module_t *module)
3333 ide_module_t **p;
3335 for (p = &ide_modules; (*p) && (*p) != module; p = &((*p)->next));
3336 if (*p)
3337 *p = (*p)->next;
3340 struct file_operations ide_fops[] = {{
3341 NULL, /* lseek - default */
3342 block_read, /* read - general block-dev read */
3343 block_write, /* write - general block-dev write */
3344 NULL, /* readdir - bad */
3345 NULL, /* poll */
3346 ide_ioctl, /* ioctl */
3347 NULL, /* mmap */
3348 ide_open, /* open */
3349 NULL, /* flush */
3350 ide_release, /* release */
3351 block_fsync, /* fsync */
3352 NULL, /* fasync */
3353 ide_check_media_change, /* check_media_change */
3354 ide_revalidate_disk /* revalidate */
3357 EXPORT_SYMBOL(ide_hwifs);
3358 EXPORT_SYMBOL(ide_register_module);
3359 EXPORT_SYMBOL(ide_unregister_module);
3360 EXPORT_SYMBOL(ide_spin_wait_hwgroup);
3363 * Probe module
3365 EXPORT_SYMBOL(drive_is_flashcard);
3366 EXPORT_SYMBOL(ide_timer_expiry);
3367 EXPORT_SYMBOL(ide_intr);
3368 EXPORT_SYMBOL(ide_geninit);
3369 EXPORT_SYMBOL(ide_fops);
3370 EXPORT_SYMBOL(ide_get_queue);
3371 EXPORT_SYMBOL(do_ide0_request);
3372 EXPORT_SYMBOL(ide_add_generic_settings);
3373 #if MAX_HWIFS > 1
3374 EXPORT_SYMBOL(do_ide1_request);
3375 #endif /* MAX_HWIFS > 1 */
3376 #if MAX_HWIFS > 2
3377 EXPORT_SYMBOL(do_ide2_request);
3378 #endif /* MAX_HWIFS > 2 */
3379 #if MAX_HWIFS > 3
3380 EXPORT_SYMBOL(do_ide3_request);
3381 #endif /* MAX_HWIFS > 3 */
3382 #if MAX_HWIFS > 4
3383 EXPORT_SYMBOL(do_ide4_request);
3384 #endif /* MAX_HWIFS > 4 */
3385 #if MAX_HWIFS > 5
3386 EXPORT_SYMBOL(do_ide5_request);
3387 #endif /* MAX_HWIFS > 5 */
3388 #if MAX_HWIFS > 6
3389 EXPORT_SYMBOL(do_ide6_request);
3390 #endif /* MAX_HWIFS > 6 */
3391 #if MAX_HWIFS > 7
3392 EXPORT_SYMBOL(do_ide7_request);
3393 #endif /* MAX_HWIFS > 7 */
3396 * Driver module
3398 EXPORT_SYMBOL(ide_scan_devices);
3399 EXPORT_SYMBOL(ide_register_subdriver);
3400 EXPORT_SYMBOL(ide_unregister_subdriver);
3401 EXPORT_SYMBOL(ide_input_data);
3402 EXPORT_SYMBOL(ide_output_data);
3403 EXPORT_SYMBOL(atapi_input_bytes);
3404 EXPORT_SYMBOL(atapi_output_bytes);
3405 EXPORT_SYMBOL(ide_set_handler);
3406 EXPORT_SYMBOL(ide_dump_status);
3407 EXPORT_SYMBOL(ide_error);
3408 EXPORT_SYMBOL(ide_fixstring);
3409 EXPORT_SYMBOL(ide_wait_stat);
3410 EXPORT_SYMBOL(ide_do_reset);
3411 EXPORT_SYMBOL(ide_init_drive_cmd);
3412 EXPORT_SYMBOL(ide_do_drive_cmd);
3413 EXPORT_SYMBOL(ide_end_drive_cmd);
3414 EXPORT_SYMBOL(ide_end_request);
3415 EXPORT_SYMBOL(ide_revalidate_disk);
3416 EXPORT_SYMBOL(ide_cmd);
3417 EXPORT_SYMBOL(ide_wait_cmd);
3418 EXPORT_SYMBOL(ide_delay_50ms);
3419 EXPORT_SYMBOL(ide_stall_queue);
3420 #ifdef CONFIG_PROC_FS
3421 EXPORT_SYMBOL(ide_add_proc_entries);
3422 EXPORT_SYMBOL(ide_remove_proc_entries);
3423 EXPORT_SYMBOL(proc_ide_read_geometry);
3424 EXPORT_SYMBOL(create_proc_ide_interfaces);
3425 #endif
3426 EXPORT_SYMBOL(ide_add_setting);
3427 EXPORT_SYMBOL(ide_remove_setting);
3429 EXPORT_SYMBOL(ide_register_hw);
3430 EXPORT_SYMBOL(ide_register);
3431 EXPORT_SYMBOL(ide_unregister);
3432 EXPORT_SYMBOL(ide_setup_ports);
3435 * This is gets invoked once during initialization, to set *everything* up
3437 __initfunc(int ide_init (void))
3439 static char banner_printed = 0;
3441 if (!banner_printed) {
3442 printk(KERN_INFO "Uniform Multi-Platform E-IDE driver " REVISION "\n");
3443 banner_printed = 1;
3446 init_ide_data ();
3448 initializing = 1;
3449 ide_init_builtin_drivers();
3450 initializing = 0;
3452 return 0;
3455 #ifdef MODULE
3456 char *options = NULL;
3457 MODULE_PARM(options,"s");
3459 __initfunc(static void parse_options (char *line))
3461 char *next = line;
3463 if (line == NULL || !*line)
3464 return;
3465 while ((line = next) != NULL) {
3466 if ((next = strchr(line,' ')) != NULL)
3467 *next++ = 0;
3468 if (!strncmp(line,"ide",3) || (!strncmp(line,"hd",2) && line[2] != '='))
3469 ide_setup(line);
3473 int init_module (void)
3475 parse_options(options);
3476 return ide_init();
3479 void cleanup_module (void)
3481 int index;
3483 for (index = 0; index < MAX_HWIFS; ++index) {
3484 ide_unregister(index);
3485 #ifdef CONFIG_BLK_DEV_IDEDMA
3486 if (ide_hwifs[index].dma_base)
3487 (void) ide_release_dma(&ide_hwifs[index]);
3488 #endif /* CONFIG_BLK_DEV_IDEDMA */
3490 #ifdef CONFIG_PROC_FS
3491 proc_ide_destroy();
3492 #endif
3494 #endif /* MODULE */