blob: 98bd45e8c1759d58d83bbe8b79e539bc88ed91d8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/ide/ide.c Version 7.00beta2 Mar 05 2003
3 *
4 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
5 */
6
7/*
8 * Mostly written by Mark Lord <mlord@pobox.com>
9 * and Gadi Oxman <gadio@netvision.net.il>
10 * and Andre Hedrick <andre@linux-ide.org>
11 *
12 * See linux/MAINTAINERS for address of current maintainer.
13 *
14 * This is the multiple IDE interface driver, as evolved from hd.c.
15 * It supports up to MAX_HWIFS IDE interfaces, on one or more IRQs
16 * (usually 14 & 15).
17 * There can be up to two drives per interface, as per the ATA-2 spec.
18 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * ...
20 *
21 * From hd.c:
22 * |
23 * | It traverses the request-list, using interrupts to jump between functions.
24 * | As nearly all functions can be called within interrupts, we may not sleep.
25 * | Special care is recommended. Have Fun!
26 * |
27 * | modified by Drew Eckhardt to check nr of hd's from the CMOS.
28 * |
29 * | Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
30 * | in the early extended-partition checks and added DM partitions.
31 * |
32 * | Early work on error handling by Mika Liljeberg (liljeber@cs.Helsinki.FI).
33 * |
34 * | IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
35 * | and general streamlining by Mark Lord (mlord@pobox.com).
36 *
37 * October, 1994 -- Complete line-by-line overhaul for linux 1.1.x, by:
38 *
39 * Mark Lord (mlord@pobox.com) (IDE Perf.Pkg)
40 * Delman Lee (delman@ieee.org) ("Mr. atdisk2")
41 * Scott Snyder (snyder@fnald0.fnal.gov) (ATAPI IDE cd-rom)
42 *
43 * This was a rewrite of just about everything from hd.c, though some original
44 * code is still sprinkled about. Think of it as a major evolution, with
45 * inspiration from lots of linux users, esp. hamish@zot.apana.org.au
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 */
47
48#define REVISION "Revision: 7.00alpha2"
49#define VERSION "Id: ide.c 7.00a2 20020906"
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define _IDE_C /* Tell ide.h it's really us */
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/module.h>
54#include <linux/types.h>
55#include <linux/string.h>
56#include <linux/kernel.h>
57#include <linux/timer.h>
58#include <linux/mm.h>
59#include <linux/interrupt.h>
60#include <linux/major.h>
61#include <linux/errno.h>
62#include <linux/genhd.h>
63#include <linux/blkpg.h>
64#include <linux/slab.h>
65#include <linux/init.h>
66#include <linux/pci.h>
67#include <linux/delay.h>
68#include <linux/ide.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/completion.h>
70#include <linux/reboot.h>
71#include <linux/cdrom.h>
72#include <linux/seq_file.h>
73#include <linux/device.h>
74#include <linux/bitops.h>
75
76#include <asm/byteorder.h>
77#include <asm/irq.h>
78#include <asm/uaccess.h>
79#include <asm/io.h>
80
81
82/* default maximum number of failures */
83#define IDE_DEFAULT_MAX_FAILURES 1
84
85static const u8 ide_hwif_to_major[] = { IDE0_MAJOR, IDE1_MAJOR,
86 IDE2_MAJOR, IDE3_MAJOR,
87 IDE4_MAJOR, IDE5_MAJOR,
88 IDE6_MAJOR, IDE7_MAJOR,
89 IDE8_MAJOR, IDE9_MAJOR };
90
91static int idebus_parameter; /* holds the "idebus=" parameter */
92static int system_bus_speed; /* holds what we think is VESA/PCI bus speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Matthias Kaehlckeef298882007-07-09 23:17:55 +020094DEFINE_MUTEX(ide_cfg_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 __cacheline_aligned_in_smp DEFINE_SPINLOCK(ide_lock);
96
Bartlomiej Zolnierkiewicz6d208b32007-05-10 00:01:11 +020097#ifdef CONFIG_IDEPCI_PCIBUS_ORDER
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static int ide_scan_direction; /* THIS was formerly 2.2.x pci=reverse */
99#endif
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101int noautodma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100103#ifdef CONFIG_BLK_DEV_IDEACPI
104int ide_noacpi = 0;
105int ide_noacpitfs = 1;
106int ide_noacpionboot = 1;
107#endif
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/*
110 * This is declared extern in ide.h, for access by other IDE modules:
111 */
112ide_hwif_t ide_hwifs[MAX_HWIFS]; /* master data repository */
113
114EXPORT_SYMBOL(ide_hwifs);
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116/*
117 * Do not even *think* about calling this!
118 */
119static void init_hwif_data(ide_hwif_t *hwif, unsigned int index)
120{
121 unsigned int unit;
122
123 /* bulk initialize hwif & drive info with zeros */
124 memset(hwif, 0, sizeof(ide_hwif_t));
125
126 /* fill in any non-zero initial values */
127 hwif->index = index;
128 hwif->major = ide_hwif_to_major[index];
129
130 hwif->name[0] = 'i';
131 hwif->name[1] = 'd';
132 hwif->name[2] = 'e';
133 hwif->name[3] = '0' + index;
134
135 hwif->bus_state = BUSSTATE_ON;
136
Aleksey Makarovf36d4022006-01-09 15:59:27 -0800137 init_completion(&hwif->gendev_rel_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 default_hwif_iops(hwif);
140 default_hwif_transport(hwif);
141 for (unit = 0; unit < MAX_DRIVES; ++unit) {
142 ide_drive_t *drive = &hwif->drives[unit];
143
144 drive->media = ide_disk;
145 drive->select.all = (unit<<4)|0xa0;
146 drive->hwif = hwif;
147 drive->ctl = 0x08;
148 drive->ready_stat = READY_STAT;
149 drive->bad_wstat = BAD_W_STAT;
150 drive->special.b.recalibrate = 1;
151 drive->special.b.set_geometry = 1;
152 drive->name[0] = 'h';
153 drive->name[1] = 'd';
154 drive->name[2] = 'a' + (index * MAX_DRIVES) + unit;
155 drive->max_failures = IDE_DEFAULT_MAX_FAILURES;
156 drive->using_dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 drive->vdma = 0;
158 INIT_LIST_HEAD(&drive->list);
Aleksey Makarovf36d4022006-01-09 15:59:27 -0800159 init_completion(&drive->gendev_rel_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
161}
162
163static void init_hwif_default(ide_hwif_t *hwif, unsigned int index)
164{
165 hw_regs_t hw;
166
167 memset(&hw, 0, sizeof(hw_regs_t));
168
169 ide_init_hwif_ports(&hw, ide_default_io_base(index), 0, &hwif->irq);
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 memcpy(hwif->io_ports, hw.io_ports, sizeof(hw.io_ports));
172
173 hwif->noprobe = !hwif->io_ports[IDE_DATA_OFFSET];
174#ifdef CONFIG_BLK_DEV_HD
175 if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA)
176 hwif->noprobe = 1; /* may be overridden by ide_setup() */
177#endif
178}
179
180extern void ide_arm_init(void);
181
182/*
183 * init_ide_data() sets reasonable default values into all fields
184 * of all instances of the hwifs and drives, but only on the first call.
185 * Subsequent calls have no effect (they don't wipe out anything).
186 *
187 * This routine is normally called at driver initialization time,
188 * but may also be called MUCH earlier during kernel "command-line"
189 * parameter processing. As such, we cannot depend on any other parts
190 * of the kernel (such as memory allocation) to be functioning yet.
191 *
192 * This is too bad, as otherwise we could dynamically allocate the
193 * ide_drive_t structs as needed, rather than always consuming memory
194 * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
195 *
196 * FIXME: We should stuff the setup data into __init and copy the
197 * relevant hwifs/allocate them properly during boot.
198 */
199#define MAGIC_COOKIE 0x12345678
200static void __init init_ide_data (void)
201{
202 ide_hwif_t *hwif;
203 unsigned int index;
204 static unsigned long magic_cookie = MAGIC_COOKIE;
205
206 if (magic_cookie != MAGIC_COOKIE)
207 return; /* already initialized */
208 magic_cookie = 0;
209
210 /* Initialise all interface structures */
211 for (index = 0; index < MAX_HWIFS; ++index) {
212 hwif = &ide_hwifs[index];
213 init_hwif_data(hwif, index);
214 init_hwif_default(hwif, index);
215#if !defined(CONFIG_PPC32) || !defined(CONFIG_PCI)
Bartlomiej Zolnierkiewicz9239b332007-10-20 00:32:33 +0200216 hwif->irq =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 ide_init_default_irq(hwif->io_ports[IDE_DATA_OFFSET]);
218#endif
219 }
220#ifdef CONFIG_IDE_ARM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 ide_arm_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222#endif
223}
224
225/**
226 * ide_system_bus_speed - guess bus speed
227 *
228 * ide_system_bus_speed() returns what we think is the system VESA/PCI
229 * bus speed (in MHz). This is used for calculating interface PIO timings.
230 * The default is 40 for known PCI systems, 50 otherwise.
231 * The "idebus=xx" parameter can be used to override this value.
232 * The actual value to be used is computed/displayed the first time
233 * through. Drivers should only use this as a last resort.
234 *
235 * Returns a guessed speed in MHz.
236 */
237
238static int ide_system_bus_speed(void)
239{
240#ifdef CONFIG_PCI
241 static struct pci_device_id pci_default[] = {
242 { PCI_DEVICE(PCI_ANY_ID, PCI_ANY_ID) },
243 { }
244 };
245#else
246#define pci_default 0
247#endif /* CONFIG_PCI */
248
249 if (!system_bus_speed) {
250 if (idebus_parameter) {
251 /* user supplied value */
252 system_bus_speed = idebus_parameter;
253 } else if (pci_dev_present(pci_default)) {
254 /* safe default value for PCI */
255 system_bus_speed = 33;
256 } else {
257 /* safe default value for VESA and PCI */
258 system_bus_speed = 50;
259 }
260 printk(KERN_INFO "ide: Assuming %dMHz system bus speed "
261 "for PIO modes%s\n", system_bus_speed,
262 idebus_parameter ? "" : "; override with idebus=xx");
263 }
264 return system_bus_speed;
265}
266
Bartlomiej Zolnierkiewiczbaa8f3e2007-10-20 00:32:31 +0200267ide_hwif_t * ide_find_port(unsigned long base)
268{
269 ide_hwif_t *hwif;
270 int i;
271
272 for (i = 0; i < MAX_HWIFS; i++) {
273 hwif = &ide_hwifs[i];
274 if (hwif->io_ports[IDE_DATA_OFFSET] == base)
275 goto found;
276 }
277
278 for (i = 0; i < MAX_HWIFS; i++) {
279 hwif = &ide_hwifs[i];
280 if (hwif->io_ports[IDE_DATA_OFFSET] == 0)
281 goto found;
282 }
283
284 hwif = NULL;
285found:
286 return hwif;
287}
288
289EXPORT_SYMBOL_GPL(ide_find_port);
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291static struct resource* hwif_request_region(ide_hwif_t *hwif,
292 unsigned long addr, int num)
293{
294 struct resource *res = request_region(addr, num, hwif->name);
295
296 if (!res)
297 printk(KERN_ERR "%s: I/O resource 0x%lX-0x%lX not free.\n",
298 hwif->name, addr, addr+num-1);
299 return res;
300}
301
302/**
303 * ide_hwif_request_regions - request resources for IDE
304 * @hwif: interface to use
305 *
306 * Requests all the needed resources for an interface.
307 * Right now core IDE code does this work which is deeply wrong.
308 * MMIO leaves it to the controller driver,
309 * PIO will migrate this way over time.
310 */
311
312int ide_hwif_request_regions(ide_hwif_t *hwif)
313{
314 unsigned long addr;
315 unsigned int i;
316
Bartlomiej Zolnierkiewicz2ad1e552007-02-17 02:40:25 +0100317 if (hwif->mmio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 addr = hwif->io_ports[IDE_CONTROL_OFFSET];
320 if (addr && !hwif_request_region(hwif, addr, 1))
321 goto control_region_busy;
322 hwif->straight8 = 0;
323 addr = hwif->io_ports[IDE_DATA_OFFSET];
324 if ((addr | 7) == hwif->io_ports[IDE_STATUS_OFFSET]) {
325 if (!hwif_request_region(hwif, addr, 8))
326 goto data_region_busy;
327 hwif->straight8 = 1;
328 return 0;
329 }
330 for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++) {
331 addr = hwif->io_ports[i];
332 if (!hwif_request_region(hwif, addr, 1)) {
333 while (--i)
334 release_region(addr, 1);
335 goto data_region_busy;
336 }
337 }
338 return 0;
339
340data_region_busy:
341 addr = hwif->io_ports[IDE_CONTROL_OFFSET];
342 if (addr)
343 release_region(addr, 1);
344control_region_busy:
345 /* If any errors are return, we drop the hwif interface. */
346 return -EBUSY;
347}
348
349/**
350 * ide_hwif_release_regions - free IDE resources
351 *
352 * Note that we only release the standard ports,
353 * and do not even try to handle any extra ports
354 * allocated for weird IDE interface chipsets.
355 *
356 * Note also that we don't yet handle mmio resources here. More
357 * importantly our caller should be doing this so we need to
358 * restructure this as a helper function for drivers.
359 */
360
361void ide_hwif_release_regions(ide_hwif_t *hwif)
362{
363 u32 i = 0;
364
Bartlomiej Zolnierkiewicz2ad1e552007-02-17 02:40:25 +0100365 if (hwif->mmio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return;
367 if (hwif->io_ports[IDE_CONTROL_OFFSET])
368 release_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
369 if (hwif->straight8) {
370 release_region(hwif->io_ports[IDE_DATA_OFFSET], 8);
371 return;
372 }
373 for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++)
374 if (hwif->io_ports[i])
375 release_region(hwif->io_ports[i], 1);
376}
377
378/**
379 * ide_hwif_restore - restore hwif to template
380 * @hwif: hwif to update
381 * @tmp_hwif: template
382 *
Sergei Shtylylov020e3222006-10-03 01:14:13 -0700383 * Restore hwif to a previous state by copying most settings
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 * from the template.
385 */
386
387static void ide_hwif_restore(ide_hwif_t *hwif, ide_hwif_t *tmp_hwif)
388{
389 hwif->hwgroup = tmp_hwif->hwgroup;
390
391 hwif->gendev.parent = tmp_hwif->gendev.parent;
392
393 hwif->proc = tmp_hwif->proc;
394
395 hwif->major = tmp_hwif->major;
396 hwif->straight8 = tmp_hwif->straight8;
397 hwif->bus_state = tmp_hwif->bus_state;
398
Bartlomiej Zolnierkiewicz6a824c92007-07-20 01:11:58 +0200399 hwif->host_flags = tmp_hwif->host_flags;
400
Bartlomiej Zolnierkiewicz4099d142007-07-20 01:11:59 +0200401 hwif->pio_mask = tmp_hwif->pio_mask;
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 hwif->ultra_mask = tmp_hwif->ultra_mask;
404 hwif->mwdma_mask = tmp_hwif->mwdma_mask;
405 hwif->swdma_mask = tmp_hwif->swdma_mask;
406
Bartlomiej Zolnierkiewicz49521f92007-07-09 23:17:58 +0200407 hwif->cbl = tmp_hwif->cbl;
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 hwif->chipset = tmp_hwif->chipset;
410 hwif->hold = tmp_hwif->hold;
411
412#ifdef CONFIG_BLK_DEV_IDEPCI
413 hwif->pci_dev = tmp_hwif->pci_dev;
414 hwif->cds = tmp_hwif->cds;
415#endif
416
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200417 hwif->set_pio_mode = tmp_hwif->set_pio_mode;
Bartlomiej Zolnierkiewicz88b2b322007-10-13 17:47:51 +0200418 hwif->set_dma_mode = tmp_hwif->set_dma_mode;
Sergei Shtylyovb4e44362007-10-11 23:53:58 +0200419 hwif->mdma_filter = tmp_hwif->mdma_filter;
Bartlomiej Zolnierkiewicz2d5eaa62007-05-10 00:01:08 +0200420 hwif->udma_filter = tmp_hwif->udma_filter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 hwif->selectproc = tmp_hwif->selectproc;
422 hwif->reset_poll = tmp_hwif->reset_poll;
423 hwif->pre_reset = tmp_hwif->pre_reset;
424 hwif->resetproc = tmp_hwif->resetproc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 hwif->maskproc = tmp_hwif->maskproc;
426 hwif->quirkproc = tmp_hwif->quirkproc;
427 hwif->busproc = tmp_hwif->busproc;
428
429 hwif->ata_input_data = tmp_hwif->ata_input_data;
430 hwif->ata_output_data = tmp_hwif->ata_output_data;
431 hwif->atapi_input_bytes = tmp_hwif->atapi_input_bytes;
432 hwif->atapi_output_bytes = tmp_hwif->atapi_output_bytes;
433
Bartlomiej Zolnierkiewicz15ce9262008-01-26 20:13:03 +0100434 hwif->dma_host_set = tmp_hwif->dma_host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 hwif->dma_setup = tmp_hwif->dma_setup;
436 hwif->dma_exec_cmd = tmp_hwif->dma_exec_cmd;
437 hwif->dma_start = tmp_hwif->dma_start;
438 hwif->ide_dma_end = tmp_hwif->ide_dma_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 hwif->ide_dma_test_irq = tmp_hwif->ide_dma_test_irq;
Albert Leef0dd8712007-02-17 02:40:21 +0100440 hwif->ide_dma_clear_irq = tmp_hwif->ide_dma_clear_irq;
Sergei Shtylyov841d2a92007-07-09 23:17:54 +0200441 hwif->dma_lost_irq = tmp_hwif->dma_lost_irq;
Sergei Shtylyovc283f5d2007-07-09 23:17:54 +0200442 hwif->dma_timeout = tmp_hwif->dma_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 hwif->OUTB = tmp_hwif->OUTB;
445 hwif->OUTBSYNC = tmp_hwif->OUTBSYNC;
446 hwif->OUTW = tmp_hwif->OUTW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 hwif->OUTSW = tmp_hwif->OUTSW;
448 hwif->OUTSL = tmp_hwif->OUTSL;
449
450 hwif->INB = tmp_hwif->INB;
451 hwif->INW = tmp_hwif->INW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 hwif->INSW = tmp_hwif->INSW;
453 hwif->INSL = tmp_hwif->INSL;
454
455 hwif->sg_max_nents = tmp_hwif->sg_max_nents;
456
457 hwif->mmio = tmp_hwif->mmio;
458 hwif->rqsize = tmp_hwif->rqsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460#ifndef CONFIG_BLK_DEV_IDECS
461 hwif->irq = tmp_hwif->irq;
462#endif
463
464 hwif->dma_base = tmp_hwif->dma_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 hwif->dma_command = tmp_hwif->dma_command;
466 hwif->dma_vendor1 = tmp_hwif->dma_vendor1;
467 hwif->dma_status = tmp_hwif->dma_status;
468 hwif->dma_vendor3 = tmp_hwif->dma_vendor3;
469 hwif->dma_prdtable = tmp_hwif->dma_prdtable;
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 hwif->config_data = tmp_hwif->config_data;
472 hwif->select_data = tmp_hwif->select_data;
Sergei Shtylylov020e3222006-10-03 01:14:13 -0700473 hwif->extra_base = tmp_hwif->extra_base;
474 hwif->extra_ports = tmp_hwif->extra_ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 hwif->hwif_data = tmp_hwif->hwif_data;
477}
478
479/**
Sergei Shtylylov020e3222006-10-03 01:14:13 -0700480 * ide_unregister - free an IDE interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 * @index: index of interface (will change soon to a pointer)
482 *
483 * Perform the final unregister of an IDE interface. At the moment
484 * we don't refcount interfaces so this will also get split up.
485 *
486 * Locking:
487 * The caller must not hold the IDE locks
488 * The drive present/vanishing is not yet properly locked
489 * Take care with the callbacks. These have been split to avoid
490 * deadlocking the IDE layer. The shutdown callback is called
491 * before we take the lock and free resources. It is up to the
492 * caller to be sure there is no pending I/O here, and that
Sergei Shtylylov020e3222006-10-03 01:14:13 -0700493 * the interface will not be reopened (present/vanishing locking
494 * isn't yet done BTW). After we commit to the final kill we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 * call the cleanup callback with the ide locks held.
496 *
497 * Unregister restores the hwif structures to the default state.
498 * This is raving bonkers.
499 */
500
501void ide_unregister(unsigned int index)
502{
503 ide_drive_t *drive;
504 ide_hwif_t *hwif, *g;
Matthias Kaehlckeef298882007-07-09 23:17:55 +0200505 static ide_hwif_t tmp_hwif; /* protected by ide_cfg_mtx */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 ide_hwgroup_t *hwgroup;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200507 int irq_count = 0, unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 BUG_ON(index >= MAX_HWIFS);
510
511 BUG_ON(in_interrupt());
512 BUG_ON(irqs_disabled());
Matthias Kaehlckeef298882007-07-09 23:17:55 +0200513 mutex_lock(&ide_cfg_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 spin_lock_irq(&ide_lock);
515 hwif = &ide_hwifs[index];
516 if (!hwif->present)
517 goto abort;
518 for (unit = 0; unit < MAX_DRIVES; ++unit) {
519 drive = &hwif->drives[unit];
Greg Kroah-Hartman94f6c592005-06-20 21:15:16 -0700520 if (!drive->present)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 continue;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200522 spin_unlock_irq(&ide_lock);
523 device_unregister(&drive->gendev);
Aleksey Makarovf36d4022006-01-09 15:59:27 -0800524 wait_for_completion(&drive->gendev_rel_comp);
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200525 spin_lock_irq(&ide_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
527 hwif->present = 0;
528
529 spin_unlock_irq(&ide_lock);
530
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +0200531 ide_proc_unregister_port(hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 hwgroup = hwif->hwgroup;
534 /*
535 * free the irq if we were the only hwif using it
536 */
537 g = hwgroup->hwif;
538 do {
539 if (g->irq == hwif->irq)
540 ++irq_count;
541 g = g->next;
542 } while (g != hwgroup->hwif);
543 if (irq_count == 1)
544 free_irq(hwif->irq, hwgroup);
545
546 spin_lock_irq(&ide_lock);
547 /*
548 * Note that we only release the standard ports,
549 * and do not even try to handle any extra ports
550 * allocated for weird IDE interface chipsets.
551 */
552 ide_hwif_release_regions(hwif);
553
554 /*
555 * Remove us from the hwgroup, and free
556 * the hwgroup if we were the only member
557 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (hwif->next == hwif) {
559 BUG_ON(hwgroup->hwif != hwif);
560 kfree(hwgroup);
561 } else {
562 /* There is another interface in hwgroup.
563 * Unlink us, and set hwgroup->drive and ->hwif to
564 * something sane.
565 */
566 g = hwgroup->hwif;
567 while (g->next != hwif)
568 g = g->next;
569 g->next = hwif->next;
570 if (hwgroup->hwif == hwif) {
571 /* Chose a random hwif for hwgroup->hwif.
572 * It's guaranteed that there are no drives
573 * left in the hwgroup.
574 */
575 BUG_ON(hwgroup->drive != NULL);
576 hwgroup->hwif = g;
577 }
578 BUG_ON(hwgroup->hwif == hwif);
579 }
580
581 /* More messed up locking ... */
582 spin_unlock_irq(&ide_lock);
583 device_unregister(&hwif->gendev);
Aleksey Makarovf36d4022006-01-09 15:59:27 -0800584 wait_for_completion(&hwif->gendev_rel_comp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 /*
587 * Remove us from the kernel's knowledge
588 */
589 blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
590 kfree(hwif->sg_table);
591 unregister_blkdev(hwif->major, hwif->name);
592 spin_lock_irq(&ide_lock);
593
594 if (hwif->dma_base) {
595 (void) ide_release_dma(hwif);
596
597 hwif->dma_base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 hwif->dma_command = 0;
599 hwif->dma_vendor1 = 0;
600 hwif->dma_status = 0;
601 hwif->dma_vendor3 = 0;
602 hwif->dma_prdtable = 0;
Sergei Shtylylov020e3222006-10-03 01:14:13 -0700603
604 hwif->extra_base = 0;
605 hwif->extra_ports = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607
608 /* copy original settings */
609 tmp_hwif = *hwif;
610
611 /* restore hwif data to pristine status */
612 init_hwif_data(hwif, index);
613 init_hwif_default(hwif, index);
614
615 ide_hwif_restore(hwif, &tmp_hwif);
616
617abort:
618 spin_unlock_irq(&ide_lock);
Matthias Kaehlckeef298882007-07-09 23:17:55 +0200619 mutex_unlock(&ide_cfg_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
622EXPORT_SYMBOL(ide_unregister);
623
624
625/**
626 * ide_setup_ports - set up IDE interface ports
627 * @hw: register descriptions
628 * @base: base register
629 * @offsets: table of register offsets
630 * @ctrl: control register
631 * @ack_irq: IRQ ack
632 * @irq: interrupt lie
633 *
634 * Setup hw_regs_t structure described by parameters. You
635 * may set up the hw structure yourself OR use this routine to
636 * do it for you. This is basically a helper
637 *
638 */
639
640void ide_setup_ports ( hw_regs_t *hw,
641 unsigned long base, int *offsets,
642 unsigned long ctrl, unsigned long intr,
643 ide_ack_intr_t *ack_intr,
644/*
645 * ide_io_ops_t *iops,
646 */
647 int irq)
648{
649 int i;
650
Roman Zippel2c3e0262006-06-23 02:04:51 -0700651 memset(hw, 0, sizeof(hw_regs_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 for (i = 0; i < IDE_NR_PORTS; i++) {
653 if (offsets[i] == -1) {
654 switch(i) {
655 case IDE_CONTROL_OFFSET:
656 hw->io_ports[i] = ctrl;
657 break;
658#if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
659 case IDE_IRQ_OFFSET:
660 hw->io_ports[i] = intr;
661 break;
662#endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
663 default:
664 hw->io_ports[i] = 0;
665 break;
666 }
667 } else {
668 hw->io_ports[i] = base + offsets[i];
669 }
670 }
671 hw->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 hw->ack_intr = ack_intr;
673/*
674 * hw->iops = iops;
675 */
676}
677
Bartlomiej Zolnierkiewicz57c802e2008-01-26 20:13:05 +0100678void ide_init_port_hw(ide_hwif_t *hwif, hw_regs_t *hw)
679{
680 memcpy(hwif->io_ports, hw->io_ports, sizeof(hwif->io_ports));
681 hwif->irq = hw->irq;
682 hwif->noprobe = 0;
683 hwif->chipset = hw->chipset;
684 hwif->gendev.parent = hw->dev;
685 hwif->ack_intr = hw->ack_intr;
686}
687EXPORT_SYMBOL_GPL(ide_init_port_hw);
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689/**
Bartlomiej Zolnierkiewiczfd9bb532007-10-20 00:32:31 +0200690 * ide_register_hw - register IDE interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 * @hw: hardware registers
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100692 * @quirkproc: quirkproc function
Bartlomiej Zolnierkiewicz869c56e2007-05-10 00:01:10 +0200693 * @initializing: set while initializing built-in drivers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 * @hwifp: pointer to returned hwif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 *
696 * Register an IDE interface, specifying exactly the registers etc.
697 * Set init=1 iff calling before probes have taken place.
698 *
699 * Returns -1 on error.
700 */
701
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100702int ide_register_hw(hw_regs_t *hw, void (*quirkproc)(ide_drive_t *),
Bartlomiej Zolnierkiewiczfd9bb532007-10-20 00:32:31 +0200703 int initializing, ide_hwif_t **hwifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 int index, retry = 1;
706 ide_hwif_t *hwif;
707
Bartlomiej Zolnierkiewicz795d74b2008-01-26 20:13:03 +0100708 if (initializing) {
709 hwif = ide_find_port(hw->io_ports[IDE_DATA_OFFSET]);
710 if (hwif) {
711 index = hwif->index;
712 goto found;
713 }
714 return -1;
715 }
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 do {
718 for (index = 0; index < MAX_HWIFS; ++index) {
719 hwif = &ide_hwifs[index];
Bartlomiej Zolnierkiewicz8f173b52007-10-20 00:32:32 +0200720 if (hwif->io_ports[IDE_DATA_OFFSET] == hw->io_ports[IDE_DATA_OFFSET])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 goto found;
722 }
723 for (index = 0; index < MAX_HWIFS; ++index) {
724 hwif = &ide_hwifs[index];
725 if (hwif->hold)
726 continue;
Bartlomiej Zolnierkiewicz795d74b2008-01-26 20:13:03 +0100727 if (!hwif->present && hwif->mate == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 goto found;
729 }
730 for (index = 0; index < MAX_HWIFS; index++)
731 ide_unregister(index);
732 } while (retry--);
733 return -1;
734found:
735 if (hwif->present)
736 ide_unregister(index);
737 else if (!hwif->hold) {
738 init_hwif_data(hwif, index);
739 init_hwif_default(hwif, index);
740 }
741 if (hwif->present)
742 return -1;
Bartlomiej Zolnierkiewicz57c802e2008-01-26 20:13:05 +0100743
744 ide_init_port_hw(hwif, hw);
Bartlomiej Zolnierkiewiczf01393e2008-01-26 20:13:03 +0100745 hwif->quirkproc = quirkproc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200747 if (initializing == 0) {
748 u8 idx[4] = { index, 0xff, 0xff, 0xff };
749
750 ide_device_add(idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
752
753 if (hwifp)
754 *hwifp = hwif;
755
756 return (initializing || hwif->present) ? index : -1;
757}
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759EXPORT_SYMBOL(ide_register_hw);
760
761/*
762 * Locks for IDE setting functionality
763 */
764
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200765DEFINE_MUTEX(ide_setting_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Matthias Kaehlckef9383c42007-07-09 23:17:56 +0200767EXPORT_SYMBOL_GPL(ide_setting_mtx);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 * ide_spin_wait_hwgroup - wait for group
771 * @drive: drive in the group
772 *
773 * Wait for an IDE device group to go non busy and then return
774 * holding the ide_lock which guards the hwgroup->busy status
775 * and right to use it.
776 */
777
778int ide_spin_wait_hwgroup (ide_drive_t *drive)
779{
780 ide_hwgroup_t *hwgroup = HWGROUP(drive);
781 unsigned long timeout = jiffies + (3 * HZ);
782
783 spin_lock_irq(&ide_lock);
784
785 while (hwgroup->busy) {
786 unsigned long lflags;
787 spin_unlock_irq(&ide_lock);
788 local_irq_set(lflags);
789 if (time_after(jiffies, timeout)) {
790 local_irq_restore(lflags);
791 printk(KERN_ERR "%s: channel busy\n", drive->name);
792 return -EBUSY;
793 }
794 local_irq_restore(lflags);
795 spin_lock_irq(&ide_lock);
796 }
797 return 0;
798}
799
800EXPORT_SYMBOL(ide_spin_wait_hwgroup);
801
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200802int set_io_32bit(ide_drive_t *drive, int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200804 if (drive->no_io_32bit)
805 return -EPERM;
806
807 if (arg < 0 || arg > 1 + (SUPPORT_VLB_SYNC << 1))
808 return -EINVAL;
809
Bartlomiej Zolnierkiewicz644a9d72007-12-12 23:32:00 +0100810 if (ide_spin_wait_hwgroup(drive))
811 return -EBUSY;
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 drive->io_32bit = arg;
814#ifdef CONFIG_BLK_DEV_DTC2278
815 if (HWIF(drive)->chipset == ide_dtc2278)
816 HWIF(drive)->drives[!drive->select.b.unit].io_32bit = arg;
817#endif /* CONFIG_BLK_DEV_DTC2278 */
Bartlomiej Zolnierkiewicz644a9d72007-12-12 23:32:00 +0100818
819 spin_unlock_irq(&ide_lock);
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return 0;
822}
823
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200824static int set_ksettings(ide_drive_t *drive, int arg)
825{
826 if (arg < 0 || arg > 1)
827 return -EINVAL;
828
829 if (ide_spin_wait_hwgroup(drive))
830 return -EBUSY;
831 drive->keep_settings = arg;
832 spin_unlock_irq(&ide_lock);
833
834 return 0;
835}
836
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200837int set_using_dma(ide_drive_t *drive, int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
839#ifdef CONFIG_BLK_DEV_IDEDMA
Bartlomiej Zolnierkiewicz87996202007-03-26 23:03:19 +0200840 ide_hwif_t *hwif = drive->hwif;
841 int err = -EPERM;
842
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200843 if (arg < 0 || arg > 1)
844 return -EINVAL;
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (!drive->id || !(drive->id->capability & 1))
Bartlomiej Zolnierkiewicz87996202007-03-26 23:03:19 +0200847 goto out;
848
Bartlomiej Zolnierkiewicz15ce9262008-01-26 20:13:03 +0100849 if (hwif->dma_host_set == NULL)
Bartlomiej Zolnierkiewicz87996202007-03-26 23:03:19 +0200850 goto out;
851
852 err = -EBUSY;
853 if (ide_spin_wait_hwgroup(drive))
854 goto out;
855 /*
856 * set ->busy flag, unlock and let it ride
857 */
858 hwif->hwgroup->busy = 1;
859 spin_unlock_irq(&ide_lock);
860
861 err = 0;
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (arg) {
Bartlomiej Zolnierkiewicz23b1bd42008-01-25 22:17:19 +0100864 if (ide_set_dma(drive))
Bartlomiej Zolnierkiewicz87996202007-03-26 23:03:19 +0200865 err = -EIO;
Bartlomiej Zolnierkiewicz7469aaf2007-02-17 02:40:26 +0100866 } else
867 ide_dma_off(drive);
Bartlomiej Zolnierkiewicz87996202007-03-26 23:03:19 +0200868
869 /*
870 * lock, clear ->busy flag and unlock before leaving
871 */
872 spin_lock_irq(&ide_lock);
873 hwif->hwgroup->busy = 0;
874 spin_unlock_irq(&ide_lock);
875out:
876 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877#else
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200878 if (arg < 0 || arg > 1)
879 return -EINVAL;
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return -EPERM;
882#endif
883}
884
Bartlomiej Zolnierkiewicz7662d042007-05-10 00:01:10 +0200885int set_pio_mode(ide_drive_t *drive, int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
887 struct request rq;
888
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200889 if (arg < 0 || arg > 255)
890 return -EINVAL;
891
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200892 if (drive->hwif->set_pio_mode == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return -ENOSYS;
Bartlomiej Zolnierkiewicz26bcb872007-10-11 23:54:00 +0200894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (drive->special.b.set_tune)
896 return -EBUSY;
897 ide_init_drive_cmd(&rq);
898 drive->tune_req = (u8) arg;
899 drive->special.b.set_tune = 1;
900 (void) ide_do_drive_cmd(drive, &rq, ide_wait);
901 return 0;
902}
903
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +0200904static int set_unmaskirq(ide_drive_t *drive, int arg)
905{
906 if (drive->no_unmask)
907 return -EPERM;
908
909 if (arg < 0 || arg > 1)
910 return -EINVAL;
911
912 if (ide_spin_wait_hwgroup(drive))
913 return -EBUSY;
914 drive->unmask = arg;
915 spin_unlock_irq(&ide_lock);
916
917 return 0;
918}
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920/**
921 * system_bus_clock - clock guess
922 *
923 * External version of the bus clock guess used by very old IDE drivers
924 * for things like VLB timings. Should not be used.
925 */
926
927int system_bus_clock (void)
928{
929 return((int) ((!system_bus_speed) ? ide_system_bus_speed() : system_bus_speed ));
930}
931
932EXPORT_SYMBOL(system_bus_clock);
933
David Brownellb887d2e2006-08-14 23:11:05 -0700934static int generic_ide_suspend(struct device *dev, pm_message_t mesg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
936 ide_drive_t *drive = dev->driver_data;
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100937 ide_hwif_t *hwif = HWIF(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 struct request rq;
939 struct request_pm_state rqpm;
940 ide_task_t args;
Shaohua Li5e321322007-10-11 23:53:58 +0200941 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100943 /* Call ACPI _GTM only once */
944 if (!(drive->dn % 2))
945 ide_acpi_get_timing(hwif);
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 memset(&rq, 0, sizeof(rq));
948 memset(&rqpm, 0, sizeof(rqpm));
949 memset(&args, 0, sizeof(args));
Jens Axboe4aff5e22006-08-10 08:44:47 +0200950 rq.cmd_type = REQ_TYPE_PM_SUSPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 rq.special = &args;
Jens Axboec00895a2006-09-30 20:29:12 +0200952 rq.data = &rqpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 rqpm.pm_step = ide_pm_state_start_suspend;
David Brownellb887d2e2006-08-14 23:11:05 -0700954 if (mesg.event == PM_EVENT_PRETHAW)
955 mesg.event = PM_EVENT_FREEZE;
956 rqpm.pm_state = mesg.event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Shaohua Li5e321322007-10-11 23:53:58 +0200958 ret = ide_do_drive_cmd(drive, &rq, ide_wait);
959 /* only call ACPI _PS3 after both drivers are suspended */
960 if (!ret && (((drive->dn % 2) && hwif->drives[0].present
961 && hwif->drives[1].present)
962 || !hwif->drives[0].present
963 || !hwif->drives[1].present))
964 ide_acpi_set_state(hwif, 0);
965 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
968static int generic_ide_resume(struct device *dev)
969{
970 ide_drive_t *drive = dev->driver_data;
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100971 ide_hwif_t *hwif = HWIF(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 struct request rq;
973 struct request_pm_state rqpm;
974 ide_task_t args;
Lee Trager0d2157f2007-06-08 15:14:30 +0200975 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100977 /* Call ACPI _STM only once */
Shaohua Li5e321322007-10-11 23:53:58 +0200978 if (!(drive->dn % 2)) {
979 ide_acpi_set_state(hwif, 1);
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100980 ide_acpi_push_timing(hwif);
Shaohua Li5e321322007-10-11 23:53:58 +0200981 }
Hannes Reineckee3a59b42007-02-07 18:19:37 +0100982
983 ide_acpi_exec_tfs(drive);
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 memset(&rq, 0, sizeof(rq));
986 memset(&rqpm, 0, sizeof(rqpm));
987 memset(&args, 0, sizeof(args));
Jens Axboe4aff5e22006-08-10 08:44:47 +0200988 rq.cmd_type = REQ_TYPE_PM_RESUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 rq.special = &args;
Jens Axboec00895a2006-09-30 20:29:12 +0200990 rq.data = &rqpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 rqpm.pm_step = ide_pm_state_start_resume;
Pavel Machekca078ba2005-09-03 15:56:57 -0700992 rqpm.pm_state = PM_EVENT_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Lee Trager0d2157f2007-06-08 15:14:30 +0200994 err = ide_do_drive_cmd(drive, &rq, ide_head_wait);
995
Rafael J. Wysockice9b2b02007-06-16 02:24:43 +0200996 if (err == 0 && dev->driver) {
997 ide_driver_t *drv = to_ide_driver(dev->driver);
998
999 if (drv->resume)
1000 drv->resume(drive);
1001 }
Lee Trager0d2157f2007-06-08 15:14:30 +02001002
1003 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
1005
1006int generic_ide_ioctl(ide_drive_t *drive, struct file *file, struct block_device *bdev,
1007 unsigned int cmd, unsigned long arg)
1008{
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02001009 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 ide_driver_t *drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 void __user *p = (void __user *)arg;
Linus Torvalds19850262007-07-17 15:57:42 -07001012 int err = 0, (*setfunc)(ide_drive_t *, int);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02001013 u8 *val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02001015 switch (cmd) {
1016 case HDIO_GET_32BIT: val = &drive->io_32bit; goto read_val;
1017 case HDIO_GET_KEEPSETTINGS: val = &drive->keep_settings; goto read_val;
1018 case HDIO_GET_UNMASKINTR: val = &drive->unmask; goto read_val;
1019 case HDIO_GET_DMA: val = &drive->using_dma; goto read_val;
1020 case HDIO_SET_32BIT: setfunc = set_io_32bit; goto set_val;
1021 case HDIO_SET_KEEPSETTINGS: setfunc = set_ksettings; goto set_val;
1022 case HDIO_SET_PIO_MODE: setfunc = set_pio_mode; goto set_val;
1023 case HDIO_SET_UNMASKINTR: setfunc = set_unmaskirq; goto set_val;
1024 case HDIO_SET_DMA: setfunc = set_using_dma; goto set_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 case HDIO_OBSOLETE_IDENTITY:
1029 case HDIO_GET_IDENTITY:
1030 if (bdev != bdev->bd_contains)
1031 return -EINVAL;
1032 if (drive->id_read == 0)
1033 return -ENOMSG;
1034 if (copy_to_user(p, drive->id, (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
1035 return -EFAULT;
1036 return 0;
1037
1038 case HDIO_GET_NICE:
1039 return put_user(drive->dsc_overlap << IDE_NICE_DSC_OVERLAP |
1040 drive->atapi_overlap << IDE_NICE_ATAPI_OVERLAP |
1041 drive->nice0 << IDE_NICE_0 |
1042 drive->nice1 << IDE_NICE_1 |
1043 drive->nice2 << IDE_NICE_2,
1044 (long __user *) arg);
1045
1046#ifdef CONFIG_IDE_TASK_IOCTL
1047 case HDIO_DRIVE_TASKFILE:
1048 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1049 return -EACCES;
1050 switch(drive->media) {
1051 case ide_disk:
1052 return ide_taskfile_ioctl(drive, cmd, arg);
1053 default:
1054 return -ENOMSG;
1055 }
1056#endif /* CONFIG_IDE_TASK_IOCTL */
1057
1058 case HDIO_DRIVE_CMD:
1059 if (!capable(CAP_SYS_RAWIO))
1060 return -EACCES;
1061 return ide_cmd_ioctl(drive, cmd, arg);
1062
1063 case HDIO_DRIVE_TASK:
1064 if (!capable(CAP_SYS_RAWIO))
1065 return -EACCES;
1066 return ide_task_ioctl(drive, cmd, arg);
1067
1068 case HDIO_SCAN_HWIF:
1069 {
1070 hw_regs_t hw;
1071 int args[3];
1072 if (!capable(CAP_SYS_RAWIO)) return -EACCES;
1073 if (copy_from_user(args, p, 3 * sizeof(int)))
1074 return -EFAULT;
1075 memset(&hw, 0, sizeof(hw));
1076 ide_init_hwif_ports(&hw, (unsigned long) args[0],
1077 (unsigned long) args[1], NULL);
1078 hw.irq = args[2];
Bartlomiej Zolnierkiewiczfd9bb532007-10-20 00:32:31 +02001079 if (ide_register_hw(&hw, NULL, 0, NULL) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return -EIO;
1081 return 0;
1082 }
1083 case HDIO_UNREGISTER_HWIF:
1084 if (!capable(CAP_SYS_RAWIO)) return -EACCES;
1085 /* (arg > MAX_HWIFS) checked in function */
1086 ide_unregister(arg);
1087 return 0;
1088 case HDIO_SET_NICE:
1089 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
1090 if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
1091 return -EPERM;
1092 drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
1093 drv = *(ide_driver_t **)bdev->bd_disk->private_data;
1094 if (drive->dsc_overlap && !drv->supports_dsc_overlap) {
1095 drive->dsc_overlap = 0;
1096 return -EPERM;
1097 }
1098 drive->nice1 = (arg >> IDE_NICE_1) & 1;
1099 return 0;
1100 case HDIO_DRIVE_RESET:
1101 {
1102 unsigned long flags;
1103 if (!capable(CAP_SYS_ADMIN)) return -EACCES;
1104
1105 /*
1106 * Abort the current command on the
1107 * group if there is one, taking
1108 * care not to allow anything else
1109 * to be queued and to die on the
1110 * spot if we miss one somehow
1111 */
1112
1113 spin_lock_irqsave(&ide_lock, flags);
1114
Alan Cox913759a2006-10-03 01:14:33 -07001115 if (HWGROUP(drive)->resetting) {
1116 spin_unlock_irqrestore(&ide_lock, flags);
1117 return -EBUSY;
1118 }
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 ide_abort(drive, "drive reset");
1121
Eric Sesterhenn125e1872006-06-23 02:06:06 -07001122 BUG_ON(HWGROUP(drive)->handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 /* Ensure nothing gets queued after we
1125 drop the lock. Reset will clear the busy */
1126
1127 HWGROUP(drive)->busy = 1;
1128 spin_unlock_irqrestore(&ide_lock, flags);
1129 (void) ide_do_reset(drive);
1130
1131 return 0;
1132 }
1133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 case HDIO_GET_BUSSTATE:
1135 if (!capable(CAP_SYS_ADMIN))
1136 return -EACCES;
1137 if (put_user(HWIF(drive)->bus_state, (long __user *)arg))
1138 return -EFAULT;
1139 return 0;
1140
1141 case HDIO_SET_BUSSTATE:
1142 if (!capable(CAP_SYS_ADMIN))
1143 return -EACCES;
1144 if (HWIF(drive)->busproc)
1145 return HWIF(drive)->busproc(drive, (int)arg);
1146 return -EOPNOTSUPP;
1147 default:
1148 return -EINVAL;
1149 }
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02001150
1151read_val:
Matthias Kaehlckef9383c42007-07-09 23:17:56 +02001152 mutex_lock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02001153 spin_lock_irqsave(&ide_lock, flags);
1154 err = *val;
1155 spin_unlock_irqrestore(&ide_lock, flags);
Matthias Kaehlckef9383c42007-07-09 23:17:56 +02001156 mutex_unlock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02001157 return err >= 0 ? put_user(err, (long __user *)arg) : err;
1158
1159set_val:
1160 if (bdev != bdev->bd_contains)
1161 err = -EINVAL;
1162 else {
1163 if (!capable(CAP_SYS_ADMIN))
1164 err = -EACCES;
1165 else {
Matthias Kaehlckef9383c42007-07-09 23:17:56 +02001166 mutex_lock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02001167 err = setfunc(drive, arg);
Matthias Kaehlckef9383c42007-07-09 23:17:56 +02001168 mutex_unlock(&ide_setting_mtx);
Bartlomiej Zolnierkiewicz14979432007-05-10 00:01:10 +02001169 }
1170 }
1171 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
1173
1174EXPORT_SYMBOL(generic_ide_ioctl);
1175
1176/*
1177 * stridx() returns the offset of c within s,
1178 * or -1 if c is '\0' or not found within s.
1179 */
1180static int __init stridx (const char *s, char c)
1181{
1182 char *i = strchr(s, c);
1183 return (i && c) ? i - s : -1;
1184}
1185
1186/*
1187 * match_parm() does parsing for ide_setup():
1188 *
1189 * 1. the first char of s must be '='.
1190 * 2. if the remainder matches one of the supplied keywords,
1191 * the index (1 based) of the keyword is negated and returned.
1192 * 3. if the remainder is a series of no more than max_vals numbers
1193 * separated by commas, the numbers are saved in vals[] and a
1194 * count of how many were saved is returned. Base10 is assumed,
1195 * and base16 is allowed when prefixed with "0x".
1196 * 4. otherwise, zero is returned.
1197 */
1198static int __init match_parm (char *s, const char *keywords[], int vals[], int max_vals)
1199{
1200 static const char *decimal = "0123456789";
1201 static const char *hex = "0123456789abcdef";
1202 int i, n;
1203
1204 if (*s++ == '=') {
1205 /*
1206 * Try matching against the supplied keywords,
1207 * and return -(index+1) if we match one
1208 */
1209 if (keywords != NULL) {
1210 for (i = 0; *keywords != NULL; ++i) {
1211 if (!strcmp(s, *keywords++))
1212 return -(i+1);
1213 }
1214 }
1215 /*
1216 * Look for a series of no more than "max_vals"
1217 * numeric values separated by commas, in base10,
1218 * or base16 when prefixed with "0x".
1219 * Return a count of how many were found.
1220 */
1221 for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
1222 vals[n] = i;
1223 while ((i = stridx(decimal, *++s)) >= 0)
1224 vals[n] = (vals[n] * 10) + i;
1225 if (*s == 'x' && !vals[n]) {
1226 while ((i = stridx(hex, *++s)) >= 0)
1227 vals[n] = (vals[n] * 0x10) + i;
1228 }
1229 if (++n == max_vals)
1230 break;
1231 if (*s == ',' || *s == ';')
1232 ++s;
1233 }
1234 if (!*s)
1235 return n;
1236 }
1237 return 0; /* zero = nothing matched */
1238}
1239
1240#ifdef CONFIG_BLK_DEV_ALI14XX
Bartlomiej Zolnierkiewicz84913882007-03-03 17:48:55 +01001241extern int probe_ali14xx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242extern int ali14xx_init(void);
1243#endif
1244#ifdef CONFIG_BLK_DEV_UMC8672
Bartlomiej Zolnierkiewicz84913882007-03-03 17:48:55 +01001245extern int probe_umc8672;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246extern int umc8672_init(void);
1247#endif
1248#ifdef CONFIG_BLK_DEV_DTC2278
Bartlomiej Zolnierkiewicz84913882007-03-03 17:48:55 +01001249extern int probe_dtc2278;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250extern int dtc2278_init(void);
1251#endif
1252#ifdef CONFIG_BLK_DEV_HT6560B
Bartlomiej Zolnierkiewicz84913882007-03-03 17:48:55 +01001253extern int probe_ht6560b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254extern int ht6560b_init(void);
1255#endif
1256#ifdef CONFIG_BLK_DEV_QD65XX
Bartlomiej Zolnierkiewicz84913882007-03-03 17:48:55 +01001257extern int probe_qd65xx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258extern int qd65xx_init(void);
1259#endif
1260
1261static int __initdata is_chipset_set[MAX_HWIFS];
1262
1263/*
1264 * ide_setup() gets called VERY EARLY during initialization,
1265 * to handle kernel "command line" strings beginning with "hdx=" or "ide".
1266 *
1267 * Remember to update Documentation/ide.txt if you change something here.
1268 */
1269static int __init ide_setup(char *s)
1270{
1271 int i, vals[3];
1272 ide_hwif_t *hwif;
1273 ide_drive_t *drive;
1274 unsigned int hw, unit;
1275 const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
1276 const char max_hwif = '0' + (MAX_HWIFS - 1);
1277
1278
1279 if (strncmp(s,"hd",2) == 0 && s[2] == '=') /* hd= is for hd.c */
1280 return 0; /* driver and not us */
1281
1282 if (strncmp(s,"ide",3) && strncmp(s,"idebus",6) && strncmp(s,"hd",2))
1283 return 0;
1284
1285 printk(KERN_INFO "ide_setup: %s", s);
1286 init_ide_data ();
1287
1288#ifdef CONFIG_BLK_DEV_IDEDOUBLER
1289 if (!strcmp(s, "ide=doubler")) {
1290 extern int ide_doubler;
1291
1292 printk(" : Enabled support for IDE doublers\n");
1293 ide_doubler = 1;
1294 return 1;
1295 }
1296#endif /* CONFIG_BLK_DEV_IDEDOUBLER */
1297
1298 if (!strcmp(s, "ide=nodma")) {
1299 printk(" : Prevented DMA\n");
1300 noautodma = 1;
Bartlomiej Zolnierkiewiczc2237012007-10-16 22:29:58 +02001301 goto obsolete_option;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
1303
Bartlomiej Zolnierkiewicz6d208b32007-05-10 00:01:11 +02001304#ifdef CONFIG_IDEPCI_PCIBUS_ORDER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if (!strcmp(s, "ide=reverse")) {
1306 ide_scan_direction = 1;
1307 printk(" : Enabled support for IDE inverse scan order.\n");
1308 return 1;
1309 }
Bartlomiej Zolnierkiewicz6d208b32007-05-10 00:01:11 +02001310#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Hannes Reineckee3a59b42007-02-07 18:19:37 +01001312#ifdef CONFIG_BLK_DEV_IDEACPI
1313 if (!strcmp(s, "ide=noacpi")) {
1314 //printk(" : Disable IDE ACPI support.\n");
1315 ide_noacpi = 1;
1316 return 1;
1317 }
1318 if (!strcmp(s, "ide=acpigtf")) {
1319 //printk(" : Enable IDE ACPI _GTF support.\n");
1320 ide_noacpitfs = 0;
1321 return 1;
1322 }
1323 if (!strcmp(s, "ide=acpionboot")) {
1324 //printk(" : Call IDE ACPI methods on boot.\n");
1325 ide_noacpionboot = 0;
1326 return 1;
1327 }
1328#endif /* CONFIG_BLK_DEV_IDEACPI */
1329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 /*
1331 * Look for drive options: "hdx="
1332 */
1333 if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
1334 const char *hd_words[] = {
Bartlomiej Zolnierkiewiczc2237012007-10-16 22:29:58 +02001335 "none", "noprobe", "nowerr", "cdrom", "nodma",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 "autotune", "noautotune", "minus8", "swapdata", "bswap",
Jens Axboe36193482006-07-28 08:54:59 +02001337 "noflush", "remap", "remap63", "scsi", NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 unit = s[2] - 'a';
1339 hw = unit / MAX_DRIVES;
1340 unit = unit % MAX_DRIVES;
1341 hwif = &ide_hwifs[hw];
1342 drive = &hwif->drives[unit];
1343 if (strncmp(s + 4, "ide-", 4) == 0) {
1344 strlcpy(drive->driver_req, s + 4, sizeof(drive->driver_req));
1345 goto done;
1346 }
1347 switch (match_parm(&s[3], hd_words, vals, 3)) {
1348 case -1: /* "none" */
1349 case -2: /* "noprobe" */
1350 drive->noprobe = 1;
1351 goto done;
1352 case -3: /* "nowerr" */
1353 drive->bad_wstat = BAD_R_STAT;
1354 hwif->noprobe = 0;
1355 goto done;
1356 case -4: /* "cdrom" */
1357 drive->present = 1;
1358 drive->media = ide_cdrom;
1359 /* an ATAPI device ignores DRDY */
1360 drive->ready_stat = 0;
1361 hwif->noprobe = 0;
1362 goto done;
Bartlomiej Zolnierkiewiczc2237012007-10-16 22:29:58 +02001363 case -5: /* nodma */
1364 drive->nodma = 1;
1365 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 case -6: /* "autotune" */
1367 drive->autotune = IDE_TUNE_AUTO;
1368 goto obsolete_option;
1369 case -7: /* "noautotune" */
1370 drive->autotune = IDE_TUNE_NOAUTO;
1371 goto obsolete_option;
1372 case -9: /* "swapdata" */
1373 case -10: /* "bswap" */
1374 drive->bswap = 1;
1375 goto done;
Jens Axboe36193482006-07-28 08:54:59 +02001376 case -11: /* noflush */
1377 drive->noflush = 1;
1378 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 case -12: /* "remap" */
1380 drive->remap_0_to_1 = 1;
1381 goto done;
1382 case -13: /* "remap63" */
1383 drive->sect0 = 63;
1384 goto done;
1385 case -14: /* "scsi" */
1386 drive->scsi = 1;
1387 goto done;
1388 case 3: /* cyl,head,sect */
1389 drive->media = ide_disk;
1390 drive->ready_stat = READY_STAT;
1391 drive->cyl = drive->bios_cyl = vals[0];
1392 drive->head = drive->bios_head = vals[1];
1393 drive->sect = drive->bios_sect = vals[2];
1394 drive->present = 1;
1395 drive->forced_geom = 1;
1396 hwif->noprobe = 0;
1397 goto done;
1398 default:
1399 goto bad_option;
1400 }
1401 }
1402
1403 if (s[0] != 'i' || s[1] != 'd' || s[2] != 'e')
1404 goto bad_option;
1405 /*
1406 * Look for bus speed option: "idebus="
1407 */
1408 if (s[3] == 'b' && s[4] == 'u' && s[5] == 's') {
1409 if (match_parm(&s[6], NULL, vals, 1) != 1)
1410 goto bad_option;
1411 if (vals[0] >= 20 && vals[0] <= 66) {
1412 idebus_parameter = vals[0];
1413 } else
1414 printk(" -- BAD BUS SPEED! Expected value from 20 to 66");
1415 goto done;
1416 }
1417 /*
1418 * Look for interface options: "idex="
1419 */
1420 if (s[3] >= '0' && s[3] <= max_hwif) {
1421 /*
1422 * Be VERY CAREFUL changing this: note hardcoded indexes below
1423 * (-8, -9, -10) are reserved to ease the hardcoding.
1424 */
1425 static const char *ide_words[] = {
Bartlomiej Zolnierkiewiczb6209a92007-03-03 17:48:55 +01001426 "noprobe", "serialize", "minus3", "minus4",
Bartlomiej Zolnierkiewicz1b516942007-10-16 22:29:57 +02001427 "reset", "minus6", "ata66", "minus8", "minus9",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 "minus10", "four", "qd65xx", "ht6560b", "cmd640_vlb",
1429 "dtc2278", "umc8672", "ali14xx", NULL };
Bartlomiej Zolnierkiewicz9239b332007-10-20 00:32:33 +02001430
1431 hw_regs_t hwregs;
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 hw = s[3] - '0';
1434 hwif = &ide_hwifs[hw];
1435 i = match_parm(&s[4], ide_words, vals, 3);
1436
1437 /*
1438 * Cryptic check to ensure chipset not already set for hwif.
1439 * Note: we can't depend on hwif->chipset here.
1440 */
1441 if ((i >= -18 && i <= -11) || (i > 0 && i <= 3)) {
1442 /* chipset already specified */
1443 if (is_chipset_set[hw])
1444 goto bad_option;
1445 if (i > -18 && i <= -11) {
1446 /* these drivers are for "ide0=" only */
1447 if (hw != 0)
1448 goto bad_hwif;
1449 /* chipset already specified for 2nd port */
1450 if (is_chipset_set[hw+1])
1451 goto bad_option;
1452 }
1453 is_chipset_set[hw] = 1;
1454 printk("\n");
1455 }
1456
1457 switch (i) {
1458#ifdef CONFIG_BLK_DEV_ALI14XX
1459 case -17: /* "ali14xx" */
1460 probe_ali14xx = 1;
1461 goto done;
1462#endif
1463#ifdef CONFIG_BLK_DEV_UMC8672
1464 case -16: /* "umc8672" */
1465 probe_umc8672 = 1;
1466 goto done;
1467#endif
1468#ifdef CONFIG_BLK_DEV_DTC2278
1469 case -15: /* "dtc2278" */
1470 probe_dtc2278 = 1;
1471 goto done;
1472#endif
1473#ifdef CONFIG_BLK_DEV_CMD640
1474 case -14: /* "cmd640_vlb" */
1475 {
1476 extern int cmd640_vlb; /* flag for cmd640.c */
1477 cmd640_vlb = 1;
1478 goto done;
1479 }
1480#endif
1481#ifdef CONFIG_BLK_DEV_HT6560B
1482 case -13: /* "ht6560b" */
1483 probe_ht6560b = 1;
1484 goto done;
1485#endif
1486#ifdef CONFIG_BLK_DEV_QD65XX
1487 case -12: /* "qd65xx" */
1488 probe_qd65xx = 1;
1489 goto done;
1490#endif
1491#ifdef CONFIG_BLK_DEV_4DRIVES
1492 case -11: /* "four" drives on one set of ports */
1493 {
1494 ide_hwif_t *mate = &ide_hwifs[hw^1];
1495 mate->drives[0].select.all ^= 0x20;
1496 mate->drives[1].select.all ^= 0x20;
1497 hwif->chipset = mate->chipset = ide_4drives;
1498 mate->irq = hwif->irq;
1499 memcpy(mate->io_ports, hwif->io_ports, sizeof(hwif->io_ports));
Bartlomiej Zolnierkiewiczb6209a92007-03-03 17:48:55 +01001500 hwif->mate = mate;
1501 mate->mate = hwif;
1502 hwif->serialized = mate->serialized = 1;
1503 goto obsolete_option;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 }
1505#endif /* CONFIG_BLK_DEV_4DRIVES */
1506 case -10: /* minus10 */
1507 case -9: /* minus9 */
1508 case -8: /* minus8 */
Bartlomiej Zolnierkiewicz1b516942007-10-16 22:29:57 +02001509 case -6:
Bartlomiej Zolnierkiewiczb6209a92007-03-03 17:48:55 +01001510 case -4:
1511 case -3:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 goto bad_option;
1513 case -7: /* ata66 */
1514#ifdef CONFIG_BLK_DEV_IDEPCI
Bartlomiej Zolnierkiewicz49521f92007-07-09 23:17:58 +02001515 /*
1516 * Use ATA_CBL_PATA40_SHORT so drive side
1517 * cable detection is also overriden.
1518 */
1519 hwif->cbl = ATA_CBL_PATA40_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 goto obsolete_option;
1521#else
1522 goto bad_hwif;
1523#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 case -5: /* "reset" */
1525 hwif->reset = 1;
1526 goto obsolete_option;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 case -2: /* "serialize" */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 hwif->mate = &ide_hwifs[hw^1];
1529 hwif->mate->mate = hwif;
1530 hwif->serialized = hwif->mate->serialized = 1;
1531 goto obsolete_option;
1532
1533 case -1: /* "noprobe" */
1534 hwif->noprobe = 1;
1535 goto done;
1536
1537 case 1: /* base */
1538 vals[1] = vals[0] + 0x206; /* default ctl */
1539 case 2: /* base,ctl */
1540 vals[2] = 0; /* default irq = probe for it */
1541 case 3: /* base,ctl,irq */
Bartlomiej Zolnierkiewicz9239b332007-10-20 00:32:33 +02001542 memset(&hwregs, 0, sizeof(hwregs));
1543 ide_init_hwif_ports(&hwregs, vals[0], vals[1], &hwif->irq);
1544 memcpy(hwif->io_ports, hwregs.io_ports, sizeof(hwif->io_ports));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 hwif->irq = vals[2];
1546 hwif->noprobe = 0;
1547 hwif->chipset = ide_forced;
1548 goto obsolete_option;
1549
1550 case 0: goto bad_option;
1551 default:
1552 printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
1553 return 1;
1554 }
1555 }
1556bad_option:
1557 printk(" -- BAD OPTION\n");
1558 return 1;
1559obsolete_option:
1560 printk(" -- OBSOLETE OPTION, WILL BE REMOVED SOON!\n");
1561 return 1;
1562bad_hwif:
1563 printk("-- NOT SUPPORTED ON ide%d", hw);
1564done:
1565 printk("\n");
1566 return 1;
1567}
1568
Al Viroeb797222007-02-01 13:52:38 +00001569extern void __init pnpide_init(void);
1570extern void __exit pnpide_exit(void);
1571extern void __init h8300_ide_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573/*
1574 * probe_for_hwifs() finds/initializes "known" IDE interfaces
1575 */
1576static void __init probe_for_hwifs (void)
1577{
Bartlomiej Zolnierkiewicz6d208b32007-05-10 00:01:11 +02001578#ifdef CONFIG_IDEPCI_PCIBUS_ORDER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 ide_scan_pcibus(ide_scan_direction);
Bartlomiej Zolnierkiewicz6d208b32007-05-10 00:01:11 +02001580#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582#ifdef CONFIG_ETRAX_IDE
1583 {
1584 extern void init_e100_ide(void);
1585 init_e100_ide();
1586 }
1587#endif /* CONFIG_ETRAX_IDE */
1588#ifdef CONFIG_BLK_DEV_CMD640
1589 {
1590 extern void ide_probe_for_cmd640x(void);
1591 ide_probe_for_cmd640x();
1592 }
1593#endif /* CONFIG_BLK_DEV_CMD640 */
1594#ifdef CONFIG_BLK_DEV_IDE_PMAC
1595 {
Andrew Morton9e5755b2007-03-03 17:48:54 +01001596 extern int pmac_ide_probe(void);
1597 (void)pmac_ide_probe();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
1599#endif /* CONFIG_BLK_DEV_IDE_PMAC */
1600#ifdef CONFIG_BLK_DEV_GAYLE
1601 {
1602 extern void gayle_init(void);
1603 gayle_init();
1604 }
1605#endif /* CONFIG_BLK_DEV_GAYLE */
1606#ifdef CONFIG_BLK_DEV_FALCON_IDE
1607 {
1608 extern void falconide_init(void);
1609 falconide_init();
1610 }
1611#endif /* CONFIG_BLK_DEV_FALCON_IDE */
1612#ifdef CONFIG_BLK_DEV_MAC_IDE
1613 {
1614 extern void macide_init(void);
1615 macide_init();
1616 }
1617#endif /* CONFIG_BLK_DEV_MAC_IDE */
1618#ifdef CONFIG_BLK_DEV_Q40IDE
1619 {
1620 extern void q40ide_init(void);
1621 q40ide_init();
1622 }
1623#endif /* CONFIG_BLK_DEV_Q40IDE */
1624#ifdef CONFIG_BLK_DEV_BUDDHA
1625 {
1626 extern void buddha_init(void);
1627 buddha_init();
1628 }
1629#endif /* CONFIG_BLK_DEV_BUDDHA */
1630#ifdef CONFIG_BLK_DEV_IDEPNP
1631 pnpide_init();
1632#endif
1633#ifdef CONFIG_H8300
1634 h8300_ide_init();
1635#endif
1636}
1637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638/*
1639 * Probe module
1640 */
1641
1642EXPORT_SYMBOL(ide_lock);
1643
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001644static int ide_bus_match(struct device *dev, struct device_driver *drv)
1645{
1646 return 1;
1647}
1648
Kay Sievers263756e2005-12-12 18:03:44 +01001649static char *media_string(ide_drive_t *drive)
1650{
1651 switch (drive->media) {
1652 case ide_disk:
1653 return "disk";
1654 case ide_cdrom:
1655 return "cdrom";
1656 case ide_tape:
1657 return "tape";
1658 case ide_floppy:
1659 return "floppy";
Danny Kukawkaa7a832d2007-04-10 22:39:14 +02001660 case ide_optical:
1661 return "optical";
Kay Sievers263756e2005-12-12 18:03:44 +01001662 default:
1663 return "UNKNOWN";
1664 }
1665}
1666
1667static ssize_t media_show(struct device *dev, struct device_attribute *attr, char *buf)
1668{
1669 ide_drive_t *drive = to_ide_device(dev);
1670 return sprintf(buf, "%s\n", media_string(drive));
1671}
1672
1673static ssize_t drivename_show(struct device *dev, struct device_attribute *attr, char *buf)
1674{
1675 ide_drive_t *drive = to_ide_device(dev);
1676 return sprintf(buf, "%s\n", drive->name);
1677}
1678
1679static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
1680{
1681 ide_drive_t *drive = to_ide_device(dev);
1682 return sprintf(buf, "ide:m-%s\n", media_string(drive));
1683}
1684
Bartlomiej Zolnierkiewicze11b9032007-12-12 23:31:58 +01001685static ssize_t model_show(struct device *dev, struct device_attribute *attr,
1686 char *buf)
1687{
1688 ide_drive_t *drive = to_ide_device(dev);
1689 return sprintf(buf, "%s\n", drive->id->model);
1690}
1691
1692static ssize_t firmware_show(struct device *dev, struct device_attribute *attr,
1693 char *buf)
1694{
1695 ide_drive_t *drive = to_ide_device(dev);
1696 return sprintf(buf, "%s\n", drive->id->fw_rev);
1697}
1698
1699static ssize_t serial_show(struct device *dev, struct device_attribute *attr,
1700 char *buf)
1701{
1702 ide_drive_t *drive = to_ide_device(dev);
1703 return sprintf(buf, "%s\n", drive->id->serial_no);
1704}
1705
Kay Sievers263756e2005-12-12 18:03:44 +01001706static struct device_attribute ide_dev_attrs[] = {
1707 __ATTR_RO(media),
1708 __ATTR_RO(drivename),
1709 __ATTR_RO(modalias),
Bartlomiej Zolnierkiewicze11b9032007-12-12 23:31:58 +01001710 __ATTR_RO(model),
1711 __ATTR_RO(firmware),
1712 __ATTR(serial, 0400, serial_show, NULL),
Kay Sievers263756e2005-12-12 18:03:44 +01001713 __ATTR_NULL
1714};
1715
Kay Sievers7eff2e72007-08-14 15:15:12 +02001716static int ide_uevent(struct device *dev, struct kobj_uevent_env *env)
Kay Sievers263756e2005-12-12 18:03:44 +01001717{
1718 ide_drive_t *drive = to_ide_device(dev);
Kay Sievers263756e2005-12-12 18:03:44 +01001719
Kay Sievers7eff2e72007-08-14 15:15:12 +02001720 add_uevent_var(env, "MEDIA=%s", media_string(drive));
1721 add_uevent_var(env, "DRIVENAME=%s", drive->name);
1722 add_uevent_var(env, "MODALIAS=ide:m-%s", media_string(drive));
Kay Sievers263756e2005-12-12 18:03:44 +01001723 return 0;
1724}
1725
Russell King4031bbe2006-01-06 11:41:00 +00001726static int generic_ide_probe(struct device *dev)
1727{
1728 ide_drive_t *drive = to_ide_device(dev);
1729 ide_driver_t *drv = to_ide_driver(dev->driver);
1730
1731 return drv->probe ? drv->probe(drive) : -ENODEV;
1732}
1733
1734static int generic_ide_remove(struct device *dev)
1735{
1736 ide_drive_t *drive = to_ide_device(dev);
1737 ide_driver_t *drv = to_ide_driver(dev->driver);
1738
1739 if (drv->remove)
1740 drv->remove(drive);
1741
1742 return 0;
1743}
1744
1745static void generic_ide_shutdown(struct device *dev)
1746{
1747 ide_drive_t *drive = to_ide_device(dev);
1748 ide_driver_t *drv = to_ide_driver(dev->driver);
1749
1750 if (dev->driver && drv->shutdown)
1751 drv->shutdown(drive);
1752}
1753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754struct bus_type ide_bus_type = {
1755 .name = "ide",
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001756 .match = ide_bus_match,
Kay Sievers263756e2005-12-12 18:03:44 +01001757 .uevent = ide_uevent,
Russell King4031bbe2006-01-06 11:41:00 +00001758 .probe = generic_ide_probe,
1759 .remove = generic_ide_remove,
1760 .shutdown = generic_ide_shutdown,
Kay Sievers263756e2005-12-12 18:03:44 +01001761 .dev_attrs = ide_dev_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 .suspend = generic_ide_suspend,
1763 .resume = generic_ide_resume,
1764};
1765
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001766EXPORT_SYMBOL_GPL(ide_bus_type);
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768/*
1769 * This is gets invoked once during initialization, to set *everything* up
1770 */
1771static int __init ide_init(void)
1772{
Randy Dunlap349ae232006-10-03 01:14:23 -07001773 int ret;
1774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 printk(KERN_INFO "Uniform Multi-Platform E-IDE driver " REVISION "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 system_bus_speed = ide_system_bus_speed();
1777
Randy Dunlap349ae232006-10-03 01:14:23 -07001778 ret = bus_register(&ide_bus_type);
1779 if (ret < 0) {
1780 printk(KERN_WARNING "IDE: bus_register error: %d\n", ret);
1781 return ret;
1782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
1784 init_ide_data();
1785
Bartlomiej Zolnierkiewicz5cbf79c2007-05-10 00:01:11 +02001786 proc_ide_create();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
1788#ifdef CONFIG_BLK_DEV_ALI14XX
1789 if (probe_ali14xx)
1790 (void)ali14xx_init();
1791#endif
1792#ifdef CONFIG_BLK_DEV_UMC8672
1793 if (probe_umc8672)
1794 (void)umc8672_init();
1795#endif
1796#ifdef CONFIG_BLK_DEV_DTC2278
1797 if (probe_dtc2278)
1798 (void)dtc2278_init();
1799#endif
1800#ifdef CONFIG_BLK_DEV_HT6560B
1801 if (probe_ht6560b)
1802 (void)ht6560b_init();
1803#endif
1804#ifdef CONFIG_BLK_DEV_QD65XX
1805 if (probe_qd65xx)
1806 (void)qd65xx_init();
1807#endif
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 /* Probe for special PCI and other "known" interface chipsets. */
1810 probe_for_hwifs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return 0;
1813}
1814
1815#ifdef MODULE
1816static char *options = NULL;
1817module_param(options, charp, 0);
1818MODULE_LICENSE("GPL");
1819
1820static void __init parse_options (char *line)
1821{
1822 char *next = line;
1823
1824 if (line == NULL || !*line)
1825 return;
1826 while ((line = next) != NULL) {
1827 if ((next = strchr(line,' ')) != NULL)
1828 *next++ = 0;
1829 if (!ide_setup(line))
1830 printk (KERN_INFO "Unknown option '%s'\n", line);
1831 }
1832}
1833
Sam Ravnborgfbd8ad32006-03-25 03:07:11 -08001834int __init init_module (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
1836 parse_options(options);
1837 return ide_init();
1838}
1839
Al Viroeb797222007-02-01 13:52:38 +00001840void __exit cleanup_module (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
1842 int index;
1843
1844 for (index = 0; index < MAX_HWIFS; ++index)
1845 ide_unregister(index);
1846
Tejun Heo68550362007-01-27 13:47:02 +01001847#ifdef CONFIG_BLK_DEV_IDEPNP
1848 pnpide_exit();
1849#endif
1850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 proc_ide_destroy();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
1853 bus_unregister(&ide_bus_type);
1854}
1855
1856#else /* !MODULE */
1857
1858__setup("", ide_setup);
1859
1860module_init(ide_init);
1861
1862#endif /* MODULE */