]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/ide/ide.c
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[net-next-2.6.git] / drivers / ide / ide.c
CommitLineData
1da177e4 1/*
59bca8cc 2 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
fc410698 3 * Copyright (C) 2003-2005, 2007 Bartlomiej Zolnierkiewicz
1da177e4
LT
4 */
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 *
1da177e4
LT
18 * ...
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
1da177e4
LT
45 */
46
1da177e4
LT
47#include <linux/module.h>
48#include <linux/types.h>
49#include <linux/string.h>
50#include <linux/kernel.h>
1da177e4
LT
51#include <linux/interrupt.h>
52#include <linux/major.h>
53#include <linux/errno.h>
54#include <linux/genhd.h>
1da177e4
LT
55#include <linux/init.h>
56#include <linux/pci.h>
1da177e4 57#include <linux/ide.h>
3ceca727 58#include <linux/hdreg.h>
1da177e4 59#include <linux/completion.h>
1da177e4 60#include <linux/device.h>
1da177e4 61
f74c9141
BZ
62struct class *ide_port_class;
63
08da591e
BZ
64/**
65 * ide_device_get - get an additional reference to a ide_drive_t
66 * @drive: device to get a reference to
67 *
68 * Gets a reference to the ide_drive_t and increments the use count of the
69 * underlying LLDD module.
70 */
71int ide_device_get(ide_drive_t *drive)
72{
73 struct device *host_dev;
74 struct module *module;
75
76 if (!get_device(&drive->gendev))
77 return -ENXIO;
78
79 host_dev = drive->hwif->host->dev[0];
80 module = host_dev ? host_dev->driver->owner : NULL;
81
82 if (module && !try_module_get(module)) {
83 put_device(&drive->gendev);
84 return -ENXIO;
85 }
86
87 return 0;
88}
89EXPORT_SYMBOL_GPL(ide_device_get);
90
91/**
92 * ide_device_put - release a reference to a ide_drive_t
93 * @drive: device to release a reference on
94 *
95 * Release a reference to the ide_drive_t and decrements the use count of
96 * the underlying LLDD module.
97 */
98void ide_device_put(ide_drive_t *drive)
99{
100#ifdef CONFIG_MODULE_UNLOAD
101 struct device *host_dev = drive->hwif->host->dev[0];
102 struct module *module = host_dev ? host_dev->driver->owner : NULL;
103
104 if (module)
105 module_put(module);
106#endif
107 put_device(&drive->gendev);
108}
109EXPORT_SYMBOL_GPL(ide_device_put);
110
8604affd
BZ
111static int ide_bus_match(struct device *dev, struct device_driver *drv)
112{
113 return 1;
114}
115
7eff2e7a 116static int ide_uevent(struct device *dev, struct kobj_uevent_env *env)
263756ec
KS
117{
118 ide_drive_t *drive = to_ide_device(dev);
7eff2e7a 119
ebdab07d 120 add_uevent_var(env, "MEDIA=%s", ide_media_string(drive));
7eff2e7a 121 add_uevent_var(env, "DRIVENAME=%s", drive->name);
ebdab07d 122 add_uevent_var(env, "MODALIAS=ide:m-%s", ide_media_string(drive));
263756ec
KS
123 return 0;
124}
125
4031bbe4
RK
126static int generic_ide_probe(struct device *dev)
127{
128 ide_drive_t *drive = to_ide_device(dev);
7f3c868b 129 struct ide_driver *drv = to_ide_driver(dev->driver);
4031bbe4
RK
130
131 return drv->probe ? drv->probe(drive) : -ENODEV;
132}
133
134static int generic_ide_remove(struct device *dev)
135{
136 ide_drive_t *drive = to_ide_device(dev);
7f3c868b 137 struct ide_driver *drv = to_ide_driver(dev->driver);
4031bbe4
RK
138
139 if (drv->remove)
140 drv->remove(drive);
141
142 return 0;
143}
144
145static void generic_ide_shutdown(struct device *dev)
146{
147 ide_drive_t *drive = to_ide_device(dev);
7f3c868b 148 struct ide_driver *drv = to_ide_driver(dev->driver);
4031bbe4
RK
149
150 if (dev->driver && drv->shutdown)
151 drv->shutdown(drive);
152}
153
1da177e4
LT
154struct bus_type ide_bus_type = {
155 .name = "ide",
8604affd 156 .match = ide_bus_match,
263756ec 157 .uevent = ide_uevent,
4031bbe4
RK
158 .probe = generic_ide_probe,
159 .remove = generic_ide_remove,
160 .shutdown = generic_ide_shutdown,
263756ec 161 .dev_attrs = ide_dev_attrs,
1da177e4
LT
162 .suspend = generic_ide_suspend,
163 .resume = generic_ide_resume,
164};
165
8604affd
BZ
166EXPORT_SYMBOL_GPL(ide_bus_type);
167
ebae41a5
BZ
168int ide_vlb_clk;
169EXPORT_SYMBOL_GPL(ide_vlb_clk);
170
171module_param_named(vlb_clock, ide_vlb_clk, int, 0);
172MODULE_PARM_DESC(vlb_clock, "VLB clock frequency (in MHz)");
173
174int ide_pci_clk;
175EXPORT_SYMBOL_GPL(ide_pci_clk);
176
177module_param_named(pci_clock, ide_pci_clk, int, 0);
178MODULE_PARM_DESC(pci_clock, "PCI bus clock frequency (in MHz)");
179
6e87543a
BZ
180static int ide_set_dev_param_mask(const char *s, struct kernel_param *kp)
181{
182 int a, b, i, j = 1;
183 unsigned int *dev_param_mask = (unsigned int *)kp->arg;
184
0af80c04 185 /* controller . device (0 or 1) [ : 1 (set) | 0 (clear) ] */
6e87543a
BZ
186 if (sscanf(s, "%d.%d:%d", &a, &b, &j) != 3 &&
187 sscanf(s, "%d.%d", &a, &b) != 2)
188 return -EINVAL;
189
190 i = a * MAX_DRIVES + b;
191
192 if (i >= MAX_HWIFS * MAX_DRIVES || j < 0 || j > 1)
193 return -EINVAL;
194
195 if (j)
196 *dev_param_mask |= (1 << i);
197 else
0af80c04 198 *dev_param_mask &= ~(1 << i);
6e87543a
BZ
199
200 return 0;
201}
202
203static unsigned int ide_nodma;
204
205module_param_call(nodma, ide_set_dev_param_mask, NULL, &ide_nodma, 0);
206MODULE_PARM_DESC(nodma, "disallow DMA for a device");
207
208static unsigned int ide_noflush;
209
210module_param_call(noflush, ide_set_dev_param_mask, NULL, &ide_noflush, 0);
211MODULE_PARM_DESC(noflush, "disable flush requests for a device");
212
075affcb
BZ
213static unsigned int ide_nohpa;
214
215module_param_call(nohpa, ide_set_dev_param_mask, NULL, &ide_nohpa, 0);
216MODULE_PARM_DESC(nohpa, "disable Host Protected Area for a device");
217
6e87543a
BZ
218static unsigned int ide_noprobe;
219
220module_param_call(noprobe, ide_set_dev_param_mask, NULL, &ide_noprobe, 0);
221MODULE_PARM_DESC(noprobe, "skip probing for a device");
222
223static unsigned int ide_nowerr;
224
225module_param_call(nowerr, ide_set_dev_param_mask, NULL, &ide_nowerr, 0);
3a7d2484 226MODULE_PARM_DESC(nowerr, "ignore the ATA_DF bit for a device");
6e87543a 227
4706a7e0
BZ
228static unsigned int ide_cdroms;
229
230module_param_call(cdrom, ide_set_dev_param_mask, NULL, &ide_cdroms, 0);
231MODULE_PARM_DESC(cdrom, "force device as a CD-ROM");
232
233struct chs_geom {
234 unsigned int cyl;
235 u8 head;
236 u8 sect;
237};
238
239static unsigned int ide_disks;
240static struct chs_geom ide_disks_chs[MAX_HWIFS * MAX_DRIVES];
241
242static int ide_set_disk_chs(const char *str, struct kernel_param *kp)
243{
244 int a, b, c = 0, h = 0, s = 0, i, j = 1;
245
0af80c04
DF
246 /* controller . device (0 or 1) : Cylinders , Heads , Sectors */
247 /* controller . device (0 or 1) : 1 (use CHS) | 0 (ignore CHS) */
4706a7e0
BZ
248 if (sscanf(str, "%d.%d:%d,%d,%d", &a, &b, &c, &h, &s) != 5 &&
249 sscanf(str, "%d.%d:%d", &a, &b, &j) != 3)
250 return -EINVAL;
251
252 i = a * MAX_DRIVES + b;
253
254 if (i >= MAX_HWIFS * MAX_DRIVES || j < 0 || j > 1)
255 return -EINVAL;
256
257 if (c > INT_MAX || h > 255 || s > 255)
258 return -EINVAL;
259
260 if (j)
261 ide_disks |= (1 << i);
262 else
0af80c04 263 ide_disks &= ~(1 << i);
4706a7e0
BZ
264
265 ide_disks_chs[i].cyl = c;
266 ide_disks_chs[i].head = h;
267 ide_disks_chs[i].sect = s;
268
269 return 0;
270}
271
272module_param_call(chs, ide_set_disk_chs, NULL, NULL, 0);
273MODULE_PARM_DESC(chs, "force device as a disk (using CHS)");
274
123995b9 275static void ide_dev_apply_params(ide_drive_t *drive, u8 unit)
6e87543a 276{
123995b9 277 int i = drive->hwif->index * MAX_DRIVES + unit;
6e87543a
BZ
278
279 if (ide_nodma & (1 << i)) {
280 printk(KERN_INFO "ide: disallowing DMA for %s\n", drive->name);
97100fc8 281 drive->dev_flags |= IDE_DFLAG_NODMA;
6e87543a
BZ
282 }
283 if (ide_noflush & (1 << i)) {
284 printk(KERN_INFO "ide: disabling flush requests for %s\n",
285 drive->name);
97100fc8 286 drive->dev_flags |= IDE_DFLAG_NOFLUSH;
6e87543a 287 }
075affcb
BZ
288 if (ide_nohpa & (1 << i)) {
289 printk(KERN_INFO "ide: disabling Host Protected Area for %s\n",
290 drive->name);
291 drive->dev_flags |= IDE_DFLAG_NOHPA;
292 }
6e87543a
BZ
293 if (ide_noprobe & (1 << i)) {
294 printk(KERN_INFO "ide: skipping probe for %s\n", drive->name);
97100fc8 295 drive->dev_flags |= IDE_DFLAG_NOPROBE;
6e87543a
BZ
296 }
297 if (ide_nowerr & (1 << i)) {
3a7d2484 298 printk(KERN_INFO "ide: ignoring the ATA_DF bit for %s\n",
6e87543a
BZ
299 drive->name);
300 drive->bad_wstat = BAD_R_STAT;
301 }
4706a7e0
BZ
302 if (ide_cdroms & (1 << i)) {
303 printk(KERN_INFO "ide: forcing %s as a CD-ROM\n", drive->name);
97100fc8 304 drive->dev_flags |= IDE_DFLAG_PRESENT;
4706a7e0
BZ
305 drive->media = ide_cdrom;
306 /* an ATAPI device ignores DRDY */
307 drive->ready_stat = 0;
308 }
309 if (ide_disks & (1 << i)) {
310 drive->cyl = drive->bios_cyl = ide_disks_chs[i].cyl;
311 drive->head = drive->bios_head = ide_disks_chs[i].head;
312 drive->sect = drive->bios_sect = ide_disks_chs[i].sect;
97100fc8 313
4706a7e0
BZ
314 printk(KERN_INFO "ide: forcing %s as a disk (%d/%d/%d)\n",
315 drive->name,
316 drive->cyl, drive->head, drive->sect);
97100fc8
BZ
317
318 drive->dev_flags |= IDE_DFLAG_FORCED_GEOM | IDE_DFLAG_PRESENT;
4706a7e0 319 drive->media = ide_disk;
3a7d2484 320 drive->ready_stat = ATA_DRDY;
4706a7e0 321 }
6e87543a
BZ
322}
323
9fd91d95
BZ
324static unsigned int ide_ignore_cable;
325
326static int ide_set_ignore_cable(const char *s, struct kernel_param *kp)
327{
328 int i, j = 1;
329
0af80c04
DF
330 /* controller (ignore) */
331 /* controller : 1 (ignore) | 0 (use) */
9fd91d95
BZ
332 if (sscanf(s, "%d:%d", &i, &j) != 2 && sscanf(s, "%d", &i) != 1)
333 return -EINVAL;
334
335 if (i >= MAX_HWIFS || j < 0 || j > 1)
336 return -EINVAL;
337
338 if (j)
339 ide_ignore_cable |= (1 << i);
340 else
0af80c04 341 ide_ignore_cable &= ~(1 << i);
9fd91d95
BZ
342
343 return 0;
344}
345
346module_param_call(ignore_cable, ide_set_ignore_cable, NULL, NULL, 0);
347MODULE_PARM_DESC(ignore_cable, "ignore cable detection");
348
349void ide_port_apply_params(ide_hwif_t *hwif)
350{
2bd24a1c 351 ide_drive_t *drive;
6e87543a
BZ
352 int i;
353
9fd91d95
BZ
354 if (ide_ignore_cable & (1 << hwif->index)) {
355 printk(KERN_INFO "ide: ignoring cable detection for %s\n",
356 hwif->name);
357 hwif->cbl = ATA_CBL_PATA40_SHORT;
358 }
6e87543a 359
2bd24a1c
BZ
360 ide_port_for_each_dev(i, drive, hwif)
361 ide_dev_apply_params(drive, i);
9fd91d95
BZ
362}
363
1da177e4
LT
364/*
365 * This is gets invoked once during initialization, to set *everything* up
366 */
367static int __init ide_init(void)
368{
349ae23f
RD
369 int ret;
370
eba8ff94 371 printk(KERN_INFO "Uniform Multi-Platform E-IDE driver\n");
537f06c5 372
349ae23f
RD
373 ret = bus_register(&ide_bus_type);
374 if (ret < 0) {
375 printk(KERN_WARNING "IDE: bus_register error: %d\n", ret);
376 return ret;
377 }
1da177e4 378
f74c9141
BZ
379 ide_port_class = class_create(THIS_MODULE, "ide_port");
380 if (IS_ERR(ide_port_class)) {
381 ret = PTR_ERR(ide_port_class);
382 goto out_port_class;
383 }
f74c9141 384
8b803bd1
BZ
385 ide_acpi_init();
386
5cbf79cd 387 proc_ide_create();
1da177e4 388
1da177e4 389 return 0;
f74c9141
BZ
390
391out_port_class:
392 bus_unregister(&ide_bus_type);
393
394 return ret;
1da177e4
LT
395}
396
931ee0dc 397static void __exit ide_exit(void)
1da177e4 398{
1da177e4 399 proc_ide_destroy();
1da177e4 400
f74c9141
BZ
401 class_destroy(ide_port_class);
402
1da177e4
LT
403 bus_unregister(&ide_bus_type);
404}
405
1da177e4 406module_init(ide_init);
931ee0dc 407module_exit(ide_exit);
1da177e4 408
931ee0dc 409MODULE_LICENSE("GPL");