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 *
19 * Primary:    ide0, port 0x1f0; major=3;  hda is minor=0; hdb is minor=64
20 * Secondary:  ide1, port 0x170; major=22; hdc is minor=0; hdd is minor=64
21 * Tertiary:   ide2, port 0x???; major=33; hde is minor=0; hdf is minor=64
22 * Quaternary: ide3, port 0x???; major=34; hdg is minor=0; hdh is minor=64
23 * ...
24 *
25 *  From hd.c:
26 *  |
27 *  | It traverses the request-list, using interrupts to jump between functions.
28 *  | As nearly all functions can be called within interrupts, we may not sleep.
29 *  | Special care is recommended.  Have Fun!
30 *  |
31 *  | modified by Drew Eckhardt to check nr of hd's from the CMOS.
32 *  |
33 *  | Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
34 *  | in the early extended-partition checks and added DM partitions.
35 *  |
36 *  | Early work on error handling by Mika Liljeberg (liljeber@cs.Helsinki.FI).
37 *  |
38 *  | IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
39 *  | and general streamlining by Mark Lord (mlord@pobox.com).
40 *
41 *  October, 1994 -- Complete line-by-line overhaul for linux 1.1.x, by:
42 *
43 *	Mark Lord	(mlord@pobox.com)		(IDE Perf.Pkg)
44 *	Delman Lee	(delman@ieee.org)		("Mr. atdisk2")
45 *	Scott Snyder	(snyder@fnald0.fnal.gov)	(ATAPI IDE cd-rom)
46 *
47 *  This was a rewrite of just about everything from hd.c, though some original
48 *  code is still sprinkled about.  Think of it as a major evolution, with
49 *  inspiration from lots of linux users, esp.  hamish@zot.apana.org.au
50 *
51 *  Version 1.0 ALPHA	initial code, primary i/f working okay
52 *  Version 1.3 BETA	dual i/f on shared irq tested & working!
53 *  Version 1.4 BETA	added auto probing for irq(s)
54 *  Version 1.5 BETA	added ALPHA (untested) support for IDE cd-roms,
55 *  ...
56 * Version 5.50		allow values as small as 20 for idebus=
57 * Version 5.51		force non io_32bit in drive_cmd_intr()
58 *			change delay_10ms() to delay_50ms() to fix problems
59 * Version 5.52		fix incorrect invalidation of removable devices
60 *			add "hdx=slow" command line option
61 * Version 5.60		start to modularize the driver; the disk and ATAPI
62 *			 drivers can be compiled as loadable modules.
63 *			move IDE probe code to ide-probe.c
64 *			move IDE disk code to ide-disk.c
65 *			add support for generic IDE device subdrivers
66 *			add m68k code from Geert Uytterhoeven
67 *			probe all interfaces by default
68 *			add ioctl to (re)probe an interface
69 * Version 6.00		use per device request queues
70 *			attempt to optimize shared hwgroup performance
71 *			add ioctl to manually adjust bandwidth algorithms
72 *			add kerneld support for the probe module
73 *			fix bug in ide_error()
74 *			fix bug in the first ide_get_lock() call for Atari
75 *			don't flush leftover data for ATAPI devices
76 * Version 6.01		clear hwgroup->active while the hwgroup sleeps
77 *			support HDIO_GETGEO for floppies
78 * Version 6.02		fix ide_ack_intr() call
79 *			check partition table on floppies
80 * Version 6.03		handle bad status bit sequencing in ide_wait_stat()
81 * Version 6.10		deleted old entries from this list of updates
82 *			replaced triton.c with ide-dma.c generic PCI DMA
83 *			added support for BIOS-enabled UltraDMA
84 *			rename all "promise" things to "pdc4030"
85 *			fix EZ-DRIVE handling on small disks
86 * Version 6.11		fix probe error in ide_scan_devices()
87 *			fix ancient "jiffies" polling bugs
88 *			mask all hwgroup interrupts on each irq entry
89 * Version 6.12		integrate ioctl and proc interfaces
90 *			fix parsing of "idex=" command line parameter
91 * Version 6.13		add support for ide4/ide5 courtesy rjones@orchestream.com
92 * Version 6.14		fixed IRQ sharing among PCI devices
93 * Version 6.15		added SMP awareness to IDE drivers
94 * Version 6.16		fixed various bugs; even more SMP friendly
95 * Version 6.17		fix for newest EZ-Drive problem
96 * Version 6.18		default unpartitioned-disk translation now "BIOS LBA"
97 * Version 6.19		Re-design for a UNIFORM driver for all platforms,
98 *			  model based on suggestions from Russell King and
99 *			  Geert Uytterhoeven
100 *			Promise DC4030VL now supported.
101 *			add support for ide6/ide7
102 *			delay_50ms() changed to ide_delay_50ms() and exported.
103 * Version 6.20		Added/Fixed Generic ATA-66 support and hwif detection.
104 *			Added hdx=flash to allow for second flash disk
105 *			  detection w/o the hang loop.
106 *			Added support for ide8/ide9
107 *			Added idex=ata66 for the quirky chipsets that are
108 *			  ATA-66 compliant, but have yet to determine a method
109 *			  of verification of the 80c cable presence.
110 *			  Specifically Promise's PDC20262 chipset.
111 * Version 6.21		Fixing/Fixed SMP spinlock issue with insight from an old
112 *			  hat that clarified original low level driver design.
113 * Version 6.30		Added SMP support; fixed multmode issues.  -ml
114 * Version 6.31		Debug Share INTR's and request queue streaming
115 *			Native ATA-100 support
116 *			Prep for Cascades Project
117 * Version 7.00alpha	First named revision of ide rearrange
118 *
119 *  Some additional driver compile-time options are in ./include/linux/ide.h
120 *
121 *  To do, in likely order of completion:
122 *	- modify kernel to obtain BIOS geometry for drives on 2nd/3rd/4th i/f
123 *
124 */
125
126#define	REVISION	"Revision: 7.00alpha2"
127#define	VERSION		"Id: ide.c 7.00a2 20020906"
128
129#define _IDE_C			/* Tell ide.h it's really us */
130
131#include <linux/module.h>
132#include <linux/types.h>
133#include <linux/string.h>
134#include <linux/kernel.h>
135#include <linux/timer.h>
136#include <linux/mm.h>
137#include <linux/interrupt.h>
138#include <linux/major.h>
139#include <linux/errno.h>
140#include <linux/genhd.h>
141#include <linux/blkpg.h>
142#include <linux/slab.h>
143#include <linux/init.h>
144#include <linux/pci.h>
145#include <linux/delay.h>
146#include <linux/ide.h>
147#include <linux/completion.h>
148#include <linux/reboot.h>
149#include <linux/cdrom.h>
150#include <linux/seq_file.h>
151#include <linux/device.h>
152#include <linux/bitops.h>
153
154#include <asm/byteorder.h>
155#include <asm/irq.h>
156#include <asm/uaccess.h>
157#include <asm/io.h>
158
159
160/* default maximum number of failures */
161#define IDE_DEFAULT_MAX_FAILURES 	1
162
163static const u8 ide_hwif_to_major[] = { IDE0_MAJOR, IDE1_MAJOR,
164					IDE2_MAJOR, IDE3_MAJOR,
165					IDE4_MAJOR, IDE5_MAJOR,
166					IDE6_MAJOR, IDE7_MAJOR,
167					IDE8_MAJOR, IDE9_MAJOR };
168
169static int idebus_parameter;	/* holds the "idebus=" parameter */
170static int system_bus_speed;	/* holds what we think is VESA/PCI bus speed */
171
172DECLARE_MUTEX(ide_cfg_sem);
173 __cacheline_aligned_in_smp DEFINE_SPINLOCK(ide_lock);
174
175#ifdef CONFIG_IDEPCI_PCIBUS_ORDER
176static int ide_scan_direction; /* THIS was formerly 2.2.x pci=reverse */
177#endif
178
179int noautodma = 0;
180
181EXPORT_SYMBOL(noautodma);
182
183#ifdef CONFIG_BLK_DEV_IDEACPI
184int ide_noacpi = 0;
185int ide_noacpitfs = 1;
186int ide_noacpionboot = 1;
187#endif
188
189/*
190 * This is declared extern in ide.h, for access by other IDE modules:
191 */
192ide_hwif_t ide_hwifs[MAX_HWIFS];	/* master data repository */
193
194EXPORT_SYMBOL(ide_hwifs);
195
196/*
197 * Do not even *think* about calling this!
198 */
199static void init_hwif_data(ide_hwif_t *hwif, unsigned int index)
200{
201	unsigned int unit;
202
203	/* bulk initialize hwif & drive info with zeros */
204	memset(hwif, 0, sizeof(ide_hwif_t));
205
206	/* fill in any non-zero initial values */
207	hwif->index	= index;
208	hwif->major	= ide_hwif_to_major[index];
209
210	hwif->name[0]	= 'i';
211	hwif->name[1]	= 'd';
212	hwif->name[2]	= 'e';
213	hwif->name[3]	= '0' + index;
214
215	hwif->bus_state	= BUSSTATE_ON;
216
217	hwif->atapi_dma = 0;		/* disable all atapi dma */
218
219	init_completion(&hwif->gendev_rel_comp);
220
221	default_hwif_iops(hwif);
222	default_hwif_transport(hwif);
223	for (unit = 0; unit < MAX_DRIVES; ++unit) {
224		ide_drive_t *drive = &hwif->drives[unit];
225
226		drive->media			= ide_disk;
227		drive->select.all		= (unit<<4)|0xa0;
228		drive->hwif			= hwif;
229		drive->ctl			= 0x08;
230		drive->ready_stat		= READY_STAT;
231		drive->bad_wstat		= BAD_W_STAT;
232		drive->special.b.recalibrate	= 1;
233		drive->special.b.set_geometry	= 1;
234		drive->name[0]			= 'h';
235		drive->name[1]			= 'd';
236		drive->name[2]			= 'a' + (index * MAX_DRIVES) + unit;
237		drive->max_failures		= IDE_DEFAULT_MAX_FAILURES;
238		drive->using_dma		= 0;
239		drive->vdma			= 0;
240		INIT_LIST_HEAD(&drive->list);
241		init_completion(&drive->gendev_rel_comp);
242	}
243}
244
245static void init_hwif_default(ide_hwif_t *hwif, unsigned int index)
246{
247	hw_regs_t hw;
248
249	memset(&hw, 0, sizeof(hw_regs_t));
250
251	ide_init_hwif_ports(&hw, ide_default_io_base(index), 0, &hwif->irq);
252
253	memcpy(&hwif->hw, &hw, sizeof(hw));
254	memcpy(hwif->io_ports, hw.io_ports, sizeof(hw.io_ports));
255
256	hwif->noprobe = !hwif->io_ports[IDE_DATA_OFFSET];
257#ifdef CONFIG_BLK_DEV_HD
258	if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA)
259		hwif->noprobe = 1;	/* may be overridden by ide_setup() */
260#endif
261}
262
263extern void ide_arm_init(void);
264
265#define MAGIC_COOKIE 0x12345678
266static void __init init_ide_data (void)
267{
268	ide_hwif_t *hwif;
269	unsigned int index;
270	static unsigned long magic_cookie = MAGIC_COOKIE;
271
272	if (magic_cookie != MAGIC_COOKIE)
273		return;		/* already initialized */
274	magic_cookie = 0;
275
276	/* Initialise all interface structures */
277	for (index = 0; index < MAX_HWIFS; ++index) {
278		hwif = &ide_hwifs[index];
279		init_hwif_data(hwif, index);
280		init_hwif_default(hwif, index);
281#if !defined(CONFIG_PPC32) || !defined(CONFIG_PCI)
282		hwif->irq = hwif->hw.irq =
283			ide_init_default_irq(hwif->io_ports[IDE_DATA_OFFSET]);
284#endif
285	}
286#ifdef CONFIG_IDE_ARM
287	ide_arm_init();
288#endif
289}
290
291/**
292 *	ide_system_bus_speed	-	guess bus speed
293 *
294 *	ide_system_bus_speed() returns what we think is the system VESA/PCI
295 *	bus speed (in MHz). This is used for calculating interface PIO timings.
296 *	The default is 40 for known PCI systems, 50 otherwise.
297 *	The "idebus=xx" parameter can be used to override this value.
298 *	The actual value to be used is computed/displayed the first time
299 *	through. Drivers should only use this as a last resort.
300 *
301 *	Returns a guessed speed in MHz.
302 */
303
304static int ide_system_bus_speed(void)
305{
306#ifdef CONFIG_PCI
307	static struct pci_device_id pci_default[] = {
308		{ PCI_DEVICE(PCI_ANY_ID, PCI_ANY_ID) },
309		{ }
310	};
311#else
312#define pci_default 0
313#endif /* CONFIG_PCI */
314
315	if (!system_bus_speed) {
316		if (idebus_parameter) {
317			/* user supplied value */
318			system_bus_speed = idebus_parameter;
319		} else if (pci_dev_present(pci_default)) {
320			/* safe default value for PCI */
321			system_bus_speed = 33;
322		} else {
323			/* safe default value for VESA and PCI */
324			system_bus_speed = 50;
325		}
326		printk(KERN_INFO "ide: Assuming %dMHz system bus speed "
327			"for PIO modes%s\n", system_bus_speed,
328			idebus_parameter ? "" : "; override with idebus=xx");
329	}
330	return system_bus_speed;
331}
332
333static struct resource* hwif_request_region(ide_hwif_t *hwif,
334					    unsigned long addr, int num)
335{
336	struct resource *res = request_region(addr, num, hwif->name);
337
338	if (!res)
339		printk(KERN_ERR "%s: I/O resource 0x%lX-0x%lX not free.\n",
340				hwif->name, addr, addr+num-1);
341	return res;
342}
343
344/**
345 *	ide_hwif_request_regions - request resources for IDE
346 *	@hwif: interface to use
347 *
348 *	Requests all the needed resources for an interface.
349 *	Right now core IDE code does this work which is deeply wrong.
350 *	MMIO leaves it to the controller driver,
351 *	PIO will migrate this way over time.
352 */
353
354int ide_hwif_request_regions(ide_hwif_t *hwif)
355{
356	unsigned long addr;
357	unsigned int i;
358
359	if (hwif->mmio)
360		return 0;
361	addr = hwif->io_ports[IDE_CONTROL_OFFSET];
362	if (addr && !hwif_request_region(hwif, addr, 1))
363		goto control_region_busy;
364	hwif->straight8 = 0;
365	addr = hwif->io_ports[IDE_DATA_OFFSET];
366	if ((addr | 7) == hwif->io_ports[IDE_STATUS_OFFSET]) {
367		if (!hwif_request_region(hwif, addr, 8))
368			goto data_region_busy;
369		hwif->straight8 = 1;
370		return 0;
371	}
372	for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++) {
373		addr = hwif->io_ports[i];
374		if (!hwif_request_region(hwif, addr, 1)) {
375			while (--i)
376				release_region(addr, 1);
377			goto data_region_busy;
378		}
379	}
380	return 0;
381
382data_region_busy:
383	addr = hwif->io_ports[IDE_CONTROL_OFFSET];
384	if (addr)
385		release_region(addr, 1);
386control_region_busy:
387	/* If any errors are return, we drop the hwif interface. */
388	return -EBUSY;
389}
390
391/**
392 *	ide_hwif_release_regions - free IDE resources
393 *
394 *	Note that we only release the standard ports,
395 *	and do not even try to handle any extra ports
396 *	allocated for weird IDE interface chipsets.
397 *
398 *	Note also that we don't yet handle mmio resources here. More
399 *	importantly our caller should be doing this so we need to
400 *	restructure this as a helper function for drivers.
401 */
402
403void ide_hwif_release_regions(ide_hwif_t *hwif)
404{
405	u32 i = 0;
406
407	if (hwif->mmio)
408		return;
409	if (hwif->io_ports[IDE_CONTROL_OFFSET])
410		release_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
411	if (hwif->straight8) {
412		release_region(hwif->io_ports[IDE_DATA_OFFSET], 8);
413		return;
414	}
415	for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++)
416		if (hwif->io_ports[i])
417			release_region(hwif->io_ports[i], 1);
418}
419
420/**
421 *	ide_hwif_restore	-	restore hwif to template
422 *	@hwif: hwif to update
423 *	@tmp_hwif: template
424 *
425 *	Restore hwif to a previous state by copying most settings
426 *	from the template.
427 */
428
429static void ide_hwif_restore(ide_hwif_t *hwif, ide_hwif_t *tmp_hwif)
430{
431	hwif->hwgroup			= tmp_hwif->hwgroup;
432
433	hwif->gendev.parent		= tmp_hwif->gendev.parent;
434
435	hwif->proc			= tmp_hwif->proc;
436
437	hwif->major			= tmp_hwif->major;
438	hwif->straight8			= tmp_hwif->straight8;
439	hwif->bus_state			= tmp_hwif->bus_state;
440
441	hwif->atapi_dma			= tmp_hwif->atapi_dma;
442	hwif->ultra_mask		= tmp_hwif->ultra_mask;
443	hwif->mwdma_mask		= tmp_hwif->mwdma_mask;
444	hwif->swdma_mask		= tmp_hwif->swdma_mask;
445
446	hwif->chipset			= tmp_hwif->chipset;
447	hwif->hold			= tmp_hwif->hold;
448
449#ifdef CONFIG_BLK_DEV_IDEPCI
450	hwif->pci_dev			= tmp_hwif->pci_dev;
451	hwif->cds			= tmp_hwif->cds;
452#endif
453
454	hwif->tuneproc			= tmp_hwif->tuneproc;
455	hwif->speedproc			= tmp_hwif->speedproc;
456	hwif->udma_filter		= tmp_hwif->udma_filter;
457	hwif->selectproc		= tmp_hwif->selectproc;
458	hwif->reset_poll		= tmp_hwif->reset_poll;
459	hwif->pre_reset			= tmp_hwif->pre_reset;
460	hwif->resetproc			= tmp_hwif->resetproc;
461	hwif->intrproc			= tmp_hwif->intrproc;
462	hwif->maskproc			= tmp_hwif->maskproc;
463	hwif->quirkproc			= tmp_hwif->quirkproc;
464	hwif->busproc			= tmp_hwif->busproc;
465
466	hwif->ata_input_data		= tmp_hwif->ata_input_data;
467	hwif->ata_output_data		= tmp_hwif->ata_output_data;
468	hwif->atapi_input_bytes		= tmp_hwif->atapi_input_bytes;
469	hwif->atapi_output_bytes	= tmp_hwif->atapi_output_bytes;
470
471	hwif->dma_setup			= tmp_hwif->dma_setup;
472	hwif->dma_exec_cmd		= tmp_hwif->dma_exec_cmd;
473	hwif->dma_start			= tmp_hwif->dma_start;
474	hwif->ide_dma_end		= tmp_hwif->ide_dma_end;
475	hwif->ide_dma_check		= tmp_hwif->ide_dma_check;
476	hwif->ide_dma_on		= tmp_hwif->ide_dma_on;
477	hwif->dma_off_quietly		= tmp_hwif->dma_off_quietly;
478	hwif->ide_dma_test_irq		= tmp_hwif->ide_dma_test_irq;
479	hwif->ide_dma_clear_irq		= tmp_hwif->ide_dma_clear_irq;
480	hwif->dma_host_on		= tmp_hwif->dma_host_on;
481	hwif->dma_host_off		= tmp_hwif->dma_host_off;
482	hwif->ide_dma_lostirq		= tmp_hwif->ide_dma_lostirq;
483	hwif->ide_dma_timeout		= tmp_hwif->ide_dma_timeout;
484
485	hwif->OUTB			= tmp_hwif->OUTB;
486	hwif->OUTBSYNC			= tmp_hwif->OUTBSYNC;
487	hwif->OUTW			= tmp_hwif->OUTW;
488	hwif->OUTSW			= tmp_hwif->OUTSW;
489	hwif->OUTSL			= tmp_hwif->OUTSL;
490
491	hwif->INB			= tmp_hwif->INB;
492	hwif->INW			= tmp_hwif->INW;
493	hwif->INSW			= tmp_hwif->INSW;
494	hwif->INSL			= tmp_hwif->INSL;
495
496	hwif->sg_max_nents		= tmp_hwif->sg_max_nents;
497
498	hwif->mmio			= tmp_hwif->mmio;
499	hwif->rqsize			= tmp_hwif->rqsize;
500	hwif->no_lba48			= tmp_hwif->no_lba48;
501
502#ifndef CONFIG_BLK_DEV_IDECS
503	hwif->irq			= tmp_hwif->irq;
504#endif
505
506	hwif->dma_base			= tmp_hwif->dma_base;
507	hwif->dma_master		= tmp_hwif->dma_master;
508	hwif->dma_command		= tmp_hwif->dma_command;
509	hwif->dma_vendor1		= tmp_hwif->dma_vendor1;
510	hwif->dma_status		= tmp_hwif->dma_status;
511	hwif->dma_vendor3		= tmp_hwif->dma_vendor3;
512	hwif->dma_prdtable		= tmp_hwif->dma_prdtable;
513
514	hwif->config_data		= tmp_hwif->config_data;
515	hwif->select_data		= tmp_hwif->select_data;
516	hwif->extra_base		= tmp_hwif->extra_base;
517	hwif->extra_ports		= tmp_hwif->extra_ports;
518	hwif->autodma			= tmp_hwif->autodma;
519	hwif->udma_four			= tmp_hwif->udma_four;
520
521	hwif->hwif_data			= tmp_hwif->hwif_data;
522}
523
524/**
525 *	ide_unregister		-	free an IDE interface
526 *	@index: index of interface (will change soon to a pointer)
527 *
528 *	Perform the final unregister of an IDE interface. At the moment
529 *	we don't refcount interfaces so this will also get split up.
530 *
531 *	Locking:
532 *	The caller must not hold the IDE locks
533 *	The drive present/vanishing is not yet properly locked
534 *	Take care with the callbacks. These have been split to avoid
535 *	deadlocking the IDE layer. The shutdown callback is called
536 *	before we take the lock and free resources. It is up to the
537 *	caller to be sure there is no pending I/O here, and that
538 *	the interface will not be reopened (present/vanishing locking
539 *	isn't yet done BTW). After we commit to the final kill we
540 *	call the cleanup callback with the ide locks held.
541 *
542 *	Unregister restores the hwif structures to the default state.
543 *	This is raving bonkers.
544 */
545
546void ide_unregister(unsigned int index)
547{
548	ide_drive_t *drive;
549	ide_hwif_t *hwif, *g;
550	static ide_hwif_t tmp_hwif; /* protected by ide_cfg_sem */
551	ide_hwgroup_t *hwgroup;
552	int irq_count = 0, unit;
553
554	BUG_ON(index >= MAX_HWIFS);
555
556	BUG_ON(in_interrupt());
557	BUG_ON(irqs_disabled());
558	down(&ide_cfg_sem);
559	spin_lock_irq(&ide_lock);
560	hwif = &ide_hwifs[index];
561	if (!hwif->present)
562		goto abort;
563	for (unit = 0; unit < MAX_DRIVES; ++unit) {
564		drive = &hwif->drives[unit];
565		if (!drive->present)
566			continue;
567		spin_unlock_irq(&ide_lock);
568		device_unregister(&drive->gendev);
569		wait_for_completion(&drive->gendev_rel_comp);
570		spin_lock_irq(&ide_lock);
571	}
572	hwif->present = 0;
573
574	spin_unlock_irq(&ide_lock);
575
576	ide_proc_unregister_port(hwif);
577
578	hwgroup = hwif->hwgroup;
579	/*
580	 * free the irq if we were the only hwif using it
581	 */
582	g = hwgroup->hwif;
583	do {
584		if (g->irq == hwif->irq)
585			++irq_count;
586		g = g->next;
587	} while (g != hwgroup->hwif);
588	if (irq_count == 1)
589		free_irq(hwif->irq, hwgroup);
590
591	spin_lock_irq(&ide_lock);
592	/*
593	 * Note that we only release the standard ports,
594	 * and do not even try to handle any extra ports
595	 * allocated for weird IDE interface chipsets.
596	 */
597	ide_hwif_release_regions(hwif);
598
599	/*
600	 * Remove us from the hwgroup, and free
601	 * the hwgroup if we were the only member
602	 */
603	if (hwif->next == hwif) {
604		BUG_ON(hwgroup->hwif != hwif);
605		kfree(hwgroup);
606	} else {
607		/* There is another interface in hwgroup.
608		 * Unlink us, and set hwgroup->drive and ->hwif to
609		 * something sane.
610		 */
611		g = hwgroup->hwif;
612		while (g->next != hwif)
613			g = g->next;
614		g->next = hwif->next;
615		if (hwgroup->hwif == hwif) {
616			/* Chose a random hwif for hwgroup->hwif.
617			 * It's guaranteed that there are no drives
618			 * left in the hwgroup.
619			 */
620			BUG_ON(hwgroup->drive != NULL);
621			hwgroup->hwif = g;
622		}
623		BUG_ON(hwgroup->hwif == hwif);
624	}
625
626	/* More messed up locking ... */
627	spin_unlock_irq(&ide_lock);
628	device_unregister(&hwif->gendev);
629	wait_for_completion(&hwif->gendev_rel_comp);
630
631	/*
632	 * Remove us from the kernel's knowledge
633	 */
634	blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
635	kfree(hwif->sg_table);
636	unregister_blkdev(hwif->major, hwif->name);
637	spin_lock_irq(&ide_lock);
638
639	if (hwif->dma_base) {
640		(void) ide_release_dma(hwif);
641
642		hwif->dma_base = 0;
643		hwif->dma_master = 0;
644		hwif->dma_command = 0;
645		hwif->dma_vendor1 = 0;
646		hwif->dma_status = 0;
647		hwif->dma_vendor3 = 0;
648		hwif->dma_prdtable = 0;
649
650		hwif->extra_base  = 0;
651		hwif->extra_ports = 0;
652	}
653
654	/* copy original settings */
655	tmp_hwif = *hwif;
656
657	/* restore hwif data to pristine status */
658	init_hwif_data(hwif, index);
659	init_hwif_default(hwif, index);
660
661	ide_hwif_restore(hwif, &tmp_hwif);
662
663abort:
664	spin_unlock_irq(&ide_lock);
665	up(&ide_cfg_sem);
666}
667
668EXPORT_SYMBOL(ide_unregister);
669
670
671/**
672 *	ide_setup_ports 	-	set up IDE interface ports
673 *	@hw: register descriptions
674 *	@base: base register
675 *	@offsets: table of register offsets
676 *	@ctrl: control register
677 *	@ack_irq: IRQ ack
678 *	@irq: interrupt lie
679 *
680 *	Setup hw_regs_t structure described by parameters.  You
681 *	may set up the hw structure yourself OR use this routine to
682 *	do it for you. This is basically a helper
683 *
684 */
685
686void ide_setup_ports (	hw_regs_t *hw,
687			unsigned long base, int *offsets,
688			unsigned long ctrl, unsigned long intr,
689			ide_ack_intr_t *ack_intr,
690/*
691 *			ide_io_ops_t *iops,
692 */
693			int irq)
694{
695	int i;
696
697	memset(hw, 0, sizeof(hw_regs_t));
698	for (i = 0; i < IDE_NR_PORTS; i++) {
699		if (offsets[i] == -1) {
700			switch(i) {
701				case IDE_CONTROL_OFFSET:
702					hw->io_ports[i] = ctrl;
703					break;
704#if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
705				case IDE_IRQ_OFFSET:
706					hw->io_ports[i] = intr;
707					break;
708#endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
709				default:
710					hw->io_ports[i] = 0;
711					break;
712			}
713		} else {
714			hw->io_ports[i] = base + offsets[i];
715		}
716	}
717	hw->irq = irq;
718	hw->dma = NO_DMA;
719	hw->ack_intr = ack_intr;
720/*
721 *	hw->iops = iops;
722 */
723}
724
725/**
726 *	ide_register_hw_with_fixup	-	register IDE interface
727 *	@hw: hardware registers
728 *	@initializing: set while initializing built-in drivers
729 *	@hwifp: pointer to returned hwif
730 *	@fixup: fixup function
731 *
732 *	Register an IDE interface, specifying exactly the registers etc.
733 *	Set init=1 iff calling before probes have taken place.
734 *
735 *	Returns -1 on error.
736 */
737
738int ide_register_hw_with_fixup(hw_regs_t *hw, int initializing,
739			       ide_hwif_t **hwifp,
740			       void(*fixup)(ide_hwif_t *hwif))
741{
742	int index, retry = 1;
743	ide_hwif_t *hwif;
744
745	do {
746		for (index = 0; index < MAX_HWIFS; ++index) {
747			hwif = &ide_hwifs[index];
748			if (hwif->hw.io_ports[IDE_DATA_OFFSET] == hw->io_ports[IDE_DATA_OFFSET])
749				goto found;
750		}
751		for (index = 0; index < MAX_HWIFS; ++index) {
752			hwif = &ide_hwifs[index];
753			if (hwif->hold)
754				continue;
755			if ((!hwif->present && !hwif->mate && !initializing) ||
756			    (!hwif->hw.io_ports[IDE_DATA_OFFSET] && initializing))
757				goto found;
758		}
759		for (index = 0; index < MAX_HWIFS; index++)
760			ide_unregister(index);
761	} while (retry--);
762	return -1;
763found:
764	if (hwif->present)
765		ide_unregister(index);
766	else if (!hwif->hold) {
767		init_hwif_data(hwif, index);
768		init_hwif_default(hwif, index);
769	}
770	if (hwif->present)
771		return -1;
772	memcpy(&hwif->hw, hw, sizeof(*hw));
773	memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->hw.io_ports));
774	hwif->irq = hw->irq;
775	hwif->noprobe = 0;
776	hwif->chipset = hw->chipset;
777	hwif->gendev.parent = hw->dev;
778
779	if (!initializing) {
780		probe_hwif_init_with_fixup(hwif, fixup);
781		ide_proc_register_port(hwif);
782	}
783
784	if (hwifp)
785		*hwifp = hwif;
786
787	return (initializing || hwif->present) ? index : -1;
788}
789
790EXPORT_SYMBOL(ide_register_hw_with_fixup);
791
792int ide_register_hw(hw_regs_t *hw, int initializing, ide_hwif_t **hwifp)
793{
794	return ide_register_hw_with_fixup(hw, initializing, hwifp, NULL);
795}
796
797EXPORT_SYMBOL(ide_register_hw);
798
799/*
800 *	Locks for IDE setting functionality
801 */
802
803DECLARE_MUTEX(ide_setting_sem);
804
805EXPORT_SYMBOL_GPL(ide_setting_sem);
806
807/**
808 *	ide_spin_wait_hwgroup	-	wait for group
809 *	@drive: drive in the group
810 *
811 *	Wait for an IDE device group to go non busy and then return
812 *	holding the ide_lock which guards the hwgroup->busy status
813 *	and right to use it.
814 */
815
816int ide_spin_wait_hwgroup (ide_drive_t *drive)
817{
818	ide_hwgroup_t *hwgroup = HWGROUP(drive);
819	unsigned long timeout = jiffies + (3 * HZ);
820
821	spin_lock_irq(&ide_lock);
822
823	while (hwgroup->busy) {
824		unsigned long lflags;
825		spin_unlock_irq(&ide_lock);
826		local_irq_set(lflags);
827		if (time_after(jiffies, timeout)) {
828			local_irq_restore(lflags);
829			printk(KERN_ERR "%s: channel busy\n", drive->name);
830			return -EBUSY;
831		}
832		local_irq_restore(lflags);
833		spin_lock_irq(&ide_lock);
834	}
835	return 0;
836}
837
838EXPORT_SYMBOL(ide_spin_wait_hwgroup);
839
840int set_io_32bit(ide_drive_t *drive, int arg)
841{
842	if (drive->no_io_32bit)
843		return -EPERM;
844
845	if (arg < 0 || arg > 1 + (SUPPORT_VLB_SYNC << 1))
846		return -EINVAL;
847
848	drive->io_32bit = arg;
849#ifdef CONFIG_BLK_DEV_DTC2278
850	if (HWIF(drive)->chipset == ide_dtc2278)
851		HWIF(drive)->drives[!drive->select.b.unit].io_32bit = arg;
852#endif /* CONFIG_BLK_DEV_DTC2278 */
853	return 0;
854}
855
856static int set_ksettings(ide_drive_t *drive, int arg)
857{
858	if (arg < 0 || arg > 1)
859		return -EINVAL;
860
861	if (ide_spin_wait_hwgroup(drive))
862		return -EBUSY;
863	drive->keep_settings = arg;
864	spin_unlock_irq(&ide_lock);
865
866	return 0;
867}
868
869int set_using_dma(ide_drive_t *drive, int arg)
870{
871#ifdef CONFIG_BLK_DEV_IDEDMA
872	ide_hwif_t *hwif = drive->hwif;
873	int err = -EPERM;
874
875	if (arg < 0 || arg > 1)
876		return -EINVAL;
877
878	if (!drive->id || !(drive->id->capability & 1))
879		goto out;
880
881	if (hwif->ide_dma_check == NULL)
882		goto out;
883
884	err = -EBUSY;
885	if (ide_spin_wait_hwgroup(drive))
886		goto out;
887	/*
888	 * set ->busy flag, unlock and let it ride
889	 */
890	hwif->hwgroup->busy = 1;
891	spin_unlock_irq(&ide_lock);
892
893	err = 0;
894
895	if (arg) {
896		hwif->dma_off_quietly(drive);
897		if (ide_set_dma(drive) || hwif->ide_dma_on(drive))
898			err = -EIO;
899	} else
900		ide_dma_off(drive);
901
902	/*
903	 * lock, clear ->busy flag and unlock before leaving
904	 */
905	spin_lock_irq(&ide_lock);
906	hwif->hwgroup->busy = 0;
907	spin_unlock_irq(&ide_lock);
908out:
909	return err;
910#else
911	if (arg < 0 || arg > 1)
912		return -EINVAL;
913
914	return -EPERM;
915#endif
916}
917
918int set_pio_mode(ide_drive_t *drive, int arg)
919{
920	struct request rq;
921
922	if (arg < 0 || arg > 255)
923		return -EINVAL;
924
925	if (!HWIF(drive)->tuneproc)
926		return -ENOSYS;
927	if (drive->special.b.set_tune)
928		return -EBUSY;
929	ide_init_drive_cmd(&rq);
930	drive->tune_req = (u8) arg;
931	drive->special.b.set_tune = 1;
932	(void) ide_do_drive_cmd(drive, &rq, ide_wait);
933	return 0;
934}
935
936static int set_unmaskirq(ide_drive_t *drive, int arg)
937{
938	if (drive->no_unmask)
939		return -EPERM;
940
941	if (arg < 0 || arg > 1)
942		return -EINVAL;
943
944	if (ide_spin_wait_hwgroup(drive))
945		return -EBUSY;
946	drive->unmask = arg;
947	spin_unlock_irq(&ide_lock);
948
949	return 0;
950}
951
952/**
953 *	system_bus_clock	-	clock guess
954 *
955 *	External version of the bus clock guess used by very old IDE drivers
956 *	for things like VLB timings. Should not be used.
957 */
958
959int system_bus_clock (void)
960{
961	return((int) ((!system_bus_speed) ? ide_system_bus_speed() : system_bus_speed ));
962}
963
964EXPORT_SYMBOL(system_bus_clock);
965
966static int generic_ide_suspend(struct device *dev, pm_message_t mesg)
967{
968	ide_drive_t *drive = dev->driver_data;
969	ide_hwif_t *hwif = HWIF(drive);
970	struct request rq;
971	struct request_pm_state rqpm;
972	ide_task_t args;
973
974	/* Call ACPI _GTM only once */
975	if (!(drive->dn % 2))
976		ide_acpi_get_timing(hwif);
977
978	memset(&rq, 0, sizeof(rq));
979	memset(&rqpm, 0, sizeof(rqpm));
980	memset(&args, 0, sizeof(args));
981	rq.cmd_type = REQ_TYPE_PM_SUSPEND;
982	rq.special = &args;
983	rq.data = &rqpm;
984	rqpm.pm_step = ide_pm_state_start_suspend;
985	if (mesg.event == PM_EVENT_PRETHAW)
986		mesg.event = PM_EVENT_FREEZE;
987	rqpm.pm_state = mesg.event;
988
989	return ide_do_drive_cmd(drive, &rq, ide_wait);
990}
991
992static int generic_ide_resume(struct device *dev)
993{
994	ide_drive_t *drive = dev->driver_data;
995	ide_hwif_t *hwif = HWIF(drive);
996	struct request rq;
997	struct request_pm_state rqpm;
998	ide_task_t args;
999	int err;
1000
1001	/* Call ACPI _STM only once */
1002	if (!(drive->dn % 2))
1003		ide_acpi_push_timing(hwif);
1004
1005	ide_acpi_exec_tfs(drive);
1006
1007	memset(&rq, 0, sizeof(rq));
1008	memset(&rqpm, 0, sizeof(rqpm));
1009	memset(&args, 0, sizeof(args));
1010	rq.cmd_type = REQ_TYPE_PM_RESUME;
1011	rq.special = &args;
1012	rq.data = &rqpm;
1013	rqpm.pm_step = ide_pm_state_start_resume;
1014	rqpm.pm_state = PM_EVENT_ON;
1015
1016	err = ide_do_drive_cmd(drive, &rq, ide_head_wait);
1017
1018	if (err == 0 && dev->driver) {
1019		ide_driver_t *drv = to_ide_driver(dev->driver);
1020
1021		if (drv->resume)
1022			drv->resume(drive);
1023	}
1024
1025	return err;
1026}
1027
1028int generic_ide_ioctl(ide_drive_t *drive, struct file *file, struct block_device *bdev,
1029			unsigned int cmd, unsigned long arg)
1030{
1031	unsigned long flags;
1032	ide_driver_t *drv;
1033	void __user *p = (void __user *)arg;
1034	int err = 0, (*setfunc)(ide_drive_t *, int);
1035	u8 *val;
1036
1037	switch (cmd) {
1038	case HDIO_GET_32BIT:	    val = &drive->io_32bit;	 goto read_val;
1039	case HDIO_GET_KEEPSETTINGS: val = &drive->keep_settings; goto read_val;
1040	case HDIO_GET_UNMASKINTR:   val = &drive->unmask;	 goto read_val;
1041	case HDIO_GET_DMA:	    val = &drive->using_dma;	 goto read_val;
1042	case HDIO_SET_32BIT:	    setfunc = set_io_32bit;	 goto set_val;
1043	case HDIO_SET_KEEPSETTINGS: setfunc = set_ksettings;	 goto set_val;
1044	case HDIO_SET_PIO_MODE:	    setfunc = set_pio_mode;	 goto set_val;
1045	case HDIO_SET_UNMASKINTR:   setfunc = set_unmaskirq;	 goto set_val;
1046	case HDIO_SET_DMA:	    setfunc = set_using_dma;	 goto set_val;
1047	}
1048
1049	switch (cmd) {
1050		case HDIO_OBSOLETE_IDENTITY:
1051		case HDIO_GET_IDENTITY:
1052			if (bdev != bdev->bd_contains)
1053				return -EINVAL;
1054			if (drive->id_read == 0)
1055				return -ENOMSG;
1056			if (copy_to_user(p, drive->id, (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
1057				return -EFAULT;
1058			return 0;
1059
1060		case HDIO_GET_NICE:
1061			return put_user(drive->dsc_overlap	<<	IDE_NICE_DSC_OVERLAP	|
1062					drive->atapi_overlap	<<	IDE_NICE_ATAPI_OVERLAP	|
1063					drive->nice0		<< 	IDE_NICE_0		|
1064					drive->nice1		<<	IDE_NICE_1		|
1065					drive->nice2		<<	IDE_NICE_2,
1066					(long __user *) arg);
1067
1068#ifdef CONFIG_IDE_TASK_IOCTL
1069		case HDIO_DRIVE_TASKFILE:
1070		        if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1071				return -EACCES;
1072			switch(drive->media) {
1073				case ide_disk:
1074					return ide_taskfile_ioctl(drive, cmd, arg);
1075				default:
1076					return -ENOMSG;
1077			}
1078#endif /* CONFIG_IDE_TASK_IOCTL */
1079
1080		case HDIO_DRIVE_CMD:
1081			if (!capable(CAP_SYS_RAWIO))
1082				return -EACCES;
1083			return ide_cmd_ioctl(drive, cmd, arg);
1084
1085		case HDIO_DRIVE_TASK:
1086			if (!capable(CAP_SYS_RAWIO))
1087				return -EACCES;
1088			return ide_task_ioctl(drive, cmd, arg);
1089
1090		case HDIO_SCAN_HWIF:
1091		{
1092			hw_regs_t hw;
1093			int args[3];
1094			if (!capable(CAP_SYS_RAWIO)) return -EACCES;
1095			if (copy_from_user(args, p, 3 * sizeof(int)))
1096				return -EFAULT;
1097			memset(&hw, 0, sizeof(hw));
1098			ide_init_hwif_ports(&hw, (unsigned long) args[0],
1099					    (unsigned long) args[1], NULL);
1100			hw.irq = args[2];
1101			if (ide_register_hw(&hw, 0, NULL) == -1)
1102				return -EIO;
1103			return 0;
1104		}
1105	        case HDIO_UNREGISTER_HWIF:
1106			if (!capable(CAP_SYS_RAWIO)) return -EACCES;
1107			/* (arg > MAX_HWIFS) checked in function */
1108			ide_unregister(arg);
1109			return 0;
1110		case HDIO_SET_NICE:
1111			if (!capable(CAP_SYS_ADMIN)) return -EACCES;
1112			if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
1113				return -EPERM;
1114			drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
1115			drv = *(ide_driver_t **)bdev->bd_disk->private_data;
1116			if (drive->dsc_overlap && !drv->supports_dsc_overlap) {
1117				drive->dsc_overlap = 0;
1118				return -EPERM;
1119			}
1120			drive->nice1 = (arg >> IDE_NICE_1) & 1;
1121			return 0;
1122		case HDIO_DRIVE_RESET:
1123		{
1124			unsigned long flags;
1125			if (!capable(CAP_SYS_ADMIN)) return -EACCES;
1126
1127			/*
1128			 *	Abort the current command on the
1129			 *	group if there is one, taking
1130			 *	care not to allow anything else
1131			 *	to be queued and to die on the
1132			 *	spot if we miss one somehow
1133			 */
1134
1135			spin_lock_irqsave(&ide_lock, flags);
1136
1137			if (HWGROUP(drive)->resetting) {
1138				spin_unlock_irqrestore(&ide_lock, flags);
1139				return -EBUSY;
1140			}
1141
1142			ide_abort(drive, "drive reset");
1143
1144			BUG_ON(HWGROUP(drive)->handler);
1145
1146			/* Ensure nothing gets queued after we
1147			   drop the lock. Reset will clear the busy */
1148
1149			HWGROUP(drive)->busy = 1;
1150			spin_unlock_irqrestore(&ide_lock, flags);
1151			(void) ide_do_reset(drive);
1152
1153			return 0;
1154		}
1155
1156		case CDROMEJECT:
1157		case CDROMCLOSETRAY:
1158			return scsi_cmd_ioctl(file, bdev->bd_disk, cmd, p);
1159
1160		case HDIO_GET_BUSSTATE:
1161			if (!capable(CAP_SYS_ADMIN))
1162				return -EACCES;
1163			if (put_user(HWIF(drive)->bus_state, (long __user *)arg))
1164				return -EFAULT;
1165			return 0;
1166
1167		case HDIO_SET_BUSSTATE:
1168			if (!capable(CAP_SYS_ADMIN))
1169				return -EACCES;
1170			if (HWIF(drive)->busproc)
1171				return HWIF(drive)->busproc(drive, (int)arg);
1172			return -EOPNOTSUPP;
1173		default:
1174			return -EINVAL;
1175	}
1176
1177read_val:
1178	down(&ide_setting_sem);
1179	spin_lock_irqsave(&ide_lock, flags);
1180	err = *val;
1181	spin_unlock_irqrestore(&ide_lock, flags);
1182	up(&ide_setting_sem);
1183	return err >= 0 ? put_user(err, (long __user *)arg) : err;
1184
1185set_val:
1186	if (bdev != bdev->bd_contains)
1187		err = -EINVAL;
1188	else {
1189		if (!capable(CAP_SYS_ADMIN))
1190			err = -EACCES;
1191		else {
1192			down(&ide_setting_sem);
1193			err = setfunc(drive, arg);
1194			up(&ide_setting_sem);
1195		}
1196	}
1197	return err;
1198}
1199
1200EXPORT_SYMBOL(generic_ide_ioctl);
1201
1202/*
1203 * stridx() returns the offset of c within s,
1204 * or -1 if c is '\0' or not found within s.
1205 */
1206static int __init stridx (const char *s, char c)
1207{
1208	char *i = strchr(s, c);
1209	return (i && c) ? i - s : -1;
1210}
1211
1212/*
1213 * match_parm() does parsing for ide_setup():
1214 *
1215 * 1. the first char of s must be '='.
1216 * 2. if the remainder matches one of the supplied keywords,
1217 *     the index (1 based) of the keyword is negated and returned.
1218 * 3. if the remainder is a series of no more than max_vals numbers
1219 *     separated by commas, the numbers are saved in vals[] and a
1220 *     count of how many were saved is returned.  Base10 is assumed,
1221 *     and base16 is allowed when prefixed with "0x".
1222 * 4. otherwise, zero is returned.
1223 */
1224static int __init match_parm (char *s, const char *keywords[], int vals[], int max_vals)
1225{
1226	static const char *decimal = "0123456789";
1227	static const char *hex = "0123456789abcdef";
1228	int i, n;
1229
1230	if (*s++ == '=') {
1231		/*
1232		 * Try matching against the supplied keywords,
1233		 * and return -(index+1) if we match one
1234		 */
1235		if (keywords != NULL) {
1236			for (i = 0; *keywords != NULL; ++i) {
1237				if (!strcmp(s, *keywords++))
1238					return -(i+1);
1239			}
1240		}
1241		/*
1242		 * Look for a series of no more than "max_vals"
1243		 * numeric values separated by commas, in base10,
1244		 * or base16 when prefixed with "0x".
1245		 * Return a count of how many were found.
1246		 */
1247		for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
1248			vals[n] = i;
1249			while ((i = stridx(decimal, *++s)) >= 0)
1250				vals[n] = (vals[n] * 10) + i;
1251			if (*s == 'x' && !vals[n]) {
1252				while ((i = stridx(hex, *++s)) >= 0)
1253					vals[n] = (vals[n] * 0x10) + i;
1254			}
1255			if (++n == max_vals)
1256				break;
1257			if (*s == ',' || *s == ';')
1258				++s;
1259		}
1260		if (!*s)
1261			return n;
1262	}
1263	return 0;	/* zero = nothing matched */
1264}
1265
1266#ifdef CONFIG_BLK_DEV_ALI14XX
1267extern int probe_ali14xx;
1268extern int ali14xx_init(void);
1269#endif
1270#ifdef CONFIG_BLK_DEV_UMC8672
1271extern int probe_umc8672;
1272extern int umc8672_init(void);
1273#endif
1274#ifdef CONFIG_BLK_DEV_DTC2278
1275extern int probe_dtc2278;
1276extern int dtc2278_init(void);
1277#endif
1278#ifdef CONFIG_BLK_DEV_HT6560B
1279extern int probe_ht6560b;
1280extern int ht6560b_init(void);
1281#endif
1282#ifdef CONFIG_BLK_DEV_QD65XX
1283extern int probe_qd65xx;
1284extern int qd65xx_init(void);
1285#endif
1286
1287static int __initdata is_chipset_set[MAX_HWIFS];
1288
1289/*
1290 * ide_setup() gets called VERY EARLY during initialization,
1291 * to handle kernel "command line" strings beginning with "hdx=" or "ide".
1292 *
1293 * Remember to update Documentation/ide.txt if you change something here.
1294 */
1295static int __init ide_setup(char *s)
1296{
1297	int i, vals[3];
1298	ide_hwif_t *hwif;
1299	ide_drive_t *drive;
1300	unsigned int hw, unit;
1301	const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
1302	const char max_hwif  = '0' + (MAX_HWIFS - 1);
1303
1304
1305	if (strncmp(s,"hd",2) == 0 && s[2] == '=')	/* hd= is for hd.c   */
1306		return 0;				/* driver and not us */
1307
1308	if (strncmp(s,"ide",3) && strncmp(s,"idebus",6) && strncmp(s,"hd",2))
1309		return 0;
1310
1311	printk(KERN_INFO "ide_setup: %s", s);
1312	init_ide_data ();
1313
1314#ifdef CONFIG_BLK_DEV_IDEDOUBLER
1315	if (!strcmp(s, "ide=doubler")) {
1316		extern int ide_doubler;
1317
1318		printk(" : Enabled support for IDE doublers\n");
1319		ide_doubler = 1;
1320		return 1;
1321	}
1322#endif /* CONFIG_BLK_DEV_IDEDOUBLER */
1323
1324	if (!strcmp(s, "ide=nodma")) {
1325		printk(" : Prevented DMA\n");
1326		noautodma = 1;
1327		return 1;
1328	}
1329
1330#ifdef CONFIG_IDEPCI_PCIBUS_ORDER
1331	if (!strcmp(s, "ide=reverse")) {
1332		ide_scan_direction = 1;
1333		printk(" : Enabled support for IDE inverse scan order.\n");
1334		return 1;
1335	}
1336#endif
1337
1338#ifdef CONFIG_BLK_DEV_IDEACPI
1339	if (!strcmp(s, "ide=noacpi")) {
1340		//printk(" : Disable IDE ACPI support.\n");
1341		ide_noacpi = 1;
1342		return 1;
1343	}
1344	if (!strcmp(s, "ide=acpigtf")) {
1345		//printk(" : Enable IDE ACPI _GTF support.\n");
1346		ide_noacpitfs = 0;
1347		return 1;
1348	}
1349	if (!strcmp(s, "ide=acpionboot")) {
1350		//printk(" : Call IDE ACPI methods on boot.\n");
1351		ide_noacpionboot = 0;
1352		return 1;
1353	}
1354#endif /* CONFIG_BLK_DEV_IDEACPI */
1355
1356	/*
1357	 * Look for drive options:  "hdx="
1358	 */
1359	if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
1360		const char *hd_words[] = {
1361			"none", "noprobe", "nowerr", "cdrom", "minus5",
1362			"autotune", "noautotune", "minus8", "swapdata", "bswap",
1363			"noflush", "remap", "remap63", "scsi", NULL };
1364		unit = s[2] - 'a';
1365		hw   = unit / MAX_DRIVES;
1366		unit = unit % MAX_DRIVES;
1367		hwif = &ide_hwifs[hw];
1368		drive = &hwif->drives[unit];
1369		if (strncmp(s + 4, "ide-", 4) == 0) {
1370			strlcpy(drive->driver_req, s + 4, sizeof(drive->driver_req));
1371			goto done;
1372		}
1373		switch (match_parm(&s[3], hd_words, vals, 3)) {
1374			case -1: /* "none" */
1375			case -2: /* "noprobe" */
1376				drive->noprobe = 1;
1377				goto done;
1378			case -3: /* "nowerr" */
1379				drive->bad_wstat = BAD_R_STAT;
1380				hwif->noprobe = 0;
1381				goto done;
1382			case -4: /* "cdrom" */
1383				drive->present = 1;
1384				drive->media = ide_cdrom;
1385				/* an ATAPI device ignores DRDY */
1386				drive->ready_stat = 0;
1387				hwif->noprobe = 0;
1388				goto done;
1389			case -6: /* "autotune" */
1390				drive->autotune = IDE_TUNE_AUTO;
1391				goto obsolete_option;
1392			case -7: /* "noautotune" */
1393				drive->autotune = IDE_TUNE_NOAUTO;
1394				goto obsolete_option;
1395			case -9: /* "swapdata" */
1396			case -10: /* "bswap" */
1397				drive->bswap = 1;
1398				goto done;
1399			case -11: /* noflush */
1400				drive->noflush = 1;
1401				goto done;
1402			case -12: /* "remap" */
1403				drive->remap_0_to_1 = 1;
1404				goto done;
1405			case -13: /* "remap63" */
1406				drive->sect0 = 63;
1407				goto done;
1408			case -14: /* "scsi" */
1409				drive->scsi = 1;
1410				goto done;
1411			case 3: /* cyl,head,sect */
1412				drive->media	= ide_disk;
1413				drive->ready_stat = READY_STAT;
1414				drive->cyl	= drive->bios_cyl  = vals[0];
1415				drive->head	= drive->bios_head = vals[1];
1416				drive->sect	= drive->bios_sect = vals[2];
1417				drive->present	= 1;
1418				drive->forced_geom = 1;
1419				hwif->noprobe = 0;
1420				goto done;
1421			default:
1422				goto bad_option;
1423		}
1424	}
1425
1426	if (s[0] != 'i' || s[1] != 'd' || s[2] != 'e')
1427		goto bad_option;
1428	/*
1429	 * Look for bus speed option:  "idebus="
1430	 */
1431	if (s[3] == 'b' && s[4] == 'u' && s[5] == 's') {
1432		if (match_parm(&s[6], NULL, vals, 1) != 1)
1433			goto bad_option;
1434		if (vals[0] >= 20 && vals[0] <= 66) {
1435			idebus_parameter = vals[0];
1436		} else
1437			printk(" -- BAD BUS SPEED! Expected value from 20 to 66");
1438		goto done;
1439	}
1440	/*
1441	 * Look for interface options:  "idex="
1442	 */
1443	if (s[3] >= '0' && s[3] <= max_hwif) {
1444		/*
1445		 * Be VERY CAREFUL changing this: note hardcoded indexes below
1446		 * (-8, -9, -10) are reserved to ease the hardcoding.
1447		 */
1448		static const char *ide_words[] = {
1449			"noprobe", "serialize", "minus3", "minus4",
1450			"reset", "dma", "ata66", "minus8", "minus9",
1451			"minus10", "four", "qd65xx", "ht6560b", "cmd640_vlb",
1452			"dtc2278", "umc8672", "ali14xx", NULL };
1453		hw = s[3] - '0';
1454		hwif = &ide_hwifs[hw];
1455		i = match_parm(&s[4], ide_words, vals, 3);
1456
1457		/*
1458		 * Cryptic check to ensure chipset not already set for hwif.
1459		 * Note: we can't depend on hwif->chipset here.
1460		 */
1461		if ((i >= -18 && i <= -11) || (i > 0 && i <= 3)) {
1462			/* chipset already specified */
1463			if (is_chipset_set[hw])
1464				goto bad_option;
1465			if (i > -18 && i <= -11) {
1466				/* these drivers are for "ide0=" only */
1467				if (hw != 0)
1468					goto bad_hwif;
1469				/* chipset already specified for 2nd port */
1470				if (is_chipset_set[hw+1])
1471					goto bad_option;
1472			}
1473			is_chipset_set[hw] = 1;
1474			printk("\n");
1475		}
1476
1477		switch (i) {
1478#ifdef CONFIG_BLK_DEV_ALI14XX
1479			case -17: /* "ali14xx" */
1480				probe_ali14xx = 1;
1481				goto done;
1482#endif
1483#ifdef CONFIG_BLK_DEV_UMC8672
1484			case -16: /* "umc8672" */
1485				probe_umc8672 = 1;
1486				goto done;
1487#endif
1488#ifdef CONFIG_BLK_DEV_DTC2278
1489			case -15: /* "dtc2278" */
1490				probe_dtc2278 = 1;
1491				goto done;
1492#endif
1493#ifdef CONFIG_BLK_DEV_CMD640
1494			case -14: /* "cmd640_vlb" */
1495			{
1496				extern int cmd640_vlb; /* flag for cmd640.c */
1497				cmd640_vlb = 1;
1498				goto done;
1499			}
1500#endif
1501#ifdef CONFIG_BLK_DEV_HT6560B
1502			case -13: /* "ht6560b" */
1503				probe_ht6560b = 1;
1504				goto done;
1505#endif
1506#ifdef CONFIG_BLK_DEV_QD65XX
1507			case -12: /* "qd65xx" */
1508				probe_qd65xx = 1;
1509				goto done;
1510#endif
1511#ifdef CONFIG_BLK_DEV_4DRIVES
1512			case -11: /* "four" drives on one set of ports */
1513			{
1514				ide_hwif_t *mate = &ide_hwifs[hw^1];
1515				mate->drives[0].select.all ^= 0x20;
1516				mate->drives[1].select.all ^= 0x20;
1517				hwif->chipset = mate->chipset = ide_4drives;
1518				mate->irq = hwif->irq;
1519				memcpy(mate->io_ports, hwif->io_ports, sizeof(hwif->io_ports));
1520				hwif->mate = mate;
1521				mate->mate = hwif;
1522				hwif->serialized = mate->serialized = 1;
1523				goto obsolete_option;
1524			}
1525#endif /* CONFIG_BLK_DEV_4DRIVES */
1526			case -10: /* minus10 */
1527			case -9: /* minus9 */
1528			case -8: /* minus8 */
1529			case -4:
1530			case -3:
1531				goto bad_option;
1532			case -7: /* ata66 */
1533#ifdef CONFIG_BLK_DEV_IDEPCI
1534				hwif->udma_four = 1;
1535				goto obsolete_option;
1536#else
1537				goto bad_hwif;
1538#endif
1539			case -6: /* dma */
1540				hwif->autodma = 1;
1541				goto obsolete_option;
1542			case -5: /* "reset" */
1543				hwif->reset = 1;
1544				goto obsolete_option;
1545			case -2: /* "serialize" */
1546				hwif->mate = &ide_hwifs[hw^1];
1547				hwif->mate->mate = hwif;
1548				hwif->serialized = hwif->mate->serialized = 1;
1549				goto obsolete_option;
1550
1551			case -1: /* "noprobe" */
1552				hwif->noprobe = 1;
1553				goto done;
1554
1555			case 1:	/* base */
1556				vals[1] = vals[0] + 0x206; /* default ctl */
1557			case 2: /* base,ctl */
1558				vals[2] = 0;	/* default irq = probe for it */
1559			case 3: /* base,ctl,irq */
1560				hwif->hw.irq = vals[2];
1561				ide_init_hwif_ports(&hwif->hw, (unsigned long) vals[0], (unsigned long) vals[1], &hwif->irq);
1562				memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->io_ports));
1563				hwif->irq      = vals[2];
1564				hwif->noprobe  = 0;
1565				hwif->chipset  = ide_forced;
1566				goto obsolete_option;
1567
1568			case 0: goto bad_option;
1569			default:
1570				printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
1571				return 1;
1572		}
1573	}
1574bad_option:
1575	printk(" -- BAD OPTION\n");
1576	return 1;
1577obsolete_option:
1578	printk(" -- OBSOLETE OPTION, WILL BE REMOVED SOON!\n");
1579	return 1;
1580bad_hwif:
1581	printk("-- NOT SUPPORTED ON ide%d", hw);
1582done:
1583	printk("\n");
1584	return 1;
1585}
1586
1587extern void __init pnpide_init(void);
1588extern void __exit pnpide_exit(void);
1589extern void __init h8300_ide_init(void);
1590
1591/*
1592 * probe_for_hwifs() finds/initializes "known" IDE interfaces
1593 */
1594static void __init probe_for_hwifs (void)
1595{
1596#ifdef CONFIG_IDEPCI_PCIBUS_ORDER
1597	ide_scan_pcibus(ide_scan_direction);
1598#endif
1599
1600#ifdef CONFIG_ETRAX_IDE
1601	{
1602		extern void init_e100_ide(void);
1603		init_e100_ide();
1604	}
1605#endif /* CONFIG_ETRAX_IDE */
1606#ifdef CONFIG_BLK_DEV_CMD640
1607	{
1608		extern void ide_probe_for_cmd640x(void);
1609		ide_probe_for_cmd640x();
1610	}
1611#endif /* CONFIG_BLK_DEV_CMD640 */
1612#ifdef CONFIG_BLK_DEV_IDE_PMAC
1613	{
1614		extern int pmac_ide_probe(void);
1615		(void)pmac_ide_probe();
1616	}
1617#endif /* CONFIG_BLK_DEV_IDE_PMAC */
1618#ifdef CONFIG_BLK_DEV_GAYLE
1619	{
1620		extern void gayle_init(void);
1621		gayle_init();
1622	}
1623#endif /* CONFIG_BLK_DEV_GAYLE */
1624#ifdef CONFIG_BLK_DEV_FALCON_IDE
1625	{
1626		extern void falconide_init(void);
1627		falconide_init();
1628	}
1629#endif /* CONFIG_BLK_DEV_FALCON_IDE */
1630#ifdef CONFIG_BLK_DEV_MAC_IDE
1631	{
1632		extern void macide_init(void);
1633		macide_init();
1634	}
1635#endif /* CONFIG_BLK_DEV_MAC_IDE */
1636#ifdef CONFIG_BLK_DEV_Q40IDE
1637	{
1638		extern void q40ide_init(void);
1639		q40ide_init();
1640	}
1641#endif /* CONFIG_BLK_DEV_Q40IDE */
1642#ifdef CONFIG_BLK_DEV_BUDDHA
1643	{
1644		extern void buddha_init(void);
1645		buddha_init();
1646	}
1647#endif /* CONFIG_BLK_DEV_BUDDHA */
1648#ifdef CONFIG_BLK_DEV_IDEPNP
1649	pnpide_init();
1650#endif
1651#ifdef CONFIG_H8300
1652	h8300_ide_init();
1653#endif
1654}
1655
1656/*
1657 * Probe module
1658 */
1659
1660EXPORT_SYMBOL(ide_lock);
1661
1662static int ide_bus_match(struct device *dev, struct device_driver *drv)
1663{
1664	return 1;
1665}
1666
1667static char *media_string(ide_drive_t *drive)
1668{
1669	switch (drive->media) {
1670	case ide_disk:
1671		return "disk";
1672	case ide_cdrom:
1673		return "cdrom";
1674	case ide_tape:
1675		return "tape";
1676	case ide_floppy:
1677		return "floppy";
1678	case ide_optical:
1679		return "optical";
1680	default:
1681		return "UNKNOWN";
1682	}
1683}
1684
1685static ssize_t media_show(struct device *dev, struct device_attribute *attr, char *buf)
1686{
1687	ide_drive_t *drive = to_ide_device(dev);
1688	return sprintf(buf, "%s\n", media_string(drive));
1689}
1690
1691static ssize_t drivename_show(struct device *dev, struct device_attribute *attr, char *buf)
1692{
1693	ide_drive_t *drive = to_ide_device(dev);
1694	return sprintf(buf, "%s\n", drive->name);
1695}
1696
1697static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
1698{
1699	ide_drive_t *drive = to_ide_device(dev);
1700	return sprintf(buf, "ide:m-%s\n", media_string(drive));
1701}
1702
1703static struct device_attribute ide_dev_attrs[] = {
1704	__ATTR_RO(media),
1705	__ATTR_RO(drivename),
1706	__ATTR_RO(modalias),
1707	__ATTR_NULL
1708};
1709
1710static int ide_uevent(struct device *dev, char **envp, int num_envp,
1711		      char *buffer, int buffer_size)
1712{
1713	ide_drive_t *drive = to_ide_device(dev);
1714	int i = 0;
1715	int length = 0;
1716
1717	add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
1718		       "MEDIA=%s", media_string(drive));
1719	add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
1720		       "DRIVENAME=%s", drive->name);
1721	add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
1722		       "MODALIAS=ide:m-%s", media_string(drive));
1723	envp[i] = NULL;
1724	return 0;
1725}
1726
1727static int generic_ide_probe(struct device *dev)
1728{
1729	ide_drive_t *drive = to_ide_device(dev);
1730	ide_driver_t *drv = to_ide_driver(dev->driver);
1731
1732	return drv->probe ? drv->probe(drive) : -ENODEV;
1733}
1734
1735static int generic_ide_remove(struct device *dev)
1736{
1737	ide_drive_t *drive = to_ide_device(dev);
1738	ide_driver_t *drv = to_ide_driver(dev->driver);
1739
1740	if (drv->remove)
1741		drv->remove(drive);
1742
1743	return 0;
1744}
1745
1746static void generic_ide_shutdown(struct device *dev)
1747{
1748	ide_drive_t *drive = to_ide_device(dev);
1749	ide_driver_t *drv = to_ide_driver(dev->driver);
1750
1751	if (dev->driver && drv->shutdown)
1752		drv->shutdown(drive);
1753}
1754
1755struct bus_type ide_bus_type = {
1756	.name		= "ide",
1757	.match		= ide_bus_match,
1758	.uevent		= ide_uevent,
1759	.probe		= generic_ide_probe,
1760	.remove		= generic_ide_remove,
1761	.shutdown	= generic_ide_shutdown,
1762	.dev_attrs	= ide_dev_attrs,
1763	.suspend	= generic_ide_suspend,
1764	.resume		= generic_ide_resume,
1765};
1766
1767EXPORT_SYMBOL_GPL(ide_bus_type);
1768
1769/*
1770 * This is gets invoked once during initialization, to set *everything* up
1771 */
1772static int __init ide_init(void)
1773{
1774	int ret;
1775
1776	printk(KERN_INFO "Uniform Multi-Platform E-IDE driver " REVISION "\n");
1777	system_bus_speed = ide_system_bus_speed();
1778
1779	ret = bus_register(&ide_bus_type);
1780	if (ret < 0) {
1781		printk(KERN_WARNING "IDE: bus_register error: %d\n", ret);
1782		return ret;
1783	}
1784
1785	init_ide_data();
1786
1787	proc_ide_create();
1788
1789#ifdef CONFIG_BLK_DEV_ALI14XX
1790	if (probe_ali14xx)
1791		(void)ali14xx_init();
1792#endif
1793#ifdef CONFIG_BLK_DEV_UMC8672
1794	if (probe_umc8672)
1795		(void)umc8672_init();
1796#endif
1797#ifdef CONFIG_BLK_DEV_DTC2278
1798	if (probe_dtc2278)
1799		(void)dtc2278_init();
1800#endif
1801#ifdef CONFIG_BLK_DEV_HT6560B
1802	if (probe_ht6560b)
1803		(void)ht6560b_init();
1804#endif
1805#ifdef CONFIG_BLK_DEV_QD65XX
1806	if (probe_qd65xx)
1807		(void)qd65xx_init();
1808#endif
1809
1810	/* Probe for special PCI and other "known" interface chipsets. */
1811	probe_for_hwifs();
1812
1813	return 0;
1814}
1815
1816#ifdef MODULE
1817static char *options = NULL;
1818module_param(options, charp, 0);
1819MODULE_LICENSE("GPL");
1820
1821static void __init parse_options (char *line)
1822{
1823	char *next = line;
1824
1825	if (line == NULL || !*line)
1826		return;
1827	while ((line = next) != NULL) {
1828 		if ((next = strchr(line,' ')) != NULL)
1829			*next++ = 0;
1830		if (!ide_setup(line))
1831			printk (KERN_INFO "Unknown option '%s'\n", line);
1832	}
1833}
1834
1835int __init init_module (void)
1836{
1837	parse_options(options);
1838	return ide_init();
1839}
1840
1841void __exit cleanup_module (void)
1842{
1843	int index;
1844
1845	for (index = 0; index < MAX_HWIFS; ++index)
1846		ide_unregister(index);
1847
1848#ifdef CONFIG_BLK_DEV_IDEPNP
1849	pnpide_exit();
1850#endif
1851
1852	proc_ide_destroy();
1853
1854	bus_unregister(&ide_bus_type);
1855}
1856
1857#else /* !MODULE */
1858
1859__setup("", ide_setup);
1860
1861module_init(ide_init);
1862
1863#endif /* MODULE */
1864