[MIPS] Fix another trap decoding issue
[linux-2.6/linux-mips/linux-dm7025.git] / drivers / ide / ide.c
blob917c72dcd33dccc806c42c20d9662750ebe52b89
1 /*
2 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
3 * Copyrifht (C) 2003-2005, 2007 Bartlomiej Zolnierkiewicz
4 */
6 /*
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
15 * (usually 14 & 15).
16 * There can be up to two drives per interface, as per the ATA-2 spec.
18 * ...
20 * From hd.c:
21 * |
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!
25 * |
26 * | modified by Drew Eckhardt to check nr of hd's from the CMOS.
27 * |
28 * | Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
29 * | in the early extended-partition checks and added DM partitions.
30 * |
31 * | Early work on error handling by Mika Liljeberg (liljeber@cs.Helsinki.FI).
32 * |
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>
54 #include <linux/mm.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>
73 #include <asm/irq.h>
74 #include <asm/uaccess.h>
75 #include <asm/io.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);
95 int noautodma = 0;
97 #ifdef CONFIG_BLK_DEV_IDEACPI
98 int ide_noacpi = 0;
99 int ide_noacpitfs = 1;
100 int ide_noacpionboot = 1;
101 #endif
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 */
121 hwif->index = index;
122 hwif->major = ide_hwif_to_major[index];
124 hwif->name[0] = 'i';
125 hwif->name[1] = 'd';
126 hwif->name[2] = 'e';
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)
142 int unit;
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;
152 drive->hwif = hwif;
153 drive->ctl = 0x08;
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)
188 unsigned int index;
189 static unsigned long magic_cookie = MAGIC_COOKIE;
191 if (magic_cookie != MAGIC_COOKIE)
192 return; /* already initialized */
193 magic_cookie = 0;
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)
218 #ifdef CONFIG_PCI
219 static struct pci_device_id pci_default[] = {
220 { PCI_DEVICE(PCI_ANY_ID, PCI_ANY_ID) },
223 #else
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(unsigned long base)
237 ide_hwif_t *hwif;
238 int i;
240 for (i = 0; i < MAX_HWIFS; i++) {
241 hwif = &ide_hwifs[i];
242 if (hwif->io_ports[IDE_DATA_OFFSET] == base)
243 goto found;
246 for (i = 0; i < MAX_HWIFS; i++) {
247 hwif = &ide_hwifs[i];
248 if (hwif->chipset == ide_unknown)
249 goto found;
252 hwif = NULL;
253 found:
254 return hwif;
257 EXPORT_SYMBOL_GPL(ide_find_port);
259 static struct resource* hwif_request_region(ide_hwif_t *hwif,
260 unsigned long addr, int num)
262 struct resource *res = request_region(addr, num, hwif->name);
264 if (!res)
265 printk(KERN_ERR "%s: I/O resource 0x%lX-0x%lX not free.\n",
266 hwif->name, addr, addr+num-1);
267 return res;
271 * ide_hwif_request_regions - request resources for IDE
272 * @hwif: interface to use
274 * Requests all the needed resources for an interface.
275 * Right now core IDE code does this work which is deeply wrong.
276 * MMIO leaves it to the controller driver,
277 * PIO will migrate this way over time.
280 int ide_hwif_request_regions(ide_hwif_t *hwif)
282 unsigned long addr;
283 unsigned int i;
285 if (hwif->mmio)
286 return 0;
287 addr = hwif->io_ports[IDE_CONTROL_OFFSET];
288 if (addr && !hwif_request_region(hwif, addr, 1))
289 goto control_region_busy;
290 hwif->straight8 = 0;
291 addr = hwif->io_ports[IDE_DATA_OFFSET];
292 if ((addr | 7) == hwif->io_ports[IDE_STATUS_OFFSET]) {
293 if (!hwif_request_region(hwif, addr, 8))
294 goto data_region_busy;
295 hwif->straight8 = 1;
296 return 0;
298 for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++) {
299 addr = hwif->io_ports[i];
300 if (!hwif_request_region(hwif, addr, 1)) {
301 while (--i)
302 release_region(addr, 1);
303 goto data_region_busy;
306 return 0;
308 data_region_busy:
309 addr = hwif->io_ports[IDE_CONTROL_OFFSET];
310 if (addr)
311 release_region(addr, 1);
312 control_region_busy:
313 /* If any errors are return, we drop the hwif interface. */
314 return -EBUSY;
318 * ide_hwif_release_regions - free IDE resources
320 * Note that we only release the standard ports,
321 * and do not even try to handle any extra ports
322 * allocated for weird IDE interface chipsets.
324 * Note also that we don't yet handle mmio resources here. More
325 * importantly our caller should be doing this so we need to
326 * restructure this as a helper function for drivers.
329 void ide_hwif_release_regions(ide_hwif_t *hwif)
331 u32 i = 0;
333 if (hwif->mmio)
334 return;
335 if (hwif->io_ports[IDE_CONTROL_OFFSET])
336 release_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
337 if (hwif->straight8) {
338 release_region(hwif->io_ports[IDE_DATA_OFFSET], 8);
339 return;
341 for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++)
342 if (hwif->io_ports[i])
343 release_region(hwif->io_ports[i], 1);
346 void ide_remove_port_from_hwgroup(ide_hwif_t *hwif)
348 ide_hwgroup_t *hwgroup = hwif->hwgroup;
350 spin_lock_irq(&ide_lock);
352 * Remove us from the hwgroup, and free
353 * the hwgroup if we were the only member
355 if (hwif->next == hwif) {
356 BUG_ON(hwgroup->hwif != hwif);
357 kfree(hwgroup);
358 } else {
359 /* There is another interface in hwgroup.
360 * Unlink us, and set hwgroup->drive and ->hwif to
361 * something sane.
363 ide_hwif_t *g = hwgroup->hwif;
365 while (g->next != hwif)
366 g = g->next;
367 g->next = hwif->next;
368 if (hwgroup->hwif == hwif) {
369 /* Chose a random hwif for hwgroup->hwif.
370 * It's guaranteed that there are no drives
371 * left in the hwgroup.
373 BUG_ON(hwgroup->drive != NULL);
374 hwgroup->hwif = g;
376 BUG_ON(hwgroup->hwif == hwif);
378 spin_unlock_irq(&ide_lock);
381 /* Called with ide_lock held. */
382 static void __ide_port_unregister_devices(ide_hwif_t *hwif)
384 int i;
386 for (i = 0; i < MAX_DRIVES; i++) {
387 ide_drive_t *drive = &hwif->drives[i];
389 if (drive->present) {
390 spin_unlock_irq(&ide_lock);
391 device_unregister(&drive->gendev);
392 wait_for_completion(&drive->gendev_rel_comp);
393 spin_lock_irq(&ide_lock);
398 void ide_port_unregister_devices(ide_hwif_t *hwif)
400 mutex_lock(&ide_cfg_mtx);
401 spin_lock_irq(&ide_lock);
402 __ide_port_unregister_devices(hwif);
403 hwif->present = 0;
404 ide_port_init_devices_data(hwif);
405 spin_unlock_irq(&ide_lock);
406 mutex_unlock(&ide_cfg_mtx);
408 EXPORT_SYMBOL_GPL(ide_port_unregister_devices);
411 * ide_unregister - free an IDE interface
412 * @index: index of interface (will change soon to a pointer)
414 * Perform the final unregister of an IDE interface. At the moment
415 * we don't refcount interfaces so this will also get split up.
417 * Locking:
418 * The caller must not hold the IDE locks
419 * The drive present/vanishing is not yet properly locked
420 * Take care with the callbacks. These have been split to avoid
421 * deadlocking the IDE layer. The shutdown callback is called
422 * before we take the lock and free resources. It is up to the
423 * caller to be sure there is no pending I/O here, and that
424 * the interface will not be reopened (present/vanishing locking
425 * isn't yet done BTW). After we commit to the final kill we
426 * call the cleanup callback with the ide locks held.
428 * Unregister restores the hwif structures to the default state.
429 * This is raving bonkers.
432 void ide_unregister(unsigned int index)
434 ide_hwif_t *hwif, *g;
435 ide_hwgroup_t *hwgroup;
436 int irq_count = 0;
438 BUG_ON(index >= MAX_HWIFS);
440 BUG_ON(in_interrupt());
441 BUG_ON(irqs_disabled());
442 mutex_lock(&ide_cfg_mtx);
443 spin_lock_irq(&ide_lock);
444 hwif = &ide_hwifs[index];
445 if (!hwif->present)
446 goto abort;
447 __ide_port_unregister_devices(hwif);
448 hwif->present = 0;
450 spin_unlock_irq(&ide_lock);
452 ide_proc_unregister_port(hwif);
454 hwgroup = hwif->hwgroup;
456 * free the irq if we were the only hwif using it
458 g = hwgroup->hwif;
459 do {
460 if (g->irq == hwif->irq)
461 ++irq_count;
462 g = g->next;
463 } while (g != hwgroup->hwif);
464 if (irq_count == 1)
465 free_irq(hwif->irq, hwgroup);
467 ide_remove_port_from_hwgroup(hwif);
469 device_unregister(hwif->portdev);
470 device_unregister(&hwif->gendev);
471 wait_for_completion(&hwif->gendev_rel_comp);
474 * Remove us from the kernel's knowledge
476 blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
477 kfree(hwif->sg_table);
478 unregister_blkdev(hwif->major, hwif->name);
479 spin_lock_irq(&ide_lock);
481 if (hwif->dma_base)
482 (void)ide_release_dma(hwif);
484 ide_hwif_release_regions(hwif);
486 /* restore hwif data to pristine status */
487 ide_init_port_data(hwif, index);
489 abort:
490 spin_unlock_irq(&ide_lock);
491 mutex_unlock(&ide_cfg_mtx);
494 EXPORT_SYMBOL(ide_unregister);
496 void ide_init_port_hw(ide_hwif_t *hwif, hw_regs_t *hw)
498 memcpy(hwif->io_ports, hw->io_ports, sizeof(hwif->io_ports));
499 hwif->irq = hw->irq;
500 hwif->noprobe = 0;
501 hwif->chipset = hw->chipset;
502 hwif->gendev.parent = hw->dev;
503 hwif->ack_intr = hw->ack_intr;
505 EXPORT_SYMBOL_GPL(ide_init_port_hw);
508 * Locks for IDE setting functionality
511 DEFINE_MUTEX(ide_setting_mtx);
513 EXPORT_SYMBOL_GPL(ide_setting_mtx);
516 * ide_spin_wait_hwgroup - wait for group
517 * @drive: drive in the group
519 * Wait for an IDE device group to go non busy and then return
520 * holding the ide_lock which guards the hwgroup->busy status
521 * and right to use it.
524 int ide_spin_wait_hwgroup (ide_drive_t *drive)
526 ide_hwgroup_t *hwgroup = HWGROUP(drive);
527 unsigned long timeout = jiffies + (3 * HZ);
529 spin_lock_irq(&ide_lock);
531 while (hwgroup->busy) {
532 unsigned long lflags;
533 spin_unlock_irq(&ide_lock);
534 local_irq_set(lflags);
535 if (time_after(jiffies, timeout)) {
536 local_irq_restore(lflags);
537 printk(KERN_ERR "%s: channel busy\n", drive->name);
538 return -EBUSY;
540 local_irq_restore(lflags);
541 spin_lock_irq(&ide_lock);
543 return 0;
546 EXPORT_SYMBOL(ide_spin_wait_hwgroup);
548 int set_io_32bit(ide_drive_t *drive, int arg)
550 if (drive->no_io_32bit)
551 return -EPERM;
553 if (arg < 0 || arg > 1 + (SUPPORT_VLB_SYNC << 1))
554 return -EINVAL;
556 if (ide_spin_wait_hwgroup(drive))
557 return -EBUSY;
559 drive->io_32bit = arg;
561 spin_unlock_irq(&ide_lock);
563 return 0;
566 static int set_ksettings(ide_drive_t *drive, int arg)
568 if (arg < 0 || arg > 1)
569 return -EINVAL;
571 if (ide_spin_wait_hwgroup(drive))
572 return -EBUSY;
573 drive->keep_settings = arg;
574 spin_unlock_irq(&ide_lock);
576 return 0;
579 int set_using_dma(ide_drive_t *drive, int arg)
581 #ifdef CONFIG_BLK_DEV_IDEDMA
582 ide_hwif_t *hwif = drive->hwif;
583 int err = -EPERM;
585 if (arg < 0 || arg > 1)
586 return -EINVAL;
588 if (!drive->id || !(drive->id->capability & 1))
589 goto out;
591 if (hwif->dma_host_set == NULL)
592 goto out;
594 err = -EBUSY;
595 if (ide_spin_wait_hwgroup(drive))
596 goto out;
598 * set ->busy flag, unlock and let it ride
600 hwif->hwgroup->busy = 1;
601 spin_unlock_irq(&ide_lock);
603 err = 0;
605 if (arg) {
606 if (ide_set_dma(drive))
607 err = -EIO;
608 } else
609 ide_dma_off(drive);
612 * lock, clear ->busy flag and unlock before leaving
614 spin_lock_irq(&ide_lock);
615 hwif->hwgroup->busy = 0;
616 spin_unlock_irq(&ide_lock);
617 out:
618 return err;
619 #else
620 if (arg < 0 || arg > 1)
621 return -EINVAL;
623 return -EPERM;
624 #endif
627 int set_pio_mode(ide_drive_t *drive, int arg)
629 struct request rq;
631 if (arg < 0 || arg > 255)
632 return -EINVAL;
634 if (drive->hwif->set_pio_mode == NULL)
635 return -ENOSYS;
637 if (drive->special.b.set_tune)
638 return -EBUSY;
640 ide_init_drive_cmd(&rq);
641 rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
643 drive->tune_req = (u8) arg;
644 drive->special.b.set_tune = 1;
645 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
646 return 0;
649 static int set_unmaskirq(ide_drive_t *drive, int arg)
651 if (drive->no_unmask)
652 return -EPERM;
654 if (arg < 0 || arg > 1)
655 return -EINVAL;
657 if (ide_spin_wait_hwgroup(drive))
658 return -EBUSY;
659 drive->unmask = arg;
660 spin_unlock_irq(&ide_lock);
662 return 0;
666 * system_bus_clock - clock guess
668 * External version of the bus clock guess used by very old IDE drivers
669 * for things like VLB timings. Should not be used.
672 int system_bus_clock (void)
674 return system_bus_speed;
677 EXPORT_SYMBOL(system_bus_clock);
679 static int generic_ide_suspend(struct device *dev, pm_message_t mesg)
681 ide_drive_t *drive = dev->driver_data;
682 ide_hwif_t *hwif = HWIF(drive);
683 struct request rq;
684 struct request_pm_state rqpm;
685 ide_task_t args;
686 int ret;
688 /* Call ACPI _GTM only once */
689 if (!(drive->dn % 2))
690 ide_acpi_get_timing(hwif);
692 memset(&rq, 0, sizeof(rq));
693 memset(&rqpm, 0, sizeof(rqpm));
694 memset(&args, 0, sizeof(args));
695 rq.cmd_type = REQ_TYPE_PM_SUSPEND;
696 rq.special = &args;
697 rq.data = &rqpm;
698 rqpm.pm_step = ide_pm_state_start_suspend;
699 if (mesg.event == PM_EVENT_PRETHAW)
700 mesg.event = PM_EVENT_FREEZE;
701 rqpm.pm_state = mesg.event;
703 ret = ide_do_drive_cmd(drive, &rq, ide_wait);
704 /* only call ACPI _PS3 after both drivers are suspended */
705 if (!ret && (((drive->dn % 2) && hwif->drives[0].present
706 && hwif->drives[1].present)
707 || !hwif->drives[0].present
708 || !hwif->drives[1].present))
709 ide_acpi_set_state(hwif, 0);
710 return ret;
713 static int generic_ide_resume(struct device *dev)
715 ide_drive_t *drive = dev->driver_data;
716 ide_hwif_t *hwif = HWIF(drive);
717 struct request rq;
718 struct request_pm_state rqpm;
719 ide_task_t args;
720 int err;
722 /* Call ACPI _STM only once */
723 if (!(drive->dn % 2)) {
724 ide_acpi_set_state(hwif, 1);
725 ide_acpi_push_timing(hwif);
728 ide_acpi_exec_tfs(drive);
730 memset(&rq, 0, sizeof(rq));
731 memset(&rqpm, 0, sizeof(rqpm));
732 memset(&args, 0, sizeof(args));
733 rq.cmd_type = REQ_TYPE_PM_RESUME;
734 rq.special = &args;
735 rq.data = &rqpm;
736 rqpm.pm_step = ide_pm_state_start_resume;
737 rqpm.pm_state = PM_EVENT_ON;
739 err = ide_do_drive_cmd(drive, &rq, ide_head_wait);
741 if (err == 0 && dev->driver) {
742 ide_driver_t *drv = to_ide_driver(dev->driver);
744 if (drv->resume)
745 drv->resume(drive);
748 return err;
751 int generic_ide_ioctl(ide_drive_t *drive, struct file *file, struct block_device *bdev,
752 unsigned int cmd, unsigned long arg)
754 unsigned long flags;
755 ide_driver_t *drv;
756 void __user *p = (void __user *)arg;
757 int err = 0, (*setfunc)(ide_drive_t *, int);
758 u8 *val;
760 switch (cmd) {
761 case HDIO_GET_32BIT: val = &drive->io_32bit; goto read_val;
762 case HDIO_GET_KEEPSETTINGS: val = &drive->keep_settings; goto read_val;
763 case HDIO_GET_UNMASKINTR: val = &drive->unmask; goto read_val;
764 case HDIO_GET_DMA: val = &drive->using_dma; goto read_val;
765 case HDIO_SET_32BIT: setfunc = set_io_32bit; goto set_val;
766 case HDIO_SET_KEEPSETTINGS: setfunc = set_ksettings; goto set_val;
767 case HDIO_SET_PIO_MODE: setfunc = set_pio_mode; goto set_val;
768 case HDIO_SET_UNMASKINTR: setfunc = set_unmaskirq; goto set_val;
769 case HDIO_SET_DMA: setfunc = set_using_dma; goto set_val;
772 switch (cmd) {
773 case HDIO_OBSOLETE_IDENTITY:
774 case HDIO_GET_IDENTITY:
775 if (bdev != bdev->bd_contains)
776 return -EINVAL;
777 if (drive->id_read == 0)
778 return -ENOMSG;
779 if (copy_to_user(p, drive->id, (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
780 return -EFAULT;
781 return 0;
783 case HDIO_GET_NICE:
784 return put_user(drive->dsc_overlap << IDE_NICE_DSC_OVERLAP |
785 drive->atapi_overlap << IDE_NICE_ATAPI_OVERLAP |
786 drive->nice1 << IDE_NICE_1,
787 (long __user *) arg);
788 #ifdef CONFIG_IDE_TASK_IOCTL
789 case HDIO_DRIVE_TASKFILE:
790 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
791 return -EACCES;
792 switch(drive->media) {
793 case ide_disk:
794 return ide_taskfile_ioctl(drive, cmd, arg);
795 default:
796 return -ENOMSG;
798 #endif /* CONFIG_IDE_TASK_IOCTL */
800 case HDIO_DRIVE_CMD:
801 if (!capable(CAP_SYS_RAWIO))
802 return -EACCES;
803 return ide_cmd_ioctl(drive, cmd, arg);
805 case HDIO_DRIVE_TASK:
806 if (!capable(CAP_SYS_RAWIO))
807 return -EACCES;
808 return ide_task_ioctl(drive, cmd, arg);
809 case HDIO_SET_NICE:
810 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
811 if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
812 return -EPERM;
813 drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
814 drv = *(ide_driver_t **)bdev->bd_disk->private_data;
815 if (drive->dsc_overlap && !drv->supports_dsc_overlap) {
816 drive->dsc_overlap = 0;
817 return -EPERM;
819 drive->nice1 = (arg >> IDE_NICE_1) & 1;
820 return 0;
821 case HDIO_DRIVE_RESET:
822 if (!capable(CAP_SYS_ADMIN))
823 return -EACCES;
826 * Abort the current command on the
827 * group if there is one, taking
828 * care not to allow anything else
829 * to be queued and to die on the
830 * spot if we miss one somehow
833 spin_lock_irqsave(&ide_lock, flags);
835 if (HWGROUP(drive)->resetting) {
836 spin_unlock_irqrestore(&ide_lock, flags);
837 return -EBUSY;
840 ide_abort(drive, "drive reset");
842 BUG_ON(HWGROUP(drive)->handler);
844 /* Ensure nothing gets queued after we
845 drop the lock. Reset will clear the busy */
847 HWGROUP(drive)->busy = 1;
848 spin_unlock_irqrestore(&ide_lock, flags);
849 (void) ide_do_reset(drive);
851 return 0;
852 case HDIO_GET_BUSSTATE:
853 if (!capable(CAP_SYS_ADMIN))
854 return -EACCES;
855 if (put_user(HWIF(drive)->bus_state, (long __user *)arg))
856 return -EFAULT;
857 return 0;
859 case HDIO_SET_BUSSTATE:
860 if (!capable(CAP_SYS_ADMIN))
861 return -EACCES;
862 return -EOPNOTSUPP;
863 default:
864 return -EINVAL;
867 read_val:
868 mutex_lock(&ide_setting_mtx);
869 spin_lock_irqsave(&ide_lock, flags);
870 err = *val;
871 spin_unlock_irqrestore(&ide_lock, flags);
872 mutex_unlock(&ide_setting_mtx);
873 return err >= 0 ? put_user(err, (long __user *)arg) : err;
875 set_val:
876 if (bdev != bdev->bd_contains)
877 err = -EINVAL;
878 else {
879 if (!capable(CAP_SYS_ADMIN))
880 err = -EACCES;
881 else {
882 mutex_lock(&ide_setting_mtx);
883 err = setfunc(drive, arg);
884 mutex_unlock(&ide_setting_mtx);
887 return err;
890 EXPORT_SYMBOL(generic_ide_ioctl);
893 * stridx() returns the offset of c within s,
894 * or -1 if c is '\0' or not found within s.
896 static int __init stridx (const char *s, char c)
898 char *i = strchr(s, c);
899 return (i && c) ? i - s : -1;
903 * match_parm() does parsing for ide_setup():
905 * 1. the first char of s must be '='.
906 * 2. if the remainder matches one of the supplied keywords,
907 * the index (1 based) of the keyword is negated and returned.
908 * 3. if the remainder is a series of no more than max_vals numbers
909 * separated by commas, the numbers are saved in vals[] and a
910 * count of how many were saved is returned. Base10 is assumed,
911 * and base16 is allowed when prefixed with "0x".
912 * 4. otherwise, zero is returned.
914 static int __init match_parm (char *s, const char *keywords[], int vals[], int max_vals)
916 static const char *decimal = "0123456789";
917 static const char *hex = "0123456789abcdef";
918 int i, n;
920 if (*s++ == '=') {
922 * Try matching against the supplied keywords,
923 * and return -(index+1) if we match one
925 if (keywords != NULL) {
926 for (i = 0; *keywords != NULL; ++i) {
927 if (!strcmp(s, *keywords++))
928 return -(i+1);
932 * Look for a series of no more than "max_vals"
933 * numeric values separated by commas, in base10,
934 * or base16 when prefixed with "0x".
935 * Return a count of how many were found.
937 for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
938 vals[n] = i;
939 while ((i = stridx(decimal, *++s)) >= 0)
940 vals[n] = (vals[n] * 10) + i;
941 if (*s == 'x' && !vals[n]) {
942 while ((i = stridx(hex, *++s)) >= 0)
943 vals[n] = (vals[n] * 0x10) + i;
945 if (++n == max_vals)
946 break;
947 if (*s == ',' || *s == ';')
948 ++s;
950 if (!*s)
951 return n;
953 return 0; /* zero = nothing matched */
956 extern int probe_ali14xx;
957 extern int probe_umc8672;
958 extern int probe_dtc2278;
959 extern int probe_ht6560b;
960 extern int probe_qd65xx;
961 extern int cmd640_vlb;
962 extern int probe_4drives;
964 static int __initdata is_chipset_set;
967 * ide_setup() gets called VERY EARLY during initialization,
968 * to handle kernel "command line" strings beginning with "hdx=" or "ide".
970 * Remember to update Documentation/ide/ide.txt if you change something here.
972 static int __init ide_setup(char *s)
974 int i, vals[3];
975 ide_hwif_t *hwif;
976 ide_drive_t *drive;
977 unsigned int hw, unit;
978 const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
979 const char max_hwif = '0' + (MAX_HWIFS - 1);
982 if (strncmp(s,"hd",2) == 0 && s[2] == '=') /* hd= is for hd.c */
983 return 0; /* driver and not us */
985 if (strncmp(s,"ide",3) && strncmp(s,"idebus",6) && strncmp(s,"hd",2))
986 return 0;
988 printk(KERN_INFO "ide_setup: %s", s);
989 init_ide_data ();
991 #ifdef CONFIG_BLK_DEV_IDEDOUBLER
992 if (!strcmp(s, "ide=doubler")) {
993 extern int ide_doubler;
995 printk(" : Enabled support for IDE doublers\n");
996 ide_doubler = 1;
997 return 1;
999 #endif /* CONFIG_BLK_DEV_IDEDOUBLER */
1001 if (!strcmp(s, "ide=nodma")) {
1002 printk(" : Prevented DMA\n");
1003 noautodma = 1;
1004 goto obsolete_option;
1007 #ifdef CONFIG_BLK_DEV_IDEACPI
1008 if (!strcmp(s, "ide=noacpi")) {
1009 //printk(" : Disable IDE ACPI support.\n");
1010 ide_noacpi = 1;
1011 return 1;
1013 if (!strcmp(s, "ide=acpigtf")) {
1014 //printk(" : Enable IDE ACPI _GTF support.\n");
1015 ide_noacpitfs = 0;
1016 return 1;
1018 if (!strcmp(s, "ide=acpionboot")) {
1019 //printk(" : Call IDE ACPI methods on boot.\n");
1020 ide_noacpionboot = 0;
1021 return 1;
1023 #endif /* CONFIG_BLK_DEV_IDEACPI */
1026 * Look for drive options: "hdx="
1028 if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
1029 const char *hd_words[] = {
1030 "none", "noprobe", "nowerr", "cdrom", "nodma",
1031 "autotune", "noautotune", "-8", "-9", "-10",
1032 "noflush", "remap", "remap63", "scsi", NULL };
1033 unit = s[2] - 'a';
1034 hw = unit / MAX_DRIVES;
1035 unit = unit % MAX_DRIVES;
1036 hwif = &ide_hwifs[hw];
1037 drive = &hwif->drives[unit];
1038 if (strncmp(s + 4, "ide-", 4) == 0) {
1039 strlcpy(drive->driver_req, s + 4, sizeof(drive->driver_req));
1040 goto obsolete_option;
1042 switch (match_parm(&s[3], hd_words, vals, 3)) {
1043 case -1: /* "none" */
1044 case -2: /* "noprobe" */
1045 drive->noprobe = 1;
1046 goto done;
1047 case -3: /* "nowerr" */
1048 drive->bad_wstat = BAD_R_STAT;
1049 hwif->noprobe = 0;
1050 goto done;
1051 case -4: /* "cdrom" */
1052 drive->present = 1;
1053 drive->media = ide_cdrom;
1054 /* an ATAPI device ignores DRDY */
1055 drive->ready_stat = 0;
1056 hwif->noprobe = 0;
1057 goto done;
1058 case -5: /* nodma */
1059 drive->nodma = 1;
1060 goto done;
1061 case -6: /* "autotune" */
1062 drive->autotune = IDE_TUNE_AUTO;
1063 goto obsolete_option;
1064 case -7: /* "noautotune" */
1065 drive->autotune = IDE_TUNE_NOAUTO;
1066 goto obsolete_option;
1067 case -11: /* noflush */
1068 drive->noflush = 1;
1069 goto done;
1070 case -12: /* "remap" */
1071 drive->remap_0_to_1 = 1;
1072 goto obsolete_option;
1073 case -13: /* "remap63" */
1074 drive->sect0 = 63;
1075 goto obsolete_option;
1076 case -14: /* "scsi" */
1077 drive->scsi = 1;
1078 goto obsolete_option;
1079 case 3: /* cyl,head,sect */
1080 drive->media = ide_disk;
1081 drive->ready_stat = READY_STAT;
1082 drive->cyl = drive->bios_cyl = vals[0];
1083 drive->head = drive->bios_head = vals[1];
1084 drive->sect = drive->bios_sect = vals[2];
1085 drive->present = 1;
1086 drive->forced_geom = 1;
1087 hwif->noprobe = 0;
1088 goto done;
1089 default:
1090 goto bad_option;
1094 if (s[0] != 'i' || s[1] != 'd' || s[2] != 'e')
1095 goto bad_option;
1097 * Look for bus speed option: "idebus="
1099 if (s[3] == 'b' && s[4] == 'u' && s[5] == 's') {
1100 if (match_parm(&s[6], NULL, vals, 1) != 1)
1101 goto bad_option;
1102 if (vals[0] >= 20 && vals[0] <= 66) {
1103 idebus_parameter = vals[0];
1104 } else
1105 printk(" -- BAD BUS SPEED! Expected value from 20 to 66");
1106 goto done;
1109 * Look for interface options: "idex="
1111 if (s[3] >= '0' && s[3] <= max_hwif) {
1113 * Be VERY CAREFUL changing this: note hardcoded indexes below
1114 * (-8, -9, -10) are reserved to ease the hardcoding.
1116 static const char *ide_words[] = {
1117 "minus1", "serialize", "minus3", "minus4",
1118 "reset", "minus6", "ata66", "minus8", "minus9",
1119 "minus10", "four", "qd65xx", "ht6560b", "cmd640_vlb",
1120 "dtc2278", "umc8672", "ali14xx", NULL };
1122 hw = s[3] - '0';
1123 hwif = &ide_hwifs[hw];
1124 i = match_parm(&s[4], ide_words, vals, 3);
1127 * Cryptic check to ensure chipset not already set for hwif.
1128 * Note: we can't depend on hwif->chipset here.
1130 if (i >= -18 && i <= -11) {
1131 /* chipset already specified */
1132 if (is_chipset_set)
1133 goto bad_option;
1134 /* these drivers are for "ide0=" only */
1135 if (hw != 0)
1136 goto bad_hwif;
1137 is_chipset_set = 1;
1138 printk("\n");
1141 switch (i) {
1142 #ifdef CONFIG_BLK_DEV_ALI14XX
1143 case -17: /* "ali14xx" */
1144 probe_ali14xx = 1;
1145 goto obsolete_option;
1146 #endif
1147 #ifdef CONFIG_BLK_DEV_UMC8672
1148 case -16: /* "umc8672" */
1149 probe_umc8672 = 1;
1150 goto obsolete_option;
1151 #endif
1152 #ifdef CONFIG_BLK_DEV_DTC2278
1153 case -15: /* "dtc2278" */
1154 probe_dtc2278 = 1;
1155 goto obsolete_option;
1156 #endif
1157 #ifdef CONFIG_BLK_DEV_CMD640
1158 case -14: /* "cmd640_vlb" */
1159 cmd640_vlb = 1;
1160 goto obsolete_option;
1161 #endif
1162 #ifdef CONFIG_BLK_DEV_HT6560B
1163 case -13: /* "ht6560b" */
1164 probe_ht6560b = 1;
1165 goto obsolete_option;
1166 #endif
1167 #ifdef CONFIG_BLK_DEV_QD65XX
1168 case -12: /* "qd65xx" */
1169 probe_qd65xx = 1;
1170 goto obsolete_option;
1171 #endif
1172 #ifdef CONFIG_BLK_DEV_4DRIVES
1173 case -11: /* "four" drives on one set of ports */
1174 probe_4drives = 1;
1175 goto obsolete_option;
1176 #endif
1177 case -10: /* minus10 */
1178 case -9: /* minus9 */
1179 case -8: /* minus8 */
1180 case -6:
1181 case -4:
1182 case -3:
1183 goto bad_option;
1184 case -7: /* ata66 */
1185 #ifdef CONFIG_BLK_DEV_IDEPCI
1187 * Use ATA_CBL_PATA40_SHORT so drive side
1188 * cable detection is also overriden.
1190 hwif->cbl = ATA_CBL_PATA40_SHORT;
1191 goto obsolete_option;
1192 #else
1193 goto bad_hwif;
1194 #endif
1195 case -5: /* "reset" */
1196 hwif->reset = 1;
1197 goto obsolete_option;
1198 case -2: /* "serialize" */
1199 hwif->mate = &ide_hwifs[hw^1];
1200 hwif->mate->mate = hwif;
1201 hwif->serialized = hwif->mate->serialized = 1;
1202 goto obsolete_option;
1204 case -1:
1205 case 0:
1206 case 1:
1207 case 2:
1208 case 3:
1209 goto bad_option;
1210 default:
1211 printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
1212 return 1;
1215 bad_option:
1216 printk(" -- BAD OPTION\n");
1217 return 1;
1218 obsolete_option:
1219 printk(" -- OBSOLETE OPTION, WILL BE REMOVED SOON!\n");
1220 return 1;
1221 bad_hwif:
1222 printk("-- NOT SUPPORTED ON ide%d", hw);
1223 done:
1224 printk("\n");
1225 return 1;
1228 EXPORT_SYMBOL(ide_lock);
1230 static int ide_bus_match(struct device *dev, struct device_driver *drv)
1232 return 1;
1235 static char *media_string(ide_drive_t *drive)
1237 switch (drive->media) {
1238 case ide_disk:
1239 return "disk";
1240 case ide_cdrom:
1241 return "cdrom";
1242 case ide_tape:
1243 return "tape";
1244 case ide_floppy:
1245 return "floppy";
1246 case ide_optical:
1247 return "optical";
1248 default:
1249 return "UNKNOWN";
1253 static ssize_t media_show(struct device *dev, struct device_attribute *attr, char *buf)
1255 ide_drive_t *drive = to_ide_device(dev);
1256 return sprintf(buf, "%s\n", media_string(drive));
1259 static ssize_t drivename_show(struct device *dev, struct device_attribute *attr, char *buf)
1261 ide_drive_t *drive = to_ide_device(dev);
1262 return sprintf(buf, "%s\n", drive->name);
1265 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
1267 ide_drive_t *drive = to_ide_device(dev);
1268 return sprintf(buf, "ide:m-%s\n", media_string(drive));
1271 static ssize_t model_show(struct device *dev, struct device_attribute *attr,
1272 char *buf)
1274 ide_drive_t *drive = to_ide_device(dev);
1275 return sprintf(buf, "%s\n", drive->id->model);
1278 static ssize_t firmware_show(struct device *dev, struct device_attribute *attr,
1279 char *buf)
1281 ide_drive_t *drive = to_ide_device(dev);
1282 return sprintf(buf, "%s\n", drive->id->fw_rev);
1285 static ssize_t serial_show(struct device *dev, struct device_attribute *attr,
1286 char *buf)
1288 ide_drive_t *drive = to_ide_device(dev);
1289 return sprintf(buf, "%s\n", drive->id->serial_no);
1292 static struct device_attribute ide_dev_attrs[] = {
1293 __ATTR_RO(media),
1294 __ATTR_RO(drivename),
1295 __ATTR_RO(modalias),
1296 __ATTR_RO(model),
1297 __ATTR_RO(firmware),
1298 __ATTR(serial, 0400, serial_show, NULL),
1299 __ATTR_NULL
1302 static int ide_uevent(struct device *dev, struct kobj_uevent_env *env)
1304 ide_drive_t *drive = to_ide_device(dev);
1306 add_uevent_var(env, "MEDIA=%s", media_string(drive));
1307 add_uevent_var(env, "DRIVENAME=%s", drive->name);
1308 add_uevent_var(env, "MODALIAS=ide:m-%s", media_string(drive));
1309 return 0;
1312 static int generic_ide_probe(struct device *dev)
1314 ide_drive_t *drive = to_ide_device(dev);
1315 ide_driver_t *drv = to_ide_driver(dev->driver);
1317 return drv->probe ? drv->probe(drive) : -ENODEV;
1320 static int generic_ide_remove(struct device *dev)
1322 ide_drive_t *drive = to_ide_device(dev);
1323 ide_driver_t *drv = to_ide_driver(dev->driver);
1325 if (drv->remove)
1326 drv->remove(drive);
1328 return 0;
1331 static void generic_ide_shutdown(struct device *dev)
1333 ide_drive_t *drive = to_ide_device(dev);
1334 ide_driver_t *drv = to_ide_driver(dev->driver);
1336 if (dev->driver && drv->shutdown)
1337 drv->shutdown(drive);
1340 struct bus_type ide_bus_type = {
1341 .name = "ide",
1342 .match = ide_bus_match,
1343 .uevent = ide_uevent,
1344 .probe = generic_ide_probe,
1345 .remove = generic_ide_remove,
1346 .shutdown = generic_ide_shutdown,
1347 .dev_attrs = ide_dev_attrs,
1348 .suspend = generic_ide_suspend,
1349 .resume = generic_ide_resume,
1352 EXPORT_SYMBOL_GPL(ide_bus_type);
1354 static void ide_port_class_release(struct device *portdev)
1356 ide_hwif_t *hwif = dev_get_drvdata(portdev);
1358 put_device(&hwif->gendev);
1362 * This is gets invoked once during initialization, to set *everything* up
1364 static int __init ide_init(void)
1366 int ret;
1368 printk(KERN_INFO "Uniform Multi-Platform E-IDE driver\n");
1369 system_bus_speed = ide_system_bus_speed();
1371 printk(KERN_INFO "ide: Assuming %dMHz system bus speed "
1372 "for PIO modes%s\n", system_bus_speed,
1373 idebus_parameter ? "" : "; override with idebus=xx");
1375 ret = bus_register(&ide_bus_type);
1376 if (ret < 0) {
1377 printk(KERN_WARNING "IDE: bus_register error: %d\n", ret);
1378 return ret;
1381 ide_port_class = class_create(THIS_MODULE, "ide_port");
1382 if (IS_ERR(ide_port_class)) {
1383 ret = PTR_ERR(ide_port_class);
1384 goto out_port_class;
1386 ide_port_class->dev_release = ide_port_class_release;
1388 init_ide_data();
1390 proc_ide_create();
1392 return 0;
1394 out_port_class:
1395 bus_unregister(&ide_bus_type);
1397 return ret;
1400 #ifdef MODULE
1401 static char *options = NULL;
1402 module_param(options, charp, 0);
1403 MODULE_LICENSE("GPL");
1405 static void __init parse_options (char *line)
1407 char *next = line;
1409 if (line == NULL || !*line)
1410 return;
1411 while ((line = next) != NULL) {
1412 if ((next = strchr(line,' ')) != NULL)
1413 *next++ = 0;
1414 if (!ide_setup(line))
1415 printk (KERN_INFO "Unknown option '%s'\n", line);
1419 int __init init_module (void)
1421 parse_options(options);
1422 return ide_init();
1425 void __exit cleanup_module (void)
1427 int index;
1429 for (index = 0; index < MAX_HWIFS; ++index)
1430 ide_unregister(index);
1432 proc_ide_destroy();
1434 class_destroy(ide_port_class);
1436 bus_unregister(&ide_bus_type);
1439 #else /* !MODULE */
1441 __setup("", ide_setup);
1443 module_init(ide_init);
1445 #endif /* MODULE */