blob: f78e789ea5f78eb6f9f9e1daff2715a493e18964 [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
63
64/* default maximum number of failures */
65#define IDE_DEFAULT_MAX_FAILURES 1
66
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +020067struct class *ide_port_class;
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static const u8 ide_hwif_to_major[] = { IDE0_MAJOR, IDE1_MAJOR,
70 IDE2_MAJOR, IDE3_MAJOR,
71 IDE4_MAJOR, IDE5_MAJOR,
72 IDE6_MAJOR, IDE7_MAJOR,
73 IDE8_MAJOR, IDE9_MAJOR };
74
Matthias Kaehlckeef298882007-07-09 23:17:55 +020075DEFINE_MUTEX(ide_cfg_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Bartlomiej Zolnierkiewicz931ee0d2008-07-15 21:21:47 +020077__cacheline_aligned_in_smp DEFINE_SPINLOCK(ide_lock);
78EXPORT_SYMBOL(ide_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Bartlomiej Zolnierkiewicz43514ed2008-04-18 00:46:22 +020080static void ide_port_init_devices_data(ide_hwif_t *);
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/*
83 * Do not even *think* about calling this!
84 */
Bartlomiej Zolnierkiewiczcbb010c2008-01-26 20:13:06 +010085void ide_init_port_data(ide_hwif_t *hwif, unsigned int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 /* bulk initialize hwif & drive info with zeros */
88 memset(hwif, 0, sizeof(ide_hwif_t));
89
90 /* fill in any non-zero initial values */
91 hwif->index = index;
92 hwif->major = ide_hwif_to_major[index];
93
94 hwif->name[0] = 'i';
95 hwif->name[1] = 'd';
96 hwif->name[2] = 'e';
97 hwif->name[3] = '0' + index;
98
Aleksey Makarovf36d4022006-01-09 15:59:27 -080099 init_completion(&hwif->gendev_rel_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Bartlomiej Zolnierkiewicz374e0422008-07-23 19:55:56 +0200101 hwif->tp_ops = &default_tp_ops;
Bartlomiej Zolnierkiewicz43514ed2008-04-18 00:46:22 +0200102
103 ide_port_init_devices_data(hwif);
104}
Bartlomiej Zolnierkiewicz43514ed2008-04-18 00:46:22 +0200105
106static void ide_port_init_devices_data(ide_hwif_t *hwif)
107{
108 int unit;
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 for (unit = 0; unit < MAX_DRIVES; ++unit) {
111 ide_drive_t *drive = &hwif->drives[unit];
Bartlomiej Zolnierkiewicz43514ed2008-04-18 00:46:22 +0200112 u8 j = (hwif->index * MAX_DRIVES) + unit;
113
114 memset(drive, 0, sizeof(*drive));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 drive->media = ide_disk;
117 drive->select.all = (unit<<4)|0xa0;
118 drive->hwif = hwif;
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200119 drive->ready_stat = ATA_DRDY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 drive->bad_wstat = BAD_W_STAT;
121 drive->special.b.recalibrate = 1;
122 drive->special.b.set_geometry = 1;
123 drive->name[0] = 'h';
124 drive->name[1] = 'd';
Bartlomiej Zolnierkiewicz43514ed2008-04-18 00:46:22 +0200125 drive->name[2] = 'a' + j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 drive->max_failures = IDE_DEFAULT_MAX_FAILURES;
Bartlomiej Zolnierkiewicz43514ed2008-04-18 00:46:22 +0200127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 INIT_LIST_HEAD(&drive->list);
Aleksey Makarovf36d4022006-01-09 15:59:27 -0800129 init_completion(&drive->gendev_rel_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131}
Bartlomiej Zolnierkiewicz43514ed2008-04-18 00:46:22 +0200132
Bartlomiej Zolnierkiewicz71bf9f62008-04-18 00:46:22 +0200133/* Called with ide_lock held. */
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +0200134static void __ide_port_unregister_devices(ide_hwif_t *hwif)
Bartlomiej Zolnierkiewicz71bf9f62008-04-18 00:46:22 +0200135{
136 int i;
137
138 for (i = 0; i < MAX_DRIVES; i++) {
139 ide_drive_t *drive = &hwif->drives[i];
140
141 if (drive->present) {
142 spin_unlock_irq(&ide_lock);
143 device_unregister(&drive->gendev);
144 wait_for_completion(&drive->gendev_rel_comp);
145 spin_lock_irq(&ide_lock);
146 }
147 }
148}
149
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +0200150void ide_port_unregister_devices(ide_hwif_t *hwif)
151{
152 mutex_lock(&ide_cfg_mtx);
153 spin_lock_irq(&ide_lock);
154 __ide_port_unregister_devices(hwif);
155 hwif->present = 0;
156 ide_port_init_devices_data(hwif);
157 spin_unlock_irq(&ide_lock);
158 mutex_unlock(&ide_cfg_mtx);
159}
160EXPORT_SYMBOL_GPL(ide_port_unregister_devices);
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/**
Sergei Shtylylov020e3222006-10-03 01:14:13 -0700163 * ide_unregister - free an IDE interface
Bartlomiej Zolnierkiewicz387750c2008-04-27 15:38:31 +0200164 * @hwif: IDE interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 *
166 * Perform the final unregister of an IDE interface. At the moment
167 * we don't refcount interfaces so this will also get split up.
168 *
169 * Locking:
170 * The caller must not hold the IDE locks
171 * The drive present/vanishing is not yet properly locked
172 * Take care with the callbacks. These have been split to avoid
173 * deadlocking the IDE layer. The shutdown callback is called
174 * before we take the lock and free resources. It is up to the
175 * caller to be sure there is no pending I/O here, and that
Sergei Shtylylov020e3222006-10-03 01:14:13 -0700176 * the interface will not be reopened (present/vanishing locking
177 * isn't yet done BTW). After we commit to the final kill we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 * call the cleanup callback with the ide locks held.
179 *
180 * Unregister restores the hwif structures to the default state.
181 * This is raving bonkers.
182 */
183
Bartlomiej Zolnierkiewicz387750c2008-04-27 15:38:31 +0200184void ide_unregister(ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Bartlomiej Zolnierkiewicz387750c2008-04-27 15:38:31 +0200186 ide_hwif_t *g;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 ide_hwgroup_t *hwgroup;
Bartlomiej Zolnierkiewicz71bf9f62008-04-18 00:46:22 +0200188 int irq_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 BUG_ON(in_interrupt());
191 BUG_ON(irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Bartlomiej Zolnierkiewiczbd8a59e2008-07-05 20:30:51 +0200193 mutex_lock(&ide_cfg_mtx);
194
195 spin_lock_irq(&ide_lock);
196 if (hwif->present) {
197 __ide_port_unregister_devices(hwif);
198 hwif->present = 0;
199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 spin_unlock_irq(&ide_lock);
201
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200202 ide_proc_unregister_port(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 hwgroup = hwif->hwgroup;
205 /*
206 * free the irq if we were the only hwif using it
207 */
208 g = hwgroup->hwif;
209 do {
210 if (g->irq == hwif->irq)
211 ++irq_count;
212 g = g->next;
213 } while (g != hwgroup->hwif);
214 if (irq_count == 1)
215 free_irq(hwif->irq, hwgroup);
216
Bartlomiej Zolnierkiewicz96e5ad32008-02-01 23:09:35 +0100217 ide_remove_port_from_hwgroup(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200219 device_unregister(hwif->portdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 device_unregister(&hwif->gendev);
Aleksey Makarovf36d4022006-01-09 15:59:27 -0800221 wait_for_completion(&hwif->gendev_rel_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 /*
224 * Remove us from the kernel's knowledge
225 */
226 blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
227 kfree(hwif->sg_table);
228 unregister_blkdev(hwif->major, hwif->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Bartlomiej Zolnierkiewicz93de00f2008-04-18 00:46:24 +0200230 if (hwif->dma_base)
Bartlomiej Zolnierkiewicz0d1bad22008-04-26 22:25:19 +0200231 ide_release_dma_engine(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Matthias Kaehlckeef298882007-07-09 23:17:55 +0200233 mutex_unlock(&ide_cfg_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
Bartlomiej Zolnierkiewicz57c802e2008-01-26 20:13:05 +0100236void ide_init_port_hw(ide_hwif_t *hwif, hw_regs_t *hw)
237{
Bartlomiej Zolnierkiewicz4c3032d2008-04-27 15:38:32 +0200238 memcpy(&hwif->io_ports, &hw->io_ports, sizeof(hwif->io_ports));
Bartlomiej Zolnierkiewicz57c802e2008-01-26 20:13:05 +0100239 hwif->irq = hw->irq;
Bartlomiej Zolnierkiewicz57c802e2008-01-26 20:13:05 +0100240 hwif->chipset = hw->chipset;
Bartlomiej Zolnierkiewiczc56c5642008-07-16 20:33:40 +0200241 hwif->dev = hw->dev;
242 hwif->gendev.parent = hw->parent ? hw->parent : hw->dev;
Bartlomiej Zolnierkiewicz57c802e2008-01-26 20:13:05 +0100243 hwif->ack_intr = hw->ack_intr;
Bartlomiej Zolnierkiewiczd6276b52008-07-23 19:55:56 +0200244 hwif->config_data = hw->config;
Bartlomiej Zolnierkiewicz57c802e2008-01-26 20:13:05 +0100245}
Bartlomiej Zolnierkiewicz57c802e2008-01-26 20:13:05 +0100246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247/*
248 * Locks for IDE setting functionality
249 */
250
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200251DEFINE_MUTEX(ide_setting_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200253EXPORT_SYMBOL_GPL(ide_setting_mtx);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 * ide_spin_wait_hwgroup - wait for group
257 * @drive: drive in the group
258 *
259 * Wait for an IDE device group to go non busy and then return
260 * holding the ide_lock which guards the hwgroup->busy status
261 * and right to use it.
262 */
263
264int ide_spin_wait_hwgroup (ide_drive_t *drive)
265{
266 ide_hwgroup_t *hwgroup = HWGROUP(drive);
267 unsigned long timeout = jiffies + (3 * HZ);
268
269 spin_lock_irq(&ide_lock);
270
271 while (hwgroup->busy) {
272 unsigned long lflags;
273 spin_unlock_irq(&ide_lock);
274 local_irq_set(lflags);
275 if (time_after(jiffies, timeout)) {
276 local_irq_restore(lflags);
277 printk(KERN_ERR "%s: channel busy\n", drive->name);
278 return -EBUSY;
279 }
280 local_irq_restore(lflags);
281 spin_lock_irq(&ide_lock);
282 }
283 return 0;
284}
285
286EXPORT_SYMBOL(ide_spin_wait_hwgroup);
287
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200288ide_devset_get(io_32bit, io_32bit);
289
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200290int set_io_32bit(ide_drive_t *drive, int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200292 if (drive->no_io_32bit)
293 return -EPERM;
294
295 if (arg < 0 || arg > 1 + (SUPPORT_VLB_SYNC << 1))
296 return -EINVAL;
297
Bartlomiej Zolnierkiewicz644a9d72007-12-12 23:32:00 +0100298 if (ide_spin_wait_hwgroup(drive))
299 return -EBUSY;
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 drive->io_32bit = arg;
Bartlomiej Zolnierkiewicz644a9d72007-12-12 23:32:00 +0100302
303 spin_unlock_irq(&ide_lock);
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return 0;
306}
307
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200308ide_devset_get(ksettings, keep_settings);
309
Bartlomiej Zolnierkiewicz263138a2008-10-10 22:39:27 +0200310int set_ksettings(ide_drive_t *drive, int arg)
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200311{
312 if (arg < 0 || arg > 1)
313 return -EINVAL;
314
315 if (ide_spin_wait_hwgroup(drive))
316 return -EBUSY;
317 drive->keep_settings = arg;
318 spin_unlock_irq(&ide_lock);
319
320 return 0;
321}
322
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200323ide_devset_get(using_dma, using_dma);
324
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200325int set_using_dma(ide_drive_t *drive, int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327#ifdef CONFIG_BLK_DEV_IDEDMA
Bartlomiej Zolnierkiewicz87996202007-03-26 23:03:19 +0200328 ide_hwif_t *hwif = drive->hwif;
329 int err = -EPERM;
330
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200331 if (arg < 0 || arg > 1)
332 return -EINVAL;
333
Bartlomiej Zolnierkiewicz48fb2682008-10-10 22:39:19 +0200334 if (ata_id_has_dma(drive->id) == 0)
Bartlomiej Zolnierkiewicz87996202007-03-26 23:03:19 +0200335 goto out;
336
Bartlomiej Zolnierkiewicz5e37bdc2008-04-26 22:25:24 +0200337 if (hwif->dma_ops == NULL)
Bartlomiej Zolnierkiewicz87996202007-03-26 23:03:19 +0200338 goto out;
339
340 err = -EBUSY;
341 if (ide_spin_wait_hwgroup(drive))
342 goto out;
343 /*
344 * set ->busy flag, unlock and let it ride
345 */
346 hwif->hwgroup->busy = 1;
347 spin_unlock_irq(&ide_lock);
348
349 err = 0;
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (arg) {
Bartlomiej Zolnierkiewicz23b1bd42008-01-25 22:17:19 +0100352 if (ide_set_dma(drive))
Bartlomiej Zolnierkiewicz87996202007-03-26 23:03:19 +0200353 err = -EIO;
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100354 } else
355 ide_dma_off(drive);
Bartlomiej Zolnierkiewicz87996202007-03-26 23:03:19 +0200356
357 /*
358 * lock, clear ->busy flag and unlock before leaving
359 */
360 spin_lock_irq(&ide_lock);
361 hwif->hwgroup->busy = 0;
362 spin_unlock_irq(&ide_lock);
363out:
364 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365#else
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200366 if (arg < 0 || arg > 1)
367 return -EINVAL;
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return -EPERM;
370#endif
371}
372
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200373int set_pio_mode(ide_drive_t *drive, int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
FUJITA Tomonori5b114712008-07-15 21:21:44 +0200375 struct request *rq;
Bartlomiej Zolnierkiewicz784506c2008-04-26 17:36:43 +0200376 ide_hwif_t *hwif = drive->hwif;
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200377 const struct ide_port_ops *port_ops = hwif->port_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200379 if (arg < 0 || arg > 255)
380 return -EINVAL;
381
Bartlomiej Zolnierkiewiczac95bee2008-04-26 22:25:14 +0200382 if (port_ops == NULL || port_ops->set_pio_mode == NULL ||
Bartlomiej Zolnierkiewicz784506c2008-04-26 17:36:43 +0200383 (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return -ENOSYS;
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (drive->special.b.set_tune)
387 return -EBUSY;
Bartlomiej Zolnierkiewicz145b75e2008-01-26 20:13:11 +0100388
FUJITA Tomonori5b114712008-07-15 21:21:44 +0200389 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
390 rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
Bartlomiej Zolnierkiewicz145b75e2008-01-26 20:13:11 +0100391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 drive->tune_req = (u8) arg;
393 drive->special.b.set_tune = 1;
FUJITA Tomonori5b114712008-07-15 21:21:44 +0200394
395 blk_execute_rq(drive->queue, NULL, rq, 0);
396 blk_put_request(rq);
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return 0;
399}
400
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200401ide_devset_get(unmaskirq, unmask);
402
Bartlomiej Zolnierkiewicz263138a2008-10-10 22:39:27 +0200403int set_unmaskirq(ide_drive_t *drive, int arg)
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200404{
405 if (drive->no_unmask)
406 return -EPERM;
407
408 if (arg < 0 || arg > 1)
409 return -EINVAL;
410
411 if (ide_spin_wait_hwgroup(drive))
412 return -EBUSY;
413 drive->unmask = arg;
414 spin_unlock_irq(&ide_lock);
415
416 return 0;
417}
418
David Brownellb887d2e2006-08-14 23:11:05 -0700419static int generic_ide_suspend(struct device *dev, pm_message_t mesg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 ide_drive_t *drive = dev->driver_data;
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100422 ide_hwif_t *hwif = HWIF(drive);
FUJITA Tomonori5b114712008-07-15 21:21:44 +0200423 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 struct request_pm_state rqpm;
425 ide_task_t args;
Shaohua Li5e321322007-10-11 23:53:58 +0200426 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100428 /* Call ACPI _GTM only once */
429 if (!(drive->dn % 2))
430 ide_acpi_get_timing(hwif);
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 memset(&rqpm, 0, sizeof(rqpm));
433 memset(&args, 0, sizeof(args));
FUJITA Tomonori5b114712008-07-15 21:21:44 +0200434 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
435 rq->cmd_type = REQ_TYPE_PM_SUSPEND;
436 rq->special = &args;
437 rq->data = &rqpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 rqpm.pm_step = ide_pm_state_start_suspend;
David Brownellb887d2e2006-08-14 23:11:05 -0700439 if (mesg.event == PM_EVENT_PRETHAW)
440 mesg.event = PM_EVENT_FREEZE;
441 rqpm.pm_state = mesg.event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
FUJITA Tomonori5b114712008-07-15 21:21:44 +0200443 ret = blk_execute_rq(drive->queue, NULL, rq, 0);
444 blk_put_request(rq);
Shaohua Li5e321322007-10-11 23:53:58 +0200445 /* only call ACPI _PS3 after both drivers are suspended */
446 if (!ret && (((drive->dn % 2) && hwif->drives[0].present
447 && hwif->drives[1].present)
448 || !hwif->drives[0].present
449 || !hwif->drives[1].present))
450 ide_acpi_set_state(hwif, 0);
451 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
454static int generic_ide_resume(struct device *dev)
455{
456 ide_drive_t *drive = dev->driver_data;
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100457 ide_hwif_t *hwif = HWIF(drive);
FUJITA Tomonori5b114712008-07-15 21:21:44 +0200458 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 struct request_pm_state rqpm;
460 ide_task_t args;
Lee Trager0d2157f2007-06-08 15:14:30 +0200461 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100463 /* Call ACPI _STM only once */
Shaohua Li5e321322007-10-11 23:53:58 +0200464 if (!(drive->dn % 2)) {
465 ide_acpi_set_state(hwif, 1);
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100466 ide_acpi_push_timing(hwif);
Shaohua Li5e321322007-10-11 23:53:58 +0200467 }
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100468
469 ide_acpi_exec_tfs(drive);
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 memset(&rqpm, 0, sizeof(rqpm));
472 memset(&args, 0, sizeof(args));
FUJITA Tomonori5b114712008-07-15 21:21:44 +0200473 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
474 rq->cmd_type = REQ_TYPE_PM_RESUME;
475 rq->cmd_flags |= REQ_PREEMPT;
476 rq->special = &args;
477 rq->data = &rqpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 rqpm.pm_step = ide_pm_state_start_resume;
Pavel Machekca078ba2005-09-03 15:56:57 -0700479 rqpm.pm_state = PM_EVENT_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
FUJITA Tomonori5b114712008-07-15 21:21:44 +0200481 err = blk_execute_rq(drive->queue, NULL, rq, 1);
482 blk_put_request(rq);
Lee Trager0d2157f2007-06-08 15:14:30 +0200483
Rafael J. Wysockice9b2b02007-06-16 02:24:43 +0200484 if (err == 0 && dev->driver) {
485 ide_driver_t *drv = to_ide_driver(dev->driver);
486
487 if (drv->resume)
488 drv->resume(drive);
489 }
Lee Trager0d2157f2007-06-08 15:14:30 +0200490
491 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
Elias Oltmanns64a8f002008-07-16 20:33:48 +0200494static int generic_drive_reset(ide_drive_t *drive)
Elias Oltmanns79e36a92008-07-16 20:33:48 +0200495{
496 struct request *rq;
Elias Oltmanns64a8f002008-07-16 20:33:48 +0200497 int ret = 0;
Elias Oltmanns79e36a92008-07-16 20:33:48 +0200498
499 rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
500 rq->cmd_type = REQ_TYPE_SPECIAL;
501 rq->cmd_len = 1;
502 rq->cmd[0] = REQ_DRIVE_RESET;
503 rq->cmd_flags |= REQ_SOFTBARRIER;
Elias Oltmanns64a8f002008-07-16 20:33:48 +0200504 if (blk_execute_rq(drive->queue, NULL, rq, 1))
505 ret = rq->errors;
Elias Oltmanns79e36a92008-07-16 20:33:48 +0200506 blk_put_request(rq);
Elias Oltmanns64a8f002008-07-16 20:33:48 +0200507 return ret;
Elias Oltmanns79e36a92008-07-16 20:33:48 +0200508}
509
Bartlomiej Zolnierkiewicz48fb2682008-10-10 22:39:19 +0200510static int ide_get_identity_ioctl(ide_drive_t *drive, unsigned int cmd,
511 unsigned long arg)
512{
513 u16 *id = NULL;
514 int size = (cmd == HDIO_GET_IDENTITY) ? (ATA_ID_WORDS * 2) : 142;
515 int rc = 0;
516
517 if (drive->id_read == 0) {
518 rc = -ENOMSG;
519 goto out;
520 }
521
522 id = kmalloc(size, GFP_KERNEL);
523 if (id == NULL) {
524 rc = -ENOMEM;
525 goto out;
526 }
527
528 memcpy(id, drive->id, size);
Bartlomiej Zolnierkiewicz93734a22008-10-10 22:39:31 +0200529 ata_id_to_hd_driveid(id);
Bartlomiej Zolnierkiewicz48fb2682008-10-10 22:39:19 +0200530
531 if (copy_to_user((void __user *)arg, id, size))
532 rc = -EFAULT;
533
534 kfree(id);
535out:
536 return rc;
537}
538
Bartlomiej Zolnierkiewicz9fa295e2008-10-10 22:39:29 +0200539static int ide_get_nice_ioctl(ide_drive_t *drive, unsigned long arg)
540{
541 return put_user((drive->dsc_overlap << IDE_NICE_DSC_OVERLAP) |
542 (drive->nice1 << IDE_NICE_1), (long __user *)arg);
543}
544
545static int ide_set_nice_ioctl(ide_drive_t *drive, unsigned long arg)
546{
547 if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
548 return -EPERM;
549
550 if (((arg >> IDE_NICE_DSC_OVERLAP) & 1) &&
551 (drive->media == ide_disk || drive->media == ide_floppy ||
552 drive->scsi))
553 return -EPERM;
554
555 drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
556 drive->nice1 = (arg >> IDE_NICE_1) & 1;
557
558 return 0;
559}
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561int generic_ide_ioctl(ide_drive_t *drive, struct file *file, struct block_device *bdev,
562 unsigned int cmd, unsigned long arg)
563{
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200564 unsigned long flags;
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200565 int err = 0, (*getfunc)(ide_drive_t *), (*setfunc)(ide_drive_t *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200567 switch (cmd) {
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200568 case HDIO_GET_32BIT: getfunc = get_io_32bit; goto read_val;
569 case HDIO_GET_KEEPSETTINGS: getfunc = get_ksettings; goto read_val;
570 case HDIO_GET_UNMASKINTR: getfunc = get_unmaskirq; goto read_val;
571 case HDIO_GET_DMA: getfunc = get_using_dma; goto read_val;
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200572 case HDIO_SET_32BIT: setfunc = set_io_32bit; goto set_val;
573 case HDIO_SET_KEEPSETTINGS: setfunc = set_ksettings; goto set_val;
574 case HDIO_SET_PIO_MODE: setfunc = set_pio_mode; goto set_val;
575 case HDIO_SET_UNMASKINTR: setfunc = set_unmaskirq; goto set_val;
576 case HDIO_SET_DMA: setfunc = set_using_dma; goto set_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 case HDIO_OBSOLETE_IDENTITY:
581 case HDIO_GET_IDENTITY:
582 if (bdev != bdev->bd_contains)
583 return -EINVAL;
Bartlomiej Zolnierkiewicz48fb2682008-10-10 22:39:19 +0200584 return ide_get_identity_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 case HDIO_GET_NICE:
Bartlomiej Zolnierkiewicz9fa295e2008-10-10 22:39:29 +0200586 return ide_get_nice_ioctl(drive, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587#ifdef CONFIG_IDE_TASK_IOCTL
588 case HDIO_DRIVE_TASKFILE:
589 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
590 return -EACCES;
591 switch(drive->media) {
592 case ide_disk:
593 return ide_taskfile_ioctl(drive, cmd, arg);
594 default:
595 return -ENOMSG;
596 }
597#endif /* CONFIG_IDE_TASK_IOCTL */
598
599 case HDIO_DRIVE_CMD:
600 if (!capable(CAP_SYS_RAWIO))
601 return -EACCES;
602 return ide_cmd_ioctl(drive, cmd, arg);
603
604 case HDIO_DRIVE_TASK:
605 if (!capable(CAP_SYS_RAWIO))
606 return -EACCES;
607 return ide_task_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 case HDIO_SET_NICE:
Bartlomiej Zolnierkiewicz9fa295e2008-10-10 22:39:29 +0200609 if (!capable(CAP_SYS_ADMIN))
610 return -EACCES;
611 return ide_set_nice_ioctl(drive, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 case HDIO_DRIVE_RESET:
Bartlomiej Zolnierkiewiczdbecebc2008-02-26 21:50:35 +0100613 if (!capable(CAP_SYS_ADMIN))
614 return -EACCES;
615
Elias Oltmanns64a8f002008-07-16 20:33:48 +0200616 return generic_drive_reset(drive);
Elias Oltmanns79e36a92008-07-16 20:33:48 +0200617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 case HDIO_GET_BUSSTATE:
619 if (!capable(CAP_SYS_ADMIN))
620 return -EACCES;
Bartlomiej Zolnierkiewicz9232c142008-10-10 22:39:33 +0200621 if (put_user(BUSSTATE_ON, (long __user *)arg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return -EFAULT;
623 return 0;
624
625 case HDIO_SET_BUSSTATE:
626 if (!capable(CAP_SYS_ADMIN))
627 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return -EOPNOTSUPP;
629 default:
630 return -EINVAL;
631 }
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200632
633read_val:
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200634 mutex_lock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200635 spin_lock_irqsave(&ide_lock, flags);
Bartlomiej Zolnierkiewicz8185d5a2008-10-10 22:39:28 +0200636 err = getfunc(drive);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200637 spin_unlock_irqrestore(&ide_lock, flags);
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200638 mutex_unlock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200639 return err >= 0 ? put_user(err, (long __user *)arg) : err;
640
641set_val:
642 if (bdev != bdev->bd_contains)
643 err = -EINVAL;
644 else {
645 if (!capable(CAP_SYS_ADMIN))
646 err = -EACCES;
647 else {
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200648 mutex_lock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200649 err = setfunc(drive, arg);
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200650 mutex_unlock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200651 }
652 }
653 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
656EXPORT_SYMBOL(generic_ide_ioctl);
657
Bartlomiej Zolnierkiewicz08da5912008-07-24 22:53:15 +0200658/**
659 * ide_device_get - get an additional reference to a ide_drive_t
660 * @drive: device to get a reference to
661 *
662 * Gets a reference to the ide_drive_t and increments the use count of the
663 * underlying LLDD module.
664 */
665int ide_device_get(ide_drive_t *drive)
666{
667 struct device *host_dev;
668 struct module *module;
669
670 if (!get_device(&drive->gendev))
671 return -ENXIO;
672
673 host_dev = drive->hwif->host->dev[0];
674 module = host_dev ? host_dev->driver->owner : NULL;
675
676 if (module && !try_module_get(module)) {
677 put_device(&drive->gendev);
678 return -ENXIO;
679 }
680
681 return 0;
682}
683EXPORT_SYMBOL_GPL(ide_device_get);
684
685/**
686 * ide_device_put - release a reference to a ide_drive_t
687 * @drive: device to release a reference on
688 *
689 * Release a reference to the ide_drive_t and decrements the use count of
690 * the underlying LLDD module.
691 */
692void ide_device_put(ide_drive_t *drive)
693{
694#ifdef CONFIG_MODULE_UNLOAD
695 struct device *host_dev = drive->hwif->host->dev[0];
696 struct module *module = host_dev ? host_dev->driver->owner : NULL;
697
698 if (module)
699 module_put(module);
700#endif
701 put_device(&drive->gendev);
702}
703EXPORT_SYMBOL_GPL(ide_device_put);
704
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200705static int ide_bus_match(struct device *dev, struct device_driver *drv)
706{
707 return 1;
708}
709
Kay Sievers263756e2005-12-12 18:03:44 +0100710static char *media_string(ide_drive_t *drive)
711{
712 switch (drive->media) {
713 case ide_disk:
714 return "disk";
715 case ide_cdrom:
716 return "cdrom";
717 case ide_tape:
718 return "tape";
719 case ide_floppy:
720 return "floppy";
Danny Kukawkaa7a832d2007-04-10 22:39:14 +0200721 case ide_optical:
722 return "optical";
Kay Sievers263756e2005-12-12 18:03:44 +0100723 default:
724 return "UNKNOWN";
725 }
726}
727
728static ssize_t media_show(struct device *dev, struct device_attribute *attr, char *buf)
729{
730 ide_drive_t *drive = to_ide_device(dev);
731 return sprintf(buf, "%s\n", media_string(drive));
732}
733
734static ssize_t drivename_show(struct device *dev, struct device_attribute *attr, char *buf)
735{
736 ide_drive_t *drive = to_ide_device(dev);
737 return sprintf(buf, "%s\n", drive->name);
738}
739
740static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
741{
742 ide_drive_t *drive = to_ide_device(dev);
743 return sprintf(buf, "ide:m-%s\n", media_string(drive));
744}
745
Bartlomiej Zolnierkiewicze11b9032007-12-12 23:31:58 +0100746static ssize_t model_show(struct device *dev, struct device_attribute *attr,
747 char *buf)
748{
749 ide_drive_t *drive = to_ide_device(dev);
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200750 return sprintf(buf, "%s\n", (char *)&drive->id[ATA_ID_PROD]);
Bartlomiej Zolnierkiewicze11b9032007-12-12 23:31:58 +0100751}
752
753static ssize_t firmware_show(struct device *dev, struct device_attribute *attr,
754 char *buf)
755{
756 ide_drive_t *drive = to_ide_device(dev);
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200757 return sprintf(buf, "%s\n", (char *)&drive->id[ATA_ID_FW_REV]);
Bartlomiej Zolnierkiewicze11b9032007-12-12 23:31:58 +0100758}
759
760static ssize_t serial_show(struct device *dev, struct device_attribute *attr,
761 char *buf)
762{
763 ide_drive_t *drive = to_ide_device(dev);
Bartlomiej Zolnierkiewicz4dde4492008-10-10 22:39:19 +0200764 return sprintf(buf, "%s\n", (char *)&drive->id[ATA_ID_SERNO]);
Bartlomiej Zolnierkiewicze11b9032007-12-12 23:31:58 +0100765}
766
Kay Sievers263756e2005-12-12 18:03:44 +0100767static struct device_attribute ide_dev_attrs[] = {
768 __ATTR_RO(media),
769 __ATTR_RO(drivename),
770 __ATTR_RO(modalias),
Bartlomiej Zolnierkiewicze11b9032007-12-12 23:31:58 +0100771 __ATTR_RO(model),
772 __ATTR_RO(firmware),
773 __ATTR(serial, 0400, serial_show, NULL),
Kay Sievers263756e2005-12-12 18:03:44 +0100774 __ATTR_NULL
775};
776
Kay Sievers7eff2e72007-08-14 15:15:12 +0200777static int ide_uevent(struct device *dev, struct kobj_uevent_env *env)
Kay Sievers263756e2005-12-12 18:03:44 +0100778{
779 ide_drive_t *drive = to_ide_device(dev);
Kay Sievers263756e2005-12-12 18:03:44 +0100780
Kay Sievers7eff2e72007-08-14 15:15:12 +0200781 add_uevent_var(env, "MEDIA=%s", media_string(drive));
782 add_uevent_var(env, "DRIVENAME=%s", drive->name);
783 add_uevent_var(env, "MODALIAS=ide:m-%s", media_string(drive));
Kay Sievers263756e2005-12-12 18:03:44 +0100784 return 0;
785}
786
Russell King4031bbe2006-01-06 11:41:00 +0000787static int generic_ide_probe(struct device *dev)
788{
789 ide_drive_t *drive = to_ide_device(dev);
790 ide_driver_t *drv = to_ide_driver(dev->driver);
791
792 return drv->probe ? drv->probe(drive) : -ENODEV;
793}
794
795static int generic_ide_remove(struct device *dev)
796{
797 ide_drive_t *drive = to_ide_device(dev);
798 ide_driver_t *drv = to_ide_driver(dev->driver);
799
800 if (drv->remove)
801 drv->remove(drive);
802
803 return 0;
804}
805
806static void generic_ide_shutdown(struct device *dev)
807{
808 ide_drive_t *drive = to_ide_device(dev);
809 ide_driver_t *drv = to_ide_driver(dev->driver);
810
811 if (dev->driver && drv->shutdown)
812 drv->shutdown(drive);
813}
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815struct bus_type ide_bus_type = {
816 .name = "ide",
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200817 .match = ide_bus_match,
Kay Sievers263756e2005-12-12 18:03:44 +0100818 .uevent = ide_uevent,
Russell King4031bbe2006-01-06 11:41:00 +0000819 .probe = generic_ide_probe,
820 .remove = generic_ide_remove,
821 .shutdown = generic_ide_shutdown,
Kay Sievers263756e2005-12-12 18:03:44 +0100822 .dev_attrs = ide_dev_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 .suspend = generic_ide_suspend,
824 .resume = generic_ide_resume,
825};
826
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200827EXPORT_SYMBOL_GPL(ide_bus_type);
828
Bartlomiej Zolnierkiewiczebae41a2008-04-27 15:38:29 +0200829int ide_vlb_clk;
830EXPORT_SYMBOL_GPL(ide_vlb_clk);
831
832module_param_named(vlb_clock, ide_vlb_clk, int, 0);
833MODULE_PARM_DESC(vlb_clock, "VLB clock frequency (in MHz)");
834
835int ide_pci_clk;
836EXPORT_SYMBOL_GPL(ide_pci_clk);
837
838module_param_named(pci_clock, ide_pci_clk, int, 0);
839MODULE_PARM_DESC(pci_clock, "PCI bus clock frequency (in MHz)");
840
Bartlomiej Zolnierkiewicz6e875432008-04-27 15:38:30 +0200841static int ide_set_dev_param_mask(const char *s, struct kernel_param *kp)
842{
843 int a, b, i, j = 1;
844 unsigned int *dev_param_mask = (unsigned int *)kp->arg;
845
846 if (sscanf(s, "%d.%d:%d", &a, &b, &j) != 3 &&
847 sscanf(s, "%d.%d", &a, &b) != 2)
848 return -EINVAL;
849
850 i = a * MAX_DRIVES + b;
851
852 if (i >= MAX_HWIFS * MAX_DRIVES || j < 0 || j > 1)
853 return -EINVAL;
854
855 if (j)
856 *dev_param_mask |= (1 << i);
857 else
858 *dev_param_mask &= (1 << i);
859
860 return 0;
861}
862
863static unsigned int ide_nodma;
864
865module_param_call(nodma, ide_set_dev_param_mask, NULL, &ide_nodma, 0);
866MODULE_PARM_DESC(nodma, "disallow DMA for a device");
867
868static unsigned int ide_noflush;
869
870module_param_call(noflush, ide_set_dev_param_mask, NULL, &ide_noflush, 0);
871MODULE_PARM_DESC(noflush, "disable flush requests for a device");
872
873static unsigned int ide_noprobe;
874
875module_param_call(noprobe, ide_set_dev_param_mask, NULL, &ide_noprobe, 0);
876MODULE_PARM_DESC(noprobe, "skip probing for a device");
877
878static unsigned int ide_nowerr;
879
880module_param_call(nowerr, ide_set_dev_param_mask, NULL, &ide_nowerr, 0);
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200881MODULE_PARM_DESC(nowerr, "ignore the ATA_DF bit for a device");
Bartlomiej Zolnierkiewicz6e875432008-04-27 15:38:30 +0200882
Bartlomiej Zolnierkiewicz4706a7e2008-04-27 15:38:30 +0200883static unsigned int ide_cdroms;
884
885module_param_call(cdrom, ide_set_dev_param_mask, NULL, &ide_cdroms, 0);
886MODULE_PARM_DESC(cdrom, "force device as a CD-ROM");
887
888struct chs_geom {
889 unsigned int cyl;
890 u8 head;
891 u8 sect;
892};
893
894static unsigned int ide_disks;
895static struct chs_geom ide_disks_chs[MAX_HWIFS * MAX_DRIVES];
896
897static int ide_set_disk_chs(const char *str, struct kernel_param *kp)
898{
899 int a, b, c = 0, h = 0, s = 0, i, j = 1;
900
901 if (sscanf(str, "%d.%d:%d,%d,%d", &a, &b, &c, &h, &s) != 5 &&
902 sscanf(str, "%d.%d:%d", &a, &b, &j) != 3)
903 return -EINVAL;
904
905 i = a * MAX_DRIVES + b;
906
907 if (i >= MAX_HWIFS * MAX_DRIVES || j < 0 || j > 1)
908 return -EINVAL;
909
910 if (c > INT_MAX || h > 255 || s > 255)
911 return -EINVAL;
912
913 if (j)
914 ide_disks |= (1 << i);
915 else
916 ide_disks &= (1 << i);
917
918 ide_disks_chs[i].cyl = c;
919 ide_disks_chs[i].head = h;
920 ide_disks_chs[i].sect = s;
921
922 return 0;
923}
924
925module_param_call(chs, ide_set_disk_chs, NULL, NULL, 0);
926MODULE_PARM_DESC(chs, "force device as a disk (using CHS)");
927
Bartlomiej Zolnierkiewicz6e875432008-04-27 15:38:30 +0200928static void ide_dev_apply_params(ide_drive_t *drive)
929{
930 int i = drive->hwif->index * MAX_DRIVES + drive->select.b.unit;
931
932 if (ide_nodma & (1 << i)) {
933 printk(KERN_INFO "ide: disallowing DMA for %s\n", drive->name);
934 drive->nodma = 1;
935 }
936 if (ide_noflush & (1 << i)) {
937 printk(KERN_INFO "ide: disabling flush requests for %s\n",
938 drive->name);
939 drive->noflush = 1;
940 }
941 if (ide_noprobe & (1 << i)) {
942 printk(KERN_INFO "ide: skipping probe for %s\n", drive->name);
943 drive->noprobe = 1;
944 }
945 if (ide_nowerr & (1 << i)) {
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200946 printk(KERN_INFO "ide: ignoring the ATA_DF bit for %s\n",
Bartlomiej Zolnierkiewicz6e875432008-04-27 15:38:30 +0200947 drive->name);
948 drive->bad_wstat = BAD_R_STAT;
949 }
Bartlomiej Zolnierkiewicz4706a7e2008-04-27 15:38:30 +0200950 if (ide_cdroms & (1 << i)) {
951 printk(KERN_INFO "ide: forcing %s as a CD-ROM\n", drive->name);
952 drive->present = 1;
953 drive->media = ide_cdrom;
954 /* an ATAPI device ignores DRDY */
955 drive->ready_stat = 0;
956 }
957 if (ide_disks & (1 << i)) {
958 drive->cyl = drive->bios_cyl = ide_disks_chs[i].cyl;
959 drive->head = drive->bios_head = ide_disks_chs[i].head;
960 drive->sect = drive->bios_sect = ide_disks_chs[i].sect;
961 drive->forced_geom = 1;
962 printk(KERN_INFO "ide: forcing %s as a disk (%d/%d/%d)\n",
963 drive->name,
964 drive->cyl, drive->head, drive->sect);
965 drive->present = 1;
966 drive->media = ide_disk;
Bartlomiej Zolnierkiewicz3a7d2482008-10-10 22:39:21 +0200967 drive->ready_stat = ATA_DRDY;
Bartlomiej Zolnierkiewicz4706a7e2008-04-27 15:38:30 +0200968 }
Bartlomiej Zolnierkiewicz6e875432008-04-27 15:38:30 +0200969}
970
Bartlomiej Zolnierkiewicz9fd91d92008-04-27 15:38:23 +0200971static unsigned int ide_ignore_cable;
972
973static int ide_set_ignore_cable(const char *s, struct kernel_param *kp)
974{
975 int i, j = 1;
976
977 if (sscanf(s, "%d:%d", &i, &j) != 2 && sscanf(s, "%d", &i) != 1)
978 return -EINVAL;
979
980 if (i >= MAX_HWIFS || j < 0 || j > 1)
981 return -EINVAL;
982
983 if (j)
984 ide_ignore_cable |= (1 << i);
985 else
986 ide_ignore_cable &= (1 << i);
987
988 return 0;
989}
990
991module_param_call(ignore_cable, ide_set_ignore_cable, NULL, NULL, 0);
992MODULE_PARM_DESC(ignore_cable, "ignore cable detection");
993
994void ide_port_apply_params(ide_hwif_t *hwif)
995{
Bartlomiej Zolnierkiewicz6e875432008-04-27 15:38:30 +0200996 int i;
997
Bartlomiej Zolnierkiewicz9fd91d92008-04-27 15:38:23 +0200998 if (ide_ignore_cable & (1 << hwif->index)) {
999 printk(KERN_INFO "ide: ignoring cable detection for %s\n",
1000 hwif->name);
1001 hwif->cbl = ATA_CBL_PATA40_SHORT;
1002 }
Bartlomiej Zolnierkiewicz6e875432008-04-27 15:38:30 +02001003
1004 for (i = 0; i < MAX_DRIVES; i++)
1005 ide_dev_apply_params(&hwif->drives[i]);
Bartlomiej Zolnierkiewicz9fd91d92008-04-27 15:38:23 +02001006}
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008/*
1009 * This is gets invoked once during initialization, to set *everything* up
1010 */
1011static int __init ide_init(void)
1012{
Randy Dunlap349ae232006-10-03 01:14:23 -07001013 int ret;
1014
Bartlomiej Zolnierkiewiczeba8ff92008-02-11 00:32:13 +01001015 printk(KERN_INFO "Uniform Multi-Platform E-IDE driver\n");
Bartlomiej Zolnierkiewicz537f06c2008-02-01 23:09:35 +01001016
Randy Dunlap349ae232006-10-03 01:14:23 -07001017 ret = bus_register(&ide_bus_type);
1018 if (ret < 0) {
1019 printk(KERN_WARNING "IDE: bus_register error: %d\n", ret);
1020 return ret;
1021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +02001023 ide_port_class = class_create(THIS_MODULE, "ide_port");
1024 if (IS_ERR(ide_port_class)) {
1025 ret = PTR_ERR(ide_port_class);
1026 goto out_port_class;
1027 }
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +02001028
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +02001029 proc_ide_create();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return 0;
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +02001032
1033out_port_class:
1034 bus_unregister(&ide_bus_type);
1035
1036 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
Bartlomiej Zolnierkiewicz931ee0d2008-07-15 21:21:47 +02001039static void __exit ide_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 proc_ide_destroy();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +02001043 class_destroy(ide_port_class);
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 bus_unregister(&ide_bus_type);
1046}
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048module_init(ide_init);
Bartlomiej Zolnierkiewicz931ee0d2008-07-15 21:21:47 +02001049module_exit(ide_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Bartlomiej Zolnierkiewicz931ee0d2008-07-15 21:21:47 +02001051MODULE_LICENSE("GPL");