Import 2.3.39pre2
[davej-history.git] / drivers / block / ide.c
blob996219398f7d26d831dd8b4f9394ea04f6664c8d
1 /*
2 * linux/drivers/block/ide.c Version 6.21 November 9, 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@ieee.org) ("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 * add support for ide6/ide7
100 * delay_50ms() changed to ide_delay_50ms() and exported.
101 * Version 6.20 Added/Fixed Generic ATA-66 support and hwif detection.
102 * Added hdx=flash to allow for second flash disk
103 * detection w/o the hang loop.
104 * Added support for ide8/ide9
105 * Added idex=ata66 for the quirky chipsets that are
106 * ATA-66 compliant, but have yet to determine a method
107 * of verification of the 80c cable presence.
108 * Specifically Promise's PDC20262 chipset.
109 * Version 6.21 Fixing/Fixed SMP spinlock issue with insight from an old
110 * hat that clarified original low level driver design.
112 * Some additional driver compile-time options are in ./include/linux/ide.h
114 * To do, in likely order of completion:
115 * - modify kernel to obtain BIOS geometry for drives on 2nd/3rd/4th i/f
119 #define REVISION "Revision: 6.21"
120 #define VERSION "Id: ide.c 6.21 1999/11/10"
122 #undef REALLY_SLOW_IO /* most systems can safely undef this */
124 #define _IDE_C /* Tell ide.h it's really us */
126 #include <linux/config.h>
127 #include <linux/module.h>
128 #include <linux/types.h>
129 #include <linux/string.h>
130 #include <linux/kernel.h>
131 #include <linux/timer.h>
132 #include <linux/mm.h>
133 #include <linux/interrupt.h>
134 #include <linux/major.h>
135 #include <linux/errno.h>
136 #include <linux/genhd.h>
137 #include <linux/blkpg.h>
138 #include <linux/malloc.h>
139 #ifndef MODULE
140 #include <linux/init.h>
141 #endif /* MODULE */
142 #include <linux/pci.h>
143 #include <linux/delay.h>
144 #include <linux/ide.h>
146 #include <asm/byteorder.h>
147 #include <asm/irq.h>
148 #include <asm/uaccess.h>
149 #include <asm/io.h>
150 #include <asm/bitops.h>
152 #include "ide_modes.h"
154 #ifdef CONFIG_KMOD
155 #include <linux/kmod.h>
156 #endif /* CONFIG_KMOD */
158 #ifdef CONFIG_BLK_DEV_VIA82CXXX
159 extern byte fifoconfig; /* defined in via82cxxx.c used by ide_setup() */
160 #endif /* CONFIG_BLK_DEV_VIA82CXXX */
162 static const byte ide_hwif_to_major[] = { IDE0_MAJOR, IDE1_MAJOR,
163 IDE2_MAJOR, IDE3_MAJOR,
164 IDE4_MAJOR, IDE5_MAJOR,
165 IDE6_MAJOR, IDE7_MAJOR,
166 IDE8_MAJOR, IDE9_MAJOR };
168 static int idebus_parameter; /* holds the "idebus=" parameter */
169 static int system_bus_speed; /* holds what we think is VESA/PCI bus speed */
170 static int initializing; /* set while initializing built-in drivers */
172 #if defined(__mc68000__) || defined(CONFIG_APUS)
174 * ide_lock is used by the Atari code to obtain access to the IDE interrupt,
175 * which is shared between several drivers.
177 static int ide_lock = 0;
178 #endif /* __mc68000__ || CONFIG_APUS */
181 * ide_modules keeps track of the available IDE chipset/probe/driver modules.
183 ide_module_t *ide_modules = NULL;
186 * This is declared extern in ide.h, for access by other IDE modules:
188 ide_hwif_t ide_hwifs[MAX_HWIFS]; /* master data repository */
190 #if (DISK_RECOVERY_TIME > 0)
192 * For really screwy hardware (hey, at least it *can* be used with Linux)
193 * we can enforce a minimum delay time between successive operations.
195 static unsigned long read_timer (void)
197 unsigned long t, flags;
198 int i;
200 __save_flags(flags); /* local CPU only */
201 __cli(); /* local CPU only */
202 t = jiffies * 11932;
203 outb_p(0, 0x43);
204 i = inb_p(0x40);
205 i |= inb(0x40) << 8;
206 __restore_flags(flags); /* local CPU only */
207 return (t - i);
209 #endif /* DISK_RECOVERY_TIME */
211 static inline void set_recovery_timer (ide_hwif_t *hwif)
213 #if (DISK_RECOVERY_TIME > 0)
214 hwif->last_time = read_timer();
215 #endif /* DISK_RECOVERY_TIME */
219 * Do not even *think* about calling this!
221 static void init_hwif_data (unsigned int index)
223 unsigned int unit;
224 hw_regs_t hw;
225 ide_hwif_t *hwif = &ide_hwifs[index];
227 /* bulk initialize hwif & drive info with zeros */
228 memset(hwif, 0, sizeof(ide_hwif_t));
229 memset(&hw, 0, sizeof(hw_regs_t));
231 /* fill in any non-zero initial values */
232 hwif->index = index;
233 ide_init_hwif_ports(&hw, ide_default_io_base(index), 0, &hwif->irq);
234 memcpy(&hwif->hw, &hw, sizeof(hw));
235 memcpy(hwif->io_ports, hw.io_ports, sizeof(hw.io_ports));
236 hwif->noprobe = !hwif->io_ports[IDE_DATA_OFFSET];
237 #ifdef CONFIG_BLK_DEV_HD
238 if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA)
239 hwif->noprobe = 1; /* may be overridden by ide_setup() */
240 #endif /* CONFIG_BLK_DEV_HD */
241 hwif->major = ide_hwif_to_major[index];
242 hwif->name[0] = 'i';
243 hwif->name[1] = 'd';
244 hwif->name[2] = 'e';
245 hwif->name[3] = '0' + index;
246 for (unit = 0; unit < MAX_DRIVES; ++unit) {
247 ide_drive_t *drive = &hwif->drives[unit];
249 drive->media = ide_disk;
250 drive->select.all = (unit<<4)|0xa0;
251 drive->hwif = hwif;
252 drive->ctl = 0x08;
253 drive->ready_stat = READY_STAT;
254 drive->bad_wstat = BAD_W_STAT;
255 drive->special.b.recalibrate = 1;
256 drive->special.b.set_geometry = 1;
257 drive->name[0] = 'h';
258 drive->name[1] = 'd';
259 drive->name[2] = 'a' + (index * MAX_DRIVES) + unit;
260 init_waitqueue_head(&drive->wqueue);
265 * init_ide_data() sets reasonable default values into all fields
266 * of all instances of the hwifs and drives, but only on the first call.
267 * Subsequent calls have no effect (they don't wipe out anything).
269 * This routine is normally called at driver initialization time,
270 * but may also be called MUCH earlier during kernel "command-line"
271 * parameter processing. As such, we cannot depend on any other parts
272 * of the kernel (such as memory allocation) to be functioning yet.
274 * This is too bad, as otherwise we could dynamically allocate the
275 * ide_drive_t structs as needed, rather than always consuming memory
276 * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
278 #define MAGIC_COOKIE 0x12345678
279 static void init_ide_data (void)
281 unsigned int index;
282 static unsigned long magic_cookie = MAGIC_COOKIE;
284 if (magic_cookie != MAGIC_COOKIE)
285 return; /* already initialized */
286 magic_cookie = 0;
288 /* Initialise all interface structures */
289 for (index = 0; index < MAX_HWIFS; ++index)
290 init_hwif_data(index);
292 /* Add default hw interfaces */
293 ide_init_default_hwifs();
295 idebus_parameter = 0;
296 system_bus_speed = 0;
300 * CompactFlash cards and their brethern pretend to be removable hard disks, except:
301 * (1) they never have a slave unit, and
302 * (2) they don't have doorlock mechanisms.
303 * This test catches them, and is invoked elsewhere when setting appropriate config bits.
305 * FIXME: This treatment is probably applicable for *all* PCMCIA (PC CARD) devices,
306 * so in linux 2.3.x we should change this to just treat all PCMCIA drives this way,
307 * and get rid of the model-name tests below (too big of an interface change for 2.2.x).
308 * At that time, we might also consider parameterizing the timeouts and retries,
309 * since these are MUCH faster than mechanical drives. -M.Lord
311 int drive_is_flashcard (ide_drive_t *drive)
313 struct hd_driveid *id = drive->id;
315 if (drive->removable && id != NULL) {
316 if (id->config == 0x848a) return 1; /* CompactFlash */
317 if (!strncmp(id->model, "KODAK ATA_FLASH", 15) /* Kodak */
318 || !strncmp(id->model, "Hitachi CV", 10) /* Hitachi */
319 || !strncmp(id->model, "SunDisk SDCFB", 13) /* SunDisk */
320 || !strncmp(id->model, "HAGIWARA HPC", 12) /* Hagiwara */
321 || !strncmp(id->model, "ATA_FLASH", 9)) /* Simple Tech */
323 return 1; /* yes, it is a flash memory card */
326 return 0; /* no, it is not a flash memory card */
330 * ide_system_bus_speed() returns what we think is the system VESA/PCI
331 * bus speed (in MHz). This is used for calculating interface PIO timings.
332 * The default is 40 for known PCI systems, 50 otherwise.
333 * The "idebus=xx" parameter can be used to override this value.
334 * The actual value to be used is computed/displayed the first time through.
336 int ide_system_bus_speed (void)
338 if (!system_bus_speed) {
339 if (idebus_parameter)
340 system_bus_speed = idebus_parameter; /* user supplied value */
341 #ifdef CONFIG_PCI
342 else if (pci_present())
343 system_bus_speed = 40; /* safe default value for PCI */
344 #endif /* CONFIG_PCI */
345 else
346 system_bus_speed = 50; /* safe default value for VESA and PCI */
347 printk("ide: Assuming %dMHz system bus speed for PIO modes%s\n", system_bus_speed,
348 idebus_parameter ? "" : "; override with idebus=xx");
350 return system_bus_speed;
353 #if SUPPORT_VLB_SYNC
355 * Some localbus EIDE interfaces require a special access sequence
356 * when using 32-bit I/O instructions to transfer data. We call this
357 * the "vlb_sync" sequence, which consists of three successive reads
358 * of the sector count register location, with interrupts disabled
359 * to ensure that the reads all happen together.
361 static inline void do_vlb_sync (ide_ioreg_t port) {
362 (void) inb (port);
363 (void) inb (port);
364 (void) inb (port);
366 #endif /* SUPPORT_VLB_SYNC */
369 * This is used for most PIO data transfers *from* the IDE interface
371 void ide_input_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
373 byte io_32bit = drive->io_32bit;
375 if (io_32bit) {
376 #if SUPPORT_VLB_SYNC
377 if (io_32bit & 2) {
378 unsigned long flags;
379 __save_flags(flags); /* local CPU only */
380 __cli(); /* local CPU only */
381 do_vlb_sync(IDE_NSECTOR_REG);
382 insl(IDE_DATA_REG, buffer, wcount);
383 __restore_flags(flags); /* local CPU only */
384 } else
385 #endif /* SUPPORT_VLB_SYNC */
386 insl(IDE_DATA_REG, buffer, wcount);
387 } else {
388 #if SUPPORT_SLOW_DATA_PORTS
389 if (drive->slow) {
390 unsigned short *ptr = (unsigned short *) buffer;
391 while (wcount--) {
392 *ptr++ = inw_p(IDE_DATA_REG);
393 *ptr++ = inw_p(IDE_DATA_REG);
395 } else
396 #endif /* SUPPORT_SLOW_DATA_PORTS */
397 insw(IDE_DATA_REG, buffer, wcount<<1);
402 * This is used for most PIO data transfers *to* the IDE interface
404 void ide_output_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
406 byte io_32bit = drive->io_32bit;
408 if (io_32bit) {
409 #if SUPPORT_VLB_SYNC
410 if (io_32bit & 2) {
411 unsigned long flags;
412 __save_flags(flags); /* local CPU only */
413 __cli(); /* local CPU only */
414 do_vlb_sync(IDE_NSECTOR_REG);
415 outsl(IDE_DATA_REG, buffer, wcount);
416 __restore_flags(flags); /* local CPU only */
417 } else
418 #endif /* SUPPORT_VLB_SYNC */
419 outsl(IDE_DATA_REG, buffer, wcount);
420 } else {
421 #if SUPPORT_SLOW_DATA_PORTS
422 if (drive->slow) {
423 unsigned short *ptr = (unsigned short *) buffer;
424 while (wcount--) {
425 outw_p(*ptr++, IDE_DATA_REG);
426 outw_p(*ptr++, IDE_DATA_REG);
428 } else
429 #endif /* SUPPORT_SLOW_DATA_PORTS */
430 outsw(IDE_DATA_REG, buffer, wcount<<1);
435 * The following routines are mainly used by the ATAPI drivers.
437 * These routines will round up any request for an odd number of bytes,
438 * so if an odd bytecount is specified, be sure that there's at least one
439 * extra byte allocated for the buffer.
441 void atapi_input_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount)
443 ++bytecount;
444 #if defined(CONFIG_ATARI) || defined(CONFIG_Q40)
445 if (MACH_IS_ATARI || MACH_IS_Q40) {
446 /* Atari has a byte-swapped IDE interface */
447 insw_swapw(IDE_DATA_REG, buffer, bytecount / 2);
448 return;
450 #endif /* CONFIG_ATARI */
451 ide_input_data (drive, buffer, bytecount / 4);
452 if ((bytecount & 0x03) >= 2)
453 insw (IDE_DATA_REG, ((byte *)buffer) + (bytecount & ~0x03), 1);
456 void atapi_output_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount)
458 ++bytecount;
459 #if defined(CONFIG_ATARI) || defined(CONFIG_Q40)
460 if (MACH_IS_ATARI || MACH_IS_Q40) {
461 /* Atari has a byte-swapped IDE interface */
462 outsw_swapw(IDE_DATA_REG, buffer, bytecount / 2);
463 return;
465 #endif /* CONFIG_ATARI */
466 ide_output_data (drive, buffer, bytecount / 4);
467 if ((bytecount & 0x03) >= 2)
468 outsw (IDE_DATA_REG, ((byte *)buffer) + (bytecount & ~0x03), 1);
472 * Needed for PCI irq sharing
474 static inline int drive_is_ready (ide_drive_t *drive)
476 if (drive->waiting_for_dma)
477 return HWIF(drive)->dmaproc(ide_dma_test_irq, drive);
478 #if 0
479 udelay(1); /* need to guarantee 400ns since last command was issued */
480 #endif
481 if (GET_STAT() & BUSY_STAT) /* Note: this may clear a pending IRQ!! */
482 return 0; /* drive busy: definitely not interrupting */
483 return 1; /* drive ready: *might* be interrupting */
487 * This is our end_request replacement function.
489 void ide_end_request (byte uptodate, ide_hwgroup_t *hwgroup)
491 struct request *rq;
492 unsigned long flags;
494 spin_lock_irqsave(&io_request_lock, flags);
495 rq = hwgroup->rq;
497 if (!end_that_request_first(rq, uptodate, hwgroup->drive->name)) {
498 add_blkdev_randomness(MAJOR(rq->rq_dev));
499 hwgroup->drive->queue.current_request = rq->next;
500 blk_dev[MAJOR(rq->rq_dev)].request_queue.current_request = NULL;
501 hwgroup->rq = NULL;
502 end_that_request_last(rq);
504 spin_unlock_irqrestore(&io_request_lock, flags);
508 * This should get invoked any time we exit the driver to
509 * wait for an interrupt response from a drive. handler() points
510 * at the appropriate code to handle the next interrupt, and a
511 * timer is started to prevent us from waiting forever in case
512 * something goes wrong (see the ide_timer_expiry() handler later on).
514 void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
515 unsigned int timeout, ide_expiry_t *expiry)
517 unsigned long flags;
518 ide_hwgroup_t *hwgroup = HWGROUP(drive);
520 spin_lock_irqsave(&io_request_lock, flags);
521 if (hwgroup->handler != NULL) {
522 printk("%s: ide_set_handler: handler not null; old=%p, new=%p\n",
523 drive->name, hwgroup->handler, handler);
525 hwgroup->handler = handler;
526 hwgroup->expiry = expiry;
527 hwgroup->timer.expires = jiffies + timeout;
528 add_timer(&hwgroup->timer);
529 spin_unlock_irqrestore(&io_request_lock, flags);
533 * current_capacity() returns the capacity (in sectors) of a drive
534 * according to its current geometry/LBA settings.
536 unsigned long current_capacity (ide_drive_t *drive)
538 if (!drive->present)
539 return 0;
540 if (drive->driver != NULL)
541 return DRIVER(drive)->capacity(drive);
542 return 0;
546 * ide_geninit() is called exactly *once* for each major, from genhd.c,
547 * at the beginning of the initial partition check for the drives.
549 void ide_geninit (struct gendisk *gd)
551 unsigned int unit;
552 ide_hwif_t *hwif = gd->real_devices;
554 for (unit = 0; unit < gd->nr_real; ++unit) {
555 ide_drive_t *drive = &hwif->drives[unit];
557 drive->part[0].nr_sects = current_capacity(drive);
558 if (!drive->present || (drive->media != ide_disk && drive->media != ide_floppy) ||
559 drive->driver == NULL || !drive->part[0].nr_sects)
560 drive->part[0].start_sect = -1; /* skip partition check */
564 static ide_startstop_t do_reset1 (ide_drive_t *, int); /* needed below */
567 * atapi_reset_pollfunc() gets invoked to poll the interface for completion every 50ms
568 * during an atapi drive reset operation. If the drive has not yet responded,
569 * and we have not yet hit our maximum waiting time, then the timer is restarted
570 * for another 50ms.
572 static ide_startstop_t atapi_reset_pollfunc (ide_drive_t *drive)
574 ide_hwgroup_t *hwgroup = HWGROUP(drive);
575 byte stat;
577 SELECT_DRIVE(HWIF(drive),drive);
578 udelay (10);
580 if (OK_STAT(stat=GET_STAT(), 0, BUSY_STAT)) {
581 printk("%s: ATAPI reset complete\n", drive->name);
582 } else {
583 if (0 < (signed long)(hwgroup->poll_timeout - jiffies)) {
584 ide_set_handler (drive, &atapi_reset_pollfunc, HZ/20, NULL);
585 return ide_started; /* continue polling */
587 hwgroup->poll_timeout = 0; /* end of polling */
588 printk("%s: ATAPI reset timed-out, status=0x%02x\n", drive->name, stat);
589 return do_reset1 (drive, 1); /* do it the old fashioned way */
591 hwgroup->poll_timeout = 0; /* done polling */
592 return ide_stopped;
596 * reset_pollfunc() gets invoked to poll the interface for completion every 50ms
597 * during an ide reset operation. If the drives have not yet responded,
598 * and we have not yet hit our maximum waiting time, then the timer is restarted
599 * for another 50ms.
601 static ide_startstop_t reset_pollfunc (ide_drive_t *drive)
603 ide_hwgroup_t *hwgroup = HWGROUP(drive);
604 ide_hwif_t *hwif = HWIF(drive);
605 byte tmp;
607 if (!OK_STAT(tmp=GET_STAT(), 0, BUSY_STAT)) {
608 if (0 < (signed long)(hwgroup->poll_timeout - jiffies)) {
609 ide_set_handler (drive, &reset_pollfunc, HZ/20, NULL);
610 return ide_started; /* continue polling */
612 printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
613 } else {
614 printk("%s: reset: ", hwif->name);
615 if ((tmp = GET_ERR()) == 1)
616 printk("success\n");
617 else {
618 #if FANCY_STATUS_DUMPS
619 printk("master: ");
620 switch (tmp & 0x7f) {
621 case 1: printk("passed");
622 break;
623 case 2: printk("formatter device error");
624 break;
625 case 3: printk("sector buffer error");
626 break;
627 case 4: printk("ECC circuitry error");
628 break;
629 case 5: printk("controlling MPU error");
630 break;
631 default:printk("error (0x%02x?)", tmp);
633 if (tmp & 0x80)
634 printk("; slave: failed");
635 printk("\n");
636 #else
637 printk("failed\n");
638 #endif /* FANCY_STATUS_DUMPS */
641 hwgroup->poll_timeout = 0; /* done polling */
642 return ide_stopped;
645 static void pre_reset (ide_drive_t *drive)
647 if (!drive->keep_settings) {
648 drive->unmask = 0;
649 drive->io_32bit = 0;
650 if (drive->using_dma)
651 (void) HWIF(drive)->dmaproc(ide_dma_off, drive);
653 if (drive->driver != NULL)
654 DRIVER(drive)->pre_reset(drive);
658 * do_reset1() attempts to recover a confused drive by resetting it.
659 * Unfortunately, resetting a disk drive actually resets all devices on
660 * the same interface, so it can really be thought of as resetting the
661 * interface rather than resetting the drive.
663 * ATAPI devices have their own reset mechanism which allows them to be
664 * individually reset without clobbering other devices on the same interface.
666 * Unfortunately, the IDE interface does not generate an interrupt to let
667 * us know when the reset operation has finished, so we must poll for this.
668 * Equally poor, though, is the fact that this may a very long time to complete,
669 * (up to 30 seconds worstcase). So, instead of busy-waiting here for it,
670 * we set a timer to poll at 50ms intervals.
672 static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
674 unsigned int unit;
675 unsigned long flags;
676 ide_hwif_t *hwif = HWIF(drive);
677 ide_hwgroup_t *hwgroup = HWGROUP(drive);
679 __save_flags(flags); /* local CPU only */
680 __cli(); /* local CPU only */
682 /* For an ATAPI device, first try an ATAPI SRST. */
683 if (drive->media != ide_disk && !do_not_try_atapi) {
684 pre_reset(drive);
685 SELECT_DRIVE(hwif,drive);
686 udelay (20);
687 OUT_BYTE (WIN_SRST, IDE_COMMAND_REG);
688 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
689 ide_set_handler (drive, &atapi_reset_pollfunc, HZ/20, NULL);
690 __restore_flags (flags); /* local CPU only */
691 return ide_started;
695 * First, reset any device state data we were maintaining
696 * for any of the drives on this interface.
698 for (unit = 0; unit < MAX_DRIVES; ++unit)
699 pre_reset(&hwif->drives[unit]);
701 #if OK_TO_RESET_CONTROLLER
702 if (!IDE_CONTROL_REG) {
703 __restore_flags(flags);
704 return ide_stopped;
707 * Note that we also set nIEN while resetting the device,
708 * to mask unwanted interrupts from the interface during the reset.
709 * However, due to the design of PC hardware, this will cause an
710 * immediate interrupt due to the edge transition it produces.
711 * This single interrupt gives us a "fast poll" for drives that
712 * recover from reset very quickly, saving us the first 50ms wait time.
714 OUT_BYTE(drive->ctl|6,IDE_CONTROL_REG); /* set SRST and nIEN */
715 udelay(10); /* more than enough time */
716 OUT_BYTE(drive->ctl|2,IDE_CONTROL_REG); /* clear SRST, leave nIEN */
717 udelay(10); /* more than enough time */
718 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
719 ide_set_handler (drive, &reset_pollfunc, HZ/20, NULL);
722 * Some weird controller like resetting themselves to a strange
723 * state when the disks are reset this way. At least, the Winbond
724 * 553 documentation says that
726 if (hwif->resetproc != NULL)
727 hwif->resetproc(drive);
729 #endif /* OK_TO_RESET_CONTROLLER */
731 __restore_flags (flags); /* local CPU only */
732 return ide_started;
736 * ide_do_reset() is the entry point to the drive/interface reset code.
738 ide_startstop_t ide_do_reset (ide_drive_t *drive)
740 return do_reset1 (drive, 0);
744 * Clean up after success/failure of an explicit drive cmd
746 void ide_end_drive_cmd (ide_drive_t *drive, byte stat, byte err)
748 unsigned long flags;
749 struct request *rq = HWGROUP(drive)->rq;
751 if (rq->cmd == IDE_DRIVE_CMD) {
752 byte *args = (byte *) rq->buffer;
753 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
754 if (args) {
755 args[0] = stat;
756 args[1] = err;
757 args[2] = IN_BYTE(IDE_NSECTOR_REG);
760 spin_lock_irqsave(&io_request_lock, flags);
761 drive->queue.current_request = rq->next;
762 blk_dev[MAJOR(rq->rq_dev)].request_queue.current_request = NULL;
763 HWGROUP(drive)->rq = NULL;
764 rq->rq_status = RQ_INACTIVE;
765 spin_unlock_irqrestore(&io_request_lock, flags);
766 if (rq->sem != NULL)
767 up(rq->sem); /* inform originator that rq has been serviced */
771 * Error reporting, in human readable form (luxurious, but a memory hog).
773 byte ide_dump_status (ide_drive_t *drive, const char *msg, byte stat)
775 unsigned long flags;
776 byte err = 0;
778 __save_flags (flags); /* local CPU only */
779 ide__sti(); /* local CPU only */
780 printk("%s: %s: status=0x%02x", drive->name, msg, stat);
781 #if FANCY_STATUS_DUMPS
782 printk(" { ");
783 if (stat & BUSY_STAT)
784 printk("Busy ");
785 else {
786 if (stat & READY_STAT) printk("DriveReady ");
787 if (stat & WRERR_STAT) printk("DeviceFault ");
788 if (stat & SEEK_STAT) printk("SeekComplete ");
789 if (stat & DRQ_STAT) printk("DataRequest ");
790 if (stat & ECC_STAT) printk("CorrectedError ");
791 if (stat & INDEX_STAT) printk("Index ");
792 if (stat & ERR_STAT) printk("Error ");
794 printk("}");
795 #endif /* FANCY_STATUS_DUMPS */
796 printk("\n");
797 if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
798 err = GET_ERR();
799 printk("%s: %s: error=0x%02x", drive->name, msg, err);
800 #if FANCY_STATUS_DUMPS
801 if (drive->media == ide_disk) {
802 printk(" { ");
803 if (err & ABRT_ERR) printk("DriveStatusError ");
804 if (err & ICRC_ERR) printk((err & ABRT_ERR) ? "BadCRC " : "BadSector ");
805 if (err & ECC_ERR) printk("UncorrectableError ");
806 if (err & ID_ERR) printk("SectorIdNotFound ");
807 if (err & TRK0_ERR) printk("TrackZeroNotFound ");
808 if (err & MARK_ERR) printk("AddrMarkNotFound ");
809 printk("}");
810 if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR || (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
811 byte cur = IN_BYTE(IDE_SELECT_REG);
812 if (cur & 0x40) { /* using LBA? */
813 printk(", LBAsect=%ld", (unsigned long)
814 ((cur&0xf)<<24)
815 |(IN_BYTE(IDE_HCYL_REG)<<16)
816 |(IN_BYTE(IDE_LCYL_REG)<<8)
817 | IN_BYTE(IDE_SECTOR_REG));
818 } else {
819 printk(", CHS=%d/%d/%d",
820 (IN_BYTE(IDE_HCYL_REG)<<8) +
821 IN_BYTE(IDE_LCYL_REG),
822 cur & 0xf,
823 IN_BYTE(IDE_SECTOR_REG));
825 if (HWGROUP(drive)->rq)
826 printk(", sector=%ld", HWGROUP(drive)->rq->sector);
829 #endif /* FANCY_STATUS_DUMPS */
830 printk("\n");
832 __restore_flags (flags); /* local CPU only */
833 return err;
837 * try_to_flush_leftover_data() is invoked in response to a drive
838 * unexpectedly having its DRQ_STAT bit set. As an alternative to
839 * resetting the drive, this routine tries to clear the condition
840 * by read a sector's worth of data from the drive. Of course,
841 * this may not help if the drive is *waiting* for data from *us*.
843 static void try_to_flush_leftover_data (ide_drive_t *drive)
845 int i = (drive->mult_count ? drive->mult_count : 1) * SECTOR_WORDS;
847 if (drive->media != ide_disk)
848 return;
849 while (i > 0) {
850 unsigned long buffer[16];
851 unsigned int wcount = (i > 16) ? 16 : i;
852 i -= wcount;
853 ide_input_data (drive, buffer, wcount);
858 * ide_error() takes action based on the error returned by the drive.
860 ide_startstop_t ide_error (ide_drive_t *drive, const char *msg, byte stat)
862 struct request *rq;
863 byte err;
865 err = ide_dump_status(drive, msg, stat);
866 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
867 return ide_stopped;
868 /* retry only "normal" I/O: */
869 if (rq->cmd == IDE_DRIVE_CMD) {
870 rq->errors = 1;
871 ide_end_drive_cmd(drive, stat, err);
872 return ide_stopped;
874 if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) { /* other bits are useless when BUSY */
875 rq->errors |= ERROR_RESET;
876 } else {
877 if (drive->media == ide_disk && (stat & ERR_STAT)) {
878 /* err has different meaning on cdrom and tape */
879 if (err == ABRT_ERR) {
880 if (drive->select.b.lba && IN_BYTE(IDE_COMMAND_REG) == WIN_SPECIFY)
881 return ide_stopped; /* some newer drives don't support WIN_SPECIFY */
882 } else if ((err & (ABRT_ERR | ICRC_ERR)) == (ABRT_ERR | ICRC_ERR))
883 ; /* UDMA crc error -- just retry the operation */
884 else if (err & (BBD_ERR | ECC_ERR)) /* retries won't help these */
885 rq->errors = ERROR_MAX;
886 else if (err & TRK0_ERR) /* help it find track zero */
887 rq->errors |= ERROR_RECAL;
889 if ((stat & DRQ_STAT) && rq->cmd != WRITE)
890 try_to_flush_leftover_data(drive);
892 if (GET_STAT() & (BUSY_STAT|DRQ_STAT))
893 rq->errors |= ERROR_RESET; /* Mmmm.. timing problem */
895 if (rq->errors >= ERROR_MAX) {
896 if (drive->driver != NULL)
897 DRIVER(drive)->end_request(0, HWGROUP(drive));
898 else
899 ide_end_request(0, HWGROUP(drive));
900 } else {
901 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
902 ++rq->errors;
903 return ide_do_reset(drive);
905 if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
906 drive->special.b.recalibrate = 1;
907 ++rq->errors;
909 return ide_stopped;
913 * Issue a simple drive command
914 * The drive must be selected beforehand.
916 void ide_cmd (ide_drive_t *drive, byte cmd, byte nsect, ide_handler_t *handler)
918 ide_set_handler (drive, handler, WAIT_CMD, NULL);
919 if (IDE_CONTROL_REG)
920 OUT_BYTE(drive->ctl,IDE_CONTROL_REG); /* clear nIEN */
921 OUT_BYTE(nsect,IDE_NSECTOR_REG);
922 OUT_BYTE(cmd,IDE_COMMAND_REG);
926 * drive_cmd_intr() is invoked on completion of a special DRIVE_CMD.
928 static ide_startstop_t drive_cmd_intr (ide_drive_t *drive)
930 struct request *rq = HWGROUP(drive)->rq;
931 byte *args = (byte *) rq->buffer;
932 byte stat = GET_STAT();
933 int retries = 10;
935 ide__sti(); /* local CPU only */
936 if ((stat & DRQ_STAT) && args && args[3]) {
937 byte io_32bit = drive->io_32bit;
938 drive->io_32bit = 0;
939 ide_input_data(drive, &args[4], args[3] * SECTOR_WORDS);
940 drive->io_32bit = io_32bit;
941 while (((stat = GET_STAT()) & BUSY_STAT) && retries--)
942 udelay(100);
945 if (!OK_STAT(stat, READY_STAT, BAD_STAT))
946 return ide_error(drive, "drive_cmd", stat); /* calls ide_end_drive_cmd */
947 ide_end_drive_cmd (drive, stat, GET_ERR());
948 return ide_stopped;
952 * do_special() is used to issue WIN_SPECIFY, WIN_RESTORE, and WIN_SETMULT
953 * commands to a drive. It used to do much more, but has been scaled back.
955 static ide_startstop_t do_special (ide_drive_t *drive)
957 special_t *s = &drive->special;
959 #ifdef DEBUG
960 printk("%s: do_special: 0x%02x\n", drive->name, s->all);
961 #endif
962 if (s->b.set_tune) {
963 ide_tuneproc_t *tuneproc = HWIF(drive)->tuneproc;
964 s->b.set_tune = 0;
965 if (tuneproc != NULL)
966 tuneproc(drive, drive->tune_req);
967 } else if (drive->driver != NULL) {
968 return DRIVER(drive)->special(drive);
969 } else if (s->all) {
970 printk("%s: bad special flag: 0x%02x\n", drive->name, s->all);
971 s->all = 0;
973 return ide_stopped;
977 * This routine busy-waits for the drive status to be not "busy".
978 * It then checks the status for all of the "good" bits and none
979 * of the "bad" bits, and if all is okay it returns 0. All other
980 * cases return 1 after invoking ide_error() -- caller should just return.
982 * This routine should get fixed to not hog the cpu during extra long waits..
983 * That could be done by busy-waiting for the first jiffy or two, and then
984 * setting a timer to wake up at half second intervals thereafter,
985 * until timeout is achieved, before timing out.
987 int ide_wait_stat (ide_startstop_t *startstop, ide_drive_t *drive, byte good, byte bad, unsigned long timeout) {
988 byte stat;
989 int i;
990 unsigned long flags;
992 udelay(1); /* spec allows drive 400ns to assert "BUSY" */
993 if ((stat = GET_STAT()) & BUSY_STAT) {
994 __save_flags(flags); /* local CPU only */
995 ide__sti(); /* local CPU only */
996 timeout += jiffies;
997 while ((stat = GET_STAT()) & BUSY_STAT) {
998 if (0 < (signed long)(jiffies - timeout)) {
999 __restore_flags(flags); /* local CPU only */
1000 *startstop = ide_error(drive, "status timeout", stat);
1001 return 1;
1004 __restore_flags(flags); /* local CPU only */
1007 * Allow status to settle, then read it again.
1008 * A few rare drives vastly violate the 400ns spec here,
1009 * so we'll wait up to 10usec for a "good" status
1010 * rather than expensively fail things immediately.
1011 * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
1013 for (i = 0; i < 10; i++) {
1014 udelay(1);
1015 if (OK_STAT((stat = GET_STAT()), good, bad))
1016 return 0;
1018 *startstop = ide_error(drive, "status error", stat);
1019 return 1;
1023 * execute_drive_cmd() issues a special drive command,
1024 * usually initiated by ioctl() from the external hdparm program.
1026 static ide_startstop_t execute_drive_cmd (ide_drive_t *drive, struct request *rq)
1028 byte *args = rq->buffer;
1029 if (args) {
1030 #ifdef DEBUG
1031 printk("%s: DRIVE_CMD cmd=0x%02x sc=0x%02x fr=0x%02x xx=0x%02x\n",
1032 drive->name, args[0], args[1], args[2], args[3]);
1033 #endif
1034 if (args[0] == WIN_SMART) {
1035 OUT_BYTE(0x4f, IDE_LCYL_REG);
1036 OUT_BYTE(0xc2, IDE_HCYL_REG);
1037 OUT_BYTE(args[2],IDE_FEATURE_REG);
1038 OUT_BYTE(args[1],IDE_SECTOR_REG);
1039 ide_cmd(drive, args[0], args[3], &drive_cmd_intr);
1040 return ide_started;
1042 OUT_BYTE(args[2],IDE_FEATURE_REG);
1043 ide_cmd(drive, args[0], args[1], &drive_cmd_intr);
1044 return ide_started;
1045 } else {
1047 * NULL is actually a valid way of waiting for
1048 * all current requests to be flushed from the queue.
1050 #ifdef DEBUG
1051 printk("%s: DRIVE_CMD (null)\n", drive->name);
1052 #endif
1053 ide_end_drive_cmd(drive, GET_STAT(), GET_ERR());
1054 return ide_stopped;
1059 * start_request() initiates handling of a new I/O request
1061 static ide_startstop_t start_request (ide_drive_t *drive)
1063 ide_startstop_t startstop;
1064 unsigned long block, blockend;
1065 struct request *rq = drive->queue.current_request;
1066 unsigned int minor = MINOR(rq->rq_dev), unit = minor >> PARTN_BITS;
1067 ide_hwif_t *hwif = HWIF(drive);
1069 #ifdef DEBUG
1070 printk("%s: start_request: current=0x%08lx\n", hwif->name, (unsigned long) rq);
1071 #endif
1072 if (unit >= MAX_DRIVES) {
1073 printk("%s: bad device number: %s\n", hwif->name, kdevname(rq->rq_dev));
1074 goto kill_rq;
1076 #ifdef DEBUG
1077 if (rq->bh && !buffer_locked(rq->bh)) {
1078 printk("%s: block not locked\n", drive->name);
1079 goto kill_rq;
1081 #endif
1082 block = rq->sector;
1083 blockend = block + rq->nr_sectors;
1084 if ((blockend < block) || (blockend > drive->part[minor&PARTN_MASK].nr_sects)) {
1085 printk("%s%c: bad access: block=%ld, count=%ld\n", drive->name,
1086 (minor&PARTN_MASK)?'0'+(minor&PARTN_MASK):' ', block, rq->nr_sectors);
1087 goto kill_rq;
1089 block += drive->part[minor&PARTN_MASK].start_sect + drive->sect0;
1091 /* Yecch - this will shift the entire interval,
1092 possibly killing some innocent following sector */
1093 if (block == 0 && drive->remap_0_to_1 == 1)
1094 block = 1; /* redirect MBR access to EZ-Drive partn table */
1096 #if (DISK_RECOVERY_TIME > 0)
1097 while ((read_timer() - hwif->last_time) < DISK_RECOVERY_TIME);
1098 #endif
1100 SELECT_DRIVE(hwif, drive);
1101 if (ide_wait_stat(&startstop, drive, drive->ready_stat, BUSY_STAT|DRQ_STAT, WAIT_READY)) {
1102 printk("%s: drive not ready for command\n", drive->name);
1103 return startstop;
1105 if (!drive->special.all) {
1106 if (rq->cmd == IDE_DRIVE_CMD) {
1107 return execute_drive_cmd(drive, rq);
1109 if (drive->driver != NULL) {
1110 return (DRIVER(drive)->do_request(drive, rq, block));
1112 printk("%s: media type %d not supported\n", drive->name, drive->media);
1113 goto kill_rq;
1115 return do_special(drive);
1116 kill_rq:
1117 if (drive->driver != NULL)
1118 DRIVER(drive)->end_request(0, HWGROUP(drive));
1119 else
1120 ide_end_request(0, HWGROUP(drive));
1121 return ide_stopped;
1125 * ide_stall_queue() can be used by a drive to give excess bandwidth back
1126 * to the hwgroup by sleeping for timeout jiffies.
1128 void ide_stall_queue (ide_drive_t *drive, unsigned long timeout)
1130 if (timeout > WAIT_WORSTCASE)
1131 timeout = WAIT_WORSTCASE;
1132 drive->sleep = timeout + jiffies;
1135 #define WAKEUP(drive) ((drive)->service_start + 2 * (drive)->service_time)
1138 * choose_drive() selects the next drive which will be serviced.
1140 static inline ide_drive_t *choose_drive (ide_hwgroup_t *hwgroup)
1142 ide_drive_t *drive, *best;
1144 repeat:
1145 best = NULL;
1146 drive = hwgroup->drive;
1147 do {
1148 if (drive->queue.current_request && (!drive->sleep || 0 <= (signed long)(jiffies - drive->sleep))) {
1149 if (!best
1150 || (drive->sleep && (!best->sleep || 0 < (signed long)(best->sleep - drive->sleep)))
1151 || (!best->sleep && 0 < (signed long)(WAKEUP(best) - WAKEUP(drive))))
1153 struct blk_dev_struct *bdev = &blk_dev[HWIF(drive)->major];
1154 if( !bdev->request_queue.plugged )
1155 best = drive;
1158 } while ((drive = drive->next) != hwgroup->drive);
1159 if (best && best->nice1 && !best->sleep && best != hwgroup->drive && best->service_time > WAIT_MIN_SLEEP) {
1160 long t = (signed long)(WAKEUP(best) - jiffies);
1161 if (t >= WAIT_MIN_SLEEP) {
1163 * We *may* have some time to spare, but first let's see if
1164 * someone can potentially benefit from our nice mood today..
1166 drive = best->next;
1167 do {
1168 if (!drive->sleep
1169 && 0 < (signed long)(WAKEUP(drive) - (jiffies - best->service_time))
1170 && 0 < (signed long)((jiffies + t) - WAKEUP(drive)))
1172 ide_stall_queue(best, IDE_MIN(t, 10 * WAIT_MIN_SLEEP));
1173 goto repeat;
1175 } while ((drive = drive->next) != best);
1178 return best;
1182 * Issue a new request to a drive from hwgroup
1183 * Caller must have already done spin_lock_irqsave(&io_request_lock, ..);
1185 * A hwgroup is a serialized group of IDE interfaces. Usually there is
1186 * exactly one hwif (interface) per hwgroup, but buggy controllers (eg. CMD640)
1187 * may have both interfaces in a single hwgroup to "serialize" access.
1188 * Or possibly multiple ISA interfaces can share a common IRQ by being grouped
1189 * together into one hwgroup for serialized access.
1191 * Note also that several hwgroups can end up sharing a single IRQ,
1192 * possibly along with many other devices. This is especially common in
1193 * PCI-based systems with off-board IDE controller cards.
1195 * The IDE driver uses the single global io_request_lock spinlock to protect
1196 * access to the request queues, and to protect the hwgroup->busy flag.
1198 * The first thread into the driver for a particular hwgroup sets the
1199 * hwgroup->busy flag to indicate that this hwgroup is now active,
1200 * and then initiates processing of the top request from the request queue.
1202 * Other threads attempting entry notice the busy setting, and will simply
1203 * queue their new requests and exit immediately. Note that hwgroup->busy
1204 * remains set even when the driver is merely awaiting the next interrupt.
1205 * Thus, the meaning is "this hwgroup is busy processing a request".
1207 * When processing of a request completes, the completing thread or IRQ-handler
1208 * will start the next request from the queue. If no more work remains,
1209 * the driver will clear the hwgroup->busy flag and exit.
1211 * The io_request_lock (spinlock) is used to protect all access to the
1212 * hwgroup->busy flag, but is otherwise not needed for most processing in
1213 * the driver. This makes the driver much more friendlier to shared IRQs
1214 * than previous designs, while remaining 100% (?) SMP safe and capable.
1216 static void ide_do_request (ide_hwgroup_t *hwgroup)
1218 struct blk_dev_struct *bdev;
1219 ide_drive_t *drive;
1220 ide_hwif_t *hwif;
1221 ide_startstop_t startstop;
1223 ide_get_lock(&ide_lock, ide_intr, hwgroup); /* for atari only: POSSIBLY BROKEN HERE(?) */
1225 __cli(); /* necessary paranoia: ensure IRQs are masked on local CPU */
1227 while (!hwgroup->busy) {
1228 hwgroup->busy = 1;
1229 drive = choose_drive(hwgroup);
1230 if (drive == NULL) {
1231 unsigned long sleep = 0;
1232 hwgroup->rq = NULL;
1233 drive = hwgroup->drive;
1234 do {
1235 bdev = &blk_dev[HWIF(drive)->major];
1236 if( !bdev->request_queue.plugged )
1237 bdev->request_queue.current_request = NULL; /* (broken since patch-2.1.15) */
1238 if (drive->sleep && (!sleep || 0 < (signed long)(sleep - drive->sleep)))
1239 sleep = drive->sleep;
1240 } while ((drive = drive->next) != hwgroup->drive);
1241 if (sleep) {
1243 * Take a short snooze, and then wake up this hwgroup again.
1244 * This gives other hwgroups on the same a chance to
1245 * play fairly with us, just in case there are big differences
1246 * in relative throughputs.. don't want to hog the cpu too much.
1248 if (0 < (signed long)(jiffies + WAIT_MIN_SLEEP - sleep))
1249 sleep = jiffies + WAIT_MIN_SLEEP;
1250 #if 1
1251 if (hwgroup->timer.next || hwgroup->timer.prev)
1252 printk("ide_set_handler: timer already active\n");
1253 #endif
1254 hwgroup->sleeping = 1; /* so that ide_timer_expiry knows what to do */
1255 mod_timer(&hwgroup->timer, sleep);
1256 /* we purposely leave hwgroup->busy==1 while sleeping */
1257 } else {
1258 /* Ugly, but how can we sleep for the lock otherwise? perhaps from tq_scheduler? */
1259 ide_release_lock(&ide_lock); /* for atari only */
1260 hwgroup->busy = 0;
1262 return; /* no more work for this hwgroup (for now) */
1264 hwif = HWIF(drive);
1265 if (hwgroup->hwif->sharing_irq && hwif != hwgroup->hwif && hwif->io_ports[IDE_CONTROL_OFFSET]) {
1266 /* set nIEN for previous hwif */
1267 OUT_BYTE(hwgroup->drive->ctl|2, hwgroup->hwif->io_ports[IDE_CONTROL_OFFSET]);
1269 hwgroup->hwif = hwif;
1270 hwgroup->drive = drive;
1271 drive->sleep = 0;
1272 drive->service_start = jiffies;
1274 bdev = &blk_dev[hwif->major];
1275 if( bdev->request_queue.plugged ) /* FIXME: paranoia */
1276 printk("%s: Huh? nuking plugged queue\n", drive->name);
1277 bdev->request_queue.current_request = hwgroup->rq = drive->queue.current_request;
1278 spin_unlock(&io_request_lock);
1279 if (!hwif->serialized) /* play it safe with buggy hardware */
1280 ide__sti();
1281 startstop = start_request(drive);
1282 spin_lock_irq(&io_request_lock);
1283 if (startstop == ide_stopped)
1284 hwgroup->busy = 0;
1289 * ide_get_queue() returns the queue which corresponds to a given device.
1291 request_queue_t *ide_get_queue (kdev_t dev)
1293 ide_hwif_t *hwif = (ide_hwif_t *)blk_dev[MAJOR(dev)].data;
1295 return &hwif->drives[DEVICE_NR(dev) & 1].queue;
1298 void do_ide0_request (request_queue_t *q)
1300 ide_do_request (ide_hwifs[0].hwgroup);
1303 #if MAX_HWIFS > 1
1304 void do_ide1_request (request_queue_t *q)
1306 ide_do_request (ide_hwifs[1].hwgroup);
1308 #endif /* MAX_HWIFS > 1 */
1310 #if MAX_HWIFS > 2
1311 void do_ide2_request (request_queue_t *q)
1313 ide_do_request (ide_hwifs[2].hwgroup);
1315 #endif /* MAX_HWIFS > 2 */
1317 #if MAX_HWIFS > 3
1318 void do_ide3_request (request_queue_t *q)
1320 ide_do_request (ide_hwifs[3].hwgroup);
1322 #endif /* MAX_HWIFS > 3 */
1324 #if MAX_HWIFS > 4
1325 void do_ide4_request (request_queue_t *q)
1327 ide_do_request (ide_hwifs[4].hwgroup);
1329 #endif /* MAX_HWIFS > 4 */
1331 #if MAX_HWIFS > 5
1332 void do_ide5_request (request_queue_t *q)
1334 ide_do_request (ide_hwifs[5].hwgroup);
1336 #endif /* MAX_HWIFS > 5 */
1338 #if MAX_HWIFS > 6
1339 void do_ide6_request (request_queue_t *q)
1341 ide_do_request (ide_hwifs[6].hwgroup);
1343 #endif /* MAX_HWIFS > 6 */
1345 #if MAX_HWIFS > 7
1346 void do_ide7_request (request_queue_t *q)
1348 ide_do_request (ide_hwifs[7].hwgroup);
1350 #endif /* MAX_HWIFS > 7 */
1352 #if MAX_HWIFS > 8
1353 void do_ide8_request (request_queue_t *q)
1355 ide_do_request (ide_hwifs[8].hwgroup);
1357 #endif /* MAX_HWIFS > 8 */
1359 #if MAX_HWIFS > 9
1360 void do_ide9_request (request_queue_t *q)
1362 ide_do_request (ide_hwifs[9].hwgroup);
1364 #endif /* MAX_HWIFS > 9 */
1367 * ide_timer_expiry() is our timeout function for all drive operations.
1368 * But note that it can also be invoked as a result of a "sleep" operation
1369 * triggered by the mod_timer() call in ide_do_request.
1371 void ide_timer_expiry (unsigned long data)
1373 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *) data;
1374 ide_handler_t *handler;
1375 ide_expiry_t *expiry;
1376 unsigned long flags;
1377 unsigned long wait;
1379 spin_lock_irqsave(&io_request_lock, flags);
1380 del_timer(&hwgroup->timer);
1382 if ((handler = hwgroup->handler) == NULL) {
1384 * Either a marginal timeout occured
1385 * (got the interrupt just as timer expired),
1386 * or we were "sleeping" to give other devices a chance.
1387 * Either way, we don't really want to complain about anything.
1389 if (hwgroup->sleeping) {
1390 hwgroup->sleeping = 0;
1391 hwgroup->busy = 0;
1393 } else {
1394 ide_drive_t *drive = hwgroup->drive;
1395 hwgroup->handler = NULL;
1396 if (!drive) {
1397 printk("ide_timer_expiry: hwgroup->drive was NULL\n");
1398 } else {
1399 ide_hwif_t *hwif;
1400 ide_startstop_t startstop;
1401 if (!hwgroup->busy) {
1402 hwgroup->busy = 1; /* paranoia */
1403 printk("%s: ide_timer_expiry: hwgroup->busy was 0 ??\n", drive->name);
1405 if ((expiry = hwgroup->expiry) != NULL) {
1406 /* continue */
1407 if ((wait = expiry(drive)) != 0) {
1408 /* reset timer */
1409 hwgroup->timer.expires = jiffies + wait;
1410 add_timer(&hwgroup->timer);
1411 spin_unlock_irqrestore(&io_request_lock, flags);
1412 return;
1416 * We need to simulate a real interrupt when invoking
1417 * the handler() function, which means we need to globally
1418 * mask the specific IRQ:
1420 spin_unlock(&io_request_lock);
1421 hwif = HWIF(drive);
1422 disable_irq(hwif->irq);
1423 __cli(); /* local CPU only, as if we were handling an interrupt */
1424 if (hwgroup->poll_timeout != 0) {
1425 startstop = handler(drive);
1426 } else if (drive_is_ready(drive)) {
1427 if (drive->waiting_for_dma)
1428 (void) hwgroup->hwif->dmaproc(ide_dma_lostirq, drive);
1429 (void)ide_ack_intr(hwif);
1430 printk("%s: lost interrupt\n", drive->name);
1431 startstop = handler(drive);
1432 } else {
1433 if (drive->waiting_for_dma) {
1434 (void) hwgroup->hwif->dmaproc(ide_dma_end, drive);
1435 printk("%s: timeout waiting for DMA\n", drive->name);
1436 (void) hwgroup->hwif->dmaproc(ide_dma_timeout, drive);
1438 startstop = ide_error(drive, "irq timeout", GET_STAT());
1440 set_recovery_timer(hwif);
1441 drive->service_time = jiffies - drive->service_start;
1442 enable_irq(hwif->irq);
1443 spin_lock_irq(&io_request_lock);
1444 if (startstop == ide_stopped)
1445 hwgroup->busy = 0;
1448 ide_do_request(hwgroup);
1449 spin_unlock_irqrestore(&io_request_lock, flags);
1453 * There's nothing really useful we can do with an unexpected interrupt,
1454 * other than reading the status register (to clear it), and logging it.
1455 * There should be no way that an irq can happen before we're ready for it,
1456 * so we needn't worry much about losing an "important" interrupt here.
1458 * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
1459 * drive enters "idle", "standby", or "sleep" mode, so if the status looks
1460 * "good", we just ignore the interrupt completely.
1462 * This routine assumes __cli() is in effect when called.
1464 * If an unexpected interrupt happens on irq15 while we are handling irq14
1465 * and if the two interfaces are "serialized" (CMD640), then it looks like
1466 * we could screw up by interfering with a new request being set up for irq15.
1468 * In reality, this is a non-issue. The new command is not sent unless the
1469 * drive is ready to accept one, in which case we know the drive is not
1470 * trying to interrupt us. And ide_set_handler() is always invoked before
1471 * completing the issuance of any new drive command, so we will not be
1472 * accidently invoked as a result of any valid command completion interrupt.
1475 static void unexpected_intr (int irq, ide_hwgroup_t *hwgroup)
1477 byte stat;
1478 ide_hwif_t *hwif = hwgroup->hwif;
1481 * handle the unexpected interrupt
1483 do {
1484 if (hwif->irq == irq) {
1485 stat = IN_BYTE(hwif->io_ports[IDE_STATUS_OFFSET]);
1486 if (!OK_STAT(stat, READY_STAT, BAD_STAT)) {
1487 /* Try to not flood the console with msgs */
1488 static unsigned long last_msgtime = 0, count = 0;
1489 ++count;
1490 if (0 < (signed long)(jiffies - (last_msgtime + HZ))) {
1491 last_msgtime = jiffies;
1492 printk("%s%s: unexpected interrupt, status=0x%02x, count=%ld\n",
1493 hwif->name, (hwif->next == hwgroup->hwif) ? "" : "(?)", stat, count);
1497 } while ((hwif = hwif->next) != hwgroup->hwif);
1501 * entry point for all interrupts, caller does __cli() for us
1503 void ide_intr (int irq, void *dev_id, struct pt_regs *regs)
1505 unsigned long flags;
1506 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *)dev_id;
1507 ide_hwif_t *hwif;
1508 ide_drive_t *drive;
1509 ide_handler_t *handler;
1510 ide_startstop_t startstop;
1512 spin_lock_irqsave(&io_request_lock, flags);
1513 hwif = hwgroup->hwif;
1515 if (!ide_ack_intr(hwif)) {
1516 spin_unlock_irqrestore(&io_request_lock, flags);
1517 return;
1520 if ((handler = hwgroup->handler) == NULL || hwgroup->poll_timeout != 0) {
1522 * Not expecting an interrupt from this drive.
1523 * That means this could be:
1524 * (1) an interrupt from another PCI device
1525 * sharing the same PCI INT# as us.
1526 * or (2) a drive just entered sleep or standby mode,
1527 * and is interrupting to let us know.
1528 * or (3) a spurious interrupt of unknown origin.
1530 * For PCI, we cannot tell the difference,
1531 * so in that case we just ignore it and hope it goes away.
1533 #ifdef CONFIG_BLK_DEV_IDEPCI
1534 if (IDE_PCI_DEVID_EQ(hwif->pci_devid, IDE_PCI_DEVID_NULL))
1535 #endif /* CONFIG_BLK_DEV_IDEPCI */
1538 * Probably not a shared PCI interrupt,
1539 * so we can safely try to do something about it:
1541 unexpected_intr(irq, hwgroup);
1542 #ifdef CONFIG_BLK_DEV_IDEPCI
1543 } else {
1545 * Whack the status register, just in case we have a leftover pending IRQ.
1547 (void) IN_BYTE(hwif->io_ports[IDE_STATUS_OFFSET]);
1548 #endif /* CONFIG_BLK_DEV_IDEPCI */
1550 spin_unlock_irqrestore(&io_request_lock, flags);
1551 return;
1553 drive = hwgroup->drive;
1554 if (!drive) {
1556 * This should NEVER happen, and there isn't much we could do about it here.
1558 spin_unlock_irqrestore(&io_request_lock, flags);
1559 return;
1561 if (!drive_is_ready(drive)) {
1563 * This happens regularly when we share a PCI IRQ with another device.
1564 * Unfortunately, it can also happen with some buggy drives that trigger
1565 * the IRQ before their status register is up to date. Hopefully we have
1566 * enough advance overhead that the latter isn't a problem.
1568 spin_unlock_irqrestore(&io_request_lock, flags);
1569 return;
1571 if (!hwgroup->busy) {
1572 hwgroup->busy = 1; /* paranoia */
1573 printk("%s: ide_intr: hwgroup->busy was 0 ??\n", drive->name);
1575 hwgroup->handler = NULL;
1576 del_timer(&hwgroup->timer);
1577 spin_unlock(&io_request_lock);
1579 if (drive->unmask)
1580 ide__sti(); /* local CPU only */
1581 startstop = handler(drive); /* service this interrupt, may set handler for next interrupt */
1582 spin_lock_irq(&io_request_lock);
1585 * Note that handler() may have set things up for another
1586 * interrupt to occur soon, but it cannot happen until
1587 * we exit from this routine, because it will be the
1588 * same irq as is currently being serviced here, and Linux
1589 * won't allow another of the same (on any CPU) until we return.
1591 set_recovery_timer(HWIF(drive));
1592 drive->service_time = jiffies - drive->service_start;
1593 if (startstop == ide_stopped) {
1594 if (hwgroup->handler == NULL) { /* paranoia */
1595 hwgroup->busy = 0;
1596 ide_do_request(hwgroup);
1597 } else {
1598 printk("%s: ide_intr: huh? expected NULL handler on exit\n", drive->name);
1601 spin_unlock_irqrestore(&io_request_lock, flags);
1605 * get_info_ptr() returns the (ide_drive_t *) for a given device number.
1606 * It returns NULL if the given device number does not match any present drives.
1608 ide_drive_t *get_info_ptr (kdev_t i_rdev)
1610 int major = MAJOR(i_rdev);
1611 unsigned int h;
1613 for (h = 0; h < MAX_HWIFS; ++h) {
1614 ide_hwif_t *hwif = &ide_hwifs[h];
1615 if (hwif->present && major == hwif->major) {
1616 unsigned unit = DEVICE_NR(i_rdev);
1617 if (unit < MAX_DRIVES) {
1618 ide_drive_t *drive = &hwif->drives[unit];
1619 if (drive->present)
1620 return drive;
1622 break;
1625 return NULL;
1629 * This function is intended to be used prior to invoking ide_do_drive_cmd().
1631 void ide_init_drive_cmd (struct request *rq)
1633 rq->buffer = NULL;
1634 rq->cmd = IDE_DRIVE_CMD;
1635 rq->sector = 0;
1636 rq->nr_sectors = 0;
1637 rq->current_nr_sectors = 0;
1638 rq->sem = NULL;
1639 rq->bh = NULL;
1640 rq->bhtail = NULL;
1641 rq->next = NULL;
1645 * This function issues a special IDE device request
1646 * onto the request queue.
1648 * If action is ide_wait, then the rq is queued at the end of the
1649 * request queue, and the function sleeps until it has been processed.
1650 * This is for use when invoked from an ioctl handler.
1652 * If action is ide_preempt, then the rq is queued at the head of
1653 * the request queue, displacing the currently-being-processed
1654 * request and this function returns immediately without waiting
1655 * for the new rq to be completed. This is VERY DANGEROUS, and is
1656 * intended for careful use by the ATAPI tape/cdrom driver code.
1658 * If action is ide_next, then the rq is queued immediately after
1659 * the currently-being-processed-request (if any), and the function
1660 * returns without waiting for the new rq to be completed. As above,
1661 * This is VERY DANGEROUS, and is intended for careful use by the
1662 * ATAPI tape/cdrom driver code.
1664 * If action is ide_end, then the rq is queued at the end of the
1665 * request queue, and the function returns immediately without waiting
1666 * for the new rq to be completed. This is again intended for careful
1667 * use by the ATAPI tape/cdrom driver code.
1669 int ide_do_drive_cmd (ide_drive_t *drive, struct request *rq, ide_action_t action)
1671 unsigned long flags;
1672 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1673 unsigned int major = HWIF(drive)->major;
1674 struct request *cur_rq;
1675 DECLARE_MUTEX_LOCKED(sem);
1677 #ifdef CONFIG_BLK_DEV_PDC4030
1678 if (HWIF(drive)->chipset == ide_pdc4030 && rq->buffer != NULL)
1679 return -ENOSYS; /* special drive cmds not supported */
1680 #endif
1681 rq->errors = 0;
1682 rq->rq_status = RQ_ACTIVE;
1683 rq->rq_dev = MKDEV(major,(drive->select.b.unit)<<PARTN_BITS);
1684 if (action == ide_wait)
1685 rq->sem = &sem;
1686 spin_lock_irqsave(&io_request_lock, flags);
1687 cur_rq = drive->queue.current_request;
1688 if (cur_rq == NULL || action == ide_preempt) {
1689 rq->next = cur_rq;
1690 drive->queue.current_request = rq;
1691 if (action == ide_preempt)
1692 hwgroup->rq = NULL;
1693 } else {
1694 if (action == ide_wait || action == ide_end) {
1695 while (cur_rq->next != NULL) /* find end of list */
1696 cur_rq = cur_rq->next;
1698 rq->next = cur_rq->next;
1699 cur_rq->next = rq;
1701 ide_do_request(hwgroup);
1702 spin_unlock_irqrestore(&io_request_lock, flags);
1703 if (action == ide_wait) {
1704 down(&sem); /* wait for it to be serviced */
1705 return rq->errors ? -EIO : 0; /* return -EIO if errors */
1707 return 0;
1712 * This routine is called to flush all partitions and partition tables
1713 * for a changed disk, and then re-read the new partition table.
1714 * If we are revalidating a disk because of a media change, then we
1715 * enter with usage == 0. If we are using an ioctl, we automatically have
1716 * usage == 1 (we need an open channel to use an ioctl :-), so this
1717 * is our limit.
1719 int ide_revalidate_disk (kdev_t i_rdev)
1721 ide_drive_t *drive;
1722 ide_hwgroup_t *hwgroup;
1723 unsigned int p, major, minor;
1724 long flags;
1726 if ((drive = get_info_ptr(i_rdev)) == NULL)
1727 return -ENODEV;
1728 major = MAJOR(i_rdev);
1729 minor = drive->select.b.unit << PARTN_BITS;
1730 hwgroup = HWGROUP(drive);
1731 spin_lock_irqsave(&io_request_lock, flags);
1732 if (drive->busy || (drive->usage > 1)) {
1733 spin_unlock_irqrestore(&io_request_lock, flags);
1734 return -EBUSY;
1736 drive->busy = 1;
1737 MOD_INC_USE_COUNT;
1738 spin_unlock_irqrestore(&io_request_lock, flags);
1740 for (p = 0; p < (1<<PARTN_BITS); ++p) {
1741 if (drive->part[p].nr_sects > 0) {
1742 kdev_t devp = MKDEV(major, minor+p);
1743 struct super_block * sb = get_super(devp);
1744 fsync_dev (devp);
1745 if (sb)
1746 invalidate_inodes(sb);
1747 invalidate_buffers (devp);
1748 set_blocksize(devp, 1024);
1750 drive->part[p].start_sect = 0;
1751 drive->part[p].nr_sects = 0;
1754 drive->part[0].nr_sects = current_capacity(drive);
1755 if ((drive->media != ide_disk && drive->media != ide_floppy) ||
1756 drive->driver == NULL || !drive->part[0].nr_sects)
1757 drive->part[0].start_sect = -1;
1758 resetup_one_dev(HWIF(drive)->gd, drive->select.b.unit);
1760 drive->busy = 0;
1761 wake_up(&drive->wqueue);
1762 MOD_DEC_USE_COUNT;
1763 return 0;
1766 static void revalidate_drives (void)
1768 ide_hwif_t *hwif;
1769 ide_drive_t *drive;
1770 int index, unit;
1772 for (index = 0; index < MAX_HWIFS; ++index) {
1773 hwif = &ide_hwifs[index];
1774 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1775 drive = &ide_hwifs[index].drives[unit];
1776 if (drive->revalidate) {
1777 drive->revalidate = 0;
1778 if (!initializing)
1779 (void) ide_revalidate_disk(MKDEV(hwif->major, unit<<PARTN_BITS));
1785 static void ide_init_module (int type)
1787 int found = 0;
1788 ide_module_t *module = ide_modules;
1790 while (module) {
1791 if (module->type == type) {
1792 found = 1;
1793 (void) module->init();
1795 module = module->next;
1797 revalidate_drives();
1798 #ifdef CONFIG_KMOD
1799 if (!found && type == IDE_PROBE_MODULE)
1800 (void) request_module("ide-probe");
1801 #endif /* CONFIG_KMOD */
1804 static int ide_open (struct inode * inode, struct file * filp)
1806 ide_drive_t *drive;
1807 int rc;
1809 if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
1810 return -ENXIO;
1811 MOD_INC_USE_COUNT;
1812 if (drive->driver == NULL)
1813 ide_init_module(IDE_DRIVER_MODULE);
1814 #ifdef CONFIG_KMOD
1815 if (drive->driver == NULL) {
1816 if (drive->media == ide_disk)
1817 (void) request_module("ide-disk");
1818 if (drive->media == ide_cdrom)
1819 (void) request_module("ide-cd");
1820 if (drive->media == ide_tape)
1821 (void) request_module("ide-tape");
1822 if (drive->media == ide_floppy)
1823 (void) request_module("ide-floppy");
1825 #endif /* CONFIG_KMOD */
1826 while (drive->busy)
1827 sleep_on(&drive->wqueue);
1828 drive->usage++;
1829 if (drive->driver != NULL) {
1830 if ((rc = DRIVER(drive)->open(inode, filp, drive)))
1831 MOD_DEC_USE_COUNT;
1832 return rc;
1834 printk ("%s: driver not present\n", drive->name);
1835 drive->usage--;
1836 MOD_DEC_USE_COUNT;
1837 return -ENXIO;
1841 * Releasing a block device means we sync() it, so that it can safely
1842 * be forgotten about...
1844 static int ide_release (struct inode * inode, struct file * file)
1846 ide_drive_t *drive;
1848 if ((drive = get_info_ptr(inode->i_rdev)) != NULL) {
1849 drive->usage--;
1850 if (drive->driver != NULL)
1851 DRIVER(drive)->release(inode, file, drive);
1852 MOD_DEC_USE_COUNT;
1854 return 0;
1857 int ide_replace_subdriver (ide_drive_t *drive, const char *driver)
1859 if (!drive->present || drive->busy || drive->usage)
1860 goto abort;
1861 if (drive->driver != NULL && DRIVER(drive)->cleanup(drive))
1862 goto abort;
1863 strncpy(drive->driver_req, driver, 9);
1864 ide_init_module(IDE_DRIVER_MODULE);
1865 drive->driver_req[0] = 0;
1866 ide_init_module(IDE_DRIVER_MODULE);
1867 if (DRIVER(drive) && !strcmp(DRIVER(drive)->name, driver))
1868 return 0;
1869 abort:
1870 return 1;
1873 #ifdef CONFIG_PROC_FS
1874 ide_proc_entry_t generic_subdriver_entries[] = {
1875 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
1876 { NULL, 0, NULL, NULL }
1878 #endif
1880 void ide_unregister (unsigned int index)
1882 struct gendisk *gd, **gdp;
1883 ide_drive_t *drive, *d;
1884 ide_hwif_t *hwif, *g;
1885 ide_hwgroup_t *hwgroup;
1886 int irq_count = 0, unit, i;
1887 unsigned long flags;
1888 unsigned int p, minor;
1889 ide_hwif_t old_hwif;
1891 if (index >= MAX_HWIFS)
1892 return;
1893 save_flags(flags); /* all CPUs */
1894 cli(); /* all CPUs */
1895 hwif = &ide_hwifs[index];
1896 if (!hwif->present)
1897 goto abort;
1898 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1899 drive = &hwif->drives[unit];
1900 if (!drive->present)
1901 continue;
1902 if (drive->busy || drive->usage)
1903 goto abort;
1904 if (drive->driver != NULL && DRIVER(drive)->cleanup(drive))
1905 goto abort;
1907 hwif->present = 0;
1910 * All clear? Then blow away the buffer cache
1912 sti();
1913 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1914 drive = &hwif->drives[unit];
1915 if (!drive->present)
1916 continue;
1917 minor = drive->select.b.unit << PARTN_BITS;
1918 for (p = 0; p < (1<<PARTN_BITS); ++p) {
1919 if (drive->part[p].nr_sects > 0) {
1920 kdev_t devp = MKDEV(hwif->major, minor+p);
1921 struct super_block * sb = get_super(devp);
1922 if (sb) invalidate_inodes(sb);
1923 invalidate_buffers (devp);
1926 #ifdef CONFIG_PROC_FS
1927 destroy_proc_ide_drives(hwif);
1928 #endif
1930 cli();
1931 hwgroup = hwif->hwgroup;
1934 * free the irq if we were the only hwif using it
1936 g = hwgroup->hwif;
1937 do {
1938 if (g->irq == hwif->irq)
1939 ++irq_count;
1940 g = g->next;
1941 } while (g != hwgroup->hwif);
1942 if (irq_count == 1)
1943 free_irq(hwif->irq, hwgroup);
1946 * Note that we only release the standard ports,
1947 * and do not even try to handle any extra ports
1948 * allocated for weird IDE interface chipsets.
1950 ide_release_region(hwif->io_ports[IDE_DATA_OFFSET], 8);
1951 if (hwif->io_ports[IDE_CONTROL_OFFSET])
1952 ide_release_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
1955 * Remove us from the hwgroup, and free
1956 * the hwgroup if we were the only member
1958 d = hwgroup->drive;
1959 for (i = 0; i < MAX_DRIVES; ++i) {
1960 drive = &hwif->drives[i];
1961 if (!drive->present)
1962 continue;
1963 while (hwgroup->drive->next != drive)
1964 hwgroup->drive = hwgroup->drive->next;
1965 hwgroup->drive->next = drive->next;
1966 if (hwgroup->drive == drive)
1967 hwgroup->drive = NULL;
1968 if (drive->id != NULL) {
1969 kfree(drive->id);
1970 drive->id = NULL;
1972 drive->present = 0;
1974 if (d->present)
1975 hwgroup->drive = d;
1976 while (hwgroup->hwif->next != hwif)
1977 hwgroup->hwif = hwgroup->hwif->next;
1978 hwgroup->hwif->next = hwif->next;
1979 if (hwgroup->hwif == hwif)
1980 kfree(hwgroup);
1981 else
1982 hwgroup->hwif = HWIF(hwgroup->drive);
1984 #ifdef CONFIG_BLK_DEV_IDEDMA
1985 if (hwif->dma_base)
1986 (void) ide_release_dma(hwif);
1987 #endif /* CONFIG_BLK_DEV_IDEDMA */
1990 * Remove us from the kernel's knowledge
1992 unregister_blkdev(hwif->major, hwif->name);
1993 kfree(blksize_size[hwif->major]);
1994 kfree(max_sectors[hwif->major]);
1995 kfree(max_readahead[hwif->major]);
1996 blk_cleanup_queue(BLK_DEFAULT_QUEUE(hwif->major));
1997 blk_dev[hwif->major].data = NULL;
1998 blk_dev[hwif->major].queue = NULL;
1999 blksize_size[hwif->major] = NULL;
2000 for (gdp = &gendisk_head; *gdp; gdp = &((*gdp)->next))
2001 if (*gdp == hwif->gd)
2002 break;
2003 if (*gdp == NULL)
2004 printk("gd not in disk chain!\n");
2005 else {
2006 gd = *gdp; *gdp = gd->next;
2007 kfree(gd->sizes);
2008 kfree(gd->part);
2009 kfree(gd);
2011 old_hwif = *hwif;
2012 init_hwif_data (index); /* restore hwif data to pristine status */
2013 hwif->hwgroup = old_hwif.hwgroup;
2014 hwif->tuneproc = old_hwif.tuneproc;
2015 hwif->resetproc = old_hwif.resetproc;
2016 hwif->dmaproc = old_hwif.dmaproc;
2017 hwif->dma_base = old_hwif.dma_base;
2018 hwif->dma_extra = old_hwif.dma_extra;
2019 hwif->config_data = old_hwif.config_data;
2020 hwif->select_data = old_hwif.select_data;
2021 hwif->irq = old_hwif.irq;
2022 hwif->major = old_hwif.major;
2023 hwif->proc = old_hwif.proc;
2024 hwif->udma_four = old_hwif.udma_four;
2025 hwif->chipset = old_hwif.chipset;
2026 hwif->pci_dev = old_hwif.pci_dev;
2027 hwif->pci_devid = old_hwif.pci_devid;
2028 abort:
2029 restore_flags(flags); /* all CPUs */
2033 * Setup hw_regs_t structure described by parameters. You
2034 * may set up the hw structure yourself OR use this routine to
2035 * do it for you.
2037 void ide_setup_ports ( hw_regs_t *hw,
2038 ide_ioreg_t base, int *offsets,
2039 ide_ioreg_t ctrl, ide_ioreg_t intr,
2040 ide_ack_intr_t *ack_intr, int irq)
2042 int i;
2044 for (i = 0; i < IDE_NR_PORTS; i++) {
2045 if (offsets[i] == -1) {
2046 switch(i) {
2047 case IDE_CONTROL_OFFSET:
2048 hw->io_ports[i] = ctrl;
2049 break;
2050 case IDE_IRQ_OFFSET:
2051 hw->io_ports[i] = intr;
2052 break;
2053 default:
2054 hw->io_ports[i] = 0;
2055 break;
2057 } else {
2058 hw->io_ports[i] = base + offsets[i];
2061 hw->irq = irq;
2062 hw->ack_intr = ack_intr;
2066 * Register an IDE interface, specifing exactly the registers etc
2067 * Set init=1 iff calling before probes have taken place.
2069 int ide_register_hw (hw_regs_t *hw, ide_hwif_t **hwifp)
2071 int index, retry = 1;
2072 ide_hwif_t *hwif;
2074 do {
2075 for (index = 0; index < MAX_HWIFS; ++index) {
2076 hwif = &ide_hwifs[index];
2077 if (hwif->hw.io_ports[IDE_DATA_OFFSET] == hw->io_ports[IDE_DATA_OFFSET])
2078 goto found;
2080 for (index = 0; index < MAX_HWIFS; ++index) {
2081 hwif = &ide_hwifs[index];
2082 if ((!hwif->present && !hwif->mate && !initializing) ||
2083 (!hwif->hw.io_ports[IDE_DATA_OFFSET] && initializing))
2084 goto found;
2086 for (index = 0; index < MAX_HWIFS; index++)
2087 ide_unregister(index);
2088 } while (retry--);
2089 return -1;
2090 found:
2091 if (hwif->present)
2092 ide_unregister(index);
2093 if (hwif->present)
2094 return -1;
2095 memcpy(&hwif->hw, hw, sizeof(*hw));
2096 memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->hw.io_ports));
2097 hwif->irq = hw->irq;
2098 hwif->noprobe = 0;
2100 if (!initializing) {
2101 ide_init_module(IDE_PROBE_MODULE);
2102 #ifdef CONFIG_PROC_FS
2103 create_proc_ide_interfaces();
2104 #endif
2105 ide_init_module(IDE_DRIVER_MODULE);
2108 if (hwifp)
2109 *hwifp = hwif;
2111 return (initializing || hwif->present) ? index : -1;
2115 * Compatability function with existing drivers. If you want
2116 * something different, use the function above.
2118 int ide_register (int arg1, int arg2, int irq)
2120 hw_regs_t hw;
2121 ide_init_hwif_ports(&hw, (ide_ioreg_t) arg1, (ide_ioreg_t) arg2, NULL);
2122 hw.irq = irq;
2123 return ide_register_hw(&hw, NULL);
2126 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)
2128 ide_settings_t **p = (ide_settings_t **) &drive->settings, *setting = NULL;
2130 while ((*p) && strcmp((*p)->name, name) < 0)
2131 p = &((*p)->next);
2132 if ((setting = kmalloc(sizeof(*setting), GFP_KERNEL)) == NULL)
2133 goto abort;
2134 memset(setting, 0, sizeof(*setting));
2135 if ((setting->name = kmalloc(strlen(name) + 1, GFP_KERNEL)) == NULL)
2136 goto abort;
2137 strcpy(setting->name, name); setting->rw = rw;
2138 setting->read_ioctl = read_ioctl; setting->write_ioctl = write_ioctl;
2139 setting->data_type = data_type; setting->min = min;
2140 setting->max = max; setting->mul_factor = mul_factor;
2141 setting->div_factor = div_factor; setting->data = data;
2142 setting->set = set; setting->next = *p;
2143 if (drive->driver)
2144 setting->auto_remove = 1;
2145 *p = setting;
2146 return;
2147 abort:
2148 if (setting)
2149 kfree(setting);
2152 void ide_remove_setting (ide_drive_t *drive, char *name)
2154 ide_settings_t **p = (ide_settings_t **) &drive->settings, *setting;
2156 while ((*p) && strcmp((*p)->name, name))
2157 p = &((*p)->next);
2158 if ((setting = (*p)) == NULL)
2159 return;
2160 (*p) = setting->next;
2161 kfree(setting->name);
2162 kfree(setting);
2165 static ide_settings_t *ide_find_setting_by_ioctl (ide_drive_t *drive, int cmd)
2167 ide_settings_t *setting = drive->settings;
2169 while (setting) {
2170 if (setting->read_ioctl == cmd || setting->write_ioctl == cmd)
2171 break;
2172 setting = setting->next;
2174 return setting;
2177 ide_settings_t *ide_find_setting_by_name (ide_drive_t *drive, char *name)
2179 ide_settings_t *setting = drive->settings;
2181 while (setting) {
2182 if (strcmp(setting->name, name) == 0)
2183 break;
2184 setting = setting->next;
2186 return setting;
2189 static void auto_remove_settings (ide_drive_t *drive)
2191 ide_settings_t *setting;
2192 repeat:
2193 setting = drive->settings;
2194 while (setting) {
2195 if (setting->auto_remove) {
2196 ide_remove_setting(drive, setting->name);
2197 goto repeat;
2199 setting = setting->next;
2203 int ide_read_setting (ide_drive_t *drive, ide_settings_t *setting)
2205 int val = -EINVAL;
2206 unsigned long flags;
2208 if ((setting->rw & SETTING_READ)) {
2209 spin_lock_irqsave(&io_request_lock, flags);
2210 switch(setting->data_type) {
2211 case TYPE_BYTE:
2212 val = *((u8 *) setting->data);
2213 break;
2214 case TYPE_SHORT:
2215 val = *((u16 *) setting->data);
2216 break;
2217 case TYPE_INT:
2218 case TYPE_INTA:
2219 val = *((u32 *) setting->data);
2220 break;
2222 spin_unlock_irqrestore(&io_request_lock, flags);
2224 return val;
2227 int ide_spin_wait_hwgroup (ide_drive_t *drive, unsigned long *flags)
2229 ide_hwgroup_t *hwgroup = HWGROUP(drive);
2230 unsigned long timeout = jiffies + (3 * HZ);
2232 spin_lock_irqsave(&io_request_lock, *flags);
2233 while (hwgroup->busy) {
2234 unsigned long lflags;
2235 spin_unlock_irqrestore(&io_request_lock, *flags);
2236 __save_flags(lflags); /* local CPU only */
2237 __sti(); /* local CPU only; needed for jiffies */
2238 if (0 < (signed long)(jiffies - timeout)) {
2239 __restore_flags(lflags); /* local CPU only */
2240 printk("%s: channel busy\n", drive->name);
2241 return -EBUSY;
2243 __restore_flags(lflags); /* local CPU only */
2244 spin_lock_irqsave(&io_request_lock, *flags);
2246 return 0;
2250 * FIXME: This should be changed to enqueue a special request
2251 * to the driver to change settings, and then wait on a sema for completion.
2252 * The current scheme of polling is kludgey, though safe enough.
2254 int ide_write_setting (ide_drive_t *drive, ide_settings_t *setting, int val)
2256 unsigned long flags;
2257 int i;
2258 u32 *p;
2260 if (!capable(CAP_SYS_ADMIN))
2261 return -EACCES;
2262 if (!(setting->rw & SETTING_WRITE))
2263 return -EPERM;
2264 if (val < setting->min || val > setting->max)
2265 return -EINVAL;
2266 if (setting->set)
2267 return setting->set(drive, val);
2268 if (ide_spin_wait_hwgroup(drive, &flags))
2269 return -EBUSY;
2270 switch (setting->data_type) {
2271 case TYPE_BYTE:
2272 *((u8 *) setting->data) = val;
2273 break;
2274 case TYPE_SHORT:
2275 *((u16 *) setting->data) = val;
2276 break;
2277 case TYPE_INT:
2278 *((u32 *) setting->data) = val;
2279 break;
2280 case TYPE_INTA:
2281 p = (u32 *) setting->data;
2282 for (i = 0; i < 1 << PARTN_BITS; i++, p++)
2283 *p = val;
2284 break;
2286 spin_unlock_irqrestore(&io_request_lock, flags);
2287 return 0;
2290 static int set_io_32bit(ide_drive_t *drive, int arg)
2292 drive->io_32bit = arg;
2293 #ifdef CONFIG_BLK_DEV_DTC2278
2294 if (HWIF(drive)->chipset == ide_dtc2278)
2295 HWIF(drive)->drives[!drive->select.b.unit].io_32bit = arg;
2296 #endif /* CONFIG_BLK_DEV_DTC2278 */
2297 return 0;
2300 static int set_using_dma (ide_drive_t *drive, int arg)
2302 if (!drive->driver || !DRIVER(drive)->supports_dma)
2303 return -EPERM;
2304 if (!drive->id || !(drive->id->capability & 1) || !HWIF(drive)->dmaproc)
2305 return -EPERM;
2306 if (HWIF(drive)->dmaproc(arg ? ide_dma_on : ide_dma_off, drive))
2307 return -EIO;
2308 return 0;
2311 static int set_pio_mode (ide_drive_t *drive, int arg)
2313 struct request rq;
2315 if (!HWIF(drive)->tuneproc)
2316 return -ENOSYS;
2317 if (drive->special.b.set_tune)
2318 return -EBUSY;
2319 ide_init_drive_cmd(&rq);
2320 drive->tune_req = (byte) arg;
2321 drive->special.b.set_tune = 1;
2322 (void) ide_do_drive_cmd (drive, &rq, ide_wait);
2323 return 0;
2326 void ide_add_generic_settings (ide_drive_t *drive)
2329 * drive setting name read/write access read ioctl write ioctl data type min max mul_factor div_factor data pointer set function
2331 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);
2332 ide_add_setting(drive, "keepsettings", SETTING_RW, HDIO_GET_KEEPSETTINGS, HDIO_SET_KEEPSETTINGS, TYPE_BYTE, 0, 1, 1, 1, &drive->keep_settings, NULL);
2333 ide_add_setting(drive, "nice1", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->nice1, NULL);
2334 ide_add_setting(drive, "pio_mode", SETTING_WRITE, -1, HDIO_SET_PIO_MODE, TYPE_BYTE, 0, 255, 1, 1, NULL, set_pio_mode);
2335 ide_add_setting(drive, "slow", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->slow, NULL);
2336 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);
2337 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);
2338 ide_add_setting(drive, "ide_scsi", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->scsi, NULL);
2341 int ide_wait_cmd (ide_drive_t *drive, int cmd, int nsect, int feature, int sectors, byte *buf)
2343 struct request rq;
2344 byte buffer[4];
2346 if (!buf)
2347 buf = buffer;
2348 memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
2349 ide_init_drive_cmd(&rq);
2350 rq.buffer = buf;
2351 *buf++ = cmd;
2352 *buf++ = nsect;
2353 *buf++ = feature;
2354 *buf++ = sectors;
2355 return ide_do_drive_cmd(drive, &rq, ide_wait);
2359 * Delay for *at least* 50ms. As we don't know how much time is left
2360 * until the next tick occurs, we wait an extra tick to be safe.
2361 * This is used only during the probing/polling for drives at boot time.
2363 * However, its usefullness may be needed in other places, thus we export it now.
2364 * The future may change this to a millisecond setable delay.
2366 void ide_delay_50ms (void)
2368 unsigned long timeout = jiffies + ((HZ + 19)/20) + 1;
2369 while (0 < (signed long)(timeout - jiffies));
2372 static int ide_ioctl (struct inode *inode, struct file *file,
2373 unsigned int cmd, unsigned long arg)
2375 int err = 0, major, minor;
2376 ide_drive_t *drive;
2377 struct request rq;
2378 kdev_t dev;
2379 ide_settings_t *setting;
2381 if (!inode || !(dev = inode->i_rdev))
2382 return -EINVAL;
2383 major = MAJOR(dev); minor = MINOR(dev);
2384 if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
2385 return -ENODEV;
2387 if ((setting = ide_find_setting_by_ioctl(drive, cmd)) != NULL) {
2388 if (cmd == setting->read_ioctl) {
2389 err = ide_read_setting(drive, setting);
2390 return err >= 0 ? put_user(err, (long *) arg) : err;
2391 } else {
2392 if ((MINOR(inode->i_rdev) & PARTN_MASK))
2393 return -EINVAL;
2394 return ide_write_setting(drive, setting, arg);
2398 ide_init_drive_cmd (&rq);
2399 switch (cmd) {
2400 case HDIO_GETGEO:
2402 struct hd_geometry *loc = (struct hd_geometry *) arg;
2403 unsigned short bios_cyl = drive->bios_cyl; /* truncate */
2404 if (!loc || (drive->media != ide_disk && drive->media != ide_floppy)) return -EINVAL;
2405 if (put_user(drive->bios_head, (byte *) &loc->heads)) return -EFAULT;
2406 if (put_user(drive->bios_sect, (byte *) &loc->sectors)) return -EFAULT;
2407 if (put_user(bios_cyl, (unsigned short *) &loc->cylinders)) return -EFAULT;
2408 if (put_user((unsigned)drive->part[MINOR(inode->i_rdev)&PARTN_MASK].start_sect,
2409 (unsigned long *) &loc->start)) return -EFAULT;
2410 return 0;
2413 case BLKGETSIZE: /* Return device size */
2414 return put_user(drive->part[MINOR(inode->i_rdev)&PARTN_MASK].nr_sects, (long *) arg);
2416 case BLKRRPART: /* Re-read partition tables */
2417 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2418 return ide_revalidate_disk(inode->i_rdev);
2420 case HDIO_OBSOLETE_IDENTITY:
2421 case HDIO_GET_IDENTITY:
2422 if (MINOR(inode->i_rdev) & PARTN_MASK)
2423 return -EINVAL;
2424 if (drive->id == NULL)
2425 return -ENOMSG;
2426 if (copy_to_user((char *)arg, (char *)drive->id, (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
2427 return -EFAULT;
2428 return 0;
2430 case HDIO_GET_NICE:
2431 return put_user(drive->dsc_overlap << IDE_NICE_DSC_OVERLAP |
2432 drive->atapi_overlap << IDE_NICE_ATAPI_OVERLAP |
2433 drive->nice0 << IDE_NICE_0 |
2434 drive->nice1 << IDE_NICE_1 |
2435 drive->nice2 << IDE_NICE_2,
2436 (long *) arg);
2437 case HDIO_DRIVE_CMD:
2439 byte args[4], *argbuf = args;
2440 int argsize = 4;
2441 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2442 if (NULL == (void *) arg)
2443 return ide_do_drive_cmd(drive, &rq, ide_wait);
2444 if (copy_from_user(args, (void *)arg, 4))
2445 return -EFAULT;
2446 if (args[3]) {
2447 argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
2448 argbuf = kmalloc(argsize, GFP_KERNEL);
2449 if (argbuf == NULL)
2450 return -ENOMEM;
2451 memcpy(argbuf, args, 4);
2453 if (ide_ata66_check(drive, args[0], args[1], args[2]))
2454 goto abort;
2456 err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
2458 if (!err && set_transfer(drive, args[0], args[1], args[2])) {
2459 ide_driveid_update(drive);
2461 abort:
2462 if (copy_to_user((void *)arg, argbuf, argsize))
2463 err = -EFAULT;
2464 if (argsize > 4)
2465 kfree(argbuf);
2466 return err;
2469 case HDIO_SCAN_HWIF:
2471 int args[3];
2472 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2473 if (copy_from_user(args, (void *)arg, 3 * sizeof(int)))
2474 return -EFAULT;
2475 if (ide_register(args[0], args[1], args[2]) == -1)
2476 return -EIO;
2477 return 0;
2479 case HDIO_UNREGISTER_HWIF:
2480 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2481 /* should I check here for arg > MAX_HWIFS, or
2482 just let ide_unregister fail silently? -- shaver */
2483 ide_unregister(arg);
2484 return 0;
2485 case HDIO_SET_NICE:
2486 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2487 if (drive->driver == NULL)
2488 return -EPERM;
2489 if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
2490 return -EPERM;
2491 drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
2492 if (drive->dsc_overlap && !DRIVER(drive)->supports_dsc_overlap) {
2493 drive->dsc_overlap = 0;
2494 return -EPERM;
2496 drive->nice1 = (arg >> IDE_NICE_1) & 1;
2497 return 0;
2499 case BLKROSET:
2500 case BLKROGET:
2501 case BLKFLSBUF:
2502 case BLKSSZGET:
2503 case BLKPG:
2504 return blk_ioctl(inode->i_rdev, cmd, arg);
2506 default:
2507 if (drive->driver != NULL)
2508 return DRIVER(drive)->ioctl(drive, inode, file, cmd, arg);
2509 return -EPERM;
2513 static int ide_check_media_change (kdev_t i_rdev)
2515 ide_drive_t *drive;
2517 if ((drive = get_info_ptr(i_rdev)) == NULL)
2518 return -ENODEV;
2519 if (drive->driver != NULL)
2520 return DRIVER(drive)->media_change(drive);
2521 return 0;
2524 void ide_fixstring (byte *s, const int bytecount, const int byteswap)
2526 byte *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */
2528 if (byteswap) {
2529 /* convert from big-endian to host byte order */
2530 for (p = end ; p != s;) {
2531 unsigned short *pp = (unsigned short *) (p -= 2);
2532 *pp = ntohs(*pp);
2536 /* strip leading blanks */
2537 while (s != end && *s == ' ')
2538 ++s;
2540 /* compress internal blanks and strip trailing blanks */
2541 while (s != end && *s) {
2542 if (*s++ != ' ' || (s != end && *s && *s != ' '))
2543 *p++ = *(s-1);
2546 /* wipe out trailing garbage */
2547 while (p != end)
2548 *p++ = '\0';
2552 * stridx() returns the offset of c within s,
2553 * or -1 if c is '\0' or not found within s.
2555 static int __init stridx (const char *s, char c)
2557 char *i = strchr(s, c);
2558 return (i && c) ? i - s : -1;
2562 * match_parm() does parsing for ide_setup():
2564 * 1. the first char of s must be '='.
2565 * 2. if the remainder matches one of the supplied keywords,
2566 * the index (1 based) of the keyword is negated and returned.
2567 * 3. if the remainder is a series of no more than max_vals numbers
2568 * separated by commas, the numbers are saved in vals[] and a
2569 * count of how many were saved is returned. Base10 is assumed,
2570 * and base16 is allowed when prefixed with "0x".
2571 * 4. otherwise, zero is returned.
2573 static int __init match_parm (char *s, const char *keywords[], int vals[], int max_vals)
2575 static const char *decimal = "0123456789";
2576 static const char *hex = "0123456789abcdef";
2577 int i, n;
2579 if (*s++ == '=') {
2581 * Try matching against the supplied keywords,
2582 * and return -(index+1) if we match one
2584 if (keywords != NULL) {
2585 for (i = 0; *keywords != NULL; ++i) {
2586 if (!strcmp(s, *keywords++))
2587 return -(i+1);
2591 * Look for a series of no more than "max_vals"
2592 * numeric values separated by commas, in base10,
2593 * or base16 when prefixed with "0x".
2594 * Return a count of how many were found.
2596 for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
2597 vals[n] = i;
2598 while ((i = stridx(decimal, *++s)) >= 0)
2599 vals[n] = (vals[n] * 10) + i;
2600 if (*s == 'x' && !vals[n]) {
2601 while ((i = stridx(hex, *++s)) >= 0)
2602 vals[n] = (vals[n] * 0x10) + i;
2604 if (++n == max_vals)
2605 break;
2606 if (*s == ',' || *s == ';')
2607 ++s;
2609 if (!*s)
2610 return n;
2612 return 0; /* zero = nothing matched */
2616 * ide_setup() gets called VERY EARLY during initialization,
2617 * to handle kernel "command line" strings beginning with "hdx="
2618 * or "ide". Here is the complete set currently supported:
2620 * "hdx=" is recognized for all "x" from "a" to "h", such as "hdc".
2621 * "idex=" is recognized for all "x" from "0" to "3", such as "ide1".
2623 * "hdx=noprobe" : drive may be present, but do not probe for it
2624 * "hdx=none" : drive is NOT present, ignore cmos and do not probe
2625 * "hdx=nowerr" : ignore the WRERR_STAT bit on this drive
2626 * "hdx=cdrom" : drive is present, and is a cdrom drive
2627 * "hdx=cyl,head,sect" : disk drive is present, with specified geometry
2628 * "hdx=noremap" : do not remap 0->1 even though EZD was detected
2629 * "hdx=autotune" : driver will attempt to tune interface speed
2630 * to the fastest PIO mode supported,
2631 * if possible for this drive only.
2632 * Not fully supported by all chipset types,
2633 * and quite likely to cause trouble with
2634 * older/odd IDE drives.
2636 * "hdx=slow" : insert a huge pause after each access to the data
2637 * port. Should be used only as a last resort.
2639 * "hdx=swapdata" : when the drive is a disk, byte swap all data
2640 * "hdx=bswap" : same as above..........
2641 * "hdxlun=xx" : set the drive last logical unit.
2642 * "hdx=flash" : allows for more than one ata_flash disk to be
2643 * registered. In most cases, only one device
2644 * will be present.
2645 * "hdx=scsi" : the return of the ide-scsi flag, this is useful for
2646 * allowwing ide-floppy, ide-tape, and ide-cdrom|writers
2647 * to use ide-scsi emulation on a device specific option.
2648 * "idebus=xx" : inform IDE driver of VESA/PCI bus speed in MHz,
2649 * where "xx" is between 20 and 66 inclusive,
2650 * used when tuning chipset PIO modes.
2651 * For PCI bus, 25 is correct for a P75 system,
2652 * 30 is correct for P90,P120,P180 systems,
2653 * and 33 is used for P100,P133,P166 systems.
2654 * If in doubt, use idebus=33 for PCI.
2655 * As for VLB, it is safest to not specify it.
2657 * "idex=noprobe" : do not attempt to access/use this interface
2658 * "idex=base" : probe for an interface at the addr specified,
2659 * where "base" is usually 0x1f0 or 0x170
2660 * and "ctl" is assumed to be "base"+0x206
2661 * "idex=base,ctl" : specify both base and ctl
2662 * "idex=base,ctl,irq" : specify base, ctl, and irq number
2663 * "idex=autotune" : driver will attempt to tune interface speed
2664 * to the fastest PIO mode supported,
2665 * for all drives on this interface.
2666 * Not fully supported by all chipset types,
2667 * and quite likely to cause trouble with
2668 * older/odd IDE drives.
2669 * "idex=noautotune" : driver will NOT attempt to tune interface speed
2670 * This is the default for most chipsets,
2671 * except the cmd640.
2672 * "idex=serialize" : do not overlap operations on idex and ide(x^1)
2673 * "idex=four" : four drives on idex and ide(x^1) share same ports
2674 * "idex=reset" : reset interface before first use
2675 * "idex=dma" : enable DMA by default on both drives if possible
2676 * "idex=ata66" : informs the interface that it has an 80c cable
2677 * for chipsets that are ATA-66 capable, but
2678 * the ablity to bit test for detection is
2679 * currently unknown.
2681 * "splitfifo=betweenChan"
2682 * : FIFO Configuration of VIA 82c586(<nothing>,"A"or"B").
2683 * --see what follows...
2684 * "splitfifo=betweenChan,thresholdprim,thresholdsec"
2685 * : FIFO Configuration of VIA 82c586(<nothing>,"A" or "B").
2686 * betweenChan = 1(all FIFO's to primary channel)
2687 * , 2(all FIFO's to secondary channel)
2688 * , 3 or 4(evenly shared between them).
2689 * note: without FIFO, a channel is (u)dma disabled!
2690 * thresholdprim = 4, 3, 2 or 1
2691 * (standing for 1, 3/4, 1/2, 1/4).
2692 * Sets the threshold of FIFO to begin dma
2693 * transfer on the primary channel.
2694 * thresholdsec = cf upper, but for secondary channel.
2696 * The following are valid ONLY on ide0, (except dc4030)
2697 * and the defaults for the base,ctl ports must not be altered.
2699 * "ide0=dtc2278" : probe/support DTC2278 interface
2700 * "ide0=ht6560b" : probe/support HT6560B interface
2701 * "ide0=cmd640_vlb" : *REQUIRED* for VLB cards with the CMD640 chip
2702 * (not for PCI -- automatically detected)
2703 * "ide0=qd6580" : probe/support qd6580 interface
2704 * "ide0=ali14xx" : probe/support ali14xx chipsets (ALI M1439, M1443, M1445)
2705 * "ide0=umc8672" : probe/support umc8672 chipsets
2706 * "idex=dc4030" : probe/support Promise DC4030VL interface
2707 * "ide=doubler" : probe/support IDE doublers on Amiga
2709 int __init ide_setup (char *s)
2711 int i, vals[3];
2712 ide_hwif_t *hwif;
2713 ide_drive_t *drive;
2714 unsigned int hw, unit;
2715 const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
2716 const char max_hwif = '0' + (MAX_HWIFS - 1);
2718 printk("ide_setup: %s", s);
2720 #ifdef CONFIG_BLK_DEV_IDEDOUBLER
2721 if (!strcmp(s, "ide=doubler")) {
2722 extern int ide_doubler;
2724 printk("ide: Enabled support for IDE doublers\n");
2725 ide_doubler = 1;
2726 return 0;
2728 #endif /* CONFIG_BLK_DEV_IDEDOUBLER */
2730 init_ide_data ();
2733 * Look for drive options: "hdx="
2735 if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
2736 const char *hd_words[] = {"none", "noprobe", "nowerr", "cdrom",
2737 "serialize", "autotune", "noautotune",
2738 "slow", "swapdata", "bswap", "flash",
2739 "remap", "noremap", "scsi", NULL};
2740 unit = s[2] - 'a';
2741 hw = unit / MAX_DRIVES;
2742 unit = unit % MAX_DRIVES;
2743 hwif = &ide_hwifs[hw];
2744 drive = &hwif->drives[unit];
2745 if (strncmp(s + 4, "ide-", 4) == 0) {
2746 strncpy(drive->driver_req, s + 4, 9);
2747 goto done;
2750 * Look for last lun option: "hdxlun="
2752 if (s[3] == 'l' && s[4] == 'u' && s[5] == 'n') {
2753 if (match_parm(&s[6], NULL, vals, 1) != 1)
2754 goto bad_option;
2755 if (vals[0] >= 0 && vals[0] <= 7) {
2756 drive->last_lun = vals[0];
2757 drive->forced_lun = 1;
2758 } else
2759 printk(" -- BAD LAST LUN! Expected value from 0 to 7");
2760 goto done;
2762 switch (match_parm(&s[3], hd_words, vals, 3)) {
2763 case -1: /* "none" */
2764 drive->nobios = 1; /* drop into "noprobe" */
2765 case -2: /* "noprobe" */
2766 drive->noprobe = 1;
2767 goto done;
2768 case -3: /* "nowerr" */
2769 drive->bad_wstat = BAD_R_STAT;
2770 hwif->noprobe = 0;
2771 goto done;
2772 case -4: /* "cdrom" */
2773 drive->present = 1;
2774 drive->media = ide_cdrom;
2775 hwif->noprobe = 0;
2776 goto done;
2777 case -5: /* "serialize" */
2778 printk(" -- USE \"ide%d=serialize\" INSTEAD", hw);
2779 goto do_serialize;
2780 case -6: /* "autotune" */
2781 drive->autotune = 1;
2782 goto done;
2783 case -7: /* "noautotune" */
2784 drive->autotune = 2;
2785 goto done;
2786 case -8: /* "slow" */
2787 drive->slow = 1;
2788 goto done;
2789 case -9: /* "swapdata" or "bswap" */
2790 case -10:
2791 drive->bswap = 1;
2792 goto done;
2793 case -11: /* "flash" */
2794 drive->ata_flash = 1;
2795 goto done;
2796 case -12: /* "remap" */
2797 drive->remap_0_to_1 = 1;
2798 goto done;
2799 case -13: /* "noremap" */
2800 drive->remap_0_to_1 = 2;
2801 goto done;
2802 case -14: /* "scsi" */
2803 #if defined(CONFIG_BLK_DEV_IDESCSI) && defined(CONFIG_SCSI)
2804 drive->scsi = 1;
2805 goto done;
2806 #else
2807 drive->scsi = 0;
2808 goto bad_option;
2809 #endif /* defined(CONFIG_BLK_DEV_IDESCSI) && defined(CONFIG_SCSI) */
2810 case 3: /* cyl,head,sect */
2811 drive->media = ide_disk;
2812 drive->cyl = drive->bios_cyl = vals[0];
2813 drive->head = drive->bios_head = vals[1];
2814 drive->sect = drive->bios_sect = vals[2];
2815 drive->present = 1;
2816 drive->forced_geom = 1;
2817 hwif->noprobe = 0;
2818 goto done;
2819 default:
2820 goto bad_option;
2824 #if defined(CONFIG_BLK_DEV_VIA82CXXX)
2826 * Look for drive option "splitfifo=..."
2829 if (s[0] == 's' && s[1] == 'p' && s[2] == 'l' &&
2830 s[3] == 'i' && s[4] == 't' && s[5] == 'f' &&
2831 s[6] == 'i' && s[7] == 'f' && s[8] == 'o') {
2832 byte tmp = 0x3a; /* default config byte */
2834 i = match_parm(&s[9], NULL, vals, 3);
2835 switch(i) {
2836 case 3:
2837 tmp &= 0xf0;
2838 if ((vals[1] > 0) && (vals[1] < 5)) {
2839 /* sets threshold for primary Channel: */
2840 byte x = 4 - vals[1];
2841 tmp |= (x << 2);
2843 else
2844 goto bad_option;
2845 if ((vals[2] > 0) && (vals[2] < 5)) {
2846 /* sets threshold for secondary Channel: */
2847 byte x = 4 - vals[2];
2848 tmp |= x;
2850 else
2851 goto bad_option;
2852 case 1:
2853 /* set the FIFO config between channels to 0: */
2854 tmp &= 0x9f;
2855 /* set the needed FIFO config between channels: */
2856 if (vals[0] == 1) /* primary fifo only */
2857 tmp |= 0x10;
2858 else if (vals[0] == 2) /* secondary fifo only */
2859 tmp |= 0x70;
2860 else if (vals[0] == 4) /* other shared fifo config */
2861 tmp |= 0x50;
2862 else if (vals[0] == 3) /* default config */
2863 tmp |= 0x30;
2864 else
2865 goto bad_option;
2866 break;
2867 default:
2868 goto bad_option;
2870 /* set the found option in fifoconfig */
2871 fifoconfig = tmp;
2872 goto done;
2874 #endif /* defined(CONFIG_BLK_DEV_VIA82CXXX) */
2876 if (s[0] != 'i' || s[1] != 'd' || s[2] != 'e')
2877 goto bad_option;
2879 * Look for bus speed option: "idebus="
2881 if (s[3] == 'b' && s[4] == 'u' && s[5] == 's') {
2882 if (match_parm(&s[6], NULL, vals, 1) != 1)
2883 goto bad_option;
2884 if (vals[0] >= 20 && vals[0] <= 66)
2885 idebus_parameter = vals[0];
2886 else
2887 printk(" -- BAD BUS SPEED! Expected value from 20 to 66");
2888 goto done;
2891 * Look for interface options: "idex="
2893 if (s[3] >= '0' && s[3] <= max_hwif) {
2895 * Be VERY CAREFUL changing this: note hardcoded indexes below
2896 * -8,-9,-10 : are reserved for future idex calls to ease the hardcoding.
2898 const char *ide_words[] = {
2899 "noprobe", "serialize", "autotune", "noautotune", "reset", "dma", "ata66",
2900 "minus8", "minus9", "minus10",
2901 "four", "qd6580", "ht6560b", "cmd640_vlb", "dtc2278", "umc8672", "ali14xx", "dc4030", NULL };
2902 hw = s[3] - '0';
2903 hwif = &ide_hwifs[hw];
2904 i = match_parm(&s[4], ide_words, vals, 3);
2907 * Cryptic check to ensure chipset not already set for hwif:
2909 if (i > 0 || i <= -11) { /* is parameter a chipset name? */
2910 if (hwif->chipset != ide_unknown)
2911 goto bad_option; /* chipset already specified */
2912 if (i <= -11 && i != -18 && hw != 0)
2913 goto bad_hwif; /* chipset drivers are for "ide0=" only */
2914 if (i <= -11 && i != -18 && ide_hwifs[hw+1].chipset != ide_unknown)
2915 goto bad_option; /* chipset for 2nd port already specified */
2916 printk("\n");
2919 switch (i) {
2920 #ifdef CONFIG_BLK_DEV_PDC4030
2921 case -18: /* "dc4030" */
2923 extern void init_pdc4030(void);
2924 init_pdc4030();
2925 goto done;
2927 #endif /* CONFIG_BLK_DEV_PDC4030 */
2928 #ifdef CONFIG_BLK_DEV_ALI14XX
2929 case -17: /* "ali14xx" */
2931 extern void init_ali14xx (void);
2932 init_ali14xx();
2933 goto done;
2935 #endif /* CONFIG_BLK_DEV_ALI14XX */
2936 #ifdef CONFIG_BLK_DEV_UMC8672
2937 case -16: /* "umc8672" */
2939 extern void init_umc8672 (void);
2940 init_umc8672();
2941 goto done;
2943 #endif /* CONFIG_BLK_DEV_UMC8672 */
2944 #ifdef CONFIG_BLK_DEV_DTC2278
2945 case -15: /* "dtc2278" */
2947 extern void init_dtc2278 (void);
2948 init_dtc2278();
2949 goto done;
2951 #endif /* CONFIG_BLK_DEV_DTC2278 */
2952 #ifdef CONFIG_BLK_DEV_CMD640
2953 case -14: /* "cmd640_vlb" */
2955 extern int cmd640_vlb; /* flag for cmd640.c */
2956 cmd640_vlb = 1;
2957 goto done;
2959 #endif /* CONFIG_BLK_DEV_CMD640 */
2960 #ifdef CONFIG_BLK_DEV_HT6560B
2961 case -13: /* "ht6560b" */
2963 extern void init_ht6560b (void);
2964 init_ht6560b();
2965 goto done;
2967 #endif /* CONFIG_BLK_DEV_HT6560B */
2968 #if CONFIG_BLK_DEV_QD6580
2969 case -12: /* "qd6580" */
2971 extern void init_qd6580 (void);
2972 init_qd6580();
2973 goto done;
2975 #endif /* CONFIG_BLK_DEV_QD6580 */
2976 #ifdef CONFIG_BLK_DEV_4DRIVES
2977 case -11: /* "four" drives on one set of ports */
2979 ide_hwif_t *mate = &ide_hwifs[hw^1];
2980 mate->drives[0].select.all ^= 0x20;
2981 mate->drives[1].select.all ^= 0x20;
2982 hwif->chipset = mate->chipset = ide_4drives;
2983 mate->irq = hwif->irq;
2984 memcpy(mate->io_ports, hwif->io_ports, sizeof(hwif->io_ports));
2985 goto do_serialize;
2987 #endif /* CONFIG_BLK_DEV_4DRIVES */
2988 case -10: /* minus10 */
2989 case -9: /* minus9 */
2990 case -8: /* minus8 */
2991 goto bad_option;
2992 case -7: /* ata66 */
2993 #ifdef CONFIG_BLK_DEV_IDEPCI
2994 hwif->udma_four = 1;
2995 goto done;
2996 #else /* !CONFIG_BLK_DEV_IDEPCI */
2997 hwif->udma_four = 0;
2998 goto bad_hwif;
2999 #endif /* CONFIG_BLK_DEV_IDEPCI */
3000 case -6: /* dma */
3001 hwif->autodma = 1;
3002 goto done;
3003 case -5: /* "reset" */
3004 hwif->reset = 1;
3005 goto done;
3006 case -4: /* "noautotune" */
3007 hwif->drives[0].autotune = 2;
3008 hwif->drives[1].autotune = 2;
3009 goto done;
3010 case -3: /* "autotune" */
3011 hwif->drives[0].autotune = 1;
3012 hwif->drives[1].autotune = 1;
3013 goto done;
3014 case -2: /* "serialize" */
3015 do_serialize:
3016 hwif->mate = &ide_hwifs[hw^1];
3017 hwif->mate->mate = hwif;
3018 hwif->serialized = hwif->mate->serialized = 1;
3019 goto done;
3021 case -1: /* "noprobe" */
3022 hwif->noprobe = 1;
3023 goto done;
3025 case 1: /* base */
3026 vals[1] = vals[0] + 0x206; /* default ctl */
3027 case 2: /* base,ctl */
3028 vals[2] = 0; /* default irq = probe for it */
3029 case 3: /* base,ctl,irq */
3030 hwif->hw.irq = vals[2];
3031 ide_init_hwif_ports(&hwif->hw, (ide_ioreg_t) vals[0], (ide_ioreg_t) vals[1], &hwif->irq);
3032 memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->io_ports));
3033 hwif->irq = vals[2];
3034 hwif->noprobe = 0;
3035 hwif->chipset = ide_generic;
3036 goto done;
3038 case 0: goto bad_option;
3039 default:
3040 printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
3041 return 0;
3044 bad_option:
3045 printk(" -- BAD OPTION\n");
3046 return 0;
3047 bad_hwif:
3048 printk("-- NOT SUPPORTED ON ide%d", hw);
3049 done:
3050 printk("\n");
3051 return 0;
3055 * probe_for_hwifs() finds/initializes "known" IDE interfaces
3057 static void __init probe_for_hwifs (void)
3059 #ifdef CONFIG_PCI
3060 if (pci_present())
3062 #ifdef CONFIG_BLK_DEV_IDEPCI
3063 ide_scan_pcibus();
3064 #else
3065 #ifdef CONFIG_BLK_DEV_RZ1000
3067 extern void ide_probe_for_rz100x(void);
3068 ide_probe_for_rz100x();
3070 #endif /* CONFIG_BLK_DEV_RZ1000 */
3071 #endif /* CONFIG_BLK_DEV_IDEPCI */
3073 #endif /* CONFIG_PCI */
3075 #ifdef CONFIG_BLK_DEV_CMD640
3077 extern void ide_probe_for_cmd640x(void);
3078 ide_probe_for_cmd640x();
3080 #endif /* CONFIG_BLK_DEV_CMD640 */
3081 #ifdef CONFIG_BLK_DEV_PDC4030
3083 extern int ide_probe_for_pdc4030(void);
3084 (void) ide_probe_for_pdc4030();
3086 #endif /* CONFIG_BLK_DEV_PDC4030 */
3087 #ifdef CONFIG_BLK_DEV_IDE_PMAC
3089 extern void pmac_ide_probe(void);
3090 pmac_ide_probe();
3092 #endif /* CONFIG_BLK_DEV_IDE_PMAC */
3093 #ifdef CONFIG_BLK_DEV_IDE_ICSIDE
3095 extern void icside_init(void);
3096 icside_init();
3098 #endif /* CONFIG_BLK_DEV_IDE_ICSIDE */
3099 #ifdef CONFIG_BLK_DEV_IDE_RAPIDE
3101 extern void rapide_init(void);
3102 rapide_init();
3104 #endif /* CONFIG_BLK_DEV_IDE_RAPIDE */
3105 #ifdef CONFIG_BLK_DEV_GAYLE
3107 extern void gayle_init(void);
3108 gayle_init();
3110 #endif /* CONFIG_BLK_DEV_GAYLE */
3111 #ifdef CONFIG_BLK_DEV_FALCON_IDE
3113 extern void falconide_init(void);
3114 falconide_init();
3116 #endif /* CONFIG_BLK_DEV_FALCON_IDE */
3117 #ifdef CONFIG_BLK_DEV_MAC_IDE
3119 extern void macide_init(void);
3120 macide_init();
3122 #endif /* CONFIG_BLK_DEV_MAC_IDE */
3123 #ifdef CONFIG_BLK_DEV_BUDDHA
3125 extern void buddha_init(void);
3126 buddha_init();
3128 #endif /* CONFIG_BLK_DEV_BUDDHA */
3131 void __init ide_init_builtin_drivers (void)
3134 * Probe for special PCI and other "known" interface chipsets
3136 probe_for_hwifs ();
3138 #ifdef CONFIG_BLK_DEV_IDE
3139 #if defined(__mc68000__) || defined(CONFIG_APUS)
3140 if (ide_hwifs[0].io_ports[IDE_DATA_OFFSET]) {
3141 ide_get_lock(&ide_lock, NULL, NULL); /* for atari only */
3142 disable_irq(ide_hwifs[0].irq);
3144 #endif /* __mc68000__ || CONFIG_APUS */
3146 (void) ideprobe_init();
3148 #if defined(__mc68000__) || defined(CONFIG_APUS)
3149 if (ide_hwifs[0].io_ports[IDE_DATA_OFFSET]) {
3150 enable_irq(ide_hwifs[0].irq);
3151 ide_release_lock(&ide_lock); /* for atari only */
3153 #endif /* __mc68000__ || CONFIG_APUS */
3154 #endif /* CONFIG_BLK_DEV_IDE */
3156 #ifdef CONFIG_PROC_FS
3157 proc_ide_create();
3158 #endif
3161 * Attempt to match drivers for the available drives
3163 #ifdef CONFIG_BLK_DEV_IDEDISK
3164 (void) idedisk_init();
3165 #endif /* CONFIG_BLK_DEV_IDEDISK */
3166 #ifdef CONFIG_BLK_DEV_IDECD
3167 (void) ide_cdrom_init();
3168 #endif /* CONFIG_BLK_DEV_IDECD */
3169 #ifdef CONFIG_BLK_DEV_IDETAPE
3170 (void) idetape_init();
3171 #endif /* CONFIG_BLK_DEV_IDETAPE */
3172 #ifdef CONFIG_BLK_DEV_IDEFLOPPY
3173 (void) idefloppy_init();
3174 #endif /* CONFIG_BLK_DEV_IDEFLOPPY */
3175 #ifdef CONFIG_BLK_DEV_IDESCSI
3176 #ifdef CONFIG_SCSI
3177 (void) idescsi_init();
3178 #else
3179 #warning ide scsi-emulation selected but no SCSI-subsystem in kernel
3180 #endif
3181 #endif /* CONFIG_BLK_DEV_IDESCSI */
3184 static int default_cleanup (ide_drive_t *drive)
3186 return ide_unregister_subdriver(drive);
3189 static ide_startstop_t default_do_request(ide_drive_t *drive, struct request *rq, unsigned long block)
3191 ide_end_request(0, HWGROUP(drive));
3192 return ide_stopped;
3195 static void default_end_request (byte uptodate, ide_hwgroup_t *hwgroup)
3197 ide_end_request(uptodate, hwgroup);
3200 static int default_ioctl (ide_drive_t *drive, struct inode *inode, struct file *file,
3201 unsigned int cmd, unsigned long arg)
3203 return -EIO;
3206 static int default_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
3208 drive->usage--;
3209 return -EIO;
3212 static void default_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
3216 static int default_check_media_change (ide_drive_t *drive)
3218 return 1;
3221 static void default_pre_reset (ide_drive_t *drive)
3225 static unsigned long default_capacity (ide_drive_t *drive)
3227 return 0x7fffffff; /* cdrom or tape */
3230 static ide_startstop_t default_special (ide_drive_t *drive)
3232 special_t *s = &drive->special;
3234 s->all = 0;
3235 drive->mult_req = 0;
3236 return ide_stopped;
3239 static void setup_driver_defaults (ide_drive_t *drive)
3241 ide_driver_t *d = drive->driver;
3243 if (d->cleanup == NULL) d->cleanup = default_cleanup;
3244 if (d->do_request == NULL) d->do_request = default_do_request;
3245 if (d->end_request == NULL) d->end_request = default_end_request;
3246 if (d->ioctl == NULL) d->ioctl = default_ioctl;
3247 if (d->open == NULL) d->open = default_open;
3248 if (d->release == NULL) d->release = default_release;
3249 if (d->media_change == NULL) d->media_change = default_check_media_change;
3250 if (d->pre_reset == NULL) d->pre_reset = default_pre_reset;
3251 if (d->capacity == NULL) d->capacity = default_capacity;
3252 if (d->special == NULL) d->special = default_special;
3255 ide_drive_t *ide_scan_devices (byte media, const char *name, ide_driver_t *driver, int n)
3257 unsigned int unit, index, i;
3259 for (index = 0; index < MAX_HWIFS; ++index)
3260 if (ide_hwifs[index].present) goto search;
3261 ide_init_module(IDE_PROBE_MODULE);
3262 search:
3263 for (index = 0, i = 0; index < MAX_HWIFS; ++index) {
3264 ide_hwif_t *hwif = &ide_hwifs[index];
3265 if (!hwif->present)
3266 continue;
3267 for (unit = 0; unit < MAX_DRIVES; ++unit) {
3268 ide_drive_t *drive = &hwif->drives[unit];
3269 char *req = drive->driver_req;
3270 if (*req && !strstr(name, req))
3271 continue;
3272 if (drive->present && drive->media == media && drive->driver == driver && ++i > n)
3273 return drive;
3276 return NULL;
3279 int ide_register_subdriver (ide_drive_t *drive, ide_driver_t *driver, int version)
3281 unsigned long flags;
3283 save_flags(flags); /* all CPUs */
3284 cli(); /* all CPUs */
3285 if (version != IDE_SUBDRIVER_VERSION || !drive->present || drive->driver != NULL || drive->busy || drive->usage) {
3286 restore_flags(flags); /* all CPUs */
3287 return 1;
3289 drive->driver = driver;
3290 setup_driver_defaults(drive);
3291 restore_flags(flags); /* all CPUs */
3292 if (drive->autotune != 2) {
3293 if (driver->supports_dma && HWIF(drive)->dmaproc != NULL)
3294 (void) (HWIF(drive)->dmaproc(ide_dma_check, drive));
3295 drive->dsc_overlap = (drive->next != drive && driver->supports_dsc_overlap);
3296 drive->nice1 = 1;
3298 drive->revalidate = 1;
3299 #ifdef CONFIG_PROC_FS
3300 ide_add_proc_entries(drive->proc, generic_subdriver_entries, drive);
3301 ide_add_proc_entries(drive->proc, driver->proc, drive);
3302 #endif
3303 return 0;
3306 int ide_unregister_subdriver (ide_drive_t *drive)
3308 unsigned long flags;
3310 save_flags(flags); /* all CPUs */
3311 cli(); /* all CPUs */
3312 if (drive->usage || drive->busy || drive->driver == NULL || DRIVER(drive)->busy) {
3313 restore_flags(flags); /* all CPUs */
3314 return 1;
3316 #ifdef CONFIG_PROC_FS
3317 ide_remove_proc_entries(drive->proc, DRIVER(drive)->proc);
3318 ide_remove_proc_entries(drive->proc, generic_subdriver_entries);
3319 #endif
3320 auto_remove_settings(drive);
3321 drive->driver = NULL;
3322 restore_flags(flags); /* all CPUs */
3323 return 0;
3326 int ide_register_module (ide_module_t *module)
3328 ide_module_t *p = ide_modules;
3330 while (p) {
3331 if (p == module)
3332 return 1;
3333 p = p->next;
3335 module->next = ide_modules;
3336 ide_modules = module;
3337 revalidate_drives();
3338 return 0;
3341 void ide_unregister_module (ide_module_t *module)
3343 ide_module_t **p;
3345 for (p = &ide_modules; (*p) && (*p) != module; p = &((*p)->next));
3346 if (*p)
3347 *p = (*p)->next;
3350 struct block_device_operations ide_fops[] = {{
3351 open: ide_open,
3352 release: ide_release,
3353 ioctl: ide_ioctl,
3354 check_media_change: ide_check_media_change,
3355 revalidate: ide_revalidate_disk
3358 EXPORT_SYMBOL(ide_hwifs);
3359 EXPORT_SYMBOL(ide_register_module);
3360 EXPORT_SYMBOL(ide_unregister_module);
3361 EXPORT_SYMBOL(ide_spin_wait_hwgroup);
3364 * Probe module
3366 EXPORT_SYMBOL(drive_is_flashcard);
3367 EXPORT_SYMBOL(ide_timer_expiry);
3368 EXPORT_SYMBOL(ide_intr);
3369 EXPORT_SYMBOL(ide_geninit);
3370 EXPORT_SYMBOL(ide_fops);
3371 EXPORT_SYMBOL(ide_get_queue);
3372 EXPORT_SYMBOL(do_ide0_request);
3373 EXPORT_SYMBOL(ide_add_generic_settings);
3374 #if MAX_HWIFS > 1
3375 EXPORT_SYMBOL(do_ide1_request);
3376 #endif /* MAX_HWIFS > 1 */
3377 #if MAX_HWIFS > 2
3378 EXPORT_SYMBOL(do_ide2_request);
3379 #endif /* MAX_HWIFS > 2 */
3380 #if MAX_HWIFS > 3
3381 EXPORT_SYMBOL(do_ide3_request);
3382 #endif /* MAX_HWIFS > 3 */
3383 #if MAX_HWIFS > 4
3384 EXPORT_SYMBOL(do_ide4_request);
3385 #endif /* MAX_HWIFS > 4 */
3386 #if MAX_HWIFS > 5
3387 EXPORT_SYMBOL(do_ide5_request);
3388 #endif /* MAX_HWIFS > 5 */
3389 #if MAX_HWIFS > 6
3390 EXPORT_SYMBOL(do_ide6_request);
3391 #endif /* MAX_HWIFS > 6 */
3392 #if MAX_HWIFS > 7
3393 EXPORT_SYMBOL(do_ide7_request);
3394 #endif /* MAX_HWIFS > 7 */
3395 #if MAX_HWIFS > 8
3396 EXPORT_SYMBOL(do_ide8_request);
3397 #endif /* MAX_HWIFS > 8 */
3398 #if MAX_HWIFS > 9
3399 EXPORT_SYMBOL(do_ide9_request);
3400 #endif /* MAX_HWIFS > 9 */
3403 * Driver module
3405 EXPORT_SYMBOL(ide_scan_devices);
3406 EXPORT_SYMBOL(ide_register_subdriver);
3407 EXPORT_SYMBOL(ide_unregister_subdriver);
3408 EXPORT_SYMBOL(ide_replace_subdriver);
3409 EXPORT_SYMBOL(ide_input_data);
3410 EXPORT_SYMBOL(ide_output_data);
3411 EXPORT_SYMBOL(atapi_input_bytes);
3412 EXPORT_SYMBOL(atapi_output_bytes);
3413 EXPORT_SYMBOL(ide_set_handler);
3414 EXPORT_SYMBOL(ide_dump_status);
3415 EXPORT_SYMBOL(ide_error);
3416 EXPORT_SYMBOL(ide_fixstring);
3417 EXPORT_SYMBOL(ide_wait_stat);
3418 EXPORT_SYMBOL(ide_do_reset);
3419 EXPORT_SYMBOL(ide_init_drive_cmd);
3420 EXPORT_SYMBOL(ide_do_drive_cmd);
3421 EXPORT_SYMBOL(ide_end_drive_cmd);
3422 EXPORT_SYMBOL(ide_end_request);
3423 EXPORT_SYMBOL(ide_revalidate_disk);
3424 EXPORT_SYMBOL(ide_cmd);
3425 EXPORT_SYMBOL(ide_wait_cmd);
3426 EXPORT_SYMBOL(ide_delay_50ms);
3427 EXPORT_SYMBOL(ide_stall_queue);
3428 #ifdef CONFIG_PROC_FS
3429 EXPORT_SYMBOL(ide_add_proc_entries);
3430 EXPORT_SYMBOL(ide_remove_proc_entries);
3431 EXPORT_SYMBOL(proc_ide_read_geometry);
3432 EXPORT_SYMBOL(create_proc_ide_interfaces);
3433 #endif
3434 EXPORT_SYMBOL(ide_add_setting);
3435 EXPORT_SYMBOL(ide_remove_setting);
3437 EXPORT_SYMBOL(ide_register_hw);
3438 EXPORT_SYMBOL(ide_register);
3439 EXPORT_SYMBOL(ide_unregister);
3440 EXPORT_SYMBOL(ide_setup_ports);
3442 EXPORT_SYMBOL(get_info_ptr);
3443 EXPORT_SYMBOL(current_capacity);
3446 * This is gets invoked once during initialization, to set *everything* up
3448 int __init ide_init (void)
3450 static char banner_printed = 0;
3452 if (!banner_printed) {
3453 printk(KERN_INFO "Uniform Multi-Platform E-IDE driver " REVISION "\n");
3454 banner_printed = 1;
3457 init_ide_data ();
3459 initializing = 1;
3460 ide_init_builtin_drivers();
3461 initializing = 0;
3463 return 0;
3466 static void __init parse_options (char *line)
3468 char *next = line;
3470 if (line == NULL || !*line)
3471 return;
3472 while ((line = next) != NULL) {
3473 if ((next = strchr(line,' ')) != NULL)
3474 *next++ = 0;
3475 if (!strncmp(line,"ide",3) ||
3476 #ifdef CONFIG_BLK_DEV_VIA82CXXX
3477 !strncmp(line,"splitfifo",9) ||
3478 #endif /* CONFIG_BLK_DEV_VIA82CXXX */
3479 !strncmp(line,"hdxlun",6) ||
3480 (!strncmp(line,"hd",2) && line[2] != '='))
3481 (void) ide_setup(line);
3485 #ifdef MODULE
3486 char *options = NULL;
3487 MODULE_PARM(options,"s");
3489 int init_module (void)
3491 parse_options(options);
3492 return ide_init();
3495 void cleanup_module (void)
3497 int index;
3499 for (index = 0; index < MAX_HWIFS; ++index) {
3500 ide_unregister(index);
3501 #ifdef CONFIG_BLK_DEV_IDEDMA
3502 if (ide_hwifs[index].dma_base)
3503 (void) ide_release_dma(&ide_hwifs[index]);
3504 #endif /* CONFIG_BLK_DEV_IDEDMA */
3506 #ifdef CONFIG_PROC_FS
3507 proc_ide_destroy();
3508 #endif
3511 #else /* !MODULE */
3513 static int parse_ide_setup (char *line)
3515 parse_options(line);
3516 return 0;
3518 __setup("", parse_ide_setup);
3520 #endif /* MODULE */