2 * linux/drivers/ide/ide.c Version 7.00beta2 Mar 05 2003
4 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
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
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
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!
31 * | modified by Drew Eckhardt to check nr of hd's from the CMOS.
33 * | Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
34 * | in the early extended-partition checks and added DM partitions.
36 * | Early work on error handling by Mika Liljeberg (liljeber@cs.Helsinki.FI).
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,
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
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/bitops.h>
158 #include <asm/byteorder.h>
160 #include <asm/uaccess.h>
164 /* default maximum number of failures */
165 #define IDE_DEFAULT_MAX_FAILURES 1
167 static const u8 ide_hwif_to_major[] = { IDE0_MAJOR, IDE1_MAJOR,
168 IDE2_MAJOR, IDE3_MAJOR,
169 IDE4_MAJOR, IDE5_MAJOR,
170 IDE6_MAJOR, IDE7_MAJOR,
171 IDE8_MAJOR, IDE9_MAJOR };
173 static int idebus_parameter; /* holds the "idebus=" parameter */
174 static int system_bus_speed; /* holds what we think is VESA/PCI bus speed */
175 static int initializing; /* set while initializing built-in drivers */
177 DECLARE_MUTEX(ide_cfg_sem);
178 spinlock_t ide_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
180 #ifdef CONFIG_BLK_DEV_IDEPCI
181 static int ide_scan_direction; /* THIS was formerly 2.2.x pci=reverse */
184 #ifdef CONFIG_IDEDMA_AUTO
190 EXPORT_SYMBOL(noautodma);
193 * This is declared extern in ide.h, for access by other IDE modules:
195 ide_hwif_t ide_hwifs[MAX_HWIFS]; /* master data repository */
197 EXPORT_SYMBOL(ide_hwifs);
199 extern ide_driver_t idedefault_driver;
200 static void setup_driver_defaults(ide_driver_t *driver);
203 * Do not even *think* about calling this!
205 static void init_hwif_data(ide_hwif_t *hwif, unsigned int index)
209 /* bulk initialize hwif & drive info with zeros */
210 memset(hwif, 0, sizeof(ide_hwif_t));
212 /* fill in any non-zero initial values */
214 hwif->major = ide_hwif_to_major[index];
219 hwif->name[3] = '0' + index;
221 hwif->bus_state = BUSSTATE_ON;
223 hwif->atapi_dma = 0; /* disable all atapi dma */
224 hwif->ultra_mask = 0x80; /* disable all ultra */
225 hwif->mwdma_mask = 0x80; /* disable all mwdma */
226 hwif->swdma_mask = 0x80; /* disable all swdma */
228 sema_init(&hwif->gendev_rel_sem, 0);
230 default_hwif_iops(hwif);
231 default_hwif_transport(hwif);
232 for (unit = 0; unit < MAX_DRIVES; ++unit) {
233 ide_drive_t *drive = &hwif->drives[unit];
235 drive->media = ide_disk;
236 drive->select.all = (unit<<4)|0xa0;
239 drive->ready_stat = READY_STAT;
240 drive->bad_wstat = BAD_W_STAT;
241 drive->special.b.recalibrate = 1;
242 drive->special.b.set_geometry = 1;
243 drive->name[0] = 'h';
244 drive->name[1] = 'd';
245 drive->name[2] = 'a' + (index * MAX_DRIVES) + unit;
246 drive->max_failures = IDE_DEFAULT_MAX_FAILURES;
247 drive->using_dma = 0;
249 drive->driver = &idedefault_driver;
251 INIT_LIST_HEAD(&drive->list);
252 sema_init(&drive->gendev_rel_sem, 0);
256 static void init_hwif_default(ide_hwif_t *hwif, unsigned int index)
260 memset(&hw, 0, sizeof(hw_regs_t));
262 ide_init_hwif_ports(&hw, ide_default_io_base(index), 0, &hwif->irq);
264 memcpy(&hwif->hw, &hw, sizeof(hw));
265 memcpy(hwif->io_ports, hw.io_ports, sizeof(hw.io_ports));
267 hwif->noprobe = !hwif->io_ports[IDE_DATA_OFFSET];
268 #ifdef CONFIG_BLK_DEV_HD
269 if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA)
270 hwif->noprobe = 1; /* may be overridden by ide_setup() */
274 extern void ide_arm_init(void);
277 * init_ide_data() sets reasonable default values into all fields
278 * of all instances of the hwifs and drives, but only on the first call.
279 * Subsequent calls have no effect (they don't wipe out anything).
281 * This routine is normally called at driver initialization time,
282 * but may also be called MUCH earlier during kernel "command-line"
283 * parameter processing. As such, we cannot depend on any other parts
284 * of the kernel (such as memory allocation) to be functioning yet.
286 * This is too bad, as otherwise we could dynamically allocate the
287 * ide_drive_t structs as needed, rather than always consuming memory
288 * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
290 * FIXME: We should stuff the setup data into __init and copy the
291 * relevant hwifs/allocate them properly during boot.
293 #define MAGIC_COOKIE 0x12345678
294 static void __init init_ide_data (void)
298 static unsigned long magic_cookie = MAGIC_COOKIE;
300 if (magic_cookie != MAGIC_COOKIE)
301 return; /* already initialized */
304 setup_driver_defaults(&idedefault_driver);
306 /* Initialise all interface structures */
307 for (index = 0; index < MAX_HWIFS; ++index) {
308 hwif = &ide_hwifs[index];
309 init_hwif_data(hwif, index);
310 init_hwif_default(hwif, index);
311 #if !defined(CONFIG_PPC32) || !defined(CONFIG_PCI)
312 hwif->irq = hwif->hw.irq =
313 ide_init_default_irq(hwif->io_ports[IDE_DATA_OFFSET]);
316 #ifdef CONFIG_IDE_ARM
324 * ide_system_bus_speed - guess bus speed
326 * ide_system_bus_speed() returns what we think is the system VESA/PCI
327 * bus speed (in MHz). This is used for calculating interface PIO timings.
328 * The default is 40 for known PCI systems, 50 otherwise.
329 * The "idebus=xx" parameter can be used to override this value.
330 * The actual value to be used is computed/displayed the first time
331 * through. Drivers should only use this as a last resort.
333 * Returns a guessed speed in MHz.
336 int ide_system_bus_speed (void)
338 static struct pci_device_id pci_default[] = {
339 { PCI_DEVICE(PCI_ANY_ID, PCI_ANY_ID) },
343 if (!system_bus_speed) {
344 if (idebus_parameter) {
345 /* user supplied value */
346 system_bus_speed = idebus_parameter;
347 } else if (pci_dev_present(pci_default)) {
348 /* safe default value for PCI */
349 system_bus_speed = 33;
351 /* safe default value for VESA and PCI */
352 system_bus_speed = 50;
354 printk(KERN_INFO "ide: Assuming %dMHz system bus speed "
355 "for PIO modes%s\n", system_bus_speed,
356 idebus_parameter ? "" : "; override with idebus=xx");
358 return system_bus_speed;
361 static int ide_open (struct inode * inode, struct file * filp)
367 * drives_lock protects the list of drives, drivers_lock the
368 * list of drivers. Currently nobody takes both at once.
371 static spinlock_t drives_lock = SPIN_LOCK_UNLOCKED;
372 static spinlock_t drivers_lock = SPIN_LOCK_UNLOCKED;
373 static LIST_HEAD(drivers);
375 /* Iterator for the driver list. */
377 static void *m_start(struct seq_file *m, loff_t *pos)
381 spin_lock(&drivers_lock);
382 list_for_each(p, &drivers)
384 return list_entry(p, ide_driver_t, drivers);
388 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
390 struct list_head *p = ((ide_driver_t *)v)->drivers.next;
392 return p==&drivers ? NULL : list_entry(p, ide_driver_t, drivers);
395 static void m_stop(struct seq_file *m, void *v)
397 spin_unlock(&drivers_lock);
400 static int show_driver(struct seq_file *m, void *v)
402 ide_driver_t *driver = v;
403 seq_printf(m, "%s version %s\n", driver->name, driver->version);
407 struct seq_operations ide_drivers_op = {
414 #ifdef CONFIG_PROC_FS
415 struct proc_dir_entry *proc_ide_root;
417 ide_proc_entry_t generic_subdriver_entries[] = {
418 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
419 { NULL, 0, NULL, NULL }
423 static struct resource* hwif_request_region(ide_hwif_t *hwif,
424 unsigned long addr, int num)
426 struct resource *res = request_region(addr, num, hwif->name);
429 printk(KERN_ERR "%s: I/O resource 0x%lX-0x%lX not free.\n",
430 hwif->name, addr, addr+num-1);
435 * ide_hwif_request_regions - request resources for IDE
436 * @hwif: interface to use
438 * Requests all the needed resources for an interface.
439 * Right now core IDE code does this work which is deeply wrong.
440 * MMIO leaves it to the controller driver,
441 * PIO will migrate this way over time.
444 int ide_hwif_request_regions(ide_hwif_t *hwif)
451 BUG_ON(hwif->mmio == 1);
452 addr = hwif->io_ports[IDE_CONTROL_OFFSET];
453 if (addr && !hwif_request_region(hwif, addr, 1))
454 goto control_region_busy;
456 addr = hwif->io_ports[IDE_DATA_OFFSET];
457 if ((addr | 7) == hwif->io_ports[IDE_STATUS_OFFSET]) {
458 if (!hwif_request_region(hwif, addr, 8))
459 goto data_region_busy;
463 for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++) {
464 addr = hwif->io_ports[i];
465 if (!hwif_request_region(hwif, addr, 1)) {
467 release_region(addr, 1);
468 goto data_region_busy;
474 addr = hwif->io_ports[IDE_CONTROL_OFFSET];
476 release_region(addr, 1);
478 /* If any errors are return, we drop the hwif interface. */
483 * ide_hwif_release_regions - free IDE resources
485 * Note that we only release the standard ports,
486 * and do not even try to handle any extra ports
487 * allocated for weird IDE interface chipsets.
489 * Note also that we don't yet handle mmio resources here. More
490 * importantly our caller should be doing this so we need to
491 * restructure this as a helper function for drivers.
494 void ide_hwif_release_regions(ide_hwif_t *hwif)
500 if (hwif->io_ports[IDE_CONTROL_OFFSET])
501 release_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
502 if (hwif->straight8) {
503 release_region(hwif->io_ports[IDE_DATA_OFFSET], 8);
506 for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++)
507 if (hwif->io_ports[i])
508 release_region(hwif->io_ports[i], 1);
512 * ide_hwif_restore - restore hwif to template
513 * @hwif: hwif to update
514 * @tmp_hwif: template
516 * Restore hwif to a previous state by copying most settngs
520 static void ide_hwif_restore(ide_hwif_t *hwif, ide_hwif_t *tmp_hwif)
522 hwif->hwgroup = tmp_hwif->hwgroup;
524 hwif->gendev.parent = tmp_hwif->gendev.parent;
526 hwif->proc = tmp_hwif->proc;
528 hwif->major = tmp_hwif->major;
529 hwif->straight8 = tmp_hwif->straight8;
530 hwif->bus_state = tmp_hwif->bus_state;
532 hwif->atapi_dma = tmp_hwif->atapi_dma;
533 hwif->ultra_mask = tmp_hwif->ultra_mask;
534 hwif->mwdma_mask = tmp_hwif->mwdma_mask;
535 hwif->swdma_mask = tmp_hwif->swdma_mask;
537 hwif->chipset = tmp_hwif->chipset;
538 hwif->hold = tmp_hwif->hold;
540 #ifdef CONFIG_BLK_DEV_IDEPCI
541 hwif->pci_dev = tmp_hwif->pci_dev;
542 hwif->cds = tmp_hwif->cds;
545 hwif->tuneproc = tmp_hwif->tuneproc;
546 hwif->speedproc = tmp_hwif->speedproc;
547 hwif->selectproc = tmp_hwif->selectproc;
548 hwif->reset_poll = tmp_hwif->reset_poll;
549 hwif->pre_reset = tmp_hwif->pre_reset;
550 hwif->resetproc = tmp_hwif->resetproc;
551 hwif->intrproc = tmp_hwif->intrproc;
552 hwif->maskproc = tmp_hwif->maskproc;
553 hwif->quirkproc = tmp_hwif->quirkproc;
554 hwif->busproc = tmp_hwif->busproc;
556 hwif->ata_input_data = tmp_hwif->ata_input_data;
557 hwif->ata_output_data = tmp_hwif->ata_output_data;
558 hwif->atapi_input_bytes = tmp_hwif->atapi_input_bytes;
559 hwif->atapi_output_bytes = tmp_hwif->atapi_output_bytes;
561 hwif->dma_setup = tmp_hwif->dma_setup;
562 hwif->dma_exec_cmd = tmp_hwif->dma_exec_cmd;
563 hwif->dma_start = tmp_hwif->dma_start;
564 hwif->ide_dma_end = tmp_hwif->ide_dma_end;
565 hwif->ide_dma_check = tmp_hwif->ide_dma_check;
566 hwif->ide_dma_on = tmp_hwif->ide_dma_on;
567 hwif->ide_dma_off_quietly = tmp_hwif->ide_dma_off_quietly;
568 hwif->ide_dma_test_irq = tmp_hwif->ide_dma_test_irq;
569 hwif->ide_dma_host_on = tmp_hwif->ide_dma_host_on;
570 hwif->ide_dma_host_off = tmp_hwif->ide_dma_host_off;
571 hwif->ide_dma_lostirq = tmp_hwif->ide_dma_lostirq;
572 hwif->ide_dma_timeout = tmp_hwif->ide_dma_timeout;
574 hwif->OUTB = tmp_hwif->OUTB;
575 hwif->OUTBSYNC = tmp_hwif->OUTBSYNC;
576 hwif->OUTW = tmp_hwif->OUTW;
577 hwif->OUTL = tmp_hwif->OUTL;
578 hwif->OUTSW = tmp_hwif->OUTSW;
579 hwif->OUTSL = tmp_hwif->OUTSL;
581 hwif->INB = tmp_hwif->INB;
582 hwif->INW = tmp_hwif->INW;
583 hwif->INL = tmp_hwif->INL;
584 hwif->INSW = tmp_hwif->INSW;
585 hwif->INSL = tmp_hwif->INSL;
587 hwif->sg_max_nents = tmp_hwif->sg_max_nents;
589 hwif->mmio = tmp_hwif->mmio;
590 hwif->rqsize = tmp_hwif->rqsize;
591 hwif->no_lba48 = tmp_hwif->no_lba48;
593 #ifndef CONFIG_BLK_DEV_IDECS
594 hwif->irq = tmp_hwif->irq;
597 hwif->dma_base = tmp_hwif->dma_base;
598 hwif->dma_master = tmp_hwif->dma_master;
599 hwif->dma_command = tmp_hwif->dma_command;
600 hwif->dma_vendor1 = tmp_hwif->dma_vendor1;
601 hwif->dma_status = tmp_hwif->dma_status;
602 hwif->dma_vendor3 = tmp_hwif->dma_vendor3;
603 hwif->dma_prdtable = tmp_hwif->dma_prdtable;
605 hwif->dma_extra = tmp_hwif->dma_extra;
606 hwif->config_data = tmp_hwif->config_data;
607 hwif->select_data = tmp_hwif->select_data;
608 hwif->autodma = tmp_hwif->autodma;
609 hwif->udma_four = tmp_hwif->udma_four;
610 hwif->no_dsc = tmp_hwif->no_dsc;
612 hwif->hwif_data = tmp_hwif->hwif_data;
616 * ide_unregister - free an ide interface
617 * @index: index of interface (will change soon to a pointer)
619 * Perform the final unregister of an IDE interface. At the moment
620 * we don't refcount interfaces so this will also get split up.
623 * The caller must not hold the IDE locks
624 * The drive present/vanishing is not yet properly locked
625 * Take care with the callbacks. These have been split to avoid
626 * deadlocking the IDE layer. The shutdown callback is called
627 * before we take the lock and free resources. It is up to the
628 * caller to be sure there is no pending I/O here, and that
629 * the interfce will not be reopened (present/vanishing locking
630 * isnt yet done btw). After we commit to the final kill we
631 * call the cleanup callback with the ide locks held.
633 * Unregister restores the hwif structures to the default state.
634 * This is raving bonkers.
637 void ide_unregister(unsigned int index)
640 ide_hwif_t *hwif, *g;
641 static ide_hwif_t tmp_hwif; /* protected by ide_cfg_sem */
642 ide_hwgroup_t *hwgroup;
643 int irq_count = 0, unit, i;
645 BUG_ON(index >= MAX_HWIFS);
647 BUG_ON(in_interrupt());
648 BUG_ON(irqs_disabled());
650 spin_lock_irq(&ide_lock);
651 hwif = &ide_hwifs[index];
654 for (unit = 0; unit < MAX_DRIVES; ++unit) {
655 drive = &hwif->drives[unit];
658 if (drive->usage || DRIVER(drive)->busy)
664 spin_unlock_irq(&ide_lock);
666 for (unit = 0; unit < MAX_DRIVES; ++unit) {
667 drive = &hwif->drives[unit];
670 DRIVER(drive)->cleanup(drive);
673 destroy_proc_ide_interface(hwif);
675 hwgroup = hwif->hwgroup;
677 * free the irq if we were the only hwif using it
681 if (g->irq == hwif->irq)
684 } while (g != hwgroup->hwif);
686 free_irq(hwif->irq, hwgroup);
688 spin_lock_irq(&ide_lock);
690 * Note that we only release the standard ports,
691 * and do not even try to handle any extra ports
692 * allocated for weird IDE interface chipsets.
694 ide_hwif_release_regions(hwif);
697 * Remove us from the hwgroup, and free
698 * the hwgroup if we were the only member
700 for (i = 0; i < MAX_DRIVES; ++i) {
701 drive = &hwif->drives[i];
702 if (drive->devfs_name[0] != '\0') {
703 devfs_remove(drive->devfs_name);
704 drive->devfs_name[0] = '\0';
708 if (drive == drive->next) {
709 /* special case: last drive from hwgroup. */
710 BUG_ON(hwgroup->drive != drive);
711 hwgroup->drive = NULL;
715 walk = hwgroup->drive;
716 while (walk->next != drive)
718 walk->next = drive->next;
719 if (hwgroup->drive == drive) {
720 hwgroup->drive = drive->next;
721 hwgroup->hwif = HWIF(hwgroup->drive);
724 BUG_ON(hwgroup->drive == drive);
725 if (drive->id != NULL) {
730 /* Messed up locking ... */
731 spin_unlock_irq(&ide_lock);
732 blk_cleanup_queue(drive->queue);
733 device_unregister(&drive->gendev);
734 down(&drive->gendev_rel_sem);
735 spin_lock_irq(&ide_lock);
738 if (hwif->next == hwif) {
739 BUG_ON(hwgroup->hwif != hwif);
742 /* There is another interface in hwgroup.
743 * Unlink us, and set hwgroup->drive and ->hwif to
747 while (g->next != hwif)
749 g->next = hwif->next;
750 if (hwgroup->hwif == hwif) {
751 /* Chose a random hwif for hwgroup->hwif.
752 * It's guaranteed that there are no drives
753 * left in the hwgroup.
755 BUG_ON(hwgroup->drive != NULL);
758 BUG_ON(hwgroup->hwif == hwif);
761 /* More messed up locking ... */
762 spin_unlock_irq(&ide_lock);
763 device_unregister(&hwif->gendev);
764 down(&hwif->gendev_rel_sem);
767 * Remove us from the kernel's knowledge
769 blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
770 for (i = 0; i < MAX_DRIVES; i++) {
771 struct gendisk *disk = hwif->drives[i].disk;
772 hwif->drives[i].disk = NULL;
775 kfree(hwif->sg_table);
776 unregister_blkdev(hwif->major, hwif->name);
777 spin_lock_irq(&ide_lock);
779 if (hwif->dma_base) {
780 (void) ide_release_dma(hwif);
783 hwif->dma_master = 0;
784 hwif->dma_command = 0;
785 hwif->dma_vendor1 = 0;
786 hwif->dma_status = 0;
787 hwif->dma_vendor3 = 0;
788 hwif->dma_prdtable = 0;
791 /* copy original settings */
794 /* restore hwif data to pristine status */
795 init_hwif_data(hwif, index);
796 init_hwif_default(hwif, index);
798 ide_hwif_restore(hwif, &tmp_hwif);
801 spin_unlock_irq(&ide_lock);
805 EXPORT_SYMBOL(ide_unregister);
809 * ide_setup_ports - set up IDE interface ports
810 * @hw: register descriptions
811 * @base: base register
812 * @offsets: table of register offsets
813 * @ctrl: control register
815 * @irq: interrupt lie
817 * Setup hw_regs_t structure described by parameters. You
818 * may set up the hw structure yourself OR use this routine to
819 * do it for you. This is basically a helper
823 void ide_setup_ports ( hw_regs_t *hw,
824 unsigned long base, int *offsets,
825 unsigned long ctrl, unsigned long intr,
826 ide_ack_intr_t *ack_intr,
828 * ide_io_ops_t *iops,
834 for (i = 0; i < IDE_NR_PORTS; i++) {
835 if (offsets[i] == -1) {
837 case IDE_CONTROL_OFFSET:
838 hw->io_ports[i] = ctrl;
840 #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
842 hw->io_ports[i] = intr;
844 #endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
850 hw->io_ports[i] = base + offsets[i];
855 hw->ack_intr = ack_intr;
862 * ide_register_hw_with_fixup - register IDE interface
863 * @hw: hardware registers
864 * @hwifp: pointer to returned hwif
865 * @fixup: fixup function
867 * Register an IDE interface, specifying exactly the registers etc.
868 * Set init=1 iff calling before probes have taken place.
870 * Returns -1 on error.
873 int ide_register_hw_with_fixup(hw_regs_t *hw, ide_hwif_t **hwifp, void(*fixup)(ide_hwif_t *hwif))
875 int index, retry = 1;
879 for (index = 0; index < MAX_HWIFS; ++index) {
880 hwif = &ide_hwifs[index];
881 if (hwif->hw.io_ports[IDE_DATA_OFFSET] == hw->io_ports[IDE_DATA_OFFSET])
884 for (index = 0; index < MAX_HWIFS; ++index) {
885 hwif = &ide_hwifs[index];
888 if ((!hwif->present && !hwif->mate && !initializing) ||
889 (!hwif->hw.io_ports[IDE_DATA_OFFSET] && initializing))
892 for (index = 0; index < MAX_HWIFS; index++)
893 ide_unregister(index);
898 ide_unregister(index);
899 else if (!hwif->hold) {
900 init_hwif_data(hwif, index);
901 init_hwif_default(hwif, index);
905 memcpy(&hwif->hw, hw, sizeof(*hw));
906 memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->hw.io_ports));
909 hwif->chipset = hw->chipset;
912 probe_hwif_init_with_fixup(hwif, fixup);
913 create_proc_ide_interfaces();
919 return (initializing || hwif->present) ? index : -1;
922 EXPORT_SYMBOL(ide_register_hw_with_fixup);
924 int ide_register_hw(hw_regs_t *hw, ide_hwif_t **hwifp)
926 return ide_register_hw_with_fixup(hw, hwifp, NULL);
929 EXPORT_SYMBOL(ide_register_hw);
932 * Locks for IDE setting functionality
935 DECLARE_MUTEX(ide_setting_sem);
938 * ide_add_setting - add an ide setting option
939 * @drive: drive to use
940 * @name: setting name
941 * @rw: true if the function is read write
942 * @read_ioctl: function to call on read
943 * @write_ioctl: function to call on write
944 * @data_type: type of data
945 * @min: range minimum
946 * @max: range maximum
947 * @mul_factor: multiplication scale
948 * @div_factor: divison scale
949 * @data: private data field
952 * Removes the setting named from the device if it is present.
953 * The function takes the settings_lock to protect against
954 * parallel changes. This function must not be called from IRQ
955 * context. Returns 0 on success or -1 on failure.
957 * BUGS: This code is seriously over-engineered. There is also
958 * magic about how the driver specific features are setup. If
959 * a driver is attached we assume the driver settings are auto
963 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)
965 ide_settings_t **p = (ide_settings_t **) &drive->settings, *setting = NULL;
967 down(&ide_setting_sem);
968 while ((*p) && strcmp((*p)->name, name) < 0)
970 if ((setting = kmalloc(sizeof(*setting), GFP_KERNEL)) == NULL)
972 memset(setting, 0, sizeof(*setting));
973 if ((setting->name = kmalloc(strlen(name) + 1, GFP_KERNEL)) == NULL)
975 strcpy(setting->name, name);
977 setting->read_ioctl = read_ioctl;
978 setting->write_ioctl = write_ioctl;
979 setting->data_type = data_type;
982 setting->mul_factor = mul_factor;
983 setting->div_factor = div_factor;
984 setting->data = data;
988 if (drive->driver != &idedefault_driver)
989 setting->auto_remove = 1;
991 up(&ide_setting_sem);
994 up(&ide_setting_sem);
1000 EXPORT_SYMBOL(ide_add_setting);
1003 * __ide_remove_setting - remove an ide setting option
1004 * @drive: drive to use
1005 * @name: setting name
1007 * Removes the setting named from the device if it is present.
1008 * The caller must hold the setting semaphore.
1011 static void __ide_remove_setting (ide_drive_t *drive, char *name)
1013 ide_settings_t **p, *setting;
1015 p = (ide_settings_t **) &drive->settings;
1017 while ((*p) && strcmp((*p)->name, name))
1019 if ((setting = (*p)) == NULL)
1022 (*p) = setting->next;
1024 kfree(setting->name);
1029 * ide_find_setting_by_ioctl - find a drive specific ioctl
1030 * @drive: drive to scan
1031 * @cmd: ioctl command to handle
1033 * Scan's the device setting table for a matching entry and returns
1034 * this or NULL if no entry is found. The caller must hold the
1038 static ide_settings_t *ide_find_setting_by_ioctl (ide_drive_t *drive, int cmd)
1040 ide_settings_t *setting = drive->settings;
1043 if (setting->read_ioctl == cmd || setting->write_ioctl == cmd)
1045 setting = setting->next;
1052 * ide_find_setting_by_name - find a drive specific setting
1053 * @drive: drive to scan
1054 * @name: setting name
1056 * Scan's the device setting table for a matching entry and returns
1057 * this or NULL if no entry is found. The caller must hold the
1061 ide_settings_t *ide_find_setting_by_name (ide_drive_t *drive, char *name)
1063 ide_settings_t *setting = drive->settings;
1066 if (strcmp(setting->name, name) == 0)
1068 setting = setting->next;
1074 * auto_remove_settings - remove driver specific settings
1077 * Automatically remove all the driver specific settings for this
1078 * drive. This function may sleep and must not be called from IRQ
1079 * context. The caller must hold ide_setting_sem.
1082 static void auto_remove_settings (ide_drive_t *drive)
1084 ide_settings_t *setting;
1086 setting = drive->settings;
1088 if (setting->auto_remove) {
1089 __ide_remove_setting(drive, setting->name);
1092 setting = setting->next;
1097 * ide_read_setting - read an IDE setting
1098 * @drive: drive to read from
1099 * @setting: drive setting
1101 * Read a drive setting and return the value. The caller
1102 * must hold the ide_setting_sem when making this call.
1104 * BUGS: the data return and error are the same return value
1105 * so an error -EINVAL and true return of the same value cannot
1109 int ide_read_setting (ide_drive_t *drive, ide_settings_t *setting)
1112 unsigned long flags;
1114 if ((setting->rw & SETTING_READ)) {
1115 spin_lock_irqsave(&ide_lock, flags);
1116 switch(setting->data_type) {
1118 val = *((u8 *) setting->data);
1121 val = *((u16 *) setting->data);
1125 val = *((u32 *) setting->data);
1128 spin_unlock_irqrestore(&ide_lock, flags);
1134 * ide_spin_wait_hwgroup - wait for group
1135 * @drive: drive in the group
1137 * Wait for an IDE device group to go non busy and then return
1138 * holding the ide_lock which guards the hwgroup->busy status
1139 * and right to use it.
1142 int ide_spin_wait_hwgroup (ide_drive_t *drive)
1144 ide_hwgroup_t *hwgroup = HWGROUP(drive);
1145 unsigned long timeout = jiffies + (3 * HZ);
1147 spin_lock_irq(&ide_lock);
1149 while (hwgroup->busy) {
1150 unsigned long lflags;
1151 spin_unlock_irq(&ide_lock);
1152 local_irq_set(lflags);
1153 if (time_after(jiffies, timeout)) {
1154 local_irq_restore(lflags);
1155 printk(KERN_ERR "%s: channel busy\n", drive->name);
1158 local_irq_restore(lflags);
1159 spin_lock_irq(&ide_lock);
1164 EXPORT_SYMBOL(ide_spin_wait_hwgroup);
1167 * ide_write_setting - read an IDE setting
1168 * @drive: drive to read from
1169 * @setting: drive setting
1172 * Write a drive setting if it is possible. The caller
1173 * must hold the ide_setting_sem when making this call.
1175 * BUGS: the data return and error are the same return value
1176 * so an error -EINVAL and true return of the same value cannot
1179 * FIXME: This should be changed to enqueue a special request
1180 * to the driver to change settings, and then wait on a sema for completion.
1181 * The current scheme of polling is kludgy, though safe enough.
1184 int ide_write_setting (ide_drive_t *drive, ide_settings_t *setting, int val)
1189 if (!capable(CAP_SYS_ADMIN))
1191 if (!(setting->rw & SETTING_WRITE))
1193 if (val < setting->min || val > setting->max)
1196 return setting->set(drive, val);
1197 if (ide_spin_wait_hwgroup(drive))
1199 switch (setting->data_type) {
1201 *((u8 *) setting->data) = val;
1204 *((u16 *) setting->data) = val;
1207 *((u32 *) setting->data) = val;
1210 p = (u32 *) setting->data;
1211 for (i = 0; i < 1 << PARTN_BITS; i++, p++)
1215 spin_unlock_irq(&ide_lock);
1219 static int set_io_32bit(ide_drive_t *drive, int arg)
1221 drive->io_32bit = arg;
1222 #ifdef CONFIG_BLK_DEV_DTC2278
1223 if (HWIF(drive)->chipset == ide_dtc2278)
1224 HWIF(drive)->drives[!drive->select.b.unit].io_32bit = arg;
1225 #endif /* CONFIG_BLK_DEV_DTC2278 */
1229 static int set_using_dma (ide_drive_t *drive, int arg)
1231 #ifdef CONFIG_BLK_DEV_IDEDMA
1232 if (!drive->id || !(drive->id->capability & 1))
1234 if (HWIF(drive)->ide_dma_check == NULL)
1237 if (HWIF(drive)->ide_dma_check(drive)) return -EIO;
1238 if (HWIF(drive)->ide_dma_on(drive)) return -EIO;
1240 if (__ide_dma_off(drive))
1249 static int set_pio_mode (ide_drive_t *drive, int arg)
1253 if (!HWIF(drive)->tuneproc)
1255 if (drive->special.b.set_tune)
1257 ide_init_drive_cmd(&rq);
1258 drive->tune_req = (u8) arg;
1259 drive->special.b.set_tune = 1;
1260 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
1264 static int set_xfer_rate (ide_drive_t *drive, int arg)
1266 int err = ide_wait_cmd(drive,
1267 WIN_SETFEATURES, (u8) arg,
1268 SETFEATURES_XFER, 0, NULL);
1271 ide_set_xfer_rate(drive, (u8) arg);
1272 ide_driveid_update(drive);
1278 * ide_add_generic_settings - generic ide settings
1279 * @drive: drive being configured
1281 * Add the generic parts of the system settings to the /proc files and
1282 * ioctls for this IDE device. The caller must not be holding the
1286 void ide_add_generic_settings (ide_drive_t *drive)
1289 * drive setting name read/write access read ioctl write ioctl data type min max mul_factor div_factor data pointer set function
1291 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);
1292 ide_add_setting(drive, "keepsettings", SETTING_RW, HDIO_GET_KEEPSETTINGS, HDIO_SET_KEEPSETTINGS, TYPE_BYTE, 0, 1, 1, 1, &drive->keep_settings, NULL);
1293 ide_add_setting(drive, "nice1", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->nice1, NULL);
1294 ide_add_setting(drive, "pio_mode", SETTING_WRITE, -1, HDIO_SET_PIO_MODE, TYPE_BYTE, 0, 255, 1, 1, NULL, set_pio_mode);
1295 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);
1296 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);
1297 ide_add_setting(drive, "init_speed", SETTING_RW, -1, -1, TYPE_BYTE, 0, 70, 1, 1, &drive->init_speed, NULL);
1298 ide_add_setting(drive, "current_speed", SETTING_RW, -1, -1, TYPE_BYTE, 0, 70, 1, 1, &drive->current_speed, set_xfer_rate);
1299 ide_add_setting(drive, "number", SETTING_RW, -1, -1, TYPE_BYTE, 0, 3, 1, 1, &drive->dn, NULL);
1303 * system_bus_clock - clock guess
1305 * External version of the bus clock guess used by very old IDE drivers
1306 * for things like VLB timings. Should not be used.
1309 int system_bus_clock (void)
1311 return((int) ((!system_bus_speed) ? ide_system_bus_speed() : system_bus_speed ));
1314 EXPORT_SYMBOL(system_bus_clock);
1317 * Locking is badly broken here - since way back. That sucker is
1318 * root-only, but that's not an excuse... The real question is what
1319 * exclusion rules do we want here.
1321 int ide_replace_subdriver (ide_drive_t *drive, const char *driver)
1323 if (!drive->present || drive->usage || drive->dead)
1325 if (DRIVER(drive)->cleanup(drive))
1327 strlcpy(drive->driver_req, driver, sizeof(drive->driver_req));
1328 if (ata_attach(drive)) {
1329 spin_lock(&drives_lock);
1330 list_del_init(&drive->list);
1331 spin_unlock(&drives_lock);
1332 drive->driver_req[0] = 0;
1335 drive->driver_req[0] = 0;
1337 if (DRIVER(drive)!= &idedefault_driver && !strcmp(DRIVER(drive)->name, driver))
1344 * ata_attach - attach an ATA/ATAPI device
1345 * @drive: drive to attach
1347 * Takes a drive that is as yet not assigned to any midlayer IDE
1348 * driver (or is assigned to the default driver) and figures out
1349 * which driver would like to own it. If nobody claims the drive
1350 * then it is automatically attached to the default driver used for
1351 * unclaimed objects.
1353 * A return of zero indicates attachment to a driver, of one
1354 * attachment to the default driver.
1356 * Takes drivers_lock.
1359 int ata_attach(ide_drive_t *drive)
1361 struct list_head *p;
1362 spin_lock(&drivers_lock);
1363 list_for_each(p, &drivers) {
1364 ide_driver_t *driver = list_entry(p, ide_driver_t, drivers);
1365 if (!try_module_get(driver->owner))
1367 spin_unlock(&drivers_lock);
1368 if (driver->attach(drive) == 0) {
1369 module_put(driver->owner);
1370 drive->gendev.driver = &driver->gen_driver;
1373 spin_lock(&drivers_lock);
1374 module_put(driver->owner);
1376 drive->gendev.driver = &idedefault_driver.gen_driver;
1377 spin_unlock(&drivers_lock);
1378 if(idedefault_driver.attach(drive) != 0)
1379 panic("ide: default attach failed");
1383 static int generic_ide_suspend(struct device *dev, pm_message_t state)
1385 ide_drive_t *drive = dev->driver_data;
1387 struct request_pm_state rqpm;
1390 memset(&rq, 0, sizeof(rq));
1391 memset(&rqpm, 0, sizeof(rqpm));
1392 memset(&args, 0, sizeof(args));
1393 rq.flags = REQ_PM_SUSPEND;
1396 rqpm.pm_step = ide_pm_state_start_suspend;
1397 rqpm.pm_state = state;
1399 return ide_do_drive_cmd(drive, &rq, ide_wait);
1402 static int generic_ide_resume(struct device *dev)
1404 ide_drive_t *drive = dev->driver_data;
1406 struct request_pm_state rqpm;
1409 memset(&rq, 0, sizeof(rq));
1410 memset(&rqpm, 0, sizeof(rqpm));
1411 memset(&args, 0, sizeof(args));
1412 rq.flags = REQ_PM_RESUME;
1415 rqpm.pm_step = ide_pm_state_start_resume;
1418 return ide_do_drive_cmd(drive, &rq, ide_head_wait);
1421 int generic_ide_ioctl(struct file *file, struct block_device *bdev,
1422 unsigned int cmd, unsigned long arg)
1424 ide_drive_t *drive = bdev->bd_disk->private_data;
1425 ide_settings_t *setting;
1427 void __user *p = (void __user *)arg;
1429 down(&ide_setting_sem);
1430 if ((setting = ide_find_setting_by_ioctl(drive, cmd)) != NULL) {
1431 if (cmd == setting->read_ioctl) {
1432 err = ide_read_setting(drive, setting);
1433 up(&ide_setting_sem);
1434 return err >= 0 ? put_user(err, (long __user *)arg) : err;
1436 if (bdev != bdev->bd_contains)
1439 err = ide_write_setting(drive, setting, arg);
1440 up(&ide_setting_sem);
1444 up(&ide_setting_sem);
1449 struct hd_geometry geom;
1450 if (!p || (drive->media != ide_disk && drive->media != ide_floppy)) return -EINVAL;
1451 geom.heads = drive->bios_head;
1452 geom.sectors = drive->bios_sect;
1453 geom.cylinders = (u16)drive->bios_cyl; /* truncate */
1454 geom.start = get_start_sect(bdev);
1455 if (copy_to_user(p, &geom, sizeof(struct hd_geometry)))
1460 case HDIO_OBSOLETE_IDENTITY:
1461 case HDIO_GET_IDENTITY:
1462 if (bdev != bdev->bd_contains)
1464 if (drive->id_read == 0)
1466 if (copy_to_user(p, drive->id, (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
1471 return put_user(drive->dsc_overlap << IDE_NICE_DSC_OVERLAP |
1472 drive->atapi_overlap << IDE_NICE_ATAPI_OVERLAP |
1473 drive->nice0 << IDE_NICE_0 |
1474 drive->nice1 << IDE_NICE_1 |
1475 drive->nice2 << IDE_NICE_2,
1476 (long __user *) arg);
1478 #ifdef CONFIG_IDE_TASK_IOCTL
1479 case HDIO_DRIVE_TASKFILE:
1480 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1482 switch(drive->media) {
1484 return ide_taskfile_ioctl(drive, cmd, arg);
1488 #endif /* CONFIG_IDE_TASK_IOCTL */
1490 case HDIO_DRIVE_CMD:
1491 if (!capable(CAP_SYS_RAWIO))
1493 return ide_cmd_ioctl(drive, cmd, arg);
1495 case HDIO_DRIVE_TASK:
1496 if (!capable(CAP_SYS_RAWIO))
1498 return ide_task_ioctl(drive, cmd, arg);
1500 case HDIO_SCAN_HWIF:
1504 if (!capable(CAP_SYS_RAWIO)) return -EACCES;
1505 if (copy_from_user(args, p, 3 * sizeof(int)))
1507 memset(&hw, 0, sizeof(hw));
1508 ide_init_hwif_ports(&hw, (unsigned long) args[0],
1509 (unsigned long) args[1], NULL);
1511 if (ide_register_hw(&hw, NULL) == -1)
1515 case HDIO_UNREGISTER_HWIF:
1516 if (!capable(CAP_SYS_RAWIO)) return -EACCES;
1517 /* (arg > MAX_HWIFS) checked in function */
1518 ide_unregister(arg);
1521 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
1522 if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
1524 drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
1525 if (drive->dsc_overlap && !DRIVER(drive)->supports_dsc_overlap) {
1526 drive->dsc_overlap = 0;
1529 drive->nice1 = (arg >> IDE_NICE_1) & 1;
1531 case HDIO_DRIVE_RESET:
1533 unsigned long flags;
1534 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
1537 * Abort the current command on the
1538 * group if there is one, taking
1539 * care not to allow anything else
1540 * to be queued and to die on the
1541 * spot if we miss one somehow
1544 spin_lock_irqsave(&ide_lock, flags);
1546 ide_abort(drive, "drive reset");
1548 if(HWGROUP(drive)->handler)
1551 /* Ensure nothing gets queued after we
1552 drop the lock. Reset will clear the busy */
1554 HWGROUP(drive)->busy = 1;
1555 spin_unlock_irqrestore(&ide_lock, flags);
1556 (void) ide_do_reset(drive);
1562 case CDROMCLOSETRAY:
1563 return scsi_cmd_ioctl(file, bdev->bd_disk, cmd, p);
1565 case HDIO_GET_BUSSTATE:
1566 if (!capable(CAP_SYS_ADMIN))
1568 if (put_user(HWIF(drive)->bus_state, (long __user *)arg))
1572 case HDIO_SET_BUSSTATE:
1573 if (!capable(CAP_SYS_ADMIN))
1575 if (HWIF(drive)->busproc)
1576 return HWIF(drive)->busproc(drive, (int)arg);
1583 EXPORT_SYMBOL(generic_ide_ioctl);
1586 * stridx() returns the offset of c within s,
1587 * or -1 if c is '\0' or not found within s.
1589 static int __init stridx (const char *s, char c)
1591 char *i = strchr(s, c);
1592 return (i && c) ? i - s : -1;
1596 * match_parm() does parsing for ide_setup():
1598 * 1. the first char of s must be '='.
1599 * 2. if the remainder matches one of the supplied keywords,
1600 * the index (1 based) of the keyword is negated and returned.
1601 * 3. if the remainder is a series of no more than max_vals numbers
1602 * separated by commas, the numbers are saved in vals[] and a
1603 * count of how many were saved is returned. Base10 is assumed,
1604 * and base16 is allowed when prefixed with "0x".
1605 * 4. otherwise, zero is returned.
1607 static int __init match_parm (char *s, const char *keywords[], int vals[], int max_vals)
1609 static const char *decimal = "0123456789";
1610 static const char *hex = "0123456789abcdef";
1615 * Try matching against the supplied keywords,
1616 * and return -(index+1) if we match one
1618 if (keywords != NULL) {
1619 for (i = 0; *keywords != NULL; ++i) {
1620 if (!strcmp(s, *keywords++))
1625 * Look for a series of no more than "max_vals"
1626 * numeric values separated by commas, in base10,
1627 * or base16 when prefixed with "0x".
1628 * Return a count of how many were found.
1630 for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
1632 while ((i = stridx(decimal, *++s)) >= 0)
1633 vals[n] = (vals[n] * 10) + i;
1634 if (*s == 'x' && !vals[n]) {
1635 while ((i = stridx(hex, *++s)) >= 0)
1636 vals[n] = (vals[n] * 0x10) + i;
1638 if (++n == max_vals)
1640 if (*s == ',' || *s == ';')
1646 return 0; /* zero = nothing matched */
1649 #ifdef CONFIG_BLK_DEV_ALI14XX
1650 static int __initdata probe_ali14xx;
1651 extern int ali14xx_init(void);
1653 #ifdef CONFIG_BLK_DEV_UMC8672
1654 static int __initdata probe_umc8672;
1655 extern int umc8672_init(void);
1657 #ifdef CONFIG_BLK_DEV_DTC2278
1658 static int __initdata probe_dtc2278;
1659 extern int dtc2278_init(void);
1661 #ifdef CONFIG_BLK_DEV_HT6560B
1662 static int __initdata probe_ht6560b;
1663 extern int ht6560b_init(void);
1665 #ifdef CONFIG_BLK_DEV_QD65XX
1666 static int __initdata probe_qd65xx;
1667 extern int qd65xx_init(void);
1670 static int __initdata is_chipset_set[MAX_HWIFS];
1673 * ide_setup() gets called VERY EARLY during initialization,
1674 * to handle kernel "command line" strings beginning with "hdx=" or "ide".
1676 * Remember to update Documentation/ide.txt if you change something here.
1678 int __init ide_setup (char *s)
1683 unsigned int hw, unit;
1684 const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
1685 const char max_hwif = '0' + (MAX_HWIFS - 1);
1688 if (strncmp(s,"hd",2) == 0 && s[2] == '=') /* hd= is for hd.c */
1689 return 0; /* driver and not us */
1691 if (strncmp(s,"ide",3) && strncmp(s,"idebus",6) && strncmp(s,"hd",2))
1694 printk(KERN_INFO "ide_setup: %s", s);
1697 #ifdef CONFIG_BLK_DEV_IDEDOUBLER
1698 if (!strcmp(s, "ide=doubler")) {
1699 extern int ide_doubler;
1701 printk(" : Enabled support for IDE doublers\n");
1705 #endif /* CONFIG_BLK_DEV_IDEDOUBLER */
1707 if (!strcmp(s, "ide=nodma")) {
1708 printk(" : Prevented DMA\n");
1713 #ifdef CONFIG_BLK_DEV_IDEPCI
1714 if (!strcmp(s, "ide=reverse")) {
1715 ide_scan_direction = 1;
1716 printk(" : Enabled support for IDE inverse scan order.\n");
1719 #endif /* CONFIG_BLK_DEV_IDEPCI */
1722 * Look for drive options: "hdx="
1724 if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
1725 const char *hd_words[] = {
1726 "none", "noprobe", "nowerr", "cdrom", "serialize",
1727 "autotune", "noautotune", "minus8", "swapdata", "bswap",
1728 "minus11", "remap", "remap63", "scsi", NULL };
1730 hw = unit / MAX_DRIVES;
1731 unit = unit % MAX_DRIVES;
1732 hwif = &ide_hwifs[hw];
1733 drive = &hwif->drives[unit];
1734 if (strncmp(s + 4, "ide-", 4) == 0) {
1735 strlcpy(drive->driver_req, s + 4, sizeof(drive->driver_req));
1738 switch (match_parm(&s[3], hd_words, vals, 3)) {
1739 case -1: /* "none" */
1740 case -2: /* "noprobe" */
1743 case -3: /* "nowerr" */
1744 drive->bad_wstat = BAD_R_STAT;
1747 case -4: /* "cdrom" */
1749 drive->media = ide_cdrom;
1752 case -5: /* "serialize" */
1753 printk(" -- USE \"ide%d=serialize\" INSTEAD", hw);
1755 case -6: /* "autotune" */
1756 drive->autotune = IDE_TUNE_AUTO;
1757 goto obsolete_option;
1758 case -7: /* "noautotune" */
1759 drive->autotune = IDE_TUNE_NOAUTO;
1760 goto obsolete_option;
1761 case -9: /* "swapdata" */
1762 case -10: /* "bswap" */
1765 case -12: /* "remap" */
1766 drive->remap_0_to_1 = 1;
1768 case -13: /* "remap63" */
1771 case -14: /* "scsi" */
1774 case 3: /* cyl,head,sect */
1775 drive->media = ide_disk;
1776 drive->cyl = drive->bios_cyl = vals[0];
1777 drive->head = drive->bios_head = vals[1];
1778 drive->sect = drive->bios_sect = vals[2];
1780 drive->forced_geom = 1;
1788 if (s[0] != 'i' || s[1] != 'd' || s[2] != 'e')
1791 * Look for bus speed option: "idebus="
1793 if (s[3] == 'b' && s[4] == 'u' && s[5] == 's') {
1794 if (match_parm(&s[6], NULL, vals, 1) != 1)
1796 if (vals[0] >= 20 && vals[0] <= 66) {
1797 idebus_parameter = vals[0];
1799 printk(" -- BAD BUS SPEED! Expected value from 20 to 66");
1803 * Look for interface options: "idex="
1805 if (s[3] >= '0' && s[3] <= max_hwif) {
1807 * Be VERY CAREFUL changing this: note hardcoded indexes below
1808 * (-8, -9, -10) are reserved to ease the hardcoding.
1810 static const char *ide_words[] = {
1811 "noprobe", "serialize", "autotune", "noautotune",
1812 "reset", "dma", "ata66", "minus8", "minus9",
1813 "minus10", "four", "qd65xx", "ht6560b", "cmd640_vlb",
1814 "dtc2278", "umc8672", "ali14xx", NULL };
1816 hwif = &ide_hwifs[hw];
1817 i = match_parm(&s[4], ide_words, vals, 3);
1820 * Cryptic check to ensure chipset not already set for hwif.
1821 * Note: we can't depend on hwif->chipset here.
1823 if ((i >= -18 && i <= -11) || (i > 0 && i <= 3)) {
1824 /* chipset already specified */
1825 if (is_chipset_set[hw])
1827 if (i > -18 && i <= -11) {
1828 /* these drivers are for "ide0=" only */
1831 /* chipset already specified for 2nd port */
1832 if (is_chipset_set[hw+1])
1835 is_chipset_set[hw] = 1;
1840 #ifdef CONFIG_BLK_DEV_ALI14XX
1841 case -17: /* "ali14xx" */
1845 #ifdef CONFIG_BLK_DEV_UMC8672
1846 case -16: /* "umc8672" */
1850 #ifdef CONFIG_BLK_DEV_DTC2278
1851 case -15: /* "dtc2278" */
1855 #ifdef CONFIG_BLK_DEV_CMD640
1856 case -14: /* "cmd640_vlb" */
1858 extern int cmd640_vlb; /* flag for cmd640.c */
1863 #ifdef CONFIG_BLK_DEV_HT6560B
1864 case -13: /* "ht6560b" */
1868 #ifdef CONFIG_BLK_DEV_QD65XX
1869 case -12: /* "qd65xx" */
1873 #ifdef CONFIG_BLK_DEV_4DRIVES
1874 case -11: /* "four" drives on one set of ports */
1876 ide_hwif_t *mate = &ide_hwifs[hw^1];
1877 mate->drives[0].select.all ^= 0x20;
1878 mate->drives[1].select.all ^= 0x20;
1879 hwif->chipset = mate->chipset = ide_4drives;
1880 mate->irq = hwif->irq;
1881 memcpy(mate->io_ports, hwif->io_ports, sizeof(hwif->io_ports));
1884 #endif /* CONFIG_BLK_DEV_4DRIVES */
1885 case -10: /* minus10 */
1886 case -9: /* minus9 */
1887 case -8: /* minus8 */
1889 case -7: /* ata66 */
1890 #ifdef CONFIG_BLK_DEV_IDEPCI
1891 hwif->udma_four = 1;
1892 goto obsolete_option;
1898 goto obsolete_option;
1899 case -5: /* "reset" */
1901 goto obsolete_option;
1902 case -4: /* "noautotune" */
1903 hwif->drives[0].autotune = IDE_TUNE_NOAUTO;
1904 hwif->drives[1].autotune = IDE_TUNE_NOAUTO;
1905 goto obsolete_option;
1906 case -3: /* "autotune" */
1907 hwif->drives[0].autotune = IDE_TUNE_AUTO;
1908 hwif->drives[1].autotune = IDE_TUNE_AUTO;
1909 goto obsolete_option;
1910 case -2: /* "serialize" */
1912 hwif->mate = &ide_hwifs[hw^1];
1913 hwif->mate->mate = hwif;
1914 hwif->serialized = hwif->mate->serialized = 1;
1915 goto obsolete_option;
1917 case -1: /* "noprobe" */
1922 vals[1] = vals[0] + 0x206; /* default ctl */
1923 case 2: /* base,ctl */
1924 vals[2] = 0; /* default irq = probe for it */
1925 case 3: /* base,ctl,irq */
1926 hwif->hw.irq = vals[2];
1927 ide_init_hwif_ports(&hwif->hw, (unsigned long) vals[0], (unsigned long) vals[1], &hwif->irq);
1928 memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->io_ports));
1929 hwif->irq = vals[2];
1931 hwif->chipset = ide_forced;
1932 goto obsolete_option;
1934 case 0: goto bad_option;
1936 printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
1941 printk(" -- BAD OPTION\n");
1944 printk(" -- OBSOLETE OPTION, WILL BE REMOVED SOON!\n");
1947 printk("-- NOT SUPPORTED ON ide%d", hw);
1953 extern void pnpide_init(void);
1954 extern void h8300_ide_init(void);
1957 * probe_for_hwifs() finds/initializes "known" IDE interfaces
1959 static void __init probe_for_hwifs (void)
1961 #ifdef CONFIG_BLK_DEV_IDEPCI
1962 ide_scan_pcibus(ide_scan_direction);
1963 #endif /* CONFIG_BLK_DEV_IDEPCI */
1965 #ifdef CONFIG_ETRAX_IDE
1967 extern void init_e100_ide(void);
1970 #endif /* CONFIG_ETRAX_IDE */
1971 #ifdef CONFIG_BLK_DEV_CMD640
1973 extern void ide_probe_for_cmd640x(void);
1974 ide_probe_for_cmd640x();
1976 #endif /* CONFIG_BLK_DEV_CMD640 */
1977 #ifdef CONFIG_BLK_DEV_IDE_PMAC
1979 extern void pmac_ide_probe(void);
1982 #endif /* CONFIG_BLK_DEV_IDE_PMAC */
1983 #ifdef CONFIG_BLK_DEV_GAYLE
1985 extern void gayle_init(void);
1988 #endif /* CONFIG_BLK_DEV_GAYLE */
1989 #ifdef CONFIG_BLK_DEV_FALCON_IDE
1991 extern void falconide_init(void);
1994 #endif /* CONFIG_BLK_DEV_FALCON_IDE */
1995 #ifdef CONFIG_BLK_DEV_MAC_IDE
1997 extern void macide_init(void);
2000 #endif /* CONFIG_BLK_DEV_MAC_IDE */
2001 #ifdef CONFIG_BLK_DEV_Q40IDE
2003 extern void q40ide_init(void);
2006 #endif /* CONFIG_BLK_DEV_Q40IDE */
2007 #ifdef CONFIG_BLK_DEV_BUDDHA
2009 extern void buddha_init(void);
2012 #endif /* CONFIG_BLK_DEV_BUDDHA */
2013 #ifdef CONFIG_BLK_DEV_IDEPNP
2021 static ide_startstop_t default_do_request (ide_drive_t *drive, struct request *rq, sector_t block)
2023 ide_end_request(drive, 0, 0);
2027 static int default_end_request (ide_drive_t *drive, int uptodate, int nr_sects)
2029 return ide_end_request(drive, uptodate, nr_sects);
2032 static ide_startstop_t
2033 default_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
2035 return __ide_error(drive, rq, stat, err);
2038 static void default_pre_reset (ide_drive_t *drive)
2042 static sector_t default_capacity (ide_drive_t *drive)
2047 static ide_startstop_t default_special (ide_drive_t *drive)
2049 special_t *s = &drive->special;
2052 drive->mult_req = 0;
2056 static ide_startstop_t default_abort(ide_drive_t *drive, struct request *rq)
2058 return __ide_abort(drive, rq);
2061 static ide_startstop_t default_start_power_step(ide_drive_t *drive,
2064 rq->pm->pm_step = ide_pm_state_completed;
2068 static void setup_driver_defaults (ide_driver_t *d)
2070 BUG_ON(d->attach == NULL || d->cleanup == NULL);
2072 if (d->do_request == NULL) d->do_request = default_do_request;
2073 if (d->end_request == NULL) d->end_request = default_end_request;
2074 if (d->error == NULL) d->error = default_error;
2075 if (d->abort == NULL) d->abort = default_abort;
2076 if (d->pre_reset == NULL) d->pre_reset = default_pre_reset;
2077 if (d->capacity == NULL) d->capacity = default_capacity;
2078 if (d->special == NULL) d->special = default_special;
2079 if (d->start_power_step == NULL)
2080 d->start_power_step = default_start_power_step;
2083 int ide_register_subdriver(ide_drive_t *drive, ide_driver_t *driver)
2085 unsigned long flags;
2087 BUG_ON(!drive->driver);
2089 spin_lock_irqsave(&ide_lock, flags);
2090 if (!drive->present || drive->driver != &idedefault_driver ||
2091 drive->usage || drive->dead) {
2092 spin_unlock_irqrestore(&ide_lock, flags);
2095 drive->driver = driver;
2096 spin_unlock_irqrestore(&ide_lock, flags);
2097 spin_lock(&drives_lock);
2098 list_add_tail(&drive->list, &driver->drives);
2099 spin_unlock(&drives_lock);
2100 // printk(KERN_INFO "%s: attached %s driver.\n", drive->name, driver->name);
2101 if ((drive->autotune == IDE_TUNE_DEFAULT) ||
2102 (drive->autotune == IDE_TUNE_AUTO)) {
2103 /* DMA timings and setup moved to ide-probe.c */
2104 drive->dsc_overlap = (drive->next != drive && driver->supports_dsc_overlap);
2107 #ifdef CONFIG_PROC_FS
2108 if (drive->driver != &idedefault_driver) {
2109 ide_add_proc_entries(drive->proc, generic_subdriver_entries, drive);
2110 ide_add_proc_entries(drive->proc, driver->proc, drive);
2116 EXPORT_SYMBOL(ide_register_subdriver);
2119 * ide_unregister_subdriver - disconnect drive from driver
2120 * @drive: drive to unplug
2122 * Disconnect a drive from the driver it was attached to and then
2123 * clean up the various proc files and other objects attached to it.
2125 * Takes ide_setting_sem, ide_lock and drives_lock.
2126 * Caller must hold none of the locks.
2128 * No locking versus subdriver unload because we are moving to the
2129 * default driver anyway. Wants double checking.
2132 int ide_unregister_subdriver (ide_drive_t *drive)
2134 unsigned long flags;
2136 down(&ide_setting_sem);
2137 spin_lock_irqsave(&ide_lock, flags);
2138 if (drive->usage || drive->driver == &idedefault_driver || DRIVER(drive)->busy) {
2139 spin_unlock_irqrestore(&ide_lock, flags);
2140 up(&ide_setting_sem);
2143 #ifdef CONFIG_PROC_FS
2144 ide_remove_proc_entries(drive->proc, DRIVER(drive)->proc);
2145 ide_remove_proc_entries(drive->proc, generic_subdriver_entries);
2147 auto_remove_settings(drive);
2148 drive->driver = &idedefault_driver;
2149 spin_unlock_irqrestore(&ide_lock, flags);
2150 up(&ide_setting_sem);
2151 spin_lock(&drives_lock);
2152 list_del_init(&drive->list);
2153 spin_unlock(&drives_lock);
2154 /* drive will be added to &idedefault_driver->drives in ata_attach() */
2158 EXPORT_SYMBOL(ide_unregister_subdriver);
2160 static int ide_drive_remove(struct device * dev)
2162 ide_drive_t * drive = container_of(dev,ide_drive_t,gendev);
2163 DRIVER(drive)->cleanup(drive);
2168 * ide_register_driver - register IDE device driver
2169 * @driver: the IDE device driver
2171 * Register a new device driver and then scan the devices
2172 * on the IDE bus in case any should be attached to the
2173 * driver we have just registered. If so attach them.
2175 * Takes drivers_lock and drives_lock.
2178 int ide_register_driver(ide_driver_t *driver)
2180 struct list_head list;
2181 struct list_head *list_loop;
2182 struct list_head *tmp_storage;
2184 setup_driver_defaults(driver);
2186 spin_lock(&drivers_lock);
2187 list_add(&driver->drivers, &drivers);
2188 spin_unlock(&drivers_lock);
2190 INIT_LIST_HEAD(&list);
2191 spin_lock(&drives_lock);
2192 list_splice_init(&idedefault_driver.drives, &list);
2193 spin_unlock(&drives_lock);
2195 list_for_each_safe(list_loop, tmp_storage, &list) {
2196 ide_drive_t *drive = container_of(list_loop, ide_drive_t, list);
2197 list_del_init(&drive->list);
2201 driver->gen_driver.name = (char *) driver->name;
2202 driver->gen_driver.bus = &ide_bus_type;
2203 driver->gen_driver.remove = ide_drive_remove;
2204 return driver_register(&driver->gen_driver);
2207 EXPORT_SYMBOL(ide_register_driver);
2210 * ide_unregister_driver - unregister IDE device driver
2211 * @driver: the IDE device driver
2213 * Called when a driver module is being unloaded. We reattach any
2214 * devices to whatever driver claims them next (typically the default
2217 * Takes drivers_lock and called functions will take ide_setting_sem.
2220 void ide_unregister_driver(ide_driver_t *driver)
2224 spin_lock(&drivers_lock);
2225 list_del(&driver->drivers);
2226 spin_unlock(&drivers_lock);
2228 driver_unregister(&driver->gen_driver);
2230 while(!list_empty(&driver->drives)) {
2231 drive = list_entry(driver->drives.next, ide_drive_t, list);
2232 if (driver->cleanup(drive)) {
2233 printk(KERN_ERR "%s: cleanup_module() called while still busy\n", drive->name);
2240 EXPORT_SYMBOL(ide_unregister_driver);
2242 struct block_device_operations ide_fops[] = {{
2243 .owner = THIS_MODULE,
2247 EXPORT_SYMBOL(ide_fops);
2253 EXPORT_SYMBOL(ide_lock);
2255 struct bus_type ide_bus_type = {
2257 .suspend = generic_ide_suspend,
2258 .resume = generic_ide_resume,
2262 * This is gets invoked once during initialization, to set *everything* up
2264 int __init ide_init (void)
2266 printk(KERN_INFO "Uniform Multi-Platform E-IDE driver " REVISION "\n");
2267 devfs_mk_dir("ide");
2268 system_bus_speed = ide_system_bus_speed();
2270 bus_register(&ide_bus_type);
2274 #ifdef CONFIG_PROC_FS
2275 proc_ide_root = proc_mkdir("ide", NULL);
2278 #ifdef CONFIG_BLK_DEV_ALI14XX
2280 (void)ali14xx_init();
2282 #ifdef CONFIG_BLK_DEV_UMC8672
2284 (void)umc8672_init();
2286 #ifdef CONFIG_BLK_DEV_DTC2278
2288 (void)dtc2278_init();
2290 #ifdef CONFIG_BLK_DEV_HT6560B
2292 (void)ht6560b_init();
2294 #ifdef CONFIG_BLK_DEV_QD65XX
2296 (void)qd65xx_init();
2300 /* Probe for special PCI and other "known" interface chipsets. */
2304 #ifdef CONFIG_PROC_FS
2311 char *options = NULL;
2312 module_param(options, charp, 0);
2313 MODULE_LICENSE("GPL");
2315 static void __init parse_options (char *line)
2319 if (line == NULL || !*line)
2321 while ((line = next) != NULL) {
2322 if ((next = strchr(line,' ')) != NULL)
2324 if (!ide_setup(line))
2325 printk (KERN_INFO "Unknown option '%s'\n", line);
2329 int init_module (void)
2331 parse_options(options);
2335 void cleanup_module (void)
2339 for (index = 0; index < MAX_HWIFS; ++index)
2340 ide_unregister(index);
2342 #ifdef CONFIG_PROC_FS
2345 devfs_remove("ide");
2347 bus_unregister(&ide_bus_type);
2352 __setup("", ide_setup);
2354 module_init(ide_init);