blob: 917c72dcd33dccc806c42c20d9662750ebe52b89 [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)
3 * Copyrifht (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#define _IDE_C /* Tell ide.h it's really us */
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#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>
71
72#include <asm/byteorder.h>
73#include <asm/irq.h>
74#include <asm/uaccess.h>
75#include <asm/io.h>
76
77
78/* default maximum number of failures */
79#define IDE_DEFAULT_MAX_FAILURES 1
80
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +020081struct class *ide_port_class;
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static 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 };
88
89static int idebus_parameter; /* holds the "idebus=" parameter */
90static int system_bus_speed; /* holds what we think is VESA/PCI bus speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Matthias Kaehlckeef298882007-07-09 23:17:55 +020092DEFINE_MUTEX(ide_cfg_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 __cacheline_aligned_in_smp DEFINE_SPINLOCK(ide_lock);
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095int noautodma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Hannes Reineckee3a59b42007-02-07 18:19:37 +010097#ifdef CONFIG_BLK_DEV_IDEACPI
98int ide_noacpi = 0;
99int ide_noacpitfs = 1;
100int ide_noacpionboot = 1;
101#endif
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/*
104 * This is declared extern in ide.h, for access by other IDE modules:
105 */
106ide_hwif_t ide_hwifs[MAX_HWIFS]; /* master data repository */
107
108EXPORT_SYMBOL(ide_hwifs);
109
Bartlomiej Zolnierkiewicz43514ed2008-04-18 00:46:22 +0200110static void ide_port_init_devices_data(ide_hwif_t *);
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/*
113 * Do not even *think* about calling this!
114 */
Bartlomiej Zolnierkiewiczcbb010c2008-01-26 20:13:06 +0100115void ide_init_port_data(ide_hwif_t *hwif, unsigned int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 /* bulk initialize hwif & drive info with zeros */
118 memset(hwif, 0, sizeof(ide_hwif_t));
119
120 /* fill in any non-zero initial values */
121 hwif->index = index;
122 hwif->major = ide_hwif_to_major[index];
123
124 hwif->name[0] = 'i';
125 hwif->name[1] = 'd';
126 hwif->name[2] = 'e';
127 hwif->name[3] = '0' + index;
128
129 hwif->bus_state = BUSSTATE_ON;
130
Aleksey Makarovf36d4022006-01-09 15:59:27 -0800131 init_completion(&hwif->gendev_rel_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 default_hwif_iops(hwif);
134 default_hwif_transport(hwif);
Bartlomiej Zolnierkiewicz43514ed2008-04-18 00:46:22 +0200135
136 ide_port_init_devices_data(hwif);
137}
138EXPORT_SYMBOL_GPL(ide_init_port_data);
139
140static void ide_port_init_devices_data(ide_hwif_t *hwif)
141{
142 int unit;
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 for (unit = 0; unit < MAX_DRIVES; ++unit) {
145 ide_drive_t *drive = &hwif->drives[unit];
Bartlomiej Zolnierkiewicz43514ed2008-04-18 00:46:22 +0200146 u8 j = (hwif->index * MAX_DRIVES) + unit;
147
148 memset(drive, 0, sizeof(*drive));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
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';
Bartlomiej Zolnierkiewicz43514ed2008-04-18 00:46:22 +0200160 drive->name[2] = 'a' + j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 drive->max_failures = IDE_DEFAULT_MAX_FAILURES;
Bartlomiej Zolnierkiewicz43514ed2008-04-18 00:46:22 +0200162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 INIT_LIST_HEAD(&drive->list);
Aleksey Makarovf36d4022006-01-09 15:59:27 -0800164 init_completion(&drive->gendev_rel_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166}
Bartlomiej Zolnierkiewicz43514ed2008-04-18 00:46:22 +0200167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/*
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).
172 *
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.
177 *
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.
181 *
182 * FIXME: We should stuff the setup data into __init and copy the
183 * relevant hwifs/allocate them properly during boot.
184 */
185#define MAGIC_COOKIE 0x12345678
186static void __init init_ide_data (void)
187{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 unsigned int index;
189 static unsigned long magic_cookie = MAGIC_COOKIE;
190
191 if (magic_cookie != MAGIC_COOKIE)
192 return; /* already initialized */
193 magic_cookie = 0;
194
195 /* Initialise all interface structures */
196 for (index = 0; index < MAX_HWIFS; ++index) {
Bartlomiej Zolnierkiewicz05734262008-04-18 00:46:25 +0200197 ide_hwif_t *hwif = &ide_hwifs[index];
198
Bartlomiej Zolnierkiewiczcbb010c2008-01-26 20:13:06 +0100199 ide_init_port_data(hwif, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
203/**
204 * ide_system_bus_speed - guess bus speed
205 *
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.
212 *
213 * Returns a guessed speed in MHz.
214 */
215
216static int ide_system_bus_speed(void)
217{
218#ifdef CONFIG_PCI
219 static struct pci_device_id pci_default[] = {
220 { PCI_DEVICE(PCI_ANY_ID, PCI_ANY_ID) },
221 { }
222 };
223#else
224#define pci_default 0
225#endif /* CONFIG_PCI */
226
Bartlomiej Zolnierkiewicz537f06c2008-02-01 23:09:35 +0100227 /* user supplied value */
228 if (idebus_parameter)
229 return idebus_parameter;
230
231 /* safe default value for PCI or VESA and PCI*/
232 return pci_dev_present(pci_default) ? 33 : 50;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Bartlomiej Zolnierkiewiczbaa8f3e2007-10-20 00:32:31 +0200235ide_hwif_t * ide_find_port(unsigned long base)
236{
237 ide_hwif_t *hwif;
238 int i;
239
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;
244 }
245
246 for (i = 0; i < MAX_HWIFS; i++) {
247 hwif = &ide_hwifs[i];
Bartlomiej Zolnierkiewicz2625cd52008-04-18 00:46:21 +0200248 if (hwif->chipset == ide_unknown)
Bartlomiej Zolnierkiewiczbaa8f3e2007-10-20 00:32:31 +0200249 goto found;
250 }
251
252 hwif = NULL;
253found:
254 return hwif;
255}
256
257EXPORT_SYMBOL_GPL(ide_find_port);
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259static struct resource* hwif_request_region(ide_hwif_t *hwif,
260 unsigned long addr, int num)
261{
262 struct resource *res = request_region(addr, num, hwif->name);
263
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;
268}
269
270/**
271 * ide_hwif_request_regions - request resources for IDE
272 * @hwif: interface to use
273 *
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.
278 */
279
280int ide_hwif_request_regions(ide_hwif_t *hwif)
281{
282 unsigned long addr;
283 unsigned int i;
284
Bartlomiej Zolnierkiewicz2ad1e552007-02-17 02:40:25 +0100285 if (hwif->mmio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 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;
297 }
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;
304 }
305 }
306 return 0;
307
308data_region_busy:
309 addr = hwif->io_ports[IDE_CONTROL_OFFSET];
310 if (addr)
311 release_region(addr, 1);
312control_region_busy:
313 /* If any errors are return, we drop the hwif interface. */
314 return -EBUSY;
315}
316
317/**
318 * ide_hwif_release_regions - free IDE resources
319 *
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.
323 *
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.
327 */
328
329void ide_hwif_release_regions(ide_hwif_t *hwif)
330{
331 u32 i = 0;
332
Bartlomiej Zolnierkiewicz2ad1e552007-02-17 02:40:25 +0100333 if (hwif->mmio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 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;
340 }
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);
344}
345
Bartlomiej Zolnierkiewiczfbd13082008-02-01 23:09:36 +0100346void ide_remove_port_from_hwgroup(ide_hwif_t *hwif)
Bartlomiej Zolnierkiewicz96e5ad32008-02-01 23:09:35 +0100347{
348 ide_hwgroup_t *hwgroup = hwif->hwgroup;
349
350 spin_lock_irq(&ide_lock);
351 /*
352 * Remove us from the hwgroup, and free
353 * the hwgroup if we were the only member
354 */
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.
362 */
363 ide_hwif_t *g = hwgroup->hwif;
364
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.
372 */
373 BUG_ON(hwgroup->drive != NULL);
374 hwgroup->hwif = g;
375 }
376 BUG_ON(hwgroup->hwif == hwif);
377 }
378 spin_unlock_irq(&ide_lock);
379}
380
Bartlomiej Zolnierkiewicz71bf9f62008-04-18 00:46:22 +0200381/* Called with ide_lock held. */
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +0200382static void __ide_port_unregister_devices(ide_hwif_t *hwif)
Bartlomiej Zolnierkiewicz71bf9f62008-04-18 00:46:22 +0200383{
384 int i;
385
386 for (i = 0; i < MAX_DRIVES; i++) {
387 ide_drive_t *drive = &hwif->drives[i];
388
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);
394 }
395 }
396}
397
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +0200398void ide_port_unregister_devices(ide_hwif_t *hwif)
399{
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);
407}
408EXPORT_SYMBOL_GPL(ide_port_unregister_devices);
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410/**
Sergei Shtylylov020e3222006-10-03 01:14:13 -0700411 * ide_unregister - free an IDE interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 * @index: index of interface (will change soon to a pointer)
413 *
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.
416 *
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
Sergei Shtylylov020e3222006-10-03 01:14:13 -0700424 * the interface will not be reopened (present/vanishing locking
425 * isn't yet done BTW). After we commit to the final kill we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 * call the cleanup callback with the ide locks held.
427 *
428 * Unregister restores the hwif structures to the default state.
429 * This is raving bonkers.
430 */
431
Bartlomiej Zolnierkiewicz93de00f2008-04-18 00:46:24 +0200432void ide_unregister(unsigned int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 ide_hwif_t *hwif, *g;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 ide_hwgroup_t *hwgroup;
Bartlomiej Zolnierkiewicz71bf9f62008-04-18 00:46:22 +0200436 int irq_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 BUG_ON(index >= MAX_HWIFS);
439
440 BUG_ON(in_interrupt());
441 BUG_ON(irqs_disabled());
Matthias Kaehlckeef298882007-07-09 23:17:55 +0200442 mutex_lock(&ide_cfg_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 spin_lock_irq(&ide_lock);
444 hwif = &ide_hwifs[index];
445 if (!hwif->present)
446 goto abort;
Bartlomiej Zolnierkiewicz2dde7862008-04-18 00:46:23 +0200447 __ide_port_unregister_devices(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 hwif->present = 0;
449
450 spin_unlock_irq(&ide_lock);
451
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200452 ide_proc_unregister_port(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 hwgroup = hwif->hwgroup;
455 /*
456 * free the irq if we were the only hwif using it
457 */
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);
466
Bartlomiej Zolnierkiewicz96e5ad32008-02-01 23:09:35 +0100467 ide_remove_port_from_hwgroup(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +0200469 device_unregister(hwif->portdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 device_unregister(&hwif->gendev);
Aleksey Makarovf36d4022006-01-09 15:59:27 -0800471 wait_for_completion(&hwif->gendev_rel_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 /*
474 * Remove us from the kernel's knowledge
475 */
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);
480
Bartlomiej Zolnierkiewicz93de00f2008-04-18 00:46:24 +0200481 if (hwif->dma_base)
482 (void)ide_release_dma(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Bartlomiej Zolnierkiewicz7cba97f2008-02-01 23:09:35 +0100484 ide_hwif_release_regions(hwif);
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 /* restore hwif data to pristine status */
Bartlomiej Zolnierkiewiczcbb010c2008-01-26 20:13:06 +0100487 ide_init_port_data(hwif, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489abort:
490 spin_unlock_irq(&ide_lock);
Matthias Kaehlckeef298882007-07-09 23:17:55 +0200491 mutex_unlock(&ide_cfg_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
494EXPORT_SYMBOL(ide_unregister);
495
Bartlomiej Zolnierkiewicz57c802e2008-01-26 20:13:05 +0100496void ide_init_port_hw(ide_hwif_t *hwif, hw_regs_t *hw)
497{
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;
504}
505EXPORT_SYMBOL_GPL(ide_init_port_hw);
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507/*
508 * Locks for IDE setting functionality
509 */
510
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200511DEFINE_MUTEX(ide_setting_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200513EXPORT_SYMBOL_GPL(ide_setting_mtx);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 * ide_spin_wait_hwgroup - wait for group
517 * @drive: drive in the group
518 *
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.
522 */
523
524int ide_spin_wait_hwgroup (ide_drive_t *drive)
525{
526 ide_hwgroup_t *hwgroup = HWGROUP(drive);
527 unsigned long timeout = jiffies + (3 * HZ);
528
529 spin_lock_irq(&ide_lock);
530
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;
539 }
540 local_irq_restore(lflags);
541 spin_lock_irq(&ide_lock);
542 }
543 return 0;
544}
545
546EXPORT_SYMBOL(ide_spin_wait_hwgroup);
547
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200548int set_io_32bit(ide_drive_t *drive, int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200550 if (drive->no_io_32bit)
551 return -EPERM;
552
553 if (arg < 0 || arg > 1 + (SUPPORT_VLB_SYNC << 1))
554 return -EINVAL;
555
Bartlomiej Zolnierkiewicz644a9d72007-12-12 23:32:00 +0100556 if (ide_spin_wait_hwgroup(drive))
557 return -EBUSY;
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 drive->io_32bit = arg;
Bartlomiej Zolnierkiewicz644a9d72007-12-12 23:32:00 +0100560
561 spin_unlock_irq(&ide_lock);
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return 0;
564}
565
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200566static int set_ksettings(ide_drive_t *drive, int arg)
567{
568 if (arg < 0 || arg > 1)
569 return -EINVAL;
570
571 if (ide_spin_wait_hwgroup(drive))
572 return -EBUSY;
573 drive->keep_settings = arg;
574 spin_unlock_irq(&ide_lock);
575
576 return 0;
577}
578
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200579int set_using_dma(ide_drive_t *drive, int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
581#ifdef CONFIG_BLK_DEV_IDEDMA
Bartlomiej Zolnierkiewicz87996202007-03-26 23:03:19 +0200582 ide_hwif_t *hwif = drive->hwif;
583 int err = -EPERM;
584
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200585 if (arg < 0 || arg > 1)
586 return -EINVAL;
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (!drive->id || !(drive->id->capability & 1))
Bartlomiej Zolnierkiewicz87996202007-03-26 23:03:19 +0200589 goto out;
590
Bartlomiej Zolnierkiewicz15ce9262008-01-26 20:13:03 +0100591 if (hwif->dma_host_set == NULL)
Bartlomiej Zolnierkiewicz87996202007-03-26 23:03:19 +0200592 goto out;
593
594 err = -EBUSY;
595 if (ide_spin_wait_hwgroup(drive))
596 goto out;
597 /*
598 * set ->busy flag, unlock and let it ride
599 */
600 hwif->hwgroup->busy = 1;
601 spin_unlock_irq(&ide_lock);
602
603 err = 0;
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (arg) {
Bartlomiej Zolnierkiewicz23b1bd42008-01-25 22:17:19 +0100606 if (ide_set_dma(drive))
Bartlomiej Zolnierkiewicz87996202007-03-26 23:03:19 +0200607 err = -EIO;
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100608 } else
609 ide_dma_off(drive);
Bartlomiej Zolnierkiewicz87996202007-03-26 23:03:19 +0200610
611 /*
612 * lock, clear ->busy flag and unlock before leaving
613 */
614 spin_lock_irq(&ide_lock);
615 hwif->hwgroup->busy = 0;
616 spin_unlock_irq(&ide_lock);
617out:
618 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619#else
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200620 if (arg < 0 || arg > 1)
621 return -EINVAL;
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return -EPERM;
624#endif
625}
626
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200627int set_pio_mode(ide_drive_t *drive, int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 struct request rq;
630
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200631 if (arg < 0 || arg > 255)
632 return -EINVAL;
633
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200634 if (drive->hwif->set_pio_mode == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return -ENOSYS;
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (drive->special.b.set_tune)
638 return -EBUSY;
Bartlomiej Zolnierkiewicz145b75e2008-01-26 20:13:11 +0100639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 ide_init_drive_cmd(&rq);
Bartlomiej Zolnierkiewicz852738f2008-01-26 20:13:12 +0100641 rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
Bartlomiej Zolnierkiewicz145b75e2008-01-26 20:13:11 +0100642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 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;
647}
648
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200649static int set_unmaskirq(ide_drive_t *drive, int arg)
650{
651 if (drive->no_unmask)
652 return -EPERM;
653
654 if (arg < 0 || arg > 1)
655 return -EINVAL;
656
657 if (ide_spin_wait_hwgroup(drive))
658 return -EBUSY;
659 drive->unmask = arg;
660 spin_unlock_irq(&ide_lock);
661
662 return 0;
663}
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665/**
666 * system_bus_clock - clock guess
667 *
668 * External version of the bus clock guess used by very old IDE drivers
669 * for things like VLB timings. Should not be used.
670 */
671
672int system_bus_clock (void)
673{
Bartlomiej Zolnierkiewicz537f06c2008-02-01 23:09:35 +0100674 return system_bus_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
677EXPORT_SYMBOL(system_bus_clock);
678
David Brownellb887d2e2006-08-14 23:11:05 -0700679static int generic_ide_suspend(struct device *dev, pm_message_t mesg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 ide_drive_t *drive = dev->driver_data;
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100682 ide_hwif_t *hwif = HWIF(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 struct request rq;
684 struct request_pm_state rqpm;
685 ide_task_t args;
Shaohua Li5e321322007-10-11 23:53:58 +0200686 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100688 /* Call ACPI _GTM only once */
689 if (!(drive->dn % 2))
690 ide_acpi_get_timing(hwif);
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 memset(&rq, 0, sizeof(rq));
693 memset(&rqpm, 0, sizeof(rqpm));
694 memset(&args, 0, sizeof(args));
Jens Axboe4aff5e22006-08-10 08:44:47 +0200695 rq.cmd_type = REQ_TYPE_PM_SUSPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 rq.special = &args;
Jens Axboec00895a2006-09-30 20:29:12 +0200697 rq.data = &rqpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 rqpm.pm_step = ide_pm_state_start_suspend;
David Brownellb887d2e2006-08-14 23:11:05 -0700699 if (mesg.event == PM_EVENT_PRETHAW)
700 mesg.event = PM_EVENT_FREEZE;
701 rqpm.pm_state = mesg.event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Shaohua Li5e321322007-10-11 23:53:58 +0200703 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
713static int generic_ide_resume(struct device *dev)
714{
715 ide_drive_t *drive = dev->driver_data;
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100716 ide_hwif_t *hwif = HWIF(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 struct request rq;
718 struct request_pm_state rqpm;
719 ide_task_t args;
Lee Trager0d2157f2007-06-08 15:14:30 +0200720 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100722 /* Call ACPI _STM only once */
Shaohua Li5e321322007-10-11 23:53:58 +0200723 if (!(drive->dn % 2)) {
724 ide_acpi_set_state(hwif, 1);
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100725 ide_acpi_push_timing(hwif);
Shaohua Li5e321322007-10-11 23:53:58 +0200726 }
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100727
728 ide_acpi_exec_tfs(drive);
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 memset(&rq, 0, sizeof(rq));
731 memset(&rqpm, 0, sizeof(rqpm));
732 memset(&args, 0, sizeof(args));
Jens Axboe4aff5e22006-08-10 08:44:47 +0200733 rq.cmd_type = REQ_TYPE_PM_RESUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 rq.special = &args;
Jens Axboec00895a2006-09-30 20:29:12 +0200735 rq.data = &rqpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 rqpm.pm_step = ide_pm_state_start_resume;
Pavel Machekca078ba2005-09-03 15:56:57 -0700737 rqpm.pm_state = PM_EVENT_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Lee Trager0d2157f2007-06-08 15:14:30 +0200739 err = ide_do_drive_cmd(drive, &rq, ide_head_wait);
740
Rafael J. Wysockice9b2b02007-06-16 02:24:43 +0200741 if (err == 0 && dev->driver) {
742 ide_driver_t *drv = to_ide_driver(dev->driver);
743
744 if (drv->resume)
745 drv->resume(drive);
746 }
Lee Trager0d2157f2007-06-08 15:14:30 +0200747
748 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
751int generic_ide_ioctl(ide_drive_t *drive, struct file *file, struct block_device *bdev,
752 unsigned int cmd, unsigned long arg)
753{
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200754 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 ide_driver_t *drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 void __user *p = (void __user *)arg;
Linus Torvalds19850262007-07-17 15:57:42 -0700757 int err = 0, (*setfunc)(ide_drive_t *, int);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200758 u8 *val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200760 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 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;
782
783 case HDIO_GET_NICE:
784 return put_user(drive->dsc_overlap << IDE_NICE_DSC_OVERLAP |
785 drive->atapi_overlap << IDE_NICE_ATAPI_OVERLAP |
Bartlomiej Zolnierkiewicz92b83c82008-02-02 19:56:45 +0100786 drive->nice1 << IDE_NICE_1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 (long __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788#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;
797 }
798#endif /* CONFIG_IDE_TASK_IOCTL */
799
800 case HDIO_DRIVE_CMD:
801 if (!capable(CAP_SYS_RAWIO))
802 return -EACCES;
803 return ide_cmd_ioctl(drive, cmd, arg);
804
805 case HDIO_DRIVE_TASK:
806 if (!capable(CAP_SYS_RAWIO))
807 return -EACCES;
808 return ide_task_ioctl(drive, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 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;
818 }
819 drive->nice1 = (arg >> IDE_NICE_1) & 1;
820 return 0;
821 case HDIO_DRIVE_RESET:
Bartlomiej Zolnierkiewiczdbecebc2008-02-26 21:50:35 +0100822 if (!capable(CAP_SYS_ADMIN))
823 return -EACCES;
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 /*
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
831 */
832
833 spin_lock_irqsave(&ide_lock, flags);
834
Alan Cox913759a2006-10-03 01:14:33 -0700835 if (HWGROUP(drive)->resetting) {
836 spin_unlock_irqrestore(&ide_lock, flags);
837 return -EBUSY;
838 }
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 ide_abort(drive, "drive reset");
841
Eric Sesterhenn125e1872006-06-23 02:06:06 -0700842 BUG_ON(HWGROUP(drive)->handler);
Bartlomiej Zolnierkiewiczdbecebc2008-02-26 21:50:35 +0100843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 /* Ensure nothing gets queued after we
845 drop the lock. Reset will clear the busy */
Bartlomiej Zolnierkiewiczdbecebc2008-02-26 21:50:35 +0100846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 HWGROUP(drive)->busy = 1;
848 spin_unlock_irqrestore(&ide_lock, flags);
849 (void) ide_do_reset(drive);
850
851 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 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;
858
859 case HDIO_SET_BUSSTATE:
860 if (!capable(CAP_SYS_ADMIN))
861 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return -EOPNOTSUPP;
863 default:
864 return -EINVAL;
865 }
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200866
867read_val:
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200868 mutex_lock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200869 spin_lock_irqsave(&ide_lock, flags);
870 err = *val;
871 spin_unlock_irqrestore(&ide_lock, flags);
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200872 mutex_unlock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200873 return err >= 0 ? put_user(err, (long __user *)arg) : err;
874
875set_val:
876 if (bdev != bdev->bd_contains)
877 err = -EINVAL;
878 else {
879 if (!capable(CAP_SYS_ADMIN))
880 err = -EACCES;
881 else {
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200882 mutex_lock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200883 err = setfunc(drive, arg);
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200884 mutex_unlock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200885 }
886 }
887 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
890EXPORT_SYMBOL(generic_ide_ioctl);
891
892/*
893 * stridx() returns the offset of c within s,
894 * or -1 if c is '\0' or not found within s.
895 */
896static int __init stridx (const char *s, char c)
897{
898 char *i = strchr(s, c);
899 return (i && c) ? i - s : -1;
900}
901
902/*
903 * match_parm() does parsing for ide_setup():
904 *
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.
913 */
914static int __init match_parm (char *s, const char *keywords[], int vals[], int max_vals)
915{
916 static const char *decimal = "0123456789";
917 static const char *hex = "0123456789abcdef";
918 int i, n;
919
920 if (*s++ == '=') {
921 /*
922 * Try matching against the supplied keywords,
923 * and return -(index+1) if we match one
924 */
925 if (keywords != NULL) {
926 for (i = 0; *keywords != NULL; ++i) {
927 if (!strcmp(s, *keywords++))
928 return -(i+1);
929 }
930 }
931 /*
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.
936 */
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;
944 }
945 if (++n == max_vals)
946 break;
947 if (*s == ',' || *s == ';')
948 ++s;
949 }
950 if (!*s)
951 return n;
952 }
953 return 0; /* zero = nothing matched */
954}
955
Bartlomiej Zolnierkiewicz84913882007-03-03 17:48:55 +0100956extern int probe_ali14xx;
Bartlomiej Zolnierkiewicz84913882007-03-03 17:48:55 +0100957extern int probe_umc8672;
Bartlomiej Zolnierkiewicz84913882007-03-03 17:48:55 +0100958extern int probe_dtc2278;
Bartlomiej Zolnierkiewicz84913882007-03-03 17:48:55 +0100959extern int probe_ht6560b;
Bartlomiej Zolnierkiewicz84913882007-03-03 17:48:55 +0100960extern int probe_qd65xx;
Bartlomiej Zolnierkiewiczade2daf2008-01-26 20:13:07 +0100961extern int cmd640_vlb;
Bartlomiej Zolnierkiewiczffd4f6f2008-04-18 00:46:34 +0200962extern int probe_4drives;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Bartlomiej Zolnierkiewicz9a0e77f2008-04-18 00:46:24 +0200964static int __initdata is_chipset_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966/*
967 * ide_setup() gets called VERY EARLY during initialization,
968 * to handle kernel "command line" strings beginning with "hdx=" or "ide".
969 *
Randy Dunlap1c10e932008-03-07 21:53:50 +0100970 * Remember to update Documentation/ide/ide.txt if you change something here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 */
972static int __init ide_setup(char *s)
973{
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);
980
981
982 if (strncmp(s,"hd",2) == 0 && s[2] == '=') /* hd= is for hd.c */
983 return 0; /* driver and not us */
984
985 if (strncmp(s,"ide",3) && strncmp(s,"idebus",6) && strncmp(s,"hd",2))
986 return 0;
987
988 printk(KERN_INFO "ide_setup: %s", s);
989 init_ide_data ();
990
991#ifdef CONFIG_BLK_DEV_IDEDOUBLER
992 if (!strcmp(s, "ide=doubler")) {
993 extern int ide_doubler;
994
995 printk(" : Enabled support for IDE doublers\n");
996 ide_doubler = 1;
997 return 1;
998 }
999#endif /* CONFIG_BLK_DEV_IDEDOUBLER */
1000
1001 if (!strcmp(s, "ide=nodma")) {
1002 printk(" : Prevented DMA\n");
1003 noautodma = 1;
Bartlomiej Zolnierkiewiczc2237012007-10-16 22:29:58 +02001004 goto obsolete_option;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
1006
Hannes Reineckee3a59b42007-02-07 18:19:37 +01001007#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;
1012 }
1013 if (!strcmp(s, "ide=acpigtf")) {
1014 //printk(" : Enable IDE ACPI _GTF support.\n");
1015 ide_noacpitfs = 0;
1016 return 1;
1017 }
1018 if (!strcmp(s, "ide=acpionboot")) {
1019 //printk(" : Call IDE ACPI methods on boot.\n");
1020 ide_noacpionboot = 0;
1021 return 1;
1022 }
1023#endif /* CONFIG_BLK_DEV_IDEACPI */
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 /*
1026 * Look for drive options: "hdx="
1027 */
1028 if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
1029 const char *hd_words[] = {
Bartlomiej Zolnierkiewiczc2237012007-10-16 22:29:58 +02001030 "none", "noprobe", "nowerr", "cdrom", "nodma",
Bartlomiej Zolnierkiewicz9e47be02008-01-26 20:13:09 +01001031 "autotune", "noautotune", "-8", "-9", "-10",
Jens Axboe36193482006-07-28 08:54:59 +02001032 "noflush", "remap", "remap63", "scsi", NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 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));
Bartlomiej Zolnierkiewicz15220d92008-03-22 16:40:21 +01001040 goto obsolete_option;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
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;
Bartlomiej Zolnierkiewiczc2237012007-10-16 22:29:58 +02001058 case -5: /* nodma */
1059 drive->nodma = 1;
1060 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 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;
Jens Axboe36193482006-07-28 08:54:59 +02001067 case -11: /* noflush */
1068 drive->noflush = 1;
1069 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 case -12: /* "remap" */
1071 drive->remap_0_to_1 = 1;
Bartlomiej Zolnierkiewiczd708c402008-03-22 16:40:22 +01001072 goto obsolete_option;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 case -13: /* "remap63" */
1074 drive->sect0 = 63;
Bartlomiej Zolnierkiewiczd708c402008-03-22 16:40:22 +01001075 goto obsolete_option;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 case -14: /* "scsi" */
1077 drive->scsi = 1;
Bartlomiej Zolnierkiewicz15220d92008-03-22 16:40:21 +01001078 goto obsolete_option;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 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;
1091 }
1092 }
1093
1094 if (s[0] != 'i' || s[1] != 'd' || s[2] != 'e')
1095 goto bad_option;
1096 /*
1097 * Look for bus speed option: "idebus="
1098 */
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;
1107 }
1108 /*
1109 * Look for interface options: "idex="
1110 */
1111 if (s[3] >= '0' && s[3] <= max_hwif) {
1112 /*
1113 * Be VERY CAREFUL changing this: note hardcoded indexes below
1114 * (-8, -9, -10) are reserved to ease the hardcoding.
1115 */
1116 static const char *ide_words[] = {
Bartlomiej Zolnierkiewiczf01674e2008-04-18 00:46:35 +02001117 "minus1", "serialize", "minus3", "minus4",
Bartlomiej Zolnierkiewicz1b516942007-10-16 22:29:57 +02001118 "reset", "minus6", "ata66", "minus8", "minus9",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 "minus10", "four", "qd65xx", "ht6560b", "cmd640_vlb",
1120 "dtc2278", "umc8672", "ali14xx", NULL };
Bartlomiej Zolnierkiewicz9239b332007-10-20 00:32:33 +02001121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 hw = s[3] - '0';
1123 hwif = &ide_hwifs[hw];
1124 i = match_parm(&s[4], ide_words, vals, 3);
1125
1126 /*
1127 * Cryptic check to ensure chipset not already set for hwif.
1128 * Note: we can't depend on hwif->chipset here.
1129 */
Bartlomiej Zolnierkiewicz9a0e77f2008-04-18 00:46:24 +02001130 if (i >= -18 && i <= -11) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 /* chipset already specified */
Bartlomiej Zolnierkiewicz9a0e77f2008-04-18 00:46:24 +02001132 if (is_chipset_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 goto bad_option;
Bartlomiej Zolnierkiewicz9a0e77f2008-04-18 00:46:24 +02001134 /* these drivers are for "ide0=" only */
1135 if (hw != 0)
1136 goto bad_hwif;
1137 is_chipset_set = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 printk("\n");
1139 }
1140
1141 switch (i) {
1142#ifdef CONFIG_BLK_DEV_ALI14XX
1143 case -17: /* "ali14xx" */
1144 probe_ali14xx = 1;
Bartlomiej Zolnierkiewicz9c027c62008-03-22 16:40:21 +01001145 goto obsolete_option;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146#endif
1147#ifdef CONFIG_BLK_DEV_UMC8672
1148 case -16: /* "umc8672" */
1149 probe_umc8672 = 1;
Bartlomiej Zolnierkiewicz9c027c62008-03-22 16:40:21 +01001150 goto obsolete_option;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151#endif
1152#ifdef CONFIG_BLK_DEV_DTC2278
1153 case -15: /* "dtc2278" */
1154 probe_dtc2278 = 1;
Bartlomiej Zolnierkiewicz9c027c62008-03-22 16:40:21 +01001155 goto obsolete_option;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156#endif
1157#ifdef CONFIG_BLK_DEV_CMD640
1158 case -14: /* "cmd640_vlb" */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 cmd640_vlb = 1;
Bartlomiej Zolnierkiewicz9c027c62008-03-22 16:40:21 +01001160 goto obsolete_option;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161#endif
1162#ifdef CONFIG_BLK_DEV_HT6560B
1163 case -13: /* "ht6560b" */
1164 probe_ht6560b = 1;
Bartlomiej Zolnierkiewicz9c027c62008-03-22 16:40:21 +01001165 goto obsolete_option;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166#endif
1167#ifdef CONFIG_BLK_DEV_QD65XX
1168 case -12: /* "qd65xx" */
1169 probe_qd65xx = 1;
Bartlomiej Zolnierkiewicz9c027c62008-03-22 16:40:21 +01001170 goto obsolete_option;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171#endif
1172#ifdef CONFIG_BLK_DEV_4DRIVES
1173 case -11: /* "four" drives on one set of ports */
Bartlomiej Zolnierkiewiczffd4f6f2008-04-18 00:46:34 +02001174 probe_4drives = 1;
Bartlomiej Zolnierkiewiczb6209a92007-03-03 17:48:55 +01001175 goto obsolete_option;
Bartlomiej Zolnierkiewiczffd4f6f2008-04-18 00:46:34 +02001176#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 case -10: /* minus10 */
1178 case -9: /* minus9 */
1179 case -8: /* minus8 */
Bartlomiej Zolnierkiewicz1b516942007-10-16 22:29:57 +02001180 case -6:
Bartlomiej Zolnierkiewiczb6209a92007-03-03 17:48:55 +01001181 case -4:
1182 case -3:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 goto bad_option;
1184 case -7: /* ata66 */
1185#ifdef CONFIG_BLK_DEV_IDEPCI
Bartlomiej Zolnierkiewicz49521f92007-07-09 23:17:58 +02001186 /*
1187 * Use ATA_CBL_PATA40_SHORT so drive side
1188 * cable detection is also overriden.
1189 */
1190 hwif->cbl = ATA_CBL_PATA40_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 goto obsolete_option;
1192#else
1193 goto bad_hwif;
1194#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 case -5: /* "reset" */
1196 hwif->reset = 1;
1197 goto obsolete_option;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 case -2: /* "serialize" */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 hwif->mate = &ide_hwifs[hw^1];
1200 hwif->mate->mate = hwif;
1201 hwif->serialized = hwif->mate->serialized = 1;
1202 goto obsolete_option;
1203
Bartlomiej Zolnierkiewiczf01674e2008-04-18 00:46:35 +02001204 case -1:
Bartlomiej Zolnierkiewicz9a0e77f2008-04-18 00:46:24 +02001205 case 0:
1206 case 1:
1207 case 2:
1208 case 3:
1209 goto bad_option;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 default:
1211 printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
1212 return 1;
1213 }
1214 }
1215bad_option:
1216 printk(" -- BAD OPTION\n");
1217 return 1;
1218obsolete_option:
1219 printk(" -- OBSOLETE OPTION, WILL BE REMOVED SOON!\n");
1220 return 1;
1221bad_hwif:
1222 printk("-- NOT SUPPORTED ON ide%d", hw);
1223done:
1224 printk("\n");
1225 return 1;
1226}
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228EXPORT_SYMBOL(ide_lock);
1229
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001230static int ide_bus_match(struct device *dev, struct device_driver *drv)
1231{
1232 return 1;
1233}
1234
Kay Sievers263756e2005-12-12 18:03:44 +01001235static char *media_string(ide_drive_t *drive)
1236{
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";
Danny Kukawkaa7a832d2007-04-10 22:39:14 +02001246 case ide_optical:
1247 return "optical";
Kay Sievers263756e2005-12-12 18:03:44 +01001248 default:
1249 return "UNKNOWN";
1250 }
1251}
1252
1253static ssize_t media_show(struct device *dev, struct device_attribute *attr, char *buf)
1254{
1255 ide_drive_t *drive = to_ide_device(dev);
1256 return sprintf(buf, "%s\n", media_string(drive));
1257}
1258
1259static ssize_t drivename_show(struct device *dev, struct device_attribute *attr, char *buf)
1260{
1261 ide_drive_t *drive = to_ide_device(dev);
1262 return sprintf(buf, "%s\n", drive->name);
1263}
1264
1265static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
1266{
1267 ide_drive_t *drive = to_ide_device(dev);
1268 return sprintf(buf, "ide:m-%s\n", media_string(drive));
1269}
1270
Bartlomiej Zolnierkiewicze11b9032007-12-12 23:31:58 +01001271static ssize_t model_show(struct device *dev, struct device_attribute *attr,
1272 char *buf)
1273{
1274 ide_drive_t *drive = to_ide_device(dev);
1275 return sprintf(buf, "%s\n", drive->id->model);
1276}
1277
1278static ssize_t firmware_show(struct device *dev, struct device_attribute *attr,
1279 char *buf)
1280{
1281 ide_drive_t *drive = to_ide_device(dev);
1282 return sprintf(buf, "%s\n", drive->id->fw_rev);
1283}
1284
1285static ssize_t serial_show(struct device *dev, struct device_attribute *attr,
1286 char *buf)
1287{
1288 ide_drive_t *drive = to_ide_device(dev);
1289 return sprintf(buf, "%s\n", drive->id->serial_no);
1290}
1291
Kay Sievers263756e2005-12-12 18:03:44 +01001292static struct device_attribute ide_dev_attrs[] = {
1293 __ATTR_RO(media),
1294 __ATTR_RO(drivename),
1295 __ATTR_RO(modalias),
Bartlomiej Zolnierkiewicze11b9032007-12-12 23:31:58 +01001296 __ATTR_RO(model),
1297 __ATTR_RO(firmware),
1298 __ATTR(serial, 0400, serial_show, NULL),
Kay Sievers263756e2005-12-12 18:03:44 +01001299 __ATTR_NULL
1300};
1301
Kay Sievers7eff2e72007-08-14 15:15:12 +02001302static int ide_uevent(struct device *dev, struct kobj_uevent_env *env)
Kay Sievers263756e2005-12-12 18:03:44 +01001303{
1304 ide_drive_t *drive = to_ide_device(dev);
Kay Sievers263756e2005-12-12 18:03:44 +01001305
Kay Sievers7eff2e72007-08-14 15:15:12 +02001306 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));
Kay Sievers263756e2005-12-12 18:03:44 +01001309 return 0;
1310}
1311
Russell King4031bbe2006-01-06 11:41:00 +00001312static int generic_ide_probe(struct device *dev)
1313{
1314 ide_drive_t *drive = to_ide_device(dev);
1315 ide_driver_t *drv = to_ide_driver(dev->driver);
1316
1317 return drv->probe ? drv->probe(drive) : -ENODEV;
1318}
1319
1320static int generic_ide_remove(struct device *dev)
1321{
1322 ide_drive_t *drive = to_ide_device(dev);
1323 ide_driver_t *drv = to_ide_driver(dev->driver);
1324
1325 if (drv->remove)
1326 drv->remove(drive);
1327
1328 return 0;
1329}
1330
1331static void generic_ide_shutdown(struct device *dev)
1332{
1333 ide_drive_t *drive = to_ide_device(dev);
1334 ide_driver_t *drv = to_ide_driver(dev->driver);
1335
1336 if (dev->driver && drv->shutdown)
1337 drv->shutdown(drive);
1338}
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340struct bus_type ide_bus_type = {
1341 .name = "ide",
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001342 .match = ide_bus_match,
Kay Sievers263756e2005-12-12 18:03:44 +01001343 .uevent = ide_uevent,
Russell King4031bbe2006-01-06 11:41:00 +00001344 .probe = generic_ide_probe,
1345 .remove = generic_ide_remove,
1346 .shutdown = generic_ide_shutdown,
Kay Sievers263756e2005-12-12 18:03:44 +01001347 .dev_attrs = ide_dev_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 .suspend = generic_ide_suspend,
1349 .resume = generic_ide_resume,
1350};
1351
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001352EXPORT_SYMBOL_GPL(ide_bus_type);
1353
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +02001354static void ide_port_class_release(struct device *portdev)
1355{
1356 ide_hwif_t *hwif = dev_get_drvdata(portdev);
1357
1358 put_device(&hwif->gendev);
1359}
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361/*
1362 * This is gets invoked once during initialization, to set *everything* up
1363 */
1364static int __init ide_init(void)
1365{
Randy Dunlap349ae232006-10-03 01:14:23 -07001366 int ret;
1367
Bartlomiej Zolnierkiewiczeba8ff92008-02-11 00:32:13 +01001368 printk(KERN_INFO "Uniform Multi-Platform E-IDE driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 system_bus_speed = ide_system_bus_speed();
1370
Bartlomiej Zolnierkiewicz537f06c2008-02-01 23:09:35 +01001371 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");
1374
Randy Dunlap349ae232006-10-03 01:14:23 -07001375 ret = bus_register(&ide_bus_type);
1376 if (ret < 0) {
1377 printk(KERN_WARNING "IDE: bus_register error: %d\n", ret);
1378 return ret;
1379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +02001381 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;
1385 }
1386 ide_port_class->dev_release = ide_port_class_release;
1387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 init_ide_data();
1389
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +02001390 proc_ide_create();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return 0;
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +02001393
1394out_port_class:
1395 bus_unregister(&ide_bus_type);
1396
1397 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
1400#ifdef MODULE
1401static char *options = NULL;
1402module_param(options, charp, 0);
1403MODULE_LICENSE("GPL");
1404
1405static void __init parse_options (char *line)
1406{
1407 char *next = line;
1408
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);
1416 }
1417}
1418
Sam Ravnborgfbd8ad32006-03-25 03:07:11 -08001419int __init init_module (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
1421 parse_options(options);
1422 return ide_init();
1423}
1424
Al Viroeb797222007-02-01 13:52:38 +00001425void __exit cleanup_module (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
1427 int index;
1428
1429 for (index = 0; index < MAX_HWIFS; ++index)
Bartlomiej Zolnierkiewicz93de00f2008-04-18 00:46:24 +02001430 ide_unregister(index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 proc_ide_destroy();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Bartlomiej Zolnierkiewiczf74c9142008-04-18 00:46:23 +02001434 class_destroy(ide_port_class);
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 bus_unregister(&ide_bus_type);
1437}
1438
1439#else /* !MODULE */
1440
1441__setup("", ide_setup);
1442
1443module_init(ide_init);
1444
1445#endif /* MODULE */