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
);
170 * init_ide_data() sets reasonable default values into all fields
171 * of all instances of the hwifs and drives, but only on the first call.
172 * Subsequent calls have no effect (they don't wipe out anything).
174 * This routine is normally called at driver initialization time,
175 * but may also be called MUCH earlier during kernel "command-line"
176 * parameter processing. As such, we cannot depend on any other parts
177 * of the kernel (such as memory allocation) to be functioning yet.
179 * This is too bad, as otherwise we could dynamically allocate the
180 * ide_drive_t structs as needed, rather than always consuming memory
181 * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
183 * FIXME: We should stuff the setup data into __init and copy the
184 * relevant hwifs/allocate them properly during boot.
186 #define MAGIC_COOKIE 0x12345678
187 static void __init
init_ide_data (void)
190 static unsigned long magic_cookie
= MAGIC_COOKIE
;
193 if (magic_cookie
!= MAGIC_COOKIE
)
194 return; /* already initialized */
197 /* Initialise all interface structures */
198 for (index
= 0; index
< MAX_HWIFS
; ++index
) {
199 ide_hwif_t
*hwif
= &ide_hwifs
[index
];
200 unsigned long io_addr
= ide_default_io_base(index
);
201 unsigned long ctl_addr
= ide_default_io_ctl(io_addr
);
203 ide_init_port_data(hwif
, index
);
205 #ifdef CONFIG_IDE_ARCH_OBSOLETE_INIT
206 memset(&hw
, 0, sizeof(hw
));
207 ide_std_init_ports(&hw
, io_addr
, ctl_addr
);
208 memcpy(hwif
->io_ports
, hw
.io_ports
, sizeof(hw
.io_ports
));
210 hwif
->noprobe
= !hwif
->io_ports
[IDE_DATA_OFFSET
];
211 #if !defined(CONFIG_PPC32) || defined(CONFIG_PPLUS) || !defined(CONFIG_PCI)
213 ide_init_default_irq(hwif
->io_ports
[IDE_DATA_OFFSET
]);
219 * ide_system_bus_speed - guess bus speed
221 * ide_system_bus_speed() returns what we think is the system VESA/PCI
222 * bus speed (in MHz). This is used for calculating interface PIO timings.
223 * The default is 40 for known PCI systems, 50 otherwise.
224 * The "idebus=xx" parameter can be used to override this value.
225 * The actual value to be used is computed/displayed the first time
226 * through. Drivers should only use this as a last resort.
228 * Returns a guessed speed in MHz.
231 static int ide_system_bus_speed(void)
234 static struct pci_device_id pci_default
[] = {
235 { PCI_DEVICE(PCI_ANY_ID
, PCI_ANY_ID
) },
239 #define pci_default 0
240 #endif /* CONFIG_PCI */
242 /* user supplied value */
243 if (idebus_parameter
)
244 return idebus_parameter
;
246 /* safe default value for PCI or VESA and PCI*/
247 return pci_dev_present(pci_default
) ? 33 : 50;
250 ide_hwif_t
* ide_find_port(unsigned long base
)
255 for (i
= 0; i
< MAX_HWIFS
; i
++) {
256 hwif
= &ide_hwifs
[i
];
257 if (hwif
->io_ports
[IDE_DATA_OFFSET
] == base
)
261 for (i
= 0; i
< MAX_HWIFS
; i
++) {
262 hwif
= &ide_hwifs
[i
];
263 if (hwif
->chipset
== ide_unknown
)
272 EXPORT_SYMBOL_GPL(ide_find_port
);
274 static struct resource
* hwif_request_region(ide_hwif_t
*hwif
,
275 unsigned long addr
, int num
)
277 struct resource
*res
= request_region(addr
, num
, hwif
->name
);
280 printk(KERN_ERR
"%s: I/O resource 0x%lX-0x%lX not free.\n",
281 hwif
->name
, addr
, addr
+num
-1);
286 * ide_hwif_request_regions - request resources for IDE
287 * @hwif: interface to use
289 * Requests all the needed resources for an interface.
290 * Right now core IDE code does this work which is deeply wrong.
291 * MMIO leaves it to the controller driver,
292 * PIO will migrate this way over time.
295 int ide_hwif_request_regions(ide_hwif_t
*hwif
)
302 addr
= hwif
->io_ports
[IDE_CONTROL_OFFSET
];
303 if (addr
&& !hwif_request_region(hwif
, addr
, 1))
304 goto control_region_busy
;
306 addr
= hwif
->io_ports
[IDE_DATA_OFFSET
];
307 if ((addr
| 7) == hwif
->io_ports
[IDE_STATUS_OFFSET
]) {
308 if (!hwif_request_region(hwif
, addr
, 8))
309 goto data_region_busy
;
313 for (i
= IDE_DATA_OFFSET
; i
<= IDE_STATUS_OFFSET
; i
++) {
314 addr
= hwif
->io_ports
[i
];
315 if (!hwif_request_region(hwif
, addr
, 1)) {
317 release_region(addr
, 1);
318 goto data_region_busy
;
324 addr
= hwif
->io_ports
[IDE_CONTROL_OFFSET
];
326 release_region(addr
, 1);
328 /* If any errors are return, we drop the hwif interface. */
333 * ide_hwif_release_regions - free IDE resources
335 * Note that we only release the standard ports,
336 * and do not even try to handle any extra ports
337 * allocated for weird IDE interface chipsets.
339 * Note also that we don't yet handle mmio resources here. More
340 * importantly our caller should be doing this so we need to
341 * restructure this as a helper function for drivers.
344 void ide_hwif_release_regions(ide_hwif_t
*hwif
)
350 if (hwif
->io_ports
[IDE_CONTROL_OFFSET
])
351 release_region(hwif
->io_ports
[IDE_CONTROL_OFFSET
], 1);
352 if (hwif
->straight8
) {
353 release_region(hwif
->io_ports
[IDE_DATA_OFFSET
], 8);
356 for (i
= IDE_DATA_OFFSET
; i
<= IDE_STATUS_OFFSET
; i
++)
357 if (hwif
->io_ports
[i
])
358 release_region(hwif
->io_ports
[i
], 1);
361 void ide_remove_port_from_hwgroup(ide_hwif_t
*hwif
)
363 ide_hwgroup_t
*hwgroup
= hwif
->hwgroup
;
365 spin_lock_irq(&ide_lock
);
367 * Remove us from the hwgroup, and free
368 * the hwgroup if we were the only member
370 if (hwif
->next
== hwif
) {
371 BUG_ON(hwgroup
->hwif
!= hwif
);
374 /* There is another interface in hwgroup.
375 * Unlink us, and set hwgroup->drive and ->hwif to
378 ide_hwif_t
*g
= hwgroup
->hwif
;
380 while (g
->next
!= hwif
)
382 g
->next
= hwif
->next
;
383 if (hwgroup
->hwif
== hwif
) {
384 /* Chose a random hwif for hwgroup->hwif.
385 * It's guaranteed that there are no drives
386 * left in the hwgroup.
388 BUG_ON(hwgroup
->drive
!= NULL
);
391 BUG_ON(hwgroup
->hwif
== hwif
);
393 spin_unlock_irq(&ide_lock
);
396 /* Called with ide_lock held. */
397 static void __ide_port_unregister_devices(ide_hwif_t
*hwif
)
401 for (i
= 0; i
< MAX_DRIVES
; i
++) {
402 ide_drive_t
*drive
= &hwif
->drives
[i
];
404 if (drive
->present
) {
405 spin_unlock_irq(&ide_lock
);
406 device_unregister(&drive
->gendev
);
407 wait_for_completion(&drive
->gendev_rel_comp
);
408 spin_lock_irq(&ide_lock
);
413 void ide_port_unregister_devices(ide_hwif_t
*hwif
)
415 mutex_lock(&ide_cfg_mtx
);
416 spin_lock_irq(&ide_lock
);
417 __ide_port_unregister_devices(hwif
);
419 ide_port_init_devices_data(hwif
);
420 spin_unlock_irq(&ide_lock
);
421 mutex_unlock(&ide_cfg_mtx
);
423 EXPORT_SYMBOL_GPL(ide_port_unregister_devices
);
426 * ide_unregister - free an IDE interface
427 * @index: index of interface (will change soon to a pointer)
429 * Perform the final unregister of an IDE interface. At the moment
430 * we don't refcount interfaces so this will also get split up.
433 * The caller must not hold the IDE locks
434 * The drive present/vanishing is not yet properly locked
435 * Take care with the callbacks. These have been split to avoid
436 * deadlocking the IDE layer. The shutdown callback is called
437 * before we take the lock and free resources. It is up to the
438 * caller to be sure there is no pending I/O here, and that
439 * the interface will not be reopened (present/vanishing locking
440 * isn't yet done BTW). After we commit to the final kill we
441 * call the cleanup callback with the ide locks held.
443 * Unregister restores the hwif structures to the default state.
444 * This is raving bonkers.
447 void ide_unregister(unsigned int index
)
449 ide_hwif_t
*hwif
, *g
;
450 ide_hwgroup_t
*hwgroup
;
453 BUG_ON(index
>= MAX_HWIFS
);
455 BUG_ON(in_interrupt());
456 BUG_ON(irqs_disabled());
457 mutex_lock(&ide_cfg_mtx
);
458 spin_lock_irq(&ide_lock
);
459 hwif
= &ide_hwifs
[index
];
462 __ide_port_unregister_devices(hwif
);
465 spin_unlock_irq(&ide_lock
);
467 ide_proc_unregister_port(hwif
);
469 hwgroup
= hwif
->hwgroup
;
471 * free the irq if we were the only hwif using it
475 if (g
->irq
== hwif
->irq
)
478 } while (g
!= hwgroup
->hwif
);
480 free_irq(hwif
->irq
, hwgroup
);
482 ide_remove_port_from_hwgroup(hwif
);
484 device_unregister(hwif
->portdev
);
485 device_unregister(&hwif
->gendev
);
486 wait_for_completion(&hwif
->gendev_rel_comp
);
489 * Remove us from the kernel's knowledge
491 blk_unregister_region(MKDEV(hwif
->major
, 0), MAX_DRIVES
<<PARTN_BITS
);
492 kfree(hwif
->sg_table
);
493 unregister_blkdev(hwif
->major
, hwif
->name
);
494 spin_lock_irq(&ide_lock
);
497 (void)ide_release_dma(hwif
);
499 ide_hwif_release_regions(hwif
);
501 /* restore hwif data to pristine status */
502 ide_init_port_data(hwif
, index
);
505 spin_unlock_irq(&ide_lock
);
506 mutex_unlock(&ide_cfg_mtx
);
509 EXPORT_SYMBOL(ide_unregister
);
511 void ide_init_port_hw(ide_hwif_t
*hwif
, hw_regs_t
*hw
)
513 memcpy(hwif
->io_ports
, hw
->io_ports
, sizeof(hwif
->io_ports
));
516 hwif
->chipset
= hw
->chipset
;
517 hwif
->gendev
.parent
= hw
->dev
;
518 hwif
->ack_intr
= hw
->ack_intr
;
520 EXPORT_SYMBOL_GPL(ide_init_port_hw
);
523 * Locks for IDE setting functionality
526 DEFINE_MUTEX(ide_setting_mtx
);
528 EXPORT_SYMBOL_GPL(ide_setting_mtx
);
531 * ide_spin_wait_hwgroup - wait for group
532 * @drive: drive in the group
534 * Wait for an IDE device group to go non busy and then return
535 * holding the ide_lock which guards the hwgroup->busy status
536 * and right to use it.
539 int ide_spin_wait_hwgroup (ide_drive_t
*drive
)
541 ide_hwgroup_t
*hwgroup
= HWGROUP(drive
);
542 unsigned long timeout
= jiffies
+ (3 * HZ
);
544 spin_lock_irq(&ide_lock
);
546 while (hwgroup
->busy
) {
547 unsigned long lflags
;
548 spin_unlock_irq(&ide_lock
);
549 local_irq_set(lflags
);
550 if (time_after(jiffies
, timeout
)) {
551 local_irq_restore(lflags
);
552 printk(KERN_ERR
"%s: channel busy\n", drive
->name
);
555 local_irq_restore(lflags
);
556 spin_lock_irq(&ide_lock
);
561 EXPORT_SYMBOL(ide_spin_wait_hwgroup
);
563 int set_io_32bit(ide_drive_t
*drive
, int arg
)
565 if (drive
->no_io_32bit
)
568 if (arg
< 0 || arg
> 1 + (SUPPORT_VLB_SYNC
<< 1))
571 if (ide_spin_wait_hwgroup(drive
))
574 drive
->io_32bit
= arg
;
576 spin_unlock_irq(&ide_lock
);
581 static int set_ksettings(ide_drive_t
*drive
, int arg
)
583 if (arg
< 0 || arg
> 1)
586 if (ide_spin_wait_hwgroup(drive
))
588 drive
->keep_settings
= arg
;
589 spin_unlock_irq(&ide_lock
);
594 int set_using_dma(ide_drive_t
*drive
, int arg
)
596 #ifdef CONFIG_BLK_DEV_IDEDMA
597 ide_hwif_t
*hwif
= drive
->hwif
;
600 if (arg
< 0 || arg
> 1)
603 if (!drive
->id
|| !(drive
->id
->capability
& 1))
606 if (hwif
->dma_host_set
== NULL
)
610 if (ide_spin_wait_hwgroup(drive
))
613 * set ->busy flag, unlock and let it ride
615 hwif
->hwgroup
->busy
= 1;
616 spin_unlock_irq(&ide_lock
);
621 if (ide_set_dma(drive
))
627 * lock, clear ->busy flag and unlock before leaving
629 spin_lock_irq(&ide_lock
);
630 hwif
->hwgroup
->busy
= 0;
631 spin_unlock_irq(&ide_lock
);
635 if (arg
< 0 || arg
> 1)
642 int set_pio_mode(ide_drive_t
*drive
, int arg
)
646 if (arg
< 0 || arg
> 255)
649 if (drive
->hwif
->set_pio_mode
== NULL
)
652 if (drive
->special
.b
.set_tune
)
655 ide_init_drive_cmd(&rq
);
656 rq
.cmd_type
= REQ_TYPE_ATA_TASKFILE
;
658 drive
->tune_req
= (u8
) arg
;
659 drive
->special
.b
.set_tune
= 1;
660 (void) ide_do_drive_cmd(drive
, &rq
, ide_wait
);
664 static int set_unmaskirq(ide_drive_t
*drive
, int arg
)
666 if (drive
->no_unmask
)
669 if (arg
< 0 || arg
> 1)
672 if (ide_spin_wait_hwgroup(drive
))
675 spin_unlock_irq(&ide_lock
);
681 * system_bus_clock - clock guess
683 * External version of the bus clock guess used by very old IDE drivers
684 * for things like VLB timings. Should not be used.
687 int system_bus_clock (void)
689 return system_bus_speed
;
692 EXPORT_SYMBOL(system_bus_clock
);
694 static int generic_ide_suspend(struct device
*dev
, pm_message_t mesg
)
696 ide_drive_t
*drive
= dev
->driver_data
;
697 ide_hwif_t
*hwif
= HWIF(drive
);
699 struct request_pm_state rqpm
;
703 /* Call ACPI _GTM only once */
704 if (!(drive
->dn
% 2))
705 ide_acpi_get_timing(hwif
);
707 memset(&rq
, 0, sizeof(rq
));
708 memset(&rqpm
, 0, sizeof(rqpm
));
709 memset(&args
, 0, sizeof(args
));
710 rq
.cmd_type
= REQ_TYPE_PM_SUSPEND
;
713 rqpm
.pm_step
= ide_pm_state_start_suspend
;
714 if (mesg
.event
== PM_EVENT_PRETHAW
)
715 mesg
.event
= PM_EVENT_FREEZE
;
716 rqpm
.pm_state
= mesg
.event
;
718 ret
= ide_do_drive_cmd(drive
, &rq
, ide_wait
);
719 /* only call ACPI _PS3 after both drivers are suspended */
720 if (!ret
&& (((drive
->dn
% 2) && hwif
->drives
[0].present
721 && hwif
->drives
[1].present
)
722 || !hwif
->drives
[0].present
723 || !hwif
->drives
[1].present
))
724 ide_acpi_set_state(hwif
, 0);
728 static int generic_ide_resume(struct device
*dev
)
730 ide_drive_t
*drive
= dev
->driver_data
;
731 ide_hwif_t
*hwif
= HWIF(drive
);
733 struct request_pm_state rqpm
;
737 /* Call ACPI _STM only once */
738 if (!(drive
->dn
% 2)) {
739 ide_acpi_set_state(hwif
, 1);
740 ide_acpi_push_timing(hwif
);
743 ide_acpi_exec_tfs(drive
);
745 memset(&rq
, 0, sizeof(rq
));
746 memset(&rqpm
, 0, sizeof(rqpm
));
747 memset(&args
, 0, sizeof(args
));
748 rq
.cmd_type
= REQ_TYPE_PM_RESUME
;
751 rqpm
.pm_step
= ide_pm_state_start_resume
;
752 rqpm
.pm_state
= PM_EVENT_ON
;
754 err
= ide_do_drive_cmd(drive
, &rq
, ide_head_wait
);
756 if (err
== 0 && dev
->driver
) {
757 ide_driver_t
*drv
= to_ide_driver(dev
->driver
);
766 int generic_ide_ioctl(ide_drive_t
*drive
, struct file
*file
, struct block_device
*bdev
,
767 unsigned int cmd
, unsigned long arg
)
771 void __user
*p
= (void __user
*)arg
;
772 int err
= 0, (*setfunc
)(ide_drive_t
*, int);
776 case HDIO_GET_32BIT
: val
= &drive
->io_32bit
; goto read_val
;
777 case HDIO_GET_KEEPSETTINGS
: val
= &drive
->keep_settings
; goto read_val
;
778 case HDIO_GET_UNMASKINTR
: val
= &drive
->unmask
; goto read_val
;
779 case HDIO_GET_DMA
: val
= &drive
->using_dma
; goto read_val
;
780 case HDIO_SET_32BIT
: setfunc
= set_io_32bit
; goto set_val
;
781 case HDIO_SET_KEEPSETTINGS
: setfunc
= set_ksettings
; goto set_val
;
782 case HDIO_SET_PIO_MODE
: setfunc
= set_pio_mode
; goto set_val
;
783 case HDIO_SET_UNMASKINTR
: setfunc
= set_unmaskirq
; goto set_val
;
784 case HDIO_SET_DMA
: setfunc
= set_using_dma
; goto set_val
;
788 case HDIO_OBSOLETE_IDENTITY
:
789 case HDIO_GET_IDENTITY
:
790 if (bdev
!= bdev
->bd_contains
)
792 if (drive
->id_read
== 0)
794 if (copy_to_user(p
, drive
->id
, (cmd
== HDIO_GET_IDENTITY
) ? sizeof(*drive
->id
) : 142))
799 return put_user(drive
->dsc_overlap
<< IDE_NICE_DSC_OVERLAP
|
800 drive
->atapi_overlap
<< IDE_NICE_ATAPI_OVERLAP
|
801 drive
->nice1
<< IDE_NICE_1
,
802 (long __user
*) arg
);
803 #ifdef CONFIG_IDE_TASK_IOCTL
804 case HDIO_DRIVE_TASKFILE
:
805 if (!capable(CAP_SYS_ADMIN
) || !capable(CAP_SYS_RAWIO
))
807 switch(drive
->media
) {
809 return ide_taskfile_ioctl(drive
, cmd
, arg
);
813 #endif /* CONFIG_IDE_TASK_IOCTL */
816 if (!capable(CAP_SYS_RAWIO
))
818 return ide_cmd_ioctl(drive
, cmd
, arg
);
820 case HDIO_DRIVE_TASK
:
821 if (!capable(CAP_SYS_RAWIO
))
823 return ide_task_ioctl(drive
, cmd
, arg
);
825 if (!capable(CAP_SYS_ADMIN
)) return -EACCES
;
826 if (arg
!= (arg
& ((1 << IDE_NICE_DSC_OVERLAP
) | (1 << IDE_NICE_1
))))
828 drive
->dsc_overlap
= (arg
>> IDE_NICE_DSC_OVERLAP
) & 1;
829 drv
= *(ide_driver_t
**)bdev
->bd_disk
->private_data
;
830 if (drive
->dsc_overlap
&& !drv
->supports_dsc_overlap
) {
831 drive
->dsc_overlap
= 0;
834 drive
->nice1
= (arg
>> IDE_NICE_1
) & 1;
836 case HDIO_DRIVE_RESET
:
837 if (!capable(CAP_SYS_ADMIN
))
841 * Abort the current command on the
842 * group if there is one, taking
843 * care not to allow anything else
844 * to be queued and to die on the
845 * spot if we miss one somehow
848 spin_lock_irqsave(&ide_lock
, flags
);
850 if (HWGROUP(drive
)->resetting
) {
851 spin_unlock_irqrestore(&ide_lock
, flags
);
855 ide_abort(drive
, "drive reset");
857 BUG_ON(HWGROUP(drive
)->handler
);
859 /* Ensure nothing gets queued after we
860 drop the lock. Reset will clear the busy */
862 HWGROUP(drive
)->busy
= 1;
863 spin_unlock_irqrestore(&ide_lock
, flags
);
864 (void) ide_do_reset(drive
);
867 case HDIO_GET_BUSSTATE
:
868 if (!capable(CAP_SYS_ADMIN
))
870 if (put_user(HWIF(drive
)->bus_state
, (long __user
*)arg
))
874 case HDIO_SET_BUSSTATE
:
875 if (!capable(CAP_SYS_ADMIN
))
883 mutex_lock(&ide_setting_mtx
);
884 spin_lock_irqsave(&ide_lock
, flags
);
886 spin_unlock_irqrestore(&ide_lock
, flags
);
887 mutex_unlock(&ide_setting_mtx
);
888 return err
>= 0 ? put_user(err
, (long __user
*)arg
) : err
;
891 if (bdev
!= bdev
->bd_contains
)
894 if (!capable(CAP_SYS_ADMIN
))
897 mutex_lock(&ide_setting_mtx
);
898 err
= setfunc(drive
, arg
);
899 mutex_unlock(&ide_setting_mtx
);
905 EXPORT_SYMBOL(generic_ide_ioctl
);
908 * stridx() returns the offset of c within s,
909 * or -1 if c is '\0' or not found within s.
911 static int __init
stridx (const char *s
, char c
)
913 char *i
= strchr(s
, c
);
914 return (i
&& c
) ? i
- s
: -1;
918 * match_parm() does parsing for ide_setup():
920 * 1. the first char of s must be '='.
921 * 2. if the remainder matches one of the supplied keywords,
922 * the index (1 based) of the keyword is negated and returned.
923 * 3. if the remainder is a series of no more than max_vals numbers
924 * separated by commas, the numbers are saved in vals[] and a
925 * count of how many were saved is returned. Base10 is assumed,
926 * and base16 is allowed when prefixed with "0x".
927 * 4. otherwise, zero is returned.
929 static int __init
match_parm (char *s
, const char *keywords
[], int vals
[], int max_vals
)
931 static const char *decimal
= "0123456789";
932 static const char *hex
= "0123456789abcdef";
937 * Try matching against the supplied keywords,
938 * and return -(index+1) if we match one
940 if (keywords
!= NULL
) {
941 for (i
= 0; *keywords
!= NULL
; ++i
) {
942 if (!strcmp(s
, *keywords
++))
947 * Look for a series of no more than "max_vals"
948 * numeric values separated by commas, in base10,
949 * or base16 when prefixed with "0x".
950 * Return a count of how many were found.
952 for (n
= 0; (i
= stridx(decimal
, *s
)) >= 0;) {
954 while ((i
= stridx(decimal
, *++s
)) >= 0)
955 vals
[n
] = (vals
[n
] * 10) + i
;
956 if (*s
== 'x' && !vals
[n
]) {
957 while ((i
= stridx(hex
, *++s
)) >= 0)
958 vals
[n
] = (vals
[n
] * 0x10) + i
;
962 if (*s
== ',' || *s
== ';')
968 return 0; /* zero = nothing matched */
971 extern int probe_ali14xx
;
972 extern int probe_umc8672
;
973 extern int probe_dtc2278
;
974 extern int probe_ht6560b
;
975 extern int probe_qd65xx
;
976 extern int cmd640_vlb
;
978 static int __initdata is_chipset_set
;
981 * ide_setup() gets called VERY EARLY during initialization,
982 * to handle kernel "command line" strings beginning with "hdx=" or "ide".
984 * Remember to update Documentation/ide/ide.txt if you change something here.
986 static int __init
ide_setup(char *s
)
991 unsigned int hw
, unit
;
992 const char max_drive
= 'a' + ((MAX_HWIFS
* MAX_DRIVES
) - 1);
993 const char max_hwif
= '0' + (MAX_HWIFS
- 1);
996 if (strncmp(s
,"hd",2) == 0 && s
[2] == '=') /* hd= is for hd.c */
997 return 0; /* driver and not us */
999 if (strncmp(s
,"ide",3) && strncmp(s
,"idebus",6) && strncmp(s
,"hd",2))
1002 printk(KERN_INFO
"ide_setup: %s", s
);
1005 #ifdef CONFIG_BLK_DEV_IDEDOUBLER
1006 if (!strcmp(s
, "ide=doubler")) {
1007 extern int ide_doubler
;
1009 printk(" : Enabled support for IDE doublers\n");
1013 #endif /* CONFIG_BLK_DEV_IDEDOUBLER */
1015 if (!strcmp(s
, "ide=nodma")) {
1016 printk(" : Prevented DMA\n");
1018 goto obsolete_option
;
1021 #ifdef CONFIG_BLK_DEV_IDEACPI
1022 if (!strcmp(s
, "ide=noacpi")) {
1023 //printk(" : Disable IDE ACPI support.\n");
1027 if (!strcmp(s
, "ide=acpigtf")) {
1028 //printk(" : Enable IDE ACPI _GTF support.\n");
1032 if (!strcmp(s
, "ide=acpionboot")) {
1033 //printk(" : Call IDE ACPI methods on boot.\n");
1034 ide_noacpionboot
= 0;
1037 #endif /* CONFIG_BLK_DEV_IDEACPI */
1040 * Look for drive options: "hdx="
1042 if (s
[0] == 'h' && s
[1] == 'd' && s
[2] >= 'a' && s
[2] <= max_drive
) {
1043 const char *hd_words
[] = {
1044 "none", "noprobe", "nowerr", "cdrom", "nodma",
1045 "autotune", "noautotune", "-8", "-9", "-10",
1046 "noflush", "remap", "remap63", "scsi", NULL
};
1048 hw
= unit
/ MAX_DRIVES
;
1049 unit
= unit
% MAX_DRIVES
;
1050 hwif
= &ide_hwifs
[hw
];
1051 drive
= &hwif
->drives
[unit
];
1052 if (strncmp(s
+ 4, "ide-", 4) == 0) {
1053 strlcpy(drive
->driver_req
, s
+ 4, sizeof(drive
->driver_req
));
1054 goto obsolete_option
;
1056 switch (match_parm(&s
[3], hd_words
, vals
, 3)) {
1057 case -1: /* "none" */
1058 case -2: /* "noprobe" */
1061 case -3: /* "nowerr" */
1062 drive
->bad_wstat
= BAD_R_STAT
;
1065 case -4: /* "cdrom" */
1067 drive
->media
= ide_cdrom
;
1068 /* an ATAPI device ignores DRDY */
1069 drive
->ready_stat
= 0;
1072 case -5: /* nodma */
1075 case -6: /* "autotune" */
1076 drive
->autotune
= IDE_TUNE_AUTO
;
1077 goto obsolete_option
;
1078 case -7: /* "noautotune" */
1079 drive
->autotune
= IDE_TUNE_NOAUTO
;
1080 goto obsolete_option
;
1081 case -11: /* noflush */
1084 case -12: /* "remap" */
1085 drive
->remap_0_to_1
= 1;
1086 goto obsolete_option
;
1087 case -13: /* "remap63" */
1089 goto obsolete_option
;
1090 case -14: /* "scsi" */
1092 goto obsolete_option
;
1093 case 3: /* cyl,head,sect */
1094 drive
->media
= ide_disk
;
1095 drive
->ready_stat
= READY_STAT
;
1096 drive
->cyl
= drive
->bios_cyl
= vals
[0];
1097 drive
->head
= drive
->bios_head
= vals
[1];
1098 drive
->sect
= drive
->bios_sect
= vals
[2];
1100 drive
->forced_geom
= 1;
1108 if (s
[0] != 'i' || s
[1] != 'd' || s
[2] != 'e')
1111 * Look for bus speed option: "idebus="
1113 if (s
[3] == 'b' && s
[4] == 'u' && s
[5] == 's') {
1114 if (match_parm(&s
[6], NULL
, vals
, 1) != 1)
1116 if (vals
[0] >= 20 && vals
[0] <= 66) {
1117 idebus_parameter
= vals
[0];
1119 printk(" -- BAD BUS SPEED! Expected value from 20 to 66");
1123 * Look for interface options: "idex="
1125 if (s
[3] >= '0' && s
[3] <= max_hwif
) {
1127 * Be VERY CAREFUL changing this: note hardcoded indexes below
1128 * (-8, -9, -10) are reserved to ease the hardcoding.
1130 static const char *ide_words
[] = {
1131 "noprobe", "serialize", "minus3", "minus4",
1132 "reset", "minus6", "ata66", "minus8", "minus9",
1133 "minus10", "four", "qd65xx", "ht6560b", "cmd640_vlb",
1134 "dtc2278", "umc8672", "ali14xx", NULL
};
1137 hwif
= &ide_hwifs
[hw
];
1138 i
= match_parm(&s
[4], ide_words
, vals
, 3);
1141 * Cryptic check to ensure chipset not already set for hwif.
1142 * Note: we can't depend on hwif->chipset here.
1144 if (i
>= -18 && i
<= -11) {
1145 /* chipset already specified */
1148 /* these drivers are for "ide0=" only */
1156 #ifdef CONFIG_BLK_DEV_ALI14XX
1157 case -17: /* "ali14xx" */
1159 goto obsolete_option
;
1161 #ifdef CONFIG_BLK_DEV_UMC8672
1162 case -16: /* "umc8672" */
1164 goto obsolete_option
;
1166 #ifdef CONFIG_BLK_DEV_DTC2278
1167 case -15: /* "dtc2278" */
1169 goto obsolete_option
;
1171 #ifdef CONFIG_BLK_DEV_CMD640
1172 case -14: /* "cmd640_vlb" */
1174 goto obsolete_option
;
1176 #ifdef CONFIG_BLK_DEV_HT6560B
1177 case -13: /* "ht6560b" */
1179 goto obsolete_option
;
1181 #ifdef CONFIG_BLK_DEV_QD65XX
1182 case -12: /* "qd65xx" */
1184 goto obsolete_option
;
1186 #ifdef CONFIG_BLK_DEV_4DRIVES
1187 case -11: /* "four" drives on one set of ports */
1189 ide_hwif_t
*mate
= &ide_hwifs
[hw
^1];
1190 mate
->drives
[0].select
.all
^= 0x20;
1191 mate
->drives
[1].select
.all
^= 0x20;
1192 hwif
->chipset
= mate
->chipset
= ide_4drives
;
1193 mate
->irq
= hwif
->irq
;
1194 memcpy(mate
->io_ports
, hwif
->io_ports
, sizeof(hwif
->io_ports
));
1197 hwif
->serialized
= mate
->serialized
= 1;
1198 goto obsolete_option
;
1200 #endif /* CONFIG_BLK_DEV_4DRIVES */
1201 case -10: /* minus10 */
1202 case -9: /* minus9 */
1203 case -8: /* minus8 */
1208 case -7: /* ata66 */
1209 #ifdef CONFIG_BLK_DEV_IDEPCI
1211 * Use ATA_CBL_PATA40_SHORT so drive side
1212 * cable detection is also overriden.
1214 hwif
->cbl
= ATA_CBL_PATA40_SHORT
;
1215 goto obsolete_option
;
1219 case -5: /* "reset" */
1221 goto obsolete_option
;
1222 case -2: /* "serialize" */
1223 hwif
->mate
= &ide_hwifs
[hw
^1];
1224 hwif
->mate
->mate
= hwif
;
1225 hwif
->serialized
= hwif
->mate
->serialized
= 1;
1226 goto obsolete_option
;
1228 case -1: /* "noprobe" */
1230 goto obsolete_option
;
1237 printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
1242 printk(" -- BAD OPTION\n");
1245 printk(" -- OBSOLETE OPTION, WILL BE REMOVED SOON!\n");
1248 printk("-- NOT SUPPORTED ON ide%d", hw
);
1254 EXPORT_SYMBOL(ide_lock
);
1256 static int ide_bus_match(struct device
*dev
, struct device_driver
*drv
)
1261 static char *media_string(ide_drive_t
*drive
)
1263 switch (drive
->media
) {
1279 static ssize_t
media_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1281 ide_drive_t
*drive
= to_ide_device(dev
);
1282 return sprintf(buf
, "%s\n", media_string(drive
));
1285 static ssize_t
drivename_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1287 ide_drive_t
*drive
= to_ide_device(dev
);
1288 return sprintf(buf
, "%s\n", drive
->name
);
1291 static ssize_t
modalias_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1293 ide_drive_t
*drive
= to_ide_device(dev
);
1294 return sprintf(buf
, "ide:m-%s\n", media_string(drive
));
1297 static ssize_t
model_show(struct device
*dev
, struct device_attribute
*attr
,
1300 ide_drive_t
*drive
= to_ide_device(dev
);
1301 return sprintf(buf
, "%s\n", drive
->id
->model
);
1304 static ssize_t
firmware_show(struct device
*dev
, struct device_attribute
*attr
,
1307 ide_drive_t
*drive
= to_ide_device(dev
);
1308 return sprintf(buf
, "%s\n", drive
->id
->fw_rev
);
1311 static ssize_t
serial_show(struct device
*dev
, struct device_attribute
*attr
,
1314 ide_drive_t
*drive
= to_ide_device(dev
);
1315 return sprintf(buf
, "%s\n", drive
->id
->serial_no
);
1318 static struct device_attribute ide_dev_attrs
[] = {
1320 __ATTR_RO(drivename
),
1321 __ATTR_RO(modalias
),
1323 __ATTR_RO(firmware
),
1324 __ATTR(serial
, 0400, serial_show
, NULL
),
1328 static int ide_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
1330 ide_drive_t
*drive
= to_ide_device(dev
);
1332 add_uevent_var(env
, "MEDIA=%s", media_string(drive
));
1333 add_uevent_var(env
, "DRIVENAME=%s", drive
->name
);
1334 add_uevent_var(env
, "MODALIAS=ide:m-%s", media_string(drive
));
1338 static int generic_ide_probe(struct device
*dev
)
1340 ide_drive_t
*drive
= to_ide_device(dev
);
1341 ide_driver_t
*drv
= to_ide_driver(dev
->driver
);
1343 return drv
->probe
? drv
->probe(drive
) : -ENODEV
;
1346 static int generic_ide_remove(struct device
*dev
)
1348 ide_drive_t
*drive
= to_ide_device(dev
);
1349 ide_driver_t
*drv
= to_ide_driver(dev
->driver
);
1357 static void generic_ide_shutdown(struct device
*dev
)
1359 ide_drive_t
*drive
= to_ide_device(dev
);
1360 ide_driver_t
*drv
= to_ide_driver(dev
->driver
);
1362 if (dev
->driver
&& drv
->shutdown
)
1363 drv
->shutdown(drive
);
1366 struct bus_type ide_bus_type
= {
1368 .match
= ide_bus_match
,
1369 .uevent
= ide_uevent
,
1370 .probe
= generic_ide_probe
,
1371 .remove
= generic_ide_remove
,
1372 .shutdown
= generic_ide_shutdown
,
1373 .dev_attrs
= ide_dev_attrs
,
1374 .suspend
= generic_ide_suspend
,
1375 .resume
= generic_ide_resume
,
1378 EXPORT_SYMBOL_GPL(ide_bus_type
);
1380 static void ide_port_class_release(struct device
*portdev
)
1382 ide_hwif_t
*hwif
= dev_get_drvdata(portdev
);
1384 put_device(&hwif
->gendev
);
1388 * This is gets invoked once during initialization, to set *everything* up
1390 static int __init
ide_init(void)
1394 printk(KERN_INFO
"Uniform Multi-Platform E-IDE driver\n");
1395 system_bus_speed
= ide_system_bus_speed();
1397 printk(KERN_INFO
"ide: Assuming %dMHz system bus speed "
1398 "for PIO modes%s\n", system_bus_speed
,
1399 idebus_parameter
? "" : "; override with idebus=xx");
1401 ret
= bus_register(&ide_bus_type
);
1403 printk(KERN_WARNING
"IDE: bus_register error: %d\n", ret
);
1407 ide_port_class
= class_create(THIS_MODULE
, "ide_port");
1408 if (IS_ERR(ide_port_class
)) {
1409 ret
= PTR_ERR(ide_port_class
);
1410 goto out_port_class
;
1412 ide_port_class
->dev_release
= ide_port_class_release
;
1421 bus_unregister(&ide_bus_type
);
1427 static char *options
= NULL
;
1428 module_param(options
, charp
, 0);
1429 MODULE_LICENSE("GPL");
1431 static void __init
parse_options (char *line
)
1435 if (line
== NULL
|| !*line
)
1437 while ((line
= next
) != NULL
) {
1438 if ((next
= strchr(line
,' ')) != NULL
)
1440 if (!ide_setup(line
))
1441 printk (KERN_INFO
"Unknown option '%s'\n", line
);
1445 int __init
init_module (void)
1447 parse_options(options
);
1451 void __exit
cleanup_module (void)
1455 for (index
= 0; index
< MAX_HWIFS
; ++index
)
1456 ide_unregister(index
);
1460 class_destroy(ide_port_class
);
1462 bus_unregister(&ide_bus_type
);
1467 __setup("", ide_setup
);
1469 module_init(ide_init
);