Import 2.3.25pre1
[davej-history.git] / drivers / block / ide.c
blobf0c9d58da05fc82dcc0db7c393f3490eba783aac
1 /*
2 * linux/drivers/block/ide.c Version 6.20 July 10, 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.
110 * Some additional driver compile-time options are in ./include/linux/ide.h
112 * To do, in likely order of completion:
113 * - modify kernel to obtain BIOS geometry for drives on 2nd/3rd/4th i/f
116 #define REVISION "Revision: 6.20"
117 #define VERSION "Id: ide.c 6.20 1999/07/10"
119 #undef REALLY_SLOW_IO /* most systems can safely undef this */
121 #define _IDE_C /* Tell ide.h it's really us */
123 #include <linux/config.h>
124 #include <linux/module.h>
125 #include <linux/types.h>
126 #include <linux/string.h>
127 #include <linux/kernel.h>
128 #include <linux/timer.h>
129 #include <linux/mm.h>
130 #include <linux/interrupt.h>
131 #include <linux/major.h>
132 #include <linux/errno.h>
133 #include <linux/genhd.h>
134 #include <linux/blkpg.h>
135 #include <linux/malloc.h>
136 #include <linux/pci.h>
137 #include <linux/delay.h>
138 #include <linux/ide.h>
140 #include <asm/byteorder.h>
141 #include <asm/irq.h>
142 #include <asm/uaccess.h>
143 #include <asm/io.h>
144 #include <asm/bitops.h>
146 #include "ide_modes.h"
148 #ifdef CONFIG_KMOD
149 #include <linux/kmod.h>
150 #endif /* CONFIG_KMOD */
152 #ifdef CONFIG_BLK_DEV_VIA82CXXX
153 extern byte fifoconfig; /* defined in via82cxxx.c used by ide_setup() */
154 #endif /* CONFIG_BLK_DEV_VIA82CXXX */
156 static const byte ide_hwif_to_major[] = { IDE0_MAJOR, IDE1_MAJOR,
157 IDE2_MAJOR, IDE3_MAJOR,
158 IDE4_MAJOR, IDE5_MAJOR,
159 IDE6_MAJOR, IDE7_MAJOR,
160 IDE8_MAJOR, IDE9_MAJOR };
162 static int idebus_parameter; /* holds the "idebus=" parameter */
163 static int system_bus_speed; /* holds what we think is VESA/PCI bus speed */
164 static int initializing; /* set while initializing built-in drivers */
166 #if defined(__mc68000__) || defined(CONFIG_APUS)
168 * ide_lock is used by the Atari code to obtain access to the IDE interrupt,
169 * which is shared between several drivers.
171 static int ide_lock = 0;
172 #endif /* __mc68000__ || CONFIG_APUS */
175 * ide_modules keeps track of the available IDE chipset/probe/driver modules.
177 ide_module_t *ide_modules = NULL;
180 * This is declared extern in ide.h, for access by other IDE modules:
182 ide_hwif_t ide_hwifs[MAX_HWIFS]; /* master data repository */
184 #if (DISK_RECOVERY_TIME > 0)
186 * For really screwy hardware (hey, at least it *can* be used with Linux)
187 * we can enforce a minimum delay time between successive operations.
189 static unsigned long read_timer(void)
191 unsigned long t, flags;
192 int i;
194 __save_flags(flags); /* local CPU only */
195 __cli(); /* local CPU only */
196 t = jiffies * 11932;
197 outb_p(0, 0x43);
198 i = inb_p(0x40);
199 i |= inb(0x40) << 8;
200 __restore_flags(flags); /* local CPU only */
201 return (t - i);
203 #endif /* DISK_RECOVERY_TIME */
205 static inline void set_recovery_timer (ide_hwif_t *hwif)
207 #if (DISK_RECOVERY_TIME > 0)
208 hwif->last_time = read_timer();
209 #endif /* DISK_RECOVERY_TIME */
213 * Do not even *think* about calling this!
215 static void init_hwif_data (unsigned int index)
217 unsigned int unit;
218 hw_regs_t hw;
219 ide_hwif_t *hwif = &ide_hwifs[index];
221 /* bulk initialize hwif & drive info with zeros */
222 memset(hwif, 0, sizeof(ide_hwif_t));
223 memset(&hw, 0, sizeof(hw_regs_t));
225 /* fill in any non-zero initial values */
226 hwif->index = index;
227 ide_init_hwif_ports(&hw, ide_default_io_base(index), 0, &hwif->irq);
228 memcpy(&hwif->hw, &hw, sizeof(hw));
229 memcpy(hwif->io_ports, hw.io_ports, sizeof(hw.io_ports));
230 hwif->noprobe = !hwif->io_ports[IDE_DATA_OFFSET];
231 #ifdef CONFIG_BLK_DEV_HD
232 if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA)
233 hwif->noprobe = 1; /* may be overridden by ide_setup() */
234 #endif /* CONFIG_BLK_DEV_HD */
235 hwif->major = ide_hwif_to_major[index];
236 hwif->name[0] = 'i';
237 hwif->name[1] = 'd';
238 hwif->name[2] = 'e';
239 hwif->name[3] = '0' + index;
240 for (unit = 0; unit < MAX_DRIVES; ++unit) {
241 ide_drive_t *drive = &hwif->drives[unit];
243 drive->media = ide_disk;
244 drive->select.all = (unit<<4)|0xa0;
245 drive->hwif = hwif;
246 drive->ctl = 0x08;
247 drive->ready_stat = READY_STAT;
248 drive->bad_wstat = BAD_W_STAT;
249 drive->special.b.recalibrate = 1;
250 drive->special.b.set_geometry = 1;
251 drive->name[0] = 'h';
252 drive->name[1] = 'd';
253 drive->name[2] = 'a' + (index * MAX_DRIVES) + unit;
254 init_waitqueue_head(&drive->wqueue);
259 * init_ide_data() sets reasonable default values into all fields
260 * of all instances of the hwifs and drives, but only on the first call.
261 * Subsequent calls have no effect (they don't wipe out anything).
263 * This routine is normally called at driver initialization time,
264 * but may also be called MUCH earlier during kernel "command-line"
265 * parameter processing. As such, we cannot depend on any other parts
266 * of the kernel (such as memory allocation) to be functioning yet.
268 * This is too bad, as otherwise we could dynamically allocate the
269 * ide_drive_t structs as needed, rather than always consuming memory
270 * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
272 #define MAGIC_COOKIE 0x12345678
273 static void init_ide_data (void)
275 unsigned int index;
276 static unsigned long magic_cookie = MAGIC_COOKIE;
278 if (magic_cookie != MAGIC_COOKIE)
279 return; /* already initialized */
280 magic_cookie = 0;
282 /* Initialise all interface structures */
283 for (index = 0; index < MAX_HWIFS; ++index)
284 init_hwif_data(index);
286 /* Add default hw interfaces */
287 ide_init_default_hwifs();
289 idebus_parameter = 0;
290 system_bus_speed = 0;
294 * CompactFlash cards and their brethern pretend to be removable hard disks, except:
295 * (1) they never have a slave unit, and
296 * (2) they don't have doorlock mechanisms.
297 * This test catches them, and is invoked elsewhere when setting appropriate config bits.
299 * FIXME: This treatment is probably applicable for *all* PCMCIA (PC CARD) devices,
300 * so in linux 2.3.x we should change this to just treat all PCMCIA drives this way,
301 * and get rid of the model-name tests below (too big of an interface change for 2.2.x).
302 * At that time, we might also consider parameterizing the timeouts and retries,
303 * since these are MUCH faster than mechanical drives. -M.Lord
305 int drive_is_flashcard (ide_drive_t *drive)
307 struct hd_driveid *id = drive->id;
309 if (drive->removable && id != NULL) {
310 if (!strncmp(id->model, "KODAK ATA_FLASH", 15) /* Kodak */
311 || !strncmp(id->model, "Hitachi CV", 10) /* Hitachi */
312 || !strncmp(id->model, "SunDisk SDCFB", 13) /* SunDisk */
313 || !strncmp(id->model, "HAGIWARA HPC", 12) /* Hagiwara */
314 || !strncmp(id->model, "ATA_FLASH", 9)) /* Simple Tech */
316 return 1; /* yes, it is a flash memory card */
319 return 0; /* no, it is not a flash memory card */
323 * ide_system_bus_speed() returns what we think is the system VESA/PCI
324 * bus speed (in MHz). This is used for calculating interface PIO timings.
325 * The default is 40 for known PCI systems, 50 otherwise.
326 * The "idebus=xx" parameter can be used to override this value.
327 * The actual value to be used is computed/displayed the first time through.
329 int ide_system_bus_speed (void)
331 if (!system_bus_speed) {
332 if (idebus_parameter)
333 system_bus_speed = idebus_parameter; /* user supplied value */
334 #ifdef CONFIG_PCI
335 else if (pci_present())
336 system_bus_speed = 40; /* safe default value for PCI */
337 #endif /* CONFIG_PCI */
338 else
339 system_bus_speed = 50; /* safe default value for VESA and PCI */
340 printk("ide: Assuming %dMHz system bus speed for PIO modes%s\n", system_bus_speed,
341 idebus_parameter ? "" : "; override with idebus=xx");
343 return system_bus_speed;
346 #if SUPPORT_VLB_SYNC
348 * Some localbus EIDE interfaces require a special access sequence
349 * when using 32-bit I/O instructions to transfer data. We call this
350 * the "vlb_sync" sequence, which consists of three successive reads
351 * of the sector count register location, with interrupts disabled
352 * to ensure that the reads all happen together.
354 static inline void do_vlb_sync (ide_ioreg_t port) {
355 (void) inb (port);
356 (void) inb (port);
357 (void) inb (port);
359 #endif /* SUPPORT_VLB_SYNC */
362 * This is used for most PIO data transfers *from* the IDE interface
364 void ide_input_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
366 byte io_32bit = drive->io_32bit;
368 if (io_32bit) {
369 #if SUPPORT_VLB_SYNC
370 if (io_32bit & 2) {
371 unsigned long flags;
372 __save_flags(flags); /* local CPU only */
373 __cli(); /* local CPU only */
374 do_vlb_sync(IDE_NSECTOR_REG);
375 insl(IDE_DATA_REG, buffer, wcount);
376 __restore_flags(flags); /* local CPU only */
377 } else
378 #endif /* SUPPORT_VLB_SYNC */
379 insl(IDE_DATA_REG, buffer, wcount);
380 } else {
381 #if SUPPORT_SLOW_DATA_PORTS
382 if (drive->slow) {
383 unsigned short *ptr = (unsigned short *) buffer;
384 while (wcount--) {
385 *ptr++ = inw_p(IDE_DATA_REG);
386 *ptr++ = inw_p(IDE_DATA_REG);
388 } else
389 #endif /* SUPPORT_SLOW_DATA_PORTS */
390 insw(IDE_DATA_REG, buffer, wcount<<1);
395 * This is used for most PIO data transfers *to* the IDE interface
397 void ide_output_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
399 byte io_32bit = drive->io_32bit;
401 if (io_32bit) {
402 #if SUPPORT_VLB_SYNC
403 if (io_32bit & 2) {
404 unsigned long flags;
405 __save_flags(flags); /* local CPU only */
406 __cli(); /* local CPU only */
407 do_vlb_sync(IDE_NSECTOR_REG);
408 outsl(IDE_DATA_REG, buffer, wcount);
409 __restore_flags(flags); /* local CPU only */
410 } else
411 #endif /* SUPPORT_VLB_SYNC */
412 outsl(IDE_DATA_REG, buffer, wcount);
413 } else {
414 #if SUPPORT_SLOW_DATA_PORTS
415 if (drive->slow) {
416 unsigned short *ptr = (unsigned short *) buffer;
417 while (wcount--) {
418 outw_p(*ptr++, IDE_DATA_REG);
419 outw_p(*ptr++, IDE_DATA_REG);
421 } else
422 #endif /* SUPPORT_SLOW_DATA_PORTS */
423 outsw(IDE_DATA_REG, buffer, wcount<<1);
428 * The following routines are mainly used by the ATAPI drivers.
430 * These routines will round up any request for an odd number of bytes,
431 * so if an odd bytecount is specified, be sure that there's at least one
432 * extra byte allocated for the buffer.
434 void atapi_input_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount)
436 ++bytecount;
437 #if defined(CONFIG_ATARI) || defined(CONFIG_Q40)
438 if (MACH_IS_ATARI || MACH_IS_Q40) {
439 /* Atari has a byte-swapped IDE interface */
440 insw_swapw(IDE_DATA_REG, buffer, bytecount / 2);
441 return;
443 #endif /* CONFIG_ATARI */
444 ide_input_data (drive, buffer, bytecount / 4);
445 if ((bytecount & 0x03) >= 2)
446 insw (IDE_DATA_REG, ((byte *)buffer) + (bytecount & ~0x03), 1);
449 void atapi_output_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount)
451 ++bytecount;
452 #if defined(CONFIG_ATARI) || defined(CONFIG_Q40)
453 if (MACH_IS_ATARI || MACH_IS_Q40) {
454 /* Atari has a byte-swapped IDE interface */
455 outsw_swapw(IDE_DATA_REG, buffer, bytecount / 2);
456 return;
458 #endif /* CONFIG_ATARI */
459 ide_output_data (drive, buffer, bytecount / 4);
460 if ((bytecount & 0x03) >= 2)
461 outsw (IDE_DATA_REG, ((byte *)buffer) + (bytecount & ~0x03), 1);
465 * Needed for PCI irq sharing
467 static inline int drive_is_ready (ide_drive_t *drive)
469 if (drive->waiting_for_dma)
470 return HWIF(drive)->dmaproc(ide_dma_test_irq, drive);
471 #if 0
472 udelay(1); /* need to guarantee 400ns since last command was issued */
473 #endif
474 if (GET_STAT() & BUSY_STAT)
475 return 0; /* drive busy: definitely not interrupting */
476 return 1; /* drive ready: *might* be interrupting */
480 * This is our end_request replacement function.
482 void ide_end_request(byte uptodate, ide_hwgroup_t *hwgroup)
484 struct request *rq;
485 unsigned long flags;
487 spin_lock_irqsave(&io_request_lock, flags);
488 rq = hwgroup->rq;
490 if (!end_that_request_first(rq, uptodate, hwgroup->drive->name)) {
491 add_blkdev_randomness(MAJOR(rq->rq_dev));
492 hwgroup->drive->queue = rq->next;
493 blk_dev[MAJOR(rq->rq_dev)].current_request = NULL;
494 hwgroup->rq = NULL;
495 end_that_request_last(rq);
497 spin_unlock_irqrestore(&io_request_lock, flags);
501 * The below two are helpers used when modifying the drive timeout.
503 static inline unsigned long set_timeout(ide_drive_t *drive, unsigned long timeout)
505 unsigned long foo = drive->timeout;
506 drive->timeout = timeout;
507 return foo;
510 #define restore_timeout(drive, old) (drive->timeout = old)
513 * This should get invoked any time we exit the driver to
514 * wait for an interrupt response from a drive. handler() points
515 * at the appropriate code to handle the next interrupt, and a
516 * timer is started to prevent us from waiting forever in case
517 * something goes wrong (see the ide_timer_expiry() handler later on).
519 void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler)
521 unsigned long flags;
522 ide_hwgroup_t *hwgroup = HWGROUP(drive);
524 spin_lock_irqsave(&hwgroup->spinlock, flags);
525 #ifdef DEBUG
526 if (hwgroup->handler != NULL) {
527 printk("%s: ide_set_handler: handler not null; old=%p, new=%p\n",
528 drive->name, hwgroup->handler, handler);
530 #endif
531 hwgroup->handler = handler;
532 /* 0 means don't timeout */
533 if (drive->timeout && !timer_pending(&hwgroup->timer)) {
534 hwgroup->timer.expires = jiffies + drive->timeout;
535 add_timer(&(hwgroup->timer));
537 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
541 * current_capacity() returns the capacity (in sectors) of a drive
542 * according to its current geometry/LBA settings.
544 unsigned long current_capacity (ide_drive_t *drive)
546 if (!drive->present)
547 return 0;
548 if (drive->driver != NULL)
549 return DRIVER(drive)->capacity(drive);
550 return 0;
554 * ide_geninit() is called exactly *once* for each major, from genhd.c,
555 * at the beginning of the initial partition check for the drives.
557 void ide_geninit (struct gendisk *gd)
559 unsigned int unit;
560 ide_hwif_t *hwif = gd->real_devices;
562 for (unit = 0; unit < gd->nr_real; ++unit) {
563 ide_drive_t *drive = &hwif->drives[unit];
565 drive->part[0].nr_sects = current_capacity(drive);
566 if (!drive->present || (drive->media != ide_disk && drive->media != ide_floppy) ||
567 drive->driver == NULL || !drive->part[0].nr_sects)
568 drive->part[0].start_sect = -1; /* skip partition check */
572 static void do_reset1 (ide_drive_t *, int); /* needed below */
575 * atapi_reset_pollfunc() gets invoked to poll the interface for completion every 50ms
576 * during an atapi drive reset operation. If the drive has not yet responded,
577 * and we have not yet hit our maximum waiting time, then the timer is restarted
578 * for another 50ms.
580 static void atapi_reset_pollfunc (ide_drive_t *drive)
582 ide_hwgroup_t *hwgroup = HWGROUP(drive);
583 unsigned long old_timeout;
584 byte stat;
586 SELECT_DRIVE(HWIF(drive),drive);
587 udelay (10);
589 if (OK_STAT(stat=GET_STAT(), 0, BUSY_STAT)) {
590 printk("%s: ATAPI reset complete\n", drive->name);
591 } else {
592 if (0 < (signed long)(hwgroup->poll_timeout - jiffies)) {
593 old_timeout = set_timeout(drive, HZ / 20);
594 ide_set_handler (drive, &atapi_reset_pollfunc);
595 restore_timeout(drive, old_timeout);
596 return; /* continue polling */
598 hwgroup->poll_timeout = 0; /* end of polling */
599 printk("%s: ATAPI reset timed-out, status=0x%02x\n", drive->name, stat);
600 do_reset1 (drive, 1); /* do it the old fashioned way */
601 return;
603 hwgroup->poll_timeout = 0; /* done polling */
607 * reset_pollfunc() gets invoked to poll the interface for completion every 50ms
608 * during an ide reset operation. If the drives have not yet responded,
609 * and we have not yet hit our maximum waiting time, then the timer is restarted
610 * for another 50ms.
612 static void reset_pollfunc (ide_drive_t *drive)
614 ide_hwgroup_t *hwgroup = HWGROUP(drive);
615 ide_hwif_t *hwif = HWIF(drive);
616 unsigned long old_timeout;
617 byte tmp;
619 if (!OK_STAT(tmp=GET_STAT(), 0, BUSY_STAT)) {
620 if (0 < (signed long)(hwgroup->poll_timeout - jiffies)) {
621 old_timeout = set_timeout(drive, HZ / 20);
622 ide_set_handler (drive, &reset_pollfunc);
623 restore_timeout(drive, old_timeout);
624 return; /* continue polling */
626 printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
627 } else {
628 printk("%s: reset: ", hwif->name);
629 if ((tmp = GET_ERR()) == 1)
630 printk("success\n");
631 else {
632 #if FANCY_STATUS_DUMPS
633 printk("master: ");
634 switch (tmp & 0x7f) {
635 case 1: printk("passed");
636 break;
637 case 2: printk("formatter device error");
638 break;
639 case 3: printk("sector buffer error");
640 break;
641 case 4: printk("ECC circuitry error");
642 break;
643 case 5: printk("controlling MPU error");
644 break;
645 default:printk("error (0x%02x?)", tmp);
647 if (tmp & 0x80)
648 printk("; slave: failed");
649 printk("\n");
650 #else
651 printk("failed\n");
652 #endif /* FANCY_STATUS_DUMPS */
655 hwgroup->poll_timeout = 0; /* done polling */
658 static void pre_reset (ide_drive_t *drive)
660 if (!drive->keep_settings) {
661 drive->unmask = 0;
662 drive->io_32bit = 0;
663 if (drive->using_dma)
664 (void) HWIF(drive)->dmaproc(ide_dma_off, drive);
666 if (drive->driver != NULL)
667 DRIVER(drive)->pre_reset(drive);
671 * do_reset1() attempts to recover a confused drive by resetting it.
672 * Unfortunately, resetting a disk drive actually resets all devices on
673 * the same interface, so it can really be thought of as resetting the
674 * interface rather than resetting the drive.
676 * ATAPI devices have their own reset mechanism which allows them to be
677 * individually reset without clobbering other devices on the same interface.
679 * Unfortunately, the IDE interface does not generate an interrupt to let
680 * us know when the reset operation has finished, so we must poll for this.
681 * Equally poor, though, is the fact that this may a very long time to complete,
682 * (up to 30 seconds worstcase). So, instead of busy-waiting here for it,
683 * we set a timer to poll at 50ms intervals.
685 static void do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
687 unsigned int unit;
688 unsigned long flags;
689 ide_hwif_t *hwif = HWIF(drive);
690 ide_hwgroup_t *hwgroup = HWGROUP(drive);
691 unsigned long old_timeout;
693 __save_flags(flags); /* local CPU only */
694 __cli(); /* local CPU only */
696 /* For an ATAPI device, first try an ATAPI SRST. */
697 if (drive->media != ide_disk && !do_not_try_atapi) {
698 pre_reset(drive);
699 SELECT_DRIVE(hwif,drive);
700 udelay (20);
701 OUT_BYTE (WIN_SRST, IDE_COMMAND_REG);
702 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
703 old_timeout = set_timeout(drive, HZ / 20);
704 ide_set_handler (drive, &atapi_reset_pollfunc);
705 restore_timeout(drive, old_timeout);
706 __restore_flags (flags); /* local CPU only */
707 return;
711 * First, reset any device state data we were maintaining
712 * for any of the drives on this interface.
714 for (unit = 0; unit < MAX_DRIVES; ++unit)
715 pre_reset(&hwif->drives[unit]);
717 #if OK_TO_RESET_CONTROLLER
718 if (!IDE_CONTROL_REG) {
719 __restore_flags(flags);
720 return;
723 * Note that we also set nIEN while resetting the device,
724 * to mask unwanted interrupts from the interface during the reset.
725 * However, due to the design of PC hardware, this will cause an
726 * immediate interrupt due to the edge transition it produces.
727 * This single interrupt gives us a "fast poll" for drives that
728 * recover from reset very quickly, saving us the first 50ms wait time.
730 OUT_BYTE(drive->ctl|6,IDE_CONTROL_REG); /* set SRST and nIEN */
731 udelay(10); /* more than enough time */
732 OUT_BYTE(drive->ctl|2,IDE_CONTROL_REG); /* clear SRST, leave nIEN */
733 udelay(10); /* more than enough time */
734 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
735 old_timeout = set_timeout(drive, HZ / 20);
736 ide_set_handler (drive, &reset_pollfunc);
737 restore_timeout(drive, old_timeout);
740 * Some weird controller like resetting themselves to a strange
741 * state when the disks are reset this way. At least, the Winbond
742 * 553 documentation says that
744 if (hwif->resetproc != NULL)
745 hwif->resetproc(drive);
747 #endif /* OK_TO_RESET_CONTROLLER */
749 __restore_flags (flags); /* local CPU only */
753 * ide_do_reset() is the entry point to the drive/interface reset code.
755 void ide_do_reset (ide_drive_t *drive)
757 do_reset1 (drive, 0);
761 * Clean up after success/failure of an explicit drive cmd
763 void ide_end_drive_cmd (ide_drive_t *drive, byte stat, byte err)
765 unsigned long flags;
766 struct request *rq = HWGROUP(drive)->rq;
768 if (rq->cmd == IDE_DRIVE_CMD) {
769 byte *args = (byte *) rq->buffer;
770 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
771 if (args) {
772 args[0] = stat;
773 args[1] = err;
774 args[2] = IN_BYTE(IDE_NSECTOR_REG);
777 spin_lock_irqsave(&io_request_lock, flags);
778 drive->queue = rq->next;
779 blk_dev[MAJOR(rq->rq_dev)].current_request = NULL;
780 HWGROUP(drive)->rq = NULL;
781 rq->rq_status = RQ_INACTIVE;
782 spin_unlock_irqrestore(&io_request_lock, flags);
783 save_flags(flags); /* all CPUs; overkill? */
784 cli(); /* all CPUs; overkill? */
785 if (rq->sem != NULL)
786 up(rq->sem); /* inform originator that rq has been serviced */
787 restore_flags(flags); /* all CPUs; overkill? */
791 * Error reporting, in human readable form (luxurious, but a memory hog).
793 byte ide_dump_status (ide_drive_t *drive, const char *msg, byte stat)
795 unsigned long flags;
796 byte err = 0;
798 __save_flags (flags); /* local CPU only */
799 ide__sti(); /* local CPU only */
800 printk("%s: %s: status=0x%02x", drive->name, msg, stat);
801 #if FANCY_STATUS_DUMPS
802 printk(" { ");
803 if (stat & BUSY_STAT)
804 printk("Busy ");
805 else {
806 if (stat & READY_STAT) printk("DriveReady ");
807 if (stat & WRERR_STAT) printk("DeviceFault ");
808 if (stat & SEEK_STAT) printk("SeekComplete ");
809 if (stat & DRQ_STAT) printk("DataRequest ");
810 if (stat & ECC_STAT) printk("CorrectedError ");
811 if (stat & INDEX_STAT) printk("Index ");
812 if (stat & ERR_STAT) printk("Error ");
814 printk("}");
815 #endif /* FANCY_STATUS_DUMPS */
816 printk("\n");
817 if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
818 err = GET_ERR();
819 printk("%s: %s: error=0x%02x", drive->name, msg, err);
820 #if FANCY_STATUS_DUMPS
821 if (drive->media == ide_disk) {
822 printk(" { ");
823 if (err & ABRT_ERR) printk("DriveStatusError ");
824 if (err & ICRC_ERR) printk((err & ABRT_ERR) ? "BadCRC " : "BadSector ");
825 if (err & ECC_ERR) printk("UncorrectableError ");
826 if (err & ID_ERR) printk("SectorIdNotFound ");
827 if (err & TRK0_ERR) printk("TrackZeroNotFound ");
828 if (err & MARK_ERR) printk("AddrMarkNotFound ");
829 printk("}");
830 if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR || (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
831 byte cur = IN_BYTE(IDE_SELECT_REG);
832 if (cur & 0x40) { /* using LBA? */
833 printk(", LBAsect=%ld", (unsigned long)
834 ((cur&0xf)<<24)
835 |(IN_BYTE(IDE_HCYL_REG)<<16)
836 |(IN_BYTE(IDE_LCYL_REG)<<8)
837 | IN_BYTE(IDE_SECTOR_REG));
838 } else {
839 printk(", CHS=%d/%d/%d",
840 (IN_BYTE(IDE_HCYL_REG)<<8) +
841 IN_BYTE(IDE_LCYL_REG),
842 cur & 0xf,
843 IN_BYTE(IDE_SECTOR_REG));
845 if (HWGROUP(drive)->rq)
846 printk(", sector=%ld", HWGROUP(drive)->rq->sector);
849 #endif /* FANCY_STATUS_DUMPS */
850 printk("\n");
852 __restore_flags (flags); /* local CPU only */
853 return err;
857 * try_to_flush_leftover_data() is invoked in response to a drive
858 * unexpectedly having its DRQ_STAT bit set. As an alternative to
859 * resetting the drive, this routine tries to clear the condition
860 * by read a sector's worth of data from the drive. Of course,
861 * this may not help if the drive is *waiting* for data from *us*.
863 static void try_to_flush_leftover_data (ide_drive_t *drive)
865 int i = (drive->mult_count ? drive->mult_count : 1) * SECTOR_WORDS;
867 if (drive->media != ide_disk)
868 return;
869 while (i > 0) {
870 unsigned long buffer[16];
871 unsigned int wcount = (i > 16) ? 16 : i;
872 i -= wcount;
873 ide_input_data (drive, buffer, wcount);
878 * ide_error() takes action based on the error returned by the drive.
880 void ide_error (ide_drive_t *drive, const char *msg, byte stat)
882 struct request *rq;
883 byte err;
885 err = ide_dump_status(drive, msg, stat);
886 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
887 return;
888 /* retry only "normal" I/O: */
889 if (rq->cmd == IDE_DRIVE_CMD) {
890 rq->errors = 1;
891 ide_end_drive_cmd(drive, stat, err);
892 return;
894 if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) { /* other bits are useless when BUSY */
895 rq->errors |= ERROR_RESET;
896 } else {
897 if (drive->media == ide_disk && (stat & ERR_STAT)) {
898 /* err has different meaning on cdrom and tape */
899 if (err == ABRT_ERR) {
900 if (drive->select.b.lba && IN_BYTE(IDE_COMMAND_REG) == WIN_SPECIFY)
901 return; /* some newer drives don't support WIN_SPECIFY */
902 } else if ((err & (ABRT_ERR | ICRC_ERR)) == (ABRT_ERR | ICRC_ERR))
903 ; /* UDMA crc error -- just retry the operation */
904 else if (err & (BBD_ERR | ECC_ERR)) /* retries won't help these */
905 rq->errors = ERROR_MAX;
906 else if (err & TRK0_ERR) /* help it find track zero */
907 rq->errors |= ERROR_RECAL;
909 if ((stat & DRQ_STAT) && rq->cmd != WRITE)
910 try_to_flush_leftover_data(drive);
912 if (GET_STAT() & (BUSY_STAT|DRQ_STAT))
913 rq->errors |= ERROR_RESET; /* Mmmm.. timing problem */
915 if (rq->errors >= ERROR_MAX) {
916 if (drive->driver != NULL)
917 DRIVER(drive)->end_request(0, HWGROUP(drive));
918 else
919 ide_end_request(0, HWGROUP(drive));
920 } else {
921 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
922 ++rq->errors;
923 ide_do_reset(drive);
924 return;
925 } else if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
926 drive->special.b.recalibrate = 1;
927 ++rq->errors;
932 * Issue a simple drive command
933 * The drive must be selected beforehand.
935 void ide_cmd(ide_drive_t *drive, byte cmd, byte nsect, ide_handler_t *handler)
937 ide_set_handler (drive, handler);
938 if (IDE_CONTROL_REG)
939 OUT_BYTE(drive->ctl,IDE_CONTROL_REG); /* clear nIEN */
940 OUT_BYTE(nsect,IDE_NSECTOR_REG);
941 OUT_BYTE(cmd,IDE_COMMAND_REG);
945 * drive_cmd_intr() is invoked on completion of a special DRIVE_CMD.
947 static void drive_cmd_intr (ide_drive_t *drive)
949 struct request *rq = HWGROUP(drive)->rq;
950 byte *args = (byte *) rq->buffer;
951 byte stat = GET_STAT();
952 int retries = 10;
954 ide__sti(); /* local CPU only */
955 if ((stat & DRQ_STAT) && args && args[3]) {
956 byte io_32bit = drive->io_32bit;
957 drive->io_32bit = 0;
958 ide_input_data(drive, &args[4], args[3] * SECTOR_WORDS);
959 drive->io_32bit = io_32bit;
960 while (((stat = GET_STAT()) & BUSY_STAT) && retries--)
961 udelay(100);
964 if (OK_STAT(stat, READY_STAT, BAD_STAT))
965 ide_end_drive_cmd (drive, stat, GET_ERR());
966 else
967 ide_error(drive, "drive_cmd", stat); /* calls ide_end_drive_cmd */
971 * do_special() is used to issue WIN_SPECIFY, WIN_RESTORE, and WIN_SETMULT
972 * commands to a drive. It used to do much more, but has been scaled back.
974 static inline void do_special (ide_drive_t *drive)
976 special_t *s = &drive->special;
978 #ifdef DEBUG
979 printk("%s: do_special: 0x%02x\n", drive->name, s->all);
980 #endif
981 if (s->b.set_tune) {
982 ide_tuneproc_t *tuneproc = HWIF(drive)->tuneproc;
983 s->b.set_tune = 0;
984 if (tuneproc != NULL)
985 tuneproc(drive, drive->tune_req);
986 } else if (drive->driver != NULL) {
987 DRIVER(drive)->special(drive);
988 } else if (s->all) {
989 printk("%s: bad special flag: 0x%02x\n", drive->name, s->all);
990 s->all = 0;
995 * This routine busy-waits for the drive status to be not "busy".
996 * It then checks the status for all of the "good" bits and none
997 * of the "bad" bits, and if all is okay it returns 0. All other
998 * cases return 1 after invoking ide_error() -- caller should just return.
1000 * This routine should get fixed to not hog the cpu during extra long waits..
1001 * That could be done by busy-waiting for the first jiffy or two, and then
1002 * setting a timer to wake up at half second intervals thereafter,
1003 * until timeout is achieved, before timing out.
1005 int ide_wait_stat (ide_drive_t *drive, byte good, byte bad, unsigned long timeout)
1007 byte stat;
1008 int i;
1009 unsigned long flags;
1011 udelay(1); /* spec allows drive 400ns to assert "BUSY" */
1012 if ((stat = GET_STAT()) & BUSY_STAT) {
1013 __save_flags(flags); /* local CPU only */
1014 ide__sti(); /* local CPU only */
1015 timeout += jiffies;
1016 while ((stat = GET_STAT()) & BUSY_STAT) {
1017 if (0 < (signed long)(jiffies - timeout)) {
1018 __restore_flags(flags); /* local CPU only */
1019 ide_error(drive, "status timeout", stat);
1020 return 1;
1023 __restore_flags(flags); /* local CPU only */
1026 * Allow status to settle, then read it again.
1027 * A few rare drives vastly violate the 400ns spec here,
1028 * so we'll wait up to 10usec for a "good" status
1029 * rather than expensively fail things immediately.
1030 * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
1032 for (i = 0; i < 10; i++) {
1033 udelay(1);
1034 if (OK_STAT((stat = GET_STAT()), good, bad))
1035 return 0;
1037 ide_error(drive, "status error", stat);
1038 return 1;
1042 * execute_drive_cmd() issues a special drive command,
1043 * usually initiated by ioctl() from the external hdparm program.
1045 static void execute_drive_cmd (ide_drive_t *drive, struct request *rq)
1047 byte *args = rq->buffer;
1048 if (args) {
1049 #ifdef DEBUG
1050 printk("%s: DRIVE_CMD cmd=0x%02x sc=0x%02x fr=0x%02x xx=0x%02x\n",
1051 drive->name, args[0], args[1], args[2], args[3]);
1052 #endif
1053 if (args[0] == WIN_SMART) {
1054 OUT_BYTE(0x4f, IDE_LCYL_REG);
1055 OUT_BYTE(0xc2, IDE_HCYL_REG);
1056 OUT_BYTE(args[2],IDE_FEATURE_REG);
1057 OUT_BYTE(args[1],IDE_SECTOR_REG);
1058 ide_cmd(drive, args[0], args[3], &drive_cmd_intr);
1059 return;
1061 OUT_BYTE(args[2],IDE_FEATURE_REG);
1062 ide_cmd(drive, args[0], args[1], &drive_cmd_intr);
1063 return;
1064 } else {
1066 * NULL is actually a valid way of waiting for
1067 * all current requests to be flushed from the queue.
1069 #ifdef DEBUG
1070 printk("%s: DRIVE_CMD (null)\n", drive->name);
1071 #endif
1072 ide_end_drive_cmd(drive, GET_STAT(), GET_ERR());
1073 return;
1078 * start_request() initiates handling of a new I/O request
1080 static inline void start_request (ide_drive_t *drive)
1082 unsigned long block, blockend;
1083 struct request *rq = drive->queue;
1084 unsigned int minor = MINOR(rq->rq_dev), unit = minor >> PARTN_BITS;
1085 ide_hwif_t *hwif = HWIF(drive);
1087 ide__sti(); /* local CPU only */
1088 #ifdef DEBUG
1089 printk("%s: start_request: current=0x%08lx\n", hwif->name, (unsigned long) rq);
1090 #endif
1091 if (unit >= MAX_DRIVES) {
1092 printk("%s: bad device number: %s\n", hwif->name, kdevname(rq->rq_dev));
1093 goto kill_rq;
1095 #ifdef DEBUG
1096 if (rq->bh && !buffer_locked(rq->bh)) {
1097 printk("%s: block not locked\n", drive->name);
1098 goto kill_rq;
1100 #endif
1101 block = rq->sector;
1102 blockend = block + rq->nr_sectors;
1103 if ((blockend < block) || (blockend > drive->part[minor&PARTN_MASK].nr_sects)) {
1104 printk("%s%c: bad access: block=%ld, count=%ld\n", drive->name,
1105 (minor&PARTN_MASK)?'0'+(minor&PARTN_MASK):' ', block, rq->nr_sectors);
1106 goto kill_rq;
1108 block += drive->part[minor&PARTN_MASK].start_sect + drive->sect0;
1110 /* Yecch - this will shift the entire interval,
1111 possibly killing some innocent following sector */
1112 if (block == 0 && drive->remap_0_to_1 == 1)
1113 block = 1; /* redirect MBR access to EZ-Drive partn table */
1115 #if (DISK_RECOVERY_TIME > 0)
1116 while ((read_timer() - hwif->last_time) < DISK_RECOVERY_TIME);
1117 #endif
1119 SELECT_DRIVE(hwif, drive);
1120 if (ide_wait_stat(drive, drive->ready_stat, BUSY_STAT|DRQ_STAT, WAIT_READY)) {
1121 printk("%s: drive not ready for command\n", drive->name);
1122 return;
1124 if (!drive->special.all) {
1125 if (rq->cmd == IDE_DRIVE_CMD) {
1126 execute_drive_cmd(drive, rq);
1127 return;
1129 if (drive->driver != NULL) {
1130 DRIVER(drive)->do_request(drive, rq, block);
1131 return;
1133 printk("%s: media type %d not supported\n", drive->name, drive->media);
1134 goto kill_rq;
1136 do_special(drive);
1137 return;
1138 kill_rq:
1139 if (drive->driver != NULL)
1140 DRIVER(drive)->end_request(0, HWGROUP(drive));
1141 else
1142 ide_end_request(0, HWGROUP(drive));
1146 * ide_stall_queue() can be used by a drive to give excess bandwidth back
1147 * to the hwgroup by sleeping for timeout jiffies.
1149 void ide_stall_queue (ide_drive_t *drive, unsigned long timeout)
1151 if (timeout > WAIT_WORSTCASE)
1152 timeout = WAIT_WORSTCASE;
1153 drive->sleep = timeout + jiffies;
1156 #define WAKEUP(drive) ((drive)->service_start + 2 * (drive)->service_time)
1159 * choose_drive() selects the next drive which will be serviced.
1161 static inline ide_drive_t *choose_drive (ide_hwgroup_t *hwgroup)
1163 ide_drive_t *drive, *best;
1165 repeat:
1166 best = NULL;
1167 drive = hwgroup->drive;
1168 do {
1169 if (drive->queue && (!drive->sleep || 0 <= (signed long)(jiffies - drive->sleep))) {
1170 if (!best
1171 || (drive->sleep && (!best->sleep || 0 < (signed long)(best->sleep - drive->sleep)))
1172 || (!best->sleep && 0 < (signed long)(WAKEUP(best) - WAKEUP(drive))))
1174 struct blk_dev_struct *bdev = &blk_dev[HWIF(drive)->major];
1175 if (bdev->current_request != &bdev->plug)
1176 best = drive;
1179 } while ((drive = drive->next) != hwgroup->drive);
1180 if (best && best->nice1 && !best->sleep && best != hwgroup->drive && best->service_time > WAIT_MIN_SLEEP) {
1181 long t = (signed long)(WAKEUP(best) - jiffies);
1182 if (t >= WAIT_MIN_SLEEP) {
1184 * We *may* have some time to spare, but first let's see if
1185 * someone can potentially benefit from our nice mood today..
1187 drive = best->next;
1188 do {
1189 if (!drive->sleep
1190 && 0 < (signed long)(WAKEUP(drive) - (jiffies - best->service_time))
1191 && 0 < (signed long)((jiffies + t) - WAKEUP(drive)))
1193 ide_stall_queue(best, IDE_MIN(t, 10 * WAIT_MIN_SLEEP));
1194 goto repeat;
1196 } while ((drive = drive->next) != best);
1199 return best;
1203 * Caller must have already acquired spinlock using *spinflags
1205 static void ide_do_request (ide_hwgroup_t *hwgroup, unsigned long *hwgroup_flags, int masked_irq)
1207 struct blk_dev_struct *bdev;
1208 ide_drive_t *drive;
1209 ide_hwif_t *hwif;
1210 unsigned long io_flags;
1212 hwgroup->busy = 1;
1213 while (hwgroup->handler == NULL) {
1214 spin_lock_irqsave(&io_request_lock, io_flags);
1215 drive = choose_drive(hwgroup);
1216 if (drive == NULL) {
1217 unsigned long sleep = 0;
1219 hwgroup->rq = NULL;
1220 drive = hwgroup->drive;
1221 do {
1222 bdev = &blk_dev[HWIF(drive)->major];
1223 if (bdev->current_request != &bdev->plug) /* FIXME: this will do for now */
1224 bdev->current_request = NULL; /* (broken since patch-2.1.15) */
1225 if (drive->sleep && (!sleep || 0 < (signed long)(sleep - drive->sleep)))
1226 sleep = drive->sleep;
1227 } while ((drive = drive->next) != hwgroup->drive);
1228 spin_unlock_irqrestore(&io_request_lock, io_flags);
1229 if (sleep) {
1230 if (0 < (signed long)(jiffies + WAIT_MIN_SLEEP - sleep))
1231 sleep = jiffies + WAIT_MIN_SLEEP;
1232 #if 1
1233 if (hwgroup->timer.next || hwgroup->timer.prev)
1234 printk("ide_set_handler: timer already active\n");
1235 #endif
1236 mod_timer(&hwgroup->timer, sleep);
1237 } else {
1238 /* Ugly, but how can we sleep for the lock otherwise? perhaps from tq_scheduler? */
1239 ide_release_lock(&ide_lock); /* for atari only */
1241 hwgroup->busy = 0;
1242 return;
1244 hwif = HWIF(drive);
1245 if (hwgroup->hwif->sharing_irq && hwif != hwgroup->hwif &&
1246 hwif->io_ports[IDE_CONTROL_OFFSET])
1247 /* set nIEN for previous hwif */
1248 OUT_BYTE(hwgroup->drive->ctl|2, hwgroup->hwif->io_ports[IDE_CONTROL_OFFSET]);
1249 hwgroup->hwif = hwif;
1250 hwgroup->drive = drive;
1251 drive->sleep = 0;
1252 drive->service_start = jiffies;
1254 bdev = &blk_dev[hwif->major];
1255 if (bdev->current_request == &bdev->plug) /* FIXME: paranoia */
1256 printk("%s: Huh? nuking plugged queue\n", drive->name);
1257 bdev->current_request = hwgroup->rq = drive->queue;
1258 spin_unlock_irqrestore(&io_request_lock, io_flags);
1260 #if 0
1261 if (hwif->irq != masked_irq)
1262 disable_irq_nosync(hwif->irq);
1263 spin_unlock_irqrestore(&hwgroup->spinlock, *hwgroup_flags);
1264 start_request(drive);
1265 spin_lock_irqsave(&hwgroup->spinlock, *hwgroup_flags);
1266 if (hwif->irq != masked_irq)
1267 enable_irq(hwif->irq);
1268 #else
1270 if (masked_irq && hwif->irq != masked_irq) {
1271 printk("%s: (disable_irq) %smasked_irq %d\n",
1272 drive->name,
1273 masked_irq ? "" : "un_", hwif->irq);
1275 #if 0
1276 disable_irq(hwif->irq);
1277 #else
1278 disable_irq_nosync(hwif->irq);
1279 #endif
1281 spin_unlock_irqrestore(&hwgroup->spinlock, *hwgroup_flags);
1282 start_request(drive);
1283 spin_lock_irqsave(&hwgroup->spinlock, *hwgroup_flags);
1284 if (masked_irq && hwif->irq != masked_irq) {
1285 printk("%s: (enable_irq) %smasked_irq %d\n",
1286 drive->name,
1287 masked_irq ? "" : "un_", hwif->irq);
1288 enable_irq(hwif->irq);
1290 #endif
1295 * ide_get_queue() returns the queue which corresponds to a given device.
1297 struct request **ide_get_queue (kdev_t dev)
1299 ide_hwif_t *hwif = (ide_hwif_t *)blk_dev[MAJOR(dev)].data;
1301 return &hwif->drives[DEVICE_NR(dev) & 1].queue;
1305 * do_hwgroup_request() invokes ide_do_request() after claiming hwgroup->busy.
1307 static void do_hwgroup_request (ide_hwgroup_t *hwgroup)
1309 unsigned long flags;
1311 spin_lock_irqsave(&hwgroup->spinlock, flags);
1312 if (hwgroup->busy) {
1313 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1314 return;
1316 del_timer(&hwgroup->timer);
1317 ide_get_lock(&ide_lock, ide_intr, hwgroup); /* for atari only */
1318 ide_do_request(hwgroup, &flags, 0);
1319 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1323 * ll_rw_blk.c invokes our do_idex_request() function
1324 * with the io_request_spinlock already grabbed.
1325 * Since we need to do our own spinlock's internally,
1326 * on paths that don't necessarily originate through the
1327 * do_idex_request() path, we have to undo the spinlock on entry,
1328 * and restore it again on exit.
1329 * Fortunately, this is mostly a nop for non-SMP kernels.
1331 static inline void unlock_do_hwgroup_request (ide_hwgroup_t *hwgroup)
1333 spin_unlock(&io_request_lock);
1334 do_hwgroup_request (hwgroup);
1335 spin_lock_irq(&io_request_lock);
1338 void do_ide0_request (void)
1340 unlock_do_hwgroup_request (ide_hwifs[0].hwgroup);
1343 #if MAX_HWIFS > 1
1344 void do_ide1_request (void)
1346 unlock_do_hwgroup_request (ide_hwifs[1].hwgroup);
1348 #endif /* MAX_HWIFS > 1 */
1350 #if MAX_HWIFS > 2
1351 void do_ide2_request (void)
1353 unlock_do_hwgroup_request (ide_hwifs[2].hwgroup);
1355 #endif /* MAX_HWIFS > 2 */
1357 #if MAX_HWIFS > 3
1358 void do_ide3_request (void)
1360 unlock_do_hwgroup_request (ide_hwifs[3].hwgroup);
1362 #endif /* MAX_HWIFS > 3 */
1364 #if MAX_HWIFS > 4
1365 void do_ide4_request (void)
1367 unlock_do_hwgroup_request (ide_hwifs[4].hwgroup);
1369 #endif /* MAX_HWIFS > 4 */
1371 #if MAX_HWIFS > 5
1372 void do_ide5_request (void)
1374 unlock_do_hwgroup_request (ide_hwifs[5].hwgroup);
1376 #endif /* MAX_HWIFS > 5 */
1378 #if MAX_HWIFS > 6
1379 void do_ide6_request (void)
1381 unlock_do_hwgroup_request (ide_hwifs[6].hwgroup);
1383 #endif /* MAX_HWIFS > 6 */
1385 #if MAX_HWIFS > 7
1386 void do_ide7_request (void)
1388 unlock_do_hwgroup_request (ide_hwifs[7].hwgroup);
1390 #endif /* MAX_HWIFS > 7 */
1392 #if MAX_HWIFS > 8
1393 void do_ide8_request (void)
1395 unlock_do_hwgroup_request (ide_hwifs[8].hwgroup);
1397 #endif /* MAX_HWIFS > 8 */
1399 #if MAX_HWIFS > 9
1400 void do_ide9_request (void)
1402 unlock_do_hwgroup_request (ide_hwifs[9].hwgroup);
1404 #endif /* MAX_HWIFS > 9 */
1406 static void start_next_request (ide_hwgroup_t *hwgroup, int masked_irq)
1408 unsigned long flags;
1409 ide_drive_t *drive;
1411 spin_lock_irqsave(&hwgroup->spinlock, flags);
1412 if (hwgroup->handler != NULL) {
1413 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1414 return;
1416 drive = hwgroup->drive;
1417 set_recovery_timer(HWIF(drive));
1418 drive->service_time = jiffies - drive->service_start;
1419 ide_do_request(hwgroup, &flags, masked_irq);
1420 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1423 void ide_timer_expiry (unsigned long data)
1425 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *) data;
1426 ide_drive_t *drive;
1427 ide_handler_t *handler;
1428 unsigned long flags;
1430 spin_lock_irqsave(&hwgroup->spinlock, flags);
1431 drive = hwgroup->drive;
1432 if ((handler = hwgroup->handler) == NULL) {
1433 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1434 do_hwgroup_request(hwgroup);
1435 return;
1437 hwgroup->busy = 1; /* should already be "1" */
1438 hwgroup->handler = NULL;
1439 /* polling in progress or just don't timeout */
1440 if (hwgroup->poll_timeout != 0) {
1441 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1442 handler(drive);
1443 } else if (drive_is_ready(drive)) {
1444 printk("%s: lost interrupt\n", drive->name);
1445 (void) hwgroup->hwif->dmaproc(ide_dma_lostirq, drive);
1446 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1447 handler(drive);
1448 } else {
1449 if (drive->waiting_for_dma) {
1450 (void) hwgroup->hwif->dmaproc(ide_dma_end, drive);
1451 printk("%s: timeout waiting for DMA\n", drive->name);
1453 * need something here for HPT34X.......AMH
1454 * irq timeout: status=0x58 { DriveReady SeekComplete DataRequest }
1456 (void) hwgroup->hwif->dmaproc(ide_dma_timeout, drive);
1458 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1459 ide_error(drive, "irq timeout", GET_STAT());
1461 start_next_request(hwgroup, 0);
1465 * There's nothing really useful we can do with an unexpected interrupt,
1466 * other than reading the status register (to clear it), and logging it.
1467 * There should be no way that an irq can happen before we're ready for it,
1468 * so we needn't worry much about losing an "important" interrupt here.
1470 * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
1471 * drive enters "idle", "standby", or "sleep" mode, so if the status looks
1472 * "good", we just ignore the interrupt completely.
1474 * This routine assumes __cli() is in effect when called.
1476 * If an unexpected interrupt happens on irq15 while we are handling irq14
1477 * and if the two interfaces are "serialized" (CMD640), then it looks like
1478 * we could screw up by interfering with a new request being set up for irq15.
1480 * In reality, this is a non-issue. The new command is not sent unless the
1481 * drive is ready to accept one, in which case we know the drive is not
1482 * trying to interrupt us. And ide_set_handler() is always invoked before
1483 * completing the issuance of any new drive command, so we will not be
1484 * accidently invoked as a result of any valid command completion interrupt.
1487 static void unexpected_intr (int irq, ide_hwgroup_t *hwgroup)
1489 byte stat;
1490 ide_hwif_t *hwif = hwgroup->hwif;
1493 * handle the unexpected interrupt
1495 do {
1496 if (hwif->irq == irq) {
1497 stat = IN_BYTE(hwif->io_ports[IDE_STATUS_OFFSET]);
1498 if (!OK_STAT(stat, READY_STAT, BAD_STAT)) {
1499 /* Try to not flood the console with msgs */
1500 static unsigned long last_msgtime = 0, count = 0;
1501 ++count;
1502 if (0 < (signed long)(jiffies - (last_msgtime + HZ))) {
1503 last_msgtime = jiffies;
1504 printk("%s%s: unexpected interrupt, status=0x%02x, count=%ld\n",
1505 hwif->name, (hwif->next == hwgroup->hwif) ? "" : "(?)", stat, count);
1509 } while ((hwif = hwif->next) != hwgroup->hwif);
1512 * entry point for all interrupts, caller does __cli() for us
1514 void ide_intr (int irq, void *dev_id, struct pt_regs *regs)
1516 unsigned long flags;
1517 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *)dev_id;
1518 ide_hwif_t *hwif;
1519 ide_drive_t *drive;
1520 ide_handler_t *handler;
1522 __cli(); /* local CPU only */
1523 spin_lock_irqsave(&hwgroup->spinlock, flags);
1524 hwif = hwgroup->hwif;
1526 if (!ide_ack_intr(hwif)) {
1527 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1528 return;
1531 if ((handler = hwgroup->handler) == NULL || hwgroup->poll_timeout != 0) {
1533 * Not expecting an interrupt from this drive.
1534 * That means this could be:
1535 * (1) an interrupt from another PCI device
1536 * sharing the same PCI INT# as us.
1537 * or (2) a drive just entered sleep or standby mode,
1538 * and is interrupting to let us know.
1539 * or (3) a spurious interrupt of unknown origin.
1541 * For PCI, we cannot tell the difference,
1542 * so in that case we just ignore it and hope it goes away.
1544 #ifdef CONFIG_BLK_DEV_IDEPCI
1545 if (IDE_PCI_DEVID_EQ(hwif->pci_devid, IDE_PCI_DEVID_NULL))
1546 #endif /* CONFIG_BLK_DEV_IDEPCI */
1549 * Probably not a shared PCI interrupt,
1550 * so we can safely try to do something about it:
1552 unexpected_intr(irq, hwgroup);
1554 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1555 return;
1557 drive = hwgroup->drive;
1558 if (!drive || !drive_is_ready(drive)) {
1559 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1560 return;
1562 hwgroup->handler = NULL;
1563 del_timer(&(hwgroup->timer));
1564 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1565 if (drive->unmask)
1566 ide__sti(); /* local CPU only */
1567 handler(drive); /* service this interrupt, may set handler for next interrupt */
1569 * Note that handler() may have set things up for another
1570 * interrupt to occur soon, but it cannot happen until
1571 * we exit from this routine, because it will be the
1572 * same irq as is currently being serviced here,
1573 * and Linux won't allow another (on any CPU) until we return.
1575 start_next_request(hwgroup, hwif->irq);
1579 * get_info_ptr() returns the (ide_drive_t *) for a given device number.
1580 * It returns NULL if the given device number does not match any present drives.
1582 ide_drive_t *get_info_ptr (kdev_t i_rdev)
1584 int major = MAJOR(i_rdev);
1585 unsigned int h;
1587 for (h = 0; h < MAX_HWIFS; ++h) {
1588 ide_hwif_t *hwif = &ide_hwifs[h];
1589 if (hwif->present && major == hwif->major) {
1590 unsigned unit = DEVICE_NR(i_rdev);
1591 if (unit < MAX_DRIVES) {
1592 ide_drive_t *drive = &hwif->drives[unit];
1593 if (drive->present)
1594 return drive;
1596 break;
1599 return NULL;
1603 * This function is intended to be used prior to invoking ide_do_drive_cmd().
1605 void ide_init_drive_cmd (struct request *rq)
1607 rq->buffer = NULL;
1608 rq->cmd = IDE_DRIVE_CMD;
1609 rq->sector = 0;
1610 rq->nr_sectors = 0;
1611 rq->current_nr_sectors = 0;
1612 rq->sem = NULL;
1613 rq->bh = NULL;
1614 rq->bhtail = NULL;
1615 rq->next = NULL;
1619 * This function issues a special IDE device request
1620 * onto the request queue.
1622 * If action is ide_wait, then the rq is queued at the end of the
1623 * request queue, and the function sleeps until it has been processed.
1624 * This is for use when invoked from an ioctl handler.
1626 * If action is ide_preempt, then the rq is queued at the head of
1627 * the request queue, displacing the currently-being-processed
1628 * request and this function returns immediately without waiting
1629 * for the new rq to be completed. This is VERY DANGEROUS, and is
1630 * intended for careful use by the ATAPI tape/cdrom driver code.
1632 * If action is ide_next, then the rq is queued immediately after
1633 * the currently-being-processed-request (if any), and the function
1634 * returns without waiting for the new rq to be completed. As above,
1635 * This is VERY DANGEROUS, and is intended for careful use by the
1636 * ATAPI tape/cdrom driver code.
1638 * If action is ide_end, then the rq is queued at the end of the
1639 * request queue, and the function returns immediately without waiting
1640 * for the new rq to be completed. This is again intended for careful
1641 * use by the ATAPI tape/cdrom driver code.
1643 int ide_do_drive_cmd (ide_drive_t *drive, struct request *rq, ide_action_t action)
1645 unsigned long flags;
1646 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1647 unsigned int major = HWIF(drive)->major;
1648 struct request *cur_rq;
1649 DECLARE_MUTEX_LOCKED(sem);
1651 #ifdef CONFIG_BLK_DEV_PDC4030
1652 if (HWIF(drive)->chipset == ide_pdc4030 && rq->buffer != NULL)
1653 return -ENOSYS; /* special drive cmds not supported */
1654 #endif
1655 rq->errors = 0;
1656 rq->rq_status = RQ_ACTIVE;
1657 rq->rq_dev = MKDEV(major,(drive->select.b.unit)<<PARTN_BITS);
1658 if (action == ide_wait)
1659 rq->sem = &sem;
1661 spin_lock_irqsave(&io_request_lock, flags);
1662 cur_rq = drive->queue;
1663 if (cur_rq == NULL || action == ide_preempt) {
1664 rq->next = cur_rq;
1665 drive->queue = rq;
1666 if (action == ide_preempt)
1667 hwgroup->rq = NULL;
1668 } else {
1669 if (action == ide_wait || action == ide_end) {
1670 while (cur_rq->next != NULL) /* find end of list */
1671 cur_rq = cur_rq->next;
1673 rq->next = cur_rq->next;
1674 cur_rq->next = rq;
1676 spin_unlock_irqrestore(&io_request_lock, flags);
1677 do_hwgroup_request(hwgroup);
1678 if (action == ide_wait) {
1679 down(&sem); /* wait for it to be serviced */
1680 rq->sem = NULL;
1682 return rq->errors ? -EIO : 0; /* return -EIO if errors */
1686 * This routine is called to flush all partitions and partition tables
1687 * for a changed disk, and then re-read the new partition table.
1688 * If we are revalidating a disk because of a media change, then we
1689 * enter with usage == 0. If we are using an ioctl, we automatically have
1690 * usage == 1 (we need an open channel to use an ioctl :-), so this
1691 * is our limit.
1693 int ide_revalidate_disk(kdev_t i_rdev)
1695 ide_drive_t *drive;
1696 ide_hwgroup_t *hwgroup;
1697 unsigned int p, major, minor;
1698 long flags;
1700 if ((drive = get_info_ptr(i_rdev)) == NULL)
1701 return -ENODEV;
1702 major = MAJOR(i_rdev);
1703 minor = drive->select.b.unit << PARTN_BITS;
1704 hwgroup = HWGROUP(drive);
1705 spin_lock_irqsave(&hwgroup->spinlock, flags);
1706 if (drive->busy || (drive->usage > 1)) {
1707 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1708 return -EBUSY;
1710 drive->busy = 1;
1711 MOD_INC_USE_COUNT;
1712 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1714 for (p = 0; p < (1<<PARTN_BITS); ++p) {
1715 if (drive->part[p].nr_sects > 0) {
1716 kdev_t devp = MKDEV(major, minor+p);
1717 struct super_block * sb = get_super(devp);
1718 fsync_dev (devp);
1719 if (sb)
1720 invalidate_inodes(sb);
1721 invalidate_buffers (devp);
1722 set_blocksize(devp, 1024);
1724 drive->part[p].start_sect = 0;
1725 drive->part[p].nr_sects = 0;
1728 drive->part[0].nr_sects = current_capacity(drive);
1729 if ((drive->media != ide_disk && drive->media != ide_floppy) ||
1730 drive->driver == NULL || !drive->part[0].nr_sects)
1731 drive->part[0].start_sect = -1;
1732 resetup_one_dev(HWIF(drive)->gd, drive->select.b.unit);
1734 drive->busy = 0;
1735 wake_up(&drive->wqueue);
1736 MOD_DEC_USE_COUNT;
1737 return 0;
1740 static void revalidate_drives (void)
1742 ide_hwif_t *hwif;
1743 ide_drive_t *drive;
1744 int index, unit;
1746 for (index = 0; index < MAX_HWIFS; ++index) {
1747 hwif = &ide_hwifs[index];
1748 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1749 drive = &ide_hwifs[index].drives[unit];
1750 if (drive->revalidate) {
1751 drive->revalidate = 0;
1752 if (!initializing)
1753 (void) ide_revalidate_disk(MKDEV(hwif->major, unit<<PARTN_BITS));
1759 static void ide_init_module (int type)
1761 int found = 0;
1762 ide_module_t *module = ide_modules;
1764 while (module) {
1765 if (module->type == type) {
1766 found = 1;
1767 (void) module->init();
1769 module = module->next;
1771 revalidate_drives();
1772 #ifdef CONFIG_KMOD
1773 if (!found && type == IDE_PROBE_MODULE)
1774 (void) request_module("ide-probe");
1775 #endif /* CONFIG_KMOD */
1778 static int ide_open(struct inode * inode, struct file * filp)
1780 ide_drive_t *drive;
1781 int rc;
1783 if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
1784 return -ENXIO;
1785 MOD_INC_USE_COUNT;
1786 if (drive->driver == NULL)
1787 ide_init_module(IDE_DRIVER_MODULE);
1788 #ifdef CONFIG_KMOD
1789 if (drive->driver == NULL) {
1790 if (drive->media == ide_disk)
1791 (void) request_module("ide-disk");
1792 if (drive->media == ide_cdrom)
1793 (void) request_module("ide-cd");
1794 if (drive->media == ide_tape)
1795 (void) request_module("ide-tape");
1796 if (drive->media == ide_floppy)
1797 (void) request_module("ide-floppy");
1799 #endif /* CONFIG_KMOD */
1800 while (drive->busy)
1801 sleep_on(&drive->wqueue);
1802 drive->usage++;
1803 if (drive->driver != NULL) {
1804 if ((rc = DRIVER(drive)->open(inode, filp, drive)))
1805 MOD_DEC_USE_COUNT;
1806 return rc;
1808 printk ("%s: driver not present\n", drive->name);
1809 drive->usage--;
1810 MOD_DEC_USE_COUNT;
1811 return -ENXIO;
1815 * Releasing a block device means we sync() it, so that it can safely
1816 * be forgotten about...
1818 static int ide_release(struct inode * inode, struct file * file)
1820 ide_drive_t *drive;
1822 if ((drive = get_info_ptr(inode->i_rdev)) != NULL) {
1823 fsync_dev(inode->i_rdev);
1824 drive->usage--;
1825 if (drive->driver != NULL)
1826 DRIVER(drive)->release(inode, file, drive);
1827 MOD_DEC_USE_COUNT;
1829 return 0;
1832 int ide_replace_subdriver(ide_drive_t *drive, const char *driver)
1834 if (!drive->present || drive->busy || drive->usage)
1835 goto abort;
1836 if (drive->driver != NULL && DRIVER(drive)->cleanup(drive))
1837 goto abort;
1838 strncpy(drive->driver_req, driver, 9);
1839 ide_init_module(IDE_DRIVER_MODULE);
1840 drive->driver_req[0] = 0;
1841 ide_init_module(IDE_DRIVER_MODULE);
1842 if (DRIVER(drive) && !strcmp(DRIVER(drive)->name, driver))
1843 return 0;
1844 abort:
1845 return 1;
1848 #ifdef CONFIG_PROC_FS
1849 ide_proc_entry_t generic_subdriver_entries[] = {
1850 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
1851 { NULL, 0, NULL, NULL }
1853 #endif
1855 void ide_unregister (unsigned int index)
1857 struct gendisk *gd, **gdp;
1858 ide_drive_t *drive, *d;
1859 ide_hwif_t *hwif, *g;
1860 ide_hwgroup_t *hwgroup;
1861 int irq_count = 0, unit, i;
1862 unsigned long flags;
1863 unsigned int p, minor;
1864 ide_hwif_t old_hwif;
1866 if (index >= MAX_HWIFS)
1867 return;
1868 save_flags(flags); /* all CPUs */
1869 cli(); /* all CPUs */
1870 hwif = &ide_hwifs[index];
1871 if (!hwif->present)
1872 goto abort;
1873 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1874 drive = &hwif->drives[unit];
1875 if (!drive->present)
1876 continue;
1877 if (drive->busy || drive->usage)
1878 goto abort;
1879 if (drive->driver != NULL && DRIVER(drive)->cleanup(drive))
1880 goto abort;
1882 hwif->present = 0;
1885 * All clear? Then blow away the buffer cache
1887 sti();
1888 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1889 drive = &hwif->drives[unit];
1890 if (!drive->present)
1891 continue;
1892 minor = drive->select.b.unit << PARTN_BITS;
1893 for (p = 0; p < (1<<PARTN_BITS); ++p) {
1894 if (drive->part[p].nr_sects > 0) {
1895 kdev_t devp = MKDEV(hwif->major, minor+p);
1896 struct super_block * sb = get_super(devp);
1897 if (sb) invalidate_inodes(sb);
1898 invalidate_buffers (devp);
1901 #ifdef CONFIG_PROC_FS
1902 destroy_proc_ide_drives(hwif);
1903 #endif
1905 cli();
1906 hwgroup = hwif->hwgroup;
1909 * free the irq if we were the only hwif using it
1911 g = hwgroup->hwif;
1912 do {
1913 if (g->irq == hwif->irq)
1914 ++irq_count;
1915 g = g->next;
1916 } while (g != hwgroup->hwif);
1917 if (irq_count == 1)
1918 free_irq(hwif->irq, hwgroup);
1921 * Note that we only release the standard ports,
1922 * and do not even try to handle any extra ports
1923 * allocated for weird IDE interface chipsets.
1925 ide_release_region(hwif->io_ports[IDE_DATA_OFFSET], 8);
1926 if (hwif->io_ports[IDE_CONTROL_OFFSET])
1927 ide_release_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
1930 * Remove us from the hwgroup, and free
1931 * the hwgroup if we were the only member
1933 d = hwgroup->drive;
1934 for (i = 0; i < MAX_DRIVES; ++i) {
1935 drive = &hwif->drives[i];
1936 if (!drive->present)
1937 continue;
1938 while (hwgroup->drive->next != drive)
1939 hwgroup->drive = hwgroup->drive->next;
1940 hwgroup->drive->next = drive->next;
1941 if (hwgroup->drive == drive)
1942 hwgroup->drive = NULL;
1943 if (drive->id != NULL) {
1944 kfree(drive->id);
1945 drive->id = NULL;
1947 drive->present = 0;
1949 if (d->present)
1950 hwgroup->drive = d;
1951 while (hwgroup->hwif->next != hwif)
1952 hwgroup->hwif = hwgroup->hwif->next;
1953 hwgroup->hwif->next = hwif->next;
1954 if (hwgroup->hwif == hwif)
1955 kfree(hwgroup);
1956 else
1957 hwgroup->hwif = HWIF(hwgroup->drive);
1959 #ifdef CONFIG_BLK_DEV_IDEDMA
1960 if (hwif->dma_base)
1961 (void) ide_release_dma(hwif);
1962 #endif /* CONFIG_BLK_DEV_IDEDMA */
1965 * Remove us from the kernel's knowledge
1967 unregister_blkdev(hwif->major, hwif->name);
1968 kfree(blksize_size[hwif->major]);
1969 kfree(max_sectors[hwif->major]);
1970 kfree(max_readahead[hwif->major]);
1971 blk_dev[hwif->major].request_fn = NULL;
1972 blk_dev[hwif->major].data = NULL;
1973 blk_dev[hwif->major].queue = NULL;
1974 blksize_size[hwif->major] = NULL;
1975 for (gdp = &gendisk_head; *gdp; gdp = &((*gdp)->next))
1976 if (*gdp == hwif->gd)
1977 break;
1978 if (*gdp == NULL)
1979 printk("gd not in disk chain!\n");
1980 else {
1981 gd = *gdp; *gdp = gd->next;
1982 kfree(gd->sizes);
1983 kfree(gd->part);
1984 kfree(gd);
1986 old_hwif = *hwif;
1987 init_hwif_data (index); /* restore hwif data to pristine status */
1988 hwif->hwgroup = old_hwif.hwgroup;
1989 hwif->tuneproc = old_hwif.tuneproc;
1990 hwif->dmaproc = old_hwif.dmaproc;
1991 hwif->dma_base = old_hwif.dma_base;
1992 hwif->dma_extra = old_hwif.dma_extra;
1993 hwif->config_data = old_hwif.config_data;
1994 hwif->select_data = old_hwif.select_data;
1995 hwif->irq = old_hwif.irq;
1996 hwif->major = old_hwif.major;
1997 hwif->proc = old_hwif.proc;
1998 hwif->udma_four = old_hwif.udma_four;
1999 hwif->chipset = old_hwif.chipset;
2000 hwif->pci_dev = old_hwif.pci_dev;
2001 hwif->pci_devid = old_hwif.pci_devid;
2002 abort:
2003 restore_flags(flags); /* all CPUs */
2007 * Setup hw_regs_t structure described by parameters. You
2008 * may set up the hw structure yourself OR use this routine to
2009 * do it for you.
2011 void ide_setup_ports ( hw_regs_t *hw,
2012 ide_ioreg_t base, int *offsets,
2013 ide_ioreg_t ctrl, ide_ioreg_t intr,
2014 ide_ack_intr_t *ack_intr, int irq)
2016 int i;
2018 for (i = 0; i < IDE_NR_PORTS; i++) {
2019 if (offsets[i] == -1) {
2020 switch(i) {
2021 case IDE_CONTROL_OFFSET:
2022 hw->io_ports[i] = ctrl;
2023 break;
2024 case IDE_IRQ_OFFSET:
2025 hw->io_ports[i] = intr;
2026 break;
2027 default:
2028 hw->io_ports[i] = 0;
2029 break;
2031 } else {
2032 hw->io_ports[i] = base + offsets[i];
2035 hw->irq = irq;
2036 hw->ack_intr = ack_intr;
2040 * Register an IDE interface, specifing exactly the registers etc
2041 * Set init=1 iff calling before probes have taken place.
2043 int ide_register_hw (hw_regs_t *hw, ide_hwif_t **hwifp)
2045 int index, retry = 1;
2046 ide_hwif_t *hwif;
2048 do {
2049 for (index = 0; index < MAX_HWIFS; ++index) {
2050 hwif = &ide_hwifs[index];
2051 if (hwif->hw.io_ports[IDE_DATA_OFFSET] == hw->io_ports[IDE_DATA_OFFSET])
2052 goto found;
2054 for (index = 0; index < MAX_HWIFS; ++index) {
2055 hwif = &ide_hwifs[index];
2056 if ((!hwif->present && !hwif->mate && !initializing) ||
2057 (!hwif->hw.io_ports[IDE_DATA_OFFSET] && initializing))
2058 goto found;
2060 for (index = 0; index < MAX_HWIFS; index++)
2061 ide_unregister(index);
2062 } while (retry--);
2063 return -1;
2064 found:
2065 if (hwif->present)
2066 ide_unregister(index);
2067 if (hwif->present)
2068 return -1;
2069 memcpy(&hwif->hw, hw, sizeof(*hw));
2070 memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->hw.io_ports));
2071 hwif->irq = hw->irq;
2072 hwif->noprobe = 0;
2074 if (!initializing) {
2075 ide_init_module(IDE_PROBE_MODULE);
2076 #ifdef CONFIG_PROC_FS
2077 create_proc_ide_interfaces();
2078 #endif
2079 ide_init_module(IDE_DRIVER_MODULE);
2082 if (hwifp)
2083 *hwifp = hwif;
2085 return (initializing || hwif->present) ? index : -1;
2089 * Compatability function with existing drivers. If you want
2090 * something different, use the function above.
2092 int ide_register (int arg1, int arg2, int irq)
2094 hw_regs_t hw;
2095 ide_init_hwif_ports(&hw, (ide_ioreg_t) arg1, (ide_ioreg_t) arg2, NULL);
2096 hw.irq = irq;
2097 return ide_register_hw(&hw, NULL);
2100 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)
2102 ide_settings_t **p = (ide_settings_t **) &drive->settings, *setting = NULL;
2104 while ((*p) && strcmp((*p)->name, name) < 0)
2105 p = &((*p)->next);
2106 if ((setting = kmalloc(sizeof(*setting), GFP_KERNEL)) == NULL)
2107 goto abort;
2108 memset(setting, 0, sizeof(*setting));
2109 if ((setting->name = kmalloc(strlen(name) + 1, GFP_KERNEL)) == NULL)
2110 goto abort;
2111 strcpy(setting->name, name); setting->rw = rw;
2112 setting->read_ioctl = read_ioctl; setting->write_ioctl = write_ioctl;
2113 setting->data_type = data_type; setting->min = min;
2114 setting->max = max; setting->mul_factor = mul_factor;
2115 setting->div_factor = div_factor; setting->data = data;
2116 setting->set = set; setting->next = *p;
2117 if (drive->driver)
2118 setting->auto_remove = 1;
2119 *p = setting;
2120 return;
2121 abort:
2122 if (setting)
2123 kfree(setting);
2126 void ide_remove_setting(ide_drive_t *drive, char *name)
2128 ide_settings_t **p = (ide_settings_t **) &drive->settings, *setting;
2130 while ((*p) && strcmp((*p)->name, name))
2131 p = &((*p)->next);
2132 if ((setting = (*p)) == NULL)
2133 return;
2134 (*p) = setting->next;
2135 kfree(setting->name);
2136 kfree(setting);
2139 static ide_settings_t *ide_find_setting_by_ioctl(ide_drive_t *drive, int cmd)
2141 ide_settings_t *setting = drive->settings;
2143 while (setting) {
2144 if (setting->read_ioctl == cmd || setting->write_ioctl == cmd)
2145 break;
2146 setting = setting->next;
2148 return setting;
2151 ide_settings_t *ide_find_setting_by_name(ide_drive_t *drive, char *name)
2153 ide_settings_t *setting = drive->settings;
2155 while (setting) {
2156 if (strcmp(setting->name, name) == 0)
2157 break;
2158 setting = setting->next;
2160 return setting;
2163 static void auto_remove_settings(ide_drive_t *drive)
2165 ide_settings_t *setting;
2166 repeat:
2167 setting = drive->settings;
2168 while (setting) {
2169 if (setting->auto_remove) {
2170 ide_remove_setting(drive, setting->name);
2171 goto repeat;
2173 setting = setting->next;
2177 int ide_read_setting(ide_drive_t *drive, ide_settings_t *setting)
2179 int val = -EINVAL;
2180 unsigned long flags;
2182 if ((setting->rw & SETTING_READ)) {
2183 spin_lock_irqsave(&HWGROUP(drive)->spinlock, flags);
2184 switch(setting->data_type) {
2185 case TYPE_BYTE:
2186 val = *((u8 *) setting->data);
2187 break;
2188 case TYPE_SHORT:
2189 val = *((u16 *) setting->data);
2190 break;
2191 case TYPE_INT:
2192 case TYPE_INTA:
2193 val = *((u32 *) setting->data);
2194 break;
2196 spin_unlock_irqrestore(&HWGROUP(drive)->spinlock, flags);
2198 return val;
2201 int ide_spin_wait_hwgroup (ide_drive_t *drive, unsigned long *flags)
2203 ide_hwgroup_t *hwgroup = HWGROUP(drive);
2204 unsigned long timeout = jiffies + (3 * HZ);
2206 spin_lock_irqsave(&hwgroup->spinlock, *flags);
2207 while (hwgroup->busy) {
2208 spin_unlock_irqrestore(&hwgroup->spinlock, *flags);
2209 __sti(); /* local CPU only; needed for jiffies */
2210 if (0 < (signed long)(jiffies - timeout)) {
2211 printk("%s: channel busy\n", drive->name);
2212 return -EBUSY;
2214 spin_lock_irqsave(&hwgroup->spinlock, *flags);
2216 return 0;
2219 int ide_write_setting(ide_drive_t *drive, ide_settings_t *setting, int val)
2221 unsigned long flags;
2222 int i;
2223 u32 *p;
2225 if (!capable(CAP_SYS_ADMIN))
2226 return -EACCES;
2227 if (!(setting->rw & SETTING_WRITE))
2228 return -EPERM;
2229 if (val < setting->min || val > setting->max)
2230 return -EINVAL;
2231 if (setting->set)
2232 return setting->set(drive, val);
2233 if (ide_spin_wait_hwgroup(drive, &flags))
2234 return -EBUSY;
2235 switch (setting->data_type) {
2236 case TYPE_BYTE:
2237 *((u8 *) setting->data) = val;
2238 break;
2239 case TYPE_SHORT:
2240 *((u16 *) setting->data) = val;
2241 break;
2242 case TYPE_INT:
2243 *((u32 *) setting->data) = val;
2244 break;
2245 case TYPE_INTA:
2246 p = (u32 *) setting->data;
2247 for (i = 0; i < 1 << PARTN_BITS; i++, p++)
2248 *p = val;
2249 break;
2251 spin_unlock_irqrestore(&HWGROUP(drive)->spinlock, flags);
2252 return 0;
2255 static int set_io_32bit(ide_drive_t *drive, int arg)
2257 drive->io_32bit = arg;
2258 #ifdef CONFIG_BLK_DEV_DTC2278
2259 if (HWIF(drive)->chipset == ide_dtc2278)
2260 HWIF(drive)->drives[!drive->select.b.unit].io_32bit = arg;
2261 #endif /* CONFIG_BLK_DEV_DTC2278 */
2262 return 0;
2265 static int set_using_dma(ide_drive_t *drive, int arg)
2267 if (!drive->driver || !DRIVER(drive)->supports_dma)
2268 return -EPERM;
2269 if (!drive->id || !(drive->id->capability & 1) || !HWIF(drive)->dmaproc)
2270 return -EPERM;
2271 if (HWIF(drive)->dmaproc(arg ? ide_dma_on : ide_dma_off, drive))
2272 return -EIO;
2273 return 0;
2276 static int set_pio_mode(ide_drive_t *drive, int arg)
2278 struct request rq;
2280 if (!HWIF(drive)->tuneproc)
2281 return -ENOSYS;
2282 if (drive->special.b.set_tune)
2283 return -EBUSY;
2284 ide_init_drive_cmd(&rq);
2285 drive->tune_req = (byte) arg;
2286 drive->special.b.set_tune = 1;
2287 (void) ide_do_drive_cmd (drive, &rq, ide_wait);
2288 return 0;
2291 void ide_add_generic_settings(ide_drive_t *drive)
2294 * drive setting name read/write access read ioctl write ioctl data type min max mul_factor div_factor data pointer set function
2296 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);
2297 ide_add_setting(drive, "keepsettings", SETTING_RW, HDIO_GET_KEEPSETTINGS, HDIO_SET_KEEPSETTINGS, TYPE_BYTE, 0, 1, 1, 1, &drive->keep_settings, NULL);
2298 ide_add_setting(drive, "nice1", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->nice1, NULL);
2299 ide_add_setting(drive, "pio_mode", SETTING_WRITE, -1, HDIO_SET_PIO_MODE, TYPE_BYTE, 0, 255, 1, 1, NULL, set_pio_mode);
2300 ide_add_setting(drive, "slow", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->slow, NULL);
2301 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);
2302 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);
2305 int ide_wait_cmd (ide_drive_t *drive, int cmd, int nsect, int feature, int sectors, byte *buf)
2307 struct request rq;
2308 byte buffer[4];
2310 if (!buf)
2311 buf = buffer;
2312 memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
2313 ide_init_drive_cmd(&rq);
2314 rq.buffer = buf;
2315 *buf++ = cmd;
2316 *buf++ = nsect;
2317 *buf++ = feature;
2318 *buf++ = sectors;
2319 return ide_do_drive_cmd(drive, &rq, ide_wait);
2323 * Delay for *at least* 50ms. As we don't know how much time is left
2324 * until the next tick occurs, we wait an extra tick to be safe.
2325 * This is used only during the probing/polling for drives at boot time.
2327 * However, its usefullness may be needed in other places, thus we export it now.
2328 * The future may change this to a millisecond setable delay.
2330 void ide_delay_50ms (void)
2332 unsigned long timeout = jiffies + ((HZ + 19)/20) + 1;
2333 while (0 < (signed long)(timeout - jiffies));
2336 int ide_config_drive_speed (ide_drive_t *drive, byte speed)
2338 struct hd_driveid *id = drive->id;
2339 unsigned long flags;
2340 int err;
2342 __save_flags(flags); /* local CPU only */
2343 __cli(); /* local CPU only */
2346 * Don't use ide_wait_cmd here - it will
2347 * attempt to set_geometry and recalibrate,
2348 * but for some reason these don't work at
2349 * this point (lost interrupt).
2351 SELECT_DRIVE(HWIF(drive), drive);
2352 if (IDE_CONTROL_REG)
2353 OUT_BYTE(drive->ctl | 2, IDE_CONTROL_REG);
2354 OUT_BYTE(speed, IDE_NSECTOR_REG);
2355 OUT_BYTE(SETFEATURES_XFER, IDE_FEATURE_REG);
2356 OUT_BYTE(WIN_SETFEATURES, IDE_COMMAND_REG);
2358 err = ide_wait_stat(drive, DRIVE_READY, BUSY_STAT|DRQ_STAT|ERR_STAT, WAIT_CMD);
2360 #if 0
2361 if (IDE_CONTROL_REG)
2362 OUT_BYTE(drive->ctl, IDE_CONTROL_REG);
2363 #endif
2365 __restore_flags(flags); /* local CPU only */
2367 switch(speed) {
2368 case XFER_UDMA_4:
2369 if (!((id->dma_ultra >> 8) & 16)) {
2370 drive->id->dma_ultra &= ~0xFF00;
2371 drive->id->dma_ultra |= 0x1010;
2373 drive->id->dma_mword &= ~0x0F00;
2374 drive->id->dma_1word &= ~0x0F00;
2375 break;
2376 case XFER_UDMA_3:
2377 if (!((id->dma_ultra >> 8) & 8)) {
2378 drive->id->dma_ultra &= ~0xFF00;
2379 drive->id->dma_ultra |= 0x0808;
2381 drive->id->dma_mword &= ~0x0F00;
2382 drive->id->dma_1word &= ~0x0F00;
2383 break;
2384 case XFER_UDMA_2:
2385 if (!((id->dma_ultra >> 8) & 4)) {
2386 drive->id->dma_ultra &= ~0xFF00;
2387 drive->id->dma_ultra |= 0x0404;
2389 drive->id->dma_mword &= ~0x0F00;
2390 drive->id->dma_1word &= ~0x0F00;
2391 break;
2392 case XFER_UDMA_1:
2393 if (!((id->dma_ultra >> 8) & 2)) {
2394 drive->id->dma_ultra &= ~0xFF00;
2395 drive->id->dma_ultra |= 0x0202;
2397 drive->id->dma_mword &= ~0x0F00;
2398 drive->id->dma_1word &= ~0x0F00;
2399 break;
2400 case XFER_UDMA_0:
2401 if (!((id->dma_ultra >> 8) & 1)) {
2402 drive->id->dma_ultra &= ~0xFF00;
2403 drive->id->dma_ultra |= 0x0101;
2405 drive->id->dma_mword &= ~0x0F00;
2406 drive->id->dma_1word &= ~0x0F00;
2407 break;
2408 case XFER_MW_DMA_2:
2409 drive->id->dma_ultra &= ~0xFF00;
2410 if (!((id->dma_mword >> 8) & 4)) {
2411 drive->id->dma_mword &= ~0x0F00;
2412 drive->id->dma_mword |= 0x0404;
2414 drive->id->dma_1word &= ~0x0F00;
2415 break;
2416 case XFER_MW_DMA_1:
2417 drive->id->dma_ultra &= ~0xFF00;
2418 if (!((id->dma_mword >> 8) & 2)) {
2419 drive->id->dma_mword &= ~0x0F00;
2420 drive->id->dma_mword |= 0x0202;
2422 drive->id->dma_1word &= ~0x0F00;
2423 break;
2424 case XFER_MW_DMA_0:
2425 drive->id->dma_ultra &= ~0xFF00;
2426 if (!((id->dma_mword >> 8) & 1)) {
2427 drive->id->dma_mword &= ~0x0F00;
2428 drive->id->dma_mword |= 0x0101;
2430 drive->id->dma_1word &= ~0x0F00;
2431 break;
2432 case XFER_SW_DMA_2:
2433 drive->id->dma_ultra &= ~0xFF00;
2434 drive->id->dma_mword &= ~0x0F00;
2435 if (!((id->dma_1word >> 8) & 4)) {
2436 drive->id->dma_1word &= ~0x0F00;
2437 drive->id->dma_1word |= 0x0404;
2439 break;
2440 case XFER_SW_DMA_1:
2441 drive->id->dma_ultra &= ~0xFF00;
2442 drive->id->dma_mword &= ~0x0F00;
2443 if (!((id->dma_1word >> 8) & 2)) {
2444 drive->id->dma_1word &= ~0x0F00;
2445 drive->id->dma_1word |= 0x0202;
2447 break;
2448 case XFER_SW_DMA_0:
2449 drive->id->dma_ultra &= ~0xFF00;
2450 drive->id->dma_mword &= ~0x0F00;
2451 if (!((id->dma_1word >> 8) & 1)) {
2452 drive->id->dma_1word &= ~0x0F00;
2453 drive->id->dma_1word |= 0x0101;
2455 break;
2456 default:
2457 drive->id->dma_ultra &= ~0xFF00;
2458 drive->id->dma_mword &= ~0x0F00;
2459 drive->id->dma_1word &= ~0x0F00;
2460 break;
2463 return(err);
2466 static int ide_ioctl (struct inode *inode, struct file *file,
2467 unsigned int cmd, unsigned long arg)
2469 int err = 0, major, minor;
2470 ide_drive_t *drive;
2471 struct request rq;
2472 kdev_t dev;
2473 ide_settings_t *setting;
2475 if (!inode || !(dev = inode->i_rdev))
2476 return -EINVAL;
2477 major = MAJOR(dev); minor = MINOR(dev);
2478 if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
2479 return -ENODEV;
2481 if ((setting = ide_find_setting_by_ioctl(drive, cmd)) != NULL) {
2482 if (cmd == setting->read_ioctl) {
2483 err = ide_read_setting(drive, setting);
2484 return err >= 0 ? put_user(err, (long *) arg) : err;
2485 } else {
2486 if ((MINOR(inode->i_rdev) & PARTN_MASK))
2487 return -EINVAL;
2488 return ide_write_setting(drive, setting, arg);
2492 ide_init_drive_cmd (&rq);
2493 switch (cmd) {
2494 case HDIO_GETGEO:
2496 struct hd_geometry *loc = (struct hd_geometry *) arg;
2497 unsigned short bios_cyl = drive->bios_cyl; /* truncate */
2498 if (!loc || (drive->media != ide_disk && drive->media != ide_floppy)) return -EINVAL;
2499 if (put_user(drive->bios_head, (byte *) &loc->heads)) return -EFAULT;
2500 if (put_user(drive->bios_sect, (byte *) &loc->sectors)) return -EFAULT;
2501 if (put_user(bios_cyl, (unsigned short *) &loc->cylinders)) return -EFAULT;
2502 if (put_user((unsigned)drive->part[MINOR(inode->i_rdev)&PARTN_MASK].start_sect,
2503 (unsigned long *) &loc->start)) return -EFAULT;
2504 return 0;
2507 case BLKGETSIZE: /* Return device size */
2508 return put_user(drive->part[MINOR(inode->i_rdev)&PARTN_MASK].nr_sects, (long *) arg);
2510 case BLKRRPART: /* Re-read partition tables */
2511 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2512 return ide_revalidate_disk(inode->i_rdev);
2514 case HDIO_OBSOLETE_IDENTITY:
2515 case HDIO_GET_IDENTITY:
2516 if (MINOR(inode->i_rdev) & PARTN_MASK)
2517 return -EINVAL;
2518 if (drive->id == NULL)
2519 return -ENOMSG;
2520 if (copy_to_user((char *)arg, (char *)drive->id,
2521 (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
2522 return -EFAULT;
2523 return 0;
2525 case HDIO_GET_NICE:
2526 return put_user(drive->dsc_overlap << IDE_NICE_DSC_OVERLAP |
2527 drive->atapi_overlap << IDE_NICE_ATAPI_OVERLAP |
2528 drive->nice0 << IDE_NICE_0 |
2529 drive->nice1 << IDE_NICE_1 |
2530 drive->nice2 << IDE_NICE_2,
2531 (long *) arg);
2532 case HDIO_DRIVE_CMD:
2534 byte args[4], *argbuf = args;
2535 int argsize = 4;
2536 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2537 if (NULL == (void *) arg)
2538 return ide_do_drive_cmd(drive, &rq, ide_wait);
2539 if (copy_from_user(args, (void *)arg, 4))
2540 return -EFAULT;
2541 if (args[3]) {
2542 argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
2543 argbuf = kmalloc(argsize, GFP_KERNEL);
2544 if (argbuf == NULL)
2545 return -ENOMEM;
2546 memcpy(argbuf, args, 4);
2548 if ((((byte *)arg)[0] == WIN_SETFEATURES) &&
2549 (((byte *)arg)[1] > 66) &&
2550 (((byte *)arg)[2] == 3)) {
2551 if (!HWIF(drive)->udma_four) {
2552 printk("%s: Speed warnings UDMA 3/4 is not functional.\n", HWIF(drive)->name);
2553 goto abort;
2555 if ((drive->id->word93 & 0x2000) == 0) {
2556 printk("%s: Speed warnings UDMA 3/4 is not functional.\n", drive->name);
2557 goto abort;
2560 err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
2561 if (!err &&
2562 (((byte *)arg)[0] == WIN_SETFEATURES) &&
2563 (((byte *)arg)[1] >= 16) &&
2564 (((byte *)arg)[2] == 3) &&
2565 (drive->id->dma_ultra ||
2566 drive->id->dma_mword ||
2567 drive->id->dma_1word)) {
2570 * Re-read drive->id for possible DMA mode
2571 * change (copied from ide-probe.c)
2573 struct hd_driveid *id;
2574 unsigned long timeout, irqs, flags;
2576 probe_irq_off(probe_irq_on());
2577 irqs = probe_irq_on();
2578 if (IDE_CONTROL_REG)
2579 OUT_BYTE(drive->ctl,IDE_CONTROL_REG);
2580 ide_delay_50ms();
2581 OUT_BYTE(WIN_IDENTIFY, IDE_COMMAND_REG);
2582 timeout = jiffies + WAIT_WORSTCASE;
2583 do {
2584 if (0 < (signed long)(jiffies - timeout)) {
2585 if (irqs)
2586 (void) probe_irq_off(irqs);
2587 goto abort; /* drive timed-out */
2589 ide_delay_50ms(); /* give drive a breather */
2590 } while (IN_BYTE(IDE_ALTSTATUS_REG) & BUSY_STAT);
2591 ide_delay_50ms(); /* wait for IRQ and DRQ_STAT */
2592 if (!OK_STAT(GET_STAT(),DRQ_STAT,BAD_R_STAT))
2593 goto abort;
2594 __save_flags(flags); /* local CPU only */
2595 __cli(); /* local CPU only; some systems need this */
2596 id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC);
2597 ide_input_data(drive, id, SECTOR_WORDS);
2598 (void) GET_STAT(); /* clear drive IRQ */
2599 ide__sti(); /* local CPU only */
2600 __restore_flags(flags); /* local CPU only */
2601 ide_fix_driveid(id);
2602 if (id && id->cyls) {
2603 drive->id->dma_ultra = id->dma_ultra;
2604 drive->id->dma_mword = id->dma_mword;
2605 drive->id->dma_1word = id->dma_1word;
2606 /* anything more ? */
2607 #ifdef DEBUG
2608 printk("%s: dma_ultra=%04X, dma_mword=%04X, dma_1word=%04X\n",
2609 drive->name, id->dma_ultra, id->dma_mword, id->dma_1word);
2610 #endif
2611 kfree(id);
2614 abort:
2615 if (copy_to_user((void *)arg, argbuf, argsize))
2616 err = -EFAULT;
2617 if (argsize > 4)
2618 kfree(argbuf);
2619 return err;
2622 case HDIO_SCAN_HWIF:
2624 int args[3];
2625 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2626 if (copy_from_user(args, (void *)arg, 3 * sizeof(int)))
2627 return -EFAULT;
2628 if (ide_register(args[0], args[1], args[2]) == -1)
2629 return -EIO;
2630 return 0;
2632 case HDIO_UNREGISTER_HWIF:
2633 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2634 /* should I check here for arg > MAX_HWIFS, or
2635 just let ide_unregister fail silently? -- shaver */
2636 ide_unregister(arg);
2637 return 0;
2638 case HDIO_SET_NICE:
2639 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2640 if (drive->driver == NULL)
2641 return -EPERM;
2642 if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
2643 return -EPERM;
2644 drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
2645 if (drive->dsc_overlap && !DRIVER(drive)->supports_dsc_overlap) {
2646 drive->dsc_overlap = 0;
2647 return -EPERM;
2649 drive->nice1 = (arg >> IDE_NICE_1) & 1;
2650 return 0;
2652 case BLKROSET:
2653 case BLKROGET:
2654 case BLKFLSBUF:
2655 case BLKSSZGET:
2656 case BLKPG:
2657 return blk_ioctl(inode->i_rdev, cmd, arg);
2659 default:
2660 if (drive->driver != NULL)
2661 return DRIVER(drive)->ioctl(drive, inode, file, cmd, arg);
2662 return -EPERM;
2666 static int ide_check_media_change (kdev_t i_rdev)
2668 ide_drive_t *drive;
2670 if ((drive = get_info_ptr(i_rdev)) == NULL)
2671 return -ENODEV;
2672 if (drive->driver != NULL)
2673 return DRIVER(drive)->media_change(drive);
2674 return 0;
2677 void ide_fixstring (byte *s, const int bytecount, const int byteswap)
2679 byte *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */
2681 if (byteswap) {
2682 /* convert from big-endian to host byte order */
2683 for (p = end ; p != s;) {
2684 unsigned short *pp = (unsigned short *) (p -= 2);
2685 *pp = ntohs(*pp);
2689 /* strip leading blanks */
2690 while (s != end && *s == ' ')
2691 ++s;
2693 /* compress internal blanks and strip trailing blanks */
2694 while (s != end && *s) {
2695 if (*s++ != ' ' || (s != end && *s && *s != ' '))
2696 *p++ = *(s-1);
2699 /* wipe out trailing garbage */
2700 while (p != end)
2701 *p++ = '\0';
2707 char *ide_xfer_verbose (byte xfer_rate) {
2708 switch(xfer_rate) {
2709 case XFER_UDMA_4: return("UDMA 4");
2710 case XFER_UDMA_3: return("UDMA 3");
2711 case XFER_UDMA_2: return("UDMA 2");
2712 case XFER_UDMA_1: return("UDMA 1");
2713 case XFER_UDMA_0: return("UDMA 0");
2714 case XFER_MW_DMA_2: return("MW DMA 2");
2715 case XFER_MW_DMA_1: return("MW DMA 1");
2716 case XFER_MW_DMA_0: return("MW DMA 0");
2717 case XFER_SW_DMA_2: return("SW DMA 2");
2718 case XFER_SW_DMA_1: return("SW DMA 1");
2719 case XFER_SW_DMA_0: return("SW DMA 0");
2720 case XFER_PIO_4: return("PIO 4");
2721 case XFER_PIO_3: return("PIO 3");
2722 case XFER_PIO_2: return("PIO 2");
2723 case XFER_PIO_1: return("PIO 1");
2724 case XFER_PIO_0: return("PIO 0");
2725 case XFER_PIO_SLOW: return("PIO SLOW");
2726 default: return("XFER ERROR");
2731 * stridx() returns the offset of c within s,
2732 * or -1 if c is '\0' or not found within s.
2734 static int __init stridx (const char *s, char c)
2736 char *i = strchr(s, c);
2737 return (i && c) ? i - s : -1;
2741 * match_parm() does parsing for ide_setup():
2743 * 1. the first char of s must be '='.
2744 * 2. if the remainder matches one of the supplied keywords,
2745 * the index (1 based) of the keyword is negated and returned.
2746 * 3. if the remainder is a series of no more than max_vals numbers
2747 * separated by commas, the numbers are saved in vals[] and a
2748 * count of how many were saved is returned. Base10 is assumed,
2749 * and base16 is allowed when prefixed with "0x".
2750 * 4. otherwise, zero is returned.
2752 static int __init match_parm (char *s, const char *keywords[], int vals[], int max_vals)
2754 static const char *decimal = "0123456789";
2755 static const char *hex = "0123456789abcdef";
2756 int i, n;
2758 if (*s++ == '=') {
2760 * Try matching against the supplied keywords,
2761 * and return -(index+1) if we match one
2763 if (keywords != NULL) {
2764 for (i = 0; *keywords != NULL; ++i) {
2765 if (!strcmp(s, *keywords++))
2766 return -(i+1);
2770 * Look for a series of no more than "max_vals"
2771 * numeric values separated by commas, in base10,
2772 * or base16 when prefixed with "0x".
2773 * Return a count of how many were found.
2775 for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
2776 vals[n] = i;
2777 while ((i = stridx(decimal, *++s)) >= 0)
2778 vals[n] = (vals[n] * 10) + i;
2779 if (*s == 'x' && !vals[n]) {
2780 while ((i = stridx(hex, *++s)) >= 0)
2781 vals[n] = (vals[n] * 0x10) + i;
2783 if (++n == max_vals)
2784 break;
2785 if (*s == ',' || *s == ';')
2786 ++s;
2788 if (!*s)
2789 return n;
2791 return 0; /* zero = nothing matched */
2795 * ide_setup() gets called VERY EARLY during initialization,
2796 * to handle kernel "command line" strings beginning with "hdx="
2797 * or "ide". Here is the complete set currently supported:
2799 * "hdx=" is recognized for all "x" from "a" to "h", such as "hdc".
2800 * "idex=" is recognized for all "x" from "0" to "3", such as "ide1".
2802 * "hdx=noprobe" : drive may be present, but do not probe for it
2803 * "hdx=none" : drive is NOT present, ignore cmos and do not probe
2804 * "hdx=nowerr" : ignore the WRERR_STAT bit on this drive
2805 * "hdx=cdrom" : drive is present, and is a cdrom drive
2806 * "hdx=cyl,head,sect" : disk drive is present, with specified geometry
2807 * "hdx=noremap" : do not remap 0->1 even though EZD was detected
2808 * "hdx=autotune" : driver will attempt to tune interface speed
2809 * to the fastest PIO mode supported,
2810 * if possible for this drive only.
2811 * Not fully supported by all chipset types,
2812 * and quite likely to cause trouble with
2813 * older/odd IDE drives.
2815 * "hdx=slow" : insert a huge pause after each access to the data
2816 * port. Should be used only as a last resort.
2818 * "hdx=swapdata" : when the drive is a disk, byte swap all data
2819 * "hdx=bswap" : same as above..........
2820 * "hdx=flash" : allows for more than one ata_flash disk to be
2821 * registered. In most cases, only one device
2822 * will be present.
2824 * "idebus=xx" : inform IDE driver of VESA/PCI bus speed in MHz,
2825 * where "xx" is between 20 and 66 inclusive,
2826 * used when tuning chipset PIO modes.
2827 * For PCI bus, 25 is correct for a P75 system,
2828 * 30 is correct for P90,P120,P180 systems,
2829 * and 33 is used for P100,P133,P166 systems.
2830 * If in doubt, use idebus=33 for PCI.
2831 * As for VLB, it is safest to not specify it.
2833 * "idex=noprobe" : do not attempt to access/use this interface
2834 * "idex=base" : probe for an interface at the addr specified,
2835 * where "base" is usually 0x1f0 or 0x170
2836 * and "ctl" is assumed to be "base"+0x206
2837 * "idex=base,ctl" : specify both base and ctl
2838 * "idex=base,ctl,irq" : specify base, ctl, and irq number
2839 * "idex=autotune" : driver will attempt to tune interface speed
2840 * to the fastest PIO mode supported,
2841 * for all drives on this interface.
2842 * Not fully supported by all chipset types,
2843 * and quite likely to cause trouble with
2844 * older/odd IDE drives.
2845 * "idex=noautotune" : driver will NOT attempt to tune interface speed
2846 * This is the default for most chipsets,
2847 * except the cmd640.
2848 * "idex=serialize" : do not overlap operations on idex and ide(x^1)
2849 * "idex=four" : four drives on idex and ide(x^1) share same ports
2850 * "idex=reset" : reset interface before first use
2851 * "idex=dma" : enable DMA by default on both drives if possible
2852 * "idex=ata66" : informs the interface that it has an 80c cable
2853 * for chipsets that are ATA-66 capable, but
2854 * the ablity to bit test for detection is
2855 * currently unknown.
2857 * "splitfifo=betweenChan"
2858 * : FIFO Configuration of VIA 82c586(<nothing>,"A"or"B").
2859 * --see what follows...
2860 * "splitfifo=betweenChan,thresholdprim,thresholdsec"
2861 * : FIFO Configuration of VIA 82c586(<nothing>,"A" or "B").
2862 * betweenChan = 1(all FIFO's to primary channel)
2863 * , 2(all FIFO's to secondary channel)
2864 * , 3 or 4(evenly shared between them).
2865 * note: without FIFO, a channel is (u)dma disabled!
2866 * thresholdprim = 4, 3, 2 or 1
2867 * (standing for 1, 3/4, 1/2, 1/4).
2868 * Sets the threshold of FIFO to begin dma
2869 * transfer on the primary channel.
2870 * thresholdsec = cf upper, but for secondary channel.
2872 * The following are valid ONLY on ide0, (except dc4030)
2873 * and the defaults for the base,ctl ports must not be altered.
2875 * "ide0=dtc2278" : probe/support DTC2278 interface
2876 * "ide0=ht6560b" : probe/support HT6560B interface
2877 * "ide0=cmd640_vlb" : *REQUIRED* for VLB cards with the CMD640 chip
2878 * (not for PCI -- automatically detected)
2879 * "ide0=qd6580" : probe/support qd6580 interface
2880 * "ide0=ali14xx" : probe/support ali14xx chipsets (ALI M1439, M1443, M1445)
2881 * "ide0=umc8672" : probe/support umc8672 chipsets
2882 * "idex=dc4030" : probe/support Promise DC4030VL interface
2883 * "ide=doubler" : probe/support IDE doublers on Amiga
2885 void __init ide_setup (char *s)
2887 int i, vals[3];
2888 ide_hwif_t *hwif;
2889 ide_drive_t *drive;
2890 unsigned int hw, unit;
2891 const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
2892 const char max_hwif = '0' + (MAX_HWIFS - 1);
2894 printk("ide_setup: %s", s);
2896 #ifdef CONFIG_BLK_DEV_IDEDOUBLER
2897 if (!strcmp(s, "ide=doubler")) {
2898 extern int ide_doubler;
2900 printk("ide: Enabled support for IDE doublers\n");
2901 ide_doubler = 1;
2902 return;
2904 #endif /* CONFIG_BLK_DEV_IDEDOUBLER */
2906 init_ide_data ();
2909 * Look for drive options: "hdx="
2911 if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
2912 const char *hd_words[] = {"none", "noprobe", "nowerr", "cdrom",
2913 "serialize", "autotune", "noautotune",
2914 "slow", "swapdata", "bswap", "flash",
2915 "remap", "noremap", NULL};
2916 unit = s[2] - 'a';
2917 hw = unit / MAX_DRIVES;
2918 unit = unit % MAX_DRIVES;
2919 hwif = &ide_hwifs[hw];
2920 drive = &hwif->drives[unit];
2921 if (strncmp(s + 4, "ide-", 4) == 0) {
2922 strncpy(drive->driver_req, s + 4, 9);
2923 goto done;
2925 switch (match_parm(&s[3], hd_words, vals, 3)) {
2926 case -1: /* "none" */
2927 drive->nobios = 1; /* drop into "noprobe" */
2928 case -2: /* "noprobe" */
2929 drive->noprobe = 1;
2930 goto done;
2931 case -3: /* "nowerr" */
2932 drive->bad_wstat = BAD_R_STAT;
2933 hwif->noprobe = 0;
2934 goto done;
2935 case -4: /* "cdrom" */
2936 drive->present = 1;
2937 drive->media = ide_cdrom;
2938 hwif->noprobe = 0;
2939 goto done;
2940 case -5: /* "serialize" */
2941 printk(" -- USE \"ide%d=serialize\" INSTEAD", hw);
2942 goto do_serialize;
2943 case -6: /* "autotune" */
2944 drive->autotune = 1;
2945 goto done;
2946 case -7: /* "noautotune" */
2947 drive->autotune = 2;
2948 goto done;
2949 case -8: /* "slow" */
2950 drive->slow = 1;
2951 goto done;
2952 case -9: /* "swapdata" or "bswap" */
2953 case -10:
2954 drive->bswap = 1;
2955 goto done;
2956 case -11: /* "flash" */
2957 drive->ata_flash = 1;
2958 goto done;
2959 case -12: /* "remap" */
2960 drive->remap_0_to_1 = 1;
2961 goto done;
2962 case -13: /* "noremap" */
2963 drive->remap_0_to_1 = 2;
2964 goto done;
2965 case 3: /* cyl,head,sect */
2966 drive->media = ide_disk;
2967 drive->cyl = drive->bios_cyl = vals[0];
2968 drive->head = drive->bios_head = vals[1];
2969 drive->sect = drive->bios_sect = vals[2];
2970 drive->present = 1;
2971 drive->forced_geom = 1;
2972 hwif->noprobe = 0;
2973 goto done;
2974 default:
2975 goto bad_option;
2979 #if defined(CONFIG_BLK_DEV_VIA82CXXX)
2981 * Look for drive option "splitfifo=..."
2984 if (s[0] == 's' && s[1] == 'p' && s[2] == 'l' &&
2985 s[3] == 'i' && s[4] == 't' && s[5] == 'f' &&
2986 s[6] == 'i' && s[7] == 'f' && s[8] == 'o') {
2987 byte tmp = 0x3a; /* default config byte */
2989 i = match_parm(&s[9], NULL, vals, 3);
2990 switch(i) {
2991 case 3:
2992 tmp &= 0xf0;
2993 if ((vals[1] > 0) && (vals[1] < 5)) {
2994 /* sets threshold for primary Channel: */
2995 byte x = 4 - vals[1];
2996 tmp |= (x << 2);
2998 else
2999 goto bad_option;
3000 if ((vals[2] > 0) && (vals[2] < 5)) {
3001 /* sets threshold for secondary Channel: */
3002 byte x = 4 - vals[2];
3003 tmp |= x;
3005 else
3006 goto bad_option;
3007 case 1:
3008 /* set the FIFO config between channels to 0: */
3009 tmp &= 0x9f;
3010 /* set the needed FIFO config between channels: */
3011 if (vals[0] == 1) /* primary fifo only */
3012 tmp |= 0x10;
3013 else if (vals[0] == 2) /* secondary fifo only */
3014 tmp |= 0x70;
3015 else if (vals[0] == 4) /* other shared fifo config */
3016 tmp |= 0x50;
3017 else if (vals[0] == 3) /* default config */
3018 tmp |= 0x30;
3019 else
3020 goto bad_option;
3021 break;
3022 default:
3023 goto bad_option;
3025 /* set the found option in fifoconfig */
3026 fifoconfig = tmp;
3027 goto done;
3029 #endif /* defined(CONFIG_BLK_DEV_VIA82CXXX) */
3031 if (s[0] != 'i' || s[1] != 'd' || s[2] != 'e')
3032 goto bad_option;
3034 * Look for bus speed option: "idebus="
3036 if (s[3] == 'b' && s[4] == 'u' && s[5] == 's') {
3037 if (match_parm(&s[6], NULL, vals, 1) != 1)
3038 goto bad_option;
3039 if (vals[0] >= 20 && vals[0] <= 66)
3040 idebus_parameter = vals[0];
3041 else
3042 printk(" -- BAD BUS SPEED! Expected value from 20 to 66");
3043 goto done;
3046 * Look for interface options: "idex="
3048 if (s[3] >= '0' && s[3] <= max_hwif) {
3050 * Be VERY CAREFUL changing this: note hardcoded indexes below
3051 * -8,-9,-10 : are reserved for future idex calls to ease the hardcoding.
3053 const char *ide_words[] = {
3054 "noprobe", "serialize", "autotune", "noautotune", "reset", "dma", "ata66",
3055 "minus8", "minus9", "minus10",
3056 "four", "qd6580", "ht6560b", "cmd640_vlb", "dtc2278", "umc8672", "ali14xx", "dc4030", NULL };
3057 hw = s[3] - '0';
3058 hwif = &ide_hwifs[hw];
3059 i = match_parm(&s[4], ide_words, vals, 3);
3062 * Cryptic check to ensure chipset not already set for hwif:
3064 if (i > 0 || i <= -11) { /* is parameter a chipset name? */
3065 if (hwif->chipset != ide_unknown)
3066 goto bad_option; /* chipset already specified */
3067 if (i <= -11 && i != -18 && hw != 0)
3068 goto bad_hwif; /* chipset drivers are for "ide0=" only */
3069 if (i <= -11 && i != -18 && ide_hwifs[hw+1].chipset != ide_unknown)
3070 goto bad_option; /* chipset for 2nd port already specified */
3071 printk("\n");
3074 switch (i) {
3075 #ifdef CONFIG_BLK_DEV_PDC4030
3076 case -18: /* "dc4030" */
3078 extern void init_pdc4030(void);
3079 init_pdc4030();
3080 goto done;
3082 #endif /* CONFIG_BLK_DEV_PDC4030 */
3083 #ifdef CONFIG_BLK_DEV_ALI14XX
3084 case -17: /* "ali14xx" */
3086 extern void init_ali14xx (void);
3087 init_ali14xx();
3088 goto done;
3090 #endif /* CONFIG_BLK_DEV_ALI14XX */
3091 #ifdef CONFIG_BLK_DEV_UMC8672
3092 case -16: /* "umc8672" */
3094 extern void init_umc8672 (void);
3095 init_umc8672();
3096 goto done;
3098 #endif /* CONFIG_BLK_DEV_UMC8672 */
3099 #ifdef CONFIG_BLK_DEV_DTC2278
3100 case -15: /* "dtc2278" */
3102 extern void init_dtc2278 (void);
3103 init_dtc2278();
3104 goto done;
3106 #endif /* CONFIG_BLK_DEV_DTC2278 */
3107 #ifdef CONFIG_BLK_DEV_CMD640
3108 case -14: /* "cmd640_vlb" */
3110 extern int cmd640_vlb; /* flag for cmd640.c */
3111 cmd640_vlb = 1;
3112 goto done;
3114 #endif /* CONFIG_BLK_DEV_CMD640 */
3115 #ifdef CONFIG_BLK_DEV_HT6560B
3116 case -13: /* "ht6560b" */
3118 extern void init_ht6560b (void);
3119 init_ht6560b();
3120 goto done;
3122 #endif /* CONFIG_BLK_DEV_HT6560B */
3123 #if CONFIG_BLK_DEV_QD6580
3124 case -12: /* "qd6580" */
3126 extern void init_qd6580 (void);
3127 init_qd6580();
3128 goto done;
3130 #endif /* CONFIG_BLK_DEV_QD6580 */
3131 #ifdef CONFIG_BLK_DEV_4DRIVES
3132 case -11: /* "four" drives on one set of ports */
3134 ide_hwif_t *mate = &ide_hwifs[hw^1];
3135 mate->drives[0].select.all ^= 0x20;
3136 mate->drives[1].select.all ^= 0x20;
3137 hwif->chipset = mate->chipset = ide_4drives;
3138 mate->irq = hwif->irq;
3139 memcpy(mate->io_ports, hwif->io_ports, sizeof(hwif->io_ports));
3140 goto do_serialize;
3142 #endif /* CONFIG_BLK_DEV_4DRIVES */
3143 case -10: /* minus10 */
3144 case -9: /* minus9 */
3145 case -8: /* minus8 */
3146 goto bad_option;
3147 case -7: /* ata66 */
3148 #ifdef CONFIG_BLK_DEV_IDEPCI
3149 hwif->udma_four = 1;
3150 goto done;
3151 #else /* !CONFIG_BLK_DEV_IDEPCI */
3152 hwif->udma_four = 0;
3153 goto bad_hwif;
3154 #endif /* CONFIG_BLK_DEV_IDEPCI */
3155 case -6: /* dma */
3156 hwif->autodma = 1;
3157 goto done;
3158 case -5: /* "reset" */
3159 hwif->reset = 1;
3160 goto done;
3161 case -4: /* "noautotune" */
3162 hwif->drives[0].autotune = 2;
3163 hwif->drives[1].autotune = 2;
3164 goto done;
3165 case -3: /* "autotune" */
3166 hwif->drives[0].autotune = 1;
3167 hwif->drives[1].autotune = 1;
3168 goto done;
3169 case -2: /* "serialize" */
3170 do_serialize:
3171 hwif->mate = &ide_hwifs[hw^1];
3172 hwif->mate->mate = hwif;
3173 hwif->serialized = hwif->mate->serialized = 1;
3174 goto done;
3176 case -1: /* "noprobe" */
3177 hwif->noprobe = 1;
3178 goto done;
3180 case 1: /* base */
3181 vals[1] = vals[0] + 0x206; /* default ctl */
3182 case 2: /* base,ctl */
3183 vals[2] = 0; /* default irq = probe for it */
3184 case 3: /* base,ctl,irq */
3185 hwif->hw.irq = vals[2];
3186 ide_init_hwif_ports(&hwif->hw, (ide_ioreg_t) vals[0], (ide_ioreg_t) vals[1], &hwif->irq);
3187 memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->io_ports));
3188 hwif->irq = vals[2];
3189 hwif->noprobe = 0;
3190 hwif->chipset = ide_generic;
3191 goto done;
3193 case 0: goto bad_option;
3194 default:
3195 printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
3196 return;
3199 bad_option:
3200 printk(" -- BAD OPTION\n");
3201 return;
3202 bad_hwif:
3203 printk("-- NOT SUPPORTED ON ide%d", hw);
3204 done:
3205 printk("\n");
3209 * probe_for_hwifs() finds/initializes "known" IDE interfaces
3211 static void __init probe_for_hwifs (void)
3213 #ifdef CONFIG_PCI
3214 if (pci_present())
3216 #ifdef CONFIG_BLK_DEV_IDEPCI
3217 ide_scan_pcibus();
3218 #else
3219 #ifdef CONFIG_BLK_DEV_RZ1000
3221 extern void ide_probe_for_rz100x(void);
3222 ide_probe_for_rz100x();
3224 #endif /* CONFIG_BLK_DEV_RZ1000 */
3225 #endif /* CONFIG_BLK_DEV_IDEPCI */
3227 #endif /* CONFIG_PCI */
3229 #ifdef CONFIG_BLK_DEV_CMD640
3231 extern void ide_probe_for_cmd640x(void);
3232 ide_probe_for_cmd640x();
3234 #endif /* CONFIG_BLK_DEV_CMD640 */
3235 #ifdef CONFIG_BLK_DEV_PDC4030
3237 extern int ide_probe_for_pdc4030(void);
3238 (void) ide_probe_for_pdc4030();
3240 #endif /* CONFIG_BLK_DEV_PDC4030 */
3241 #ifdef CONFIG_BLK_DEV_IDE_PMAC
3243 extern void pmac_ide_probe(void);
3244 pmac_ide_probe();
3246 #endif /* CONFIG_BLK_DEV_IDE_PMAC */
3247 #ifdef CONFIG_BLK_DEV_IDE_ICSIDE
3249 extern void icside_init(void);
3250 icside_init();
3252 #endif /* CONFIG_BLK_DEV_IDE_ICSIDE */
3253 #ifdef CONFIG_BLK_DEV_IDE_RAPIDE
3255 extern void rapide_init(void);
3256 rapide_init();
3258 #endif /* CONFIG_BLK_DEV_IDE_RAPIDE */
3259 #ifdef CONFIG_BLK_DEV_GAYLE
3261 extern void gayle_init(void);
3262 gayle_init();
3264 #endif /* CONFIG_BLK_DEV_GAYLE */
3265 #ifdef CONFIG_BLK_DEV_FALCON_IDE
3267 extern void falconide_init(void);
3268 falconide_init();
3270 #endif /* CONFIG_BLK_DEV_FALCON_IDE */
3271 #ifdef CONFIG_BLK_DEV_MAC_IDE
3273 extern void macide_init(void);
3274 macide_init();
3276 #endif /* CONFIG_BLK_DEV_MAC_IDE */
3277 #ifdef CONFIG_BLK_DEV_BUDDHA
3279 extern void buddha_init(void);
3280 buddha_init();
3282 #endif /* CONFIG_BLK_DEV_BUDDHA */
3285 void __init ide_init_builtin_drivers (void)
3288 * Probe for special PCI and other "known" interface chipsets
3290 probe_for_hwifs ();
3292 #ifdef CONFIG_BLK_DEV_IDE
3293 #if defined(__mc68000__) || defined(CONFIG_APUS)
3294 if (ide_hwifs[0].io_ports[IDE_DATA_OFFSET]) {
3295 ide_get_lock(&ide_lock, NULL, NULL); /* for atari only */
3296 disable_irq(ide_hwifs[0].irq);
3298 #endif /* __mc68000__ || CONFIG_APUS */
3300 (void) ideprobe_init();
3302 #if defined(__mc68000__) || defined(CONFIG_APUS)
3303 if (ide_hwifs[0].io_ports[IDE_DATA_OFFSET]) {
3304 enable_irq(ide_hwifs[0].irq);
3305 ide_release_lock(&ide_lock); /* for atari only */
3307 #endif /* __mc68000__ || CONFIG_APUS */
3308 #endif /* CONFIG_BLK_DEV_IDE */
3310 #ifdef CONFIG_PROC_FS
3311 proc_ide_create();
3312 #endif
3315 * Attempt to match drivers for the available drives
3317 #ifdef CONFIG_BLK_DEV_IDEDISK
3318 (void) idedisk_init();
3319 #endif /* CONFIG_BLK_DEV_IDEDISK */
3320 #ifdef CONFIG_BLK_DEV_IDECD
3321 (void) ide_cdrom_init();
3322 #endif /* CONFIG_BLK_DEV_IDECD */
3323 #ifdef CONFIG_BLK_DEV_IDETAPE
3324 (void) idetape_init();
3325 #endif /* CONFIG_BLK_DEV_IDETAPE */
3326 #ifdef CONFIG_BLK_DEV_IDEFLOPPY
3327 (void) idefloppy_init();
3328 #endif /* CONFIG_BLK_DEV_IDEFLOPPY */
3329 #ifdef CONFIG_BLK_DEV_IDESCSI
3330 #ifdef CONFIG_SCSI
3331 (void) idescsi_init();
3332 #else
3333 #warning ide scsi-emulation selected but no SCSI-subsystem in kernel
3334 #endif
3335 #endif /* CONFIG_BLK_DEV_IDESCSI */
3338 static int default_cleanup (ide_drive_t *drive)
3340 return ide_unregister_subdriver(drive);
3343 static void default_do_request(ide_drive_t *drive, struct request *rq, unsigned long block)
3345 ide_end_request(0, HWGROUP(drive));
3348 static void default_end_request (byte uptodate, ide_hwgroup_t *hwgroup)
3350 ide_end_request(uptodate, hwgroup);
3353 static int default_ioctl (ide_drive_t *drive, struct inode *inode, struct file *file,
3354 unsigned int cmd, unsigned long arg)
3356 return -EIO;
3359 static int default_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
3361 drive->usage--;
3362 return -EIO;
3365 static void default_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
3369 static int default_check_media_change (ide_drive_t *drive)
3371 return 1;
3374 static void default_pre_reset (ide_drive_t *drive)
3378 static unsigned long default_capacity (ide_drive_t *drive)
3380 return 0x7fffffff; /* cdrom or tape */
3383 static void default_special (ide_drive_t *drive)
3385 special_t *s = &drive->special;
3387 s->all = 0;
3388 drive->mult_req = 0;
3391 static void setup_driver_defaults (ide_drive_t *drive)
3393 ide_driver_t *d = drive->driver;
3395 if (d->cleanup == NULL) d->cleanup = default_cleanup;
3396 if (d->do_request == NULL) d->do_request = default_do_request;
3397 if (d->end_request == NULL) d->end_request = default_end_request;
3398 if (d->ioctl == NULL) d->ioctl = default_ioctl;
3399 if (d->open == NULL) d->open = default_open;
3400 if (d->release == NULL) d->release = default_release;
3401 if (d->media_change == NULL) d->media_change = default_check_media_change;
3402 if (d->pre_reset == NULL) d->pre_reset = default_pre_reset;
3403 if (d->capacity == NULL) d->capacity = default_capacity;
3404 if (d->special == NULL) d->special = default_special;
3407 ide_drive_t *ide_scan_devices (byte media, const char *name, ide_driver_t *driver, int n)
3409 unsigned int unit, index, i;
3411 for (index = 0; index < MAX_HWIFS; ++index)
3412 if (ide_hwifs[index].present) goto search;
3413 ide_init_module(IDE_PROBE_MODULE);
3414 search:
3415 for (index = 0, i = 0; index < MAX_HWIFS; ++index) {
3416 ide_hwif_t *hwif = &ide_hwifs[index];
3417 if (!hwif->present)
3418 continue;
3419 for (unit = 0; unit < MAX_DRIVES; ++unit) {
3420 ide_drive_t *drive = &hwif->drives[unit];
3421 char *req = drive->driver_req;
3422 if (*req && !strstr(name, req))
3423 continue;
3424 if (drive->present && drive->media == media && drive->driver == driver && ++i > n)
3425 return drive;
3428 return NULL;
3431 int ide_register_subdriver (ide_drive_t *drive, ide_driver_t *driver, int version)
3433 unsigned long flags;
3435 save_flags(flags); /* all CPUs */
3436 cli(); /* all CPUs */
3437 if (version != IDE_SUBDRIVER_VERSION || !drive->present || drive->driver != NULL || drive->busy || drive->usage) {
3438 restore_flags(flags); /* all CPUs */
3439 return 1;
3441 drive->driver = driver;
3442 setup_driver_defaults(drive);
3443 restore_flags(flags); /* all CPUs */
3444 if (drive->autotune != 2) {
3445 if (driver->supports_dma && HWIF(drive)->dmaproc != NULL)
3446 (void) (HWIF(drive)->dmaproc(ide_dma_check, drive));
3447 drive->dsc_overlap = (drive->next != drive && driver->supports_dsc_overlap);
3448 drive->nice1 = 1;
3450 drive->revalidate = 1;
3451 #ifdef CONFIG_PROC_FS
3452 ide_add_proc_entries(drive->proc, generic_subdriver_entries, drive);
3453 ide_add_proc_entries(drive->proc, driver->proc, drive);
3454 #endif
3455 return 0;
3458 int ide_unregister_subdriver (ide_drive_t *drive)
3460 unsigned long flags;
3462 save_flags(flags); /* all CPUs */
3463 cli(); /* all CPUs */
3464 if (drive->usage || drive->busy || drive->driver == NULL || DRIVER(drive)->busy) {
3465 restore_flags(flags); /* all CPUs */
3466 return 1;
3468 #ifdef CONFIG_PROC_FS
3469 ide_remove_proc_entries(drive->proc, DRIVER(drive)->proc);
3470 ide_remove_proc_entries(drive->proc, generic_subdriver_entries);
3471 #endif
3472 auto_remove_settings(drive);
3473 drive->driver = NULL;
3474 restore_flags(flags); /* all CPUs */
3475 return 0;
3478 int ide_register_module (ide_module_t *module)
3480 ide_module_t *p = ide_modules;
3482 while (p) {
3483 if (p == module)
3484 return 1;
3485 p = p->next;
3487 module->next = ide_modules;
3488 ide_modules = module;
3489 revalidate_drives();
3490 return 0;
3493 void ide_unregister_module (ide_module_t *module)
3495 ide_module_t **p;
3497 for (p = &ide_modules; (*p) && (*p) != module; p = &((*p)->next));
3498 if (*p)
3499 *p = (*p)->next;
3502 struct file_operations ide_fops[] = {{
3503 NULL, /* lseek - default */
3504 block_read, /* read - general block-dev read */
3505 block_write, /* write - general block-dev write */
3506 NULL, /* readdir - bad */
3507 NULL, /* poll */
3508 ide_ioctl, /* ioctl */
3509 NULL, /* mmap */
3510 ide_open, /* open */
3511 NULL, /* flush */
3512 ide_release, /* release */
3513 block_fsync, /* fsync */
3514 NULL, /* fasync */
3515 ide_check_media_change, /* check_media_change */
3516 ide_revalidate_disk /* revalidate */
3519 EXPORT_SYMBOL(ide_hwifs);
3520 EXPORT_SYMBOL(ide_register_module);
3521 EXPORT_SYMBOL(ide_unregister_module);
3522 EXPORT_SYMBOL(ide_spin_wait_hwgroup);
3525 * Probe module
3527 EXPORT_SYMBOL(drive_is_flashcard);
3528 EXPORT_SYMBOL(ide_timer_expiry);
3529 EXPORT_SYMBOL(ide_intr);
3530 EXPORT_SYMBOL(ide_geninit);
3531 EXPORT_SYMBOL(ide_fops);
3532 EXPORT_SYMBOL(ide_get_queue);
3533 EXPORT_SYMBOL(do_ide0_request);
3534 EXPORT_SYMBOL(ide_add_generic_settings);
3535 #if MAX_HWIFS > 1
3536 EXPORT_SYMBOL(do_ide1_request);
3537 #endif /* MAX_HWIFS > 1 */
3538 #if MAX_HWIFS > 2
3539 EXPORT_SYMBOL(do_ide2_request);
3540 #endif /* MAX_HWIFS > 2 */
3541 #if MAX_HWIFS > 3
3542 EXPORT_SYMBOL(do_ide3_request);
3543 #endif /* MAX_HWIFS > 3 */
3544 #if MAX_HWIFS > 4
3545 EXPORT_SYMBOL(do_ide4_request);
3546 #endif /* MAX_HWIFS > 4 */
3547 #if MAX_HWIFS > 5
3548 EXPORT_SYMBOL(do_ide5_request);
3549 #endif /* MAX_HWIFS > 5 */
3550 #if MAX_HWIFS > 6
3551 EXPORT_SYMBOL(do_ide6_request);
3552 #endif /* MAX_HWIFS > 6 */
3553 #if MAX_HWIFS > 7
3554 EXPORT_SYMBOL(do_ide7_request);
3555 #endif /* MAX_HWIFS > 7 */
3556 #if MAX_HWIFS > 8
3557 EXPORT_SYMBOL(do_ide8_request);
3558 #endif /* MAX_HWIFS > 8 */
3559 #if MAX_HWIFS > 9
3560 EXPORT_SYMBOL(do_ide9_request);
3561 #endif /* MAX_HWIFS > 9 */
3564 * Driver module
3566 EXPORT_SYMBOL(ide_scan_devices);
3567 EXPORT_SYMBOL(ide_register_subdriver);
3568 EXPORT_SYMBOL(ide_unregister_subdriver);
3569 EXPORT_SYMBOL(ide_replace_subdriver);
3570 EXPORT_SYMBOL(ide_input_data);
3571 EXPORT_SYMBOL(ide_output_data);
3572 EXPORT_SYMBOL(atapi_input_bytes);
3573 EXPORT_SYMBOL(atapi_output_bytes);
3574 EXPORT_SYMBOL(ide_set_handler);
3575 EXPORT_SYMBOL(ide_dump_status);
3576 EXPORT_SYMBOL(ide_error);
3577 EXPORT_SYMBOL(ide_fixstring);
3578 EXPORT_SYMBOL(ide_wait_stat);
3579 EXPORT_SYMBOL(ide_do_reset);
3580 EXPORT_SYMBOL(ide_init_drive_cmd);
3581 EXPORT_SYMBOL(ide_do_drive_cmd);
3582 EXPORT_SYMBOL(ide_end_drive_cmd);
3583 EXPORT_SYMBOL(ide_end_request);
3584 EXPORT_SYMBOL(ide_revalidate_disk);
3585 EXPORT_SYMBOL(ide_cmd);
3586 EXPORT_SYMBOL(ide_wait_cmd);
3587 EXPORT_SYMBOL(ide_delay_50ms);
3588 EXPORT_SYMBOL(ide_config_drive_speed);
3589 EXPORT_SYMBOL(ide_stall_queue);
3590 #ifdef CONFIG_PROC_FS
3591 EXPORT_SYMBOL(ide_add_proc_entries);
3592 EXPORT_SYMBOL(ide_remove_proc_entries);
3593 EXPORT_SYMBOL(proc_ide_read_geometry);
3594 EXPORT_SYMBOL(create_proc_ide_interfaces);
3595 #endif
3596 EXPORT_SYMBOL(ide_add_setting);
3597 EXPORT_SYMBOL(ide_remove_setting);
3599 EXPORT_SYMBOL(ide_register_hw);
3600 EXPORT_SYMBOL(ide_register);
3601 EXPORT_SYMBOL(ide_unregister);
3602 EXPORT_SYMBOL(ide_setup_ports);
3604 EXPORT_SYMBOL(get_info_ptr);
3605 EXPORT_SYMBOL(current_capacity);
3608 * This is gets invoked once during initialization, to set *everything* up
3610 int __init ide_init (void)
3612 static char banner_printed = 0;
3614 if (!banner_printed) {
3615 printk(KERN_INFO "Uniform Multi-Platform E-IDE driver " REVISION "\n");
3616 banner_printed = 1;
3619 init_ide_data ();
3621 initializing = 1;
3622 ide_init_builtin_drivers();
3623 initializing = 0;
3625 return 0;
3628 #ifdef MODULE
3629 char *options = NULL;
3630 MODULE_PARM(options,"s");
3632 static void __init parse_options (char *line)
3634 char *next = line;
3636 if (line == NULL || !*line)
3637 return;
3638 while ((line = next) != NULL) {
3639 if ((next = strchr(line,' ')) != NULL)
3640 *next++ = 0;
3641 if (!strncmp(line,"ide",3) ||
3642 #ifdef CONFIG_BLK_DEV_VIA82CXXX
3643 !strncmp(line,"splitfifo",9) ||
3644 #endif /* CONFIG_BLK_DEV_VIA82CXXX */
3645 (!strncmp(line,"hd",2) && line[2] != '='))
3646 ide_setup(line);
3650 int init_module (void)
3652 parse_options(options);
3653 return ide_init();
3656 void cleanup_module (void)
3658 int index;
3660 for (index = 0; index < MAX_HWIFS; ++index) {
3661 ide_unregister(index);
3662 #ifdef CONFIG_BLK_DEV_IDEDMA
3663 if (ide_hwifs[index].dma_base)
3664 (void) ide_release_dma(&ide_hwifs[index]);
3665 #endif /* CONFIG_BLK_DEV_IDEDMA */
3667 #ifdef CONFIG_PROC_FS
3668 proc_ide_destroy();
3669 #endif
3671 #endif /* MODULE */