More meth updates.
[linux-2.6/linux-mips.git] / drivers / ide / ide.c
blob66ade950ab924df93ba628ae7170a02ce2f4331e
1 /*
2 * linux/drivers/ide/ide.c Version 7.00beta2 Mar 05 2003
4 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
5 */
7 /*
8 * Mostly written by Mark Lord <mlord@pobox.com>
9 * and Gadi Oxman <gadio@netvision.net.il>
10 * and Andre Hedrick <andre@linux-ide.org>
12 * See linux/MAINTAINERS for address of current maintainer.
14 * This is the multiple IDE interface driver, as evolved from hd.c.
15 * It supports up to MAX_HWIFS IDE interfaces, on one or more IRQs
16 * (usually 14 & 15).
17 * There can be up to two drives per interface, as per the ATA-2 spec.
19 * Primary: ide0, port 0x1f0; major=3; hda is minor=0; hdb is minor=64
20 * Secondary: ide1, port 0x170; major=22; hdc is minor=0; hdd is minor=64
21 * Tertiary: ide2, port 0x???; major=33; hde is minor=0; hdf is minor=64
22 * Quaternary: ide3, port 0x???; major=34; hdg is minor=0; hdh is minor=64
23 * ...
25 * From hd.c:
26 * |
27 * | It traverses the request-list, using interrupts to jump between functions.
28 * | As nearly all functions can be called within interrupts, we may not sleep.
29 * | Special care is recommended. Have Fun!
30 * |
31 * | modified by Drew Eckhardt to check nr of hd's from the CMOS.
32 * |
33 * | Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
34 * | in the early extended-partition checks and added DM partitions.
35 * |
36 * | Early work on error handling by Mika Liljeberg (liljeber@cs.Helsinki.FI).
37 * |
38 * | IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
39 * | and general streamlining by Mark Lord (mlord@pobox.com).
41 * October, 1994 -- Complete line-by-line overhaul for linux 1.1.x, by:
43 * Mark Lord (mlord@pobox.com) (IDE Perf.Pkg)
44 * Delman Lee (delman@ieee.org) ("Mr. atdisk2")
45 * Scott Snyder (snyder@fnald0.fnal.gov) (ATAPI IDE cd-rom)
47 * This was a rewrite of just about everything from hd.c, though some original
48 * code is still sprinkled about. Think of it as a major evolution, with
49 * inspiration from lots of linux users, esp. hamish@zot.apana.org.au
51 * Version 1.0 ALPHA initial code, primary i/f working okay
52 * Version 1.3 BETA dual i/f on shared irq tested & working!
53 * Version 1.4 BETA added auto probing for irq(s)
54 * Version 1.5 BETA added ALPHA (untested) support for IDE cd-roms,
55 * ...
56 * Version 5.50 allow values as small as 20 for idebus=
57 * Version 5.51 force non io_32bit in drive_cmd_intr()
58 * change delay_10ms() to delay_50ms() to fix problems
59 * Version 5.52 fix incorrect invalidation of removable devices
60 * add "hdx=slow" command line option
61 * Version 5.60 start to modularize the driver; the disk and ATAPI
62 * drivers can be compiled as loadable modules.
63 * move IDE probe code to ide-probe.c
64 * move IDE disk code to ide-disk.c
65 * add support for generic IDE device subdrivers
66 * add m68k code from Geert Uytterhoeven
67 * probe all interfaces by default
68 * add ioctl to (re)probe an interface
69 * Version 6.00 use per device request queues
70 * attempt to optimize shared hwgroup performance
71 * add ioctl to manually adjust bandwidth algorithms
72 * add kerneld support for the probe module
73 * fix bug in ide_error()
74 * fix bug in the first ide_get_lock() call for Atari
75 * don't flush leftover data for ATAPI devices
76 * Version 6.01 clear hwgroup->active while the hwgroup sleeps
77 * support HDIO_GETGEO for floppies
78 * Version 6.02 fix ide_ack_intr() call
79 * check partition table on floppies
80 * Version 6.03 handle bad status bit sequencing in ide_wait_stat()
81 * Version 6.10 deleted old entries from this list of updates
82 * replaced triton.c with ide-dma.c generic PCI DMA
83 * added support for BIOS-enabled UltraDMA
84 * rename all "promise" things to "pdc4030"
85 * fix EZ-DRIVE handling on small disks
86 * Version 6.11 fix probe error in ide_scan_devices()
87 * fix ancient "jiffies" polling bugs
88 * mask all hwgroup interrupts on each irq entry
89 * Version 6.12 integrate ioctl and proc interfaces
90 * fix parsing of "idex=" command line parameter
91 * Version 6.13 add support for ide4/ide5 courtesy rjones@orchestream.com
92 * Version 6.14 fixed IRQ sharing among PCI devices
93 * Version 6.15 added SMP awareness to IDE drivers
94 * Version 6.16 fixed various bugs; even more SMP friendly
95 * Version 6.17 fix for newest EZ-Drive problem
96 * Version 6.18 default unpartitioned-disk translation now "BIOS LBA"
97 * Version 6.19 Re-design for a UNIFORM driver for all platforms,
98 * model based on suggestions from Russell King and
99 * Geert Uytterhoeven
100 * Promise DC4030VL now supported.
101 * add support for ide6/ide7
102 * delay_50ms() changed to ide_delay_50ms() and exported.
103 * Version 6.20 Added/Fixed Generic ATA-66 support and hwif detection.
104 * Added hdx=flash to allow for second flash disk
105 * detection w/o the hang loop.
106 * Added support for ide8/ide9
107 * Added idex=ata66 for the quirky chipsets that are
108 * ATA-66 compliant, but have yet to determine a method
109 * of verification of the 80c cable presence.
110 * Specifically Promise's PDC20262 chipset.
111 * Version 6.21 Fixing/Fixed SMP spinlock issue with insight from an old
112 * hat that clarified original low level driver design.
113 * Version 6.30 Added SMP support; fixed multmode issues. -ml
114 * Version 6.31 Debug Share INTR's and request queue streaming
115 * Native ATA-100 support
116 * Prep for Cascades Project
117 * Version 7.00alpha First named revision of ide rearrange
119 * Some additional driver compile-time options are in ./include/linux/ide.h
121 * To do, in likely order of completion:
122 * - modify kernel to obtain BIOS geometry for drives on 2nd/3rd/4th i/f
126 #define REVISION "Revision: 7.00alpha2"
127 #define VERSION "Id: ide.c 7.00a2 20020906"
129 #undef REALLY_SLOW_IO /* most systems can safely undef this */
131 #define _IDE_C /* Tell ide.h it's really us */
133 #include <linux/config.h>
134 #include <linux/module.h>
135 #include <linux/types.h>
136 #include <linux/string.h>
137 #include <linux/kernel.h>
138 #include <linux/timer.h>
139 #include <linux/mm.h>
140 #include <linux/interrupt.h>
141 #include <linux/major.h>
142 #include <linux/errno.h>
143 #include <linux/genhd.h>
144 #include <linux/blkpg.h>
145 #include <linux/slab.h>
146 #include <linux/init.h>
147 #include <linux/pci.h>
148 #include <linux/delay.h>
149 #include <linux/ide.h>
150 #include <linux/devfs_fs_kernel.h>
151 #include <linux/completion.h>
152 #include <linux/reboot.h>
153 #include <linux/cdrom.h>
154 #include <linux/seq_file.h>
155 #include <linux/device.h>
156 #include <linux/kmod.h>
158 #include <asm/byteorder.h>
159 #include <asm/irq.h>
160 #include <asm/uaccess.h>
161 #include <asm/io.h>
162 #include <asm/bitops.h>
164 #include "ide_modes.h"
167 /* default maximum number of failures */
168 #define IDE_DEFAULT_MAX_FAILURES 1
170 static const u8 ide_hwif_to_major[] = { IDE0_MAJOR, IDE1_MAJOR,
171 IDE2_MAJOR, IDE3_MAJOR,
172 IDE4_MAJOR, IDE5_MAJOR,
173 IDE6_MAJOR, IDE7_MAJOR,
174 IDE8_MAJOR, IDE9_MAJOR };
176 static int idebus_parameter; /* holds the "idebus=" parameter */
177 static int system_bus_speed; /* holds what we think is VESA/PCI bus speed */
178 static int initializing; /* set while initializing built-in drivers */
180 DECLARE_MUTEX(ide_cfg_sem);
181 spinlock_t ide_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
183 static int ide_scan_direction; /* THIS was formerly 2.2.x pci=reverse */
185 #ifdef CONFIG_IDEDMA_AUTO
186 int noautodma = 0;
187 #else
188 int noautodma = 1;
189 #endif
191 EXPORT_SYMBOL(noautodma);
194 * ide_modules keeps track of the available IDE chipset/probe/driver modules.
196 ide_module_t *ide_chipsets;
197 ide_module_t *ide_probe;
200 * This is declared extern in ide.h, for access by other IDE modules:
202 ide_hwif_t ide_hwifs[MAX_HWIFS]; /* master data repository */
204 EXPORT_SYMBOL(ide_hwifs);
206 ide_devices_t *idedisk;
207 ide_devices_t *idecd;
208 ide_devices_t *idefloppy;
209 ide_devices_t *idetape;
210 ide_devices_t *idescsi;
212 EXPORT_SYMBOL(idedisk);
213 EXPORT_SYMBOL(idecd);
214 EXPORT_SYMBOL(idefloppy);
215 EXPORT_SYMBOL(idetape);
216 EXPORT_SYMBOL(idescsi);
218 extern ide_driver_t idedefault_driver;
219 static void setup_driver_defaults (ide_drive_t *drive);
222 * Do not even *think* about calling this!
224 static void init_hwif_data (unsigned int index)
226 unsigned int unit;
227 hw_regs_t hw;
228 ide_hwif_t *hwif = &ide_hwifs[index];
230 /* bulk initialize hwif & drive info with zeros */
231 memset(hwif, 0, sizeof(ide_hwif_t));
232 memset(&hw, 0, sizeof(hw_regs_t));
234 /* fill in any non-zero initial values */
235 hwif->index = index;
236 ide_init_hwif_ports(&hw, ide_default_io_base(index), 0, &hwif->irq);
237 memcpy(&hwif->hw, &hw, sizeof(hw));
238 memcpy(hwif->io_ports, hw.io_ports, sizeof(hw.io_ports));
239 hwif->noprobe = !hwif->io_ports[IDE_DATA_OFFSET];
240 #ifdef CONFIG_BLK_DEV_HD
241 if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA)
242 hwif->noprobe = 1; /* may be overridden by ide_setup() */
243 #endif /* CONFIG_BLK_DEV_HD */
244 hwif->major = ide_hwif_to_major[index];
245 hwif->name[0] = 'i';
246 hwif->name[1] = 'd';
247 hwif->name[2] = 'e';
248 hwif->name[3] = '0' + index;
249 hwif->bus_state = BUSSTATE_ON;
250 hwif->reset_poll= NULL;
251 hwif->pre_reset = NULL;
253 hwif->atapi_dma = 0; /* disable all atapi dma */
254 hwif->ultra_mask = 0x80; /* disable all ultra */
255 hwif->mwdma_mask = 0x80; /* disable all mwdma */
256 hwif->swdma_mask = 0x80; /* disable all swdma */
258 default_hwif_iops(hwif);
259 default_hwif_transport(hwif);
260 for (unit = 0; unit < MAX_DRIVES; ++unit) {
261 ide_drive_t *drive = &hwif->drives[unit];
263 drive->media = ide_disk;
264 drive->select.all = (unit<<4)|0xa0;
265 drive->hwif = hwif;
266 drive->ctl = 0x08;
267 drive->ready_stat = READY_STAT;
268 drive->bad_wstat = BAD_W_STAT;
269 drive->special.b.recalibrate = 1;
270 drive->special.b.set_geometry = 1;
271 drive->name[0] = 'h';
272 drive->name[1] = 'd';
273 drive->name[2] = 'a' + (index * MAX_DRIVES) + unit;
274 drive->max_failures = IDE_DEFAULT_MAX_FAILURES;
275 drive->using_dma = 0;
276 drive->is_flash = 0;
277 drive->driver = &idedefault_driver;
278 setup_driver_defaults(drive);
279 drive->vdma = 0;
280 INIT_LIST_HEAD(&drive->list);
285 * init_ide_data() sets reasonable default values into all fields
286 * of all instances of the hwifs and drives, but only on the first call.
287 * Subsequent calls have no effect (they don't wipe out anything).
289 * This routine is normally called at driver initialization time,
290 * but may also be called MUCH earlier during kernel "command-line"
291 * parameter processing. As such, we cannot depend on any other parts
292 * of the kernel (such as memory allocation) to be functioning yet.
294 * This is too bad, as otherwise we could dynamically allocate the
295 * ide_drive_t structs as needed, rather than always consuming memory
296 * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
298 * FIXME: We should stuff the setup data into __init and copy the
299 * relevant hwifs/allocate them properly during boot.
301 #define MAGIC_COOKIE 0x12345678
302 static void __init init_ide_data (void)
304 unsigned int index;
305 static unsigned long magic_cookie = MAGIC_COOKIE;
307 if (magic_cookie != MAGIC_COOKIE)
308 return; /* already initialized */
309 magic_cookie = 0;
311 /* Initialise all interface structures */
312 for (index = 0; index < MAX_HWIFS; ++index)
313 init_hwif_data(index);
315 /* Add default hw interfaces */
316 ide_init_default_hwifs();
318 idebus_parameter = 0;
319 system_bus_speed = 0;
323 * ide_system_bus_speed() returns what we think is the system VESA/PCI
324 * bus speed (in MHz). This is used for calculating interface PIO timings.
325 * The default is 40 for known PCI systems, 50 otherwise.
326 * The "idebus=xx" parameter can be used to override this value.
327 * The actual value to be used is computed/displayed the first time through.
329 int ide_system_bus_speed (void)
331 if (!system_bus_speed) {
332 if (idebus_parameter) {
333 /* user supplied value */
334 system_bus_speed = idebus_parameter;
335 } else if (pci_find_device(PCI_ANY_ID, PCI_ANY_ID, NULL) != NULL) {
336 /* safe default value for PCI */
337 system_bus_speed = 33;
338 } else {
339 /* safe default value for VESA and PCI */
340 system_bus_speed = 50;
342 printk(KERN_INFO "ide: Assuming %dMHz system bus speed "
343 "for PIO modes%s\n", system_bus_speed,
344 idebus_parameter ? "" : "; override with idebus=xx");
346 return system_bus_speed;
350 * current_capacity() returns the capacity (in sectors) of a drive
351 * according to its current geometry/LBA settings.
353 unsigned long current_capacity (ide_drive_t *drive)
355 if (!drive->present)
356 return 0;
357 return DRIVER(drive)->capacity(drive);
360 EXPORT_SYMBOL(current_capacity);
363 * Error reporting, in human readable form (luxurious, but a memory hog).
365 u8 ide_dump_status (ide_drive_t *drive, const char *msg, u8 stat)
367 ide_hwif_t *hwif = HWIF(drive);
368 unsigned long flags;
369 u8 err = 0;
371 local_irq_set(flags);
372 printk(KERN_WARNING "%s: %s: status=0x%02x", drive->name, msg, stat);
373 #if FANCY_STATUS_DUMPS
374 printk(" { ");
375 if (stat & BUSY_STAT) {
376 printk("Busy ");
377 } else {
378 if (stat & READY_STAT) printk("DriveReady ");
379 if (stat & WRERR_STAT) printk("DeviceFault ");
380 if (stat & SEEK_STAT) printk("SeekComplete ");
381 if (stat & DRQ_STAT) printk("DataRequest ");
382 if (stat & ECC_STAT) printk("CorrectedError ");
383 if (stat & INDEX_STAT) printk("Index ");
384 if (stat & ERR_STAT) printk("Error ");
386 printk("}");
387 #endif /* FANCY_STATUS_DUMPS */
388 printk("\n");
389 if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
390 err = hwif->INB(IDE_ERROR_REG);
391 printk("%s: %s: error=0x%02x", drive->name, msg, err);
392 #if FANCY_STATUS_DUMPS
393 if (drive->media == ide_disk) {
394 printk(" { ");
395 if (err & ABRT_ERR) printk("DriveStatusError ");
396 if (err & ICRC_ERR) printk("Bad%s ", (err & ABRT_ERR) ? "CRC" : "Sector");
397 if (err & ECC_ERR) printk("UncorrectableError ");
398 if (err & ID_ERR) printk("SectorIdNotFound ");
399 if (err & TRK0_ERR) printk("TrackZeroNotFound ");
400 if (err & MARK_ERR) printk("AddrMarkNotFound ");
401 printk("}");
402 if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR || (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
403 if ((drive->id->command_set_2 & 0x0400) &&
404 (drive->id->cfs_enable_2 & 0x0400) &&
405 (drive->addressing == 1)) {
406 u64 sectors = 0;
407 u32 high = 0;
408 u32 low = ide_read_24(drive);
409 hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
410 high = ide_read_24(drive);
412 sectors = ((u64)high << 24) | low;
413 printk(", LBAsect=%llu, high=%d, low=%d",
414 (long long) sectors,
415 high, low);
416 } else {
417 u8 cur = hwif->INB(IDE_SELECT_REG);
418 if (cur & 0x40) { /* using LBA? */
419 printk(", LBAsect=%ld", (unsigned long)
420 ((cur&0xf)<<24)
421 |(hwif->INB(IDE_HCYL_REG)<<16)
422 |(hwif->INB(IDE_LCYL_REG)<<8)
423 | hwif->INB(IDE_SECTOR_REG));
424 } else {
425 printk(", CHS=%d/%d/%d",
426 (hwif->INB(IDE_HCYL_REG)<<8) +
427 hwif->INB(IDE_LCYL_REG),
428 cur & 0xf,
429 hwif->INB(IDE_SECTOR_REG));
432 if (HWGROUP(drive) && HWGROUP(drive)->rq)
433 printk(", sector=%llu", (unsigned long long)HWGROUP(drive)->rq->sector);
436 #endif /* FANCY_STATUS_DUMPS */
437 printk("\n");
439 local_irq_restore(flags);
440 return err;
443 EXPORT_SYMBOL(ide_dump_status);
447 void ide_probe_module (void)
449 if (!ide_probe) {
450 #if defined(CONFIG_KMOD) && defined(CONFIG_BLK_DEV_IDE_MODULE)
451 (void) request_module("ide-probe-mod");
452 #endif /* (CONFIG_KMOD) && (CONFIG_BLK_DEV_IDE_MODULE) */
453 } else {
454 (void) ide_probe->init();
458 EXPORT_SYMBOL(ide_probe_module);
460 static int ide_open (struct inode * inode, struct file * filp)
462 return -ENXIO;
465 static spinlock_t drives_lock = SPIN_LOCK_UNLOCKED;
466 static spinlock_t drivers_lock = SPIN_LOCK_UNLOCKED;
467 static LIST_HEAD(drivers);
469 /* Iterator */
470 static void *m_start(struct seq_file *m, loff_t *pos)
472 struct list_head *p;
473 loff_t l = *pos;
474 spin_lock(&drivers_lock);
475 list_for_each(p, &drivers)
476 if (!l--)
477 return list_entry(p, ide_driver_t, drivers);
478 return NULL;
480 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
482 struct list_head *p = ((ide_driver_t *)v)->drivers.next;
483 (*pos)++;
484 return p==&drivers ? NULL : list_entry(p, ide_driver_t, drivers);
486 static void m_stop(struct seq_file *m, void *v)
488 spin_unlock(&drivers_lock);
490 static int show_driver(struct seq_file *m, void *v)
492 ide_driver_t *driver = v;
493 seq_printf(m, "%s version %s\n", driver->name, driver->version);
494 return 0;
496 struct seq_operations ide_drivers_op = {
497 .start = m_start,
498 .next = m_next,
499 .stop = m_stop,
500 .show = show_driver
503 #ifdef CONFIG_PROC_FS
504 ide_proc_entry_t generic_subdriver_entries[] = {
505 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
506 { NULL, 0, NULL, NULL }
508 #endif
511 #define hwif_release_region(addr, num) \
512 ((hwif->mmio) ? release_mem_region((addr),(num)) : release_region((addr),(num)))
515 * hwif_unregister - free IDE resources
517 * Note that we only release the standard ports,
518 * and do not even try to handle any extra ports
519 * allocated for weird IDE interface chipsets.
521 * Note also that we don't yet handle mmio resources here. More
522 * importantly our caller should be doing this so we need to
523 * restructure this as a helper function for drivers.
526 void hwif_unregister (ide_hwif_t *hwif)
528 u32 i = 0;
530 if (hwif->mmio == 2)
531 return;
532 if (hwif->io_ports[IDE_CONTROL_OFFSET])
533 hwif_release_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
534 #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
535 if (hwif->io_ports[IDE_IRQ_OFFSET])
536 hwif_release_region(hwif->io_ports[IDE_IRQ_OFFSET], 1);
537 #endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
539 if (hwif->straight8) {
540 hwif_release_region(hwif->io_ports[IDE_DATA_OFFSET], 8);
541 return;
543 for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++) {
544 if (hwif->io_ports[i]) {
545 hwif_release_region(hwif->io_ports[i], 1);
550 EXPORT_SYMBOL(hwif_unregister);
552 extern void init_hwif_data(unsigned int index);
555 * ide_unregister - free an ide interface
556 * @index: index of interface (will change soon to a pointer)
558 * Perform the final unregister of an IDE interface. At the moment
559 * we don't refcount interfaces so this will also get split up.
561 * Locking:
562 * The caller must not hold the IDE locks
563 * The drive present/vanishing is not yet properly locked
564 * Take care with the callbacks. These have been split to avoid
565 * deadlocking the IDE layer. The shutdown callback is called
566 * before we take the lock and free resources. It is up to the
567 * caller to be sure there is no pending I/O here, and that
568 * the interfce will not be reopened (present/vanishing locking
569 * isnt yet done btw). After we commit to the final kill we
570 * call the cleanup callback with the ide locks held.
572 * Unregister restores the hwif structures to the default state.
573 * This is raving bonkers.
576 void ide_unregister (unsigned int index)
578 ide_drive_t *drive;
579 ide_hwif_t *hwif, *g;
580 ide_hwgroup_t *hwgroup;
581 int irq_count = 0, unit, i;
582 ide_hwif_t old_hwif;
584 if (index >= MAX_HWIFS)
585 BUG();
587 BUG_ON(in_interrupt());
588 BUG_ON(irqs_disabled());
589 down(&ide_cfg_sem);
590 spin_lock_irq(&ide_lock);
591 hwif = &ide_hwifs[index];
592 if (!hwif->present)
593 goto abort;
594 for (unit = 0; unit < MAX_DRIVES; ++unit) {
595 drive = &hwif->drives[unit];
596 if (!drive->present)
597 continue;
598 if (drive->usage)
599 goto abort;
600 if (DRIVER(drive)->shutdown(drive))
601 goto abort;
603 hwif->present = 0;
605 spin_unlock_irq(&ide_lock);
607 for (unit = 0; unit < MAX_DRIVES; ++unit) {
608 drive = &hwif->drives[unit];
609 if (!drive->present)
610 continue;
611 DRIVER(drive)->cleanup(drive);
614 #ifdef CONFIG_PROC_FS
615 destroy_proc_ide_drives(hwif);
616 #endif
617 hwgroup = hwif->hwgroup;
620 * free the irq if we were the only hwif using it
622 g = hwgroup->hwif;
623 do {
624 if (g->irq == hwif->irq)
625 ++irq_count;
626 g = g->next;
627 } while (g != hwgroup->hwif);
628 if (irq_count == 1)
629 free_irq(hwif->irq, hwgroup);
631 spin_lock_irq(&ide_lock);
633 * Note that we only release the standard ports,
634 * and do not even try to handle any extra ports
635 * allocated for weird IDE interface chipsets.
637 hwif_unregister(hwif);
640 * Remove us from the hwgroup, and free
641 * the hwgroup if we were the only member
643 for (i = 0; i < MAX_DRIVES; ++i) {
644 drive = &hwif->drives[i];
645 if (drive->devfs_name[0] != '\0') {
646 devfs_remove(drive->devfs_name);
647 drive->devfs_name[0] = '\0';
649 if (!drive->present)
650 continue;
651 if (drive == drive->next) {
652 /* special case: last drive from hwgroup. */
653 BUG_ON(hwgroup->drive != drive);
654 hwgroup->drive = NULL;
655 } else {
656 ide_drive_t *walk;
658 walk = hwgroup->drive;
659 while (walk->next != drive)
660 walk = walk->next;
661 walk->next = drive->next;
662 if (hwgroup->drive == drive) {
663 hwgroup->drive = drive->next;
664 hwgroup->hwif = HWIF(hwgroup->drive);
667 BUG_ON(hwgroup->drive == drive);
668 if (drive->id != NULL) {
669 kfree(drive->id);
670 drive->id = NULL;
672 drive->present = 0;
673 blk_cleanup_queue(&drive->queue);
675 if (hwif->next == hwif) {
676 kfree(hwgroup);
677 BUG_ON(hwgroup->hwif != hwif);
678 } else {
679 /* There is another interface in hwgroup.
680 * Unlink us, and set hwgroup->drive and ->hwif to
681 * something sane.
683 g = hwgroup->hwif;
684 while (g->next != hwif)
685 g = g->next;
686 g->next = hwif->next;
687 if (hwgroup->hwif == hwif) {
688 /* Chose a random hwif for hwgroup->hwif.
689 * It's guaranteed that there are no drives
690 * left in the hwgroup.
692 BUG_ON(hwgroup->drive != NULL);
693 hwgroup->hwif = g;
695 BUG_ON(hwgroup->hwif == hwif);
698 #if !defined(CONFIG_DMA_NONPCI)
699 if (hwif->dma_base) {
700 (void) ide_release_dma(hwif);
702 hwif->dma_base = 0;
703 hwif->dma_master = 0;
704 hwif->dma_command = 0;
705 hwif->dma_vendor1 = 0;
706 hwif->dma_status = 0;
707 hwif->dma_vendor3 = 0;
708 hwif->dma_prdtable = 0;
710 #endif /* !(CONFIG_DMA_NONPCI) */
713 * Remove us from the kernel's knowledge
715 blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
716 for (i = 0; i < MAX_DRIVES; i++) {
717 struct gendisk *disk = hwif->drives[i].disk;
718 hwif->drives[i].disk = NULL;
719 put_disk(disk);
721 unregister_blkdev(hwif->major, hwif->name);
722 old_hwif = *hwif;
723 init_hwif_data(index); /* restore hwif data to pristine status */
724 hwif->hwgroup = old_hwif.hwgroup;
726 hwif->proc = old_hwif.proc;
728 hwif->major = old_hwif.major;
729 // hwif->index = old_hwif.index;
730 // hwif->channel = old_hwif.channel;
731 hwif->straight8 = old_hwif.straight8;
732 hwif->bus_state = old_hwif.bus_state;
734 hwif->atapi_dma = old_hwif.atapi_dma;
735 hwif->ultra_mask = old_hwif.ultra_mask;
736 hwif->mwdma_mask = old_hwif.mwdma_mask;
737 hwif->swdma_mask = old_hwif.swdma_mask;
739 hwif->chipset = old_hwif.chipset;
741 #ifdef CONFIG_BLK_DEV_IDEPCI
742 hwif->pci_dev = old_hwif.pci_dev;
743 hwif->cds = old_hwif.cds;
744 #endif /* CONFIG_BLK_DEV_IDEPCI */
746 #if 0
747 hwif->hwifops = old_hwif.hwifops;
748 #else
749 hwif->identify = old_hwif.identify;
750 hwif->tuneproc = old_hwif.tuneproc;
751 hwif->speedproc = old_hwif.speedproc;
752 hwif->selectproc = old_hwif.selectproc;
753 hwif->reset_poll = old_hwif.reset_poll;
754 hwif->pre_reset = old_hwif.pre_reset;
755 hwif->resetproc = old_hwif.resetproc;
756 hwif->intrproc = old_hwif.intrproc;
757 hwif->maskproc = old_hwif.maskproc;
758 hwif->quirkproc = old_hwif.quirkproc;
759 hwif->busproc = old_hwif.busproc;
760 #endif
762 #if 0
763 hwif->pioops = old_hwif.pioops;
764 #else
765 hwif->ata_input_data = old_hwif.ata_input_data;
766 hwif->ata_output_data = old_hwif.ata_output_data;
767 hwif->atapi_input_bytes = old_hwif.atapi_input_bytes;
768 hwif->atapi_output_bytes = old_hwif.atapi_output_bytes;
769 #endif
771 #if 0
772 hwif->dmaops = old_hwif.dmaops;
773 #else
774 hwif->ide_dma_read = old_hwif.ide_dma_read;
775 hwif->ide_dma_write = old_hwif.ide_dma_write;
776 hwif->ide_dma_begin = old_hwif.ide_dma_begin;
777 hwif->ide_dma_end = old_hwif.ide_dma_end;
778 hwif->ide_dma_check = old_hwif.ide_dma_check;
779 hwif->ide_dma_on = old_hwif.ide_dma_on;
780 hwif->ide_dma_off = old_hwif.ide_dma_off;
781 hwif->ide_dma_off_quietly = old_hwif.ide_dma_off_quietly;
782 hwif->ide_dma_test_irq = old_hwif.ide_dma_test_irq;
783 hwif->ide_dma_host_on = old_hwif.ide_dma_host_on;
784 hwif->ide_dma_host_off = old_hwif.ide_dma_host_off;
785 hwif->ide_dma_bad_drive = old_hwif.ide_dma_bad_drive;
786 hwif->ide_dma_good_drive = old_hwif.ide_dma_good_drive;
787 hwif->ide_dma_count = old_hwif.ide_dma_count;
788 hwif->ide_dma_verbose = old_hwif.ide_dma_verbose;
789 hwif->ide_dma_retune = old_hwif.ide_dma_retune;
790 hwif->ide_dma_lostirq = old_hwif.ide_dma_lostirq;
791 hwif->ide_dma_timeout = old_hwif.ide_dma_timeout;
792 #endif
794 #if 0
795 hwif->iops = old_hwif.iops;
796 #else
797 hwif->OUTB = old_hwif.OUTB;
798 hwif->OUTBSYNC = old_hwif.OUTBSYNC;
799 hwif->OUTW = old_hwif.OUTW;
800 hwif->OUTL = old_hwif.OUTL;
801 hwif->OUTSW = old_hwif.OUTSW;
802 hwif->OUTSL = old_hwif.OUTSL;
804 hwif->INB = old_hwif.INB;
805 hwif->INW = old_hwif.INW;
806 hwif->INL = old_hwif.INL;
807 hwif->INSW = old_hwif.INSW;
808 hwif->INSL = old_hwif.INSL;
809 #endif
811 hwif->mmio = old_hwif.mmio;
812 hwif->rqsize = old_hwif.rqsize;
813 hwif->addressing = old_hwif.addressing;
814 #ifndef CONFIG_BLK_DEV_IDECS
815 hwif->irq = old_hwif.irq;
816 #endif /* CONFIG_BLK_DEV_IDECS */
817 hwif->initializing = old_hwif.initializing;
819 hwif->dma_base = old_hwif.dma_base;
820 hwif->dma_master = old_hwif.dma_master;
821 hwif->dma_command = old_hwif.dma_command;
822 hwif->dma_vendor1 = old_hwif.dma_vendor1;
823 hwif->dma_status = old_hwif.dma_status;
824 hwif->dma_vendor3 = old_hwif.dma_vendor3;
825 hwif->dma_prdtable = old_hwif.dma_prdtable;
827 hwif->dma_extra = old_hwif.dma_extra;
828 hwif->config_data = old_hwif.config_data;
829 hwif->select_data = old_hwif.select_data;
830 hwif->autodma = old_hwif.autodma;
831 hwif->udma_four = old_hwif.udma_four;
832 hwif->no_dsc = old_hwif.no_dsc;
834 hwif->hwif_data = old_hwif.hwif_data;
835 abort:
836 spin_unlock_irq(&ide_lock);
837 up(&ide_cfg_sem);
840 EXPORT_SYMBOL(ide_unregister);
844 * ide_setup_ports - set up IDE interface ports
845 * @hw: register descriptions
846 * @base: base register
847 * @offsets: table of register offsets
848 * @ctrl: control register
849 * @ack_irq: IRQ ack
850 * @irq: interrupt lie
852 * Setup hw_regs_t structure described by parameters. You
853 * may set up the hw structure yourself OR use this routine to
854 * do it for you. This is basically a helper
858 void ide_setup_ports ( hw_regs_t *hw,
859 unsigned long base, int *offsets,
860 unsigned long ctrl, unsigned long intr,
861 ide_ack_intr_t *ack_intr,
863 * ide_io_ops_t *iops,
865 int irq)
867 int i;
869 for (i = 0; i < IDE_NR_PORTS; i++) {
870 if (offsets[i] == -1) {
871 switch(i) {
872 case IDE_CONTROL_OFFSET:
873 hw->io_ports[i] = ctrl;
874 break;
875 #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
876 case IDE_IRQ_OFFSET:
877 hw->io_ports[i] = intr;
878 break;
879 #endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
880 default:
881 hw->io_ports[i] = 0;
882 break;
884 } else {
885 hw->io_ports[i] = base + offsets[i];
888 hw->irq = irq;
889 hw->dma = NO_DMA;
890 hw->ack_intr = ack_intr;
892 * hw->iops = iops;
896 EXPORT_SYMBOL(ide_setup_ports);
899 * Register an IDE interface, specifying exactly the registers etc
900 * Set init=1 iff calling before probes have taken place.
902 int ide_register_hw (hw_regs_t *hw, ide_hwif_t **hwifp)
904 int index, retry = 1;
905 ide_hwif_t *hwif;
907 do {
908 for (index = 0; index < MAX_HWIFS; ++index) {
909 hwif = &ide_hwifs[index];
910 if (hwif->hw.io_ports[IDE_DATA_OFFSET] == hw->io_ports[IDE_DATA_OFFSET])
911 goto found;
913 for (index = 0; index < MAX_HWIFS; ++index) {
914 hwif = &ide_hwifs[index];
915 if (hwif->hold)
916 continue;
917 if ((!hwif->present && !hwif->mate && !initializing) ||
918 (!hwif->hw.io_ports[IDE_DATA_OFFSET] && initializing))
919 goto found;
921 for (index = 0; index < MAX_HWIFS; index++)
922 ide_unregister(index);
923 } while (retry--);
924 return -1;
925 found:
926 if (hwif->present)
927 ide_unregister(index);
928 else if (!hwif->hold)
929 init_hwif_data(index);
930 if (hwif->present)
931 return -1;
932 memcpy(&hwif->hw, hw, sizeof(*hw));
933 memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->hw.io_ports));
934 hwif->irq = hw->irq;
935 hwif->noprobe = 0;
936 hwif->chipset = hw->chipset;
938 if (!initializing) {
939 ide_probe_module();
940 #ifdef CONFIG_PROC_FS
941 create_proc_ide_interfaces();
942 #endif
945 if (hwifp)
946 *hwifp = hwif;
948 return (initializing || hwif->present) ? index : -1;
951 EXPORT_SYMBOL(ide_register_hw);
954 * Compatibility function with existing drivers. If you want
955 * something different, use the function above.
957 int ide_register (int arg1, int arg2, int irq)
959 hw_regs_t hw;
960 ide_init_hwif_ports(&hw, (unsigned long) arg1, (unsigned long) arg2, NULL);
961 hw.irq = irq;
962 return ide_register_hw(&hw, NULL);
965 EXPORT_SYMBOL(ide_register);
969 * Locks for IDE setting functionality
972 DECLARE_MUTEX(ide_setting_sem);
973 EXPORT_SYMBOL(ide_setting_sem);
976 * ide_add_setting - add an ide setting option
977 * @drive: drive to use
978 * @name: setting name
979 * @rw: true if the function is read write
980 * @read_ioctl: function to call on read
981 * @write_ioctl: function to call on write
982 * @data_type: type of data
983 * @min: range minimum
984 * @max: range maximum
985 * @mul_factor: multiplication scale
986 * @div_factor: divison scale
987 * @data: private data field
988 * @set: setting
990 * Removes the setting named from the device if it is present.
991 * The function takes the settings_lock to protect against
992 * parallel changes. This function must not be called from IRQ
993 * context. Returns 0 on success or -1 on failure.
995 * BUGS: This code is seriously over-engineered. There is also
996 * magic about how the driver specific features are setup. If
997 * a driver is attached we assume the driver settings are auto
998 * remove.
1001 int 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)
1003 ide_settings_t **p = (ide_settings_t **) &drive->settings, *setting = NULL;
1005 down(&ide_setting_sem);
1006 while ((*p) && strcmp((*p)->name, name) < 0)
1007 p = &((*p)->next);
1008 if ((setting = kmalloc(sizeof(*setting), GFP_KERNEL)) == NULL)
1009 goto abort;
1010 memset(setting, 0, sizeof(*setting));
1011 if ((setting->name = kmalloc(strlen(name) + 1, GFP_KERNEL)) == NULL)
1012 goto abort;
1013 strcpy(setting->name, name);
1014 setting->rw = rw;
1015 setting->read_ioctl = read_ioctl;
1016 setting->write_ioctl = write_ioctl;
1017 setting->data_type = data_type;
1018 setting->min = min;
1019 setting->max = max;
1020 setting->mul_factor = mul_factor;
1021 setting->div_factor = div_factor;
1022 setting->data = data;
1023 setting->set = set;
1025 setting->next = *p;
1026 if (drive->driver != &idedefault_driver)
1027 setting->auto_remove = 1;
1028 *p = setting;
1029 up(&ide_setting_sem);
1030 return 0;
1031 abort:
1032 up(&ide_setting_sem);
1033 if (setting)
1034 kfree(setting);
1035 return -1;
1038 EXPORT_SYMBOL(ide_add_setting);
1041 * __ide_remove_setting - remove an ide setting option
1042 * @drive: drive to use
1043 * @name: setting name
1045 * Removes the setting named from the device if it is present.
1046 * The caller must hold the setting semaphore.
1049 static void __ide_remove_setting (ide_drive_t *drive, char *name)
1051 ide_settings_t **p, *setting;
1053 p = (ide_settings_t **) &drive->settings;
1055 while ((*p) && strcmp((*p)->name, name))
1056 p = &((*p)->next);
1057 if ((setting = (*p)) == NULL)
1058 return;
1060 (*p) = setting->next;
1062 kfree(setting->name);
1063 kfree(setting);
1067 * ide_remove_setting - remove an ide setting option
1068 * @drive: drive to use
1069 * @name: setting name
1071 * Removes the setting named from the device if it is present.
1072 * The function takes the settings_lock to protect against
1073 * parallel changes. This function must not be called from IRQ
1074 * context.
1077 void ide_remove_setting (ide_drive_t *drive, char *name)
1079 down(&ide_setting_sem);
1080 __ide_remove_setting(drive, name);
1081 up(&ide_setting_sem);
1084 EXPORT_SYMBOL(ide_remove_setting);
1087 * ide_find_setting_by_ioctl - find a drive specific ioctl
1088 * @drive: drive to scan
1089 * @cmd: ioctl command to handle
1091 * Scan's the device setting table for a matching entry and returns
1092 * this or NULL if no entry is found. The caller must hold the
1093 * setting semaphore
1096 static ide_settings_t *ide_find_setting_by_ioctl (ide_drive_t *drive, int cmd)
1098 ide_settings_t *setting = drive->settings;
1100 while (setting) {
1101 if (setting->read_ioctl == cmd || setting->write_ioctl == cmd)
1102 break;
1103 setting = setting->next;
1106 return setting;
1110 * ide_find_setting_by_name - find a drive specific setting
1111 * @drive: drive to scan
1112 * @name: setting name
1114 * Scan's the device setting table for a matching entry and returns
1115 * this or NULL if no entry is found. The caller must hold the
1116 * setting semaphore
1119 ide_settings_t *ide_find_setting_by_name (ide_drive_t *drive, char *name)
1121 ide_settings_t *setting = drive->settings;
1123 while (setting) {
1124 if (strcmp(setting->name, name) == 0)
1125 break;
1126 setting = setting->next;
1128 return setting;
1132 * auto_remove_settings - remove driver specific settings
1133 * @drive: drive
1135 * Automatically remove all the driver specific settings for this
1136 * drive. This function may sleep and must not be called from IRQ
1137 * context. Takes the settings_lock
1140 static void auto_remove_settings (ide_drive_t *drive)
1142 ide_settings_t *setting;
1143 down(&ide_setting_sem);
1144 repeat:
1145 setting = drive->settings;
1146 while (setting) {
1147 if (setting->auto_remove) {
1148 __ide_remove_setting(drive, setting->name);
1149 goto repeat;
1151 setting = setting->next;
1153 up(&ide_setting_sem);
1157 * ide_read_setting - read an IDE setting
1158 * @drive: drive to read from
1159 * @setting: drive setting
1161 * Read a drive setting and return the value. The caller
1162 * must hold the ide_setting_sem when making this call.
1164 * BUGS: the data return and error are the same return value
1165 * so an error -EINVAL and true return of the same value cannot
1166 * be told apart
1169 int ide_read_setting (ide_drive_t *drive, ide_settings_t *setting)
1171 int val = -EINVAL;
1172 unsigned long flags;
1174 if ((setting->rw & SETTING_READ)) {
1175 spin_lock_irqsave(&ide_lock, flags);
1176 switch(setting->data_type) {
1177 case TYPE_BYTE:
1178 val = *((u8 *) setting->data);
1179 break;
1180 case TYPE_SHORT:
1181 val = *((u16 *) setting->data);
1182 break;
1183 case TYPE_INT:
1184 case TYPE_INTA:
1185 val = *((u32 *) setting->data);
1186 break;
1188 spin_unlock_irqrestore(&ide_lock, flags);
1190 return val;
1193 int ide_spin_wait_hwgroup (ide_drive_t *drive)
1195 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1196 unsigned long timeout = jiffies + (3 * HZ);
1198 spin_lock_irq(&ide_lock);
1200 while (hwgroup->busy) {
1201 unsigned long lflags;
1202 spin_unlock_irq(&ide_lock);
1203 local_irq_set(lflags);
1204 if (time_after(jiffies, timeout)) {
1205 local_irq_restore(lflags);
1206 printk(KERN_ERR "%s: channel busy\n", drive->name);
1207 return -EBUSY;
1209 local_irq_restore(lflags);
1210 spin_lock_irq(&ide_lock);
1212 return 0;
1215 EXPORT_SYMBOL(ide_spin_wait_hwgroup);
1218 * ide_write_setting - read an IDE setting
1219 * @drive: drive to read from
1220 * @setting: drive setting
1221 * @val: value
1223 * Write a drive setting if it is possible. The caller
1224 * must hold the ide_setting_sem when making this call.
1226 * BUGS: the data return and error are the same return value
1227 * so an error -EINVAL and true return of the same value cannot
1228 * be told apart
1230 * FIXME: This should be changed to enqueue a special request
1231 * to the driver to change settings, and then wait on a sema for completion.
1232 * The current scheme of polling is kludgy, though safe enough.
1234 int ide_write_setting (ide_drive_t *drive, ide_settings_t *setting, int val)
1236 int i;
1237 u32 *p;
1239 if (!capable(CAP_SYS_ADMIN))
1240 return -EACCES;
1241 if (!(setting->rw & SETTING_WRITE))
1242 return -EPERM;
1243 if (val < setting->min || val > setting->max)
1244 return -EINVAL;
1245 if (setting->set)
1246 return setting->set(drive, val);
1247 if (ide_spin_wait_hwgroup(drive))
1248 return -EBUSY;
1249 switch (setting->data_type) {
1250 case TYPE_BYTE:
1251 *((u8 *) setting->data) = val;
1252 break;
1253 case TYPE_SHORT:
1254 *((u16 *) setting->data) = val;
1255 break;
1256 case TYPE_INT:
1257 *((u32 *) setting->data) = val;
1258 break;
1259 case TYPE_INTA:
1260 p = (u32 *) setting->data;
1261 for (i = 0; i < 1 << PARTN_BITS; i++, p++)
1262 *p = val;
1263 break;
1265 spin_unlock_irq(&ide_lock);
1266 return 0;
1269 EXPORT_SYMBOL(ide_write_setting);
1271 static int set_io_32bit(ide_drive_t *drive, int arg)
1273 drive->io_32bit = arg;
1274 #ifdef CONFIG_BLK_DEV_DTC2278
1275 if (HWIF(drive)->chipset == ide_dtc2278)
1276 HWIF(drive)->drives[!drive->select.b.unit].io_32bit = arg;
1277 #endif /* CONFIG_BLK_DEV_DTC2278 */
1278 return 0;
1281 static int set_using_dma (ide_drive_t *drive, int arg)
1283 if (!DRIVER(drive)->supports_dma)
1284 return -EPERM;
1285 if (!drive->id || !(drive->id->capability & 1))
1286 return -EPERM;
1287 if (HWIF(drive)->ide_dma_check == NULL)
1288 return -EPERM;
1289 if (arg) {
1290 if (HWIF(drive)->ide_dma_check(drive)) return -EIO;
1291 if (HWIF(drive)->ide_dma_on(drive)) return -EIO;
1292 } else {
1293 if (HWIF(drive)->ide_dma_off(drive)) return -EIO;
1295 return 0;
1298 static int set_pio_mode (ide_drive_t *drive, int arg)
1300 struct request rq;
1302 if (!HWIF(drive)->tuneproc)
1303 return -ENOSYS;
1304 if (drive->special.b.set_tune)
1305 return -EBUSY;
1306 ide_init_drive_cmd(&rq);
1307 drive->tune_req = (u8) arg;
1308 drive->special.b.set_tune = 1;
1309 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
1310 return 0;
1313 static int set_xfer_rate (ide_drive_t *drive, int arg)
1315 int err = ide_wait_cmd(drive,
1316 WIN_SETFEATURES, (u8) arg,
1317 SETFEATURES_XFER, 0, NULL);
1319 if (!err && arg) {
1320 ide_set_xfer_rate(drive, (u8) arg);
1321 ide_driveid_update(drive);
1323 return err;
1326 int ide_atapi_to_scsi (ide_drive_t *drive, int arg)
1328 if (drive->media == ide_disk) {
1329 drive->scsi = 0;
1330 return 0;
1333 if (DRIVER(drive)->cleanup(drive)) {
1334 drive->scsi = 0;
1335 return 0;
1338 drive->scsi = (u8) arg;
1339 ata_attach(drive);
1340 return 0;
1343 void ide_add_generic_settings (ide_drive_t *drive)
1346 * drive setting name read/write access read ioctl write ioctl data type min max mul_factor div_factor data pointer set function
1348 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);
1349 ide_add_setting(drive, "keepsettings", SETTING_RW, HDIO_GET_KEEPSETTINGS, HDIO_SET_KEEPSETTINGS, TYPE_BYTE, 0, 1, 1, 1, &drive->keep_settings, NULL);
1350 ide_add_setting(drive, "nice1", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->nice1, NULL);
1351 ide_add_setting(drive, "pio_mode", SETTING_WRITE, -1, HDIO_SET_PIO_MODE, TYPE_BYTE, 0, 255, 1, 1, NULL, set_pio_mode);
1352 ide_add_setting(drive, "slow", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->slow, NULL);
1353 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);
1354 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);
1355 ide_add_setting(drive, "init_speed", SETTING_RW, -1, -1, TYPE_BYTE, 0, 70, 1, 1, &drive->init_speed, NULL);
1356 ide_add_setting(drive, "current_speed", SETTING_RW, -1, -1, TYPE_BYTE, 0, 70, 1, 1, &drive->current_speed, set_xfer_rate);
1357 ide_add_setting(drive, "number", SETTING_RW, -1, -1, TYPE_BYTE, 0, 3, 1, 1, &drive->dn, NULL);
1358 if (drive->media != ide_disk)
1359 ide_add_setting(drive, "ide-scsi", SETTING_RW, -1, HDIO_SET_IDE_SCSI, TYPE_BYTE, 0, 1, 1, 1, &drive->scsi, ide_atapi_to_scsi);
1363 * Delay for *at least* 50ms. As we don't know how much time is left
1364 * until the next tick occurs, we wait an extra tick to be safe.
1365 * This is used only during the probing/polling for drives at boot time.
1367 * However, its usefullness may be needed in other places, thus we export it now.
1368 * The future may change this to a millisecond setable delay.
1370 void ide_delay_50ms (void)
1372 #ifndef CONFIG_BLK_DEV_IDECS
1373 mdelay(50);
1374 #else
1375 __set_current_state(TASK_UNINTERRUPTIBLE);
1376 schedule_timeout(1+HZ/20);
1377 #endif /* CONFIG_BLK_DEV_IDECS */
1380 EXPORT_SYMBOL(ide_delay_50ms);
1382 int system_bus_clock (void)
1384 return((int) ((!system_bus_speed) ? ide_system_bus_speed() : system_bus_speed ));
1387 EXPORT_SYMBOL(system_bus_clock);
1390 * Locking is badly broken here - since way back. That sucker is
1391 * root-only, but that's not an excuse... The real question is what
1392 * exclusion rules do we want here.
1394 int ide_replace_subdriver (ide_drive_t *drive, const char *driver)
1396 if (!drive->present || drive->usage || drive->dead)
1397 goto abort;
1398 if (DRIVER(drive)->cleanup(drive))
1399 goto abort;
1400 strlcpy(drive->driver_req, driver, sizeof(drive->driver_req));
1401 if (ata_attach(drive)) {
1402 spin_lock(&drives_lock);
1403 list_del_init(&drive->list);
1404 spin_unlock(&drives_lock);
1405 drive->driver_req[0] = 0;
1406 ata_attach(drive);
1407 } else {
1408 drive->driver_req[0] = 0;
1410 if (DRIVER(drive)!= &idedefault_driver && !strcmp(DRIVER(drive)->name, driver))
1411 return 0;
1412 abort:
1413 return 1;
1416 EXPORT_SYMBOL(ide_replace_subdriver);
1418 int ata_attach(ide_drive_t *drive)
1420 struct list_head *p;
1421 spin_lock(&drivers_lock);
1422 list_for_each(p, &drivers) {
1423 ide_driver_t *driver = list_entry(p, ide_driver_t, drivers);
1424 if (!try_module_get(driver->owner))
1425 continue;
1426 spin_unlock(&drivers_lock);
1427 if (driver->attach(drive) == 0) {
1428 module_put(driver->owner);
1429 drive->gendev.driver = &driver->gen_driver;
1430 return 0;
1432 spin_lock(&drivers_lock);
1433 module_put(driver->owner);
1435 drive->gendev.driver = &idedefault_driver.gen_driver;
1436 spin_unlock(&drivers_lock);
1437 if(idedefault_driver.attach(drive) != 0)
1438 panic("ide: default attach failed");
1439 return 1;
1442 EXPORT_SYMBOL(ata_attach);
1444 int generic_ide_suspend(struct device *dev, u32 state, u32 level)
1446 ide_drive_t *drive = dev->driver_data;
1447 struct request rq;
1448 struct request_pm_state rqpm;
1449 ide_task_t args;
1451 if (level == dev->power_state || level != SUSPEND_SAVE_STATE)
1452 return 0;
1454 memset(&rq, 0, sizeof(rq));
1455 memset(&rqpm, 0, sizeof(rqpm));
1456 memset(&args, 0, sizeof(args));
1457 rq.flags = REQ_PM_SUSPEND;
1458 rq.special = &args;
1459 rq.pm = &rqpm;
1460 rqpm.pm_step = ide_pm_state_start_suspend;
1461 rqpm.pm_state = state;
1463 return ide_do_drive_cmd(drive, &rq, ide_wait);
1466 EXPORT_SYMBOL(generic_ide_suspend);
1468 int generic_ide_resume(struct device *dev, u32 level)
1470 ide_drive_t *drive = dev->driver_data;
1471 struct request rq;
1472 struct request_pm_state rqpm;
1473 ide_task_t args;
1475 if (level == dev->power_state || level != RESUME_RESTORE_STATE)
1476 return 0;
1478 memset(&rq, 0, sizeof(rq));
1479 memset(&rqpm, 0, sizeof(rqpm));
1480 memset(&args, 0, sizeof(args));
1481 rq.flags = REQ_PM_RESUME;
1482 rq.special = &args;
1483 rq.pm = &rqpm;
1484 rqpm.pm_step = ide_pm_state_start_resume;
1485 rqpm.pm_state = 0;
1487 return ide_do_drive_cmd(drive, &rq, ide_head_wait);
1490 EXPORT_SYMBOL(generic_ide_resume);
1492 int generic_ide_ioctl(struct block_device *bdev, unsigned int cmd,
1493 unsigned long arg)
1495 ide_drive_t *drive = bdev->bd_disk->private_data;
1496 ide_settings_t *setting;
1497 int err = 0;
1499 down(&ide_setting_sem);
1500 if ((setting = ide_find_setting_by_ioctl(drive, cmd)) != NULL) {
1501 if (cmd == setting->read_ioctl) {
1502 err = ide_read_setting(drive, setting);
1503 up(&ide_setting_sem);
1504 return err >= 0 ? put_user(err, (long *) arg) : err;
1505 } else {
1506 if (bdev != bdev->bd_contains)
1507 err = -EINVAL;
1508 else
1509 err = ide_write_setting(drive, setting, arg);
1510 up(&ide_setting_sem);
1511 return err;
1514 up(&ide_setting_sem);
1516 switch (cmd) {
1517 case HDIO_GETGEO:
1519 struct hd_geometry *loc = (struct hd_geometry *) arg;
1520 u16 bios_cyl = drive->bios_cyl; /* truncate */
1521 if (!loc || (drive->media != ide_disk && drive->media != ide_floppy)) return -EINVAL;
1522 if (put_user(drive->bios_head, (u8 *) &loc->heads)) return -EFAULT;
1523 if (put_user(drive->bios_sect, (u8 *) &loc->sectors)) return -EFAULT;
1524 if (put_user(bios_cyl, (u16 *) &loc->cylinders)) return -EFAULT;
1525 if (put_user((unsigned)get_start_sect(bdev),
1526 (unsigned long *) &loc->start)) return -EFAULT;
1527 return 0;
1530 case HDIO_GETGEO_BIG_RAW:
1532 struct hd_big_geometry *loc = (struct hd_big_geometry *) arg;
1533 if (!loc || (drive->media != ide_disk && drive->media != ide_floppy)) return -EINVAL;
1534 if (put_user(drive->head, (u8 *) &loc->heads)) return -EFAULT;
1535 if (put_user(drive->sect, (u8 *) &loc->sectors)) return -EFAULT;
1536 if (put_user(drive->cyl, (unsigned int *) &loc->cylinders)) return -EFAULT;
1537 if (put_user((unsigned)get_start_sect(bdev),
1538 (unsigned long *) &loc->start)) return -EFAULT;
1539 return 0;
1542 case HDIO_OBSOLETE_IDENTITY:
1543 case HDIO_GET_IDENTITY:
1544 if (bdev != bdev->bd_contains)
1545 return -EINVAL;
1546 if (drive->id_read == 0)
1547 return -ENOMSG;
1548 if (copy_to_user((char *)arg, (char *)drive->id, (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
1549 return -EFAULT;
1550 return 0;
1552 case HDIO_GET_NICE:
1553 return put_user(drive->dsc_overlap << IDE_NICE_DSC_OVERLAP |
1554 drive->atapi_overlap << IDE_NICE_ATAPI_OVERLAP |
1555 drive->nice0 << IDE_NICE_0 |
1556 drive->nice1 << IDE_NICE_1 |
1557 drive->nice2 << IDE_NICE_2,
1558 (long *) arg);
1560 #ifdef CONFIG_IDE_TASK_IOCTL
1561 case HDIO_DRIVE_TASKFILE:
1562 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1563 return -EACCES;
1564 switch(drive->media) {
1565 case ide_disk:
1566 return ide_taskfile_ioctl(drive, cmd, arg);
1567 #ifdef CONFIG_PKT_TASK_IOCTL
1568 case ide_cdrom:
1569 case ide_tape:
1570 case ide_floppy:
1571 return pkt_taskfile_ioctl(drive, cmd, arg);
1572 #endif /* CONFIG_PKT_TASK_IOCTL */
1573 default:
1574 return -ENOMSG;
1576 #endif /* CONFIG_IDE_TASK_IOCTL */
1578 case HDIO_DRIVE_CMD:
1579 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1580 return -EACCES;
1581 return ide_cmd_ioctl(drive, cmd, arg);
1583 case HDIO_DRIVE_TASK:
1584 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1585 return -EACCES;
1586 return ide_task_ioctl(drive, cmd, arg);
1588 case HDIO_SCAN_HWIF:
1590 int args[3];
1591 if (!capable(CAP_SYS_RAWIO)) return -EACCES;
1592 if (copy_from_user(args, (void *)arg, 3 * sizeof(int)))
1593 return -EFAULT;
1594 if (ide_register(args[0], args[1], args[2]) == -1)
1595 return -EIO;
1596 return 0;
1598 case HDIO_UNREGISTER_HWIF:
1599 if (!capable(CAP_SYS_RAWIO)) return -EACCES;
1600 /* (arg > MAX_HWIFS) checked in function */
1601 ide_unregister(arg);
1602 return 0;
1603 case HDIO_SET_NICE:
1604 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
1605 if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
1606 return -EPERM;
1607 drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
1608 if (drive->dsc_overlap && !DRIVER(drive)->supports_dsc_overlap) {
1609 drive->dsc_overlap = 0;
1610 return -EPERM;
1612 drive->nice1 = (arg >> IDE_NICE_1) & 1;
1613 return 0;
1614 case HDIO_DRIVE_RESET:
1616 unsigned long flags;
1617 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
1620 * Abort the current command on the
1621 * group if there is one, taking
1622 * care not to allow anything else
1623 * to be queued and to die on the
1624 * spot if we miss one somehow
1627 spin_lock_irqsave(&ide_lock, flags);
1629 DRIVER(drive)->abort(drive, "drive reset");
1630 if(HWGROUP(drive)->handler)
1631 BUG();
1633 /* Ensure nothing gets queued after we
1634 drop the lock. Reset will clear the busy */
1636 HWGROUP(drive)->busy = 1;
1637 spin_unlock_irqrestore(&ide_lock, flags);
1638 (void) ide_do_reset(drive);
1639 if (drive->suspend_reset) {
1641 * APM WAKE UP todo !!
1642 * int nogoodpower = 1;
1643 * while(nogoodpower) {
1644 * check_power1() or check_power2()
1645 * nogoodpower = 0;
1646 * }
1647 * HWIF(drive)->multiproc(drive);
1649 return ioctl_by_bdev(bdev, BLKRRPART, 0);
1651 return 0;
1654 case CDROMEJECT:
1655 case CDROMCLOSETRAY:
1656 return scsi_cmd_ioctl(bdev, cmd, arg);
1658 case HDIO_GET_BUSSTATE:
1659 if (!capable(CAP_SYS_ADMIN))
1660 return -EACCES;
1661 if (put_user(HWIF(drive)->bus_state, (long *)arg))
1662 return -EFAULT;
1663 return 0;
1665 case HDIO_SET_BUSSTATE:
1666 if (!capable(CAP_SYS_ADMIN))
1667 return -EACCES;
1668 if (HWIF(drive)->busproc)
1669 return HWIF(drive)->busproc(drive, (int)arg);
1670 return -EOPNOTSUPP;
1671 default:
1672 return -EINVAL;
1676 EXPORT_SYMBOL(generic_ide_ioctl);
1679 * stridx() returns the offset of c within s,
1680 * or -1 if c is '\0' or not found within s.
1682 static int __init stridx (const char *s, char c)
1684 char *i = strchr(s, c);
1685 return (i && c) ? i - s : -1;
1689 * match_parm() does parsing for ide_setup():
1691 * 1. the first char of s must be '='.
1692 * 2. if the remainder matches one of the supplied keywords,
1693 * the index (1 based) of the keyword is negated and returned.
1694 * 3. if the remainder is a series of no more than max_vals numbers
1695 * separated by commas, the numbers are saved in vals[] and a
1696 * count of how many were saved is returned. Base10 is assumed,
1697 * and base16 is allowed when prefixed with "0x".
1698 * 4. otherwise, zero is returned.
1700 static int __init match_parm (char *s, const char *keywords[], int vals[], int max_vals)
1702 static const char *decimal = "0123456789";
1703 static const char *hex = "0123456789abcdef";
1704 int i, n;
1706 if (*s++ == '=') {
1708 * Try matching against the supplied keywords,
1709 * and return -(index+1) if we match one
1711 if (keywords != NULL) {
1712 for (i = 0; *keywords != NULL; ++i) {
1713 if (!strcmp(s, *keywords++))
1714 return -(i+1);
1718 * Look for a series of no more than "max_vals"
1719 * numeric values separated by commas, in base10,
1720 * or base16 when prefixed with "0x".
1721 * Return a count of how many were found.
1723 for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
1724 vals[n] = i;
1725 while ((i = stridx(decimal, *++s)) >= 0)
1726 vals[n] = (vals[n] * 10) + i;
1727 if (*s == 'x' && !vals[n]) {
1728 while ((i = stridx(hex, *++s)) >= 0)
1729 vals[n] = (vals[n] * 0x10) + i;
1731 if (++n == max_vals)
1732 break;
1733 if (*s == ',' || *s == ';')
1734 ++s;
1736 if (!*s)
1737 return n;
1739 return 0; /* zero = nothing matched */
1742 #ifdef CONFIG_BLK_DEV_PDC4030
1743 static int __initdata probe_pdc4030;
1744 extern void init_pdc4030(void);
1745 #endif
1746 #ifdef CONFIG_BLK_DEV_ALI14XX
1747 static int __initdata probe_ali14xx;
1748 extern void init_ali14xx(void);
1749 #endif
1750 #ifdef CONFIG_BLK_DEV_UMC8672
1751 static int __initdata probe_umc8672;
1752 extern void init_umc8672(void);
1753 #endif
1754 #ifdef CONFIG_BLK_DEV_DTC2278
1755 static int __initdata probe_dtc2278;
1756 extern void init_dtc2278(void);
1757 #endif
1758 #ifdef CONFIG_BLK_DEV_HT6560B
1759 static int __initdata probe_ht6560b;
1760 extern void init_ht6560b(void);
1761 #endif
1762 #ifdef CONFIG_BLK_DEV_QD65XX
1763 static int __initdata probe_qd65xx;
1764 extern void init_qd65xx(void);
1765 #endif
1767 static int __initdata is_chipset_set[MAX_HWIFS];
1770 * ide_setup() gets called VERY EARLY during initialization,
1771 * to handle kernel "command line" strings beginning with "hdx="
1772 * or "ide". Here is the complete set currently supported:
1774 * "hdx=" is recognized for all "x" from "a" to "h", such as "hdc".
1775 * "idex=" is recognized for all "x" from "0" to "3", such as "ide1".
1777 * "hdx=noprobe" : drive may be present, but do not probe for it
1778 * "hdx=none" : drive is NOT present, ignore cmos and do not probe
1779 * "hdx=nowerr" : ignore the WRERR_STAT bit on this drive
1780 * "hdx=cdrom" : drive is present, and is a cdrom drive
1781 * "hdx=cyl,head,sect" : disk drive is present, with specified geometry
1782 * "hdx=remap63" : add 63 to all sector numbers (for OnTrack DM)
1783 * "hdx=remap" : remap 0->1 (for EZDrive)
1784 * "hdx=autotune" : driver will attempt to tune interface speed
1785 * to the fastest PIO mode supported,
1786 * if possible for this drive only.
1787 * Not fully supported by all chipset types,
1788 * and quite likely to cause trouble with
1789 * older/odd IDE drives.
1790 * "hdx=biostimings" : driver will NOT attempt to tune interface speed
1791 * (DMA/PIO) but always honour BIOS timings.
1792 * "hdx=slow" : insert a huge pause after each access to the data
1793 * port. Should be used only as a last resort.
1795 * "hdx=swapdata" : when the drive is a disk, byte swap all data
1796 * "hdx=bswap" : same as above..........
1797 * "hdxlun=xx" : set the drive last logical unit.
1798 * "hdx=flash" : allows for more than one ata_flash disk to be
1799 * registered. In most cases, only one device
1800 * will be present.
1801 * "hdx=scsi" : the return of the ide-scsi flag, this is useful for
1802 * allowwing ide-floppy, ide-tape, and ide-cdrom|writers
1803 * to use ide-scsi emulation on a device specific option.
1804 * "idebus=xx" : inform IDE driver of VESA/PCI bus speed in MHz,
1805 * where "xx" is between 20 and 66 inclusive,
1806 * used when tuning chipset PIO modes.
1807 * For PCI bus, 25 is correct for a P75 system,
1808 * 30 is correct for P90,P120,P180 systems,
1809 * and 33 is used for P100,P133,P166 systems.
1810 * If in doubt, use idebus=33 for PCI.
1811 * As for VLB, it is safest to not specify it.
1813 * "idex=noprobe" : do not attempt to access/use this interface
1814 * "idex=base" : probe for an interface at the addr specified,
1815 * where "base" is usually 0x1f0 or 0x170
1816 * and "ctl" is assumed to be "base"+0x206
1817 * "idex=base,ctl" : specify both base and ctl
1818 * "idex=base,ctl,irq" : specify base, ctl, and irq number
1819 * "idex=autotune" : driver will attempt to tune interface speed
1820 * to the fastest PIO mode supported,
1821 * for all drives on this interface.
1822 * Not fully supported by all chipset types,
1823 * and quite likely to cause trouble with
1824 * older/odd IDE drives.
1825 * "idex=noautotune" : driver will NOT attempt to tune interface speed
1826 * This is the default for most chipsets,
1827 * except the cmd640.
1828 * "idex=biostimings" : driver will NOT attempt to tune interface speed
1829 * (DMA/PIO) but always honour BIOS timings.
1830 * "idex=serialize" : do not overlap operations on idex and ide(x^1)
1831 * "idex=four" : four drives on idex and ide(x^1) share same ports
1832 * "idex=reset" : reset interface before first use
1833 * "idex=dma" : enable DMA by default on both drives if possible
1834 * "idex=ata66" : informs the interface that it has an 80c cable
1835 * for chipsets that are ATA-66 capable, but
1836 * the ablity to bit test for detection is
1837 * currently unknown.
1838 * "ide=reverse" : Formerly called to pci sub-system, but now local.
1840 * The following are valid ONLY on ide0, (except dc4030)
1841 * and the defaults for the base,ctl ports must not be altered.
1843 * "ide0=dtc2278" : probe/support DTC2278 interface
1844 * "ide0=ht6560b" : probe/support HT6560B interface
1845 * "ide0=cmd640_vlb" : *REQUIRED* for VLB cards with the CMD640 chip
1846 * (not for PCI -- automatically detected)
1847 * "ide0=qd65xx" : probe/support qd65xx interface
1848 * "ide0=ali14xx" : probe/support ali14xx chipsets (ALI M1439, M1443, M1445)
1849 * "ide0=umc8672" : probe/support umc8672 chipsets
1850 * "idex=dc4030" : probe/support Promise DC4030VL interface
1851 * "ide=doubler" : probe/support IDE doublers on Amiga
1853 int __init ide_setup (char *s)
1855 int i, vals[3];
1856 ide_hwif_t *hwif;
1857 ide_drive_t *drive;
1858 unsigned int hw, unit;
1859 const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
1860 const char max_hwif = '0' + (MAX_HWIFS - 1);
1863 if (strncmp(s,"hd",2) == 0 && s[2] == '=') /* hd= is for hd.c */
1864 return 0; /* driver and not us */
1866 if (strncmp(s,"ide",3) &&
1867 strncmp(s,"idebus",6) &&
1868 strncmp(s,"hd",2)) /* hdx= & hdxlun= */
1869 return 0;
1871 printk(KERN_INFO "ide_setup: %s", s);
1872 init_ide_data ();
1874 #ifdef CONFIG_BLK_DEV_IDEDOUBLER
1875 if (!strcmp(s, "ide=doubler")) {
1876 extern int ide_doubler;
1878 printk(" : Enabled support for IDE doublers\n");
1879 ide_doubler = 1;
1880 return 1;
1882 #endif /* CONFIG_BLK_DEV_IDEDOUBLER */
1884 if (!strcmp(s, "ide=nodma")) {
1885 printk("IDE: Prevented DMA\n");
1886 noautodma = 1;
1887 return 1;
1890 #ifdef CONFIG_BLK_DEV_IDEPCI
1891 if (!strcmp(s, "ide=reverse")) {
1892 ide_scan_direction = 1;
1893 printk(" : Enabled support for IDE inverse scan order.\n");
1894 return 1;
1896 #endif /* CONFIG_BLK_DEV_IDEPCI */
1899 * Look for drive options: "hdx="
1901 if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
1902 const char *hd_words[] = {
1903 "none", "noprobe", "nowerr", "cdrom", "serialize",
1904 "autotune", "noautotune", "slow", "swapdata", "bswap",
1905 "flash", "remap", "remap63", "scsi", "biostimings",
1906 NULL };
1907 unit = s[2] - 'a';
1908 hw = unit / MAX_DRIVES;
1909 unit = unit % MAX_DRIVES;
1910 hwif = &ide_hwifs[hw];
1911 drive = &hwif->drives[unit];
1912 if (strncmp(s + 4, "ide-", 4) == 0) {
1913 strlcpy(drive->driver_req, s + 4, sizeof(drive->driver_req));
1914 goto done;
1917 * Look for last lun option: "hdxlun="
1919 if (s[3] == 'l' && s[4] == 'u' && s[5] == 'n') {
1920 if (match_parm(&s[6], NULL, vals, 1) != 1)
1921 goto bad_option;
1922 if (vals[0] >= 0 && vals[0] <= 7) {
1923 drive->last_lun = vals[0];
1924 drive->forced_lun = 1;
1925 } else
1926 printk(" -- BAD LAST LUN! Expected value from 0 to 7");
1927 goto done;
1929 switch (match_parm(&s[3], hd_words, vals, 3)) {
1930 case -1: /* "none" */
1931 drive->nobios = 1; /* drop into "noprobe" */
1932 case -2: /* "noprobe" */
1933 drive->noprobe = 1;
1934 goto done;
1935 case -3: /* "nowerr" */
1936 drive->bad_wstat = BAD_R_STAT;
1937 hwif->noprobe = 0;
1938 goto done;
1939 case -4: /* "cdrom" */
1940 drive->present = 1;
1941 drive->media = ide_cdrom;
1942 hwif->noprobe = 0;
1943 goto done;
1944 case -5: /* "serialize" */
1945 printk(" -- USE \"ide%d=serialize\" INSTEAD", hw);
1946 goto do_serialize;
1947 case -6: /* "autotune" */
1948 drive->autotune = IDE_TUNE_AUTO;
1949 goto done;
1950 case -7: /* "noautotune" */
1951 drive->autotune = IDE_TUNE_NOAUTO;
1952 goto done;
1953 case -8: /* "slow" */
1954 drive->slow = 1;
1955 goto done;
1956 case -9: /* "swapdata" */
1957 case -10: /* "bswap" */
1958 drive->bswap = 1;
1959 goto done;
1960 case -11: /* "flash" */
1961 drive->ata_flash = 1;
1962 goto done;
1963 case -12: /* "remap" */
1964 drive->remap_0_to_1 = 1;
1965 goto done;
1966 case -13: /* "remap63" */
1967 drive->sect0 = 63;
1968 goto done;
1969 case -14: /* "scsi" */
1970 drive->scsi = 1;
1971 goto done;
1972 case -15: /* "biostimings" */
1973 drive->autotune = IDE_TUNE_BIOS;
1974 goto done;
1975 case 3: /* cyl,head,sect */
1976 drive->media = ide_disk;
1977 drive->cyl = drive->bios_cyl = vals[0];
1978 drive->head = drive->bios_head = vals[1];
1979 drive->sect = drive->bios_sect = vals[2];
1980 drive->present = 1;
1981 drive->forced_geom = 1;
1982 hwif->noprobe = 0;
1983 goto done;
1984 default:
1985 goto bad_option;
1989 if (s[0] != 'i' || s[1] != 'd' || s[2] != 'e')
1990 goto bad_option;
1992 * Look for bus speed option: "idebus="
1994 if (s[3] == 'b' && s[4] == 'u' && s[5] == 's') {
1995 if (match_parm(&s[6], NULL, vals, 1) != 1)
1996 goto bad_option;
1997 if (vals[0] >= 20 && vals[0] <= 66) {
1998 idebus_parameter = vals[0];
1999 } else
2000 printk(" -- BAD BUS SPEED! Expected value from 20 to 66");
2001 goto done;
2004 * Look for interface options: "idex="
2006 if (s[3] >= '0' && s[3] <= max_hwif) {
2008 * Be VERY CAREFUL changing this: note hardcoded indexes below
2009 * -9,-10 : are reserved for future idex calls to ease the hardcoding.
2011 const char *ide_words[] = {
2012 "noprobe", "serialize", "autotune", "noautotune",
2013 "reset", "dma", "ata66", "biostimings", "minus9",
2014 "minus10", "four", "qd65xx", "ht6560b", "cmd640_vlb",
2015 "dtc2278", "umc8672", "ali14xx", "dc4030", NULL };
2016 hw = s[3] - '0';
2017 hwif = &ide_hwifs[hw];
2018 i = match_parm(&s[4], ide_words, vals, 3);
2021 * Cryptic check to ensure chipset not already set for hwif.
2022 * Note: we can't depend on hwif->chipset here.
2024 if ((i >= -18 && i <= -11) || (i > 0 && i <= 3)) {
2025 /* chipset already specified */
2026 if (is_chipset_set[hw])
2027 goto bad_option;
2028 if (i > -18 && i <= -11) {
2029 /* these drivers are for "ide0=" only */
2030 if (hw != 0)
2031 goto bad_hwif;
2032 /* chipset already specified for 2nd port */
2033 if (is_chipset_set[hw+1])
2034 goto bad_option;
2036 is_chipset_set[hw] = 1;
2037 printk("\n");
2040 switch (i) {
2041 #ifdef CONFIG_BLK_DEV_PDC4030
2042 case -18: /* "dc4030" */
2043 probe_pdc4030 = 1;
2044 goto done;
2045 #endif
2046 #ifdef CONFIG_BLK_DEV_ALI14XX
2047 case -17: /* "ali14xx" */
2048 probe_ali14xx = 1;
2049 goto done;
2050 #endif
2051 #ifdef CONFIG_BLK_DEV_UMC8672
2052 case -16: /* "umc8672" */
2053 probe_umc8672 = 1;
2054 goto done;
2055 #endif
2056 #ifdef CONFIG_BLK_DEV_DTC2278
2057 case -15: /* "dtc2278" */
2058 probe_dtc2278 = 1;
2059 goto done;
2060 #endif
2061 #ifdef CONFIG_BLK_DEV_CMD640
2062 case -14: /* "cmd640_vlb" */
2064 extern int cmd640_vlb; /* flag for cmd640.c */
2065 cmd640_vlb = 1;
2066 goto done;
2068 #endif
2069 #ifdef CONFIG_BLK_DEV_HT6560B
2070 case -13: /* "ht6560b" */
2071 probe_ht6560b = 1;
2072 goto done;
2073 #endif
2074 #ifdef CONFIG_BLK_DEV_QD65XX
2075 case -12: /* "qd65xx" */
2076 probe_qd65xx = 1;
2077 goto done;
2078 #endif
2079 #ifdef CONFIG_BLK_DEV_4DRIVES
2080 case -11: /* "four" drives on one set of ports */
2082 ide_hwif_t *mate = &ide_hwifs[hw^1];
2083 mate->drives[0].select.all ^= 0x20;
2084 mate->drives[1].select.all ^= 0x20;
2085 hwif->chipset = mate->chipset = ide_4drives;
2086 mate->irq = hwif->irq;
2087 memcpy(mate->io_ports, hwif->io_ports, sizeof(hwif->io_ports));
2088 goto do_serialize;
2090 #endif /* CONFIG_BLK_DEV_4DRIVES */
2091 case -10: /* minus10 */
2092 case -9: /* minus9 */
2093 goto bad_option;
2094 case -8: /* "biostimings" */
2095 hwif->drives[0].autotune = IDE_TUNE_BIOS;
2096 hwif->drives[1].autotune = IDE_TUNE_BIOS;
2097 goto done;
2098 case -7: /* ata66 */
2099 #ifdef CONFIG_BLK_DEV_IDEPCI
2100 hwif->udma_four = 1;
2101 goto done;
2102 #else /* !CONFIG_BLK_DEV_IDEPCI */
2103 hwif->udma_four = 0;
2104 goto bad_hwif;
2105 #endif /* CONFIG_BLK_DEV_IDEPCI */
2106 case -6: /* dma */
2107 hwif->autodma = 1;
2108 goto done;
2109 case -5: /* "reset" */
2110 hwif->reset = 1;
2111 goto done;
2112 case -4: /* "noautotune" */
2113 hwif->drives[0].autotune = IDE_TUNE_NOAUTO;
2114 hwif->drives[1].autotune = IDE_TUNE_NOAUTO;
2115 goto done;
2116 case -3: /* "autotune" */
2117 hwif->drives[0].autotune = IDE_TUNE_AUTO;
2118 hwif->drives[1].autotune = IDE_TUNE_AUTO;
2119 goto done;
2120 case -2: /* "serialize" */
2121 do_serialize:
2122 hwif->mate = &ide_hwifs[hw^1];
2123 hwif->mate->mate = hwif;
2124 hwif->serialized = hwif->mate->serialized = 1;
2125 goto done;
2127 case -1: /* "noprobe" */
2128 hwif->noprobe = 1;
2129 goto done;
2131 case 1: /* base */
2132 vals[1] = vals[0] + 0x206; /* default ctl */
2133 case 2: /* base,ctl */
2134 vals[2] = 0; /* default irq = probe for it */
2135 case 3: /* base,ctl,irq */
2136 hwif->hw.irq = vals[2];
2137 ide_init_hwif_ports(&hwif->hw, (unsigned long) vals[0], (unsigned long) vals[1], &hwif->irq);
2138 memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->io_ports));
2139 hwif->irq = vals[2];
2140 hwif->noprobe = 0;
2141 hwif->chipset = ide_generic;
2142 goto done;
2144 case 0: goto bad_option;
2145 default:
2146 printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
2147 return 1;
2150 bad_option:
2151 printk(" -- BAD OPTION\n");
2152 return 1;
2153 bad_hwif:
2154 printk("-- NOT SUPPORTED ON ide%d", hw);
2155 done:
2156 printk("\n");
2157 return 1;
2161 * probe_for_hwifs() finds/initializes "known" IDE interfaces
2163 static void __init probe_for_hwifs (void)
2165 #ifdef CONFIG_BLK_DEV_IDEPCI
2166 ide_scan_pcibus(ide_scan_direction);
2167 #endif /* CONFIG_BLK_DEV_IDEPCI */
2169 #ifdef CONFIG_ETRAX_IDE
2171 extern void init_e100_ide(void);
2172 init_e100_ide();
2174 #endif /* CONFIG_ETRAX_IDE */
2175 #ifdef CONFIG_BLK_DEV_CMD640
2177 extern void ide_probe_for_cmd640x(void);
2178 ide_probe_for_cmd640x();
2180 #endif /* CONFIG_BLK_DEV_CMD640 */
2181 #ifdef CONFIG_BLK_DEV_PDC4030
2183 extern int ide_probe_for_pdc4030(void);
2184 (void) ide_probe_for_pdc4030();
2186 #endif /* CONFIG_BLK_DEV_PDC4030 */
2187 #ifdef CONFIG_BLK_DEV_IDE_PMAC
2189 extern void pmac_ide_probe(void);
2190 pmac_ide_probe();
2192 #endif /* CONFIG_BLK_DEV_IDE_PMAC */
2193 #ifdef CONFIG_BLK_DEV_IDE_SWARM
2195 extern void swarm_ide_probe(void);
2196 swarm_ide_probe();
2198 #endif /* CONFIG_BLK_DEV_IDE_SWARM */
2199 #ifdef CONFIG_BLK_DEV_GAYLE
2201 extern void gayle_init(void);
2202 gayle_init();
2204 #endif /* CONFIG_BLK_DEV_GAYLE */
2205 #ifdef CONFIG_BLK_DEV_FALCON_IDE
2207 extern void falconide_init(void);
2208 falconide_init();
2210 #endif /* CONFIG_BLK_DEV_FALCON_IDE */
2211 #ifdef CONFIG_BLK_DEV_MAC_IDE
2213 extern void macide_init(void);
2214 macide_init();
2216 #endif /* CONFIG_BLK_DEV_MAC_IDE */
2217 #ifdef CONFIG_BLK_DEV_Q40IDE
2219 extern void q40ide_init(void);
2220 q40ide_init();
2222 #endif /* CONFIG_BLK_DEV_Q40IDE */
2223 #ifdef CONFIG_BLK_DEV_BUDDHA
2225 extern void buddha_init(void);
2226 buddha_init();
2228 #endif /* CONFIG_BLK_DEV_BUDDHA */
2229 #if defined(CONFIG_BLK_DEV_IDEPNP) && defined(CONFIG_PNP)
2231 extern void pnpide_init(int enable);
2232 pnpide_init(1);
2234 #endif /* CONFIG_BLK_DEV_IDEPNP */
2237 void __init ide_init_builtin_drivers (void)
2240 * Probe for special PCI and other "known" interface chipsets
2242 probe_for_hwifs ();
2244 #ifdef CONFIG_BLK_DEV_IDE
2245 if (ide_hwifs[0].io_ports[IDE_DATA_OFFSET])
2246 ide_get_lock(NULL, NULL); /* for atari only */
2248 (void) ideprobe_init();
2250 if (ide_hwifs[0].io_ports[IDE_DATA_OFFSET])
2251 ide_release_lock(); /* for atari only */
2252 #endif /* CONFIG_BLK_DEV_IDE */
2254 #ifdef CONFIG_PROC_FS
2255 proc_ide_create();
2256 #endif
2260 * Actually unregister the subdriver. Called with the
2261 * request lock dropped.
2264 static int default_cleanup (ide_drive_t *drive)
2266 return ide_unregister_subdriver(drive);
2270 * Check if we can unregister the subdriver. Called with the
2271 * request lock held.
2274 static int default_shutdown(ide_drive_t *drive)
2276 if (drive->usage || DRIVER(drive)->busy) {
2277 return 1;
2279 drive->dead = 1;
2280 return 0;
2284 * Default function to use for the cache flush operation. This
2285 * must be replaced for disk devices (see ATA specification
2286 * documents on cache flush and drive suspend rules)
2288 * If we have no device attached or the device is not writable
2289 * this handler is sufficient.
2292 static int default_flushcache (ide_drive_t *drive)
2294 return 0;
2297 static ide_startstop_t default_do_request (ide_drive_t *drive, struct request *rq, sector_t block)
2299 ide_end_request(drive, 0, 0);
2300 return ide_stopped;
2303 static int default_end_request (ide_drive_t *drive, int uptodate, int nr_sects)
2305 return ide_end_request(drive, uptodate, nr_sects);
2308 static u8 default_sense (ide_drive_t *drive, const char *msg, u8 stat)
2310 return ide_dump_status(drive, msg, stat);
2313 static ide_startstop_t default_error (ide_drive_t *drive, const char *msg, u8 stat)
2315 return ide_error(drive, msg, stat);
2318 static void default_pre_reset (ide_drive_t *drive)
2322 static unsigned long default_capacity (ide_drive_t *drive)
2324 return 0x7fffffff;
2327 static ide_startstop_t default_special (ide_drive_t *drive)
2329 special_t *s = &drive->special;
2331 s->all = 0;
2332 drive->mult_req = 0;
2333 return ide_stopped;
2336 static int default_attach (ide_drive_t *drive)
2338 printk(KERN_ERR "%s: does not support hotswap of device class !\n",
2339 drive->name);
2341 return 0;
2344 static ide_startstop_t default_abort (ide_drive_t *drive, const char *msg)
2346 return ide_abort(drive, msg);
2349 static void setup_driver_defaults (ide_drive_t *drive)
2351 ide_driver_t *d = drive->driver;
2353 if (d->cleanup == NULL) d->cleanup = default_cleanup;
2354 if (d->shutdown == NULL) d->shutdown = default_shutdown;
2355 if (d->flushcache == NULL) d->flushcache = default_flushcache;
2356 if (d->do_request == NULL) d->do_request = default_do_request;
2357 if (d->end_request == NULL) d->end_request = default_end_request;
2358 if (d->sense == NULL) d->sense = default_sense;
2359 if (d->error == NULL) d->error = default_error;
2360 if (d->abort == NULL) d->abort = default_abort;
2361 if (d->pre_reset == NULL) d->pre_reset = default_pre_reset;
2362 if (d->capacity == NULL) d->capacity = default_capacity;
2363 if (d->special == NULL) d->special = default_special;
2364 if (d->attach == NULL) d->attach = default_attach;
2367 int ide_register_subdriver (ide_drive_t *drive, ide_driver_t *driver, int version)
2369 unsigned long flags;
2371 BUG_ON(drive->driver == NULL);
2373 spin_lock_irqsave(&ide_lock, flags);
2374 if (version != IDE_SUBDRIVER_VERSION || !drive->present ||
2375 drive->driver != &idedefault_driver || drive->usage || drive->dead) {
2376 spin_unlock_irqrestore(&ide_lock, flags);
2377 return 1;
2379 drive->driver = driver;
2380 setup_driver_defaults(drive);
2381 spin_unlock_irqrestore(&ide_lock, flags);
2382 spin_lock(&drives_lock);
2383 list_add_tail(&drive->list, &driver->drives);
2384 spin_unlock(&drives_lock);
2385 // printk(KERN_INFO "%s: attached %s driver.\n", drive->name, driver->name);
2386 if ((drive->autotune == IDE_TUNE_DEFAULT) ||
2387 (drive->autotune == IDE_TUNE_AUTO)) {
2388 /* DMA timings and setup moved to ide-probe.c */
2389 if (!driver->supports_dma && HWIF(drive)->ide_dma_off_quietly)
2390 // HWIF(drive)->ide_dma_off_quietly(drive);
2391 HWIF(drive)->ide_dma_off(drive);
2392 drive->dsc_overlap = (drive->next != drive && driver->supports_dsc_overlap);
2393 drive->nice1 = 1;
2395 drive->suspend_reset = 0;
2396 #ifdef CONFIG_PROC_FS
2397 if (drive->driver != &idedefault_driver) {
2398 ide_add_proc_entries(drive->proc, generic_subdriver_entries, drive);
2399 ide_add_proc_entries(drive->proc, driver->proc, drive);
2401 #endif
2402 return 0;
2405 EXPORT_SYMBOL(ide_register_subdriver);
2407 int ide_unregister_subdriver (ide_drive_t *drive)
2409 unsigned long flags;
2411 spin_lock_irqsave(&ide_lock, flags);
2412 if (drive->usage || drive->driver == &idedefault_driver || DRIVER(drive)->busy) {
2413 spin_unlock_irqrestore(&ide_lock, flags);
2414 return 1;
2416 #if defined(CONFIG_BLK_DEV_IDEPNP) && defined(CONFIG_PNP) && defined(MODULE)
2417 pnpide_init(0);
2418 #endif /* CONFIG_BLK_DEV_IDEPNP */
2419 #ifdef CONFIG_PROC_FS
2420 ide_remove_proc_entries(drive->proc, DRIVER(drive)->proc);
2421 ide_remove_proc_entries(drive->proc, generic_subdriver_entries);
2422 #endif
2423 auto_remove_settings(drive);
2424 drive->driver = &idedefault_driver;
2425 setup_driver_defaults(drive);
2426 spin_unlock_irqrestore(&ide_lock, flags);
2427 spin_lock(&drives_lock);
2428 list_del_init(&drive->list);
2429 spin_unlock(&drives_lock);
2430 /* drive will be added to &idedefault_driver->drives in ata_attach() */
2431 return 0;
2434 EXPORT_SYMBOL(ide_unregister_subdriver);
2436 static int ide_drive_remove(struct device * dev)
2438 ide_drive_t * drive = container_of(dev,ide_drive_t,gendev);
2439 DRIVER(drive)->cleanup(drive);
2440 return 0;
2443 int ide_register_driver(ide_driver_t *driver)
2445 struct list_head list;
2446 struct list_head *list_loop;
2447 struct list_head *tmp_storage;
2449 spin_lock(&drivers_lock);
2450 list_add(&driver->drivers, &drivers);
2451 spin_unlock(&drivers_lock);
2453 INIT_LIST_HEAD(&list);
2454 spin_lock(&drives_lock);
2455 list_splice_init(&idedefault_driver.drives, &list);
2456 spin_unlock(&drives_lock);
2458 list_for_each_safe(list_loop, tmp_storage, &list) {
2459 ide_drive_t *drive = container_of(list_loop, ide_drive_t, list);
2460 list_del_init(&drive->list);
2461 if (drive->present)
2462 ata_attach(drive);
2464 driver->gen_driver.name = (char *) driver->name;
2465 driver->gen_driver.bus = &ide_bus_type;
2466 driver->gen_driver.remove = ide_drive_remove;
2467 return driver_register(&driver->gen_driver);
2470 EXPORT_SYMBOL(ide_register_driver);
2472 void ide_unregister_driver(ide_driver_t *driver)
2474 ide_drive_t *drive;
2476 spin_lock(&drivers_lock);
2477 list_del(&driver->drivers);
2478 spin_unlock(&drivers_lock);
2480 driver_unregister(&driver->gen_driver);
2482 while(!list_empty(&driver->drives)) {
2483 drive = list_entry(driver->drives.next, ide_drive_t, list);
2484 if (driver->cleanup(drive)) {
2485 printk(KERN_ERR "%s: cleanup_module() called while still busy\n", drive->name);
2486 BUG();
2488 /* We must remove proc entries defined in this module.
2489 Otherwise we oops while accessing these entries */
2490 #ifdef CONFIG_PROC_FS
2491 if (drive->proc)
2492 ide_remove_proc_entries(drive->proc, driver->proc);
2493 #endif
2494 ata_attach(drive);
2498 EXPORT_SYMBOL(ide_unregister_driver);
2500 struct block_device_operations ide_fops[] = {{
2501 .owner = THIS_MODULE,
2502 .open = ide_open,
2505 EXPORT_SYMBOL(ide_fops);
2508 * Probe module
2511 EXPORT_SYMBOL(ide_lock);
2512 EXPORT_SYMBOL(ide_probe);
2514 struct bus_type ide_bus_type = {
2515 .name = "ide",
2519 * This is gets invoked once during initialization, to set *everything* up
2521 int __init ide_init (void)
2523 static char banner_printed;
2524 if (!banner_printed) {
2525 printk(KERN_INFO "Uniform Multi-Platform E-IDE driver " REVISION "\n");
2526 devfs_mk_dir("ide");
2527 system_bus_speed = ide_system_bus_speed();
2528 banner_printed = 1;
2531 bus_register(&ide_bus_type);
2533 init_ide_data();
2535 #ifdef CONFIG_BLK_DEV_PDC4030
2536 if (probe_pdc4030)
2537 init_pdc4030();
2538 #endif
2539 #ifdef CONFIG_BLK_DEV_ALI14XX
2540 if (probe_ali14xx)
2541 init_ali14xx();
2542 #endif
2543 #ifdef CONFIG_BLK_DEV_UMC8672
2544 if (probe_umc8672)
2545 init_umc8672();
2546 #endif
2547 #ifdef CONFIG_BLK_DEV_DTC2278
2548 if (probe_dtc2278)
2549 init_dtc2278();
2550 #endif
2551 #ifdef CONFIG_BLK_DEV_HT6560B
2552 if (probe_ht6560b)
2553 init_ht6560b();
2554 #endif
2555 #ifdef CONFIG_BLK_DEV_QD65XX
2556 if (probe_qd65xx)
2557 init_qd65xx();
2558 #endif
2560 initializing = 1;
2561 ide_init_builtin_drivers();
2562 initializing = 0;
2564 return 0;
2567 #ifdef MODULE
2568 char *options = NULL;
2569 MODULE_PARM(options,"s");
2570 MODULE_LICENSE("GPL");
2572 static void __init parse_options (char *line)
2574 char *next = line;
2576 if (line == NULL || !*line)
2577 return;
2578 while ((line = next) != NULL) {
2579 if ((next = strchr(line,' ')) != NULL)
2580 *next++ = 0;
2581 if (!ide_setup(line))
2582 printk (KERN_INFO "Unknown option '%s'\n", line);
2586 int init_module (void)
2588 parse_options(options);
2589 return ide_init();
2592 void cleanup_module (void)
2594 int index;
2596 for (index = 0; index < MAX_HWIFS; ++index) {
2597 ide_unregister(index);
2598 #if !defined(CONFIG_DMA_NONPCI)
2599 if (ide_hwifs[index].dma_base)
2600 (void) ide_release_dma(&ide_hwifs[index]);
2601 #endif /* !(CONFIG_DMA_NONPCI) */
2604 #ifdef CONFIG_PROC_FS
2605 proc_ide_destroy();
2606 #endif
2607 devfs_remove("ide");
2609 bus_unregister(&ide_bus_type);
2612 #else /* !MODULE */
2614 __setup("", ide_setup);
2616 module_init(ide_init);
2618 #endif /* MODULE */