2 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
3 * Copyrifht (C) 2003-2005, 2007 Bartlomiej Zolnierkiewicz
7 * Mostly written by Mark Lord <mlord@pobox.com>
8 * and Gadi Oxman <gadio@netvision.net.il>
9 * and Andre Hedrick <andre@linux-ide.org>
11 * See linux/MAINTAINERS for address of current maintainer.
13 * This is the multiple IDE interface driver, as evolved from hd.c.
14 * It supports up to MAX_HWIFS IDE interfaces, on one or more IRQs
16 * There can be up to two drives per interface, as per the ATA-2 spec.
22 * | It traverses the request-list, using interrupts to jump between functions.
23 * | As nearly all functions can be called within interrupts, we may not sleep.
24 * | Special care is recommended. Have Fun!
26 * | modified by Drew Eckhardt to check nr of hd's from the CMOS.
28 * | Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
29 * | in the early extended-partition checks and added DM partitions.
31 * | Early work on error handling by Mika Liljeberg (liljeber@cs.Helsinki.FI).
33 * | IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
34 * | and general streamlining by Mark Lord (mlord@pobox.com).
36 * October, 1994 -- Complete line-by-line overhaul for linux 1.1.x, by:
38 * Mark Lord (mlord@pobox.com) (IDE Perf.Pkg)
39 * Delman Lee (delman@ieee.org) ("Mr. atdisk2")
40 * Scott Snyder (snyder@fnald0.fnal.gov) (ATAPI IDE cd-rom)
42 * This was a rewrite of just about everything from hd.c, though some original
43 * code is still sprinkled about. Think of it as a major evolution, with
44 * inspiration from lots of linux users, esp. hamish@zot.apana.org.au
47 #define _IDE_C /* Tell ide.h it's really us */
49 #include <linux/module.h>
50 #include <linux/types.h>
51 #include <linux/string.h>
52 #include <linux/kernel.h>
53 #include <linux/timer.h>
55 #include <linux/interrupt.h>
56 #include <linux/major.h>
57 #include <linux/errno.h>
58 #include <linux/genhd.h>
59 #include <linux/blkpg.h>
60 #include <linux/slab.h>
61 #include <linux/init.h>
62 #include <linux/pci.h>
63 #include <linux/delay.h>
64 #include <linux/ide.h>
65 #include <linux/completion.h>
66 #include <linux/reboot.h>
67 #include <linux/cdrom.h>
68 #include <linux/seq_file.h>
69 #include <linux/device.h>
70 #include <linux/bitops.h>
72 #include <asm/byteorder.h>
74 #include <asm/uaccess.h>
78 /* default maximum number of failures */
79 #define IDE_DEFAULT_MAX_FAILURES 1
81 struct class *ide_port_class;
83 static const u8 ide_hwif_to_major[] = { IDE0_MAJOR, IDE1_MAJOR,
84 IDE2_MAJOR, IDE3_MAJOR,
85 IDE4_MAJOR, IDE5_MAJOR,
86 IDE6_MAJOR, IDE7_MAJOR,
87 IDE8_MAJOR, IDE9_MAJOR };
89 static int idebus_parameter; /* holds the "idebus=" parameter */
90 static int system_bus_speed; /* holds what we think is VESA/PCI bus speed */
92 DEFINE_MUTEX(ide_cfg_mtx);
93 __cacheline_aligned_in_smp DEFINE_SPINLOCK(ide_lock);
97 #ifdef CONFIG_BLK_DEV_IDEACPI
99 int ide_noacpitfs = 1;
100 int ide_noacpionboot = 1;
104 * This is declared extern in ide.h, for access by other IDE modules:
106 ide_hwif_t ide_hwifs[MAX_HWIFS]; /* master data repository */
108 EXPORT_SYMBOL(ide_hwifs);
110 static void ide_port_init_devices_data(ide_hwif_t *);
113 * Do not even *think* about calling this!
115 void ide_init_port_data(ide_hwif_t *hwif, unsigned int index)
117 /* bulk initialize hwif & drive info with zeros */
118 memset(hwif, 0, sizeof(ide_hwif_t));
120 /* fill in any non-zero initial values */
122 hwif->major = ide_hwif_to_major[index];
127 hwif->name[3] = '0' + index;
129 hwif->bus_state = BUSSTATE_ON;
131 init_completion(&hwif->gendev_rel_comp);
133 default_hwif_iops(hwif);
134 default_hwif_transport(hwif);
136 ide_port_init_devices_data(hwif);
138 EXPORT_SYMBOL_GPL(ide_init_port_data);
140 static void ide_port_init_devices_data(ide_hwif_t *hwif)
144 for (unit = 0; unit < MAX_DRIVES; ++unit) {
145 ide_drive_t *drive = &hwif->drives[unit];
146 u8 j = (hwif->index * MAX_DRIVES) + unit;
148 memset(drive, 0, sizeof(*drive));
150 drive->media = ide_disk;
151 drive->select.all = (unit<<4)|0xa0;
154 drive->ready_stat = READY_STAT;
155 drive->bad_wstat = BAD_W_STAT;
156 drive->special.b.recalibrate = 1;
157 drive->special.b.set_geometry = 1;
158 drive->name[0] = 'h';
159 drive->name[1] = 'd';
160 drive->name[2] = 'a' + j;
161 drive->max_failures = IDE_DEFAULT_MAX_FAILURES;
163 INIT_LIST_HEAD(&drive->list);
164 init_completion(&drive->gendev_rel_comp);
169 * init_ide_data() sets reasonable default values into all fields
170 * of all instances of the hwifs and drives, but only on the first call.
171 * Subsequent calls have no effect (they don't wipe out anything).
173 * This routine is normally called at driver initialization time,
174 * but may also be called MUCH earlier during kernel "command-line"
175 * parameter processing. As such, we cannot depend on any other parts
176 * of the kernel (such as memory allocation) to be functioning yet.
178 * This is too bad, as otherwise we could dynamically allocate the
179 * ide_drive_t structs as needed, rather than always consuming memory
180 * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
182 * FIXME: We should stuff the setup data into __init and copy the
183 * relevant hwifs/allocate them properly during boot.
185 #define MAGIC_COOKIE 0x12345678
186 static void __init init_ide_data (void)
189 static unsigned long magic_cookie = MAGIC_COOKIE;
191 if (magic_cookie != MAGIC_COOKIE)
192 return; /* already initialized */
195 /* Initialise all interface structures */
196 for (index = 0; index < MAX_HWIFS; ++index) {
197 ide_hwif_t *hwif = &ide_hwifs[index];
199 ide_init_port_data(hwif, index);
204 * ide_system_bus_speed - guess bus speed
206 * ide_system_bus_speed() returns what we think is the system VESA/PCI
207 * bus speed (in MHz). This is used for calculating interface PIO timings.
208 * The default is 40 for known PCI systems, 50 otherwise.
209 * The "idebus=xx" parameter can be used to override this value.
210 * The actual value to be used is computed/displayed the first time
211 * through. Drivers should only use this as a last resort.
213 * Returns a guessed speed in MHz.
216 static int ide_system_bus_speed(void)
219 static struct pci_device_id pci_default[] = {
220 { PCI_DEVICE(PCI_ANY_ID, PCI_ANY_ID) },
224 #define pci_default 0
225 #endif /* CONFIG_PCI */
227 /* user supplied value */
228 if (idebus_parameter)
229 return idebus_parameter;
231 /* safe default value for PCI or VESA and PCI*/
232 return pci_dev_present(pci_default) ? 33 : 50;
235 ide_hwif_t *ide_find_port(void)
240 for (i = 0; i < MAX_HWIFS; i++) {
241 hwif = &ide_hwifs[i];
242 if (hwif->chipset == ide_unknown)
248 EXPORT_SYMBOL_GPL(ide_find_port);
250 static struct resource* hwif_request_region(ide_hwif_t *hwif,
251 unsigned long addr, int num)
253 struct resource *res = request_region(addr, num, hwif->name);
256 printk(KERN_ERR "%s: I/O resource 0x%lX-0x%lX not free.\n",
257 hwif->name, addr, addr+num-1);
262 * ide_hwif_request_regions - request resources for IDE
263 * @hwif: interface to use
265 * Requests all the needed resources for an interface.
266 * Right now core IDE code does this work which is deeply wrong.
267 * MMIO leaves it to the controller driver,
268 * PIO will migrate this way over time.
271 int ide_hwif_request_regions(ide_hwif_t *hwif)
278 addr = hwif->io_ports[IDE_CONTROL_OFFSET];
279 if (addr && !hwif_request_region(hwif, addr, 1))
280 goto control_region_busy;
282 addr = hwif->io_ports[IDE_DATA_OFFSET];
283 if ((addr | 7) == hwif->io_ports[IDE_STATUS_OFFSET]) {
284 if (!hwif_request_region(hwif, addr, 8))
285 goto data_region_busy;
289 for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++) {
290 addr = hwif->io_ports[i];
291 if (!hwif_request_region(hwif, addr, 1)) {
293 release_region(addr, 1);
294 goto data_region_busy;
300 addr = hwif->io_ports[IDE_CONTROL_OFFSET];
302 release_region(addr, 1);
304 /* If any errors are return, we drop the hwif interface. */
309 * ide_hwif_release_regions - free IDE resources
311 * Note that we only release the standard ports,
312 * and do not even try to handle any extra ports
313 * allocated for weird IDE interface chipsets.
315 * Note also that we don't yet handle mmio resources here. More
316 * importantly our caller should be doing this so we need to
317 * restructure this as a helper function for drivers.
320 void ide_hwif_release_regions(ide_hwif_t *hwif)
326 if (hwif->io_ports[IDE_CONTROL_OFFSET])
327 release_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
328 if (hwif->straight8) {
329 release_region(hwif->io_ports[IDE_DATA_OFFSET], 8);
332 for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++)
333 if (hwif->io_ports[i])
334 release_region(hwif->io_ports[i], 1);
337 void ide_remove_port_from_hwgroup(ide_hwif_t *hwif)
339 ide_hwgroup_t *hwgroup = hwif->hwgroup;
341 spin_lock_irq(&ide_lock);
343 * Remove us from the hwgroup, and free
344 * the hwgroup if we were the only member
346 if (hwif->next == hwif) {
347 BUG_ON(hwgroup->hwif != hwif);
350 /* There is another interface in hwgroup.
351 * Unlink us, and set hwgroup->drive and ->hwif to
354 ide_hwif_t *g = hwgroup->hwif;
356 while (g->next != hwif)
358 g->next = hwif->next;
359 if (hwgroup->hwif == hwif) {
360 /* Chose a random hwif for hwgroup->hwif.
361 * It's guaranteed that there are no drives
362 * left in the hwgroup.
364 BUG_ON(hwgroup->drive != NULL);
367 BUG_ON(hwgroup->hwif == hwif);
369 spin_unlock_irq(&ide_lock);
372 /* Called with ide_lock held. */
373 static void __ide_port_unregister_devices(ide_hwif_t *hwif)
377 for (i = 0; i < MAX_DRIVES; i++) {
378 ide_drive_t *drive = &hwif->drives[i];
380 if (drive->present) {
381 spin_unlock_irq(&ide_lock);
382 device_unregister(&drive->gendev);
383 wait_for_completion(&drive->gendev_rel_comp);
384 spin_lock_irq(&ide_lock);
389 void ide_port_unregister_devices(ide_hwif_t *hwif)
391 mutex_lock(&ide_cfg_mtx);
392 spin_lock_irq(&ide_lock);
393 __ide_port_unregister_devices(hwif);
395 ide_port_init_devices_data(hwif);
396 spin_unlock_irq(&ide_lock);
397 mutex_unlock(&ide_cfg_mtx);
399 EXPORT_SYMBOL_GPL(ide_port_unregister_devices);
402 * ide_unregister - free an IDE interface
403 * @index: index of interface (will change soon to a pointer)
405 * Perform the final unregister of an IDE interface. At the moment
406 * we don't refcount interfaces so this will also get split up.
409 * The caller must not hold the IDE locks
410 * The drive present/vanishing is not yet properly locked
411 * Take care with the callbacks. These have been split to avoid
412 * deadlocking the IDE layer. The shutdown callback is called
413 * before we take the lock and free resources. It is up to the
414 * caller to be sure there is no pending I/O here, and that
415 * the interface will not be reopened (present/vanishing locking
416 * isn't yet done BTW). After we commit to the final kill we
417 * call the cleanup callback with the ide locks held.
419 * Unregister restores the hwif structures to the default state.
420 * This is raving bonkers.
423 void ide_unregister(unsigned int index)
425 ide_hwif_t *hwif, *g;
426 ide_hwgroup_t *hwgroup;
429 BUG_ON(index >= MAX_HWIFS);
431 BUG_ON(in_interrupt());
432 BUG_ON(irqs_disabled());
433 mutex_lock(&ide_cfg_mtx);
434 spin_lock_irq(&ide_lock);
435 hwif = &ide_hwifs[index];
438 __ide_port_unregister_devices(hwif);
441 spin_unlock_irq(&ide_lock);
443 ide_proc_unregister_port(hwif);
445 hwgroup = hwif->hwgroup;
447 * free the irq if we were the only hwif using it
451 if (g->irq == hwif->irq)
454 } while (g != hwgroup->hwif);
456 free_irq(hwif->irq, hwgroup);
458 ide_remove_port_from_hwgroup(hwif);
460 device_unregister(hwif->portdev);
461 device_unregister(&hwif->gendev);
462 wait_for_completion(&hwif->gendev_rel_comp);
465 * Remove us from the kernel's knowledge
467 blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
468 kfree(hwif->sg_table);
469 unregister_blkdev(hwif->major, hwif->name);
470 spin_lock_irq(&ide_lock);
473 (void)ide_release_dma(hwif);
475 ide_hwif_release_regions(hwif);
477 /* restore hwif data to pristine status */
478 ide_init_port_data(hwif, index);
481 spin_unlock_irq(&ide_lock);
482 mutex_unlock(&ide_cfg_mtx);
485 EXPORT_SYMBOL(ide_unregister);
487 void ide_init_port_hw(ide_hwif_t *hwif, hw_regs_t *hw)
489 memcpy(hwif->io_ports, hw->io_ports, sizeof(hwif->io_ports));
492 hwif->chipset = hw->chipset;
493 hwif->gendev.parent = hw->dev;
494 hwif->ack_intr = hw->ack_intr;
496 EXPORT_SYMBOL_GPL(ide_init_port_hw);
499 * Locks for IDE setting functionality
502 DEFINE_MUTEX(ide_setting_mtx);
504 EXPORT_SYMBOL_GPL(ide_setting_mtx);
507 * ide_spin_wait_hwgroup - wait for group
508 * @drive: drive in the group
510 * Wait for an IDE device group to go non busy and then return
511 * holding the ide_lock which guards the hwgroup->busy status
512 * and right to use it.
515 int ide_spin_wait_hwgroup (ide_drive_t *drive)
517 ide_hwgroup_t *hwgroup = HWGROUP(drive);
518 unsigned long timeout = jiffies + (3 * HZ);
520 spin_lock_irq(&ide_lock);
522 while (hwgroup->busy) {
523 unsigned long lflags;
524 spin_unlock_irq(&ide_lock);
525 local_irq_set(lflags);
526 if (time_after(jiffies, timeout)) {
527 local_irq_restore(lflags);
528 printk(KERN_ERR "%s: channel busy\n", drive->name);
531 local_irq_restore(lflags);
532 spin_lock_irq(&ide_lock);
537 EXPORT_SYMBOL(ide_spin_wait_hwgroup);
539 int set_io_32bit(ide_drive_t *drive, int arg)
541 if (drive->no_io_32bit)
544 if (arg < 0 || arg > 1 + (SUPPORT_VLB_SYNC << 1))
547 if (ide_spin_wait_hwgroup(drive))
550 drive->io_32bit = arg;
552 spin_unlock_irq(&ide_lock);
557 static int set_ksettings(ide_drive_t *drive, int arg)
559 if (arg < 0 || arg > 1)
562 if (ide_spin_wait_hwgroup(drive))
564 drive->keep_settings = arg;
565 spin_unlock_irq(&ide_lock);
570 int set_using_dma(ide_drive_t *drive, int arg)
572 #ifdef CONFIG_BLK_DEV_IDEDMA
573 ide_hwif_t *hwif = drive->hwif;
576 if (arg < 0 || arg > 1)
579 if (!drive->id || !(drive->id->capability & 1))
582 if (hwif->dma_host_set == NULL)
586 if (ide_spin_wait_hwgroup(drive))
589 * set ->busy flag, unlock and let it ride
591 hwif->hwgroup->busy = 1;
592 spin_unlock_irq(&ide_lock);
597 if (ide_set_dma(drive))
603 * lock, clear ->busy flag and unlock before leaving
605 spin_lock_irq(&ide_lock);
606 hwif->hwgroup->busy = 0;
607 spin_unlock_irq(&ide_lock);
611 if (arg < 0 || arg > 1)
618 int set_pio_mode(ide_drive_t *drive, int arg)
622 if (arg < 0 || arg > 255)
625 if (drive->hwif->set_pio_mode == NULL)
628 if (drive->special.b.set_tune)
631 ide_init_drive_cmd(&rq);
632 rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
634 drive->tune_req = (u8) arg;
635 drive->special.b.set_tune = 1;
636 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
640 static int set_unmaskirq(ide_drive_t *drive, int arg)
642 if (drive->no_unmask)
645 if (arg < 0 || arg > 1)
648 if (ide_spin_wait_hwgroup(drive))
651 spin_unlock_irq(&ide_lock);
657 * system_bus_clock - clock guess
659 * External version of the bus clock guess used by very old IDE drivers
660 * for things like VLB timings. Should not be used.
663 int system_bus_clock (void)
665 return system_bus_speed;
668 EXPORT_SYMBOL(system_bus_clock);
670 static int generic_ide_suspend(struct device *dev, pm_message_t mesg)
672 ide_drive_t *drive = dev->driver_data;
673 ide_hwif_t *hwif = HWIF(drive);
675 struct request_pm_state rqpm;
679 /* Call ACPI _GTM only once */
680 if (!(drive->dn % 2))
681 ide_acpi_get_timing(hwif);
683 memset(&rq, 0, sizeof(rq));
684 memset(&rqpm, 0, sizeof(rqpm));
685 memset(&args, 0, sizeof(args));
686 rq.cmd_type = REQ_TYPE_PM_SUSPEND;
689 rqpm.pm_step = ide_pm_state_start_suspend;
690 if (mesg.event == PM_EVENT_PRETHAW)
691 mesg.event = PM_EVENT_FREEZE;
692 rqpm.pm_state = mesg.event;
694 ret = ide_do_drive_cmd(drive, &rq, ide_wait);
695 /* only call ACPI _PS3 after both drivers are suspended */
696 if (!ret && (((drive->dn % 2) && hwif->drives[0].present
697 && hwif->drives[1].present)
698 || !hwif->drives[0].present
699 || !hwif->drives[1].present))
700 ide_acpi_set_state(hwif, 0);
704 static int generic_ide_resume(struct device *dev)
706 ide_drive_t *drive = dev->driver_data;
707 ide_hwif_t *hwif = HWIF(drive);
709 struct request_pm_state rqpm;
713 /* Call ACPI _STM only once */
714 if (!(drive->dn % 2)) {
715 ide_acpi_set_state(hwif, 1);
716 ide_acpi_push_timing(hwif);
719 ide_acpi_exec_tfs(drive);
721 memset(&rq, 0, sizeof(rq));
722 memset(&rqpm, 0, sizeof(rqpm));
723 memset(&args, 0, sizeof(args));
724 rq.cmd_type = REQ_TYPE_PM_RESUME;
727 rqpm.pm_step = ide_pm_state_start_resume;
728 rqpm.pm_state = PM_EVENT_ON;
730 err = ide_do_drive_cmd(drive, &rq, ide_head_wait);
732 if (err == 0 && dev->driver) {
733 ide_driver_t *drv = to_ide_driver(dev->driver);
742 int generic_ide_ioctl(ide_drive_t *drive, struct file *file, struct block_device *bdev,
743 unsigned int cmd, unsigned long arg)
747 void __user *p = (void __user *)arg;
748 int err = 0, (*setfunc)(ide_drive_t *, int);
752 case HDIO_GET_32BIT: val = &drive->io_32bit; goto read_val;
753 case HDIO_GET_KEEPSETTINGS: val = &drive->keep_settings; goto read_val;
754 case HDIO_GET_UNMASKINTR: val = &drive->unmask; goto read_val;
755 case HDIO_GET_DMA: val = &drive->using_dma; goto read_val;
756 case HDIO_SET_32BIT: setfunc = set_io_32bit; goto set_val;
757 case HDIO_SET_KEEPSETTINGS: setfunc = set_ksettings; goto set_val;
758 case HDIO_SET_PIO_MODE: setfunc = set_pio_mode; goto set_val;
759 case HDIO_SET_UNMASKINTR: setfunc = set_unmaskirq; goto set_val;
760 case HDIO_SET_DMA: setfunc = set_using_dma; goto set_val;
764 case HDIO_OBSOLETE_IDENTITY:
765 case HDIO_GET_IDENTITY:
766 if (bdev != bdev->bd_contains)
768 if (drive->id_read == 0)
770 if (copy_to_user(p, drive->id, (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
775 return put_user(drive->dsc_overlap << IDE_NICE_DSC_OVERLAP |
776 drive->atapi_overlap << IDE_NICE_ATAPI_OVERLAP |
777 drive->nice1 << IDE_NICE_1,
778 (long __user *) arg);
779 #ifdef CONFIG_IDE_TASK_IOCTL
780 case HDIO_DRIVE_TASKFILE:
781 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
783 switch(drive->media) {
785 return ide_taskfile_ioctl(drive, cmd, arg);
789 #endif /* CONFIG_IDE_TASK_IOCTL */
792 if (!capable(CAP_SYS_RAWIO))
794 return ide_cmd_ioctl(drive, cmd, arg);
796 case HDIO_DRIVE_TASK:
797 if (!capable(CAP_SYS_RAWIO))
799 return ide_task_ioctl(drive, cmd, arg);
801 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
802 if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
804 drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
805 drv = *(ide_driver_t **)bdev->bd_disk->private_data;
806 if (drive->dsc_overlap && !drv->supports_dsc_overlap) {
807 drive->dsc_overlap = 0;
810 drive->nice1 = (arg >> IDE_NICE_1) & 1;
812 case HDIO_DRIVE_RESET:
813 if (!capable(CAP_SYS_ADMIN))
817 * Abort the current command on the
818 * group if there is one, taking
819 * care not to allow anything else
820 * to be queued and to die on the
821 * spot if we miss one somehow
824 spin_lock_irqsave(&ide_lock, flags);
826 if (HWGROUP(drive)->resetting) {
827 spin_unlock_irqrestore(&ide_lock, flags);
831 ide_abort(drive, "drive reset");
833 BUG_ON(HWGROUP(drive)->handler);
835 /* Ensure nothing gets queued after we
836 drop the lock. Reset will clear the busy */
838 HWGROUP(drive)->busy = 1;
839 spin_unlock_irqrestore(&ide_lock, flags);
840 (void) ide_do_reset(drive);
843 case HDIO_GET_BUSSTATE:
844 if (!capable(CAP_SYS_ADMIN))
846 if (put_user(HWIF(drive)->bus_state, (long __user *)arg))
850 case HDIO_SET_BUSSTATE:
851 if (!capable(CAP_SYS_ADMIN))
859 mutex_lock(&ide_setting_mtx);
860 spin_lock_irqsave(&ide_lock, flags);
862 spin_unlock_irqrestore(&ide_lock, flags);
863 mutex_unlock(&ide_setting_mtx);
864 return err >= 0 ? put_user(err, (long __user *)arg) : err;
867 if (bdev != bdev->bd_contains)
870 if (!capable(CAP_SYS_ADMIN))
873 mutex_lock(&ide_setting_mtx);
874 err = setfunc(drive, arg);
875 mutex_unlock(&ide_setting_mtx);
881 EXPORT_SYMBOL(generic_ide_ioctl);
884 * stridx() returns the offset of c within s,
885 * or -1 if c is '\0' or not found within s.
887 static int __init stridx (const char *s, char c)
889 char *i = strchr(s, c);
890 return (i && c) ? i - s : -1;
894 * match_parm() does parsing for ide_setup():
896 * 1. the first char of s must be '='.
897 * 2. if the remainder matches one of the supplied keywords,
898 * the index (1 based) of the keyword is negated and returned.
899 * 3. if the remainder is a series of no more than max_vals numbers
900 * separated by commas, the numbers are saved in vals[] and a
901 * count of how many were saved is returned. Base10 is assumed,
902 * and base16 is allowed when prefixed with "0x".
903 * 4. otherwise, zero is returned.
905 static int __init match_parm (char *s, const char *keywords[], int vals[], int max_vals)
907 static const char *decimal = "0123456789";
908 static const char *hex = "0123456789abcdef";
913 * Try matching against the supplied keywords,
914 * and return -(index+1) if we match one
916 if (keywords != NULL) {
917 for (i = 0; *keywords != NULL; ++i) {
918 if (!strcmp(s, *keywords++))
923 * Look for a series of no more than "max_vals"
924 * numeric values separated by commas, in base10,
925 * or base16 when prefixed with "0x".
926 * Return a count of how many were found.
928 for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
930 while ((i = stridx(decimal, *++s)) >= 0)
931 vals[n] = (vals[n] * 10) + i;
932 if (*s == 'x' && !vals[n]) {
933 while ((i = stridx(hex, *++s)) >= 0)
934 vals[n] = (vals[n] * 0x10) + i;
938 if (*s == ',' || *s == ';')
944 return 0; /* zero = nothing matched */
947 extern int probe_ali14xx;
948 extern int probe_umc8672;
949 extern int probe_dtc2278;
950 extern int probe_ht6560b;
951 extern int probe_qd65xx;
952 extern int cmd640_vlb;
953 extern int probe_4drives;
955 static int __initdata is_chipset_set;
958 * ide_setup() gets called VERY EARLY during initialization,
959 * to handle kernel "command line" strings beginning with "hdx=" or "ide".
961 * Remember to update Documentation/ide/ide.txt if you change something here.
963 static int __init ide_setup(char *s)
968 unsigned int hw, unit;
969 const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
970 const char max_hwif = '0' + (MAX_HWIFS - 1);
973 if (strncmp(s,"hd",2) == 0 && s[2] == '=') /* hd= is for hd.c */
974 return 0; /* driver and not us */
976 if (strncmp(s,"ide",3) && strncmp(s,"idebus",6) && strncmp(s,"hd",2))
979 printk(KERN_INFO "ide_setup: %s", s);
982 #ifdef CONFIG_BLK_DEV_IDEDOUBLER
983 if (!strcmp(s, "ide=doubler")) {
984 extern int ide_doubler;
986 printk(" : Enabled support for IDE doublers\n");
990 #endif /* CONFIG_BLK_DEV_IDEDOUBLER */
992 if (!strcmp(s, "ide=nodma")) {
993 printk(" : Prevented DMA\n");
995 goto obsolete_option;
998 #ifdef CONFIG_BLK_DEV_IDEACPI
999 if (!strcmp(s, "ide=noacpi")) {
1000 //printk(" : Disable IDE ACPI support.\n");
1004 if (!strcmp(s, "ide=acpigtf")) {
1005 //printk(" : Enable IDE ACPI _GTF support.\n");
1009 if (!strcmp(s, "ide=acpionboot")) {
1010 //printk(" : Call IDE ACPI methods on boot.\n");
1011 ide_noacpionboot = 0;
1014 #endif /* CONFIG_BLK_DEV_IDEACPI */
1017 * Look for drive options: "hdx="
1019 if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
1020 const char *hd_words[] = {
1021 "none", "noprobe", "nowerr", "cdrom", "nodma",
1022 "autotune", "noautotune", "-8", "-9", "-10",
1023 "noflush", "remap", "remap63", "scsi", NULL };
1025 hw = unit / MAX_DRIVES;
1026 unit = unit % MAX_DRIVES;
1027 hwif = &ide_hwifs[hw];
1028 drive = &hwif->drives[unit];
1029 if (strncmp(s + 4, "ide-", 4) == 0) {
1030 strlcpy(drive->driver_req, s + 4, sizeof(drive->driver_req));
1031 goto obsolete_option;
1033 switch (match_parm(&s[3], hd_words, vals, 3)) {
1034 case -1: /* "none" */
1035 case -2: /* "noprobe" */
1038 case -3: /* "nowerr" */
1039 drive->bad_wstat = BAD_R_STAT;
1042 case -4: /* "cdrom" */
1044 drive->media = ide_cdrom;
1045 /* an ATAPI device ignores DRDY */
1046 drive->ready_stat = 0;
1049 case -5: /* nodma */
1052 case -6: /* "autotune" */
1053 drive->autotune = IDE_TUNE_AUTO;
1054 goto obsolete_option;
1055 case -7: /* "noautotune" */
1056 drive->autotune = IDE_TUNE_NOAUTO;
1057 goto obsolete_option;
1058 case -11: /* noflush */
1061 case -12: /* "remap" */
1062 drive->remap_0_to_1 = 1;
1063 goto obsolete_option;
1064 case -13: /* "remap63" */
1066 goto obsolete_option;
1067 case -14: /* "scsi" */
1069 goto obsolete_option;
1070 case 3: /* cyl,head,sect */
1071 drive->media = ide_disk;
1072 drive->ready_stat = READY_STAT;
1073 drive->cyl = drive->bios_cyl = vals[0];
1074 drive->head = drive->bios_head = vals[1];
1075 drive->sect = drive->bios_sect = vals[2];
1077 drive->forced_geom = 1;
1085 if (s[0] != 'i' || s[1] != 'd' || s[2] != 'e')
1088 * Look for bus speed option: "idebus="
1090 if (s[3] == 'b' && s[4] == 'u' && s[5] == 's') {
1091 if (match_parm(&s[6], NULL, vals, 1) != 1)
1093 if (vals[0] >= 20 && vals[0] <= 66) {
1094 idebus_parameter = vals[0];
1096 printk(" -- BAD BUS SPEED! Expected value from 20 to 66");
1100 * Look for interface options: "idex="
1102 if (s[3] >= '0' && s[3] <= max_hwif) {
1104 * Be VERY CAREFUL changing this: note hardcoded indexes below
1105 * (-8, -9, -10) are reserved to ease the hardcoding.
1107 static const char *ide_words[] = {
1108 "minus1", "serialize", "minus3", "minus4",
1109 "reset", "minus6", "ata66", "minus8", "minus9",
1110 "minus10", "four", "qd65xx", "ht6560b", "cmd640_vlb",
1111 "dtc2278", "umc8672", "ali14xx", NULL };
1114 hwif = &ide_hwifs[hw];
1115 i = match_parm(&s[4], ide_words, vals, 3);
1118 * Cryptic check to ensure chipset not already set for hwif.
1119 * Note: we can't depend on hwif->chipset here.
1121 if (i >= -18 && i <= -11) {
1122 /* chipset already specified */
1125 /* these drivers are for "ide0=" only */
1133 #ifdef CONFIG_BLK_DEV_ALI14XX
1134 case -17: /* "ali14xx" */
1136 goto obsolete_option;
1138 #ifdef CONFIG_BLK_DEV_UMC8672
1139 case -16: /* "umc8672" */
1141 goto obsolete_option;
1143 #ifdef CONFIG_BLK_DEV_DTC2278
1144 case -15: /* "dtc2278" */
1146 goto obsolete_option;
1148 #ifdef CONFIG_BLK_DEV_CMD640
1149 case -14: /* "cmd640_vlb" */
1151 goto obsolete_option;
1153 #ifdef CONFIG_BLK_DEV_HT6560B
1154 case -13: /* "ht6560b" */
1156 goto obsolete_option;
1158 #ifdef CONFIG_BLK_DEV_QD65XX
1159 case -12: /* "qd65xx" */
1161 goto obsolete_option;
1163 #ifdef CONFIG_BLK_DEV_4DRIVES
1164 case -11: /* "four" drives on one set of ports */
1166 goto obsolete_option;
1168 case -10: /* minus10 */
1169 case -9: /* minus9 */
1170 case -8: /* minus8 */
1175 case -7: /* ata66 */
1176 #ifdef CONFIG_BLK_DEV_IDEPCI
1178 * Use ATA_CBL_PATA40_SHORT so drive side
1179 * cable detection is also overriden.
1181 hwif->cbl = ATA_CBL_PATA40_SHORT;
1182 goto obsolete_option;
1186 case -5: /* "reset" */
1188 goto obsolete_option;
1189 case -2: /* "serialize" */
1190 hwif->mate = &ide_hwifs[hw^1];
1191 hwif->mate->mate = hwif;
1192 hwif->serialized = hwif->mate->serialized = 1;
1193 goto obsolete_option;
1202 printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
1207 printk(" -- BAD OPTION\n");
1210 printk(" -- OBSOLETE OPTION, WILL BE REMOVED SOON!\n");
1213 printk("-- NOT SUPPORTED ON ide%d", hw);
1219 EXPORT_SYMBOL(ide_lock);
1221 static int ide_bus_match(struct device *dev, struct device_driver *drv)
1226 static char *media_string(ide_drive_t *drive)
1228 switch (drive->media) {
1244 static ssize_t media_show(struct device *dev, struct device_attribute *attr, char *buf)
1246 ide_drive_t *drive = to_ide_device(dev);
1247 return sprintf(buf, "%s\n", media_string(drive));
1250 static ssize_t drivename_show(struct device *dev, struct device_attribute *attr, char *buf)
1252 ide_drive_t *drive = to_ide_device(dev);
1253 return sprintf(buf, "%s\n", drive->name);
1256 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
1258 ide_drive_t *drive = to_ide_device(dev);
1259 return sprintf(buf, "ide:m-%s\n", media_string(drive));
1262 static ssize_t model_show(struct device *dev, struct device_attribute *attr,
1265 ide_drive_t *drive = to_ide_device(dev);
1266 return sprintf(buf, "%s\n", drive->id->model);
1269 static ssize_t firmware_show(struct device *dev, struct device_attribute *attr,
1272 ide_drive_t *drive = to_ide_device(dev);
1273 return sprintf(buf, "%s\n", drive->id->fw_rev);
1276 static ssize_t serial_show(struct device *dev, struct device_attribute *attr,
1279 ide_drive_t *drive = to_ide_device(dev);
1280 return sprintf(buf, "%s\n", drive->id->serial_no);
1283 static struct device_attribute ide_dev_attrs[] = {
1285 __ATTR_RO(drivename),
1286 __ATTR_RO(modalias),
1288 __ATTR_RO(firmware),
1289 __ATTR(serial, 0400, serial_show, NULL),
1293 static int ide_uevent(struct device *dev, struct kobj_uevent_env *env)
1295 ide_drive_t *drive = to_ide_device(dev);
1297 add_uevent_var(env, "MEDIA=%s", media_string(drive));
1298 add_uevent_var(env, "DRIVENAME=%s", drive->name);
1299 add_uevent_var(env, "MODALIAS=ide:m-%s", media_string(drive));
1303 static int generic_ide_probe(struct device *dev)
1305 ide_drive_t *drive = to_ide_device(dev);
1306 ide_driver_t *drv = to_ide_driver(dev->driver);
1308 return drv->probe ? drv->probe(drive) : -ENODEV;
1311 static int generic_ide_remove(struct device *dev)
1313 ide_drive_t *drive = to_ide_device(dev);
1314 ide_driver_t *drv = to_ide_driver(dev->driver);
1322 static void generic_ide_shutdown(struct device *dev)
1324 ide_drive_t *drive = to_ide_device(dev);
1325 ide_driver_t *drv = to_ide_driver(dev->driver);
1327 if (dev->driver && drv->shutdown)
1328 drv->shutdown(drive);
1331 struct bus_type ide_bus_type = {
1333 .match = ide_bus_match,
1334 .uevent = ide_uevent,
1335 .probe = generic_ide_probe,
1336 .remove = generic_ide_remove,
1337 .shutdown = generic_ide_shutdown,
1338 .dev_attrs = ide_dev_attrs,
1339 .suspend = generic_ide_suspend,
1340 .resume = generic_ide_resume,
1343 EXPORT_SYMBOL_GPL(ide_bus_type);
1345 static void ide_port_class_release(struct device *portdev)
1347 ide_hwif_t *hwif = dev_get_drvdata(portdev);
1349 put_device(&hwif->gendev);
1353 * This is gets invoked once during initialization, to set *everything* up
1355 static int __init ide_init(void)
1359 printk(KERN_INFO "Uniform Multi-Platform E-IDE driver\n");
1360 system_bus_speed = ide_system_bus_speed();
1362 printk(KERN_INFO "ide: Assuming %dMHz system bus speed "
1363 "for PIO modes%s\n", system_bus_speed,
1364 idebus_parameter ? "" : "; override with idebus=xx");
1366 ret = bus_register(&ide_bus_type);
1368 printk(KERN_WARNING "IDE: bus_register error: %d\n", ret);
1372 ide_port_class = class_create(THIS_MODULE, "ide_port");
1373 if (IS_ERR(ide_port_class)) {
1374 ret = PTR_ERR(ide_port_class);
1375 goto out_port_class;
1377 ide_port_class->dev_release = ide_port_class_release;
1386 bus_unregister(&ide_bus_type);
1392 static char *options = NULL;
1393 module_param(options, charp, 0);
1394 MODULE_LICENSE("GPL");
1396 static void __init parse_options (char *line)
1400 if (line == NULL || !*line)
1402 while ((line = next) != NULL) {
1403 if ((next = strchr(line,' ')) != NULL)
1405 if (!ide_setup(line))
1406 printk (KERN_INFO "Unknown option '%s'\n", line);
1410 int __init init_module (void)
1412 parse_options(options);
1416 void __exit cleanup_module (void)
1420 for (index = 0; index < MAX_HWIFS; ++index)
1421 ide_unregister(index);
1425 class_destroy(ide_port_class);
1427 bus_unregister(&ide_bus_type);
1432 __setup("", ide_setup);
1434 module_init(ide_init);