]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/ide/ide.c
ide: add ide_init_port_hw() helper
[mirror_ubuntu-jammy-kernel.git] / drivers / ide / ide.c
CommitLineData
1da177e4
LT
1/*
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 *
1da177e4
LT
19 * ...
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
1da177e4
LT
46 */
47
48#define REVISION "Revision: 7.00alpha2"
49#define VERSION "Id: ide.c 7.00a2 20020906"
50
1da177e4
LT
51#define _IDE_C /* Tell ide.h it's really us */
52
1da177e4
LT
53#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>
1da177e4
LT
69#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 */
1da177e4 93
ef29888e 94DEFINE_MUTEX(ide_cfg_mtx);
1da177e4
LT
95 __cacheline_aligned_in_smp DEFINE_SPINLOCK(ide_lock);
96
6d208b39 97#ifdef CONFIG_IDEPCI_PCIBUS_ORDER
1da177e4
LT
98static int ide_scan_direction; /* THIS was formerly 2.2.x pci=reverse */
99#endif
100
1da177e4 101int noautodma = 0;
1da177e4 102
e3a59b4d
HR
103#ifdef CONFIG_BLK_DEV_IDEACPI
104int ide_noacpi = 0;
105int ide_noacpitfs = 1;
106int ide_noacpionboot = 1;
107#endif
108
1da177e4
LT
109/*
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
1da177e4
LT
116/*
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
f36d4024 137 init_completion(&hwif->gendev_rel_comp);
1da177e4
LT
138
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;
1da177e4
LT
157 drive->vdma = 0;
158 INIT_LIST_HEAD(&drive->list);
f36d4024 159 init_completion(&drive->gendev_rel_comp);
1da177e4
LT
160 }
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
1da177e4
LT
171 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)
9239b333 216 hwif->irq =
1da177e4
LT
217 ide_init_default_irq(hwif->io_ports[IDE_DATA_OFFSET]);
218#endif
219 }
220#ifdef CONFIG_IDE_ARM
1da177e4 221 ide_arm_init();
1da177e4
LT
222#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
baa8f3e9
BZ
267ide_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
1da177e4
LT
291static 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
2ad1e558 317 if (hwif->mmio)
1da177e4 318 return 0;
1da177e4
LT
319 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
2ad1e558 365 if (hwif->mmio)
1da177e4
LT
366 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 *
020e322d 383 * Restore hwif to a previous state by copying most settings
1da177e4
LT
384 * 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
6a824c92
BZ
399 hwif->host_flags = tmp_hwif->host_flags;
400
4099d143
BZ
401 hwif->pio_mask = tmp_hwif->pio_mask;
402
1da177e4
LT
403 hwif->ultra_mask = tmp_hwif->ultra_mask;
404 hwif->mwdma_mask = tmp_hwif->mwdma_mask;
405 hwif->swdma_mask = tmp_hwif->swdma_mask;
406
49521f97
BZ
407 hwif->cbl = tmp_hwif->cbl;
408
1da177e4
LT
409 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
26bcb879 417 hwif->set_pio_mode = tmp_hwif->set_pio_mode;
88b2b32b 418 hwif->set_dma_mode = tmp_hwif->set_dma_mode;
b4e44369 419 hwif->mdma_filter = tmp_hwif->mdma_filter;
2d5eaa6d 420 hwif->udma_filter = tmp_hwif->udma_filter;
1da177e4
LT
421 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;
1da177e4
LT
425 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
15ce926a 434 hwif->dma_host_set = tmp_hwif->dma_host_set;
1da177e4
LT
435 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;
1da177e4 439 hwif->ide_dma_test_irq = tmp_hwif->ide_dma_test_irq;
f0dd8712 440 hwif->ide_dma_clear_irq = tmp_hwif->ide_dma_clear_irq;
841d2a9b 441 hwif->dma_lost_irq = tmp_hwif->dma_lost_irq;
c283f5db 442 hwif->dma_timeout = tmp_hwif->dma_timeout;
1da177e4
LT
443
444 hwif->OUTB = tmp_hwif->OUTB;
445 hwif->OUTBSYNC = tmp_hwif->OUTBSYNC;
446 hwif->OUTW = tmp_hwif->OUTW;
1da177e4
LT
447 hwif->OUTSW = tmp_hwif->OUTSW;
448 hwif->OUTSL = tmp_hwif->OUTSL;
449
450 hwif->INB = tmp_hwif->INB;
451 hwif->INW = tmp_hwif->INW;
1da177e4
LT
452 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;
1da177e4
LT
459
460#ifndef CONFIG_BLK_DEV_IDECS
461 hwif->irq = tmp_hwif->irq;
462#endif
463
464 hwif->dma_base = tmp_hwif->dma_base;
1da177e4
LT
465 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
1da177e4
LT
471 hwif->config_data = tmp_hwif->config_data;
472 hwif->select_data = tmp_hwif->select_data;
020e322d
SS
473 hwif->extra_base = tmp_hwif->extra_base;
474 hwif->extra_ports = tmp_hwif->extra_ports;
1da177e4
LT
475
476 hwif->hwif_data = tmp_hwif->hwif_data;
477}
478
479/**
020e322d 480 * ide_unregister - free an IDE interface
1da177e4
LT
481 * @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
020e322d
SS
493 * the interface will not be reopened (present/vanishing locking
494 * isn't yet done BTW). After we commit to the final kill we
1da177e4
LT
495 * 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;
ef29888e 505 static ide_hwif_t tmp_hwif; /* protected by ide_cfg_mtx */
1da177e4 506 ide_hwgroup_t *hwgroup;
8604affd 507 int irq_count = 0, unit;
1da177e4
LT
508
509 BUG_ON(index >= MAX_HWIFS);
510
511 BUG_ON(in_interrupt());
512 BUG_ON(irqs_disabled());
ef29888e 513 mutex_lock(&ide_cfg_mtx);
1da177e4
LT
514 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];
94f6c59d 520 if (!drive->present)
1da177e4 521 continue;
8604affd
BZ
522 spin_unlock_irq(&ide_lock);
523 device_unregister(&drive->gendev);
f36d4024 524 wait_for_completion(&drive->gendev_rel_comp);
8604affd 525 spin_lock_irq(&ide_lock);
1da177e4
LT
526 }
527 hwif->present = 0;
528
529 spin_unlock_irq(&ide_lock);
530
5cbf79cd 531 ide_proc_unregister_port(hwif);
1da177e4
LT
532
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 */
1da177e4
LT
558 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);
f36d4024 584 wait_for_completion(&hwif->gendev_rel_comp);
1da177e4
LT
585
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;
1da177e4
LT
598 hwif->dma_command = 0;
599 hwif->dma_vendor1 = 0;
600 hwif->dma_status = 0;
601 hwif->dma_vendor3 = 0;
602 hwif->dma_prdtable = 0;
020e322d
SS
603
604 hwif->extra_base = 0;
605 hwif->extra_ports = 0;
1da177e4
LT
606 }
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);
ef29888e 619 mutex_unlock(&ide_cfg_mtx);
1da177e4
LT
620}
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
2c3e0262 651 memset(hw, 0, sizeof(hw_regs_t));
1da177e4
LT
652 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;
1da177e4
LT
672 hw->ack_intr = ack_intr;
673/*
674 * hw->iops = iops;
675 */
676}
677
57c802e8
BZ
678void 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
1da177e4 689/**
fd9bb539 690 * ide_register_hw - register IDE interface
1da177e4 691 * @hw: hardware registers
f01393e4 692 * @quirkproc: quirkproc function
869c56ee 693 * @initializing: set while initializing built-in drivers
1da177e4 694 * @hwifp: pointer to returned hwif
1da177e4
LT
695 *
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
f01393e4 702int ide_register_hw(hw_regs_t *hw, void (*quirkproc)(ide_drive_t *),
fd9bb539 703 int initializing, ide_hwif_t **hwifp)
1da177e4
LT
704{
705 int index, retry = 1;
706 ide_hwif_t *hwif;
707
795d74b5
BZ
708 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
1da177e4
LT
717 do {
718 for (index = 0; index < MAX_HWIFS; ++index) {
719 hwif = &ide_hwifs[index];
8f173b5b 720 if (hwif->io_ports[IDE_DATA_OFFSET] == hw->io_ports[IDE_DATA_OFFSET])
1da177e4
LT
721 goto found;
722 }
723 for (index = 0; index < MAX_HWIFS; ++index) {
724 hwif = &ide_hwifs[index];
725 if (hwif->hold)
726 continue;
795d74b5 727 if (!hwif->present && hwif->mate == NULL)
1da177e4
LT
728 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;
57c802e8
BZ
743
744 ide_init_port_hw(hwif, hw);
f01393e4 745 hwif->quirkproc = quirkproc;
1da177e4 746
8447d9d5
BZ
747 if (initializing == 0) {
748 u8 idx[4] = { index, 0xff, 0xff, 0xff };
749
750 ide_device_add(idx);
1da177e4
LT
751 }
752
753 if (hwifp)
754 *hwifp = hwif;
755
756 return (initializing || hwif->present) ? index : -1;
757}
758
1da177e4
LT
759EXPORT_SYMBOL(ide_register_hw);
760
761/*
762 * Locks for IDE setting functionality
763 */
764
f9383c42 765DEFINE_MUTEX(ide_setting_mtx);
1da177e4 766
f9383c42 767EXPORT_SYMBOL_GPL(ide_setting_mtx);
1497943e 768
1da177e4
LT
769/**
770 * 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
7662d046 802int set_io_32bit(ide_drive_t *drive, int arg)
1da177e4 803{
1497943e
BZ
804 if (drive->no_io_32bit)
805 return -EPERM;
806
807 if (arg < 0 || arg > 1 + (SUPPORT_VLB_SYNC << 1))
808 return -EINVAL;
809
644a9d76
BZ
810 if (ide_spin_wait_hwgroup(drive))
811 return -EBUSY;
812
1da177e4
LT
813 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 */
644a9d76
BZ
818
819 spin_unlock_irq(&ide_lock);
820
1da177e4
LT
821 return 0;
822}
823
1497943e
BZ
824static 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
7662d046 837int set_using_dma(ide_drive_t *drive, int arg)
1da177e4
LT
838{
839#ifdef CONFIG_BLK_DEV_IDEDMA
87996204
BZ
840 ide_hwif_t *hwif = drive->hwif;
841 int err = -EPERM;
842
1497943e
BZ
843 if (arg < 0 || arg > 1)
844 return -EINVAL;
845
1da177e4 846 if (!drive->id || !(drive->id->capability & 1))
87996204
BZ
847 goto out;
848
15ce926a 849 if (hwif->dma_host_set == NULL)
87996204
BZ
850 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
1da177e4 863 if (arg) {
23b1bd45 864 if (ide_set_dma(drive))
87996204 865 err = -EIO;
7469aaf6
BZ
866 } else
867 ide_dma_off(drive);
87996204
BZ
868
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;
1da177e4 877#else
1497943e
BZ
878 if (arg < 0 || arg > 1)
879 return -EINVAL;
880
1da177e4
LT
881 return -EPERM;
882#endif
883}
884
7662d046 885int set_pio_mode(ide_drive_t *drive, int arg)
1da177e4
LT
886{
887 struct request rq;
888
1497943e
BZ
889 if (arg < 0 || arg > 255)
890 return -EINVAL;
891
26bcb879 892 if (drive->hwif->set_pio_mode == NULL)
1da177e4 893 return -ENOSYS;
26bcb879 894
1da177e4
LT
895 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
1497943e
BZ
904static 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
1da177e4
LT
920/**
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
b887d2e6 934static int generic_ide_suspend(struct device *dev, pm_message_t mesg)
1da177e4
LT
935{
936 ide_drive_t *drive = dev->driver_data;
e3a59b4d 937 ide_hwif_t *hwif = HWIF(drive);
1da177e4
LT
938 struct request rq;
939 struct request_pm_state rqpm;
940 ide_task_t args;
5e32132b 941 int ret;
1da177e4 942
e3a59b4d
HR
943 /* Call ACPI _GTM only once */
944 if (!(drive->dn % 2))
945 ide_acpi_get_timing(hwif);
946
1da177e4
LT
947 memset(&rq, 0, sizeof(rq));
948 memset(&rqpm, 0, sizeof(rqpm));
949 memset(&args, 0, sizeof(args));
4aff5e23 950 rq.cmd_type = REQ_TYPE_PM_SUSPEND;
1da177e4 951 rq.special = &args;
c00895ab 952 rq.data = &rqpm;
1da177e4 953 rqpm.pm_step = ide_pm_state_start_suspend;
b887d2e6
DB
954 if (mesg.event == PM_EVENT_PRETHAW)
955 mesg.event = PM_EVENT_FREEZE;
956 rqpm.pm_state = mesg.event;
1da177e4 957
5e32132b
SL
958 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;
1da177e4
LT
966}
967
968static int generic_ide_resume(struct device *dev)
969{
970 ide_drive_t *drive = dev->driver_data;
e3a59b4d 971 ide_hwif_t *hwif = HWIF(drive);
1da177e4
LT
972 struct request rq;
973 struct request_pm_state rqpm;
974 ide_task_t args;
0d2157f7 975 int err;
1da177e4 976
e3a59b4d 977 /* Call ACPI _STM only once */
5e32132b
SL
978 if (!(drive->dn % 2)) {
979 ide_acpi_set_state(hwif, 1);
e3a59b4d 980 ide_acpi_push_timing(hwif);
5e32132b 981 }
e3a59b4d
HR
982
983 ide_acpi_exec_tfs(drive);
984
1da177e4
LT
985 memset(&rq, 0, sizeof(rq));
986 memset(&rqpm, 0, sizeof(rqpm));
987 memset(&args, 0, sizeof(args));
4aff5e23 988 rq.cmd_type = REQ_TYPE_PM_RESUME;
1da177e4 989 rq.special = &args;
c00895ab 990 rq.data = &rqpm;
1da177e4 991 rqpm.pm_step = ide_pm_state_start_resume;
ca078bae 992 rqpm.pm_state = PM_EVENT_ON;
1da177e4 993
0d2157f7
LT
994 err = ide_do_drive_cmd(drive, &rq, ide_head_wait);
995
ce9b2b0a
RW
996 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 }
0d2157f7
LT
1002
1003 return err;
1da177e4
LT
1004}
1005
1006int generic_ide_ioctl(ide_drive_t *drive, struct file *file, struct block_device *bdev,
1007 unsigned int cmd, unsigned long arg)
1008{
1497943e 1009 unsigned long flags;
1da177e4 1010 ide_driver_t *drv;
1da177e4 1011 void __user *p = (void __user *)arg;
1985026d 1012 int err = 0, (*setfunc)(ide_drive_t *, int);
1497943e 1013 u8 *val;
1da177e4 1014
1497943e
BZ
1015 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;
1da177e4 1025 }
1da177e4
LT
1026
1027 switch (cmd) {
1da177e4
LT
1028 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];
fd9bb539 1079 if (ide_register_hw(&hw, NULL, 0, NULL) == -1)
1da177e4
LT
1080 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
913759ac
AC
1115 if (HWGROUP(drive)->resetting) {
1116 spin_unlock_irqrestore(&ide_lock, flags);
1117 return -EBUSY;
1118 }
1119
1da177e4
LT
1120 ide_abort(drive, "drive reset");
1121
125e1874 1122 BUG_ON(HWGROUP(drive)->handler);
1da177e4
LT
1123
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
1da177e4
LT
1134 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 }
1497943e
BZ
1150
1151read_val:
f9383c42 1152 mutex_lock(&ide_setting_mtx);
1497943e
BZ
1153 spin_lock_irqsave(&ide_lock, flags);
1154 err = *val;
1155 spin_unlock_irqrestore(&ide_lock, flags);
f9383c42 1156 mutex_unlock(&ide_setting_mtx);
1497943e
BZ
1157 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 {
f9383c42 1166 mutex_lock(&ide_setting_mtx);
1497943e 1167 err = setfunc(drive, arg);
f9383c42 1168 mutex_unlock(&ide_setting_mtx);
1497943e
BZ
1169 }
1170 }
1171 return err;
1da177e4
LT
1172}
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
84913882 1241extern int probe_ali14xx;
1da177e4
LT
1242extern int ali14xx_init(void);
1243#endif
1244#ifdef CONFIG_BLK_DEV_UMC8672
84913882 1245extern int probe_umc8672;
1da177e4
LT
1246extern int umc8672_init(void);
1247#endif
1248#ifdef CONFIG_BLK_DEV_DTC2278
84913882 1249extern int probe_dtc2278;
1da177e4
LT
1250extern int dtc2278_init(void);
1251#endif
1252#ifdef CONFIG_BLK_DEV_HT6560B
84913882 1253extern int probe_ht6560b;
1da177e4
LT
1254extern int ht6560b_init(void);
1255#endif
1256#ifdef CONFIG_BLK_DEV_QD65XX
84913882 1257extern int probe_qd65xx;
1da177e4
LT
1258extern 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;
c223701c 1301 goto obsolete_option;
1da177e4
LT
1302 }
1303
6d208b39 1304#ifdef CONFIG_IDEPCI_PCIBUS_ORDER
1da177e4
LT
1305 if (!strcmp(s, "ide=reverse")) {
1306 ide_scan_direction = 1;
1307 printk(" : Enabled support for IDE inverse scan order.\n");
1308 return 1;
1309 }
6d208b39 1310#endif
1da177e4 1311
e3a59b4d
HR
1312#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
1da177e4
LT
1330 /*
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[] = {
c223701c 1335 "none", "noprobe", "nowerr", "cdrom", "nodma",
1da177e4 1336 "autotune", "noautotune", "minus8", "swapdata", "bswap",
36193484 1337 "noflush", "remap", "remap63", "scsi", NULL };
1da177e4
LT
1338 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;
c223701c
BZ
1363 case -5: /* nodma */
1364 drive->nodma = 1;
1365 goto done;
1da177e4
LT
1366 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;
36193484
JA
1376 case -11: /* noflush */
1377 drive->noflush = 1;
1378 goto done;
1da177e4
LT
1379 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[] = {
b6209a90 1426 "noprobe", "serialize", "minus3", "minus4",
1b516940 1427 "reset", "minus6", "ata66", "minus8", "minus9",
1da177e4
LT
1428 "minus10", "four", "qd65xx", "ht6560b", "cmd640_vlb",
1429 "dtc2278", "umc8672", "ali14xx", NULL };
9239b333
BZ
1430
1431 hw_regs_t hwregs;
1432
1da177e4
LT
1433 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));
b6209a90
BZ
1500 hwif->mate = mate;
1501 mate->mate = hwif;
1502 hwif->serialized = mate->serialized = 1;
1503 goto obsolete_option;
1da177e4
LT
1504 }
1505#endif /* CONFIG_BLK_DEV_4DRIVES */
1506 case -10: /* minus10 */
1507 case -9: /* minus9 */
1508 case -8: /* minus8 */
1b516940 1509 case -6:
b6209a90
BZ
1510 case -4:
1511 case -3:
1da177e4
LT
1512 goto bad_option;
1513 case -7: /* ata66 */
1514#ifdef CONFIG_BLK_DEV_IDEPCI
49521f97
BZ
1515 /*
1516 * Use ATA_CBL_PATA40_SHORT so drive side
1517 * cable detection is also overriden.
1518 */
1519 hwif->cbl = ATA_CBL_PATA40_SHORT;
1da177e4
LT
1520 goto obsolete_option;
1521#else
1522 goto bad_hwif;
1523#endif
1da177e4
LT
1524 case -5: /* "reset" */
1525 hwif->reset = 1;
1526 goto obsolete_option;
1da177e4 1527 case -2: /* "serialize" */
1da177e4
LT
1528 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 */
9239b333
BZ
1542 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));
1da177e4
LT
1545 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
eb797227
AV
1569extern void __init pnpide_init(void);
1570extern void __exit pnpide_exit(void);
1571extern void __init h8300_ide_init(void);
1da177e4
LT
1572
1573/*
1574 * probe_for_hwifs() finds/initializes "known" IDE interfaces
1575 */
1576static void __init probe_for_hwifs (void)
1577{
6d208b39 1578#ifdef CONFIG_IDEPCI_PCIBUS_ORDER
1da177e4 1579 ide_scan_pcibus(ide_scan_direction);
6d208b39 1580#endif
1da177e4
LT
1581
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 {
9e5755bc
AM
1596 extern int pmac_ide_probe(void);
1597 (void)pmac_ide_probe();
1da177e4
LT
1598 }
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
1da177e4
LT
1638/*
1639 * Probe module
1640 */
1641
1642EXPORT_SYMBOL(ide_lock);
1643
8604affd
BZ
1644static int ide_bus_match(struct device *dev, struct device_driver *drv)
1645{
1646 return 1;
1647}
1648
263756ec
KS
1649static 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";
a7a832de
DK
1660 case ide_optical:
1661 return "optical";
263756ec
KS
1662 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
e11b9035
BZ
1685static 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
263756ec
KS
1706static struct device_attribute ide_dev_attrs[] = {
1707 __ATTR_RO(media),
1708 __ATTR_RO(drivename),
1709 __ATTR_RO(modalias),
e11b9035
BZ
1710 __ATTR_RO(model),
1711 __ATTR_RO(firmware),
1712 __ATTR(serial, 0400, serial_show, NULL),
263756ec
KS
1713 __ATTR_NULL
1714};
1715
7eff2e7a 1716static int ide_uevent(struct device *dev, struct kobj_uevent_env *env)
263756ec
KS
1717{
1718 ide_drive_t *drive = to_ide_device(dev);
7eff2e7a
KS
1719
1720 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));
263756ec
KS
1723 return 0;
1724}
1725
4031bbe4
RK
1726static 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
1da177e4
LT
1754struct bus_type ide_bus_type = {
1755 .name = "ide",
8604affd 1756 .match = ide_bus_match,
263756ec 1757 .uevent = ide_uevent,
4031bbe4
RK
1758 .probe = generic_ide_probe,
1759 .remove = generic_ide_remove,
1760 .shutdown = generic_ide_shutdown,
263756ec 1761 .dev_attrs = ide_dev_attrs,
1da177e4
LT
1762 .suspend = generic_ide_suspend,
1763 .resume = generic_ide_resume,
1764};
1765
8604affd
BZ
1766EXPORT_SYMBOL_GPL(ide_bus_type);
1767
1da177e4
LT
1768/*
1769 * This is gets invoked once during initialization, to set *everything* up
1770 */
1771static int __init ide_init(void)
1772{
349ae23f
RD
1773 int ret;
1774
1da177e4 1775 printk(KERN_INFO "Uniform Multi-Platform E-IDE driver " REVISION "\n");
1da177e4
LT
1776 system_bus_speed = ide_system_bus_speed();
1777
349ae23f
RD
1778 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 }
1da177e4
LT
1783
1784 init_ide_data();
1785
5cbf79cd 1786 proc_ide_create();
1da177e4
LT
1787
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
1da177e4
LT
1809 /* Probe for special PCI and other "known" interface chipsets. */
1810 probe_for_hwifs();
1da177e4 1811
1da177e4
LT
1812 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
fbd8ad30 1834int __init init_module (void)
1da177e4
LT
1835{
1836 parse_options(options);
1837 return ide_init();
1838}
1839
eb797227 1840void __exit cleanup_module (void)
1da177e4
LT
1841{
1842 int index;
1843
1844 for (index = 0; index < MAX_HWIFS; ++index)
1845 ide_unregister(index);
1846
6855036a
TH
1847#ifdef CONFIG_BLK_DEV_IDEPNP
1848 pnpide_exit();
1849#endif
1850
1da177e4 1851 proc_ide_destroy();
1da177e4
LT
1852
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 */