blob: 92c9b90931e73393f0d47b311b3fb1839483a1df [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Bartlomiej Zolnierkiewicz59bca8c2008-02-01 23:09:33 +01002 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
Pavel Machekfc410692008-07-23 19:56:02 +02003 * Copyright (C) 2003-2005, 2007 Bartlomiej Zolnierkiewicz
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
5
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>
10 *
11 * See linux/MAINTAINERS for address of current maintainer.
12 *
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.
17 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * ...
19 *
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).
35 *
36 * October, 1994 -- Complete line-by-line overhaul for linux 1.1.x, by:
37 *
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)
41 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 */
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/module.h>
48#include <linux/types.h>
49#include <linux/string.h>
50#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/interrupt.h>
52#include <linux/major.h>
53#include <linux/errno.h>
54#include <linux/genhd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/slab.h>
56#include <linux/init.h>
57#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/ide.h>
Bartlomiej Zolnierkiewicz3ceca722008-10-10 22:39:27 +020059#include <linux/hdreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/completion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +020063struct class *ide_port_class;
64
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +020065/**
66 * ide_device_get - get an additional reference to a ide_drive_t
67 * @drive: device to get a reference to
68 *
69 * Gets a reference to the ide_drive_t and increments the use count of the
70 * underlying LLDD module.
71 */
72int ide_device_get(ide_drive_t *drive)
73{
74 struct device *host_dev;
75 struct module *module;
76
77 if (!get_device(&drive->gendev))
78 return -ENXIO;
79
80 host_dev = drive->hwif->host->dev[0];
81 module = host_dev ? host_dev->driver->owner : NULL;
82
83 if (module && !try_module_get(module)) {
84 put_device(&drive->gendev);
85 return -ENXIO;
86 }
87
88 return 0;
89}
90EXPORT_SYMBOL_GPL(ide_device_get);
91
92/**
93 * ide_device_put - release a reference to a ide_drive_t
94 * @drive: device to release a reference on
95 *
96 * Release a reference to the ide_drive_t and decrements the use count of
97 * the underlying LLDD module.
98 */
99void ide_device_put(ide_drive_t *drive)
100{
101#ifdef CONFIG_MODULE_UNLOAD
102 struct device *host_dev = drive->hwif->host->dev[0];
103 struct module *module = host_dev ? host_dev->driver->owner : NULL;
104
105 if (module)
106 module_put(module);
107#endif
108 put_device(&drive->gendev);
109}
110EXPORT_SYMBOL_GPL(ide_device_put);
111
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200112static int ide_bus_match(struct device *dev, struct device_driver *drv)
113{
114 return 1;
115}
116
Kay Sievers7eff2e72007-08-14 15:15:12 +0200117static int ide_uevent(struct device *dev, struct kobj_uevent_env *env)
Kay Sievers263756e2005-12-12 18:03:44 +0100118{
119 ide_drive_t *drive = to_ide_device(dev);
Kay Sievers263756e2005-12-12 18:03:44 +0100120
Bartlomiej Zolnierkiewiczebdab072009-01-02 16:12:48 +0100121 add_uevent_var(env, "MEDIA=%s", ide_media_string(drive));
Kay Sievers7eff2e72007-08-14 15:15:12 +0200122 add_uevent_var(env, "DRIVENAME=%s", drive->name);
Bartlomiej Zolnierkiewiczebdab072009-01-02 16:12:48 +0100123 add_uevent_var(env, "MODALIAS=ide:m-%s", ide_media_string(drive));
Kay Sievers263756e2005-12-12 18:03:44 +0100124 return 0;
125}
126
Russell King4031bbe2006-01-06 11:41:00 +0000127static int generic_ide_probe(struct device *dev)
128{
129 ide_drive_t *drive = to_ide_device(dev);
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100130 struct ide_driver *drv = to_ide_driver(dev->driver);
Russell King4031bbe2006-01-06 11:41:00 +0000131
132 return drv->probe ? drv->probe(drive) : -ENODEV;
133}
134
135static int generic_ide_remove(struct device *dev)
136{
137 ide_drive_t *drive = to_ide_device(dev);
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100138 struct ide_driver *drv = to_ide_driver(dev->driver);
Russell King4031bbe2006-01-06 11:41:00 +0000139
140 if (drv->remove)
141 drv->remove(drive);
142
143 return 0;
144}
145
146static void generic_ide_shutdown(struct device *dev)
147{
148 ide_drive_t *drive = to_ide_device(dev);
Bartlomiej Zolnierkiewicz7f3c8682009-01-06 17:20:53 +0100149 struct ide_driver *drv = to_ide_driver(dev->driver);
Russell King4031bbe2006-01-06 11:41:00 +0000150
151 if (dev->driver && drv->shutdown)
152 drv->shutdown(drive);
153}
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155struct bus_type ide_bus_type = {
156 .name = "ide",
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200157 .match = ide_bus_match,
Kay Sievers263756e2005-12-12 18:03:44 +0100158 .uevent = ide_uevent,
Russell King4031bbe2006-01-06 11:41:00 +0000159 .probe = generic_ide_probe,
160 .remove = generic_ide_remove,
161 .shutdown = generic_ide_shutdown,
Kay Sievers263756e2005-12-12 18:03:44 +0100162 .dev_attrs = ide_dev_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 .suspend = generic_ide_suspend,
164 .resume = generic_ide_resume,
165};
166
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200167EXPORT_SYMBOL_GPL(ide_bus_type);
168
Bartlomiej Zolnierkiewiczebae41a2008-04-27 15:38:29 +0200169int ide_vlb_clk;
170EXPORT_SYMBOL_GPL(ide_vlb_clk);
171
172module_param_named(vlb_clock, ide_vlb_clk, int, 0);
173MODULE_PARM_DESC(vlb_clock, "VLB clock frequency (in MHz)");
174
175int ide_pci_clk;
176EXPORT_SYMBOL_GPL(ide_pci_clk);
177
178module_param_named(pci_clock, ide_pci_clk, int, 0);
179MODULE_PARM_DESC(pci_clock, "PCI bus clock frequency (in MHz)");
180
Bartlomiej Zolnierkiewicz6e875432008-04-27 15:38:30 +0200181static int ide_set_dev_param_mask(const char *s, struct kernel_param *kp)
182{
183 int a, b, i, j = 1;
184 unsigned int *dev_param_mask = (unsigned int *)kp->arg;
185
David Fries0af80c02009-02-25 20:28:21 +0100186 /* controller . device (0 or 1) [ : 1 (set) | 0 (clear) ] */
Bartlomiej Zolnierkiewicz6e875432008-04-27 15:38:30 +0200187 if (sscanf(s, "%d.%d:%d", &a, &b, &j) != 3 &&
188 sscanf(s, "%d.%d", &a, &b) != 2)
189 return -EINVAL;
190
191 i = a * MAX_DRIVES + b;
192
193 if (i >= MAX_HWIFS * MAX_DRIVES || j < 0 || j > 1)
194 return -EINVAL;
195
196 if (j)
197 *dev_param_mask |= (1 << i);
198 else
David Fries0af80c02009-02-25 20:28:21 +0100199 *dev_param_mask &= ~(1 << i);
Bartlomiej Zolnierkiewicz6e875432008-04-27 15:38:30 +0200200
201 return 0;
202}
203
204static unsigned int ide_nodma;
205
206module_param_call(nodma, ide_set_dev_param_mask, NULL, &ide_nodma, 0);
207MODULE_PARM_DESC(nodma, "disallow DMA for a device");
208
209static unsigned int ide_noflush;
210
211module_param_call(noflush, ide_set_dev_param_mask, NULL, &ide_noflush, 0);
212MODULE_PARM_DESC(noflush, "disable flush requests for a device");
213
214static unsigned int ide_noprobe;
215
216module_param_call(noprobe, ide_set_dev_param_mask, NULL, &ide_noprobe, 0);
217MODULE_PARM_DESC(noprobe, "skip probing for a device");
218
219static unsigned int ide_nowerr;
220
221module_param_call(nowerr, ide_set_dev_param_mask, NULL, &ide_nowerr, 0);
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200222MODULE_PARM_DESC(nowerr, "ignore the ATA_DF bit for a device");
Bartlomiej Zolnierkiewicz6e875432008-04-27 15:38:30 +0200223
Bartlomiej Zolnierkiewicz4706a7e2008-04-27 15:38:30 +0200224static unsigned int ide_cdroms;
225
226module_param_call(cdrom, ide_set_dev_param_mask, NULL, &ide_cdroms, 0);
227MODULE_PARM_DESC(cdrom, "force device as a CD-ROM");
228
229struct chs_geom {
230 unsigned int cyl;
231 u8 head;
232 u8 sect;
233};
234
235static unsigned int ide_disks;
236static struct chs_geom ide_disks_chs[MAX_HWIFS * MAX_DRIVES];
237
238static int ide_set_disk_chs(const char *str, struct kernel_param *kp)
239{
240 int a, b, c = 0, h = 0, s = 0, i, j = 1;
241
David Fries0af80c02009-02-25 20:28:21 +0100242 /* controller . device (0 or 1) : Cylinders , Heads , Sectors */
243 /* controller . device (0 or 1) : 1 (use CHS) | 0 (ignore CHS) */
Bartlomiej Zolnierkiewicz4706a7e2008-04-27 15:38:30 +0200244 if (sscanf(str, "%d.%d:%d,%d,%d", &a, &b, &c, &h, &s) != 5 &&
245 sscanf(str, "%d.%d:%d", &a, &b, &j) != 3)
246 return -EINVAL;
247
248 i = a * MAX_DRIVES + b;
249
250 if (i >= MAX_HWIFS * MAX_DRIVES || j < 0 || j > 1)
251 return -EINVAL;
252
253 if (c > INT_MAX || h > 255 || s > 255)
254 return -EINVAL;
255
256 if (j)
257 ide_disks |= (1 << i);
258 else
David Fries0af80c02009-02-25 20:28:21 +0100259 ide_disks &= ~(1 << i);
Bartlomiej Zolnierkiewicz4706a7e2008-04-27 15:38:30 +0200260
261 ide_disks_chs[i].cyl = c;
262 ide_disks_chs[i].head = h;
263 ide_disks_chs[i].sect = s;
264
265 return 0;
266}
267
268module_param_call(chs, ide_set_disk_chs, NULL, NULL, 0);
269MODULE_PARM_DESC(chs, "force device as a disk (using CHS)");
270
Bartlomiej Zolnierkiewicz123995b2008-10-13 21:39:40 +0200271static void ide_dev_apply_params(ide_drive_t *drive, u8 unit)
Bartlomiej Zolnierkiewicz6e875432008-04-27 15:38:30 +0200272{
Bartlomiej Zolnierkiewicz123995b2008-10-13 21:39:40 +0200273 int i = drive->hwif->index * MAX_DRIVES + unit;
Bartlomiej Zolnierkiewicz6e875432008-04-27 15:38:30 +0200274
275 if (ide_nodma & (1 << i)) {
276 printk(KERN_INFO "ide: disallowing DMA for %s\n", drive->name);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200277 drive->dev_flags |= IDE_DFLAG_NODMA;
Bartlomiej Zolnierkiewicz6e875432008-04-27 15:38:30 +0200278 }
279 if (ide_noflush & (1 << i)) {
280 printk(KERN_INFO "ide: disabling flush requests for %s\n",
281 drive->name);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200282 drive->dev_flags |= IDE_DFLAG_NOFLUSH;
Bartlomiej Zolnierkiewicz6e875432008-04-27 15:38:30 +0200283 }
284 if (ide_noprobe & (1 << i)) {
285 printk(KERN_INFO "ide: skipping probe for %s\n", drive->name);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200286 drive->dev_flags |= IDE_DFLAG_NOPROBE;
Bartlomiej Zolnierkiewicz6e875432008-04-27 15:38:30 +0200287 }
288 if (ide_nowerr & (1 << i)) {
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200289 printk(KERN_INFO "ide: ignoring the ATA_DF bit for %s\n",
Bartlomiej Zolnierkiewicz6e875432008-04-27 15:38:30 +0200290 drive->name);
291 drive->bad_wstat = BAD_R_STAT;
292 }
Bartlomiej Zolnierkiewicz4706a7e2008-04-27 15:38:30 +0200293 if (ide_cdroms & (1 << i)) {
294 printk(KERN_INFO "ide: forcing %s as a CD-ROM\n", drive->name);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200295 drive->dev_flags |= IDE_DFLAG_PRESENT;
Bartlomiej Zolnierkiewicz4706a7e2008-04-27 15:38:30 +0200296 drive->media = ide_cdrom;
297 /* an ATAPI device ignores DRDY */
298 drive->ready_stat = 0;
299 }
300 if (ide_disks & (1 << i)) {
301 drive->cyl = drive->bios_cyl = ide_disks_chs[i].cyl;
302 drive->head = drive->bios_head = ide_disks_chs[i].head;
303 drive->sect = drive->bios_sect = ide_disks_chs[i].sect;
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200304
Bartlomiej Zolnierkiewicz4706a7e2008-04-27 15:38:30 +0200305 printk(KERN_INFO "ide: forcing %s as a disk (%d/%d/%d)\n",
306 drive->name,
307 drive->cyl, drive->head, drive->sect);
Bartlomiej Zolnierkiewicz97100fc2008-10-13 21:39:36 +0200308
309 drive->dev_flags |= IDE_DFLAG_FORCED_GEOM | IDE_DFLAG_PRESENT;
Bartlomiej Zolnierkiewicz4706a7e2008-04-27 15:38:30 +0200310 drive->media = ide_disk;
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200311 drive->ready_stat = ATA_DRDY;
Bartlomiej Zolnierkiewicz4706a7e2008-04-27 15:38:30 +0200312 }
Bartlomiej Zolnierkiewicz6e875432008-04-27 15:38:30 +0200313}
314
Bartlomiej Zolnierkiewicz9fd91d92008-04-27 15:38:23 +0200315static unsigned int ide_ignore_cable;
316
317static int ide_set_ignore_cable(const char *s, struct kernel_param *kp)
318{
319 int i, j = 1;
320
David Fries0af80c02009-02-25 20:28:21 +0100321 /* controller (ignore) */
322 /* controller : 1 (ignore) | 0 (use) */
Bartlomiej Zolnierkiewicz9fd91d92008-04-27 15:38:23 +0200323 if (sscanf(s, "%d:%d", &i, &j) != 2 && sscanf(s, "%d", &i) != 1)
324 return -EINVAL;
325
326 if (i >= MAX_HWIFS || j < 0 || j > 1)
327 return -EINVAL;
328
329 if (j)
330 ide_ignore_cable |= (1 << i);
331 else
David Fries0af80c02009-02-25 20:28:21 +0100332 ide_ignore_cable &= ~(1 << i);
Bartlomiej Zolnierkiewicz9fd91d92008-04-27 15:38:23 +0200333
334 return 0;
335}
336
337module_param_call(ignore_cable, ide_set_ignore_cable, NULL, NULL, 0);
338MODULE_PARM_DESC(ignore_cable, "ignore cable detection");
339
340void ide_port_apply_params(ide_hwif_t *hwif)
341{
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100342 ide_drive_t *drive;
Bartlomiej Zolnierkiewicz6e875432008-04-27 15:38:30 +0200343 int i;
344
Bartlomiej Zolnierkiewicz9fd91d92008-04-27 15:38:23 +0200345 if (ide_ignore_cable & (1 << hwif->index)) {
346 printk(KERN_INFO "ide: ignoring cable detection for %s\n",
347 hwif->name);
348 hwif->cbl = ATA_CBL_PATA40_SHORT;
349 }
Bartlomiej Zolnierkiewicz6e875432008-04-27 15:38:30 +0200350
Bartlomiej Zolnierkiewicz2bd24a12009-01-06 17:20:56 +0100351 ide_port_for_each_dev(i, drive, hwif)
352 ide_dev_apply_params(drive, i);
Bartlomiej Zolnierkiewicz9fd91d92008-04-27 15:38:23 +0200353}
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355/*
356 * This is gets invoked once during initialization, to set *everything* up
357 */
358static int __init ide_init(void)
359{
Randy Dunlap349ae232006-10-03 01:14:23 -0700360 int ret;
361
Bartlomiej Zolnierkiewiczeba8ff92008-02-11 00:32:13 +0100362 printk(KERN_INFO "Uniform Multi-Platform E-IDE driver\n");
Bartlomiej Zolnierkiewicz537f06c2008-02-01 23:09:35 +0100363
Randy Dunlap349ae232006-10-03 01:14:23 -0700364 ret = bus_register(&ide_bus_type);
365 if (ret < 0) {
366 printk(KERN_WARNING "IDE: bus_register error: %d\n", ret);
367 return ret;
368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200370 ide_port_class = class_create(THIS_MODULE, "ide_port");
371 if (IS_ERR(ide_port_class)) {
372 ret = PTR_ERR(ide_port_class);
373 goto out_port_class;
374 }
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200375
Bartlomiej Zolnierkiewicz8b803bd2009-03-24 23:22:41 +0100376 ide_acpi_init();
377
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200378 proc_ide_create();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return 0;
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200381
382out_port_class:
383 bus_unregister(&ide_bus_type);
384
385 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Bartlomiej Zolnierkiewicz931ee0d2008-07-15 21:21:47 +0200388static void __exit ide_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 proc_ide_destroy();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200392 class_destroy(ide_port_class);
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 bus_unregister(&ide_bus_type);
395}
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397module_init(ide_init);
Bartlomiej Zolnierkiewicz931ee0d2008-07-15 21:21:47 +0200398module_exit(ide_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Bartlomiej Zolnierkiewicz931ee0d2008-07-15 21:21:47 +0200400MODULE_LICENSE("GPL");