2 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
3 * Copyright (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 #include <linux/module.h>
48 #include <linux/types.h>
49 #include <linux/string.h>
50 #include <linux/kernel.h>
51 #include <linux/interrupt.h>
52 #include <linux/major.h>
53 #include <linux/errno.h>
54 #include <linux/genhd.h>
55 #include <linux/slab.h>
56 #include <linux/init.h>
57 #include <linux/pci.h>
58 #include <linux/ide.h>
59 #include <linux/hdreg.h>
60 #include <linux/completion.h>
61 #include <linux/device.h>
63 struct class *ide_port_class;
66 * Locks for IDE setting functionality
69 DEFINE_MUTEX(ide_setting_mtx);
71 ide_devset_get(io_32bit, io_32bit);
73 static int set_io_32bit(ide_drive_t *drive, int arg)
75 if (drive->dev_flags & IDE_DFLAG_NO_IO_32BIT)
78 if (arg < 0 || arg > 1 + (SUPPORT_VLB_SYNC << 1))
81 drive->io_32bit = arg;
86 ide_devset_get_flag(ksettings, IDE_DFLAG_KEEP_SETTINGS);
88 static int set_ksettings(ide_drive_t *drive, int arg)
90 if (arg < 0 || arg > 1)
94 drive->dev_flags |= IDE_DFLAG_KEEP_SETTINGS;
96 drive->dev_flags &= ~IDE_DFLAG_KEEP_SETTINGS;
101 ide_devset_get_flag(using_dma, IDE_DFLAG_USING_DMA);
103 static int set_using_dma(ide_drive_t *drive, int arg)
105 #ifdef CONFIG_BLK_DEV_IDEDMA
108 if (arg < 0 || arg > 1)
111 if (ata_id_has_dma(drive->id) == 0)
114 if (drive->hwif->dma_ops == NULL)
120 if (ide_set_dma(drive))
128 if (arg < 0 || arg > 1)
136 * handle HDIO_SET_PIO_MODE ioctl abusers here, eventually it will go away
138 static int set_pio_mode_abuse(ide_hwif_t *hwif, u8 req_pio)
147 return (hwif->host_flags & IDE_HFLAG_ABUSE_DMA_MODES) ? 1 : 0;
150 return (hwif->host_flags & IDE_HFLAG_ABUSE_PREFETCH) ? 1 : 0;
153 return (hwif->host_flags & IDE_HFLAG_ABUSE_FAST_DEVSEL) ? 1 : 0;
159 static int set_pio_mode(ide_drive_t *drive, int arg)
161 ide_hwif_t *hwif = drive->hwif;
162 const struct ide_port_ops *port_ops = hwif->port_ops;
164 if (arg < 0 || arg > 255)
167 if (port_ops == NULL || port_ops->set_pio_mode == NULL ||
168 (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
171 if (set_pio_mode_abuse(drive->hwif, arg)) {
172 if (arg == 8 || arg == 9) {
175 /* take lock for IDE_DFLAG_[NO_]UNMASK/[NO_]IO_32BIT */
176 spin_lock_irqsave(&hwif->lock, flags);
177 port_ops->set_pio_mode(drive, arg);
178 spin_unlock_irqrestore(&hwif->lock, flags);
180 port_ops->set_pio_mode(drive, arg);
182 int keep_dma = !!(drive->dev_flags & IDE_DFLAG_USING_DMA);
184 ide_set_pio(drive, arg);
186 if (hwif->host_flags & IDE_HFLAG_SET_PIO_MODE_KEEP_DMA) {
195 ide_devset_get_flag(unmaskirq, IDE_DFLAG_UNMASK);
197 static int set_unmaskirq(ide_drive_t *drive, int arg)
199 if (drive->dev_flags & IDE_DFLAG_NO_UNMASK)
202 if (arg < 0 || arg > 1)
206 drive->dev_flags |= IDE_DFLAG_UNMASK;
208 drive->dev_flags &= ~IDE_DFLAG_UNMASK;
213 ide_ext_devset_rw_sync(io_32bit, io_32bit);
214 ide_ext_devset_rw_sync(keepsettings, ksettings);
215 ide_ext_devset_rw_sync(unmaskirq, unmaskirq);
216 ide_ext_devset_rw_sync(using_dma, using_dma);
217 __IDE_DEVSET(pio_mode, DS_SYNC, NULL, set_pio_mode);
220 * ide_device_get - get an additional reference to a ide_drive_t
221 * @drive: device to get a reference to
223 * Gets a reference to the ide_drive_t and increments the use count of the
224 * underlying LLDD module.
226 int ide_device_get(ide_drive_t *drive)
228 struct device *host_dev;
229 struct module *module;
231 if (!get_device(&drive->gendev))
234 host_dev = drive->hwif->host->dev[0];
235 module = host_dev ? host_dev->driver->owner : NULL;
237 if (module && !try_module_get(module)) {
238 put_device(&drive->gendev);
244 EXPORT_SYMBOL_GPL(ide_device_get);
247 * ide_device_put - release a reference to a ide_drive_t
248 * @drive: device to release a reference on
250 * Release a reference to the ide_drive_t and decrements the use count of
251 * the underlying LLDD module.
253 void ide_device_put(ide_drive_t *drive)
255 #ifdef CONFIG_MODULE_UNLOAD
256 struct device *host_dev = drive->hwif->host->dev[0];
257 struct module *module = host_dev ? host_dev->driver->owner : NULL;
262 put_device(&drive->gendev);
264 EXPORT_SYMBOL_GPL(ide_device_put);
266 static int ide_bus_match(struct device *dev, struct device_driver *drv)
271 static int ide_uevent(struct device *dev, struct kobj_uevent_env *env)
273 ide_drive_t *drive = to_ide_device(dev);
275 add_uevent_var(env, "MEDIA=%s", ide_media_string(drive));
276 add_uevent_var(env, "DRIVENAME=%s", drive->name);
277 add_uevent_var(env, "MODALIAS=ide:m-%s", ide_media_string(drive));
281 static int generic_ide_probe(struct device *dev)
283 ide_drive_t *drive = to_ide_device(dev);
284 struct ide_driver *drv = to_ide_driver(dev->driver);
286 return drv->probe ? drv->probe(drive) : -ENODEV;
289 static int generic_ide_remove(struct device *dev)
291 ide_drive_t *drive = to_ide_device(dev);
292 struct ide_driver *drv = to_ide_driver(dev->driver);
300 static void generic_ide_shutdown(struct device *dev)
302 ide_drive_t *drive = to_ide_device(dev);
303 struct ide_driver *drv = to_ide_driver(dev->driver);
305 if (dev->driver && drv->shutdown)
306 drv->shutdown(drive);
309 struct bus_type ide_bus_type = {
311 .match = ide_bus_match,
312 .uevent = ide_uevent,
313 .probe = generic_ide_probe,
314 .remove = generic_ide_remove,
315 .shutdown = generic_ide_shutdown,
316 .dev_attrs = ide_dev_attrs,
317 .suspend = generic_ide_suspend,
318 .resume = generic_ide_resume,
321 EXPORT_SYMBOL_GPL(ide_bus_type);
324 EXPORT_SYMBOL_GPL(ide_vlb_clk);
326 module_param_named(vlb_clock, ide_vlb_clk, int, 0);
327 MODULE_PARM_DESC(vlb_clock, "VLB clock frequency (in MHz)");
330 EXPORT_SYMBOL_GPL(ide_pci_clk);
332 module_param_named(pci_clock, ide_pci_clk, int, 0);
333 MODULE_PARM_DESC(pci_clock, "PCI bus clock frequency (in MHz)");
335 static int ide_set_dev_param_mask(const char *s, struct kernel_param *kp)
338 unsigned int *dev_param_mask = (unsigned int *)kp->arg;
340 /* controller . device (0 or 1) [ : 1 (set) | 0 (clear) ] */
341 if (sscanf(s, "%d.%d:%d", &a, &b, &j) != 3 &&
342 sscanf(s, "%d.%d", &a, &b) != 2)
345 i = a * MAX_DRIVES + b;
347 if (i >= MAX_HWIFS * MAX_DRIVES || j < 0 || j > 1)
351 *dev_param_mask |= (1 << i);
353 *dev_param_mask &= ~(1 << i);
358 static unsigned int ide_nodma;
360 module_param_call(nodma, ide_set_dev_param_mask, NULL, &ide_nodma, 0);
361 MODULE_PARM_DESC(nodma, "disallow DMA for a device");
363 static unsigned int ide_noflush;
365 module_param_call(noflush, ide_set_dev_param_mask, NULL, &ide_noflush, 0);
366 MODULE_PARM_DESC(noflush, "disable flush requests for a device");
368 static unsigned int ide_noprobe;
370 module_param_call(noprobe, ide_set_dev_param_mask, NULL, &ide_noprobe, 0);
371 MODULE_PARM_DESC(noprobe, "skip probing for a device");
373 static unsigned int ide_nowerr;
375 module_param_call(nowerr, ide_set_dev_param_mask, NULL, &ide_nowerr, 0);
376 MODULE_PARM_DESC(nowerr, "ignore the ATA_DF bit for a device");
378 static unsigned int ide_cdroms;
380 module_param_call(cdrom, ide_set_dev_param_mask, NULL, &ide_cdroms, 0);
381 MODULE_PARM_DESC(cdrom, "force device as a CD-ROM");
389 static unsigned int ide_disks;
390 static struct chs_geom ide_disks_chs[MAX_HWIFS * MAX_DRIVES];
392 static int ide_set_disk_chs(const char *str, struct kernel_param *kp)
394 int a, b, c = 0, h = 0, s = 0, i, j = 1;
396 /* controller . device (0 or 1) : Cylinders , Heads , Sectors */
397 /* controller . device (0 or 1) : 1 (use CHS) | 0 (ignore CHS) */
398 if (sscanf(str, "%d.%d:%d,%d,%d", &a, &b, &c, &h, &s) != 5 &&
399 sscanf(str, "%d.%d:%d", &a, &b, &j) != 3)
402 i = a * MAX_DRIVES + b;
404 if (i >= MAX_HWIFS * MAX_DRIVES || j < 0 || j > 1)
407 if (c > INT_MAX || h > 255 || s > 255)
411 ide_disks |= (1 << i);
413 ide_disks &= ~(1 << i);
415 ide_disks_chs[i].cyl = c;
416 ide_disks_chs[i].head = h;
417 ide_disks_chs[i].sect = s;
422 module_param_call(chs, ide_set_disk_chs, NULL, NULL, 0);
423 MODULE_PARM_DESC(chs, "force device as a disk (using CHS)");
425 static void ide_dev_apply_params(ide_drive_t *drive, u8 unit)
427 int i = drive->hwif->index * MAX_DRIVES + unit;
429 if (ide_nodma & (1 << i)) {
430 printk(KERN_INFO "ide: disallowing DMA for %s\n", drive->name);
431 drive->dev_flags |= IDE_DFLAG_NODMA;
433 if (ide_noflush & (1 << i)) {
434 printk(KERN_INFO "ide: disabling flush requests for %s\n",
436 drive->dev_flags |= IDE_DFLAG_NOFLUSH;
438 if (ide_noprobe & (1 << i)) {
439 printk(KERN_INFO "ide: skipping probe for %s\n", drive->name);
440 drive->dev_flags |= IDE_DFLAG_NOPROBE;
442 if (ide_nowerr & (1 << i)) {
443 printk(KERN_INFO "ide: ignoring the ATA_DF bit for %s\n",
445 drive->bad_wstat = BAD_R_STAT;
447 if (ide_cdroms & (1 << i)) {
448 printk(KERN_INFO "ide: forcing %s as a CD-ROM\n", drive->name);
449 drive->dev_flags |= IDE_DFLAG_PRESENT;
450 drive->media = ide_cdrom;
451 /* an ATAPI device ignores DRDY */
452 drive->ready_stat = 0;
454 if (ide_disks & (1 << i)) {
455 drive->cyl = drive->bios_cyl = ide_disks_chs[i].cyl;
456 drive->head = drive->bios_head = ide_disks_chs[i].head;
457 drive->sect = drive->bios_sect = ide_disks_chs[i].sect;
459 printk(KERN_INFO "ide: forcing %s as a disk (%d/%d/%d)\n",
461 drive->cyl, drive->head, drive->sect);
463 drive->dev_flags |= IDE_DFLAG_FORCED_GEOM | IDE_DFLAG_PRESENT;
464 drive->media = ide_disk;
465 drive->ready_stat = ATA_DRDY;
469 static unsigned int ide_ignore_cable;
471 static int ide_set_ignore_cable(const char *s, struct kernel_param *kp)
475 /* controller (ignore) */
476 /* controller : 1 (ignore) | 0 (use) */
477 if (sscanf(s, "%d:%d", &i, &j) != 2 && sscanf(s, "%d", &i) != 1)
480 if (i >= MAX_HWIFS || j < 0 || j > 1)
484 ide_ignore_cable |= (1 << i);
486 ide_ignore_cable &= ~(1 << i);
491 module_param_call(ignore_cable, ide_set_ignore_cable, NULL, NULL, 0);
492 MODULE_PARM_DESC(ignore_cable, "ignore cable detection");
494 void ide_port_apply_params(ide_hwif_t *hwif)
499 if (ide_ignore_cable & (1 << hwif->index)) {
500 printk(KERN_INFO "ide: ignoring cable detection for %s\n",
502 hwif->cbl = ATA_CBL_PATA40_SHORT;
505 ide_port_for_each_dev(i, drive, hwif)
506 ide_dev_apply_params(drive, i);
510 * This is gets invoked once during initialization, to set *everything* up
512 static int __init ide_init(void)
516 printk(KERN_INFO "Uniform Multi-Platform E-IDE driver\n");
518 ret = bus_register(&ide_bus_type);
520 printk(KERN_WARNING "IDE: bus_register error: %d\n", ret);
524 ide_port_class = class_create(THIS_MODULE, "ide_port");
525 if (IS_ERR(ide_port_class)) {
526 ret = PTR_ERR(ide_port_class);
535 bus_unregister(&ide_bus_type);
540 static void __exit ide_exit(void)
544 class_destroy(ide_port_class);
546 bus_unregister(&ide_bus_type);
549 module_init(ide_init);
550 module_exit(ide_exit);
552 MODULE_LICENSE("GPL");