Import 2.1.122
[davej-history.git] / drivers / block / ide.c
blob6be9525ef19c0a078d0615946ae622b19bf518b4
1 /*
2 * linux/drivers/block/ide.c Version 6.18 August 16, 1998
4 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
5 */
7 /*
8 * Maintained by Mark Lord <mlord@pobox.com>
9 * and Gadi Oxman <gadio@netvision.net.il>
11 * This is the multiple IDE interface driver, as evolved from hd.c.
12 * It supports up to MAX_HWIFS IDE interfaces, on one or more IRQs (usually 14 & 15).
13 * There can be up to two drives per interface, as per the ATA-2 spec.
15 * Primary: ide0, port 0x1f0; major=3; hda is minor=0; hdb is minor=64
16 * Secondary: ide1, port 0x170; major=22; hdc is minor=0; hdd is minor=64
17 * Tertiary: ide2, port 0x???; major=33; hde is minor=0; hdf is minor=64
18 * Quaternary: ide3, port 0x???; major=34; hdg is minor=0; hdh is minor=64
19 * ...
21 * From hd.c:
22 * |
23 * | It traverses the request-list, using interrupts to jump between functions.
24 * | As nearly all functions can be called within interrupts, we may not sleep.
25 * | Special care is recommended. Have Fun!
26 * |
27 * | modified by Drew Eckhardt to check nr of hd's from the CMOS.
28 * |
29 * | Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
30 * | in the early extended-partition checks and added DM partitions.
31 * |
32 * | Early work on error handling by Mika Liljeberg (liljeber@cs.Helsinki.FI).
33 * |
34 * | IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
35 * | and general streamlining by Mark Lord (mlord@pobox.com).
37 * October, 1994 -- Complete line-by-line overhaul for linux 1.1.x, by:
39 * Mark Lord (mlord@pobox.com) (IDE Perf.Pkg)
40 * Delman Lee (delman@mipg.upenn.edu) ("Mr. atdisk2")
41 * Scott Snyder (snyder@fnald0.fnal.gov) (ATAPI IDE cd-rom)
43 * This was a rewrite of just about everything from hd.c, though some original
44 * code is still sprinkled about. Think of it as a major evolution, with
45 * inspiration from lots of linux users, esp. hamish@zot.apana.org.au
47 * Version 1.0 ALPHA initial code, primary i/f working okay
48 * Version 1.3 BETA dual i/f on shared irq tested & working!
49 * Version 1.4 BETA added auto probing for irq(s)
50 * Version 1.5 BETA added ALPHA (untested) support for IDE cd-roms,
51 * ...
52 * Version 5.50 allow values as small as 20 for idebus=
53 * Version 5.51 force non io_32bit in drive_cmd_intr()
54 * change delay_10ms() to delay_50ms() to fix problems
55 * Version 5.52 fix incorrect invalidation of removable devices
56 * add "hdx=slow" command line option
57 * Version 5.60 start to modularize the driver; the disk and ATAPI
58 * drivers can be compiled as loadable modules.
59 * move IDE probe code to ide-probe.c
60 * move IDE disk code to ide-disk.c
61 * add support for generic IDE device subdrivers
62 * add m68k code from Geert Uytterhoeven
63 * probe all interfaces by default
64 * add ioctl to (re)probe an interface
65 * Version 6.00 use per device request queues
66 * attempt to optimize shared hwgroup performance
67 * add ioctl to manually adjust bandwidth algorithms
68 * add kerneld support for the probe module
69 * fix bug in ide_error()
70 * fix bug in the first ide_get_lock() call for Atari
71 * don't flush leftover data for ATAPI devices
72 * Version 6.01 clear hwgroup->active while the hwgroup sleeps
73 * support HDIO_GETGEO for floppies
74 * Version 6.02 fix ide_ack_intr() call
75 * check partition table on floppies
76 * Version 6.03 handle bad status bit sequencing in ide_wait_stat()
77 * Version 6.10 deleted old entries from this list of updates
78 * replaced triton.c with ide-dma.c generic PCI DMA
79 * added support for BIOS-enabled UltraDMA
80 * rename all "promise" things to "pdc4030"
81 * fix EZ-DRIVE handling on small disks
82 * Version 6.11 fix probe error in ide_scan_devices()
83 * fix ancient "jiffies" polling bugs
84 * mask all hwgroup interrupts on each irq entry
85 * Version 6.12 integrate ioctl and proc interfaces
86 * fix parsing of "idex=" command line parameter
87 * Version 6.13 add support for ide4/ide5 courtesy rjones@orchestream.com
88 * Version 6.14 fixed IRQ sharing among PCI devices
89 * Version 6.15 added SMP awareness to IDE drivers
90 * Version 6.16 fixed various bugs; even more SMP friendly
91 * Version 6.17 fix for newest EZ-Drive problem
92 * Version 6.18 default unpartitioned-disk translation now "BIOS LBA"
94 * Some additional driver compile-time options are in ide.h
96 * To do, in likely order of completion:
97 * - modify kernel to obtain BIOS geometry for drives on 2nd/3rd/4th i/f
100 #undef REALLY_SLOW_IO /* most systems can safely undef this */
102 #define _IDE_C /* Tell ide.h it's really us */
104 #include <linux/config.h>
105 #include <linux/module.h>
106 #include <linux/types.h>
107 #include <linux/string.h>
108 #include <linux/kernel.h>
109 #include <linux/timer.h>
110 #include <linux/mm.h>
111 #include <linux/interrupt.h>
112 #include <linux/major.h>
113 #include <linux/errno.h>
114 #include <linux/genhd.h>
115 #include <linux/malloc.h>
116 #include <linux/pci.h>
117 #include <linux/delay.h>
119 #include <asm/byteorder.h>
120 #include <asm/irq.h>
121 #include <asm/uaccess.h>
122 #include <asm/io.h>
123 #include <asm/bitops.h>
125 #include "ide.h"
126 #include "ide_modes.h"
128 #ifdef CONFIG_KMOD
129 #include <linux/kmod.h>
130 #endif /* CONFIG_KMOD */
132 static const byte ide_hwif_to_major[] = {IDE0_MAJOR, IDE1_MAJOR, IDE2_MAJOR, IDE3_MAJOR, IDE4_MAJOR, IDE5_MAJOR };
134 static int idebus_parameter; /* holds the "idebus=" parameter */
135 static int system_bus_speed; /* holds what we think is VESA/PCI bus speed */
136 static int initializing; /* set while initializing built-in drivers */
139 * ide_lock is used by the Atari code to obtain access to the IDE interrupt,
140 * which is shared between several drivers.
142 static int ide_lock = 0;
145 * ide_modules keeps track of the available IDE chipset/probe/driver modules.
147 ide_module_t *ide_modules = NULL;
150 * This is declared extern in ide.h, for access by other IDE modules:
152 ide_hwif_t ide_hwifs[MAX_HWIFS]; /* master data repository */
154 #if (DISK_RECOVERY_TIME > 0)
156 * For really screwy hardware (hey, at least it *can* be used with Linux)
157 * we can enforce a minimum delay time between successive operations.
159 static unsigned long read_timer(void)
161 unsigned long t, flags;
162 int i;
164 __save_flags(flags); /* local CPU only */
165 __cli(); /* local CPU only */
166 t = jiffies * 11932;
167 outb_p(0, 0x43);
168 i = inb_p(0x40);
169 i |= inb(0x40) << 8;
170 __restore_flags(flags); /* local CPU only */
171 return (t - i);
173 #endif /* DISK_RECOVERY_TIME */
175 static inline void set_recovery_timer (ide_hwif_t *hwif)
177 #if (DISK_RECOVERY_TIME > 0)
178 hwif->last_time = read_timer();
179 #endif /* DISK_RECOVERY_TIME */
183 * Do not even *think* about calling this!
185 static void init_hwif_data (unsigned int index)
187 unsigned int unit;
188 ide_hwif_t *hwif = &ide_hwifs[index];
190 /* bulk initialize hwif & drive info with zeros */
191 memset(hwif, 0, sizeof(ide_hwif_t));
193 /* fill in any non-zero initial values */
194 hwif->index = index;
195 ide_init_hwif_ports(hwif->io_ports, ide_default_io_base(index), &hwif->irq);
196 hwif->noprobe = !hwif->io_ports[IDE_DATA_OFFSET];
197 #ifdef CONFIG_BLK_DEV_HD
198 if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA)
199 hwif->noprobe = 1; /* may be overridden by ide_setup() */
200 #endif /* CONFIG_BLK_DEV_HD */
201 hwif->major = ide_hwif_to_major[index];
202 hwif->name[0] = 'i';
203 hwif->name[1] = 'd';
204 hwif->name[2] = 'e';
205 hwif->name[3] = '0' + index;
206 for (unit = 0; unit < MAX_DRIVES; ++unit) {
207 ide_drive_t *drive = &hwif->drives[unit];
209 drive->media = ide_disk;
210 drive->select.all = (unit<<4)|0xa0;
211 drive->hwif = hwif;
212 drive->ctl = 0x08;
213 drive->ready_stat = READY_STAT;
214 drive->bad_wstat = BAD_W_STAT;
215 drive->special.b.recalibrate = 1;
216 drive->special.b.set_geometry = 1;
217 drive->name[0] = 'h';
218 drive->name[1] = 'd';
219 drive->name[2] = 'a' + (index * MAX_DRIVES) + unit;
224 * init_ide_data() sets reasonable default values into all fields
225 * of all instances of the hwifs and drives, but only on the first call.
226 * Subsequent calls have no effect (they don't wipe out anything).
228 * This routine is normally called at driver initialization time,
229 * but may also be called MUCH earlier during kernel "command-line"
230 * parameter processing. As such, we cannot depend on any other parts
231 * of the kernel (such as memory allocation) to be functioning yet.
233 * This is too bad, as otherwise we could dynamically allocate the
234 * ide_drive_t structs as needed, rather than always consuming memory
235 * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
237 #define MAGIC_COOKIE 0x12345678
238 static void init_ide_data (void)
240 unsigned int index;
241 static unsigned long magic_cookie = MAGIC_COOKIE;
243 if (magic_cookie != MAGIC_COOKIE)
244 return; /* already initialized */
245 magic_cookie = 0;
247 for (index = 0; index < MAX_HWIFS; ++index)
248 init_hwif_data(index);
250 idebus_parameter = 0;
251 system_bus_speed = 0;
255 * ide_system_bus_speed() returns what we think is the system VESA/PCI
256 * bus speed (in MHz). This is used for calculating interface PIO timings.
257 * The default is 40 for known PCI systems, 50 otherwise.
258 * The "idebus=xx" parameter can be used to override this value.
259 * The actual value to be used is computed/displayed the first time through.
261 int ide_system_bus_speed (void)
263 if (!system_bus_speed) {
264 if (idebus_parameter)
265 system_bus_speed = idebus_parameter; /* user supplied value */
266 #ifdef CONFIG_PCI
267 else if (pci_present())
268 system_bus_speed = 40; /* safe default value for PCI */
269 #endif /* CONFIG_PCI */
270 else
271 system_bus_speed = 50; /* safe default value for VESA and PCI */
272 printk("ide: Assuming %dMHz system bus speed for PIO modes%s\n", system_bus_speed,
273 idebus_parameter ? "" : "; override with idebus=xx");
275 return system_bus_speed;
278 #if SUPPORT_VLB_SYNC
280 * Some localbus EIDE interfaces require a special access sequence
281 * when using 32-bit I/O instructions to transfer data. We call this
282 * the "vlb_sync" sequence, which consists of three successive reads
283 * of the sector count register location, with interrupts disabled
284 * to ensure that the reads all happen together.
286 static inline void do_vlb_sync (ide_ioreg_t port) {
287 (void) inb (port);
288 (void) inb (port);
289 (void) inb (port);
291 #endif /* SUPPORT_VLB_SYNC */
294 * This is used for most PIO data transfers *from* the IDE interface
296 void ide_input_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
298 byte io_32bit = drive->io_32bit;
300 if (io_32bit) {
301 #if SUPPORT_VLB_SYNC
302 if (io_32bit & 2) {
303 unsigned long flags;
304 __save_flags(flags); /* local CPU only */
305 __cli(); /* local CPU only */
306 do_vlb_sync(IDE_NSECTOR_REG);
307 insl(IDE_DATA_REG, buffer, wcount);
308 __restore_flags(flags); /* local CPU only */
309 } else
310 #endif /* SUPPORT_VLB_SYNC */
311 insl(IDE_DATA_REG, buffer, wcount);
312 } else {
313 #if SUPPORT_SLOW_DATA_PORTS
314 if (drive->slow) {
315 unsigned short *ptr = (unsigned short *) buffer;
316 while (wcount--) {
317 *ptr++ = inw_p(IDE_DATA_REG);
318 *ptr++ = inw_p(IDE_DATA_REG);
320 } else
321 #endif /* SUPPORT_SLOW_DATA_PORTS */
322 insw(IDE_DATA_REG, buffer, wcount<<1);
327 * This is used for most PIO data transfers *to* the IDE interface
329 void ide_output_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
331 byte io_32bit = drive->io_32bit;
333 if (io_32bit) {
334 #if SUPPORT_VLB_SYNC
335 if (io_32bit & 2) {
336 unsigned long flags;
337 __save_flags(flags); /* local CPU only */
338 __cli(); /* local CPU only */
339 do_vlb_sync(IDE_NSECTOR_REG);
340 outsl(IDE_DATA_REG, buffer, wcount);
341 __restore_flags(flags); /* local CPU only */
342 } else
343 #endif /* SUPPORT_VLB_SYNC */
344 outsl(IDE_DATA_REG, buffer, wcount);
345 } else {
346 #if SUPPORT_SLOW_DATA_PORTS
347 if (drive->slow) {
348 unsigned short *ptr = (unsigned short *) buffer;
349 while (wcount--) {
350 outw_p(*ptr++, IDE_DATA_REG);
351 outw_p(*ptr++, IDE_DATA_REG);
353 } else
354 #endif /* SUPPORT_SLOW_DATA_PORTS */
355 outsw(IDE_DATA_REG, buffer, wcount<<1);
360 * The following routines are mainly used by the ATAPI drivers.
362 * These routines will round up any request for an odd number of bytes,
363 * so if an odd bytecount is specified, be sure that there's at least one
364 * extra byte allocated for the buffer.
366 void atapi_input_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount)
368 ++bytecount;
369 #ifdef CONFIG_ATARI
370 if (MACH_IS_ATARI) {
371 /* Atari has a byte-swapped IDE interface */
372 insw_swapw(IDE_DATA_REG, buffer, bytecount / 2);
373 return;
375 #endif /* CONFIG_ATARI */
376 ide_input_data (drive, buffer, bytecount / 4);
377 if ((bytecount & 0x03) >= 2)
378 insw (IDE_DATA_REG, ((byte *)buffer) + (bytecount & ~0x03), 1);
381 void atapi_output_bytes (ide_drive_t *drive, void *buffer, unsigned int bytecount)
383 ++bytecount;
384 #ifdef CONFIG_ATARI
385 if (MACH_IS_ATARI) {
386 /* Atari has a byte-swapped IDE interface */
387 outsw_swapw(IDE_DATA_REG, buffer, bytecount / 2);
388 return;
390 #endif /* CONFIG_ATARI */
391 ide_output_data (drive, buffer, bytecount / 4);
392 if ((bytecount & 0x03) >= 2)
393 outsw (IDE_DATA_REG, ((byte *)buffer) + (bytecount & ~0x03), 1);
397 * Needed for PCI irq sharing
399 static inline int drive_is_ready (ide_drive_t *drive)
401 if (drive->waiting_for_dma)
402 return HWIF(drive)->dmaproc(ide_dma_test_irq, drive);
403 #if 0
404 udelay(1); /* need to guarantee 400ns since last command was issued */
405 #endif
406 if (GET_STAT() & BUSY_STAT)
407 return 0; /* drive busy: definitely not interrupting */
408 return 1; /* drive ready: *might* be interrupting */
412 * This is our end_request replacement function.
414 void ide_end_request(byte uptodate, ide_hwgroup_t *hwgroup)
416 struct request *rq;
417 unsigned long flags;
419 spin_lock_irqsave(&io_request_lock, flags);
420 rq = hwgroup->rq;
422 if (!end_that_request_first(rq, uptodate, hwgroup->drive->name)) {
423 add_blkdev_randomness(MAJOR(rq->rq_dev));
424 hwgroup->drive->queue = rq->next;
425 blk_dev[MAJOR(rq->rq_dev)].current_request = NULL;
426 hwgroup->rq = NULL;
427 end_that_request_last(rq);
429 spin_unlock_irqrestore(&io_request_lock, flags);
433 * This should get invoked any time we exit the driver to
434 * wait for an interrupt response from a drive. handler() points
435 * at the appropriate code to handle the next interrupt, and a
436 * timer is started to prevent us from waiting forever in case
437 * something goes wrong (see the ide_timer_expiry() handler later on).
439 void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler, unsigned int timeout)
441 unsigned long flags;
442 ide_hwgroup_t *hwgroup = HWGROUP(drive);
444 spin_lock_irqsave(&hwgroup->spinlock, flags);
445 #ifdef DEBUG
446 if (hwgroup->handler != NULL) {
447 printk("%s: ide_set_handler: handler not null; old=%p, new=%p\n",
448 drive->name, hwgroup->handler, handler);
450 #endif
451 hwgroup->handler = handler;
452 hwgroup->timer.expires = jiffies + timeout;
453 add_timer(&(hwgroup->timer));
454 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
458 * current_capacity() returns the capacity (in sectors) of a drive
459 * according to its current geometry/LBA settings.
461 static unsigned long current_capacity (ide_drive_t *drive)
463 if (!drive->present)
464 return 0;
465 if (drive->driver != NULL)
466 return DRIVER(drive)->capacity(drive);
467 return 0;
471 * ide_geninit() is called exactly *once* for each major, from genhd.c,
472 * at the beginning of the initial partition check for the drives.
474 void ide_geninit (struct gendisk *gd)
476 unsigned int unit;
477 ide_hwif_t *hwif = gd->real_devices;
479 for (unit = 0; unit < gd->nr_real; ++unit) {
480 ide_drive_t *drive = &hwif->drives[unit];
482 drive->part[0].nr_sects = current_capacity(drive);
483 if (!drive->present || (drive->media != ide_disk && drive->media != ide_floppy) ||
484 drive->driver == NULL || !drive->part[0].nr_sects)
485 drive->part[0].start_sect = -1; /* skip partition check */
489 static void do_reset1 (ide_drive_t *, int); /* needed below */
492 * atapi_reset_pollfunc() gets invoked to poll the interface for completion every 50ms
493 * during an atapi drive reset operation. If the drive has not yet responded,
494 * and we have not yet hit our maximum waiting time, then the timer is restarted
495 * for another 50ms.
497 static void atapi_reset_pollfunc (ide_drive_t *drive)
499 ide_hwgroup_t *hwgroup = HWGROUP(drive);
500 byte stat;
502 SELECT_DRIVE(HWIF(drive),drive);
503 udelay (10);
505 if (OK_STAT(stat=GET_STAT(), 0, BUSY_STAT)) {
506 printk("%s: ATAPI reset complete\n", drive->name);
507 } else {
508 if (0 < (signed long)(hwgroup->poll_timeout - jiffies)) {
509 ide_set_handler (drive, &atapi_reset_pollfunc, HZ/20);
510 return; /* continue polling */
512 hwgroup->poll_timeout = 0; /* end of polling */
513 printk("%s: ATAPI reset timed-out, status=0x%02x\n", drive->name, stat);
514 do_reset1 (drive, 1); /* do it the old fashioned way */
515 return;
517 hwgroup->poll_timeout = 0; /* done polling */
521 * reset_pollfunc() gets invoked to poll the interface for completion every 50ms
522 * during an ide reset operation. If the drives have not yet responded,
523 * and we have not yet hit our maximum waiting time, then the timer is restarted
524 * for another 50ms.
526 static void reset_pollfunc (ide_drive_t *drive)
528 ide_hwgroup_t *hwgroup = HWGROUP(drive);
529 ide_hwif_t *hwif = HWIF(drive);
530 byte tmp;
532 if (!OK_STAT(tmp=GET_STAT(), 0, BUSY_STAT)) {
533 if (0 < (signed long)(hwgroup->poll_timeout - jiffies)) {
534 ide_set_handler (drive, &reset_pollfunc, HZ/20);
535 return; /* continue polling */
537 printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
538 } else {
539 printk("%s: reset: ", hwif->name);
540 if ((tmp = GET_ERR()) == 1)
541 printk("success\n");
542 else {
543 #if FANCY_STATUS_DUMPS
544 printk("master: ");
545 switch (tmp & 0x7f) {
546 case 1: printk("passed");
547 break;
548 case 2: printk("formatter device error");
549 break;
550 case 3: printk("sector buffer error");
551 break;
552 case 4: printk("ECC circuitry error");
553 break;
554 case 5: printk("controlling MPU error");
555 break;
556 default:printk("error (0x%02x?)", tmp);
558 if (tmp & 0x80)
559 printk("; slave: failed");
560 printk("\n");
561 #else
562 printk("failed\n");
563 #endif /* FANCY_STATUS_DUMPS */
566 hwgroup->poll_timeout = 0; /* done polling */
569 static void pre_reset (ide_drive_t *drive)
571 if (!drive->keep_settings) {
572 drive->unmask = 0;
573 drive->io_32bit = 0;
574 if (drive->using_dma)
575 (void) HWIF(drive)->dmaproc(ide_dma_off, drive);
577 if (drive->driver != NULL)
578 DRIVER(drive)->pre_reset(drive);
582 * do_reset1() attempts to recover a confused drive by resetting it.
583 * Unfortunately, resetting a disk drive actually resets all devices on
584 * the same interface, so it can really be thought of as resetting the
585 * interface rather than resetting the drive.
587 * ATAPI devices have their own reset mechanism which allows them to be
588 * individually reset without clobbering other devices on the same interface.
590 * Unfortunately, the IDE interface does not generate an interrupt to let
591 * us know when the reset operation has finished, so we must poll for this.
592 * Equally poor, though, is the fact that this may a very long time to complete,
593 * (up to 30 seconds worstcase). So, instead of busy-waiting here for it,
594 * we set a timer to poll at 50ms intervals.
596 static void do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
598 unsigned int unit;
599 unsigned long flags;
600 ide_hwif_t *hwif = HWIF(drive);
601 ide_hwgroup_t *hwgroup = HWGROUP(drive);
603 __save_flags(flags); /* local CPU only */
604 __cli(); /* local CPU only */
606 /* For an ATAPI device, first try an ATAPI SRST. */
607 if (drive->media != ide_disk && !do_not_try_atapi) {
608 pre_reset(drive);
609 SELECT_DRIVE(hwif,drive);
610 udelay (20);
611 OUT_BYTE (WIN_SRST, IDE_COMMAND_REG);
612 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
613 ide_set_handler (drive, &atapi_reset_pollfunc, HZ/20);
614 __restore_flags (flags); /* local CPU only */
615 return;
619 * First, reset any device state data we were maintaining
620 * for any of the drives on this interface.
622 for (unit = 0; unit < MAX_DRIVES; ++unit)
623 pre_reset(&hwif->drives[unit]);
625 #if OK_TO_RESET_CONTROLLER
627 * Note that we also set nIEN while resetting the device,
628 * to mask unwanted interrupts from the interface during the reset.
629 * However, due to the design of PC hardware, this will cause an
630 * immediate interrupt due to the edge transition it produces.
631 * This single interrupt gives us a "fast poll" for drives that
632 * recover from reset very quickly, saving us the first 50ms wait time.
634 OUT_BYTE(drive->ctl|6,IDE_CONTROL_REG); /* set SRST and nIEN */
635 udelay(10); /* more than enough time */
636 OUT_BYTE(drive->ctl|2,IDE_CONTROL_REG); /* clear SRST, leave nIEN */
637 udelay(10); /* more than enough time */
638 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
639 ide_set_handler (drive, &reset_pollfunc, HZ/20);
640 #endif /* OK_TO_RESET_CONTROLLER */
642 __restore_flags (flags); /* local CPU only */
646 * ide_do_reset() is the entry point to the drive/interface reset code.
648 void ide_do_reset (ide_drive_t *drive)
650 do_reset1 (drive, 0);
654 * Clean up after success/failure of an explicit drive cmd
656 void ide_end_drive_cmd (ide_drive_t *drive, byte stat, byte err)
658 unsigned long flags;
659 struct request *rq = HWGROUP(drive)->rq;
661 if (rq->cmd == IDE_DRIVE_CMD) {
662 byte *args = (byte *) rq->buffer;
663 rq->errors = !OK_STAT(stat,READY_STAT,BAD_STAT);
664 if (args) {
665 args[0] = stat;
666 args[1] = err;
667 args[2] = IN_BYTE(IDE_NSECTOR_REG);
670 spin_lock_irqsave(&io_request_lock, flags);
671 drive->queue = rq->next;
672 blk_dev[MAJOR(rq->rq_dev)].current_request = NULL;
673 HWGROUP(drive)->rq = NULL;
674 rq->rq_status = RQ_INACTIVE;
675 spin_unlock_irqrestore(&io_request_lock, flags);
676 save_flags(flags); /* all CPUs; overkill? */
677 cli(); /* all CPUs; overkill? */
678 if (rq->sem != NULL)
679 up(rq->sem); /* inform originator that rq has been serviced */
680 restore_flags(flags); /* all CPUs; overkill? */
684 * Error reporting, in human readable form (luxurious, but a memory hog).
686 byte ide_dump_status (ide_drive_t *drive, const char *msg, byte stat)
688 unsigned long flags;
689 byte err = 0;
691 __save_flags (flags); /* local CPU only */
692 ide__sti(); /* local CPU only */
693 printk("%s: %s: status=0x%02x", drive->name, msg, stat);
694 #if FANCY_STATUS_DUMPS
695 printk(" { ");
696 if (stat & BUSY_STAT)
697 printk("Busy ");
698 else {
699 if (stat & READY_STAT) printk("DriveReady ");
700 if (stat & WRERR_STAT) printk("DeviceFault ");
701 if (stat & SEEK_STAT) printk("SeekComplete ");
702 if (stat & DRQ_STAT) printk("DataRequest ");
703 if (stat & ECC_STAT) printk("CorrectedError ");
704 if (stat & INDEX_STAT) printk("Index ");
705 if (stat & ERR_STAT) printk("Error ");
707 printk("}");
708 #endif /* FANCY_STATUS_DUMPS */
709 printk("\n");
710 if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
711 err = GET_ERR();
712 printk("%s: %s: error=0x%02x", drive->name, msg, err);
713 #if FANCY_STATUS_DUMPS
714 if (drive->media == ide_disk) {
715 printk(" { ");
716 if (err & ABRT_ERR) printk("DriveStatusError ");
717 if (err & ICRC_ERR) printk((err & ABRT_ERR) ? "BadCRC " : "BadSector ");
718 if (err & ECC_ERR) printk("UncorrectableError ");
719 if (err & ID_ERR) printk("SectorIdNotFound ");
720 if (err & TRK0_ERR) printk("TrackZeroNotFound ");
721 if (err & MARK_ERR) printk("AddrMarkNotFound ");
722 printk("}");
723 if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR || (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
724 byte cur = IN_BYTE(IDE_SELECT_REG);
725 if (cur & 0x40) { /* using LBA? */
726 printk(", LBAsect=%ld", (unsigned long)
727 ((cur&0xf)<<24)
728 |(IN_BYTE(IDE_HCYL_REG)<<16)
729 |(IN_BYTE(IDE_LCYL_REG)<<8)
730 | IN_BYTE(IDE_SECTOR_REG));
731 } else {
732 printk(", CHS=%d/%d/%d",
733 (IN_BYTE(IDE_HCYL_REG)<<8) +
734 IN_BYTE(IDE_LCYL_REG),
735 cur & 0xf,
736 IN_BYTE(IDE_SECTOR_REG));
738 if (HWGROUP(drive)->rq)
739 printk(", sector=%ld", HWGROUP(drive)->rq->sector);
742 #endif /* FANCY_STATUS_DUMPS */
743 printk("\n");
745 __restore_flags (flags); /* local CPU only */
746 return err;
750 * try_to_flush_leftover_data() is invoked in response to a drive
751 * unexpectedly having its DRQ_STAT bit set. As an alternative to
752 * resetting the drive, this routine tries to clear the condition
753 * by read a sector's worth of data from the drive. Of course,
754 * this may not help if the drive is *waiting* for data from *us*.
756 static void try_to_flush_leftover_data (ide_drive_t *drive)
758 int i = (drive->mult_count ? drive->mult_count : 1) * SECTOR_WORDS;
760 if (drive->media != ide_disk)
761 return;
762 while (i > 0) {
763 unsigned long buffer[16];
764 unsigned int wcount = (i > 16) ? 16 : i;
765 i -= wcount;
766 ide_input_data (drive, buffer, wcount);
771 * ide_error() takes action based on the error returned by the drive.
773 void ide_error (ide_drive_t *drive, const char *msg, byte stat)
775 struct request *rq;
776 byte err;
778 err = ide_dump_status(drive, msg, stat);
779 if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
780 return;
781 /* retry only "normal" I/O: */
782 if (rq->cmd == IDE_DRIVE_CMD) {
783 rq->errors = 1;
784 ide_end_drive_cmd(drive, stat, err);
785 return;
787 if (stat & BUSY_STAT) { /* other bits are useless when BUSY */
788 rq->errors |= ERROR_RESET;
789 } else {
790 if (drive->media == ide_disk && (stat & ERR_STAT)) {
791 /* err has different meaning on cdrom and tape */
792 if (err == ABRT_ERR) {
793 if (drive->select.b.lba && IN_BYTE(IDE_COMMAND_REG) == WIN_SPECIFY)
794 return; /* some newer drives don't support WIN_SPECIFY */
795 } else if ((err & (ABRT_ERR | ICRC_ERR)) == (ABRT_ERR | ICRC_ERR))
796 ; /* UDMA crc error -- just retry the operation */
797 else if (err & (BBD_ERR | ECC_ERR)) /* retries won't help these */
798 rq->errors = ERROR_MAX;
799 else if (err & TRK0_ERR) /* help it find track zero */
800 rq->errors |= ERROR_RECAL;
802 if ((stat & DRQ_STAT) && rq->cmd != WRITE)
803 try_to_flush_leftover_data(drive);
805 if (GET_STAT() & (BUSY_STAT|DRQ_STAT))
806 rq->errors |= ERROR_RESET; /* Mmmm.. timing problem */
808 if (rq->errors >= ERROR_MAX) {
809 if (drive->driver != NULL)
810 DRIVER(drive)->end_request(0, HWGROUP(drive));
811 else
812 ide_end_request(0, HWGROUP(drive));
813 } else {
814 if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
815 ++rq->errors;
816 ide_do_reset(drive);
817 return;
818 } else if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
819 drive->special.b.recalibrate = 1;
820 ++rq->errors;
825 * Issue a simple drive command
826 * The drive must be selected beforehand.
828 void ide_cmd(ide_drive_t *drive, byte cmd, byte nsect, ide_handler_t *handler)
830 ide_set_handler (drive, handler, WAIT_CMD);
831 OUT_BYTE(drive->ctl,IDE_CONTROL_REG); /* clear nIEN */
832 OUT_BYTE(nsect,IDE_NSECTOR_REG);
833 OUT_BYTE(cmd,IDE_COMMAND_REG);
837 * drive_cmd_intr() is invoked on completion of a special DRIVE_CMD.
839 static void drive_cmd_intr (ide_drive_t *drive)
841 struct request *rq = HWGROUP(drive)->rq;
842 byte *args = (byte *) rq->buffer;
843 byte stat = GET_STAT();
844 int retries = 10;
846 ide__sti(); /* local CPU only */
847 if ((stat & DRQ_STAT) && args && args[3]) {
848 byte io_32bit = drive->io_32bit;
849 drive->io_32bit = 0;
850 ide_input_data(drive, &args[4], args[3] * SECTOR_WORDS);
851 drive->io_32bit = io_32bit;
852 while (((stat = GET_STAT()) & BUSY_STAT) && retries--)
853 udelay(100);
856 if (OK_STAT(stat, READY_STAT, BAD_STAT))
857 ide_end_drive_cmd (drive, stat, GET_ERR());
858 else
859 ide_error(drive, "drive_cmd", stat); /* calls ide_end_drive_cmd */
863 * do_special() is used to issue WIN_SPECIFY, WIN_RESTORE, and WIN_SETMULT
864 * commands to a drive. It used to do much more, but has been scaled back.
866 static inline void do_special (ide_drive_t *drive)
868 special_t *s = &drive->special;
870 #ifdef DEBUG
871 printk("%s: do_special: 0x%02x\n", drive->name, s->all);
872 #endif
873 if (s->b.set_tune) {
874 ide_tuneproc_t *tuneproc = HWIF(drive)->tuneproc;
875 s->b.set_tune = 0;
876 if (tuneproc != NULL)
877 tuneproc(drive, drive->tune_req);
878 } else if (drive->driver != NULL) {
879 DRIVER(drive)->special(drive);
880 } else if (s->all) {
881 printk("%s: bad special flag: 0x%02x\n", drive->name, s->all);
882 s->all = 0;
887 * This routine busy-waits for the drive status to be not "busy".
888 * It then checks the status for all of the "good" bits and none
889 * of the "bad" bits, and if all is okay it returns 0. All other
890 * cases return 1 after invoking ide_error() -- caller should just return.
892 * This routine should get fixed to not hog the cpu during extra long waits..
893 * That could be done by busy-waiting for the first jiffy or two, and then
894 * setting a timer to wake up at half second intervals thereafter,
895 * until timeout is achieved, before timing out.
897 int ide_wait_stat (ide_drive_t *drive, byte good, byte bad, unsigned long timeout)
899 byte stat;
900 unsigned long flags;
902 udelay(1); /* spec allows drive 400ns to assert "BUSY" */
903 if ((stat = GET_STAT()) & BUSY_STAT) {
904 __save_flags(flags); /* local CPU only */
905 ide__sti(); /* local CPU only */
906 timeout += jiffies;
907 while ((stat = GET_STAT()) & BUSY_STAT) {
908 if (0 < (signed long)(jiffies - timeout)) {
909 __restore_flags(flags); /* local CPU only */
910 ide_error(drive, "status timeout", stat);
911 return 1;
914 __restore_flags(flags); /* local CPU only */
916 udelay(1); /* allow status to settle, then read it again */
917 if (OK_STAT((stat = GET_STAT()), good, bad))
918 return 0;
919 ide_error(drive, "status error", stat);
920 return 1;
924 * execute_drive_cmd() issues a special drive command,
925 * usually initiated by ioctl() from the external hdparm program.
927 static void execute_drive_cmd (ide_drive_t *drive, struct request *rq)
929 byte *args = rq->buffer;
930 if (args) {
931 #ifdef DEBUG
932 printk("%s: DRIVE_CMD cmd=0x%02x sc=0x%02x fr=0x%02x xx=0x%02x\n",
933 drive->name, args[0], args[1], args[2], args[3]);
934 #endif
935 if (args[0] == WIN_SMART) {
936 OUT_BYTE(0x4f, IDE_LCYL_REG);
937 OUT_BYTE(0xc2, IDE_HCYL_REG);
939 OUT_BYTE(args[2],IDE_FEATURE_REG);
940 ide_cmd(drive, args[0], args[1], &drive_cmd_intr);
941 return;
942 } else {
944 * NULL is actually a valid way of waiting for
945 * all current requests to be flushed from the queue.
947 #ifdef DEBUG
948 printk("%s: DRIVE_CMD (null)\n", drive->name);
949 #endif
950 ide_end_drive_cmd(drive, GET_STAT(), GET_ERR());
951 return;
956 * start_request() initiates handling of a new I/O request
958 static inline void start_request (ide_drive_t *drive)
960 unsigned long block, blockend;
961 struct request *rq = drive->queue;
962 unsigned int minor = MINOR(rq->rq_dev), unit = minor >> PARTN_BITS;
963 ide_hwif_t *hwif = HWIF(drive);
965 ide__sti(); /* local CPU only */
966 #ifdef DEBUG
967 printk("%s: start_request: current=0x%08lx\n", hwif->name, (unsigned long) rq);
968 #endif
969 if (unit >= MAX_DRIVES) {
970 printk("%s: bad device number: %s\n", hwif->name, kdevname(rq->rq_dev));
971 goto kill_rq;
973 #ifdef DEBUG
974 if (rq->bh && !buffer_locked(rq->bh)) {
975 printk("%s: block not locked\n", drive->name);
976 goto kill_rq;
978 #endif
979 block = rq->sector;
980 blockend = block + rq->nr_sectors;
981 if ((blockend < block) || (blockend > drive->part[minor&PARTN_MASK].nr_sects)) {
982 printk("%s%c: bad access: block=%ld, count=%ld\n", drive->name,
983 (minor&PARTN_MASK)?'0'+(minor&PARTN_MASK):' ', block, rq->nr_sectors);
984 goto kill_rq;
986 block += drive->part[minor&PARTN_MASK].start_sect + drive->sect0;
987 #if FAKE_FDISK_FOR_EZDRIVE
988 if (block == 0 && drive->remap_0_to_1)
989 block = 1; /* redirect MBR access to EZ-Drive partn table */
990 #endif /* FAKE_FDISK_FOR_EZDRIVE */
991 #if (DISK_RECOVERY_TIME > 0)
992 while ((read_timer() - hwif->last_time) < DISK_RECOVERY_TIME);
993 #endif
994 SELECT_DRIVE(hwif, drive);
995 if (ide_wait_stat(drive, drive->ready_stat, BUSY_STAT|DRQ_STAT, WAIT_READY)) {
996 printk("%s: drive not ready for command\n", drive->name);
997 return;
999 if (!drive->special.all) {
1000 if (rq->cmd == IDE_DRIVE_CMD) {
1001 execute_drive_cmd(drive, rq);
1002 return;
1004 if (drive->driver != NULL) {
1005 DRIVER(drive)->do_request(drive, rq, block);
1006 return;
1008 printk("%s: media type %d not supported\n", drive->name, drive->media);
1009 goto kill_rq;
1011 do_special(drive);
1012 return;
1013 kill_rq:
1014 if (drive->driver != NULL)
1015 DRIVER(drive)->end_request(0, HWGROUP(drive));
1016 else
1017 ide_end_request(0, HWGROUP(drive));
1021 * ide_stall_queue() can be used by a drive to give excess bandwidth back
1022 * to the hwgroup by sleeping for timeout jiffies.
1024 void ide_stall_queue (ide_drive_t *drive, unsigned long timeout)
1026 if (timeout > WAIT_WORSTCASE)
1027 timeout = WAIT_WORSTCASE;
1028 drive->sleep = timeout + jiffies;
1031 #define WAKEUP(drive) ((drive)->service_start + 2 * (drive)->service_time)
1034 * choose_drive() selects the next drive which will be serviced.
1036 static inline ide_drive_t *choose_drive (ide_hwgroup_t *hwgroup)
1038 ide_drive_t *drive, *best;
1040 repeat:
1041 best = NULL;
1042 drive = hwgroup->drive;
1043 do {
1044 if (drive->queue && (!drive->sleep || 0 <= (signed long)(jiffies - drive->sleep))) {
1045 if (!best
1046 || (drive->sleep && (!best->sleep || 0 < (signed long)(best->sleep - drive->sleep)))
1047 || (!best->sleep && 0 < (signed long)(WAKEUP(best) - WAKEUP(drive))))
1049 struct blk_dev_struct *bdev = &blk_dev[HWIF(drive)->major];
1050 if (bdev->current_request != &bdev->plug)
1051 best = drive;
1054 } while ((drive = drive->next) != hwgroup->drive);
1055 if (best && best->nice1 && !best->sleep && best != hwgroup->drive && best->service_time > WAIT_MIN_SLEEP) {
1056 long t = (signed long)(WAKEUP(best) - jiffies);
1057 if (t >= WAIT_MIN_SLEEP) {
1059 * We *may* have some time to spare, but first let's see if
1060 * someone can potentially benefit from our nice mood today..
1062 drive = best->next;
1063 do {
1064 if (!drive->sleep
1065 && 0 < (signed long)(WAKEUP(drive) - (jiffies - best->service_time))
1066 && 0 < (signed long)((jiffies + t) - WAKEUP(drive)))
1068 ide_stall_queue(best, IDE_MIN(t, 10 * WAIT_MIN_SLEEP));
1069 goto repeat;
1071 } while ((drive = drive->next) != best);
1074 return best;
1078 * Caller must have already acquired spinlock using *spinflags
1080 static void ide_do_request (ide_hwgroup_t *hwgroup, unsigned long *hwgroup_flags, int masked_irq)
1082 struct blk_dev_struct *bdev;
1083 ide_drive_t *drive;
1084 ide_hwif_t *hwif;
1085 unsigned long io_flags;
1087 hwgroup->busy = 1;
1088 while (hwgroup->handler == NULL) {
1089 spin_lock_irqsave(&io_request_lock, io_flags);
1090 drive = choose_drive(hwgroup);
1091 if (drive == NULL) {
1092 unsigned long sleep = 0;
1094 hwgroup->rq = NULL;
1095 drive = hwgroup->drive;
1096 do {
1097 bdev = &blk_dev[HWIF(drive)->major];
1098 if (bdev->current_request != &bdev->plug) /* FIXME: this will do for now */
1099 bdev->current_request = NULL; /* (broken since patch-2.1.15) */
1100 if (drive->sleep && (!sleep || 0 < (signed long)(sleep - drive->sleep)))
1101 sleep = drive->sleep;
1102 } while ((drive = drive->next) != hwgroup->drive);
1103 spin_unlock_irqrestore(&io_request_lock, io_flags);
1104 if (sleep) {
1105 if (0 < (signed long)(jiffies + WAIT_MIN_SLEEP - sleep))
1106 sleep = jiffies + WAIT_MIN_SLEEP;
1107 #if 1
1108 if (hwgroup->timer.next || hwgroup->timer.prev)
1109 printk("ide_set_handler: timer already active\n");
1110 #endif
1111 mod_timer(&hwgroup->timer, sleep);
1112 } else {
1113 /* Ugly, but how can we sleep for the lock otherwise? perhaps from tq_scheduler? */
1114 ide_release_lock(&ide_lock); /* for atari only */
1116 hwgroup->busy = 0;
1117 return;
1119 hwif = HWIF(drive);
1120 if (hwgroup->hwif->sharing_irq && hwif != hwgroup->hwif) /* set nIEN for previous hwif */
1121 OUT_BYTE(hwgroup->drive->ctl|2, hwgroup->hwif->io_ports[IDE_CONTROL_OFFSET]);
1122 hwgroup->hwif = hwif;
1123 hwgroup->drive = drive;
1124 drive->sleep = 0;
1125 drive->service_start = jiffies;
1127 bdev = &blk_dev[hwif->major];
1128 if (bdev->current_request == &bdev->plug) /* FIXME: paranoia */
1129 printk("%s: Huh? nuking plugged queue\n", drive->name);
1130 bdev->current_request = hwgroup->rq = drive->queue;
1131 spin_unlock_irqrestore(&io_request_lock, io_flags);
1133 if (hwif->irq != masked_irq)
1134 disable_irq(hwif->irq);
1135 spin_unlock_irqrestore(&hwgroup->spinlock, *hwgroup_flags);
1136 start_request(drive);
1137 spin_lock_irqsave(&hwgroup->spinlock, *hwgroup_flags);
1138 if (hwif->irq != masked_irq)
1139 enable_irq(hwif->irq);
1144 * ide_get_queue() returns the queue which corresponds to a given device.
1146 struct request **ide_get_queue (kdev_t dev)
1148 ide_hwif_t *hwif = (ide_hwif_t *)blk_dev[MAJOR(dev)].data;
1150 return &hwif->drives[DEVICE_NR(dev) & 1].queue;
1154 * do_hwgroup_request() invokes ide_do_request() after claiming hwgroup->busy.
1156 static void do_hwgroup_request (ide_hwgroup_t *hwgroup)
1158 unsigned long flags;
1160 spin_lock_irqsave(&hwgroup->spinlock, flags);
1161 if (hwgroup->busy) {
1162 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1163 return;
1165 del_timer(&hwgroup->timer);
1166 ide_get_lock(&ide_lock, ide_intr, hwgroup); /* for atari only */
1167 ide_do_request(hwgroup, &flags, 0);
1168 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1172 * ll_rw_blk.c invokes our do_idex_request() function
1173 * with the io_request_spinlock already grabbed.
1174 * Since we need to do our own spinlock's internally,
1175 * on paths that don't necessarily originate through the
1176 * do_idex_request() path, we have to undo the spinlock on entry,
1177 * and restore it again on exit.
1178 * Fortunately, this is mostly a nop for non-SMP kernels.
1180 static inline void unlock_do_hwgroup_request (ide_hwgroup_t *hwgroup)
1182 spin_unlock(&io_request_lock);
1183 do_hwgroup_request (hwgroup);
1184 spin_lock_irq(&io_request_lock);
1187 void do_ide0_request (void)
1189 unlock_do_hwgroup_request (ide_hwifs[0].hwgroup);
1192 #if MAX_HWIFS > 1
1193 void do_ide1_request (void)
1195 unlock_do_hwgroup_request (ide_hwifs[1].hwgroup);
1197 #endif /* MAX_HWIFS > 1 */
1199 #if MAX_HWIFS > 2
1200 void do_ide2_request (void)
1202 unlock_do_hwgroup_request (ide_hwifs[2].hwgroup);
1204 #endif /* MAX_HWIFS > 2 */
1206 #if MAX_HWIFS > 3
1207 void do_ide3_request (void)
1209 unlock_do_hwgroup_request (ide_hwifs[3].hwgroup);
1211 #endif /* MAX_HWIFS > 3 */
1213 #if MAX_HWIFS > 4
1214 void do_ide4_request (void)
1216 unlock_do_hwgroup_request (ide_hwifs[4].hwgroup);
1218 #endif /* MAX_HWIFS > 4 */
1220 #if MAX_HWIFS > 5
1221 void do_ide5_request (void)
1223 unlock_do_hwgroup_request (ide_hwifs[5].hwgroup);
1225 #endif /* MAX_HWIFS > 5 */
1227 static void start_next_request (ide_hwgroup_t *hwgroup, int masked_irq)
1229 unsigned long flags;
1230 ide_drive_t *drive;
1232 spin_lock_irqsave(&hwgroup->spinlock, flags);
1233 if (hwgroup->handler != NULL) {
1234 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1235 return;
1237 drive = hwgroup->drive;
1238 set_recovery_timer(HWIF(drive));
1239 drive->service_time = jiffies - drive->service_start;
1240 ide_do_request(hwgroup, &flags, masked_irq);
1241 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1244 void ide_timer_expiry (unsigned long data)
1246 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *) data;
1247 ide_drive_t *drive;
1248 ide_handler_t *handler;
1249 unsigned long flags;
1251 spin_lock_irqsave(&hwgroup->spinlock, flags);
1252 drive = hwgroup->drive;
1253 if ((handler = hwgroup->handler) == NULL) {
1254 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1255 do_hwgroup_request(hwgroup);
1256 return;
1258 hwgroup->busy = 1; /* should already be "1" */
1259 hwgroup->handler = NULL;
1260 del_timer(&hwgroup->timer); /* Is this needed?? */
1261 if (hwgroup->poll_timeout != 0) { /* polling in progress? */
1262 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1263 handler(drive);
1264 } else if (drive_is_ready(drive)) {
1265 printk("%s: lost interrupt\n", drive->name);
1266 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1267 handler(drive);
1268 } else {
1269 if (drive->waiting_for_dma) {
1270 (void) hwgroup->hwif->dmaproc(ide_dma_end, drive);
1271 printk("%s: timeout waiting for DMA\n", drive->name);
1273 * need something here for HX PIIX3 UDMA and HPT343.......AMH
1274 * irq timeout: status=0x58 { DriveReady SeekComplete DataRequest }
1277 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1278 ide_error(drive, "irq timeout", GET_STAT());
1280 start_next_request(hwgroup, 0);
1284 * There's nothing really useful we can do with an unexpected interrupt,
1285 * other than reading the status register (to clear it), and logging it.
1286 * There should be no way that an irq can happen before we're ready for it,
1287 * so we needn't worry much about losing an "important" interrupt here.
1289 * On laptops (and "green" PCs), an unexpected interrupt occurs whenever the
1290 * drive enters "idle", "standby", or "sleep" mode, so if the status looks
1291 * "good", we just ignore the interrupt completely.
1293 * This routine assumes __cli() is in effect when called.
1295 * If an unexpected interrupt happens on irq15 while we are handling irq14
1296 * and if the two interfaces are "serialized" (CMD640), then it looks like
1297 * we could screw up by interfering with a new request being set up for irq15.
1299 * In reality, this is a non-issue. The new command is not sent unless the
1300 * drive is ready to accept one, in which case we know the drive is not
1301 * trying to interrupt us. And ide_set_handler() is always invoked before
1302 * completing the issuance of any new drive command, so we will not be
1303 * accidently invoked as a result of any valid command completion interrupt.
1306 static void unexpected_intr (int irq, ide_hwgroup_t *hwgroup)
1308 byte stat;
1309 ide_hwif_t *hwif = hwgroup->hwif;
1312 * handle the unexpected interrupt
1314 do {
1315 if (hwif->irq == irq) {
1316 stat = IN_BYTE(hwif->io_ports[IDE_STATUS_OFFSET]);
1317 if (!OK_STAT(stat, READY_STAT, BAD_STAT)) {
1318 /* Try to not flood the console with msgs */
1319 static unsigned long last_msgtime = 0, count = 0;
1320 ++count;
1321 if (0 < (signed long)(jiffies - (last_msgtime + HZ))) {
1322 last_msgtime = jiffies;
1323 printk("%s%s: unexpected interrupt, status=0x%02x, count=%ld\n",
1324 hwif->name, (hwif->next == hwgroup->hwif) ? "" : "(?)", stat, count);
1328 } while ((hwif = hwif->next) != hwgroup->hwif);
1331 * entry point for all interrupts, caller does __cli() for us
1333 void ide_intr (int irq, void *dev_id, struct pt_regs *regs)
1335 unsigned long flags;
1336 ide_hwgroup_t *hwgroup = (ide_hwgroup_t *)dev_id;
1337 ide_hwif_t *hwif;
1338 ide_drive_t *drive;
1339 ide_handler_t *handler;
1341 __cli(); /* local CPU only */
1342 spin_lock_irqsave(&hwgroup->spinlock, flags);
1343 hwif = hwgroup->hwif;
1344 if ((handler = hwgroup->handler) == NULL || hwgroup->poll_timeout != 0) {
1346 * Not expecting an interrupt from this drive.
1347 * That means this could be:
1348 * (1) an interrupt from another PCI device
1349 * sharing the same PCI INT# as us.
1350 * or (2) a drive just entered sleep or standby mode,
1351 * and is interrupting to let us know.
1352 * or (3) a spurious interrupt of unknown origin.
1354 * For PCI, we cannot tell the difference,
1355 * so in that case we just ignore it and hope it goes away.
1357 #ifdef CONFIG_BLK_DEV_IDEPCI
1358 if (IDE_PCI_DEVID_EQ(hwif->pci_devid, IDE_PCI_DEVID_NULL))
1359 #endif /* CONFIG_BLK_DEV_IDEPCI */
1362 * Probably not a shared PCI interrupt,
1363 * so we can safely try to do something about it:
1365 (void)ide_ack_intr(hwif->io_ports[IDE_STATUS_OFFSET], hwif->io_ports[IDE_IRQ_OFFSET]);
1366 unexpected_intr(irq, hwgroup);
1368 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1369 return;
1371 drive = hwgroup->drive;
1372 if (!drive || !drive_is_ready(drive)) {
1373 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1374 return;
1376 hwgroup->handler = NULL;
1377 (void)ide_ack_intr(hwif->io_ports[IDE_STATUS_OFFSET], hwif->io_ports[IDE_IRQ_OFFSET]);
1378 del_timer(&(hwgroup->timer));
1379 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1380 if (drive->unmask)
1381 ide__sti(); /* local CPU only */
1382 handler(drive); /* service this interrupt, may set handler for next interrupt */
1384 * Note that handler() may have set things up for another
1385 * interrupt to occur soon, but it cannot happen until
1386 * we exit from this routine, because it will be the
1387 * same irq as is currently being serviced here,
1388 * and Linux won't allow another (on any CPU) until we return.
1390 start_next_request(hwgroup, hwif->irq);
1394 * get_info_ptr() returns the (ide_drive_t *) for a given device number.
1395 * It returns NULL if the given device number does not match any present drives.
1397 static ide_drive_t *get_info_ptr (kdev_t i_rdev)
1399 int major = MAJOR(i_rdev);
1400 unsigned int h;
1402 for (h = 0; h < MAX_HWIFS; ++h) {
1403 ide_hwif_t *hwif = &ide_hwifs[h];
1404 if (hwif->present && major == hwif->major) {
1405 unsigned unit = DEVICE_NR(i_rdev);
1406 if (unit < MAX_DRIVES) {
1407 ide_drive_t *drive = &hwif->drives[unit];
1408 if (drive->present)
1409 return drive;
1411 break;
1414 return NULL;
1418 * This function is intended to be used prior to invoking ide_do_drive_cmd().
1420 void ide_init_drive_cmd (struct request *rq)
1422 rq->buffer = NULL;
1423 rq->cmd = IDE_DRIVE_CMD;
1424 rq->sector = 0;
1425 rq->nr_sectors = 0;
1426 rq->current_nr_sectors = 0;
1427 rq->sem = NULL;
1428 rq->bh = NULL;
1429 rq->bhtail = NULL;
1430 rq->next = NULL;
1434 * This function issues a special IDE device request
1435 * onto the request queue.
1437 * If action is ide_wait, then then rq is queued at the end of
1438 * the request queue, and the function sleeps until it has been
1439 * processed. This is for use when invoked from an ioctl handler.
1441 * If action is ide_preempt, then the rq is queued at the head of
1442 * the request queue, displacing the currently-being-processed
1443 * request and this function returns immediately without waiting
1444 * for the new rq to be completed. This is VERY DANGEROUS, and is
1445 * intended for careful use by the ATAPI tape/cdrom driver code.
1447 * If action is ide_next, then the rq is queued immediately after
1448 * the currently-being-processed-request (if any), and the function
1449 * returns without waiting for the new rq to be completed. As above,
1450 * This is VERY DANGEROUS, and is intended for careful use by the
1451 * ATAPI tape/cdrom driver code.
1453 * If action is ide_end, then the rq is queued at the end of the
1454 * request queue, and the function returns immediately without waiting
1455 * for the new rq to be completed. This is again intended for careful
1456 * use by the ATAPI tape/cdrom driver code.
1458 int ide_do_drive_cmd (ide_drive_t *drive, struct request *rq, ide_action_t action)
1460 unsigned long flags;
1461 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1462 unsigned int major = HWIF(drive)->major;
1463 struct request *cur_rq;
1464 struct semaphore sem = MUTEX_LOCKED;
1466 if (IS_PDC4030_DRIVE && rq->buffer != NULL)
1467 return -ENOSYS; /* special drive cmds not supported */
1468 rq->errors = 0;
1469 rq->rq_status = RQ_ACTIVE;
1470 rq->rq_dev = MKDEV(major,(drive->select.b.unit)<<PARTN_BITS);
1471 if (action == ide_wait)
1472 rq->sem = &sem;
1474 spin_lock_irqsave(&io_request_lock, flags);
1475 cur_rq = drive->queue;
1476 if (cur_rq == NULL || action == ide_preempt) {
1477 rq->next = cur_rq;
1478 drive->queue = rq;
1479 if (action == ide_preempt)
1480 hwgroup->rq = NULL;
1481 } else {
1482 if (action == ide_wait || action == ide_end) {
1483 while (cur_rq->next != NULL) /* find end of list */
1484 cur_rq = cur_rq->next;
1486 rq->next = cur_rq->next;
1487 cur_rq->next = rq;
1489 spin_unlock_irqrestore(&io_request_lock, flags);
1490 do_hwgroup_request(hwgroup);
1491 save_flags(flags); /* all CPUs; overkill? */
1492 cli(); /* all CPUs; overkill? */
1493 if (action == ide_wait && rq->rq_status != RQ_INACTIVE)
1494 down(&sem); /* wait for it to be serviced */
1495 restore_flags(flags); /* all CPUs; overkill? */
1496 return rq->errors ? -EIO : 0; /* return -EIO if errors */
1500 * This routine is called to flush all partitions and partition tables
1501 * for a changed disk, and then re-read the new partition table.
1502 * If we are revalidating a disk because of a media change, then we
1503 * enter with usage == 0. If we are using an ioctl, we automatically have
1504 * usage == 1 (we need an open channel to use an ioctl :-), so this
1505 * is our limit.
1507 int ide_revalidate_disk(kdev_t i_rdev)
1509 ide_drive_t *drive;
1510 ide_hwgroup_t *hwgroup;
1511 unsigned int p, major, minor;
1512 long flags;
1514 if ((drive = get_info_ptr(i_rdev)) == NULL)
1515 return -ENODEV;
1516 major = MAJOR(i_rdev);
1517 minor = drive->select.b.unit << PARTN_BITS;
1518 hwgroup = HWGROUP(drive);
1519 spin_lock_irqsave(&hwgroup->spinlock, flags);
1520 if (drive->busy || (drive->usage > 1)) {
1521 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1522 return -EBUSY;
1524 drive->busy = 1;
1525 MOD_INC_USE_COUNT;
1526 spin_unlock_irqrestore(&hwgroup->spinlock, flags);
1528 for (p = 0; p < (1<<PARTN_BITS); ++p) {
1529 if (drive->part[p].nr_sects > 0) {
1530 kdev_t devp = MKDEV(major, minor+p);
1531 struct super_block * sb = get_super(devp);
1532 fsync_dev (devp);
1533 if (sb)
1534 invalidate_inodes(sb);
1535 invalidate_buffers (devp);
1536 set_blocksize(devp, 1024);
1538 drive->part[p].start_sect = 0;
1539 drive->part[p].nr_sects = 0;
1542 drive->part[0].nr_sects = current_capacity(drive);
1543 if ((drive->media != ide_disk && drive->media != ide_floppy) ||
1544 drive->driver == NULL || !drive->part[0].nr_sects)
1545 drive->part[0].start_sect = -1;
1546 resetup_one_dev(HWIF(drive)->gd, drive->select.b.unit);
1548 drive->busy = 0;
1549 wake_up(&drive->wqueue);
1550 MOD_DEC_USE_COUNT;
1551 return 0;
1554 static void revalidate_drives (void)
1556 ide_hwif_t *hwif;
1557 ide_drive_t *drive;
1558 int index, unit;
1560 for (index = 0; index < MAX_HWIFS; ++index) {
1561 hwif = &ide_hwifs[index];
1562 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1563 drive = &ide_hwifs[index].drives[unit];
1564 if (drive->revalidate) {
1565 drive->revalidate = 0;
1566 if (!initializing)
1567 (void) ide_revalidate_disk(MKDEV(hwif->major, unit<<PARTN_BITS));
1573 static void ide_init_module (int type)
1575 int found = 0;
1576 ide_module_t *module = ide_modules;
1578 while (module) {
1579 if (module->type == type) {
1580 found = 1;
1581 (void) module->init();
1583 module = module->next;
1585 revalidate_drives();
1586 #ifdef CONFIG_KMOD
1587 if (!found && type == IDE_PROBE_MODULE)
1588 (void) request_module("ide-probe");
1589 #endif /* CONFIG_KMOD */
1592 static int ide_open(struct inode * inode, struct file * filp)
1594 ide_drive_t *drive;
1595 int rc;
1597 if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
1598 return -ENXIO;
1599 MOD_INC_USE_COUNT;
1600 if (drive->driver == NULL)
1601 ide_init_module(IDE_DRIVER_MODULE);
1602 #ifdef CONFIG_KMOD
1603 if (drive->driver == NULL) {
1604 if (drive->media == ide_disk)
1605 (void) request_module("ide-disk");
1606 if (drive->media == ide_cdrom)
1607 (void) request_module("ide-cd");
1608 if (drive->media == ide_tape)
1609 (void) request_module("ide-tape");
1610 if (drive->media == ide_floppy)
1611 (void) request_module("ide-floppy");
1613 #endif /* CONFIG_KMOD */
1614 while (drive->busy)
1615 sleep_on(&drive->wqueue);
1616 drive->usage++;
1617 if (drive->driver != NULL) {
1618 if ((rc = DRIVER(drive)->open(inode, filp, drive)))
1619 MOD_DEC_USE_COUNT;
1620 return rc;
1622 printk ("%s: driver not present\n", drive->name);
1623 drive->usage--;
1624 MOD_DEC_USE_COUNT;
1625 return -ENXIO;
1629 * Releasing a block device means we sync() it, so that it can safely
1630 * be forgotten about...
1632 static int ide_release(struct inode * inode, struct file * file)
1634 ide_drive_t *drive;
1636 if ((drive = get_info_ptr(inode->i_rdev)) != NULL) {
1637 fsync_dev(inode->i_rdev);
1638 drive->usage--;
1639 if (drive->driver != NULL)
1640 DRIVER(drive)->release(inode, file, drive);
1641 MOD_DEC_USE_COUNT;
1643 return 0;
1646 int ide_replace_subdriver(ide_drive_t *drive, const char *driver)
1648 if (!drive->present || drive->busy || drive->usage)
1649 goto abort;
1650 if (drive->driver != NULL && DRIVER(drive)->cleanup(drive))
1651 goto abort;
1652 strncpy(drive->driver_req, driver, 9);
1653 ide_init_module(IDE_DRIVER_MODULE);
1654 drive->driver_req[0] = 0;
1655 ide_init_module(IDE_DRIVER_MODULE);
1656 if (DRIVER(drive) && !strcmp(DRIVER(drive)->name, driver))
1657 return 0;
1658 abort:
1659 return 1;
1662 void ide_unregister (unsigned int index)
1664 struct gendisk *gd, **gdp;
1665 ide_drive_t *drive, *d;
1666 ide_hwif_t *hwif, *g;
1667 ide_hwgroup_t *hwgroup;
1668 int irq_count = 0, unit, i;
1669 unsigned long flags;
1671 if (index >= MAX_HWIFS)
1672 return;
1673 save_flags(flags); /* all CPUs */
1674 cli(); /* all CPUs */
1675 hwif = &ide_hwifs[index];
1676 if (!hwif->present)
1677 goto abort;
1678 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1679 drive = &hwif->drives[unit];
1680 if (!drive->present)
1681 continue;
1682 if (drive->busy || drive->usage)
1683 goto abort;
1684 if (drive->driver != NULL && DRIVER(drive)->cleanup(drive))
1685 goto abort;
1687 hwif->present = 0;
1688 hwgroup = hwif->hwgroup;
1691 * free the irq if we were the only hwif using it
1693 g = hwgroup->hwif;
1694 do {
1695 if (g->irq == hwif->irq)
1696 ++irq_count;
1697 g = g->next;
1698 } while (g != hwgroup->hwif);
1699 if (irq_count == 1)
1700 free_irq(hwif->irq, hwgroup);
1703 * Note that we only release the standard ports,
1704 * and do not even try to handle any extra ports
1705 * allocated for weird IDE interface chipsets.
1707 ide_release_region(hwif->io_ports[IDE_DATA_OFFSET], 8);
1708 ide_release_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
1711 * Remove us from the hwgroup, and free
1712 * the hwgroup if we were the only member
1714 d = hwgroup->drive;
1715 for (i = 0; i < MAX_DRIVES; ++i) {
1716 drive = &hwif->drives[i];
1717 if (!drive->present)
1718 continue;
1719 while (hwgroup->drive->next != drive)
1720 hwgroup->drive = hwgroup->drive->next;
1721 hwgroup->drive->next = drive->next;
1722 if (hwgroup->drive == drive)
1723 hwgroup->drive = NULL;
1724 if (drive->id != NULL) {
1725 kfree(drive->id);
1726 drive->id = NULL;
1728 drive->present = 0;
1730 if (d->present)
1731 hwgroup->drive = d;
1732 while (hwgroup->hwif->next != hwif)
1733 hwgroup->hwif = hwgroup->hwif->next;
1734 hwgroup->hwif->next = hwif->next;
1735 if (hwgroup->hwif == hwif)
1736 kfree(hwgroup);
1737 else
1738 hwgroup->hwif = HWIF(hwgroup->drive);
1741 * Remove us from the kernel's knowledge
1743 unregister_blkdev(hwif->major, hwif->name);
1744 kfree(blksize_size[hwif->major]);
1745 kfree(max_sectors[hwif->major]);
1746 kfree(max_readahead[hwif->major]);
1747 blk_dev[hwif->major].request_fn = NULL;
1748 blk_dev[hwif->major].data = NULL;
1749 blk_dev[hwif->major].queue = NULL;
1750 blksize_size[hwif->major] = NULL;
1751 for (gdp = &gendisk_head; *gdp; gdp = &((*gdp)->next))
1752 if (*gdp == hwif->gd)
1753 break;
1754 if (*gdp == NULL)
1755 printk("gd not in disk chain!\n");
1756 else {
1757 gd = *gdp; *gdp = gd->next;
1758 kfree(gd->sizes);
1759 kfree(gd->part);
1760 kfree(gd);
1762 init_hwif_data (index); /* restore hwif data to pristine status */
1763 abort:
1764 restore_flags(flags); /* all CPUs */
1767 int ide_register (int arg1, int arg2, int irq)
1769 int index, retry = 1;
1770 ide_hwif_t *hwif;
1771 ide_ioreg_t data_port = (ide_ioreg_t) arg1, ctl_port = (ide_ioreg_t) arg2;
1773 do {
1774 for (index = 0; index < MAX_HWIFS; ++index) {
1775 hwif = &ide_hwifs[index];
1776 if (hwif->io_ports[IDE_DATA_OFFSET] == data_port)
1777 goto found;
1779 for (index = 0; index < MAX_HWIFS; ++index) {
1780 hwif = &ide_hwifs[index];
1781 if (!hwif->present)
1782 goto found;
1784 for (index = 0; index < MAX_HWIFS; index++)
1785 ide_unregister(index);
1786 } while (retry--);
1787 return -1;
1788 found:
1789 if (hwif->present)
1790 ide_unregister(index);
1791 if (hwif->present)
1792 return -1;
1793 ide_init_hwif_ports(hwif->io_ports, data_port, &hwif->irq);
1794 if (ctl_port)
1795 hwif->io_ports[IDE_CONTROL_OFFSET] = ctl_port;
1796 hwif->irq = irq;
1797 hwif->noprobe = 0;
1798 ide_init_module(IDE_PROBE_MODULE);
1799 ide_init_module(IDE_DRIVER_MODULE);
1800 return hwif->present ? index : -1;
1803 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)
1805 ide_settings_t **p = (ide_settings_t **) &drive->settings, *setting = NULL;
1807 while ((*p) && strcmp((*p)->name, name) < 0)
1808 p = &((*p)->next);
1809 if ((setting = kmalloc(sizeof(*setting), GFP_KERNEL)) == NULL)
1810 goto abort;
1811 memset(setting, 0, sizeof(*setting));
1812 if ((setting->name = kmalloc(strlen(name) + 1, GFP_KERNEL)) == NULL)
1813 goto abort;
1814 strcpy(setting->name, name); setting->rw = rw;
1815 setting->read_ioctl = read_ioctl; setting->write_ioctl = write_ioctl;
1816 setting->data_type = data_type; setting->min = min;
1817 setting->max = max; setting->mul_factor = mul_factor;
1818 setting->div_factor = div_factor; setting->data = data;
1819 setting->set = set; setting->next = *p;
1820 if (drive->driver)
1821 setting->auto_remove = 1;
1822 *p = setting;
1823 return;
1824 abort:
1825 if (setting)
1826 kfree(setting);
1829 void ide_remove_setting(ide_drive_t *drive, char *name)
1831 ide_settings_t **p = (ide_settings_t **) &drive->settings, *setting;
1833 while ((*p) && strcmp((*p)->name, name))
1834 p = &((*p)->next);
1835 if ((setting = (*p)) == NULL)
1836 return;
1837 (*p) = setting->next;
1838 kfree(setting->name);
1839 kfree(setting);
1842 static ide_settings_t *ide_find_setting_by_ioctl(ide_drive_t *drive, int cmd)
1844 ide_settings_t *setting = drive->settings;
1846 while (setting) {
1847 if (setting->read_ioctl == cmd || setting->write_ioctl == cmd)
1848 break;
1849 setting = setting->next;
1851 return setting;
1854 ide_settings_t *ide_find_setting_by_name(ide_drive_t *drive, char *name)
1856 ide_settings_t *setting = drive->settings;
1858 while (setting) {
1859 if (strcmp(setting->name, name) == 0)
1860 break;
1861 setting = setting->next;
1863 return setting;
1866 static void auto_remove_settings(ide_drive_t *drive)
1868 ide_settings_t *setting;
1869 repeat:
1870 setting = drive->settings;
1871 while (setting) {
1872 if (setting->auto_remove) {
1873 ide_remove_setting(drive, setting->name);
1874 goto repeat;
1876 setting = setting->next;
1880 int ide_read_setting(ide_drive_t *drive, ide_settings_t *setting)
1882 int val = -EINVAL;
1883 unsigned long flags;
1885 if ((setting->rw & SETTING_READ)) {
1886 spin_lock_irqsave(&HWGROUP(drive)->spinlock, flags);
1887 switch(setting->data_type) {
1888 case TYPE_BYTE:
1889 val = *((u8 *) setting->data);
1890 break;
1891 case TYPE_SHORT:
1892 val = *((u16 *) setting->data);
1893 break;
1894 case TYPE_INT:
1895 case TYPE_INTA:
1896 val = *((u32 *) setting->data);
1897 break;
1899 spin_unlock_irqrestore(&HWGROUP(drive)->spinlock, flags);
1901 return val;
1904 int ide_spin_wait_hwgroup (ide_drive_t *drive, unsigned long *flags)
1906 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1907 unsigned long timeout = jiffies + (3 * HZ);
1909 spin_lock_irqsave(&hwgroup->spinlock, *flags);
1910 while (hwgroup->busy) {
1911 spin_unlock_irqrestore(&hwgroup->spinlock, *flags);
1912 __sti(); /* local CPU only; needed for jiffies */
1913 if (0 < (signed long)(jiffies - timeout)) {
1914 printk("%s: channel busy\n", drive->name);
1915 return -EBUSY;
1917 spin_lock_irqsave(&hwgroup->spinlock, *flags);
1919 return 0;
1922 int ide_write_setting(ide_drive_t *drive, ide_settings_t *setting, int val)
1924 unsigned long flags;
1925 int i;
1926 u32 *p;
1928 if (!capable(CAP_SYS_ADMIN))
1929 return -EACCES;
1930 if (!(setting->rw & SETTING_WRITE))
1931 return -EPERM;
1932 if (val < setting->min || val > setting->max)
1933 return -EINVAL;
1934 if (setting->set)
1935 return setting->set(drive, val);
1936 if (ide_spin_wait_hwgroup(drive, &flags))
1937 return -EBUSY;
1938 switch (setting->data_type) {
1939 case TYPE_BYTE:
1940 *((u8 *) setting->data) = val;
1941 break;
1942 case TYPE_SHORT:
1943 *((u16 *) setting->data) = val;
1944 break;
1945 case TYPE_INT:
1946 *((u32 *) setting->data) = val;
1947 break;
1948 case TYPE_INTA:
1949 p = (u32 *) setting->data;
1950 for (i = 0; i < 1 << PARTN_BITS; i++, p++)
1951 *p = val;
1952 break;
1954 spin_unlock_irqrestore(&HWGROUP(drive)->spinlock, flags);
1955 return 0;
1958 static int set_io_32bit(ide_drive_t *drive, int arg)
1960 drive->io_32bit = arg;
1961 #ifdef CONFIG_BLK_DEV_DTC2278
1962 if (HWIF(drive)->chipset == ide_dtc2278)
1963 HWIF(drive)->drives[!drive->select.b.unit].io_32bit = arg;
1964 #endif /* CONFIG_BLK_DEV_DTC2278 */
1965 return 0;
1968 static int set_using_dma(ide_drive_t *drive, int arg)
1970 if (!drive->driver || !DRIVER(drive)->supports_dma)
1971 return -EPERM;
1972 if (!drive->id || !(drive->id->capability & 1) || !HWIF(drive)->dmaproc)
1973 return -EPERM;
1974 if (HWIF(drive)->dmaproc(arg ? ide_dma_on : ide_dma_off, drive))
1975 return -EIO;
1976 return 0;
1979 static int set_pio_mode(ide_drive_t *drive, int arg)
1981 struct request rq;
1983 if (!HWIF(drive)->tuneproc)
1984 return -ENOSYS;
1985 if (drive->special.b.set_tune)
1986 return -EBUSY;
1987 ide_init_drive_cmd(&rq);
1988 drive->tune_req = (byte) arg;
1989 drive->special.b.set_tune = 1;
1990 (void) ide_do_drive_cmd (drive, &rq, ide_wait);
1991 return 0;
1994 void ide_add_generic_settings(ide_drive_t *drive)
1997 * drive setting name read/write access read ioctl write ioctl data type min max mul_factor div_factor data pointer set function
1999 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);
2000 ide_add_setting(drive, "keepsettings", SETTING_RW, HDIO_GET_KEEPSETTINGS, HDIO_SET_KEEPSETTINGS, TYPE_BYTE, 0, 1, 1, 1, &drive->keep_settings, NULL);
2001 ide_add_setting(drive, "nice1", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->nice1, NULL);
2002 ide_add_setting(drive, "pio_mode", SETTING_WRITE, -1, HDIO_SET_PIO_MODE, TYPE_BYTE, 0, 255, 1, 1, NULL, set_pio_mode);
2003 ide_add_setting(drive, "slow", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->slow, NULL);
2004 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);
2005 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);
2008 int ide_wait_cmd (ide_drive_t *drive, int cmd, int nsect, int feature, int sectors, byte *buf)
2010 struct request rq;
2011 byte buffer[4];
2013 if (!buf)
2014 buf = buffer;
2015 memset(buf, 0, 4 + SECTOR_WORDS * 4 * sectors);
2016 ide_init_drive_cmd(&rq);
2017 rq.buffer = buf;
2018 *buf++ = cmd;
2019 *buf++ = nsect;
2020 *buf++ = feature;
2021 *buf++ = sectors;
2022 return ide_do_drive_cmd(drive, &rq, ide_wait);
2025 static int ide_ioctl (struct inode *inode, struct file *file,
2026 unsigned int cmd, unsigned long arg)
2028 int err, major, minor;
2029 ide_drive_t *drive;
2030 struct request rq;
2031 kdev_t dev;
2032 ide_settings_t *setting;
2034 if (!inode || !(dev = inode->i_rdev))
2035 return -EINVAL;
2036 major = MAJOR(dev); minor = MINOR(dev);
2037 if ((drive = get_info_ptr(inode->i_rdev)) == NULL)
2038 return -ENODEV;
2040 if ((setting = ide_find_setting_by_ioctl(drive, cmd)) != NULL) {
2041 if (cmd == setting->read_ioctl) {
2042 err = ide_read_setting(drive, setting);
2043 return err >= 0 ? put_user(err, (long *) arg) : err;
2044 } else {
2045 if ((MINOR(inode->i_rdev) & PARTN_MASK))
2046 return -EINVAL;
2047 return ide_write_setting(drive, setting, arg);
2051 ide_init_drive_cmd (&rq);
2052 switch (cmd) {
2053 case HDIO_GETGEO:
2055 struct hd_geometry *loc = (struct hd_geometry *) arg;
2056 if (!loc || (drive->media != ide_disk && drive->media != ide_floppy)) return -EINVAL;
2057 if (put_user(drive->bios_head, (byte *) &loc->heads)) return -EFAULT;
2058 if (put_user(drive->bios_sect, (byte *) &loc->sectors)) return -EFAULT;
2059 if (put_user(drive->bios_cyl, (unsigned short *) &loc->cylinders)) return -EFAULT;
2060 if (put_user((unsigned)drive->part[MINOR(inode->i_rdev)&PARTN_MASK].start_sect,
2061 (unsigned long *) &loc->start)) return -EFAULT;
2062 return 0;
2064 case BLKFLSBUF:
2065 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2066 fsync_dev(inode->i_rdev);
2067 invalidate_buffers(inode->i_rdev);
2068 return 0;
2070 case BLKGETSIZE: /* Return device size */
2071 return put_user(drive->part[MINOR(inode->i_rdev)&PARTN_MASK].nr_sects, (long *) arg);
2073 case BLKRRPART: /* Re-read partition tables */
2074 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2075 return ide_revalidate_disk(inode->i_rdev);
2077 case HDIO_OBSOLETE_IDENTITY:
2078 case HDIO_GET_IDENTITY:
2079 if (MINOR(inode->i_rdev) & PARTN_MASK)
2080 return -EINVAL;
2081 if (drive->id == NULL)
2082 return -ENOMSG;
2083 if (copy_to_user((char *)arg, (char *)drive->id, (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
2084 return -EFAULT;
2085 return 0;
2087 case HDIO_GET_NICE:
2088 return put_user(drive->dsc_overlap << IDE_NICE_DSC_OVERLAP |
2089 drive->atapi_overlap << IDE_NICE_ATAPI_OVERLAP |
2090 drive->nice0 << IDE_NICE_0 |
2091 drive->nice1 << IDE_NICE_1 |
2092 drive->nice2 << IDE_NICE_2,
2093 (long *) arg);
2094 case HDIO_DRIVE_CMD:
2096 byte args[4], *argbuf = args;
2097 int argsize = 4;
2098 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2099 if (NULL == (void *) arg)
2100 return ide_do_drive_cmd(drive, &rq, ide_wait);
2101 if (copy_from_user(args, (void *)arg, 4))
2102 return -EFAULT;
2103 if (args[3]) {
2104 argsize = 4 + (SECTOR_WORDS * 4 * args[3]);
2105 argbuf = kmalloc(argsize, GFP_KERNEL);
2106 if (argbuf == NULL)
2107 return -ENOMEM;
2108 memcpy(argbuf, args, 4);
2110 err = ide_wait_cmd(drive, args[0], args[1], args[2], args[3], argbuf);
2111 if (copy_to_user((void *)arg, argbuf, argsize))
2112 err = -EFAULT;
2113 if (argsize > 4)
2114 kfree(argbuf);
2115 return err;
2118 case HDIO_SCAN_HWIF:
2120 int args[3];
2121 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2122 if (copy_from_user(args, (void *)arg, 3 * sizeof(int)))
2123 return -EFAULT;
2124 if (ide_register(args[0], args[1], args[2]) == -1)
2125 return -EIO;
2126 return 0;
2128 case HDIO_SET_NICE:
2129 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
2130 if (drive->driver == NULL)
2131 return -EPERM;
2132 if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
2133 return -EPERM;
2134 drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
2135 if (drive->dsc_overlap && !DRIVER(drive)->supports_dsc_overlap) {
2136 drive->dsc_overlap = 0;
2137 return -EPERM;
2139 drive->nice1 = (arg >> IDE_NICE_1) & 1;
2140 return 0;
2142 RO_IOCTLS(inode->i_rdev, arg);
2144 default:
2145 if (drive->driver != NULL)
2146 return DRIVER(drive)->ioctl(drive, inode, file, cmd, arg);
2147 return -EPERM;
2151 static int ide_check_media_change (kdev_t i_rdev)
2153 ide_drive_t *drive;
2155 if ((drive = get_info_ptr(i_rdev)) == NULL)
2156 return -ENODEV;
2157 if (drive->driver != NULL)
2158 return DRIVER(drive)->media_change(drive);
2159 return 0;
2162 void ide_fixstring (byte *s, const int bytecount, const int byteswap)
2164 byte *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */
2166 if (byteswap) {
2167 /* convert from big-endian to host byte order */
2168 for (p = end ; p != s;) {
2169 unsigned short *pp = (unsigned short *) (p -= 2);
2170 *pp = ntohs(*pp);
2174 /* strip leading blanks */
2175 while (s != end && *s == ' ')
2176 ++s;
2178 /* compress internal blanks and strip trailing blanks */
2179 while (s != end && *s) {
2180 if (*s++ != ' ' || (s != end && *s && *s != ' '))
2181 *p++ = *(s-1);
2184 /* wipe out trailing garbage */
2185 while (p != end)
2186 *p++ = '\0';
2190 * stridx() returns the offset of c within s,
2191 * or -1 if c is '\0' or not found within s.
2193 __initfunc(static int stridx (const char *s, char c))
2195 char *i = strchr(s, c);
2196 return (i && c) ? i - s : -1;
2200 * match_parm() does parsing for ide_setup():
2202 * 1. the first char of s must be '='.
2203 * 2. if the remainder matches one of the supplied keywords,
2204 * the index (1 based) of the keyword is negated and returned.
2205 * 3. if the remainder is a series of no more than max_vals numbers
2206 * separated by commas, the numbers are saved in vals[] and a
2207 * count of how many were saved is returned. Base10 is assumed,
2208 * and base16 is allowed when prefixed with "0x".
2209 * 4. otherwise, zero is returned.
2211 __initfunc(static int match_parm (char *s, const char *keywords[], int vals[], int max_vals))
2213 static const char *decimal = "0123456789";
2214 static const char *hex = "0123456789abcdef";
2215 int i, n;
2217 if (*s++ == '=') {
2219 * Try matching against the supplied keywords,
2220 * and return -(index+1) if we match one
2222 if (keywords != NULL) {
2223 for (i = 0; *keywords != NULL; ++i) {
2224 if (!strcmp(s, *keywords++))
2225 return -(i+1);
2229 * Look for a series of no more than "max_vals"
2230 * numeric values separated by commas, in base10,
2231 * or base16 when prefixed with "0x".
2232 * Return a count of how many were found.
2234 for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
2235 vals[n] = i;
2236 while ((i = stridx(decimal, *++s)) >= 0)
2237 vals[n] = (vals[n] * 10) + i;
2238 if (*s == 'x' && !vals[n]) {
2239 while ((i = stridx(hex, *++s)) >= 0)
2240 vals[n] = (vals[n] * 0x10) + i;
2242 if (++n == max_vals)
2243 break;
2244 if (*s == ',' || *s == ';')
2245 ++s;
2247 if (!*s)
2248 return n;
2250 return 0; /* zero = nothing matched */
2254 * ide_setup() gets called VERY EARLY during initialization,
2255 * to handle kernel "command line" strings beginning with "hdx="
2256 * or "ide". Here is the complete set currently supported:
2258 * "hdx=" is recognized for all "x" from "a" to "h", such as "hdc".
2259 * "idex=" is recognized for all "x" from "0" to "3", such as "ide1".
2261 * "hdx=noprobe" : drive may be present, but do not probe for it
2262 * "hdx=none" : drive is NOT present, ignore cmos and do not probe
2263 * "hdx=nowerr" : ignore the WRERR_STAT bit on this drive
2264 * "hdx=cdrom" : drive is present, and is a cdrom drive
2265 * "hdx=cyl,head,sect" : disk drive is present, with specified geometry
2266 * "hdx=autotune" : driver will attempt to tune interface speed
2267 * to the fastest PIO mode supported,
2268 * if possible for this drive only.
2269 * Not fully supported by all chipset types,
2270 * and quite likely to cause trouble with
2271 * older/odd IDE drives.
2273 * "idebus=xx" : inform IDE driver of VESA/PCI bus speed in MHz,
2274 * where "xx" is between 20 and 66 inclusive,
2275 * used when tuning chipset PIO modes.
2276 * For PCI bus, 25 is correct for a P75 system,
2277 * 30 is correct for P90,P120,P180 systems,
2278 * and 33 is used for P100,P133,P166 systems.
2279 * If in doubt, use idebus=33 for PCI.
2280 * As for VLB, it is safest to not specify it.
2282 * "idex=noprobe" : do not attempt to access/use this interface
2283 * "idex=base" : probe for an interface at the addr specified,
2284 * where "base" is usually 0x1f0 or 0x170
2285 * and "ctl" is assumed to be "base"+0x206
2286 * "idex=base,ctl" : specify both base and ctl
2287 * "idex=base,ctl,irq" : specify base, ctl, and irq number
2288 * "idex=autotune" : driver will attempt to tune interface speed
2289 * to the fastest PIO mode supported,
2290 * for all drives on this interface.
2291 * Not fully supported by all chipset types,
2292 * and quite likely to cause trouble with
2293 * older/odd IDE drives.
2294 * "idex=noautotune" : driver will NOT attempt to tune interface speed
2295 * This is the default for most chipsets,
2296 * except the cmd640.
2297 * "idex=serialize" : do not overlap operations on idex and ide(x^1)
2298 * "idex=four" : four drives on idex and ide(x^1) share same ports
2299 * "idex=reset" : reset interface before first use
2300 * "idex=dma" : enable DMA by default on both drives if possible
2302 * The following are valid ONLY on ide0,
2303 * and the defaults for the base,ctl ports must not be altered.
2305 * "ide0=dtc2278" : probe/support DTC2278 interface
2306 * "ide0=ht6560b" : probe/support HT6560B interface
2307 * "ide0=cmd640_vlb" : *REQUIRED* for VLB cards with the CMD640 chip
2308 * (not for PCI -- automatically detected)
2309 * "ide0=qd6580" : probe/support qd6580 interface
2310 * "ide0=ali14xx" : probe/support ali14xx chipsets (ALI M1439, M1443, M1445)
2311 * "ide0=umc8672" : probe/support umc8672 chipsets
2313 __initfunc(void ide_setup (char *s))
2315 int i, vals[3];
2316 ide_hwif_t *hwif;
2317 ide_drive_t *drive;
2318 unsigned int hw, unit;
2319 const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
2320 const char max_hwif = '0' + (MAX_HWIFS - 1);
2322 printk("ide_setup: %s", s);
2323 init_ide_data ();
2326 * Look for drive options: "hdx="
2328 if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
2329 const char *hd_words[] = {"none", "noprobe", "nowerr", "cdrom",
2330 "serialize", "autotune", "noautotune",
2331 "slow", "swapdata", NULL};
2332 unit = s[2] - 'a';
2333 hw = unit / MAX_DRIVES;
2334 unit = unit % MAX_DRIVES;
2335 hwif = &ide_hwifs[hw];
2336 drive = &hwif->drives[unit];
2337 switch (match_parm(&s[3], hd_words, vals, 3)) {
2338 case -1: /* "none" */
2339 drive->nobios = 1; /* drop into "noprobe" */
2340 case -2: /* "noprobe" */
2341 drive->noprobe = 1;
2342 goto done;
2343 case -3: /* "nowerr" */
2344 drive->bad_wstat = BAD_R_STAT;
2345 hwif->noprobe = 0;
2346 goto done;
2347 case -4: /* "cdrom" */
2348 drive->present = 1;
2349 drive->media = ide_cdrom;
2350 hwif->noprobe = 0;
2351 goto done;
2352 case -5: /* "serialize" */
2353 printk(" -- USE \"ide%d=serialize\" INSTEAD", hw);
2354 goto do_serialize;
2355 case -6: /* "autotune" */
2356 drive->autotune = 1;
2357 goto done;
2358 case -7: /* "noautotune" */
2359 drive->autotune = 2;
2360 goto done;
2361 case -8: /* "slow" */
2362 drive->slow = 1;
2363 goto done;
2364 case -9: /* swapdata */
2365 drive->bswap = 1;
2366 goto done;
2367 case 3: /* cyl,head,sect */
2368 drive->media = ide_disk;
2369 drive->cyl = drive->bios_cyl = vals[0];
2370 drive->head = drive->bios_head = vals[1];
2371 drive->sect = drive->bios_sect = vals[2];
2372 drive->present = 1;
2373 drive->forced_geom = 1;
2374 hwif->noprobe = 0;
2375 goto done;
2376 default:
2377 goto bad_option;
2381 if (s[0] != 'i' || s[1] != 'd' || s[2] != 'e')
2382 goto bad_option;
2384 * Look for bus speed option: "idebus="
2386 if (s[3] == 'b' && s[4] == 'u' && s[5] == 's') {
2387 if (match_parm(&s[6], NULL, vals, 1) != 1)
2388 goto bad_option;
2389 if (vals[0] >= 20 && vals[0] <= 66)
2390 idebus_parameter = vals[0];
2391 else
2392 printk(" -- BAD BUS SPEED! Expected value from 20 to 66");
2393 goto done;
2396 * Look for interface options: "idex="
2398 if (s[3] >= '0' && s[3] <= max_hwif) {
2400 * Be VERY CAREFUL changing this: note hardcoded indexes below
2402 const char *ide_words[] = {"noprobe", "serialize", "autotune", "noautotune", "reset", "dma", "four",
2403 "qd6580", "ht6560b", "cmd640_vlb", "dtc2278", "umc8672", "ali14xx", "dc4030", NULL};
2404 hw = s[3] - '0';
2405 hwif = &ide_hwifs[hw];
2406 i = match_parm(&s[4], ide_words, vals, 3);
2409 * Cryptic check to ensure chipset not already set for hwif:
2411 if (i > 0 || i <= -7) { /* is parameter a chipset name? */
2412 if (hwif->chipset != ide_unknown)
2413 goto bad_option; /* chipset already specified */
2414 if (i <= -7 && hw != 0)
2415 goto bad_hwif; /* chipset drivers are for "ide0=" only */
2416 if (i <= -7 && ide_hwifs[hw^1].chipset != ide_unknown)
2417 goto bad_option; /* chipset for 2nd port already specified */
2418 printk("\n");
2421 switch (i) {
2422 #ifdef CONFIG_BLK_DEV_PDC4030
2423 case -14: /* "dc4030" */
2425 extern void setup_pdc4030(ide_hwif_t *);
2426 setup_pdc4030(hwif);
2427 goto done;
2429 #endif /* CONFIG_BLK_DEV_PDC4030 */
2430 #ifdef CONFIG_BLK_DEV_ALI14XX
2431 case -13: /* "ali14xx" */
2433 extern void init_ali14xx (void);
2434 init_ali14xx();
2435 goto done;
2437 #endif /* CONFIG_BLK_DEV_ALI14XX */
2438 #ifdef CONFIG_BLK_DEV_UMC8672
2439 case -12: /* "umc8672" */
2441 extern void init_umc8672 (void);
2442 init_umc8672();
2443 goto done;
2445 #endif /* CONFIG_BLK_DEV_UMC8672 */
2446 #ifdef CONFIG_BLK_DEV_DTC2278
2447 case -11: /* "dtc2278" */
2449 extern void init_dtc2278 (void);
2450 init_dtc2278();
2451 goto done;
2453 #endif /* CONFIG_BLK_DEV_DTC2278 */
2454 #ifdef CONFIG_BLK_DEV_CMD640
2455 case -10: /* "cmd640_vlb" */
2457 extern int cmd640_vlb; /* flag for cmd640.c */
2458 cmd640_vlb = 1;
2459 goto done;
2461 #endif /* CONFIG_BLK_DEV_CMD640 */
2462 #ifdef CONFIG_BLK_DEV_HT6560B
2463 case -9: /* "ht6560b" */
2465 extern void init_ht6560b (void);
2466 init_ht6560b();
2467 goto done;
2469 #endif /* CONFIG_BLK_DEV_HT6560B */
2470 #if CONFIG_BLK_DEV_QD6580
2471 case -8: /* "qd6580" */
2473 extern void init_qd6580 (void);
2474 init_qd6580();
2475 goto done;
2477 #endif /* CONFIG_BLK_DEV_QD6580 */
2478 #ifdef CONFIG_BLK_DEV_4DRIVES
2479 case -7: /* "four" drives on one set of ports */
2481 ide_hwif_t *mate = &ide_hwifs[hw^1];
2482 mate->drives[0].select.all ^= 0x20;
2483 mate->drives[1].select.all ^= 0x20;
2484 hwif->chipset = mate->chipset = ide_4drives;
2485 mate->irq = hwif->irq;
2486 memcpy(mate->io_ports, hwif->io_ports, sizeof(hwif->io_ports));
2487 goto do_serialize;
2489 #endif /* CONFIG_BLK_DEV_4DRIVES */
2490 case -6: /* dma */
2491 hwif->autodma = 1;
2492 goto done;
2493 case -5: /* "reset" */
2494 hwif->reset = 1;
2495 goto done;
2496 case -4: /* "noautotune" */
2497 hwif->drives[0].autotune = 2;
2498 hwif->drives[1].autotune = 2;
2499 goto done;
2500 case -3: /* "autotune" */
2501 hwif->drives[0].autotune = 1;
2502 hwif->drives[1].autotune = 1;
2503 goto done;
2504 case -2: /* "serialize" */
2505 do_serialize:
2506 hwif->mate = &ide_hwifs[hw^1];
2507 hwif->mate->mate = hwif;
2508 hwif->serialized = hwif->mate->serialized = 1;
2509 goto done;
2511 case -1: /* "noprobe" */
2512 hwif->noprobe = 1;
2513 goto done;
2515 case 1: /* base */
2516 vals[1] = vals[0] + 0x206; /* default ctl */
2517 case 2: /* base,ctl */
2518 vals[2] = 0; /* default irq = probe for it */
2519 case 3: /* base,ctl,irq */
2520 ide_init_hwif_ports(hwif->io_ports, (ide_ioreg_t) vals[0], &hwif->irq);
2521 hwif->io_ports[IDE_CONTROL_OFFSET] = (ide_ioreg_t) vals[1];
2522 hwif->irq = vals[2];
2523 hwif->noprobe = 0;
2524 hwif->chipset = ide_generic;
2525 goto done;
2527 case 0: goto bad_option;
2528 default:
2529 printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
2530 return;
2533 bad_option:
2534 printk(" -- BAD OPTION\n");
2535 return;
2536 bad_hwif:
2537 printk("-- NOT SUPPORTED ON ide%d", hw);
2538 done:
2539 printk("\n");
2543 * This routine is called from the partition-table code in genhd.c
2544 * to "convert" a drive to a logical geometry with fewer than 1024 cyls.
2546 * The second parameter, "xparm", determines exactly how the translation
2547 * will be handled:
2548 * 0 = convert to CHS with fewer than 1024 cyls
2549 * using the same method as Ontrack DiskManager.
2550 * 1 = same as "0", plus offset everything by 63 sectors.
2551 * -1 = similar to "0", plus redirect sector 0 to sector 1.
2552 * >1 = convert to a CHS geometry with "xparm" heads.
2554 * Returns 0 if the translation was not possible, if the device was not
2555 * an IDE disk drive, or if a geometry was "forced" on the commandline.
2556 * Returns 1 if the geometry translation was successful.
2558 int ide_xlate_1024 (kdev_t i_rdev, int xparm, const char *msg)
2560 ide_drive_t *drive;
2561 static const byte head_vals[] = {4, 8, 16, 32, 64, 128, 255, 0};
2562 const byte *heads = head_vals;
2563 unsigned long tracks;
2565 if ((drive = get_info_ptr(i_rdev)) == NULL || drive->forced_geom)
2566 return 0;
2568 if (xparm > 1 && xparm <= drive->bios_head && drive->bios_sect == 63)
2569 return 0; /* we already have a translation */
2571 printk("%s ", msg);
2573 if (xparm < 0 && (drive->bios_cyl * drive->bios_head * drive->bios_sect) < (1024 * 16 * 63))
2574 return 0; /* small disk: no translation needed */
2576 if (drive->id) {
2577 drive->cyl = drive->id->cyls;
2578 drive->head = drive->id->heads;
2579 drive->sect = drive->id->sectors;
2581 drive->bios_cyl = drive->cyl;
2582 drive->bios_head = drive->head;
2583 drive->bios_sect = drive->sect;
2584 drive->special.b.set_geometry = 1;
2586 tracks = drive->bios_cyl * drive->bios_head * drive->bios_sect / 63;
2587 drive->bios_sect = 63;
2588 if (xparm > 1) {
2589 drive->bios_head = xparm;
2590 drive->bios_cyl = tracks / drive->bios_head;
2591 } else {
2592 while (drive->bios_cyl >= 1024) {
2593 drive->bios_head = *heads;
2594 drive->bios_cyl = tracks / drive->bios_head;
2595 if (0 == *++heads)
2596 break;
2598 #if FAKE_FDISK_FOR_EZDRIVE
2599 if (xparm == -1) {
2600 drive->remap_0_to_1 = 1;
2601 printk("[remap 0->1] ");
2602 } else
2603 #endif /* FAKE_FDISK_FOR_EZDRIVE */
2604 if (xparm == 1) {
2605 drive->sect0 = 63;
2606 drive->bios_cyl = (tracks - 1) / drive->bios_head;
2607 printk("[remap +63] ");
2610 drive->part[0].nr_sects = current_capacity(drive);
2611 printk("[%d/%d/%d]", drive->bios_cyl, drive->bios_head, drive->bios_sect);
2612 return 1;
2616 * probe_for_hwifs() finds/initializes "known" IDE interfaces
2618 __initfunc(static void probe_for_hwifs (void))
2620 #ifdef CONFIG_PCI
2621 if (pci_present())
2623 #ifdef CONFIG_BLK_DEV_IDEPCI
2624 ide_scan_pcibus();
2625 #else
2626 #ifdef CONFIG_BLK_DEV_RZ1000
2628 extern void ide_probe_for_rz100x(void);
2629 ide_probe_for_rz100x();
2631 #endif /* CONFIG_BLK_DEV_RZ1000 */
2632 #endif /* CONFIG_BLK_DEV_IDEPCI */
2634 #endif /* CONFIG_PCI */
2636 #ifdef CONFIG_BLK_DEV_CMD640
2638 extern void ide_probe_for_cmd640x(void);
2639 ide_probe_for_cmd640x();
2641 #endif /* CONFIG_BLK_DEV_CMD640 */
2642 #ifdef CONFIG_BLK_DEV_PDC4030
2644 extern int init_pdc4030(void);
2645 (void) init_pdc4030();
2647 #endif /* CONFIG_BLK_DEV_PDC4030 */
2648 #ifdef CONFIG_BLK_DEV_IDE_PMAC
2650 extern void pmac_ide_probe(void);
2651 pmac_ide_probe();
2653 #endif /* CONFIG_BLK_DEV_IDE_PMAC */
2656 __initfunc(void ide_init_builtin_drivers (void))
2659 * Probe for special PCI and other "known" interface chipsets
2661 probe_for_hwifs ();
2663 #ifdef CONFIG_BLK_DEV_IDE
2664 #if defined(__mc68000__) || defined(CONFIG_APUS)
2665 if (ide_hwifs[0].io_ports[IDE_DATA_OFFSET]) {
2666 ide_get_lock(&ide_lock, NULL, NULL); /* for atari only */
2667 disable_irq(ide_hwifs[0].irq);
2669 #endif /* __mc68000__ || CONFIG_APUS */
2671 (void) ideprobe_init();
2673 #if defined(__mc68000__) || defined(CONFIG_APUS)
2674 if (ide_hwifs[0].io_ports[IDE_DATA_OFFSET]) {
2675 enable_irq(ide_hwifs[0].irq);
2676 ide_release_lock(&ide_lock); /* for atari only */
2678 #endif /* __mc68000__ || CONFIG_APUS */
2679 #endif /* CONFIG_BLK_DEV_IDE */
2681 #ifdef CONFIG_PROC_FS
2682 proc_ide_create();
2683 #endif
2686 * Attempt to match drivers for the available drives
2688 #ifdef CONFIG_BLK_DEV_IDEDISK
2689 (void) idedisk_init();
2690 #endif /* CONFIG_BLK_DEV_IDEDISK */
2691 #ifdef CONFIG_BLK_DEV_IDECD
2692 (void) ide_cdrom_init();
2693 #endif /* CONFIG_BLK_DEV_IDECD */
2694 #ifdef CONFIG_BLK_DEV_IDETAPE
2695 (void) idetape_init();
2696 #endif /* CONFIG_BLK_DEV_IDETAPE */
2697 #ifdef CONFIG_BLK_DEV_IDEFLOPPY
2698 (void) idefloppy_init();
2699 #endif /* CONFIG_BLK_DEV_IDEFLOPPY */
2700 #ifdef CONFIG_BLK_DEV_IDESCSI
2701 (void) idescsi_init();
2702 #endif /* CONFIG_BLK_DEV_IDESCSI */
2705 static int default_cleanup (ide_drive_t *drive)
2707 return ide_unregister_subdriver(drive);
2710 static void default_do_request(ide_drive_t *drive, struct request *rq, unsigned long block)
2712 ide_end_request(0, HWGROUP(drive));
2715 static void default_end_request (byte uptodate, ide_hwgroup_t *hwgroup)
2717 ide_end_request(uptodate, hwgroup);
2720 static int default_ioctl (ide_drive_t *drive, struct inode *inode, struct file *file,
2721 unsigned int cmd, unsigned long arg)
2723 return -EIO;
2726 static int default_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
2728 drive->usage--;
2729 return -EIO;
2732 static void default_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
2736 static int default_check_media_change (ide_drive_t *drive)
2738 return 1;
2741 static void default_pre_reset (ide_drive_t *drive)
2745 static unsigned long default_capacity (ide_drive_t *drive)
2747 return 0x7fffffff; /* cdrom or tape */
2750 static void default_special (ide_drive_t *drive)
2752 special_t *s = &drive->special;
2754 s->all = 0;
2755 drive->mult_req = 0;
2758 static void setup_driver_defaults (ide_drive_t *drive)
2760 ide_driver_t *d = drive->driver;
2762 if (d->cleanup == NULL) d->cleanup = default_cleanup;
2763 if (d->do_request == NULL) d->do_request = default_do_request;
2764 if (d->end_request == NULL) d->end_request = default_end_request;
2765 if (d->ioctl == NULL) d->ioctl = default_ioctl;
2766 if (d->open == NULL) d->open = default_open;
2767 if (d->release == NULL) d->release = default_release;
2768 if (d->media_change == NULL) d->media_change = default_check_media_change;
2769 if (d->pre_reset == NULL) d->pre_reset = default_pre_reset;
2770 if (d->capacity == NULL) d->capacity = default_capacity;
2771 if (d->special == NULL) d->special = default_special;
2774 ide_drive_t *ide_scan_devices (byte media, const char *name, ide_driver_t *driver, int n)
2776 unsigned int unit, index, i;
2778 for (index = 0; index < MAX_HWIFS; ++index)
2779 if (ide_hwifs[index].present) goto search;
2780 ide_init_module(IDE_PROBE_MODULE);
2781 search:
2782 for (index = 0, i = 0; index < MAX_HWIFS; ++index) {
2783 ide_hwif_t *hwif = &ide_hwifs[index];
2784 if (!hwif->present)
2785 continue;
2786 for (unit = 0; unit < MAX_DRIVES; ++unit) {
2787 ide_drive_t *drive = &hwif->drives[unit];
2788 char *req = drive->driver_req;
2789 if (*req && !strstr(name, req))
2790 continue;
2791 if (drive->present && drive->media == media && drive->driver == driver && ++i > n)
2792 return drive;
2795 return NULL;
2798 #ifdef CONFIG_PROC_FS
2799 static ide_proc_entry_t generic_subdriver_entries[] = {
2800 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
2801 { NULL, 0, NULL, NULL }
2803 #endif
2805 int ide_register_subdriver (ide_drive_t *drive, ide_driver_t *driver, int version)
2807 unsigned long flags;
2809 save_flags(flags); /* all CPUs */
2810 cli(); /* all CPUs */
2811 if (version != IDE_SUBDRIVER_VERSION || !drive->present || drive->driver != NULL || drive->busy || drive->usage) {
2812 restore_flags(flags); /* all CPUs */
2813 return 1;
2815 drive->driver = driver;
2816 setup_driver_defaults(drive);
2817 restore_flags(flags); /* all CPUs */
2818 if (drive->autotune != 2) {
2819 if (driver->supports_dma && HWIF(drive)->dmaproc != NULL)
2820 (void) (HWIF(drive)->dmaproc(ide_dma_check, drive));
2821 drive->dsc_overlap = (drive->next != drive && driver->supports_dsc_overlap);
2822 drive->nice1 = 1;
2824 drive->revalidate = 1;
2825 #ifdef CONFIG_PROC_FS
2826 ide_add_proc_entries(drive->proc, generic_subdriver_entries, drive);
2827 ide_add_proc_entries(drive->proc, driver->proc, drive);
2828 #endif
2829 return 0;
2832 int ide_unregister_subdriver (ide_drive_t *drive)
2834 unsigned long flags;
2836 save_flags(flags); /* all CPUs */
2837 cli(); /* all CPUs */
2838 if (drive->usage || drive->busy || drive->driver == NULL || DRIVER(drive)->busy) {
2839 restore_flags(flags); /* all CPUs */
2840 return 1;
2842 #ifdef CONFIG_PROC_FS
2843 ide_remove_proc_entries(drive->proc, DRIVER(drive)->proc);
2844 ide_remove_proc_entries(drive->proc, generic_subdriver_entries);
2845 #endif
2846 auto_remove_settings(drive);
2847 drive->driver = NULL;
2848 restore_flags(flags); /* all CPUs */
2849 return 0;
2852 int ide_register_module (ide_module_t *module)
2854 ide_module_t *p = ide_modules;
2856 while (p) {
2857 if (p == module)
2858 return 1;
2859 p = p->next;
2861 module->next = ide_modules;
2862 ide_modules = module;
2863 revalidate_drives();
2864 return 0;
2867 void ide_unregister_module (ide_module_t *module)
2869 ide_module_t **p;
2871 for (p = &ide_modules; (*p) && (*p) != module; p = &((*p)->next));
2872 if (*p)
2873 *p = (*p)->next;
2876 struct file_operations ide_fops[] = {{
2877 NULL, /* lseek - default */
2878 block_read, /* read - general block-dev read */
2879 block_write, /* write - general block-dev write */
2880 NULL, /* readdir - bad */
2881 NULL, /* poll */
2882 ide_ioctl, /* ioctl */
2883 NULL, /* mmap */
2884 ide_open, /* open */
2885 NULL, /* flush */
2886 ide_release, /* release */
2887 block_fsync, /* fsync */
2888 NULL, /* fasync */
2889 ide_check_media_change, /* check_media_change */
2890 ide_revalidate_disk /* revalidate */
2893 EXPORT_SYMBOL(ide_hwifs);
2894 EXPORT_SYMBOL(ide_register_module);
2895 EXPORT_SYMBOL(ide_unregister_module);
2896 EXPORT_SYMBOL(ide_spin_wait_hwgroup);
2899 * Probe module
2901 EXPORT_SYMBOL(ide_timer_expiry);
2902 EXPORT_SYMBOL(ide_intr);
2903 EXPORT_SYMBOL(ide_geninit);
2904 EXPORT_SYMBOL(ide_fops);
2905 EXPORT_SYMBOL(ide_get_queue);
2906 EXPORT_SYMBOL(do_ide0_request);
2907 EXPORT_SYMBOL(ide_add_generic_settings);
2908 #if MAX_HWIFS > 1
2909 EXPORT_SYMBOL(do_ide1_request);
2910 #endif /* MAX_HWIFS > 1 */
2911 #if MAX_HWIFS > 2
2912 EXPORT_SYMBOL(do_ide2_request);
2913 #endif /* MAX_HWIFS > 2 */
2914 #if MAX_HWIFS > 3
2915 EXPORT_SYMBOL(do_ide3_request);
2916 #endif /* MAX_HWIFS > 3 */
2917 #if MAX_HWIFS > 4
2918 EXPORT_SYMBOL(do_ide4_request);
2919 #endif /* MAX_HWIFS > 4 */
2920 #if MAX_HWIFS > 5
2921 EXPORT_SYMBOL(do_ide5_request);
2922 #endif /* MAX_HWIFS > 5 */
2925 * Driver module
2927 EXPORT_SYMBOL(ide_scan_devices);
2928 EXPORT_SYMBOL(ide_register_subdriver);
2929 EXPORT_SYMBOL(ide_unregister_subdriver);
2930 EXPORT_SYMBOL(ide_input_data);
2931 EXPORT_SYMBOL(ide_output_data);
2932 EXPORT_SYMBOL(atapi_input_bytes);
2933 EXPORT_SYMBOL(atapi_output_bytes);
2934 EXPORT_SYMBOL(ide_set_handler);
2935 EXPORT_SYMBOL(ide_dump_status);
2936 EXPORT_SYMBOL(ide_error);
2937 EXPORT_SYMBOL(ide_fixstring);
2938 EXPORT_SYMBOL(ide_wait_stat);
2939 EXPORT_SYMBOL(ide_do_reset);
2940 EXPORT_SYMBOL(ide_init_drive_cmd);
2941 EXPORT_SYMBOL(ide_do_drive_cmd);
2942 EXPORT_SYMBOL(ide_end_drive_cmd);
2943 EXPORT_SYMBOL(ide_end_request);
2944 EXPORT_SYMBOL(ide_revalidate_disk);
2945 EXPORT_SYMBOL(ide_cmd);
2946 EXPORT_SYMBOL(ide_wait_cmd);
2947 EXPORT_SYMBOL(ide_stall_queue);
2948 #ifdef CONFIG_PROC_FS
2949 EXPORT_SYMBOL(ide_add_proc_entries);
2950 EXPORT_SYMBOL(ide_remove_proc_entries);
2951 EXPORT_SYMBOL(proc_ide_read_geometry);
2952 #endif
2953 EXPORT_SYMBOL(ide_add_setting);
2954 EXPORT_SYMBOL(ide_remove_setting);
2956 EXPORT_SYMBOL(ide_register);
2957 EXPORT_SYMBOL(ide_unregister);
2960 * This is gets invoked once during initialization, to set *everything* up
2962 __initfunc(int ide_init (void))
2964 init_ide_data ();
2966 initializing = 1;
2967 ide_init_builtin_drivers();
2968 initializing = 0;
2970 return 0;
2973 #ifdef MODULE
2974 char *options = NULL;
2975 MODULE_PARM(options,"s");
2977 __initfunc(static void parse_options (char *line))
2979 char *next = line;
2981 if (line == NULL || !*line)
2982 return;
2983 while ((line = next) != NULL) {
2984 if ((next = strchr(line,' ')) != NULL)
2985 *next++ = 0;
2986 if (!strncmp(line,"ide",3) || (!strncmp(line,"hd",2) && line[2] != '='))
2987 ide_setup(line);
2991 int init_module (void)
2993 parse_options(options);
2994 return ide_init();
2997 void cleanup_module (void)
2999 int index;
3001 for (index = 0; index < MAX_HWIFS; ++index)
3002 ide_unregister(index);
3003 #ifdef CONFIG_PROC_FS
3004 proc_ide_destroy();
3005 #endif
3007 #endif /* MODULE */